1 /* Copyright (C) 2011-2016 B.A.T.M.A.N. contributors:
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.
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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 #include "bridge_loop_avoidance.h"
21 #include <linux/atomic.h>
22 #include <linux/byteorder/generic.h>
23 #include <linux/compiler.h>
24 #include <linux/crc16.h>
25 #include <linux/errno.h>
26 #include <linux/etherdevice.h>
28 #include <linux/if_arp.h>
29 #include <linux/if_ether.h>
30 #include <linux/if_vlan.h>
31 #include <linux/jhash.h>
32 #include <linux/jiffies.h>
33 #include <linux/kernel.h>
34 #include <linux/kref.h>
35 #include <linux/list.h>
36 #include <linux/lockdep.h>
37 #include <linux/netdevice.h>
38 #include <linux/rculist.h>
39 #include <linux/rcupdate.h>
40 #include <linux/seq_file.h>
41 #include <linux/skbuff.h>
42 #include <linux/slab.h>
43 #include <linux/spinlock.h>
44 #include <linux/stddef.h>
45 #include <linux/string.h>
46 #include <linux/workqueue.h>
49 #include "hard-interface.h"
51 #include "originator.h"
53 #include "translation-table.h"
55 static const u8 batadv_announce_mac[4] = {0x43, 0x05, 0x43, 0x05};
57 static void batadv_bla_periodic_work(struct work_struct *work);
59 batadv_bla_send_announce(struct batadv_priv *bat_priv,
60 struct batadv_bla_backbone_gw *backbone_gw);
63 * batadv_choose_claim - choose the right bucket for a claim.
65 * @size: size of the hash table
67 * Return: the hash index of the claim
69 static inline u32 batadv_choose_claim(const void *data, u32 size)
71 struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
74 hash = jhash(&claim->addr, sizeof(claim->addr), hash);
75 hash = jhash(&claim->vid, sizeof(claim->vid), hash);
81 * batadv_choose_backbone_gw - choose the right bucket for a backbone gateway.
83 * @size: size of the hash table
85 * Return: the hash index of the backbone gateway
87 static inline u32 batadv_choose_backbone_gw(const void *data, u32 size)
89 const struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
92 hash = jhash(&claim->addr, sizeof(claim->addr), hash);
93 hash = jhash(&claim->vid, sizeof(claim->vid), hash);
99 * batadv_compare_backbone_gw - compare address and vid of two backbone gws
100 * @node: list node of the first entry to compare
101 * @data2: pointer to the second backbone gateway
103 * Return: 1 if the backbones have the same data, 0 otherwise
105 static int batadv_compare_backbone_gw(const struct hlist_node *node,
108 const void *data1 = container_of(node, struct batadv_bla_backbone_gw,
110 const struct batadv_bla_backbone_gw *gw1 = data1;
111 const struct batadv_bla_backbone_gw *gw2 = data2;
113 if (!batadv_compare_eth(gw1->orig, gw2->orig))
116 if (gw1->vid != gw2->vid)
123 * batadv_compare_backbone_gw - compare address and vid of two claims
124 * @node: list node of the first entry to compare
125 * @data2: pointer to the second claims
127 * Return: 1 if the claim have the same data, 0 otherwise
129 static int batadv_compare_claim(const struct hlist_node *node,
132 const void *data1 = container_of(node, struct batadv_bla_claim,
134 const struct batadv_bla_claim *cl1 = data1;
135 const struct batadv_bla_claim *cl2 = data2;
137 if (!batadv_compare_eth(cl1->addr, cl2->addr))
140 if (cl1->vid != cl2->vid)
147 * batadv_backbone_gw_release - release backbone gw from lists and queue for
148 * free after rcu grace period
149 * @ref: kref pointer of the backbone gw
151 static void batadv_backbone_gw_release(struct kref *ref)
153 struct batadv_bla_backbone_gw *backbone_gw;
155 backbone_gw = container_of(ref, struct batadv_bla_backbone_gw,
158 kfree_rcu(backbone_gw, rcu);
162 * batadv_backbone_gw_free_ref - decrement the backbone gw refcounter and
163 * possibly release it
164 * @backbone_gw: backbone gateway to be free'd
167 batadv_backbone_gw_free_ref(struct batadv_bla_backbone_gw *backbone_gw)
169 kref_put(&backbone_gw->refcount, batadv_backbone_gw_release);
173 * batadv_claim_release - release claim from lists and queue for free after rcu
175 * @ref: kref pointer of the claim
177 static void batadv_claim_release(struct batadv_bla_claim *claim)
179 batadv_backbone_gw_free_ref(claim->backbone_gw);
180 kfree_rcu(claim, rcu);
184 * batadv_claim_free_ref - decrement the claim refcounter and possibly
186 * @claim: claim to be free'd
188 static void batadv_claim_free_ref(struct batadv_bla_claim *claim)
190 if (atomic_dec_and_test(&claim->refcount))
191 batadv_claim_release(claim);
195 * batadv_claim_hash_find - looks for a claim in the claim hash
196 * @bat_priv: the bat priv with all the soft interface information
197 * @data: search data (may be local/static data)
199 * Return: claim if found or NULL otherwise.
201 static struct batadv_bla_claim
202 *batadv_claim_hash_find(struct batadv_priv *bat_priv,
203 struct batadv_bla_claim *data)
205 struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
206 struct hlist_head *head;
207 struct batadv_bla_claim *claim;
208 struct batadv_bla_claim *claim_tmp = NULL;
214 index = batadv_choose_claim(data, hash->size);
215 head = &hash->table[index];
218 hlist_for_each_entry_rcu(claim, head, hash_entry) {
219 if (!batadv_compare_claim(&claim->hash_entry, data))
222 if (!atomic_inc_not_zero(&claim->refcount))
234 * batadv_backbone_hash_find - looks for a backbone gateway in the hash
235 * @bat_priv: the bat priv with all the soft interface information
236 * @addr: the address of the originator
239 * Return: backbone gateway if found or NULL otherwise
241 static struct batadv_bla_backbone_gw *
242 batadv_backbone_hash_find(struct batadv_priv *bat_priv, u8 *addr,
245 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
246 struct hlist_head *head;
247 struct batadv_bla_backbone_gw search_entry, *backbone_gw;
248 struct batadv_bla_backbone_gw *backbone_gw_tmp = NULL;
254 ether_addr_copy(search_entry.orig, addr);
255 search_entry.vid = vid;
257 index = batadv_choose_backbone_gw(&search_entry, hash->size);
258 head = &hash->table[index];
261 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
262 if (!batadv_compare_backbone_gw(&backbone_gw->hash_entry,
266 if (!kref_get_unless_zero(&backbone_gw->refcount))
269 backbone_gw_tmp = backbone_gw;
274 return backbone_gw_tmp;
278 * batadv_bla_del_backbone_claims - delete all claims for a backbone
279 * @backbone_gw: backbone gateway where the claims should be removed
282 batadv_bla_del_backbone_claims(struct batadv_bla_backbone_gw *backbone_gw)
284 struct batadv_hashtable *hash;
285 struct hlist_node *node_tmp;
286 struct hlist_head *head;
287 struct batadv_bla_claim *claim;
289 spinlock_t *list_lock; /* protects write access to the hash lists */
291 hash = backbone_gw->bat_priv->bla.claim_hash;
295 for (i = 0; i < hash->size; i++) {
296 head = &hash->table[i];
297 list_lock = &hash->list_locks[i];
299 spin_lock_bh(list_lock);
300 hlist_for_each_entry_safe(claim, node_tmp,
302 if (claim->backbone_gw != backbone_gw)
305 batadv_claim_free_ref(claim);
306 hlist_del_rcu(&claim->hash_entry);
308 spin_unlock_bh(list_lock);
311 /* all claims gone, initialize CRC */
312 spin_lock_bh(&backbone_gw->crc_lock);
313 backbone_gw->crc = BATADV_BLA_CRC_INIT;
314 spin_unlock_bh(&backbone_gw->crc_lock);
318 * batadv_bla_send_claim - sends a claim frame according to the provided info
319 * @bat_priv: the bat priv with all the soft interface information
320 * @mac: the mac address to be announced within the claim
322 * @claimtype: the type of the claim (CLAIM, UNCLAIM, ANNOUNCE, ...)
324 static void batadv_bla_send_claim(struct batadv_priv *bat_priv, u8 *mac,
325 unsigned short vid, int claimtype)
328 struct ethhdr *ethhdr;
329 struct batadv_hard_iface *primary_if;
330 struct net_device *soft_iface;
332 struct batadv_bla_claim_dst local_claim_dest;
335 primary_if = batadv_primary_if_get_selected(bat_priv);
339 memcpy(&local_claim_dest, &bat_priv->bla.claim_dest,
340 sizeof(local_claim_dest));
341 local_claim_dest.type = claimtype;
343 soft_iface = primary_if->soft_iface;
345 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
346 /* IP DST: 0.0.0.0 */
348 primary_if->soft_iface,
349 /* IP SRC: 0.0.0.0 */
351 /* Ethernet DST: Broadcast */
353 /* Ethernet SRC/HW SRC: originator mac */
354 primary_if->net_dev->dev_addr,
355 /* HW DST: FF:43:05:XX:YY:YY
356 * with XX = claim type
357 * and YY:YY = group id
359 (u8 *)&local_claim_dest);
364 ethhdr = (struct ethhdr *)skb->data;
365 hw_src = (u8 *)ethhdr + ETH_HLEN + sizeof(struct arphdr);
367 /* now we pretend that the client would have sent this ... */
369 case BATADV_CLAIM_TYPE_CLAIM:
370 /* normal claim frame
371 * set Ethernet SRC to the clients mac
373 ether_addr_copy(ethhdr->h_source, mac);
374 batadv_dbg(BATADV_DBG_BLA, bat_priv,
375 "bla_send_claim(): CLAIM %pM on vid %d\n", mac,
376 BATADV_PRINT_VID(vid));
378 case BATADV_CLAIM_TYPE_UNCLAIM:
380 * set HW SRC to the clients mac
382 ether_addr_copy(hw_src, mac);
383 batadv_dbg(BATADV_DBG_BLA, bat_priv,
384 "bla_send_claim(): UNCLAIM %pM on vid %d\n", mac,
385 BATADV_PRINT_VID(vid));
387 case BATADV_CLAIM_TYPE_ANNOUNCE:
388 /* announcement frame
389 * set HW SRC to the special mac containg the crc
391 ether_addr_copy(hw_src, mac);
392 batadv_dbg(BATADV_DBG_BLA, bat_priv,
393 "bla_send_claim(): ANNOUNCE of %pM on vid %d\n",
394 ethhdr->h_source, BATADV_PRINT_VID(vid));
396 case BATADV_CLAIM_TYPE_REQUEST:
398 * set HW SRC and header destination to the receiving backbone
401 ether_addr_copy(hw_src, mac);
402 ether_addr_copy(ethhdr->h_dest, mac);
403 batadv_dbg(BATADV_DBG_BLA, bat_priv,
404 "bla_send_claim(): REQUEST of %pM to %pM on vid %d\n",
405 ethhdr->h_source, ethhdr->h_dest,
406 BATADV_PRINT_VID(vid));
410 if (vid & BATADV_VLAN_HAS_TAG)
411 skb = vlan_insert_tag(skb, htons(ETH_P_8021Q),
412 vid & VLAN_VID_MASK);
414 skb_reset_mac_header(skb);
415 skb->protocol = eth_type_trans(skb, soft_iface);
416 batadv_inc_counter(bat_priv, BATADV_CNT_RX);
417 batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
418 skb->len + ETH_HLEN);
419 soft_iface->last_rx = jiffies;
424 batadv_hardif_free_ref(primary_if);
428 * batadv_bla_get_backbone_gw - finds or creates a backbone gateway
429 * @bat_priv: the bat priv with all the soft interface information
430 * @orig: the mac address of the originator
432 * @own_backbone: set if the requested backbone is local
434 * Return: the (possibly created) backbone gateway or NULL on error
436 static struct batadv_bla_backbone_gw *
437 batadv_bla_get_backbone_gw(struct batadv_priv *bat_priv, u8 *orig,
438 unsigned short vid, bool own_backbone)
440 struct batadv_bla_backbone_gw *entry;
441 struct batadv_orig_node *orig_node;
444 entry = batadv_backbone_hash_find(bat_priv, orig, vid);
449 batadv_dbg(BATADV_DBG_BLA, bat_priv,
450 "bla_get_backbone_gw(): not found (%pM, %d), creating new entry\n",
451 orig, BATADV_PRINT_VID(vid));
453 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
458 entry->lasttime = jiffies;
459 entry->crc = BATADV_BLA_CRC_INIT;
460 entry->bat_priv = bat_priv;
461 spin_lock_init(&entry->crc_lock);
462 atomic_set(&entry->request_sent, 0);
463 atomic_set(&entry->wait_periods, 0);
464 ether_addr_copy(entry->orig, orig);
466 /* one for the hash, one for returning */
467 kref_init(&entry->refcount);
468 kref_get(&entry->refcount);
470 hash_added = batadv_hash_add(bat_priv->bla.backbone_hash,
471 batadv_compare_backbone_gw,
472 batadv_choose_backbone_gw, entry,
475 if (unlikely(hash_added != 0)) {
476 /* hash failed, free the structure */
481 /* this is a gateway now, remove any TT entry on this VLAN */
482 orig_node = batadv_orig_hash_find(bat_priv, orig);
484 batadv_tt_global_del_orig(bat_priv, orig_node, vid,
485 "became a backbone gateway");
486 batadv_orig_node_free_ref(orig_node);
490 batadv_bla_send_announce(bat_priv, entry);
492 /* this will be decreased in the worker thread */
493 atomic_inc(&entry->request_sent);
494 atomic_set(&entry->wait_periods, BATADV_BLA_WAIT_PERIODS);
495 atomic_inc(&bat_priv->bla.num_requests);
502 * batadv_bla_update_own_backbone_gw - updates the own backbone gw for a VLAN
503 * @bat_priv: the bat priv with all the soft interface information
504 * @primary_if: the selected primary interface
505 * @vid: VLAN identifier
507 * update or add the own backbone gw to make sure we announce
508 * where we receive other backbone gws
511 batadv_bla_update_own_backbone_gw(struct batadv_priv *bat_priv,
512 struct batadv_hard_iface *primary_if,
515 struct batadv_bla_backbone_gw *backbone_gw;
517 backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
518 primary_if->net_dev->dev_addr,
520 if (unlikely(!backbone_gw))
523 backbone_gw->lasttime = jiffies;
524 batadv_backbone_gw_free_ref(backbone_gw);
528 * batadv_bla_answer_request - answer a bla request by sending own claims
529 * @bat_priv: the bat priv with all the soft interface information
530 * @primary_if: interface where the request came on
531 * @vid: the vid where the request came on
533 * Repeat all of our own claims, and finally send an ANNOUNCE frame
534 * to allow the requester another check if the CRC is correct now.
536 static void batadv_bla_answer_request(struct batadv_priv *bat_priv,
537 struct batadv_hard_iface *primary_if,
540 struct hlist_head *head;
541 struct batadv_hashtable *hash;
542 struct batadv_bla_claim *claim;
543 struct batadv_bla_backbone_gw *backbone_gw;
546 batadv_dbg(BATADV_DBG_BLA, bat_priv,
547 "bla_answer_request(): received a claim request, send all of our own claims again\n");
549 backbone_gw = batadv_backbone_hash_find(bat_priv,
550 primary_if->net_dev->dev_addr,
555 hash = bat_priv->bla.claim_hash;
556 for (i = 0; i < hash->size; i++) {
557 head = &hash->table[i];
560 hlist_for_each_entry_rcu(claim, head, hash_entry) {
561 /* only own claims are interesting */
562 if (claim->backbone_gw != backbone_gw)
565 batadv_bla_send_claim(bat_priv, claim->addr, claim->vid,
566 BATADV_CLAIM_TYPE_CLAIM);
571 /* finally, send an announcement frame */
572 batadv_bla_send_announce(bat_priv, backbone_gw);
573 batadv_backbone_gw_free_ref(backbone_gw);
577 * batadv_bla_send_request - send a request to repeat claims
578 * @backbone_gw: the backbone gateway from whom we are out of sync
580 * When the crc is wrong, ask the backbone gateway for a full table update.
581 * After the request, it will repeat all of his own claims and finally
582 * send an announcement claim with which we can check again.
584 static void batadv_bla_send_request(struct batadv_bla_backbone_gw *backbone_gw)
586 /* first, remove all old entries */
587 batadv_bla_del_backbone_claims(backbone_gw);
589 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
590 "Sending REQUEST to %pM\n", backbone_gw->orig);
593 batadv_bla_send_claim(backbone_gw->bat_priv, backbone_gw->orig,
594 backbone_gw->vid, BATADV_CLAIM_TYPE_REQUEST);
596 /* no local broadcasts should be sent or received, for now. */
597 if (!atomic_read(&backbone_gw->request_sent)) {
598 atomic_inc(&backbone_gw->bat_priv->bla.num_requests);
599 atomic_set(&backbone_gw->request_sent, 1);
604 * batadv_bla_send_announce - Send an announcement frame
605 * @bat_priv: the bat priv with all the soft interface information
606 * @backbone_gw: our backbone gateway which should be announced
608 static void batadv_bla_send_announce(struct batadv_priv *bat_priv,
609 struct batadv_bla_backbone_gw *backbone_gw)
614 memcpy(mac, batadv_announce_mac, 4);
615 spin_lock_bh(&backbone_gw->crc_lock);
616 crc = htons(backbone_gw->crc);
617 spin_unlock_bh(&backbone_gw->crc_lock);
618 memcpy(&mac[4], &crc, 2);
620 batadv_bla_send_claim(bat_priv, mac, backbone_gw->vid,
621 BATADV_CLAIM_TYPE_ANNOUNCE);
625 * batadv_bla_add_claim - Adds a claim in the claim hash
626 * @bat_priv: the bat priv with all the soft interface information
627 * @mac: the mac address of the claim
628 * @vid: the VLAN ID of the frame
629 * @backbone_gw: the backbone gateway which claims it
631 static void batadv_bla_add_claim(struct batadv_priv *bat_priv,
632 const u8 *mac, const unsigned short vid,
633 struct batadv_bla_backbone_gw *backbone_gw)
635 struct batadv_bla_claim *claim;
636 struct batadv_bla_claim search_claim;
639 ether_addr_copy(search_claim.addr, mac);
640 search_claim.vid = vid;
641 claim = batadv_claim_hash_find(bat_priv, &search_claim);
643 /* create a new claim entry if it does not exist yet. */
645 claim = kzalloc(sizeof(*claim), GFP_ATOMIC);
649 ether_addr_copy(claim->addr, mac);
651 claim->lasttime = jiffies;
652 claim->backbone_gw = backbone_gw;
654 atomic_set(&claim->refcount, 2);
655 batadv_dbg(BATADV_DBG_BLA, bat_priv,
656 "bla_add_claim(): adding new entry %pM, vid %d to hash ...\n",
657 mac, BATADV_PRINT_VID(vid));
658 hash_added = batadv_hash_add(bat_priv->bla.claim_hash,
659 batadv_compare_claim,
660 batadv_choose_claim, claim,
663 if (unlikely(hash_added != 0)) {
664 /* only local changes happened. */
669 claim->lasttime = jiffies;
670 if (claim->backbone_gw == backbone_gw)
671 /* no need to register a new backbone */
674 batadv_dbg(BATADV_DBG_BLA, bat_priv,
675 "bla_add_claim(): changing ownership for %pM, vid %d\n",
676 mac, BATADV_PRINT_VID(vid));
678 spin_lock_bh(&claim->backbone_gw->crc_lock);
679 claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
680 spin_unlock_bh(&claim->backbone_gw->crc_lock);
681 batadv_backbone_gw_free_ref(claim->backbone_gw);
683 /* set (new) backbone gw */
684 kref_get(&backbone_gw->refcount);
685 claim->backbone_gw = backbone_gw;
687 spin_lock_bh(&backbone_gw->crc_lock);
688 backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
689 spin_unlock_bh(&backbone_gw->crc_lock);
690 backbone_gw->lasttime = jiffies;
693 batadv_claim_free_ref(claim);
697 * batadv_bla_del_claim - delete a claim from the claim hash
698 * @bat_priv: the bat priv with all the soft interface information
699 * @mac: mac address of the claim to be removed
700 * @vid: VLAN id for the claim to be removed
702 static void batadv_bla_del_claim(struct batadv_priv *bat_priv,
703 const u8 *mac, const unsigned short vid)
705 struct batadv_bla_claim search_claim, *claim;
707 ether_addr_copy(search_claim.addr, mac);
708 search_claim.vid = vid;
709 claim = batadv_claim_hash_find(bat_priv, &search_claim);
713 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla_del_claim(): %pM, vid %d\n",
714 mac, BATADV_PRINT_VID(vid));
716 batadv_hash_remove(bat_priv->bla.claim_hash, batadv_compare_claim,
717 batadv_choose_claim, claim);
718 batadv_claim_free_ref(claim); /* reference from the hash is gone */
720 spin_lock_bh(&claim->backbone_gw->crc_lock);
721 claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
722 spin_unlock_bh(&claim->backbone_gw->crc_lock);
724 /* don't need the reference from hash_find() anymore */
725 batadv_claim_free_ref(claim);
729 * batadv_handle_announce - check for ANNOUNCE frame
730 * @bat_priv: the bat priv with all the soft interface information
731 * @an_addr: announcement mac address (ARP Sender HW address)
732 * @backbone_addr: originator address of the sender (Ethernet source MAC)
733 * @vid: the VLAN ID of the frame
735 * Return: 1 if handled
737 static int batadv_handle_announce(struct batadv_priv *bat_priv, u8 *an_addr,
738 u8 *backbone_addr, unsigned short vid)
740 struct batadv_bla_backbone_gw *backbone_gw;
741 u16 backbone_crc, crc;
743 if (memcmp(an_addr, batadv_announce_mac, 4) != 0)
746 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
749 if (unlikely(!backbone_gw))
752 /* handle as ANNOUNCE frame */
753 backbone_gw->lasttime = jiffies;
754 crc = ntohs(*((__be16 *)(&an_addr[4])));
756 batadv_dbg(BATADV_DBG_BLA, bat_priv,
757 "handle_announce(): ANNOUNCE vid %d (sent by %pM)... CRC = %#.4x\n",
758 BATADV_PRINT_VID(vid), backbone_gw->orig, crc);
760 spin_lock_bh(&backbone_gw->crc_lock);
761 backbone_crc = backbone_gw->crc;
762 spin_unlock_bh(&backbone_gw->crc_lock);
764 if (backbone_crc != crc) {
765 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
766 "handle_announce(): CRC FAILED for %pM/%d (my = %#.4x, sent = %#.4x)\n",
768 BATADV_PRINT_VID(backbone_gw->vid),
771 batadv_bla_send_request(backbone_gw);
773 /* if we have sent a request and the crc was OK,
774 * we can allow traffic again.
776 if (atomic_read(&backbone_gw->request_sent)) {
777 atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
778 atomic_set(&backbone_gw->request_sent, 0);
782 batadv_backbone_gw_free_ref(backbone_gw);
787 * batadv_handle_request - check for REQUEST frame
788 * @bat_priv: the bat priv with all the soft interface information
789 * @primary_if: the primary hard interface of this batman soft interface
790 * @backbone_addr: backbone address to be requested (ARP sender HW MAC)
791 * @ethhdr: ethernet header of a packet
792 * @vid: the VLAN ID of the frame
794 * Return: 1 if handled
796 static int batadv_handle_request(struct batadv_priv *bat_priv,
797 struct batadv_hard_iface *primary_if,
798 u8 *backbone_addr, struct ethhdr *ethhdr,
801 /* check for REQUEST frame */
802 if (!batadv_compare_eth(backbone_addr, ethhdr->h_dest))
805 /* sanity check, this should not happen on a normal switch,
806 * we ignore it in this case.
808 if (!batadv_compare_eth(ethhdr->h_dest, primary_if->net_dev->dev_addr))
811 batadv_dbg(BATADV_DBG_BLA, bat_priv,
812 "handle_request(): REQUEST vid %d (sent by %pM)...\n",
813 BATADV_PRINT_VID(vid), ethhdr->h_source);
815 batadv_bla_answer_request(bat_priv, primary_if, vid);
820 * batadv_handle_unclaim - check for UNCLAIM frame
821 * @bat_priv: the bat priv with all the soft interface information
822 * @primary_if: the primary hard interface of this batman soft interface
823 * @backbone_addr: originator address of the backbone (Ethernet source)
824 * @claim_addr: Client to be unclaimed (ARP sender HW MAC)
825 * @vid: the VLAN ID of the frame
827 * Return: 1 if handled
829 static int batadv_handle_unclaim(struct batadv_priv *bat_priv,
830 struct batadv_hard_iface *primary_if,
831 u8 *backbone_addr, u8 *claim_addr,
834 struct batadv_bla_backbone_gw *backbone_gw;
836 /* unclaim in any case if it is our own */
837 if (primary_if && batadv_compare_eth(backbone_addr,
838 primary_if->net_dev->dev_addr))
839 batadv_bla_send_claim(bat_priv, claim_addr, vid,
840 BATADV_CLAIM_TYPE_UNCLAIM);
842 backbone_gw = batadv_backbone_hash_find(bat_priv, backbone_addr, vid);
847 /* this must be an UNCLAIM frame */
848 batadv_dbg(BATADV_DBG_BLA, bat_priv,
849 "handle_unclaim(): UNCLAIM %pM on vid %d (sent by %pM)...\n",
850 claim_addr, BATADV_PRINT_VID(vid), backbone_gw->orig);
852 batadv_bla_del_claim(bat_priv, claim_addr, vid);
853 batadv_backbone_gw_free_ref(backbone_gw);
858 * batadv_handle_claim - check for CLAIM frame
859 * @bat_priv: the bat priv with all the soft interface information
860 * @primary_if: the primary hard interface of this batman soft interface
861 * @backbone_addr: originator address of the backbone (Ethernet Source)
862 * @claim_addr: client mac address to be claimed (ARP sender HW MAC)
863 * @vid: the VLAN ID of the frame
865 * Return: 1 if handled
867 static int batadv_handle_claim(struct batadv_priv *bat_priv,
868 struct batadv_hard_iface *primary_if,
869 u8 *backbone_addr, u8 *claim_addr,
872 struct batadv_bla_backbone_gw *backbone_gw;
874 /* register the gateway if not yet available, and add the claim. */
876 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
879 if (unlikely(!backbone_gw))
882 /* this must be a CLAIM frame */
883 batadv_bla_add_claim(bat_priv, claim_addr, vid, backbone_gw);
884 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
885 batadv_bla_send_claim(bat_priv, claim_addr, vid,
886 BATADV_CLAIM_TYPE_CLAIM);
888 /* TODO: we could call something like tt_local_del() here. */
890 batadv_backbone_gw_free_ref(backbone_gw);
895 * batadv_check_claim_group - check for claim group membership
896 * @bat_priv: the bat priv with all the soft interface information
897 * @primary_if: the primary interface of this batman interface
898 * @hw_src: the Hardware source in the ARP Header
899 * @hw_dst: the Hardware destination in the ARP Header
900 * @ethhdr: pointer to the Ethernet header of the claim frame
902 * checks if it is a claim packet and if its on the same group.
903 * This function also applies the group ID of the sender
904 * if it is in the same mesh.
907 * 2 - if it is a claim packet and on the same group
908 * 1 - if is a claim packet from another group
909 * 0 - if it is not a claim packet
911 static int batadv_check_claim_group(struct batadv_priv *bat_priv,
912 struct batadv_hard_iface *primary_if,
913 u8 *hw_src, u8 *hw_dst,
914 struct ethhdr *ethhdr)
917 struct batadv_orig_node *orig_node;
918 struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
920 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
921 bla_dst_own = &bat_priv->bla.claim_dest;
923 /* if announcement packet, use the source,
924 * otherwise assume it is in the hw_src
926 switch (bla_dst->type) {
927 case BATADV_CLAIM_TYPE_CLAIM:
928 backbone_addr = hw_src;
930 case BATADV_CLAIM_TYPE_REQUEST:
931 case BATADV_CLAIM_TYPE_ANNOUNCE:
932 case BATADV_CLAIM_TYPE_UNCLAIM:
933 backbone_addr = ethhdr->h_source;
939 /* don't accept claim frames from ourselves */
940 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
943 /* if its already the same group, it is fine. */
944 if (bla_dst->group == bla_dst_own->group)
947 /* lets see if this originator is in our mesh */
948 orig_node = batadv_orig_hash_find(bat_priv, backbone_addr);
950 /* dont accept claims from gateways which are not in
951 * the same mesh or group.
956 /* if our mesh friends mac is bigger, use it for ourselves. */
957 if (ntohs(bla_dst->group) > ntohs(bla_dst_own->group)) {
958 batadv_dbg(BATADV_DBG_BLA, bat_priv,
959 "taking other backbones claim group: %#.4x\n",
960 ntohs(bla_dst->group));
961 bla_dst_own->group = bla_dst->group;
964 batadv_orig_node_free_ref(orig_node);
970 * batadv_bla_process_claim - Check if this is a claim frame, and process it
971 * @bat_priv: the bat priv with all the soft interface information
972 * @primary_if: the primary hard interface of this batman soft interface
973 * @skb: the frame to be checked
975 * Return: 1 if it was a claim frame, otherwise return 0 to
976 * tell the callee that it can use the frame on its own.
978 static int batadv_bla_process_claim(struct batadv_priv *bat_priv,
979 struct batadv_hard_iface *primary_if,
982 struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
984 struct vlan_hdr *vhdr, vhdr_buf;
985 struct ethhdr *ethhdr;
986 struct arphdr *arphdr;
993 vid = batadv_get_vid(skb, 0);
994 ethhdr = eth_hdr(skb);
996 proto = ethhdr->h_proto;
998 if (vid & BATADV_VLAN_HAS_TAG) {
999 /* Traverse the VLAN/Ethertypes.
1001 * At this point it is known that the first protocol is a VLAN
1002 * header, so start checking at the encapsulated protocol.
1004 * The depth of the VLAN headers is recorded to drop BLA claim
1005 * frames encapsulated into multiple VLAN headers (QinQ).
1008 vhdr = skb_header_pointer(skb, headlen, VLAN_HLEN,
1013 proto = vhdr->h_vlan_encapsulated_proto;
1014 headlen += VLAN_HLEN;
1016 } while (proto == htons(ETH_P_8021Q));
1019 if (proto != htons(ETH_P_ARP))
1020 return 0; /* not a claim frame */
1022 /* this must be a ARP frame. check if it is a claim. */
1024 if (unlikely(!pskb_may_pull(skb, headlen + arp_hdr_len(skb->dev))))
1027 /* pskb_may_pull() may have modified the pointers, get ethhdr again */
1028 ethhdr = eth_hdr(skb);
1029 arphdr = (struct arphdr *)((u8 *)ethhdr + headlen);
1031 /* Check whether the ARP frame carries a valid
1034 if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
1036 if (arphdr->ar_pro != htons(ETH_P_IP))
1038 if (arphdr->ar_hln != ETH_ALEN)
1040 if (arphdr->ar_pln != 4)
1043 hw_src = (u8 *)arphdr + sizeof(struct arphdr);
1044 hw_dst = hw_src + ETH_ALEN + 4;
1045 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
1046 bla_dst_own = &bat_priv->bla.claim_dest;
1048 /* check if it is a claim frame in general */
1049 if (memcmp(bla_dst->magic, bla_dst_own->magic,
1050 sizeof(bla_dst->magic)) != 0)
1053 /* check if there is a claim frame encapsulated deeper in (QinQ) and
1054 * drop that, as this is not supported by BLA but should also not be
1055 * sent via the mesh.
1060 /* check if it is a claim frame. */
1061 ret = batadv_check_claim_group(bat_priv, primary_if, hw_src, hw_dst,
1064 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1065 "bla_process_claim(): received a claim frame from another group. From: %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n",
1066 ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src,
1072 /* become a backbone gw ourselves on this vlan if not happened yet */
1073 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
1075 /* check for the different types of claim frames ... */
1076 switch (bla_dst->type) {
1077 case BATADV_CLAIM_TYPE_CLAIM:
1078 if (batadv_handle_claim(bat_priv, primary_if, hw_src,
1079 ethhdr->h_source, vid))
1082 case BATADV_CLAIM_TYPE_UNCLAIM:
1083 if (batadv_handle_unclaim(bat_priv, primary_if,
1084 ethhdr->h_source, hw_src, vid))
1088 case BATADV_CLAIM_TYPE_ANNOUNCE:
1089 if (batadv_handle_announce(bat_priv, hw_src, ethhdr->h_source,
1093 case BATADV_CLAIM_TYPE_REQUEST:
1094 if (batadv_handle_request(bat_priv, primary_if, hw_src, ethhdr,
1100 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1101 "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",
1102 ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src, hw_dst);
1107 * batadv_bla_purge_backbone_gw - Remove backbone gateways after a timeout or
1109 * @bat_priv: the bat priv with all the soft interface information
1110 * @now: whether the whole hash shall be wiped now
1112 * Check when we last heard from other nodes, and remove them in case of
1113 * a time out, or clean all backbone gws if now is set.
1115 static void batadv_bla_purge_backbone_gw(struct batadv_priv *bat_priv, int now)
1117 struct batadv_bla_backbone_gw *backbone_gw;
1118 struct hlist_node *node_tmp;
1119 struct hlist_head *head;
1120 struct batadv_hashtable *hash;
1121 spinlock_t *list_lock; /* protects write access to the hash lists */
1124 hash = bat_priv->bla.backbone_hash;
1128 for (i = 0; i < hash->size; i++) {
1129 head = &hash->table[i];
1130 list_lock = &hash->list_locks[i];
1132 spin_lock_bh(list_lock);
1133 hlist_for_each_entry_safe(backbone_gw, node_tmp,
1137 if (!batadv_has_timed_out(backbone_gw->lasttime,
1138 BATADV_BLA_BACKBONE_TIMEOUT))
1141 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
1142 "bla_purge_backbone_gw(): backbone gw %pM timed out\n",
1146 /* don't wait for the pending request anymore */
1147 if (atomic_read(&backbone_gw->request_sent))
1148 atomic_dec(&bat_priv->bla.num_requests);
1150 batadv_bla_del_backbone_claims(backbone_gw);
1152 hlist_del_rcu(&backbone_gw->hash_entry);
1153 batadv_backbone_gw_free_ref(backbone_gw);
1155 spin_unlock_bh(list_lock);
1160 * batadv_bla_purge_claims - Remove claims after a timeout or immediately
1161 * @bat_priv: the bat priv with all the soft interface information
1162 * @primary_if: the selected primary interface, may be NULL if now is set
1163 * @now: whether the whole hash shall be wiped now
1165 * Check when we heard last time from our own claims, and remove them in case of
1166 * a time out, or clean all claims if now is set
1168 static void batadv_bla_purge_claims(struct batadv_priv *bat_priv,
1169 struct batadv_hard_iface *primary_if,
1172 struct batadv_bla_claim *claim;
1173 struct hlist_head *head;
1174 struct batadv_hashtable *hash;
1177 hash = bat_priv->bla.claim_hash;
1181 for (i = 0; i < hash->size; i++) {
1182 head = &hash->table[i];
1185 hlist_for_each_entry_rcu(claim, head, hash_entry) {
1188 if (!batadv_compare_eth(claim->backbone_gw->orig,
1189 primary_if->net_dev->dev_addr))
1191 if (!batadv_has_timed_out(claim->lasttime,
1192 BATADV_BLA_CLAIM_TIMEOUT))
1195 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1196 "bla_purge_claims(): %pM, vid %d, time out\n",
1197 claim->addr, claim->vid);
1200 batadv_handle_unclaim(bat_priv, primary_if,
1201 claim->backbone_gw->orig,
1202 claim->addr, claim->vid);
1209 * batadv_bla_update_orig_address - Update the backbone gateways when the own
1210 * originator address changes
1211 * @bat_priv: the bat priv with all the soft interface information
1212 * @primary_if: the new selected primary_if
1213 * @oldif: the old primary interface, may be NULL
1215 void batadv_bla_update_orig_address(struct batadv_priv *bat_priv,
1216 struct batadv_hard_iface *primary_if,
1217 struct batadv_hard_iface *oldif)
1219 struct batadv_bla_backbone_gw *backbone_gw;
1220 struct hlist_head *head;
1221 struct batadv_hashtable *hash;
1225 /* reset bridge loop avoidance group id */
1226 group = htons(crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN));
1227 bat_priv->bla.claim_dest.group = group;
1229 /* purge everything when bridge loop avoidance is turned off */
1230 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1234 batadv_bla_purge_claims(bat_priv, NULL, 1);
1235 batadv_bla_purge_backbone_gw(bat_priv, 1);
1239 hash = bat_priv->bla.backbone_hash;
1243 for (i = 0; i < hash->size; i++) {
1244 head = &hash->table[i];
1247 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
1248 /* own orig still holds the old value. */
1249 if (!batadv_compare_eth(backbone_gw->orig,
1250 oldif->net_dev->dev_addr))
1253 ether_addr_copy(backbone_gw->orig,
1254 primary_if->net_dev->dev_addr);
1255 /* send an announce frame so others will ask for our
1256 * claims and update their tables.
1258 batadv_bla_send_announce(bat_priv, backbone_gw);
1265 * batadv_bla_status_update - purge bla interfaces if necessary
1266 * @net_dev: the soft interface net device
1268 void batadv_bla_status_update(struct net_device *net_dev)
1270 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1271 struct batadv_hard_iface *primary_if;
1273 primary_if = batadv_primary_if_get_selected(bat_priv);
1277 /* this function already purges everything when bla is disabled,
1278 * so just call that one.
1280 batadv_bla_update_orig_address(bat_priv, primary_if, primary_if);
1281 batadv_hardif_free_ref(primary_if);
1285 * batadv_bla_periodic_work - performs periodic bla work
1286 * @work: kernel work struct
1288 * periodic work to do:
1289 * * purge structures when they are too old
1290 * * send announcements
1292 static void batadv_bla_periodic_work(struct work_struct *work)
1294 struct delayed_work *delayed_work;
1295 struct batadv_priv *bat_priv;
1296 struct batadv_priv_bla *priv_bla;
1297 struct hlist_head *head;
1298 struct batadv_bla_backbone_gw *backbone_gw;
1299 struct batadv_hashtable *hash;
1300 struct batadv_hard_iface *primary_if;
1303 delayed_work = container_of(work, struct delayed_work, work);
1304 priv_bla = container_of(delayed_work, struct batadv_priv_bla, work);
1305 bat_priv = container_of(priv_bla, struct batadv_priv, bla);
1306 primary_if = batadv_primary_if_get_selected(bat_priv);
1310 batadv_bla_purge_claims(bat_priv, primary_if, 0);
1311 batadv_bla_purge_backbone_gw(bat_priv, 0);
1313 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1316 hash = bat_priv->bla.backbone_hash;
1320 for (i = 0; i < hash->size; i++) {
1321 head = &hash->table[i];
1324 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
1325 if (!batadv_compare_eth(backbone_gw->orig,
1326 primary_if->net_dev->dev_addr))
1329 backbone_gw->lasttime = jiffies;
1331 batadv_bla_send_announce(bat_priv, backbone_gw);
1333 /* request_sent is only set after creation to avoid
1334 * problems when we are not yet known as backbone gw
1337 * We can reset this now after we waited some periods
1338 * to give bridge forward delays and bla group forming
1342 if (atomic_read(&backbone_gw->request_sent) == 0)
1345 if (!atomic_dec_and_test(&backbone_gw->wait_periods))
1348 atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
1349 atomic_set(&backbone_gw->request_sent, 0);
1355 batadv_hardif_free_ref(primary_if);
1357 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1358 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
1361 /* The hash for claim and backbone hash receive the same key because they
1362 * are getting initialized by hash_new with the same key. Reinitializing
1363 * them with to different keys to allow nested locking without generating
1366 static struct lock_class_key batadv_claim_hash_lock_class_key;
1367 static struct lock_class_key batadv_backbone_hash_lock_class_key;
1370 * batadv_bla_init - initialize all bla structures
1371 * @bat_priv: the bat priv with all the soft interface information
1373 * Return: 0 on success, < 0 on error.
1375 int batadv_bla_init(struct batadv_priv *bat_priv)
1378 u8 claim_dest[ETH_ALEN] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00};
1379 struct batadv_hard_iface *primary_if;
1381 unsigned long entrytime;
1383 spin_lock_init(&bat_priv->bla.bcast_duplist_lock);
1385 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hash registering\n");
1387 /* setting claim destination address */
1388 memcpy(&bat_priv->bla.claim_dest.magic, claim_dest, 3);
1389 bat_priv->bla.claim_dest.type = 0;
1390 primary_if = batadv_primary_if_get_selected(bat_priv);
1392 crc = crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN);
1393 bat_priv->bla.claim_dest.group = htons(crc);
1394 batadv_hardif_free_ref(primary_if);
1396 bat_priv->bla.claim_dest.group = 0; /* will be set later */
1399 /* initialize the duplicate list */
1400 entrytime = jiffies - msecs_to_jiffies(BATADV_DUPLIST_TIMEOUT);
1401 for (i = 0; i < BATADV_DUPLIST_SIZE; i++)
1402 bat_priv->bla.bcast_duplist[i].entrytime = entrytime;
1403 bat_priv->bla.bcast_duplist_curr = 0;
1405 if (bat_priv->bla.claim_hash)
1408 bat_priv->bla.claim_hash = batadv_hash_new(128);
1409 bat_priv->bla.backbone_hash = batadv_hash_new(32);
1411 if (!bat_priv->bla.claim_hash || !bat_priv->bla.backbone_hash)
1414 batadv_hash_set_lock_class(bat_priv->bla.claim_hash,
1415 &batadv_claim_hash_lock_class_key);
1416 batadv_hash_set_lock_class(bat_priv->bla.backbone_hash,
1417 &batadv_backbone_hash_lock_class_key);
1419 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hashes initialized\n");
1421 INIT_DELAYED_WORK(&bat_priv->bla.work, batadv_bla_periodic_work);
1423 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1424 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
1429 * batadv_bla_check_bcast_duplist - Check if a frame is in the broadcast dup.
1430 * @bat_priv: the bat priv with all the soft interface information
1431 * @skb: contains the bcast_packet to be checked
1433 * check if it is on our broadcast list. Another gateway might
1434 * have sent the same packet because it is connected to the same backbone,
1435 * so we have to remove this duplicate.
1437 * This is performed by checking the CRC, which will tell us
1438 * with a good chance that it is the same packet. If it is furthermore
1439 * sent by another host, drop it. We allow equal packets from
1440 * the same host however as this might be intended.
1442 * Return: 1 if a packet is in the duplicate list, 0 otherwise.
1444 int batadv_bla_check_bcast_duplist(struct batadv_priv *bat_priv,
1445 struct sk_buff *skb)
1447 int i, curr, ret = 0;
1449 struct batadv_bcast_packet *bcast_packet;
1450 struct batadv_bcast_duplist_entry *entry;
1452 bcast_packet = (struct batadv_bcast_packet *)skb->data;
1454 /* calculate the crc ... */
1455 crc = batadv_skb_crc32(skb, (u8 *)(bcast_packet + 1));
1457 spin_lock_bh(&bat_priv->bla.bcast_duplist_lock);
1459 for (i = 0; i < BATADV_DUPLIST_SIZE; i++) {
1460 curr = (bat_priv->bla.bcast_duplist_curr + i);
1461 curr %= BATADV_DUPLIST_SIZE;
1462 entry = &bat_priv->bla.bcast_duplist[curr];
1464 /* we can stop searching if the entry is too old ;
1465 * later entries will be even older
1467 if (batadv_has_timed_out(entry->entrytime,
1468 BATADV_DUPLIST_TIMEOUT))
1471 if (entry->crc != crc)
1474 if (batadv_compare_eth(entry->orig, bcast_packet->orig))
1477 /* this entry seems to match: same crc, not too old,
1478 * and from another gw. therefore return 1 to forbid it.
1483 /* not found, add a new entry (overwrite the oldest entry)
1484 * and allow it, its the first occurrence.
1486 curr = (bat_priv->bla.bcast_duplist_curr + BATADV_DUPLIST_SIZE - 1);
1487 curr %= BATADV_DUPLIST_SIZE;
1488 entry = &bat_priv->bla.bcast_duplist[curr];
1490 entry->entrytime = jiffies;
1491 ether_addr_copy(entry->orig, bcast_packet->orig);
1492 bat_priv->bla.bcast_duplist_curr = curr;
1495 spin_unlock_bh(&bat_priv->bla.bcast_duplist_lock);
1501 * batadv_bla_is_backbone_gw_orig - Check if the originator is a gateway for
1502 * the VLAN identified by vid.
1503 * @bat_priv: the bat priv with all the soft interface information
1504 * @orig: originator mac address
1505 * @vid: VLAN identifier
1507 * Return: true if orig is a backbone for this vid, false otherwise.
1509 bool batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, u8 *orig,
1512 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
1513 struct hlist_head *head;
1514 struct batadv_bla_backbone_gw *backbone_gw;
1517 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1523 for (i = 0; i < hash->size; i++) {
1524 head = &hash->table[i];
1527 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
1528 if (batadv_compare_eth(backbone_gw->orig, orig) &&
1529 backbone_gw->vid == vid) {
1541 * batadv_bla_is_backbone_gw - check if originator is a backbone gw for a VLAN.
1542 * @skb: the frame to be checked
1543 * @orig_node: the orig_node of the frame
1544 * @hdr_size: maximum length of the frame
1546 * Return: 1 if the orig_node is also a gateway on the soft interface, otherwise
1549 int batadv_bla_is_backbone_gw(struct sk_buff *skb,
1550 struct batadv_orig_node *orig_node, int hdr_size)
1552 struct batadv_bla_backbone_gw *backbone_gw;
1555 if (!atomic_read(&orig_node->bat_priv->bridge_loop_avoidance))
1558 /* first, find out the vid. */
1559 if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
1562 vid = batadv_get_vid(skb, hdr_size);
1564 /* see if this originator is a backbone gw for this VLAN */
1565 backbone_gw = batadv_backbone_hash_find(orig_node->bat_priv,
1566 orig_node->orig, vid);
1570 batadv_backbone_gw_free_ref(backbone_gw);
1575 * batadv_bla_init - free all bla structures
1576 * @bat_priv: the bat priv with all the soft interface information
1578 * for softinterface free or module unload
1580 void batadv_bla_free(struct batadv_priv *bat_priv)
1582 struct batadv_hard_iface *primary_if;
1584 cancel_delayed_work_sync(&bat_priv->bla.work);
1585 primary_if = batadv_primary_if_get_selected(bat_priv);
1587 if (bat_priv->bla.claim_hash) {
1588 batadv_bla_purge_claims(bat_priv, primary_if, 1);
1589 batadv_hash_destroy(bat_priv->bla.claim_hash);
1590 bat_priv->bla.claim_hash = NULL;
1592 if (bat_priv->bla.backbone_hash) {
1593 batadv_bla_purge_backbone_gw(bat_priv, 1);
1594 batadv_hash_destroy(bat_priv->bla.backbone_hash);
1595 bat_priv->bla.backbone_hash = NULL;
1598 batadv_hardif_free_ref(primary_if);
1602 * batadv_bla_rx - check packets coming from the mesh.
1603 * @bat_priv: the bat priv with all the soft interface information
1604 * @skb: the frame to be checked
1605 * @vid: the VLAN ID of the frame
1606 * @is_bcast: the packet came in a broadcast packet type.
1608 * batadv_bla_rx avoidance checks if:
1609 * * we have to race for a claim
1610 * * if the frame is allowed on the LAN
1612 * in these cases, the skb is further handled by this function
1614 * Return: 1 if handled, otherwise it returns 0 and the caller shall further
1617 int batadv_bla_rx(struct batadv_priv *bat_priv, struct sk_buff *skb,
1618 unsigned short vid, bool is_bcast)
1620 struct ethhdr *ethhdr;
1621 struct batadv_bla_claim search_claim, *claim = NULL;
1622 struct batadv_hard_iface *primary_if;
1625 ethhdr = eth_hdr(skb);
1627 primary_if = batadv_primary_if_get_selected(bat_priv);
1631 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1634 if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
1635 /* don't allow broadcasts while requests are in flight */
1636 if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast)
1639 ether_addr_copy(search_claim.addr, ethhdr->h_source);
1640 search_claim.vid = vid;
1641 claim = batadv_claim_hash_find(bat_priv, &search_claim);
1644 /* possible optimization: race for a claim */
1645 /* No claim exists yet, claim it for us!
1647 batadv_handle_claim(bat_priv, primary_if,
1648 primary_if->net_dev->dev_addr,
1649 ethhdr->h_source, vid);
1653 /* if it is our own claim ... */
1654 if (batadv_compare_eth(claim->backbone_gw->orig,
1655 primary_if->net_dev->dev_addr)) {
1656 /* ... allow it in any case */
1657 claim->lasttime = jiffies;
1661 /* if it is a broadcast ... */
1662 if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast) {
1663 /* ... drop it. the responsible gateway is in charge.
1665 * We need to check is_bcast because with the gateway
1666 * feature, broadcasts (like DHCP requests) may be sent
1667 * using a unicast packet type.
1671 /* seems the client considers us as its best gateway.
1672 * send a claim and update the claim table
1675 batadv_handle_claim(bat_priv, primary_if,
1676 primary_if->net_dev->dev_addr,
1677 ethhdr->h_source, vid);
1681 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
1691 batadv_hardif_free_ref(primary_if);
1693 batadv_claim_free_ref(claim);
1698 * batadv_bla_tx - check packets going into the mesh
1699 * @bat_priv: the bat priv with all the soft interface information
1700 * @skb: the frame to be checked
1701 * @vid: the VLAN ID of the frame
1703 * batadv_bla_tx checks if:
1704 * * a claim was received which has to be processed
1705 * * the frame is allowed on the mesh
1707 * in these cases, the skb is further handled by this function.
1709 * This call might reallocate skb data.
1711 * Return: 1 if handled, otherwise it returns 0 and the caller shall further
1714 int batadv_bla_tx(struct batadv_priv *bat_priv, struct sk_buff *skb,
1717 struct ethhdr *ethhdr;
1718 struct batadv_bla_claim search_claim, *claim = NULL;
1719 struct batadv_hard_iface *primary_if;
1722 primary_if = batadv_primary_if_get_selected(bat_priv);
1726 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1729 if (batadv_bla_process_claim(bat_priv, primary_if, skb))
1732 ethhdr = eth_hdr(skb);
1734 if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
1735 /* don't allow broadcasts while requests are in flight */
1736 if (is_multicast_ether_addr(ethhdr->h_dest))
1739 ether_addr_copy(search_claim.addr, ethhdr->h_source);
1740 search_claim.vid = vid;
1742 claim = batadv_claim_hash_find(bat_priv, &search_claim);
1744 /* if no claim exists, allow it. */
1748 /* check if we are responsible. */
1749 if (batadv_compare_eth(claim->backbone_gw->orig,
1750 primary_if->net_dev->dev_addr)) {
1751 /* if yes, the client has roamed and we have
1754 batadv_handle_unclaim(bat_priv, primary_if,
1755 primary_if->net_dev->dev_addr,
1756 ethhdr->h_source, vid);
1760 /* check if it is a multicast/broadcast frame */
1761 if (is_multicast_ether_addr(ethhdr->h_dest)) {
1762 /* drop it. the responsible gateway has forwarded it into
1763 * the backbone network.
1767 /* we must allow it. at least if we are
1768 * responsible for the DESTINATION.
1773 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
1780 batadv_hardif_free_ref(primary_if);
1782 batadv_claim_free_ref(claim);
1787 * batadv_bla_claim_table_seq_print_text - print the claim table in a seq file
1788 * @seq: seq file to print on
1793 int batadv_bla_claim_table_seq_print_text(struct seq_file *seq, void *offset)
1795 struct net_device *net_dev = (struct net_device *)seq->private;
1796 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1797 struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
1798 struct batadv_bla_claim *claim;
1799 struct batadv_hard_iface *primary_if;
1800 struct hlist_head *head;
1806 primary_if = batadv_seq_print_text_primary_if_get(seq);
1810 primary_addr = primary_if->net_dev->dev_addr;
1812 "Claims announced for the mesh %s (orig %pM, group id %#.4x)\n",
1813 net_dev->name, primary_addr,
1814 ntohs(bat_priv->bla.claim_dest.group));
1815 seq_printf(seq, " %-17s %-5s %-17s [o] (%-6s)\n",
1816 "Client", "VID", "Originator", "CRC");
1817 for (i = 0; i < hash->size; i++) {
1818 head = &hash->table[i];
1821 hlist_for_each_entry_rcu(claim, head, hash_entry) {
1822 is_own = batadv_compare_eth(claim->backbone_gw->orig,
1825 spin_lock_bh(&claim->backbone_gw->crc_lock);
1826 backbone_crc = claim->backbone_gw->crc;
1827 spin_unlock_bh(&claim->backbone_gw->crc_lock);
1828 seq_printf(seq, " * %pM on %5d by %pM [%c] (%#.4x)\n",
1829 claim->addr, BATADV_PRINT_VID(claim->vid),
1830 claim->backbone_gw->orig,
1831 (is_own ? 'x' : ' '),
1838 batadv_hardif_free_ref(primary_if);
1843 * batadv_bla_backbone_table_seq_print_text - print the backbone table in a seq
1845 * @seq: seq file to print on
1850 int batadv_bla_backbone_table_seq_print_text(struct seq_file *seq, void *offset)
1852 struct net_device *net_dev = (struct net_device *)seq->private;
1853 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1854 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
1855 struct batadv_bla_backbone_gw *backbone_gw;
1856 struct batadv_hard_iface *primary_if;
1857 struct hlist_head *head;
1864 primary_if = batadv_seq_print_text_primary_if_get(seq);
1868 primary_addr = primary_if->net_dev->dev_addr;
1870 "Backbones announced for the mesh %s (orig %pM, group id %#.4x)\n",
1871 net_dev->name, primary_addr,
1872 ntohs(bat_priv->bla.claim_dest.group));
1873 seq_printf(seq, " %-17s %-5s %-9s (%-6s)\n",
1874 "Originator", "VID", "last seen", "CRC");
1875 for (i = 0; i < hash->size; i++) {
1876 head = &hash->table[i];
1879 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
1880 msecs = jiffies_to_msecs(jiffies -
1881 backbone_gw->lasttime);
1882 secs = msecs / 1000;
1883 msecs = msecs % 1000;
1885 is_own = batadv_compare_eth(backbone_gw->orig,
1890 spin_lock_bh(&backbone_gw->crc_lock);
1891 backbone_crc = backbone_gw->crc;
1892 spin_unlock_bh(&backbone_gw->crc_lock);
1894 seq_printf(seq, " * %pM on %5d %4i.%03is (%#.4x)\n",
1896 BATADV_PRINT_VID(backbone_gw->vid), secs,
1897 msecs, backbone_crc);
1903 batadv_hardif_free_ref(primary_if);