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>
7 #include <linux/sched.h>
9 #include <linux/rculist.h>
10 #include <linux/nsproxy.h>
11 #include <net/net_namespace.h>
12 #include <net/netns/generic.h>
15 * Our network namespace constructor/destructor lists
18 static LIST_HEAD(pernet_list
);
19 static struct list_head
*first_device
= &pernet_list
;
20 static DEFINE_MUTEX(net_mutex
);
22 LIST_HEAD(net_namespace_list
);
23 EXPORT_SYMBOL_GPL(net_namespace_list
);
26 EXPORT_SYMBOL(init_net
);
28 #define INITIAL_NET_GEN_PTRS 13 /* +1 for len +2 for rcu_head */
30 static int ops_init(const struct pernet_operations
*ops
, struct net
*net
)
33 if (ops
->id
&& ops
->size
) {
34 void *data
= kzalloc(ops
->size
, GFP_KERNEL
);
38 err
= net_assign_generic(net
, *ops
->id
, data
);
45 return ops
->init(net
);
49 static void ops_free(const struct pernet_operations
*ops
, struct net
*net
)
51 if (ops
->id
&& ops
->size
) {
53 kfree(net_generic(net
, id
));
57 static void ops_exit_list(const struct pernet_operations
*ops
,
58 struct list_head
*net_exit_list
)
62 list_for_each_entry(net
, net_exit_list
, exit_list
)
66 ops
->exit_batch(net_exit_list
);
69 static void ops_free_list(const struct pernet_operations
*ops
,
70 struct list_head
*net_exit_list
)
73 if (ops
->size
&& ops
->id
) {
74 list_for_each_entry(net
, net_exit_list
, exit_list
)
80 * setup_net runs the initializers for the network namespace object.
82 static __net_init
int setup_net(struct net
*net
)
84 /* Must be called with net_mutex held */
85 const struct pernet_operations
*ops
, *saved_ops
;
87 LIST_HEAD(net_exit_list
);
89 atomic_set(&net
->count
, 1);
91 #ifdef NETNS_REFCNT_DEBUG
92 atomic_set(&net
->use_count
, 0);
95 list_for_each_entry(ops
, &pernet_list
, list
) {
96 error
= ops_init(ops
, net
);
104 /* Walk through the list backwards calling the exit functions
105 * for the pernet modules whose init functions did not fail.
107 list_add(&net
->exit_list
, &net_exit_list
);
109 list_for_each_entry_continue_reverse(ops
, &pernet_list
, list
)
110 ops_exit_list(ops
, &net_exit_list
);
113 list_for_each_entry_continue_reverse(ops
, &pernet_list
, list
)
114 ops_free_list(ops
, &net_exit_list
);
120 static struct net_generic
*net_alloc_generic(void)
122 struct net_generic
*ng
;
123 size_t generic_size
= sizeof(struct net_generic
) +
124 INITIAL_NET_GEN_PTRS
* sizeof(void *);
126 ng
= kzalloc(generic_size
, GFP_KERNEL
);
128 ng
->len
= INITIAL_NET_GEN_PTRS
;
134 static struct kmem_cache
*net_cachep
;
135 static struct workqueue_struct
*netns_wq
;
137 static struct net
*net_alloc(void)
139 struct net
*net
= NULL
;
140 struct net_generic
*ng
;
142 ng
= net_alloc_generic();
146 net
= kmem_cache_zalloc(net_cachep
, GFP_KERNEL
);
150 rcu_assign_pointer(net
->gen
, ng
);
159 static void net_free(struct net
*net
)
161 #ifdef NETNS_REFCNT_DEBUG
162 if (unlikely(atomic_read(&net
->use_count
) != 0)) {
163 printk(KERN_EMERG
"network namespace not free! Usage: %d\n",
164 atomic_read(&net
->use_count
));
169 kmem_cache_free(net_cachep
, net
);
172 static struct net
*net_create(void)
179 return ERR_PTR(-ENOMEM
);
180 mutex_lock(&net_mutex
);
184 list_add_tail_rcu(&net
->list
, &net_namespace_list
);
187 mutex_unlock(&net_mutex
);
195 struct net
*copy_net_ns(unsigned long flags
, struct net
*old_net
)
197 if (!(flags
& CLONE_NEWNET
))
198 return get_net(old_net
);
202 static DEFINE_SPINLOCK(cleanup_list_lock
);
203 static LIST_HEAD(cleanup_list
); /* Must hold cleanup_list_lock to touch */
205 static void cleanup_net(struct work_struct
*work
)
207 const struct pernet_operations
*ops
;
208 struct net
*net
, *tmp
;
209 LIST_HEAD(net_kill_list
);
210 LIST_HEAD(net_exit_list
);
212 /* Atomically snapshot the list of namespaces to cleanup */
213 spin_lock_irq(&cleanup_list_lock
);
214 list_replace_init(&cleanup_list
, &net_kill_list
);
215 spin_unlock_irq(&cleanup_list_lock
);
217 mutex_lock(&net_mutex
);
219 /* Don't let anyone else find us. */
221 list_for_each_entry(net
, &net_kill_list
, cleanup_list
) {
222 list_del_rcu(&net
->list
);
223 list_add_tail(&net
->exit_list
, &net_exit_list
);
228 * Another CPU might be rcu-iterating the list, wait for it.
229 * This needs to be before calling the exit() notifiers, so
230 * the rcu_barrier() below isn't sufficient alone.
234 /* Run all of the network namespace exit methods */
235 list_for_each_entry_reverse(ops
, &pernet_list
, list
)
236 ops_exit_list(ops
, &net_exit_list
);
238 /* Free the net generic variables */
239 list_for_each_entry_reverse(ops
, &pernet_list
, list
)
240 ops_free_list(ops
, &net_exit_list
);
242 mutex_unlock(&net_mutex
);
244 /* Ensure there are no outstanding rcu callbacks using this
249 /* Finally it is safe to free my network namespace structure */
250 list_for_each_entry_safe(net
, tmp
, &net_exit_list
, exit_list
) {
251 list_del_init(&net
->exit_list
);
255 static DECLARE_WORK(net_cleanup_work
, cleanup_net
);
257 void __put_net(struct net
*net
)
259 /* Cleanup the network namespace in process context */
262 spin_lock_irqsave(&cleanup_list_lock
, flags
);
263 list_add(&net
->cleanup_list
, &cleanup_list
);
264 spin_unlock_irqrestore(&cleanup_list_lock
, flags
);
266 queue_work(netns_wq
, &net_cleanup_work
);
268 EXPORT_SYMBOL_GPL(__put_net
);
271 struct net
*copy_net_ns(unsigned long flags
, struct net
*old_net
)
273 if (flags
& CLONE_NEWNET
)
274 return ERR_PTR(-EINVAL
);
279 struct net
*get_net_ns_by_pid(pid_t pid
)
281 struct task_struct
*tsk
;
284 /* Lookup the network namespace */
285 net
= ERR_PTR(-ESRCH
);
287 tsk
= find_task_by_vpid(pid
);
289 struct nsproxy
*nsproxy
;
290 nsproxy
= task_nsproxy(tsk
);
292 net
= get_net(nsproxy
->net_ns
);
297 EXPORT_SYMBOL_GPL(get_net_ns_by_pid
);
299 static int __init
net_ns_init(void)
301 struct net_generic
*ng
;
304 net_cachep
= kmem_cache_create("net_namespace", sizeof(struct net
),
308 /* Create workqueue for cleanup */
309 netns_wq
= create_singlethread_workqueue("netns");
311 panic("Could not create netns workq");
314 ng
= net_alloc_generic();
316 panic("Could not allocate generic netns");
318 rcu_assign_pointer(init_net
.gen
, ng
);
320 mutex_lock(&net_mutex
);
321 if (setup_net(&init_net
))
322 panic("Could not setup the initial network namespace");
325 list_add_tail_rcu(&init_net
.list
, &net_namespace_list
);
328 mutex_unlock(&net_mutex
);
333 pure_initcall(net_ns_init
);
336 static int __register_pernet_operations(struct list_head
*list
,
337 struct pernet_operations
*ops
)
341 LIST_HEAD(net_exit_list
);
343 list_add_tail(&ops
->list
, list
);
344 if (ops
->init
|| (ops
->id
&& ops
->size
)) {
346 error
= ops_init(ops
, net
);
349 list_add_tail(&net
->exit_list
, &net_exit_list
);
355 /* If I have an error cleanup all namespaces I initialized */
356 list_del(&ops
->list
);
357 ops_exit_list(ops
, &net_exit_list
);
358 ops_free_list(ops
, &net_exit_list
);
362 static void __unregister_pernet_operations(struct pernet_operations
*ops
)
365 LIST_HEAD(net_exit_list
);
367 list_del(&ops
->list
);
369 list_add_tail(&net
->exit_list
, &net_exit_list
);
370 ops_exit_list(ops
, &net_exit_list
);
371 ops_free_list(ops
, &net_exit_list
);
376 static int __register_pernet_operations(struct list_head
*list
,
377 struct pernet_operations
*ops
)
380 err
= ops_init(ops
, &init_net
);
382 ops_free(ops
, &init_net
);
387 static void __unregister_pernet_operations(struct pernet_operations
*ops
)
389 LIST_HEAD(net_exit_list
);
390 list_add(&init_net
.exit_list
, &net_exit_list
);
391 ops_exit_list(ops
, &net_exit_list
);
392 ops_free_list(ops
, &net_exit_list
);
395 #endif /* CONFIG_NET_NS */
397 static DEFINE_IDA(net_generic_ids
);
399 static int register_pernet_operations(struct list_head
*list
,
400 struct pernet_operations
*ops
)
406 error
= ida_get_new_above(&net_generic_ids
, 1, ops
->id
);
408 if (error
== -EAGAIN
) {
409 ida_pre_get(&net_generic_ids
, GFP_KERNEL
);
415 error
= __register_pernet_operations(list
, ops
);
419 ida_remove(&net_generic_ids
, *ops
->id
);
425 static void unregister_pernet_operations(struct pernet_operations
*ops
)
428 __unregister_pernet_operations(ops
);
431 ida_remove(&net_generic_ids
, *ops
->id
);
435 * register_pernet_subsys - register a network namespace subsystem
436 * @ops: pernet operations structure for the subsystem
438 * Register a subsystem which has init and exit functions
439 * that are called when network namespaces are created and
440 * destroyed respectively.
442 * When registered all network namespace init functions are
443 * called for every existing network namespace. Allowing kernel
444 * modules to have a race free view of the set of network namespaces.
446 * When a new network namespace is created all of the init
447 * methods are called in the order in which they were registered.
449 * When a network namespace is destroyed all of the exit methods
450 * are called in the reverse of the order with which they were
453 int register_pernet_subsys(struct pernet_operations
*ops
)
456 mutex_lock(&net_mutex
);
457 error
= register_pernet_operations(first_device
, ops
);
458 mutex_unlock(&net_mutex
);
461 EXPORT_SYMBOL_GPL(register_pernet_subsys
);
464 * unregister_pernet_subsys - unregister a network namespace subsystem
465 * @ops: pernet operations structure to manipulate
467 * Remove the pernet operations structure from the list to be
468 * used when network namespaces are created or destroyed. In
469 * addition run the exit method for all existing network
472 void unregister_pernet_subsys(struct pernet_operations
*module
)
474 mutex_lock(&net_mutex
);
475 unregister_pernet_operations(module
);
476 mutex_unlock(&net_mutex
);
478 EXPORT_SYMBOL_GPL(unregister_pernet_subsys
);
481 * register_pernet_device - register a network namespace device
482 * @ops: pernet operations structure for the subsystem
484 * Register a device which has init and exit functions
485 * that are called when network namespaces are created and
486 * destroyed respectively.
488 * When registered all network namespace init functions are
489 * called for every existing network namespace. Allowing kernel
490 * modules to have a race free view of the set of network namespaces.
492 * When a new network namespace is created all of the init
493 * methods are called in the order in which they were registered.
495 * When a network namespace is destroyed all of the exit methods
496 * are called in the reverse of the order with which they were
499 int register_pernet_device(struct pernet_operations
*ops
)
502 mutex_lock(&net_mutex
);
503 error
= register_pernet_operations(&pernet_list
, ops
);
504 if (!error
&& (first_device
== &pernet_list
))
505 first_device
= &ops
->list
;
506 mutex_unlock(&net_mutex
);
509 EXPORT_SYMBOL_GPL(register_pernet_device
);
512 * unregister_pernet_device - unregister a network namespace netdevice
513 * @ops: pernet operations structure to manipulate
515 * Remove the pernet operations structure from the list to be
516 * used when network namespaces are created or destroyed. In
517 * addition run the exit method for all existing network
520 void unregister_pernet_device(struct pernet_operations
*ops
)
522 mutex_lock(&net_mutex
);
523 if (&ops
->list
== first_device
)
524 first_device
= first_device
->next
;
525 unregister_pernet_operations(ops
);
526 mutex_unlock(&net_mutex
);
528 EXPORT_SYMBOL_GPL(unregister_pernet_device
);
530 static void net_generic_release(struct rcu_head
*rcu
)
532 struct net_generic
*ng
;
534 ng
= container_of(rcu
, struct net_generic
, rcu
);
538 int net_assign_generic(struct net
*net
, int id
, void *data
)
540 struct net_generic
*ng
, *old_ng
;
542 BUG_ON(!mutex_is_locked(&net_mutex
));
545 ng
= old_ng
= net
->gen
;
546 if (old_ng
->len
>= id
)
549 ng
= kzalloc(sizeof(struct net_generic
) +
550 id
* sizeof(void *), GFP_KERNEL
);
555 * Some synchronisation notes:
557 * The net_generic explores the net->gen array inside rcu
558 * read section. Besides once set the net->gen->ptr[x]
559 * pointer never changes (see rules in netns/generic.h).
561 * That said, we simply duplicate this array and schedule
562 * the old copy for kfree after a grace period.
566 memcpy(&ng
->ptr
, &old_ng
->ptr
, old_ng
->len
* sizeof(void*));
568 rcu_assign_pointer(net
->gen
, ng
);
569 call_rcu(&old_ng
->rcu
, net_generic_release
);
571 ng
->ptr
[id
- 1] = data
;
574 EXPORT_SYMBOL_GPL(net_assign_generic
);