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