br_vlan.c 26.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

#include "br_private.h"
8
#include "br_private_tunnel.h"
9

10 11
static inline int br_vlan_cmp(struct rhashtable_compare_arg *arg,
			      const void *ptr)
12
{
13 14 15 16 17 18 19 20 21 22
	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),
23 24
	.nelem_hint = 3,
	.locks_mul = 1,
25 26 27 28 29 30 31 32 33 34
	.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);
}

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

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

	return true;
44 45
}

46
static bool __vlan_delete_pvid(struct net_bridge_vlan_group *vg, u16 vid)
47
{
48
	if (vg->pvid != vid)
49
		return false;
50 51

	smp_wmb();
52
	vg->pvid = 0;
53 54

	return true;
55 56
}

57 58
/* return true if anything changed, false otherwise */
static bool __vlan_add_flags(struct net_bridge_vlan *v, u16 flags)
59
{
60
	struct net_bridge_vlan_group *vg;
61 62
	u16 old_flags = v->flags;
	bool ret;
63 64

	if (br_vlan_is_master(v))
65
		vg = br_vlan_group(v->br);
66
	else
67
		vg = nbp_vlan_group(v->port);
68 69

	if (flags & BRIDGE_VLAN_INFO_PVID)
70
		ret = __vlan_add_pvid(vg, v->vid);
71
	else
72
		ret = __vlan_delete_pvid(vg, v->vid);
73 74

	if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
75
		v->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
76
	else
77
		v->flags &= ~BRIDGE_VLAN_INFO_UNTAGGED;
78 79

	return ret || !!(old_flags ^ v->flags);
80 81
}

82 83 84 85 86
static int __vlan_vid_add(struct net_device *dev, struct net_bridge *br,
			  u16 vid, u16 flags)
{
	int err;

87 88
	/* Try switchdev op first. In case it is not supported, fallback to
	 * 8021q add.
89
	 */
90
	err = br_switchdev_port_vlan_add(dev, vid, flags);
91 92
	if (err == -EOPNOTSUPP)
		return vlan_vid_add(dev, br->vlan_proto, vid);
93 94 95
	return err;
}

96
static void __vlan_add_list(struct net_bridge_vlan *v)
97
{
98
	struct net_bridge_vlan_group *vg;
99 100
	struct list_head *headp, *hpos;
	struct net_bridge_vlan *vent;
101

102 103 104 105 106 107
	if (br_vlan_is_master(v))
		vg = br_vlan_group(v->br);
	else
		vg = nbp_vlan_group(v->port);

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

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

123 124
static int __vlan_vid_del(struct net_device *dev, struct net_bridge *br,
			  u16 vid)
125
{
126
	int err;
127

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

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

147 148
	vg = br_vlan_group(br);
	masterv = br_vlan_find(vg, vid);
149
	if (!masterv) {
150 151
		bool changed;

152
		/* missing global ctx, create it now */
153
		if (br_vlan_add(br, vid, 0, &changed))
154
			return NULL;
155
		masterv = br_vlan_find(vg, vid);
156 157
		if (WARN_ON(!masterv))
			return NULL;
158 159
		refcount_set(&masterv->refcnt, 1);
		return masterv;
160
	}
161
	refcount_inc(&masterv->refcnt);
162 163 164 165

	return masterv;
}

166 167 168 169 170 171 172 173 174 175 176
static void br_master_vlan_rcu_free(struct rcu_head *rcu)
{
	struct net_bridge_vlan *v;

	v = container_of(rcu, struct net_bridge_vlan, rcu);
	WARN_ON(!br_vlan_is_master(v));
	free_percpu(v->stats);
	v->stats = NULL;
	kfree(v);
}

177 178
static void br_vlan_put_master(struct net_bridge_vlan *masterv)
{
179 180
	struct net_bridge_vlan_group *vg;

181 182 183
	if (!br_vlan_is_master(masterv))
		return;

184
	vg = br_vlan_group(masterv->br);
185
	if (refcount_dec_and_test(&masterv->refcnt)) {
186
		rhashtable_remove_fast(&vg->vlan_hash,
187 188
				       &masterv->vnode, br_vlan_rht_params);
		__vlan_del_list(masterv);
189
		call_rcu(&masterv->rcu, br_master_vlan_rcu_free);
190 191 192
	}
}

193 194 195 196
/* 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)
197
 * 2. vlan is being added on a bridge (both master and brentry flags)
198
 * 3. vlan is being added on a port, but a global entry didn't exist which
199
 *    is being created right now (master flag set, brentry flag unset), the
200
 *    global entry is used for global per-vlan features, but not for filtering
201
 * 4. same as 3 but with both master and brentry flags set so the entry
202 203 204
 *    will be used for filtering in both the port and the bridge
 */
static int __vlan_add(struct net_bridge_vlan *v, u16 flags)
205
{
206 207
	struct net_bridge_vlan *masterv = NULL;
	struct net_bridge_port *p = NULL;
208
	struct net_bridge_vlan_group *vg;
209 210 211 212 213 214 215
	struct net_device *dev;
	struct net_bridge *br;
	int err;

	if (br_vlan_is_master(v)) {
		br = v->br;
		dev = br->dev;
216
		vg = br_vlan_group(br);
217 218 219 220
	} else {
		p = v->port;
		br = p->br;
		dev = p->dev;
221
		vg = nbp_vlan_group(p);
222 223 224 225 226 227 228 229 230 231 232 233 234
	}

	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) {
235 236 237 238 239
			bool changed;

			err = br_vlan_add(br, v->vid,
					  flags | BRIDGE_VLAN_INFO_BRENTRY,
					  &changed);
240 241 242 243
			if (err)
				goto out_filt;
		}

244 245 246
		masterv = br_vlan_get_master(br, v->vid);
		if (!masterv)
			goto out_filt;
247
		v->brvlan = masterv;
248
		v->stats = masterv->stats;
249 250
	}

251
	/* Add the dev mac and count the vlan only if it's usable */
252 253 254 255 256 257
	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;
		}
258
		vg->num_vlans++;
259 260
	}

261 262
	err = rhashtable_lookup_insert_fast(&vg->vlan_hash, &v->vnode,
					    br_vlan_rht_params);
263 264
	if (err)
		goto out_fdb_insert;
265

266 267 268 269 270 271
	__vlan_add_list(v);
	__vlan_add_flags(v, flags);
out:
	return err;

out_fdb_insert:
272 273 274 275
	if (br_vlan_should_use(v)) {
		br_fdb_find_delete_local(br, p, dev->dev_addr, v->vid);
		vg->num_vlans--;
	}
276 277 278 279 280

out_filt:
	if (p) {
		__vlan_vid_del(dev, br, v->vid);
		if (masterv) {
281
			br_vlan_put_master(masterv);
282 283 284 285 286 287 288 289 290 291
			v->brvlan = NULL;
		}
	}

	goto out;
}

static int __vlan_del(struct net_bridge_vlan *v)
{
	struct net_bridge_vlan *masterv = v;
292
	struct net_bridge_vlan_group *vg;
293 294
	struct net_bridge_port *p = NULL;
	int err = 0;
295

296
	if (br_vlan_is_master(v)) {
297
		vg = br_vlan_group(v->br);
298 299
	} else {
		p = v->port;
300
		vg = nbp_vlan_group(v->port);
301 302
		masterv = v->brvlan;
	}
303

304
	__vlan_delete_pvid(vg, v->vid);
305 306
	if (p) {
		err = __vlan_vid_del(p->dev, p->br, v->vid);
307
		if (err)
308
			goto out;
309
	}
310

311 312 313
	if (br_vlan_should_use(v)) {
		v->flags &= ~BRIDGE_VLAN_INFO_BRENTRY;
		vg->num_vlans--;
314 315 316
	}

	if (masterv != v) {
317
		vlan_tunnel_info_del(vg, v);
318 319
		rhashtable_remove_fast(&vg->vlan_hash, &v->vnode,
				       br_vlan_rht_params);
320
		__vlan_del_list(v);
321 322
		kfree_rcu(v, rcu);
	}
323

324
	br_vlan_put_master(masterv);
325 326
out:
	return err;
327 328
}

329 330 331 332
static void __vlan_group_free(struct net_bridge_vlan_group *vg)
{
	WARN_ON(!list_empty(&vg->vlan_list));
	rhashtable_destroy(&vg->vlan_hash);
333
	vlan_tunnel_deinit(vg);
334 335 336 337
	kfree(vg);
}

static void __vlan_flush(struct net_bridge_vlan_group *vg)
338
{
339 340
	struct net_bridge_vlan *vlan, *tmp;

341 342
	__vlan_delete_pvid(vg, vg->pvid);
	list_for_each_entry_safe(vlan, tmp, &vg->vlan_list, vlist)
343
		__vlan_del(vlan);
344 345
}

346
struct sk_buff *br_handle_vlan(struct net_bridge *br,
347
			       const struct net_bridge_port *p,
348
			       struct net_bridge_vlan_group *vg,
349
			       struct sk_buff *skb)
350
{
351
	struct br_vlan_stats *stats;
352
	struct net_bridge_vlan *v;
353 354
	u16 vid;

355 356
	/* If this packet was not filtered at input, let it pass */
	if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
357 358
		goto out;

359 360 361 362 363 364 365
	/* 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
366 367 368 369
	 * 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.
	 */
370
	if (!v || !br_vlan_should_use(v)) {
371 372 373 374 375 376 377
		if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) {
			goto out;
		} else {
			kfree_skb(skb);
			return NULL;
		}
	}
378 379 380 381 382 383 384 385
	if (br->vlan_stats_enabled) {
		stats = this_cpu_ptr(v->stats);
		u64_stats_update_begin(&stats->syncp);
		stats->tx_bytes += skb->len;
		stats->tx_packets++;
		u64_stats_update_end(&stats->syncp);
	}

386
	if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
387
		skb->vlan_tci = 0;
388 389 390 391 392 393

	if (p && (p->flags & BR_VLAN_TUNNEL) &&
	    br_handle_egress_vlan_tunnel(skb, v)) {
		kfree_skb(skb);
		return NULL;
	}
394 395 396 397 398
out:
	return skb;
}

/* Called under RCU */
399 400
static bool __allowed_ingress(const struct net_bridge *br,
			      struct net_bridge_vlan_group *vg,
401
			      struct sk_buff *skb, u16 *vid)
402
{
403 404
	struct br_vlan_stats *stats;
	struct net_bridge_vlan *v;
405
	bool tagged;
406

407
	BR_INPUT_SKB_CB(skb)->vlan_filtered = true;
408 409 410 411
	/* 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.
	 */
412
	if (unlikely(!skb_vlan_tag_present(skb) &&
413
		     skb->protocol == br->vlan_proto)) {
414
		skb = skb_vlan_untag(skb);
415 416 417 418
		if (unlikely(!skb))
			return false;
	}

419 420
	if (!br_vlan_get_tag(skb, vid)) {
		/* Tagged frame */
421
		if (skb->vlan_proto != br->vlan_proto) {
422 423
			/* Protocol-mismatch, empty out vlan_tci for new tag */
			skb_push(skb, ETH_HLEN);
424
			skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
425
							skb_vlan_tag_get(skb));
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
			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;
	}

441
	if (!*vid) {
442 443
		u16 pvid = br_get_pvid(vg);

444 445 446
		/* 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.
447
		 */
V
Vlad Yasevich 已提交
448
		if (!pvid)
449
			goto drop;
450

451 452
		/* PVID is set on this port.  Any untagged or priority-tagged
		 * ingress frame is considered to belong to this vlan.
453
		 */
454
		*vid = pvid;
455
		if (likely(!tagged))
456
			/* Untagged Frame. */
457
			__vlan_hwaccel_put_tag(skb, br->vlan_proto, pvid);
458 459 460 461 462 463 464 465
		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;

466 467 468
		/* if stats are disabled we can avoid the lookup */
		if (!br->vlan_stats_enabled)
			return true;
469
	}
470
	v = br_vlan_find(vg, *vid);
471 472 473 474 475 476 477 478 479 480 481 482 483
	if (!v || !br_vlan_should_use(v))
		goto drop;

	if (br->vlan_stats_enabled) {
		stats = this_cpu_ptr(v->stats);
		u64_stats_update_begin(&stats->syncp);
		stats->rx_bytes += skb->len;
		stats->rx_packets++;
		u64_stats_update_end(&stats->syncp);
	}

	return true;

484 485
drop:
	kfree_skb(skb);
486 487 488
	return false;
}

489 490 491
bool br_allowed_ingress(const struct net_bridge *br,
			struct net_bridge_vlan_group *vg, struct sk_buff *skb,
			u16 *vid)
492 493 494 495 496 497 498 499 500
{
	/* 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;
	}

501
	return __allowed_ingress(br, vg, skb, vid);
502 503
}

504
/* Called under RCU. */
505
bool br_allowed_egress(struct net_bridge_vlan_group *vg,
506 507
		       const struct sk_buff *skb)
{
508
	const struct net_bridge_vlan *v;
509 510
	u16 vid;

511 512
	/* If this packet was not filtered at input, let it pass */
	if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
513 514 515
		return true;

	br_vlan_get_tag(skb, &vid);
516 517
	v = br_vlan_find(vg, vid);
	if (v && br_vlan_should_use(v))
518 519 520 521 522
		return true;

	return false;
}

523 524 525
/* Called under RCU */
bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid)
{
526
	struct net_bridge_vlan_group *vg;
527 528
	struct net_bridge *br = p->br;

529
	/* If filtering was disabled at input, let it pass. */
530
	if (!br->vlan_enabled)
531 532
		return true;

533
	vg = nbp_vlan_group_rcu(p);
534
	if (!vg || !vg->num_vlans)
535 536
		return false;

537 538 539
	if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto)
		*vid = 0;

540
	if (!*vid) {
541
		*vid = br_get_pvid(vg);
V
Vlad Yasevich 已提交
542
		if (!*vid)
543 544 545 546 547
			return false;

		return true;
	}

548
	if (br_vlan_find(vg, *vid))
549 550 551 552 553
		return true;

	return false;
}

554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
static int br_vlan_add_existing(struct net_bridge *br,
				struct net_bridge_vlan_group *vg,
				struct net_bridge_vlan *vlan,
				u16 flags, bool *changed)
{
	int err;

	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 */
		err = br_fdb_insert(br, NULL, br->dev->dev_addr,
				    vlan->vid);
		if (err) {
			br_err(br, "failed to insert local address into bridge forwarding table\n");
			return err;
		}

		refcount_inc(&vlan->refcnt);
		vlan->flags |= BRIDGE_VLAN_INFO_BRENTRY;
		vg->num_vlans++;
		*changed = true;
	}

	if (__vlan_add_flags(vlan, flags))
		*changed = true;

	return 0;
}

585 586
/* Must be protected by RTNL.
 * Must be called with vid in range from 1 to 4094 inclusive.
587
 * changed must be true only if the vlan was created or updated
588
 */
589
int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags, bool *changed)
590
{
591
	struct net_bridge_vlan_group *vg;
592 593
	struct net_bridge_vlan *vlan;
	int ret;
594 595 596

	ASSERT_RTNL();

597
	*changed = false;
598 599
	vg = br_vlan_group(br);
	vlan = br_vlan_find(vg, vid);
600 601
	if (vlan)
		return br_vlan_add_existing(br, vg, vlan, flags, changed);
602

603 604
	vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
	if (!vlan)
605 606
		return -ENOMEM;

607 608 609 610 611
	vlan->stats = netdev_alloc_pcpu_stats(struct br_vlan_stats);
	if (!vlan->stats) {
		kfree(vlan);
		return -ENOMEM;
	}
612 613 614 615 616
	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)
617
		refcount_set(&vlan->refcnt, 1);
618
	ret = __vlan_add(vlan, flags);
619 620
	if (ret) {
		free_percpu(vlan->stats);
621
		kfree(vlan);
622 623
	} else {
		*changed = true;
624
	}
625

626
	return ret;
627 628
}

629 630 631
/* Must be protected by RTNL.
 * Must be called with vid in range from 1 to 4094 inclusive.
 */
632 633
int br_vlan_delete(struct net_bridge *br, u16 vid)
{
634
	struct net_bridge_vlan_group *vg;
635
	struct net_bridge_vlan *v;
636 637 638

	ASSERT_RTNL();

639 640
	vg = br_vlan_group(br);
	v = br_vlan_find(vg, vid);
641 642
	if (!v || !br_vlan_is_brentry(v))
		return -ENOENT;
643

644
	br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
645
	br_fdb_delete_by_port(br, NULL, vid, 0);
646

647 648
	vlan_tunnel_info_del(vg, v);

649
	return __vlan_del(v);
650 651 652 653
}

void br_vlan_flush(struct net_bridge *br)
{
654 655
	struct net_bridge_vlan_group *vg;

656 657
	ASSERT_RTNL();

658 659 660 661 662
	vg = br_vlan_group(br);
	__vlan_flush(vg);
	RCU_INIT_POINTER(br->vlgrp, NULL);
	synchronize_rcu();
	__vlan_group_free(vg);
663 664
}

665
struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid)
666
{
667 668
	if (!vg)
		return NULL;
669

670
	return br_vlan_lookup(&vg->vlan_hash, vid);
671 672
}

673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
/* 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]);
}

700
int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
701
{
702 703 704 705 706 707 708 709
	struct switchdev_attr attr = {
		.orig_dev = br->dev,
		.id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
		.flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
		.u.vlan_filtering = val,
	};
	int err;

710
	if (br->vlan_enabled == val)
711
		return 0;
712

713 714 715 716
	err = switchdev_port_attr_set(br->dev, &attr);
	if (err && err != -EOPNOTSUPP)
		return err;

717
	br->vlan_enabled = val;
718
	br_manage_promisc(br);
719 720
	recalculate_group_addr(br);
	br_recalculate_fwd_mask(br);
721

722 723 724 725 726
	return 0;
}

int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
{
727
	return __br_vlan_filter_toggle(br, val);
728 729
}

730 731 732 733 734 735 736 737
bool br_vlan_enabled(const struct net_device *dev)
{
	struct net_bridge *br = netdev_priv(dev);

	return !!br->vlan_enabled;
}
EXPORT_SYMBOL_GPL(br_vlan_enabled);

738
int __br_vlan_set_proto(struct net_bridge *br, __be16 proto)
739 740 741
{
	int err = 0;
	struct net_bridge_port *p;
742
	struct net_bridge_vlan *vlan;
743
	struct net_bridge_vlan_group *vg;
744
	__be16 oldproto;
745 746

	if (br->vlan_proto == proto)
747
		return 0;
748 749 750

	/* Add VLANs for the new proto to the device filter. */
	list_for_each_entry(p, &br->port_list, list) {
751 752
		vg = nbp_vlan_group(p);
		list_for_each_entry(vlan, &vg->vlan_list, vlist) {
753
			err = vlan_vid_add(p->dev, proto, vlan->vid);
754 755 756 757 758 759 760 761 762 763 764 765
			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. */
766 767 768
	list_for_each_entry(p, &br->port_list, list) {
		vg = nbp_vlan_group(p);
		list_for_each_entry(vlan, &vg->vlan_list, vlist)
769
			vlan_vid_del(p->dev, oldproto, vlan->vid);
770
	}
771

772
	return 0;
773 774

err_filt:
775
	list_for_each_entry_continue_reverse(vlan, &vg->vlan_list, vlist)
776
		vlan_vid_del(p->dev, proto, vlan->vid);
777

778 779 780
	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)
781
			vlan_vid_del(p->dev, proto, vlan->vid);
782
	}
783

784 785 786 787 788 789 790 791
	return err;
}

int br_vlan_set_proto(struct net_bridge *br, unsigned long val)
{
	if (val != ETH_P_8021Q && val != ETH_P_8021AD)
		return -EPROTONOSUPPORT;

792
	return __br_vlan_set_proto(br, htons(val));
793 794
}

795 796 797 798 799 800 801 802 803 804 805 806 807 808
int br_vlan_set_stats(struct net_bridge *br, unsigned long val)
{
	switch (val) {
	case 0:
	case 1:
		br->vlan_stats_enabled = val;
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

809
static bool vlan_default_pvid(struct net_bridge_vlan_group *vg, u16 vid)
810
{
811 812
	struct net_bridge_vlan *v;

813
	if (vid != vg->pvid)
814 815 816 817 818 819 820 821
		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;
822 823 824 825 826 827 828 829 830 831
}

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.
	 */
832
	if (vlan_default_pvid(br_vlan_group(br), pvid))
833 834 835
		br_vlan_delete(br, pvid);

	list_for_each_entry(p, &br->port_list, list) {
836
		if (vlan_default_pvid(nbp_vlan_group(p), pvid))
837 838 839 840 841 842
			nbp_vlan_delete(p, pvid);
	}

	br->default_pvid = 0;
}

843
int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid)
844
{
845
	const struct net_bridge_vlan *pvent;
846
	struct net_bridge_vlan_group *vg;
847
	struct net_bridge_port *p;
848 849
	unsigned long *changed;
	bool vlchange;
850 851 852
	u16 old_pvid;
	int err = 0;

853 854 855 856 857
	if (!pvid) {
		br_vlan_disable_default_pvid(br);
		return 0;
	}

858 859 860 861 862 863 864 865 866 867
	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.
	 */
868 869 870
	vg = br_vlan_group(br);
	pvent = br_vlan_find(vg, pvid);
	if ((!old_pvid || vlan_default_pvid(vg, old_pvid)) &&
871
	    (!pvent || !br_vlan_should_use(pvent))) {
872 873
		err = br_vlan_add(br, pvid,
				  BRIDGE_VLAN_INFO_PVID |
874
				  BRIDGE_VLAN_INFO_UNTAGGED |
875 876
				  BRIDGE_VLAN_INFO_BRENTRY,
				  &vlchange);
877 878 879 880 881 882 883 884 885 886
		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.
		 */
887
		vg = nbp_vlan_group(p);
888
		if ((old_pvid &&
889 890
		     !vlan_default_pvid(vg, old_pvid)) ||
		    br_vlan_find(vg, pvid))
891 892 893 894
			continue;

		err = nbp_vlan_add(p, pvid,
				   BRIDGE_VLAN_INFO_PVID |
895 896
				   BRIDGE_VLAN_INFO_UNTAGGED,
				   &vlchange);
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
		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 |
917 918
				     BRIDGE_VLAN_INFO_UNTAGGED,
				     &vlchange);
919 920 921 922 923 924 925
		nbp_vlan_delete(p, pvid);
	}

	if (test_bit(0, changed)) {
		if (old_pvid)
			br_vlan_add(br, old_pvid,
				    BRIDGE_VLAN_INFO_PVID |
926
				    BRIDGE_VLAN_INFO_UNTAGGED |
927 928
				    BRIDGE_VLAN_INFO_BRENTRY,
				    &vlchange);
929 930 931 932 933
		br_vlan_delete(br, pvid);
	}
	goto out;
}

934 935 936 937 938
int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val)
{
	u16 pvid = val;
	int err = 0;

939
	if (val >= VLAN_VID_MASK)
940 941 942
		return -EINVAL;

	if (pvid == br->default_pvid)
943
		goto out;
944 945 946 947 948

	/* 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;
949
		goto out;
950
	}
951
	err = __br_vlan_set_default_pvid(br, pvid);
952
out:
953 954 955
	return err;
}

956
int br_vlan_init(struct net_bridge *br)
957
{
958
	struct net_bridge_vlan_group *vg;
959
	int ret = -ENOMEM;
960
	bool changed;
961

962 963
	vg = kzalloc(sizeof(*vg), GFP_KERNEL);
	if (!vg)
964
		goto out;
965
	ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
966 967
	if (ret)
		goto err_rhtbl;
968 969 970
	ret = vlan_tunnel_init(vg);
	if (ret)
		goto err_tunnel_init;
971
	INIT_LIST_HEAD(&vg->vlan_list);
972
	br->vlan_proto = htons(ETH_P_8021Q);
973
	br->default_pvid = 1;
974
	rcu_assign_pointer(br->vlgrp, vg);
975 976
	ret = br_vlan_add(br, 1,
			  BRIDGE_VLAN_INFO_PVID | BRIDGE_VLAN_INFO_UNTAGGED |
977
			  BRIDGE_VLAN_INFO_BRENTRY, &changed);
978 979 980 981 982 983 984
	if (ret)
		goto err_vlan_add;

out:
	return ret;

err_vlan_add:
985 986
	vlan_tunnel_deinit(vg);
err_tunnel_init:
987
	rhashtable_destroy(&vg->vlan_hash);
988
err_rhtbl:
989
	kfree(vg);
990 991 992 993 994 995

	goto out;
}

int nbp_vlan_init(struct net_bridge_port *p)
{
996 997 998 999 1000 1001
	struct switchdev_attr attr = {
		.orig_dev = p->br->dev,
		.id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
		.flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
		.u.vlan_filtering = p->br->vlan_enabled,
	};
1002
	struct net_bridge_vlan_group *vg;
1003 1004
	int ret = -ENOMEM;

1005 1006
	vg = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL);
	if (!vg)
1007 1008
		goto out;

1009 1010 1011 1012
	ret = switchdev_port_attr_set(p->dev, &attr);
	if (ret && ret != -EOPNOTSUPP)
		goto err_vlan_enabled;

1013
	ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
1014 1015
	if (ret)
		goto err_rhtbl;
1016 1017 1018
	ret = vlan_tunnel_init(vg);
	if (ret)
		goto err_tunnel_init;
1019
	INIT_LIST_HEAD(&vg->vlan_list);
1020
	rcu_assign_pointer(p->vlgrp, vg);
1021
	if (p->br->default_pvid) {
1022 1023
		bool changed;

1024 1025
		ret = nbp_vlan_add(p, p->br->default_pvid,
				   BRIDGE_VLAN_INFO_PVID |
1026 1027
				   BRIDGE_VLAN_INFO_UNTAGGED,
				   &changed);
1028 1029 1030 1031 1032 1033 1034
		if (ret)
			goto err_vlan_add;
	}
out:
	return ret;

err_vlan_add:
1035 1036
	RCU_INIT_POINTER(p->vlgrp, NULL);
	synchronize_rcu();
1037 1038 1039
	vlan_tunnel_deinit(vg);
err_tunnel_init:
	rhashtable_destroy(&vg->vlan_hash);
1040
err_rhtbl:
1041
err_vlan_enabled:
1042
	kfree(vg);
1043 1044

	goto out;
1045 1046
}

1047 1048
/* Must be protected by RTNL.
 * Must be called with vid in range from 1 to 4094 inclusive.
1049
 * changed must be true only if the vlan was created or updated
1050
 */
1051 1052
int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags,
		 bool *changed)
1053
{
1054 1055
	struct net_bridge_vlan *vlan;
	int ret;
1056 1057 1058

	ASSERT_RTNL();

1059
	*changed = false;
1060
	vlan = br_vlan_find(nbp_vlan_group(port), vid);
1061
	if (vlan) {
1062
		/* Pass the flags to the hardware bridge */
1063
		ret = br_switchdev_port_vlan_add(port->dev, vid, flags);
1064 1065
		if (ret && ret != -EOPNOTSUPP)
			return ret;
1066 1067
		*changed = __vlan_add_flags(vlan, flags);

1068
		return 0;
1069 1070
	}

1071 1072 1073
	vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
	if (!vlan)
		return -ENOMEM;
1074

1075 1076 1077 1078 1079
	vlan->vid = vid;
	vlan->port = port;
	ret = __vlan_add(vlan, flags);
	if (ret)
		kfree(vlan);
1080 1081
	else
		*changed = true;
1082

1083
	return ret;
1084 1085
}

1086 1087 1088
/* Must be protected by RTNL.
 * Must be called with vid in range from 1 to 4094 inclusive.
 */
1089 1090
int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
{
1091
	struct net_bridge_vlan *v;
1092 1093 1094

	ASSERT_RTNL();

1095
	v = br_vlan_find(nbp_vlan_group(port), vid);
1096 1097
	if (!v)
		return -ENOENT;
1098
	br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
1099
	br_fdb_delete_by_port(port->br, port, vid, 0);
1100

1101
	return __vlan_del(v);
1102 1103 1104 1105
}

void nbp_vlan_flush(struct net_bridge_port *port)
{
1106 1107
	struct net_bridge_vlan_group *vg;

1108 1109
	ASSERT_RTNL();

1110 1111 1112 1113 1114
	vg = nbp_vlan_group(port);
	__vlan_flush(vg);
	RCU_INIT_POINTER(port->vlgrp, NULL);
	synchronize_rcu();
	__vlan_group_free(vg);
1115
}
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142

void br_vlan_get_stats(const struct net_bridge_vlan *v,
		       struct br_vlan_stats *stats)
{
	int i;

	memset(stats, 0, sizeof(*stats));
	for_each_possible_cpu(i) {
		u64 rxpackets, rxbytes, txpackets, txbytes;
		struct br_vlan_stats *cpu_stats;
		unsigned int start;

		cpu_stats = per_cpu_ptr(v->stats, i);
		do {
			start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
			rxpackets = cpu_stats->rx_packets;
			rxbytes = cpu_stats->rx_bytes;
			txbytes = cpu_stats->tx_bytes;
			txpackets = cpu_stats->tx_packets;
		} while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));

		stats->rx_packets += rxpackets;
		stats->rx_bytes += rxbytes;
		stats->tx_bytes += txbytes;
		stats->tx_packets += txpackets;
	}
}
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169

int br_vlan_get_pvid(const struct net_device *dev, u16 *p_pvid)
{
	struct net_bridge_vlan_group *vg;

	ASSERT_RTNL();
	if (netif_is_bridge_master(dev))
		vg = br_vlan_group(netdev_priv(dev));
	else
		return -EINVAL;

	*p_pvid = br_get_pvid(vg);
	return 0;
}
EXPORT_SYMBOL_GPL(br_vlan_get_pvid);

int br_vlan_get_info(const struct net_device *dev, u16 vid,
		     struct bridge_vlan_info *p_vinfo)
{
	struct net_bridge_vlan_group *vg;
	struct net_bridge_vlan *v;
	struct net_bridge_port *p;

	ASSERT_RTNL();
	p = br_port_get_check_rtnl(dev);
	if (p)
		vg = nbp_vlan_group(p);
1170 1171
	else if (netif_is_bridge_master(dev))
		vg = br_vlan_group(netdev_priv(dev));
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
	else
		return -EINVAL;

	v = br_vlan_find(vg, vid);
	if (!v)
		return -ENOENT;

	p_vinfo->vid = vid;
	p_vinfo->flags = v->flags;
	return 0;
}
EXPORT_SYMBOL_GPL(br_vlan_get_info);