xref: /openbmc/linux/net/bridge/br_vlan.c (revision 1e328ed5)
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,
38 			    const struct net_bridge_vlan *v)
39 {
40 	if (vg->pvid == v->vid)
41 		return false;
42 
43 	smp_wmb();
44 	br_vlan_set_pvid_state(vg, v->state);
45 	vg->pvid = v->vid;
46 
47 	return true;
48 }
49 
50 static bool __vlan_delete_pvid(struct net_bridge_vlan_group *vg, u16 vid)
51 {
52 	if (vg->pvid != vid)
53 		return false;
54 
55 	smp_wmb();
56 	vg->pvid = 0;
57 
58 	return true;
59 }
60 
61 /* return true if anything changed, false otherwise */
62 static bool __vlan_add_flags(struct net_bridge_vlan *v, u16 flags)
63 {
64 	struct net_bridge_vlan_group *vg;
65 	u16 old_flags = v->flags;
66 	bool ret;
67 
68 	if (br_vlan_is_master(v))
69 		vg = br_vlan_group(v->br);
70 	else
71 		vg = nbp_vlan_group(v->port);
72 
73 	if (flags & BRIDGE_VLAN_INFO_PVID)
74 		ret = __vlan_add_pvid(vg, v);
75 	else
76 		ret = __vlan_delete_pvid(vg, v->vid);
77 
78 	if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
79 		v->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
80 	else
81 		v->flags &= ~BRIDGE_VLAN_INFO_UNTAGGED;
82 
83 	return ret || !!(old_flags ^ v->flags);
84 }
85 
86 static int __vlan_vid_add(struct net_device *dev, struct net_bridge *br,
87 			  struct net_bridge_vlan *v, u16 flags,
88 			  struct netlink_ext_ack *extack)
89 {
90 	int err;
91 
92 	/* Try switchdev op first. In case it is not supported, fallback to
93 	 * 8021q add.
94 	 */
95 	err = br_switchdev_port_vlan_add(dev, v->vid, flags, extack);
96 	if (err == -EOPNOTSUPP)
97 		return vlan_vid_add(dev, br->vlan_proto, v->vid);
98 	v->priv_flags |= BR_VLFLAG_ADDED_BY_SWITCHDEV;
99 	return err;
100 }
101 
102 static void __vlan_add_list(struct net_bridge_vlan *v)
103 {
104 	struct net_bridge_vlan_group *vg;
105 	struct list_head *headp, *hpos;
106 	struct net_bridge_vlan *vent;
107 
108 	if (br_vlan_is_master(v))
109 		vg = br_vlan_group(v->br);
110 	else
111 		vg = nbp_vlan_group(v->port);
112 
113 	headp = &vg->vlan_list;
114 	list_for_each_prev(hpos, headp) {
115 		vent = list_entry(hpos, struct net_bridge_vlan, vlist);
116 		if (v->vid < vent->vid)
117 			continue;
118 		else
119 			break;
120 	}
121 	list_add_rcu(&v->vlist, hpos);
122 }
123 
124 static void __vlan_del_list(struct net_bridge_vlan *v)
125 {
126 	list_del_rcu(&v->vlist);
127 }
128 
129 static int __vlan_vid_del(struct net_device *dev, struct net_bridge *br,
130 			  const struct net_bridge_vlan *v)
131 {
132 	int err;
133 
134 	/* Try switchdev op first. In case it is not supported, fallback to
135 	 * 8021q del.
136 	 */
137 	err = br_switchdev_port_vlan_del(dev, v->vid);
138 	if (!(v->priv_flags & BR_VLFLAG_ADDED_BY_SWITCHDEV))
139 		vlan_vid_del(dev, br->vlan_proto, v->vid);
140 	return err == -EOPNOTSUPP ? 0 : err;
141 }
142 
143 /* Returns a master vlan, if it didn't exist it gets created. In all cases
144  * a reference is taken to the master vlan before returning.
145  */
146 static struct net_bridge_vlan *
147 br_vlan_get_master(struct net_bridge *br, u16 vid,
148 		   struct netlink_ext_ack *extack)
149 {
150 	struct net_bridge_vlan_group *vg;
151 	struct net_bridge_vlan *masterv;
152 
153 	vg = br_vlan_group(br);
154 	masterv = br_vlan_find(vg, vid);
155 	if (!masterv) {
156 		bool changed;
157 
158 		/* missing global ctx, create it now */
159 		if (br_vlan_add(br, vid, 0, &changed, extack))
160 			return NULL;
161 		masterv = br_vlan_find(vg, vid);
162 		if (WARN_ON(!masterv))
163 			return NULL;
164 		refcount_set(&masterv->refcnt, 1);
165 		return masterv;
166 	}
167 	refcount_inc(&masterv->refcnt);
168 
169 	return masterv;
170 }
171 
172 static void br_master_vlan_rcu_free(struct rcu_head *rcu)
173 {
174 	struct net_bridge_vlan *v;
175 
176 	v = container_of(rcu, struct net_bridge_vlan, rcu);
177 	WARN_ON(!br_vlan_is_master(v));
178 	free_percpu(v->stats);
179 	v->stats = NULL;
180 	kfree(v);
181 }
182 
183 static void br_vlan_put_master(struct net_bridge_vlan *masterv)
184 {
185 	struct net_bridge_vlan_group *vg;
186 
187 	if (!br_vlan_is_master(masterv))
188 		return;
189 
190 	vg = br_vlan_group(masterv->br);
191 	if (refcount_dec_and_test(&masterv->refcnt)) {
192 		rhashtable_remove_fast(&vg->vlan_hash,
193 				       &masterv->vnode, br_vlan_rht_params);
194 		__vlan_del_list(masterv);
195 		call_rcu(&masterv->rcu, br_master_vlan_rcu_free);
196 	}
197 }
198 
199 static void nbp_vlan_rcu_free(struct rcu_head *rcu)
200 {
201 	struct net_bridge_vlan *v;
202 
203 	v = container_of(rcu, struct net_bridge_vlan, rcu);
204 	WARN_ON(br_vlan_is_master(v));
205 	/* if we had per-port stats configured then free them here */
206 	if (v->priv_flags & BR_VLFLAG_PER_PORT_STATS)
207 		free_percpu(v->stats);
208 	v->stats = NULL;
209 	kfree(v);
210 }
211 
212 /* This is the shared VLAN add function which works for both ports and bridge
213  * devices. There are four possible calls to this function in terms of the
214  * vlan entry type:
215  * 1. vlan is being added on a port (no master flags, global entry exists)
216  * 2. vlan is being added on a bridge (both master and brentry flags)
217  * 3. vlan is being added on a port, but a global entry didn't exist which
218  *    is being created right now (master flag set, brentry flag unset), the
219  *    global entry is used for global per-vlan features, but not for filtering
220  * 4. same as 3 but with both master and brentry flags set so the entry
221  *    will be used for filtering in both the port and the bridge
222  */
223 static int __vlan_add(struct net_bridge_vlan *v, u16 flags,
224 		      struct netlink_ext_ack *extack)
225 {
226 	struct net_bridge_vlan *masterv = NULL;
227 	struct net_bridge_port *p = NULL;
228 	struct net_bridge_vlan_group *vg;
229 	struct net_device *dev;
230 	struct net_bridge *br;
231 	int err;
232 
233 	if (br_vlan_is_master(v)) {
234 		br = v->br;
235 		dev = br->dev;
236 		vg = br_vlan_group(br);
237 	} else {
238 		p = v->port;
239 		br = p->br;
240 		dev = p->dev;
241 		vg = nbp_vlan_group(p);
242 	}
243 
244 	if (p) {
245 		/* Add VLAN to the device filter if it is supported.
246 		 * This ensures tagged traffic enters the bridge when
247 		 * promiscuous mode is disabled by br_manage_promisc().
248 		 */
249 		err = __vlan_vid_add(dev, br, v, flags, extack);
250 		if (err)
251 			goto out;
252 
253 		/* need to work on the master vlan too */
254 		if (flags & BRIDGE_VLAN_INFO_MASTER) {
255 			bool changed;
256 
257 			err = br_vlan_add(br, v->vid,
258 					  flags | BRIDGE_VLAN_INFO_BRENTRY,
259 					  &changed, extack);
260 			if (err)
261 				goto out_filt;
262 
263 			if (changed)
264 				br_vlan_notify(br, NULL, v->vid, 0,
265 					       RTM_NEWVLAN);
266 		}
267 
268 		masterv = br_vlan_get_master(br, v->vid, extack);
269 		if (!masterv) {
270 			err = -ENOMEM;
271 			goto out_filt;
272 		}
273 		v->brvlan = masterv;
274 		if (br_opt_get(br, BROPT_VLAN_STATS_PER_PORT)) {
275 			v->stats =
276 			     netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
277 			if (!v->stats) {
278 				err = -ENOMEM;
279 				goto out_filt;
280 			}
281 			v->priv_flags |= BR_VLFLAG_PER_PORT_STATS;
282 		} else {
283 			v->stats = masterv->stats;
284 		}
285 	} else {
286 		err = br_switchdev_port_vlan_add(dev, v->vid, flags, extack);
287 		if (err && err != -EOPNOTSUPP)
288 			goto out;
289 	}
290 
291 	/* Add the dev mac and count the vlan only if it's usable */
292 	if (br_vlan_should_use(v)) {
293 		err = br_fdb_insert(br, p, dev->dev_addr, v->vid);
294 		if (err) {
295 			br_err(br, "failed insert local address into bridge forwarding table\n");
296 			goto out_filt;
297 		}
298 		vg->num_vlans++;
299 	}
300 
301 	/* set the state before publishing */
302 	v->state = BR_STATE_FORWARDING;
303 
304 	err = rhashtable_lookup_insert_fast(&vg->vlan_hash, &v->vnode,
305 					    br_vlan_rht_params);
306 	if (err)
307 		goto out_fdb_insert;
308 
309 	__vlan_add_list(v);
310 	__vlan_add_flags(v, flags);
311 
312 	if (p)
313 		nbp_vlan_set_vlan_dev_state(p, v->vid);
314 out:
315 	return err;
316 
317 out_fdb_insert:
318 	if (br_vlan_should_use(v)) {
319 		br_fdb_find_delete_local(br, p, dev->dev_addr, v->vid);
320 		vg->num_vlans--;
321 	}
322 
323 out_filt:
324 	if (p) {
325 		__vlan_vid_del(dev, br, v);
326 		if (masterv) {
327 			if (v->stats && masterv->stats != v->stats)
328 				free_percpu(v->stats);
329 			v->stats = NULL;
330 
331 			br_vlan_put_master(masterv);
332 			v->brvlan = NULL;
333 		}
334 	} else {
335 		br_switchdev_port_vlan_del(dev, v->vid);
336 	}
337 
338 	goto out;
339 }
340 
341 static int __vlan_del(struct net_bridge_vlan *v)
342 {
343 	struct net_bridge_vlan *masterv = v;
344 	struct net_bridge_vlan_group *vg;
345 	struct net_bridge_port *p = NULL;
346 	int err = 0;
347 
348 	if (br_vlan_is_master(v)) {
349 		vg = br_vlan_group(v->br);
350 	} else {
351 		p = v->port;
352 		vg = nbp_vlan_group(v->port);
353 		masterv = v->brvlan;
354 	}
355 
356 	__vlan_delete_pvid(vg, v->vid);
357 	if (p) {
358 		err = __vlan_vid_del(p->dev, p->br, v);
359 		if (err)
360 			goto out;
361 	} else {
362 		err = br_switchdev_port_vlan_del(v->br->dev, v->vid);
363 		if (err && err != -EOPNOTSUPP)
364 			goto out;
365 		err = 0;
366 	}
367 
368 	if (br_vlan_should_use(v)) {
369 		v->flags &= ~BRIDGE_VLAN_INFO_BRENTRY;
370 		vg->num_vlans--;
371 	}
372 
373 	if (masterv != v) {
374 		vlan_tunnel_info_del(vg, v);
375 		rhashtable_remove_fast(&vg->vlan_hash, &v->vnode,
376 				       br_vlan_rht_params);
377 		__vlan_del_list(v);
378 		nbp_vlan_set_vlan_dev_state(p, v->vid);
379 		call_rcu(&v->rcu, nbp_vlan_rcu_free);
380 	}
381 
382 	br_vlan_put_master(masterv);
383 out:
384 	return err;
385 }
386 
387 static void __vlan_group_free(struct net_bridge_vlan_group *vg)
388 {
389 	WARN_ON(!list_empty(&vg->vlan_list));
390 	rhashtable_destroy(&vg->vlan_hash);
391 	vlan_tunnel_deinit(vg);
392 	kfree(vg);
393 }
394 
395 static void __vlan_flush(const struct net_bridge *br,
396 			 const struct net_bridge_port *p,
397 			 struct net_bridge_vlan_group *vg)
398 {
399 	struct net_bridge_vlan *vlan, *tmp;
400 	u16 v_start = 0, v_end = 0;
401 
402 	__vlan_delete_pvid(vg, vg->pvid);
403 	list_for_each_entry_safe(vlan, tmp, &vg->vlan_list, vlist) {
404 		/* take care of disjoint ranges */
405 		if (!v_start) {
406 			v_start = vlan->vid;
407 		} else if (vlan->vid - v_end != 1) {
408 			/* found range end, notify and start next one */
409 			br_vlan_notify(br, p, v_start, v_end, RTM_DELVLAN);
410 			v_start = vlan->vid;
411 		}
412 		v_end = vlan->vid;
413 
414 		__vlan_del(vlan);
415 	}
416 
417 	/* notify about the last/whole vlan range */
418 	if (v_start)
419 		br_vlan_notify(br, p, v_start, v_end, RTM_DELVLAN);
420 }
421 
422 struct sk_buff *br_handle_vlan(struct net_bridge *br,
423 			       const struct net_bridge_port *p,
424 			       struct net_bridge_vlan_group *vg,
425 			       struct sk_buff *skb)
426 {
427 	struct pcpu_sw_netstats *stats;
428 	struct net_bridge_vlan *v;
429 	u16 vid;
430 
431 	/* If this packet was not filtered at input, let it pass */
432 	if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
433 		goto out;
434 
435 	/* At this point, we know that the frame was filtered and contains
436 	 * a valid vlan id.  If the vlan id has untagged flag set,
437 	 * send untagged; otherwise, send tagged.
438 	 */
439 	br_vlan_get_tag(skb, &vid);
440 	v = br_vlan_find(vg, vid);
441 	/* Vlan entry must be configured at this point.  The
442 	 * only exception is the bridge is set in promisc mode and the
443 	 * packet is destined for the bridge device.  In this case
444 	 * pass the packet as is.
445 	 */
446 	if (!v || !br_vlan_should_use(v)) {
447 		if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) {
448 			goto out;
449 		} else {
450 			kfree_skb(skb);
451 			return NULL;
452 		}
453 	}
454 	if (br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) {
455 		stats = this_cpu_ptr(v->stats);
456 		u64_stats_update_begin(&stats->syncp);
457 		stats->tx_bytes += skb->len;
458 		stats->tx_packets++;
459 		u64_stats_update_end(&stats->syncp);
460 	}
461 
462 	if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
463 		__vlan_hwaccel_clear_tag(skb);
464 
465 	if (p && (p->flags & BR_VLAN_TUNNEL) &&
466 	    br_handle_egress_vlan_tunnel(skb, v)) {
467 		kfree_skb(skb);
468 		return NULL;
469 	}
470 out:
471 	return skb;
472 }
473 
474 /* Called under RCU */
475 static bool __allowed_ingress(const struct net_bridge *br,
476 			      struct net_bridge_vlan_group *vg,
477 			      struct sk_buff *skb, u16 *vid,
478 			      u8 *state)
479 {
480 	struct pcpu_sw_netstats *stats;
481 	struct net_bridge_vlan *v;
482 	bool tagged;
483 
484 	BR_INPUT_SKB_CB(skb)->vlan_filtered = true;
485 	/* If vlan tx offload is disabled on bridge device and frame was
486 	 * sent from vlan device on the bridge device, it does not have
487 	 * HW accelerated vlan tag.
488 	 */
489 	if (unlikely(!skb_vlan_tag_present(skb) &&
490 		     skb->protocol == br->vlan_proto)) {
491 		skb = skb_vlan_untag(skb);
492 		if (unlikely(!skb))
493 			return false;
494 	}
495 
496 	if (!br_vlan_get_tag(skb, vid)) {
497 		/* Tagged frame */
498 		if (skb->vlan_proto != br->vlan_proto) {
499 			/* Protocol-mismatch, empty out vlan_tci for new tag */
500 			skb_push(skb, ETH_HLEN);
501 			skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
502 							skb_vlan_tag_get(skb));
503 			if (unlikely(!skb))
504 				return false;
505 
506 			skb_pull(skb, ETH_HLEN);
507 			skb_reset_mac_len(skb);
508 			*vid = 0;
509 			tagged = false;
510 		} else {
511 			tagged = true;
512 		}
513 	} else {
514 		/* Untagged frame */
515 		tagged = false;
516 	}
517 
518 	if (!*vid) {
519 		u16 pvid = br_get_pvid(vg);
520 
521 		/* Frame had a tag with VID 0 or did not have a tag.
522 		 * See if pvid is set on this port.  That tells us which
523 		 * vlan untagged or priority-tagged traffic belongs to.
524 		 */
525 		if (!pvid)
526 			goto drop;
527 
528 		/* PVID is set on this port.  Any untagged or priority-tagged
529 		 * ingress frame is considered to belong to this vlan.
530 		 */
531 		*vid = pvid;
532 		if (likely(!tagged))
533 			/* Untagged Frame. */
534 			__vlan_hwaccel_put_tag(skb, br->vlan_proto, pvid);
535 		else
536 			/* Priority-tagged Frame.
537 			 * At this point, we know that skb->vlan_tci VID
538 			 * field was 0.
539 			 * We update only VID field and preserve PCP field.
540 			 */
541 			skb->vlan_tci |= pvid;
542 
543 		/* if stats are disabled we can avoid the lookup */
544 		if (!br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) {
545 			if (*state == BR_STATE_FORWARDING) {
546 				*state = br_vlan_get_pvid_state(vg);
547 				return br_vlan_state_allowed(*state, true);
548 			} else {
549 				return true;
550 			}
551 		}
552 	}
553 	v = br_vlan_find(vg, *vid);
554 	if (!v || !br_vlan_should_use(v))
555 		goto drop;
556 
557 	if (*state == BR_STATE_FORWARDING) {
558 		*state = br_vlan_get_state(v);
559 		if (!br_vlan_state_allowed(*state, true))
560 			goto drop;
561 	}
562 
563 	if (br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) {
564 		stats = this_cpu_ptr(v->stats);
565 		u64_stats_update_begin(&stats->syncp);
566 		stats->rx_bytes += skb->len;
567 		stats->rx_packets++;
568 		u64_stats_update_end(&stats->syncp);
569 	}
570 
571 	return true;
572 
573 drop:
574 	kfree_skb(skb);
575 	return false;
576 }
577 
578 bool br_allowed_ingress(const struct net_bridge *br,
579 			struct net_bridge_vlan_group *vg, struct sk_buff *skb,
580 			u16 *vid, u8 *state)
581 {
582 	/* If VLAN filtering is disabled on the bridge, all packets are
583 	 * permitted.
584 	 */
585 	if (!br_opt_get(br, BROPT_VLAN_ENABLED)) {
586 		BR_INPUT_SKB_CB(skb)->vlan_filtered = false;
587 		return true;
588 	}
589 
590 	return __allowed_ingress(br, vg, skb, vid, state);
591 }
592 
593 /* Called under RCU. */
594 bool br_allowed_egress(struct net_bridge_vlan_group *vg,
595 		       const struct sk_buff *skb)
596 {
597 	const struct net_bridge_vlan *v;
598 	u16 vid;
599 
600 	/* If this packet was not filtered at input, let it pass */
601 	if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
602 		return true;
603 
604 	br_vlan_get_tag(skb, &vid);
605 	v = br_vlan_find(vg, vid);
606 	if (v && br_vlan_should_use(v) &&
607 	    br_vlan_state_allowed(br_vlan_get_state(v), false))
608 		return true;
609 
610 	return false;
611 }
612 
613 /* Called under RCU */
614 bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid)
615 {
616 	struct net_bridge_vlan_group *vg;
617 	struct net_bridge *br = p->br;
618 	struct net_bridge_vlan *v;
619 
620 	/* If filtering was disabled at input, let it pass. */
621 	if (!br_opt_get(br, BROPT_VLAN_ENABLED))
622 		return true;
623 
624 	vg = nbp_vlan_group_rcu(p);
625 	if (!vg || !vg->num_vlans)
626 		return false;
627 
628 	if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto)
629 		*vid = 0;
630 
631 	if (!*vid) {
632 		*vid = br_get_pvid(vg);
633 		if (!*vid ||
634 		    !br_vlan_state_allowed(br_vlan_get_pvid_state(vg), true))
635 			return false;
636 
637 		return true;
638 	}
639 
640 	v = br_vlan_find(vg, *vid);
641 	if (v && br_vlan_state_allowed(br_vlan_get_state(v), true))
642 		return true;
643 
644 	return false;
645 }
646 
647 static int br_vlan_add_existing(struct net_bridge *br,
648 				struct net_bridge_vlan_group *vg,
649 				struct net_bridge_vlan *vlan,
650 				u16 flags, bool *changed,
651 				struct netlink_ext_ack *extack)
652 {
653 	int err;
654 
655 	err = br_switchdev_port_vlan_add(br->dev, vlan->vid, flags, extack);
656 	if (err && err != -EOPNOTSUPP)
657 		return err;
658 
659 	if (!br_vlan_is_brentry(vlan)) {
660 		/* Trying to change flags of non-existent bridge vlan */
661 		if (!(flags & BRIDGE_VLAN_INFO_BRENTRY)) {
662 			err = -EINVAL;
663 			goto err_flags;
664 		}
665 		/* It was only kept for port vlans, now make it real */
666 		err = br_fdb_insert(br, NULL, br->dev->dev_addr,
667 				    vlan->vid);
668 		if (err) {
669 			br_err(br, "failed to insert local address into bridge forwarding table\n");
670 			goto err_fdb_insert;
671 		}
672 
673 		refcount_inc(&vlan->refcnt);
674 		vlan->flags |= BRIDGE_VLAN_INFO_BRENTRY;
675 		vg->num_vlans++;
676 		*changed = true;
677 	}
678 
679 	if (__vlan_add_flags(vlan, flags))
680 		*changed = true;
681 
682 	return 0;
683 
684 err_fdb_insert:
685 err_flags:
686 	br_switchdev_port_vlan_del(br->dev, vlan->vid);
687 	return err;
688 }
689 
690 /* Must be protected by RTNL.
691  * Must be called with vid in range from 1 to 4094 inclusive.
692  * changed must be true only if the vlan was created or updated
693  */
694 int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags, bool *changed,
695 		struct netlink_ext_ack *extack)
696 {
697 	struct net_bridge_vlan_group *vg;
698 	struct net_bridge_vlan *vlan;
699 	int ret;
700 
701 	ASSERT_RTNL();
702 
703 	*changed = false;
704 	vg = br_vlan_group(br);
705 	vlan = br_vlan_find(vg, vid);
706 	if (vlan)
707 		return br_vlan_add_existing(br, vg, vlan, flags, changed,
708 					    extack);
709 
710 	vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
711 	if (!vlan)
712 		return -ENOMEM;
713 
714 	vlan->stats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
715 	if (!vlan->stats) {
716 		kfree(vlan);
717 		return -ENOMEM;
718 	}
719 	vlan->vid = vid;
720 	vlan->flags = flags | BRIDGE_VLAN_INFO_MASTER;
721 	vlan->flags &= ~BRIDGE_VLAN_INFO_PVID;
722 	vlan->br = br;
723 	if (flags & BRIDGE_VLAN_INFO_BRENTRY)
724 		refcount_set(&vlan->refcnt, 1);
725 	ret = __vlan_add(vlan, flags, extack);
726 	if (ret) {
727 		free_percpu(vlan->stats);
728 		kfree(vlan);
729 	} else {
730 		*changed = true;
731 	}
732 
733 	return ret;
734 }
735 
736 /* Must be protected by RTNL.
737  * Must be called with vid in range from 1 to 4094 inclusive.
738  */
739 int br_vlan_delete(struct net_bridge *br, u16 vid)
740 {
741 	struct net_bridge_vlan_group *vg;
742 	struct net_bridge_vlan *v;
743 
744 	ASSERT_RTNL();
745 
746 	vg = br_vlan_group(br);
747 	v = br_vlan_find(vg, vid);
748 	if (!v || !br_vlan_is_brentry(v))
749 		return -ENOENT;
750 
751 	br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
752 	br_fdb_delete_by_port(br, NULL, vid, 0);
753 
754 	vlan_tunnel_info_del(vg, v);
755 
756 	return __vlan_del(v);
757 }
758 
759 void br_vlan_flush(struct net_bridge *br)
760 {
761 	struct net_bridge_vlan_group *vg;
762 
763 	ASSERT_RTNL();
764 
765 	vg = br_vlan_group(br);
766 	__vlan_flush(br, NULL, vg);
767 	RCU_INIT_POINTER(br->vlgrp, NULL);
768 	synchronize_rcu();
769 	__vlan_group_free(vg);
770 }
771 
772 struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid)
773 {
774 	if (!vg)
775 		return NULL;
776 
777 	return br_vlan_lookup(&vg->vlan_hash, vid);
778 }
779 
780 /* Must be protected by RTNL. */
781 static void recalculate_group_addr(struct net_bridge *br)
782 {
783 	if (br_opt_get(br, BROPT_GROUP_ADDR_SET))
784 		return;
785 
786 	spin_lock_bh(&br->lock);
787 	if (!br_opt_get(br, BROPT_VLAN_ENABLED) ||
788 	    br->vlan_proto == htons(ETH_P_8021Q)) {
789 		/* Bridge Group Address */
790 		br->group_addr[5] = 0x00;
791 	} else { /* vlan_enabled && ETH_P_8021AD */
792 		/* Provider Bridge Group Address */
793 		br->group_addr[5] = 0x08;
794 	}
795 	spin_unlock_bh(&br->lock);
796 }
797 
798 /* Must be protected by RTNL. */
799 void br_recalculate_fwd_mask(struct net_bridge *br)
800 {
801 	if (!br_opt_get(br, BROPT_VLAN_ENABLED) ||
802 	    br->vlan_proto == htons(ETH_P_8021Q))
803 		br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT;
804 	else /* vlan_enabled && ETH_P_8021AD */
805 		br->group_fwd_mask_required = BR_GROUPFWD_8021AD &
806 					      ~(1u << br->group_addr[5]);
807 }
808 
809 int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
810 {
811 	struct switchdev_attr attr = {
812 		.orig_dev = br->dev,
813 		.id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
814 		.flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
815 		.u.vlan_filtering = val,
816 	};
817 	int err;
818 
819 	if (br_opt_get(br, BROPT_VLAN_ENABLED) == !!val)
820 		return 0;
821 
822 	err = switchdev_port_attr_set(br->dev, &attr);
823 	if (err && err != -EOPNOTSUPP)
824 		return err;
825 
826 	br_opt_toggle(br, BROPT_VLAN_ENABLED, !!val);
827 	br_manage_promisc(br);
828 	recalculate_group_addr(br);
829 	br_recalculate_fwd_mask(br);
830 
831 	return 0;
832 }
833 
834 int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
835 {
836 	return __br_vlan_filter_toggle(br, val);
837 }
838 
839 bool br_vlan_enabled(const struct net_device *dev)
840 {
841 	struct net_bridge *br = netdev_priv(dev);
842 
843 	return br_opt_get(br, BROPT_VLAN_ENABLED);
844 }
845 EXPORT_SYMBOL_GPL(br_vlan_enabled);
846 
847 int br_vlan_get_proto(const struct net_device *dev, u16 *p_proto)
848 {
849 	struct net_bridge *br = netdev_priv(dev);
850 
851 	*p_proto = ntohs(br->vlan_proto);
852 
853 	return 0;
854 }
855 EXPORT_SYMBOL_GPL(br_vlan_get_proto);
856 
857 int __br_vlan_set_proto(struct net_bridge *br, __be16 proto)
858 {
859 	struct switchdev_attr attr = {
860 		.orig_dev = br->dev,
861 		.id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_PROTOCOL,
862 		.flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
863 		.u.vlan_protocol = ntohs(proto),
864 	};
865 	int err = 0;
866 	struct net_bridge_port *p;
867 	struct net_bridge_vlan *vlan;
868 	struct net_bridge_vlan_group *vg;
869 	__be16 oldproto = br->vlan_proto;
870 
871 	if (br->vlan_proto == proto)
872 		return 0;
873 
874 	err = switchdev_port_attr_set(br->dev, &attr);
875 	if (err && err != -EOPNOTSUPP)
876 		return err;
877 
878 	/* Add VLANs for the new proto to the device filter. */
879 	list_for_each_entry(p, &br->port_list, list) {
880 		vg = nbp_vlan_group(p);
881 		list_for_each_entry(vlan, &vg->vlan_list, vlist) {
882 			err = vlan_vid_add(p->dev, proto, vlan->vid);
883 			if (err)
884 				goto err_filt;
885 		}
886 	}
887 
888 	br->vlan_proto = proto;
889 
890 	recalculate_group_addr(br);
891 	br_recalculate_fwd_mask(br);
892 
893 	/* Delete VLANs for the old proto from the device filter. */
894 	list_for_each_entry(p, &br->port_list, list) {
895 		vg = nbp_vlan_group(p);
896 		list_for_each_entry(vlan, &vg->vlan_list, vlist)
897 			vlan_vid_del(p->dev, oldproto, vlan->vid);
898 	}
899 
900 	return 0;
901 
902 err_filt:
903 	attr.u.vlan_protocol = ntohs(oldproto);
904 	switchdev_port_attr_set(br->dev, &attr);
905 
906 	list_for_each_entry_continue_reverse(vlan, &vg->vlan_list, vlist)
907 		vlan_vid_del(p->dev, proto, vlan->vid);
908 
909 	list_for_each_entry_continue_reverse(p, &br->port_list, list) {
910 		vg = nbp_vlan_group(p);
911 		list_for_each_entry(vlan, &vg->vlan_list, vlist)
912 			vlan_vid_del(p->dev, proto, vlan->vid);
913 	}
914 
915 	return err;
916 }
917 
918 int br_vlan_set_proto(struct net_bridge *br, unsigned long val)
919 {
920 	if (!eth_type_vlan(htons(val)))
921 		return -EPROTONOSUPPORT;
922 
923 	return __br_vlan_set_proto(br, htons(val));
924 }
925 
926 int br_vlan_set_stats(struct net_bridge *br, unsigned long val)
927 {
928 	switch (val) {
929 	case 0:
930 	case 1:
931 		br_opt_toggle(br, BROPT_VLAN_STATS_ENABLED, !!val);
932 		break;
933 	default:
934 		return -EINVAL;
935 	}
936 
937 	return 0;
938 }
939 
940 int br_vlan_set_stats_per_port(struct net_bridge *br, unsigned long val)
941 {
942 	struct net_bridge_port *p;
943 
944 	/* allow to change the option if there are no port vlans configured */
945 	list_for_each_entry(p, &br->port_list, list) {
946 		struct net_bridge_vlan_group *vg = nbp_vlan_group(p);
947 
948 		if (vg->num_vlans)
949 			return -EBUSY;
950 	}
951 
952 	switch (val) {
953 	case 0:
954 	case 1:
955 		br_opt_toggle(br, BROPT_VLAN_STATS_PER_PORT, !!val);
956 		break;
957 	default:
958 		return -EINVAL;
959 	}
960 
961 	return 0;
962 }
963 
964 static bool vlan_default_pvid(struct net_bridge_vlan_group *vg, u16 vid)
965 {
966 	struct net_bridge_vlan *v;
967 
968 	if (vid != vg->pvid)
969 		return false;
970 
971 	v = br_vlan_lookup(&vg->vlan_hash, vid);
972 	if (v && br_vlan_should_use(v) &&
973 	    (v->flags & BRIDGE_VLAN_INFO_UNTAGGED))
974 		return true;
975 
976 	return false;
977 }
978 
979 static void br_vlan_disable_default_pvid(struct net_bridge *br)
980 {
981 	struct net_bridge_port *p;
982 	u16 pvid = br->default_pvid;
983 
984 	/* Disable default_pvid on all ports where it is still
985 	 * configured.
986 	 */
987 	if (vlan_default_pvid(br_vlan_group(br), pvid)) {
988 		if (!br_vlan_delete(br, pvid))
989 			br_vlan_notify(br, NULL, pvid, 0, RTM_DELVLAN);
990 	}
991 
992 	list_for_each_entry(p, &br->port_list, list) {
993 		if (vlan_default_pvid(nbp_vlan_group(p), pvid) &&
994 		    !nbp_vlan_delete(p, pvid))
995 			br_vlan_notify(br, p, pvid, 0, RTM_DELVLAN);
996 	}
997 
998 	br->default_pvid = 0;
999 }
1000 
1001 int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid,
1002 			       struct netlink_ext_ack *extack)
1003 {
1004 	const struct net_bridge_vlan *pvent;
1005 	struct net_bridge_vlan_group *vg;
1006 	struct net_bridge_port *p;
1007 	unsigned long *changed;
1008 	bool vlchange;
1009 	u16 old_pvid;
1010 	int err = 0;
1011 
1012 	if (!pvid) {
1013 		br_vlan_disable_default_pvid(br);
1014 		return 0;
1015 	}
1016 
1017 	changed = bitmap_zalloc(BR_MAX_PORTS, GFP_KERNEL);
1018 	if (!changed)
1019 		return -ENOMEM;
1020 
1021 	old_pvid = br->default_pvid;
1022 
1023 	/* Update default_pvid config only if we do not conflict with
1024 	 * user configuration.
1025 	 */
1026 	vg = br_vlan_group(br);
1027 	pvent = br_vlan_find(vg, pvid);
1028 	if ((!old_pvid || vlan_default_pvid(vg, old_pvid)) &&
1029 	    (!pvent || !br_vlan_should_use(pvent))) {
1030 		err = br_vlan_add(br, pvid,
1031 				  BRIDGE_VLAN_INFO_PVID |
1032 				  BRIDGE_VLAN_INFO_UNTAGGED |
1033 				  BRIDGE_VLAN_INFO_BRENTRY,
1034 				  &vlchange, extack);
1035 		if (err)
1036 			goto out;
1037 
1038 		if (br_vlan_delete(br, old_pvid))
1039 			br_vlan_notify(br, NULL, old_pvid, 0, RTM_DELVLAN);
1040 		br_vlan_notify(br, NULL, pvid, 0, RTM_NEWVLAN);
1041 		set_bit(0, changed);
1042 	}
1043 
1044 	list_for_each_entry(p, &br->port_list, list) {
1045 		/* Update default_pvid config only if we do not conflict with
1046 		 * user configuration.
1047 		 */
1048 		vg = nbp_vlan_group(p);
1049 		if ((old_pvid &&
1050 		     !vlan_default_pvid(vg, old_pvid)) ||
1051 		    br_vlan_find(vg, pvid))
1052 			continue;
1053 
1054 		err = nbp_vlan_add(p, pvid,
1055 				   BRIDGE_VLAN_INFO_PVID |
1056 				   BRIDGE_VLAN_INFO_UNTAGGED,
1057 				   &vlchange, extack);
1058 		if (err)
1059 			goto err_port;
1060 		if (nbp_vlan_delete(p, old_pvid))
1061 			br_vlan_notify(br, p, old_pvid, 0, RTM_DELVLAN);
1062 		br_vlan_notify(p->br, p, pvid, 0, RTM_NEWVLAN);
1063 		set_bit(p->port_no, changed);
1064 	}
1065 
1066 	br->default_pvid = pvid;
1067 
1068 out:
1069 	bitmap_free(changed);
1070 	return err;
1071 
1072 err_port:
1073 	list_for_each_entry_continue_reverse(p, &br->port_list, list) {
1074 		if (!test_bit(p->port_no, changed))
1075 			continue;
1076 
1077 		if (old_pvid) {
1078 			nbp_vlan_add(p, old_pvid,
1079 				     BRIDGE_VLAN_INFO_PVID |
1080 				     BRIDGE_VLAN_INFO_UNTAGGED,
1081 				     &vlchange, NULL);
1082 			br_vlan_notify(p->br, p, old_pvid, 0, RTM_NEWVLAN);
1083 		}
1084 		nbp_vlan_delete(p, pvid);
1085 		br_vlan_notify(br, p, pvid, 0, RTM_DELVLAN);
1086 	}
1087 
1088 	if (test_bit(0, changed)) {
1089 		if (old_pvid) {
1090 			br_vlan_add(br, old_pvid,
1091 				    BRIDGE_VLAN_INFO_PVID |
1092 				    BRIDGE_VLAN_INFO_UNTAGGED |
1093 				    BRIDGE_VLAN_INFO_BRENTRY,
1094 				    &vlchange, NULL);
1095 			br_vlan_notify(br, NULL, old_pvid, 0, RTM_NEWVLAN);
1096 		}
1097 		br_vlan_delete(br, pvid);
1098 		br_vlan_notify(br, NULL, pvid, 0, RTM_DELVLAN);
1099 	}
1100 	goto out;
1101 }
1102 
1103 int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val)
1104 {
1105 	u16 pvid = val;
1106 	int err = 0;
1107 
1108 	if (val >= VLAN_VID_MASK)
1109 		return -EINVAL;
1110 
1111 	if (pvid == br->default_pvid)
1112 		goto out;
1113 
1114 	/* Only allow default pvid change when filtering is disabled */
1115 	if (br_opt_get(br, BROPT_VLAN_ENABLED)) {
1116 		pr_info_once("Please disable vlan filtering to change default_pvid\n");
1117 		err = -EPERM;
1118 		goto out;
1119 	}
1120 	err = __br_vlan_set_default_pvid(br, pvid, NULL);
1121 out:
1122 	return err;
1123 }
1124 
1125 int br_vlan_init(struct net_bridge *br)
1126 {
1127 	struct net_bridge_vlan_group *vg;
1128 	int ret = -ENOMEM;
1129 
1130 	vg = kzalloc(sizeof(*vg), GFP_KERNEL);
1131 	if (!vg)
1132 		goto out;
1133 	ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
1134 	if (ret)
1135 		goto err_rhtbl;
1136 	ret = vlan_tunnel_init(vg);
1137 	if (ret)
1138 		goto err_tunnel_init;
1139 	INIT_LIST_HEAD(&vg->vlan_list);
1140 	br->vlan_proto = htons(ETH_P_8021Q);
1141 	br->default_pvid = 1;
1142 	rcu_assign_pointer(br->vlgrp, vg);
1143 
1144 out:
1145 	return ret;
1146 
1147 err_tunnel_init:
1148 	rhashtable_destroy(&vg->vlan_hash);
1149 err_rhtbl:
1150 	kfree(vg);
1151 
1152 	goto out;
1153 }
1154 
1155 int nbp_vlan_init(struct net_bridge_port *p, struct netlink_ext_ack *extack)
1156 {
1157 	struct switchdev_attr attr = {
1158 		.orig_dev = p->br->dev,
1159 		.id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
1160 		.flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
1161 		.u.vlan_filtering = br_opt_get(p->br, BROPT_VLAN_ENABLED),
1162 	};
1163 	struct net_bridge_vlan_group *vg;
1164 	int ret = -ENOMEM;
1165 
1166 	vg = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL);
1167 	if (!vg)
1168 		goto out;
1169 
1170 	ret = switchdev_port_attr_set(p->dev, &attr);
1171 	if (ret && ret != -EOPNOTSUPP)
1172 		goto err_vlan_enabled;
1173 
1174 	ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
1175 	if (ret)
1176 		goto err_rhtbl;
1177 	ret = vlan_tunnel_init(vg);
1178 	if (ret)
1179 		goto err_tunnel_init;
1180 	INIT_LIST_HEAD(&vg->vlan_list);
1181 	rcu_assign_pointer(p->vlgrp, vg);
1182 	if (p->br->default_pvid) {
1183 		bool changed;
1184 
1185 		ret = nbp_vlan_add(p, p->br->default_pvid,
1186 				   BRIDGE_VLAN_INFO_PVID |
1187 				   BRIDGE_VLAN_INFO_UNTAGGED,
1188 				   &changed, extack);
1189 		if (ret)
1190 			goto err_vlan_add;
1191 		br_vlan_notify(p->br, p, p->br->default_pvid, 0, RTM_NEWVLAN);
1192 	}
1193 out:
1194 	return ret;
1195 
1196 err_vlan_add:
1197 	RCU_INIT_POINTER(p->vlgrp, NULL);
1198 	synchronize_rcu();
1199 	vlan_tunnel_deinit(vg);
1200 err_tunnel_init:
1201 	rhashtable_destroy(&vg->vlan_hash);
1202 err_rhtbl:
1203 err_vlan_enabled:
1204 	kfree(vg);
1205 
1206 	goto out;
1207 }
1208 
1209 /* Must be protected by RTNL.
1210  * Must be called with vid in range from 1 to 4094 inclusive.
1211  * changed must be true only if the vlan was created or updated
1212  */
1213 int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags,
1214 		 bool *changed, struct netlink_ext_ack *extack)
1215 {
1216 	struct net_bridge_vlan *vlan;
1217 	int ret;
1218 
1219 	ASSERT_RTNL();
1220 
1221 	*changed = false;
1222 	vlan = br_vlan_find(nbp_vlan_group(port), vid);
1223 	if (vlan) {
1224 		/* Pass the flags to the hardware bridge */
1225 		ret = br_switchdev_port_vlan_add(port->dev, vid, flags, extack);
1226 		if (ret && ret != -EOPNOTSUPP)
1227 			return ret;
1228 		*changed = __vlan_add_flags(vlan, flags);
1229 
1230 		return 0;
1231 	}
1232 
1233 	vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
1234 	if (!vlan)
1235 		return -ENOMEM;
1236 
1237 	vlan->vid = vid;
1238 	vlan->port = port;
1239 	ret = __vlan_add(vlan, flags, extack);
1240 	if (ret)
1241 		kfree(vlan);
1242 	else
1243 		*changed = true;
1244 
1245 	return ret;
1246 }
1247 
1248 /* Must be protected by RTNL.
1249  * Must be called with vid in range from 1 to 4094 inclusive.
1250  */
1251 int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
1252 {
1253 	struct net_bridge_vlan *v;
1254 
1255 	ASSERT_RTNL();
1256 
1257 	v = br_vlan_find(nbp_vlan_group(port), vid);
1258 	if (!v)
1259 		return -ENOENT;
1260 	br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
1261 	br_fdb_delete_by_port(port->br, port, vid, 0);
1262 
1263 	return __vlan_del(v);
1264 }
1265 
1266 void nbp_vlan_flush(struct net_bridge_port *port)
1267 {
1268 	struct net_bridge_vlan_group *vg;
1269 
1270 	ASSERT_RTNL();
1271 
1272 	vg = nbp_vlan_group(port);
1273 	__vlan_flush(port->br, port, vg);
1274 	RCU_INIT_POINTER(port->vlgrp, NULL);
1275 	synchronize_rcu();
1276 	__vlan_group_free(vg);
1277 }
1278 
1279 void br_vlan_get_stats(const struct net_bridge_vlan *v,
1280 		       struct pcpu_sw_netstats *stats)
1281 {
1282 	int i;
1283 
1284 	memset(stats, 0, sizeof(*stats));
1285 	for_each_possible_cpu(i) {
1286 		u64 rxpackets, rxbytes, txpackets, txbytes;
1287 		struct pcpu_sw_netstats *cpu_stats;
1288 		unsigned int start;
1289 
1290 		cpu_stats = per_cpu_ptr(v->stats, i);
1291 		do {
1292 			start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
1293 			rxpackets = cpu_stats->rx_packets;
1294 			rxbytes = cpu_stats->rx_bytes;
1295 			txbytes = cpu_stats->tx_bytes;
1296 			txpackets = cpu_stats->tx_packets;
1297 		} while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
1298 
1299 		stats->rx_packets += rxpackets;
1300 		stats->rx_bytes += rxbytes;
1301 		stats->tx_bytes += txbytes;
1302 		stats->tx_packets += txpackets;
1303 	}
1304 }
1305 
1306 int br_vlan_get_pvid(const struct net_device *dev, u16 *p_pvid)
1307 {
1308 	struct net_bridge_vlan_group *vg;
1309 	struct net_bridge_port *p;
1310 
1311 	ASSERT_RTNL();
1312 	p = br_port_get_check_rtnl(dev);
1313 	if (p)
1314 		vg = nbp_vlan_group(p);
1315 	else if (netif_is_bridge_master(dev))
1316 		vg = br_vlan_group(netdev_priv(dev));
1317 	else
1318 		return -EINVAL;
1319 
1320 	*p_pvid = br_get_pvid(vg);
1321 	return 0;
1322 }
1323 EXPORT_SYMBOL_GPL(br_vlan_get_pvid);
1324 
1325 int br_vlan_get_pvid_rcu(const struct net_device *dev, u16 *p_pvid)
1326 {
1327 	struct net_bridge_vlan_group *vg;
1328 	struct net_bridge_port *p;
1329 
1330 	p = br_port_get_check_rcu(dev);
1331 	if (p)
1332 		vg = nbp_vlan_group_rcu(p);
1333 	else if (netif_is_bridge_master(dev))
1334 		vg = br_vlan_group_rcu(netdev_priv(dev));
1335 	else
1336 		return -EINVAL;
1337 
1338 	*p_pvid = br_get_pvid(vg);
1339 	return 0;
1340 }
1341 EXPORT_SYMBOL_GPL(br_vlan_get_pvid_rcu);
1342 
1343 int br_vlan_get_info(const struct net_device *dev, u16 vid,
1344 		     struct bridge_vlan_info *p_vinfo)
1345 {
1346 	struct net_bridge_vlan_group *vg;
1347 	struct net_bridge_vlan *v;
1348 	struct net_bridge_port *p;
1349 
1350 	ASSERT_RTNL();
1351 	p = br_port_get_check_rtnl(dev);
1352 	if (p)
1353 		vg = nbp_vlan_group(p);
1354 	else if (netif_is_bridge_master(dev))
1355 		vg = br_vlan_group(netdev_priv(dev));
1356 	else
1357 		return -EINVAL;
1358 
1359 	v = br_vlan_find(vg, vid);
1360 	if (!v)
1361 		return -ENOENT;
1362 
1363 	p_vinfo->vid = vid;
1364 	p_vinfo->flags = v->flags;
1365 	if (vid == br_get_pvid(vg))
1366 		p_vinfo->flags |= BRIDGE_VLAN_INFO_PVID;
1367 	return 0;
1368 }
1369 EXPORT_SYMBOL_GPL(br_vlan_get_info);
1370 
1371 static int br_vlan_is_bind_vlan_dev(const struct net_device *dev)
1372 {
1373 	return is_vlan_dev(dev) &&
1374 		!!(vlan_dev_priv(dev)->flags & VLAN_FLAG_BRIDGE_BINDING);
1375 }
1376 
1377 static int br_vlan_is_bind_vlan_dev_fn(struct net_device *dev,
1378 			       __always_unused struct netdev_nested_priv *priv)
1379 {
1380 	return br_vlan_is_bind_vlan_dev(dev);
1381 }
1382 
1383 static bool br_vlan_has_upper_bind_vlan_dev(struct net_device *dev)
1384 {
1385 	int found;
1386 
1387 	rcu_read_lock();
1388 	found = netdev_walk_all_upper_dev_rcu(dev, br_vlan_is_bind_vlan_dev_fn,
1389 					      NULL);
1390 	rcu_read_unlock();
1391 
1392 	return !!found;
1393 }
1394 
1395 struct br_vlan_bind_walk_data {
1396 	u16 vid;
1397 	struct net_device *result;
1398 };
1399 
1400 static int br_vlan_match_bind_vlan_dev_fn(struct net_device *dev,
1401 					  struct netdev_nested_priv *priv)
1402 {
1403 	struct br_vlan_bind_walk_data *data = priv->data;
1404 	int found = 0;
1405 
1406 	if (br_vlan_is_bind_vlan_dev(dev) &&
1407 	    vlan_dev_priv(dev)->vlan_id == data->vid) {
1408 		data->result = dev;
1409 		found = 1;
1410 	}
1411 
1412 	return found;
1413 }
1414 
1415 static struct net_device *
1416 br_vlan_get_upper_bind_vlan_dev(struct net_device *dev, u16 vid)
1417 {
1418 	struct br_vlan_bind_walk_data data = {
1419 		.vid = vid,
1420 	};
1421 	struct netdev_nested_priv priv = {
1422 		.data = (void *)&data,
1423 	};
1424 
1425 	rcu_read_lock();
1426 	netdev_walk_all_upper_dev_rcu(dev, br_vlan_match_bind_vlan_dev_fn,
1427 				      &priv);
1428 	rcu_read_unlock();
1429 
1430 	return data.result;
1431 }
1432 
1433 static bool br_vlan_is_dev_up(const struct net_device *dev)
1434 {
1435 	return  !!(dev->flags & IFF_UP) && netif_oper_up(dev);
1436 }
1437 
1438 static void br_vlan_set_vlan_dev_state(const struct net_bridge *br,
1439 				       struct net_device *vlan_dev)
1440 {
1441 	u16 vid = vlan_dev_priv(vlan_dev)->vlan_id;
1442 	struct net_bridge_vlan_group *vg;
1443 	struct net_bridge_port *p;
1444 	bool has_carrier = false;
1445 
1446 	if (!netif_carrier_ok(br->dev)) {
1447 		netif_carrier_off(vlan_dev);
1448 		return;
1449 	}
1450 
1451 	list_for_each_entry(p, &br->port_list, list) {
1452 		vg = nbp_vlan_group(p);
1453 		if (br_vlan_find(vg, vid) && br_vlan_is_dev_up(p->dev)) {
1454 			has_carrier = true;
1455 			break;
1456 		}
1457 	}
1458 
1459 	if (has_carrier)
1460 		netif_carrier_on(vlan_dev);
1461 	else
1462 		netif_carrier_off(vlan_dev);
1463 }
1464 
1465 static void br_vlan_set_all_vlan_dev_state(struct net_bridge_port *p)
1466 {
1467 	struct net_bridge_vlan_group *vg = nbp_vlan_group(p);
1468 	struct net_bridge_vlan *vlan;
1469 	struct net_device *vlan_dev;
1470 
1471 	list_for_each_entry(vlan, &vg->vlan_list, vlist) {
1472 		vlan_dev = br_vlan_get_upper_bind_vlan_dev(p->br->dev,
1473 							   vlan->vid);
1474 		if (vlan_dev) {
1475 			if (br_vlan_is_dev_up(p->dev)) {
1476 				if (netif_carrier_ok(p->br->dev))
1477 					netif_carrier_on(vlan_dev);
1478 			} else {
1479 				br_vlan_set_vlan_dev_state(p->br, vlan_dev);
1480 			}
1481 		}
1482 	}
1483 }
1484 
1485 static void br_vlan_upper_change(struct net_device *dev,
1486 				 struct net_device *upper_dev,
1487 				 bool linking)
1488 {
1489 	struct net_bridge *br = netdev_priv(dev);
1490 
1491 	if (!br_vlan_is_bind_vlan_dev(upper_dev))
1492 		return;
1493 
1494 	if (linking) {
1495 		br_vlan_set_vlan_dev_state(br, upper_dev);
1496 		br_opt_toggle(br, BROPT_VLAN_BRIDGE_BINDING, true);
1497 	} else {
1498 		br_opt_toggle(br, BROPT_VLAN_BRIDGE_BINDING,
1499 			      br_vlan_has_upper_bind_vlan_dev(dev));
1500 	}
1501 }
1502 
1503 struct br_vlan_link_state_walk_data {
1504 	struct net_bridge *br;
1505 };
1506 
1507 static int br_vlan_link_state_change_fn(struct net_device *vlan_dev,
1508 					struct netdev_nested_priv *priv)
1509 {
1510 	struct br_vlan_link_state_walk_data *data = priv->data;
1511 
1512 	if (br_vlan_is_bind_vlan_dev(vlan_dev))
1513 		br_vlan_set_vlan_dev_state(data->br, vlan_dev);
1514 
1515 	return 0;
1516 }
1517 
1518 static void br_vlan_link_state_change(struct net_device *dev,
1519 				      struct net_bridge *br)
1520 {
1521 	struct br_vlan_link_state_walk_data data = {
1522 		.br = br
1523 	};
1524 	struct netdev_nested_priv priv = {
1525 		.data = (void *)&data,
1526 	};
1527 
1528 	rcu_read_lock();
1529 	netdev_walk_all_upper_dev_rcu(dev, br_vlan_link_state_change_fn,
1530 				      &priv);
1531 	rcu_read_unlock();
1532 }
1533 
1534 /* Must be protected by RTNL. */
1535 static void nbp_vlan_set_vlan_dev_state(struct net_bridge_port *p, u16 vid)
1536 {
1537 	struct net_device *vlan_dev;
1538 
1539 	if (!br_opt_get(p->br, BROPT_VLAN_BRIDGE_BINDING))
1540 		return;
1541 
1542 	vlan_dev = br_vlan_get_upper_bind_vlan_dev(p->br->dev, vid);
1543 	if (vlan_dev)
1544 		br_vlan_set_vlan_dev_state(p->br, vlan_dev);
1545 }
1546 
1547 /* Must be protected by RTNL. */
1548 int br_vlan_bridge_event(struct net_device *dev, unsigned long event, void *ptr)
1549 {
1550 	struct netdev_notifier_changeupper_info *info;
1551 	struct net_bridge *br = netdev_priv(dev);
1552 	int vlcmd = 0, ret = 0;
1553 	bool changed = false;
1554 
1555 	switch (event) {
1556 	case NETDEV_REGISTER:
1557 		ret = br_vlan_add(br, br->default_pvid,
1558 				  BRIDGE_VLAN_INFO_PVID |
1559 				  BRIDGE_VLAN_INFO_UNTAGGED |
1560 				  BRIDGE_VLAN_INFO_BRENTRY, &changed, NULL);
1561 		vlcmd = RTM_NEWVLAN;
1562 		break;
1563 	case NETDEV_UNREGISTER:
1564 		changed = !br_vlan_delete(br, br->default_pvid);
1565 		vlcmd = RTM_DELVLAN;
1566 		break;
1567 	case NETDEV_CHANGEUPPER:
1568 		info = ptr;
1569 		br_vlan_upper_change(dev, info->upper_dev, info->linking);
1570 		break;
1571 
1572 	case NETDEV_CHANGE:
1573 	case NETDEV_UP:
1574 		if (!br_opt_get(br, BROPT_VLAN_BRIDGE_BINDING))
1575 			break;
1576 		br_vlan_link_state_change(dev, br);
1577 		break;
1578 	}
1579 	if (changed)
1580 		br_vlan_notify(br, NULL, br->default_pvid, 0, vlcmd);
1581 
1582 	return ret;
1583 }
1584 
1585 /* Must be protected by RTNL. */
1586 void br_vlan_port_event(struct net_bridge_port *p, unsigned long event)
1587 {
1588 	if (!br_opt_get(p->br, BROPT_VLAN_BRIDGE_BINDING))
1589 		return;
1590 
1591 	switch (event) {
1592 	case NETDEV_CHANGE:
1593 	case NETDEV_DOWN:
1594 	case NETDEV_UP:
1595 		br_vlan_set_all_vlan_dev_state(p);
1596 		break;
1597 	}
1598 }
1599 
1600 static bool br_vlan_stats_fill(struct sk_buff *skb,
1601 			       const struct net_bridge_vlan *v)
1602 {
1603 	struct pcpu_sw_netstats stats;
1604 	struct nlattr *nest;
1605 
1606 	nest = nla_nest_start(skb, BRIDGE_VLANDB_ENTRY_STATS);
1607 	if (!nest)
1608 		return false;
1609 
1610 	br_vlan_get_stats(v, &stats);
1611 	if (nla_put_u64_64bit(skb, BRIDGE_VLANDB_STATS_RX_BYTES, stats.rx_bytes,
1612 			      BRIDGE_VLANDB_STATS_PAD) ||
1613 	    nla_put_u64_64bit(skb, BRIDGE_VLANDB_STATS_RX_PACKETS,
1614 			      stats.rx_packets, BRIDGE_VLANDB_STATS_PAD) ||
1615 	    nla_put_u64_64bit(skb, BRIDGE_VLANDB_STATS_TX_BYTES, stats.tx_bytes,
1616 			      BRIDGE_VLANDB_STATS_PAD) ||
1617 	    nla_put_u64_64bit(skb, BRIDGE_VLANDB_STATS_TX_PACKETS,
1618 			      stats.tx_packets, BRIDGE_VLANDB_STATS_PAD))
1619 		goto out_err;
1620 
1621 	nla_nest_end(skb, nest);
1622 
1623 	return true;
1624 
1625 out_err:
1626 	nla_nest_cancel(skb, nest);
1627 	return false;
1628 }
1629 
1630 /* v_opts is used to dump the options which must be equal in the whole range */
1631 static bool br_vlan_fill_vids(struct sk_buff *skb, u16 vid, u16 vid_range,
1632 			      const struct net_bridge_vlan *v_opts,
1633 			      u16 flags,
1634 			      bool dump_stats)
1635 {
1636 	struct bridge_vlan_info info;
1637 	struct nlattr *nest;
1638 
1639 	nest = nla_nest_start(skb, BRIDGE_VLANDB_ENTRY);
1640 	if (!nest)
1641 		return false;
1642 
1643 	memset(&info, 0, sizeof(info));
1644 	info.vid = vid;
1645 	if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
1646 		info.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1647 	if (flags & BRIDGE_VLAN_INFO_PVID)
1648 		info.flags |= BRIDGE_VLAN_INFO_PVID;
1649 
1650 	if (nla_put(skb, BRIDGE_VLANDB_ENTRY_INFO, sizeof(info), &info))
1651 		goto out_err;
1652 
1653 	if (vid_range && vid < vid_range &&
1654 	    !(flags & BRIDGE_VLAN_INFO_PVID) &&
1655 	    nla_put_u16(skb, BRIDGE_VLANDB_ENTRY_RANGE, vid_range))
1656 		goto out_err;
1657 
1658 	if (v_opts) {
1659 		if (!br_vlan_opts_fill(skb, v_opts))
1660 			goto out_err;
1661 
1662 		if (dump_stats && !br_vlan_stats_fill(skb, v_opts))
1663 			goto out_err;
1664 	}
1665 
1666 	nla_nest_end(skb, nest);
1667 
1668 	return true;
1669 
1670 out_err:
1671 	nla_nest_cancel(skb, nest);
1672 	return false;
1673 }
1674 
1675 static size_t rtnl_vlan_nlmsg_size(void)
1676 {
1677 	return NLMSG_ALIGN(sizeof(struct br_vlan_msg))
1678 		+ nla_total_size(0) /* BRIDGE_VLANDB_ENTRY */
1679 		+ nla_total_size(sizeof(u16)) /* BRIDGE_VLANDB_ENTRY_RANGE */
1680 		+ nla_total_size(sizeof(struct bridge_vlan_info)) /* BRIDGE_VLANDB_ENTRY_INFO */
1681 		+ br_vlan_opts_nl_size(); /* bridge vlan options */
1682 }
1683 
1684 void br_vlan_notify(const struct net_bridge *br,
1685 		    const struct net_bridge_port *p,
1686 		    u16 vid, u16 vid_range,
1687 		    int cmd)
1688 {
1689 	struct net_bridge_vlan_group *vg;
1690 	struct net_bridge_vlan *v = NULL;
1691 	struct br_vlan_msg *bvm;
1692 	struct nlmsghdr *nlh;
1693 	struct sk_buff *skb;
1694 	int err = -ENOBUFS;
1695 	struct net *net;
1696 	u16 flags = 0;
1697 	int ifindex;
1698 
1699 	/* right now notifications are done only with rtnl held */
1700 	ASSERT_RTNL();
1701 
1702 	if (p) {
1703 		ifindex = p->dev->ifindex;
1704 		vg = nbp_vlan_group(p);
1705 		net = dev_net(p->dev);
1706 	} else {
1707 		ifindex = br->dev->ifindex;
1708 		vg = br_vlan_group(br);
1709 		net = dev_net(br->dev);
1710 	}
1711 
1712 	skb = nlmsg_new(rtnl_vlan_nlmsg_size(), GFP_KERNEL);
1713 	if (!skb)
1714 		goto out_err;
1715 
1716 	err = -EMSGSIZE;
1717 	nlh = nlmsg_put(skb, 0, 0, cmd, sizeof(*bvm), 0);
1718 	if (!nlh)
1719 		goto out_err;
1720 	bvm = nlmsg_data(nlh);
1721 	memset(bvm, 0, sizeof(*bvm));
1722 	bvm->family = AF_BRIDGE;
1723 	bvm->ifindex = ifindex;
1724 
1725 	switch (cmd) {
1726 	case RTM_NEWVLAN:
1727 		/* need to find the vlan due to flags/options */
1728 		v = br_vlan_find(vg, vid);
1729 		if (!v || !br_vlan_should_use(v))
1730 			goto out_kfree;
1731 
1732 		flags = v->flags;
1733 		if (br_get_pvid(vg) == v->vid)
1734 			flags |= BRIDGE_VLAN_INFO_PVID;
1735 		break;
1736 	case RTM_DELVLAN:
1737 		break;
1738 	default:
1739 		goto out_kfree;
1740 	}
1741 
1742 	if (!br_vlan_fill_vids(skb, vid, vid_range, v, flags, false))
1743 		goto out_err;
1744 
1745 	nlmsg_end(skb, nlh);
1746 	rtnl_notify(skb, net, 0, RTNLGRP_BRVLAN, NULL, GFP_KERNEL);
1747 	return;
1748 
1749 out_err:
1750 	rtnl_set_sk_err(net, RTNLGRP_BRVLAN, err);
1751 out_kfree:
1752 	kfree_skb(skb);
1753 }
1754 
1755 /* check if v_curr can enter a range ending in range_end */
1756 bool br_vlan_can_enter_range(const struct net_bridge_vlan *v_curr,
1757 			     const struct net_bridge_vlan *range_end)
1758 {
1759 	return v_curr->vid - range_end->vid == 1 &&
1760 	       range_end->flags == v_curr->flags &&
1761 	       br_vlan_opts_eq_range(v_curr, range_end);
1762 }
1763 
1764 static int br_vlan_dump_dev(const struct net_device *dev,
1765 			    struct sk_buff *skb,
1766 			    struct netlink_callback *cb,
1767 			    u32 dump_flags)
1768 {
1769 	struct net_bridge_vlan *v, *range_start = NULL, *range_end = NULL;
1770 	bool dump_stats = !!(dump_flags & BRIDGE_VLANDB_DUMPF_STATS);
1771 	struct net_bridge_vlan_group *vg;
1772 	int idx = 0, s_idx = cb->args[1];
1773 	struct nlmsghdr *nlh = NULL;
1774 	struct net_bridge_port *p;
1775 	struct br_vlan_msg *bvm;
1776 	struct net_bridge *br;
1777 	int err = 0;
1778 	u16 pvid;
1779 
1780 	if (!netif_is_bridge_master(dev) && !netif_is_bridge_port(dev))
1781 		return -EINVAL;
1782 
1783 	if (netif_is_bridge_master(dev)) {
1784 		br = netdev_priv(dev);
1785 		vg = br_vlan_group_rcu(br);
1786 		p = NULL;
1787 	} else {
1788 		p = br_port_get_rcu(dev);
1789 		if (WARN_ON(!p))
1790 			return -EINVAL;
1791 		vg = nbp_vlan_group_rcu(p);
1792 		br = p->br;
1793 	}
1794 
1795 	if (!vg)
1796 		return 0;
1797 
1798 	nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1799 			RTM_NEWVLAN, sizeof(*bvm), NLM_F_MULTI);
1800 	if (!nlh)
1801 		return -EMSGSIZE;
1802 	bvm = nlmsg_data(nlh);
1803 	memset(bvm, 0, sizeof(*bvm));
1804 	bvm->family = PF_BRIDGE;
1805 	bvm->ifindex = dev->ifindex;
1806 	pvid = br_get_pvid(vg);
1807 
1808 	/* idx must stay at range's beginning until it is filled in */
1809 	list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
1810 		if (!br_vlan_should_use(v))
1811 			continue;
1812 		if (idx < s_idx) {
1813 			idx++;
1814 			continue;
1815 		}
1816 
1817 		if (!range_start) {
1818 			range_start = v;
1819 			range_end = v;
1820 			continue;
1821 		}
1822 
1823 		if (dump_stats || v->vid == pvid ||
1824 		    !br_vlan_can_enter_range(v, range_end)) {
1825 			u16 vlan_flags = br_vlan_flags(range_start, pvid);
1826 
1827 			if (!br_vlan_fill_vids(skb, range_start->vid,
1828 					       range_end->vid, range_start,
1829 					       vlan_flags, dump_stats)) {
1830 				err = -EMSGSIZE;
1831 				break;
1832 			}
1833 			/* advance number of filled vlans */
1834 			idx += range_end->vid - range_start->vid + 1;
1835 
1836 			range_start = v;
1837 		}
1838 		range_end = v;
1839 	}
1840 
1841 	/* err will be 0 and range_start will be set in 3 cases here:
1842 	 * - first vlan (range_start == range_end)
1843 	 * - last vlan (range_start == range_end, not in range)
1844 	 * - last vlan range (range_start != range_end, in range)
1845 	 */
1846 	if (!err && range_start &&
1847 	    !br_vlan_fill_vids(skb, range_start->vid, range_end->vid,
1848 			       range_start, br_vlan_flags(range_start, pvid),
1849 			       dump_stats))
1850 		err = -EMSGSIZE;
1851 
1852 	cb->args[1] = err ? idx : 0;
1853 
1854 	nlmsg_end(skb, nlh);
1855 
1856 	return err;
1857 }
1858 
1859 static const struct nla_policy br_vlan_db_dump_pol[BRIDGE_VLANDB_DUMP_MAX + 1] = {
1860 	[BRIDGE_VLANDB_DUMP_FLAGS] = { .type = NLA_U32 },
1861 };
1862 
1863 static int br_vlan_rtm_dump(struct sk_buff *skb, struct netlink_callback *cb)
1864 {
1865 	struct nlattr *dtb[BRIDGE_VLANDB_DUMP_MAX + 1];
1866 	int idx = 0, err = 0, s_idx = cb->args[0];
1867 	struct net *net = sock_net(skb->sk);
1868 	struct br_vlan_msg *bvm;
1869 	struct net_device *dev;
1870 	u32 dump_flags = 0;
1871 
1872 	err = nlmsg_parse(cb->nlh, sizeof(*bvm), dtb, BRIDGE_VLANDB_DUMP_MAX,
1873 			  br_vlan_db_dump_pol, cb->extack);
1874 	if (err < 0)
1875 		return err;
1876 
1877 	bvm = nlmsg_data(cb->nlh);
1878 	if (dtb[BRIDGE_VLANDB_DUMP_FLAGS])
1879 		dump_flags = nla_get_u32(dtb[BRIDGE_VLANDB_DUMP_FLAGS]);
1880 
1881 	rcu_read_lock();
1882 	if (bvm->ifindex) {
1883 		dev = dev_get_by_index_rcu(net, bvm->ifindex);
1884 		if (!dev) {
1885 			err = -ENODEV;
1886 			goto out_err;
1887 		}
1888 		err = br_vlan_dump_dev(dev, skb, cb, dump_flags);
1889 		if (err && err != -EMSGSIZE)
1890 			goto out_err;
1891 	} else {
1892 		for_each_netdev_rcu(net, dev) {
1893 			if (idx < s_idx)
1894 				goto skip;
1895 
1896 			err = br_vlan_dump_dev(dev, skb, cb, dump_flags);
1897 			if (err == -EMSGSIZE)
1898 				break;
1899 skip:
1900 			idx++;
1901 		}
1902 	}
1903 	cb->args[0] = idx;
1904 	rcu_read_unlock();
1905 
1906 	return skb->len;
1907 
1908 out_err:
1909 	rcu_read_unlock();
1910 
1911 	return err;
1912 }
1913 
1914 static const struct nla_policy br_vlan_db_policy[BRIDGE_VLANDB_ENTRY_MAX + 1] = {
1915 	[BRIDGE_VLANDB_ENTRY_INFO]	=
1916 		NLA_POLICY_EXACT_LEN(sizeof(struct bridge_vlan_info)),
1917 	[BRIDGE_VLANDB_ENTRY_RANGE]	= { .type = NLA_U16 },
1918 	[BRIDGE_VLANDB_ENTRY_STATE]	= { .type = NLA_U8 },
1919 	[BRIDGE_VLANDB_ENTRY_TUNNEL_INFO] = { .type = NLA_NESTED },
1920 };
1921 
1922 static int br_vlan_rtm_process_one(struct net_device *dev,
1923 				   const struct nlattr *attr,
1924 				   int cmd, struct netlink_ext_ack *extack)
1925 {
1926 	struct bridge_vlan_info *vinfo, vrange_end, *vinfo_last = NULL;
1927 	struct nlattr *tb[BRIDGE_VLANDB_ENTRY_MAX + 1];
1928 	bool changed = false, skip_processing = false;
1929 	struct net_bridge_vlan_group *vg;
1930 	struct net_bridge_port *p = NULL;
1931 	int err = 0, cmdmap = 0;
1932 	struct net_bridge *br;
1933 
1934 	if (netif_is_bridge_master(dev)) {
1935 		br = netdev_priv(dev);
1936 		vg = br_vlan_group(br);
1937 	} else {
1938 		p = br_port_get_rtnl(dev);
1939 		if (WARN_ON(!p))
1940 			return -ENODEV;
1941 		br = p->br;
1942 		vg = nbp_vlan_group(p);
1943 	}
1944 
1945 	if (WARN_ON(!vg))
1946 		return -ENODEV;
1947 
1948 	err = nla_parse_nested(tb, BRIDGE_VLANDB_ENTRY_MAX, attr,
1949 			       br_vlan_db_policy, extack);
1950 	if (err)
1951 		return err;
1952 
1953 	if (!tb[BRIDGE_VLANDB_ENTRY_INFO]) {
1954 		NL_SET_ERR_MSG_MOD(extack, "Missing vlan entry info");
1955 		return -EINVAL;
1956 	}
1957 	memset(&vrange_end, 0, sizeof(vrange_end));
1958 
1959 	vinfo = nla_data(tb[BRIDGE_VLANDB_ENTRY_INFO]);
1960 	if (vinfo->flags & (BRIDGE_VLAN_INFO_RANGE_BEGIN |
1961 			    BRIDGE_VLAN_INFO_RANGE_END)) {
1962 		NL_SET_ERR_MSG_MOD(extack, "Old-style vlan ranges are not allowed when using RTM vlan calls");
1963 		return -EINVAL;
1964 	}
1965 	if (!br_vlan_valid_id(vinfo->vid, extack))
1966 		return -EINVAL;
1967 
1968 	if (tb[BRIDGE_VLANDB_ENTRY_RANGE]) {
1969 		vrange_end.vid = nla_get_u16(tb[BRIDGE_VLANDB_ENTRY_RANGE]);
1970 		/* validate user-provided flags without RANGE_BEGIN */
1971 		vrange_end.flags = BRIDGE_VLAN_INFO_RANGE_END | vinfo->flags;
1972 		vinfo->flags |= BRIDGE_VLAN_INFO_RANGE_BEGIN;
1973 
1974 		/* vinfo_last is the range start, vinfo the range end */
1975 		vinfo_last = vinfo;
1976 		vinfo = &vrange_end;
1977 
1978 		if (!br_vlan_valid_id(vinfo->vid, extack) ||
1979 		    !br_vlan_valid_range(vinfo, vinfo_last, extack))
1980 			return -EINVAL;
1981 	}
1982 
1983 	switch (cmd) {
1984 	case RTM_NEWVLAN:
1985 		cmdmap = RTM_SETLINK;
1986 		skip_processing = !!(vinfo->flags & BRIDGE_VLAN_INFO_ONLY_OPTS);
1987 		break;
1988 	case RTM_DELVLAN:
1989 		cmdmap = RTM_DELLINK;
1990 		break;
1991 	}
1992 
1993 	if (!skip_processing) {
1994 		struct bridge_vlan_info *tmp_last = vinfo_last;
1995 
1996 		/* br_process_vlan_info may overwrite vinfo_last */
1997 		err = br_process_vlan_info(br, p, cmdmap, vinfo, &tmp_last,
1998 					   &changed, extack);
1999 
2000 		/* notify first if anything changed */
2001 		if (changed)
2002 			br_ifinfo_notify(cmdmap, br, p);
2003 
2004 		if (err)
2005 			return err;
2006 	}
2007 
2008 	/* deal with options */
2009 	if (cmd == RTM_NEWVLAN) {
2010 		struct net_bridge_vlan *range_start, *range_end;
2011 
2012 		if (vinfo_last) {
2013 			range_start = br_vlan_find(vg, vinfo_last->vid);
2014 			range_end = br_vlan_find(vg, vinfo->vid);
2015 		} else {
2016 			range_start = br_vlan_find(vg, vinfo->vid);
2017 			range_end = range_start;
2018 		}
2019 
2020 		err = br_vlan_process_options(br, p, range_start, range_end,
2021 					      tb, extack);
2022 	}
2023 
2024 	return err;
2025 }
2026 
2027 static int br_vlan_rtm_process(struct sk_buff *skb, struct nlmsghdr *nlh,
2028 			       struct netlink_ext_ack *extack)
2029 {
2030 	struct net *net = sock_net(skb->sk);
2031 	struct br_vlan_msg *bvm;
2032 	struct net_device *dev;
2033 	struct nlattr *attr;
2034 	int err, vlans = 0;
2035 	int rem;
2036 
2037 	/* this should validate the header and check for remaining bytes */
2038 	err = nlmsg_parse(nlh, sizeof(*bvm), NULL, BRIDGE_VLANDB_MAX, NULL,
2039 			  extack);
2040 	if (err < 0)
2041 		return err;
2042 
2043 	bvm = nlmsg_data(nlh);
2044 	dev = __dev_get_by_index(net, bvm->ifindex);
2045 	if (!dev)
2046 		return -ENODEV;
2047 
2048 	if (!netif_is_bridge_master(dev) && !netif_is_bridge_port(dev)) {
2049 		NL_SET_ERR_MSG_MOD(extack, "The device is not a valid bridge or bridge port");
2050 		return -EINVAL;
2051 	}
2052 
2053 	nlmsg_for_each_attr(attr, nlh, sizeof(*bvm), rem) {
2054 		if (nla_type(attr) != BRIDGE_VLANDB_ENTRY)
2055 			continue;
2056 
2057 		vlans++;
2058 		err = br_vlan_rtm_process_one(dev, attr, nlh->nlmsg_type,
2059 					      extack);
2060 		if (err)
2061 			break;
2062 	}
2063 	if (!vlans) {
2064 		NL_SET_ERR_MSG_MOD(extack, "No vlans found to process");
2065 		err = -EINVAL;
2066 	}
2067 
2068 	return err;
2069 }
2070 
2071 void br_vlan_rtnl_init(void)
2072 {
2073 	rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_GETVLAN, NULL,
2074 			     br_vlan_rtm_dump, 0);
2075 	rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_NEWVLAN,
2076 			     br_vlan_rtm_process, NULL, 0);
2077 	rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_DELVLAN,
2078 			     br_vlan_rtm_process, NULL, 0);
2079 }
2080 
2081 void br_vlan_rtnl_uninit(void)
2082 {
2083 	rtnl_unregister(PF_BRIDGE, RTM_GETVLAN);
2084 	rtnl_unregister(PF_BRIDGE, RTM_NEWVLAN);
2085 	rtnl_unregister(PF_BRIDGE, RTM_DELVLAN);
2086 }
2087