ns proc: Return -ENOENT for a nonexistent /proc/self/ns/ entry.
[deliverable/linux.git] / net / core / net_namespace.c
CommitLineData
5f256bec
EB
1#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>
9dd776b6 7#include <linux/sched.h>
c93cf61f 8#include <linux/idr.h>
11a28d37 9#include <linux/rculist.h>
30ffee84 10#include <linux/nsproxy.h>
f0630529
EB
11#include <linux/proc_fs.h>
12#include <linux/file.h>
5f256bec 13#include <net/net_namespace.h>
dec827d1 14#include <net/netns/generic.h>
5f256bec
EB
15
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
5f256bec 24LIST_HEAD(net_namespace_list);
b76a461f 25EXPORT_SYMBOL_GPL(net_namespace_list);
5f256bec 26
5f256bec 27struct net init_net;
ff4b9502 28EXPORT_SYMBOL(init_net);
5f256bec 29
dec827d1
PE
30#define INITIAL_NET_GEN_PTRS 13 /* +1 for len +2 for rcu_head */
31
05fceb4a
JP
32static 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
1c87733d
ED
47 old_ng = rcu_dereference_protected(net->gen,
48 lockdep_is_held(&net_mutex));
49 ng = old_ng;
05fceb4a
JP
50 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
f875bae0
EB
79static 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
72ad937a
EB
106static 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 }
72ad937a
EB
114 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
5f256bec
EB
128/*
129 * setup_net runs the initializers for the network namespace object.
130 */
1a2ee93d 131static __net_init int setup_net(struct net *net)
5f256bec
EB
132{
133 /* Must be called with net_mutex held */
f875bae0 134 const struct pernet_operations *ops, *saved_ops;
486a87f1 135 int error = 0;
72ad937a 136 LIST_HEAD(net_exit_list);
5f256bec 137
5f256bec 138 atomic_set(&net->count, 1);
486a87f1 139
5d1e4468 140#ifdef NETNS_REFCNT_DEBUG
5f256bec 141 atomic_set(&net->use_count, 0);
5d1e4468 142#endif
5f256bec 143
768f3591 144 list_for_each_entry(ops, &pernet_list, list) {
f875bae0
EB
145 error = ops_init(ops, net);
146 if (error < 0)
147 goto out_undo;
5f256bec
EB
148 }
149out:
150 return error;
768f3591 151
5f256bec
EB
152out_undo:
153 /* Walk through the list backwards calling the exit functions
154 * for the pernet modules whose init functions did not fail.
155 */
72ad937a 156 list_add(&net->exit_list, &net_exit_list);
f875bae0 157 saved_ops = ops;
72ad937a
EB
158 list_for_each_entry_continue_reverse(ops, &pernet_list, list)
159 ops_exit_list(ops, &net_exit_list);
160
f875bae0
EB
161 ops = saved_ops;
162 list_for_each_entry_continue_reverse(ops, &pernet_list, list)
72ad937a 163 ops_free_list(ops, &net_exit_list);
310928d9
DL
164
165 rcu_barrier();
5f256bec
EB
166 goto out;
167}
168
486a87f1 169static struct net_generic *net_alloc_generic(void)
6a1a3b9f 170{
486a87f1
DL
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;
6a1a3b9f
PE
180}
181
ebe47d47
CN
182#ifdef CONFIG_NET_NS
183static struct kmem_cache *net_cachep;
184static struct workqueue_struct *netns_wq;
185
486a87f1 186static struct net *net_alloc(void)
45a19b0a 187{
486a87f1
DL
188 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);
45a19b0a 196 if (!net)
486a87f1 197 goto out_free;
45a19b0a 198
486a87f1
DL
199 rcu_assign_pointer(net->gen, ng);
200out:
201 return net;
202
203out_free:
204 kfree(ng);
205 goto out;
206}
207
208static void net_free(struct net *net)
209{
5d1e4468 210#ifdef NETNS_REFCNT_DEBUG
45a19b0a
JFS
211 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 }
5d1e4468 216#endif
4ef079cc 217 kfree(net->gen);
45a19b0a
JFS
218 kmem_cache_free(net_cachep, net);
219}
220
088eb2d9 221static struct net *net_create(void)
9dd776b6 222{
088eb2d9
AD
223 struct net *net;
224 int rv;
9dd776b6 225
088eb2d9
AD
226 net = net_alloc();
227 if (!net)
228 return ERR_PTR(-ENOMEM);
9dd776b6 229 mutex_lock(&net_mutex);
088eb2d9
AD
230 rv = setup_net(net);
231 if (rv == 0) {
486a87f1 232 rtnl_lock();
11a28d37 233 list_add_tail_rcu(&net->list, &net_namespace_list);
486a87f1
DL
234 rtnl_unlock();
235 }
9dd776b6 236 mutex_unlock(&net_mutex);
088eb2d9
AD
237 if (rv < 0) {
238 net_free(net);
239 return ERR_PTR(rv);
240 }
241 return net;
242}
486a87f1 243
088eb2d9
AD
244struct 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();
9dd776b6
EB
249}
250
2b035b39
EB
251static DEFINE_SPINLOCK(cleanup_list_lock);
252static LIST_HEAD(cleanup_list); /* Must hold cleanup_list_lock to touch */
253
6a1a3b9f
PE
254static void cleanup_net(struct work_struct *work)
255{
f875bae0 256 const struct pernet_operations *ops;
2b035b39
EB
257 struct net *net, *tmp;
258 LIST_HEAD(net_kill_list);
72ad937a 259 LIST_HEAD(net_exit_list);
6a1a3b9f 260
2b035b39
EB
261 /* 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);
6a1a3b9f
PE
265
266 mutex_lock(&net_mutex);
267
268 /* Don't let anyone else find us. */
269 rtnl_lock();
72ad937a 270 list_for_each_entry(net, &net_kill_list, cleanup_list) {
2b035b39 271 list_del_rcu(&net->list);
72ad937a
EB
272 list_add_tail(&net->exit_list, &net_exit_list);
273 }
6a1a3b9f
PE
274 rtnl_unlock();
275
11a28d37
JB
276 /*
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
6a1a3b9f 283 /* Run all of the network namespace exit methods */
72ad937a
EB
284 list_for_each_entry_reverse(ops, &pernet_list, list)
285 ops_exit_list(ops, &net_exit_list);
286
f875bae0 287 /* Free the net generic variables */
72ad937a
EB
288 list_for_each_entry_reverse(ops, &pernet_list, list)
289 ops_free_list(ops, &net_exit_list);
6a1a3b9f
PE
290
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 */
72ad937a
EB
299 list_for_each_entry_safe(net, tmp, &net_exit_list, exit_list) {
300 list_del_init(&net->exit_list);
2b035b39
EB
301 net_free(net);
302 }
6a1a3b9f 303}
2b035b39 304static DECLARE_WORK(net_cleanup_work, cleanup_net);
6a1a3b9f
PE
305
306void __put_net(struct net *net)
307{
308 /* Cleanup the network namespace in process context */
2b035b39
EB
309 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);
6a1a3b9f
PE
316}
317EXPORT_SYMBOL_GPL(__put_net);
318
319#else
320struct net *copy_net_ns(unsigned long flags, struct net *old_net)
321{
322 if (flags & CLONE_NEWNET)
323 return ERR_PTR(-EINVAL);
324 return old_net;
325}
326#endif
327
30ffee84
JB
328struct net *get_net_ns_by_pid(pid_t pid)
329{
330 struct task_struct *tsk;
331 struct net *net;
332
333 /* Lookup the network namespace */
334 net = ERR_PTR(-ESRCH);
335 rcu_read_lock();
336 tsk = find_task_by_vpid(pid);
337 if (tsk) {
338 struct nsproxy *nsproxy;
339 nsproxy = task_nsproxy(tsk);
340 if (nsproxy)
341 net = get_net(nsproxy->net_ns);
342 }
343 rcu_read_unlock();
344 return net;
345}
346EXPORT_SYMBOL_GPL(get_net_ns_by_pid);
347
f0630529
EB
348struct 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);
364out:
365 if (file)
366 fput(file);
367 return net;
368}
369
5f256bec
EB
370static int __init net_ns_init(void)
371{
486a87f1 372 struct net_generic *ng;
5f256bec 373
d57a9212 374#ifdef CONFIG_NET_NS
5f256bec
EB
375 net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
376 SMP_CACHE_BYTES,
377 SLAB_PANIC, NULL);
3ef1355d
BT
378
379 /* Create workqueue for cleanup */
380 netns_wq = create_singlethread_workqueue("netns");
381 if (!netns_wq)
382 panic("Could not create netns workq");
d57a9212 383#endif
3ef1355d 384
486a87f1
DL
385 ng = net_alloc_generic();
386 if (!ng)
387 panic("Could not allocate generic netns");
388
389 rcu_assign_pointer(init_net.gen, ng);
390
5f256bec 391 mutex_lock(&net_mutex);
ca0f3112
SH
392 if (setup_net(&init_net))
393 panic("Could not setup the initial network namespace");
5f256bec 394
f4618d39 395 rtnl_lock();
11a28d37 396 list_add_tail_rcu(&init_net.list, &net_namespace_list);
f4618d39 397 rtnl_unlock();
5f256bec
EB
398
399 mutex_unlock(&net_mutex);
5f256bec
EB
400
401 return 0;
402}
403
404pure_initcall(net_ns_init);
405
ed160e83 406#ifdef CONFIG_NET_NS
f875bae0
EB
407static int __register_pernet_operations(struct list_head *list,
408 struct pernet_operations *ops)
5f256bec 409{
72ad937a 410 struct net *net;
5f256bec 411 int error;
72ad937a 412 LIST_HEAD(net_exit_list);
5f256bec 413
5f256bec 414 list_add_tail(&ops->list, list);
f875bae0 415 if (ops->init || (ops->id && ops->size)) {
1dba323b 416 for_each_net(net) {
f875bae0 417 error = ops_init(ops, net);
5f256bec
EB
418 if (error)
419 goto out_undo;
72ad937a 420 list_add_tail(&net->exit_list, &net_exit_list);
5f256bec
EB
421 }
422 }
1dba323b 423 return 0;
5f256bec
EB
424
425out_undo:
426 /* If I have an error cleanup all namespaces I initialized */
427 list_del(&ops->list);
72ad937a
EB
428 ops_exit_list(ops, &net_exit_list);
429 ops_free_list(ops, &net_exit_list);
1dba323b 430 return error;
5f256bec
EB
431}
432
f875bae0 433static void __unregister_pernet_operations(struct pernet_operations *ops)
5f256bec
EB
434{
435 struct net *net;
72ad937a 436 LIST_HEAD(net_exit_list);
5f256bec
EB
437
438 list_del(&ops->list);
72ad937a
EB
439 for_each_net(net)
440 list_add_tail(&net->exit_list, &net_exit_list);
441 ops_exit_list(ops, &net_exit_list);
442 ops_free_list(ops, &net_exit_list);
5f256bec
EB
443}
444
ed160e83
DL
445#else
446
f875bae0
EB
447static int __register_pernet_operations(struct list_head *list,
448 struct pernet_operations *ops)
ed160e83 449{
f875bae0
EB
450 int err = 0;
451 err = ops_init(ops, &init_net);
452 if (err)
453 ops_free(ops, &init_net);
454 return err;
455
ed160e83
DL
456}
457
f875bae0 458static void __unregister_pernet_operations(struct pernet_operations *ops)
ed160e83 459{
72ad937a
EB
460 LIST_HEAD(net_exit_list);
461 list_add(&init_net.exit_list, &net_exit_list);
462 ops_exit_list(ops, &net_exit_list);
463 ops_free_list(ops, &net_exit_list);
ed160e83 464}
f875bae0
EB
465
466#endif /* CONFIG_NET_NS */
ed160e83 467
c93cf61f
PE
468static DEFINE_IDA(net_generic_ids);
469
f875bae0
EB
470static int register_pernet_operations(struct list_head *list,
471 struct pernet_operations *ops)
472{
473 int error;
474
475 if (ops->id) {
476again:
477 error = ida_get_new_above(&net_generic_ids, 1, ops->id);
478 if (error < 0) {
479 if (error == -EAGAIN) {
480 ida_pre_get(&net_generic_ids, GFP_KERNEL);
481 goto again;
482 }
483 return error;
484 }
485 }
486 error = __register_pernet_operations(list, ops);
3a765eda
EB
487 if (error) {
488 rcu_barrier();
489 if (ops->id)
490 ida_remove(&net_generic_ids, *ops->id);
491 }
f875bae0
EB
492
493 return error;
494}
495
496static void unregister_pernet_operations(struct pernet_operations *ops)
497{
498
499 __unregister_pernet_operations(ops);
3a765eda 500 rcu_barrier();
f875bae0
EB
501 if (ops->id)
502 ida_remove(&net_generic_ids, *ops->id);
503}
504
5f256bec
EB
505/**
506 * register_pernet_subsys - register a network namespace subsystem
507 * @ops: pernet operations structure for the subsystem
508 *
509 * Register a subsystem which has init and exit functions
510 * that are called when network namespaces are created and
511 * destroyed respectively.
512 *
513 * When registered all network namespace init functions are
514 * called for every existing network namespace. Allowing kernel
515 * modules to have a race free view of the set of network namespaces.
516 *
517 * When a new network namespace is created all of the init
518 * methods are called in the order in which they were registered.
519 *
520 * When a network namespace is destroyed all of the exit methods
521 * are called in the reverse of the order with which they were
522 * registered.
523 */
524int register_pernet_subsys(struct pernet_operations *ops)
525{
526 int error;
527 mutex_lock(&net_mutex);
528 error = register_pernet_operations(first_device, ops);
529 mutex_unlock(&net_mutex);
530 return error;
531}
532EXPORT_SYMBOL_GPL(register_pernet_subsys);
533
534/**
535 * unregister_pernet_subsys - unregister a network namespace subsystem
536 * @ops: pernet operations structure to manipulate
537 *
538 * Remove the pernet operations structure from the list to be
53379e57 539 * used when network namespaces are created or destroyed. In
5f256bec
EB
540 * addition run the exit method for all existing network
541 * namespaces.
542 */
b3c981d2 543void unregister_pernet_subsys(struct pernet_operations *ops)
5f256bec
EB
544{
545 mutex_lock(&net_mutex);
b3c981d2 546 unregister_pernet_operations(ops);
5f256bec
EB
547 mutex_unlock(&net_mutex);
548}
549EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
550
551/**
552 * register_pernet_device - register a network namespace device
553 * @ops: pernet operations structure for the subsystem
554 *
555 * Register a device which has init and exit functions
556 * that are called when network namespaces are created and
557 * destroyed respectively.
558 *
559 * When registered all network namespace init functions are
560 * called for every existing network namespace. Allowing kernel
561 * modules to have a race free view of the set of network namespaces.
562 *
563 * When a new network namespace is created all of the init
564 * methods are called in the order in which they were registered.
565 *
566 * When a network namespace is destroyed all of the exit methods
567 * are called in the reverse of the order with which they were
568 * registered.
569 */
570int register_pernet_device(struct pernet_operations *ops)
571{
572 int error;
573 mutex_lock(&net_mutex);
574 error = register_pernet_operations(&pernet_list, ops);
575 if (!error && (first_device == &pernet_list))
576 first_device = &ops->list;
577 mutex_unlock(&net_mutex);
578 return error;
579}
580EXPORT_SYMBOL_GPL(register_pernet_device);
581
582/**
583 * unregister_pernet_device - unregister a network namespace netdevice
584 * @ops: pernet operations structure to manipulate
585 *
586 * Remove the pernet operations structure from the list to be
53379e57 587 * used when network namespaces are created or destroyed. In
5f256bec
EB
588 * addition run the exit method for all existing network
589 * namespaces.
590 */
591void unregister_pernet_device(struct pernet_operations *ops)
592{
593 mutex_lock(&net_mutex);
594 if (&ops->list == first_device)
595 first_device = first_device->next;
596 unregister_pernet_operations(ops);
597 mutex_unlock(&net_mutex);
598}
599EXPORT_SYMBOL_GPL(unregister_pernet_device);
13b6f576
EB
600
601#ifdef CONFIG_NET_NS
602static void *netns_get(struct task_struct *task)
603{
f0630529
EB
604 struct net *net = NULL;
605 struct nsproxy *nsproxy;
606
13b6f576 607 rcu_read_lock();
f0630529
EB
608 nsproxy = task_nsproxy(task);
609 if (nsproxy)
610 net = get_net(nsproxy->net_ns);
13b6f576 611 rcu_read_unlock();
f0630529 612
13b6f576
EB
613 return net;
614}
615
616static void netns_put(void *ns)
617{
618 put_net(ns);
619}
620
621static int netns_install(struct nsproxy *nsproxy, void *ns)
622{
623 put_net(nsproxy->net_ns);
624 nsproxy->net_ns = get_net(ns);
625 return 0;
626}
627
628const struct proc_ns_operations netns_operations = {
629 .name = "net",
630 .type = CLONE_NEWNET,
631 .get = netns_get,
632 .put = netns_put,
633 .install = netns_install,
634};
635#endif
This page took 0.348215 seconds and 5 git commands to generate.