]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/core/net_namespace.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mason/linux...
[mirror_ubuntu-artful-kernel.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>
bc3b2d7f 13#include <linux/export.h>
5f256bec 14#include <net/net_namespace.h>
dec827d1 15#include <net/netns/generic.h>
5f256bec
EB
16
17/*
18 * Our network namespace constructor/destructor lists
19 */
20
21static LIST_HEAD(pernet_list);
22static struct list_head *first_device = &pernet_list;
23static DEFINE_MUTEX(net_mutex);
24
5f256bec 25LIST_HEAD(net_namespace_list);
b76a461f 26EXPORT_SYMBOL_GPL(net_namespace_list);
5f256bec 27
5f256bec 28struct net init_net;
ff4b9502 29EXPORT_SYMBOL(init_net);
5f256bec 30
dec827d1
PE
31#define INITIAL_NET_GEN_PTRS 13 /* +1 for len +2 for rcu_head */
32
05fceb4a
JP
33static int net_assign_generic(struct net *net, int id, void *data)
34{
35 struct net_generic *ng, *old_ng;
36
37 BUG_ON(!mutex_is_locked(&net_mutex));
38 BUG_ON(id == 0);
39
1c87733d
ED
40 old_ng = rcu_dereference_protected(net->gen,
41 lockdep_is_held(&net_mutex));
42 ng = old_ng;
05fceb4a
JP
43 if (old_ng->len >= id)
44 goto assign;
45
46 ng = kzalloc(sizeof(struct net_generic) +
47 id * sizeof(void *), GFP_KERNEL);
48 if (ng == NULL)
49 return -ENOMEM;
50
51 /*
52 * Some synchronisation notes:
53 *
54 * The net_generic explores the net->gen array inside rcu
55 * read section. Besides once set the net->gen->ptr[x]
56 * pointer never changes (see rules in netns/generic.h).
57 *
58 * That said, we simply duplicate this array and schedule
59 * the old copy for kfree after a grace period.
60 */
61
62 ng->len = id;
63 memcpy(&ng->ptr, &old_ng->ptr, old_ng->len * sizeof(void*));
64
65 rcu_assign_pointer(net->gen, ng);
04d4dfed 66 kfree_rcu(old_ng, rcu);
05fceb4a
JP
67assign:
68 ng->ptr[id - 1] = data;
69 return 0;
70}
71
f875bae0
EB
72static int ops_init(const struct pernet_operations *ops, struct net *net)
73{
74 int err;
75 if (ops->id && ops->size) {
76 void *data = kzalloc(ops->size, GFP_KERNEL);
77 if (!data)
78 return -ENOMEM;
79
80 err = net_assign_generic(net, *ops->id, data);
81 if (err) {
82 kfree(data);
83 return err;
84 }
85 }
86 if (ops->init)
87 return ops->init(net);
88 return 0;
89}
90
91static void ops_free(const struct pernet_operations *ops, struct net *net)
92{
93 if (ops->id && ops->size) {
94 int id = *ops->id;
95 kfree(net_generic(net, id));
96 }
97}
98
72ad937a
EB
99static void ops_exit_list(const struct pernet_operations *ops,
100 struct list_head *net_exit_list)
101{
102 struct net *net;
103 if (ops->exit) {
104 list_for_each_entry(net, net_exit_list, exit_list)
105 ops->exit(net);
106 }
72ad937a
EB
107 if (ops->exit_batch)
108 ops->exit_batch(net_exit_list);
109}
110
111static void ops_free_list(const struct pernet_operations *ops,
112 struct list_head *net_exit_list)
113{
114 struct net *net;
115 if (ops->size && ops->id) {
116 list_for_each_entry(net, net_exit_list, exit_list)
117 ops_free(ops, net);
118 }
119}
120
5f256bec
EB
121/*
122 * setup_net runs the initializers for the network namespace object.
123 */
1a2ee93d 124static __net_init int setup_net(struct net *net)
5f256bec
EB
125{
126 /* Must be called with net_mutex held */
f875bae0 127 const struct pernet_operations *ops, *saved_ops;
486a87f1 128 int error = 0;
72ad937a 129 LIST_HEAD(net_exit_list);
5f256bec 130
5f256bec 131 atomic_set(&net->count, 1);
a685e089 132 atomic_set(&net->passive, 1);
4e985ada 133 net->dev_base_seq = 1;
486a87f1 134
5d1e4468 135#ifdef NETNS_REFCNT_DEBUG
5f256bec 136 atomic_set(&net->use_count, 0);
5d1e4468 137#endif
5f256bec 138
768f3591 139 list_for_each_entry(ops, &pernet_list, list) {
f875bae0
EB
140 error = ops_init(ops, net);
141 if (error < 0)
142 goto out_undo;
5f256bec
EB
143 }
144out:
145 return error;
768f3591 146
5f256bec
EB
147out_undo:
148 /* Walk through the list backwards calling the exit functions
149 * for the pernet modules whose init functions did not fail.
150 */
72ad937a 151 list_add(&net->exit_list, &net_exit_list);
f875bae0 152 saved_ops = ops;
72ad937a
EB
153 list_for_each_entry_continue_reverse(ops, &pernet_list, list)
154 ops_exit_list(ops, &net_exit_list);
155
f875bae0
EB
156 ops = saved_ops;
157 list_for_each_entry_continue_reverse(ops, &pernet_list, list)
72ad937a 158 ops_free_list(ops, &net_exit_list);
310928d9
DL
159
160 rcu_barrier();
5f256bec
EB
161 goto out;
162}
163
486a87f1 164static struct net_generic *net_alloc_generic(void)
6a1a3b9f 165{
486a87f1
DL
166 struct net_generic *ng;
167 size_t generic_size = sizeof(struct net_generic) +
168 INITIAL_NET_GEN_PTRS * sizeof(void *);
169
170 ng = kzalloc(generic_size, GFP_KERNEL);
171 if (ng)
172 ng->len = INITIAL_NET_GEN_PTRS;
173
174 return ng;
6a1a3b9f
PE
175}
176
ebe47d47
CN
177#ifdef CONFIG_NET_NS
178static struct kmem_cache *net_cachep;
179static struct workqueue_struct *netns_wq;
180
486a87f1 181static struct net *net_alloc(void)
45a19b0a 182{
486a87f1
DL
183 struct net *net = NULL;
184 struct net_generic *ng;
185
186 ng = net_alloc_generic();
187 if (!ng)
188 goto out;
189
190 net = kmem_cache_zalloc(net_cachep, GFP_KERNEL);
45a19b0a 191 if (!net)
486a87f1 192 goto out_free;
45a19b0a 193
486a87f1
DL
194 rcu_assign_pointer(net->gen, ng);
195out:
196 return net;
197
198out_free:
199 kfree(ng);
200 goto out;
201}
202
203static void net_free(struct net *net)
204{
5d1e4468 205#ifdef NETNS_REFCNT_DEBUG
45a19b0a
JFS
206 if (unlikely(atomic_read(&net->use_count) != 0)) {
207 printk(KERN_EMERG "network namespace not free! Usage: %d\n",
208 atomic_read(&net->use_count));
209 return;
210 }
5d1e4468 211#endif
4ef079cc 212 kfree(net->gen);
45a19b0a
JFS
213 kmem_cache_free(net_cachep, net);
214}
215
a685e089
AV
216void net_drop_ns(void *p)
217{
218 struct net *ns = p;
219 if (ns && atomic_dec_and_test(&ns->passive))
220 net_free(ns);
221}
222
911cb193 223struct net *copy_net_ns(unsigned long flags, struct net *old_net)
9dd776b6 224{
088eb2d9
AD
225 struct net *net;
226 int rv;
9dd776b6 227
911cb193
RL
228 if (!(flags & CLONE_NEWNET))
229 return get_net(old_net);
230
088eb2d9
AD
231 net = net_alloc();
232 if (!net)
233 return ERR_PTR(-ENOMEM);
9dd776b6 234 mutex_lock(&net_mutex);
088eb2d9
AD
235 rv = setup_net(net);
236 if (rv == 0) {
486a87f1 237 rtnl_lock();
11a28d37 238 list_add_tail_rcu(&net->list, &net_namespace_list);
486a87f1
DL
239 rtnl_unlock();
240 }
9dd776b6 241 mutex_unlock(&net_mutex);
088eb2d9 242 if (rv < 0) {
a685e089 243 net_drop_ns(net);
088eb2d9
AD
244 return ERR_PTR(rv);
245 }
246 return net;
247}
486a87f1 248
2b035b39
EB
249static DEFINE_SPINLOCK(cleanup_list_lock);
250static LIST_HEAD(cleanup_list); /* Must hold cleanup_list_lock to touch */
251
6a1a3b9f
PE
252static void cleanup_net(struct work_struct *work)
253{
f875bae0 254 const struct pernet_operations *ops;
2b035b39
EB
255 struct net *net, *tmp;
256 LIST_HEAD(net_kill_list);
72ad937a 257 LIST_HEAD(net_exit_list);
6a1a3b9f 258
2b035b39
EB
259 /* Atomically snapshot the list of namespaces to cleanup */
260 spin_lock_irq(&cleanup_list_lock);
261 list_replace_init(&cleanup_list, &net_kill_list);
262 spin_unlock_irq(&cleanup_list_lock);
6a1a3b9f
PE
263
264 mutex_lock(&net_mutex);
265
266 /* Don't let anyone else find us. */
267 rtnl_lock();
72ad937a 268 list_for_each_entry(net, &net_kill_list, cleanup_list) {
2b035b39 269 list_del_rcu(&net->list);
72ad937a
EB
270 list_add_tail(&net->exit_list, &net_exit_list);
271 }
6a1a3b9f
PE
272 rtnl_unlock();
273
11a28d37
JB
274 /*
275 * Another CPU might be rcu-iterating the list, wait for it.
276 * This needs to be before calling the exit() notifiers, so
277 * the rcu_barrier() below isn't sufficient alone.
278 */
279 synchronize_rcu();
280
6a1a3b9f 281 /* Run all of the network namespace exit methods */
72ad937a
EB
282 list_for_each_entry_reverse(ops, &pernet_list, list)
283 ops_exit_list(ops, &net_exit_list);
284
f875bae0 285 /* Free the net generic variables */
72ad937a
EB
286 list_for_each_entry_reverse(ops, &pernet_list, list)
287 ops_free_list(ops, &net_exit_list);
6a1a3b9f
PE
288
289 mutex_unlock(&net_mutex);
290
291 /* Ensure there are no outstanding rcu callbacks using this
292 * network namespace.
293 */
294 rcu_barrier();
295
296 /* Finally it is safe to free my network namespace structure */
72ad937a
EB
297 list_for_each_entry_safe(net, tmp, &net_exit_list, exit_list) {
298 list_del_init(&net->exit_list);
a685e089 299 net_drop_ns(net);
2b035b39 300 }
6a1a3b9f 301}
2b035b39 302static DECLARE_WORK(net_cleanup_work, cleanup_net);
6a1a3b9f
PE
303
304void __put_net(struct net *net)
305{
306 /* Cleanup the network namespace in process context */
2b035b39
EB
307 unsigned long flags;
308
309 spin_lock_irqsave(&cleanup_list_lock, flags);
310 list_add(&net->cleanup_list, &cleanup_list);
311 spin_unlock_irqrestore(&cleanup_list_lock, flags);
312
313 queue_work(netns_wq, &net_cleanup_work);
6a1a3b9f
PE
314}
315EXPORT_SYMBOL_GPL(__put_net);
316
956c9207
SR
317struct net *get_net_ns_by_fd(int fd)
318{
319 struct proc_inode *ei;
320 struct file *file;
321 struct net *net;
322
956c9207 323 file = proc_ns_fget(fd);
c316e6a3
AV
324 if (IS_ERR(file))
325 return ERR_CAST(file);
956c9207
SR
326
327 ei = PROC_I(file->f_dentry->d_inode);
c316e6a3
AV
328 if (ei->ns_ops == &netns_operations)
329 net = get_net(ei->ns);
330 else
331 net = ERR_PTR(-EINVAL);
956c9207 332
c316e6a3 333 fput(file);
956c9207
SR
334 return net;
335}
336
6a1a3b9f
PE
337#else
338struct net *copy_net_ns(unsigned long flags, struct net *old_net)
339{
340 if (flags & CLONE_NEWNET)
341 return ERR_PTR(-EINVAL);
342 return old_net;
343}
956c9207
SR
344
345struct net *get_net_ns_by_fd(int fd)
346{
347 return ERR_PTR(-EINVAL);
348}
6a1a3b9f
PE
349#endif
350
30ffee84
JB
351struct net *get_net_ns_by_pid(pid_t pid)
352{
353 struct task_struct *tsk;
354 struct net *net;
355
356 /* Lookup the network namespace */
357 net = ERR_PTR(-ESRCH);
358 rcu_read_lock();
359 tsk = find_task_by_vpid(pid);
360 if (tsk) {
361 struct nsproxy *nsproxy;
362 nsproxy = task_nsproxy(tsk);
363 if (nsproxy)
364 net = get_net(nsproxy->net_ns);
365 }
366 rcu_read_unlock();
367 return net;
368}
369EXPORT_SYMBOL_GPL(get_net_ns_by_pid);
370
5f256bec
EB
371static int __init net_ns_init(void)
372{
486a87f1 373 struct net_generic *ng;
5f256bec 374
d57a9212 375#ifdef CONFIG_NET_NS
5f256bec
EB
376 net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
377 SMP_CACHE_BYTES,
378 SLAB_PANIC, NULL);
3ef1355d
BT
379
380 /* Create workqueue for cleanup */
381 netns_wq = create_singlethread_workqueue("netns");
382 if (!netns_wq)
383 panic("Could not create netns workq");
d57a9212 384#endif
3ef1355d 385
486a87f1
DL
386 ng = net_alloc_generic();
387 if (!ng)
388 panic("Could not allocate generic netns");
389
390 rcu_assign_pointer(init_net.gen, ng);
391
5f256bec 392 mutex_lock(&net_mutex);
ca0f3112
SH
393 if (setup_net(&init_net))
394 panic("Could not setup the initial network namespace");
5f256bec 395
f4618d39 396 rtnl_lock();
11a28d37 397 list_add_tail_rcu(&init_net.list, &net_namespace_list);
f4618d39 398 rtnl_unlock();
5f256bec
EB
399
400 mutex_unlock(&net_mutex);
5f256bec
EB
401
402 return 0;
403}
404
405pure_initcall(net_ns_init);
406
ed160e83 407#ifdef CONFIG_NET_NS
f875bae0
EB
408static int __register_pernet_operations(struct list_head *list,
409 struct pernet_operations *ops)
5f256bec 410{
72ad937a 411 struct net *net;
5f256bec 412 int error;
72ad937a 413 LIST_HEAD(net_exit_list);
5f256bec 414
5f256bec 415 list_add_tail(&ops->list, list);
f875bae0 416 if (ops->init || (ops->id && ops->size)) {
1dba323b 417 for_each_net(net) {
f875bae0 418 error = ops_init(ops, net);
5f256bec
EB
419 if (error)
420 goto out_undo;
72ad937a 421 list_add_tail(&net->exit_list, &net_exit_list);
5f256bec
EB
422 }
423 }
1dba323b 424 return 0;
5f256bec
EB
425
426out_undo:
427 /* If I have an error cleanup all namespaces I initialized */
428 list_del(&ops->list);
72ad937a
EB
429 ops_exit_list(ops, &net_exit_list);
430 ops_free_list(ops, &net_exit_list);
1dba323b 431 return error;
5f256bec
EB
432}
433
f875bae0 434static void __unregister_pernet_operations(struct pernet_operations *ops)
5f256bec
EB
435{
436 struct net *net;
72ad937a 437 LIST_HEAD(net_exit_list);
5f256bec
EB
438
439 list_del(&ops->list);
72ad937a
EB
440 for_each_net(net)
441 list_add_tail(&net->exit_list, &net_exit_list);
442 ops_exit_list(ops, &net_exit_list);
443 ops_free_list(ops, &net_exit_list);
5f256bec
EB
444}
445
ed160e83
DL
446#else
447
f875bae0
EB
448static int __register_pernet_operations(struct list_head *list,
449 struct pernet_operations *ops)
ed160e83 450{
f875bae0
EB
451 int err = 0;
452 err = ops_init(ops, &init_net);
453 if (err)
454 ops_free(ops, &init_net);
455 return err;
456
ed160e83
DL
457}
458
f875bae0 459static void __unregister_pernet_operations(struct pernet_operations *ops)
ed160e83 460{
72ad937a
EB
461 LIST_HEAD(net_exit_list);
462 list_add(&init_net.exit_list, &net_exit_list);
463 ops_exit_list(ops, &net_exit_list);
464 ops_free_list(ops, &net_exit_list);
ed160e83 465}
f875bae0
EB
466
467#endif /* CONFIG_NET_NS */
ed160e83 468
c93cf61f
PE
469static DEFINE_IDA(net_generic_ids);
470
f875bae0
EB
471static int register_pernet_operations(struct list_head *list,
472 struct pernet_operations *ops)
473{
474 int error;
475
476 if (ops->id) {
477again:
478 error = ida_get_new_above(&net_generic_ids, 1, ops->id);
479 if (error < 0) {
480 if (error == -EAGAIN) {
481 ida_pre_get(&net_generic_ids, GFP_KERNEL);
482 goto again;
483 }
484 return error;
485 }
486 }
487 error = __register_pernet_operations(list, ops);
3a765eda
EB
488 if (error) {
489 rcu_barrier();
490 if (ops->id)
491 ida_remove(&net_generic_ids, *ops->id);
492 }
f875bae0
EB
493
494 return error;
495}
496
497static void unregister_pernet_operations(struct pernet_operations *ops)
498{
499
500 __unregister_pernet_operations(ops);
3a765eda 501 rcu_barrier();
f875bae0
EB
502 if (ops->id)
503 ida_remove(&net_generic_ids, *ops->id);
504}
505
5f256bec
EB
506/**
507 * register_pernet_subsys - register a network namespace subsystem
508 * @ops: pernet operations structure for the subsystem
509 *
510 * Register a subsystem which has init and exit functions
511 * that are called when network namespaces are created and
512 * destroyed respectively.
513 *
514 * When registered all network namespace init functions are
515 * called for every existing network namespace. Allowing kernel
516 * modules to have a race free view of the set of network namespaces.
517 *
518 * When a new network namespace is created all of the init
519 * methods are called in the order in which they were registered.
520 *
521 * When a network namespace is destroyed all of the exit methods
522 * are called in the reverse of the order with which they were
523 * registered.
524 */
525int register_pernet_subsys(struct pernet_operations *ops)
526{
527 int error;
528 mutex_lock(&net_mutex);
529 error = register_pernet_operations(first_device, ops);
530 mutex_unlock(&net_mutex);
531 return error;
532}
533EXPORT_SYMBOL_GPL(register_pernet_subsys);
534
535/**
536 * unregister_pernet_subsys - unregister a network namespace subsystem
537 * @ops: pernet operations structure to manipulate
538 *
539 * Remove the pernet operations structure from the list to be
53379e57 540 * used when network namespaces are created or destroyed. In
5f256bec
EB
541 * addition run the exit method for all existing network
542 * namespaces.
543 */
b3c981d2 544void unregister_pernet_subsys(struct pernet_operations *ops)
5f256bec
EB
545{
546 mutex_lock(&net_mutex);
b3c981d2 547 unregister_pernet_operations(ops);
5f256bec
EB
548 mutex_unlock(&net_mutex);
549}
550EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
551
552/**
553 * register_pernet_device - register a network namespace device
554 * @ops: pernet operations structure for the subsystem
555 *
556 * Register a device which has init and exit functions
557 * that are called when network namespaces are created and
558 * destroyed respectively.
559 *
560 * When registered all network namespace init functions are
561 * called for every existing network namespace. Allowing kernel
562 * modules to have a race free view of the set of network namespaces.
563 *
564 * When a new network namespace is created all of the init
565 * methods are called in the order in which they were registered.
566 *
567 * When a network namespace is destroyed all of the exit methods
568 * are called in the reverse of the order with which they were
569 * registered.
570 */
571int register_pernet_device(struct pernet_operations *ops)
572{
573 int error;
574 mutex_lock(&net_mutex);
575 error = register_pernet_operations(&pernet_list, ops);
576 if (!error && (first_device == &pernet_list))
577 first_device = &ops->list;
578 mutex_unlock(&net_mutex);
579 return error;
580}
581EXPORT_SYMBOL_GPL(register_pernet_device);
582
583/**
584 * unregister_pernet_device - unregister a network namespace netdevice
585 * @ops: pernet operations structure to manipulate
586 *
587 * Remove the pernet operations structure from the list to be
53379e57 588 * used when network namespaces are created or destroyed. In
5f256bec
EB
589 * addition run the exit method for all existing network
590 * namespaces.
591 */
592void unregister_pernet_device(struct pernet_operations *ops)
593{
594 mutex_lock(&net_mutex);
595 if (&ops->list == first_device)
596 first_device = first_device->next;
597 unregister_pernet_operations(ops);
598 mutex_unlock(&net_mutex);
599}
600EXPORT_SYMBOL_GPL(unregister_pernet_device);
13b6f576
EB
601
602#ifdef CONFIG_NET_NS
603static void *netns_get(struct task_struct *task)
604{
f0630529
EB
605 struct net *net = NULL;
606 struct nsproxy *nsproxy;
607
13b6f576 608 rcu_read_lock();
f0630529
EB
609 nsproxy = task_nsproxy(task);
610 if (nsproxy)
611 net = get_net(nsproxy->net_ns);
13b6f576 612 rcu_read_unlock();
f0630529 613
13b6f576
EB
614 return net;
615}
616
617static void netns_put(void *ns)
618{
619 put_net(ns);
620}
621
622static int netns_install(struct nsproxy *nsproxy, void *ns)
623{
624 put_net(nsproxy->net_ns);
625 nsproxy->net_ns = get_net(ns);
626 return 0;
627}
628
629const struct proc_ns_operations netns_operations = {
630 .name = "net",
631 .type = CLONE_NEWNET,
632 .get = netns_get,
633 .put = netns_put,
634 .install = netns_install,
635};
636#endif