Skip to content

Commit 311cca4

Browse files
kuba-mooPaolo Abeni
authored andcommitted
net: fix ifname in netlink ntf during netns move
dev_get_valid_name() overwrites the netdev's name on success. This makes it hard to use in prepare-commit-like fashion, where we do validation first, and "commit" to the change later. Factor out a helper which lets us save the new name to a buffer. Use it to fix the problem of notification on netns move having incorrect name: 5: eth0: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN group default link/ether be:4d:58:f9:d5:40 brd ff:ff:ff:ff:ff:ff 6: eth1: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN group default link/ether 1e:4a:34:36:e3:cd brd ff:ff:ff:ff:ff:ff [ ~]# ip link set dev eth0 netns 1 name eth1 ip monitor inside netns: Deleted inet eth0 Deleted inet6 eth0 Deleted 5: eth1: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN group default link/ether be:4d:58:f9:d5:40 brd ff:ff:ff:ff:ff:ff new-netnsid 0 new-ifindex 7 Name is reported as eth1 in old netns for ifindex 5, already renamed. Fixes: d903102 ("net: device name allocation cleanups") Signed-off-by: Jakub Kicinski <[email protected]> Reviewed-by: Jiri Pirko <[email protected]> Signed-off-by: Paolo Abeni <[email protected]>
1 parent a602ee3 commit 311cca4

File tree

1 file changed

+31
-13
lines changed

1 file changed

+31
-13
lines changed

net/core/dev.c

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,6 +1123,26 @@ static int __dev_alloc_name(struct net *net, const char *name, char *buf)
11231123
return -ENFILE;
11241124
}
11251125

1126+
static int dev_prep_valid_name(struct net *net, struct net_device *dev,
1127+
const char *want_name, char *out_name)
1128+
{
1129+
int ret;
1130+
1131+
if (!dev_valid_name(want_name))
1132+
return -EINVAL;
1133+
1134+
if (strchr(want_name, '%')) {
1135+
ret = __dev_alloc_name(net, want_name, out_name);
1136+
return ret < 0 ? ret : 0;
1137+
} else if (netdev_name_in_use(net, want_name)) {
1138+
return -EEXIST;
1139+
} else if (out_name != want_name) {
1140+
strscpy(out_name, want_name, IFNAMSIZ);
1141+
}
1142+
1143+
return 0;
1144+
}
1145+
11261146
static int dev_alloc_name_ns(struct net *net,
11271147
struct net_device *dev,
11281148
const char *name)
@@ -1160,19 +1180,13 @@ EXPORT_SYMBOL(dev_alloc_name);
11601180
static int dev_get_valid_name(struct net *net, struct net_device *dev,
11611181
const char *name)
11621182
{
1163-
BUG_ON(!net);
1164-
1165-
if (!dev_valid_name(name))
1166-
return -EINVAL;
1167-
1168-
if (strchr(name, '%'))
1169-
return dev_alloc_name_ns(net, dev, name);
1170-
else if (netdev_name_in_use(net, name))
1171-
return -EEXIST;
1172-
else if (dev->name != name)
1173-
strscpy(dev->name, name, IFNAMSIZ);
1183+
char buf[IFNAMSIZ];
1184+
int ret;
11741185

1175-
return 0;
1186+
ret = dev_prep_valid_name(net, dev, name, buf);
1187+
if (ret >= 0)
1188+
strscpy(dev->name, buf, IFNAMSIZ);
1189+
return ret;
11761190
}
11771191

11781192
/**
@@ -11038,6 +11052,7 @@ int __dev_change_net_namespace(struct net_device *dev, struct net *net,
1103811052
const char *pat, int new_ifindex)
1103911053
{
1104011054
struct net *net_old = dev_net(dev);
11055+
char new_name[IFNAMSIZ] = {};
1104111056
int err, new_nsid;
1104211057

1104311058
ASSERT_RTNL();
@@ -11064,7 +11079,7 @@ int __dev_change_net_namespace(struct net_device *dev, struct net *net,
1106411079
/* We get here if we can't use the current device name */
1106511080
if (!pat)
1106611081
goto out;
11067-
err = dev_get_valid_name(net, dev, pat);
11082+
err = dev_prep_valid_name(net, dev, pat, new_name);
1106811083
if (err < 0)
1106911084
goto out;
1107011085
}
@@ -11135,6 +11150,9 @@ int __dev_change_net_namespace(struct net_device *dev, struct net *net,
1113511150
kobject_uevent(&dev->dev.kobj, KOBJ_ADD);
1113611151
netdev_adjacent_add_links(dev);
1113711152

11153+
if (new_name[0]) /* Rename the netdev to prepared name */
11154+
strscpy(dev->name, new_name, IFNAMSIZ);
11155+
1113811156
/* Fixup kobjects */
1113911157
err = device_rename(&dev->dev, dev->name);
1114011158
WARN_ON(err);

0 commit comments

Comments
 (0)