Skip to content

Commit

Permalink
net: Support specifying the network namespace upon device creation.
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Eric W. Biederman authored and David S. Miller committed Nov 8, 2009
1 parent f7a3a1d commit 81adee4
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 23 deletions.
2 changes: 1 addition & 1 deletion drivers/net/can/dev.c
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ static int can_fill_xstats(struct sk_buff *skb, const struct net_device *dev)
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[])
{
return -EOPNOTSUPP;
Expand Down
4 changes: 2 additions & 2 deletions drivers/net/macvlan.c
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ static int macvlan_get_tx_queues(struct net *net,
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 macvlan_dev *vlan = netdev_priv(dev);
Expand All @@ -515,7 +515,7 @@ static int macvlan_newlink(struct net_device *dev,
if (!tb[IFLA_LINK])
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)
return -ENODEV;

Expand Down
15 changes: 12 additions & 3 deletions drivers/net/veth.c
Original file line number Diff line number Diff line change
Expand Up @@ -340,14 +340,15 @@ static int veth_validate(struct nlattr *tb[], struct nlattr *data[])

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[])
{
int err;
struct net_device *peer;
struct veth_priv *priv;
char ifname[IFNAMSIZ];
struct nlattr *peer_tb[IFLA_MAX + 1], **tbp;
struct net *net;

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

peer = rtnl_create_link(dev_net(dev), ifname, &veth_link_ops, tbp);
if (IS_ERR(peer))
net = rtnl_link_get_net(src_net, tbp);
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);
}

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

err = register_netdevice(peer);
put_net(net);
net = NULL;
if (err < 0)
goto err_register_peer;

Expand Down
8 changes: 5 additions & 3 deletions include/net/rtnetlink.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ struct rtnl_link_ops {
int (*validate)(struct nlattr *tb[],
struct nlattr *data[]);

int (*newlink)(struct net_device *dev,
int (*newlink)(struct net *src_net,
struct net_device *dev,
struct nlattr *tb[],
struct nlattr *data[]);
int (*changelink)(struct net_device *dev,
Expand Down Expand Up @@ -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 void rtnl_link_unregister(struct rtnl_link_ops *ops);

extern struct net_device *rtnl_create_link(struct net *net, char *ifname,
const struct rtnl_link_ops *ops, struct nlattr *tb[]);
extern struct net *rtnl_link_get_net(struct net *src_net, 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];

#define MODULE_ALIAS_RTNL_LINK(kind) MODULE_ALIAS("rtnl-link-" kind)
Expand Down
4 changes: 2 additions & 2 deletions net/8021q/vlan_netlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ static int vlan_get_tx_queues(struct net *net,
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 vlan_dev_info *vlan = vlan_dev_info(dev);
Expand All @@ -131,7 +131,7 @@ static int vlan_newlink(struct net_device *dev,

if (!tb[IFLA_LINK])
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)
return -ENODEV;

Expand Down
38 changes: 27 additions & 11 deletions net/core/rtnetlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,20 @@ static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
[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[])
{
if (dev) {
Expand All @@ -756,8 +770,7 @@ static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm,
int err;

if (tb[IFLA_NET_NS_PID]) {
struct net *net;
net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
struct net *net = rtnl_link_get_net(dev_net(dev), tb);
if (IS_ERR(net)) {
err = PTR_ERR(net);
goto errout;
Expand Down Expand Up @@ -976,16 +989,16 @@ static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
return 0;
}

struct net_device *rtnl_create_link(struct net *net, char *ifname,
const struct rtnl_link_ops *ops, struct nlattr *tb[])
struct net_device *rtnl_create_link(struct net *src_net, struct net *net,
char *ifname, const struct rtnl_link_ops *ops, struct nlattr *tb[])
{
int err;
struct net_device *dev;
unsigned int num_queues = 1;
unsigned int real_num_queues = 1;

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);
if (err)
goto err;
Expand All @@ -995,16 +1008,16 @@ struct net_device *rtnl_create_link(struct net *net, char *ifname,
if (!dev)
goto err;

dev_net_set(dev, net);
dev->rtnl_link_ops = ops;
dev->real_num_tx_queues = real_num_queues;

if (strchr(dev->name, '%')) {
err = dev_alloc_name(dev, dev->name);
if (err < 0)
goto err_free;
}

dev_net_set(dev, net);
dev->rtnl_link_ops = ops;

if (tb[IFLA_MTU])
dev->mtu = nla_get_u32(tb[IFLA_MTU]);
if (tb[IFLA_ADDRESS])
Expand Down Expand Up @@ -1083,6 +1096,7 @@ static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)

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

if (ops) {
if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
Expand Down Expand Up @@ -1147,17 +1161,19 @@ static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
if (!ifname[0])
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))
err = PTR_ERR(dev);
else if (ops->newlink)
err = ops->newlink(dev, tb, data);
err = ops->newlink(net, dev, tb, data);
else
err = register_netdevice(dev);

if (err < 0 && !IS_ERR(dev))
free_netdev(dev);

put_net(dest_net);
return err;
}
}
Expand Down
2 changes: 1 addition & 1 deletion net/ipv4/ip_gre.c
Original file line number Diff line number Diff line change
Expand Up @@ -1483,7 +1483,7 @@ static void ipgre_tap_setup(struct net_device *dev)
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 ip_tunnel *nt;
Expand Down

0 comments on commit 81adee4

Please sign in to comment.