1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/kernel.h>
3 #include <linux/netdevice.h>
4 #include <linux/rtnetlink.h>
5 #include <linux/slab.h>
6 #include <net/switchdev.h>
8 #include "br_private.h"
9 #include "br_private_tunnel.h"
11 static void nbp_vlan_set_vlan_dev_state(struct net_bridge_port
*p
, u16 vid
);
13 static inline int br_vlan_cmp(struct rhashtable_compare_arg
*arg
,
16 const struct net_bridge_vlan
*vle
= ptr
;
17 u16 vid
= *(u16
*)arg
->key
;
19 return vle
->vid
!= vid
;
22 static const struct rhashtable_params br_vlan_rht_params
= {
23 .head_offset
= offsetof(struct net_bridge_vlan
, vnode
),
24 .key_offset
= offsetof(struct net_bridge_vlan
, vid
),
25 .key_len
= sizeof(u16
),
27 .max_size
= VLAN_N_VID
,
28 .obj_cmpfn
= br_vlan_cmp
,
29 .automatic_shrinking
= true,
32 static struct net_bridge_vlan
*br_vlan_lookup(struct rhashtable
*tbl
, u16 vid
)
34 return rhashtable_lookup_fast(tbl
, &vid
, br_vlan_rht_params
);
37 static bool __vlan_add_pvid(struct net_bridge_vlan_group
*vg
,
38 const struct net_bridge_vlan
*v
)
40 if (vg
->pvid
== v
->vid
)
44 br_vlan_set_pvid_state(vg
, v
->state
);
50 static bool __vlan_delete_pvid(struct net_bridge_vlan_group
*vg
, u16 vid
)
61 /* return true if anything changed, false otherwise */
62 static bool __vlan_add_flags(struct net_bridge_vlan
*v
, u16 flags
)
64 struct net_bridge_vlan_group
*vg
;
65 u16 old_flags
= v
->flags
;
68 if (br_vlan_is_master(v
))
69 vg
= br_vlan_group(v
->br
);
71 vg
= nbp_vlan_group(v
->port
);
73 if (flags
& BRIDGE_VLAN_INFO_PVID
)
74 ret
= __vlan_add_pvid(vg
, v
);
76 ret
= __vlan_delete_pvid(vg
, v
->vid
);
78 if (flags
& BRIDGE_VLAN_INFO_UNTAGGED
)
79 v
->flags
|= BRIDGE_VLAN_INFO_UNTAGGED
;
81 v
->flags
&= ~BRIDGE_VLAN_INFO_UNTAGGED
;
83 return ret
|| !!(old_flags
^ v
->flags
);
86 static int __vlan_vid_add(struct net_device
*dev
, struct net_bridge
*br
,
87 struct net_bridge_vlan
*v
, u16 flags
,
88 struct netlink_ext_ack
*extack
)
92 /* Try switchdev op first. In case it is not supported, fallback to
95 err
= br_switchdev_port_vlan_add(dev
, v
->vid
, flags
, extack
);
96 if (err
== -EOPNOTSUPP
)
97 return vlan_vid_add(dev
, br
->vlan_proto
, v
->vid
);
98 v
->priv_flags
|= BR_VLFLAG_ADDED_BY_SWITCHDEV
;
102 static void __vlan_add_list(struct net_bridge_vlan
*v
)
104 struct net_bridge_vlan_group
*vg
;
105 struct list_head
*headp
, *hpos
;
106 struct net_bridge_vlan
*vent
;
108 if (br_vlan_is_master(v
))
109 vg
= br_vlan_group(v
->br
);
111 vg
= nbp_vlan_group(v
->port
);
113 headp
= &vg
->vlan_list
;
114 list_for_each_prev(hpos
, headp
) {
115 vent
= list_entry(hpos
, struct net_bridge_vlan
, vlist
);
116 if (v
->vid
< vent
->vid
)
121 list_add_rcu(&v
->vlist
, hpos
);
124 static void __vlan_del_list(struct net_bridge_vlan
*v
)
126 list_del_rcu(&v
->vlist
);
129 static int __vlan_vid_del(struct net_device
*dev
, struct net_bridge
*br
,
130 const struct net_bridge_vlan
*v
)
134 /* Try switchdev op first. In case it is not supported, fallback to
137 err
= br_switchdev_port_vlan_del(dev
, v
->vid
);
138 if (!(v
->priv_flags
& BR_VLFLAG_ADDED_BY_SWITCHDEV
))
139 vlan_vid_del(dev
, br
->vlan_proto
, v
->vid
);
140 return err
== -EOPNOTSUPP
? 0 : err
;
143 /* Returns a master vlan, if it didn't exist it gets created. In all cases a
144 * a reference is taken to the master vlan before returning.
146 static struct net_bridge_vlan
*
147 br_vlan_get_master(struct net_bridge
*br
, u16 vid
,
148 struct netlink_ext_ack
*extack
)
150 struct net_bridge_vlan_group
*vg
;
151 struct net_bridge_vlan
*masterv
;
153 vg
= br_vlan_group(br
);
154 masterv
= br_vlan_find(vg
, vid
);
158 /* missing global ctx, create it now */
159 if (br_vlan_add(br
, vid
, 0, &changed
, extack
))
161 masterv
= br_vlan_find(vg
, vid
);
162 if (WARN_ON(!masterv
))
164 refcount_set(&masterv
->refcnt
, 1);
167 refcount_inc(&masterv
->refcnt
);
172 static void br_master_vlan_rcu_free(struct rcu_head
*rcu
)
174 struct net_bridge_vlan
*v
;
176 v
= container_of(rcu
, struct net_bridge_vlan
, rcu
);
177 WARN_ON(!br_vlan_is_master(v
));
178 free_percpu(v
->stats
);
183 static void br_vlan_put_master(struct net_bridge_vlan
*masterv
)
185 struct net_bridge_vlan_group
*vg
;
187 if (!br_vlan_is_master(masterv
))
190 vg
= br_vlan_group(masterv
->br
);
191 if (refcount_dec_and_test(&masterv
->refcnt
)) {
192 rhashtable_remove_fast(&vg
->vlan_hash
,
193 &masterv
->vnode
, br_vlan_rht_params
);
194 __vlan_del_list(masterv
);
195 call_rcu(&masterv
->rcu
, br_master_vlan_rcu_free
);
199 static void nbp_vlan_rcu_free(struct rcu_head
*rcu
)
201 struct net_bridge_vlan
*v
;
203 v
= container_of(rcu
, struct net_bridge_vlan
, rcu
);
204 WARN_ON(br_vlan_is_master(v
));
205 /* if we had per-port stats configured then free them here */
206 if (v
->priv_flags
& BR_VLFLAG_PER_PORT_STATS
)
207 free_percpu(v
->stats
);
212 /* This is the shared VLAN add function which works for both ports and bridge
213 * devices. There are four possible calls to this function in terms of the
215 * 1. vlan is being added on a port (no master flags, global entry exists)
216 * 2. vlan is being added on a bridge (both master and brentry flags)
217 * 3. vlan is being added on a port, but a global entry didn't exist which
218 * is being created right now (master flag set, brentry flag unset), the
219 * global entry is used for global per-vlan features, but not for filtering
220 * 4. same as 3 but with both master and brentry flags set so the entry
221 * will be used for filtering in both the port and the bridge
223 static int __vlan_add(struct net_bridge_vlan
*v
, u16 flags
,
224 struct netlink_ext_ack
*extack
)
226 struct net_bridge_vlan
*masterv
= NULL
;
227 struct net_bridge_port
*p
= NULL
;
228 struct net_bridge_vlan_group
*vg
;
229 struct net_device
*dev
;
230 struct net_bridge
*br
;
233 if (br_vlan_is_master(v
)) {
236 vg
= br_vlan_group(br
);
241 vg
= nbp_vlan_group(p
);
245 /* Add VLAN to the device filter if it is supported.
246 * This ensures tagged traffic enters the bridge when
247 * promiscuous mode is disabled by br_manage_promisc().
249 err
= __vlan_vid_add(dev
, br
, v
, flags
, extack
);
253 /* need to work on the master vlan too */
254 if (flags
& BRIDGE_VLAN_INFO_MASTER
) {
257 err
= br_vlan_add(br
, v
->vid
,
258 flags
| BRIDGE_VLAN_INFO_BRENTRY
,
264 br_vlan_notify(br
, NULL
, v
->vid
, 0,
268 masterv
= br_vlan_get_master(br
, v
->vid
, extack
);
272 if (br_opt_get(br
, BROPT_VLAN_STATS_PER_PORT
)) {
273 v
->stats
= netdev_alloc_pcpu_stats(struct br_vlan_stats
);
278 v
->priv_flags
|= BR_VLFLAG_PER_PORT_STATS
;
280 v
->stats
= masterv
->stats
;
283 err
= br_switchdev_port_vlan_add(dev
, v
->vid
, flags
, extack
);
284 if (err
&& err
!= -EOPNOTSUPP
)
288 /* Add the dev mac and count the vlan only if it's usable */
289 if (br_vlan_should_use(v
)) {
290 err
= br_fdb_insert(br
, p
, dev
->dev_addr
, v
->vid
);
292 br_err(br
, "failed insert local address into bridge forwarding table\n");
298 /* set the state before publishing */
299 v
->state
= BR_STATE_FORWARDING
;
301 err
= rhashtable_lookup_insert_fast(&vg
->vlan_hash
, &v
->vnode
,
307 __vlan_add_flags(v
, flags
);
310 nbp_vlan_set_vlan_dev_state(p
, v
->vid
);
315 if (br_vlan_should_use(v
)) {
316 br_fdb_find_delete_local(br
, p
, dev
->dev_addr
, v
->vid
);
322 __vlan_vid_del(dev
, br
, v
);
324 if (v
->stats
&& masterv
->stats
!= v
->stats
)
325 free_percpu(v
->stats
);
328 br_vlan_put_master(masterv
);
332 br_switchdev_port_vlan_del(dev
, v
->vid
);
338 static int __vlan_del(struct net_bridge_vlan
*v
)
340 struct net_bridge_vlan
*masterv
= v
;
341 struct net_bridge_vlan_group
*vg
;
342 struct net_bridge_port
*p
= NULL
;
345 if (br_vlan_is_master(v
)) {
346 vg
= br_vlan_group(v
->br
);
349 vg
= nbp_vlan_group(v
->port
);
353 __vlan_delete_pvid(vg
, v
->vid
);
355 err
= __vlan_vid_del(p
->dev
, p
->br
, v
);
359 err
= br_switchdev_port_vlan_del(v
->br
->dev
, v
->vid
);
360 if (err
&& err
!= -EOPNOTSUPP
)
365 if (br_vlan_should_use(v
)) {
366 v
->flags
&= ~BRIDGE_VLAN_INFO_BRENTRY
;
371 vlan_tunnel_info_del(vg
, v
);
372 rhashtable_remove_fast(&vg
->vlan_hash
, &v
->vnode
,
375 nbp_vlan_set_vlan_dev_state(p
, v
->vid
);
376 call_rcu(&v
->rcu
, nbp_vlan_rcu_free
);
379 br_vlan_put_master(masterv
);
384 static void __vlan_group_free(struct net_bridge_vlan_group
*vg
)
386 WARN_ON(!list_empty(&vg
->vlan_list
));
387 rhashtable_destroy(&vg
->vlan_hash
);
388 vlan_tunnel_deinit(vg
);
392 static void __vlan_flush(const struct net_bridge
*br
,
393 const struct net_bridge_port
*p
,
394 struct net_bridge_vlan_group
*vg
)
396 struct net_bridge_vlan
*vlan
, *tmp
;
397 u16 v_start
= 0, v_end
= 0;
399 __vlan_delete_pvid(vg
, vg
->pvid
);
400 list_for_each_entry_safe(vlan
, tmp
, &vg
->vlan_list
, vlist
) {
401 /* take care of disjoint ranges */
404 } else if (vlan
->vid
- v_end
!= 1) {
405 /* found range end, notify and start next one */
406 br_vlan_notify(br
, p
, v_start
, v_end
, RTM_DELVLAN
);
414 /* notify about the last/whole vlan range */
416 br_vlan_notify(br
, p
, v_start
, v_end
, RTM_DELVLAN
);
419 struct sk_buff
*br_handle_vlan(struct net_bridge
*br
,
420 const struct net_bridge_port
*p
,
421 struct net_bridge_vlan_group
*vg
,
424 struct br_vlan_stats
*stats
;
425 struct net_bridge_vlan
*v
;
428 /* If this packet was not filtered at input, let it pass */
429 if (!BR_INPUT_SKB_CB(skb
)->vlan_filtered
)
432 /* At this point, we know that the frame was filtered and contains
433 * a valid vlan id. If the vlan id has untagged flag set,
434 * send untagged; otherwise, send tagged.
436 br_vlan_get_tag(skb
, &vid
);
437 v
= br_vlan_find(vg
, vid
);
438 /* Vlan entry must be configured at this point. The
439 * only exception is the bridge is set in promisc mode and the
440 * packet is destined for the bridge device. In this case
441 * pass the packet as is.
443 if (!v
|| !br_vlan_should_use(v
)) {
444 if ((br
->dev
->flags
& IFF_PROMISC
) && skb
->dev
== br
->dev
) {
451 if (br_opt_get(br
, BROPT_VLAN_STATS_ENABLED
)) {
452 stats
= this_cpu_ptr(v
->stats
);
453 u64_stats_update_begin(&stats
->syncp
);
454 stats
->tx_bytes
+= skb
->len
;
456 u64_stats_update_end(&stats
->syncp
);
459 if (v
->flags
& BRIDGE_VLAN_INFO_UNTAGGED
)
460 __vlan_hwaccel_clear_tag(skb
);
462 if (p
&& (p
->flags
& BR_VLAN_TUNNEL
) &&
463 br_handle_egress_vlan_tunnel(skb
, v
)) {
471 /* Called under RCU */
472 static bool __allowed_ingress(const struct net_bridge
*br
,
473 struct net_bridge_vlan_group
*vg
,
474 struct sk_buff
*skb
, u16
*vid
,
477 struct br_vlan_stats
*stats
;
478 struct net_bridge_vlan
*v
;
481 BR_INPUT_SKB_CB(skb
)->vlan_filtered
= true;
482 /* If vlan tx offload is disabled on bridge device and frame was
483 * sent from vlan device on the bridge device, it does not have
484 * HW accelerated vlan tag.
486 if (unlikely(!skb_vlan_tag_present(skb
) &&
487 skb
->protocol
== br
->vlan_proto
)) {
488 skb
= skb_vlan_untag(skb
);
493 if (!br_vlan_get_tag(skb
, vid
)) {
495 if (skb
->vlan_proto
!= br
->vlan_proto
) {
496 /* Protocol-mismatch, empty out vlan_tci for new tag */
497 skb_push(skb
, ETH_HLEN
);
498 skb
= vlan_insert_tag_set_proto(skb
, skb
->vlan_proto
,
499 skb_vlan_tag_get(skb
));
503 skb_pull(skb
, ETH_HLEN
);
504 skb_reset_mac_len(skb
);
516 u16 pvid
= br_get_pvid(vg
);
518 /* Frame had a tag with VID 0 or did not have a tag.
519 * See if pvid is set on this port. That tells us which
520 * vlan untagged or priority-tagged traffic belongs to.
525 /* PVID is set on this port. Any untagged or priority-tagged
526 * ingress frame is considered to belong to this vlan.
530 /* Untagged Frame. */
531 __vlan_hwaccel_put_tag(skb
, br
->vlan_proto
, pvid
);
533 /* Priority-tagged Frame.
534 * At this point, we know that skb->vlan_tci VID
536 * We update only VID field and preserve PCP field.
538 skb
->vlan_tci
|= pvid
;
540 /* if stats are disabled we can avoid the lookup */
541 if (!br_opt_get(br
, BROPT_VLAN_STATS_ENABLED
)) {
542 if (*state
== BR_STATE_FORWARDING
) {
543 *state
= br_vlan_get_pvid_state(vg
);
544 return br_vlan_state_allowed(*state
, true);
550 v
= br_vlan_find(vg
, *vid
);
551 if (!v
|| !br_vlan_should_use(v
))
554 if (*state
== BR_STATE_FORWARDING
) {
555 *state
= br_vlan_get_state(v
);
556 if (!br_vlan_state_allowed(*state
, true))
560 if (br_opt_get(br
, BROPT_VLAN_STATS_ENABLED
)) {
561 stats
= this_cpu_ptr(v
->stats
);
562 u64_stats_update_begin(&stats
->syncp
);
563 stats
->rx_bytes
+= skb
->len
;
565 u64_stats_update_end(&stats
->syncp
);
575 bool br_allowed_ingress(const struct net_bridge
*br
,
576 struct net_bridge_vlan_group
*vg
, struct sk_buff
*skb
,
579 /* If VLAN filtering is disabled on the bridge, all packets are
582 if (!br_opt_get(br
, BROPT_VLAN_ENABLED
)) {
583 BR_INPUT_SKB_CB(skb
)->vlan_filtered
= false;
587 return __allowed_ingress(br
, vg
, skb
, vid
, state
);
590 /* Called under RCU. */
591 bool br_allowed_egress(struct net_bridge_vlan_group
*vg
,
592 const struct sk_buff
*skb
)
594 const struct net_bridge_vlan
*v
;
597 /* If this packet was not filtered at input, let it pass */
598 if (!BR_INPUT_SKB_CB(skb
)->vlan_filtered
)
601 br_vlan_get_tag(skb
, &vid
);
602 v
= br_vlan_find(vg
, vid
);
603 if (v
&& br_vlan_should_use(v
) &&
604 br_vlan_state_allowed(br_vlan_get_state(v
), false))
610 /* Called under RCU */
611 bool br_should_learn(struct net_bridge_port
*p
, struct sk_buff
*skb
, u16
*vid
)
613 struct net_bridge_vlan_group
*vg
;
614 struct net_bridge
*br
= p
->br
;
615 struct net_bridge_vlan
*v
;
617 /* If filtering was disabled at input, let it pass. */
618 if (!br_opt_get(br
, BROPT_VLAN_ENABLED
))
621 vg
= nbp_vlan_group_rcu(p
);
622 if (!vg
|| !vg
->num_vlans
)
625 if (!br_vlan_get_tag(skb
, vid
) && skb
->vlan_proto
!= br
->vlan_proto
)
629 *vid
= br_get_pvid(vg
);
631 !br_vlan_state_allowed(br_vlan_get_pvid_state(vg
), true))
637 v
= br_vlan_find(vg
, *vid
);
638 if (v
&& br_vlan_state_allowed(br_vlan_get_state(v
), true))
644 static int br_vlan_add_existing(struct net_bridge
*br
,
645 struct net_bridge_vlan_group
*vg
,
646 struct net_bridge_vlan
*vlan
,
647 u16 flags
, bool *changed
,
648 struct netlink_ext_ack
*extack
)
652 err
= br_switchdev_port_vlan_add(br
->dev
, vlan
->vid
, flags
, extack
);
653 if (err
&& err
!= -EOPNOTSUPP
)
656 if (!br_vlan_is_brentry(vlan
)) {
657 /* Trying to change flags of non-existent bridge vlan */
658 if (!(flags
& BRIDGE_VLAN_INFO_BRENTRY
)) {
662 /* It was only kept for port vlans, now make it real */
663 err
= br_fdb_insert(br
, NULL
, br
->dev
->dev_addr
,
666 br_err(br
, "failed to insert local address into bridge forwarding table\n");
670 refcount_inc(&vlan
->refcnt
);
671 vlan
->flags
|= BRIDGE_VLAN_INFO_BRENTRY
;
676 if (__vlan_add_flags(vlan
, flags
))
683 br_switchdev_port_vlan_del(br
->dev
, vlan
->vid
);
687 /* Must be protected by RTNL.
688 * Must be called with vid in range from 1 to 4094 inclusive.
689 * changed must be true only if the vlan was created or updated
691 int br_vlan_add(struct net_bridge
*br
, u16 vid
, u16 flags
, bool *changed
,
692 struct netlink_ext_ack
*extack
)
694 struct net_bridge_vlan_group
*vg
;
695 struct net_bridge_vlan
*vlan
;
701 vg
= br_vlan_group(br
);
702 vlan
= br_vlan_find(vg
, vid
);
704 return br_vlan_add_existing(br
, vg
, vlan
, flags
, changed
,
707 vlan
= kzalloc(sizeof(*vlan
), GFP_KERNEL
);
711 vlan
->stats
= netdev_alloc_pcpu_stats(struct br_vlan_stats
);
717 vlan
->flags
= flags
| BRIDGE_VLAN_INFO_MASTER
;
718 vlan
->flags
&= ~BRIDGE_VLAN_INFO_PVID
;
720 if (flags
& BRIDGE_VLAN_INFO_BRENTRY
)
721 refcount_set(&vlan
->refcnt
, 1);
722 ret
= __vlan_add(vlan
, flags
, extack
);
724 free_percpu(vlan
->stats
);
733 /* Must be protected by RTNL.
734 * Must be called with vid in range from 1 to 4094 inclusive.
736 int br_vlan_delete(struct net_bridge
*br
, u16 vid
)
738 struct net_bridge_vlan_group
*vg
;
739 struct net_bridge_vlan
*v
;
743 vg
= br_vlan_group(br
);
744 v
= br_vlan_find(vg
, vid
);
745 if (!v
|| !br_vlan_is_brentry(v
))
748 br_fdb_find_delete_local(br
, NULL
, br
->dev
->dev_addr
, vid
);
749 br_fdb_delete_by_port(br
, NULL
, vid
, 0);
751 vlan_tunnel_info_del(vg
, v
);
753 return __vlan_del(v
);
756 void br_vlan_flush(struct net_bridge
*br
)
758 struct net_bridge_vlan_group
*vg
;
762 vg
= br_vlan_group(br
);
763 __vlan_flush(br
, NULL
, vg
);
764 RCU_INIT_POINTER(br
->vlgrp
, NULL
);
766 __vlan_group_free(vg
);
769 struct net_bridge_vlan
*br_vlan_find(struct net_bridge_vlan_group
*vg
, u16 vid
)
774 return br_vlan_lookup(&vg
->vlan_hash
, vid
);
777 /* Must be protected by RTNL. */
778 static void recalculate_group_addr(struct net_bridge
*br
)
780 if (br_opt_get(br
, BROPT_GROUP_ADDR_SET
))
783 spin_lock_bh(&br
->lock
);
784 if (!br_opt_get(br
, BROPT_VLAN_ENABLED
) ||
785 br
->vlan_proto
== htons(ETH_P_8021Q
)) {
786 /* Bridge Group Address */
787 br
->group_addr
[5] = 0x00;
788 } else { /* vlan_enabled && ETH_P_8021AD */
789 /* Provider Bridge Group Address */
790 br
->group_addr
[5] = 0x08;
792 spin_unlock_bh(&br
->lock
);
795 /* Must be protected by RTNL. */
796 void br_recalculate_fwd_mask(struct net_bridge
*br
)
798 if (!br_opt_get(br
, BROPT_VLAN_ENABLED
) ||
799 br
->vlan_proto
== htons(ETH_P_8021Q
))
800 br
->group_fwd_mask_required
= BR_GROUPFWD_DEFAULT
;
801 else /* vlan_enabled && ETH_P_8021AD */
802 br
->group_fwd_mask_required
= BR_GROUPFWD_8021AD
&
803 ~(1u << br
->group_addr
[5]);
806 int __br_vlan_filter_toggle(struct net_bridge
*br
, unsigned long val
)
808 struct switchdev_attr attr
= {
810 .id
= SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING
,
811 .flags
= SWITCHDEV_F_SKIP_EOPNOTSUPP
,
812 .u
.vlan_filtering
= val
,
816 if (br_opt_get(br
, BROPT_VLAN_ENABLED
) == !!val
)
819 err
= switchdev_port_attr_set(br
->dev
, &attr
);
820 if (err
&& err
!= -EOPNOTSUPP
)
823 br_opt_toggle(br
, BROPT_VLAN_ENABLED
, !!val
);
824 br_manage_promisc(br
);
825 recalculate_group_addr(br
);
826 br_recalculate_fwd_mask(br
);
831 int br_vlan_filter_toggle(struct net_bridge
*br
, unsigned long val
)
833 return __br_vlan_filter_toggle(br
, val
);
836 bool br_vlan_enabled(const struct net_device
*dev
)
838 struct net_bridge
*br
= netdev_priv(dev
);
840 return br_opt_get(br
, BROPT_VLAN_ENABLED
);
842 EXPORT_SYMBOL_GPL(br_vlan_enabled
);
844 int br_vlan_get_proto(const struct net_device
*dev
, u16
*p_proto
)
846 struct net_bridge
*br
= netdev_priv(dev
);
848 *p_proto
= ntohs(br
->vlan_proto
);
852 EXPORT_SYMBOL_GPL(br_vlan_get_proto
);
854 int __br_vlan_set_proto(struct net_bridge
*br
, __be16 proto
)
857 struct net_bridge_port
*p
;
858 struct net_bridge_vlan
*vlan
;
859 struct net_bridge_vlan_group
*vg
;
862 if (br
->vlan_proto
== proto
)
865 /* Add VLANs for the new proto to the device filter. */
866 list_for_each_entry(p
, &br
->port_list
, list
) {
867 vg
= nbp_vlan_group(p
);
868 list_for_each_entry(vlan
, &vg
->vlan_list
, vlist
) {
869 err
= vlan_vid_add(p
->dev
, proto
, vlan
->vid
);
875 oldproto
= br
->vlan_proto
;
876 br
->vlan_proto
= proto
;
878 recalculate_group_addr(br
);
879 br_recalculate_fwd_mask(br
);
881 /* Delete VLANs for the old proto from the device filter. */
882 list_for_each_entry(p
, &br
->port_list
, list
) {
883 vg
= nbp_vlan_group(p
);
884 list_for_each_entry(vlan
, &vg
->vlan_list
, vlist
)
885 vlan_vid_del(p
->dev
, oldproto
, vlan
->vid
);
891 list_for_each_entry_continue_reverse(vlan
, &vg
->vlan_list
, vlist
)
892 vlan_vid_del(p
->dev
, proto
, vlan
->vid
);
894 list_for_each_entry_continue_reverse(p
, &br
->port_list
, list
) {
895 vg
= nbp_vlan_group(p
);
896 list_for_each_entry(vlan
, &vg
->vlan_list
, vlist
)
897 vlan_vid_del(p
->dev
, proto
, vlan
->vid
);
903 int br_vlan_set_proto(struct net_bridge
*br
, unsigned long val
)
905 if (val
!= ETH_P_8021Q
&& val
!= ETH_P_8021AD
)
906 return -EPROTONOSUPPORT
;
908 return __br_vlan_set_proto(br
, htons(val
));
911 int br_vlan_set_stats(struct net_bridge
*br
, unsigned long val
)
916 br_opt_toggle(br
, BROPT_VLAN_STATS_ENABLED
, !!val
);
925 int br_vlan_set_stats_per_port(struct net_bridge
*br
, unsigned long val
)
927 struct net_bridge_port
*p
;
929 /* allow to change the option if there are no port vlans configured */
930 list_for_each_entry(p
, &br
->port_list
, list
) {
931 struct net_bridge_vlan_group
*vg
= nbp_vlan_group(p
);
940 br_opt_toggle(br
, BROPT_VLAN_STATS_PER_PORT
, !!val
);
949 static bool vlan_default_pvid(struct net_bridge_vlan_group
*vg
, u16 vid
)
951 struct net_bridge_vlan
*v
;
956 v
= br_vlan_lookup(&vg
->vlan_hash
, vid
);
957 if (v
&& br_vlan_should_use(v
) &&
958 (v
->flags
& BRIDGE_VLAN_INFO_UNTAGGED
))
964 static void br_vlan_disable_default_pvid(struct net_bridge
*br
)
966 struct net_bridge_port
*p
;
967 u16 pvid
= br
->default_pvid
;
969 /* Disable default_pvid on all ports where it is still
972 if (vlan_default_pvid(br_vlan_group(br
), pvid
)) {
973 if (!br_vlan_delete(br
, pvid
))
974 br_vlan_notify(br
, NULL
, pvid
, 0, RTM_DELVLAN
);
977 list_for_each_entry(p
, &br
->port_list
, list
) {
978 if (vlan_default_pvid(nbp_vlan_group(p
), pvid
) &&
979 !nbp_vlan_delete(p
, pvid
))
980 br_vlan_notify(br
, p
, pvid
, 0, RTM_DELVLAN
);
983 br
->default_pvid
= 0;
986 int __br_vlan_set_default_pvid(struct net_bridge
*br
, u16 pvid
,
987 struct netlink_ext_ack
*extack
)
989 const struct net_bridge_vlan
*pvent
;
990 struct net_bridge_vlan_group
*vg
;
991 struct net_bridge_port
*p
;
992 unsigned long *changed
;
998 br_vlan_disable_default_pvid(br
);
1002 changed
= bitmap_zalloc(BR_MAX_PORTS
, GFP_KERNEL
);
1006 old_pvid
= br
->default_pvid
;
1008 /* Update default_pvid config only if we do not conflict with
1009 * user configuration.
1011 vg
= br_vlan_group(br
);
1012 pvent
= br_vlan_find(vg
, pvid
);
1013 if ((!old_pvid
|| vlan_default_pvid(vg
, old_pvid
)) &&
1014 (!pvent
|| !br_vlan_should_use(pvent
))) {
1015 err
= br_vlan_add(br
, pvid
,
1016 BRIDGE_VLAN_INFO_PVID
|
1017 BRIDGE_VLAN_INFO_UNTAGGED
|
1018 BRIDGE_VLAN_INFO_BRENTRY
,
1023 if (br_vlan_delete(br
, old_pvid
))
1024 br_vlan_notify(br
, NULL
, old_pvid
, 0, RTM_DELVLAN
);
1025 br_vlan_notify(br
, NULL
, pvid
, 0, RTM_NEWVLAN
);
1026 set_bit(0, changed
);
1029 list_for_each_entry(p
, &br
->port_list
, list
) {
1030 /* Update default_pvid config only if we do not conflict with
1031 * user configuration.
1033 vg
= nbp_vlan_group(p
);
1035 !vlan_default_pvid(vg
, old_pvid
)) ||
1036 br_vlan_find(vg
, pvid
))
1039 err
= nbp_vlan_add(p
, pvid
,
1040 BRIDGE_VLAN_INFO_PVID
|
1041 BRIDGE_VLAN_INFO_UNTAGGED
,
1045 if (nbp_vlan_delete(p
, old_pvid
))
1046 br_vlan_notify(br
, p
, old_pvid
, 0, RTM_DELVLAN
);
1047 br_vlan_notify(p
->br
, p
, pvid
, 0, RTM_NEWVLAN
);
1048 set_bit(p
->port_no
, changed
);
1051 br
->default_pvid
= pvid
;
1054 bitmap_free(changed
);
1058 list_for_each_entry_continue_reverse(p
, &br
->port_list
, list
) {
1059 if (!test_bit(p
->port_no
, changed
))
1063 nbp_vlan_add(p
, old_pvid
,
1064 BRIDGE_VLAN_INFO_PVID
|
1065 BRIDGE_VLAN_INFO_UNTAGGED
,
1067 br_vlan_notify(p
->br
, p
, old_pvid
, 0, RTM_NEWVLAN
);
1069 nbp_vlan_delete(p
, pvid
);
1070 br_vlan_notify(br
, p
, pvid
, 0, RTM_DELVLAN
);
1073 if (test_bit(0, changed
)) {
1075 br_vlan_add(br
, old_pvid
,
1076 BRIDGE_VLAN_INFO_PVID
|
1077 BRIDGE_VLAN_INFO_UNTAGGED
|
1078 BRIDGE_VLAN_INFO_BRENTRY
,
1080 br_vlan_notify(br
, NULL
, old_pvid
, 0, RTM_NEWVLAN
);
1082 br_vlan_delete(br
, pvid
);
1083 br_vlan_notify(br
, NULL
, pvid
, 0, RTM_DELVLAN
);
1088 int br_vlan_set_default_pvid(struct net_bridge
*br
, unsigned long val
)
1093 if (val
>= VLAN_VID_MASK
)
1096 if (pvid
== br
->default_pvid
)
1099 /* Only allow default pvid change when filtering is disabled */
1100 if (br_opt_get(br
, BROPT_VLAN_ENABLED
)) {
1101 pr_info_once("Please disable vlan filtering to change default_pvid\n");
1105 err
= __br_vlan_set_default_pvid(br
, pvid
, NULL
);
1110 int br_vlan_init(struct net_bridge
*br
)
1112 struct net_bridge_vlan_group
*vg
;
1115 vg
= kzalloc(sizeof(*vg
), GFP_KERNEL
);
1118 ret
= rhashtable_init(&vg
->vlan_hash
, &br_vlan_rht_params
);
1121 ret
= vlan_tunnel_init(vg
);
1123 goto err_tunnel_init
;
1124 INIT_LIST_HEAD(&vg
->vlan_list
);
1125 br
->vlan_proto
= htons(ETH_P_8021Q
);
1126 br
->default_pvid
= 1;
1127 rcu_assign_pointer(br
->vlgrp
, vg
);
1133 rhashtable_destroy(&vg
->vlan_hash
);
1140 int nbp_vlan_init(struct net_bridge_port
*p
, struct netlink_ext_ack
*extack
)
1142 struct switchdev_attr attr
= {
1143 .orig_dev
= p
->br
->dev
,
1144 .id
= SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING
,
1145 .flags
= SWITCHDEV_F_SKIP_EOPNOTSUPP
,
1146 .u
.vlan_filtering
= br_opt_get(p
->br
, BROPT_VLAN_ENABLED
),
1148 struct net_bridge_vlan_group
*vg
;
1151 vg
= kzalloc(sizeof(struct net_bridge_vlan_group
), GFP_KERNEL
);
1155 ret
= switchdev_port_attr_set(p
->dev
, &attr
);
1156 if (ret
&& ret
!= -EOPNOTSUPP
)
1157 goto err_vlan_enabled
;
1159 ret
= rhashtable_init(&vg
->vlan_hash
, &br_vlan_rht_params
);
1162 ret
= vlan_tunnel_init(vg
);
1164 goto err_tunnel_init
;
1165 INIT_LIST_HEAD(&vg
->vlan_list
);
1166 rcu_assign_pointer(p
->vlgrp
, vg
);
1167 if (p
->br
->default_pvid
) {
1170 ret
= nbp_vlan_add(p
, p
->br
->default_pvid
,
1171 BRIDGE_VLAN_INFO_PVID
|
1172 BRIDGE_VLAN_INFO_UNTAGGED
,
1176 br_vlan_notify(p
->br
, p
, p
->br
->default_pvid
, 0, RTM_NEWVLAN
);
1182 RCU_INIT_POINTER(p
->vlgrp
, NULL
);
1184 vlan_tunnel_deinit(vg
);
1186 rhashtable_destroy(&vg
->vlan_hash
);
1194 /* Must be protected by RTNL.
1195 * Must be called with vid in range from 1 to 4094 inclusive.
1196 * changed must be true only if the vlan was created or updated
1198 int nbp_vlan_add(struct net_bridge_port
*port
, u16 vid
, u16 flags
,
1199 bool *changed
, struct netlink_ext_ack
*extack
)
1201 struct net_bridge_vlan
*vlan
;
1207 vlan
= br_vlan_find(nbp_vlan_group(port
), vid
);
1209 /* Pass the flags to the hardware bridge */
1210 ret
= br_switchdev_port_vlan_add(port
->dev
, vid
, flags
, extack
);
1211 if (ret
&& ret
!= -EOPNOTSUPP
)
1213 *changed
= __vlan_add_flags(vlan
, flags
);
1218 vlan
= kzalloc(sizeof(*vlan
), GFP_KERNEL
);
1224 ret
= __vlan_add(vlan
, flags
, extack
);
1233 /* Must be protected by RTNL.
1234 * Must be called with vid in range from 1 to 4094 inclusive.
1236 int nbp_vlan_delete(struct net_bridge_port
*port
, u16 vid
)
1238 struct net_bridge_vlan
*v
;
1242 v
= br_vlan_find(nbp_vlan_group(port
), vid
);
1245 br_fdb_find_delete_local(port
->br
, port
, port
->dev
->dev_addr
, vid
);
1246 br_fdb_delete_by_port(port
->br
, port
, vid
, 0);
1248 return __vlan_del(v
);
1251 void nbp_vlan_flush(struct net_bridge_port
*port
)
1253 struct net_bridge_vlan_group
*vg
;
1257 vg
= nbp_vlan_group(port
);
1258 __vlan_flush(port
->br
, port
, vg
);
1259 RCU_INIT_POINTER(port
->vlgrp
, NULL
);
1261 __vlan_group_free(vg
);
1264 void br_vlan_get_stats(const struct net_bridge_vlan
*v
,
1265 struct br_vlan_stats
*stats
)
1269 memset(stats
, 0, sizeof(*stats
));
1270 for_each_possible_cpu(i
) {
1271 u64 rxpackets
, rxbytes
, txpackets
, txbytes
;
1272 struct br_vlan_stats
*cpu_stats
;
1275 cpu_stats
= per_cpu_ptr(v
->stats
, i
);
1277 start
= u64_stats_fetch_begin_irq(&cpu_stats
->syncp
);
1278 rxpackets
= cpu_stats
->rx_packets
;
1279 rxbytes
= cpu_stats
->rx_bytes
;
1280 txbytes
= cpu_stats
->tx_bytes
;
1281 txpackets
= cpu_stats
->tx_packets
;
1282 } while (u64_stats_fetch_retry_irq(&cpu_stats
->syncp
, start
));
1284 stats
->rx_packets
+= rxpackets
;
1285 stats
->rx_bytes
+= rxbytes
;
1286 stats
->tx_bytes
+= txbytes
;
1287 stats
->tx_packets
+= txpackets
;
1291 static int __br_vlan_get_pvid(const struct net_device
*dev
,
1292 struct net_bridge_port
*p
, u16
*p_pvid
)
1294 struct net_bridge_vlan_group
*vg
;
1297 vg
= nbp_vlan_group(p
);
1298 else if (netif_is_bridge_master(dev
))
1299 vg
= br_vlan_group(netdev_priv(dev
));
1303 *p_pvid
= br_get_pvid(vg
);
1307 int br_vlan_get_pvid(const struct net_device
*dev
, u16
*p_pvid
)
1311 return __br_vlan_get_pvid(dev
, br_port_get_check_rtnl(dev
), p_pvid
);
1313 EXPORT_SYMBOL_GPL(br_vlan_get_pvid
);
1315 int br_vlan_get_pvid_rcu(const struct net_device
*dev
, u16
*p_pvid
)
1317 return __br_vlan_get_pvid(dev
, br_port_get_check_rcu(dev
), p_pvid
);
1319 EXPORT_SYMBOL_GPL(br_vlan_get_pvid_rcu
);
1321 int br_vlan_get_info(const struct net_device
*dev
, u16 vid
,
1322 struct bridge_vlan_info
*p_vinfo
)
1324 struct net_bridge_vlan_group
*vg
;
1325 struct net_bridge_vlan
*v
;
1326 struct net_bridge_port
*p
;
1329 p
= br_port_get_check_rtnl(dev
);
1331 vg
= nbp_vlan_group(p
);
1332 else if (netif_is_bridge_master(dev
))
1333 vg
= br_vlan_group(netdev_priv(dev
));
1337 v
= br_vlan_find(vg
, vid
);
1342 p_vinfo
->flags
= v
->flags
;
1343 if (vid
== br_get_pvid(vg
))
1344 p_vinfo
->flags
|= BRIDGE_VLAN_INFO_PVID
;
1347 EXPORT_SYMBOL_GPL(br_vlan_get_info
);
1349 static int br_vlan_is_bind_vlan_dev(const struct net_device
*dev
)
1351 return is_vlan_dev(dev
) &&
1352 !!(vlan_dev_priv(dev
)->flags
& VLAN_FLAG_BRIDGE_BINDING
);
1355 static int br_vlan_is_bind_vlan_dev_fn(struct net_device
*dev
,
1356 __always_unused
void *data
)
1358 return br_vlan_is_bind_vlan_dev(dev
);
1361 static bool br_vlan_has_upper_bind_vlan_dev(struct net_device
*dev
)
1366 found
= netdev_walk_all_upper_dev_rcu(dev
, br_vlan_is_bind_vlan_dev_fn
,
1373 struct br_vlan_bind_walk_data
{
1375 struct net_device
*result
;
1378 static int br_vlan_match_bind_vlan_dev_fn(struct net_device
*dev
,
1381 struct br_vlan_bind_walk_data
*data
= data_in
;
1384 if (br_vlan_is_bind_vlan_dev(dev
) &&
1385 vlan_dev_priv(dev
)->vlan_id
== data
->vid
) {
1393 static struct net_device
*
1394 br_vlan_get_upper_bind_vlan_dev(struct net_device
*dev
, u16 vid
)
1396 struct br_vlan_bind_walk_data data
= {
1401 netdev_walk_all_upper_dev_rcu(dev
, br_vlan_match_bind_vlan_dev_fn
,
1408 static bool br_vlan_is_dev_up(const struct net_device
*dev
)
1410 return !!(dev
->flags
& IFF_UP
) && netif_oper_up(dev
);
1413 static void br_vlan_set_vlan_dev_state(const struct net_bridge
*br
,
1414 struct net_device
*vlan_dev
)
1416 u16 vid
= vlan_dev_priv(vlan_dev
)->vlan_id
;
1417 struct net_bridge_vlan_group
*vg
;
1418 struct net_bridge_port
*p
;
1419 bool has_carrier
= false;
1421 if (!netif_carrier_ok(br
->dev
)) {
1422 netif_carrier_off(vlan_dev
);
1426 list_for_each_entry(p
, &br
->port_list
, list
) {
1427 vg
= nbp_vlan_group(p
);
1428 if (br_vlan_find(vg
, vid
) && br_vlan_is_dev_up(p
->dev
)) {
1435 netif_carrier_on(vlan_dev
);
1437 netif_carrier_off(vlan_dev
);
1440 static void br_vlan_set_all_vlan_dev_state(struct net_bridge_port
*p
)
1442 struct net_bridge_vlan_group
*vg
= nbp_vlan_group(p
);
1443 struct net_bridge_vlan
*vlan
;
1444 struct net_device
*vlan_dev
;
1446 list_for_each_entry(vlan
, &vg
->vlan_list
, vlist
) {
1447 vlan_dev
= br_vlan_get_upper_bind_vlan_dev(p
->br
->dev
,
1450 if (br_vlan_is_dev_up(p
->dev
)) {
1451 if (netif_carrier_ok(p
->br
->dev
))
1452 netif_carrier_on(vlan_dev
);
1454 br_vlan_set_vlan_dev_state(p
->br
, vlan_dev
);
1460 static void br_vlan_upper_change(struct net_device
*dev
,
1461 struct net_device
*upper_dev
,
1464 struct net_bridge
*br
= netdev_priv(dev
);
1466 if (!br_vlan_is_bind_vlan_dev(upper_dev
))
1470 br_vlan_set_vlan_dev_state(br
, upper_dev
);
1471 br_opt_toggle(br
, BROPT_VLAN_BRIDGE_BINDING
, true);
1473 br_opt_toggle(br
, BROPT_VLAN_BRIDGE_BINDING
,
1474 br_vlan_has_upper_bind_vlan_dev(dev
));
1478 struct br_vlan_link_state_walk_data
{
1479 struct net_bridge
*br
;
1482 static int br_vlan_link_state_change_fn(struct net_device
*vlan_dev
,
1485 struct br_vlan_link_state_walk_data
*data
= data_in
;
1487 if (br_vlan_is_bind_vlan_dev(vlan_dev
))
1488 br_vlan_set_vlan_dev_state(data
->br
, vlan_dev
);
1493 static void br_vlan_link_state_change(struct net_device
*dev
,
1494 struct net_bridge
*br
)
1496 struct br_vlan_link_state_walk_data data
= {
1501 netdev_walk_all_upper_dev_rcu(dev
, br_vlan_link_state_change_fn
,
1506 /* Must be protected by RTNL. */
1507 static void nbp_vlan_set_vlan_dev_state(struct net_bridge_port
*p
, u16 vid
)
1509 struct net_device
*vlan_dev
;
1511 if (!br_opt_get(p
->br
, BROPT_VLAN_BRIDGE_BINDING
))
1514 vlan_dev
= br_vlan_get_upper_bind_vlan_dev(p
->br
->dev
, vid
);
1516 br_vlan_set_vlan_dev_state(p
->br
, vlan_dev
);
1519 /* Must be protected by RTNL. */
1520 int br_vlan_bridge_event(struct net_device
*dev
, unsigned long event
, void *ptr
)
1522 struct netdev_notifier_changeupper_info
*info
;
1523 struct net_bridge
*br
= netdev_priv(dev
);
1524 int vlcmd
= 0, ret
= 0;
1525 bool changed
= false;
1528 case NETDEV_REGISTER
:
1529 ret
= br_vlan_add(br
, br
->default_pvid
,
1530 BRIDGE_VLAN_INFO_PVID
|
1531 BRIDGE_VLAN_INFO_UNTAGGED
|
1532 BRIDGE_VLAN_INFO_BRENTRY
, &changed
, NULL
);
1533 vlcmd
= RTM_NEWVLAN
;
1535 case NETDEV_UNREGISTER
:
1536 changed
= !br_vlan_delete(br
, br
->default_pvid
);
1537 vlcmd
= RTM_DELVLAN
;
1539 case NETDEV_CHANGEUPPER
:
1541 br_vlan_upper_change(dev
, info
->upper_dev
, info
->linking
);
1546 if (!br_opt_get(br
, BROPT_VLAN_BRIDGE_BINDING
))
1548 br_vlan_link_state_change(dev
, br
);
1552 br_vlan_notify(br
, NULL
, br
->default_pvid
, 0, vlcmd
);
1557 /* Must be protected by RTNL. */
1558 void br_vlan_port_event(struct net_bridge_port
*p
, unsigned long event
)
1560 if (!br_opt_get(p
->br
, BROPT_VLAN_BRIDGE_BINDING
))
1567 br_vlan_set_all_vlan_dev_state(p
);
1572 /* v_opts is used to dump the options which must be equal in the whole range */
1573 static bool br_vlan_fill_vids(struct sk_buff
*skb
, u16 vid
, u16 vid_range
,
1574 const struct net_bridge_vlan
*v_opts
,
1577 struct bridge_vlan_info info
;
1578 struct nlattr
*nest
;
1580 nest
= nla_nest_start(skb
, BRIDGE_VLANDB_ENTRY
);
1584 memset(&info
, 0, sizeof(info
));
1586 if (flags
& BRIDGE_VLAN_INFO_UNTAGGED
)
1587 info
.flags
|= BRIDGE_VLAN_INFO_UNTAGGED
;
1588 if (flags
& BRIDGE_VLAN_INFO_PVID
)
1589 info
.flags
|= BRIDGE_VLAN_INFO_PVID
;
1591 if (nla_put(skb
, BRIDGE_VLANDB_ENTRY_INFO
, sizeof(info
), &info
))
1594 if (vid_range
&& vid
< vid_range
&&
1595 !(flags
& BRIDGE_VLAN_INFO_PVID
) &&
1596 nla_put_u16(skb
, BRIDGE_VLANDB_ENTRY_RANGE
, vid_range
))
1599 if (v_opts
&& !br_vlan_opts_fill(skb
, v_opts
))
1602 nla_nest_end(skb
, nest
);
1607 nla_nest_cancel(skb
, nest
);
1611 static size_t rtnl_vlan_nlmsg_size(void)
1613 return NLMSG_ALIGN(sizeof(struct br_vlan_msg
))
1614 + nla_total_size(0) /* BRIDGE_VLANDB_ENTRY */
1615 + nla_total_size(sizeof(u16
)) /* BRIDGE_VLANDB_ENTRY_RANGE */
1616 + nla_total_size(sizeof(struct bridge_vlan_info
)) /* BRIDGE_VLANDB_ENTRY_INFO */
1617 + br_vlan_opts_nl_size(); /* bridge vlan options */
1620 void br_vlan_notify(const struct net_bridge
*br
,
1621 const struct net_bridge_port
*p
,
1622 u16 vid
, u16 vid_range
,
1625 struct net_bridge_vlan_group
*vg
;
1626 struct net_bridge_vlan
*v
= NULL
;
1627 struct br_vlan_msg
*bvm
;
1628 struct nlmsghdr
*nlh
;
1629 struct sk_buff
*skb
;
1635 /* right now notifications are done only with rtnl held */
1639 ifindex
= p
->dev
->ifindex
;
1640 vg
= nbp_vlan_group(p
);
1641 net
= dev_net(p
->dev
);
1643 ifindex
= br
->dev
->ifindex
;
1644 vg
= br_vlan_group(br
);
1645 net
= dev_net(br
->dev
);
1648 skb
= nlmsg_new(rtnl_vlan_nlmsg_size(), GFP_KERNEL
);
1653 nlh
= nlmsg_put(skb
, 0, 0, cmd
, sizeof(*bvm
), 0);
1656 bvm
= nlmsg_data(nlh
);
1657 memset(bvm
, 0, sizeof(*bvm
));
1658 bvm
->family
= AF_BRIDGE
;
1659 bvm
->ifindex
= ifindex
;
1663 /* need to find the vlan due to flags/options */
1664 v
= br_vlan_find(vg
, vid
);
1665 if (!v
|| !br_vlan_should_use(v
))
1669 if (br_get_pvid(vg
) == v
->vid
)
1670 flags
|= BRIDGE_VLAN_INFO_PVID
;
1678 if (!br_vlan_fill_vids(skb
, vid
, vid_range
, v
, flags
))
1681 nlmsg_end(skb
, nlh
);
1682 rtnl_notify(skb
, net
, 0, RTNLGRP_BRVLAN
, NULL
, GFP_KERNEL
);
1686 rtnl_set_sk_err(net
, RTNLGRP_BRVLAN
, err
);
1691 /* check if v_curr can enter a range ending in range_end */
1692 bool br_vlan_can_enter_range(const struct net_bridge_vlan
*v_curr
,
1693 const struct net_bridge_vlan
*range_end
)
1695 return v_curr
->vid
- range_end
->vid
== 1 &&
1696 range_end
->flags
== v_curr
->flags
&&
1697 br_vlan_opts_eq(v_curr
, range_end
);
1700 static int br_vlan_dump_dev(const struct net_device
*dev
,
1701 struct sk_buff
*skb
,
1702 struct netlink_callback
*cb
)
1704 struct net_bridge_vlan
*v
, *range_start
= NULL
, *range_end
= NULL
;
1705 struct net_bridge_vlan_group
*vg
;
1706 int idx
= 0, s_idx
= cb
->args
[1];
1707 struct nlmsghdr
*nlh
= NULL
;
1708 struct net_bridge_port
*p
;
1709 struct br_vlan_msg
*bvm
;
1710 struct net_bridge
*br
;
1714 if (!netif_is_bridge_master(dev
) && !netif_is_bridge_port(dev
))
1717 if (netif_is_bridge_master(dev
)) {
1718 br
= netdev_priv(dev
);
1719 vg
= br_vlan_group_rcu(br
);
1722 p
= br_port_get_rcu(dev
);
1725 vg
= nbp_vlan_group_rcu(p
);
1732 nlh
= nlmsg_put(skb
, NETLINK_CB(cb
->skb
).portid
, cb
->nlh
->nlmsg_seq
,
1733 RTM_NEWVLAN
, sizeof(*bvm
), NLM_F_MULTI
);
1736 bvm
= nlmsg_data(nlh
);
1737 memset(bvm
, 0, sizeof(*bvm
));
1738 bvm
->family
= PF_BRIDGE
;
1739 bvm
->ifindex
= dev
->ifindex
;
1740 pvid
= br_get_pvid(vg
);
1742 /* idx must stay at range's beginning until it is filled in */
1743 list_for_each_entry_rcu(v
, &vg
->vlan_list
, vlist
) {
1744 if (!br_vlan_should_use(v
))
1757 if (v
->vid
== pvid
|| !br_vlan_can_enter_range(v
, range_end
)) {
1758 u16 flags
= br_vlan_flags(range_start
, pvid
);
1760 if (!br_vlan_fill_vids(skb
, range_start
->vid
,
1761 range_end
->vid
, range_start
,
1766 /* advance number of filled vlans */
1767 idx
+= range_end
->vid
- range_start
->vid
+ 1;
1774 /* err will be 0 and range_start will be set in 3 cases here:
1775 * - first vlan (range_start == range_end)
1776 * - last vlan (range_start == range_end, not in range)
1777 * - last vlan range (range_start != range_end, in range)
1779 if (!err
&& range_start
&&
1780 !br_vlan_fill_vids(skb
, range_start
->vid
, range_end
->vid
,
1781 range_start
, br_vlan_flags(range_start
, pvid
)))
1784 cb
->args
[1] = err
? idx
: 0;
1786 nlmsg_end(skb
, nlh
);
1791 static int br_vlan_rtm_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1793 int idx
= 0, err
= 0, s_idx
= cb
->args
[0];
1794 struct net
*net
= sock_net(skb
->sk
);
1795 struct br_vlan_msg
*bvm
;
1796 struct net_device
*dev
;
1798 err
= nlmsg_parse(cb
->nlh
, sizeof(*bvm
), NULL
, 0, NULL
, cb
->extack
);
1802 bvm
= nlmsg_data(cb
->nlh
);
1806 dev
= dev_get_by_index_rcu(net
, bvm
->ifindex
);
1811 err
= br_vlan_dump_dev(dev
, skb
, cb
);
1812 if (err
&& err
!= -EMSGSIZE
)
1815 for_each_netdev_rcu(net
, dev
) {
1819 err
= br_vlan_dump_dev(dev
, skb
, cb
);
1820 if (err
== -EMSGSIZE
)
1837 static const struct nla_policy br_vlan_db_policy
[BRIDGE_VLANDB_ENTRY_MAX
+ 1] = {
1838 [BRIDGE_VLANDB_ENTRY_INFO
] = { .type
= NLA_EXACT_LEN
,
1839 .len
= sizeof(struct bridge_vlan_info
) },
1840 [BRIDGE_VLANDB_ENTRY_RANGE
] = { .type
= NLA_U16
},
1841 [BRIDGE_VLANDB_ENTRY_STATE
] = { .type
= NLA_U8
},
1844 static int br_vlan_rtm_process_one(struct net_device
*dev
,
1845 const struct nlattr
*attr
,
1846 int cmd
, struct netlink_ext_ack
*extack
)
1848 struct bridge_vlan_info
*vinfo
, vrange_end
, *vinfo_last
= NULL
;
1849 struct nlattr
*tb
[BRIDGE_VLANDB_ENTRY_MAX
+ 1];
1850 bool changed
= false, skip_processing
= false;
1851 struct net_bridge_vlan_group
*vg
;
1852 struct net_bridge_port
*p
= NULL
;
1853 int err
= 0, cmdmap
= 0;
1854 struct net_bridge
*br
;
1856 if (netif_is_bridge_master(dev
)) {
1857 br
= netdev_priv(dev
);
1858 vg
= br_vlan_group(br
);
1860 p
= br_port_get_rtnl(dev
);
1864 vg
= nbp_vlan_group(p
);
1870 err
= nla_parse_nested(tb
, BRIDGE_VLANDB_ENTRY_MAX
, attr
,
1871 br_vlan_db_policy
, extack
);
1875 if (!tb
[BRIDGE_VLANDB_ENTRY_INFO
]) {
1876 NL_SET_ERR_MSG_MOD(extack
, "Missing vlan entry info");
1879 memset(&vrange_end
, 0, sizeof(vrange_end
));
1881 vinfo
= nla_data(tb
[BRIDGE_VLANDB_ENTRY_INFO
]);
1882 if (vinfo
->flags
& (BRIDGE_VLAN_INFO_RANGE_BEGIN
|
1883 BRIDGE_VLAN_INFO_RANGE_END
)) {
1884 NL_SET_ERR_MSG_MOD(extack
, "Old-style vlan ranges are not allowed when using RTM vlan calls");
1887 if (!br_vlan_valid_id(vinfo
->vid
, extack
))
1890 if (tb
[BRIDGE_VLANDB_ENTRY_RANGE
]) {
1891 vrange_end
.vid
= nla_get_u16(tb
[BRIDGE_VLANDB_ENTRY_RANGE
]);
1892 /* validate user-provided flags without RANGE_BEGIN */
1893 vrange_end
.flags
= BRIDGE_VLAN_INFO_RANGE_END
| vinfo
->flags
;
1894 vinfo
->flags
|= BRIDGE_VLAN_INFO_RANGE_BEGIN
;
1896 /* vinfo_last is the range start, vinfo the range end */
1898 vinfo
= &vrange_end
;
1900 if (!br_vlan_valid_id(vinfo
->vid
, extack
) ||
1901 !br_vlan_valid_range(vinfo
, vinfo_last
, extack
))
1907 cmdmap
= RTM_SETLINK
;
1908 skip_processing
= !!(vinfo
->flags
& BRIDGE_VLAN_INFO_ONLY_OPTS
);
1911 cmdmap
= RTM_DELLINK
;
1915 if (!skip_processing
) {
1916 struct bridge_vlan_info
*tmp_last
= vinfo_last
;
1918 /* br_process_vlan_info may overwrite vinfo_last */
1919 err
= br_process_vlan_info(br
, p
, cmdmap
, vinfo
, &tmp_last
,
1922 /* notify first if anything changed */
1924 br_ifinfo_notify(cmdmap
, br
, p
);
1930 /* deal with options */
1931 if (cmd
== RTM_NEWVLAN
) {
1932 struct net_bridge_vlan
*range_start
, *range_end
;
1935 range_start
= br_vlan_find(vg
, vinfo_last
->vid
);
1936 range_end
= br_vlan_find(vg
, vinfo
->vid
);
1938 range_start
= br_vlan_find(vg
, vinfo
->vid
);
1939 range_end
= range_start
;
1942 err
= br_vlan_process_options(br
, p
, range_start
, range_end
,
1949 static int br_vlan_rtm_process(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
1950 struct netlink_ext_ack
*extack
)
1952 struct net
*net
= sock_net(skb
->sk
);
1953 struct br_vlan_msg
*bvm
;
1954 struct net_device
*dev
;
1955 struct nlattr
*attr
;
1959 /* this should validate the header and check for remaining bytes */
1960 err
= nlmsg_parse(nlh
, sizeof(*bvm
), NULL
, BRIDGE_VLANDB_MAX
, NULL
,
1965 bvm
= nlmsg_data(nlh
);
1966 dev
= __dev_get_by_index(net
, bvm
->ifindex
);
1970 if (!netif_is_bridge_master(dev
) && !netif_is_bridge_port(dev
)) {
1971 NL_SET_ERR_MSG_MOD(extack
, "The device is not a valid bridge or bridge port");
1975 nlmsg_for_each_attr(attr
, nlh
, sizeof(*bvm
), rem
) {
1976 if (nla_type(attr
) != BRIDGE_VLANDB_ENTRY
)
1980 err
= br_vlan_rtm_process_one(dev
, attr
, nlh
->nlmsg_type
,
1986 NL_SET_ERR_MSG_MOD(extack
, "No vlans found to process");
1993 void br_vlan_rtnl_init(void)
1995 rtnl_register_module(THIS_MODULE
, PF_BRIDGE
, RTM_GETVLAN
, NULL
,
1996 br_vlan_rtm_dump
, 0);
1997 rtnl_register_module(THIS_MODULE
, PF_BRIDGE
, RTM_NEWVLAN
,
1998 br_vlan_rtm_process
, NULL
, 0);
1999 rtnl_register_module(THIS_MODULE
, PF_BRIDGE
, RTM_DELVLAN
,
2000 br_vlan_rtm_process
, NULL
, 0);
2003 void br_vlan_rtnl_uninit(void)
2005 rtnl_unregister(PF_BRIDGE
, RTM_GETVLAN
);
2006 rtnl_unregister(PF_BRIDGE
, RTM_NEWVLAN
);
2007 rtnl_unregister(PF_BRIDGE
, RTM_DELVLAN
);