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)
119 list_add_rcu(&v->vlist, hpos);
122 static void __vlan_del_list(struct net_bridge_vlan *v)
124 list_del_rcu(&v->vlist);
127 static int __vlan_vid_del(struct net_device *dev, struct net_bridge *br,
128 const struct net_bridge_vlan *v)
132 /* Try switchdev op first. In case it is not supported, fallback to
135 err = br_switchdev_port_vlan_del(dev, v->vid);
136 if (!(v->priv_flags & BR_VLFLAG_ADDED_BY_SWITCHDEV))
137 vlan_vid_del(dev, br->vlan_proto, v->vid);
138 return err == -EOPNOTSUPP ? 0 : err;
141 /* Returns a master vlan, if it didn't exist it gets created. In all cases
142 * a reference is taken to the master vlan before returning.
144 static struct net_bridge_vlan *
145 br_vlan_get_master(struct net_bridge *br, u16 vid,
146 struct netlink_ext_ack *extack)
148 struct net_bridge_vlan_group *vg;
149 struct net_bridge_vlan *masterv;
151 vg = br_vlan_group(br);
152 masterv = br_vlan_find(vg, vid);
156 /* missing global ctx, create it now */
157 if (br_vlan_add(br, vid, 0, &changed, extack))
159 masterv = br_vlan_find(vg, vid);
160 if (WARN_ON(!masterv))
162 refcount_set(&masterv->refcnt, 1);
165 refcount_inc(&masterv->refcnt);
170 static void br_master_vlan_rcu_free(struct rcu_head *rcu)
172 struct net_bridge_vlan *v;
174 v = container_of(rcu, struct net_bridge_vlan, rcu);
175 WARN_ON(!br_vlan_is_master(v));
176 free_percpu(v->stats);
181 static void br_vlan_put_master(struct net_bridge_vlan *masterv)
183 struct net_bridge_vlan_group *vg;
185 if (!br_vlan_is_master(masterv))
188 vg = br_vlan_group(masterv->br);
189 if (refcount_dec_and_test(&masterv->refcnt)) {
190 rhashtable_remove_fast(&vg->vlan_hash,
191 &masterv->vnode, br_vlan_rht_params);
192 __vlan_del_list(masterv);
193 call_rcu(&masterv->rcu, br_master_vlan_rcu_free);
197 static void nbp_vlan_rcu_free(struct rcu_head *rcu)
199 struct net_bridge_vlan *v;
201 v = container_of(rcu, struct net_bridge_vlan, rcu);
202 WARN_ON(br_vlan_is_master(v));
203 /* if we had per-port stats configured then free them here */
204 if (v->priv_flags & BR_VLFLAG_PER_PORT_STATS)
205 free_percpu(v->stats);
210 /* This is the shared VLAN add function which works for both ports and bridge
211 * devices. There are four possible calls to this function in terms of the
213 * 1. vlan is being added on a port (no master flags, global entry exists)
214 * 2. vlan is being added on a bridge (both master and brentry flags)
215 * 3. vlan is being added on a port, but a global entry didn't exist which
216 * is being created right now (master flag set, brentry flag unset), the
217 * global entry is used for global per-vlan features, but not for filtering
218 * 4. same as 3 but with both master and brentry flags set so the entry
219 * will be used for filtering in both the port and the bridge
221 static int __vlan_add(struct net_bridge_vlan *v, u16 flags,
222 struct netlink_ext_ack *extack)
224 struct net_bridge_vlan *masterv = NULL;
225 struct net_bridge_port *p = NULL;
226 struct net_bridge_vlan_group *vg;
227 struct net_device *dev;
228 struct net_bridge *br;
231 if (br_vlan_is_master(v)) {
234 vg = br_vlan_group(br);
239 vg = nbp_vlan_group(p);
243 /* Add VLAN to the device filter if it is supported.
244 * This ensures tagged traffic enters the bridge when
245 * promiscuous mode is disabled by br_manage_promisc().
247 err = __vlan_vid_add(dev, br, v, flags, extack);
251 /* need to work on the master vlan too */
252 if (flags & BRIDGE_VLAN_INFO_MASTER) {
255 err = br_vlan_add(br, v->vid,
256 flags | BRIDGE_VLAN_INFO_BRENTRY,
262 br_vlan_notify(br, NULL, v->vid, 0,
266 masterv = br_vlan_get_master(br, v->vid, extack);
272 if (br_opt_get(br, BROPT_VLAN_STATS_PER_PORT)) {
274 netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
279 v->priv_flags |= BR_VLFLAG_PER_PORT_STATS;
281 v->stats = masterv->stats;
284 err = br_switchdev_port_vlan_add(dev, v->vid, flags, extack);
285 if (err && err != -EOPNOTSUPP)
289 /* Add the dev mac and count the vlan only if it's usable */
290 if (br_vlan_should_use(v)) {
291 err = br_fdb_insert(br, p, dev->dev_addr, v->vid);
293 br_err(br, "failed insert local address into bridge forwarding table\n");
299 /* set the state before publishing */
300 v->state = BR_STATE_FORWARDING;
302 err = rhashtable_lookup_insert_fast(&vg->vlan_hash, &v->vnode,
308 __vlan_add_flags(v, flags);
311 nbp_vlan_set_vlan_dev_state(p, v->vid);
316 if (br_vlan_should_use(v)) {
317 br_fdb_find_delete_local(br, p, dev->dev_addr, v->vid);
323 __vlan_vid_del(dev, br, v);
325 if (v->stats && masterv->stats != v->stats)
326 free_percpu(v->stats);
329 br_vlan_put_master(masterv);
333 br_switchdev_port_vlan_del(dev, v->vid);
339 static int __vlan_del(struct net_bridge_vlan *v)
341 struct net_bridge_vlan *masterv = v;
342 struct net_bridge_vlan_group *vg;
343 struct net_bridge_port *p = NULL;
346 if (br_vlan_is_master(v)) {
347 vg = br_vlan_group(v->br);
350 vg = nbp_vlan_group(v->port);
354 __vlan_delete_pvid(vg, v->vid);
356 err = __vlan_vid_del(p->dev, p->br, v);
360 err = br_switchdev_port_vlan_del(v->br->dev, v->vid);
361 if (err && err != -EOPNOTSUPP)
366 if (br_vlan_should_use(v)) {
367 v->flags &= ~BRIDGE_VLAN_INFO_BRENTRY;
372 vlan_tunnel_info_del(vg, v);
373 rhashtable_remove_fast(&vg->vlan_hash, &v->vnode,
376 nbp_vlan_set_vlan_dev_state(p, v->vid);
377 call_rcu(&v->rcu, nbp_vlan_rcu_free);
380 br_vlan_put_master(masterv);
385 static void __vlan_group_free(struct net_bridge_vlan_group *vg)
387 WARN_ON(!list_empty(&vg->vlan_list));
388 rhashtable_destroy(&vg->vlan_hash);
389 vlan_tunnel_deinit(vg);
393 static void __vlan_flush(const struct net_bridge *br,
394 const struct net_bridge_port *p,
395 struct net_bridge_vlan_group *vg)
397 struct net_bridge_vlan *vlan, *tmp;
398 u16 v_start = 0, v_end = 0;
400 __vlan_delete_pvid(vg, vg->pvid);
401 list_for_each_entry_safe(vlan, tmp, &vg->vlan_list, vlist) {
402 /* take care of disjoint ranges */
405 } else if (vlan->vid - v_end != 1) {
406 /* found range end, notify and start next one */
407 br_vlan_notify(br, p, v_start, v_end, RTM_DELVLAN);
415 /* notify about the last/whole vlan range */
417 br_vlan_notify(br, p, v_start, v_end, RTM_DELVLAN);
420 struct sk_buff *br_handle_vlan(struct net_bridge *br,
421 const struct net_bridge_port *p,
422 struct net_bridge_vlan_group *vg,
425 struct pcpu_sw_netstats *stats;
426 struct net_bridge_vlan *v;
429 /* If this packet was not filtered at input, let it pass */
430 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
433 /* At this point, we know that the frame was filtered and contains
434 * a valid vlan id. If the vlan id has untagged flag set,
435 * send untagged; otherwise, send tagged.
437 br_vlan_get_tag(skb, &vid);
438 v = br_vlan_find(vg, vid);
439 /* Vlan entry must be configured at this point. The
440 * only exception is the bridge is set in promisc mode and the
441 * packet is destined for the bridge device. In this case
442 * pass the packet as is.
444 if (!v || !br_vlan_should_use(v)) {
445 if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) {
452 if (br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) {
453 stats = this_cpu_ptr(v->stats);
454 u64_stats_update_begin(&stats->syncp);
455 stats->tx_bytes += skb->len;
457 u64_stats_update_end(&stats->syncp);
460 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
461 __vlan_hwaccel_clear_tag(skb);
463 if (p && (p->flags & BR_VLAN_TUNNEL) &&
464 br_handle_egress_vlan_tunnel(skb, v)) {
472 /* Called under RCU */
473 static bool __allowed_ingress(const struct net_bridge *br,
474 struct net_bridge_vlan_group *vg,
475 struct sk_buff *skb, u16 *vid,
478 struct pcpu_sw_netstats *stats;
479 struct net_bridge_vlan *v;
482 BR_INPUT_SKB_CB(skb)->vlan_filtered = true;
483 /* If vlan tx offload is disabled on bridge device and frame was
484 * sent from vlan device on the bridge device, it does not have
485 * HW accelerated vlan tag.
487 if (unlikely(!skb_vlan_tag_present(skb) &&
488 skb->protocol == br->vlan_proto)) {
489 skb = skb_vlan_untag(skb);
494 if (!br_vlan_get_tag(skb, vid)) {
496 if (skb->vlan_proto != br->vlan_proto) {
497 /* Protocol-mismatch, empty out vlan_tci for new tag */
498 skb_push(skb, ETH_HLEN);
499 skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
500 skb_vlan_tag_get(skb));
504 skb_pull(skb, ETH_HLEN);
505 skb_reset_mac_len(skb);
517 u16 pvid = br_get_pvid(vg);
519 /* Frame had a tag with VID 0 or did not have a tag.
520 * See if pvid is set on this port. That tells us which
521 * vlan untagged or priority-tagged traffic belongs to.
526 /* PVID is set on this port. Any untagged or priority-tagged
527 * ingress frame is considered to belong to this vlan.
531 /* Untagged Frame. */
532 __vlan_hwaccel_put_tag(skb, br->vlan_proto, pvid);
534 /* Priority-tagged Frame.
535 * At this point, we know that skb->vlan_tci VID
537 * We update only VID field and preserve PCP field.
539 skb->vlan_tci |= pvid;
541 /* if stats are disabled we can avoid the lookup */
542 if (!br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) {
543 if (*state == BR_STATE_FORWARDING) {
544 *state = br_vlan_get_pvid_state(vg);
545 return br_vlan_state_allowed(*state, true);
551 v = br_vlan_find(vg, *vid);
552 if (!v || !br_vlan_should_use(v))
555 if (*state == BR_STATE_FORWARDING) {
556 *state = br_vlan_get_state(v);
557 if (!br_vlan_state_allowed(*state, true))
561 if (br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) {
562 stats = this_cpu_ptr(v->stats);
563 u64_stats_update_begin(&stats->syncp);
564 stats->rx_bytes += skb->len;
566 u64_stats_update_end(&stats->syncp);
576 bool br_allowed_ingress(const struct net_bridge *br,
577 struct net_bridge_vlan_group *vg, struct sk_buff *skb,
580 /* If VLAN filtering is disabled on the bridge, all packets are
583 if (!br_opt_get(br, BROPT_VLAN_ENABLED)) {
584 BR_INPUT_SKB_CB(skb)->vlan_filtered = false;
588 return __allowed_ingress(br, vg, skb, vid, state);
591 /* Called under RCU. */
592 bool br_allowed_egress(struct net_bridge_vlan_group *vg,
593 const struct sk_buff *skb)
595 const struct net_bridge_vlan *v;
598 /* If this packet was not filtered at input, let it pass */
599 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
602 br_vlan_get_tag(skb, &vid);
603 v = br_vlan_find(vg, vid);
604 if (v && br_vlan_should_use(v) &&
605 br_vlan_state_allowed(br_vlan_get_state(v), false))
611 /* Called under RCU */
612 bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid)
614 struct net_bridge_vlan_group *vg;
615 struct net_bridge *br = p->br;
616 struct net_bridge_vlan *v;
618 /* If filtering was disabled at input, let it pass. */
619 if (!br_opt_get(br, BROPT_VLAN_ENABLED))
622 vg = nbp_vlan_group_rcu(p);
623 if (!vg || !vg->num_vlans)
626 if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto)
630 *vid = br_get_pvid(vg);
632 !br_vlan_state_allowed(br_vlan_get_pvid_state(vg), true))
638 v = br_vlan_find(vg, *vid);
639 if (v && br_vlan_state_allowed(br_vlan_get_state(v), true))
645 static int br_vlan_add_existing(struct net_bridge *br,
646 struct net_bridge_vlan_group *vg,
647 struct net_bridge_vlan *vlan,
648 u16 flags, bool *changed,
649 struct netlink_ext_ack *extack)
653 err = br_switchdev_port_vlan_add(br->dev, vlan->vid, flags, extack);
654 if (err && err != -EOPNOTSUPP)
657 if (!br_vlan_is_brentry(vlan)) {
658 /* Trying to change flags of non-existent bridge vlan */
659 if (!(flags & BRIDGE_VLAN_INFO_BRENTRY)) {
663 /* It was only kept for port vlans, now make it real */
664 err = br_fdb_insert(br, NULL, br->dev->dev_addr,
667 br_err(br, "failed to insert local address into bridge forwarding table\n");
671 refcount_inc(&vlan->refcnt);
672 vlan->flags |= BRIDGE_VLAN_INFO_BRENTRY;
677 if (__vlan_add_flags(vlan, flags))
684 br_switchdev_port_vlan_del(br->dev, vlan->vid);
688 /* Must be protected by RTNL.
689 * Must be called with vid in range from 1 to 4094 inclusive.
690 * changed must be true only if the vlan was created or updated
692 int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags, bool *changed,
693 struct netlink_ext_ack *extack)
695 struct net_bridge_vlan_group *vg;
696 struct net_bridge_vlan *vlan;
702 vg = br_vlan_group(br);
703 vlan = br_vlan_find(vg, vid);
705 return br_vlan_add_existing(br, vg, vlan, flags, changed,
708 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
712 vlan->stats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
718 vlan->flags = flags | BRIDGE_VLAN_INFO_MASTER;
719 vlan->flags &= ~BRIDGE_VLAN_INFO_PVID;
721 if (flags & BRIDGE_VLAN_INFO_BRENTRY)
722 refcount_set(&vlan->refcnt, 1);
723 ret = __vlan_add(vlan, flags, extack);
725 free_percpu(vlan->stats);
734 /* Must be protected by RTNL.
735 * Must be called with vid in range from 1 to 4094 inclusive.
737 int br_vlan_delete(struct net_bridge *br, u16 vid)
739 struct net_bridge_vlan_group *vg;
740 struct net_bridge_vlan *v;
744 vg = br_vlan_group(br);
745 v = br_vlan_find(vg, vid);
746 if (!v || !br_vlan_is_brentry(v))
749 br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
750 br_fdb_delete_by_port(br, NULL, vid, 0);
752 vlan_tunnel_info_del(vg, v);
754 return __vlan_del(v);
757 void br_vlan_flush(struct net_bridge *br)
759 struct net_bridge_vlan_group *vg;
763 vg = br_vlan_group(br);
764 __vlan_flush(br, NULL, vg);
765 RCU_INIT_POINTER(br->vlgrp, NULL);
767 __vlan_group_free(vg);
770 struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid)
775 return br_vlan_lookup(&vg->vlan_hash, vid);
778 /* Must be protected by RTNL. */
779 static void recalculate_group_addr(struct net_bridge *br)
781 if (br_opt_get(br, BROPT_GROUP_ADDR_SET))
784 spin_lock_bh(&br->lock);
785 if (!br_opt_get(br, BROPT_VLAN_ENABLED) ||
786 br->vlan_proto == htons(ETH_P_8021Q)) {
787 /* Bridge Group Address */
788 br->group_addr[5] = 0x00;
789 } else { /* vlan_enabled && ETH_P_8021AD */
790 /* Provider Bridge Group Address */
791 br->group_addr[5] = 0x08;
793 spin_unlock_bh(&br->lock);
796 /* Must be protected by RTNL. */
797 void br_recalculate_fwd_mask(struct net_bridge *br)
799 if (!br_opt_get(br, BROPT_VLAN_ENABLED) ||
800 br->vlan_proto == htons(ETH_P_8021Q))
801 br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT;
802 else /* vlan_enabled && ETH_P_8021AD */
803 br->group_fwd_mask_required = BR_GROUPFWD_8021AD &
804 ~(1u << br->group_addr[5]);
807 int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val,
808 struct netlink_ext_ack *extack)
810 struct switchdev_attr attr = {
812 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
813 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
814 .u.vlan_filtering = val,
818 if (br_opt_get(br, BROPT_VLAN_ENABLED) == !!val)
821 err = switchdev_port_attr_set(br->dev, &attr, extack);
822 if (err && err != -EOPNOTSUPP)
825 br_opt_toggle(br, BROPT_VLAN_ENABLED, !!val);
826 br_manage_promisc(br);
827 recalculate_group_addr(br);
828 br_recalculate_fwd_mask(br);
833 bool br_vlan_enabled(const struct net_device *dev)
835 struct net_bridge *br = netdev_priv(dev);
837 return br_opt_get(br, BROPT_VLAN_ENABLED);
839 EXPORT_SYMBOL_GPL(br_vlan_enabled);
841 int br_vlan_get_proto(const struct net_device *dev, u16 *p_proto)
843 struct net_bridge *br = netdev_priv(dev);
845 *p_proto = ntohs(br->vlan_proto);
849 EXPORT_SYMBOL_GPL(br_vlan_get_proto);
851 int __br_vlan_set_proto(struct net_bridge *br, __be16 proto,
852 struct netlink_ext_ack *extack)
854 struct switchdev_attr attr = {
856 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_PROTOCOL,
857 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
858 .u.vlan_protocol = ntohs(proto),
861 struct net_bridge_port *p;
862 struct net_bridge_vlan *vlan;
863 struct net_bridge_vlan_group *vg;
864 __be16 oldproto = br->vlan_proto;
866 if (br->vlan_proto == proto)
869 err = switchdev_port_attr_set(br->dev, &attr, extack);
870 if (err && err != -EOPNOTSUPP)
873 /* Add VLANs for the new proto to the device filter. */
874 list_for_each_entry(p, &br->port_list, list) {
875 vg = nbp_vlan_group(p);
876 list_for_each_entry(vlan, &vg->vlan_list, vlist) {
877 err = vlan_vid_add(p->dev, proto, vlan->vid);
883 br->vlan_proto = proto;
885 recalculate_group_addr(br);
886 br_recalculate_fwd_mask(br);
888 /* Delete VLANs for the old proto from the device filter. */
889 list_for_each_entry(p, &br->port_list, list) {
890 vg = nbp_vlan_group(p);
891 list_for_each_entry(vlan, &vg->vlan_list, vlist)
892 vlan_vid_del(p->dev, oldproto, vlan->vid);
898 attr.u.vlan_protocol = ntohs(oldproto);
899 switchdev_port_attr_set(br->dev, &attr, NULL);
901 list_for_each_entry_continue_reverse(vlan, &vg->vlan_list, vlist)
902 vlan_vid_del(p->dev, proto, vlan->vid);
904 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
905 vg = nbp_vlan_group(p);
906 list_for_each_entry(vlan, &vg->vlan_list, vlist)
907 vlan_vid_del(p->dev, proto, vlan->vid);
913 int br_vlan_set_proto(struct net_bridge *br, unsigned long val,
914 struct netlink_ext_ack *extack)
916 if (!eth_type_vlan(htons(val)))
917 return -EPROTONOSUPPORT;
919 return __br_vlan_set_proto(br, htons(val), extack);
922 int br_vlan_set_stats(struct net_bridge *br, unsigned long val)
927 br_opt_toggle(br, BROPT_VLAN_STATS_ENABLED, !!val);
936 int br_vlan_set_stats_per_port(struct net_bridge *br, unsigned long val)
938 struct net_bridge_port *p;
940 /* allow to change the option if there are no port vlans configured */
941 list_for_each_entry(p, &br->port_list, list) {
942 struct net_bridge_vlan_group *vg = nbp_vlan_group(p);
951 br_opt_toggle(br, BROPT_VLAN_STATS_PER_PORT, !!val);
960 static bool vlan_default_pvid(struct net_bridge_vlan_group *vg, u16 vid)
962 struct net_bridge_vlan *v;
967 v = br_vlan_lookup(&vg->vlan_hash, vid);
968 if (v && br_vlan_should_use(v) &&
969 (v->flags & BRIDGE_VLAN_INFO_UNTAGGED))
975 static void br_vlan_disable_default_pvid(struct net_bridge *br)
977 struct net_bridge_port *p;
978 u16 pvid = br->default_pvid;
980 /* Disable default_pvid on all ports where it is still
983 if (vlan_default_pvid(br_vlan_group(br), pvid)) {
984 if (!br_vlan_delete(br, pvid))
985 br_vlan_notify(br, NULL, pvid, 0, RTM_DELVLAN);
988 list_for_each_entry(p, &br->port_list, list) {
989 if (vlan_default_pvid(nbp_vlan_group(p), pvid) &&
990 !nbp_vlan_delete(p, pvid))
991 br_vlan_notify(br, p, pvid, 0, RTM_DELVLAN);
994 br->default_pvid = 0;
997 int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid,
998 struct netlink_ext_ack *extack)
1000 const struct net_bridge_vlan *pvent;
1001 struct net_bridge_vlan_group *vg;
1002 struct net_bridge_port *p;
1003 unsigned long *changed;
1009 br_vlan_disable_default_pvid(br);
1013 changed = bitmap_zalloc(BR_MAX_PORTS, GFP_KERNEL);
1017 old_pvid = br->default_pvid;
1019 /* Update default_pvid config only if we do not conflict with
1020 * user configuration.
1022 vg = br_vlan_group(br);
1023 pvent = br_vlan_find(vg, pvid);
1024 if ((!old_pvid || vlan_default_pvid(vg, old_pvid)) &&
1025 (!pvent || !br_vlan_should_use(pvent))) {
1026 err = br_vlan_add(br, pvid,
1027 BRIDGE_VLAN_INFO_PVID |
1028 BRIDGE_VLAN_INFO_UNTAGGED |
1029 BRIDGE_VLAN_INFO_BRENTRY,
1034 if (br_vlan_delete(br, old_pvid))
1035 br_vlan_notify(br, NULL, old_pvid, 0, RTM_DELVLAN);
1036 br_vlan_notify(br, NULL, pvid, 0, RTM_NEWVLAN);
1037 set_bit(0, changed);
1040 list_for_each_entry(p, &br->port_list, list) {
1041 /* Update default_pvid config only if we do not conflict with
1042 * user configuration.
1044 vg = nbp_vlan_group(p);
1046 !vlan_default_pvid(vg, old_pvid)) ||
1047 br_vlan_find(vg, pvid))
1050 err = nbp_vlan_add(p, pvid,
1051 BRIDGE_VLAN_INFO_PVID |
1052 BRIDGE_VLAN_INFO_UNTAGGED,
1056 if (nbp_vlan_delete(p, old_pvid))
1057 br_vlan_notify(br, p, old_pvid, 0, RTM_DELVLAN);
1058 br_vlan_notify(p->br, p, pvid, 0, RTM_NEWVLAN);
1059 set_bit(p->port_no, changed);
1062 br->default_pvid = pvid;
1065 bitmap_free(changed);
1069 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
1070 if (!test_bit(p->port_no, changed))
1074 nbp_vlan_add(p, old_pvid,
1075 BRIDGE_VLAN_INFO_PVID |
1076 BRIDGE_VLAN_INFO_UNTAGGED,
1078 br_vlan_notify(p->br, p, old_pvid, 0, RTM_NEWVLAN);
1080 nbp_vlan_delete(p, pvid);
1081 br_vlan_notify(br, p, pvid, 0, RTM_DELVLAN);
1084 if (test_bit(0, changed)) {
1086 br_vlan_add(br, old_pvid,
1087 BRIDGE_VLAN_INFO_PVID |
1088 BRIDGE_VLAN_INFO_UNTAGGED |
1089 BRIDGE_VLAN_INFO_BRENTRY,
1091 br_vlan_notify(br, NULL, old_pvid, 0, RTM_NEWVLAN);
1093 br_vlan_delete(br, pvid);
1094 br_vlan_notify(br, NULL, pvid, 0, RTM_DELVLAN);
1099 int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val,
1100 struct netlink_ext_ack *extack)
1105 if (val >= VLAN_VID_MASK)
1108 if (pvid == br->default_pvid)
1111 /* Only allow default pvid change when filtering is disabled */
1112 if (br_opt_get(br, BROPT_VLAN_ENABLED)) {
1113 pr_info_once("Please disable vlan filtering to change default_pvid\n");
1117 err = __br_vlan_set_default_pvid(br, pvid, extack);
1122 int br_vlan_init(struct net_bridge *br)
1124 struct net_bridge_vlan_group *vg;
1127 vg = kzalloc(sizeof(*vg), GFP_KERNEL);
1130 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
1133 ret = vlan_tunnel_init(vg);
1135 goto err_tunnel_init;
1136 INIT_LIST_HEAD(&vg->vlan_list);
1137 br->vlan_proto = htons(ETH_P_8021Q);
1138 br->default_pvid = 1;
1139 rcu_assign_pointer(br->vlgrp, vg);
1145 rhashtable_destroy(&vg->vlan_hash);
1152 int nbp_vlan_init(struct net_bridge_port *p, struct netlink_ext_ack *extack)
1154 struct switchdev_attr attr = {
1155 .orig_dev = p->br->dev,
1156 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
1157 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
1158 .u.vlan_filtering = br_opt_get(p->br, BROPT_VLAN_ENABLED),
1160 struct net_bridge_vlan_group *vg;
1163 vg = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL);
1167 ret = switchdev_port_attr_set(p->dev, &attr, extack);
1168 if (ret && ret != -EOPNOTSUPP)
1169 goto err_vlan_enabled;
1171 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
1174 ret = vlan_tunnel_init(vg);
1176 goto err_tunnel_init;
1177 INIT_LIST_HEAD(&vg->vlan_list);
1178 rcu_assign_pointer(p->vlgrp, vg);
1179 if (p->br->default_pvid) {
1182 ret = nbp_vlan_add(p, p->br->default_pvid,
1183 BRIDGE_VLAN_INFO_PVID |
1184 BRIDGE_VLAN_INFO_UNTAGGED,
1188 br_vlan_notify(p->br, p, p->br->default_pvid, 0, RTM_NEWVLAN);
1194 RCU_INIT_POINTER(p->vlgrp, NULL);
1196 vlan_tunnel_deinit(vg);
1198 rhashtable_destroy(&vg->vlan_hash);
1206 /* Must be protected by RTNL.
1207 * Must be called with vid in range from 1 to 4094 inclusive.
1208 * changed must be true only if the vlan was created or updated
1210 int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags,
1211 bool *changed, struct netlink_ext_ack *extack)
1213 struct net_bridge_vlan *vlan;
1219 vlan = br_vlan_find(nbp_vlan_group(port), vid);
1221 /* Pass the flags to the hardware bridge */
1222 ret = br_switchdev_port_vlan_add(port->dev, vid, flags, extack);
1223 if (ret && ret != -EOPNOTSUPP)
1225 *changed = __vlan_add_flags(vlan, flags);
1230 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
1236 ret = __vlan_add(vlan, flags, extack);
1245 /* Must be protected by RTNL.
1246 * Must be called with vid in range from 1 to 4094 inclusive.
1248 int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
1250 struct net_bridge_vlan *v;
1254 v = br_vlan_find(nbp_vlan_group(port), vid);
1257 br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
1258 br_fdb_delete_by_port(port->br, port, vid, 0);
1260 return __vlan_del(v);
1263 void nbp_vlan_flush(struct net_bridge_port *port)
1265 struct net_bridge_vlan_group *vg;
1269 vg = nbp_vlan_group(port);
1270 __vlan_flush(port->br, port, vg);
1271 RCU_INIT_POINTER(port->vlgrp, NULL);
1273 __vlan_group_free(vg);
1276 void br_vlan_get_stats(const struct net_bridge_vlan *v,
1277 struct pcpu_sw_netstats *stats)
1281 memset(stats, 0, sizeof(*stats));
1282 for_each_possible_cpu(i) {
1283 u64 rxpackets, rxbytes, txpackets, txbytes;
1284 struct pcpu_sw_netstats *cpu_stats;
1287 cpu_stats = per_cpu_ptr(v->stats, i);
1289 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
1290 rxpackets = cpu_stats->rx_packets;
1291 rxbytes = cpu_stats->rx_bytes;
1292 txbytes = cpu_stats->tx_bytes;
1293 txpackets = cpu_stats->tx_packets;
1294 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
1296 stats->rx_packets += rxpackets;
1297 stats->rx_bytes += rxbytes;
1298 stats->tx_bytes += txbytes;
1299 stats->tx_packets += txpackets;
1303 int br_vlan_get_pvid(const struct net_device *dev, u16 *p_pvid)
1305 struct net_bridge_vlan_group *vg;
1306 struct net_bridge_port *p;
1309 p = br_port_get_check_rtnl(dev);
1311 vg = nbp_vlan_group(p);
1312 else if (netif_is_bridge_master(dev))
1313 vg = br_vlan_group(netdev_priv(dev));
1317 *p_pvid = br_get_pvid(vg);
1320 EXPORT_SYMBOL_GPL(br_vlan_get_pvid);
1322 int br_vlan_get_pvid_rcu(const struct net_device *dev, u16 *p_pvid)
1324 struct net_bridge_vlan_group *vg;
1325 struct net_bridge_port *p;
1327 p = br_port_get_check_rcu(dev);
1329 vg = nbp_vlan_group_rcu(p);
1330 else if (netif_is_bridge_master(dev))
1331 vg = br_vlan_group_rcu(netdev_priv(dev));
1335 *p_pvid = br_get_pvid(vg);
1338 EXPORT_SYMBOL_GPL(br_vlan_get_pvid_rcu);
1340 void br_vlan_fill_forward_path_pvid(struct net_bridge *br,
1341 struct net_device_path_ctx *ctx,
1342 struct net_device_path *path)
1344 struct net_bridge_vlan_group *vg;
1345 int idx = ctx->num_vlans - 1;
1348 path->bridge.vlan_mode = DEV_PATH_BR_VLAN_KEEP;
1350 if (!br_opt_get(br, BROPT_VLAN_ENABLED))
1353 vg = br_vlan_group(br);
1356 ctx->vlan[idx].proto == br->vlan_proto) {
1357 vid = ctx->vlan[idx].id;
1359 path->bridge.vlan_mode = DEV_PATH_BR_VLAN_TAG;
1360 vid = br_get_pvid(vg);
1363 path->bridge.vlan_id = vid;
1364 path->bridge.vlan_proto = br->vlan_proto;
1367 int br_vlan_fill_forward_path_mode(struct net_bridge *br,
1368 struct net_bridge_port *dst,
1369 struct net_device_path *path)
1371 struct net_bridge_vlan_group *vg;
1372 struct net_bridge_vlan *v;
1374 if (!br_opt_get(br, BROPT_VLAN_ENABLED))
1377 vg = nbp_vlan_group_rcu(dst);
1378 v = br_vlan_find(vg, path->bridge.vlan_id);
1379 if (!v || !br_vlan_should_use(v))
1382 if (!(v->flags & BRIDGE_VLAN_INFO_UNTAGGED))
1385 if (path->bridge.vlan_mode == DEV_PATH_BR_VLAN_TAG)
1386 path->bridge.vlan_mode = DEV_PATH_BR_VLAN_KEEP;
1387 else if (v->priv_flags & BR_VLFLAG_ADDED_BY_SWITCHDEV)
1388 path->bridge.vlan_mode = DEV_PATH_BR_VLAN_UNTAG_HW;
1390 path->bridge.vlan_mode = DEV_PATH_BR_VLAN_UNTAG;
1395 int br_vlan_get_info(const struct net_device *dev, u16 vid,
1396 struct bridge_vlan_info *p_vinfo)
1398 struct net_bridge_vlan_group *vg;
1399 struct net_bridge_vlan *v;
1400 struct net_bridge_port *p;
1403 p = br_port_get_check_rtnl(dev);
1405 vg = nbp_vlan_group(p);
1406 else if (netif_is_bridge_master(dev))
1407 vg = br_vlan_group(netdev_priv(dev));
1411 v = br_vlan_find(vg, vid);
1416 p_vinfo->flags = v->flags;
1417 if (vid == br_get_pvid(vg))
1418 p_vinfo->flags |= BRIDGE_VLAN_INFO_PVID;
1421 EXPORT_SYMBOL_GPL(br_vlan_get_info);
1423 static int br_vlan_is_bind_vlan_dev(const struct net_device *dev)
1425 return is_vlan_dev(dev) &&
1426 !!(vlan_dev_priv(dev)->flags & VLAN_FLAG_BRIDGE_BINDING);
1429 static int br_vlan_is_bind_vlan_dev_fn(struct net_device *dev,
1430 __always_unused struct netdev_nested_priv *priv)
1432 return br_vlan_is_bind_vlan_dev(dev);
1435 static bool br_vlan_has_upper_bind_vlan_dev(struct net_device *dev)
1440 found = netdev_walk_all_upper_dev_rcu(dev, br_vlan_is_bind_vlan_dev_fn,
1447 struct br_vlan_bind_walk_data {
1449 struct net_device *result;
1452 static int br_vlan_match_bind_vlan_dev_fn(struct net_device *dev,
1453 struct netdev_nested_priv *priv)
1455 struct br_vlan_bind_walk_data *data = priv->data;
1458 if (br_vlan_is_bind_vlan_dev(dev) &&
1459 vlan_dev_priv(dev)->vlan_id == data->vid) {
1467 static struct net_device *
1468 br_vlan_get_upper_bind_vlan_dev(struct net_device *dev, u16 vid)
1470 struct br_vlan_bind_walk_data data = {
1473 struct netdev_nested_priv priv = {
1474 .data = (void *)&data,
1478 netdev_walk_all_upper_dev_rcu(dev, br_vlan_match_bind_vlan_dev_fn,
1485 static bool br_vlan_is_dev_up(const struct net_device *dev)
1487 return !!(dev->flags & IFF_UP) && netif_oper_up(dev);
1490 static void br_vlan_set_vlan_dev_state(const struct net_bridge *br,
1491 struct net_device *vlan_dev)
1493 u16 vid = vlan_dev_priv(vlan_dev)->vlan_id;
1494 struct net_bridge_vlan_group *vg;
1495 struct net_bridge_port *p;
1496 bool has_carrier = false;
1498 if (!netif_carrier_ok(br->dev)) {
1499 netif_carrier_off(vlan_dev);
1503 list_for_each_entry(p, &br->port_list, list) {
1504 vg = nbp_vlan_group(p);
1505 if (br_vlan_find(vg, vid) && br_vlan_is_dev_up(p->dev)) {
1512 netif_carrier_on(vlan_dev);
1514 netif_carrier_off(vlan_dev);
1517 static void br_vlan_set_all_vlan_dev_state(struct net_bridge_port *p)
1519 struct net_bridge_vlan_group *vg = nbp_vlan_group(p);
1520 struct net_bridge_vlan *vlan;
1521 struct net_device *vlan_dev;
1523 list_for_each_entry(vlan, &vg->vlan_list, vlist) {
1524 vlan_dev = br_vlan_get_upper_bind_vlan_dev(p->br->dev,
1527 if (br_vlan_is_dev_up(p->dev)) {
1528 if (netif_carrier_ok(p->br->dev))
1529 netif_carrier_on(vlan_dev);
1531 br_vlan_set_vlan_dev_state(p->br, vlan_dev);
1537 static void br_vlan_upper_change(struct net_device *dev,
1538 struct net_device *upper_dev,
1541 struct net_bridge *br = netdev_priv(dev);
1543 if (!br_vlan_is_bind_vlan_dev(upper_dev))
1547 br_vlan_set_vlan_dev_state(br, upper_dev);
1548 br_opt_toggle(br, BROPT_VLAN_BRIDGE_BINDING, true);
1550 br_opt_toggle(br, BROPT_VLAN_BRIDGE_BINDING,
1551 br_vlan_has_upper_bind_vlan_dev(dev));
1555 struct br_vlan_link_state_walk_data {
1556 struct net_bridge *br;
1559 static int br_vlan_link_state_change_fn(struct net_device *vlan_dev,
1560 struct netdev_nested_priv *priv)
1562 struct br_vlan_link_state_walk_data *data = priv->data;
1564 if (br_vlan_is_bind_vlan_dev(vlan_dev))
1565 br_vlan_set_vlan_dev_state(data->br, vlan_dev);
1570 static void br_vlan_link_state_change(struct net_device *dev,
1571 struct net_bridge *br)
1573 struct br_vlan_link_state_walk_data data = {
1576 struct netdev_nested_priv priv = {
1577 .data = (void *)&data,
1581 netdev_walk_all_upper_dev_rcu(dev, br_vlan_link_state_change_fn,
1586 /* Must be protected by RTNL. */
1587 static void nbp_vlan_set_vlan_dev_state(struct net_bridge_port *p, u16 vid)
1589 struct net_device *vlan_dev;
1591 if (!br_opt_get(p->br, BROPT_VLAN_BRIDGE_BINDING))
1594 vlan_dev = br_vlan_get_upper_bind_vlan_dev(p->br->dev, vid);
1596 br_vlan_set_vlan_dev_state(p->br, vlan_dev);
1599 /* Must be protected by RTNL. */
1600 int br_vlan_bridge_event(struct net_device *dev, unsigned long event, void *ptr)
1602 struct netdev_notifier_changeupper_info *info;
1603 struct net_bridge *br = netdev_priv(dev);
1604 int vlcmd = 0, ret = 0;
1605 bool changed = false;
1608 case NETDEV_REGISTER:
1609 ret = br_vlan_add(br, br->default_pvid,
1610 BRIDGE_VLAN_INFO_PVID |
1611 BRIDGE_VLAN_INFO_UNTAGGED |
1612 BRIDGE_VLAN_INFO_BRENTRY, &changed, NULL);
1613 vlcmd = RTM_NEWVLAN;
1615 case NETDEV_UNREGISTER:
1616 changed = !br_vlan_delete(br, br->default_pvid);
1617 vlcmd = RTM_DELVLAN;
1619 case NETDEV_CHANGEUPPER:
1621 br_vlan_upper_change(dev, info->upper_dev, info->linking);
1626 if (!br_opt_get(br, BROPT_VLAN_BRIDGE_BINDING))
1628 br_vlan_link_state_change(dev, br);
1632 br_vlan_notify(br, NULL, br->default_pvid, 0, vlcmd);
1637 /* Must be protected by RTNL. */
1638 void br_vlan_port_event(struct net_bridge_port *p, unsigned long event)
1640 if (!br_opt_get(p->br, BROPT_VLAN_BRIDGE_BINDING))
1647 br_vlan_set_all_vlan_dev_state(p);
1652 static bool br_vlan_stats_fill(struct sk_buff *skb,
1653 const struct net_bridge_vlan *v)
1655 struct pcpu_sw_netstats stats;
1656 struct nlattr *nest;
1658 nest = nla_nest_start(skb, BRIDGE_VLANDB_ENTRY_STATS);
1662 br_vlan_get_stats(v, &stats);
1663 if (nla_put_u64_64bit(skb, BRIDGE_VLANDB_STATS_RX_BYTES, stats.rx_bytes,
1664 BRIDGE_VLANDB_STATS_PAD) ||
1665 nla_put_u64_64bit(skb, BRIDGE_VLANDB_STATS_RX_PACKETS,
1666 stats.rx_packets, BRIDGE_VLANDB_STATS_PAD) ||
1667 nla_put_u64_64bit(skb, BRIDGE_VLANDB_STATS_TX_BYTES, stats.tx_bytes,
1668 BRIDGE_VLANDB_STATS_PAD) ||
1669 nla_put_u64_64bit(skb, BRIDGE_VLANDB_STATS_TX_PACKETS,
1670 stats.tx_packets, BRIDGE_VLANDB_STATS_PAD))
1673 nla_nest_end(skb, nest);
1678 nla_nest_cancel(skb, nest);
1682 /* v_opts is used to dump the options which must be equal in the whole range */
1683 static bool br_vlan_fill_vids(struct sk_buff *skb, u16 vid, u16 vid_range,
1684 const struct net_bridge_vlan *v_opts,
1688 struct bridge_vlan_info info;
1689 struct nlattr *nest;
1691 nest = nla_nest_start(skb, BRIDGE_VLANDB_ENTRY);
1695 memset(&info, 0, sizeof(info));
1697 if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
1698 info.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1699 if (flags & BRIDGE_VLAN_INFO_PVID)
1700 info.flags |= BRIDGE_VLAN_INFO_PVID;
1702 if (nla_put(skb, BRIDGE_VLANDB_ENTRY_INFO, sizeof(info), &info))
1705 if (vid_range && vid < vid_range &&
1706 !(flags & BRIDGE_VLAN_INFO_PVID) &&
1707 nla_put_u16(skb, BRIDGE_VLANDB_ENTRY_RANGE, vid_range))
1711 if (!br_vlan_opts_fill(skb, v_opts))
1714 if (dump_stats && !br_vlan_stats_fill(skb, v_opts))
1718 nla_nest_end(skb, nest);
1723 nla_nest_cancel(skb, nest);
1727 static size_t rtnl_vlan_nlmsg_size(void)
1729 return NLMSG_ALIGN(sizeof(struct br_vlan_msg))
1730 + nla_total_size(0) /* BRIDGE_VLANDB_ENTRY */
1731 + nla_total_size(sizeof(u16)) /* BRIDGE_VLANDB_ENTRY_RANGE */
1732 + nla_total_size(sizeof(struct bridge_vlan_info)) /* BRIDGE_VLANDB_ENTRY_INFO */
1733 + br_vlan_opts_nl_size(); /* bridge vlan options */
1736 void br_vlan_notify(const struct net_bridge *br,
1737 const struct net_bridge_port *p,
1738 u16 vid, u16 vid_range,
1741 struct net_bridge_vlan_group *vg;
1742 struct net_bridge_vlan *v = NULL;
1743 struct br_vlan_msg *bvm;
1744 struct nlmsghdr *nlh;
1745 struct sk_buff *skb;
1751 /* right now notifications are done only with rtnl held */
1755 ifindex = p->dev->ifindex;
1756 vg = nbp_vlan_group(p);
1757 net = dev_net(p->dev);
1759 ifindex = br->dev->ifindex;
1760 vg = br_vlan_group(br);
1761 net = dev_net(br->dev);
1764 skb = nlmsg_new(rtnl_vlan_nlmsg_size(), GFP_KERNEL);
1769 nlh = nlmsg_put(skb, 0, 0, cmd, sizeof(*bvm), 0);
1772 bvm = nlmsg_data(nlh);
1773 memset(bvm, 0, sizeof(*bvm));
1774 bvm->family = AF_BRIDGE;
1775 bvm->ifindex = ifindex;
1779 /* need to find the vlan due to flags/options */
1780 v = br_vlan_find(vg, vid);
1781 if (!v || !br_vlan_should_use(v))
1785 if (br_get_pvid(vg) == v->vid)
1786 flags |= BRIDGE_VLAN_INFO_PVID;
1794 if (!br_vlan_fill_vids(skb, vid, vid_range, v, flags, false))
1797 nlmsg_end(skb, nlh);
1798 rtnl_notify(skb, net, 0, RTNLGRP_BRVLAN, NULL, GFP_KERNEL);
1802 rtnl_set_sk_err(net, RTNLGRP_BRVLAN, err);
1807 static int br_vlan_replay_one(struct notifier_block *nb,
1808 struct net_device *dev,
1809 struct switchdev_obj_port_vlan *vlan,
1810 const void *ctx, unsigned long action,
1811 struct netlink_ext_ack *extack)
1813 struct switchdev_notifier_port_obj_info obj_info = {
1823 err = nb->notifier_call(nb, action, &obj_info);
1824 return notifier_to_errno(err);
1827 int br_vlan_replay(struct net_device *br_dev, struct net_device *dev,
1828 const void *ctx, bool adding, struct notifier_block *nb,
1829 struct netlink_ext_ack *extack)
1831 struct net_bridge_vlan_group *vg;
1832 struct net_bridge_vlan *v;
1833 struct net_bridge_port *p;
1834 struct net_bridge *br;
1835 unsigned long action;
1841 if (!netif_is_bridge_master(br_dev))
1844 if (!netif_is_bridge_master(dev) && !netif_is_bridge_port(dev))
1847 if (netif_is_bridge_master(dev)) {
1848 br = netdev_priv(dev);
1849 vg = br_vlan_group(br);
1852 p = br_port_get_rtnl(dev);
1855 vg = nbp_vlan_group(p);
1863 action = SWITCHDEV_PORT_OBJ_ADD;
1865 action = SWITCHDEV_PORT_OBJ_DEL;
1867 pvid = br_get_pvid(vg);
1869 list_for_each_entry(v, &vg->vlan_list, vlist) {
1870 struct switchdev_obj_port_vlan vlan = {
1871 .obj.orig_dev = dev,
1872 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
1873 .flags = br_vlan_flags(v, pvid),
1877 if (!br_vlan_should_use(v))
1880 err = br_vlan_replay_one(nb, dev, &vlan, ctx, action, extack);
1887 EXPORT_SYMBOL_GPL(br_vlan_replay);
1889 /* check if v_curr can enter a range ending in range_end */
1890 bool br_vlan_can_enter_range(const struct net_bridge_vlan *v_curr,
1891 const struct net_bridge_vlan *range_end)
1893 return v_curr->vid - range_end->vid == 1 &&
1894 range_end->flags == v_curr->flags &&
1895 br_vlan_opts_eq_range(v_curr, range_end);
1898 static int br_vlan_dump_dev(const struct net_device *dev,
1899 struct sk_buff *skb,
1900 struct netlink_callback *cb,
1903 struct net_bridge_vlan *v, *range_start = NULL, *range_end = NULL;
1904 bool dump_stats = !!(dump_flags & BRIDGE_VLANDB_DUMPF_STATS);
1905 struct net_bridge_vlan_group *vg;
1906 int idx = 0, s_idx = cb->args[1];
1907 struct nlmsghdr *nlh = NULL;
1908 struct net_bridge_port *p;
1909 struct br_vlan_msg *bvm;
1910 struct net_bridge *br;
1914 if (!netif_is_bridge_master(dev) && !netif_is_bridge_port(dev))
1917 if (netif_is_bridge_master(dev)) {
1918 br = netdev_priv(dev);
1919 vg = br_vlan_group_rcu(br);
1922 p = br_port_get_rcu(dev);
1925 vg = nbp_vlan_group_rcu(p);
1932 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1933 RTM_NEWVLAN, sizeof(*bvm), NLM_F_MULTI);
1936 bvm = nlmsg_data(nlh);
1937 memset(bvm, 0, sizeof(*bvm));
1938 bvm->family = PF_BRIDGE;
1939 bvm->ifindex = dev->ifindex;
1940 pvid = br_get_pvid(vg);
1942 /* idx must stay at range's beginning until it is filled in */
1943 list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
1944 if (!br_vlan_should_use(v))
1957 if (dump_stats || v->vid == pvid ||
1958 !br_vlan_can_enter_range(v, range_end)) {
1959 u16 vlan_flags = br_vlan_flags(range_start, pvid);
1961 if (!br_vlan_fill_vids(skb, range_start->vid,
1962 range_end->vid, range_start,
1963 vlan_flags, dump_stats)) {
1967 /* advance number of filled vlans */
1968 idx += range_end->vid - range_start->vid + 1;
1975 /* err will be 0 and range_start will be set in 3 cases here:
1976 * - first vlan (range_start == range_end)
1977 * - last vlan (range_start == range_end, not in range)
1978 * - last vlan range (range_start != range_end, in range)
1980 if (!err && range_start &&
1981 !br_vlan_fill_vids(skb, range_start->vid, range_end->vid,
1982 range_start, br_vlan_flags(range_start, pvid),
1986 cb->args[1] = err ? idx : 0;
1988 nlmsg_end(skb, nlh);
1993 static const struct nla_policy br_vlan_db_dump_pol[BRIDGE_VLANDB_DUMP_MAX + 1] = {
1994 [BRIDGE_VLANDB_DUMP_FLAGS] = { .type = NLA_U32 },
1997 static int br_vlan_rtm_dump(struct sk_buff *skb, struct netlink_callback *cb)
1999 struct nlattr *dtb[BRIDGE_VLANDB_DUMP_MAX + 1];
2000 int idx = 0, err = 0, s_idx = cb->args[0];
2001 struct net *net = sock_net(skb->sk);
2002 struct br_vlan_msg *bvm;
2003 struct net_device *dev;
2006 err = nlmsg_parse(cb->nlh, sizeof(*bvm), dtb, BRIDGE_VLANDB_DUMP_MAX,
2007 br_vlan_db_dump_pol, cb->extack);
2011 bvm = nlmsg_data(cb->nlh);
2012 if (dtb[BRIDGE_VLANDB_DUMP_FLAGS])
2013 dump_flags = nla_get_u32(dtb[BRIDGE_VLANDB_DUMP_FLAGS]);
2017 dev = dev_get_by_index_rcu(net, bvm->ifindex);
2022 err = br_vlan_dump_dev(dev, skb, cb, dump_flags);
2023 if (err && err != -EMSGSIZE)
2026 for_each_netdev_rcu(net, dev) {
2030 err = br_vlan_dump_dev(dev, skb, cb, dump_flags);
2031 if (err == -EMSGSIZE)
2048 static const struct nla_policy br_vlan_db_policy[BRIDGE_VLANDB_ENTRY_MAX + 1] = {
2049 [BRIDGE_VLANDB_ENTRY_INFO] =
2050 NLA_POLICY_EXACT_LEN(sizeof(struct bridge_vlan_info)),
2051 [BRIDGE_VLANDB_ENTRY_RANGE] = { .type = NLA_U16 },
2052 [BRIDGE_VLANDB_ENTRY_STATE] = { .type = NLA_U8 },
2053 [BRIDGE_VLANDB_ENTRY_TUNNEL_INFO] = { .type = NLA_NESTED },
2056 static int br_vlan_rtm_process_one(struct net_device *dev,
2057 const struct nlattr *attr,
2058 int cmd, struct netlink_ext_ack *extack)
2060 struct bridge_vlan_info *vinfo, vrange_end, *vinfo_last = NULL;
2061 struct nlattr *tb[BRIDGE_VLANDB_ENTRY_MAX + 1];
2062 bool changed = false, skip_processing = false;
2063 struct net_bridge_vlan_group *vg;
2064 struct net_bridge_port *p = NULL;
2065 int err = 0, cmdmap = 0;
2066 struct net_bridge *br;
2068 if (netif_is_bridge_master(dev)) {
2069 br = netdev_priv(dev);
2070 vg = br_vlan_group(br);
2072 p = br_port_get_rtnl(dev);
2076 vg = nbp_vlan_group(p);
2082 err = nla_parse_nested(tb, BRIDGE_VLANDB_ENTRY_MAX, attr,
2083 br_vlan_db_policy, extack);
2087 if (!tb[BRIDGE_VLANDB_ENTRY_INFO]) {
2088 NL_SET_ERR_MSG_MOD(extack, "Missing vlan entry info");
2091 memset(&vrange_end, 0, sizeof(vrange_end));
2093 vinfo = nla_data(tb[BRIDGE_VLANDB_ENTRY_INFO]);
2094 if (vinfo->flags & (BRIDGE_VLAN_INFO_RANGE_BEGIN |
2095 BRIDGE_VLAN_INFO_RANGE_END)) {
2096 NL_SET_ERR_MSG_MOD(extack, "Old-style vlan ranges are not allowed when using RTM vlan calls");
2099 if (!br_vlan_valid_id(vinfo->vid, extack))
2102 if (tb[BRIDGE_VLANDB_ENTRY_RANGE]) {
2103 vrange_end.vid = nla_get_u16(tb[BRIDGE_VLANDB_ENTRY_RANGE]);
2104 /* validate user-provided flags without RANGE_BEGIN */
2105 vrange_end.flags = BRIDGE_VLAN_INFO_RANGE_END | vinfo->flags;
2106 vinfo->flags |= BRIDGE_VLAN_INFO_RANGE_BEGIN;
2108 /* vinfo_last is the range start, vinfo the range end */
2110 vinfo = &vrange_end;
2112 if (!br_vlan_valid_id(vinfo->vid, extack) ||
2113 !br_vlan_valid_range(vinfo, vinfo_last, extack))
2119 cmdmap = RTM_SETLINK;
2120 skip_processing = !!(vinfo->flags & BRIDGE_VLAN_INFO_ONLY_OPTS);
2123 cmdmap = RTM_DELLINK;
2127 if (!skip_processing) {
2128 struct bridge_vlan_info *tmp_last = vinfo_last;
2130 /* br_process_vlan_info may overwrite vinfo_last */
2131 err = br_process_vlan_info(br, p, cmdmap, vinfo, &tmp_last,
2134 /* notify first if anything changed */
2136 br_ifinfo_notify(cmdmap, br, p);
2142 /* deal with options */
2143 if (cmd == RTM_NEWVLAN) {
2144 struct net_bridge_vlan *range_start, *range_end;
2147 range_start = br_vlan_find(vg, vinfo_last->vid);
2148 range_end = br_vlan_find(vg, vinfo->vid);
2150 range_start = br_vlan_find(vg, vinfo->vid);
2151 range_end = range_start;
2154 err = br_vlan_process_options(br, p, range_start, range_end,
2161 static int br_vlan_rtm_process(struct sk_buff *skb, struct nlmsghdr *nlh,
2162 struct netlink_ext_ack *extack)
2164 struct net *net = sock_net(skb->sk);
2165 struct br_vlan_msg *bvm;
2166 struct net_device *dev;
2167 struct nlattr *attr;
2171 /* this should validate the header and check for remaining bytes */
2172 err = nlmsg_parse(nlh, sizeof(*bvm), NULL, BRIDGE_VLANDB_MAX, NULL,
2177 bvm = nlmsg_data(nlh);
2178 dev = __dev_get_by_index(net, bvm->ifindex);
2182 if (!netif_is_bridge_master(dev) && !netif_is_bridge_port(dev)) {
2183 NL_SET_ERR_MSG_MOD(extack, "The device is not a valid bridge or bridge port");
2187 nlmsg_for_each_attr(attr, nlh, sizeof(*bvm), rem) {
2188 if (nla_type(attr) != BRIDGE_VLANDB_ENTRY)
2192 err = br_vlan_rtm_process_one(dev, attr, nlh->nlmsg_type,
2198 NL_SET_ERR_MSG_MOD(extack, "No vlans found to process");
2205 void br_vlan_rtnl_init(void)
2207 rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_GETVLAN, NULL,
2208 br_vlan_rtm_dump, 0);
2209 rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_NEWVLAN,
2210 br_vlan_rtm_process, NULL, 0);
2211 rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_DELVLAN,
2212 br_vlan_rtm_process, NULL, 0);
2215 void br_vlan_rtnl_uninit(void)
2217 rtnl_unregister(PF_BRIDGE, RTM_GETVLAN);
2218 rtnl_unregister(PF_BRIDGE, RTM_NEWVLAN);
2219 rtnl_unregister(PF_BRIDGE, RTM_DELVLAN);