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"
9 static inline int br_vlan_cmp(struct rhashtable_compare_arg
*arg
,
12 const struct net_bridge_vlan
*vle
= ptr
;
13 u16 vid
= *(u16
*)arg
->key
;
15 return vle
->vid
!= vid
;
18 static const struct rhashtable_params br_vlan_rht_params
= {
19 .head_offset
= offsetof(struct net_bridge_vlan
, vnode
),
20 .key_offset
= offsetof(struct net_bridge_vlan
, vid
),
21 .key_len
= sizeof(u16
),
24 .max_size
= VLAN_N_VID
,
25 .obj_cmpfn
= br_vlan_cmp
,
26 .automatic_shrinking
= true,
29 static struct net_bridge_vlan
*br_vlan_lookup(struct rhashtable
*tbl
, u16 vid
)
31 return rhashtable_lookup_fast(tbl
, &vid
, br_vlan_rht_params
);
34 static void __vlan_add_pvid(struct net_bridge_vlan_group
*vg
, u16 vid
)
43 static void __vlan_delete_pvid(struct net_bridge_vlan_group
*vg
, u16 vid
)
52 static void __vlan_add_flags(struct net_bridge_vlan
*v
, u16 flags
)
54 struct net_bridge_vlan_group
*vg
;
56 if (br_vlan_is_master(v
))
57 vg
= br_vlan_group(v
->br
);
59 vg
= nbp_vlan_group(v
->port
);
61 if (flags
& BRIDGE_VLAN_INFO_PVID
)
62 __vlan_add_pvid(vg
, v
->vid
);
64 __vlan_delete_pvid(vg
, v
->vid
);
66 if (flags
& BRIDGE_VLAN_INFO_UNTAGGED
)
67 v
->flags
|= BRIDGE_VLAN_INFO_UNTAGGED
;
69 v
->flags
&= ~BRIDGE_VLAN_INFO_UNTAGGED
;
72 static int __vlan_vid_add(struct net_device
*dev
, struct net_bridge
*br
,
75 struct switchdev_obj_port_vlan v
= {
77 .obj
.id
= SWITCHDEV_OBJ_ID_PORT_VLAN
,
84 /* Try switchdev op first. In case it is not supported, fallback to
87 err
= switchdev_port_obj_add(dev
, &v
.obj
);
88 if (err
== -EOPNOTSUPP
)
89 return vlan_vid_add(dev
, br
->vlan_proto
, vid
);
93 static void __vlan_add_list(struct net_bridge_vlan
*v
)
95 struct net_bridge_vlan_group
*vg
;
96 struct list_head
*headp
, *hpos
;
97 struct net_bridge_vlan
*vent
;
99 if (br_vlan_is_master(v
))
100 vg
= br_vlan_group(v
->br
);
102 vg
= nbp_vlan_group(v
->port
);
104 headp
= &vg
->vlan_list
;
105 list_for_each_prev(hpos
, headp
) {
106 vent
= list_entry(hpos
, struct net_bridge_vlan
, vlist
);
107 if (v
->vid
< vent
->vid
)
112 list_add_rcu(&v
->vlist
, hpos
);
115 static void __vlan_del_list(struct net_bridge_vlan
*v
)
117 list_del_rcu(&v
->vlist
);
120 static int __vlan_vid_del(struct net_device
*dev
, struct net_bridge
*br
,
123 struct switchdev_obj_port_vlan v
= {
125 .obj
.id
= SWITCHDEV_OBJ_ID_PORT_VLAN
,
131 /* Try switchdev op first. In case it is not supported, fallback to
134 err
= switchdev_port_obj_del(dev
, &v
.obj
);
135 if (err
== -EOPNOTSUPP
) {
136 vlan_vid_del(dev
, br
->vlan_proto
, vid
);
142 /* Returns a master vlan, if it didn't exist it gets created. In all cases a
143 * a reference is taken to the master vlan before returning.
145 static struct net_bridge_vlan
*br_vlan_get_master(struct net_bridge
*br
, u16 vid
)
147 struct net_bridge_vlan_group
*vg
;
148 struct net_bridge_vlan
*masterv
;
150 vg
= br_vlan_group(br
);
151 masterv
= br_vlan_find(vg
, vid
);
153 /* missing global ctx, create it now */
154 if (br_vlan_add(br
, vid
, 0))
156 masterv
= br_vlan_find(vg
, vid
);
157 if (WARN_ON(!masterv
))
160 atomic_inc(&masterv
->refcnt
);
165 static void br_master_vlan_rcu_free(struct rcu_head
*rcu
)
167 struct net_bridge_vlan
*v
;
169 v
= container_of(rcu
, struct net_bridge_vlan
, rcu
);
170 WARN_ON(!br_vlan_is_master(v
));
171 free_percpu(v
->stats
);
176 static void br_vlan_put_master(struct net_bridge_vlan
*masterv
)
178 struct net_bridge_vlan_group
*vg
;
180 if (!br_vlan_is_master(masterv
))
183 vg
= br_vlan_group(masterv
->br
);
184 if (atomic_dec_and_test(&masterv
->refcnt
)) {
185 rhashtable_remove_fast(&vg
->vlan_hash
,
186 &masterv
->vnode
, br_vlan_rht_params
);
187 __vlan_del_list(masterv
);
188 call_rcu(&masterv
->rcu
, br_master_vlan_rcu_free
);
192 /* This is the shared VLAN add function which works for both ports and bridge
193 * devices. There are four possible calls to this function in terms of the
195 * 1. vlan is being added on a port (no master flags, global entry exists)
196 * 2. vlan is being added on a bridge (both master and brentry flags)
197 * 3. vlan is being added on a port, but a global entry didn't exist which
198 * is being created right now (master flag set, brentry flag unset), the
199 * global entry is used for global per-vlan features, but not for filtering
200 * 4. same as 3 but with both master and brentry flags set so the entry
201 * will be used for filtering in both the port and the bridge
203 static int __vlan_add(struct net_bridge_vlan
*v
, u16 flags
)
205 struct net_bridge_vlan
*masterv
= NULL
;
206 struct net_bridge_port
*p
= NULL
;
207 struct net_bridge_vlan_group
*vg
;
208 struct net_device
*dev
;
209 struct net_bridge
*br
;
212 if (br_vlan_is_master(v
)) {
215 vg
= br_vlan_group(br
);
220 vg
= nbp_vlan_group(p
);
224 /* Add VLAN to the device filter if it is supported.
225 * This ensures tagged traffic enters the bridge when
226 * promiscuous mode is disabled by br_manage_promisc().
228 err
= __vlan_vid_add(dev
, br
, v
->vid
, flags
);
232 /* need to work on the master vlan too */
233 if (flags
& BRIDGE_VLAN_INFO_MASTER
) {
234 err
= br_vlan_add(br
, v
->vid
, flags
|
235 BRIDGE_VLAN_INFO_BRENTRY
);
240 masterv
= br_vlan_get_master(br
, v
->vid
);
244 v
->stats
= masterv
->stats
;
247 /* Add the dev mac and count the vlan only if it's usable */
248 if (br_vlan_should_use(v
)) {
249 err
= br_fdb_insert(br
, p
, dev
->dev_addr
, v
->vid
);
251 br_err(br
, "failed insert local address into bridge forwarding table\n");
257 err
= rhashtable_lookup_insert_fast(&vg
->vlan_hash
, &v
->vnode
,
263 __vlan_add_flags(v
, flags
);
268 if (br_vlan_should_use(v
)) {
269 br_fdb_find_delete_local(br
, p
, dev
->dev_addr
, v
->vid
);
275 __vlan_vid_del(dev
, br
, v
->vid
);
277 br_vlan_put_master(masterv
);
285 static int __vlan_del(struct net_bridge_vlan
*v
)
287 struct net_bridge_vlan
*masterv
= v
;
288 struct net_bridge_vlan_group
*vg
;
289 struct net_bridge_port
*p
= NULL
;
292 if (br_vlan_is_master(v
)) {
293 vg
= br_vlan_group(v
->br
);
296 vg
= nbp_vlan_group(v
->port
);
300 __vlan_delete_pvid(vg
, v
->vid
);
302 err
= __vlan_vid_del(p
->dev
, p
->br
, v
->vid
);
307 if (br_vlan_should_use(v
)) {
308 v
->flags
&= ~BRIDGE_VLAN_INFO_BRENTRY
;
313 rhashtable_remove_fast(&vg
->vlan_hash
, &v
->vnode
,
319 br_vlan_put_master(masterv
);
324 static void __vlan_group_free(struct net_bridge_vlan_group
*vg
)
326 WARN_ON(!list_empty(&vg
->vlan_list
));
327 rhashtable_destroy(&vg
->vlan_hash
);
331 static void __vlan_flush(struct net_bridge_vlan_group
*vg
)
333 struct net_bridge_vlan
*vlan
, *tmp
;
335 __vlan_delete_pvid(vg
, vg
->pvid
);
336 list_for_each_entry_safe(vlan
, tmp
, &vg
->vlan_list
, vlist
)
340 struct sk_buff
*br_handle_vlan(struct net_bridge
*br
,
341 struct net_bridge_vlan_group
*vg
,
344 struct br_vlan_stats
*stats
;
345 struct net_bridge_vlan
*v
;
348 /* If this packet was not filtered at input, let it pass */
349 if (!BR_INPUT_SKB_CB(skb
)->vlan_filtered
)
352 /* At this point, we know that the frame was filtered and contains
353 * a valid vlan id. If the vlan id has untagged flag set,
354 * send untagged; otherwise, send tagged.
356 br_vlan_get_tag(skb
, &vid
);
357 v
= br_vlan_find(vg
, vid
);
358 /* Vlan entry must be configured at this point. The
359 * only exception is the bridge is set in promisc mode and the
360 * packet is destined for the bridge device. In this case
361 * pass the packet as is.
363 if (!v
|| !br_vlan_should_use(v
)) {
364 if ((br
->dev
->flags
& IFF_PROMISC
) && skb
->dev
== br
->dev
) {
371 if (br
->vlan_stats_enabled
) {
372 stats
= this_cpu_ptr(v
->stats
);
373 u64_stats_update_begin(&stats
->syncp
);
374 stats
->tx_bytes
+= skb
->len
;
376 u64_stats_update_end(&stats
->syncp
);
379 if (v
->flags
& BRIDGE_VLAN_INFO_UNTAGGED
)
385 /* Called under RCU */
386 static bool __allowed_ingress(const struct net_bridge
*br
,
387 struct net_bridge_vlan_group
*vg
,
388 struct sk_buff
*skb
, u16
*vid
)
390 struct br_vlan_stats
*stats
;
391 struct net_bridge_vlan
*v
;
394 BR_INPUT_SKB_CB(skb
)->vlan_filtered
= true;
395 /* If vlan tx offload is disabled on bridge device and frame was
396 * sent from vlan device on the bridge device, it does not have
397 * HW accelerated vlan tag.
399 if (unlikely(!skb_vlan_tag_present(skb
) &&
400 skb
->protocol
== br
->vlan_proto
)) {
401 skb
= skb_vlan_untag(skb
);
406 if (!br_vlan_get_tag(skb
, vid
)) {
408 if (skb
->vlan_proto
!= br
->vlan_proto
) {
409 /* Protocol-mismatch, empty out vlan_tci for new tag */
410 skb_push(skb
, ETH_HLEN
);
411 skb
= vlan_insert_tag_set_proto(skb
, skb
->vlan_proto
,
412 skb_vlan_tag_get(skb
));
416 skb_pull(skb
, ETH_HLEN
);
417 skb_reset_mac_len(skb
);
429 u16 pvid
= br_get_pvid(vg
);
431 /* Frame had a tag with VID 0 or did not have a tag.
432 * See if pvid is set on this port. That tells us which
433 * vlan untagged or priority-tagged traffic belongs to.
438 /* PVID is set on this port. Any untagged or priority-tagged
439 * ingress frame is considered to belong to this vlan.
443 /* Untagged Frame. */
444 __vlan_hwaccel_put_tag(skb
, br
->vlan_proto
, pvid
);
446 /* Priority-tagged Frame.
447 * At this point, We know that skb->vlan_tci had
448 * VLAN_TAG_PRESENT bit and its VID field was 0x000.
449 * We update only VID field and preserve PCP field.
451 skb
->vlan_tci
|= pvid
;
453 /* if stats are disabled we can avoid the lookup */
454 if (!br
->vlan_stats_enabled
)
457 v
= br_vlan_find(vg
, *vid
);
458 if (!v
|| !br_vlan_should_use(v
))
461 if (br
->vlan_stats_enabled
) {
462 stats
= this_cpu_ptr(v
->stats
);
463 u64_stats_update_begin(&stats
->syncp
);
464 stats
->rx_bytes
+= skb
->len
;
466 u64_stats_update_end(&stats
->syncp
);
476 bool br_allowed_ingress(const struct net_bridge
*br
,
477 struct net_bridge_vlan_group
*vg
, struct sk_buff
*skb
,
480 /* If VLAN filtering is disabled on the bridge, all packets are
483 if (!br
->vlan_enabled
) {
484 BR_INPUT_SKB_CB(skb
)->vlan_filtered
= false;
488 return __allowed_ingress(br
, vg
, skb
, vid
);
491 /* Called under RCU. */
492 bool br_allowed_egress(struct net_bridge_vlan_group
*vg
,
493 const struct sk_buff
*skb
)
495 const struct net_bridge_vlan
*v
;
498 /* If this packet was not filtered at input, let it pass */
499 if (!BR_INPUT_SKB_CB(skb
)->vlan_filtered
)
502 br_vlan_get_tag(skb
, &vid
);
503 v
= br_vlan_find(vg
, vid
);
504 if (v
&& br_vlan_should_use(v
))
510 /* Called under RCU */
511 bool br_should_learn(struct net_bridge_port
*p
, struct sk_buff
*skb
, u16
*vid
)
513 struct net_bridge_vlan_group
*vg
;
514 struct net_bridge
*br
= p
->br
;
516 /* If filtering was disabled at input, let it pass. */
517 if (!br
->vlan_enabled
)
520 vg
= nbp_vlan_group_rcu(p
);
521 if (!vg
|| !vg
->num_vlans
)
524 if (!br_vlan_get_tag(skb
, vid
) && skb
->vlan_proto
!= br
->vlan_proto
)
528 *vid
= br_get_pvid(vg
);
535 if (br_vlan_find(vg
, *vid
))
541 /* Must be protected by RTNL.
542 * Must be called with vid in range from 1 to 4094 inclusive.
544 int br_vlan_add(struct net_bridge
*br
, u16 vid
, u16 flags
)
546 struct net_bridge_vlan_group
*vg
;
547 struct net_bridge_vlan
*vlan
;
552 vg
= br_vlan_group(br
);
553 vlan
= br_vlan_find(vg
, vid
);
555 if (!br_vlan_is_brentry(vlan
)) {
556 /* Trying to change flags of non-existent bridge vlan */
557 if (!(flags
& BRIDGE_VLAN_INFO_BRENTRY
))
559 /* It was only kept for port vlans, now make it real */
560 ret
= br_fdb_insert(br
, NULL
, br
->dev
->dev_addr
,
563 br_err(br
, "failed insert local address into bridge forwarding table\n");
566 atomic_inc(&vlan
->refcnt
);
567 vlan
->flags
|= BRIDGE_VLAN_INFO_BRENTRY
;
570 __vlan_add_flags(vlan
, flags
);
574 vlan
= kzalloc(sizeof(*vlan
), GFP_KERNEL
);
578 vlan
->stats
= netdev_alloc_pcpu_stats(struct br_vlan_stats
);
584 vlan
->flags
= flags
| BRIDGE_VLAN_INFO_MASTER
;
585 vlan
->flags
&= ~BRIDGE_VLAN_INFO_PVID
;
587 if (flags
& BRIDGE_VLAN_INFO_BRENTRY
)
588 atomic_set(&vlan
->refcnt
, 1);
589 ret
= __vlan_add(vlan
, flags
);
591 free_percpu(vlan
->stats
);
598 /* Must be protected by RTNL.
599 * Must be called with vid in range from 1 to 4094 inclusive.
601 int br_vlan_delete(struct net_bridge
*br
, u16 vid
)
603 struct net_bridge_vlan_group
*vg
;
604 struct net_bridge_vlan
*v
;
608 vg
= br_vlan_group(br
);
609 v
= br_vlan_find(vg
, vid
);
610 if (!v
|| !br_vlan_is_brentry(v
))
613 br_fdb_find_delete_local(br
, NULL
, br
->dev
->dev_addr
, vid
);
614 br_fdb_delete_by_port(br
, NULL
, vid
, 0);
616 return __vlan_del(v
);
619 void br_vlan_flush(struct net_bridge
*br
)
621 struct net_bridge_vlan_group
*vg
;
625 vg
= br_vlan_group(br
);
627 RCU_INIT_POINTER(br
->vlgrp
, NULL
);
629 __vlan_group_free(vg
);
632 struct net_bridge_vlan
*br_vlan_find(struct net_bridge_vlan_group
*vg
, u16 vid
)
637 return br_vlan_lookup(&vg
->vlan_hash
, vid
);
640 /* Must be protected by RTNL. */
641 static void recalculate_group_addr(struct net_bridge
*br
)
643 if (br
->group_addr_set
)
646 spin_lock_bh(&br
->lock
);
647 if (!br
->vlan_enabled
|| br
->vlan_proto
== htons(ETH_P_8021Q
)) {
648 /* Bridge Group Address */
649 br
->group_addr
[5] = 0x00;
650 } else { /* vlan_enabled && ETH_P_8021AD */
651 /* Provider Bridge Group Address */
652 br
->group_addr
[5] = 0x08;
654 spin_unlock_bh(&br
->lock
);
657 /* Must be protected by RTNL. */
658 void br_recalculate_fwd_mask(struct net_bridge
*br
)
660 if (!br
->vlan_enabled
|| br
->vlan_proto
== htons(ETH_P_8021Q
))
661 br
->group_fwd_mask_required
= BR_GROUPFWD_DEFAULT
;
662 else /* vlan_enabled && ETH_P_8021AD */
663 br
->group_fwd_mask_required
= BR_GROUPFWD_8021AD
&
664 ~(1u << br
->group_addr
[5]);
667 int __br_vlan_filter_toggle(struct net_bridge
*br
, unsigned long val
)
669 struct switchdev_attr attr
= {
671 .id
= SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING
,
672 .flags
= SWITCHDEV_F_SKIP_EOPNOTSUPP
,
673 .u
.vlan_filtering
= val
,
677 if (br
->vlan_enabled
== val
)
680 err
= switchdev_port_attr_set(br
->dev
, &attr
);
681 if (err
&& err
!= -EOPNOTSUPP
)
684 br
->vlan_enabled
= val
;
685 br_manage_promisc(br
);
686 recalculate_group_addr(br
);
687 br_recalculate_fwd_mask(br
);
692 int br_vlan_filter_toggle(struct net_bridge
*br
, unsigned long val
)
694 return __br_vlan_filter_toggle(br
, val
);
697 int __br_vlan_set_proto(struct net_bridge
*br
, __be16 proto
)
700 struct net_bridge_port
*p
;
701 struct net_bridge_vlan
*vlan
;
702 struct net_bridge_vlan_group
*vg
;
705 if (br
->vlan_proto
== proto
)
708 /* Add VLANs for the new proto to the device filter. */
709 list_for_each_entry(p
, &br
->port_list
, list
) {
710 vg
= nbp_vlan_group(p
);
711 list_for_each_entry(vlan
, &vg
->vlan_list
, vlist
) {
712 err
= vlan_vid_add(p
->dev
, proto
, vlan
->vid
);
718 oldproto
= br
->vlan_proto
;
719 br
->vlan_proto
= proto
;
721 recalculate_group_addr(br
);
722 br_recalculate_fwd_mask(br
);
724 /* Delete VLANs for the old proto from the device filter. */
725 list_for_each_entry(p
, &br
->port_list
, list
) {
726 vg
= nbp_vlan_group(p
);
727 list_for_each_entry(vlan
, &vg
->vlan_list
, vlist
)
728 vlan_vid_del(p
->dev
, oldproto
, vlan
->vid
);
734 list_for_each_entry_continue_reverse(vlan
, &vg
->vlan_list
, vlist
)
735 vlan_vid_del(p
->dev
, proto
, vlan
->vid
);
737 list_for_each_entry_continue_reverse(p
, &br
->port_list
, list
) {
738 vg
= nbp_vlan_group(p
);
739 list_for_each_entry(vlan
, &vg
->vlan_list
, vlist
)
740 vlan_vid_del(p
->dev
, proto
, vlan
->vid
);
746 int br_vlan_set_proto(struct net_bridge
*br
, unsigned long val
)
748 if (val
!= ETH_P_8021Q
&& val
!= ETH_P_8021AD
)
749 return -EPROTONOSUPPORT
;
751 return __br_vlan_set_proto(br
, htons(val
));
754 int br_vlan_set_stats(struct net_bridge
*br
, unsigned long val
)
759 br
->vlan_stats_enabled
= val
;
768 static bool vlan_default_pvid(struct net_bridge_vlan_group
*vg
, u16 vid
)
770 struct net_bridge_vlan
*v
;
775 v
= br_vlan_lookup(&vg
->vlan_hash
, vid
);
776 if (v
&& br_vlan_should_use(v
) &&
777 (v
->flags
& BRIDGE_VLAN_INFO_UNTAGGED
))
783 static void br_vlan_disable_default_pvid(struct net_bridge
*br
)
785 struct net_bridge_port
*p
;
786 u16 pvid
= br
->default_pvid
;
788 /* Disable default_pvid on all ports where it is still
791 if (vlan_default_pvid(br_vlan_group(br
), pvid
))
792 br_vlan_delete(br
, pvid
);
794 list_for_each_entry(p
, &br
->port_list
, list
) {
795 if (vlan_default_pvid(nbp_vlan_group(p
), pvid
))
796 nbp_vlan_delete(p
, pvid
);
799 br
->default_pvid
= 0;
802 int __br_vlan_set_default_pvid(struct net_bridge
*br
, u16 pvid
)
804 const struct net_bridge_vlan
*pvent
;
805 struct net_bridge_vlan_group
*vg
;
806 struct net_bridge_port
*p
;
809 unsigned long *changed
;
812 br_vlan_disable_default_pvid(br
);
816 changed
= kcalloc(BITS_TO_LONGS(BR_MAX_PORTS
), sizeof(unsigned long),
821 old_pvid
= br
->default_pvid
;
823 /* Update default_pvid config only if we do not conflict with
824 * user configuration.
826 vg
= br_vlan_group(br
);
827 pvent
= br_vlan_find(vg
, pvid
);
828 if ((!old_pvid
|| vlan_default_pvid(vg
, old_pvid
)) &&
829 (!pvent
|| !br_vlan_should_use(pvent
))) {
830 err
= br_vlan_add(br
, pvid
,
831 BRIDGE_VLAN_INFO_PVID
|
832 BRIDGE_VLAN_INFO_UNTAGGED
|
833 BRIDGE_VLAN_INFO_BRENTRY
);
836 br_vlan_delete(br
, old_pvid
);
840 list_for_each_entry(p
, &br
->port_list
, list
) {
841 /* Update default_pvid config only if we do not conflict with
842 * user configuration.
844 vg
= nbp_vlan_group(p
);
846 !vlan_default_pvid(vg
, old_pvid
)) ||
847 br_vlan_find(vg
, pvid
))
850 err
= nbp_vlan_add(p
, pvid
,
851 BRIDGE_VLAN_INFO_PVID
|
852 BRIDGE_VLAN_INFO_UNTAGGED
);
855 nbp_vlan_delete(p
, old_pvid
);
856 set_bit(p
->port_no
, changed
);
859 br
->default_pvid
= pvid
;
866 list_for_each_entry_continue_reverse(p
, &br
->port_list
, list
) {
867 if (!test_bit(p
->port_no
, changed
))
871 nbp_vlan_add(p
, old_pvid
,
872 BRIDGE_VLAN_INFO_PVID
|
873 BRIDGE_VLAN_INFO_UNTAGGED
);
874 nbp_vlan_delete(p
, pvid
);
877 if (test_bit(0, changed
)) {
879 br_vlan_add(br
, old_pvid
,
880 BRIDGE_VLAN_INFO_PVID
|
881 BRIDGE_VLAN_INFO_UNTAGGED
|
882 BRIDGE_VLAN_INFO_BRENTRY
);
883 br_vlan_delete(br
, pvid
);
888 int br_vlan_set_default_pvid(struct net_bridge
*br
, unsigned long val
)
893 if (val
>= VLAN_VID_MASK
)
896 if (pvid
== br
->default_pvid
)
899 /* Only allow default pvid change when filtering is disabled */
900 if (br
->vlan_enabled
) {
901 pr_info_once("Please disable vlan filtering to change default_pvid\n");
905 err
= __br_vlan_set_default_pvid(br
, pvid
);
910 int br_vlan_init(struct net_bridge
*br
)
912 struct net_bridge_vlan_group
*vg
;
915 vg
= kzalloc(sizeof(*vg
), GFP_KERNEL
);
918 ret
= rhashtable_init(&vg
->vlan_hash
, &br_vlan_rht_params
);
921 INIT_LIST_HEAD(&vg
->vlan_list
);
922 br
->vlan_proto
= htons(ETH_P_8021Q
);
923 br
->default_pvid
= 1;
924 rcu_assign_pointer(br
->vlgrp
, vg
);
925 ret
= br_vlan_add(br
, 1,
926 BRIDGE_VLAN_INFO_PVID
| BRIDGE_VLAN_INFO_UNTAGGED
|
927 BRIDGE_VLAN_INFO_BRENTRY
);
935 rhashtable_destroy(&vg
->vlan_hash
);
942 int nbp_vlan_init(struct net_bridge_port
*p
)
944 struct switchdev_attr attr
= {
945 .orig_dev
= p
->br
->dev
,
946 .id
= SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING
,
947 .flags
= SWITCHDEV_F_SKIP_EOPNOTSUPP
,
948 .u
.vlan_filtering
= p
->br
->vlan_enabled
,
950 struct net_bridge_vlan_group
*vg
;
953 vg
= kzalloc(sizeof(struct net_bridge_vlan_group
), GFP_KERNEL
);
957 ret
= switchdev_port_attr_set(p
->dev
, &attr
);
958 if (ret
&& ret
!= -EOPNOTSUPP
)
959 goto err_vlan_enabled
;
961 ret
= rhashtable_init(&vg
->vlan_hash
, &br_vlan_rht_params
);
964 INIT_LIST_HEAD(&vg
->vlan_list
);
965 rcu_assign_pointer(p
->vlgrp
, vg
);
966 if (p
->br
->default_pvid
) {
967 ret
= nbp_vlan_add(p
, p
->br
->default_pvid
,
968 BRIDGE_VLAN_INFO_PVID
|
969 BRIDGE_VLAN_INFO_UNTAGGED
);
977 RCU_INIT_POINTER(p
->vlgrp
, NULL
);
979 rhashtable_destroy(&vg
->vlan_hash
);
987 /* Must be protected by RTNL.
988 * Must be called with vid in range from 1 to 4094 inclusive.
990 int nbp_vlan_add(struct net_bridge_port
*port
, u16 vid
, u16 flags
)
992 struct switchdev_obj_port_vlan v
= {
993 .obj
.orig_dev
= port
->dev
,
994 .obj
.id
= SWITCHDEV_OBJ_ID_PORT_VLAN
,
999 struct net_bridge_vlan
*vlan
;
1004 vlan
= br_vlan_find(nbp_vlan_group(port
), vid
);
1006 /* Pass the flags to the hardware bridge */
1007 ret
= switchdev_port_obj_add(port
->dev
, &v
.obj
);
1008 if (ret
&& ret
!= -EOPNOTSUPP
)
1010 __vlan_add_flags(vlan
, flags
);
1014 vlan
= kzalloc(sizeof(*vlan
), GFP_KERNEL
);
1020 ret
= __vlan_add(vlan
, flags
);
1027 /* Must be protected by RTNL.
1028 * Must be called with vid in range from 1 to 4094 inclusive.
1030 int nbp_vlan_delete(struct net_bridge_port
*port
, u16 vid
)
1032 struct net_bridge_vlan
*v
;
1036 v
= br_vlan_find(nbp_vlan_group(port
), vid
);
1039 br_fdb_find_delete_local(port
->br
, port
, port
->dev
->dev_addr
, vid
);
1040 br_fdb_delete_by_port(port
->br
, port
, vid
, 0);
1042 return __vlan_del(v
);
1045 void nbp_vlan_flush(struct net_bridge_port
*port
)
1047 struct net_bridge_vlan_group
*vg
;
1051 vg
= nbp_vlan_group(port
);
1053 RCU_INIT_POINTER(port
->vlgrp
, NULL
);
1055 __vlan_group_free(vg
);
1058 void br_vlan_get_stats(const struct net_bridge_vlan
*v
,
1059 struct br_vlan_stats
*stats
)
1063 memset(stats
, 0, sizeof(*stats
));
1064 for_each_possible_cpu(i
) {
1065 u64 rxpackets
, rxbytes
, txpackets
, txbytes
;
1066 struct br_vlan_stats
*cpu_stats
;
1069 cpu_stats
= per_cpu_ptr(v
->stats
, i
);
1071 start
= u64_stats_fetch_begin_irq(&cpu_stats
->syncp
);
1072 rxpackets
= cpu_stats
->rx_packets
;
1073 rxbytes
= cpu_stats
->rx_bytes
;
1074 txbytes
= cpu_stats
->tx_bytes
;
1075 txpackets
= cpu_stats
->tx_packets
;
1076 } while (u64_stats_fetch_retry_irq(&cpu_stats
->syncp
, start
));
1078 stats
->rx_packets
+= rxpackets
;
1079 stats
->rx_bytes
+= rxbytes
;
1080 stats
->tx_bytes
+= txbytes
;
1081 stats
->tx_packets
+= txpackets
;