Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm
[linux/fpc-iii.git] / net / bridge / br_vlan.c
blobb6de4f45716184d04f84886f8fdb0481c31012ad
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>
7 #include "br_private.h"
9 static inline int br_vlan_cmp(struct rhashtable_compare_arg *arg,
10 const void *ptr)
12 const struct net_bridge_vlan *vle = ptr;
13 u16 vid = *(u16 *)arg->key;
15 return vle->vid != vid;
18 static const struct rhashtable_params br_vlan_rht_params = {
19 .head_offset = offsetof(struct net_bridge_vlan, vnode),
20 .key_offset = offsetof(struct net_bridge_vlan, vid),
21 .key_len = sizeof(u16),
22 .nelem_hint = 3,
23 .locks_mul = 1,
24 .max_size = VLAN_N_VID,
25 .obj_cmpfn = br_vlan_cmp,
26 .automatic_shrinking = true,
29 static struct net_bridge_vlan *br_vlan_lookup(struct rhashtable *tbl, u16 vid)
31 return rhashtable_lookup_fast(tbl, &vid, br_vlan_rht_params);
34 static void __vlan_add_pvid(struct net_bridge_vlan_group *vg, u16 vid)
36 if (vg->pvid == vid)
37 return;
39 smp_wmb();
40 vg->pvid = vid;
43 static void __vlan_delete_pvid(struct net_bridge_vlan_group *vg, u16 vid)
45 if (vg->pvid != vid)
46 return;
48 smp_wmb();
49 vg->pvid = 0;
52 static void __vlan_add_flags(struct net_bridge_vlan *v, u16 flags)
54 struct net_bridge_vlan_group *vg;
56 if (br_vlan_is_master(v))
57 vg = br_vlan_group(v->br);
58 else
59 vg = nbp_vlan_group(v->port);
61 if (flags & BRIDGE_VLAN_INFO_PVID)
62 __vlan_add_pvid(vg, v->vid);
63 else
64 __vlan_delete_pvid(vg, v->vid);
66 if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
67 v->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
68 else
69 v->flags &= ~BRIDGE_VLAN_INFO_UNTAGGED;
72 static int __vlan_vid_add(struct net_device *dev, struct net_bridge *br,
73 u16 vid, u16 flags)
75 struct switchdev_obj_port_vlan v = {
76 .obj.orig_dev = dev,
77 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
78 .flags = flags,
79 .vid_begin = vid,
80 .vid_end = vid,
82 int err;
84 /* Try switchdev op first. In case it is not supported, fallback to
85 * 8021q add.
87 err = switchdev_port_obj_add(dev, &v.obj);
88 if (err == -EOPNOTSUPP)
89 return vlan_vid_add(dev, br->vlan_proto, vid);
90 return err;
93 static void __vlan_add_list(struct net_bridge_vlan *v)
95 struct net_bridge_vlan_group *vg;
96 struct list_head *headp, *hpos;
97 struct net_bridge_vlan *vent;
99 if (br_vlan_is_master(v))
100 vg = br_vlan_group(v->br);
101 else
102 vg = nbp_vlan_group(v->port);
104 headp = &vg->vlan_list;
105 list_for_each_prev(hpos, headp) {
106 vent = list_entry(hpos, struct net_bridge_vlan, vlist);
107 if (v->vid < vent->vid)
108 continue;
109 else
110 break;
112 list_add_rcu(&v->vlist, hpos);
115 static void __vlan_del_list(struct net_bridge_vlan *v)
117 list_del_rcu(&v->vlist);
120 static int __vlan_vid_del(struct net_device *dev, struct net_bridge *br,
121 u16 vid)
123 struct switchdev_obj_port_vlan v = {
124 .obj.orig_dev = dev,
125 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
126 .vid_begin = vid,
127 .vid_end = vid,
129 int err;
131 /* Try switchdev op first. In case it is not supported, fallback to
132 * 8021q del.
134 err = switchdev_port_obj_del(dev, &v.obj);
135 if (err == -EOPNOTSUPP) {
136 vlan_vid_del(dev, br->vlan_proto, vid);
137 return 0;
139 return err;
142 /* Returns a master vlan, if it didn't exist it gets created. In all cases a
143 * a reference is taken to the master vlan before returning.
145 static struct net_bridge_vlan *br_vlan_get_master(struct net_bridge *br, u16 vid)
147 struct net_bridge_vlan_group *vg;
148 struct net_bridge_vlan *masterv;
150 vg = br_vlan_group(br);
151 masterv = br_vlan_find(vg, vid);
152 if (!masterv) {
153 /* missing global ctx, create it now */
154 if (br_vlan_add(br, vid, 0))
155 return NULL;
156 masterv = br_vlan_find(vg, vid);
157 if (WARN_ON(!masterv))
158 return NULL;
160 atomic_inc(&masterv->refcnt);
162 return masterv;
165 static void br_master_vlan_rcu_free(struct rcu_head *rcu)
167 struct net_bridge_vlan *v;
169 v = container_of(rcu, struct net_bridge_vlan, rcu);
170 WARN_ON(!br_vlan_is_master(v));
171 free_percpu(v->stats);
172 v->stats = NULL;
173 kfree(v);
176 static void br_vlan_put_master(struct net_bridge_vlan *masterv)
178 struct net_bridge_vlan_group *vg;
180 if (!br_vlan_is_master(masterv))
181 return;
183 vg = br_vlan_group(masterv->br);
184 if (atomic_dec_and_test(&masterv->refcnt)) {
185 rhashtable_remove_fast(&vg->vlan_hash,
186 &masterv->vnode, br_vlan_rht_params);
187 __vlan_del_list(masterv);
188 call_rcu(&masterv->rcu, br_master_vlan_rcu_free);
192 /* This is the shared VLAN add function which works for both ports and bridge
193 * devices. There are four possible calls to this function in terms of the
194 * vlan entry type:
195 * 1. vlan is being added on a port (no master flags, global entry exists)
196 * 2. vlan is being added on a bridge (both master and brentry flags)
197 * 3. vlan is being added on a port, but a global entry didn't exist which
198 * is being created right now (master flag set, brentry flag unset), the
199 * global entry is used for global per-vlan features, but not for filtering
200 * 4. same as 3 but with both master and brentry flags set so the entry
201 * will be used for filtering in both the port and the bridge
203 static int __vlan_add(struct net_bridge_vlan *v, u16 flags)
205 struct net_bridge_vlan *masterv = NULL;
206 struct net_bridge_port *p = NULL;
207 struct net_bridge_vlan_group *vg;
208 struct net_device *dev;
209 struct net_bridge *br;
210 int err;
212 if (br_vlan_is_master(v)) {
213 br = v->br;
214 dev = br->dev;
215 vg = br_vlan_group(br);
216 } else {
217 p = v->port;
218 br = p->br;
219 dev = p->dev;
220 vg = nbp_vlan_group(p);
223 if (p) {
224 /* Add VLAN to the device filter if it is supported.
225 * This ensures tagged traffic enters the bridge when
226 * promiscuous mode is disabled by br_manage_promisc().
228 err = __vlan_vid_add(dev, br, v->vid, flags);
229 if (err)
230 goto out;
232 /* need to work on the master vlan too */
233 if (flags & BRIDGE_VLAN_INFO_MASTER) {
234 err = br_vlan_add(br, v->vid, flags |
235 BRIDGE_VLAN_INFO_BRENTRY);
236 if (err)
237 goto out_filt;
240 masterv = br_vlan_get_master(br, v->vid);
241 if (!masterv)
242 goto out_filt;
243 v->brvlan = masterv;
244 v->stats = masterv->stats;
247 /* Add the dev mac and count the vlan only if it's usable */
248 if (br_vlan_should_use(v)) {
249 err = br_fdb_insert(br, p, dev->dev_addr, v->vid);
250 if (err) {
251 br_err(br, "failed insert local address into bridge forwarding table\n");
252 goto out_filt;
254 vg->num_vlans++;
257 err = rhashtable_lookup_insert_fast(&vg->vlan_hash, &v->vnode,
258 br_vlan_rht_params);
259 if (err)
260 goto out_fdb_insert;
262 __vlan_add_list(v);
263 __vlan_add_flags(v, flags);
264 out:
265 return err;
267 out_fdb_insert:
268 if (br_vlan_should_use(v)) {
269 br_fdb_find_delete_local(br, p, dev->dev_addr, v->vid);
270 vg->num_vlans--;
273 out_filt:
274 if (p) {
275 __vlan_vid_del(dev, br, v->vid);
276 if (masterv) {
277 br_vlan_put_master(masterv);
278 v->brvlan = NULL;
282 goto out;
285 static int __vlan_del(struct net_bridge_vlan *v)
287 struct net_bridge_vlan *masterv = v;
288 struct net_bridge_vlan_group *vg;
289 struct net_bridge_port *p = NULL;
290 int err = 0;
292 if (br_vlan_is_master(v)) {
293 vg = br_vlan_group(v->br);
294 } else {
295 p = v->port;
296 vg = nbp_vlan_group(v->port);
297 masterv = v->brvlan;
300 __vlan_delete_pvid(vg, v->vid);
301 if (p) {
302 err = __vlan_vid_del(p->dev, p->br, v->vid);
303 if (err)
304 goto out;
307 if (br_vlan_should_use(v)) {
308 v->flags &= ~BRIDGE_VLAN_INFO_BRENTRY;
309 vg->num_vlans--;
312 if (masterv != v) {
313 rhashtable_remove_fast(&vg->vlan_hash, &v->vnode,
314 br_vlan_rht_params);
315 __vlan_del_list(v);
316 kfree_rcu(v, rcu);
319 br_vlan_put_master(masterv);
320 out:
321 return err;
324 static void __vlan_group_free(struct net_bridge_vlan_group *vg)
326 WARN_ON(!list_empty(&vg->vlan_list));
327 rhashtable_destroy(&vg->vlan_hash);
328 kfree(vg);
331 static void __vlan_flush(struct net_bridge_vlan_group *vg)
333 struct net_bridge_vlan *vlan, *tmp;
335 __vlan_delete_pvid(vg, vg->pvid);
336 list_for_each_entry_safe(vlan, tmp, &vg->vlan_list, vlist)
337 __vlan_del(vlan);
340 struct sk_buff *br_handle_vlan(struct net_bridge *br,
341 struct net_bridge_vlan_group *vg,
342 struct sk_buff *skb)
344 struct br_vlan_stats *stats;
345 struct net_bridge_vlan *v;
346 u16 vid;
348 /* If this packet was not filtered at input, let it pass */
349 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
350 goto out;
352 /* At this point, we know that the frame was filtered and contains
353 * a valid vlan id. If the vlan id has untagged flag set,
354 * send untagged; otherwise, send tagged.
356 br_vlan_get_tag(skb, &vid);
357 v = br_vlan_find(vg, vid);
358 /* Vlan entry must be configured at this point. The
359 * only exception is the bridge is set in promisc mode and the
360 * packet is destined for the bridge device. In this case
361 * pass the packet as is.
363 if (!v || !br_vlan_should_use(v)) {
364 if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) {
365 goto out;
366 } else {
367 kfree_skb(skb);
368 return NULL;
371 if (br->vlan_stats_enabled) {
372 stats = this_cpu_ptr(v->stats);
373 u64_stats_update_begin(&stats->syncp);
374 stats->tx_bytes += skb->len;
375 stats->tx_packets++;
376 u64_stats_update_end(&stats->syncp);
379 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
380 skb->vlan_tci = 0;
381 out:
382 return skb;
385 /* Called under RCU */
386 static bool __allowed_ingress(const struct net_bridge *br,
387 struct net_bridge_vlan_group *vg,
388 struct sk_buff *skb, u16 *vid)
390 struct br_vlan_stats *stats;
391 struct net_bridge_vlan *v;
392 bool tagged;
394 BR_INPUT_SKB_CB(skb)->vlan_filtered = true;
395 /* If vlan tx offload is disabled on bridge device and frame was
396 * sent from vlan device on the bridge device, it does not have
397 * HW accelerated vlan tag.
399 if (unlikely(!skb_vlan_tag_present(skb) &&
400 skb->protocol == br->vlan_proto)) {
401 skb = skb_vlan_untag(skb);
402 if (unlikely(!skb))
403 return false;
406 if (!br_vlan_get_tag(skb, vid)) {
407 /* Tagged frame */
408 if (skb->vlan_proto != br->vlan_proto) {
409 /* Protocol-mismatch, empty out vlan_tci for new tag */
410 skb_push(skb, ETH_HLEN);
411 skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
412 skb_vlan_tag_get(skb));
413 if (unlikely(!skb))
414 return false;
416 skb_pull(skb, ETH_HLEN);
417 skb_reset_mac_len(skb);
418 *vid = 0;
419 tagged = false;
420 } else {
421 tagged = true;
423 } else {
424 /* Untagged frame */
425 tagged = false;
428 if (!*vid) {
429 u16 pvid = br_get_pvid(vg);
431 /* Frame had a tag with VID 0 or did not have a tag.
432 * See if pvid is set on this port. That tells us which
433 * vlan untagged or priority-tagged traffic belongs to.
435 if (!pvid)
436 goto drop;
438 /* PVID is set on this port. Any untagged or priority-tagged
439 * ingress frame is considered to belong to this vlan.
441 *vid = pvid;
442 if (likely(!tagged))
443 /* Untagged Frame. */
444 __vlan_hwaccel_put_tag(skb, br->vlan_proto, pvid);
445 else
446 /* Priority-tagged Frame.
447 * At this point, We know that skb->vlan_tci had
448 * VLAN_TAG_PRESENT bit and its VID field was 0x000.
449 * We update only VID field and preserve PCP field.
451 skb->vlan_tci |= pvid;
453 /* if stats are disabled we can avoid the lookup */
454 if (!br->vlan_stats_enabled)
455 return true;
457 v = br_vlan_find(vg, *vid);
458 if (!v || !br_vlan_should_use(v))
459 goto drop;
461 if (br->vlan_stats_enabled) {
462 stats = this_cpu_ptr(v->stats);
463 u64_stats_update_begin(&stats->syncp);
464 stats->rx_bytes += skb->len;
465 stats->rx_packets++;
466 u64_stats_update_end(&stats->syncp);
469 return true;
471 drop:
472 kfree_skb(skb);
473 return false;
476 bool br_allowed_ingress(const struct net_bridge *br,
477 struct net_bridge_vlan_group *vg, struct sk_buff *skb,
478 u16 *vid)
480 /* If VLAN filtering is disabled on the bridge, all packets are
481 * permitted.
483 if (!br->vlan_enabled) {
484 BR_INPUT_SKB_CB(skb)->vlan_filtered = false;
485 return true;
488 return __allowed_ingress(br, vg, skb, vid);
491 /* Called under RCU. */
492 bool br_allowed_egress(struct net_bridge_vlan_group *vg,
493 const struct sk_buff *skb)
495 const struct net_bridge_vlan *v;
496 u16 vid;
498 /* If this packet was not filtered at input, let it pass */
499 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
500 return true;
502 br_vlan_get_tag(skb, &vid);
503 v = br_vlan_find(vg, vid);
504 if (v && br_vlan_should_use(v))
505 return true;
507 return false;
510 /* Called under RCU */
511 bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid)
513 struct net_bridge_vlan_group *vg;
514 struct net_bridge *br = p->br;
516 /* If filtering was disabled at input, let it pass. */
517 if (!br->vlan_enabled)
518 return true;
520 vg = nbp_vlan_group_rcu(p);
521 if (!vg || !vg->num_vlans)
522 return false;
524 if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto)
525 *vid = 0;
527 if (!*vid) {
528 *vid = br_get_pvid(vg);
529 if (!*vid)
530 return false;
532 return true;
535 if (br_vlan_find(vg, *vid))
536 return true;
538 return false;
541 /* Must be protected by RTNL.
542 * Must be called with vid in range from 1 to 4094 inclusive.
544 int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags)
546 struct net_bridge_vlan_group *vg;
547 struct net_bridge_vlan *vlan;
548 int ret;
550 ASSERT_RTNL();
552 vg = br_vlan_group(br);
553 vlan = br_vlan_find(vg, vid);
554 if (vlan) {
555 if (!br_vlan_is_brentry(vlan)) {
556 /* Trying to change flags of non-existent bridge vlan */
557 if (!(flags & BRIDGE_VLAN_INFO_BRENTRY))
558 return -EINVAL;
559 /* It was only kept for port vlans, now make it real */
560 ret = br_fdb_insert(br, NULL, br->dev->dev_addr,
561 vlan->vid);
562 if (ret) {
563 br_err(br, "failed insert local address into bridge forwarding table\n");
564 return ret;
566 atomic_inc(&vlan->refcnt);
567 vlan->flags |= BRIDGE_VLAN_INFO_BRENTRY;
568 vg->num_vlans++;
570 __vlan_add_flags(vlan, flags);
571 return 0;
574 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
575 if (!vlan)
576 return -ENOMEM;
578 vlan->stats = netdev_alloc_pcpu_stats(struct br_vlan_stats);
579 if (!vlan->stats) {
580 kfree(vlan);
581 return -ENOMEM;
583 vlan->vid = vid;
584 vlan->flags = flags | BRIDGE_VLAN_INFO_MASTER;
585 vlan->flags &= ~BRIDGE_VLAN_INFO_PVID;
586 vlan->br = br;
587 if (flags & BRIDGE_VLAN_INFO_BRENTRY)
588 atomic_set(&vlan->refcnt, 1);
589 ret = __vlan_add(vlan, flags);
590 if (ret) {
591 free_percpu(vlan->stats);
592 kfree(vlan);
595 return ret;
598 /* Must be protected by RTNL.
599 * Must be called with vid in range from 1 to 4094 inclusive.
601 int br_vlan_delete(struct net_bridge *br, u16 vid)
603 struct net_bridge_vlan_group *vg;
604 struct net_bridge_vlan *v;
606 ASSERT_RTNL();
608 vg = br_vlan_group(br);
609 v = br_vlan_find(vg, vid);
610 if (!v || !br_vlan_is_brentry(v))
611 return -ENOENT;
613 br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
614 br_fdb_delete_by_port(br, NULL, vid, 0);
616 return __vlan_del(v);
619 void br_vlan_flush(struct net_bridge *br)
621 struct net_bridge_vlan_group *vg;
623 ASSERT_RTNL();
625 vg = br_vlan_group(br);
626 __vlan_flush(vg);
627 RCU_INIT_POINTER(br->vlgrp, NULL);
628 synchronize_rcu();
629 __vlan_group_free(vg);
632 struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid)
634 if (!vg)
635 return NULL;
637 return br_vlan_lookup(&vg->vlan_hash, vid);
640 /* Must be protected by RTNL. */
641 static void recalculate_group_addr(struct net_bridge *br)
643 if (br->group_addr_set)
644 return;
646 spin_lock_bh(&br->lock);
647 if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q)) {
648 /* Bridge Group Address */
649 br->group_addr[5] = 0x00;
650 } else { /* vlan_enabled && ETH_P_8021AD */
651 /* Provider Bridge Group Address */
652 br->group_addr[5] = 0x08;
654 spin_unlock_bh(&br->lock);
657 /* Must be protected by RTNL. */
658 void br_recalculate_fwd_mask(struct net_bridge *br)
660 if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q))
661 br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT;
662 else /* vlan_enabled && ETH_P_8021AD */
663 br->group_fwd_mask_required = BR_GROUPFWD_8021AD &
664 ~(1u << br->group_addr[5]);
667 int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
669 struct switchdev_attr attr = {
670 .orig_dev = br->dev,
671 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
672 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
673 .u.vlan_filtering = val,
675 int err;
677 if (br->vlan_enabled == val)
678 return 0;
680 err = switchdev_port_attr_set(br->dev, &attr);
681 if (err && err != -EOPNOTSUPP)
682 return err;
684 br->vlan_enabled = val;
685 br_manage_promisc(br);
686 recalculate_group_addr(br);
687 br_recalculate_fwd_mask(br);
689 return 0;
692 int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
694 return __br_vlan_filter_toggle(br, val);
697 int __br_vlan_set_proto(struct net_bridge *br, __be16 proto)
699 int err = 0;
700 struct net_bridge_port *p;
701 struct net_bridge_vlan *vlan;
702 struct net_bridge_vlan_group *vg;
703 __be16 oldproto;
705 if (br->vlan_proto == proto)
706 return 0;
708 /* Add VLANs for the new proto to the device filter. */
709 list_for_each_entry(p, &br->port_list, list) {
710 vg = nbp_vlan_group(p);
711 list_for_each_entry(vlan, &vg->vlan_list, vlist) {
712 err = vlan_vid_add(p->dev, proto, vlan->vid);
713 if (err)
714 goto err_filt;
718 oldproto = br->vlan_proto;
719 br->vlan_proto = proto;
721 recalculate_group_addr(br);
722 br_recalculate_fwd_mask(br);
724 /* Delete VLANs for the old proto from the device filter. */
725 list_for_each_entry(p, &br->port_list, list) {
726 vg = nbp_vlan_group(p);
727 list_for_each_entry(vlan, &vg->vlan_list, vlist)
728 vlan_vid_del(p->dev, oldproto, vlan->vid);
731 return 0;
733 err_filt:
734 list_for_each_entry_continue_reverse(vlan, &vg->vlan_list, vlist)
735 vlan_vid_del(p->dev, proto, vlan->vid);
737 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
738 vg = nbp_vlan_group(p);
739 list_for_each_entry(vlan, &vg->vlan_list, vlist)
740 vlan_vid_del(p->dev, proto, vlan->vid);
743 return err;
746 int br_vlan_set_proto(struct net_bridge *br, unsigned long val)
748 if (val != ETH_P_8021Q && val != ETH_P_8021AD)
749 return -EPROTONOSUPPORT;
751 return __br_vlan_set_proto(br, htons(val));
754 int br_vlan_set_stats(struct net_bridge *br, unsigned long val)
756 switch (val) {
757 case 0:
758 case 1:
759 br->vlan_stats_enabled = val;
760 break;
761 default:
762 return -EINVAL;
765 return 0;
768 static bool vlan_default_pvid(struct net_bridge_vlan_group *vg, u16 vid)
770 struct net_bridge_vlan *v;
772 if (vid != vg->pvid)
773 return false;
775 v = br_vlan_lookup(&vg->vlan_hash, vid);
776 if (v && br_vlan_should_use(v) &&
777 (v->flags & BRIDGE_VLAN_INFO_UNTAGGED))
778 return true;
780 return false;
783 static void br_vlan_disable_default_pvid(struct net_bridge *br)
785 struct net_bridge_port *p;
786 u16 pvid = br->default_pvid;
788 /* Disable default_pvid on all ports where it is still
789 * configured.
791 if (vlan_default_pvid(br_vlan_group(br), pvid))
792 br_vlan_delete(br, pvid);
794 list_for_each_entry(p, &br->port_list, list) {
795 if (vlan_default_pvid(nbp_vlan_group(p), pvid))
796 nbp_vlan_delete(p, pvid);
799 br->default_pvid = 0;
802 int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid)
804 const struct net_bridge_vlan *pvent;
805 struct net_bridge_vlan_group *vg;
806 struct net_bridge_port *p;
807 u16 old_pvid;
808 int err = 0;
809 unsigned long *changed;
811 if (!pvid) {
812 br_vlan_disable_default_pvid(br);
813 return 0;
816 changed = kcalloc(BITS_TO_LONGS(BR_MAX_PORTS), sizeof(unsigned long),
817 GFP_KERNEL);
818 if (!changed)
819 return -ENOMEM;
821 old_pvid = br->default_pvid;
823 /* Update default_pvid config only if we do not conflict with
824 * user configuration.
826 vg = br_vlan_group(br);
827 pvent = br_vlan_find(vg, pvid);
828 if ((!old_pvid || vlan_default_pvid(vg, old_pvid)) &&
829 (!pvent || !br_vlan_should_use(pvent))) {
830 err = br_vlan_add(br, pvid,
831 BRIDGE_VLAN_INFO_PVID |
832 BRIDGE_VLAN_INFO_UNTAGGED |
833 BRIDGE_VLAN_INFO_BRENTRY);
834 if (err)
835 goto out;
836 br_vlan_delete(br, old_pvid);
837 set_bit(0, changed);
840 list_for_each_entry(p, &br->port_list, list) {
841 /* Update default_pvid config only if we do not conflict with
842 * user configuration.
844 vg = nbp_vlan_group(p);
845 if ((old_pvid &&
846 !vlan_default_pvid(vg, old_pvid)) ||
847 br_vlan_find(vg, pvid))
848 continue;
850 err = nbp_vlan_add(p, pvid,
851 BRIDGE_VLAN_INFO_PVID |
852 BRIDGE_VLAN_INFO_UNTAGGED);
853 if (err)
854 goto err_port;
855 nbp_vlan_delete(p, old_pvid);
856 set_bit(p->port_no, changed);
859 br->default_pvid = pvid;
861 out:
862 kfree(changed);
863 return err;
865 err_port:
866 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
867 if (!test_bit(p->port_no, changed))
868 continue;
870 if (old_pvid)
871 nbp_vlan_add(p, old_pvid,
872 BRIDGE_VLAN_INFO_PVID |
873 BRIDGE_VLAN_INFO_UNTAGGED);
874 nbp_vlan_delete(p, pvid);
877 if (test_bit(0, changed)) {
878 if (old_pvid)
879 br_vlan_add(br, old_pvid,
880 BRIDGE_VLAN_INFO_PVID |
881 BRIDGE_VLAN_INFO_UNTAGGED |
882 BRIDGE_VLAN_INFO_BRENTRY);
883 br_vlan_delete(br, pvid);
885 goto out;
888 int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val)
890 u16 pvid = val;
891 int err = 0;
893 if (val >= VLAN_VID_MASK)
894 return -EINVAL;
896 if (pvid == br->default_pvid)
897 goto out;
899 /* Only allow default pvid change when filtering is disabled */
900 if (br->vlan_enabled) {
901 pr_info_once("Please disable vlan filtering to change default_pvid\n");
902 err = -EPERM;
903 goto out;
905 err = __br_vlan_set_default_pvid(br, pvid);
906 out:
907 return err;
910 int br_vlan_init(struct net_bridge *br)
912 struct net_bridge_vlan_group *vg;
913 int ret = -ENOMEM;
915 vg = kzalloc(sizeof(*vg), GFP_KERNEL);
916 if (!vg)
917 goto out;
918 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
919 if (ret)
920 goto err_rhtbl;
921 INIT_LIST_HEAD(&vg->vlan_list);
922 br->vlan_proto = htons(ETH_P_8021Q);
923 br->default_pvid = 1;
924 rcu_assign_pointer(br->vlgrp, vg);
925 ret = br_vlan_add(br, 1,
926 BRIDGE_VLAN_INFO_PVID | BRIDGE_VLAN_INFO_UNTAGGED |
927 BRIDGE_VLAN_INFO_BRENTRY);
928 if (ret)
929 goto err_vlan_add;
931 out:
932 return ret;
934 err_vlan_add:
935 rhashtable_destroy(&vg->vlan_hash);
936 err_rhtbl:
937 kfree(vg);
939 goto out;
942 int nbp_vlan_init(struct net_bridge_port *p)
944 struct switchdev_attr attr = {
945 .orig_dev = p->br->dev,
946 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
947 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
948 .u.vlan_filtering = p->br->vlan_enabled,
950 struct net_bridge_vlan_group *vg;
951 int ret = -ENOMEM;
953 vg = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL);
954 if (!vg)
955 goto out;
957 ret = switchdev_port_attr_set(p->dev, &attr);
958 if (ret && ret != -EOPNOTSUPP)
959 goto err_vlan_enabled;
961 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
962 if (ret)
963 goto err_rhtbl;
964 INIT_LIST_HEAD(&vg->vlan_list);
965 rcu_assign_pointer(p->vlgrp, vg);
966 if (p->br->default_pvid) {
967 ret = nbp_vlan_add(p, p->br->default_pvid,
968 BRIDGE_VLAN_INFO_PVID |
969 BRIDGE_VLAN_INFO_UNTAGGED);
970 if (ret)
971 goto err_vlan_add;
973 out:
974 return ret;
976 err_vlan_add:
977 RCU_INIT_POINTER(p->vlgrp, NULL);
978 synchronize_rcu();
979 rhashtable_destroy(&vg->vlan_hash);
980 err_vlan_enabled:
981 err_rhtbl:
982 kfree(vg);
984 goto out;
987 /* Must be protected by RTNL.
988 * Must be called with vid in range from 1 to 4094 inclusive.
990 int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags)
992 struct switchdev_obj_port_vlan v = {
993 .obj.orig_dev = port->dev,
994 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
995 .flags = flags,
996 .vid_begin = vid,
997 .vid_end = vid,
999 struct net_bridge_vlan *vlan;
1000 int ret;
1002 ASSERT_RTNL();
1004 vlan = br_vlan_find(nbp_vlan_group(port), vid);
1005 if (vlan) {
1006 /* Pass the flags to the hardware bridge */
1007 ret = switchdev_port_obj_add(port->dev, &v.obj);
1008 if (ret && ret != -EOPNOTSUPP)
1009 return ret;
1010 __vlan_add_flags(vlan, flags);
1011 return 0;
1014 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
1015 if (!vlan)
1016 return -ENOMEM;
1018 vlan->vid = vid;
1019 vlan->port = port;
1020 ret = __vlan_add(vlan, flags);
1021 if (ret)
1022 kfree(vlan);
1024 return ret;
1027 /* Must be protected by RTNL.
1028 * Must be called with vid in range from 1 to 4094 inclusive.
1030 int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
1032 struct net_bridge_vlan *v;
1034 ASSERT_RTNL();
1036 v = br_vlan_find(nbp_vlan_group(port), vid);
1037 if (!v)
1038 return -ENOENT;
1039 br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
1040 br_fdb_delete_by_port(port->br, port, vid, 0);
1042 return __vlan_del(v);
1045 void nbp_vlan_flush(struct net_bridge_port *port)
1047 struct net_bridge_vlan_group *vg;
1049 ASSERT_RTNL();
1051 vg = nbp_vlan_group(port);
1052 __vlan_flush(vg);
1053 RCU_INIT_POINTER(port->vlgrp, NULL);
1054 synchronize_rcu();
1055 __vlan_group_free(vg);
1058 void br_vlan_get_stats(const struct net_bridge_vlan *v,
1059 struct br_vlan_stats *stats)
1061 int i;
1063 memset(stats, 0, sizeof(*stats));
1064 for_each_possible_cpu(i) {
1065 u64 rxpackets, rxbytes, txpackets, txbytes;
1066 struct br_vlan_stats *cpu_stats;
1067 unsigned int start;
1069 cpu_stats = per_cpu_ptr(v->stats, i);
1070 do {
1071 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
1072 rxpackets = cpu_stats->rx_packets;
1073 rxbytes = cpu_stats->rx_bytes;
1074 txbytes = cpu_stats->tx_bytes;
1075 txpackets = cpu_stats->tx_packets;
1076 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
1078 stats->rx_packets += rxpackets;
1079 stats->rx_bytes += rxbytes;
1080 stats->tx_bytes += txbytes;
1081 stats->tx_packets += txpackets;