1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2011-2018 B.A.T.M.A.N. contributors:
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #include "bridge_loop_avoidance.h"
22 #include <linux/atomic.h>
23 #include <linux/byteorder/generic.h>
24 #include <linux/compiler.h>
25 #include <linux/crc16.h>
26 #include <linux/errno.h>
27 #include <linux/etherdevice.h>
28 #include <linux/gfp.h>
29 #include <linux/if_arp.h>
30 #include <linux/if_ether.h>
31 #include <linux/if_vlan.h>
32 #include <linux/jhash.h>
33 #include <linux/jiffies.h>
34 #include <linux/kernel.h>
35 #include <linux/kref.h>
36 #include <linux/list.h>
37 #include <linux/lockdep.h>
38 #include <linux/netdevice.h>
39 #include <linux/netlink.h>
40 #include <linux/preempt.h>
41 #include <linux/rculist.h>
42 #include <linux/rcupdate.h>
43 #include <linux/seq_file.h>
44 #include <linux/skbuff.h>
45 #include <linux/slab.h>
46 #include <linux/spinlock.h>
47 #include <linux/stddef.h>
48 #include <linux/string.h>
49 #include <linux/workqueue.h>
51 #include <net/genetlink.h>
52 #include <net/netlink.h>
54 #include <uapi/linux/batadv_packet.h>
55 #include <uapi/linux/batman_adv.h>
57 #include "hard-interface.h"
61 #include "originator.h"
62 #include "soft-interface.h"
64 #include "translation-table.h"
66 static const u8 batadv_announce_mac[4] = {0x43, 0x05, 0x43, 0x05};
68 static void batadv_bla_periodic_work(struct work_struct *work);
70 batadv_bla_send_announce(struct batadv_priv *bat_priv,
71 struct batadv_bla_backbone_gw *backbone_gw);
74 * batadv_choose_claim() - choose the right bucket for a claim.
76 * @size: size of the hash table
78 * Return: the hash index of the claim
80 static inline u32 batadv_choose_claim(const void *data, u32 size)
82 struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
85 hash = jhash(&claim->addr, sizeof(claim->addr), hash);
86 hash = jhash(&claim->vid, sizeof(claim->vid), hash);
92 * batadv_choose_backbone_gw() - choose the right bucket for a backbone gateway.
94 * @size: size of the hash table
96 * Return: the hash index of the backbone gateway
98 static inline u32 batadv_choose_backbone_gw(const void *data, u32 size)
100 const struct batadv_bla_backbone_gw *gw;
103 gw = (struct batadv_bla_backbone_gw *)data;
104 hash = jhash(&gw->orig, sizeof(gw->orig), hash);
105 hash = jhash(&gw->vid, sizeof(gw->vid), hash);
111 * batadv_compare_backbone_gw() - compare address and vid of two backbone gws
112 * @node: list node of the first entry to compare
113 * @data2: pointer to the second backbone gateway
115 * Return: true if the backbones have the same data, false otherwise
117 static bool batadv_compare_backbone_gw(const struct hlist_node *node,
120 const void *data1 = container_of(node, struct batadv_bla_backbone_gw,
122 const struct batadv_bla_backbone_gw *gw1 = data1;
123 const struct batadv_bla_backbone_gw *gw2 = data2;
125 if (!batadv_compare_eth(gw1->orig, gw2->orig))
128 if (gw1->vid != gw2->vid)
135 * batadv_compare_claim() - compare address and vid of two claims
136 * @node: list node of the first entry to compare
137 * @data2: pointer to the second claims
139 * Return: true if the claim have the same data, 0 otherwise
141 static bool batadv_compare_claim(const struct hlist_node *node,
144 const void *data1 = container_of(node, struct batadv_bla_claim,
146 const struct batadv_bla_claim *cl1 = data1;
147 const struct batadv_bla_claim *cl2 = data2;
149 if (!batadv_compare_eth(cl1->addr, cl2->addr))
152 if (cl1->vid != cl2->vid)
159 * batadv_backbone_gw_release() - release backbone gw from lists and queue for
160 * free after rcu grace period
161 * @ref: kref pointer of the backbone gw
163 static void batadv_backbone_gw_release(struct kref *ref)
165 struct batadv_bla_backbone_gw *backbone_gw;
167 backbone_gw = container_of(ref, struct batadv_bla_backbone_gw,
170 kfree_rcu(backbone_gw, rcu);
174 * batadv_backbone_gw_put() - decrement the backbone gw refcounter and possibly
176 * @backbone_gw: backbone gateway to be free'd
178 static void batadv_backbone_gw_put(struct batadv_bla_backbone_gw *backbone_gw)
180 kref_put(&backbone_gw->refcount, batadv_backbone_gw_release);
184 * batadv_claim_release() - release claim from lists and queue for free after
186 * @ref: kref pointer of the claim
188 static void batadv_claim_release(struct kref *ref)
190 struct batadv_bla_claim *claim;
191 struct batadv_bla_backbone_gw *old_backbone_gw;
193 claim = container_of(ref, struct batadv_bla_claim, refcount);
195 spin_lock_bh(&claim->backbone_lock);
196 old_backbone_gw = claim->backbone_gw;
197 claim->backbone_gw = NULL;
198 spin_unlock_bh(&claim->backbone_lock);
200 spin_lock_bh(&old_backbone_gw->crc_lock);
201 old_backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
202 spin_unlock_bh(&old_backbone_gw->crc_lock);
204 batadv_backbone_gw_put(old_backbone_gw);
206 kfree_rcu(claim, rcu);
210 * batadv_claim_put() - decrement the claim refcounter and possibly release it
211 * @claim: claim to be free'd
213 static void batadv_claim_put(struct batadv_bla_claim *claim)
215 kref_put(&claim->refcount, batadv_claim_release);
219 * batadv_claim_hash_find() - looks for a claim in the claim hash
220 * @bat_priv: the bat priv with all the soft interface information
221 * @data: search data (may be local/static data)
223 * Return: claim if found or NULL otherwise.
225 static struct batadv_bla_claim *
226 batadv_claim_hash_find(struct batadv_priv *bat_priv,
227 struct batadv_bla_claim *data)
229 struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
230 struct hlist_head *head;
231 struct batadv_bla_claim *claim;
232 struct batadv_bla_claim *claim_tmp = NULL;
238 index = batadv_choose_claim(data, hash->size);
239 head = &hash->table[index];
242 hlist_for_each_entry_rcu(claim, head, hash_entry) {
243 if (!batadv_compare_claim(&claim->hash_entry, data))
246 if (!kref_get_unless_zero(&claim->refcount))
258 * batadv_backbone_hash_find() - looks for a backbone gateway in the hash
259 * @bat_priv: the bat priv with all the soft interface information
260 * @addr: the address of the originator
263 * Return: backbone gateway if found or NULL otherwise
265 static struct batadv_bla_backbone_gw *
266 batadv_backbone_hash_find(struct batadv_priv *bat_priv, u8 *addr,
269 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
270 struct hlist_head *head;
271 struct batadv_bla_backbone_gw search_entry, *backbone_gw;
272 struct batadv_bla_backbone_gw *backbone_gw_tmp = NULL;
278 ether_addr_copy(search_entry.orig, addr);
279 search_entry.vid = vid;
281 index = batadv_choose_backbone_gw(&search_entry, hash->size);
282 head = &hash->table[index];
285 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
286 if (!batadv_compare_backbone_gw(&backbone_gw->hash_entry,
290 if (!kref_get_unless_zero(&backbone_gw->refcount))
293 backbone_gw_tmp = backbone_gw;
298 return backbone_gw_tmp;
302 * batadv_bla_del_backbone_claims() - delete all claims for a backbone
303 * @backbone_gw: backbone gateway where the claims should be removed
306 batadv_bla_del_backbone_claims(struct batadv_bla_backbone_gw *backbone_gw)
308 struct batadv_hashtable *hash;
309 struct hlist_node *node_tmp;
310 struct hlist_head *head;
311 struct batadv_bla_claim *claim;
313 spinlock_t *list_lock; /* protects write access to the hash lists */
315 hash = backbone_gw->bat_priv->bla.claim_hash;
319 for (i = 0; i < hash->size; i++) {
320 head = &hash->table[i];
321 list_lock = &hash->list_locks[i];
323 spin_lock_bh(list_lock);
324 hlist_for_each_entry_safe(claim, node_tmp,
326 if (claim->backbone_gw != backbone_gw)
329 batadv_claim_put(claim);
330 hlist_del_rcu(&claim->hash_entry);
332 spin_unlock_bh(list_lock);
335 /* all claims gone, initialize CRC */
336 spin_lock_bh(&backbone_gw->crc_lock);
337 backbone_gw->crc = BATADV_BLA_CRC_INIT;
338 spin_unlock_bh(&backbone_gw->crc_lock);
342 * batadv_bla_send_claim() - sends a claim frame according to the provided info
343 * @bat_priv: the bat priv with all the soft interface information
344 * @mac: the mac address to be announced within the claim
346 * @claimtype: the type of the claim (CLAIM, UNCLAIM, ANNOUNCE, ...)
348 static void batadv_bla_send_claim(struct batadv_priv *bat_priv, u8 *mac,
349 unsigned short vid, int claimtype)
352 struct ethhdr *ethhdr;
353 struct batadv_hard_iface *primary_if;
354 struct net_device *soft_iface;
356 struct batadv_bla_claim_dst local_claim_dest;
359 primary_if = batadv_primary_if_get_selected(bat_priv);
363 memcpy(&local_claim_dest, &bat_priv->bla.claim_dest,
364 sizeof(local_claim_dest));
365 local_claim_dest.type = claimtype;
367 soft_iface = primary_if->soft_iface;
369 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
370 /* IP DST: 0.0.0.0 */
372 primary_if->soft_iface,
373 /* IP SRC: 0.0.0.0 */
375 /* Ethernet DST: Broadcast */
377 /* Ethernet SRC/HW SRC: originator mac */
378 primary_if->net_dev->dev_addr,
379 /* HW DST: FF:43:05:XX:YY:YY
380 * with XX = claim type
381 * and YY:YY = group id
383 (u8 *)&local_claim_dest);
388 ethhdr = (struct ethhdr *)skb->data;
389 hw_src = (u8 *)ethhdr + ETH_HLEN + sizeof(struct arphdr);
391 /* now we pretend that the client would have sent this ... */
393 case BATADV_CLAIM_TYPE_CLAIM:
394 /* normal claim frame
395 * set Ethernet SRC to the clients mac
397 ether_addr_copy(ethhdr->h_source, mac);
398 batadv_dbg(BATADV_DBG_BLA, bat_priv,
399 "%s(): CLAIM %pM on vid %d\n", __func__, mac,
400 batadv_print_vid(vid));
402 case BATADV_CLAIM_TYPE_UNCLAIM:
404 * set HW SRC to the clients mac
406 ether_addr_copy(hw_src, mac);
407 batadv_dbg(BATADV_DBG_BLA, bat_priv,
408 "%s(): UNCLAIM %pM on vid %d\n", __func__, mac,
409 batadv_print_vid(vid));
411 case BATADV_CLAIM_TYPE_ANNOUNCE:
412 /* announcement frame
413 * set HW SRC to the special mac containg the crc
415 ether_addr_copy(hw_src, mac);
416 batadv_dbg(BATADV_DBG_BLA, bat_priv,
417 "%s(): ANNOUNCE of %pM on vid %d\n", __func__,
418 ethhdr->h_source, batadv_print_vid(vid));
420 case BATADV_CLAIM_TYPE_REQUEST:
422 * set HW SRC and header destination to the receiving backbone
425 ether_addr_copy(hw_src, mac);
426 ether_addr_copy(ethhdr->h_dest, mac);
427 batadv_dbg(BATADV_DBG_BLA, bat_priv,
428 "%s(): REQUEST of %pM to %pM on vid %d\n", __func__,
429 ethhdr->h_source, ethhdr->h_dest,
430 batadv_print_vid(vid));
432 case BATADV_CLAIM_TYPE_LOOPDETECT:
433 ether_addr_copy(ethhdr->h_source, mac);
434 batadv_dbg(BATADV_DBG_BLA, bat_priv,
435 "%s(): LOOPDETECT of %pM to %pM on vid %d\n",
436 __func__, ethhdr->h_source, ethhdr->h_dest,
437 batadv_print_vid(vid));
442 if (vid & BATADV_VLAN_HAS_TAG) {
443 skb = vlan_insert_tag(skb, htons(ETH_P_8021Q),
444 vid & VLAN_VID_MASK);
449 skb_reset_mac_header(skb);
450 skb->protocol = eth_type_trans(skb, soft_iface);
451 batadv_inc_counter(bat_priv, BATADV_CNT_RX);
452 batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
453 skb->len + ETH_HLEN);
461 batadv_hardif_put(primary_if);
465 * batadv_bla_loopdetect_report() - worker for reporting the loop
466 * @work: work queue item
468 * Throws an uevent, as the loopdetect check function can't do that itself
469 * since the kernel may sleep while throwing uevents.
471 static void batadv_bla_loopdetect_report(struct work_struct *work)
473 struct batadv_bla_backbone_gw *backbone_gw;
474 struct batadv_priv *bat_priv;
475 char vid_str[6] = { '\0' };
477 backbone_gw = container_of(work, struct batadv_bla_backbone_gw,
479 bat_priv = backbone_gw->bat_priv;
481 batadv_info(bat_priv->soft_iface,
482 "Possible loop on VLAN %d detected which can't be handled by BLA - please check your network setup!\n",
483 batadv_print_vid(backbone_gw->vid));
484 snprintf(vid_str, sizeof(vid_str), "%d",
485 batadv_print_vid(backbone_gw->vid));
486 vid_str[sizeof(vid_str) - 1] = 0;
488 batadv_throw_uevent(bat_priv, BATADV_UEV_BLA, BATADV_UEV_LOOPDETECT,
491 batadv_backbone_gw_put(backbone_gw);
495 * batadv_bla_get_backbone_gw() - finds or creates a backbone gateway
496 * @bat_priv: the bat priv with all the soft interface information
497 * @orig: the mac address of the originator
499 * @own_backbone: set if the requested backbone is local
501 * Return: the (possibly created) backbone gateway or NULL on error
503 static struct batadv_bla_backbone_gw *
504 batadv_bla_get_backbone_gw(struct batadv_priv *bat_priv, u8 *orig,
505 unsigned short vid, bool own_backbone)
507 struct batadv_bla_backbone_gw *entry;
508 struct batadv_orig_node *orig_node;
511 entry = batadv_backbone_hash_find(bat_priv, orig, vid);
516 batadv_dbg(BATADV_DBG_BLA, bat_priv,
517 "%s(): not found (%pM, %d), creating new entry\n", __func__,
518 orig, batadv_print_vid(vid));
520 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
525 entry->lasttime = jiffies;
526 entry->crc = BATADV_BLA_CRC_INIT;
527 entry->bat_priv = bat_priv;
528 spin_lock_init(&entry->crc_lock);
529 atomic_set(&entry->request_sent, 0);
530 atomic_set(&entry->wait_periods, 0);
531 ether_addr_copy(entry->orig, orig);
532 INIT_WORK(&entry->report_work, batadv_bla_loopdetect_report);
533 kref_init(&entry->refcount);
535 kref_get(&entry->refcount);
536 hash_added = batadv_hash_add(bat_priv->bla.backbone_hash,
537 batadv_compare_backbone_gw,
538 batadv_choose_backbone_gw, entry,
541 if (unlikely(hash_added != 0)) {
542 /* hash failed, free the structure */
547 /* this is a gateway now, remove any TT entry on this VLAN */
548 orig_node = batadv_orig_hash_find(bat_priv, orig);
550 batadv_tt_global_del_orig(bat_priv, orig_node, vid,
551 "became a backbone gateway");
552 batadv_orig_node_put(orig_node);
556 batadv_bla_send_announce(bat_priv, entry);
558 /* this will be decreased in the worker thread */
559 atomic_inc(&entry->request_sent);
560 atomic_set(&entry->wait_periods, BATADV_BLA_WAIT_PERIODS);
561 atomic_inc(&bat_priv->bla.num_requests);
568 * batadv_bla_update_own_backbone_gw() - updates the own backbone gw for a VLAN
569 * @bat_priv: the bat priv with all the soft interface information
570 * @primary_if: the selected primary interface
571 * @vid: VLAN identifier
573 * update or add the own backbone gw to make sure we announce
574 * where we receive other backbone gws
577 batadv_bla_update_own_backbone_gw(struct batadv_priv *bat_priv,
578 struct batadv_hard_iface *primary_if,
581 struct batadv_bla_backbone_gw *backbone_gw;
583 backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
584 primary_if->net_dev->dev_addr,
586 if (unlikely(!backbone_gw))
589 backbone_gw->lasttime = jiffies;
590 batadv_backbone_gw_put(backbone_gw);
594 * batadv_bla_answer_request() - answer a bla request by sending own claims
595 * @bat_priv: the bat priv with all the soft interface information
596 * @primary_if: interface where the request came on
597 * @vid: the vid where the request came on
599 * Repeat all of our own claims, and finally send an ANNOUNCE frame
600 * to allow the requester another check if the CRC is correct now.
602 static void batadv_bla_answer_request(struct batadv_priv *bat_priv,
603 struct batadv_hard_iface *primary_if,
606 struct hlist_head *head;
607 struct batadv_hashtable *hash;
608 struct batadv_bla_claim *claim;
609 struct batadv_bla_backbone_gw *backbone_gw;
612 batadv_dbg(BATADV_DBG_BLA, bat_priv,
613 "%s(): received a claim request, send all of our own claims again\n",
616 backbone_gw = batadv_backbone_hash_find(bat_priv,
617 primary_if->net_dev->dev_addr,
622 hash = bat_priv->bla.claim_hash;
623 for (i = 0; i < hash->size; i++) {
624 head = &hash->table[i];
627 hlist_for_each_entry_rcu(claim, head, hash_entry) {
628 /* only own claims are interesting */
629 if (claim->backbone_gw != backbone_gw)
632 batadv_bla_send_claim(bat_priv, claim->addr, claim->vid,
633 BATADV_CLAIM_TYPE_CLAIM);
638 /* finally, send an announcement frame */
639 batadv_bla_send_announce(bat_priv, backbone_gw);
640 batadv_backbone_gw_put(backbone_gw);
644 * batadv_bla_send_request() - send a request to repeat claims
645 * @backbone_gw: the backbone gateway from whom we are out of sync
647 * When the crc is wrong, ask the backbone gateway for a full table update.
648 * After the request, it will repeat all of his own claims and finally
649 * send an announcement claim with which we can check again.
651 static void batadv_bla_send_request(struct batadv_bla_backbone_gw *backbone_gw)
653 /* first, remove all old entries */
654 batadv_bla_del_backbone_claims(backbone_gw);
656 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
657 "Sending REQUEST to %pM\n", backbone_gw->orig);
660 batadv_bla_send_claim(backbone_gw->bat_priv, backbone_gw->orig,
661 backbone_gw->vid, BATADV_CLAIM_TYPE_REQUEST);
663 /* no local broadcasts should be sent or received, for now. */
664 if (!atomic_read(&backbone_gw->request_sent)) {
665 atomic_inc(&backbone_gw->bat_priv->bla.num_requests);
666 atomic_set(&backbone_gw->request_sent, 1);
671 * batadv_bla_send_announce() - Send an announcement frame
672 * @bat_priv: the bat priv with all the soft interface information
673 * @backbone_gw: our backbone gateway which should be announced
675 static void batadv_bla_send_announce(struct batadv_priv *bat_priv,
676 struct batadv_bla_backbone_gw *backbone_gw)
681 memcpy(mac, batadv_announce_mac, 4);
682 spin_lock_bh(&backbone_gw->crc_lock);
683 crc = htons(backbone_gw->crc);
684 spin_unlock_bh(&backbone_gw->crc_lock);
685 memcpy(&mac[4], &crc, 2);
687 batadv_bla_send_claim(bat_priv, mac, backbone_gw->vid,
688 BATADV_CLAIM_TYPE_ANNOUNCE);
692 * batadv_bla_add_claim() - Adds a claim in the claim hash
693 * @bat_priv: the bat priv with all the soft interface information
694 * @mac: the mac address of the claim
695 * @vid: the VLAN ID of the frame
696 * @backbone_gw: the backbone gateway which claims it
698 static void batadv_bla_add_claim(struct batadv_priv *bat_priv,
699 const u8 *mac, const unsigned short vid,
700 struct batadv_bla_backbone_gw *backbone_gw)
702 struct batadv_bla_backbone_gw *old_backbone_gw;
703 struct batadv_bla_claim *claim;
704 struct batadv_bla_claim search_claim;
705 bool remove_crc = false;
708 ether_addr_copy(search_claim.addr, mac);
709 search_claim.vid = vid;
710 claim = batadv_claim_hash_find(bat_priv, &search_claim);
712 /* create a new claim entry if it does not exist yet. */
714 claim = kzalloc(sizeof(*claim), GFP_ATOMIC);
718 ether_addr_copy(claim->addr, mac);
719 spin_lock_init(&claim->backbone_lock);
721 claim->lasttime = jiffies;
722 kref_get(&backbone_gw->refcount);
723 claim->backbone_gw = backbone_gw;
724 kref_init(&claim->refcount);
726 batadv_dbg(BATADV_DBG_BLA, bat_priv,
727 "%s(): adding new entry %pM, vid %d to hash ...\n",
728 __func__, mac, batadv_print_vid(vid));
730 kref_get(&claim->refcount);
731 hash_added = batadv_hash_add(bat_priv->bla.claim_hash,
732 batadv_compare_claim,
733 batadv_choose_claim, claim,
736 if (unlikely(hash_added != 0)) {
737 /* only local changes happened. */
742 claim->lasttime = jiffies;
743 if (claim->backbone_gw == backbone_gw)
744 /* no need to register a new backbone */
747 batadv_dbg(BATADV_DBG_BLA, bat_priv,
748 "%s(): changing ownership for %pM, vid %d to gw %pM\n",
749 __func__, mac, batadv_print_vid(vid),
755 /* replace backbone_gw atomically and adjust reference counters */
756 spin_lock_bh(&claim->backbone_lock);
757 old_backbone_gw = claim->backbone_gw;
758 kref_get(&backbone_gw->refcount);
759 claim->backbone_gw = backbone_gw;
760 spin_unlock_bh(&claim->backbone_lock);
763 /* remove claim address from old backbone_gw */
764 spin_lock_bh(&old_backbone_gw->crc_lock);
765 old_backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
766 spin_unlock_bh(&old_backbone_gw->crc_lock);
769 batadv_backbone_gw_put(old_backbone_gw);
771 /* add claim address to new backbone_gw */
772 spin_lock_bh(&backbone_gw->crc_lock);
773 backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
774 spin_unlock_bh(&backbone_gw->crc_lock);
775 backbone_gw->lasttime = jiffies;
778 batadv_claim_put(claim);
782 * batadv_bla_claim_get_backbone_gw() - Get valid reference for backbone_gw of
784 * @claim: claim whose backbone_gw should be returned
786 * Return: valid reference to claim::backbone_gw
788 static struct batadv_bla_backbone_gw *
789 batadv_bla_claim_get_backbone_gw(struct batadv_bla_claim *claim)
791 struct batadv_bla_backbone_gw *backbone_gw;
793 spin_lock_bh(&claim->backbone_lock);
794 backbone_gw = claim->backbone_gw;
795 kref_get(&backbone_gw->refcount);
796 spin_unlock_bh(&claim->backbone_lock);
802 * batadv_bla_del_claim() - delete a claim from the claim hash
803 * @bat_priv: the bat priv with all the soft interface information
804 * @mac: mac address of the claim to be removed
805 * @vid: VLAN id for the claim to be removed
807 static void batadv_bla_del_claim(struct batadv_priv *bat_priv,
808 const u8 *mac, const unsigned short vid)
810 struct batadv_bla_claim search_claim, *claim;
811 struct batadv_bla_claim *claim_removed_entry;
812 struct hlist_node *claim_removed_node;
814 ether_addr_copy(search_claim.addr, mac);
815 search_claim.vid = vid;
816 claim = batadv_claim_hash_find(bat_priv, &search_claim);
820 batadv_dbg(BATADV_DBG_BLA, bat_priv, "%s(): %pM, vid %d\n", __func__,
821 mac, batadv_print_vid(vid));
823 claim_removed_node = batadv_hash_remove(bat_priv->bla.claim_hash,
824 batadv_compare_claim,
825 batadv_choose_claim, claim);
826 if (!claim_removed_node)
829 /* reference from the hash is gone */
830 claim_removed_entry = hlist_entry(claim_removed_node,
831 struct batadv_bla_claim, hash_entry);
832 batadv_claim_put(claim_removed_entry);
835 /* don't need the reference from hash_find() anymore */
836 batadv_claim_put(claim);
840 * batadv_handle_announce() - check for ANNOUNCE frame
841 * @bat_priv: the bat priv with all the soft interface information
842 * @an_addr: announcement mac address (ARP Sender HW address)
843 * @backbone_addr: originator address of the sender (Ethernet source MAC)
844 * @vid: the VLAN ID of the frame
846 * Return: true if handled
848 static bool batadv_handle_announce(struct batadv_priv *bat_priv, u8 *an_addr,
849 u8 *backbone_addr, unsigned short vid)
851 struct batadv_bla_backbone_gw *backbone_gw;
852 u16 backbone_crc, crc;
854 if (memcmp(an_addr, batadv_announce_mac, 4) != 0)
857 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
860 if (unlikely(!backbone_gw))
863 /* handle as ANNOUNCE frame */
864 backbone_gw->lasttime = jiffies;
865 crc = ntohs(*((__be16 *)(&an_addr[4])));
867 batadv_dbg(BATADV_DBG_BLA, bat_priv,
868 "%s(): ANNOUNCE vid %d (sent by %pM)... CRC = %#.4x\n",
869 __func__, batadv_print_vid(vid), backbone_gw->orig, crc);
871 spin_lock_bh(&backbone_gw->crc_lock);
872 backbone_crc = backbone_gw->crc;
873 spin_unlock_bh(&backbone_gw->crc_lock);
875 if (backbone_crc != crc) {
876 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
877 "%s(): CRC FAILED for %pM/%d (my = %#.4x, sent = %#.4x)\n",
878 __func__, backbone_gw->orig,
879 batadv_print_vid(backbone_gw->vid),
882 batadv_bla_send_request(backbone_gw);
884 /* if we have sent a request and the crc was OK,
885 * we can allow traffic again.
887 if (atomic_read(&backbone_gw->request_sent)) {
888 atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
889 atomic_set(&backbone_gw->request_sent, 0);
893 batadv_backbone_gw_put(backbone_gw);
898 * batadv_handle_request() - check for REQUEST frame
899 * @bat_priv: the bat priv with all the soft interface information
900 * @primary_if: the primary hard interface of this batman soft interface
901 * @backbone_addr: backbone address to be requested (ARP sender HW MAC)
902 * @ethhdr: ethernet header of a packet
903 * @vid: the VLAN ID of the frame
905 * Return: true if handled
907 static bool batadv_handle_request(struct batadv_priv *bat_priv,
908 struct batadv_hard_iface *primary_if,
909 u8 *backbone_addr, struct ethhdr *ethhdr,
912 /* check for REQUEST frame */
913 if (!batadv_compare_eth(backbone_addr, ethhdr->h_dest))
916 /* sanity check, this should not happen on a normal switch,
917 * we ignore it in this case.
919 if (!batadv_compare_eth(ethhdr->h_dest, primary_if->net_dev->dev_addr))
922 batadv_dbg(BATADV_DBG_BLA, bat_priv,
923 "%s(): REQUEST vid %d (sent by %pM)...\n",
924 __func__, batadv_print_vid(vid), ethhdr->h_source);
926 batadv_bla_answer_request(bat_priv, primary_if, vid);
931 * batadv_handle_unclaim() - check for UNCLAIM frame
932 * @bat_priv: the bat priv with all the soft interface information
933 * @primary_if: the primary hard interface of this batman soft interface
934 * @backbone_addr: originator address of the backbone (Ethernet source)
935 * @claim_addr: Client to be unclaimed (ARP sender HW MAC)
936 * @vid: the VLAN ID of the frame
938 * Return: true if handled
940 static bool batadv_handle_unclaim(struct batadv_priv *bat_priv,
941 struct batadv_hard_iface *primary_if,
942 u8 *backbone_addr, u8 *claim_addr,
945 struct batadv_bla_backbone_gw *backbone_gw;
947 /* unclaim in any case if it is our own */
948 if (primary_if && batadv_compare_eth(backbone_addr,
949 primary_if->net_dev->dev_addr))
950 batadv_bla_send_claim(bat_priv, claim_addr, vid,
951 BATADV_CLAIM_TYPE_UNCLAIM);
953 backbone_gw = batadv_backbone_hash_find(bat_priv, backbone_addr, vid);
958 /* this must be an UNCLAIM frame */
959 batadv_dbg(BATADV_DBG_BLA, bat_priv,
960 "%s(): UNCLAIM %pM on vid %d (sent by %pM)...\n", __func__,
961 claim_addr, batadv_print_vid(vid), backbone_gw->orig);
963 batadv_bla_del_claim(bat_priv, claim_addr, vid);
964 batadv_backbone_gw_put(backbone_gw);
969 * batadv_handle_claim() - check for CLAIM frame
970 * @bat_priv: the bat priv with all the soft interface information
971 * @primary_if: the primary hard interface of this batman soft interface
972 * @backbone_addr: originator address of the backbone (Ethernet Source)
973 * @claim_addr: client mac address to be claimed (ARP sender HW MAC)
974 * @vid: the VLAN ID of the frame
976 * Return: true if handled
978 static bool batadv_handle_claim(struct batadv_priv *bat_priv,
979 struct batadv_hard_iface *primary_if,
980 u8 *backbone_addr, u8 *claim_addr,
983 struct batadv_bla_backbone_gw *backbone_gw;
985 /* register the gateway if not yet available, and add the claim. */
987 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
990 if (unlikely(!backbone_gw))
993 /* this must be a CLAIM frame */
994 batadv_bla_add_claim(bat_priv, claim_addr, vid, backbone_gw);
995 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
996 batadv_bla_send_claim(bat_priv, claim_addr, vid,
997 BATADV_CLAIM_TYPE_CLAIM);
999 /* TODO: we could call something like tt_local_del() here. */
1001 batadv_backbone_gw_put(backbone_gw);
1006 * batadv_check_claim_group() - check for claim group membership
1007 * @bat_priv: the bat priv with all the soft interface information
1008 * @primary_if: the primary interface of this batman interface
1009 * @hw_src: the Hardware source in the ARP Header
1010 * @hw_dst: the Hardware destination in the ARP Header
1011 * @ethhdr: pointer to the Ethernet header of the claim frame
1013 * checks if it is a claim packet and if its on the same group.
1014 * This function also applies the group ID of the sender
1015 * if it is in the same mesh.
1018 * 2 - if it is a claim packet and on the same group
1019 * 1 - if is a claim packet from another group
1020 * 0 - if it is not a claim packet
1022 static int batadv_check_claim_group(struct batadv_priv *bat_priv,
1023 struct batadv_hard_iface *primary_if,
1024 u8 *hw_src, u8 *hw_dst,
1025 struct ethhdr *ethhdr)
1028 struct batadv_orig_node *orig_node;
1029 struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
1031 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
1032 bla_dst_own = &bat_priv->bla.claim_dest;
1034 /* if announcement packet, use the source,
1035 * otherwise assume it is in the hw_src
1037 switch (bla_dst->type) {
1038 case BATADV_CLAIM_TYPE_CLAIM:
1039 backbone_addr = hw_src;
1041 case BATADV_CLAIM_TYPE_REQUEST:
1042 case BATADV_CLAIM_TYPE_ANNOUNCE:
1043 case BATADV_CLAIM_TYPE_UNCLAIM:
1044 backbone_addr = ethhdr->h_source;
1050 /* don't accept claim frames from ourselves */
1051 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
1054 /* if its already the same group, it is fine. */
1055 if (bla_dst->group == bla_dst_own->group)
1058 /* lets see if this originator is in our mesh */
1059 orig_node = batadv_orig_hash_find(bat_priv, backbone_addr);
1061 /* dont accept claims from gateways which are not in
1062 * the same mesh or group.
1067 /* if our mesh friends mac is bigger, use it for ourselves. */
1068 if (ntohs(bla_dst->group) > ntohs(bla_dst_own->group)) {
1069 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1070 "taking other backbones claim group: %#.4x\n",
1071 ntohs(bla_dst->group));
1072 bla_dst_own->group = bla_dst->group;
1075 batadv_orig_node_put(orig_node);
1081 * batadv_bla_process_claim() - Check if this is a claim frame, and process it
1082 * @bat_priv: the bat priv with all the soft interface information
1083 * @primary_if: the primary hard interface of this batman soft interface
1084 * @skb: the frame to be checked
1086 * Return: true if it was a claim frame, otherwise return false to
1087 * tell the callee that it can use the frame on its own.
1089 static bool batadv_bla_process_claim(struct batadv_priv *bat_priv,
1090 struct batadv_hard_iface *primary_if,
1091 struct sk_buff *skb)
1093 struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
1094 u8 *hw_src, *hw_dst;
1095 struct vlan_hdr *vhdr, vhdr_buf;
1096 struct ethhdr *ethhdr;
1097 struct arphdr *arphdr;
1104 vid = batadv_get_vid(skb, 0);
1105 ethhdr = eth_hdr(skb);
1107 proto = ethhdr->h_proto;
1109 if (vid & BATADV_VLAN_HAS_TAG) {
1110 /* Traverse the VLAN/Ethertypes.
1112 * At this point it is known that the first protocol is a VLAN
1113 * header, so start checking at the encapsulated protocol.
1115 * The depth of the VLAN headers is recorded to drop BLA claim
1116 * frames encapsulated into multiple VLAN headers (QinQ).
1119 vhdr = skb_header_pointer(skb, headlen, VLAN_HLEN,
1124 proto = vhdr->h_vlan_encapsulated_proto;
1125 headlen += VLAN_HLEN;
1127 } while (proto == htons(ETH_P_8021Q));
1130 if (proto != htons(ETH_P_ARP))
1131 return false; /* not a claim frame */
1133 /* this must be a ARP frame. check if it is a claim. */
1135 if (unlikely(!pskb_may_pull(skb, headlen + arp_hdr_len(skb->dev))))
1138 /* pskb_may_pull() may have modified the pointers, get ethhdr again */
1139 ethhdr = eth_hdr(skb);
1140 arphdr = (struct arphdr *)((u8 *)ethhdr + headlen);
1142 /* Check whether the ARP frame carries a valid
1145 if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
1147 if (arphdr->ar_pro != htons(ETH_P_IP))
1149 if (arphdr->ar_hln != ETH_ALEN)
1151 if (arphdr->ar_pln != 4)
1154 hw_src = (u8 *)arphdr + sizeof(struct arphdr);
1155 hw_dst = hw_src + ETH_ALEN + 4;
1156 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
1157 bla_dst_own = &bat_priv->bla.claim_dest;
1159 /* check if it is a claim frame in general */
1160 if (memcmp(bla_dst->magic, bla_dst_own->magic,
1161 sizeof(bla_dst->magic)) != 0)
1164 /* check if there is a claim frame encapsulated deeper in (QinQ) and
1165 * drop that, as this is not supported by BLA but should also not be
1166 * sent via the mesh.
1171 /* Let the loopdetect frames on the mesh in any case. */
1172 if (bla_dst->type == BATADV_CLAIM_TYPE_LOOPDETECT)
1175 /* check if it is a claim frame. */
1176 ret = batadv_check_claim_group(bat_priv, primary_if, hw_src, hw_dst,
1179 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1180 "%s(): received a claim frame from another group. From: %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n",
1181 __func__, ethhdr->h_source, batadv_print_vid(vid),
1187 /* become a backbone gw ourselves on this vlan if not happened yet */
1188 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
1190 /* check for the different types of claim frames ... */
1191 switch (bla_dst->type) {
1192 case BATADV_CLAIM_TYPE_CLAIM:
1193 if (batadv_handle_claim(bat_priv, primary_if, hw_src,
1194 ethhdr->h_source, vid))
1197 case BATADV_CLAIM_TYPE_UNCLAIM:
1198 if (batadv_handle_unclaim(bat_priv, primary_if,
1199 ethhdr->h_source, hw_src, vid))
1203 case BATADV_CLAIM_TYPE_ANNOUNCE:
1204 if (batadv_handle_announce(bat_priv, hw_src, ethhdr->h_source,
1208 case BATADV_CLAIM_TYPE_REQUEST:
1209 if (batadv_handle_request(bat_priv, primary_if, hw_src, ethhdr,
1215 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1216 "%s(): ERROR - this looks like a claim frame, but is useless. eth src %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n",
1217 __func__, ethhdr->h_source, batadv_print_vid(vid), hw_src,
1223 * batadv_bla_purge_backbone_gw() - Remove backbone gateways after a timeout or
1225 * @bat_priv: the bat priv with all the soft interface information
1226 * @now: whether the whole hash shall be wiped now
1228 * Check when we last heard from other nodes, and remove them in case of
1229 * a time out, or clean all backbone gws if now is set.
1231 static void batadv_bla_purge_backbone_gw(struct batadv_priv *bat_priv, int now)
1233 struct batadv_bla_backbone_gw *backbone_gw;
1234 struct hlist_node *node_tmp;
1235 struct hlist_head *head;
1236 struct batadv_hashtable *hash;
1237 spinlock_t *list_lock; /* protects write access to the hash lists */
1240 hash = bat_priv->bla.backbone_hash;
1244 for (i = 0; i < hash->size; i++) {
1245 head = &hash->table[i];
1246 list_lock = &hash->list_locks[i];
1248 spin_lock_bh(list_lock);
1249 hlist_for_each_entry_safe(backbone_gw, node_tmp,
1253 if (!batadv_has_timed_out(backbone_gw->lasttime,
1254 BATADV_BLA_BACKBONE_TIMEOUT))
1257 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
1258 "%s(): backbone gw %pM timed out\n",
1259 __func__, backbone_gw->orig);
1262 /* don't wait for the pending request anymore */
1263 if (atomic_read(&backbone_gw->request_sent))
1264 atomic_dec(&bat_priv->bla.num_requests);
1266 batadv_bla_del_backbone_claims(backbone_gw);
1268 hlist_del_rcu(&backbone_gw->hash_entry);
1269 batadv_backbone_gw_put(backbone_gw);
1271 spin_unlock_bh(list_lock);
1276 * batadv_bla_purge_claims() - Remove claims after a timeout or immediately
1277 * @bat_priv: the bat priv with all the soft interface information
1278 * @primary_if: the selected primary interface, may be NULL if now is set
1279 * @now: whether the whole hash shall be wiped now
1281 * Check when we heard last time from our own claims, and remove them in case of
1282 * a time out, or clean all claims if now is set
1284 static void batadv_bla_purge_claims(struct batadv_priv *bat_priv,
1285 struct batadv_hard_iface *primary_if,
1288 struct batadv_bla_backbone_gw *backbone_gw;
1289 struct batadv_bla_claim *claim;
1290 struct hlist_head *head;
1291 struct batadv_hashtable *hash;
1294 hash = bat_priv->bla.claim_hash;
1298 for (i = 0; i < hash->size; i++) {
1299 head = &hash->table[i];
1302 hlist_for_each_entry_rcu(claim, head, hash_entry) {
1303 backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
1307 if (!batadv_compare_eth(backbone_gw->orig,
1308 primary_if->net_dev->dev_addr))
1311 if (!batadv_has_timed_out(claim->lasttime,
1312 BATADV_BLA_CLAIM_TIMEOUT))
1315 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1316 "%s(): timed out.\n", __func__);
1319 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1320 "%s(): %pM, vid %d\n", __func__,
1321 claim->addr, claim->vid);
1323 batadv_handle_unclaim(bat_priv, primary_if,
1325 claim->addr, claim->vid);
1327 batadv_backbone_gw_put(backbone_gw);
1334 * batadv_bla_update_orig_address() - Update the backbone gateways when the own
1335 * originator address changes
1336 * @bat_priv: the bat priv with all the soft interface information
1337 * @primary_if: the new selected primary_if
1338 * @oldif: the old primary interface, may be NULL
1340 void batadv_bla_update_orig_address(struct batadv_priv *bat_priv,
1341 struct batadv_hard_iface *primary_if,
1342 struct batadv_hard_iface *oldif)
1344 struct batadv_bla_backbone_gw *backbone_gw;
1345 struct hlist_head *head;
1346 struct batadv_hashtable *hash;
1350 /* reset bridge loop avoidance group id */
1351 group = htons(crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN));
1352 bat_priv->bla.claim_dest.group = group;
1354 /* purge everything when bridge loop avoidance is turned off */
1355 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1359 batadv_bla_purge_claims(bat_priv, NULL, 1);
1360 batadv_bla_purge_backbone_gw(bat_priv, 1);
1364 hash = bat_priv->bla.backbone_hash;
1368 for (i = 0; i < hash->size; i++) {
1369 head = &hash->table[i];
1372 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
1373 /* own orig still holds the old value. */
1374 if (!batadv_compare_eth(backbone_gw->orig,
1375 oldif->net_dev->dev_addr))
1378 ether_addr_copy(backbone_gw->orig,
1379 primary_if->net_dev->dev_addr);
1380 /* send an announce frame so others will ask for our
1381 * claims and update their tables.
1383 batadv_bla_send_announce(bat_priv, backbone_gw);
1390 * batadv_bla_send_loopdetect() - send a loopdetect frame
1391 * @bat_priv: the bat priv with all the soft interface information
1392 * @backbone_gw: the backbone gateway for which a loop should be detected
1394 * To detect loops that the bridge loop avoidance can't handle, send a loop
1395 * detection packet on the backbone. Unlike other BLA frames, this frame will
1396 * be allowed on the mesh by other nodes. If it is received on the mesh, this
1397 * indicates that there is a loop.
1400 batadv_bla_send_loopdetect(struct batadv_priv *bat_priv,
1401 struct batadv_bla_backbone_gw *backbone_gw)
1403 batadv_dbg(BATADV_DBG_BLA, bat_priv, "Send loopdetect frame for vid %d\n",
1405 batadv_bla_send_claim(bat_priv, bat_priv->bla.loopdetect_addr,
1406 backbone_gw->vid, BATADV_CLAIM_TYPE_LOOPDETECT);
1410 * batadv_bla_status_update() - purge bla interfaces if necessary
1411 * @net_dev: the soft interface net device
1413 void batadv_bla_status_update(struct net_device *net_dev)
1415 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1416 struct batadv_hard_iface *primary_if;
1418 primary_if = batadv_primary_if_get_selected(bat_priv);
1422 /* this function already purges everything when bla is disabled,
1423 * so just call that one.
1425 batadv_bla_update_orig_address(bat_priv, primary_if, primary_if);
1426 batadv_hardif_put(primary_if);
1430 * batadv_bla_periodic_work() - performs periodic bla work
1431 * @work: kernel work struct
1433 * periodic work to do:
1434 * * purge structures when they are too old
1435 * * send announcements
1437 static void batadv_bla_periodic_work(struct work_struct *work)
1439 struct delayed_work *delayed_work;
1440 struct batadv_priv *bat_priv;
1441 struct batadv_priv_bla *priv_bla;
1442 struct hlist_head *head;
1443 struct batadv_bla_backbone_gw *backbone_gw;
1444 struct batadv_hashtable *hash;
1445 struct batadv_hard_iface *primary_if;
1446 bool send_loopdetect = false;
1449 delayed_work = to_delayed_work(work);
1450 priv_bla = container_of(delayed_work, struct batadv_priv_bla, work);
1451 bat_priv = container_of(priv_bla, struct batadv_priv, bla);
1452 primary_if = batadv_primary_if_get_selected(bat_priv);
1456 batadv_bla_purge_claims(bat_priv, primary_if, 0);
1457 batadv_bla_purge_backbone_gw(bat_priv, 0);
1459 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1462 if (atomic_dec_and_test(&bat_priv->bla.loopdetect_next)) {
1463 /* set a new random mac address for the next bridge loop
1464 * detection frames. Set the locally administered bit to avoid
1465 * collisions with users mac addresses.
1467 eth_random_addr(bat_priv->bla.loopdetect_addr);
1468 bat_priv->bla.loopdetect_addr[0] = 0xba;
1469 bat_priv->bla.loopdetect_addr[1] = 0xbe;
1470 bat_priv->bla.loopdetect_lasttime = jiffies;
1471 atomic_set(&bat_priv->bla.loopdetect_next,
1472 BATADV_BLA_LOOPDETECT_PERIODS);
1474 /* mark for sending loop detect on all VLANs */
1475 send_loopdetect = true;
1478 hash = bat_priv->bla.backbone_hash;
1482 for (i = 0; i < hash->size; i++) {
1483 head = &hash->table[i];
1486 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
1487 if (!batadv_compare_eth(backbone_gw->orig,
1488 primary_if->net_dev->dev_addr))
1491 backbone_gw->lasttime = jiffies;
1493 batadv_bla_send_announce(bat_priv, backbone_gw);
1494 if (send_loopdetect)
1495 batadv_bla_send_loopdetect(bat_priv,
1498 /* request_sent is only set after creation to avoid
1499 * problems when we are not yet known as backbone gw
1502 * We can reset this now after we waited some periods
1503 * to give bridge forward delays and bla group forming
1507 if (atomic_read(&backbone_gw->request_sent) == 0)
1510 if (!atomic_dec_and_test(&backbone_gw->wait_periods))
1513 atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
1514 atomic_set(&backbone_gw->request_sent, 0);
1520 batadv_hardif_put(primary_if);
1522 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1523 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
1526 /* The hash for claim and backbone hash receive the same key because they
1527 * are getting initialized by hash_new with the same key. Reinitializing
1528 * them with to different keys to allow nested locking without generating
1531 static struct lock_class_key batadv_claim_hash_lock_class_key;
1532 static struct lock_class_key batadv_backbone_hash_lock_class_key;
1535 * batadv_bla_init() - initialize all bla structures
1536 * @bat_priv: the bat priv with all the soft interface information
1538 * Return: 0 on success, < 0 on error.
1540 int batadv_bla_init(struct batadv_priv *bat_priv)
1543 u8 claim_dest[ETH_ALEN] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00};
1544 struct batadv_hard_iface *primary_if;
1546 unsigned long entrytime;
1548 spin_lock_init(&bat_priv->bla.bcast_duplist_lock);
1550 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hash registering\n");
1552 /* setting claim destination address */
1553 memcpy(&bat_priv->bla.claim_dest.magic, claim_dest, 3);
1554 bat_priv->bla.claim_dest.type = 0;
1555 primary_if = batadv_primary_if_get_selected(bat_priv);
1557 crc = crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN);
1558 bat_priv->bla.claim_dest.group = htons(crc);
1559 batadv_hardif_put(primary_if);
1561 bat_priv->bla.claim_dest.group = 0; /* will be set later */
1564 /* initialize the duplicate list */
1565 entrytime = jiffies - msecs_to_jiffies(BATADV_DUPLIST_TIMEOUT);
1566 for (i = 0; i < BATADV_DUPLIST_SIZE; i++)
1567 bat_priv->bla.bcast_duplist[i].entrytime = entrytime;
1568 bat_priv->bla.bcast_duplist_curr = 0;
1570 atomic_set(&bat_priv->bla.loopdetect_next,
1571 BATADV_BLA_LOOPDETECT_PERIODS);
1573 if (bat_priv->bla.claim_hash)
1576 bat_priv->bla.claim_hash = batadv_hash_new(128);
1577 bat_priv->bla.backbone_hash = batadv_hash_new(32);
1579 if (!bat_priv->bla.claim_hash || !bat_priv->bla.backbone_hash)
1582 batadv_hash_set_lock_class(bat_priv->bla.claim_hash,
1583 &batadv_claim_hash_lock_class_key);
1584 batadv_hash_set_lock_class(bat_priv->bla.backbone_hash,
1585 &batadv_backbone_hash_lock_class_key);
1587 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hashes initialized\n");
1589 INIT_DELAYED_WORK(&bat_priv->bla.work, batadv_bla_periodic_work);
1591 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1592 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
1597 * batadv_bla_check_duplist() - Check if a frame is in the broadcast dup.
1598 * @bat_priv: the bat priv with all the soft interface information
1599 * @skb: contains the multicast packet to be checked
1600 * @payload_ptr: pointer to position inside the head buffer of the skb
1601 * marking the start of the data to be CRC'ed
1602 * @orig: originator mac address, NULL if unknown
1604 * Check if it is on our broadcast list. Another gateway might have sent the
1605 * same packet because it is connected to the same backbone, so we have to
1606 * remove this duplicate.
1608 * This is performed by checking the CRC, which will tell us
1609 * with a good chance that it is the same packet. If it is furthermore
1610 * sent by another host, drop it. We allow equal packets from
1611 * the same host however as this might be intended.
1613 * Return: true if a packet is in the duplicate list, false otherwise.
1615 static bool batadv_bla_check_duplist(struct batadv_priv *bat_priv,
1616 struct sk_buff *skb, u8 *payload_ptr,
1619 struct batadv_bcast_duplist_entry *entry;
1624 /* calculate the crc ... */
1625 crc = batadv_skb_crc32(skb, payload_ptr);
1627 spin_lock_bh(&bat_priv->bla.bcast_duplist_lock);
1629 for (i = 0; i < BATADV_DUPLIST_SIZE; i++) {
1630 curr = (bat_priv->bla.bcast_duplist_curr + i);
1631 curr %= BATADV_DUPLIST_SIZE;
1632 entry = &bat_priv->bla.bcast_duplist[curr];
1634 /* we can stop searching if the entry is too old ;
1635 * later entries will be even older
1637 if (batadv_has_timed_out(entry->entrytime,
1638 BATADV_DUPLIST_TIMEOUT))
1641 if (entry->crc != crc)
1644 /* are the originators both known and not anonymous? */
1645 if (orig && !is_zero_ether_addr(orig) &&
1646 !is_zero_ether_addr(entry->orig)) {
1647 /* If known, check if the new frame came from
1648 * the same originator:
1649 * We are safe to take identical frames from the
1650 * same orig, if known, as multiplications in
1651 * the mesh are detected via the (orig, seqno) pair.
1652 * So we can be a bit more liberal here and allow
1653 * identical frames from the same orig which the source
1654 * host might have sent multiple times on purpose.
1656 if (batadv_compare_eth(entry->orig, orig))
1660 /* this entry seems to match: same crc, not too old,
1661 * and from another gw. therefore return true to forbid it.
1666 /* not found, add a new entry (overwrite the oldest entry)
1667 * and allow it, its the first occurrence.
1669 curr = (bat_priv->bla.bcast_duplist_curr + BATADV_DUPLIST_SIZE - 1);
1670 curr %= BATADV_DUPLIST_SIZE;
1671 entry = &bat_priv->bla.bcast_duplist[curr];
1673 entry->entrytime = jiffies;
1675 /* known originator */
1677 ether_addr_copy(entry->orig, orig);
1678 /* anonymous originator */
1680 eth_zero_addr(entry->orig);
1682 bat_priv->bla.bcast_duplist_curr = curr;
1685 spin_unlock_bh(&bat_priv->bla.bcast_duplist_lock);
1691 * batadv_bla_check_ucast_duplist() - Check if a frame is in the broadcast dup.
1692 * @bat_priv: the bat priv with all the soft interface information
1693 * @skb: contains the multicast packet to be checked, decapsulated from a
1696 * Check if it is on our broadcast list. Another gateway might have sent the
1697 * same packet because it is connected to the same backbone, so we have to
1698 * remove this duplicate.
1700 * Return: true if a packet is in the duplicate list, false otherwise.
1702 static bool batadv_bla_check_ucast_duplist(struct batadv_priv *bat_priv,
1703 struct sk_buff *skb)
1705 return batadv_bla_check_duplist(bat_priv, skb, (u8 *)skb->data, NULL);
1709 * batadv_bla_check_bcast_duplist() - Check if a frame is in the broadcast dup.
1710 * @bat_priv: the bat priv with all the soft interface information
1711 * @skb: contains the bcast_packet to be checked
1713 * Check if it is on our broadcast list. Another gateway might have sent the
1714 * same packet because it is connected to the same backbone, so we have to
1715 * remove this duplicate.
1717 * Return: true if a packet is in the duplicate list, false otherwise.
1719 bool batadv_bla_check_bcast_duplist(struct batadv_priv *bat_priv,
1720 struct sk_buff *skb)
1722 struct batadv_bcast_packet *bcast_packet;
1725 bcast_packet = (struct batadv_bcast_packet *)skb->data;
1726 payload_ptr = (u8 *)(bcast_packet + 1);
1728 return batadv_bla_check_duplist(bat_priv, skb, payload_ptr,
1729 bcast_packet->orig);
1733 * batadv_bla_is_backbone_gw_orig() - Check if the originator is a gateway for
1734 * the VLAN identified by vid.
1735 * @bat_priv: the bat priv with all the soft interface information
1736 * @orig: originator mac address
1737 * @vid: VLAN identifier
1739 * Return: true if orig is a backbone for this vid, false otherwise.
1741 bool batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, u8 *orig,
1744 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
1745 struct hlist_head *head;
1746 struct batadv_bla_backbone_gw *backbone_gw;
1749 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1755 for (i = 0; i < hash->size; i++) {
1756 head = &hash->table[i];
1759 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
1760 if (batadv_compare_eth(backbone_gw->orig, orig) &&
1761 backbone_gw->vid == vid) {
1773 * batadv_bla_is_backbone_gw() - check if originator is a backbone gw for a VLAN
1774 * @skb: the frame to be checked
1775 * @orig_node: the orig_node of the frame
1776 * @hdr_size: maximum length of the frame
1778 * Return: true if the orig_node is also a gateway on the soft interface,
1779 * otherwise it returns false.
1781 bool batadv_bla_is_backbone_gw(struct sk_buff *skb,
1782 struct batadv_orig_node *orig_node, int hdr_size)
1784 struct batadv_bla_backbone_gw *backbone_gw;
1787 if (!atomic_read(&orig_node->bat_priv->bridge_loop_avoidance))
1790 /* first, find out the vid. */
1791 if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
1794 vid = batadv_get_vid(skb, hdr_size);
1796 /* see if this originator is a backbone gw for this VLAN */
1797 backbone_gw = batadv_backbone_hash_find(orig_node->bat_priv,
1798 orig_node->orig, vid);
1802 batadv_backbone_gw_put(backbone_gw);
1807 * batadv_bla_free() - free all bla structures
1808 * @bat_priv: the bat priv with all the soft interface information
1810 * for softinterface free or module unload
1812 void batadv_bla_free(struct batadv_priv *bat_priv)
1814 struct batadv_hard_iface *primary_if;
1816 cancel_delayed_work_sync(&bat_priv->bla.work);
1817 primary_if = batadv_primary_if_get_selected(bat_priv);
1819 if (bat_priv->bla.claim_hash) {
1820 batadv_bla_purge_claims(bat_priv, primary_if, 1);
1821 batadv_hash_destroy(bat_priv->bla.claim_hash);
1822 bat_priv->bla.claim_hash = NULL;
1824 if (bat_priv->bla.backbone_hash) {
1825 batadv_bla_purge_backbone_gw(bat_priv, 1);
1826 batadv_hash_destroy(bat_priv->bla.backbone_hash);
1827 bat_priv->bla.backbone_hash = NULL;
1830 batadv_hardif_put(primary_if);
1834 * batadv_bla_loopdetect_check() - check and handle a detected loop
1835 * @bat_priv: the bat priv with all the soft interface information
1836 * @skb: the packet to check
1837 * @primary_if: interface where the request came on
1838 * @vid: the VLAN ID of the frame
1840 * Checks if this packet is a loop detect frame which has been sent by us,
1841 * throw an uevent and log the event if that is the case.
1843 * Return: true if it is a loop detect frame which is to be dropped, false
1847 batadv_bla_loopdetect_check(struct batadv_priv *bat_priv, struct sk_buff *skb,
1848 struct batadv_hard_iface *primary_if,
1851 struct batadv_bla_backbone_gw *backbone_gw;
1852 struct ethhdr *ethhdr;
1855 ethhdr = eth_hdr(skb);
1857 /* Only check for the MAC address and skip more checks here for
1858 * performance reasons - this function is on the hotpath, after all.
1860 if (!batadv_compare_eth(ethhdr->h_source,
1861 bat_priv->bla.loopdetect_addr))
1864 /* If the packet came too late, don't forward it on the mesh
1865 * but don't consider that as loop. It might be a coincidence.
1867 if (batadv_has_timed_out(bat_priv->bla.loopdetect_lasttime,
1868 BATADV_BLA_LOOPDETECT_TIMEOUT))
1871 backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
1872 primary_if->net_dev->dev_addr,
1874 if (unlikely(!backbone_gw))
1877 ret = queue_work(batadv_event_workqueue, &backbone_gw->report_work);
1879 /* backbone_gw is unreferenced in the report work function function
1880 * if queue_work() call was successful
1883 batadv_backbone_gw_put(backbone_gw);
1889 * batadv_bla_rx() - check packets coming from the mesh.
1890 * @bat_priv: the bat priv with all the soft interface information
1891 * @skb: the frame to be checked
1892 * @vid: the VLAN ID of the frame
1893 * @packet_type: the batman packet type this frame came in
1895 * batadv_bla_rx avoidance checks if:
1896 * * we have to race for a claim
1897 * * if the frame is allowed on the LAN
1899 * in these cases, the skb is further handled by this function
1901 * Return: true if handled, otherwise it returns false and the caller shall
1902 * further process the skb.
1904 bool batadv_bla_rx(struct batadv_priv *bat_priv, struct sk_buff *skb,
1905 unsigned short vid, int packet_type)
1907 struct batadv_bla_backbone_gw *backbone_gw;
1908 struct ethhdr *ethhdr;
1909 struct batadv_bla_claim search_claim, *claim = NULL;
1910 struct batadv_hard_iface *primary_if;
1914 ethhdr = eth_hdr(skb);
1916 primary_if = batadv_primary_if_get_selected(bat_priv);
1920 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1923 if (batadv_bla_loopdetect_check(bat_priv, skb, primary_if, vid))
1926 if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
1927 /* don't allow multicast packets while requests are in flight */
1928 if (is_multicast_ether_addr(ethhdr->h_dest))
1929 /* Both broadcast flooding or multicast-via-unicasts
1930 * delivery might send to multiple backbone gateways
1931 * sharing the same LAN and therefore need to coordinate
1932 * which backbone gateway forwards into the LAN,
1933 * by claiming the payload source address.
1935 * Broadcast flooding and multicast-via-unicasts
1936 * delivery use the following two batman packet types.
1937 * Note: explicitly exclude BATADV_UNICAST_4ADDR,
1938 * as the DHCP gateway feature will send explicitly
1939 * to only one BLA gateway, so the claiming process
1940 * should be avoided there.
1942 if (packet_type == BATADV_BCAST ||
1943 packet_type == BATADV_UNICAST)
1946 /* potential duplicates from foreign BLA backbone gateways via
1947 * multicast-in-unicast packets
1949 if (is_multicast_ether_addr(ethhdr->h_dest) &&
1950 packet_type == BATADV_UNICAST &&
1951 batadv_bla_check_ucast_duplist(bat_priv, skb))
1954 ether_addr_copy(search_claim.addr, ethhdr->h_source);
1955 search_claim.vid = vid;
1956 claim = batadv_claim_hash_find(bat_priv, &search_claim);
1959 /* possible optimization: race for a claim */
1960 /* No claim exists yet, claim it for us!
1963 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1964 "%s(): Unclaimed MAC %pM found. Claim it. Local: %s\n",
1965 __func__, ethhdr->h_source,
1966 batadv_is_my_client(bat_priv,
1967 ethhdr->h_source, vid) ?
1969 batadv_handle_claim(bat_priv, primary_if,
1970 primary_if->net_dev->dev_addr,
1971 ethhdr->h_source, vid);
1975 /* if it is our own claim ... */
1976 backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
1977 own_claim = batadv_compare_eth(backbone_gw->orig,
1978 primary_if->net_dev->dev_addr);
1979 batadv_backbone_gw_put(backbone_gw);
1982 /* ... allow it in any case */
1983 claim->lasttime = jiffies;
1987 /* if it is a multicast ... */
1988 if (is_multicast_ether_addr(ethhdr->h_dest) &&
1989 (packet_type == BATADV_BCAST || packet_type == BATADV_UNICAST)) {
1990 /* ... drop it. the responsible gateway is in charge.
1992 * We need to check packet type because with the gateway
1993 * feature, broadcasts (like DHCP requests) may be sent
1994 * using a unicast 4 address packet type. See comment above.
1998 /* seems the client considers us as its best gateway.
1999 * send a claim and update the claim table
2002 batadv_handle_claim(bat_priv, primary_if,
2003 primary_if->net_dev->dev_addr,
2004 ethhdr->h_source, vid);
2008 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
2018 batadv_hardif_put(primary_if);
2020 batadv_claim_put(claim);
2025 * batadv_bla_tx() - check packets going into the mesh
2026 * @bat_priv: the bat priv with all the soft interface information
2027 * @skb: the frame to be checked
2028 * @vid: the VLAN ID of the frame
2030 * batadv_bla_tx checks if:
2031 * * a claim was received which has to be processed
2032 * * the frame is allowed on the mesh
2034 * in these cases, the skb is further handled by this function.
2036 * This call might reallocate skb data.
2038 * Return: true if handled, otherwise it returns false and the caller shall
2039 * further process the skb.
2041 bool batadv_bla_tx(struct batadv_priv *bat_priv, struct sk_buff *skb,
2044 struct ethhdr *ethhdr;
2045 struct batadv_bla_claim search_claim, *claim = NULL;
2046 struct batadv_bla_backbone_gw *backbone_gw;
2047 struct batadv_hard_iface *primary_if;
2051 primary_if = batadv_primary_if_get_selected(bat_priv);
2055 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
2058 if (batadv_bla_process_claim(bat_priv, primary_if, skb))
2061 ethhdr = eth_hdr(skb);
2063 if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
2064 /* don't allow broadcasts while requests are in flight */
2065 if (is_multicast_ether_addr(ethhdr->h_dest))
2068 ether_addr_copy(search_claim.addr, ethhdr->h_source);
2069 search_claim.vid = vid;
2071 claim = batadv_claim_hash_find(bat_priv, &search_claim);
2073 /* if no claim exists, allow it. */
2077 /* check if we are responsible. */
2078 backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
2079 client_roamed = batadv_compare_eth(backbone_gw->orig,
2080 primary_if->net_dev->dev_addr);
2081 batadv_backbone_gw_put(backbone_gw);
2083 if (client_roamed) {
2084 /* if yes, the client has roamed and we have
2087 if (batadv_has_timed_out(claim->lasttime, 100)) {
2088 /* only unclaim if the last claim entry is
2089 * older than 100 ms to make sure we really
2090 * have a roaming client here.
2092 batadv_dbg(BATADV_DBG_BLA, bat_priv, "%s(): Roaming client %pM detected. Unclaim it.\n",
2093 __func__, ethhdr->h_source);
2094 batadv_handle_unclaim(bat_priv, primary_if,
2095 primary_if->net_dev->dev_addr,
2096 ethhdr->h_source, vid);
2099 batadv_dbg(BATADV_DBG_BLA, bat_priv, "%s(): Race for claim %pM detected. Drop packet.\n",
2100 __func__, ethhdr->h_source);
2105 /* check if it is a multicast/broadcast frame */
2106 if (is_multicast_ether_addr(ethhdr->h_dest)) {
2107 /* drop it. the responsible gateway has forwarded it into
2108 * the backbone network.
2112 /* we must allow it. at least if we are
2113 * responsible for the DESTINATION.
2118 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
2125 batadv_hardif_put(primary_if);
2127 batadv_claim_put(claim);
2131 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
2133 * batadv_bla_claim_table_seq_print_text() - print the claim table in a seq file
2134 * @seq: seq file to print on
2139 int batadv_bla_claim_table_seq_print_text(struct seq_file *seq, void *offset)
2141 struct net_device *net_dev = (struct net_device *)seq->private;
2142 struct batadv_priv *bat_priv = netdev_priv(net_dev);
2143 struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
2144 struct batadv_bla_backbone_gw *backbone_gw;
2145 struct batadv_bla_claim *claim;
2146 struct batadv_hard_iface *primary_if;
2147 struct hlist_head *head;
2153 primary_if = batadv_seq_print_text_primary_if_get(seq);
2157 primary_addr = primary_if->net_dev->dev_addr;
2159 "Claims announced for the mesh %s (orig %pM, group id %#.4x)\n",
2160 net_dev->name, primary_addr,
2161 ntohs(bat_priv->bla.claim_dest.group));
2163 " Client VID Originator [o] (CRC )\n");
2164 for (i = 0; i < hash->size; i++) {
2165 head = &hash->table[i];
2168 hlist_for_each_entry_rcu(claim, head, hash_entry) {
2169 backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
2171 is_own = batadv_compare_eth(backbone_gw->orig,
2174 spin_lock_bh(&backbone_gw->crc_lock);
2175 backbone_crc = backbone_gw->crc;
2176 spin_unlock_bh(&backbone_gw->crc_lock);
2177 seq_printf(seq, " * %pM on %5d by %pM [%c] (%#.4x)\n",
2178 claim->addr, batadv_print_vid(claim->vid),
2180 (is_own ? 'x' : ' '),
2183 batadv_backbone_gw_put(backbone_gw);
2189 batadv_hardif_put(primary_if);
2195 * batadv_bla_claim_dump_entry() - dump one entry of the claim table
2196 * to a netlink socket
2197 * @msg: buffer for the message
2198 * @portid: netlink port
2199 * @seq: Sequence number of netlink message
2200 * @primary_if: primary interface
2201 * @claim: entry to dump
2203 * Return: 0 or error code.
2206 batadv_bla_claim_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
2207 struct batadv_hard_iface *primary_if,
2208 struct batadv_bla_claim *claim)
2210 u8 *primary_addr = primary_if->net_dev->dev_addr;
2216 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
2217 NLM_F_MULTI, BATADV_CMD_GET_BLA_CLAIM);
2223 is_own = batadv_compare_eth(claim->backbone_gw->orig,
2226 spin_lock_bh(&claim->backbone_gw->crc_lock);
2227 backbone_crc = claim->backbone_gw->crc;
2228 spin_unlock_bh(&claim->backbone_gw->crc_lock);
2231 if (nla_put_flag(msg, BATADV_ATTR_BLA_OWN)) {
2232 genlmsg_cancel(msg, hdr);
2236 if (nla_put(msg, BATADV_ATTR_BLA_ADDRESS, ETH_ALEN, claim->addr) ||
2237 nla_put_u16(msg, BATADV_ATTR_BLA_VID, claim->vid) ||
2238 nla_put(msg, BATADV_ATTR_BLA_BACKBONE, ETH_ALEN,
2239 claim->backbone_gw->orig) ||
2240 nla_put_u16(msg, BATADV_ATTR_BLA_CRC,
2242 genlmsg_cancel(msg, hdr);
2246 genlmsg_end(msg, hdr);
2254 * batadv_bla_claim_dump_bucket() - dump one bucket of the claim table
2255 * to a netlink socket
2256 * @msg: buffer for the message
2257 * @portid: netlink port
2258 * @seq: Sequence number of netlink message
2259 * @primary_if: primary interface
2260 * @head: bucket to dump
2261 * @idx_skip: How many entries to skip
2266 batadv_bla_claim_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
2267 struct batadv_hard_iface *primary_if,
2268 struct hlist_head *head, int *idx_skip)
2270 struct batadv_bla_claim *claim;
2275 hlist_for_each_entry_rcu(claim, head, hash_entry) {
2276 if (idx++ < *idx_skip)
2279 ret = batadv_bla_claim_dump_entry(msg, portid, seq,
2282 *idx_skip = idx - 1;
2294 * batadv_bla_claim_dump() - dump claim table to a netlink socket
2295 * @msg: buffer for the message
2296 * @cb: callback structure containing arguments
2298 * Return: message length.
2300 int batadv_bla_claim_dump(struct sk_buff *msg, struct netlink_callback *cb)
2302 struct batadv_hard_iface *primary_if = NULL;
2303 int portid = NETLINK_CB(cb->skb).portid;
2304 struct net *net = sock_net(cb->skb->sk);
2305 struct net_device *soft_iface;
2306 struct batadv_hashtable *hash;
2307 struct batadv_priv *bat_priv;
2308 int bucket = cb->args[0];
2309 struct hlist_head *head;
2310 int idx = cb->args[1];
2314 ifindex = batadv_netlink_get_ifindex(cb->nlh,
2315 BATADV_ATTR_MESH_IFINDEX);
2319 soft_iface = dev_get_by_index(net, ifindex);
2320 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
2325 bat_priv = netdev_priv(soft_iface);
2326 hash = bat_priv->bla.claim_hash;
2328 primary_if = batadv_primary_if_get_selected(bat_priv);
2329 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
2334 while (bucket < hash->size) {
2335 head = &hash->table[bucket];
2337 if (batadv_bla_claim_dump_bucket(msg, portid,
2339 primary_if, head, &idx))
2344 cb->args[0] = bucket;
2351 batadv_hardif_put(primary_if);
2354 dev_put(soft_iface);
2359 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
2361 * batadv_bla_backbone_table_seq_print_text() - print the backbone table in a
2363 * @seq: seq file to print on
2368 int batadv_bla_backbone_table_seq_print_text(struct seq_file *seq, void *offset)
2370 struct net_device *net_dev = (struct net_device *)seq->private;
2371 struct batadv_priv *bat_priv = netdev_priv(net_dev);
2372 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
2373 struct batadv_bla_backbone_gw *backbone_gw;
2374 struct batadv_hard_iface *primary_if;
2375 struct hlist_head *head;
2382 primary_if = batadv_seq_print_text_primary_if_get(seq);
2386 primary_addr = primary_if->net_dev->dev_addr;
2388 "Backbones announced for the mesh %s (orig %pM, group id %#.4x)\n",
2389 net_dev->name, primary_addr,
2390 ntohs(bat_priv->bla.claim_dest.group));
2391 seq_puts(seq, " Originator VID last seen (CRC )\n");
2392 for (i = 0; i < hash->size; i++) {
2393 head = &hash->table[i];
2396 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
2397 msecs = jiffies_to_msecs(jiffies -
2398 backbone_gw->lasttime);
2399 secs = msecs / 1000;
2400 msecs = msecs % 1000;
2402 is_own = batadv_compare_eth(backbone_gw->orig,
2407 spin_lock_bh(&backbone_gw->crc_lock);
2408 backbone_crc = backbone_gw->crc;
2409 spin_unlock_bh(&backbone_gw->crc_lock);
2411 seq_printf(seq, " * %pM on %5d %4i.%03is (%#.4x)\n",
2413 batadv_print_vid(backbone_gw->vid), secs,
2414 msecs, backbone_crc);
2420 batadv_hardif_put(primary_if);
2426 * batadv_bla_backbone_dump_entry() - dump one entry of the backbone table to a
2428 * @msg: buffer for the message
2429 * @portid: netlink port
2430 * @seq: Sequence number of netlink message
2431 * @primary_if: primary interface
2432 * @backbone_gw: entry to dump
2434 * Return: 0 or error code.
2437 batadv_bla_backbone_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
2438 struct batadv_hard_iface *primary_if,
2439 struct batadv_bla_backbone_gw *backbone_gw)
2441 u8 *primary_addr = primary_if->net_dev->dev_addr;
2448 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
2449 NLM_F_MULTI, BATADV_CMD_GET_BLA_BACKBONE);
2455 is_own = batadv_compare_eth(backbone_gw->orig, primary_addr);
2457 spin_lock_bh(&backbone_gw->crc_lock);
2458 backbone_crc = backbone_gw->crc;
2459 spin_unlock_bh(&backbone_gw->crc_lock);
2461 msecs = jiffies_to_msecs(jiffies - backbone_gw->lasttime);
2464 if (nla_put_flag(msg, BATADV_ATTR_BLA_OWN)) {
2465 genlmsg_cancel(msg, hdr);
2469 if (nla_put(msg, BATADV_ATTR_BLA_BACKBONE, ETH_ALEN,
2470 backbone_gw->orig) ||
2471 nla_put_u16(msg, BATADV_ATTR_BLA_VID, backbone_gw->vid) ||
2472 nla_put_u16(msg, BATADV_ATTR_BLA_CRC,
2474 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS, msecs)) {
2475 genlmsg_cancel(msg, hdr);
2479 genlmsg_end(msg, hdr);
2487 * batadv_bla_backbone_dump_bucket() - dump one bucket of the backbone table to
2489 * @msg: buffer for the message
2490 * @portid: netlink port
2491 * @seq: Sequence number of netlink message
2492 * @primary_if: primary interface
2493 * @head: bucket to dump
2494 * @idx_skip: How many entries to skip
2499 batadv_bla_backbone_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
2500 struct batadv_hard_iface *primary_if,
2501 struct hlist_head *head, int *idx_skip)
2503 struct batadv_bla_backbone_gw *backbone_gw;
2508 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
2509 if (idx++ < *idx_skip)
2512 ret = batadv_bla_backbone_dump_entry(msg, portid, seq,
2513 primary_if, backbone_gw);
2515 *idx_skip = idx - 1;
2527 * batadv_bla_backbone_dump() - dump backbone table to a netlink socket
2528 * @msg: buffer for the message
2529 * @cb: callback structure containing arguments
2531 * Return: message length.
2533 int batadv_bla_backbone_dump(struct sk_buff *msg, struct netlink_callback *cb)
2535 struct batadv_hard_iface *primary_if = NULL;
2536 int portid = NETLINK_CB(cb->skb).portid;
2537 struct net *net = sock_net(cb->skb->sk);
2538 struct net_device *soft_iface;
2539 struct batadv_hashtable *hash;
2540 struct batadv_priv *bat_priv;
2541 int bucket = cb->args[0];
2542 struct hlist_head *head;
2543 int idx = cb->args[1];
2547 ifindex = batadv_netlink_get_ifindex(cb->nlh,
2548 BATADV_ATTR_MESH_IFINDEX);
2552 soft_iface = dev_get_by_index(net, ifindex);
2553 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
2558 bat_priv = netdev_priv(soft_iface);
2559 hash = bat_priv->bla.backbone_hash;
2561 primary_if = batadv_primary_if_get_selected(bat_priv);
2562 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
2567 while (bucket < hash->size) {
2568 head = &hash->table[bucket];
2570 if (batadv_bla_backbone_dump_bucket(msg, portid,
2572 primary_if, head, &idx))
2577 cb->args[0] = bucket;
2584 batadv_hardif_put(primary_if);
2587 dev_put(soft_iface);
2592 #ifdef CONFIG_BATMAN_ADV_DAT
2594 * batadv_bla_check_claim() - check if address is claimed
2596 * @bat_priv: the bat priv with all the soft interface information
2597 * @addr: mac address of which the claim status is checked
2600 * addr is checked if this address is claimed by the local device itself.
2602 * Return: true if bla is disabled or the mac is claimed by the device,
2603 * false if the device addr is already claimed by another gateway
2605 bool batadv_bla_check_claim(struct batadv_priv *bat_priv,
2606 u8 *addr, unsigned short vid)
2608 struct batadv_bla_claim search_claim;
2609 struct batadv_bla_claim *claim = NULL;
2610 struct batadv_hard_iface *primary_if = NULL;
2613 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
2616 primary_if = batadv_primary_if_get_selected(bat_priv);
2620 /* First look if the mac address is claimed */
2621 ether_addr_copy(search_claim.addr, addr);
2622 search_claim.vid = vid;
2624 claim = batadv_claim_hash_find(bat_priv, &search_claim);
2626 /* If there is a claim and we are not owner of the claim,
2630 if (!batadv_compare_eth(claim->backbone_gw->orig,
2631 primary_if->net_dev->dev_addr))
2633 batadv_claim_put(claim);
2636 batadv_hardif_put(primary_if);