br_vlan.c 21.7 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
	struct net_bridge_vlan_group *vg;

	if (br_vlan_is_master(v))
57
		vg = br_vlan_group(v->br);
58
	else
59
		vg = nbp_vlan_group(v->port);
60 61 62 63 64

	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
static int __vlan_vid_add(struct net_device *dev, struct net_bridge *br,
			  u16 vid, u16 flags)
{
75 76 77 78 79 80
	struct switchdev_obj_port_vlan v = {
		.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
		.flags = flags,
		.vid_begin = vid,
		.vid_end = vid,
	};
81 82
	int err;

83 84
	/* Try switchdev op first. In case it is not supported, fallback to
	 * 8021q add.
85
	 */
86 87 88
	err = switchdev_port_obj_add(dev, &v.obj);
	if (err == -EOPNOTSUPP)
		return vlan_vid_add(dev, br->vlan_proto, vid);
89 90 91
	return err;
}

92
static void __vlan_add_list(struct net_bridge_vlan *v)
93
{
94
	struct net_bridge_vlan_group *vg;
95 96
	struct list_head *headp, *hpos;
	struct net_bridge_vlan *vent;
97

98 99 100 101 102 103
	if (br_vlan_is_master(v))
		vg = br_vlan_group(v->br);
	else
		vg = nbp_vlan_group(v->port);

	headp = &vg->vlan_list;
104 105 106 107 108 109
	list_for_each_prev(hpos, headp) {
		vent = list_entry(hpos, struct net_bridge_vlan, vlist);
		if (v->vid < vent->vid)
			continue;
		else
			break;
110
	}
111
	list_add_rcu(&v->vlist, hpos);
112
}
113

114 115
static void __vlan_del_list(struct net_bridge_vlan *v)
{
116
	list_del_rcu(&v->vlist);
117 118
}

119 120
static int __vlan_vid_del(struct net_device *dev, struct net_bridge *br,
			  u16 vid)
121
{
122 123 124 125 126 127
	struct switchdev_obj_port_vlan v = {
		.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
		.vid_begin = vid,
		.vid_end = vid,
	};
	int err;
128

129 130
	/* Try switchdev op first. In case it is not supported, fallback to
	 * 8021q del.
131
	 */
132 133
	err = switchdev_port_obj_del(dev, &v.obj);
	if (err == -EOPNOTSUPP) {
134
		vlan_vid_del(dev, br->vlan_proto, vid);
135
		return 0;
136
	}
137
	return err;
138 139
}

140 141 142 143 144
/* 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)
{
145
	struct net_bridge_vlan_group *vg;
146 147
	struct net_bridge_vlan *masterv;

148 149
	vg = br_vlan_group(br);
	masterv = br_vlan_find(vg, vid);
150 151 152 153
	if (!masterv) {
		/* missing global ctx, create it now */
		if (br_vlan_add(br, vid, 0))
			return NULL;
154
		masterv = br_vlan_find(vg, vid);
155 156 157 158 159 160 161 162 163 164
		if (WARN_ON(!masterv))
			return NULL;
	}
	atomic_inc(&masterv->refcnt);

	return masterv;
}

static void br_vlan_put_master(struct net_bridge_vlan *masterv)
{
165 166
	struct net_bridge_vlan_group *vg;

167 168 169
	if (!br_vlan_is_master(masterv))
		return;

170
	vg = br_vlan_group(masterv->br);
171
	if (atomic_dec_and_test(&masterv->refcnt)) {
172
		rhashtable_remove_fast(&vg->vlan_hash,
173 174 175 176 177 178
				       &masterv->vnode, br_vlan_rht_params);
		__vlan_del_list(masterv);
		kfree_rcu(masterv, rcu);
	}
}

179 180 181 182
/* 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)
183
 * 2. vlan is being added on a bridge (both master and brentry flags)
184
 * 3. vlan is being added on a port, but a global entry didn't exist which
185
 *    is being created right now (master flag set, brentry flag unset), the
186
 *    global entry is used for global per-vlan features, but not for filtering
187
 * 4. same as 3 but with both master and brentry flags set so the entry
188 189 190
 *    will be used for filtering in both the port and the bridge
 */
static int __vlan_add(struct net_bridge_vlan *v, u16 flags)
191
{
192 193
	struct net_bridge_vlan *masterv = NULL;
	struct net_bridge_port *p = NULL;
194
	struct net_bridge_vlan_group *vg;
195 196 197 198 199 200 201
	struct net_device *dev;
	struct net_bridge *br;
	int err;

	if (br_vlan_is_master(v)) {
		br = v->br;
		dev = br->dev;
202
		vg = br_vlan_group(br);
203 204 205 206
	} else {
		p = v->port;
		br = p->br;
		dev = p->dev;
207
		vg = nbp_vlan_group(p);
208 209 210 211 212 213 214 215 216 217 218 219 220
	}

	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) {
221 222
			err = br_vlan_add(br, v->vid, flags |
						      BRIDGE_VLAN_INFO_BRENTRY);
223 224 225 226
			if (err)
				goto out_filt;
		}

227 228 229
		masterv = br_vlan_get_master(br, v->vid);
		if (!masterv)
			goto out_filt;
230 231 232
		v->brvlan = masterv;
	}

233
	/* Add the dev mac and count the vlan only if it's usable */
234 235 236 237 238 239
	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;
		}
240
		vg->num_vlans++;
241 242
	}

243 244
	err = rhashtable_lookup_insert_fast(&vg->vlan_hash, &v->vnode,
					    br_vlan_rht_params);
245 246
	if (err)
		goto out_fdb_insert;
247

248 249 250 251 252 253
	__vlan_add_list(v);
	__vlan_add_flags(v, flags);
out:
	return err;

out_fdb_insert:
254 255 256 257
	if (br_vlan_should_use(v)) {
		br_fdb_find_delete_local(br, p, dev->dev_addr, v->vid);
		vg->num_vlans--;
	}
258 259 260 261 262

out_filt:
	if (p) {
		__vlan_vid_del(dev, br, v->vid);
		if (masterv) {
263
			br_vlan_put_master(masterv);
264 265 266 267 268 269 270 271 272 273
			v->brvlan = NULL;
		}
	}

	goto out;
}

static int __vlan_del(struct net_bridge_vlan *v)
{
	struct net_bridge_vlan *masterv = v;
274
	struct net_bridge_vlan_group *vg;
275 276
	struct net_bridge_port *p = NULL;
	int err = 0;
277

278
	if (br_vlan_is_master(v)) {
279
		vg = br_vlan_group(v->br);
280 281
	} else {
		p = v->port;
282
		vg = nbp_vlan_group(v->port);
283 284
		masterv = v->brvlan;
	}
285

286
	__vlan_delete_pvid(vg, v->vid);
287 288
	if (p) {
		err = __vlan_vid_del(p->dev, p->br, v->vid);
289
		if (err)
290
			goto out;
291
	}
292

293 294 295
	if (br_vlan_should_use(v)) {
		v->flags &= ~BRIDGE_VLAN_INFO_BRENTRY;
		vg->num_vlans--;
296 297 298
	}

	if (masterv != v) {
299 300
		rhashtable_remove_fast(&vg->vlan_hash, &v->vnode,
				       br_vlan_rht_params);
301
		__vlan_del_list(v);
302 303
		kfree_rcu(v, rcu);
	}
304

305
	br_vlan_put_master(masterv);
306 307
out:
	return err;
308 309
}

310 311 312 313 314 315 316 317
static void __vlan_group_free(struct net_bridge_vlan_group *vg)
{
	WARN_ON(!list_empty(&vg->vlan_list));
	rhashtable_destroy(&vg->vlan_hash);
	kfree(vg);
}

static void __vlan_flush(struct net_bridge_vlan_group *vg)
318
{
319 320
	struct net_bridge_vlan *vlan, *tmp;

321 322
	__vlan_delete_pvid(vg, vg->pvid);
	list_for_each_entry_safe(vlan, tmp, &vg->vlan_list, vlist)
323
		__vlan_del(vlan);
324 325
}

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

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

337 338 339 340 341 342 343
	/* 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
344 345 346 347
	 * 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.
	 */
348
	if (!v || !br_vlan_should_use(v)) {
349 350 351 352 353 354 355
		if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) {
			goto out;
		} else {
			kfree_skb(skb);
			return NULL;
		}
	}
356
	if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
357
		skb->vlan_tci = 0;
358 359 360 361 362 363

out:
	return skb;
}

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

370
	BR_INPUT_SKB_CB(skb)->vlan_filtered = true;
371 372 373 374
	/* 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.
	 */
375
	if (unlikely(!skb_vlan_tag_present(skb) &&
376
		     skb->protocol == proto)) {
377
		skb = skb_vlan_untag(skb);
378 379 380 381
		if (unlikely(!skb))
			return false;
	}

382 383 384 385 386
	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);
387
			skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
388
							skb_vlan_tag_get(skb));
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
			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;
	}

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

407 408 409
		/* 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.
410
		 */
V
Vlad Yasevich 已提交
411
		if (!pvid)
412
			goto drop;
413

414 415
		/* PVID is set on this port.  Any untagged or priority-tagged
		 * ingress frame is considered to belong to this vlan.
416
		 */
417
		*vid = pvid;
418
		if (likely(!tagged))
419
			/* Untagged Frame. */
420
			__vlan_hwaccel_put_tag(skb, proto, pvid);
421 422 423 424 425 426 427 428
		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;

429 430 431 432
		return true;
	}

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

441 442 443
bool br_allowed_ingress(const struct net_bridge *br,
			struct net_bridge_vlan_group *vg, struct sk_buff *skb,
			u16 *vid)
444 445 446 447 448 449 450 451 452
{
	/* 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;
	}

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

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

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

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

	return false;
}

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

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

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

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

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

		return true;
	}

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

	return false;
}

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

	ASSERT_RTNL();

517 518
	vg = br_vlan_group(br);
	vlan = br_vlan_find(vg, vid);
519 520 521 522 523 524 525 526 527 528 529 530 531 532
	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;
533
			vg->num_vlans++;
534 535 536 537
		}
		__vlan_add_flags(vlan, flags);
		return 0;
	}
538

539 540
	vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
	if (!vlan)
541 542
		return -ENOMEM;

543 544 545 546 547 548 549 550 551
	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);
552

553
	return ret;
554 555
}

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

	ASSERT_RTNL();

566 567
	vg = br_vlan_group(br);
	v = br_vlan_find(vg, vid);
568 569
	if (!v || !br_vlan_is_brentry(v))
		return -ENOENT;
570

571
	br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
572
	br_fdb_delete_by_port(br, NULL, vid, 0);
573

574
	return __vlan_del(v);
575 576 577 578
}

void br_vlan_flush(struct net_bridge *br)
{
579 580
	struct net_bridge_vlan_group *vg;

581 582
	ASSERT_RTNL();

583 584 585 586 587
	vg = br_vlan_group(br);
	__vlan_flush(vg);
	RCU_INIT_POINTER(br->vlgrp, NULL);
	synchronize_rcu();
	__vlan_group_free(vg);
588 589
}

590
struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid)
591
{
592 593
	if (!vg)
		return NULL;
594

595
	return br_vlan_lookup(&vg->vlan_hash, vid);
596 597
}

598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
/* 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]);
}

625
int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
626 627
{
	if (br->vlan_enabled == val)
628
		return 0;
629 630

	br->vlan_enabled = val;
631
	br_manage_promisc(br);
632 633
	recalculate_group_addr(br);
	br_recalculate_fwd_mask(br);
634

635 636 637 638 639 640 641 642 643
	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);
644
	rtnl_unlock();
645

646 647 648
	return 0;
}

649
int __br_vlan_set_proto(struct net_bridge *br, __be16 proto)
650 651 652
{
	int err = 0;
	struct net_bridge_port *p;
653
	struct net_bridge_vlan *vlan;
654
	struct net_bridge_vlan_group *vg;
655
	__be16 oldproto;
656 657

	if (br->vlan_proto == proto)
658
		return 0;
659 660 661

	/* Add VLANs for the new proto to the device filter. */
	list_for_each_entry(p, &br->port_list, list) {
662 663
		vg = nbp_vlan_group(p);
		list_for_each_entry(vlan, &vg->vlan_list, vlist) {
664
			err = vlan_vid_add(p->dev, proto, vlan->vid);
665 666 667 668 669 670 671 672 673 674 675 676
			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. */
677 678 679
	list_for_each_entry(p, &br->port_list, list) {
		vg = nbp_vlan_group(p);
		list_for_each_entry(vlan, &vg->vlan_list, vlist)
680
			vlan_vid_del(p->dev, oldproto, vlan->vid);
681
	}
682

683
	return 0;
684 685

err_filt:
686
	list_for_each_entry_continue_reverse(vlan, &vg->vlan_list, vlist)
687
		vlan_vid_del(p->dev, proto, vlan->vid);
688

689 690 691
	list_for_each_entry_continue_reverse(p, &br->port_list, list) {
		vg = nbp_vlan_group(p);
		list_for_each_entry(vlan, &vg->vlan_list, vlist)
692
			vlan_vid_del(p->dev, proto, vlan->vid);
693
	}
694

695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
	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;
712 713
}

714
static bool vlan_default_pvid(struct net_bridge_vlan_group *vg, u16 vid)
715
{
716 717
	struct net_bridge_vlan *v;

718
	if (vid != vg->pvid)
719 720 721 722 723 724 725 726
		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;
727 728 729 730 731 732 733 734 735 736
}

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.
	 */
737
	if (vlan_default_pvid(br_vlan_group(br), pvid))
738 739 740
		br_vlan_delete(br, pvid);

	list_for_each_entry(p, &br->port_list, list) {
741
		if (vlan_default_pvid(nbp_vlan_group(p), pvid))
742 743 744 745 746 747
			nbp_vlan_delete(p, pvid);
	}

	br->default_pvid = 0;
}

748
int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid)
749
{
750
	const struct net_bridge_vlan *pvent;
751
	struct net_bridge_vlan_group *vg;
752 753 754 755 756
	struct net_bridge_port *p;
	u16 old_pvid;
	int err = 0;
	unsigned long *changed;

757 758 759 760 761
	if (!pvid) {
		br_vlan_disable_default_pvid(br);
		return 0;
	}

762 763 764 765 766 767 768 769 770 771
	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.
	 */
772 773 774
	vg = br_vlan_group(br);
	pvent = br_vlan_find(vg, pvid);
	if ((!old_pvid || vlan_default_pvid(vg, old_pvid)) &&
775
	    (!pvent || !br_vlan_should_use(pvent))) {
776 777
		err = br_vlan_add(br, pvid,
				  BRIDGE_VLAN_INFO_PVID |
778 779
				  BRIDGE_VLAN_INFO_UNTAGGED |
				  BRIDGE_VLAN_INFO_BRENTRY);
780 781 782 783 784 785 786 787 788 789
		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.
		 */
790
		vg = nbp_vlan_group(p);
791
		if ((old_pvid &&
792 793
		     !vlan_default_pvid(vg, old_pvid)) ||
		    br_vlan_find(vg, pvid))
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
			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 |
827 828
				    BRIDGE_VLAN_INFO_UNTAGGED |
				    BRIDGE_VLAN_INFO_BRENTRY);
829 830 831 832 833
		br_vlan_delete(br, pvid);
	}
	goto out;
}

834 835 836 837 838
int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val)
{
	u16 pvid = val;
	int err = 0;

839
	if (val >= VLAN_VID_MASK)
840 841 842 843 844 845 846 847 848 849 850 851 852 853
		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;
	}
854
	err = __br_vlan_set_default_pvid(br, pvid);
855 856 857 858 859
unlock:
	rtnl_unlock();
	return err;
}

860
int br_vlan_init(struct net_bridge *br)
861
{
862
	struct net_bridge_vlan_group *vg;
863 864
	int ret = -ENOMEM;

865 866
	vg = kzalloc(sizeof(*vg), GFP_KERNEL);
	if (!vg)
867
		goto out;
868
	ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
869 870
	if (ret)
		goto err_rhtbl;
871
	INIT_LIST_HEAD(&vg->vlan_list);
872
	br->vlan_proto = htons(ETH_P_8021Q);
873
	br->default_pvid = 1;
874
	rcu_assign_pointer(br->vlgrp, vg);
875 876 877 878 879 880 881 882 883 884
	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:
885
	rhashtable_destroy(&vg->vlan_hash);
886
err_rhtbl:
887
	kfree(vg);
888 889 890 891 892 893

	goto out;
}

int nbp_vlan_init(struct net_bridge_port *p)
{
894
	struct net_bridge_vlan_group *vg;
895 896
	int ret = -ENOMEM;

897 898
	vg = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL);
	if (!vg)
899 900
		goto out;

901
	ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
902 903
	if (ret)
		goto err_rhtbl;
904
	INIT_LIST_HEAD(&vg->vlan_list);
905
	rcu_assign_pointer(p->vlgrp, vg);
906 907 908 909 910 911 912 913 914 915 916
	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:
917 918
	RCU_INIT_POINTER(p->vlgrp, NULL);
	synchronize_rcu();
919
	rhashtable_destroy(&vg->vlan_hash);
920
err_rhtbl:
921
	kfree(vg);
922 923

	goto out;
924 925
}

926 927 928
/* Must be protected by RTNL.
 * Must be called with vid in range from 1 to 4094 inclusive.
 */
929
int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags)
930
{
931 932
	struct net_bridge_vlan *vlan;
	int ret;
933 934 935

	ASSERT_RTNL();

936
	vlan = br_vlan_find(nbp_vlan_group(port), vid);
937 938 939
	if (vlan) {
		__vlan_add_flags(vlan, flags);
		return 0;
940 941
	}

942 943 944
	vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
	if (!vlan)
		return -ENOMEM;
945

946 947 948 949 950
	vlan->vid = vid;
	vlan->port = port;
	ret = __vlan_add(vlan, flags);
	if (ret)
		kfree(vlan);
951

952
	return ret;
953 954
}

955 956 957
/* Must be protected by RTNL.
 * Must be called with vid in range from 1 to 4094 inclusive.
 */
958 959
int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
{
960
	struct net_bridge_vlan *v;
961 962 963

	ASSERT_RTNL();

964
	v = br_vlan_find(nbp_vlan_group(port), vid);
965 966
	if (!v)
		return -ENOENT;
967
	br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
968
	br_fdb_delete_by_port(port->br, port, vid, 0);
969

970
	return __vlan_del(v);
971 972 973 974
}

void nbp_vlan_flush(struct net_bridge_port *port)
{
975 976
	struct net_bridge_vlan_group *vg;

977 978
	ASSERT_RTNL();

979 980 981 982 983
	vg = nbp_vlan_group(port);
	__vlan_flush(vg);
	RCU_INIT_POINTER(port->vlgrp, NULL);
	synchronize_rcu();
	__vlan_group_free(vg);
984
}