xref: /openbmc/linux/net/bridge/br_vlan.c (revision b830f94f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/kernel.h>
3 #include <linux/netdevice.h>
4 #include <linux/rtnetlink.h>
5 #include <linux/slab.h>
6 #include <net/switchdev.h>
7 
8 #include "br_private.h"
9 #include "br_private_tunnel.h"
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,
14 			      const void *ptr)
15 {
16 	const struct net_bridge_vlan *vle = ptr;
17 	u16 vid = *(u16 *)arg->key;
18 
19 	return vle->vid != vid;
20 }
21 
22 static const struct rhashtable_params br_vlan_rht_params = {
23 	.head_offset = offsetof(struct net_bridge_vlan, vnode),
24 	.key_offset = offsetof(struct net_bridge_vlan, vid),
25 	.key_len = sizeof(u16),
26 	.nelem_hint = 3,
27 	.max_size = VLAN_N_VID,
28 	.obj_cmpfn = br_vlan_cmp,
29 	.automatic_shrinking = true,
30 };
31 
32 static struct net_bridge_vlan *br_vlan_lookup(struct rhashtable *tbl, u16 vid)
33 {
34 	return rhashtable_lookup_fast(tbl, &vid, br_vlan_rht_params);
35 }
36 
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 /* return true if anything changed, false otherwise */
60 static bool __vlan_add_flags(struct net_bridge_vlan *v, u16 flags)
61 {
62 	struct net_bridge_vlan_group *vg;
63 	u16 old_flags = v->flags;
64 	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 			  struct net_bridge_vlan *v, u16 flags,
86 			  struct netlink_ext_ack *extack)
87 {
88 	int err;
89 
90 	/* Try switchdev op first. In case it is not supported, fallback to
91 	 * 8021q add.
92 	 */
93 	err = br_switchdev_port_vlan_add(dev, v->vid, flags, extack);
94 	if (err == -EOPNOTSUPP)
95 		return vlan_vid_add(dev, br->vlan_proto, v->vid);
96 	v->priv_flags |= BR_VLFLAG_ADDED_BY_SWITCHDEV;
97 	return err;
98 }
99 
100 static void __vlan_add_list(struct net_bridge_vlan *v)
101 {
102 	struct net_bridge_vlan_group *vg;
103 	struct list_head *headp, *hpos;
104 	struct net_bridge_vlan *vent;
105 
106 	if (br_vlan_is_master(v))
107 		vg = br_vlan_group(v->br);
108 	else
109 		vg = nbp_vlan_group(v->port);
110 
111 	headp = &vg->vlan_list;
112 	list_for_each_prev(hpos, headp) {
113 		vent = list_entry(hpos, struct net_bridge_vlan, vlist);
114 		if (v->vid < vent->vid)
115 			continue;
116 		else
117 			break;
118 	}
119 	list_add_rcu(&v->vlist, hpos);
120 }
121 
122 static void __vlan_del_list(struct net_bridge_vlan *v)
123 {
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 	/* Try switchdev op first. In case it is not supported, fallback to
133 	 * 8021q del.
134 	 */
135 	err = br_switchdev_port_vlan_del(dev, v->vid);
136 	if (!(v->priv_flags & BR_VLFLAG_ADDED_BY_SWITCHDEV))
137 		vlan_vid_del(dev, br->vlan_proto, v->vid);
138 	return err == -EOPNOTSUPP ? 0 : err;
139 }
140 
141 /* Returns a master vlan, if it didn't exist it gets created. In all cases a
142  * a reference is taken to the master vlan before returning.
143  */
144 static struct net_bridge_vlan *
145 br_vlan_get_master(struct net_bridge *br, u16 vid,
146 		   struct netlink_ext_ack *extack)
147 {
148 	struct net_bridge_vlan_group *vg;
149 	struct net_bridge_vlan *masterv;
150 
151 	vg = br_vlan_group(br);
152 	masterv = br_vlan_find(vg, vid);
153 	if (!masterv) {
154 		bool changed;
155 
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 		if (WARN_ON(!masterv))
161 			return NULL;
162 		refcount_set(&masterv->refcnt, 1);
163 		return masterv;
164 	}
165 	refcount_inc(&masterv->refcnt);
166 
167 	return masterv;
168 }
169 
170 static void br_master_vlan_rcu_free(struct rcu_head *rcu)
171 {
172 	struct net_bridge_vlan *v;
173 
174 	v = container_of(rcu, struct net_bridge_vlan, rcu);
175 	WARN_ON(!br_vlan_is_master(v));
176 	free_percpu(v->stats);
177 	v->stats = NULL;
178 	kfree(v);
179 }
180 
181 static void br_vlan_put_master(struct net_bridge_vlan *masterv)
182 {
183 	struct net_bridge_vlan_group *vg;
184 
185 	if (!br_vlan_is_master(masterv))
186 		return;
187 
188 	vg = br_vlan_group(masterv->br);
189 	if (refcount_dec_and_test(&masterv->refcnt)) {
190 		rhashtable_remove_fast(&vg->vlan_hash,
191 				       &masterv->vnode, br_vlan_rht_params);
192 		__vlan_del_list(masterv);
193 		call_rcu(&masterv->rcu, br_master_vlan_rcu_free);
194 	}
195 }
196 
197 static void nbp_vlan_rcu_free(struct rcu_head *rcu)
198 {
199 	struct net_bridge_vlan *v;
200 
201 	v = container_of(rcu, struct net_bridge_vlan, rcu);
202 	WARN_ON(br_vlan_is_master(v));
203 	/* if we had per-port stats configured then free them here */
204 	if (v->priv_flags & BR_VLFLAG_PER_PORT_STATS)
205 		free_percpu(v->stats);
206 	v->stats = NULL;
207 	kfree(v);
208 }
209 
210 /* This is the shared VLAN add function which works for both ports and bridge
211  * devices. There are four possible calls to this function in terms of the
212  * vlan entry type:
213  * 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  *    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,
222 		      struct netlink_ext_ack *extack)
223 {
224 	struct net_bridge_vlan *masterv = NULL;
225 	struct net_bridge_port *p = NULL;
226 	struct net_bridge_vlan_group *vg;
227 	struct net_device *dev;
228 	struct net_bridge *br;
229 	int err;
230 
231 	if (br_vlan_is_master(v)) {
232 		br = v->br;
233 		dev = br->dev;
234 		vg = br_vlan_group(br);
235 	} else {
236 		p = v->port;
237 		br = p->br;
238 		dev = p->dev;
239 		vg = nbp_vlan_group(p);
240 	}
241 
242 	if (p) {
243 		/* Add VLAN to the device filter if it is supported.
244 		 * This ensures tagged traffic enters the bridge when
245 		 * promiscuous mode is disabled by br_manage_promisc().
246 		 */
247 		err = __vlan_vid_add(dev, br, v, flags, extack);
248 		if (err)
249 			goto out;
250 
251 		/* need to work on the master vlan too */
252 		if (flags & BRIDGE_VLAN_INFO_MASTER) {
253 			bool changed;
254 
255 			err = br_vlan_add(br, v->vid,
256 					  flags | BRIDGE_VLAN_INFO_BRENTRY,
257 					  &changed, extack);
258 			if (err)
259 				goto out_filt;
260 		}
261 
262 		masterv = br_vlan_get_master(br, v->vid, extack);
263 		if (!masterv)
264 			goto out_filt;
265 		v->brvlan = masterv;
266 		if (br_opt_get(br, BROPT_VLAN_STATS_PER_PORT)) {
267 			v->stats = netdev_alloc_pcpu_stats(struct br_vlan_stats);
268 			if (!v->stats) {
269 				err = -ENOMEM;
270 				goto out_filt;
271 			}
272 			v->priv_flags |= BR_VLFLAG_PER_PORT_STATS;
273 		} else {
274 			v->stats = masterv->stats;
275 		}
276 	} else {
277 		err = br_switchdev_port_vlan_add(dev, v->vid, flags, extack);
278 		if (err && err != -EOPNOTSUPP)
279 			goto out;
280 	}
281 
282 	/* Add the dev mac and count the vlan only if it's usable */
283 	if (br_vlan_should_use(v)) {
284 		err = br_fdb_insert(br, p, dev->dev_addr, v->vid);
285 		if (err) {
286 			br_err(br, "failed insert local address into bridge forwarding table\n");
287 			goto out_filt;
288 		}
289 		vg->num_vlans++;
290 	}
291 
292 	err = rhashtable_lookup_insert_fast(&vg->vlan_hash, &v->vnode,
293 					    br_vlan_rht_params);
294 	if (err)
295 		goto out_fdb_insert;
296 
297 	__vlan_add_list(v);
298 	__vlan_add_flags(v, flags);
299 
300 	if (p)
301 		nbp_vlan_set_vlan_dev_state(p, v->vid);
302 out:
303 	return err;
304 
305 out_fdb_insert:
306 	if (br_vlan_should_use(v)) {
307 		br_fdb_find_delete_local(br, p, dev->dev_addr, v->vid);
308 		vg->num_vlans--;
309 	}
310 
311 out_filt:
312 	if (p) {
313 		__vlan_vid_del(dev, br, v);
314 		if (masterv) {
315 			if (v->stats && masterv->stats != v->stats)
316 				free_percpu(v->stats);
317 			v->stats = NULL;
318 
319 			br_vlan_put_master(masterv);
320 			v->brvlan = NULL;
321 		}
322 	} else {
323 		br_switchdev_port_vlan_del(dev, v->vid);
324 	}
325 
326 	goto out;
327 }
328 
329 static int __vlan_del(struct net_bridge_vlan *v)
330 {
331 	struct net_bridge_vlan *masterv = v;
332 	struct net_bridge_vlan_group *vg;
333 	struct net_bridge_port *p = NULL;
334 	int err = 0;
335 
336 	if (br_vlan_is_master(v)) {
337 		vg = br_vlan_group(v->br);
338 	} else {
339 		p = v->port;
340 		vg = nbp_vlan_group(v->port);
341 		masterv = v->brvlan;
342 	}
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 	} else {
350 		err = br_switchdev_port_vlan_del(v->br->dev, v->vid);
351 		if (err && err != -EOPNOTSUPP)
352 			goto out;
353 		err = 0;
354 	}
355 
356 	if (br_vlan_should_use(v)) {
357 		v->flags &= ~BRIDGE_VLAN_INFO_BRENTRY;
358 		vg->num_vlans--;
359 	}
360 
361 	if (masterv != v) {
362 		vlan_tunnel_info_del(vg, v);
363 		rhashtable_remove_fast(&vg->vlan_hash, &v->vnode,
364 				       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 out:
372 	return err;
373 }
374 
375 static void __vlan_group_free(struct net_bridge_vlan_group *vg)
376 {
377 	WARN_ON(!list_empty(&vg->vlan_list));
378 	rhashtable_destroy(&vg->vlan_hash);
379 	vlan_tunnel_deinit(vg);
380 	kfree(vg);
381 }
382 
383 static void __vlan_flush(struct net_bridge_vlan_group *vg)
384 {
385 	struct net_bridge_vlan *vlan, *tmp;
386 
387 	__vlan_delete_pvid(vg, vg->pvid);
388 	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 	u16 vid;
400 
401 	/* If this packet was not filtered at input, let it pass */
402 	if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
403 		goto out;
404 
405 	/* At this point, we know that the frame was filtered and contains
406 	 * a valid vlan id.  If the vlan id has untagged flag set,
407 	 * send untagged; otherwise, send tagged.
408 	 */
409 	br_vlan_get_tag(skb, &vid);
410 	v = br_vlan_find(vg, vid);
411 	/* Vlan entry must be configured at this point.  The
412 	 * only exception is the bridge is set in promisc mode and the
413 	 * packet is destined for the bridge device.  In this case
414 	 * pass the packet as is.
415 	 */
416 	if (!v || !br_vlan_should_use(v)) {
417 		if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) {
418 			goto out;
419 		} else {
420 			kfree_skb(skb);
421 			return NULL;
422 		}
423 	}
424 	if (br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) {
425 		stats = this_cpu_ptr(v->stats);
426 		u64_stats_update_begin(&stats->syncp);
427 		stats->tx_bytes += skb->len;
428 		stats->tx_packets++;
429 		u64_stats_update_end(&stats->syncp);
430 	}
431 
432 	if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
433 		__vlan_hwaccel_clear_tag(skb);
434 
435 	if (p && (p->flags & BR_VLAN_TUNNEL) &&
436 	    br_handle_egress_vlan_tunnel(skb, v)) {
437 		kfree_skb(skb);
438 		return NULL;
439 	}
440 out:
441 	return skb;
442 }
443 
444 /* Called under RCU */
445 static bool __allowed_ingress(const struct net_bridge *br,
446 			      struct net_bridge_vlan_group *vg,
447 			      struct sk_buff *skb, u16 *vid)
448 {
449 	struct br_vlan_stats *stats;
450 	struct net_bridge_vlan *v;
451 	bool tagged;
452 
453 	BR_INPUT_SKB_CB(skb)->vlan_filtered = true;
454 	/* If vlan tx offload is disabled on bridge device and frame was
455 	 * sent from vlan device on the bridge device, it does not have
456 	 * HW accelerated vlan tag.
457 	 */
458 	if (unlikely(!skb_vlan_tag_present(skb) &&
459 		     skb->protocol == br->vlan_proto)) {
460 		skb = skb_vlan_untag(skb);
461 		if (unlikely(!skb))
462 			return false;
463 	}
464 
465 	if (!br_vlan_get_tag(skb, vid)) {
466 		/* Tagged frame */
467 		if (skb->vlan_proto != br->vlan_proto) {
468 			/* Protocol-mismatch, empty out vlan_tci for new tag */
469 			skb_push(skb, ETH_HLEN);
470 			skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
471 							skb_vlan_tag_get(skb));
472 			if (unlikely(!skb))
473 				return false;
474 
475 			skb_pull(skb, ETH_HLEN);
476 			skb_reset_mac_len(skb);
477 			*vid = 0;
478 			tagged = false;
479 		} else {
480 			tagged = true;
481 		}
482 	} else {
483 		/* Untagged frame */
484 		tagged = false;
485 	}
486 
487 	if (!*vid) {
488 		u16 pvid = br_get_pvid(vg);
489 
490 		/* Frame had a tag with VID 0 or did not have a tag.
491 		 * See if pvid is set on this port.  That tells us which
492 		 * vlan untagged or priority-tagged traffic belongs to.
493 		 */
494 		if (!pvid)
495 			goto drop;
496 
497 		/* PVID is set on this port.  Any untagged or priority-tagged
498 		 * 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 		else
505 			/* Priority-tagged Frame.
506 			 * At this point, we know that skb->vlan_tci VID
507 			 * field was 0.
508 			 * We update only VID field and preserve PCP field.
509 			 */
510 			skb->vlan_tci |= pvid;
511 
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 	if (!v || !br_vlan_should_use(v))
518 		goto drop;
519 
520 	if (br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) {
521 		stats = this_cpu_ptr(v->stats);
522 		u64_stats_update_begin(&stats->syncp);
523 		stats->rx_bytes += skb->len;
524 		stats->rx_packets++;
525 		u64_stats_update_end(&stats->syncp);
526 	}
527 
528 	return true;
529 
530 drop:
531 	kfree_skb(skb);
532 	return false;
533 }
534 
535 bool br_allowed_ingress(const struct net_bridge *br,
536 			struct net_bridge_vlan_group *vg, struct sk_buff *skb,
537 			u16 *vid)
538 {
539 	/* If VLAN filtering is disabled on the bridge, all packets are
540 	 * permitted.
541 	 */
542 	if (!br_opt_get(br, BROPT_VLAN_ENABLED)) {
543 		BR_INPUT_SKB_CB(skb)->vlan_filtered = false;
544 		return true;
545 	}
546 
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 		       const struct sk_buff *skb)
553 {
554 	const struct net_bridge_vlan *v;
555 	u16 vid;
556 
557 	/* If this packet was not filtered at input, let it pass */
558 	if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
559 		return true;
560 
561 	br_vlan_get_tag(skb, &vid);
562 	v = br_vlan_find(vg, vid);
563 	if (v && br_vlan_should_use(v))
564 		return true;
565 
566 	return false;
567 }
568 
569 /* Called under RCU */
570 bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid)
571 {
572 	struct net_bridge_vlan_group *vg;
573 	struct net_bridge *br = p->br;
574 
575 	/* If filtering was disabled at input, let it pass. */
576 	if (!br_opt_get(br, BROPT_VLAN_ENABLED))
577 		return true;
578 
579 	vg = nbp_vlan_group_rcu(p);
580 	if (!vg || !vg->num_vlans)
581 		return false;
582 
583 	if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto)
584 		*vid = 0;
585 
586 	if (!*vid) {
587 		*vid = br_get_pvid(vg);
588 		if (!*vid)
589 			return false;
590 
591 		return true;
592 	}
593 
594 	if (br_vlan_find(vg, *vid))
595 		return true;
596 
597 	return false;
598 }
599 
600 static int br_vlan_add_existing(struct net_bridge *br,
601 				struct net_bridge_vlan_group *vg,
602 				struct net_bridge_vlan *vlan,
603 				u16 flags, bool *changed,
604 				struct netlink_ext_ack *extack)
605 {
606 	int err;
607 
608 	err = br_switchdev_port_vlan_add(br->dev, vlan->vid, flags, extack);
609 	if (err && err != -EOPNOTSUPP)
610 		return err;
611 
612 	if (!br_vlan_is_brentry(vlan)) {
613 		/* Trying to change flags of non-existent bridge vlan */
614 		if (!(flags & BRIDGE_VLAN_INFO_BRENTRY)) {
615 			err = -EINVAL;
616 			goto err_flags;
617 		}
618 		/* It was only kept for port vlans, now make it real */
619 		err = br_fdb_insert(br, NULL, br->dev->dev_addr,
620 				    vlan->vid);
621 		if (err) {
622 			br_err(br, "failed to insert local address into bridge forwarding table\n");
623 			goto err_fdb_insert;
624 		}
625 
626 		refcount_inc(&vlan->refcnt);
627 		vlan->flags |= BRIDGE_VLAN_INFO_BRENTRY;
628 		vg->num_vlans++;
629 		*changed = true;
630 	}
631 
632 	if (__vlan_add_flags(vlan, flags))
633 		*changed = true;
634 
635 	return 0;
636 
637 err_fdb_insert:
638 err_flags:
639 	br_switchdev_port_vlan_del(br->dev, vlan->vid);
640 	return err;
641 }
642 
643 /* Must be protected by RTNL.
644  * 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 int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags, bool *changed,
648 		struct netlink_ext_ack *extack)
649 {
650 	struct net_bridge_vlan_group *vg;
651 	struct net_bridge_vlan *vlan;
652 	int ret;
653 
654 	ASSERT_RTNL();
655 
656 	*changed = false;
657 	vg = br_vlan_group(br);
658 	vlan = br_vlan_find(vg, vid);
659 	if (vlan)
660 		return br_vlan_add_existing(br, vg, vlan, flags, changed,
661 					    extack);
662 
663 	vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
664 	if (!vlan)
665 		return -ENOMEM;
666 
667 	vlan->stats = netdev_alloc_pcpu_stats(struct br_vlan_stats);
668 	if (!vlan->stats) {
669 		kfree(vlan);
670 		return -ENOMEM;
671 	}
672 	vlan->vid = vid;
673 	vlan->flags = flags | BRIDGE_VLAN_INFO_MASTER;
674 	vlan->flags &= ~BRIDGE_VLAN_INFO_PVID;
675 	vlan->br = br;
676 	if (flags & BRIDGE_VLAN_INFO_BRENTRY)
677 		refcount_set(&vlan->refcnt, 1);
678 	ret = __vlan_add(vlan, flags, extack);
679 	if (ret) {
680 		free_percpu(vlan->stats);
681 		kfree(vlan);
682 	} else {
683 		*changed = true;
684 	}
685 
686 	return ret;
687 }
688 
689 /* Must be protected by RTNL.
690  * 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 {
694 	struct net_bridge_vlan_group *vg;
695 	struct net_bridge_vlan *v;
696 
697 	ASSERT_RTNL();
698 
699 	vg = br_vlan_group(br);
700 	v = br_vlan_find(vg, vid);
701 	if (!v || !br_vlan_is_brentry(v))
702 		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 	vlan_tunnel_info_del(vg, v);
708 
709 	return __vlan_del(v);
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 	vg = br_vlan_group(br);
719 	__vlan_flush(vg);
720 	RCU_INIT_POINTER(br->vlgrp, NULL);
721 	synchronize_rcu();
722 	__vlan_group_free(vg);
723 }
724 
725 struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid)
726 {
727 	if (!vg)
728 		return NULL;
729 
730 	return br_vlan_lookup(&vg->vlan_hash, vid);
731 }
732 
733 /* Must be protected by RTNL. */
734 static void recalculate_group_addr(struct net_bridge *br)
735 {
736 	if (br_opt_get(br, BROPT_GROUP_ADDR_SET))
737 		return;
738 
739 	spin_lock_bh(&br->lock);
740 	if (!br_opt_get(br, BROPT_VLAN_ENABLED) ||
741 	    br->vlan_proto == htons(ETH_P_8021Q)) {
742 		/* Bridge Group Address */
743 		br->group_addr[5] = 0x00;
744 	} else { /* vlan_enabled && ETH_P_8021AD */
745 		/* Provider Bridge Group Address */
746 		br->group_addr[5] = 0x08;
747 	}
748 	spin_unlock_bh(&br->lock);
749 }
750 
751 /* Must be protected by RTNL. */
752 void br_recalculate_fwd_mask(struct net_bridge *br)
753 {
754 	if (!br_opt_get(br, BROPT_VLAN_ENABLED) ||
755 	    br->vlan_proto == htons(ETH_P_8021Q))
756 		br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT;
757 	else /* vlan_enabled && ETH_P_8021AD */
758 		br->group_fwd_mask_required = BR_GROUPFWD_8021AD &
759 					      ~(1u << br->group_addr[5]);
760 }
761 
762 int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
763 {
764 	struct switchdev_attr attr = {
765 		.orig_dev = br->dev,
766 		.id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
767 		.flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
768 		.u.vlan_filtering = val,
769 	};
770 	int err;
771 
772 	if (br_opt_get(br, BROPT_VLAN_ENABLED) == !!val)
773 		return 0;
774 
775 	err = switchdev_port_attr_set(br->dev, &attr);
776 	if (err && err != -EOPNOTSUPP)
777 		return err;
778 
779 	br_opt_toggle(br, BROPT_VLAN_ENABLED, !!val);
780 	br_manage_promisc(br);
781 	recalculate_group_addr(br);
782 	br_recalculate_fwd_mask(br);
783 
784 	return 0;
785 }
786 
787 int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
788 {
789 	return __br_vlan_filter_toggle(br, val);
790 }
791 
792 bool br_vlan_enabled(const struct net_device *dev)
793 {
794 	struct net_bridge *br = netdev_priv(dev);
795 
796 	return br_opt_get(br, BROPT_VLAN_ENABLED);
797 }
798 EXPORT_SYMBOL_GPL(br_vlan_enabled);
799 
800 int br_vlan_get_proto(const struct net_device *dev, u16 *p_proto)
801 {
802 	struct net_bridge *br = netdev_priv(dev);
803 
804 	*p_proto = ntohs(br->vlan_proto);
805 
806 	return 0;
807 }
808 EXPORT_SYMBOL_GPL(br_vlan_get_proto);
809 
810 int __br_vlan_set_proto(struct net_bridge *br, __be16 proto)
811 {
812 	int err = 0;
813 	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 	/* Add VLANs for the new proto to the device filter. */
822 	list_for_each_entry(p, &br->port_list, list) {
823 		vg = nbp_vlan_group(p);
824 		list_for_each_entry(vlan, &vg->vlan_list, vlist) {
825 			err = vlan_vid_add(p->dev, proto, vlan->vid);
826 			if (err)
827 				goto err_filt;
828 		}
829 	}
830 
831 	oldproto = br->vlan_proto;
832 	br->vlan_proto = proto;
833 
834 	recalculate_group_addr(br);
835 	br_recalculate_fwd_mask(br);
836 
837 	/* Delete VLANs for the old proto from the device filter. */
838 	list_for_each_entry(p, &br->port_list, list) {
839 		vg = nbp_vlan_group(p);
840 		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 	list_for_each_entry_continue_reverse(p, &br->port_list, list) {
851 		vg = nbp_vlan_group(p);
852 		list_for_each_entry(vlan, &vg->vlan_list, vlist)
853 			vlan_vid_del(p->dev, proto, vlan->vid);
854 	}
855 
856 	return err;
857 }
858 
859 int br_vlan_set_proto(struct net_bridge *br, unsigned long val)
860 {
861 	if (val != ETH_P_8021Q && val != ETH_P_8021AD)
862 		return -EPROTONOSUPPORT;
863 
864 	return __br_vlan_set_proto(br, htons(val));
865 }
866 
867 int br_vlan_set_stats(struct net_bridge *br, unsigned long val)
868 {
869 	switch (val) {
870 	case 0:
871 	case 1:
872 		br_opt_toggle(br, BROPT_VLAN_STATS_ENABLED, !!val);
873 		break;
874 	default:
875 		return -EINVAL;
876 	}
877 
878 	return 0;
879 }
880 
881 int br_vlan_set_stats_per_port(struct net_bridge *br, unsigned long val)
882 {
883 	struct net_bridge_port *p;
884 
885 	/* allow to change the option if there are no port vlans configured */
886 	list_for_each_entry(p, &br->port_list, list) {
887 		struct net_bridge_vlan_group *vg = nbp_vlan_group(p);
888 
889 		if (vg->num_vlans)
890 			return -EBUSY;
891 	}
892 
893 	switch (val) {
894 	case 0:
895 	case 1:
896 		br_opt_toggle(br, BROPT_VLAN_STATS_PER_PORT, !!val);
897 		break;
898 	default:
899 		return -EINVAL;
900 	}
901 
902 	return 0;
903 }
904 
905 static bool vlan_default_pvid(struct net_bridge_vlan_group *vg, u16 vid)
906 {
907 	struct net_bridge_vlan *v;
908 
909 	if (vid != vg->pvid)
910 		return false;
911 
912 	v = br_vlan_lookup(&vg->vlan_hash, vid);
913 	if (v && br_vlan_should_use(v) &&
914 	    (v->flags & BRIDGE_VLAN_INFO_UNTAGGED))
915 		return true;
916 
917 	return false;
918 }
919 
920 static void br_vlan_disable_default_pvid(struct net_bridge *br)
921 {
922 	struct net_bridge_port *p;
923 	u16 pvid = br->default_pvid;
924 
925 	/* Disable default_pvid on all ports where it is still
926 	 * configured.
927 	 */
928 	if (vlan_default_pvid(br_vlan_group(br), pvid))
929 		br_vlan_delete(br, pvid);
930 
931 	list_for_each_entry(p, &br->port_list, list) {
932 		if (vlan_default_pvid(nbp_vlan_group(p), pvid))
933 			nbp_vlan_delete(p, pvid);
934 	}
935 
936 	br->default_pvid = 0;
937 }
938 
939 int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid,
940 			       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 	unsigned long *changed;
946 	bool vlchange;
947 	u16 old_pvid;
948 	int err = 0;
949 
950 	if (!pvid) {
951 		br_vlan_disable_default_pvid(br);
952 		return 0;
953 	}
954 
955 	changed = bitmap_zalloc(BR_MAX_PORTS, GFP_KERNEL);
956 	if (!changed)
957 		return -ENOMEM;
958 
959 	old_pvid = br->default_pvid;
960 
961 	/* Update default_pvid config only if we do not conflict with
962 	 * user configuration.
963 	 */
964 	vg = br_vlan_group(br);
965 	pvent = br_vlan_find(vg, pvid);
966 	if ((!old_pvid || vlan_default_pvid(vg, old_pvid)) &&
967 	    (!pvent || !br_vlan_should_use(pvent))) {
968 		err = br_vlan_add(br, pvid,
969 				  BRIDGE_VLAN_INFO_PVID |
970 				  BRIDGE_VLAN_INFO_UNTAGGED |
971 				  BRIDGE_VLAN_INFO_BRENTRY,
972 				  &vlchange, extack);
973 		if (err)
974 			goto out;
975 		br_vlan_delete(br, old_pvid);
976 		set_bit(0, changed);
977 	}
978 
979 	list_for_each_entry(p, &br->port_list, list) {
980 		/* Update default_pvid config only if we do not conflict with
981 		 * user configuration.
982 		 */
983 		vg = nbp_vlan_group(p);
984 		if ((old_pvid &&
985 		     !vlan_default_pvid(vg, old_pvid)) ||
986 		    br_vlan_find(vg, pvid))
987 			continue;
988 
989 		err = nbp_vlan_add(p, pvid,
990 				   BRIDGE_VLAN_INFO_PVID |
991 				   BRIDGE_VLAN_INFO_UNTAGGED,
992 				   &vlchange, extack);
993 		if (err)
994 			goto err_port;
995 		nbp_vlan_delete(p, old_pvid);
996 		set_bit(p->port_no, changed);
997 	}
998 
999 	br->default_pvid = pvid;
1000 
1001 out:
1002 	bitmap_free(changed);
1003 	return err;
1004 
1005 err_port:
1006 	list_for_each_entry_continue_reverse(p, &br->port_list, list) {
1007 		if (!test_bit(p->port_no, changed))
1008 			continue;
1009 
1010 		if (old_pvid)
1011 			nbp_vlan_add(p, old_pvid,
1012 				     BRIDGE_VLAN_INFO_PVID |
1013 				     BRIDGE_VLAN_INFO_UNTAGGED,
1014 				     &vlchange, NULL);
1015 		nbp_vlan_delete(p, pvid);
1016 	}
1017 
1018 	if (test_bit(0, changed)) {
1019 		if (old_pvid)
1020 			br_vlan_add(br, old_pvid,
1021 				    BRIDGE_VLAN_INFO_PVID |
1022 				    BRIDGE_VLAN_INFO_UNTAGGED |
1023 				    BRIDGE_VLAN_INFO_BRENTRY,
1024 				    &vlchange, NULL);
1025 		br_vlan_delete(br, pvid);
1026 	}
1027 	goto out;
1028 }
1029 
1030 int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val)
1031 {
1032 	u16 pvid = val;
1033 	int err = 0;
1034 
1035 	if (val >= VLAN_VID_MASK)
1036 		return -EINVAL;
1037 
1038 	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 		pr_info_once("Please disable vlan filtering to change default_pvid\n");
1044 		err = -EPERM;
1045 		goto out;
1046 	}
1047 	err = __br_vlan_set_default_pvid(br, pvid, NULL);
1048 out:
1049 	return err;
1050 }
1051 
1052 int br_vlan_init(struct net_bridge *br)
1053 {
1054 	struct net_bridge_vlan_group *vg;
1055 	int ret = -ENOMEM;
1056 	bool changed;
1057 
1058 	vg = kzalloc(sizeof(*vg), GFP_KERNEL);
1059 	if (!vg)
1060 		goto out;
1061 	ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
1062 	if (ret)
1063 		goto err_rhtbl;
1064 	ret = vlan_tunnel_init(vg);
1065 	if (ret)
1066 		goto err_tunnel_init;
1067 	INIT_LIST_HEAD(&vg->vlan_list);
1068 	br->vlan_proto = htons(ETH_P_8021Q);
1069 	br->default_pvid = 1;
1070 	rcu_assign_pointer(br->vlgrp, vg);
1071 	ret = br_vlan_add(br, 1,
1072 			  BRIDGE_VLAN_INFO_PVID | BRIDGE_VLAN_INFO_UNTAGGED |
1073 			  BRIDGE_VLAN_INFO_BRENTRY, &changed, NULL);
1074 	if (ret)
1075 		goto err_vlan_add;
1076 
1077 out:
1078 	return ret;
1079 
1080 err_vlan_add:
1081 	vlan_tunnel_deinit(vg);
1082 err_tunnel_init:
1083 	rhashtable_destroy(&vg->vlan_hash);
1084 err_rhtbl:
1085 	kfree(vg);
1086 
1087 	goto out;
1088 }
1089 
1090 int nbp_vlan_init(struct net_bridge_port *p, struct netlink_ext_ack *extack)
1091 {
1092 	struct switchdev_attr attr = {
1093 		.orig_dev = p->br->dev,
1094 		.id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
1095 		.flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
1096 		.u.vlan_filtering = br_opt_get(p->br, BROPT_VLAN_ENABLED),
1097 	};
1098 	struct net_bridge_vlan_group *vg;
1099 	int ret = -ENOMEM;
1100 
1101 	vg = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL);
1102 	if (!vg)
1103 		goto out;
1104 
1105 	ret = switchdev_port_attr_set(p->dev, &attr);
1106 	if (ret && ret != -EOPNOTSUPP)
1107 		goto err_vlan_enabled;
1108 
1109 	ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
1110 	if (ret)
1111 		goto err_rhtbl;
1112 	ret = vlan_tunnel_init(vg);
1113 	if (ret)
1114 		goto err_tunnel_init;
1115 	INIT_LIST_HEAD(&vg->vlan_list);
1116 	rcu_assign_pointer(p->vlgrp, vg);
1117 	if (p->br->default_pvid) {
1118 		bool changed;
1119 
1120 		ret = nbp_vlan_add(p, p->br->default_pvid,
1121 				   BRIDGE_VLAN_INFO_PVID |
1122 				   BRIDGE_VLAN_INFO_UNTAGGED,
1123 				   &changed, extack);
1124 		if (ret)
1125 			goto err_vlan_add;
1126 	}
1127 out:
1128 	return ret;
1129 
1130 err_vlan_add:
1131 	RCU_INIT_POINTER(p->vlgrp, NULL);
1132 	synchronize_rcu();
1133 	vlan_tunnel_deinit(vg);
1134 err_tunnel_init:
1135 	rhashtable_destroy(&vg->vlan_hash);
1136 err_rhtbl:
1137 err_vlan_enabled:
1138 	kfree(vg);
1139 
1140 	goto out;
1141 }
1142 
1143 /* Must be protected by RTNL.
1144  * Must be called with vid in range from 1 to 4094 inclusive.
1145  * changed must be true only if the vlan was created or updated
1146  */
1147 int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags,
1148 		 bool *changed, struct netlink_ext_ack *extack)
1149 {
1150 	struct net_bridge_vlan *vlan;
1151 	int ret;
1152 
1153 	ASSERT_RTNL();
1154 
1155 	*changed = false;
1156 	vlan = br_vlan_find(nbp_vlan_group(port), vid);
1157 	if (vlan) {
1158 		/* Pass the flags to the hardware bridge */
1159 		ret = br_switchdev_port_vlan_add(port->dev, vid, flags, extack);
1160 		if (ret && ret != -EOPNOTSUPP)
1161 			return ret;
1162 		*changed = __vlan_add_flags(vlan, flags);
1163 
1164 		return 0;
1165 	}
1166 
1167 	vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
1168 	if (!vlan)
1169 		return -ENOMEM;
1170 
1171 	vlan->vid = vid;
1172 	vlan->port = port;
1173 	ret = __vlan_add(vlan, flags, extack);
1174 	if (ret)
1175 		kfree(vlan);
1176 	else
1177 		*changed = true;
1178 
1179 	return ret;
1180 }
1181 
1182 /* Must be protected by RTNL.
1183  * Must be called with vid in range from 1 to 4094 inclusive.
1184  */
1185 int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
1186 {
1187 	struct net_bridge_vlan *v;
1188 
1189 	ASSERT_RTNL();
1190 
1191 	v = br_vlan_find(nbp_vlan_group(port), vid);
1192 	if (!v)
1193 		return -ENOENT;
1194 	br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
1195 	br_fdb_delete_by_port(port->br, port, vid, 0);
1196 
1197 	return __vlan_del(v);
1198 }
1199 
1200 void nbp_vlan_flush(struct net_bridge_port *port)
1201 {
1202 	struct net_bridge_vlan_group *vg;
1203 
1204 	ASSERT_RTNL();
1205 
1206 	vg = nbp_vlan_group(port);
1207 	__vlan_flush(vg);
1208 	RCU_INIT_POINTER(port->vlgrp, NULL);
1209 	synchronize_rcu();
1210 	__vlan_group_free(vg);
1211 }
1212 
1213 void br_vlan_get_stats(const struct net_bridge_vlan *v,
1214 		       struct br_vlan_stats *stats)
1215 {
1216 	int i;
1217 
1218 	memset(stats, 0, sizeof(*stats));
1219 	for_each_possible_cpu(i) {
1220 		u64 rxpackets, rxbytes, txpackets, txbytes;
1221 		struct br_vlan_stats *cpu_stats;
1222 		unsigned int start;
1223 
1224 		cpu_stats = per_cpu_ptr(v->stats, i);
1225 		do {
1226 			start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
1227 			rxpackets = cpu_stats->rx_packets;
1228 			rxbytes = cpu_stats->rx_bytes;
1229 			txbytes = cpu_stats->tx_bytes;
1230 			txpackets = cpu_stats->tx_packets;
1231 		} while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
1232 
1233 		stats->rx_packets += rxpackets;
1234 		stats->rx_bytes += rxbytes;
1235 		stats->tx_bytes += txbytes;
1236 		stats->tx_packets += txpackets;
1237 	}
1238 }
1239 
1240 static int __br_vlan_get_pvid(const struct net_device *dev,
1241 			      struct net_bridge_port *p, u16 *p_pvid)
1242 {
1243 	struct net_bridge_vlan_group *vg;
1244 
1245 	if (p)
1246 		vg = nbp_vlan_group(p);
1247 	else if (netif_is_bridge_master(dev))
1248 		vg = br_vlan_group(netdev_priv(dev));
1249 	else
1250 		return -EINVAL;
1251 
1252 	*p_pvid = br_get_pvid(vg);
1253 	return 0;
1254 }
1255 
1256 int br_vlan_get_pvid(const struct net_device *dev, u16 *p_pvid)
1257 {
1258 	ASSERT_RTNL();
1259 
1260 	return __br_vlan_get_pvid(dev, br_port_get_check_rtnl(dev), p_pvid);
1261 }
1262 EXPORT_SYMBOL_GPL(br_vlan_get_pvid);
1263 
1264 int br_vlan_get_pvid_rcu(const struct net_device *dev, u16 *p_pvid)
1265 {
1266 	return __br_vlan_get_pvid(dev, br_port_get_check_rcu(dev), p_pvid);
1267 }
1268 EXPORT_SYMBOL_GPL(br_vlan_get_pvid_rcu);
1269 
1270 int br_vlan_get_info(const struct net_device *dev, u16 vid,
1271 		     struct bridge_vlan_info *p_vinfo)
1272 {
1273 	struct net_bridge_vlan_group *vg;
1274 	struct net_bridge_vlan *v;
1275 	struct net_bridge_port *p;
1276 
1277 	ASSERT_RTNL();
1278 	p = br_port_get_check_rtnl(dev);
1279 	if (p)
1280 		vg = nbp_vlan_group(p);
1281 	else if (netif_is_bridge_master(dev))
1282 		vg = br_vlan_group(netdev_priv(dev));
1283 	else
1284 		return -EINVAL;
1285 
1286 	v = br_vlan_find(vg, vid);
1287 	if (!v)
1288 		return -ENOENT;
1289 
1290 	p_vinfo->vid = vid;
1291 	p_vinfo->flags = v->flags;
1292 	return 0;
1293 }
1294 EXPORT_SYMBOL_GPL(br_vlan_get_info);
1295 
1296 static int br_vlan_is_bind_vlan_dev(const struct net_device *dev)
1297 {
1298 	return is_vlan_dev(dev) &&
1299 		!!(vlan_dev_priv(dev)->flags & VLAN_FLAG_BRIDGE_BINDING);
1300 }
1301 
1302 static int br_vlan_is_bind_vlan_dev_fn(struct net_device *dev,
1303 				       __always_unused void *data)
1304 {
1305 	return br_vlan_is_bind_vlan_dev(dev);
1306 }
1307 
1308 static bool br_vlan_has_upper_bind_vlan_dev(struct net_device *dev)
1309 {
1310 	int found;
1311 
1312 	rcu_read_lock();
1313 	found = netdev_walk_all_upper_dev_rcu(dev, br_vlan_is_bind_vlan_dev_fn,
1314 					      NULL);
1315 	rcu_read_unlock();
1316 
1317 	return !!found;
1318 }
1319 
1320 struct br_vlan_bind_walk_data {
1321 	u16 vid;
1322 	struct net_device *result;
1323 };
1324 
1325 static int br_vlan_match_bind_vlan_dev_fn(struct net_device *dev,
1326 					  void *data_in)
1327 {
1328 	struct br_vlan_bind_walk_data *data = data_in;
1329 	int found = 0;
1330 
1331 	if (br_vlan_is_bind_vlan_dev(dev) &&
1332 	    vlan_dev_priv(dev)->vlan_id == data->vid) {
1333 		data->result = dev;
1334 		found = 1;
1335 	}
1336 
1337 	return found;
1338 }
1339 
1340 static struct net_device *
1341 br_vlan_get_upper_bind_vlan_dev(struct net_device *dev, u16 vid)
1342 {
1343 	struct br_vlan_bind_walk_data data = {
1344 		.vid = vid,
1345 	};
1346 
1347 	rcu_read_lock();
1348 	netdev_walk_all_upper_dev_rcu(dev, br_vlan_match_bind_vlan_dev_fn,
1349 				      &data);
1350 	rcu_read_unlock();
1351 
1352 	return data.result;
1353 }
1354 
1355 static bool br_vlan_is_dev_up(const struct net_device *dev)
1356 {
1357 	return  !!(dev->flags & IFF_UP) && netif_oper_up(dev);
1358 }
1359 
1360 static void br_vlan_set_vlan_dev_state(const struct net_bridge *br,
1361 				       struct net_device *vlan_dev)
1362 {
1363 	u16 vid = vlan_dev_priv(vlan_dev)->vlan_id;
1364 	struct net_bridge_vlan_group *vg;
1365 	struct net_bridge_port *p;
1366 	bool has_carrier = false;
1367 
1368 	if (!netif_carrier_ok(br->dev)) {
1369 		netif_carrier_off(vlan_dev);
1370 		return;
1371 	}
1372 
1373 	list_for_each_entry(p, &br->port_list, list) {
1374 		vg = nbp_vlan_group(p);
1375 		if (br_vlan_find(vg, vid) && br_vlan_is_dev_up(p->dev)) {
1376 			has_carrier = true;
1377 			break;
1378 		}
1379 	}
1380 
1381 	if (has_carrier)
1382 		netif_carrier_on(vlan_dev);
1383 	else
1384 		netif_carrier_off(vlan_dev);
1385 }
1386 
1387 static void br_vlan_set_all_vlan_dev_state(struct net_bridge_port *p)
1388 {
1389 	struct net_bridge_vlan_group *vg = nbp_vlan_group(p);
1390 	struct net_bridge_vlan *vlan;
1391 	struct net_device *vlan_dev;
1392 
1393 	list_for_each_entry(vlan, &vg->vlan_list, vlist) {
1394 		vlan_dev = br_vlan_get_upper_bind_vlan_dev(p->br->dev,
1395 							   vlan->vid);
1396 		if (vlan_dev) {
1397 			if (br_vlan_is_dev_up(p->dev)) {
1398 				if (netif_carrier_ok(p->br->dev))
1399 					netif_carrier_on(vlan_dev);
1400 			} else {
1401 				br_vlan_set_vlan_dev_state(p->br, vlan_dev);
1402 			}
1403 		}
1404 	}
1405 }
1406 
1407 static void br_vlan_upper_change(struct net_device *dev,
1408 				 struct net_device *upper_dev,
1409 				 bool linking)
1410 {
1411 	struct net_bridge *br = netdev_priv(dev);
1412 
1413 	if (!br_vlan_is_bind_vlan_dev(upper_dev))
1414 		return;
1415 
1416 	if (linking) {
1417 		br_vlan_set_vlan_dev_state(br, upper_dev);
1418 		br_opt_toggle(br, BROPT_VLAN_BRIDGE_BINDING, true);
1419 	} else {
1420 		br_opt_toggle(br, BROPT_VLAN_BRIDGE_BINDING,
1421 			      br_vlan_has_upper_bind_vlan_dev(dev));
1422 	}
1423 }
1424 
1425 struct br_vlan_link_state_walk_data {
1426 	struct net_bridge *br;
1427 };
1428 
1429 static int br_vlan_link_state_change_fn(struct net_device *vlan_dev,
1430 					void *data_in)
1431 {
1432 	struct br_vlan_link_state_walk_data *data = data_in;
1433 
1434 	if (br_vlan_is_bind_vlan_dev(vlan_dev))
1435 		br_vlan_set_vlan_dev_state(data->br, vlan_dev);
1436 
1437 	return 0;
1438 }
1439 
1440 static void br_vlan_link_state_change(struct net_device *dev,
1441 				      struct net_bridge *br)
1442 {
1443 	struct br_vlan_link_state_walk_data data = {
1444 		.br = br
1445 	};
1446 
1447 	rcu_read_lock();
1448 	netdev_walk_all_upper_dev_rcu(dev, br_vlan_link_state_change_fn,
1449 				      &data);
1450 	rcu_read_unlock();
1451 }
1452 
1453 /* Must be protected by RTNL. */
1454 static void nbp_vlan_set_vlan_dev_state(struct net_bridge_port *p, u16 vid)
1455 {
1456 	struct net_device *vlan_dev;
1457 
1458 	if (!br_opt_get(p->br, BROPT_VLAN_BRIDGE_BINDING))
1459 		return;
1460 
1461 	vlan_dev = br_vlan_get_upper_bind_vlan_dev(p->br->dev, vid);
1462 	if (vlan_dev)
1463 		br_vlan_set_vlan_dev_state(p->br, vlan_dev);
1464 }
1465 
1466 /* Must be protected by RTNL. */
1467 void br_vlan_bridge_event(struct net_device *dev, unsigned long event,
1468 			  void *ptr)
1469 {
1470 	struct netdev_notifier_changeupper_info *info;
1471 	struct net_bridge *br;
1472 
1473 	switch (event) {
1474 	case NETDEV_CHANGEUPPER:
1475 		info = ptr;
1476 		br_vlan_upper_change(dev, info->upper_dev, info->linking);
1477 		break;
1478 
1479 	case NETDEV_CHANGE:
1480 	case NETDEV_UP:
1481 		br = netdev_priv(dev);
1482 		if (!br_opt_get(br, BROPT_VLAN_BRIDGE_BINDING))
1483 			return;
1484 		br_vlan_link_state_change(dev, br);
1485 		break;
1486 	}
1487 }
1488 
1489 /* Must be protected by RTNL. */
1490 void br_vlan_port_event(struct net_bridge_port *p, unsigned long event)
1491 {
1492 	if (!br_opt_get(p->br, BROPT_VLAN_BRIDGE_BINDING))
1493 		return;
1494 
1495 	switch (event) {
1496 	case NETDEV_CHANGE:
1497 	case NETDEV_DOWN:
1498 	case NETDEV_UP:
1499 		br_vlan_set_all_vlan_dev_state(p);
1500 		break;
1501 	}
1502 }
1503