1 #include <linux/kernel.h>
2 #include <linux/netdevice.h>
3 #include <linux/rtnetlink.h>
4 #include <linux/slab.h>
5 #include <net/switchdev.h>
7 #include "br_private.h"
8 #include "br_private_tunnel.h"
10 static inline int br_vlan_cmp(struct rhashtable_compare_arg
*arg
,
13 const struct net_bridge_vlan
*vle
= ptr
;
14 u16 vid
= *(u16
*)arg
->key
;
16 return vle
->vid
!= vid
;
19 static const struct rhashtable_params br_vlan_rht_params
= {
20 .head_offset
= offsetof(struct net_bridge_vlan
, vnode
),
21 .key_offset
= offsetof(struct net_bridge_vlan
, vid
),
22 .key_len
= sizeof(u16
),
25 .max_size
= VLAN_N_VID
,
26 .obj_cmpfn
= br_vlan_cmp
,
27 .automatic_shrinking
= true,
30 static struct net_bridge_vlan
*br_vlan_lookup(struct rhashtable
*tbl
, u16 vid
)
32 return rhashtable_lookup_fast(tbl
, &vid
, br_vlan_rht_params
);
35 static bool __vlan_add_pvid(struct net_bridge_vlan_group
*vg
, u16 vid
)
46 static bool __vlan_delete_pvid(struct net_bridge_vlan_group
*vg
, u16 vid
)
57 /* return true if anything changed, false otherwise */
58 static bool __vlan_add_flags(struct net_bridge_vlan
*v
, u16 flags
)
60 struct net_bridge_vlan_group
*vg
;
61 u16 old_flags
= v
->flags
;
64 if (br_vlan_is_master(v
))
65 vg
= br_vlan_group(v
->br
);
67 vg
= nbp_vlan_group(v
->port
);
69 if (flags
& BRIDGE_VLAN_INFO_PVID
)
70 ret
= __vlan_add_pvid(vg
, v
->vid
);
72 ret
= __vlan_delete_pvid(vg
, v
->vid
);
74 if (flags
& BRIDGE_VLAN_INFO_UNTAGGED
)
75 v
->flags
|= BRIDGE_VLAN_INFO_UNTAGGED
;
77 v
->flags
&= ~BRIDGE_VLAN_INFO_UNTAGGED
;
79 return ret
|| !!(old_flags
^ v
->flags
);
82 static int __vlan_vid_add(struct net_device
*dev
, struct net_bridge
*br
,
85 struct switchdev_obj_port_vlan v
= {
87 .obj
.id
= SWITCHDEV_OBJ_ID_PORT_VLAN
,
94 /* Try switchdev op first. In case it is not supported, fallback to
97 err
= switchdev_port_obj_add(dev
, &v
.obj
);
98 if (err
== -EOPNOTSUPP
)
99 return vlan_vid_add(dev
, br
->vlan_proto
, vid
);
103 static void __vlan_add_list(struct net_bridge_vlan
*v
)
105 struct net_bridge_vlan_group
*vg
;
106 struct list_head
*headp
, *hpos
;
107 struct net_bridge_vlan
*vent
;
109 if (br_vlan_is_master(v
))
110 vg
= br_vlan_group(v
->br
);
112 vg
= nbp_vlan_group(v
->port
);
114 headp
= &vg
->vlan_list
;
115 list_for_each_prev(hpos
, headp
) {
116 vent
= list_entry(hpos
, struct net_bridge_vlan
, vlist
);
117 if (v
->vid
< vent
->vid
)
122 list_add_rcu(&v
->vlist
, hpos
);
125 static void __vlan_del_list(struct net_bridge_vlan
*v
)
127 list_del_rcu(&v
->vlist
);
130 static int __vlan_vid_del(struct net_device
*dev
, struct net_bridge
*br
,
133 struct switchdev_obj_port_vlan v
= {
135 .obj
.id
= SWITCHDEV_OBJ_ID_PORT_VLAN
,
141 /* Try switchdev op first. In case it is not supported, fallback to
144 err
= switchdev_port_obj_del(dev
, &v
.obj
);
145 if (err
== -EOPNOTSUPP
) {
146 vlan_vid_del(dev
, br
->vlan_proto
, vid
);
152 /* Returns a master vlan, if it didn't exist it gets created. In all cases a
153 * a reference is taken to the master vlan before returning.
155 static struct net_bridge_vlan
*br_vlan_get_master(struct net_bridge
*br
, u16 vid
)
157 struct net_bridge_vlan_group
*vg
;
158 struct net_bridge_vlan
*masterv
;
160 vg
= br_vlan_group(br
);
161 masterv
= br_vlan_find(vg
, vid
);
165 /* missing global ctx, create it now */
166 if (br_vlan_add(br
, vid
, 0, &changed
))
168 masterv
= br_vlan_find(vg
, vid
);
169 if (WARN_ON(!masterv
))
171 refcount_set(&masterv
->refcnt
, 1);
174 refcount_inc(&masterv
->refcnt
);
179 static void br_master_vlan_rcu_free(struct rcu_head
*rcu
)
181 struct net_bridge_vlan
*v
;
183 v
= container_of(rcu
, struct net_bridge_vlan
, rcu
);
184 WARN_ON(!br_vlan_is_master(v
));
185 free_percpu(v
->stats
);
190 static void br_vlan_put_master(struct net_bridge_vlan
*masterv
)
192 struct net_bridge_vlan_group
*vg
;
194 if (!br_vlan_is_master(masterv
))
197 vg
= br_vlan_group(masterv
->br
);
198 if (refcount_dec_and_test(&masterv
->refcnt
)) {
199 rhashtable_remove_fast(&vg
->vlan_hash
,
200 &masterv
->vnode
, br_vlan_rht_params
);
201 __vlan_del_list(masterv
);
202 call_rcu(&masterv
->rcu
, br_master_vlan_rcu_free
);
206 /* This is the shared VLAN add function which works for both ports and bridge
207 * devices. There are four possible calls to this function in terms of the
209 * 1. vlan is being added on a port (no master flags, global entry exists)
210 * 2. vlan is being added on a bridge (both master and brentry flags)
211 * 3. vlan is being added on a port, but a global entry didn't exist which
212 * is being created right now (master flag set, brentry flag unset), the
213 * global entry is used for global per-vlan features, but not for filtering
214 * 4. same as 3 but with both master and brentry flags set so the entry
215 * will be used for filtering in both the port and the bridge
217 static int __vlan_add(struct net_bridge_vlan
*v
, u16 flags
)
219 struct net_bridge_vlan
*masterv
= NULL
;
220 struct net_bridge_port
*p
= NULL
;
221 struct net_bridge_vlan_group
*vg
;
222 struct net_device
*dev
;
223 struct net_bridge
*br
;
226 if (br_vlan_is_master(v
)) {
229 vg
= br_vlan_group(br
);
234 vg
= nbp_vlan_group(p
);
238 /* Add VLAN to the device filter if it is supported.
239 * This ensures tagged traffic enters the bridge when
240 * promiscuous mode is disabled by br_manage_promisc().
242 err
= __vlan_vid_add(dev
, br
, v
->vid
, flags
);
246 /* need to work on the master vlan too */
247 if (flags
& BRIDGE_VLAN_INFO_MASTER
) {
250 err
= br_vlan_add(br
, v
->vid
,
251 flags
| BRIDGE_VLAN_INFO_BRENTRY
,
257 masterv
= br_vlan_get_master(br
, v
->vid
);
261 v
->stats
= masterv
->stats
;
264 /* Add the dev mac and count the vlan only if it's usable */
265 if (br_vlan_should_use(v
)) {
266 err
= br_fdb_insert(br
, p
, dev
->dev_addr
, v
->vid
);
268 br_err(br
, "failed insert local address into bridge forwarding table\n");
274 err
= rhashtable_lookup_insert_fast(&vg
->vlan_hash
, &v
->vnode
,
280 __vlan_add_flags(v
, flags
);
285 if (br_vlan_should_use(v
)) {
286 br_fdb_find_delete_local(br
, p
, dev
->dev_addr
, v
->vid
);
292 __vlan_vid_del(dev
, br
, v
->vid
);
294 br_vlan_put_master(masterv
);
302 static int __vlan_del(struct net_bridge_vlan
*v
)
304 struct net_bridge_vlan
*masterv
= v
;
305 struct net_bridge_vlan_group
*vg
;
306 struct net_bridge_port
*p
= NULL
;
309 if (br_vlan_is_master(v
)) {
310 vg
= br_vlan_group(v
->br
);
313 vg
= nbp_vlan_group(v
->port
);
317 __vlan_delete_pvid(vg
, v
->vid
);
319 err
= __vlan_vid_del(p
->dev
, p
->br
, v
->vid
);
324 if (br_vlan_should_use(v
)) {
325 v
->flags
&= ~BRIDGE_VLAN_INFO_BRENTRY
;
330 vlan_tunnel_info_del(vg
, v
);
331 rhashtable_remove_fast(&vg
->vlan_hash
, &v
->vnode
,
337 br_vlan_put_master(masterv
);
342 static void __vlan_group_free(struct net_bridge_vlan_group
*vg
)
344 WARN_ON(!list_empty(&vg
->vlan_list
));
345 rhashtable_destroy(&vg
->vlan_hash
);
346 vlan_tunnel_deinit(vg
);
350 static void __vlan_flush(struct net_bridge_vlan_group
*vg
)
352 struct net_bridge_vlan
*vlan
, *tmp
;
354 __vlan_delete_pvid(vg
, vg
->pvid
);
355 list_for_each_entry_safe(vlan
, tmp
, &vg
->vlan_list
, vlist
)
359 struct sk_buff
*br_handle_vlan(struct net_bridge
*br
,
360 const struct net_bridge_port
*p
,
361 struct net_bridge_vlan_group
*vg
,
364 struct br_vlan_stats
*stats
;
365 struct net_bridge_vlan
*v
;
368 /* If this packet was not filtered at input, let it pass */
369 if (!BR_INPUT_SKB_CB(skb
)->vlan_filtered
)
372 /* At this point, we know that the frame was filtered and contains
373 * a valid vlan id. If the vlan id has untagged flag set,
374 * send untagged; otherwise, send tagged.
376 br_vlan_get_tag(skb
, &vid
);
377 v
= br_vlan_find(vg
, vid
);
378 /* Vlan entry must be configured at this point. The
379 * only exception is the bridge is set in promisc mode and the
380 * packet is destined for the bridge device. In this case
381 * pass the packet as is.
383 if (!v
|| !br_vlan_should_use(v
)) {
384 if ((br
->dev
->flags
& IFF_PROMISC
) && skb
->dev
== br
->dev
) {
391 if (br
->vlan_stats_enabled
) {
392 stats
= this_cpu_ptr(v
->stats
);
393 u64_stats_update_begin(&stats
->syncp
);
394 stats
->tx_bytes
+= skb
->len
;
396 u64_stats_update_end(&stats
->syncp
);
399 if (v
->flags
& BRIDGE_VLAN_INFO_UNTAGGED
)
402 if (p
&& (p
->flags
& BR_VLAN_TUNNEL
) &&
403 br_handle_egress_vlan_tunnel(skb
, v
)) {
411 /* Called under RCU */
412 static bool __allowed_ingress(const struct net_bridge
*br
,
413 struct net_bridge_vlan_group
*vg
,
414 struct sk_buff
*skb
, u16
*vid
)
416 struct br_vlan_stats
*stats
;
417 struct net_bridge_vlan
*v
;
420 BR_INPUT_SKB_CB(skb
)->vlan_filtered
= true;
421 /* If vlan tx offload is disabled on bridge device and frame was
422 * sent from vlan device on the bridge device, it does not have
423 * HW accelerated vlan tag.
425 if (unlikely(!skb_vlan_tag_present(skb
) &&
426 skb
->protocol
== br
->vlan_proto
)) {
427 skb
= skb_vlan_untag(skb
);
432 if (!br_vlan_get_tag(skb
, vid
)) {
434 if (skb
->vlan_proto
!= br
->vlan_proto
) {
435 /* Protocol-mismatch, empty out vlan_tci for new tag */
436 skb_push(skb
, ETH_HLEN
);
437 skb
= vlan_insert_tag_set_proto(skb
, skb
->vlan_proto
,
438 skb_vlan_tag_get(skb
));
442 skb_pull(skb
, ETH_HLEN
);
443 skb_reset_mac_len(skb
);
455 u16 pvid
= br_get_pvid(vg
);
457 /* Frame had a tag with VID 0 or did not have a tag.
458 * See if pvid is set on this port. That tells us which
459 * vlan untagged or priority-tagged traffic belongs to.
464 /* PVID is set on this port. Any untagged or priority-tagged
465 * ingress frame is considered to belong to this vlan.
469 /* Untagged Frame. */
470 __vlan_hwaccel_put_tag(skb
, br
->vlan_proto
, pvid
);
472 /* Priority-tagged Frame.
473 * At this point, We know that skb->vlan_tci had
474 * VLAN_TAG_PRESENT bit and its VID field was 0x000.
475 * We update only VID field and preserve PCP field.
477 skb
->vlan_tci
|= pvid
;
479 /* if stats are disabled we can avoid the lookup */
480 if (!br
->vlan_stats_enabled
)
483 v
= br_vlan_find(vg
, *vid
);
484 if (!v
|| !br_vlan_should_use(v
))
487 if (br
->vlan_stats_enabled
) {
488 stats
= this_cpu_ptr(v
->stats
);
489 u64_stats_update_begin(&stats
->syncp
);
490 stats
->rx_bytes
+= skb
->len
;
492 u64_stats_update_end(&stats
->syncp
);
502 bool br_allowed_ingress(const struct net_bridge
*br
,
503 struct net_bridge_vlan_group
*vg
, struct sk_buff
*skb
,
506 /* If VLAN filtering is disabled on the bridge, all packets are
509 if (!br
->vlan_enabled
) {
510 BR_INPUT_SKB_CB(skb
)->vlan_filtered
= false;
514 return __allowed_ingress(br
, vg
, skb
, vid
);
517 /* Called under RCU. */
518 bool br_allowed_egress(struct net_bridge_vlan_group
*vg
,
519 const struct sk_buff
*skb
)
521 const struct net_bridge_vlan
*v
;
524 /* If this packet was not filtered at input, let it pass */
525 if (!BR_INPUT_SKB_CB(skb
)->vlan_filtered
)
528 br_vlan_get_tag(skb
, &vid
);
529 v
= br_vlan_find(vg
, vid
);
530 if (v
&& br_vlan_should_use(v
))
536 /* Called under RCU */
537 bool br_should_learn(struct net_bridge_port
*p
, struct sk_buff
*skb
, u16
*vid
)
539 struct net_bridge_vlan_group
*vg
;
540 struct net_bridge
*br
= p
->br
;
542 /* If filtering was disabled at input, let it pass. */
543 if (!br
->vlan_enabled
)
546 vg
= nbp_vlan_group_rcu(p
);
547 if (!vg
|| !vg
->num_vlans
)
550 if (!br_vlan_get_tag(skb
, vid
) && skb
->vlan_proto
!= br
->vlan_proto
)
554 *vid
= br_get_pvid(vg
);
561 if (br_vlan_find(vg
, *vid
))
567 /* Must be protected by RTNL.
568 * Must be called with vid in range from 1 to 4094 inclusive.
569 * changed must be true only if the vlan was created or updated
571 int br_vlan_add(struct net_bridge
*br
, u16 vid
, u16 flags
, bool *changed
)
573 struct net_bridge_vlan_group
*vg
;
574 struct net_bridge_vlan
*vlan
;
580 vg
= br_vlan_group(br
);
581 vlan
= br_vlan_find(vg
, vid
);
583 if (!br_vlan_is_brentry(vlan
)) {
584 /* Trying to change flags of non-existent bridge vlan */
585 if (!(flags
& BRIDGE_VLAN_INFO_BRENTRY
))
587 /* It was only kept for port vlans, now make it real */
588 ret
= br_fdb_insert(br
, NULL
, br
->dev
->dev_addr
,
591 br_err(br
, "failed insert local address into bridge forwarding table\n");
594 refcount_inc(&vlan
->refcnt
);
595 vlan
->flags
|= BRIDGE_VLAN_INFO_BRENTRY
;
599 if (__vlan_add_flags(vlan
, flags
))
605 vlan
= kzalloc(sizeof(*vlan
), GFP_KERNEL
);
609 vlan
->stats
= netdev_alloc_pcpu_stats(struct br_vlan_stats
);
615 vlan
->flags
= flags
| BRIDGE_VLAN_INFO_MASTER
;
616 vlan
->flags
&= ~BRIDGE_VLAN_INFO_PVID
;
618 if (flags
& BRIDGE_VLAN_INFO_BRENTRY
)
619 refcount_set(&vlan
->refcnt
, 1);
620 ret
= __vlan_add(vlan
, flags
);
622 free_percpu(vlan
->stats
);
631 /* Must be protected by RTNL.
632 * Must be called with vid in range from 1 to 4094 inclusive.
634 int br_vlan_delete(struct net_bridge
*br
, u16 vid
)
636 struct net_bridge_vlan_group
*vg
;
637 struct net_bridge_vlan
*v
;
641 vg
= br_vlan_group(br
);
642 v
= br_vlan_find(vg
, vid
);
643 if (!v
|| !br_vlan_is_brentry(v
))
646 br_fdb_find_delete_local(br
, NULL
, br
->dev
->dev_addr
, vid
);
647 br_fdb_delete_by_port(br
, NULL
, vid
, 0);
649 vlan_tunnel_info_del(vg
, v
);
651 return __vlan_del(v
);
654 void br_vlan_flush(struct net_bridge
*br
)
656 struct net_bridge_vlan_group
*vg
;
660 vg
= br_vlan_group(br
);
662 RCU_INIT_POINTER(br
->vlgrp
, NULL
);
664 __vlan_group_free(vg
);
667 struct net_bridge_vlan
*br_vlan_find(struct net_bridge_vlan_group
*vg
, u16 vid
)
672 return br_vlan_lookup(&vg
->vlan_hash
, vid
);
675 /* Must be protected by RTNL. */
676 static void recalculate_group_addr(struct net_bridge
*br
)
678 if (br
->group_addr_set
)
681 spin_lock_bh(&br
->lock
);
682 if (!br
->vlan_enabled
|| br
->vlan_proto
== htons(ETH_P_8021Q
)) {
683 /* Bridge Group Address */
684 br
->group_addr
[5] = 0x00;
685 } else { /* vlan_enabled && ETH_P_8021AD */
686 /* Provider Bridge Group Address */
687 br
->group_addr
[5] = 0x08;
689 spin_unlock_bh(&br
->lock
);
692 /* Must be protected by RTNL. */
693 void br_recalculate_fwd_mask(struct net_bridge
*br
)
695 if (!br
->vlan_enabled
|| br
->vlan_proto
== htons(ETH_P_8021Q
))
696 br
->group_fwd_mask_required
= BR_GROUPFWD_DEFAULT
;
697 else /* vlan_enabled && ETH_P_8021AD */
698 br
->group_fwd_mask_required
= BR_GROUPFWD_8021AD
&
699 ~(1u << br
->group_addr
[5]);
702 int __br_vlan_filter_toggle(struct net_bridge
*br
, unsigned long val
)
704 struct switchdev_attr attr
= {
706 .id
= SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING
,
707 .flags
= SWITCHDEV_F_SKIP_EOPNOTSUPP
,
708 .u
.vlan_filtering
= val
,
712 if (br
->vlan_enabled
== val
)
715 err
= switchdev_port_attr_set(br
->dev
, &attr
);
716 if (err
&& err
!= -EOPNOTSUPP
)
719 br
->vlan_enabled
= val
;
720 br_manage_promisc(br
);
721 recalculate_group_addr(br
);
722 br_recalculate_fwd_mask(br
);
727 int br_vlan_filter_toggle(struct net_bridge
*br
, unsigned long val
)
729 return __br_vlan_filter_toggle(br
, val
);
732 bool br_vlan_enabled(const struct net_device
*dev
)
734 struct net_bridge
*br
= netdev_priv(dev
);
736 return !!br
->vlan_enabled
;
738 EXPORT_SYMBOL_GPL(br_vlan_enabled
);
740 int __br_vlan_set_proto(struct net_bridge
*br
, __be16 proto
)
743 struct net_bridge_port
*p
;
744 struct net_bridge_vlan
*vlan
;
745 struct net_bridge_vlan_group
*vg
;
748 if (br
->vlan_proto
== proto
)
751 /* Add VLANs for the new proto to the device filter. */
752 list_for_each_entry(p
, &br
->port_list
, list
) {
753 vg
= nbp_vlan_group(p
);
754 list_for_each_entry(vlan
, &vg
->vlan_list
, vlist
) {
755 err
= vlan_vid_add(p
->dev
, proto
, vlan
->vid
);
761 oldproto
= br
->vlan_proto
;
762 br
->vlan_proto
= proto
;
764 recalculate_group_addr(br
);
765 br_recalculate_fwd_mask(br
);
767 /* Delete VLANs for the old proto from the device filter. */
768 list_for_each_entry(p
, &br
->port_list
, list
) {
769 vg
= nbp_vlan_group(p
);
770 list_for_each_entry(vlan
, &vg
->vlan_list
, vlist
)
771 vlan_vid_del(p
->dev
, oldproto
, vlan
->vid
);
777 list_for_each_entry_continue_reverse(vlan
, &vg
->vlan_list
, vlist
)
778 vlan_vid_del(p
->dev
, proto
, vlan
->vid
);
780 list_for_each_entry_continue_reverse(p
, &br
->port_list
, list
) {
781 vg
= nbp_vlan_group(p
);
782 list_for_each_entry(vlan
, &vg
->vlan_list
, vlist
)
783 vlan_vid_del(p
->dev
, proto
, vlan
->vid
);
789 int br_vlan_set_proto(struct net_bridge
*br
, unsigned long val
)
791 if (val
!= ETH_P_8021Q
&& val
!= ETH_P_8021AD
)
792 return -EPROTONOSUPPORT
;
794 return __br_vlan_set_proto(br
, htons(val
));
797 int br_vlan_set_stats(struct net_bridge
*br
, unsigned long val
)
802 br
->vlan_stats_enabled
= val
;
811 static bool vlan_default_pvid(struct net_bridge_vlan_group
*vg
, u16 vid
)
813 struct net_bridge_vlan
*v
;
818 v
= br_vlan_lookup(&vg
->vlan_hash
, vid
);
819 if (v
&& br_vlan_should_use(v
) &&
820 (v
->flags
& BRIDGE_VLAN_INFO_UNTAGGED
))
826 static void br_vlan_disable_default_pvid(struct net_bridge
*br
)
828 struct net_bridge_port
*p
;
829 u16 pvid
= br
->default_pvid
;
831 /* Disable default_pvid on all ports where it is still
834 if (vlan_default_pvid(br_vlan_group(br
), pvid
))
835 br_vlan_delete(br
, pvid
);
837 list_for_each_entry(p
, &br
->port_list
, list
) {
838 if (vlan_default_pvid(nbp_vlan_group(p
), pvid
))
839 nbp_vlan_delete(p
, pvid
);
842 br
->default_pvid
= 0;
845 int __br_vlan_set_default_pvid(struct net_bridge
*br
, u16 pvid
)
847 const struct net_bridge_vlan
*pvent
;
848 struct net_bridge_vlan_group
*vg
;
849 struct net_bridge_port
*p
;
850 unsigned long *changed
;
856 br_vlan_disable_default_pvid(br
);
860 changed
= kcalloc(BITS_TO_LONGS(BR_MAX_PORTS
), sizeof(unsigned long),
865 old_pvid
= br
->default_pvid
;
867 /* Update default_pvid config only if we do not conflict with
868 * user configuration.
870 vg
= br_vlan_group(br
);
871 pvent
= br_vlan_find(vg
, pvid
);
872 if ((!old_pvid
|| vlan_default_pvid(vg
, old_pvid
)) &&
873 (!pvent
|| !br_vlan_should_use(pvent
))) {
874 err
= br_vlan_add(br
, pvid
,
875 BRIDGE_VLAN_INFO_PVID
|
876 BRIDGE_VLAN_INFO_UNTAGGED
|
877 BRIDGE_VLAN_INFO_BRENTRY
,
881 br_vlan_delete(br
, old_pvid
);
885 list_for_each_entry(p
, &br
->port_list
, list
) {
886 /* Update default_pvid config only if we do not conflict with
887 * user configuration.
889 vg
= nbp_vlan_group(p
);
891 !vlan_default_pvid(vg
, old_pvid
)) ||
892 br_vlan_find(vg
, pvid
))
895 err
= nbp_vlan_add(p
, pvid
,
896 BRIDGE_VLAN_INFO_PVID
|
897 BRIDGE_VLAN_INFO_UNTAGGED
,
901 nbp_vlan_delete(p
, old_pvid
);
902 set_bit(p
->port_no
, changed
);
905 br
->default_pvid
= pvid
;
912 list_for_each_entry_continue_reverse(p
, &br
->port_list
, list
) {
913 if (!test_bit(p
->port_no
, changed
))
917 nbp_vlan_add(p
, old_pvid
,
918 BRIDGE_VLAN_INFO_PVID
|
919 BRIDGE_VLAN_INFO_UNTAGGED
,
921 nbp_vlan_delete(p
, pvid
);
924 if (test_bit(0, changed
)) {
926 br_vlan_add(br
, old_pvid
,
927 BRIDGE_VLAN_INFO_PVID
|
928 BRIDGE_VLAN_INFO_UNTAGGED
|
929 BRIDGE_VLAN_INFO_BRENTRY
,
931 br_vlan_delete(br
, pvid
);
936 int br_vlan_set_default_pvid(struct net_bridge
*br
, unsigned long val
)
941 if (val
>= VLAN_VID_MASK
)
944 if (pvid
== br
->default_pvid
)
947 /* Only allow default pvid change when filtering is disabled */
948 if (br
->vlan_enabled
) {
949 pr_info_once("Please disable vlan filtering to change default_pvid\n");
953 err
= __br_vlan_set_default_pvid(br
, pvid
);
958 int br_vlan_init(struct net_bridge
*br
)
960 struct net_bridge_vlan_group
*vg
;
964 vg
= kzalloc(sizeof(*vg
), GFP_KERNEL
);
967 ret
= rhashtable_init(&vg
->vlan_hash
, &br_vlan_rht_params
);
970 ret
= vlan_tunnel_init(vg
);
972 goto err_tunnel_init
;
973 INIT_LIST_HEAD(&vg
->vlan_list
);
974 br
->vlan_proto
= htons(ETH_P_8021Q
);
975 br
->default_pvid
= 1;
976 rcu_assign_pointer(br
->vlgrp
, vg
);
977 ret
= br_vlan_add(br
, 1,
978 BRIDGE_VLAN_INFO_PVID
| BRIDGE_VLAN_INFO_UNTAGGED
|
979 BRIDGE_VLAN_INFO_BRENTRY
, &changed
);
987 vlan_tunnel_deinit(vg
);
989 rhashtable_destroy(&vg
->vlan_hash
);
996 int nbp_vlan_init(struct net_bridge_port
*p
)
998 struct switchdev_attr attr
= {
999 .orig_dev
= p
->br
->dev
,
1000 .id
= SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING
,
1001 .flags
= SWITCHDEV_F_SKIP_EOPNOTSUPP
,
1002 .u
.vlan_filtering
= p
->br
->vlan_enabled
,
1004 struct net_bridge_vlan_group
*vg
;
1007 vg
= kzalloc(sizeof(struct net_bridge_vlan_group
), GFP_KERNEL
);
1011 ret
= switchdev_port_attr_set(p
->dev
, &attr
);
1012 if (ret
&& ret
!= -EOPNOTSUPP
)
1013 goto err_vlan_enabled
;
1015 ret
= rhashtable_init(&vg
->vlan_hash
, &br_vlan_rht_params
);
1018 ret
= vlan_tunnel_init(vg
);
1020 goto err_tunnel_init
;
1021 INIT_LIST_HEAD(&vg
->vlan_list
);
1022 rcu_assign_pointer(p
->vlgrp
, vg
);
1023 if (p
->br
->default_pvid
) {
1026 ret
= nbp_vlan_add(p
, p
->br
->default_pvid
,
1027 BRIDGE_VLAN_INFO_PVID
|
1028 BRIDGE_VLAN_INFO_UNTAGGED
,
1037 RCU_INIT_POINTER(p
->vlgrp
, NULL
);
1039 vlan_tunnel_deinit(vg
);
1041 rhashtable_destroy(&vg
->vlan_hash
);
1049 /* Must be protected by RTNL.
1050 * Must be called with vid in range from 1 to 4094 inclusive.
1051 * changed must be true only if the vlan was created or updated
1053 int nbp_vlan_add(struct net_bridge_port
*port
, u16 vid
, u16 flags
,
1056 struct switchdev_obj_port_vlan v
= {
1057 .obj
.orig_dev
= port
->dev
,
1058 .obj
.id
= SWITCHDEV_OBJ_ID_PORT_VLAN
,
1063 struct net_bridge_vlan
*vlan
;
1069 vlan
= br_vlan_find(nbp_vlan_group(port
), vid
);
1071 /* Pass the flags to the hardware bridge */
1072 ret
= switchdev_port_obj_add(port
->dev
, &v
.obj
);
1073 if (ret
&& ret
!= -EOPNOTSUPP
)
1075 *changed
= __vlan_add_flags(vlan
, flags
);
1080 vlan
= kzalloc(sizeof(*vlan
), GFP_KERNEL
);
1086 ret
= __vlan_add(vlan
, flags
);
1095 /* Must be protected by RTNL.
1096 * Must be called with vid in range from 1 to 4094 inclusive.
1098 int nbp_vlan_delete(struct net_bridge_port
*port
, u16 vid
)
1100 struct net_bridge_vlan
*v
;
1104 v
= br_vlan_find(nbp_vlan_group(port
), vid
);
1107 br_fdb_find_delete_local(port
->br
, port
, port
->dev
->dev_addr
, vid
);
1108 br_fdb_delete_by_port(port
->br
, port
, vid
, 0);
1110 return __vlan_del(v
);
1113 void nbp_vlan_flush(struct net_bridge_port
*port
)
1115 struct net_bridge_vlan_group
*vg
;
1119 vg
= nbp_vlan_group(port
);
1121 RCU_INIT_POINTER(port
->vlgrp
, NULL
);
1123 __vlan_group_free(vg
);
1126 void br_vlan_get_stats(const struct net_bridge_vlan
*v
,
1127 struct br_vlan_stats
*stats
)
1131 memset(stats
, 0, sizeof(*stats
));
1132 for_each_possible_cpu(i
) {
1133 u64 rxpackets
, rxbytes
, txpackets
, txbytes
;
1134 struct br_vlan_stats
*cpu_stats
;
1137 cpu_stats
= per_cpu_ptr(v
->stats
, i
);
1139 start
= u64_stats_fetch_begin_irq(&cpu_stats
->syncp
);
1140 rxpackets
= cpu_stats
->rx_packets
;
1141 rxbytes
= cpu_stats
->rx_bytes
;
1142 txbytes
= cpu_stats
->tx_bytes
;
1143 txpackets
= cpu_stats
->tx_packets
;
1144 } while (u64_stats_fetch_retry_irq(&cpu_stats
->syncp
, start
));
1146 stats
->rx_packets
+= rxpackets
;
1147 stats
->rx_bytes
+= rxbytes
;
1148 stats
->tx_bytes
+= txbytes
;
1149 stats
->tx_packets
+= txpackets
;