Skip to content

Commit 317f481

Browse files
NicolasDichteldavem330
authored andcommitted
rtnl: allow to create device with IFLA_LINK_NETNSID set
This patch adds the ability to create a netdevice in a specified netns and then move it into the final netns. In fact, it allows to have a symetry between get and set rtnl messages. Signed-off-by: Nicolas Dichtel <nicolas.dichtel@6wind.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 1728d4f commit 317f481

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

net/core/rtnetlink.c

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,7 @@ static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
12481248
[IFLA_PHYS_PORT_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
12491249
[IFLA_CARRIER_CHANGES] = { .type = NLA_U32 }, /* ignored */
12501250
[IFLA_PHYS_SWITCH_ID] = { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1251+
[IFLA_LINK_NETNSID] = { .type = NLA_S32 },
12511252
};
12521253

12531254
static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
@@ -2021,7 +2022,7 @@ static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh)
20212022
struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 0];
20222023
struct nlattr **data = NULL;
20232024
struct nlattr **slave_data = NULL;
2024-
struct net *dest_net;
2025+
struct net *dest_net, *link_net = NULL;
20252026

20262027
if (ops) {
20272028
if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
@@ -2127,7 +2128,18 @@ static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh)
21272128
if (IS_ERR(dest_net))
21282129
return PTR_ERR(dest_net);
21292130

2130-
dev = rtnl_create_link(dest_net, ifname, name_assign_type, ops, tb);
2131+
if (tb[IFLA_LINK_NETNSID]) {
2132+
int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
2133+
2134+
link_net = get_net_ns_by_id(dest_net, id);
2135+
if (!link_net) {
2136+
err = -EINVAL;
2137+
goto out;
2138+
}
2139+
}
2140+
2141+
dev = rtnl_create_link(link_net ? : dest_net, ifname,
2142+
name_assign_type, ops, tb);
21312143
if (IS_ERR(dev)) {
21322144
err = PTR_ERR(dev);
21332145
goto out;
@@ -2155,9 +2167,16 @@ static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh)
21552167
}
21562168
}
21572169
err = rtnl_configure_link(dev, ifm);
2158-
if (err < 0)
2170+
if (err < 0) {
21592171
unregister_netdevice(dev);
2172+
goto out;
2173+
}
2174+
2175+
if (link_net)
2176+
err = dev_change_net_namespace(dev, dest_net, ifname);
21602177
out:
2178+
if (link_net)
2179+
put_net(link_net);
21612180
put_net(dest_net);
21622181
return err;
21632182
}

0 commit comments

Comments
 (0)