br_fdb.c 27.2 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 43
static void fdb_notify(struct net_bridge *br,
		       const struct net_bridge_fdb_entry *, int);
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
static void fdb_delete(struct net_bridge *br, struct net_bridge_fdb_entry *f)
L
Linus Torvalds 已提交
199
{
200 201
	trace_fdb_delete(br, f);

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

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

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

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

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

	fdb_delete(br, f);
}

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

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

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

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

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

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

307 308
	spin_lock_bh(&br->hash_lock);

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

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

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

343 344 345 346 347 348 349
	/* 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 已提交
350

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

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

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

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

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

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

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

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

I
Igor Maravić 已提交
413
#if IS_ENABLED(CONFIG_ATM_LANE)
414 415 416
/* 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 已提交
417 418
{
	struct net_bridge_fdb_entry *fdb;
419
	struct net_bridge_port *port;
420 421
	int ret;

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

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

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

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

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

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

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

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

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

470 471 472
		/* 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;
473

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

	return num;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

L
Linus Torvalds 已提交
583
			/* fastpath: update of existing entry */
584 585 586
			if (unlikely(source != fdb->dst)) {
				fdb->dst = source;
				fdb_modified = true;
587 588 589
				/* Take over HW learned entry */
				if (unlikely(fdb->added_by_external_learn))
					fdb->added_by_external_learn = 0;
590
			}
591 592
			if (now != fdb->updated)
				fdb->updated = now;
593 594
			if (unlikely(added_by_user))
				fdb->added_by_user = 1;
595 596
			if (unlikely(fdb_modified)) {
				trace_br_fdb_update(br, source, addr, vid, added_by_user);
597
				fdb_notify(br, fdb, RTM_NEWNEIGH);
598
			}
L
Linus Torvalds 已提交
599 600
		}
	} else {
601
		spin_lock(&br->hash_lock);
602 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);
			fdb_notify(br, fdb, RTM_NEWNEIGH);
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	 = 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

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

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

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

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

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 */
684
		+ nla_total_size(sizeof(u32)) /* NDA_MASTER */
685
		+ nla_total_size(sizeof(u16)) /* NDA_VLAN */
686 687 688
		+ nla_total_size(sizeof(struct nda_cacheinfo));
}

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

696 697
	br_switchdev_fdb_notify(fdb, type);

698 699 700 701
	skb = nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC);
	if (skb == NULL)
		goto errout;

702
	err = fdb_fill_info(skb, br, fdb, 0, 0, type, 0);
703 704 705 706 707 708 709 710 711
	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:
712
	rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
713 714 715
}

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

726
	if (!(dev->priv_flags & IFF_EBRIDGE))
727
		return err;
728

729 730 731
	if (!filter_dev) {
		err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
		if (err < 0)
732
			return err;
733
	}
734

735 736 737 738 739 740
	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)
741
				goto skip;
742 743 744 745 746 747
			/* !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)
748
				goto skip;
749
		}
750 751 752 753 754 755 756 757 758 759 760 761
		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;
762
	}
763
	rcu_read_unlock();
764

765
	return err;
766
}
767

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

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

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

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

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

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

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

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

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

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

	return 0;
}

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

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

	return err;
}

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

881 882
	trace_br_fdb_add(ndm, dev, addr, vid, nlh_flags);

S
stephen hemminger 已提交
883 884 885 886 887
	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;
	}

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

893 894 895 896 897 898 899 900 901 902
	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;
		}
903
		br = p->br;
904
		vg = nbp_vlan_group(p);
905 906
	}

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

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

934
out:
935 936 937
	return err;
}

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

944
	fdb = br_fdb_find(br, addr, vlan);
945
	if (!fdb || fdb->dst != p)
946 947
		return -ENOENT;

948
	fdb_delete(br, fdb);
949

950 951 952
	return 0;
}

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

959 960 961
	spin_lock_bh(&br->hash_lock);
	err = fdb_delete_by_addr_and_port(br, p, addr, vid);
	spin_unlock_bh(&br->hash_lock);
962 963 964 965

	return err;
}

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

977 978 979 980 981 982 983 984 985 986 987
	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);
988
		br = p->br;
989 990
	}

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

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

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

1012 1013
	return err;
}
1014 1015 1016

int br_fdb_sync_static(struct net_bridge *br, struct net_bridge_port *p)
{
1017
	struct net_bridge_fdb_entry *f, *tmp;
1018
	int err = 0;
1019 1020 1021

	ASSERT_RTNL();

1022 1023 1024 1025 1026 1027 1028 1029 1030
	/* 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;
1031
	}
1032 1033
done:
	rcu_read_unlock();
1034

1035
	return err;
1036

1037 1038 1039 1040 1041 1042 1043 1044
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);
1045
	}
1046 1047

	goto done;
1048 1049 1050 1051
}

void br_fdb_unsync_static(struct net_bridge *br, struct net_bridge_port *p)
{
1052
	struct net_bridge_fdb_entry *f;
1053 1054 1055

	ASSERT_RTNL();

1056 1057 1058 1059 1060
	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;
1061

1062
		dev_uc_del(p->dev, f->key.addr.addr);
1063
	}
1064
	rcu_read_unlock();
1065
}
1066

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

1074 1075
	trace_br_fdb_external_learn_add(br, p, addr, vid);

1076 1077
	spin_lock_bh(&br->hash_lock);

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

		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)
			fdb_notify(br, fdb, RTM_NEWNEIGH);
1106 1107 1108 1109 1110 1111 1112 1113
	}

err_unlock:
	spin_unlock_bh(&br->hash_lock);

	return err;
}

1114
int br_fdb_external_learn_del(struct net_bridge *br, struct net_bridge_port *p,
1115 1116 1117 1118 1119 1120 1121
			      const unsigned char *addr, u16 vid)
{
	struct net_bridge_fdb_entry *fdb;
	int err = 0;

	spin_lock_bh(&br->hash_lock);

1122
	fdb = br_fdb_find(br, addr, vid);
1123 1124 1125 1126 1127 1128 1129 1130 1131
	if (fdb && fdb->added_by_external_learn)
		fdb_delete(br, fdb);
	else
		err = -ENOENT;

	spin_unlock_bh(&br->hash_lock);

	return err;
}
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145

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);
}