1 /* Copyright (C) 2011-2016 B.A.T.M.A.N. contributors:
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 #include "bridge_loop_avoidance.h"
21 #include <linux/atomic.h>
22 #include <linux/byteorder/generic.h>
23 #include <linux/compiler.h>
24 #include <linux/crc16.h>
25 #include <linux/errno.h>
26 #include <linux/etherdevice.h>
28 #include <linux/if_arp.h>
29 #include <linux/if_ether.h>
30 #include <linux/if_vlan.h>
31 #include <linux/jhash.h>
32 #include <linux/jiffies.h>
33 #include <linux/kernel.h>
34 #include <linux/kref.h>
35 #include <linux/list.h>
36 #include <linux/lockdep.h>
37 #include <linux/netdevice.h>
38 #include <linux/rculist.h>
39 #include <linux/rcupdate.h>
40 #include <linux/seq_file.h>
41 #include <linux/skbuff.h>
42 #include <linux/slab.h>
43 #include <linux/spinlock.h>
44 #include <linux/stddef.h>
45 #include <linux/string.h>
46 #include <linux/workqueue.h>
49 #include "hard-interface.h"
51 #include "originator.h"
53 #include "translation-table.h"
55 static const u8 batadv_announce_mac[4] = {0x43, 0x05, 0x43, 0x05};
57 static void batadv_bla_periodic_work(struct work_struct *work);
59 batadv_bla_send_announce(struct batadv_priv *bat_priv,
60 struct batadv_bla_backbone_gw *backbone_gw);
63 * batadv_choose_claim - choose the right bucket for a claim.
65 * @size: size of the hash table
67 * Return: the hash index of the claim
69 static inline u32 batadv_choose_claim(const void *data, u32 size)
71 struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
74 hash = jhash(&claim->addr, sizeof(claim->addr), hash);
75 hash = jhash(&claim->vid, sizeof(claim->vid), hash);
81 * batadv_choose_backbone_gw - choose the right bucket for a backbone gateway.
83 * @size: size of the hash table
85 * Return: the hash index of the backbone gateway
87 static inline u32 batadv_choose_backbone_gw(const void *data, u32 size)
89 const struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
92 hash = jhash(&claim->addr, sizeof(claim->addr), hash);
93 hash = jhash(&claim->vid, sizeof(claim->vid), hash);
99 * batadv_compare_backbone_gw - compare address and vid of two backbone gws
100 * @node: list node of the first entry to compare
101 * @data2: pointer to the second backbone gateway
103 * Return: 1 if the backbones have the same data, 0 otherwise
105 static int batadv_compare_backbone_gw(const struct hlist_node *node,
108 const void *data1 = container_of(node, struct batadv_bla_backbone_gw,
110 const struct batadv_bla_backbone_gw *gw1 = data1;
111 const struct batadv_bla_backbone_gw *gw2 = data2;
113 if (!batadv_compare_eth(gw1->orig, gw2->orig))
116 if (gw1->vid != gw2->vid)
123 * batadv_compare_backbone_gw - compare address and vid of two claims
124 * @node: list node of the first entry to compare
125 * @data2: pointer to the second claims
127 * Return: 1 if the claim have the same data, 0 otherwise
129 static int batadv_compare_claim(const struct hlist_node *node,
132 const void *data1 = container_of(node, struct batadv_bla_claim,
134 const struct batadv_bla_claim *cl1 = data1;
135 const struct batadv_bla_claim *cl2 = data2;
137 if (!batadv_compare_eth(cl1->addr, cl2->addr))
140 if (cl1->vid != cl2->vid)
147 * batadv_backbone_gw_release - release backbone gw from lists and queue for
148 * free after rcu grace period
149 * @ref: kref pointer of the backbone gw
151 static void batadv_backbone_gw_release(struct kref *ref)
153 struct batadv_bla_backbone_gw *backbone_gw;
155 backbone_gw = container_of(ref, struct batadv_bla_backbone_gw,
158 kfree_rcu(backbone_gw, rcu);
162 * batadv_backbone_gw_free_ref - decrement the backbone gw refcounter and
163 * possibly release it
164 * @backbone_gw: backbone gateway to be free'd
167 batadv_backbone_gw_free_ref(struct batadv_bla_backbone_gw *backbone_gw)
169 kref_put(&backbone_gw->refcount, batadv_backbone_gw_release);
173 * batadv_claim_release - release claim from lists and queue for free after rcu
175 * @ref: kref pointer of the claim
177 static void batadv_claim_release(struct kref *ref)
179 struct batadv_bla_claim *claim;
181 claim = container_of(ref, struct batadv_bla_claim, refcount);
183 batadv_backbone_gw_free_ref(claim->backbone_gw);
184 kfree_rcu(claim, rcu);
188 * batadv_claim_free_ref - decrement the claim refcounter and possibly
190 * @claim: claim to be free'd
192 static void batadv_claim_free_ref(struct batadv_bla_claim *claim)
194 kref_put(&claim->refcount, batadv_claim_release);
198 * batadv_claim_hash_find - looks for a claim in the claim hash
199 * @bat_priv: the bat priv with all the soft interface information
200 * @data: search data (may be local/static data)
202 * Return: claim if found or NULL otherwise.
204 static struct batadv_bla_claim
205 *batadv_claim_hash_find(struct batadv_priv *bat_priv,
206 struct batadv_bla_claim *data)
208 struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
209 struct hlist_head *head;
210 struct batadv_bla_claim *claim;
211 struct batadv_bla_claim *claim_tmp = NULL;
217 index = batadv_choose_claim(data, hash->size);
218 head = &hash->table[index];
221 hlist_for_each_entry_rcu(claim, head, hash_entry) {
222 if (!batadv_compare_claim(&claim->hash_entry, data))
225 if (!kref_get_unless_zero(&claim->refcount))
237 * batadv_backbone_hash_find - looks for a backbone gateway in the hash
238 * @bat_priv: the bat priv with all the soft interface information
239 * @addr: the address of the originator
242 * Return: backbone gateway if found or NULL otherwise
244 static struct batadv_bla_backbone_gw *
245 batadv_backbone_hash_find(struct batadv_priv *bat_priv, u8 *addr,
248 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
249 struct hlist_head *head;
250 struct batadv_bla_backbone_gw search_entry, *backbone_gw;
251 struct batadv_bla_backbone_gw *backbone_gw_tmp = NULL;
257 ether_addr_copy(search_entry.orig, addr);
258 search_entry.vid = vid;
260 index = batadv_choose_backbone_gw(&search_entry, hash->size);
261 head = &hash->table[index];
264 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
265 if (!batadv_compare_backbone_gw(&backbone_gw->hash_entry,
269 if (!kref_get_unless_zero(&backbone_gw->refcount))
272 backbone_gw_tmp = backbone_gw;
277 return backbone_gw_tmp;
281 * batadv_bla_del_backbone_claims - delete all claims for a backbone
282 * @backbone_gw: backbone gateway where the claims should be removed
285 batadv_bla_del_backbone_claims(struct batadv_bla_backbone_gw *backbone_gw)
287 struct batadv_hashtable *hash;
288 struct hlist_node *node_tmp;
289 struct hlist_head *head;
290 struct batadv_bla_claim *claim;
292 spinlock_t *list_lock; /* protects write access to the hash lists */
294 hash = backbone_gw->bat_priv->bla.claim_hash;
298 for (i = 0; i < hash->size; i++) {
299 head = &hash->table[i];
300 list_lock = &hash->list_locks[i];
302 spin_lock_bh(list_lock);
303 hlist_for_each_entry_safe(claim, node_tmp,
305 if (claim->backbone_gw != backbone_gw)
308 batadv_claim_free_ref(claim);
309 hlist_del_rcu(&claim->hash_entry);
311 spin_unlock_bh(list_lock);
314 /* all claims gone, initialize CRC */
315 spin_lock_bh(&backbone_gw->crc_lock);
316 backbone_gw->crc = BATADV_BLA_CRC_INIT;
317 spin_unlock_bh(&backbone_gw->crc_lock);
321 * batadv_bla_send_claim - sends a claim frame according to the provided info
322 * @bat_priv: the bat priv with all the soft interface information
323 * @mac: the mac address to be announced within the claim
325 * @claimtype: the type of the claim (CLAIM, UNCLAIM, ANNOUNCE, ...)
327 static void batadv_bla_send_claim(struct batadv_priv *bat_priv, u8 *mac,
328 unsigned short vid, int claimtype)
331 struct ethhdr *ethhdr;
332 struct batadv_hard_iface *primary_if;
333 struct net_device *soft_iface;
335 struct batadv_bla_claim_dst local_claim_dest;
338 primary_if = batadv_primary_if_get_selected(bat_priv);
342 memcpy(&local_claim_dest, &bat_priv->bla.claim_dest,
343 sizeof(local_claim_dest));
344 local_claim_dest.type = claimtype;
346 soft_iface = primary_if->soft_iface;
348 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
349 /* IP DST: 0.0.0.0 */
351 primary_if->soft_iface,
352 /* IP SRC: 0.0.0.0 */
354 /* Ethernet DST: Broadcast */
356 /* Ethernet SRC/HW SRC: originator mac */
357 primary_if->net_dev->dev_addr,
358 /* HW DST: FF:43:05:XX:YY:YY
359 * with XX = claim type
360 * and YY:YY = group id
362 (u8 *)&local_claim_dest);
367 ethhdr = (struct ethhdr *)skb->data;
368 hw_src = (u8 *)ethhdr + ETH_HLEN + sizeof(struct arphdr);
370 /* now we pretend that the client would have sent this ... */
372 case BATADV_CLAIM_TYPE_CLAIM:
373 /* normal claim frame
374 * set Ethernet SRC to the clients mac
376 ether_addr_copy(ethhdr->h_source, mac);
377 batadv_dbg(BATADV_DBG_BLA, bat_priv,
378 "bla_send_claim(): CLAIM %pM on vid %d\n", mac,
379 BATADV_PRINT_VID(vid));
381 case BATADV_CLAIM_TYPE_UNCLAIM:
383 * set HW SRC to the clients mac
385 ether_addr_copy(hw_src, mac);
386 batadv_dbg(BATADV_DBG_BLA, bat_priv,
387 "bla_send_claim(): UNCLAIM %pM on vid %d\n", mac,
388 BATADV_PRINT_VID(vid));
390 case BATADV_CLAIM_TYPE_ANNOUNCE:
391 /* announcement frame
392 * set HW SRC to the special mac containg the crc
394 ether_addr_copy(hw_src, mac);
395 batadv_dbg(BATADV_DBG_BLA, bat_priv,
396 "bla_send_claim(): ANNOUNCE of %pM on vid %d\n",
397 ethhdr->h_source, BATADV_PRINT_VID(vid));
399 case BATADV_CLAIM_TYPE_REQUEST:
401 * set HW SRC and header destination to the receiving backbone
404 ether_addr_copy(hw_src, mac);
405 ether_addr_copy(ethhdr->h_dest, mac);
406 batadv_dbg(BATADV_DBG_BLA, bat_priv,
407 "bla_send_claim(): REQUEST of %pM to %pM on vid %d\n",
408 ethhdr->h_source, ethhdr->h_dest,
409 BATADV_PRINT_VID(vid));
413 if (vid & BATADV_VLAN_HAS_TAG)
414 skb = vlan_insert_tag(skb, htons(ETH_P_8021Q),
415 vid & VLAN_VID_MASK);
417 skb_reset_mac_header(skb);
418 skb->protocol = eth_type_trans(skb, soft_iface);
419 batadv_inc_counter(bat_priv, BATADV_CNT_RX);
420 batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
421 skb->len + ETH_HLEN);
422 soft_iface->last_rx = jiffies;
427 batadv_hardif_free_ref(primary_if);
431 * batadv_bla_get_backbone_gw - finds or creates a backbone gateway
432 * @bat_priv: the bat priv with all the soft interface information
433 * @orig: the mac address of the originator
435 * @own_backbone: set if the requested backbone is local
437 * Return: the (possibly created) backbone gateway or NULL on error
439 static struct batadv_bla_backbone_gw *
440 batadv_bla_get_backbone_gw(struct batadv_priv *bat_priv, u8 *orig,
441 unsigned short vid, bool own_backbone)
443 struct batadv_bla_backbone_gw *entry;
444 struct batadv_orig_node *orig_node;
447 entry = batadv_backbone_hash_find(bat_priv, orig, vid);
452 batadv_dbg(BATADV_DBG_BLA, bat_priv,
453 "bla_get_backbone_gw(): not found (%pM, %d), creating new entry\n",
454 orig, BATADV_PRINT_VID(vid));
456 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
461 entry->lasttime = jiffies;
462 entry->crc = BATADV_BLA_CRC_INIT;
463 entry->bat_priv = bat_priv;
464 spin_lock_init(&entry->crc_lock);
465 atomic_set(&entry->request_sent, 0);
466 atomic_set(&entry->wait_periods, 0);
467 ether_addr_copy(entry->orig, orig);
469 /* one for the hash, one for returning */
470 kref_init(&entry->refcount);
471 kref_get(&entry->refcount);
473 hash_added = batadv_hash_add(bat_priv->bla.backbone_hash,
474 batadv_compare_backbone_gw,
475 batadv_choose_backbone_gw, entry,
478 if (unlikely(hash_added != 0)) {
479 /* hash failed, free the structure */
484 /* this is a gateway now, remove any TT entry on this VLAN */
485 orig_node = batadv_orig_hash_find(bat_priv, orig);
487 batadv_tt_global_del_orig(bat_priv, orig_node, vid,
488 "became a backbone gateway");
489 batadv_orig_node_free_ref(orig_node);
493 batadv_bla_send_announce(bat_priv, entry);
495 /* this will be decreased in the worker thread */
496 atomic_inc(&entry->request_sent);
497 atomic_set(&entry->wait_periods, BATADV_BLA_WAIT_PERIODS);
498 atomic_inc(&bat_priv->bla.num_requests);
505 * batadv_bla_update_own_backbone_gw - updates the own backbone gw for a VLAN
506 * @bat_priv: the bat priv with all the soft interface information
507 * @primary_if: the selected primary interface
508 * @vid: VLAN identifier
510 * update or add the own backbone gw to make sure we announce
511 * where we receive other backbone gws
514 batadv_bla_update_own_backbone_gw(struct batadv_priv *bat_priv,
515 struct batadv_hard_iface *primary_if,
518 struct batadv_bla_backbone_gw *backbone_gw;
520 backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
521 primary_if->net_dev->dev_addr,
523 if (unlikely(!backbone_gw))
526 backbone_gw->lasttime = jiffies;
527 batadv_backbone_gw_free_ref(backbone_gw);
531 * batadv_bla_answer_request - answer a bla request by sending own claims
532 * @bat_priv: the bat priv with all the soft interface information
533 * @primary_if: interface where the request came on
534 * @vid: the vid where the request came on
536 * Repeat all of our own claims, and finally send an ANNOUNCE frame
537 * to allow the requester another check if the CRC is correct now.
539 static void batadv_bla_answer_request(struct batadv_priv *bat_priv,
540 struct batadv_hard_iface *primary_if,
543 struct hlist_head *head;
544 struct batadv_hashtable *hash;
545 struct batadv_bla_claim *claim;
546 struct batadv_bla_backbone_gw *backbone_gw;
549 batadv_dbg(BATADV_DBG_BLA, bat_priv,
550 "bla_answer_request(): received a claim request, send all of our own claims again\n");
552 backbone_gw = batadv_backbone_hash_find(bat_priv,
553 primary_if->net_dev->dev_addr,
558 hash = bat_priv->bla.claim_hash;
559 for (i = 0; i < hash->size; i++) {
560 head = &hash->table[i];
563 hlist_for_each_entry_rcu(claim, head, hash_entry) {
564 /* only own claims are interesting */
565 if (claim->backbone_gw != backbone_gw)
568 batadv_bla_send_claim(bat_priv, claim->addr, claim->vid,
569 BATADV_CLAIM_TYPE_CLAIM);
574 /* finally, send an announcement frame */
575 batadv_bla_send_announce(bat_priv, backbone_gw);
576 batadv_backbone_gw_free_ref(backbone_gw);
580 * batadv_bla_send_request - send a request to repeat claims
581 * @backbone_gw: the backbone gateway from whom we are out of sync
583 * When the crc is wrong, ask the backbone gateway for a full table update.
584 * After the request, it will repeat all of his own claims and finally
585 * send an announcement claim with which we can check again.
587 static void batadv_bla_send_request(struct batadv_bla_backbone_gw *backbone_gw)
589 /* first, remove all old entries */
590 batadv_bla_del_backbone_claims(backbone_gw);
592 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
593 "Sending REQUEST to %pM\n", backbone_gw->orig);
596 batadv_bla_send_claim(backbone_gw->bat_priv, backbone_gw->orig,
597 backbone_gw->vid, BATADV_CLAIM_TYPE_REQUEST);
599 /* no local broadcasts should be sent or received, for now. */
600 if (!atomic_read(&backbone_gw->request_sent)) {
601 atomic_inc(&backbone_gw->bat_priv->bla.num_requests);
602 atomic_set(&backbone_gw->request_sent, 1);
607 * batadv_bla_send_announce - Send an announcement frame
608 * @bat_priv: the bat priv with all the soft interface information
609 * @backbone_gw: our backbone gateway which should be announced
611 static void batadv_bla_send_announce(struct batadv_priv *bat_priv,
612 struct batadv_bla_backbone_gw *backbone_gw)
617 memcpy(mac, batadv_announce_mac, 4);
618 spin_lock_bh(&backbone_gw->crc_lock);
619 crc = htons(backbone_gw->crc);
620 spin_unlock_bh(&backbone_gw->crc_lock);
621 memcpy(&mac[4], &crc, 2);
623 batadv_bla_send_claim(bat_priv, mac, backbone_gw->vid,
624 BATADV_CLAIM_TYPE_ANNOUNCE);
628 * batadv_bla_add_claim - Adds a claim in the claim hash
629 * @bat_priv: the bat priv with all the soft interface information
630 * @mac: the mac address of the claim
631 * @vid: the VLAN ID of the frame
632 * @backbone_gw: the backbone gateway which claims it
634 static void batadv_bla_add_claim(struct batadv_priv *bat_priv,
635 const u8 *mac, const unsigned short vid,
636 struct batadv_bla_backbone_gw *backbone_gw)
638 struct batadv_bla_claim *claim;
639 struct batadv_bla_claim search_claim;
642 ether_addr_copy(search_claim.addr, mac);
643 search_claim.vid = vid;
644 claim = batadv_claim_hash_find(bat_priv, &search_claim);
646 /* create a new claim entry if it does not exist yet. */
648 claim = kzalloc(sizeof(*claim), GFP_ATOMIC);
652 ether_addr_copy(claim->addr, mac);
654 claim->lasttime = jiffies;
655 claim->backbone_gw = backbone_gw;
657 kref_init(&claim->refcount);
658 kref_get(&claim->refcount);
659 batadv_dbg(BATADV_DBG_BLA, bat_priv,
660 "bla_add_claim(): adding new entry %pM, vid %d to hash ...\n",
661 mac, BATADV_PRINT_VID(vid));
662 hash_added = batadv_hash_add(bat_priv->bla.claim_hash,
663 batadv_compare_claim,
664 batadv_choose_claim, claim,
667 if (unlikely(hash_added != 0)) {
668 /* only local changes happened. */
673 claim->lasttime = jiffies;
674 if (claim->backbone_gw == backbone_gw)
675 /* no need to register a new backbone */
678 batadv_dbg(BATADV_DBG_BLA, bat_priv,
679 "bla_add_claim(): changing ownership for %pM, vid %d\n",
680 mac, BATADV_PRINT_VID(vid));
682 spin_lock_bh(&claim->backbone_gw->crc_lock);
683 claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
684 spin_unlock_bh(&claim->backbone_gw->crc_lock);
685 batadv_backbone_gw_free_ref(claim->backbone_gw);
687 /* set (new) backbone gw */
688 kref_get(&backbone_gw->refcount);
689 claim->backbone_gw = backbone_gw;
691 spin_lock_bh(&backbone_gw->crc_lock);
692 backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
693 spin_unlock_bh(&backbone_gw->crc_lock);
694 backbone_gw->lasttime = jiffies;
697 batadv_claim_free_ref(claim);
701 * batadv_bla_del_claim - delete a claim from the claim hash
702 * @bat_priv: the bat priv with all the soft interface information
703 * @mac: mac address of the claim to be removed
704 * @vid: VLAN id for the claim to be removed
706 static void batadv_bla_del_claim(struct batadv_priv *bat_priv,
707 const u8 *mac, const unsigned short vid)
709 struct batadv_bla_claim search_claim, *claim;
711 ether_addr_copy(search_claim.addr, mac);
712 search_claim.vid = vid;
713 claim = batadv_claim_hash_find(bat_priv, &search_claim);
717 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla_del_claim(): %pM, vid %d\n",
718 mac, BATADV_PRINT_VID(vid));
720 batadv_hash_remove(bat_priv->bla.claim_hash, batadv_compare_claim,
721 batadv_choose_claim, claim);
722 batadv_claim_free_ref(claim); /* reference from the hash is gone */
724 spin_lock_bh(&claim->backbone_gw->crc_lock);
725 claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
726 spin_unlock_bh(&claim->backbone_gw->crc_lock);
728 /* don't need the reference from hash_find() anymore */
729 batadv_claim_free_ref(claim);
733 * batadv_handle_announce - check for ANNOUNCE frame
734 * @bat_priv: the bat priv with all the soft interface information
735 * @an_addr: announcement mac address (ARP Sender HW address)
736 * @backbone_addr: originator address of the sender (Ethernet source MAC)
737 * @vid: the VLAN ID of the frame
739 * Return: 1 if handled
741 static int batadv_handle_announce(struct batadv_priv *bat_priv, u8 *an_addr,
742 u8 *backbone_addr, unsigned short vid)
744 struct batadv_bla_backbone_gw *backbone_gw;
745 u16 backbone_crc, crc;
747 if (memcmp(an_addr, batadv_announce_mac, 4) != 0)
750 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
753 if (unlikely(!backbone_gw))
756 /* handle as ANNOUNCE frame */
757 backbone_gw->lasttime = jiffies;
758 crc = ntohs(*((__be16 *)(&an_addr[4])));
760 batadv_dbg(BATADV_DBG_BLA, bat_priv,
761 "handle_announce(): ANNOUNCE vid %d (sent by %pM)... CRC = %#.4x\n",
762 BATADV_PRINT_VID(vid), backbone_gw->orig, crc);
764 spin_lock_bh(&backbone_gw->crc_lock);
765 backbone_crc = backbone_gw->crc;
766 spin_unlock_bh(&backbone_gw->crc_lock);
768 if (backbone_crc != crc) {
769 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
770 "handle_announce(): CRC FAILED for %pM/%d (my = %#.4x, sent = %#.4x)\n",
772 BATADV_PRINT_VID(backbone_gw->vid),
775 batadv_bla_send_request(backbone_gw);
777 /* if we have sent a request and the crc was OK,
778 * we can allow traffic again.
780 if (atomic_read(&backbone_gw->request_sent)) {
781 atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
782 atomic_set(&backbone_gw->request_sent, 0);
786 batadv_backbone_gw_free_ref(backbone_gw);
791 * batadv_handle_request - check for REQUEST frame
792 * @bat_priv: the bat priv with all the soft interface information
793 * @primary_if: the primary hard interface of this batman soft interface
794 * @backbone_addr: backbone address to be requested (ARP sender HW MAC)
795 * @ethhdr: ethernet header of a packet
796 * @vid: the VLAN ID of the frame
798 * Return: 1 if handled
800 static int batadv_handle_request(struct batadv_priv *bat_priv,
801 struct batadv_hard_iface *primary_if,
802 u8 *backbone_addr, struct ethhdr *ethhdr,
805 /* check for REQUEST frame */
806 if (!batadv_compare_eth(backbone_addr, ethhdr->h_dest))
809 /* sanity check, this should not happen on a normal switch,
810 * we ignore it in this case.
812 if (!batadv_compare_eth(ethhdr->h_dest, primary_if->net_dev->dev_addr))
815 batadv_dbg(BATADV_DBG_BLA, bat_priv,
816 "handle_request(): REQUEST vid %d (sent by %pM)...\n",
817 BATADV_PRINT_VID(vid), ethhdr->h_source);
819 batadv_bla_answer_request(bat_priv, primary_if, vid);
824 * batadv_handle_unclaim - check for UNCLAIM frame
825 * @bat_priv: the bat priv with all the soft interface information
826 * @primary_if: the primary hard interface of this batman soft interface
827 * @backbone_addr: originator address of the backbone (Ethernet source)
828 * @claim_addr: Client to be unclaimed (ARP sender HW MAC)
829 * @vid: the VLAN ID of the frame
831 * Return: 1 if handled
833 static int batadv_handle_unclaim(struct batadv_priv *bat_priv,
834 struct batadv_hard_iface *primary_if,
835 u8 *backbone_addr, u8 *claim_addr,
838 struct batadv_bla_backbone_gw *backbone_gw;
840 /* unclaim in any case if it is our own */
841 if (primary_if && batadv_compare_eth(backbone_addr,
842 primary_if->net_dev->dev_addr))
843 batadv_bla_send_claim(bat_priv, claim_addr, vid,
844 BATADV_CLAIM_TYPE_UNCLAIM);
846 backbone_gw = batadv_backbone_hash_find(bat_priv, backbone_addr, vid);
851 /* this must be an UNCLAIM frame */
852 batadv_dbg(BATADV_DBG_BLA, bat_priv,
853 "handle_unclaim(): UNCLAIM %pM on vid %d (sent by %pM)...\n",
854 claim_addr, BATADV_PRINT_VID(vid), backbone_gw->orig);
856 batadv_bla_del_claim(bat_priv, claim_addr, vid);
857 batadv_backbone_gw_free_ref(backbone_gw);
862 * batadv_handle_claim - check for CLAIM frame
863 * @bat_priv: the bat priv with all the soft interface information
864 * @primary_if: the primary hard interface of this batman soft interface
865 * @backbone_addr: originator address of the backbone (Ethernet Source)
866 * @claim_addr: client mac address to be claimed (ARP sender HW MAC)
867 * @vid: the VLAN ID of the frame
869 * Return: 1 if handled
871 static int batadv_handle_claim(struct batadv_priv *bat_priv,
872 struct batadv_hard_iface *primary_if,
873 u8 *backbone_addr, u8 *claim_addr,
876 struct batadv_bla_backbone_gw *backbone_gw;
878 /* register the gateway if not yet available, and add the claim. */
880 backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
883 if (unlikely(!backbone_gw))
886 /* this must be a CLAIM frame */
887 batadv_bla_add_claim(bat_priv, claim_addr, vid, backbone_gw);
888 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
889 batadv_bla_send_claim(bat_priv, claim_addr, vid,
890 BATADV_CLAIM_TYPE_CLAIM);
892 /* TODO: we could call something like tt_local_del() here. */
894 batadv_backbone_gw_free_ref(backbone_gw);
899 * batadv_check_claim_group - check for claim group membership
900 * @bat_priv: the bat priv with all the soft interface information
901 * @primary_if: the primary interface of this batman interface
902 * @hw_src: the Hardware source in the ARP Header
903 * @hw_dst: the Hardware destination in the ARP Header
904 * @ethhdr: pointer to the Ethernet header of the claim frame
906 * checks if it is a claim packet and if its on the same group.
907 * This function also applies the group ID of the sender
908 * if it is in the same mesh.
911 * 2 - if it is a claim packet and on the same group
912 * 1 - if is a claim packet from another group
913 * 0 - if it is not a claim packet
915 static int batadv_check_claim_group(struct batadv_priv *bat_priv,
916 struct batadv_hard_iface *primary_if,
917 u8 *hw_src, u8 *hw_dst,
918 struct ethhdr *ethhdr)
921 struct batadv_orig_node *orig_node;
922 struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
924 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
925 bla_dst_own = &bat_priv->bla.claim_dest;
927 /* if announcement packet, use the source,
928 * otherwise assume it is in the hw_src
930 switch (bla_dst->type) {
931 case BATADV_CLAIM_TYPE_CLAIM:
932 backbone_addr = hw_src;
934 case BATADV_CLAIM_TYPE_REQUEST:
935 case BATADV_CLAIM_TYPE_ANNOUNCE:
936 case BATADV_CLAIM_TYPE_UNCLAIM:
937 backbone_addr = ethhdr->h_source;
943 /* don't accept claim frames from ourselves */
944 if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
947 /* if its already the same group, it is fine. */
948 if (bla_dst->group == bla_dst_own->group)
951 /* lets see if this originator is in our mesh */
952 orig_node = batadv_orig_hash_find(bat_priv, backbone_addr);
954 /* dont accept claims from gateways which are not in
955 * the same mesh or group.
960 /* if our mesh friends mac is bigger, use it for ourselves. */
961 if (ntohs(bla_dst->group) > ntohs(bla_dst_own->group)) {
962 batadv_dbg(BATADV_DBG_BLA, bat_priv,
963 "taking other backbones claim group: %#.4x\n",
964 ntohs(bla_dst->group));
965 bla_dst_own->group = bla_dst->group;
968 batadv_orig_node_free_ref(orig_node);
974 * batadv_bla_process_claim - Check if this is a claim frame, and process it
975 * @bat_priv: the bat priv with all the soft interface information
976 * @primary_if: the primary hard interface of this batman soft interface
977 * @skb: the frame to be checked
979 * Return: 1 if it was a claim frame, otherwise return 0 to
980 * tell the callee that it can use the frame on its own.
982 static int batadv_bla_process_claim(struct batadv_priv *bat_priv,
983 struct batadv_hard_iface *primary_if,
986 struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
988 struct vlan_hdr *vhdr, vhdr_buf;
989 struct ethhdr *ethhdr;
990 struct arphdr *arphdr;
997 vid = batadv_get_vid(skb, 0);
998 ethhdr = eth_hdr(skb);
1000 proto = ethhdr->h_proto;
1002 if (vid & BATADV_VLAN_HAS_TAG) {
1003 /* Traverse the VLAN/Ethertypes.
1005 * At this point it is known that the first protocol is a VLAN
1006 * header, so start checking at the encapsulated protocol.
1008 * The depth of the VLAN headers is recorded to drop BLA claim
1009 * frames encapsulated into multiple VLAN headers (QinQ).
1012 vhdr = skb_header_pointer(skb, headlen, VLAN_HLEN,
1017 proto = vhdr->h_vlan_encapsulated_proto;
1018 headlen += VLAN_HLEN;
1020 } while (proto == htons(ETH_P_8021Q));
1023 if (proto != htons(ETH_P_ARP))
1024 return 0; /* not a claim frame */
1026 /* this must be a ARP frame. check if it is a claim. */
1028 if (unlikely(!pskb_may_pull(skb, headlen + arp_hdr_len(skb->dev))))
1031 /* pskb_may_pull() may have modified the pointers, get ethhdr again */
1032 ethhdr = eth_hdr(skb);
1033 arphdr = (struct arphdr *)((u8 *)ethhdr + headlen);
1035 /* Check whether the ARP frame carries a valid
1038 if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
1040 if (arphdr->ar_pro != htons(ETH_P_IP))
1042 if (arphdr->ar_hln != ETH_ALEN)
1044 if (arphdr->ar_pln != 4)
1047 hw_src = (u8 *)arphdr + sizeof(struct arphdr);
1048 hw_dst = hw_src + ETH_ALEN + 4;
1049 bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
1050 bla_dst_own = &bat_priv->bla.claim_dest;
1052 /* check if it is a claim frame in general */
1053 if (memcmp(bla_dst->magic, bla_dst_own->magic,
1054 sizeof(bla_dst->magic)) != 0)
1057 /* check if there is a claim frame encapsulated deeper in (QinQ) and
1058 * drop that, as this is not supported by BLA but should also not be
1059 * sent via the mesh.
1064 /* check if it is a claim frame. */
1065 ret = batadv_check_claim_group(bat_priv, primary_if, hw_src, hw_dst,
1068 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1069 "bla_process_claim(): received a claim frame from another group. From: %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n",
1070 ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src,
1076 /* become a backbone gw ourselves on this vlan if not happened yet */
1077 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
1079 /* check for the different types of claim frames ... */
1080 switch (bla_dst->type) {
1081 case BATADV_CLAIM_TYPE_CLAIM:
1082 if (batadv_handle_claim(bat_priv, primary_if, hw_src,
1083 ethhdr->h_source, vid))
1086 case BATADV_CLAIM_TYPE_UNCLAIM:
1087 if (batadv_handle_unclaim(bat_priv, primary_if,
1088 ethhdr->h_source, hw_src, vid))
1092 case BATADV_CLAIM_TYPE_ANNOUNCE:
1093 if (batadv_handle_announce(bat_priv, hw_src, ethhdr->h_source,
1097 case BATADV_CLAIM_TYPE_REQUEST:
1098 if (batadv_handle_request(bat_priv, primary_if, hw_src, ethhdr,
1104 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1105 "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",
1106 ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src, hw_dst);
1111 * batadv_bla_purge_backbone_gw - Remove backbone gateways after a timeout or
1113 * @bat_priv: the bat priv with all the soft interface information
1114 * @now: whether the whole hash shall be wiped now
1116 * Check when we last heard from other nodes, and remove them in case of
1117 * a time out, or clean all backbone gws if now is set.
1119 static void batadv_bla_purge_backbone_gw(struct batadv_priv *bat_priv, int now)
1121 struct batadv_bla_backbone_gw *backbone_gw;
1122 struct hlist_node *node_tmp;
1123 struct hlist_head *head;
1124 struct batadv_hashtable *hash;
1125 spinlock_t *list_lock; /* protects write access to the hash lists */
1128 hash = bat_priv->bla.backbone_hash;
1132 for (i = 0; i < hash->size; i++) {
1133 head = &hash->table[i];
1134 list_lock = &hash->list_locks[i];
1136 spin_lock_bh(list_lock);
1137 hlist_for_each_entry_safe(backbone_gw, node_tmp,
1141 if (!batadv_has_timed_out(backbone_gw->lasttime,
1142 BATADV_BLA_BACKBONE_TIMEOUT))
1145 batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
1146 "bla_purge_backbone_gw(): backbone gw %pM timed out\n",
1150 /* don't wait for the pending request anymore */
1151 if (atomic_read(&backbone_gw->request_sent))
1152 atomic_dec(&bat_priv->bla.num_requests);
1154 batadv_bla_del_backbone_claims(backbone_gw);
1156 hlist_del_rcu(&backbone_gw->hash_entry);
1157 batadv_backbone_gw_free_ref(backbone_gw);
1159 spin_unlock_bh(list_lock);
1164 * batadv_bla_purge_claims - Remove claims after a timeout or immediately
1165 * @bat_priv: the bat priv with all the soft interface information
1166 * @primary_if: the selected primary interface, may be NULL if now is set
1167 * @now: whether the whole hash shall be wiped now
1169 * Check when we heard last time from our own claims, and remove them in case of
1170 * a time out, or clean all claims if now is set
1172 static void batadv_bla_purge_claims(struct batadv_priv *bat_priv,
1173 struct batadv_hard_iface *primary_if,
1176 struct batadv_bla_claim *claim;
1177 struct hlist_head *head;
1178 struct batadv_hashtable *hash;
1181 hash = bat_priv->bla.claim_hash;
1185 for (i = 0; i < hash->size; i++) {
1186 head = &hash->table[i];
1189 hlist_for_each_entry_rcu(claim, head, hash_entry) {
1192 if (!batadv_compare_eth(claim->backbone_gw->orig,
1193 primary_if->net_dev->dev_addr))
1195 if (!batadv_has_timed_out(claim->lasttime,
1196 BATADV_BLA_CLAIM_TIMEOUT))
1199 batadv_dbg(BATADV_DBG_BLA, bat_priv,
1200 "bla_purge_claims(): %pM, vid %d, time out\n",
1201 claim->addr, claim->vid);
1204 batadv_handle_unclaim(bat_priv, primary_if,
1205 claim->backbone_gw->orig,
1206 claim->addr, claim->vid);
1213 * batadv_bla_update_orig_address - Update the backbone gateways when the own
1214 * originator address changes
1215 * @bat_priv: the bat priv with all the soft interface information
1216 * @primary_if: the new selected primary_if
1217 * @oldif: the old primary interface, may be NULL
1219 void batadv_bla_update_orig_address(struct batadv_priv *bat_priv,
1220 struct batadv_hard_iface *primary_if,
1221 struct batadv_hard_iface *oldif)
1223 struct batadv_bla_backbone_gw *backbone_gw;
1224 struct hlist_head *head;
1225 struct batadv_hashtable *hash;
1229 /* reset bridge loop avoidance group id */
1230 group = htons(crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN));
1231 bat_priv->bla.claim_dest.group = group;
1233 /* purge everything when bridge loop avoidance is turned off */
1234 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1238 batadv_bla_purge_claims(bat_priv, NULL, 1);
1239 batadv_bla_purge_backbone_gw(bat_priv, 1);
1243 hash = bat_priv->bla.backbone_hash;
1247 for (i = 0; i < hash->size; i++) {
1248 head = &hash->table[i];
1251 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
1252 /* own orig still holds the old value. */
1253 if (!batadv_compare_eth(backbone_gw->orig,
1254 oldif->net_dev->dev_addr))
1257 ether_addr_copy(backbone_gw->orig,
1258 primary_if->net_dev->dev_addr);
1259 /* send an announce frame so others will ask for our
1260 * claims and update their tables.
1262 batadv_bla_send_announce(bat_priv, backbone_gw);
1269 * batadv_bla_status_update - purge bla interfaces if necessary
1270 * @net_dev: the soft interface net device
1272 void batadv_bla_status_update(struct net_device *net_dev)
1274 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1275 struct batadv_hard_iface *primary_if;
1277 primary_if = batadv_primary_if_get_selected(bat_priv);
1281 /* this function already purges everything when bla is disabled,
1282 * so just call that one.
1284 batadv_bla_update_orig_address(bat_priv, primary_if, primary_if);
1285 batadv_hardif_free_ref(primary_if);
1289 * batadv_bla_periodic_work - performs periodic bla work
1290 * @work: kernel work struct
1292 * periodic work to do:
1293 * * purge structures when they are too old
1294 * * send announcements
1296 static void batadv_bla_periodic_work(struct work_struct *work)
1298 struct delayed_work *delayed_work;
1299 struct batadv_priv *bat_priv;
1300 struct batadv_priv_bla *priv_bla;
1301 struct hlist_head *head;
1302 struct batadv_bla_backbone_gw *backbone_gw;
1303 struct batadv_hashtable *hash;
1304 struct batadv_hard_iface *primary_if;
1307 delayed_work = container_of(work, struct delayed_work, work);
1308 priv_bla = container_of(delayed_work, struct batadv_priv_bla, work);
1309 bat_priv = container_of(priv_bla, struct batadv_priv, bla);
1310 primary_if = batadv_primary_if_get_selected(bat_priv);
1314 batadv_bla_purge_claims(bat_priv, primary_if, 0);
1315 batadv_bla_purge_backbone_gw(bat_priv, 0);
1317 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1320 hash = bat_priv->bla.backbone_hash;
1324 for (i = 0; i < hash->size; i++) {
1325 head = &hash->table[i];
1328 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
1329 if (!batadv_compare_eth(backbone_gw->orig,
1330 primary_if->net_dev->dev_addr))
1333 backbone_gw->lasttime = jiffies;
1335 batadv_bla_send_announce(bat_priv, backbone_gw);
1337 /* request_sent is only set after creation to avoid
1338 * problems when we are not yet known as backbone gw
1341 * We can reset this now after we waited some periods
1342 * to give bridge forward delays and bla group forming
1346 if (atomic_read(&backbone_gw->request_sent) == 0)
1349 if (!atomic_dec_and_test(&backbone_gw->wait_periods))
1352 atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
1353 atomic_set(&backbone_gw->request_sent, 0);
1359 batadv_hardif_free_ref(primary_if);
1361 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1362 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
1365 /* The hash for claim and backbone hash receive the same key because they
1366 * are getting initialized by hash_new with the same key. Reinitializing
1367 * them with to different keys to allow nested locking without generating
1370 static struct lock_class_key batadv_claim_hash_lock_class_key;
1371 static struct lock_class_key batadv_backbone_hash_lock_class_key;
1374 * batadv_bla_init - initialize all bla structures
1375 * @bat_priv: the bat priv with all the soft interface information
1377 * Return: 0 on success, < 0 on error.
1379 int batadv_bla_init(struct batadv_priv *bat_priv)
1382 u8 claim_dest[ETH_ALEN] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00};
1383 struct batadv_hard_iface *primary_if;
1385 unsigned long entrytime;
1387 spin_lock_init(&bat_priv->bla.bcast_duplist_lock);
1389 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hash registering\n");
1391 /* setting claim destination address */
1392 memcpy(&bat_priv->bla.claim_dest.magic, claim_dest, 3);
1393 bat_priv->bla.claim_dest.type = 0;
1394 primary_if = batadv_primary_if_get_selected(bat_priv);
1396 crc = crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN);
1397 bat_priv->bla.claim_dest.group = htons(crc);
1398 batadv_hardif_free_ref(primary_if);
1400 bat_priv->bla.claim_dest.group = 0; /* will be set later */
1403 /* initialize the duplicate list */
1404 entrytime = jiffies - msecs_to_jiffies(BATADV_DUPLIST_TIMEOUT);
1405 for (i = 0; i < BATADV_DUPLIST_SIZE; i++)
1406 bat_priv->bla.bcast_duplist[i].entrytime = entrytime;
1407 bat_priv->bla.bcast_duplist_curr = 0;
1409 if (bat_priv->bla.claim_hash)
1412 bat_priv->bla.claim_hash = batadv_hash_new(128);
1413 bat_priv->bla.backbone_hash = batadv_hash_new(32);
1415 if (!bat_priv->bla.claim_hash || !bat_priv->bla.backbone_hash)
1418 batadv_hash_set_lock_class(bat_priv->bla.claim_hash,
1419 &batadv_claim_hash_lock_class_key);
1420 batadv_hash_set_lock_class(bat_priv->bla.backbone_hash,
1421 &batadv_backbone_hash_lock_class_key);
1423 batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hashes initialized\n");
1425 INIT_DELAYED_WORK(&bat_priv->bla.work, batadv_bla_periodic_work);
1427 queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1428 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
1433 * batadv_bla_check_bcast_duplist - Check if a frame is in the broadcast dup.
1434 * @bat_priv: the bat priv with all the soft interface information
1435 * @skb: contains the bcast_packet to be checked
1437 * check if it is on our broadcast list. Another gateway might
1438 * have sent the same packet because it is connected to the same backbone,
1439 * so we have to remove this duplicate.
1441 * This is performed by checking the CRC, which will tell us
1442 * with a good chance that it is the same packet. If it is furthermore
1443 * sent by another host, drop it. We allow equal packets from
1444 * the same host however as this might be intended.
1446 * Return: 1 if a packet is in the duplicate list, 0 otherwise.
1448 int batadv_bla_check_bcast_duplist(struct batadv_priv *bat_priv,
1449 struct sk_buff *skb)
1451 int i, curr, ret = 0;
1453 struct batadv_bcast_packet *bcast_packet;
1454 struct batadv_bcast_duplist_entry *entry;
1456 bcast_packet = (struct batadv_bcast_packet *)skb->data;
1458 /* calculate the crc ... */
1459 crc = batadv_skb_crc32(skb, (u8 *)(bcast_packet + 1));
1461 spin_lock_bh(&bat_priv->bla.bcast_duplist_lock);
1463 for (i = 0; i < BATADV_DUPLIST_SIZE; i++) {
1464 curr = (bat_priv->bla.bcast_duplist_curr + i);
1465 curr %= BATADV_DUPLIST_SIZE;
1466 entry = &bat_priv->bla.bcast_duplist[curr];
1468 /* we can stop searching if the entry is too old ;
1469 * later entries will be even older
1471 if (batadv_has_timed_out(entry->entrytime,
1472 BATADV_DUPLIST_TIMEOUT))
1475 if (entry->crc != crc)
1478 if (batadv_compare_eth(entry->orig, bcast_packet->orig))
1481 /* this entry seems to match: same crc, not too old,
1482 * and from another gw. therefore return 1 to forbid it.
1487 /* not found, add a new entry (overwrite the oldest entry)
1488 * and allow it, its the first occurrence.
1490 curr = (bat_priv->bla.bcast_duplist_curr + BATADV_DUPLIST_SIZE - 1);
1491 curr %= BATADV_DUPLIST_SIZE;
1492 entry = &bat_priv->bla.bcast_duplist[curr];
1494 entry->entrytime = jiffies;
1495 ether_addr_copy(entry->orig, bcast_packet->orig);
1496 bat_priv->bla.bcast_duplist_curr = curr;
1499 spin_unlock_bh(&bat_priv->bla.bcast_duplist_lock);
1505 * batadv_bla_is_backbone_gw_orig - Check if the originator is a gateway for
1506 * the VLAN identified by vid.
1507 * @bat_priv: the bat priv with all the soft interface information
1508 * @orig: originator mac address
1509 * @vid: VLAN identifier
1511 * Return: true if orig is a backbone for this vid, false otherwise.
1513 bool batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, u8 *orig,
1516 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
1517 struct hlist_head *head;
1518 struct batadv_bla_backbone_gw *backbone_gw;
1521 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1527 for (i = 0; i < hash->size; i++) {
1528 head = &hash->table[i];
1531 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
1532 if (batadv_compare_eth(backbone_gw->orig, orig) &&
1533 backbone_gw->vid == vid) {
1545 * batadv_bla_is_backbone_gw - check if originator is a backbone gw for a VLAN.
1546 * @skb: the frame to be checked
1547 * @orig_node: the orig_node of the frame
1548 * @hdr_size: maximum length of the frame
1550 * Return: 1 if the orig_node is also a gateway on the soft interface, otherwise
1553 int batadv_bla_is_backbone_gw(struct sk_buff *skb,
1554 struct batadv_orig_node *orig_node, int hdr_size)
1556 struct batadv_bla_backbone_gw *backbone_gw;
1559 if (!atomic_read(&orig_node->bat_priv->bridge_loop_avoidance))
1562 /* first, find out the vid. */
1563 if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
1566 vid = batadv_get_vid(skb, hdr_size);
1568 /* see if this originator is a backbone gw for this VLAN */
1569 backbone_gw = batadv_backbone_hash_find(orig_node->bat_priv,
1570 orig_node->orig, vid);
1574 batadv_backbone_gw_free_ref(backbone_gw);
1579 * batadv_bla_init - free all bla structures
1580 * @bat_priv: the bat priv with all the soft interface information
1582 * for softinterface free or module unload
1584 void batadv_bla_free(struct batadv_priv *bat_priv)
1586 struct batadv_hard_iface *primary_if;
1588 cancel_delayed_work_sync(&bat_priv->bla.work);
1589 primary_if = batadv_primary_if_get_selected(bat_priv);
1591 if (bat_priv->bla.claim_hash) {
1592 batadv_bla_purge_claims(bat_priv, primary_if, 1);
1593 batadv_hash_destroy(bat_priv->bla.claim_hash);
1594 bat_priv->bla.claim_hash = NULL;
1596 if (bat_priv->bla.backbone_hash) {
1597 batadv_bla_purge_backbone_gw(bat_priv, 1);
1598 batadv_hash_destroy(bat_priv->bla.backbone_hash);
1599 bat_priv->bla.backbone_hash = NULL;
1602 batadv_hardif_free_ref(primary_if);
1606 * batadv_bla_rx - check packets coming from the mesh.
1607 * @bat_priv: the bat priv with all the soft interface information
1608 * @skb: the frame to be checked
1609 * @vid: the VLAN ID of the frame
1610 * @is_bcast: the packet came in a broadcast packet type.
1612 * batadv_bla_rx avoidance checks if:
1613 * * we have to race for a claim
1614 * * if the frame is allowed on the LAN
1616 * in these cases, the skb is further handled by this function
1618 * Return: 1 if handled, otherwise it returns 0 and the caller shall further
1621 int batadv_bla_rx(struct batadv_priv *bat_priv, struct sk_buff *skb,
1622 unsigned short vid, bool is_bcast)
1624 struct ethhdr *ethhdr;
1625 struct batadv_bla_claim search_claim, *claim = NULL;
1626 struct batadv_hard_iface *primary_if;
1629 ethhdr = eth_hdr(skb);
1631 primary_if = batadv_primary_if_get_selected(bat_priv);
1635 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1638 if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
1639 /* don't allow broadcasts while requests are in flight */
1640 if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast)
1643 ether_addr_copy(search_claim.addr, ethhdr->h_source);
1644 search_claim.vid = vid;
1645 claim = batadv_claim_hash_find(bat_priv, &search_claim);
1648 /* possible optimization: race for a claim */
1649 /* No claim exists yet, claim it for us!
1651 batadv_handle_claim(bat_priv, primary_if,
1652 primary_if->net_dev->dev_addr,
1653 ethhdr->h_source, vid);
1657 /* if it is our own claim ... */
1658 if (batadv_compare_eth(claim->backbone_gw->orig,
1659 primary_if->net_dev->dev_addr)) {
1660 /* ... allow it in any case */
1661 claim->lasttime = jiffies;
1665 /* if it is a broadcast ... */
1666 if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast) {
1667 /* ... drop it. the responsible gateway is in charge.
1669 * We need to check is_bcast because with the gateway
1670 * feature, broadcasts (like DHCP requests) may be sent
1671 * using a unicast packet type.
1675 /* seems the client considers us as its best gateway.
1676 * send a claim and update the claim table
1679 batadv_handle_claim(bat_priv, primary_if,
1680 primary_if->net_dev->dev_addr,
1681 ethhdr->h_source, vid);
1685 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
1695 batadv_hardif_free_ref(primary_if);
1697 batadv_claim_free_ref(claim);
1702 * batadv_bla_tx - check packets going into the mesh
1703 * @bat_priv: the bat priv with all the soft interface information
1704 * @skb: the frame to be checked
1705 * @vid: the VLAN ID of the frame
1707 * batadv_bla_tx checks if:
1708 * * a claim was received which has to be processed
1709 * * the frame is allowed on the mesh
1711 * in these cases, the skb is further handled by this function.
1713 * This call might reallocate skb data.
1715 * Return: 1 if handled, otherwise it returns 0 and the caller shall further
1718 int batadv_bla_tx(struct batadv_priv *bat_priv, struct sk_buff *skb,
1721 struct ethhdr *ethhdr;
1722 struct batadv_bla_claim search_claim, *claim = NULL;
1723 struct batadv_hard_iface *primary_if;
1726 primary_if = batadv_primary_if_get_selected(bat_priv);
1730 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1733 if (batadv_bla_process_claim(bat_priv, primary_if, skb))
1736 ethhdr = eth_hdr(skb);
1738 if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
1739 /* don't allow broadcasts while requests are in flight */
1740 if (is_multicast_ether_addr(ethhdr->h_dest))
1743 ether_addr_copy(search_claim.addr, ethhdr->h_source);
1744 search_claim.vid = vid;
1746 claim = batadv_claim_hash_find(bat_priv, &search_claim);
1748 /* if no claim exists, allow it. */
1752 /* check if we are responsible. */
1753 if (batadv_compare_eth(claim->backbone_gw->orig,
1754 primary_if->net_dev->dev_addr)) {
1755 /* if yes, the client has roamed and we have
1758 batadv_handle_unclaim(bat_priv, primary_if,
1759 primary_if->net_dev->dev_addr,
1760 ethhdr->h_source, vid);
1764 /* check if it is a multicast/broadcast frame */
1765 if (is_multicast_ether_addr(ethhdr->h_dest)) {
1766 /* drop it. the responsible gateway has forwarded it into
1767 * the backbone network.
1771 /* we must allow it. at least if we are
1772 * responsible for the DESTINATION.
1777 batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
1784 batadv_hardif_free_ref(primary_if);
1786 batadv_claim_free_ref(claim);
1791 * batadv_bla_claim_table_seq_print_text - print the claim table in a seq file
1792 * @seq: seq file to print on
1797 int batadv_bla_claim_table_seq_print_text(struct seq_file *seq, void *offset)
1799 struct net_device *net_dev = (struct net_device *)seq->private;
1800 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1801 struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
1802 struct batadv_bla_claim *claim;
1803 struct batadv_hard_iface *primary_if;
1804 struct hlist_head *head;
1810 primary_if = batadv_seq_print_text_primary_if_get(seq);
1814 primary_addr = primary_if->net_dev->dev_addr;
1816 "Claims announced for the mesh %s (orig %pM, group id %#.4x)\n",
1817 net_dev->name, primary_addr,
1818 ntohs(bat_priv->bla.claim_dest.group));
1819 seq_printf(seq, " %-17s %-5s %-17s [o] (%-6s)\n",
1820 "Client", "VID", "Originator", "CRC");
1821 for (i = 0; i < hash->size; i++) {
1822 head = &hash->table[i];
1825 hlist_for_each_entry_rcu(claim, head, hash_entry) {
1826 is_own = batadv_compare_eth(claim->backbone_gw->orig,
1829 spin_lock_bh(&claim->backbone_gw->crc_lock);
1830 backbone_crc = claim->backbone_gw->crc;
1831 spin_unlock_bh(&claim->backbone_gw->crc_lock);
1832 seq_printf(seq, " * %pM on %5d by %pM [%c] (%#.4x)\n",
1833 claim->addr, BATADV_PRINT_VID(claim->vid),
1834 claim->backbone_gw->orig,
1835 (is_own ? 'x' : ' '),
1842 batadv_hardif_free_ref(primary_if);
1847 * batadv_bla_backbone_table_seq_print_text - print the backbone table in a seq
1849 * @seq: seq file to print on
1854 int batadv_bla_backbone_table_seq_print_text(struct seq_file *seq, void *offset)
1856 struct net_device *net_dev = (struct net_device *)seq->private;
1857 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1858 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
1859 struct batadv_bla_backbone_gw *backbone_gw;
1860 struct batadv_hard_iface *primary_if;
1861 struct hlist_head *head;
1868 primary_if = batadv_seq_print_text_primary_if_get(seq);
1872 primary_addr = primary_if->net_dev->dev_addr;
1874 "Backbones announced for the mesh %s (orig %pM, group id %#.4x)\n",
1875 net_dev->name, primary_addr,
1876 ntohs(bat_priv->bla.claim_dest.group));
1877 seq_printf(seq, " %-17s %-5s %-9s (%-6s)\n",
1878 "Originator", "VID", "last seen", "CRC");
1879 for (i = 0; i < hash->size; i++) {
1880 head = &hash->table[i];
1883 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
1884 msecs = jiffies_to_msecs(jiffies -
1885 backbone_gw->lasttime);
1886 secs = msecs / 1000;
1887 msecs = msecs % 1000;
1889 is_own = batadv_compare_eth(backbone_gw->orig,
1894 spin_lock_bh(&backbone_gw->crc_lock);
1895 backbone_crc = backbone_gw->crc;
1896 spin_unlock_bh(&backbone_gw->crc_lock);
1898 seq_printf(seq, " * %pM on %5d %4i.%03is (%#.4x)\n",
1900 BATADV_PRINT_VID(backbone_gw->vid), secs,
1901 msecs, backbone_crc);
1907 batadv_hardif_free_ref(primary_if);