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 br_multicast_toggle_one_vlan(masterv, false);
194 br_multicast_ctx_deinit(&masterv->br_mcast_ctx);
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;
285 br_multicast_port_ctx_init(p, v, &v->port_mcast_ctx);
287 err = br_switchdev_port_vlan_add(dev, v->vid, flags, extack);
288 if (err && err != -EOPNOTSUPP)
290 br_multicast_ctx_init(br, v, &v->br_mcast_ctx);
291 v->priv_flags |= BR_VLFLAG_GLOBAL_MCAST_ENABLED;
294 /* Add the dev mac and count the vlan only if it's usable */
295 if (br_vlan_should_use(v)) {
296 err = br_fdb_insert(br, p, dev->dev_addr, v->vid);
298 br_err(br, "failed insert local address into bridge forwarding table\n");
304 /* set the state before publishing */
305 v->state = BR_STATE_FORWARDING;
307 err = rhashtable_lookup_insert_fast(&vg->vlan_hash, &v->vnode,
313 __vlan_add_flags(v, flags);
314 br_multicast_toggle_one_vlan(v, true);
317 nbp_vlan_set_vlan_dev_state(p, v->vid);
322 if (br_vlan_should_use(v)) {
323 br_fdb_find_delete_local(br, p, dev->dev_addr, v->vid);
329 __vlan_vid_del(dev, br, v);
331 if (v->stats && masterv->stats != v->stats)
332 free_percpu(v->stats);
335 br_vlan_put_master(masterv);
339 br_switchdev_port_vlan_del(dev, v->vid);
345 static int __vlan_del(struct net_bridge_vlan *v)
347 struct net_bridge_vlan *masterv = v;
348 struct net_bridge_vlan_group *vg;
349 struct net_bridge_port *p = NULL;
352 if (br_vlan_is_master(v)) {
353 vg = br_vlan_group(v->br);
356 vg = nbp_vlan_group(v->port);
360 __vlan_delete_pvid(vg, v->vid);
362 err = __vlan_vid_del(p->dev, p->br, v);
366 err = br_switchdev_port_vlan_del(v->br->dev, v->vid);
367 if (err && err != -EOPNOTSUPP)
372 if (br_vlan_should_use(v)) {
373 v->flags &= ~BRIDGE_VLAN_INFO_BRENTRY;
378 vlan_tunnel_info_del(vg, v);
379 rhashtable_remove_fast(&vg->vlan_hash, &v->vnode,
382 nbp_vlan_set_vlan_dev_state(p, v->vid);
383 br_multicast_toggle_one_vlan(v, false);
384 br_multicast_port_ctx_deinit(&v->port_mcast_ctx);
385 call_rcu(&v->rcu, nbp_vlan_rcu_free);
388 br_vlan_put_master(masterv);
393 static void __vlan_group_free(struct net_bridge_vlan_group *vg)
395 WARN_ON(!list_empty(&vg->vlan_list));
396 rhashtable_destroy(&vg->vlan_hash);
397 vlan_tunnel_deinit(vg);
401 static void __vlan_flush(const struct net_bridge *br,
402 const struct net_bridge_port *p,
403 struct net_bridge_vlan_group *vg)
405 struct net_bridge_vlan *vlan, *tmp;
406 u16 v_start = 0, v_end = 0;
408 __vlan_delete_pvid(vg, vg->pvid);
409 list_for_each_entry_safe(vlan, tmp, &vg->vlan_list, vlist) {
410 /* take care of disjoint ranges */
413 } else if (vlan->vid - v_end != 1) {
414 /* found range end, notify and start next one */
415 br_vlan_notify(br, p, v_start, v_end, RTM_DELVLAN);
423 /* notify about the last/whole vlan range */
425 br_vlan_notify(br, p, v_start, v_end, RTM_DELVLAN);
428 struct sk_buff *br_handle_vlan(struct net_bridge *br,
429 const struct net_bridge_port *p,
430 struct net_bridge_vlan_group *vg,
433 struct pcpu_sw_netstats *stats;
434 struct net_bridge_vlan *v;
437 /* If this packet was not filtered at input, let it pass */
438 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
441 /* At this point, we know that the frame was filtered and contains
442 * a valid vlan id. If the vlan id has untagged flag set,
443 * send untagged; otherwise, send tagged.
445 br_vlan_get_tag(skb, &vid);
446 v = br_vlan_find(vg, vid);
447 /* Vlan entry must be configured at this point. The
448 * only exception is the bridge is set in promisc mode and the
449 * packet is destined for the bridge device. In this case
450 * pass the packet as is.
452 if (!v || !br_vlan_should_use(v)) {
453 if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) {
460 if (br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) {
461 stats = this_cpu_ptr(v->stats);
462 u64_stats_update_begin(&stats->syncp);
463 stats->tx_bytes += skb->len;
465 u64_stats_update_end(&stats->syncp);
468 /* If the skb will be sent using forwarding offload, the assumption is
469 * that the switchdev will inject the packet into hardware together
470 * with the bridge VLAN, so that it can be forwarded according to that
471 * VLAN. The switchdev should deal with popping the VLAN header in
472 * hardware on each egress port as appropriate. So only strip the VLAN
473 * header if forwarding offload is not being used.
475 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED &&
476 !br_switchdev_frame_uses_tx_fwd_offload(skb))
477 __vlan_hwaccel_clear_tag(skb);
479 if (p && (p->flags & BR_VLAN_TUNNEL) &&
480 br_handle_egress_vlan_tunnel(skb, v)) {
488 /* Called under RCU */
489 static bool __allowed_ingress(const struct net_bridge *br,
490 struct net_bridge_vlan_group *vg,
491 struct sk_buff *skb, u16 *vid,
493 struct net_bridge_vlan **vlan)
495 struct pcpu_sw_netstats *stats;
496 struct net_bridge_vlan *v;
499 BR_INPUT_SKB_CB(skb)->vlan_filtered = true;
500 /* If vlan tx offload is disabled on bridge device and frame was
501 * sent from vlan device on the bridge device, it does not have
502 * HW accelerated vlan tag.
504 if (unlikely(!skb_vlan_tag_present(skb) &&
505 skb->protocol == br->vlan_proto)) {
506 skb = skb_vlan_untag(skb);
511 if (!br_vlan_get_tag(skb, vid)) {
513 if (skb->vlan_proto != br->vlan_proto) {
514 /* Protocol-mismatch, empty out vlan_tci for new tag */
515 skb_push(skb, ETH_HLEN);
516 skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
517 skb_vlan_tag_get(skb));
521 skb_pull(skb, ETH_HLEN);
522 skb_reset_mac_len(skb);
534 u16 pvid = br_get_pvid(vg);
536 /* Frame had a tag with VID 0 or did not have a tag.
537 * See if pvid is set on this port. That tells us which
538 * vlan untagged or priority-tagged traffic belongs to.
543 /* PVID is set on this port. Any untagged or priority-tagged
544 * ingress frame is considered to belong to this vlan.
548 /* Untagged Frame. */
549 __vlan_hwaccel_put_tag(skb, br->vlan_proto, pvid);
551 /* Priority-tagged Frame.
552 * At this point, we know that skb->vlan_tci VID
554 * We update only VID field and preserve PCP field.
556 skb->vlan_tci |= pvid;
558 /* if snooping and stats are disabled we can avoid the lookup */
559 if (!br_opt_get(br, BROPT_MCAST_VLAN_SNOOPING_ENABLED) &&
560 !br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) {
561 if (*state == BR_STATE_FORWARDING) {
562 *state = br_vlan_get_pvid_state(vg);
563 return br_vlan_state_allowed(*state, true);
569 v = br_vlan_find(vg, *vid);
570 if (!v || !br_vlan_should_use(v))
573 if (*state == BR_STATE_FORWARDING) {
574 *state = br_vlan_get_state(v);
575 if (!br_vlan_state_allowed(*state, true))
579 if (br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) {
580 stats = this_cpu_ptr(v->stats);
581 u64_stats_update_begin(&stats->syncp);
582 stats->rx_bytes += skb->len;
584 u64_stats_update_end(&stats->syncp);
596 bool br_allowed_ingress(const struct net_bridge *br,
597 struct net_bridge_vlan_group *vg, struct sk_buff *skb,
599 struct net_bridge_vlan **vlan)
601 /* If VLAN filtering is disabled on the bridge, all packets are
605 if (!br_opt_get(br, BROPT_VLAN_ENABLED)) {
606 BR_INPUT_SKB_CB(skb)->vlan_filtered = false;
610 return __allowed_ingress(br, vg, skb, vid, state, vlan);
613 /* Called under RCU. */
614 bool br_allowed_egress(struct net_bridge_vlan_group *vg,
615 const struct sk_buff *skb)
617 const struct net_bridge_vlan *v;
620 /* If this packet was not filtered at input, let it pass */
621 if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
624 br_vlan_get_tag(skb, &vid);
625 v = br_vlan_find(vg, vid);
626 if (v && br_vlan_should_use(v) &&
627 br_vlan_state_allowed(br_vlan_get_state(v), false))
633 /* Called under RCU */
634 bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid)
636 struct net_bridge_vlan_group *vg;
637 struct net_bridge *br = p->br;
638 struct net_bridge_vlan *v;
640 /* If filtering was disabled at input, let it pass. */
641 if (!br_opt_get(br, BROPT_VLAN_ENABLED))
644 vg = nbp_vlan_group_rcu(p);
645 if (!vg || !vg->num_vlans)
648 if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto)
652 *vid = br_get_pvid(vg);
654 !br_vlan_state_allowed(br_vlan_get_pvid_state(vg), true))
660 v = br_vlan_find(vg, *vid);
661 if (v && br_vlan_state_allowed(br_vlan_get_state(v), true))
667 static int br_vlan_add_existing(struct net_bridge *br,
668 struct net_bridge_vlan_group *vg,
669 struct net_bridge_vlan *vlan,
670 u16 flags, bool *changed,
671 struct netlink_ext_ack *extack)
675 err = br_switchdev_port_vlan_add(br->dev, vlan->vid, flags, extack);
676 if (err && err != -EOPNOTSUPP)
679 if (!br_vlan_is_brentry(vlan)) {
680 /* Trying to change flags of non-existent bridge vlan */
681 if (!(flags & BRIDGE_VLAN_INFO_BRENTRY)) {
685 /* It was only kept for port vlans, now make it real */
686 err = br_fdb_insert(br, NULL, br->dev->dev_addr,
689 br_err(br, "failed to insert local address into bridge forwarding table\n");
693 refcount_inc(&vlan->refcnt);
694 vlan->flags |= BRIDGE_VLAN_INFO_BRENTRY;
697 br_multicast_toggle_one_vlan(vlan, true);
700 if (__vlan_add_flags(vlan, flags))
707 br_switchdev_port_vlan_del(br->dev, vlan->vid);
711 /* Must be protected by RTNL.
712 * Must be called with vid in range from 1 to 4094 inclusive.
713 * changed must be true only if the vlan was created or updated
715 int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags, bool *changed,
716 struct netlink_ext_ack *extack)
718 struct net_bridge_vlan_group *vg;
719 struct net_bridge_vlan *vlan;
725 vg = br_vlan_group(br);
726 vlan = br_vlan_find(vg, vid);
728 return br_vlan_add_existing(br, vg, vlan, flags, changed,
731 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
735 vlan->stats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
741 vlan->flags = flags | BRIDGE_VLAN_INFO_MASTER;
742 vlan->flags &= ~BRIDGE_VLAN_INFO_PVID;
744 if (flags & BRIDGE_VLAN_INFO_BRENTRY)
745 refcount_set(&vlan->refcnt, 1);
746 ret = __vlan_add(vlan, flags, extack);
748 free_percpu(vlan->stats);
757 /* Must be protected by RTNL.
758 * Must be called with vid in range from 1 to 4094 inclusive.
760 int br_vlan_delete(struct net_bridge *br, u16 vid)
762 struct net_bridge_vlan_group *vg;
763 struct net_bridge_vlan *v;
767 vg = br_vlan_group(br);
768 v = br_vlan_find(vg, vid);
769 if (!v || !br_vlan_is_brentry(v))
772 br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
773 br_fdb_delete_by_port(br, NULL, vid, 0);
775 vlan_tunnel_info_del(vg, v);
777 return __vlan_del(v);
780 void br_vlan_flush(struct net_bridge *br)
782 struct net_bridge_vlan_group *vg;
786 vg = br_vlan_group(br);
787 __vlan_flush(br, NULL, vg);
788 RCU_INIT_POINTER(br->vlgrp, NULL);
790 __vlan_group_free(vg);
793 struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid)
798 return br_vlan_lookup(&vg->vlan_hash, vid);
801 /* Must be protected by RTNL. */
802 static void recalculate_group_addr(struct net_bridge *br)
804 if (br_opt_get(br, BROPT_GROUP_ADDR_SET))
807 spin_lock_bh(&br->lock);
808 if (!br_opt_get(br, BROPT_VLAN_ENABLED) ||
809 br->vlan_proto == htons(ETH_P_8021Q)) {
810 /* Bridge Group Address */
811 br->group_addr[5] = 0x00;
812 } else { /* vlan_enabled && ETH_P_8021AD */
813 /* Provider Bridge Group Address */
814 br->group_addr[5] = 0x08;
816 spin_unlock_bh(&br->lock);
819 /* Must be protected by RTNL. */
820 void br_recalculate_fwd_mask(struct net_bridge *br)
822 if (!br_opt_get(br, BROPT_VLAN_ENABLED) ||
823 br->vlan_proto == htons(ETH_P_8021Q))
824 br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT;
825 else /* vlan_enabled && ETH_P_8021AD */
826 br->group_fwd_mask_required = BR_GROUPFWD_8021AD &
827 ~(1u << br->group_addr[5]);
830 int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val,
831 struct netlink_ext_ack *extack)
833 struct switchdev_attr attr = {
835 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
836 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
837 .u.vlan_filtering = val,
841 if (br_opt_get(br, BROPT_VLAN_ENABLED) == !!val)
844 br_opt_toggle(br, BROPT_VLAN_ENABLED, !!val);
846 err = switchdev_port_attr_set(br->dev, &attr, extack);
847 if (err && err != -EOPNOTSUPP) {
848 br_opt_toggle(br, BROPT_VLAN_ENABLED, !val);
852 br_manage_promisc(br);
853 recalculate_group_addr(br);
854 br_recalculate_fwd_mask(br);
855 if (!val && br_opt_get(br, BROPT_MCAST_VLAN_SNOOPING_ENABLED)) {
856 br_info(br, "vlan filtering disabled, automatically disabling multicast vlan snooping\n");
857 br_multicast_toggle_vlan_snooping(br, false, NULL);
863 bool br_vlan_enabled(const struct net_device *dev)
865 struct net_bridge *br = netdev_priv(dev);
867 return br_opt_get(br, BROPT_VLAN_ENABLED);
869 EXPORT_SYMBOL_GPL(br_vlan_enabled);
871 int br_vlan_get_proto(const struct net_device *dev, u16 *p_proto)
873 struct net_bridge *br = netdev_priv(dev);
875 *p_proto = ntohs(br->vlan_proto);
879 EXPORT_SYMBOL_GPL(br_vlan_get_proto);
881 int __br_vlan_set_proto(struct net_bridge *br, __be16 proto,
882 struct netlink_ext_ack *extack)
884 struct switchdev_attr attr = {
886 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_PROTOCOL,
887 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
888 .u.vlan_protocol = ntohs(proto),
891 struct net_bridge_port *p;
892 struct net_bridge_vlan *vlan;
893 struct net_bridge_vlan_group *vg;
894 __be16 oldproto = br->vlan_proto;
896 if (br->vlan_proto == proto)
899 err = switchdev_port_attr_set(br->dev, &attr, extack);
900 if (err && err != -EOPNOTSUPP)
903 /* Add VLANs for the new proto to the device filter. */
904 list_for_each_entry(p, &br->port_list, list) {
905 vg = nbp_vlan_group(p);
906 list_for_each_entry(vlan, &vg->vlan_list, vlist) {
907 err = vlan_vid_add(p->dev, proto, vlan->vid);
913 br->vlan_proto = proto;
915 recalculate_group_addr(br);
916 br_recalculate_fwd_mask(br);
918 /* Delete VLANs for the old proto from the device filter. */
919 list_for_each_entry(p, &br->port_list, list) {
920 vg = nbp_vlan_group(p);
921 list_for_each_entry(vlan, &vg->vlan_list, vlist)
922 vlan_vid_del(p->dev, oldproto, vlan->vid);
928 attr.u.vlan_protocol = ntohs(oldproto);
929 switchdev_port_attr_set(br->dev, &attr, NULL);
931 list_for_each_entry_continue_reverse(vlan, &vg->vlan_list, vlist)
932 vlan_vid_del(p->dev, proto, vlan->vid);
934 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
935 vg = nbp_vlan_group(p);
936 list_for_each_entry(vlan, &vg->vlan_list, vlist)
937 vlan_vid_del(p->dev, proto, vlan->vid);
943 int br_vlan_set_proto(struct net_bridge *br, unsigned long val,
944 struct netlink_ext_ack *extack)
946 if (!eth_type_vlan(htons(val)))
947 return -EPROTONOSUPPORT;
949 return __br_vlan_set_proto(br, htons(val), extack);
952 int br_vlan_set_stats(struct net_bridge *br, unsigned long val)
957 br_opt_toggle(br, BROPT_VLAN_STATS_ENABLED, !!val);
966 int br_vlan_set_stats_per_port(struct net_bridge *br, unsigned long val)
968 struct net_bridge_port *p;
970 /* allow to change the option if there are no port vlans configured */
971 list_for_each_entry(p, &br->port_list, list) {
972 struct net_bridge_vlan_group *vg = nbp_vlan_group(p);
981 br_opt_toggle(br, BROPT_VLAN_STATS_PER_PORT, !!val);
990 static bool vlan_default_pvid(struct net_bridge_vlan_group *vg, u16 vid)
992 struct net_bridge_vlan *v;
997 v = br_vlan_lookup(&vg->vlan_hash, vid);
998 if (v && br_vlan_should_use(v) &&
999 (v->flags & BRIDGE_VLAN_INFO_UNTAGGED))
1005 static void br_vlan_disable_default_pvid(struct net_bridge *br)
1007 struct net_bridge_port *p;
1008 u16 pvid = br->default_pvid;
1010 /* Disable default_pvid on all ports where it is still
1013 if (vlan_default_pvid(br_vlan_group(br), pvid)) {
1014 if (!br_vlan_delete(br, pvid))
1015 br_vlan_notify(br, NULL, pvid, 0, RTM_DELVLAN);
1018 list_for_each_entry(p, &br->port_list, list) {
1019 if (vlan_default_pvid(nbp_vlan_group(p), pvid) &&
1020 !nbp_vlan_delete(p, pvid))
1021 br_vlan_notify(br, p, pvid, 0, RTM_DELVLAN);
1024 br->default_pvid = 0;
1027 int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid,
1028 struct netlink_ext_ack *extack)
1030 const struct net_bridge_vlan *pvent;
1031 struct net_bridge_vlan_group *vg;
1032 struct net_bridge_port *p;
1033 unsigned long *changed;
1039 br_vlan_disable_default_pvid(br);
1043 changed = bitmap_zalloc(BR_MAX_PORTS, GFP_KERNEL);
1047 old_pvid = br->default_pvid;
1049 /* Update default_pvid config only if we do not conflict with
1050 * user configuration.
1052 vg = br_vlan_group(br);
1053 pvent = br_vlan_find(vg, pvid);
1054 if ((!old_pvid || vlan_default_pvid(vg, old_pvid)) &&
1055 (!pvent || !br_vlan_should_use(pvent))) {
1056 err = br_vlan_add(br, pvid,
1057 BRIDGE_VLAN_INFO_PVID |
1058 BRIDGE_VLAN_INFO_UNTAGGED |
1059 BRIDGE_VLAN_INFO_BRENTRY,
1064 if (br_vlan_delete(br, old_pvid))
1065 br_vlan_notify(br, NULL, old_pvid, 0, RTM_DELVLAN);
1066 br_vlan_notify(br, NULL, pvid, 0, RTM_NEWVLAN);
1067 set_bit(0, changed);
1070 list_for_each_entry(p, &br->port_list, list) {
1071 /* Update default_pvid config only if we do not conflict with
1072 * user configuration.
1074 vg = nbp_vlan_group(p);
1076 !vlan_default_pvid(vg, old_pvid)) ||
1077 br_vlan_find(vg, pvid))
1080 err = nbp_vlan_add(p, pvid,
1081 BRIDGE_VLAN_INFO_PVID |
1082 BRIDGE_VLAN_INFO_UNTAGGED,
1086 if (nbp_vlan_delete(p, old_pvid))
1087 br_vlan_notify(br, p, old_pvid, 0, RTM_DELVLAN);
1088 br_vlan_notify(p->br, p, pvid, 0, RTM_NEWVLAN);
1089 set_bit(p->port_no, changed);
1092 br->default_pvid = pvid;
1095 bitmap_free(changed);
1099 list_for_each_entry_continue_reverse(p, &br->port_list, list) {
1100 if (!test_bit(p->port_no, changed))
1104 nbp_vlan_add(p, old_pvid,
1105 BRIDGE_VLAN_INFO_PVID |
1106 BRIDGE_VLAN_INFO_UNTAGGED,
1108 br_vlan_notify(p->br, p, old_pvid, 0, RTM_NEWVLAN);
1110 nbp_vlan_delete(p, pvid);
1111 br_vlan_notify(br, p, pvid, 0, RTM_DELVLAN);
1114 if (test_bit(0, changed)) {
1116 br_vlan_add(br, old_pvid,
1117 BRIDGE_VLAN_INFO_PVID |
1118 BRIDGE_VLAN_INFO_UNTAGGED |
1119 BRIDGE_VLAN_INFO_BRENTRY,
1121 br_vlan_notify(br, NULL, old_pvid, 0, RTM_NEWVLAN);
1123 br_vlan_delete(br, pvid);
1124 br_vlan_notify(br, NULL, pvid, 0, RTM_DELVLAN);
1129 int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val,
1130 struct netlink_ext_ack *extack)
1135 if (val >= VLAN_VID_MASK)
1138 if (pvid == br->default_pvid)
1141 /* Only allow default pvid change when filtering is disabled */
1142 if (br_opt_get(br, BROPT_VLAN_ENABLED)) {
1143 pr_info_once("Please disable vlan filtering to change default_pvid\n");
1147 err = __br_vlan_set_default_pvid(br, pvid, extack);
1152 int br_vlan_init(struct net_bridge *br)
1154 struct net_bridge_vlan_group *vg;
1157 vg = kzalloc(sizeof(*vg), GFP_KERNEL);
1160 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
1163 ret = vlan_tunnel_init(vg);
1165 goto err_tunnel_init;
1166 INIT_LIST_HEAD(&vg->vlan_list);
1167 br->vlan_proto = htons(ETH_P_8021Q);
1168 br->default_pvid = 1;
1169 rcu_assign_pointer(br->vlgrp, vg);
1175 rhashtable_destroy(&vg->vlan_hash);
1182 int nbp_vlan_init(struct net_bridge_port *p, struct netlink_ext_ack *extack)
1184 struct switchdev_attr attr = {
1185 .orig_dev = p->br->dev,
1186 .id = SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
1187 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP,
1188 .u.vlan_filtering = br_opt_get(p->br, BROPT_VLAN_ENABLED),
1190 struct net_bridge_vlan_group *vg;
1193 vg = kzalloc(sizeof(struct net_bridge_vlan_group), GFP_KERNEL);
1197 ret = switchdev_port_attr_set(p->dev, &attr, extack);
1198 if (ret && ret != -EOPNOTSUPP)
1199 goto err_vlan_enabled;
1201 ret = rhashtable_init(&vg->vlan_hash, &br_vlan_rht_params);
1204 ret = vlan_tunnel_init(vg);
1206 goto err_tunnel_init;
1207 INIT_LIST_HEAD(&vg->vlan_list);
1208 rcu_assign_pointer(p->vlgrp, vg);
1209 if (p->br->default_pvid) {
1212 ret = nbp_vlan_add(p, p->br->default_pvid,
1213 BRIDGE_VLAN_INFO_PVID |
1214 BRIDGE_VLAN_INFO_UNTAGGED,
1218 br_vlan_notify(p->br, p, p->br->default_pvid, 0, RTM_NEWVLAN);
1224 RCU_INIT_POINTER(p->vlgrp, NULL);
1226 vlan_tunnel_deinit(vg);
1228 rhashtable_destroy(&vg->vlan_hash);
1236 /* Must be protected by RTNL.
1237 * Must be called with vid in range from 1 to 4094 inclusive.
1238 * changed must be true only if the vlan was created or updated
1240 int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags,
1241 bool *changed, struct netlink_ext_ack *extack)
1243 struct net_bridge_vlan *vlan;
1249 vlan = br_vlan_find(nbp_vlan_group(port), vid);
1251 /* Pass the flags to the hardware bridge */
1252 ret = br_switchdev_port_vlan_add(port->dev, vid, flags, extack);
1253 if (ret && ret != -EOPNOTSUPP)
1255 *changed = __vlan_add_flags(vlan, flags);
1260 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
1266 ret = __vlan_add(vlan, flags, extack);
1275 /* Must be protected by RTNL.
1276 * Must be called with vid in range from 1 to 4094 inclusive.
1278 int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
1280 struct net_bridge_vlan *v;
1284 v = br_vlan_find(nbp_vlan_group(port), vid);
1287 br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
1288 br_fdb_delete_by_port(port->br, port, vid, 0);
1290 return __vlan_del(v);
1293 void nbp_vlan_flush(struct net_bridge_port *port)
1295 struct net_bridge_vlan_group *vg;
1299 vg = nbp_vlan_group(port);
1300 __vlan_flush(port->br, port, vg);
1301 RCU_INIT_POINTER(port->vlgrp, NULL);
1303 __vlan_group_free(vg);
1306 void br_vlan_get_stats(const struct net_bridge_vlan *v,
1307 struct pcpu_sw_netstats *stats)
1311 memset(stats, 0, sizeof(*stats));
1312 for_each_possible_cpu(i) {
1313 u64 rxpackets, rxbytes, txpackets, txbytes;
1314 struct pcpu_sw_netstats *cpu_stats;
1317 cpu_stats = per_cpu_ptr(v->stats, i);
1319 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
1320 rxpackets = cpu_stats->rx_packets;
1321 rxbytes = cpu_stats->rx_bytes;
1322 txbytes = cpu_stats->tx_bytes;
1323 txpackets = cpu_stats->tx_packets;
1324 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
1326 stats->rx_packets += rxpackets;
1327 stats->rx_bytes += rxbytes;
1328 stats->tx_bytes += txbytes;
1329 stats->tx_packets += txpackets;
1333 int br_vlan_get_pvid(const struct net_device *dev, u16 *p_pvid)
1335 struct net_bridge_vlan_group *vg;
1336 struct net_bridge_port *p;
1339 p = br_port_get_check_rtnl(dev);
1341 vg = nbp_vlan_group(p);
1342 else if (netif_is_bridge_master(dev))
1343 vg = br_vlan_group(netdev_priv(dev));
1347 *p_pvid = br_get_pvid(vg);
1350 EXPORT_SYMBOL_GPL(br_vlan_get_pvid);
1352 int br_vlan_get_pvid_rcu(const struct net_device *dev, u16 *p_pvid)
1354 struct net_bridge_vlan_group *vg;
1355 struct net_bridge_port *p;
1357 p = br_port_get_check_rcu(dev);
1359 vg = nbp_vlan_group_rcu(p);
1360 else if (netif_is_bridge_master(dev))
1361 vg = br_vlan_group_rcu(netdev_priv(dev));
1365 *p_pvid = br_get_pvid(vg);
1368 EXPORT_SYMBOL_GPL(br_vlan_get_pvid_rcu);
1370 void br_vlan_fill_forward_path_pvid(struct net_bridge *br,
1371 struct net_device_path_ctx *ctx,
1372 struct net_device_path *path)
1374 struct net_bridge_vlan_group *vg;
1375 int idx = ctx->num_vlans - 1;
1378 path->bridge.vlan_mode = DEV_PATH_BR_VLAN_KEEP;
1380 if (!br_opt_get(br, BROPT_VLAN_ENABLED))
1383 vg = br_vlan_group(br);
1386 ctx->vlan[idx].proto == br->vlan_proto) {
1387 vid = ctx->vlan[idx].id;
1389 path->bridge.vlan_mode = DEV_PATH_BR_VLAN_TAG;
1390 vid = br_get_pvid(vg);
1393 path->bridge.vlan_id = vid;
1394 path->bridge.vlan_proto = br->vlan_proto;
1397 int br_vlan_fill_forward_path_mode(struct net_bridge *br,
1398 struct net_bridge_port *dst,
1399 struct net_device_path *path)
1401 struct net_bridge_vlan_group *vg;
1402 struct net_bridge_vlan *v;
1404 if (!br_opt_get(br, BROPT_VLAN_ENABLED))
1407 vg = nbp_vlan_group_rcu(dst);
1408 v = br_vlan_find(vg, path->bridge.vlan_id);
1409 if (!v || !br_vlan_should_use(v))
1412 if (!(v->flags & BRIDGE_VLAN_INFO_UNTAGGED))
1415 if (path->bridge.vlan_mode == DEV_PATH_BR_VLAN_TAG)
1416 path->bridge.vlan_mode = DEV_PATH_BR_VLAN_KEEP;
1417 else if (v->priv_flags & BR_VLFLAG_ADDED_BY_SWITCHDEV)
1418 path->bridge.vlan_mode = DEV_PATH_BR_VLAN_UNTAG_HW;
1420 path->bridge.vlan_mode = DEV_PATH_BR_VLAN_UNTAG;
1425 int br_vlan_get_info(const struct net_device *dev, u16 vid,
1426 struct bridge_vlan_info *p_vinfo)
1428 struct net_bridge_vlan_group *vg;
1429 struct net_bridge_vlan *v;
1430 struct net_bridge_port *p;
1433 p = br_port_get_check_rtnl(dev);
1435 vg = nbp_vlan_group(p);
1436 else if (netif_is_bridge_master(dev))
1437 vg = br_vlan_group(netdev_priv(dev));
1441 v = br_vlan_find(vg, vid);
1446 p_vinfo->flags = v->flags;
1447 if (vid == br_get_pvid(vg))
1448 p_vinfo->flags |= BRIDGE_VLAN_INFO_PVID;
1451 EXPORT_SYMBOL_GPL(br_vlan_get_info);
1453 int br_vlan_get_info_rcu(const struct net_device *dev, u16 vid,
1454 struct bridge_vlan_info *p_vinfo)
1456 struct net_bridge_vlan_group *vg;
1457 struct net_bridge_vlan *v;
1458 struct net_bridge_port *p;
1460 p = br_port_get_check_rcu(dev);
1462 vg = nbp_vlan_group_rcu(p);
1463 else if (netif_is_bridge_master(dev))
1464 vg = br_vlan_group_rcu(netdev_priv(dev));
1468 v = br_vlan_find(vg, vid);
1473 p_vinfo->flags = v->flags;
1474 if (vid == br_get_pvid(vg))
1475 p_vinfo->flags |= BRIDGE_VLAN_INFO_PVID;
1478 EXPORT_SYMBOL_GPL(br_vlan_get_info_rcu);
1480 static int br_vlan_is_bind_vlan_dev(const struct net_device *dev)
1482 return is_vlan_dev(dev) &&
1483 !!(vlan_dev_priv(dev)->flags & VLAN_FLAG_BRIDGE_BINDING);
1486 static int br_vlan_is_bind_vlan_dev_fn(struct net_device *dev,
1487 __always_unused struct netdev_nested_priv *priv)
1489 return br_vlan_is_bind_vlan_dev(dev);
1492 static bool br_vlan_has_upper_bind_vlan_dev(struct net_device *dev)
1497 found = netdev_walk_all_upper_dev_rcu(dev, br_vlan_is_bind_vlan_dev_fn,
1504 struct br_vlan_bind_walk_data {
1506 struct net_device *result;
1509 static int br_vlan_match_bind_vlan_dev_fn(struct net_device *dev,
1510 struct netdev_nested_priv *priv)
1512 struct br_vlan_bind_walk_data *data = priv->data;
1515 if (br_vlan_is_bind_vlan_dev(dev) &&
1516 vlan_dev_priv(dev)->vlan_id == data->vid) {
1524 static struct net_device *
1525 br_vlan_get_upper_bind_vlan_dev(struct net_device *dev, u16 vid)
1527 struct br_vlan_bind_walk_data data = {
1530 struct netdev_nested_priv priv = {
1531 .data = (void *)&data,
1535 netdev_walk_all_upper_dev_rcu(dev, br_vlan_match_bind_vlan_dev_fn,
1542 static bool br_vlan_is_dev_up(const struct net_device *dev)
1544 return !!(dev->flags & IFF_UP) && netif_oper_up(dev);
1547 static void br_vlan_set_vlan_dev_state(const struct net_bridge *br,
1548 struct net_device *vlan_dev)
1550 u16 vid = vlan_dev_priv(vlan_dev)->vlan_id;
1551 struct net_bridge_vlan_group *vg;
1552 struct net_bridge_port *p;
1553 bool has_carrier = false;
1555 if (!netif_carrier_ok(br->dev)) {
1556 netif_carrier_off(vlan_dev);
1560 list_for_each_entry(p, &br->port_list, list) {
1561 vg = nbp_vlan_group(p);
1562 if (br_vlan_find(vg, vid) && br_vlan_is_dev_up(p->dev)) {
1569 netif_carrier_on(vlan_dev);
1571 netif_carrier_off(vlan_dev);
1574 static void br_vlan_set_all_vlan_dev_state(struct net_bridge_port *p)
1576 struct net_bridge_vlan_group *vg = nbp_vlan_group(p);
1577 struct net_bridge_vlan *vlan;
1578 struct net_device *vlan_dev;
1580 list_for_each_entry(vlan, &vg->vlan_list, vlist) {
1581 vlan_dev = br_vlan_get_upper_bind_vlan_dev(p->br->dev,
1584 if (br_vlan_is_dev_up(p->dev)) {
1585 if (netif_carrier_ok(p->br->dev))
1586 netif_carrier_on(vlan_dev);
1588 br_vlan_set_vlan_dev_state(p->br, vlan_dev);
1594 static void br_vlan_upper_change(struct net_device *dev,
1595 struct net_device *upper_dev,
1598 struct net_bridge *br = netdev_priv(dev);
1600 if (!br_vlan_is_bind_vlan_dev(upper_dev))
1604 br_vlan_set_vlan_dev_state(br, upper_dev);
1605 br_opt_toggle(br, BROPT_VLAN_BRIDGE_BINDING, true);
1607 br_opt_toggle(br, BROPT_VLAN_BRIDGE_BINDING,
1608 br_vlan_has_upper_bind_vlan_dev(dev));
1612 struct br_vlan_link_state_walk_data {
1613 struct net_bridge *br;
1616 static int br_vlan_link_state_change_fn(struct net_device *vlan_dev,
1617 struct netdev_nested_priv *priv)
1619 struct br_vlan_link_state_walk_data *data = priv->data;
1621 if (br_vlan_is_bind_vlan_dev(vlan_dev))
1622 br_vlan_set_vlan_dev_state(data->br, vlan_dev);
1627 static void br_vlan_link_state_change(struct net_device *dev,
1628 struct net_bridge *br)
1630 struct br_vlan_link_state_walk_data data = {
1633 struct netdev_nested_priv priv = {
1634 .data = (void *)&data,
1638 netdev_walk_all_upper_dev_rcu(dev, br_vlan_link_state_change_fn,
1643 /* Must be protected by RTNL. */
1644 static void nbp_vlan_set_vlan_dev_state(struct net_bridge_port *p, u16 vid)
1646 struct net_device *vlan_dev;
1648 if (!br_opt_get(p->br, BROPT_VLAN_BRIDGE_BINDING))
1651 vlan_dev = br_vlan_get_upper_bind_vlan_dev(p->br->dev, vid);
1653 br_vlan_set_vlan_dev_state(p->br, vlan_dev);
1656 /* Must be protected by RTNL. */
1657 int br_vlan_bridge_event(struct net_device *dev, unsigned long event, void *ptr)
1659 struct netdev_notifier_changeupper_info *info;
1660 struct net_bridge *br = netdev_priv(dev);
1661 int vlcmd = 0, ret = 0;
1662 bool changed = false;
1665 case NETDEV_REGISTER:
1666 ret = br_vlan_add(br, br->default_pvid,
1667 BRIDGE_VLAN_INFO_PVID |
1668 BRIDGE_VLAN_INFO_UNTAGGED |
1669 BRIDGE_VLAN_INFO_BRENTRY, &changed, NULL);
1670 vlcmd = RTM_NEWVLAN;
1672 case NETDEV_UNREGISTER:
1673 changed = !br_vlan_delete(br, br->default_pvid);
1674 vlcmd = RTM_DELVLAN;
1676 case NETDEV_CHANGEUPPER:
1678 br_vlan_upper_change(dev, info->upper_dev, info->linking);
1683 if (!br_opt_get(br, BROPT_VLAN_BRIDGE_BINDING))
1685 br_vlan_link_state_change(dev, br);
1689 br_vlan_notify(br, NULL, br->default_pvid, 0, vlcmd);
1694 /* Must be protected by RTNL. */
1695 void br_vlan_port_event(struct net_bridge_port *p, unsigned long event)
1697 if (!br_opt_get(p->br, BROPT_VLAN_BRIDGE_BINDING))
1704 br_vlan_set_all_vlan_dev_state(p);
1709 static bool br_vlan_stats_fill(struct sk_buff *skb,
1710 const struct net_bridge_vlan *v)
1712 struct pcpu_sw_netstats stats;
1713 struct nlattr *nest;
1715 nest = nla_nest_start(skb, BRIDGE_VLANDB_ENTRY_STATS);
1719 br_vlan_get_stats(v, &stats);
1720 if (nla_put_u64_64bit(skb, BRIDGE_VLANDB_STATS_RX_BYTES, stats.rx_bytes,
1721 BRIDGE_VLANDB_STATS_PAD) ||
1722 nla_put_u64_64bit(skb, BRIDGE_VLANDB_STATS_RX_PACKETS,
1723 stats.rx_packets, BRIDGE_VLANDB_STATS_PAD) ||
1724 nla_put_u64_64bit(skb, BRIDGE_VLANDB_STATS_TX_BYTES, stats.tx_bytes,
1725 BRIDGE_VLANDB_STATS_PAD) ||
1726 nla_put_u64_64bit(skb, BRIDGE_VLANDB_STATS_TX_PACKETS,
1727 stats.tx_packets, BRIDGE_VLANDB_STATS_PAD))
1730 nla_nest_end(skb, nest);
1735 nla_nest_cancel(skb, nest);
1739 /* v_opts is used to dump the options which must be equal in the whole range */
1740 static bool br_vlan_fill_vids(struct sk_buff *skb, u16 vid, u16 vid_range,
1741 const struct net_bridge_vlan *v_opts,
1745 struct bridge_vlan_info info;
1746 struct nlattr *nest;
1748 nest = nla_nest_start(skb, BRIDGE_VLANDB_ENTRY);
1752 memset(&info, 0, sizeof(info));
1754 if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
1755 info.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1756 if (flags & BRIDGE_VLAN_INFO_PVID)
1757 info.flags |= BRIDGE_VLAN_INFO_PVID;
1759 if (nla_put(skb, BRIDGE_VLANDB_ENTRY_INFO, sizeof(info), &info))
1762 if (vid_range && vid < vid_range &&
1763 !(flags & BRIDGE_VLAN_INFO_PVID) &&
1764 nla_put_u16(skb, BRIDGE_VLANDB_ENTRY_RANGE, vid_range))
1768 if (!br_vlan_opts_fill(skb, v_opts))
1771 if (dump_stats && !br_vlan_stats_fill(skb, v_opts))
1775 nla_nest_end(skb, nest);
1780 nla_nest_cancel(skb, nest);
1784 static size_t rtnl_vlan_nlmsg_size(void)
1786 return NLMSG_ALIGN(sizeof(struct br_vlan_msg))
1787 + nla_total_size(0) /* BRIDGE_VLANDB_ENTRY */
1788 + nla_total_size(sizeof(u16)) /* BRIDGE_VLANDB_ENTRY_RANGE */
1789 + nla_total_size(sizeof(struct bridge_vlan_info)) /* BRIDGE_VLANDB_ENTRY_INFO */
1790 + br_vlan_opts_nl_size(); /* bridge vlan options */
1793 void br_vlan_notify(const struct net_bridge *br,
1794 const struct net_bridge_port *p,
1795 u16 vid, u16 vid_range,
1798 struct net_bridge_vlan_group *vg;
1799 struct net_bridge_vlan *v = NULL;
1800 struct br_vlan_msg *bvm;
1801 struct nlmsghdr *nlh;
1802 struct sk_buff *skb;
1808 /* right now notifications are done only with rtnl held */
1812 ifindex = p->dev->ifindex;
1813 vg = nbp_vlan_group(p);
1814 net = dev_net(p->dev);
1816 ifindex = br->dev->ifindex;
1817 vg = br_vlan_group(br);
1818 net = dev_net(br->dev);
1821 skb = nlmsg_new(rtnl_vlan_nlmsg_size(), GFP_KERNEL);
1826 nlh = nlmsg_put(skb, 0, 0, cmd, sizeof(*bvm), 0);
1829 bvm = nlmsg_data(nlh);
1830 memset(bvm, 0, sizeof(*bvm));
1831 bvm->family = AF_BRIDGE;
1832 bvm->ifindex = ifindex;
1836 /* need to find the vlan due to flags/options */
1837 v = br_vlan_find(vg, vid);
1838 if (!v || !br_vlan_should_use(v))
1842 if (br_get_pvid(vg) == v->vid)
1843 flags |= BRIDGE_VLAN_INFO_PVID;
1851 if (!br_vlan_fill_vids(skb, vid, vid_range, v, flags, false))
1854 nlmsg_end(skb, nlh);
1855 rtnl_notify(skb, net, 0, RTNLGRP_BRVLAN, NULL, GFP_KERNEL);
1859 rtnl_set_sk_err(net, RTNLGRP_BRVLAN, err);
1864 static int br_vlan_replay_one(struct notifier_block *nb,
1865 struct net_device *dev,
1866 struct switchdev_obj_port_vlan *vlan,
1867 const void *ctx, unsigned long action,
1868 struct netlink_ext_ack *extack)
1870 struct switchdev_notifier_port_obj_info obj_info = {
1880 err = nb->notifier_call(nb, action, &obj_info);
1881 return notifier_to_errno(err);
1884 int br_vlan_replay(struct net_device *br_dev, struct net_device *dev,
1885 const void *ctx, bool adding, struct notifier_block *nb,
1886 struct netlink_ext_ack *extack)
1888 struct net_bridge_vlan_group *vg;
1889 struct net_bridge_vlan *v;
1890 struct net_bridge_port *p;
1891 struct net_bridge *br;
1892 unsigned long action;
1901 if (!netif_is_bridge_master(br_dev))
1904 if (!netif_is_bridge_master(dev) && !netif_is_bridge_port(dev))
1907 if (netif_is_bridge_master(dev)) {
1908 br = netdev_priv(dev);
1909 vg = br_vlan_group(br);
1912 p = br_port_get_rtnl(dev);
1915 vg = nbp_vlan_group(p);
1923 action = SWITCHDEV_PORT_OBJ_ADD;
1925 action = SWITCHDEV_PORT_OBJ_DEL;
1927 pvid = br_get_pvid(vg);
1929 list_for_each_entry(v, &vg->vlan_list, vlist) {
1930 struct switchdev_obj_port_vlan vlan = {
1931 .obj.orig_dev = dev,
1932 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
1933 .flags = br_vlan_flags(v, pvid),
1937 if (!br_vlan_should_use(v))
1940 err = br_vlan_replay_one(nb, dev, &vlan, ctx, action, extack);
1948 /* check if v_curr can enter a range ending in range_end */
1949 bool br_vlan_can_enter_range(const struct net_bridge_vlan *v_curr,
1950 const struct net_bridge_vlan *range_end)
1952 return v_curr->vid - range_end->vid == 1 &&
1953 range_end->flags == v_curr->flags &&
1954 br_vlan_opts_eq_range(v_curr, range_end);
1957 static int br_vlan_dump_dev(const struct net_device *dev,
1958 struct sk_buff *skb,
1959 struct netlink_callback *cb,
1962 struct net_bridge_vlan *v, *range_start = NULL, *range_end = NULL;
1963 bool dump_global = !!(dump_flags & BRIDGE_VLANDB_DUMPF_GLOBAL);
1964 bool dump_stats = !!(dump_flags & BRIDGE_VLANDB_DUMPF_STATS);
1965 struct net_bridge_vlan_group *vg;
1966 int idx = 0, s_idx = cb->args[1];
1967 struct nlmsghdr *nlh = NULL;
1968 struct net_bridge_port *p;
1969 struct br_vlan_msg *bvm;
1970 struct net_bridge *br;
1974 if (!netif_is_bridge_master(dev) && !netif_is_bridge_port(dev))
1977 if (netif_is_bridge_master(dev)) {
1978 br = netdev_priv(dev);
1979 vg = br_vlan_group_rcu(br);
1982 /* global options are dumped only for bridge devices */
1986 p = br_port_get_rcu(dev);
1989 vg = nbp_vlan_group_rcu(p);
1996 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1997 RTM_NEWVLAN, sizeof(*bvm), NLM_F_MULTI);
2000 bvm = nlmsg_data(nlh);
2001 memset(bvm, 0, sizeof(*bvm));
2002 bvm->family = PF_BRIDGE;
2003 bvm->ifindex = dev->ifindex;
2004 pvid = br_get_pvid(vg);
2006 /* idx must stay at range's beginning until it is filled in */
2007 list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
2008 if (!dump_global && !br_vlan_should_use(v))
2022 if (br_vlan_global_opts_can_enter_range(v, range_end))
2024 if (!br_vlan_global_opts_fill(skb, range_start->vid,
2030 /* advance number of filled vlans */
2031 idx += range_end->vid - range_start->vid + 1;
2034 } else if (dump_stats || v->vid == pvid ||
2035 !br_vlan_can_enter_range(v, range_end)) {
2036 u16 vlan_flags = br_vlan_flags(range_start, pvid);
2038 if (!br_vlan_fill_vids(skb, range_start->vid,
2039 range_end->vid, range_start,
2040 vlan_flags, dump_stats)) {
2044 /* advance number of filled vlans */
2045 idx += range_end->vid - range_start->vid + 1;
2053 /* err will be 0 and range_start will be set in 3 cases here:
2054 * - first vlan (range_start == range_end)
2055 * - last vlan (range_start == range_end, not in range)
2056 * - last vlan range (range_start != range_end, in range)
2058 if (!err && range_start) {
2060 !br_vlan_global_opts_fill(skb, range_start->vid,
2061 range_end->vid, range_start))
2063 else if (!dump_global &&
2064 !br_vlan_fill_vids(skb, range_start->vid,
2065 range_end->vid, range_start,
2066 br_vlan_flags(range_start, pvid),
2071 cb->args[1] = err ? idx : 0;
2073 nlmsg_end(skb, nlh);
2078 static const struct nla_policy br_vlan_db_dump_pol[BRIDGE_VLANDB_DUMP_MAX + 1] = {
2079 [BRIDGE_VLANDB_DUMP_FLAGS] = { .type = NLA_U32 },
2082 static int br_vlan_rtm_dump(struct sk_buff *skb, struct netlink_callback *cb)
2084 struct nlattr *dtb[BRIDGE_VLANDB_DUMP_MAX + 1];
2085 int idx = 0, err = 0, s_idx = cb->args[0];
2086 struct net *net = sock_net(skb->sk);
2087 struct br_vlan_msg *bvm;
2088 struct net_device *dev;
2091 err = nlmsg_parse(cb->nlh, sizeof(*bvm), dtb, BRIDGE_VLANDB_DUMP_MAX,
2092 br_vlan_db_dump_pol, cb->extack);
2096 bvm = nlmsg_data(cb->nlh);
2097 if (dtb[BRIDGE_VLANDB_DUMP_FLAGS])
2098 dump_flags = nla_get_u32(dtb[BRIDGE_VLANDB_DUMP_FLAGS]);
2102 dev = dev_get_by_index_rcu(net, bvm->ifindex);
2107 err = br_vlan_dump_dev(dev, skb, cb, dump_flags);
2108 if (err && err != -EMSGSIZE)
2111 for_each_netdev_rcu(net, dev) {
2115 err = br_vlan_dump_dev(dev, skb, cb, dump_flags);
2116 if (err == -EMSGSIZE)
2133 static const struct nla_policy br_vlan_db_policy[BRIDGE_VLANDB_ENTRY_MAX + 1] = {
2134 [BRIDGE_VLANDB_ENTRY_INFO] =
2135 NLA_POLICY_EXACT_LEN(sizeof(struct bridge_vlan_info)),
2136 [BRIDGE_VLANDB_ENTRY_RANGE] = { .type = NLA_U16 },
2137 [BRIDGE_VLANDB_ENTRY_STATE] = { .type = NLA_U8 },
2138 [BRIDGE_VLANDB_ENTRY_TUNNEL_INFO] = { .type = NLA_NESTED },
2139 [BRIDGE_VLANDB_ENTRY_MCAST_ROUTER] = { .type = NLA_U8 },
2142 static int br_vlan_rtm_process_one(struct net_device *dev,
2143 const struct nlattr *attr,
2144 int cmd, struct netlink_ext_ack *extack)
2146 struct bridge_vlan_info *vinfo, vrange_end, *vinfo_last = NULL;
2147 struct nlattr *tb[BRIDGE_VLANDB_ENTRY_MAX + 1];
2148 bool changed = false, skip_processing = false;
2149 struct net_bridge_vlan_group *vg;
2150 struct net_bridge_port *p = NULL;
2151 int err = 0, cmdmap = 0;
2152 struct net_bridge *br;
2154 if (netif_is_bridge_master(dev)) {
2155 br = netdev_priv(dev);
2156 vg = br_vlan_group(br);
2158 p = br_port_get_rtnl(dev);
2162 vg = nbp_vlan_group(p);
2168 err = nla_parse_nested(tb, BRIDGE_VLANDB_ENTRY_MAX, attr,
2169 br_vlan_db_policy, extack);
2173 if (!tb[BRIDGE_VLANDB_ENTRY_INFO]) {
2174 NL_SET_ERR_MSG_MOD(extack, "Missing vlan entry info");
2177 memset(&vrange_end, 0, sizeof(vrange_end));
2179 vinfo = nla_data(tb[BRIDGE_VLANDB_ENTRY_INFO]);
2180 if (vinfo->flags & (BRIDGE_VLAN_INFO_RANGE_BEGIN |
2181 BRIDGE_VLAN_INFO_RANGE_END)) {
2182 NL_SET_ERR_MSG_MOD(extack, "Old-style vlan ranges are not allowed when using RTM vlan calls");
2185 if (!br_vlan_valid_id(vinfo->vid, extack))
2188 if (tb[BRIDGE_VLANDB_ENTRY_RANGE]) {
2189 vrange_end.vid = nla_get_u16(tb[BRIDGE_VLANDB_ENTRY_RANGE]);
2190 /* validate user-provided flags without RANGE_BEGIN */
2191 vrange_end.flags = BRIDGE_VLAN_INFO_RANGE_END | vinfo->flags;
2192 vinfo->flags |= BRIDGE_VLAN_INFO_RANGE_BEGIN;
2194 /* vinfo_last is the range start, vinfo the range end */
2196 vinfo = &vrange_end;
2198 if (!br_vlan_valid_id(vinfo->vid, extack) ||
2199 !br_vlan_valid_range(vinfo, vinfo_last, extack))
2205 cmdmap = RTM_SETLINK;
2206 skip_processing = !!(vinfo->flags & BRIDGE_VLAN_INFO_ONLY_OPTS);
2209 cmdmap = RTM_DELLINK;
2213 if (!skip_processing) {
2214 struct bridge_vlan_info *tmp_last = vinfo_last;
2216 /* br_process_vlan_info may overwrite vinfo_last */
2217 err = br_process_vlan_info(br, p, cmdmap, vinfo, &tmp_last,
2220 /* notify first if anything changed */
2222 br_ifinfo_notify(cmdmap, br, p);
2228 /* deal with options */
2229 if (cmd == RTM_NEWVLAN) {
2230 struct net_bridge_vlan *range_start, *range_end;
2233 range_start = br_vlan_find(vg, vinfo_last->vid);
2234 range_end = br_vlan_find(vg, vinfo->vid);
2236 range_start = br_vlan_find(vg, vinfo->vid);
2237 range_end = range_start;
2240 err = br_vlan_process_options(br, p, range_start, range_end,
2247 static int br_vlan_rtm_process(struct sk_buff *skb, struct nlmsghdr *nlh,
2248 struct netlink_ext_ack *extack)
2250 struct net *net = sock_net(skb->sk);
2251 struct br_vlan_msg *bvm;
2252 struct net_device *dev;
2253 struct nlattr *attr;
2257 /* this should validate the header and check for remaining bytes */
2258 err = nlmsg_parse(nlh, sizeof(*bvm), NULL, BRIDGE_VLANDB_MAX, NULL,
2263 bvm = nlmsg_data(nlh);
2264 dev = __dev_get_by_index(net, bvm->ifindex);
2268 if (!netif_is_bridge_master(dev) && !netif_is_bridge_port(dev)) {
2269 NL_SET_ERR_MSG_MOD(extack, "The device is not a valid bridge or bridge port");
2273 nlmsg_for_each_attr(attr, nlh, sizeof(*bvm), rem) {
2274 switch (nla_type(attr)) {
2275 case BRIDGE_VLANDB_ENTRY:
2276 err = br_vlan_rtm_process_one(dev, attr,
2280 case BRIDGE_VLANDB_GLOBAL_OPTIONS:
2281 err = br_vlan_rtm_process_global_options(dev, attr,
2294 NL_SET_ERR_MSG_MOD(extack, "No vlans found to process");
2301 void br_vlan_rtnl_init(void)
2303 rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_GETVLAN, NULL,
2304 br_vlan_rtm_dump, 0);
2305 rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_NEWVLAN,
2306 br_vlan_rtm_process, NULL, 0);
2307 rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_DELVLAN,
2308 br_vlan_rtm_process, NULL, 0);
2311 void br_vlan_rtnl_uninit(void)
2313 rtnl_unregister(PF_BRIDGE, RTM_GETVLAN);
2314 rtnl_unregister(PF_BRIDGE, RTM_NEWVLAN);
2315 rtnl_unregister(PF_BRIDGE, RTM_DELVLAN);