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