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,
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),
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)
43 static void __vlan_delete_pvid(struct net_bridge_vlan_group *vg, u16 vid)
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))
61 if (flags & BRIDGE_VLAN_INFO_PVID)
62 __vlan_add_pvid(vg, v->vid);
64 __vlan_delete_pvid(vg, v->vid);
66 if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
67 v->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
69 v->flags &= ~BRIDGE_VLAN_INFO_UNTAGGED;
72 static int __vlan_vid_add(struct net_device *dev, struct net_bridge *br,
75 const struct net_device_ops *ops = dev->netdev_ops;
78 /* If driver uses VLAN ndo ops, use 8021q to install vid
79 * on device, otherwise try switchdev ops to install vid.
82 if (ops->ndo_vlan_rx_add_vid) {
83 err = vlan_vid_add(dev, br->vlan_proto, vid);
85 struct switchdev_obj_port_vlan v = {
86 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
92 err = switchdev_port_obj_add(dev, &v.obj);
93 if (err == -EOPNOTSUPP)
100 static void __vlan_add_list(struct net_bridge_vlan *v)
102 struct list_head *headp, *hpos;
103 struct net_bridge_vlan *vent;
105 headp = br_vlan_is_master(v) ? &v->br->vlgrp->vlan_list :
106 &v->port->vlgrp->vlan_list;
107 list_for_each_prev(hpos, headp) {
108 vent = list_entry(hpos, struct net_bridge_vlan, vlist);
109 if (v->vid < vent->vid)
114 list_add_rcu(&v->vlist, hpos);
117 static void __vlan_del_list(struct net_bridge_vlan *v)
119 list_del_rcu(&v->vlist);
122 static int __vlan_vid_del(struct net_device *dev, struct net_bridge *br,
125 const struct net_device_ops *ops = dev->netdev_ops;
128 /* If driver uses VLAN ndo ops, use 8021q to delete vid
129 * on device, otherwise try switchdev ops to delete vid.
132 if (ops->ndo_vlan_rx_kill_vid) {
133 vlan_vid_del(dev, br->vlan_proto, vid);
135 struct switchdev_obj_port_vlan v = {
136 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
141 err = switchdev_port_obj_del(dev, &v.obj);
142 if (err == -EOPNOTSUPP)
149 /* Returns a master vlan, if it didn't exist it gets created. In all cases a
150 * a reference is taken to the master vlan before returning.
152 static struct net_bridge_vlan *br_vlan_get_master(struct net_bridge *br, u16 vid)
154 struct net_bridge_vlan *masterv;
156 masterv = br_vlan_find(br->vlgrp, vid);
158 /* missing global ctx, create it now */
159 if (br_vlan_add(br, vid, 0))
161 masterv = br_vlan_find(br->vlgrp, vid);
162 if (WARN_ON(!masterv))
165 atomic_inc(&masterv->refcnt);
170 static void br_vlan_put_master(struct net_bridge_vlan *masterv)
172 if (!br_vlan_is_master(masterv))
175 if (atomic_dec_and_test(&masterv->refcnt)) {
176 rhashtable_remove_fast(&masterv->br->vlgrp->vlan_hash,
177 &masterv->vnode, br_vlan_rht_params);
178 __vlan_del_list(masterv);
179 kfree_rcu(masterv, rcu);
183 /* This is the shared VLAN add function which works for both ports and bridge
184 * devices. There are four possible calls to this function in terms of the
186 * 1. vlan is being added on a port (no master flags, global entry exists)
187 * 2. vlan is being added on a bridge (both master and brvlan flags)
188 * 3. vlan is being added on a port, but a global entry didn't exist which
189 * is being created right now (master flag set, brvlan flag unset), the
190 * global entry is used for global per-vlan features, but not for filtering
191 * 4. same as 3 but with both master and brvlan flags set so the entry
192 * will be used for filtering in both the port and the bridge
194 static int __vlan_add(struct net_bridge_vlan *v, u16 flags)
196 struct net_bridge_vlan *masterv = NULL;
197 struct net_bridge_port *p = NULL;
198 struct net_bridge_vlan_group *vg;
199 struct net_device *dev;
200 struct net_bridge *br;
203 if (br_vlan_is_master(v)) {
215 /* Add VLAN to the device filter if it is supported.
216 * This ensures tagged traffic enters the bridge when
217 * promiscuous mode is disabled by br_manage_promisc().
219 err = __vlan_vid_add(dev, br, v->vid, flags);
223 /* need to work on the master vlan too */
224 if (flags & BRIDGE_VLAN_INFO_MASTER) {
225 err = br_vlan_add(br, v->vid, flags |
226 BRIDGE_VLAN_INFO_BRENTRY);
231 masterv = br_vlan_get_master(br, v->vid);
237 /* Add the dev mac and count the vlan only if it's usable */
238 if (br_vlan_should_use(v)) {
239 err = br_fdb_insert(br, p, dev->dev_addr, v->vid);
241 br_err(br, "failed insert local address into bridge forwarding table\n");
247 err = rhashtable_lookup_insert_fast(&vg->vlan_hash, &v->vnode,
253 __vlan_add_flags(v, flags);
258 if (br_vlan_should_use(v)) {
259 br_fdb_find_delete_local(br, p, dev->dev_addr, v->vid);
265 __vlan_vid_del(dev, br, v->vid);
267 br_vlan_put_master(masterv);
275 static int __vlan_del(struct net_bridge_vlan *v)
277 struct net_bridge_vlan *masterv = v;
278 struct net_bridge_vlan_group *vg;
279 struct net_bridge_port *p = NULL;
282 if (br_vlan_is_master(v)) {
290 __vlan_delete_pvid(vg, v->vid);
292 err = __vlan_vid_del(p->dev, p->br, v->vid);
297 if (br_vlan_should_use(v)) {
298 v->flags &= ~BRIDGE_VLAN_INFO_BRENTRY;
303 rhashtable_remove_fast(&vg->vlan_hash, &v->vnode,
309 br_vlan_put_master(masterv);
314 static void __vlan_flush(struct net_bridge_vlan_group *vlgrp)
316 struct net_bridge_vlan *vlan, *tmp;
318 __vlan_delete_pvid(vlgrp, vlgrp->pvid);
319 list_for_each_entry_safe(vlan, tmp, &vlgrp->vlan_list, vlist)
321 rhashtable_destroy(&vlgrp->vlan_hash);
325 struct sk_buff *br_handle_vlan(struct net_bridge *br,
326 struct net_bridge_vlan_group *vg,
329 struct net_bridge_vlan *v;
332 /* If this packet was not filtered at input, let it pass */
333 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
336 /* At this point, we know that the frame was filtered and contains
337 * a valid vlan id. If the vlan id has untagged flag set,
338 * send untagged; otherwise, send tagged.
340 br_vlan_get_tag(skb, &vid);
341 v = br_vlan_find(vg, vid);
342 /* Vlan entry must be configured at this point. The
343 * only exception is the bridge is set in promisc mode and the
344 * packet is destined for the bridge device. In this case
345 * pass the packet as is.
347 if (!v || !br_vlan_should_use(v)) {
348 if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) {
355 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
362 /* Called under RCU */
363 static bool __allowed_ingress(struct net_bridge_vlan_group *vg, __be16 proto,
364 struct sk_buff *skb, u16 *vid)
366 const struct net_bridge_vlan *v;
369 BR_INPUT_SKB_CB(skb)->vlan_filtered = true;
370 /* If vlan tx offload is disabled on bridge device and frame was
371 * sent from vlan device on the bridge device, it does not have
372 * HW accelerated vlan tag.
374 if (unlikely(!skb_vlan_tag_present(skb) &&
375 skb->protocol == proto)) {
376 skb = skb_vlan_untag(skb);
381 if (!br_vlan_get_tag(skb, vid)) {
383 if (skb->vlan_proto != proto) {
384 /* Protocol-mismatch, empty out vlan_tci for new tag */
385 skb_push(skb, ETH_HLEN);
386 skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
387 skb_vlan_tag_get(skb));
391 skb_pull(skb, ETH_HLEN);
392 skb_reset_mac_len(skb);
404 u16 pvid = br_get_pvid(vg);
406 /* Frame had a tag with VID 0 or did not have a tag.
407 * See if pvid is set on this port. That tells us which
408 * vlan untagged or priority-tagged traffic belongs to.
413 /* PVID is set on this port. Any untagged or priority-tagged
414 * ingress frame is considered to belong to this vlan.
418 /* Untagged Frame. */
419 __vlan_hwaccel_put_tag(skb, proto, pvid);
421 /* Priority-tagged Frame.
422 * At this point, We know that skb->vlan_tci had
423 * VLAN_TAG_PRESENT bit and its VID field was 0x000.
424 * We update only VID field and preserve PCP field.
426 skb->vlan_tci |= pvid;
431 /* Frame had a valid vlan tag. See if vlan is allowed */
432 v = br_vlan_find(vg, *vid);
433 if (v && br_vlan_should_use(v))
440 bool br_allowed_ingress(const struct net_bridge *br,
441 struct net_bridge_vlan_group *vg, struct sk_buff *skb,
444 /* If VLAN filtering is disabled on the bridge, all packets are
447 if (!br->vlan_enabled) {
448 BR_INPUT_SKB_CB(skb)->vlan_filtered = false;
452 return __allowed_ingress(vg, br->vlan_proto, skb, vid);
455 /* Called under RCU. */
456 bool br_allowed_egress(struct net_bridge_vlan_group *vg,
457 const struct sk_buff *skb)
459 const struct net_bridge_vlan *v;
462 /* If this packet was not filtered at input, let it pass */
463 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
466 br_vlan_get_tag(skb, &vid);
467 v = br_vlan_find(vg, vid);
468 if (v && br_vlan_should_use(v))
474 /* Called under RCU */
475 bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid)
477 struct net_bridge_vlan_group *vg;
478 struct net_bridge *br = p->br;
480 /* If filtering was disabled at input, let it pass. */
481 if (!br->vlan_enabled)
485 if (!vg || !vg->num_vlans)
488 if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto)
492 *vid = br_get_pvid(vg);
499 if (br_vlan_find(vg, *vid))
505 /* Must be protected by RTNL.
506 * Must be called with vid in range from 1 to 4094 inclusive.
508 int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags)
510 struct net_bridge_vlan *vlan;
515 vlan = br_vlan_find(br->vlgrp, vid);
517 if (!br_vlan_is_brentry(vlan)) {
518 /* Trying to change flags of non-existent bridge vlan */
519 if (!(flags & BRIDGE_VLAN_INFO_BRENTRY))
521 /* It was only kept for port vlans, now make it real */
522 ret = br_fdb_insert(br, NULL, br->dev->dev_addr,
525 br_err(br, "failed insert local address into bridge forwarding table\n");
528 atomic_inc(&vlan->refcnt);
529 vlan->flags |= BRIDGE_VLAN_INFO_BRENTRY;
530 br->vlgrp->num_vlans++;
532 __vlan_add_flags(vlan, flags);
536 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
541 vlan->flags = flags | BRIDGE_VLAN_INFO_MASTER;
542 vlan->flags &= ~BRIDGE_VLAN_INFO_PVID;
544 if (flags & BRIDGE_VLAN_INFO_BRENTRY)
545 atomic_set(&vlan->refcnt, 1);
546 ret = __vlan_add(vlan, flags);
553 /* Must be protected by RTNL.
554 * Must be called with vid in range from 1 to 4094 inclusive.
556 int br_vlan_delete(struct net_bridge *br, u16 vid)
558 struct net_bridge_vlan *v;
562 v = br_vlan_find(br->vlgrp, vid);
563 if (!v || !br_vlan_is_brentry(v))
566 br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
568 return __vlan_del(v);
571 void br_vlan_flush(struct net_bridge *br)
575 __vlan_flush(br_vlan_group(br));
578 struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid)
583 return br_vlan_lookup(&vg->vlan_hash, vid);
586 /* Must be protected by RTNL. */
587 static void recalculate_group_addr(struct net_bridge *br)
589 if (br->group_addr_set)
592 spin_lock_bh(&br->lock);
593 if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q)) {
594 /* Bridge Group Address */
595 br->group_addr[5] = 0x00;
596 } else { /* vlan_enabled && ETH_P_8021AD */
597 /* Provider Bridge Group Address */
598 br->group_addr[5] = 0x08;
600 spin_unlock_bh(&br->lock);
603 /* Must be protected by RTNL. */
604 void br_recalculate_fwd_mask(struct net_bridge *br)
606 if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q))
607 br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT;
608 else /* vlan_enabled && ETH_P_8021AD */
609 br->group_fwd_mask_required = BR_GROUPFWD_8021AD &
610 ~(1u << br->group_addr[5]);
613 int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
615 if (br->vlan_enabled == val)
618 br->vlan_enabled = val;
619 br_manage_promisc(br);
620 recalculate_group_addr(br);
621 br_recalculate_fwd_mask(br);
626 int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
629 return restart_syscall();
631 __br_vlan_filter_toggle(br, val);
637 int __br_vlan_set_proto(struct net_bridge *br, __be16 proto)
640 struct net_bridge_port *p;
641 struct net_bridge_vlan *vlan;
644 if (br->vlan_proto == proto)
647 /* Add VLANs for the new proto to the device filter. */
648 list_for_each_entry(p, &br->port_list, list) {
649 list_for_each_entry(vlan, &p->vlgrp->vlan_list, vlist) {
650 err = vlan_vid_add(p->dev, proto, vlan->vid);
656 oldproto = br->vlan_proto;
657 br->vlan_proto = proto;
659 recalculate_group_addr(br);
660 br_recalculate_fwd_mask(br);
662 /* Delete VLANs for the old proto from the device filter. */
663 list_for_each_entry(p, &br->port_list, list)
664 list_for_each_entry(vlan, &p->vlgrp->vlan_list, vlist)
665 vlan_vid_del(p->dev, oldproto, vlan->vid);
670 list_for_each_entry_continue_reverse(vlan, &p->vlgrp->vlan_list, vlist)
671 vlan_vid_del(p->dev, proto, vlan->vid);
673 list_for_each_entry_continue_reverse(p, &br->port_list, list)
674 list_for_each_entry(vlan, &p->vlgrp->vlan_list, vlist)
675 vlan_vid_del(p->dev, proto, vlan->vid);
680 int br_vlan_set_proto(struct net_bridge *br, unsigned long val)
684 if (val != ETH_P_8021Q && val != ETH_P_8021AD)
685 return -EPROTONOSUPPORT;
688 return restart_syscall();
690 err = __br_vlan_set_proto(br, htons(val));
696 static bool vlan_default_pvid(struct net_bridge_vlan_group *vg, u16 vid)
698 struct net_bridge_vlan *v;
703 v = br_vlan_lookup(&vg->vlan_hash, vid);
704 if (v && br_vlan_should_use(v) &&
705 (v->flags & BRIDGE_VLAN_INFO_UNTAGGED))
711 static void br_vlan_disable_default_pvid(struct net_bridge *br)
713 struct net_bridge_port *p;
714 u16 pvid = br->default_pvid;
716 /* Disable default_pvid on all ports where it is still
719 if (vlan_default_pvid(br->vlgrp, pvid))
720 br_vlan_delete(br, pvid);
722 list_for_each_entry(p, &br->port_list, list) {
723 if (vlan_default_pvid(p->vlgrp, pvid))
724 nbp_vlan_delete(p, pvid);
727 br->default_pvid = 0;
730 static int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid)
732 const struct net_bridge_vlan *pvent;
733 struct net_bridge_port *p;
736 unsigned long *changed;
738 changed = kcalloc(BITS_TO_LONGS(BR_MAX_PORTS), sizeof(unsigned long),
743 old_pvid = br->default_pvid;
745 /* Update default_pvid config only if we do not conflict with
746 * user configuration.
748 pvent = br_vlan_find(br->vlgrp, pvid);
749 if ((!old_pvid || vlan_default_pvid(br->vlgrp, old_pvid)) &&
750 (!pvent || !br_vlan_should_use(pvent))) {
751 err = br_vlan_add(br, pvid,
752 BRIDGE_VLAN_INFO_PVID |
753 BRIDGE_VLAN_INFO_UNTAGGED |
754 BRIDGE_VLAN_INFO_BRENTRY);
757 br_vlan_delete(br, old_pvid);
761 list_for_each_entry(p, &br->port_list, list) {
762 /* Update default_pvid config only if we do not conflict with
763 * user configuration.
766 !vlan_default_pvid(p->vlgrp, old_pvid)) ||
767 br_vlan_find(p->vlgrp, pvid))
770 err = nbp_vlan_add(p, pvid,
771 BRIDGE_VLAN_INFO_PVID |
772 BRIDGE_VLAN_INFO_UNTAGGED);
775 nbp_vlan_delete(p, old_pvid);
776 set_bit(p->port_no, changed);
779 br->default_pvid = pvid;
786 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
787 if (!test_bit(p->port_no, changed))
791 nbp_vlan_add(p, old_pvid,
792 BRIDGE_VLAN_INFO_PVID |
793 BRIDGE_VLAN_INFO_UNTAGGED);
794 nbp_vlan_delete(p, pvid);
797 if (test_bit(0, changed)) {
799 br_vlan_add(br, old_pvid,
800 BRIDGE_VLAN_INFO_PVID |
801 BRIDGE_VLAN_INFO_UNTAGGED |
802 BRIDGE_VLAN_INFO_BRENTRY);
803 br_vlan_delete(br, pvid);
808 int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val)
813 if (val >= VLAN_VID_MASK)
817 return restart_syscall();
819 if (pvid == br->default_pvid)
822 /* Only allow default pvid change when filtering is disabled */
823 if (br->vlan_enabled) {
824 pr_info_once("Please disable vlan filtering to change default_pvid\n");
830 br_vlan_disable_default_pvid(br);
832 err = __br_vlan_set_default_pvid(br, pvid);
839 int br_vlan_init(struct net_bridge *br)
843 br->vlgrp = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL);
846 ret = rhashtable_init(&br->vlgrp->vlan_hash, &br_vlan_rht_params);
849 INIT_LIST_HEAD(&br->vlgrp->vlan_list);
850 br->vlan_proto = htons(ETH_P_8021Q);
851 br->default_pvid = 1;
852 ret = br_vlan_add(br, 1,
853 BRIDGE_VLAN_INFO_PVID | BRIDGE_VLAN_INFO_UNTAGGED |
854 BRIDGE_VLAN_INFO_BRENTRY);
862 rhashtable_destroy(&br->vlgrp->vlan_hash);
869 int nbp_vlan_init(struct net_bridge_port *p)
871 struct net_bridge_vlan_group *vg;
874 vg = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL);
878 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
881 INIT_LIST_HEAD(&vg->vlan_list);
882 /* Make sure everything's committed before publishing vg */
885 if (p->br->default_pvid) {
886 ret = nbp_vlan_add(p, p->br->default_pvid,
887 BRIDGE_VLAN_INFO_PVID |
888 BRIDGE_VLAN_INFO_UNTAGGED);
896 rhashtable_destroy(&vg->vlan_hash);
903 /* Must be protected by RTNL.
904 * Must be called with vid in range from 1 to 4094 inclusive.
906 int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags)
908 struct net_bridge_vlan *vlan;
913 vlan = br_vlan_find(port->vlgrp, vid);
915 __vlan_add_flags(vlan, flags);
919 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
925 ret = __vlan_add(vlan, flags);
932 /* Must be protected by RTNL.
933 * Must be called with vid in range from 1 to 4094 inclusive.
935 int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
937 struct net_bridge_vlan *v;
941 v = br_vlan_find(port->vlgrp, vid);
944 br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
945 br_fdb_delete_by_port(port->br, port, vid, 0);
947 return __vlan_del(v);
950 void nbp_vlan_flush(struct net_bridge_port *port)
952 struct net_bridge_vlan *vlan;
956 list_for_each_entry(vlan, &port->vlgrp->vlan_list, vlist)
957 vlan_vid_del(port->dev, port->br->vlan_proto, vlan->vid);
959 __vlan_flush(nbp_vlan_group(port));