br_vlan.c 32.4 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 void nbp_vlan_set_vlan_dev_state(struct net_bridge_port *p, u16 vid);

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

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

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

	return true;
45 46
}

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

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

	return true;
56 57
}

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

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

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

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

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

83
static int __vlan_vid_add(struct net_device *dev, struct net_bridge *br,
84 85
			  struct net_bridge_vlan *v, u16 flags,
			  struct netlink_ext_ack *extack)
86 87 88
{
	int err;

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

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

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

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

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

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

131 132
	/* Try switchdev op first. In case it is not supported, fallback to
	 * 8021q del.
133
	 */
134 135 136 137
	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;
138 139
}

140 141 142
/* 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.
 */
143 144 145
static struct net_bridge_vlan *
br_vlan_get_master(struct net_bridge *br, u16 vid,
		   struct netlink_ext_ack *extack)
146
{
147
	struct net_bridge_vlan_group *vg;
148 149
	struct net_bridge_vlan *masterv;

150 151
	vg = br_vlan_group(br);
	masterv = br_vlan_find(vg, vid);
152
	if (!masterv) {
153 154
		bool changed;

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

	return masterv;
}

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

180 181
static void br_vlan_put_master(struct net_bridge_vlan *masterv)
{
182 183
	struct net_bridge_vlan_group *vg;

184 185 186
	if (!br_vlan_is_master(masterv))
		return;

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

196 197 198 199 200 201 202
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 */
203
	if (v->priv_flags & BR_VLFLAG_PER_PORT_STATS)
204 205 206 207 208
		free_percpu(v->stats);
	v->stats = NULL;
	kfree(v);
}

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

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

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

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

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

261
		masterv = br_vlan_get_master(br, v->vid, extack);
262 263
		if (!masterv)
			goto out_filt;
264
		v->brvlan = masterv;
265 266 267 268 269 270
		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;
			}
271
			v->priv_flags |= BR_VLFLAG_PER_PORT_STATS;
272 273 274
		} else {
			v->stats = masterv->stats;
		}
275
	} else {
276
		err = br_switchdev_port_vlan_add(dev, v->vid, flags, extack);
277 278
		if (err && err != -EOPNOTSUPP)
			goto out;
279 280
	}

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

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

296 297
	__vlan_add_list(v);
	__vlan_add_flags(v, flags);
298 299 300

	if (p)
		nbp_vlan_set_vlan_dev_state(p, v->vid);
301 302 303 304
out:
	return err;

out_fdb_insert:
305 306 307 308
	if (br_vlan_should_use(v)) {
		br_fdb_find_delete_local(br, p, dev->dev_addr, v->vid);
		vg->num_vlans--;
	}
309 310 311

out_filt:
	if (p) {
312
		__vlan_vid_del(dev, br, v);
313
		if (masterv) {
314 315 316 317
			if (v->stats && masterv->stats != v->stats)
				free_percpu(v->stats);
			v->stats = NULL;

318
			br_vlan_put_master(masterv);
319 320
			v->brvlan = NULL;
		}
321 322
	} else {
		br_switchdev_port_vlan_del(dev, v->vid);
323 324 325 326 327 328 329 330
	}

	goto out;
}

static int __vlan_del(struct net_bridge_vlan *v)
{
	struct net_bridge_vlan *masterv = v;
331
	struct net_bridge_vlan_group *vg;
332 333
	struct net_bridge_port *p = NULL;
	int err = 0;
334

335
	if (br_vlan_is_master(v)) {
336
		vg = br_vlan_group(v->br);
337 338
	} else {
		p = v->port;
339
		vg = nbp_vlan_group(v->port);
340 341
		masterv = v->brvlan;
	}
342

343
	__vlan_delete_pvid(vg, v->vid);
344
	if (p) {
345
		err = __vlan_vid_del(p->dev, p->br, v);
346
		if (err)
347
			goto out;
348 349 350 351 352
	} else {
		err = br_switchdev_port_vlan_del(v->br->dev, v->vid);
		if (err && err != -EOPNOTSUPP)
			goto out;
		err = 0;
353
	}
354

355 356 357
	if (br_vlan_should_use(v)) {
		v->flags &= ~BRIDGE_VLAN_INFO_BRENTRY;
		vg->num_vlans--;
358 359 360
	}

	if (masterv != v) {
361
		vlan_tunnel_info_del(vg, v);
362 363
		rhashtable_remove_fast(&vg->vlan_hash, &v->vnode,
				       br_vlan_rht_params);
364
		__vlan_del_list(v);
365
		nbp_vlan_set_vlan_dev_state(p, v->vid);
366
		call_rcu(&v->rcu, nbp_vlan_rcu_free);
367
	}
368

369
	br_vlan_put_master(masterv);
370 371
out:
	return err;
372 373
}

374 375 376 377
static void __vlan_group_free(struct net_bridge_vlan_group *vg)
{
	WARN_ON(!list_empty(&vg->vlan_list));
	rhashtable_destroy(&vg->vlan_hash);
378
	vlan_tunnel_deinit(vg);
379 380 381 382
	kfree(vg);
}

static void __vlan_flush(struct net_bridge_vlan_group *vg)
383
{
384 385
	struct net_bridge_vlan *vlan, *tmp;

386 387
	__vlan_delete_pvid(vg, vg->pvid);
	list_for_each_entry_safe(vlan, tmp, &vg->vlan_list, vlist)
388
		__vlan_del(vlan);
389 390
}

391
struct sk_buff *br_handle_vlan(struct net_bridge *br,
392
			       const struct net_bridge_port *p,
393
			       struct net_bridge_vlan_group *vg,
394
			       struct sk_buff *skb)
395
{
396
	struct br_vlan_stats *stats;
397
	struct net_bridge_vlan *v;
398 399
	u16 vid;

400 401
	/* If this packet was not filtered at input, let it pass */
	if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
402 403
		goto out;

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

431
	if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
432
		__vlan_hwaccel_clear_tag(skb);
433 434 435 436 437 438

	if (p && (p->flags & BR_VLAN_TUNNEL) &&
	    br_handle_egress_vlan_tunnel(skb, v)) {
		kfree_skb(skb);
		return NULL;
	}
439 440 441 442 443
out:
	return skb;
}

/* Called under RCU */
444 445
static bool __allowed_ingress(const struct net_bridge *br,
			      struct net_bridge_vlan_group *vg,
446
			      struct sk_buff *skb, u16 *vid)
447
{
448 449
	struct br_vlan_stats *stats;
	struct net_bridge_vlan *v;
450
	bool tagged;
451

452
	BR_INPUT_SKB_CB(skb)->vlan_filtered = true;
453 454 455 456
	/* 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.
	 */
457
	if (unlikely(!skb_vlan_tag_present(skb) &&
458
		     skb->protocol == br->vlan_proto)) {
459
		skb = skb_vlan_untag(skb);
460 461 462 463
		if (unlikely(!skb))
			return false;
	}

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

486
	if (!*vid) {
487 488
		u16 pvid = br_get_pvid(vg);

489 490 491
		/* 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.
492
		 */
V
Vlad Yasevich 已提交
493
		if (!pvid)
494
			goto drop;
495

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

511
		/* if stats are disabled we can avoid the lookup */
512
		if (!br_opt_get(br, BROPT_VLAN_STATS_ENABLED))
513
			return true;
514
	}
515
	v = br_vlan_find(vg, *vid);
516 517 518
	if (!v || !br_vlan_should_use(v))
		goto drop;

519
	if (br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) {
520 521 522 523 524 525 526 527 528
		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;

529 530
drop:
	kfree_skb(skb);
531 532 533
	return false;
}

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

546
	return __allowed_ingress(br, vg, skb, vid);
547 548
}

549
/* Called under RCU. */
550
bool br_allowed_egress(struct net_bridge_vlan_group *vg,
551 552
		       const struct sk_buff *skb)
{
553
	const struct net_bridge_vlan *v;
554 555
	u16 vid;

556 557
	/* If this packet was not filtered at input, let it pass */
	if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
558 559 560
		return true;

	br_vlan_get_tag(skb, &vid);
561 562
	v = br_vlan_find(vg, vid);
	if (v && br_vlan_should_use(v))
563 564 565 566 567
		return true;

	return false;
}

568 569 570
/* Called under RCU */
bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid)
{
571
	struct net_bridge_vlan_group *vg;
572 573
	struct net_bridge *br = p->br;

574
	/* If filtering was disabled at input, let it pass. */
575
	if (!br_opt_get(br, BROPT_VLAN_ENABLED))
576 577
		return true;

578
	vg = nbp_vlan_group_rcu(p);
579
	if (!vg || !vg->num_vlans)
580 581
		return false;

582 583 584
	if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto)
		*vid = 0;

585
	if (!*vid) {
586
		*vid = br_get_pvid(vg);
V
Vlad Yasevich 已提交
587
		if (!*vid)
588 589 590 591 592
			return false;

		return true;
	}

593
	if (br_vlan_find(vg, *vid))
594 595 596 597 598
		return true;

	return false;
}

599 600 601
static int br_vlan_add_existing(struct net_bridge *br,
				struct net_bridge_vlan_group *vg,
				struct net_bridge_vlan *vlan,
602 603
				u16 flags, bool *changed,
				struct netlink_ext_ack *extack)
604 605 606
{
	int err;

607
	err = br_switchdev_port_vlan_add(br->dev, vlan->vid, flags, extack);
608 609 610
	if (err && err != -EOPNOTSUPP)
		return err;

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

		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;
635 636 637 638 639

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

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

	ASSERT_RTNL();

655
	*changed = false;
656 657
	vg = br_vlan_group(br);
	vlan = br_vlan_find(vg, vid);
658
	if (vlan)
659 660
		return br_vlan_add_existing(br, vg, vlan, flags, changed,
					    extack);
661

662 663
	vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
	if (!vlan)
664 665
		return -ENOMEM;

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

685
	return ret;
686 687
}

688 689 690
/* Must be protected by RTNL.
 * Must be called with vid in range from 1 to 4094 inclusive.
 */
691 692
int br_vlan_delete(struct net_bridge *br, u16 vid)
{
693
	struct net_bridge_vlan_group *vg;
694
	struct net_bridge_vlan *v;
695 696 697

	ASSERT_RTNL();

698 699
	vg = br_vlan_group(br);
	v = br_vlan_find(vg, vid);
700 701
	if (!v || !br_vlan_is_brentry(v))
		return -ENOENT;
702

703
	br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
704
	br_fdb_delete_by_port(br, NULL, vid, 0);
705

706 707
	vlan_tunnel_info_del(vg, v);

708
	return __vlan_del(v);
709 710 711 712
}

void br_vlan_flush(struct net_bridge *br)
{
713 714
	struct net_bridge_vlan_group *vg;

715 716
	ASSERT_RTNL();

717 718 719 720 721
	vg = br_vlan_group(br);
	__vlan_flush(vg);
	RCU_INIT_POINTER(br->vlgrp, NULL);
	synchronize_rcu();
	__vlan_group_free(vg);
722 723
}

724
struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid)
725
{
726 727
	if (!vg)
		return NULL;
728

729
	return br_vlan_lookup(&vg->vlan_hash, vid);
730 731
}

732 733 734
/* Must be protected by RTNL. */
static void recalculate_group_addr(struct net_bridge *br)
{
735
	if (br_opt_get(br, BROPT_GROUP_ADDR_SET))
736 737 738
		return;

	spin_lock_bh(&br->lock);
739 740
	if (!br_opt_get(br, BROPT_VLAN_ENABLED) ||
	    br->vlan_proto == htons(ETH_P_8021Q)) {
741 742 743 744 745 746 747 748 749 750 751 752
		/* 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)
{
753 754
	if (!br_opt_get(br, BROPT_VLAN_ENABLED) ||
	    br->vlan_proto == htons(ETH_P_8021Q))
755 756 757 758 759 760
		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]);
}

761
int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
762
{
763 764 765 766 767 768 769 770
	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;

771
	if (br_opt_get(br, BROPT_VLAN_ENABLED) == !!val)
772
		return 0;
773

774 775 776 777
	err = switchdev_port_attr_set(br->dev, &attr);
	if (err && err != -EOPNOTSUPP)
		return err;

778
	br_opt_toggle(br, BROPT_VLAN_ENABLED, !!val);
779
	br_manage_promisc(br);
780 781
	recalculate_group_addr(br);
	br_recalculate_fwd_mask(br);
782

783 784 785 786 787
	return 0;
}

int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
{
788
	return __br_vlan_filter_toggle(br, val);
789 790
}

791 792 793 794
bool br_vlan_enabled(const struct net_device *dev)
{
	struct net_bridge *br = netdev_priv(dev);

795
	return br_opt_get(br, BROPT_VLAN_ENABLED);
796 797 798
}
EXPORT_SYMBOL_GPL(br_vlan_enabled);

799
int __br_vlan_set_proto(struct net_bridge *br, __be16 proto)
800 801 802
{
	int err = 0;
	struct net_bridge_port *p;
803
	struct net_bridge_vlan *vlan;
804
	struct net_bridge_vlan_group *vg;
805
	__be16 oldproto;
806 807

	if (br->vlan_proto == proto)
808
		return 0;
809 810 811

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

833
	return 0;
834 835

err_filt:
836
	list_for_each_entry_continue_reverse(vlan, &vg->vlan_list, vlist)
837
		vlan_vid_del(p->dev, proto, vlan->vid);
838

839 840 841
	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)
842
			vlan_vid_del(p->dev, proto, vlan->vid);
843
	}
844

845 846 847 848 849 850 851 852
	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;

853
	return __br_vlan_set_proto(br, htons(val));
854 855
}

856 857 858 859 860
int br_vlan_set_stats(struct net_bridge *br, unsigned long val)
{
	switch (val) {
	case 0:
	case 1:
861
		br_opt_toggle(br, BROPT_VLAN_STATS_ENABLED, !!val);
862 863
		break;
	default:
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
		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:
888 889 890 891 892 893
		return -EINVAL;
	}

	return 0;
}

894
static bool vlan_default_pvid(struct net_bridge_vlan_group *vg, u16 vid)
895
{
896 897
	struct net_bridge_vlan *v;

898
	if (vid != vg->pvid)
899 900 901 902 903 904 905 906
		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;
907 908 909 910 911 912 913 914 915 916
}

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.
	 */
917
	if (vlan_default_pvid(br_vlan_group(br), pvid))
918 919 920
		br_vlan_delete(br, pvid);

	list_for_each_entry(p, &br->port_list, list) {
921
		if (vlan_default_pvid(nbp_vlan_group(p), pvid))
922 923 924 925 926 927
			nbp_vlan_delete(p, pvid);
	}

	br->default_pvid = 0;
}

928 929
int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid,
			       struct netlink_ext_ack *extack)
930
{
931
	const struct net_bridge_vlan *pvent;
932
	struct net_bridge_vlan_group *vg;
933
	struct net_bridge_port *p;
934 935
	unsigned long *changed;
	bool vlchange;
936 937 938
	u16 old_pvid;
	int err = 0;

939 940 941 942 943
	if (!pvid) {
		br_vlan_disable_default_pvid(br);
		return 0;
	}

944
	changed = bitmap_zalloc(BR_MAX_PORTS, GFP_KERNEL);
945 946 947 948 949 950 951 952
	if (!changed)
		return -ENOMEM;

	old_pvid = br->default_pvid;

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

		err = nbp_vlan_add(p, pvid,
				   BRIDGE_VLAN_INFO_PVID |
980
				   BRIDGE_VLAN_INFO_UNTAGGED,
981
				   &vlchange, extack);
982 983 984 985 986 987 988 989 990
		if (err)
			goto err_port;
		nbp_vlan_delete(p, old_pvid);
		set_bit(p->port_no, changed);
	}

	br->default_pvid = pvid;

out:
991
	bitmap_free(changed);
992 993 994 995 996 997 998 999 1000 1001
	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 |
1002
				     BRIDGE_VLAN_INFO_UNTAGGED,
1003
				     &vlchange, NULL);
1004 1005 1006 1007 1008 1009 1010
		nbp_vlan_delete(p, pvid);
	}

	if (test_bit(0, changed)) {
		if (old_pvid)
			br_vlan_add(br, old_pvid,
				    BRIDGE_VLAN_INFO_PVID |
1011
				    BRIDGE_VLAN_INFO_UNTAGGED |
1012
				    BRIDGE_VLAN_INFO_BRENTRY,
1013
				    &vlchange, NULL);
1014 1015 1016 1017 1018
		br_vlan_delete(br, pvid);
	}
	goto out;
}

1019 1020 1021 1022 1023
int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val)
{
	u16 pvid = val;
	int err = 0;

1024
	if (val >= VLAN_VID_MASK)
1025 1026 1027
		return -EINVAL;

	if (pvid == br->default_pvid)
1028
		goto out;
1029 1030

	/* Only allow default pvid change when filtering is disabled */
1031
	if (br_opt_get(br, BROPT_VLAN_ENABLED)) {
1032 1033
		pr_info_once("Please disable vlan filtering to change default_pvid\n");
		err = -EPERM;
1034
		goto out;
1035
	}
1036
	err = __br_vlan_set_default_pvid(br, pvid, NULL);
1037
out:
1038 1039 1040
	return err;
}

1041
int br_vlan_init(struct net_bridge *br)
1042
{
1043
	struct net_bridge_vlan_group *vg;
1044
	int ret = -ENOMEM;
1045
	bool changed;
1046

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

out:
	return ret;

err_vlan_add:
1070 1071
	vlan_tunnel_deinit(vg);
err_tunnel_init:
1072
	rhashtable_destroy(&vg->vlan_hash);
1073
err_rhtbl:
1074
	kfree(vg);
1075 1076 1077 1078

	goto out;
}

1079
int nbp_vlan_init(struct net_bridge_port *p, struct netlink_ext_ack *extack)
1080
{
1081 1082 1083 1084
	struct switchdev_attr attr = {
		.orig_dev = p->br->dev,
		.id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
		.flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
1085
		.u.vlan_filtering = br_opt_get(p->br, BROPT_VLAN_ENABLED),
1086
	};
1087
	struct net_bridge_vlan_group *vg;
1088 1089
	int ret = -ENOMEM;

1090 1091
	vg = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL);
	if (!vg)
1092 1093
		goto out;

1094 1095 1096 1097
	ret = switchdev_port_attr_set(p->dev, &attr);
	if (ret && ret != -EOPNOTSUPP)
		goto err_vlan_enabled;

1098
	ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
1099 1100
	if (ret)
		goto err_rhtbl;
1101 1102 1103
	ret = vlan_tunnel_init(vg);
	if (ret)
		goto err_tunnel_init;
1104
	INIT_LIST_HEAD(&vg->vlan_list);
1105
	rcu_assign_pointer(p->vlgrp, vg);
1106
	if (p->br->default_pvid) {
1107 1108
		bool changed;

1109 1110
		ret = nbp_vlan_add(p, p->br->default_pvid,
				   BRIDGE_VLAN_INFO_PVID |
1111
				   BRIDGE_VLAN_INFO_UNTAGGED,
1112
				   &changed, extack);
1113 1114 1115 1116 1117 1118 1119
		if (ret)
			goto err_vlan_add;
	}
out:
	return ret;

err_vlan_add:
1120 1121
	RCU_INIT_POINTER(p->vlgrp, NULL);
	synchronize_rcu();
1122 1123 1124
	vlan_tunnel_deinit(vg);
err_tunnel_init:
	rhashtable_destroy(&vg->vlan_hash);
1125
err_rhtbl:
1126
err_vlan_enabled:
1127
	kfree(vg);
1128 1129

	goto out;
1130 1131
}

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

	ASSERT_RTNL();

1144
	*changed = false;
1145
	vlan = br_vlan_find(nbp_vlan_group(port), vid);
1146
	if (vlan) {
1147
		/* Pass the flags to the hardware bridge */
1148
		ret = br_switchdev_port_vlan_add(port->dev, vid, flags, extack);
1149 1150
		if (ret && ret != -EOPNOTSUPP)
			return ret;
1151 1152
		*changed = __vlan_add_flags(vlan, flags);

1153
		return 0;
1154 1155
	}

1156 1157 1158
	vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
	if (!vlan)
		return -ENOMEM;
1159

1160 1161
	vlan->vid = vid;
	vlan->port = port;
1162
	ret = __vlan_add(vlan, flags, extack);
1163 1164
	if (ret)
		kfree(vlan);
1165 1166
	else
		*changed = true;
1167

1168
	return ret;
1169 1170
}

1171 1172 1173
/* Must be protected by RTNL.
 * Must be called with vid in range from 1 to 4094 inclusive.
 */
1174 1175
int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
{
1176
	struct net_bridge_vlan *v;
1177 1178 1179

	ASSERT_RTNL();

1180
	v = br_vlan_find(nbp_vlan_group(port), vid);
1181 1182
	if (!v)
		return -ENOENT;
1183
	br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
1184
	br_fdb_delete_by_port(port->br, port, vid, 0);
1185

1186
	return __vlan_del(v);
1187 1188 1189 1190
}

void nbp_vlan_flush(struct net_bridge_port *port)
{
1191 1192
	struct net_bridge_vlan_group *vg;

1193 1194
	ASSERT_RTNL();

1195 1196 1197 1198 1199
	vg = nbp_vlan_group(port);
	__vlan_flush(vg);
	RCU_INIT_POINTER(port->vlgrp, NULL);
	synchronize_rcu();
	__vlan_group_free(vg);
1200
}
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227

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;
	}
}
1228 1229 1230 1231

int br_vlan_get_pvid(const struct net_device *dev, u16 *p_pvid)
{
	struct net_bridge_vlan_group *vg;
1232
	struct net_bridge_port *p;
1233 1234

	ASSERT_RTNL();
1235 1236 1237 1238
	p = br_port_get_check_rtnl(dev);
	if (p)
		vg = nbp_vlan_group(p);
	else if (netif_is_bridge_master(dev))
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
		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);
1259 1260
	else if (netif_is_bridge_master(dev))
		vg = br_vlan_group(netdev_priv(dev));
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
	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);
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

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

1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
/* Must be protected by RTNL. */
static void nbp_vlan_set_vlan_dev_state(struct net_bridge_port *p, u16 vid)
{
	struct net_device *vlan_dev;

	if (!br_opt_get(p->br, BROPT_VLAN_BRIDGE_BINDING))
		return;

	vlan_dev = br_vlan_get_upper_bind_vlan_dev(p->br->dev, vid);
	if (vlan_dev)
		br_vlan_set_vlan_dev_state(p->br, vlan_dev);
}

1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
/* 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;
	}
}