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))
57 vg = br_vlan_group(v->br);
59 vg = nbp_vlan_group(v->port);
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 struct switchdev_obj_port_vlan v = {
76 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
83 /* Try switchdev op first. In case it is not supported, fallback to
86 err = switchdev_port_obj_add(dev, &v.obj);
87 if (err == -EOPNOTSUPP)
88 return vlan_vid_add(dev, br->vlan_proto, vid);
92 static void __vlan_add_list(struct net_bridge_vlan *v)
94 struct net_bridge_vlan_group *vg;
95 struct list_head *headp, *hpos;
96 struct net_bridge_vlan *vent;
98 if (br_vlan_is_master(v))
99 vg = br_vlan_group(v->br);
101 vg = nbp_vlan_group(v->port);
103 headp = &vg->vlan_list;
104 list_for_each_prev(hpos, headp) {
105 vent = list_entry(hpos, struct net_bridge_vlan, vlist);
106 if (v->vid < vent->vid)
111 list_add_rcu(&v->vlist, hpos);
114 static void __vlan_del_list(struct net_bridge_vlan *v)
116 list_del_rcu(&v->vlist);
119 static int __vlan_vid_del(struct net_device *dev, struct net_bridge *br,
122 struct switchdev_obj_port_vlan v = {
123 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
129 /* Try switchdev op first. In case it is not supported, fallback to
132 err = switchdev_port_obj_del(dev, &v.obj);
133 if (err == -EOPNOTSUPP) {
134 vlan_vid_del(dev, br->vlan_proto, vid);
140 /* Returns a master vlan, if it didn't exist it gets created. In all cases a
141 * a reference is taken to the master vlan before returning.
143 static struct net_bridge_vlan *br_vlan_get_master(struct net_bridge *br, u16 vid)
145 struct net_bridge_vlan_group *vg;
146 struct net_bridge_vlan *masterv;
148 vg = br_vlan_group(br);
149 masterv = br_vlan_find(vg, vid);
151 /* missing global ctx, create it now */
152 if (br_vlan_add(br, vid, 0))
154 masterv = br_vlan_find(vg, vid);
155 if (WARN_ON(!masterv))
158 atomic_inc(&masterv->refcnt);
163 static void br_vlan_put_master(struct net_bridge_vlan *masterv)
165 struct net_bridge_vlan_group *vg;
167 if (!br_vlan_is_master(masterv))
170 vg = br_vlan_group(masterv->br);
171 if (atomic_dec_and_test(&masterv->refcnt)) {
172 rhashtable_remove_fast(&vg->vlan_hash,
173 &masterv->vnode, br_vlan_rht_params);
174 __vlan_del_list(masterv);
175 kfree_rcu(masterv, rcu);
179 /* This is the shared VLAN add function which works for both ports and bridge
180 * devices. There are four possible calls to this function in terms of the
182 * 1. vlan is being added on a port (no master flags, global entry exists)
183 * 2. vlan is being added on a bridge (both master and brentry flags)
184 * 3. vlan is being added on a port, but a global entry didn't exist which
185 * is being created right now (master flag set, brentry flag unset), the
186 * global entry is used for global per-vlan features, but not for filtering
187 * 4. same as 3 but with both master and brentry flags set so the entry
188 * will be used for filtering in both the port and the bridge
190 static int __vlan_add(struct net_bridge_vlan *v, u16 flags)
192 struct net_bridge_vlan *masterv = NULL;
193 struct net_bridge_port *p = NULL;
194 struct net_bridge_vlan_group *vg;
195 struct net_device *dev;
196 struct net_bridge *br;
199 if (br_vlan_is_master(v)) {
202 vg = br_vlan_group(br);
207 vg = nbp_vlan_group(p);
211 /* Add VLAN to the device filter if it is supported.
212 * This ensures tagged traffic enters the bridge when
213 * promiscuous mode is disabled by br_manage_promisc().
215 err = __vlan_vid_add(dev, br, v->vid, flags);
219 /* need to work on the master vlan too */
220 if (flags & BRIDGE_VLAN_INFO_MASTER) {
221 err = br_vlan_add(br, v->vid, flags |
222 BRIDGE_VLAN_INFO_BRENTRY);
227 masterv = br_vlan_get_master(br, v->vid);
235 /* Add the dev mac and count the vlan only if it's usable */
236 if (br_vlan_should_use(v)) {
237 err = br_fdb_insert(br, p, dev->dev_addr, v->vid);
239 br_err(br, "failed insert local address into bridge forwarding table\n");
245 err = rhashtable_lookup_insert_fast(&vg->vlan_hash, &v->vnode,
251 __vlan_add_flags(v, flags);
256 if (br_vlan_should_use(v)) {
257 br_fdb_find_delete_local(br, p, dev->dev_addr, v->vid);
263 __vlan_vid_del(dev, br, v->vid);
265 br_vlan_put_master(masterv);
273 static int __vlan_del(struct net_bridge_vlan *v)
275 struct net_bridge_vlan *masterv = v;
276 struct net_bridge_vlan_group *vg;
277 struct net_bridge_port *p = NULL;
280 if (br_vlan_is_master(v)) {
281 vg = br_vlan_group(v->br);
284 vg = nbp_vlan_group(v->port);
288 __vlan_delete_pvid(vg, v->vid);
290 err = __vlan_vid_del(p->dev, p->br, v->vid);
295 if (br_vlan_should_use(v)) {
296 v->flags &= ~BRIDGE_VLAN_INFO_BRENTRY;
301 rhashtable_remove_fast(&vg->vlan_hash, &v->vnode,
307 br_vlan_put_master(masterv);
312 static void __vlan_group_free(struct net_bridge_vlan_group *vg)
314 WARN_ON(!list_empty(&vg->vlan_list));
315 rhashtable_destroy(&vg->vlan_hash);
319 static void __vlan_flush(struct net_bridge_vlan_group *vg)
321 struct net_bridge_vlan *vlan, *tmp;
323 __vlan_delete_pvid(vg, vg->pvid);
324 list_for_each_entry_safe(vlan, tmp, &vg->vlan_list, vlist)
328 struct sk_buff *br_handle_vlan(struct net_bridge *br,
329 struct net_bridge_vlan_group *vg,
332 struct net_bridge_vlan *v;
335 /* If this packet was not filtered at input, let it pass */
336 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
339 /* At this point, we know that the frame was filtered and contains
340 * a valid vlan id. If the vlan id has untagged flag set,
341 * send untagged; otherwise, send tagged.
343 br_vlan_get_tag(skb, &vid);
344 v = br_vlan_find(vg, vid);
345 /* Vlan entry must be configured at this point. The
346 * only exception is the bridge is set in promisc mode and the
347 * packet is destined for the bridge device. In this case
348 * pass the packet as is.
350 if (!v || !br_vlan_should_use(v)) {
351 if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) {
358 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
365 /* Called under RCU */
366 static bool __allowed_ingress(struct net_bridge_vlan_group *vg, __be16 proto,
367 struct sk_buff *skb, u16 *vid)
369 const struct net_bridge_vlan *v;
372 BR_INPUT_SKB_CB(skb)->vlan_filtered = true;
373 /* If vlan tx offload is disabled on bridge device and frame was
374 * sent from vlan device on the bridge device, it does not have
375 * HW accelerated vlan tag.
377 if (unlikely(!skb_vlan_tag_present(skb) &&
378 skb->protocol == proto)) {
379 skb = skb_vlan_untag(skb);
384 if (!br_vlan_get_tag(skb, vid)) {
386 if (skb->vlan_proto != proto) {
387 /* Protocol-mismatch, empty out vlan_tci for new tag */
388 skb_push(skb, ETH_HLEN);
389 skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
390 skb_vlan_tag_get(skb));
394 skb_pull(skb, ETH_HLEN);
395 skb_reset_mac_len(skb);
407 u16 pvid = br_get_pvid(vg);
409 /* Frame had a tag with VID 0 or did not have a tag.
410 * See if pvid is set on this port. That tells us which
411 * vlan untagged or priority-tagged traffic belongs to.
416 /* PVID is set on this port. Any untagged or priority-tagged
417 * ingress frame is considered to belong to this vlan.
421 /* Untagged Frame. */
422 __vlan_hwaccel_put_tag(skb, proto, pvid);
424 /* Priority-tagged Frame.
425 * At this point, We know that skb->vlan_tci had
426 * VLAN_TAG_PRESENT bit and its VID field was 0x000.
427 * We update only VID field and preserve PCP field.
429 skb->vlan_tci |= pvid;
434 /* Frame had a valid vlan tag. See if vlan is allowed */
435 v = br_vlan_find(vg, *vid);
436 if (v && br_vlan_should_use(v))
443 bool br_allowed_ingress(const struct net_bridge *br,
444 struct net_bridge_vlan_group *vg, struct sk_buff *skb,
447 /* If VLAN filtering is disabled on the bridge, all packets are
450 if (!br->vlan_enabled) {
451 BR_INPUT_SKB_CB(skb)->vlan_filtered = false;
455 return __allowed_ingress(vg, br->vlan_proto, skb, vid);
458 /* Called under RCU. */
459 bool br_allowed_egress(struct net_bridge_vlan_group *vg,
460 const struct sk_buff *skb)
462 const struct net_bridge_vlan *v;
465 /* If this packet was not filtered at input, let it pass */
466 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
469 br_vlan_get_tag(skb, &vid);
470 v = br_vlan_find(vg, vid);
471 if (v && br_vlan_should_use(v))
477 /* Called under RCU */
478 bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid)
480 struct net_bridge_vlan_group *vg;
481 struct net_bridge *br = p->br;
483 /* If filtering was disabled at input, let it pass. */
484 if (!br->vlan_enabled)
487 vg = nbp_vlan_group_rcu(p);
488 if (!vg || !vg->num_vlans)
491 if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto)
495 *vid = br_get_pvid(vg);
502 if (br_vlan_find(vg, *vid))
508 /* Must be protected by RTNL.
509 * Must be called with vid in range from 1 to 4094 inclusive.
511 int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags)
513 struct net_bridge_vlan_group *vg;
514 struct net_bridge_vlan *vlan;
519 vg = br_vlan_group(br);
520 vlan = br_vlan_find(vg, vid);
522 if (!br_vlan_is_brentry(vlan)) {
523 /* Trying to change flags of non-existent bridge vlan */
524 if (!(flags & BRIDGE_VLAN_INFO_BRENTRY))
526 /* It was only kept for port vlans, now make it real */
527 ret = br_fdb_insert(br, NULL, br->dev->dev_addr,
530 br_err(br, "failed insert local address into bridge forwarding table\n");
533 atomic_inc(&vlan->refcnt);
534 vlan->flags |= BRIDGE_VLAN_INFO_BRENTRY;
537 __vlan_add_flags(vlan, flags);
541 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
546 vlan->flags = flags | BRIDGE_VLAN_INFO_MASTER;
547 vlan->flags &= ~BRIDGE_VLAN_INFO_PVID;
549 if (flags & BRIDGE_VLAN_INFO_BRENTRY)
550 atomic_set(&vlan->refcnt, 1);
551 ret = __vlan_add(vlan, flags);
558 /* Must be protected by RTNL.
559 * Must be called with vid in range from 1 to 4094 inclusive.
561 int br_vlan_delete(struct net_bridge *br, u16 vid)
563 struct net_bridge_vlan_group *vg;
564 struct net_bridge_vlan *v;
568 vg = br_vlan_group(br);
569 v = br_vlan_find(vg, vid);
570 if (!v || !br_vlan_is_brentry(v))
573 br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
574 br_fdb_delete_by_port(br, NULL, vid, 0);
576 return __vlan_del(v);
579 void br_vlan_flush(struct net_bridge *br)
581 struct net_bridge_vlan_group *vg;
585 /* delete auto-added default pvid local fdb before flushing vlans
586 * otherwise it will be leaked on bridge device init failure
588 br_fdb_delete_by_port(br, NULL, 0, 1);
590 vg = br_vlan_group(br);
592 RCU_INIT_POINTER(br->vlgrp, NULL);
594 __vlan_group_free(vg);
597 struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid)
602 return br_vlan_lookup(&vg->vlan_hash, vid);
605 /* Must be protected by RTNL. */
606 static void recalculate_group_addr(struct net_bridge *br)
608 if (br->group_addr_set)
611 spin_lock_bh(&br->lock);
612 if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q)) {
613 /* Bridge Group Address */
614 br->group_addr[5] = 0x00;
615 } else { /* vlan_enabled && ETH_P_8021AD */
616 /* Provider Bridge Group Address */
617 br->group_addr[5] = 0x08;
619 spin_unlock_bh(&br->lock);
622 /* Must be protected by RTNL. */
623 void br_recalculate_fwd_mask(struct net_bridge *br)
625 if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q))
626 br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT;
627 else /* vlan_enabled && ETH_P_8021AD */
628 br->group_fwd_mask_required = BR_GROUPFWD_8021AD &
629 ~(1u << br->group_addr[5]);
632 int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
634 if (br->vlan_enabled == val)
637 br->vlan_enabled = val;
638 br_manage_promisc(br);
639 recalculate_group_addr(br);
640 br_recalculate_fwd_mask(br);
645 int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
648 return restart_syscall();
650 __br_vlan_filter_toggle(br, val);
656 int __br_vlan_set_proto(struct net_bridge *br, __be16 proto)
659 struct net_bridge_port *p;
660 struct net_bridge_vlan *vlan;
661 struct net_bridge_vlan_group *vg;
664 if (br->vlan_proto == proto)
667 /* Add VLANs for the new proto to the device filter. */
668 list_for_each_entry(p, &br->port_list, list) {
669 vg = nbp_vlan_group(p);
670 list_for_each_entry(vlan, &vg->vlan_list, vlist) {
671 err = vlan_vid_add(p->dev, proto, vlan->vid);
677 oldproto = br->vlan_proto;
678 br->vlan_proto = proto;
680 recalculate_group_addr(br);
681 br_recalculate_fwd_mask(br);
683 /* Delete VLANs for the old proto from the device filter. */
684 list_for_each_entry(p, &br->port_list, list) {
685 vg = nbp_vlan_group(p);
686 list_for_each_entry(vlan, &vg->vlan_list, vlist)
687 vlan_vid_del(p->dev, oldproto, vlan->vid);
693 list_for_each_entry_continue_reverse(vlan, &vg->vlan_list, vlist)
694 vlan_vid_del(p->dev, proto, vlan->vid);
696 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
697 vg = nbp_vlan_group(p);
698 list_for_each_entry(vlan, &vg->vlan_list, vlist)
699 vlan_vid_del(p->dev, proto, vlan->vid);
705 int br_vlan_set_proto(struct net_bridge *br, unsigned long val)
709 if (val != ETH_P_8021Q && val != ETH_P_8021AD)
710 return -EPROTONOSUPPORT;
713 return restart_syscall();
715 err = __br_vlan_set_proto(br, htons(val));
721 static bool vlan_default_pvid(struct net_bridge_vlan_group *vg, u16 vid)
723 struct net_bridge_vlan *v;
728 v = br_vlan_lookup(&vg->vlan_hash, vid);
729 if (v && br_vlan_should_use(v) &&
730 (v->flags & BRIDGE_VLAN_INFO_UNTAGGED))
736 static void br_vlan_disable_default_pvid(struct net_bridge *br)
738 struct net_bridge_port *p;
739 u16 pvid = br->default_pvid;
741 /* Disable default_pvid on all ports where it is still
744 if (vlan_default_pvid(br_vlan_group(br), pvid))
745 br_vlan_delete(br, pvid);
747 list_for_each_entry(p, &br->port_list, list) {
748 if (vlan_default_pvid(nbp_vlan_group(p), pvid))
749 nbp_vlan_delete(p, pvid);
752 br->default_pvid = 0;
755 int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid)
757 const struct net_bridge_vlan *pvent;
758 struct net_bridge_vlan_group *vg;
759 struct net_bridge_port *p;
762 unsigned long *changed;
765 br_vlan_disable_default_pvid(br);
769 changed = kcalloc(BITS_TO_LONGS(BR_MAX_PORTS), sizeof(unsigned long),
774 old_pvid = br->default_pvid;
776 /* Update default_pvid config only if we do not conflict with
777 * user configuration.
779 vg = br_vlan_group(br);
780 pvent = br_vlan_find(vg, pvid);
781 if ((!old_pvid || vlan_default_pvid(vg, old_pvid)) &&
782 (!pvent || !br_vlan_should_use(pvent))) {
783 err = br_vlan_add(br, pvid,
784 BRIDGE_VLAN_INFO_PVID |
785 BRIDGE_VLAN_INFO_UNTAGGED |
786 BRIDGE_VLAN_INFO_BRENTRY);
789 br_vlan_delete(br, old_pvid);
793 list_for_each_entry(p, &br->port_list, list) {
794 /* Update default_pvid config only if we do not conflict with
795 * user configuration.
797 vg = nbp_vlan_group(p);
799 !vlan_default_pvid(vg, old_pvid)) ||
800 br_vlan_find(vg, pvid))
803 err = nbp_vlan_add(p, pvid,
804 BRIDGE_VLAN_INFO_PVID |
805 BRIDGE_VLAN_INFO_UNTAGGED);
808 nbp_vlan_delete(p, old_pvid);
809 set_bit(p->port_no, changed);
812 br->default_pvid = pvid;
819 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
820 if (!test_bit(p->port_no, changed))
824 nbp_vlan_add(p, old_pvid,
825 BRIDGE_VLAN_INFO_PVID |
826 BRIDGE_VLAN_INFO_UNTAGGED);
827 nbp_vlan_delete(p, pvid);
830 if (test_bit(0, changed)) {
832 br_vlan_add(br, old_pvid,
833 BRIDGE_VLAN_INFO_PVID |
834 BRIDGE_VLAN_INFO_UNTAGGED |
835 BRIDGE_VLAN_INFO_BRENTRY);
836 br_vlan_delete(br, pvid);
841 int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val)
846 if (val >= VLAN_VID_MASK)
850 return restart_syscall();
852 if (pvid == br->default_pvid)
855 /* Only allow default pvid change when filtering is disabled */
856 if (br->vlan_enabled) {
857 pr_info_once("Please disable vlan filtering to change default_pvid\n");
861 err = __br_vlan_set_default_pvid(br, pvid);
867 int br_vlan_init(struct net_bridge *br)
869 struct net_bridge_vlan_group *vg;
872 vg = kzalloc(sizeof(*vg), GFP_KERNEL);
875 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
878 INIT_LIST_HEAD(&vg->vlan_list);
879 br->vlan_proto = htons(ETH_P_8021Q);
880 br->default_pvid = 1;
881 rcu_assign_pointer(br->vlgrp, vg);
882 ret = br_vlan_add(br, 1,
883 BRIDGE_VLAN_INFO_PVID | BRIDGE_VLAN_INFO_UNTAGGED |
884 BRIDGE_VLAN_INFO_BRENTRY);
892 rhashtable_destroy(&vg->vlan_hash);
899 int nbp_vlan_init(struct net_bridge_port *p)
901 struct net_bridge_vlan_group *vg;
904 vg = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL);
908 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
911 INIT_LIST_HEAD(&vg->vlan_list);
912 rcu_assign_pointer(p->vlgrp, vg);
913 if (p->br->default_pvid) {
914 ret = nbp_vlan_add(p, p->br->default_pvid,
915 BRIDGE_VLAN_INFO_PVID |
916 BRIDGE_VLAN_INFO_UNTAGGED);
924 RCU_INIT_POINTER(p->vlgrp, NULL);
926 rhashtable_destroy(&vg->vlan_hash);
933 /* Must be protected by RTNL.
934 * Must be called with vid in range from 1 to 4094 inclusive.
936 int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags)
938 struct net_bridge_vlan *vlan;
943 vlan = br_vlan_find(nbp_vlan_group(port), vid);
945 __vlan_add_flags(vlan, flags);
949 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
955 ret = __vlan_add(vlan, flags);
962 /* Must be protected by RTNL.
963 * Must be called with vid in range from 1 to 4094 inclusive.
965 int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
967 struct net_bridge_vlan *v;
971 v = br_vlan_find(nbp_vlan_group(port), vid);
974 br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
975 br_fdb_delete_by_port(port->br, port, vid, 0);
977 return __vlan_del(v);
980 void nbp_vlan_flush(struct net_bridge_port *port)
982 struct net_bridge_vlan_group *vg;
986 vg = nbp_vlan_group(port);
988 RCU_INIT_POINTER(port->vlgrp, NULL);
990 __vlan_group_free(vg);