bridge: vlan: cleanups & fixes
Subsystems (3)
Nikolay Aleksandrov
maintainercover bridge: vlan: cleanups & fixes
From: Nikolay Aleksandrov <[email protected]>
Hi,
This is the first follow-up set, patch 01 reduces the default rhashtable size and the number of locks that can be allocated. Patch 02 and 04 fix possible null pointer dereferences due to the new ordering and initialization on port add/del, and patch 03 moves the "pvid" member in the net_bridge_vlan_group struct in order to simplify code (similar to how it was with the older struct). Patch 05 fixes adding a vlan on a port which is pvid and doesn't have a global context yet.
Please review carefully, I think this is the first use of rhashtable's "locks_mul" member in the tree and I'd like to make sure it's correct. Another thing that needs special attention is the nbp_vlan_flush() move after the rx_handler unregister.
Cheers,
Nik
Nikolay Aleksandrov (5):
bridge: vlan: adjust rhashtable initial size and hash locks size
bridge: vlan: fix possible null vlgrp deref while registering new port
bridge: vlan: move pvid inside net_bridge_vlan_group
bridge: vlan: fix possible null ptr derefs on port init and deinit
bridge: vlan: don't pass flags when creating context only
net/bridge/br_device.c | 2 +- net/bridge/br_if.c | 3 +- net/bridge/br_input.c | 2 +- net/bridge/br_netlink.c | 42 +++++++--------- net/bridge/br_private.h | 44 +++++------------ net/bridge/br_vlan.c | 127 ++++++++++++++++++++++-------------------------- 6 files changed, 93 insertions(+), 127 deletions(-)
Signature · 1 line
2.4.3
Nikolay Aleksandrov
maintainerPATCH 1/5 bridge: vlan: adjust rhashtable initial size and hash locks size
From: Nikolay Aleksandrov <[email protected]>
As Stephen pointed out the default initial size is more than we need, so let's start small (4 elements, thus nelem_hint = 3). Also limit the hash locks to the number of CPUs as we don't need any write-side scaling and this looks like the minimum.
--- net/bridge/br_vlan.c | 2 ++ 1 file changed, 2 insertions(+)
net/bridge/br_vlan.c +2 -0
@@ -19,6 +19,8 @@ static const struct rhashtable_params br_vlan_rht_params = { .head_offset = offsetof(struct net_bridge_vlan, vnode), .key_offset = offsetof(struct net_bridge_vlan, vid), .key_len = sizeof(u16), + .nelem_hint = 3, + .locks_mul = 1, .max_size = VLAN_N_VID, .obj_cmpfn = br_vlan_cmp, .automatic_shrinking = true,
Signature · 1 line
2.4.3
Nikolay Aleksandrov
maintainerPATCH 2/5 bridge: vlan: fix possible null vlgrp deref while registering new port
In reply to Nikolay AleksandrovFrom: Nikolay Aleksandrov <[email protected]>
While a new port is being initialized the rx_handler gets set, but the vlans get initialized later in br_add_if() and in that window if we receive a frame with a link-local address we can try to dereference p->vlgrp in:br_handle_frame() -> br_handle_local_finish() -> br_should_learn()
Fix this by checking vlgrp before using it.
--- net/bridge/br_vlan.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
net/bridge/br_vlan.c +3 -1
@@ -476,13 +476,15 @@ bool br_allowed_egress(struct net_bridge_vlan_group *vg,/* Called under RCU */ bool br_should_learn (struct net_bridge_port *p,struct sk_buff *skb,u16 *vid) { +struct net_bridge_vlan_group *vg;struct net_bridge *br = p->br;/* If filtering was disabled at input, let it pass. */ if (!br->vlan_enabled)return true; -if (!p->vlgrp->num_vlans) + vg = p->vlgrp; +if (!vg || !vg->num_vlans)return false;if (!br_vlan_get_tag (skb, vid) && skb->vlan_proto != br->vlan_proto)
Signature · 1 line
2.4.3
Nikolay Aleksandrov
maintainerPATCH 3/5 bridge: vlan: move pvid inside net_bridge_vlan_group
In reply to Nikolay AleksandrovFrom: Nikolay Aleksandrov <[email protected]>
One obvious way to converge more code (which was also used by the previous vlan code) is to move pvid inside net_bridge_vlan_group. This allows us to simplify some and remove other port-specific functions. Also gives us the ability to simply pass the vlan group and use all of the contained information.
--- net/bridge/br_device.c | 2 +- net/bridge/br_input.c | 2 +- net/bridge/br_netlink.c | 42 +++++++++----------- net/bridge/br_private.h | 44 ++++++--------------- net/bridge/br_vlan.c | 103 ++++++++++++++++++++---------------------------- 5 files changed, 75 insertions(+), 118 deletions(-)
net/bridge/br_device.c +1 -1
@@ -56,7 +56,7 @@ netdev_tx_t br_dev_xmit(struct sk_buff *skb, struct net_device *dev)skb_reset_mac_header (skb);skb_pull (skb,ETH_HLEN ); -if (!br_allowed_ingress (br, skb, &vid)) +if (!br_allowed_ingress (br,br_vlan_group (br), skb, &vid)) goto out;if (is_broadcast_ether_addr (dest))
net/bridge/br_input.c +1 -1
@@ -140,7 +140,7 @@ int br_handle_frame_finish(struct net *net, struct sock *sk, struct sk_buff *skbif (!p || p->state ==BR_STATE_DISABLED ) goto drop; -if (!nbp_allowed_ingress (p, skb, &vid)) +if (!br_allowed_ingress (p->br,nbp_vlan_group (p), skb, &vid)) goto out;/* insert into forwarding database after filtering to avoid spoofing */
net/bridge/br_netlink.c +18 -24
134 lines not on this page · raw message
net/bridge/br_private.h +12 -32
102 lines not on this page · raw message
net/bridge/br_vlan.c +43 -60
238 lines not on this page · raw message
Signature · 1 line
2.4.3
Nikolay Aleksandrov
maintainerPATCH 4/5 bridge: vlan: fix possible null ptr derefs on port init and deinit
In reply to Nikolay AleksandrovFrom: Nikolay Aleksandrov <[email protected]>
When a new port is being added we need to make vlgrp available after rhashtable has been initialized and when removing a port we need to flush the vlans and free the resources after we're sure noone can use the port, i.e. after it's removed from the port list and synchronize_rcu is executed.
--- net/bridge/br_if.c | 3 ++- net/bridge/br_vlan.c | 16 ++++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-)
net/bridge/br_if.c +2 -1
@@ -248,7 +248,6 @@ static void del_nbp(struct net_bridge_port *p)list_del_rcu (&p->list); -nbp_vlan_flush (p);br_fdb_delete_by_port (br, p,0 ,1 );nbp_update_port_count (br);
@@ -257,6 +256,8 @@ static void del_nbp(struct net_bridge_port *p) dev->priv_flags &= ~IFF_BRIDGE_PORT ;netdev_rx_handler_unregister (dev); +/* use the synchronize_rcu done by netdev_rx_handler_unregister */ +nbp_vlan_flush (p);br_multicast_del_port (p);
net/bridge/br_vlan.c +10 -6
@@ -854,16 +854,20 @@ err_rhtbl:int nbp_vlan_init (struct net_bridge_port *p) { +struct net_bridge_vlan_group *vg;int ret = -ENOMEM ; - p->vlgrp =kzalloc (sizeof (struct net_bridge_vlan_group ),GFP_KERNEL ); -if (!p->vlgrp) + vg =kzalloc (sizeof (struct net_bridge_vlan_group ),GFP_KERNEL ); +if (!vg) goto out; - ret =rhashtable_init (&p->vlgrp->vlan_hash, &br_vlan_rht_params); + ret =rhashtable_init (&vg->vlan_hash, &br_vlan_rht_params);if (ret) goto err_rhtbl; -INIT_LIST_HEAD (&p->vlgrp->vlan_list); +INIT_LIST_HEAD (&vg->vlan_list); +/* Make sure everything's committed before publishing vg */ +smp_wmb (); + p->vlgrp = vg;if (p->br->default_pvid) { ret = nbp_vlan_add(p, p->br->default_pvid, BRIDGE_VLAN_INFO_PVID |
@@ -875,9 +879,9 @@ out:return ret; err_vlan_add: -rhashtable_destroy (&p->vlgrp->vlan_hash); +rhashtable_destroy (&vg->vlan_hash); err_rhtbl: -kfree (p->vlgrp); +kfree (vg); goto out; }
Signature · 1 line
2.4.3
Re: [PATCH net-next 4/5] bridge: vlan: fix possible null ptr derefs on port init and deinit
Quoted 7 lines · Wed, Sep 30, 2015 at 09:16:54PM IDT, [email protected] wrote:
From: Nikolay Aleksandrov <[email protected]>
When a new port is being added we need to make vlgrp available after rhashtable has been initialized and when removing a port we need to flush the vlans and free the resources after we're sure noone can use the port, i.e. after it's removed from the port list and
synchronize_rcuis executed.
Hi Nikolay,
Changing the order of port deinit breaks symmetry with the init sequence. It also introduces a problem for switchdev drivers. Flushing the VLANs clears HW VLAN filters and then, when port is unlinked from bridge and CHANGEUPPER is received, port is configured to direct traffic to CPU (as it's not offloaded anymore). Doing the reverse (like in this patch) renders the port unusable.
Regarding the reason for this change, are you afraid that vlgrp will be accessed in bridge's rx handler or xmit function after it's freed? If so, maybe we can access it using RCU primitives? That way, both the rx handler and xmit function (executed under RCU lock) will either have a valid copy or not. Looking at previous iterations of this code, I see that was the case with the 'net_port_vlans' struct.
I can start working on a fix if you agree with the proposed solution.
Thanks.
Re: [PATCH net-next 4/5] bridge: vlan: fix possible null ptr derefs on port init and deinit
Quoted 25 lines · Ido Schimmel
Quoted 7 lines · Wed, Sep 30, 2015 at 09:16:54PM IDT, [email protected] wrote:
From: Nikolay Aleksandrov <[email protected]>
When a new port is being added we need to make vlgrp available after rhashtable has been initialized and when removing a port we need to flush the vlans and free the resources after we're sure noone can use the port, i.e. after it's removed from the port list and
synchronize_rcuis executed.Hi Nikolay,
Changing the order of port deinit breaks symmetry with the init sequence. It also introduces a problem for switchdev drivers. Flushing the VLANs clears HW VLAN filters and then, when port is unlinked from bridge and CHANGEUPPER is received, port is configured to direct traffic to CPU (as it's not offloaded anymore). Doing the reverse (like in this patch) renders the port unusable.
Regarding the reason for this change, are you afraid that vlgrp will be accessed in bridge's rx handler or xmit function after it's freed? If so, maybe we can access it using RCU primitives? That way, both the rx handler and xmit function (executed under RCU lock) will either have a valid copy or not. Looking at previous iterations of this code, I see that was the case with the '
net_port_vlans' struct.
Quoted 4 lines
I can start working on a fix if you agree with the proposed solution.
Thanks.
Hi,
Ah, I didn't know about this, I feared that something might rely on the particular order of the operations but didn't have a way to test this one in particular. Anyway, your proposed solution sounds good to me.
Thank you,
Nik
Nikolay Aleksandrov
maintainerRe: [PATCH net-next 4/5] bridge: vlan: fix possible null ptr derefs on port init and deinit
Quoted 31 lines · Nikolay Aleksandrov
Quoted 25 lines · Ido Schimmel
Quoted 7 lines · Wed, Sep 30, 2015 at 09:16:54PM IDT, [email protected] wrote:
From: Nikolay Aleksandrov <[email protected]>
When a new port is being added we need to make vlgrp available after rhashtable has been initialized and when removing a port we need to flush the vlans and free the resources after we're sure noone can use the port, i.e. after it's removed from the port list and
synchronize_rcuis executed.Hi Nikolay,
Changing the order of port deinit breaks symmetry with the init sequence. It also introduces a problem for switchdev drivers. Flushing the VLANs clears HW VLAN filters and then, when port is unlinked from bridge and CHANGEUPPER is received, port is configured to direct traffic to CPU (as it's not offloaded anymore). Doing the reverse (like in this patch) renders the port unusable.
Regarding the reason for this change, are you afraid that vlgrp will be accessed in bridge's rx handler or xmit function after it's freed? If so, maybe we can access it using RCU primitives? That way, both the rx handler and xmit function (executed under RCU lock) will either have a valid copy or not. Looking at previous iterations of this code, I see that was the case with the '
net_port_vlans' struct.Quoted 4 lines
I can start working on a fix if you agree with the proposed solution.
Thanks.
Quoted 7 lines
Hi,
Ah, I didn't know about this, I feared that something might rely on the particular order of the operations but didn't have a way to test this one in particular. Anyway, your proposed solution sounds good to me.Thank you,
Nik
One thing to be careful about is the creation/destruction of the rhashtable itself and the order of operations in regard to vlgrp visibility, so it's not only if vlgrp is visible or not - it should be visible after rhashtable has been initialized and should be removed before it's freed, the rest is pretty straight-forward.
Nikolay Aleksandrov
maintainerPATCH 5/5 bridge: vlan: don't pass flags when creating context only
In reply to Nikolay AleksandrovFrom: Nikolay Aleksandrov <[email protected]>
We should not pass the original flags when creating a context vlan only because they may contain some flags that change behaviour in the bridge. The new global context should be with minimal set of flags, so pass 0 and let br_vlan_add() set the master flag only.
--- net/bridge/br_vlan.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/bridge/br_vlan.c +1 -1
@@ -197,7 +197,7 @@ static int __vlan_add(struct net_bridge_vlan *v, u16 flags) masterv =br_vlan_find (br->vlgrp, v->vid);if (!masterv) {/* missing global ctx, create it now */ - err =br_vlan_add (br, v->vid, master_flags); + err =br_vlan_add (br, v->vid,0 );if (err) goto out_filt; masterv =br_vlan_find (br->vlgrp, v->vid);
Signature · 1 line
2.4.3
David Miller
maintainerFrom: Nikolay Aleksandrov <[email protected]>
Date: Wed, 30 Sep 2015 20:16:50 +0200
Quoted 11 lines
This is the first follow-up set, patch 01 reduces the default rhashtable size and the number of locks that can be allocated. Patch 02 and 04 fix possible null pointer dereferences due to the new ordering and initialization on port add/del, and patch 03 moves the "pvid" member in the
net_bridge_vlan_groupstruct in order to simplify code (similar to how it was with the older struct). Patch 05 fixes adding a vlan on a port which is pvid and doesn't have a global context yet.
Please review carefully, I think this is the first use of rhashtable's "locks_mul" member in the tree and I'd like to make sure it's correct. Another thing that needs special attention is thenbp_vlan_flush()move after therx_handlerunregister.
Series applied, thanks.