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/netlink.h>
39 #include <linux/preempt.h>
40 #include <linux/rculist.h>
41 #include <linux/rcupdate.h>
42 #include <linux/seq_file.h>
43 #include <linux/skbuff.h>
44 #include <linux/slab.h>
45 #include <linux/spinlock.h>
46 #include <linux/stddef.h>
47 #include <linux/string.h>
48 #include <linux/workqueue.h>
50 #include <net/genetlink.h>
51 #include <net/netlink.h>
53 #include <uapi/linux/batman_adv.h>
55 #include "hard-interface.h"
59 #include "originator.h"
61 #include "soft-interface.h"
63 #include "translation-table.h"
65 static const u8 batadv_announce_mac[4] = {0x43, 0x05, 0x43, 0x05};
67 static void batadv_bla_periodic_work(struct work_struct *work);
69 batadv_bla_send_announce(struct batadv_priv *bat_priv,
70 struct batadv_bla_backbone_gw *backbone_gw);
73 * batadv_choose_claim - choose the right bucket for a claim.
75 * @size: size of the hash table
77 * Return: the hash index of the claim
79 static inline u32 batadv_choose_claim(const void *data, u32 size)
81 struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
84 hash = jhash(&claim->addr, sizeof(claim->addr), hash);
85 hash = jhash(&claim->vid, sizeof(claim->vid), hash);
91 * batadv_choose_backbone_gw - choose the right bucket for a backbone gateway.
93 * @size: size of the hash table
95 * Return: the hash index of the backbone gateway
97 static inline u32 batadv_choose_backbone_gw(const void *data, u32 size)
99 const struct batadv_bla_backbone_gw *gw;
102 gw = (struct batadv_bla_backbone_gw *)data;
103 hash = jhash(&gw->orig, sizeof(gw->orig), hash);
104 hash = jhash(&gw->vid, sizeof(gw->vid), hash);
110 * batadv_compare_backbone_gw - compare address and vid of two backbone gws
111 * @node: list node of the first entry to compare
112 * @data2: pointer to the second backbone gateway
114 * Return: true if the backbones have the same data, false otherwise
116 static bool batadv_compare_backbone_gw(const struct hlist_node *node,
119 const void *data1 = container_of(node, struct batadv_bla_backbone_gw,
121 const struct batadv_bla_backbone_gw *gw1 = data1;
122 const struct batadv_bla_backbone_gw *gw2 = data2;
124 if (!batadv_compare_eth(gw1->orig, gw2->orig))
127 if (gw1->vid != gw2->vid)
134 * batadv_compare_claim - compare address and vid of two claims
135 * @node: list node of the first entry to compare
136 * @data2: pointer to the second claims
138 * Return: true if the claim have the same data, 0 otherwise
140 static bool batadv_compare_claim(const struct hlist_node *node,
143 const void *data1 = container_of(node, struct batadv_bla_claim,
145 const struct batadv_bla_claim *cl1 = data1;
146 const struct batadv_bla_claim *cl2 = data2;
148 if (!batadv_compare_eth(cl1->addr, cl2->addr))
151 if (cl1->vid != cl2->vid)
158 * batadv_backbone_gw_release - release backbone gw from lists and queue for
159 * free after rcu grace period
160 * @ref: kref pointer of the backbone gw
162 static void batadv_backbone_gw_release(struct kref *ref)
164 struct batadv_bla_backbone_gw *backbone_gw;
166 backbone_gw = container_of(ref, struct batadv_bla_backbone_gw,
169 kfree_rcu(backbone_gw, rcu);
173 * batadv_backbone_gw_put - decrement the backbone gw refcounter and possibly
175 * @backbone_gw: backbone gateway to be free'd
177 static void batadv_backbone_gw_put(struct batadv_bla_backbone_gw *backbone_gw)
179 kref_put(&backbone_gw->refcount, batadv_backbone_gw_release);
183 * batadv_claim_release - release claim from lists and queue for free after rcu
185 * @ref: kref pointer of the claim
187 static void batadv_claim_release(struct kref *ref)
189 struct batadv_bla_claim *claim;
190 struct batadv_bla_backbone_gw *old_backbone_gw;
192 claim = container_of(ref, struct batadv_bla_claim, refcount);
194 spin_lock_bh(&claim->backbone_lock);
195 old_backbone_gw = claim->backbone_gw;
196 claim->backbone_gw = NULL;
197 spin_unlock_bh(&claim->backbone_lock);
199 spin_lock_bh(&old_backbone_gw->crc_lock);
200 old_backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
201 spin_unlock_bh(&old_backbone_gw->crc_lock);
203 batadv_backbone_gw_put(old_backbone_gw);
205 kfree_rcu(claim, rcu);
209 * batadv_claim_put - decrement the claim refcounter and possibly
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 "bla_send_claim(): CLAIM %pM on vid %d\n", 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 "bla_send_claim(): UNCLAIM %pM on vid %d\n", 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 "bla_send_claim(): ANNOUNCE of %pM on vid %d\n",
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 "bla_send_claim(): REQUEST of %pM to %pM on vid %d\n",
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 "bla_send_claim(): LOOPDETECT of %pM to %pM on vid %d\n",
436 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);
454 soft_iface->last_rx = jiffies;
462 batadv_hardif_put(primary_if);
466 * batadv_bla_loopdetect_report - worker for reporting the loop
467 * @work: work queue item
469 * Throws an uevent, as the loopdetect check function can't do that itself
470 * since the kernel may sleep while throwing uevents.
472 static void batadv_bla_loopdetect_report(struct work_struct *work)
474 struct batadv_bla_backbone_gw *backbone_gw;
475 struct batadv_priv *bat_priv;
476 char vid_str[6] = { '\0' };
478 backbone_gw = container_of(work, struct batadv_bla_backbone_gw,
480 bat_priv = backbone_gw->bat_priv;
482 batadv_info(bat_priv->soft_iface,
483 "Possible loop on VLAN %d detected which can't be handled by BLA - please check your network setup!\n",
484 BATADV_PRINT_VID(backbone_gw->vid));
485 snprintf(vid_str, sizeof(vid_str), "%d",
486 BATADV_PRINT_VID(backbone_gw->vid));
487 vid_str[sizeof(vid_str) - 1] = 0;
489 batadv_throw_uevent(bat_priv, BATADV_UEV_BLA, BATADV_UEV_LOOPDETECT,
492 batadv_backbone_gw_put(backbone_gw);
496 * batadv_bla_get_backbone_gw - finds or creates a backbone gateway
497 * @bat_priv: the bat priv with all the soft interface information
498 * @orig: the mac address of the originator
500 * @own_backbone: set if the requested backbone is local
502 * Return: the (possibly created) backbone gateway or NULL on error
504 static struct batadv_bla_backbone_gw *
505 batadv_bla_get_backbone_gw(struct batadv_priv *bat_priv, u8 *orig,
506 unsigned short vid, bool own_backbone)
508 struct batadv_bla_backbone_gw *entry;
509 struct batadv_orig_node *orig_node;
512 entry = batadv_backbone_hash_find(bat_priv, orig, vid);
517 batadv_dbg(BATADV_DBG_BLA, bat_priv,
518 "bla_get_backbone_gw(): not found (%pM, %d), creating new entry\n",
519 orig, BATADV_PRINT_VID(vid));
521 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
526 entry->lasttime = jiffies;
527 entry->crc = BATADV_BLA_CRC_INIT;
528 entry->bat_priv = bat_priv;
529 spin_lock_init(&entry->crc_lock);
530 atomic_set(&entry->request_sent, 0);
531 atomic_set(&entry->wait_periods, 0);
532 ether_addr_copy(entry->orig, orig);
533 INIT_WORK(&entry->report_work, batadv_bla_loopdetect_report);
534 kref_init(&entry->refcount);
536 kref_get(&entry->refcount);
537 hash_added = batadv_hash_add(bat_priv->bla.backbone_hash,
538 batadv_compare_backbone_gw,
539 batadv_choose_backbone_gw, entry,
542 if (unlikely(hash_added != 0)) {
543 /* hash failed, free the structure */
548 /* this is a gateway now, remove any TT entry on this VLAN */
549 orig_node = batadv_orig_hash_find(bat_priv, orig);
551 batadv_tt_global_del_orig(bat_priv, orig_node, vid,
552 "became a backbone gateway");
553 batadv_orig_node_put(orig_node);
557 batadv_bla_send_announce(bat_priv, entry);
559 /* this will be decreased in the worker thread */
560 atomic_inc(&entry->request_sent);
561 atomic_set(&entry->wait_periods, BATADV_BLA_WAIT_PERIODS);
562 atomic_inc(&bat_priv->bla.num_requests);
569 * batadv_bla_update_own_backbone_gw - updates the own backbone gw for a VLAN
570 * @bat_priv: the bat priv with all the soft interface information
571 * @primary_if: the selected primary interface
572 * @vid: VLAN identifier
574 * update or add the own backbone gw to make sure we announce
575 * where we receive other backbone gws
578 batadv_bla_update_own_backbone_gw(struct batadv_priv *bat_priv,
579 struct batadv_hard_iface *primary_if,
582 struct batadv_bla_backbone_gw *backbone_gw;
584 backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
585 primary_if->net_dev->dev_addr,
587 if (unlikely(!backbone_gw))
590 backbone_gw->lasttime = jiffies;
591 batadv_backbone_gw_put(backbone_gw);
595 * batadv_bla_answer_request - answer a bla request by sending own claims
596 * @bat_priv: the bat priv with all the soft interface information
597 * @primary_if: interface where the request came on
598 * @vid: the vid where the request came on
600 * Repeat all of our own claims, and finally send an ANNOUNCE frame
601 * to allow the requester another check if the CRC is correct now.
603 static void batadv_bla_answer_request(struct batadv_priv *bat_priv,
604 struct batadv_hard_iface *primary_if,
607 struct hlist_head *head;
608 struct batadv_hashtable *hash;
609 struct batadv_bla_claim *claim;
610 struct batadv_bla_backbone_gw *backbone_gw;
613 batadv_dbg(BATADV_DBG_BLA, bat_priv,
614 "bla_answer_request(): 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 "bla_add_claim(): adding new entry %pM, vid %d to hash ...\n",
728 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 "bla_add_claim(): changing ownership for %pM, vid %d\n",
749 mac, BATADV_PRINT_VID(vid));
754 /* replace backbone_gw atomically and adjust reference counters */
755 spin_lock_bh(&claim->backbone_lock);
756 old_backbone_gw = claim->backbone_gw;
757 kref_get(&backbone_gw->refcount);
758 claim->backbone_gw = backbone_gw;
759 spin_unlock_bh(&claim->backbone_lock);
762 /* remove claim address from old backbone_gw */
763 spin_lock_bh(&old_backbone_gw->crc_lock);
764 old_backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
765 spin_unlock_bh(&old_backbone_gw->crc_lock);
768 batadv_backbone_gw_put(old_backbone_gw);
770 /* add claim address to new backbone_gw */
771 spin_lock_bh(&backbone_gw->crc_lock);
772 backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
773 spin_unlock_bh(&backbone_gw->crc_lock);
774 backbone_gw->lasttime = jiffies;
777 batadv_claim_put(claim);
781 * batadv_bla_claim_get_backbone_gw - Get valid reference for backbone_gw of
783 * @claim: claim whose backbone_gw should be returned
785 * Return: valid reference to claim::backbone_gw
787 static struct batadv_bla_backbone_gw *
788 batadv_bla_claim_get_backbone_gw(struct batadv_bla_claim *claim)
790 struct batadv_bla_backbone_gw *backbone_gw;
792 spin_lock_bh(&claim->backbone_lock);
793 backbone_gw = claim->backbone_gw;
794 kref_get(&backbone_gw->refcount);
795 spin_unlock_bh(&claim->backbone_lock);
801 * batadv_bla_del_claim - delete a claim from the claim hash
802 * @bat_priv: the bat priv with all the soft interface information
803 * @mac: mac address of the claim to be removed
804 * @vid: VLAN id for the claim to be removed
806 static void batadv_bla_del_claim(struct batadv_priv *bat_priv,
807 const u8 *mac, const unsigned short vid)
809 struct batadv_bla_claim search_claim, *claim;
810 struct batadv_bla_claim *claim_removed_entry;
811 struct hlist_node *claim_removed_node;
813 ether_addr_copy(search_claim.addr, mac);
814 search_claim.vid = vid;
815 claim = batadv_claim_hash_find(bat_priv, &search_claim);
819 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla_del_claim(): %pM, vid %d\n",
820 mac, BATADV_PRINT_VID(vid));
822 claim_removed_node = batadv_hash_remove(bat_priv->bla.claim_hash,
823 batadv_compare_claim,
824 batadv_choose_claim, claim);
825 if (!claim_removed_node)
828 /* reference from the hash is gone */
829 claim_removed_entry = hlist_entry(claim_removed_node,
830 struct batadv_bla_claim, hash_entry);
831 batadv_claim_put(claim_removed_entry);
834 /* don't need the reference from hash_find() anymore */
835 batadv_claim_put(claim);
839 * batadv_handle_announce - check for ANNOUNCE frame
840 * @bat_priv: the bat priv with all the soft interface information
841 * @an_addr: announcement mac address (ARP Sender HW address)
842 * @backbone_addr: originator address of the sender (Ethernet source MAC)
843 * @vid: the VLAN ID of the frame
845 * Return: true if handled
847 static bool batadv_handle_announce(struct batadv_priv *bat_priv, u8 *an_addr,
848 u8 *backbone_addr, unsigned short vid)
850 struct batadv_bla_backbone_gw *backbone_gw;
851 u16 backbone_crc, crc;
853 if (memcmp(an_addr, batadv_announce_mac, 4) != 0)
856 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
859 if (unlikely(!backbone_gw))
862 /* handle as ANNOUNCE frame */
863 backbone_gw->lasttime = jiffies;
864 crc = ntohs(*((__be16 *)(&an_addr[4])));
866 batadv_dbg(BATADV_DBG_BLA, bat_priv,
867 "handle_announce(): ANNOUNCE vid %d (sent by %pM)... CRC = %#.4x\n",
868 BATADV_PRINT_VID(vid), backbone_gw->orig, crc);
870 spin_lock_bh(&backbone_gw->crc_lock);
871 backbone_crc = backbone_gw->crc;
872 spin_unlock_bh(&backbone_gw->crc_lock);
874 if (backbone_crc != crc) {
875 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
876 "handle_announce(): CRC FAILED for %pM/%d (my = %#.4x, sent = %#.4x)\n",
878 BATADV_PRINT_VID(backbone_gw->vid),
881 batadv_bla_send_request(backbone_gw);
883 /* if we have sent a request and the crc was OK,
884 * we can allow traffic again.
886 if (atomic_read(&backbone_gw->request_sent)) {
887 atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
888 atomic_set(&backbone_gw->request_sent, 0);
892 batadv_backbone_gw_put(backbone_gw);
897 * batadv_handle_request - check for REQUEST frame
898 * @bat_priv: the bat priv with all the soft interface information
899 * @primary_if: the primary hard interface of this batman soft interface
900 * @backbone_addr: backbone address to be requested (ARP sender HW MAC)
901 * @ethhdr: ethernet header of a packet
902 * @vid: the VLAN ID of the frame
904 * Return: true if handled
906 static bool batadv_handle_request(struct batadv_priv *bat_priv,
907 struct batadv_hard_iface *primary_if,
908 u8 *backbone_addr, struct ethhdr *ethhdr,
911 /* check for REQUEST frame */
912 if (!batadv_compare_eth(backbone_addr, ethhdr->h_dest))
915 /* sanity check, this should not happen on a normal switch,
916 * we ignore it in this case.
918 if (!batadv_compare_eth(ethhdr->h_dest, primary_if->net_dev->dev_addr))
921 batadv_dbg(BATADV_DBG_BLA, bat_priv,
922 "handle_request(): REQUEST vid %d (sent by %pM)...\n",
923 BATADV_PRINT_VID(vid), ethhdr->h_source);
925 batadv_bla_answer_request(bat_priv, primary_if, vid);
930 * batadv_handle_unclaim - check for UNCLAIM frame
931 * @bat_priv: the bat priv with all the soft interface information
932 * @primary_if: the primary hard interface of this batman soft interface
933 * @backbone_addr: originator address of the backbone (Ethernet source)
934 * @claim_addr: Client to be unclaimed (ARP sender HW MAC)
935 * @vid: the VLAN ID of the frame
937 * Return: true if handled
939 static bool batadv_handle_unclaim(struct batadv_priv *bat_priv,
940 struct batadv_hard_iface *primary_if,
941 u8 *backbone_addr, u8 *claim_addr,
944 struct batadv_bla_backbone_gw *backbone_gw;
946 /* unclaim in any case if it is our own */
947 if (primary_if && batadv_compare_eth(backbone_addr,
948 primary_if->net_dev->dev_addr))
949 batadv_bla_send_claim(bat_priv, claim_addr, vid,
950 BATADV_CLAIM_TYPE_UNCLAIM);
952 backbone_gw = batadv_backbone_hash_find(bat_priv, backbone_addr, vid);
957 /* this must be an UNCLAIM frame */
958 batadv_dbg(BATADV_DBG_BLA, bat_priv,
959 "handle_unclaim(): UNCLAIM %pM on vid %d (sent by %pM)...\n",
960 claim_addr, BATADV_PRINT_VID(vid), backbone_gw->orig);
962 batadv_bla_del_claim(bat_priv, claim_addr, vid);
963 batadv_backbone_gw_put(backbone_gw);
968 * batadv_handle_claim - check for CLAIM frame
969 * @bat_priv: the bat priv with all the soft interface information
970 * @primary_if: the primary hard interface of this batman soft interface
971 * @backbone_addr: originator address of the backbone (Ethernet Source)
972 * @claim_addr: client mac address to be claimed (ARP sender HW MAC)
973 * @vid: the VLAN ID of the frame
975 * Return: true if handled
977 static bool batadv_handle_claim(struct batadv_priv *bat_priv,
978 struct batadv_hard_iface *primary_if,
979 u8 *backbone_addr, u8 *claim_addr,
982 struct batadv_bla_backbone_gw *backbone_gw;
984 /* register the gateway if not yet available, and add the claim. */
986 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
989 if (unlikely(!backbone_gw))
992 /* this must be a CLAIM frame */
993 batadv_bla_add_claim(bat_priv, claim_addr, vid, backbone_gw);
994 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
995 batadv_bla_send_claim(bat_priv, claim_addr, vid,
996 BATADV_CLAIM_TYPE_CLAIM);
998 /* TODO: we could call something like tt_local_del() here. */
1000 batadv_backbone_gw_put(backbone_gw);
1005 * batadv_check_claim_group - check for claim group membership
1006 * @bat_priv: the bat priv with all the soft interface information
1007 * @primary_if: the primary interface of this batman interface
1008 * @hw_src: the Hardware source in the ARP Header
1009 * @hw_dst: the Hardware destination in the ARP Header
1010 * @ethhdr: pointer to the Ethernet header of the claim frame
1012 * checks if it is a claim packet and if its on the same group.
1013 * This function also applies the group ID of the sender
1014 * if it is in the same mesh.
1017 * 2 - if it is a claim packet and on the same group
1018 * 1 - if is a claim packet from another group
1019 * 0 - if it is not a claim packet
1021 static int batadv_check_claim_group(struct batadv_priv *bat_priv,
1022 struct batadv_hard_iface *primary_if,
1023 u8 *hw_src, u8 *hw_dst,
1024 struct ethhdr *ethhdr)
1027 struct batadv_orig_node *orig_node;
1028 struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
1030 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
1031 bla_dst_own = &bat_priv->bla.claim_dest;
1033 /* if announcement packet, use the source,
1034 * otherwise assume it is in the hw_src
1036 switch (bla_dst->type) {
1037 case BATADV_CLAIM_TYPE_CLAIM:
1038 backbone_addr = hw_src;
1040 case BATADV_CLAIM_TYPE_REQUEST:
1041 case BATADV_CLAIM_TYPE_ANNOUNCE:
1042 case BATADV_CLAIM_TYPE_UNCLAIM:
1043 backbone_addr = ethhdr->h_source;
1049 /* don't accept claim frames from ourselves */
1050 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
1053 /* if its already the same group, it is fine. */
1054 if (bla_dst->group == bla_dst_own->group)
1057 /* lets see if this originator is in our mesh */
1058 orig_node = batadv_orig_hash_find(bat_priv, backbone_addr);
1060 /* dont accept claims from gateways which are not in
1061 * the same mesh or group.
1066 /* if our mesh friends mac is bigger, use it for ourselves. */
1067 if (ntohs(bla_dst->group) > ntohs(bla_dst_own->group)) {
1068 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1069 "taking other backbones claim group: %#.4x\n",
1070 ntohs(bla_dst->group));
1071 bla_dst_own->group = bla_dst->group;
1074 batadv_orig_node_put(orig_node);
1080 * batadv_bla_process_claim - Check if this is a claim frame, and process it
1081 * @bat_priv: the bat priv with all the soft interface information
1082 * @primary_if: the primary hard interface of this batman soft interface
1083 * @skb: the frame to be checked
1085 * Return: true if it was a claim frame, otherwise return false to
1086 * tell the callee that it can use the frame on its own.
1088 static bool batadv_bla_process_claim(struct batadv_priv *bat_priv,
1089 struct batadv_hard_iface *primary_if,
1090 struct sk_buff *skb)
1092 struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
1093 u8 *hw_src, *hw_dst;
1094 struct vlan_hdr *vhdr, vhdr_buf;
1095 struct ethhdr *ethhdr;
1096 struct arphdr *arphdr;
1103 vid = batadv_get_vid(skb, 0);
1104 ethhdr = eth_hdr(skb);
1106 proto = ethhdr->h_proto;
1108 if (vid & BATADV_VLAN_HAS_TAG) {
1109 /* Traverse the VLAN/Ethertypes.
1111 * At this point it is known that the first protocol is a VLAN
1112 * header, so start checking at the encapsulated protocol.
1114 * The depth of the VLAN headers is recorded to drop BLA claim
1115 * frames encapsulated into multiple VLAN headers (QinQ).
1118 vhdr = skb_header_pointer(skb, headlen, VLAN_HLEN,
1123 proto = vhdr->h_vlan_encapsulated_proto;
1124 headlen += VLAN_HLEN;
1126 } while (proto == htons(ETH_P_8021Q));
1129 if (proto != htons(ETH_P_ARP))
1130 return false; /* not a claim frame */
1132 /* this must be a ARP frame. check if it is a claim. */
1134 if (unlikely(!pskb_may_pull(skb, headlen + arp_hdr_len(skb->dev))))
1137 /* pskb_may_pull() may have modified the pointers, get ethhdr again */
1138 ethhdr = eth_hdr(skb);
1139 arphdr = (struct arphdr *)((u8 *)ethhdr + headlen);
1141 /* Check whether the ARP frame carries a valid
1144 if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
1146 if (arphdr->ar_pro != htons(ETH_P_IP))
1148 if (arphdr->ar_hln != ETH_ALEN)
1150 if (arphdr->ar_pln != 4)
1153 hw_src = (u8 *)arphdr + sizeof(struct arphdr);
1154 hw_dst = hw_src + ETH_ALEN + 4;
1155 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
1156 bla_dst_own = &bat_priv->bla.claim_dest;
1158 /* check if it is a claim frame in general */
1159 if (memcmp(bla_dst->magic, bla_dst_own->magic,
1160 sizeof(bla_dst->magic)) != 0)
1163 /* check if there is a claim frame encapsulated deeper in (QinQ) and
1164 * drop that, as this is not supported by BLA but should also not be
1165 * sent via the mesh.
1170 /* Let the loopdetect frames on the mesh in any case. */
1171 if (bla_dst->type == BATADV_CLAIM_TYPE_LOOPDETECT)
1174 /* check if it is a claim frame. */
1175 ret = batadv_check_claim_group(bat_priv, primary_if, hw_src, hw_dst,
1178 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1179 "bla_process_claim(): received a claim frame from another group. From: %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n",
1180 ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src,
1186 /* become a backbone gw ourselves on this vlan if not happened yet */
1187 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
1189 /* check for the different types of claim frames ... */
1190 switch (bla_dst->type) {
1191 case BATADV_CLAIM_TYPE_CLAIM:
1192 if (batadv_handle_claim(bat_priv, primary_if, hw_src,
1193 ethhdr->h_source, vid))
1196 case BATADV_CLAIM_TYPE_UNCLAIM:
1197 if (batadv_handle_unclaim(bat_priv, primary_if,
1198 ethhdr->h_source, hw_src, vid))
1202 case BATADV_CLAIM_TYPE_ANNOUNCE:
1203 if (batadv_handle_announce(bat_priv, hw_src, ethhdr->h_source,
1207 case BATADV_CLAIM_TYPE_REQUEST:
1208 if (batadv_handle_request(bat_priv, primary_if, hw_src, ethhdr,
1214 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1215 "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",
1216 ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src, hw_dst);
1221 * batadv_bla_purge_backbone_gw - Remove backbone gateways after a timeout or
1223 * @bat_priv: the bat priv with all the soft interface information
1224 * @now: whether the whole hash shall be wiped now
1226 * Check when we last heard from other nodes, and remove them in case of
1227 * a time out, or clean all backbone gws if now is set.
1229 static void batadv_bla_purge_backbone_gw(struct batadv_priv *bat_priv, int now)
1231 struct batadv_bla_backbone_gw *backbone_gw;
1232 struct hlist_node *node_tmp;
1233 struct hlist_head *head;
1234 struct batadv_hashtable *hash;
1235 spinlock_t *list_lock; /* protects write access to the hash lists */
1238 hash = bat_priv->bla.backbone_hash;
1242 for (i = 0; i < hash->size; i++) {
1243 head = &hash->table[i];
1244 list_lock = &hash->list_locks[i];
1246 spin_lock_bh(list_lock);
1247 hlist_for_each_entry_safe(backbone_gw, node_tmp,
1251 if (!batadv_has_timed_out(backbone_gw->lasttime,
1252 BATADV_BLA_BACKBONE_TIMEOUT))
1255 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
1256 "bla_purge_backbone_gw(): backbone gw %pM timed out\n",
1260 /* don't wait for the pending request anymore */
1261 if (atomic_read(&backbone_gw->request_sent))
1262 atomic_dec(&bat_priv->bla.num_requests);
1264 batadv_bla_del_backbone_claims(backbone_gw);
1266 hlist_del_rcu(&backbone_gw->hash_entry);
1267 batadv_backbone_gw_put(backbone_gw);
1269 spin_unlock_bh(list_lock);
1274 * batadv_bla_purge_claims - Remove claims after a timeout or immediately
1275 * @bat_priv: the bat priv with all the soft interface information
1276 * @primary_if: the selected primary interface, may be NULL if now is set
1277 * @now: whether the whole hash shall be wiped now
1279 * Check when we heard last time from our own claims, and remove them in case of
1280 * a time out, or clean all claims if now is set
1282 static void batadv_bla_purge_claims(struct batadv_priv *bat_priv,
1283 struct batadv_hard_iface *primary_if,
1286 struct batadv_bla_backbone_gw *backbone_gw;
1287 struct batadv_bla_claim *claim;
1288 struct hlist_head *head;
1289 struct batadv_hashtable *hash;
1292 hash = bat_priv->bla.claim_hash;
1296 for (i = 0; i < hash->size; i++) {
1297 head = &hash->table[i];
1300 hlist_for_each_entry_rcu(claim, head, hash_entry) {
1301 backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
1305 if (!batadv_compare_eth(backbone_gw->orig,
1306 primary_if->net_dev->dev_addr))
1309 if (!batadv_has_timed_out(claim->lasttime,
1310 BATADV_BLA_CLAIM_TIMEOUT))
1313 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1314 "bla_purge_claims(): %pM, vid %d, time out\n",
1315 claim->addr, claim->vid);
1318 batadv_handle_unclaim(bat_priv, primary_if,
1320 claim->addr, claim->vid);
1322 batadv_backbone_gw_put(backbone_gw);
1329 * batadv_bla_update_orig_address - Update the backbone gateways when the own
1330 * originator address changes
1331 * @bat_priv: the bat priv with all the soft interface information
1332 * @primary_if: the new selected primary_if
1333 * @oldif: the old primary interface, may be NULL
1335 void batadv_bla_update_orig_address(struct batadv_priv *bat_priv,
1336 struct batadv_hard_iface *primary_if,
1337 struct batadv_hard_iface *oldif)
1339 struct batadv_bla_backbone_gw *backbone_gw;
1340 struct hlist_head *head;
1341 struct batadv_hashtable *hash;
1345 /* reset bridge loop avoidance group id */
1346 group = htons(crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN));
1347 bat_priv->bla.claim_dest.group = group;
1349 /* purge everything when bridge loop avoidance is turned off */
1350 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1354 batadv_bla_purge_claims(bat_priv, NULL, 1);
1355 batadv_bla_purge_backbone_gw(bat_priv, 1);
1359 hash = bat_priv->bla.backbone_hash;
1363 for (i = 0; i < hash->size; i++) {
1364 head = &hash->table[i];
1367 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
1368 /* own orig still holds the old value. */
1369 if (!batadv_compare_eth(backbone_gw->orig,
1370 oldif->net_dev->dev_addr))
1373 ether_addr_copy(backbone_gw->orig,
1374 primary_if->net_dev->dev_addr);
1375 /* send an announce frame so others will ask for our
1376 * claims and update their tables.
1378 batadv_bla_send_announce(bat_priv, backbone_gw);
1385 * batadv_bla_send_loopdetect - send a loopdetect frame
1386 * @bat_priv: the bat priv with all the soft interface information
1387 * @backbone_gw: the backbone gateway for which a loop should be detected
1389 * To detect loops that the bridge loop avoidance can't handle, send a loop
1390 * detection packet on the backbone. Unlike other BLA frames, this frame will
1391 * be allowed on the mesh by other nodes. If it is received on the mesh, this
1392 * indicates that there is a loop.
1395 batadv_bla_send_loopdetect(struct batadv_priv *bat_priv,
1396 struct batadv_bla_backbone_gw *backbone_gw)
1398 batadv_dbg(BATADV_DBG_BLA, bat_priv, "Send loopdetect frame for vid %d\n",
1400 batadv_bla_send_claim(bat_priv, bat_priv->bla.loopdetect_addr,
1401 backbone_gw->vid, BATADV_CLAIM_TYPE_LOOPDETECT);
1405 * batadv_bla_status_update - purge bla interfaces if necessary
1406 * @net_dev: the soft interface net device
1408 void batadv_bla_status_update(struct net_device *net_dev)
1410 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1411 struct batadv_hard_iface *primary_if;
1413 primary_if = batadv_primary_if_get_selected(bat_priv);
1417 /* this function already purges everything when bla is disabled,
1418 * so just call that one.
1420 batadv_bla_update_orig_address(bat_priv, primary_if, primary_if);
1421 batadv_hardif_put(primary_if);
1425 * batadv_bla_periodic_work - performs periodic bla work
1426 * @work: kernel work struct
1428 * periodic work to do:
1429 * * purge structures when they are too old
1430 * * send announcements
1432 static void batadv_bla_periodic_work(struct work_struct *work)
1434 struct delayed_work *delayed_work;
1435 struct batadv_priv *bat_priv;
1436 struct batadv_priv_bla *priv_bla;
1437 struct hlist_head *head;
1438 struct batadv_bla_backbone_gw *backbone_gw;
1439 struct batadv_hashtable *hash;
1440 struct batadv_hard_iface *primary_if;
1441 bool send_loopdetect = false;
1444 delayed_work = to_delayed_work(work);
1445 priv_bla = container_of(delayed_work, struct batadv_priv_bla, work);
1446 bat_priv = container_of(priv_bla, struct batadv_priv, bla);
1447 primary_if = batadv_primary_if_get_selected(bat_priv);
1451 batadv_bla_purge_claims(bat_priv, primary_if, 0);
1452 batadv_bla_purge_backbone_gw(bat_priv, 0);
1454 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1457 if (atomic_dec_and_test(&bat_priv->bla.loopdetect_next)) {
1458 /* set a new random mac address for the next bridge loop
1459 * detection frames. Set the locally administered bit to avoid
1460 * collisions with users mac addresses.
1462 random_ether_addr(bat_priv->bla.loopdetect_addr);
1463 bat_priv->bla.loopdetect_addr[0] = 0xba;
1464 bat_priv->bla.loopdetect_addr[1] = 0xbe;
1465 bat_priv->bla.loopdetect_lasttime = jiffies;
1466 atomic_set(&bat_priv->bla.loopdetect_next,
1467 BATADV_BLA_LOOPDETECT_PERIODS);
1469 /* mark for sending loop detect on all VLANs */
1470 send_loopdetect = true;
1473 hash = bat_priv->bla.backbone_hash;
1477 for (i = 0; i < hash->size; i++) {
1478 head = &hash->table[i];
1481 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
1482 if (!batadv_compare_eth(backbone_gw->orig,
1483 primary_if->net_dev->dev_addr))
1486 backbone_gw->lasttime = jiffies;
1488 batadv_bla_send_announce(bat_priv, backbone_gw);
1489 if (send_loopdetect)
1490 batadv_bla_send_loopdetect(bat_priv,
1493 /* request_sent is only set after creation to avoid
1494 * problems when we are not yet known as backbone gw
1497 * We can reset this now after we waited some periods
1498 * to give bridge forward delays and bla group forming
1502 if (atomic_read(&backbone_gw->request_sent) == 0)
1505 if (!atomic_dec_and_test(&backbone_gw->wait_periods))
1508 atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
1509 atomic_set(&backbone_gw->request_sent, 0);
1515 batadv_hardif_put(primary_if);
1517 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1518 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
1521 /* The hash for claim and backbone hash receive the same key because they
1522 * are getting initialized by hash_new with the same key. Reinitializing
1523 * them with to different keys to allow nested locking without generating
1526 static struct lock_class_key batadv_claim_hash_lock_class_key;
1527 static struct lock_class_key batadv_backbone_hash_lock_class_key;
1530 * batadv_bla_init - initialize all bla structures
1531 * @bat_priv: the bat priv with all the soft interface information
1533 * Return: 0 on success, < 0 on error.
1535 int batadv_bla_init(struct batadv_priv *bat_priv)
1538 u8 claim_dest[ETH_ALEN] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00};
1539 struct batadv_hard_iface *primary_if;
1541 unsigned long entrytime;
1543 spin_lock_init(&bat_priv->bla.bcast_duplist_lock);
1545 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hash registering\n");
1547 /* setting claim destination address */
1548 memcpy(&bat_priv->bla.claim_dest.magic, claim_dest, 3);
1549 bat_priv->bla.claim_dest.type = 0;
1550 primary_if = batadv_primary_if_get_selected(bat_priv);
1552 crc = crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN);
1553 bat_priv->bla.claim_dest.group = htons(crc);
1554 batadv_hardif_put(primary_if);
1556 bat_priv->bla.claim_dest.group = 0; /* will be set later */
1559 /* initialize the duplicate list */
1560 entrytime = jiffies - msecs_to_jiffies(BATADV_DUPLIST_TIMEOUT);
1561 for (i = 0; i < BATADV_DUPLIST_SIZE; i++)
1562 bat_priv->bla.bcast_duplist[i].entrytime = entrytime;
1563 bat_priv->bla.bcast_duplist_curr = 0;
1565 atomic_set(&bat_priv->bla.loopdetect_next,
1566 BATADV_BLA_LOOPDETECT_PERIODS);
1568 if (bat_priv->bla.claim_hash)
1571 bat_priv->bla.claim_hash = batadv_hash_new(128);
1572 if (!bat_priv->bla.claim_hash)
1575 bat_priv->bla.backbone_hash = batadv_hash_new(32);
1576 if (!bat_priv->bla.backbone_hash) {
1577 batadv_hash_destroy(bat_priv->bla.claim_hash);
1581 batadv_hash_set_lock_class(bat_priv->bla.claim_hash,
1582 &batadv_claim_hash_lock_class_key);
1583 batadv_hash_set_lock_class(bat_priv->bla.backbone_hash,
1584 &batadv_backbone_hash_lock_class_key);
1586 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hashes initialized\n");
1588 INIT_DELAYED_WORK(&bat_priv->bla.work, batadv_bla_periodic_work);
1590 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1591 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
1596 * batadv_bla_check_duplist() - Check if a frame is in the broadcast dup.
1597 * @bat_priv: the bat priv with all the soft interface information
1598 * @skb: contains the multicast packet to be checked
1599 * @payload_ptr: pointer to position inside the head buffer of the skb
1600 * marking the start of the data to be CRC'ed
1601 * @orig: originator mac address, NULL if unknown
1603 * Check if it is on our broadcast list. Another gateway might have sent the
1604 * same packet because it is connected to the same backbone, so we have to
1605 * remove this duplicate.
1607 * This is performed by checking the CRC, which will tell us
1608 * with a good chance that it is the same packet. If it is furthermore
1609 * sent by another host, drop it. We allow equal packets from
1610 * the same host however as this might be intended.
1612 * Return: true if a packet is in the duplicate list, false otherwise.
1614 static bool batadv_bla_check_duplist(struct batadv_priv *bat_priv,
1615 struct sk_buff *skb, u8 *payload_ptr,
1618 struct batadv_bcast_duplist_entry *entry;
1623 /* calculate the crc ... */
1624 crc = batadv_skb_crc32(skb, payload_ptr);
1626 spin_lock_bh(&bat_priv->bla.bcast_duplist_lock);
1628 for (i = 0; i < BATADV_DUPLIST_SIZE; i++) {
1629 curr = (bat_priv->bla.bcast_duplist_curr + i);
1630 curr %= BATADV_DUPLIST_SIZE;
1631 entry = &bat_priv->bla.bcast_duplist[curr];
1633 /* we can stop searching if the entry is too old ;
1634 * later entries will be even older
1636 if (batadv_has_timed_out(entry->entrytime,
1637 BATADV_DUPLIST_TIMEOUT))
1640 if (entry->crc != crc)
1643 /* are the originators both known and not anonymous? */
1644 if (orig && !is_zero_ether_addr(orig) &&
1645 !is_zero_ether_addr(entry->orig)) {
1646 /* If known, check if the new frame came from
1647 * the same originator:
1648 * We are safe to take identical frames from the
1649 * same orig, if known, as multiplications in
1650 * the mesh are detected via the (orig, seqno) pair.
1651 * So we can be a bit more liberal here and allow
1652 * identical frames from the same orig which the source
1653 * host might have sent multiple times on purpose.
1655 if (batadv_compare_eth(entry->orig, orig))
1659 /* this entry seems to match: same crc, not too old,
1660 * and from another gw. therefore return true to forbid it.
1665 /* not found, add a new entry (overwrite the oldest entry)
1666 * and allow it, its the first occurrence.
1668 curr = (bat_priv->bla.bcast_duplist_curr + BATADV_DUPLIST_SIZE - 1);
1669 curr %= BATADV_DUPLIST_SIZE;
1670 entry = &bat_priv->bla.bcast_duplist[curr];
1672 entry->entrytime = jiffies;
1674 /* known originator */
1676 ether_addr_copy(entry->orig, orig);
1677 /* anonymous originator */
1679 eth_zero_addr(entry->orig);
1681 bat_priv->bla.bcast_duplist_curr = curr;
1684 spin_unlock_bh(&bat_priv->bla.bcast_duplist_lock);
1690 * batadv_bla_check_ucast_duplist() - Check if a frame is in the broadcast dup.
1691 * @bat_priv: the bat priv with all the soft interface information
1692 * @skb: contains the multicast packet to be checked, decapsulated from a
1695 * Check if it is on our broadcast list. Another gateway might have sent the
1696 * same packet because it is connected to the same backbone, so we have to
1697 * remove this duplicate.
1699 * Return: true if a packet is in the duplicate list, false otherwise.
1701 static bool batadv_bla_check_ucast_duplist(struct batadv_priv *bat_priv,
1702 struct sk_buff *skb)
1704 return batadv_bla_check_duplist(bat_priv, skb, (u8 *)skb->data, NULL);
1708 * batadv_bla_check_bcast_duplist() - Check if a frame is in the broadcast dup.
1709 * @bat_priv: the bat priv with all the soft interface information
1710 * @skb: contains the bcast_packet to be checked
1712 * Check if it is on our broadcast list. Another gateway might have sent the
1713 * same packet because it is connected to the same backbone, so we have to
1714 * remove this duplicate.
1716 * Return: true if a packet is in the duplicate list, false otherwise.
1718 bool batadv_bla_check_bcast_duplist(struct batadv_priv *bat_priv,
1719 struct sk_buff *skb)
1721 struct batadv_bcast_packet *bcast_packet;
1724 bcast_packet = (struct batadv_bcast_packet *)skb->data;
1725 payload_ptr = (u8 *)(bcast_packet + 1);
1727 return batadv_bla_check_duplist(bat_priv, skb, payload_ptr,
1728 bcast_packet->orig);
1732 * batadv_bla_is_backbone_gw_orig - Check if the originator is a gateway for
1733 * the VLAN identified by vid.
1734 * @bat_priv: the bat priv with all the soft interface information
1735 * @orig: originator mac address
1736 * @vid: VLAN identifier
1738 * Return: true if orig is a backbone for this vid, false otherwise.
1740 bool batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, u8 *orig,
1743 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
1744 struct hlist_head *head;
1745 struct batadv_bla_backbone_gw *backbone_gw;
1748 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1754 for (i = 0; i < hash->size; i++) {
1755 head = &hash->table[i];
1758 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
1759 if (batadv_compare_eth(backbone_gw->orig, orig) &&
1760 backbone_gw->vid == vid) {
1772 * batadv_bla_is_backbone_gw - check if originator is a backbone gw for a VLAN.
1773 * @skb: the frame to be checked
1774 * @orig_node: the orig_node of the frame
1775 * @hdr_size: maximum length of the frame
1777 * Return: true if the orig_node is also a gateway on the soft interface,
1778 * otherwise it returns false.
1780 bool batadv_bla_is_backbone_gw(struct sk_buff *skb,
1781 struct batadv_orig_node *orig_node, int hdr_size)
1783 struct batadv_bla_backbone_gw *backbone_gw;
1786 if (!atomic_read(&orig_node->bat_priv->bridge_loop_avoidance))
1789 /* first, find out the vid. */
1790 if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
1793 vid = batadv_get_vid(skb, hdr_size);
1795 /* see if this originator is a backbone gw for this VLAN */
1796 backbone_gw = batadv_backbone_hash_find(orig_node->bat_priv,
1797 orig_node->orig, vid);
1801 batadv_backbone_gw_put(backbone_gw);
1806 * batadv_bla_free - free all bla structures
1807 * @bat_priv: the bat priv with all the soft interface information
1809 * for softinterface free or module unload
1811 void batadv_bla_free(struct batadv_priv *bat_priv)
1813 struct batadv_hard_iface *primary_if;
1815 cancel_delayed_work_sync(&bat_priv->bla.work);
1816 primary_if = batadv_primary_if_get_selected(bat_priv);
1818 if (bat_priv->bla.claim_hash) {
1819 batadv_bla_purge_claims(bat_priv, primary_if, 1);
1820 batadv_hash_destroy(bat_priv->bla.claim_hash);
1821 bat_priv->bla.claim_hash = NULL;
1823 if (bat_priv->bla.backbone_hash) {
1824 batadv_bla_purge_backbone_gw(bat_priv, 1);
1825 batadv_hash_destroy(bat_priv->bla.backbone_hash);
1826 bat_priv->bla.backbone_hash = NULL;
1829 batadv_hardif_put(primary_if);
1833 * batadv_bla_loopdetect_check - check and handle a detected loop
1834 * @bat_priv: the bat priv with all the soft interface information
1835 * @skb: the packet to check
1836 * @primary_if: interface where the request came on
1837 * @vid: the VLAN ID of the frame
1839 * Checks if this packet is a loop detect frame which has been sent by us,
1840 * throw an uevent and log the event if that is the case.
1842 * Return: true if it is a loop detect frame which is to be dropped, false
1846 batadv_bla_loopdetect_check(struct batadv_priv *bat_priv, struct sk_buff *skb,
1847 struct batadv_hard_iface *primary_if,
1850 struct batadv_bla_backbone_gw *backbone_gw;
1851 struct ethhdr *ethhdr;
1854 ethhdr = eth_hdr(skb);
1856 /* Only check for the MAC address and skip more checks here for
1857 * performance reasons - this function is on the hotpath, after all.
1859 if (!batadv_compare_eth(ethhdr->h_source,
1860 bat_priv->bla.loopdetect_addr))
1863 /* If the packet came too late, don't forward it on the mesh
1864 * but don't consider that as loop. It might be a coincidence.
1866 if (batadv_has_timed_out(bat_priv->bla.loopdetect_lasttime,
1867 BATADV_BLA_LOOPDETECT_TIMEOUT))
1870 backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
1871 primary_if->net_dev->dev_addr,
1873 if (unlikely(!backbone_gw))
1876 ret = queue_work(batadv_event_workqueue, &backbone_gw->report_work);
1878 /* backbone_gw is unreferenced in the report work function function
1879 * if queue_work() call was successful
1882 batadv_backbone_gw_put(backbone_gw);
1888 * batadv_bla_rx - check packets coming from the mesh.
1889 * @bat_priv: the bat priv with all the soft interface information
1890 * @skb: the frame to be checked
1891 * @vid: the VLAN ID of the frame
1892 * @packet_type: the batman packet type this frame came in
1894 * batadv_bla_rx avoidance checks if:
1895 * * we have to race for a claim
1896 * * if the frame is allowed on the LAN
1898 * in these cases, the skb is further handled by this function
1900 * Return: true if handled, otherwise it returns false and the caller shall
1901 * further process the skb.
1903 bool batadv_bla_rx(struct batadv_priv *bat_priv, struct sk_buff *skb,
1904 unsigned short vid, int packet_type)
1906 struct batadv_bla_backbone_gw *backbone_gw;
1907 struct ethhdr *ethhdr;
1908 struct batadv_bla_claim search_claim, *claim = NULL;
1909 struct batadv_hard_iface *primary_if;
1913 ethhdr = eth_hdr(skb);
1915 primary_if = batadv_primary_if_get_selected(bat_priv);
1919 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1922 if (batadv_bla_loopdetect_check(bat_priv, skb, primary_if, vid))
1925 if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
1926 /* don't allow multicast packets while requests are in flight */
1927 if (is_multicast_ether_addr(ethhdr->h_dest))
1928 /* Both broadcast flooding or multicast-via-unicasts
1929 * delivery might send to multiple backbone gateways
1930 * sharing the same LAN and therefore need to coordinate
1931 * which backbone gateway forwards into the LAN,
1932 * by claiming the payload source address.
1934 * Broadcast flooding and multicast-via-unicasts
1935 * delivery use the following two batman packet types.
1936 * Note: explicitly exclude BATADV_UNICAST_4ADDR,
1937 * as the DHCP gateway feature will send explicitly
1938 * to only one BLA gateway, so the claiming process
1939 * should be avoided there.
1941 if (packet_type == BATADV_BCAST ||
1942 packet_type == BATADV_UNICAST)
1945 /* potential duplicates from foreign BLA backbone gateways via
1946 * multicast-in-unicast packets
1948 if (is_multicast_ether_addr(ethhdr->h_dest) &&
1949 packet_type == BATADV_UNICAST &&
1950 batadv_bla_check_ucast_duplist(bat_priv, skb))
1953 ether_addr_copy(search_claim.addr, ethhdr->h_source);
1954 search_claim.vid = vid;
1955 claim = batadv_claim_hash_find(bat_priv, &search_claim);
1958 /* possible optimization: race for a claim */
1959 /* No claim exists yet, claim it for us!
1961 batadv_handle_claim(bat_priv, primary_if,
1962 primary_if->net_dev->dev_addr,
1963 ethhdr->h_source, vid);
1967 /* if it is our own claim ... */
1968 backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
1969 own_claim = batadv_compare_eth(backbone_gw->orig,
1970 primary_if->net_dev->dev_addr);
1971 batadv_backbone_gw_put(backbone_gw);
1974 /* ... allow it in any case */
1975 claim->lasttime = jiffies;
1979 /* if it is a multicast ... */
1980 if (is_multicast_ether_addr(ethhdr->h_dest) &&
1981 (packet_type == BATADV_BCAST || packet_type == BATADV_UNICAST)) {
1982 /* ... drop it. the responsible gateway is in charge.
1984 * We need to check packet type because with the gateway
1985 * feature, broadcasts (like DHCP requests) may be sent
1986 * using a unicast 4 address packet type. See comment above.
1990 /* seems the client considers us as its best gateway.
1991 * send a claim and update the claim table
1994 batadv_handle_claim(bat_priv, primary_if,
1995 primary_if->net_dev->dev_addr,
1996 ethhdr->h_source, vid);
2000 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
2010 batadv_hardif_put(primary_if);
2012 batadv_claim_put(claim);
2017 * batadv_bla_tx - check packets going into the mesh
2018 * @bat_priv: the bat priv with all the soft interface information
2019 * @skb: the frame to be checked
2020 * @vid: the VLAN ID of the frame
2022 * batadv_bla_tx checks if:
2023 * * a claim was received which has to be processed
2024 * * the frame is allowed on the mesh
2026 * in these cases, the skb is further handled by this function.
2028 * This call might reallocate skb data.
2030 * Return: true if handled, otherwise it returns false and the caller shall
2031 * further process the skb.
2033 bool batadv_bla_tx(struct batadv_priv *bat_priv, struct sk_buff *skb,
2036 struct ethhdr *ethhdr;
2037 struct batadv_bla_claim search_claim, *claim = NULL;
2038 struct batadv_bla_backbone_gw *backbone_gw;
2039 struct batadv_hard_iface *primary_if;
2043 primary_if = batadv_primary_if_get_selected(bat_priv);
2047 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
2050 if (batadv_bla_process_claim(bat_priv, primary_if, skb))
2053 ethhdr = eth_hdr(skb);
2055 if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
2056 /* don't allow broadcasts while requests are in flight */
2057 if (is_multicast_ether_addr(ethhdr->h_dest))
2060 ether_addr_copy(search_claim.addr, ethhdr->h_source);
2061 search_claim.vid = vid;
2063 claim = batadv_claim_hash_find(bat_priv, &search_claim);
2065 /* if no claim exists, allow it. */
2069 /* check if we are responsible. */
2070 backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
2071 client_roamed = batadv_compare_eth(backbone_gw->orig,
2072 primary_if->net_dev->dev_addr);
2073 batadv_backbone_gw_put(backbone_gw);
2075 if (client_roamed) {
2076 /* if yes, the client has roamed and we have
2079 if (batadv_has_timed_out(claim->lasttime, 100)) {
2080 /* only unclaim if the last claim entry is
2081 * older than 100 ms to make sure we really
2082 * have a roaming client here.
2084 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla_tx(): Roaming client %pM detected. Unclaim it.\n",
2086 batadv_handle_unclaim(bat_priv, primary_if,
2087 primary_if->net_dev->dev_addr,
2088 ethhdr->h_source, vid);
2091 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla_tx(): Race for claim %pM detected. Drop packet.\n",
2097 /* check if it is a multicast/broadcast frame */
2098 if (is_multicast_ether_addr(ethhdr->h_dest)) {
2099 /* drop it. the responsible gateway has forwarded it into
2100 * the backbone network.
2104 /* we must allow it. at least if we are
2105 * responsible for the DESTINATION.
2110 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
2117 batadv_hardif_put(primary_if);
2119 batadv_claim_put(claim);
2123 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
2125 * batadv_bla_claim_table_seq_print_text - print the claim table in a seq file
2126 * @seq: seq file to print on
2131 int batadv_bla_claim_table_seq_print_text(struct seq_file *seq, void *offset)
2133 struct net_device *net_dev = (struct net_device *)seq->private;
2134 struct batadv_priv *bat_priv = netdev_priv(net_dev);
2135 struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
2136 struct batadv_bla_backbone_gw *backbone_gw;
2137 struct batadv_bla_claim *claim;
2138 struct batadv_hard_iface *primary_if;
2139 struct hlist_head *head;
2145 primary_if = batadv_seq_print_text_primary_if_get(seq);
2149 primary_addr = primary_if->net_dev->dev_addr;
2151 "Claims announced for the mesh %s (orig %pM, group id %#.4x)\n",
2152 net_dev->name, primary_addr,
2153 ntohs(bat_priv->bla.claim_dest.group));
2155 " Client VID Originator [o] (CRC )\n");
2156 for (i = 0; i < hash->size; i++) {
2157 head = &hash->table[i];
2160 hlist_for_each_entry_rcu(claim, head, hash_entry) {
2161 backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
2163 is_own = batadv_compare_eth(backbone_gw->orig,
2166 spin_lock_bh(&backbone_gw->crc_lock);
2167 backbone_crc = backbone_gw->crc;
2168 spin_unlock_bh(&backbone_gw->crc_lock);
2169 seq_printf(seq, " * %pM on %5d by %pM [%c] (%#.4x)\n",
2170 claim->addr, BATADV_PRINT_VID(claim->vid),
2172 (is_own ? 'x' : ' '),
2175 batadv_backbone_gw_put(backbone_gw);
2181 batadv_hardif_put(primary_if);
2187 * batadv_bla_claim_dump_entry - dump one entry of the claim table
2188 * to a netlink socket
2189 * @msg: buffer for the message
2190 * @portid: netlink port
2191 * @seq: Sequence number of netlink message
2192 * @primary_if: primary interface
2193 * @claim: entry to dump
2195 * Return: 0 or error code.
2198 batadv_bla_claim_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
2199 struct batadv_hard_iface *primary_if,
2200 struct batadv_bla_claim *claim)
2202 u8 *primary_addr = primary_if->net_dev->dev_addr;
2208 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
2209 NLM_F_MULTI, BATADV_CMD_GET_BLA_CLAIM);
2215 is_own = batadv_compare_eth(claim->backbone_gw->orig,
2218 spin_lock_bh(&claim->backbone_gw->crc_lock);
2219 backbone_crc = claim->backbone_gw->crc;
2220 spin_unlock_bh(&claim->backbone_gw->crc_lock);
2223 if (nla_put_flag(msg, BATADV_ATTR_BLA_OWN)) {
2224 genlmsg_cancel(msg, hdr);
2228 if (nla_put(msg, BATADV_ATTR_BLA_ADDRESS, ETH_ALEN, claim->addr) ||
2229 nla_put_u16(msg, BATADV_ATTR_BLA_VID, claim->vid) ||
2230 nla_put(msg, BATADV_ATTR_BLA_BACKBONE, ETH_ALEN,
2231 claim->backbone_gw->orig) ||
2232 nla_put_u16(msg, BATADV_ATTR_BLA_CRC,
2234 genlmsg_cancel(msg, hdr);
2238 genlmsg_end(msg, hdr);
2246 * batadv_bla_claim_dump_bucket - dump one bucket of the claim table
2247 * to a netlink socket
2248 * @msg: buffer for the message
2249 * @portid: netlink port
2250 * @seq: Sequence number of netlink message
2251 * @primary_if: primary interface
2252 * @head: bucket to dump
2253 * @idx_skip: How many entries to skip
2258 batadv_bla_claim_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
2259 struct batadv_hard_iface *primary_if,
2260 struct hlist_head *head, int *idx_skip)
2262 struct batadv_bla_claim *claim;
2267 hlist_for_each_entry_rcu(claim, head, hash_entry) {
2268 if (idx++ < *idx_skip)
2271 ret = batadv_bla_claim_dump_entry(msg, portid, seq,
2274 *idx_skip = idx - 1;
2286 * batadv_bla_claim_dump - dump claim table to a netlink socket
2287 * @msg: buffer for the message
2288 * @cb: callback structure containing arguments
2290 * Return: message length.
2292 int batadv_bla_claim_dump(struct sk_buff *msg, struct netlink_callback *cb)
2294 struct batadv_hard_iface *primary_if = NULL;
2295 int portid = NETLINK_CB(cb->skb).portid;
2296 struct net *net = sock_net(cb->skb->sk);
2297 struct net_device *soft_iface;
2298 struct batadv_hashtable *hash;
2299 struct batadv_priv *bat_priv;
2300 int bucket = cb->args[0];
2301 struct hlist_head *head;
2302 int idx = cb->args[1];
2306 ifindex = batadv_netlink_get_ifindex(cb->nlh,
2307 BATADV_ATTR_MESH_IFINDEX);
2311 soft_iface = dev_get_by_index(net, ifindex);
2312 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
2317 bat_priv = netdev_priv(soft_iface);
2318 hash = bat_priv->bla.claim_hash;
2320 primary_if = batadv_primary_if_get_selected(bat_priv);
2321 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
2326 while (bucket < hash->size) {
2327 head = &hash->table[bucket];
2329 if (batadv_bla_claim_dump_bucket(msg, portid,
2331 primary_if, head, &idx))
2336 cb->args[0] = bucket;
2343 batadv_hardif_put(primary_if);
2346 dev_put(soft_iface);
2351 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
2353 * batadv_bla_backbone_table_seq_print_text - print the backbone table in a seq
2355 * @seq: seq file to print on
2360 int batadv_bla_backbone_table_seq_print_text(struct seq_file *seq, void *offset)
2362 struct net_device *net_dev = (struct net_device *)seq->private;
2363 struct batadv_priv *bat_priv = netdev_priv(net_dev);
2364 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
2365 struct batadv_bla_backbone_gw *backbone_gw;
2366 struct batadv_hard_iface *primary_if;
2367 struct hlist_head *head;
2374 primary_if = batadv_seq_print_text_primary_if_get(seq);
2378 primary_addr = primary_if->net_dev->dev_addr;
2380 "Backbones announced for the mesh %s (orig %pM, group id %#.4x)\n",
2381 net_dev->name, primary_addr,
2382 ntohs(bat_priv->bla.claim_dest.group));
2383 seq_puts(seq, " Originator VID last seen (CRC )\n");
2384 for (i = 0; i < hash->size; i++) {
2385 head = &hash->table[i];
2388 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
2389 msecs = jiffies_to_msecs(jiffies -
2390 backbone_gw->lasttime);
2391 secs = msecs / 1000;
2392 msecs = msecs % 1000;
2394 is_own = batadv_compare_eth(backbone_gw->orig,
2399 spin_lock_bh(&backbone_gw->crc_lock);
2400 backbone_crc = backbone_gw->crc;
2401 spin_unlock_bh(&backbone_gw->crc_lock);
2403 seq_printf(seq, " * %pM on %5d %4i.%03is (%#.4x)\n",
2405 BATADV_PRINT_VID(backbone_gw->vid), secs,
2406 msecs, backbone_crc);
2412 batadv_hardif_put(primary_if);
2418 * batadv_bla_backbone_dump_entry - dump one entry of the backbone table
2419 * to a netlink socket
2420 * @msg: buffer for the message
2421 * @portid: netlink port
2422 * @seq: Sequence number of netlink message
2423 * @primary_if: primary interface
2424 * @backbone_gw: entry to dump
2426 * Return: 0 or error code.
2429 batadv_bla_backbone_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
2430 struct batadv_hard_iface *primary_if,
2431 struct batadv_bla_backbone_gw *backbone_gw)
2433 u8 *primary_addr = primary_if->net_dev->dev_addr;
2440 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
2441 NLM_F_MULTI, BATADV_CMD_GET_BLA_BACKBONE);
2447 is_own = batadv_compare_eth(backbone_gw->orig, primary_addr);
2449 spin_lock_bh(&backbone_gw->crc_lock);
2450 backbone_crc = backbone_gw->crc;
2451 spin_unlock_bh(&backbone_gw->crc_lock);
2453 msecs = jiffies_to_msecs(jiffies - backbone_gw->lasttime);
2456 if (nla_put_flag(msg, BATADV_ATTR_BLA_OWN)) {
2457 genlmsg_cancel(msg, hdr);
2461 if (nla_put(msg, BATADV_ATTR_BLA_BACKBONE, ETH_ALEN,
2462 backbone_gw->orig) ||
2463 nla_put_u16(msg, BATADV_ATTR_BLA_VID, backbone_gw->vid) ||
2464 nla_put_u16(msg, BATADV_ATTR_BLA_CRC,
2466 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS, msecs)) {
2467 genlmsg_cancel(msg, hdr);
2471 genlmsg_end(msg, hdr);
2479 * batadv_bla_backbone_dump_bucket - dump one bucket of the backbone table
2480 * to a netlink socket
2481 * @msg: buffer for the message
2482 * @portid: netlink port
2483 * @seq: Sequence number of netlink message
2484 * @primary_if: primary interface
2485 * @head: bucket to dump
2486 * @idx_skip: How many entries to skip
2491 batadv_bla_backbone_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
2492 struct batadv_hard_iface *primary_if,
2493 struct hlist_head *head, int *idx_skip)
2495 struct batadv_bla_backbone_gw *backbone_gw;
2500 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
2501 if (idx++ < *idx_skip)
2504 ret = batadv_bla_backbone_dump_entry(msg, portid, seq,
2505 primary_if, backbone_gw);
2507 *idx_skip = idx - 1;
2519 * batadv_bla_backbone_dump - dump backbone table to a netlink socket
2520 * @msg: buffer for the message
2521 * @cb: callback structure containing arguments
2523 * Return: message length.
2525 int batadv_bla_backbone_dump(struct sk_buff *msg, struct netlink_callback *cb)
2527 struct batadv_hard_iface *primary_if = NULL;
2528 int portid = NETLINK_CB(cb->skb).portid;
2529 struct net *net = sock_net(cb->skb->sk);
2530 struct net_device *soft_iface;
2531 struct batadv_hashtable *hash;
2532 struct batadv_priv *bat_priv;
2533 int bucket = cb->args[0];
2534 struct hlist_head *head;
2535 int idx = cb->args[1];
2539 ifindex = batadv_netlink_get_ifindex(cb->nlh,
2540 BATADV_ATTR_MESH_IFINDEX);
2544 soft_iface = dev_get_by_index(net, ifindex);
2545 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
2550 bat_priv = netdev_priv(soft_iface);
2551 hash = bat_priv->bla.backbone_hash;
2553 primary_if = batadv_primary_if_get_selected(bat_priv);
2554 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
2559 while (bucket < hash->size) {
2560 head = &hash->table[bucket];
2562 if (batadv_bla_backbone_dump_bucket(msg, portid,
2564 primary_if, head, &idx))
2569 cb->args[0] = bucket;
2576 batadv_hardif_put(primary_if);
2579 dev_put(soft_iface);