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