aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEric W. Biederman <ebiederm@xmission.com>2007-09-27 01:04:26 -0400
committerDavid S. Miller <davem@sunset.davemloft.net>2007-10-10 19:52:46 -0400
commit9dd776b6d7b0b85966b6ddd03e2b2aae59012ab1 (patch)
treeed92aee1f242bb31a0965a4156063eac916ae15e
parent8b41d1887db718be9a2cd9e18c58ce25a4c7fd93 (diff)
[NET]: Add network namespace clone & unshare support.
This patch allows you to create a new network namespace using sys_clone, or sys_unshare. As the network namespace is still experimental and under development clone and unshare support is only made available when CONFIG_NET_NS is selected at compile time. As this patch introduces network namespace support into code paths that exist when the CONFIG_NET is not selected there are a few additions made to net_namespace.h to allow a few more functions to be used when the networking stack is not compiled in. Signed-off-by: Eric W. Biederman <ebiederm@xmission.com> Signed-off-by: David S. Miller <davem@davemloft.net>
-rw-r--r--include/linux/sched.h1
-rw-r--r--include/net/net_namespace.h18
-rw-r--r--kernel/fork.c3
-rw-r--r--kernel/nsproxy.c15
-rw-r--r--net/Kconfig8
-rw-r--r--net/core/net_namespace.c43
6 files changed, 83 insertions, 5 deletions
diff --git a/include/linux/sched.h b/include/linux/sched.h
index 313c6b6e774f..a4a141055c44 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -27,6 +27,7 @@
27#define CLONE_NEWUTS 0x04000000 /* New utsname group? */ 27#define CLONE_NEWUTS 0x04000000 /* New utsname group? */
28#define CLONE_NEWIPC 0x08000000 /* New ipcs */ 28#define CLONE_NEWIPC 0x08000000 /* New ipcs */
29#define CLONE_NEWUSER 0x10000000 /* New user namespace */ 29#define CLONE_NEWUSER 0x10000000 /* New user namespace */
30#define CLONE_NEWNET 0x20000000 /* New network namespace */
30 31
31/* 32/*
32 * Scheduling policies 33 * Scheduling policies
diff --git a/include/net/net_namespace.h b/include/net/net_namespace.h
index ac8f8304094e..3ea4194613ed 100644
--- a/include/net/net_namespace.h
+++ b/include/net/net_namespace.h
@@ -38,11 +38,23 @@ extern struct net init_net;
38 38
39extern struct list_head net_namespace_list; 39extern struct list_head net_namespace_list;
40 40
41#ifdef CONFIG_NET
42extern struct net *copy_net_ns(unsigned long flags, struct net *net_ns);
43#else
44static inline struct net *copy_net_ns(unsigned long flags, struct net *net_ns)
45{
46 /* There is nothing to copy so this is a noop */
47 return net_ns;
48}
49#endif
50
41extern void __put_net(struct net *net); 51extern void __put_net(struct net *net);
42 52
43static inline struct net *get_net(struct net *net) 53static inline struct net *get_net(struct net *net)
44{ 54{
55#ifdef CONFIG_NET
45 atomic_inc(&net->count); 56 atomic_inc(&net->count);
57#endif
46 return net; 58 return net;
47} 59}
48 60
@@ -60,19 +72,25 @@ static inline struct net *maybe_get_net(struct net *net)
60 72
61static inline void put_net(struct net *net) 73static inline void put_net(struct net *net)
62{ 74{
75#ifdef CONFIG_NET
63 if (atomic_dec_and_test(&net->count)) 76 if (atomic_dec_and_test(&net->count))
64 __put_net(net); 77 __put_net(net);
78#endif
65} 79}
66 80
67static inline struct net *hold_net(struct net *net) 81static inline struct net *hold_net(struct net *net)
68{ 82{
83#ifdef CONFIG_NET
69 atomic_inc(&net->use_count); 84 atomic_inc(&net->use_count);
85#endif
70 return net; 86 return net;
71} 87}
72 88
73static inline void release_net(struct net *net) 89static inline void release_net(struct net *net)
74{ 90{
91#ifdef CONFIG_NET
75 atomic_dec(&net->use_count); 92 atomic_dec(&net->use_count);
93#endif
76} 94}
77 95
78extern void net_lock(void); 96extern void net_lock(void);
diff --git a/kernel/fork.c b/kernel/fork.c
index 33f12f48684a..5e67f90a1694 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -1608,7 +1608,8 @@ asmlinkage long sys_unshare(unsigned long unshare_flags)
1608 err = -EINVAL; 1608 err = -EINVAL;
1609 if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND| 1609 if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND|
1610 CLONE_VM|CLONE_FILES|CLONE_SYSVSEM| 1610 CLONE_VM|CLONE_FILES|CLONE_SYSVSEM|
1611 CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWUSER)) 1611 CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWUSER|
1612 CLONE_NEWNET))
1612 goto bad_unshare_out; 1613 goto bad_unshare_out;
1613 1614
1614 if ((err = unshare_thread(unshare_flags))) 1615 if ((err = unshare_thread(unshare_flags)))
diff --git a/kernel/nsproxy.c b/kernel/nsproxy.c
index a4fb7d46971f..f1decd21a534 100644
--- a/kernel/nsproxy.c
+++ b/kernel/nsproxy.c
@@ -20,6 +20,7 @@
20#include <linux/mnt_namespace.h> 20#include <linux/mnt_namespace.h>
21#include <linux/utsname.h> 21#include <linux/utsname.h>
22#include <linux/pid_namespace.h> 22#include <linux/pid_namespace.h>
23#include <net/net_namespace.h>
23 24
24static struct kmem_cache *nsproxy_cachep; 25static struct kmem_cache *nsproxy_cachep;
25 26
@@ -98,8 +99,17 @@ static struct nsproxy *create_new_namespaces(unsigned long flags,
98 goto out_user; 99 goto out_user;
99 } 100 }
100 101
102 new_nsp->net_ns = copy_net_ns(flags, tsk->nsproxy->net_ns);
103 if (IS_ERR(new_nsp->net_ns)) {
104 err = PTR_ERR(new_nsp->net_ns);
105 goto out_net;
106 }
107
101 return new_nsp; 108 return new_nsp;
102 109
110out_net:
111 if (new_nsp->user_ns)
112 put_user_ns(new_nsp->user_ns);
103out_user: 113out_user:
104 if (new_nsp->pid_ns) 114 if (new_nsp->pid_ns)
105 put_pid_ns(new_nsp->pid_ns); 115 put_pid_ns(new_nsp->pid_ns);
@@ -132,7 +142,7 @@ int copy_namespaces(unsigned long flags, struct task_struct *tsk)
132 142
133 get_nsproxy(old_ns); 143 get_nsproxy(old_ns);
134 144
135 if (!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC | CLONE_NEWUSER))) 145 if (!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC | CLONE_NEWUSER | CLONE_NEWNET)))
136 return 0; 146 return 0;
137 147
138 if (!capable(CAP_SYS_ADMIN)) { 148 if (!capable(CAP_SYS_ADMIN)) {
@@ -164,6 +174,7 @@ void free_nsproxy(struct nsproxy *ns)
164 put_pid_ns(ns->pid_ns); 174 put_pid_ns(ns->pid_ns);
165 if (ns->user_ns) 175 if (ns->user_ns)
166 put_user_ns(ns->user_ns); 176 put_user_ns(ns->user_ns);
177 put_net(ns->net_ns);
167 kmem_cache_free(nsproxy_cachep, ns); 178 kmem_cache_free(nsproxy_cachep, ns);
168} 179}
169 180
@@ -177,7 +188,7 @@ int unshare_nsproxy_namespaces(unsigned long unshare_flags,
177 int err = 0; 188 int err = 0;
178 189
179 if (!(unshare_flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC | 190 if (!(unshare_flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC |
180 CLONE_NEWUSER))) 191 CLONE_NEWUSER | CLONE_NEWNET)))
181 return 0; 192 return 0;
182 193
183 if (!capable(CAP_SYS_ADMIN)) 194 if (!capable(CAP_SYS_ADMIN))
diff --git a/net/Kconfig b/net/Kconfig
index cdba08ca2efe..ab4e6da5012f 100644
--- a/net/Kconfig
+++ b/net/Kconfig
@@ -27,6 +27,14 @@ if NET
27 27
28menu "Networking options" 28menu "Networking options"
29 29
30config NET_NS
31 bool "Network namespace support"
32 default n
33 depends on EXPERIMENTAL && !SYSFS
34 help
35 Allow user space to create what appear to be multiple instances
36 of the network stack.
37
30source "net/packet/Kconfig" 38source "net/packet/Kconfig"
31source "net/unix/Kconfig" 39source "net/unix/Kconfig"
32source "net/xfrm/Kconfig" 40source "net/xfrm/Kconfig"
diff --git a/net/core/net_namespace.c b/net/core/net_namespace.c
index 0e6cb02d7b77..e478e353ea6b 100644
--- a/net/core/net_namespace.c
+++ b/net/core/net_namespace.c
@@ -4,6 +4,7 @@
4#include <linux/slab.h> 4#include <linux/slab.h>
5#include <linux/list.h> 5#include <linux/list.h>
6#include <linux/delay.h> 6#include <linux/delay.h>
7#include <linux/sched.h>
7#include <net/net_namespace.h> 8#include <net/net_namespace.h>
8 9
9/* 10/*
@@ -32,12 +33,10 @@ void net_unlock(void)
32 mutex_unlock(&net_list_mutex); 33 mutex_unlock(&net_list_mutex);
33} 34}
34 35
35#if 0
36static struct net *net_alloc(void) 36static struct net *net_alloc(void)
37{ 37{
38 return kmem_cache_alloc(net_cachep, GFP_KERNEL); 38 return kmem_cache_alloc(net_cachep, GFP_KERNEL);
39} 39}
40#endif
41 40
42static void net_free(struct net *net) 41static void net_free(struct net *net)
43{ 42{
@@ -128,6 +127,46 @@ out_undo:
128 goto out; 127 goto out;
129} 128}
130 129
130struct net *copy_net_ns(unsigned long flags, struct net *old_net)
131{
132 struct net *new_net = NULL;
133 int err;
134
135 get_net(old_net);
136
137 if (!(flags & CLONE_NEWNET))
138 return old_net;
139
140#ifndef CONFIG_NET_NS
141 return ERR_PTR(-EINVAL);
142#endif
143
144 err = -ENOMEM;
145 new_net = net_alloc();
146 if (!new_net)
147 goto out;
148
149 mutex_lock(&net_mutex);
150 err = setup_net(new_net);
151 if (err)
152 goto out_unlock;
153
154 net_lock();
155 list_add_tail(&new_net->list, &net_namespace_list);
156 net_unlock();
157
158
159out_unlock:
160 mutex_unlock(&net_mutex);
161out:
162 put_net(old_net);
163 if (err) {
164 net_free(new_net);
165 new_net = ERR_PTR(err);
166 }
167 return new_net;
168}
169
131static int __init net_ns_init(void) 170static int __init net_ns_init(void)
132{ 171{
133 int err; 172 int err;