br_vlan.c 31.9 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
	.nelem_hint = 3,
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 bool __vlan_add_pvid(struct net_bridge_vlan_group *vg, u16 vid)
35
{
36
	if (vg->pvid == vid)
37
		return false;
38 39

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

	return true;
43 44
}

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

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

	return true;
54 55
}

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

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

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

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

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

81
static int __vlan_vid_add(struct net_device *dev, struct net_bridge *br,
82 83
			  struct net_bridge_vlan *v, u16 flags,
			  struct netlink_ext_ack *extack)
84 85 86
{
	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, v->vid, flags, extack);
91
	if (err == -EOPNOTSUPP)
92 93
		return vlan_vid_add(dev, br->vlan_proto, v->vid);
	v->priv_flags |= BR_VLFLAG_ADDED_BY_SWITCHDEV;
94 95 96
	return err;
}

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

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

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

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

124
static int __vlan_vid_del(struct net_device *dev, struct net_bridge *br,
125
			  const struct net_bridge_vlan *v)
126
{
127
	int err;
128

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

138 139 140
/* 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.
 */
141 142 143
static struct net_bridge_vlan *
br_vlan_get_master(struct net_bridge *br, u16 vid,
		   struct netlink_ext_ack *extack)
144
{
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
	if (!masterv) {
151 152
		bool changed;

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

	return masterv;
}

167 168 169 170 171 172 173 174 175 176 177
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);
}

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

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

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

194 195 196 197 198 199 200
static void nbp_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));
	/* if we had per-port stats configured then free them here */
201
	if (v->priv_flags & BR_VLFLAG_PER_PORT_STATS)
202 203 204 205 206
		free_percpu(v->stats);
	v->stats = NULL;
	kfree(v);
}

207 208 209 210
/* 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)
211
 * 2. vlan is being added on a bridge (both master and brentry flags)
212
 * 3. vlan is being added on a port, but a global entry didn't exist which
213
 *    is being created right now (master flag set, brentry flag unset), the
214
 *    global entry is used for global per-vlan features, but not for filtering
215
 * 4. same as 3 but with both master and brentry flags set so the entry
216 217
 *    will be used for filtering in both the port and the bridge
 */
218 219
static int __vlan_add(struct net_bridge_vlan *v, u16 flags,
		      struct netlink_ext_ack *extack)
220
{
221 222
	struct net_bridge_vlan *masterv = NULL;
	struct net_bridge_port *p = NULL;
223
	struct net_bridge_vlan_group *vg;
224 225 226 227 228 229 230
	struct net_device *dev;
	struct net_bridge *br;
	int err;

	if (br_vlan_is_master(v)) {
		br = v->br;
		dev = br->dev;
231
		vg = br_vlan_group(br);
232 233 234 235
	} else {
		p = v->port;
		br = p->br;
		dev = p->dev;
236
		vg = nbp_vlan_group(p);
237 238 239 240 241 242 243
	}

	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().
		 */
244
		err = __vlan_vid_add(dev, br, v, flags, extack);
245 246 247 248 249
		if (err)
			goto out;

		/* need to work on the master vlan too */
		if (flags & BRIDGE_VLAN_INFO_MASTER) {
250 251 252 253
			bool changed;

			err = br_vlan_add(br, v->vid,
					  flags | BRIDGE_VLAN_INFO_BRENTRY,
254
					  &changed, extack);
255 256 257 258
			if (err)
				goto out_filt;
		}

259
		masterv = br_vlan_get_master(br, v->vid, extack);
260 261
		if (!masterv)
			goto out_filt;
262
		v->brvlan = masterv;
263 264 265 266 267 268
		if (br_opt_get(br, BROPT_VLAN_STATS_PER_PORT)) {
			v->stats = netdev_alloc_pcpu_stats(struct br_vlan_stats);
			if (!v->stats) {
				err = -ENOMEM;
				goto out_filt;
			}
269
			v->priv_flags |= BR_VLFLAG_PER_PORT_STATS;
270 271 272
		} else {
			v->stats = masterv->stats;
		}
273
	} else {
274
		err = br_switchdev_port_vlan_add(dev, v->vid, flags, extack);
275 276
		if (err && err != -EOPNOTSUPP)
			goto out;
277 278
	}

279
	/* Add the dev mac and count the vlan only if it's usable */
280 281 282 283 284 285
	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;
		}
286
		vg->num_vlans++;
287 288
	}

289 290
	err = rhashtable_lookup_insert_fast(&vg->vlan_hash, &v->vnode,
					    br_vlan_rht_params);
291 292
	if (err)
		goto out_fdb_insert;
293

294 295 296 297 298 299
	__vlan_add_list(v);
	__vlan_add_flags(v, flags);
out:
	return err;

out_fdb_insert:
300 301 302 303
	if (br_vlan_should_use(v)) {
		br_fdb_find_delete_local(br, p, dev->dev_addr, v->vid);
		vg->num_vlans--;
	}
304 305 306

out_filt:
	if (p) {
307
		__vlan_vid_del(dev, br, v);
308
		if (masterv) {
309 310 311 312
			if (v->stats && masterv->stats != v->stats)
				free_percpu(v->stats);
			v->stats = NULL;

313
			br_vlan_put_master(masterv);
314 315
			v->brvlan = NULL;
		}
316 317
	} else {
		br_switchdev_port_vlan_del(dev, v->vid);
318 319 320 321 322 323 324 325
	}

	goto out;
}

static int __vlan_del(struct net_bridge_vlan *v)
{
	struct net_bridge_vlan *masterv = v;
326
	struct net_bridge_vlan_group *vg;
327 328
	struct net_bridge_port *p = NULL;
	int err = 0;
329

330
	if (br_vlan_is_master(v)) {
331
		vg = br_vlan_group(v->br);
332 333
	} else {
		p = v->port;
334
		vg = nbp_vlan_group(v->port);
335 336
		masterv = v->brvlan;
	}
337

338
	__vlan_delete_pvid(vg, v->vid);
339
	if (p) {
340
		err = __vlan_vid_del(p->dev, p->br, v);
341
		if (err)
342
			goto out;
343 344 345 346 347
	} else {
		err = br_switchdev_port_vlan_del(v->br->dev, v->vid);
		if (err && err != -EOPNOTSUPP)
			goto out;
		err = 0;
348
	}
349

350 351 352
	if (br_vlan_should_use(v)) {
		v->flags &= ~BRIDGE_VLAN_INFO_BRENTRY;
		vg->num_vlans--;
353 354 355
	}

	if (masterv != v) {
356
		vlan_tunnel_info_del(vg, v);
357 358
		rhashtable_remove_fast(&vg->vlan_hash, &v->vnode,
				       br_vlan_rht_params);
359
		__vlan_del_list(v);
360
		call_rcu(&v->rcu, nbp_vlan_rcu_free);
361
	}
362

363
	br_vlan_put_master(masterv);
364 365
out:
	return err;
366 367
}

368 369 370 371
static void __vlan_group_free(struct net_bridge_vlan_group *vg)
{
	WARN_ON(!list_empty(&vg->vlan_list));
	rhashtable_destroy(&vg->vlan_hash);
372
	vlan_tunnel_deinit(vg);
373 374 375 376
	kfree(vg);
}

static void __vlan_flush(struct net_bridge_vlan_group *vg)
377
{
378 379
	struct net_bridge_vlan *vlan, *tmp;

380 381
	__vlan_delete_pvid(vg, vg->pvid);
	list_for_each_entry_safe(vlan, tmp, &vg->vlan_list, vlist)
382
		__vlan_del(vlan);
383 384
}

385
struct sk_buff *br_handle_vlan(struct net_bridge *br,
386
			       const struct net_bridge_port *p,
387
			       struct net_bridge_vlan_group *vg,
388
			       struct sk_buff *skb)
389
{
390
	struct br_vlan_stats *stats;
391
	struct net_bridge_vlan *v;
392 393
	u16 vid;

394 395
	/* If this packet was not filtered at input, let it pass */
	if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
396 397
		goto out;

398 399 400 401 402 403 404
	/* 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
405 406 407 408
	 * 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.
	 */
409
	if (!v || !br_vlan_should_use(v)) {
410 411 412 413 414 415 416
		if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) {
			goto out;
		} else {
			kfree_skb(skb);
			return NULL;
		}
	}
417
	if (br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) {
418 419 420 421 422 423 424
		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);
	}

425
	if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
426
		__vlan_hwaccel_clear_tag(skb);
427 428 429 430 431 432

	if (p && (p->flags & BR_VLAN_TUNNEL) &&
	    br_handle_egress_vlan_tunnel(skb, v)) {
		kfree_skb(skb);
		return NULL;
	}
433 434 435 436 437
out:
	return skb;
}

/* Called under RCU */
438 439
static bool __allowed_ingress(const struct net_bridge *br,
			      struct net_bridge_vlan_group *vg,
440
			      struct sk_buff *skb, u16 *vid)
441
{
442 443
	struct br_vlan_stats *stats;
	struct net_bridge_vlan *v;
444
	bool tagged;
445

446
	BR_INPUT_SKB_CB(skb)->vlan_filtered = true;
447 448 449 450
	/* 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.
	 */
451
	if (unlikely(!skb_vlan_tag_present(skb) &&
452
		     skb->protocol == br->vlan_proto)) {
453
		skb = skb_vlan_untag(skb);
454 455 456 457
		if (unlikely(!skb))
			return false;
	}

458 459
	if (!br_vlan_get_tag(skb, vid)) {
		/* Tagged frame */
460
		if (skb->vlan_proto != br->vlan_proto) {
461 462
			/* Protocol-mismatch, empty out vlan_tci for new tag */
			skb_push(skb, ETH_HLEN);
463
			skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
464
							skb_vlan_tag_get(skb));
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
			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;
	}

480
	if (!*vid) {
481 482
		u16 pvid = br_get_pvid(vg);

483 484 485
		/* 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.
486
		 */
V
Vlad Yasevich 已提交
487
		if (!pvid)
488
			goto drop;
489

490 491
		/* PVID is set on this port.  Any untagged or priority-tagged
		 * ingress frame is considered to belong to this vlan.
492
		 */
493
		*vid = pvid;
494
		if (likely(!tagged))
495
			/* Untagged Frame. */
496
			__vlan_hwaccel_put_tag(skb, br->vlan_proto, pvid);
497 498
		else
			/* Priority-tagged Frame.
499 500
			 * At this point, we know that skb->vlan_tci VID
			 * field was 0.
501 502 503 504
			 * We update only VID field and preserve PCP field.
			 */
			skb->vlan_tci |= pvid;

505
		/* if stats are disabled we can avoid the lookup */
506
		if (!br_opt_get(br, BROPT_VLAN_STATS_ENABLED))
507
			return true;
508
	}
509
	v = br_vlan_find(vg, *vid);
510 511 512
	if (!v || !br_vlan_should_use(v))
		goto drop;

513
	if (br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) {
514 515 516 517 518 519 520 521 522
		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;

523 524
drop:
	kfree_skb(skb);
525 526 527
	return false;
}

528 529 530
bool br_allowed_ingress(const struct net_bridge *br,
			struct net_bridge_vlan_group *vg, struct sk_buff *skb,
			u16 *vid)
531 532 533 534
{
	/* If VLAN filtering is disabled on the bridge, all packets are
	 * permitted.
	 */
535
	if (!br_opt_get(br, BROPT_VLAN_ENABLED)) {
536 537 538 539
		BR_INPUT_SKB_CB(skb)->vlan_filtered = false;
		return true;
	}

540
	return __allowed_ingress(br, vg, skb, vid);
541 542
}

543
/* Called under RCU. */
544
bool br_allowed_egress(struct net_bridge_vlan_group *vg,
545 546
		       const struct sk_buff *skb)
{
547
	const struct net_bridge_vlan *v;
548 549
	u16 vid;

550 551
	/* If this packet was not filtered at input, let it pass */
	if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
552 553 554
		return true;

	br_vlan_get_tag(skb, &vid);
555 556
	v = br_vlan_find(vg, vid);
	if (v && br_vlan_should_use(v))
557 558 559 560 561
		return true;

	return false;
}

562 563 564
/* Called under RCU */
bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid)
{
565
	struct net_bridge_vlan_group *vg;
566 567
	struct net_bridge *br = p->br;

568
	/* If filtering was disabled at input, let it pass. */
569
	if (!br_opt_get(br, BROPT_VLAN_ENABLED))
570 571
		return true;

572
	vg = nbp_vlan_group_rcu(p);
573
	if (!vg || !vg->num_vlans)
574 575
		return false;

576 577 578
	if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto)
		*vid = 0;

579
	if (!*vid) {
580
		*vid = br_get_pvid(vg);
V
Vlad Yasevich 已提交
581
		if (!*vid)
582 583 584 585 586
			return false;

		return true;
	}

587
	if (br_vlan_find(vg, *vid))
588 589 590 591 592
		return true;

	return false;
}

593 594 595
static int br_vlan_add_existing(struct net_bridge *br,
				struct net_bridge_vlan_group *vg,
				struct net_bridge_vlan *vlan,
596 597
				u16 flags, bool *changed,
				struct netlink_ext_ack *extack)
598 599 600
{
	int err;

601
	err = br_switchdev_port_vlan_add(br->dev, vlan->vid, flags, extack);
602 603 604
	if (err && err != -EOPNOTSUPP)
		return err;

605 606
	if (!br_vlan_is_brentry(vlan)) {
		/* Trying to change flags of non-existent bridge vlan */
607 608 609 610
		if (!(flags & BRIDGE_VLAN_INFO_BRENTRY)) {
			err = -EINVAL;
			goto err_flags;
		}
611 612 613 614 615
		/* 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");
616
			goto err_fdb_insert;
617 618 619 620 621 622 623 624 625 626 627 628
		}

		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;
629 630 631 632 633

err_fdb_insert:
err_flags:
	br_switchdev_port_vlan_del(br->dev, vlan->vid);
	return err;
634 635
}

636 637
/* Must be protected by RTNL.
 * Must be called with vid in range from 1 to 4094 inclusive.
638
 * changed must be true only if the vlan was created or updated
639
 */
640 641
int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags, bool *changed,
		struct netlink_ext_ack *extack)
642
{
643
	struct net_bridge_vlan_group *vg;
644 645
	struct net_bridge_vlan *vlan;
	int ret;
646 647 648

	ASSERT_RTNL();

649
	*changed = false;
650 651
	vg = br_vlan_group(br);
	vlan = br_vlan_find(vg, vid);
652
	if (vlan)
653 654
		return br_vlan_add_existing(br, vg, vlan, flags, changed,
					    extack);
655

656 657
	vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
	if (!vlan)
658 659
		return -ENOMEM;

660 661 662 663 664
	vlan->stats = netdev_alloc_pcpu_stats(struct br_vlan_stats);
	if (!vlan->stats) {
		kfree(vlan);
		return -ENOMEM;
	}
665 666 667 668 669
	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)
670
		refcount_set(&vlan->refcnt, 1);
671
	ret = __vlan_add(vlan, flags, extack);
672 673
	if (ret) {
		free_percpu(vlan->stats);
674
		kfree(vlan);
675 676
	} else {
		*changed = true;
677
	}
678

679
	return ret;
680 681
}

682 683 684
/* Must be protected by RTNL.
 * Must be called with vid in range from 1 to 4094 inclusive.
 */
685 686
int br_vlan_delete(struct net_bridge *br, u16 vid)
{
687
	struct net_bridge_vlan_group *vg;
688
	struct net_bridge_vlan *v;
689 690 691

	ASSERT_RTNL();

692 693
	vg = br_vlan_group(br);
	v = br_vlan_find(vg, vid);
694 695
	if (!v || !br_vlan_is_brentry(v))
		return -ENOENT;
696

697
	br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
698
	br_fdb_delete_by_port(br, NULL, vid, 0);
699

700 701
	vlan_tunnel_info_del(vg, v);

702
	return __vlan_del(v);
703 704 705 706
}

void br_vlan_flush(struct net_bridge *br)
{
707 708
	struct net_bridge_vlan_group *vg;

709 710
	ASSERT_RTNL();

711 712 713 714 715
	vg = br_vlan_group(br);
	__vlan_flush(vg);
	RCU_INIT_POINTER(br->vlgrp, NULL);
	synchronize_rcu();
	__vlan_group_free(vg);
716 717
}

718
struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid)
719
{
720 721
	if (!vg)
		return NULL;
722

723
	return br_vlan_lookup(&vg->vlan_hash, vid);
724 725
}

726 727 728
/* Must be protected by RTNL. */
static void recalculate_group_addr(struct net_bridge *br)
{
729
	if (br_opt_get(br, BROPT_GROUP_ADDR_SET))
730 731 732
		return;

	spin_lock_bh(&br->lock);
733 734
	if (!br_opt_get(br, BROPT_VLAN_ENABLED) ||
	    br->vlan_proto == htons(ETH_P_8021Q)) {
735 736 737 738 739 740 741 742 743 744 745 746
		/* 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)
{
747 748
	if (!br_opt_get(br, BROPT_VLAN_ENABLED) ||
	    br->vlan_proto == htons(ETH_P_8021Q))
749 750 751 752 753 754
		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]);
}

755
int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
756
{
757 758 759 760 761 762 763 764
	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;

765
	if (br_opt_get(br, BROPT_VLAN_ENABLED) == !!val)
766
		return 0;
767

768 769 770 771
	err = switchdev_port_attr_set(br->dev, &attr);
	if (err && err != -EOPNOTSUPP)
		return err;

772
	br_opt_toggle(br, BROPT_VLAN_ENABLED, !!val);
773
	br_manage_promisc(br);
774 775
	recalculate_group_addr(br);
	br_recalculate_fwd_mask(br);
776

777 778 779 780 781
	return 0;
}

int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
{
782
	return __br_vlan_filter_toggle(br, val);
783 784
}

785 786 787 788
bool br_vlan_enabled(const struct net_device *dev)
{
	struct net_bridge *br = netdev_priv(dev);

789
	return br_opt_get(br, BROPT_VLAN_ENABLED);
790 791 792
}
EXPORT_SYMBOL_GPL(br_vlan_enabled);

793
int __br_vlan_set_proto(struct net_bridge *br, __be16 proto)
794 795 796
{
	int err = 0;
	struct net_bridge_port *p;
797
	struct net_bridge_vlan *vlan;
798
	struct net_bridge_vlan_group *vg;
799
	__be16 oldproto;
800 801

	if (br->vlan_proto == proto)
802
		return 0;
803 804 805

	/* Add VLANs for the new proto to the device filter. */
	list_for_each_entry(p, &br->port_list, list) {
806 807
		vg = nbp_vlan_group(p);
		list_for_each_entry(vlan, &vg->vlan_list, vlist) {
808
			err = vlan_vid_add(p->dev, proto, vlan->vid);
809 810 811 812 813 814 815 816 817 818 819 820
			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. */
821 822 823
	list_for_each_entry(p, &br->port_list, list) {
		vg = nbp_vlan_group(p);
		list_for_each_entry(vlan, &vg->vlan_list, vlist)
824
			vlan_vid_del(p->dev, oldproto, vlan->vid);
825
	}
826

827
	return 0;
828 829

err_filt:
830
	list_for_each_entry_continue_reverse(vlan, &vg->vlan_list, vlist)
831
		vlan_vid_del(p->dev, proto, vlan->vid);
832

833 834 835
	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)
836
			vlan_vid_del(p->dev, proto, vlan->vid);
837
	}
838

839 840 841 842 843 844 845 846
	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;

847
	return __br_vlan_set_proto(br, htons(val));
848 849
}

850 851 852 853 854
int br_vlan_set_stats(struct net_bridge *br, unsigned long val)
{
	switch (val) {
	case 0:
	case 1:
855
		br_opt_toggle(br, BROPT_VLAN_STATS_ENABLED, !!val);
856 857
		break;
	default:
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
		return -EINVAL;
	}

	return 0;
}

int br_vlan_set_stats_per_port(struct net_bridge *br, unsigned long val)
{
	struct net_bridge_port *p;

	/* allow to change the option if there are no port vlans configured */
	list_for_each_entry(p, &br->port_list, list) {
		struct net_bridge_vlan_group *vg = nbp_vlan_group(p);

		if (vg->num_vlans)
			return -EBUSY;
	}

	switch (val) {
	case 0:
	case 1:
		br_opt_toggle(br, BROPT_VLAN_STATS_PER_PORT, !!val);
		break;
	default:
882 883 884 885 886 887
		return -EINVAL;
	}

	return 0;
}

888
static bool vlan_default_pvid(struct net_bridge_vlan_group *vg, u16 vid)
889
{
890 891
	struct net_bridge_vlan *v;

892
	if (vid != vg->pvid)
893 894 895 896 897 898 899 900
		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;
901 902 903 904 905 906 907 908 909 910
}

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.
	 */
911
	if (vlan_default_pvid(br_vlan_group(br), pvid))
912 913 914
		br_vlan_delete(br, pvid);

	list_for_each_entry(p, &br->port_list, list) {
915
		if (vlan_default_pvid(nbp_vlan_group(p), pvid))
916 917 918 919 920 921
			nbp_vlan_delete(p, pvid);
	}

	br->default_pvid = 0;
}

922 923
int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid,
			       struct netlink_ext_ack *extack)
924
{
925
	const struct net_bridge_vlan *pvent;
926
	struct net_bridge_vlan_group *vg;
927
	struct net_bridge_port *p;
928 929
	unsigned long *changed;
	bool vlchange;
930 931 932
	u16 old_pvid;
	int err = 0;

933 934 935 936 937
	if (!pvid) {
		br_vlan_disable_default_pvid(br);
		return 0;
	}

938
	changed = bitmap_zalloc(BR_MAX_PORTS, GFP_KERNEL);
939 940 941 942 943 944 945 946
	if (!changed)
		return -ENOMEM;

	old_pvid = br->default_pvid;

	/* Update default_pvid config only if we do not conflict with
	 * user configuration.
	 */
947 948 949
	vg = br_vlan_group(br);
	pvent = br_vlan_find(vg, pvid);
	if ((!old_pvid || vlan_default_pvid(vg, old_pvid)) &&
950
	    (!pvent || !br_vlan_should_use(pvent))) {
951 952
		err = br_vlan_add(br, pvid,
				  BRIDGE_VLAN_INFO_PVID |
953
				  BRIDGE_VLAN_INFO_UNTAGGED |
954
				  BRIDGE_VLAN_INFO_BRENTRY,
955
				  &vlchange, extack);
956 957 958 959 960 961 962 963 964 965
		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.
		 */
966
		vg = nbp_vlan_group(p);
967
		if ((old_pvid &&
968 969
		     !vlan_default_pvid(vg, old_pvid)) ||
		    br_vlan_find(vg, pvid))
970 971 972 973
			continue;

		err = nbp_vlan_add(p, pvid,
				   BRIDGE_VLAN_INFO_PVID |
974
				   BRIDGE_VLAN_INFO_UNTAGGED,
975
				   &vlchange, extack);
976 977 978 979 980 981 982 983 984
		if (err)
			goto err_port;
		nbp_vlan_delete(p, old_pvid);
		set_bit(p->port_no, changed);
	}

	br->default_pvid = pvid;

out:
985
	bitmap_free(changed);
986 987 988 989 990 991 992 993 994 995
	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 |
996
				     BRIDGE_VLAN_INFO_UNTAGGED,
997
				     &vlchange, NULL);
998 999 1000 1001 1002 1003 1004
		nbp_vlan_delete(p, pvid);
	}

	if (test_bit(0, changed)) {
		if (old_pvid)
			br_vlan_add(br, old_pvid,
				    BRIDGE_VLAN_INFO_PVID |
1005
				    BRIDGE_VLAN_INFO_UNTAGGED |
1006
				    BRIDGE_VLAN_INFO_BRENTRY,
1007
				    &vlchange, NULL);
1008 1009 1010 1011 1012
		br_vlan_delete(br, pvid);
	}
	goto out;
}

1013 1014 1015 1016 1017
int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val)
{
	u16 pvid = val;
	int err = 0;

1018
	if (val >= VLAN_VID_MASK)
1019 1020 1021
		return -EINVAL;

	if (pvid == br->default_pvid)
1022
		goto out;
1023 1024

	/* Only allow default pvid change when filtering is disabled */
1025
	if (br_opt_get(br, BROPT_VLAN_ENABLED)) {
1026 1027
		pr_info_once("Please disable vlan filtering to change default_pvid\n");
		err = -EPERM;
1028
		goto out;
1029
	}
1030
	err = __br_vlan_set_default_pvid(br, pvid, NULL);
1031
out:
1032 1033 1034
	return err;
}

1035
int br_vlan_init(struct net_bridge *br)
1036
{
1037
	struct net_bridge_vlan_group *vg;
1038
	int ret = -ENOMEM;
1039
	bool changed;
1040

1041 1042
	vg = kzalloc(sizeof(*vg), GFP_KERNEL);
	if (!vg)
1043
		goto out;
1044
	ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
1045 1046
	if (ret)
		goto err_rhtbl;
1047 1048 1049
	ret = vlan_tunnel_init(vg);
	if (ret)
		goto err_tunnel_init;
1050
	INIT_LIST_HEAD(&vg->vlan_list);
1051
	br->vlan_proto = htons(ETH_P_8021Q);
1052
	br->default_pvid = 1;
1053
	rcu_assign_pointer(br->vlgrp, vg);
1054 1055
	ret = br_vlan_add(br, 1,
			  BRIDGE_VLAN_INFO_PVID | BRIDGE_VLAN_INFO_UNTAGGED |
1056
			  BRIDGE_VLAN_INFO_BRENTRY, &changed, NULL);
1057 1058 1059 1060 1061 1062 1063
	if (ret)
		goto err_vlan_add;

out:
	return ret;

err_vlan_add:
1064 1065
	vlan_tunnel_deinit(vg);
err_tunnel_init:
1066
	rhashtable_destroy(&vg->vlan_hash);
1067
err_rhtbl:
1068
	kfree(vg);
1069 1070 1071 1072

	goto out;
}

1073
int nbp_vlan_init(struct net_bridge_port *p, struct netlink_ext_ack *extack)
1074
{
1075 1076 1077 1078
	struct switchdev_attr attr = {
		.orig_dev = p->br->dev,
		.id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
		.flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
1079
		.u.vlan_filtering = br_opt_get(p->br, BROPT_VLAN_ENABLED),
1080
	};
1081
	struct net_bridge_vlan_group *vg;
1082 1083
	int ret = -ENOMEM;

1084 1085
	vg = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL);
	if (!vg)
1086 1087
		goto out;

1088 1089 1090 1091
	ret = switchdev_port_attr_set(p->dev, &attr);
	if (ret && ret != -EOPNOTSUPP)
		goto err_vlan_enabled;

1092
	ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
1093 1094
	if (ret)
		goto err_rhtbl;
1095 1096 1097
	ret = vlan_tunnel_init(vg);
	if (ret)
		goto err_tunnel_init;
1098
	INIT_LIST_HEAD(&vg->vlan_list);
1099
	rcu_assign_pointer(p->vlgrp, vg);
1100
	if (p->br->default_pvid) {
1101 1102
		bool changed;

1103 1104
		ret = nbp_vlan_add(p, p->br->default_pvid,
				   BRIDGE_VLAN_INFO_PVID |
1105
				   BRIDGE_VLAN_INFO_UNTAGGED,
1106
				   &changed, extack);
1107 1108 1109 1110 1111 1112 1113
		if (ret)
			goto err_vlan_add;
	}
out:
	return ret;

err_vlan_add:
1114 1115
	RCU_INIT_POINTER(p->vlgrp, NULL);
	synchronize_rcu();
1116 1117 1118
	vlan_tunnel_deinit(vg);
err_tunnel_init:
	rhashtable_destroy(&vg->vlan_hash);
1119
err_rhtbl:
1120
err_vlan_enabled:
1121
	kfree(vg);
1122 1123

	goto out;
1124 1125
}

1126 1127
/* Must be protected by RTNL.
 * Must be called with vid in range from 1 to 4094 inclusive.
1128
 * changed must be true only if the vlan was created or updated
1129
 */
1130
int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags,
1131
		 bool *changed, struct netlink_ext_ack *extack)
1132
{
1133 1134
	struct net_bridge_vlan *vlan;
	int ret;
1135 1136 1137

	ASSERT_RTNL();

1138
	*changed = false;
1139
	vlan = br_vlan_find(nbp_vlan_group(port), vid);
1140
	if (vlan) {
1141
		/* Pass the flags to the hardware bridge */
1142
		ret = br_switchdev_port_vlan_add(port->dev, vid, flags, extack);
1143 1144
		if (ret && ret != -EOPNOTSUPP)
			return ret;
1145 1146
		*changed = __vlan_add_flags(vlan, flags);

1147
		return 0;
1148 1149
	}

1150 1151 1152
	vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
	if (!vlan)
		return -ENOMEM;
1153

1154 1155
	vlan->vid = vid;
	vlan->port = port;
1156
	ret = __vlan_add(vlan, flags, extack);
1157 1158
	if (ret)
		kfree(vlan);
1159 1160
	else
		*changed = true;
1161

1162
	return ret;
1163 1164
}

1165 1166 1167
/* Must be protected by RTNL.
 * Must be called with vid in range from 1 to 4094 inclusive.
 */
1168 1169
int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
{
1170
	struct net_bridge_vlan *v;
1171 1172 1173

	ASSERT_RTNL();

1174
	v = br_vlan_find(nbp_vlan_group(port), vid);
1175 1176
	if (!v)
		return -ENOENT;
1177
	br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
1178
	br_fdb_delete_by_port(port->br, port, vid, 0);
1179

1180
	return __vlan_del(v);
1181 1182 1183 1184
}

void nbp_vlan_flush(struct net_bridge_port *port)
{
1185 1186
	struct net_bridge_vlan_group *vg;

1187 1188
	ASSERT_RTNL();

1189 1190 1191 1192 1193
	vg = nbp_vlan_group(port);
	__vlan_flush(vg);
	RCU_INIT_POINTER(port->vlgrp, NULL);
	synchronize_rcu();
	__vlan_group_free(vg);
1194
}
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221

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;
	}
}
1222 1223 1224 1225

int br_vlan_get_pvid(const struct net_device *dev, u16 *p_pvid)
{
	struct net_bridge_vlan_group *vg;
1226
	struct net_bridge_port *p;
1227 1228

	ASSERT_RTNL();
1229 1230 1231 1232
	p = br_port_get_check_rtnl(dev);
	if (p)
		vg = nbp_vlan_group(p);
	else if (netif_is_bridge_master(dev))
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
		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);
1253 1254
	else if (netif_is_bridge_master(dev))
		vg = br_vlan_group(netdev_priv(dev));
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
	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);
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417

static int br_vlan_is_bind_vlan_dev(const struct net_device *dev)
{
	return is_vlan_dev(dev) &&
		!!(vlan_dev_priv(dev)->flags & VLAN_FLAG_BRIDGE_BINDING);
}

static int br_vlan_is_bind_vlan_dev_fn(struct net_device *dev,
				       __always_unused void *data)
{
	return br_vlan_is_bind_vlan_dev(dev);
}

static bool br_vlan_has_upper_bind_vlan_dev(struct net_device *dev)
{
	int found;

	rcu_read_lock();
	found = netdev_walk_all_upper_dev_rcu(dev, br_vlan_is_bind_vlan_dev_fn,
					      NULL);
	rcu_read_unlock();

	return !!found;
}

struct br_vlan_bind_walk_data {
	u16 vid;
	struct net_device *result;
};

static int br_vlan_match_bind_vlan_dev_fn(struct net_device *dev,
					  void *data_in)
{
	struct br_vlan_bind_walk_data *data = data_in;
	int found = 0;

	if (br_vlan_is_bind_vlan_dev(dev) &&
	    vlan_dev_priv(dev)->vlan_id == data->vid) {
		data->result = dev;
		found = 1;
	}

	return found;
}

static struct net_device *
br_vlan_get_upper_bind_vlan_dev(struct net_device *dev, u16 vid)
{
	struct br_vlan_bind_walk_data data = {
		.vid = vid,
	};

	rcu_read_lock();
	netdev_walk_all_upper_dev_rcu(dev, br_vlan_match_bind_vlan_dev_fn,
				      &data);
	rcu_read_unlock();

	return data.result;
}

static bool br_vlan_is_dev_up(const struct net_device *dev)
{
	return  !!(dev->flags & IFF_UP) && netif_oper_up(dev);
}

static void br_vlan_set_vlan_dev_state(const struct net_bridge *br,
				       struct net_device *vlan_dev)
{
	u16 vid = vlan_dev_priv(vlan_dev)->vlan_id;
	struct net_bridge_vlan_group *vg;
	struct net_bridge_port *p;
	bool has_carrier = false;

	list_for_each_entry(p, &br->port_list, list) {
		vg = nbp_vlan_group(p);
		if (br_vlan_find(vg, vid) && br_vlan_is_dev_up(p->dev)) {
			has_carrier = true;
			break;
		}
	}

	if (has_carrier)
		netif_carrier_on(vlan_dev);
	else
		netif_carrier_off(vlan_dev);
}

static void br_vlan_set_all_vlan_dev_state(struct net_bridge_port *p)
{
	struct net_bridge_vlan_group *vg = nbp_vlan_group(p);
	struct net_bridge_vlan *vlan;
	struct net_device *vlan_dev;

	list_for_each_entry(vlan, &vg->vlan_list, vlist) {
		vlan_dev = br_vlan_get_upper_bind_vlan_dev(p->br->dev,
							   vlan->vid);
		if (vlan_dev) {
			if (br_vlan_is_dev_up(p->dev))
				netif_carrier_on(vlan_dev);
			else
				br_vlan_set_vlan_dev_state(p->br, vlan_dev);
		}
	}
}

static void br_vlan_upper_change(struct net_device *dev,
				 struct net_device *upper_dev,
				 bool linking)
{
	struct net_bridge *br = netdev_priv(dev);

	if (!br_vlan_is_bind_vlan_dev(upper_dev))
		return;

	if (linking) {
		br_vlan_set_vlan_dev_state(br, upper_dev);
		br_opt_toggle(br, BROPT_VLAN_BRIDGE_BINDING, true);
	} else {
		br_opt_toggle(br, BROPT_VLAN_BRIDGE_BINDING,
			      br_vlan_has_upper_bind_vlan_dev(dev));
	}
}

/* Must be protected by RTNL. */
void br_vlan_bridge_event(struct net_device *dev, unsigned long event,
			  void *ptr)
{
	struct netdev_notifier_changeupper_info *info;

	switch (event) {
	case NETDEV_CHANGEUPPER:
		info = ptr;
		br_vlan_upper_change(dev, info->upper_dev, info->linking);
		break;
	}
}

/* Must be protected by RTNL. */
void br_vlan_port_event(struct net_bridge_port *p, unsigned long event)
{
	if (!br_opt_get(p->br, BROPT_VLAN_BRIDGE_BINDING))
		return;

	switch (event) {
	case NETDEV_CHANGE:
	case NETDEV_DOWN:
	case NETDEV_UP:
		br_vlan_set_all_vlan_dev_state(p);
		break;
	}
}