1 /* Copyright (C) 2011-2015 B.A.T.M.A.N. contributors:
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 #include "bridge_loop_avoidance.h"
21 #include <linux/atomic.h>
22 #include <linux/byteorder/generic.h>
23 #include <linux/compiler.h>
24 #include <linux/crc16.h>
25 #include <linux/errno.h>
26 #include <linux/etherdevice.h>
28 #include <linux/if_arp.h>
29 #include <linux/if_ether.h>
30 #include <linux/if_vlan.h>
31 #include <linux/jhash.h>
32 #include <linux/jiffies.h>
33 #include <linux/kernel.h>
34 #include <linux/list.h>
35 #include <linux/lockdep.h>
36 #include <linux/netdevice.h>
37 #include <linux/rculist.h>
38 #include <linux/rcupdate.h>
39 #include <linux/seq_file.h>
40 #include <linux/skbuff.h>
41 #include <linux/slab.h>
42 #include <linux/spinlock.h>
43 #include <linux/stddef.h>
44 #include <linux/string.h>
45 #include <linux/workqueue.h>
48 #include "hard-interface.h"
50 #include "originator.h"
52 #include "translation-table.h"
54 static const u8 batadv_announce_mac
[4] = {0x43, 0x05, 0x43, 0x05};
56 static void batadv_bla_periodic_work(struct work_struct
*work
);
58 batadv_bla_send_announce(struct batadv_priv
*bat_priv
,
59 struct batadv_bla_backbone_gw
*backbone_gw
);
61 /* return the index of the claim */
62 static inline u32
batadv_choose_claim(const void *data
, u32 size
)
64 struct batadv_bla_claim
*claim
= (struct batadv_bla_claim
*)data
;
67 hash
= jhash(&claim
->addr
, sizeof(claim
->addr
), hash
);
68 hash
= jhash(&claim
->vid
, sizeof(claim
->vid
), hash
);
73 /* return the index of the backbone gateway */
74 static inline u32
batadv_choose_backbone_gw(const void *data
, u32 size
)
76 const struct batadv_bla_claim
*claim
= (struct batadv_bla_claim
*)data
;
79 hash
= jhash(&claim
->addr
, sizeof(claim
->addr
), hash
);
80 hash
= jhash(&claim
->vid
, sizeof(claim
->vid
), hash
);
85 /* compares address and vid of two backbone gws */
86 static int batadv_compare_backbone_gw(const struct hlist_node
*node
,
89 const void *data1
= container_of(node
, struct batadv_bla_backbone_gw
,
91 const struct batadv_bla_backbone_gw
*gw1
= data1
;
92 const struct batadv_bla_backbone_gw
*gw2
= data2
;
94 if (!batadv_compare_eth(gw1
->orig
, gw2
->orig
))
97 if (gw1
->vid
!= gw2
->vid
)
103 /* compares address and vid of two claims */
104 static int batadv_compare_claim(const struct hlist_node
*node
,
107 const void *data1
= container_of(node
, struct batadv_bla_claim
,
109 const struct batadv_bla_claim
*cl1
= data1
;
110 const struct batadv_bla_claim
*cl2
= data2
;
112 if (!batadv_compare_eth(cl1
->addr
, cl2
->addr
))
115 if (cl1
->vid
!= cl2
->vid
)
121 /* free a backbone gw */
123 batadv_backbone_gw_free_ref(struct batadv_bla_backbone_gw
*backbone_gw
)
125 if (atomic_dec_and_test(&backbone_gw
->refcount
))
126 kfree_rcu(backbone_gw
, rcu
);
129 /* finally deinitialize the claim */
130 static void batadv_claim_release(struct batadv_bla_claim
*claim
)
132 batadv_backbone_gw_free_ref(claim
->backbone_gw
);
133 kfree_rcu(claim
, rcu
);
136 /* free a claim, call claim_free_rcu if its the last reference */
137 static void batadv_claim_free_ref(struct batadv_bla_claim
*claim
)
139 if (atomic_dec_and_test(&claim
->refcount
))
140 batadv_claim_release(claim
);
144 * batadv_claim_hash_find
145 * @bat_priv: the bat priv with all the soft interface information
146 * @data: search data (may be local/static data)
148 * looks for a claim in the hash, and returns it if found
151 static struct batadv_bla_claim
152 *batadv_claim_hash_find(struct batadv_priv
*bat_priv
,
153 struct batadv_bla_claim
*data
)
155 struct batadv_hashtable
*hash
= bat_priv
->bla
.claim_hash
;
156 struct hlist_head
*head
;
157 struct batadv_bla_claim
*claim
;
158 struct batadv_bla_claim
*claim_tmp
= NULL
;
164 index
= batadv_choose_claim(data
, hash
->size
);
165 head
= &hash
->table
[index
];
168 hlist_for_each_entry_rcu(claim
, head
, hash_entry
) {
169 if (!batadv_compare_claim(&claim
->hash_entry
, data
))
172 if (!atomic_inc_not_zero(&claim
->refcount
))
184 * batadv_backbone_hash_find - looks for a claim in the hash
185 * @bat_priv: the bat priv with all the soft interface information
186 * @addr: the address of the originator
189 * Returns claim if found or NULL otherwise.
191 static struct batadv_bla_backbone_gw
*
192 batadv_backbone_hash_find(struct batadv_priv
*bat_priv
, u8
*addr
,
195 struct batadv_hashtable
*hash
= bat_priv
->bla
.backbone_hash
;
196 struct hlist_head
*head
;
197 struct batadv_bla_backbone_gw search_entry
, *backbone_gw
;
198 struct batadv_bla_backbone_gw
*backbone_gw_tmp
= NULL
;
204 ether_addr_copy(search_entry
.orig
, addr
);
205 search_entry
.vid
= vid
;
207 index
= batadv_choose_backbone_gw(&search_entry
, hash
->size
);
208 head
= &hash
->table
[index
];
211 hlist_for_each_entry_rcu(backbone_gw
, head
, hash_entry
) {
212 if (!batadv_compare_backbone_gw(&backbone_gw
->hash_entry
,
216 if (!atomic_inc_not_zero(&backbone_gw
->refcount
))
219 backbone_gw_tmp
= backbone_gw
;
224 return backbone_gw_tmp
;
227 /* delete all claims for a backbone */
229 batadv_bla_del_backbone_claims(struct batadv_bla_backbone_gw
*backbone_gw
)
231 struct batadv_hashtable
*hash
;
232 struct hlist_node
*node_tmp
;
233 struct hlist_head
*head
;
234 struct batadv_bla_claim
*claim
;
236 spinlock_t
*list_lock
; /* protects write access to the hash lists */
238 hash
= backbone_gw
->bat_priv
->bla
.claim_hash
;
242 for (i
= 0; i
< hash
->size
; i
++) {
243 head
= &hash
->table
[i
];
244 list_lock
= &hash
->list_locks
[i
];
246 spin_lock_bh(list_lock
);
247 hlist_for_each_entry_safe(claim
, node_tmp
,
249 if (claim
->backbone_gw
!= backbone_gw
)
252 batadv_claim_free_ref(claim
);
253 hlist_del_rcu(&claim
->hash_entry
);
255 spin_unlock_bh(list_lock
);
258 /* all claims gone, initialize CRC */
259 backbone_gw
->crc
= BATADV_BLA_CRC_INIT
;
263 * batadv_bla_send_claim - sends a claim frame according to the provided info
264 * @bat_priv: the bat priv with all the soft interface information
265 * @mac: the mac address to be announced within the claim
267 * @claimtype: the type of the claim (CLAIM, UNCLAIM, ANNOUNCE, ...)
269 static void batadv_bla_send_claim(struct batadv_priv
*bat_priv
, u8
*mac
,
270 unsigned short vid
, int claimtype
)
273 struct ethhdr
*ethhdr
;
274 struct batadv_hard_iface
*primary_if
;
275 struct net_device
*soft_iface
;
277 struct batadv_bla_claim_dst local_claim_dest
;
280 primary_if
= batadv_primary_if_get_selected(bat_priv
);
284 memcpy(&local_claim_dest
, &bat_priv
->bla
.claim_dest
,
285 sizeof(local_claim_dest
));
286 local_claim_dest
.type
= claimtype
;
288 soft_iface
= primary_if
->soft_iface
;
290 skb
= arp_create(ARPOP_REPLY
, ETH_P_ARP
,
291 /* IP DST: 0.0.0.0 */
293 primary_if
->soft_iface
,
294 /* IP SRC: 0.0.0.0 */
296 /* Ethernet DST: Broadcast */
298 /* Ethernet SRC/HW SRC: originator mac */
299 primary_if
->net_dev
->dev_addr
,
300 /* HW DST: FF:43:05:XX:YY:YY
301 * with XX = claim type
302 * and YY:YY = group id
304 (u8
*)&local_claim_dest
);
309 ethhdr
= (struct ethhdr
*)skb
->data
;
310 hw_src
= (u8
*)ethhdr
+ ETH_HLEN
+ sizeof(struct arphdr
);
312 /* now we pretend that the client would have sent this ... */
314 case BATADV_CLAIM_TYPE_CLAIM
:
315 /* normal claim frame
316 * set Ethernet SRC to the clients mac
318 ether_addr_copy(ethhdr
->h_source
, mac
);
319 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
320 "bla_send_claim(): CLAIM %pM on vid %d\n", mac
,
321 BATADV_PRINT_VID(vid
));
323 case BATADV_CLAIM_TYPE_UNCLAIM
:
325 * set HW SRC to the clients mac
327 ether_addr_copy(hw_src
, mac
);
328 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
329 "bla_send_claim(): UNCLAIM %pM on vid %d\n", mac
,
330 BATADV_PRINT_VID(vid
));
332 case BATADV_CLAIM_TYPE_ANNOUNCE
:
333 /* announcement frame
334 * set HW SRC to the special mac containg the crc
336 ether_addr_copy(hw_src
, mac
);
337 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
338 "bla_send_claim(): ANNOUNCE of %pM on vid %d\n",
339 ethhdr
->h_source
, BATADV_PRINT_VID(vid
));
341 case BATADV_CLAIM_TYPE_REQUEST
:
343 * set HW SRC and header destination to the receiving backbone
346 ether_addr_copy(hw_src
, mac
);
347 ether_addr_copy(ethhdr
->h_dest
, mac
);
348 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
349 "bla_send_claim(): REQUEST of %pM to %pM on vid %d\n",
350 ethhdr
->h_source
, ethhdr
->h_dest
,
351 BATADV_PRINT_VID(vid
));
355 if (vid
& BATADV_VLAN_HAS_TAG
)
356 skb
= vlan_insert_tag(skb
, htons(ETH_P_8021Q
),
357 vid
& VLAN_VID_MASK
);
359 skb_reset_mac_header(skb
);
360 skb
->protocol
= eth_type_trans(skb
, soft_iface
);
361 batadv_inc_counter(bat_priv
, BATADV_CNT_RX
);
362 batadv_add_counter(bat_priv
, BATADV_CNT_RX_BYTES
,
363 skb
->len
+ ETH_HLEN
);
364 soft_iface
->last_rx
= jiffies
;
369 batadv_hardif_free_ref(primary_if
);
373 * batadv_bla_get_backbone_gw
374 * @bat_priv: the bat priv with all the soft interface information
375 * @orig: the mac address of the originator
377 * @own_backbone: set if the requested backbone is local
379 * searches for the backbone gw or creates a new one if it could not
382 static struct batadv_bla_backbone_gw
*
383 batadv_bla_get_backbone_gw(struct batadv_priv
*bat_priv
, u8
*orig
,
384 unsigned short vid
, bool own_backbone
)
386 struct batadv_bla_backbone_gw
*entry
;
387 struct batadv_orig_node
*orig_node
;
390 entry
= batadv_backbone_hash_find(bat_priv
, orig
, vid
);
395 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
396 "bla_get_backbone_gw(): not found (%pM, %d), creating new entry\n",
397 orig
, BATADV_PRINT_VID(vid
));
399 entry
= kzalloc(sizeof(*entry
), GFP_ATOMIC
);
404 entry
->lasttime
= jiffies
;
405 entry
->crc
= BATADV_BLA_CRC_INIT
;
406 entry
->bat_priv
= bat_priv
;
407 atomic_set(&entry
->request_sent
, 0);
408 atomic_set(&entry
->wait_periods
, 0);
409 ether_addr_copy(entry
->orig
, orig
);
411 /* one for the hash, one for returning */
412 atomic_set(&entry
->refcount
, 2);
414 hash_added
= batadv_hash_add(bat_priv
->bla
.backbone_hash
,
415 batadv_compare_backbone_gw
,
416 batadv_choose_backbone_gw
, entry
,
419 if (unlikely(hash_added
!= 0)) {
420 /* hash failed, free the structure */
425 /* this is a gateway now, remove any TT entry on this VLAN */
426 orig_node
= batadv_orig_hash_find(bat_priv
, orig
);
428 batadv_tt_global_del_orig(bat_priv
, orig_node
, vid
,
429 "became a backbone gateway");
430 batadv_orig_node_free_ref(orig_node
);
434 batadv_bla_send_announce(bat_priv
, entry
);
436 /* this will be decreased in the worker thread */
437 atomic_inc(&entry
->request_sent
);
438 atomic_set(&entry
->wait_periods
, BATADV_BLA_WAIT_PERIODS
);
439 atomic_inc(&bat_priv
->bla
.num_requests
);
445 /* update or add the own backbone gw to make sure we announce
446 * where we receive other backbone gws
449 batadv_bla_update_own_backbone_gw(struct batadv_priv
*bat_priv
,
450 struct batadv_hard_iface
*primary_if
,
453 struct batadv_bla_backbone_gw
*backbone_gw
;
455 backbone_gw
= batadv_bla_get_backbone_gw(bat_priv
,
456 primary_if
->net_dev
->dev_addr
,
458 if (unlikely(!backbone_gw
))
461 backbone_gw
->lasttime
= jiffies
;
462 batadv_backbone_gw_free_ref(backbone_gw
);
466 * batadv_bla_answer_request - answer a bla request by sending own claims
467 * @bat_priv: the bat priv with all the soft interface information
468 * @primary_if: interface where the request came on
469 * @vid: the vid where the request came on
471 * Repeat all of our own claims, and finally send an ANNOUNCE frame
472 * to allow the requester another check if the CRC is correct now.
474 static void batadv_bla_answer_request(struct batadv_priv
*bat_priv
,
475 struct batadv_hard_iface
*primary_if
,
478 struct hlist_head
*head
;
479 struct batadv_hashtable
*hash
;
480 struct batadv_bla_claim
*claim
;
481 struct batadv_bla_backbone_gw
*backbone_gw
;
484 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
485 "bla_answer_request(): received a claim request, send all of our own claims again\n");
487 backbone_gw
= batadv_backbone_hash_find(bat_priv
,
488 primary_if
->net_dev
->dev_addr
,
493 hash
= bat_priv
->bla
.claim_hash
;
494 for (i
= 0; i
< hash
->size
; i
++) {
495 head
= &hash
->table
[i
];
498 hlist_for_each_entry_rcu(claim
, head
, hash_entry
) {
499 /* only own claims are interesting */
500 if (claim
->backbone_gw
!= backbone_gw
)
503 batadv_bla_send_claim(bat_priv
, claim
->addr
, claim
->vid
,
504 BATADV_CLAIM_TYPE_CLAIM
);
509 /* finally, send an announcement frame */
510 batadv_bla_send_announce(bat_priv
, backbone_gw
);
511 batadv_backbone_gw_free_ref(backbone_gw
);
515 * batadv_bla_send_request - send a request to repeat claims
516 * @backbone_gw: the backbone gateway from whom we are out of sync
518 * When the crc is wrong, ask the backbone gateway for a full table update.
519 * After the request, it will repeat all of his own claims and finally
520 * send an announcement claim with which we can check again.
522 static void batadv_bla_send_request(struct batadv_bla_backbone_gw
*backbone_gw
)
524 /* first, remove all old entries */
525 batadv_bla_del_backbone_claims(backbone_gw
);
527 batadv_dbg(BATADV_DBG_BLA
, backbone_gw
->bat_priv
,
528 "Sending REQUEST to %pM\n", backbone_gw
->orig
);
531 batadv_bla_send_claim(backbone_gw
->bat_priv
, backbone_gw
->orig
,
532 backbone_gw
->vid
, BATADV_CLAIM_TYPE_REQUEST
);
534 /* no local broadcasts should be sent or received, for now. */
535 if (!atomic_read(&backbone_gw
->request_sent
)) {
536 atomic_inc(&backbone_gw
->bat_priv
->bla
.num_requests
);
537 atomic_set(&backbone_gw
->request_sent
, 1);
542 * batadv_bla_send_announce
543 * @bat_priv: the bat priv with all the soft interface information
544 * @backbone_gw: our backbone gateway which should be announced
546 * This function sends an announcement. It is called from multiple
549 static void batadv_bla_send_announce(struct batadv_priv
*bat_priv
,
550 struct batadv_bla_backbone_gw
*backbone_gw
)
555 memcpy(mac
, batadv_announce_mac
, 4);
556 crc
= htons(backbone_gw
->crc
);
557 memcpy(&mac
[4], &crc
, 2);
559 batadv_bla_send_claim(bat_priv
, mac
, backbone_gw
->vid
,
560 BATADV_CLAIM_TYPE_ANNOUNCE
);
564 * batadv_bla_add_claim - Adds a claim in the claim hash
565 * @bat_priv: the bat priv with all the soft interface information
566 * @mac: the mac address of the claim
567 * @vid: the VLAN ID of the frame
568 * @backbone_gw: the backbone gateway which claims it
570 static void batadv_bla_add_claim(struct batadv_priv
*bat_priv
,
571 const u8
*mac
, const unsigned short vid
,
572 struct batadv_bla_backbone_gw
*backbone_gw
)
574 struct batadv_bla_claim
*claim
;
575 struct batadv_bla_claim search_claim
;
578 ether_addr_copy(search_claim
.addr
, mac
);
579 search_claim
.vid
= vid
;
580 claim
= batadv_claim_hash_find(bat_priv
, &search_claim
);
582 /* create a new claim entry if it does not exist yet. */
584 claim
= kzalloc(sizeof(*claim
), GFP_ATOMIC
);
588 ether_addr_copy(claim
->addr
, mac
);
590 claim
->lasttime
= jiffies
;
591 claim
->backbone_gw
= backbone_gw
;
593 atomic_set(&claim
->refcount
, 2);
594 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
595 "bla_add_claim(): adding new entry %pM, vid %d to hash ...\n",
596 mac
, BATADV_PRINT_VID(vid
));
597 hash_added
= batadv_hash_add(bat_priv
->bla
.claim_hash
,
598 batadv_compare_claim
,
599 batadv_choose_claim
, claim
,
602 if (unlikely(hash_added
!= 0)) {
603 /* only local changes happened. */
608 claim
->lasttime
= jiffies
;
609 if (claim
->backbone_gw
== backbone_gw
)
610 /* no need to register a new backbone */
613 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
614 "bla_add_claim(): changing ownership for %pM, vid %d\n",
615 mac
, BATADV_PRINT_VID(vid
));
617 claim
->backbone_gw
->crc
^= crc16(0, claim
->addr
, ETH_ALEN
);
618 batadv_backbone_gw_free_ref(claim
->backbone_gw
);
620 /* set (new) backbone gw */
621 atomic_inc(&backbone_gw
->refcount
);
622 claim
->backbone_gw
= backbone_gw
;
624 backbone_gw
->crc
^= crc16(0, claim
->addr
, ETH_ALEN
);
625 backbone_gw
->lasttime
= jiffies
;
628 batadv_claim_free_ref(claim
);
631 /* Delete a claim from the claim hash which has the
632 * given mac address and vid.
634 static void batadv_bla_del_claim(struct batadv_priv
*bat_priv
,
635 const u8
*mac
, const unsigned short vid
)
637 struct batadv_bla_claim search_claim
, *claim
;
639 ether_addr_copy(search_claim
.addr
, mac
);
640 search_claim
.vid
= vid
;
641 claim
= batadv_claim_hash_find(bat_priv
, &search_claim
);
645 batadv_dbg(BATADV_DBG_BLA
, bat_priv
, "bla_del_claim(): %pM, vid %d\n",
646 mac
, BATADV_PRINT_VID(vid
));
648 batadv_hash_remove(bat_priv
->bla
.claim_hash
, batadv_compare_claim
,
649 batadv_choose_claim
, claim
);
650 batadv_claim_free_ref(claim
); /* reference from the hash is gone */
652 claim
->backbone_gw
->crc
^= crc16(0, claim
->addr
, ETH_ALEN
);
654 /* don't need the reference from hash_find() anymore */
655 batadv_claim_free_ref(claim
);
658 /* check for ANNOUNCE frame, return 1 if handled */
659 static int batadv_handle_announce(struct batadv_priv
*bat_priv
, u8
*an_addr
,
660 u8
*backbone_addr
, unsigned short vid
)
662 struct batadv_bla_backbone_gw
*backbone_gw
;
665 if (memcmp(an_addr
, batadv_announce_mac
, 4) != 0)
668 backbone_gw
= batadv_bla_get_backbone_gw(bat_priv
, backbone_addr
, vid
,
671 if (unlikely(!backbone_gw
))
674 /* handle as ANNOUNCE frame */
675 backbone_gw
->lasttime
= jiffies
;
676 crc
= ntohs(*((__be16
*)(&an_addr
[4])));
678 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
679 "handle_announce(): ANNOUNCE vid %d (sent by %pM)... CRC = %#.4x\n",
680 BATADV_PRINT_VID(vid
), backbone_gw
->orig
, crc
);
682 if (backbone_gw
->crc
!= crc
) {
683 batadv_dbg(BATADV_DBG_BLA
, backbone_gw
->bat_priv
,
684 "handle_announce(): CRC FAILED for %pM/%d (my = %#.4x, sent = %#.4x)\n",
686 BATADV_PRINT_VID(backbone_gw
->vid
),
687 backbone_gw
->crc
, crc
);
689 batadv_bla_send_request(backbone_gw
);
691 /* if we have sent a request and the crc was OK,
692 * we can allow traffic again.
694 if (atomic_read(&backbone_gw
->request_sent
)) {
695 atomic_dec(&backbone_gw
->bat_priv
->bla
.num_requests
);
696 atomic_set(&backbone_gw
->request_sent
, 0);
700 batadv_backbone_gw_free_ref(backbone_gw
);
704 /* check for REQUEST frame, return 1 if handled */
705 static int batadv_handle_request(struct batadv_priv
*bat_priv
,
706 struct batadv_hard_iface
*primary_if
,
707 u8
*backbone_addr
, struct ethhdr
*ethhdr
,
710 /* check for REQUEST frame */
711 if (!batadv_compare_eth(backbone_addr
, ethhdr
->h_dest
))
714 /* sanity check, this should not happen on a normal switch,
715 * we ignore it in this case.
717 if (!batadv_compare_eth(ethhdr
->h_dest
, primary_if
->net_dev
->dev_addr
))
720 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
721 "handle_request(): REQUEST vid %d (sent by %pM)...\n",
722 BATADV_PRINT_VID(vid
), ethhdr
->h_source
);
724 batadv_bla_answer_request(bat_priv
, primary_if
, vid
);
728 /* check for UNCLAIM frame, return 1 if handled */
729 static int batadv_handle_unclaim(struct batadv_priv
*bat_priv
,
730 struct batadv_hard_iface
*primary_if
,
731 u8
*backbone_addr
, u8
*claim_addr
,
734 struct batadv_bla_backbone_gw
*backbone_gw
;
736 /* unclaim in any case if it is our own */
737 if (primary_if
&& batadv_compare_eth(backbone_addr
,
738 primary_if
->net_dev
->dev_addr
))
739 batadv_bla_send_claim(bat_priv
, claim_addr
, vid
,
740 BATADV_CLAIM_TYPE_UNCLAIM
);
742 backbone_gw
= batadv_backbone_hash_find(bat_priv
, backbone_addr
, vid
);
747 /* this must be an UNCLAIM frame */
748 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
749 "handle_unclaim(): UNCLAIM %pM on vid %d (sent by %pM)...\n",
750 claim_addr
, BATADV_PRINT_VID(vid
), backbone_gw
->orig
);
752 batadv_bla_del_claim(bat_priv
, claim_addr
, vid
);
753 batadv_backbone_gw_free_ref(backbone_gw
);
757 /* check for CLAIM frame, return 1 if handled */
758 static int batadv_handle_claim(struct batadv_priv
*bat_priv
,
759 struct batadv_hard_iface
*primary_if
,
760 u8
*backbone_addr
, u8
*claim_addr
,
763 struct batadv_bla_backbone_gw
*backbone_gw
;
765 /* register the gateway if not yet available, and add the claim. */
767 backbone_gw
= batadv_bla_get_backbone_gw(bat_priv
, backbone_addr
, vid
,
770 if (unlikely(!backbone_gw
))
773 /* this must be a CLAIM frame */
774 batadv_bla_add_claim(bat_priv
, claim_addr
, vid
, backbone_gw
);
775 if (batadv_compare_eth(backbone_addr
, primary_if
->net_dev
->dev_addr
))
776 batadv_bla_send_claim(bat_priv
, claim_addr
, vid
,
777 BATADV_CLAIM_TYPE_CLAIM
);
779 /* TODO: we could call something like tt_local_del() here. */
781 batadv_backbone_gw_free_ref(backbone_gw
);
786 * batadv_check_claim_group
787 * @bat_priv: the bat priv with all the soft interface information
788 * @primary_if: the primary interface of this batman interface
789 * @hw_src: the Hardware source in the ARP Header
790 * @hw_dst: the Hardware destination in the ARP Header
791 * @ethhdr: pointer to the Ethernet header of the claim frame
793 * checks if it is a claim packet and if its on the same group.
794 * This function also applies the group ID of the sender
795 * if it is in the same mesh.
798 * 2 - if it is a claim packet and on the same group
799 * 1 - if is a claim packet from another group
800 * 0 - if it is not a claim packet
802 static int batadv_check_claim_group(struct batadv_priv
*bat_priv
,
803 struct batadv_hard_iface
*primary_if
,
804 u8
*hw_src
, u8
*hw_dst
,
805 struct ethhdr
*ethhdr
)
808 struct batadv_orig_node
*orig_node
;
809 struct batadv_bla_claim_dst
*bla_dst
, *bla_dst_own
;
811 bla_dst
= (struct batadv_bla_claim_dst
*)hw_dst
;
812 bla_dst_own
= &bat_priv
->bla
.claim_dest
;
814 /* if announcement packet, use the source,
815 * otherwise assume it is in the hw_src
817 switch (bla_dst
->type
) {
818 case BATADV_CLAIM_TYPE_CLAIM
:
819 backbone_addr
= hw_src
;
821 case BATADV_CLAIM_TYPE_REQUEST
:
822 case BATADV_CLAIM_TYPE_ANNOUNCE
:
823 case BATADV_CLAIM_TYPE_UNCLAIM
:
824 backbone_addr
= ethhdr
->h_source
;
830 /* don't accept claim frames from ourselves */
831 if (batadv_compare_eth(backbone_addr
, primary_if
->net_dev
->dev_addr
))
834 /* if its already the same group, it is fine. */
835 if (bla_dst
->group
== bla_dst_own
->group
)
838 /* lets see if this originator is in our mesh */
839 orig_node
= batadv_orig_hash_find(bat_priv
, backbone_addr
);
841 /* dont accept claims from gateways which are not in
842 * the same mesh or group.
847 /* if our mesh friends mac is bigger, use it for ourselves. */
848 if (ntohs(bla_dst
->group
) > ntohs(bla_dst_own
->group
)) {
849 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
850 "taking other backbones claim group: %#.4x\n",
851 ntohs(bla_dst
->group
));
852 bla_dst_own
->group
= bla_dst
->group
;
855 batadv_orig_node_free_ref(orig_node
);
861 * batadv_bla_process_claim
862 * @bat_priv: the bat priv with all the soft interface information
863 * @primary_if: the primary hard interface of this batman soft interface
864 * @skb: the frame to be checked
866 * Check if this is a claim frame, and process it accordingly.
868 * returns 1 if it was a claim frame, otherwise return 0 to
869 * tell the callee that it can use the frame on its own.
871 static int batadv_bla_process_claim(struct batadv_priv
*bat_priv
,
872 struct batadv_hard_iface
*primary_if
,
875 struct batadv_bla_claim_dst
*bla_dst
, *bla_dst_own
;
877 struct vlan_hdr
*vhdr
, vhdr_buf
;
878 struct ethhdr
*ethhdr
;
879 struct arphdr
*arphdr
;
886 vid
= batadv_get_vid(skb
, 0);
887 ethhdr
= eth_hdr(skb
);
889 proto
= ethhdr
->h_proto
;
891 if (vid
& BATADV_VLAN_HAS_TAG
) {
892 /* Traverse the VLAN/Ethertypes.
894 * At this point it is known that the first protocol is a VLAN
895 * header, so start checking at the encapsulated protocol.
897 * The depth of the VLAN headers is recorded to drop BLA claim
898 * frames encapsulated into multiple VLAN headers (QinQ).
901 vhdr
= skb_header_pointer(skb
, headlen
, VLAN_HLEN
,
906 proto
= vhdr
->h_vlan_encapsulated_proto
;
907 headlen
+= VLAN_HLEN
;
909 } while (proto
== htons(ETH_P_8021Q
));
912 if (proto
!= htons(ETH_P_ARP
))
913 return 0; /* not a claim frame */
915 /* this must be a ARP frame. check if it is a claim. */
917 if (unlikely(!pskb_may_pull(skb
, headlen
+ arp_hdr_len(skb
->dev
))))
920 /* pskb_may_pull() may have modified the pointers, get ethhdr again */
921 ethhdr
= eth_hdr(skb
);
922 arphdr
= (struct arphdr
*)((u8
*)ethhdr
+ headlen
);
924 /* Check whether the ARP frame carries a valid
927 if (arphdr
->ar_hrd
!= htons(ARPHRD_ETHER
))
929 if (arphdr
->ar_pro
!= htons(ETH_P_IP
))
931 if (arphdr
->ar_hln
!= ETH_ALEN
)
933 if (arphdr
->ar_pln
!= 4)
936 hw_src
= (u8
*)arphdr
+ sizeof(struct arphdr
);
937 hw_dst
= hw_src
+ ETH_ALEN
+ 4;
938 bla_dst
= (struct batadv_bla_claim_dst
*)hw_dst
;
939 bla_dst_own
= &bat_priv
->bla
.claim_dest
;
941 /* check if it is a claim frame in general */
942 if (memcmp(bla_dst
->magic
, bla_dst_own
->magic
,
943 sizeof(bla_dst
->magic
)) != 0)
946 /* check if there is a claim frame encapsulated deeper in (QinQ) and
947 * drop that, as this is not supported by BLA but should also not be
953 /* check if it is a claim frame. */
954 ret
= batadv_check_claim_group(bat_priv
, primary_if
, hw_src
, hw_dst
,
957 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
958 "bla_process_claim(): received a claim frame from another group. From: %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n",
959 ethhdr
->h_source
, BATADV_PRINT_VID(vid
), hw_src
,
965 /* become a backbone gw ourselves on this vlan if not happened yet */
966 batadv_bla_update_own_backbone_gw(bat_priv
, primary_if
, vid
);
968 /* check for the different types of claim frames ... */
969 switch (bla_dst
->type
) {
970 case BATADV_CLAIM_TYPE_CLAIM
:
971 if (batadv_handle_claim(bat_priv
, primary_if
, hw_src
,
972 ethhdr
->h_source
, vid
))
975 case BATADV_CLAIM_TYPE_UNCLAIM
:
976 if (batadv_handle_unclaim(bat_priv
, primary_if
,
977 ethhdr
->h_source
, hw_src
, vid
))
981 case BATADV_CLAIM_TYPE_ANNOUNCE
:
982 if (batadv_handle_announce(bat_priv
, hw_src
, ethhdr
->h_source
,
986 case BATADV_CLAIM_TYPE_REQUEST
:
987 if (batadv_handle_request(bat_priv
, primary_if
, hw_src
, ethhdr
,
993 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
994 "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",
995 ethhdr
->h_source
, BATADV_PRINT_VID(vid
), hw_src
, hw_dst
);
999 /* Check when we last heard from other nodes, and remove them in case of
1000 * a time out, or clean all backbone gws if now is set.
1002 static void batadv_bla_purge_backbone_gw(struct batadv_priv
*bat_priv
, int now
)
1004 struct batadv_bla_backbone_gw
*backbone_gw
;
1005 struct hlist_node
*node_tmp
;
1006 struct hlist_head
*head
;
1007 struct batadv_hashtable
*hash
;
1008 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1011 hash
= bat_priv
->bla
.backbone_hash
;
1015 for (i
= 0; i
< hash
->size
; i
++) {
1016 head
= &hash
->table
[i
];
1017 list_lock
= &hash
->list_locks
[i
];
1019 spin_lock_bh(list_lock
);
1020 hlist_for_each_entry_safe(backbone_gw
, node_tmp
,
1024 if (!batadv_has_timed_out(backbone_gw
->lasttime
,
1025 BATADV_BLA_BACKBONE_TIMEOUT
))
1028 batadv_dbg(BATADV_DBG_BLA
, backbone_gw
->bat_priv
,
1029 "bla_purge_backbone_gw(): backbone gw %pM timed out\n",
1033 /* don't wait for the pending request anymore */
1034 if (atomic_read(&backbone_gw
->request_sent
))
1035 atomic_dec(&bat_priv
->bla
.num_requests
);
1037 batadv_bla_del_backbone_claims(backbone_gw
);
1039 hlist_del_rcu(&backbone_gw
->hash_entry
);
1040 batadv_backbone_gw_free_ref(backbone_gw
);
1042 spin_unlock_bh(list_lock
);
1047 * batadv_bla_purge_claims
1048 * @bat_priv: the bat priv with all the soft interface information
1049 * @primary_if: the selected primary interface, may be NULL if now is set
1050 * @now: whether the whole hash shall be wiped now
1052 * Check when we heard last time from our own claims, and remove them in case of
1053 * a time out, or clean all claims if now is set
1055 static void batadv_bla_purge_claims(struct batadv_priv
*bat_priv
,
1056 struct batadv_hard_iface
*primary_if
,
1059 struct batadv_bla_claim
*claim
;
1060 struct hlist_head
*head
;
1061 struct batadv_hashtable
*hash
;
1064 hash
= bat_priv
->bla
.claim_hash
;
1068 for (i
= 0; i
< hash
->size
; i
++) {
1069 head
= &hash
->table
[i
];
1072 hlist_for_each_entry_rcu(claim
, head
, hash_entry
) {
1075 if (!batadv_compare_eth(claim
->backbone_gw
->orig
,
1076 primary_if
->net_dev
->dev_addr
))
1078 if (!batadv_has_timed_out(claim
->lasttime
,
1079 BATADV_BLA_CLAIM_TIMEOUT
))
1082 batadv_dbg(BATADV_DBG_BLA
, bat_priv
,
1083 "bla_purge_claims(): %pM, vid %d, time out\n",
1084 claim
->addr
, claim
->vid
);
1087 batadv_handle_unclaim(bat_priv
, primary_if
,
1088 claim
->backbone_gw
->orig
,
1089 claim
->addr
, claim
->vid
);
1096 * batadv_bla_update_orig_address
1097 * @bat_priv: the bat priv with all the soft interface information
1098 * @primary_if: the new selected primary_if
1099 * @oldif: the old primary interface, may be NULL
1101 * Update the backbone gateways when the own orig address changes.
1103 void batadv_bla_update_orig_address(struct batadv_priv
*bat_priv
,
1104 struct batadv_hard_iface
*primary_if
,
1105 struct batadv_hard_iface
*oldif
)
1107 struct batadv_bla_backbone_gw
*backbone_gw
;
1108 struct hlist_head
*head
;
1109 struct batadv_hashtable
*hash
;
1113 /* reset bridge loop avoidance group id */
1114 group
= htons(crc16(0, primary_if
->net_dev
->dev_addr
, ETH_ALEN
));
1115 bat_priv
->bla
.claim_dest
.group
= group
;
1117 /* purge everything when bridge loop avoidance is turned off */
1118 if (!atomic_read(&bat_priv
->bridge_loop_avoidance
))
1122 batadv_bla_purge_claims(bat_priv
, NULL
, 1);
1123 batadv_bla_purge_backbone_gw(bat_priv
, 1);
1127 hash
= bat_priv
->bla
.backbone_hash
;
1131 for (i
= 0; i
< hash
->size
; i
++) {
1132 head
= &hash
->table
[i
];
1135 hlist_for_each_entry_rcu(backbone_gw
, head
, hash_entry
) {
1136 /* own orig still holds the old value. */
1137 if (!batadv_compare_eth(backbone_gw
->orig
,
1138 oldif
->net_dev
->dev_addr
))
1141 ether_addr_copy(backbone_gw
->orig
,
1142 primary_if
->net_dev
->dev_addr
);
1143 /* send an announce frame so others will ask for our
1144 * claims and update their tables.
1146 batadv_bla_send_announce(bat_priv
, backbone_gw
);
1152 /* periodic work to do:
1153 * * purge structures when they are too old
1154 * * send announcements
1156 static void batadv_bla_periodic_work(struct work_struct
*work
)
1158 struct delayed_work
*delayed_work
;
1159 struct batadv_priv
*bat_priv
;
1160 struct batadv_priv_bla
*priv_bla
;
1161 struct hlist_head
*head
;
1162 struct batadv_bla_backbone_gw
*backbone_gw
;
1163 struct batadv_hashtable
*hash
;
1164 struct batadv_hard_iface
*primary_if
;
1167 delayed_work
= container_of(work
, struct delayed_work
, work
);
1168 priv_bla
= container_of(delayed_work
, struct batadv_priv_bla
, work
);
1169 bat_priv
= container_of(priv_bla
, struct batadv_priv
, bla
);
1170 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1174 batadv_bla_purge_claims(bat_priv
, primary_if
, 0);
1175 batadv_bla_purge_backbone_gw(bat_priv
, 0);
1177 if (!atomic_read(&bat_priv
->bridge_loop_avoidance
))
1180 hash
= bat_priv
->bla
.backbone_hash
;
1184 for (i
= 0; i
< hash
->size
; i
++) {
1185 head
= &hash
->table
[i
];
1188 hlist_for_each_entry_rcu(backbone_gw
, head
, hash_entry
) {
1189 if (!batadv_compare_eth(backbone_gw
->orig
,
1190 primary_if
->net_dev
->dev_addr
))
1193 backbone_gw
->lasttime
= jiffies
;
1195 batadv_bla_send_announce(bat_priv
, backbone_gw
);
1197 /* request_sent is only set after creation to avoid
1198 * problems when we are not yet known as backbone gw
1201 * We can reset this now after we waited some periods
1202 * to give bridge forward delays and bla group forming
1206 if (atomic_read(&backbone_gw
->request_sent
) == 0)
1209 if (!atomic_dec_and_test(&backbone_gw
->wait_periods
))
1212 atomic_dec(&backbone_gw
->bat_priv
->bla
.num_requests
);
1213 atomic_set(&backbone_gw
->request_sent
, 0);
1219 batadv_hardif_free_ref(primary_if
);
1221 queue_delayed_work(batadv_event_workqueue
, &bat_priv
->bla
.work
,
1222 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH
));
1225 /* The hash for claim and backbone hash receive the same key because they
1226 * are getting initialized by hash_new with the same key. Reinitializing
1227 * them with to different keys to allow nested locking without generating
1230 static struct lock_class_key batadv_claim_hash_lock_class_key
;
1231 static struct lock_class_key batadv_backbone_hash_lock_class_key
;
1233 /* initialize all bla structures */
1234 int batadv_bla_init(struct batadv_priv
*bat_priv
)
1237 u8 claim_dest
[ETH_ALEN
] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00};
1238 struct batadv_hard_iface
*primary_if
;
1240 unsigned long entrytime
;
1242 spin_lock_init(&bat_priv
->bla
.bcast_duplist_lock
);
1244 batadv_dbg(BATADV_DBG_BLA
, bat_priv
, "bla hash registering\n");
1246 /* setting claim destination address */
1247 memcpy(&bat_priv
->bla
.claim_dest
.magic
, claim_dest
, 3);
1248 bat_priv
->bla
.claim_dest
.type
= 0;
1249 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1251 crc
= crc16(0, primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
1252 bat_priv
->bla
.claim_dest
.group
= htons(crc
);
1253 batadv_hardif_free_ref(primary_if
);
1255 bat_priv
->bla
.claim_dest
.group
= 0; /* will be set later */
1258 /* initialize the duplicate list */
1259 entrytime
= jiffies
- msecs_to_jiffies(BATADV_DUPLIST_TIMEOUT
);
1260 for (i
= 0; i
< BATADV_DUPLIST_SIZE
; i
++)
1261 bat_priv
->bla
.bcast_duplist
[i
].entrytime
= entrytime
;
1262 bat_priv
->bla
.bcast_duplist_curr
= 0;
1264 if (bat_priv
->bla
.claim_hash
)
1267 bat_priv
->bla
.claim_hash
= batadv_hash_new(128);
1268 bat_priv
->bla
.backbone_hash
= batadv_hash_new(32);
1270 if (!bat_priv
->bla
.claim_hash
|| !bat_priv
->bla
.backbone_hash
)
1273 batadv_hash_set_lock_class(bat_priv
->bla
.claim_hash
,
1274 &batadv_claim_hash_lock_class_key
);
1275 batadv_hash_set_lock_class(bat_priv
->bla
.backbone_hash
,
1276 &batadv_backbone_hash_lock_class_key
);
1278 batadv_dbg(BATADV_DBG_BLA
, bat_priv
, "bla hashes initialized\n");
1280 INIT_DELAYED_WORK(&bat_priv
->bla
.work
, batadv_bla_periodic_work
);
1282 queue_delayed_work(batadv_event_workqueue
, &bat_priv
->bla
.work
,
1283 msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH
));
1288 * batadv_bla_check_bcast_duplist
1289 * @bat_priv: the bat priv with all the soft interface information
1290 * @skb: contains the bcast_packet to be checked
1292 * check if it is on our broadcast list. Another gateway might
1293 * have sent the same packet because it is connected to the same backbone,
1294 * so we have to remove this duplicate.
1296 * This is performed by checking the CRC, which will tell us
1297 * with a good chance that it is the same packet. If it is furthermore
1298 * sent by another host, drop it. We allow equal packets from
1299 * the same host however as this might be intended.
1301 int batadv_bla_check_bcast_duplist(struct batadv_priv
*bat_priv
,
1302 struct sk_buff
*skb
)
1304 int i
, curr
, ret
= 0;
1306 struct batadv_bcast_packet
*bcast_packet
;
1307 struct batadv_bcast_duplist_entry
*entry
;
1309 bcast_packet
= (struct batadv_bcast_packet
*)skb
->data
;
1311 /* calculate the crc ... */
1312 crc
= batadv_skb_crc32(skb
, (u8
*)(bcast_packet
+ 1));
1314 spin_lock_bh(&bat_priv
->bla
.bcast_duplist_lock
);
1316 for (i
= 0; i
< BATADV_DUPLIST_SIZE
; i
++) {
1317 curr
= (bat_priv
->bla
.bcast_duplist_curr
+ i
);
1318 curr
%= BATADV_DUPLIST_SIZE
;
1319 entry
= &bat_priv
->bla
.bcast_duplist
[curr
];
1321 /* we can stop searching if the entry is too old ;
1322 * later entries will be even older
1324 if (batadv_has_timed_out(entry
->entrytime
,
1325 BATADV_DUPLIST_TIMEOUT
))
1328 if (entry
->crc
!= crc
)
1331 if (batadv_compare_eth(entry
->orig
, bcast_packet
->orig
))
1334 /* this entry seems to match: same crc, not too old,
1335 * and from another gw. therefore return 1 to forbid it.
1340 /* not found, add a new entry (overwrite the oldest entry)
1341 * and allow it, its the first occurrence.
1343 curr
= (bat_priv
->bla
.bcast_duplist_curr
+ BATADV_DUPLIST_SIZE
- 1);
1344 curr
%= BATADV_DUPLIST_SIZE
;
1345 entry
= &bat_priv
->bla
.bcast_duplist
[curr
];
1347 entry
->entrytime
= jiffies
;
1348 ether_addr_copy(entry
->orig
, bcast_packet
->orig
);
1349 bat_priv
->bla
.bcast_duplist_curr
= curr
;
1352 spin_unlock_bh(&bat_priv
->bla
.bcast_duplist_lock
);
1358 * batadv_bla_is_backbone_gw_orig
1359 * @bat_priv: the bat priv with all the soft interface information
1360 * @orig: originator mac address
1361 * @vid: VLAN identifier
1363 * Check if the originator is a gateway for the VLAN identified by vid.
1365 * Returns true if orig is a backbone for this vid, false otherwise.
1367 bool batadv_bla_is_backbone_gw_orig(struct batadv_priv
*bat_priv
, u8
*orig
,
1370 struct batadv_hashtable
*hash
= bat_priv
->bla
.backbone_hash
;
1371 struct hlist_head
*head
;
1372 struct batadv_bla_backbone_gw
*backbone_gw
;
1375 if (!atomic_read(&bat_priv
->bridge_loop_avoidance
))
1381 for (i
= 0; i
< hash
->size
; i
++) {
1382 head
= &hash
->table
[i
];
1385 hlist_for_each_entry_rcu(backbone_gw
, head
, hash_entry
) {
1386 if (batadv_compare_eth(backbone_gw
->orig
, orig
) &&
1387 backbone_gw
->vid
== vid
) {
1399 * batadv_bla_is_backbone_gw
1400 * @skb: the frame to be checked
1401 * @orig_node: the orig_node of the frame
1402 * @hdr_size: maximum length of the frame
1404 * bla_is_backbone_gw inspects the skb for the VLAN ID and returns 1
1405 * if the orig_node is also a gateway on the soft interface, otherwise it
1408 int batadv_bla_is_backbone_gw(struct sk_buff
*skb
,
1409 struct batadv_orig_node
*orig_node
, int hdr_size
)
1411 struct batadv_bla_backbone_gw
*backbone_gw
;
1414 if (!atomic_read(&orig_node
->bat_priv
->bridge_loop_avoidance
))
1417 /* first, find out the vid. */
1418 if (!pskb_may_pull(skb
, hdr_size
+ ETH_HLEN
))
1421 vid
= batadv_get_vid(skb
, hdr_size
);
1423 /* see if this originator is a backbone gw for this VLAN */
1424 backbone_gw
= batadv_backbone_hash_find(orig_node
->bat_priv
,
1425 orig_node
->orig
, vid
);
1429 batadv_backbone_gw_free_ref(backbone_gw
);
1433 /* free all bla structures (for softinterface free or module unload) */
1434 void batadv_bla_free(struct batadv_priv
*bat_priv
)
1436 struct batadv_hard_iface
*primary_if
;
1438 cancel_delayed_work_sync(&bat_priv
->bla
.work
);
1439 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1441 if (bat_priv
->bla
.claim_hash
) {
1442 batadv_bla_purge_claims(bat_priv
, primary_if
, 1);
1443 batadv_hash_destroy(bat_priv
->bla
.claim_hash
);
1444 bat_priv
->bla
.claim_hash
= NULL
;
1446 if (bat_priv
->bla
.backbone_hash
) {
1447 batadv_bla_purge_backbone_gw(bat_priv
, 1);
1448 batadv_hash_destroy(bat_priv
->bla
.backbone_hash
);
1449 bat_priv
->bla
.backbone_hash
= NULL
;
1452 batadv_hardif_free_ref(primary_if
);
1457 * @bat_priv: the bat priv with all the soft interface information
1458 * @skb: the frame to be checked
1459 * @vid: the VLAN ID of the frame
1460 * @is_bcast: the packet came in a broadcast packet type.
1462 * bla_rx avoidance checks if:
1463 * * we have to race for a claim
1464 * * if the frame is allowed on the LAN
1466 * in these cases, the skb is further handled by this function and
1467 * returns 1, otherwise it returns 0 and the caller shall further
1470 int batadv_bla_rx(struct batadv_priv
*bat_priv
, struct sk_buff
*skb
,
1471 unsigned short vid
, bool is_bcast
)
1473 struct ethhdr
*ethhdr
;
1474 struct batadv_bla_claim search_claim
, *claim
= NULL
;
1475 struct batadv_hard_iface
*primary_if
;
1478 ethhdr
= eth_hdr(skb
);
1480 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1484 if (!atomic_read(&bat_priv
->bridge_loop_avoidance
))
1487 if (unlikely(atomic_read(&bat_priv
->bla
.num_requests
)))
1488 /* don't allow broadcasts while requests are in flight */
1489 if (is_multicast_ether_addr(ethhdr
->h_dest
) && is_bcast
)
1492 ether_addr_copy(search_claim
.addr
, ethhdr
->h_source
);
1493 search_claim
.vid
= vid
;
1494 claim
= batadv_claim_hash_find(bat_priv
, &search_claim
);
1497 /* possible optimization: race for a claim */
1498 /* No claim exists yet, claim it for us!
1500 batadv_handle_claim(bat_priv
, primary_if
,
1501 primary_if
->net_dev
->dev_addr
,
1502 ethhdr
->h_source
, vid
);
1506 /* if it is our own claim ... */
1507 if (batadv_compare_eth(claim
->backbone_gw
->orig
,
1508 primary_if
->net_dev
->dev_addr
)) {
1509 /* ... allow it in any case */
1510 claim
->lasttime
= jiffies
;
1514 /* if it is a broadcast ... */
1515 if (is_multicast_ether_addr(ethhdr
->h_dest
) && is_bcast
) {
1516 /* ... drop it. the responsible gateway is in charge.
1518 * We need to check is_bcast because with the gateway
1519 * feature, broadcasts (like DHCP requests) may be sent
1520 * using a unicast packet type.
1524 /* seems the client considers us as its best gateway.
1525 * send a claim and update the claim table
1528 batadv_handle_claim(bat_priv
, primary_if
,
1529 primary_if
->net_dev
->dev_addr
,
1530 ethhdr
->h_source
, vid
);
1534 batadv_bla_update_own_backbone_gw(bat_priv
, primary_if
, vid
);
1544 batadv_hardif_free_ref(primary_if
);
1546 batadv_claim_free_ref(claim
);
1552 * @bat_priv: the bat priv with all the soft interface information
1553 * @skb: the frame to be checked
1554 * @vid: the VLAN ID of the frame
1557 * * a claim was received which has to be processed
1558 * * the frame is allowed on the mesh
1560 * in these cases, the skb is further handled by this function and
1561 * returns 1, otherwise it returns 0 and the caller shall further
1564 * This call might reallocate skb data.
1566 int batadv_bla_tx(struct batadv_priv
*bat_priv
, struct sk_buff
*skb
,
1569 struct ethhdr
*ethhdr
;
1570 struct batadv_bla_claim search_claim
, *claim
= NULL
;
1571 struct batadv_hard_iface
*primary_if
;
1574 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1578 if (!atomic_read(&bat_priv
->bridge_loop_avoidance
))
1581 if (batadv_bla_process_claim(bat_priv
, primary_if
, skb
))
1584 ethhdr
= eth_hdr(skb
);
1586 if (unlikely(atomic_read(&bat_priv
->bla
.num_requests
)))
1587 /* don't allow broadcasts while requests are in flight */
1588 if (is_multicast_ether_addr(ethhdr
->h_dest
))
1591 ether_addr_copy(search_claim
.addr
, ethhdr
->h_source
);
1592 search_claim
.vid
= vid
;
1594 claim
= batadv_claim_hash_find(bat_priv
, &search_claim
);
1596 /* if no claim exists, allow it. */
1600 /* check if we are responsible. */
1601 if (batadv_compare_eth(claim
->backbone_gw
->orig
,
1602 primary_if
->net_dev
->dev_addr
)) {
1603 /* if yes, the client has roamed and we have
1606 if (batadv_has_timed_out(claim
->lasttime
, 100)) {
1607 /* only unclaim if the last claim entry is
1608 * older than 100 ms to make sure we really
1609 * have a roaming client here.
1611 batadv_dbg(BATADV_DBG_BLA
, bat_priv
, "bla_tx(): Roaming client %pM detected. Unclaim it.\n",
1613 batadv_handle_unclaim(bat_priv
, primary_if
,
1614 primary_if
->net_dev
->dev_addr
,
1615 ethhdr
->h_source
, vid
);
1618 batadv_dbg(BATADV_DBG_BLA
, bat_priv
, "bla_tx(): Race for claim %pM detected. Drop packet.\n",
1624 /* check if it is a multicast/broadcast frame */
1625 if (is_multicast_ether_addr(ethhdr
->h_dest
)) {
1626 /* drop it. the responsible gateway has forwarded it into
1627 * the backbone network.
1631 /* we must allow it. at least if we are
1632 * responsible for the DESTINATION.
1637 batadv_bla_update_own_backbone_gw(bat_priv
, primary_if
, vid
);
1644 batadv_hardif_free_ref(primary_if
);
1646 batadv_claim_free_ref(claim
);
1650 int batadv_bla_claim_table_seq_print_text(struct seq_file
*seq
, void *offset
)
1652 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
1653 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
1654 struct batadv_hashtable
*hash
= bat_priv
->bla
.claim_hash
;
1655 struct batadv_bla_claim
*claim
;
1656 struct batadv_hard_iface
*primary_if
;
1657 struct hlist_head
*head
;
1662 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
1666 primary_addr
= primary_if
->net_dev
->dev_addr
;
1668 "Claims announced for the mesh %s (orig %pM, group id %#.4x)\n",
1669 net_dev
->name
, primary_addr
,
1670 ntohs(bat_priv
->bla
.claim_dest
.group
));
1671 seq_printf(seq
, " %-17s %-5s %-17s [o] (%-6s)\n",
1672 "Client", "VID", "Originator", "CRC");
1673 for (i
= 0; i
< hash
->size
; i
++) {
1674 head
= &hash
->table
[i
];
1677 hlist_for_each_entry_rcu(claim
, head
, hash_entry
) {
1678 is_own
= batadv_compare_eth(claim
->backbone_gw
->orig
,
1680 seq_printf(seq
, " * %pM on %5d by %pM [%c] (%#.4x)\n",
1681 claim
->addr
, BATADV_PRINT_VID(claim
->vid
),
1682 claim
->backbone_gw
->orig
,
1683 (is_own
? 'x' : ' '),
1684 claim
->backbone_gw
->crc
);
1690 batadv_hardif_free_ref(primary_if
);
1694 int batadv_bla_backbone_table_seq_print_text(struct seq_file
*seq
, void *offset
)
1696 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
1697 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
1698 struct batadv_hashtable
*hash
= bat_priv
->bla
.backbone_hash
;
1699 struct batadv_bla_backbone_gw
*backbone_gw
;
1700 struct batadv_hard_iface
*primary_if
;
1701 struct hlist_head
*head
;
1707 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
1711 primary_addr
= primary_if
->net_dev
->dev_addr
;
1713 "Backbones announced for the mesh %s (orig %pM, group id %#.4x)\n",
1714 net_dev
->name
, primary_addr
,
1715 ntohs(bat_priv
->bla
.claim_dest
.group
));
1716 seq_printf(seq
, " %-17s %-5s %-9s (%-6s)\n",
1717 "Originator", "VID", "last seen", "CRC");
1718 for (i
= 0; i
< hash
->size
; i
++) {
1719 head
= &hash
->table
[i
];
1722 hlist_for_each_entry_rcu(backbone_gw
, head
, hash_entry
) {
1723 msecs
= jiffies_to_msecs(jiffies
-
1724 backbone_gw
->lasttime
);
1725 secs
= msecs
/ 1000;
1726 msecs
= msecs
% 1000;
1728 is_own
= batadv_compare_eth(backbone_gw
->orig
,
1733 seq_printf(seq
, " * %pM on %5d %4i.%03is (%#.4x)\n",
1735 BATADV_PRINT_VID(backbone_gw
->vid
), secs
,
1736 msecs
, backbone_gw
->crc
);
1742 batadv_hardif_free_ref(primary_if
);