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