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