Commit 828de4f6 authored by Gao feng's avatar Gao feng Committed by David S. Miller

net: dev: fix incorrect getting net device's name

When moving a nic from net namespace A to net namespace B,
in dev_change_net_namesapce,we call __dev_get_by_name to
decide if the netns B has the device has the same name.

if the netns B already has the same named device,we call
dev_get_valid_name to try to get a valid name for this nic in
the netns B,but net_device->nd_net still point to netns A now.

this patch fix it.
Signed-off-by: default avatarGao feng <gaofeng@cn.fujitsu.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 3fd91fb3
...@@ -959,18 +959,30 @@ int dev_alloc_name(struct net_device *dev, const char *name) ...@@ -959,18 +959,30 @@ int dev_alloc_name(struct net_device *dev, const char *name)
} }
EXPORT_SYMBOL(dev_alloc_name); EXPORT_SYMBOL(dev_alloc_name);
static int dev_get_valid_name(struct net_device *dev, const char *name) static int dev_alloc_name_ns(struct net *net,
struct net_device *dev,
const char *name)
{ {
struct net *net; char buf[IFNAMSIZ];
int ret;
BUG_ON(!dev_net(dev)); ret = __dev_alloc_name(net, name, buf);
net = dev_net(dev); if (ret >= 0)
strlcpy(dev->name, buf, IFNAMSIZ);
return ret;
}
static int dev_get_valid_name(struct net *net,
struct net_device *dev,
const char *name)
{
BUG_ON(!net);
if (!dev_valid_name(name)) if (!dev_valid_name(name))
return -EINVAL; return -EINVAL;
if (strchr(name, '%')) if (strchr(name, '%'))
return dev_alloc_name(dev, name); return dev_alloc_name_ns(net, dev, name);
else if (__dev_get_by_name(net, name)) else if (__dev_get_by_name(net, name))
return -EEXIST; return -EEXIST;
else if (dev->name != name) else if (dev->name != name)
...@@ -1006,7 +1018,7 @@ int dev_change_name(struct net_device *dev, const char *newname) ...@@ -1006,7 +1018,7 @@ int dev_change_name(struct net_device *dev, const char *newname)
memcpy(oldname, dev->name, IFNAMSIZ); memcpy(oldname, dev->name, IFNAMSIZ);
err = dev_get_valid_name(dev, newname); err = dev_get_valid_name(net, dev, newname);
if (err < 0) if (err < 0)
return err; return err;
...@@ -5589,7 +5601,7 @@ int register_netdevice(struct net_device *dev) ...@@ -5589,7 +5601,7 @@ int register_netdevice(struct net_device *dev)
dev->iflink = -1; dev->iflink = -1;
ret = dev_get_valid_name(dev, dev->name); ret = dev_get_valid_name(net, dev, dev->name);
if (ret < 0) if (ret < 0)
goto out; goto out;
...@@ -6233,7 +6245,7 @@ int dev_change_net_namespace(struct net_device *dev, struct net *net, const char ...@@ -6233,7 +6245,7 @@ int dev_change_net_namespace(struct net_device *dev, struct net *net, const char
/* We get here if we can't use the current device name */ /* We get here if we can't use the current device name */
if (!pat) if (!pat)
goto out; goto out;
if (dev_get_valid_name(dev, pat) < 0) if (dev_get_valid_name(net, dev, pat) < 0)
goto out; goto out;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment