blob: c9732acd59ff33570ba05e89eec782531d520fe7 [file] [log] [blame]
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001/* Copyright (C) 2011-2012 B.A.T.M.A.N. contributors:
Simon Wunderlich23721382012-01-22 20:00:19 +01002 *
3 * Simon Wunderlich
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
Simon Wunderlich23721382012-01-22 20:00:19 +010018 */
19
20#include "main.h"
21#include "hash.h"
22#include "hard-interface.h"
23#include "originator.h"
24#include "bridge_loop_avoidance.h"
Simon Wunderlich20ff9d52012-01-22 20:00:23 +010025#include "translation-table.h"
Simon Wunderlich23721382012-01-22 20:00:19 +010026#include "send.h"
27
28#include <linux/etherdevice.h>
29#include <linux/crc16.h>
30#include <linux/if_arp.h>
31#include <net/arp.h>
32#include <linux/if_vlan.h>
33
Sven Eckelmann3b300de2012-05-12 18:33:53 +020034static const uint8_t batadv_announce_mac[4] = {0x43, 0x05, 0x43, 0x05};
Simon Wunderlich23721382012-01-22 20:00:19 +010035
Sven Eckelmann3b300de2012-05-12 18:33:53 +020036static void batadv_bla_periodic_work(struct work_struct *work);
Sven Eckelmann56303d32012-06-05 22:31:31 +020037static void batadv_bla_send_announce(struct batadv_priv *bat_priv,
38 struct batadv_backbone_gw *backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +010039
40/* return the index of the claim */
Sven Eckelmann3b300de2012-05-12 18:33:53 +020041static inline uint32_t batadv_choose_claim(const void *data, uint32_t size)
Simon Wunderlich23721382012-01-22 20:00:19 +010042{
43 const unsigned char *key = data;
44 uint32_t hash = 0;
45 size_t i;
46
47 for (i = 0; i < ETH_ALEN + sizeof(short); i++) {
48 hash += key[i];
49 hash += (hash << 10);
50 hash ^= (hash >> 6);
51 }
52
53 hash += (hash << 3);
54 hash ^= (hash >> 11);
55 hash += (hash << 15);
56
57 return hash % size;
58}
59
60/* return the index of the backbone gateway */
Sven Eckelmann3b300de2012-05-12 18:33:53 +020061static inline uint32_t batadv_choose_backbone_gw(const void *data,
62 uint32_t size)
Simon Wunderlich23721382012-01-22 20:00:19 +010063{
64 const unsigned char *key = data;
65 uint32_t hash = 0;
66 size_t i;
67
68 for (i = 0; i < ETH_ALEN + sizeof(short); i++) {
69 hash += key[i];
70 hash += (hash << 10);
71 hash ^= (hash >> 6);
72 }
73
74 hash += (hash << 3);
75 hash ^= (hash >> 11);
76 hash += (hash << 15);
77
78 return hash % size;
79}
80
81
82/* compares address and vid of two backbone gws */
Sven Eckelmann3b300de2012-05-12 18:33:53 +020083static int batadv_compare_backbone_gw(const struct hlist_node *node,
84 const void *data2)
Simon Wunderlich23721382012-01-22 20:00:19 +010085{
Sven Eckelmann56303d32012-06-05 22:31:31 +020086 const void *data1 = container_of(node, struct batadv_backbone_gw,
Simon Wunderlich23721382012-01-22 20:00:19 +010087 hash_entry);
88
89 return (memcmp(data1, data2, ETH_ALEN + sizeof(short)) == 0 ? 1 : 0);
90}
91
92/* compares address and vid of two claims */
Sven Eckelmann3b300de2012-05-12 18:33:53 +020093static int batadv_compare_claim(const struct hlist_node *node,
94 const void *data2)
Simon Wunderlich23721382012-01-22 20:00:19 +010095{
Sven Eckelmann56303d32012-06-05 22:31:31 +020096 const void *data1 = container_of(node, struct batadv_claim,
Simon Wunderlich23721382012-01-22 20:00:19 +010097 hash_entry);
98
99 return (memcmp(data1, data2, ETH_ALEN + sizeof(short)) == 0 ? 1 : 0);
100}
101
102/* free a backbone gw */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200103static void batadv_backbone_gw_free_ref(struct batadv_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100104{
105 if (atomic_dec_and_test(&backbone_gw->refcount))
106 kfree_rcu(backbone_gw, rcu);
107}
108
109/* finally deinitialize the claim */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200110static void batadv_claim_free_rcu(struct rcu_head *rcu)
Simon Wunderlich23721382012-01-22 20:00:19 +0100111{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200112 struct batadv_claim *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +0100113
Sven Eckelmann56303d32012-06-05 22:31:31 +0200114 claim = container_of(rcu, struct batadv_claim, rcu);
Simon Wunderlich23721382012-01-22 20:00:19 +0100115
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200116 batadv_backbone_gw_free_ref(claim->backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100117 kfree(claim);
118}
119
120/* free a claim, call claim_free_rcu if its the last reference */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200121static void batadv_claim_free_ref(struct batadv_claim *claim)
Simon Wunderlich23721382012-01-22 20:00:19 +0100122{
123 if (atomic_dec_and_test(&claim->refcount))
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200124 call_rcu(&claim->rcu, batadv_claim_free_rcu);
Simon Wunderlich23721382012-01-22 20:00:19 +0100125}
126
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200127/* @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100128 * @data: search data (may be local/static data)
129 *
130 * looks for a claim in the hash, and returns it if found
131 * or NULL otherwise.
132 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200133static struct batadv_claim *batadv_claim_hash_find(struct batadv_priv *bat_priv,
134 struct batadv_claim *data)
Simon Wunderlich23721382012-01-22 20:00:19 +0100135{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200136 struct batadv_hashtable *hash = bat_priv->claim_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100137 struct hlist_head *head;
138 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200139 struct batadv_claim *claim;
140 struct batadv_claim *claim_tmp = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +0100141 int index;
142
143 if (!hash)
144 return NULL;
145
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200146 index = batadv_choose_claim(data, hash->size);
Simon Wunderlich23721382012-01-22 20:00:19 +0100147 head = &hash->table[index];
148
149 rcu_read_lock();
150 hlist_for_each_entry_rcu(claim, node, head, hash_entry) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200151 if (!batadv_compare_claim(&claim->hash_entry, data))
Simon Wunderlich23721382012-01-22 20:00:19 +0100152 continue;
153
154 if (!atomic_inc_not_zero(&claim->refcount))
155 continue;
156
157 claim_tmp = claim;
158 break;
159 }
160 rcu_read_unlock();
161
162 return claim_tmp;
163}
164
Ben Hutchings2c530402012-07-10 10:55:09 +0000165/**
166 * batadv_backbone_hash_find - looks for a claim in the hash
167 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100168 * @addr: the address of the originator
169 * @vid: the VLAN ID
170 *
Ben Hutchings2c530402012-07-10 10:55:09 +0000171 * Returns claim if found or NULL otherwise.
Simon Wunderlich23721382012-01-22 20:00:19 +0100172 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200173static struct batadv_backbone_gw *
174batadv_backbone_hash_find(struct batadv_priv *bat_priv,
175 uint8_t *addr, short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100176{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200177 struct batadv_hashtable *hash = bat_priv->backbone_hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100178 struct hlist_head *head;
179 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200180 struct batadv_backbone_gw search_entry, *backbone_gw;
181 struct batadv_backbone_gw *backbone_gw_tmp = NULL;
Simon Wunderlich23721382012-01-22 20:00:19 +0100182 int index;
183
184 if (!hash)
185 return NULL;
186
187 memcpy(search_entry.orig, addr, ETH_ALEN);
188 search_entry.vid = vid;
189
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200190 index = batadv_choose_backbone_gw(&search_entry, hash->size);
Simon Wunderlich23721382012-01-22 20:00:19 +0100191 head = &hash->table[index];
192
193 rcu_read_lock();
194 hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200195 if (!batadv_compare_backbone_gw(&backbone_gw->hash_entry,
196 &search_entry))
Simon Wunderlich23721382012-01-22 20:00:19 +0100197 continue;
198
199 if (!atomic_inc_not_zero(&backbone_gw->refcount))
200 continue;
201
202 backbone_gw_tmp = backbone_gw;
203 break;
204 }
205 rcu_read_unlock();
206
207 return backbone_gw_tmp;
208}
209
210/* delete all claims for a backbone */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200211static void
212batadv_bla_del_backbone_claims(struct batadv_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100213{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200214 struct batadv_hashtable *hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100215 struct hlist_node *node, *node_tmp;
216 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200217 struct batadv_claim *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +0100218 int i;
219 spinlock_t *list_lock; /* protects write access to the hash lists */
220
221 hash = backbone_gw->bat_priv->claim_hash;
222 if (!hash)
223 return;
224
225 for (i = 0; i < hash->size; i++) {
226 head = &hash->table[i];
227 list_lock = &hash->list_locks[i];
228
229 spin_lock_bh(list_lock);
230 hlist_for_each_entry_safe(claim, node, node_tmp,
231 head, hash_entry) {
232
233 if (claim->backbone_gw != backbone_gw)
234 continue;
235
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200236 batadv_claim_free_ref(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100237 hlist_del_rcu(node);
238 }
239 spin_unlock_bh(list_lock);
240 }
241
242 /* all claims gone, intialize CRC */
Sven Eckelmann3964f722012-06-03 22:19:10 +0200243 backbone_gw->crc = BATADV_BLA_CRC_INIT;
Simon Wunderlich23721382012-01-22 20:00:19 +0100244}
245
Ben Hutchings2c530402012-07-10 10:55:09 +0000246/**
247 * batadv_bla_send_claim - sends a claim frame according to the provided info
248 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100249 * @orig: the mac address to be announced within the claim
250 * @vid: the VLAN ID
251 * @claimtype: the type of the claim (CLAIM, UNCLAIM, ANNOUNCE, ...)
Simon Wunderlich23721382012-01-22 20:00:19 +0100252 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200253static void batadv_bla_send_claim(struct batadv_priv *bat_priv, uint8_t *mac,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200254 short vid, int claimtype)
Simon Wunderlich23721382012-01-22 20:00:19 +0100255{
256 struct sk_buff *skb;
257 struct ethhdr *ethhdr;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200258 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +0100259 struct net_device *soft_iface;
260 uint8_t *hw_src;
Sven Eckelmann96412692012-06-05 22:31:30 +0200261 struct batadv_bla_claim_dst local_claim_dest;
Al Viro3e2f1a12012-04-22 07:47:50 +0100262 __be32 zeroip = 0;
Simon Wunderlich23721382012-01-22 20:00:19 +0100263
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200264 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +0100265 if (!primary_if)
266 return;
267
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100268 memcpy(&local_claim_dest, &bat_priv->claim_dest,
269 sizeof(local_claim_dest));
Simon Wunderlich23721382012-01-22 20:00:19 +0100270 local_claim_dest.type = claimtype;
271
272 soft_iface = primary_if->soft_iface;
273
274 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
275 /* IP DST: 0.0.0.0 */
276 zeroip,
277 primary_if->soft_iface,
278 /* IP SRC: 0.0.0.0 */
279 zeroip,
280 /* Ethernet DST: Broadcast */
281 NULL,
282 /* Ethernet SRC/HW SRC: originator mac */
283 primary_if->net_dev->dev_addr,
Simon Wunderlich99e966f2012-06-23 12:34:17 +0200284 /* HW DST: FF:43:05:XX:YY:YY
Simon Wunderlich23721382012-01-22 20:00:19 +0100285 * with XX = claim type
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100286 * and YY:YY = group id
Simon Wunderlich23721382012-01-22 20:00:19 +0100287 */
288 (uint8_t *)&local_claim_dest);
289
290 if (!skb)
291 goto out;
292
293 ethhdr = (struct ethhdr *)skb->data;
Antonio Quartulli0d125072012-02-18 11:27:34 +0100294 hw_src = (uint8_t *)ethhdr + ETH_HLEN + sizeof(struct arphdr);
Simon Wunderlich23721382012-01-22 20:00:19 +0100295
296 /* now we pretend that the client would have sent this ... */
297 switch (claimtype) {
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200298 case BATADV_CLAIM_TYPE_CLAIM:
Simon Wunderlich23721382012-01-22 20:00:19 +0100299 /* normal claim frame
300 * set Ethernet SRC to the clients mac
301 */
302 memcpy(ethhdr->h_source, mac, ETH_ALEN);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200303 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200304 "bla_send_claim(): CLAIM %pM on vid %d\n", mac, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100305 break;
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200306 case BATADV_CLAIM_TYPE_UNCLAIM:
Simon Wunderlich23721382012-01-22 20:00:19 +0100307 /* unclaim frame
308 * set HW SRC to the clients mac
309 */
310 memcpy(hw_src, mac, ETH_ALEN);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200311 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200312 "bla_send_claim(): UNCLAIM %pM on vid %d\n", mac,
313 vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100314 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200315 case BATADV_CLAIM_TYPE_ANNOUNCE:
Simon Wunderlich23721382012-01-22 20:00:19 +0100316 /* announcement frame
317 * set HW SRC to the special mac containg the crc
318 */
319 memcpy(hw_src, mac, ETH_ALEN);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200320 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200321 "bla_send_claim(): ANNOUNCE of %pM on vid %d\n",
322 ethhdr->h_source, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100323 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200324 case BATADV_CLAIM_TYPE_REQUEST:
Simon Wunderlich23721382012-01-22 20:00:19 +0100325 /* request frame
Simon Wunderlich99e966f2012-06-23 12:34:17 +0200326 * set HW SRC and header destination to the receiving backbone
327 * gws mac
Simon Wunderlich23721382012-01-22 20:00:19 +0100328 */
329 memcpy(hw_src, mac, ETH_ALEN);
330 memcpy(ethhdr->h_dest, mac, ETH_ALEN);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200331 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200332 "bla_send_claim(): REQUEST of %pM to %pMon vid %d\n",
333 ethhdr->h_source, ethhdr->h_dest, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100334 break;
335
336 }
337
338 if (vid != -1)
339 skb = vlan_insert_tag(skb, vid);
340
341 skb_reset_mac_header(skb);
342 skb->protocol = eth_type_trans(skb, soft_iface);
Marek Lindner1c9b0552012-06-23 11:47:53 +0200343 batadv_inc_counter(bat_priv, BATADV_CNT_RX);
344 batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
345 skb->len + ETH_HLEN);
Simon Wunderlich23721382012-01-22 20:00:19 +0100346 soft_iface->last_rx = jiffies;
347
348 netif_rx(skb);
349out:
350 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200351 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +0100352}
353
Ben Hutchings2c530402012-07-10 10:55:09 +0000354/**
355 * batadv_bla_get_backbone_gw
356 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100357 * @orig: the mac address of the originator
358 * @vid: the VLAN ID
359 *
360 * searches for the backbone gw or creates a new one if it could not
361 * be found.
362 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200363static struct batadv_backbone_gw *
364batadv_bla_get_backbone_gw(struct batadv_priv *bat_priv, uint8_t *orig,
365 short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100366{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200367 struct batadv_backbone_gw *entry;
368 struct batadv_orig_node *orig_node;
Simon Wunderlich23721382012-01-22 20:00:19 +0100369 int hash_added;
370
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200371 entry = batadv_backbone_hash_find(bat_priv, orig, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100372
373 if (entry)
374 return entry;
375
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200376 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200377 "bla_get_backbone_gw(): not found (%pM, %d), creating new entry\n",
378 orig, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100379
380 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
381 if (!entry)
382 return NULL;
383
384 entry->vid = vid;
385 entry->lasttime = jiffies;
Sven Eckelmann3964f722012-06-03 22:19:10 +0200386 entry->crc = BATADV_BLA_CRC_INIT;
Simon Wunderlich23721382012-01-22 20:00:19 +0100387 entry->bat_priv = bat_priv;
388 atomic_set(&entry->request_sent, 0);
389 memcpy(entry->orig, orig, ETH_ALEN);
390
391 /* one for the hash, one for returning */
392 atomic_set(&entry->refcount, 2);
393
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200394 hash_added = batadv_hash_add(bat_priv->backbone_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200395 batadv_compare_backbone_gw,
396 batadv_choose_backbone_gw, entry,
397 &entry->hash_entry);
Simon Wunderlich23721382012-01-22 20:00:19 +0100398
399 if (unlikely(hash_added != 0)) {
400 /* hash failed, free the structure */
401 kfree(entry);
402 return NULL;
403 }
404
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100405 /* this is a gateway now, remove any tt entries */
Sven Eckelmannda641192012-05-12 13:48:56 +0200406 orig_node = batadv_orig_hash_find(bat_priv, orig);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100407 if (orig_node) {
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200408 batadv_tt_global_del_orig(bat_priv, orig_node,
409 "became a backbone gateway");
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200410 batadv_orig_node_free_ref(orig_node);
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100411 }
Simon Wunderlich23721382012-01-22 20:00:19 +0100412 return entry;
413}
414
415/* update or add the own backbone gw to make sure we announce
416 * where we receive other backbone gws
417 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200418static void
419batadv_bla_update_own_backbone_gw(struct batadv_priv *bat_priv,
420 struct batadv_hard_iface *primary_if,
421 short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100422{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200423 struct batadv_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100424
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200425 backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
426 primary_if->net_dev->dev_addr,
427 vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100428 if (unlikely(!backbone_gw))
429 return;
430
431 backbone_gw->lasttime = jiffies;
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200432 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100433}
434
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200435/* @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100436 * @vid: the vid where the request came on
437 *
438 * Repeat all of our own claims, and finally send an ANNOUNCE frame
439 * to allow the requester another check if the CRC is correct now.
440 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200441static void batadv_bla_answer_request(struct batadv_priv *bat_priv,
442 struct batadv_hard_iface *primary_if,
443 short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100444{
445 struct hlist_node *node;
446 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200447 struct batadv_hashtable *hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200448 struct batadv_claim *claim;
449 struct batadv_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100450 int i;
451
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200452 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200453 "bla_answer_request(): received a claim request, send all of our own claims again\n");
Simon Wunderlich23721382012-01-22 20:00:19 +0100454
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200455 backbone_gw = batadv_backbone_hash_find(bat_priv,
456 primary_if->net_dev->dev_addr,
457 vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100458 if (!backbone_gw)
459 return;
460
461 hash = bat_priv->claim_hash;
462 for (i = 0; i < hash->size; i++) {
463 head = &hash->table[i];
464
465 rcu_read_lock();
466 hlist_for_each_entry_rcu(claim, node, head, hash_entry) {
467 /* only own claims are interesting */
468 if (claim->backbone_gw != backbone_gw)
469 continue;
470
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200471 batadv_bla_send_claim(bat_priv, claim->addr, claim->vid,
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200472 BATADV_CLAIM_TYPE_CLAIM);
Simon Wunderlich23721382012-01-22 20:00:19 +0100473 }
474 rcu_read_unlock();
475 }
476
477 /* finally, send an announcement frame */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200478 batadv_bla_send_announce(bat_priv, backbone_gw);
479 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100480}
481
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200482/* @backbone_gw: the backbone gateway from whom we are out of sync
Simon Wunderlich23721382012-01-22 20:00:19 +0100483 *
484 * When the crc is wrong, ask the backbone gateway for a full table update.
485 * After the request, it will repeat all of his own claims and finally
486 * send an announcement claim with which we can check again.
487 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200488static void batadv_bla_send_request(struct batadv_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100489{
490 /* first, remove all old entries */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200491 batadv_bla_del_backbone_claims(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100492
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200493 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
494 "Sending REQUEST to %pM\n", backbone_gw->orig);
Simon Wunderlich23721382012-01-22 20:00:19 +0100495
496 /* send request */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200497 batadv_bla_send_claim(backbone_gw->bat_priv, backbone_gw->orig,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200498 backbone_gw->vid, BATADV_CLAIM_TYPE_REQUEST);
Simon Wunderlich23721382012-01-22 20:00:19 +0100499
500 /* no local broadcasts should be sent or received, for now. */
501 if (!atomic_read(&backbone_gw->request_sent)) {
502 atomic_inc(&backbone_gw->bat_priv->bla_num_requests);
503 atomic_set(&backbone_gw->request_sent, 1);
504 }
505}
506
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200507/* @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100508 * @backbone_gw: our backbone gateway which should be announced
509 *
510 * This function sends an announcement. It is called from multiple
511 * places.
512 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200513static void batadv_bla_send_announce(struct batadv_priv *bat_priv,
514 struct batadv_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100515{
516 uint8_t mac[ETH_ALEN];
Al Viro3e2f1a12012-04-22 07:47:50 +0100517 __be16 crc;
Simon Wunderlich23721382012-01-22 20:00:19 +0100518
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200519 memcpy(mac, batadv_announce_mac, 4);
Simon Wunderlich23721382012-01-22 20:00:19 +0100520 crc = htons(backbone_gw->crc);
Al Viro1a5852d2012-04-22 07:50:29 +0100521 memcpy(&mac[4], &crc, 2);
Simon Wunderlich23721382012-01-22 20:00:19 +0100522
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200523 batadv_bla_send_claim(bat_priv, mac, backbone_gw->vid,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200524 BATADV_CLAIM_TYPE_ANNOUNCE);
Simon Wunderlich23721382012-01-22 20:00:19 +0100525
526}
527
Ben Hutchings2c530402012-07-10 10:55:09 +0000528/**
529 * batadv_bla_add_claim - Adds a claim in the claim hash
530 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100531 * @mac: the mac address of the claim
532 * @vid: the VLAN ID of the frame
533 * @backbone_gw: the backbone gateway which claims it
Simon Wunderlich23721382012-01-22 20:00:19 +0100534 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200535static void batadv_bla_add_claim(struct batadv_priv *bat_priv,
536 const uint8_t *mac, const short vid,
537 struct batadv_backbone_gw *backbone_gw)
Simon Wunderlich23721382012-01-22 20:00:19 +0100538{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200539 struct batadv_claim *claim;
540 struct batadv_claim search_claim;
Simon Wunderlich23721382012-01-22 20:00:19 +0100541 int hash_added;
542
543 memcpy(search_claim.addr, mac, ETH_ALEN);
544 search_claim.vid = vid;
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200545 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100546
547 /* create a new claim entry if it does not exist yet. */
548 if (!claim) {
549 claim = kzalloc(sizeof(*claim), GFP_ATOMIC);
550 if (!claim)
551 return;
552
553 memcpy(claim->addr, mac, ETH_ALEN);
554 claim->vid = vid;
555 claim->lasttime = jiffies;
556 claim->backbone_gw = backbone_gw;
557
558 atomic_set(&claim->refcount, 2);
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200559 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200560 "bla_add_claim(): adding new entry %pM, vid %d to hash ...\n",
561 mac, vid);
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200562 hash_added = batadv_hash_add(bat_priv->claim_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200563 batadv_compare_claim,
564 batadv_choose_claim, claim,
565 &claim->hash_entry);
Simon Wunderlich23721382012-01-22 20:00:19 +0100566
567 if (unlikely(hash_added != 0)) {
568 /* only local changes happened. */
569 kfree(claim);
570 return;
571 }
572 } else {
573 claim->lasttime = jiffies;
574 if (claim->backbone_gw == backbone_gw)
575 /* no need to register a new backbone */
576 goto claim_free_ref;
577
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200578 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200579 "bla_add_claim(): changing ownership for %pM, vid %d\n",
580 mac, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100581
582 claim->backbone_gw->crc ^=
583 crc16(0, claim->addr, ETH_ALEN);
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200584 batadv_backbone_gw_free_ref(claim->backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100585
586 }
587 /* set (new) backbone gw */
588 atomic_inc(&backbone_gw->refcount);
589 claim->backbone_gw = backbone_gw;
590
591 backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
592 backbone_gw->lasttime = jiffies;
593
594claim_free_ref:
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200595 batadv_claim_free_ref(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100596}
597
598/* Delete a claim from the claim hash which has the
599 * given mac address and vid.
600 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200601static void batadv_bla_del_claim(struct batadv_priv *bat_priv,
602 const uint8_t *mac, const short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100603{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200604 struct batadv_claim search_claim, *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +0100605
606 memcpy(search_claim.addr, mac, ETH_ALEN);
607 search_claim.vid = vid;
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200608 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100609 if (!claim)
610 return;
611
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200612 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla_del_claim(): %pM, vid %d\n",
613 mac, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100614
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200615 batadv_hash_remove(bat_priv->claim_hash, batadv_compare_claim,
616 batadv_choose_claim, claim);
617 batadv_claim_free_ref(claim); /* reference from the hash is gone */
Simon Wunderlich23721382012-01-22 20:00:19 +0100618
619 claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
620
621 /* don't need the reference from hash_find() anymore */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200622 batadv_claim_free_ref(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +0100623}
624
625/* check for ANNOUNCE frame, return 1 if handled */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200626static int batadv_handle_announce(struct batadv_priv *bat_priv,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200627 uint8_t *an_addr, uint8_t *backbone_addr,
628 short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100629{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200630 struct batadv_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100631 uint16_t crc;
632
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200633 if (memcmp(an_addr, batadv_announce_mac, 4) != 0)
Simon Wunderlich23721382012-01-22 20:00:19 +0100634 return 0;
635
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200636 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100637
638 if (unlikely(!backbone_gw))
639 return 1;
640
641
642 /* handle as ANNOUNCE frame */
643 backbone_gw->lasttime = jiffies;
Al Viro3e2f1a12012-04-22 07:47:50 +0100644 crc = ntohs(*((__be16 *)(&an_addr[4])));
Simon Wunderlich23721382012-01-22 20:00:19 +0100645
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200646 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200647 "handle_announce(): ANNOUNCE vid %d (sent by %pM)... CRC = %04x\n",
648 vid, backbone_gw->orig, crc);
Simon Wunderlich23721382012-01-22 20:00:19 +0100649
650 if (backbone_gw->crc != crc) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200651 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200652 "handle_announce(): CRC FAILED for %pM/%d (my = %04x, sent = %04x)\n",
653 backbone_gw->orig, backbone_gw->vid,
654 backbone_gw->crc, crc);
Simon Wunderlich23721382012-01-22 20:00:19 +0100655
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200656 batadv_bla_send_request(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100657 } else {
658 /* if we have sent a request and the crc was OK,
659 * we can allow traffic again.
660 */
661 if (atomic_read(&backbone_gw->request_sent)) {
662 atomic_dec(&backbone_gw->bat_priv->bla_num_requests);
663 atomic_set(&backbone_gw->request_sent, 0);
664 }
665 }
666
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200667 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100668 return 1;
669}
670
671/* check for REQUEST frame, return 1 if handled */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200672static int batadv_handle_request(struct batadv_priv *bat_priv,
673 struct batadv_hard_iface *primary_if,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200674 uint8_t *backbone_addr,
675 struct ethhdr *ethhdr, short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100676{
677 /* check for REQUEST frame */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200678 if (!batadv_compare_eth(backbone_addr, ethhdr->h_dest))
Simon Wunderlich23721382012-01-22 20:00:19 +0100679 return 0;
680
681 /* sanity check, this should not happen on a normal switch,
682 * we ignore it in this case.
683 */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200684 if (!batadv_compare_eth(ethhdr->h_dest, primary_if->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +0100685 return 1;
686
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200687 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200688 "handle_request(): REQUEST vid %d (sent by %pM)...\n",
689 vid, ethhdr->h_source);
Simon Wunderlich23721382012-01-22 20:00:19 +0100690
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200691 batadv_bla_answer_request(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100692 return 1;
693}
694
695/* check for UNCLAIM frame, return 1 if handled */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200696static int batadv_handle_unclaim(struct batadv_priv *bat_priv,
697 struct batadv_hard_iface *primary_if,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200698 uint8_t *backbone_addr,
699 uint8_t *claim_addr, short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100700{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200701 struct batadv_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100702
703 /* unclaim in any case if it is our own */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200704 if (primary_if && batadv_compare_eth(backbone_addr,
705 primary_if->net_dev->dev_addr))
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200706 batadv_bla_send_claim(bat_priv, claim_addr, vid,
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200707 BATADV_CLAIM_TYPE_UNCLAIM);
Simon Wunderlich23721382012-01-22 20:00:19 +0100708
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200709 backbone_gw = batadv_backbone_hash_find(bat_priv, backbone_addr, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100710
711 if (!backbone_gw)
712 return 1;
713
714 /* this must be an UNCLAIM frame */
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200715 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200716 "handle_unclaim(): UNCLAIM %pM on vid %d (sent by %pM)...\n",
717 claim_addr, vid, backbone_gw->orig);
Simon Wunderlich23721382012-01-22 20:00:19 +0100718
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200719 batadv_bla_del_claim(bat_priv, claim_addr, vid);
720 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100721 return 1;
722}
723
724/* check for CLAIM frame, return 1 if handled */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200725static int batadv_handle_claim(struct batadv_priv *bat_priv,
726 struct batadv_hard_iface *primary_if,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200727 uint8_t *backbone_addr, uint8_t *claim_addr,
728 short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +0100729{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200730 struct batadv_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100731
732 /* register the gateway if not yet available, and add the claim. */
733
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200734 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100735
736 if (unlikely(!backbone_gw))
737 return 1;
738
739 /* this must be a CLAIM frame */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200740 batadv_bla_add_claim(bat_priv, claim_addr, vid, backbone_gw);
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200741 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200742 batadv_bla_send_claim(bat_priv, claim_addr, vid,
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200743 BATADV_CLAIM_TYPE_CLAIM);
Simon Wunderlich23721382012-01-22 20:00:19 +0100744
745 /* TODO: we could call something like tt_local_del() here. */
746
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200747 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100748 return 1;
749}
750
Ben Hutchings2c530402012-07-10 10:55:09 +0000751/**
752 * batadv_check_claim_group
753 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100754 * @hw_src: the Hardware source in the ARP Header
755 * @hw_dst: the Hardware destination in the ARP Header
756 * @ethhdr: pointer to the Ethernet header of the claim frame
757 *
758 * checks if it is a claim packet and if its on the same group.
759 * This function also applies the group ID of the sender
760 * if it is in the same mesh.
761 *
762 * returns:
763 * 2 - if it is a claim packet and on the same group
764 * 1 - if is a claim packet from another group
765 * 0 - if it is not a claim packet
766 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200767static int batadv_check_claim_group(struct batadv_priv *bat_priv,
768 struct batadv_hard_iface *primary_if,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200769 uint8_t *hw_src, uint8_t *hw_dst,
770 struct ethhdr *ethhdr)
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100771{
772 uint8_t *backbone_addr;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200773 struct batadv_orig_node *orig_node;
Sven Eckelmann96412692012-06-05 22:31:30 +0200774 struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100775
Sven Eckelmann96412692012-06-05 22:31:30 +0200776 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100777 bla_dst_own = &bat_priv->claim_dest;
778
779 /* check if it is a claim packet in general */
780 if (memcmp(bla_dst->magic, bla_dst_own->magic,
781 sizeof(bla_dst->magic)) != 0)
782 return 0;
783
784 /* if announcement packet, use the source,
785 * otherwise assume it is in the hw_src
786 */
787 switch (bla_dst->type) {
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200788 case BATADV_CLAIM_TYPE_CLAIM:
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100789 backbone_addr = hw_src;
790 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200791 case BATADV_CLAIM_TYPE_REQUEST:
792 case BATADV_CLAIM_TYPE_ANNOUNCE:
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200793 case BATADV_CLAIM_TYPE_UNCLAIM:
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100794 backbone_addr = ethhdr->h_source;
795 break;
796 default:
797 return 0;
798 }
799
800 /* don't accept claim frames from ourselves */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200801 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100802 return 0;
803
804 /* if its already the same group, it is fine. */
805 if (bla_dst->group == bla_dst_own->group)
806 return 2;
807
808 /* lets see if this originator is in our mesh */
Sven Eckelmannda641192012-05-12 13:48:56 +0200809 orig_node = batadv_orig_hash_find(bat_priv, backbone_addr);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100810
811 /* dont accept claims from gateways which are not in
812 * the same mesh or group.
813 */
814 if (!orig_node)
815 return 1;
816
817 /* if our mesh friends mac is bigger, use it for ourselves. */
818 if (ntohs(bla_dst->group) > ntohs(bla_dst_own->group)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200819 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200820 "taking other backbones claim group: %04x\n",
821 ntohs(bla_dst->group));
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100822 bla_dst_own->group = bla_dst->group;
823 }
824
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200825 batadv_orig_node_free_ref(orig_node);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100826
827 return 2;
828}
829
830
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200831/* @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100832 * @skb: the frame to be checked
833 *
834 * Check if this is a claim frame, and process it accordingly.
835 *
836 * returns 1 if it was a claim frame, otherwise return 0 to
837 * tell the callee that it can use the frame on its own.
838 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200839static int batadv_bla_process_claim(struct batadv_priv *bat_priv,
840 struct batadv_hard_iface *primary_if,
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200841 struct sk_buff *skb)
Simon Wunderlich23721382012-01-22 20:00:19 +0100842{
843 struct ethhdr *ethhdr;
844 struct vlan_ethhdr *vhdr;
845 struct arphdr *arphdr;
846 uint8_t *hw_src, *hw_dst;
Sven Eckelmann96412692012-06-05 22:31:30 +0200847 struct batadv_bla_claim_dst *bla_dst;
Simon Wunderlich23721382012-01-22 20:00:19 +0100848 uint16_t proto;
849 int headlen;
850 short vid = -1;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100851 int ret;
Simon Wunderlich23721382012-01-22 20:00:19 +0100852
853 ethhdr = (struct ethhdr *)skb_mac_header(skb);
854
855 if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) {
856 vhdr = (struct vlan_ethhdr *)ethhdr;
857 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
858 proto = ntohs(vhdr->h_vlan_encapsulated_proto);
859 headlen = sizeof(*vhdr);
860 } else {
861 proto = ntohs(ethhdr->h_proto);
Antonio Quartulli0d125072012-02-18 11:27:34 +0100862 headlen = ETH_HLEN;
Simon Wunderlich23721382012-01-22 20:00:19 +0100863 }
864
865 if (proto != ETH_P_ARP)
866 return 0; /* not a claim frame */
867
868 /* this must be a ARP frame. check if it is a claim. */
869
870 if (unlikely(!pskb_may_pull(skb, headlen + arp_hdr_len(skb->dev))))
871 return 0;
872
873 /* pskb_may_pull() may have modified the pointers, get ethhdr again */
874 ethhdr = (struct ethhdr *)skb_mac_header(skb);
875 arphdr = (struct arphdr *)((uint8_t *)ethhdr + headlen);
876
877 /* Check whether the ARP frame carries a valid
878 * IP information
879 */
Simon Wunderlich23721382012-01-22 20:00:19 +0100880 if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
881 return 0;
882 if (arphdr->ar_pro != htons(ETH_P_IP))
883 return 0;
884 if (arphdr->ar_hln != ETH_ALEN)
885 return 0;
886 if (arphdr->ar_pln != 4)
887 return 0;
888
889 hw_src = (uint8_t *)arphdr + sizeof(struct arphdr);
890 hw_dst = hw_src + ETH_ALEN + 4;
Sven Eckelmann96412692012-06-05 22:31:30 +0200891 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
Simon Wunderlich23721382012-01-22 20:00:19 +0100892
893 /* check if it is a claim frame. */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200894 ret = batadv_check_claim_group(bat_priv, primary_if, hw_src, hw_dst,
895 ethhdr);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100896 if (ret == 1)
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200897 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200898 "bla_process_claim(): received a claim frame from another group. From: %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n",
899 ethhdr->h_source, vid, hw_src, hw_dst);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +0100900
901 if (ret < 2)
902 return ret;
Simon Wunderlich23721382012-01-22 20:00:19 +0100903
904 /* become a backbone gw ourselves on this vlan if not happened yet */
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200905 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +0100906
907 /* check for the different types of claim frames ... */
908 switch (bla_dst->type) {
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200909 case BATADV_CLAIM_TYPE_CLAIM:
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200910 if (batadv_handle_claim(bat_priv, primary_if, hw_src,
911 ethhdr->h_source, vid))
Simon Wunderlich23721382012-01-22 20:00:19 +0100912 return 1;
913 break;
Simon Wunderlich3eb87732012-06-23 12:34:18 +0200914 case BATADV_CLAIM_TYPE_UNCLAIM:
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200915 if (batadv_handle_unclaim(bat_priv, primary_if,
916 ethhdr->h_source, hw_src, vid))
Simon Wunderlich23721382012-01-22 20:00:19 +0100917 return 1;
918 break;
919
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200920 case BATADV_CLAIM_TYPE_ANNOUNCE:
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200921 if (batadv_handle_announce(bat_priv, hw_src, ethhdr->h_source,
922 vid))
Simon Wunderlich23721382012-01-22 20:00:19 +0100923 return 1;
924 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200925 case BATADV_CLAIM_TYPE_REQUEST:
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200926 if (batadv_handle_request(bat_priv, primary_if, hw_src, ethhdr,
927 vid))
Simon Wunderlich23721382012-01-22 20:00:19 +0100928 return 1;
929 break;
930 }
931
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200932 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200933 "bla_process_claim(): ERROR - this looks like a claim frame, but is useless. eth src %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n",
934 ethhdr->h_source, vid, hw_src, hw_dst);
Simon Wunderlich23721382012-01-22 20:00:19 +0100935 return 1;
936}
937
938/* Check when we last heard from other nodes, and remove them in case of
939 * a time out, or clean all backbone gws if now is set.
940 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200941static void batadv_bla_purge_backbone_gw(struct batadv_priv *bat_priv, int now)
Simon Wunderlich23721382012-01-22 20:00:19 +0100942{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200943 struct batadv_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +0100944 struct hlist_node *node, *node_tmp;
945 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200946 struct batadv_hashtable *hash;
Simon Wunderlich23721382012-01-22 20:00:19 +0100947 spinlock_t *list_lock; /* protects write access to the hash lists */
948 int i;
949
950 hash = bat_priv->backbone_hash;
951 if (!hash)
952 return;
953
954 for (i = 0; i < hash->size; i++) {
955 head = &hash->table[i];
956 list_lock = &hash->list_locks[i];
957
958 spin_lock_bh(list_lock);
959 hlist_for_each_entry_safe(backbone_gw, node, node_tmp,
960 head, hash_entry) {
961 if (now)
962 goto purge_now;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200963 if (!batadv_has_timed_out(backbone_gw->lasttime,
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200964 BATADV_BLA_BACKBONE_TIMEOUT))
Simon Wunderlich23721382012-01-22 20:00:19 +0100965 continue;
966
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200967 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200968 "bla_purge_backbone_gw(): backbone gw %pM timed out\n",
969 backbone_gw->orig);
Simon Wunderlich23721382012-01-22 20:00:19 +0100970
971purge_now:
972 /* don't wait for the pending request anymore */
973 if (atomic_read(&backbone_gw->request_sent))
974 atomic_dec(&bat_priv->bla_num_requests);
975
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200976 batadv_bla_del_backbone_claims(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100977
978 hlist_del_rcu(node);
Sven Eckelmann3b300de2012-05-12 18:33:53 +0200979 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +0100980 }
981 spin_unlock_bh(list_lock);
982 }
983}
984
Ben Hutchings2c530402012-07-10 10:55:09 +0000985/**
986 * batadv_bla_purge_claims
987 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +0100988 * @primary_if: the selected primary interface, may be NULL if now is set
989 * @now: whether the whole hash shall be wiped now
990 *
991 * Check when we heard last time from our own claims, and remove them in case of
992 * a time out, or clean all claims if now is set
993 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200994static void batadv_bla_purge_claims(struct batadv_priv *bat_priv,
995 struct batadv_hard_iface *primary_if,
996 int now)
Simon Wunderlich23721382012-01-22 20:00:19 +0100997{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200998 struct batadv_claim *claim;
Simon Wunderlich23721382012-01-22 20:00:19 +0100999 struct hlist_node *node;
1000 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001001 struct batadv_hashtable *hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001002 int i;
1003
1004 hash = bat_priv->claim_hash;
1005 if (!hash)
1006 return;
1007
1008 for (i = 0; i < hash->size; i++) {
1009 head = &hash->table[i];
1010
1011 rcu_read_lock();
1012 hlist_for_each_entry_rcu(claim, node, head, hash_entry) {
1013 if (now)
1014 goto purge_now;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001015 if (!batadv_compare_eth(claim->backbone_gw->orig,
1016 primary_if->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +01001017 continue;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001018 if (!batadv_has_timed_out(claim->lasttime,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001019 BATADV_BLA_CLAIM_TIMEOUT))
Simon Wunderlich23721382012-01-22 20:00:19 +01001020 continue;
1021
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001022 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001023 "bla_purge_claims(): %pM, vid %d, time out\n",
1024 claim->addr, claim->vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001025
1026purge_now:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001027 batadv_handle_unclaim(bat_priv, primary_if,
1028 claim->backbone_gw->orig,
1029 claim->addr, claim->vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001030 }
1031 rcu_read_unlock();
1032 }
1033}
1034
Ben Hutchings2c530402012-07-10 10:55:09 +00001035/**
1036 * batadv_bla_update_orig_address
1037 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001038 * @primary_if: the new selected primary_if
1039 * @oldif: the old primary interface, may be NULL
1040 *
1041 * Update the backbone gateways when the own orig address changes.
Simon Wunderlich23721382012-01-22 20:00:19 +01001042 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001043void batadv_bla_update_orig_address(struct batadv_priv *bat_priv,
1044 struct batadv_hard_iface *primary_if,
1045 struct batadv_hard_iface *oldif)
Simon Wunderlich23721382012-01-22 20:00:19 +01001046{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001047 struct batadv_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +01001048 struct hlist_node *node;
1049 struct hlist_head *head;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001050 struct batadv_hashtable *hash;
Simon Wunderlich23721382012-01-22 20:00:19 +01001051 int i;
1052
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001053 /* reset bridge loop avoidance group id */
1054 bat_priv->claim_dest.group =
1055 htons(crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN));
1056
Simon Wunderlich23721382012-01-22 20:00:19 +01001057 if (!oldif) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001058 batadv_bla_purge_claims(bat_priv, NULL, 1);
1059 batadv_bla_purge_backbone_gw(bat_priv, 1);
Simon Wunderlich23721382012-01-22 20:00:19 +01001060 return;
1061 }
1062
1063 hash = bat_priv->backbone_hash;
1064 if (!hash)
1065 return;
1066
1067 for (i = 0; i < hash->size; i++) {
1068 head = &hash->table[i];
1069
1070 rcu_read_lock();
1071 hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
1072 /* own orig still holds the old value. */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001073 if (!batadv_compare_eth(backbone_gw->orig,
1074 oldif->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +01001075 continue;
1076
1077 memcpy(backbone_gw->orig,
1078 primary_if->net_dev->dev_addr, ETH_ALEN);
1079 /* send an announce frame so others will ask for our
1080 * claims and update their tables.
1081 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001082 batadv_bla_send_announce(bat_priv, backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001083 }
1084 rcu_read_unlock();
1085 }
1086}
1087
1088
1089
1090/* (re)start the timer */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001091static void batadv_bla_start_timer(struct batadv_priv *bat_priv)
Simon Wunderlich23721382012-01-22 20:00:19 +01001092{
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001093 INIT_DELAYED_WORK(&bat_priv->bla_work, batadv_bla_periodic_work);
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001094 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla_work,
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001095 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
Simon Wunderlich23721382012-01-22 20:00:19 +01001096}
1097
1098/* periodic work to do:
1099 * * purge structures when they are too old
1100 * * send announcements
1101 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001102static void batadv_bla_periodic_work(struct work_struct *work)
Simon Wunderlich23721382012-01-22 20:00:19 +01001103{
1104 struct delayed_work *delayed_work =
1105 container_of(work, struct delayed_work, work);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001106 struct batadv_priv *bat_priv;
Simon Wunderlich23721382012-01-22 20:00:19 +01001107 struct hlist_node *node;
1108 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001109 struct batadv_backbone_gw *backbone_gw;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001110 struct batadv_hashtable *hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001111 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001112 int i;
1113
Sven Eckelmann56303d32012-06-05 22:31:31 +02001114 bat_priv = container_of(delayed_work, struct batadv_priv, bla_work);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001115 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001116 if (!primary_if)
1117 goto out;
1118
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001119 batadv_bla_purge_claims(bat_priv, primary_if, 0);
1120 batadv_bla_purge_backbone_gw(bat_priv, 0);
Simon Wunderlich23721382012-01-22 20:00:19 +01001121
1122 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1123 goto out;
1124
1125 hash = bat_priv->backbone_hash;
1126 if (!hash)
1127 goto out;
1128
1129 for (i = 0; i < hash->size; i++) {
1130 head = &hash->table[i];
1131
1132 rcu_read_lock();
1133 hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001134 if (!batadv_compare_eth(backbone_gw->orig,
1135 primary_if->net_dev->dev_addr))
Simon Wunderlich23721382012-01-22 20:00:19 +01001136 continue;
1137
1138 backbone_gw->lasttime = jiffies;
1139
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001140 batadv_bla_send_announce(bat_priv, backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001141 }
1142 rcu_read_unlock();
1143 }
1144out:
1145 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001146 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001147
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001148 batadv_bla_start_timer(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001149}
1150
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001151/* The hash for claim and backbone hash receive the same key because they
1152 * are getting initialized by hash_new with the same key. Reinitializing
1153 * them with to different keys to allow nested locking without generating
1154 * lockdep warnings
1155 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001156static struct lock_class_key batadv_claim_hash_lock_class_key;
1157static struct lock_class_key batadv_backbone_hash_lock_class_key;
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001158
Simon Wunderlich23721382012-01-22 20:00:19 +01001159/* initialize all bla structures */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001160int batadv_bla_init(struct batadv_priv *bat_priv)
Simon Wunderlich23721382012-01-22 20:00:19 +01001161{
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001162 int i;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001163 uint8_t claim_dest[ETH_ALEN] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00};
Sven Eckelmann56303d32012-06-05 22:31:31 +02001164 struct batadv_hard_iface *primary_if;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001165
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001166 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hash registering\n");
Simon Wunderlich23721382012-01-22 20:00:19 +01001167
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001168 /* setting claim destination address */
1169 memcpy(&bat_priv->claim_dest.magic, claim_dest, 3);
1170 bat_priv->claim_dest.type = 0;
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001171 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001172 if (primary_if) {
1173 bat_priv->claim_dest.group =
1174 htons(crc16(0, primary_if->net_dev->dev_addr,
1175 ETH_ALEN));
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001176 batadv_hardif_free_ref(primary_if);
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001177 } else {
1178 bat_priv->claim_dest.group = 0; /* will be set later */
1179 }
1180
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001181 /* initialize the duplicate list */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001182 for (i = 0; i < BATADV_DUPLIST_SIZE; i++)
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001183 bat_priv->bcast_duplist[i].entrytime =
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001184 jiffies - msecs_to_jiffies(BATADV_DUPLIST_TIMEOUT);
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001185 bat_priv->bcast_duplist_curr = 0;
1186
Simon Wunderlich23721382012-01-22 20:00:19 +01001187 if (bat_priv->claim_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001188 return 0;
Simon Wunderlich23721382012-01-22 20:00:19 +01001189
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001190 bat_priv->claim_hash = batadv_hash_new(128);
1191 bat_priv->backbone_hash = batadv_hash_new(32);
Simon Wunderlich23721382012-01-22 20:00:19 +01001192
1193 if (!bat_priv->claim_hash || !bat_priv->backbone_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +02001194 return -ENOMEM;
Simon Wunderlich23721382012-01-22 20:00:19 +01001195
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001196 batadv_hash_set_lock_class(bat_priv->claim_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001197 &batadv_claim_hash_lock_class_key);
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001198 batadv_hash_set_lock_class(bat_priv->backbone_hash,
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001199 &batadv_backbone_hash_lock_class_key);
Sven Eckelmann5d52dad2012-03-29 12:38:20 +02001200
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001201 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hashes initialized\n");
Simon Wunderlich23721382012-01-22 20:00:19 +01001202
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001203 batadv_bla_start_timer(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +02001204 return 0;
Simon Wunderlich23721382012-01-22 20:00:19 +01001205}
1206
Ben Hutchings2c530402012-07-10 10:55:09 +00001207/**
1208 * batadv_bla_check_bcast_duplist
1209 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001210 * @bcast_packet: originator mac address
1211 * @hdr_size: maximum length of the frame
1212 *
1213 * check if it is on our broadcast list. Another gateway might
1214 * have sent the same packet because it is connected to the same backbone,
1215 * so we have to remove this duplicate.
1216 *
1217 * This is performed by checking the CRC, which will tell us
1218 * with a good chance that it is the same packet. If it is furthermore
1219 * sent by another host, drop it. We allow equal packets from
1220 * the same host however as this might be intended.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001221 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001222int batadv_bla_check_bcast_duplist(struct batadv_priv *bat_priv,
Sven Eckelmann96412692012-06-05 22:31:30 +02001223 struct batadv_bcast_packet *bcast_packet,
Sven Eckelmann08adf152012-05-12 13:38:47 +02001224 int hdr_size)
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001225{
1226 int i, length, curr;
1227 uint8_t *content;
1228 uint16_t crc;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001229 struct batadv_bcast_duplist_entry *entry;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001230
1231 length = hdr_size - sizeof(*bcast_packet);
1232 content = (uint8_t *)bcast_packet;
1233 content += sizeof(*bcast_packet);
1234
1235 /* calculate the crc ... */
1236 crc = crc16(0, content, length);
1237
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001238 for (i = 0; i < BATADV_DUPLIST_SIZE; i++) {
1239 curr = (bat_priv->bcast_duplist_curr + i) % BATADV_DUPLIST_SIZE;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001240 entry = &bat_priv->bcast_duplist[curr];
1241
1242 /* we can stop searching if the entry is too old ;
1243 * later entries will be even older
1244 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001245 if (batadv_has_timed_out(entry->entrytime,
1246 BATADV_DUPLIST_TIMEOUT))
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001247 break;
1248
1249 if (entry->crc != crc)
1250 continue;
1251
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001252 if (batadv_compare_eth(entry->orig, bcast_packet->orig))
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001253 continue;
1254
1255 /* this entry seems to match: same crc, not too old,
1256 * and from another gw. therefore return 1 to forbid it.
1257 */
1258 return 1;
1259 }
1260 /* not found, add a new entry (overwrite the oldest entry) */
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001261 curr = (bat_priv->bcast_duplist_curr + BATADV_DUPLIST_SIZE - 1);
1262 curr %= BATADV_DUPLIST_SIZE;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001263 entry = &bat_priv->bcast_duplist[curr];
1264 entry->crc = crc;
1265 entry->entrytime = jiffies;
1266 memcpy(entry->orig, bcast_packet->orig, ETH_ALEN);
1267 bat_priv->bcast_duplist_curr = curr;
1268
1269 /* allow it, its the first occurence. */
1270 return 0;
1271}
1272
1273
1274
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001275/* @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001276 * @orig: originator mac address
1277 *
1278 * check if the originator is a gateway for any VLAN ID.
1279 *
1280 * returns 1 if it is found, 0 otherwise
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001281 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001282int batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, uint8_t *orig)
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001283{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001284 struct batadv_hashtable *hash = bat_priv->backbone_hash;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001285 struct hlist_head *head;
1286 struct hlist_node *node;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001287 struct batadv_backbone_gw *backbone_gw;
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001288 int i;
1289
1290 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1291 return 0;
1292
1293 if (!hash)
1294 return 0;
1295
1296 for (i = 0; i < hash->size; i++) {
1297 head = &hash->table[i];
1298
1299 rcu_read_lock();
1300 hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001301 if (batadv_compare_eth(backbone_gw->orig, orig)) {
Simon Wunderlich20ff9d52012-01-22 20:00:23 +01001302 rcu_read_unlock();
1303 return 1;
1304 }
1305 }
1306 rcu_read_unlock();
1307 }
1308
1309 return 0;
1310}
1311
1312
Ben Hutchings2c530402012-07-10 10:55:09 +00001313/**
1314 * batadv_bla_is_backbone_gw
1315 * @skb: the frame to be checked
Simon Wunderlich23721382012-01-22 20:00:19 +01001316 * @orig_node: the orig_node of the frame
1317 * @hdr_size: maximum length of the frame
1318 *
1319 * bla_is_backbone_gw inspects the skb for the VLAN ID and returns 1
1320 * if the orig_node is also a gateway on the soft interface, otherwise it
1321 * returns 0.
Simon Wunderlich23721382012-01-22 20:00:19 +01001322 */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001323int batadv_bla_is_backbone_gw(struct sk_buff *skb,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001324 struct batadv_orig_node *orig_node, int hdr_size)
Simon Wunderlich23721382012-01-22 20:00:19 +01001325{
1326 struct ethhdr *ethhdr;
1327 struct vlan_ethhdr *vhdr;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001328 struct batadv_backbone_gw *backbone_gw;
Simon Wunderlich23721382012-01-22 20:00:19 +01001329 short vid = -1;
1330
1331 if (!atomic_read(&orig_node->bat_priv->bridge_loop_avoidance))
1332 return 0;
1333
1334 /* first, find out the vid. */
Antonio Quartulli0d125072012-02-18 11:27:34 +01001335 if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
Simon Wunderlich23721382012-01-22 20:00:19 +01001336 return 0;
1337
1338 ethhdr = (struct ethhdr *)(((uint8_t *)skb->data) + hdr_size);
1339
1340 if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) {
1341 if (!pskb_may_pull(skb, hdr_size + sizeof(struct vlan_ethhdr)))
1342 return 0;
1343
1344 vhdr = (struct vlan_ethhdr *)(((uint8_t *)skb->data) +
1345 hdr_size);
1346 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
1347 }
1348
1349 /* see if this originator is a backbone gw for this VLAN */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001350 backbone_gw = batadv_backbone_hash_find(orig_node->bat_priv,
1351 orig_node->orig, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001352 if (!backbone_gw)
1353 return 0;
1354
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001355 batadv_backbone_gw_free_ref(backbone_gw);
Simon Wunderlich23721382012-01-22 20:00:19 +01001356 return 1;
1357}
1358
1359/* free all bla structures (for softinterface free or module unload) */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001360void batadv_bla_free(struct batadv_priv *bat_priv)
Simon Wunderlich23721382012-01-22 20:00:19 +01001361{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001362 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001363
1364 cancel_delayed_work_sync(&bat_priv->bla_work);
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001365 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001366
1367 if (bat_priv->claim_hash) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001368 batadv_bla_purge_claims(bat_priv, primary_if, 1);
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001369 batadv_hash_destroy(bat_priv->claim_hash);
Simon Wunderlich23721382012-01-22 20:00:19 +01001370 bat_priv->claim_hash = NULL;
1371 }
1372 if (bat_priv->backbone_hash) {
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001373 batadv_bla_purge_backbone_gw(bat_priv, 1);
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001374 batadv_hash_destroy(bat_priv->backbone_hash);
Simon Wunderlich23721382012-01-22 20:00:19 +01001375 bat_priv->backbone_hash = NULL;
1376 }
1377 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001378 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001379}
1380
Ben Hutchings2c530402012-07-10 10:55:09 +00001381/**
1382 * batadv_bla_rx
1383 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001384 * @skb: the frame to be checked
1385 * @vid: the VLAN ID of the frame
Simon Wunderlich2d3f6cc2012-07-04 20:38:19 +02001386 * @is_bcast: the packet came in a broadcast packet type.
Simon Wunderlich23721382012-01-22 20:00:19 +01001387 *
1388 * bla_rx avoidance checks if:
1389 * * we have to race for a claim
1390 * * if the frame is allowed on the LAN
1391 *
1392 * in these cases, the skb is further handled by this function and
1393 * returns 1, otherwise it returns 0 and the caller shall further
1394 * process the skb.
Simon Wunderlich23721382012-01-22 20:00:19 +01001395 */
David S. Miller04c9f412012-07-10 23:56:33 -07001396int batadv_bla_rx(struct batadv_priv *bat_priv, struct sk_buff *skb, short vid,
1397 bool is_bcast)
Simon Wunderlich23721382012-01-22 20:00:19 +01001398{
1399 struct ethhdr *ethhdr;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001400 struct batadv_claim search_claim, *claim = NULL;
1401 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001402 int ret;
1403
1404 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1405
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001406 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001407 if (!primary_if)
1408 goto handled;
1409
1410 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1411 goto allow;
1412
1413
1414 if (unlikely(atomic_read(&bat_priv->bla_num_requests)))
1415 /* don't allow broadcasts while requests are in flight */
Simon Wunderlich2d3f6cc2012-07-04 20:38:19 +02001416 if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast)
Simon Wunderlich23721382012-01-22 20:00:19 +01001417 goto handled;
1418
1419 memcpy(search_claim.addr, ethhdr->h_source, ETH_ALEN);
1420 search_claim.vid = vid;
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001421 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001422
1423 if (!claim) {
1424 /* possible optimization: race for a claim */
1425 /* No claim exists yet, claim it for us!
1426 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001427 batadv_handle_claim(bat_priv, primary_if,
1428 primary_if->net_dev->dev_addr,
1429 ethhdr->h_source, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001430 goto allow;
1431 }
1432
1433 /* if it is our own claim ... */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001434 if (batadv_compare_eth(claim->backbone_gw->orig,
1435 primary_if->net_dev->dev_addr)) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001436 /* ... allow it in any case */
1437 claim->lasttime = jiffies;
1438 goto allow;
1439 }
1440
1441 /* if it is a broadcast ... */
Simon Wunderlich2d3f6cc2012-07-04 20:38:19 +02001442 if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast) {
1443 /* ... drop it. the responsible gateway is in charge.
1444 *
1445 * We need to check is_bcast because with the gateway
1446 * feature, broadcasts (like DHCP requests) may be sent
1447 * using a unicast packet type.
1448 */
Simon Wunderlich23721382012-01-22 20:00:19 +01001449 goto handled;
1450 } else {
1451 /* seems the client considers us as its best gateway.
1452 * send a claim and update the claim table
1453 * immediately.
1454 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001455 batadv_handle_claim(bat_priv, primary_if,
1456 primary_if->net_dev->dev_addr,
1457 ethhdr->h_source, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001458 goto allow;
1459 }
1460allow:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001461 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001462 ret = 0;
1463 goto out;
1464
1465handled:
1466 kfree_skb(skb);
1467 ret = 1;
1468
1469out:
1470 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001471 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001472 if (claim)
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001473 batadv_claim_free_ref(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001474 return ret;
1475}
1476
Ben Hutchings2c530402012-07-10 10:55:09 +00001477/**
1478 * batadv_bla_tx
1479 * @bat_priv: the bat priv with all the soft interface information
Simon Wunderlich23721382012-01-22 20:00:19 +01001480 * @skb: the frame to be checked
1481 * @vid: the VLAN ID of the frame
1482 *
1483 * bla_tx checks if:
1484 * * a claim was received which has to be processed
1485 * * the frame is allowed on the mesh
1486 *
1487 * in these cases, the skb is further handled by this function and
1488 * returns 1, otherwise it returns 0 and the caller shall further
1489 * process the skb.
Simon Wunderlich23721382012-01-22 20:00:19 +01001490 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001491int batadv_bla_tx(struct batadv_priv *bat_priv, struct sk_buff *skb, short vid)
Simon Wunderlich23721382012-01-22 20:00:19 +01001492{
1493 struct ethhdr *ethhdr;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001494 struct batadv_claim search_claim, *claim = NULL;
1495 struct batadv_hard_iface *primary_if;
Simon Wunderlich23721382012-01-22 20:00:19 +01001496 int ret = 0;
1497
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001498 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich23721382012-01-22 20:00:19 +01001499 if (!primary_if)
1500 goto out;
1501
1502 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1503 goto allow;
1504
1505 /* in VLAN case, the mac header might not be set. */
1506 skb_reset_mac_header(skb);
1507
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001508 if (batadv_bla_process_claim(bat_priv, primary_if, skb))
Simon Wunderlich23721382012-01-22 20:00:19 +01001509 goto handled;
1510
1511 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1512
1513 if (unlikely(atomic_read(&bat_priv->bla_num_requests)))
1514 /* don't allow broadcasts while requests are in flight */
1515 if (is_multicast_ether_addr(ethhdr->h_dest))
1516 goto handled;
1517
1518 memcpy(search_claim.addr, ethhdr->h_source, ETH_ALEN);
1519 search_claim.vid = vid;
1520
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001521 claim = batadv_claim_hash_find(bat_priv, &search_claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001522
1523 /* if no claim exists, allow it. */
1524 if (!claim)
1525 goto allow;
1526
1527 /* check if we are responsible. */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001528 if (batadv_compare_eth(claim->backbone_gw->orig,
1529 primary_if->net_dev->dev_addr)) {
Simon Wunderlich23721382012-01-22 20:00:19 +01001530 /* if yes, the client has roamed and we have
1531 * to unclaim it.
1532 */
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001533 batadv_handle_unclaim(bat_priv, primary_if,
1534 primary_if->net_dev->dev_addr,
1535 ethhdr->h_source, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001536 goto allow;
1537 }
1538
1539 /* check if it is a multicast/broadcast frame */
1540 if (is_multicast_ether_addr(ethhdr->h_dest)) {
1541 /* drop it. the responsible gateway has forwarded it into
1542 * the backbone network.
1543 */
1544 goto handled;
1545 } else {
1546 /* we must allow it. at least if we are
1547 * responsible for the DESTINATION.
1548 */
1549 goto allow;
1550 }
1551allow:
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001552 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
Simon Wunderlich23721382012-01-22 20:00:19 +01001553 ret = 0;
1554 goto out;
1555handled:
1556 ret = 1;
1557out:
1558 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001559 batadv_hardif_free_ref(primary_if);
Simon Wunderlich23721382012-01-22 20:00:19 +01001560 if (claim)
Sven Eckelmann3b300de2012-05-12 18:33:53 +02001561 batadv_claim_free_ref(claim);
Simon Wunderlich23721382012-01-22 20:00:19 +01001562 return ret;
1563}
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001564
Sven Eckelmann08adf152012-05-12 13:38:47 +02001565int batadv_bla_claim_table_seq_print_text(struct seq_file *seq, void *offset)
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001566{
1567 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001568 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001569 struct batadv_hashtable *hash = bat_priv->claim_hash;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001570 struct batadv_claim *claim;
1571 struct batadv_hard_iface *primary_if;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001572 struct hlist_node *node;
1573 struct hlist_head *head;
1574 uint32_t i;
1575 bool is_own;
1576 int ret = 0;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001577 uint8_t *primary_addr;
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001578
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001579 primary_if = batadv_primary_if_get_selected(bat_priv);
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001580 if (!primary_if) {
1581 ret = seq_printf(seq,
1582 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
1583 net_dev->name);
1584 goto out;
1585 }
1586
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001587 if (primary_if->if_status != BATADV_IF_ACTIVE) {
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001588 ret = seq_printf(seq,
1589 "BATMAN mesh %s disabled - primary interface not active\n",
1590 net_dev->name);
1591 goto out;
1592 }
1593
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001594 primary_addr = primary_if->net_dev->dev_addr;
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001595 seq_printf(seq,
1596 "Claims announced for the mesh %s (orig %pM, group id %04x)\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001597 net_dev->name, primary_addr,
Simon Wunderlich38ef3d12012-01-22 20:00:26 +01001598 ntohs(bat_priv->claim_dest.group));
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001599 seq_printf(seq, " %-17s %-5s %-17s [o] (%-4s)\n",
1600 "Client", "VID", "Originator", "CRC");
1601 for (i = 0; i < hash->size; i++) {
1602 head = &hash->table[i];
1603
1604 rcu_read_lock();
1605 hlist_for_each_entry_rcu(claim, node, head, hash_entry) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001606 is_own = batadv_compare_eth(claim->backbone_gw->orig,
1607 primary_addr);
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001608 seq_printf(seq, " * %pM on % 5d by %pM [%c] (%04x)\n",
1609 claim->addr, claim->vid,
1610 claim->backbone_gw->orig,
1611 (is_own ? 'x' : ' '),
1612 claim->backbone_gw->crc);
1613 }
1614 rcu_read_unlock();
1615 }
1616out:
1617 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +02001618 batadv_hardif_free_ref(primary_if);
Simon Wunderlich9bf8e4d2012-01-22 20:00:21 +01001619 return ret;
1620}
Simon Wunderlich536a23f2012-06-18 18:39:26 +02001621
1622int batadv_bla_backbone_table_seq_print_text(struct seq_file *seq, void *offset)
1623{
1624 struct net_device *net_dev = (struct net_device *)seq->private;
1625 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1626 struct batadv_hashtable *hash = bat_priv->backbone_hash;
1627 struct batadv_backbone_gw *backbone_gw;
1628 struct batadv_hard_iface *primary_if;
1629 struct hlist_node *node;
1630 struct hlist_head *head;
1631 int secs, msecs;
1632 uint32_t i;
1633 bool is_own;
1634 int ret = 0;
1635 uint8_t *primary_addr;
1636
1637 primary_if = batadv_primary_if_get_selected(bat_priv);
1638 if (!primary_if) {
1639 ret = seq_printf(seq,
1640 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
1641 net_dev->name);
1642 goto out;
1643 }
1644
1645 if (primary_if->if_status != BATADV_IF_ACTIVE) {
1646 ret = seq_printf(seq,
1647 "BATMAN mesh %s disabled - primary interface not active\n",
1648 net_dev->name);
1649 goto out;
1650 }
1651
1652 primary_addr = primary_if->net_dev->dev_addr;
1653 seq_printf(seq,
1654 "Backbones announced for the mesh %s (orig %pM, group id %04x)\n",
1655 net_dev->name, primary_addr,
1656 ntohs(bat_priv->claim_dest.group));
1657 seq_printf(seq, " %-17s %-5s %-9s (%-4s)\n",
1658 "Originator", "VID", "last seen", "CRC");
1659 for (i = 0; i < hash->size; i++) {
1660 head = &hash->table[i];
1661
1662 rcu_read_lock();
1663 hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
1664 msecs = jiffies_to_msecs(jiffies -
1665 backbone_gw->lasttime);
1666 secs = msecs / 1000;
1667 msecs = msecs % 1000;
1668
1669 is_own = batadv_compare_eth(backbone_gw->orig,
1670 primary_addr);
1671 if (is_own)
1672 continue;
1673
1674 seq_printf(seq,
1675 " * %pM on % 5d % 4i.%03is (%04x)\n",
1676 backbone_gw->orig, backbone_gw->vid,
1677 secs, msecs, backbone_gw->crc);
1678 }
1679 rcu_read_unlock();
1680 }
1681out:
1682 if (primary_if)
1683 batadv_hardif_free_ref(primary_if);
1684 return ret;
1685}