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"
54 #include "translation-table.h"
56 static const u8 batadv_announce_mac
[4] = {0x43, 0x05, 0x43, 0x05};
58 static void batadv_bla_periodic_work(struct work_struct
*work
);
60 batadv_bla_send_announce(struct batadv_priv
*bat_priv
,
61 struct batadv_bla_backbone_gw
*backbone_gw
);
64 * batadv_choose_claim - choose the right bucket for a claim.
66 * @size: size of the hash table
68 * Return: the hash index of the claim
70 static inline u32
batadv_choose_claim(const void *data
, u32 size
)
72 struct batadv_bla_claim
*claim
= (struct batadv_bla_claim
*)data
;
75 hash
= jhash(&claim
->addr
, sizeof(claim
->addr
), hash
);
76 hash
= jhash(&claim
->vid
, sizeof(claim
->vid
), hash
);
82 * batadv_choose_backbone_gw - choose the right bucket for a backbone gateway.
84 * @size: size of the hash table
86 * Return: the hash index of the backbone gateway
88 static inline u32
batadv_choose_backbone_gw(const void *data
, u32 size
)
90 const struct batadv_bla_claim
*claim
= (struct batadv_bla_claim
*)data
;
93 hash
= jhash(&claim
->addr
, sizeof(claim
->addr
), hash
);
94 hash
= jhash(&claim
->vid
, sizeof(claim
->vid
), hash
);
100 * batadv_compare_backbone_gw - compare address and vid of two backbone gws
101 * @node: list node of the first entry to compare
102 * @data2: pointer to the second backbone gateway
104 * Return: true if the backbones have the same data, false otherwise
106 static bool batadv_compare_backbone_gw(const struct hlist_node
*node
,
109 const void *data1
= container_of(node
, struct batadv_bla_backbone_gw
,
111 const struct batadv_bla_backbone_gw
*gw1
= data1
;
112 const struct batadv_bla_backbone_gw
*gw2
= data2
;
114 if (!batadv_compare_eth(gw1
->orig
, gw2
->orig
))
117 if (gw1
->vid
!= gw2
->vid
)
124 * batadv_compare_claim - compare address and vid of two claims
125 * @node: list node of the first entry to compare
126 * @data2: pointer to the second claims
128 * Return: true if the claim have the same data, 0 otherwise
130 static bool batadv_compare_claim(const struct hlist_node
*node
,
133 const void *data1
= container_of(node
, struct batadv_bla_claim
,
135 const struct batadv_bla_claim
*cl1
= data1
;
136 const struct batadv_bla_claim
*cl2
= data2
;
138 if (!batadv_compare_eth(cl1
->addr
, cl2
->addr
))
141 if (cl1
->vid
!= cl2
->vid
)
148 * batadv_backbone_gw_release - release backbone gw from lists and queue for
149 * free after rcu grace period
150 * @ref: kref pointer of the backbone gw
152 static void batadv_backbone_gw_release(struct kref
*ref
)
154 struct batadv_bla_backbone_gw
*backbone_gw
;
156 backbone_gw
= container_of(ref
, struct batadv_bla_backbone_gw
,
159 kfree_rcu(backbone_gw
, rcu
);
163 * batadv_backbone_gw_put - decrement the backbone gw refcounter and possibly
165 * @backbone_gw: backbone gateway to be free'd
167 static void batadv_backbone_gw_put(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_put(claim
->backbone_gw
);
184 kfree_rcu(claim
, rcu
);
188 * batadv_claim_put - decrement the claim refcounter and possibly
190 * @claim: claim to be free'd
192 static void batadv_claim_put(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_put(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
));
411 case BATADV_CLAIM_TYPE_LOOPDETECT
:
412 ether_addr_copy(ethhdr
->h_source
, mac
);
413 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
414 "bla_send_claim(): LOOPDETECT of %pM to %pM on vid %d\n",
415 ethhdr
->h_source
, ethhdr
->h_dest
,
416 BATADV_PRINT_VID(vid
));
421 if (vid
& BATADV_VLAN_HAS_TAG
)
422 skb
= vlan_insert_tag(skb
, htons(ETH_P_8021Q
),
423 vid
& VLAN_VID_MASK
);
425 skb_reset_mac_header(skb
);
426 skb
->protocol
= eth_type_trans(skb
, soft_iface
);
427 batadv_inc_counter(bat_priv
, BATADV_CNT_RX
);
428 batadv_add_counter(bat_priv
, BATADV_CNT_RX_BYTES
,
429 skb
->len
+ ETH_HLEN
);
430 soft_iface
->last_rx
= jiffies
;
435 batadv_hardif_put(primary_if
);
439 * batadv_bla_loopdetect_report - worker for reporting the loop
440 * @work: work queue item
442 * Throws an uevent, as the loopdetect check function can't do that itself
443 * since the kernel may sleep while throwing uevents.
445 static void batadv_bla_loopdetect_report(struct work_struct
*work
)
447 struct batadv_bla_backbone_gw
*backbone_gw
;
448 struct batadv_priv
*bat_priv
;
449 char vid_str
[6] = { '\0' };
451 backbone_gw
= container_of(work
, struct batadv_bla_backbone_gw
,
453 bat_priv
= backbone_gw
->bat_priv
;
455 batadv_info(bat_priv
->soft_iface
,
456 "Possible loop on VLAN %d detected which can't be handled by BLA - please check your network setup!\n",
457 BATADV_PRINT_VID(backbone_gw
->vid
));
458 snprintf(vid_str
, sizeof(vid_str
), "%d",
459 BATADV_PRINT_VID(backbone_gw
->vid
));
460 vid_str
[sizeof(vid_str
) - 1] = 0;
462 batadv_throw_uevent(bat_priv
, BATADV_UEV_BLA
, BATADV_UEV_LOOPDETECT
,
465 batadv_backbone_gw_put(backbone_gw
);
469 * batadv_bla_get_backbone_gw - finds or creates a backbone gateway
470 * @bat_priv: the bat priv with all the soft interface information
471 * @orig: the mac address of the originator
473 * @own_backbone: set if the requested backbone is local
475 * Return: the (possibly created) backbone gateway or NULL on error
477 static struct batadv_bla_backbone_gw
*
478 batadv_bla_get_backbone_gw(struct batadv_priv
*bat_priv
, u8
*orig
,
479 unsigned short vid
, bool own_backbone
)
481 struct batadv_bla_backbone_gw
*entry
;
482 struct batadv_orig_node
*orig_node
;
485 entry
= batadv_backbone_hash_find(bat_priv
, orig
, vid
);
490 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
491 "bla_get_backbone_gw(): not found (%pM, %d), creating new entry\n",
492 orig
, BATADV_PRINT_VID(vid
));
494 entry
= kzalloc(sizeof(*entry
), GFP_ATOMIC
);
499 entry
->lasttime
= jiffies
;
500 entry
->crc
= BATADV_BLA_CRC_INIT
;
501 entry
->bat_priv
= bat_priv
;
502 spin_lock_init(&entry
->crc_lock
);
503 atomic_set(&entry
->request_sent
, 0);
504 atomic_set(&entry
->wait_periods
, 0);
505 ether_addr_copy(entry
->orig
, orig
);
506 INIT_WORK(&entry
->report_work
, batadv_bla_loopdetect_report
);
508 /* one for the hash, one for returning */
509 kref_init(&entry
->refcount
);
510 kref_get(&entry
->refcount
);
512 hash_added
= batadv_hash_add(bat_priv
->bla
.backbone_hash
,
513 batadv_compare_backbone_gw
,
514 batadv_choose_backbone_gw
, entry
,
517 if (unlikely(hash_added
!= 0)) {
518 /* hash failed, free the structure */
523 /* this is a gateway now, remove any TT entry on this VLAN */
524 orig_node
= batadv_orig_hash_find(bat_priv
, orig
);
526 batadv_tt_global_del_orig(bat_priv
, orig_node
, vid
,
527 "became a backbone gateway");
528 batadv_orig_node_put(orig_node
);
532 batadv_bla_send_announce(bat_priv
, entry
);
534 /* this will be decreased in the worker thread */
535 atomic_inc(&entry
->request_sent
);
536 atomic_set(&entry
->wait_periods
, BATADV_BLA_WAIT_PERIODS
);
537 atomic_inc(&bat_priv
->bla
.num_requests
);
544 * batadv_bla_update_own_backbone_gw - updates the own backbone gw for a VLAN
545 * @bat_priv: the bat priv with all the soft interface information
546 * @primary_if: the selected primary interface
547 * @vid: VLAN identifier
549 * update or add the own backbone gw to make sure we announce
550 * where we receive other backbone gws
553 batadv_bla_update_own_backbone_gw(struct batadv_priv
*bat_priv
,
554 struct batadv_hard_iface
*primary_if
,
557 struct batadv_bla_backbone_gw
*backbone_gw
;
559 backbone_gw
= batadv_bla_get_backbone_gw(bat_priv
,
560 primary_if
->net_dev
->dev_addr
,
562 if (unlikely(!backbone_gw
))
565 backbone_gw
->lasttime
= jiffies
;
566 batadv_backbone_gw_put(backbone_gw
);
570 * batadv_bla_answer_request - answer a bla request by sending own claims
571 * @bat_priv: the bat priv with all the soft interface information
572 * @primary_if: interface where the request came on
573 * @vid: the vid where the request came on
575 * Repeat all of our own claims, and finally send an ANNOUNCE frame
576 * to allow the requester another check if the CRC is correct now.
578 static void batadv_bla_answer_request(struct batadv_priv
*bat_priv
,
579 struct batadv_hard_iface
*primary_if
,
582 struct hlist_head
*head
;
583 struct batadv_hashtable
*hash
;
584 struct batadv_bla_claim
*claim
;
585 struct batadv_bla_backbone_gw
*backbone_gw
;
588 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
589 "bla_answer_request(): received a claim request, send all of our own claims again\n");
591 backbone_gw
= batadv_backbone_hash_find(bat_priv
,
592 primary_if
->net_dev
->dev_addr
,
597 hash
= bat_priv
->bla
.claim_hash
;
598 for (i
= 0; i
< hash
->size
; i
++) {
599 head
= &hash
->table
[i
];
602 hlist_for_each_entry_rcu(claim
, head
, hash_entry
) {
603 /* only own claims are interesting */
604 if (claim
->backbone_gw
!= backbone_gw
)
607 batadv_bla_send_claim(bat_priv
, claim
->addr
, claim
->vid
,
608 BATADV_CLAIM_TYPE_CLAIM
);
613 /* finally, send an announcement frame */
614 batadv_bla_send_announce(bat_priv
, backbone_gw
);
615 batadv_backbone_gw_put(backbone_gw
);
619 * batadv_bla_send_request - send a request to repeat claims
620 * @backbone_gw: the backbone gateway from whom we are out of sync
622 * When the crc is wrong, ask the backbone gateway for a full table update.
623 * After the request, it will repeat all of his own claims and finally
624 * send an announcement claim with which we can check again.
626 static void batadv_bla_send_request(struct batadv_bla_backbone_gw
*backbone_gw
)
628 /* first, remove all old entries */
629 batadv_bla_del_backbone_claims(backbone_gw
);
631 batadv_dbg(BATADV_DBG_BLA
, backbone_gw
->bat_priv
,
632 "Sending REQUEST to %pM\n", backbone_gw
->orig
);
635 batadv_bla_send_claim(backbone_gw
->bat_priv
, backbone_gw
->orig
,
636 backbone_gw
->vid
, BATADV_CLAIM_TYPE_REQUEST
);
638 /* no local broadcasts should be sent or received, for now. */
639 if (!atomic_read(&backbone_gw
->request_sent
)) {
640 atomic_inc(&backbone_gw
->bat_priv
->bla
.num_requests
);
641 atomic_set(&backbone_gw
->request_sent
, 1);
646 * batadv_bla_send_announce - Send an announcement frame
647 * @bat_priv: the bat priv with all the soft interface information
648 * @backbone_gw: our backbone gateway which should be announced
650 static void batadv_bla_send_announce(struct batadv_priv
*bat_priv
,
651 struct batadv_bla_backbone_gw
*backbone_gw
)
656 memcpy(mac
, batadv_announce_mac
, 4);
657 spin_lock_bh(&backbone_gw
->crc_lock
);
658 crc
= htons(backbone_gw
->crc
);
659 spin_unlock_bh(&backbone_gw
->crc_lock
);
660 memcpy(&mac
[4], &crc
, 2);
662 batadv_bla_send_claim(bat_priv
, mac
, backbone_gw
->vid
,
663 BATADV_CLAIM_TYPE_ANNOUNCE
);
667 * batadv_bla_add_claim - Adds a claim in the claim hash
668 * @bat_priv: the bat priv with all the soft interface information
669 * @mac: the mac address of the claim
670 * @vid: the VLAN ID of the frame
671 * @backbone_gw: the backbone gateway which claims it
673 static void batadv_bla_add_claim(struct batadv_priv
*bat_priv
,
674 const u8
*mac
, const unsigned short vid
,
675 struct batadv_bla_backbone_gw
*backbone_gw
)
677 struct batadv_bla_claim
*claim
;
678 struct batadv_bla_claim search_claim
;
681 ether_addr_copy(search_claim
.addr
, mac
);
682 search_claim
.vid
= vid
;
683 claim
= batadv_claim_hash_find(bat_priv
, &search_claim
);
685 /* create a new claim entry if it does not exist yet. */
687 claim
= kzalloc(sizeof(*claim
), GFP_ATOMIC
);
691 ether_addr_copy(claim
->addr
, mac
);
693 claim
->lasttime
= jiffies
;
694 claim
->backbone_gw
= backbone_gw
;
696 kref_init(&claim
->refcount
);
697 kref_get(&claim
->refcount
);
698 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
699 "bla_add_claim(): adding new entry %pM, vid %d to hash ...\n",
700 mac
, BATADV_PRINT_VID(vid
));
701 hash_added
= batadv_hash_add(bat_priv
->bla
.claim_hash
,
702 batadv_compare_claim
,
703 batadv_choose_claim
, claim
,
706 if (unlikely(hash_added
!= 0)) {
707 /* only local changes happened. */
712 claim
->lasttime
= jiffies
;
713 if (claim
->backbone_gw
== backbone_gw
)
714 /* no need to register a new backbone */
717 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
718 "bla_add_claim(): changing ownership for %pM, vid %d\n",
719 mac
, BATADV_PRINT_VID(vid
));
721 spin_lock_bh(&claim
->backbone_gw
->crc_lock
);
722 claim
->backbone_gw
->crc
^= crc16(0, claim
->addr
, ETH_ALEN
);
723 spin_unlock_bh(&claim
->backbone_gw
->crc_lock
);
724 batadv_backbone_gw_put(claim
->backbone_gw
);
726 /* set (new) backbone gw */
727 kref_get(&backbone_gw
->refcount
);
728 claim
->backbone_gw
= backbone_gw
;
730 spin_lock_bh(&backbone_gw
->crc_lock
);
731 backbone_gw
->crc
^= crc16(0, claim
->addr
, ETH_ALEN
);
732 spin_unlock_bh(&backbone_gw
->crc_lock
);
733 backbone_gw
->lasttime
= jiffies
;
736 batadv_claim_put(claim
);
740 * batadv_bla_del_claim - delete a claim from the claim hash
741 * @bat_priv: the bat priv with all the soft interface information
742 * @mac: mac address of the claim to be removed
743 * @vid: VLAN id for the claim to be removed
745 static void batadv_bla_del_claim(struct batadv_priv
*bat_priv
,
746 const u8
*mac
, const unsigned short vid
)
748 struct batadv_bla_claim search_claim
, *claim
;
750 ether_addr_copy(search_claim
.addr
, mac
);
751 search_claim
.vid
= vid
;
752 claim
= batadv_claim_hash_find(bat_priv
, &search_claim
);
756 batadv_dbg(BATADV_DBG_BLA
, bat_priv
, "bla_del_claim(): %pM, vid %d\n",
757 mac
, BATADV_PRINT_VID(vid
));
759 batadv_hash_remove(bat_priv
->bla
.claim_hash
, batadv_compare_claim
,
760 batadv_choose_claim
, claim
);
761 batadv_claim_put(claim
); /* reference from the hash is gone */
763 spin_lock_bh(&claim
->backbone_gw
->crc_lock
);
764 claim
->backbone_gw
->crc
^= crc16(0, claim
->addr
, ETH_ALEN
);
765 spin_unlock_bh(&claim
->backbone_gw
->crc_lock
);
767 /* don't need the reference from hash_find() anymore */
768 batadv_claim_put(claim
);
772 * batadv_handle_announce - check for ANNOUNCE frame
773 * @bat_priv: the bat priv with all the soft interface information
774 * @an_addr: announcement mac address (ARP Sender HW address)
775 * @backbone_addr: originator address of the sender (Ethernet source MAC)
776 * @vid: the VLAN ID of the frame
778 * Return: true if handled
780 static bool batadv_handle_announce(struct batadv_priv
*bat_priv
, u8
*an_addr
,
781 u8
*backbone_addr
, unsigned short vid
)
783 struct batadv_bla_backbone_gw
*backbone_gw
;
784 u16 backbone_crc
, crc
;
786 if (memcmp(an_addr
, batadv_announce_mac
, 4) != 0)
789 backbone_gw
= batadv_bla_get_backbone_gw(bat_priv
, backbone_addr
, vid
,
792 if (unlikely(!backbone_gw
))
795 /* handle as ANNOUNCE frame */
796 backbone_gw
->lasttime
= jiffies
;
797 crc
= ntohs(*((__be16
*)(&an_addr
[4])));
799 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
800 "handle_announce(): ANNOUNCE vid %d (sent by %pM)... CRC = %#.4x\n",
801 BATADV_PRINT_VID(vid
), backbone_gw
->orig
, crc
);
803 spin_lock_bh(&backbone_gw
->crc_lock
);
804 backbone_crc
= backbone_gw
->crc
;
805 spin_unlock_bh(&backbone_gw
->crc_lock
);
807 if (backbone_crc
!= crc
) {
808 batadv_dbg(BATADV_DBG_BLA
, backbone_gw
->bat_priv
,
809 "handle_announce(): CRC FAILED for %pM/%d (my = %#.4x, sent = %#.4x)\n",
811 BATADV_PRINT_VID(backbone_gw
->vid
),
814 batadv_bla_send_request(backbone_gw
);
816 /* if we have sent a request and the crc was OK,
817 * we can allow traffic again.
819 if (atomic_read(&backbone_gw
->request_sent
)) {
820 atomic_dec(&backbone_gw
->bat_priv
->bla
.num_requests
);
821 atomic_set(&backbone_gw
->request_sent
, 0);
825 batadv_backbone_gw_put(backbone_gw
);
830 * batadv_handle_request - check for REQUEST frame
831 * @bat_priv: the bat priv with all the soft interface information
832 * @primary_if: the primary hard interface of this batman soft interface
833 * @backbone_addr: backbone address to be requested (ARP sender HW MAC)
834 * @ethhdr: ethernet header of a packet
835 * @vid: the VLAN ID of the frame
837 * Return: true if handled
839 static bool batadv_handle_request(struct batadv_priv
*bat_priv
,
840 struct batadv_hard_iface
*primary_if
,
841 u8
*backbone_addr
, struct ethhdr
*ethhdr
,
844 /* check for REQUEST frame */
845 if (!batadv_compare_eth(backbone_addr
, ethhdr
->h_dest
))
848 /* sanity check, this should not happen on a normal switch,
849 * we ignore it in this case.
851 if (!batadv_compare_eth(ethhdr
->h_dest
, primary_if
->net_dev
->dev_addr
))
854 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
855 "handle_request(): REQUEST vid %d (sent by %pM)...\n",
856 BATADV_PRINT_VID(vid
), ethhdr
->h_source
);
858 batadv_bla_answer_request(bat_priv
, primary_if
, vid
);
863 * batadv_handle_unclaim - check for UNCLAIM frame
864 * @bat_priv: the bat priv with all the soft interface information
865 * @primary_if: the primary hard interface of this batman soft interface
866 * @backbone_addr: originator address of the backbone (Ethernet source)
867 * @claim_addr: Client to be unclaimed (ARP sender HW MAC)
868 * @vid: the VLAN ID of the frame
870 * Return: true if handled
872 static bool batadv_handle_unclaim(struct batadv_priv
*bat_priv
,
873 struct batadv_hard_iface
*primary_if
,
874 u8
*backbone_addr
, u8
*claim_addr
,
877 struct batadv_bla_backbone_gw
*backbone_gw
;
879 /* unclaim in any case if it is our own */
880 if (primary_if
&& batadv_compare_eth(backbone_addr
,
881 primary_if
->net_dev
->dev_addr
))
882 batadv_bla_send_claim(bat_priv
, claim_addr
, vid
,
883 BATADV_CLAIM_TYPE_UNCLAIM
);
885 backbone_gw
= batadv_backbone_hash_find(bat_priv
, backbone_addr
, vid
);
890 /* this must be an UNCLAIM frame */
891 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
892 "handle_unclaim(): UNCLAIM %pM on vid %d (sent by %pM)...\n",
893 claim_addr
, BATADV_PRINT_VID(vid
), backbone_gw
->orig
);
895 batadv_bla_del_claim(bat_priv
, claim_addr
, vid
);
896 batadv_backbone_gw_put(backbone_gw
);
901 * batadv_handle_claim - check for CLAIM frame
902 * @bat_priv: the bat priv with all the soft interface information
903 * @primary_if: the primary hard interface of this batman soft interface
904 * @backbone_addr: originator address of the backbone (Ethernet Source)
905 * @claim_addr: client mac address to be claimed (ARP sender HW MAC)
906 * @vid: the VLAN ID of the frame
908 * Return: true if handled
910 static bool batadv_handle_claim(struct batadv_priv
*bat_priv
,
911 struct batadv_hard_iface
*primary_if
,
912 u8
*backbone_addr
, u8
*claim_addr
,
915 struct batadv_bla_backbone_gw
*backbone_gw
;
917 /* register the gateway if not yet available, and add the claim. */
919 backbone_gw
= batadv_bla_get_backbone_gw(bat_priv
, backbone_addr
, vid
,
922 if (unlikely(!backbone_gw
))
925 /* this must be a CLAIM frame */
926 batadv_bla_add_claim(bat_priv
, claim_addr
, vid
, backbone_gw
);
927 if (batadv_compare_eth(backbone_addr
, primary_if
->net_dev
->dev_addr
))
928 batadv_bla_send_claim(bat_priv
, claim_addr
, vid
,
929 BATADV_CLAIM_TYPE_CLAIM
);
931 /* TODO: we could call something like tt_local_del() here. */
933 batadv_backbone_gw_put(backbone_gw
);
938 * batadv_check_claim_group - check for claim group membership
939 * @bat_priv: the bat priv with all the soft interface information
940 * @primary_if: the primary interface of this batman interface
941 * @hw_src: the Hardware source in the ARP Header
942 * @hw_dst: the Hardware destination in the ARP Header
943 * @ethhdr: pointer to the Ethernet header of the claim frame
945 * checks if it is a claim packet and if its on the same group.
946 * This function also applies the group ID of the sender
947 * if it is in the same mesh.
950 * 2 - if it is a claim packet and on the same group
951 * 1 - if is a claim packet from another group
952 * 0 - if it is not a claim packet
954 static int batadv_check_claim_group(struct batadv_priv
*bat_priv
,
955 struct batadv_hard_iface
*primary_if
,
956 u8
*hw_src
, u8
*hw_dst
,
957 struct ethhdr
*ethhdr
)
960 struct batadv_orig_node
*orig_node
;
961 struct batadv_bla_claim_dst
*bla_dst
, *bla_dst_own
;
963 bla_dst
= (struct batadv_bla_claim_dst
*)hw_dst
;
964 bla_dst_own
= &bat_priv
->bla
.claim_dest
;
966 /* if announcement packet, use the source,
967 * otherwise assume it is in the hw_src
969 switch (bla_dst
->type
) {
970 case BATADV_CLAIM_TYPE_CLAIM
:
971 backbone_addr
= hw_src
;
973 case BATADV_CLAIM_TYPE_REQUEST
:
974 case BATADV_CLAIM_TYPE_ANNOUNCE
:
975 case BATADV_CLAIM_TYPE_UNCLAIM
:
976 backbone_addr
= ethhdr
->h_source
;
982 /* don't accept claim frames from ourselves */
983 if (batadv_compare_eth(backbone_addr
, primary_if
->net_dev
->dev_addr
))
986 /* if its already the same group, it is fine. */
987 if (bla_dst
->group
== bla_dst_own
->group
)
990 /* lets see if this originator is in our mesh */
991 orig_node
= batadv_orig_hash_find(bat_priv
, backbone_addr
);
993 /* dont accept claims from gateways which are not in
994 * the same mesh or group.
999 /* if our mesh friends mac is bigger, use it for ourselves. */
1000 if (ntohs(bla_dst
->group
) > ntohs(bla_dst_own
->group
)) {
1001 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
1002 "taking other backbones claim group: %#.4x\n",
1003 ntohs(bla_dst
->group
));
1004 bla_dst_own
->group
= bla_dst
->group
;
1007 batadv_orig_node_put(orig_node
);
1013 * batadv_bla_process_claim - Check if this is a claim frame, and process it
1014 * @bat_priv: the bat priv with all the soft interface information
1015 * @primary_if: the primary hard interface of this batman soft interface
1016 * @skb: the frame to be checked
1018 * Return: true if it was a claim frame, otherwise return false to
1019 * tell the callee that it can use the frame on its own.
1021 static bool batadv_bla_process_claim(struct batadv_priv
*bat_priv
,
1022 struct batadv_hard_iface
*primary_if
,
1023 struct sk_buff
*skb
)
1025 struct batadv_bla_claim_dst
*bla_dst
, *bla_dst_own
;
1026 u8
*hw_src
, *hw_dst
;
1027 struct vlan_hdr
*vhdr
, vhdr_buf
;
1028 struct ethhdr
*ethhdr
;
1029 struct arphdr
*arphdr
;
1036 vid
= batadv_get_vid(skb
, 0);
1037 ethhdr
= eth_hdr(skb
);
1039 proto
= ethhdr
->h_proto
;
1041 if (vid
& BATADV_VLAN_HAS_TAG
) {
1042 /* Traverse the VLAN/Ethertypes.
1044 * At this point it is known that the first protocol is a VLAN
1045 * header, so start checking at the encapsulated protocol.
1047 * The depth of the VLAN headers is recorded to drop BLA claim
1048 * frames encapsulated into multiple VLAN headers (QinQ).
1051 vhdr
= skb_header_pointer(skb
, headlen
, VLAN_HLEN
,
1056 proto
= vhdr
->h_vlan_encapsulated_proto
;
1057 headlen
+= VLAN_HLEN
;
1059 } while (proto
== htons(ETH_P_8021Q
));
1062 if (proto
!= htons(ETH_P_ARP
))
1063 return false; /* not a claim frame */
1065 /* this must be a ARP frame. check if it is a claim. */
1067 if (unlikely(!pskb_may_pull(skb
, headlen
+ arp_hdr_len(skb
->dev
))))
1070 /* pskb_may_pull() may have modified the pointers, get ethhdr again */
1071 ethhdr
= eth_hdr(skb
);
1072 arphdr
= (struct arphdr
*)((u8
*)ethhdr
+ headlen
);
1074 /* Check whether the ARP frame carries a valid
1077 if (arphdr
->ar_hrd
!= htons(ARPHRD_ETHER
))
1079 if (arphdr
->ar_pro
!= htons(ETH_P_IP
))
1081 if (arphdr
->ar_hln
!= ETH_ALEN
)
1083 if (arphdr
->ar_pln
!= 4)
1086 hw_src
= (u8
*)arphdr
+ sizeof(struct arphdr
);
1087 hw_dst
= hw_src
+ ETH_ALEN
+ 4;
1088 bla_dst
= (struct batadv_bla_claim_dst
*)hw_dst
;
1089 bla_dst_own
= &bat_priv
->bla
.claim_dest
;
1091 /* check if it is a claim frame in general */
1092 if (memcmp(bla_dst
->magic
, bla_dst_own
->magic
,
1093 sizeof(bla_dst
->magic
)) != 0)
1096 /* check if there is a claim frame encapsulated deeper in (QinQ) and
1097 * drop that, as this is not supported by BLA but should also not be
1098 * sent via the mesh.
1103 /* Let the loopdetect frames on the mesh in any case. */
1104 if (bla_dst
->type
== BATADV_CLAIM_TYPE_LOOPDETECT
)
1107 /* check if it is a claim frame. */
1108 ret
= batadv_check_claim_group(bat_priv
, primary_if
, hw_src
, hw_dst
,
1111 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
1112 "bla_process_claim(): received a claim frame from another group. From: %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n",
1113 ethhdr
->h_source
, BATADV_PRINT_VID(vid
), hw_src
,
1119 /* become a backbone gw ourselves on this vlan if not happened yet */
1120 batadv_bla_update_own_backbone_gw(bat_priv
, primary_if
, vid
);
1122 /* check for the different types of claim frames ... */
1123 switch (bla_dst
->type
) {
1124 case BATADV_CLAIM_TYPE_CLAIM
:
1125 if (batadv_handle_claim(bat_priv
, primary_if
, hw_src
,
1126 ethhdr
->h_source
, vid
))
1129 case BATADV_CLAIM_TYPE_UNCLAIM
:
1130 if (batadv_handle_unclaim(bat_priv
, primary_if
,
1131 ethhdr
->h_source
, hw_src
, vid
))
1135 case BATADV_CLAIM_TYPE_ANNOUNCE
:
1136 if (batadv_handle_announce(bat_priv
, hw_src
, ethhdr
->h_source
,
1140 case BATADV_CLAIM_TYPE_REQUEST
:
1141 if (batadv_handle_request(bat_priv
, primary_if
, hw_src
, ethhdr
,
1147 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
1148 "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",
1149 ethhdr
->h_source
, BATADV_PRINT_VID(vid
), hw_src
, hw_dst
);
1154 * batadv_bla_purge_backbone_gw - Remove backbone gateways after a timeout or
1156 * @bat_priv: the bat priv with all the soft interface information
1157 * @now: whether the whole hash shall be wiped now
1159 * Check when we last heard from other nodes, and remove them in case of
1160 * a time out, or clean all backbone gws if now is set.
1162 static void batadv_bla_purge_backbone_gw(struct batadv_priv
*bat_priv
, int now
)
1164 struct batadv_bla_backbone_gw
*backbone_gw
;
1165 struct hlist_node
*node_tmp
;
1166 struct hlist_head
*head
;
1167 struct batadv_hashtable
*hash
;
1168 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1171 hash
= bat_priv
->bla
.backbone_hash
;
1175 for (i
= 0; i
< hash
->size
; i
++) {
1176 head
= &hash
->table
[i
];
1177 list_lock
= &hash
->list_locks
[i
];
1179 spin_lock_bh(list_lock
);
1180 hlist_for_each_entry_safe(backbone_gw
, node_tmp
,
1184 if (!batadv_has_timed_out(backbone_gw
->lasttime
,
1185 BATADV_BLA_BACKBONE_TIMEOUT
))
1188 batadv_dbg(BATADV_DBG_BLA
, backbone_gw
->bat_priv
,
1189 "bla_purge_backbone_gw(): backbone gw %pM timed out\n",
1193 /* don't wait for the pending request anymore */
1194 if (atomic_read(&backbone_gw
->request_sent
))
1195 atomic_dec(&bat_priv
->bla
.num_requests
);
1197 batadv_bla_del_backbone_claims(backbone_gw
);
1199 hlist_del_rcu(&backbone_gw
->hash_entry
);
1200 batadv_backbone_gw_put(backbone_gw
);
1202 spin_unlock_bh(list_lock
);
1207 * batadv_bla_purge_claims - Remove claims after a timeout or immediately
1208 * @bat_priv: the bat priv with all the soft interface information
1209 * @primary_if: the selected primary interface, may be NULL if now is set
1210 * @now: whether the whole hash shall be wiped now
1212 * Check when we heard last time from our own claims, and remove them in case of
1213 * a time out, or clean all claims if now is set
1215 static void batadv_bla_purge_claims(struct batadv_priv
*bat_priv
,
1216 struct batadv_hard_iface
*primary_if
,
1219 struct batadv_bla_claim
*claim
;
1220 struct hlist_head
*head
;
1221 struct batadv_hashtable
*hash
;
1224 hash
= bat_priv
->bla
.claim_hash
;
1228 for (i
= 0; i
< hash
->size
; i
++) {
1229 head
= &hash
->table
[i
];
1232 hlist_for_each_entry_rcu(claim
, head
, hash_entry
) {
1235 if (!batadv_compare_eth(claim
->backbone_gw
->orig
,
1236 primary_if
->net_dev
->dev_addr
))
1238 if (!batadv_has_timed_out(claim
->lasttime
,
1239 BATADV_BLA_CLAIM_TIMEOUT
))
1242 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
1243 "bla_purge_claims(): %pM, vid %d, time out\n",
1244 claim
->addr
, claim
->vid
);
1247 batadv_handle_unclaim(bat_priv
, primary_if
,
1248 claim
->backbone_gw
->orig
,
1249 claim
->addr
, claim
->vid
);
1256 * batadv_bla_update_orig_address - Update the backbone gateways when the own
1257 * originator address changes
1258 * @bat_priv: the bat priv with all the soft interface information
1259 * @primary_if: the new selected primary_if
1260 * @oldif: the old primary interface, may be NULL
1262 void batadv_bla_update_orig_address(struct batadv_priv
*bat_priv
,
1263 struct batadv_hard_iface
*primary_if
,
1264 struct batadv_hard_iface
*oldif
)
1266 struct batadv_bla_backbone_gw
*backbone_gw
;
1267 struct hlist_head
*head
;
1268 struct batadv_hashtable
*hash
;
1272 /* reset bridge loop avoidance group id */
1273 group
= htons(crc16(0, primary_if
->net_dev
->dev_addr
, ETH_ALEN
));
1274 bat_priv
->bla
.claim_dest
.group
= group
;
1276 /* purge everything when bridge loop avoidance is turned off */
1277 if (!atomic_read(&bat_priv
->bridge_loop_avoidance
))
1281 batadv_bla_purge_claims(bat_priv
, NULL
, 1);
1282 batadv_bla_purge_backbone_gw(bat_priv
, 1);
1286 hash
= bat_priv
->bla
.backbone_hash
;
1290 for (i
= 0; i
< hash
->size
; i
++) {
1291 head
= &hash
->table
[i
];
1294 hlist_for_each_entry_rcu(backbone_gw
, head
, hash_entry
) {
1295 /* own orig still holds the old value. */
1296 if (!batadv_compare_eth(backbone_gw
->orig
,
1297 oldif
->net_dev
->dev_addr
))
1300 ether_addr_copy(backbone_gw
->orig
,
1301 primary_if
->net_dev
->dev_addr
);
1302 /* send an announce frame so others will ask for our
1303 * claims and update their tables.
1305 batadv_bla_send_announce(bat_priv
, backbone_gw
);
1312 * batadv_bla_send_loopdetect - send a loopdetect frame
1313 * @bat_priv: the bat priv with all the soft interface information
1314 * @backbone_gw: the backbone gateway for which a loop should be detected
1316 * To detect loops that the bridge loop avoidance can't handle, send a loop
1317 * detection packet on the backbone. Unlike other BLA frames, this frame will
1318 * be allowed on the mesh by other nodes. If it is received on the mesh, this
1319 * indicates that there is a loop.
1322 batadv_bla_send_loopdetect(struct batadv_priv
*bat_priv
,
1323 struct batadv_bla_backbone_gw
*backbone_gw
)
1325 batadv_dbg(BATADV_DBG_BLA
, bat_priv
, "Send loopdetect frame for vid %d\n",
1327 batadv_bla_send_claim(bat_priv
, bat_priv
->bla
.loopdetect_addr
,
1328 backbone_gw
->vid
, BATADV_CLAIM_TYPE_LOOPDETECT
);
1332 * batadv_bla_status_update - purge bla interfaces if necessary
1333 * @net_dev: the soft interface net device
1335 void batadv_bla_status_update(struct net_device
*net_dev
)
1337 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
1338 struct batadv_hard_iface
*primary_if
;
1340 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1344 /* this function already purges everything when bla is disabled,
1345 * so just call that one.
1347 batadv_bla_update_orig_address(bat_priv
, primary_if
, primary_if
);
1348 batadv_hardif_put(primary_if
);
1352 * batadv_bla_periodic_work - performs periodic bla work
1353 * @work: kernel work struct
1355 * periodic work to do:
1356 * * purge structures when they are too old
1357 * * send announcements
1359 static void batadv_bla_periodic_work(struct work_struct
*work
)
1361 struct delayed_work
*delayed_work
;
1362 struct batadv_priv
*bat_priv
;
1363 struct batadv_priv_bla
*priv_bla
;
1364 struct hlist_head
*head
;
1365 struct batadv_bla_backbone_gw
*backbone_gw
;
1366 struct batadv_hashtable
*hash
;
1367 struct batadv_hard_iface
*primary_if
;
1368 bool send_loopdetect
= false;
1371 delayed_work
= to_delayed_work(work
);
1372 priv_bla
= container_of(delayed_work
, struct batadv_priv_bla
, work
);
1373 bat_priv
= container_of(priv_bla
, struct batadv_priv
, bla
);
1374 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1378 batadv_bla_purge_claims(bat_priv
, primary_if
, 0);
1379 batadv_bla_purge_backbone_gw(bat_priv
, 0);
1381 if (!atomic_read(&bat_priv
->bridge_loop_avoidance
))
1384 if (atomic_dec_and_test(&bat_priv
->bla
.loopdetect_next
)) {
1385 /* set a new random mac address for the next bridge loop
1386 * detection frames. Set the locally administered bit to avoid
1387 * collisions with users mac addresses.
1389 random_ether_addr(bat_priv
->bla
.loopdetect_addr
);
1390 bat_priv
->bla
.loopdetect_addr
[0] = 0xba;
1391 bat_priv
->bla
.loopdetect_addr
[1] = 0xbe;
1392 bat_priv
->bla
.loopdetect_lasttime
= jiffies
;
1393 atomic_set(&bat_priv
->bla
.loopdetect_next
,
1394 BATADV_BLA_LOOPDETECT_PERIODS
);
1396 /* mark for sending loop detect on all VLANs */
1397 send_loopdetect
= true;
1400 hash
= bat_priv
->bla
.backbone_hash
;
1404 for (i
= 0; i
< hash
->size
; i
++) {
1405 head
= &hash
->table
[i
];
1408 hlist_for_each_entry_rcu(backbone_gw
, head
, hash_entry
) {
1409 if (!batadv_compare_eth(backbone_gw
->orig
,
1410 primary_if
->net_dev
->dev_addr
))
1413 backbone_gw
->lasttime
= jiffies
;
1415 batadv_bla_send_announce(bat_priv
, backbone_gw
);
1416 if (send_loopdetect
)
1417 batadv_bla_send_loopdetect(bat_priv
,
1420 /* request_sent is only set after creation to avoid
1421 * problems when we are not yet known as backbone gw
1424 * We can reset this now after we waited some periods
1425 * to give bridge forward delays and bla group forming
1429 if (atomic_read(&backbone_gw
->request_sent
) == 0)
1432 if (!atomic_dec_and_test(&backbone_gw
->wait_periods
))
1435 atomic_dec(&backbone_gw
->bat_priv
->bla
.num_requests
);
1436 atomic_set(&backbone_gw
->request_sent
, 0);
1442 batadv_hardif_put(primary_if
);
1444 queue_delayed_work(batadv_event_workqueue
, &bat_priv
->bla
.work
,
1445 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH
));
1448 /* The hash for claim and backbone hash receive the same key because they
1449 * are getting initialized by hash_new with the same key. Reinitializing
1450 * them with to different keys to allow nested locking without generating
1453 static struct lock_class_key batadv_claim_hash_lock_class_key
;
1454 static struct lock_class_key batadv_backbone_hash_lock_class_key
;
1457 * batadv_bla_init - initialize all bla structures
1458 * @bat_priv: the bat priv with all the soft interface information
1460 * Return: 0 on success, < 0 on error.
1462 int batadv_bla_init(struct batadv_priv
*bat_priv
)
1465 u8 claim_dest
[ETH_ALEN
] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00};
1466 struct batadv_hard_iface
*primary_if
;
1468 unsigned long entrytime
;
1470 spin_lock_init(&bat_priv
->bla
.bcast_duplist_lock
);
1472 batadv_dbg(BATADV_DBG_BLA
, bat_priv
, "bla hash registering\n");
1474 /* setting claim destination address */
1475 memcpy(&bat_priv
->bla
.claim_dest
.magic
, claim_dest
, 3);
1476 bat_priv
->bla
.claim_dest
.type
= 0;
1477 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1479 crc
= crc16(0, primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
1480 bat_priv
->bla
.claim_dest
.group
= htons(crc
);
1481 batadv_hardif_put(primary_if
);
1483 bat_priv
->bla
.claim_dest
.group
= 0; /* will be set later */
1486 /* initialize the duplicate list */
1487 entrytime
= jiffies
- msecs_to_jiffies(BATADV_DUPLIST_TIMEOUT
);
1488 for (i
= 0; i
< BATADV_DUPLIST_SIZE
; i
++)
1489 bat_priv
->bla
.bcast_duplist
[i
].entrytime
= entrytime
;
1490 bat_priv
->bla
.bcast_duplist_curr
= 0;
1492 atomic_set(&bat_priv
->bla
.loopdetect_next
,
1493 BATADV_BLA_LOOPDETECT_PERIODS
);
1495 if (bat_priv
->bla
.claim_hash
)
1498 bat_priv
->bla
.claim_hash
= batadv_hash_new(128);
1499 bat_priv
->bla
.backbone_hash
= batadv_hash_new(32);
1501 if (!bat_priv
->bla
.claim_hash
|| !bat_priv
->bla
.backbone_hash
)
1504 batadv_hash_set_lock_class(bat_priv
->bla
.claim_hash
,
1505 &batadv_claim_hash_lock_class_key
);
1506 batadv_hash_set_lock_class(bat_priv
->bla
.backbone_hash
,
1507 &batadv_backbone_hash_lock_class_key
);
1509 batadv_dbg(BATADV_DBG_BLA
, bat_priv
, "bla hashes initialized\n");
1511 INIT_DELAYED_WORK(&bat_priv
->bla
.work
, batadv_bla_periodic_work
);
1513 queue_delayed_work(batadv_event_workqueue
, &bat_priv
->bla
.work
,
1514 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH
));
1519 * batadv_bla_check_bcast_duplist - Check if a frame is in the broadcast dup.
1520 * @bat_priv: the bat priv with all the soft interface information
1521 * @skb: contains the bcast_packet to be checked
1523 * check if it is on our broadcast list. Another gateway might
1524 * have sent the same packet because it is connected to the same backbone,
1525 * so we have to remove this duplicate.
1527 * This is performed by checking the CRC, which will tell us
1528 * with a good chance that it is the same packet. If it is furthermore
1529 * sent by another host, drop it. We allow equal packets from
1530 * the same host however as this might be intended.
1532 * Return: true if a packet is in the duplicate list, false otherwise.
1534 bool batadv_bla_check_bcast_duplist(struct batadv_priv
*bat_priv
,
1535 struct sk_buff
*skb
)
1539 struct batadv_bcast_packet
*bcast_packet
;
1540 struct batadv_bcast_duplist_entry
*entry
;
1543 bcast_packet
= (struct batadv_bcast_packet
*)skb
->data
;
1545 /* calculate the crc ... */
1546 crc
= batadv_skb_crc32(skb
, (u8
*)(bcast_packet
+ 1));
1548 spin_lock_bh(&bat_priv
->bla
.bcast_duplist_lock
);
1550 for (i
= 0; i
< BATADV_DUPLIST_SIZE
; i
++) {
1551 curr
= (bat_priv
->bla
.bcast_duplist_curr
+ i
);
1552 curr
%= BATADV_DUPLIST_SIZE
;
1553 entry
= &bat_priv
->bla
.bcast_duplist
[curr
];
1555 /* we can stop searching if the entry is too old ;
1556 * later entries will be even older
1558 if (batadv_has_timed_out(entry
->entrytime
,
1559 BATADV_DUPLIST_TIMEOUT
))
1562 if (entry
->crc
!= crc
)
1565 if (batadv_compare_eth(entry
->orig
, bcast_packet
->orig
))
1568 /* this entry seems to match: same crc, not too old,
1569 * and from another gw. therefore return true to forbid it.
1574 /* not found, add a new entry (overwrite the oldest entry)
1575 * and allow it, its the first occurrence.
1577 curr
= (bat_priv
->bla
.bcast_duplist_curr
+ BATADV_DUPLIST_SIZE
- 1);
1578 curr
%= BATADV_DUPLIST_SIZE
;
1579 entry
= &bat_priv
->bla
.bcast_duplist
[curr
];
1581 entry
->entrytime
= jiffies
;
1582 ether_addr_copy(entry
->orig
, bcast_packet
->orig
);
1583 bat_priv
->bla
.bcast_duplist_curr
= curr
;
1586 spin_unlock_bh(&bat_priv
->bla
.bcast_duplist_lock
);
1592 * batadv_bla_is_backbone_gw_orig - Check if the originator is a gateway for
1593 * the VLAN identified by vid.
1594 * @bat_priv: the bat priv with all the soft interface information
1595 * @orig: originator mac address
1596 * @vid: VLAN identifier
1598 * Return: true if orig is a backbone for this vid, false otherwise.
1600 bool batadv_bla_is_backbone_gw_orig(struct batadv_priv
*bat_priv
, u8
*orig
,
1603 struct batadv_hashtable
*hash
= bat_priv
->bla
.backbone_hash
;
1604 struct hlist_head
*head
;
1605 struct batadv_bla_backbone_gw
*backbone_gw
;
1608 if (!atomic_read(&bat_priv
->bridge_loop_avoidance
))
1614 for (i
= 0; i
< hash
->size
; i
++) {
1615 head
= &hash
->table
[i
];
1618 hlist_for_each_entry_rcu(backbone_gw
, head
, hash_entry
) {
1619 if (batadv_compare_eth(backbone_gw
->orig
, orig
) &&
1620 backbone_gw
->vid
== vid
) {
1632 * batadv_bla_is_backbone_gw - check if originator is a backbone gw for a VLAN.
1633 * @skb: the frame to be checked
1634 * @orig_node: the orig_node of the frame
1635 * @hdr_size: maximum length of the frame
1637 * Return: true if the orig_node is also a gateway on the soft interface,
1638 * otherwise it returns false.
1640 bool batadv_bla_is_backbone_gw(struct sk_buff
*skb
,
1641 struct batadv_orig_node
*orig_node
, int hdr_size
)
1643 struct batadv_bla_backbone_gw
*backbone_gw
;
1646 if (!atomic_read(&orig_node
->bat_priv
->bridge_loop_avoidance
))
1649 /* first, find out the vid. */
1650 if (!pskb_may_pull(skb
, hdr_size
+ ETH_HLEN
))
1653 vid
= batadv_get_vid(skb
, hdr_size
);
1655 /* see if this originator is a backbone gw for this VLAN */
1656 backbone_gw
= batadv_backbone_hash_find(orig_node
->bat_priv
,
1657 orig_node
->orig
, vid
);
1661 batadv_backbone_gw_put(backbone_gw
);
1666 * batadv_bla_free - free all bla structures
1667 * @bat_priv: the bat priv with all the soft interface information
1669 * for softinterface free or module unload
1671 void batadv_bla_free(struct batadv_priv
*bat_priv
)
1673 struct batadv_hard_iface
*primary_if
;
1675 cancel_delayed_work_sync(&bat_priv
->bla
.work
);
1676 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1678 if (bat_priv
->bla
.claim_hash
) {
1679 batadv_bla_purge_claims(bat_priv
, primary_if
, 1);
1680 batadv_hash_destroy(bat_priv
->bla
.claim_hash
);
1681 bat_priv
->bla
.claim_hash
= NULL
;
1683 if (bat_priv
->bla
.backbone_hash
) {
1684 batadv_bla_purge_backbone_gw(bat_priv
, 1);
1685 batadv_hash_destroy(bat_priv
->bla
.backbone_hash
);
1686 bat_priv
->bla
.backbone_hash
= NULL
;
1689 batadv_hardif_put(primary_if
);
1693 * batadv_bla_loopdetect_check - check and handle a detected loop
1694 * @bat_priv: the bat priv with all the soft interface information
1695 * @skb: the packet to check
1696 * @primary_if: interface where the request came on
1697 * @vid: the VLAN ID of the frame
1699 * Checks if this packet is a loop detect frame which has been sent by us,
1700 * throw an uevent and log the event if that is the case.
1702 * Return: true if it is a loop detect frame which is to be dropped, false
1706 batadv_bla_loopdetect_check(struct batadv_priv
*bat_priv
, struct sk_buff
*skb
,
1707 struct batadv_hard_iface
*primary_if
,
1710 struct batadv_bla_backbone_gw
*backbone_gw
;
1711 struct ethhdr
*ethhdr
;
1713 ethhdr
= eth_hdr(skb
);
1715 /* Only check for the MAC address and skip more checks here for
1716 * performance reasons - this function is on the hotpath, after all.
1718 if (!batadv_compare_eth(ethhdr
->h_source
,
1719 bat_priv
->bla
.loopdetect_addr
))
1722 /* If the packet came too late, don't forward it on the mesh
1723 * but don't consider that as loop. It might be a coincidence.
1725 if (batadv_has_timed_out(bat_priv
->bla
.loopdetect_lasttime
,
1726 BATADV_BLA_LOOPDETECT_TIMEOUT
))
1729 backbone_gw
= batadv_bla_get_backbone_gw(bat_priv
,
1730 primary_if
->net_dev
->dev_addr
,
1732 if (unlikely(!backbone_gw
))
1735 queue_work(batadv_event_workqueue
, &backbone_gw
->report_work
);
1736 /* backbone_gw is unreferenced in the report work function function */
1742 * batadv_bla_rx - check packets coming from the mesh.
1743 * @bat_priv: the bat priv with all the soft interface information
1744 * @skb: the frame to be checked
1745 * @vid: the VLAN ID of the frame
1746 * @is_bcast: the packet came in a broadcast packet type.
1748 * batadv_bla_rx avoidance checks if:
1749 * * we have to race for a claim
1750 * * if the frame is allowed on the LAN
1752 * in these cases, the skb is further handled by this function
1754 * Return: true if handled, otherwise it returns false and the caller shall
1755 * further process the skb.
1757 bool batadv_bla_rx(struct batadv_priv
*bat_priv
, struct sk_buff
*skb
,
1758 unsigned short vid
, bool is_bcast
)
1760 struct ethhdr
*ethhdr
;
1761 struct batadv_bla_claim search_claim
, *claim
= NULL
;
1762 struct batadv_hard_iface
*primary_if
;
1765 ethhdr
= eth_hdr(skb
);
1767 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1771 if (!atomic_read(&bat_priv
->bridge_loop_avoidance
))
1774 if (batadv_bla_loopdetect_check(bat_priv
, skb
, primary_if
, vid
))
1777 if (unlikely(atomic_read(&bat_priv
->bla
.num_requests
)))
1778 /* don't allow broadcasts while requests are in flight */
1779 if (is_multicast_ether_addr(ethhdr
->h_dest
) && is_bcast
)
1782 ether_addr_copy(search_claim
.addr
, ethhdr
->h_source
);
1783 search_claim
.vid
= vid
;
1784 claim
= batadv_claim_hash_find(bat_priv
, &search_claim
);
1787 /* possible optimization: race for a claim */
1788 /* No claim exists yet, claim it for us!
1790 batadv_handle_claim(bat_priv
, primary_if
,
1791 primary_if
->net_dev
->dev_addr
,
1792 ethhdr
->h_source
, vid
);
1796 /* if it is our own claim ... */
1797 if (batadv_compare_eth(claim
->backbone_gw
->orig
,
1798 primary_if
->net_dev
->dev_addr
)) {
1799 /* ... allow it in any case */
1800 claim
->lasttime
= jiffies
;
1804 /* if it is a broadcast ... */
1805 if (is_multicast_ether_addr(ethhdr
->h_dest
) && is_bcast
) {
1806 /* ... drop it. the responsible gateway is in charge.
1808 * We need to check is_bcast because with the gateway
1809 * feature, broadcasts (like DHCP requests) may be sent
1810 * using a unicast packet type.
1814 /* seems the client considers us as its best gateway.
1815 * send a claim and update the claim table
1818 batadv_handle_claim(bat_priv
, primary_if
,
1819 primary_if
->net_dev
->dev_addr
,
1820 ethhdr
->h_source
, vid
);
1824 batadv_bla_update_own_backbone_gw(bat_priv
, primary_if
, vid
);
1834 batadv_hardif_put(primary_if
);
1836 batadv_claim_put(claim
);
1841 * batadv_bla_tx - check packets going into the mesh
1842 * @bat_priv: the bat priv with all the soft interface information
1843 * @skb: the frame to be checked
1844 * @vid: the VLAN ID of the frame
1846 * batadv_bla_tx checks if:
1847 * * a claim was received which has to be processed
1848 * * the frame is allowed on the mesh
1850 * in these cases, the skb is further handled by this function.
1852 * This call might reallocate skb data.
1854 * Return: true if handled, otherwise it returns false and the caller shall
1855 * further process the skb.
1857 bool batadv_bla_tx(struct batadv_priv
*bat_priv
, struct sk_buff
*skb
,
1860 struct ethhdr
*ethhdr
;
1861 struct batadv_bla_claim search_claim
, *claim
= NULL
;
1862 struct batadv_hard_iface
*primary_if
;
1865 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1869 if (!atomic_read(&bat_priv
->bridge_loop_avoidance
))
1872 if (batadv_bla_process_claim(bat_priv
, primary_if
, skb
))
1875 ethhdr
= eth_hdr(skb
);
1877 if (unlikely(atomic_read(&bat_priv
->bla
.num_requests
)))
1878 /* don't allow broadcasts while requests are in flight */
1879 if (is_multicast_ether_addr(ethhdr
->h_dest
))
1882 ether_addr_copy(search_claim
.addr
, ethhdr
->h_source
);
1883 search_claim
.vid
= vid
;
1885 claim
= batadv_claim_hash_find(bat_priv
, &search_claim
);
1887 /* if no claim exists, allow it. */
1891 /* check if we are responsible. */
1892 if (batadv_compare_eth(claim
->backbone_gw
->orig
,
1893 primary_if
->net_dev
->dev_addr
)) {
1894 /* if yes, the client has roamed and we have
1897 batadv_handle_unclaim(bat_priv
, primary_if
,
1898 primary_if
->net_dev
->dev_addr
,
1899 ethhdr
->h_source
, vid
);
1903 /* check if it is a multicast/broadcast frame */
1904 if (is_multicast_ether_addr(ethhdr
->h_dest
)) {
1905 /* drop it. the responsible gateway has forwarded it into
1906 * the backbone network.
1910 /* we must allow it. at least if we are
1911 * responsible for the DESTINATION.
1916 batadv_bla_update_own_backbone_gw(bat_priv
, primary_if
, vid
);
1923 batadv_hardif_put(primary_if
);
1925 batadv_claim_put(claim
);
1930 * batadv_bla_claim_table_seq_print_text - print the claim table in a seq file
1931 * @seq: seq file to print on
1936 int batadv_bla_claim_table_seq_print_text(struct seq_file
*seq
, void *offset
)
1938 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
1939 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
1940 struct batadv_hashtable
*hash
= bat_priv
->bla
.claim_hash
;
1941 struct batadv_bla_claim
*claim
;
1942 struct batadv_hard_iface
*primary_if
;
1943 struct hlist_head
*head
;
1949 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
1953 primary_addr
= primary_if
->net_dev
->dev_addr
;
1955 "Claims announced for the mesh %s (orig %pM, group id %#.4x)\n",
1956 net_dev
->name
, primary_addr
,
1957 ntohs(bat_priv
->bla
.claim_dest
.group
));
1959 " Client VID Originator [o] (CRC )\n");
1960 for (i
= 0; i
< hash
->size
; i
++) {
1961 head
= &hash
->table
[i
];
1964 hlist_for_each_entry_rcu(claim
, head
, hash_entry
) {
1965 is_own
= batadv_compare_eth(claim
->backbone_gw
->orig
,
1968 spin_lock_bh(&claim
->backbone_gw
->crc_lock
);
1969 backbone_crc
= claim
->backbone_gw
->crc
;
1970 spin_unlock_bh(&claim
->backbone_gw
->crc_lock
);
1971 seq_printf(seq
, " * %pM on %5d by %pM [%c] (%#.4x)\n",
1972 claim
->addr
, BATADV_PRINT_VID(claim
->vid
),
1973 claim
->backbone_gw
->orig
,
1974 (is_own
? 'x' : ' '),
1981 batadv_hardif_put(primary_if
);
1986 * batadv_bla_backbone_table_seq_print_text - print the backbone table in a seq
1988 * @seq: seq file to print on
1993 int batadv_bla_backbone_table_seq_print_text(struct seq_file
*seq
, void *offset
)
1995 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
1996 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
1997 struct batadv_hashtable
*hash
= bat_priv
->bla
.backbone_hash
;
1998 struct batadv_bla_backbone_gw
*backbone_gw
;
1999 struct batadv_hard_iface
*primary_if
;
2000 struct hlist_head
*head
;
2007 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
2011 primary_addr
= primary_if
->net_dev
->dev_addr
;
2013 "Backbones announced for the mesh %s (orig %pM, group id %#.4x)\n",
2014 net_dev
->name
, primary_addr
,
2015 ntohs(bat_priv
->bla
.claim_dest
.group
));
2016 seq_puts(seq
, " Originator VID last seen (CRC )\n");
2017 for (i
= 0; i
< hash
->size
; i
++) {
2018 head
= &hash
->table
[i
];
2021 hlist_for_each_entry_rcu(backbone_gw
, head
, hash_entry
) {
2022 msecs
= jiffies_to_msecs(jiffies
-
2023 backbone_gw
->lasttime
);
2024 secs
= msecs
/ 1000;
2025 msecs
= msecs
% 1000;
2027 is_own
= batadv_compare_eth(backbone_gw
->orig
,
2032 spin_lock_bh(&backbone_gw
->crc_lock
);
2033 backbone_crc
= backbone_gw
->crc
;
2034 spin_unlock_bh(&backbone_gw
->crc_lock
);
2036 seq_printf(seq
, " * %pM on %5d %4i.%03is (%#.4x)\n",
2038 BATADV_PRINT_VID(backbone_gw
->vid
), secs
,
2039 msecs
, backbone_crc
);
2045 batadv_hardif_put(primary_if
);