1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/kernel.h>
3 #include <linux/netdevice.h>
4 #include <linux/rtnetlink.h>
5 #include <linux/slab.h>
6 #include <net/switchdev.h>
8 #include "br_private.h"
9 #include "br_private_tunnel.h"
11 static void nbp_vlan_set_vlan_dev_state(struct net_bridge_port *p, u16 vid);
13 static inline int br_vlan_cmp(struct rhashtable_compare_arg *arg,
16 const struct net_bridge_vlan *vle = ptr;
17 u16 vid = *(u16 *)arg->key;
19 return vle->vid != vid;
22 static const struct rhashtable_params br_vlan_rht_params = {
23 .head_offset = offsetof(struct net_bridge_vlan, vnode),
24 .key_offset = offsetof(struct net_bridge_vlan, vid),
25 .key_len = sizeof(u16),
27 .max_size = VLAN_N_VID,
28 .obj_cmpfn = br_vlan_cmp,
29 .automatic_shrinking = true,
32 static struct net_bridge_vlan *br_vlan_lookup(struct rhashtable *tbl, u16 vid)
34 return rhashtable_lookup_fast(tbl, &vid, br_vlan_rht_params);
37 static bool __vlan_add_pvid(struct net_bridge_vlan_group *vg,
38 const struct net_bridge_vlan *v)
40 if (vg->pvid == v->vid)
44 br_vlan_set_pvid_state(vg, v->state);
50 static bool __vlan_delete_pvid(struct net_bridge_vlan_group *vg, u16 vid)
61 /* return true if anything changed, false otherwise */
62 static bool __vlan_add_flags(struct net_bridge_vlan *v, u16 flags)
64 struct net_bridge_vlan_group *vg;
65 u16 old_flags = v->flags;
68 if (br_vlan_is_master(v))
69 vg = br_vlan_group(v->br);
71 vg = nbp_vlan_group(v->port);
73 if (flags & BRIDGE_VLAN_INFO_PVID)
74 ret = __vlan_add_pvid(vg, v);
76 ret = __vlan_delete_pvid(vg, v->vid);
78 if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
79 v->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
81 v->flags &= ~BRIDGE_VLAN_INFO_UNTAGGED;
83 return ret || !!(old_flags ^ v->flags);
86 static int __vlan_vid_add(struct net_device *dev, struct net_bridge *br,
87 struct net_bridge_vlan *v, u16 flags,
88 struct netlink_ext_ack *extack)
92 /* Try switchdev op first. In case it is not supported, fallback to
95 err = br_switchdev_port_vlan_add(dev, v->vid, flags, extack);
96 if (err == -EOPNOTSUPP)
97 return vlan_vid_add(dev, br->vlan_proto, v->vid);
98 v->priv_flags |= BR_VLFLAG_ADDED_BY_SWITCHDEV;
102 static void __vlan_add_list(struct net_bridge_vlan *v)
104 struct net_bridge_vlan_group *vg;
105 struct list_head *headp, *hpos;
106 struct net_bridge_vlan *vent;
108 if (br_vlan_is_master(v))
109 vg = br_vlan_group(v->br);
111 vg = nbp_vlan_group(v->port);
113 headp = &vg->vlan_list;
114 list_for_each_prev(hpos, headp) {
115 vent = list_entry(hpos, struct net_bridge_vlan, vlist);
116 if (v->vid < vent->vid)
121 list_add_rcu(&v->vlist, hpos);
124 static void __vlan_del_list(struct net_bridge_vlan *v)
126 list_del_rcu(&v->vlist);
129 static int __vlan_vid_del(struct net_device *dev, struct net_bridge *br,
130 const struct net_bridge_vlan *v)
134 /* Try switchdev op first. In case it is not supported, fallback to
137 err = br_switchdev_port_vlan_del(dev, v->vid);
138 if (!(v->priv_flags & BR_VLFLAG_ADDED_BY_SWITCHDEV))
139 vlan_vid_del(dev, br->vlan_proto, v->vid);
140 return err == -EOPNOTSUPP ? 0 : err;
143 /* Returns a master vlan, if it didn't exist it gets created. In all cases
144 * a reference is taken to the master vlan before returning.
146 static struct net_bridge_vlan *
147 br_vlan_get_master(struct net_bridge *br, u16 vid,
148 struct netlink_ext_ack *extack)
150 struct net_bridge_vlan_group *vg;
151 struct net_bridge_vlan *masterv;
153 vg = br_vlan_group(br);
154 masterv = br_vlan_find(vg, vid);
158 /* missing global ctx, create it now */
159 if (br_vlan_add(br, vid, 0, &changed, extack))
161 masterv = br_vlan_find(vg, vid);
162 if (WARN_ON(!masterv))
164 refcount_set(&masterv->refcnt, 1);
167 refcount_inc(&masterv->refcnt);
172 static void br_master_vlan_rcu_free(struct rcu_head *rcu)
174 struct net_bridge_vlan *v;
176 v = container_of(rcu, struct net_bridge_vlan, rcu);
177 WARN_ON(!br_vlan_is_master(v));
178 free_percpu(v->stats);
183 static void br_vlan_put_master(struct net_bridge_vlan *masterv)
185 struct net_bridge_vlan_group *vg;
187 if (!br_vlan_is_master(masterv))
190 vg = br_vlan_group(masterv->br);
191 if (refcount_dec_and_test(&masterv->refcnt)) {
192 rhashtable_remove_fast(&vg->vlan_hash,
193 &masterv->vnode, br_vlan_rht_params);
194 __vlan_del_list(masterv);
195 call_rcu(&masterv->rcu, br_master_vlan_rcu_free);
199 static void nbp_vlan_rcu_free(struct rcu_head *rcu)
201 struct net_bridge_vlan *v;
203 v = container_of(rcu, struct net_bridge_vlan, rcu);
204 WARN_ON(br_vlan_is_master(v));
205 /* if we had per-port stats configured then free them here */
206 if (v->priv_flags & BR_VLFLAG_PER_PORT_STATS)
207 free_percpu(v->stats);
212 /* This is the shared VLAN add function which works for both ports and bridge
213 * devices. There are four possible calls to this function in terms of the
215 * 1. vlan is being added on a port (no master flags, global entry exists)
216 * 2. vlan is being added on a bridge (both master and brentry flags)
217 * 3. vlan is being added on a port, but a global entry didn't exist which
218 * is being created right now (master flag set, brentry flag unset), the
219 * global entry is used for global per-vlan features, but not for filtering
220 * 4. same as 3 but with both master and brentry flags set so the entry
221 * will be used for filtering in both the port and the bridge
223 static int __vlan_add(struct net_bridge_vlan *v, u16 flags,
224 struct netlink_ext_ack *extack)
226 struct net_bridge_vlan *masterv = NULL;
227 struct net_bridge_port *p = NULL;
228 struct net_bridge_vlan_group *vg;
229 struct net_device *dev;
230 struct net_bridge *br;
233 if (br_vlan_is_master(v)) {
236 vg = br_vlan_group(br);
241 vg = nbp_vlan_group(p);
245 /* Add VLAN to the device filter if it is supported.
246 * This ensures tagged traffic enters the bridge when
247 * promiscuous mode is disabled by br_manage_promisc().
249 err = __vlan_vid_add(dev, br, v, flags, extack);
253 /* need to work on the master vlan too */
254 if (flags & BRIDGE_VLAN_INFO_MASTER) {
257 err = br_vlan_add(br, v->vid,
258 flags | BRIDGE_VLAN_INFO_BRENTRY,
264 br_vlan_notify(br, NULL, v->vid, 0,
268 masterv = br_vlan_get_master(br, v->vid, extack);
274 if (br_opt_get(br, BROPT_VLAN_STATS_PER_PORT)) {
276 netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
281 v->priv_flags |= BR_VLFLAG_PER_PORT_STATS;
283 v->stats = masterv->stats;
286 err = br_switchdev_port_vlan_add(dev, v->vid, flags, extack);
287 if (err && err != -EOPNOTSUPP)
291 /* Add the dev mac and count the vlan only if it's usable */
292 if (br_vlan_should_use(v)) {
293 err = br_fdb_insert(br, p, dev->dev_addr, v->vid);
295 br_err(br, "failed insert local address into bridge forwarding table\n");
301 /* set the state before publishing */
302 v->state = BR_STATE_FORWARDING;
304 err = rhashtable_lookup_insert_fast(&vg->vlan_hash, &v->vnode,
310 __vlan_add_flags(v, flags);
313 nbp_vlan_set_vlan_dev_state(p, v->vid);
318 if (br_vlan_should_use(v)) {
319 br_fdb_find_delete_local(br, p, dev->dev_addr, v->vid);
325 __vlan_vid_del(dev, br, v);
327 if (v->stats && masterv->stats != v->stats)
328 free_percpu(v->stats);
331 br_vlan_put_master(masterv);
335 br_switchdev_port_vlan_del(dev, v->vid);
341 static int __vlan_del(struct net_bridge_vlan *v)
343 struct net_bridge_vlan *masterv = v;
344 struct net_bridge_vlan_group *vg;
345 struct net_bridge_port *p = NULL;
348 if (br_vlan_is_master(v)) {
349 vg = br_vlan_group(v->br);
352 vg = nbp_vlan_group(v->port);
356 __vlan_delete_pvid(vg, v->vid);
358 err = __vlan_vid_del(p->dev, p->br, v);
362 err = br_switchdev_port_vlan_del(v->br->dev, v->vid);
363 if (err && err != -EOPNOTSUPP)
368 if (br_vlan_should_use(v)) {
369 v->flags &= ~BRIDGE_VLAN_INFO_BRENTRY;
374 vlan_tunnel_info_del(vg, v);
375 rhashtable_remove_fast(&vg->vlan_hash, &v->vnode,
378 nbp_vlan_set_vlan_dev_state(p, v->vid);
379 call_rcu(&v->rcu, nbp_vlan_rcu_free);
382 br_vlan_put_master(masterv);
387 static void __vlan_group_free(struct net_bridge_vlan_group *vg)
389 WARN_ON(!list_empty(&vg->vlan_list));
390 rhashtable_destroy(&vg->vlan_hash);
391 vlan_tunnel_deinit(vg);
395 static void __vlan_flush(const struct net_bridge *br,
396 const struct net_bridge_port *p,
397 struct net_bridge_vlan_group *vg)
399 struct net_bridge_vlan *vlan, *tmp;
400 u16 v_start = 0, v_end = 0;
402 __vlan_delete_pvid(vg, vg->pvid);
403 list_for_each_entry_safe(vlan, tmp, &vg->vlan_list, vlist) {
404 /* take care of disjoint ranges */
407 } else if (vlan->vid - v_end != 1) {
408 /* found range end, notify and start next one */
409 br_vlan_notify(br, p, v_start, v_end, RTM_DELVLAN);
417 /* notify about the last/whole vlan range */
419 br_vlan_notify(br, p, v_start, v_end, RTM_DELVLAN);
422 struct sk_buff *br_handle_vlan(struct net_bridge *br,
423 const struct net_bridge_port *p,
424 struct net_bridge_vlan_group *vg,
427 struct pcpu_sw_netstats *stats;
428 struct net_bridge_vlan *v;
431 /* If this packet was not filtered at input, let it pass */
432 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
435 /* At this point, we know that the frame was filtered and contains
436 * a valid vlan id. If the vlan id has untagged flag set,
437 * send untagged; otherwise, send tagged.
439 br_vlan_get_tag(skb, &vid);
440 v = br_vlan_find(vg, vid);
441 /* Vlan entry must be configured at this point. The
442 * only exception is the bridge is set in promisc mode and the
443 * packet is destined for the bridge device. In this case
444 * pass the packet as is.
446 if (!v || !br_vlan_should_use(v)) {
447 if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) {
454 if (br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) {
455 stats = this_cpu_ptr(v->stats);
456 u64_stats_update_begin(&stats->syncp);
457 stats->tx_bytes += skb->len;
459 u64_stats_update_end(&stats->syncp);
462 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
463 __vlan_hwaccel_clear_tag(skb);
465 if (p && (p->flags & BR_VLAN_TUNNEL) &&
466 br_handle_egress_vlan_tunnel(skb, v)) {
474 /* Called under RCU */
475 static bool __allowed_ingress(const struct net_bridge *br,
476 struct net_bridge_vlan_group *vg,
477 struct sk_buff *skb, u16 *vid,
480 struct pcpu_sw_netstats *stats;
481 struct net_bridge_vlan *v;
484 BR_INPUT_SKB_CB(skb)->vlan_filtered = true;
485 /* If vlan tx offload is disabled on bridge device and frame was
486 * sent from vlan device on the bridge device, it does not have
487 * HW accelerated vlan tag.
489 if (unlikely(!skb_vlan_tag_present(skb) &&
490 skb->protocol == br->vlan_proto)) {
491 skb = skb_vlan_untag(skb);
496 if (!br_vlan_get_tag(skb, vid)) {
498 if (skb->vlan_proto != br->vlan_proto) {
499 /* Protocol-mismatch, empty out vlan_tci for new tag */
500 skb_push(skb, ETH_HLEN);
501 skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
502 skb_vlan_tag_get(skb));
506 skb_pull(skb, ETH_HLEN);
507 skb_reset_mac_len(skb);
519 u16 pvid = br_get_pvid(vg);
521 /* Frame had a tag with VID 0 or did not have a tag.
522 * See if pvid is set on this port. That tells us which
523 * vlan untagged or priority-tagged traffic belongs to.
528 /* PVID is set on this port. Any untagged or priority-tagged
529 * ingress frame is considered to belong to this vlan.
533 /* Untagged Frame. */
534 __vlan_hwaccel_put_tag(skb, br->vlan_proto, pvid);
536 /* Priority-tagged Frame.
537 * At this point, we know that skb->vlan_tci VID
539 * We update only VID field and preserve PCP field.
541 skb->vlan_tci |= pvid;
543 /* if stats are disabled we can avoid the lookup */
544 if (!br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) {
545 if (*state == BR_STATE_FORWARDING) {
546 *state = br_vlan_get_pvid_state(vg);
547 return br_vlan_state_allowed(*state, true);
553 v = br_vlan_find(vg, *vid);
554 if (!v || !br_vlan_should_use(v))
557 if (*state == BR_STATE_FORWARDING) {
558 *state = br_vlan_get_state(v);
559 if (!br_vlan_state_allowed(*state, true))
563 if (br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) {
564 stats = this_cpu_ptr(v->stats);
565 u64_stats_update_begin(&stats->syncp);
566 stats->rx_bytes += skb->len;
568 u64_stats_update_end(&stats->syncp);
578 bool br_allowed_ingress(const struct net_bridge *br,
579 struct net_bridge_vlan_group *vg, struct sk_buff *skb,
582 /* If VLAN filtering is disabled on the bridge, all packets are
585 if (!br_opt_get(br, BROPT_VLAN_ENABLED)) {
586 BR_INPUT_SKB_CB(skb)->vlan_filtered = false;
590 return __allowed_ingress(br, vg, skb, vid, state);
593 /* Called under RCU. */
594 bool br_allowed_egress(struct net_bridge_vlan_group *vg,
595 const struct sk_buff *skb)
597 const struct net_bridge_vlan *v;
600 /* If this packet was not filtered at input, let it pass */
601 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
604 br_vlan_get_tag(skb, &vid);
605 v = br_vlan_find(vg, vid);
606 if (v && br_vlan_should_use(v) &&
607 br_vlan_state_allowed(br_vlan_get_state(v), false))
613 /* Called under RCU */
614 bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid)
616 struct net_bridge_vlan_group *vg;
617 struct net_bridge *br = p->br;
618 struct net_bridge_vlan *v;
620 /* If filtering was disabled at input, let it pass. */
621 if (!br_opt_get(br, BROPT_VLAN_ENABLED))
624 vg = nbp_vlan_group_rcu(p);
625 if (!vg || !vg->num_vlans)
628 if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto)
632 *vid = br_get_pvid(vg);
634 !br_vlan_state_allowed(br_vlan_get_pvid_state(vg), true))
640 v = br_vlan_find(vg, *vid);
641 if (v && br_vlan_state_allowed(br_vlan_get_state(v), true))
647 static int br_vlan_add_existing(struct net_bridge *br,
648 struct net_bridge_vlan_group *vg,
649 struct net_bridge_vlan *vlan,
650 u16 flags, bool *changed,
651 struct netlink_ext_ack *extack)
655 err = br_switchdev_port_vlan_add(br->dev, vlan->vid, flags, extack);
656 if (err && err != -EOPNOTSUPP)
659 if (!br_vlan_is_brentry(vlan)) {
660 /* Trying to change flags of non-existent bridge vlan */
661 if (!(flags & BRIDGE_VLAN_INFO_BRENTRY)) {
665 /* It was only kept for port vlans, now make it real */
666 err = br_fdb_insert(br, NULL, br->dev->dev_addr,
669 br_err(br, "failed to insert local address into bridge forwarding table\n");
673 refcount_inc(&vlan->refcnt);
674 vlan->flags |= BRIDGE_VLAN_INFO_BRENTRY;
679 if (__vlan_add_flags(vlan, flags))
686 br_switchdev_port_vlan_del(br->dev, vlan->vid);
690 /* Must be protected by RTNL.
691 * Must be called with vid in range from 1 to 4094 inclusive.
692 * changed must be true only if the vlan was created or updated
694 int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags, bool *changed,
695 struct netlink_ext_ack *extack)
697 struct net_bridge_vlan_group *vg;
698 struct net_bridge_vlan *vlan;
704 vg = br_vlan_group(br);
705 vlan = br_vlan_find(vg, vid);
707 return br_vlan_add_existing(br, vg, vlan, flags, changed,
710 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
714 vlan->stats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
720 vlan->flags = flags | BRIDGE_VLAN_INFO_MASTER;
721 vlan->flags &= ~BRIDGE_VLAN_INFO_PVID;
723 if (flags & BRIDGE_VLAN_INFO_BRENTRY)
724 refcount_set(&vlan->refcnt, 1);
725 ret = __vlan_add(vlan, flags, extack);
727 free_percpu(vlan->stats);
736 /* Must be protected by RTNL.
737 * Must be called with vid in range from 1 to 4094 inclusive.
739 int br_vlan_delete(struct net_bridge *br, u16 vid)
741 struct net_bridge_vlan_group *vg;
742 struct net_bridge_vlan *v;
746 vg = br_vlan_group(br);
747 v = br_vlan_find(vg, vid);
748 if (!v || !br_vlan_is_brentry(v))
751 br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
752 br_fdb_delete_by_port(br, NULL, vid, 0);
754 vlan_tunnel_info_del(vg, v);
756 return __vlan_del(v);
759 void br_vlan_flush(struct net_bridge *br)
761 struct net_bridge_vlan_group *vg;
765 vg = br_vlan_group(br);
766 __vlan_flush(br, NULL, vg);
767 RCU_INIT_POINTER(br->vlgrp, NULL);
769 __vlan_group_free(vg);
772 struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid)
777 return br_vlan_lookup(&vg->vlan_hash, vid);
780 /* Must be protected by RTNL. */
781 static void recalculate_group_addr(struct net_bridge *br)
783 if (br_opt_get(br, BROPT_GROUP_ADDR_SET))
786 spin_lock_bh(&br->lock);
787 if (!br_opt_get(br, BROPT_VLAN_ENABLED) ||
788 br->vlan_proto == htons(ETH_P_8021Q)) {
789 /* Bridge Group Address */
790 br->group_addr[5] = 0x00;
791 } else { /* vlan_enabled && ETH_P_8021AD */
792 /* Provider Bridge Group Address */
793 br->group_addr[5] = 0x08;
795 spin_unlock_bh(&br->lock);
798 /* Must be protected by RTNL. */
799 void br_recalculate_fwd_mask(struct net_bridge *br)
801 if (!br_opt_get(br, BROPT_VLAN_ENABLED) ||
802 br->vlan_proto == htons(ETH_P_8021Q))
803 br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT;
804 else /* vlan_enabled && ETH_P_8021AD */
805 br->group_fwd_mask_required = BR_GROUPFWD_8021AD &
806 ~(1u << br->group_addr[5]);
809 int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val,
810 struct netlink_ext_ack *extack)
812 struct switchdev_attr attr = {
814 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
815 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
816 .u.vlan_filtering = val,
820 if (br_opt_get(br, BROPT_VLAN_ENABLED) == !!val)
823 err = switchdev_port_attr_set(br->dev, &attr, extack);
824 if (err && err != -EOPNOTSUPP)
827 br_opt_toggle(br, BROPT_VLAN_ENABLED, !!val);
828 br_manage_promisc(br);
829 recalculate_group_addr(br);
830 br_recalculate_fwd_mask(br);
835 bool br_vlan_enabled(const struct net_device *dev)
837 struct net_bridge *br = netdev_priv(dev);
839 return br_opt_get(br, BROPT_VLAN_ENABLED);
841 EXPORT_SYMBOL_GPL(br_vlan_enabled);
843 int br_vlan_get_proto(const struct net_device *dev, u16 *p_proto)
845 struct net_bridge *br = netdev_priv(dev);
847 *p_proto = ntohs(br->vlan_proto);
851 EXPORT_SYMBOL_GPL(br_vlan_get_proto);
853 int __br_vlan_set_proto(struct net_bridge *br, __be16 proto,
854 struct netlink_ext_ack *extack)
856 struct switchdev_attr attr = {
858 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_PROTOCOL,
859 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
860 .u.vlan_protocol = ntohs(proto),
863 struct net_bridge_port *p;
864 struct net_bridge_vlan *vlan;
865 struct net_bridge_vlan_group *vg;
866 __be16 oldproto = br->vlan_proto;
868 if (br->vlan_proto == proto)
871 err = switchdev_port_attr_set(br->dev, &attr, extack);
872 if (err && err != -EOPNOTSUPP)
875 /* Add VLANs for the new proto to the device filter. */
876 list_for_each_entry(p, &br->port_list, list) {
877 vg = nbp_vlan_group(p);
878 list_for_each_entry(vlan, &vg->vlan_list, vlist) {
879 err = vlan_vid_add(p->dev, proto, vlan->vid);
885 br->vlan_proto = proto;
887 recalculate_group_addr(br);
888 br_recalculate_fwd_mask(br);
890 /* Delete VLANs for the old proto from the device filter. */
891 list_for_each_entry(p, &br->port_list, list) {
892 vg = nbp_vlan_group(p);
893 list_for_each_entry(vlan, &vg->vlan_list, vlist)
894 vlan_vid_del(p->dev, oldproto, vlan->vid);
900 attr.u.vlan_protocol = ntohs(oldproto);
901 switchdev_port_attr_set(br->dev, &attr, NULL);
903 list_for_each_entry_continue_reverse(vlan, &vg->vlan_list, vlist)
904 vlan_vid_del(p->dev, proto, vlan->vid);
906 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
907 vg = nbp_vlan_group(p);
908 list_for_each_entry(vlan, &vg->vlan_list, vlist)
909 vlan_vid_del(p->dev, proto, vlan->vid);
915 int br_vlan_set_proto(struct net_bridge *br, unsigned long val,
916 struct netlink_ext_ack *extack)
918 if (!eth_type_vlan(htons(val)))
919 return -EPROTONOSUPPORT;
921 return __br_vlan_set_proto(br, htons(val), extack);
924 int br_vlan_set_stats(struct net_bridge *br, unsigned long val)
929 br_opt_toggle(br, BROPT_VLAN_STATS_ENABLED, !!val);
938 int br_vlan_set_stats_per_port(struct net_bridge *br, unsigned long val)
940 struct net_bridge_port *p;
942 /* allow to change the option if there are no port vlans configured */
943 list_for_each_entry(p, &br->port_list, list) {
944 struct net_bridge_vlan_group *vg = nbp_vlan_group(p);
953 br_opt_toggle(br, BROPT_VLAN_STATS_PER_PORT, !!val);
962 static bool vlan_default_pvid(struct net_bridge_vlan_group *vg, u16 vid)
964 struct net_bridge_vlan *v;
969 v = br_vlan_lookup(&vg->vlan_hash, vid);
970 if (v && br_vlan_should_use(v) &&
971 (v->flags & BRIDGE_VLAN_INFO_UNTAGGED))
977 static void br_vlan_disable_default_pvid(struct net_bridge *br)
979 struct net_bridge_port *p;
980 u16 pvid = br->default_pvid;
982 /* Disable default_pvid on all ports where it is still
985 if (vlan_default_pvid(br_vlan_group(br), pvid)) {
986 if (!br_vlan_delete(br, pvid))
987 br_vlan_notify(br, NULL, pvid, 0, RTM_DELVLAN);
990 list_for_each_entry(p, &br->port_list, list) {
991 if (vlan_default_pvid(nbp_vlan_group(p), pvid) &&
992 !nbp_vlan_delete(p, pvid))
993 br_vlan_notify(br, p, pvid, 0, RTM_DELVLAN);
996 br->default_pvid = 0;
999 int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid,
1000 struct netlink_ext_ack *extack)
1002 const struct net_bridge_vlan *pvent;
1003 struct net_bridge_vlan_group *vg;
1004 struct net_bridge_port *p;
1005 unsigned long *changed;
1011 br_vlan_disable_default_pvid(br);
1015 changed = bitmap_zalloc(BR_MAX_PORTS, GFP_KERNEL);
1019 old_pvid = br->default_pvid;
1021 /* Update default_pvid config only if we do not conflict with
1022 * user configuration.
1024 vg = br_vlan_group(br);
1025 pvent = br_vlan_find(vg, pvid);
1026 if ((!old_pvid || vlan_default_pvid(vg, old_pvid)) &&
1027 (!pvent || !br_vlan_should_use(pvent))) {
1028 err = br_vlan_add(br, pvid,
1029 BRIDGE_VLAN_INFO_PVID |
1030 BRIDGE_VLAN_INFO_UNTAGGED |
1031 BRIDGE_VLAN_INFO_BRENTRY,
1036 if (br_vlan_delete(br, old_pvid))
1037 br_vlan_notify(br, NULL, old_pvid, 0, RTM_DELVLAN);
1038 br_vlan_notify(br, NULL, pvid, 0, RTM_NEWVLAN);
1039 set_bit(0, changed);
1042 list_for_each_entry(p, &br->port_list, list) {
1043 /* Update default_pvid config only if we do not conflict with
1044 * user configuration.
1046 vg = nbp_vlan_group(p);
1048 !vlan_default_pvid(vg, old_pvid)) ||
1049 br_vlan_find(vg, pvid))
1052 err = nbp_vlan_add(p, pvid,
1053 BRIDGE_VLAN_INFO_PVID |
1054 BRIDGE_VLAN_INFO_UNTAGGED,
1058 if (nbp_vlan_delete(p, old_pvid))
1059 br_vlan_notify(br, p, old_pvid, 0, RTM_DELVLAN);
1060 br_vlan_notify(p->br, p, pvid, 0, RTM_NEWVLAN);
1061 set_bit(p->port_no, changed);
1064 br->default_pvid = pvid;
1067 bitmap_free(changed);
1071 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
1072 if (!test_bit(p->port_no, changed))
1076 nbp_vlan_add(p, old_pvid,
1077 BRIDGE_VLAN_INFO_PVID |
1078 BRIDGE_VLAN_INFO_UNTAGGED,
1080 br_vlan_notify(p->br, p, old_pvid, 0, RTM_NEWVLAN);
1082 nbp_vlan_delete(p, pvid);
1083 br_vlan_notify(br, p, pvid, 0, RTM_DELVLAN);
1086 if (test_bit(0, changed)) {
1088 br_vlan_add(br, old_pvid,
1089 BRIDGE_VLAN_INFO_PVID |
1090 BRIDGE_VLAN_INFO_UNTAGGED |
1091 BRIDGE_VLAN_INFO_BRENTRY,
1093 br_vlan_notify(br, NULL, old_pvid, 0, RTM_NEWVLAN);
1095 br_vlan_delete(br, pvid);
1096 br_vlan_notify(br, NULL, pvid, 0, RTM_DELVLAN);
1101 int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val,
1102 struct netlink_ext_ack *extack)
1107 if (val >= VLAN_VID_MASK)
1110 if (pvid == br->default_pvid)
1113 /* Only allow default pvid change when filtering is disabled */
1114 if (br_opt_get(br, BROPT_VLAN_ENABLED)) {
1115 pr_info_once("Please disable vlan filtering to change default_pvid\n");
1119 err = __br_vlan_set_default_pvid(br, pvid, extack);
1124 int br_vlan_init(struct net_bridge *br)
1126 struct net_bridge_vlan_group *vg;
1129 vg = kzalloc(sizeof(*vg), GFP_KERNEL);
1132 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
1135 ret = vlan_tunnel_init(vg);
1137 goto err_tunnel_init;
1138 INIT_LIST_HEAD(&vg->vlan_list);
1139 br->vlan_proto = htons(ETH_P_8021Q);
1140 br->default_pvid = 1;
1141 rcu_assign_pointer(br->vlgrp, vg);
1147 rhashtable_destroy(&vg->vlan_hash);
1154 int nbp_vlan_init(struct net_bridge_port *p, struct netlink_ext_ack *extack)
1156 struct switchdev_attr attr = {
1157 .orig_dev = p->br->dev,
1158 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
1159 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
1160 .u.vlan_filtering = br_opt_get(p->br, BROPT_VLAN_ENABLED),
1162 struct net_bridge_vlan_group *vg;
1165 vg = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL);
1169 ret = switchdev_port_attr_set(p->dev, &attr, extack);
1170 if (ret && ret != -EOPNOTSUPP)
1171 goto err_vlan_enabled;
1173 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
1176 ret = vlan_tunnel_init(vg);
1178 goto err_tunnel_init;
1179 INIT_LIST_HEAD(&vg->vlan_list);
1180 rcu_assign_pointer(p->vlgrp, vg);
1181 if (p->br->default_pvid) {
1184 ret = nbp_vlan_add(p, p->br->default_pvid,
1185 BRIDGE_VLAN_INFO_PVID |
1186 BRIDGE_VLAN_INFO_UNTAGGED,
1190 br_vlan_notify(p->br, p, p->br->default_pvid, 0, RTM_NEWVLAN);
1196 RCU_INIT_POINTER(p->vlgrp, NULL);
1198 vlan_tunnel_deinit(vg);
1200 rhashtable_destroy(&vg->vlan_hash);
1208 /* Must be protected by RTNL.
1209 * Must be called with vid in range from 1 to 4094 inclusive.
1210 * changed must be true only if the vlan was created or updated
1212 int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags,
1213 bool *changed, struct netlink_ext_ack *extack)
1215 struct net_bridge_vlan *vlan;
1221 vlan = br_vlan_find(nbp_vlan_group(port), vid);
1223 /* Pass the flags to the hardware bridge */
1224 ret = br_switchdev_port_vlan_add(port->dev, vid, flags, extack);
1225 if (ret && ret != -EOPNOTSUPP)
1227 *changed = __vlan_add_flags(vlan, flags);
1232 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
1238 ret = __vlan_add(vlan, flags, extack);
1247 /* Must be protected by RTNL.
1248 * Must be called with vid in range from 1 to 4094 inclusive.
1250 int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
1252 struct net_bridge_vlan *v;
1256 v = br_vlan_find(nbp_vlan_group(port), vid);
1259 br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
1260 br_fdb_delete_by_port(port->br, port, vid, 0);
1262 return __vlan_del(v);
1265 void nbp_vlan_flush(struct net_bridge_port *port)
1267 struct net_bridge_vlan_group *vg;
1271 vg = nbp_vlan_group(port);
1272 __vlan_flush(port->br, port, vg);
1273 RCU_INIT_POINTER(port->vlgrp, NULL);
1275 __vlan_group_free(vg);
1278 void br_vlan_get_stats(const struct net_bridge_vlan *v,
1279 struct pcpu_sw_netstats *stats)
1283 memset(stats, 0, sizeof(*stats));
1284 for_each_possible_cpu(i) {
1285 u64 rxpackets, rxbytes, txpackets, txbytes;
1286 struct pcpu_sw_netstats *cpu_stats;
1289 cpu_stats = per_cpu_ptr(v->stats, i);
1291 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
1292 rxpackets = cpu_stats->rx_packets;
1293 rxbytes = cpu_stats->rx_bytes;
1294 txbytes = cpu_stats->tx_bytes;
1295 txpackets = cpu_stats->tx_packets;
1296 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
1298 stats->rx_packets += rxpackets;
1299 stats->rx_bytes += rxbytes;
1300 stats->tx_bytes += txbytes;
1301 stats->tx_packets += txpackets;
1305 int br_vlan_get_pvid(const struct net_device *dev, u16 *p_pvid)
1307 struct net_bridge_vlan_group *vg;
1308 struct net_bridge_port *p;
1311 p = br_port_get_check_rtnl(dev);
1313 vg = nbp_vlan_group(p);
1314 else if (netif_is_bridge_master(dev))
1315 vg = br_vlan_group(netdev_priv(dev));
1319 *p_pvid = br_get_pvid(vg);
1322 EXPORT_SYMBOL_GPL(br_vlan_get_pvid);
1324 int br_vlan_get_pvid_rcu(const struct net_device *dev, u16 *p_pvid)
1326 struct net_bridge_vlan_group *vg;
1327 struct net_bridge_port *p;
1329 p = br_port_get_check_rcu(dev);
1331 vg = nbp_vlan_group_rcu(p);
1332 else if (netif_is_bridge_master(dev))
1333 vg = br_vlan_group_rcu(netdev_priv(dev));
1337 *p_pvid = br_get_pvid(vg);
1340 EXPORT_SYMBOL_GPL(br_vlan_get_pvid_rcu);
1342 int br_vlan_get_info(const struct net_device *dev, u16 vid,
1343 struct bridge_vlan_info *p_vinfo)
1345 struct net_bridge_vlan_group *vg;
1346 struct net_bridge_vlan *v;
1347 struct net_bridge_port *p;
1350 p = br_port_get_check_rtnl(dev);
1352 vg = nbp_vlan_group(p);
1353 else if (netif_is_bridge_master(dev))
1354 vg = br_vlan_group(netdev_priv(dev));
1358 v = br_vlan_find(vg, vid);
1363 p_vinfo->flags = v->flags;
1364 if (vid == br_get_pvid(vg))
1365 p_vinfo->flags |= BRIDGE_VLAN_INFO_PVID;
1368 EXPORT_SYMBOL_GPL(br_vlan_get_info);
1370 static int br_vlan_is_bind_vlan_dev(const struct net_device *dev)
1372 return is_vlan_dev(dev) &&
1373 !!(vlan_dev_priv(dev)->flags & VLAN_FLAG_BRIDGE_BINDING);
1376 static int br_vlan_is_bind_vlan_dev_fn(struct net_device *dev,
1377 __always_unused struct netdev_nested_priv *priv)
1379 return br_vlan_is_bind_vlan_dev(dev);
1382 static bool br_vlan_has_upper_bind_vlan_dev(struct net_device *dev)
1387 found = netdev_walk_all_upper_dev_rcu(dev, br_vlan_is_bind_vlan_dev_fn,
1394 struct br_vlan_bind_walk_data {
1396 struct net_device *result;
1399 static int br_vlan_match_bind_vlan_dev_fn(struct net_device *dev,
1400 struct netdev_nested_priv *priv)
1402 struct br_vlan_bind_walk_data *data = priv->data;
1405 if (br_vlan_is_bind_vlan_dev(dev) &&
1406 vlan_dev_priv(dev)->vlan_id == data->vid) {
1414 static struct net_device *
1415 br_vlan_get_upper_bind_vlan_dev(struct net_device *dev, u16 vid)
1417 struct br_vlan_bind_walk_data data = {
1420 struct netdev_nested_priv priv = {
1421 .data = (void *)&data,
1425 netdev_walk_all_upper_dev_rcu(dev, br_vlan_match_bind_vlan_dev_fn,
1432 static bool br_vlan_is_dev_up(const struct net_device *dev)
1434 return !!(dev->flags & IFF_UP) && netif_oper_up(dev);
1437 static void br_vlan_set_vlan_dev_state(const struct net_bridge *br,
1438 struct net_device *vlan_dev)
1440 u16 vid = vlan_dev_priv(vlan_dev)->vlan_id;
1441 struct net_bridge_vlan_group *vg;
1442 struct net_bridge_port *p;
1443 bool has_carrier = false;
1445 if (!netif_carrier_ok(br->dev)) {
1446 netif_carrier_off(vlan_dev);
1450 list_for_each_entry(p, &br->port_list, list) {
1451 vg = nbp_vlan_group(p);
1452 if (br_vlan_find(vg, vid) && br_vlan_is_dev_up(p->dev)) {
1459 netif_carrier_on(vlan_dev);
1461 netif_carrier_off(vlan_dev);
1464 static void br_vlan_set_all_vlan_dev_state(struct net_bridge_port *p)
1466 struct net_bridge_vlan_group *vg = nbp_vlan_group(p);
1467 struct net_bridge_vlan *vlan;
1468 struct net_device *vlan_dev;
1470 list_for_each_entry(vlan, &vg->vlan_list, vlist) {
1471 vlan_dev = br_vlan_get_upper_bind_vlan_dev(p->br->dev,
1474 if (br_vlan_is_dev_up(p->dev)) {
1475 if (netif_carrier_ok(p->br->dev))
1476 netif_carrier_on(vlan_dev);
1478 br_vlan_set_vlan_dev_state(p->br, vlan_dev);
1484 static void br_vlan_upper_change(struct net_device *dev,
1485 struct net_device *upper_dev,
1488 struct net_bridge *br = netdev_priv(dev);
1490 if (!br_vlan_is_bind_vlan_dev(upper_dev))
1494 br_vlan_set_vlan_dev_state(br, upper_dev);
1495 br_opt_toggle(br, BROPT_VLAN_BRIDGE_BINDING, true);
1497 br_opt_toggle(br, BROPT_VLAN_BRIDGE_BINDING,
1498 br_vlan_has_upper_bind_vlan_dev(dev));
1502 struct br_vlan_link_state_walk_data {
1503 struct net_bridge *br;
1506 static int br_vlan_link_state_change_fn(struct net_device *vlan_dev,
1507 struct netdev_nested_priv *priv)
1509 struct br_vlan_link_state_walk_data *data = priv->data;
1511 if (br_vlan_is_bind_vlan_dev(vlan_dev))
1512 br_vlan_set_vlan_dev_state(data->br, vlan_dev);
1517 static void br_vlan_link_state_change(struct net_device *dev,
1518 struct net_bridge *br)
1520 struct br_vlan_link_state_walk_data data = {
1523 struct netdev_nested_priv priv = {
1524 .data = (void *)&data,
1528 netdev_walk_all_upper_dev_rcu(dev, br_vlan_link_state_change_fn,
1533 /* Must be protected by RTNL. */
1534 static void nbp_vlan_set_vlan_dev_state(struct net_bridge_port *p, u16 vid)
1536 struct net_device *vlan_dev;
1538 if (!br_opt_get(p->br, BROPT_VLAN_BRIDGE_BINDING))
1541 vlan_dev = br_vlan_get_upper_bind_vlan_dev(p->br->dev, vid);
1543 br_vlan_set_vlan_dev_state(p->br, vlan_dev);
1546 /* Must be protected by RTNL. */
1547 int br_vlan_bridge_event(struct net_device *dev, unsigned long event, void *ptr)
1549 struct netdev_notifier_changeupper_info *info;
1550 struct net_bridge *br = netdev_priv(dev);
1551 int vlcmd = 0, ret = 0;
1552 bool changed = false;
1555 case NETDEV_REGISTER:
1556 ret = br_vlan_add(br, br->default_pvid,
1557 BRIDGE_VLAN_INFO_PVID |
1558 BRIDGE_VLAN_INFO_UNTAGGED |
1559 BRIDGE_VLAN_INFO_BRENTRY, &changed, NULL);
1560 vlcmd = RTM_NEWVLAN;
1562 case NETDEV_UNREGISTER:
1563 changed = !br_vlan_delete(br, br->default_pvid);
1564 vlcmd = RTM_DELVLAN;
1566 case NETDEV_CHANGEUPPER:
1568 br_vlan_upper_change(dev, info->upper_dev, info->linking);
1573 if (!br_opt_get(br, BROPT_VLAN_BRIDGE_BINDING))
1575 br_vlan_link_state_change(dev, br);
1579 br_vlan_notify(br, NULL, br->default_pvid, 0, vlcmd);
1584 /* Must be protected by RTNL. */
1585 void br_vlan_port_event(struct net_bridge_port *p, unsigned long event)
1587 if (!br_opt_get(p->br, BROPT_VLAN_BRIDGE_BINDING))
1594 br_vlan_set_all_vlan_dev_state(p);
1599 static bool br_vlan_stats_fill(struct sk_buff *skb,
1600 const struct net_bridge_vlan *v)
1602 struct pcpu_sw_netstats stats;
1603 struct nlattr *nest;
1605 nest = nla_nest_start(skb, BRIDGE_VLANDB_ENTRY_STATS);
1609 br_vlan_get_stats(v, &stats);
1610 if (nla_put_u64_64bit(skb, BRIDGE_VLANDB_STATS_RX_BYTES, stats.rx_bytes,
1611 BRIDGE_VLANDB_STATS_PAD) ||
1612 nla_put_u64_64bit(skb, BRIDGE_VLANDB_STATS_RX_PACKETS,
1613 stats.rx_packets, BRIDGE_VLANDB_STATS_PAD) ||
1614 nla_put_u64_64bit(skb, BRIDGE_VLANDB_STATS_TX_BYTES, stats.tx_bytes,
1615 BRIDGE_VLANDB_STATS_PAD) ||
1616 nla_put_u64_64bit(skb, BRIDGE_VLANDB_STATS_TX_PACKETS,
1617 stats.tx_packets, BRIDGE_VLANDB_STATS_PAD))
1620 nla_nest_end(skb, nest);
1625 nla_nest_cancel(skb, nest);
1629 /* v_opts is used to dump the options which must be equal in the whole range */
1630 static bool br_vlan_fill_vids(struct sk_buff *skb, u16 vid, u16 vid_range,
1631 const struct net_bridge_vlan *v_opts,
1635 struct bridge_vlan_info info;
1636 struct nlattr *nest;
1638 nest = nla_nest_start(skb, BRIDGE_VLANDB_ENTRY);
1642 memset(&info, 0, sizeof(info));
1644 if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
1645 info.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1646 if (flags & BRIDGE_VLAN_INFO_PVID)
1647 info.flags |= BRIDGE_VLAN_INFO_PVID;
1649 if (nla_put(skb, BRIDGE_VLANDB_ENTRY_INFO, sizeof(info), &info))
1652 if (vid_range && vid < vid_range &&
1653 !(flags & BRIDGE_VLAN_INFO_PVID) &&
1654 nla_put_u16(skb, BRIDGE_VLANDB_ENTRY_RANGE, vid_range))
1658 if (!br_vlan_opts_fill(skb, v_opts))
1661 if (dump_stats && !br_vlan_stats_fill(skb, v_opts))
1665 nla_nest_end(skb, nest);
1670 nla_nest_cancel(skb, nest);
1674 static size_t rtnl_vlan_nlmsg_size(void)
1676 return NLMSG_ALIGN(sizeof(struct br_vlan_msg))
1677 + nla_total_size(0) /* BRIDGE_VLANDB_ENTRY */
1678 + nla_total_size(sizeof(u16)) /* BRIDGE_VLANDB_ENTRY_RANGE */
1679 + nla_total_size(sizeof(struct bridge_vlan_info)) /* BRIDGE_VLANDB_ENTRY_INFO */
1680 + br_vlan_opts_nl_size(); /* bridge vlan options */
1683 void br_vlan_notify(const struct net_bridge *br,
1684 const struct net_bridge_port *p,
1685 u16 vid, u16 vid_range,
1688 struct net_bridge_vlan_group *vg;
1689 struct net_bridge_vlan *v = NULL;
1690 struct br_vlan_msg *bvm;
1691 struct nlmsghdr *nlh;
1692 struct sk_buff *skb;
1698 /* right now notifications are done only with rtnl held */
1702 ifindex = p->dev->ifindex;
1703 vg = nbp_vlan_group(p);
1704 net = dev_net(p->dev);
1706 ifindex = br->dev->ifindex;
1707 vg = br_vlan_group(br);
1708 net = dev_net(br->dev);
1711 skb = nlmsg_new(rtnl_vlan_nlmsg_size(), GFP_KERNEL);
1716 nlh = nlmsg_put(skb, 0, 0, cmd, sizeof(*bvm), 0);
1719 bvm = nlmsg_data(nlh);
1720 memset(bvm, 0, sizeof(*bvm));
1721 bvm->family = AF_BRIDGE;
1722 bvm->ifindex = ifindex;
1726 /* need to find the vlan due to flags/options */
1727 v = br_vlan_find(vg, vid);
1728 if (!v || !br_vlan_should_use(v))
1732 if (br_get_pvid(vg) == v->vid)
1733 flags |= BRIDGE_VLAN_INFO_PVID;
1741 if (!br_vlan_fill_vids(skb, vid, vid_range, v, flags, false))
1744 nlmsg_end(skb, nlh);
1745 rtnl_notify(skb, net, 0, RTNLGRP_BRVLAN, NULL, GFP_KERNEL);
1749 rtnl_set_sk_err(net, RTNLGRP_BRVLAN, err);
1754 /* check if v_curr can enter a range ending in range_end */
1755 bool br_vlan_can_enter_range(const struct net_bridge_vlan *v_curr,
1756 const struct net_bridge_vlan *range_end)
1758 return v_curr->vid - range_end->vid == 1 &&
1759 range_end->flags == v_curr->flags &&
1760 br_vlan_opts_eq_range(v_curr, range_end);
1763 static int br_vlan_dump_dev(const struct net_device *dev,
1764 struct sk_buff *skb,
1765 struct netlink_callback *cb,
1768 struct net_bridge_vlan *v, *range_start = NULL, *range_end = NULL;
1769 bool dump_stats = !!(dump_flags & BRIDGE_VLANDB_DUMPF_STATS);
1770 struct net_bridge_vlan_group *vg;
1771 int idx = 0, s_idx = cb->args[1];
1772 struct nlmsghdr *nlh = NULL;
1773 struct net_bridge_port *p;
1774 struct br_vlan_msg *bvm;
1775 struct net_bridge *br;
1779 if (!netif_is_bridge_master(dev) && !netif_is_bridge_port(dev))
1782 if (netif_is_bridge_master(dev)) {
1783 br = netdev_priv(dev);
1784 vg = br_vlan_group_rcu(br);
1787 p = br_port_get_rcu(dev);
1790 vg = nbp_vlan_group_rcu(p);
1797 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1798 RTM_NEWVLAN, sizeof(*bvm), NLM_F_MULTI);
1801 bvm = nlmsg_data(nlh);
1802 memset(bvm, 0, sizeof(*bvm));
1803 bvm->family = PF_BRIDGE;
1804 bvm->ifindex = dev->ifindex;
1805 pvid = br_get_pvid(vg);
1807 /* idx must stay at range's beginning until it is filled in */
1808 list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
1809 if (!br_vlan_should_use(v))
1822 if (dump_stats || v->vid == pvid ||
1823 !br_vlan_can_enter_range(v, range_end)) {
1824 u16 vlan_flags = br_vlan_flags(range_start, pvid);
1826 if (!br_vlan_fill_vids(skb, range_start->vid,
1827 range_end->vid, range_start,
1828 vlan_flags, dump_stats)) {
1832 /* advance number of filled vlans */
1833 idx += range_end->vid - range_start->vid + 1;
1840 /* err will be 0 and range_start will be set in 3 cases here:
1841 * - first vlan (range_start == range_end)
1842 * - last vlan (range_start == range_end, not in range)
1843 * - last vlan range (range_start != range_end, in range)
1845 if (!err && range_start &&
1846 !br_vlan_fill_vids(skb, range_start->vid, range_end->vid,
1847 range_start, br_vlan_flags(range_start, pvid),
1851 cb->args[1] = err ? idx : 0;
1853 nlmsg_end(skb, nlh);
1858 static const struct nla_policy br_vlan_db_dump_pol[BRIDGE_VLANDB_DUMP_MAX + 1] = {
1859 [BRIDGE_VLANDB_DUMP_FLAGS] = { .type = NLA_U32 },
1862 static int br_vlan_rtm_dump(struct sk_buff *skb, struct netlink_callback *cb)
1864 struct nlattr *dtb[BRIDGE_VLANDB_DUMP_MAX + 1];
1865 int idx = 0, err = 0, s_idx = cb->args[0];
1866 struct net *net = sock_net(skb->sk);
1867 struct br_vlan_msg *bvm;
1868 struct net_device *dev;
1871 err = nlmsg_parse(cb->nlh, sizeof(*bvm), dtb, BRIDGE_VLANDB_DUMP_MAX,
1872 br_vlan_db_dump_pol, cb->extack);
1876 bvm = nlmsg_data(cb->nlh);
1877 if (dtb[BRIDGE_VLANDB_DUMP_FLAGS])
1878 dump_flags = nla_get_u32(dtb[BRIDGE_VLANDB_DUMP_FLAGS]);
1882 dev = dev_get_by_index_rcu(net, bvm->ifindex);
1887 err = br_vlan_dump_dev(dev, skb, cb, dump_flags);
1888 if (err && err != -EMSGSIZE)
1891 for_each_netdev_rcu(net, dev) {
1895 err = br_vlan_dump_dev(dev, skb, cb, dump_flags);
1896 if (err == -EMSGSIZE)
1913 static const struct nla_policy br_vlan_db_policy[BRIDGE_VLANDB_ENTRY_MAX + 1] = {
1914 [BRIDGE_VLANDB_ENTRY_INFO] =
1915 NLA_POLICY_EXACT_LEN(sizeof(struct bridge_vlan_info)),
1916 [BRIDGE_VLANDB_ENTRY_RANGE] = { .type = NLA_U16 },
1917 [BRIDGE_VLANDB_ENTRY_STATE] = { .type = NLA_U8 },
1918 [BRIDGE_VLANDB_ENTRY_TUNNEL_INFO] = { .type = NLA_NESTED },
1921 static int br_vlan_rtm_process_one(struct net_device *dev,
1922 const struct nlattr *attr,
1923 int cmd, struct netlink_ext_ack *extack)
1925 struct bridge_vlan_info *vinfo, vrange_end, *vinfo_last = NULL;
1926 struct nlattr *tb[BRIDGE_VLANDB_ENTRY_MAX + 1];
1927 bool changed = false, skip_processing = false;
1928 struct net_bridge_vlan_group *vg;
1929 struct net_bridge_port *p = NULL;
1930 int err = 0, cmdmap = 0;
1931 struct net_bridge *br;
1933 if (netif_is_bridge_master(dev)) {
1934 br = netdev_priv(dev);
1935 vg = br_vlan_group(br);
1937 p = br_port_get_rtnl(dev);
1941 vg = nbp_vlan_group(p);
1947 err = nla_parse_nested(tb, BRIDGE_VLANDB_ENTRY_MAX, attr,
1948 br_vlan_db_policy, extack);
1952 if (!tb[BRIDGE_VLANDB_ENTRY_INFO]) {
1953 NL_SET_ERR_MSG_MOD(extack, "Missing vlan entry info");
1956 memset(&vrange_end, 0, sizeof(vrange_end));
1958 vinfo = nla_data(tb[BRIDGE_VLANDB_ENTRY_INFO]);
1959 if (vinfo->flags & (BRIDGE_VLAN_INFO_RANGE_BEGIN |
1960 BRIDGE_VLAN_INFO_RANGE_END)) {
1961 NL_SET_ERR_MSG_MOD(extack, "Old-style vlan ranges are not allowed when using RTM vlan calls");
1964 if (!br_vlan_valid_id(vinfo->vid, extack))
1967 if (tb[BRIDGE_VLANDB_ENTRY_RANGE]) {
1968 vrange_end.vid = nla_get_u16(tb[BRIDGE_VLANDB_ENTRY_RANGE]);
1969 /* validate user-provided flags without RANGE_BEGIN */
1970 vrange_end.flags = BRIDGE_VLAN_INFO_RANGE_END | vinfo->flags;
1971 vinfo->flags |= BRIDGE_VLAN_INFO_RANGE_BEGIN;
1973 /* vinfo_last is the range start, vinfo the range end */
1975 vinfo = &vrange_end;
1977 if (!br_vlan_valid_id(vinfo->vid, extack) ||
1978 !br_vlan_valid_range(vinfo, vinfo_last, extack))
1984 cmdmap = RTM_SETLINK;
1985 skip_processing = !!(vinfo->flags & BRIDGE_VLAN_INFO_ONLY_OPTS);
1988 cmdmap = RTM_DELLINK;
1992 if (!skip_processing) {
1993 struct bridge_vlan_info *tmp_last = vinfo_last;
1995 /* br_process_vlan_info may overwrite vinfo_last */
1996 err = br_process_vlan_info(br, p, cmdmap, vinfo, &tmp_last,
1999 /* notify first if anything changed */
2001 br_ifinfo_notify(cmdmap, br, p);
2007 /* deal with options */
2008 if (cmd == RTM_NEWVLAN) {
2009 struct net_bridge_vlan *range_start, *range_end;
2012 range_start = br_vlan_find(vg, vinfo_last->vid);
2013 range_end = br_vlan_find(vg, vinfo->vid);
2015 range_start = br_vlan_find(vg, vinfo->vid);
2016 range_end = range_start;
2019 err = br_vlan_process_options(br, p, range_start, range_end,
2026 static int br_vlan_rtm_process(struct sk_buff *skb, struct nlmsghdr *nlh,
2027 struct netlink_ext_ack *extack)
2029 struct net *net = sock_net(skb->sk);
2030 struct br_vlan_msg *bvm;
2031 struct net_device *dev;
2032 struct nlattr *attr;
2036 /* this should validate the header and check for remaining bytes */
2037 err = nlmsg_parse(nlh, sizeof(*bvm), NULL, BRIDGE_VLANDB_MAX, NULL,
2042 bvm = nlmsg_data(nlh);
2043 dev = __dev_get_by_index(net, bvm->ifindex);
2047 if (!netif_is_bridge_master(dev) && !netif_is_bridge_port(dev)) {
2048 NL_SET_ERR_MSG_MOD(extack, "The device is not a valid bridge or bridge port");
2052 nlmsg_for_each_attr(attr, nlh, sizeof(*bvm), rem) {
2053 if (nla_type(attr) != BRIDGE_VLANDB_ENTRY)
2057 err = br_vlan_rtm_process_one(dev, attr, nlh->nlmsg_type,
2063 NL_SET_ERR_MSG_MOD(extack, "No vlans found to process");
2070 void br_vlan_rtnl_init(void)
2072 rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_GETVLAN, NULL,
2073 br_vlan_rtm_dump, 0);
2074 rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_NEWVLAN,
2075 br_vlan_rtm_process, NULL, 0);
2076 rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_DELVLAN,
2077 br_vlan_rtm_process, NULL, 0);
2080 void br_vlan_rtnl_uninit(void)
2082 rtnl_unregister(PF_BRIDGE, RTM_GETVLAN);
2083 rtnl_unregister(PF_BRIDGE, RTM_NEWVLAN);
2084 rtnl_unregister(PF_BRIDGE, RTM_DELVLAN);