blob: d4cf178bdfc74a104dcff231ea065ec79f8a7c3d [file] [log] [blame]
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001#include <linux/workqueue.h>
2#include <linux/rtnetlink.h>
3#include <linux/cache.h>
4#include <linux/slab.h>
5#include <linux/list.h>
6#include <linux/delay.h>
Eric W. Biederman9dd776b2007-09-26 22:04:26 -07007#include <linux/sched.h>
Pavel Emelyanovc93cf612008-04-15 00:35:23 -07008#include <linux/idr.h>
Johannes Berg11a28d32009-07-10 09:51:33 +00009#include <linux/rculist.h>
Johannes Berg30ffee82009-07-10 09:51:35 +000010#include <linux/nsproxy.h>
Eric W. Biedermanf0630522011-05-04 17:51:50 -070011#include <linux/proc_fs.h>
12#include <linux/file.h>
Eric W. Biederman5f256be2007-09-12 11:50:50 +020013#include <net/net_namespace.h>
Pavel Emelyanovdec827d2008-04-15 00:36:08 -070014#include <net/netns/generic.h>
Eric W. Biederman5f256be2007-09-12 11:50:50 +020015
16/*
17 * Our network namespace constructor/destructor lists
18 */
19
20static LIST_HEAD(pernet_list);
21static struct list_head *first_device = &pernet_list;
22static DEFINE_MUTEX(net_mutex);
23
Eric W. Biederman5f256be2007-09-12 11:50:50 +020024LIST_HEAD(net_namespace_list);
Alexey Dobriyanb76a4612008-10-08 11:35:06 +020025EXPORT_SYMBOL_GPL(net_namespace_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +020026
Eric W. Biederman5f256be2007-09-12 11:50:50 +020027struct net init_net;
Denis V. Lunevff4b9502008-01-22 22:05:33 -080028EXPORT_SYMBOL(init_net);
Eric W. Biederman5f256be2007-09-12 11:50:50 +020029
Pavel Emelyanovdec827d2008-04-15 00:36:08 -070030#define INITIAL_NET_GEN_PTRS 13 /* +1 for len +2 for rcu_head */
31
Jiri Pirko05fceb42010-04-23 01:40:47 +000032static void net_generic_release(struct rcu_head *rcu)
33{
34 struct net_generic *ng;
35
36 ng = container_of(rcu, struct net_generic, rcu);
37 kfree(ng);
38}
39
40static int net_assign_generic(struct net *net, int id, void *data)
41{
42 struct net_generic *ng, *old_ng;
43
44 BUG_ON(!mutex_is_locked(&net_mutex));
45 BUG_ON(id == 0);
46
Eric Dumazet1c877332010-10-25 03:20:11 +000047 old_ng = rcu_dereference_protected(net->gen,
48 lockdep_is_held(&net_mutex));
49 ng = old_ng;
Jiri Pirko05fceb42010-04-23 01:40:47 +000050 if (old_ng->len >= id)
51 goto assign;
52
53 ng = kzalloc(sizeof(struct net_generic) +
54 id * sizeof(void *), GFP_KERNEL);
55 if (ng == NULL)
56 return -ENOMEM;
57
58 /*
59 * Some synchronisation notes:
60 *
61 * The net_generic explores the net->gen array inside rcu
62 * read section. Besides once set the net->gen->ptr[x]
63 * pointer never changes (see rules in netns/generic.h).
64 *
65 * That said, we simply duplicate this array and schedule
66 * the old copy for kfree after a grace period.
67 */
68
69 ng->len = id;
70 memcpy(&ng->ptr, &old_ng->ptr, old_ng->len * sizeof(void*));
71
72 rcu_assign_pointer(net->gen, ng);
73 call_rcu(&old_ng->rcu, net_generic_release);
74assign:
75 ng->ptr[id - 1] = data;
76 return 0;
77}
78
Eric W. Biedermanf875bae2009-11-29 22:25:28 +000079static int ops_init(const struct pernet_operations *ops, struct net *net)
80{
81 int err;
82 if (ops->id && ops->size) {
83 void *data = kzalloc(ops->size, GFP_KERNEL);
84 if (!data)
85 return -ENOMEM;
86
87 err = net_assign_generic(net, *ops->id, data);
88 if (err) {
89 kfree(data);
90 return err;
91 }
92 }
93 if (ops->init)
94 return ops->init(net);
95 return 0;
96}
97
98static void ops_free(const struct pernet_operations *ops, struct net *net)
99{
100 if (ops->id && ops->size) {
101 int id = *ops->id;
102 kfree(net_generic(net, id));
103 }
104}
105
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000106static void ops_exit_list(const struct pernet_operations *ops,
107 struct list_head *net_exit_list)
108{
109 struct net *net;
110 if (ops->exit) {
111 list_for_each_entry(net, net_exit_list, exit_list)
112 ops->exit(net);
113 }
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000114 if (ops->exit_batch)
115 ops->exit_batch(net_exit_list);
116}
117
118static void ops_free_list(const struct pernet_operations *ops,
119 struct list_head *net_exit_list)
120{
121 struct net *net;
122 if (ops->size && ops->id) {
123 list_for_each_entry(net, net_exit_list, exit_list)
124 ops_free(ops, net);
125 }
126}
127
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700128/*
129 * setup_net runs the initializers for the network namespace object.
130 */
Pavel Emelyanov1a2ee932007-11-01 00:45:59 -0700131static __net_init int setup_net(struct net *net)
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700132{
133 /* Must be called with net_mutex held */
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000134 const struct pernet_operations *ops, *saved_ops;
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800135 int error = 0;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000136 LIST_HEAD(net_exit_list);
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700137
138 atomic_set(&net->count, 1);
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800139
Denis V. Lunev5d1e4462008-04-16 01:58:04 -0700140#ifdef NETNS_REFCNT_DEBUG
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700141 atomic_set(&net->use_count, 0);
Denis V. Lunev5d1e4462008-04-16 01:58:04 -0700142#endif
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700143
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700144 list_for_each_entry(ops, &pernet_list, list) {
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000145 error = ops_init(ops, net);
146 if (error < 0)
147 goto out_undo;
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700148 }
149out:
150 return error;
151
152out_undo:
153 /* Walk through the list backwards calling the exit functions
154 * for the pernet modules whose init functions did not fail.
155 */
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000156 list_add(&net->exit_list, &net_exit_list);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000157 saved_ops = ops;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000158 list_for_each_entry_continue_reverse(ops, &pernet_list, list)
159 ops_exit_list(ops, &net_exit_list);
160
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000161 ops = saved_ops;
162 list_for_each_entry_continue_reverse(ops, &pernet_list, list)
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000163 ops_free_list(ops, &net_exit_list);
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700164
165 rcu_barrier();
166 goto out;
167}
168
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800169static struct net_generic *net_alloc_generic(void)
170{
171 struct net_generic *ng;
172 size_t generic_size = sizeof(struct net_generic) +
173 INITIAL_NET_GEN_PTRS * sizeof(void *);
174
175 ng = kzalloc(generic_size, GFP_KERNEL);
176 if (ng)
177 ng->len = INITIAL_NET_GEN_PTRS;
178
179 return ng;
180}
181
Clemens Nossebe47d42009-02-23 15:37:35 -0800182#ifdef CONFIG_NET_NS
183static struct kmem_cache *net_cachep;
184static struct workqueue_struct *netns_wq;
185
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200186static struct net *net_alloc(void)
187{
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800188 struct net *net = NULL;
189 struct net_generic *ng;
190
191 ng = net_alloc_generic();
192 if (!ng)
193 goto out;
194
195 net = kmem_cache_zalloc(net_cachep, GFP_KERNEL);
196 if (!net)
197 goto out_free;
198
199 rcu_assign_pointer(net->gen, ng);
200out:
201 return net;
202
203out_free:
204 kfree(ng);
205 goto out;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200206}
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200207
Johann Felix Soden45a19b02007-11-07 01:30:30 -0800208static void net_free(struct net *net)
209{
Denis V. Lunev5d1e4462008-04-16 01:58:04 -0700210#ifdef NETNS_REFCNT_DEBUG
Johann Felix Soden45a19b02007-11-07 01:30:30 -0800211 if (unlikely(atomic_read(&net->use_count) != 0)) {
212 printk(KERN_EMERG "network namespace not free! Usage: %d\n",
213 atomic_read(&net->use_count));
214 return;
215 }
Denis V. Lunev5d1e4462008-04-16 01:58:04 -0700216#endif
Alexey Dobriyan4ef079c2008-10-14 22:54:48 -0700217 kfree(net->gen);
Johann Felix Soden45a19b02007-11-07 01:30:30 -0800218 kmem_cache_free(net_cachep, net);
219}
220
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700221static struct net *net_create(void)
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700222{
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700223 struct net *net;
224 int rv;
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700225
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700226 net = net_alloc();
227 if (!net)
228 return ERR_PTR(-ENOMEM);
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700229 mutex_lock(&net_mutex);
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700230 rv = setup_net(net);
231 if (rv == 0) {
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800232 rtnl_lock();
Johannes Berg11a28d32009-07-10 09:51:33 +0000233 list_add_tail_rcu(&net->list, &net_namespace_list);
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800234 rtnl_unlock();
235 }
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700236 mutex_unlock(&net_mutex);
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700237 if (rv < 0) {
238 net_free(net);
239 return ERR_PTR(rv);
240 }
241 return net;
242}
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800243
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700244struct net *copy_net_ns(unsigned long flags, struct net *old_net)
245{
246 if (!(flags & CLONE_NEWNET))
247 return get_net(old_net);
248 return net_create();
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700249}
250
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000251static DEFINE_SPINLOCK(cleanup_list_lock);
252static LIST_HEAD(cleanup_list); /* Must hold cleanup_list_lock to touch */
253
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200254static void cleanup_net(struct work_struct *work)
255{
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000256 const struct pernet_operations *ops;
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000257 struct net *net, *tmp;
258 LIST_HEAD(net_kill_list);
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000259 LIST_HEAD(net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200260
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000261 /* Atomically snapshot the list of namespaces to cleanup */
262 spin_lock_irq(&cleanup_list_lock);
263 list_replace_init(&cleanup_list, &net_kill_list);
264 spin_unlock_irq(&cleanup_list_lock);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200265
266 mutex_lock(&net_mutex);
267
268 /* Don't let anyone else find us. */
Eric W. Biedermanf4618d32007-09-26 22:40:08 -0700269 rtnl_lock();
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000270 list_for_each_entry(net, &net_kill_list, cleanup_list) {
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000271 list_del_rcu(&net->list);
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000272 list_add_tail(&net->exit_list, &net_exit_list);
273 }
Eric W. Biedermanf4618d32007-09-26 22:40:08 -0700274 rtnl_unlock();
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200275
Johannes Berg11a28d32009-07-10 09:51:33 +0000276 /*
277 * Another CPU might be rcu-iterating the list, wait for it.
278 * This needs to be before calling the exit() notifiers, so
279 * the rcu_barrier() below isn't sufficient alone.
280 */
281 synchronize_rcu();
282
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200283 /* Run all of the network namespace exit methods */
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000284 list_for_each_entry_reverse(ops, &pernet_list, list)
285 ops_exit_list(ops, &net_exit_list);
286
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000287 /* Free the net generic variables */
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000288 list_for_each_entry_reverse(ops, &pernet_list, list)
289 ops_free_list(ops, &net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200290
291 mutex_unlock(&net_mutex);
292
293 /* Ensure there are no outstanding rcu callbacks using this
294 * network namespace.
295 */
296 rcu_barrier();
297
298 /* Finally it is safe to free my network namespace structure */
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000299 list_for_each_entry_safe(net, tmp, &net_exit_list, exit_list) {
300 list_del_init(&net->exit_list);
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000301 net_free(net);
302 }
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200303}
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000304static DECLARE_WORK(net_cleanup_work, cleanup_net);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200305
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200306void __put_net(struct net *net)
307{
308 /* Cleanup the network namespace in process context */
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000309 unsigned long flags;
310
311 spin_lock_irqsave(&cleanup_list_lock, flags);
312 list_add(&net->cleanup_list, &cleanup_list);
313 spin_unlock_irqrestore(&cleanup_list_lock, flags);
314
315 queue_work(netns_wq, &net_cleanup_work);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200316}
317EXPORT_SYMBOL_GPL(__put_net);
318
Stephen Rothwell956c9202011-05-12 13:51:13 +1000319struct net *get_net_ns_by_fd(int fd)
320{
321 struct proc_inode *ei;
322 struct file *file;
323 struct net *net;
324
325 net = ERR_PTR(-EINVAL);
326 file = proc_ns_fget(fd);
327 if (!file)
328 goto out;
329
330 ei = PROC_I(file->f_dentry->d_inode);
331 if (ei->ns_ops != &netns_operations)
332 goto out;
333
334 net = get_net(ei->ns);
335out:
336 if (file)
337 fput(file);
338 return net;
339}
340
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700341#else
Eric W. Biederman9dd776b2007-09-26 22:04:26 -0700342struct net *copy_net_ns(unsigned long flags, struct net *old_net)
343{
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700344 if (flags & CLONE_NEWNET)
345 return ERR_PTR(-EINVAL);
346 return old_net;
Eric W. Biederman9dd776b2007-09-26 22:04:26 -0700347}
Stephen Rothwell956c9202011-05-12 13:51:13 +1000348
349struct net *get_net_ns_by_fd(int fd)
350{
351 return ERR_PTR(-EINVAL);
352}
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700353#endif
Eric W. Biederman9dd776b2007-09-26 22:04:26 -0700354
Johannes Berg30ffee82009-07-10 09:51:35 +0000355struct net *get_net_ns_by_pid(pid_t pid)
356{
357 struct task_struct *tsk;
358 struct net *net;
359
360 /* Lookup the network namespace */
361 net = ERR_PTR(-ESRCH);
362 rcu_read_lock();
363 tsk = find_task_by_vpid(pid);
364 if (tsk) {
365 struct nsproxy *nsproxy;
366 nsproxy = task_nsproxy(tsk);
367 if (nsproxy)
368 net = get_net(nsproxy->net_ns);
369 }
370 rcu_read_unlock();
371 return net;
372}
373EXPORT_SYMBOL_GPL(get_net_ns_by_pid);
374
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200375static int __init net_ns_init(void)
376{
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800377 struct net_generic *ng;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200378
Pavel Emelyanovd57a9212007-11-01 00:46:50 -0700379#ifdef CONFIG_NET_NS
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200380 net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
381 SMP_CACHE_BYTES,
382 SLAB_PANIC, NULL);
Benjamin Thery3ef13552007-11-19 23:18:16 -0800383
384 /* Create workqueue for cleanup */
385 netns_wq = create_singlethread_workqueue("netns");
386 if (!netns_wq)
387 panic("Could not create netns workq");
Pavel Emelyanovd57a9212007-11-01 00:46:50 -0700388#endif
Benjamin Thery3ef13552007-11-19 23:18:16 -0800389
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800390 ng = net_alloc_generic();
391 if (!ng)
392 panic("Could not allocate generic netns");
393
394 rcu_assign_pointer(init_net.gen, ng);
395
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200396 mutex_lock(&net_mutex);
Stephen Hemmingerca0f3112009-05-21 15:10:31 -0700397 if (setup_net(&init_net))
398 panic("Could not setup the initial network namespace");
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200399
Eric W. Biedermanf4618d32007-09-26 22:40:08 -0700400 rtnl_lock();
Johannes Berg11a28d32009-07-10 09:51:33 +0000401 list_add_tail_rcu(&init_net.list, &net_namespace_list);
Eric W. Biedermanf4618d32007-09-26 22:40:08 -0700402 rtnl_unlock();
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200403
404 mutex_unlock(&net_mutex);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200405
406 return 0;
407}
408
409pure_initcall(net_ns_init);
410
Denis V. Luneved160e82007-11-13 03:23:21 -0800411#ifdef CONFIG_NET_NS
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000412static int __register_pernet_operations(struct list_head *list,
413 struct pernet_operations *ops)
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200414{
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000415 struct net *net;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200416 int error;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000417 LIST_HEAD(net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200418
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200419 list_add_tail(&ops->list, list);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000420 if (ops->init || (ops->id && ops->size)) {
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700421 for_each_net(net) {
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000422 error = ops_init(ops, net);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200423 if (error)
424 goto out_undo;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000425 list_add_tail(&net->exit_list, &net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200426 }
427 }
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700428 return 0;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200429
430out_undo:
431 /* If I have an error cleanup all namespaces I initialized */
432 list_del(&ops->list);
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000433 ops_exit_list(ops, &net_exit_list);
434 ops_free_list(ops, &net_exit_list);
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700435 return error;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200436}
437
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000438static void __unregister_pernet_operations(struct pernet_operations *ops)
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200439{
440 struct net *net;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000441 LIST_HEAD(net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200442
443 list_del(&ops->list);
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000444 for_each_net(net)
445 list_add_tail(&net->exit_list, &net_exit_list);
446 ops_exit_list(ops, &net_exit_list);
447 ops_free_list(ops, &net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200448}
449
Denis V. Luneved160e82007-11-13 03:23:21 -0800450#else
451
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000452static int __register_pernet_operations(struct list_head *list,
453 struct pernet_operations *ops)
454{
455 int err = 0;
456 err = ops_init(ops, &init_net);
457 if (err)
458 ops_free(ops, &init_net);
459 return err;
460
461}
462
463static void __unregister_pernet_operations(struct pernet_operations *ops)
464{
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000465 LIST_HEAD(net_exit_list);
466 list_add(&init_net.exit_list, &net_exit_list);
467 ops_exit_list(ops, &net_exit_list);
468 ops_free_list(ops, &net_exit_list);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000469}
470
471#endif /* CONFIG_NET_NS */
472
473static DEFINE_IDA(net_generic_ids);
474
Denis V. Luneved160e82007-11-13 03:23:21 -0800475static int register_pernet_operations(struct list_head *list,
476 struct pernet_operations *ops)
477{
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000478 int error;
479
480 if (ops->id) {
481again:
482 error = ida_get_new_above(&net_generic_ids, 1, ops->id);
483 if (error < 0) {
484 if (error == -EAGAIN) {
485 ida_pre_get(&net_generic_ids, GFP_KERNEL);
486 goto again;
487 }
488 return error;
489 }
490 }
491 error = __register_pernet_operations(list, ops);
Eric W. Biederman3a765ed2009-12-03 02:29:06 +0000492 if (error) {
493 rcu_barrier();
494 if (ops->id)
495 ida_remove(&net_generic_ids, *ops->id);
496 }
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000497
498 return error;
Denis V. Luneved160e82007-11-13 03:23:21 -0800499}
500
501static void unregister_pernet_operations(struct pernet_operations *ops)
502{
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000503
504 __unregister_pernet_operations(ops);
Eric W. Biederman3a765ed2009-12-03 02:29:06 +0000505 rcu_barrier();
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000506 if (ops->id)
507 ida_remove(&net_generic_ids, *ops->id);
Denis V. Luneved160e82007-11-13 03:23:21 -0800508}
Pavel Emelyanovc93cf612008-04-15 00:35:23 -0700509
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200510/**
511 * register_pernet_subsys - register a network namespace subsystem
512 * @ops: pernet operations structure for the subsystem
513 *
514 * Register a subsystem which has init and exit functions
515 * that are called when network namespaces are created and
516 * destroyed respectively.
517 *
518 * When registered all network namespace init functions are
519 * called for every existing network namespace. Allowing kernel
520 * modules to have a race free view of the set of network namespaces.
521 *
522 * When a new network namespace is created all of the init
523 * methods are called in the order in which they were registered.
524 *
525 * When a network namespace is destroyed all of the exit methods
526 * are called in the reverse of the order with which they were
527 * registered.
528 */
529int register_pernet_subsys(struct pernet_operations *ops)
530{
531 int error;
532 mutex_lock(&net_mutex);
533 error = register_pernet_operations(first_device, ops);
534 mutex_unlock(&net_mutex);
535 return error;
536}
537EXPORT_SYMBOL_GPL(register_pernet_subsys);
538
539/**
540 * unregister_pernet_subsys - unregister a network namespace subsystem
541 * @ops: pernet operations structure to manipulate
542 *
543 * Remove the pernet operations structure from the list to be
Oliver Pinter53379e52008-02-03 17:56:48 +0200544 * used when network namespaces are created or destroyed. In
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200545 * addition run the exit method for all existing network
546 * namespaces.
547 */
Jiri Pirkob3c981d2010-04-25 00:49:56 -0700548void unregister_pernet_subsys(struct pernet_operations *ops)
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200549{
550 mutex_lock(&net_mutex);
Jiri Pirkob3c981d2010-04-25 00:49:56 -0700551 unregister_pernet_operations(ops);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200552 mutex_unlock(&net_mutex);
553}
554EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
555
556/**
557 * register_pernet_device - register a network namespace device
558 * @ops: pernet operations structure for the subsystem
559 *
560 * Register a device which has init and exit functions
561 * that are called when network namespaces are created and
562 * destroyed respectively.
563 *
564 * When registered all network namespace init functions are
565 * called for every existing network namespace. Allowing kernel
566 * modules to have a race free view of the set of network namespaces.
567 *
568 * When a new network namespace is created all of the init
569 * methods are called in the order in which they were registered.
570 *
571 * When a network namespace is destroyed all of the exit methods
572 * are called in the reverse of the order with which they were
573 * registered.
574 */
575int register_pernet_device(struct pernet_operations *ops)
576{
577 int error;
578 mutex_lock(&net_mutex);
579 error = register_pernet_operations(&pernet_list, ops);
580 if (!error && (first_device == &pernet_list))
581 first_device = &ops->list;
582 mutex_unlock(&net_mutex);
583 return error;
584}
585EXPORT_SYMBOL_GPL(register_pernet_device);
586
587/**
588 * unregister_pernet_device - unregister a network namespace netdevice
589 * @ops: pernet operations structure to manipulate
590 *
591 * Remove the pernet operations structure from the list to be
Oliver Pinter53379e52008-02-03 17:56:48 +0200592 * used when network namespaces are created or destroyed. In
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200593 * addition run the exit method for all existing network
594 * namespaces.
595 */
596void unregister_pernet_device(struct pernet_operations *ops)
597{
598 mutex_lock(&net_mutex);
599 if (&ops->list == first_device)
600 first_device = first_device->next;
601 unregister_pernet_operations(ops);
602 mutex_unlock(&net_mutex);
603}
604EXPORT_SYMBOL_GPL(unregister_pernet_device);
Eric W. Biederman13b6f572010-03-07 18:14:23 -0800605
606#ifdef CONFIG_NET_NS
607static void *netns_get(struct task_struct *task)
608{
Eric W. Biedermanf0630522011-05-04 17:51:50 -0700609 struct net *net = NULL;
610 struct nsproxy *nsproxy;
611
Eric W. Biederman13b6f572010-03-07 18:14:23 -0800612 rcu_read_lock();
Eric W. Biedermanf0630522011-05-04 17:51:50 -0700613 nsproxy = task_nsproxy(task);
614 if (nsproxy)
615 net = get_net(nsproxy->net_ns);
Eric W. Biederman13b6f572010-03-07 18:14:23 -0800616 rcu_read_unlock();
Eric W. Biedermanf0630522011-05-04 17:51:50 -0700617
Eric W. Biederman13b6f572010-03-07 18:14:23 -0800618 return net;
619}
620
621static void netns_put(void *ns)
622{
623 put_net(ns);
624}
625
626static int netns_install(struct nsproxy *nsproxy, void *ns)
627{
628 put_net(nsproxy->net_ns);
629 nsproxy->net_ns = get_net(ns);
630 return 0;
631}
632
633const struct proc_ns_operations netns_operations = {
634 .name = "net",
635 .type = CLONE_NEWNET,
636 .get = netns_get,
637 .put = netns_put,
638 .install = netns_install,
639};
640#endif