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 void __vlan_add_pvid(struct net_bridge_vlan_group
*vg
, u16 vid
)
44 static void __vlan_delete_pvid(struct net_bridge_vlan_group
*vg
, u16 vid
)
53 static void __vlan_add_flags(struct net_bridge_vlan
*v
, u16 flags
)
55 struct net_bridge_vlan_group
*vg
;
57 if (br_vlan_is_master(v
))
58 vg
= br_vlan_group(v
->br
);
60 vg
= nbp_vlan_group(v
->port
);
62 if (flags
& BRIDGE_VLAN_INFO_PVID
)
63 __vlan_add_pvid(vg
, v
->vid
);
65 __vlan_delete_pvid(vg
, v
->vid
);
67 if (flags
& BRIDGE_VLAN_INFO_UNTAGGED
)
68 v
->flags
|= BRIDGE_VLAN_INFO_UNTAGGED
;
70 v
->flags
&= ~BRIDGE_VLAN_INFO_UNTAGGED
;
73 static int __vlan_vid_add(struct net_device
*dev
, struct net_bridge
*br
,
76 struct switchdev_obj_port_vlan v
= {
78 .obj
.id
= SWITCHDEV_OBJ_ID_PORT_VLAN
,
85 /* Try switchdev op first. In case it is not supported, fallback to
88 err
= switchdev_port_obj_add(dev
, &v
.obj
);
89 if (err
== -EOPNOTSUPP
)
90 return vlan_vid_add(dev
, br
->vlan_proto
, vid
);
94 static void __vlan_add_list(struct net_bridge_vlan
*v
)
96 struct net_bridge_vlan_group
*vg
;
97 struct list_head
*headp
, *hpos
;
98 struct net_bridge_vlan
*vent
;
100 if (br_vlan_is_master(v
))
101 vg
= br_vlan_group(v
->br
);
103 vg
= nbp_vlan_group(v
->port
);
105 headp
= &vg
->vlan_list
;
106 list_for_each_prev(hpos
, headp
) {
107 vent
= list_entry(hpos
, struct net_bridge_vlan
, vlist
);
108 if (v
->vid
< vent
->vid
)
113 list_add_rcu(&v
->vlist
, hpos
);
116 static void __vlan_del_list(struct net_bridge_vlan
*v
)
118 list_del_rcu(&v
->vlist
);
121 static int __vlan_vid_del(struct net_device
*dev
, struct net_bridge
*br
,
124 struct switchdev_obj_port_vlan v
= {
126 .obj
.id
= SWITCHDEV_OBJ_ID_PORT_VLAN
,
132 /* Try switchdev op first. In case it is not supported, fallback to
135 err
= switchdev_port_obj_del(dev
, &v
.obj
);
136 if (err
== -EOPNOTSUPP
) {
137 vlan_vid_del(dev
, br
->vlan_proto
, vid
);
143 /* Returns a master vlan, if it didn't exist it gets created. In all cases a
144 * a reference is taken to the master vlan before returning.
146 static struct net_bridge_vlan
*br_vlan_get_master(struct net_bridge
*br
, u16 vid
)
148 struct net_bridge_vlan_group
*vg
;
149 struct net_bridge_vlan
*masterv
;
151 vg
= br_vlan_group(br
);
152 masterv
= br_vlan_find(vg
, vid
);
154 /* missing global ctx, create it now */
155 if (br_vlan_add(br
, vid
, 0))
157 masterv
= br_vlan_find(vg
, vid
);
158 if (WARN_ON(!masterv
))
161 refcount_inc(&masterv
->refcnt
);
166 static void br_master_vlan_rcu_free(struct rcu_head
*rcu
)
168 struct net_bridge_vlan
*v
;
170 v
= container_of(rcu
, struct net_bridge_vlan
, rcu
);
171 WARN_ON(!br_vlan_is_master(v
));
172 free_percpu(v
->stats
);
177 static void br_vlan_put_master(struct net_bridge_vlan
*masterv
)
179 struct net_bridge_vlan_group
*vg
;
181 if (!br_vlan_is_master(masterv
))
184 vg
= br_vlan_group(masterv
->br
);
185 if (refcount_dec_and_test(&masterv
->refcnt
)) {
186 rhashtable_remove_fast(&vg
->vlan_hash
,
187 &masterv
->vnode
, br_vlan_rht_params
);
188 __vlan_del_list(masterv
);
189 call_rcu(&masterv
->rcu
, br_master_vlan_rcu_free
);
193 /* This is the shared VLAN add function which works for both ports and bridge
194 * devices. There are four possible calls to this function in terms of the
196 * 1. vlan is being added on a port (no master flags, global entry exists)
197 * 2. vlan is being added on a bridge (both master and brentry flags)
198 * 3. vlan is being added on a port, but a global entry didn't exist which
199 * is being created right now (master flag set, brentry flag unset), the
200 * global entry is used for global per-vlan features, but not for filtering
201 * 4. same as 3 but with both master and brentry flags set so the entry
202 * will be used for filtering in both the port and the bridge
204 static int __vlan_add(struct net_bridge_vlan
*v
, u16 flags
)
206 struct net_bridge_vlan
*masterv
= NULL
;
207 struct net_bridge_port
*p
= NULL
;
208 struct net_bridge_vlan_group
*vg
;
209 struct net_device
*dev
;
210 struct net_bridge
*br
;
213 if (br_vlan_is_master(v
)) {
216 vg
= br_vlan_group(br
);
221 vg
= nbp_vlan_group(p
);
225 /* Add VLAN to the device filter if it is supported.
226 * This ensures tagged traffic enters the bridge when
227 * promiscuous mode is disabled by br_manage_promisc().
229 err
= __vlan_vid_add(dev
, br
, v
->vid
, flags
);
233 /* need to work on the master vlan too */
234 if (flags
& BRIDGE_VLAN_INFO_MASTER
) {
235 err
= br_vlan_add(br
, v
->vid
, flags
|
236 BRIDGE_VLAN_INFO_BRENTRY
);
241 masterv
= br_vlan_get_master(br
, v
->vid
);
245 v
->stats
= masterv
->stats
;
248 /* Add the dev mac and count the vlan only if it's usable */
249 if (br_vlan_should_use(v
)) {
250 err
= br_fdb_insert(br
, p
, dev
->dev_addr
, v
->vid
);
252 br_err(br
, "failed insert local address into bridge forwarding table\n");
258 err
= rhashtable_lookup_insert_fast(&vg
->vlan_hash
, &v
->vnode
,
264 __vlan_add_flags(v
, flags
);
269 if (br_vlan_should_use(v
)) {
270 br_fdb_find_delete_local(br
, p
, dev
->dev_addr
, v
->vid
);
276 __vlan_vid_del(dev
, br
, v
->vid
);
278 br_vlan_put_master(masterv
);
286 static int __vlan_del(struct net_bridge_vlan
*v
)
288 struct net_bridge_vlan
*masterv
= v
;
289 struct net_bridge_vlan_group
*vg
;
290 struct net_bridge_port
*p
= NULL
;
293 if (br_vlan_is_master(v
)) {
294 vg
= br_vlan_group(v
->br
);
297 vg
= nbp_vlan_group(v
->port
);
301 __vlan_delete_pvid(vg
, v
->vid
);
303 err
= __vlan_vid_del(p
->dev
, p
->br
, v
->vid
);
308 if (br_vlan_should_use(v
)) {
309 v
->flags
&= ~BRIDGE_VLAN_INFO_BRENTRY
;
314 vlan_tunnel_info_del(vg
, v
);
315 rhashtable_remove_fast(&vg
->vlan_hash
, &v
->vnode
,
321 br_vlan_put_master(masterv
);
326 static void __vlan_group_free(struct net_bridge_vlan_group
*vg
)
328 WARN_ON(!list_empty(&vg
->vlan_list
));
329 rhashtable_destroy(&vg
->vlan_hash
);
330 vlan_tunnel_deinit(vg
);
334 static void __vlan_flush(struct net_bridge_vlan_group
*vg
)
336 struct net_bridge_vlan
*vlan
, *tmp
;
338 __vlan_delete_pvid(vg
, vg
->pvid
);
339 list_for_each_entry_safe(vlan
, tmp
, &vg
->vlan_list
, vlist
)
343 struct sk_buff
*br_handle_vlan(struct net_bridge
*br
,
344 const struct net_bridge_port
*p
,
345 struct net_bridge_vlan_group
*vg
,
348 struct br_vlan_stats
*stats
;
349 struct net_bridge_vlan
*v
;
352 /* If this packet was not filtered at input, let it pass */
353 if (!BR_INPUT_SKB_CB(skb
)->vlan_filtered
)
356 /* At this point, we know that the frame was filtered and contains
357 * a valid vlan id. If the vlan id has untagged flag set,
358 * send untagged; otherwise, send tagged.
360 br_vlan_get_tag(skb
, &vid
);
361 v
= br_vlan_find(vg
, vid
);
362 /* Vlan entry must be configured at this point. The
363 * only exception is the bridge is set in promisc mode and the
364 * packet is destined for the bridge device. In this case
365 * pass the packet as is.
367 if (!v
|| !br_vlan_should_use(v
)) {
368 if ((br
->dev
->flags
& IFF_PROMISC
) && skb
->dev
== br
->dev
) {
375 if (br
->vlan_stats_enabled
) {
376 stats
= this_cpu_ptr(v
->stats
);
377 u64_stats_update_begin(&stats
->syncp
);
378 stats
->tx_bytes
+= skb
->len
;
380 u64_stats_update_end(&stats
->syncp
);
383 if (v
->flags
& BRIDGE_VLAN_INFO_UNTAGGED
)
386 if (p
&& (p
->flags
& BR_VLAN_TUNNEL
) &&
387 br_handle_egress_vlan_tunnel(skb
, v
)) {
395 /* Called under RCU */
396 static bool __allowed_ingress(const struct net_bridge
*br
,
397 struct net_bridge_vlan_group
*vg
,
398 struct sk_buff
*skb
, u16
*vid
)
400 struct br_vlan_stats
*stats
;
401 struct net_bridge_vlan
*v
;
404 BR_INPUT_SKB_CB(skb
)->vlan_filtered
= true;
405 /* If vlan tx offload is disabled on bridge device and frame was
406 * sent from vlan device on the bridge device, it does not have
407 * HW accelerated vlan tag.
409 if (unlikely(!skb_vlan_tag_present(skb
) &&
410 skb
->protocol
== br
->vlan_proto
)) {
411 skb
= skb_vlan_untag(skb
);
416 if (!br_vlan_get_tag(skb
, vid
)) {
418 if (skb
->vlan_proto
!= br
->vlan_proto
) {
419 /* Protocol-mismatch, empty out vlan_tci for new tag */
420 skb_push(skb
, ETH_HLEN
);
421 skb
= vlan_insert_tag_set_proto(skb
, skb
->vlan_proto
,
422 skb_vlan_tag_get(skb
));
426 skb_pull(skb
, ETH_HLEN
);
427 skb_reset_mac_len(skb
);
439 u16 pvid
= br_get_pvid(vg
);
441 /* Frame had a tag with VID 0 or did not have a tag.
442 * See if pvid is set on this port. That tells us which
443 * vlan untagged or priority-tagged traffic belongs to.
448 /* PVID is set on this port. Any untagged or priority-tagged
449 * ingress frame is considered to belong to this vlan.
453 /* Untagged Frame. */
454 __vlan_hwaccel_put_tag(skb
, br
->vlan_proto
, pvid
);
456 /* Priority-tagged Frame.
457 * At this point, We know that skb->vlan_tci had
458 * VLAN_TAG_PRESENT bit and its VID field was 0x000.
459 * We update only VID field and preserve PCP field.
461 skb
->vlan_tci
|= pvid
;
463 /* if stats are disabled we can avoid the lookup */
464 if (!br
->vlan_stats_enabled
)
467 v
= br_vlan_find(vg
, *vid
);
468 if (!v
|| !br_vlan_should_use(v
))
471 if (br
->vlan_stats_enabled
) {
472 stats
= this_cpu_ptr(v
->stats
);
473 u64_stats_update_begin(&stats
->syncp
);
474 stats
->rx_bytes
+= skb
->len
;
476 u64_stats_update_end(&stats
->syncp
);
486 bool br_allowed_ingress(const struct net_bridge
*br
,
487 struct net_bridge_vlan_group
*vg
, struct sk_buff
*skb
,
490 /* If VLAN filtering is disabled on the bridge, all packets are
493 if (!br
->vlan_enabled
) {
494 BR_INPUT_SKB_CB(skb
)->vlan_filtered
= false;
498 return __allowed_ingress(br
, vg
, skb
, vid
);
501 /* Called under RCU. */
502 bool br_allowed_egress(struct net_bridge_vlan_group
*vg
,
503 const struct sk_buff
*skb
)
505 const struct net_bridge_vlan
*v
;
508 /* If this packet was not filtered at input, let it pass */
509 if (!BR_INPUT_SKB_CB(skb
)->vlan_filtered
)
512 br_vlan_get_tag(skb
, &vid
);
513 v
= br_vlan_find(vg
, vid
);
514 if (v
&& br_vlan_should_use(v
))
520 /* Called under RCU */
521 bool br_should_learn(struct net_bridge_port
*p
, struct sk_buff
*skb
, u16
*vid
)
523 struct net_bridge_vlan_group
*vg
;
524 struct net_bridge
*br
= p
->br
;
526 /* If filtering was disabled at input, let it pass. */
527 if (!br
->vlan_enabled
)
530 vg
= nbp_vlan_group_rcu(p
);
531 if (!vg
|| !vg
->num_vlans
)
534 if (!br_vlan_get_tag(skb
, vid
) && skb
->vlan_proto
!= br
->vlan_proto
)
538 *vid
= br_get_pvid(vg
);
545 if (br_vlan_find(vg
, *vid
))
551 /* Must be protected by RTNL.
552 * Must be called with vid in range from 1 to 4094 inclusive.
554 int br_vlan_add(struct net_bridge
*br
, u16 vid
, u16 flags
)
556 struct net_bridge_vlan_group
*vg
;
557 struct net_bridge_vlan
*vlan
;
562 vg
= br_vlan_group(br
);
563 vlan
= br_vlan_find(vg
, vid
);
565 if (!br_vlan_is_brentry(vlan
)) {
566 /* Trying to change flags of non-existent bridge vlan */
567 if (!(flags
& BRIDGE_VLAN_INFO_BRENTRY
))
569 /* It was only kept for port vlans, now make it real */
570 ret
= br_fdb_insert(br
, NULL
, br
->dev
->dev_addr
,
573 br_err(br
, "failed insert local address into bridge forwarding table\n");
576 refcount_inc(&vlan
->refcnt
);
577 vlan
->flags
|= BRIDGE_VLAN_INFO_BRENTRY
;
580 __vlan_add_flags(vlan
, flags
);
584 vlan
= kzalloc(sizeof(*vlan
), GFP_KERNEL
);
588 vlan
->stats
= netdev_alloc_pcpu_stats(struct br_vlan_stats
);
594 vlan
->flags
= flags
| BRIDGE_VLAN_INFO_MASTER
;
595 vlan
->flags
&= ~BRIDGE_VLAN_INFO_PVID
;
597 if (flags
& BRIDGE_VLAN_INFO_BRENTRY
)
598 refcount_set(&vlan
->refcnt
, 1);
599 ret
= __vlan_add(vlan
, flags
);
601 free_percpu(vlan
->stats
);
608 /* Must be protected by RTNL.
609 * Must be called with vid in range from 1 to 4094 inclusive.
611 int br_vlan_delete(struct net_bridge
*br
, u16 vid
)
613 struct net_bridge_vlan_group
*vg
;
614 struct net_bridge_vlan
*v
;
618 vg
= br_vlan_group(br
);
619 v
= br_vlan_find(vg
, vid
);
620 if (!v
|| !br_vlan_is_brentry(v
))
623 br_fdb_find_delete_local(br
, NULL
, br
->dev
->dev_addr
, vid
);
624 br_fdb_delete_by_port(br
, NULL
, vid
, 0);
626 vlan_tunnel_info_del(vg
, v
);
628 return __vlan_del(v
);
631 void br_vlan_flush(struct net_bridge
*br
)
633 struct net_bridge_vlan_group
*vg
;
637 vg
= br_vlan_group(br
);
639 RCU_INIT_POINTER(br
->vlgrp
, NULL
);
641 __vlan_group_free(vg
);
644 struct net_bridge_vlan
*br_vlan_find(struct net_bridge_vlan_group
*vg
, u16 vid
)
649 return br_vlan_lookup(&vg
->vlan_hash
, vid
);
652 /* Must be protected by RTNL. */
653 static void recalculate_group_addr(struct net_bridge
*br
)
655 if (br
->group_addr_set
)
658 spin_lock_bh(&br
->lock
);
659 if (!br
->vlan_enabled
|| br
->vlan_proto
== htons(ETH_P_8021Q
)) {
660 /* Bridge Group Address */
661 br
->group_addr
[5] = 0x00;
662 } else { /* vlan_enabled && ETH_P_8021AD */
663 /* Provider Bridge Group Address */
664 br
->group_addr
[5] = 0x08;
666 spin_unlock_bh(&br
->lock
);
669 /* Must be protected by RTNL. */
670 void br_recalculate_fwd_mask(struct net_bridge
*br
)
672 if (!br
->vlan_enabled
|| br
->vlan_proto
== htons(ETH_P_8021Q
))
673 br
->group_fwd_mask_required
= BR_GROUPFWD_DEFAULT
;
674 else /* vlan_enabled && ETH_P_8021AD */
675 br
->group_fwd_mask_required
= BR_GROUPFWD_8021AD
&
676 ~(1u << br
->group_addr
[5]);
679 int __br_vlan_filter_toggle(struct net_bridge
*br
, unsigned long val
)
681 struct switchdev_attr attr
= {
683 .id
= SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING
,
684 .flags
= SWITCHDEV_F_SKIP_EOPNOTSUPP
,
685 .u
.vlan_filtering
= val
,
689 if (br
->vlan_enabled
== val
)
692 err
= switchdev_port_attr_set(br
->dev
, &attr
);
693 if (err
&& err
!= -EOPNOTSUPP
)
696 br
->vlan_enabled
= val
;
697 br_manage_promisc(br
);
698 recalculate_group_addr(br
);
699 br_recalculate_fwd_mask(br
);
704 int br_vlan_filter_toggle(struct net_bridge
*br
, unsigned long val
)
706 return __br_vlan_filter_toggle(br
, val
);
709 bool br_vlan_enabled(const struct net_device
*dev
)
711 struct net_bridge
*br
= netdev_priv(dev
);
713 return !!br
->vlan_enabled
;
715 EXPORT_SYMBOL_GPL(br_vlan_enabled
);
717 int __br_vlan_set_proto(struct net_bridge
*br
, __be16 proto
)
720 struct net_bridge_port
*p
;
721 struct net_bridge_vlan
*vlan
;
722 struct net_bridge_vlan_group
*vg
;
725 if (br
->vlan_proto
== proto
)
728 /* Add VLANs for the new proto to the device filter. */
729 list_for_each_entry(p
, &br
->port_list
, list
) {
730 vg
= nbp_vlan_group(p
);
731 list_for_each_entry(vlan
, &vg
->vlan_list
, vlist
) {
732 err
= vlan_vid_add(p
->dev
, proto
, vlan
->vid
);
738 oldproto
= br
->vlan_proto
;
739 br
->vlan_proto
= proto
;
741 recalculate_group_addr(br
);
742 br_recalculate_fwd_mask(br
);
744 /* Delete VLANs for the old proto from the device filter. */
745 list_for_each_entry(p
, &br
->port_list
, list
) {
746 vg
= nbp_vlan_group(p
);
747 list_for_each_entry(vlan
, &vg
->vlan_list
, vlist
)
748 vlan_vid_del(p
->dev
, oldproto
, vlan
->vid
);
754 list_for_each_entry_continue_reverse(vlan
, &vg
->vlan_list
, vlist
)
755 vlan_vid_del(p
->dev
, proto
, vlan
->vid
);
757 list_for_each_entry_continue_reverse(p
, &br
->port_list
, list
) {
758 vg
= nbp_vlan_group(p
);
759 list_for_each_entry(vlan
, &vg
->vlan_list
, vlist
)
760 vlan_vid_del(p
->dev
, proto
, vlan
->vid
);
766 int br_vlan_set_proto(struct net_bridge
*br
, unsigned long val
)
768 if (val
!= ETH_P_8021Q
&& val
!= ETH_P_8021AD
)
769 return -EPROTONOSUPPORT
;
771 return __br_vlan_set_proto(br
, htons(val
));
774 int br_vlan_set_stats(struct net_bridge
*br
, unsigned long val
)
779 br
->vlan_stats_enabled
= val
;
788 static bool vlan_default_pvid(struct net_bridge_vlan_group
*vg
, u16 vid
)
790 struct net_bridge_vlan
*v
;
795 v
= br_vlan_lookup(&vg
->vlan_hash
, vid
);
796 if (v
&& br_vlan_should_use(v
) &&
797 (v
->flags
& BRIDGE_VLAN_INFO_UNTAGGED
))
803 static void br_vlan_disable_default_pvid(struct net_bridge
*br
)
805 struct net_bridge_port
*p
;
806 u16 pvid
= br
->default_pvid
;
808 /* Disable default_pvid on all ports where it is still
811 if (vlan_default_pvid(br_vlan_group(br
), pvid
))
812 br_vlan_delete(br
, pvid
);
814 list_for_each_entry(p
, &br
->port_list
, list
) {
815 if (vlan_default_pvid(nbp_vlan_group(p
), pvid
))
816 nbp_vlan_delete(p
, pvid
);
819 br
->default_pvid
= 0;
822 int __br_vlan_set_default_pvid(struct net_bridge
*br
, u16 pvid
)
824 const struct net_bridge_vlan
*pvent
;
825 struct net_bridge_vlan_group
*vg
;
826 struct net_bridge_port
*p
;
829 unsigned long *changed
;
832 br_vlan_disable_default_pvid(br
);
836 changed
= kcalloc(BITS_TO_LONGS(BR_MAX_PORTS
), sizeof(unsigned long),
841 old_pvid
= br
->default_pvid
;
843 /* Update default_pvid config only if we do not conflict with
844 * user configuration.
846 vg
= br_vlan_group(br
);
847 pvent
= br_vlan_find(vg
, pvid
);
848 if ((!old_pvid
|| vlan_default_pvid(vg
, old_pvid
)) &&
849 (!pvent
|| !br_vlan_should_use(pvent
))) {
850 err
= br_vlan_add(br
, pvid
,
851 BRIDGE_VLAN_INFO_PVID
|
852 BRIDGE_VLAN_INFO_UNTAGGED
|
853 BRIDGE_VLAN_INFO_BRENTRY
);
856 br_vlan_delete(br
, old_pvid
);
860 list_for_each_entry(p
, &br
->port_list
, list
) {
861 /* Update default_pvid config only if we do not conflict with
862 * user configuration.
864 vg
= nbp_vlan_group(p
);
866 !vlan_default_pvid(vg
, old_pvid
)) ||
867 br_vlan_find(vg
, pvid
))
870 err
= nbp_vlan_add(p
, pvid
,
871 BRIDGE_VLAN_INFO_PVID
|
872 BRIDGE_VLAN_INFO_UNTAGGED
);
875 nbp_vlan_delete(p
, old_pvid
);
876 set_bit(p
->port_no
, changed
);
879 br
->default_pvid
= pvid
;
886 list_for_each_entry_continue_reverse(p
, &br
->port_list
, list
) {
887 if (!test_bit(p
->port_no
, changed
))
891 nbp_vlan_add(p
, old_pvid
,
892 BRIDGE_VLAN_INFO_PVID
|
893 BRIDGE_VLAN_INFO_UNTAGGED
);
894 nbp_vlan_delete(p
, pvid
);
897 if (test_bit(0, changed
)) {
899 br_vlan_add(br
, old_pvid
,
900 BRIDGE_VLAN_INFO_PVID
|
901 BRIDGE_VLAN_INFO_UNTAGGED
|
902 BRIDGE_VLAN_INFO_BRENTRY
);
903 br_vlan_delete(br
, pvid
);
908 int br_vlan_set_default_pvid(struct net_bridge
*br
, unsigned long val
)
913 if (val
>= VLAN_VID_MASK
)
916 if (pvid
== br
->default_pvid
)
919 /* Only allow default pvid change when filtering is disabled */
920 if (br
->vlan_enabled
) {
921 pr_info_once("Please disable vlan filtering to change default_pvid\n");
925 err
= __br_vlan_set_default_pvid(br
, pvid
);
930 int br_vlan_init(struct net_bridge
*br
)
932 struct net_bridge_vlan_group
*vg
;
935 vg
= kzalloc(sizeof(*vg
), GFP_KERNEL
);
938 ret
= rhashtable_init(&vg
->vlan_hash
, &br_vlan_rht_params
);
941 ret
= vlan_tunnel_init(vg
);
943 goto err_tunnel_init
;
944 INIT_LIST_HEAD(&vg
->vlan_list
);
945 br
->vlan_proto
= htons(ETH_P_8021Q
);
946 br
->default_pvid
= 1;
947 rcu_assign_pointer(br
->vlgrp
, vg
);
948 ret
= br_vlan_add(br
, 1,
949 BRIDGE_VLAN_INFO_PVID
| BRIDGE_VLAN_INFO_UNTAGGED
|
950 BRIDGE_VLAN_INFO_BRENTRY
);
958 vlan_tunnel_deinit(vg
);
960 rhashtable_destroy(&vg
->vlan_hash
);
967 int nbp_vlan_init(struct net_bridge_port
*p
)
969 struct switchdev_attr attr
= {
970 .orig_dev
= p
->br
->dev
,
971 .id
= SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING
,
972 .flags
= SWITCHDEV_F_SKIP_EOPNOTSUPP
,
973 .u
.vlan_filtering
= p
->br
->vlan_enabled
,
975 struct net_bridge_vlan_group
*vg
;
978 vg
= kzalloc(sizeof(struct net_bridge_vlan_group
), GFP_KERNEL
);
982 ret
= switchdev_port_attr_set(p
->dev
, &attr
);
983 if (ret
&& ret
!= -EOPNOTSUPP
)
984 goto err_vlan_enabled
;
986 ret
= rhashtable_init(&vg
->vlan_hash
, &br_vlan_rht_params
);
989 ret
= vlan_tunnel_init(vg
);
991 goto err_tunnel_init
;
992 INIT_LIST_HEAD(&vg
->vlan_list
);
993 rcu_assign_pointer(p
->vlgrp
, vg
);
994 if (p
->br
->default_pvid
) {
995 ret
= nbp_vlan_add(p
, p
->br
->default_pvid
,
996 BRIDGE_VLAN_INFO_PVID
|
997 BRIDGE_VLAN_INFO_UNTAGGED
);
1005 RCU_INIT_POINTER(p
->vlgrp
, NULL
);
1007 vlan_tunnel_deinit(vg
);
1009 rhashtable_destroy(&vg
->vlan_hash
);
1017 /* Must be protected by RTNL.
1018 * Must be called with vid in range from 1 to 4094 inclusive.
1020 int nbp_vlan_add(struct net_bridge_port
*port
, u16 vid
, u16 flags
)
1022 struct switchdev_obj_port_vlan v
= {
1023 .obj
.orig_dev
= port
->dev
,
1024 .obj
.id
= SWITCHDEV_OBJ_ID_PORT_VLAN
,
1029 struct net_bridge_vlan
*vlan
;
1034 vlan
= br_vlan_find(nbp_vlan_group(port
), vid
);
1036 /* Pass the flags to the hardware bridge */
1037 ret
= switchdev_port_obj_add(port
->dev
, &v
.obj
);
1038 if (ret
&& ret
!= -EOPNOTSUPP
)
1040 __vlan_add_flags(vlan
, flags
);
1044 vlan
= kzalloc(sizeof(*vlan
), GFP_KERNEL
);
1050 ret
= __vlan_add(vlan
, flags
);
1057 /* Must be protected by RTNL.
1058 * Must be called with vid in range from 1 to 4094 inclusive.
1060 int nbp_vlan_delete(struct net_bridge_port
*port
, u16 vid
)
1062 struct net_bridge_vlan
*v
;
1066 v
= br_vlan_find(nbp_vlan_group(port
), vid
);
1069 br_fdb_find_delete_local(port
->br
, port
, port
->dev
->dev_addr
, vid
);
1070 br_fdb_delete_by_port(port
->br
, port
, vid
, 0);
1072 return __vlan_del(v
);
1075 void nbp_vlan_flush(struct net_bridge_port
*port
)
1077 struct net_bridge_vlan_group
*vg
;
1081 vg
= nbp_vlan_group(port
);
1083 RCU_INIT_POINTER(port
->vlgrp
, NULL
);
1085 __vlan_group_free(vg
);
1088 void br_vlan_get_stats(const struct net_bridge_vlan
*v
,
1089 struct br_vlan_stats
*stats
)
1093 memset(stats
, 0, sizeof(*stats
));
1094 for_each_possible_cpu(i
) {
1095 u64 rxpackets
, rxbytes
, txpackets
, txbytes
;
1096 struct br_vlan_stats
*cpu_stats
;
1099 cpu_stats
= per_cpu_ptr(v
->stats
, i
);
1101 start
= u64_stats_fetch_begin_irq(&cpu_stats
->syncp
);
1102 rxpackets
= cpu_stats
->rx_packets
;
1103 rxbytes
= cpu_stats
->rx_bytes
;
1104 txbytes
= cpu_stats
->tx_bytes
;
1105 txpackets
= cpu_stats
->tx_packets
;
1106 } while (u64_stats_fetch_retry_irq(&cpu_stats
->syncp
, start
));
1108 stats
->rx_packets
+= rxpackets
;
1109 stats
->rx_bytes
+= rxbytes
;
1110 stats
->tx_bytes
+= txbytes
;
1111 stats
->tx_packets
+= txpackets
;