xref: /openbmc/linux/net/bridge/br_vlan.c (revision 82e6fdd6)
1 #include <linux/kernel.h>
2 #include <linux/netdevice.h>
3 #include <linux/rtnetlink.h>
4 #include <linux/slab.h>
5 #include <net/switchdev.h>
6 
7 #include "br_private.h"
8 #include "br_private_tunnel.h"
9 
10 static inline int br_vlan_cmp(struct rhashtable_compare_arg *arg,
11 			      const void *ptr)
12 {
13 	const struct net_bridge_vlan *vle = ptr;
14 	u16 vid = *(u16 *)arg->key;
15 
16 	return vle->vid != vid;
17 }
18 
19 static const struct rhashtable_params br_vlan_rht_params = {
20 	.head_offset = offsetof(struct net_bridge_vlan, vnode),
21 	.key_offset = offsetof(struct net_bridge_vlan, vid),
22 	.key_len = sizeof(u16),
23 	.nelem_hint = 3,
24 	.locks_mul = 1,
25 	.max_size = VLAN_N_VID,
26 	.obj_cmpfn = br_vlan_cmp,
27 	.automatic_shrinking = true,
28 };
29 
30 static struct net_bridge_vlan *br_vlan_lookup(struct rhashtable *tbl, u16 vid)
31 {
32 	return rhashtable_lookup_fast(tbl, &vid, br_vlan_rht_params);
33 }
34 
35 static bool __vlan_add_pvid(struct net_bridge_vlan_group *vg, u16 vid)
36 {
37 	if (vg->pvid == vid)
38 		return false;
39 
40 	smp_wmb();
41 	vg->pvid = vid;
42 
43 	return true;
44 }
45 
46 static bool __vlan_delete_pvid(struct net_bridge_vlan_group *vg, u16 vid)
47 {
48 	if (vg->pvid != vid)
49 		return false;
50 
51 	smp_wmb();
52 	vg->pvid = 0;
53 
54 	return true;
55 }
56 
57 /* return true if anything changed, false otherwise */
58 static bool __vlan_add_flags(struct net_bridge_vlan *v, u16 flags)
59 {
60 	struct net_bridge_vlan_group *vg;
61 	u16 old_flags = v->flags;
62 	bool ret;
63 
64 	if (br_vlan_is_master(v))
65 		vg = br_vlan_group(v->br);
66 	else
67 		vg = nbp_vlan_group(v->port);
68 
69 	if (flags & BRIDGE_VLAN_INFO_PVID)
70 		ret = __vlan_add_pvid(vg, v->vid);
71 	else
72 		ret = __vlan_delete_pvid(vg, v->vid);
73 
74 	if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
75 		v->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
76 	else
77 		v->flags &= ~BRIDGE_VLAN_INFO_UNTAGGED;
78 
79 	return ret || !!(old_flags ^ v->flags);
80 }
81 
82 static int __vlan_vid_add(struct net_device *dev, struct net_bridge *br,
83 			  u16 vid, u16 flags)
84 {
85 	struct switchdev_obj_port_vlan v = {
86 		.obj.orig_dev = dev,
87 		.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
88 		.flags = flags,
89 		.vid_begin = vid,
90 		.vid_end = vid,
91 	};
92 	int err;
93 
94 	/* Try switchdev op first. In case it is not supported, fallback to
95 	 * 8021q add.
96 	 */
97 	err = switchdev_port_obj_add(dev, &v.obj);
98 	if (err == -EOPNOTSUPP)
99 		return vlan_vid_add(dev, br->vlan_proto, vid);
100 	return err;
101 }
102 
103 static void __vlan_add_list(struct net_bridge_vlan *v)
104 {
105 	struct net_bridge_vlan_group *vg;
106 	struct list_head *headp, *hpos;
107 	struct net_bridge_vlan *vent;
108 
109 	if (br_vlan_is_master(v))
110 		vg = br_vlan_group(v->br);
111 	else
112 		vg = nbp_vlan_group(v->port);
113 
114 	headp = &vg->vlan_list;
115 	list_for_each_prev(hpos, headp) {
116 		vent = list_entry(hpos, struct net_bridge_vlan, vlist);
117 		if (v->vid < vent->vid)
118 			continue;
119 		else
120 			break;
121 	}
122 	list_add_rcu(&v->vlist, hpos);
123 }
124 
125 static void __vlan_del_list(struct net_bridge_vlan *v)
126 {
127 	list_del_rcu(&v->vlist);
128 }
129 
130 static int __vlan_vid_del(struct net_device *dev, struct net_bridge *br,
131 			  u16 vid)
132 {
133 	struct switchdev_obj_port_vlan v = {
134 		.obj.orig_dev = dev,
135 		.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
136 		.vid_begin = vid,
137 		.vid_end = vid,
138 	};
139 	int err;
140 
141 	/* Try switchdev op first. In case it is not supported, fallback to
142 	 * 8021q del.
143 	 */
144 	err = switchdev_port_obj_del(dev, &v.obj);
145 	if (err == -EOPNOTSUPP) {
146 		vlan_vid_del(dev, br->vlan_proto, vid);
147 		return 0;
148 	}
149 	return err;
150 }
151 
152 /* Returns a master vlan, if it didn't exist it gets created. In all cases a
153  * a reference is taken to the master vlan before returning.
154  */
155 static struct net_bridge_vlan *br_vlan_get_master(struct net_bridge *br, u16 vid)
156 {
157 	struct net_bridge_vlan_group *vg;
158 	struct net_bridge_vlan *masterv;
159 
160 	vg = br_vlan_group(br);
161 	masterv = br_vlan_find(vg, vid);
162 	if (!masterv) {
163 		bool changed;
164 
165 		/* missing global ctx, create it now */
166 		if (br_vlan_add(br, vid, 0, &changed))
167 			return NULL;
168 		masterv = br_vlan_find(vg, vid);
169 		if (WARN_ON(!masterv))
170 			return NULL;
171 	}
172 	refcount_inc(&masterv->refcnt);
173 
174 	return masterv;
175 }
176 
177 static void br_master_vlan_rcu_free(struct rcu_head *rcu)
178 {
179 	struct net_bridge_vlan *v;
180 
181 	v = container_of(rcu, struct net_bridge_vlan, rcu);
182 	WARN_ON(!br_vlan_is_master(v));
183 	free_percpu(v->stats);
184 	v->stats = NULL;
185 	kfree(v);
186 }
187 
188 static void br_vlan_put_master(struct net_bridge_vlan *masterv)
189 {
190 	struct net_bridge_vlan_group *vg;
191 
192 	if (!br_vlan_is_master(masterv))
193 		return;
194 
195 	vg = br_vlan_group(masterv->br);
196 	if (refcount_dec_and_test(&masterv->refcnt)) {
197 		rhashtable_remove_fast(&vg->vlan_hash,
198 				       &masterv->vnode, br_vlan_rht_params);
199 		__vlan_del_list(masterv);
200 		call_rcu(&masterv->rcu, br_master_vlan_rcu_free);
201 	}
202 }
203 
204 /* This is the shared VLAN add function which works for both ports and bridge
205  * devices. There are four possible calls to this function in terms of the
206  * vlan entry type:
207  * 1. vlan is being added on a port (no master flags, global entry exists)
208  * 2. vlan is being added on a bridge (both master and brentry flags)
209  * 3. vlan is being added on a port, but a global entry didn't exist which
210  *    is being created right now (master flag set, brentry flag unset), the
211  *    global entry is used for global per-vlan features, but not for filtering
212  * 4. same as 3 but with both master and brentry flags set so the entry
213  *    will be used for filtering in both the port and the bridge
214  */
215 static int __vlan_add(struct net_bridge_vlan *v, u16 flags)
216 {
217 	struct net_bridge_vlan *masterv = NULL;
218 	struct net_bridge_port *p = NULL;
219 	struct net_bridge_vlan_group *vg;
220 	struct net_device *dev;
221 	struct net_bridge *br;
222 	int err;
223 
224 	if (br_vlan_is_master(v)) {
225 		br = v->br;
226 		dev = br->dev;
227 		vg = br_vlan_group(br);
228 	} else {
229 		p = v->port;
230 		br = p->br;
231 		dev = p->dev;
232 		vg = nbp_vlan_group(p);
233 	}
234 
235 	if (p) {
236 		/* Add VLAN to the device filter if it is supported.
237 		 * This ensures tagged traffic enters the bridge when
238 		 * promiscuous mode is disabled by br_manage_promisc().
239 		 */
240 		err = __vlan_vid_add(dev, br, v->vid, flags);
241 		if (err)
242 			goto out;
243 
244 		/* need to work on the master vlan too */
245 		if (flags & BRIDGE_VLAN_INFO_MASTER) {
246 			bool changed;
247 
248 			err = br_vlan_add(br, v->vid,
249 					  flags | BRIDGE_VLAN_INFO_BRENTRY,
250 					  &changed);
251 			if (err)
252 				goto out_filt;
253 		}
254 
255 		masterv = br_vlan_get_master(br, v->vid);
256 		if (!masterv)
257 			goto out_filt;
258 		v->brvlan = masterv;
259 		v->stats = masterv->stats;
260 	}
261 
262 	/* Add the dev mac and count the vlan only if it's usable */
263 	if (br_vlan_should_use(v)) {
264 		err = br_fdb_insert(br, p, dev->dev_addr, v->vid);
265 		if (err) {
266 			br_err(br, "failed insert local address into bridge forwarding table\n");
267 			goto out_filt;
268 		}
269 		vg->num_vlans++;
270 	}
271 
272 	err = rhashtable_lookup_insert_fast(&vg->vlan_hash, &v->vnode,
273 					    br_vlan_rht_params);
274 	if (err)
275 		goto out_fdb_insert;
276 
277 	__vlan_add_list(v);
278 	__vlan_add_flags(v, flags);
279 out:
280 	return err;
281 
282 out_fdb_insert:
283 	if (br_vlan_should_use(v)) {
284 		br_fdb_find_delete_local(br, p, dev->dev_addr, v->vid);
285 		vg->num_vlans--;
286 	}
287 
288 out_filt:
289 	if (p) {
290 		__vlan_vid_del(dev, br, v->vid);
291 		if (masterv) {
292 			br_vlan_put_master(masterv);
293 			v->brvlan = NULL;
294 		}
295 	}
296 
297 	goto out;
298 }
299 
300 static int __vlan_del(struct net_bridge_vlan *v)
301 {
302 	struct net_bridge_vlan *masterv = v;
303 	struct net_bridge_vlan_group *vg;
304 	struct net_bridge_port *p = NULL;
305 	int err = 0;
306 
307 	if (br_vlan_is_master(v)) {
308 		vg = br_vlan_group(v->br);
309 	} else {
310 		p = v->port;
311 		vg = nbp_vlan_group(v->port);
312 		masterv = v->brvlan;
313 	}
314 
315 	__vlan_delete_pvid(vg, v->vid);
316 	if (p) {
317 		err = __vlan_vid_del(p->dev, p->br, v->vid);
318 		if (err)
319 			goto out;
320 	}
321 
322 	if (br_vlan_should_use(v)) {
323 		v->flags &= ~BRIDGE_VLAN_INFO_BRENTRY;
324 		vg->num_vlans--;
325 	}
326 
327 	if (masterv != v) {
328 		vlan_tunnel_info_del(vg, v);
329 		rhashtable_remove_fast(&vg->vlan_hash, &v->vnode,
330 				       br_vlan_rht_params);
331 		__vlan_del_list(v);
332 		kfree_rcu(v, rcu);
333 	}
334 
335 	br_vlan_put_master(masterv);
336 out:
337 	return err;
338 }
339 
340 static void __vlan_group_free(struct net_bridge_vlan_group *vg)
341 {
342 	WARN_ON(!list_empty(&vg->vlan_list));
343 	rhashtable_destroy(&vg->vlan_hash);
344 	vlan_tunnel_deinit(vg);
345 	kfree(vg);
346 }
347 
348 static void __vlan_flush(struct net_bridge_vlan_group *vg)
349 {
350 	struct net_bridge_vlan *vlan, *tmp;
351 
352 	__vlan_delete_pvid(vg, vg->pvid);
353 	list_for_each_entry_safe(vlan, tmp, &vg->vlan_list, vlist)
354 		__vlan_del(vlan);
355 }
356 
357 struct sk_buff *br_handle_vlan(struct net_bridge *br,
358 			       const struct net_bridge_port *p,
359 			       struct net_bridge_vlan_group *vg,
360 			       struct sk_buff *skb)
361 {
362 	struct br_vlan_stats *stats;
363 	struct net_bridge_vlan *v;
364 	u16 vid;
365 
366 	/* If this packet was not filtered at input, let it pass */
367 	if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
368 		goto out;
369 
370 	/* At this point, we know that the frame was filtered and contains
371 	 * a valid vlan id.  If the vlan id has untagged flag set,
372 	 * send untagged; otherwise, send tagged.
373 	 */
374 	br_vlan_get_tag(skb, &vid);
375 	v = br_vlan_find(vg, vid);
376 	/* Vlan entry must be configured at this point.  The
377 	 * only exception is the bridge is set in promisc mode and the
378 	 * packet is destined for the bridge device.  In this case
379 	 * pass the packet as is.
380 	 */
381 	if (!v || !br_vlan_should_use(v)) {
382 		if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) {
383 			goto out;
384 		} else {
385 			kfree_skb(skb);
386 			return NULL;
387 		}
388 	}
389 	if (br->vlan_stats_enabled) {
390 		stats = this_cpu_ptr(v->stats);
391 		u64_stats_update_begin(&stats->syncp);
392 		stats->tx_bytes += skb->len;
393 		stats->tx_packets++;
394 		u64_stats_update_end(&stats->syncp);
395 	}
396 
397 	if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
398 		skb->vlan_tci = 0;
399 
400 	if (p && (p->flags & BR_VLAN_TUNNEL) &&
401 	    br_handle_egress_vlan_tunnel(skb, v)) {
402 		kfree_skb(skb);
403 		return NULL;
404 	}
405 out:
406 	return skb;
407 }
408 
409 /* Called under RCU */
410 static bool __allowed_ingress(const struct net_bridge *br,
411 			      struct net_bridge_vlan_group *vg,
412 			      struct sk_buff *skb, u16 *vid)
413 {
414 	struct br_vlan_stats *stats;
415 	struct net_bridge_vlan *v;
416 	bool tagged;
417 
418 	BR_INPUT_SKB_CB(skb)->vlan_filtered = true;
419 	/* If vlan tx offload is disabled on bridge device and frame was
420 	 * sent from vlan device on the bridge device, it does not have
421 	 * HW accelerated vlan tag.
422 	 */
423 	if (unlikely(!skb_vlan_tag_present(skb) &&
424 		     skb->protocol == br->vlan_proto)) {
425 		skb = skb_vlan_untag(skb);
426 		if (unlikely(!skb))
427 			return false;
428 	}
429 
430 	if (!br_vlan_get_tag(skb, vid)) {
431 		/* Tagged frame */
432 		if (skb->vlan_proto != br->vlan_proto) {
433 			/* Protocol-mismatch, empty out vlan_tci for new tag */
434 			skb_push(skb, ETH_HLEN);
435 			skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
436 							skb_vlan_tag_get(skb));
437 			if (unlikely(!skb))
438 				return false;
439 
440 			skb_pull(skb, ETH_HLEN);
441 			skb_reset_mac_len(skb);
442 			*vid = 0;
443 			tagged = false;
444 		} else {
445 			tagged = true;
446 		}
447 	} else {
448 		/* Untagged frame */
449 		tagged = false;
450 	}
451 
452 	if (!*vid) {
453 		u16 pvid = br_get_pvid(vg);
454 
455 		/* Frame had a tag with VID 0 or did not have a tag.
456 		 * See if pvid is set on this port.  That tells us which
457 		 * vlan untagged or priority-tagged traffic belongs to.
458 		 */
459 		if (!pvid)
460 			goto drop;
461 
462 		/* PVID is set on this port.  Any untagged or priority-tagged
463 		 * ingress frame is considered to belong to this vlan.
464 		 */
465 		*vid = pvid;
466 		if (likely(!tagged))
467 			/* Untagged Frame. */
468 			__vlan_hwaccel_put_tag(skb, br->vlan_proto, pvid);
469 		else
470 			/* Priority-tagged Frame.
471 			 * At this point, We know that skb->vlan_tci had
472 			 * VLAN_TAG_PRESENT bit and its VID field was 0x000.
473 			 * We update only VID field and preserve PCP field.
474 			 */
475 			skb->vlan_tci |= pvid;
476 
477 		/* if stats are disabled we can avoid the lookup */
478 		if (!br->vlan_stats_enabled)
479 			return true;
480 	}
481 	v = br_vlan_find(vg, *vid);
482 	if (!v || !br_vlan_should_use(v))
483 		goto drop;
484 
485 	if (br->vlan_stats_enabled) {
486 		stats = this_cpu_ptr(v->stats);
487 		u64_stats_update_begin(&stats->syncp);
488 		stats->rx_bytes += skb->len;
489 		stats->rx_packets++;
490 		u64_stats_update_end(&stats->syncp);
491 	}
492 
493 	return true;
494 
495 drop:
496 	kfree_skb(skb);
497 	return false;
498 }
499 
500 bool br_allowed_ingress(const struct net_bridge *br,
501 			struct net_bridge_vlan_group *vg, struct sk_buff *skb,
502 			u16 *vid)
503 {
504 	/* If VLAN filtering is disabled on the bridge, all packets are
505 	 * permitted.
506 	 */
507 	if (!br->vlan_enabled) {
508 		BR_INPUT_SKB_CB(skb)->vlan_filtered = false;
509 		return true;
510 	}
511 
512 	return __allowed_ingress(br, vg, skb, vid);
513 }
514 
515 /* Called under RCU. */
516 bool br_allowed_egress(struct net_bridge_vlan_group *vg,
517 		       const struct sk_buff *skb)
518 {
519 	const struct net_bridge_vlan *v;
520 	u16 vid;
521 
522 	/* If this packet was not filtered at input, let it pass */
523 	if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
524 		return true;
525 
526 	br_vlan_get_tag(skb, &vid);
527 	v = br_vlan_find(vg, vid);
528 	if (v && br_vlan_should_use(v))
529 		return true;
530 
531 	return false;
532 }
533 
534 /* Called under RCU */
535 bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid)
536 {
537 	struct net_bridge_vlan_group *vg;
538 	struct net_bridge *br = p->br;
539 
540 	/* If filtering was disabled at input, let it pass. */
541 	if (!br->vlan_enabled)
542 		return true;
543 
544 	vg = nbp_vlan_group_rcu(p);
545 	if (!vg || !vg->num_vlans)
546 		return false;
547 
548 	if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto)
549 		*vid = 0;
550 
551 	if (!*vid) {
552 		*vid = br_get_pvid(vg);
553 		if (!*vid)
554 			return false;
555 
556 		return true;
557 	}
558 
559 	if (br_vlan_find(vg, *vid))
560 		return true;
561 
562 	return false;
563 }
564 
565 /* Must be protected by RTNL.
566  * Must be called with vid in range from 1 to 4094 inclusive.
567  * changed must be true only if the vlan was created or updated
568  */
569 int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags, bool *changed)
570 {
571 	struct net_bridge_vlan_group *vg;
572 	struct net_bridge_vlan *vlan;
573 	int ret;
574 
575 	ASSERT_RTNL();
576 
577 	*changed = false;
578 	vg = br_vlan_group(br);
579 	vlan = br_vlan_find(vg, vid);
580 	if (vlan) {
581 		if (!br_vlan_is_brentry(vlan)) {
582 			/* Trying to change flags of non-existent bridge vlan */
583 			if (!(flags & BRIDGE_VLAN_INFO_BRENTRY))
584 				return -EINVAL;
585 			/* It was only kept for port vlans, now make it real */
586 			ret = br_fdb_insert(br, NULL, br->dev->dev_addr,
587 					    vlan->vid);
588 			if (ret) {
589 				br_err(br, "failed insert local address into bridge forwarding table\n");
590 				return ret;
591 			}
592 			refcount_inc(&vlan->refcnt);
593 			vlan->flags |= BRIDGE_VLAN_INFO_BRENTRY;
594 			vg->num_vlans++;
595 			*changed = true;
596 		}
597 		if (__vlan_add_flags(vlan, flags))
598 			*changed = true;
599 
600 		return 0;
601 	}
602 
603 	vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
604 	if (!vlan)
605 		return -ENOMEM;
606 
607 	vlan->stats = netdev_alloc_pcpu_stats(struct br_vlan_stats);
608 	if (!vlan->stats) {
609 		kfree(vlan);
610 		return -ENOMEM;
611 	}
612 	vlan->vid = vid;
613 	vlan->flags = flags | BRIDGE_VLAN_INFO_MASTER;
614 	vlan->flags &= ~BRIDGE_VLAN_INFO_PVID;
615 	vlan->br = br;
616 	if (flags & BRIDGE_VLAN_INFO_BRENTRY)
617 		refcount_set(&vlan->refcnt, 1);
618 	ret = __vlan_add(vlan, flags);
619 	if (ret) {
620 		free_percpu(vlan->stats);
621 		kfree(vlan);
622 	} else {
623 		*changed = true;
624 	}
625 
626 	return ret;
627 }
628 
629 /* Must be protected by RTNL.
630  * Must be called with vid in range from 1 to 4094 inclusive.
631  */
632 int br_vlan_delete(struct net_bridge *br, u16 vid)
633 {
634 	struct net_bridge_vlan_group *vg;
635 	struct net_bridge_vlan *v;
636 
637 	ASSERT_RTNL();
638 
639 	vg = br_vlan_group(br);
640 	v = br_vlan_find(vg, vid);
641 	if (!v || !br_vlan_is_brentry(v))
642 		return -ENOENT;
643 
644 	br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
645 	br_fdb_delete_by_port(br, NULL, vid, 0);
646 
647 	vlan_tunnel_info_del(vg, v);
648 
649 	return __vlan_del(v);
650 }
651 
652 void br_vlan_flush(struct net_bridge *br)
653 {
654 	struct net_bridge_vlan_group *vg;
655 
656 	ASSERT_RTNL();
657 
658 	vg = br_vlan_group(br);
659 	__vlan_flush(vg);
660 	RCU_INIT_POINTER(br->vlgrp, NULL);
661 	synchronize_rcu();
662 	__vlan_group_free(vg);
663 }
664 
665 struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid)
666 {
667 	if (!vg)
668 		return NULL;
669 
670 	return br_vlan_lookup(&vg->vlan_hash, vid);
671 }
672 
673 /* Must be protected by RTNL. */
674 static void recalculate_group_addr(struct net_bridge *br)
675 {
676 	if (br->group_addr_set)
677 		return;
678 
679 	spin_lock_bh(&br->lock);
680 	if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q)) {
681 		/* Bridge Group Address */
682 		br->group_addr[5] = 0x00;
683 	} else { /* vlan_enabled && ETH_P_8021AD */
684 		/* Provider Bridge Group Address */
685 		br->group_addr[5] = 0x08;
686 	}
687 	spin_unlock_bh(&br->lock);
688 }
689 
690 /* Must be protected by RTNL. */
691 void br_recalculate_fwd_mask(struct net_bridge *br)
692 {
693 	if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q))
694 		br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT;
695 	else /* vlan_enabled && ETH_P_8021AD */
696 		br->group_fwd_mask_required = BR_GROUPFWD_8021AD &
697 					      ~(1u << br->group_addr[5]);
698 }
699 
700 int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
701 {
702 	struct switchdev_attr attr = {
703 		.orig_dev = br->dev,
704 		.id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
705 		.flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
706 		.u.vlan_filtering = val,
707 	};
708 	int err;
709 
710 	if (br->vlan_enabled == val)
711 		return 0;
712 
713 	err = switchdev_port_attr_set(br->dev, &attr);
714 	if (err && err != -EOPNOTSUPP)
715 		return err;
716 
717 	br->vlan_enabled = val;
718 	br_manage_promisc(br);
719 	recalculate_group_addr(br);
720 	br_recalculate_fwd_mask(br);
721 
722 	return 0;
723 }
724 
725 int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
726 {
727 	return __br_vlan_filter_toggle(br, val);
728 }
729 
730 bool br_vlan_enabled(const struct net_device *dev)
731 {
732 	struct net_bridge *br = netdev_priv(dev);
733 
734 	return !!br->vlan_enabled;
735 }
736 EXPORT_SYMBOL_GPL(br_vlan_enabled);
737 
738 int __br_vlan_set_proto(struct net_bridge *br, __be16 proto)
739 {
740 	int err = 0;
741 	struct net_bridge_port *p;
742 	struct net_bridge_vlan *vlan;
743 	struct net_bridge_vlan_group *vg;
744 	__be16 oldproto;
745 
746 	if (br->vlan_proto == proto)
747 		return 0;
748 
749 	/* Add VLANs for the new proto to the device filter. */
750 	list_for_each_entry(p, &br->port_list, list) {
751 		vg = nbp_vlan_group(p);
752 		list_for_each_entry(vlan, &vg->vlan_list, vlist) {
753 			err = vlan_vid_add(p->dev, proto, vlan->vid);
754 			if (err)
755 				goto err_filt;
756 		}
757 	}
758 
759 	oldproto = br->vlan_proto;
760 	br->vlan_proto = proto;
761 
762 	recalculate_group_addr(br);
763 	br_recalculate_fwd_mask(br);
764 
765 	/* Delete VLANs for the old proto from the device filter. */
766 	list_for_each_entry(p, &br->port_list, list) {
767 		vg = nbp_vlan_group(p);
768 		list_for_each_entry(vlan, &vg->vlan_list, vlist)
769 			vlan_vid_del(p->dev, oldproto, vlan->vid);
770 	}
771 
772 	return 0;
773 
774 err_filt:
775 	list_for_each_entry_continue_reverse(vlan, &vg->vlan_list, vlist)
776 		vlan_vid_del(p->dev, proto, vlan->vid);
777 
778 	list_for_each_entry_continue_reverse(p, &br->port_list, list) {
779 		vg = nbp_vlan_group(p);
780 		list_for_each_entry(vlan, &vg->vlan_list, vlist)
781 			vlan_vid_del(p->dev, proto, vlan->vid);
782 	}
783 
784 	return err;
785 }
786 
787 int br_vlan_set_proto(struct net_bridge *br, unsigned long val)
788 {
789 	if (val != ETH_P_8021Q && val != ETH_P_8021AD)
790 		return -EPROTONOSUPPORT;
791 
792 	return __br_vlan_set_proto(br, htons(val));
793 }
794 
795 int br_vlan_set_stats(struct net_bridge *br, unsigned long val)
796 {
797 	switch (val) {
798 	case 0:
799 	case 1:
800 		br->vlan_stats_enabled = val;
801 		break;
802 	default:
803 		return -EINVAL;
804 	}
805 
806 	return 0;
807 }
808 
809 static bool vlan_default_pvid(struct net_bridge_vlan_group *vg, u16 vid)
810 {
811 	struct net_bridge_vlan *v;
812 
813 	if (vid != vg->pvid)
814 		return false;
815 
816 	v = br_vlan_lookup(&vg->vlan_hash, vid);
817 	if (v && br_vlan_should_use(v) &&
818 	    (v->flags & BRIDGE_VLAN_INFO_UNTAGGED))
819 		return true;
820 
821 	return false;
822 }
823 
824 static void br_vlan_disable_default_pvid(struct net_bridge *br)
825 {
826 	struct net_bridge_port *p;
827 	u16 pvid = br->default_pvid;
828 
829 	/* Disable default_pvid on all ports where it is still
830 	 * configured.
831 	 */
832 	if (vlan_default_pvid(br_vlan_group(br), pvid))
833 		br_vlan_delete(br, pvid);
834 
835 	list_for_each_entry(p, &br->port_list, list) {
836 		if (vlan_default_pvid(nbp_vlan_group(p), pvid))
837 			nbp_vlan_delete(p, pvid);
838 	}
839 
840 	br->default_pvid = 0;
841 }
842 
843 int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid)
844 {
845 	const struct net_bridge_vlan *pvent;
846 	struct net_bridge_vlan_group *vg;
847 	struct net_bridge_port *p;
848 	unsigned long *changed;
849 	bool vlchange;
850 	u16 old_pvid;
851 	int err = 0;
852 
853 	if (!pvid) {
854 		br_vlan_disable_default_pvid(br);
855 		return 0;
856 	}
857 
858 	changed = kcalloc(BITS_TO_LONGS(BR_MAX_PORTS), sizeof(unsigned long),
859 			  GFP_KERNEL);
860 	if (!changed)
861 		return -ENOMEM;
862 
863 	old_pvid = br->default_pvid;
864 
865 	/* Update default_pvid config only if we do not conflict with
866 	 * user configuration.
867 	 */
868 	vg = br_vlan_group(br);
869 	pvent = br_vlan_find(vg, pvid);
870 	if ((!old_pvid || vlan_default_pvid(vg, old_pvid)) &&
871 	    (!pvent || !br_vlan_should_use(pvent))) {
872 		err = br_vlan_add(br, pvid,
873 				  BRIDGE_VLAN_INFO_PVID |
874 				  BRIDGE_VLAN_INFO_UNTAGGED |
875 				  BRIDGE_VLAN_INFO_BRENTRY,
876 				  &vlchange);
877 		if (err)
878 			goto out;
879 		br_vlan_delete(br, old_pvid);
880 		set_bit(0, changed);
881 	}
882 
883 	list_for_each_entry(p, &br->port_list, list) {
884 		/* Update default_pvid config only if we do not conflict with
885 		 * user configuration.
886 		 */
887 		vg = nbp_vlan_group(p);
888 		if ((old_pvid &&
889 		     !vlan_default_pvid(vg, old_pvid)) ||
890 		    br_vlan_find(vg, pvid))
891 			continue;
892 
893 		err = nbp_vlan_add(p, pvid,
894 				   BRIDGE_VLAN_INFO_PVID |
895 				   BRIDGE_VLAN_INFO_UNTAGGED,
896 				   &vlchange);
897 		if (err)
898 			goto err_port;
899 		nbp_vlan_delete(p, old_pvid);
900 		set_bit(p->port_no, changed);
901 	}
902 
903 	br->default_pvid = pvid;
904 
905 out:
906 	kfree(changed);
907 	return err;
908 
909 err_port:
910 	list_for_each_entry_continue_reverse(p, &br->port_list, list) {
911 		if (!test_bit(p->port_no, changed))
912 			continue;
913 
914 		if (old_pvid)
915 			nbp_vlan_add(p, old_pvid,
916 				     BRIDGE_VLAN_INFO_PVID |
917 				     BRIDGE_VLAN_INFO_UNTAGGED,
918 				     &vlchange);
919 		nbp_vlan_delete(p, pvid);
920 	}
921 
922 	if (test_bit(0, changed)) {
923 		if (old_pvid)
924 			br_vlan_add(br, old_pvid,
925 				    BRIDGE_VLAN_INFO_PVID |
926 				    BRIDGE_VLAN_INFO_UNTAGGED |
927 				    BRIDGE_VLAN_INFO_BRENTRY,
928 				    &vlchange);
929 		br_vlan_delete(br, pvid);
930 	}
931 	goto out;
932 }
933 
934 int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val)
935 {
936 	u16 pvid = val;
937 	int err = 0;
938 
939 	if (val >= VLAN_VID_MASK)
940 		return -EINVAL;
941 
942 	if (pvid == br->default_pvid)
943 		goto out;
944 
945 	/* Only allow default pvid change when filtering is disabled */
946 	if (br->vlan_enabled) {
947 		pr_info_once("Please disable vlan filtering to change default_pvid\n");
948 		err = -EPERM;
949 		goto out;
950 	}
951 	err = __br_vlan_set_default_pvid(br, pvid);
952 out:
953 	return err;
954 }
955 
956 int br_vlan_init(struct net_bridge *br)
957 {
958 	struct net_bridge_vlan_group *vg;
959 	int ret = -ENOMEM;
960 	bool changed;
961 
962 	vg = kzalloc(sizeof(*vg), GFP_KERNEL);
963 	if (!vg)
964 		goto out;
965 	ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
966 	if (ret)
967 		goto err_rhtbl;
968 	ret = vlan_tunnel_init(vg);
969 	if (ret)
970 		goto err_tunnel_init;
971 	INIT_LIST_HEAD(&vg->vlan_list);
972 	br->vlan_proto = htons(ETH_P_8021Q);
973 	br->default_pvid = 1;
974 	rcu_assign_pointer(br->vlgrp, vg);
975 	ret = br_vlan_add(br, 1,
976 			  BRIDGE_VLAN_INFO_PVID | BRIDGE_VLAN_INFO_UNTAGGED |
977 			  BRIDGE_VLAN_INFO_BRENTRY, &changed);
978 	if (ret)
979 		goto err_vlan_add;
980 
981 out:
982 	return ret;
983 
984 err_vlan_add:
985 	vlan_tunnel_deinit(vg);
986 err_tunnel_init:
987 	rhashtable_destroy(&vg->vlan_hash);
988 err_rhtbl:
989 	kfree(vg);
990 
991 	goto out;
992 }
993 
994 int nbp_vlan_init(struct net_bridge_port *p)
995 {
996 	struct switchdev_attr attr = {
997 		.orig_dev = p->br->dev,
998 		.id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
999 		.flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
1000 		.u.vlan_filtering = p->br->vlan_enabled,
1001 	};
1002 	struct net_bridge_vlan_group *vg;
1003 	int ret = -ENOMEM;
1004 
1005 	vg = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL);
1006 	if (!vg)
1007 		goto out;
1008 
1009 	ret = switchdev_port_attr_set(p->dev, &attr);
1010 	if (ret && ret != -EOPNOTSUPP)
1011 		goto err_vlan_enabled;
1012 
1013 	ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
1014 	if (ret)
1015 		goto err_rhtbl;
1016 	ret = vlan_tunnel_init(vg);
1017 	if (ret)
1018 		goto err_tunnel_init;
1019 	INIT_LIST_HEAD(&vg->vlan_list);
1020 	rcu_assign_pointer(p->vlgrp, vg);
1021 	if (p->br->default_pvid) {
1022 		bool changed;
1023 
1024 		ret = nbp_vlan_add(p, p->br->default_pvid,
1025 				   BRIDGE_VLAN_INFO_PVID |
1026 				   BRIDGE_VLAN_INFO_UNTAGGED,
1027 				   &changed);
1028 		if (ret)
1029 			goto err_vlan_add;
1030 	}
1031 out:
1032 	return ret;
1033 
1034 err_vlan_add:
1035 	RCU_INIT_POINTER(p->vlgrp, NULL);
1036 	synchronize_rcu();
1037 	vlan_tunnel_deinit(vg);
1038 err_tunnel_init:
1039 	rhashtable_destroy(&vg->vlan_hash);
1040 err_rhtbl:
1041 err_vlan_enabled:
1042 	kfree(vg);
1043 
1044 	goto out;
1045 }
1046 
1047 /* Must be protected by RTNL.
1048  * Must be called with vid in range from 1 to 4094 inclusive.
1049  * changed must be true only if the vlan was created or updated
1050  */
1051 int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags,
1052 		 bool *changed)
1053 {
1054 	struct switchdev_obj_port_vlan v = {
1055 		.obj.orig_dev = port->dev,
1056 		.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
1057 		.flags = flags,
1058 		.vid_begin = vid,
1059 		.vid_end = vid,
1060 	};
1061 	struct net_bridge_vlan *vlan;
1062 	int ret;
1063 
1064 	ASSERT_RTNL();
1065 
1066 	*changed = false;
1067 	vlan = br_vlan_find(nbp_vlan_group(port), vid);
1068 	if (vlan) {
1069 		/* Pass the flags to the hardware bridge */
1070 		ret = switchdev_port_obj_add(port->dev, &v.obj);
1071 		if (ret && ret != -EOPNOTSUPP)
1072 			return ret;
1073 		*changed = __vlan_add_flags(vlan, flags);
1074 
1075 		return 0;
1076 	}
1077 
1078 	vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
1079 	if (!vlan)
1080 		return -ENOMEM;
1081 
1082 	vlan->vid = vid;
1083 	vlan->port = port;
1084 	ret = __vlan_add(vlan, flags);
1085 	if (ret)
1086 		kfree(vlan);
1087 	else
1088 		*changed = true;
1089 
1090 	return ret;
1091 }
1092 
1093 /* Must be protected by RTNL.
1094  * Must be called with vid in range from 1 to 4094 inclusive.
1095  */
1096 int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
1097 {
1098 	struct net_bridge_vlan *v;
1099 
1100 	ASSERT_RTNL();
1101 
1102 	v = br_vlan_find(nbp_vlan_group(port), vid);
1103 	if (!v)
1104 		return -ENOENT;
1105 	br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
1106 	br_fdb_delete_by_port(port->br, port, vid, 0);
1107 
1108 	return __vlan_del(v);
1109 }
1110 
1111 void nbp_vlan_flush(struct net_bridge_port *port)
1112 {
1113 	struct net_bridge_vlan_group *vg;
1114 
1115 	ASSERT_RTNL();
1116 
1117 	vg = nbp_vlan_group(port);
1118 	__vlan_flush(vg);
1119 	RCU_INIT_POINTER(port->vlgrp, NULL);
1120 	synchronize_rcu();
1121 	__vlan_group_free(vg);
1122 }
1123 
1124 void br_vlan_get_stats(const struct net_bridge_vlan *v,
1125 		       struct br_vlan_stats *stats)
1126 {
1127 	int i;
1128 
1129 	memset(stats, 0, sizeof(*stats));
1130 	for_each_possible_cpu(i) {
1131 		u64 rxpackets, rxbytes, txpackets, txbytes;
1132 		struct br_vlan_stats *cpu_stats;
1133 		unsigned int start;
1134 
1135 		cpu_stats = per_cpu_ptr(v->stats, i);
1136 		do {
1137 			start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
1138 			rxpackets = cpu_stats->rx_packets;
1139 			rxbytes = cpu_stats->rx_bytes;
1140 			txbytes = cpu_stats->tx_bytes;
1141 			txpackets = cpu_stats->tx_packets;
1142 		} while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
1143 
1144 		stats->rx_packets += rxpackets;
1145 		stats->rx_bytes += rxbytes;
1146 		stats->tx_bytes += txbytes;
1147 		stats->tx_packets += txpackets;
1148 	}
1149 }
1150