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 <linux/proc_fs.h>
12 #include <linux/file.h>
13 #include <linux/export.h>
14 #include <net/net_namespace.h>
15 #include <net/netns/generic.h>
18 * Our network namespace constructor/destructor lists
21 static LIST_HEAD(pernet_list);
22 static struct list_head *first_device = &pernet_list;
23 static DEFINE_MUTEX(net_mutex);
25 LIST_HEAD(net_namespace_list);
26 EXPORT_SYMBOL_GPL(net_namespace_list);
29 EXPORT_SYMBOL(init_net);
31 #define INITIAL_NET_GEN_PTRS 13 /* +1 for len +2 for rcu_head */
33 static unsigned int max_gen_ptrs = INITIAL_NET_GEN_PTRS;
35 static struct net_generic *net_alloc_generic(void)
37 struct net_generic *ng;
38 size_t generic_size = offsetof(struct net_generic, ptr[max_gen_ptrs]);
40 ng = kzalloc(generic_size, GFP_KERNEL);
42 ng->len = max_gen_ptrs;
47 static int net_assign_generic(struct net *net, int id, void *data)
49 struct net_generic *ng, *old_ng;
51 BUG_ON(!mutex_is_locked(&net_mutex));
54 old_ng = rcu_dereference_protected(net->gen,
55 lockdep_is_held(&net_mutex));
57 if (old_ng->len >= id)
60 ng = net_alloc_generic();
65 * Some synchronisation notes:
67 * The net_generic explores the net->gen array inside rcu
68 * read section. Besides once set the net->gen->ptr[x]
69 * pointer never changes (see rules in netns/generic.h).
71 * That said, we simply duplicate this array and schedule
72 * the old copy for kfree after a grace period.
75 memcpy(&ng->ptr, &old_ng->ptr, old_ng->len * sizeof(void*));
77 rcu_assign_pointer(net->gen, ng);
78 kfree_rcu(old_ng, rcu);
80 ng->ptr[id - 1] = data;
84 static int ops_init(const struct pernet_operations *ops, struct net *net)
89 if (ops->id && ops->size) {
90 data = kzalloc(ops->size, GFP_KERNEL);
94 err = net_assign_generic(net, *ops->id, data);
100 err = ops->init(net);
111 static void ops_free(const struct pernet_operations *ops, struct net *net)
113 if (ops->id && ops->size) {
115 kfree(net_generic(net, id));
119 static void ops_exit_list(const struct pernet_operations *ops,
120 struct list_head *net_exit_list)
124 list_for_each_entry(net, net_exit_list, exit_list)
128 ops->exit_batch(net_exit_list);
131 static void ops_free_list(const struct pernet_operations *ops,
132 struct list_head *net_exit_list)
135 if (ops->size && ops->id) {
136 list_for_each_entry(net, net_exit_list, exit_list)
142 * setup_net runs the initializers for the network namespace object.
144 static __net_init int setup_net(struct net *net)
146 /* Must be called with net_mutex held */
147 const struct pernet_operations *ops, *saved_ops;
149 LIST_HEAD(net_exit_list);
151 atomic_set(&net->count, 1);
152 atomic_set(&net->passive, 1);
153 net->dev_base_seq = 1;
155 #ifdef NETNS_REFCNT_DEBUG
156 atomic_set(&net->use_count, 0);
159 list_for_each_entry(ops, &pernet_list, list) {
160 error = ops_init(ops, net);
168 /* Walk through the list backwards calling the exit functions
169 * for the pernet modules whose init functions did not fail.
171 list_add(&net->exit_list, &net_exit_list);
173 list_for_each_entry_continue_reverse(ops, &pernet_list, list)
174 ops_exit_list(ops, &net_exit_list);
177 list_for_each_entry_continue_reverse(ops, &pernet_list, list)
178 ops_free_list(ops, &net_exit_list);
186 static struct kmem_cache *net_cachep;
187 static struct workqueue_struct *netns_wq;
189 static struct net *net_alloc(void)
191 struct net *net = NULL;
192 struct net_generic *ng;
194 ng = net_alloc_generic();
198 net = kmem_cache_zalloc(net_cachep, GFP_KERNEL);
202 rcu_assign_pointer(net->gen, ng);
211 static void net_free(struct net *net)
213 #ifdef NETNS_REFCNT_DEBUG
214 if (unlikely(atomic_read(&net->use_count) != 0)) {
215 printk(KERN_EMERG "network namespace not free! Usage: %d\n",
216 atomic_read(&net->use_count));
221 kmem_cache_free(net_cachep, net);
224 void net_drop_ns(void *p)
227 if (ns && atomic_dec_and_test(&ns->passive))
231 struct net *copy_net_ns(unsigned long flags, struct net *old_net)
236 if (!(flags & CLONE_NEWNET))
237 return get_net(old_net);
241 return ERR_PTR(-ENOMEM);
242 mutex_lock(&net_mutex);
246 list_add_tail_rcu(&net->list, &net_namespace_list);
249 mutex_unlock(&net_mutex);
257 static DEFINE_SPINLOCK(cleanup_list_lock);
258 static LIST_HEAD(cleanup_list); /* Must hold cleanup_list_lock to touch */
260 static void cleanup_net(struct work_struct *work)
262 const struct pernet_operations *ops;
263 struct net *net, *tmp;
264 LIST_HEAD(net_kill_list);
265 LIST_HEAD(net_exit_list);
267 /* Atomically snapshot the list of namespaces to cleanup */
268 spin_lock_irq(&cleanup_list_lock);
269 list_replace_init(&cleanup_list, &net_kill_list);
270 spin_unlock_irq(&cleanup_list_lock);
272 mutex_lock(&net_mutex);
274 /* Don't let anyone else find us. */
276 list_for_each_entry(net, &net_kill_list, cleanup_list) {
277 list_del_rcu(&net->list);
278 list_add_tail(&net->exit_list, &net_exit_list);
283 * Another CPU might be rcu-iterating the list, wait for it.
284 * This needs to be before calling the exit() notifiers, so
285 * the rcu_barrier() below isn't sufficient alone.
289 /* Run all of the network namespace exit methods */
290 list_for_each_entry_reverse(ops, &pernet_list, list)
291 ops_exit_list(ops, &net_exit_list);
293 /* Free the net generic variables */
294 list_for_each_entry_reverse(ops, &pernet_list, list)
295 ops_free_list(ops, &net_exit_list);
297 mutex_unlock(&net_mutex);
299 /* Ensure there are no outstanding rcu callbacks using this
304 /* Finally it is safe to free my network namespace structure */
305 list_for_each_entry_safe(net, tmp, &net_exit_list, exit_list) {
306 list_del_init(&net->exit_list);
310 static DECLARE_WORK(net_cleanup_work, cleanup_net);
312 void __put_net(struct net *net)
314 /* Cleanup the network namespace in process context */
317 spin_lock_irqsave(&cleanup_list_lock, flags);
318 list_add(&net->cleanup_list, &cleanup_list);
319 spin_unlock_irqrestore(&cleanup_list_lock, flags);
321 queue_work(netns_wq, &net_cleanup_work);
323 EXPORT_SYMBOL_GPL(__put_net);
325 struct net *get_net_ns_by_fd(int fd)
327 struct proc_inode *ei;
331 file = proc_ns_fget(fd);
333 return ERR_CAST(file);
335 ei = PROC_I(file->f_dentry->d_inode);
336 if (ei->ns_ops == &netns_operations)
337 net = get_net(ei->ns);
339 net = ERR_PTR(-EINVAL);
346 struct net *copy_net_ns(unsigned long flags, struct net *old_net)
348 if (flags & CLONE_NEWNET)
349 return ERR_PTR(-EINVAL);
353 struct net *get_net_ns_by_fd(int fd)
355 return ERR_PTR(-EINVAL);
359 struct net *get_net_ns_by_pid(pid_t pid)
361 struct task_struct *tsk;
364 /* Lookup the network namespace */
365 net = ERR_PTR(-ESRCH);
367 tsk = find_task_by_vpid(pid);
369 struct nsproxy *nsproxy;
370 nsproxy = task_nsproxy(tsk);
372 net = get_net(nsproxy->net_ns);
377 EXPORT_SYMBOL_GPL(get_net_ns_by_pid);
379 static int __init net_ns_init(void)
381 struct net_generic *ng;
384 net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
388 /* Create workqueue for cleanup */
389 netns_wq = create_singlethread_workqueue("netns");
391 panic("Could not create netns workq");
394 ng = net_alloc_generic();
396 panic("Could not allocate generic netns");
398 rcu_assign_pointer(init_net.gen, ng);
400 mutex_lock(&net_mutex);
401 if (setup_net(&init_net))
402 panic("Could not setup the initial network namespace");
405 list_add_tail_rcu(&init_net.list, &net_namespace_list);
408 mutex_unlock(&net_mutex);
413 pure_initcall(net_ns_init);
416 static int __register_pernet_operations(struct list_head *list,
417 struct pernet_operations *ops)
421 LIST_HEAD(net_exit_list);
423 list_add_tail(&ops->list, list);
424 if (ops->init || (ops->id && ops->size)) {
426 error = ops_init(ops, net);
429 list_add_tail(&net->exit_list, &net_exit_list);
435 /* If I have an error cleanup all namespaces I initialized */
436 list_del(&ops->list);
437 ops_exit_list(ops, &net_exit_list);
438 ops_free_list(ops, &net_exit_list);
442 static void __unregister_pernet_operations(struct pernet_operations *ops)
445 LIST_HEAD(net_exit_list);
447 list_del(&ops->list);
449 list_add_tail(&net->exit_list, &net_exit_list);
450 ops_exit_list(ops, &net_exit_list);
451 ops_free_list(ops, &net_exit_list);
456 static int __register_pernet_operations(struct list_head *list,
457 struct pernet_operations *ops)
459 return ops_init(ops, &init_net);
462 static void __unregister_pernet_operations(struct pernet_operations *ops)
464 LIST_HEAD(net_exit_list);
465 list_add(&init_net.exit_list, &net_exit_list);
466 ops_exit_list(ops, &net_exit_list);
467 ops_free_list(ops, &net_exit_list);
470 #endif /* CONFIG_NET_NS */
472 static DEFINE_IDA(net_generic_ids);
474 static int register_pernet_operations(struct list_head *list,
475 struct pernet_operations *ops)
481 error = ida_get_new_above(&net_generic_ids, 1, ops->id);
483 if (error == -EAGAIN) {
484 ida_pre_get(&net_generic_ids, GFP_KERNEL);
489 max_gen_ptrs = max_t(unsigned int, max_gen_ptrs, *ops->id);
491 error = __register_pernet_operations(list, ops);
495 ida_remove(&net_generic_ids, *ops->id);
501 static void unregister_pernet_operations(struct pernet_operations *ops)
504 __unregister_pernet_operations(ops);
507 ida_remove(&net_generic_ids, *ops->id);
511 * register_pernet_subsys - register a network namespace subsystem
512 * @ops: pernet operations structure for the subsystem
514 * Register a subsystem which has init and exit functions
515 * that are called when network namespaces are created and
516 * destroyed respectively.
518 * When registered all network namespace init functions are
519 * called for every existing network namespace. Allowing kernel
520 * modules to have a race free view of the set of network namespaces.
522 * When a new network namespace is created all of the init
523 * methods are called in the order in which they were registered.
525 * When a network namespace is destroyed all of the exit methods
526 * are called in the reverse of the order with which they were
529 int register_pernet_subsys(struct pernet_operations *ops)
532 mutex_lock(&net_mutex);
533 error = register_pernet_operations(first_device, ops);
534 mutex_unlock(&net_mutex);
537 EXPORT_SYMBOL_GPL(register_pernet_subsys);
540 * unregister_pernet_subsys - unregister a network namespace subsystem
541 * @ops: pernet operations structure to manipulate
543 * Remove the pernet operations structure from the list to be
544 * used when network namespaces are created or destroyed. In
545 * addition run the exit method for all existing network
548 void unregister_pernet_subsys(struct pernet_operations *ops)
550 mutex_lock(&net_mutex);
551 unregister_pernet_operations(ops);
552 mutex_unlock(&net_mutex);
554 EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
557 * register_pernet_device - register a network namespace device
558 * @ops: pernet operations structure for the subsystem
560 * Register a device which has init and exit functions
561 * that are called when network namespaces are created and
562 * destroyed respectively.
564 * When registered all network namespace init functions are
565 * called for every existing network namespace. Allowing kernel
566 * modules to have a race free view of the set of network namespaces.
568 * When a new network namespace is created all of the init
569 * methods are called in the order in which they were registered.
571 * When a network namespace is destroyed all of the exit methods
572 * are called in the reverse of the order with which they were
575 int register_pernet_device(struct pernet_operations *ops)
578 mutex_lock(&net_mutex);
579 error = register_pernet_operations(&pernet_list, ops);
580 if (!error && (first_device == &pernet_list))
581 first_device = &ops->list;
582 mutex_unlock(&net_mutex);
585 EXPORT_SYMBOL_GPL(register_pernet_device);
588 * unregister_pernet_device - unregister a network namespace netdevice
589 * @ops: pernet operations structure to manipulate
591 * Remove the pernet operations structure from the list to be
592 * used when network namespaces are created or destroyed. In
593 * addition run the exit method for all existing network
596 void unregister_pernet_device(struct pernet_operations *ops)
598 mutex_lock(&net_mutex);
599 if (&ops->list == first_device)
600 first_device = first_device->next;
601 unregister_pernet_operations(ops);
602 mutex_unlock(&net_mutex);
604 EXPORT_SYMBOL_GPL(unregister_pernet_device);
607 static void *netns_get(struct task_struct *task)
609 struct net *net = NULL;
610 struct nsproxy *nsproxy;
613 nsproxy = task_nsproxy(task);
615 net = get_net(nsproxy->net_ns);
621 static void netns_put(void *ns)
626 static int netns_install(struct nsproxy *nsproxy, void *ns)
628 put_net(nsproxy->net_ns);
629 nsproxy->net_ns = get_net(ns);
633 const struct proc_ns_operations netns_operations = {
635 .type = CLONE_NEWNET,
638 .install = netns_install,