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
))
172 refcount_inc(&masterv
->refcnt
);
177 static void br_master_vlan_rcu_free(struct rcu_head
*rcu
)
179 struct net_bridge_vlan
*v
;
181 v
= container_of(rcu
, struct net_bridge_vlan
, rcu
);
182 WARN_ON(!br_vlan_is_master(v
));
183 free_percpu(v
->stats
);
188 static void br_vlan_put_master(struct net_bridge_vlan
*masterv
)
190 struct net_bridge_vlan_group
*vg
;
192 if (!br_vlan_is_master(masterv
))
195 vg
= br_vlan_group(masterv
->br
);
196 if (refcount_dec_and_test(&masterv
->refcnt
)) {
197 rhashtable_remove_fast(&vg
->vlan_hash
,
198 &masterv
->vnode
, br_vlan_rht_params
);
199 __vlan_del_list(masterv
);
200 call_rcu(&masterv
->rcu
, br_master_vlan_rcu_free
);
204 /* This is the shared VLAN add function which works for both ports and bridge
205 * devices. There are four possible calls to this function in terms of the
207 * 1. vlan is being added on a port (no master flags, global entry exists)
208 * 2. vlan is being added on a bridge (both master and brentry flags)
209 * 3. vlan is being added on a port, but a global entry didn't exist which
210 * is being created right now (master flag set, brentry flag unset), the
211 * global entry is used for global per-vlan features, but not for filtering
212 * 4. same as 3 but with both master and brentry flags set so the entry
213 * will be used for filtering in both the port and the bridge
215 static int __vlan_add(struct net_bridge_vlan
*v
, u16 flags
)
217 struct net_bridge_vlan
*masterv
= NULL
;
218 struct net_bridge_port
*p
= NULL
;
219 struct net_bridge_vlan_group
*vg
;
220 struct net_device
*dev
;
221 struct net_bridge
*br
;
224 if (br_vlan_is_master(v
)) {
227 vg
= br_vlan_group(br
);
232 vg
= nbp_vlan_group(p
);
236 /* Add VLAN to the device filter if it is supported.
237 * This ensures tagged traffic enters the bridge when
238 * promiscuous mode is disabled by br_manage_promisc().
240 err
= __vlan_vid_add(dev
, br
, v
->vid
, flags
);
244 /* need to work on the master vlan too */
245 if (flags
& BRIDGE_VLAN_INFO_MASTER
) {
248 err
= br_vlan_add(br
, v
->vid
,
249 flags
| BRIDGE_VLAN_INFO_BRENTRY
,
255 masterv
= br_vlan_get_master(br
, v
->vid
);
259 v
->stats
= masterv
->stats
;
262 /* Add the dev mac and count the vlan only if it's usable */
263 if (br_vlan_should_use(v
)) {
264 err
= br_fdb_insert(br
, p
, dev
->dev_addr
, v
->vid
);
266 br_err(br
, "failed insert local address into bridge forwarding table\n");
272 err
= rhashtable_lookup_insert_fast(&vg
->vlan_hash
, &v
->vnode
,
278 __vlan_add_flags(v
, flags
);
283 if (br_vlan_should_use(v
)) {
284 br_fdb_find_delete_local(br
, p
, dev
->dev_addr
, v
->vid
);
290 __vlan_vid_del(dev
, br
, v
->vid
);
292 br_vlan_put_master(masterv
);
300 static int __vlan_del(struct net_bridge_vlan
*v
)
302 struct net_bridge_vlan
*masterv
= v
;
303 struct net_bridge_vlan_group
*vg
;
304 struct net_bridge_port
*p
= NULL
;
307 if (br_vlan_is_master(v
)) {
308 vg
= br_vlan_group(v
->br
);
311 vg
= nbp_vlan_group(v
->port
);
315 __vlan_delete_pvid(vg
, v
->vid
);
317 err
= __vlan_vid_del(p
->dev
, p
->br
, v
->vid
);
322 if (br_vlan_should_use(v
)) {
323 v
->flags
&= ~BRIDGE_VLAN_INFO_BRENTRY
;
328 vlan_tunnel_info_del(vg
, v
);
329 rhashtable_remove_fast(&vg
->vlan_hash
, &v
->vnode
,
335 br_vlan_put_master(masterv
);
340 static void __vlan_group_free(struct net_bridge_vlan_group
*vg
)
342 WARN_ON(!list_empty(&vg
->vlan_list
));
343 rhashtable_destroy(&vg
->vlan_hash
);
344 vlan_tunnel_deinit(vg
);
348 static void __vlan_flush(struct net_bridge_vlan_group
*vg
)
350 struct net_bridge_vlan
*vlan
, *tmp
;
352 __vlan_delete_pvid(vg
, vg
->pvid
);
353 list_for_each_entry_safe(vlan
, tmp
, &vg
->vlan_list
, vlist
)
357 struct sk_buff
*br_handle_vlan(struct net_bridge
*br
,
358 const struct net_bridge_port
*p
,
359 struct net_bridge_vlan_group
*vg
,
362 struct br_vlan_stats
*stats
;
363 struct net_bridge_vlan
*v
;
366 /* If this packet was not filtered at input, let it pass */
367 if (!BR_INPUT_SKB_CB(skb
)->vlan_filtered
)
370 /* At this point, we know that the frame was filtered and contains
371 * a valid vlan id. If the vlan id has untagged flag set,
372 * send untagged; otherwise, send tagged.
374 br_vlan_get_tag(skb
, &vid
);
375 v
= br_vlan_find(vg
, vid
);
376 /* Vlan entry must be configured at this point. The
377 * only exception is the bridge is set in promisc mode and the
378 * packet is destined for the bridge device. In this case
379 * pass the packet as is.
381 if (!v
|| !br_vlan_should_use(v
)) {
382 if ((br
->dev
->flags
& IFF_PROMISC
) && skb
->dev
== br
->dev
) {
389 if (br
->vlan_stats_enabled
) {
390 stats
= this_cpu_ptr(v
->stats
);
391 u64_stats_update_begin(&stats
->syncp
);
392 stats
->tx_bytes
+= skb
->len
;
394 u64_stats_update_end(&stats
->syncp
);
397 if (v
->flags
& BRIDGE_VLAN_INFO_UNTAGGED
)
400 if (p
&& (p
->flags
& BR_VLAN_TUNNEL
) &&
401 br_handle_egress_vlan_tunnel(skb
, v
)) {
409 /* Called under RCU */
410 static bool __allowed_ingress(const struct net_bridge
*br
,
411 struct net_bridge_vlan_group
*vg
,
412 struct sk_buff
*skb
, u16
*vid
)
414 struct br_vlan_stats
*stats
;
415 struct net_bridge_vlan
*v
;
418 BR_INPUT_SKB_CB(skb
)->vlan_filtered
= true;
419 /* If vlan tx offload is disabled on bridge device and frame was
420 * sent from vlan device on the bridge device, it does not have
421 * HW accelerated vlan tag.
423 if (unlikely(!skb_vlan_tag_present(skb
) &&
424 skb
->protocol
== br
->vlan_proto
)) {
425 skb
= skb_vlan_untag(skb
);
430 if (!br_vlan_get_tag(skb
, vid
)) {
432 if (skb
->vlan_proto
!= br
->vlan_proto
) {
433 /* Protocol-mismatch, empty out vlan_tci for new tag */
434 skb_push(skb
, ETH_HLEN
);
435 skb
= vlan_insert_tag_set_proto(skb
, skb
->vlan_proto
,
436 skb_vlan_tag_get(skb
));
440 skb_pull(skb
, ETH_HLEN
);
441 skb_reset_mac_len(skb
);
453 u16 pvid
= br_get_pvid(vg
);
455 /* Frame had a tag with VID 0 or did not have a tag.
456 * See if pvid is set on this port. That tells us which
457 * vlan untagged or priority-tagged traffic belongs to.
462 /* PVID is set on this port. Any untagged or priority-tagged
463 * ingress frame is considered to belong to this vlan.
467 /* Untagged Frame. */
468 __vlan_hwaccel_put_tag(skb
, br
->vlan_proto
, pvid
);
470 /* Priority-tagged Frame.
471 * At this point, We know that skb->vlan_tci had
472 * VLAN_TAG_PRESENT bit and its VID field was 0x000.
473 * We update only VID field and preserve PCP field.
475 skb
->vlan_tci
|= pvid
;
477 /* if stats are disabled we can avoid the lookup */
478 if (!br
->vlan_stats_enabled
)
481 v
= br_vlan_find(vg
, *vid
);
482 if (!v
|| !br_vlan_should_use(v
))
485 if (br
->vlan_stats_enabled
) {
486 stats
= this_cpu_ptr(v
->stats
);
487 u64_stats_update_begin(&stats
->syncp
);
488 stats
->rx_bytes
+= skb
->len
;
490 u64_stats_update_end(&stats
->syncp
);
500 bool br_allowed_ingress(const struct net_bridge
*br
,
501 struct net_bridge_vlan_group
*vg
, struct sk_buff
*skb
,
504 /* If VLAN filtering is disabled on the bridge, all packets are
507 if (!br
->vlan_enabled
) {
508 BR_INPUT_SKB_CB(skb
)->vlan_filtered
= false;
512 return __allowed_ingress(br
, vg
, skb
, vid
);
515 /* Called under RCU. */
516 bool br_allowed_egress(struct net_bridge_vlan_group
*vg
,
517 const struct sk_buff
*skb
)
519 const struct net_bridge_vlan
*v
;
522 /* If this packet was not filtered at input, let it pass */
523 if (!BR_INPUT_SKB_CB(skb
)->vlan_filtered
)
526 br_vlan_get_tag(skb
, &vid
);
527 v
= br_vlan_find(vg
, vid
);
528 if (v
&& br_vlan_should_use(v
))
534 /* Called under RCU */
535 bool br_should_learn(struct net_bridge_port
*p
, struct sk_buff
*skb
, u16
*vid
)
537 struct net_bridge_vlan_group
*vg
;
538 struct net_bridge
*br
= p
->br
;
540 /* If filtering was disabled at input, let it pass. */
541 if (!br
->vlan_enabled
)
544 vg
= nbp_vlan_group_rcu(p
);
545 if (!vg
|| !vg
->num_vlans
)
548 if (!br_vlan_get_tag(skb
, vid
) && skb
->vlan_proto
!= br
->vlan_proto
)
552 *vid
= br_get_pvid(vg
);
559 if (br_vlan_find(vg
, *vid
))
565 /* Must be protected by RTNL.
566 * Must be called with vid in range from 1 to 4094 inclusive.
567 * changed must be true only if the vlan was created or updated
569 int br_vlan_add(struct net_bridge
*br
, u16 vid
, u16 flags
, bool *changed
)
571 struct net_bridge_vlan_group
*vg
;
572 struct net_bridge_vlan
*vlan
;
578 vg
= br_vlan_group(br
);
579 vlan
= br_vlan_find(vg
, vid
);
581 if (!br_vlan_is_brentry(vlan
)) {
582 /* Trying to change flags of non-existent bridge vlan */
583 if (!(flags
& BRIDGE_VLAN_INFO_BRENTRY
))
585 /* It was only kept for port vlans, now make it real */
586 ret
= br_fdb_insert(br
, NULL
, br
->dev
->dev_addr
,
589 br_err(br
, "failed insert local address into bridge forwarding table\n");
592 refcount_inc(&vlan
->refcnt
);
593 vlan
->flags
|= BRIDGE_VLAN_INFO_BRENTRY
;
597 if (__vlan_add_flags(vlan
, flags
))
603 vlan
= kzalloc(sizeof(*vlan
), GFP_KERNEL
);
607 vlan
->stats
= netdev_alloc_pcpu_stats(struct br_vlan_stats
);
613 vlan
->flags
= flags
| BRIDGE_VLAN_INFO_MASTER
;
614 vlan
->flags
&= ~BRIDGE_VLAN_INFO_PVID
;
616 if (flags
& BRIDGE_VLAN_INFO_BRENTRY
)
617 refcount_set(&vlan
->refcnt
, 1);
618 ret
= __vlan_add(vlan
, flags
);
620 free_percpu(vlan
->stats
);
629 /* Must be protected by RTNL.
630 * Must be called with vid in range from 1 to 4094 inclusive.
632 int br_vlan_delete(struct net_bridge
*br
, u16 vid
)
634 struct net_bridge_vlan_group
*vg
;
635 struct net_bridge_vlan
*v
;
639 vg
= br_vlan_group(br
);
640 v
= br_vlan_find(vg
, vid
);
641 if (!v
|| !br_vlan_is_brentry(v
))
644 br_fdb_find_delete_local(br
, NULL
, br
->dev
->dev_addr
, vid
);
645 br_fdb_delete_by_port(br
, NULL
, vid
, 0);
647 vlan_tunnel_info_del(vg
, v
);
649 return __vlan_del(v
);
652 void br_vlan_flush(struct net_bridge
*br
)
654 struct net_bridge_vlan_group
*vg
;
658 vg
= br_vlan_group(br
);
660 RCU_INIT_POINTER(br
->vlgrp
, NULL
);
662 __vlan_group_free(vg
);
665 struct net_bridge_vlan
*br_vlan_find(struct net_bridge_vlan_group
*vg
, u16 vid
)
670 return br_vlan_lookup(&vg
->vlan_hash
, vid
);
673 /* Must be protected by RTNL. */
674 static void recalculate_group_addr(struct net_bridge
*br
)
676 if (br
->group_addr_set
)
679 spin_lock_bh(&br
->lock
);
680 if (!br
->vlan_enabled
|| br
->vlan_proto
== htons(ETH_P_8021Q
)) {
681 /* Bridge Group Address */
682 br
->group_addr
[5] = 0x00;
683 } else { /* vlan_enabled && ETH_P_8021AD */
684 /* Provider Bridge Group Address */
685 br
->group_addr
[5] = 0x08;
687 spin_unlock_bh(&br
->lock
);
690 /* Must be protected by RTNL. */
691 void br_recalculate_fwd_mask(struct net_bridge
*br
)
693 if (!br
->vlan_enabled
|| br
->vlan_proto
== htons(ETH_P_8021Q
))
694 br
->group_fwd_mask_required
= BR_GROUPFWD_DEFAULT
;
695 else /* vlan_enabled && ETH_P_8021AD */
696 br
->group_fwd_mask_required
= BR_GROUPFWD_8021AD
&
697 ~(1u << br
->group_addr
[5]);
700 int __br_vlan_filter_toggle(struct net_bridge
*br
, unsigned long val
)
702 struct switchdev_attr attr
= {
704 .id
= SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING
,
705 .flags
= SWITCHDEV_F_SKIP_EOPNOTSUPP
,
706 .u
.vlan_filtering
= val
,
710 if (br
->vlan_enabled
== val
)
713 err
= switchdev_port_attr_set(br
->dev
, &attr
);
714 if (err
&& err
!= -EOPNOTSUPP
)
717 br
->vlan_enabled
= val
;
718 br_manage_promisc(br
);
719 recalculate_group_addr(br
);
720 br_recalculate_fwd_mask(br
);
725 int br_vlan_filter_toggle(struct net_bridge
*br
, unsigned long val
)
727 return __br_vlan_filter_toggle(br
, val
);
730 bool br_vlan_enabled(const struct net_device
*dev
)
732 struct net_bridge
*br
= netdev_priv(dev
);
734 return !!br
->vlan_enabled
;
736 EXPORT_SYMBOL_GPL(br_vlan_enabled
);
738 int __br_vlan_set_proto(struct net_bridge
*br
, __be16 proto
)
741 struct net_bridge_port
*p
;
742 struct net_bridge_vlan
*vlan
;
743 struct net_bridge_vlan_group
*vg
;
746 if (br
->vlan_proto
== proto
)
749 /* Add VLANs for the new proto to the device filter. */
750 list_for_each_entry(p
, &br
->port_list
, list
) {
751 vg
= nbp_vlan_group(p
);
752 list_for_each_entry(vlan
, &vg
->vlan_list
, vlist
) {
753 err
= vlan_vid_add(p
->dev
, proto
, vlan
->vid
);
759 oldproto
= br
->vlan_proto
;
760 br
->vlan_proto
= proto
;
762 recalculate_group_addr(br
);
763 br_recalculate_fwd_mask(br
);
765 /* Delete VLANs for the old proto from the device filter. */
766 list_for_each_entry(p
, &br
->port_list
, list
) {
767 vg
= nbp_vlan_group(p
);
768 list_for_each_entry(vlan
, &vg
->vlan_list
, vlist
)
769 vlan_vid_del(p
->dev
, oldproto
, vlan
->vid
);
775 list_for_each_entry_continue_reverse(vlan
, &vg
->vlan_list
, vlist
)
776 vlan_vid_del(p
->dev
, proto
, vlan
->vid
);
778 list_for_each_entry_continue_reverse(p
, &br
->port_list
, list
) {
779 vg
= nbp_vlan_group(p
);
780 list_for_each_entry(vlan
, &vg
->vlan_list
, vlist
)
781 vlan_vid_del(p
->dev
, proto
, vlan
->vid
);
787 int br_vlan_set_proto(struct net_bridge
*br
, unsigned long val
)
789 if (val
!= ETH_P_8021Q
&& val
!= ETH_P_8021AD
)
790 return -EPROTONOSUPPORT
;
792 return __br_vlan_set_proto(br
, htons(val
));
795 int br_vlan_set_stats(struct net_bridge
*br
, unsigned long val
)
800 br
->vlan_stats_enabled
= val
;
809 static bool vlan_default_pvid(struct net_bridge_vlan_group
*vg
, u16 vid
)
811 struct net_bridge_vlan
*v
;
816 v
= br_vlan_lookup(&vg
->vlan_hash
, vid
);
817 if (v
&& br_vlan_should_use(v
) &&
818 (v
->flags
& BRIDGE_VLAN_INFO_UNTAGGED
))
824 static void br_vlan_disable_default_pvid(struct net_bridge
*br
)
826 struct net_bridge_port
*p
;
827 u16 pvid
= br
->default_pvid
;
829 /* Disable default_pvid on all ports where it is still
832 if (vlan_default_pvid(br_vlan_group(br
), pvid
))
833 br_vlan_delete(br
, pvid
);
835 list_for_each_entry(p
, &br
->port_list
, list
) {
836 if (vlan_default_pvid(nbp_vlan_group(p
), pvid
))
837 nbp_vlan_delete(p
, pvid
);
840 br
->default_pvid
= 0;
843 int __br_vlan_set_default_pvid(struct net_bridge
*br
, u16 pvid
)
845 const struct net_bridge_vlan
*pvent
;
846 struct net_bridge_vlan_group
*vg
;
847 struct net_bridge_port
*p
;
848 unsigned long *changed
;
854 br_vlan_disable_default_pvid(br
);
858 changed
= kcalloc(BITS_TO_LONGS(BR_MAX_PORTS
), sizeof(unsigned long),
863 old_pvid
= br
->default_pvid
;
865 /* Update default_pvid config only if we do not conflict with
866 * user configuration.
868 vg
= br_vlan_group(br
);
869 pvent
= br_vlan_find(vg
, pvid
);
870 if ((!old_pvid
|| vlan_default_pvid(vg
, old_pvid
)) &&
871 (!pvent
|| !br_vlan_should_use(pvent
))) {
872 err
= br_vlan_add(br
, pvid
,
873 BRIDGE_VLAN_INFO_PVID
|
874 BRIDGE_VLAN_INFO_UNTAGGED
|
875 BRIDGE_VLAN_INFO_BRENTRY
,
879 br_vlan_delete(br
, old_pvid
);
883 list_for_each_entry(p
, &br
->port_list
, list
) {
884 /* Update default_pvid config only if we do not conflict with
885 * user configuration.
887 vg
= nbp_vlan_group(p
);
889 !vlan_default_pvid(vg
, old_pvid
)) ||
890 br_vlan_find(vg
, pvid
))
893 err
= nbp_vlan_add(p
, pvid
,
894 BRIDGE_VLAN_INFO_PVID
|
895 BRIDGE_VLAN_INFO_UNTAGGED
,
899 nbp_vlan_delete(p
, old_pvid
);
900 set_bit(p
->port_no
, changed
);
903 br
->default_pvid
= pvid
;
910 list_for_each_entry_continue_reverse(p
, &br
->port_list
, list
) {
911 if (!test_bit(p
->port_no
, changed
))
915 nbp_vlan_add(p
, old_pvid
,
916 BRIDGE_VLAN_INFO_PVID
|
917 BRIDGE_VLAN_INFO_UNTAGGED
,
919 nbp_vlan_delete(p
, pvid
);
922 if (test_bit(0, changed
)) {
924 br_vlan_add(br
, old_pvid
,
925 BRIDGE_VLAN_INFO_PVID
|
926 BRIDGE_VLAN_INFO_UNTAGGED
|
927 BRIDGE_VLAN_INFO_BRENTRY
,
929 br_vlan_delete(br
, pvid
);
934 int br_vlan_set_default_pvid(struct net_bridge
*br
, unsigned long val
)
939 if (val
>= VLAN_VID_MASK
)
942 if (pvid
== br
->default_pvid
)
945 /* Only allow default pvid change when filtering is disabled */
946 if (br
->vlan_enabled
) {
947 pr_info_once("Please disable vlan filtering to change default_pvid\n");
951 err
= __br_vlan_set_default_pvid(br
, pvid
);
956 int br_vlan_init(struct net_bridge
*br
)
958 struct net_bridge_vlan_group
*vg
;
962 vg
= kzalloc(sizeof(*vg
), GFP_KERNEL
);
965 ret
= rhashtable_init(&vg
->vlan_hash
, &br_vlan_rht_params
);
968 ret
= vlan_tunnel_init(vg
);
970 goto err_tunnel_init
;
971 INIT_LIST_HEAD(&vg
->vlan_list
);
972 br
->vlan_proto
= htons(ETH_P_8021Q
);
973 br
->default_pvid
= 1;
974 rcu_assign_pointer(br
->vlgrp
, vg
);
975 ret
= br_vlan_add(br
, 1,
976 BRIDGE_VLAN_INFO_PVID
| BRIDGE_VLAN_INFO_UNTAGGED
|
977 BRIDGE_VLAN_INFO_BRENTRY
, &changed
);
985 vlan_tunnel_deinit(vg
);
987 rhashtable_destroy(&vg
->vlan_hash
);
994 int nbp_vlan_init(struct net_bridge_port
*p
)
996 struct switchdev_attr attr
= {
997 .orig_dev
= p
->br
->dev
,
998 .id
= SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING
,
999 .flags
= SWITCHDEV_F_SKIP_EOPNOTSUPP
,
1000 .u
.vlan_filtering
= p
->br
->vlan_enabled
,
1002 struct net_bridge_vlan_group
*vg
;
1005 vg
= kzalloc(sizeof(struct net_bridge_vlan_group
), GFP_KERNEL
);
1009 ret
= switchdev_port_attr_set(p
->dev
, &attr
);
1010 if (ret
&& ret
!= -EOPNOTSUPP
)
1011 goto err_vlan_enabled
;
1013 ret
= rhashtable_init(&vg
->vlan_hash
, &br_vlan_rht_params
);
1016 ret
= vlan_tunnel_init(vg
);
1018 goto err_tunnel_init
;
1019 INIT_LIST_HEAD(&vg
->vlan_list
);
1020 rcu_assign_pointer(p
->vlgrp
, vg
);
1021 if (p
->br
->default_pvid
) {
1024 ret
= nbp_vlan_add(p
, p
->br
->default_pvid
,
1025 BRIDGE_VLAN_INFO_PVID
|
1026 BRIDGE_VLAN_INFO_UNTAGGED
,
1035 RCU_INIT_POINTER(p
->vlgrp
, NULL
);
1037 vlan_tunnel_deinit(vg
);
1039 rhashtable_destroy(&vg
->vlan_hash
);
1047 /* Must be protected by RTNL.
1048 * Must be called with vid in range from 1 to 4094 inclusive.
1049 * changed must be true only if the vlan was created or updated
1051 int nbp_vlan_add(struct net_bridge_port
*port
, u16 vid
, u16 flags
,
1054 struct switchdev_obj_port_vlan v
= {
1055 .obj
.orig_dev
= port
->dev
,
1056 .obj
.id
= SWITCHDEV_OBJ_ID_PORT_VLAN
,
1061 struct net_bridge_vlan
*vlan
;
1067 vlan
= br_vlan_find(nbp_vlan_group(port
), vid
);
1069 /* Pass the flags to the hardware bridge */
1070 ret
= switchdev_port_obj_add(port
->dev
, &v
.obj
);
1071 if (ret
&& ret
!= -EOPNOTSUPP
)
1073 *changed
= __vlan_add_flags(vlan
, flags
);
1078 vlan
= kzalloc(sizeof(*vlan
), GFP_KERNEL
);
1084 ret
= __vlan_add(vlan
, flags
);
1093 /* Must be protected by RTNL.
1094 * Must be called with vid in range from 1 to 4094 inclusive.
1096 int nbp_vlan_delete(struct net_bridge_port
*port
, u16 vid
)
1098 struct net_bridge_vlan
*v
;
1102 v
= br_vlan_find(nbp_vlan_group(port
), vid
);
1105 br_fdb_find_delete_local(port
->br
, port
, port
->dev
->dev_addr
, vid
);
1106 br_fdb_delete_by_port(port
->br
, port
, vid
, 0);
1108 return __vlan_del(v
);
1111 void nbp_vlan_flush(struct net_bridge_port
*port
)
1113 struct net_bridge_vlan_group
*vg
;
1117 vg
= nbp_vlan_group(port
);
1119 RCU_INIT_POINTER(port
->vlgrp
, NULL
);
1121 __vlan_group_free(vg
);
1124 void br_vlan_get_stats(const struct net_bridge_vlan
*v
,
1125 struct br_vlan_stats
*stats
)
1129 memset(stats
, 0, sizeof(*stats
));
1130 for_each_possible_cpu(i
) {
1131 u64 rxpackets
, rxbytes
, txpackets
, txbytes
;
1132 struct br_vlan_stats
*cpu_stats
;
1135 cpu_stats
= per_cpu_ptr(v
->stats
, i
);
1137 start
= u64_stats_fetch_begin_irq(&cpu_stats
->syncp
);
1138 rxpackets
= cpu_stats
->rx_packets
;
1139 rxbytes
= cpu_stats
->rx_bytes
;
1140 txbytes
= cpu_stats
->tx_bytes
;
1141 txpackets
= cpu_stats
->tx_packets
;
1142 } while (u64_stats_fetch_retry_irq(&cpu_stats
->syncp
, start
));
1144 stats
->rx_packets
+= rxpackets
;
1145 stats
->rx_bytes
+= rxbytes
;
1146 stats
->tx_bytes
+= txbytes
;
1147 stats
->tx_packets
+= txpackets
;