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/list.h>
35 #include <linux/lockdep.h>
36 #include <linux/netdevice.h>
37 #include <linux/rculist.h>
38 #include <linux/rcupdate.h>
39 #include <linux/seq_file.h>
40 #include <linux/skbuff.h>
41 #include <linux/slab.h>
42 #include <linux/spinlock.h>
43 #include <linux/stddef.h>
44 #include <linux/string.h>
45 #include <linux/workqueue.h>
48 #include "hard-interface.h"
50 #include "originator.h"
52 #include "translation-table.h"
54 static const u8 batadv_announce_mac[4] = {0x43, 0x05, 0x43, 0x05};
56 static void batadv_bla_periodic_work(struct work_struct *work);
58 batadv_bla_send_announce(struct batadv_priv *bat_priv,
59 struct batadv_bla_backbone_gw *backbone_gw);
62 * batadv_choose_claim - choose the right bucket for a claim.
64 * @size: size of the hash table
66 * Return: the hash index of the claim
68 static inline u32 batadv_choose_claim(const void *data, u32 size)
70 struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
73 hash = jhash(&claim->addr, sizeof(claim->addr), hash);
74 hash = jhash(&claim->vid, sizeof(claim->vid), hash);
80 * batadv_choose_backbone_gw - choose the right bucket for a backbone gateway.
82 * @size: size of the hash table
84 * Return: the hash index of the backbone gateway
86 static inline u32 batadv_choose_backbone_gw(const void *data, u32 size)
88 const struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
91 hash = jhash(&claim->addr, sizeof(claim->addr), hash);
92 hash = jhash(&claim->vid, sizeof(claim->vid), hash);
98 * batadv_compare_backbone_gw - compare address and vid of two backbone gws
99 * @node: list node of the first entry to compare
100 * @data2: pointer to the second backbone gateway
102 * Return: 1 if the backbones have the same data, 0 otherwise
104 static int batadv_compare_backbone_gw(const struct hlist_node *node,
107 const void *data1 = container_of(node, struct batadv_bla_backbone_gw,
109 const struct batadv_bla_backbone_gw *gw1 = data1;
110 const struct batadv_bla_backbone_gw *gw2 = data2;
112 if (!batadv_compare_eth(gw1->orig, gw2->orig))
115 if (gw1->vid != gw2->vid)
122 * batadv_compare_backbone_gw - compare address and vid of two claims
123 * @node: list node of the first entry to compare
124 * @data2: pointer to the second claims
126 * Return: 1 if the claim have the same data, 0 otherwise
128 static int batadv_compare_claim(const struct hlist_node *node,
131 const void *data1 = container_of(node, struct batadv_bla_claim,
133 const struct batadv_bla_claim *cl1 = data1;
134 const struct batadv_bla_claim *cl2 = data2;
136 if (!batadv_compare_eth(cl1->addr, cl2->addr))
139 if (cl1->vid != cl2->vid)
146 * batadv_compare_backbone_gw - free backbone gw
147 * @backbone_gw: backbone gateway to be free'd
150 batadv_backbone_gw_free_ref(struct batadv_bla_backbone_gw *backbone_gw)
152 if (atomic_dec_and_test(&backbone_gw->refcount))
153 kfree_rcu(backbone_gw, rcu);
157 * batadv_claim_release - release claim from lists and queue for free after rcu
159 * @ref: kref pointer of the claim
161 static void batadv_claim_release(struct batadv_bla_claim *claim)
163 batadv_backbone_gw_free_ref(claim->backbone_gw);
164 kfree_rcu(claim, rcu);
168 * batadv_claim_free_ref - decrement the claim refcounter and possibly
170 * @claim: claim to be free'd
172 static void batadv_claim_free_ref(struct batadv_bla_claim *claim)
174 if (atomic_dec_and_test(&claim->refcount))
175 batadv_claim_release(claim);
179 * batadv_claim_hash_find - looks for a claim in the claim hash
180 * @bat_priv: the bat priv with all the soft interface information
181 * @data: search data (may be local/static data)
183 * Return: claim if found or NULL otherwise.
185 static struct batadv_bla_claim
186 *batadv_claim_hash_find(struct batadv_priv *bat_priv,
187 struct batadv_bla_claim *data)
189 struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
190 struct hlist_head *head;
191 struct batadv_bla_claim *claim;
192 struct batadv_bla_claim *claim_tmp = NULL;
198 index = batadv_choose_claim(data, hash->size);
199 head = &hash->table[index];
202 hlist_for_each_entry_rcu(claim, head, hash_entry) {
203 if (!batadv_compare_claim(&claim->hash_entry, data))
206 if (!atomic_inc_not_zero(&claim->refcount))
218 * batadv_backbone_hash_find - looks for a backbone gateway in the hash
219 * @bat_priv: the bat priv with all the soft interface information
220 * @addr: the address of the originator
223 * Return: backbone gateway if found or NULL otherwise
225 static struct batadv_bla_backbone_gw *
226 batadv_backbone_hash_find(struct batadv_priv *bat_priv, u8 *addr,
229 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
230 struct hlist_head *head;
231 struct batadv_bla_backbone_gw search_entry, *backbone_gw;
232 struct batadv_bla_backbone_gw *backbone_gw_tmp = NULL;
238 ether_addr_copy(search_entry.orig, addr);
239 search_entry.vid = vid;
241 index = batadv_choose_backbone_gw(&search_entry, hash->size);
242 head = &hash->table[index];
245 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
246 if (!batadv_compare_backbone_gw(&backbone_gw->hash_entry,
250 if (!atomic_inc_not_zero(&backbone_gw->refcount))
253 backbone_gw_tmp = backbone_gw;
258 return backbone_gw_tmp;
262 * batadv_bla_del_backbone_claims - delete all claims for a backbone
263 * @backbone_gw: backbone gateway where the claims should be removed
266 batadv_bla_del_backbone_claims(struct batadv_bla_backbone_gw *backbone_gw)
268 struct batadv_hashtable *hash;
269 struct hlist_node *node_tmp;
270 struct hlist_head *head;
271 struct batadv_bla_claim *claim;
273 spinlock_t *list_lock; /* protects write access to the hash lists */
275 hash = backbone_gw->bat_priv->bla.claim_hash;
279 for (i = 0; i < hash->size; i++) {
280 head = &hash->table[i];
281 list_lock = &hash->list_locks[i];
283 spin_lock_bh(list_lock);
284 hlist_for_each_entry_safe(claim, node_tmp,
286 if (claim->backbone_gw != backbone_gw)
289 batadv_claim_free_ref(claim);
290 hlist_del_rcu(&claim->hash_entry);
292 spin_unlock_bh(list_lock);
295 /* all claims gone, initialize CRC */
296 spin_lock_bh(&backbone_gw->crc_lock);
297 backbone_gw->crc = BATADV_BLA_CRC_INIT;
298 spin_unlock_bh(&backbone_gw->crc_lock);
302 * batadv_bla_send_claim - sends a claim frame according to the provided info
303 * @bat_priv: the bat priv with all the soft interface information
304 * @mac: the mac address to be announced within the claim
306 * @claimtype: the type of the claim (CLAIM, UNCLAIM, ANNOUNCE, ...)
308 static void batadv_bla_send_claim(struct batadv_priv *bat_priv, u8 *mac,
309 unsigned short vid, int claimtype)
312 struct ethhdr *ethhdr;
313 struct batadv_hard_iface *primary_if;
314 struct net_device *soft_iface;
316 struct batadv_bla_claim_dst local_claim_dest;
319 primary_if = batadv_primary_if_get_selected(bat_priv);
323 memcpy(&local_claim_dest, &bat_priv->bla.claim_dest,
324 sizeof(local_claim_dest));
325 local_claim_dest.type = claimtype;
327 soft_iface = primary_if->soft_iface;
329 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
330 /* IP DST: 0.0.0.0 */
332 primary_if->soft_iface,
333 /* IP SRC: 0.0.0.0 */
335 /* Ethernet DST: Broadcast */
337 /* Ethernet SRC/HW SRC: originator mac */
338 primary_if->net_dev->dev_addr,
339 /* HW DST: FF:43:05:XX:YY:YY
340 * with XX = claim type
341 * and YY:YY = group id
343 (u8 *)&local_claim_dest);
348 ethhdr = (struct ethhdr *)skb->data;
349 hw_src = (u8 *)ethhdr + ETH_HLEN + sizeof(struct arphdr);
351 /* now we pretend that the client would have sent this ... */
353 case BATADV_CLAIM_TYPE_CLAIM:
354 /* normal claim frame
355 * set Ethernet SRC to the clients mac
357 ether_addr_copy(ethhdr->h_source, mac);
358 batadv_dbg(BATADV_DBG_BLA, bat_priv,
359 "bla_send_claim(): CLAIM %pM on vid %d\n", mac,
360 BATADV_PRINT_VID(vid));
362 case BATADV_CLAIM_TYPE_UNCLAIM:
364 * set HW SRC to the clients mac
366 ether_addr_copy(hw_src, mac);
367 batadv_dbg(BATADV_DBG_BLA, bat_priv,
368 "bla_send_claim(): UNCLAIM %pM on vid %d\n", mac,
369 BATADV_PRINT_VID(vid));
371 case BATADV_CLAIM_TYPE_ANNOUNCE:
372 /* announcement frame
373 * set HW SRC to the special mac containg the crc
375 ether_addr_copy(hw_src, mac);
376 batadv_dbg(BATADV_DBG_BLA, bat_priv,
377 "bla_send_claim(): ANNOUNCE of %pM on vid %d\n",
378 ethhdr->h_source, BATADV_PRINT_VID(vid));
380 case BATADV_CLAIM_TYPE_REQUEST:
382 * set HW SRC and header destination to the receiving backbone
385 ether_addr_copy(hw_src, mac);
386 ether_addr_copy(ethhdr->h_dest, mac);
387 batadv_dbg(BATADV_DBG_BLA, bat_priv,
388 "bla_send_claim(): REQUEST of %pM to %pM on vid %d\n",
389 ethhdr->h_source, ethhdr->h_dest,
390 BATADV_PRINT_VID(vid));
394 if (vid & BATADV_VLAN_HAS_TAG)
395 skb = vlan_insert_tag(skb, htons(ETH_P_8021Q),
396 vid & VLAN_VID_MASK);
398 skb_reset_mac_header(skb);
399 skb->protocol = eth_type_trans(skb, soft_iface);
400 batadv_inc_counter(bat_priv, BATADV_CNT_RX);
401 batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
402 skb->len + ETH_HLEN);
403 soft_iface->last_rx = jiffies;
408 batadv_hardif_free_ref(primary_if);
412 * batadv_bla_get_backbone_gw - finds or creates a backbone gateway
413 * @bat_priv: the bat priv with all the soft interface information
414 * @orig: the mac address of the originator
416 * @own_backbone: set if the requested backbone is local
418 * Return: the (possibly created) backbone gateway or NULL on error
420 static struct batadv_bla_backbone_gw *
421 batadv_bla_get_backbone_gw(struct batadv_priv *bat_priv, u8 *orig,
422 unsigned short vid, bool own_backbone)
424 struct batadv_bla_backbone_gw *entry;
425 struct batadv_orig_node *orig_node;
428 entry = batadv_backbone_hash_find(bat_priv, orig, vid);
433 batadv_dbg(BATADV_DBG_BLA, bat_priv,
434 "bla_get_backbone_gw(): not found (%pM, %d), creating new entry\n",
435 orig, BATADV_PRINT_VID(vid));
437 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
442 entry->lasttime = jiffies;
443 entry->crc = BATADV_BLA_CRC_INIT;
444 entry->bat_priv = bat_priv;
445 spin_lock_init(&entry->crc_lock);
446 atomic_set(&entry->request_sent, 0);
447 atomic_set(&entry->wait_periods, 0);
448 ether_addr_copy(entry->orig, orig);
450 /* one for the hash, one for returning */
451 atomic_set(&entry->refcount, 2);
453 hash_added = batadv_hash_add(bat_priv->bla.backbone_hash,
454 batadv_compare_backbone_gw,
455 batadv_choose_backbone_gw, entry,
458 if (unlikely(hash_added != 0)) {
459 /* hash failed, free the structure */
464 /* this is a gateway now, remove any TT entry on this VLAN */
465 orig_node = batadv_orig_hash_find(bat_priv, orig);
467 batadv_tt_global_del_orig(bat_priv, orig_node, vid,
468 "became a backbone gateway");
469 batadv_orig_node_free_ref(orig_node);
473 batadv_bla_send_announce(bat_priv, entry);
475 /* this will be decreased in the worker thread */
476 atomic_inc(&entry->request_sent);
477 atomic_set(&entry->wait_periods, BATADV_BLA_WAIT_PERIODS);
478 atomic_inc(&bat_priv->bla.num_requests);
485 * batadv_bla_update_own_backbone_gw - updates the own backbone gw for a VLAN
486 * @bat_priv: the bat priv with all the soft interface information
487 * @primary_if: the selected primary interface
488 * @vid: VLAN identifier
490 * update or add the own backbone gw to make sure we announce
491 * where we receive other backbone gws
494 batadv_bla_update_own_backbone_gw(struct batadv_priv *bat_priv,
495 struct batadv_hard_iface *primary_if,
498 struct batadv_bla_backbone_gw *backbone_gw;
500 backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
501 primary_if->net_dev->dev_addr,
503 if (unlikely(!backbone_gw))
506 backbone_gw->lasttime = jiffies;
507 batadv_backbone_gw_free_ref(backbone_gw);
511 * batadv_bla_answer_request - answer a bla request by sending own claims
512 * @bat_priv: the bat priv with all the soft interface information
513 * @primary_if: interface where the request came on
514 * @vid: the vid where the request came on
516 * Repeat all of our own claims, and finally send an ANNOUNCE frame
517 * to allow the requester another check if the CRC is correct now.
519 static void batadv_bla_answer_request(struct batadv_priv *bat_priv,
520 struct batadv_hard_iface *primary_if,
523 struct hlist_head *head;
524 struct batadv_hashtable *hash;
525 struct batadv_bla_claim *claim;
526 struct batadv_bla_backbone_gw *backbone_gw;
529 batadv_dbg(BATADV_DBG_BLA, bat_priv,
530 "bla_answer_request(): received a claim request, send all of our own claims again\n");
532 backbone_gw = batadv_backbone_hash_find(bat_priv,
533 primary_if->net_dev->dev_addr,
538 hash = bat_priv->bla.claim_hash;
539 for (i = 0; i < hash->size; i++) {
540 head = &hash->table[i];
543 hlist_for_each_entry_rcu(claim, head, hash_entry) {
544 /* only own claims are interesting */
545 if (claim->backbone_gw != backbone_gw)
548 batadv_bla_send_claim(bat_priv, claim->addr, claim->vid,
549 BATADV_CLAIM_TYPE_CLAIM);
554 /* finally, send an announcement frame */
555 batadv_bla_send_announce(bat_priv, backbone_gw);
556 batadv_backbone_gw_free_ref(backbone_gw);
560 * batadv_bla_send_request - send a request to repeat claims
561 * @backbone_gw: the backbone gateway from whom we are out of sync
563 * When the crc is wrong, ask the backbone gateway for a full table update.
564 * After the request, it will repeat all of his own claims and finally
565 * send an announcement claim with which we can check again.
567 static void batadv_bla_send_request(struct batadv_bla_backbone_gw *backbone_gw)
569 /* first, remove all old entries */
570 batadv_bla_del_backbone_claims(backbone_gw);
572 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
573 "Sending REQUEST to %pM\n", backbone_gw->orig);
576 batadv_bla_send_claim(backbone_gw->bat_priv, backbone_gw->orig,
577 backbone_gw->vid, BATADV_CLAIM_TYPE_REQUEST);
579 /* no local broadcasts should be sent or received, for now. */
580 if (!atomic_read(&backbone_gw->request_sent)) {
581 atomic_inc(&backbone_gw->bat_priv->bla.num_requests);
582 atomic_set(&backbone_gw->request_sent, 1);
587 * batadv_bla_send_announce - Send an announcement frame
588 * @bat_priv: the bat priv with all the soft interface information
589 * @backbone_gw: our backbone gateway which should be announced
591 static void batadv_bla_send_announce(struct batadv_priv *bat_priv,
592 struct batadv_bla_backbone_gw *backbone_gw)
597 memcpy(mac, batadv_announce_mac, 4);
598 spin_lock_bh(&backbone_gw->crc_lock);
599 crc = htons(backbone_gw->crc);
600 spin_unlock_bh(&backbone_gw->crc_lock);
601 memcpy(&mac[4], &crc, 2);
603 batadv_bla_send_claim(bat_priv, mac, backbone_gw->vid,
604 BATADV_CLAIM_TYPE_ANNOUNCE);
608 * batadv_bla_add_claim - Adds a claim in the claim hash
609 * @bat_priv: the bat priv with all the soft interface information
610 * @mac: the mac address of the claim
611 * @vid: the VLAN ID of the frame
612 * @backbone_gw: the backbone gateway which claims it
614 static void batadv_bla_add_claim(struct batadv_priv *bat_priv,
615 const u8 *mac, const unsigned short vid,
616 struct batadv_bla_backbone_gw *backbone_gw)
618 struct batadv_bla_claim *claim;
619 struct batadv_bla_claim search_claim;
622 ether_addr_copy(search_claim.addr, mac);
623 search_claim.vid = vid;
624 claim = batadv_claim_hash_find(bat_priv, &search_claim);
626 /* create a new claim entry if it does not exist yet. */
628 claim = kzalloc(sizeof(*claim), GFP_ATOMIC);
632 ether_addr_copy(claim->addr, mac);
634 claim->lasttime = jiffies;
635 claim->backbone_gw = backbone_gw;
637 atomic_set(&claim->refcount, 2);
638 batadv_dbg(BATADV_DBG_BLA, bat_priv,
639 "bla_add_claim(): adding new entry %pM, vid %d to hash ...\n",
640 mac, BATADV_PRINT_VID(vid));
641 hash_added = batadv_hash_add(bat_priv->bla.claim_hash,
642 batadv_compare_claim,
643 batadv_choose_claim, claim,
646 if (unlikely(hash_added != 0)) {
647 /* only local changes happened. */
652 claim->lasttime = jiffies;
653 if (claim->backbone_gw == backbone_gw)
654 /* no need to register a new backbone */
657 batadv_dbg(BATADV_DBG_BLA, bat_priv,
658 "bla_add_claim(): changing ownership for %pM, vid %d\n",
659 mac, BATADV_PRINT_VID(vid));
661 spin_lock_bh(&claim->backbone_gw->crc_lock);
662 claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
663 spin_unlock_bh(&claim->backbone_gw->crc_lock);
664 batadv_backbone_gw_free_ref(claim->backbone_gw);
666 /* set (new) backbone gw */
667 atomic_inc(&backbone_gw->refcount);
668 claim->backbone_gw = backbone_gw;
670 spin_lock_bh(&backbone_gw->crc_lock);
671 backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
672 spin_unlock_bh(&backbone_gw->crc_lock);
673 backbone_gw->lasttime = jiffies;
676 batadv_claim_free_ref(claim);
680 * batadv_bla_del_claim - delete a claim from the claim hash
681 * @bat_priv: the bat priv with all the soft interface information
682 * @mac: mac address of the claim to be removed
683 * @vid: VLAN id for the claim to be removed
685 static void batadv_bla_del_claim(struct batadv_priv *bat_priv,
686 const u8 *mac, const unsigned short vid)
688 struct batadv_bla_claim search_claim, *claim;
690 ether_addr_copy(search_claim.addr, mac);
691 search_claim.vid = vid;
692 claim = batadv_claim_hash_find(bat_priv, &search_claim);
696 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla_del_claim(): %pM, vid %d\n",
697 mac, BATADV_PRINT_VID(vid));
699 batadv_hash_remove(bat_priv->bla.claim_hash, batadv_compare_claim,
700 batadv_choose_claim, claim);
701 batadv_claim_free_ref(claim); /* reference from the hash is gone */
703 spin_lock_bh(&claim->backbone_gw->crc_lock);
704 claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
705 spin_unlock_bh(&claim->backbone_gw->crc_lock);
707 /* don't need the reference from hash_find() anymore */
708 batadv_claim_free_ref(claim);
712 * batadv_handle_announce - check for ANNOUNCE frame
713 * @bat_priv: the bat priv with all the soft interface information
714 * @an_addr: announcement mac address (ARP Sender HW address)
715 * @backbone_addr: originator address of the sender (Ethernet source MAC)
716 * @vid: the VLAN ID of the frame
718 * Return: 1 if handled
720 static int batadv_handle_announce(struct batadv_priv *bat_priv, u8 *an_addr,
721 u8 *backbone_addr, unsigned short vid)
723 struct batadv_bla_backbone_gw *backbone_gw;
724 u16 backbone_crc, crc;
726 if (memcmp(an_addr, batadv_announce_mac, 4) != 0)
729 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
732 if (unlikely(!backbone_gw))
735 /* handle as ANNOUNCE frame */
736 backbone_gw->lasttime = jiffies;
737 crc = ntohs(*((__be16 *)(&an_addr[4])));
739 batadv_dbg(BATADV_DBG_BLA, bat_priv,
740 "handle_announce(): ANNOUNCE vid %d (sent by %pM)... CRC = %#.4x\n",
741 BATADV_PRINT_VID(vid), backbone_gw->orig, crc);
743 spin_lock_bh(&backbone_gw->crc_lock);
744 backbone_crc = backbone_gw->crc;
745 spin_unlock_bh(&backbone_gw->crc_lock);
747 if (backbone_crc != crc) {
748 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
749 "handle_announce(): CRC FAILED for %pM/%d (my = %#.4x, sent = %#.4x)\n",
751 BATADV_PRINT_VID(backbone_gw->vid),
754 batadv_bla_send_request(backbone_gw);
756 /* if we have sent a request and the crc was OK,
757 * we can allow traffic again.
759 if (atomic_read(&backbone_gw->request_sent)) {
760 atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
761 atomic_set(&backbone_gw->request_sent, 0);
765 batadv_backbone_gw_free_ref(backbone_gw);
770 * batadv_handle_request - check for REQUEST frame
771 * @bat_priv: the bat priv with all the soft interface information
772 * @primary_if: the primary hard interface of this batman soft interface
773 * @backbone_addr: backbone address to be requested (ARP sender HW MAC)
774 * @ethhdr: ethernet header of a packet
775 * @vid: the VLAN ID of the frame
777 * Return: 1 if handled
779 static int batadv_handle_request(struct batadv_priv *bat_priv,
780 struct batadv_hard_iface *primary_if,
781 u8 *backbone_addr, struct ethhdr *ethhdr,
784 /* check for REQUEST frame */
785 if (!batadv_compare_eth(backbone_addr, ethhdr->h_dest))
788 /* sanity check, this should not happen on a normal switch,
789 * we ignore it in this case.
791 if (!batadv_compare_eth(ethhdr->h_dest, primary_if->net_dev->dev_addr))
794 batadv_dbg(BATADV_DBG_BLA, bat_priv,
795 "handle_request(): REQUEST vid %d (sent by %pM)...\n",
796 BATADV_PRINT_VID(vid), ethhdr->h_source);
798 batadv_bla_answer_request(bat_priv, primary_if, vid);
803 * batadv_handle_unclaim - check for UNCLAIM frame
804 * @bat_priv: the bat priv with all the soft interface information
805 * @primary_if: the primary hard interface of this batman soft interface
806 * @backbone_addr: originator address of the backbone (Ethernet source)
807 * @claim_addr: Client to be unclaimed (ARP sender HW MAC)
808 * @vid: the VLAN ID of the frame
810 * Return: 1 if handled
812 static int batadv_handle_unclaim(struct batadv_priv *bat_priv,
813 struct batadv_hard_iface *primary_if,
814 u8 *backbone_addr, u8 *claim_addr,
817 struct batadv_bla_backbone_gw *backbone_gw;
819 /* unclaim in any case if it is our own */
820 if (primary_if && batadv_compare_eth(backbone_addr,
821 primary_if->net_dev->dev_addr))
822 batadv_bla_send_claim(bat_priv, claim_addr, vid,
823 BATADV_CLAIM_TYPE_UNCLAIM);
825 backbone_gw = batadv_backbone_hash_find(bat_priv, backbone_addr, vid);
830 /* this must be an UNCLAIM frame */
831 batadv_dbg(BATADV_DBG_BLA, bat_priv,
832 "handle_unclaim(): UNCLAIM %pM on vid %d (sent by %pM)...\n",
833 claim_addr, BATADV_PRINT_VID(vid), backbone_gw->orig);
835 batadv_bla_del_claim(bat_priv, claim_addr, vid);
836 batadv_backbone_gw_free_ref(backbone_gw);
841 * batadv_handle_claim - check for CLAIM frame
842 * @bat_priv: the bat priv with all the soft interface information
843 * @primary_if: the primary hard interface of this batman soft interface
844 * @backbone_addr: originator address of the backbone (Ethernet Source)
845 * @claim_addr: client mac address to be claimed (ARP sender HW MAC)
846 * @vid: the VLAN ID of the frame
848 * Return: 1 if handled
850 static int batadv_handle_claim(struct batadv_priv *bat_priv,
851 struct batadv_hard_iface *primary_if,
852 u8 *backbone_addr, u8 *claim_addr,
855 struct batadv_bla_backbone_gw *backbone_gw;
857 /* register the gateway if not yet available, and add the claim. */
859 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
862 if (unlikely(!backbone_gw))
865 /* this must be a CLAIM frame */
866 batadv_bla_add_claim(bat_priv, claim_addr, vid, backbone_gw);
867 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
868 batadv_bla_send_claim(bat_priv, claim_addr, vid,
869 BATADV_CLAIM_TYPE_CLAIM);
871 /* TODO: we could call something like tt_local_del() here. */
873 batadv_backbone_gw_free_ref(backbone_gw);
878 * batadv_check_claim_group - check for claim group membership
879 * @bat_priv: the bat priv with all the soft interface information
880 * @primary_if: the primary interface of this batman interface
881 * @hw_src: the Hardware source in the ARP Header
882 * @hw_dst: the Hardware destination in the ARP Header
883 * @ethhdr: pointer to the Ethernet header of the claim frame
885 * checks if it is a claim packet and if its on the same group.
886 * This function also applies the group ID of the sender
887 * if it is in the same mesh.
890 * 2 - if it is a claim packet and on the same group
891 * 1 - if is a claim packet from another group
892 * 0 - if it is not a claim packet
894 static int batadv_check_claim_group(struct batadv_priv *bat_priv,
895 struct batadv_hard_iface *primary_if,
896 u8 *hw_src, u8 *hw_dst,
897 struct ethhdr *ethhdr)
900 struct batadv_orig_node *orig_node;
901 struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
903 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
904 bla_dst_own = &bat_priv->bla.claim_dest;
906 /* if announcement packet, use the source,
907 * otherwise assume it is in the hw_src
909 switch (bla_dst->type) {
910 case BATADV_CLAIM_TYPE_CLAIM:
911 backbone_addr = hw_src;
913 case BATADV_CLAIM_TYPE_REQUEST:
914 case BATADV_CLAIM_TYPE_ANNOUNCE:
915 case BATADV_CLAIM_TYPE_UNCLAIM:
916 backbone_addr = ethhdr->h_source;
922 /* don't accept claim frames from ourselves */
923 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
926 /* if its already the same group, it is fine. */
927 if (bla_dst->group == bla_dst_own->group)
930 /* lets see if this originator is in our mesh */
931 orig_node = batadv_orig_hash_find(bat_priv, backbone_addr);
933 /* dont accept claims from gateways which are not in
934 * the same mesh or group.
939 /* if our mesh friends mac is bigger, use it for ourselves. */
940 if (ntohs(bla_dst->group) > ntohs(bla_dst_own->group)) {
941 batadv_dbg(BATADV_DBG_BLA, bat_priv,
942 "taking other backbones claim group: %#.4x\n",
943 ntohs(bla_dst->group));
944 bla_dst_own->group = bla_dst->group;
947 batadv_orig_node_free_ref(orig_node);
953 * batadv_bla_process_claim - Check if this is a claim frame, and process it
954 * @bat_priv: the bat priv with all the soft interface information
955 * @primary_if: the primary hard interface of this batman soft interface
956 * @skb: the frame to be checked
958 * Return: 1 if it was a claim frame, otherwise return 0 to
959 * tell the callee that it can use the frame on its own.
961 static int batadv_bla_process_claim(struct batadv_priv *bat_priv,
962 struct batadv_hard_iface *primary_if,
965 struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
967 struct vlan_hdr *vhdr, vhdr_buf;
968 struct ethhdr *ethhdr;
969 struct arphdr *arphdr;
976 vid = batadv_get_vid(skb, 0);
977 ethhdr = eth_hdr(skb);
979 proto = ethhdr->h_proto;
981 if (vid & BATADV_VLAN_HAS_TAG) {
982 /* Traverse the VLAN/Ethertypes.
984 * At this point it is known that the first protocol is a VLAN
985 * header, so start checking at the encapsulated protocol.
987 * The depth of the VLAN headers is recorded to drop BLA claim
988 * frames encapsulated into multiple VLAN headers (QinQ).
991 vhdr = skb_header_pointer(skb, headlen, VLAN_HLEN,
996 proto = vhdr->h_vlan_encapsulated_proto;
997 headlen += VLAN_HLEN;
999 } while (proto == htons(ETH_P_8021Q));
1002 if (proto != htons(ETH_P_ARP))
1003 return 0; /* not a claim frame */
1005 /* this must be a ARP frame. check if it is a claim. */
1007 if (unlikely(!pskb_may_pull(skb, headlen + arp_hdr_len(skb->dev))))
1010 /* pskb_may_pull() may have modified the pointers, get ethhdr again */
1011 ethhdr = eth_hdr(skb);
1012 arphdr = (struct arphdr *)((u8 *)ethhdr + headlen);
1014 /* Check whether the ARP frame carries a valid
1017 if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
1019 if (arphdr->ar_pro != htons(ETH_P_IP))
1021 if (arphdr->ar_hln != ETH_ALEN)
1023 if (arphdr->ar_pln != 4)
1026 hw_src = (u8 *)arphdr + sizeof(struct arphdr);
1027 hw_dst = hw_src + ETH_ALEN + 4;
1028 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
1029 bla_dst_own = &bat_priv->bla.claim_dest;
1031 /* check if it is a claim frame in general */
1032 if (memcmp(bla_dst->magic, bla_dst_own->magic,
1033 sizeof(bla_dst->magic)) != 0)
1036 /* check if there is a claim frame encapsulated deeper in (QinQ) and
1037 * drop that, as this is not supported by BLA but should also not be
1038 * sent via the mesh.
1043 /* check if it is a claim frame. */
1044 ret = batadv_check_claim_group(bat_priv, primary_if, hw_src, hw_dst,
1047 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1048 "bla_process_claim(): received a claim frame from another group. From: %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n",
1049 ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src,
1055 /* become a backbone gw ourselves on this vlan if not happened yet */
1056 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
1058 /* check for the different types of claim frames ... */
1059 switch (bla_dst->type) {
1060 case BATADV_CLAIM_TYPE_CLAIM:
1061 if (batadv_handle_claim(bat_priv, primary_if, hw_src,
1062 ethhdr->h_source, vid))
1065 case BATADV_CLAIM_TYPE_UNCLAIM:
1066 if (batadv_handle_unclaim(bat_priv, primary_if,
1067 ethhdr->h_source, hw_src, vid))
1071 case BATADV_CLAIM_TYPE_ANNOUNCE:
1072 if (batadv_handle_announce(bat_priv, hw_src, ethhdr->h_source,
1076 case BATADV_CLAIM_TYPE_REQUEST:
1077 if (batadv_handle_request(bat_priv, primary_if, hw_src, ethhdr,
1083 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1084 "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",
1085 ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src, hw_dst);
1090 * batadv_bla_purge_backbone_gw - Remove backbone gateways after a timeout or
1092 * @bat_priv: the bat priv with all the soft interface information
1093 * @now: whether the whole hash shall be wiped now
1095 * Check when we last heard from other nodes, and remove them in case of
1096 * a time out, or clean all backbone gws if now is set.
1098 static void batadv_bla_purge_backbone_gw(struct batadv_priv *bat_priv, int now)
1100 struct batadv_bla_backbone_gw *backbone_gw;
1101 struct hlist_node *node_tmp;
1102 struct hlist_head *head;
1103 struct batadv_hashtable *hash;
1104 spinlock_t *list_lock; /* protects write access to the hash lists */
1107 hash = bat_priv->bla.backbone_hash;
1111 for (i = 0; i < hash->size; i++) {
1112 head = &hash->table[i];
1113 list_lock = &hash->list_locks[i];
1115 spin_lock_bh(list_lock);
1116 hlist_for_each_entry_safe(backbone_gw, node_tmp,
1120 if (!batadv_has_timed_out(backbone_gw->lasttime,
1121 BATADV_BLA_BACKBONE_TIMEOUT))
1124 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
1125 "bla_purge_backbone_gw(): backbone gw %pM timed out\n",
1129 /* don't wait for the pending request anymore */
1130 if (atomic_read(&backbone_gw->request_sent))
1131 atomic_dec(&bat_priv->bla.num_requests);
1133 batadv_bla_del_backbone_claims(backbone_gw);
1135 hlist_del_rcu(&backbone_gw->hash_entry);
1136 batadv_backbone_gw_free_ref(backbone_gw);
1138 spin_unlock_bh(list_lock);
1143 * batadv_bla_purge_claims - Remove claims after a timeout or immediately
1144 * @bat_priv: the bat priv with all the soft interface information
1145 * @primary_if: the selected primary interface, may be NULL if now is set
1146 * @now: whether the whole hash shall be wiped now
1148 * Check when we heard last time from our own claims, and remove them in case of
1149 * a time out, or clean all claims if now is set
1151 static void batadv_bla_purge_claims(struct batadv_priv *bat_priv,
1152 struct batadv_hard_iface *primary_if,
1155 struct batadv_bla_claim *claim;
1156 struct hlist_head *head;
1157 struct batadv_hashtable *hash;
1160 hash = bat_priv->bla.claim_hash;
1164 for (i = 0; i < hash->size; i++) {
1165 head = &hash->table[i];
1168 hlist_for_each_entry_rcu(claim, head, hash_entry) {
1171 if (!batadv_compare_eth(claim->backbone_gw->orig,
1172 primary_if->net_dev->dev_addr))
1174 if (!batadv_has_timed_out(claim->lasttime,
1175 BATADV_BLA_CLAIM_TIMEOUT))
1178 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1179 "bla_purge_claims(): %pM, vid %d, time out\n",
1180 claim->addr, claim->vid);
1183 batadv_handle_unclaim(bat_priv, primary_if,
1184 claim->backbone_gw->orig,
1185 claim->addr, claim->vid);
1192 * batadv_bla_update_orig_address - Update the backbone gateways when the own
1193 * originator address changes
1194 * @bat_priv: the bat priv with all the soft interface information
1195 * @primary_if: the new selected primary_if
1196 * @oldif: the old primary interface, may be NULL
1198 void batadv_bla_update_orig_address(struct batadv_priv *bat_priv,
1199 struct batadv_hard_iface *primary_if,
1200 struct batadv_hard_iface *oldif)
1202 struct batadv_bla_backbone_gw *backbone_gw;
1203 struct hlist_head *head;
1204 struct batadv_hashtable *hash;
1208 /* reset bridge loop avoidance group id */
1209 group = htons(crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN));
1210 bat_priv->bla.claim_dest.group = group;
1212 /* purge everything when bridge loop avoidance is turned off */
1213 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1217 batadv_bla_purge_claims(bat_priv, NULL, 1);
1218 batadv_bla_purge_backbone_gw(bat_priv, 1);
1222 hash = bat_priv->bla.backbone_hash;
1226 for (i = 0; i < hash->size; i++) {
1227 head = &hash->table[i];
1230 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
1231 /* own orig still holds the old value. */
1232 if (!batadv_compare_eth(backbone_gw->orig,
1233 oldif->net_dev->dev_addr))
1236 ether_addr_copy(backbone_gw->orig,
1237 primary_if->net_dev->dev_addr);
1238 /* send an announce frame so others will ask for our
1239 * claims and update their tables.
1241 batadv_bla_send_announce(bat_priv, backbone_gw);
1248 * batadv_bla_status_update - purge bla interfaces if necessary
1249 * @net_dev: the soft interface net device
1251 void batadv_bla_status_update(struct net_device *net_dev)
1253 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1254 struct batadv_hard_iface *primary_if;
1256 primary_if = batadv_primary_if_get_selected(bat_priv);
1260 /* this function already purges everything when bla is disabled,
1261 * so just call that one.
1263 batadv_bla_update_orig_address(bat_priv, primary_if, primary_if);
1264 batadv_hardif_free_ref(primary_if);
1268 * batadv_bla_periodic_work - performs periodic bla work
1269 * @work: kernel work struct
1271 * periodic work to do:
1272 * * purge structures when they are too old
1273 * * send announcements
1275 static void batadv_bla_periodic_work(struct work_struct *work)
1277 struct delayed_work *delayed_work;
1278 struct batadv_priv *bat_priv;
1279 struct batadv_priv_bla *priv_bla;
1280 struct hlist_head *head;
1281 struct batadv_bla_backbone_gw *backbone_gw;
1282 struct batadv_hashtable *hash;
1283 struct batadv_hard_iface *primary_if;
1286 delayed_work = container_of(work, struct delayed_work, work);
1287 priv_bla = container_of(delayed_work, struct batadv_priv_bla, work);
1288 bat_priv = container_of(priv_bla, struct batadv_priv, bla);
1289 primary_if = batadv_primary_if_get_selected(bat_priv);
1293 batadv_bla_purge_claims(bat_priv, primary_if, 0);
1294 batadv_bla_purge_backbone_gw(bat_priv, 0);
1296 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1299 hash = bat_priv->bla.backbone_hash;
1303 for (i = 0; i < hash->size; i++) {
1304 head = &hash->table[i];
1307 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
1308 if (!batadv_compare_eth(backbone_gw->orig,
1309 primary_if->net_dev->dev_addr))
1312 backbone_gw->lasttime = jiffies;
1314 batadv_bla_send_announce(bat_priv, backbone_gw);
1316 /* request_sent is only set after creation to avoid
1317 * problems when we are not yet known as backbone gw
1320 * We can reset this now after we waited some periods
1321 * to give bridge forward delays and bla group forming
1325 if (atomic_read(&backbone_gw->request_sent) == 0)
1328 if (!atomic_dec_and_test(&backbone_gw->wait_periods))
1331 atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
1332 atomic_set(&backbone_gw->request_sent, 0);
1338 batadv_hardif_free_ref(primary_if);
1340 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1341 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
1344 /* The hash for claim and backbone hash receive the same key because they
1345 * are getting initialized by hash_new with the same key. Reinitializing
1346 * them with to different keys to allow nested locking without generating
1349 static struct lock_class_key batadv_claim_hash_lock_class_key;
1350 static struct lock_class_key batadv_backbone_hash_lock_class_key;
1353 * batadv_bla_init - initialize all bla structures
1354 * @bat_priv: the bat priv with all the soft interface information
1356 * Return: 0 on success, < 0 on error.
1358 int batadv_bla_init(struct batadv_priv *bat_priv)
1361 u8 claim_dest[ETH_ALEN] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00};
1362 struct batadv_hard_iface *primary_if;
1364 unsigned long entrytime;
1366 spin_lock_init(&bat_priv->bla.bcast_duplist_lock);
1368 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hash registering\n");
1370 /* setting claim destination address */
1371 memcpy(&bat_priv->bla.claim_dest.magic, claim_dest, 3);
1372 bat_priv->bla.claim_dest.type = 0;
1373 primary_if = batadv_primary_if_get_selected(bat_priv);
1375 crc = crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN);
1376 bat_priv->bla.claim_dest.group = htons(crc);
1377 batadv_hardif_free_ref(primary_if);
1379 bat_priv->bla.claim_dest.group = 0; /* will be set later */
1382 /* initialize the duplicate list */
1383 entrytime = jiffies - msecs_to_jiffies(BATADV_DUPLIST_TIMEOUT);
1384 for (i = 0; i < BATADV_DUPLIST_SIZE; i++)
1385 bat_priv->bla.bcast_duplist[i].entrytime = entrytime;
1386 bat_priv->bla.bcast_duplist_curr = 0;
1388 if (bat_priv->bla.claim_hash)
1391 bat_priv->bla.claim_hash = batadv_hash_new(128);
1392 bat_priv->bla.backbone_hash = batadv_hash_new(32);
1394 if (!bat_priv->bla.claim_hash || !bat_priv->bla.backbone_hash)
1397 batadv_hash_set_lock_class(bat_priv->bla.claim_hash,
1398 &batadv_claim_hash_lock_class_key);
1399 batadv_hash_set_lock_class(bat_priv->bla.backbone_hash,
1400 &batadv_backbone_hash_lock_class_key);
1402 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hashes initialized\n");
1404 INIT_DELAYED_WORK(&bat_priv->bla.work, batadv_bla_periodic_work);
1406 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1407 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
1412 * batadv_bla_check_bcast_duplist - Check if a frame is in the broadcast dup.
1413 * @bat_priv: the bat priv with all the soft interface information
1414 * @skb: contains the bcast_packet to be checked
1416 * check if it is on our broadcast list. Another gateway might
1417 * have sent the same packet because it is connected to the same backbone,
1418 * so we have to remove this duplicate.
1420 * This is performed by checking the CRC, which will tell us
1421 * with a good chance that it is the same packet. If it is furthermore
1422 * sent by another host, drop it. We allow equal packets from
1423 * the same host however as this might be intended.
1425 * Return: 1 if a packet is in the duplicate list, 0 otherwise.
1427 int batadv_bla_check_bcast_duplist(struct batadv_priv *bat_priv,
1428 struct sk_buff *skb)
1430 int i, curr, ret = 0;
1432 struct batadv_bcast_packet *bcast_packet;
1433 struct batadv_bcast_duplist_entry *entry;
1435 bcast_packet = (struct batadv_bcast_packet *)skb->data;
1437 /* calculate the crc ... */
1438 crc = batadv_skb_crc32(skb, (u8 *)(bcast_packet + 1));
1440 spin_lock_bh(&bat_priv->bla.bcast_duplist_lock);
1442 for (i = 0; i < BATADV_DUPLIST_SIZE; i++) {
1443 curr = (bat_priv->bla.bcast_duplist_curr + i);
1444 curr %= BATADV_DUPLIST_SIZE;
1445 entry = &bat_priv->bla.bcast_duplist[curr];
1447 /* we can stop searching if the entry is too old ;
1448 * later entries will be even older
1450 if (batadv_has_timed_out(entry->entrytime,
1451 BATADV_DUPLIST_TIMEOUT))
1454 if (entry->crc != crc)
1457 if (batadv_compare_eth(entry->orig, bcast_packet->orig))
1460 /* this entry seems to match: same crc, not too old,
1461 * and from another gw. therefore return 1 to forbid it.
1466 /* not found, add a new entry (overwrite the oldest entry)
1467 * and allow it, its the first occurrence.
1469 curr = (bat_priv->bla.bcast_duplist_curr + BATADV_DUPLIST_SIZE - 1);
1470 curr %= BATADV_DUPLIST_SIZE;
1471 entry = &bat_priv->bla.bcast_duplist[curr];
1473 entry->entrytime = jiffies;
1474 ether_addr_copy(entry->orig, bcast_packet->orig);
1475 bat_priv->bla.bcast_duplist_curr = curr;
1478 spin_unlock_bh(&bat_priv->bla.bcast_duplist_lock);
1484 * batadv_bla_is_backbone_gw_orig - Check if the originator is a gateway for
1485 * the VLAN identified by vid.
1486 * @bat_priv: the bat priv with all the soft interface information
1487 * @orig: originator mac address
1488 * @vid: VLAN identifier
1490 * Return: true if orig is a backbone for this vid, false otherwise.
1492 bool batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, u8 *orig,
1495 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
1496 struct hlist_head *head;
1497 struct batadv_bla_backbone_gw *backbone_gw;
1500 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1506 for (i = 0; i < hash->size; i++) {
1507 head = &hash->table[i];
1510 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
1511 if (batadv_compare_eth(backbone_gw->orig, orig) &&
1512 backbone_gw->vid == vid) {
1524 * batadv_bla_is_backbone_gw - check if originator is a backbone gw for a VLAN.
1525 * @skb: the frame to be checked
1526 * @orig_node: the orig_node of the frame
1527 * @hdr_size: maximum length of the frame
1529 * Return: 1 if the orig_node is also a gateway on the soft interface, otherwise
1532 int batadv_bla_is_backbone_gw(struct sk_buff *skb,
1533 struct batadv_orig_node *orig_node, int hdr_size)
1535 struct batadv_bla_backbone_gw *backbone_gw;
1538 if (!atomic_read(&orig_node->bat_priv->bridge_loop_avoidance))
1541 /* first, find out the vid. */
1542 if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
1545 vid = batadv_get_vid(skb, hdr_size);
1547 /* see if this originator is a backbone gw for this VLAN */
1548 backbone_gw = batadv_backbone_hash_find(orig_node->bat_priv,
1549 orig_node->orig, vid);
1553 batadv_backbone_gw_free_ref(backbone_gw);
1558 * batadv_bla_init - free all bla structures
1559 * @bat_priv: the bat priv with all the soft interface information
1561 * for softinterface free or module unload
1563 void batadv_bla_free(struct batadv_priv *bat_priv)
1565 struct batadv_hard_iface *primary_if;
1567 cancel_delayed_work_sync(&bat_priv->bla.work);
1568 primary_if = batadv_primary_if_get_selected(bat_priv);
1570 if (bat_priv->bla.claim_hash) {
1571 batadv_bla_purge_claims(bat_priv, primary_if, 1);
1572 batadv_hash_destroy(bat_priv->bla.claim_hash);
1573 bat_priv->bla.claim_hash = NULL;
1575 if (bat_priv->bla.backbone_hash) {
1576 batadv_bla_purge_backbone_gw(bat_priv, 1);
1577 batadv_hash_destroy(bat_priv->bla.backbone_hash);
1578 bat_priv->bla.backbone_hash = NULL;
1581 batadv_hardif_free_ref(primary_if);
1585 * batadv_bla_rx - check packets coming from the mesh.
1586 * @bat_priv: the bat priv with all the soft interface information
1587 * @skb: the frame to be checked
1588 * @vid: the VLAN ID of the frame
1589 * @is_bcast: the packet came in a broadcast packet type.
1591 * batadv_bla_rx avoidance checks if:
1592 * * we have to race for a claim
1593 * * if the frame is allowed on the LAN
1595 * in these cases, the skb is further handled by this function
1597 * Return: 1 if handled, otherwise it returns 0 and the caller shall further
1600 int batadv_bla_rx(struct batadv_priv *bat_priv, struct sk_buff *skb,
1601 unsigned short vid, bool is_bcast)
1603 struct ethhdr *ethhdr;
1604 struct batadv_bla_claim search_claim, *claim = NULL;
1605 struct batadv_hard_iface *primary_if;
1608 ethhdr = eth_hdr(skb);
1610 primary_if = batadv_primary_if_get_selected(bat_priv);
1614 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1617 if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
1618 /* don't allow broadcasts while requests are in flight */
1619 if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast)
1622 ether_addr_copy(search_claim.addr, ethhdr->h_source);
1623 search_claim.vid = vid;
1624 claim = batadv_claim_hash_find(bat_priv, &search_claim);
1627 /* possible optimization: race for a claim */
1628 /* No claim exists yet, claim it for us!
1630 batadv_handle_claim(bat_priv, primary_if,
1631 primary_if->net_dev->dev_addr,
1632 ethhdr->h_source, vid);
1636 /* if it is our own claim ... */
1637 if (batadv_compare_eth(claim->backbone_gw->orig,
1638 primary_if->net_dev->dev_addr)) {
1639 /* ... allow it in any case */
1640 claim->lasttime = jiffies;
1644 /* if it is a broadcast ... */
1645 if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast) {
1646 /* ... drop it. the responsible gateway is in charge.
1648 * We need to check is_bcast because with the gateway
1649 * feature, broadcasts (like DHCP requests) may be sent
1650 * using a unicast packet type.
1654 /* seems the client considers us as its best gateway.
1655 * send a claim and update the claim table
1658 batadv_handle_claim(bat_priv, primary_if,
1659 primary_if->net_dev->dev_addr,
1660 ethhdr->h_source, vid);
1664 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
1674 batadv_hardif_free_ref(primary_if);
1676 batadv_claim_free_ref(claim);
1681 * batadv_bla_tx - check packets going into the mesh
1682 * @bat_priv: the bat priv with all the soft interface information
1683 * @skb: the frame to be checked
1684 * @vid: the VLAN ID of the frame
1686 * batadv_bla_tx checks if:
1687 * * a claim was received which has to be processed
1688 * * the frame is allowed on the mesh
1690 * in these cases, the skb is further handled by this function.
1692 * This call might reallocate skb data.
1694 * Return: 1 if handled, otherwise it returns 0 and the caller shall further
1697 int batadv_bla_tx(struct batadv_priv *bat_priv, struct sk_buff *skb,
1700 struct ethhdr *ethhdr;
1701 struct batadv_bla_claim search_claim, *claim = NULL;
1702 struct batadv_hard_iface *primary_if;
1705 primary_if = batadv_primary_if_get_selected(bat_priv);
1709 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1712 if (batadv_bla_process_claim(bat_priv, primary_if, skb))
1715 ethhdr = eth_hdr(skb);
1717 if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
1718 /* don't allow broadcasts while requests are in flight */
1719 if (is_multicast_ether_addr(ethhdr->h_dest))
1722 ether_addr_copy(search_claim.addr, ethhdr->h_source);
1723 search_claim.vid = vid;
1725 claim = batadv_claim_hash_find(bat_priv, &search_claim);
1727 /* if no claim exists, allow it. */
1731 /* check if we are responsible. */
1732 if (batadv_compare_eth(claim->backbone_gw->orig,
1733 primary_if->net_dev->dev_addr)) {
1734 /* if yes, the client has roamed and we have
1737 batadv_handle_unclaim(bat_priv, primary_if,
1738 primary_if->net_dev->dev_addr,
1739 ethhdr->h_source, vid);
1743 /* check if it is a multicast/broadcast frame */
1744 if (is_multicast_ether_addr(ethhdr->h_dest)) {
1745 /* drop it. the responsible gateway has forwarded it into
1746 * the backbone network.
1750 /* we must allow it. at least if we are
1751 * responsible for the DESTINATION.
1756 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
1763 batadv_hardif_free_ref(primary_if);
1765 batadv_claim_free_ref(claim);
1770 * batadv_bla_claim_table_seq_print_text - print the claim table in a seq file
1771 * @seq: seq file to print on
1776 int batadv_bla_claim_table_seq_print_text(struct seq_file *seq, void *offset)
1778 struct net_device *net_dev = (struct net_device *)seq->private;
1779 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1780 struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
1781 struct batadv_bla_claim *claim;
1782 struct batadv_hard_iface *primary_if;
1783 struct hlist_head *head;
1789 primary_if = batadv_seq_print_text_primary_if_get(seq);
1793 primary_addr = primary_if->net_dev->dev_addr;
1795 "Claims announced for the mesh %s (orig %pM, group id %#.4x)\n",
1796 net_dev->name, primary_addr,
1797 ntohs(bat_priv->bla.claim_dest.group));
1798 seq_printf(seq, " %-17s %-5s %-17s [o] (%-6s)\n",
1799 "Client", "VID", "Originator", "CRC");
1800 for (i = 0; i < hash->size; i++) {
1801 head = &hash->table[i];
1804 hlist_for_each_entry_rcu(claim, head, hash_entry) {
1805 is_own = batadv_compare_eth(claim->backbone_gw->orig,
1808 spin_lock_bh(&claim->backbone_gw->crc_lock);
1809 backbone_crc = claim->backbone_gw->crc;
1810 spin_unlock_bh(&claim->backbone_gw->crc_lock);
1811 seq_printf(seq, " * %pM on %5d by %pM [%c] (%#.4x)\n",
1812 claim->addr, BATADV_PRINT_VID(claim->vid),
1813 claim->backbone_gw->orig,
1814 (is_own ? 'x' : ' '),
1821 batadv_hardif_free_ref(primary_if);
1826 * batadv_bla_backbone_table_seq_print_text - print the backbone table in a seq
1828 * @seq: seq file to print on
1833 int batadv_bla_backbone_table_seq_print_text(struct seq_file *seq, void *offset)
1835 struct net_device *net_dev = (struct net_device *)seq->private;
1836 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1837 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
1838 struct batadv_bla_backbone_gw *backbone_gw;
1839 struct batadv_hard_iface *primary_if;
1840 struct hlist_head *head;
1847 primary_if = batadv_seq_print_text_primary_if_get(seq);
1851 primary_addr = primary_if->net_dev->dev_addr;
1853 "Backbones announced for the mesh %s (orig %pM, group id %#.4x)\n",
1854 net_dev->name, primary_addr,
1855 ntohs(bat_priv->bla.claim_dest.group));
1856 seq_printf(seq, " %-17s %-5s %-9s (%-6s)\n",
1857 "Originator", "VID", "last seen", "CRC");
1858 for (i = 0; i < hash->size; i++) {
1859 head = &hash->table[i];
1862 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
1863 msecs = jiffies_to_msecs(jiffies -
1864 backbone_gw->lasttime);
1865 secs = msecs / 1000;
1866 msecs = msecs % 1000;
1868 is_own = batadv_compare_eth(backbone_gw->orig,
1873 spin_lock_bh(&backbone_gw->crc_lock);
1874 backbone_crc = backbone_gw->crc;
1875 spin_unlock_bh(&backbone_gw->crc_lock);
1877 seq_printf(seq, " * %pM on %5d %4i.%03is (%#.4x)\n",
1879 BATADV_PRINT_VID(backbone_gw->vid), secs,
1880 msecs, backbone_crc);
1886 batadv_hardif_free_ref(primary_if);