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 */
31 * setup_net runs the initializers for the network namespace object.
33 static __net_init int setup_net(struct net *net)
35 /* Must be called with net_mutex held */
36 struct pernet_operations *ops;
39 atomic_set(&net->count, 1);
41 #ifdef NETNS_REFCNT_DEBUG
42 atomic_set(&net->use_count, 0);
45 list_for_each_entry(ops, &pernet_list, list) {
47 error = ops->init(net);
56 /* Walk through the list backwards calling the exit functions
57 * for the pernet modules whose init functions did not fail.
59 list_for_each_entry_continue_reverse(ops, &pernet_list, list) {
68 static struct net_generic *net_alloc_generic(void)
70 struct net_generic *ng;
71 size_t generic_size = sizeof(struct net_generic) +
72 INITIAL_NET_GEN_PTRS * sizeof(void *);
74 ng = kzalloc(generic_size, GFP_KERNEL);
76 ng->len = INITIAL_NET_GEN_PTRS;
82 static struct kmem_cache *net_cachep;
83 static struct workqueue_struct *netns_wq;
85 static struct net *net_alloc(void)
87 struct net *net = NULL;
88 struct net_generic *ng;
90 ng = net_alloc_generic();
94 net = kmem_cache_zalloc(net_cachep, GFP_KERNEL);
98 rcu_assign_pointer(net->gen, ng);
107 static void net_free(struct net *net)
109 #ifdef NETNS_REFCNT_DEBUG
110 if (unlikely(atomic_read(&net->use_count) != 0)) {
111 printk(KERN_EMERG "network namespace not free! Usage: %d\n",
112 atomic_read(&net->use_count));
117 kmem_cache_free(net_cachep, net);
120 static struct net *net_create(void)
127 return ERR_PTR(-ENOMEM);
128 mutex_lock(&net_mutex);
132 list_add_tail_rcu(&net->list, &net_namespace_list);
135 mutex_unlock(&net_mutex);
143 struct net *copy_net_ns(unsigned long flags, struct net *old_net)
145 if (!(flags & CLONE_NEWNET))
146 return get_net(old_net);
150 static void cleanup_net(struct work_struct *work)
152 struct pernet_operations *ops;
155 net = container_of(work, struct net, work);
157 mutex_lock(&net_mutex);
159 /* Don't let anyone else find us. */
161 list_del_rcu(&net->list);
165 * Another CPU might be rcu-iterating the list, wait for it.
166 * This needs to be before calling the exit() notifiers, so
167 * the rcu_barrier() below isn't sufficient alone.
171 /* Run all of the network namespace exit methods */
172 list_for_each_entry_reverse(ops, &pernet_list, list) {
177 mutex_unlock(&net_mutex);
179 /* Ensure there are no outstanding rcu callbacks using this
184 /* Finally it is safe to free my network namespace structure */
188 void __put_net(struct net *net)
190 /* Cleanup the network namespace in process context */
191 INIT_WORK(&net->work, cleanup_net);
192 queue_work(netns_wq, &net->work);
194 EXPORT_SYMBOL_GPL(__put_net);
197 struct net *copy_net_ns(unsigned long flags, struct net *old_net)
199 if (flags & CLONE_NEWNET)
200 return ERR_PTR(-EINVAL);
205 struct net *get_net_ns_by_pid(pid_t pid)
207 struct task_struct *tsk;
210 /* Lookup the network namespace */
211 net = ERR_PTR(-ESRCH);
213 tsk = find_task_by_vpid(pid);
215 struct nsproxy *nsproxy;
216 nsproxy = task_nsproxy(tsk);
218 net = get_net(nsproxy->net_ns);
223 EXPORT_SYMBOL_GPL(get_net_ns_by_pid);
225 static int __init net_ns_init(void)
227 struct net_generic *ng;
230 net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
234 /* Create workqueue for cleanup */
235 netns_wq = create_singlethread_workqueue("netns");
237 panic("Could not create netns workq");
240 ng = net_alloc_generic();
242 panic("Could not allocate generic netns");
244 rcu_assign_pointer(init_net.gen, ng);
246 mutex_lock(&net_mutex);
247 if (setup_net(&init_net))
248 panic("Could not setup the initial network namespace");
251 list_add_tail_rcu(&init_net.list, &net_namespace_list);
254 mutex_unlock(&net_mutex);
259 pure_initcall(net_ns_init);
262 static int register_pernet_operations(struct list_head *list,
263 struct pernet_operations *ops)
265 struct net *net, *undo_net;
268 list_add_tail(&ops->list, list);
271 error = ops->init(net);
279 /* If I have an error cleanup all namespaces I initialized */
280 list_del(&ops->list);
282 for_each_net(undo_net) {
292 static void unregister_pernet_operations(struct pernet_operations *ops)
296 list_del(&ops->list);
304 static int register_pernet_operations(struct list_head *list,
305 struct pernet_operations *ops)
307 if (ops->init == NULL)
309 return ops->init(&init_net);
312 static void unregister_pernet_operations(struct pernet_operations *ops)
315 ops->exit(&init_net);
319 static DEFINE_IDA(net_generic_ids);
322 * register_pernet_subsys - register a network namespace subsystem
323 * @ops: pernet operations structure for the subsystem
325 * Register a subsystem which has init and exit functions
326 * that are called when network namespaces are created and
327 * destroyed respectively.
329 * When registered all network namespace init functions are
330 * called for every existing network namespace. Allowing kernel
331 * modules to have a race free view of the set of network namespaces.
333 * When a new network namespace is created all of the init
334 * methods are called in the order in which they were registered.
336 * When a network namespace is destroyed all of the exit methods
337 * are called in the reverse of the order with which they were
340 int register_pernet_subsys(struct pernet_operations *ops)
343 mutex_lock(&net_mutex);
344 error = register_pernet_operations(first_device, ops);
345 mutex_unlock(&net_mutex);
348 EXPORT_SYMBOL_GPL(register_pernet_subsys);
351 * unregister_pernet_subsys - unregister a network namespace subsystem
352 * @ops: pernet operations structure to manipulate
354 * Remove the pernet operations structure from the list to be
355 * used when network namespaces are created or destroyed. In
356 * addition run the exit method for all existing network
359 void unregister_pernet_subsys(struct pernet_operations *module)
361 mutex_lock(&net_mutex);
362 unregister_pernet_operations(module);
363 mutex_unlock(&net_mutex);
365 EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
367 int register_pernet_gen_subsys(int *id, struct pernet_operations *ops)
371 mutex_lock(&net_mutex);
373 rv = ida_get_new_above(&net_generic_ids, 1, id);
376 ida_pre_get(&net_generic_ids, GFP_KERNEL);
381 rv = register_pernet_operations(first_device, ops);
383 ida_remove(&net_generic_ids, *id);
385 mutex_unlock(&net_mutex);
388 EXPORT_SYMBOL_GPL(register_pernet_gen_subsys);
390 void unregister_pernet_gen_subsys(int id, struct pernet_operations *ops)
392 mutex_lock(&net_mutex);
393 unregister_pernet_operations(ops);
394 ida_remove(&net_generic_ids, id);
395 mutex_unlock(&net_mutex);
397 EXPORT_SYMBOL_GPL(unregister_pernet_gen_subsys);
400 * register_pernet_device - register a network namespace device
401 * @ops: pernet operations structure for the subsystem
403 * Register a device which has init and exit functions
404 * that are called when network namespaces are created and
405 * destroyed respectively.
407 * When registered all network namespace init functions are
408 * called for every existing network namespace. Allowing kernel
409 * modules to have a race free view of the set of network namespaces.
411 * When a new network namespace is created all of the init
412 * methods are called in the order in which they were registered.
414 * When a network namespace is destroyed all of the exit methods
415 * are called in the reverse of the order with which they were
418 int register_pernet_device(struct pernet_operations *ops)
421 mutex_lock(&net_mutex);
422 error = register_pernet_operations(&pernet_list, ops);
423 if (!error && (first_device == &pernet_list))
424 first_device = &ops->list;
425 mutex_unlock(&net_mutex);
428 EXPORT_SYMBOL_GPL(register_pernet_device);
430 int register_pernet_gen_device(int *id, struct pernet_operations *ops)
433 mutex_lock(&net_mutex);
435 error = ida_get_new_above(&net_generic_ids, 1, id);
437 if (error == -EAGAIN) {
438 ida_pre_get(&net_generic_ids, GFP_KERNEL);
443 error = register_pernet_operations(&pernet_list, ops);
445 ida_remove(&net_generic_ids, *id);
446 else if (first_device == &pernet_list)
447 first_device = &ops->list;
449 mutex_unlock(&net_mutex);
452 EXPORT_SYMBOL_GPL(register_pernet_gen_device);
455 * unregister_pernet_device - unregister a network namespace netdevice
456 * @ops: pernet operations structure to manipulate
458 * Remove the pernet operations structure from the list to be
459 * used when network namespaces are created or destroyed. In
460 * addition run the exit method for all existing network
463 void unregister_pernet_device(struct pernet_operations *ops)
465 mutex_lock(&net_mutex);
466 if (&ops->list == first_device)
467 first_device = first_device->next;
468 unregister_pernet_operations(ops);
469 mutex_unlock(&net_mutex);
471 EXPORT_SYMBOL_GPL(unregister_pernet_device);
473 void unregister_pernet_gen_device(int id, struct pernet_operations *ops)
475 mutex_lock(&net_mutex);
476 if (&ops->list == first_device)
477 first_device = first_device->next;
478 unregister_pernet_operations(ops);
479 ida_remove(&net_generic_ids, id);
480 mutex_unlock(&net_mutex);
482 EXPORT_SYMBOL_GPL(unregister_pernet_gen_device);
484 static void net_generic_release(struct rcu_head *rcu)
486 struct net_generic *ng;
488 ng = container_of(rcu, struct net_generic, rcu);
492 int net_assign_generic(struct net *net, int id, void *data)
494 struct net_generic *ng, *old_ng;
496 BUG_ON(!mutex_is_locked(&net_mutex));
499 ng = old_ng = net->gen;
500 if (old_ng->len >= id)
503 ng = kzalloc(sizeof(struct net_generic) +
504 id * sizeof(void *), GFP_KERNEL);
509 * Some synchronisation notes:
511 * The net_generic explores the net->gen array inside rcu
512 * read section. Besides once set the net->gen->ptr[x]
513 * pointer never changes (see rules in netns/generic.h).
515 * That said, we simply duplicate this array and schedule
516 * the old copy for kfree after a grace period.
520 memcpy(&ng->ptr, &old_ng->ptr, old_ng->len * sizeof(void*));
522 rcu_assign_pointer(net->gen, ng);
523 call_rcu(&old_ng->rcu, net_generic_release);
525 ng->ptr[id - 1] = data;
528 EXPORT_SYMBOL_GPL(net_assign_generic);