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