blob: 6b3edc9e6f196e9ef5922d26484f60531aad7a7f [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>
Eric W. Biederman5f256be2007-09-12 11:50:50 +02009#include <net/net_namespace.h>
Pavel Emelyanovdec827d2008-04-15 00:36:08 -070010#include <net/netns/generic.h>
Eric W. Biederman5f256be2007-09-12 11:50:50 +020011
12/*
13 * Our network namespace constructor/destructor lists
14 */
15
16static LIST_HEAD(pernet_list);
17static struct list_head *first_device = &pernet_list;
18static DEFINE_MUTEX(net_mutex);
19
Eric W. Biederman5f256be2007-09-12 11:50:50 +020020LIST_HEAD(net_namespace_list);
Alexey Dobriyanb76a4612008-10-08 11:35:06 +020021EXPORT_SYMBOL_GPL(net_namespace_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +020022
Eric W. Biederman5f256be2007-09-12 11:50:50 +020023struct net init_net;
Denis V. Lunevff4b9502008-01-22 22:05:33 -080024EXPORT_SYMBOL(init_net);
Eric W. Biederman5f256be2007-09-12 11:50:50 +020025
Pavel Emelyanovdec827d2008-04-15 00:36:08 -070026#define INITIAL_NET_GEN_PTRS 13 /* +1 for len +2 for rcu_head */
27
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -070028/*
29 * setup_net runs the initializers for the network namespace object.
30 */
Pavel Emelyanov1a2ee932007-11-01 00:45:59 -070031static __net_init int setup_net(struct net *net)
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -070032{
33 /* Must be called with net_mutex held */
34 struct pernet_operations *ops;
Daniel Lezcano486a87f2009-02-22 00:07:53 -080035 int error = 0;
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -070036
37 atomic_set(&net->count, 1);
Daniel Lezcano486a87f2009-02-22 00:07:53 -080038
Denis V. Lunev5d1e4462008-04-16 01:58:04 -070039#ifdef NETNS_REFCNT_DEBUG
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -070040 atomic_set(&net->use_count, 0);
Denis V. Lunev5d1e4462008-04-16 01:58:04 -070041#endif
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -070042
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -070043 list_for_each_entry(ops, &pernet_list, list) {
44 if (ops->init) {
45 error = ops->init(net);
46 if (error < 0)
47 goto out_undo;
48 }
49 }
50out:
51 return error;
52
53out_undo:
54 /* Walk through the list backwards calling the exit functions
55 * for the pernet modules whose init functions did not fail.
56 */
57 list_for_each_entry_continue_reverse(ops, &pernet_list, list) {
58 if (ops->exit)
59 ops->exit(net);
60 }
61
62 rcu_barrier();
63 goto out;
64}
65
Daniel Lezcano486a87f2009-02-22 00:07:53 -080066static struct net_generic *net_alloc_generic(void)
67{
68 struct net_generic *ng;
69 size_t generic_size = sizeof(struct net_generic) +
70 INITIAL_NET_GEN_PTRS * sizeof(void *);
71
72 ng = kzalloc(generic_size, GFP_KERNEL);
73 if (ng)
74 ng->len = INITIAL_NET_GEN_PTRS;
75
76 return ng;
77}
78
Clemens Nossebe47d42009-02-23 15:37:35 -080079#ifdef CONFIG_NET_NS
80static struct kmem_cache *net_cachep;
81static struct workqueue_struct *netns_wq;
82
Eric W. Biederman5f256be2007-09-12 11:50:50 +020083static struct net *net_alloc(void)
84{
Daniel Lezcano486a87f2009-02-22 00:07:53 -080085 struct net *net = NULL;
86 struct net_generic *ng;
87
88 ng = net_alloc_generic();
89 if (!ng)
90 goto out;
91
92 net = kmem_cache_zalloc(net_cachep, GFP_KERNEL);
93 if (!net)
94 goto out_free;
95
96 rcu_assign_pointer(net->gen, ng);
97out:
98 return net;
99
100out_free:
101 kfree(ng);
102 goto out;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200103}
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200104
Johann Felix Soden45a19b02007-11-07 01:30:30 -0800105static void net_free(struct net *net)
106{
Denis V. Lunev5d1e4462008-04-16 01:58:04 -0700107#ifdef NETNS_REFCNT_DEBUG
Johann Felix Soden45a19b02007-11-07 01:30:30 -0800108 if (unlikely(atomic_read(&net->use_count) != 0)) {
109 printk(KERN_EMERG "network namespace not free! Usage: %d\n",
110 atomic_read(&net->use_count));
111 return;
112 }
Denis V. Lunev5d1e4462008-04-16 01:58:04 -0700113#endif
Alexey Dobriyan4ef079c2008-10-14 22:54:48 -0700114 kfree(net->gen);
Johann Felix Soden45a19b02007-11-07 01:30:30 -0800115 kmem_cache_free(net_cachep, net);
116}
117
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700118static struct net *net_create(void)
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700119{
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700120 struct net *net;
121 int rv;
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700122
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700123 net = net_alloc();
124 if (!net)
125 return ERR_PTR(-ENOMEM);
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700126 mutex_lock(&net_mutex);
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700127 rv = setup_net(net);
128 if (rv == 0) {
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800129 rtnl_lock();
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700130 list_add_tail(&net->list, &net_namespace_list);
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800131 rtnl_unlock();
132 }
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700133 mutex_unlock(&net_mutex);
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700134 if (rv < 0) {
135 net_free(net);
136 return ERR_PTR(rv);
137 }
138 return net;
139}
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800140
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700141struct net *copy_net_ns(unsigned long flags, struct net *old_net)
142{
143 if (!(flags & CLONE_NEWNET))
144 return get_net(old_net);
145 return net_create();
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700146}
147
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200148static void cleanup_net(struct work_struct *work)
149{
150 struct pernet_operations *ops;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200151 struct net *net;
152
153 net = container_of(work, struct net, work);
154
155 mutex_lock(&net_mutex);
156
157 /* Don't let anyone else find us. */
Eric W. Biedermanf4618d32007-09-26 22:40:08 -0700158 rtnl_lock();
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200159 list_del(&net->list);
Eric W. Biedermanf4618d32007-09-26 22:40:08 -0700160 rtnl_unlock();
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200161
162 /* Run all of the network namespace exit methods */
Pavel Emelyanov768f35912007-09-18 13:20:41 -0700163 list_for_each_entry_reverse(ops, &pernet_list, list) {
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200164 if (ops->exit)
165 ops->exit(net);
166 }
167
168 mutex_unlock(&net_mutex);
169
170 /* Ensure there are no outstanding rcu callbacks using this
171 * network namespace.
172 */
173 rcu_barrier();
174
175 /* Finally it is safe to free my network namespace structure */
176 net_free(net);
177}
178
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200179void __put_net(struct net *net)
180{
181 /* Cleanup the network namespace in process context */
182 INIT_WORK(&net->work, cleanup_net);
Benjamin Thery3ef13552007-11-19 23:18:16 -0800183 queue_work(netns_wq, &net->work);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200184}
185EXPORT_SYMBOL_GPL(__put_net);
186
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700187#else
Eric W. Biederman9dd776b2007-09-26 22:04:26 -0700188struct net *copy_net_ns(unsigned long flags, struct net *old_net)
189{
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700190 if (flags & CLONE_NEWNET)
191 return ERR_PTR(-EINVAL);
192 return old_net;
Eric W. Biederman9dd776b2007-09-26 22:04:26 -0700193}
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700194#endif
Eric W. Biederman9dd776b2007-09-26 22:04:26 -0700195
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200196static int __init net_ns_init(void)
197{
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800198 struct net_generic *ng;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200199 int err;
200
201 printk(KERN_INFO "net_namespace: %zd bytes\n", sizeof(struct net));
Pavel Emelyanovd57a9212007-11-01 00:46:50 -0700202#ifdef CONFIG_NET_NS
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200203 net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
204 SMP_CACHE_BYTES,
205 SLAB_PANIC, NULL);
Benjamin Thery3ef13552007-11-19 23:18:16 -0800206
207 /* Create workqueue for cleanup */
208 netns_wq = create_singlethread_workqueue("netns");
209 if (!netns_wq)
210 panic("Could not create netns workq");
Pavel Emelyanovd57a9212007-11-01 00:46:50 -0700211#endif
Benjamin Thery3ef13552007-11-19 23:18:16 -0800212
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800213 ng = net_alloc_generic();
214 if (!ng)
215 panic("Could not allocate generic netns");
216
217 rcu_assign_pointer(init_net.gen, ng);
218
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200219 mutex_lock(&net_mutex);
220 err = setup_net(&init_net);
221
Eric W. Biedermanf4618d32007-09-26 22:40:08 -0700222 rtnl_lock();
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200223 list_add_tail(&init_net.list, &net_namespace_list);
Eric W. Biedermanf4618d32007-09-26 22:40:08 -0700224 rtnl_unlock();
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200225
226 mutex_unlock(&net_mutex);
227 if (err)
228 panic("Could not setup the initial network namespace");
229
230 return 0;
231}
232
233pure_initcall(net_ns_init);
234
Denis V. Luneved160e82007-11-13 03:23:21 -0800235#ifdef CONFIG_NET_NS
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200236static int register_pernet_operations(struct list_head *list,
237 struct pernet_operations *ops)
238{
239 struct net *net, *undo_net;
240 int error;
241
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200242 list_add_tail(&ops->list, list);
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700243 if (ops->init) {
244 for_each_net(net) {
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200245 error = ops->init(net);
246 if (error)
247 goto out_undo;
248 }
249 }
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700250 return 0;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200251
252out_undo:
253 /* If I have an error cleanup all namespaces I initialized */
254 list_del(&ops->list);
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700255 if (ops->exit) {
256 for_each_net(undo_net) {
257 if (undo_net == net)
258 goto undone;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200259 ops->exit(undo_net);
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700260 }
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200261 }
262undone:
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700263 return error;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200264}
265
266static void unregister_pernet_operations(struct pernet_operations *ops)
267{
268 struct net *net;
269
270 list_del(&ops->list);
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700271 if (ops->exit)
272 for_each_net(net)
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200273 ops->exit(net);
274}
275
Denis V. Luneved160e82007-11-13 03:23:21 -0800276#else
277
278static int register_pernet_operations(struct list_head *list,
279 struct pernet_operations *ops)
280{
281 if (ops->init == NULL)
282 return 0;
283 return ops->init(&init_net);
284}
285
286static void unregister_pernet_operations(struct pernet_operations *ops)
287{
288 if (ops->exit)
289 ops->exit(&init_net);
290}
291#endif
292
Pavel Emelyanovc93cf612008-04-15 00:35:23 -0700293static DEFINE_IDA(net_generic_ids);
294
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200295/**
296 * register_pernet_subsys - register a network namespace subsystem
297 * @ops: pernet operations structure for the subsystem
298 *
299 * Register a subsystem which has init and exit functions
300 * that are called when network namespaces are created and
301 * destroyed respectively.
302 *
303 * When registered all network namespace init functions are
304 * called for every existing network namespace. Allowing kernel
305 * modules to have a race free view of the set of network namespaces.
306 *
307 * When a new network namespace is created all of the init
308 * methods are called in the order in which they were registered.
309 *
310 * When a network namespace is destroyed all of the exit methods
311 * are called in the reverse of the order with which they were
312 * registered.
313 */
314int register_pernet_subsys(struct pernet_operations *ops)
315{
316 int error;
317 mutex_lock(&net_mutex);
318 error = register_pernet_operations(first_device, ops);
319 mutex_unlock(&net_mutex);
320 return error;
321}
322EXPORT_SYMBOL_GPL(register_pernet_subsys);
323
324/**
325 * unregister_pernet_subsys - unregister a network namespace subsystem
326 * @ops: pernet operations structure to manipulate
327 *
328 * Remove the pernet operations structure from the list to be
Oliver Pinter53379e52008-02-03 17:56:48 +0200329 * used when network namespaces are created or destroyed. In
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200330 * addition run the exit method for all existing network
331 * namespaces.
332 */
333void unregister_pernet_subsys(struct pernet_operations *module)
334{
335 mutex_lock(&net_mutex);
336 unregister_pernet_operations(module);
337 mutex_unlock(&net_mutex);
338}
339EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
340
Alexey Dobriyan485ac572008-10-30 23:55:16 -0700341int register_pernet_gen_subsys(int *id, struct pernet_operations *ops)
342{
343 int rv;
344
345 mutex_lock(&net_mutex);
346again:
347 rv = ida_get_new_above(&net_generic_ids, 1, id);
348 if (rv < 0) {
349 if (rv == -EAGAIN) {
350 ida_pre_get(&net_generic_ids, GFP_KERNEL);
351 goto again;
352 }
353 goto out;
354 }
355 rv = register_pernet_operations(first_device, ops);
356 if (rv < 0)
357 ida_remove(&net_generic_ids, *id);
Alexey Dobriyan485ac572008-10-30 23:55:16 -0700358out:
Jiri Slaby357f5b02009-01-17 06:47:12 +0000359 mutex_unlock(&net_mutex);
Alexey Dobriyan485ac572008-10-30 23:55:16 -0700360 return rv;
361}
362EXPORT_SYMBOL_GPL(register_pernet_gen_subsys);
363
364void unregister_pernet_gen_subsys(int id, struct pernet_operations *ops)
365{
366 mutex_lock(&net_mutex);
367 unregister_pernet_operations(ops);
368 ida_remove(&net_generic_ids, id);
369 mutex_unlock(&net_mutex);
370}
371EXPORT_SYMBOL_GPL(unregister_pernet_gen_subsys);
372
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200373/**
374 * register_pernet_device - register a network namespace device
375 * @ops: pernet operations structure for the subsystem
376 *
377 * Register a device which has init and exit functions
378 * that are called when network namespaces are created and
379 * destroyed respectively.
380 *
381 * When registered all network namespace init functions are
382 * called for every existing network namespace. Allowing kernel
383 * modules to have a race free view of the set of network namespaces.
384 *
385 * When a new network namespace is created all of the init
386 * methods are called in the order in which they were registered.
387 *
388 * When a network namespace is destroyed all of the exit methods
389 * are called in the reverse of the order with which they were
390 * registered.
391 */
392int register_pernet_device(struct pernet_operations *ops)
393{
394 int error;
395 mutex_lock(&net_mutex);
396 error = register_pernet_operations(&pernet_list, ops);
397 if (!error && (first_device == &pernet_list))
398 first_device = &ops->list;
399 mutex_unlock(&net_mutex);
400 return error;
401}
402EXPORT_SYMBOL_GPL(register_pernet_device);
403
Pavel Emelyanovc93cf612008-04-15 00:35:23 -0700404int register_pernet_gen_device(int *id, struct pernet_operations *ops)
405{
406 int error;
407 mutex_lock(&net_mutex);
408again:
409 error = ida_get_new_above(&net_generic_ids, 1, id);
410 if (error) {
411 if (error == -EAGAIN) {
412 ida_pre_get(&net_generic_ids, GFP_KERNEL);
413 goto again;
414 }
415 goto out;
416 }
417 error = register_pernet_operations(&pernet_list, ops);
418 if (error)
419 ida_remove(&net_generic_ids, *id);
420 else if (first_device == &pernet_list)
421 first_device = &ops->list;
422out:
423 mutex_unlock(&net_mutex);
424 return error;
425}
426EXPORT_SYMBOL_GPL(register_pernet_gen_device);
427
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200428/**
429 * unregister_pernet_device - unregister a network namespace netdevice
430 * @ops: pernet operations structure to manipulate
431 *
432 * Remove the pernet operations structure from the list to be
Oliver Pinter53379e52008-02-03 17:56:48 +0200433 * used when network namespaces are created or destroyed. In
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200434 * addition run the exit method for all existing network
435 * namespaces.
436 */
437void unregister_pernet_device(struct pernet_operations *ops)
438{
439 mutex_lock(&net_mutex);
440 if (&ops->list == first_device)
441 first_device = first_device->next;
442 unregister_pernet_operations(ops);
443 mutex_unlock(&net_mutex);
444}
445EXPORT_SYMBOL_GPL(unregister_pernet_device);
Pavel Emelyanovc93cf612008-04-15 00:35:23 -0700446
447void unregister_pernet_gen_device(int id, struct pernet_operations *ops)
448{
449 mutex_lock(&net_mutex);
450 if (&ops->list == first_device)
451 first_device = first_device->next;
452 unregister_pernet_operations(ops);
453 ida_remove(&net_generic_ids, id);
454 mutex_unlock(&net_mutex);
455}
456EXPORT_SYMBOL_GPL(unregister_pernet_gen_device);
Pavel Emelyanovdec827d2008-04-15 00:36:08 -0700457
458static void net_generic_release(struct rcu_head *rcu)
459{
460 struct net_generic *ng;
461
462 ng = container_of(rcu, struct net_generic, rcu);
463 kfree(ng);
464}
465
466int net_assign_generic(struct net *net, int id, void *data)
467{
468 struct net_generic *ng, *old_ng;
469
470 BUG_ON(!mutex_is_locked(&net_mutex));
471 BUG_ON(id == 0);
472
473 ng = old_ng = net->gen;
474 if (old_ng->len >= id)
475 goto assign;
476
477 ng = kzalloc(sizeof(struct net_generic) +
478 id * sizeof(void *), GFP_KERNEL);
479 if (ng == NULL)
480 return -ENOMEM;
481
482 /*
483 * Some synchronisation notes:
484 *
485 * The net_generic explores the net->gen array inside rcu
486 * read section. Besides once set the net->gen->ptr[x]
487 * pointer never changes (see rules in netns/generic.h).
488 *
489 * That said, we simply duplicate this array and schedule
490 * the old copy for kfree after a grace period.
491 */
492
493 ng->len = id;
Pavel Emelyanovdec827d2008-04-15 00:36:08 -0700494 memcpy(&ng->ptr, &old_ng->ptr, old_ng->len);
495
496 rcu_assign_pointer(net->gen, ng);
497 call_rcu(&old_ng->rcu, net_generic_release);
498assign:
499 ng->ptr[id - 1] = data;
500 return 0;
501}
502EXPORT_SYMBOL_GPL(net_assign_generic);