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 void __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
);
48 static void __vlan_delete_pvid(struct net_bridge_vlan_group
*vg
, u16 vid
)
57 /* Update the BRIDGE_VLAN_INFO_PVID and BRIDGE_VLAN_INFO_UNTAGGED flags of @v.
58 * If @commit is false, return just whether the BRIDGE_VLAN_INFO_PVID and
59 * BRIDGE_VLAN_INFO_UNTAGGED bits of @flags would produce any change onto @v.
61 static bool __vlan_flags_update(struct net_bridge_vlan
*v
, u16 flags
,
64 struct net_bridge_vlan_group
*vg
;
67 if (br_vlan_is_master(v
))
68 vg
= br_vlan_group(v
->br
);
70 vg
= nbp_vlan_group(v
->port
);
72 /* check if anything would be changed on commit */
73 change
= !!(flags
& BRIDGE_VLAN_INFO_PVID
) == !!(vg
->pvid
!= v
->vid
) ||
74 ((flags
^ v
->flags
) & BRIDGE_VLAN_INFO_UNTAGGED
);
79 if (flags
& BRIDGE_VLAN_INFO_PVID
)
80 __vlan_add_pvid(vg
, v
);
82 __vlan_delete_pvid(vg
, v
->vid
);
84 if (flags
& BRIDGE_VLAN_INFO_UNTAGGED
)
85 v
->flags
|= BRIDGE_VLAN_INFO_UNTAGGED
;
87 v
->flags
&= ~BRIDGE_VLAN_INFO_UNTAGGED
;
93 static bool __vlan_flags_would_change(struct net_bridge_vlan
*v
, u16 flags
)
95 return __vlan_flags_update(v
, flags
, false);
98 static void __vlan_flags_commit(struct net_bridge_vlan
*v
, u16 flags
)
100 __vlan_flags_update(v
, flags
, true);
103 static int __vlan_vid_add(struct net_device
*dev
, struct net_bridge
*br
,
104 struct net_bridge_vlan
*v
, u16 flags
,
105 struct netlink_ext_ack
*extack
)
109 /* Try switchdev op first. In case it is not supported, fallback to
112 err
= br_switchdev_port_vlan_add(dev
, v
->vid
, flags
, false, extack
);
113 if (err
== -EOPNOTSUPP
)
114 return vlan_vid_add(dev
, br
->vlan_proto
, v
->vid
);
115 v
->priv_flags
|= BR_VLFLAG_ADDED_BY_SWITCHDEV
;
119 static void __vlan_add_list(struct net_bridge_vlan
*v
)
121 struct net_bridge_vlan_group
*vg
;
122 struct list_head
*headp
, *hpos
;
123 struct net_bridge_vlan
*vent
;
125 if (br_vlan_is_master(v
))
126 vg
= br_vlan_group(v
->br
);
128 vg
= nbp_vlan_group(v
->port
);
130 headp
= &vg
->vlan_list
;
131 list_for_each_prev(hpos
, headp
) {
132 vent
= list_entry(hpos
, struct net_bridge_vlan
, vlist
);
133 if (v
->vid
>= vent
->vid
)
136 list_add_rcu(&v
->vlist
, hpos
);
139 static void __vlan_del_list(struct net_bridge_vlan
*v
)
141 list_del_rcu(&v
->vlist
);
144 static int __vlan_vid_del(struct net_device
*dev
, struct net_bridge
*br
,
145 const struct net_bridge_vlan
*v
)
149 /* Try switchdev op first. In case it is not supported, fallback to
152 err
= br_switchdev_port_vlan_del(dev
, v
->vid
);
153 if (!(v
->priv_flags
& BR_VLFLAG_ADDED_BY_SWITCHDEV
))
154 vlan_vid_del(dev
, br
->vlan_proto
, v
->vid
);
155 return err
== -EOPNOTSUPP
? 0 : err
;
158 /* Returns a master vlan, if it didn't exist it gets created. In all cases
159 * a reference is taken to the master vlan before returning.
161 static struct net_bridge_vlan
*
162 br_vlan_get_master(struct net_bridge
*br
, u16 vid
,
163 struct netlink_ext_ack
*extack
)
165 struct net_bridge_vlan_group
*vg
;
166 struct net_bridge_vlan
*masterv
;
168 vg
= br_vlan_group(br
);
169 masterv
= br_vlan_find(vg
, vid
);
173 /* missing global ctx, create it now */
174 if (br_vlan_add(br
, vid
, 0, &changed
, extack
))
176 masterv
= br_vlan_find(vg
, vid
);
177 if (WARN_ON(!masterv
))
179 refcount_set(&masterv
->refcnt
, 1);
182 refcount_inc(&masterv
->refcnt
);
187 static void br_master_vlan_rcu_free(struct rcu_head
*rcu
)
189 struct net_bridge_vlan
*v
;
191 v
= container_of(rcu
, struct net_bridge_vlan
, rcu
);
192 WARN_ON(!br_vlan_is_master(v
));
193 free_percpu(v
->stats
);
198 static void br_vlan_put_master(struct net_bridge_vlan
*masterv
)
200 struct net_bridge_vlan_group
*vg
;
202 if (!br_vlan_is_master(masterv
))
205 vg
= br_vlan_group(masterv
->br
);
206 if (refcount_dec_and_test(&masterv
->refcnt
)) {
207 rhashtable_remove_fast(&vg
->vlan_hash
,
208 &masterv
->vnode
, br_vlan_rht_params
);
209 __vlan_del_list(masterv
);
210 br_multicast_toggle_one_vlan(masterv
, false);
211 br_multicast_ctx_deinit(&masterv
->br_mcast_ctx
);
212 call_rcu(&masterv
->rcu
, br_master_vlan_rcu_free
);
216 static void nbp_vlan_rcu_free(struct rcu_head
*rcu
)
218 struct net_bridge_vlan
*v
;
220 v
= container_of(rcu
, struct net_bridge_vlan
, rcu
);
221 WARN_ON(br_vlan_is_master(v
));
222 /* if we had per-port stats configured then free them here */
223 if (v
->priv_flags
& BR_VLFLAG_PER_PORT_STATS
)
224 free_percpu(v
->stats
);
229 static void br_vlan_init_state(struct net_bridge_vlan
*v
)
231 struct net_bridge
*br
;
233 if (br_vlan_is_master(v
))
238 if (br_opt_get(br
, BROPT_MST_ENABLED
)) {
239 br_mst_vlan_init_state(v
);
243 v
->state
= BR_STATE_FORWARDING
;
247 /* This is the shared VLAN add function which works for both ports and bridge
248 * devices. There are four possible calls to this function in terms of the
250 * 1. vlan is being added on a port (no master flags, global entry exists)
251 * 2. vlan is being added on a bridge (both master and brentry flags)
252 * 3. vlan is being added on a port, but a global entry didn't exist which
253 * is being created right now (master flag set, brentry flag unset), the
254 * global entry is used for global per-vlan features, but not for filtering
255 * 4. same as 3 but with both master and brentry flags set so the entry
256 * will be used for filtering in both the port and the bridge
258 static int __vlan_add(struct net_bridge_vlan
*v
, u16 flags
,
259 struct netlink_ext_ack
*extack
)
261 struct net_bridge_vlan
*masterv
= NULL
;
262 struct net_bridge_port
*p
= NULL
;
263 struct net_bridge_vlan_group
*vg
;
264 struct net_device
*dev
;
265 struct net_bridge
*br
;
268 if (br_vlan_is_master(v
)) {
271 vg
= br_vlan_group(br
);
276 vg
= nbp_vlan_group(p
);
280 /* Add VLAN to the device filter if it is supported.
281 * This ensures tagged traffic enters the bridge when
282 * promiscuous mode is disabled by br_manage_promisc().
284 err
= __vlan_vid_add(dev
, br
, v
, flags
, extack
);
288 /* need to work on the master vlan too */
289 if (flags
& BRIDGE_VLAN_INFO_MASTER
) {
292 err
= br_vlan_add(br
, v
->vid
,
293 flags
| BRIDGE_VLAN_INFO_BRENTRY
,
299 br_vlan_notify(br
, NULL
, v
->vid
, 0,
303 masterv
= br_vlan_get_master(br
, v
->vid
, extack
);
309 if (br_opt_get(br
, BROPT_VLAN_STATS_PER_PORT
)) {
311 netdev_alloc_pcpu_stats(struct pcpu_sw_netstats
);
316 v
->priv_flags
|= BR_VLFLAG_PER_PORT_STATS
;
318 v
->stats
= masterv
->stats
;
320 br_multicast_port_ctx_init(p
, v
, &v
->port_mcast_ctx
);
322 if (br_vlan_should_use(v
)) {
323 err
= br_switchdev_port_vlan_add(dev
, v
->vid
, flags
,
325 if (err
&& err
!= -EOPNOTSUPP
)
328 br_multicast_ctx_init(br
, v
, &v
->br_mcast_ctx
);
329 v
->priv_flags
|= BR_VLFLAG_GLOBAL_MCAST_ENABLED
;
332 /* Add the dev mac and count the vlan only if it's usable */
333 if (br_vlan_should_use(v
)) {
334 err
= br_fdb_add_local(br
, p
, dev
->dev_addr
, v
->vid
);
336 br_err(br
, "failed insert local address into bridge forwarding table\n");
342 /* set the state before publishing */
343 br_vlan_init_state(v
);
345 err
= rhashtable_lookup_insert_fast(&vg
->vlan_hash
, &v
->vnode
,
351 __vlan_flags_commit(v
, flags
);
352 br_multicast_toggle_one_vlan(v
, true);
355 nbp_vlan_set_vlan_dev_state(p
, v
->vid
);
360 if (br_vlan_should_use(v
)) {
361 br_fdb_find_delete_local(br
, p
, dev
->dev_addr
, v
->vid
);
367 __vlan_vid_del(dev
, br
, v
);
369 if (v
->stats
&& masterv
->stats
!= v
->stats
)
370 free_percpu(v
->stats
);
373 br_vlan_put_master(masterv
);
377 br_switchdev_port_vlan_del(dev
, v
->vid
);
383 static int __vlan_del(struct net_bridge_vlan
*v
)
385 struct net_bridge_vlan
*masterv
= v
;
386 struct net_bridge_vlan_group
*vg
;
387 struct net_bridge_port
*p
= NULL
;
390 if (br_vlan_is_master(v
)) {
391 vg
= br_vlan_group(v
->br
);
394 vg
= nbp_vlan_group(v
->port
);
398 __vlan_delete_pvid(vg
, v
->vid
);
400 err
= __vlan_vid_del(p
->dev
, p
->br
, v
);
404 err
= br_switchdev_port_vlan_del(v
->br
->dev
, v
->vid
);
405 if (err
&& err
!= -EOPNOTSUPP
)
410 if (br_vlan_should_use(v
)) {
411 v
->flags
&= ~BRIDGE_VLAN_INFO_BRENTRY
;
416 vlan_tunnel_info_del(vg
, v
);
417 rhashtable_remove_fast(&vg
->vlan_hash
, &v
->vnode
,
420 nbp_vlan_set_vlan_dev_state(p
, v
->vid
);
421 br_multicast_toggle_one_vlan(v
, false);
422 br_multicast_port_ctx_deinit(&v
->port_mcast_ctx
);
423 call_rcu(&v
->rcu
, nbp_vlan_rcu_free
);
426 br_vlan_put_master(masterv
);
431 static void __vlan_group_free(struct net_bridge_vlan_group
*vg
)
433 WARN_ON(!list_empty(&vg
->vlan_list
));
434 rhashtable_destroy(&vg
->vlan_hash
);
435 vlan_tunnel_deinit(vg
);
439 static void __vlan_flush(const struct net_bridge
*br
,
440 const struct net_bridge_port
*p
,
441 struct net_bridge_vlan_group
*vg
)
443 struct net_bridge_vlan
*vlan
, *tmp
;
444 u16 v_start
= 0, v_end
= 0;
447 __vlan_delete_pvid(vg
, vg
->pvid
);
448 list_for_each_entry_safe(vlan
, tmp
, &vg
->vlan_list
, vlist
) {
449 /* take care of disjoint ranges */
452 } else if (vlan
->vid
- v_end
!= 1) {
453 /* found range end, notify and start next one */
454 br_vlan_notify(br
, p
, v_start
, v_end
, RTM_DELVLAN
);
459 err
= __vlan_del(vlan
);
462 "port %u(%s) failed to delete vlan %d: %pe\n",
463 (unsigned int) p
->port_no
, p
->dev
->name
,
464 vlan
->vid
, ERR_PTR(err
));
468 /* notify about the last/whole vlan range */
470 br_vlan_notify(br
, p
, v_start
, v_end
, RTM_DELVLAN
);
473 struct sk_buff
*br_handle_vlan(struct net_bridge
*br
,
474 const struct net_bridge_port
*p
,
475 struct net_bridge_vlan_group
*vg
,
478 struct pcpu_sw_netstats
*stats
;
479 struct net_bridge_vlan
*v
;
482 /* If this packet was not filtered at input, let it pass */
483 if (!BR_INPUT_SKB_CB(skb
)->vlan_filtered
)
486 /* At this point, we know that the frame was filtered and contains
487 * a valid vlan id. If the vlan id has untagged flag set,
488 * send untagged; otherwise, send tagged.
490 br_vlan_get_tag(skb
, &vid
);
491 v
= br_vlan_find(vg
, vid
);
492 /* Vlan entry must be configured at this point. The
493 * only exception is the bridge is set in promisc mode and the
494 * packet is destined for the bridge device. In this case
495 * pass the packet as is.
497 if (!v
|| !br_vlan_should_use(v
)) {
498 if ((br
->dev
->flags
& IFF_PROMISC
) && skb
->dev
== br
->dev
) {
505 if (br_opt_get(br
, BROPT_VLAN_STATS_ENABLED
)) {
506 stats
= this_cpu_ptr(v
->stats
);
507 u64_stats_update_begin(&stats
->syncp
);
508 u64_stats_add(&stats
->tx_bytes
, skb
->len
);
509 u64_stats_inc(&stats
->tx_packets
);
510 u64_stats_update_end(&stats
->syncp
);
513 /* If the skb will be sent using forwarding offload, the assumption is
514 * that the switchdev will inject the packet into hardware together
515 * with the bridge VLAN, so that it can be forwarded according to that
516 * VLAN. The switchdev should deal with popping the VLAN header in
517 * hardware on each egress port as appropriate. So only strip the VLAN
518 * header if forwarding offload is not being used.
520 if (v
->flags
& BRIDGE_VLAN_INFO_UNTAGGED
&&
521 !br_switchdev_frame_uses_tx_fwd_offload(skb
))
522 __vlan_hwaccel_clear_tag(skb
);
524 if (p
&& (p
->flags
& BR_VLAN_TUNNEL
) &&
525 br_handle_egress_vlan_tunnel(skb
, v
)) {
533 /* Called under RCU */
534 static bool __allowed_ingress(const struct net_bridge
*br
,
535 struct net_bridge_vlan_group
*vg
,
536 struct sk_buff
*skb
, u16
*vid
,
538 struct net_bridge_vlan
**vlan
)
540 struct pcpu_sw_netstats
*stats
;
541 struct net_bridge_vlan
*v
;
544 BR_INPUT_SKB_CB(skb
)->vlan_filtered
= true;
545 /* If vlan tx offload is disabled on bridge device and frame was
546 * sent from vlan device on the bridge device, it does not have
547 * HW accelerated vlan tag.
549 if (unlikely(!skb_vlan_tag_present(skb
) &&
550 skb
->protocol
== br
->vlan_proto
)) {
551 skb
= skb_vlan_untag(skb
);
556 if (!br_vlan_get_tag(skb
, vid
)) {
558 if (skb
->vlan_proto
!= br
->vlan_proto
) {
559 /* Protocol-mismatch, empty out vlan_tci for new tag */
560 skb_push(skb
, ETH_HLEN
);
561 skb
= vlan_insert_tag_set_proto(skb
, skb
->vlan_proto
,
562 skb_vlan_tag_get(skb
));
566 skb_pull(skb
, ETH_HLEN
);
567 skb_reset_mac_len(skb
);
579 u16 pvid
= br_get_pvid(vg
);
581 /* Frame had a tag with VID 0 or did not have a tag.
582 * See if pvid is set on this port. That tells us which
583 * vlan untagged or priority-tagged traffic belongs to.
588 /* PVID is set on this port. Any untagged or priority-tagged
589 * ingress frame is considered to belong to this vlan.
593 /* Untagged Frame. */
594 __vlan_hwaccel_put_tag(skb
, br
->vlan_proto
, pvid
);
596 /* Priority-tagged Frame.
597 * At this point, we know that skb->vlan_tci VID
599 * We update only VID field and preserve PCP field.
601 skb
->vlan_tci
|= pvid
;
603 /* if snooping and stats are disabled we can avoid the lookup */
604 if (!br_opt_get(br
, BROPT_MCAST_VLAN_SNOOPING_ENABLED
) &&
605 !br_opt_get(br
, BROPT_VLAN_STATS_ENABLED
)) {
606 if (*state
== BR_STATE_FORWARDING
) {
607 *state
= br_vlan_get_pvid_state(vg
);
608 if (!br_vlan_state_allowed(*state
, true))
614 v
= br_vlan_find(vg
, *vid
);
615 if (!v
|| !br_vlan_should_use(v
))
618 if (*state
== BR_STATE_FORWARDING
) {
619 *state
= br_vlan_get_state(v
);
620 if (!br_vlan_state_allowed(*state
, true))
624 if (br_opt_get(br
, BROPT_VLAN_STATS_ENABLED
)) {
625 stats
= this_cpu_ptr(v
->stats
);
626 u64_stats_update_begin(&stats
->syncp
);
627 u64_stats_add(&stats
->rx_bytes
, skb
->len
);
628 u64_stats_inc(&stats
->rx_packets
);
629 u64_stats_update_end(&stats
->syncp
);
641 bool br_allowed_ingress(const struct net_bridge
*br
,
642 struct net_bridge_vlan_group
*vg
, struct sk_buff
*skb
,
644 struct net_bridge_vlan
**vlan
)
646 /* If VLAN filtering is disabled on the bridge, all packets are
650 if (!br_opt_get(br
, BROPT_VLAN_ENABLED
)) {
651 BR_INPUT_SKB_CB(skb
)->vlan_filtered
= false;
655 return __allowed_ingress(br
, vg
, skb
, vid
, state
, vlan
);
658 /* Called under RCU. */
659 bool br_allowed_egress(struct net_bridge_vlan_group
*vg
,
660 const struct sk_buff
*skb
)
662 const struct net_bridge_vlan
*v
;
665 /* If this packet was not filtered at input, let it pass */
666 if (!BR_INPUT_SKB_CB(skb
)->vlan_filtered
)
669 br_vlan_get_tag(skb
, &vid
);
670 v
= br_vlan_find(vg
, vid
);
671 if (v
&& br_vlan_should_use(v
) &&
672 br_vlan_state_allowed(br_vlan_get_state(v
), false))
678 /* Called under RCU */
679 bool br_should_learn(struct net_bridge_port
*p
, struct sk_buff
*skb
, u16
*vid
)
681 struct net_bridge_vlan_group
*vg
;
682 struct net_bridge
*br
= p
->br
;
683 struct net_bridge_vlan
*v
;
685 /* If filtering was disabled at input, let it pass. */
686 if (!br_opt_get(br
, BROPT_VLAN_ENABLED
))
689 vg
= nbp_vlan_group_rcu(p
);
690 if (!vg
|| !vg
->num_vlans
)
693 if (!br_vlan_get_tag(skb
, vid
) && skb
->vlan_proto
!= br
->vlan_proto
)
697 *vid
= br_get_pvid(vg
);
699 !br_vlan_state_allowed(br_vlan_get_pvid_state(vg
), true))
705 v
= br_vlan_find(vg
, *vid
);
706 if (v
&& br_vlan_state_allowed(br_vlan_get_state(v
), true))
712 static int br_vlan_add_existing(struct net_bridge
*br
,
713 struct net_bridge_vlan_group
*vg
,
714 struct net_bridge_vlan
*vlan
,
715 u16 flags
, bool *changed
,
716 struct netlink_ext_ack
*extack
)
718 bool would_change
= __vlan_flags_would_change(vlan
, flags
);
719 bool becomes_brentry
= false;
722 if (!br_vlan_is_brentry(vlan
)) {
723 /* Trying to change flags of non-existent bridge vlan */
724 if (!(flags
& BRIDGE_VLAN_INFO_BRENTRY
))
727 becomes_brentry
= true;
730 /* Master VLANs that aren't brentries weren't notified before,
731 * time to notify them now.
733 if (becomes_brentry
|| would_change
) {
734 err
= br_switchdev_port_vlan_add(br
->dev
, vlan
->vid
, flags
,
735 would_change
, extack
);
736 if (err
&& err
!= -EOPNOTSUPP
)
740 if (becomes_brentry
) {
741 /* It was only kept for port vlans, now make it real */
742 err
= br_fdb_add_local(br
, NULL
, br
->dev
->dev_addr
, vlan
->vid
);
744 br_err(br
, "failed to insert local address into bridge forwarding table\n");
748 refcount_inc(&vlan
->refcnt
);
749 vlan
->flags
|= BRIDGE_VLAN_INFO_BRENTRY
;
752 br_multicast_toggle_one_vlan(vlan
, true);
755 __vlan_flags_commit(vlan
, flags
);
762 br_switchdev_port_vlan_del(br
->dev
, vlan
->vid
);
766 /* Must be protected by RTNL.
767 * Must be called with vid in range from 1 to 4094 inclusive.
768 * changed must be true only if the vlan was created or updated
770 int br_vlan_add(struct net_bridge
*br
, u16 vid
, u16 flags
, bool *changed
,
771 struct netlink_ext_ack
*extack
)
773 struct net_bridge_vlan_group
*vg
;
774 struct net_bridge_vlan
*vlan
;
780 vg
= br_vlan_group(br
);
781 vlan
= br_vlan_find(vg
, vid
);
783 return br_vlan_add_existing(br
, vg
, vlan
, flags
, changed
,
786 vlan
= kzalloc(sizeof(*vlan
), GFP_KERNEL
);
790 vlan
->stats
= netdev_alloc_pcpu_stats(struct pcpu_sw_netstats
);
796 vlan
->flags
= flags
| BRIDGE_VLAN_INFO_MASTER
;
797 vlan
->flags
&= ~BRIDGE_VLAN_INFO_PVID
;
799 if (flags
& BRIDGE_VLAN_INFO_BRENTRY
)
800 refcount_set(&vlan
->refcnt
, 1);
801 ret
= __vlan_add(vlan
, flags
, extack
);
803 free_percpu(vlan
->stats
);
812 /* Must be protected by RTNL.
813 * Must be called with vid in range from 1 to 4094 inclusive.
815 int br_vlan_delete(struct net_bridge
*br
, u16 vid
)
817 struct net_bridge_vlan_group
*vg
;
818 struct net_bridge_vlan
*v
;
822 vg
= br_vlan_group(br
);
823 v
= br_vlan_find(vg
, vid
);
824 if (!v
|| !br_vlan_is_brentry(v
))
827 br_fdb_find_delete_local(br
, NULL
, br
->dev
->dev_addr
, vid
);
828 br_fdb_delete_by_port(br
, NULL
, vid
, 0);
830 vlan_tunnel_info_del(vg
, v
);
832 return __vlan_del(v
);
835 void br_vlan_flush(struct net_bridge
*br
)
837 struct net_bridge_vlan_group
*vg
;
841 vg
= br_vlan_group(br
);
842 __vlan_flush(br
, NULL
, vg
);
843 RCU_INIT_POINTER(br
->vlgrp
, NULL
);
845 __vlan_group_free(vg
);
848 struct net_bridge_vlan
*br_vlan_find(struct net_bridge_vlan_group
*vg
, u16 vid
)
853 return br_vlan_lookup(&vg
->vlan_hash
, vid
);
856 /* Must be protected by RTNL. */
857 static void recalculate_group_addr(struct net_bridge
*br
)
859 if (br_opt_get(br
, BROPT_GROUP_ADDR_SET
))
862 spin_lock_bh(&br
->lock
);
863 if (!br_opt_get(br
, BROPT_VLAN_ENABLED
) ||
864 br
->vlan_proto
== htons(ETH_P_8021Q
)) {
865 /* Bridge Group Address */
866 br
->group_addr
[5] = 0x00;
867 } else { /* vlan_enabled && ETH_P_8021AD */
868 /* Provider Bridge Group Address */
869 br
->group_addr
[5] = 0x08;
871 spin_unlock_bh(&br
->lock
);
874 /* Must be protected by RTNL. */
875 void br_recalculate_fwd_mask(struct net_bridge
*br
)
877 if (!br_opt_get(br
, BROPT_VLAN_ENABLED
) ||
878 br
->vlan_proto
== htons(ETH_P_8021Q
))
879 br
->group_fwd_mask_required
= BR_GROUPFWD_DEFAULT
;
880 else /* vlan_enabled && ETH_P_8021AD */
881 br
->group_fwd_mask_required
= BR_GROUPFWD_8021AD
&
882 ~(1u << br
->group_addr
[5]);
885 int br_vlan_filter_toggle(struct net_bridge
*br
, unsigned long val
,
886 struct netlink_ext_ack
*extack
)
888 struct switchdev_attr attr
= {
890 .id
= SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING
,
891 .flags
= SWITCHDEV_F_SKIP_EOPNOTSUPP
,
892 .u
.vlan_filtering
= val
,
896 if (br_opt_get(br
, BROPT_VLAN_ENABLED
) == !!val
)
899 br_opt_toggle(br
, BROPT_VLAN_ENABLED
, !!val
);
901 err
= switchdev_port_attr_set(br
->dev
, &attr
, extack
);
902 if (err
&& err
!= -EOPNOTSUPP
) {
903 br_opt_toggle(br
, BROPT_VLAN_ENABLED
, !val
);
907 br_manage_promisc(br
);
908 recalculate_group_addr(br
);
909 br_recalculate_fwd_mask(br
);
910 if (!val
&& br_opt_get(br
, BROPT_MCAST_VLAN_SNOOPING_ENABLED
)) {
911 br_info(br
, "vlan filtering disabled, automatically disabling multicast vlan snooping\n");
912 br_multicast_toggle_vlan_snooping(br
, false, NULL
);
918 bool br_vlan_enabled(const struct net_device
*dev
)
920 struct net_bridge
*br
= netdev_priv(dev
);
922 return br_opt_get(br
, BROPT_VLAN_ENABLED
);
924 EXPORT_SYMBOL_GPL(br_vlan_enabled
);
926 int br_vlan_get_proto(const struct net_device
*dev
, u16
*p_proto
)
928 struct net_bridge
*br
= netdev_priv(dev
);
930 *p_proto
= ntohs(br
->vlan_proto
);
934 EXPORT_SYMBOL_GPL(br_vlan_get_proto
);
936 int __br_vlan_set_proto(struct net_bridge
*br
, __be16 proto
,
937 struct netlink_ext_ack
*extack
)
939 struct switchdev_attr attr
= {
941 .id
= SWITCHDEV_ATTR_ID_BRIDGE_VLAN_PROTOCOL
,
942 .flags
= SWITCHDEV_F_SKIP_EOPNOTSUPP
,
943 .u
.vlan_protocol
= ntohs(proto
),
946 struct net_bridge_port
*p
;
947 struct net_bridge_vlan
*vlan
;
948 struct net_bridge_vlan_group
*vg
;
949 __be16 oldproto
= br
->vlan_proto
;
951 if (br
->vlan_proto
== proto
)
954 err
= switchdev_port_attr_set(br
->dev
, &attr
, extack
);
955 if (err
&& err
!= -EOPNOTSUPP
)
958 /* Add VLANs for the new proto to the device filter. */
959 list_for_each_entry(p
, &br
->port_list
, list
) {
960 vg
= nbp_vlan_group(p
);
961 list_for_each_entry(vlan
, &vg
->vlan_list
, vlist
) {
962 if (vlan
->priv_flags
& BR_VLFLAG_ADDED_BY_SWITCHDEV
)
964 err
= vlan_vid_add(p
->dev
, proto
, vlan
->vid
);
970 br
->vlan_proto
= proto
;
972 recalculate_group_addr(br
);
973 br_recalculate_fwd_mask(br
);
975 /* Delete VLANs for the old proto from the device filter. */
976 list_for_each_entry(p
, &br
->port_list
, list
) {
977 vg
= nbp_vlan_group(p
);
978 list_for_each_entry(vlan
, &vg
->vlan_list
, vlist
) {
979 if (vlan
->priv_flags
& BR_VLFLAG_ADDED_BY_SWITCHDEV
)
981 vlan_vid_del(p
->dev
, oldproto
, vlan
->vid
);
988 attr
.u
.vlan_protocol
= ntohs(oldproto
);
989 switchdev_port_attr_set(br
->dev
, &attr
, NULL
);
991 list_for_each_entry_continue_reverse(vlan
, &vg
->vlan_list
, vlist
) {
992 if (vlan
->priv_flags
& BR_VLFLAG_ADDED_BY_SWITCHDEV
)
994 vlan_vid_del(p
->dev
, proto
, vlan
->vid
);
997 list_for_each_entry_continue_reverse(p
, &br
->port_list
, list
) {
998 vg
= nbp_vlan_group(p
);
999 list_for_each_entry(vlan
, &vg
->vlan_list
, vlist
) {
1000 if (vlan
->priv_flags
& BR_VLFLAG_ADDED_BY_SWITCHDEV
)
1002 vlan_vid_del(p
->dev
, proto
, vlan
->vid
);
1009 int br_vlan_set_proto(struct net_bridge
*br
, unsigned long val
,
1010 struct netlink_ext_ack
*extack
)
1012 if (!eth_type_vlan(htons(val
)))
1013 return -EPROTONOSUPPORT
;
1015 return __br_vlan_set_proto(br
, htons(val
), extack
);
1018 int br_vlan_set_stats(struct net_bridge
*br
, unsigned long val
)
1023 br_opt_toggle(br
, BROPT_VLAN_STATS_ENABLED
, !!val
);
1032 int br_vlan_set_stats_per_port(struct net_bridge
*br
, unsigned long val
)
1034 struct net_bridge_port
*p
;
1036 /* allow to change the option if there are no port vlans configured */
1037 list_for_each_entry(p
, &br
->port_list
, list
) {
1038 struct net_bridge_vlan_group
*vg
= nbp_vlan_group(p
);
1047 br_opt_toggle(br
, BROPT_VLAN_STATS_PER_PORT
, !!val
);
1056 static bool vlan_default_pvid(struct net_bridge_vlan_group
*vg
, u16 vid
)
1058 struct net_bridge_vlan
*v
;
1060 if (vid
!= vg
->pvid
)
1063 v
= br_vlan_lookup(&vg
->vlan_hash
, vid
);
1064 if (v
&& br_vlan_should_use(v
) &&
1065 (v
->flags
& BRIDGE_VLAN_INFO_UNTAGGED
))
1071 static void br_vlan_disable_default_pvid(struct net_bridge
*br
)
1073 struct net_bridge_port
*p
;
1074 u16 pvid
= br
->default_pvid
;
1076 /* Disable default_pvid on all ports where it is still
1079 if (vlan_default_pvid(br_vlan_group(br
), pvid
)) {
1080 if (!br_vlan_delete(br
, pvid
))
1081 br_vlan_notify(br
, NULL
, pvid
, 0, RTM_DELVLAN
);
1084 list_for_each_entry(p
, &br
->port_list
, list
) {
1085 if (vlan_default_pvid(nbp_vlan_group(p
), pvid
) &&
1086 !nbp_vlan_delete(p
, pvid
))
1087 br_vlan_notify(br
, p
, pvid
, 0, RTM_DELVLAN
);
1090 br
->default_pvid
= 0;
1093 int __br_vlan_set_default_pvid(struct net_bridge
*br
, u16 pvid
,
1094 struct netlink_ext_ack
*extack
)
1096 const struct net_bridge_vlan
*pvent
;
1097 struct net_bridge_vlan_group
*vg
;
1098 struct net_bridge_port
*p
;
1099 unsigned long *changed
;
1105 br_vlan_disable_default_pvid(br
);
1109 changed
= bitmap_zalloc(BR_MAX_PORTS
, GFP_KERNEL
);
1113 old_pvid
= br
->default_pvid
;
1115 /* Update default_pvid config only if we do not conflict with
1116 * user configuration.
1118 vg
= br_vlan_group(br
);
1119 pvent
= br_vlan_find(vg
, pvid
);
1120 if ((!old_pvid
|| vlan_default_pvid(vg
, old_pvid
)) &&
1121 (!pvent
|| !br_vlan_should_use(pvent
))) {
1122 err
= br_vlan_add(br
, pvid
,
1123 BRIDGE_VLAN_INFO_PVID
|
1124 BRIDGE_VLAN_INFO_UNTAGGED
|
1125 BRIDGE_VLAN_INFO_BRENTRY
,
1130 if (br_vlan_delete(br
, old_pvid
))
1131 br_vlan_notify(br
, NULL
, old_pvid
, 0, RTM_DELVLAN
);
1132 br_vlan_notify(br
, NULL
, pvid
, 0, RTM_NEWVLAN
);
1133 __set_bit(0, changed
);
1136 list_for_each_entry(p
, &br
->port_list
, list
) {
1137 /* Update default_pvid config only if we do not conflict with
1138 * user configuration.
1140 vg
= nbp_vlan_group(p
);
1142 !vlan_default_pvid(vg
, old_pvid
)) ||
1143 br_vlan_find(vg
, pvid
))
1146 err
= nbp_vlan_add(p
, pvid
,
1147 BRIDGE_VLAN_INFO_PVID
|
1148 BRIDGE_VLAN_INFO_UNTAGGED
,
1152 if (nbp_vlan_delete(p
, old_pvid
))
1153 br_vlan_notify(br
, p
, old_pvid
, 0, RTM_DELVLAN
);
1154 br_vlan_notify(p
->br
, p
, pvid
, 0, RTM_NEWVLAN
);
1155 __set_bit(p
->port_no
, changed
);
1158 br
->default_pvid
= pvid
;
1161 bitmap_free(changed
);
1165 list_for_each_entry_continue_reverse(p
, &br
->port_list
, list
) {
1166 if (!test_bit(p
->port_no
, changed
))
1170 nbp_vlan_add(p
, old_pvid
,
1171 BRIDGE_VLAN_INFO_PVID
|
1172 BRIDGE_VLAN_INFO_UNTAGGED
,
1174 br_vlan_notify(p
->br
, p
, old_pvid
, 0, RTM_NEWVLAN
);
1176 nbp_vlan_delete(p
, pvid
);
1177 br_vlan_notify(br
, p
, pvid
, 0, RTM_DELVLAN
);
1180 if (test_bit(0, changed
)) {
1182 br_vlan_add(br
, old_pvid
,
1183 BRIDGE_VLAN_INFO_PVID
|
1184 BRIDGE_VLAN_INFO_UNTAGGED
|
1185 BRIDGE_VLAN_INFO_BRENTRY
,
1187 br_vlan_notify(br
, NULL
, old_pvid
, 0, RTM_NEWVLAN
);
1189 br_vlan_delete(br
, pvid
);
1190 br_vlan_notify(br
, NULL
, pvid
, 0, RTM_DELVLAN
);
1195 int br_vlan_set_default_pvid(struct net_bridge
*br
, unsigned long val
,
1196 struct netlink_ext_ack
*extack
)
1201 if (val
>= VLAN_VID_MASK
)
1204 if (pvid
== br
->default_pvid
)
1207 /* Only allow default pvid change when filtering is disabled */
1208 if (br_opt_get(br
, BROPT_VLAN_ENABLED
)) {
1209 pr_info_once("Please disable vlan filtering to change default_pvid\n");
1213 err
= __br_vlan_set_default_pvid(br
, pvid
, extack
);
1218 int br_vlan_init(struct net_bridge
*br
)
1220 struct net_bridge_vlan_group
*vg
;
1223 vg
= kzalloc(sizeof(*vg
), GFP_KERNEL
);
1226 ret
= rhashtable_init(&vg
->vlan_hash
, &br_vlan_rht_params
);
1229 ret
= vlan_tunnel_init(vg
);
1231 goto err_tunnel_init
;
1232 INIT_LIST_HEAD(&vg
->vlan_list
);
1233 br
->vlan_proto
= htons(ETH_P_8021Q
);
1234 br
->default_pvid
= 1;
1235 rcu_assign_pointer(br
->vlgrp
, vg
);
1241 rhashtable_destroy(&vg
->vlan_hash
);
1248 int nbp_vlan_init(struct net_bridge_port
*p
, struct netlink_ext_ack
*extack
)
1250 struct switchdev_attr attr
= {
1251 .orig_dev
= p
->br
->dev
,
1252 .id
= SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING
,
1253 .flags
= SWITCHDEV_F_SKIP_EOPNOTSUPP
,
1254 .u
.vlan_filtering
= br_opt_get(p
->br
, BROPT_VLAN_ENABLED
),
1256 struct net_bridge_vlan_group
*vg
;
1259 vg
= kzalloc(sizeof(struct net_bridge_vlan_group
), GFP_KERNEL
);
1263 ret
= switchdev_port_attr_set(p
->dev
, &attr
, extack
);
1264 if (ret
&& ret
!= -EOPNOTSUPP
)
1265 goto err_vlan_enabled
;
1267 ret
= rhashtable_init(&vg
->vlan_hash
, &br_vlan_rht_params
);
1270 ret
= vlan_tunnel_init(vg
);
1272 goto err_tunnel_init
;
1273 INIT_LIST_HEAD(&vg
->vlan_list
);
1274 rcu_assign_pointer(p
->vlgrp
, vg
);
1275 if (p
->br
->default_pvid
) {
1278 ret
= nbp_vlan_add(p
, p
->br
->default_pvid
,
1279 BRIDGE_VLAN_INFO_PVID
|
1280 BRIDGE_VLAN_INFO_UNTAGGED
,
1284 br_vlan_notify(p
->br
, p
, p
->br
->default_pvid
, 0, RTM_NEWVLAN
);
1290 RCU_INIT_POINTER(p
->vlgrp
, NULL
);
1292 vlan_tunnel_deinit(vg
);
1294 rhashtable_destroy(&vg
->vlan_hash
);
1302 /* Must be protected by RTNL.
1303 * Must be called with vid in range from 1 to 4094 inclusive.
1304 * changed must be true only if the vlan was created or updated
1306 int nbp_vlan_add(struct net_bridge_port
*port
, u16 vid
, u16 flags
,
1307 bool *changed
, struct netlink_ext_ack
*extack
)
1309 struct net_bridge_vlan
*vlan
;
1315 vlan
= br_vlan_find(nbp_vlan_group(port
), vid
);
1317 bool would_change
= __vlan_flags_would_change(vlan
, flags
);
1320 /* Pass the flags to the hardware bridge */
1321 ret
= br_switchdev_port_vlan_add(port
->dev
, vid
, flags
,
1323 if (ret
&& ret
!= -EOPNOTSUPP
)
1327 __vlan_flags_commit(vlan
, flags
);
1328 *changed
= would_change
;
1333 vlan
= kzalloc(sizeof(*vlan
), GFP_KERNEL
);
1339 ret
= __vlan_add(vlan
, flags
, extack
);
1348 /* Must be protected by RTNL.
1349 * Must be called with vid in range from 1 to 4094 inclusive.
1351 int nbp_vlan_delete(struct net_bridge_port
*port
, u16 vid
)
1353 struct net_bridge_vlan
*v
;
1357 v
= br_vlan_find(nbp_vlan_group(port
), vid
);
1360 br_fdb_find_delete_local(port
->br
, port
, port
->dev
->dev_addr
, vid
);
1361 br_fdb_delete_by_port(port
->br
, port
, vid
, 0);
1363 return __vlan_del(v
);
1366 void nbp_vlan_flush(struct net_bridge_port
*port
)
1368 struct net_bridge_vlan_group
*vg
;
1372 vg
= nbp_vlan_group(port
);
1373 __vlan_flush(port
->br
, port
, vg
);
1374 RCU_INIT_POINTER(port
->vlgrp
, NULL
);
1376 __vlan_group_free(vg
);
1379 void br_vlan_get_stats(const struct net_bridge_vlan
*v
,
1380 struct pcpu_sw_netstats
*stats
)
1384 memset(stats
, 0, sizeof(*stats
));
1385 for_each_possible_cpu(i
) {
1386 u64 rxpackets
, rxbytes
, txpackets
, txbytes
;
1387 struct pcpu_sw_netstats
*cpu_stats
;
1390 cpu_stats
= per_cpu_ptr(v
->stats
, i
);
1392 start
= u64_stats_fetch_begin(&cpu_stats
->syncp
);
1393 rxpackets
= u64_stats_read(&cpu_stats
->rx_packets
);
1394 rxbytes
= u64_stats_read(&cpu_stats
->rx_bytes
);
1395 txbytes
= u64_stats_read(&cpu_stats
->tx_bytes
);
1396 txpackets
= u64_stats_read(&cpu_stats
->tx_packets
);
1397 } while (u64_stats_fetch_retry(&cpu_stats
->syncp
, start
));
1399 u64_stats_add(&stats
->rx_packets
, rxpackets
);
1400 u64_stats_add(&stats
->rx_bytes
, rxbytes
);
1401 u64_stats_add(&stats
->tx_bytes
, txbytes
);
1402 u64_stats_add(&stats
->tx_packets
, txpackets
);
1406 int br_vlan_get_pvid(const struct net_device
*dev
, u16
*p_pvid
)
1408 struct net_bridge_vlan_group
*vg
;
1409 struct net_bridge_port
*p
;
1412 p
= br_port_get_check_rtnl(dev
);
1414 vg
= nbp_vlan_group(p
);
1415 else if (netif_is_bridge_master(dev
))
1416 vg
= br_vlan_group(netdev_priv(dev
));
1420 *p_pvid
= br_get_pvid(vg
);
1423 EXPORT_SYMBOL_GPL(br_vlan_get_pvid
);
1425 int br_vlan_get_pvid_rcu(const struct net_device
*dev
, u16
*p_pvid
)
1427 struct net_bridge_vlan_group
*vg
;
1428 struct net_bridge_port
*p
;
1430 p
= br_port_get_check_rcu(dev
);
1432 vg
= nbp_vlan_group_rcu(p
);
1433 else if (netif_is_bridge_master(dev
))
1434 vg
= br_vlan_group_rcu(netdev_priv(dev
));
1438 *p_pvid
= br_get_pvid(vg
);
1441 EXPORT_SYMBOL_GPL(br_vlan_get_pvid_rcu
);
1443 void br_vlan_fill_forward_path_pvid(struct net_bridge
*br
,
1444 struct net_device_path_ctx
*ctx
,
1445 struct net_device_path
*path
)
1447 struct net_bridge_vlan_group
*vg
;
1448 int idx
= ctx
->num_vlans
- 1;
1451 path
->bridge
.vlan_mode
= DEV_PATH_BR_VLAN_KEEP
;
1453 if (!br_opt_get(br
, BROPT_VLAN_ENABLED
))
1456 vg
= br_vlan_group(br
);
1459 ctx
->vlan
[idx
].proto
== br
->vlan_proto
) {
1460 vid
= ctx
->vlan
[idx
].id
;
1462 path
->bridge
.vlan_mode
= DEV_PATH_BR_VLAN_TAG
;
1463 vid
= br_get_pvid(vg
);
1466 path
->bridge
.vlan_id
= vid
;
1467 path
->bridge
.vlan_proto
= br
->vlan_proto
;
1470 int br_vlan_fill_forward_path_mode(struct net_bridge
*br
,
1471 struct net_bridge_port
*dst
,
1472 struct net_device_path
*path
)
1474 struct net_bridge_vlan_group
*vg
;
1475 struct net_bridge_vlan
*v
;
1477 if (!br_opt_get(br
, BROPT_VLAN_ENABLED
))
1480 vg
= nbp_vlan_group_rcu(dst
);
1481 v
= br_vlan_find(vg
, path
->bridge
.vlan_id
);
1482 if (!v
|| !br_vlan_should_use(v
))
1485 if (!(v
->flags
& BRIDGE_VLAN_INFO_UNTAGGED
))
1488 if (path
->bridge
.vlan_mode
== DEV_PATH_BR_VLAN_TAG
)
1489 path
->bridge
.vlan_mode
= DEV_PATH_BR_VLAN_KEEP
;
1490 else if (v
->priv_flags
& BR_VLFLAG_ADDED_BY_SWITCHDEV
)
1491 path
->bridge
.vlan_mode
= DEV_PATH_BR_VLAN_UNTAG_HW
;
1493 path
->bridge
.vlan_mode
= DEV_PATH_BR_VLAN_UNTAG
;
1498 int br_vlan_get_info(const struct net_device
*dev
, u16 vid
,
1499 struct bridge_vlan_info
*p_vinfo
)
1501 struct net_bridge_vlan_group
*vg
;
1502 struct net_bridge_vlan
*v
;
1503 struct net_bridge_port
*p
;
1506 p
= br_port_get_check_rtnl(dev
);
1508 vg
= nbp_vlan_group(p
);
1509 else if (netif_is_bridge_master(dev
))
1510 vg
= br_vlan_group(netdev_priv(dev
));
1514 v
= br_vlan_find(vg
, vid
);
1519 p_vinfo
->flags
= v
->flags
;
1520 if (vid
== br_get_pvid(vg
))
1521 p_vinfo
->flags
|= BRIDGE_VLAN_INFO_PVID
;
1524 EXPORT_SYMBOL_GPL(br_vlan_get_info
);
1526 int br_vlan_get_info_rcu(const struct net_device
*dev
, u16 vid
,
1527 struct bridge_vlan_info
*p_vinfo
)
1529 struct net_bridge_vlan_group
*vg
;
1530 struct net_bridge_vlan
*v
;
1531 struct net_bridge_port
*p
;
1533 p
= br_port_get_check_rcu(dev
);
1535 vg
= nbp_vlan_group_rcu(p
);
1536 else if (netif_is_bridge_master(dev
))
1537 vg
= br_vlan_group_rcu(netdev_priv(dev
));
1541 v
= br_vlan_find(vg
, vid
);
1546 p_vinfo
->flags
= v
->flags
;
1547 if (vid
== br_get_pvid(vg
))
1548 p_vinfo
->flags
|= BRIDGE_VLAN_INFO_PVID
;
1551 EXPORT_SYMBOL_GPL(br_vlan_get_info_rcu
);
1553 static int br_vlan_is_bind_vlan_dev(const struct net_device
*dev
)
1555 return is_vlan_dev(dev
) &&
1556 !!(vlan_dev_priv(dev
)->flags
& VLAN_FLAG_BRIDGE_BINDING
);
1559 static int br_vlan_is_bind_vlan_dev_fn(struct net_device
*dev
,
1560 __always_unused
struct netdev_nested_priv
*priv
)
1562 return br_vlan_is_bind_vlan_dev(dev
);
1565 static bool br_vlan_has_upper_bind_vlan_dev(struct net_device
*dev
)
1570 found
= netdev_walk_all_upper_dev_rcu(dev
, br_vlan_is_bind_vlan_dev_fn
,
1577 struct br_vlan_bind_walk_data
{
1579 struct net_device
*result
;
1582 static int br_vlan_match_bind_vlan_dev_fn(struct net_device
*dev
,
1583 struct netdev_nested_priv
*priv
)
1585 struct br_vlan_bind_walk_data
*data
= priv
->data
;
1588 if (br_vlan_is_bind_vlan_dev(dev
) &&
1589 vlan_dev_priv(dev
)->vlan_id
== data
->vid
) {
1597 static struct net_device
*
1598 br_vlan_get_upper_bind_vlan_dev(struct net_device
*dev
, u16 vid
)
1600 struct br_vlan_bind_walk_data data
= {
1603 struct netdev_nested_priv priv
= {
1604 .data
= (void *)&data
,
1608 netdev_walk_all_upper_dev_rcu(dev
, br_vlan_match_bind_vlan_dev_fn
,
1615 static bool br_vlan_is_dev_up(const struct net_device
*dev
)
1617 return !!(dev
->flags
& IFF_UP
) && netif_oper_up(dev
);
1620 static void br_vlan_set_vlan_dev_state(const struct net_bridge
*br
,
1621 struct net_device
*vlan_dev
)
1623 u16 vid
= vlan_dev_priv(vlan_dev
)->vlan_id
;
1624 struct net_bridge_vlan_group
*vg
;
1625 struct net_bridge_port
*p
;
1626 bool has_carrier
= false;
1628 if (!netif_carrier_ok(br
->dev
)) {
1629 netif_carrier_off(vlan_dev
);
1633 list_for_each_entry(p
, &br
->port_list
, list
) {
1634 vg
= nbp_vlan_group(p
);
1635 if (br_vlan_find(vg
, vid
) && br_vlan_is_dev_up(p
->dev
)) {
1642 netif_carrier_on(vlan_dev
);
1644 netif_carrier_off(vlan_dev
);
1647 static void br_vlan_set_all_vlan_dev_state(struct net_bridge_port
*p
)
1649 struct net_bridge_vlan_group
*vg
= nbp_vlan_group(p
);
1650 struct net_bridge_vlan
*vlan
;
1651 struct net_device
*vlan_dev
;
1653 list_for_each_entry(vlan
, &vg
->vlan_list
, vlist
) {
1654 vlan_dev
= br_vlan_get_upper_bind_vlan_dev(p
->br
->dev
,
1657 if (br_vlan_is_dev_up(p
->dev
)) {
1658 if (netif_carrier_ok(p
->br
->dev
))
1659 netif_carrier_on(vlan_dev
);
1661 br_vlan_set_vlan_dev_state(p
->br
, vlan_dev
);
1667 static void br_vlan_upper_change(struct net_device
*dev
,
1668 struct net_device
*upper_dev
,
1671 struct net_bridge
*br
= netdev_priv(dev
);
1673 if (!br_vlan_is_bind_vlan_dev(upper_dev
))
1677 br_vlan_set_vlan_dev_state(br
, upper_dev
);
1678 br_opt_toggle(br
, BROPT_VLAN_BRIDGE_BINDING
, true);
1680 br_opt_toggle(br
, BROPT_VLAN_BRIDGE_BINDING
,
1681 br_vlan_has_upper_bind_vlan_dev(dev
));
1685 struct br_vlan_link_state_walk_data
{
1686 struct net_bridge
*br
;
1689 static int br_vlan_link_state_change_fn(struct net_device
*vlan_dev
,
1690 struct netdev_nested_priv
*priv
)
1692 struct br_vlan_link_state_walk_data
*data
= priv
->data
;
1694 if (br_vlan_is_bind_vlan_dev(vlan_dev
))
1695 br_vlan_set_vlan_dev_state(data
->br
, vlan_dev
);
1700 static void br_vlan_link_state_change(struct net_device
*dev
,
1701 struct net_bridge
*br
)
1703 struct br_vlan_link_state_walk_data data
= {
1706 struct netdev_nested_priv priv
= {
1707 .data
= (void *)&data
,
1711 netdev_walk_all_upper_dev_rcu(dev
, br_vlan_link_state_change_fn
,
1716 /* Must be protected by RTNL. */
1717 static void nbp_vlan_set_vlan_dev_state(struct net_bridge_port
*p
, u16 vid
)
1719 struct net_device
*vlan_dev
;
1721 if (!br_opt_get(p
->br
, BROPT_VLAN_BRIDGE_BINDING
))
1724 vlan_dev
= br_vlan_get_upper_bind_vlan_dev(p
->br
->dev
, vid
);
1726 br_vlan_set_vlan_dev_state(p
->br
, vlan_dev
);
1729 /* Must be protected by RTNL. */
1730 int br_vlan_bridge_event(struct net_device
*dev
, unsigned long event
, void *ptr
)
1732 struct netdev_notifier_changeupper_info
*info
;
1733 struct net_bridge
*br
= netdev_priv(dev
);
1734 int vlcmd
= 0, ret
= 0;
1735 bool changed
= false;
1738 case NETDEV_REGISTER
:
1739 ret
= br_vlan_add(br
, br
->default_pvid
,
1740 BRIDGE_VLAN_INFO_PVID
|
1741 BRIDGE_VLAN_INFO_UNTAGGED
|
1742 BRIDGE_VLAN_INFO_BRENTRY
, &changed
, NULL
);
1743 vlcmd
= RTM_NEWVLAN
;
1745 case NETDEV_UNREGISTER
:
1746 changed
= !br_vlan_delete(br
, br
->default_pvid
);
1747 vlcmd
= RTM_DELVLAN
;
1749 case NETDEV_CHANGEUPPER
:
1751 br_vlan_upper_change(dev
, info
->upper_dev
, info
->linking
);
1756 if (!br_opt_get(br
, BROPT_VLAN_BRIDGE_BINDING
))
1758 br_vlan_link_state_change(dev
, br
);
1762 br_vlan_notify(br
, NULL
, br
->default_pvid
, 0, vlcmd
);
1767 /* Must be protected by RTNL. */
1768 void br_vlan_port_event(struct net_bridge_port
*p
, unsigned long event
)
1770 if (!br_opt_get(p
->br
, BROPT_VLAN_BRIDGE_BINDING
))
1777 br_vlan_set_all_vlan_dev_state(p
);
1782 static bool br_vlan_stats_fill(struct sk_buff
*skb
,
1783 const struct net_bridge_vlan
*v
)
1785 struct pcpu_sw_netstats stats
;
1786 struct nlattr
*nest
;
1788 nest
= nla_nest_start(skb
, BRIDGE_VLANDB_ENTRY_STATS
);
1792 br_vlan_get_stats(v
, &stats
);
1793 if (nla_put_u64_64bit(skb
, BRIDGE_VLANDB_STATS_RX_BYTES
,
1794 u64_stats_read(&stats
.rx_bytes
),
1795 BRIDGE_VLANDB_STATS_PAD
) ||
1796 nla_put_u64_64bit(skb
, BRIDGE_VLANDB_STATS_RX_PACKETS
,
1797 u64_stats_read(&stats
.rx_packets
),
1798 BRIDGE_VLANDB_STATS_PAD
) ||
1799 nla_put_u64_64bit(skb
, BRIDGE_VLANDB_STATS_TX_BYTES
,
1800 u64_stats_read(&stats
.tx_bytes
),
1801 BRIDGE_VLANDB_STATS_PAD
) ||
1802 nla_put_u64_64bit(skb
, BRIDGE_VLANDB_STATS_TX_PACKETS
,
1803 u64_stats_read(&stats
.tx_packets
),
1804 BRIDGE_VLANDB_STATS_PAD
))
1807 nla_nest_end(skb
, nest
);
1812 nla_nest_cancel(skb
, nest
);
1816 /* v_opts is used to dump the options which must be equal in the whole range */
1817 static bool br_vlan_fill_vids(struct sk_buff
*skb
, u16 vid
, u16 vid_range
,
1818 const struct net_bridge_vlan
*v_opts
,
1819 const struct net_bridge_port
*p
,
1823 struct bridge_vlan_info info
;
1824 struct nlattr
*nest
;
1826 nest
= nla_nest_start(skb
, BRIDGE_VLANDB_ENTRY
);
1830 memset(&info
, 0, sizeof(info
));
1832 if (flags
& BRIDGE_VLAN_INFO_UNTAGGED
)
1833 info
.flags
|= BRIDGE_VLAN_INFO_UNTAGGED
;
1834 if (flags
& BRIDGE_VLAN_INFO_PVID
)
1835 info
.flags
|= BRIDGE_VLAN_INFO_PVID
;
1837 if (nla_put(skb
, BRIDGE_VLANDB_ENTRY_INFO
, sizeof(info
), &info
))
1840 if (vid_range
&& vid
< vid_range
&&
1841 !(flags
& BRIDGE_VLAN_INFO_PVID
) &&
1842 nla_put_u16(skb
, BRIDGE_VLANDB_ENTRY_RANGE
, vid_range
))
1846 if (!br_vlan_opts_fill(skb
, v_opts
, p
))
1849 if (dump_stats
&& !br_vlan_stats_fill(skb
, v_opts
))
1853 nla_nest_end(skb
, nest
);
1858 nla_nest_cancel(skb
, nest
);
1862 static size_t rtnl_vlan_nlmsg_size(void)
1864 return NLMSG_ALIGN(sizeof(struct br_vlan_msg
))
1865 + nla_total_size(0) /* BRIDGE_VLANDB_ENTRY */
1866 + nla_total_size(sizeof(u16
)) /* BRIDGE_VLANDB_ENTRY_RANGE */
1867 + nla_total_size(sizeof(struct bridge_vlan_info
)) /* BRIDGE_VLANDB_ENTRY_INFO */
1868 + br_vlan_opts_nl_size(); /* bridge vlan options */
1871 void br_vlan_notify(const struct net_bridge
*br
,
1872 const struct net_bridge_port
*p
,
1873 u16 vid
, u16 vid_range
,
1876 struct net_bridge_vlan_group
*vg
;
1877 struct net_bridge_vlan
*v
= NULL
;
1878 struct br_vlan_msg
*bvm
;
1879 struct nlmsghdr
*nlh
;
1880 struct sk_buff
*skb
;
1886 /* right now notifications are done only with rtnl held */
1890 ifindex
= p
->dev
->ifindex
;
1891 vg
= nbp_vlan_group(p
);
1892 net
= dev_net(p
->dev
);
1894 ifindex
= br
->dev
->ifindex
;
1895 vg
= br_vlan_group(br
);
1896 net
= dev_net(br
->dev
);
1899 skb
= nlmsg_new(rtnl_vlan_nlmsg_size(), GFP_KERNEL
);
1904 nlh
= nlmsg_put(skb
, 0, 0, cmd
, sizeof(*bvm
), 0);
1907 bvm
= nlmsg_data(nlh
);
1908 memset(bvm
, 0, sizeof(*bvm
));
1909 bvm
->family
= AF_BRIDGE
;
1910 bvm
->ifindex
= ifindex
;
1914 /* need to find the vlan due to flags/options */
1915 v
= br_vlan_find(vg
, vid
);
1916 if (!v
|| !br_vlan_should_use(v
))
1920 if (br_get_pvid(vg
) == v
->vid
)
1921 flags
|= BRIDGE_VLAN_INFO_PVID
;
1929 if (!br_vlan_fill_vids(skb
, vid
, vid_range
, v
, p
, flags
, false))
1932 nlmsg_end(skb
, nlh
);
1933 rtnl_notify(skb
, net
, 0, RTNLGRP_BRVLAN
, NULL
, GFP_KERNEL
);
1937 rtnl_set_sk_err(net
, RTNLGRP_BRVLAN
, err
);
1942 /* check if v_curr can enter a range ending in range_end */
1943 bool br_vlan_can_enter_range(const struct net_bridge_vlan
*v_curr
,
1944 const struct net_bridge_vlan
*range_end
)
1946 return v_curr
->vid
- range_end
->vid
== 1 &&
1947 range_end
->flags
== v_curr
->flags
&&
1948 br_vlan_opts_eq_range(v_curr
, range_end
);
1951 static int br_vlan_dump_dev(const struct net_device
*dev
,
1952 struct sk_buff
*skb
,
1953 struct netlink_callback
*cb
,
1956 struct net_bridge_vlan
*v
, *range_start
= NULL
, *range_end
= NULL
;
1957 bool dump_global
= !!(dump_flags
& BRIDGE_VLANDB_DUMPF_GLOBAL
);
1958 bool dump_stats
= !!(dump_flags
& BRIDGE_VLANDB_DUMPF_STATS
);
1959 struct net_bridge_vlan_group
*vg
;
1960 int idx
= 0, s_idx
= cb
->args
[1];
1961 struct nlmsghdr
*nlh
= NULL
;
1962 struct net_bridge_port
*p
;
1963 struct br_vlan_msg
*bvm
;
1964 struct net_bridge
*br
;
1968 if (!netif_is_bridge_master(dev
) && !netif_is_bridge_port(dev
))
1971 if (netif_is_bridge_master(dev
)) {
1972 br
= netdev_priv(dev
);
1973 vg
= br_vlan_group_rcu(br
);
1976 /* global options are dumped only for bridge devices */
1980 p
= br_port_get_rcu(dev
);
1983 vg
= nbp_vlan_group_rcu(p
);
1990 nlh
= nlmsg_put(skb
, NETLINK_CB(cb
->skb
).portid
, cb
->nlh
->nlmsg_seq
,
1991 RTM_NEWVLAN
, sizeof(*bvm
), NLM_F_MULTI
);
1994 bvm
= nlmsg_data(nlh
);
1995 memset(bvm
, 0, sizeof(*bvm
));
1996 bvm
->family
= PF_BRIDGE
;
1997 bvm
->ifindex
= dev
->ifindex
;
1998 pvid
= br_get_pvid(vg
);
2000 /* idx must stay at range's beginning until it is filled in */
2001 list_for_each_entry_rcu(v
, &vg
->vlan_list
, vlist
) {
2002 if (!dump_global
&& !br_vlan_should_use(v
))
2016 if (br_vlan_global_opts_can_enter_range(v
, range_end
))
2018 if (!br_vlan_global_opts_fill(skb
, range_start
->vid
,
2024 /* advance number of filled vlans */
2025 idx
+= range_end
->vid
- range_start
->vid
+ 1;
2028 } else if (dump_stats
|| v
->vid
== pvid
||
2029 !br_vlan_can_enter_range(v
, range_end
)) {
2030 u16 vlan_flags
= br_vlan_flags(range_start
, pvid
);
2032 if (!br_vlan_fill_vids(skb
, range_start
->vid
,
2033 range_end
->vid
, range_start
,
2034 p
, vlan_flags
, dump_stats
)) {
2038 /* advance number of filled vlans */
2039 idx
+= range_end
->vid
- range_start
->vid
+ 1;
2047 /* err will be 0 and range_start will be set in 3 cases here:
2048 * - first vlan (range_start == range_end)
2049 * - last vlan (range_start == range_end, not in range)
2050 * - last vlan range (range_start != range_end, in range)
2052 if (!err
&& range_start
) {
2054 !br_vlan_global_opts_fill(skb
, range_start
->vid
,
2055 range_end
->vid
, range_start
))
2057 else if (!dump_global
&&
2058 !br_vlan_fill_vids(skb
, range_start
->vid
,
2059 range_end
->vid
, range_start
,
2060 p
, br_vlan_flags(range_start
, pvid
),
2065 cb
->args
[1] = err
? idx
: 0;
2067 nlmsg_end(skb
, nlh
);
2072 static const struct nla_policy br_vlan_db_dump_pol
[BRIDGE_VLANDB_DUMP_MAX
+ 1] = {
2073 [BRIDGE_VLANDB_DUMP_FLAGS
] = { .type
= NLA_U32
},
2076 static int br_vlan_rtm_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2078 struct nlattr
*dtb
[BRIDGE_VLANDB_DUMP_MAX
+ 1];
2079 int idx
= 0, err
= 0, s_idx
= cb
->args
[0];
2080 struct net
*net
= sock_net(skb
->sk
);
2081 struct br_vlan_msg
*bvm
;
2082 struct net_device
*dev
;
2085 err
= nlmsg_parse(cb
->nlh
, sizeof(*bvm
), dtb
, BRIDGE_VLANDB_DUMP_MAX
,
2086 br_vlan_db_dump_pol
, cb
->extack
);
2090 bvm
= nlmsg_data(cb
->nlh
);
2091 if (dtb
[BRIDGE_VLANDB_DUMP_FLAGS
])
2092 dump_flags
= nla_get_u32(dtb
[BRIDGE_VLANDB_DUMP_FLAGS
]);
2096 dev
= dev_get_by_index_rcu(net
, bvm
->ifindex
);
2101 err
= br_vlan_dump_dev(dev
, skb
, cb
, dump_flags
);
2102 /* if the dump completed without an error we return 0 here */
2103 if (err
!= -EMSGSIZE
)
2106 for_each_netdev_rcu(net
, dev
) {
2110 err
= br_vlan_dump_dev(dev
, skb
, cb
, dump_flags
);
2111 if (err
== -EMSGSIZE
)
2128 static const struct nla_policy br_vlan_db_policy
[BRIDGE_VLANDB_ENTRY_MAX
+ 1] = {
2129 [BRIDGE_VLANDB_ENTRY_INFO
] =
2130 NLA_POLICY_EXACT_LEN(sizeof(struct bridge_vlan_info
)),
2131 [BRIDGE_VLANDB_ENTRY_RANGE
] = { .type
= NLA_U16
},
2132 [BRIDGE_VLANDB_ENTRY_STATE
] = { .type
= NLA_U8
},
2133 [BRIDGE_VLANDB_ENTRY_TUNNEL_INFO
] = { .type
= NLA_NESTED
},
2134 [BRIDGE_VLANDB_ENTRY_MCAST_ROUTER
] = { .type
= NLA_U8
},
2135 [BRIDGE_VLANDB_ENTRY_MCAST_N_GROUPS
] = { .type
= NLA_REJECT
},
2136 [BRIDGE_VLANDB_ENTRY_MCAST_MAX_GROUPS
] = { .type
= NLA_U32
},
2137 [BRIDGE_VLANDB_ENTRY_NEIGH_SUPPRESS
] = NLA_POLICY_MAX(NLA_U8
, 1),
2140 static int br_vlan_rtm_process_one(struct net_device
*dev
,
2141 const struct nlattr
*attr
,
2142 int cmd
, struct netlink_ext_ack
*extack
)
2144 struct bridge_vlan_info
*vinfo
, vrange_end
, *vinfo_last
= NULL
;
2145 struct nlattr
*tb
[BRIDGE_VLANDB_ENTRY_MAX
+ 1];
2146 bool changed
= false, skip_processing
= false;
2147 struct net_bridge_vlan_group
*vg
;
2148 struct net_bridge_port
*p
= NULL
;
2149 int err
= 0, cmdmap
= 0;
2150 struct net_bridge
*br
;
2152 if (netif_is_bridge_master(dev
)) {
2153 br
= netdev_priv(dev
);
2154 vg
= br_vlan_group(br
);
2156 p
= br_port_get_rtnl(dev
);
2160 vg
= nbp_vlan_group(p
);
2166 err
= nla_parse_nested(tb
, BRIDGE_VLANDB_ENTRY_MAX
, attr
,
2167 br_vlan_db_policy
, extack
);
2171 if (!tb
[BRIDGE_VLANDB_ENTRY_INFO
]) {
2172 NL_SET_ERR_MSG_MOD(extack
, "Missing vlan entry info");
2175 memset(&vrange_end
, 0, sizeof(vrange_end
));
2177 vinfo
= nla_data(tb
[BRIDGE_VLANDB_ENTRY_INFO
]);
2178 if (vinfo
->flags
& (BRIDGE_VLAN_INFO_RANGE_BEGIN
|
2179 BRIDGE_VLAN_INFO_RANGE_END
)) {
2180 NL_SET_ERR_MSG_MOD(extack
, "Old-style vlan ranges are not allowed when using RTM vlan calls");
2183 if (!br_vlan_valid_id(vinfo
->vid
, extack
))
2186 if (tb
[BRIDGE_VLANDB_ENTRY_RANGE
]) {
2187 vrange_end
.vid
= nla_get_u16(tb
[BRIDGE_VLANDB_ENTRY_RANGE
]);
2188 /* validate user-provided flags without RANGE_BEGIN */
2189 vrange_end
.flags
= BRIDGE_VLAN_INFO_RANGE_END
| vinfo
->flags
;
2190 vinfo
->flags
|= BRIDGE_VLAN_INFO_RANGE_BEGIN
;
2192 /* vinfo_last is the range start, vinfo the range end */
2194 vinfo
= &vrange_end
;
2196 if (!br_vlan_valid_id(vinfo
->vid
, extack
) ||
2197 !br_vlan_valid_range(vinfo
, vinfo_last
, extack
))
2203 cmdmap
= RTM_SETLINK
;
2204 skip_processing
= !!(vinfo
->flags
& BRIDGE_VLAN_INFO_ONLY_OPTS
);
2207 cmdmap
= RTM_DELLINK
;
2211 if (!skip_processing
) {
2212 struct bridge_vlan_info
*tmp_last
= vinfo_last
;
2214 /* br_process_vlan_info may overwrite vinfo_last */
2215 err
= br_process_vlan_info(br
, p
, cmdmap
, vinfo
, &tmp_last
,
2218 /* notify first if anything changed */
2220 br_ifinfo_notify(cmdmap
, br
, p
);
2226 /* deal with options */
2227 if (cmd
== RTM_NEWVLAN
) {
2228 struct net_bridge_vlan
*range_start
, *range_end
;
2231 range_start
= br_vlan_find(vg
, vinfo_last
->vid
);
2232 range_end
= br_vlan_find(vg
, vinfo
->vid
);
2234 range_start
= br_vlan_find(vg
, vinfo
->vid
);
2235 range_end
= range_start
;
2238 err
= br_vlan_process_options(br
, p
, range_start
, range_end
,
2245 static int br_vlan_rtm_process(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2246 struct netlink_ext_ack
*extack
)
2248 struct net
*net
= sock_net(skb
->sk
);
2249 struct br_vlan_msg
*bvm
;
2250 struct net_device
*dev
;
2251 struct nlattr
*attr
;
2255 /* this should validate the header and check for remaining bytes */
2256 err
= nlmsg_parse(nlh
, sizeof(*bvm
), NULL
, BRIDGE_VLANDB_MAX
, NULL
,
2261 bvm
= nlmsg_data(nlh
);
2262 dev
= __dev_get_by_index(net
, bvm
->ifindex
);
2266 if (!netif_is_bridge_master(dev
) && !netif_is_bridge_port(dev
)) {
2267 NL_SET_ERR_MSG_MOD(extack
, "The device is not a valid bridge or bridge port");
2271 nlmsg_for_each_attr(attr
, nlh
, sizeof(*bvm
), rem
) {
2272 switch (nla_type(attr
)) {
2273 case BRIDGE_VLANDB_ENTRY
:
2274 err
= br_vlan_rtm_process_one(dev
, attr
,
2278 case BRIDGE_VLANDB_GLOBAL_OPTIONS
:
2279 err
= br_vlan_rtm_process_global_options(dev
, attr
,
2292 NL_SET_ERR_MSG_MOD(extack
, "No vlans found to process");
2299 static const struct rtnl_msg_handler br_vlan_rtnl_msg_handlers
[] = {
2300 {THIS_MODULE
, PF_BRIDGE
, RTM_NEWVLAN
, br_vlan_rtm_process
, NULL
, 0},
2301 {THIS_MODULE
, PF_BRIDGE
, RTM_DELVLAN
, br_vlan_rtm_process
, NULL
, 0},
2302 {THIS_MODULE
, PF_BRIDGE
, RTM_GETVLAN
, NULL
, br_vlan_rtm_dump
, 0},
2305 int br_vlan_rtnl_init(void)
2307 return rtnl_register_many(br_vlan_rtnl_msg_handlers
);
2310 void br_vlan_rtnl_uninit(void)
2312 rtnl_unregister_many(br_vlan_rtnl_msg_handlers
);