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"
52 #include "originator.h"
55 #include "translation-table.h"
57 static const u8 batadv_announce_mac
[4] = {0x43, 0x05, 0x43, 0x05};
59 static void batadv_bla_periodic_work(struct work_struct
*work
);
61 batadv_bla_send_announce(struct batadv_priv
*bat_priv
,
62 struct batadv_bla_backbone_gw
*backbone_gw
);
65 * batadv_choose_claim - choose the right bucket for a claim.
67 * @size: size of the hash table
69 * Return: the hash index of the claim
71 static inline u32
batadv_choose_claim(const void *data
, u32 size
)
73 struct batadv_bla_claim
*claim
= (struct batadv_bla_claim
*)data
;
76 hash
= jhash(&claim
->addr
, sizeof(claim
->addr
), hash
);
77 hash
= jhash(&claim
->vid
, sizeof(claim
->vid
), hash
);
83 * batadv_choose_backbone_gw - choose the right bucket for a backbone gateway.
85 * @size: size of the hash table
87 * Return: the hash index of the backbone gateway
89 static inline u32
batadv_choose_backbone_gw(const void *data
, u32 size
)
91 const struct batadv_bla_claim
*claim
= (struct batadv_bla_claim
*)data
;
94 hash
= jhash(&claim
->addr
, sizeof(claim
->addr
), hash
);
95 hash
= jhash(&claim
->vid
, sizeof(claim
->vid
), hash
);
101 * batadv_compare_backbone_gw - compare address and vid of two backbone gws
102 * @node: list node of the first entry to compare
103 * @data2: pointer to the second backbone gateway
105 * Return: true if the backbones have the same data, false otherwise
107 static bool batadv_compare_backbone_gw(const struct hlist_node
*node
,
110 const void *data1
= container_of(node
, struct batadv_bla_backbone_gw
,
112 const struct batadv_bla_backbone_gw
*gw1
= data1
;
113 const struct batadv_bla_backbone_gw
*gw2
= data2
;
115 if (!batadv_compare_eth(gw1
->orig
, gw2
->orig
))
118 if (gw1
->vid
!= gw2
->vid
)
125 * batadv_compare_claim - compare address and vid of two claims
126 * @node: list node of the first entry to compare
127 * @data2: pointer to the second claims
129 * Return: true if the claim have the same data, 0 otherwise
131 static bool batadv_compare_claim(const struct hlist_node
*node
,
134 const void *data1
= container_of(node
, struct batadv_bla_claim
,
136 const struct batadv_bla_claim
*cl1
= data1
;
137 const struct batadv_bla_claim
*cl2
= data2
;
139 if (!batadv_compare_eth(cl1
->addr
, cl2
->addr
))
142 if (cl1
->vid
!= cl2
->vid
)
149 * batadv_backbone_gw_release - release backbone gw from lists and queue for
150 * free after rcu grace period
151 * @ref: kref pointer of the backbone gw
153 static void batadv_backbone_gw_release(struct kref
*ref
)
155 struct batadv_bla_backbone_gw
*backbone_gw
;
157 backbone_gw
= container_of(ref
, struct batadv_bla_backbone_gw
,
160 kfree_rcu(backbone_gw
, rcu
);
164 * batadv_backbone_gw_put - decrement the backbone gw refcounter and possibly
166 * @backbone_gw: backbone gateway to be free'd
168 static void batadv_backbone_gw_put(struct batadv_bla_backbone_gw
*backbone_gw
)
170 kref_put(&backbone_gw
->refcount
, batadv_backbone_gw_release
);
174 * batadv_claim_release - release claim from lists and queue for free after rcu
176 * @ref: kref pointer of the claim
178 static void batadv_claim_release(struct kref
*ref
)
180 struct batadv_bla_claim
*claim
;
181 struct batadv_bla_backbone_gw
*old_backbone_gw
;
183 claim
= container_of(ref
, struct batadv_bla_claim
, refcount
);
185 spin_lock_bh(&claim
->backbone_lock
);
186 old_backbone_gw
= claim
->backbone_gw
;
187 claim
->backbone_gw
= NULL
;
188 spin_unlock_bh(&claim
->backbone_lock
);
190 spin_lock_bh(&old_backbone_gw
->crc_lock
);
191 old_backbone_gw
->crc
^= crc16(0, claim
->addr
, ETH_ALEN
);
192 spin_unlock_bh(&old_backbone_gw
->crc_lock
);
194 batadv_backbone_gw_put(old_backbone_gw
);
196 kfree_rcu(claim
, rcu
);
200 * batadv_claim_put - decrement the claim refcounter and possibly
202 * @claim: claim to be free'd
204 static void batadv_claim_put(struct batadv_bla_claim
*claim
)
206 kref_put(&claim
->refcount
, batadv_claim_release
);
210 * batadv_claim_hash_find - looks for a claim in the claim hash
211 * @bat_priv: the bat priv with all the soft interface information
212 * @data: search data (may be local/static data)
214 * Return: claim if found or NULL otherwise.
216 static struct batadv_bla_claim
*
217 batadv_claim_hash_find(struct batadv_priv
*bat_priv
,
218 struct batadv_bla_claim
*data
)
220 struct batadv_hashtable
*hash
= bat_priv
->bla
.claim_hash
;
221 struct hlist_head
*head
;
222 struct batadv_bla_claim
*claim
;
223 struct batadv_bla_claim
*claim_tmp
= NULL
;
229 index
= batadv_choose_claim(data
, hash
->size
);
230 head
= &hash
->table
[index
];
233 hlist_for_each_entry_rcu(claim
, head
, hash_entry
) {
234 if (!batadv_compare_claim(&claim
->hash_entry
, data
))
237 if (!kref_get_unless_zero(&claim
->refcount
))
249 * batadv_backbone_hash_find - looks for a backbone gateway in the hash
250 * @bat_priv: the bat priv with all the soft interface information
251 * @addr: the address of the originator
254 * Return: backbone gateway if found or NULL otherwise
256 static struct batadv_bla_backbone_gw
*
257 batadv_backbone_hash_find(struct batadv_priv
*bat_priv
, u8
*addr
,
260 struct batadv_hashtable
*hash
= bat_priv
->bla
.backbone_hash
;
261 struct hlist_head
*head
;
262 struct batadv_bla_backbone_gw search_entry
, *backbone_gw
;
263 struct batadv_bla_backbone_gw
*backbone_gw_tmp
= NULL
;
269 ether_addr_copy(search_entry
.orig
, addr
);
270 search_entry
.vid
= vid
;
272 index
= batadv_choose_backbone_gw(&search_entry
, hash
->size
);
273 head
= &hash
->table
[index
];
276 hlist_for_each_entry_rcu(backbone_gw
, head
, hash_entry
) {
277 if (!batadv_compare_backbone_gw(&backbone_gw
->hash_entry
,
281 if (!kref_get_unless_zero(&backbone_gw
->refcount
))
284 backbone_gw_tmp
= backbone_gw
;
289 return backbone_gw_tmp
;
293 * batadv_bla_del_backbone_claims - delete all claims for a backbone
294 * @backbone_gw: backbone gateway where the claims should be removed
297 batadv_bla_del_backbone_claims(struct batadv_bla_backbone_gw
*backbone_gw
)
299 struct batadv_hashtable
*hash
;
300 struct hlist_node
*node_tmp
;
301 struct hlist_head
*head
;
302 struct batadv_bla_claim
*claim
;
304 spinlock_t
*list_lock
; /* protects write access to the hash lists */
306 hash
= backbone_gw
->bat_priv
->bla
.claim_hash
;
310 for (i
= 0; i
< hash
->size
; i
++) {
311 head
= &hash
->table
[i
];
312 list_lock
= &hash
->list_locks
[i
];
314 spin_lock_bh(list_lock
);
315 hlist_for_each_entry_safe(claim
, node_tmp
,
317 if (claim
->backbone_gw
!= backbone_gw
)
320 batadv_claim_put(claim
);
321 hlist_del_rcu(&claim
->hash_entry
);
323 spin_unlock_bh(list_lock
);
326 /* all claims gone, initialize CRC */
327 spin_lock_bh(&backbone_gw
->crc_lock
);
328 backbone_gw
->crc
= BATADV_BLA_CRC_INIT
;
329 spin_unlock_bh(&backbone_gw
->crc_lock
);
333 * batadv_bla_send_claim - sends a claim frame according to the provided info
334 * @bat_priv: the bat priv with all the soft interface information
335 * @mac: the mac address to be announced within the claim
337 * @claimtype: the type of the claim (CLAIM, UNCLAIM, ANNOUNCE, ...)
339 static void batadv_bla_send_claim(struct batadv_priv
*bat_priv
, u8
*mac
,
340 unsigned short vid
, int claimtype
)
343 struct ethhdr
*ethhdr
;
344 struct batadv_hard_iface
*primary_if
;
345 struct net_device
*soft_iface
;
347 struct batadv_bla_claim_dst local_claim_dest
;
350 primary_if
= batadv_primary_if_get_selected(bat_priv
);
354 memcpy(&local_claim_dest
, &bat_priv
->bla
.claim_dest
,
355 sizeof(local_claim_dest
));
356 local_claim_dest
.type
= claimtype
;
358 soft_iface
= primary_if
->soft_iface
;
360 skb
= arp_create(ARPOP_REPLY
, ETH_P_ARP
,
361 /* IP DST: 0.0.0.0 */
363 primary_if
->soft_iface
,
364 /* IP SRC: 0.0.0.0 */
366 /* Ethernet DST: Broadcast */
368 /* Ethernet SRC/HW SRC: originator mac */
369 primary_if
->net_dev
->dev_addr
,
370 /* HW DST: FF:43:05:XX:YY:YY
371 * with XX = claim type
372 * and YY:YY = group id
374 (u8
*)&local_claim_dest
);
379 ethhdr
= (struct ethhdr
*)skb
->data
;
380 hw_src
= (u8
*)ethhdr
+ ETH_HLEN
+ sizeof(struct arphdr
);
382 /* now we pretend that the client would have sent this ... */
384 case BATADV_CLAIM_TYPE_CLAIM
:
385 /* normal claim frame
386 * set Ethernet SRC to the clients mac
388 ether_addr_copy(ethhdr
->h_source
, mac
);
389 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
390 "bla_send_claim(): CLAIM %pM on vid %d\n", mac
,
391 BATADV_PRINT_VID(vid
));
393 case BATADV_CLAIM_TYPE_UNCLAIM
:
395 * set HW SRC to the clients mac
397 ether_addr_copy(hw_src
, mac
);
398 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
399 "bla_send_claim(): UNCLAIM %pM on vid %d\n", mac
,
400 BATADV_PRINT_VID(vid
));
402 case BATADV_CLAIM_TYPE_ANNOUNCE
:
403 /* announcement frame
404 * set HW SRC to the special mac containg the crc
406 ether_addr_copy(hw_src
, mac
);
407 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
408 "bla_send_claim(): ANNOUNCE of %pM on vid %d\n",
409 ethhdr
->h_source
, BATADV_PRINT_VID(vid
));
411 case BATADV_CLAIM_TYPE_REQUEST
:
413 * set HW SRC and header destination to the receiving backbone
416 ether_addr_copy(hw_src
, mac
);
417 ether_addr_copy(ethhdr
->h_dest
, mac
);
418 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
419 "bla_send_claim(): REQUEST of %pM to %pM on vid %d\n",
420 ethhdr
->h_source
, ethhdr
->h_dest
,
421 BATADV_PRINT_VID(vid
));
423 case BATADV_CLAIM_TYPE_LOOPDETECT
:
424 ether_addr_copy(ethhdr
->h_source
, mac
);
425 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
426 "bla_send_claim(): LOOPDETECT of %pM to %pM on vid %d\n",
427 ethhdr
->h_source
, ethhdr
->h_dest
,
428 BATADV_PRINT_VID(vid
));
433 if (vid
& BATADV_VLAN_HAS_TAG
) {
434 skb
= vlan_insert_tag(skb
, htons(ETH_P_8021Q
),
435 vid
& VLAN_VID_MASK
);
440 skb_reset_mac_header(skb
);
441 skb
->protocol
= eth_type_trans(skb
, soft_iface
);
442 batadv_inc_counter(bat_priv
, BATADV_CNT_RX
);
443 batadv_add_counter(bat_priv
, BATADV_CNT_RX_BYTES
,
444 skb
->len
+ ETH_HLEN
);
445 soft_iface
->last_rx
= jiffies
;
450 batadv_hardif_put(primary_if
);
454 * batadv_bla_loopdetect_report - worker for reporting the loop
455 * @work: work queue item
457 * Throws an uevent, as the loopdetect check function can't do that itself
458 * since the kernel may sleep while throwing uevents.
460 static void batadv_bla_loopdetect_report(struct work_struct
*work
)
462 struct batadv_bla_backbone_gw
*backbone_gw
;
463 struct batadv_priv
*bat_priv
;
464 char vid_str
[6] = { '\0' };
466 backbone_gw
= container_of(work
, struct batadv_bla_backbone_gw
,
468 bat_priv
= backbone_gw
->bat_priv
;
470 batadv_info(bat_priv
->soft_iface
,
471 "Possible loop on VLAN %d detected which can't be handled by BLA - please check your network setup!\n",
472 BATADV_PRINT_VID(backbone_gw
->vid
));
473 snprintf(vid_str
, sizeof(vid_str
), "%d",
474 BATADV_PRINT_VID(backbone_gw
->vid
));
475 vid_str
[sizeof(vid_str
) - 1] = 0;
477 batadv_throw_uevent(bat_priv
, BATADV_UEV_BLA
, BATADV_UEV_LOOPDETECT
,
480 batadv_backbone_gw_put(backbone_gw
);
484 * batadv_bla_get_backbone_gw - finds or creates a backbone gateway
485 * @bat_priv: the bat priv with all the soft interface information
486 * @orig: the mac address of the originator
488 * @own_backbone: set if the requested backbone is local
490 * Return: the (possibly created) backbone gateway or NULL on error
492 static struct batadv_bla_backbone_gw
*
493 batadv_bla_get_backbone_gw(struct batadv_priv
*bat_priv
, u8
*orig
,
494 unsigned short vid
, bool own_backbone
)
496 struct batadv_bla_backbone_gw
*entry
;
497 struct batadv_orig_node
*orig_node
;
500 entry
= batadv_backbone_hash_find(bat_priv
, orig
, vid
);
505 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
506 "bla_get_backbone_gw(): not found (%pM, %d), creating new entry\n",
507 orig
, BATADV_PRINT_VID(vid
));
509 entry
= kzalloc(sizeof(*entry
), GFP_ATOMIC
);
514 entry
->lasttime
= jiffies
;
515 entry
->crc
= BATADV_BLA_CRC_INIT
;
516 entry
->bat_priv
= bat_priv
;
517 spin_lock_init(&entry
->crc_lock
);
518 atomic_set(&entry
->request_sent
, 0);
519 atomic_set(&entry
->wait_periods
, 0);
520 ether_addr_copy(entry
->orig
, orig
);
521 INIT_WORK(&entry
->report_work
, batadv_bla_loopdetect_report
);
523 /* one for the hash, one for returning */
524 kref_init(&entry
->refcount
);
525 kref_get(&entry
->refcount
);
527 hash_added
= batadv_hash_add(bat_priv
->bla
.backbone_hash
,
528 batadv_compare_backbone_gw
,
529 batadv_choose_backbone_gw
, entry
,
532 if (unlikely(hash_added
!= 0)) {
533 /* hash failed, free the structure */
538 /* this is a gateway now, remove any TT entry on this VLAN */
539 orig_node
= batadv_orig_hash_find(bat_priv
, orig
);
541 batadv_tt_global_del_orig(bat_priv
, orig_node
, vid
,
542 "became a backbone gateway");
543 batadv_orig_node_put(orig_node
);
547 batadv_bla_send_announce(bat_priv
, entry
);
549 /* this will be decreased in the worker thread */
550 atomic_inc(&entry
->request_sent
);
551 atomic_set(&entry
->wait_periods
, BATADV_BLA_WAIT_PERIODS
);
552 atomic_inc(&bat_priv
->bla
.num_requests
);
559 * batadv_bla_update_own_backbone_gw - updates the own backbone gw for a VLAN
560 * @bat_priv: the bat priv with all the soft interface information
561 * @primary_if: the selected primary interface
562 * @vid: VLAN identifier
564 * update or add the own backbone gw to make sure we announce
565 * where we receive other backbone gws
568 batadv_bla_update_own_backbone_gw(struct batadv_priv
*bat_priv
,
569 struct batadv_hard_iface
*primary_if
,
572 struct batadv_bla_backbone_gw
*backbone_gw
;
574 backbone_gw
= batadv_bla_get_backbone_gw(bat_priv
,
575 primary_if
->net_dev
->dev_addr
,
577 if (unlikely(!backbone_gw
))
580 backbone_gw
->lasttime
= jiffies
;
581 batadv_backbone_gw_put(backbone_gw
);
585 * batadv_bla_answer_request - answer a bla request by sending own claims
586 * @bat_priv: the bat priv with all the soft interface information
587 * @primary_if: interface where the request came on
588 * @vid: the vid where the request came on
590 * Repeat all of our own claims, and finally send an ANNOUNCE frame
591 * to allow the requester another check if the CRC is correct now.
593 static void batadv_bla_answer_request(struct batadv_priv
*bat_priv
,
594 struct batadv_hard_iface
*primary_if
,
597 struct hlist_head
*head
;
598 struct batadv_hashtable
*hash
;
599 struct batadv_bla_claim
*claim
;
600 struct batadv_bla_backbone_gw
*backbone_gw
;
603 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
604 "bla_answer_request(): received a claim request, send all of our own claims again\n");
606 backbone_gw
= batadv_backbone_hash_find(bat_priv
,
607 primary_if
->net_dev
->dev_addr
,
612 hash
= bat_priv
->bla
.claim_hash
;
613 for (i
= 0; i
< hash
->size
; i
++) {
614 head
= &hash
->table
[i
];
617 hlist_for_each_entry_rcu(claim
, head
, hash_entry
) {
618 /* only own claims are interesting */
619 if (claim
->backbone_gw
!= backbone_gw
)
622 batadv_bla_send_claim(bat_priv
, claim
->addr
, claim
->vid
,
623 BATADV_CLAIM_TYPE_CLAIM
);
628 /* finally, send an announcement frame */
629 batadv_bla_send_announce(bat_priv
, backbone_gw
);
630 batadv_backbone_gw_put(backbone_gw
);
634 * batadv_bla_send_request - send a request to repeat claims
635 * @backbone_gw: the backbone gateway from whom we are out of sync
637 * When the crc is wrong, ask the backbone gateway for a full table update.
638 * After the request, it will repeat all of his own claims and finally
639 * send an announcement claim with which we can check again.
641 static void batadv_bla_send_request(struct batadv_bla_backbone_gw
*backbone_gw
)
643 /* first, remove all old entries */
644 batadv_bla_del_backbone_claims(backbone_gw
);
646 batadv_dbg(BATADV_DBG_BLA
, backbone_gw
->bat_priv
,
647 "Sending REQUEST to %pM\n", backbone_gw
->orig
);
650 batadv_bla_send_claim(backbone_gw
->bat_priv
, backbone_gw
->orig
,
651 backbone_gw
->vid
, BATADV_CLAIM_TYPE_REQUEST
);
653 /* no local broadcasts should be sent or received, for now. */
654 if (!atomic_read(&backbone_gw
->request_sent
)) {
655 atomic_inc(&backbone_gw
->bat_priv
->bla
.num_requests
);
656 atomic_set(&backbone_gw
->request_sent
, 1);
661 * batadv_bla_send_announce - Send an announcement frame
662 * @bat_priv: the bat priv with all the soft interface information
663 * @backbone_gw: our backbone gateway which should be announced
665 static void batadv_bla_send_announce(struct batadv_priv
*bat_priv
,
666 struct batadv_bla_backbone_gw
*backbone_gw
)
671 memcpy(mac
, batadv_announce_mac
, 4);
672 spin_lock_bh(&backbone_gw
->crc_lock
);
673 crc
= htons(backbone_gw
->crc
);
674 spin_unlock_bh(&backbone_gw
->crc_lock
);
675 memcpy(&mac
[4], &crc
, 2);
677 batadv_bla_send_claim(bat_priv
, mac
, backbone_gw
->vid
,
678 BATADV_CLAIM_TYPE_ANNOUNCE
);
682 * batadv_bla_add_claim - Adds a claim in the claim hash
683 * @bat_priv: the bat priv with all the soft interface information
684 * @mac: the mac address of the claim
685 * @vid: the VLAN ID of the frame
686 * @backbone_gw: the backbone gateway which claims it
688 static void batadv_bla_add_claim(struct batadv_priv
*bat_priv
,
689 const u8
*mac
, const unsigned short vid
,
690 struct batadv_bla_backbone_gw
*backbone_gw
)
692 struct batadv_bla_backbone_gw
*old_backbone_gw
;
693 struct batadv_bla_claim
*claim
;
694 struct batadv_bla_claim search_claim
;
695 bool remove_crc
= false;
698 ether_addr_copy(search_claim
.addr
, mac
);
699 search_claim
.vid
= vid
;
700 claim
= batadv_claim_hash_find(bat_priv
, &search_claim
);
702 /* create a new claim entry if it does not exist yet. */
704 claim
= kzalloc(sizeof(*claim
), GFP_ATOMIC
);
708 ether_addr_copy(claim
->addr
, mac
);
709 spin_lock_init(&claim
->backbone_lock
);
711 claim
->lasttime
= jiffies
;
712 kref_get(&backbone_gw
->refcount
);
713 claim
->backbone_gw
= backbone_gw
;
715 kref_init(&claim
->refcount
);
716 kref_get(&claim
->refcount
);
717 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
718 "bla_add_claim(): adding new entry %pM, vid %d to hash ...\n",
719 mac
, BATADV_PRINT_VID(vid
));
720 hash_added
= batadv_hash_add(bat_priv
->bla
.claim_hash
,
721 batadv_compare_claim
,
722 batadv_choose_claim
, claim
,
725 if (unlikely(hash_added
!= 0)) {
726 /* only local changes happened. */
731 claim
->lasttime
= jiffies
;
732 if (claim
->backbone_gw
== backbone_gw
)
733 /* no need to register a new backbone */
736 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
737 "bla_add_claim(): changing ownership for %pM, vid %d\n",
738 mac
, BATADV_PRINT_VID(vid
));
743 /* replace backbone_gw atomically and adjust reference counters */
744 spin_lock_bh(&claim
->backbone_lock
);
745 old_backbone_gw
= claim
->backbone_gw
;
746 kref_get(&backbone_gw
->refcount
);
747 claim
->backbone_gw
= backbone_gw
;
748 spin_unlock_bh(&claim
->backbone_lock
);
751 /* remove claim address from old backbone_gw */
752 spin_lock_bh(&old_backbone_gw
->crc_lock
);
753 old_backbone_gw
->crc
^= crc16(0, claim
->addr
, ETH_ALEN
);
754 spin_unlock_bh(&old_backbone_gw
->crc_lock
);
757 batadv_backbone_gw_put(old_backbone_gw
);
759 /* add claim address to new backbone_gw */
760 spin_lock_bh(&backbone_gw
->crc_lock
);
761 backbone_gw
->crc
^= crc16(0, claim
->addr
, ETH_ALEN
);
762 spin_unlock_bh(&backbone_gw
->crc_lock
);
763 backbone_gw
->lasttime
= jiffies
;
766 batadv_claim_put(claim
);
770 * batadv_bla_claim_get_backbone_gw - Get valid reference for backbone_gw of
772 * @claim: claim whose backbone_gw should be returned
774 * Return: valid reference to claim::backbone_gw
776 static struct batadv_bla_backbone_gw
*
777 batadv_bla_claim_get_backbone_gw(struct batadv_bla_claim
*claim
)
779 struct batadv_bla_backbone_gw
*backbone_gw
;
781 spin_lock_bh(&claim
->backbone_lock
);
782 backbone_gw
= claim
->backbone_gw
;
783 kref_get(&backbone_gw
->refcount
);
784 spin_unlock_bh(&claim
->backbone_lock
);
790 * batadv_bla_del_claim - delete a claim from the claim hash
791 * @bat_priv: the bat priv with all the soft interface information
792 * @mac: mac address of the claim to be removed
793 * @vid: VLAN id for the claim to be removed
795 static void batadv_bla_del_claim(struct batadv_priv
*bat_priv
,
796 const u8
*mac
, const unsigned short vid
)
798 struct batadv_bla_claim search_claim
, *claim
;
800 ether_addr_copy(search_claim
.addr
, mac
);
801 search_claim
.vid
= vid
;
802 claim
= batadv_claim_hash_find(bat_priv
, &search_claim
);
806 batadv_dbg(BATADV_DBG_BLA
, bat_priv
, "bla_del_claim(): %pM, vid %d\n",
807 mac
, BATADV_PRINT_VID(vid
));
809 batadv_hash_remove(bat_priv
->bla
.claim_hash
, batadv_compare_claim
,
810 batadv_choose_claim
, claim
);
811 batadv_claim_put(claim
); /* reference from the hash is gone */
813 /* don't need the reference from hash_find() anymore */
814 batadv_claim_put(claim
);
818 * batadv_handle_announce - check for ANNOUNCE frame
819 * @bat_priv: the bat priv with all the soft interface information
820 * @an_addr: announcement mac address (ARP Sender HW address)
821 * @backbone_addr: originator address of the sender (Ethernet source MAC)
822 * @vid: the VLAN ID of the frame
824 * Return: true if handled
826 static bool batadv_handle_announce(struct batadv_priv
*bat_priv
, u8
*an_addr
,
827 u8
*backbone_addr
, unsigned short vid
)
829 struct batadv_bla_backbone_gw
*backbone_gw
;
830 u16 backbone_crc
, crc
;
832 if (memcmp(an_addr
, batadv_announce_mac
, 4) != 0)
835 backbone_gw
= batadv_bla_get_backbone_gw(bat_priv
, backbone_addr
, vid
,
838 if (unlikely(!backbone_gw
))
841 /* handle as ANNOUNCE frame */
842 backbone_gw
->lasttime
= jiffies
;
843 crc
= ntohs(*((__be16
*)(&an_addr
[4])));
845 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
846 "handle_announce(): ANNOUNCE vid %d (sent by %pM)... CRC = %#.4x\n",
847 BATADV_PRINT_VID(vid
), backbone_gw
->orig
, crc
);
849 spin_lock_bh(&backbone_gw
->crc_lock
);
850 backbone_crc
= backbone_gw
->crc
;
851 spin_unlock_bh(&backbone_gw
->crc_lock
);
853 if (backbone_crc
!= crc
) {
854 batadv_dbg(BATADV_DBG_BLA
, backbone_gw
->bat_priv
,
855 "handle_announce(): CRC FAILED for %pM/%d (my = %#.4x, sent = %#.4x)\n",
857 BATADV_PRINT_VID(backbone_gw
->vid
),
860 batadv_bla_send_request(backbone_gw
);
862 /* if we have sent a request and the crc was OK,
863 * we can allow traffic again.
865 if (atomic_read(&backbone_gw
->request_sent
)) {
866 atomic_dec(&backbone_gw
->bat_priv
->bla
.num_requests
);
867 atomic_set(&backbone_gw
->request_sent
, 0);
871 batadv_backbone_gw_put(backbone_gw
);
876 * batadv_handle_request - check for REQUEST frame
877 * @bat_priv: the bat priv with all the soft interface information
878 * @primary_if: the primary hard interface of this batman soft interface
879 * @backbone_addr: backbone address to be requested (ARP sender HW MAC)
880 * @ethhdr: ethernet header of a packet
881 * @vid: the VLAN ID of the frame
883 * Return: true if handled
885 static bool batadv_handle_request(struct batadv_priv
*bat_priv
,
886 struct batadv_hard_iface
*primary_if
,
887 u8
*backbone_addr
, struct ethhdr
*ethhdr
,
890 /* check for REQUEST frame */
891 if (!batadv_compare_eth(backbone_addr
, ethhdr
->h_dest
))
894 /* sanity check, this should not happen on a normal switch,
895 * we ignore it in this case.
897 if (!batadv_compare_eth(ethhdr
->h_dest
, primary_if
->net_dev
->dev_addr
))
900 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
901 "handle_request(): REQUEST vid %d (sent by %pM)...\n",
902 BATADV_PRINT_VID(vid
), ethhdr
->h_source
);
904 batadv_bla_answer_request(bat_priv
, primary_if
, vid
);
909 * batadv_handle_unclaim - check for UNCLAIM frame
910 * @bat_priv: the bat priv with all the soft interface information
911 * @primary_if: the primary hard interface of this batman soft interface
912 * @backbone_addr: originator address of the backbone (Ethernet source)
913 * @claim_addr: Client to be unclaimed (ARP sender HW MAC)
914 * @vid: the VLAN ID of the frame
916 * Return: true if handled
918 static bool batadv_handle_unclaim(struct batadv_priv
*bat_priv
,
919 struct batadv_hard_iface
*primary_if
,
920 u8
*backbone_addr
, u8
*claim_addr
,
923 struct batadv_bla_backbone_gw
*backbone_gw
;
925 /* unclaim in any case if it is our own */
926 if (primary_if
&& batadv_compare_eth(backbone_addr
,
927 primary_if
->net_dev
->dev_addr
))
928 batadv_bla_send_claim(bat_priv
, claim_addr
, vid
,
929 BATADV_CLAIM_TYPE_UNCLAIM
);
931 backbone_gw
= batadv_backbone_hash_find(bat_priv
, backbone_addr
, vid
);
936 /* this must be an UNCLAIM frame */
937 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
938 "handle_unclaim(): UNCLAIM %pM on vid %d (sent by %pM)...\n",
939 claim_addr
, BATADV_PRINT_VID(vid
), backbone_gw
->orig
);
941 batadv_bla_del_claim(bat_priv
, claim_addr
, vid
);
942 batadv_backbone_gw_put(backbone_gw
);
947 * batadv_handle_claim - check for CLAIM frame
948 * @bat_priv: the bat priv with all the soft interface information
949 * @primary_if: the primary hard interface of this batman soft interface
950 * @backbone_addr: originator address of the backbone (Ethernet Source)
951 * @claim_addr: client mac address to be claimed (ARP sender HW MAC)
952 * @vid: the VLAN ID of the frame
954 * Return: true if handled
956 static bool batadv_handle_claim(struct batadv_priv
*bat_priv
,
957 struct batadv_hard_iface
*primary_if
,
958 u8
*backbone_addr
, u8
*claim_addr
,
961 struct batadv_bla_backbone_gw
*backbone_gw
;
963 /* register the gateway if not yet available, and add the claim. */
965 backbone_gw
= batadv_bla_get_backbone_gw(bat_priv
, backbone_addr
, vid
,
968 if (unlikely(!backbone_gw
))
971 /* this must be a CLAIM frame */
972 batadv_bla_add_claim(bat_priv
, claim_addr
, vid
, backbone_gw
);
973 if (batadv_compare_eth(backbone_addr
, primary_if
->net_dev
->dev_addr
))
974 batadv_bla_send_claim(bat_priv
, claim_addr
, vid
,
975 BATADV_CLAIM_TYPE_CLAIM
);
977 /* TODO: we could call something like tt_local_del() here. */
979 batadv_backbone_gw_put(backbone_gw
);
984 * batadv_check_claim_group - check for claim group membership
985 * @bat_priv: the bat priv with all the soft interface information
986 * @primary_if: the primary interface of this batman interface
987 * @hw_src: the Hardware source in the ARP Header
988 * @hw_dst: the Hardware destination in the ARP Header
989 * @ethhdr: pointer to the Ethernet header of the claim frame
991 * checks if it is a claim packet and if its on the same group.
992 * This function also applies the group ID of the sender
993 * if it is in the same mesh.
996 * 2 - if it is a claim packet and on the same group
997 * 1 - if is a claim packet from another group
998 * 0 - if it is not a claim packet
1000 static int batadv_check_claim_group(struct batadv_priv
*bat_priv
,
1001 struct batadv_hard_iface
*primary_if
,
1002 u8
*hw_src
, u8
*hw_dst
,
1003 struct ethhdr
*ethhdr
)
1006 struct batadv_orig_node
*orig_node
;
1007 struct batadv_bla_claim_dst
*bla_dst
, *bla_dst_own
;
1009 bla_dst
= (struct batadv_bla_claim_dst
*)hw_dst
;
1010 bla_dst_own
= &bat_priv
->bla
.claim_dest
;
1012 /* if announcement packet, use the source,
1013 * otherwise assume it is in the hw_src
1015 switch (bla_dst
->type
) {
1016 case BATADV_CLAIM_TYPE_CLAIM
:
1017 backbone_addr
= hw_src
;
1019 case BATADV_CLAIM_TYPE_REQUEST
:
1020 case BATADV_CLAIM_TYPE_ANNOUNCE
:
1021 case BATADV_CLAIM_TYPE_UNCLAIM
:
1022 backbone_addr
= ethhdr
->h_source
;
1028 /* don't accept claim frames from ourselves */
1029 if (batadv_compare_eth(backbone_addr
, primary_if
->net_dev
->dev_addr
))
1032 /* if its already the same group, it is fine. */
1033 if (bla_dst
->group
== bla_dst_own
->group
)
1036 /* lets see if this originator is in our mesh */
1037 orig_node
= batadv_orig_hash_find(bat_priv
, backbone_addr
);
1039 /* dont accept claims from gateways which are not in
1040 * the same mesh or group.
1045 /* if our mesh friends mac is bigger, use it for ourselves. */
1046 if (ntohs(bla_dst
->group
) > ntohs(bla_dst_own
->group
)) {
1047 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
1048 "taking other backbones claim group: %#.4x\n",
1049 ntohs(bla_dst
->group
));
1050 bla_dst_own
->group
= bla_dst
->group
;
1053 batadv_orig_node_put(orig_node
);
1059 * batadv_bla_process_claim - Check if this is a claim frame, and process it
1060 * @bat_priv: the bat priv with all the soft interface information
1061 * @primary_if: the primary hard interface of this batman soft interface
1062 * @skb: the frame to be checked
1064 * Return: true if it was a claim frame, otherwise return false to
1065 * tell the callee that it can use the frame on its own.
1067 static bool batadv_bla_process_claim(struct batadv_priv
*bat_priv
,
1068 struct batadv_hard_iface
*primary_if
,
1069 struct sk_buff
*skb
)
1071 struct batadv_bla_claim_dst
*bla_dst
, *bla_dst_own
;
1072 u8
*hw_src
, *hw_dst
;
1073 struct vlan_hdr
*vhdr
, vhdr_buf
;
1074 struct ethhdr
*ethhdr
;
1075 struct arphdr
*arphdr
;
1082 vid
= batadv_get_vid(skb
, 0);
1083 ethhdr
= eth_hdr(skb
);
1085 proto
= ethhdr
->h_proto
;
1087 if (vid
& BATADV_VLAN_HAS_TAG
) {
1088 /* Traverse the VLAN/Ethertypes.
1090 * At this point it is known that the first protocol is a VLAN
1091 * header, so start checking at the encapsulated protocol.
1093 * The depth of the VLAN headers is recorded to drop BLA claim
1094 * frames encapsulated into multiple VLAN headers (QinQ).
1097 vhdr
= skb_header_pointer(skb
, headlen
, VLAN_HLEN
,
1102 proto
= vhdr
->h_vlan_encapsulated_proto
;
1103 headlen
+= VLAN_HLEN
;
1105 } while (proto
== htons(ETH_P_8021Q
));
1108 if (proto
!= htons(ETH_P_ARP
))
1109 return false; /* not a claim frame */
1111 /* this must be a ARP frame. check if it is a claim. */
1113 if (unlikely(!pskb_may_pull(skb
, headlen
+ arp_hdr_len(skb
->dev
))))
1116 /* pskb_may_pull() may have modified the pointers, get ethhdr again */
1117 ethhdr
= eth_hdr(skb
);
1118 arphdr
= (struct arphdr
*)((u8
*)ethhdr
+ headlen
);
1120 /* Check whether the ARP frame carries a valid
1123 if (arphdr
->ar_hrd
!= htons(ARPHRD_ETHER
))
1125 if (arphdr
->ar_pro
!= htons(ETH_P_IP
))
1127 if (arphdr
->ar_hln
!= ETH_ALEN
)
1129 if (arphdr
->ar_pln
!= 4)
1132 hw_src
= (u8
*)arphdr
+ sizeof(struct arphdr
);
1133 hw_dst
= hw_src
+ ETH_ALEN
+ 4;
1134 bla_dst
= (struct batadv_bla_claim_dst
*)hw_dst
;
1135 bla_dst_own
= &bat_priv
->bla
.claim_dest
;
1137 /* check if it is a claim frame in general */
1138 if (memcmp(bla_dst
->magic
, bla_dst_own
->magic
,
1139 sizeof(bla_dst
->magic
)) != 0)
1142 /* check if there is a claim frame encapsulated deeper in (QinQ) and
1143 * drop that, as this is not supported by BLA but should also not be
1144 * sent via the mesh.
1149 /* Let the loopdetect frames on the mesh in any case. */
1150 if (bla_dst
->type
== BATADV_CLAIM_TYPE_LOOPDETECT
)
1153 /* check if it is a claim frame. */
1154 ret
= batadv_check_claim_group(bat_priv
, primary_if
, hw_src
, hw_dst
,
1157 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
1158 "bla_process_claim(): received a claim frame from another group. From: %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n",
1159 ethhdr
->h_source
, BATADV_PRINT_VID(vid
), hw_src
,
1165 /* become a backbone gw ourselves on this vlan if not happened yet */
1166 batadv_bla_update_own_backbone_gw(bat_priv
, primary_if
, vid
);
1168 /* check for the different types of claim frames ... */
1169 switch (bla_dst
->type
) {
1170 case BATADV_CLAIM_TYPE_CLAIM
:
1171 if (batadv_handle_claim(bat_priv
, primary_if
, hw_src
,
1172 ethhdr
->h_source
, vid
))
1175 case BATADV_CLAIM_TYPE_UNCLAIM
:
1176 if (batadv_handle_unclaim(bat_priv
, primary_if
,
1177 ethhdr
->h_source
, hw_src
, vid
))
1181 case BATADV_CLAIM_TYPE_ANNOUNCE
:
1182 if (batadv_handle_announce(bat_priv
, hw_src
, ethhdr
->h_source
,
1186 case BATADV_CLAIM_TYPE_REQUEST
:
1187 if (batadv_handle_request(bat_priv
, primary_if
, hw_src
, ethhdr
,
1193 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
1194 "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",
1195 ethhdr
->h_source
, BATADV_PRINT_VID(vid
), hw_src
, hw_dst
);
1200 * batadv_bla_purge_backbone_gw - Remove backbone gateways after a timeout or
1202 * @bat_priv: the bat priv with all the soft interface information
1203 * @now: whether the whole hash shall be wiped now
1205 * Check when we last heard from other nodes, and remove them in case of
1206 * a time out, or clean all backbone gws if now is set.
1208 static void batadv_bla_purge_backbone_gw(struct batadv_priv
*bat_priv
, int now
)
1210 struct batadv_bla_backbone_gw
*backbone_gw
;
1211 struct hlist_node
*node_tmp
;
1212 struct hlist_head
*head
;
1213 struct batadv_hashtable
*hash
;
1214 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1217 hash
= bat_priv
->bla
.backbone_hash
;
1221 for (i
= 0; i
< hash
->size
; i
++) {
1222 head
= &hash
->table
[i
];
1223 list_lock
= &hash
->list_locks
[i
];
1225 spin_lock_bh(list_lock
);
1226 hlist_for_each_entry_safe(backbone_gw
, node_tmp
,
1230 if (!batadv_has_timed_out(backbone_gw
->lasttime
,
1231 BATADV_BLA_BACKBONE_TIMEOUT
))
1234 batadv_dbg(BATADV_DBG_BLA
, backbone_gw
->bat_priv
,
1235 "bla_purge_backbone_gw(): backbone gw %pM timed out\n",
1239 /* don't wait for the pending request anymore */
1240 if (atomic_read(&backbone_gw
->request_sent
))
1241 atomic_dec(&bat_priv
->bla
.num_requests
);
1243 batadv_bla_del_backbone_claims(backbone_gw
);
1245 hlist_del_rcu(&backbone_gw
->hash_entry
);
1246 batadv_backbone_gw_put(backbone_gw
);
1248 spin_unlock_bh(list_lock
);
1253 * batadv_bla_purge_claims - Remove claims after a timeout or immediately
1254 * @bat_priv: the bat priv with all the soft interface information
1255 * @primary_if: the selected primary interface, may be NULL if now is set
1256 * @now: whether the whole hash shall be wiped now
1258 * Check when we heard last time from our own claims, and remove them in case of
1259 * a time out, or clean all claims if now is set
1261 static void batadv_bla_purge_claims(struct batadv_priv
*bat_priv
,
1262 struct batadv_hard_iface
*primary_if
,
1265 struct batadv_bla_backbone_gw
*backbone_gw
;
1266 struct batadv_bla_claim
*claim
;
1267 struct hlist_head
*head
;
1268 struct batadv_hashtable
*hash
;
1271 hash
= bat_priv
->bla
.claim_hash
;
1275 for (i
= 0; i
< hash
->size
; i
++) {
1276 head
= &hash
->table
[i
];
1279 hlist_for_each_entry_rcu(claim
, head
, hash_entry
) {
1280 backbone_gw
= batadv_bla_claim_get_backbone_gw(claim
);
1284 if (!batadv_compare_eth(backbone_gw
->orig
,
1285 primary_if
->net_dev
->dev_addr
))
1288 if (!batadv_has_timed_out(claim
->lasttime
,
1289 BATADV_BLA_CLAIM_TIMEOUT
))
1292 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
1293 "bla_purge_claims(): %pM, vid %d, time out\n",
1294 claim
->addr
, claim
->vid
);
1297 batadv_handle_unclaim(bat_priv
, primary_if
,
1299 claim
->addr
, claim
->vid
);
1301 batadv_backbone_gw_put(backbone_gw
);
1308 * batadv_bla_update_orig_address - Update the backbone gateways when the own
1309 * originator address changes
1310 * @bat_priv: the bat priv with all the soft interface information
1311 * @primary_if: the new selected primary_if
1312 * @oldif: the old primary interface, may be NULL
1314 void batadv_bla_update_orig_address(struct batadv_priv
*bat_priv
,
1315 struct batadv_hard_iface
*primary_if
,
1316 struct batadv_hard_iface
*oldif
)
1318 struct batadv_bla_backbone_gw
*backbone_gw
;
1319 struct hlist_head
*head
;
1320 struct batadv_hashtable
*hash
;
1324 /* reset bridge loop avoidance group id */
1325 group
= htons(crc16(0, primary_if
->net_dev
->dev_addr
, ETH_ALEN
));
1326 bat_priv
->bla
.claim_dest
.group
= group
;
1328 /* purge everything when bridge loop avoidance is turned off */
1329 if (!atomic_read(&bat_priv
->bridge_loop_avoidance
))
1333 batadv_bla_purge_claims(bat_priv
, NULL
, 1);
1334 batadv_bla_purge_backbone_gw(bat_priv
, 1);
1338 hash
= bat_priv
->bla
.backbone_hash
;
1342 for (i
= 0; i
< hash
->size
; i
++) {
1343 head
= &hash
->table
[i
];
1346 hlist_for_each_entry_rcu(backbone_gw
, head
, hash_entry
) {
1347 /* own orig still holds the old value. */
1348 if (!batadv_compare_eth(backbone_gw
->orig
,
1349 oldif
->net_dev
->dev_addr
))
1352 ether_addr_copy(backbone_gw
->orig
,
1353 primary_if
->net_dev
->dev_addr
);
1354 /* send an announce frame so others will ask for our
1355 * claims and update their tables.
1357 batadv_bla_send_announce(bat_priv
, backbone_gw
);
1364 * batadv_bla_send_loopdetect - send a loopdetect frame
1365 * @bat_priv: the bat priv with all the soft interface information
1366 * @backbone_gw: the backbone gateway for which a loop should be detected
1368 * To detect loops that the bridge loop avoidance can't handle, send a loop
1369 * detection packet on the backbone. Unlike other BLA frames, this frame will
1370 * be allowed on the mesh by other nodes. If it is received on the mesh, this
1371 * indicates that there is a loop.
1374 batadv_bla_send_loopdetect(struct batadv_priv
*bat_priv
,
1375 struct batadv_bla_backbone_gw
*backbone_gw
)
1377 batadv_dbg(BATADV_DBG_BLA
, bat_priv
, "Send loopdetect frame for vid %d\n",
1379 batadv_bla_send_claim(bat_priv
, bat_priv
->bla
.loopdetect_addr
,
1380 backbone_gw
->vid
, BATADV_CLAIM_TYPE_LOOPDETECT
);
1384 * batadv_bla_status_update - purge bla interfaces if necessary
1385 * @net_dev: the soft interface net device
1387 void batadv_bla_status_update(struct net_device
*net_dev
)
1389 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
1390 struct batadv_hard_iface
*primary_if
;
1392 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1396 /* this function already purges everything when bla is disabled,
1397 * so just call that one.
1399 batadv_bla_update_orig_address(bat_priv
, primary_if
, primary_if
);
1400 batadv_hardif_put(primary_if
);
1404 * batadv_bla_periodic_work - performs periodic bla work
1405 * @work: kernel work struct
1407 * periodic work to do:
1408 * * purge structures when they are too old
1409 * * send announcements
1411 static void batadv_bla_periodic_work(struct work_struct
*work
)
1413 struct delayed_work
*delayed_work
;
1414 struct batadv_priv
*bat_priv
;
1415 struct batadv_priv_bla
*priv_bla
;
1416 struct hlist_head
*head
;
1417 struct batadv_bla_backbone_gw
*backbone_gw
;
1418 struct batadv_hashtable
*hash
;
1419 struct batadv_hard_iface
*primary_if
;
1420 bool send_loopdetect
= false;
1423 delayed_work
= to_delayed_work(work
);
1424 priv_bla
= container_of(delayed_work
, struct batadv_priv_bla
, work
);
1425 bat_priv
= container_of(priv_bla
, struct batadv_priv
, bla
);
1426 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1430 batadv_bla_purge_claims(bat_priv
, primary_if
, 0);
1431 batadv_bla_purge_backbone_gw(bat_priv
, 0);
1433 if (!atomic_read(&bat_priv
->bridge_loop_avoidance
))
1436 if (atomic_dec_and_test(&bat_priv
->bla
.loopdetect_next
)) {
1437 /* set a new random mac address for the next bridge loop
1438 * detection frames. Set the locally administered bit to avoid
1439 * collisions with users mac addresses.
1441 random_ether_addr(bat_priv
->bla
.loopdetect_addr
);
1442 bat_priv
->bla
.loopdetect_addr
[0] = 0xba;
1443 bat_priv
->bla
.loopdetect_addr
[1] = 0xbe;
1444 bat_priv
->bla
.loopdetect_lasttime
= jiffies
;
1445 atomic_set(&bat_priv
->bla
.loopdetect_next
,
1446 BATADV_BLA_LOOPDETECT_PERIODS
);
1448 /* mark for sending loop detect on all VLANs */
1449 send_loopdetect
= true;
1452 hash
= bat_priv
->bla
.backbone_hash
;
1456 for (i
= 0; i
< hash
->size
; i
++) {
1457 head
= &hash
->table
[i
];
1460 hlist_for_each_entry_rcu(backbone_gw
, head
, hash_entry
) {
1461 if (!batadv_compare_eth(backbone_gw
->orig
,
1462 primary_if
->net_dev
->dev_addr
))
1465 backbone_gw
->lasttime
= jiffies
;
1467 batadv_bla_send_announce(bat_priv
, backbone_gw
);
1468 if (send_loopdetect
)
1469 batadv_bla_send_loopdetect(bat_priv
,
1472 /* request_sent is only set after creation to avoid
1473 * problems when we are not yet known as backbone gw
1476 * We can reset this now after we waited some periods
1477 * to give bridge forward delays and bla group forming
1481 if (atomic_read(&backbone_gw
->request_sent
) == 0)
1484 if (!atomic_dec_and_test(&backbone_gw
->wait_periods
))
1487 atomic_dec(&backbone_gw
->bat_priv
->bla
.num_requests
);
1488 atomic_set(&backbone_gw
->request_sent
, 0);
1494 batadv_hardif_put(primary_if
);
1496 queue_delayed_work(batadv_event_workqueue
, &bat_priv
->bla
.work
,
1497 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH
));
1500 /* The hash for claim and backbone hash receive the same key because they
1501 * are getting initialized by hash_new with the same key. Reinitializing
1502 * them with to different keys to allow nested locking without generating
1505 static struct lock_class_key batadv_claim_hash_lock_class_key
;
1506 static struct lock_class_key batadv_backbone_hash_lock_class_key
;
1509 * batadv_bla_init - initialize all bla structures
1510 * @bat_priv: the bat priv with all the soft interface information
1512 * Return: 0 on success, < 0 on error.
1514 int batadv_bla_init(struct batadv_priv
*bat_priv
)
1517 u8 claim_dest
[ETH_ALEN
] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00};
1518 struct batadv_hard_iface
*primary_if
;
1520 unsigned long entrytime
;
1522 spin_lock_init(&bat_priv
->bla
.bcast_duplist_lock
);
1524 batadv_dbg(BATADV_DBG_BLA
, bat_priv
, "bla hash registering\n");
1526 /* setting claim destination address */
1527 memcpy(&bat_priv
->bla
.claim_dest
.magic
, claim_dest
, 3);
1528 bat_priv
->bla
.claim_dest
.type
= 0;
1529 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1531 crc
= crc16(0, primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
1532 bat_priv
->bla
.claim_dest
.group
= htons(crc
);
1533 batadv_hardif_put(primary_if
);
1535 bat_priv
->bla
.claim_dest
.group
= 0; /* will be set later */
1538 /* initialize the duplicate list */
1539 entrytime
= jiffies
- msecs_to_jiffies(BATADV_DUPLIST_TIMEOUT
);
1540 for (i
= 0; i
< BATADV_DUPLIST_SIZE
; i
++)
1541 bat_priv
->bla
.bcast_duplist
[i
].entrytime
= entrytime
;
1542 bat_priv
->bla
.bcast_duplist_curr
= 0;
1544 atomic_set(&bat_priv
->bla
.loopdetect_next
,
1545 BATADV_BLA_LOOPDETECT_PERIODS
);
1547 if (bat_priv
->bla
.claim_hash
)
1550 bat_priv
->bla
.claim_hash
= batadv_hash_new(128);
1551 bat_priv
->bla
.backbone_hash
= batadv_hash_new(32);
1553 if (!bat_priv
->bla
.claim_hash
|| !bat_priv
->bla
.backbone_hash
)
1556 batadv_hash_set_lock_class(bat_priv
->bla
.claim_hash
,
1557 &batadv_claim_hash_lock_class_key
);
1558 batadv_hash_set_lock_class(bat_priv
->bla
.backbone_hash
,
1559 &batadv_backbone_hash_lock_class_key
);
1561 batadv_dbg(BATADV_DBG_BLA
, bat_priv
, "bla hashes initialized\n");
1563 INIT_DELAYED_WORK(&bat_priv
->bla
.work
, batadv_bla_periodic_work
);
1565 queue_delayed_work(batadv_event_workqueue
, &bat_priv
->bla
.work
,
1566 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH
));
1571 * batadv_bla_check_bcast_duplist - Check if a frame is in the broadcast dup.
1572 * @bat_priv: the bat priv with all the soft interface information
1573 * @skb: contains the bcast_packet to be checked
1575 * check if it is on our broadcast list. Another gateway might
1576 * have sent the same packet because it is connected to the same backbone,
1577 * so we have to remove this duplicate.
1579 * This is performed by checking the CRC, which will tell us
1580 * with a good chance that it is the same packet. If it is furthermore
1581 * sent by another host, drop it. We allow equal packets from
1582 * the same host however as this might be intended.
1584 * Return: true if a packet is in the duplicate list, false otherwise.
1586 bool batadv_bla_check_bcast_duplist(struct batadv_priv
*bat_priv
,
1587 struct sk_buff
*skb
)
1591 struct batadv_bcast_packet
*bcast_packet
;
1592 struct batadv_bcast_duplist_entry
*entry
;
1595 bcast_packet
= (struct batadv_bcast_packet
*)skb
->data
;
1597 /* calculate the crc ... */
1598 crc
= batadv_skb_crc32(skb
, (u8
*)(bcast_packet
+ 1));
1600 spin_lock_bh(&bat_priv
->bla
.bcast_duplist_lock
);
1602 for (i
= 0; i
< BATADV_DUPLIST_SIZE
; i
++) {
1603 curr
= (bat_priv
->bla
.bcast_duplist_curr
+ i
);
1604 curr
%= BATADV_DUPLIST_SIZE
;
1605 entry
= &bat_priv
->bla
.bcast_duplist
[curr
];
1607 /* we can stop searching if the entry is too old ;
1608 * later entries will be even older
1610 if (batadv_has_timed_out(entry
->entrytime
,
1611 BATADV_DUPLIST_TIMEOUT
))
1614 if (entry
->crc
!= crc
)
1617 if (batadv_compare_eth(entry
->orig
, bcast_packet
->orig
))
1620 /* this entry seems to match: same crc, not too old,
1621 * and from another gw. therefore return true to forbid it.
1626 /* not found, add a new entry (overwrite the oldest entry)
1627 * and allow it, its the first occurrence.
1629 curr
= (bat_priv
->bla
.bcast_duplist_curr
+ BATADV_DUPLIST_SIZE
- 1);
1630 curr
%= BATADV_DUPLIST_SIZE
;
1631 entry
= &bat_priv
->bla
.bcast_duplist
[curr
];
1633 entry
->entrytime
= jiffies
;
1634 ether_addr_copy(entry
->orig
, bcast_packet
->orig
);
1635 bat_priv
->bla
.bcast_duplist_curr
= curr
;
1638 spin_unlock_bh(&bat_priv
->bla
.bcast_duplist_lock
);
1644 * batadv_bla_is_backbone_gw_orig - Check if the originator is a gateway for
1645 * the VLAN identified by vid.
1646 * @bat_priv: the bat priv with all the soft interface information
1647 * @orig: originator mac address
1648 * @vid: VLAN identifier
1650 * Return: true if orig is a backbone for this vid, false otherwise.
1652 bool batadv_bla_is_backbone_gw_orig(struct batadv_priv
*bat_priv
, u8
*orig
,
1655 struct batadv_hashtable
*hash
= bat_priv
->bla
.backbone_hash
;
1656 struct hlist_head
*head
;
1657 struct batadv_bla_backbone_gw
*backbone_gw
;
1660 if (!atomic_read(&bat_priv
->bridge_loop_avoidance
))
1666 for (i
= 0; i
< hash
->size
; i
++) {
1667 head
= &hash
->table
[i
];
1670 hlist_for_each_entry_rcu(backbone_gw
, head
, hash_entry
) {
1671 if (batadv_compare_eth(backbone_gw
->orig
, orig
) &&
1672 backbone_gw
->vid
== vid
) {
1684 * batadv_bla_is_backbone_gw - check if originator is a backbone gw for a VLAN.
1685 * @skb: the frame to be checked
1686 * @orig_node: the orig_node of the frame
1687 * @hdr_size: maximum length of the frame
1689 * Return: true if the orig_node is also a gateway on the soft interface,
1690 * otherwise it returns false.
1692 bool batadv_bla_is_backbone_gw(struct sk_buff
*skb
,
1693 struct batadv_orig_node
*orig_node
, int hdr_size
)
1695 struct batadv_bla_backbone_gw
*backbone_gw
;
1698 if (!atomic_read(&orig_node
->bat_priv
->bridge_loop_avoidance
))
1701 /* first, find out the vid. */
1702 if (!pskb_may_pull(skb
, hdr_size
+ ETH_HLEN
))
1705 vid
= batadv_get_vid(skb
, hdr_size
);
1707 /* see if this originator is a backbone gw for this VLAN */
1708 backbone_gw
= batadv_backbone_hash_find(orig_node
->bat_priv
,
1709 orig_node
->orig
, vid
);
1713 batadv_backbone_gw_put(backbone_gw
);
1718 * batadv_bla_free - free all bla structures
1719 * @bat_priv: the bat priv with all the soft interface information
1721 * for softinterface free or module unload
1723 void batadv_bla_free(struct batadv_priv
*bat_priv
)
1725 struct batadv_hard_iface
*primary_if
;
1727 cancel_delayed_work_sync(&bat_priv
->bla
.work
);
1728 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1730 if (bat_priv
->bla
.claim_hash
) {
1731 batadv_bla_purge_claims(bat_priv
, primary_if
, 1);
1732 batadv_hash_destroy(bat_priv
->bla
.claim_hash
);
1733 bat_priv
->bla
.claim_hash
= NULL
;
1735 if (bat_priv
->bla
.backbone_hash
) {
1736 batadv_bla_purge_backbone_gw(bat_priv
, 1);
1737 batadv_hash_destroy(bat_priv
->bla
.backbone_hash
);
1738 bat_priv
->bla
.backbone_hash
= NULL
;
1741 batadv_hardif_put(primary_if
);
1745 * batadv_bla_loopdetect_check - check and handle a detected loop
1746 * @bat_priv: the bat priv with all the soft interface information
1747 * @skb: the packet to check
1748 * @primary_if: interface where the request came on
1749 * @vid: the VLAN ID of the frame
1751 * Checks if this packet is a loop detect frame which has been sent by us,
1752 * throw an uevent and log the event if that is the case.
1754 * Return: true if it is a loop detect frame which is to be dropped, false
1758 batadv_bla_loopdetect_check(struct batadv_priv
*bat_priv
, struct sk_buff
*skb
,
1759 struct batadv_hard_iface
*primary_if
,
1762 struct batadv_bla_backbone_gw
*backbone_gw
;
1763 struct ethhdr
*ethhdr
;
1765 ethhdr
= eth_hdr(skb
);
1767 /* Only check for the MAC address and skip more checks here for
1768 * performance reasons - this function is on the hotpath, after all.
1770 if (!batadv_compare_eth(ethhdr
->h_source
,
1771 bat_priv
->bla
.loopdetect_addr
))
1774 /* If the packet came too late, don't forward it on the mesh
1775 * but don't consider that as loop. It might be a coincidence.
1777 if (batadv_has_timed_out(bat_priv
->bla
.loopdetect_lasttime
,
1778 BATADV_BLA_LOOPDETECT_TIMEOUT
))
1781 backbone_gw
= batadv_bla_get_backbone_gw(bat_priv
,
1782 primary_if
->net_dev
->dev_addr
,
1784 if (unlikely(!backbone_gw
))
1787 queue_work(batadv_event_workqueue
, &backbone_gw
->report_work
);
1788 /* backbone_gw is unreferenced in the report work function function */
1794 * batadv_bla_rx - check packets coming from the mesh.
1795 * @bat_priv: the bat priv with all the soft interface information
1796 * @skb: the frame to be checked
1797 * @vid: the VLAN ID of the frame
1798 * @is_bcast: the packet came in a broadcast packet type.
1800 * batadv_bla_rx avoidance checks if:
1801 * * we have to race for a claim
1802 * * if the frame is allowed on the LAN
1804 * in these cases, the skb is further handled by this function
1806 * Return: true if handled, otherwise it returns false and the caller shall
1807 * further process the skb.
1809 bool batadv_bla_rx(struct batadv_priv
*bat_priv
, struct sk_buff
*skb
,
1810 unsigned short vid
, bool is_bcast
)
1812 struct batadv_bla_backbone_gw
*backbone_gw
;
1813 struct ethhdr
*ethhdr
;
1814 struct batadv_bla_claim search_claim
, *claim
= NULL
;
1815 struct batadv_hard_iface
*primary_if
;
1819 ethhdr
= eth_hdr(skb
);
1821 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1825 if (!atomic_read(&bat_priv
->bridge_loop_avoidance
))
1828 if (batadv_bla_loopdetect_check(bat_priv
, skb
, primary_if
, vid
))
1831 if (unlikely(atomic_read(&bat_priv
->bla
.num_requests
)))
1832 /* don't allow broadcasts while requests are in flight */
1833 if (is_multicast_ether_addr(ethhdr
->h_dest
) && is_bcast
)
1836 ether_addr_copy(search_claim
.addr
, ethhdr
->h_source
);
1837 search_claim
.vid
= vid
;
1838 claim
= batadv_claim_hash_find(bat_priv
, &search_claim
);
1841 /* possible optimization: race for a claim */
1842 /* No claim exists yet, claim it for us!
1844 batadv_handle_claim(bat_priv
, primary_if
,
1845 primary_if
->net_dev
->dev_addr
,
1846 ethhdr
->h_source
, vid
);
1850 /* if it is our own claim ... */
1851 backbone_gw
= batadv_bla_claim_get_backbone_gw(claim
);
1852 own_claim
= batadv_compare_eth(backbone_gw
->orig
,
1853 primary_if
->net_dev
->dev_addr
);
1854 batadv_backbone_gw_put(backbone_gw
);
1857 /* ... allow it in any case */
1858 claim
->lasttime
= jiffies
;
1862 /* if it is a broadcast ... */
1863 if (is_multicast_ether_addr(ethhdr
->h_dest
) && is_bcast
) {
1864 /* ... drop it. the responsible gateway is in charge.
1866 * We need to check is_bcast because with the gateway
1867 * feature, broadcasts (like DHCP requests) may be sent
1868 * using a unicast packet type.
1872 /* seems the client considers us as its best gateway.
1873 * send a claim and update the claim table
1876 batadv_handle_claim(bat_priv
, primary_if
,
1877 primary_if
->net_dev
->dev_addr
,
1878 ethhdr
->h_source
, vid
);
1882 batadv_bla_update_own_backbone_gw(bat_priv
, primary_if
, vid
);
1892 batadv_hardif_put(primary_if
);
1894 batadv_claim_put(claim
);
1899 * batadv_bla_tx - check packets going into the mesh
1900 * @bat_priv: the bat priv with all the soft interface information
1901 * @skb: the frame to be checked
1902 * @vid: the VLAN ID of the frame
1904 * batadv_bla_tx checks if:
1905 * * a claim was received which has to be processed
1906 * * the frame is allowed on the mesh
1908 * in these cases, the skb is further handled by this function.
1910 * This call might reallocate skb data.
1912 * Return: true if handled, otherwise it returns false and the caller shall
1913 * further process the skb.
1915 bool batadv_bla_tx(struct batadv_priv
*bat_priv
, struct sk_buff
*skb
,
1918 struct ethhdr
*ethhdr
;
1919 struct batadv_bla_claim search_claim
, *claim
= NULL
;
1920 struct batadv_bla_backbone_gw
*backbone_gw
;
1921 struct batadv_hard_iface
*primary_if
;
1925 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1929 if (!atomic_read(&bat_priv
->bridge_loop_avoidance
))
1932 if (batadv_bla_process_claim(bat_priv
, primary_if
, skb
))
1935 ethhdr
= eth_hdr(skb
);
1937 if (unlikely(atomic_read(&bat_priv
->bla
.num_requests
)))
1938 /* don't allow broadcasts while requests are in flight */
1939 if (is_multicast_ether_addr(ethhdr
->h_dest
))
1942 ether_addr_copy(search_claim
.addr
, ethhdr
->h_source
);
1943 search_claim
.vid
= vid
;
1945 claim
= batadv_claim_hash_find(bat_priv
, &search_claim
);
1947 /* if no claim exists, allow it. */
1951 /* check if we are responsible. */
1952 backbone_gw
= batadv_bla_claim_get_backbone_gw(claim
);
1953 client_roamed
= batadv_compare_eth(backbone_gw
->orig
,
1954 primary_if
->net_dev
->dev_addr
);
1955 batadv_backbone_gw_put(backbone_gw
);
1957 if (client_roamed
) {
1958 /* if yes, the client has roamed and we have
1961 batadv_handle_unclaim(bat_priv
, primary_if
,
1962 primary_if
->net_dev
->dev_addr
,
1963 ethhdr
->h_source
, vid
);
1967 /* check if it is a multicast/broadcast frame */
1968 if (is_multicast_ether_addr(ethhdr
->h_dest
)) {
1969 /* drop it. the responsible gateway has forwarded it into
1970 * the backbone network.
1974 /* we must allow it. at least if we are
1975 * responsible for the DESTINATION.
1980 batadv_bla_update_own_backbone_gw(bat_priv
, primary_if
, vid
);
1987 batadv_hardif_put(primary_if
);
1989 batadv_claim_put(claim
);
1994 * batadv_bla_claim_table_seq_print_text - print the claim table in a seq file
1995 * @seq: seq file to print on
2000 int batadv_bla_claim_table_seq_print_text(struct seq_file
*seq
, void *offset
)
2002 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
2003 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
2004 struct batadv_hashtable
*hash
= bat_priv
->bla
.claim_hash
;
2005 struct batadv_bla_backbone_gw
*backbone_gw
;
2006 struct batadv_bla_claim
*claim
;
2007 struct batadv_hard_iface
*primary_if
;
2008 struct hlist_head
*head
;
2014 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
2018 primary_addr
= primary_if
->net_dev
->dev_addr
;
2020 "Claims announced for the mesh %s (orig %pM, group id %#.4x)\n",
2021 net_dev
->name
, primary_addr
,
2022 ntohs(bat_priv
->bla
.claim_dest
.group
));
2024 " Client VID Originator [o] (CRC )\n");
2025 for (i
= 0; i
< hash
->size
; i
++) {
2026 head
= &hash
->table
[i
];
2029 hlist_for_each_entry_rcu(claim
, head
, hash_entry
) {
2030 backbone_gw
= batadv_bla_claim_get_backbone_gw(claim
);
2032 is_own
= batadv_compare_eth(backbone_gw
->orig
,
2035 spin_lock_bh(&backbone_gw
->crc_lock
);
2036 backbone_crc
= backbone_gw
->crc
;
2037 spin_unlock_bh(&backbone_gw
->crc_lock
);
2038 seq_printf(seq
, " * %pM on %5d by %pM [%c] (%#.4x)\n",
2039 claim
->addr
, BATADV_PRINT_VID(claim
->vid
),
2041 (is_own
? 'x' : ' '),
2044 batadv_backbone_gw_put(backbone_gw
);
2050 batadv_hardif_put(primary_if
);
2055 * batadv_bla_backbone_table_seq_print_text - print the backbone table in a seq
2057 * @seq: seq file to print on
2062 int batadv_bla_backbone_table_seq_print_text(struct seq_file
*seq
, void *offset
)
2064 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
2065 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
2066 struct batadv_hashtable
*hash
= bat_priv
->bla
.backbone_hash
;
2067 struct batadv_bla_backbone_gw
*backbone_gw
;
2068 struct batadv_hard_iface
*primary_if
;
2069 struct hlist_head
*head
;
2076 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
2080 primary_addr
= primary_if
->net_dev
->dev_addr
;
2082 "Backbones announced for the mesh %s (orig %pM, group id %#.4x)\n",
2083 net_dev
->name
, primary_addr
,
2084 ntohs(bat_priv
->bla
.claim_dest
.group
));
2085 seq_puts(seq
, " Originator VID last seen (CRC )\n");
2086 for (i
= 0; i
< hash
->size
; i
++) {
2087 head
= &hash
->table
[i
];
2090 hlist_for_each_entry_rcu(backbone_gw
, head
, hash_entry
) {
2091 msecs
= jiffies_to_msecs(jiffies
-
2092 backbone_gw
->lasttime
);
2093 secs
= msecs
/ 1000;
2094 msecs
= msecs
% 1000;
2096 is_own
= batadv_compare_eth(backbone_gw
->orig
,
2101 spin_lock_bh(&backbone_gw
->crc_lock
);
2102 backbone_crc
= backbone_gw
->crc
;
2103 spin_unlock_bh(&backbone_gw
->crc_lock
);
2105 seq_printf(seq
, " * %pM on %5d %4i.%03is (%#.4x)\n",
2107 BATADV_PRINT_VID(backbone_gw
->vid
), secs
,
2108 msecs
, backbone_crc
);
2114 batadv_hardif_put(primary_if
);