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