1 /* Copyright (C) 2011-2014 B.A.T.M.A.N. contributors:
2  *
3  * Simon Wunderlich
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 2 of the GNU General Public
7  * License as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "main.h"
19 #include "hash.h"
20 #include "hard-interface.h"
21 #include "originator.h"
22 #include "bridge_loop_avoidance.h"
23 #include "translation-table.h"
24 #include "send.h"
25 
26 #include <linux/etherdevice.h>
27 #include <linux/crc16.h>
28 #include <linux/if_arp.h>
29 #include <net/arp.h>
30 #include <linux/if_vlan.h>
31 
32 static const uint8_t batadv_announce_mac[4] = {0x43, 0x05, 0x43, 0x05};
33 
34 static void batadv_bla_periodic_work(struct work_struct *work);
35 static void
36 batadv_bla_send_announce(struct batadv_priv *bat_priv,
37 			 struct batadv_bla_backbone_gw *backbone_gw);
38 
39 /* return the index of the claim */
40 static inline uint32_t batadv_choose_claim(const void *data, uint32_t size)
41 {
42 	struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
43 	uint32_t hash = 0;
44 
45 	hash = batadv_hash_bytes(hash, &claim->addr, sizeof(claim->addr));
46 	hash = batadv_hash_bytes(hash, &claim->vid, sizeof(claim->vid));
47 
48 	hash += (hash << 3);
49 	hash ^= (hash >> 11);
50 	hash += (hash << 15);
51 
52 	return hash % size;
53 }
54 
55 /* return the index of the backbone gateway */
56 static inline uint32_t batadv_choose_backbone_gw(const void *data,
57 						 uint32_t size)
58 {
59 	const struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
60 	uint32_t hash = 0;
61 
62 	hash = batadv_hash_bytes(hash, &claim->addr, sizeof(claim->addr));
63 	hash = batadv_hash_bytes(hash, &claim->vid, sizeof(claim->vid));
64 
65 	hash += (hash << 3);
66 	hash ^= (hash >> 11);
67 	hash += (hash << 15);
68 
69 	return hash % size;
70 }
71 
72 
73 /* compares address and vid of two backbone gws */
74 static int batadv_compare_backbone_gw(const struct hlist_node *node,
75 				      const void *data2)
76 {
77 	const void *data1 = container_of(node, struct batadv_bla_backbone_gw,
78 					 hash_entry);
79 	const struct batadv_bla_backbone_gw *gw1 = data1, *gw2 = data2;
80 
81 	if (!batadv_compare_eth(gw1->orig, gw2->orig))
82 		return 0;
83 
84 	if (gw1->vid != gw2->vid)
85 		return 0;
86 
87 	return 1;
88 }
89 
90 /* compares address and vid of two claims */
91 static int batadv_compare_claim(const struct hlist_node *node,
92 				const void *data2)
93 {
94 	const void *data1 = container_of(node, struct batadv_bla_claim,
95 					 hash_entry);
96 	const struct batadv_bla_claim *cl1 = data1, *cl2 = data2;
97 
98 	if (!batadv_compare_eth(cl1->addr, cl2->addr))
99 		return 0;
100 
101 	if (cl1->vid != cl2->vid)
102 		return 0;
103 
104 	return 1;
105 }
106 
107 /* free a backbone gw */
108 static void
109 batadv_backbone_gw_free_ref(struct batadv_bla_backbone_gw *backbone_gw)
110 {
111 	if (atomic_dec_and_test(&backbone_gw->refcount))
112 		kfree_rcu(backbone_gw, rcu);
113 }
114 
115 /* finally deinitialize the claim */
116 static void batadv_claim_free_rcu(struct rcu_head *rcu)
117 {
118 	struct batadv_bla_claim *claim;
119 
120 	claim = container_of(rcu, struct batadv_bla_claim, rcu);
121 
122 	batadv_backbone_gw_free_ref(claim->backbone_gw);
123 	kfree(claim);
124 }
125 
126 /* free a claim, call claim_free_rcu if its the last reference */
127 static void batadv_claim_free_ref(struct batadv_bla_claim *claim)
128 {
129 	if (atomic_dec_and_test(&claim->refcount))
130 		call_rcu(&claim->rcu, batadv_claim_free_rcu);
131 }
132 
133 /**
134  * batadv_claim_hash_find
135  * @bat_priv: the bat priv with all the soft interface information
136  * @data: search data (may be local/static data)
137  *
138  * looks for a claim in the hash, and returns it if found
139  * or NULL otherwise.
140  */
141 static struct batadv_bla_claim
142 *batadv_claim_hash_find(struct batadv_priv *bat_priv,
143 			struct batadv_bla_claim *data)
144 {
145 	struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
146 	struct hlist_head *head;
147 	struct batadv_bla_claim *claim;
148 	struct batadv_bla_claim *claim_tmp = NULL;
149 	int index;
150 
151 	if (!hash)
152 		return NULL;
153 
154 	index = batadv_choose_claim(data, hash->size);
155 	head = &hash->table[index];
156 
157 	rcu_read_lock();
158 	hlist_for_each_entry_rcu(claim, head, hash_entry) {
159 		if (!batadv_compare_claim(&claim->hash_entry, data))
160 			continue;
161 
162 		if (!atomic_inc_not_zero(&claim->refcount))
163 			continue;
164 
165 		claim_tmp = claim;
166 		break;
167 	}
168 	rcu_read_unlock();
169 
170 	return claim_tmp;
171 }
172 
173 /**
174  * batadv_backbone_hash_find - looks for a claim in the hash
175  * @bat_priv: the bat priv with all the soft interface information
176  * @addr: the address of the originator
177  * @vid: the VLAN ID
178  *
179  * Returns claim if found or NULL otherwise.
180  */
181 static struct batadv_bla_backbone_gw *
182 batadv_backbone_hash_find(struct batadv_priv *bat_priv,
183 			  uint8_t *addr, unsigned short vid)
184 {
185 	struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
186 	struct hlist_head *head;
187 	struct batadv_bla_backbone_gw search_entry, *backbone_gw;
188 	struct batadv_bla_backbone_gw *backbone_gw_tmp = NULL;
189 	int index;
190 
191 	if (!hash)
192 		return NULL;
193 
194 	memcpy(search_entry.orig, addr, ETH_ALEN);
195 	search_entry.vid = vid;
196 
197 	index = batadv_choose_backbone_gw(&search_entry, hash->size);
198 	head = &hash->table[index];
199 
200 	rcu_read_lock();
201 	hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
202 		if (!batadv_compare_backbone_gw(&backbone_gw->hash_entry,
203 						&search_entry))
204 			continue;
205 
206 		if (!atomic_inc_not_zero(&backbone_gw->refcount))
207 			continue;
208 
209 		backbone_gw_tmp = backbone_gw;
210 		break;
211 	}
212 	rcu_read_unlock();
213 
214 	return backbone_gw_tmp;
215 }
216 
217 /* delete all claims for a backbone */
218 static void
219 batadv_bla_del_backbone_claims(struct batadv_bla_backbone_gw *backbone_gw)
220 {
221 	struct batadv_hashtable *hash;
222 	struct hlist_node *node_tmp;
223 	struct hlist_head *head;
224 	struct batadv_bla_claim *claim;
225 	int i;
226 	spinlock_t *list_lock;	/* protects write access to the hash lists */
227 
228 	hash = backbone_gw->bat_priv->bla.claim_hash;
229 	if (!hash)
230 		return;
231 
232 	for (i = 0; i < hash->size; i++) {
233 		head = &hash->table[i];
234 		list_lock = &hash->list_locks[i];
235 
236 		spin_lock_bh(list_lock);
237 		hlist_for_each_entry_safe(claim, node_tmp,
238 					  head, hash_entry) {
239 			if (claim->backbone_gw != backbone_gw)
240 				continue;
241 
242 			batadv_claim_free_ref(claim);
243 			hlist_del_rcu(&claim->hash_entry);
244 		}
245 		spin_unlock_bh(list_lock);
246 	}
247 
248 	/* all claims gone, intialize CRC */
249 	backbone_gw->crc = BATADV_BLA_CRC_INIT;
250 }
251 
252 /**
253  * batadv_bla_send_claim - sends a claim frame according to the provided info
254  * @bat_priv: the bat priv with all the soft interface information
255  * @orig: the mac address to be announced within the claim
256  * @vid: the VLAN ID
257  * @claimtype: the type of the claim (CLAIM, UNCLAIM, ANNOUNCE, ...)
258  */
259 static void batadv_bla_send_claim(struct batadv_priv *bat_priv, uint8_t *mac,
260 				  unsigned short vid, int claimtype)
261 {
262 	struct sk_buff *skb;
263 	struct ethhdr *ethhdr;
264 	struct batadv_hard_iface *primary_if;
265 	struct net_device *soft_iface;
266 	uint8_t *hw_src;
267 	struct batadv_bla_claim_dst local_claim_dest;
268 	__be32 zeroip = 0;
269 
270 	primary_if = batadv_primary_if_get_selected(bat_priv);
271 	if (!primary_if)
272 		return;
273 
274 	memcpy(&local_claim_dest, &bat_priv->bla.claim_dest,
275 	       sizeof(local_claim_dest));
276 	local_claim_dest.type = claimtype;
277 
278 	soft_iface = primary_if->soft_iface;
279 
280 	skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
281 			 /* IP DST: 0.0.0.0 */
282 			 zeroip,
283 			 primary_if->soft_iface,
284 			 /* IP SRC: 0.0.0.0 */
285 			 zeroip,
286 			 /* Ethernet DST: Broadcast */
287 			 NULL,
288 			 /* Ethernet SRC/HW SRC:  originator mac */
289 			 primary_if->net_dev->dev_addr,
290 			 /* HW DST: FF:43:05:XX:YY:YY
291 			  * with XX   = claim type
292 			  * and YY:YY = group id
293 			  */
294 			 (uint8_t *)&local_claim_dest);
295 
296 	if (!skb)
297 		goto out;
298 
299 	ethhdr = (struct ethhdr *)skb->data;
300 	hw_src = (uint8_t *)ethhdr + ETH_HLEN + sizeof(struct arphdr);
301 
302 	/* now we pretend that the client would have sent this ... */
303 	switch (claimtype) {
304 	case BATADV_CLAIM_TYPE_CLAIM:
305 		/* normal claim frame
306 		 * set Ethernet SRC to the clients mac
307 		 */
308 		memcpy(ethhdr->h_source, mac, ETH_ALEN);
309 		batadv_dbg(BATADV_DBG_BLA, bat_priv,
310 			   "bla_send_claim(): CLAIM %pM on vid %d\n", mac,
311 			   BATADV_PRINT_VID(vid));
312 		break;
313 	case BATADV_CLAIM_TYPE_UNCLAIM:
314 		/* unclaim frame
315 		 * set HW SRC to the clients mac
316 		 */
317 		memcpy(hw_src, mac, ETH_ALEN);
318 		batadv_dbg(BATADV_DBG_BLA, bat_priv,
319 			   "bla_send_claim(): UNCLAIM %pM on vid %d\n", mac,
320 			   BATADV_PRINT_VID(vid));
321 		break;
322 	case BATADV_CLAIM_TYPE_ANNOUNCE:
323 		/* announcement frame
324 		 * set HW SRC to the special mac containg the crc
325 		 */
326 		memcpy(hw_src, mac, ETH_ALEN);
327 		batadv_dbg(BATADV_DBG_BLA, bat_priv,
328 			   "bla_send_claim(): ANNOUNCE of %pM on vid %d\n",
329 			   ethhdr->h_source, BATADV_PRINT_VID(vid));
330 		break;
331 	case BATADV_CLAIM_TYPE_REQUEST:
332 		/* request frame
333 		 * set HW SRC and header destination to the receiving backbone
334 		 * gws mac
335 		 */
336 		memcpy(hw_src, mac, ETH_ALEN);
337 		memcpy(ethhdr->h_dest, mac, ETH_ALEN);
338 		batadv_dbg(BATADV_DBG_BLA, bat_priv,
339 			   "bla_send_claim(): REQUEST of %pM to %pM on vid %d\n",
340 			   ethhdr->h_source, ethhdr->h_dest,
341 			   BATADV_PRINT_VID(vid));
342 		break;
343 	}
344 
345 	if (vid & BATADV_VLAN_HAS_TAG)
346 		skb = vlan_insert_tag(skb, htons(ETH_P_8021Q),
347 				      vid & VLAN_VID_MASK);
348 
349 	skb_reset_mac_header(skb);
350 	skb->protocol = eth_type_trans(skb, soft_iface);
351 	batadv_inc_counter(bat_priv, BATADV_CNT_RX);
352 	batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
353 			   skb->len + ETH_HLEN);
354 	soft_iface->last_rx = jiffies;
355 
356 	netif_rx(skb);
357 out:
358 	if (primary_if)
359 		batadv_hardif_free_ref(primary_if);
360 }
361 
362 /**
363  * batadv_bla_get_backbone_gw
364  * @bat_priv: the bat priv with all the soft interface information
365  * @orig: the mac address of the originator
366  * @vid: the VLAN ID
367  *
368  * searches for the backbone gw or creates a new one if it could not
369  * be found.
370  */
371 static struct batadv_bla_backbone_gw *
372 batadv_bla_get_backbone_gw(struct batadv_priv *bat_priv, uint8_t *orig,
373 			   unsigned short vid, bool own_backbone)
374 {
375 	struct batadv_bla_backbone_gw *entry;
376 	struct batadv_orig_node *orig_node;
377 	int hash_added;
378 
379 	entry = batadv_backbone_hash_find(bat_priv, orig, vid);
380 
381 	if (entry)
382 		return entry;
383 
384 	batadv_dbg(BATADV_DBG_BLA, bat_priv,
385 		   "bla_get_backbone_gw(): not found (%pM, %d), creating new entry\n",
386 		   orig, BATADV_PRINT_VID(vid));
387 
388 	entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
389 	if (!entry)
390 		return NULL;
391 
392 	entry->vid = vid;
393 	entry->lasttime = jiffies;
394 	entry->crc = BATADV_BLA_CRC_INIT;
395 	entry->bat_priv = bat_priv;
396 	atomic_set(&entry->request_sent, 0);
397 	atomic_set(&entry->wait_periods, 0);
398 	memcpy(entry->orig, orig, ETH_ALEN);
399 
400 	/* one for the hash, one for returning */
401 	atomic_set(&entry->refcount, 2);
402 
403 	hash_added = batadv_hash_add(bat_priv->bla.backbone_hash,
404 				     batadv_compare_backbone_gw,
405 				     batadv_choose_backbone_gw, entry,
406 				     &entry->hash_entry);
407 
408 	if (unlikely(hash_added != 0)) {
409 		/* hash failed, free the structure */
410 		kfree(entry);
411 		return NULL;
412 	}
413 
414 	/* this is a gateway now, remove any TT entry on this VLAN */
415 	orig_node = batadv_orig_hash_find(bat_priv, orig);
416 	if (orig_node) {
417 		batadv_tt_global_del_orig(bat_priv, orig_node, vid,
418 					  "became a backbone gateway");
419 		batadv_orig_node_free_ref(orig_node);
420 	}
421 
422 	if (own_backbone) {
423 		batadv_bla_send_announce(bat_priv, entry);
424 
425 		/* this will be decreased in the worker thread */
426 		atomic_inc(&entry->request_sent);
427 		atomic_set(&entry->wait_periods, BATADV_BLA_WAIT_PERIODS);
428 		atomic_inc(&bat_priv->bla.num_requests);
429 	}
430 
431 	return entry;
432 }
433 
434 /* update or add the own backbone gw to make sure we announce
435  * where we receive other backbone gws
436  */
437 static void
438 batadv_bla_update_own_backbone_gw(struct batadv_priv *bat_priv,
439 				  struct batadv_hard_iface *primary_if,
440 				  unsigned short vid)
441 {
442 	struct batadv_bla_backbone_gw *backbone_gw;
443 
444 	backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
445 						 primary_if->net_dev->dev_addr,
446 						 vid, true);
447 	if (unlikely(!backbone_gw))
448 		return;
449 
450 	backbone_gw->lasttime = jiffies;
451 	batadv_backbone_gw_free_ref(backbone_gw);
452 }
453 
454 /**
455  * batadv_bla_answer_request - answer a bla request by sending own claims
456  * @bat_priv: the bat priv with all the soft interface information
457  * @vid: the vid where the request came on
458  *
459  * Repeat all of our own claims, and finally send an ANNOUNCE frame
460  * to allow the requester another check if the CRC is correct now.
461  */
462 static void batadv_bla_answer_request(struct batadv_priv *bat_priv,
463 				      struct batadv_hard_iface *primary_if,
464 				      unsigned short vid)
465 {
466 	struct hlist_head *head;
467 	struct batadv_hashtable *hash;
468 	struct batadv_bla_claim *claim;
469 	struct batadv_bla_backbone_gw *backbone_gw;
470 	int i;
471 
472 	batadv_dbg(BATADV_DBG_BLA, bat_priv,
473 		   "bla_answer_request(): received a claim request, send all of our own claims again\n");
474 
475 	backbone_gw = batadv_backbone_hash_find(bat_priv,
476 						primary_if->net_dev->dev_addr,
477 						vid);
478 	if (!backbone_gw)
479 		return;
480 
481 	hash = bat_priv->bla.claim_hash;
482 	for (i = 0; i < hash->size; i++) {
483 		head = &hash->table[i];
484 
485 		rcu_read_lock();
486 		hlist_for_each_entry_rcu(claim, head, hash_entry) {
487 			/* only own claims are interesting */
488 			if (claim->backbone_gw != backbone_gw)
489 				continue;
490 
491 			batadv_bla_send_claim(bat_priv, claim->addr, claim->vid,
492 					      BATADV_CLAIM_TYPE_CLAIM);
493 		}
494 		rcu_read_unlock();
495 	}
496 
497 	/* finally, send an announcement frame */
498 	batadv_bla_send_announce(bat_priv, backbone_gw);
499 	batadv_backbone_gw_free_ref(backbone_gw);
500 }
501 
502 /**
503  * batadv_bla_send_request - send a request to repeat claims
504  * @backbone_gw: the backbone gateway from whom we are out of sync
505  *
506  * When the crc is wrong, ask the backbone gateway for a full table update.
507  * After the request, it will repeat all of his own claims and finally
508  * send an announcement claim with which we can check again.
509  */
510 static void batadv_bla_send_request(struct batadv_bla_backbone_gw *backbone_gw)
511 {
512 	/* first, remove all old entries */
513 	batadv_bla_del_backbone_claims(backbone_gw);
514 
515 	batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
516 		   "Sending REQUEST to %pM\n", backbone_gw->orig);
517 
518 	/* send request */
519 	batadv_bla_send_claim(backbone_gw->bat_priv, backbone_gw->orig,
520 			      backbone_gw->vid, BATADV_CLAIM_TYPE_REQUEST);
521 
522 	/* no local broadcasts should be sent or received, for now. */
523 	if (!atomic_read(&backbone_gw->request_sent)) {
524 		atomic_inc(&backbone_gw->bat_priv->bla.num_requests);
525 		atomic_set(&backbone_gw->request_sent, 1);
526 	}
527 }
528 
529 /**
530  * batadv_bla_send_announce
531  * @bat_priv: the bat priv with all the soft interface information
532  * @backbone_gw: our backbone gateway which should be announced
533  *
534  * This function sends an announcement. It is called from multiple
535  * places.
536  */
537 static void batadv_bla_send_announce(struct batadv_priv *bat_priv,
538 				     struct batadv_bla_backbone_gw *backbone_gw)
539 {
540 	uint8_t mac[ETH_ALEN];
541 	__be16 crc;
542 
543 	memcpy(mac, batadv_announce_mac, 4);
544 	crc = htons(backbone_gw->crc);
545 	memcpy(&mac[4], &crc, 2);
546 
547 	batadv_bla_send_claim(bat_priv, mac, backbone_gw->vid,
548 			      BATADV_CLAIM_TYPE_ANNOUNCE);
549 }
550 
551 /**
552  * batadv_bla_add_claim - Adds a claim in the claim hash
553  * @bat_priv: the bat priv with all the soft interface information
554  * @mac: the mac address of the claim
555  * @vid: the VLAN ID of the frame
556  * @backbone_gw: the backbone gateway which claims it
557  */
558 static void batadv_bla_add_claim(struct batadv_priv *bat_priv,
559 				 const uint8_t *mac, const unsigned short vid,
560 				 struct batadv_bla_backbone_gw *backbone_gw)
561 {
562 	struct batadv_bla_claim *claim;
563 	struct batadv_bla_claim search_claim;
564 	int hash_added;
565 
566 	memcpy(search_claim.addr, mac, ETH_ALEN);
567 	search_claim.vid = vid;
568 	claim = batadv_claim_hash_find(bat_priv, &search_claim);
569 
570 	/* create a new claim entry if it does not exist yet. */
571 	if (!claim) {
572 		claim = kzalloc(sizeof(*claim), GFP_ATOMIC);
573 		if (!claim)
574 			return;
575 
576 		memcpy(claim->addr, mac, ETH_ALEN);
577 		claim->vid = vid;
578 		claim->lasttime = jiffies;
579 		claim->backbone_gw = backbone_gw;
580 
581 		atomic_set(&claim->refcount, 2);
582 		batadv_dbg(BATADV_DBG_BLA, bat_priv,
583 			   "bla_add_claim(): adding new entry %pM, vid %d to hash ...\n",
584 			   mac, BATADV_PRINT_VID(vid));
585 		hash_added = batadv_hash_add(bat_priv->bla.claim_hash,
586 					     batadv_compare_claim,
587 					     batadv_choose_claim, claim,
588 					     &claim->hash_entry);
589 
590 		if (unlikely(hash_added != 0)) {
591 			/* only local changes happened. */
592 			kfree(claim);
593 			return;
594 		}
595 	} else {
596 		claim->lasttime = jiffies;
597 		if (claim->backbone_gw == backbone_gw)
598 			/* no need to register a new backbone */
599 			goto claim_free_ref;
600 
601 		batadv_dbg(BATADV_DBG_BLA, bat_priv,
602 			   "bla_add_claim(): changing ownership for %pM, vid %d\n",
603 			   mac, BATADV_PRINT_VID(vid));
604 
605 		claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
606 		batadv_backbone_gw_free_ref(claim->backbone_gw);
607 	}
608 	/* set (new) backbone gw */
609 	atomic_inc(&backbone_gw->refcount);
610 	claim->backbone_gw = backbone_gw;
611 
612 	backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
613 	backbone_gw->lasttime = jiffies;
614 
615 claim_free_ref:
616 	batadv_claim_free_ref(claim);
617 }
618 
619 /* Delete a claim from the claim hash which has the
620  * given mac address and vid.
621  */
622 static void batadv_bla_del_claim(struct batadv_priv *bat_priv,
623 				 const uint8_t *mac, const unsigned short vid)
624 {
625 	struct batadv_bla_claim search_claim, *claim;
626 
627 	memcpy(search_claim.addr, mac, ETH_ALEN);
628 	search_claim.vid = vid;
629 	claim = batadv_claim_hash_find(bat_priv, &search_claim);
630 	if (!claim)
631 		return;
632 
633 	batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla_del_claim(): %pM, vid %d\n",
634 		   mac, BATADV_PRINT_VID(vid));
635 
636 	batadv_hash_remove(bat_priv->bla.claim_hash, batadv_compare_claim,
637 			   batadv_choose_claim, claim);
638 	batadv_claim_free_ref(claim); /* reference from the hash is gone */
639 
640 	claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
641 
642 	/* don't need the reference from hash_find() anymore */
643 	batadv_claim_free_ref(claim);
644 }
645 
646 /* check for ANNOUNCE frame, return 1 if handled */
647 static int batadv_handle_announce(struct batadv_priv *bat_priv,
648 				  uint8_t *an_addr, uint8_t *backbone_addr,
649 				  unsigned short vid)
650 {
651 	struct batadv_bla_backbone_gw *backbone_gw;
652 	uint16_t crc;
653 
654 	if (memcmp(an_addr, batadv_announce_mac, 4) != 0)
655 		return 0;
656 
657 	backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
658 						 false);
659 
660 	if (unlikely(!backbone_gw))
661 		return 1;
662 
663 
664 	/* handle as ANNOUNCE frame */
665 	backbone_gw->lasttime = jiffies;
666 	crc = ntohs(*((__be16 *)(&an_addr[4])));
667 
668 	batadv_dbg(BATADV_DBG_BLA, bat_priv,
669 		   "handle_announce(): ANNOUNCE vid %d (sent by %pM)... CRC = %#.4x\n",
670 		   BATADV_PRINT_VID(vid), backbone_gw->orig, crc);
671 
672 	if (backbone_gw->crc != crc) {
673 		batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
674 			   "handle_announce(): CRC FAILED for %pM/%d (my = %#.4x, sent = %#.4x)\n",
675 			   backbone_gw->orig,
676 			   BATADV_PRINT_VID(backbone_gw->vid),
677 			   backbone_gw->crc, crc);
678 
679 		batadv_bla_send_request(backbone_gw);
680 	} else {
681 		/* if we have sent a request and the crc was OK,
682 		 * we can allow traffic again.
683 		 */
684 		if (atomic_read(&backbone_gw->request_sent)) {
685 			atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
686 			atomic_set(&backbone_gw->request_sent, 0);
687 		}
688 	}
689 
690 	batadv_backbone_gw_free_ref(backbone_gw);
691 	return 1;
692 }
693 
694 /* check for REQUEST frame, return 1 if handled */
695 static int batadv_handle_request(struct batadv_priv *bat_priv,
696 				 struct batadv_hard_iface *primary_if,
697 				 uint8_t *backbone_addr,
698 				 struct ethhdr *ethhdr, unsigned short vid)
699 {
700 	/* check for REQUEST frame */
701 	if (!batadv_compare_eth(backbone_addr, ethhdr->h_dest))
702 		return 0;
703 
704 	/* sanity check, this should not happen on a normal switch,
705 	 * we ignore it in this case.
706 	 */
707 	if (!batadv_compare_eth(ethhdr->h_dest, primary_if->net_dev->dev_addr))
708 		return 1;
709 
710 	batadv_dbg(BATADV_DBG_BLA, bat_priv,
711 		   "handle_request(): REQUEST vid %d (sent by %pM)...\n",
712 		   BATADV_PRINT_VID(vid), ethhdr->h_source);
713 
714 	batadv_bla_answer_request(bat_priv, primary_if, vid);
715 	return 1;
716 }
717 
718 /* check for UNCLAIM frame, return 1 if handled */
719 static int batadv_handle_unclaim(struct batadv_priv *bat_priv,
720 				 struct batadv_hard_iface *primary_if,
721 				 uint8_t *backbone_addr,
722 				 uint8_t *claim_addr, unsigned short vid)
723 {
724 	struct batadv_bla_backbone_gw *backbone_gw;
725 
726 	/* unclaim in any case if it is our own */
727 	if (primary_if && batadv_compare_eth(backbone_addr,
728 					     primary_if->net_dev->dev_addr))
729 		batadv_bla_send_claim(bat_priv, claim_addr, vid,
730 				      BATADV_CLAIM_TYPE_UNCLAIM);
731 
732 	backbone_gw = batadv_backbone_hash_find(bat_priv, backbone_addr, vid);
733 
734 	if (!backbone_gw)
735 		return 1;
736 
737 	/* this must be an UNCLAIM frame */
738 	batadv_dbg(BATADV_DBG_BLA, bat_priv,
739 		   "handle_unclaim(): UNCLAIM %pM on vid %d (sent by %pM)...\n",
740 		   claim_addr, BATADV_PRINT_VID(vid), backbone_gw->orig);
741 
742 	batadv_bla_del_claim(bat_priv, claim_addr, vid);
743 	batadv_backbone_gw_free_ref(backbone_gw);
744 	return 1;
745 }
746 
747 /* check for CLAIM frame, return 1 if handled */
748 static int batadv_handle_claim(struct batadv_priv *bat_priv,
749 			       struct batadv_hard_iface *primary_if,
750 			       uint8_t *backbone_addr, uint8_t *claim_addr,
751 			       unsigned short vid)
752 {
753 	struct batadv_bla_backbone_gw *backbone_gw;
754 
755 	/* register the gateway if not yet available, and add the claim. */
756 
757 	backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
758 						 false);
759 
760 	if (unlikely(!backbone_gw))
761 		return 1;
762 
763 	/* this must be a CLAIM frame */
764 	batadv_bla_add_claim(bat_priv, claim_addr, vid, backbone_gw);
765 	if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
766 		batadv_bla_send_claim(bat_priv, claim_addr, vid,
767 				      BATADV_CLAIM_TYPE_CLAIM);
768 
769 	/* TODO: we could call something like tt_local_del() here. */
770 
771 	batadv_backbone_gw_free_ref(backbone_gw);
772 	return 1;
773 }
774 
775 /**
776  * batadv_check_claim_group
777  * @bat_priv: the bat priv with all the soft interface information
778  * @hw_src: the Hardware source in the ARP Header
779  * @hw_dst: the Hardware destination in the ARP Header
780  * @ethhdr: pointer to the Ethernet header of the claim frame
781  *
782  * checks if it is a claim packet and if its on the same group.
783  * This function also applies the group ID of the sender
784  * if it is in the same mesh.
785  *
786  * returns:
787  *	2  - if it is a claim packet and on the same group
788  *	1  - if is a claim packet from another group
789  *	0  - if it is not a claim packet
790  */
791 static int batadv_check_claim_group(struct batadv_priv *bat_priv,
792 				    struct batadv_hard_iface *primary_if,
793 				    uint8_t *hw_src, uint8_t *hw_dst,
794 				    struct ethhdr *ethhdr)
795 {
796 	uint8_t *backbone_addr;
797 	struct batadv_orig_node *orig_node;
798 	struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
799 
800 	bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
801 	bla_dst_own = &bat_priv->bla.claim_dest;
802 
803 	/* check if it is a claim packet in general */
804 	if (memcmp(bla_dst->magic, bla_dst_own->magic,
805 		   sizeof(bla_dst->magic)) != 0)
806 		return 0;
807 
808 	/* if announcement packet, use the source,
809 	 * otherwise assume it is in the hw_src
810 	 */
811 	switch (bla_dst->type) {
812 	case BATADV_CLAIM_TYPE_CLAIM:
813 		backbone_addr = hw_src;
814 		break;
815 	case BATADV_CLAIM_TYPE_REQUEST:
816 	case BATADV_CLAIM_TYPE_ANNOUNCE:
817 	case BATADV_CLAIM_TYPE_UNCLAIM:
818 		backbone_addr = ethhdr->h_source;
819 		break;
820 	default:
821 		return 0;
822 	}
823 
824 	/* don't accept claim frames from ourselves */
825 	if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
826 		return 0;
827 
828 	/* if its already the same group, it is fine. */
829 	if (bla_dst->group == bla_dst_own->group)
830 		return 2;
831 
832 	/* lets see if this originator is in our mesh */
833 	orig_node = batadv_orig_hash_find(bat_priv, backbone_addr);
834 
835 	/* dont accept claims from gateways which are not in
836 	 * the same mesh or group.
837 	 */
838 	if (!orig_node)
839 		return 1;
840 
841 	/* if our mesh friends mac is bigger, use it for ourselves. */
842 	if (ntohs(bla_dst->group) > ntohs(bla_dst_own->group)) {
843 		batadv_dbg(BATADV_DBG_BLA, bat_priv,
844 			   "taking other backbones claim group: %#.4x\n",
845 			   ntohs(bla_dst->group));
846 		bla_dst_own->group = bla_dst->group;
847 	}
848 
849 	batadv_orig_node_free_ref(orig_node);
850 
851 	return 2;
852 }
853 
854 
855 /**
856  * batadv_bla_process_claim
857  * @bat_priv: the bat priv with all the soft interface information
858  * @skb: the frame to be checked
859  *
860  * Check if this is a claim frame, and process it accordingly.
861  *
862  * returns 1 if it was a claim frame, otherwise return 0 to
863  * tell the callee that it can use the frame on its own.
864  */
865 static int batadv_bla_process_claim(struct batadv_priv *bat_priv,
866 				    struct batadv_hard_iface *primary_if,
867 				    struct sk_buff *skb)
868 {
869 	struct batadv_bla_claim_dst *bla_dst;
870 	uint8_t *hw_src, *hw_dst;
871 	struct vlan_ethhdr *vhdr;
872 	struct ethhdr *ethhdr;
873 	struct arphdr *arphdr;
874 	unsigned short vid;
875 	__be16 proto;
876 	int headlen;
877 	int ret;
878 
879 	vid = batadv_get_vid(skb, 0);
880 	ethhdr = eth_hdr(skb);
881 
882 	proto = ethhdr->h_proto;
883 	headlen = ETH_HLEN;
884 	if (vid & BATADV_VLAN_HAS_TAG) {
885 		vhdr = (struct vlan_ethhdr *)ethhdr;
886 		proto = vhdr->h_vlan_encapsulated_proto;
887 		headlen += VLAN_HLEN;
888 	}
889 
890 	if (proto != htons(ETH_P_ARP))
891 		return 0; /* not a claim frame */
892 
893 	/* this must be a ARP frame. check if it is a claim. */
894 
895 	if (unlikely(!pskb_may_pull(skb, headlen + arp_hdr_len(skb->dev))))
896 		return 0;
897 
898 	/* pskb_may_pull() may have modified the pointers, get ethhdr again */
899 	ethhdr = eth_hdr(skb);
900 	arphdr = (struct arphdr *)((uint8_t *)ethhdr + headlen);
901 
902 	/* Check whether the ARP frame carries a valid
903 	 * IP information
904 	 */
905 	if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
906 		return 0;
907 	if (arphdr->ar_pro != htons(ETH_P_IP))
908 		return 0;
909 	if (arphdr->ar_hln != ETH_ALEN)
910 		return 0;
911 	if (arphdr->ar_pln != 4)
912 		return 0;
913 
914 	hw_src = (uint8_t *)arphdr + sizeof(struct arphdr);
915 	hw_dst = hw_src + ETH_ALEN + 4;
916 	bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
917 
918 	/* check if it is a claim frame. */
919 	ret = batadv_check_claim_group(bat_priv, primary_if, hw_src, hw_dst,
920 				       ethhdr);
921 	if (ret == 1)
922 		batadv_dbg(BATADV_DBG_BLA, bat_priv,
923 			   "bla_process_claim(): received a claim frame from another group. From: %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n",
924 			   ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src,
925 			   hw_dst);
926 
927 	if (ret < 2)
928 		return ret;
929 
930 	/* become a backbone gw ourselves on this vlan if not happened yet */
931 	batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
932 
933 	/* check for the different types of claim frames ... */
934 	switch (bla_dst->type) {
935 	case BATADV_CLAIM_TYPE_CLAIM:
936 		if (batadv_handle_claim(bat_priv, primary_if, hw_src,
937 					ethhdr->h_source, vid))
938 			return 1;
939 		break;
940 	case BATADV_CLAIM_TYPE_UNCLAIM:
941 		if (batadv_handle_unclaim(bat_priv, primary_if,
942 					  ethhdr->h_source, hw_src, vid))
943 			return 1;
944 		break;
945 
946 	case BATADV_CLAIM_TYPE_ANNOUNCE:
947 		if (batadv_handle_announce(bat_priv, hw_src, ethhdr->h_source,
948 					   vid))
949 			return 1;
950 		break;
951 	case BATADV_CLAIM_TYPE_REQUEST:
952 		if (batadv_handle_request(bat_priv, primary_if, hw_src, ethhdr,
953 					  vid))
954 			return 1;
955 		break;
956 	}
957 
958 	batadv_dbg(BATADV_DBG_BLA, bat_priv,
959 		   "bla_process_claim(): ERROR - this looks like a claim frame, but is useless. eth src %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n",
960 		   ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src, hw_dst);
961 	return 1;
962 }
963 
964 /* Check when we last heard from other nodes, and remove them in case of
965  * a time out, or clean all backbone gws if now is set.
966  */
967 static void batadv_bla_purge_backbone_gw(struct batadv_priv *bat_priv, int now)
968 {
969 	struct batadv_bla_backbone_gw *backbone_gw;
970 	struct hlist_node *node_tmp;
971 	struct hlist_head *head;
972 	struct batadv_hashtable *hash;
973 	spinlock_t *list_lock;	/* protects write access to the hash lists */
974 	int i;
975 
976 	hash = bat_priv->bla.backbone_hash;
977 	if (!hash)
978 		return;
979 
980 	for (i = 0; i < hash->size; i++) {
981 		head = &hash->table[i];
982 		list_lock = &hash->list_locks[i];
983 
984 		spin_lock_bh(list_lock);
985 		hlist_for_each_entry_safe(backbone_gw, node_tmp,
986 					  head, hash_entry) {
987 			if (now)
988 				goto purge_now;
989 			if (!batadv_has_timed_out(backbone_gw->lasttime,
990 						  BATADV_BLA_BACKBONE_TIMEOUT))
991 				continue;
992 
993 			batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
994 				   "bla_purge_backbone_gw(): backbone gw %pM timed out\n",
995 				   backbone_gw->orig);
996 
997 purge_now:
998 			/* don't wait for the pending request anymore */
999 			if (atomic_read(&backbone_gw->request_sent))
1000 				atomic_dec(&bat_priv->bla.num_requests);
1001 
1002 			batadv_bla_del_backbone_claims(backbone_gw);
1003 
1004 			hlist_del_rcu(&backbone_gw->hash_entry);
1005 			batadv_backbone_gw_free_ref(backbone_gw);
1006 		}
1007 		spin_unlock_bh(list_lock);
1008 	}
1009 }
1010 
1011 /**
1012  * batadv_bla_purge_claims
1013  * @bat_priv: the bat priv with all the soft interface information
1014  * @primary_if: the selected primary interface, may be NULL if now is set
1015  * @now: whether the whole hash shall be wiped now
1016  *
1017  * Check when we heard last time from our own claims, and remove them in case of
1018  * a time out, or clean all claims if now is set
1019  */
1020 static void batadv_bla_purge_claims(struct batadv_priv *bat_priv,
1021 				    struct batadv_hard_iface *primary_if,
1022 				    int now)
1023 {
1024 	struct batadv_bla_claim *claim;
1025 	struct hlist_head *head;
1026 	struct batadv_hashtable *hash;
1027 	int i;
1028 
1029 	hash = bat_priv->bla.claim_hash;
1030 	if (!hash)
1031 		return;
1032 
1033 	for (i = 0; i < hash->size; i++) {
1034 		head = &hash->table[i];
1035 
1036 		rcu_read_lock();
1037 		hlist_for_each_entry_rcu(claim, head, hash_entry) {
1038 			if (now)
1039 				goto purge_now;
1040 			if (!batadv_compare_eth(claim->backbone_gw->orig,
1041 						primary_if->net_dev->dev_addr))
1042 				continue;
1043 			if (!batadv_has_timed_out(claim->lasttime,
1044 						  BATADV_BLA_CLAIM_TIMEOUT))
1045 				continue;
1046 
1047 			batadv_dbg(BATADV_DBG_BLA, bat_priv,
1048 				   "bla_purge_claims(): %pM, vid %d, time out\n",
1049 				   claim->addr, claim->vid);
1050 
1051 purge_now:
1052 			batadv_handle_unclaim(bat_priv, primary_if,
1053 					      claim->backbone_gw->orig,
1054 					      claim->addr, claim->vid);
1055 		}
1056 		rcu_read_unlock();
1057 	}
1058 }
1059 
1060 /**
1061  * batadv_bla_update_orig_address
1062  * @bat_priv: the bat priv with all the soft interface information
1063  * @primary_if: the new selected primary_if
1064  * @oldif: the old primary interface, may be NULL
1065  *
1066  * Update the backbone gateways when the own orig address changes.
1067  */
1068 void batadv_bla_update_orig_address(struct batadv_priv *bat_priv,
1069 				    struct batadv_hard_iface *primary_if,
1070 				    struct batadv_hard_iface *oldif)
1071 {
1072 	struct batadv_bla_backbone_gw *backbone_gw;
1073 	struct hlist_head *head;
1074 	struct batadv_hashtable *hash;
1075 	__be16 group;
1076 	int i;
1077 
1078 	/* reset bridge loop avoidance group id */
1079 	group = htons(crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN));
1080 	bat_priv->bla.claim_dest.group = group;
1081 
1082 	/* purge everything when bridge loop avoidance is turned off */
1083 	if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1084 		oldif = NULL;
1085 
1086 	if (!oldif) {
1087 		batadv_bla_purge_claims(bat_priv, NULL, 1);
1088 		batadv_bla_purge_backbone_gw(bat_priv, 1);
1089 		return;
1090 	}
1091 
1092 	hash = bat_priv->bla.backbone_hash;
1093 	if (!hash)
1094 		return;
1095 
1096 	for (i = 0; i < hash->size; i++) {
1097 		head = &hash->table[i];
1098 
1099 		rcu_read_lock();
1100 		hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
1101 			/* own orig still holds the old value. */
1102 			if (!batadv_compare_eth(backbone_gw->orig,
1103 						oldif->net_dev->dev_addr))
1104 				continue;
1105 
1106 			memcpy(backbone_gw->orig,
1107 			       primary_if->net_dev->dev_addr, ETH_ALEN);
1108 			/* send an announce frame so others will ask for our
1109 			 * claims and update their tables.
1110 			 */
1111 			batadv_bla_send_announce(bat_priv, backbone_gw);
1112 		}
1113 		rcu_read_unlock();
1114 	}
1115 }
1116 
1117 /* periodic work to do:
1118  *  * purge structures when they are too old
1119  *  * send announcements
1120  */
1121 static void batadv_bla_periodic_work(struct work_struct *work)
1122 {
1123 	struct delayed_work *delayed_work;
1124 	struct batadv_priv *bat_priv;
1125 	struct batadv_priv_bla *priv_bla;
1126 	struct hlist_head *head;
1127 	struct batadv_bla_backbone_gw *backbone_gw;
1128 	struct batadv_hashtable *hash;
1129 	struct batadv_hard_iface *primary_if;
1130 	int i;
1131 
1132 	delayed_work = container_of(work, struct delayed_work, work);
1133 	priv_bla = container_of(delayed_work, struct batadv_priv_bla, work);
1134 	bat_priv = container_of(priv_bla, struct batadv_priv, bla);
1135 	primary_if = batadv_primary_if_get_selected(bat_priv);
1136 	if (!primary_if)
1137 		goto out;
1138 
1139 	batadv_bla_purge_claims(bat_priv, primary_if, 0);
1140 	batadv_bla_purge_backbone_gw(bat_priv, 0);
1141 
1142 	if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1143 		goto out;
1144 
1145 	hash = bat_priv->bla.backbone_hash;
1146 	if (!hash)
1147 		goto out;
1148 
1149 	for (i = 0; i < hash->size; i++) {
1150 		head = &hash->table[i];
1151 
1152 		rcu_read_lock();
1153 		hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
1154 			if (!batadv_compare_eth(backbone_gw->orig,
1155 						primary_if->net_dev->dev_addr))
1156 				continue;
1157 
1158 			backbone_gw->lasttime = jiffies;
1159 
1160 			batadv_bla_send_announce(bat_priv, backbone_gw);
1161 
1162 			/* request_sent is only set after creation to avoid
1163 			 * problems when we are not yet known as backbone gw
1164 			 * in the backbone.
1165 			 *
1166 			 * We can reset this now after we waited some periods
1167 			 * to give bridge forward delays and bla group forming
1168 			 * some grace time.
1169 			 */
1170 
1171 			if (atomic_read(&backbone_gw->request_sent) == 0)
1172 				continue;
1173 
1174 			if (!atomic_dec_and_test(&backbone_gw->wait_periods))
1175 				continue;
1176 
1177 			atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
1178 			atomic_set(&backbone_gw->request_sent, 0);
1179 		}
1180 		rcu_read_unlock();
1181 	}
1182 out:
1183 	if (primary_if)
1184 		batadv_hardif_free_ref(primary_if);
1185 
1186 	queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1187 			   msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
1188 }
1189 
1190 /* The hash for claim and backbone hash receive the same key because they
1191  * are getting initialized by hash_new with the same key. Reinitializing
1192  * them with to different keys to allow nested locking without generating
1193  * lockdep warnings
1194  */
1195 static struct lock_class_key batadv_claim_hash_lock_class_key;
1196 static struct lock_class_key batadv_backbone_hash_lock_class_key;
1197 
1198 /* initialize all bla structures */
1199 int batadv_bla_init(struct batadv_priv *bat_priv)
1200 {
1201 	int i;
1202 	uint8_t claim_dest[ETH_ALEN] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00};
1203 	struct batadv_hard_iface *primary_if;
1204 	uint16_t crc;
1205 	unsigned long entrytime;
1206 
1207 	spin_lock_init(&bat_priv->bla.bcast_duplist_lock);
1208 
1209 	batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hash registering\n");
1210 
1211 	/* setting claim destination address */
1212 	memcpy(&bat_priv->bla.claim_dest.magic, claim_dest, 3);
1213 	bat_priv->bla.claim_dest.type = 0;
1214 	primary_if = batadv_primary_if_get_selected(bat_priv);
1215 	if (primary_if) {
1216 		crc = crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN);
1217 		bat_priv->bla.claim_dest.group = htons(crc);
1218 		batadv_hardif_free_ref(primary_if);
1219 	} else {
1220 		bat_priv->bla.claim_dest.group = 0; /* will be set later */
1221 	}
1222 
1223 	/* initialize the duplicate list */
1224 	entrytime = jiffies - msecs_to_jiffies(BATADV_DUPLIST_TIMEOUT);
1225 	for (i = 0; i < BATADV_DUPLIST_SIZE; i++)
1226 		bat_priv->bla.bcast_duplist[i].entrytime = entrytime;
1227 	bat_priv->bla.bcast_duplist_curr = 0;
1228 
1229 	if (bat_priv->bla.claim_hash)
1230 		return 0;
1231 
1232 	bat_priv->bla.claim_hash = batadv_hash_new(128);
1233 	bat_priv->bla.backbone_hash = batadv_hash_new(32);
1234 
1235 	if (!bat_priv->bla.claim_hash || !bat_priv->bla.backbone_hash)
1236 		return -ENOMEM;
1237 
1238 	batadv_hash_set_lock_class(bat_priv->bla.claim_hash,
1239 				   &batadv_claim_hash_lock_class_key);
1240 	batadv_hash_set_lock_class(bat_priv->bla.backbone_hash,
1241 				   &batadv_backbone_hash_lock_class_key);
1242 
1243 	batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hashes initialized\n");
1244 
1245 	INIT_DELAYED_WORK(&bat_priv->bla.work, batadv_bla_periodic_work);
1246 
1247 	queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
1248 			   msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
1249 	return 0;
1250 }
1251 
1252 /**
1253  * batadv_bla_check_bcast_duplist
1254  * @bat_priv: the bat priv with all the soft interface information
1255  * @skb: contains the bcast_packet to be checked
1256  *
1257  * check if it is on our broadcast list. Another gateway might
1258  * have sent the same packet because it is connected to the same backbone,
1259  * so we have to remove this duplicate.
1260  *
1261  * This is performed by checking the CRC, which will tell us
1262  * with a good chance that it is the same packet. If it is furthermore
1263  * sent by another host, drop it. We allow equal packets from
1264  * the same host however as this might be intended.
1265  */
1266 int batadv_bla_check_bcast_duplist(struct batadv_priv *bat_priv,
1267 				   struct sk_buff *skb)
1268 {
1269 	int i, curr, ret = 0;
1270 	__be32 crc;
1271 	struct batadv_bcast_packet *bcast_packet;
1272 	struct batadv_bcast_duplist_entry *entry;
1273 
1274 	bcast_packet = (struct batadv_bcast_packet *)skb->data;
1275 
1276 	/* calculate the crc ... */
1277 	crc = batadv_skb_crc32(skb, (u8 *)(bcast_packet + 1));
1278 
1279 	spin_lock_bh(&bat_priv->bla.bcast_duplist_lock);
1280 
1281 	for (i = 0; i < BATADV_DUPLIST_SIZE; i++) {
1282 		curr = (bat_priv->bla.bcast_duplist_curr + i);
1283 		curr %= BATADV_DUPLIST_SIZE;
1284 		entry = &bat_priv->bla.bcast_duplist[curr];
1285 
1286 		/* we can stop searching if the entry is too old ;
1287 		 * later entries will be even older
1288 		 */
1289 		if (batadv_has_timed_out(entry->entrytime,
1290 					 BATADV_DUPLIST_TIMEOUT))
1291 			break;
1292 
1293 		if (entry->crc != crc)
1294 			continue;
1295 
1296 		if (batadv_compare_eth(entry->orig, bcast_packet->orig))
1297 			continue;
1298 
1299 		/* this entry seems to match: same crc, not too old,
1300 		 * and from another gw. therefore return 1 to forbid it.
1301 		 */
1302 		ret = 1;
1303 		goto out;
1304 	}
1305 	/* not found, add a new entry (overwrite the oldest entry)
1306 	 * and allow it, its the first occurence.
1307 	 */
1308 	curr = (bat_priv->bla.bcast_duplist_curr + BATADV_DUPLIST_SIZE - 1);
1309 	curr %= BATADV_DUPLIST_SIZE;
1310 	entry = &bat_priv->bla.bcast_duplist[curr];
1311 	entry->crc = crc;
1312 	entry->entrytime = jiffies;
1313 	memcpy(entry->orig, bcast_packet->orig, ETH_ALEN);
1314 	bat_priv->bla.bcast_duplist_curr = curr;
1315 
1316 out:
1317 	spin_unlock_bh(&bat_priv->bla.bcast_duplist_lock);
1318 
1319 	return ret;
1320 }
1321 
1322 
1323 
1324 /**
1325  * batadv_bla_is_backbone_gw_orig
1326  * @bat_priv: the bat priv with all the soft interface information
1327  * @orig: originator mac address
1328  * @vid: VLAN identifier
1329  *
1330  * Check if the originator is a gateway for the VLAN identified by vid.
1331  *
1332  * Returns true if orig is a backbone for this vid, false otherwise.
1333  */
1334 bool batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, uint8_t *orig,
1335 				    unsigned short vid)
1336 {
1337 	struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
1338 	struct hlist_head *head;
1339 	struct batadv_bla_backbone_gw *backbone_gw;
1340 	int i;
1341 
1342 	if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1343 		return false;
1344 
1345 	if (!hash)
1346 		return false;
1347 
1348 	for (i = 0; i < hash->size; i++) {
1349 		head = &hash->table[i];
1350 
1351 		rcu_read_lock();
1352 		hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
1353 			if (batadv_compare_eth(backbone_gw->orig, orig) &&
1354 			    backbone_gw->vid == vid) {
1355 				rcu_read_unlock();
1356 				return true;
1357 			}
1358 		}
1359 		rcu_read_unlock();
1360 	}
1361 
1362 	return false;
1363 }
1364 
1365 
1366 /**
1367  * batadv_bla_is_backbone_gw
1368  * @skb: the frame to be checked
1369  * @orig_node: the orig_node of the frame
1370  * @hdr_size: maximum length of the frame
1371  *
1372  * bla_is_backbone_gw inspects the skb for the VLAN ID and returns 1
1373  * if the orig_node is also a gateway on the soft interface, otherwise it
1374  * returns 0.
1375  */
1376 int batadv_bla_is_backbone_gw(struct sk_buff *skb,
1377 			      struct batadv_orig_node *orig_node, int hdr_size)
1378 {
1379 	struct batadv_bla_backbone_gw *backbone_gw;
1380 	unsigned short vid;
1381 
1382 	if (!atomic_read(&orig_node->bat_priv->bridge_loop_avoidance))
1383 		return 0;
1384 
1385 	/* first, find out the vid. */
1386 	if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
1387 		return 0;
1388 
1389 	vid = batadv_get_vid(skb, hdr_size);
1390 
1391 	/* see if this originator is a backbone gw for this VLAN */
1392 	backbone_gw = batadv_backbone_hash_find(orig_node->bat_priv,
1393 						orig_node->orig, vid);
1394 	if (!backbone_gw)
1395 		return 0;
1396 
1397 	batadv_backbone_gw_free_ref(backbone_gw);
1398 	return 1;
1399 }
1400 
1401 /* free all bla structures (for softinterface free or module unload) */
1402 void batadv_bla_free(struct batadv_priv *bat_priv)
1403 {
1404 	struct batadv_hard_iface *primary_if;
1405 
1406 	cancel_delayed_work_sync(&bat_priv->bla.work);
1407 	primary_if = batadv_primary_if_get_selected(bat_priv);
1408 
1409 	if (bat_priv->bla.claim_hash) {
1410 		batadv_bla_purge_claims(bat_priv, primary_if, 1);
1411 		batadv_hash_destroy(bat_priv->bla.claim_hash);
1412 		bat_priv->bla.claim_hash = NULL;
1413 	}
1414 	if (bat_priv->bla.backbone_hash) {
1415 		batadv_bla_purge_backbone_gw(bat_priv, 1);
1416 		batadv_hash_destroy(bat_priv->bla.backbone_hash);
1417 		bat_priv->bla.backbone_hash = NULL;
1418 	}
1419 	if (primary_if)
1420 		batadv_hardif_free_ref(primary_if);
1421 }
1422 
1423 /**
1424  * batadv_bla_rx
1425  * @bat_priv: the bat priv with all the soft interface information
1426  * @skb: the frame to be checked
1427  * @vid: the VLAN ID of the frame
1428  * @is_bcast: the packet came in a broadcast packet type.
1429  *
1430  * bla_rx avoidance checks if:
1431  *  * we have to race for a claim
1432  *  * if the frame is allowed on the LAN
1433  *
1434  * in these cases, the skb is further handled by this function and
1435  * returns 1, otherwise it returns 0 and the caller shall further
1436  * process the skb.
1437  */
1438 int batadv_bla_rx(struct batadv_priv *bat_priv, struct sk_buff *skb,
1439 		  unsigned short vid, bool is_bcast)
1440 {
1441 	struct ethhdr *ethhdr;
1442 	struct batadv_bla_claim search_claim, *claim = NULL;
1443 	struct batadv_hard_iface *primary_if;
1444 	int ret;
1445 
1446 	ethhdr = eth_hdr(skb);
1447 
1448 	primary_if = batadv_primary_if_get_selected(bat_priv);
1449 	if (!primary_if)
1450 		goto handled;
1451 
1452 	if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1453 		goto allow;
1454 
1455 
1456 	if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
1457 		/* don't allow broadcasts while requests are in flight */
1458 		if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast)
1459 			goto handled;
1460 
1461 	memcpy(search_claim.addr, ethhdr->h_source, ETH_ALEN);
1462 	search_claim.vid = vid;
1463 	claim = batadv_claim_hash_find(bat_priv, &search_claim);
1464 
1465 	if (!claim) {
1466 		/* possible optimization: race for a claim */
1467 		/* No claim exists yet, claim it for us!
1468 		 */
1469 		batadv_handle_claim(bat_priv, primary_if,
1470 				    primary_if->net_dev->dev_addr,
1471 				    ethhdr->h_source, vid);
1472 		goto allow;
1473 	}
1474 
1475 	/* if it is our own claim ... */
1476 	if (batadv_compare_eth(claim->backbone_gw->orig,
1477 			       primary_if->net_dev->dev_addr)) {
1478 		/* ... allow it in any case */
1479 		claim->lasttime = jiffies;
1480 		goto allow;
1481 	}
1482 
1483 	/* if it is a broadcast ... */
1484 	if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast) {
1485 		/* ... drop it. the responsible gateway is in charge.
1486 		 *
1487 		 * We need to check is_bcast because with the gateway
1488 		 * feature, broadcasts (like DHCP requests) may be sent
1489 		 * using a unicast packet type.
1490 		 */
1491 		goto handled;
1492 	} else {
1493 		/* seems the client considers us as its best gateway.
1494 		 * send a claim and update the claim table
1495 		 * immediately.
1496 		 */
1497 		batadv_handle_claim(bat_priv, primary_if,
1498 				    primary_if->net_dev->dev_addr,
1499 				    ethhdr->h_source, vid);
1500 		goto allow;
1501 	}
1502 allow:
1503 	batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
1504 	ret = 0;
1505 	goto out;
1506 
1507 handled:
1508 	kfree_skb(skb);
1509 	ret = 1;
1510 
1511 out:
1512 	if (primary_if)
1513 		batadv_hardif_free_ref(primary_if);
1514 	if (claim)
1515 		batadv_claim_free_ref(claim);
1516 	return ret;
1517 }
1518 
1519 /**
1520  * batadv_bla_tx
1521  * @bat_priv: the bat priv with all the soft interface information
1522  * @skb: the frame to be checked
1523  * @vid: the VLAN ID of the frame
1524  *
1525  * bla_tx checks if:
1526  *  * a claim was received which has to be processed
1527  *  * the frame is allowed on the mesh
1528  *
1529  * in these cases, the skb is further handled by this function and
1530  * returns 1, otherwise it returns 0 and the caller shall further
1531  * process the skb.
1532  *
1533  * This call might reallocate skb data.
1534  */
1535 int batadv_bla_tx(struct batadv_priv *bat_priv, struct sk_buff *skb,
1536 		  unsigned short vid)
1537 {
1538 	struct ethhdr *ethhdr;
1539 	struct batadv_bla_claim search_claim, *claim = NULL;
1540 	struct batadv_hard_iface *primary_if;
1541 	int ret = 0;
1542 
1543 	primary_if = batadv_primary_if_get_selected(bat_priv);
1544 	if (!primary_if)
1545 		goto out;
1546 
1547 	if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1548 		goto allow;
1549 
1550 	/* in VLAN case, the mac header might not be set. */
1551 	skb_reset_mac_header(skb);
1552 
1553 	if (batadv_bla_process_claim(bat_priv, primary_if, skb))
1554 		goto handled;
1555 
1556 	ethhdr = eth_hdr(skb);
1557 
1558 	if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
1559 		/* don't allow broadcasts while requests are in flight */
1560 		if (is_multicast_ether_addr(ethhdr->h_dest))
1561 			goto handled;
1562 
1563 	memcpy(search_claim.addr, ethhdr->h_source, ETH_ALEN);
1564 	search_claim.vid = vid;
1565 
1566 	claim = batadv_claim_hash_find(bat_priv, &search_claim);
1567 
1568 	/* if no claim exists, allow it. */
1569 	if (!claim)
1570 		goto allow;
1571 
1572 	/* check if we are responsible. */
1573 	if (batadv_compare_eth(claim->backbone_gw->orig,
1574 			       primary_if->net_dev->dev_addr)) {
1575 		/* if yes, the client has roamed and we have
1576 		 * to unclaim it.
1577 		 */
1578 		batadv_handle_unclaim(bat_priv, primary_if,
1579 				      primary_if->net_dev->dev_addr,
1580 				      ethhdr->h_source, vid);
1581 		goto allow;
1582 	}
1583 
1584 	/* check if it is a multicast/broadcast frame */
1585 	if (is_multicast_ether_addr(ethhdr->h_dest)) {
1586 		/* drop it. the responsible gateway has forwarded it into
1587 		 * the backbone network.
1588 		 */
1589 		goto handled;
1590 	} else {
1591 		/* we must allow it. at least if we are
1592 		 * responsible for the DESTINATION.
1593 		 */
1594 		goto allow;
1595 	}
1596 allow:
1597 	batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
1598 	ret = 0;
1599 	goto out;
1600 handled:
1601 	ret = 1;
1602 out:
1603 	if (primary_if)
1604 		batadv_hardif_free_ref(primary_if);
1605 	if (claim)
1606 		batadv_claim_free_ref(claim);
1607 	return ret;
1608 }
1609 
1610 int batadv_bla_claim_table_seq_print_text(struct seq_file *seq, void *offset)
1611 {
1612 	struct net_device *net_dev = (struct net_device *)seq->private;
1613 	struct batadv_priv *bat_priv = netdev_priv(net_dev);
1614 	struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
1615 	struct batadv_bla_claim *claim;
1616 	struct batadv_hard_iface *primary_if;
1617 	struct hlist_head *head;
1618 	uint32_t i;
1619 	bool is_own;
1620 	uint8_t *primary_addr;
1621 
1622 	primary_if = batadv_seq_print_text_primary_if_get(seq);
1623 	if (!primary_if)
1624 		goto out;
1625 
1626 	primary_addr = primary_if->net_dev->dev_addr;
1627 	seq_printf(seq,
1628 		   "Claims announced for the mesh %s (orig %pM, group id %#.4x)\n",
1629 		   net_dev->name, primary_addr,
1630 		   ntohs(bat_priv->bla.claim_dest.group));
1631 	seq_printf(seq, "   %-17s    %-5s    %-17s [o] (%-6s)\n",
1632 		   "Client", "VID", "Originator", "CRC");
1633 	for (i = 0; i < hash->size; i++) {
1634 		head = &hash->table[i];
1635 
1636 		rcu_read_lock();
1637 		hlist_for_each_entry_rcu(claim, head, hash_entry) {
1638 			is_own = batadv_compare_eth(claim->backbone_gw->orig,
1639 						    primary_addr);
1640 			seq_printf(seq, " * %pM on %5d by %pM [%c] (%#.4x)\n",
1641 				   claim->addr, BATADV_PRINT_VID(claim->vid),
1642 				   claim->backbone_gw->orig,
1643 				   (is_own ? 'x' : ' '),
1644 				   claim->backbone_gw->crc);
1645 		}
1646 		rcu_read_unlock();
1647 	}
1648 out:
1649 	if (primary_if)
1650 		batadv_hardif_free_ref(primary_if);
1651 	return 0;
1652 }
1653 
1654 int batadv_bla_backbone_table_seq_print_text(struct seq_file *seq, void *offset)
1655 {
1656 	struct net_device *net_dev = (struct net_device *)seq->private;
1657 	struct batadv_priv *bat_priv = netdev_priv(net_dev);
1658 	struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
1659 	struct batadv_bla_backbone_gw *backbone_gw;
1660 	struct batadv_hard_iface *primary_if;
1661 	struct hlist_head *head;
1662 	int secs, msecs;
1663 	uint32_t i;
1664 	bool is_own;
1665 	uint8_t *primary_addr;
1666 
1667 	primary_if = batadv_seq_print_text_primary_if_get(seq);
1668 	if (!primary_if)
1669 		goto out;
1670 
1671 	primary_addr = primary_if->net_dev->dev_addr;
1672 	seq_printf(seq,
1673 		   "Backbones announced for the mesh %s (orig %pM, group id %#.4x)\n",
1674 		   net_dev->name, primary_addr,
1675 		   ntohs(bat_priv->bla.claim_dest.group));
1676 	seq_printf(seq, "   %-17s    %-5s %-9s (%-6s)\n",
1677 		   "Originator", "VID", "last seen", "CRC");
1678 	for (i = 0; i < hash->size; i++) {
1679 		head = &hash->table[i];
1680 
1681 		rcu_read_lock();
1682 		hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
1683 			msecs = jiffies_to_msecs(jiffies -
1684 						 backbone_gw->lasttime);
1685 			secs = msecs / 1000;
1686 			msecs = msecs % 1000;
1687 
1688 			is_own = batadv_compare_eth(backbone_gw->orig,
1689 						    primary_addr);
1690 			if (is_own)
1691 				continue;
1692 
1693 			seq_printf(seq, " * %pM on %5d %4i.%03is (%#.4x)\n",
1694 				   backbone_gw->orig,
1695 				   BATADV_PRINT_VID(backbone_gw->vid), secs,
1696 				   msecs, backbone_gw->crc);
1697 		}
1698 		rcu_read_unlock();
1699 	}
1700 out:
1701 	if (primary_if)
1702 		batadv_hardif_free_ref(primary_if);
1703 	return 0;
1704 }
1705