bridge_loop_avoidance.c 53.3 KB
Newer Older
1
/* Copyright (C) 2011-2016  B.A.T.M.A.N. contributors:
2 3 4 5 6 7 8 9 10 11 12 13 14
 *
 * Simon Wunderlich
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * 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 22 23
#include <linux/atomic.h>
#include <linux/byteorder/generic.h>
#include <linux/compiler.h>
24
#include <linux/crc16.h>
25 26 27
#include <linux/errno.h>
#include <linux/etherdevice.h>
#include <linux/fs.h>
28
#include <linux/if_arp.h>
29
#include <linux/if_ether.h>
30
#include <linux/if_vlan.h>
31 32 33
#include <linux/jhash.h>
#include <linux/jiffies.h>
#include <linux/kernel.h>
34
#include <linux/kref.h>
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#include <linux/list.h>
#include <linux/lockdep.h>
#include <linux/netdevice.h>
#include <linux/rculist.h>
#include <linux/rcupdate.h>
#include <linux/seq_file.h>
#include <linux/skbuff.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/stddef.h>
#include <linux/string.h>
#include <linux/workqueue.h>
#include <net/arp.h>

#include "hard-interface.h"
#include "hash.h"
#include "originator.h"
#include "packet.h"
#include "translation-table.h"
54

55
static const u8 batadv_announce_mac[4] = {0x43, 0x05, 0x43, 0x05};
56

57
static void batadv_bla_periodic_work(struct work_struct *work);
58 59 60
static void
batadv_bla_send_announce(struct batadv_priv *bat_priv,
			 struct batadv_bla_backbone_gw *backbone_gw);
61

62
/**
63 64 65
 * batadv_choose_claim - choose the right bucket for a claim.
 * @data: data to hash
 * @size: size of the hash table
66
 *
67
 * Return: the hash index of the claim
68
 */
69
static inline u32 batadv_choose_claim(const void *data, u32 size)
70
{
71
	struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
72
	u32 hash = 0;
73

74 75
	hash = jhash(&claim->addr, sizeof(claim->addr), hash);
	hash = jhash(&claim->vid, sizeof(claim->vid), hash);
76 77 78 79

	return hash % size;
}

80
/**
81 82 83
 * batadv_choose_backbone_gw - choose the right bucket for a backbone gateway.
 * @data: data to hash
 * @size: size of the hash table
84
 *
85
 * Return: the hash index of the backbone gateway
86
 */
87
static inline u32 batadv_choose_backbone_gw(const void *data, u32 size)
88
{
89
	const struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
90
	u32 hash = 0;
91

92 93
	hash = jhash(&claim->addr, sizeof(claim->addr), hash);
	hash = jhash(&claim->vid, sizeof(claim->vid), hash);
94 95 96 97

	return hash % size;
}

98 99 100 101 102 103 104
/**
 * batadv_compare_backbone_gw - compare address and vid of two backbone gws
 * @node: list node of the first entry to compare
 * @data2: pointer to the second backbone gateway
 *
 * Return: 1 if the backbones have the same data, 0 otherwise
 */
105 106
static int batadv_compare_backbone_gw(const struct hlist_node *node,
				      const void *data2)
107
{
108
	const void *data1 = container_of(node, struct batadv_bla_backbone_gw,
109
					 hash_entry);
110 111
	const struct batadv_bla_backbone_gw *gw1 = data1;
	const struct batadv_bla_backbone_gw *gw2 = data2;
112

113 114 115 116 117 118 119
	if (!batadv_compare_eth(gw1->orig, gw2->orig))
		return 0;

	if (gw1->vid != gw2->vid)
		return 0;

	return 1;
120 121
}

122 123 124 125 126 127 128
/**
 * batadv_compare_backbone_gw - compare address and vid of two claims
 * @node: list node of the first entry to compare
 * @data2: pointer to the second claims
 *
 * Return: 1 if the claim have the same data, 0 otherwise
 */
129 130
static int batadv_compare_claim(const struct hlist_node *node,
				const void *data2)
131
{
132
	const void *data1 = container_of(node, struct batadv_bla_claim,
133
					 hash_entry);
134 135
	const struct batadv_bla_claim *cl1 = data1;
	const struct batadv_bla_claim *cl2 = data2;
136 137 138 139 140 141

	if (!batadv_compare_eth(cl1->addr, cl2->addr))
		return 0;

	if (cl1->vid != cl2->vid)
		return 0;
142

143
	return 1;
144 145
}

146
/**
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
 * batadv_backbone_gw_release - release backbone gw from lists and queue for
 *  free after rcu grace period
 * @ref: kref pointer of the backbone gw
 */
static void batadv_backbone_gw_release(struct kref *ref)
{
	struct batadv_bla_backbone_gw *backbone_gw;

	backbone_gw = container_of(ref, struct batadv_bla_backbone_gw,
				   refcount);

	kfree_rcu(backbone_gw, rcu);
}

/**
 * batadv_backbone_gw_free_ref - decrement the backbone gw refcounter and
 *  possibly release it
164 165
 * @backbone_gw: backbone gateway to be free'd
 */
166 167
static void
batadv_backbone_gw_free_ref(struct batadv_bla_backbone_gw *backbone_gw)
168
{
169
	kref_put(&backbone_gw->refcount, batadv_backbone_gw_release);
170 171
}

172 173 174 175 176
/**
 * batadv_claim_release - release claim from lists and queue for free after rcu
 *  grace period
 * @ref: kref pointer of the claim
 */
177
static void batadv_claim_release(struct kref *ref)
178
{
179 180 181 182
	struct batadv_bla_claim *claim;

	claim = container_of(ref, struct batadv_bla_claim, refcount);

183
	batadv_backbone_gw_free_ref(claim->backbone_gw);
184
	kfree_rcu(claim, rcu);
185 186
}

187
/**
188 189
 * batadv_claim_free_ref - decrement the claim refcounter and possibly
 *  release it
190 191
 * @claim: claim to be free'd
 */
192
static void batadv_claim_free_ref(struct batadv_bla_claim *claim)
193
{
194
	kref_put(&claim->refcount, batadv_claim_release);
195 196
}

197
/**
198
 * batadv_claim_hash_find - looks for a claim in the claim hash
199
 * @bat_priv: the bat priv with all the soft interface information
200 201
 * @data: search data (may be local/static data)
 *
202
 * Return: claim if found or NULL otherwise.
203
 */
204 205 206
static struct batadv_bla_claim
*batadv_claim_hash_find(struct batadv_priv *bat_priv,
			struct batadv_bla_claim *data)
207
{
208
	struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
209
	struct hlist_head *head;
210 211
	struct batadv_bla_claim *claim;
	struct batadv_bla_claim *claim_tmp = NULL;
212 213 214 215 216
	int index;

	if (!hash)
		return NULL;

217
	index = batadv_choose_claim(data, hash->size);
218 219 220
	head = &hash->table[index];

	rcu_read_lock();
221
	hlist_for_each_entry_rcu(claim, head, hash_entry) {
222
		if (!batadv_compare_claim(&claim->hash_entry, data))
223 224
			continue;

225
		if (!kref_get_unless_zero(&claim->refcount))
226 227 228 229 230 231 232 233 234 235
			continue;

		claim_tmp = claim;
		break;
	}
	rcu_read_unlock();

	return claim_tmp;
}

236
/**
237
 * batadv_backbone_hash_find - looks for a backbone gateway in the hash
238
 * @bat_priv: the bat priv with all the soft interface information
239 240 241
 * @addr: the address of the originator
 * @vid: the VLAN ID
 *
242
 * Return: backbone gateway if found or NULL otherwise
243
 */
244
static struct batadv_bla_backbone_gw *
245 246
batadv_backbone_hash_find(struct batadv_priv *bat_priv, u8 *addr,
			  unsigned short vid)
247
{
248
	struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
249
	struct hlist_head *head;
250 251
	struct batadv_bla_backbone_gw search_entry, *backbone_gw;
	struct batadv_bla_backbone_gw *backbone_gw_tmp = NULL;
252 253 254 255 256
	int index;

	if (!hash)
		return NULL;

257
	ether_addr_copy(search_entry.orig, addr);
258 259
	search_entry.vid = vid;

260
	index = batadv_choose_backbone_gw(&search_entry, hash->size);
261 262 263
	head = &hash->table[index];

	rcu_read_lock();
264
	hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
265 266
		if (!batadv_compare_backbone_gw(&backbone_gw->hash_entry,
						&search_entry))
267 268
			continue;

269
		if (!kref_get_unless_zero(&backbone_gw->refcount))
270 271 272 273 274 275 276 277 278 279
			continue;

		backbone_gw_tmp = backbone_gw;
		break;
	}
	rcu_read_unlock();

	return backbone_gw_tmp;
}

280 281 282 283
/**
 * batadv_bla_del_backbone_claims - delete all claims for a backbone
 * @backbone_gw: backbone gateway where the claims should be removed
 */
284
static void
285
batadv_bla_del_backbone_claims(struct batadv_bla_backbone_gw *backbone_gw)
286
{
287
	struct batadv_hashtable *hash;
288
	struct hlist_node *node_tmp;
289
	struct hlist_head *head;
290
	struct batadv_bla_claim *claim;
291 292 293
	int i;
	spinlock_t *list_lock;	/* protects write access to the hash lists */

294
	hash = backbone_gw->bat_priv->bla.claim_hash;
295 296 297 298 299 300 301 302
	if (!hash)
		return;

	for (i = 0; i < hash->size; i++) {
		head = &hash->table[i];
		list_lock = &hash->list_locks[i];

		spin_lock_bh(list_lock);
303
		hlist_for_each_entry_safe(claim, node_tmp,
304 305 306 307
					  head, hash_entry) {
			if (claim->backbone_gw != backbone_gw)
				continue;

308
			batadv_claim_free_ref(claim);
309
			hlist_del_rcu(&claim->hash_entry);
310 311 312 313
		}
		spin_unlock_bh(list_lock);
	}

314
	/* all claims gone, initialize CRC */
315
	spin_lock_bh(&backbone_gw->crc_lock);
316
	backbone_gw->crc = BATADV_BLA_CRC_INIT;
317
	spin_unlock_bh(&backbone_gw->crc_lock);
318 319
}

320 321 322
/**
 * batadv_bla_send_claim - sends a claim frame according to the provided info
 * @bat_priv: the bat priv with all the soft interface information
323
 * @mac: the mac address to be announced within the claim
324 325 326
 * @vid: the VLAN ID
 * @claimtype: the type of the claim (CLAIM, UNCLAIM, ANNOUNCE, ...)
 */
327
static void batadv_bla_send_claim(struct batadv_priv *bat_priv, u8 *mac,
328
				  unsigned short vid, int claimtype)
329 330 331
{
	struct sk_buff *skb;
	struct ethhdr *ethhdr;
332
	struct batadv_hard_iface *primary_if;
333
	struct net_device *soft_iface;
334
	u8 *hw_src;
335
	struct batadv_bla_claim_dst local_claim_dest;
336
	__be32 zeroip = 0;
337

338
	primary_if = batadv_primary_if_get_selected(bat_priv);
339 340 341
	if (!primary_if)
		return;

342
	memcpy(&local_claim_dest, &bat_priv->bla.claim_dest,
343
	       sizeof(local_claim_dest));
344 345 346 347 348 349 350 351 352 353 354 355 356 357
	local_claim_dest.type = claimtype;

	soft_iface = primary_if->soft_iface;

	skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
			 /* IP DST: 0.0.0.0 */
			 zeroip,
			 primary_if->soft_iface,
			 /* IP SRC: 0.0.0.0 */
			 zeroip,
			 /* Ethernet DST: Broadcast */
			 NULL,
			 /* Ethernet SRC/HW SRC:  originator mac */
			 primary_if->net_dev->dev_addr,
358
			 /* HW DST: FF:43:05:XX:YY:YY
359
			  * with XX   = claim type
360
			  * and YY:YY = group id
361
			  */
362
			 (u8 *)&local_claim_dest);
363 364 365 366 367

	if (!skb)
		goto out;

	ethhdr = (struct ethhdr *)skb->data;
368
	hw_src = (u8 *)ethhdr + ETH_HLEN + sizeof(struct arphdr);
369 370 371

	/* now we pretend that the client would have sent this ... */
	switch (claimtype) {
372
	case BATADV_CLAIM_TYPE_CLAIM:
373 374 375
		/* normal claim frame
		 * set Ethernet SRC to the clients mac
		 */
376
		ether_addr_copy(ethhdr->h_source, mac);
377
		batadv_dbg(BATADV_DBG_BLA, bat_priv,
378 379
			   "bla_send_claim(): CLAIM %pM on vid %d\n", mac,
			   BATADV_PRINT_VID(vid));
380
		break;
381
	case BATADV_CLAIM_TYPE_UNCLAIM:
382 383 384
		/* unclaim frame
		 * set HW SRC to the clients mac
		 */
385
		ether_addr_copy(hw_src, mac);
386
		batadv_dbg(BATADV_DBG_BLA, bat_priv,
387
			   "bla_send_claim(): UNCLAIM %pM on vid %d\n", mac,
388
			   BATADV_PRINT_VID(vid));
389
		break;
390
	case BATADV_CLAIM_TYPE_ANNOUNCE:
391 392 393
		/* announcement frame
		 * set HW SRC to the special mac containg the crc
		 */
394
		ether_addr_copy(hw_src, mac);
395
		batadv_dbg(BATADV_DBG_BLA, bat_priv,
396
			   "bla_send_claim(): ANNOUNCE of %pM on vid %d\n",
397
			   ethhdr->h_source, BATADV_PRINT_VID(vid));
398
		break;
399
	case BATADV_CLAIM_TYPE_REQUEST:
400
		/* request frame
401 402
		 * set HW SRC and header destination to the receiving backbone
		 * gws mac
403
		 */
404 405
		ether_addr_copy(hw_src, mac);
		ether_addr_copy(ethhdr->h_dest, mac);
406
		batadv_dbg(BATADV_DBG_BLA, bat_priv,
407
			   "bla_send_claim(): REQUEST of %pM to %pM on vid %d\n",
408 409
			   ethhdr->h_source, ethhdr->h_dest,
			   BATADV_PRINT_VID(vid));
410 411 412
		break;
	}

413 414 415
	if (vid & BATADV_VLAN_HAS_TAG)
		skb = vlan_insert_tag(skb, htons(ETH_P_8021Q),
				      vid & VLAN_VID_MASK);
416 417 418

	skb_reset_mac_header(skb);
	skb->protocol = eth_type_trans(skb, soft_iface);
419 420 421
	batadv_inc_counter(bat_priv, BATADV_CNT_RX);
	batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
			   skb->len + ETH_HLEN);
422 423 424 425 426
	soft_iface->last_rx = jiffies;

	netif_rx(skb);
out:
	if (primary_if)
427
		batadv_hardif_free_ref(primary_if);
428 429
}

430
/**
431
 * batadv_bla_get_backbone_gw - finds or creates a backbone gateway
432
 * @bat_priv: the bat priv with all the soft interface information
433 434
 * @orig: the mac address of the originator
 * @vid: the VLAN ID
435
 * @own_backbone: set if the requested backbone is local
436
 *
437
 * Return: the (possibly created) backbone gateway or NULL on error
438
 */
439
static struct batadv_bla_backbone_gw *
440
batadv_bla_get_backbone_gw(struct batadv_priv *bat_priv, u8 *orig,
441
			   unsigned short vid, bool own_backbone)
442
{
443
	struct batadv_bla_backbone_gw *entry;
444
	struct batadv_orig_node *orig_node;
445 446
	int hash_added;

447
	entry = batadv_backbone_hash_find(bat_priv, orig, vid);
448 449 450 451

	if (entry)
		return entry;

452
	batadv_dbg(BATADV_DBG_BLA, bat_priv,
453
		   "bla_get_backbone_gw(): not found (%pM, %d), creating new entry\n",
454
		   orig, BATADV_PRINT_VID(vid));
455 456 457 458 459 460 461

	entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
	if (!entry)
		return NULL;

	entry->vid = vid;
	entry->lasttime = jiffies;
462
	entry->crc = BATADV_BLA_CRC_INIT;
463
	entry->bat_priv = bat_priv;
464
	spin_lock_init(&entry->crc_lock);
465
	atomic_set(&entry->request_sent, 0);
466
	atomic_set(&entry->wait_periods, 0);
467
	ether_addr_copy(entry->orig, orig);
468 469

	/* one for the hash, one for returning */
470 471
	kref_init(&entry->refcount);
	kref_get(&entry->refcount);
472

473
	hash_added = batadv_hash_add(bat_priv->bla.backbone_hash,
474 475 476
				     batadv_compare_backbone_gw,
				     batadv_choose_backbone_gw, entry,
				     &entry->hash_entry);
477 478 479 480 481 482 483

	if (unlikely(hash_added != 0)) {
		/* hash failed, free the structure */
		kfree(entry);
		return NULL;
	}

484
	/* this is a gateway now, remove any TT entry on this VLAN */
485
	orig_node = batadv_orig_hash_find(bat_priv, orig);
486
	if (orig_node) {
487
		batadv_tt_global_del_orig(bat_priv, orig_node, vid,
488
					  "became a backbone gateway");
489
		batadv_orig_node_put(orig_node);
490
	}
491

492
	if (own_backbone) {
493 494
		batadv_bla_send_announce(bat_priv, entry);

495 496
		/* this will be decreased in the worker thread */
		atomic_inc(&entry->request_sent);
497
		atomic_set(&entry->wait_periods, BATADV_BLA_WAIT_PERIODS);
498 499 500
		atomic_inc(&bat_priv->bla.num_requests);
	}

501 502 503
	return entry;
}

504 505 506 507 508 509 510
/**
 * batadv_bla_update_own_backbone_gw - updates the own backbone gw for a VLAN
 * @bat_priv: the bat priv with all the soft interface information
 * @primary_if: the selected primary interface
 * @vid: VLAN identifier
 *
 * update or add the own backbone gw to make sure we announce
511 512
 * where we receive other backbone gws
 */
513 514 515
static void
batadv_bla_update_own_backbone_gw(struct batadv_priv *bat_priv,
				  struct batadv_hard_iface *primary_if,
516
				  unsigned short vid)
517
{
518
	struct batadv_bla_backbone_gw *backbone_gw;
519

520 521
	backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
						 primary_if->net_dev->dev_addr,
522
						 vid, true);
523 524 525 526
	if (unlikely(!backbone_gw))
		return;

	backbone_gw->lasttime = jiffies;
527
	batadv_backbone_gw_free_ref(backbone_gw);
528 529
}

530 531 532
/**
 * batadv_bla_answer_request - answer a bla request by sending own claims
 * @bat_priv: the bat priv with all the soft interface information
533
 * @primary_if: interface where the request came on
534 535 536 537 538
 * @vid: the vid where the request came on
 *
 * Repeat all of our own claims, and finally send an ANNOUNCE frame
 * to allow the requester another check if the CRC is correct now.
 */
539 540
static void batadv_bla_answer_request(struct batadv_priv *bat_priv,
				      struct batadv_hard_iface *primary_if,
541
				      unsigned short vid)
542 543
{
	struct hlist_head *head;
544
	struct batadv_hashtable *hash;
545
	struct batadv_bla_claim *claim;
546
	struct batadv_bla_backbone_gw *backbone_gw;
547 548
	int i;

549
	batadv_dbg(BATADV_DBG_BLA, bat_priv,
550
		   "bla_answer_request(): received a claim request, send all of our own claims again\n");
551

552 553 554
	backbone_gw = batadv_backbone_hash_find(bat_priv,
						primary_if->net_dev->dev_addr,
						vid);
555 556 557
	if (!backbone_gw)
		return;

558
	hash = bat_priv->bla.claim_hash;
559 560 561 562
	for (i = 0; i < hash->size; i++) {
		head = &hash->table[i];

		rcu_read_lock();
563
		hlist_for_each_entry_rcu(claim, head, hash_entry) {
564 565 566 567
			/* only own claims are interesting */
			if (claim->backbone_gw != backbone_gw)
				continue;

568
			batadv_bla_send_claim(bat_priv, claim->addr, claim->vid,
569
					      BATADV_CLAIM_TYPE_CLAIM);
570 571 572 573 574
		}
		rcu_read_unlock();
	}

	/* finally, send an announcement frame */
575 576
	batadv_bla_send_announce(bat_priv, backbone_gw);
	batadv_backbone_gw_free_ref(backbone_gw);
577 578
}

579 580 581
/**
 * batadv_bla_send_request - send a request to repeat claims
 * @backbone_gw: the backbone gateway from whom we are out of sync
582 583 584 585 586
 *
 * When the crc is wrong, ask the backbone gateway for a full table update.
 * After the request, it will repeat all of his own claims and finally
 * send an announcement claim with which we can check again.
 */
587
static void batadv_bla_send_request(struct batadv_bla_backbone_gw *backbone_gw)
588 589
{
	/* first, remove all old entries */
590
	batadv_bla_del_backbone_claims(backbone_gw);
591

592 593
	batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
		   "Sending REQUEST to %pM\n", backbone_gw->orig);
594 595

	/* send request */
596
	batadv_bla_send_claim(backbone_gw->bat_priv, backbone_gw->orig,
597
			      backbone_gw->vid, BATADV_CLAIM_TYPE_REQUEST);
598 599 600

	/* no local broadcasts should be sent or received, for now. */
	if (!atomic_read(&backbone_gw->request_sent)) {
601
		atomic_inc(&backbone_gw->bat_priv->bla.num_requests);
602 603 604 605
		atomic_set(&backbone_gw->request_sent, 1);
	}
}

606
/**
607
 * batadv_bla_send_announce - Send an announcement frame
608
 * @bat_priv: the bat priv with all the soft interface information
609 610
 * @backbone_gw: our backbone gateway which should be announced
 */
611
static void batadv_bla_send_announce(struct batadv_priv *bat_priv,
612
				     struct batadv_bla_backbone_gw *backbone_gw)
613
{
614
	u8 mac[ETH_ALEN];
615
	__be16 crc;
616

617
	memcpy(mac, batadv_announce_mac, 4);
618
	spin_lock_bh(&backbone_gw->crc_lock);
619
	crc = htons(backbone_gw->crc);
620
	spin_unlock_bh(&backbone_gw->crc_lock);
621
	memcpy(&mac[4], &crc, 2);
622

623
	batadv_bla_send_claim(bat_priv, mac, backbone_gw->vid,
624
			      BATADV_CLAIM_TYPE_ANNOUNCE);
625 626
}

627 628 629
/**
 * batadv_bla_add_claim - Adds a claim in the claim hash
 * @bat_priv: the bat priv with all the soft interface information
630 631 632 633
 * @mac: the mac address of the claim
 * @vid: the VLAN ID of the frame
 * @backbone_gw: the backbone gateway which claims it
 */
634
static void batadv_bla_add_claim(struct batadv_priv *bat_priv,
635
				 const u8 *mac, const unsigned short vid,
636
				 struct batadv_bla_backbone_gw *backbone_gw)
637
{
638 639
	struct batadv_bla_claim *claim;
	struct batadv_bla_claim search_claim;
640 641
	int hash_added;

642
	ether_addr_copy(search_claim.addr, mac);
643
	search_claim.vid = vid;
644
	claim = batadv_claim_hash_find(bat_priv, &search_claim);
645 646 647 648 649 650 651

	/* create a new claim entry if it does not exist yet. */
	if (!claim) {
		claim = kzalloc(sizeof(*claim), GFP_ATOMIC);
		if (!claim)
			return;

652
		ether_addr_copy(claim->addr, mac);
653 654 655 656
		claim->vid = vid;
		claim->lasttime = jiffies;
		claim->backbone_gw = backbone_gw;

657 658
		kref_init(&claim->refcount);
		kref_get(&claim->refcount);
659
		batadv_dbg(BATADV_DBG_BLA, bat_priv,
660
			   "bla_add_claim(): adding new entry %pM, vid %d to hash ...\n",
661
			   mac, BATADV_PRINT_VID(vid));
662
		hash_added = batadv_hash_add(bat_priv->bla.claim_hash,
663 664 665
					     batadv_compare_claim,
					     batadv_choose_claim, claim,
					     &claim->hash_entry);
666 667 668 669 670 671 672 673 674 675 676 677

		if (unlikely(hash_added != 0)) {
			/* only local changes happened. */
			kfree(claim);
			return;
		}
	} else {
		claim->lasttime = jiffies;
		if (claim->backbone_gw == backbone_gw)
			/* no need to register a new backbone */
			goto claim_free_ref;

678
		batadv_dbg(BATADV_DBG_BLA, bat_priv,
679
			   "bla_add_claim(): changing ownership for %pM, vid %d\n",
680
			   mac, BATADV_PRINT_VID(vid));
681

682
		spin_lock_bh(&claim->backbone_gw->crc_lock);
683
		claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
684
		spin_unlock_bh(&claim->backbone_gw->crc_lock);
685
		batadv_backbone_gw_free_ref(claim->backbone_gw);
686 687
	}
	/* set (new) backbone gw */
688
	kref_get(&backbone_gw->refcount);
689 690
	claim->backbone_gw = backbone_gw;

691
	spin_lock_bh(&backbone_gw->crc_lock);
692
	backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
693
	spin_unlock_bh(&backbone_gw->crc_lock);
694 695 696
	backbone_gw->lasttime = jiffies;

claim_free_ref:
697
	batadv_claim_free_ref(claim);
698 699
}

700 701 702 703 704
/**
 * batadv_bla_del_claim - delete a claim from the claim hash
 * @bat_priv: the bat priv with all the soft interface information
 * @mac: mac address of the claim to be removed
 * @vid: VLAN id for the claim to be removed
705
 */
706
static void batadv_bla_del_claim(struct batadv_priv *bat_priv,
707
				 const u8 *mac, const unsigned short vid)
708
{
709
	struct batadv_bla_claim search_claim, *claim;
710

711
	ether_addr_copy(search_claim.addr, mac);
712
	search_claim.vid = vid;
713
	claim = batadv_claim_hash_find(bat_priv, &search_claim);
714 715 716
	if (!claim)
		return;

717
	batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla_del_claim(): %pM, vid %d\n",
718
		   mac, BATADV_PRINT_VID(vid));
719

720
	batadv_hash_remove(bat_priv->bla.claim_hash, batadv_compare_claim,
721 722
			   batadv_choose_claim, claim);
	batadv_claim_free_ref(claim); /* reference from the hash is gone */
723

724
	spin_lock_bh(&claim->backbone_gw->crc_lock);
725
	claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
726
	spin_unlock_bh(&claim->backbone_gw->crc_lock);
727 728

	/* don't need the reference from hash_find() anymore */
729
	batadv_claim_free_ref(claim);
730 731
}

732 733
/**
 * batadv_handle_announce - check for ANNOUNCE frame
734 735 736 737
 * @bat_priv: the bat priv with all the soft interface information
 * @an_addr: announcement mac address (ARP Sender HW address)
 * @backbone_addr: originator address of the sender (Ethernet source MAC)
 * @vid: the VLAN ID of the frame
738 739 740
 *
 * Return: 1 if handled
 */
741 742
static int batadv_handle_announce(struct batadv_priv *bat_priv, u8 *an_addr,
				  u8 *backbone_addr, unsigned short vid)
743
{
744
	struct batadv_bla_backbone_gw *backbone_gw;
745
	u16 backbone_crc, crc;
746

747
	if (memcmp(an_addr, batadv_announce_mac, 4) != 0)
748 749
		return 0;

750 751
	backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
						 false);
752 753 754 755 756 757

	if (unlikely(!backbone_gw))
		return 1;

	/* handle as ANNOUNCE frame */
	backbone_gw->lasttime = jiffies;
758
	crc = ntohs(*((__be16 *)(&an_addr[4])));
759

760
	batadv_dbg(BATADV_DBG_BLA, bat_priv,
761
		   "handle_announce(): ANNOUNCE vid %d (sent by %pM)... CRC = %#.4x\n",
762
		   BATADV_PRINT_VID(vid), backbone_gw->orig, crc);
763

764 765 766 767 768
	spin_lock_bh(&backbone_gw->crc_lock);
	backbone_crc = backbone_gw->crc;
	spin_unlock_bh(&backbone_gw->crc_lock);

	if (backbone_crc != crc) {
769
		batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
770
			   "handle_announce(): CRC FAILED for %pM/%d (my = %#.4x, sent = %#.4x)\n",
771 772
			   backbone_gw->orig,
			   BATADV_PRINT_VID(backbone_gw->vid),
773
			   backbone_crc, crc);
774

775
		batadv_bla_send_request(backbone_gw);
776 777 778 779 780
	} else {
		/* if we have sent a request and the crc was OK,
		 * we can allow traffic again.
		 */
		if (atomic_read(&backbone_gw->request_sent)) {
781
			atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
782 783 784 785
			atomic_set(&backbone_gw->request_sent, 0);
		}
	}

786
	batadv_backbone_gw_free_ref(backbone_gw);
787 788 789
	return 1;
}

790 791
/**
 * batadv_handle_request - check for REQUEST frame
792 793 794 795 796
 * @bat_priv: the bat priv with all the soft interface information
 * @primary_if: the primary hard interface of this batman soft interface
 * @backbone_addr: backbone address to be requested (ARP sender HW MAC)
 * @ethhdr: ethernet header of a packet
 * @vid: the VLAN ID of the frame
797 798 799
 *
 * Return: 1 if handled
 */
800 801
static int batadv_handle_request(struct batadv_priv *bat_priv,
				 struct batadv_hard_iface *primary_if,
802 803
				 u8 *backbone_addr, struct ethhdr *ethhdr,
				 unsigned short vid)
804 805
{
	/* check for REQUEST frame */
806
	if (!batadv_compare_eth(backbone_addr, ethhdr->h_dest))
807 808 809 810 811
		return 0;

	/* sanity check, this should not happen on a normal switch,
	 * we ignore it in this case.
	 */
812
	if (!batadv_compare_eth(ethhdr->h_dest, primary_if->net_dev->dev_addr))
813 814
		return 1;

815
	batadv_dbg(BATADV_DBG_BLA, bat_priv,
816
		   "handle_request(): REQUEST vid %d (sent by %pM)...\n",
817
		   BATADV_PRINT_VID(vid), ethhdr->h_source);
818

819
	batadv_bla_answer_request(bat_priv, primary_if, vid);
820 821 822
	return 1;
}

823 824
/**
 * batadv_handle_unclaim - check for UNCLAIM frame
825 826 827 828 829
 * @bat_priv: the bat priv with all the soft interface information
 * @primary_if: the primary hard interface of this batman soft interface
 * @backbone_addr: originator address of the backbone (Ethernet source)
 * @claim_addr: Client to be unclaimed (ARP sender HW MAC)
 * @vid: the VLAN ID of the frame
830 831 832
 *
 * Return: 1 if handled
 */
833 834
static int batadv_handle_unclaim(struct batadv_priv *bat_priv,
				 struct batadv_hard_iface *primary_if,
835 836
				 u8 *backbone_addr, u8 *claim_addr,
				 unsigned short vid)
837
{
838
	struct batadv_bla_backbone_gw *backbone_gw;
839 840

	/* unclaim in any case if it is our own */
841 842
	if (primary_if && batadv_compare_eth(backbone_addr,
					     primary_if->net_dev->dev_addr))
843
		batadv_bla_send_claim(bat_priv, claim_addr, vid,
844
				      BATADV_CLAIM_TYPE_UNCLAIM);
845

846
	backbone_gw = batadv_backbone_hash_find(bat_priv, backbone_addr, vid);
847 848 849 850 851

	if (!backbone_gw)
		return 1;

	/* this must be an UNCLAIM frame */
852
	batadv_dbg(BATADV_DBG_BLA, bat_priv,
853
		   "handle_unclaim(): UNCLAIM %pM on vid %d (sent by %pM)...\n",
854
		   claim_addr, BATADV_PRINT_VID(vid), backbone_gw->orig);
855

856 857
	batadv_bla_del_claim(bat_priv, claim_addr, vid);
	batadv_backbone_gw_free_ref(backbone_gw);
858 859 860
	return 1;
}

861 862
/**
 * batadv_handle_claim - check for CLAIM frame
863 864 865 866 867
 * @bat_priv: the bat priv with all the soft interface information
 * @primary_if: the primary hard interface of this batman soft interface
 * @backbone_addr: originator address of the backbone (Ethernet Source)
 * @claim_addr: client mac address to be claimed (ARP sender HW MAC)
 * @vid: the VLAN ID of the frame
868 869 870
 *
 * Return: 1 if handled
 */
871 872
static int batadv_handle_claim(struct batadv_priv *bat_priv,
			       struct batadv_hard_iface *primary_if,
873
			       u8 *backbone_addr, u8 *claim_addr,
874
			       unsigned short vid)
875
{
876
	struct batadv_bla_backbone_gw *backbone_gw;
877 878 879

	/* register the gateway if not yet available, and add the claim. */

880 881
	backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
						 false);
882 883 884 885 886

	if (unlikely(!backbone_gw))
		return 1;

	/* this must be a CLAIM frame */
887
	batadv_bla_add_claim(bat_priv, claim_addr, vid, backbone_gw);
888
	if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
889
		batadv_bla_send_claim(bat_priv, claim_addr, vid,
890
				      BATADV_CLAIM_TYPE_CLAIM);
891 892 893

	/* TODO: we could call something like tt_local_del() here. */

894
	batadv_backbone_gw_free_ref(backbone_gw);
895 896 897
	return 1;
}

898
/**
899
 * batadv_check_claim_group - check for claim group membership
900
 * @bat_priv: the bat priv with all the soft interface information
901
 * @primary_if: the primary interface of this batman interface
902 903 904 905 906 907 908 909
 * @hw_src: the Hardware source in the ARP Header
 * @hw_dst: the Hardware destination in the ARP Header
 * @ethhdr: pointer to the Ethernet header of the claim frame
 *
 * checks if it is a claim packet and if its on the same group.
 * This function also applies the group ID of the sender
 * if it is in the same mesh.
 *
910
 * Return:
911 912 913 914
 *	2  - if it is a claim packet and on the same group
 *	1  - if is a claim packet from another group
 *	0  - if it is not a claim packet
 */
915 916
static int batadv_check_claim_group(struct batadv_priv *bat_priv,
				    struct batadv_hard_iface *primary_if,
917
				    u8 *hw_src, u8 *hw_dst,
918
				    struct ethhdr *ethhdr)
919
{
920
	u8 *backbone_addr;
921
	struct batadv_orig_node *orig_node;
922
	struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
923

924
	bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
925
	bla_dst_own = &bat_priv->bla.claim_dest;
926 927 928 929 930

	/* if announcement packet, use the source,
	 * otherwise assume it is in the hw_src
	 */
	switch (bla_dst->type) {
931
	case BATADV_CLAIM_TYPE_CLAIM:
932 933
		backbone_addr = hw_src;
		break;
934 935
	case BATADV_CLAIM_TYPE_REQUEST:
	case BATADV_CLAIM_TYPE_ANNOUNCE:
936
	case BATADV_CLAIM_TYPE_UNCLAIM:
937 938 939 940 941 942 943
		backbone_addr = ethhdr->h_source;
		break;
	default:
		return 0;
	}

	/* don't accept claim frames from ourselves */
944
	if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
945 946 947 948 949 950 951
		return 0;

	/* if its already the same group, it is fine. */
	if (bla_dst->group == bla_dst_own->group)
		return 2;

	/* lets see if this originator is in our mesh */
952
	orig_node = batadv_orig_hash_find(bat_priv, backbone_addr);
953 954 955 956 957 958 959 960 961

	/* dont accept claims from gateways which are not in
	 * the same mesh or group.
	 */
	if (!orig_node)
		return 1;

	/* if our mesh friends mac is bigger, use it for ourselves. */
	if (ntohs(bla_dst->group) > ntohs(bla_dst_own->group)) {
962
		batadv_dbg(BATADV_DBG_BLA, bat_priv,
963
			   "taking other backbones claim group: %#.4x\n",
964
			   ntohs(bla_dst->group));
965 966 967
		bla_dst_own->group = bla_dst->group;
	}

968
	batadv_orig_node_put(orig_node);
969 970 971 972

	return 2;
}

973
/**
974
 * batadv_bla_process_claim - Check if this is a claim frame, and process it
975
 * @bat_priv: the bat priv with all the soft interface information
976
 * @primary_if: the primary hard interface of this batman soft interface
977 978
 * @skb: the frame to be checked
 *
979
 * Return: 1 if it was a claim frame, otherwise return 0 to
980 981
 * tell the callee that it can use the frame on its own.
 */
982 983
static int batadv_bla_process_claim(struct batadv_priv *bat_priv,
				    struct batadv_hard_iface *primary_if,
984
				    struct sk_buff *skb)
985
{
986
	struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
987
	u8 *hw_src, *hw_dst;
988
	struct vlan_hdr *vhdr, vhdr_buf;
989
	struct ethhdr *ethhdr;
990
	struct arphdr *arphdr;
991
	unsigned short vid;
992
	int vlan_depth = 0;
993
	__be16 proto;
994
	int headlen;
995
	int ret;
996

997
	vid = batadv_get_vid(skb, 0);
998
	ethhdr = eth_hdr(skb);
999

1000 1001 1002
	proto = ethhdr->h_proto;
	headlen = ETH_HLEN;
	if (vid & BATADV_VLAN_HAS_TAG) {
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
		/* Traverse the VLAN/Ethertypes.
		 *
		 * At this point it is known that the first protocol is a VLAN
		 * header, so start checking at the encapsulated protocol.
		 *
		 * The depth of the VLAN headers is recorded to drop BLA claim
		 * frames encapsulated into multiple VLAN headers (QinQ).
		 */
		do {
			vhdr = skb_header_pointer(skb, headlen, VLAN_HLEN,
						  &vhdr_buf);
			if (!vhdr)
				return 0;

			proto = vhdr->h_vlan_encapsulated_proto;
			headlen += VLAN_HLEN;
			vlan_depth++;
		} while (proto == htons(ETH_P_8021Q));
1021 1022
	}

1023
	if (proto != htons(ETH_P_ARP))
1024 1025 1026 1027 1028 1029 1030 1031
		return 0; /* not a claim frame */

	/* this must be a ARP frame. check if it is a claim. */

	if (unlikely(!pskb_may_pull(skb, headlen + arp_hdr_len(skb->dev))))
		return 0;

	/* pskb_may_pull() may have modified the pointers, get ethhdr again */
1032
	ethhdr = eth_hdr(skb);
1033
	arphdr = (struct arphdr *)((u8 *)ethhdr + headlen);
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046

	/* Check whether the ARP frame carries a valid
	 * IP information
	 */
	if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
		return 0;
	if (arphdr->ar_pro != htons(ETH_P_IP))
		return 0;
	if (arphdr->ar_hln != ETH_ALEN)
		return 0;
	if (arphdr->ar_pln != 4)
		return 0;

1047
	hw_src = (u8 *)arphdr + sizeof(struct arphdr);
1048
	hw_dst = hw_src + ETH_ALEN + 4;
1049
	bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
	bla_dst_own = &bat_priv->bla.claim_dest;

	/* check if it is a claim frame in general */
	if (memcmp(bla_dst->magic, bla_dst_own->magic,
		   sizeof(bla_dst->magic)) != 0)
		return 0;

	/* check if there is a claim frame encapsulated deeper in (QinQ) and
	 * drop that, as this is not supported by BLA but should also not be
	 * sent via the mesh.
	 */
	if (vlan_depth > 1)
		return 1;
1063 1064

	/* check if it is a claim frame. */
1065 1066
	ret = batadv_check_claim_group(bat_priv, primary_if, hw_src, hw_dst,
				       ethhdr);
1067
	if (ret == 1)
1068
		batadv_dbg(BATADV_DBG_BLA, bat_priv,
1069
			   "bla_process_claim(): received a claim frame from another group. From: %pM on vid %d ...(hw_src %pM, hw_dst %pM)\n",
1070 1071
			   ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src,
			   hw_dst);
1072 1073 1074

	if (ret < 2)
		return ret;
1075 1076

	/* become a backbone gw ourselves on this vlan if not happened yet */
1077
	batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
1078 1079 1080

	/* check for the different types of claim frames ... */
	switch (bla_dst->type) {
1081
	case BATADV_CLAIM_TYPE_CLAIM:
1082 1083
		if (batadv_handle_claim(bat_priv, primary_if, hw_src,
					ethhdr->h_source, vid))
1084 1085
			return 1;
		break;
1086
	case BATADV_CLAIM_TYPE_UNCLAIM:
1087 1088
		if (batadv_handle_unclaim(bat_priv, primary_if,
					  ethhdr->h_source, hw_src, vid))
1089 1090 1091
			return 1;
		break;

1092
	case BATADV_CLAIM_TYPE_ANNOUNCE:
1093 1094
		if (batadv_handle_announce(bat_priv, hw_src, ethhdr->h_source,
					   vid))
1095 1096
			return 1;
		break;
1097
	case BATADV_CLAIM_TYPE_REQUEST:
1098 1099
		if (batadv_handle_request(bat_priv, primary_if, hw_src, ethhdr,
					  vid))
1100 1101 1102 1103
			return 1;
		break;
	}

1104
	batadv_dbg(BATADV_DBG_BLA, bat_priv,
1105
		   "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",
1106
		   ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src, hw_dst);
1107 1108 1109
	return 1;
}

1110 1111 1112 1113 1114 1115 1116
/**
 * batadv_bla_purge_backbone_gw - Remove backbone gateways after a timeout or
 *  immediately
 * @bat_priv: the bat priv with all the soft interface information
 * @now: whether the whole hash shall be wiped now
 *
 * Check when we last heard from other nodes, and remove them in case of
1117 1118
 * a time out, or clean all backbone gws if now is set.
 */
1119
static void batadv_bla_purge_backbone_gw(struct batadv_priv *bat_priv, int now)
1120
{
1121
	struct batadv_bla_backbone_gw *backbone_gw;
1122
	struct hlist_node *node_tmp;
1123
	struct hlist_head *head;
1124
	struct batadv_hashtable *hash;
1125 1126 1127
	spinlock_t *list_lock;	/* protects write access to the hash lists */
	int i;

1128
	hash = bat_priv->bla.backbone_hash;
1129 1130 1131 1132 1133 1134 1135 1136
	if (!hash)
		return;

	for (i = 0; i < hash->size; i++) {
		head = &hash->table[i];
		list_lock = &hash->list_locks[i];

		spin_lock_bh(list_lock);
1137
		hlist_for_each_entry_safe(backbone_gw, node_tmp,
1138 1139 1140
					  head, hash_entry) {
			if (now)
				goto purge_now;
1141
			if (!batadv_has_timed_out(backbone_gw->lasttime,
1142
						  BATADV_BLA_BACKBONE_TIMEOUT))
1143 1144
				continue;

1145
			batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
1146 1147
				   "bla_purge_backbone_gw(): backbone gw %pM timed out\n",
				   backbone_gw->orig);
1148 1149 1150 1151

purge_now:
			/* don't wait for the pending request anymore */
			if (atomic_read(&backbone_gw->request_sent))
1152
				atomic_dec(&bat_priv->bla.num_requests);
1153

1154
			batadv_bla_del_backbone_claims(backbone_gw);
1155

1156
			hlist_del_rcu(&backbone_gw->hash_entry);
1157
			batadv_backbone_gw_free_ref(backbone_gw);
1158 1159 1160 1161 1162
		}
		spin_unlock_bh(list_lock);
	}
}

1163
/**
1164
 * batadv_bla_purge_claims - Remove claims after a timeout or immediately
1165
 * @bat_priv: the bat priv with all the soft interface information
1166 1167 1168 1169 1170 1171
 * @primary_if: the selected primary interface, may be NULL if now is set
 * @now: whether the whole hash shall be wiped now
 *
 * Check when we heard last time from our own claims, and remove them in case of
 * a time out, or clean all claims if now is set
 */
1172 1173 1174
static void batadv_bla_purge_claims(struct batadv_priv *bat_priv,
				    struct batadv_hard_iface *primary_if,
				    int now)
1175
{
1176
	struct batadv_bla_claim *claim;
1177
	struct hlist_head *head;
1178
	struct batadv_hashtable *hash;
1179 1180
	int i;

1181
	hash = bat_priv->bla.claim_hash;
1182 1183 1184 1185 1186 1187 1188
	if (!hash)
		return;

	for (i = 0; i < hash->size; i++) {
		head = &hash->table[i];

		rcu_read_lock();
1189
		hlist_for_each_entry_rcu(claim, head, hash_entry) {
1190 1191
			if (now)
				goto purge_now;
1192 1193
			if (!batadv_compare_eth(claim->backbone_gw->orig,
						primary_if->net_dev->dev_addr))
1194
				continue;
1195
			if (!batadv_has_timed_out(claim->lasttime,
1196
						  BATADV_BLA_CLAIM_TIMEOUT))
1197 1198
				continue;

1199
			batadv_dbg(BATADV_DBG_BLA, bat_priv,
1200 1201
				   "bla_purge_claims(): %pM, vid %d, time out\n",
				   claim->addr, claim->vid);
1202 1203

purge_now:
1204 1205 1206
			batadv_handle_unclaim(bat_priv, primary_if,
					      claim->backbone_gw->orig,
					      claim->addr, claim->vid);
1207 1208 1209 1210 1211
		}
		rcu_read_unlock();
	}
}

1212
/**
1213 1214
 * batadv_bla_update_orig_address - Update the backbone gateways when the own
 *  originator address changes
1215
 * @bat_priv: the bat priv with all the soft interface information
1216 1217 1218
 * @primary_if: the new selected primary_if
 * @oldif: the old primary interface, may be NULL
 */
1219 1220 1221
void batadv_bla_update_orig_address(struct batadv_priv *bat_priv,
				    struct batadv_hard_iface *primary_if,
				    struct batadv_hard_iface *oldif)
1222
{
1223
	struct batadv_bla_backbone_gw *backbone_gw;
1224
	struct hlist_head *head;
1225
	struct batadv_hashtable *hash;
1226
	__be16 group;
1227 1228
	int i;

1229
	/* reset bridge loop avoidance group id */
1230 1231
	group = htons(crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN));
	bat_priv->bla.claim_dest.group = group;
1232

1233 1234 1235 1236
	/* purge everything when bridge loop avoidance is turned off */
	if (!atomic_read(&bat_priv->bridge_loop_avoidance))
		oldif = NULL;

1237
	if (!oldif) {
1238 1239
		batadv_bla_purge_claims(bat_priv, NULL, 1);
		batadv_bla_purge_backbone_gw(bat_priv, 1);
1240 1241 1242
		return;
	}

1243
	hash = bat_priv->bla.backbone_hash;
1244 1245 1246 1247 1248 1249 1250
	if (!hash)
		return;

	for (i = 0; i < hash->size; i++) {
		head = &hash->table[i];

		rcu_read_lock();
1251
		hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
1252
			/* own orig still holds the old value. */
1253 1254
			if (!batadv_compare_eth(backbone_gw->orig,
						oldif->net_dev->dev_addr))
1255 1256
				continue;

1257 1258
			ether_addr_copy(backbone_gw->orig,
					primary_if->net_dev->dev_addr);
1259 1260 1261
			/* send an announce frame so others will ask for our
			 * claims and update their tables.
			 */
1262
			batadv_bla_send_announce(bat_priv, backbone_gw);
1263 1264 1265 1266 1267
		}
		rcu_read_unlock();
	}
}

1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
/**
 * batadv_bla_status_update - purge bla interfaces if necessary
 * @net_dev: the soft interface net device
 */
void batadv_bla_status_update(struct net_device *net_dev)
{
	struct batadv_priv *bat_priv = netdev_priv(net_dev);
	struct batadv_hard_iface *primary_if;

	primary_if = batadv_primary_if_get_selected(bat_priv);
	if (!primary_if)
		return;

	/* this function already purges everything when bla is disabled,
	 * so just call that one.
	 */
	batadv_bla_update_orig_address(bat_priv, primary_if, primary_if);
	batadv_hardif_free_ref(primary_if);
}

1288 1289 1290 1291 1292
/**
 * batadv_bla_periodic_work - performs periodic bla work
 * @work: kernel work struct
 *
 * periodic work to do:
1293 1294 1295
 *  * purge structures when they are too old
 *  * send announcements
 */
1296
static void batadv_bla_periodic_work(struct work_struct *work)
1297
{
1298
	struct delayed_work *delayed_work;
1299
	struct batadv_priv *bat_priv;
1300
	struct batadv_priv_bla *priv_bla;
1301
	struct hlist_head *head;
1302
	struct batadv_bla_backbone_gw *backbone_gw;
1303
	struct batadv_hashtable *hash;
1304
	struct batadv_hard_iface *primary_if;
1305 1306
	int i;

1307
	delayed_work = container_of(work, struct delayed_work, work);
1308 1309
	priv_bla = container_of(delayed_work, struct batadv_priv_bla, work);
	bat_priv = container_of(priv_bla, struct batadv_priv, bla);
1310
	primary_if = batadv_primary_if_get_selected(bat_priv);
1311 1312 1313
	if (!primary_if)
		goto out;

1314 1315
	batadv_bla_purge_claims(bat_priv, primary_if, 0);
	batadv_bla_purge_backbone_gw(bat_priv, 0);
1316 1317 1318 1319

	if (!atomic_read(&bat_priv->bridge_loop_avoidance))
		goto out;

1320
	hash = bat_priv->bla.backbone_hash;
1321 1322 1323 1324 1325 1326 1327
	if (!hash)
		goto out;

	for (i = 0; i < hash->size; i++) {
		head = &hash->table[i];

		rcu_read_lock();
1328
		hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
1329 1330
			if (!batadv_compare_eth(backbone_gw->orig,
						primary_if->net_dev->dev_addr))
1331 1332 1333 1334
				continue;

			backbone_gw->lasttime = jiffies;

1335
			batadv_bla_send_announce(bat_priv, backbone_gw);
1336 1337 1338 1339 1340

			/* request_sent is only set after creation to avoid
			 * problems when we are not yet known as backbone gw
			 * in the backbone.
			 *
1341 1342 1343
			 * We can reset this now after we waited some periods
			 * to give bridge forward delays and bla group forming
			 * some grace time.
1344 1345 1346 1347 1348
			 */

			if (atomic_read(&backbone_gw->request_sent) == 0)
				continue;

1349 1350 1351
			if (!atomic_dec_and_test(&backbone_gw->wait_periods))
				continue;

1352 1353
			atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
			atomic_set(&backbone_gw->request_sent, 0);
1354 1355 1356 1357 1358
		}
		rcu_read_unlock();
	}
out:
	if (primary_if)
1359
		batadv_hardif_free_ref(primary_if);
1360

1361 1362
	queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
			   msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
1363 1364
}

1365 1366 1367 1368 1369
/* The hash for claim and backbone hash receive the same key because they
 * are getting initialized by hash_new with the same key. Reinitializing
 * them with to different keys to allow nested locking without generating
 * lockdep warnings
 */
1370 1371
static struct lock_class_key batadv_claim_hash_lock_class_key;
static struct lock_class_key batadv_backbone_hash_lock_class_key;
1372

1373 1374 1375 1376 1377 1378
/**
 * batadv_bla_init - initialize all bla structures
 * @bat_priv: the bat priv with all the soft interface information
 *
 * Return: 0 on success, < 0 on error.
 */
1379
int batadv_bla_init(struct batadv_priv *bat_priv)
1380
{
1381
	int i;
1382
	u8 claim_dest[ETH_ALEN] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00};
1383
	struct batadv_hard_iface *primary_if;
1384
	u16 crc;
1385
	unsigned long entrytime;
1386

1387 1388
	spin_lock_init(&bat_priv->bla.bcast_duplist_lock);

1389
	batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hash registering\n");
1390

1391
	/* setting claim destination address */
1392 1393
	memcpy(&bat_priv->bla.claim_dest.magic, claim_dest, 3);
	bat_priv->bla.claim_dest.type = 0;
1394
	primary_if = batadv_primary_if_get_selected(bat_priv);
1395
	if (primary_if) {
1396 1397
		crc = crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN);
		bat_priv->bla.claim_dest.group = htons(crc);
1398
		batadv_hardif_free_ref(primary_if);
1399
	} else {
1400
		bat_priv->bla.claim_dest.group = 0; /* will be set later */
1401 1402
	}

1403
	/* initialize the duplicate list */
1404
	entrytime = jiffies - msecs_to_jiffies(BATADV_DUPLIST_TIMEOUT);
1405
	for (i = 0; i < BATADV_DUPLIST_SIZE; i++)
1406 1407
		bat_priv->bla.bcast_duplist[i].entrytime = entrytime;
	bat_priv->bla.bcast_duplist_curr = 0;
1408

1409
	if (bat_priv->bla.claim_hash)
1410
		return 0;
1411

1412 1413
	bat_priv->bla.claim_hash = batadv_hash_new(128);
	bat_priv->bla.backbone_hash = batadv_hash_new(32);
1414

1415
	if (!bat_priv->bla.claim_hash || !bat_priv->bla.backbone_hash)
1416
		return -ENOMEM;
1417

1418
	batadv_hash_set_lock_class(bat_priv->bla.claim_hash,
1419
				   &batadv_claim_hash_lock_class_key);
1420
	batadv_hash_set_lock_class(bat_priv->bla.backbone_hash,
1421
				   &batadv_backbone_hash_lock_class_key);
1422

1423
	batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hashes initialized\n");
1424

1425 1426 1427 1428
	INIT_DELAYED_WORK(&bat_priv->bla.work, batadv_bla_periodic_work);

	queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
			   msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
1429
	return 0;
1430 1431
}

1432
/**
1433
 * batadv_bla_check_bcast_duplist - Check if a frame is in the broadcast dup.
1434
 * @bat_priv: the bat priv with all the soft interface information
1435
 * @skb: contains the bcast_packet to be checked
1436 1437 1438 1439 1440 1441 1442 1443 1444
 *
 * check if it is on our broadcast list. Another gateway might
 * have sent the same packet because it is connected to the same backbone,
 * so we have to remove this duplicate.
 *
 * This is performed by checking the CRC, which will tell us
 * with a good chance that it is the same packet. If it is furthermore
 * sent by another host, drop it. We allow equal packets from
 * the same host however as this might be intended.
1445 1446
 *
 * Return: 1 if a packet is in the duplicate list, 0 otherwise.
1447
 */
1448
int batadv_bla_check_bcast_duplist(struct batadv_priv *bat_priv,
1449
				   struct sk_buff *skb)
1450
{
1451 1452 1453
	int i, curr, ret = 0;
	__be32 crc;
	struct batadv_bcast_packet *bcast_packet;
1454
	struct batadv_bcast_duplist_entry *entry;
1455

1456
	bcast_packet = (struct batadv_bcast_packet *)skb->data;
1457 1458

	/* calculate the crc ... */
1459
	crc = batadv_skb_crc32(skb, (u8 *)(bcast_packet + 1));
1460

1461 1462
	spin_lock_bh(&bat_priv->bla.bcast_duplist_lock);

1463
	for (i = 0; i < BATADV_DUPLIST_SIZE; i++) {
1464 1465 1466
		curr = (bat_priv->bla.bcast_duplist_curr + i);
		curr %= BATADV_DUPLIST_SIZE;
		entry = &bat_priv->bla.bcast_duplist[curr];
1467 1468 1469 1470

		/* we can stop searching if the entry is too old ;
		 * later entries will be even older
		 */
1471 1472
		if (batadv_has_timed_out(entry->entrytime,
					 BATADV_DUPLIST_TIMEOUT))
1473 1474 1475 1476 1477
			break;

		if (entry->crc != crc)
			continue;

1478
		if (batadv_compare_eth(entry->orig, bcast_packet->orig))
1479 1480 1481 1482 1483
			continue;

		/* this entry seems to match: same crc, not too old,
		 * and from another gw. therefore return 1 to forbid it.
		 */
1484 1485
		ret = 1;
		goto out;
1486
	}
1487
	/* not found, add a new entry (overwrite the oldest entry)
1488
	 * and allow it, its the first occurrence.
1489
	 */
1490
	curr = (bat_priv->bla.bcast_duplist_curr + BATADV_DUPLIST_SIZE - 1);
1491
	curr %= BATADV_DUPLIST_SIZE;
1492
	entry = &bat_priv->bla.bcast_duplist[curr];
1493 1494
	entry->crc = crc;
	entry->entrytime = jiffies;
1495
	ether_addr_copy(entry->orig, bcast_packet->orig);
1496
	bat_priv->bla.bcast_duplist_curr = curr;
1497

1498 1499 1500 1501
out:
	spin_unlock_bh(&bat_priv->bla.bcast_duplist_lock);

	return ret;
1502 1503
}

1504
/**
1505 1506
 * batadv_bla_is_backbone_gw_orig - Check if the originator is a gateway for
 *  the VLAN identified by vid.
1507
 * @bat_priv: the bat priv with all the soft interface information
1508
 * @orig: originator mac address
1509
 * @vid: VLAN identifier
1510
 *
1511
 * Return: true if orig is a backbone for this vid, false otherwise.
1512
 */
1513
bool batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, u8 *orig,
1514
				    unsigned short vid)
1515
{
1516
	struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
1517
	struct hlist_head *head;
1518
	struct batadv_bla_backbone_gw *backbone_gw;
1519 1520 1521
	int i;

	if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1522
		return false;
1523 1524

	if (!hash)
1525
		return false;
1526 1527 1528 1529 1530

	for (i = 0; i < hash->size; i++) {
		head = &hash->table[i];

		rcu_read_lock();
1531
		hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
1532 1533
			if (batadv_compare_eth(backbone_gw->orig, orig) &&
			    backbone_gw->vid == vid) {
1534
				rcu_read_unlock();
1535
				return true;
1536 1537 1538 1539 1540
			}
		}
		rcu_read_unlock();
	}

1541
	return false;
1542 1543
}

1544
/**
1545
 * batadv_bla_is_backbone_gw - check if originator is a backbone gw for a VLAN.
1546
 * @skb: the frame to be checked
1547 1548 1549
 * @orig_node: the orig_node of the frame
 * @hdr_size: maximum length of the frame
 *
1550 1551
 * Return: 1 if the orig_node is also a gateway on the soft interface, otherwise
 * it returns 0.
1552
 */
1553
int batadv_bla_is_backbone_gw(struct sk_buff *skb,
1554
			      struct batadv_orig_node *orig_node, int hdr_size)
1555
{
1556
	struct batadv_bla_backbone_gw *backbone_gw;
1557
	unsigned short vid;
1558 1559 1560 1561 1562

	if (!atomic_read(&orig_node->bat_priv->bridge_loop_avoidance))
		return 0;

	/* first, find out the vid. */
1563
	if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
1564 1565
		return 0;

1566
	vid = batadv_get_vid(skb, hdr_size);
1567 1568

	/* see if this originator is a backbone gw for this VLAN */
1569 1570
	backbone_gw = batadv_backbone_hash_find(orig_node->bat_priv,
						orig_node->orig, vid);
1571 1572 1573
	if (!backbone_gw)
		return 0;

1574
	batadv_backbone_gw_free_ref(backbone_gw);
1575 1576 1577
	return 1;
}

1578 1579 1580 1581 1582 1583
/**
 * batadv_bla_init - free all bla structures
 * @bat_priv: the bat priv with all the soft interface information
 *
 * for softinterface free or module unload
 */
1584
void batadv_bla_free(struct batadv_priv *bat_priv)
1585
{
1586
	struct batadv_hard_iface *primary_if;
1587

1588
	cancel_delayed_work_sync(&bat_priv->bla.work);
1589
	primary_if = batadv_primary_if_get_selected(bat_priv);
1590

1591
	if (bat_priv->bla.claim_hash) {
1592
		batadv_bla_purge_claims(bat_priv, primary_if, 1);
1593 1594
		batadv_hash_destroy(bat_priv->bla.claim_hash);
		bat_priv->bla.claim_hash = NULL;
1595
	}
1596
	if (bat_priv->bla.backbone_hash) {
1597
		batadv_bla_purge_backbone_gw(bat_priv, 1);
1598 1599
		batadv_hash_destroy(bat_priv->bla.backbone_hash);
		bat_priv->bla.backbone_hash = NULL;
1600 1601
	}
	if (primary_if)
1602
		batadv_hardif_free_ref(primary_if);
1603 1604
}

1605
/**
1606
 * batadv_bla_rx - check packets coming from the mesh.
1607
 * @bat_priv: the bat priv with all the soft interface information
1608 1609
 * @skb: the frame to be checked
 * @vid: the VLAN ID of the frame
1610
 * @is_bcast: the packet came in a broadcast packet type.
1611
 *
1612
 * batadv_bla_rx avoidance checks if:
1613 1614 1615
 *  * we have to race for a claim
 *  * if the frame is allowed on the LAN
 *
1616 1617 1618
 * in these cases, the skb is further handled by this function
 *
 * Return: 1 if handled, otherwise it returns 0 and the caller shall further
1619 1620
 * process the skb.
 */
1621 1622
int batadv_bla_rx(struct batadv_priv *bat_priv, struct sk_buff *skb,
		  unsigned short vid, bool is_bcast)
1623 1624
{
	struct ethhdr *ethhdr;
1625
	struct batadv_bla_claim search_claim, *claim = NULL;
1626
	struct batadv_hard_iface *primary_if;
1627 1628
	int ret;

1629
	ethhdr = eth_hdr(skb);
1630

1631
	primary_if = batadv_primary_if_get_selected(bat_priv);
1632 1633 1634 1635 1636 1637
	if (!primary_if)
		goto handled;

	if (!atomic_read(&bat_priv->bridge_loop_avoidance))
		goto allow;

1638
	if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
1639
		/* don't allow broadcasts while requests are in flight */
1640
		if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast)
1641 1642
			goto handled;

1643
	ether_addr_copy(search_claim.addr, ethhdr->h_source);
1644
	search_claim.vid = vid;
1645
	claim = batadv_claim_hash_find(bat_priv, &search_claim);
1646 1647 1648 1649 1650

	if (!claim) {
		/* possible optimization: race for a claim */
		/* No claim exists yet, claim it for us!
		 */
1651 1652 1653
		batadv_handle_claim(bat_priv, primary_if,
				    primary_if->net_dev->dev_addr,
				    ethhdr->h_source, vid);
1654 1655 1656 1657
		goto allow;
	}

	/* if it is our own claim ... */
1658 1659
	if (batadv_compare_eth(claim->backbone_gw->orig,
			       primary_if->net_dev->dev_addr)) {
1660 1661 1662 1663 1664 1665
		/* ... allow it in any case */
		claim->lasttime = jiffies;
		goto allow;
	}

	/* if it is a broadcast ... */
1666 1667 1668 1669 1670 1671 1672
	if (is_multicast_ether_addr(ethhdr->h_dest) && is_bcast) {
		/* ... drop it. the responsible gateway is in charge.
		 *
		 * We need to check is_bcast because with the gateway
		 * feature, broadcasts (like DHCP requests) may be sent
		 * using a unicast packet type.
		 */
1673 1674 1675 1676 1677 1678
		goto handled;
	} else {
		/* seems the client considers us as its best gateway.
		 * send a claim and update the claim table
		 * immediately.
		 */
1679 1680 1681
		batadv_handle_claim(bat_priv, primary_if,
				    primary_if->net_dev->dev_addr,
				    ethhdr->h_source, vid);
1682 1683 1684
		goto allow;
	}
allow:
1685
	batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
1686 1687 1688 1689 1690 1691 1692 1693 1694
	ret = 0;
	goto out;

handled:
	kfree_skb(skb);
	ret = 1;

out:
	if (primary_if)
1695
		batadv_hardif_free_ref(primary_if);
1696
	if (claim)
1697
		batadv_claim_free_ref(claim);
1698 1699 1700
	return ret;
}

1701
/**
1702
 * batadv_bla_tx - check packets going into the mesh
1703
 * @bat_priv: the bat priv with all the soft interface information
1704 1705 1706
 * @skb: the frame to be checked
 * @vid: the VLAN ID of the frame
 *
1707
 * batadv_bla_tx checks if:
1708 1709 1710
 *  * a claim was received which has to be processed
 *  * the frame is allowed on the mesh
 *
1711
 * in these cases, the skb is further handled by this function.
1712 1713
 *
 * This call might reallocate skb data.
1714 1715 1716
 *
 * Return: 1 if handled, otherwise it returns 0 and the caller shall further
 * process the skb.
1717
 */
1718 1719
int batadv_bla_tx(struct batadv_priv *bat_priv, struct sk_buff *skb,
		  unsigned short vid)
1720 1721
{
	struct ethhdr *ethhdr;
1722
	struct batadv_bla_claim search_claim, *claim = NULL;
1723
	struct batadv_hard_iface *primary_if;
1724 1725
	int ret = 0;

1726
	primary_if = batadv_primary_if_get_selected(bat_priv);
1727 1728 1729 1730 1731 1732
	if (!primary_if)
		goto out;

	if (!atomic_read(&bat_priv->bridge_loop_avoidance))
		goto allow;

1733
	if (batadv_bla_process_claim(bat_priv, primary_if, skb))
1734 1735
		goto handled;

1736
	ethhdr = eth_hdr(skb);
1737

1738
	if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
1739 1740 1741 1742
		/* don't allow broadcasts while requests are in flight */
		if (is_multicast_ether_addr(ethhdr->h_dest))
			goto handled;

1743
	ether_addr_copy(search_claim.addr, ethhdr->h_source);
1744 1745
	search_claim.vid = vid;

1746
	claim = batadv_claim_hash_find(bat_priv, &search_claim);
1747 1748 1749 1750 1751 1752

	/* if no claim exists, allow it. */
	if (!claim)
		goto allow;

	/* check if we are responsible. */
1753 1754
	if (batadv_compare_eth(claim->backbone_gw->orig,
			       primary_if->net_dev->dev_addr)) {
1755 1756 1757
		/* if yes, the client has roamed and we have
		 * to unclaim it.
		 */
1758 1759 1760
		batadv_handle_unclaim(bat_priv, primary_if,
				      primary_if->net_dev->dev_addr,
				      ethhdr->h_source, vid);
1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776
		goto allow;
	}

	/* check if it is a multicast/broadcast frame */
	if (is_multicast_ether_addr(ethhdr->h_dest)) {
		/* drop it. the responsible gateway has forwarded it into
		 * the backbone network.
		 */
		goto handled;
	} else {
		/* we must allow it. at least if we are
		 * responsible for the DESTINATION.
		 */
		goto allow;
	}
allow:
1777
	batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
1778 1779 1780 1781 1782 1783
	ret = 0;
	goto out;
handled:
	ret = 1;
out:
	if (primary_if)
1784
		batadv_hardif_free_ref(primary_if);
1785
	if (claim)
1786
		batadv_claim_free_ref(claim);
1787 1788
	return ret;
}
1789

1790 1791 1792 1793 1794 1795 1796
/**
 * batadv_bla_claim_table_seq_print_text - print the claim table in a seq file
 * @seq: seq file to print on
 * @offset: not used
 *
 * Return: always 0
 */
1797
int batadv_bla_claim_table_seq_print_text(struct seq_file *seq, void *offset)
1798 1799
{
	struct net_device *net_dev = (struct net_device *)seq->private;
1800
	struct batadv_priv *bat_priv = netdev_priv(net_dev);
1801
	struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
1802
	struct batadv_bla_claim *claim;
1803
	struct batadv_hard_iface *primary_if;
1804
	struct hlist_head *head;
1805
	u16 backbone_crc;
1806
	u32 i;
1807
	bool is_own;
1808
	u8 *primary_addr;
1809

1810 1811
	primary_if = batadv_seq_print_text_primary_if_get(seq);
	if (!primary_if)
1812 1813
		goto out;

1814
	primary_addr = primary_if->net_dev->dev_addr;
1815
	seq_printf(seq,
1816
		   "Claims announced for the mesh %s (orig %pM, group id %#.4x)\n",
1817
		   net_dev->name, primary_addr,
1818
		   ntohs(bat_priv->bla.claim_dest.group));
1819
	seq_printf(seq, "   %-17s    %-5s    %-17s [o] (%-6s)\n",
1820 1821 1822 1823 1824
		   "Client", "VID", "Originator", "CRC");
	for (i = 0; i < hash->size; i++) {
		head = &hash->table[i];

		rcu_read_lock();
1825
		hlist_for_each_entry_rcu(claim, head, hash_entry) {
1826 1827
			is_own = batadv_compare_eth(claim->backbone_gw->orig,
						    primary_addr);
1828 1829 1830 1831

			spin_lock_bh(&claim->backbone_gw->crc_lock);
			backbone_crc = claim->backbone_gw->crc;
			spin_unlock_bh(&claim->backbone_gw->crc_lock);
1832
			seq_printf(seq, " * %pM on %5d by %pM [%c] (%#.4x)\n",
1833
				   claim->addr, BATADV_PRINT_VID(claim->vid),
1834 1835
				   claim->backbone_gw->orig,
				   (is_own ? 'x' : ' '),
1836
				   backbone_crc);
1837 1838 1839 1840 1841
		}
		rcu_read_unlock();
	}
out:
	if (primary_if)
1842
		batadv_hardif_free_ref(primary_if);
1843
	return 0;
1844
}
1845

1846 1847 1848 1849 1850 1851 1852 1853
/**
 * batadv_bla_backbone_table_seq_print_text - print the backbone table in a seq
 *  file
 * @seq: seq file to print on
 * @offset: not used
 *
 * Return: always 0
 */
1854 1855 1856 1857
int batadv_bla_backbone_table_seq_print_text(struct seq_file *seq, void *offset)
{
	struct net_device *net_dev = (struct net_device *)seq->private;
	struct batadv_priv *bat_priv = netdev_priv(net_dev);
1858
	struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
1859
	struct batadv_bla_backbone_gw *backbone_gw;
1860 1861 1862
	struct batadv_hard_iface *primary_if;
	struct hlist_head *head;
	int secs, msecs;
1863
	u16 backbone_crc;
1864
	u32 i;
1865
	bool is_own;
1866
	u8 *primary_addr;
1867

1868 1869
	primary_if = batadv_seq_print_text_primary_if_get(seq);
	if (!primary_if)
1870 1871 1872 1873
		goto out;

	primary_addr = primary_if->net_dev->dev_addr;
	seq_printf(seq,
1874
		   "Backbones announced for the mesh %s (orig %pM, group id %#.4x)\n",
1875
		   net_dev->name, primary_addr,
1876
		   ntohs(bat_priv->bla.claim_dest.group));
1877
	seq_printf(seq, "   %-17s    %-5s %-9s (%-6s)\n",
1878 1879 1880 1881 1882
		   "Originator", "VID", "last seen", "CRC");
	for (i = 0; i < hash->size; i++) {
		head = &hash->table[i];

		rcu_read_lock();
1883
		hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
1884 1885 1886 1887 1888 1889 1890 1891 1892 1893
			msecs = jiffies_to_msecs(jiffies -
						 backbone_gw->lasttime);
			secs = msecs / 1000;
			msecs = msecs % 1000;

			is_own = batadv_compare_eth(backbone_gw->orig,
						    primary_addr);
			if (is_own)
				continue;

1894 1895 1896 1897
			spin_lock_bh(&backbone_gw->crc_lock);
			backbone_crc = backbone_gw->crc;
			spin_unlock_bh(&backbone_gw->crc_lock);

1898
			seq_printf(seq, " * %pM on %5d %4i.%03is (%#.4x)\n",
1899 1900
				   backbone_gw->orig,
				   BATADV_PRINT_VID(backbone_gw->vid), secs,
1901
				   msecs, backbone_crc);
1902 1903 1904 1905 1906 1907
		}
		rcu_read_unlock();
	}
out:
	if (primary_if)
		batadv_hardif_free_ref(primary_if);
1908
	return 0;
1909
}