diff options
author | Eric W. Biederman <ebiederm@xmission.com> | 2011-05-04 20:51:50 -0400 |
---|---|---|
committer | Eric W. Biederman <ebiederm@xmission.com> | 2011-05-10 17:36:03 -0400 |
commit | f063052947f770845a6252f7fa24f6f624592a24 (patch) | |
tree | 17513dbd49d5a1a08443d76374d10dda6114b7a7 | |
parent | a00eaf11a223c63fbb212369d6db69ce4c55a2d1 (diff) |
net: Allow setting the network namespace by fd
Take advantage of the new abstraction and allow network devices
to be placed in any network namespace that we have a fd to talk
about.
Acked-by: David S. Miller <davem@davemloft.net>
Acked-by: Daniel Lezcano <daniel.lezcano@free.fr>
Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
-rw-r--r-- | include/linux/if_link.h | 1 | ||||
-rw-r--r-- | include/net/net_namespace.h | 1 | ||||
-rw-r--r-- | net/core/net_namespace.c | 33 | ||||
-rw-r--r-- | net/core/rtnetlink.c | 5 |
4 files changed, 37 insertions, 3 deletions
diff --git a/include/linux/if_link.h b/include/linux/if_link.h index f4a2e6b1b864..0ee969a5593d 100644 --- a/include/linux/if_link.h +++ b/include/linux/if_link.h | |||
@@ -136,6 +136,7 @@ enum { | |||
136 | IFLA_PORT_SELF, | 136 | IFLA_PORT_SELF, |
137 | IFLA_AF_SPEC, | 137 | IFLA_AF_SPEC, |
138 | IFLA_GROUP, /* Group the device belongs to */ | 138 | IFLA_GROUP, /* Group the device belongs to */ |
139 | IFLA_NET_NS_FD, | ||
139 | __IFLA_MAX | 140 | __IFLA_MAX |
140 | }; | 141 | }; |
141 | 142 | ||
diff --git a/include/net/net_namespace.h b/include/net/net_namespace.h index 3ae491932bc8..dcc8f5749d3f 100644 --- a/include/net/net_namespace.h +++ b/include/net/net_namespace.h | |||
@@ -119,6 +119,7 @@ static inline struct net *copy_net_ns(unsigned long flags, struct net *net_ns) | |||
119 | extern struct list_head net_namespace_list; | 119 | extern struct list_head net_namespace_list; |
120 | 120 | ||
121 | extern struct net *get_net_ns_by_pid(pid_t pid); | 121 | extern struct net *get_net_ns_by_pid(pid_t pid); |
122 | extern struct net *get_net_ns_by_fd(int pid); | ||
122 | 123 | ||
123 | #ifdef CONFIG_NET_NS | 124 | #ifdef CONFIG_NET_NS |
124 | extern void __put_net(struct net *net); | 125 | extern void __put_net(struct net *net); |
diff --git a/net/core/net_namespace.c b/net/core/net_namespace.c index bf7707e09a80..b7403ff4d6c6 100644 --- a/net/core/net_namespace.c +++ b/net/core/net_namespace.c | |||
@@ -8,6 +8,8 @@ | |||
8 | #include <linux/idr.h> | 8 | #include <linux/idr.h> |
9 | #include <linux/rculist.h> | 9 | #include <linux/rculist.h> |
10 | #include <linux/nsproxy.h> | 10 | #include <linux/nsproxy.h> |
11 | #include <linux/proc_fs.h> | ||
12 | #include <linux/file.h> | ||
11 | #include <net/net_namespace.h> | 13 | #include <net/net_namespace.h> |
12 | #include <net/netns/generic.h> | 14 | #include <net/netns/generic.h> |
13 | 15 | ||
@@ -343,6 +345,28 @@ struct net *get_net_ns_by_pid(pid_t pid) | |||
343 | } | 345 | } |
344 | EXPORT_SYMBOL_GPL(get_net_ns_by_pid); | 346 | EXPORT_SYMBOL_GPL(get_net_ns_by_pid); |
345 | 347 | ||
348 | struct net *get_net_ns_by_fd(int fd) | ||
349 | { | ||
350 | struct proc_inode *ei; | ||
351 | struct file *file; | ||
352 | struct net *net; | ||
353 | |||
354 | net = ERR_PTR(-EINVAL); | ||
355 | file = proc_ns_fget(fd); | ||
356 | if (!file) | ||
357 | goto out; | ||
358 | |||
359 | ei = PROC_I(file->f_dentry->d_inode); | ||
360 | if (ei->ns_ops != &netns_operations) | ||
361 | goto out; | ||
362 | |||
363 | net = get_net(ei->ns); | ||
364 | out: | ||
365 | if (file) | ||
366 | fput(file); | ||
367 | return net; | ||
368 | } | ||
369 | |||
346 | static int __init net_ns_init(void) | 370 | static int __init net_ns_init(void) |
347 | { | 371 | { |
348 | struct net_generic *ng; | 372 | struct net_generic *ng; |
@@ -577,10 +601,15 @@ EXPORT_SYMBOL_GPL(unregister_pernet_device); | |||
577 | #ifdef CONFIG_NET_NS | 601 | #ifdef CONFIG_NET_NS |
578 | static void *netns_get(struct task_struct *task) | 602 | static void *netns_get(struct task_struct *task) |
579 | { | 603 | { |
580 | struct net *net; | 604 | struct net *net = NULL; |
605 | struct nsproxy *nsproxy; | ||
606 | |||
581 | rcu_read_lock(); | 607 | rcu_read_lock(); |
582 | net = get_net(task->nsproxy->net_ns); | 608 | nsproxy = task_nsproxy(task); |
609 | if (nsproxy) | ||
610 | net = get_net(nsproxy->net_ns); | ||
583 | rcu_read_unlock(); | 611 | rcu_read_unlock(); |
612 | |||
584 | return net; | 613 | return net; |
585 | } | 614 | } |
586 | 615 | ||
diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c index d7c4bb4b1820..dca9602c62e4 100644 --- a/net/core/rtnetlink.c +++ b/net/core/rtnetlink.c | |||
@@ -1043,6 +1043,7 @@ const struct nla_policy ifla_policy[IFLA_MAX+1] = { | |||
1043 | [IFLA_LINKMODE] = { .type = NLA_U8 }, | 1043 | [IFLA_LINKMODE] = { .type = NLA_U8 }, |
1044 | [IFLA_LINKINFO] = { .type = NLA_NESTED }, | 1044 | [IFLA_LINKINFO] = { .type = NLA_NESTED }, |
1045 | [IFLA_NET_NS_PID] = { .type = NLA_U32 }, | 1045 | [IFLA_NET_NS_PID] = { .type = NLA_U32 }, |
1046 | [IFLA_NET_NS_FD] = { .type = NLA_U32 }, | ||
1046 | [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 }, | 1047 | [IFLA_IFALIAS] = { .type = NLA_STRING, .len = IFALIASZ-1 }, |
1047 | [IFLA_VFINFO_LIST] = {. type = NLA_NESTED }, | 1048 | [IFLA_VFINFO_LIST] = {. type = NLA_NESTED }, |
1048 | [IFLA_VF_PORTS] = { .type = NLA_NESTED }, | 1049 | [IFLA_VF_PORTS] = { .type = NLA_NESTED }, |
@@ -1091,6 +1092,8 @@ struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[]) | |||
1091 | */ | 1092 | */ |
1092 | if (tb[IFLA_NET_NS_PID]) | 1093 | if (tb[IFLA_NET_NS_PID]) |
1093 | net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID])); | 1094 | net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID])); |
1095 | else if (tb[IFLA_NET_NS_FD]) | ||
1096 | net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD])); | ||
1094 | else | 1097 | else |
1095 | net = get_net(src_net); | 1098 | net = get_net(src_net); |
1096 | return net; | 1099 | return net; |
@@ -1221,7 +1224,7 @@ static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm, | |||
1221 | int send_addr_notify = 0; | 1224 | int send_addr_notify = 0; |
1222 | int err; | 1225 | int err; |
1223 | 1226 | ||
1224 | if (tb[IFLA_NET_NS_PID]) { | 1227 | if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) { |
1225 | struct net *net = rtnl_link_get_net(dev_net(dev), tb); | 1228 | struct net *net = rtnl_link_get_net(dev_net(dev), tb); |
1226 | if (IS_ERR(net)) { | 1229 | if (IS_ERR(net)) { |
1227 | err = PTR_ERR(net); | 1230 | err = PTR_ERR(net); |