batman-adv: Don't break statements after assignment operator
[linux-2.6-block.git] / net / batman-adv / bridge_loop_avoidance.c
CommitLineData
9cfc7bd6 1/* Copyright (C) 2011-2012 B.A.T.M.A.N. contributors:
23721387
SW
2 *
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
23721387
SW
18 */
19
20#include "main.h"
21#include "hash.h"
22#include "hard-interface.h"
23#include "originator.h"
24#include "bridge_loop_avoidance.h"
20ff9d59 25#include "translation-table.h"
23721387
SW
26#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
3b300de3 34static const uint8_t batadv_announce_mac[4] = {0x43, 0x05, 0x43, 0x05};
23721387 35
3b300de3 36static void batadv_bla_periodic_work(struct work_struct *work);
56303d34
SE
37static void batadv_bla_send_announce(struct batadv_priv *bat_priv,
38 struct batadv_backbone_gw *backbone_gw);
23721387
SW
39
40/* return the index of the claim */
3b300de3 41static inline uint32_t batadv_choose_claim(const void *data, uint32_t size)
23721387
SW
42{
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 */
3b300de3
SE
61static inline uint32_t batadv_choose_backbone_gw(const void *data,
62 uint32_t size)
23721387
SW
63{
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 */
3b300de3
SE
83static int batadv_compare_backbone_gw(const struct hlist_node *node,
84 const void *data2)
23721387 85{
56303d34 86 const void *data1 = container_of(node, struct batadv_backbone_gw,
23721387
SW
87 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 */
3b300de3
SE
93static int batadv_compare_claim(const struct hlist_node *node,
94 const void *data2)
23721387 95{
56303d34 96 const void *data1 = container_of(node, struct batadv_claim,
23721387
SW
97 hash_entry);
98
99 return (memcmp(data1, data2, ETH_ALEN + sizeof(short)) == 0 ? 1 : 0);
100}
101
102/* free a backbone gw */
56303d34 103static void batadv_backbone_gw_free_ref(struct batadv_backbone_gw *backbone_gw)
23721387
SW
104{
105 if (atomic_dec_and_test(&backbone_gw->refcount))
106 kfree_rcu(backbone_gw, rcu);
107}
108
109/* finally deinitialize the claim */
3b300de3 110static void batadv_claim_free_rcu(struct rcu_head *rcu)
23721387 111{
56303d34 112 struct batadv_claim *claim;
23721387 113
56303d34 114 claim = container_of(rcu, struct batadv_claim, rcu);
23721387 115
3b300de3 116 batadv_backbone_gw_free_ref(claim->backbone_gw);
23721387
SW
117 kfree(claim);
118}
119
120/* free a claim, call claim_free_rcu if its the last reference */
56303d34 121static void batadv_claim_free_ref(struct batadv_claim *claim)
23721387
SW
122{
123 if (atomic_dec_and_test(&claim->refcount))
3b300de3 124 call_rcu(&claim->rcu, batadv_claim_free_rcu);
23721387
SW
125}
126
9cfc7bd6 127/* @bat_priv: the bat priv with all the soft interface information
23721387
SW
128 * @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 */
56303d34
SE
133static struct batadv_claim *batadv_claim_hash_find(struct batadv_priv *bat_priv,
134 struct batadv_claim *data)
23721387 135{
807736f6 136 struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
23721387
SW
137 struct hlist_head *head;
138 struct hlist_node *node;
56303d34
SE
139 struct batadv_claim *claim;
140 struct batadv_claim *claim_tmp = NULL;
23721387
SW
141 int index;
142
143 if (!hash)
144 return NULL;
145
3b300de3 146 index = batadv_choose_claim(data, hash->size);
23721387
SW
147 head = &hash->table[index];
148
149 rcu_read_lock();
150 hlist_for_each_entry_rcu(claim, node, head, hash_entry) {
3b300de3 151 if (!batadv_compare_claim(&claim->hash_entry, data))
23721387
SW
152 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
2c53040f
BH
165/**
166 * batadv_backbone_hash_find - looks for a claim in the hash
167 * @bat_priv: the bat priv with all the soft interface information
23721387
SW
168 * @addr: the address of the originator
169 * @vid: the VLAN ID
170 *
2c53040f 171 * Returns claim if found or NULL otherwise.
23721387 172 */
56303d34
SE
173static struct batadv_backbone_gw *
174batadv_backbone_hash_find(struct batadv_priv *bat_priv,
175 uint8_t *addr, short vid)
23721387 176{
807736f6 177 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
23721387
SW
178 struct hlist_head *head;
179 struct hlist_node *node;
56303d34
SE
180 struct batadv_backbone_gw search_entry, *backbone_gw;
181 struct batadv_backbone_gw *backbone_gw_tmp = NULL;
23721387
SW
182 int index;
183
184 if (!hash)
185 return NULL;
186
187 memcpy(search_entry.orig, addr, ETH_ALEN);
188 search_entry.vid = vid;
189
3b300de3 190 index = batadv_choose_backbone_gw(&search_entry, hash->size);
23721387
SW
191 head = &hash->table[index];
192
193 rcu_read_lock();
194 hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
3b300de3
SE
195 if (!batadv_compare_backbone_gw(&backbone_gw->hash_entry,
196 &search_entry))
23721387
SW
197 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 */
56303d34
SE
211static void
212batadv_bla_del_backbone_claims(struct batadv_backbone_gw *backbone_gw)
23721387 213{
5bf74e9c 214 struct batadv_hashtable *hash;
23721387
SW
215 struct hlist_node *node, *node_tmp;
216 struct hlist_head *head;
56303d34 217 struct batadv_claim *claim;
23721387
SW
218 int i;
219 spinlock_t *list_lock; /* protects write access to the hash lists */
220
807736f6 221 hash = backbone_gw->bat_priv->bla.claim_hash;
23721387
SW
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
3b300de3 236 batadv_claim_free_ref(claim);
23721387
SW
237 hlist_del_rcu(node);
238 }
239 spin_unlock_bh(list_lock);
240 }
241
242 /* all claims gone, intialize CRC */
3964f728 243 backbone_gw->crc = BATADV_BLA_CRC_INIT;
23721387
SW
244}
245
2c53040f
BH
246/**
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
23721387
SW
249 * @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, ...)
23721387 252 */
56303d34 253static void batadv_bla_send_claim(struct batadv_priv *bat_priv, uint8_t *mac,
3b300de3 254 short vid, int claimtype)
23721387
SW
255{
256 struct sk_buff *skb;
257 struct ethhdr *ethhdr;
56303d34 258 struct batadv_hard_iface *primary_if;
23721387
SW
259 struct net_device *soft_iface;
260 uint8_t *hw_src;
96412690 261 struct batadv_bla_claim_dst local_claim_dest;
3e2f1a1b 262 __be32 zeroip = 0;
23721387 263
e5d89254 264 primary_if = batadv_primary_if_get_selected(bat_priv);
23721387
SW
265 if (!primary_if)
266 return;
267
807736f6 268 memcpy(&local_claim_dest, &bat_priv->bla.claim_dest,
38ef3d1d 269 sizeof(local_claim_dest));
23721387
SW
270 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,
99e966fc 284 /* HW DST: FF:43:05:XX:YY:YY
23721387 285 * with XX = claim type
38ef3d1d 286 * and YY:YY = group id
23721387
SW
287 */
288 (uint8_t *)&local_claim_dest);
289
290 if (!skb)
291 goto out;
292
293 ethhdr = (struct ethhdr *)skb->data;
0d125074 294 hw_src = (uint8_t *)ethhdr + ETH_HLEN + sizeof(struct arphdr);
23721387
SW
295
296 /* now we pretend that the client would have sent this ... */
297 switch (claimtype) {
3eb8773e 298 case BATADV_CLAIM_TYPE_CLAIM:
23721387
SW
299 /* normal claim frame
300 * set Ethernet SRC to the clients mac
301 */
302 memcpy(ethhdr->h_source, mac, ETH_ALEN);
39c75a51 303 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1eda58bf 304 "bla_send_claim(): CLAIM %pM on vid %d\n", mac, vid);
23721387 305 break;
3eb8773e 306 case BATADV_CLAIM_TYPE_UNCLAIM:
23721387
SW
307 /* unclaim frame
308 * set HW SRC to the clients mac
309 */
310 memcpy(hw_src, mac, ETH_ALEN);
39c75a51 311 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1eda58bf
SE
312 "bla_send_claim(): UNCLAIM %pM on vid %d\n", mac,
313 vid);
23721387 314 break;
acd34afa 315 case BATADV_CLAIM_TYPE_ANNOUNCE:
23721387
SW
316 /* announcement frame
317 * set HW SRC to the special mac containg the crc
318 */
319 memcpy(hw_src, mac, ETH_ALEN);
39c75a51 320 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1eda58bf
SE
321 "bla_send_claim(): ANNOUNCE of %pM on vid %d\n",
322 ethhdr->h_source, vid);
23721387 323 break;
acd34afa 324 case BATADV_CLAIM_TYPE_REQUEST:
23721387 325 /* request frame
99e966fc
SW
326 * set HW SRC and header destination to the receiving backbone
327 * gws mac
23721387
SW
328 */
329 memcpy(hw_src, mac, ETH_ALEN);
330 memcpy(ethhdr->h_dest, mac, ETH_ALEN);
39c75a51 331 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1eda58bf
SE
332 "bla_send_claim(): REQUEST of %pM to %pMon vid %d\n",
333 ethhdr->h_source, ethhdr->h_dest, vid);
23721387
SW
334 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);
1c9b0550
ML
343 batadv_inc_counter(bat_priv, BATADV_CNT_RX);
344 batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
345 skb->len + ETH_HLEN);
23721387
SW
346 soft_iface->last_rx = jiffies;
347
348 netif_rx(skb);
349out:
350 if (primary_if)
e5d89254 351 batadv_hardif_free_ref(primary_if);
23721387
SW
352}
353
2c53040f
BH
354/**
355 * batadv_bla_get_backbone_gw
356 * @bat_priv: the bat priv with all the soft interface information
23721387
SW
357 * @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 */
56303d34
SE
363static struct batadv_backbone_gw *
364batadv_bla_get_backbone_gw(struct batadv_priv *bat_priv, uint8_t *orig,
365 short vid)
23721387 366{
56303d34
SE
367 struct batadv_backbone_gw *entry;
368 struct batadv_orig_node *orig_node;
23721387
SW
369 int hash_added;
370
3b300de3 371 entry = batadv_backbone_hash_find(bat_priv, orig, vid);
23721387
SW
372
373 if (entry)
374 return entry;
375
39c75a51 376 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1eda58bf
SE
377 "bla_get_backbone_gw(): not found (%pM, %d), creating new entry\n",
378 orig, vid);
23721387
SW
379
380 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
381 if (!entry)
382 return NULL;
383
384 entry->vid = vid;
385 entry->lasttime = jiffies;
3964f728 386 entry->crc = BATADV_BLA_CRC_INIT;
23721387
SW
387 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
807736f6 394 hash_added = batadv_hash_add(bat_priv->bla.backbone_hash,
3b300de3
SE
395 batadv_compare_backbone_gw,
396 batadv_choose_backbone_gw, entry,
397 &entry->hash_entry);
23721387
SW
398
399 if (unlikely(hash_added != 0)) {
400 /* hash failed, free the structure */
401 kfree(entry);
402 return NULL;
403 }
404
20ff9d59 405 /* this is a gateway now, remove any tt entries */
da641193 406 orig_node = batadv_orig_hash_find(bat_priv, orig);
20ff9d59 407 if (orig_node) {
08c36d3e
SE
408 batadv_tt_global_del_orig(bat_priv, orig_node,
409 "became a backbone gateway");
7d211efc 410 batadv_orig_node_free_ref(orig_node);
20ff9d59 411 }
23721387
SW
412 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 */
56303d34
SE
418static void
419batadv_bla_update_own_backbone_gw(struct batadv_priv *bat_priv,
420 struct batadv_hard_iface *primary_if,
421 short vid)
23721387 422{
56303d34 423 struct batadv_backbone_gw *backbone_gw;
23721387 424
3b300de3
SE
425 backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
426 primary_if->net_dev->dev_addr,
427 vid);
23721387
SW
428 if (unlikely(!backbone_gw))
429 return;
430
431 backbone_gw->lasttime = jiffies;
3b300de3 432 batadv_backbone_gw_free_ref(backbone_gw);
23721387
SW
433}
434
9cfc7bd6 435/* @bat_priv: the bat priv with all the soft interface information
23721387
SW
436 * @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 */
56303d34
SE
441static void batadv_bla_answer_request(struct batadv_priv *bat_priv,
442 struct batadv_hard_iface *primary_if,
443 short vid)
23721387
SW
444{
445 struct hlist_node *node;
446 struct hlist_head *head;
5bf74e9c 447 struct batadv_hashtable *hash;
56303d34
SE
448 struct batadv_claim *claim;
449 struct batadv_backbone_gw *backbone_gw;
23721387
SW
450 int i;
451
39c75a51 452 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1eda58bf 453 "bla_answer_request(): received a claim request, send all of our own claims again\n");
23721387 454
3b300de3
SE
455 backbone_gw = batadv_backbone_hash_find(bat_priv,
456 primary_if->net_dev->dev_addr,
457 vid);
23721387
SW
458 if (!backbone_gw)
459 return;
460
807736f6 461 hash = bat_priv->bla.claim_hash;
23721387
SW
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
3b300de3 471 batadv_bla_send_claim(bat_priv, claim->addr, claim->vid,
3eb8773e 472 BATADV_CLAIM_TYPE_CLAIM);
23721387
SW
473 }
474 rcu_read_unlock();
475 }
476
477 /* finally, send an announcement frame */
3b300de3
SE
478 batadv_bla_send_announce(bat_priv, backbone_gw);
479 batadv_backbone_gw_free_ref(backbone_gw);
23721387
SW
480}
481
9cfc7bd6 482/* @backbone_gw: the backbone gateway from whom we are out of sync
23721387
SW
483 *
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 */
56303d34 488static void batadv_bla_send_request(struct batadv_backbone_gw *backbone_gw)
23721387
SW
489{
490 /* first, remove all old entries */
3b300de3 491 batadv_bla_del_backbone_claims(backbone_gw);
23721387 492
39c75a51
SE
493 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
494 "Sending REQUEST to %pM\n", backbone_gw->orig);
23721387
SW
495
496 /* send request */
3b300de3 497 batadv_bla_send_claim(backbone_gw->bat_priv, backbone_gw->orig,
acd34afa 498 backbone_gw->vid, BATADV_CLAIM_TYPE_REQUEST);
23721387
SW
499
500 /* no local broadcasts should be sent or received, for now. */
501 if (!atomic_read(&backbone_gw->request_sent)) {
807736f6 502 atomic_inc(&backbone_gw->bat_priv->bla.num_requests);
23721387
SW
503 atomic_set(&backbone_gw->request_sent, 1);
504 }
505}
506
9cfc7bd6 507/* @bat_priv: the bat priv with all the soft interface information
23721387
SW
508 * @backbone_gw: our backbone gateway which should be announced
509 *
510 * This function sends an announcement. It is called from multiple
511 * places.
512 */
56303d34
SE
513static void batadv_bla_send_announce(struct batadv_priv *bat_priv,
514 struct batadv_backbone_gw *backbone_gw)
23721387
SW
515{
516 uint8_t mac[ETH_ALEN];
3e2f1a1b 517 __be16 crc;
23721387 518
3b300de3 519 memcpy(mac, batadv_announce_mac, 4);
23721387 520 crc = htons(backbone_gw->crc);
1a5852d8 521 memcpy(&mac[4], &crc, 2);
23721387 522
3b300de3 523 batadv_bla_send_claim(bat_priv, mac, backbone_gw->vid,
acd34afa 524 BATADV_CLAIM_TYPE_ANNOUNCE);
23721387
SW
525
526}
527
2c53040f
BH
528/**
529 * batadv_bla_add_claim - Adds a claim in the claim hash
530 * @bat_priv: the bat priv with all the soft interface information
23721387
SW
531 * @mac: the mac address of the claim
532 * @vid: the VLAN ID of the frame
533 * @backbone_gw: the backbone gateway which claims it
23721387 534 */
56303d34
SE
535static 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)
23721387 538{
56303d34
SE
539 struct batadv_claim *claim;
540 struct batadv_claim search_claim;
23721387
SW
541 int hash_added;
542
543 memcpy(search_claim.addr, mac, ETH_ALEN);
544 search_claim.vid = vid;
3b300de3 545 claim = batadv_claim_hash_find(bat_priv, &search_claim);
23721387
SW
546
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);
39c75a51 559 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1eda58bf
SE
560 "bla_add_claim(): adding new entry %pM, vid %d to hash ...\n",
561 mac, vid);
807736f6 562 hash_added = batadv_hash_add(bat_priv->bla.claim_hash,
3b300de3
SE
563 batadv_compare_claim,
564 batadv_choose_claim, claim,
565 &claim->hash_entry);
23721387
SW
566
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
39c75a51 578 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1eda58bf
SE
579 "bla_add_claim(): changing ownership for %pM, vid %d\n",
580 mac, vid);
23721387 581
bbb1f90e 582 claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
3b300de3 583 batadv_backbone_gw_free_ref(claim->backbone_gw);
23721387
SW
584
585 }
586 /* set (new) backbone gw */
587 atomic_inc(&backbone_gw->refcount);
588 claim->backbone_gw = backbone_gw;
589
590 backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
591 backbone_gw->lasttime = jiffies;
592
593claim_free_ref:
3b300de3 594 batadv_claim_free_ref(claim);
23721387
SW
595}
596
597/* Delete a claim from the claim hash which has the
598 * given mac address and vid.
599 */
56303d34
SE
600static void batadv_bla_del_claim(struct batadv_priv *bat_priv,
601 const uint8_t *mac, const short vid)
23721387 602{
56303d34 603 struct batadv_claim search_claim, *claim;
23721387
SW
604
605 memcpy(search_claim.addr, mac, ETH_ALEN);
606 search_claim.vid = vid;
3b300de3 607 claim = batadv_claim_hash_find(bat_priv, &search_claim);
23721387
SW
608 if (!claim)
609 return;
610
39c75a51
SE
611 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla_del_claim(): %pM, vid %d\n",
612 mac, vid);
23721387 613
807736f6 614 batadv_hash_remove(bat_priv->bla.claim_hash, batadv_compare_claim,
3b300de3
SE
615 batadv_choose_claim, claim);
616 batadv_claim_free_ref(claim); /* reference from the hash is gone */
23721387
SW
617
618 claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
619
620 /* don't need the reference from hash_find() anymore */
3b300de3 621 batadv_claim_free_ref(claim);
23721387
SW
622}
623
624/* check for ANNOUNCE frame, return 1 if handled */
56303d34 625static int batadv_handle_announce(struct batadv_priv *bat_priv,
3b300de3
SE
626 uint8_t *an_addr, uint8_t *backbone_addr,
627 short vid)
23721387 628{
56303d34 629 struct batadv_backbone_gw *backbone_gw;
23721387
SW
630 uint16_t crc;
631
3b300de3 632 if (memcmp(an_addr, batadv_announce_mac, 4) != 0)
23721387
SW
633 return 0;
634
3b300de3 635 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid);
23721387
SW
636
637 if (unlikely(!backbone_gw))
638 return 1;
639
640
641 /* handle as ANNOUNCE frame */
642 backbone_gw->lasttime = jiffies;
3e2f1a1b 643 crc = ntohs(*((__be16 *)(&an_addr[4])));
23721387 644
39c75a51 645 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1eda58bf
SE
646 "handle_announce(): ANNOUNCE vid %d (sent by %pM)... CRC = %04x\n",
647 vid, backbone_gw->orig, crc);
23721387
SW
648
649 if (backbone_gw->crc != crc) {
39c75a51 650 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
1eda58bf
SE
651 "handle_announce(): CRC FAILED for %pM/%d (my = %04x, sent = %04x)\n",
652 backbone_gw->orig, backbone_gw->vid,
653 backbone_gw->crc, crc);
23721387 654
3b300de3 655 batadv_bla_send_request(backbone_gw);
23721387
SW
656 } else {
657 /* if we have sent a request and the crc was OK,
658 * we can allow traffic again.
659 */
660 if (atomic_read(&backbone_gw->request_sent)) {
807736f6 661 atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
23721387
SW
662 atomic_set(&backbone_gw->request_sent, 0);
663 }
664 }
665
3b300de3 666 batadv_backbone_gw_free_ref(backbone_gw);
23721387
SW
667 return 1;
668}
669
670/* check for REQUEST frame, return 1 if handled */
56303d34
SE
671static int batadv_handle_request(struct batadv_priv *bat_priv,
672 struct batadv_hard_iface *primary_if,
3b300de3
SE
673 uint8_t *backbone_addr,
674 struct ethhdr *ethhdr, short vid)
23721387
SW
675{
676 /* check for REQUEST frame */
1eda58bf 677 if (!batadv_compare_eth(backbone_addr, ethhdr->h_dest))
23721387
SW
678 return 0;
679
680 /* sanity check, this should not happen on a normal switch,
681 * we ignore it in this case.
682 */
1eda58bf 683 if (!batadv_compare_eth(ethhdr->h_dest, primary_if->net_dev->dev_addr))
23721387
SW
684 return 1;
685
39c75a51 686 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1eda58bf
SE
687 "handle_request(): REQUEST vid %d (sent by %pM)...\n",
688 vid, ethhdr->h_source);
23721387 689
3b300de3 690 batadv_bla_answer_request(bat_priv, primary_if, vid);
23721387
SW
691 return 1;
692}
693
694/* check for UNCLAIM frame, return 1 if handled */
56303d34
SE
695static int batadv_handle_unclaim(struct batadv_priv *bat_priv,
696 struct batadv_hard_iface *primary_if,
3b300de3
SE
697 uint8_t *backbone_addr,
698 uint8_t *claim_addr, short vid)
23721387 699{
56303d34 700 struct batadv_backbone_gw *backbone_gw;
23721387
SW
701
702 /* unclaim in any case if it is our own */
1eda58bf
SE
703 if (primary_if && batadv_compare_eth(backbone_addr,
704 primary_if->net_dev->dev_addr))
3b300de3 705 batadv_bla_send_claim(bat_priv, claim_addr, vid,
3eb8773e 706 BATADV_CLAIM_TYPE_UNCLAIM);
23721387 707
3b300de3 708 backbone_gw = batadv_backbone_hash_find(bat_priv, backbone_addr, vid);
23721387
SW
709
710 if (!backbone_gw)
711 return 1;
712
713 /* this must be an UNCLAIM frame */
39c75a51 714 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1eda58bf
SE
715 "handle_unclaim(): UNCLAIM %pM on vid %d (sent by %pM)...\n",
716 claim_addr, vid, backbone_gw->orig);
23721387 717
3b300de3
SE
718 batadv_bla_del_claim(bat_priv, claim_addr, vid);
719 batadv_backbone_gw_free_ref(backbone_gw);
23721387
SW
720 return 1;
721}
722
723/* check for CLAIM frame, return 1 if handled */
56303d34
SE
724static int batadv_handle_claim(struct batadv_priv *bat_priv,
725 struct batadv_hard_iface *primary_if,
3b300de3
SE
726 uint8_t *backbone_addr, uint8_t *claim_addr,
727 short vid)
23721387 728{
56303d34 729 struct batadv_backbone_gw *backbone_gw;
23721387
SW
730
731 /* register the gateway if not yet available, and add the claim. */
732
3b300de3 733 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid);
23721387
SW
734
735 if (unlikely(!backbone_gw))
736 return 1;
737
738 /* this must be a CLAIM frame */
3b300de3 739 batadv_bla_add_claim(bat_priv, claim_addr, vid, backbone_gw);
1eda58bf 740 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
3b300de3 741 batadv_bla_send_claim(bat_priv, claim_addr, vid,
3eb8773e 742 BATADV_CLAIM_TYPE_CLAIM);
23721387
SW
743
744 /* TODO: we could call something like tt_local_del() here. */
745
3b300de3 746 batadv_backbone_gw_free_ref(backbone_gw);
23721387
SW
747 return 1;
748}
749
2c53040f
BH
750/**
751 * batadv_check_claim_group
752 * @bat_priv: the bat priv with all the soft interface information
38ef3d1d
SW
753 * @hw_src: the Hardware source in the ARP Header
754 * @hw_dst: the Hardware destination in the ARP Header
755 * @ethhdr: pointer to the Ethernet header of the claim frame
756 *
757 * checks if it is a claim packet and if its on the same group.
758 * This function also applies the group ID of the sender
759 * if it is in the same mesh.
760 *
761 * returns:
762 * 2 - if it is a claim packet and on the same group
763 * 1 - if is a claim packet from another group
764 * 0 - if it is not a claim packet
765 */
56303d34
SE
766static int batadv_check_claim_group(struct batadv_priv *bat_priv,
767 struct batadv_hard_iface *primary_if,
3b300de3
SE
768 uint8_t *hw_src, uint8_t *hw_dst,
769 struct ethhdr *ethhdr)
38ef3d1d
SW
770{
771 uint8_t *backbone_addr;
56303d34 772 struct batadv_orig_node *orig_node;
96412690 773 struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
38ef3d1d 774
96412690 775 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
807736f6 776 bla_dst_own = &bat_priv->bla.claim_dest;
38ef3d1d
SW
777
778 /* check if it is a claim packet in general */
779 if (memcmp(bla_dst->magic, bla_dst_own->magic,
780 sizeof(bla_dst->magic)) != 0)
781 return 0;
782
783 /* if announcement packet, use the source,
784 * otherwise assume it is in the hw_src
785 */
786 switch (bla_dst->type) {
3eb8773e 787 case BATADV_CLAIM_TYPE_CLAIM:
38ef3d1d
SW
788 backbone_addr = hw_src;
789 break;
acd34afa
SE
790 case BATADV_CLAIM_TYPE_REQUEST:
791 case BATADV_CLAIM_TYPE_ANNOUNCE:
3eb8773e 792 case BATADV_CLAIM_TYPE_UNCLAIM:
38ef3d1d
SW
793 backbone_addr = ethhdr->h_source;
794 break;
795 default:
796 return 0;
797 }
798
799 /* don't accept claim frames from ourselves */
1eda58bf 800 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
38ef3d1d
SW
801 return 0;
802
803 /* if its already the same group, it is fine. */
804 if (bla_dst->group == bla_dst_own->group)
805 return 2;
806
807 /* lets see if this originator is in our mesh */
da641193 808 orig_node = batadv_orig_hash_find(bat_priv, backbone_addr);
38ef3d1d
SW
809
810 /* dont accept claims from gateways which are not in
811 * the same mesh or group.
812 */
813 if (!orig_node)
814 return 1;
815
816 /* if our mesh friends mac is bigger, use it for ourselves. */
817 if (ntohs(bla_dst->group) > ntohs(bla_dst_own->group)) {
39c75a51 818 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1eda58bf
SE
819 "taking other backbones claim group: %04x\n",
820 ntohs(bla_dst->group));
38ef3d1d
SW
821 bla_dst_own->group = bla_dst->group;
822 }
823
7d211efc 824 batadv_orig_node_free_ref(orig_node);
38ef3d1d
SW
825
826 return 2;
827}
828
829
9cfc7bd6 830/* @bat_priv: the bat priv with all the soft interface information
23721387
SW
831 * @skb: the frame to be checked
832 *
833 * Check if this is a claim frame, and process it accordingly.
834 *
835 * returns 1 if it was a claim frame, otherwise return 0 to
836 * tell the callee that it can use the frame on its own.
837 */
56303d34
SE
838static int batadv_bla_process_claim(struct batadv_priv *bat_priv,
839 struct batadv_hard_iface *primary_if,
3b300de3 840 struct sk_buff *skb)
23721387
SW
841{
842 struct ethhdr *ethhdr;
843 struct vlan_ethhdr *vhdr;
844 struct arphdr *arphdr;
845 uint8_t *hw_src, *hw_dst;
96412690 846 struct batadv_bla_claim_dst *bla_dst;
23721387
SW
847 uint16_t proto;
848 int headlen;
849 short vid = -1;
38ef3d1d 850 int ret;
23721387
SW
851
852 ethhdr = (struct ethhdr *)skb_mac_header(skb);
853
854 if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) {
855 vhdr = (struct vlan_ethhdr *)ethhdr;
856 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
857 proto = ntohs(vhdr->h_vlan_encapsulated_proto);
858 headlen = sizeof(*vhdr);
859 } else {
860 proto = ntohs(ethhdr->h_proto);
0d125074 861 headlen = ETH_HLEN;
23721387
SW
862 }
863
864 if (proto != ETH_P_ARP)
865 return 0; /* not a claim frame */
866
867 /* this must be a ARP frame. check if it is a claim. */
868
869 if (unlikely(!pskb_may_pull(skb, headlen + arp_hdr_len(skb->dev))))
870 return 0;
871
872 /* pskb_may_pull() may have modified the pointers, get ethhdr again */
873 ethhdr = (struct ethhdr *)skb_mac_header(skb);
874 arphdr = (struct arphdr *)((uint8_t *)ethhdr + headlen);
875
876 /* Check whether the ARP frame carries a valid
877 * IP information
878 */
23721387
SW
879 if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
880 return 0;
881 if (arphdr->ar_pro != htons(ETH_P_IP))
882 return 0;
883 if (arphdr->ar_hln != ETH_ALEN)
884 return 0;
885 if (arphdr->ar_pln != 4)
886 return 0;
887
888 hw_src = (uint8_t *)arphdr + sizeof(struct arphdr);
889 hw_dst = hw_src + ETH_ALEN + 4;
96412690 890 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
23721387
SW
891
892 /* check if it is a claim frame. */
3b300de3
SE
893 ret = batadv_check_claim_group(bat_priv, primary_if, hw_src, hw_dst,
894 ethhdr);
38ef3d1d 895 if (ret == 1)
39c75a51 896 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1eda58bf
SE
897 "bla_process_claim(): received a claim frame from another group. From: %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n",
898 ethhdr->h_source, vid, hw_src, hw_dst);
38ef3d1d
SW
899
900 if (ret < 2)
901 return ret;
23721387
SW
902
903 /* become a backbone gw ourselves on this vlan if not happened yet */
3b300de3 904 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
23721387
SW
905
906 /* check for the different types of claim frames ... */
907 switch (bla_dst->type) {
3eb8773e 908 case BATADV_CLAIM_TYPE_CLAIM:
3b300de3
SE
909 if (batadv_handle_claim(bat_priv, primary_if, hw_src,
910 ethhdr->h_source, vid))
23721387
SW
911 return 1;
912 break;
3eb8773e 913 case BATADV_CLAIM_TYPE_UNCLAIM:
3b300de3
SE
914 if (batadv_handle_unclaim(bat_priv, primary_if,
915 ethhdr->h_source, hw_src, vid))
23721387
SW
916 return 1;
917 break;
918
acd34afa 919 case BATADV_CLAIM_TYPE_ANNOUNCE:
3b300de3
SE
920 if (batadv_handle_announce(bat_priv, hw_src, ethhdr->h_source,
921 vid))
23721387
SW
922 return 1;
923 break;
acd34afa 924 case BATADV_CLAIM_TYPE_REQUEST:
3b300de3
SE
925 if (batadv_handle_request(bat_priv, primary_if, hw_src, ethhdr,
926 vid))
23721387
SW
927 return 1;
928 break;
929 }
930
39c75a51 931 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1eda58bf
SE
932 "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",
933 ethhdr->h_source, vid, hw_src, hw_dst);
23721387
SW
934 return 1;
935}
936
937/* Check when we last heard from other nodes, and remove them in case of
938 * a time out, or clean all backbone gws if now is set.
939 */
56303d34 940static void batadv_bla_purge_backbone_gw(struct batadv_priv *bat_priv, int now)
23721387 941{
56303d34 942 struct batadv_backbone_gw *backbone_gw;
23721387
SW
943 struct hlist_node *node, *node_tmp;
944 struct hlist_head *head;
5bf74e9c 945 struct batadv_hashtable *hash;
23721387
SW
946 spinlock_t *list_lock; /* protects write access to the hash lists */
947 int i;
948
807736f6 949 hash = bat_priv->bla.backbone_hash;
23721387
SW
950 if (!hash)
951 return;
952
953 for (i = 0; i < hash->size; i++) {
954 head = &hash->table[i];
955 list_lock = &hash->list_locks[i];
956
957 spin_lock_bh(list_lock);
958 hlist_for_each_entry_safe(backbone_gw, node, node_tmp,
959 head, hash_entry) {
960 if (now)
961 goto purge_now;
1eda58bf 962 if (!batadv_has_timed_out(backbone_gw->lasttime,
42d0b044 963 BATADV_BLA_BACKBONE_TIMEOUT))
23721387
SW
964 continue;
965
39c75a51 966 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
1eda58bf
SE
967 "bla_purge_backbone_gw(): backbone gw %pM timed out\n",
968 backbone_gw->orig);
23721387
SW
969
970purge_now:
971 /* don't wait for the pending request anymore */
972 if (atomic_read(&backbone_gw->request_sent))
807736f6 973 atomic_dec(&bat_priv->bla.num_requests);
23721387 974
3b300de3 975 batadv_bla_del_backbone_claims(backbone_gw);
23721387
SW
976
977 hlist_del_rcu(node);
3b300de3 978 batadv_backbone_gw_free_ref(backbone_gw);
23721387
SW
979 }
980 spin_unlock_bh(list_lock);
981 }
982}
983
2c53040f
BH
984/**
985 * batadv_bla_purge_claims
986 * @bat_priv: the bat priv with all the soft interface information
23721387
SW
987 * @primary_if: the selected primary interface, may be NULL if now is set
988 * @now: whether the whole hash shall be wiped now
989 *
990 * Check when we heard last time from our own claims, and remove them in case of
991 * a time out, or clean all claims if now is set
992 */
56303d34
SE
993static void batadv_bla_purge_claims(struct batadv_priv *bat_priv,
994 struct batadv_hard_iface *primary_if,
995 int now)
23721387 996{
56303d34 997 struct batadv_claim *claim;
23721387
SW
998 struct hlist_node *node;
999 struct hlist_head *head;
5bf74e9c 1000 struct batadv_hashtable *hash;
23721387
SW
1001 int i;
1002
807736f6 1003 hash = bat_priv->bla.claim_hash;
23721387
SW
1004 if (!hash)
1005 return;
1006
1007 for (i = 0; i < hash->size; i++) {
1008 head = &hash->table[i];
1009
1010 rcu_read_lock();
1011 hlist_for_each_entry_rcu(claim, node, head, hash_entry) {
1012 if (now)
1013 goto purge_now;
1eda58bf
SE
1014 if (!batadv_compare_eth(claim->backbone_gw->orig,
1015 primary_if->net_dev->dev_addr))
23721387 1016 continue;
1eda58bf 1017 if (!batadv_has_timed_out(claim->lasttime,
42d0b044 1018 BATADV_BLA_CLAIM_TIMEOUT))
23721387
SW
1019 continue;
1020
39c75a51 1021 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1eda58bf
SE
1022 "bla_purge_claims(): %pM, vid %d, time out\n",
1023 claim->addr, claim->vid);
23721387
SW
1024
1025purge_now:
3b300de3
SE
1026 batadv_handle_unclaim(bat_priv, primary_if,
1027 claim->backbone_gw->orig,
1028 claim->addr, claim->vid);
23721387
SW
1029 }
1030 rcu_read_unlock();
1031 }
1032}
1033
2c53040f
BH
1034/**
1035 * batadv_bla_update_orig_address
1036 * @bat_priv: the bat priv with all the soft interface information
23721387
SW
1037 * @primary_if: the new selected primary_if
1038 * @oldif: the old primary interface, may be NULL
1039 *
1040 * Update the backbone gateways when the own orig address changes.
23721387 1041 */
56303d34
SE
1042void batadv_bla_update_orig_address(struct batadv_priv *bat_priv,
1043 struct batadv_hard_iface *primary_if,
1044 struct batadv_hard_iface *oldif)
23721387 1045{
56303d34 1046 struct batadv_backbone_gw *backbone_gw;
23721387
SW
1047 struct hlist_node *node;
1048 struct hlist_head *head;
5bf74e9c 1049 struct batadv_hashtable *hash;
807736f6 1050 __be16 group;
23721387
SW
1051 int i;
1052
38ef3d1d 1053 /* reset bridge loop avoidance group id */
807736f6
SE
1054 group = htons(crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN));
1055 bat_priv->bla.claim_dest.group = group;
38ef3d1d 1056
23721387 1057 if (!oldif) {
3b300de3
SE
1058 batadv_bla_purge_claims(bat_priv, NULL, 1);
1059 batadv_bla_purge_backbone_gw(bat_priv, 1);
23721387
SW
1060 return;
1061 }
1062
807736f6 1063 hash = bat_priv->bla.backbone_hash;
23721387
SW
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. */
1eda58bf
SE
1073 if (!batadv_compare_eth(backbone_gw->orig,
1074 oldif->net_dev->dev_addr))
23721387
SW
1075 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 */
3b300de3 1082 batadv_bla_send_announce(bat_priv, backbone_gw);
23721387
SW
1083 }
1084 rcu_read_unlock();
1085 }
1086}
1087
1088
1089
1090/* (re)start the timer */
56303d34 1091static void batadv_bla_start_timer(struct batadv_priv *bat_priv)
23721387 1092{
807736f6
SE
1093 INIT_DELAYED_WORK(&bat_priv->bla.work, batadv_bla_periodic_work);
1094 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
42d0b044 1095 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
23721387
SW
1096}
1097
1098/* periodic work to do:
1099 * * purge structures when they are too old
1100 * * send announcements
1101 */
3b300de3 1102static void batadv_bla_periodic_work(struct work_struct *work)
23721387 1103{
bbb1f90e 1104 struct delayed_work *delayed_work;
56303d34 1105 struct batadv_priv *bat_priv;
807736f6 1106 struct batadv_priv_bla *priv_bla;
23721387
SW
1107 struct hlist_node *node;
1108 struct hlist_head *head;
56303d34 1109 struct batadv_backbone_gw *backbone_gw;
5bf74e9c 1110 struct batadv_hashtable *hash;
56303d34 1111 struct batadv_hard_iface *primary_if;
23721387
SW
1112 int i;
1113
bbb1f90e 1114 delayed_work = container_of(work, struct delayed_work, work);
807736f6
SE
1115 priv_bla = container_of(delayed_work, struct batadv_priv_bla, work);
1116 bat_priv = container_of(priv_bla, struct batadv_priv, bla);
e5d89254 1117 primary_if = batadv_primary_if_get_selected(bat_priv);
23721387
SW
1118 if (!primary_if)
1119 goto out;
1120
3b300de3
SE
1121 batadv_bla_purge_claims(bat_priv, primary_if, 0);
1122 batadv_bla_purge_backbone_gw(bat_priv, 0);
23721387
SW
1123
1124 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1125 goto out;
1126
807736f6 1127 hash = bat_priv->bla.backbone_hash;
23721387
SW
1128 if (!hash)
1129 goto out;
1130
1131 for (i = 0; i < hash->size; i++) {
1132 head = &hash->table[i];
1133
1134 rcu_read_lock();
1135 hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
1eda58bf
SE
1136 if (!batadv_compare_eth(backbone_gw->orig,
1137 primary_if->net_dev->dev_addr))
23721387
SW
1138 continue;
1139
1140 backbone_gw->lasttime = jiffies;
1141
3b300de3 1142 batadv_bla_send_announce(bat_priv, backbone_gw);
23721387
SW
1143 }
1144 rcu_read_unlock();
1145 }
1146out:
1147 if (primary_if)
e5d89254 1148 batadv_hardif_free_ref(primary_if);
23721387 1149
3b300de3 1150 batadv_bla_start_timer(bat_priv);
23721387
SW
1151}
1152
5d52dad2
SE
1153/* The hash for claim and backbone hash receive the same key because they
1154 * are getting initialized by hash_new with the same key. Reinitializing
1155 * them with to different keys to allow nested locking without generating
1156 * lockdep warnings
1157 */
3b300de3
SE
1158static struct lock_class_key batadv_claim_hash_lock_class_key;
1159static struct lock_class_key batadv_backbone_hash_lock_class_key;
5d52dad2 1160
23721387 1161/* initialize all bla structures */
56303d34 1162int batadv_bla_init(struct batadv_priv *bat_priv)
23721387 1163{
fe2da6ff 1164 int i;
38ef3d1d 1165 uint8_t claim_dest[ETH_ALEN] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00};
56303d34 1166 struct batadv_hard_iface *primary_if;
807736f6
SE
1167 uint16_t crc;
1168 unsigned long entrytime;
fe2da6ff 1169
39c75a51 1170 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hash registering\n");
23721387 1171
38ef3d1d 1172 /* setting claim destination address */
807736f6
SE
1173 memcpy(&bat_priv->bla.claim_dest.magic, claim_dest, 3);
1174 bat_priv->bla.claim_dest.type = 0;
e5d89254 1175 primary_if = batadv_primary_if_get_selected(bat_priv);
38ef3d1d 1176 if (primary_if) {
807736f6
SE
1177 crc = crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN);
1178 bat_priv->bla.claim_dest.group = htons(crc);
e5d89254 1179 batadv_hardif_free_ref(primary_if);
38ef3d1d 1180 } else {
807736f6 1181 bat_priv->bla.claim_dest.group = 0; /* will be set later */
38ef3d1d
SW
1182 }
1183
fe2da6ff 1184 /* initialize the duplicate list */
807736f6 1185 entrytime = jiffies - msecs_to_jiffies(BATADV_DUPLIST_TIMEOUT);
42d0b044 1186 for (i = 0; i < BATADV_DUPLIST_SIZE; i++)
807736f6
SE
1187 bat_priv->bla.bcast_duplist[i].entrytime = entrytime;
1188 bat_priv->bla.bcast_duplist_curr = 0;
fe2da6ff 1189
807736f6 1190 if (bat_priv->bla.claim_hash)
5346c35e 1191 return 0;
23721387 1192
807736f6
SE
1193 bat_priv->bla.claim_hash = batadv_hash_new(128);
1194 bat_priv->bla.backbone_hash = batadv_hash_new(32);
23721387 1195
807736f6 1196 if (!bat_priv->bla.claim_hash || !bat_priv->bla.backbone_hash)
5346c35e 1197 return -ENOMEM;
23721387 1198
807736f6 1199 batadv_hash_set_lock_class(bat_priv->bla.claim_hash,
3b300de3 1200 &batadv_claim_hash_lock_class_key);
807736f6 1201 batadv_hash_set_lock_class(bat_priv->bla.backbone_hash,
3b300de3 1202 &batadv_backbone_hash_lock_class_key);
5d52dad2 1203
39c75a51 1204 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hashes initialized\n");
23721387 1205
3b300de3 1206 batadv_bla_start_timer(bat_priv);
5346c35e 1207 return 0;
23721387
SW
1208}
1209
2c53040f
BH
1210/**
1211 * batadv_bla_check_bcast_duplist
1212 * @bat_priv: the bat priv with all the soft interface information
fe2da6ff
SW
1213 * @bcast_packet: originator mac address
1214 * @hdr_size: maximum length of the frame
1215 *
1216 * check if it is on our broadcast list. Another gateway might
1217 * have sent the same packet because it is connected to the same backbone,
1218 * so we have to remove this duplicate.
1219 *
1220 * This is performed by checking the CRC, which will tell us
1221 * with a good chance that it is the same packet. If it is furthermore
1222 * sent by another host, drop it. We allow equal packets from
1223 * the same host however as this might be intended.
9cfc7bd6 1224 */
56303d34 1225int batadv_bla_check_bcast_duplist(struct batadv_priv *bat_priv,
96412690 1226 struct batadv_bcast_packet *bcast_packet,
08adf151 1227 int hdr_size)
fe2da6ff
SW
1228{
1229 int i, length, curr;
1230 uint8_t *content;
1231 uint16_t crc;
56303d34 1232 struct batadv_bcast_duplist_entry *entry;
fe2da6ff
SW
1233
1234 length = hdr_size - sizeof(*bcast_packet);
1235 content = (uint8_t *)bcast_packet;
1236 content += sizeof(*bcast_packet);
1237
1238 /* calculate the crc ... */
1239 crc = crc16(0, content, length);
1240
42d0b044 1241 for (i = 0; i < BATADV_DUPLIST_SIZE; i++) {
807736f6
SE
1242 curr = (bat_priv->bla.bcast_duplist_curr + i);
1243 curr %= BATADV_DUPLIST_SIZE;
1244 entry = &bat_priv->bla.bcast_duplist[curr];
fe2da6ff
SW
1245
1246 /* we can stop searching if the entry is too old ;
1247 * later entries will be even older
1248 */
42d0b044
SE
1249 if (batadv_has_timed_out(entry->entrytime,
1250 BATADV_DUPLIST_TIMEOUT))
fe2da6ff
SW
1251 break;
1252
1253 if (entry->crc != crc)
1254 continue;
1255
1eda58bf 1256 if (batadv_compare_eth(entry->orig, bcast_packet->orig))
fe2da6ff
SW
1257 continue;
1258
1259 /* this entry seems to match: same crc, not too old,
1260 * and from another gw. therefore return 1 to forbid it.
1261 */
1262 return 1;
1263 }
1264 /* not found, add a new entry (overwrite the oldest entry) */
807736f6 1265 curr = (bat_priv->bla.bcast_duplist_curr + BATADV_DUPLIST_SIZE - 1);
42d0b044 1266 curr %= BATADV_DUPLIST_SIZE;
807736f6 1267 entry = &bat_priv->bla.bcast_duplist[curr];
fe2da6ff
SW
1268 entry->crc = crc;
1269 entry->entrytime = jiffies;
1270 memcpy(entry->orig, bcast_packet->orig, ETH_ALEN);
807736f6 1271 bat_priv->bla.bcast_duplist_curr = curr;
fe2da6ff
SW
1272
1273 /* allow it, its the first occurence. */
1274 return 0;
1275}
1276
1277
1278
9cfc7bd6 1279/* @bat_priv: the bat priv with all the soft interface information
20ff9d59
SW
1280 * @orig: originator mac address
1281 *
1282 * check if the originator is a gateway for any VLAN ID.
1283 *
1284 * returns 1 if it is found, 0 otherwise
20ff9d59 1285 */
56303d34 1286int batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, uint8_t *orig)
20ff9d59 1287{
807736f6 1288 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
20ff9d59
SW
1289 struct hlist_head *head;
1290 struct hlist_node *node;
56303d34 1291 struct batadv_backbone_gw *backbone_gw;
20ff9d59
SW
1292 int i;
1293
1294 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1295 return 0;
1296
1297 if (!hash)
1298 return 0;
1299
1300 for (i = 0; i < hash->size; i++) {
1301 head = &hash->table[i];
1302
1303 rcu_read_lock();
1304 hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
1eda58bf 1305 if (batadv_compare_eth(backbone_gw->orig, orig)) {
20ff9d59
SW
1306 rcu_read_unlock();
1307 return 1;
1308 }
1309 }
1310 rcu_read_unlock();
1311 }
1312
1313 return 0;
1314}
1315
1316
2c53040f
BH
1317/**
1318 * batadv_bla_is_backbone_gw
1319 * @skb: the frame to be checked
23721387
SW
1320 * @orig_node: the orig_node of the frame
1321 * @hdr_size: maximum length of the frame
1322 *
1323 * bla_is_backbone_gw inspects the skb for the VLAN ID and returns 1
1324 * if the orig_node is also a gateway on the soft interface, otherwise it
1325 * returns 0.
23721387 1326 */
08adf151 1327int batadv_bla_is_backbone_gw(struct sk_buff *skb,
56303d34 1328 struct batadv_orig_node *orig_node, int hdr_size)
23721387
SW
1329{
1330 struct ethhdr *ethhdr;
1331 struct vlan_ethhdr *vhdr;
56303d34 1332 struct batadv_backbone_gw *backbone_gw;
23721387
SW
1333 short vid = -1;
1334
1335 if (!atomic_read(&orig_node->bat_priv->bridge_loop_avoidance))
1336 return 0;
1337
1338 /* first, find out the vid. */
0d125074 1339 if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
23721387
SW
1340 return 0;
1341
1342 ethhdr = (struct ethhdr *)(((uint8_t *)skb->data) + hdr_size);
1343
1344 if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) {
1345 if (!pskb_may_pull(skb, hdr_size + sizeof(struct vlan_ethhdr)))
1346 return 0;
1347
1348 vhdr = (struct vlan_ethhdr *)(((uint8_t *)skb->data) +
1349 hdr_size);
1350 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
1351 }
1352
1353 /* see if this originator is a backbone gw for this VLAN */
3b300de3
SE
1354 backbone_gw = batadv_backbone_hash_find(orig_node->bat_priv,
1355 orig_node->orig, vid);
23721387
SW
1356 if (!backbone_gw)
1357 return 0;
1358
3b300de3 1359 batadv_backbone_gw_free_ref(backbone_gw);
23721387
SW
1360 return 1;
1361}
1362
1363/* free all bla structures (for softinterface free or module unload) */
56303d34 1364void batadv_bla_free(struct batadv_priv *bat_priv)
23721387 1365{
56303d34 1366 struct batadv_hard_iface *primary_if;
23721387 1367
807736f6 1368 cancel_delayed_work_sync(&bat_priv->bla.work);
e5d89254 1369 primary_if = batadv_primary_if_get_selected(bat_priv);
23721387 1370
807736f6 1371 if (bat_priv->bla.claim_hash) {
3b300de3 1372 batadv_bla_purge_claims(bat_priv, primary_if, 1);
807736f6
SE
1373 batadv_hash_destroy(bat_priv->bla.claim_hash);
1374 bat_priv->bla.claim_hash = NULL;
23721387 1375 }
807736f6 1376 if (bat_priv->bla.backbone_hash) {
3b300de3 1377 batadv_bla_purge_backbone_gw(bat_priv, 1);
807736f6
SE
1378 batadv_hash_destroy(bat_priv->bla.backbone_hash);
1379 bat_priv->bla.backbone_hash = NULL;
23721387
SW
1380 }
1381 if (primary_if)
e5d89254 1382 batadv_hardif_free_ref(primary_if);
23721387
SW
1383}
1384
2c53040f
BH
1385/**
1386 * batadv_bla_rx
1387 * @bat_priv: the bat priv with all the soft interface information
23721387
SW
1388 * @skb: the frame to be checked
1389 * @vid: the VLAN ID of the frame
2d3f6ccc 1390 * @is_bcast: the packet came in a broadcast packet type.
23721387
SW
1391 *
1392 * bla_rx avoidance checks if:
1393 * * we have to race for a claim
1394 * * if the frame is allowed on the LAN
1395 *
1396 * in these cases, the skb is further handled by this function and
1397 * returns 1, otherwise it returns 0 and the caller shall further
1398 * process the skb.
23721387 1399 */
04c9f416
DM
1400int batadv_bla_rx(struct batadv_priv *bat_priv, struct sk_buff *skb, short vid,
1401 bool is_bcast)
23721387
SW
1402{
1403 struct ethhdr *ethhdr;
56303d34
SE
1404 struct batadv_claim search_claim, *claim = NULL;
1405 struct batadv_hard_iface *primary_if;
23721387
SW
1406 int ret;
1407
1408 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1409
e5d89254 1410 primary_if = batadv_primary_if_get_selected(bat_priv);
23721387
SW
1411 if (!primary_if)
1412 goto handled;
1413
1414 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1415 goto allow;
1416
1417
807736f6 1418 if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
23721387 1419 /* don't allow broadcasts while requests are in flight */
2d3f6ccc 1420 if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast)
23721387
SW
1421 goto handled;
1422
1423 memcpy(search_claim.addr, ethhdr->h_source, ETH_ALEN);
1424 search_claim.vid = vid;
3b300de3 1425 claim = batadv_claim_hash_find(bat_priv, &search_claim);
23721387
SW
1426
1427 if (!claim) {
1428 /* possible optimization: race for a claim */
1429 /* No claim exists yet, claim it for us!
1430 */
3b300de3
SE
1431 batadv_handle_claim(bat_priv, primary_if,
1432 primary_if->net_dev->dev_addr,
1433 ethhdr->h_source, vid);
23721387
SW
1434 goto allow;
1435 }
1436
1437 /* if it is our own claim ... */
1eda58bf
SE
1438 if (batadv_compare_eth(claim->backbone_gw->orig,
1439 primary_if->net_dev->dev_addr)) {
23721387
SW
1440 /* ... allow it in any case */
1441 claim->lasttime = jiffies;
1442 goto allow;
1443 }
1444
1445 /* if it is a broadcast ... */
2d3f6ccc
SW
1446 if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast) {
1447 /* ... drop it. the responsible gateway is in charge.
1448 *
1449 * We need to check is_bcast because with the gateway
1450 * feature, broadcasts (like DHCP requests) may be sent
1451 * using a unicast packet type.
1452 */
23721387
SW
1453 goto handled;
1454 } else {
1455 /* seems the client considers us as its best gateway.
1456 * send a claim and update the claim table
1457 * immediately.
1458 */
3b300de3
SE
1459 batadv_handle_claim(bat_priv, primary_if,
1460 primary_if->net_dev->dev_addr,
1461 ethhdr->h_source, vid);
23721387
SW
1462 goto allow;
1463 }
1464allow:
3b300de3 1465 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
23721387
SW
1466 ret = 0;
1467 goto out;
1468
1469handled:
1470 kfree_skb(skb);
1471 ret = 1;
1472
1473out:
1474 if (primary_if)
e5d89254 1475 batadv_hardif_free_ref(primary_if);
23721387 1476 if (claim)
3b300de3 1477 batadv_claim_free_ref(claim);
23721387
SW
1478 return ret;
1479}
1480
2c53040f
BH
1481/**
1482 * batadv_bla_tx
1483 * @bat_priv: the bat priv with all the soft interface information
23721387
SW
1484 * @skb: the frame to be checked
1485 * @vid: the VLAN ID of the frame
1486 *
1487 * bla_tx checks if:
1488 * * a claim was received which has to be processed
1489 * * the frame is allowed on the mesh
1490 *
1491 * in these cases, the skb is further handled by this function and
1492 * returns 1, otherwise it returns 0 and the caller shall further
1493 * process the skb.
23721387 1494 */
56303d34 1495int batadv_bla_tx(struct batadv_priv *bat_priv, struct sk_buff *skb, short vid)
23721387
SW
1496{
1497 struct ethhdr *ethhdr;
56303d34
SE
1498 struct batadv_claim search_claim, *claim = NULL;
1499 struct batadv_hard_iface *primary_if;
23721387
SW
1500 int ret = 0;
1501
e5d89254 1502 primary_if = batadv_primary_if_get_selected(bat_priv);
23721387
SW
1503 if (!primary_if)
1504 goto out;
1505
1506 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1507 goto allow;
1508
1509 /* in VLAN case, the mac header might not be set. */
1510 skb_reset_mac_header(skb);
1511
3b300de3 1512 if (batadv_bla_process_claim(bat_priv, primary_if, skb))
23721387
SW
1513 goto handled;
1514
1515 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1516
807736f6 1517 if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
23721387
SW
1518 /* don't allow broadcasts while requests are in flight */
1519 if (is_multicast_ether_addr(ethhdr->h_dest))
1520 goto handled;
1521
1522 memcpy(search_claim.addr, ethhdr->h_source, ETH_ALEN);
1523 search_claim.vid = vid;
1524
3b300de3 1525 claim = batadv_claim_hash_find(bat_priv, &search_claim);
23721387
SW
1526
1527 /* if no claim exists, allow it. */
1528 if (!claim)
1529 goto allow;
1530
1531 /* check if we are responsible. */
1eda58bf
SE
1532 if (batadv_compare_eth(claim->backbone_gw->orig,
1533 primary_if->net_dev->dev_addr)) {
23721387
SW
1534 /* if yes, the client has roamed and we have
1535 * to unclaim it.
1536 */
3b300de3
SE
1537 batadv_handle_unclaim(bat_priv, primary_if,
1538 primary_if->net_dev->dev_addr,
1539 ethhdr->h_source, vid);
23721387
SW
1540 goto allow;
1541 }
1542
1543 /* check if it is a multicast/broadcast frame */
1544 if (is_multicast_ether_addr(ethhdr->h_dest)) {
1545 /* drop it. the responsible gateway has forwarded it into
1546 * the backbone network.
1547 */
1548 goto handled;
1549 } else {
1550 /* we must allow it. at least if we are
1551 * responsible for the DESTINATION.
1552 */
1553 goto allow;
1554 }
1555allow:
3b300de3 1556 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
23721387
SW
1557 ret = 0;
1558 goto out;
1559handled:
1560 ret = 1;
1561out:
1562 if (primary_if)
e5d89254 1563 batadv_hardif_free_ref(primary_if);
23721387 1564 if (claim)
3b300de3 1565 batadv_claim_free_ref(claim);
23721387
SW
1566 return ret;
1567}
9bf8e4d4 1568
08adf151 1569int batadv_bla_claim_table_seq_print_text(struct seq_file *seq, void *offset)
9bf8e4d4
SW
1570{
1571 struct net_device *net_dev = (struct net_device *)seq->private;
56303d34 1572 struct batadv_priv *bat_priv = netdev_priv(net_dev);
807736f6 1573 struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
56303d34
SE
1574 struct batadv_claim *claim;
1575 struct batadv_hard_iface *primary_if;
9bf8e4d4
SW
1576 struct hlist_node *node;
1577 struct hlist_head *head;
1578 uint32_t i;
1579 bool is_own;
1580 int ret = 0;
1eda58bf 1581 uint8_t *primary_addr;
9bf8e4d4 1582
e5d89254 1583 primary_if = batadv_primary_if_get_selected(bat_priv);
9bf8e4d4
SW
1584 if (!primary_if) {
1585 ret = seq_printf(seq,
1586 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
1587 net_dev->name);
1588 goto out;
1589 }
1590
e9a4f295 1591 if (primary_if->if_status != BATADV_IF_ACTIVE) {
9bf8e4d4
SW
1592 ret = seq_printf(seq,
1593 "BATMAN mesh %s disabled - primary interface not active\n",
1594 net_dev->name);
1595 goto out;
1596 }
1597
1eda58bf 1598 primary_addr = primary_if->net_dev->dev_addr;
38ef3d1d
SW
1599 seq_printf(seq,
1600 "Claims announced for the mesh %s (orig %pM, group id %04x)\n",
1eda58bf 1601 net_dev->name, primary_addr,
807736f6 1602 ntohs(bat_priv->bla.claim_dest.group));
9bf8e4d4
SW
1603 seq_printf(seq, " %-17s %-5s %-17s [o] (%-4s)\n",
1604 "Client", "VID", "Originator", "CRC");
1605 for (i = 0; i < hash->size; i++) {
1606 head = &hash->table[i];
1607
1608 rcu_read_lock();
1609 hlist_for_each_entry_rcu(claim, node, head, hash_entry) {
1eda58bf
SE
1610 is_own = batadv_compare_eth(claim->backbone_gw->orig,
1611 primary_addr);
9bf8e4d4
SW
1612 seq_printf(seq, " * %pM on % 5d by %pM [%c] (%04x)\n",
1613 claim->addr, claim->vid,
1614 claim->backbone_gw->orig,
1615 (is_own ? 'x' : ' '),
1616 claim->backbone_gw->crc);
1617 }
1618 rcu_read_unlock();
1619 }
1620out:
1621 if (primary_if)
e5d89254 1622 batadv_hardif_free_ref(primary_if);
9bf8e4d4
SW
1623 return ret;
1624}
536a23f1
SW
1625
1626int batadv_bla_backbone_table_seq_print_text(struct seq_file *seq, void *offset)
1627{
1628 struct net_device *net_dev = (struct net_device *)seq->private;
1629 struct batadv_priv *bat_priv = netdev_priv(net_dev);
807736f6 1630 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
536a23f1
SW
1631 struct batadv_backbone_gw *backbone_gw;
1632 struct batadv_hard_iface *primary_if;
1633 struct hlist_node *node;
1634 struct hlist_head *head;
1635 int secs, msecs;
1636 uint32_t i;
1637 bool is_own;
1638 int ret = 0;
1639 uint8_t *primary_addr;
1640
1641 primary_if = batadv_primary_if_get_selected(bat_priv);
1642 if (!primary_if) {
1643 ret = seq_printf(seq,
1644 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
1645 net_dev->name);
1646 goto out;
1647 }
1648
1649 if (primary_if->if_status != BATADV_IF_ACTIVE) {
1650 ret = seq_printf(seq,
1651 "BATMAN mesh %s disabled - primary interface not active\n",
1652 net_dev->name);
1653 goto out;
1654 }
1655
1656 primary_addr = primary_if->net_dev->dev_addr;
1657 seq_printf(seq,
1658 "Backbones announced for the mesh %s (orig %pM, group id %04x)\n",
1659 net_dev->name, primary_addr,
807736f6 1660 ntohs(bat_priv->bla.claim_dest.group));
536a23f1
SW
1661 seq_printf(seq, " %-17s %-5s %-9s (%-4s)\n",
1662 "Originator", "VID", "last seen", "CRC");
1663 for (i = 0; i < hash->size; i++) {
1664 head = &hash->table[i];
1665
1666 rcu_read_lock();
1667 hlist_for_each_entry_rcu(backbone_gw, node, head, hash_entry) {
1668 msecs = jiffies_to_msecs(jiffies -
1669 backbone_gw->lasttime);
1670 secs = msecs / 1000;
1671 msecs = msecs % 1000;
1672
1673 is_own = batadv_compare_eth(backbone_gw->orig,
1674 primary_addr);
1675 if (is_own)
1676 continue;
1677
1678 seq_printf(seq,
1679 " * %pM on % 5d % 4i.%03is (%04x)\n",
1680 backbone_gw->orig, backbone_gw->vid,
1681 secs, msecs, backbone_gw->crc);
1682 }
1683 rcu_read_unlock();
1684 }
1685out:
1686 if (primary_if)
1687 batadv_hardif_free_ref(primary_if);
1688 return ret;
1689}