Commit 81adee47 authored by Eric W. Biederman's avatar Eric W. Biederman Committed by David S. Miller

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: default avatarEric W. Biederman <ebiederm@aristanetworks.com>
Acked-by: default avatarEric Dumazet <eric.dumazet@gmail.com>
parent f7a3a1d8
...@@ -674,7 +674,7 @@ nla_put_failure: ...@@ -674,7 +674,7 @@ nla_put_failure:
return -EMSGSIZE; return -EMSGSIZE;
} }
static int can_newlink(struct net_device *dev, static int can_newlink(struct net *src_net, struct net_device *dev,
struct nlattr *tb[], struct nlattr *data[]) struct nlattr *tb[], struct nlattr *data[])
{ {
return -EOPNOTSUPP; return -EOPNOTSUPP;
......
...@@ -504,7 +504,7 @@ static int macvlan_get_tx_queues(struct net *net, ...@@ -504,7 +504,7 @@ static int macvlan_get_tx_queues(struct net *net,
return 0; return 0;
} }
static int macvlan_newlink(struct net_device *dev, static int macvlan_newlink(struct net *src_net, struct net_device *dev,
struct nlattr *tb[], struct nlattr *data[]) struct nlattr *tb[], struct nlattr *data[])
{ {
struct macvlan_dev *vlan = netdev_priv(dev); struct macvlan_dev *vlan = netdev_priv(dev);
...@@ -515,7 +515,7 @@ static int macvlan_newlink(struct net_device *dev, ...@@ -515,7 +515,7 @@ static int macvlan_newlink(struct net_device *dev,
if (!tb[IFLA_LINK]) if (!tb[IFLA_LINK])
return -EINVAL; return -EINVAL;
lowerdev = __dev_get_by_index(dev_net(dev), nla_get_u32(tb[IFLA_LINK])); lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
if (lowerdev == NULL) if (lowerdev == NULL)
return -ENODEV; return -ENODEV;
......
...@@ -340,7 +340,7 @@ static int veth_validate(struct nlattr *tb[], struct nlattr *data[]) ...@@ -340,7 +340,7 @@ static int veth_validate(struct nlattr *tb[], struct nlattr *data[])
static struct rtnl_link_ops veth_link_ops; static struct rtnl_link_ops veth_link_ops;
static int veth_newlink(struct net_device *dev, static int veth_newlink(struct net *src_net, struct net_device *dev,
struct nlattr *tb[], struct nlattr *data[]) struct nlattr *tb[], struct nlattr *data[])
{ {
int err; int err;
...@@ -348,6 +348,7 @@ static int veth_newlink(struct net_device *dev, ...@@ -348,6 +348,7 @@ static int veth_newlink(struct net_device *dev,
struct veth_priv *priv; struct veth_priv *priv;
char ifname[IFNAMSIZ]; char ifname[IFNAMSIZ];
struct nlattr *peer_tb[IFLA_MAX + 1], **tbp; struct nlattr *peer_tb[IFLA_MAX + 1], **tbp;
struct net *net;
/* /*
* create and register peer first * create and register peer first
...@@ -380,14 +381,22 @@ static int veth_newlink(struct net_device *dev, ...@@ -380,14 +381,22 @@ static int veth_newlink(struct net_device *dev,
else else
snprintf(ifname, IFNAMSIZ, DRV_NAME "%%d"); snprintf(ifname, IFNAMSIZ, DRV_NAME "%%d");
peer = rtnl_create_link(dev_net(dev), ifname, &veth_link_ops, tbp); net = rtnl_link_get_net(src_net, tbp);
if (IS_ERR(peer)) if (IS_ERR(net))
return PTR_ERR(net);
peer = rtnl_create_link(src_net, net, ifname, &veth_link_ops, tbp);
if (IS_ERR(peer)) {
put_net(net);
return PTR_ERR(peer); return PTR_ERR(peer);
}
if (tbp[IFLA_ADDRESS] == NULL) if (tbp[IFLA_ADDRESS] == NULL)
random_ether_addr(peer->dev_addr); random_ether_addr(peer->dev_addr);
err = register_netdevice(peer); err = register_netdevice(peer);
put_net(net);
net = NULL;
if (err < 0) if (err < 0)
goto err_register_peer; goto err_register_peer;
......
...@@ -55,7 +55,8 @@ struct rtnl_link_ops { ...@@ -55,7 +55,8 @@ struct rtnl_link_ops {
int (*validate)(struct nlattr *tb[], int (*validate)(struct nlattr *tb[],
struct nlattr *data[]); struct nlattr *data[]);
int (*newlink)(struct net_device *dev, int (*newlink)(struct net *src_net,
struct net_device *dev,
struct nlattr *tb[], struct nlattr *tb[],
struct nlattr *data[]); struct nlattr *data[]);
int (*changelink)(struct net_device *dev, int (*changelink)(struct net_device *dev,
...@@ -83,8 +84,9 @@ extern void rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops); ...@@ -83,8 +84,9 @@ extern void rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops);
extern int rtnl_link_register(struct rtnl_link_ops *ops); extern int rtnl_link_register(struct rtnl_link_ops *ops);
extern void rtnl_link_unregister(struct rtnl_link_ops *ops); extern void rtnl_link_unregister(struct rtnl_link_ops *ops);
extern struct net_device *rtnl_create_link(struct net *net, char *ifname, extern struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]);
const struct rtnl_link_ops *ops, struct nlattr *tb[]); extern struct net_device *rtnl_create_link(struct net *src_net, struct net *net,
char *ifname, const struct rtnl_link_ops *ops, struct nlattr *tb[]);
extern const struct nla_policy ifla_policy[IFLA_MAX+1]; extern const struct nla_policy ifla_policy[IFLA_MAX+1];
#define MODULE_ALIAS_RTNL_LINK(kind) MODULE_ALIAS("rtnl-link-" kind) #define MODULE_ALIAS_RTNL_LINK(kind) MODULE_ALIAS("rtnl-link-" kind)
......
...@@ -119,7 +119,7 @@ static int vlan_get_tx_queues(struct net *net, ...@@ -119,7 +119,7 @@ static int vlan_get_tx_queues(struct net *net,
return 0; return 0;
} }
static int vlan_newlink(struct net_device *dev, static int vlan_newlink(struct net *src_net, struct net_device *dev,
struct nlattr *tb[], struct nlattr *data[]) struct nlattr *tb[], struct nlattr *data[])
{ {
struct vlan_dev_info *vlan = vlan_dev_info(dev); struct vlan_dev_info *vlan = vlan_dev_info(dev);
...@@ -131,7 +131,7 @@ static int vlan_newlink(struct net_device *dev, ...@@ -131,7 +131,7 @@ static int vlan_newlink(struct net_device *dev,
if (!tb[IFLA_LINK]) if (!tb[IFLA_LINK])
return -EINVAL; return -EINVAL;
real_dev = __dev_get_by_index(dev_net(dev), nla_get_u32(tb[IFLA_LINK])); real_dev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
if (!real_dev) if (!real_dev)
return -ENODEV; return -ENODEV;
......
...@@ -733,6 +733,20 @@ static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = { ...@@ -733,6 +733,20 @@ static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
[IFLA_INFO_DATA] = { .type = NLA_NESTED }, [IFLA_INFO_DATA] = { .type = NLA_NESTED },
}; };
struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
{
struct net *net;
/* Examine the link attributes and figure out which
* network namespace we are talking about.
*/
if (tb[IFLA_NET_NS_PID])
net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
else
net = get_net(src_net);
return net;
}
EXPORT_SYMBOL(rtnl_link_get_net);
static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[]) static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
{ {
if (dev) { if (dev) {
...@@ -756,8 +770,7 @@ static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm, ...@@ -756,8 +770,7 @@ static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm,
int err; int err;
if (tb[IFLA_NET_NS_PID]) { if (tb[IFLA_NET_NS_PID]) {
struct net *net; struct net *net = rtnl_link_get_net(dev_net(dev), tb);
net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
if (IS_ERR(net)) { if (IS_ERR(net)) {
err = PTR_ERR(net); err = PTR_ERR(net);
goto errout; goto errout;
...@@ -976,8 +989,8 @@ static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) ...@@ -976,8 +989,8 @@ static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
return 0; return 0;
} }
struct net_device *rtnl_create_link(struct net *net, char *ifname, struct net_device *rtnl_create_link(struct net *src_net, struct net *net,
const struct rtnl_link_ops *ops, struct nlattr *tb[]) char *ifname, const struct rtnl_link_ops *ops, struct nlattr *tb[])
{ {
int err; int err;
struct net_device *dev; struct net_device *dev;
...@@ -985,7 +998,7 @@ struct net_device *rtnl_create_link(struct net *net, char *ifname, ...@@ -985,7 +998,7 @@ struct net_device *rtnl_create_link(struct net *net, char *ifname,
unsigned int real_num_queues = 1; unsigned int real_num_queues = 1;
if (ops->get_tx_queues) { if (ops->get_tx_queues) {
err = ops->get_tx_queues(net, tb, &num_queues, err = ops->get_tx_queues(src_net, tb, &num_queues,
&real_num_queues); &real_num_queues);
if (err) if (err)
goto err; goto err;
...@@ -995,16 +1008,16 @@ struct net_device *rtnl_create_link(struct net *net, char *ifname, ...@@ -995,16 +1008,16 @@ struct net_device *rtnl_create_link(struct net *net, char *ifname,
if (!dev) if (!dev)
goto err; goto err;
dev_net_set(dev, net);
dev->rtnl_link_ops = ops;
dev->real_num_tx_queues = real_num_queues; dev->real_num_tx_queues = real_num_queues;
if (strchr(dev->name, '%')) { if (strchr(dev->name, '%')) {
err = dev_alloc_name(dev, dev->name); err = dev_alloc_name(dev, dev->name);
if (err < 0) if (err < 0)
goto err_free; goto err_free;
} }
dev_net_set(dev, net);
dev->rtnl_link_ops = ops;
if (tb[IFLA_MTU]) if (tb[IFLA_MTU])
dev->mtu = nla_get_u32(tb[IFLA_MTU]); dev->mtu = nla_get_u32(tb[IFLA_MTU]);
if (tb[IFLA_ADDRESS]) if (tb[IFLA_ADDRESS])
...@@ -1083,6 +1096,7 @@ replay: ...@@ -1083,6 +1096,7 @@ replay:
if (1) { if (1) {
struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL; struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL;
struct net *dest_net;
if (ops) { if (ops) {
if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) { if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
...@@ -1147,17 +1161,19 @@ replay: ...@@ -1147,17 +1161,19 @@ replay:
if (!ifname[0]) if (!ifname[0])
snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind); snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
dev = rtnl_create_link(net, ifname, ops, tb); dest_net = rtnl_link_get_net(net, tb);
dev = rtnl_create_link(net, dest_net, ifname, ops, tb);
if (IS_ERR(dev)) if (IS_ERR(dev))
err = PTR_ERR(dev); err = PTR_ERR(dev);
else if (ops->newlink) else if (ops->newlink)
err = ops->newlink(dev, tb, data); err = ops->newlink(net, dev, tb, data);
else else
err = register_netdevice(dev); err = register_netdevice(dev);
if (err < 0 && !IS_ERR(dev)) if (err < 0 && !IS_ERR(dev))
free_netdev(dev); free_netdev(dev);
put_net(dest_net);
return err; return err;
} }
} }
......
...@@ -1483,7 +1483,7 @@ static void ipgre_tap_setup(struct net_device *dev) ...@@ -1483,7 +1483,7 @@ static void ipgre_tap_setup(struct net_device *dev)
dev->features |= NETIF_F_NETNS_LOCAL; dev->features |= NETIF_F_NETNS_LOCAL;
} }
static int ipgre_newlink(struct net_device *dev, struct nlattr *tb[], static int ipgre_newlink(struct net *src_net, struct net_device *dev, struct nlattr *tb[],
struct nlattr *data[]) struct nlattr *data[])
{ {
struct ip_tunnel *nt; struct ip_tunnel *nt;
......
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