Skip to content

Commit 81adee4

Browse files
Eric W. Biedermandavem330
authored andcommitted
net: Support specifying the network namespace upon device creation.
There is no good reason to not support userspace specifying the network namespace during device creation, and it makes it easier to create a network device and pass it to a child network namespace with a well known name. We have to be careful to ensure that the target network namespace for the new device exists through the life of the call. To keep that logic clear I have factored out the network namespace grabbing logic into rtnl_link_get_net. In addtion we need to continue to pass the source network namespace to the rtnl_link_ops.newlink method so that we can find the base device source network namespace. Signed-off-by: Eric W. Biederman <ebiederm@aristanetworks.com> Acked-by: Eric Dumazet <eric.dumazet@gmail.com>
1 parent f7a3a1d commit 81adee4

7 files changed

Lines changed: 50 additions & 23 deletions

File tree

‎drivers/net/can/dev.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev)
674674
return -EMSGSIZE;
675675
}
676676

677-
static int can_newlink(struct net_device *dev,
677+
static int can_newlink(struct net *src_net, struct net_device *dev,
678678
struct nlattr *tb[], struct nlattr *data[])
679679
{
680680
return -EOPNOTSUPP;

‎drivers/net/macvlan.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ static int macvlan_get_tx_queues(struct net *net,
504504
return 0;
505505
}
506506

507-
static int macvlan_newlink(struct net_device *dev,
507+
static int macvlan_newlink(struct net *src_net, struct net_device *dev,
508508
struct nlattr *tb[], struct nlattr *data[])
509509
{
510510
struct macvlan_dev *vlan = netdev_priv(dev);
@@ -515,7 +515,7 @@ static int macvlan_newlink(struct net_device *dev,
515515
if (!tb[IFLA_LINK])
516516
return -EINVAL;
517517

518-
lowerdev = __dev_get_by_index(dev_net(dev), nla_get_u32(tb[IFLA_LINK]));
518+
lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
519519
if (lowerdev == NULL)
520520
return -ENODEV;
521521

‎drivers/net/veth.c‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,14 +340,15 @@ static int veth_validate(struct nlattr *tb[], struct nlattr *data[])
340340

341341
static struct rtnl_link_ops veth_link_ops;
342342

343-
static int veth_newlink(struct net_device *dev,
343+
static int veth_newlink(struct net *src_net, struct net_device *dev,
344344
struct nlattr *tb[], struct nlattr *data[])
345345
{
346346
int err;
347347
struct net_device *peer;
348348
struct veth_priv *priv;
349349
char ifname[IFNAMSIZ];
350350
struct nlattr *peer_tb[IFLA_MAX + 1], **tbp;
351+
struct net *net;
351352

352353
/*
353354
* create and register peer first
@@ -380,14 +381,22 @@ static int veth_newlink(struct net_device *dev,
380381
else
381382
snprintf(ifname, IFNAMSIZ, DRV_NAME "%%d");
382383

383-
peer = rtnl_create_link(dev_net(dev), ifname, &veth_link_ops, tbp);
384-
if (IS_ERR(peer))
384+
net = rtnl_link_get_net(src_net, tbp);
385+
if (IS_ERR(net))
386+
return PTR_ERR(net);
387+
388+
peer = rtnl_create_link(src_net, net, ifname, &veth_link_ops, tbp);
389+
if (IS_ERR(peer)) {
390+
put_net(net);
385391
return PTR_ERR(peer);
392+
}
386393

387394
if (tbp[IFLA_ADDRESS] == NULL)
388395
random_ether_addr(peer->dev_addr);
389396

390397
err = register_netdevice(peer);
398+
put_net(net);
399+
net = NULL;
391400
if (err < 0)
392401
goto err_register_peer;
393402

‎include/net/rtnetlink.h‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ struct rtnl_link_ops {
5555
int (*validate)(struct nlattr *tb[],
5656
struct nlattr *data[]);
5757

58-
int (*newlink)(struct net_device *dev,
58+
int (*newlink)(struct net *src_net,
59+
struct net_device *dev,
5960
struct nlattr *tb[],
6061
struct nlattr *data[]);
6162
int (*changelink)(struct net_device *dev,
@@ -83,8 +84,9 @@ extern void rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops);
8384
extern int rtnl_link_register(struct rtnl_link_ops *ops);
8485
extern void rtnl_link_unregister(struct rtnl_link_ops *ops);
8586

86-
extern struct net_device *rtnl_create_link(struct net *net, char *ifname,
87-
const struct rtnl_link_ops *ops, struct nlattr *tb[]);
87+
extern struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]);
88+
extern struct net_device *rtnl_create_link(struct net *src_net, struct net *net,
89+
char *ifname, const struct rtnl_link_ops *ops, struct nlattr *tb[]);
8890
extern const struct nla_policy ifla_policy[IFLA_MAX+1];
8991

9092
#define MODULE_ALIAS_RTNL_LINK(kind) MODULE_ALIAS("rtnl-link-" kind)

‎net/8021q/vlan_netlink.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ static int vlan_get_tx_queues(struct net *net,
119119
return 0;
120120
}
121121

122-
static int vlan_newlink(struct net_device *dev,
122+
static int vlan_newlink(struct net *src_net, struct net_device *dev,
123123
struct nlattr *tb[], struct nlattr *data[])
124124
{
125125
struct vlan_dev_info *vlan = vlan_dev_info(dev);
@@ -131,7 +131,7 @@ static int vlan_newlink(struct net_device *dev,
131131

132132
if (!tb[IFLA_LINK])
133133
return -EINVAL;
134-
real_dev = __dev_get_by_index(dev_net(dev), nla_get_u32(tb[IFLA_LINK]));
134+
real_dev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
135135
if (!real_dev)
136136
return -ENODEV;
137137

‎net/core/rtnetlink.c‎

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,20 @@ static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
733733
[IFLA_INFO_DATA] = { .type = NLA_NESTED },
734734
};
735735

736+
struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
737+
{
738+
struct net *net;
739+
/* Examine the link attributes and figure out which
740+
* network namespace we are talking about.
741+
*/
742+
if (tb[IFLA_NET_NS_PID])
743+
net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
744+
else
745+
net = get_net(src_net);
746+
return net;
747+
}
748+
EXPORT_SYMBOL(rtnl_link_get_net);
749+
736750
static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
737751
{
738752
if (dev) {
@@ -756,8 +770,7 @@ static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm,
756770
int err;
757771

758772
if (tb[IFLA_NET_NS_PID]) {
759-
struct net *net;
760-
net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
773+
struct net *net = rtnl_link_get_net(dev_net(dev), tb);
761774
if (IS_ERR(net)) {
762775
err = PTR_ERR(net);
763776
goto errout;
@@ -976,16 +989,16 @@ static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
976989
return 0;
977990
}
978991

979-
struct net_device *rtnl_create_link(struct net *net, char *ifname,
980-
const struct rtnl_link_ops *ops, struct nlattr *tb[])
992+
struct net_device *rtnl_create_link(struct net *src_net, struct net *net,
993+
char *ifname, const struct rtnl_link_ops *ops, struct nlattr *tb[])
981994
{
982995
int err;
983996
struct net_device *dev;
984997
unsigned int num_queues = 1;
985998
unsigned int real_num_queues = 1;
986999

9871000
if (ops->get_tx_queues) {
988-
err = ops->get_tx_queues(net, tb, &num_queues,
1001+
err = ops->get_tx_queues(src_net, tb, &num_queues,
9891002
&real_num_queues);
9901003
if (err)
9911004
goto err;
@@ -995,16 +1008,16 @@ struct net_device *rtnl_create_link(struct net *net, char *ifname,
9951008
if (!dev)
9961009
goto err;
9971010

1011+
dev_net_set(dev, net);
1012+
dev->rtnl_link_ops = ops;
9981013
dev->real_num_tx_queues = real_num_queues;
1014+
9991015
if (strchr(dev->name, '%')) {
10001016
err = dev_alloc_name(dev, dev->name);
10011017
if (err < 0)
10021018
goto err_free;
10031019
}
10041020

1005-
dev_net_set(dev, net);
1006-
dev->rtnl_link_ops = ops;
1007-
10081021
if (tb[IFLA_MTU])
10091022
dev->mtu = nla_get_u32(tb[IFLA_MTU]);
10101023
if (tb[IFLA_ADDRESS])
@@ -1083,6 +1096,7 @@ static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
10831096

10841097
if (1) {
10851098
struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL;
1099+
struct net *dest_net;
10861100

10871101
if (ops) {
10881102
if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
@@ -1147,17 +1161,19 @@ static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
11471161
if (!ifname[0])
11481162
snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
11491163

1150-
dev = rtnl_create_link(net, ifname, ops, tb);
1164+
dest_net = rtnl_link_get_net(net, tb);
1165+
dev = rtnl_create_link(net, dest_net, ifname, ops, tb);
11511166

11521167
if (IS_ERR(dev))
11531168
err = PTR_ERR(dev);
11541169
else if (ops->newlink)
1155-
err = ops->newlink(dev, tb, data);
1170+
err = ops->newlink(net, dev, tb, data);
11561171
else
11571172
err = register_netdevice(dev);
1158-
11591173
if (err < 0 && !IS_ERR(dev))
11601174
free_netdev(dev);
1175+
1176+
put_net(dest_net);
11611177
return err;
11621178
}
11631179
}

‎net/ipv4/ip_gre.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1483,7 +1483,7 @@ static void ipgre_tap_setup(struct net_device *dev)
14831483
dev->features |= NETIF_F_NETNS_LOCAL;
14841484
}
14851485

1486-
static int ipgre_newlink(struct net_device *dev, struct nlattr *tb[],
1486+
static int ipgre_newlink(struct net *src_net, struct net_device *dev, struct nlattr *tb[],
14871487
struct nlattr *data[])
14881488
{
14891489
struct ip_tunnel *nt;

0 commit comments

Comments
 (0)