xref: /linux/net/batman-adv/bridge_loop_avoidance.c (revision 4c0a6a5b77152dce07804a60a453c779619a79d5)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) B.A.T.M.A.N. contributors:
3  *
4  * Simon Wunderlich
5  */
6 
7 #include "bridge_loop_avoidance.h"
8 #include "main.h"
9 
10 #include <linux/atomic.h>
11 #include <linux/bug.h>
12 #include <linux/byteorder/generic.h>
13 #include <linux/compiler.h>
14 #include <linux/container_of.h>
15 #include <linux/crc16.h>
16 #include <linux/err.h>
17 #include <linux/errno.h>
18 #include <linux/etherdevice.h>
19 #include <linux/gfp.h>
20 #include <linux/if_arp.h>
21 #include <linux/if_ether.h>
22 #include <linux/if_vlan.h>
23 #include <linux/jhash.h>
24 #include <linux/jiffies.h>
25 #include <linux/kref.h>
26 #include <linux/list.h>
27 #include <linux/lockdep.h>
28 #include <linux/netdevice.h>
29 #include <linux/netlink.h>
30 #include <linux/rculist.h>
31 #include <linux/rcupdate.h>
32 #include <linux/skbuff.h>
33 #include <linux/slab.h>
34 #include <linux/spinlock.h>
35 #include <linux/sprintf.h>
36 #include <linux/stddef.h>
37 #include <linux/string.h>
38 #include <linux/string_choices.h>
39 #include <linux/workqueue.h>
40 #include <net/arp.h>
41 #include <net/genetlink.h>
42 #include <net/netlink.h>
43 #include <uapi/linux/batadv_packet.h>
44 #include <uapi/linux/batman_adv.h>
45 
46 #include "hard-interface.h"
47 #include "hash.h"
48 #include "log.h"
49 #include "netlink.h"
50 #include "originator.h"
51 #include "translation-table.h"
52 
53 static const u8 batadv_announce_mac[4] = {0x43, 0x05, 0x43, 0x05};
54 
55 static void batadv_bla_periodic_work(struct work_struct *work);
56 static void
57 batadv_bla_send_announce(struct batadv_priv *bat_priv,
58 			 struct batadv_bla_backbone_gw *backbone_gw);
59 
60 /**
61  * batadv_choose_claim() - choose the right bucket for a claim.
62  * @data: data to hash
63  * @size: size of the hash table
64  *
65  * Return: the hash index of the claim
66  */
67 static inline u32 batadv_choose_claim(const void *data, u32 size)
68 {
69 	const struct batadv_bla_claim *claim = data;
70 	u32 hash = 0;
71 
72 	hash = jhash(&claim->addr, sizeof(claim->addr), hash);
73 	hash = jhash(&claim->vid, sizeof(claim->vid), hash);
74 
75 	return hash % size;
76 }
77 
78 /**
79  * batadv_choose_backbone_gw() - choose the right bucket for a backbone gateway.
80  * @data: data to hash
81  * @size: size of the hash table
82  *
83  * Return: the hash index of the backbone gateway
84  */
85 static inline u32 batadv_choose_backbone_gw(const void *data, u32 size)
86 {
87 	const struct batadv_bla_backbone_gw *gw;
88 	u32 hash = 0;
89 
90 	gw = data;
91 	hash = jhash(&gw->orig, sizeof(gw->orig), hash);
92 	hash = jhash(&gw->vid, sizeof(gw->vid), hash);
93 
94 	return hash % size;
95 }
96 
97 /**
98  * batadv_compare_backbone_gw() - compare address and vid of two backbone gws
99  * @node: list node of the first entry to compare
100  * @data2: pointer to the second backbone gateway
101  *
102  * Return: true if the backbones have the same data, false otherwise
103  */
104 static bool batadv_compare_backbone_gw(const struct hlist_node *node,
105 				       const void *data2)
106 {
107 	const void *data1 = container_of(node, struct batadv_bla_backbone_gw,
108 					 hash_entry);
109 	const struct batadv_bla_backbone_gw *gw1 = data1;
110 	const struct batadv_bla_backbone_gw *gw2 = data2;
111 
112 	if (!batadv_compare_eth(gw1->orig, gw2->orig))
113 		return false;
114 
115 	if (gw1->vid != gw2->vid)
116 		return false;
117 
118 	return true;
119 }
120 
121 /**
122  * batadv_compare_claim() - compare address and vid of two claims
123  * @node: list node of the first entry to compare
124  * @data2: pointer to the second claims
125  *
126  * Return: true if the claims have the same data, false otherwise
127  */
128 static bool batadv_compare_claim(const struct hlist_node *node,
129 				 const void *data2)
130 {
131 	const void *data1 = container_of(node, struct batadv_bla_claim,
132 					 hash_entry);
133 	const struct batadv_bla_claim *cl1 = data1;
134 	const struct batadv_bla_claim *cl2 = data2;
135 
136 	if (!batadv_compare_eth(cl1->addr, cl2->addr))
137 		return false;
138 
139 	if (cl1->vid != cl2->vid)
140 		return false;
141 
142 	return true;
143 }
144 
145 /**
146  * batadv_backbone_gw_release() - release backbone gw from lists and queue for
147  *  free after rcu grace period
148  * @ref: kref pointer of the backbone gw
149  */
150 static void batadv_backbone_gw_release(struct kref *ref)
151 {
152 	struct batadv_bla_backbone_gw *backbone_gw;
153 
154 	backbone_gw = container_of(ref, struct batadv_bla_backbone_gw,
155 				   refcount);
156 
157 	kfree_rcu(backbone_gw, rcu);
158 }
159 
160 /**
161  * batadv_backbone_gw_put() - decrement the backbone gw refcounter and possibly
162  *  release it
163  * @backbone_gw: backbone gateway to be free'd
164  */
165 static void batadv_backbone_gw_put(struct batadv_bla_backbone_gw *backbone_gw)
166 {
167 	if (!backbone_gw)
168 		return;
169 
170 	kref_put(&backbone_gw->refcount, batadv_backbone_gw_release);
171 }
172 
173 /**
174  * batadv_claim_release() - release claim from lists and queue for free after
175  *  rcu grace period
176  * @ref: kref pointer of the claim
177  */
178 static void batadv_claim_release(struct kref *ref)
179 {
180 	struct batadv_bla_backbone_gw *old_backbone_gw;
181 	struct batadv_bla_claim *claim;
182 
183 	claim = container_of(ref, struct batadv_bla_claim, refcount);
184 
185 	spin_lock_bh(&claim->backbone_lock);
186 	old_backbone_gw = claim->backbone_gw;
187 	claim->backbone_gw = NULL;
188 	spin_unlock_bh(&claim->backbone_lock);
189 
190 	spin_lock_bh(&old_backbone_gw->crc_lock);
191 	old_backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
192 	spin_unlock_bh(&old_backbone_gw->crc_lock);
193 
194 	batadv_backbone_gw_put(old_backbone_gw);
195 
196 	kfree_rcu(claim, rcu);
197 }
198 
199 /**
200  * batadv_claim_put() - decrement the claim refcounter and possibly release it
201  * @claim: claim to be free'd
202  */
203 static void batadv_claim_put(struct batadv_bla_claim *claim)
204 {
205 	if (!claim)
206 		return;
207 
208 	kref_put(&claim->refcount, batadv_claim_release);
209 }
210 
211 /**
212  * batadv_claim_hash_find() - looks for a claim in the claim hash
213  * @bat_priv: the bat priv with all the mesh interface information
214  * @data: search data (may be local/static data)
215  *
216  * Return: claim if found or NULL otherwise.
217  */
218 static struct batadv_bla_claim *
219 batadv_claim_hash_find(struct batadv_priv *bat_priv,
220 		       struct batadv_bla_claim *data)
221 {
222 	struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
223 	struct batadv_bla_claim *claim_tmp = NULL;
224 	struct batadv_bla_claim *claim;
225 	struct hlist_head *head;
226 	int index;
227 
228 	if (!hash)
229 		return NULL;
230 
231 	index = batadv_choose_claim(data, hash->size);
232 	head = &hash->table[index];
233 
234 	rcu_read_lock();
235 	hlist_for_each_entry_rcu(claim, head, hash_entry) {
236 		if (!batadv_compare_claim(&claim->hash_entry, data))
237 			continue;
238 
239 		if (!kref_get_unless_zero(&claim->refcount))
240 			continue;
241 
242 		claim_tmp = claim;
243 		break;
244 	}
245 	rcu_read_unlock();
246 
247 	return claim_tmp;
248 }
249 
250 /**
251  * batadv_backbone_hash_find() - looks for a backbone gateway in the hash
252  * @bat_priv: the bat priv with all the mesh interface information
253  * @addr: the address of the originator
254  * @vid: the VLAN ID
255  *
256  * Return: backbone gateway if found or NULL otherwise
257  */
258 static struct batadv_bla_backbone_gw *
259 batadv_backbone_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
260 			  unsigned short vid)
261 {
262 	struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
263 	struct batadv_bla_backbone_gw *backbone_gw_tmp = NULL;
264 	struct batadv_bla_backbone_gw search_entry;
265 	struct batadv_bla_backbone_gw *backbone_gw;
266 	struct hlist_head *head;
267 	int index;
268 
269 	if (!hash)
270 		return NULL;
271 
272 	ether_addr_copy(search_entry.orig, addr);
273 	search_entry.vid = vid;
274 
275 	index = batadv_choose_backbone_gw(&search_entry, hash->size);
276 	head = &hash->table[index];
277 
278 	rcu_read_lock();
279 	hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
280 		if (!batadv_compare_backbone_gw(&backbone_gw->hash_entry,
281 						&search_entry))
282 			continue;
283 
284 		if (!kref_get_unless_zero(&backbone_gw->refcount))
285 			continue;
286 
287 		backbone_gw_tmp = backbone_gw;
288 		break;
289 	}
290 	rcu_read_unlock();
291 
292 	return backbone_gw_tmp;
293 }
294 
295 /**
296  * batadv_bla_del_backbone_claims() - delete all claims for a backbone
297  * @backbone_gw: backbone gateway where the claims should be removed
298  */
299 static void
300 batadv_bla_del_backbone_claims(struct batadv_bla_backbone_gw *backbone_gw)
301 {
302 	spinlock_t *list_lock;	/* protects write access to the hash lists */
303 	struct batadv_bla_claim *claim;
304 	struct batadv_hashtable *hash;
305 	struct hlist_node *node_tmp;
306 	struct hlist_head *head;
307 	int i;
308 
309 	hash = backbone_gw->bat_priv->bla.claim_hash;
310 	if (!hash)
311 		return;
312 
313 	for (i = 0; i < hash->size; i++) {
314 		head = &hash->table[i];
315 		list_lock = &hash->list_locks[i];
316 
317 		spin_lock_bh(list_lock);
318 		hlist_for_each_entry_safe(claim, node_tmp,
319 					  head, hash_entry) {
320 			if (claim->backbone_gw != backbone_gw)
321 				continue;
322 
323 			hlist_del_rcu(&claim->hash_entry);
324 			batadv_claim_put(claim);
325 		}
326 		spin_unlock_bh(list_lock);
327 	}
328 }
329 
330 /**
331  * batadv_bla_send_claim() - sends a claim frame according to the provided info
332  * @bat_priv: the bat priv with all the mesh interface information
333  * @mac: the mac address to be announced within the claim
334  * @vid: the VLAN ID
335  * @claimtype: the type of the claim (CLAIM, UNCLAIM, ANNOUNCE, ...)
336  */
337 static void batadv_bla_send_claim(struct batadv_priv *bat_priv, const u8 *mac,
338 				  unsigned short vid, int claimtype)
339 {
340 	struct batadv_bla_claim_dst local_claim_dest;
341 	struct batadv_hard_iface *primary_if;
342 	struct ethhdr *ethhdr;
343 	struct sk_buff *skb;
344 	__be32 zeroip = 0;
345 	u8 *hw_src;
346 
347 	primary_if = batadv_primary_if_get_selected(bat_priv);
348 	if (!primary_if)
349 		return;
350 
351 	memcpy(&local_claim_dest, &bat_priv->bla.claim_dest,
352 	       sizeof(local_claim_dest));
353 	local_claim_dest.type = claimtype;
354 
355 	skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
356 			 /* IP DST: 0.0.0.0 */
357 			 zeroip,
358 			 primary_if->mesh_iface,
359 			 /* IP SRC: 0.0.0.0 */
360 			 zeroip,
361 			 /* Ethernet DST: Broadcast */
362 			 NULL,
363 			 /* Ethernet SRC/HW SRC:  originator mac */
364 			 primary_if->net_dev->dev_addr,
365 			 /* HW DST: FF:43:05:XX:YY:YY
366 			  * with XX   = claim type
367 			  * and YY:YY = group id
368 			  */
369 			 (u8 *)&local_claim_dest);
370 
371 	if (!skb)
372 		goto out;
373 
374 	ethhdr = (struct ethhdr *)skb->data;
375 	hw_src = (u8 *)ethhdr + ETH_HLEN + sizeof(struct arphdr);
376 
377 	/* now we pretend that the client would have sent this ... */
378 	switch (claimtype) {
379 	case BATADV_CLAIM_TYPE_CLAIM:
380 		/* normal claim frame
381 		 * set Ethernet SRC to the clients mac
382 		 */
383 		ether_addr_copy(ethhdr->h_source, mac);
384 		batadv_dbg(BATADV_DBG_BLA, bat_priv,
385 			   "%s(): CLAIM %pM on vid %d\n", __func__, mac,
386 			   batadv_print_vid(vid));
387 		break;
388 	case BATADV_CLAIM_TYPE_UNCLAIM:
389 		/* unclaim frame
390 		 * set HW SRC to the clients mac
391 		 */
392 		ether_addr_copy(hw_src, mac);
393 		batadv_dbg(BATADV_DBG_BLA, bat_priv,
394 			   "%s(): UNCLAIM %pM on vid %d\n", __func__, mac,
395 			   batadv_print_vid(vid));
396 		break;
397 	case BATADV_CLAIM_TYPE_ANNOUNCE:
398 		/* announcement frame
399 		 * set HW SRC to the special mac containing the crc
400 		 */
401 		ether_addr_copy(hw_src, mac);
402 		batadv_dbg(BATADV_DBG_BLA, bat_priv,
403 			   "%s(): ANNOUNCE of %pM on vid %d\n", __func__,
404 			   ethhdr->h_source, batadv_print_vid(vid));
405 		break;
406 	case BATADV_CLAIM_TYPE_REQUEST:
407 		/* request frame
408 		 * set HW SRC and header destination to the receiving backbone
409 		 * gws mac
410 		 */
411 		ether_addr_copy(hw_src, mac);
412 		ether_addr_copy(ethhdr->h_dest, mac);
413 		batadv_dbg(BATADV_DBG_BLA, bat_priv,
414 			   "%s(): REQUEST of %pM to %pM on vid %d\n", __func__,
415 			   ethhdr->h_source, ethhdr->h_dest,
416 			   batadv_print_vid(vid));
417 		break;
418 	case BATADV_CLAIM_TYPE_LOOPDETECT:
419 		ether_addr_copy(ethhdr->h_source, mac);
420 		batadv_dbg(BATADV_DBG_BLA, bat_priv,
421 			   "%s(): LOOPDETECT of %pM to %pM on vid %d\n",
422 			   __func__, ethhdr->h_source, ethhdr->h_dest,
423 			   batadv_print_vid(vid));
424 
425 		break;
426 	}
427 
428 	if (vid & BATADV_VLAN_HAS_TAG) {
429 		skb = vlan_insert_tag(skb, htons(ETH_P_8021Q),
430 				      vid & VLAN_VID_MASK);
431 		if (!skb)
432 			goto out;
433 	}
434 
435 	skb_reset_mac_header(skb);
436 	skb->protocol = eth_type_trans(skb, primary_if->mesh_iface);
437 	batadv_inc_counter(bat_priv, BATADV_CNT_RX);
438 	batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
439 			   skb->len + ETH_HLEN);
440 
441 	netif_rx(skb);
442 out:
443 	batadv_hardif_put(primary_if);
444 }
445 
446 /**
447  * batadv_bla_loopdetect_report() - worker for reporting the loop
448  * @work: work queue item
449  *
450  * Throws an uevent, as the loopdetect check function can't do that itself
451  * since the kernel may sleep while throwing uevents.
452  */
453 static void batadv_bla_loopdetect_report(struct work_struct *work)
454 {
455 	struct batadv_bla_backbone_gw *backbone_gw;
456 	struct batadv_priv *bat_priv;
457 	char vid_str[6] = { '\0' };
458 
459 	backbone_gw = container_of(work, struct batadv_bla_backbone_gw,
460 				   report_work);
461 	bat_priv = backbone_gw->bat_priv;
462 
463 	batadv_info(bat_priv->mesh_iface,
464 		    "Possible loop on VLAN %d detected which can't be handled by BLA - please check your network setup!\n",
465 		    batadv_print_vid(backbone_gw->vid));
466 	snprintf(vid_str, sizeof(vid_str), "%d",
467 		 batadv_print_vid(backbone_gw->vid));
468 	vid_str[sizeof(vid_str) - 1] = 0;
469 
470 	batadv_throw_uevent(bat_priv, BATADV_UEV_BLA, BATADV_UEV_LOOPDETECT,
471 			    vid_str);
472 
473 	batadv_backbone_gw_put(backbone_gw);
474 }
475 
476 /**
477  * batadv_bla_get_backbone_gw() - finds or creates a backbone gateway
478  * @bat_priv: the bat priv with all the mesh interface information
479  * @orig: the mac address of the originator
480  * @vid: the VLAN ID
481  * @own_backbone: set if the requested backbone is local
482  *
483  * Return: the (possibly created) backbone gateway or NULL on error
484  */
485 static struct batadv_bla_backbone_gw *
486 batadv_bla_get_backbone_gw(struct batadv_priv *bat_priv, const u8 *orig,
487 			   unsigned short vid, bool own_backbone)
488 {
489 	struct batadv_bla_backbone_gw *entry;
490 	struct batadv_orig_node *orig_node;
491 	int hash_added;
492 
493 	entry = batadv_backbone_hash_find(bat_priv, orig, vid);
494 
495 	if (entry)
496 		return entry;
497 
498 	batadv_dbg(BATADV_DBG_BLA, bat_priv,
499 		   "%s(): not found (%pM, %d), creating new entry\n", __func__,
500 		   orig, batadv_print_vid(vid));
501 
502 	entry = kzalloc_obj(*entry, GFP_ATOMIC);
503 	if (!entry)
504 		return NULL;
505 
506 	entry->vid = vid;
507 	WRITE_ONCE(entry->lasttime, jiffies);
508 	entry->crc = BATADV_BLA_CRC_INIT;
509 	entry->bat_priv = bat_priv;
510 	spin_lock_init(&entry->crc_lock);
511 	entry->state = BATADV_BLA_BACKBONE_GW_SYNCED;
512 	entry->wait_periods = 0;
513 	ether_addr_copy(entry->orig, orig);
514 	INIT_WORK(&entry->report_work, batadv_bla_loopdetect_report);
515 	kref_init(&entry->refcount);
516 
517 	kref_get(&entry->refcount);
518 	hash_added = batadv_hash_add(bat_priv->bla.backbone_hash,
519 				     batadv_compare_backbone_gw,
520 				     batadv_choose_backbone_gw, entry,
521 				     &entry->hash_entry);
522 
523 	if (unlikely(hash_added != 0)) {
524 		/* hash failed, free the structure */
525 		kfree(entry);
526 		return NULL;
527 	}
528 
529 	/* this is a gateway now, remove any TT entry on this VLAN */
530 	orig_node = batadv_orig_hash_find(bat_priv, orig);
531 	if (orig_node) {
532 		batadv_tt_global_del_orig(bat_priv, orig_node, vid,
533 					  "became a backbone gateway");
534 		batadv_orig_node_put(orig_node);
535 	}
536 
537 	if (own_backbone) {
538 		batadv_bla_send_announce(bat_priv, entry);
539 
540 		/* this will be decreased in the worker thread */
541 		spin_lock_bh(&bat_priv->bla.num_requests_lock);
542 		if (entry->state == BATADV_BLA_BACKBONE_GW_SYNCED) {
543 			entry->state = BATADV_BLA_BACKBONE_GW_UNSYNCED;
544 			entry->wait_periods = BATADV_BLA_WAIT_PERIODS;
545 			atomic_inc(&bat_priv->bla.num_requests);
546 		}
547 		spin_unlock_bh(&bat_priv->bla.num_requests_lock);
548 	}
549 
550 	return entry;
551 }
552 
553 /**
554  * batadv_bla_update_own_backbone_gw() - updates the own backbone gw for a VLAN
555  * @bat_priv: the bat priv with all the mesh interface information
556  * @primary_if: the selected primary interface
557  * @vid: VLAN identifier
558  *
559  * update or add the own backbone gw to make sure we announce
560  * where we receive other backbone gws
561  */
562 static void
563 batadv_bla_update_own_backbone_gw(struct batadv_priv *bat_priv,
564 				  struct batadv_hard_iface *primary_if,
565 				  unsigned short vid)
566 {
567 	struct batadv_bla_backbone_gw *backbone_gw;
568 
569 	backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
570 						 primary_if->net_dev->dev_addr,
571 						 vid, true);
572 	if (unlikely(!backbone_gw))
573 		return;
574 
575 	WRITE_ONCE(backbone_gw->lasttime, jiffies);
576 	batadv_backbone_gw_put(backbone_gw);
577 }
578 
579 /**
580  * batadv_bla_answer_request() - answer a bla request by sending own claims
581  * @bat_priv: the bat priv with all the mesh interface information
582  * @primary_if: interface where the request came on
583  * @vid: the vid where the request came on
584  *
585  * Repeat all of our own claims, and finally send an ANNOUNCE frame
586  * to allow the requester another check if the CRC is correct now.
587  */
588 static void batadv_bla_answer_request(struct batadv_priv *bat_priv,
589 				      struct batadv_hard_iface *primary_if,
590 				      unsigned short vid)
591 {
592 	struct batadv_bla_backbone_gw *backbone_gw;
593 	struct batadv_bla_claim *claim;
594 	struct batadv_hashtable *hash;
595 	struct hlist_head *head;
596 	int i;
597 
598 	batadv_dbg(BATADV_DBG_BLA, bat_priv,
599 		   "%s(): received a claim request, send all of our own claims again\n",
600 		   __func__);
601 
602 	backbone_gw = batadv_backbone_hash_find(bat_priv,
603 						primary_if->net_dev->dev_addr,
604 						vid);
605 	if (!backbone_gw)
606 		return;
607 
608 	hash = bat_priv->bla.claim_hash;
609 	for (i = 0; i < hash->size; i++) {
610 		head = &hash->table[i];
611 
612 		rcu_read_lock();
613 		hlist_for_each_entry_rcu(claim, head, hash_entry) {
614 			/* only own claims are interesting */
615 			if (claim->backbone_gw != backbone_gw)
616 				continue;
617 
618 			batadv_bla_send_claim(bat_priv, claim->addr, claim->vid,
619 					      BATADV_CLAIM_TYPE_CLAIM);
620 		}
621 		rcu_read_unlock();
622 	}
623 
624 	/* finally, send an announcement frame */
625 	batadv_bla_send_announce(bat_priv, backbone_gw);
626 	batadv_backbone_gw_put(backbone_gw);
627 }
628 
629 /**
630  * batadv_bla_send_request() - send a request to repeat claims
631  * @backbone_gw: the backbone gateway from whom we are out of sync
632  *
633  * When the crc is wrong, ask the backbone gateway for a full table update.
634  * After the request, it will repeat all of his own claims and finally
635  * send an announcement claim with which we can check again.
636  */
637 static void batadv_bla_send_request(struct batadv_bla_backbone_gw *backbone_gw)
638 {
639 	/* first, remove all old entries */
640 	batadv_bla_del_backbone_claims(backbone_gw);
641 
642 	batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
643 		   "Sending REQUEST to %pM\n", backbone_gw->orig);
644 
645 	/* send request */
646 	batadv_bla_send_claim(backbone_gw->bat_priv, backbone_gw->orig,
647 			      backbone_gw->vid, BATADV_CLAIM_TYPE_REQUEST);
648 
649 	/* no local broadcasts should be sent or received, for now. */
650 	spin_lock_bh(&backbone_gw->bat_priv->bla.num_requests_lock);
651 	if (backbone_gw->state == BATADV_BLA_BACKBONE_GW_SYNCED) {
652 		backbone_gw->state = BATADV_BLA_BACKBONE_GW_UNSYNCED;
653 		atomic_inc(&backbone_gw->bat_priv->bla.num_requests);
654 	}
655 	spin_unlock_bh(&backbone_gw->bat_priv->bla.num_requests_lock);
656 }
657 
658 /**
659  * batadv_bla_send_announce() - Send an announcement frame
660  * @bat_priv: the bat priv with all the mesh interface information
661  * @backbone_gw: our backbone gateway which should be announced
662  */
663 static void batadv_bla_send_announce(struct batadv_priv *bat_priv,
664 				     struct batadv_bla_backbone_gw *backbone_gw)
665 {
666 	u8 mac[ETH_ALEN] __aligned(2);
667 	__be16 crc;
668 
669 	memcpy(mac, batadv_announce_mac, 4);
670 	spin_lock_bh(&backbone_gw->crc_lock);
671 	crc = htons(backbone_gw->crc);
672 	spin_unlock_bh(&backbone_gw->crc_lock);
673 	memcpy(&mac[4], &crc, 2);
674 
675 	batadv_bla_send_claim(bat_priv, mac, backbone_gw->vid,
676 			      BATADV_CLAIM_TYPE_ANNOUNCE);
677 }
678 
679 /**
680  * batadv_bla_add_claim() - Adds a claim in the claim hash
681  * @bat_priv: the bat priv with all the mesh interface information
682  * @mac: the mac address of the claim
683  * @vid: the VLAN ID of the frame
684  * @backbone_gw: the backbone gateway which claims it
685  */
686 static void batadv_bla_add_claim(struct batadv_priv *bat_priv,
687 				 const u8 *mac, const unsigned short vid,
688 				 struct batadv_bla_backbone_gw *backbone_gw)
689 {
690 	struct batadv_bla_backbone_gw *old_backbone_gw;
691 	struct batadv_bla_claim search_claim;
692 	struct batadv_bla_claim *claim;
693 	int hash_added;
694 	u16 claim_crc;
695 	bool changed;
696 
697 	ether_addr_copy(search_claim.addr, mac);
698 	search_claim.vid = vid;
699 	claim = batadv_claim_hash_find(bat_priv, &search_claim);
700 	claim_crc = crc16(0, mac, ETH_ALEN);
701 
702 	/* create a new claim entry if it does not exist yet. */
703 	if (!claim) {
704 		claim = kzalloc_obj(*claim, GFP_ATOMIC);
705 		if (!claim)
706 			return;
707 
708 		ether_addr_copy(claim->addr, mac);
709 		spin_lock_init(&claim->backbone_lock);
710 		claim->vid = vid;
711 		WRITE_ONCE(claim->lasttime, jiffies);
712 		kref_get(&backbone_gw->refcount);
713 		claim->backbone_gw = backbone_gw;
714 		kref_init(&claim->refcount);
715 
716 		batadv_dbg(BATADV_DBG_BLA, bat_priv,
717 			   "%s(): adding new entry %pM, vid %d to hash ...\n",
718 			   __func__, mac, batadv_print_vid(vid));
719 
720 		kref_get(&claim->refcount);
721 		hash_added = batadv_hash_add(bat_priv->bla.claim_hash,
722 					     batadv_compare_claim,
723 					     batadv_choose_claim, claim,
724 					     &claim->hash_entry);
725 
726 		if (unlikely(hash_added != 0)) {
727 			/* only local changes happened. */
728 			batadv_backbone_gw_put(backbone_gw);
729 			kfree(claim);
730 			return;
731 		}
732 
733 		spin_lock_bh(&backbone_gw->crc_lock);
734 		backbone_gw->crc ^= claim_crc;
735 		spin_unlock_bh(&backbone_gw->crc_lock);
736 
737 		WRITE_ONCE(backbone_gw->lasttime, jiffies);
738 
739 		batadv_claim_put(claim);
740 		return;
741 	}
742 
743 	WRITE_ONCE(claim->lasttime, jiffies);
744 
745 	/* replace backbone_gw atomically and adjust reference counters */
746 	spin_lock_bh(&claim->backbone_lock);
747 	if (claim->backbone_gw != backbone_gw) {
748 		changed = true;
749 
750 		old_backbone_gw = claim->backbone_gw;
751 		kref_get(&backbone_gw->refcount);
752 		claim->backbone_gw = backbone_gw;
753 	} else {
754 		old_backbone_gw = NULL;
755 		changed = false;
756 	}
757 	spin_unlock_bh(&claim->backbone_lock);
758 
759 	if (changed) {
760 		batadv_dbg(BATADV_DBG_BLA, bat_priv,
761 			   "%s(): changing ownership for %pM, vid %d to gw %pM\n",
762 			   __func__, mac, batadv_print_vid(vid),
763 			   backbone_gw->orig);
764 
765 		/* add claim address to new backbone_gw */
766 		spin_lock_bh(&backbone_gw->crc_lock);
767 		backbone_gw->crc ^= claim_crc;
768 		spin_unlock_bh(&backbone_gw->crc_lock);
769 
770 		WRITE_ONCE(backbone_gw->lasttime, jiffies);
771 	}
772 
773 	if (old_backbone_gw) {
774 		/* remove claim address from old backbone_gw */
775 		spin_lock_bh(&old_backbone_gw->crc_lock);
776 		old_backbone_gw->crc ^= claim_crc;
777 		spin_unlock_bh(&old_backbone_gw->crc_lock);
778 
779 		batadv_backbone_gw_put(old_backbone_gw);
780 	}
781 
782 	batadv_claim_put(claim);
783 }
784 
785 /**
786  * batadv_bla_claim_get_backbone_gw() - Get valid reference for backbone_gw of
787  *  claim
788  * @claim: claim whose backbone_gw should be returned
789  *
790  * Return: valid reference to claim::backbone_gw
791  */
792 static struct batadv_bla_backbone_gw *
793 batadv_bla_claim_get_backbone_gw(struct batadv_bla_claim *claim)
794 {
795 	struct batadv_bla_backbone_gw *backbone_gw;
796 
797 	spin_lock_bh(&claim->backbone_lock);
798 	backbone_gw = claim->backbone_gw;
799 	kref_get(&backbone_gw->refcount);
800 	spin_unlock_bh(&claim->backbone_lock);
801 
802 	return backbone_gw;
803 }
804 
805 /**
806  * batadv_bla_del_claim() - delete a claim from the claim hash
807  * @bat_priv: the bat priv with all the mesh interface information
808  * @mac: mac address of the claim to be removed
809  * @vid: VLAN id for the claim to be removed
810  */
811 static void batadv_bla_del_claim(struct batadv_priv *bat_priv,
812 				 const u8 *mac, const unsigned short vid)
813 {
814 	struct batadv_bla_claim *claim_removed_entry;
815 	struct hlist_node *claim_removed_node;
816 	struct batadv_bla_claim search_claim;
817 	struct batadv_bla_claim *claim;
818 
819 	ether_addr_copy(search_claim.addr, mac);
820 	search_claim.vid = vid;
821 	claim = batadv_claim_hash_find(bat_priv, &search_claim);
822 	if (!claim)
823 		return;
824 
825 	batadv_dbg(BATADV_DBG_BLA, bat_priv, "%s(): %pM, vid %d\n", __func__,
826 		   mac, batadv_print_vid(vid));
827 
828 	claim_removed_node = batadv_hash_remove(bat_priv->bla.claim_hash,
829 						batadv_compare_claim,
830 						batadv_choose_claim, claim);
831 	if (!claim_removed_node)
832 		goto free_claim;
833 
834 	/* reference from the hash is gone */
835 	claim_removed_entry = hlist_entry(claim_removed_node,
836 					  struct batadv_bla_claim, hash_entry);
837 	batadv_claim_put(claim_removed_entry);
838 
839 free_claim:
840 	/* don't need the reference from hash_find() anymore */
841 	batadv_claim_put(claim);
842 }
843 
844 /**
845  * batadv_handle_announce() - check for ANNOUNCE frame
846  * @bat_priv: the bat priv with all the mesh interface information
847  * @an_addr: announcement mac address (ARP Sender HW address)
848  * @backbone_addr: originator address of the sender (Ethernet source MAC)
849  * @vid: the VLAN ID of the frame
850  *
851  * Return: true if handled
852  */
853 static bool batadv_handle_announce(struct batadv_priv *bat_priv, u8 *an_addr,
854 				   u8 *backbone_addr, unsigned short vid)
855 {
856 	struct batadv_bla_backbone_gw *backbone_gw;
857 	u16 backbone_crc;
858 	u16 crc;
859 
860 	if (memcmp(an_addr, batadv_announce_mac, 4) != 0)
861 		return false;
862 
863 	backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
864 						 false);
865 
866 	if (unlikely(!backbone_gw))
867 		return true;
868 
869 	/* handle as ANNOUNCE frame */
870 	WRITE_ONCE(backbone_gw->lasttime, jiffies);
871 	crc = ntohs(*((__force __be16 *)(&an_addr[4])));
872 
873 	batadv_dbg(BATADV_DBG_BLA, bat_priv,
874 		   "%s(): ANNOUNCE vid %d (sent by %pM)... CRC = %#.4x\n",
875 		   __func__, batadv_print_vid(vid), backbone_gw->orig, crc);
876 
877 	spin_lock_bh(&backbone_gw->crc_lock);
878 	backbone_crc = backbone_gw->crc;
879 	spin_unlock_bh(&backbone_gw->crc_lock);
880 
881 	if (backbone_crc != crc) {
882 		batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
883 			   "%s(): CRC FAILED for %pM/%d (my = %#.4x, sent = %#.4x)\n",
884 			   __func__, backbone_gw->orig,
885 			   batadv_print_vid(backbone_gw->vid),
886 			   backbone_crc, crc);
887 
888 		batadv_bla_send_request(backbone_gw);
889 	} else {
890 		/* if we have sent a request and the crc was OK,
891 		 * we can allow traffic again.
892 		 */
893 		spin_lock_bh(&bat_priv->bla.num_requests_lock);
894 		if (backbone_gw->state == BATADV_BLA_BACKBONE_GW_UNSYNCED) {
895 			backbone_gw->state = BATADV_BLA_BACKBONE_GW_SYNCED;
896 			atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
897 		}
898 		spin_unlock_bh(&bat_priv->bla.num_requests_lock);
899 	}
900 
901 	batadv_backbone_gw_put(backbone_gw);
902 	return true;
903 }
904 
905 /**
906  * batadv_handle_request() - check for REQUEST frame
907  * @bat_priv: the bat priv with all the mesh interface information
908  * @primary_if: the primary hard interface of this batman mesh interface
909  * @backbone_addr: backbone address to be requested (ARP sender HW MAC)
910  * @ethhdr: ethernet header of a packet
911  * @vid: the VLAN ID of the frame
912  *
913  * Return: true if handled
914  */
915 static bool batadv_handle_request(struct batadv_priv *bat_priv,
916 				  struct batadv_hard_iface *primary_if,
917 				  u8 *backbone_addr, struct ethhdr *ethhdr,
918 				  unsigned short vid)
919 {
920 	/* check for REQUEST frame */
921 	if (!batadv_compare_eth(backbone_addr, ethhdr->h_dest))
922 		return false;
923 
924 	/* sanity check, this should not happen on a normal switch,
925 	 * we ignore it in this case.
926 	 */
927 	if (!batadv_compare_eth(ethhdr->h_dest, primary_if->net_dev->dev_addr))
928 		return true;
929 
930 	batadv_dbg(BATADV_DBG_BLA, bat_priv,
931 		   "%s(): REQUEST vid %d (sent by %pM)...\n",
932 		   __func__, batadv_print_vid(vid), ethhdr->h_source);
933 
934 	batadv_bla_answer_request(bat_priv, primary_if, vid);
935 	return true;
936 }
937 
938 /**
939  * batadv_handle_unclaim() - check for UNCLAIM frame
940  * @bat_priv: the bat priv with all the mesh interface information
941  * @primary_if: the primary hard interface of this batman mesh interface
942  * @backbone_addr: originator address of the backbone (Ethernet source)
943  * @claim_addr: Client to be unclaimed (ARP sender HW MAC)
944  * @vid: the VLAN ID of the frame
945  *
946  * Return: true if handled
947  */
948 static bool batadv_handle_unclaim(struct batadv_priv *bat_priv,
949 				  struct batadv_hard_iface *primary_if,
950 				  const u8 *backbone_addr, const u8 *claim_addr,
951 				  unsigned short vid)
952 {
953 	/* unclaim in any case if it is our own */
954 	if (primary_if && batadv_compare_eth(backbone_addr,
955 					     primary_if->net_dev->dev_addr))
956 		batadv_bla_send_claim(bat_priv, claim_addr, vid,
957 				      BATADV_CLAIM_TYPE_UNCLAIM);
958 
959 	/* this must be an UNCLAIM frame */
960 	batadv_dbg(BATADV_DBG_BLA, bat_priv,
961 		   "%s(): UNCLAIM %pM on vid %d (sent by %pM)...\n", __func__,
962 		   claim_addr, batadv_print_vid(vid), backbone_addr);
963 
964 	batadv_bla_del_claim(bat_priv, claim_addr, vid);
965 	return true;
966 }
967 
968 /**
969  * batadv_handle_claim() - check for CLAIM frame
970  * @bat_priv: the bat priv with all the mesh interface information
971  * @primary_if: the primary hard interface of this batman mesh interface
972  * @backbone_addr: originator address of the backbone (Ethernet Source)
973  * @claim_addr: client mac address to be claimed (ARP sender HW MAC)
974  * @vid: the VLAN ID of the frame
975  *
976  * Return: true if handled
977  */
978 static bool batadv_handle_claim(struct batadv_priv *bat_priv,
979 				struct batadv_hard_iface *primary_if,
980 				const u8 *backbone_addr, const u8 *claim_addr,
981 				unsigned short vid)
982 {
983 	struct batadv_bla_backbone_gw *backbone_gw;
984 
985 	/* register the gateway if not yet available, and add the claim. */
986 
987 	backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
988 						 false);
989 
990 	if (unlikely(!backbone_gw))
991 		return true;
992 
993 	/* this must be a CLAIM frame */
994 	batadv_bla_add_claim(bat_priv, claim_addr, vid, backbone_gw);
995 	if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
996 		batadv_bla_send_claim(bat_priv, claim_addr, vid,
997 				      BATADV_CLAIM_TYPE_CLAIM);
998 
999 	/* TODO: we could call something like tt_local_del() here. */
1000 
1001 	batadv_backbone_gw_put(backbone_gw);
1002 	return true;
1003 }
1004 
1005 /**
1006  * batadv_check_claim_group() - check for claim group membership
1007  * @bat_priv: the bat priv with all the mesh interface information
1008  * @primary_if: the primary interface of this batman interface
1009  * @hw_src: the Hardware source in the ARP Header
1010  * @hw_dst: the Hardware destination in the ARP Header
1011  * @ethhdr: pointer to the Ethernet header of the claim frame
1012  *
1013  * checks if it is a claim packet and if it's on the same group.
1014  * This function also applies the group ID of the sender
1015  * if it is in the same mesh.
1016  *
1017  * Return:
1018  *	2  - if it is a claim packet and on the same group
1019  *	1  - if is a claim packet from another group
1020  *	0  - if it is not a claim packet
1021  */
1022 static int batadv_check_claim_group(struct batadv_priv *bat_priv,
1023 				    struct batadv_hard_iface *primary_if,
1024 				    u8 *hw_src, u8 *hw_dst,
1025 				    struct ethhdr *ethhdr)
1026 {
1027 	struct batadv_bla_claim_dst *bla_dst_own;
1028 	struct batadv_bla_claim_dst *bla_dst;
1029 	struct batadv_orig_node *orig_node;
1030 	u8 *backbone_addr;
1031 
1032 	bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
1033 	bla_dst_own = &bat_priv->bla.claim_dest;
1034 
1035 	/* if announcement packet, use the source,
1036 	 * otherwise assume it is in the hw_src
1037 	 */
1038 	switch (bla_dst->type) {
1039 	case BATADV_CLAIM_TYPE_CLAIM:
1040 		backbone_addr = hw_src;
1041 		break;
1042 	case BATADV_CLAIM_TYPE_REQUEST:
1043 	case BATADV_CLAIM_TYPE_ANNOUNCE:
1044 	case BATADV_CLAIM_TYPE_UNCLAIM:
1045 		backbone_addr = ethhdr->h_source;
1046 		break;
1047 	default:
1048 		return 0;
1049 	}
1050 
1051 	/* don't accept claim frames from ourselves */
1052 	if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
1053 		return 0;
1054 
1055 	/* if its already the same group, it is fine. */
1056 	if (bla_dst->group == bla_dst_own->group)
1057 		return 2;
1058 
1059 	/* lets see if this originator is in our mesh */
1060 	orig_node = batadv_orig_hash_find(bat_priv, backbone_addr);
1061 
1062 	/* don't accept claims from gateways which are not in
1063 	 * the same mesh or group.
1064 	 */
1065 	if (!orig_node)
1066 		return 1;
1067 
1068 	/* if our mesh friends mac is bigger, use it for ourselves. */
1069 	if (ntohs(bla_dst->group) > ntohs(bla_dst_own->group)) {
1070 		batadv_dbg(BATADV_DBG_BLA, bat_priv,
1071 			   "taking other backbones claim group: %#.4x\n",
1072 			   ntohs(bla_dst->group));
1073 		bla_dst_own->group = bla_dst->group;
1074 	}
1075 
1076 	batadv_orig_node_put(orig_node);
1077 
1078 	return 2;
1079 }
1080 
1081 /**
1082  * batadv_bla_process_claim() - Check if this is a claim frame, and process it
1083  * @bat_priv: the bat priv with all the mesh interface information
1084  * @primary_if: the primary hard interface of this batman mesh interface
1085  * @skb: the frame to be checked
1086  *
1087  * Warning: This function may reallocate the skb data buffer via
1088  * batadv_get_vid()/... Any pointer into the skb data (e.g. obtained
1089  * from skb->data or eth_hdr()) before this call must be considered
1090  * invalid afterwards and has to be reacquired.
1091  *
1092  * Return: true if it was a claim frame, otherwise return false to
1093  * tell the callee that it can use the frame on its own.
1094  */
1095 static bool batadv_bla_process_claim(struct batadv_priv *bat_priv,
1096 				     struct batadv_hard_iface *primary_if,
1097 				     struct sk_buff *skb)
1098 {
1099 	struct batadv_bla_claim_dst *bla_dst_own;
1100 	struct batadv_bla_claim_dst *bla_dst;
1101 	struct vlan_hdr vhdr_buf;
1102 	struct vlan_hdr *vhdr;
1103 	struct ethhdr *ethhdr;
1104 	struct arphdr *arphdr;
1105 	unsigned short vid;
1106 	int vlan_depth = 0;
1107 	__be16 proto;
1108 	int headlen;
1109 	u8 *hw_src;
1110 	u8 *hw_dst;
1111 	int ret;
1112 
1113 	vid = batadv_get_vid(skb, 0);
1114 	ethhdr = eth_hdr(skb);
1115 
1116 	proto = ethhdr->h_proto;
1117 	headlen = ETH_HLEN;
1118 	if (vid & BATADV_VLAN_HAS_TAG) {
1119 		/* Traverse the VLAN/Ethertypes.
1120 		 *
1121 		 * At this point it is known that the first protocol is a VLAN
1122 		 * header, so start checking at the encapsulated protocol.
1123 		 *
1124 		 * The depth of the VLAN headers is recorded to drop BLA claim
1125 		 * frames encapsulated into multiple VLAN headers (QinQ).
1126 		 */
1127 		do {
1128 			vhdr = skb_header_pointer(skb, headlen, VLAN_HLEN,
1129 						  &vhdr_buf);
1130 			if (!vhdr)
1131 				return false;
1132 
1133 			proto = vhdr->h_vlan_encapsulated_proto;
1134 			headlen += VLAN_HLEN;
1135 			vlan_depth++;
1136 		} while (proto == htons(ETH_P_8021Q));
1137 	}
1138 
1139 	if (proto != htons(ETH_P_ARP))
1140 		return false; /* not a claim frame */
1141 
1142 	/* this must be a ARP frame. check if it is a claim. */
1143 
1144 	if (unlikely(!pskb_may_pull(skb, headlen + arp_hdr_len(skb->dev))))
1145 		return false;
1146 
1147 	/* pskb_may_pull() may have modified the pointers, get ethhdr again */
1148 	ethhdr = eth_hdr(skb);
1149 	arphdr = (struct arphdr *)((u8 *)ethhdr + headlen);
1150 
1151 	/* Check whether the ARP frame carries a valid
1152 	 * IP information
1153 	 */
1154 	if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
1155 		return false;
1156 	if (arphdr->ar_pro != htons(ETH_P_IP))
1157 		return false;
1158 	if (arphdr->ar_hln != ETH_ALEN)
1159 		return false;
1160 	if (arphdr->ar_pln != 4)
1161 		return false;
1162 
1163 	hw_src = (u8 *)arphdr + sizeof(struct arphdr);
1164 	hw_dst = hw_src + ETH_ALEN + 4;
1165 	bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
1166 	bla_dst_own = &bat_priv->bla.claim_dest;
1167 
1168 	/* check if it is a claim frame in general */
1169 	if (memcmp(bla_dst->magic, bla_dst_own->magic,
1170 		   sizeof(bla_dst->magic)) != 0)
1171 		return false;
1172 
1173 	/* check if there is a claim frame encapsulated deeper in (QinQ) and
1174 	 * drop that, as this is not supported by BLA but should also not be
1175 	 * sent via the mesh.
1176 	 */
1177 	if (vlan_depth > 1)
1178 		return true;
1179 
1180 	/* Let the loopdetect frames on the mesh in any case. */
1181 	if (bla_dst->type == BATADV_CLAIM_TYPE_LOOPDETECT)
1182 		return false;
1183 
1184 	/* check if it is a claim frame. */
1185 	ret = batadv_check_claim_group(bat_priv, primary_if, hw_src, hw_dst,
1186 				       ethhdr);
1187 	if (ret == 1)
1188 		batadv_dbg(BATADV_DBG_BLA, bat_priv,
1189 			   "%s(): received a claim frame from another group. From: %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n",
1190 			   __func__, ethhdr->h_source, batadv_print_vid(vid),
1191 			   hw_src, hw_dst);
1192 
1193 	if (ret < 2)
1194 		return !!ret;
1195 
1196 	/* become a backbone gw ourselves on this vlan if not happened yet */
1197 	batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
1198 
1199 	/* check for the different types of claim frames ... */
1200 	switch (bla_dst->type) {
1201 	case BATADV_CLAIM_TYPE_CLAIM:
1202 		if (batadv_handle_claim(bat_priv, primary_if, hw_src,
1203 					ethhdr->h_source, vid))
1204 			return true;
1205 		break;
1206 	case BATADV_CLAIM_TYPE_UNCLAIM:
1207 		if (batadv_handle_unclaim(bat_priv, primary_if,
1208 					  ethhdr->h_source, hw_src, vid))
1209 			return true;
1210 		break;
1211 
1212 	case BATADV_CLAIM_TYPE_ANNOUNCE:
1213 		if (batadv_handle_announce(bat_priv, hw_src, ethhdr->h_source,
1214 					   vid))
1215 			return true;
1216 		break;
1217 	case BATADV_CLAIM_TYPE_REQUEST:
1218 		if (batadv_handle_request(bat_priv, primary_if, hw_src, ethhdr,
1219 					  vid))
1220 			return true;
1221 		break;
1222 	}
1223 
1224 	batadv_dbg(BATADV_DBG_BLA, bat_priv,
1225 		   "%s(): ERROR - this looks like a claim frame, but is useless. eth src %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n",
1226 		   __func__, ethhdr->h_source, batadv_print_vid(vid), hw_src,
1227 		   hw_dst);
1228 	return true;
1229 }
1230 
1231 /**
1232  * batadv_bla_purge_backbone_gw() - Remove backbone gateways after a timeout or
1233  *  immediately
1234  * @bat_priv: the bat priv with all the mesh interface information
1235  * @now: whether the whole hash shall be wiped now
1236  *
1237  * Check when we last heard from other nodes, and remove them in case of
1238  * a time out, or clean all backbone gws if now is set.
1239  */
1240 static void batadv_bla_purge_backbone_gw(struct batadv_priv *bat_priv, int now)
1241 {
1242 	spinlock_t *list_lock;	/* protects write access to the hash lists */
1243 	struct batadv_bla_backbone_gw *backbone_gw;
1244 	struct batadv_hashtable *hash;
1245 	struct hlist_node *node_tmp;
1246 	struct hlist_head *head;
1247 	bool purged;
1248 	int i;
1249 
1250 	hash = bat_priv->bla.backbone_hash;
1251 	if (!hash)
1252 		return;
1253 
1254 	for (i = 0; i < hash->size; i++) {
1255 		head = &hash->table[i];
1256 		list_lock = &hash->list_locks[i];
1257 
1258 		do {
1259 			purged = false;
1260 
1261 			spin_lock_bh(list_lock);
1262 			hlist_for_each_entry_safe(backbone_gw, node_tmp,
1263 						  head, hash_entry) {
1264 				if (now)
1265 					goto purge_now;
1266 				if (!batadv_has_timed_out(READ_ONCE(backbone_gw->lasttime),
1267 							  BATADV_BLA_BACKBONE_TIMEOUT))
1268 					continue;
1269 
1270 				batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
1271 					   "%s(): backbone gw %pM timed out\n",
1272 					   __func__, backbone_gw->orig);
1273 
1274 purge_now:
1275 				purged = true;
1276 
1277 				/* don't wait for the pending request anymore */
1278 				spin_lock_bh(&bat_priv->bla.num_requests_lock);
1279 				if (backbone_gw->state == BATADV_BLA_BACKBONE_GW_UNSYNCED)
1280 					atomic_dec(&bat_priv->bla.num_requests);
1281 
1282 				backbone_gw->state = BATADV_BLA_BACKBONE_GW_STOPPED;
1283 				spin_unlock_bh(&bat_priv->bla.num_requests_lock);
1284 
1285 				batadv_bla_del_backbone_claims(backbone_gw);
1286 
1287 				hlist_del_rcu(&backbone_gw->hash_entry);
1288 				break;
1289 			}
1290 			spin_unlock_bh(list_lock);
1291 
1292 			if (purged) {
1293 				/* reference for pending report_work */
1294 				if (disable_work_sync(&backbone_gw->report_work))
1295 					batadv_backbone_gw_put(backbone_gw);
1296 
1297 				/* reference for hash_entry */
1298 				batadv_backbone_gw_put(backbone_gw);
1299 			}
1300 		} while (purged);
1301 	}
1302 }
1303 
1304 /**
1305  * batadv_bla_purge_claims() - Remove claims after a timeout or immediately
1306  * @bat_priv: the bat priv with all the mesh interface information
1307  * @primary_if: the selected primary interface, may be NULL if now is set
1308  * @now: whether the whole hash shall be wiped now
1309  *
1310  * Check when we heard last time from our own claims, and remove them in case of
1311  * a time out, or clean all claims if now is set
1312  */
1313 static void batadv_bla_purge_claims(struct batadv_priv *bat_priv,
1314 				    struct batadv_hard_iface *primary_if,
1315 				    int now)
1316 {
1317 	struct batadv_bla_backbone_gw *backbone_gw;
1318 	struct batadv_bla_claim *claim;
1319 	struct batadv_hashtable *hash;
1320 	struct hlist_head *head;
1321 	int i;
1322 
1323 	hash = bat_priv->bla.claim_hash;
1324 	if (!hash)
1325 		return;
1326 
1327 	for (i = 0; i < hash->size; i++) {
1328 		head = &hash->table[i];
1329 
1330 		rcu_read_lock();
1331 		hlist_for_each_entry_rcu(claim, head, hash_entry) {
1332 			/* only purge claims not currently in the process of being released.
1333 			 * Such claims could otherwise have a NULL-ptr backbone_gw set because
1334 			 * they already went through batadv_claim_release()
1335 			 */
1336 			if (!kref_get_unless_zero(&claim->refcount))
1337 				continue;
1338 
1339 			backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
1340 			if (now)
1341 				goto purge_now;
1342 
1343 			if (!batadv_compare_eth(backbone_gw->orig,
1344 						primary_if->net_dev->dev_addr))
1345 				goto skip;
1346 
1347 			if (!batadv_has_timed_out(READ_ONCE(claim->lasttime),
1348 						  BATADV_BLA_CLAIM_TIMEOUT))
1349 				goto skip;
1350 
1351 			batadv_dbg(BATADV_DBG_BLA, bat_priv,
1352 				   "%s(): timed out.\n", __func__);
1353 
1354 purge_now:
1355 			batadv_dbg(BATADV_DBG_BLA, bat_priv,
1356 				   "%s(): %pM, vid %d\n", __func__,
1357 				   claim->addr, claim->vid);
1358 
1359 			batadv_handle_unclaim(bat_priv, primary_if,
1360 					      backbone_gw->orig,
1361 					      claim->addr, claim->vid);
1362 skip:
1363 			batadv_backbone_gw_put(backbone_gw);
1364 			batadv_claim_put(claim);
1365 		}
1366 		rcu_read_unlock();
1367 	}
1368 }
1369 
1370 /**
1371  * batadv_bla_update_orig_address() - Update the backbone gateways when the own
1372  *  originator address changes
1373  * @bat_priv: the bat priv with all the mesh interface information
1374  * @primary_if: the new selected primary_if
1375  * @oldif: the old primary interface, may be NULL
1376  */
1377 void batadv_bla_update_orig_address(struct batadv_priv *bat_priv,
1378 				    struct batadv_hard_iface *primary_if,
1379 				    struct batadv_hard_iface *oldif)
1380 {
1381 	struct batadv_bla_backbone_gw *backbone_gw;
1382 	struct batadv_hashtable *hash;
1383 	struct hlist_head *head;
1384 	__be16 group;
1385 	int i;
1386 
1387 	/* reset bridge loop avoidance group id */
1388 	group = htons(crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN));
1389 	bat_priv->bla.claim_dest.group = group;
1390 
1391 	/* purge everything when bridge loop avoidance is turned off */
1392 	if (!READ_ONCE(bat_priv->bridge_loop_avoidance))
1393 		oldif = NULL;
1394 
1395 	if (!oldif) {
1396 		batadv_bla_purge_claims(bat_priv, NULL, 1);
1397 		batadv_bla_purge_backbone_gw(bat_priv, 1);
1398 		return;
1399 	}
1400 
1401 	hash = bat_priv->bla.backbone_hash;
1402 	if (!hash)
1403 		return;
1404 
1405 	for (i = 0; i < hash->size; i++) {
1406 		head = &hash->table[i];
1407 
1408 		rcu_read_lock();
1409 		hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
1410 			/* own orig still holds the old value. */
1411 			if (!batadv_compare_eth(backbone_gw->orig,
1412 						oldif->net_dev->dev_addr))
1413 				continue;
1414 
1415 			ether_addr_copy(backbone_gw->orig,
1416 					primary_if->net_dev->dev_addr);
1417 			/* send an announce frame so others will ask for our
1418 			 * claims and update their tables.
1419 			 */
1420 			batadv_bla_send_announce(bat_priv, backbone_gw);
1421 		}
1422 		rcu_read_unlock();
1423 	}
1424 }
1425 
1426 /**
1427  * batadv_bla_send_loopdetect() - send a loopdetect frame
1428  * @bat_priv: the bat priv with all the mesh interface information
1429  * @backbone_gw: the backbone gateway for which a loop should be detected
1430  *
1431  * To detect loops that the bridge loop avoidance can't handle, send a loop
1432  * detection packet on the backbone. Unlike other BLA frames, this frame will
1433  * be allowed on the mesh by other nodes. If it is received on the mesh, this
1434  * indicates that there is a loop.
1435  */
1436 static void
1437 batadv_bla_send_loopdetect(struct batadv_priv *bat_priv,
1438 			   struct batadv_bla_backbone_gw *backbone_gw)
1439 {
1440 	batadv_dbg(BATADV_DBG_BLA, bat_priv, "Send loopdetect frame for vid %d\n",
1441 		   backbone_gw->vid);
1442 	batadv_bla_send_claim(bat_priv, bat_priv->bla.loopdetect_addr,
1443 			      backbone_gw->vid, BATADV_CLAIM_TYPE_LOOPDETECT);
1444 }
1445 
1446 /**
1447  * batadv_bla_status_update() - purge bla interfaces if necessary
1448  * @net_dev: the mesh interface net device
1449  */
1450 void batadv_bla_status_update(struct net_device *net_dev)
1451 {
1452 	struct batadv_priv *bat_priv = netdev_priv(net_dev);
1453 	struct batadv_hard_iface *primary_if;
1454 
1455 	primary_if = batadv_primary_if_get_selected(bat_priv);
1456 	if (!primary_if)
1457 		return;
1458 
1459 	/* this function already purges everything when bla is disabled,
1460 	 * so just call that one.
1461 	 */
1462 	batadv_bla_update_orig_address(bat_priv, primary_if, primary_if);
1463 	batadv_hardif_put(primary_if);
1464 }
1465 
1466 /**
1467  * batadv_bla_periodic_work() - performs periodic bla work
1468  * @work: kernel work struct
1469  *
1470  * periodic work to do:
1471  *  * purge structures when they are too old
1472  *  * send announcements
1473  */
1474 static void batadv_bla_periodic_work(struct work_struct *work)
1475 {
1476 	struct batadv_bla_backbone_gw *backbone_gw;
1477 	struct batadv_hard_iface *primary_if;
1478 	struct delayed_work *delayed_work;
1479 	struct batadv_priv_bla *priv_bla;
1480 	struct batadv_hashtable *hash;
1481 	struct batadv_priv *bat_priv;
1482 	bool send_loopdetect = false;
1483 	struct hlist_head *head;
1484 	int i;
1485 
1486 	delayed_work = to_delayed_work(work);
1487 	priv_bla = container_of(delayed_work, struct batadv_priv_bla, work);
1488 	bat_priv = container_of(priv_bla, struct batadv_priv, bla);
1489 	primary_if = batadv_primary_if_get_selected(bat_priv);
1490 	if (!primary_if)
1491 		goto out;
1492 
1493 	batadv_bla_purge_claims(bat_priv, primary_if, 0);
1494 	batadv_bla_purge_backbone_gw(bat_priv, 0);
1495 
1496 	if (!READ_ONCE(bat_priv->bridge_loop_avoidance))
1497 		goto out;
1498 
1499 	if (atomic_dec_and_test(&bat_priv->bla.loopdetect_next)) {
1500 		/* set a new random mac address for the next bridge loop
1501 		 * detection frames. Set the locally administered bit to avoid
1502 		 * collisions with users mac addresses.
1503 		 */
1504 		eth_random_addr(bat_priv->bla.loopdetect_addr);
1505 		bat_priv->bla.loopdetect_addr[0] = 0xba;
1506 		bat_priv->bla.loopdetect_addr[1] = 0xbe;
1507 		WRITE_ONCE(bat_priv->bla.loopdetect_lasttime, jiffies);
1508 		atomic_set(&bat_priv->bla.loopdetect_next,
1509 			   BATADV_BLA_LOOPDETECT_PERIODS);
1510 
1511 		/* mark for sending loop detect on all VLANs */
1512 		send_loopdetect = true;
1513 	}
1514 
1515 	hash = bat_priv->bla.backbone_hash;
1516 	if (!hash)
1517 		goto out;
1518 
1519 	for (i = 0; i < hash->size; i++) {
1520 		head = &hash->table[i];
1521 
1522 		rcu_read_lock();
1523 		hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
1524 			if (!batadv_compare_eth(backbone_gw->orig,
1525 						primary_if->net_dev->dev_addr))
1526 				continue;
1527 
1528 			WRITE_ONCE(backbone_gw->lasttime, jiffies);
1529 
1530 			batadv_bla_send_announce(bat_priv, backbone_gw);
1531 			if (send_loopdetect)
1532 				batadv_bla_send_loopdetect(bat_priv,
1533 							   backbone_gw);
1534 
1535 			/* state is only set to unsynced after creation to avoid
1536 			 * problems when we are not yet known as backbone gw
1537 			 * in the backbone.
1538 			 *
1539 			 * We can reset this now after we waited some periods
1540 			 * to give bridge forward delays and bla group forming
1541 			 * some grace time.
1542 			 */
1543 
1544 			spin_lock_bh(&bat_priv->bla.num_requests_lock);
1545 			if (backbone_gw->state != BATADV_BLA_BACKBONE_GW_UNSYNCED)
1546 				goto unlock_next;
1547 
1548 			if (backbone_gw->wait_periods > 0)
1549 				backbone_gw->wait_periods--;
1550 
1551 			if (backbone_gw->wait_periods > 0)
1552 				goto unlock_next;
1553 
1554 			backbone_gw->state = BATADV_BLA_BACKBONE_GW_SYNCED;
1555 			atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
1556 
1557 unlock_next:
1558 			spin_unlock_bh(&bat_priv->bla.num_requests_lock);
1559 		}
1560 		rcu_read_unlock();
1561 	}
1562 out:
1563 	batadv_hardif_put(primary_if);
1564 
1565 	queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1566 			   msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
1567 }
1568 
1569 /* The hash for claim and backbone hash receive the same key because they
1570  * are getting initialized by hash_new with the same key. Reinitializing
1571  * them with to different keys to allow nested locking without generating
1572  * lockdep warnings
1573  */
1574 static struct lock_class_key batadv_claim_hash_lock_class_key;
1575 static struct lock_class_key batadv_backbone_hash_lock_class_key;
1576 
1577 /**
1578  * batadv_bla_init() - initialize all bla structures
1579  * @bat_priv: the bat priv with all the mesh interface information
1580  *
1581  * Return: 0 on success, < 0 on error.
1582  */
1583 int batadv_bla_init(struct batadv_priv *bat_priv)
1584 {
1585 	static const u8 claim_dest[ETH_ALEN] __aligned(2) = {
1586 		0xff, 0x43, 0x05, 0x00, 0x00, 0x00
1587 	};
1588 	struct batadv_hard_iface *primary_if;
1589 	unsigned long entrytime;
1590 	u16 crc;
1591 	int i;
1592 
1593 	spin_lock_init(&bat_priv->bla.bcast_duplist_lock);
1594 
1595 	batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hash registering\n");
1596 
1597 	/* setting claim destination address */
1598 	memcpy(&bat_priv->bla.claim_dest.magic, claim_dest, 3);
1599 	bat_priv->bla.claim_dest.type = 0;
1600 	primary_if = batadv_primary_if_get_selected(bat_priv);
1601 	if (primary_if) {
1602 		crc = crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN);
1603 		bat_priv->bla.claim_dest.group = htons(crc);
1604 		batadv_hardif_put(primary_if);
1605 	} else {
1606 		bat_priv->bla.claim_dest.group = 0; /* will be set later */
1607 	}
1608 
1609 	/* initialize the duplicate list */
1610 	entrytime = jiffies - msecs_to_jiffies(BATADV_DUPLIST_TIMEOUT);
1611 	for (i = 0; i < BATADV_DUPLIST_SIZE; i++)
1612 		bat_priv->bla.bcast_duplist[i].entrytime = entrytime;
1613 	bat_priv->bla.bcast_duplist_curr = 0;
1614 
1615 	atomic_set(&bat_priv->bla.loopdetect_next,
1616 		   BATADV_BLA_LOOPDETECT_PERIODS);
1617 
1618 	if (bat_priv->bla.claim_hash)
1619 		return 0;
1620 
1621 	bat_priv->bla.claim_hash = batadv_hash_new(128);
1622 	if (!bat_priv->bla.claim_hash)
1623 		return -ENOMEM;
1624 
1625 	bat_priv->bla.backbone_hash = batadv_hash_new(32);
1626 	if (!bat_priv->bla.backbone_hash) {
1627 		batadv_hash_destroy(bat_priv->bla.claim_hash);
1628 		bat_priv->bla.claim_hash = NULL;
1629 		return -ENOMEM;
1630 	}
1631 
1632 	batadv_hash_set_lock_class(bat_priv->bla.claim_hash,
1633 				   &batadv_claim_hash_lock_class_key);
1634 	batadv_hash_set_lock_class(bat_priv->bla.backbone_hash,
1635 				   &batadv_backbone_hash_lock_class_key);
1636 
1637 	batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hashes initialized\n");
1638 
1639 	INIT_DELAYED_WORK(&bat_priv->bla.work, batadv_bla_periodic_work);
1640 
1641 	queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1642 			   msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
1643 	return 0;
1644 }
1645 
1646 /**
1647  * batadv_bla_check_duplist() - Check if a frame is in the broadcast dup.
1648  * @bat_priv: the bat priv with all the mesh interface information
1649  * @skb: contains the multicast packet to be checked
1650  * @payload_offset: offset in the skb, marking the start of the data to be CRC'ed
1651  * @orig: originator mac address, NULL if unknown
1652  *
1653  * Check if it is on our broadcast list. Another gateway might have sent the
1654  * same packet because it is connected to the same backbone, so we have to
1655  * remove this duplicate.
1656  *
1657  * This is performed by checking the CRC, which will tell us
1658  * with a good chance that it is the same packet. If it is furthermore
1659  * sent by another host, drop it. We allow equal packets from
1660  * the same host however as this might be intended.
1661  *
1662  * Return: true if a packet is in the duplicate list, false otherwise.
1663  */
1664 static bool batadv_bla_check_duplist(struct batadv_priv *bat_priv,
1665 				     struct sk_buff *skb, int payload_offset,
1666 				     const u8 *orig)
1667 {
1668 	struct batadv_bcast_duplist_entry *entry;
1669 	bool ret = false;
1670 	int payload_len;
1671 	int curr;
1672 	u32 crc;
1673 	int i;
1674 
1675 	/* calculate the crc ... */
1676 	payload_len = skb->len - payload_offset;
1677 	crc = skb_crc32c(skb, payload_offset, payload_len, 0);
1678 
1679 	spin_lock_bh(&bat_priv->bla.bcast_duplist_lock);
1680 
1681 	for (i = 0; i < BATADV_DUPLIST_SIZE; i++) {
1682 		curr = (bat_priv->bla.bcast_duplist_curr + i);
1683 		curr %= BATADV_DUPLIST_SIZE;
1684 		entry = &bat_priv->bla.bcast_duplist[curr];
1685 
1686 		/* we can stop searching if the entry is too old ;
1687 		 * later entries will be even older
1688 		 */
1689 		if (batadv_has_timed_out(entry->entrytime,
1690 					 BATADV_DUPLIST_TIMEOUT))
1691 			break;
1692 
1693 		if (entry->crc != crc)
1694 			continue;
1695 
1696 		/* are the originators both known and not anonymous? */
1697 		if (orig && !is_zero_ether_addr(orig) &&
1698 		    !is_zero_ether_addr(entry->orig)) {
1699 			/* If known, check if the new frame came from
1700 			 * the same originator:
1701 			 * We are safe to take identical frames from the
1702 			 * same orig, if known, as multiplications in
1703 			 * the mesh are detected via the (orig, seqno) pair.
1704 			 * So we can be a bit more liberal here and allow
1705 			 * identical frames from the same orig which the source
1706 			 * host might have sent multiple times on purpose.
1707 			 */
1708 			if (batadv_compare_eth(entry->orig, orig))
1709 				continue;
1710 		}
1711 
1712 		/* this entry seems to match: same crc, not too old,
1713 		 * and from another gw. therefore return true to forbid it.
1714 		 */
1715 		ret = true;
1716 		goto out;
1717 	}
1718 	/* not found, add a new entry (overwrite the oldest entry)
1719 	 * and allow it, its the first occurrence.
1720 	 */
1721 	curr = (bat_priv->bla.bcast_duplist_curr + BATADV_DUPLIST_SIZE - 1);
1722 	curr %= BATADV_DUPLIST_SIZE;
1723 	entry = &bat_priv->bla.bcast_duplist[curr];
1724 	entry->crc = crc;
1725 	entry->entrytime = jiffies;
1726 
1727 	/* known originator */
1728 	if (orig)
1729 		ether_addr_copy(entry->orig, orig);
1730 	/* anonymous originator */
1731 	else
1732 		eth_zero_addr(entry->orig);
1733 
1734 	bat_priv->bla.bcast_duplist_curr = curr;
1735 
1736 out:
1737 	spin_unlock_bh(&bat_priv->bla.bcast_duplist_lock);
1738 
1739 	return ret;
1740 }
1741 
1742 /**
1743  * batadv_bla_check_ucast_duplist() - Check if a frame is in the broadcast dup.
1744  * @bat_priv: the bat priv with all the mesh interface information
1745  * @skb: contains the multicast packet to be checked, decapsulated from a
1746  *  unicast_packet
1747  *
1748  * Check if it is on our broadcast list. Another gateway might have sent the
1749  * same packet because it is connected to the same backbone, so we have to
1750  * remove this duplicate.
1751  *
1752  * Return: true if a packet is in the duplicate list, false otherwise.
1753  */
1754 static bool batadv_bla_check_ucast_duplist(struct batadv_priv *bat_priv,
1755 					   struct sk_buff *skb)
1756 {
1757 	return batadv_bla_check_duplist(bat_priv, skb, 0, NULL);
1758 }
1759 
1760 /**
1761  * batadv_bla_check_bcast_duplist() - Check if a frame is in the broadcast dup.
1762  * @bat_priv: the bat priv with all the mesh interface information
1763  * @skb: contains the bcast_packet to be checked
1764  *
1765  * Check if it is on our broadcast list. Another gateway might have sent the
1766  * same packet because it is connected to the same backbone, so we have to
1767  * remove this duplicate.
1768  *
1769  * Return: true if a packet is in the duplicate list, false otherwise.
1770  */
1771 bool batadv_bla_check_bcast_duplist(struct batadv_priv *bat_priv,
1772 				    struct sk_buff *skb)
1773 {
1774 	struct batadv_bcast_packet *bcast_packet;
1775 
1776 	bcast_packet = (struct batadv_bcast_packet *)skb->data;
1777 
1778 	return batadv_bla_check_duplist(bat_priv, skb, sizeof(*bcast_packet),
1779 					bcast_packet->orig);
1780 }
1781 
1782 /**
1783  * batadv_bla_is_backbone_gw_orig() - Check if the originator is a gateway for
1784  *  the VLAN identified by vid.
1785  * @bat_priv: the bat priv with all the mesh interface information
1786  * @orig: originator mac address
1787  * @vid: VLAN identifier
1788  *
1789  * Return: true if orig is a backbone for this vid, false otherwise.
1790  */
1791 bool batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, u8 *orig,
1792 				    unsigned short vid)
1793 {
1794 	struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
1795 	struct batadv_bla_backbone_gw *backbone_gw;
1796 	struct hlist_head *head;
1797 	int i;
1798 
1799 	if (!READ_ONCE(bat_priv->bridge_loop_avoidance))
1800 		return false;
1801 
1802 	if (!hash)
1803 		return false;
1804 
1805 	for (i = 0; i < hash->size; i++) {
1806 		head = &hash->table[i];
1807 
1808 		rcu_read_lock();
1809 		hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
1810 			if (batadv_compare_eth(backbone_gw->orig, orig) &&
1811 			    backbone_gw->vid == vid) {
1812 				rcu_read_unlock();
1813 				return true;
1814 			}
1815 		}
1816 		rcu_read_unlock();
1817 	}
1818 
1819 	return false;
1820 }
1821 
1822 /**
1823  * batadv_bla_is_backbone_gw() - check if originator is a backbone gw for a VLAN
1824  * @skb: the frame to be checked
1825  * @orig_node: the orig_node of the frame
1826  * @hdr_size: maximum length of the frame
1827  *
1828  * Warning: This function may reallocate the skb data buffer via
1829  * pskb_may_pull()/batadv_get_vid()/... Any pointer into the skb data (e.g.
1830  * obtained from skb->data or eth_hdr()) before this call must be considered
1831  * invalid afterwards and has to be reacquired.
1832  *
1833  * Return: true if the orig_node is also a gateway on the mesh interface,
1834  * otherwise it returns false.
1835  */
1836 bool batadv_bla_is_backbone_gw(struct sk_buff *skb,
1837 			       struct batadv_orig_node *orig_node, int hdr_size)
1838 {
1839 	struct batadv_bla_backbone_gw *backbone_gw;
1840 	unsigned short vid;
1841 
1842 	if (!READ_ONCE(orig_node->bat_priv->bridge_loop_avoidance))
1843 		return false;
1844 
1845 	/* first, find out the vid. */
1846 	if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
1847 		return false;
1848 
1849 	vid = batadv_get_vid(skb, hdr_size);
1850 
1851 	/* see if this originator is a backbone gw for this VLAN */
1852 	backbone_gw = batadv_backbone_hash_find(orig_node->bat_priv,
1853 						orig_node->orig, vid);
1854 	if (!backbone_gw)
1855 		return false;
1856 
1857 	batadv_backbone_gw_put(backbone_gw);
1858 	return true;
1859 }
1860 
1861 /**
1862  * batadv_bla_free() - free all bla structures
1863  * @bat_priv: the bat priv with all the mesh interface information
1864  *
1865  * for meshinterface free or module unload
1866  */
1867 void batadv_bla_free(struct batadv_priv *bat_priv)
1868 {
1869 	struct batadv_hard_iface *primary_if;
1870 
1871 	disable_delayed_work_sync(&bat_priv->bla.work);
1872 	primary_if = batadv_primary_if_get_selected(bat_priv);
1873 
1874 	if (bat_priv->bla.claim_hash) {
1875 		batadv_bla_purge_claims(bat_priv, primary_if, 1);
1876 		batadv_hash_destroy(bat_priv->bla.claim_hash);
1877 		bat_priv->bla.claim_hash = NULL;
1878 	}
1879 	if (bat_priv->bla.backbone_hash) {
1880 		batadv_bla_purge_backbone_gw(bat_priv, 1);
1881 		batadv_hash_destroy(bat_priv->bla.backbone_hash);
1882 		bat_priv->bla.backbone_hash = NULL;
1883 	}
1884 	batadv_hardif_put(primary_if);
1885 }
1886 
1887 /**
1888  * batadv_bla_loopdetect_check() - check and handle a detected loop
1889  * @bat_priv: the bat priv with all the mesh interface information
1890  * @skb: the packet to check
1891  * @primary_if: interface where the request came on
1892  * @vid: the VLAN ID of the frame
1893  *
1894  * Checks if this packet is a loop detect frame which has been sent by us,
1895  * throws an uevent and logs the event if that is the case.
1896  *
1897  * Return: true if it is a loop detect frame which is to be dropped, false
1898  * otherwise.
1899  */
1900 static bool
1901 batadv_bla_loopdetect_check(struct batadv_priv *bat_priv, struct sk_buff *skb,
1902 			    struct batadv_hard_iface *primary_if,
1903 			    unsigned short vid)
1904 {
1905 	struct batadv_bla_backbone_gw *backbone_gw;
1906 	struct ethhdr *ethhdr;
1907 	bool ret;
1908 
1909 	ethhdr = eth_hdr(skb);
1910 
1911 	/* Only check for the MAC address and skip more checks here for
1912 	 * performance reasons - this function is on the hotpath, after all.
1913 	 */
1914 	if (!batadv_compare_eth(ethhdr->h_source,
1915 				bat_priv->bla.loopdetect_addr))
1916 		return false;
1917 
1918 	/* If the packet came too late, don't forward it on the mesh
1919 	 * but don't consider that as loop. It might be a coincidence.
1920 	 */
1921 	if (batadv_has_timed_out(READ_ONCE(bat_priv->bla.loopdetect_lasttime),
1922 				 BATADV_BLA_LOOPDETECT_TIMEOUT))
1923 		return true;
1924 
1925 	backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
1926 						 primary_if->net_dev->dev_addr,
1927 						 vid, true);
1928 	if (unlikely(!backbone_gw))
1929 		return true;
1930 
1931 	ret = queue_work(batadv_event_workqueue, &backbone_gw->report_work);
1932 
1933 	/* backbone_gw is unreferenced in the report work function
1934 	 * if queue_work() call was successful
1935 	 */
1936 	if (!ret)
1937 		batadv_backbone_gw_put(backbone_gw);
1938 
1939 	return true;
1940 }
1941 
1942 /**
1943  * batadv_bla_rx() - check packets coming from the mesh.
1944  * @bat_priv: the bat priv with all the mesh interface information
1945  * @skb: the frame to be checked
1946  * @vid: the VLAN ID of the frame
1947  * @packet_type: the batman packet type this frame came in
1948  *
1949  * batadv_bla_rx avoidance checks if:
1950  *  * we have to race for a claim
1951  *  * if the frame is allowed on the LAN
1952  *
1953  * In these cases, the skb is further handled by this function
1954  *
1955  * Return: true if handled, otherwise it returns false and the caller shall
1956  * further process the skb.
1957  */
1958 bool batadv_bla_rx(struct batadv_priv *bat_priv, struct sk_buff *skb,
1959 		   unsigned short vid, int packet_type)
1960 {
1961 	struct batadv_bla_backbone_gw *backbone_gw;
1962 	struct batadv_bla_claim *claim = NULL;
1963 	struct batadv_bla_claim search_claim;
1964 	struct batadv_hard_iface *primary_if;
1965 	struct ethhdr *ethhdr;
1966 	bool own_claim;
1967 	bool ret;
1968 
1969 	ethhdr = eth_hdr(skb);
1970 
1971 	primary_if = batadv_primary_if_get_selected(bat_priv);
1972 	if (!primary_if)
1973 		goto handled;
1974 
1975 	if (!READ_ONCE(bat_priv->bridge_loop_avoidance))
1976 		goto allow;
1977 
1978 	if (batadv_bla_loopdetect_check(bat_priv, skb, primary_if, vid))
1979 		goto handled;
1980 
1981 	if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
1982 		/* don't allow multicast packets while requests are in flight */
1983 		if (is_multicast_ether_addr(ethhdr->h_dest))
1984 			/* Both broadcast flooding or multicast-via-unicasts
1985 			 * delivery might send to multiple backbone gateways
1986 			 * sharing the same LAN and therefore need to coordinate
1987 			 * which backbone gateway forwards into the LAN,
1988 			 * by claiming the payload source address.
1989 			 *
1990 			 * Broadcast flooding and multicast-via-unicasts
1991 			 * delivery use the following two batman packet types.
1992 			 * Note: explicitly exclude BATADV_UNICAST_4ADDR,
1993 			 * as the DHCP gateway feature will send explicitly
1994 			 * to only one BLA gateway, so the claiming process
1995 			 * should be avoided there.
1996 			 */
1997 			if (packet_type == BATADV_BCAST ||
1998 			    packet_type == BATADV_UNICAST)
1999 				goto handled;
2000 
2001 	/* potential duplicates from foreign BLA backbone gateways via
2002 	 * multicast-in-unicast packets
2003 	 */
2004 	if (is_multicast_ether_addr(ethhdr->h_dest) &&
2005 	    packet_type == BATADV_UNICAST &&
2006 	    batadv_bla_check_ucast_duplist(bat_priv, skb))
2007 		goto handled;
2008 
2009 	ether_addr_copy(search_claim.addr, ethhdr->h_source);
2010 	search_claim.vid = vid;
2011 	claim = batadv_claim_hash_find(bat_priv, &search_claim);
2012 
2013 	if (!claim) {
2014 		bool local = batadv_is_my_client(bat_priv, ethhdr->h_source, vid);
2015 
2016 		/* possible optimization: race for a claim */
2017 		/* No claim exists yet, claim it for us!
2018 		 */
2019 
2020 		batadv_dbg(BATADV_DBG_BLA, bat_priv,
2021 			   "%s(): Unclaimed MAC %pM found. Claim it. Local: %s\n",
2022 			   __func__, ethhdr->h_source, str_yes_no(local));
2023 		batadv_handle_claim(bat_priv, primary_if,
2024 				    primary_if->net_dev->dev_addr,
2025 				    ethhdr->h_source, vid);
2026 		goto allow;
2027 	}
2028 
2029 	/* if it is our own claim ... */
2030 	backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
2031 	own_claim = batadv_compare_eth(backbone_gw->orig,
2032 				       primary_if->net_dev->dev_addr);
2033 	batadv_backbone_gw_put(backbone_gw);
2034 
2035 	if (own_claim) {
2036 		/* ... allow it in any case */
2037 		WRITE_ONCE(claim->lasttime, jiffies);
2038 		goto allow;
2039 	}
2040 
2041 	/* if it is a multicast ... */
2042 	if (is_multicast_ether_addr(ethhdr->h_dest) &&
2043 	    (packet_type == BATADV_BCAST || packet_type == BATADV_UNICAST)) {
2044 		/* ... drop it. the responsible gateway is in charge.
2045 		 *
2046 		 * We need to check packet type because with the gateway
2047 		 * feature, broadcasts (like DHCP requests) may be sent
2048 		 * using a unicast 4 address packet type. See comment above.
2049 		 */
2050 		goto handled;
2051 	} else {
2052 		/* seems the client considers us as its best gateway.
2053 		 * send a claim and update the claim table
2054 		 * immediately.
2055 		 */
2056 		batadv_handle_claim(bat_priv, primary_if,
2057 				    primary_if->net_dev->dev_addr,
2058 				    ethhdr->h_source, vid);
2059 		goto allow;
2060 	}
2061 allow:
2062 	batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
2063 	ret = false;
2064 	goto out;
2065 
2066 handled:
2067 	kfree_skb(skb);
2068 	ret = true;
2069 
2070 out:
2071 	batadv_hardif_put(primary_if);
2072 	batadv_claim_put(claim);
2073 	return ret;
2074 }
2075 
2076 /**
2077  * batadv_bla_tx() - check packets going into the mesh
2078  * @bat_priv: the bat priv with all the mesh interface information
2079  * @skb: the frame to be checked
2080  * @vid: the VLAN ID of the frame
2081  *
2082  * batadv_bla_tx checks if:
2083  *  * a claim was received which has to be processed
2084  *  * the frame is allowed on the mesh
2085  *
2086  * in these cases, the skb is further handled by this function.
2087  *
2088  * Warning: This function may reallocate the skb data buffer via
2089  * batadv_bla_process_claim()/... Any pointer into the skb data (e.g.
2090  * obtained from skb->data or eth_hdr()) before this call must be considered
2091  * invalid afterwards and has to be reacquired.
2092  *
2093  * Return: true if handled, otherwise it returns false and the caller shall
2094  * further process the skb.
2095  */
2096 bool batadv_bla_tx(struct batadv_priv *bat_priv, struct sk_buff *skb,
2097 		   unsigned short vid)
2098 {
2099 	struct batadv_bla_backbone_gw *backbone_gw;
2100 	struct batadv_bla_claim *claim = NULL;
2101 	struct batadv_bla_claim search_claim;
2102 	struct batadv_hard_iface *primary_if;
2103 	struct ethhdr *ethhdr;
2104 	bool client_roamed;
2105 	bool ret = false;
2106 
2107 	primary_if = batadv_primary_if_get_selected(bat_priv);
2108 	if (!primary_if)
2109 		goto out;
2110 
2111 	if (!READ_ONCE(bat_priv->bridge_loop_avoidance))
2112 		goto allow;
2113 
2114 	if (batadv_bla_process_claim(bat_priv, primary_if, skb))
2115 		goto handled;
2116 
2117 	ethhdr = eth_hdr(skb);
2118 
2119 	if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
2120 		/* don't allow broadcasts while requests are in flight */
2121 		if (is_multicast_ether_addr(ethhdr->h_dest))
2122 			goto handled;
2123 
2124 	ether_addr_copy(search_claim.addr, ethhdr->h_source);
2125 	search_claim.vid = vid;
2126 
2127 	claim = batadv_claim_hash_find(bat_priv, &search_claim);
2128 
2129 	/* if no claim exists, allow it. */
2130 	if (!claim)
2131 		goto allow;
2132 
2133 	/* check if we are responsible. */
2134 	backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
2135 	client_roamed = batadv_compare_eth(backbone_gw->orig,
2136 					   primary_if->net_dev->dev_addr);
2137 	batadv_backbone_gw_put(backbone_gw);
2138 
2139 	if (client_roamed) {
2140 		/* if yes, the client has roamed and we have
2141 		 * to unclaim it.
2142 		 */
2143 		if (batadv_has_timed_out(READ_ONCE(claim->lasttime), 100)) {
2144 			/* only unclaim if the last claim entry is
2145 			 * older than 100 ms to make sure we really
2146 			 * have a roaming client here.
2147 			 */
2148 			batadv_dbg(BATADV_DBG_BLA, bat_priv, "%s(): Roaming client %pM detected. Unclaim it.\n",
2149 				   __func__, ethhdr->h_source);
2150 			batadv_handle_unclaim(bat_priv, primary_if,
2151 					      primary_if->net_dev->dev_addr,
2152 					      ethhdr->h_source, vid);
2153 			goto allow;
2154 		} else {
2155 			batadv_dbg(BATADV_DBG_BLA, bat_priv, "%s(): Race for claim %pM detected. Drop packet.\n",
2156 				   __func__, ethhdr->h_source);
2157 			goto handled;
2158 		}
2159 	}
2160 
2161 	/* check if it is a multicast/broadcast frame */
2162 	if (is_multicast_ether_addr(ethhdr->h_dest)) {
2163 		/* drop it. the responsible gateway has forwarded it into
2164 		 * the backbone network.
2165 		 */
2166 		goto handled;
2167 	} else {
2168 		/* we must allow it. at least if we are
2169 		 * responsible for the DESTINATION.
2170 		 */
2171 		goto allow;
2172 	}
2173 allow:
2174 	batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
2175 	ret = false;
2176 	goto out;
2177 handled:
2178 	ret = true;
2179 out:
2180 	batadv_hardif_put(primary_if);
2181 	batadv_claim_put(claim);
2182 	return ret;
2183 }
2184 
2185 /**
2186  * batadv_bla_claim_dump_entry() - dump one entry of the claim table
2187  * to a netlink socket
2188  * @msg: buffer for the message
2189  * @portid: netlink port
2190  * @cb: Control block containing additional options
2191  * @primary_if: primary interface
2192  * @claim: entry to dump
2193  *
2194  * Return: 0 or error code.
2195  */
2196 static int
2197 batadv_bla_claim_dump_entry(struct sk_buff *msg, u32 portid,
2198 			    struct netlink_callback *cb,
2199 			    struct batadv_hard_iface *primary_if,
2200 			    struct batadv_bla_claim *claim)
2201 {
2202 	const u8 *primary_addr = primary_if->net_dev->dev_addr;
2203 	struct batadv_bla_backbone_gw *backbone_gw;
2204 	int ret = -EINVAL;
2205 	u16 backbone_crc;
2206 	bool is_own;
2207 	void *hdr;
2208 
2209 	hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
2210 			  &batadv_netlink_family, NLM_F_MULTI,
2211 			  BATADV_CMD_GET_BLA_CLAIM);
2212 	if (!hdr) {
2213 		ret = -ENOBUFS;
2214 		goto out;
2215 	}
2216 
2217 	genl_dump_check_consistent(cb, hdr);
2218 
2219 	backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
2220 
2221 	is_own = batadv_compare_eth(backbone_gw->orig, primary_addr);
2222 
2223 	spin_lock_bh(&backbone_gw->crc_lock);
2224 	backbone_crc = backbone_gw->crc;
2225 	spin_unlock_bh(&backbone_gw->crc_lock);
2226 
2227 	if (is_own)
2228 		if (nla_put_flag(msg, BATADV_ATTR_BLA_OWN)) {
2229 			genlmsg_cancel(msg, hdr);
2230 			goto put_backbone_gw;
2231 		}
2232 
2233 	if (nla_put(msg, BATADV_ATTR_BLA_ADDRESS, ETH_ALEN, claim->addr) ||
2234 	    nla_put_u16(msg, BATADV_ATTR_BLA_VID, claim->vid) ||
2235 	    nla_put(msg, BATADV_ATTR_BLA_BACKBONE, ETH_ALEN,
2236 		    backbone_gw->orig) ||
2237 	    nla_put_u16(msg, BATADV_ATTR_BLA_CRC,
2238 			backbone_crc)) {
2239 		genlmsg_cancel(msg, hdr);
2240 		goto put_backbone_gw;
2241 	}
2242 
2243 	genlmsg_end(msg, hdr);
2244 	ret = 0;
2245 
2246 put_backbone_gw:
2247 	batadv_backbone_gw_put(backbone_gw);
2248 out:
2249 	return ret;
2250 }
2251 
2252 /**
2253  * batadv_bla_claim_dump_bucket() - dump one bucket of the claim table
2254  * to a netlink socket
2255  * @msg: buffer for the message
2256  * @portid: netlink port
2257  * @cb: Control block containing additional options
2258  * @primary_if: primary interface
2259  * @hash: hash to dump
2260  * @bucket: bucket index to dump
2261  * @idx_skip: How many entries to skip
2262  *
2263  * Return: always 0.
2264  */
2265 static int
2266 batadv_bla_claim_dump_bucket(struct sk_buff *msg, u32 portid,
2267 			     struct netlink_callback *cb,
2268 			     struct batadv_hard_iface *primary_if,
2269 			     struct batadv_hashtable *hash, unsigned int bucket,
2270 			     int *idx_skip)
2271 {
2272 	struct batadv_bla_claim *claim;
2273 	int idx = 0;
2274 	int ret = 0;
2275 
2276 	spin_lock_bh(&hash->list_locks[bucket]);
2277 	cb->seq = atomic_read(&hash->generation) << 1 | 1;
2278 
2279 	hlist_for_each_entry(claim, &hash->table[bucket], hash_entry) {
2280 		if (idx++ < *idx_skip)
2281 			continue;
2282 
2283 		ret = batadv_bla_claim_dump_entry(msg, portid, cb,
2284 						  primary_if, claim);
2285 		if (ret) {
2286 			*idx_skip = idx - 1;
2287 			goto unlock;
2288 		}
2289 	}
2290 
2291 	*idx_skip = 0;
2292 unlock:
2293 	spin_unlock_bh(&hash->list_locks[bucket]);
2294 	return ret;
2295 }
2296 
2297 /**
2298  * batadv_bla_claim_dump() - dump claim table to a netlink socket
2299  * @msg: buffer for the message
2300  * @cb: callback structure containing arguments
2301  *
2302  * Return: message length.
2303  */
2304 int batadv_bla_claim_dump(struct sk_buff *msg, struct netlink_callback *cb)
2305 {
2306 	struct batadv_hard_iface *primary_if = NULL;
2307 	int portid = NETLINK_CB(cb->skb).portid;
2308 	struct net_device *mesh_iface;
2309 	struct batadv_hashtable *hash;
2310 	struct batadv_priv *bat_priv;
2311 	int bucket = cb->args[0];
2312 	int idx = cb->args[1];
2313 	int ret = 0;
2314 
2315 	mesh_iface = batadv_netlink_get_meshif(cb);
2316 	if (IS_ERR(mesh_iface))
2317 		return PTR_ERR(mesh_iface);
2318 
2319 	bat_priv = netdev_priv(mesh_iface);
2320 	hash = bat_priv->bla.claim_hash;
2321 
2322 	primary_if = batadv_primary_if_get_selected(bat_priv);
2323 	if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
2324 		ret = -ENOENT;
2325 		goto out;
2326 	}
2327 
2328 	while (bucket < hash->size) {
2329 		if (batadv_bla_claim_dump_bucket(msg, portid, cb, primary_if,
2330 						 hash, bucket, &idx))
2331 			break;
2332 		bucket++;
2333 	}
2334 
2335 	cb->args[0] = bucket;
2336 	cb->args[1] = idx;
2337 
2338 	ret = msg->len;
2339 
2340 out:
2341 	batadv_hardif_put(primary_if);
2342 
2343 	dev_put(mesh_iface);
2344 
2345 	return ret;
2346 }
2347 
2348 /**
2349  * batadv_bla_backbone_dump_entry() - dump one entry of the backbone table to a
2350  *  netlink socket
2351  * @msg: buffer for the message
2352  * @portid: netlink port
2353  * @cb: Control block containing additional options
2354  * @primary_if: primary interface
2355  * @backbone_gw: entry to dump
2356  *
2357  * Return: 0 or error code.
2358  */
2359 static int
2360 batadv_bla_backbone_dump_entry(struct sk_buff *msg, u32 portid,
2361 			       struct netlink_callback *cb,
2362 			       struct batadv_hard_iface *primary_if,
2363 			       struct batadv_bla_backbone_gw *backbone_gw)
2364 {
2365 	const u8 *primary_addr = primary_if->net_dev->dev_addr;
2366 	int ret = -EINVAL;
2367 	u16 backbone_crc;
2368 	bool is_own;
2369 	int msecs;
2370 	void *hdr;
2371 
2372 	hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
2373 			  &batadv_netlink_family, NLM_F_MULTI,
2374 			  BATADV_CMD_GET_BLA_BACKBONE);
2375 	if (!hdr) {
2376 		ret = -ENOBUFS;
2377 		goto out;
2378 	}
2379 
2380 	genl_dump_check_consistent(cb, hdr);
2381 
2382 	is_own = batadv_compare_eth(backbone_gw->orig, primary_addr);
2383 
2384 	spin_lock_bh(&backbone_gw->crc_lock);
2385 	backbone_crc = backbone_gw->crc;
2386 	spin_unlock_bh(&backbone_gw->crc_lock);
2387 
2388 	msecs = jiffies_to_msecs(jiffies - READ_ONCE(backbone_gw->lasttime));
2389 
2390 	if (is_own)
2391 		if (nla_put_flag(msg, BATADV_ATTR_BLA_OWN)) {
2392 			genlmsg_cancel(msg, hdr);
2393 			goto out;
2394 		}
2395 
2396 	if (nla_put(msg, BATADV_ATTR_BLA_BACKBONE, ETH_ALEN,
2397 		    backbone_gw->orig) ||
2398 	    nla_put_u16(msg, BATADV_ATTR_BLA_VID, backbone_gw->vid) ||
2399 	    nla_put_u16(msg, BATADV_ATTR_BLA_CRC,
2400 			backbone_crc) ||
2401 	    nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS, msecs)) {
2402 		genlmsg_cancel(msg, hdr);
2403 		goto out;
2404 	}
2405 
2406 	genlmsg_end(msg, hdr);
2407 	ret = 0;
2408 
2409 out:
2410 	return ret;
2411 }
2412 
2413 /**
2414  * batadv_bla_backbone_dump_bucket() - dump one bucket of the backbone table to
2415  *  a netlink socket
2416  * @msg: buffer for the message
2417  * @portid: netlink port
2418  * @cb: Control block containing additional options
2419  * @primary_if: primary interface
2420  * @hash: hash to dump
2421  * @bucket: bucket index to dump
2422  * @idx_skip: How many entries to skip
2423  *
2424  * Return: always 0.
2425  */
2426 static int
2427 batadv_bla_backbone_dump_bucket(struct sk_buff *msg, u32 portid,
2428 				struct netlink_callback *cb,
2429 				struct batadv_hard_iface *primary_if,
2430 				struct batadv_hashtable *hash,
2431 				unsigned int bucket, int *idx_skip)
2432 {
2433 	struct batadv_bla_backbone_gw *backbone_gw;
2434 	int idx = 0;
2435 	int ret = 0;
2436 
2437 	spin_lock_bh(&hash->list_locks[bucket]);
2438 	cb->seq = atomic_read(&hash->generation) << 1 | 1;
2439 
2440 	hlist_for_each_entry(backbone_gw, &hash->table[bucket], hash_entry) {
2441 		if (idx++ < *idx_skip)
2442 			continue;
2443 
2444 		ret = batadv_bla_backbone_dump_entry(msg, portid, cb,
2445 						     primary_if, backbone_gw);
2446 		if (ret) {
2447 			*idx_skip = idx - 1;
2448 			goto unlock;
2449 		}
2450 	}
2451 
2452 	*idx_skip = 0;
2453 unlock:
2454 	spin_unlock_bh(&hash->list_locks[bucket]);
2455 	return ret;
2456 }
2457 
2458 /**
2459  * batadv_bla_backbone_dump() - dump backbone table to a netlink socket
2460  * @msg: buffer for the message
2461  * @cb: callback structure containing arguments
2462  *
2463  * Return: message length.
2464  */
2465 int batadv_bla_backbone_dump(struct sk_buff *msg, struct netlink_callback *cb)
2466 {
2467 	struct batadv_hard_iface *primary_if = NULL;
2468 	int portid = NETLINK_CB(cb->skb).portid;
2469 	struct net_device *mesh_iface;
2470 	struct batadv_hashtable *hash;
2471 	struct batadv_priv *bat_priv;
2472 	int bucket = cb->args[0];
2473 	int idx = cb->args[1];
2474 	int ret = 0;
2475 
2476 	mesh_iface = batadv_netlink_get_meshif(cb);
2477 	if (IS_ERR(mesh_iface))
2478 		return PTR_ERR(mesh_iface);
2479 
2480 	bat_priv = netdev_priv(mesh_iface);
2481 	hash = bat_priv->bla.backbone_hash;
2482 
2483 	primary_if = batadv_primary_if_get_selected(bat_priv);
2484 	if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
2485 		ret = -ENOENT;
2486 		goto out;
2487 	}
2488 
2489 	while (bucket < hash->size) {
2490 		if (batadv_bla_backbone_dump_bucket(msg, portid, cb, primary_if,
2491 						    hash, bucket, &idx))
2492 			break;
2493 		bucket++;
2494 	}
2495 
2496 	cb->args[0] = bucket;
2497 	cb->args[1] = idx;
2498 
2499 	ret = msg->len;
2500 
2501 out:
2502 	batadv_hardif_put(primary_if);
2503 
2504 	dev_put(mesh_iface);
2505 
2506 	return ret;
2507 }
2508 
2509 #ifdef CONFIG_BATMAN_ADV_DAT
2510 /**
2511  * batadv_bla_check_claim() - check if address is claimed
2512  *
2513  * @bat_priv: the bat priv with all the mesh interface information
2514  * @addr: mac address of which the claim status is checked
2515  * @vid: the VLAN ID
2516  *
2517  * addr is checked if this address is claimed by the local device itself.
2518  *
2519  * Return: true if bla is disabled or the mac is claimed by the device,
2520  * false if the device addr is already claimed by another gateway
2521  */
2522 bool batadv_bla_check_claim(struct batadv_priv *bat_priv,
2523 			    u8 *addr, unsigned short vid)
2524 {
2525 	struct batadv_hard_iface *primary_if = NULL;
2526 	struct batadv_bla_backbone_gw *backbone_gw;
2527 	struct batadv_bla_claim *claim = NULL;
2528 	struct batadv_bla_claim search_claim;
2529 	bool ret = true;
2530 
2531 	if (!READ_ONCE(bat_priv->bridge_loop_avoidance))
2532 		return ret;
2533 
2534 	primary_if = batadv_primary_if_get_selected(bat_priv);
2535 	if (!primary_if)
2536 		return ret;
2537 
2538 	/* First look if the mac address is claimed */
2539 	ether_addr_copy(search_claim.addr, addr);
2540 	search_claim.vid = vid;
2541 
2542 	claim = batadv_claim_hash_find(bat_priv, &search_claim);
2543 
2544 	/* If there is a claim and we are not owner of the claim,
2545 	 * return false.
2546 	 */
2547 	if (claim) {
2548 		backbone_gw = batadv_bla_claim_get_backbone_gw(claim);
2549 
2550 		if (!batadv_compare_eth(backbone_gw->orig,
2551 					primary_if->net_dev->dev_addr))
2552 			ret = false;
2553 
2554 		batadv_backbone_gw_put(backbone_gw);
2555 		batadv_claim_put(claim);
2556 	}
2557 
2558 	batadv_hardif_put(primary_if);
2559 	return ret;
2560 }
2561 #endif
2562