br_vlan.c 21.2 KB
Newer Older
1 2 3 4
#include <linux/kernel.h>
#include <linux/netdevice.h>
#include <linux/rtnetlink.h>
#include <linux/slab.h>
5
#include <net/switchdev.h>
6 7 8

#include "br_private.h"

9 10
static inline int br_vlan_cmp(struct rhashtable_compare_arg *arg,
			      const void *ptr)
11
{
12 13 14 15 16 17 18 19 20 21
	const struct net_bridge_vlan *vle = ptr;
	u16 vid = *(u16 *)arg->key;

	return vle->vid != vid;
}

static const struct rhashtable_params br_vlan_rht_params = {
	.head_offset = offsetof(struct net_bridge_vlan, vnode),
	.key_offset = offsetof(struct net_bridge_vlan, vid),
	.key_len = sizeof(u16),
22 23
	.nelem_hint = 3,
	.locks_mul = 1,
24 25 26 27 28 29 30 31 32 33
	.max_size = VLAN_N_VID,
	.obj_cmpfn = br_vlan_cmp,
	.automatic_shrinking = true,
};

static struct net_bridge_vlan *br_vlan_lookup(struct rhashtable *tbl, u16 vid)
{
	return rhashtable_lookup_fast(tbl, &vid, br_vlan_rht_params);
}

34
static void __vlan_add_pvid(struct net_bridge_vlan_group *vg, u16 vid)
35
{
36
	if (vg->pvid == vid)
37 38 39
		return;

	smp_wmb();
40
	vg->pvid = vid;
41 42
}

43
static void __vlan_delete_pvid(struct net_bridge_vlan_group *vg, u16 vid)
44
{
45
	if (vg->pvid != vid)
46 47 48
		return;

	smp_wmb();
49
	vg->pvid = 0;
50 51
}

52
static void __vlan_add_flags(struct net_bridge_vlan *v, u16 flags)
53
{
54 55 56 57 58 59 60 61 62 63 64
	struct net_bridge_vlan_group *vg;

	if (br_vlan_is_master(v))
		vg = v->br->vlgrp;
	else
		vg = v->port->vlgrp;

	if (flags & BRIDGE_VLAN_INFO_PVID)
		__vlan_add_pvid(vg, v->vid);
	else
		__vlan_delete_pvid(vg, v->vid);
65 66

	if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
67
		v->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
68
	else
69
		v->flags &= ~BRIDGE_VLAN_INFO_UNTAGGED;
70 71
}

72 73 74 75 76 77 78 79 80 81 82 83 84
static int __vlan_vid_add(struct net_device *dev, struct net_bridge *br,
			  u16 vid, u16 flags)
{
	const struct net_device_ops *ops = dev->netdev_ops;
	int err;

	/* If driver uses VLAN ndo ops, use 8021q to install vid
	 * on device, otherwise try switchdev ops to install vid.
	 */

	if (ops->ndo_vlan_rx_add_vid) {
		err = vlan_vid_add(dev, br->vlan_proto, vid);
	} else {
85
		struct switchdev_obj_port_vlan v = {
86
			.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
87 88 89
			.flags = flags,
			.vid_begin = vid,
			.vid_end = vid,
90 91
		};

92
		err = switchdev_port_obj_add(dev, &v.obj);
93 94 95 96 97 98 99
		if (err == -EOPNOTSUPP)
			err = 0;
	}

	return err;
}

100
static void __vlan_add_list(struct net_bridge_vlan *v)
101
{
102 103
	struct list_head *headp, *hpos;
	struct net_bridge_vlan *vent;
104

105 106 107 108 109 110 111 112
	headp = br_vlan_is_master(v) ? &v->br->vlgrp->vlan_list :
				       &v->port->vlgrp->vlan_list;
	list_for_each_prev(hpos, headp) {
		vent = list_entry(hpos, struct net_bridge_vlan, vlist);
		if (v->vid < vent->vid)
			continue;
		else
			break;
113
	}
114
	list_add_rcu(&v->vlist, hpos);
115
}
116

117 118
static void __vlan_del_list(struct net_bridge_vlan *v)
{
119
	list_del_rcu(&v->vlist);
120 121
}

122 123
static int __vlan_vid_del(struct net_device *dev, struct net_bridge *br,
			  u16 vid)
124 125
{
	const struct net_device_ops *ops = dev->netdev_ops;
126
	int err = 0;
127 128 129 130 131 132 133 134

	/* If driver uses VLAN ndo ops, use 8021q to delete vid
	 * on device, otherwise try switchdev ops to delete vid.
	 */

	if (ops->ndo_vlan_rx_kill_vid) {
		vlan_vid_del(dev, br->vlan_proto, vid);
	} else {
135
		struct switchdev_obj_port_vlan v = {
136
			.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
137 138
			.vid_begin = vid,
			.vid_end = vid,
139 140
		};

141
		err = switchdev_port_obj_del(dev, &v.obj);
142 143
		if (err == -EOPNOTSUPP)
			err = 0;
144
	}
145 146

	return err;
147 148
}

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
/* Returns a master vlan, if it didn't exist it gets created. In all cases a
 * a reference is taken to the master vlan before returning.
 */
static struct net_bridge_vlan *br_vlan_get_master(struct net_bridge *br, u16 vid)
{
	struct net_bridge_vlan *masterv;

	masterv = br_vlan_find(br->vlgrp, vid);
	if (!masterv) {
		/* missing global ctx, create it now */
		if (br_vlan_add(br, vid, 0))
			return NULL;
		masterv = br_vlan_find(br->vlgrp, vid);
		if (WARN_ON(!masterv))
			return NULL;
	}
	atomic_inc(&masterv->refcnt);

	return masterv;
}

static void br_vlan_put_master(struct net_bridge_vlan *masterv)
{
	if (!br_vlan_is_master(masterv))
		return;

	if (atomic_dec_and_test(&masterv->refcnt)) {
		rhashtable_remove_fast(&masterv->br->vlgrp->vlan_hash,
				       &masterv->vnode, br_vlan_rht_params);
		__vlan_del_list(masterv);
		kfree_rcu(masterv, rcu);
	}
}

183 184 185 186 187 188 189 190 191 192 193 194
/* This is the shared VLAN add function which works for both ports and bridge
 * devices. There are four possible calls to this function in terms of the
 * vlan entry type:
 * 1. vlan is being added on a port (no master flags, global entry exists)
 * 2. vlan is being added on a bridge (both master and brvlan flags)
 * 3. vlan is being added on a port, but a global entry didn't exist which
 *    is being created right now (master flag set, brvlan flag unset), the
 *    global entry is used for global per-vlan features, but not for filtering
 * 4. same as 3 but with both master and brvlan flags set so the entry
 *    will be used for filtering in both the port and the bridge
 */
static int __vlan_add(struct net_bridge_vlan *v, u16 flags)
195
{
196 197
	struct net_bridge_vlan *masterv = NULL;
	struct net_bridge_port *p = NULL;
198
	struct net_bridge_vlan_group *vg;
199 200 201 202 203 204 205
	struct net_device *dev;
	struct net_bridge *br;
	int err;

	if (br_vlan_is_master(v)) {
		br = v->br;
		dev = br->dev;
206
		vg = br->vlgrp;
207 208 209 210
	} else {
		p = v->port;
		br = p->br;
		dev = p->dev;
211
		vg = p->vlgrp;
212 213 214 215 216 217 218 219 220 221 222 223 224
	}

	if (p) {
		/* Add VLAN to the device filter if it is supported.
		 * This ensures tagged traffic enters the bridge when
		 * promiscuous mode is disabled by br_manage_promisc().
		 */
		err = __vlan_vid_add(dev, br, v->vid, flags);
		if (err)
			goto out;

		/* need to work on the master vlan too */
		if (flags & BRIDGE_VLAN_INFO_MASTER) {
225 226
			err = br_vlan_add(br, v->vid, flags |
						      BRIDGE_VLAN_INFO_BRENTRY);
227 228 229 230
			if (err)
				goto out_filt;
		}

231 232 233
		masterv = br_vlan_get_master(br, v->vid);
		if (!masterv)
			goto out_filt;
234 235 236
		v->brvlan = masterv;
	}

237
	/* Add the dev mac and count the vlan only if it's usable */
238 239 240 241 242 243
	if (br_vlan_should_use(v)) {
		err = br_fdb_insert(br, p, dev->dev_addr, v->vid);
		if (err) {
			br_err(br, "failed insert local address into bridge forwarding table\n");
			goto out_filt;
		}
244
		vg->num_vlans++;
245 246
	}

247 248
	err = rhashtable_lookup_insert_fast(&vg->vlan_hash, &v->vnode,
					    br_vlan_rht_params);
249 250
	if (err)
		goto out_fdb_insert;
251

252 253 254 255 256 257
	__vlan_add_list(v);
	__vlan_add_flags(v, flags);
out:
	return err;

out_fdb_insert:
258 259 260 261
	if (br_vlan_should_use(v)) {
		br_fdb_find_delete_local(br, p, dev->dev_addr, v->vid);
		vg->num_vlans--;
	}
262 263 264 265 266

out_filt:
	if (p) {
		__vlan_vid_del(dev, br, v->vid);
		if (masterv) {
267
			br_vlan_put_master(masterv);
268 269 270 271 272 273 274 275 276 277
			v->brvlan = NULL;
		}
	}

	goto out;
}

static int __vlan_del(struct net_bridge_vlan *v)
{
	struct net_bridge_vlan *masterv = v;
278
	struct net_bridge_vlan_group *vg;
279 280
	struct net_bridge_port *p = NULL;
	int err = 0;
281

282
	if (br_vlan_is_master(v)) {
283
		vg = v->br->vlgrp;
284 285
	} else {
		p = v->port;
286
		vg = v->port->vlgrp;
287 288
		masterv = v->brvlan;
	}
289

290
	__vlan_delete_pvid(vg, v->vid);
291 292
	if (p) {
		err = __vlan_vid_del(p->dev, p->br, v->vid);
293
		if (err)
294
			goto out;
295
	}
296

297 298 299
	if (br_vlan_should_use(v)) {
		v->flags &= ~BRIDGE_VLAN_INFO_BRENTRY;
		vg->num_vlans--;
300 301 302
	}

	if (masterv != v) {
303 304
		rhashtable_remove_fast(&vg->vlan_hash, &v->vnode,
				       br_vlan_rht_params);
305
		__vlan_del_list(v);
306 307
		kfree_rcu(v, rcu);
	}
308

309
	br_vlan_put_master(masterv);
310 311
out:
	return err;
312 313
}

314
static void __vlan_flush(struct net_bridge_vlan_group *vlgrp)
315
{
316 317
	struct net_bridge_vlan *vlan, *tmp;

318
	__vlan_delete_pvid(vlgrp, vlgrp->pvid);
319 320 321 322
	list_for_each_entry_safe(vlan, tmp, &vlgrp->vlan_list, vlist)
		__vlan_del(vlan);
	rhashtable_destroy(&vlgrp->vlan_hash);
	kfree(vlgrp);
323 324
}

325
struct sk_buff *br_handle_vlan(struct net_bridge *br,
326
			       struct net_bridge_vlan_group *vg,
327
			       struct sk_buff *skb)
328
{
329
	struct net_bridge_vlan *v;
330 331
	u16 vid;

332 333
	/* If this packet was not filtered at input, let it pass */
	if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
334 335
		goto out;

336 337 338 339 340 341 342
	/* At this point, we know that the frame was filtered and contains
	 * a valid vlan id.  If the vlan id has untagged flag set,
	 * send untagged; otherwise, send tagged.
	 */
	br_vlan_get_tag(skb, &vid);
	v = br_vlan_find(vg, vid);
	/* Vlan entry must be configured at this point.  The
343 344 345 346
	 * only exception is the bridge is set in promisc mode and the
	 * packet is destined for the bridge device.  In this case
	 * pass the packet as is.
	 */
347
	if (!v || !br_vlan_should_use(v)) {
348 349 350 351 352 353 354
		if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) {
			goto out;
		} else {
			kfree_skb(skb);
			return NULL;
		}
	}
355
	if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
356
		skb->vlan_tci = 0;
357 358 359 360 361 362

out:
	return skb;
}

/* Called under RCU */
363
static bool __allowed_ingress(struct net_bridge_vlan_group *vg, __be16 proto,
364
			      struct sk_buff *skb, u16 *vid)
365
{
366
	const struct net_bridge_vlan *v;
367
	bool tagged;
368

369
	BR_INPUT_SKB_CB(skb)->vlan_filtered = true;
370 371 372 373
	/* If vlan tx offload is disabled on bridge device and frame was
	 * sent from vlan device on the bridge device, it does not have
	 * HW accelerated vlan tag.
	 */
374
	if (unlikely(!skb_vlan_tag_present(skb) &&
375
		     skb->protocol == proto)) {
376
		skb = skb_vlan_untag(skb);
377 378 379 380
		if (unlikely(!skb))
			return false;
	}

381 382 383 384 385
	if (!br_vlan_get_tag(skb, vid)) {
		/* Tagged frame */
		if (skb->vlan_proto != proto) {
			/* Protocol-mismatch, empty out vlan_tci for new tag */
			skb_push(skb, ETH_HLEN);
386
			skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
387
							skb_vlan_tag_get(skb));
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
			if (unlikely(!skb))
				return false;

			skb_pull(skb, ETH_HLEN);
			skb_reset_mac_len(skb);
			*vid = 0;
			tagged = false;
		} else {
			tagged = true;
		}
	} else {
		/* Untagged frame */
		tagged = false;
	}

403
	if (!*vid) {
404 405
		u16 pvid = br_get_pvid(vg);

406 407 408
		/* Frame had a tag with VID 0 or did not have a tag.
		 * See if pvid is set on this port.  That tells us which
		 * vlan untagged or priority-tagged traffic belongs to.
409
		 */
V
Vlad Yasevich 已提交
410
		if (!pvid)
411
			goto drop;
412

413 414
		/* PVID is set on this port.  Any untagged or priority-tagged
		 * ingress frame is considered to belong to this vlan.
415
		 */
416
		*vid = pvid;
417
		if (likely(!tagged))
418
			/* Untagged Frame. */
419
			__vlan_hwaccel_put_tag(skb, proto, pvid);
420 421 422 423 424 425 426 427
		else
			/* Priority-tagged Frame.
			 * At this point, We know that skb->vlan_tci had
			 * VLAN_TAG_PRESENT bit and its VID field was 0x000.
			 * We update only VID field and preserve PCP field.
			 */
			skb->vlan_tci |= pvid;

428 429 430 431
		return true;
	}

	/* Frame had a valid vlan tag.  See if vlan is allowed */
432
	v = br_vlan_find(vg, *vid);
433
	if (v && br_vlan_should_use(v))
434
		return true;
435 436
drop:
	kfree_skb(skb);
437 438 439
	return false;
}

440 441 442
bool br_allowed_ingress(const struct net_bridge *br,
			struct net_bridge_vlan_group *vg, struct sk_buff *skb,
			u16 *vid)
443 444 445 446 447 448 449 450 451
{
	/* If VLAN filtering is disabled on the bridge, all packets are
	 * permitted.
	 */
	if (!br->vlan_enabled) {
		BR_INPUT_SKB_CB(skb)->vlan_filtered = false;
		return true;
	}

452
	return __allowed_ingress(vg, br->vlan_proto, skb, vid);
453 454
}

455
/* Called under RCU. */
456
bool br_allowed_egress(struct net_bridge_vlan_group *vg,
457 458
		       const struct sk_buff *skb)
{
459
	const struct net_bridge_vlan *v;
460 461
	u16 vid;

462 463
	/* If this packet was not filtered at input, let it pass */
	if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
464 465 466
		return true;

	br_vlan_get_tag(skb, &vid);
467 468
	v = br_vlan_find(vg, vid);
	if (v && br_vlan_should_use(v))
469 470 471 472 473
		return true;

	return false;
}

474 475 476
/* Called under RCU */
bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid)
{
477
	struct net_bridge_vlan_group *vg;
478 479
	struct net_bridge *br = p->br;

480
	/* If filtering was disabled at input, let it pass. */
481
	if (!br->vlan_enabled)
482 483
		return true;

484 485
	vg = p->vlgrp;
	if (!vg || !vg->num_vlans)
486 487
		return false;

488 489 490
	if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto)
		*vid = 0;

491
	if (!*vid) {
492
		*vid = br_get_pvid(vg);
V
Vlad Yasevich 已提交
493
		if (!*vid)
494 495 496 497 498
			return false;

		return true;
	}

499
	if (br_vlan_find(vg, *vid))
500 501 502 503 504
		return true;

	return false;
}

505 506 507
/* Must be protected by RTNL.
 * Must be called with vid in range from 1 to 4094 inclusive.
 */
508
int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags)
509
{
510 511
	struct net_bridge_vlan *vlan;
	int ret;
512 513 514

	ASSERT_RTNL();

515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
	vlan = br_vlan_find(br->vlgrp, vid);
	if (vlan) {
		if (!br_vlan_is_brentry(vlan)) {
			/* Trying to change flags of non-existent bridge vlan */
			if (!(flags & BRIDGE_VLAN_INFO_BRENTRY))
				return -EINVAL;
			/* It was only kept for port vlans, now make it real */
			ret = br_fdb_insert(br, NULL, br->dev->dev_addr,
					    vlan->vid);
			if (ret) {
				br_err(br, "failed insert local address into bridge forwarding table\n");
				return ret;
			}
			atomic_inc(&vlan->refcnt);
			vlan->flags |= BRIDGE_VLAN_INFO_BRENTRY;
			br->vlgrp->num_vlans++;
		}
		__vlan_add_flags(vlan, flags);
		return 0;
	}
535

536 537
	vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
	if (!vlan)
538 539
		return -ENOMEM;

540 541 542 543 544 545 546 547 548
	vlan->vid = vid;
	vlan->flags = flags | BRIDGE_VLAN_INFO_MASTER;
	vlan->flags &= ~BRIDGE_VLAN_INFO_PVID;
	vlan->br = br;
	if (flags & BRIDGE_VLAN_INFO_BRENTRY)
		atomic_set(&vlan->refcnt, 1);
	ret = __vlan_add(vlan, flags);
	if (ret)
		kfree(vlan);
549

550
	return ret;
551 552
}

553 554 555
/* Must be protected by RTNL.
 * Must be called with vid in range from 1 to 4094 inclusive.
 */
556 557
int br_vlan_delete(struct net_bridge *br, u16 vid)
{
558
	struct net_bridge_vlan *v;
559 560 561

	ASSERT_RTNL();

562 563 564
	v = br_vlan_find(br->vlgrp, vid);
	if (!v || !br_vlan_is_brentry(v))
		return -ENOENT;
565

566
	br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
567

568
	return __vlan_del(v);
569 570 571 572 573 574
}

void br_vlan_flush(struct net_bridge *br)
{
	ASSERT_RTNL();

575
	__vlan_flush(br_vlan_group(br));
576 577
}

578
struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid)
579
{
580 581
	if (!vg)
		return NULL;
582

583
	return br_vlan_lookup(&vg->vlan_hash, vid);
584 585
}

586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
/* Must be protected by RTNL. */
static void recalculate_group_addr(struct net_bridge *br)
{
	if (br->group_addr_set)
		return;

	spin_lock_bh(&br->lock);
	if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q)) {
		/* Bridge Group Address */
		br->group_addr[5] = 0x00;
	} else { /* vlan_enabled && ETH_P_8021AD */
		/* Provider Bridge Group Address */
		br->group_addr[5] = 0x08;
	}
	spin_unlock_bh(&br->lock);
}

/* Must be protected by RTNL. */
void br_recalculate_fwd_mask(struct net_bridge *br)
{
	if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q))
		br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT;
	else /* vlan_enabled && ETH_P_8021AD */
		br->group_fwd_mask_required = BR_GROUPFWD_8021AD &
					      ~(1u << br->group_addr[5]);
}

613
int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
614 615
{
	if (br->vlan_enabled == val)
616
		return 0;
617 618

	br->vlan_enabled = val;
619
	br_manage_promisc(br);
620 621
	recalculate_group_addr(br);
	br_recalculate_fwd_mask(br);
622

623 624 625 626 627 628 629 630 631
	return 0;
}

int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
{
	if (!rtnl_trylock())
		return restart_syscall();

	__br_vlan_filter_toggle(br, val);
632
	rtnl_unlock();
633

634 635 636
	return 0;
}

637
int __br_vlan_set_proto(struct net_bridge *br, __be16 proto)
638 639 640
{
	int err = 0;
	struct net_bridge_port *p;
641
	struct net_bridge_vlan *vlan;
642
	__be16 oldproto;
643 644

	if (br->vlan_proto == proto)
645
		return 0;
646 647 648

	/* Add VLANs for the new proto to the device filter. */
	list_for_each_entry(p, &br->port_list, list) {
649 650
		list_for_each_entry(vlan, &p->vlgrp->vlan_list, vlist) {
			err = vlan_vid_add(p->dev, proto, vlan->vid);
651 652 653 654 655 656 657 658 659 660 661 662
			if (err)
				goto err_filt;
		}
	}

	oldproto = br->vlan_proto;
	br->vlan_proto = proto;

	recalculate_group_addr(br);
	br_recalculate_fwd_mask(br);

	/* Delete VLANs for the old proto from the device filter. */
663 664 665
	list_for_each_entry(p, &br->port_list, list)
		list_for_each_entry(vlan, &p->vlgrp->vlan_list, vlist)
			vlan_vid_del(p->dev, oldproto, vlan->vid);
666

667
	return 0;
668 669

err_filt:
670 671
	list_for_each_entry_continue_reverse(vlan, &p->vlgrp->vlan_list, vlist)
		vlan_vid_del(p->dev, proto, vlan->vid);
672

673 674 675
	list_for_each_entry_continue_reverse(p, &br->port_list, list)
		list_for_each_entry(vlan, &p->vlgrp->vlan_list, vlist)
			vlan_vid_del(p->dev, proto, vlan->vid);
676

677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
	return err;
}

int br_vlan_set_proto(struct net_bridge *br, unsigned long val)
{
	int err;

	if (val != ETH_P_8021Q && val != ETH_P_8021AD)
		return -EPROTONOSUPPORT;

	if (!rtnl_trylock())
		return restart_syscall();

	err = __br_vlan_set_proto(br, htons(val));
	rtnl_unlock();

	return err;
694 695
}

696
static bool vlan_default_pvid(struct net_bridge_vlan_group *vg, u16 vid)
697
{
698 699
	struct net_bridge_vlan *v;

700
	if (vid != vg->pvid)
701 702 703 704 705 706 707 708
		return false;

	v = br_vlan_lookup(&vg->vlan_hash, vid);
	if (v && br_vlan_should_use(v) &&
	    (v->flags & BRIDGE_VLAN_INFO_UNTAGGED))
		return true;

	return false;
709 710 711 712 713 714 715 716 717 718
}

static void br_vlan_disable_default_pvid(struct net_bridge *br)
{
	struct net_bridge_port *p;
	u16 pvid = br->default_pvid;

	/* Disable default_pvid on all ports where it is still
	 * configured.
	 */
719
	if (vlan_default_pvid(br->vlgrp, pvid))
720 721 722
		br_vlan_delete(br, pvid);

	list_for_each_entry(p, &br->port_list, list) {
723
		if (vlan_default_pvid(p->vlgrp, pvid))
724 725 726 727 728 729
			nbp_vlan_delete(p, pvid);
	}

	br->default_pvid = 0;
}

730
int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid)
731
{
732
	const struct net_bridge_vlan *pvent;
733 734 735 736 737
	struct net_bridge_port *p;
	u16 old_pvid;
	int err = 0;
	unsigned long *changed;

738 739 740 741 742
	if (!pvid) {
		br_vlan_disable_default_pvid(br);
		return 0;
	}

743 744 745 746 747 748 749 750 751 752
	changed = kcalloc(BITS_TO_LONGS(BR_MAX_PORTS), sizeof(unsigned long),
			  GFP_KERNEL);
	if (!changed)
		return -ENOMEM;

	old_pvid = br->default_pvid;

	/* Update default_pvid config only if we do not conflict with
	 * user configuration.
	 */
753
	pvent = br_vlan_find(br->vlgrp, pvid);
754
	if ((!old_pvid || vlan_default_pvid(br->vlgrp, old_pvid)) &&
755
	    (!pvent || !br_vlan_should_use(pvent))) {
756 757
		err = br_vlan_add(br, pvid,
				  BRIDGE_VLAN_INFO_PVID |
758 759
				  BRIDGE_VLAN_INFO_UNTAGGED |
				  BRIDGE_VLAN_INFO_BRENTRY);
760 761 762 763 764 765 766 767 768 769 770
		if (err)
			goto out;
		br_vlan_delete(br, old_pvid);
		set_bit(0, changed);
	}

	list_for_each_entry(p, &br->port_list, list) {
		/* Update default_pvid config only if we do not conflict with
		 * user configuration.
		 */
		if ((old_pvid &&
771
		     !vlan_default_pvid(p->vlgrp, old_pvid)) ||
772
		    br_vlan_find(p->vlgrp, pvid))
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
			continue;

		err = nbp_vlan_add(p, pvid,
				   BRIDGE_VLAN_INFO_PVID |
				   BRIDGE_VLAN_INFO_UNTAGGED);
		if (err)
			goto err_port;
		nbp_vlan_delete(p, old_pvid);
		set_bit(p->port_no, changed);
	}

	br->default_pvid = pvid;

out:
	kfree(changed);
	return err;

err_port:
	list_for_each_entry_continue_reverse(p, &br->port_list, list) {
		if (!test_bit(p->port_no, changed))
			continue;

		if (old_pvid)
			nbp_vlan_add(p, old_pvid,
				     BRIDGE_VLAN_INFO_PVID |
				     BRIDGE_VLAN_INFO_UNTAGGED);
		nbp_vlan_delete(p, pvid);
	}

	if (test_bit(0, changed)) {
		if (old_pvid)
			br_vlan_add(br, old_pvid,
				    BRIDGE_VLAN_INFO_PVID |
806 807
				    BRIDGE_VLAN_INFO_UNTAGGED |
				    BRIDGE_VLAN_INFO_BRENTRY);
808 809 810 811 812
		br_vlan_delete(br, pvid);
	}
	goto out;
}

813 814 815 816 817
int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val)
{
	u16 pvid = val;
	int err = 0;

818
	if (val >= VLAN_VID_MASK)
819 820 821 822 823 824 825 826 827 828 829 830 831 832
		return -EINVAL;

	if (!rtnl_trylock())
		return restart_syscall();

	if (pvid == br->default_pvid)
		goto unlock;

	/* Only allow default pvid change when filtering is disabled */
	if (br->vlan_enabled) {
		pr_info_once("Please disable vlan filtering to change default_pvid\n");
		err = -EPERM;
		goto unlock;
	}
833
	err = __br_vlan_set_default_pvid(br, pvid);
834 835 836 837 838
unlock:
	rtnl_unlock();
	return err;
}

839
int br_vlan_init(struct net_bridge *br)
840
{
841 842 843 844 845 846 847 848 849
	int ret = -ENOMEM;

	br->vlgrp = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL);
	if (!br->vlgrp)
		goto out;
	ret = rhashtable_init(&br->vlgrp->vlan_hash, &br_vlan_rht_params);
	if (ret)
		goto err_rhtbl;
	INIT_LIST_HEAD(&br->vlgrp->vlan_list);
850
	br->vlan_proto = htons(ETH_P_8021Q);
851
	br->default_pvid = 1;
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
	ret = br_vlan_add(br, 1,
			  BRIDGE_VLAN_INFO_PVID | BRIDGE_VLAN_INFO_UNTAGGED |
			  BRIDGE_VLAN_INFO_BRENTRY);
	if (ret)
		goto err_vlan_add;

out:
	return ret;

err_vlan_add:
	rhashtable_destroy(&br->vlgrp->vlan_hash);
err_rhtbl:
	kfree(br->vlgrp);

	goto out;
}

int nbp_vlan_init(struct net_bridge_port *p)
{
871
	struct net_bridge_vlan_group *vg;
872 873
	int ret = -ENOMEM;

874 875
	vg = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL);
	if (!vg)
876 877
		goto out;

878
	ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
879 880
	if (ret)
		goto err_rhtbl;
881 882 883 884
	INIT_LIST_HEAD(&vg->vlan_list);
	/* Make sure everything's committed before publishing vg */
	smp_wmb();
	p->vlgrp = vg;
885 886 887 888 889 890 891 892 893 894 895
	if (p->br->default_pvid) {
		ret = nbp_vlan_add(p, p->br->default_pvid,
				   BRIDGE_VLAN_INFO_PVID |
				   BRIDGE_VLAN_INFO_UNTAGGED);
		if (ret)
			goto err_vlan_add;
	}
out:
	return ret;

err_vlan_add:
896
	rhashtable_destroy(&vg->vlan_hash);
897
err_rhtbl:
898
	kfree(vg);
899 900

	goto out;
901 902
}

903 904 905
/* Must be protected by RTNL.
 * Must be called with vid in range from 1 to 4094 inclusive.
 */
906
int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags)
907
{
908 909
	struct net_bridge_vlan *vlan;
	int ret;
910 911 912

	ASSERT_RTNL();

913 914 915 916
	vlan = br_vlan_find(port->vlgrp, vid);
	if (vlan) {
		__vlan_add_flags(vlan, flags);
		return 0;
917 918
	}

919 920 921
	vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
	if (!vlan)
		return -ENOMEM;
922

923 924 925 926 927
	vlan->vid = vid;
	vlan->port = port;
	ret = __vlan_add(vlan, flags);
	if (ret)
		kfree(vlan);
928

929
	return ret;
930 931
}

932 933 934
/* Must be protected by RTNL.
 * Must be called with vid in range from 1 to 4094 inclusive.
 */
935 936
int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
{
937
	struct net_bridge_vlan *v;
938 939 940

	ASSERT_RTNL();

941 942 943
	v = br_vlan_find(port->vlgrp, vid);
	if (!v)
		return -ENOENT;
944
	br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
945
	br_fdb_delete_by_port(port->br, port, vid, 0);
946

947
	return __vlan_del(v);
948 949 950 951
}

void nbp_vlan_flush(struct net_bridge_port *port)
{
952
	struct net_bridge_vlan *vlan;
953 954 955

	ASSERT_RTNL();

956 957
	list_for_each_entry(vlan, &port->vlgrp->vlan_list, vlist)
		vlan_vid_del(port->dev, port->br->vlan_proto, vlan->vid);
958

959
	__vlan_flush(nbp_vlan_group(port));
960
}