br_vlan.c 43.4 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5
#include <linux/kernel.h>
#include <linux/netdevice.h>
#include <linux/rtnetlink.h>
#include <linux/slab.h>
6
#include <net/switchdev.h>
7 8

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

11 12
static void nbp_vlan_set_vlan_dev_state(struct net_bridge_port *p, u16 vid);

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

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

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

	return true;
46 47
}

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

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

	return true;
57 58
}

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

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

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

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

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

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

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

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

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

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

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

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

132 133
	/* Try switchdev op first. In case it is not supported, fallback to
	 * 8021q del.
134
	 */
135 136 137 138
	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;
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.
 */
144 145 146
static struct net_bridge_vlan *
br_vlan_get_master(struct net_bridge *br, u16 vid,
		   struct netlink_ext_ack *extack)
147
{
148
	struct net_bridge_vlan_group *vg;
149 150
	struct net_bridge_vlan *masterv;

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

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

	return masterv;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	goto out;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return false;
}

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

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

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

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

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

		return true;
	}

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

	return false;
}

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

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

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

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

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

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

	ASSERT_RTNL();

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

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

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

686
	return ret;
687 688
}

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

	ASSERT_RTNL();

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

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

707 708
	vlan_tunnel_info_del(vg, v);

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

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

716 717
	ASSERT_RTNL();

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

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

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

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

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

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

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

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

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

784 785 786 787 788
	return 0;
}

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

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

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

W
wenxu 已提交
800 801 802 803 804 805 806 807 808 809
int br_vlan_get_proto(const struct net_device *dev, u16 *p_proto)
{
	struct net_bridge *br = netdev_priv(dev);

	*p_proto = ntohs(br->vlan_proto);

	return 0;
}
EXPORT_SYMBOL_GPL(br_vlan_get_proto);

810
int __br_vlan_set_proto(struct net_bridge *br, __be16 proto)
811 812 813
{
	int err = 0;
	struct net_bridge_port *p;
814
	struct net_bridge_vlan *vlan;
815
	struct net_bridge_vlan_group *vg;
816
	__be16 oldproto;
817 818

	if (br->vlan_proto == proto)
819
		return 0;
820 821 822

	/* Add VLANs for the new proto to the device filter. */
	list_for_each_entry(p, &br->port_list, list) {
823 824
		vg = nbp_vlan_group(p);
		list_for_each_entry(vlan, &vg->vlan_list, vlist) {
825
			err = vlan_vid_add(p->dev, proto, vlan->vid);
826 827 828 829 830 831 832 833 834 835 836 837
			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. */
838 839 840
	list_for_each_entry(p, &br->port_list, list) {
		vg = nbp_vlan_group(p);
		list_for_each_entry(vlan, &vg->vlan_list, vlist)
841
			vlan_vid_del(p->dev, oldproto, vlan->vid);
842
	}
843

844
	return 0;
845 846

err_filt:
847
	list_for_each_entry_continue_reverse(vlan, &vg->vlan_list, vlist)
848
		vlan_vid_del(p->dev, proto, vlan->vid);
849

850 851 852
	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)
853
			vlan_vid_del(p->dev, proto, vlan->vid);
854
	}
855

856 857 858 859 860 861 862 863
	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;

864
	return __br_vlan_set_proto(br, htons(val));
865 866
}

867 868 869 870 871
int br_vlan_set_stats(struct net_bridge *br, unsigned long val)
{
	switch (val) {
	case 0:
	case 1:
872
		br_opt_toggle(br, BROPT_VLAN_STATS_ENABLED, !!val);
873 874
		break;
	default:
875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898
		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:
899 900 901 902 903 904
		return -EINVAL;
	}

	return 0;
}

905
static bool vlan_default_pvid(struct net_bridge_vlan_group *vg, u16 vid)
906
{
907 908
	struct net_bridge_vlan *v;

909
	if (vid != vg->pvid)
910 911 912 913 914 915 916 917
		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;
918 919 920 921 922 923 924 925 926 927
}

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.
	 */
928
	if (vlan_default_pvid(br_vlan_group(br), pvid))
929 930 931
		br_vlan_delete(br, pvid);

	list_for_each_entry(p, &br->port_list, list) {
932
		if (vlan_default_pvid(nbp_vlan_group(p), pvid))
933 934 935 936 937 938
			nbp_vlan_delete(p, pvid);
	}

	br->default_pvid = 0;
}

939 940
int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid,
			       struct netlink_ext_ack *extack)
941
{
942
	const struct net_bridge_vlan *pvent;
943
	struct net_bridge_vlan_group *vg;
944
	struct net_bridge_port *p;
945 946
	unsigned long *changed;
	bool vlchange;
947 948 949
	u16 old_pvid;
	int err = 0;

950 951 952 953 954
	if (!pvid) {
		br_vlan_disable_default_pvid(br);
		return 0;
	}

955
	changed = bitmap_zalloc(BR_MAX_PORTS, GFP_KERNEL);
956 957 958 959 960 961 962 963
	if (!changed)
		return -ENOMEM;

	old_pvid = br->default_pvid;

	/* Update default_pvid config only if we do not conflict with
	 * user configuration.
	 */
964 965 966
	vg = br_vlan_group(br);
	pvent = br_vlan_find(vg, pvid);
	if ((!old_pvid || vlan_default_pvid(vg, old_pvid)) &&
967
	    (!pvent || !br_vlan_should_use(pvent))) {
968 969
		err = br_vlan_add(br, pvid,
				  BRIDGE_VLAN_INFO_PVID |
970
				  BRIDGE_VLAN_INFO_UNTAGGED |
971
				  BRIDGE_VLAN_INFO_BRENTRY,
972
				  &vlchange, extack);
973 974 975 976 977 978 979 980 981 982
		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.
		 */
983
		vg = nbp_vlan_group(p);
984
		if ((old_pvid &&
985 986
		     !vlan_default_pvid(vg, old_pvid)) ||
		    br_vlan_find(vg, pvid))
987 988 989 990
			continue;

		err = nbp_vlan_add(p, pvid,
				   BRIDGE_VLAN_INFO_PVID |
991
				   BRIDGE_VLAN_INFO_UNTAGGED,
992
				   &vlchange, extack);
993 994 995 996 997 998 999 1000 1001
		if (err)
			goto err_port;
		nbp_vlan_delete(p, old_pvid);
		set_bit(p->port_no, changed);
	}

	br->default_pvid = pvid;

out:
1002
	bitmap_free(changed);
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
	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 |
1013
				     BRIDGE_VLAN_INFO_UNTAGGED,
1014
				     &vlchange, NULL);
1015 1016 1017 1018 1019 1020 1021
		nbp_vlan_delete(p, pvid);
	}

	if (test_bit(0, changed)) {
		if (old_pvid)
			br_vlan_add(br, old_pvid,
				    BRIDGE_VLAN_INFO_PVID |
1022
				    BRIDGE_VLAN_INFO_UNTAGGED |
1023
				    BRIDGE_VLAN_INFO_BRENTRY,
1024
				    &vlchange, NULL);
1025 1026 1027 1028 1029
		br_vlan_delete(br, pvid);
	}
	goto out;
}

1030 1031 1032 1033 1034
int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val)
{
	u16 pvid = val;
	int err = 0;

1035
	if (val >= VLAN_VID_MASK)
1036 1037 1038
		return -EINVAL;

	if (pvid == br->default_pvid)
1039
		goto out;
1040 1041

	/* Only allow default pvid change when filtering is disabled */
1042
	if (br_opt_get(br, BROPT_VLAN_ENABLED)) {
1043 1044
		pr_info_once("Please disable vlan filtering to change default_pvid\n");
		err = -EPERM;
1045
		goto out;
1046
	}
1047
	err = __br_vlan_set_default_pvid(br, pvid, NULL);
1048
out:
1049 1050 1051
	return err;
}

1052
int br_vlan_init(struct net_bridge *br)
1053
{
1054
	struct net_bridge_vlan_group *vg;
1055 1056
	int ret = -ENOMEM;

1057 1058
	vg = kzalloc(sizeof(*vg), GFP_KERNEL);
	if (!vg)
1059
		goto out;
1060
	ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
1061 1062
	if (ret)
		goto err_rhtbl;
1063 1064 1065
	ret = vlan_tunnel_init(vg);
	if (ret)
		goto err_tunnel_init;
1066
	INIT_LIST_HEAD(&vg->vlan_list);
1067
	br->vlan_proto = htons(ETH_P_8021Q);
1068
	br->default_pvid = 1;
1069
	rcu_assign_pointer(br->vlgrp, vg);
1070 1071 1072 1073

out:
	return ret;

1074
err_tunnel_init:
1075
	rhashtable_destroy(&vg->vlan_hash);
1076
err_rhtbl:
1077
	kfree(vg);
1078 1079 1080 1081

	goto out;
}

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

1093 1094
	vg = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL);
	if (!vg)
1095 1096
		goto out;

1097 1098 1099 1100
	ret = switchdev_port_attr_set(p->dev, &attr);
	if (ret && ret != -EOPNOTSUPP)
		goto err_vlan_enabled;

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

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

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

	goto out;
1133 1134
}

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

	ASSERT_RTNL();

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

1156
		return 0;
1157 1158
	}

1159 1160 1161
	vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
	if (!vlan)
		return -ENOMEM;
1162

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

1171
	return ret;
1172 1173
}

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

	ASSERT_RTNL();

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

1189
	return __vlan_del(v);
1190 1191 1192 1193
}

void nbp_vlan_flush(struct net_bridge_port *port)
{
1194 1195
	struct net_bridge_vlan_group *vg;

1196 1197
	ASSERT_RTNL();

1198 1199 1200 1201 1202
	vg = nbp_vlan_group(port);
	__vlan_flush(vg);
	RCU_INIT_POINTER(port->vlgrp, NULL);
	synchronize_rcu();
	__vlan_group_free(vg);
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 1228 1229 1230

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

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

1237 1238 1239
	if (p)
		vg = nbp_vlan_group(p);
	else if (netif_is_bridge_master(dev))
1240 1241 1242 1243 1244 1245 1246
		vg = br_vlan_group(netdev_priv(dev));
	else
		return -EINVAL;

	*p_pvid = br_get_pvid(vg);
	return 0;
}
1247 1248 1249 1250 1251 1252 1253

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

	return __br_vlan_get_pvid(dev, br_port_get_check_rtnl(dev), p_pvid);
}
1254 1255
EXPORT_SYMBOL_GPL(br_vlan_get_pvid);

1256 1257 1258 1259 1260 1261
int br_vlan_get_pvid_rcu(const struct net_device *dev, u16 *p_pvid)
{
	return __br_vlan_get_pvid(dev, br_port_get_check_rcu(dev), p_pvid);
}
EXPORT_SYMBOL_GPL(br_vlan_get_pvid_rcu);

1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
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);
1273 1274
	else if (netif_is_bridge_master(dev))
		vg = br_vlan_group(netdev_priv(dev));
1275 1276 1277 1278 1279 1280 1281 1282 1283
	else
		return -EINVAL;

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

	p_vinfo->vid = vid;
	p_vinfo->flags = v->flags;
1284 1285
	if (vid == br_get_pvid(vg))
		p_vinfo->flags |= BRIDGE_VLAN_INFO_PVID;
1286 1287 1288
	return 0;
}
EXPORT_SYMBOL_GPL(br_vlan_get_info);
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

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;

1362 1363 1364 1365 1366
	if (!netif_carrier_ok(br->dev)) {
		netif_carrier_off(vlan_dev);
		return;
	}

1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390
	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) {
1391 1392 1393 1394
			if (br_vlan_is_dev_up(p->dev)) {
				if (netif_carrier_ok(p->br->dev))
					netif_carrier_on(vlan_dev);
			} else {
1395
				br_vlan_set_vlan_dev_state(p->br, vlan_dev);
1396
			}
1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
		}
	}
}

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

1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446
struct br_vlan_link_state_walk_data {
	struct net_bridge *br;
};

static int br_vlan_link_state_change_fn(struct net_device *vlan_dev,
					void *data_in)
{
	struct br_vlan_link_state_walk_data *data = data_in;

	if (br_vlan_is_bind_vlan_dev(vlan_dev))
		br_vlan_set_vlan_dev_state(data->br, vlan_dev);

	return 0;
}

static void br_vlan_link_state_change(struct net_device *dev,
				      struct net_bridge *br)
{
	struct br_vlan_link_state_walk_data data = {
		.br = br
	};

	rcu_read_lock();
	netdev_walk_all_upper_dev_rcu(dev, br_vlan_link_state_change_fn,
				      &data);
	rcu_read_unlock();
}

1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459
/* 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);
}

1460
/* Must be protected by RTNL. */
1461
int br_vlan_bridge_event(struct net_device *dev, unsigned long event, void *ptr)
1462 1463
{
	struct netdev_notifier_changeupper_info *info;
1464 1465 1466
	struct net_bridge *br = netdev_priv(dev);
	bool changed;
	int ret = 0;
1467 1468

	switch (event) {
1469 1470 1471 1472 1473 1474 1475 1476 1477
	case NETDEV_REGISTER:
		ret = br_vlan_add(br, br->default_pvid,
				  BRIDGE_VLAN_INFO_PVID |
				  BRIDGE_VLAN_INFO_UNTAGGED |
				  BRIDGE_VLAN_INFO_BRENTRY, &changed, NULL);
		break;
	case NETDEV_UNREGISTER:
		br_vlan_delete(br, br->default_pvid);
		break;
1478 1479 1480 1481
	case NETDEV_CHANGEUPPER:
		info = ptr;
		br_vlan_upper_change(dev, info->upper_dev, info->linking);
		break;
1482 1483 1484 1485

	case NETDEV_CHANGE:
	case NETDEV_UP:
		if (!br_opt_get(br, BROPT_VLAN_BRIDGE_BINDING))
1486
			break;
1487 1488
		br_vlan_link_state_change(dev, br);
		break;
1489
	}
1490 1491

	return ret;
1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507
}

/* 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;
	}
}
1508

1509 1510
static bool br_vlan_fill_vids(struct sk_buff *skb, u16 vid, u16 vid_range,
			      u16 flags)
1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528
{
	struct bridge_vlan_info info;
	struct nlattr *nest;

	nest = nla_nest_start(skb, BRIDGE_VLANDB_ENTRY);
	if (!nest)
		return false;

	memset(&info, 0, sizeof(info));
	info.vid = vid;
	if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
		info.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
	if (flags & BRIDGE_VLAN_INFO_PVID)
		info.flags |= BRIDGE_VLAN_INFO_PVID;

	if (nla_put(skb, BRIDGE_VLANDB_ENTRY_INFO, sizeof(info), &info))
		goto out_err;

1529 1530 1531 1532 1533
	if (vid_range && vid < vid_range &&
	    !(flags & BRIDGE_VLAN_INFO_PVID) &&
	    nla_put_u16(skb, BRIDGE_VLANDB_ENTRY_RANGE, vid_range))
		goto out_err;

1534 1535 1536 1537 1538 1539 1540 1541 1542
	nla_nest_end(skb, nest);

	return true;

out_err:
	nla_nest_cancel(skb, nest);
	return false;
}

1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621
static size_t rtnl_vlan_nlmsg_size(void)
{
	return NLMSG_ALIGN(sizeof(struct br_vlan_msg))
		+ nla_total_size(0) /* BRIDGE_VLANDB_ENTRY */
		+ nla_total_size(sizeof(u16)) /* BRIDGE_VLANDB_ENTRY_RANGE */
		+ nla_total_size(sizeof(struct bridge_vlan_info)); /* BRIDGE_VLANDB_ENTRY_INFO */
}

void br_vlan_notify(const struct net_bridge *br,
		    const struct net_bridge_port *p,
		    u16 vid, u16 vid_range,
		    int cmd)
{
	struct net_bridge_vlan_group *vg;
	struct net_bridge_vlan *v;
	struct br_vlan_msg *bvm;
	struct nlmsghdr *nlh;
	struct sk_buff *skb;
	int err = -ENOBUFS;
	struct net *net;
	u16 flags = 0;
	int ifindex;

	/* right now notifications are done only with rtnl held */
	ASSERT_RTNL();

	if (p) {
		ifindex = p->dev->ifindex;
		vg = nbp_vlan_group(p);
		net = dev_net(p->dev);
	} else {
		ifindex = br->dev->ifindex;
		vg = br_vlan_group(br);
		net = dev_net(br->dev);
	}

	skb = nlmsg_new(rtnl_vlan_nlmsg_size(), GFP_KERNEL);
	if (!skb)
		goto out_err;

	err = -EMSGSIZE;
	nlh = nlmsg_put(skb, 0, 0, cmd, sizeof(*bvm), 0);
	if (!nlh)
		goto out_err;
	bvm = nlmsg_data(nlh);
	memset(bvm, 0, sizeof(*bvm));
	bvm->family = AF_BRIDGE;
	bvm->ifindex = ifindex;

	switch (cmd) {
	case RTM_NEWVLAN:
		/* need to find the vlan due to flags/options */
		v = br_vlan_find(vg, vid);
		if (!v || !br_vlan_should_use(v))
			goto out_kfree;

		flags = v->flags;
		if (br_get_pvid(vg) == v->vid)
			flags |= BRIDGE_VLAN_INFO_PVID;
		break;
	case RTM_DELVLAN:
		break;
	default:
		goto out_kfree;
	}

	if (!br_vlan_fill_vids(skb, vid, vid_range, flags))
		goto out_err;

	nlmsg_end(skb, nlh);
	rtnl_notify(skb, net, 0, RTNLGRP_BRVLAN, NULL, GFP_KERNEL);
	return;

out_err:
	rtnl_set_sk_err(net, RTNLGRP_BRVLAN, err);
out_kfree:
	kfree_skb(skb);
}

1622 1623 1624 1625 1626 1627 1628 1629
/* check if v_curr can enter a range ending in range_end */
static bool br_vlan_can_enter_range(const struct net_bridge_vlan *v_curr,
				    const struct net_bridge_vlan *range_end)
{
	return v_curr->vid - range_end->vid == 1 &&
	       range_end->flags == v_curr->flags;
}

1630 1631 1632 1633
static int br_vlan_dump_dev(const struct net_device *dev,
			    struct sk_buff *skb,
			    struct netlink_callback *cb)
{
1634
	struct net_bridge_vlan *v, *range_start = NULL, *range_end = NULL;
1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671
	struct net_bridge_vlan_group *vg;
	int idx = 0, s_idx = cb->args[1];
	struct nlmsghdr *nlh = NULL;
	struct net_bridge_port *p;
	struct br_vlan_msg *bvm;
	struct net_bridge *br;
	int err = 0;
	u16 pvid;

	if (!netif_is_bridge_master(dev) && !netif_is_bridge_port(dev))
		return -EINVAL;

	if (netif_is_bridge_master(dev)) {
		br = netdev_priv(dev);
		vg = br_vlan_group_rcu(br);
		p = NULL;
	} else {
		p = br_port_get_rcu(dev);
		if (WARN_ON(!p))
			return -EINVAL;
		vg = nbp_vlan_group_rcu(p);
		br = p->br;
	}

	if (!vg)
		return 0;

	nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
			RTM_NEWVLAN, sizeof(*bvm), NLM_F_MULTI);
	if (!nlh)
		return -EMSGSIZE;
	bvm = nlmsg_data(nlh);
	memset(bvm, 0, sizeof(*bvm));
	bvm->family = PF_BRIDGE;
	bvm->ifindex = dev->ifindex;
	pvid = br_get_pvid(vg);

1672
	/* idx must stay at range's beginning until it is filled in */
1673 1674 1675
	list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
		if (!br_vlan_should_use(v))
			continue;
1676 1677 1678
		if (idx < s_idx) {
			idx++;
			continue;
1679
		}
1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700

		if (!range_start) {
			range_start = v;
			range_end = v;
			continue;
		}

		if (v->vid == pvid || !br_vlan_can_enter_range(v, range_end)) {
			u16 flags = br_vlan_flags(range_start, pvid);

			if (!br_vlan_fill_vids(skb, range_start->vid,
					       range_end->vid, flags)) {
				err = -EMSGSIZE;
				break;
			}
			/* advance number of filled vlans */
			idx += range_end->vid - range_start->vid + 1;

			range_start = v;
		}
		range_end = v;
1701
	}
1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714

	/* err will be 0 and range_start will be set in 3 cases here:
	 * - first vlan (range_start == range_end)
	 * - last vlan (range_start == range_end, not in range)
	 * - last vlan range (range_start != range_end, in range)
	 */
	if (!err && range_start &&
	    !br_vlan_fill_vids(skb, range_start->vid, range_end->vid,
			       br_vlan_flags(range_start, pvid)))
		err = -EMSGSIZE;

	cb->args[1] = err ? idx : 0;

1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765
	nlmsg_end(skb, nlh);

	return err;
}

static int br_vlan_rtm_dump(struct sk_buff *skb, struct netlink_callback *cb)
{
	int idx = 0, err = 0, s_idx = cb->args[0];
	struct net *net = sock_net(skb->sk);
	struct br_vlan_msg *bvm;
	struct net_device *dev;

	err = nlmsg_parse(cb->nlh, sizeof(*bvm), NULL, 0, NULL, cb->extack);
	if (err < 0)
		return err;

	bvm = nlmsg_data(cb->nlh);

	rcu_read_lock();
	if (bvm->ifindex) {
		dev = dev_get_by_index_rcu(net, bvm->ifindex);
		if (!dev) {
			err = -ENODEV;
			goto out_err;
		}
		err = br_vlan_dump_dev(dev, skb, cb);
		if (err && err != -EMSGSIZE)
			goto out_err;
	} else {
		for_each_netdev_rcu(net, dev) {
			if (idx < s_idx)
				goto skip;

			err = br_vlan_dump_dev(dev, skb, cb);
			if (err == -EMSGSIZE)
				break;
skip:
			idx++;
		}
	}
	cb->args[0] = idx;
	rcu_read_unlock();

	return skb->len;

out_err:
	rcu_read_unlock();

	return err;
}

1766 1767 1768
static const struct nla_policy br_vlan_db_policy[BRIDGE_VLANDB_ENTRY_MAX + 1] = {
	[BRIDGE_VLANDB_ENTRY_INFO]	= { .type = NLA_EXACT_LEN,
					    .len = sizeof(struct bridge_vlan_info) },
1769
	[BRIDGE_VLANDB_ENTRY_RANGE]	= { .type = NLA_U16 },
1770 1771 1772 1773 1774 1775
};

static int br_vlan_rtm_process_one(struct net_device *dev,
				   const struct nlattr *attr,
				   int cmd, struct netlink_ext_ack *extack)
{
1776
	struct bridge_vlan_info *vinfo, vrange_end, *vinfo_last = NULL;
1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806
	struct nlattr *tb[BRIDGE_VLANDB_ENTRY_MAX + 1];
	struct net_bridge_vlan_group *vg;
	struct net_bridge_port *p = NULL;
	int err = 0, cmdmap = 0;
	struct net_bridge *br;
	bool changed = false;

	if (netif_is_bridge_master(dev)) {
		br = netdev_priv(dev);
		vg = br_vlan_group(br);
	} else {
		p = br_port_get_rtnl(dev);
		if (WARN_ON(!p))
			return -ENODEV;
		br = p->br;
		vg = nbp_vlan_group(p);
	}

	if (WARN_ON(!vg))
		return -ENODEV;

	err = nla_parse_nested(tb, BRIDGE_VLANDB_ENTRY_MAX, attr,
			       br_vlan_db_policy, extack);
	if (err)
		return err;

	if (!tb[BRIDGE_VLANDB_ENTRY_INFO]) {
		NL_SET_ERR_MSG_MOD(extack, "Missing vlan entry info");
		return -EINVAL;
	}
1807
	memset(&vrange_end, 0, sizeof(vrange_end));
1808 1809 1810 1811 1812 1813 1814 1815 1816 1817

	vinfo = nla_data(tb[BRIDGE_VLANDB_ENTRY_INFO]);
	if (vinfo->flags & (BRIDGE_VLAN_INFO_RANGE_BEGIN |
			    BRIDGE_VLAN_INFO_RANGE_END)) {
		NL_SET_ERR_MSG_MOD(extack, "Old-style vlan ranges are not allowed when using RTM vlan calls");
		return -EINVAL;
	}
	if (!br_vlan_valid_id(vinfo->vid, extack))
		return -EINVAL;

1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832
	if (tb[BRIDGE_VLANDB_ENTRY_RANGE]) {
		vrange_end.vid = nla_get_u16(tb[BRIDGE_VLANDB_ENTRY_RANGE]);
		/* validate user-provided flags without RANGE_BEGIN */
		vrange_end.flags = BRIDGE_VLAN_INFO_RANGE_END | vinfo->flags;
		vinfo->flags |= BRIDGE_VLAN_INFO_RANGE_BEGIN;

		/* vinfo_last is the range start, vinfo the range end */
		vinfo_last = vinfo;
		vinfo = &vrange_end;

		if (!br_vlan_valid_id(vinfo->vid, extack) ||
		    !br_vlan_valid_range(vinfo, vinfo_last, extack))
			return -EINVAL;
	}

1833 1834 1835 1836
	switch (cmd) {
	case RTM_NEWVLAN:
		cmdmap = RTM_SETLINK;
		break;
1837 1838 1839
	case RTM_DELVLAN:
		cmdmap = RTM_DELLINK;
		break;
1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893
	}

	err = br_process_vlan_info(br, p, cmdmap, vinfo, &vinfo_last, &changed,
				   extack);
	if (changed)
		br_ifinfo_notify(cmdmap, br, p);

	return err;
}

static int br_vlan_rtm_process(struct sk_buff *skb, struct nlmsghdr *nlh,
			       struct netlink_ext_ack *extack)
{
	struct net *net = sock_net(skb->sk);
	struct br_vlan_msg *bvm;
	struct net_device *dev;
	struct nlattr *attr;
	int err, vlans = 0;
	int rem;

	/* this should validate the header and check for remaining bytes */
	err = nlmsg_parse(nlh, sizeof(*bvm), NULL, BRIDGE_VLANDB_MAX, NULL,
			  extack);
	if (err < 0)
		return err;

	bvm = nlmsg_data(nlh);
	dev = __dev_get_by_index(net, bvm->ifindex);
	if (!dev)
		return -ENODEV;

	if (!netif_is_bridge_master(dev) && !netif_is_bridge_port(dev)) {
		NL_SET_ERR_MSG_MOD(extack, "The device is not a valid bridge or bridge port");
		return -EINVAL;
	}

	nlmsg_for_each_attr(attr, nlh, sizeof(*bvm), rem) {
		if (nla_type(attr) != BRIDGE_VLANDB_ENTRY)
			continue;

		vlans++;
		err = br_vlan_rtm_process_one(dev, attr, nlh->nlmsg_type,
					      extack);
		if (err)
			break;
	}
	if (!vlans) {
		NL_SET_ERR_MSG_MOD(extack, "No vlans found to process");
		err = -EINVAL;
	}

	return err;
}

1894 1895 1896 1897
void br_vlan_rtnl_init(void)
{
	rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_GETVLAN, NULL,
			     br_vlan_rtm_dump, 0);
1898 1899
	rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_NEWVLAN,
			     br_vlan_rtm_process, NULL, 0);
1900 1901
	rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_DELVLAN,
			     br_vlan_rtm_process, NULL, 0);
1902 1903 1904 1905 1906
}

void br_vlan_rtnl_uninit(void)
{
	rtnl_unregister(PF_BRIDGE, RTM_GETVLAN);
1907
	rtnl_unregister(PF_BRIDGE, RTM_NEWVLAN);
1908
	rtnl_unregister(PF_BRIDGE, RTM_DELVLAN);
1909
}