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/netdevice.h>
12 #include <net/net_namespace.h>
13 #include <net/netns/generic.h>
14 #include <net/rtnetlink.h>
17 * Our network namespace constructor/destructor lists
20 static LIST_HEAD(pernet_list);
21 static struct list_head *first_device = &pernet_list;
22 static DEFINE_MUTEX(net_mutex);
24 LIST_HEAD(net_namespace_list);
25 EXPORT_SYMBOL_GPL(net_namespace_list);
28 EXPORT_SYMBOL(init_net);
30 #define INITIAL_NET_GEN_PTRS 13 /* +1 for len +2 for rcu_head */
32 static void unregister_netdevices(struct net *net, struct list_head *list)
34 struct net_device *dev;
35 /* At exit all network devices most be removed from a network
36 * namespace. Do this in the reverse order of registeration.
38 for_each_netdev_reverse(net, dev) {
39 if (dev->rtnl_link_ops)
40 dev->rtnl_link_ops->dellink(dev, list);
42 unregister_netdevice_queue(dev, list);
47 * setup_net runs the initializers for the network namespace object.
49 static __net_init int setup_net(struct net *net)
51 /* Must be called with net_mutex held */
52 struct pernet_operations *ops;
55 atomic_set(&net->count, 1);
57 #ifdef NETNS_REFCNT_DEBUG
58 atomic_set(&net->use_count, 0);
61 list_for_each_entry(ops, &pernet_list, list) {
63 error = ops->init(net);
72 /* Walk through the list backwards calling the exit functions
73 * for the pernet modules whose init functions did not fail.
75 list_for_each_entry_continue_reverse(ops, &pernet_list, list) {
78 if (&ops->list == first_device) {
79 LIST_HEAD(dev_kill_list);
81 unregister_netdevices(net, &dev_kill_list);
82 unregister_netdevice_many(&dev_kill_list);
91 static struct net_generic *net_alloc_generic(void)
93 struct net_generic *ng;
94 size_t generic_size = sizeof(struct net_generic) +
95 INITIAL_NET_GEN_PTRS * sizeof(void *);
97 ng = kzalloc(generic_size, GFP_KERNEL);
99 ng->len = INITIAL_NET_GEN_PTRS;
105 static struct kmem_cache *net_cachep;
106 static struct workqueue_struct *netns_wq;
108 static struct net *net_alloc(void)
110 struct net *net = NULL;
111 struct net_generic *ng;
113 ng = net_alloc_generic();
117 net = kmem_cache_zalloc(net_cachep, GFP_KERNEL);
121 rcu_assign_pointer(net->gen, ng);
130 static void net_free(struct net *net)
132 #ifdef NETNS_REFCNT_DEBUG
133 if (unlikely(atomic_read(&net->use_count) != 0)) {
134 printk(KERN_EMERG "network namespace not free! Usage: %d\n",
135 atomic_read(&net->use_count));
140 kmem_cache_free(net_cachep, net);
143 static struct net *net_create(void)
150 return ERR_PTR(-ENOMEM);
151 mutex_lock(&net_mutex);
155 list_add_tail_rcu(&net->list, &net_namespace_list);
158 mutex_unlock(&net_mutex);
166 struct net *copy_net_ns(unsigned long flags, struct net *old_net)
168 if (!(flags & CLONE_NEWNET))
169 return get_net(old_net);
173 static DEFINE_SPINLOCK(cleanup_list_lock);
174 static LIST_HEAD(cleanup_list); /* Must hold cleanup_list_lock to touch */
176 static void cleanup_net(struct work_struct *work)
178 struct pernet_operations *ops;
179 struct net *net, *tmp;
180 LIST_HEAD(net_kill_list);
182 /* Atomically snapshot the list of namespaces to cleanup */
183 spin_lock_irq(&cleanup_list_lock);
184 list_replace_init(&cleanup_list, &net_kill_list);
185 spin_unlock_irq(&cleanup_list_lock);
187 mutex_lock(&net_mutex);
189 /* Don't let anyone else find us. */
191 list_for_each_entry(net, &net_kill_list, cleanup_list)
192 list_del_rcu(&net->list);
196 * Another CPU might be rcu-iterating the list, wait for it.
197 * This needs to be before calling the exit() notifiers, so
198 * the rcu_barrier() below isn't sufficient alone.
202 /* Run all of the network namespace exit methods */
203 list_for_each_entry_reverse(ops, &pernet_list, list) {
205 list_for_each_entry(net, &net_kill_list, cleanup_list)
208 if (&ops->list == first_device) {
209 LIST_HEAD(dev_kill_list);
211 list_for_each_entry(net, &net_kill_list, cleanup_list)
212 unregister_netdevices(net, &dev_kill_list);
213 unregister_netdevice_many(&dev_kill_list);
218 mutex_unlock(&net_mutex);
220 /* Ensure there are no outstanding rcu callbacks using this
225 /* Finally it is safe to free my network namespace structure */
226 list_for_each_entry_safe(net, tmp, &net_kill_list, cleanup_list) {
227 list_del_init(&net->cleanup_list);
231 static DECLARE_WORK(net_cleanup_work, cleanup_net);
233 void __put_net(struct net *net)
235 /* Cleanup the network namespace in process context */
238 spin_lock_irqsave(&cleanup_list_lock, flags);
239 list_add(&net->cleanup_list, &cleanup_list);
240 spin_unlock_irqrestore(&cleanup_list_lock, flags);
242 queue_work(netns_wq, &net_cleanup_work);
244 EXPORT_SYMBOL_GPL(__put_net);
247 struct net *copy_net_ns(unsigned long flags, struct net *old_net)
249 if (flags & CLONE_NEWNET)
250 return ERR_PTR(-EINVAL);
255 struct net *get_net_ns_by_pid(pid_t pid)
257 struct task_struct *tsk;
260 /* Lookup the network namespace */
261 net = ERR_PTR(-ESRCH);
263 tsk = find_task_by_vpid(pid);
265 struct nsproxy *nsproxy;
266 nsproxy = task_nsproxy(tsk);
268 net = get_net(nsproxy->net_ns);
273 EXPORT_SYMBOL_GPL(get_net_ns_by_pid);
275 static int __init net_ns_init(void)
277 struct net_generic *ng;
280 net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
284 /* Create workqueue for cleanup */
285 netns_wq = create_singlethread_workqueue("netns");
287 panic("Could not create netns workq");
290 ng = net_alloc_generic();
292 panic("Could not allocate generic netns");
294 rcu_assign_pointer(init_net.gen, ng);
296 mutex_lock(&net_mutex);
297 if (setup_net(&init_net))
298 panic("Could not setup the initial network namespace");
301 list_add_tail_rcu(&init_net.list, &net_namespace_list);
304 mutex_unlock(&net_mutex);
309 pure_initcall(net_ns_init);
312 static int register_pernet_operations(struct list_head *list,
313 struct pernet_operations *ops)
315 struct net *net, *undo_net;
318 list_add_tail(&ops->list, list);
321 error = ops->init(net);
329 /* If I have an error cleanup all namespaces I initialized */
330 list_del(&ops->list);
332 for_each_net(undo_net) {
333 if (net_eq(undo_net, net))
342 static void unregister_pernet_operations(struct pernet_operations *ops)
346 list_del(&ops->list);
354 static int register_pernet_operations(struct list_head *list,
355 struct pernet_operations *ops)
357 if (ops->init == NULL)
359 return ops->init(&init_net);
362 static void unregister_pernet_operations(struct pernet_operations *ops)
365 ops->exit(&init_net);
369 static DEFINE_IDA(net_generic_ids);
372 * register_pernet_subsys - register a network namespace subsystem
373 * @ops: pernet operations structure for the subsystem
375 * Register a subsystem which has init and exit functions
376 * that are called when network namespaces are created and
377 * destroyed respectively.
379 * When registered all network namespace init functions are
380 * called for every existing network namespace. Allowing kernel
381 * modules to have a race free view of the set of network namespaces.
383 * When a new network namespace is created all of the init
384 * methods are called in the order in which they were registered.
386 * When a network namespace is destroyed all of the exit methods
387 * are called in the reverse of the order with which they were
390 int register_pernet_subsys(struct pernet_operations *ops)
393 mutex_lock(&net_mutex);
394 error = register_pernet_operations(first_device, ops);
395 mutex_unlock(&net_mutex);
398 EXPORT_SYMBOL_GPL(register_pernet_subsys);
401 * unregister_pernet_subsys - unregister a network namespace subsystem
402 * @ops: pernet operations structure to manipulate
404 * Remove the pernet operations structure from the list to be
405 * used when network namespaces are created or destroyed. In
406 * addition run the exit method for all existing network
409 void unregister_pernet_subsys(struct pernet_operations *module)
411 mutex_lock(&net_mutex);
412 unregister_pernet_operations(module);
413 mutex_unlock(&net_mutex);
415 EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
417 int register_pernet_gen_subsys(int *id, struct pernet_operations *ops)
421 mutex_lock(&net_mutex);
423 rv = ida_get_new_above(&net_generic_ids, 1, id);
426 ida_pre_get(&net_generic_ids, GFP_KERNEL);
431 rv = register_pernet_operations(first_device, ops);
433 ida_remove(&net_generic_ids, *id);
435 mutex_unlock(&net_mutex);
438 EXPORT_SYMBOL_GPL(register_pernet_gen_subsys);
440 void unregister_pernet_gen_subsys(int id, struct pernet_operations *ops)
442 mutex_lock(&net_mutex);
443 unregister_pernet_operations(ops);
444 ida_remove(&net_generic_ids, id);
445 mutex_unlock(&net_mutex);
447 EXPORT_SYMBOL_GPL(unregister_pernet_gen_subsys);
450 * register_pernet_device - register a network namespace device
451 * @ops: pernet operations structure for the subsystem
453 * Register a device which has init and exit functions
454 * that are called when network namespaces are created and
455 * destroyed respectively.
457 * When registered all network namespace init functions are
458 * called for every existing network namespace. Allowing kernel
459 * modules to have a race free view of the set of network namespaces.
461 * When a new network namespace is created all of the init
462 * methods are called in the order in which they were registered.
464 * When a network namespace is destroyed all of the exit methods
465 * are called in the reverse of the order with which they were
468 int register_pernet_device(struct pernet_operations *ops)
471 mutex_lock(&net_mutex);
472 error = register_pernet_operations(&pernet_list, ops);
473 if (!error && (first_device == &pernet_list))
474 first_device = &ops->list;
475 mutex_unlock(&net_mutex);
478 EXPORT_SYMBOL_GPL(register_pernet_device);
480 int register_pernet_gen_device(int *id, struct pernet_operations *ops)
483 mutex_lock(&net_mutex);
485 error = ida_get_new_above(&net_generic_ids, 1, id);
487 if (error == -EAGAIN) {
488 ida_pre_get(&net_generic_ids, GFP_KERNEL);
493 error = register_pernet_operations(&pernet_list, ops);
495 ida_remove(&net_generic_ids, *id);
496 else if (first_device == &pernet_list)
497 first_device = &ops->list;
499 mutex_unlock(&net_mutex);
502 EXPORT_SYMBOL_GPL(register_pernet_gen_device);
505 * unregister_pernet_device - unregister a network namespace netdevice
506 * @ops: pernet operations structure to manipulate
508 * Remove the pernet operations structure from the list to be
509 * used when network namespaces are created or destroyed. In
510 * addition run the exit method for all existing network
513 void unregister_pernet_device(struct pernet_operations *ops)
515 mutex_lock(&net_mutex);
516 if (&ops->list == first_device)
517 first_device = first_device->next;
518 unregister_pernet_operations(ops);
519 mutex_unlock(&net_mutex);
521 EXPORT_SYMBOL_GPL(unregister_pernet_device);
523 void unregister_pernet_gen_device(int id, struct pernet_operations *ops)
525 mutex_lock(&net_mutex);
526 if (&ops->list == first_device)
527 first_device = first_device->next;
528 unregister_pernet_operations(ops);
529 ida_remove(&net_generic_ids, id);
530 mutex_unlock(&net_mutex);
532 EXPORT_SYMBOL_GPL(unregister_pernet_gen_device);
534 static void net_generic_release(struct rcu_head *rcu)
536 struct net_generic *ng;
538 ng = container_of(rcu, struct net_generic, rcu);
542 int net_assign_generic(struct net *net, int id, void *data)
544 struct net_generic *ng, *old_ng;
546 BUG_ON(!mutex_is_locked(&net_mutex));
549 ng = old_ng = net->gen;
550 if (old_ng->len >= id)
553 ng = kzalloc(sizeof(struct net_generic) +
554 id * sizeof(void *), GFP_KERNEL);
559 * Some synchronisation notes:
561 * The net_generic explores the net->gen array inside rcu
562 * read section. Besides once set the net->gen->ptr[x]
563 * pointer never changes (see rules in netns/generic.h).
565 * That said, we simply duplicate this array and schedule
566 * the old copy for kfree after a grace period.
570 memcpy(&ng->ptr, &old_ng->ptr, old_ng->len * sizeof(void*));
572 rcu_assign_pointer(net->gen, ng);
573 call_rcu(&old_ng->rcu, net_generic_release);
575 ng->ptr[id - 1] = data;
578 EXPORT_SYMBOL_GPL(net_assign_generic);