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
;
182 claim
= container_of(ref
, struct batadv_bla_claim
, refcount
);
184 batadv_backbone_gw_put(claim
->backbone_gw
);
185 kfree_rcu(claim
, rcu
);
189 * batadv_claim_put - decrement the claim refcounter and possibly
191 * @claim: claim to be free'd
193 static void batadv_claim_put(struct batadv_bla_claim
*claim
)
195 kref_put(&claim
->refcount
, batadv_claim_release
);
199 * batadv_claim_hash_find - looks for a claim in the claim hash
200 * @bat_priv: the bat priv with all the soft interface information
201 * @data: search data (may be local/static data)
203 * Return: claim if found or NULL otherwise.
205 static struct batadv_bla_claim
*
206 batadv_claim_hash_find(struct batadv_priv
*bat_priv
,
207 struct batadv_bla_claim
*data
)
209 struct batadv_hashtable
*hash
= bat_priv
->bla
.claim_hash
;
210 struct hlist_head
*head
;
211 struct batadv_bla_claim
*claim
;
212 struct batadv_bla_claim
*claim_tmp
= NULL
;
218 index
= batadv_choose_claim(data
, hash
->size
);
219 head
= &hash
->table
[index
];
222 hlist_for_each_entry_rcu(claim
, head
, hash_entry
) {
223 if (!batadv_compare_claim(&claim
->hash_entry
, data
))
226 if (!kref_get_unless_zero(&claim
->refcount
))
238 * batadv_backbone_hash_find - looks for a backbone gateway in the hash
239 * @bat_priv: the bat priv with all the soft interface information
240 * @addr: the address of the originator
243 * Return: backbone gateway if found or NULL otherwise
245 static struct batadv_bla_backbone_gw
*
246 batadv_backbone_hash_find(struct batadv_priv
*bat_priv
, u8
*addr
,
249 struct batadv_hashtable
*hash
= bat_priv
->bla
.backbone_hash
;
250 struct hlist_head
*head
;
251 struct batadv_bla_backbone_gw search_entry
, *backbone_gw
;
252 struct batadv_bla_backbone_gw
*backbone_gw_tmp
= NULL
;
258 ether_addr_copy(search_entry
.orig
, addr
);
259 search_entry
.vid
= vid
;
261 index
= batadv_choose_backbone_gw(&search_entry
, hash
->size
);
262 head
= &hash
->table
[index
];
265 hlist_for_each_entry_rcu(backbone_gw
, head
, hash_entry
) {
266 if (!batadv_compare_backbone_gw(&backbone_gw
->hash_entry
,
270 if (!kref_get_unless_zero(&backbone_gw
->refcount
))
273 backbone_gw_tmp
= backbone_gw
;
278 return backbone_gw_tmp
;
282 * batadv_bla_del_backbone_claims - delete all claims for a backbone
283 * @backbone_gw: backbone gateway where the claims should be removed
286 batadv_bla_del_backbone_claims(struct batadv_bla_backbone_gw
*backbone_gw
)
288 struct batadv_hashtable
*hash
;
289 struct hlist_node
*node_tmp
;
290 struct hlist_head
*head
;
291 struct batadv_bla_claim
*claim
;
293 spinlock_t
*list_lock
; /* protects write access to the hash lists */
295 hash
= backbone_gw
->bat_priv
->bla
.claim_hash
;
299 for (i
= 0; i
< hash
->size
; i
++) {
300 head
= &hash
->table
[i
];
301 list_lock
= &hash
->list_locks
[i
];
303 spin_lock_bh(list_lock
);
304 hlist_for_each_entry_safe(claim
, node_tmp
,
306 if (claim
->backbone_gw
!= backbone_gw
)
309 batadv_claim_put(claim
);
310 hlist_del_rcu(&claim
->hash_entry
);
312 spin_unlock_bh(list_lock
);
315 /* all claims gone, initialize CRC */
316 spin_lock_bh(&backbone_gw
->crc_lock
);
317 backbone_gw
->crc
= BATADV_BLA_CRC_INIT
;
318 spin_unlock_bh(&backbone_gw
->crc_lock
);
322 * batadv_bla_send_claim - sends a claim frame according to the provided info
323 * @bat_priv: the bat priv with all the soft interface information
324 * @mac: the mac address to be announced within the claim
326 * @claimtype: the type of the claim (CLAIM, UNCLAIM, ANNOUNCE, ...)
328 static void batadv_bla_send_claim(struct batadv_priv
*bat_priv
, u8
*mac
,
329 unsigned short vid
, int claimtype
)
332 struct ethhdr
*ethhdr
;
333 struct batadv_hard_iface
*primary_if
;
334 struct net_device
*soft_iface
;
336 struct batadv_bla_claim_dst local_claim_dest
;
339 primary_if
= batadv_primary_if_get_selected(bat_priv
);
343 memcpy(&local_claim_dest
, &bat_priv
->bla
.claim_dest
,
344 sizeof(local_claim_dest
));
345 local_claim_dest
.type
= claimtype
;
347 soft_iface
= primary_if
->soft_iface
;
349 skb
= arp_create(ARPOP_REPLY
, ETH_P_ARP
,
350 /* IP DST: 0.0.0.0 */
352 primary_if
->soft_iface
,
353 /* IP SRC: 0.0.0.0 */
355 /* Ethernet DST: Broadcast */
357 /* Ethernet SRC/HW SRC: originator mac */
358 primary_if
->net_dev
->dev_addr
,
359 /* HW DST: FF:43:05:XX:YY:YY
360 * with XX = claim type
361 * and YY:YY = group id
363 (u8
*)&local_claim_dest
);
368 ethhdr
= (struct ethhdr
*)skb
->data
;
369 hw_src
= (u8
*)ethhdr
+ ETH_HLEN
+ sizeof(struct arphdr
);
371 /* now we pretend that the client would have sent this ... */
373 case BATADV_CLAIM_TYPE_CLAIM
:
374 /* normal claim frame
375 * set Ethernet SRC to the clients mac
377 ether_addr_copy(ethhdr
->h_source
, mac
);
378 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
379 "bla_send_claim(): CLAIM %pM on vid %d\n", mac
,
380 BATADV_PRINT_VID(vid
));
382 case BATADV_CLAIM_TYPE_UNCLAIM
:
384 * set HW SRC to the clients mac
386 ether_addr_copy(hw_src
, mac
);
387 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
388 "bla_send_claim(): UNCLAIM %pM on vid %d\n", mac
,
389 BATADV_PRINT_VID(vid
));
391 case BATADV_CLAIM_TYPE_ANNOUNCE
:
392 /* announcement frame
393 * set HW SRC to the special mac containg the crc
395 ether_addr_copy(hw_src
, mac
);
396 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
397 "bla_send_claim(): ANNOUNCE of %pM on vid %d\n",
398 ethhdr
->h_source
, BATADV_PRINT_VID(vid
));
400 case BATADV_CLAIM_TYPE_REQUEST
:
402 * set HW SRC and header destination to the receiving backbone
405 ether_addr_copy(hw_src
, mac
);
406 ether_addr_copy(ethhdr
->h_dest
, mac
);
407 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
408 "bla_send_claim(): REQUEST of %pM to %pM on vid %d\n",
409 ethhdr
->h_source
, ethhdr
->h_dest
,
410 BATADV_PRINT_VID(vid
));
412 case BATADV_CLAIM_TYPE_LOOPDETECT
:
413 ether_addr_copy(ethhdr
->h_source
, mac
);
414 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
415 "bla_send_claim(): LOOPDETECT of %pM to %pM on vid %d\n",
416 ethhdr
->h_source
, ethhdr
->h_dest
,
417 BATADV_PRINT_VID(vid
));
422 if (vid
& BATADV_VLAN_HAS_TAG
)
423 skb
= vlan_insert_tag(skb
, htons(ETH_P_8021Q
),
424 vid
& VLAN_VID_MASK
);
426 skb_reset_mac_header(skb
);
427 skb
->protocol
= eth_type_trans(skb
, soft_iface
);
428 batadv_inc_counter(bat_priv
, BATADV_CNT_RX
);
429 batadv_add_counter(bat_priv
, BATADV_CNT_RX_BYTES
,
430 skb
->len
+ ETH_HLEN
);
431 soft_iface
->last_rx
= jiffies
;
436 batadv_hardif_put(primary_if
);
440 * batadv_bla_loopdetect_report - worker for reporting the loop
441 * @work: work queue item
443 * Throws an uevent, as the loopdetect check function can't do that itself
444 * since the kernel may sleep while throwing uevents.
446 static void batadv_bla_loopdetect_report(struct work_struct
*work
)
448 struct batadv_bla_backbone_gw
*backbone_gw
;
449 struct batadv_priv
*bat_priv
;
450 char vid_str
[6] = { '\0' };
452 backbone_gw
= container_of(work
, struct batadv_bla_backbone_gw
,
454 bat_priv
= backbone_gw
->bat_priv
;
456 batadv_info(bat_priv
->soft_iface
,
457 "Possible loop on VLAN %d detected which can't be handled by BLA - please check your network setup!\n",
458 BATADV_PRINT_VID(backbone_gw
->vid
));
459 snprintf(vid_str
, sizeof(vid_str
), "%d",
460 BATADV_PRINT_VID(backbone_gw
->vid
));
461 vid_str
[sizeof(vid_str
) - 1] = 0;
463 batadv_throw_uevent(bat_priv
, BATADV_UEV_BLA
, BATADV_UEV_LOOPDETECT
,
466 batadv_backbone_gw_put(backbone_gw
);
470 * batadv_bla_get_backbone_gw - finds or creates a backbone gateway
471 * @bat_priv: the bat priv with all the soft interface information
472 * @orig: the mac address of the originator
474 * @own_backbone: set if the requested backbone is local
476 * Return: the (possibly created) backbone gateway or NULL on error
478 static struct batadv_bla_backbone_gw
*
479 batadv_bla_get_backbone_gw(struct batadv_priv
*bat_priv
, u8
*orig
,
480 unsigned short vid
, bool own_backbone
)
482 struct batadv_bla_backbone_gw
*entry
;
483 struct batadv_orig_node
*orig_node
;
486 entry
= batadv_backbone_hash_find(bat_priv
, orig
, vid
);
491 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
492 "bla_get_backbone_gw(): not found (%pM, %d), creating new entry\n",
493 orig
, BATADV_PRINT_VID(vid
));
495 entry
= kzalloc(sizeof(*entry
), GFP_ATOMIC
);
500 entry
->lasttime
= jiffies
;
501 entry
->crc
= BATADV_BLA_CRC_INIT
;
502 entry
->bat_priv
= bat_priv
;
503 spin_lock_init(&entry
->crc_lock
);
504 atomic_set(&entry
->request_sent
, 0);
505 atomic_set(&entry
->wait_periods
, 0);
506 ether_addr_copy(entry
->orig
, orig
);
507 INIT_WORK(&entry
->report_work
, batadv_bla_loopdetect_report
);
509 /* one for the hash, one for returning */
510 kref_init(&entry
->refcount
);
511 kref_get(&entry
->refcount
);
513 hash_added
= batadv_hash_add(bat_priv
->bla
.backbone_hash
,
514 batadv_compare_backbone_gw
,
515 batadv_choose_backbone_gw
, entry
,
518 if (unlikely(hash_added
!= 0)) {
519 /* hash failed, free the structure */
524 /* this is a gateway now, remove any TT entry on this VLAN */
525 orig_node
= batadv_orig_hash_find(bat_priv
, orig
);
527 batadv_tt_global_del_orig(bat_priv
, orig_node
, vid
,
528 "became a backbone gateway");
529 batadv_orig_node_put(orig_node
);
533 batadv_bla_send_announce(bat_priv
, entry
);
535 /* this will be decreased in the worker thread */
536 atomic_inc(&entry
->request_sent
);
537 atomic_set(&entry
->wait_periods
, BATADV_BLA_WAIT_PERIODS
);
538 atomic_inc(&bat_priv
->bla
.num_requests
);
545 * batadv_bla_update_own_backbone_gw - updates the own backbone gw for a VLAN
546 * @bat_priv: the bat priv with all the soft interface information
547 * @primary_if: the selected primary interface
548 * @vid: VLAN identifier
550 * update or add the own backbone gw to make sure we announce
551 * where we receive other backbone gws
554 batadv_bla_update_own_backbone_gw(struct batadv_priv
*bat_priv
,
555 struct batadv_hard_iface
*primary_if
,
558 struct batadv_bla_backbone_gw
*backbone_gw
;
560 backbone_gw
= batadv_bla_get_backbone_gw(bat_priv
,
561 primary_if
->net_dev
->dev_addr
,
563 if (unlikely(!backbone_gw
))
566 backbone_gw
->lasttime
= jiffies
;
567 batadv_backbone_gw_put(backbone_gw
);
571 * batadv_bla_answer_request - answer a bla request by sending own claims
572 * @bat_priv: the bat priv with all the soft interface information
573 * @primary_if: interface where the request came on
574 * @vid: the vid where the request came on
576 * Repeat all of our own claims, and finally send an ANNOUNCE frame
577 * to allow the requester another check if the CRC is correct now.
579 static void batadv_bla_answer_request(struct batadv_priv
*bat_priv
,
580 struct batadv_hard_iface
*primary_if
,
583 struct hlist_head
*head
;
584 struct batadv_hashtable
*hash
;
585 struct batadv_bla_claim
*claim
;
586 struct batadv_bla_backbone_gw
*backbone_gw
;
589 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
590 "bla_answer_request(): received a claim request, send all of our own claims again\n");
592 backbone_gw
= batadv_backbone_hash_find(bat_priv
,
593 primary_if
->net_dev
->dev_addr
,
598 hash
= bat_priv
->bla
.claim_hash
;
599 for (i
= 0; i
< hash
->size
; i
++) {
600 head
= &hash
->table
[i
];
603 hlist_for_each_entry_rcu(claim
, head
, hash_entry
) {
604 /* only own claims are interesting */
605 if (claim
->backbone_gw
!= backbone_gw
)
608 batadv_bla_send_claim(bat_priv
, claim
->addr
, claim
->vid
,
609 BATADV_CLAIM_TYPE_CLAIM
);
614 /* finally, send an announcement frame */
615 batadv_bla_send_announce(bat_priv
, backbone_gw
);
616 batadv_backbone_gw_put(backbone_gw
);
620 * batadv_bla_send_request - send a request to repeat claims
621 * @backbone_gw: the backbone gateway from whom we are out of sync
623 * When the crc is wrong, ask the backbone gateway for a full table update.
624 * After the request, it will repeat all of his own claims and finally
625 * send an announcement claim with which we can check again.
627 static void batadv_bla_send_request(struct batadv_bla_backbone_gw
*backbone_gw
)
629 /* first, remove all old entries */
630 batadv_bla_del_backbone_claims(backbone_gw
);
632 batadv_dbg(BATADV_DBG_BLA
, backbone_gw
->bat_priv
,
633 "Sending REQUEST to %pM\n", backbone_gw
->orig
);
636 batadv_bla_send_claim(backbone_gw
->bat_priv
, backbone_gw
->orig
,
637 backbone_gw
->vid
, BATADV_CLAIM_TYPE_REQUEST
);
639 /* no local broadcasts should be sent or received, for now. */
640 if (!atomic_read(&backbone_gw
->request_sent
)) {
641 atomic_inc(&backbone_gw
->bat_priv
->bla
.num_requests
);
642 atomic_set(&backbone_gw
->request_sent
, 1);
647 * batadv_bla_send_announce - Send an announcement frame
648 * @bat_priv: the bat priv with all the soft interface information
649 * @backbone_gw: our backbone gateway which should be announced
651 static void batadv_bla_send_announce(struct batadv_priv
*bat_priv
,
652 struct batadv_bla_backbone_gw
*backbone_gw
)
657 memcpy(mac
, batadv_announce_mac
, 4);
658 spin_lock_bh(&backbone_gw
->crc_lock
);
659 crc
= htons(backbone_gw
->crc
);
660 spin_unlock_bh(&backbone_gw
->crc_lock
);
661 memcpy(&mac
[4], &crc
, 2);
663 batadv_bla_send_claim(bat_priv
, mac
, backbone_gw
->vid
,
664 BATADV_CLAIM_TYPE_ANNOUNCE
);
668 * batadv_bla_add_claim - Adds a claim in the claim hash
669 * @bat_priv: the bat priv with all the soft interface information
670 * @mac: the mac address of the claim
671 * @vid: the VLAN ID of the frame
672 * @backbone_gw: the backbone gateway which claims it
674 static void batadv_bla_add_claim(struct batadv_priv
*bat_priv
,
675 const u8
*mac
, const unsigned short vid
,
676 struct batadv_bla_backbone_gw
*backbone_gw
)
678 struct batadv_bla_claim
*claim
;
679 struct batadv_bla_claim search_claim
;
682 ether_addr_copy(search_claim
.addr
, mac
);
683 search_claim
.vid
= vid
;
684 claim
= batadv_claim_hash_find(bat_priv
, &search_claim
);
686 /* create a new claim entry if it does not exist yet. */
688 claim
= kzalloc(sizeof(*claim
), GFP_ATOMIC
);
692 ether_addr_copy(claim
->addr
, mac
);
694 claim
->lasttime
= jiffies
;
695 claim
->backbone_gw
= backbone_gw
;
697 kref_init(&claim
->refcount
);
698 kref_get(&claim
->refcount
);
699 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
700 "bla_add_claim(): adding new entry %pM, vid %d to hash ...\n",
701 mac
, BATADV_PRINT_VID(vid
));
702 hash_added
= batadv_hash_add(bat_priv
->bla
.claim_hash
,
703 batadv_compare_claim
,
704 batadv_choose_claim
, claim
,
707 if (unlikely(hash_added
!= 0)) {
708 /* only local changes happened. */
713 claim
->lasttime
= jiffies
;
714 if (claim
->backbone_gw
== backbone_gw
)
715 /* no need to register a new backbone */
718 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
719 "bla_add_claim(): changing ownership for %pM, vid %d\n",
720 mac
, BATADV_PRINT_VID(vid
));
722 spin_lock_bh(&claim
->backbone_gw
->crc_lock
);
723 claim
->backbone_gw
->crc
^= crc16(0, claim
->addr
, ETH_ALEN
);
724 spin_unlock_bh(&claim
->backbone_gw
->crc_lock
);
725 batadv_backbone_gw_put(claim
->backbone_gw
);
727 /* set (new) backbone gw */
728 kref_get(&backbone_gw
->refcount
);
729 claim
->backbone_gw
= backbone_gw
;
731 spin_lock_bh(&backbone_gw
->crc_lock
);
732 backbone_gw
->crc
^= crc16(0, claim
->addr
, ETH_ALEN
);
733 spin_unlock_bh(&backbone_gw
->crc_lock
);
734 backbone_gw
->lasttime
= jiffies
;
737 batadv_claim_put(claim
);
741 * batadv_bla_del_claim - delete a claim from the claim hash
742 * @bat_priv: the bat priv with all the soft interface information
743 * @mac: mac address of the claim to be removed
744 * @vid: VLAN id for the claim to be removed
746 static void batadv_bla_del_claim(struct batadv_priv
*bat_priv
,
747 const u8
*mac
, const unsigned short vid
)
749 struct batadv_bla_claim search_claim
, *claim
;
751 ether_addr_copy(search_claim
.addr
, mac
);
752 search_claim
.vid
= vid
;
753 claim
= batadv_claim_hash_find(bat_priv
, &search_claim
);
757 batadv_dbg(BATADV_DBG_BLA
, bat_priv
, "bla_del_claim(): %pM, vid %d\n",
758 mac
, BATADV_PRINT_VID(vid
));
760 batadv_hash_remove(bat_priv
->bla
.claim_hash
, batadv_compare_claim
,
761 batadv_choose_claim
, claim
);
762 batadv_claim_put(claim
); /* reference from the hash is gone */
764 spin_lock_bh(&claim
->backbone_gw
->crc_lock
);
765 claim
->backbone_gw
->crc
^= crc16(0, claim
->addr
, ETH_ALEN
);
766 spin_unlock_bh(&claim
->backbone_gw
->crc_lock
);
768 /* don't need the reference from hash_find() anymore */
769 batadv_claim_put(claim
);
773 * batadv_handle_announce - check for ANNOUNCE frame
774 * @bat_priv: the bat priv with all the soft interface information
775 * @an_addr: announcement mac address (ARP Sender HW address)
776 * @backbone_addr: originator address of the sender (Ethernet source MAC)
777 * @vid: the VLAN ID of the frame
779 * Return: true if handled
781 static bool batadv_handle_announce(struct batadv_priv
*bat_priv
, u8
*an_addr
,
782 u8
*backbone_addr
, unsigned short vid
)
784 struct batadv_bla_backbone_gw
*backbone_gw
;
785 u16 backbone_crc
, crc
;
787 if (memcmp(an_addr
, batadv_announce_mac
, 4) != 0)
790 backbone_gw
= batadv_bla_get_backbone_gw(bat_priv
, backbone_addr
, vid
,
793 if (unlikely(!backbone_gw
))
796 /* handle as ANNOUNCE frame */
797 backbone_gw
->lasttime
= jiffies
;
798 crc
= ntohs(*((__be16
*)(&an_addr
[4])));
800 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
801 "handle_announce(): ANNOUNCE vid %d (sent by %pM)... CRC = %#.4x\n",
802 BATADV_PRINT_VID(vid
), backbone_gw
->orig
, crc
);
804 spin_lock_bh(&backbone_gw
->crc_lock
);
805 backbone_crc
= backbone_gw
->crc
;
806 spin_unlock_bh(&backbone_gw
->crc_lock
);
808 if (backbone_crc
!= crc
) {
809 batadv_dbg(BATADV_DBG_BLA
, backbone_gw
->bat_priv
,
810 "handle_announce(): CRC FAILED for %pM/%d (my = %#.4x, sent = %#.4x)\n",
812 BATADV_PRINT_VID(backbone_gw
->vid
),
815 batadv_bla_send_request(backbone_gw
);
817 /* if we have sent a request and the crc was OK,
818 * we can allow traffic again.
820 if (atomic_read(&backbone_gw
->request_sent
)) {
821 atomic_dec(&backbone_gw
->bat_priv
->bla
.num_requests
);
822 atomic_set(&backbone_gw
->request_sent
, 0);
826 batadv_backbone_gw_put(backbone_gw
);
831 * batadv_handle_request - check for REQUEST frame
832 * @bat_priv: the bat priv with all the soft interface information
833 * @primary_if: the primary hard interface of this batman soft interface
834 * @backbone_addr: backbone address to be requested (ARP sender HW MAC)
835 * @ethhdr: ethernet header of a packet
836 * @vid: the VLAN ID of the frame
838 * Return: true if handled
840 static bool batadv_handle_request(struct batadv_priv
*bat_priv
,
841 struct batadv_hard_iface
*primary_if
,
842 u8
*backbone_addr
, struct ethhdr
*ethhdr
,
845 /* check for REQUEST frame */
846 if (!batadv_compare_eth(backbone_addr
, ethhdr
->h_dest
))
849 /* sanity check, this should not happen on a normal switch,
850 * we ignore it in this case.
852 if (!batadv_compare_eth(ethhdr
->h_dest
, primary_if
->net_dev
->dev_addr
))
855 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
856 "handle_request(): REQUEST vid %d (sent by %pM)...\n",
857 BATADV_PRINT_VID(vid
), ethhdr
->h_source
);
859 batadv_bla_answer_request(bat_priv
, primary_if
, vid
);
864 * batadv_handle_unclaim - check for UNCLAIM frame
865 * @bat_priv: the bat priv with all the soft interface information
866 * @primary_if: the primary hard interface of this batman soft interface
867 * @backbone_addr: originator address of the backbone (Ethernet source)
868 * @claim_addr: Client to be unclaimed (ARP sender HW MAC)
869 * @vid: the VLAN ID of the frame
871 * Return: true if handled
873 static bool batadv_handle_unclaim(struct batadv_priv
*bat_priv
,
874 struct batadv_hard_iface
*primary_if
,
875 u8
*backbone_addr
, u8
*claim_addr
,
878 struct batadv_bla_backbone_gw
*backbone_gw
;
880 /* unclaim in any case if it is our own */
881 if (primary_if
&& batadv_compare_eth(backbone_addr
,
882 primary_if
->net_dev
->dev_addr
))
883 batadv_bla_send_claim(bat_priv
, claim_addr
, vid
,
884 BATADV_CLAIM_TYPE_UNCLAIM
);
886 backbone_gw
= batadv_backbone_hash_find(bat_priv
, backbone_addr
, vid
);
891 /* this must be an UNCLAIM frame */
892 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
893 "handle_unclaim(): UNCLAIM %pM on vid %d (sent by %pM)...\n",
894 claim_addr
, BATADV_PRINT_VID(vid
), backbone_gw
->orig
);
896 batadv_bla_del_claim(bat_priv
, claim_addr
, vid
);
897 batadv_backbone_gw_put(backbone_gw
);
902 * batadv_handle_claim - check for CLAIM frame
903 * @bat_priv: the bat priv with all the soft interface information
904 * @primary_if: the primary hard interface of this batman soft interface
905 * @backbone_addr: originator address of the backbone (Ethernet Source)
906 * @claim_addr: client mac address to be claimed (ARP sender HW MAC)
907 * @vid: the VLAN ID of the frame
909 * Return: true if handled
911 static bool batadv_handle_claim(struct batadv_priv
*bat_priv
,
912 struct batadv_hard_iface
*primary_if
,
913 u8
*backbone_addr
, u8
*claim_addr
,
916 struct batadv_bla_backbone_gw
*backbone_gw
;
918 /* register the gateway if not yet available, and add the claim. */
920 backbone_gw
= batadv_bla_get_backbone_gw(bat_priv
, backbone_addr
, vid
,
923 if (unlikely(!backbone_gw
))
926 /* this must be a CLAIM frame */
927 batadv_bla_add_claim(bat_priv
, claim_addr
, vid
, backbone_gw
);
928 if (batadv_compare_eth(backbone_addr
, primary_if
->net_dev
->dev_addr
))
929 batadv_bla_send_claim(bat_priv
, claim_addr
, vid
,
930 BATADV_CLAIM_TYPE_CLAIM
);
932 /* TODO: we could call something like tt_local_del() here. */
934 batadv_backbone_gw_put(backbone_gw
);
939 * batadv_check_claim_group - check for claim group membership
940 * @bat_priv: the bat priv with all the soft interface information
941 * @primary_if: the primary interface of this batman interface
942 * @hw_src: the Hardware source in the ARP Header
943 * @hw_dst: the Hardware destination in the ARP Header
944 * @ethhdr: pointer to the Ethernet header of the claim frame
946 * checks if it is a claim packet and if its on the same group.
947 * This function also applies the group ID of the sender
948 * if it is in the same mesh.
951 * 2 - if it is a claim packet and on the same group
952 * 1 - if is a claim packet from another group
953 * 0 - if it is not a claim packet
955 static int batadv_check_claim_group(struct batadv_priv
*bat_priv
,
956 struct batadv_hard_iface
*primary_if
,
957 u8
*hw_src
, u8
*hw_dst
,
958 struct ethhdr
*ethhdr
)
961 struct batadv_orig_node
*orig_node
;
962 struct batadv_bla_claim_dst
*bla_dst
, *bla_dst_own
;
964 bla_dst
= (struct batadv_bla_claim_dst
*)hw_dst
;
965 bla_dst_own
= &bat_priv
->bla
.claim_dest
;
967 /* if announcement packet, use the source,
968 * otherwise assume it is in the hw_src
970 switch (bla_dst
->type
) {
971 case BATADV_CLAIM_TYPE_CLAIM
:
972 backbone_addr
= hw_src
;
974 case BATADV_CLAIM_TYPE_REQUEST
:
975 case BATADV_CLAIM_TYPE_ANNOUNCE
:
976 case BATADV_CLAIM_TYPE_UNCLAIM
:
977 backbone_addr
= ethhdr
->h_source
;
983 /* don't accept claim frames from ourselves */
984 if (batadv_compare_eth(backbone_addr
, primary_if
->net_dev
->dev_addr
))
987 /* if its already the same group, it is fine. */
988 if (bla_dst
->group
== bla_dst_own
->group
)
991 /* lets see if this originator is in our mesh */
992 orig_node
= batadv_orig_hash_find(bat_priv
, backbone_addr
);
994 /* dont accept claims from gateways which are not in
995 * the same mesh or group.
1000 /* if our mesh friends mac is bigger, use it for ourselves. */
1001 if (ntohs(bla_dst
->group
) > ntohs(bla_dst_own
->group
)) {
1002 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
1003 "taking other backbones claim group: %#.4x\n",
1004 ntohs(bla_dst
->group
));
1005 bla_dst_own
->group
= bla_dst
->group
;
1008 batadv_orig_node_put(orig_node
);
1014 * batadv_bla_process_claim - Check if this is a claim frame, and process it
1015 * @bat_priv: the bat priv with all the soft interface information
1016 * @primary_if: the primary hard interface of this batman soft interface
1017 * @skb: the frame to be checked
1019 * Return: true if it was a claim frame, otherwise return false to
1020 * tell the callee that it can use the frame on its own.
1022 static bool batadv_bla_process_claim(struct batadv_priv
*bat_priv
,
1023 struct batadv_hard_iface
*primary_if
,
1024 struct sk_buff
*skb
)
1026 struct batadv_bla_claim_dst
*bla_dst
, *bla_dst_own
;
1027 u8
*hw_src
, *hw_dst
;
1028 struct vlan_hdr
*vhdr
, vhdr_buf
;
1029 struct ethhdr
*ethhdr
;
1030 struct arphdr
*arphdr
;
1037 vid
= batadv_get_vid(skb
, 0);
1038 ethhdr
= eth_hdr(skb
);
1040 proto
= ethhdr
->h_proto
;
1042 if (vid
& BATADV_VLAN_HAS_TAG
) {
1043 /* Traverse the VLAN/Ethertypes.
1045 * At this point it is known that the first protocol is a VLAN
1046 * header, so start checking at the encapsulated protocol.
1048 * The depth of the VLAN headers is recorded to drop BLA claim
1049 * frames encapsulated into multiple VLAN headers (QinQ).
1052 vhdr
= skb_header_pointer(skb
, headlen
, VLAN_HLEN
,
1057 proto
= vhdr
->h_vlan_encapsulated_proto
;
1058 headlen
+= VLAN_HLEN
;
1060 } while (proto
== htons(ETH_P_8021Q
));
1063 if (proto
!= htons(ETH_P_ARP
))
1064 return false; /* not a claim frame */
1066 /* this must be a ARP frame. check if it is a claim. */
1068 if (unlikely(!pskb_may_pull(skb
, headlen
+ arp_hdr_len(skb
->dev
))))
1071 /* pskb_may_pull() may have modified the pointers, get ethhdr again */
1072 ethhdr
= eth_hdr(skb
);
1073 arphdr
= (struct arphdr
*)((u8
*)ethhdr
+ headlen
);
1075 /* Check whether the ARP frame carries a valid
1078 if (arphdr
->ar_hrd
!= htons(ARPHRD_ETHER
))
1080 if (arphdr
->ar_pro
!= htons(ETH_P_IP
))
1082 if (arphdr
->ar_hln
!= ETH_ALEN
)
1084 if (arphdr
->ar_pln
!= 4)
1087 hw_src
= (u8
*)arphdr
+ sizeof(struct arphdr
);
1088 hw_dst
= hw_src
+ ETH_ALEN
+ 4;
1089 bla_dst
= (struct batadv_bla_claim_dst
*)hw_dst
;
1090 bla_dst_own
= &bat_priv
->bla
.claim_dest
;
1092 /* check if it is a claim frame in general */
1093 if (memcmp(bla_dst
->magic
, bla_dst_own
->magic
,
1094 sizeof(bla_dst
->magic
)) != 0)
1097 /* check if there is a claim frame encapsulated deeper in (QinQ) and
1098 * drop that, as this is not supported by BLA but should also not be
1099 * sent via the mesh.
1104 /* Let the loopdetect frames on the mesh in any case. */
1105 if (bla_dst
->type
== BATADV_CLAIM_TYPE_LOOPDETECT
)
1108 /* check if it is a claim frame. */
1109 ret
= batadv_check_claim_group(bat_priv
, primary_if
, hw_src
, hw_dst
,
1112 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
1113 "bla_process_claim(): received a claim frame from another group. From: %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n",
1114 ethhdr
->h_source
, BATADV_PRINT_VID(vid
), hw_src
,
1120 /* become a backbone gw ourselves on this vlan if not happened yet */
1121 batadv_bla_update_own_backbone_gw(bat_priv
, primary_if
, vid
);
1123 /* check for the different types of claim frames ... */
1124 switch (bla_dst
->type
) {
1125 case BATADV_CLAIM_TYPE_CLAIM
:
1126 if (batadv_handle_claim(bat_priv
, primary_if
, hw_src
,
1127 ethhdr
->h_source
, vid
))
1130 case BATADV_CLAIM_TYPE_UNCLAIM
:
1131 if (batadv_handle_unclaim(bat_priv
, primary_if
,
1132 ethhdr
->h_source
, hw_src
, vid
))
1136 case BATADV_CLAIM_TYPE_ANNOUNCE
:
1137 if (batadv_handle_announce(bat_priv
, hw_src
, ethhdr
->h_source
,
1141 case BATADV_CLAIM_TYPE_REQUEST
:
1142 if (batadv_handle_request(bat_priv
, primary_if
, hw_src
, ethhdr
,
1148 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
1149 "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",
1150 ethhdr
->h_source
, BATADV_PRINT_VID(vid
), hw_src
, hw_dst
);
1155 * batadv_bla_purge_backbone_gw - Remove backbone gateways after a timeout or
1157 * @bat_priv: the bat priv with all the soft interface information
1158 * @now: whether the whole hash shall be wiped now
1160 * Check when we last heard from other nodes, and remove them in case of
1161 * a time out, or clean all backbone gws if now is set.
1163 static void batadv_bla_purge_backbone_gw(struct batadv_priv
*bat_priv
, int now
)
1165 struct batadv_bla_backbone_gw
*backbone_gw
;
1166 struct hlist_node
*node_tmp
;
1167 struct hlist_head
*head
;
1168 struct batadv_hashtable
*hash
;
1169 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1172 hash
= bat_priv
->bla
.backbone_hash
;
1176 for (i
= 0; i
< hash
->size
; i
++) {
1177 head
= &hash
->table
[i
];
1178 list_lock
= &hash
->list_locks
[i
];
1180 spin_lock_bh(list_lock
);
1181 hlist_for_each_entry_safe(backbone_gw
, node_tmp
,
1185 if (!batadv_has_timed_out(backbone_gw
->lasttime
,
1186 BATADV_BLA_BACKBONE_TIMEOUT
))
1189 batadv_dbg(BATADV_DBG_BLA
, backbone_gw
->bat_priv
,
1190 "bla_purge_backbone_gw(): backbone gw %pM timed out\n",
1194 /* don't wait for the pending request anymore */
1195 if (atomic_read(&backbone_gw
->request_sent
))
1196 atomic_dec(&bat_priv
->bla
.num_requests
);
1198 batadv_bla_del_backbone_claims(backbone_gw
);
1200 hlist_del_rcu(&backbone_gw
->hash_entry
);
1201 batadv_backbone_gw_put(backbone_gw
);
1203 spin_unlock_bh(list_lock
);
1208 * batadv_bla_purge_claims - Remove claims after a timeout or immediately
1209 * @bat_priv: the bat priv with all the soft interface information
1210 * @primary_if: the selected primary interface, may be NULL if now is set
1211 * @now: whether the whole hash shall be wiped now
1213 * Check when we heard last time from our own claims, and remove them in case of
1214 * a time out, or clean all claims if now is set
1216 static void batadv_bla_purge_claims(struct batadv_priv
*bat_priv
,
1217 struct batadv_hard_iface
*primary_if
,
1220 struct batadv_bla_claim
*claim
;
1221 struct hlist_head
*head
;
1222 struct batadv_hashtable
*hash
;
1225 hash
= bat_priv
->bla
.claim_hash
;
1229 for (i
= 0; i
< hash
->size
; i
++) {
1230 head
= &hash
->table
[i
];
1233 hlist_for_each_entry_rcu(claim
, head
, hash_entry
) {
1236 if (!batadv_compare_eth(claim
->backbone_gw
->orig
,
1237 primary_if
->net_dev
->dev_addr
))
1239 if (!batadv_has_timed_out(claim
->lasttime
,
1240 BATADV_BLA_CLAIM_TIMEOUT
))
1243 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
1244 "bla_purge_claims(): %pM, vid %d, time out\n",
1245 claim
->addr
, claim
->vid
);
1248 batadv_handle_unclaim(bat_priv
, primary_if
,
1249 claim
->backbone_gw
->orig
,
1250 claim
->addr
, claim
->vid
);
1257 * batadv_bla_update_orig_address - Update the backbone gateways when the own
1258 * originator address changes
1259 * @bat_priv: the bat priv with all the soft interface information
1260 * @primary_if: the new selected primary_if
1261 * @oldif: the old primary interface, may be NULL
1263 void batadv_bla_update_orig_address(struct batadv_priv
*bat_priv
,
1264 struct batadv_hard_iface
*primary_if
,
1265 struct batadv_hard_iface
*oldif
)
1267 struct batadv_bla_backbone_gw
*backbone_gw
;
1268 struct hlist_head
*head
;
1269 struct batadv_hashtable
*hash
;
1273 /* reset bridge loop avoidance group id */
1274 group
= htons(crc16(0, primary_if
->net_dev
->dev_addr
, ETH_ALEN
));
1275 bat_priv
->bla
.claim_dest
.group
= group
;
1277 /* purge everything when bridge loop avoidance is turned off */
1278 if (!atomic_read(&bat_priv
->bridge_loop_avoidance
))
1282 batadv_bla_purge_claims(bat_priv
, NULL
, 1);
1283 batadv_bla_purge_backbone_gw(bat_priv
, 1);
1287 hash
= bat_priv
->bla
.backbone_hash
;
1291 for (i
= 0; i
< hash
->size
; i
++) {
1292 head
= &hash
->table
[i
];
1295 hlist_for_each_entry_rcu(backbone_gw
, head
, hash_entry
) {
1296 /* own orig still holds the old value. */
1297 if (!batadv_compare_eth(backbone_gw
->orig
,
1298 oldif
->net_dev
->dev_addr
))
1301 ether_addr_copy(backbone_gw
->orig
,
1302 primary_if
->net_dev
->dev_addr
);
1303 /* send an announce frame so others will ask for our
1304 * claims and update their tables.
1306 batadv_bla_send_announce(bat_priv
, backbone_gw
);
1313 * batadv_bla_send_loopdetect - send a loopdetect frame
1314 * @bat_priv: the bat priv with all the soft interface information
1315 * @backbone_gw: the backbone gateway for which a loop should be detected
1317 * To detect loops that the bridge loop avoidance can't handle, send a loop
1318 * detection packet on the backbone. Unlike other BLA frames, this frame will
1319 * be allowed on the mesh by other nodes. If it is received on the mesh, this
1320 * indicates that there is a loop.
1323 batadv_bla_send_loopdetect(struct batadv_priv
*bat_priv
,
1324 struct batadv_bla_backbone_gw
*backbone_gw
)
1326 batadv_dbg(BATADV_DBG_BLA
, bat_priv
, "Send loopdetect frame for vid %d\n",
1328 batadv_bla_send_claim(bat_priv
, bat_priv
->bla
.loopdetect_addr
,
1329 backbone_gw
->vid
, BATADV_CLAIM_TYPE_LOOPDETECT
);
1333 * batadv_bla_status_update - purge bla interfaces if necessary
1334 * @net_dev: the soft interface net device
1336 void batadv_bla_status_update(struct net_device
*net_dev
)
1338 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
1339 struct batadv_hard_iface
*primary_if
;
1341 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1345 /* this function already purges everything when bla is disabled,
1346 * so just call that one.
1348 batadv_bla_update_orig_address(bat_priv
, primary_if
, primary_if
);
1349 batadv_hardif_put(primary_if
);
1353 * batadv_bla_periodic_work - performs periodic bla work
1354 * @work: kernel work struct
1356 * periodic work to do:
1357 * * purge structures when they are too old
1358 * * send announcements
1360 static void batadv_bla_periodic_work(struct work_struct
*work
)
1362 struct delayed_work
*delayed_work
;
1363 struct batadv_priv
*bat_priv
;
1364 struct batadv_priv_bla
*priv_bla
;
1365 struct hlist_head
*head
;
1366 struct batadv_bla_backbone_gw
*backbone_gw
;
1367 struct batadv_hashtable
*hash
;
1368 struct batadv_hard_iface
*primary_if
;
1369 bool send_loopdetect
= false;
1372 delayed_work
= to_delayed_work(work
);
1373 priv_bla
= container_of(delayed_work
, struct batadv_priv_bla
, work
);
1374 bat_priv
= container_of(priv_bla
, struct batadv_priv
, bla
);
1375 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1379 batadv_bla_purge_claims(bat_priv
, primary_if
, 0);
1380 batadv_bla_purge_backbone_gw(bat_priv
, 0);
1382 if (!atomic_read(&bat_priv
->bridge_loop_avoidance
))
1385 if (atomic_dec_and_test(&bat_priv
->bla
.loopdetect_next
)) {
1386 /* set a new random mac address for the next bridge loop
1387 * detection frames. Set the locally administered bit to avoid
1388 * collisions with users mac addresses.
1390 random_ether_addr(bat_priv
->bla
.loopdetect_addr
);
1391 bat_priv
->bla
.loopdetect_addr
[0] = 0xba;
1392 bat_priv
->bla
.loopdetect_addr
[1] = 0xbe;
1393 bat_priv
->bla
.loopdetect_lasttime
= jiffies
;
1394 atomic_set(&bat_priv
->bla
.loopdetect_next
,
1395 BATADV_BLA_LOOPDETECT_PERIODS
);
1397 /* mark for sending loop detect on all VLANs */
1398 send_loopdetect
= true;
1401 hash
= bat_priv
->bla
.backbone_hash
;
1405 for (i
= 0; i
< hash
->size
; i
++) {
1406 head
= &hash
->table
[i
];
1409 hlist_for_each_entry_rcu(backbone_gw
, head
, hash_entry
) {
1410 if (!batadv_compare_eth(backbone_gw
->orig
,
1411 primary_if
->net_dev
->dev_addr
))
1414 backbone_gw
->lasttime
= jiffies
;
1416 batadv_bla_send_announce(bat_priv
, backbone_gw
);
1417 if (send_loopdetect
)
1418 batadv_bla_send_loopdetect(bat_priv
,
1421 /* request_sent is only set after creation to avoid
1422 * problems when we are not yet known as backbone gw
1425 * We can reset this now after we waited some periods
1426 * to give bridge forward delays and bla group forming
1430 if (atomic_read(&backbone_gw
->request_sent
) == 0)
1433 if (!atomic_dec_and_test(&backbone_gw
->wait_periods
))
1436 atomic_dec(&backbone_gw
->bat_priv
->bla
.num_requests
);
1437 atomic_set(&backbone_gw
->request_sent
, 0);
1443 batadv_hardif_put(primary_if
);
1445 queue_delayed_work(batadv_event_workqueue
, &bat_priv
->bla
.work
,
1446 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH
));
1449 /* The hash for claim and backbone hash receive the same key because they
1450 * are getting initialized by hash_new with the same key. Reinitializing
1451 * them with to different keys to allow nested locking without generating
1454 static struct lock_class_key batadv_claim_hash_lock_class_key
;
1455 static struct lock_class_key batadv_backbone_hash_lock_class_key
;
1458 * batadv_bla_init - initialize all bla structures
1459 * @bat_priv: the bat priv with all the soft interface information
1461 * Return: 0 on success, < 0 on error.
1463 int batadv_bla_init(struct batadv_priv
*bat_priv
)
1466 u8 claim_dest
[ETH_ALEN
] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00};
1467 struct batadv_hard_iface
*primary_if
;
1469 unsigned long entrytime
;
1471 spin_lock_init(&bat_priv
->bla
.bcast_duplist_lock
);
1473 batadv_dbg(BATADV_DBG_BLA
, bat_priv
, "bla hash registering\n");
1475 /* setting claim destination address */
1476 memcpy(&bat_priv
->bla
.claim_dest
.magic
, claim_dest
, 3);
1477 bat_priv
->bla
.claim_dest
.type
= 0;
1478 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1480 crc
= crc16(0, primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
1481 bat_priv
->bla
.claim_dest
.group
= htons(crc
);
1482 batadv_hardif_put(primary_if
);
1484 bat_priv
->bla
.claim_dest
.group
= 0; /* will be set later */
1487 /* initialize the duplicate list */
1488 entrytime
= jiffies
- msecs_to_jiffies(BATADV_DUPLIST_TIMEOUT
);
1489 for (i
= 0; i
< BATADV_DUPLIST_SIZE
; i
++)
1490 bat_priv
->bla
.bcast_duplist
[i
].entrytime
= entrytime
;
1491 bat_priv
->bla
.bcast_duplist_curr
= 0;
1493 atomic_set(&bat_priv
->bla
.loopdetect_next
,
1494 BATADV_BLA_LOOPDETECT_PERIODS
);
1496 if (bat_priv
->bla
.claim_hash
)
1499 bat_priv
->bla
.claim_hash
= batadv_hash_new(128);
1500 bat_priv
->bla
.backbone_hash
= batadv_hash_new(32);
1502 if (!bat_priv
->bla
.claim_hash
|| !bat_priv
->bla
.backbone_hash
)
1505 batadv_hash_set_lock_class(bat_priv
->bla
.claim_hash
,
1506 &batadv_claim_hash_lock_class_key
);
1507 batadv_hash_set_lock_class(bat_priv
->bla
.backbone_hash
,
1508 &batadv_backbone_hash_lock_class_key
);
1510 batadv_dbg(BATADV_DBG_BLA
, bat_priv
, "bla hashes initialized\n");
1512 INIT_DELAYED_WORK(&bat_priv
->bla
.work
, batadv_bla_periodic_work
);
1514 queue_delayed_work(batadv_event_workqueue
, &bat_priv
->bla
.work
,
1515 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH
));
1520 * batadv_bla_check_bcast_duplist - Check if a frame is in the broadcast dup.
1521 * @bat_priv: the bat priv with all the soft interface information
1522 * @skb: contains the bcast_packet to be checked
1524 * check if it is on our broadcast list. Another gateway might
1525 * have sent the same packet because it is connected to the same backbone,
1526 * so we have to remove this duplicate.
1528 * This is performed by checking the CRC, which will tell us
1529 * with a good chance that it is the same packet. If it is furthermore
1530 * sent by another host, drop it. We allow equal packets from
1531 * the same host however as this might be intended.
1533 * Return: true if a packet is in the duplicate list, false otherwise.
1535 bool batadv_bla_check_bcast_duplist(struct batadv_priv
*bat_priv
,
1536 struct sk_buff
*skb
)
1540 struct batadv_bcast_packet
*bcast_packet
;
1541 struct batadv_bcast_duplist_entry
*entry
;
1544 bcast_packet
= (struct batadv_bcast_packet
*)skb
->data
;
1546 /* calculate the crc ... */
1547 crc
= batadv_skb_crc32(skb
, (u8
*)(bcast_packet
+ 1));
1549 spin_lock_bh(&bat_priv
->bla
.bcast_duplist_lock
);
1551 for (i
= 0; i
< BATADV_DUPLIST_SIZE
; i
++) {
1552 curr
= (bat_priv
->bla
.bcast_duplist_curr
+ i
);
1553 curr
%= BATADV_DUPLIST_SIZE
;
1554 entry
= &bat_priv
->bla
.bcast_duplist
[curr
];
1556 /* we can stop searching if the entry is too old ;
1557 * later entries will be even older
1559 if (batadv_has_timed_out(entry
->entrytime
,
1560 BATADV_DUPLIST_TIMEOUT
))
1563 if (entry
->crc
!= crc
)
1566 if (batadv_compare_eth(entry
->orig
, bcast_packet
->orig
))
1569 /* this entry seems to match: same crc, not too old,
1570 * and from another gw. therefore return true to forbid it.
1575 /* not found, add a new entry (overwrite the oldest entry)
1576 * and allow it, its the first occurrence.
1578 curr
= (bat_priv
->bla
.bcast_duplist_curr
+ BATADV_DUPLIST_SIZE
- 1);
1579 curr
%= BATADV_DUPLIST_SIZE
;
1580 entry
= &bat_priv
->bla
.bcast_duplist
[curr
];
1582 entry
->entrytime
= jiffies
;
1583 ether_addr_copy(entry
->orig
, bcast_packet
->orig
);
1584 bat_priv
->bla
.bcast_duplist_curr
= curr
;
1587 spin_unlock_bh(&bat_priv
->bla
.bcast_duplist_lock
);
1593 * batadv_bla_is_backbone_gw_orig - Check if the originator is a gateway for
1594 * the VLAN identified by vid.
1595 * @bat_priv: the bat priv with all the soft interface information
1596 * @orig: originator mac address
1597 * @vid: VLAN identifier
1599 * Return: true if orig is a backbone for this vid, false otherwise.
1601 bool batadv_bla_is_backbone_gw_orig(struct batadv_priv
*bat_priv
, u8
*orig
,
1604 struct batadv_hashtable
*hash
= bat_priv
->bla
.backbone_hash
;
1605 struct hlist_head
*head
;
1606 struct batadv_bla_backbone_gw
*backbone_gw
;
1609 if (!atomic_read(&bat_priv
->bridge_loop_avoidance
))
1615 for (i
= 0; i
< hash
->size
; i
++) {
1616 head
= &hash
->table
[i
];
1619 hlist_for_each_entry_rcu(backbone_gw
, head
, hash_entry
) {
1620 if (batadv_compare_eth(backbone_gw
->orig
, orig
) &&
1621 backbone_gw
->vid
== vid
) {
1633 * batadv_bla_is_backbone_gw - check if originator is a backbone gw for a VLAN.
1634 * @skb: the frame to be checked
1635 * @orig_node: the orig_node of the frame
1636 * @hdr_size: maximum length of the frame
1638 * Return: true if the orig_node is also a gateway on the soft interface,
1639 * otherwise it returns false.
1641 bool batadv_bla_is_backbone_gw(struct sk_buff
*skb
,
1642 struct batadv_orig_node
*orig_node
, int hdr_size
)
1644 struct batadv_bla_backbone_gw
*backbone_gw
;
1647 if (!atomic_read(&orig_node
->bat_priv
->bridge_loop_avoidance
))
1650 /* first, find out the vid. */
1651 if (!pskb_may_pull(skb
, hdr_size
+ ETH_HLEN
))
1654 vid
= batadv_get_vid(skb
, hdr_size
);
1656 /* see if this originator is a backbone gw for this VLAN */
1657 backbone_gw
= batadv_backbone_hash_find(orig_node
->bat_priv
,
1658 orig_node
->orig
, vid
);
1662 batadv_backbone_gw_put(backbone_gw
);
1667 * batadv_bla_free - free all bla structures
1668 * @bat_priv: the bat priv with all the soft interface information
1670 * for softinterface free or module unload
1672 void batadv_bla_free(struct batadv_priv
*bat_priv
)
1674 struct batadv_hard_iface
*primary_if
;
1676 cancel_delayed_work_sync(&bat_priv
->bla
.work
);
1677 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1679 if (bat_priv
->bla
.claim_hash
) {
1680 batadv_bla_purge_claims(bat_priv
, primary_if
, 1);
1681 batadv_hash_destroy(bat_priv
->bla
.claim_hash
);
1682 bat_priv
->bla
.claim_hash
= NULL
;
1684 if (bat_priv
->bla
.backbone_hash
) {
1685 batadv_bla_purge_backbone_gw(bat_priv
, 1);
1686 batadv_hash_destroy(bat_priv
->bla
.backbone_hash
);
1687 bat_priv
->bla
.backbone_hash
= NULL
;
1690 batadv_hardif_put(primary_if
);
1694 * batadv_bla_loopdetect_check - check and handle a detected loop
1695 * @bat_priv: the bat priv with all the soft interface information
1696 * @skb: the packet to check
1697 * @primary_if: interface where the request came on
1698 * @vid: the VLAN ID of the frame
1700 * Checks if this packet is a loop detect frame which has been sent by us,
1701 * throw an uevent and log the event if that is the case.
1703 * Return: true if it is a loop detect frame which is to be dropped, false
1707 batadv_bla_loopdetect_check(struct batadv_priv
*bat_priv
, struct sk_buff
*skb
,
1708 struct batadv_hard_iface
*primary_if
,
1711 struct batadv_bla_backbone_gw
*backbone_gw
;
1712 struct ethhdr
*ethhdr
;
1714 ethhdr
= eth_hdr(skb
);
1716 /* Only check for the MAC address and skip more checks here for
1717 * performance reasons - this function is on the hotpath, after all.
1719 if (!batadv_compare_eth(ethhdr
->h_source
,
1720 bat_priv
->bla
.loopdetect_addr
))
1723 /* If the packet came too late, don't forward it on the mesh
1724 * but don't consider that as loop. It might be a coincidence.
1726 if (batadv_has_timed_out(bat_priv
->bla
.loopdetect_lasttime
,
1727 BATADV_BLA_LOOPDETECT_TIMEOUT
))
1730 backbone_gw
= batadv_bla_get_backbone_gw(bat_priv
,
1731 primary_if
->net_dev
->dev_addr
,
1733 if (unlikely(!backbone_gw
))
1736 queue_work(batadv_event_workqueue
, &backbone_gw
->report_work
);
1737 /* backbone_gw is unreferenced in the report work function function */
1743 * batadv_bla_rx - check packets coming from the mesh.
1744 * @bat_priv: the bat priv with all the soft interface information
1745 * @skb: the frame to be checked
1746 * @vid: the VLAN ID of the frame
1747 * @is_bcast: the packet came in a broadcast packet type.
1749 * batadv_bla_rx avoidance checks if:
1750 * * we have to race for a claim
1751 * * if the frame is allowed on the LAN
1753 * in these cases, the skb is further handled by this function
1755 * Return: true if handled, otherwise it returns false and the caller shall
1756 * further process the skb.
1758 bool batadv_bla_rx(struct batadv_priv
*bat_priv
, struct sk_buff
*skb
,
1759 unsigned short vid
, bool is_bcast
)
1761 struct ethhdr
*ethhdr
;
1762 struct batadv_bla_claim search_claim
, *claim
= NULL
;
1763 struct batadv_hard_iface
*primary_if
;
1766 ethhdr
= eth_hdr(skb
);
1768 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1772 if (!atomic_read(&bat_priv
->bridge_loop_avoidance
))
1775 if (batadv_bla_loopdetect_check(bat_priv
, skb
, primary_if
, vid
))
1778 if (unlikely(atomic_read(&bat_priv
->bla
.num_requests
)))
1779 /* don't allow broadcasts while requests are in flight */
1780 if (is_multicast_ether_addr(ethhdr
->h_dest
) && is_bcast
)
1783 ether_addr_copy(search_claim
.addr
, ethhdr
->h_source
);
1784 search_claim
.vid
= vid
;
1785 claim
= batadv_claim_hash_find(bat_priv
, &search_claim
);
1788 /* possible optimization: race for a claim */
1789 /* No claim exists yet, claim it for us!
1791 batadv_handle_claim(bat_priv
, primary_if
,
1792 primary_if
->net_dev
->dev_addr
,
1793 ethhdr
->h_source
, vid
);
1797 /* if it is our own claim ... */
1798 if (batadv_compare_eth(claim
->backbone_gw
->orig
,
1799 primary_if
->net_dev
->dev_addr
)) {
1800 /* ... allow it in any case */
1801 claim
->lasttime
= jiffies
;
1805 /* if it is a broadcast ... */
1806 if (is_multicast_ether_addr(ethhdr
->h_dest
) && is_bcast
) {
1807 /* ... drop it. the responsible gateway is in charge.
1809 * We need to check is_bcast because with the gateway
1810 * feature, broadcasts (like DHCP requests) may be sent
1811 * using a unicast packet type.
1815 /* seems the client considers us as its best gateway.
1816 * send a claim and update the claim table
1819 batadv_handle_claim(bat_priv
, primary_if
,
1820 primary_if
->net_dev
->dev_addr
,
1821 ethhdr
->h_source
, vid
);
1825 batadv_bla_update_own_backbone_gw(bat_priv
, primary_if
, vid
);
1835 batadv_hardif_put(primary_if
);
1837 batadv_claim_put(claim
);
1842 * batadv_bla_tx - check packets going into the mesh
1843 * @bat_priv: the bat priv with all the soft interface information
1844 * @skb: the frame to be checked
1845 * @vid: the VLAN ID of the frame
1847 * batadv_bla_tx checks if:
1848 * * a claim was received which has to be processed
1849 * * the frame is allowed on the mesh
1851 * in these cases, the skb is further handled by this function.
1853 * This call might reallocate skb data.
1855 * Return: true if handled, otherwise it returns false and the caller shall
1856 * further process the skb.
1858 bool batadv_bla_tx(struct batadv_priv
*bat_priv
, struct sk_buff
*skb
,
1861 struct ethhdr
*ethhdr
;
1862 struct batadv_bla_claim search_claim
, *claim
= NULL
;
1863 struct batadv_hard_iface
*primary_if
;
1866 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1870 if (!atomic_read(&bat_priv
->bridge_loop_avoidance
))
1873 if (batadv_bla_process_claim(bat_priv
, primary_if
, skb
))
1876 ethhdr
= eth_hdr(skb
);
1878 if (unlikely(atomic_read(&bat_priv
->bla
.num_requests
)))
1879 /* don't allow broadcasts while requests are in flight */
1880 if (is_multicast_ether_addr(ethhdr
->h_dest
))
1883 ether_addr_copy(search_claim
.addr
, ethhdr
->h_source
);
1884 search_claim
.vid
= vid
;
1886 claim
= batadv_claim_hash_find(bat_priv
, &search_claim
);
1888 /* if no claim exists, allow it. */
1892 /* check if we are responsible. */
1893 if (batadv_compare_eth(claim
->backbone_gw
->orig
,
1894 primary_if
->net_dev
->dev_addr
)) {
1895 /* if yes, the client has roamed and we have
1898 batadv_handle_unclaim(bat_priv
, primary_if
,
1899 primary_if
->net_dev
->dev_addr
,
1900 ethhdr
->h_source
, vid
);
1904 /* check if it is a multicast/broadcast frame */
1905 if (is_multicast_ether_addr(ethhdr
->h_dest
)) {
1906 /* drop it. the responsible gateway has forwarded it into
1907 * the backbone network.
1911 /* we must allow it. at least if we are
1912 * responsible for the DESTINATION.
1917 batadv_bla_update_own_backbone_gw(bat_priv
, primary_if
, vid
);
1924 batadv_hardif_put(primary_if
);
1926 batadv_claim_put(claim
);
1931 * batadv_bla_claim_table_seq_print_text - print the claim table in a seq file
1932 * @seq: seq file to print on
1937 int batadv_bla_claim_table_seq_print_text(struct seq_file
*seq
, void *offset
)
1939 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
1940 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
1941 struct batadv_hashtable
*hash
= bat_priv
->bla
.claim_hash
;
1942 struct batadv_bla_claim
*claim
;
1943 struct batadv_hard_iface
*primary_if
;
1944 struct hlist_head
*head
;
1950 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
1954 primary_addr
= primary_if
->net_dev
->dev_addr
;
1956 "Claims announced for the mesh %s (orig %pM, group id %#.4x)\n",
1957 net_dev
->name
, primary_addr
,
1958 ntohs(bat_priv
->bla
.claim_dest
.group
));
1960 " Client VID Originator [o] (CRC )\n");
1961 for (i
= 0; i
< hash
->size
; i
++) {
1962 head
= &hash
->table
[i
];
1965 hlist_for_each_entry_rcu(claim
, head
, hash_entry
) {
1966 is_own
= batadv_compare_eth(claim
->backbone_gw
->orig
,
1969 spin_lock_bh(&claim
->backbone_gw
->crc_lock
);
1970 backbone_crc
= claim
->backbone_gw
->crc
;
1971 spin_unlock_bh(&claim
->backbone_gw
->crc_lock
);
1972 seq_printf(seq
, " * %pM on %5d by %pM [%c] (%#.4x)\n",
1973 claim
->addr
, BATADV_PRINT_VID(claim
->vid
),
1974 claim
->backbone_gw
->orig
,
1975 (is_own
? 'x' : ' '),
1982 batadv_hardif_put(primary_if
);
1987 * batadv_bla_backbone_table_seq_print_text - print the backbone table in a seq
1989 * @seq: seq file to print on
1994 int batadv_bla_backbone_table_seq_print_text(struct seq_file
*seq
, void *offset
)
1996 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
1997 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
1998 struct batadv_hashtable
*hash
= bat_priv
->bla
.backbone_hash
;
1999 struct batadv_bla_backbone_gw
*backbone_gw
;
2000 struct batadv_hard_iface
*primary_if
;
2001 struct hlist_head
*head
;
2008 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
2012 primary_addr
= primary_if
->net_dev
->dev_addr
;
2014 "Backbones announced for the mesh %s (orig %pM, group id %#.4x)\n",
2015 net_dev
->name
, primary_addr
,
2016 ntohs(bat_priv
->bla
.claim_dest
.group
));
2017 seq_puts(seq
, " Originator VID last seen (CRC )\n");
2018 for (i
= 0; i
< hash
->size
; i
++) {
2019 head
= &hash
->table
[i
];
2022 hlist_for_each_entry_rcu(backbone_gw
, head
, hash_entry
) {
2023 msecs
= jiffies_to_msecs(jiffies
-
2024 backbone_gw
->lasttime
);
2025 secs
= msecs
/ 1000;
2026 msecs
= msecs
% 1000;
2028 is_own
= batadv_compare_eth(backbone_gw
->orig
,
2033 spin_lock_bh(&backbone_gw
->crc_lock
);
2034 backbone_crc
= backbone_gw
->crc
;
2035 spin_unlock_bh(&backbone_gw
->crc_lock
);
2037 seq_printf(seq
, " * %pM on %5d %4i.%03is (%#.4x)\n",
2039 BATADV_PRINT_VID(backbone_gw
->vid
), secs
,
2040 msecs
, backbone_crc
);
2046 batadv_hardif_put(primary_if
);