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 batadv_bla_claim *claim)
178
{
179
	batadv_backbone_gw_free_ref(claim->backbone_gw);
180
	kfree_rcu(claim, rcu);
181 182
}

183
/**
184 185
 * batadv_claim_free_ref - decrement the claim refcounter and possibly
 *  release it
186 187
 * @claim: claim to be free'd
 */
188
static void batadv_claim_free_ref(struct batadv_bla_claim *claim)
189 190
{
	if (atomic_dec_and_test(&claim->refcount))
191
		batadv_claim_release(claim);
192 193
}

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

	if (!hash)
		return NULL;

214
	index = batadv_choose_claim(data, hash->size);
215 216 217
	head = &hash->table[index];

	rcu_read_lock();
218
	hlist_for_each_entry_rcu(claim, head, hash_entry) {
219
		if (!batadv_compare_claim(&claim->hash_entry, data))
220 221 222 223 224 225 226 227 228 229 230 231 232
			continue;

		if (!atomic_inc_not_zero(&claim->refcount))
			continue;

		claim_tmp = claim;
		break;
	}
	rcu_read_unlock();

	return claim_tmp;
}

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

	if (!hash)
		return NULL;

254
	ether_addr_copy(search_entry.orig, addr);
255 256
	search_entry.vid = vid;

257
	index = batadv_choose_backbone_gw(&search_entry, hash->size);
258 259 260
	head = &hash->table[index];

	rcu_read_lock();
261
	hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
262 263
		if (!batadv_compare_backbone_gw(&backbone_gw->hash_entry,
						&search_entry))
264 265
			continue;

266
		if (!kref_get_unless_zero(&backbone_gw->refcount))
267 268 269 270 271 272 273 274 275 276
			continue;

		backbone_gw_tmp = backbone_gw;
		break;
	}
	rcu_read_unlock();

	return backbone_gw_tmp;
}

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

291
	hash = backbone_gw->bat_priv->bla.claim_hash;
292 293 294 295 296 297 298 299
	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);
300
		hlist_for_each_entry_safe(claim, node_tmp,
301 302 303 304
					  head, hash_entry) {
			if (claim->backbone_gw != backbone_gw)
				continue;

305
			batadv_claim_free_ref(claim);
306
			hlist_del_rcu(&claim->hash_entry);
307 308 309 310
		}
		spin_unlock_bh(list_lock);
	}

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

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

335
	primary_if = batadv_primary_if_get_selected(bat_priv);
336 337 338
	if (!primary_if)
		return;

339
	memcpy(&local_claim_dest, &bat_priv->bla.claim_dest,
340
	       sizeof(local_claim_dest));
341 342 343 344 345 346 347 348 349 350 351 352 353 354
	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,
355
			 /* HW DST: FF:43:05:XX:YY:YY
356
			  * with XX   = claim type
357
			  * and YY:YY = group id
358
			  */
359
			 (u8 *)&local_claim_dest);
360 361 362 363 364

	if (!skb)
		goto out;

	ethhdr = (struct ethhdr *)skb->data;
365
	hw_src = (u8 *)ethhdr + ETH_HLEN + sizeof(struct arphdr);
366 367 368

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

410 411 412
	if (vid & BATADV_VLAN_HAS_TAG)
		skb = vlan_insert_tag(skb, htons(ETH_P_8021Q),
				      vid & VLAN_VID_MASK);
413 414 415

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

	netif_rx(skb);
out:
	if (primary_if)
424
		batadv_hardif_free_ref(primary_if);
425 426
}

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

444
	entry = batadv_backbone_hash_find(bat_priv, orig, vid);
445 446 447 448

	if (entry)
		return entry;

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

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

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

	/* one for the hash, one for returning */
467 468
	kref_init(&entry->refcount);
	kref_get(&entry->refcount);
469

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

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

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

489
	if (own_backbone) {
490 491
		batadv_bla_send_announce(bat_priv, entry);

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

498 499 500
	return entry;
}

501 502 503 504 505 506 507
/**
 * 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
508 509
 * where we receive other backbone gws
 */
510 511 512
static void
batadv_bla_update_own_backbone_gw(struct batadv_priv *bat_priv,
				  struct batadv_hard_iface *primary_if,
513
				  unsigned short vid)
514
{
515
	struct batadv_bla_backbone_gw *backbone_gw;
516

517 518
	backbone_gw = batadv_bla_get_backbone_gw(bat_priv,
						 primary_if->net_dev->dev_addr,
519
						 vid, true);
520 521 522 523
	if (unlikely(!backbone_gw))
		return;

	backbone_gw->lasttime = jiffies;
524
	batadv_backbone_gw_free_ref(backbone_gw);
525 526
}

527 528 529
/**
 * batadv_bla_answer_request - answer a bla request by sending own claims
 * @bat_priv: the bat priv with all the soft interface information
530
 * @primary_if: interface where the request came on
531 532 533 534 535
 * @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.
 */
536 537
static void batadv_bla_answer_request(struct batadv_priv *bat_priv,
				      struct batadv_hard_iface *primary_if,
538
				      unsigned short vid)
539 540
{
	struct hlist_head *head;
541
	struct batadv_hashtable *hash;
542
	struct batadv_bla_claim *claim;
543
	struct batadv_bla_backbone_gw *backbone_gw;
544 545
	int i;

546
	batadv_dbg(BATADV_DBG_BLA, bat_priv,
547
		   "bla_answer_request(): received a claim request, send all of our own claims again\n");
548

549 550 551
	backbone_gw = batadv_backbone_hash_find(bat_priv,
						primary_if->net_dev->dev_addr,
						vid);
552 553 554
	if (!backbone_gw)
		return;

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

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

565
			batadv_bla_send_claim(bat_priv, claim->addr, claim->vid,
566
					      BATADV_CLAIM_TYPE_CLAIM);
567 568 569 570 571
		}
		rcu_read_unlock();
	}

	/* finally, send an announcement frame */
572 573
	batadv_bla_send_announce(bat_priv, backbone_gw);
	batadv_backbone_gw_free_ref(backbone_gw);
574 575
}

576 577 578
/**
 * batadv_bla_send_request - send a request to repeat claims
 * @backbone_gw: the backbone gateway from whom we are out of sync
579 580 581 582 583
 *
 * 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.
 */
584
static void batadv_bla_send_request(struct batadv_bla_backbone_gw *backbone_gw)
585 586
{
	/* first, remove all old entries */
587
	batadv_bla_del_backbone_claims(backbone_gw);
588

589 590
	batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
		   "Sending REQUEST to %pM\n", backbone_gw->orig);
591 592

	/* send request */
593
	batadv_bla_send_claim(backbone_gw->bat_priv, backbone_gw->orig,
594
			      backbone_gw->vid, BATADV_CLAIM_TYPE_REQUEST);
595 596 597

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

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

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

620
	batadv_bla_send_claim(bat_priv, mac, backbone_gw->vid,
621
			      BATADV_CLAIM_TYPE_ANNOUNCE);
622 623
}

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

639
	ether_addr_copy(search_claim.addr, mac);
640
	search_claim.vid = vid;
641
	claim = batadv_claim_hash_find(bat_priv, &search_claim);
642 643 644 645 646 647 648

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

649
		ether_addr_copy(claim->addr, mac);
650 651 652 653 654
		claim->vid = vid;
		claim->lasttime = jiffies;
		claim->backbone_gw = backbone_gw;

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

		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;

674
		batadv_dbg(BATADV_DBG_BLA, bat_priv,
675
			   "bla_add_claim(): changing ownership for %pM, vid %d\n",
676
			   mac, BATADV_PRINT_VID(vid));
677

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

687
	spin_lock_bh(&backbone_gw->crc_lock);
688
	backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
689
	spin_unlock_bh(&backbone_gw->crc_lock);
690 691 692
	backbone_gw->lasttime = jiffies;

claim_free_ref:
693
	batadv_claim_free_ref(claim);
694 695
}

696 697 698 699 700
/**
 * 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
701
 */
702
static void batadv_bla_del_claim(struct batadv_priv *bat_priv,
703
				 const u8 *mac, const unsigned short vid)
704
{
705
	struct batadv_bla_claim search_claim, *claim;
706

707
	ether_addr_copy(search_claim.addr, mac);
708
	search_claim.vid = vid;
709
	claim = batadv_claim_hash_find(bat_priv, &search_claim);
710 711 712
	if (!claim)
		return;

713
	batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla_del_claim(): %pM, vid %d\n",
714
		   mac, BATADV_PRINT_VID(vid));
715

716
	batadv_hash_remove(bat_priv->bla.claim_hash, batadv_compare_claim,
717 718
			   batadv_choose_claim, claim);
	batadv_claim_free_ref(claim); /* reference from the hash is gone */
719

720
	spin_lock_bh(&claim->backbone_gw->crc_lock);
721
	claim->backbone_gw->crc ^= crc16(0, claim->addr, ETH_ALEN);
722
	spin_unlock_bh(&claim->backbone_gw->crc_lock);
723 724

	/* don't need the reference from hash_find() anymore */
725
	batadv_claim_free_ref(claim);
726 727
}

728 729
/**
 * batadv_handle_announce - check for ANNOUNCE frame
730 731 732 733
 * @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
734 735 736
 *
 * Return: 1 if handled
 */
737 738
static int batadv_handle_announce(struct batadv_priv *bat_priv, u8 *an_addr,
				  u8 *backbone_addr, unsigned short vid)
739
{
740
	struct batadv_bla_backbone_gw *backbone_gw;
741
	u16 backbone_crc, crc;
742

743
	if (memcmp(an_addr, batadv_announce_mac, 4) != 0)
744 745
		return 0;

746 747
	backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
						 false);
748 749 750 751 752 753

	if (unlikely(!backbone_gw))
		return 1;

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

756
	batadv_dbg(BATADV_DBG_BLA, bat_priv,
757
		   "handle_announce(): ANNOUNCE vid %d (sent by %pM)... CRC = %#.4x\n",
758
		   BATADV_PRINT_VID(vid), backbone_gw->orig, crc);
759

760 761 762 763 764
	spin_lock_bh(&backbone_gw->crc_lock);
	backbone_crc = backbone_gw->crc;
	spin_unlock_bh(&backbone_gw->crc_lock);

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

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

782
	batadv_backbone_gw_free_ref(backbone_gw);
783 784 785
	return 1;
}

786 787
/**
 * batadv_handle_request - check for REQUEST frame
788 789 790 791 792
 * @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
793 794 795
 *
 * Return: 1 if handled
 */
796 797
static int batadv_handle_request(struct batadv_priv *bat_priv,
				 struct batadv_hard_iface *primary_if,
798 799
				 u8 *backbone_addr, struct ethhdr *ethhdr,
				 unsigned short vid)
800 801
{
	/* check for REQUEST frame */
802
	if (!batadv_compare_eth(backbone_addr, ethhdr->h_dest))
803 804 805 806 807
		return 0;

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

811
	batadv_dbg(BATADV_DBG_BLA, bat_priv,
812
		   "handle_request(): REQUEST vid %d (sent by %pM)...\n",
813
		   BATADV_PRINT_VID(vid), ethhdr->h_source);
814

815
	batadv_bla_answer_request(bat_priv, primary_if, vid);
816 817 818
	return 1;
}

819 820
/**
 * batadv_handle_unclaim - check for UNCLAIM frame
821 822 823 824 825
 * @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
826 827 828
 *
 * Return: 1 if handled
 */
829 830
static int batadv_handle_unclaim(struct batadv_priv *bat_priv,
				 struct batadv_hard_iface *primary_if,
831 832
				 u8 *backbone_addr, u8 *claim_addr,
				 unsigned short vid)
833
{
834
	struct batadv_bla_backbone_gw *backbone_gw;
835 836

	/* unclaim in any case if it is our own */
837 838
	if (primary_if && batadv_compare_eth(backbone_addr,
					     primary_if->net_dev->dev_addr))
839
		batadv_bla_send_claim(bat_priv, claim_addr, vid,
840
				      BATADV_CLAIM_TYPE_UNCLAIM);
841

842
	backbone_gw = batadv_backbone_hash_find(bat_priv, backbone_addr, vid);
843 844 845 846 847

	if (!backbone_gw)
		return 1;

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

852 853
	batadv_bla_del_claim(bat_priv, claim_addr, vid);
	batadv_backbone_gw_free_ref(backbone_gw);
854 855 856
	return 1;
}

857 858
/**
 * batadv_handle_claim - check for CLAIM frame
859 860 861 862 863
 * @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
864 865 866
 *
 * Return: 1 if handled
 */
867 868
static int batadv_handle_claim(struct batadv_priv *bat_priv,
			       struct batadv_hard_iface *primary_if,
869
			       u8 *backbone_addr, u8 *claim_addr,
870
			       unsigned short vid)
871
{
872
	struct batadv_bla_backbone_gw *backbone_gw;
873 874 875

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

876 877
	backbone_gw = batadv_bla_get_backbone_gw(bat_priv, backbone_addr, vid,
						 false);
878 879 880 881 882

	if (unlikely(!backbone_gw))
		return 1;

	/* this must be a CLAIM frame */
883
	batadv_bla_add_claim(bat_priv, claim_addr, vid, backbone_gw);
884
	if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
885
		batadv_bla_send_claim(bat_priv, claim_addr, vid,
886
				      BATADV_CLAIM_TYPE_CLAIM);
887 888 889

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

890
	batadv_backbone_gw_free_ref(backbone_gw);
891 892 893
	return 1;
}

894
/**
895
 * batadv_check_claim_group - check for claim group membership
896
 * @bat_priv: the bat priv with all the soft interface information
897
 * @primary_if: the primary interface of this batman interface
898 899 900 901 902 903 904 905
 * @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.
 *
906
 * Return:
907 908 909 910
 *	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
 */
911 912
static int batadv_check_claim_group(struct batadv_priv *bat_priv,
				    struct batadv_hard_iface *primary_if,
913
				    u8 *hw_src, u8 *hw_dst,
914
				    struct ethhdr *ethhdr)
915
{
916
	u8 *backbone_addr;
917
	struct batadv_orig_node *orig_node;
918
	struct batadv_bla_claim_dst *bla_dst, *bla_dst_own;
919

920
	bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
921
	bla_dst_own = &bat_priv->bla.claim_dest;
922 923 924 925 926

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

	/* don't accept claim frames from ourselves */
940
	if (batadv_compare_eth(backbone_addr, primary_if->net_dev->dev_addr))
941 942 943 944 945 946 947
		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 */
948
	orig_node = batadv_orig_hash_find(bat_priv, backbone_addr);
949 950 951 952 953 954 955 956 957

	/* 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)) {
958
		batadv_dbg(BATADV_DBG_BLA, bat_priv,
959
			   "taking other backbones claim group: %#.4x\n",
960
			   ntohs(bla_dst->group));
961 962 963
		bla_dst_own->group = bla_dst->group;
	}

964
	batadv_orig_node_free_ref(orig_node);
965 966 967 968

	return 2;
}

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

993
	vid = batadv_get_vid(skb, 0);
994
	ethhdr = eth_hdr(skb);
995

996 997 998
	proto = ethhdr->h_proto;
	headlen = ETH_HLEN;
	if (vid & BATADV_VLAN_HAS_TAG) {
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
		/* 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));
1017 1018
	}

1019
	if (proto != htons(ETH_P_ARP))
1020 1021 1022 1023 1024 1025 1026 1027
		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 */
1028
	ethhdr = eth_hdr(skb);
1029
	arphdr = (struct arphdr *)((u8 *)ethhdr + headlen);
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042

	/* 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;

1043
	hw_src = (u8 *)arphdr + sizeof(struct arphdr);
1044
	hw_dst = hw_src + ETH_ALEN + 4;
1045
	bla_dst = (struct batadv_bla_claim_dst *)hw_dst;
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
	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;
1059 1060

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

	if (ret < 2)
		return ret;
1071 1072

	/* become a backbone gw ourselves on this vlan if not happened yet */
1073
	batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
1074 1075 1076

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

1088
	case BATADV_CLAIM_TYPE_ANNOUNCE:
1089 1090
		if (batadv_handle_announce(bat_priv, hw_src, ethhdr->h_source,
					   vid))
1091 1092
			return 1;
		break;
1093
	case BATADV_CLAIM_TYPE_REQUEST:
1094 1095
		if (batadv_handle_request(bat_priv, primary_if, hw_src, ethhdr,
					  vid))
1096 1097 1098 1099
			return 1;
		break;
	}

1100
	batadv_dbg(BATADV_DBG_BLA, bat_priv,
1101
		   "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",
1102
		   ethhdr->h_source, BATADV_PRINT_VID(vid), hw_src, hw_dst);
1103 1104 1105
	return 1;
}

1106 1107 1108 1109 1110 1111 1112
/**
 * 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
1113 1114
 * a time out, or clean all backbone gws if now is set.
 */
1115
static void batadv_bla_purge_backbone_gw(struct batadv_priv *bat_priv, int now)
1116
{
1117
	struct batadv_bla_backbone_gw *backbone_gw;
1118
	struct hlist_node *node_tmp;
1119
	struct hlist_head *head;
1120
	struct batadv_hashtable *hash;
1121 1122 1123
	spinlock_t *list_lock;	/* protects write access to the hash lists */
	int i;

1124
	hash = bat_priv->bla.backbone_hash;
1125 1126 1127 1128 1129 1130 1131 1132
	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);
1133
		hlist_for_each_entry_safe(backbone_gw, node_tmp,
1134 1135 1136
					  head, hash_entry) {
			if (now)
				goto purge_now;
1137
			if (!batadv_has_timed_out(backbone_gw->lasttime,
1138
						  BATADV_BLA_BACKBONE_TIMEOUT))
1139 1140
				continue;

1141
			batadv_dbg(BATADV_DBG_BLA, backbone_gw->bat_priv,
1142 1143
				   "bla_purge_backbone_gw(): backbone gw %pM timed out\n",
				   backbone_gw->orig);
1144 1145 1146 1147

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

1150
			batadv_bla_del_backbone_claims(backbone_gw);
1151

1152
			hlist_del_rcu(&backbone_gw->hash_entry);
1153
			batadv_backbone_gw_free_ref(backbone_gw);
1154 1155 1156 1157 1158
		}
		spin_unlock_bh(list_lock);
	}
}

1159
/**
1160
 * batadv_bla_purge_claims - Remove claims after a timeout or immediately
1161
 * @bat_priv: the bat priv with all the soft interface information
1162 1163 1164 1165 1166 1167
 * @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
 */
1168 1169 1170
static void batadv_bla_purge_claims(struct batadv_priv *bat_priv,
				    struct batadv_hard_iface *primary_if,
				    int now)
1171
{
1172
	struct batadv_bla_claim *claim;
1173
	struct hlist_head *head;
1174
	struct batadv_hashtable *hash;
1175 1176
	int i;

1177
	hash = bat_priv->bla.claim_hash;
1178 1179 1180 1181 1182 1183 1184
	if (!hash)
		return;

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

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

1195
			batadv_dbg(BATADV_DBG_BLA, bat_priv,
1196 1197
				   "bla_purge_claims(): %pM, vid %d, time out\n",
				   claim->addr, claim->vid);
1198 1199

purge_now:
1200 1201 1202
			batadv_handle_unclaim(bat_priv, primary_if,
					      claim->backbone_gw->orig,
					      claim->addr, claim->vid);
1203 1204 1205 1206 1207
		}
		rcu_read_unlock();
	}
}

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

1225
	/* reset bridge loop avoidance group id */
1226 1227
	group = htons(crc16(0, primary_if->net_dev->dev_addr, ETH_ALEN));
	bat_priv->bla.claim_dest.group = group;
1228

1229 1230 1231 1232
	/* purge everything when bridge loop avoidance is turned off */
	if (!atomic_read(&bat_priv->bridge_loop_avoidance))
		oldif = NULL;

1233
	if (!oldif) {
1234 1235
		batadv_bla_purge_claims(bat_priv, NULL, 1);
		batadv_bla_purge_backbone_gw(bat_priv, 1);
1236 1237 1238
		return;
	}

1239
	hash = bat_priv->bla.backbone_hash;
1240 1241 1242 1243 1244 1245 1246
	if (!hash)
		return;

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

		rcu_read_lock();
1247
		hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
1248
			/* own orig still holds the old value. */
1249 1250
			if (!batadv_compare_eth(backbone_gw->orig,
						oldif->net_dev->dev_addr))
1251 1252
				continue;

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

1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
/**
 * 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);
}

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

1303
	delayed_work = container_of(work, struct delayed_work, work);
1304 1305
	priv_bla = container_of(delayed_work, struct batadv_priv_bla, work);
	bat_priv = container_of(priv_bla, struct batadv_priv, bla);
1306
	primary_if = batadv_primary_if_get_selected(bat_priv);
1307 1308 1309
	if (!primary_if)
		goto out;

1310 1311
	batadv_bla_purge_claims(bat_priv, primary_if, 0);
	batadv_bla_purge_backbone_gw(bat_priv, 0);
1312 1313 1314 1315

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

1316
	hash = bat_priv->bla.backbone_hash;
1317 1318 1319 1320 1321 1322 1323
	if (!hash)
		goto out;

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

		rcu_read_lock();
1324
		hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
1325 1326
			if (!batadv_compare_eth(backbone_gw->orig,
						primary_if->net_dev->dev_addr))
1327 1328 1329 1330
				continue;

			backbone_gw->lasttime = jiffies;

1331
			batadv_bla_send_announce(bat_priv, backbone_gw);
1332 1333 1334 1335 1336

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

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

1345 1346 1347
			if (!atomic_dec_and_test(&backbone_gw->wait_periods))
				continue;

1348 1349
			atomic_dec(&backbone_gw->bat_priv->bla.num_requests);
			atomic_set(&backbone_gw->request_sent, 0);
1350 1351 1352 1353 1354
		}
		rcu_read_unlock();
	}
out:
	if (primary_if)
1355
		batadv_hardif_free_ref(primary_if);
1356

1357 1358
	queue_delayed_work(batadv_event_workqueue, &bat_priv->bla.work,
			   msecs_to_jiffies(BATADV_BLA_PERIOD_LENGTH));
1359 1360
}

1361 1362 1363 1364 1365
/* 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
 */
1366 1367
static struct lock_class_key batadv_claim_hash_lock_class_key;
static struct lock_class_key batadv_backbone_hash_lock_class_key;
1368

1369 1370 1371 1372 1373 1374
/**
 * 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.
 */
1375
int batadv_bla_init(struct batadv_priv *bat_priv)
1376
{
1377
	int i;
1378
	u8 claim_dest[ETH_ALEN] = {0xff, 0x43, 0x05, 0x00, 0x00, 0x00};
1379
	struct batadv_hard_iface *primary_if;
1380
	u16 crc;
1381
	unsigned long entrytime;
1382

1383 1384
	spin_lock_init(&bat_priv->bla.bcast_duplist_lock);

1385
	batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hash registering\n");
1386

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

1399
	/* initialize the duplicate list */
1400
	entrytime = jiffies - msecs_to_jiffies(BATADV_DUPLIST_TIMEOUT);
1401
	for (i = 0; i < BATADV_DUPLIST_SIZE; i++)
1402 1403
		bat_priv->bla.bcast_duplist[i].entrytime = entrytime;
	bat_priv->bla.bcast_duplist_curr = 0;
1404

1405
	if (bat_priv->bla.claim_hash)
1406
		return 0;
1407

1408 1409
	bat_priv->bla.claim_hash = batadv_hash_new(128);
	bat_priv->bla.backbone_hash = batadv_hash_new(32);
1410

1411
	if (!bat_priv->bla.claim_hash || !bat_priv->bla.backbone_hash)
1412
		return -ENOMEM;
1413

1414
	batadv_hash_set_lock_class(bat_priv->bla.claim_hash,
1415
				   &batadv_claim_hash_lock_class_key);
1416
	batadv_hash_set_lock_class(bat_priv->bla.backbone_hash,
1417
				   &batadv_backbone_hash_lock_class_key);
1418

1419
	batadv_dbg(BATADV_DBG_BLA, bat_priv, "bla hashes initialized\n");
1420

1421 1422 1423 1424
	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));
1425
	return 0;
1426 1427
}

1428
/**
1429
 * batadv_bla_check_bcast_duplist - Check if a frame is in the broadcast dup.
1430
 * @bat_priv: the bat priv with all the soft interface information
1431
 * @skb: contains the bcast_packet to be checked
1432 1433 1434 1435 1436 1437 1438 1439 1440
 *
 * 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.
1441 1442
 *
 * Return: 1 if a packet is in the duplicate list, 0 otherwise.
1443
 */
1444
int batadv_bla_check_bcast_duplist(struct batadv_priv *bat_priv,
1445
				   struct sk_buff *skb)
1446
{
1447 1448 1449
	int i, curr, ret = 0;
	__be32 crc;
	struct batadv_bcast_packet *bcast_packet;
1450
	struct batadv_bcast_duplist_entry *entry;
1451

1452
	bcast_packet = (struct batadv_bcast_packet *)skb->data;
1453 1454

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

1457 1458
	spin_lock_bh(&bat_priv->bla.bcast_duplist_lock);

1459
	for (i = 0; i < BATADV_DUPLIST_SIZE; i++) {
1460 1461 1462
		curr = (bat_priv->bla.bcast_duplist_curr + i);
		curr %= BATADV_DUPLIST_SIZE;
		entry = &bat_priv->bla.bcast_duplist[curr];
1463 1464 1465 1466

		/* we can stop searching if the entry is too old ;
		 * later entries will be even older
		 */
1467 1468
		if (batadv_has_timed_out(entry->entrytime,
					 BATADV_DUPLIST_TIMEOUT))
1469 1470 1471 1472 1473
			break;

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

1474
		if (batadv_compare_eth(entry->orig, bcast_packet->orig))
1475 1476 1477 1478 1479
			continue;

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

1494 1495 1496 1497
out:
	spin_unlock_bh(&bat_priv->bla.bcast_duplist_lock);

	return ret;
1498 1499
}

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

	if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1518
		return false;
1519 1520

	if (!hash)
1521
		return false;
1522 1523 1524 1525 1526

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

		rcu_read_lock();
1527
		hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
1528 1529
			if (batadv_compare_eth(backbone_gw->orig, orig) &&
			    backbone_gw->vid == vid) {
1530
				rcu_read_unlock();
1531
				return true;
1532 1533 1534 1535 1536
			}
		}
		rcu_read_unlock();
	}

1537
	return false;
1538 1539
}

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

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

	/* first, find out the vid. */
1559
	if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
1560 1561
		return 0;

1562
	vid = batadv_get_vid(skb, hdr_size);
1563 1564

	/* see if this originator is a backbone gw for this VLAN */
1565 1566
	backbone_gw = batadv_backbone_hash_find(orig_node->bat_priv,
						orig_node->orig, vid);
1567 1568 1569
	if (!backbone_gw)
		return 0;

1570
	batadv_backbone_gw_free_ref(backbone_gw);
1571 1572 1573
	return 1;
}

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

1584
	cancel_delayed_work_sync(&bat_priv->bla.work);
1585
	primary_if = batadv_primary_if_get_selected(bat_priv);
1586

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

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

1625
	ethhdr = eth_hdr(skb);
1626

1627
	primary_if = batadv_primary_if_get_selected(bat_priv);
1628 1629 1630 1631 1632 1633
	if (!primary_if)
		goto handled;

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

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

1639
	ether_addr_copy(search_claim.addr, ethhdr->h_source);
1640
	search_claim.vid = vid;
1641
	claim = batadv_claim_hash_find(bat_priv, &search_claim);
1642 1643 1644 1645 1646

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

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

	/* if it is a broadcast ... */
1662 1663 1664 1665 1666 1667 1668
	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.
		 */
1669 1670 1671 1672 1673 1674
		goto handled;
	} else {
		/* seems the client considers us as its best gateway.
		 * send a claim and update the claim table
		 * immediately.
		 */
1675 1676 1677
		batadv_handle_claim(bat_priv, primary_if,
				    primary_if->net_dev->dev_addr,
				    ethhdr->h_source, vid);
1678 1679 1680
		goto allow;
	}
allow:
1681
	batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
1682 1683 1684 1685 1686 1687 1688 1689 1690
	ret = 0;
	goto out;

handled:
	kfree_skb(skb);
	ret = 1;

out:
	if (primary_if)
1691
		batadv_hardif_free_ref(primary_if);
1692
	if (claim)
1693
		batadv_claim_free_ref(claim);
1694 1695 1696
	return ret;
}

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

1722
	primary_if = batadv_primary_if_get_selected(bat_priv);
1723 1724 1725 1726 1727 1728
	if (!primary_if)
		goto out;

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

1729
	if (batadv_bla_process_claim(bat_priv, primary_if, skb))
1730 1731
		goto handled;

1732
	ethhdr = eth_hdr(skb);
1733

1734
	if (unlikely(atomic_read(&bat_priv->bla.num_requests)))
1735 1736 1737 1738
		/* don't allow broadcasts while requests are in flight */
		if (is_multicast_ether_addr(ethhdr->h_dest))
			goto handled;

1739
	ether_addr_copy(search_claim.addr, ethhdr->h_source);
1740 1741
	search_claim.vid = vid;

1742
	claim = batadv_claim_hash_find(bat_priv, &search_claim);
1743 1744 1745 1746 1747 1748

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

	/* check if we are responsible. */
1749 1750
	if (batadv_compare_eth(claim->backbone_gw->orig,
			       primary_if->net_dev->dev_addr)) {
1751 1752 1753
		/* if yes, the client has roamed and we have
		 * to unclaim it.
		 */
1754 1755 1756
		batadv_handle_unclaim(bat_priv, primary_if,
				      primary_if->net_dev->dev_addr,
				      ethhdr->h_source, vid);
1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772
		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:
1773
	batadv_bla_update_own_backbone_gw(bat_priv, primary_if, vid);
1774 1775 1776 1777 1778 1779
	ret = 0;
	goto out;
handled:
	ret = 1;
out:
	if (primary_if)
1780
		batadv_hardif_free_ref(primary_if);
1781
	if (claim)
1782
		batadv_claim_free_ref(claim);
1783 1784
	return ret;
}
1785

1786 1787 1788 1789 1790 1791 1792
/**
 * 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
 */
1793
int batadv_bla_claim_table_seq_print_text(struct seq_file *seq, void *offset)
1794 1795
{
	struct net_device *net_dev = (struct net_device *)seq->private;
1796
	struct batadv_priv *bat_priv = netdev_priv(net_dev);
1797
	struct batadv_hashtable *hash = bat_priv->bla.claim_hash;
1798
	struct batadv_bla_claim *claim;
1799
	struct batadv_hard_iface *primary_if;
1800
	struct hlist_head *head;
1801
	u16 backbone_crc;
1802
	u32 i;
1803
	bool is_own;
1804
	u8 *primary_addr;
1805

1806 1807
	primary_if = batadv_seq_print_text_primary_if_get(seq);
	if (!primary_if)
1808 1809
		goto out;

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

		rcu_read_lock();
1821
		hlist_for_each_entry_rcu(claim, head, hash_entry) {
1822 1823
			is_own = batadv_compare_eth(claim->backbone_gw->orig,
						    primary_addr);
1824 1825 1826 1827

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

1842 1843 1844 1845 1846 1847 1848 1849
/**
 * 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
 */
1850 1851 1852 1853
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);
1854
	struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
1855
	struct batadv_bla_backbone_gw *backbone_gw;
1856 1857 1858
	struct batadv_hard_iface *primary_if;
	struct hlist_head *head;
	int secs, msecs;
1859
	u16 backbone_crc;
1860
	u32 i;
1861
	bool is_own;
1862
	u8 *primary_addr;
1863

1864 1865
	primary_if = batadv_seq_print_text_primary_if_get(seq);
	if (!primary_if)
1866 1867 1868 1869
		goto out;

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

		rcu_read_lock();
1879
		hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
1880 1881 1882 1883 1884 1885 1886 1887 1888 1889
			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;

1890 1891 1892 1893
			spin_lock_bh(&backbone_gw->crc_lock);
			backbone_crc = backbone_gw->crc;
			spin_unlock_bh(&backbone_gw->crc_lock);

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