br_fdb.c 27.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 *	Forwarding database
 *	Linux ethernet bridge
 *
 *	Authors:
 *	Lennert Buytenhek		<buytenh@gnu.org>
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License
 *	as published by the Free Software Foundation; either version
 *	2 of the License, or (at your option) any later version.
 */

#include <linux/kernel.h>
#include <linux/init.h>
16
#include <linux/rculist.h>
L
Linus Torvalds 已提交
17 18 19 20 21
#include <linux/spinlock.h>
#include <linux/times.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/jhash.h>
22
#include <linux/random.h>
23
#include <linux/slab.h>
A
Arun Sharma 已提交
24
#include <linux/atomic.h>
25
#include <asm/unaligned.h>
26
#include <linux/if_vlan.h>
27
#include <net/switchdev.h>
28
#include <trace/events/bridge.h>
L
Linus Torvalds 已提交
29 30
#include "br_private.h"

31 32 33 34 35 36 37 38
static const struct rhashtable_params br_fdb_rht_params = {
	.head_offset = offsetof(struct net_bridge_fdb_entry, rhnode),
	.key_offset = offsetof(struct net_bridge_fdb_entry, key),
	.key_len = sizeof(struct net_bridge_fdb_key),
	.automatic_shrinking = true,
	.locks_mul = 1,
};

39
static struct kmem_cache *br_fdb_cache __read_mostly;
L
Linus Torvalds 已提交
40
static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
41
		      const unsigned char *addr, u16 vid);
42
static void fdb_notify(struct net_bridge *br,
43
		       const struct net_bridge_fdb_entry *, int, bool);
L
Linus Torvalds 已提交
44

45
int __init br_fdb_init(void)
L
Linus Torvalds 已提交
46 47 48 49
{
	br_fdb_cache = kmem_cache_create("bridge_fdb_cache",
					 sizeof(struct net_bridge_fdb_entry),
					 0,
50
					 SLAB_HWCACHE_ALIGN, NULL);
51 52 53 54
	if (!br_fdb_cache)
		return -ENOMEM;

	return 0;
L
Linus Torvalds 已提交
55 56
}

A
Andrew Morton 已提交
57
void br_fdb_fini(void)
L
Linus Torvalds 已提交
58 59 60 61
{
	kmem_cache_destroy(br_fdb_cache);
}

62 63 64 65 66 67 68 69 70
int br_fdb_hash_init(struct net_bridge *br)
{
	return rhashtable_init(&br->fdb_hash_tbl, &br_fdb_rht_params);
}

void br_fdb_hash_fini(struct net_bridge *br)
{
	rhashtable_destroy(&br->fdb_hash_tbl);
}
L
Linus Torvalds 已提交
71 72 73 74

/* if topology_changing then use forward_delay (default 15 sec)
 * otherwise keep longer (default 5 minutes)
 */
75
static inline unsigned long hold_time(const struct net_bridge *br)
L
Linus Torvalds 已提交
76 77 78 79
{
	return br->topology_change ? br->forward_delay : br->ageing_time;
}

80
static inline int has_expired(const struct net_bridge *br,
L
Linus Torvalds 已提交
81 82
				  const struct net_bridge_fdb_entry *fdb)
{
83
	return !fdb->is_static && !fdb->added_by_external_learn &&
84
		time_before_eq(fdb->updated + hold_time(br), jiffies);
L
Linus Torvalds 已提交
85 86
}

87 88 89 90 91 92 93
static void fdb_rcu_free(struct rcu_head *head)
{
	struct net_bridge_fdb_entry *ent
		= container_of(head, struct net_bridge_fdb_entry, rcu);
	kmem_cache_free(br_fdb_cache, ent);
}

94
static struct net_bridge_fdb_entry *fdb_find_rcu(struct rhashtable *tbl,
95 96 97
						 const unsigned char *addr,
						 __u16 vid)
{
98
	struct net_bridge_fdb_key key;
99

100 101
	WARN_ON_ONCE(!rcu_read_lock_held());

102 103
	key.vlan_id = vid;
	memcpy(key.addr.addr, addr, sizeof(key.addr.addr));
104

105
	return rhashtable_lookup(tbl, &key, br_fdb_rht_params);
106 107 108 109 110 111 112 113 114
}

/* requires bridge hash_lock */
static struct net_bridge_fdb_entry *br_fdb_find(struct net_bridge *br,
						const unsigned char *addr,
						__u16 vid)
{
	struct net_bridge_fdb_entry *fdb;

115
	lockdep_assert_held_once(&br->hash_lock);
116

117
	rcu_read_lock();
118
	fdb = fdb_find_rcu(&br->fdb_hash_tbl, addr, vid);
119 120 121 122 123
	rcu_read_unlock();

	return fdb;
}

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
struct net_device *br_fdb_find_port(const struct net_device *br_dev,
				    const unsigned char *addr,
				    __u16 vid)
{
	struct net_bridge_fdb_entry *f;
	struct net_device *dev = NULL;
	struct net_bridge *br;

	ASSERT_RTNL();

	if (!netif_is_bridge_master(br_dev))
		return NULL;

	br = netdev_priv(br_dev);
	f = br_fdb_find(br, addr, vid);
	if (f && f->dst)
		dev = f->dst->dev;

	return dev;
}
EXPORT_SYMBOL_GPL(br_fdb_find_port);

146 147 148 149
struct net_bridge_fdb_entry *br_fdb_find_rcu(struct net_bridge *br,
					     const unsigned char *addr,
					     __u16 vid)
{
150
	return fdb_find_rcu(&br->fdb_hash_tbl, addr, vid);
151 152
}

153 154 155 156 157
/* When a static FDB entry is added, the mac address from the entry is
 * added to the bridge private HW address list and all required ports
 * are then updated with the new information.
 * Called under RTNL.
 */
158
static void fdb_add_hw_addr(struct net_bridge *br, const unsigned char *addr)
159 160
{
	int err;
161
	struct net_bridge_port *p;
162 163 164 165 166 167 168 169 170 171 172 173 174

	ASSERT_RTNL();

	list_for_each_entry(p, &br->port_list, list) {
		if (!br_promisc_port(p)) {
			err = dev_uc_add(p->dev, addr);
			if (err)
				goto undo;
		}
	}

	return;
undo:
175 176 177
	list_for_each_entry_continue_reverse(p, &br->port_list, list) {
		if (!br_promisc_port(p))
			dev_uc_del(p->dev, addr);
178 179 180 181 182 183 184 185
	}
}

/* When a static FDB entry is deleted, the HW address from that entry is
 * also removed from the bridge private HW address list and updates all
 * the ports with needed information.
 * Called under RTNL.
 */
186
static void fdb_del_hw_addr(struct net_bridge *br, const unsigned char *addr)
187 188 189 190 191 192 193 194 195 196 197
{
	struct net_bridge_port *p;

	ASSERT_RTNL();

	list_for_each_entry(p, &br->port_list, list) {
		if (!br_promisc_port(p))
			dev_uc_del(p->dev, addr);
	}
}

198 199
static void fdb_delete(struct net_bridge *br, struct net_bridge_fdb_entry *f,
		       bool swdev_notify)
L
Linus Torvalds 已提交
200
{
201 202
	trace_fdb_delete(br, f);

203
	if (f->is_static)
204
		fdb_del_hw_addr(br, f->key.addr.addr);
205

206 207 208
	hlist_del_init_rcu(&f->fdb_node);
	rhashtable_remove_fast(&br->fdb_hash_tbl, &f->rhnode,
			       br_fdb_rht_params);
209
	fdb_notify(br, f, RTM_DELNEIGH, swdev_notify);
210
	call_rcu(&f->rcu, fdb_rcu_free);
L
Linus Torvalds 已提交
211 212
}

213 214 215 216 217
/* Delete a local entry if no other port had the same address. */
static void fdb_delete_local(struct net_bridge *br,
			     const struct net_bridge_port *p,
			     struct net_bridge_fdb_entry *f)
{
218
	const unsigned char *addr = f->key.addr.addr;
219 220
	struct net_bridge_vlan_group *vg;
	const struct net_bridge_vlan *v;
221
	struct net_bridge_port *op;
222
	u16 vid = f->key.vlan_id;
223 224 225

	/* Maybe another port has same hw addr? */
	list_for_each_entry(op, &br->port_list, list) {
226
		vg = nbp_vlan_group(op);
227
		if (op != p && ether_addr_equal(op->dev->dev_addr, addr) &&
228
		    (!vid || br_vlan_find(vg, vid))) {
229
			f->dst = op;
230
			f->added_by_user = 0;
231 232 233 234
			return;
		}
	}

235 236
	vg = br_vlan_group(br);
	v = br_vlan_find(vg, vid);
237 238
	/* Maybe bridge device has same hw addr? */
	if (p && ether_addr_equal(br->dev->dev_addr, addr) &&
239
	    (!vid || (v && br_vlan_should_use(v)))) {
240
		f->dst = NULL;
241
		f->added_by_user = 0;
242 243 244
		return;
	}

245
	fdb_delete(br, f, true);
246 247
}

248 249 250 251 252 253 254
void br_fdb_find_delete_local(struct net_bridge *br,
			      const struct net_bridge_port *p,
			      const unsigned char *addr, u16 vid)
{
	struct net_bridge_fdb_entry *f;

	spin_lock_bh(&br->hash_lock);
255
	f = br_fdb_find(br, addr, vid);
256 257 258 259 260
	if (f && f->is_local && !f->added_by_user && f->dst == p)
		fdb_delete_local(br, p, f);
	spin_unlock_bh(&br->hash_lock);
}

L
Linus Torvalds 已提交
261 262
void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr)
{
263
	struct net_bridge_vlan_group *vg;
264
	struct net_bridge_fdb_entry *f;
L
Linus Torvalds 已提交
265
	struct net_bridge *br = p->br;
266
	struct net_bridge_vlan *v;
267

L
Linus Torvalds 已提交
268
	spin_lock_bh(&br->hash_lock);
269
	vg = nbp_vlan_group(p);
270 271 272 273 274 275 276 277 278 279 280
	hlist_for_each_entry(f, &br->fdb_list, fdb_node) {
		if (f->dst == p && f->is_local && !f->added_by_user) {
			/* delete old one */
			fdb_delete_local(br, p, f);

			/* if this port has no vlan information
			 * configured, we can safely be done at
			 * this point.
			 */
			if (!vg || !vg->num_vlans)
				goto insert;
L
Linus Torvalds 已提交
281 282 283
		}
	}

284 285 286 287
insert:
	/* insert new address,  may fail if invalid address or dup. */
	fdb_insert(br, p, newaddr, 0);

288
	if (!vg || !vg->num_vlans)
289 290 291 292 293 294
		goto done;

	/* Now add entries for every VLAN configured on the port.
	 * This function runs under RTNL so the bitmap will not change
	 * from under us.
	 */
295 296
	list_for_each_entry(v, &vg->vlan_list, vlist)
		fdb_insert(br, p, newaddr, v->vid);
297

298
done:
L
Linus Torvalds 已提交
299 300 301
	spin_unlock_bh(&br->hash_lock);
}

302 303
void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr)
{
304
	struct net_bridge_vlan_group *vg;
305
	struct net_bridge_fdb_entry *f;
306
	struct net_bridge_vlan *v;
307

308 309
	spin_lock_bh(&br->hash_lock);

310
	/* If old entry was unassociated with any port, then delete it. */
311
	f = br_fdb_find(br, br->dev->dev_addr, 0);
312
	if (f && f->is_local && !f->dst && !f->added_by_user)
313
		fdb_delete_local(br, NULL, f);
314

315
	fdb_insert(br, NULL, newaddr, 0);
316 317 318
	vg = br_vlan_group(br);
	if (!vg || !vg->num_vlans)
		goto out;
319 320 321 322
	/* Now remove and add entries for every VLAN configured on the
	 * bridge.  This function runs under RTNL so the bitmap will not
	 * change from under us.
	 */
323
	list_for_each_entry(v, &vg->vlan_list, vlist) {
324 325
		if (!br_vlan_should_use(v))
			continue;
326
		f = br_fdb_find(br, br->dev->dev_addr, v->vid);
327
		if (f && f->is_local && !f->dst && !f->added_by_user)
328
			fdb_delete_local(br, NULL, f);
329
		fdb_insert(br, NULL, newaddr, v->vid);
330
	}
331 332
out:
	spin_unlock_bh(&br->hash_lock);
333 334
}

335
void br_fdb_cleanup(struct work_struct *work)
L
Linus Torvalds 已提交
336
{
337 338
	struct net_bridge *br = container_of(work, struct net_bridge,
					     gc_work.work);
339
	struct net_bridge_fdb_entry *f = NULL;
L
Linus Torvalds 已提交
340
	unsigned long delay = hold_time(br);
341 342
	unsigned long work_delay = delay;
	unsigned long now = jiffies;
L
Linus Torvalds 已提交
343

344 345 346 347 348 349 350
	/* this part is tricky, in order to avoid blocking learning and
	 * consequently forwarding, we rely on rcu to delete objects with
	 * delayed freeing allowing us to continue traversing
	 */
	rcu_read_lock();
	hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
		unsigned long this_timer;
L
Linus Torvalds 已提交
351

352
		if (f->is_static || f->added_by_external_learn)
353
			continue;
354 355 356 357 358 359
		this_timer = f->updated + delay;
		if (time_after(this_timer, now)) {
			work_delay = min(work_delay, this_timer - now);
		} else {
			spin_lock_bh(&br->hash_lock);
			if (!hlist_unhashed(&f->fdb_node))
360
				fdb_delete(br, f, true);
361
			spin_unlock_bh(&br->hash_lock);
L
Linus Torvalds 已提交
362 363
		}
	}
364
	rcu_read_unlock();
L
Linus Torvalds 已提交
365

366 367 368
	/* Cleanup minimum 10 milliseconds apart */
	work_delay = max_t(unsigned long, work_delay, msecs_to_jiffies(10));
	mod_delayed_work(system_long_wq, &br->gc_work, work_delay);
L
Linus Torvalds 已提交
369 370
}

371 372 373
/* Completely flush all dynamic entries in forwarding database.*/
void br_fdb_flush(struct net_bridge *br)
{
374 375
	struct net_bridge_fdb_entry *f;
	struct hlist_node *tmp;
376 377

	spin_lock_bh(&br->hash_lock);
378 379
	hlist_for_each_entry_safe(f, tmp, &br->fdb_list, fdb_node) {
		if (!f->is_static)
380
			fdb_delete(br, f, true);
381 382 383
	}
	spin_unlock_bh(&br->hash_lock);
}
384

L
Lucas De Marchi 已提交
385
/* Flush all entries referring to a specific port.
386
 * if do_all is set also flush static entries
387
 * if vid is set delete all entries that match the vlan_id
388
 */
389 390
void br_fdb_delete_by_port(struct net_bridge *br,
			   const struct net_bridge_port *p,
391
			   u16 vid,
392
			   int do_all)
L
Linus Torvalds 已提交
393
{
394 395
	struct net_bridge_fdb_entry *f;
	struct hlist_node *tmp;
L
Linus Torvalds 已提交
396 397

	spin_lock_bh(&br->hash_lock);
398 399 400
	hlist_for_each_entry_safe(f, tmp, &br->fdb_list, fdb_node) {
		if (f->dst != p)
			continue;
401

402 403
		if (!do_all)
			if (f->is_static || (vid && f->key.vlan_id != vid))
L
Linus Torvalds 已提交
404 405
				continue;

406 407 408
		if (f->is_local)
			fdb_delete_local(br, p, f);
		else
409
			fdb_delete(br, f, true);
L
Linus Torvalds 已提交
410 411 412 413
	}
	spin_unlock_bh(&br->hash_lock);
}

I
Igor Maravić 已提交
414
#if IS_ENABLED(CONFIG_ATM_LANE)
415 416 417
/* Interface used by ATM LANE hook to test
 * if an addr is on some other bridge port */
int br_fdb_test_addr(struct net_device *dev, unsigned char *addr)
L
Linus Torvalds 已提交
418 419
{
	struct net_bridge_fdb_entry *fdb;
420
	struct net_bridge_port *port;
421 422
	int ret;

L
Linus Torvalds 已提交
423
	rcu_read_lock();
424 425 426 427
	port = br_port_get_rcu(dev);
	if (!port)
		ret = 0;
	else {
428
		fdb = br_fdb_find_rcu(port->br, addr, 0);
429
		ret = fdb && fdb->dst && fdb->dst->dev != dev &&
430 431
			fdb->dst->state == BR_STATE_FORWARDING;
	}
L
Linus Torvalds 已提交
432 433
	rcu_read_unlock();

434
	return ret;
L
Linus Torvalds 已提交
435
}
436
#endif /* CONFIG_ATM_LANE */
L
Linus Torvalds 已提交
437 438

/*
439
 * Fill buffer with forwarding table records in
L
Linus Torvalds 已提交
440 441 442 443 444 445
 * the API format.
 */
int br_fdb_fillbuf(struct net_bridge *br, void *buf,
		   unsigned long maxnum, unsigned long skip)
{
	struct net_bridge_fdb_entry *f;
446 447
	struct __fdb_entry *fe = buf;
	int num = 0;
L
Linus Torvalds 已提交
448 449 450 451

	memset(buf, 0, maxnum*sizeof(struct __fdb_entry));

	rcu_read_lock();
452 453 454
	hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
		if (num >= maxnum)
			break;
L
Linus Torvalds 已提交
455

456 457
		if (has_expired(br, f))
			continue;
L
Linus Torvalds 已提交
458

459 460 461
		/* ignore pseudo entry for local MAC address */
		if (!f->dst)
			continue;
462

463 464 465 466
		if (skip) {
			--skip;
			continue;
		}
L
Linus Torvalds 已提交
467

468 469
		/* convert from internal format to API */
		memcpy(fe->mac_addr, f->key.addr.addr, ETH_ALEN);
470

471 472 473
		/* due to ABI compat need to split into hi/lo */
		fe->port_no = f->dst->port_no;
		fe->port_hi = f->dst->port_no >> 8;
474

475 476 477 478 479
		fe->is_local = f->is_local;
		if (!f->is_static)
			fe->ageing_timer_value = jiffies_delta_to_clock_t(jiffies - f->updated);
		++fe;
		++num;
L
Linus Torvalds 已提交
480 481 482 483 484 485
	}
	rcu_read_unlock();

	return num;
}

486
static struct net_bridge_fdb_entry *fdb_create(struct net_bridge *br,
L
Linus Torvalds 已提交
487
					       struct net_bridge_port *source,
488
					       const unsigned char *addr,
489 490 491
					       __u16 vid,
					       unsigned char is_local,
					       unsigned char is_static)
L
Linus Torvalds 已提交
492 493 494 495 496
{
	struct net_bridge_fdb_entry *fdb;

	fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC);
	if (fdb) {
497
		memcpy(fdb->key.addr.addr, addr, ETH_ALEN);
L
Linus Torvalds 已提交
498
		fdb->dst = source;
499
		fdb->key.vlan_id = vid;
500 501
		fdb->is_local = is_local;
		fdb->is_static = is_static;
502
		fdb->added_by_user = 0;
503
		fdb->added_by_external_learn = 0;
504
		fdb->offloaded = 0;
505
		fdb->updated = fdb->used = jiffies;
506 507 508 509 510 511 512 513
		if (rhashtable_lookup_insert_fast(&br->fdb_hash_tbl,
						  &fdb->rhnode,
						  br_fdb_rht_params)) {
			kmem_cache_free(br_fdb_cache, fdb);
			fdb = NULL;
		} else {
			hlist_add_head_rcu(&fdb->fdb_node, &br->fdb_list);
		}
L
Linus Torvalds 已提交
514 515 516 517 518
	}
	return fdb;
}

static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
519
		  const unsigned char *addr, u16 vid)
L
Linus Torvalds 已提交
520 521 522 523 524 525
{
	struct net_bridge_fdb_entry *fdb;

	if (!is_valid_ether_addr(addr))
		return -EINVAL;

526
	fdb = br_fdb_find(br, addr, vid);
L
Linus Torvalds 已提交
527
	if (fdb) {
528
		/* it is okay to have multiple ports with same
L
Linus Torvalds 已提交
529 530
		 * address, just use the first one.
		 */
531
		if (fdb->is_local)
L
Linus Torvalds 已提交
532
			return 0;
533 534
		br_warn(br, "adding interface %s with same address as a received packet (addr:%pM, vlan:%u)\n",
		       source ? source->dev->name : br->dev->name, addr, vid);
535
		fdb_delete(br, fdb, true);
536
	}
L
Linus Torvalds 已提交
537

538
	fdb = fdb_create(br, source, addr, vid, 1, 1);
539
	if (!fdb)
L
Linus Torvalds 已提交
540 541
		return -ENOMEM;

542
	fdb_add_hw_addr(br, addr);
543
	fdb_notify(br, fdb, RTM_NEWNEIGH, true);
L
Linus Torvalds 已提交
544 545 546
	return 0;
}

547
/* Add entry for local address of interface */
L
Linus Torvalds 已提交
548
int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
549
		  const unsigned char *addr, u16 vid)
L
Linus Torvalds 已提交
550 551 552 553
{
	int ret;

	spin_lock_bh(&br->hash_lock);
554
	ret = fdb_insert(br, source, addr, vid);
L
Linus Torvalds 已提交
555 556 557 558 559
	spin_unlock_bh(&br->hash_lock);
	return ret;
}

void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
560
		   const unsigned char *addr, u16 vid, bool added_by_user)
L
Linus Torvalds 已提交
561 562
{
	struct net_bridge_fdb_entry *fdb;
563
	bool fdb_modified = false;
L
Linus Torvalds 已提交
564 565 566 567 568

	/* some users want to always flood. */
	if (hold_time(br) == 0)
		return;

569 570 571 572 573
	/* ignore packets unless we are using this port */
	if (!(source->state == BR_STATE_LEARNING ||
	      source->state == BR_STATE_FORWARDING))
		return;

574
	fdb = fdb_find_rcu(&br->fdb_hash_tbl, addr, vid);
L
Linus Torvalds 已提交
575 576 577
	if (likely(fdb)) {
		/* attempt to update an entry for a local interface */
		if (unlikely(fdb->is_local)) {
578
			if (net_ratelimit())
579 580
				br_warn(br, "received packet on %s with own address as source address (addr:%pM, vlan:%u)\n",
					source->dev->name, addr, vid);
L
Linus Torvalds 已提交
581
		} else {
582 583
			unsigned long now = jiffies;

L
Linus Torvalds 已提交
584
			/* fastpath: update of existing entry */
585 586 587
			if (unlikely(source != fdb->dst)) {
				fdb->dst = source;
				fdb_modified = true;
588 589 590
				/* Take over HW learned entry */
				if (unlikely(fdb->added_by_external_learn))
					fdb->added_by_external_learn = 0;
591
			}
592 593
			if (now != fdb->updated)
				fdb->updated = now;
594 595
			if (unlikely(added_by_user))
				fdb->added_by_user = 1;
596 597
			if (unlikely(fdb_modified)) {
				trace_br_fdb_update(br, source, addr, vid, added_by_user);
598
				fdb_notify(br, fdb, RTM_NEWNEIGH, true);
599
			}
L
Linus Torvalds 已提交
600 601
		}
	} else {
602
		spin_lock(&br->hash_lock);
603 604 605 606 607 608
		fdb = fdb_create(br, source, addr, vid, 0, 0);
		if (fdb) {
			if (unlikely(added_by_user))
				fdb->added_by_user = 1;
			trace_br_fdb_update(br, source, addr, vid,
					    added_by_user);
609
			fdb_notify(br, fdb, RTM_NEWNEIGH, true);
S
stephen hemminger 已提交
610
		}
L
Linus Torvalds 已提交
611 612 613
		/* else  we lose race and someone else inserts
		 * it first, don't bother updating
		 */
614
		spin_unlock(&br->hash_lock);
L
Linus Torvalds 已提交
615 616
	}
}
617

618 619
static int fdb_to_nud(const struct net_bridge *br,
		      const struct net_bridge_fdb_entry *fdb)
620 621 622 623 624
{
	if (fdb->is_local)
		return NUD_PERMANENT;
	else if (fdb->is_static)
		return NUD_NOARP;
625
	else if (has_expired(br, fdb))
626 627 628 629 630
		return NUD_STALE;
	else
		return NUD_REACHABLE;
}

631
static int fdb_fill_info(struct sk_buff *skb, const struct net_bridge *br,
632
			 const struct net_bridge_fdb_entry *fdb,
633
			 u32 portid, u32 seq, int type, unsigned int flags)
634 635 636 637 638 639
{
	unsigned long now = jiffies;
	struct nda_cacheinfo ci;
	struct nlmsghdr *nlh;
	struct ndmsg *ndm;

640
	nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
641 642 643 644 645 646 647
	if (nlh == NULL)
		return -EMSGSIZE;

	ndm = nlmsg_data(nlh);
	ndm->ndm_family	 = AF_BRIDGE;
	ndm->ndm_pad1    = 0;
	ndm->ndm_pad2    = 0;
648
	ndm->ndm_flags	 = 0;
649
	ndm->ndm_type	 = 0;
650
	ndm->ndm_ifindex = fdb->dst ? fdb->dst->dev->ifindex : br->dev->ifindex;
651
	ndm->ndm_state   = fdb_to_nud(br, fdb);
652

653 654 655 656 657
	if (fdb->offloaded)
		ndm->ndm_flags |= NTF_OFFLOADED;
	if (fdb->added_by_external_learn)
		ndm->ndm_flags |= NTF_EXT_LEARNED;

658
	if (nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->key.addr))
D
David S. Miller 已提交
659
		goto nla_put_failure;
660 661
	if (nla_put_u32(skb, NDA_MASTER, br->dev->ifindex))
		goto nla_put_failure;
662 663 664 665
	ci.ndm_used	 = jiffies_to_clock_t(now - fdb->used);
	ci.ndm_confirmed = 0;
	ci.ndm_updated	 = jiffies_to_clock_t(now - fdb->updated);
	ci.ndm_refcnt	 = 0;
D
David S. Miller 已提交
666 667
	if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
		goto nla_put_failure;
668

669 670
	if (fdb->key.vlan_id && nla_put(skb, NDA_VLAN, sizeof(u16),
					&fdb->key.vlan_id))
671 672
		goto nla_put_failure;

673 674
	nlmsg_end(skb, nlh);
	return 0;
675 676 677 678 679 680 681 682 683 684

nla_put_failure:
	nlmsg_cancel(skb, nlh);
	return -EMSGSIZE;
}

static inline size_t fdb_nlmsg_size(void)
{
	return NLMSG_ALIGN(sizeof(struct ndmsg))
		+ nla_total_size(ETH_ALEN) /* NDA_LLADDR */
685
		+ nla_total_size(sizeof(u32)) /* NDA_MASTER */
686
		+ nla_total_size(sizeof(u16)) /* NDA_VLAN */
687 688 689
		+ nla_total_size(sizeof(struct nda_cacheinfo));
}

690
static void fdb_notify(struct net_bridge *br,
691 692
		       const struct net_bridge_fdb_entry *fdb, int type,
		       bool swdev_notify)
693
{
694
	struct net *net = dev_net(br->dev);
695 696 697
	struct sk_buff *skb;
	int err = -ENOBUFS;

698 699
	if (swdev_notify)
		br_switchdev_fdb_notify(fdb, type);
700

701 702 703 704
	skb = nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC);
	if (skb == NULL)
		goto errout;

705
	err = fdb_fill_info(skb, br, fdb, 0, 0, type, 0);
706 707 708 709 710 711 712 713 714
	if (err < 0) {
		/* -EMSGSIZE implies BUG in fdb_nlmsg_size() */
		WARN_ON(err == -EMSGSIZE);
		kfree_skb(skb);
		goto errout;
	}
	rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
	return;
errout:
715
	rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
716 717 718
}

/* Dump information about entries, in response to GETNEIGH */
719 720 721
int br_fdb_dump(struct sk_buff *skb,
		struct netlink_callback *cb,
		struct net_device *dev,
722
		struct net_device *filter_dev,
723
		int *idx)
724
{
725
	struct net_bridge *br = netdev_priv(dev);
726
	struct net_bridge_fdb_entry *f;
727
	int err = 0;
728

729
	if (!(dev->priv_flags & IFF_EBRIDGE))
730
		return err;
731

732 733 734
	if (!filter_dev) {
		err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
		if (err < 0)
735
			return err;
736
	}
737

738 739 740 741 742 743
	rcu_read_lock();
	hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
		if (*idx < cb->args[2])
			goto skip;
		if (filter_dev && (!f->dst || f->dst->dev != filter_dev)) {
			if (filter_dev != dev)
744
				goto skip;
745 746 747 748 749 750
			/* !f->dst is a special case for bridge
			 * It means the MAC belongs to the bridge
			 * Therefore need a little more filtering
			 * we only want to dump the !f->dst case
			 */
			if (f->dst)
751
				goto skip;
752
		}
753 754 755 756 757 758 759 760 761 762 763 764
		if (!filter_dev && f->dst)
			goto skip;

		err = fdb_fill_info(skb, br, f,
				    NETLINK_CB(cb->skb).portid,
				    cb->nlh->nlmsg_seq,
				    RTM_NEWNEIGH,
				    NLM_F_MULTI);
		if (err < 0)
			break;
skip:
		*idx += 1;
765
	}
766
	rcu_read_unlock();
767

768
	return err;
769
}
770

S
stephen hemminger 已提交
771
/* Update (create or replace) forwarding database entry */
772 773
static int fdb_add_entry(struct net_bridge *br, struct net_bridge_port *source,
			 const __u8 *addr, __u16 state, __u16 flags, __u16 vid)
774 775
{
	struct net_bridge_fdb_entry *fdb;
776
	bool modified = false;
777

778
	/* If the port cannot learn allow only local and static entries */
779
	if (source && !(state & NUD_PERMANENT) && !(state & NUD_NOARP) &&
780 781 782 783
	    !(source->state == BR_STATE_LEARNING ||
	      source->state == BR_STATE_FORWARDING))
		return -EPERM;

784 785 786 787 788 789
	if (!source && !(state & NUD_PERMANENT)) {
		pr_info("bridge: RTM_NEWNEIGH %s without NUD_PERMANENT\n",
			br->dev->name);
		return -EINVAL;
	}

790
	fdb = br_fdb_find(br, addr, vid);
791 792 793
	if (fdb == NULL) {
		if (!(flags & NLM_F_CREATE))
			return -ENOENT;
794

795
		fdb = fdb_create(br, source, addr, vid, 0, 0);
796 797
		if (!fdb)
			return -ENOMEM;
798 799

		modified = true;
800 801 802
	} else {
		if (flags & NLM_F_EXCL)
			return -EEXIST;
803 804 805 806 807

		if (fdb->dst != source) {
			fdb->dst = source;
			modified = true;
		}
S
stephen hemminger 已提交
808 809
	}

810
	if (fdb_to_nud(br, fdb) != state) {
811 812 813 814
		if (state & NUD_PERMANENT) {
			fdb->is_local = 1;
			if (!fdb->is_static) {
				fdb->is_static = 1;
815
				fdb_add_hw_addr(br, addr);
816 817 818 819 820
			}
		} else if (state & NUD_NOARP) {
			fdb->is_local = 0;
			if (!fdb->is_static) {
				fdb->is_static = 1;
821
				fdb_add_hw_addr(br, addr);
822 823
			}
		} else {
S
stephen hemminger 已提交
824
			fdb->is_local = 0;
825 826
			if (fdb->is_static) {
				fdb->is_static = 0;
827
				fdb_del_hw_addr(br, addr);
828 829
			}
		}
830

831 832
		modified = true;
	}
833
	fdb->added_by_user = 1;
834 835 836 837

	fdb->used = jiffies;
	if (modified) {
		fdb->updated = jiffies;
838
		fdb_notify(br, fdb, RTM_NEWNEIGH, true);
839
	}
840 841 842 843

	return 0;
}

844 845 846
static int __br_fdb_add(struct ndmsg *ndm, struct net_bridge *br,
			struct net_bridge_port *p, const unsigned char *addr,
			u16 nlh_flags, u16 vid)
847 848 849 850
{
	int err = 0;

	if (ndm->ndm_flags & NTF_USE) {
851 852 853 854 855
		if (!p) {
			pr_info("bridge: RTM_NEWNEIGH %s with NTF_USE is not supported\n",
				br->dev->name);
			return -EINVAL;
		}
856
		local_bh_disable();
857
		rcu_read_lock();
858
		br_fdb_update(br, p, addr, vid, true);
859
		rcu_read_unlock();
860
		local_bh_enable();
861
	} else if (ndm->ndm_flags & NTF_EXT_LEARNED) {
862
		err = br_fdb_external_learn_add(br, p, addr, vid, true);
863
	} else {
864 865
		spin_lock_bh(&br->hash_lock);
		err = fdb_add_entry(br, p, addr, ndm->ndm_state,
866
				    nlh_flags, vid);
867
		spin_unlock_bh(&br->hash_lock);
868 869 870 871 872
	}

	return err;
}

873
/* Add new permanent fdb entry with RTM_NEWNEIGH */
874 875
int br_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
	       struct net_device *dev,
876
	       const unsigned char *addr, u16 vid, u16 nlh_flags)
877
{
878
	struct net_bridge_vlan_group *vg;
879
	struct net_bridge_port *p = NULL;
880
	struct net_bridge_vlan *v;
881
	struct net_bridge *br = NULL;
882
	int err = 0;
883

884 885
	trace_br_fdb_add(ndm, dev, addr, vid, nlh_flags);

S
stephen hemminger 已提交
886 887 888 889 890
	if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE))) {
		pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm->ndm_state);
		return -EINVAL;
	}

891 892 893 894 895
	if (is_zero_ether_addr(addr)) {
		pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n");
		return -EINVAL;
	}

896 897 898 899 900 901 902 903 904 905
	if (dev->priv_flags & IFF_EBRIDGE) {
		br = netdev_priv(dev);
		vg = br_vlan_group(br);
	} else {
		p = br_port_get_rtnl(dev);
		if (!p) {
			pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n",
				dev->name);
			return -EINVAL;
		}
906
		br = p->br;
907
		vg = nbp_vlan_group(p);
908 909
	}

910
	if (vid) {
911
		v = br_vlan_find(vg, vid);
912 913
		if (!v || !br_vlan_should_use(v)) {
			pr_info("bridge: RTM_NEWNEIGH with unconfigured vlan %d on %s\n", vid, dev->name);
914 915 916 917
			return -EINVAL;
		}

		/* VID was specified, so use it. */
918
		err = __br_fdb_add(ndm, br, p, addr, nlh_flags, vid);
S
stephen hemminger 已提交
919
	} else {
920
		err = __br_fdb_add(ndm, br, p, addr, nlh_flags, 0);
921
		if (err || !vg || !vg->num_vlans)
922 923 924 925 926 927
			goto out;

		/* We have vlans configured on this port and user didn't
		 * specify a VLAN.  To be nice, add/update entry for every
		 * vlan on this port.
		 */
928
		list_for_each_entry(v, &vg->vlan_list, vlist) {
929 930
			if (!br_vlan_should_use(v))
				continue;
931
			err = __br_fdb_add(ndm, br, p, addr, nlh_flags, v->vid);
932 933 934
			if (err)
				goto out;
		}
S
stephen hemminger 已提交
935
	}
936

937
out:
938 939 940
	return err;
}

941 942
static int fdb_delete_by_addr_and_port(struct net_bridge *br,
				       const struct net_bridge_port *p,
943
				       const u8 *addr, u16 vlan)
944 945 946
{
	struct net_bridge_fdb_entry *fdb;

947
	fdb = br_fdb_find(br, addr, vlan);
948
	if (!fdb || fdb->dst != p)
949 950
		return -ENOENT;

951
	fdb_delete(br, fdb, true);
952

953 954 955
	return 0;
}

956 957
static int __br_fdb_delete(struct net_bridge *br,
			   const struct net_bridge_port *p,
958 959 960 961
			   const unsigned char *addr, u16 vid)
{
	int err;

962 963 964
	spin_lock_bh(&br->hash_lock);
	err = fdb_delete_by_addr_and_port(br, p, addr, vid);
	spin_unlock_bh(&br->hash_lock);
965 966 967 968

	return err;
}

969
/* Remove neighbor entry with RTM_DELNEIGH */
970 971
int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
		  struct net_device *dev,
972
		  const unsigned char *addr, u16 vid)
973
{
974
	struct net_bridge_vlan_group *vg;
975
	struct net_bridge_port *p = NULL;
976
	struct net_bridge_vlan *v;
977
	struct net_bridge *br;
978
	int err;
979

980 981 982 983 984 985 986 987 988 989 990
	if (dev->priv_flags & IFF_EBRIDGE) {
		br = netdev_priv(dev);
		vg = br_vlan_group(br);
	} else {
		p = br_port_get_rtnl(dev);
		if (!p) {
			pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n",
				dev->name);
			return -EINVAL;
		}
		vg = nbp_vlan_group(p);
991
		br = p->br;
992 993
	}

994
	if (vid) {
995 996
		v = br_vlan_find(vg, vid);
		if (!v) {
997
			pr_info("bridge: RTM_DELNEIGH with unconfigured vlan %d on %s\n", vid, dev->name);
998 999
			return -EINVAL;
		}
1000

1001
		err = __br_fdb_delete(br, p, addr, vid);
1002
	} else {
1003
		err = -ENOENT;
1004
		err &= __br_fdb_delete(br, p, addr, 0);
1005
		if (!vg || !vg->num_vlans)
1006
			return err;
1007

1008 1009 1010
		list_for_each_entry(v, &vg->vlan_list, vlist) {
			if (!br_vlan_should_use(v))
				continue;
1011
			err &= __br_fdb_delete(br, p, addr, v->vid);
1012
		}
1013
	}
1014

1015 1016
	return err;
}
1017 1018 1019

int br_fdb_sync_static(struct net_bridge *br, struct net_bridge_port *p)
{
1020
	struct net_bridge_fdb_entry *f, *tmp;
1021
	int err = 0;
1022 1023 1024

	ASSERT_RTNL();

1025 1026 1027 1028 1029 1030 1031 1032 1033
	/* the key here is that static entries change only under rtnl */
	rcu_read_lock();
	hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
		/* We only care for static entries */
		if (!f->is_static)
			continue;
		err = dev_uc_add(p->dev, f->key.addr.addr);
		if (err)
			goto rollback;
1034
	}
1035 1036
done:
	rcu_read_unlock();
1037

1038
	return err;
1039

1040 1041 1042 1043 1044 1045 1046 1047
rollback:
	hlist_for_each_entry_rcu(tmp, &br->fdb_list, fdb_node) {
		/* We only care for static entries */
		if (!tmp->is_static)
			continue;
		if (tmp == f)
			break;
		dev_uc_del(p->dev, tmp->key.addr.addr);
1048
	}
1049 1050

	goto done;
1051 1052 1053 1054
}

void br_fdb_unsync_static(struct net_bridge *br, struct net_bridge_port *p)
{
1055
	struct net_bridge_fdb_entry *f;
1056 1057 1058

	ASSERT_RTNL();

1059 1060 1061 1062 1063
	rcu_read_lock();
	hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
		/* We only care for static entries */
		if (!f->is_static)
			continue;
1064

1065
		dev_uc_del(p->dev, f->key.addr.addr);
1066
	}
1067
	rcu_read_unlock();
1068
}
1069

1070
int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p,
1071 1072
			      const unsigned char *addr, u16 vid,
			      bool swdev_notify)
1073 1074
{
	struct net_bridge_fdb_entry *fdb;
1075
	bool modified = false;
1076 1077
	int err = 0;

1078 1079
	trace_br_fdb_external_learn_add(br, p, addr, vid);

1080 1081
	spin_lock_bh(&br->hash_lock);

1082
	fdb = br_fdb_find(br, addr, vid);
1083
	if (!fdb) {
1084
		fdb = fdb_create(br, p, addr, vid, 0, 0);
1085 1086 1087 1088 1089
		if (!fdb) {
			err = -ENOMEM;
			goto err_unlock;
		}
		fdb->added_by_external_learn = 1;
1090
		fdb_notify(br, fdb, RTM_NEWNEIGH, swdev_notify);
1091
	} else {
1092
		fdb->updated = jiffies;
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108

		if (fdb->dst != p) {
			fdb->dst = p;
			modified = true;
		}

		if (fdb->added_by_external_learn) {
			/* Refresh entry */
			fdb->used = jiffies;
		} else if (!fdb->added_by_user) {
			/* Take over SW learned entry */
			fdb->added_by_external_learn = 1;
			modified = true;
		}

		if (modified)
1109
			fdb_notify(br, fdb, RTM_NEWNEIGH, swdev_notify);
1110 1111 1112 1113 1114 1115 1116 1117
	}

err_unlock:
	spin_unlock_bh(&br->hash_lock);

	return err;
}

1118
int br_fdb_external_learn_del(struct net_bridge *br, struct net_bridge_port *p,
1119 1120
			      const unsigned char *addr, u16 vid,
			      bool swdev_notify)
1121 1122 1123 1124 1125 1126
{
	struct net_bridge_fdb_entry *fdb;
	int err = 0;

	spin_lock_bh(&br->hash_lock);

1127
	fdb = br_fdb_find(br, addr, vid);
1128
	if (fdb && fdb->added_by_external_learn)
1129
		fdb_delete(br, fdb, swdev_notify);
1130 1131 1132 1133 1134 1135 1136
	else
		err = -ENOENT;

	spin_unlock_bh(&br->hash_lock);

	return err;
}
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150

void br_fdb_offloaded_set(struct net_bridge *br, struct net_bridge_port *p,
			  const unsigned char *addr, u16 vid)
{
	struct net_bridge_fdb_entry *fdb;

	spin_lock_bh(&br->hash_lock);

	fdb = br_fdb_find(br, addr, vid);
	if (fdb)
		fdb->offloaded = 1;

	spin_unlock_bh(&br->hash_lock);
}