br_fdb.c 26.4 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>
L
Linus Torvalds 已提交
28 29
#include "br_private.h"

30
static struct kmem_cache *br_fdb_cache __read_mostly;
L
Linus Torvalds 已提交
31
static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
32
		      const unsigned char *addr, u16 vid);
33 34
static void fdb_notify(struct net_bridge *br,
		       const struct net_bridge_fdb_entry *, int);
L
Linus Torvalds 已提交
35

36 37
static u32 fdb_salt __read_mostly;

38
int __init br_fdb_init(void)
L
Linus Torvalds 已提交
39 40 41 42
{
	br_fdb_cache = kmem_cache_create("bridge_fdb_cache",
					 sizeof(struct net_bridge_fdb_entry),
					 0,
43
					 SLAB_HWCACHE_ALIGN, NULL);
44 45 46
	if (!br_fdb_cache)
		return -ENOMEM;

47
	get_random_bytes(&fdb_salt, sizeof(fdb_salt));
48
	return 0;
L
Linus Torvalds 已提交
49 50
}

A
Andrew Morton 已提交
51
void br_fdb_fini(void)
L
Linus Torvalds 已提交
52 53 54 55 56 57 58 59
{
	kmem_cache_destroy(br_fdb_cache);
}


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

65
static inline int has_expired(const struct net_bridge *br,
L
Linus Torvalds 已提交
66 67
				  const struct net_bridge_fdb_entry *fdb)
{
68
	return !fdb->is_static && !fdb->added_by_external_learn &&
69
		time_before_eq(fdb->updated + hold_time(br), jiffies);
L
Linus Torvalds 已提交
70 71
}

72
static inline int br_mac_hash(const unsigned char *mac, __u16 vid)
L
Linus Torvalds 已提交
73
{
74
	/* use 1 byte of OUI and 3 bytes of NIC */
75
	u32 key = get_unaligned((u32 *)(mac + 2));
76
	return jhash_2words(key, vid, fdb_salt) & (BR_HASH_SIZE - 1);
L
Linus Torvalds 已提交
77 78
}

79 80 81 82 83 84 85
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);
}

86 87 88 89 90 91
static struct net_bridge_fdb_entry *fdb_find_rcu(struct hlist_head *head,
						 const unsigned char *addr,
						 __u16 vid)
{
	struct net_bridge_fdb_entry *f;

92 93
	WARN_ON_ONCE(!rcu_read_lock_held());

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
	hlist_for_each_entry_rcu(f, head, hlist)
		if (ether_addr_equal(f->addr.addr, addr) && f->vlan_id == vid)
			break;

	return f;
}

/* requires bridge hash_lock */
static struct net_bridge_fdb_entry *br_fdb_find(struct net_bridge *br,
						const unsigned char *addr,
						__u16 vid)
{
	struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)];
	struct net_bridge_fdb_entry *fdb;

109 110
	WARN_ON_ONCE(!br_hash_lock_held(br));

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
	rcu_read_lock();
	fdb = fdb_find_rcu(head, addr, vid);
	rcu_read_unlock();

	return fdb;
}

struct net_bridge_fdb_entry *br_fdb_find_rcu(struct net_bridge *br,
					     const unsigned char *addr,
					     __u16 vid)
{
	struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)];

	return fdb_find_rcu(head, addr, vid);
}

127 128 129 130 131
/* 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.
 */
132
static void fdb_add_hw_addr(struct net_bridge *br, const unsigned char *addr)
133 134
{
	int err;
135
	struct net_bridge_port *p;
136 137 138 139 140 141 142 143 144 145 146 147 148

	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:
149 150 151
	list_for_each_entry_continue_reverse(p, &br->port_list, list) {
		if (!br_promisc_port(p))
			dev_uc_del(p->dev, addr);
152 153 154 155 156 157 158 159
	}
}

/* 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.
 */
160
static void fdb_del_hw_addr(struct net_bridge *br, const unsigned char *addr)
161 162 163 164 165 166 167 168 169 170 171
{
	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);
	}
}

172 173
static void fdb_del_external_learn(struct net_bridge_fdb_entry *f)
{
174
	struct switchdev_obj_port_fdb fdb = {
175
		.obj = {
176
			.orig_dev = f->dst->dev,
177 178 179
			.id = SWITCHDEV_OBJ_ID_PORT_FDB,
			.flags = SWITCHDEV_F_DEFER,
		},
180
		.vid = f->vlan_id,
181 182
	};

183
	ether_addr_copy(fdb.addr, f->addr.addr);
184
	switchdev_port_obj_del(f->dst->dev, &fdb.obj);
185 186
}

187
static void fdb_delete(struct net_bridge *br, struct net_bridge_fdb_entry *f)
L
Linus Torvalds 已提交
188
{
189
	if (f->is_static)
190
		fdb_del_hw_addr(br, f->addr.addr);
191

192 193 194
	if (f->added_by_external_learn)
		fdb_del_external_learn(f);

195
	hlist_del_init_rcu(&f->hlist);
196
	fdb_notify(br, f, RTM_DELNEIGH);
197
	call_rcu(&f->rcu, fdb_rcu_free);
L
Linus Torvalds 已提交
198 199
}

200 201 202 203 204 205
/* 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)
{
	const unsigned char *addr = f->addr.addr;
206 207
	struct net_bridge_vlan_group *vg;
	const struct net_bridge_vlan *v;
208
	struct net_bridge_port *op;
209
	u16 vid = f->vlan_id;
210 211 212

	/* Maybe another port has same hw addr? */
	list_for_each_entry(op, &br->port_list, list) {
213
		vg = nbp_vlan_group(op);
214
		if (op != p && ether_addr_equal(op->dev->dev_addr, addr) &&
215
		    (!vid || br_vlan_find(vg, vid))) {
216
			f->dst = op;
217
			f->added_by_user = 0;
218 219 220 221
			return;
		}
	}

222 223
	vg = br_vlan_group(br);
	v = br_vlan_find(vg, vid);
224 225
	/* Maybe bridge device has same hw addr? */
	if (p && ether_addr_equal(br->dev->dev_addr, addr) &&
226
	    (!vid || (v && br_vlan_should_use(v)))) {
227
		f->dst = NULL;
228
		f->added_by_user = 0;
229 230 231 232 233 234
		return;
	}

	fdb_delete(br, f);
}

235 236 237 238 239 240 241
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);
242
	f = br_fdb_find(br, addr, vid);
243 244 245 246 247
	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 已提交
248 249
void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr)
{
250
	struct net_bridge_vlan_group *vg;
L
Linus Torvalds 已提交
251
	struct net_bridge *br = p->br;
252
	struct net_bridge_vlan *v;
L
Linus Torvalds 已提交
253
	int i;
254

L
Linus Torvalds 已提交
255 256
	spin_lock_bh(&br->hash_lock);

257
	vg = nbp_vlan_group(p);
L
Linus Torvalds 已提交
258 259 260 261 262 263 264
	/* Search all chains since old address/hash is unknown */
	for (i = 0; i < BR_HASH_SIZE; i++) {
		struct hlist_node *h;
		hlist_for_each(h, &br->hash[i]) {
			struct net_bridge_fdb_entry *f;

			f = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
265
			if (f->dst == p && f->is_local && !f->added_by_user) {
L
Linus Torvalds 已提交
266
				/* delete old one */
267 268
				fdb_delete_local(br, p, f);

269 270 271 272
				/* if this port has no vlan information
				 * configured, we can safely be done at
				 * this point.
				 */
273
				if (!vg || !vg->num_vlans)
274
					goto insert;
L
Linus Torvalds 已提交
275 276 277 278
			}
		}
	}

279 280 281 282
insert:
	/* insert new address,  may fail if invalid address or dup. */
	fdb_insert(br, p, newaddr, 0);

283
	if (!vg || !vg->num_vlans)
284 285 286 287 288 289
		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.
	 */
290 291
	list_for_each_entry(v, &vg->vlan_list, vlist)
		fdb_insert(br, p, newaddr, v->vid);
292

293
done:
L
Linus Torvalds 已提交
294 295 296
	spin_unlock_bh(&br->hash_lock);
}

297 298
void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr)
{
299
	struct net_bridge_vlan_group *vg;
300
	struct net_bridge_fdb_entry *f;
301
	struct net_bridge_vlan *v;
302

303 304
	spin_lock_bh(&br->hash_lock);

305
	/* If old entry was unassociated with any port, then delete it. */
306
	f = br_fdb_find(br, br->dev->dev_addr, 0);
307
	if (f && f->is_local && !f->dst && !f->added_by_user)
308
		fdb_delete_local(br, NULL, f);
309

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

330
void br_fdb_cleanup(struct work_struct *work)
L
Linus Torvalds 已提交
331
{
332 333
	struct net_bridge *br = container_of(work, struct net_bridge,
					     gc_work.work);
L
Linus Torvalds 已提交
334
	unsigned long delay = hold_time(br);
335 336
	unsigned long work_delay = delay;
	unsigned long now = jiffies;
L
Linus Torvalds 已提交
337 338 339 340
	int i;

	for (i = 0; i < BR_HASH_SIZE; i++) {
		struct net_bridge_fdb_entry *f;
341
		struct hlist_node *n;
L
Linus Torvalds 已提交
342

343 344 345 346
		if (!br->hash[i].first)
			continue;

		spin_lock_bh(&br->hash_lock);
347
		hlist_for_each_entry_safe(f, n, &br->hash[i], hlist) {
348
			unsigned long this_timer;
349

350
			if (f->is_static)
351
				continue;
352 353
			if (f->added_by_external_learn)
				continue;
354
			this_timer = f->updated + delay;
355 356 357
			if (time_after(this_timer, now))
				work_delay = min(work_delay, this_timer - now);
			else
358
				fdb_delete(br, f);
L
Linus Torvalds 已提交
359
		}
360 361
		spin_unlock_bh(&br->hash_lock);
		cond_resched();
L
Linus Torvalds 已提交
362 363
	}

364 365 366
	/* 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 已提交
367 368
}

369 370 371 372 373 374 375 376
/* Completely flush all dynamic entries in forwarding database.*/
void br_fdb_flush(struct net_bridge *br)
{
	int i;

	spin_lock_bh(&br->hash_lock);
	for (i = 0; i < BR_HASH_SIZE; i++) {
		struct net_bridge_fdb_entry *f;
377 378
		struct hlist_node *n;
		hlist_for_each_entry_safe(f, n, &br->hash[i], hlist) {
379
			if (!f->is_static)
380
				fdb_delete(br, f);
381 382 383 384
		}
	}
	spin_unlock_bh(&br->hash_lock);
}
385

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

	spin_lock_bh(&br->hash_lock);
	for (i = 0; i < BR_HASH_SIZE; i++) {
		struct hlist_node *h, *g;
400

L
Linus Torvalds 已提交
401 402 403
		hlist_for_each_safe(h, g, &br->hash[i]) {
			struct net_bridge_fdb_entry *f
				= hlist_entry(h, struct net_bridge_fdb_entry, hlist);
404
			if (f->dst != p)
L
Linus Torvalds 已提交
405 406
				continue;

407 408 409
			if (!do_all)
				if (f->is_static || (vid && f->vlan_id != vid))
					continue;
L
Linus Torvalds 已提交
410

411 412 413 414
			if (f->is_local)
				fdb_delete_local(br, p, f);
			else
				fdb_delete(br, f);
L
Linus Torvalds 已提交
415 416 417 418 419
		}
	}
	spin_unlock_bh(&br->hash_lock);
}

I
Igor Maravić 已提交
420
#if IS_ENABLED(CONFIG_ATM_LANE)
421 422 423
/* 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 已提交
424 425
{
	struct net_bridge_fdb_entry *fdb;
426
	struct net_bridge_port *port;
427 428
	int ret;

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

440
	return ret;
L
Linus Torvalds 已提交
441
}
442
#endif /* CONFIG_ATM_LANE */
L
Linus Torvalds 已提交
443 444

/*
445
 * Fill buffer with forwarding table records in
L
Linus Torvalds 已提交
446 447 448 449 450 451 452 453 454 455 456 457 458
 * the API format.
 */
int br_fdb_fillbuf(struct net_bridge *br, void *buf,
		   unsigned long maxnum, unsigned long skip)
{
	struct __fdb_entry *fe = buf;
	int i, num = 0;
	struct net_bridge_fdb_entry *f;

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

	rcu_read_lock();
	for (i = 0; i < BR_HASH_SIZE; i++) {
459
		hlist_for_each_entry_rcu(f, &br->hash[i], hlist) {
L
Linus Torvalds 已提交
460 461 462
			if (num >= maxnum)
				goto out;

463
			if (has_expired(br, f))
L
Linus Torvalds 已提交
464 465
				continue;

466 467 468 469
			/* ignore pseudo entry for local MAC address */
			if (!f->dst)
				continue;

L
Linus Torvalds 已提交
470 471 472 473 474 475 476
			if (skip) {
				--skip;
				continue;
			}

			/* convert from internal format to API */
			memcpy(fe->mac_addr, f->addr.addr, ETH_ALEN);
477 478

			/* due to ABI compat need to split into hi/lo */
L
Linus Torvalds 已提交
479
			fe->port_no = f->dst->port_no;
480 481
			fe->port_hi = f->dst->port_no >> 8;

L
Linus Torvalds 已提交
482 483
			fe->is_local = f->is_local;
			if (!f->is_static)
484
				fe->ageing_timer_value = jiffies_delta_to_clock_t(jiffies - f->updated);
L
Linus Torvalds 已提交
485 486 487 488 489 490 491 492 493 494 495 496 497
			++fe;
			++num;
		}
	}

 out:
	rcu_read_unlock();

	return num;
}

static struct net_bridge_fdb_entry *fdb_create(struct hlist_head *head,
					       struct net_bridge_port *source,
498
					       const unsigned char *addr,
499 500 501
					       __u16 vid,
					       unsigned char is_local,
					       unsigned char is_static)
L
Linus Torvalds 已提交
502 503 504 505 506 507 508
{
	struct net_bridge_fdb_entry *fdb;

	fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC);
	if (fdb) {
		memcpy(fdb->addr.addr, addr, ETH_ALEN);
		fdb->dst = source;
509
		fdb->vlan_id = vid;
510 511
		fdb->is_local = is_local;
		fdb->is_static = is_static;
512
		fdb->added_by_user = 0;
513
		fdb->added_by_external_learn = 0;
514
		fdb->updated = fdb->used = jiffies;
515
		hlist_add_head_rcu(&fdb->hlist, head);
L
Linus Torvalds 已提交
516 517 518 519 520
	}
	return fdb;
}

static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
521
		  const unsigned char *addr, u16 vid)
L
Linus Torvalds 已提交
522
{
523
	struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)];
L
Linus Torvalds 已提交
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);
539
	}
L
Linus Torvalds 已提交
540

541
	fdb = fdb_create(head, 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);
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 hlist_head *head = &br->hash[br_mac_hash(addr, vid)];
L
Linus Torvalds 已提交
566
	struct net_bridge_fdb_entry *fdb;
567
	bool fdb_modified = false;
L
Linus Torvalds 已提交
568 569 570 571 572

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

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

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

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

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

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

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

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

D
David S. Miller 已提交
652 653
	if (nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->addr))
		goto nla_put_failure;
654 655
	if (nla_put_u32(skb, NDA_MASTER, br->dev->ifindex))
		goto nla_put_failure;
656 657 658 659
	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 已提交
660 661
	if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
		goto nla_put_failure;
662

663
	if (fdb->vlan_id && nla_put(skb, NDA_VLAN, sizeof(u16), &fdb->vlan_id))
664 665
		goto nla_put_failure;

666 667
	nlmsg_end(skb, nlh);
	return 0;
668 669 670 671 672 673 674 675 676 677

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 */
678
		+ nla_total_size(sizeof(u32)) /* NDA_MASTER */
679
		+ nla_total_size(sizeof(u16)) /* NDA_VLAN */
680 681 682
		+ nla_total_size(sizeof(struct nda_cacheinfo));
}

683 684
static void fdb_notify(struct net_bridge *br,
		       const struct net_bridge_fdb_entry *fdb, int type)
685
{
686
	struct net *net = dev_net(br->dev);
687 688 689 690 691 692 693
	struct sk_buff *skb;
	int err = -ENOBUFS;

	skb = nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC);
	if (skb == NULL)
		goto errout;

694
	err = fdb_fill_info(skb, br, fdb, 0, 0, type, 0);
695 696 697 698 699 700 701 702 703
	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:
704
	rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
705 706 707
}

/* Dump information about entries, in response to GETNEIGH */
708 709 710
int br_fdb_dump(struct sk_buff *skb,
		struct netlink_callback *cb,
		struct net_device *dev,
711
		struct net_device *filter_dev,
712
		int *idx)
713
{
714
	struct net_bridge *br = netdev_priv(dev);
715
	int err = 0;
716
	int i;
717

718 719
	if (!(dev->priv_flags & IFF_EBRIDGE))
		goto out;
720

721 722 723 724 725
	if (!filter_dev) {
		err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
		if (err < 0)
			goto out;
	}
726

727 728
	for (i = 0; i < BR_HASH_SIZE; i++) {
		struct net_bridge_fdb_entry *f;
729

730
		hlist_for_each_entry_rcu(f, &br->hash[i], hlist) {
731

732
			if (*idx < cb->args[2])
733 734
				goto skip;

735 736 737 738
			if (filter_dev &&
			    (!f->dst || f->dst->dev != filter_dev)) {
				if (filter_dev != dev)
					goto skip;
739
				/* !f->dst is a special case for bridge
740 741 742 743 744 745 746
				 * 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)
					goto skip;
			}
747 748
			if (!filter_dev && f->dst)
				goto skip;
749

750 751 752 753 754
			err = fdb_fill_info(skb, br, f,
					    NETLINK_CB(cb->skb).portid,
					    cb->nlh->nlmsg_seq,
					    RTM_NEWNEIGH,
					    NLM_F_MULTI);
755 756
			if (err < 0)
				goto out;
757
skip:
758
			*idx += 1;
759 760 761
		}
	}

762
out:
763
	return err;
764
}
765

S
stephen hemminger 已提交
766
/* Update (create or replace) forwarding database entry */
767 768
static int fdb_add_entry(struct net_bridge *br, struct net_bridge_port *source,
			 const __u8 *addr, __u16 state, __u16 flags, __u16 vid)
769
{
770
	struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)];
771
	struct net_bridge_fdb_entry *fdb;
772
	bool modified = false;
773

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

780 781 782 783 784 785
	if (!source && !(state & NUD_PERMANENT)) {
		pr_info("bridge: RTM_NEWNEIGH %s without NUD_PERMANENT\n",
			br->dev->name);
		return -EINVAL;
	}

786
	fdb = br_fdb_find(br, addr, vid);
787 788 789
	if (fdb == NULL) {
		if (!(flags & NLM_F_CREATE))
			return -ENOENT;
790

791
		fdb = fdb_create(head, source, addr, vid, 0, 0);
792 793
		if (!fdb)
			return -ENOMEM;
794 795

		modified = true;
796 797 798
	} else {
		if (flags & NLM_F_EXCL)
			return -EEXIST;
799 800 801 802 803

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

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

827 828
		modified = true;
	}
829
	fdb->added_by_user = 1;
830 831 832 833

	fdb->used = jiffies;
	if (modified) {
		fdb->updated = jiffies;
834
		fdb_notify(br, fdb, RTM_NEWNEIGH);
835
	}
836 837 838 839

	return 0;
}

840 841 842
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)
843 844 845 846
{
	int err = 0;

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

	return err;
}

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

S
stephen hemminger 已提交
878 879 880 881 882
	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;
	}

883 884 885 886 887
	if (is_zero_ether_addr(addr)) {
		pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n");
		return -EINVAL;
	}

888 889 890 891 892 893 894 895 896 897
	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;
		}
898
		br = p->br;
899
		vg = nbp_vlan_group(p);
900 901
	}

902
	if (vid) {
903
		v = br_vlan_find(vg, vid);
904 905
		if (!v || !br_vlan_should_use(v)) {
			pr_info("bridge: RTM_NEWNEIGH with unconfigured vlan %d on %s\n", vid, dev->name);
906 907 908 909
			return -EINVAL;
		}

		/* VID was specified, so use it. */
910
		err = __br_fdb_add(ndm, br, p, addr, nlh_flags, vid);
S
stephen hemminger 已提交
911
	} else {
912
		err = __br_fdb_add(ndm, br, p, addr, nlh_flags, 0);
913
		if (err || !vg || !vg->num_vlans)
914 915 916 917 918 919
			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.
		 */
920
		list_for_each_entry(v, &vg->vlan_list, vlist) {
921 922
			if (!br_vlan_should_use(v))
				continue;
923
			err = __br_fdb_add(ndm, br, p, addr, nlh_flags, v->vid);
924 925 926
			if (err)
				goto out;
		}
S
stephen hemminger 已提交
927
	}
928

929
out:
930 931 932
	return err;
}

933 934
static int fdb_delete_by_addr_and_port(struct net_bridge *br,
				       const struct net_bridge_port *p,
935
				       const u8 *addr, u16 vlan)
936 937 938
{
	struct net_bridge_fdb_entry *fdb;

939
	fdb = br_fdb_find(br, addr, vlan);
940
	if (!fdb || fdb->dst != p)
941 942
		return -ENOENT;

943
	fdb_delete(br, fdb);
944

945 946 947
	return 0;
}

948 949
static int __br_fdb_delete(struct net_bridge *br,
			   const struct net_bridge_port *p,
950 951 952 953
			   const unsigned char *addr, u16 vid)
{
	int err;

954 955 956
	spin_lock_bh(&br->hash_lock);
	err = fdb_delete_by_addr_and_port(br, p, addr, vid);
	spin_unlock_bh(&br->hash_lock);
957 958 959 960

	return err;
}

961
/* Remove neighbor entry with RTM_DELNEIGH */
962 963
int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
		  struct net_device *dev,
964
		  const unsigned char *addr, u16 vid)
965
{
966
	struct net_bridge_vlan_group *vg;
967
	struct net_bridge_port *p = NULL;
968
	struct net_bridge_vlan *v;
969
	struct net_bridge *br;
970
	int err;
971

972 973 974 975 976 977 978 979 980 981 982
	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);
983
		br = p->br;
984 985
	}

986
	if (vid) {
987 988
		v = br_vlan_find(vg, vid);
		if (!v) {
989
			pr_info("bridge: RTM_DELNEIGH with unconfigured vlan %d on %s\n", vid, dev->name);
990 991
			return -EINVAL;
		}
992

993
		err = __br_fdb_delete(br, p, addr, vid);
994
	} else {
995
		err = -ENOENT;
996
		err &= __br_fdb_delete(br, p, addr, 0);
997
		if (!vg || !vg->num_vlans)
998
			return err;
999

1000 1001 1002
		list_for_each_entry(v, &vg->vlan_list, vlist) {
			if (!br_vlan_should_use(v))
				continue;
1003
			err &= __br_fdb_delete(br, p, addr, v->vid);
1004
		}
1005
	}
1006

1007 1008
	return err;
}
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064

int br_fdb_sync_static(struct net_bridge *br, struct net_bridge_port *p)
{
	struct net_bridge_fdb_entry *fdb, *tmp;
	int i;
	int err;

	ASSERT_RTNL();

	for (i = 0; i < BR_HASH_SIZE; i++) {
		hlist_for_each_entry(fdb, &br->hash[i], hlist) {
			/* We only care for static entries */
			if (!fdb->is_static)
				continue;

			err = dev_uc_add(p->dev, fdb->addr.addr);
			if (err)
				goto rollback;
		}
	}
	return 0;

rollback:
	for (i = 0; i < BR_HASH_SIZE; i++) {
		hlist_for_each_entry(tmp, &br->hash[i], hlist) {
			/* If we reached the fdb that failed, we can stop */
			if (tmp == fdb)
				break;

			/* We only care for static entries */
			if (!tmp->is_static)
				continue;

			dev_uc_del(p->dev, tmp->addr.addr);
		}
	}
	return err;
}

void br_fdb_unsync_static(struct net_bridge *br, struct net_bridge_port *p)
{
	struct net_bridge_fdb_entry *fdb;
	int i;

	ASSERT_RTNL();

	for (i = 0; i < BR_HASH_SIZE; i++) {
		hlist_for_each_entry_rcu(fdb, &br->hash[i], hlist) {
			/* We only care for static entries */
			if (!fdb->is_static)
				continue;

			dev_uc_del(p->dev, fdb->addr.addr);
		}
	}
}
1065

1066
int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p,
1067 1068 1069 1070 1071 1072
			      const unsigned char *addr, u16 vid)
{
	struct hlist_head *head;
	struct net_bridge_fdb_entry *fdb;
	int err = 0;

1073
	ASSERT_RTNL();
1074 1075 1076
	spin_lock_bh(&br->hash_lock);

	head = &br->hash[br_mac_hash(addr, vid)];
1077
	fdb = br_fdb_find(br, addr, vid);
1078
	if (!fdb) {
1079
		fdb = fdb_create(head, p, addr, vid, 0, 0);
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
		if (!fdb) {
			err = -ENOMEM;
			goto err_unlock;
		}
		fdb->added_by_external_learn = 1;
		fdb_notify(br, fdb, RTM_NEWNEIGH);
	} else if (fdb->added_by_external_learn) {
		/* Refresh entry */
		fdb->updated = fdb->used = jiffies;
	} else if (!fdb->added_by_user) {
		/* Take over SW learned entry */
		fdb->added_by_external_learn = 1;
		fdb->updated = jiffies;
		fdb_notify(br, fdb, RTM_NEWNEIGH);
	}

err_unlock:
	spin_unlock_bh(&br->hash_lock);

	return err;
}

1102
int br_fdb_external_learn_del(struct net_bridge *br, struct net_bridge_port *p,
1103 1104 1105 1106 1107
			      const unsigned char *addr, u16 vid)
{
	struct net_bridge_fdb_entry *fdb;
	int err = 0;

1108
	ASSERT_RTNL();
1109 1110
	spin_lock_bh(&br->hash_lock);

1111
	fdb = br_fdb_find(br, addr, vid);
1112 1113 1114 1115 1116 1117 1118 1119 1120
	if (fdb && fdb->added_by_external_learn)
		fdb_delete(br, fdb);
	else
		err = -ENOENT;

	spin_unlock_bh(&br->hash_lock);

	return err;
}