Skip to content
Snippets Groups Projects
Commit f72e3d81 authored by Xie He's avatar Xie He Committed by Greg Kroah-Hartman
Browse files

net: lapbether: Prevent racing when checking whether the netif is running


[ Upstream commit 5acd0cfb ]

There are two "netif_running" checks in this driver. One is in
"lapbeth_xmit" and the other is in "lapbeth_rcv". They serve to make
sure that the LAPB APIs called in these functions are called before
"lapb_unregister" is called by the "ndo_stop" function.

However, these "netif_running" checks are unreliable, because it's
possible that immediately after "netif_running" returns true, "ndo_stop"
is called (which causes "lapb_unregister" to be called).

This patch adds locking to make sure "lapbeth_xmit" and "lapbeth_rcv" can
reliably check and ensure the netif is running while doing their work.

Fixes: 1da177e4 ("Linux-2.6.12-rc2")
Signed-off-by: default avatarXie He <xie.he.0141@gmail.com>
Acked-by: default avatarMartin Schiller <ms@dev.tdt.de>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
parent 7cc0ba67
No related merge requests found
...@@ -51,6 +51,8 @@ struct lapbethdev { ...@@ -51,6 +51,8 @@ struct lapbethdev {
struct list_head node; struct list_head node;
struct net_device *ethdev; /* link to ethernet device */ struct net_device *ethdev; /* link to ethernet device */
struct net_device *axdev; /* lapbeth device (lapb#) */ struct net_device *axdev; /* lapbeth device (lapb#) */
bool up;
spinlock_t up_lock; /* Protects "up" */
}; };
static LIST_HEAD(lapbeth_devices); static LIST_HEAD(lapbeth_devices);
...@@ -98,8 +100,9 @@ static int lapbeth_rcv(struct sk_buff *skb, struct net_device *dev, struct packe ...@@ -98,8 +100,9 @@ static int lapbeth_rcv(struct sk_buff *skb, struct net_device *dev, struct packe
rcu_read_lock(); rcu_read_lock();
lapbeth = lapbeth_get_x25_dev(dev); lapbeth = lapbeth_get_x25_dev(dev);
if (!lapbeth) if (!lapbeth)
goto drop_unlock; goto drop_unlock_rcu;
if (!netif_running(lapbeth->axdev)) spin_lock_bh(&lapbeth->up_lock);
if (!lapbeth->up)
goto drop_unlock; goto drop_unlock;
len = skb->data[0] + skb->data[1] * 256; len = skb->data[0] + skb->data[1] * 256;
...@@ -114,11 +117,14 @@ static int lapbeth_rcv(struct sk_buff *skb, struct net_device *dev, struct packe ...@@ -114,11 +117,14 @@ static int lapbeth_rcv(struct sk_buff *skb, struct net_device *dev, struct packe
goto drop_unlock; goto drop_unlock;
} }
out: out:
spin_unlock_bh(&lapbeth->up_lock);
rcu_read_unlock(); rcu_read_unlock();
return 0; return 0;
drop_unlock: drop_unlock:
kfree_skb(skb); kfree_skb(skb);
goto out; goto out;
drop_unlock_rcu:
rcu_read_unlock();
drop: drop:
kfree_skb(skb); kfree_skb(skb);
return 0; return 0;
...@@ -148,13 +154,11 @@ static int lapbeth_data_indication(struct net_device *dev, struct sk_buff *skb) ...@@ -148,13 +154,11 @@ static int lapbeth_data_indication(struct net_device *dev, struct sk_buff *skb)
static netdev_tx_t lapbeth_xmit(struct sk_buff *skb, static netdev_tx_t lapbeth_xmit(struct sk_buff *skb,
struct net_device *dev) struct net_device *dev)
{ {
struct lapbethdev *lapbeth = netdev_priv(dev);
int err; int err;
/* spin_lock_bh(&lapbeth->up_lock);
* Just to be *really* sure not to send anything if the interface if (!lapbeth->up)
* is down, the ethernet device may have gone.
*/
if (!netif_running(dev))
goto drop; goto drop;
/* There should be a pseudo header of 1 byte added by upper layers. /* There should be a pseudo header of 1 byte added by upper layers.
...@@ -185,6 +189,7 @@ static netdev_tx_t lapbeth_xmit(struct sk_buff *skb, ...@@ -185,6 +189,7 @@ static netdev_tx_t lapbeth_xmit(struct sk_buff *skb,
goto drop; goto drop;
} }
out: out:
spin_unlock_bh(&lapbeth->up_lock);
return NETDEV_TX_OK; return NETDEV_TX_OK;
drop: drop:
kfree_skb(skb); kfree_skb(skb);
...@@ -276,6 +281,7 @@ static const struct lapb_register_struct lapbeth_callbacks = { ...@@ -276,6 +281,7 @@ static const struct lapb_register_struct lapbeth_callbacks = {
*/ */
static int lapbeth_open(struct net_device *dev) static int lapbeth_open(struct net_device *dev)
{ {
struct lapbethdev *lapbeth = netdev_priv(dev);
int err; int err;
if ((err = lapb_register(dev, &lapbeth_callbacks)) != LAPB_OK) { if ((err = lapb_register(dev, &lapbeth_callbacks)) != LAPB_OK) {
...@@ -283,13 +289,22 @@ static int lapbeth_open(struct net_device *dev) ...@@ -283,13 +289,22 @@ static int lapbeth_open(struct net_device *dev)
return -ENODEV; return -ENODEV;
} }
spin_lock_bh(&lapbeth->up_lock);
lapbeth->up = true;
spin_unlock_bh(&lapbeth->up_lock);
return 0; return 0;
} }
static int lapbeth_close(struct net_device *dev) static int lapbeth_close(struct net_device *dev)
{ {
struct lapbethdev *lapbeth = netdev_priv(dev);
int err; int err;
spin_lock_bh(&lapbeth->up_lock);
lapbeth->up = false;
spin_unlock_bh(&lapbeth->up_lock);
if ((err = lapb_unregister(dev)) != LAPB_OK) if ((err = lapb_unregister(dev)) != LAPB_OK)
pr_err("lapb_unregister error: %d\n", err); pr_err("lapb_unregister error: %d\n", err);
...@@ -347,6 +362,9 @@ static int lapbeth_new_device(struct net_device *dev) ...@@ -347,6 +362,9 @@ static int lapbeth_new_device(struct net_device *dev)
dev_hold(dev); dev_hold(dev);
lapbeth->ethdev = dev; lapbeth->ethdev = dev;
lapbeth->up = false;
spin_lock_init(&lapbeth->up_lock);
rc = -EIO; rc = -EIO;
if (register_netdevice(ndev)) if (register_netdevice(ndev))
goto fail; goto fail;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment