br_fdb.c 28.3 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>
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
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);
138 139
	rcu_read_lock();
	f = br_fdb_find_rcu(br, addr, vid);
140 141
	if (f && f->dst)
		dev = f->dst->dev;
142
	rcu_read_unlock();
143 144 145 146 147

	return dev;
}
EXPORT_SYMBOL_GPL(br_fdb_find_port);

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

155 156 157 158 159
/* 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.
 */
160
static void fdb_add_hw_addr(struct net_bridge *br, const unsigned char *addr)
161 162
{
	int err;
163
	struct net_bridge_port *p;
164 165 166 167 168 169 170 171 172 173 174 175 176

	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:
177 178 179
	list_for_each_entry_continue_reverse(p, &br->port_list, list) {
		if (!br_promisc_port(p))
			dev_uc_del(p->dev, addr);
180 181 182 183 184 185 186 187
	}
}

/* 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.
 */
188
static void fdb_del_hw_addr(struct net_bridge *br, const unsigned char *addr)
189 190 191 192 193 194 195 196 197 198 199
{
	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);
	}
}

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

205
	if (f->is_static)
206
		fdb_del_hw_addr(br, f->key.addr.addr);
207

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

215 216 217 218 219
/* 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)
{
220
	const unsigned char *addr = f->key.addr.addr;
221 222
	struct net_bridge_vlan_group *vg;
	const struct net_bridge_vlan *v;
223
	struct net_bridge_port *op;
224
	u16 vid = f->key.vlan_id;
225 226 227

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

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

247
	fdb_delete(br, f, true);
248 249
}

250 251 252 253 254 255 256
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);
257
	f = br_fdb_find(br, addr, vid);
258 259 260 261 262
	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 已提交
263 264
void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr)
{
265
	struct net_bridge_vlan_group *vg;
266
	struct net_bridge_fdb_entry *f;
L
Linus Torvalds 已提交
267
	struct net_bridge *br = p->br;
268
	struct net_bridge_vlan *v;
269

L
Linus Torvalds 已提交
270
	spin_lock_bh(&br->hash_lock);
271
	vg = nbp_vlan_group(p);
272 273 274 275 276 277 278 279 280 281 282
	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 已提交
283 284 285
		}
	}

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

290
	if (!vg || !vg->num_vlans)
291 292 293 294 295 296
		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.
	 */
297 298
	list_for_each_entry(v, &vg->vlan_list, vlist)
		fdb_insert(br, p, newaddr, v->vid);
299

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

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

310 311
	spin_lock_bh(&br->hash_lock);

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

317
	fdb_insert(br, NULL, newaddr, 0);
318 319 320
	vg = br_vlan_group(br);
	if (!vg || !vg->num_vlans)
		goto out;
321 322 323 324
	/* 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.
	 */
325
	list_for_each_entry(v, &vg->vlan_list, vlist) {
326 327
		if (!br_vlan_should_use(v))
			continue;
328
		f = br_fdb_find(br, br->dev->dev_addr, v->vid);
329
		if (f && f->is_local && !f->dst && !f->added_by_user)
330
			fdb_delete_local(br, NULL, f);
331
		fdb_insert(br, NULL, newaddr, v->vid);
332
	}
333 334
out:
	spin_unlock_bh(&br->hash_lock);
335 336
}

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

346 347 348 349 350 351 352
	/* 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 已提交
353

354
		if (f->is_static || f->added_by_external_learn)
355
			continue;
356 357 358 359 360 361
		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))
362
				fdb_delete(br, f, true);
363
			spin_unlock_bh(&br->hash_lock);
L
Linus Torvalds 已提交
364 365
		}
	}
366
	rcu_read_unlock();
L
Linus Torvalds 已提交
367

368 369 370
	/* 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 已提交
371 372
}

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

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

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

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

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

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

416
#if IS_ENABLED(CONFIG_ATM_LANE)
417 418 419
/* 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 已提交
420 421
{
	struct net_bridge_fdb_entry *fdb;
422
	struct net_bridge_port *port;
423 424
	int ret;

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

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

/*
441
 * Fill buffer with forwarding table records in
L
Linus Torvalds 已提交
442 443 444 445 446 447
 * 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;
448 449
	struct __fdb_entry *fe = buf;
	int num = 0;
L
Linus Torvalds 已提交
450 451 452 453

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

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

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

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

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

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

473 474 475
		/* 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;
476

477 478 479 480 481
		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 已提交
482 483 484 485 486 487
	}
	rcu_read_unlock();

	return num;
}

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

	fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC);
	if (fdb) {
499
		memcpy(fdb->key.addr.addr, addr, ETH_ALEN);
L
Linus Torvalds 已提交
500
		fdb->dst = source;
501
		fdb->key.vlan_id = vid;
502 503
		fdb->is_local = is_local;
		fdb->is_static = is_static;
504
		fdb->added_by_user = 0;
505
		fdb->added_by_external_learn = 0;
506
		fdb->offloaded = 0;
507
		fdb->is_sticky = 0;
508
		fdb->updated = fdb->used = jiffies;
509 510 511 512 513 514 515 516
		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 已提交
517 518 519 520 521
	}
	return fdb;
}

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

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

529
	fdb = br_fdb_find(br, addr, vid);
L
Linus Torvalds 已提交
530
	if (fdb) {
531
		/* it is okay to have multiple ports with same
L
Linus Torvalds 已提交
532 533
		 * address, just use the first one.
		 */
534
		if (fdb->is_local)
L
Linus Torvalds 已提交
535
			return 0;
536 537
		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);
538
		fdb_delete(br, fdb, true);
539
	}
L
Linus Torvalds 已提交
540

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

545
	fdb_add_hw_addr(br, addr);
546
	fdb_notify(br, fdb, RTM_NEWNEIGH, true);
L
Linus Torvalds 已提交
547 548 549
	return 0;
}

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

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

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

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

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

577
	fdb = fdb_find_rcu(&br->fdb_hash_tbl, addr, vid);
L
Linus Torvalds 已提交
578 579 580
	if (likely(fdb)) {
		/* attempt to update an entry for a local interface */
		if (unlikely(fdb->is_local)) {
581
			if (net_ratelimit())
582 583
				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 已提交
584
		} else {
585 586
			unsigned long now = jiffies;

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

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

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

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

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

656 657 658 659
	if (fdb->offloaded)
		ndm->ndm_flags |= NTF_OFFLOADED;
	if (fdb->added_by_external_learn)
		ndm->ndm_flags |= NTF_EXT_LEARNED;
660 661
	if (fdb->is_sticky)
		ndm->ndm_flags |= NTF_STICKY;
662

663
	if (nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->key.addr))
664
		goto nla_put_failure;
665 666
	if (nla_put_u32(skb, NDA_MASTER, br->dev->ifindex))
		goto nla_put_failure;
667 668 669 670
	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;
671 672
	if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
		goto nla_put_failure;
673

674 675
	if (fdb->key.vlan_id && nla_put(skb, NDA_VLAN, sizeof(u16),
					&fdb->key.vlan_id))
676 677
		goto nla_put_failure;

678 679
	nlmsg_end(skb, nlh);
	return 0;
680 681 682 683 684 685 686 687 688 689

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 */
690
		+ nla_total_size(sizeof(u32)) /* NDA_MASTER */
691
		+ nla_total_size(sizeof(u16)) /* NDA_VLAN */
692 693 694
		+ nla_total_size(sizeof(struct nda_cacheinfo));
}

695
static void fdb_notify(struct net_bridge *br,
696 697
		       const struct net_bridge_fdb_entry *fdb, int type,
		       bool swdev_notify)
698
{
699
	struct net *net = dev_net(br->dev);
700 701 702
	struct sk_buff *skb;
	int err = -ENOBUFS;

703 704
	if (swdev_notify)
		br_switchdev_fdb_notify(fdb, type);
705

706 707 708 709
	skb = nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC);
	if (skb == NULL)
		goto errout;

710
	err = fdb_fill_info(skb, br, fdb, 0, 0, type, 0);
711 712 713 714 715 716 717 718 719
	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:
720
	rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
721 722 723
}

/* Dump information about entries, in response to GETNEIGH */
724 725 726
int br_fdb_dump(struct sk_buff *skb,
		struct netlink_callback *cb,
		struct net_device *dev,
727
		struct net_device *filter_dev,
728
		int *idx)
729
{
730
	struct net_bridge *br = netdev_priv(dev);
731
	struct net_bridge_fdb_entry *f;
732
	int err = 0;
733

734
	if (!(dev->priv_flags & IFF_EBRIDGE))
735
		return err;
736

737 738 739
	if (!filter_dev) {
		err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
		if (err < 0)
740
			return err;
741
	}
742

743 744 745 746 747 748
	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)
749
				goto skip;
750 751 752 753 754 755
			/* !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)
756
				goto skip;
757
		}
758 759 760 761 762 763 764 765 766 767 768 769
		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;
770
	}
771
	rcu_read_unlock();
772

773
	return err;
774
}
775

776
/* Update (create or replace) forwarding database entry */
777
static int fdb_add_entry(struct net_bridge *br, struct net_bridge_port *source,
778 779
			 const u8 *addr, u16 state, u16 flags, u16 vid,
			 u8 ndm_flags)
780
{
781
	u8 is_sticky = !!(ndm_flags & NTF_STICKY);
782
	struct net_bridge_fdb_entry *fdb;
783
	bool modified = false;
784

785
	/* If the port cannot learn allow only local and static entries */
786
	if (source && !(state & NUD_PERMANENT) && !(state & NUD_NOARP) &&
787 788 789 790
	    !(source->state == BR_STATE_LEARNING ||
	      source->state == BR_STATE_FORWARDING))
		return -EPERM;

791 792 793 794 795 796
	if (!source && !(state & NUD_PERMANENT)) {
		pr_info("bridge: RTM_NEWNEIGH %s without NUD_PERMANENT\n",
			br->dev->name);
		return -EINVAL;
	}

797 798 799
	if (is_sticky && (state & NUD_PERMANENT))
		return -EINVAL;

800
	fdb = br_fdb_find(br, addr, vid);
801 802 803
	if (fdb == NULL) {
		if (!(flags & NLM_F_CREATE))
			return -ENOENT;
804

805
		fdb = fdb_create(br, source, addr, vid, 0, 0);
806 807
		if (!fdb)
			return -ENOMEM;
808 809

		modified = true;
810 811 812
	} else {
		if (flags & NLM_F_EXCL)
			return -EEXIST;
813 814 815 816 817

		if (fdb->dst != source) {
			fdb->dst = source;
			modified = true;
		}
818 819
	}

820
	if (fdb_to_nud(br, fdb) != state) {
821 822 823 824
		if (state & NUD_PERMANENT) {
			fdb->is_local = 1;
			if (!fdb->is_static) {
				fdb->is_static = 1;
825
				fdb_add_hw_addr(br, addr);
826 827 828 829 830
			}
		} else if (state & NUD_NOARP) {
			fdb->is_local = 0;
			if (!fdb->is_static) {
				fdb->is_static = 1;
831
				fdb_add_hw_addr(br, addr);
832 833
			}
		} else {
834
			fdb->is_local = 0;
835 836
			if (fdb->is_static) {
				fdb->is_static = 0;
837
				fdb_del_hw_addr(br, addr);
838 839
			}
		}
840

841 842
		modified = true;
	}
843 844 845 846 847 848

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

849
	fdb->added_by_user = 1;
850 851 852 853

	fdb->used = jiffies;
	if (modified) {
		fdb->updated = jiffies;
854
		fdb_notify(br, fdb, RTM_NEWNEIGH, true);
855
	}
856 857 858 859

	return 0;
}

860 861 862
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)
863 864 865 866
{
	int err = 0;

	if (ndm->ndm_flags & NTF_USE) {
867 868 869 870 871
		if (!p) {
			pr_info("bridge: RTM_NEWNEIGH %s with NTF_USE is not supported\n",
				br->dev->name);
			return -EINVAL;
		}
872
		local_bh_disable();
873
		rcu_read_lock();
874
		br_fdb_update(br, p, addr, vid, true);
875
		rcu_read_unlock();
876
		local_bh_enable();
877
	} else if (ndm->ndm_flags & NTF_EXT_LEARNED) {
878
		err = br_fdb_external_learn_add(br, p, addr, vid, true);
879
	} else {
880 881
		spin_lock_bh(&br->hash_lock);
		err = fdb_add_entry(br, p, addr, ndm->ndm_state,
882
				    nlh_flags, vid, ndm->ndm_flags);
883
		spin_unlock_bh(&br->hash_lock);
884 885 886 887 888
	}

	return err;
}

889
/* Add new permanent fdb entry with RTM_NEWNEIGH */
890 891
int br_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
	       struct net_device *dev,
892
	       const unsigned char *addr, u16 vid, u16 nlh_flags)
893
{
894
	struct net_bridge_vlan_group *vg;
895
	struct net_bridge_port *p = NULL;
896
	struct net_bridge_vlan *v;
897
	struct net_bridge *br = NULL;
898
	int err = 0;
899

900 901
	trace_br_fdb_add(ndm, dev, addr, vid, nlh_flags);

902 903 904 905 906
	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;
	}

907 908 909 910 911
	if (is_zero_ether_addr(addr)) {
		pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n");
		return -EINVAL;
	}

912 913 914 915 916 917 918 919 920 921
	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;
		}
922
		br = p->br;
923
		vg = nbp_vlan_group(p);
924 925
	}

926
	if (vid) {
927
		v = br_vlan_find(vg, vid);
928 929
		if (!v || !br_vlan_should_use(v)) {
			pr_info("bridge: RTM_NEWNEIGH with unconfigured vlan %d on %s\n", vid, dev->name);
930 931 932 933
			return -EINVAL;
		}

		/* VID was specified, so use it. */
934
		err = __br_fdb_add(ndm, br, p, addr, nlh_flags, vid);
935
	} else {
936
		err = __br_fdb_add(ndm, br, p, addr, nlh_flags, 0);
937
		if (err || !vg || !vg->num_vlans)
938 939 940 941 942 943
			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.
		 */
944
		list_for_each_entry(v, &vg->vlan_list, vlist) {
945 946
			if (!br_vlan_should_use(v))
				continue;
947
			err = __br_fdb_add(ndm, br, p, addr, nlh_flags, v->vid);
948 949 950
			if (err)
				goto out;
		}
951
	}
952

953
out:
954 955 956
	return err;
}

957 958
static int fdb_delete_by_addr_and_port(struct net_bridge *br,
				       const struct net_bridge_port *p,
959
				       const u8 *addr, u16 vlan)
960 961 962
{
	struct net_bridge_fdb_entry *fdb;

963
	fdb = br_fdb_find(br, addr, vlan);
964
	if (!fdb || fdb->dst != p)
965 966
		return -ENOENT;

967
	fdb_delete(br, fdb, true);
968

969 970 971
	return 0;
}

972 973
static int __br_fdb_delete(struct net_bridge *br,
			   const struct net_bridge_port *p,
974 975 976 977
			   const unsigned char *addr, u16 vid)
{
	int err;

978 979 980
	spin_lock_bh(&br->hash_lock);
	err = fdb_delete_by_addr_and_port(br, p, addr, vid);
	spin_unlock_bh(&br->hash_lock);
981 982 983 984

	return err;
}

985
/* Remove neighbor entry with RTM_DELNEIGH */
986 987
int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
		  struct net_device *dev,
988
		  const unsigned char *addr, u16 vid)
989
{
990
	struct net_bridge_vlan_group *vg;
991
	struct net_bridge_port *p = NULL;
992
	struct net_bridge_vlan *v;
993
	struct net_bridge *br;
994
	int err;
995

996 997 998 999 1000 1001 1002 1003 1004 1005 1006
	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);
1007
		br = p->br;
1008 1009
	}

1010
	if (vid) {
1011 1012
		v = br_vlan_find(vg, vid);
		if (!v) {
1013
			pr_info("bridge: RTM_DELNEIGH with unconfigured vlan %d on %s\n", vid, dev->name);
1014 1015
			return -EINVAL;
		}
1016

1017
		err = __br_fdb_delete(br, p, addr, vid);
1018
	} else {
1019
		err = -ENOENT;
1020
		err &= __br_fdb_delete(br, p, addr, 0);
1021
		if (!vg || !vg->num_vlans)
1022
			return err;
1023

1024 1025 1026
		list_for_each_entry(v, &vg->vlan_list, vlist) {
			if (!br_vlan_should_use(v))
				continue;
1027
			err &= __br_fdb_delete(br, p, addr, v->vid);
1028
		}
1029
	}
1030

1031 1032
	return err;
}
1033 1034 1035

int br_fdb_sync_static(struct net_bridge *br, struct net_bridge_port *p)
{
1036
	struct net_bridge_fdb_entry *f, *tmp;
1037
	int err = 0;
1038 1039 1040

	ASSERT_RTNL();

1041 1042 1043 1044 1045 1046 1047 1048 1049
	/* 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;
1050
	}
1051 1052
done:
	rcu_read_unlock();
1053

1054
	return err;
1055

1056 1057 1058 1059 1060 1061 1062 1063
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);
1064
	}
1065 1066

	goto done;
1067 1068 1069 1070
}

void br_fdb_unsync_static(struct net_bridge *br, struct net_bridge_port *p)
{
1071
	struct net_bridge_fdb_entry *f;
1072 1073 1074

	ASSERT_RTNL();

1075 1076 1077 1078 1079
	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;
1080

1081
		dev_uc_del(p->dev, f->key.addr.addr);
1082
	}
1083
	rcu_read_unlock();
1084
}
1085

1086
int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p,
1087 1088
			      const unsigned char *addr, u16 vid,
			      bool swdev_notify)
1089 1090
{
	struct net_bridge_fdb_entry *fdb;
1091
	bool modified = false;
1092 1093
	int err = 0;

1094 1095
	trace_br_fdb_external_learn_add(br, p, addr, vid);

1096 1097
	spin_lock_bh(&br->hash_lock);

1098
	fdb = br_fdb_find(br, addr, vid);
1099
	if (!fdb) {
1100
		fdb = fdb_create(br, p, addr, vid, 0, 0);
1101 1102 1103 1104 1105
		if (!fdb) {
			err = -ENOMEM;
			goto err_unlock;
		}
		fdb->added_by_external_learn = 1;
1106
		fdb_notify(br, fdb, RTM_NEWNEIGH, swdev_notify);
1107
	} else {
1108
		fdb->updated = jiffies;
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124

		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)
1125
			fdb_notify(br, fdb, RTM_NEWNEIGH, swdev_notify);
1126 1127 1128 1129 1130 1131 1132 1133
	}

err_unlock:
	spin_unlock_bh(&br->hash_lock);

	return err;
}

1134
int br_fdb_external_learn_del(struct net_bridge *br, struct net_bridge_port *p,
1135 1136
			      const unsigned char *addr, u16 vid,
			      bool swdev_notify)
1137 1138 1139 1140 1141 1142
{
	struct net_bridge_fdb_entry *fdb;
	int err = 0;

	spin_lock_bh(&br->hash_lock);

1143
	fdb = br_fdb_find(br, addr, vid);
1144
	if (fdb && fdb->added_by_external_learn)
1145
		fdb_delete(br, fdb, swdev_notify);
1146 1147 1148 1149 1150 1151 1152
	else
		err = -ENOENT;

	spin_unlock_bh(&br->hash_lock);

	return err;
}
1153 1154

void br_fdb_offloaded_set(struct net_bridge *br, struct net_bridge_port *p,
1155
			  const unsigned char *addr, u16 vid, bool offloaded)
1156 1157 1158 1159 1160 1161 1162
{
	struct net_bridge_fdb_entry *fdb;

	spin_lock_bh(&br->hash_lock);

	fdb = br_fdb_find(br, addr, vid);
	if (fdb)
1163
		fdb->offloaded = offloaded;
1164 1165 1166

	spin_unlock_bh(&br->hash_lock);
}
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186

void br_fdb_clear_offload(const struct net_device *dev, u16 vid)
{
	struct net_bridge_fdb_entry *f;
	struct net_bridge_port *p;

	ASSERT_RTNL();

	p = br_port_get_rtnl(dev);
	if (!p)
		return;

	spin_lock_bh(&p->br->hash_lock);
	hlist_for_each_entry(f, &p->br->fdb_list, fdb_node) {
		if (f->dst == p && f->key.vlan_id == vid)
			f->offloaded = 0;
	}
	spin_unlock_bh(&p->br->hash_lock);
}
EXPORT_SYMBOL_GPL(br_fdb_clear_offload);
新手
引导
客服 返回
顶部