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 <net/net_namespace.h>
10 #include <net/netns/generic.h>
13 * Our network namespace constructor/destructor lists
16 static LIST_HEAD(pernet_list);
17 static struct list_head *first_device = &pernet_list;
18 static DEFINE_MUTEX(net_mutex);
20 LIST_HEAD(net_namespace_list);
21 EXPORT_SYMBOL_GPL(net_namespace_list);
24 EXPORT_SYMBOL(init_net);
26 #define INITIAL_NET_GEN_PTRS 13 /* +1 for len +2 for rcu_head */
29 * setup_net runs the initializers for the network namespace object.
31 static __net_init int setup_net(struct net *net)
33 /* Must be called with net_mutex held */
34 struct pernet_operations *ops;
37 atomic_set(&net->count, 1);
39 #ifdef NETNS_REFCNT_DEBUG
40 atomic_set(&net->use_count, 0);
43 list_for_each_entry(ops, &pernet_list, list) {
45 error = ops->init(net);
54 /* Walk through the list backwards calling the exit functions
55 * for the pernet modules whose init functions did not fail.
57 list_for_each_entry_continue_reverse(ops, &pernet_list, list) {
66 static struct net_generic *net_alloc_generic(void)
68 struct net_generic *ng;
69 size_t generic_size = sizeof(struct net_generic) +
70 INITIAL_NET_GEN_PTRS * sizeof(void *);
72 ng = kzalloc(generic_size, GFP_KERNEL);
74 ng->len = INITIAL_NET_GEN_PTRS;
80 static struct kmem_cache *net_cachep;
81 static struct workqueue_struct *netns_wq;
83 static struct net *net_alloc(void)
85 struct net *net = NULL;
86 struct net_generic *ng;
88 ng = net_alloc_generic();
92 net = kmem_cache_zalloc(net_cachep, GFP_KERNEL);
96 rcu_assign_pointer(net->gen, ng);
105 static void net_free(struct net *net)
107 #ifdef NETNS_REFCNT_DEBUG
108 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));
115 kmem_cache_free(net_cachep, net);
118 struct net *copy_net_ns(unsigned long flags, struct net *old_net)
120 struct net *new_net = NULL;
123 if (!(flags & CLONE_NEWNET))
124 return get_net(old_net);
127 new_net = net_alloc();
131 mutex_lock(&net_mutex);
132 err = setup_net(new_net);
135 list_add_tail(&new_net->list, &net_namespace_list);
138 mutex_unlock(&net_mutex);
148 new_net = ERR_PTR(err);
152 static void cleanup_net(struct work_struct *work)
154 struct pernet_operations *ops;
157 net = container_of(work, struct net, work);
159 mutex_lock(&net_mutex);
161 /* Don't let anyone else find us. */
163 list_del(&net->list);
166 /* Run all of the network namespace exit methods */
167 list_for_each_entry_reverse(ops, &pernet_list, list) {
172 mutex_unlock(&net_mutex);
174 /* Ensure there are no outstanding rcu callbacks using this
179 /* Finally it is safe to free my network namespace structure */
183 void __put_net(struct net *net)
185 /* Cleanup the network namespace in process context */
186 INIT_WORK(&net->work, cleanup_net);
187 queue_work(netns_wq, &net->work);
189 EXPORT_SYMBOL_GPL(__put_net);
192 struct net *copy_net_ns(unsigned long flags, struct net *old_net)
194 if (flags & CLONE_NEWNET)
195 return ERR_PTR(-EINVAL);
200 static int __init net_ns_init(void)
202 struct net_generic *ng;
205 printk(KERN_INFO "net_namespace: %zd bytes\n", sizeof(struct net));
207 net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
211 /* Create workqueue for cleanup */
212 netns_wq = create_singlethread_workqueue("netns");
214 panic("Could not create netns workq");
217 ng = net_alloc_generic();
219 panic("Could not allocate generic netns");
221 rcu_assign_pointer(init_net.gen, ng);
223 mutex_lock(&net_mutex);
224 err = setup_net(&init_net);
227 list_add_tail(&init_net.list, &net_namespace_list);
230 mutex_unlock(&net_mutex);
232 panic("Could not setup the initial network namespace");
237 pure_initcall(net_ns_init);
240 static int register_pernet_operations(struct list_head *list,
241 struct pernet_operations *ops)
243 struct net *net, *undo_net;
246 list_add_tail(&ops->list, list);
249 error = ops->init(net);
257 /* If I have an error cleanup all namespaces I initialized */
258 list_del(&ops->list);
260 for_each_net(undo_net) {
270 static void unregister_pernet_operations(struct pernet_operations *ops)
274 list_del(&ops->list);
282 static int register_pernet_operations(struct list_head *list,
283 struct pernet_operations *ops)
285 if (ops->init == NULL)
287 return ops->init(&init_net);
290 static void unregister_pernet_operations(struct pernet_operations *ops)
293 ops->exit(&init_net);
297 static DEFINE_IDA(net_generic_ids);
300 * register_pernet_subsys - register a network namespace subsystem
301 * @ops: pernet operations structure for the subsystem
303 * Register a subsystem which has init and exit functions
304 * that are called when network namespaces are created and
305 * destroyed respectively.
307 * When registered all network namespace init functions are
308 * called for every existing network namespace. Allowing kernel
309 * modules to have a race free view of the set of network namespaces.
311 * When a new network namespace is created all of the init
312 * methods are called in the order in which they were registered.
314 * When a network namespace is destroyed all of the exit methods
315 * are called in the reverse of the order with which they were
318 int register_pernet_subsys(struct pernet_operations *ops)
321 mutex_lock(&net_mutex);
322 error = register_pernet_operations(first_device, ops);
323 mutex_unlock(&net_mutex);
326 EXPORT_SYMBOL_GPL(register_pernet_subsys);
329 * unregister_pernet_subsys - unregister a network namespace subsystem
330 * @ops: pernet operations structure to manipulate
332 * Remove the pernet operations structure from the list to be
333 * used when network namespaces are created or destroyed. In
334 * addition run the exit method for all existing network
337 void unregister_pernet_subsys(struct pernet_operations *module)
339 mutex_lock(&net_mutex);
340 unregister_pernet_operations(module);
341 mutex_unlock(&net_mutex);
343 EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
345 int register_pernet_gen_subsys(int *id, struct pernet_operations *ops)
349 mutex_lock(&net_mutex);
351 rv = ida_get_new_above(&net_generic_ids, 1, id);
354 ida_pre_get(&net_generic_ids, GFP_KERNEL);
359 rv = register_pernet_operations(first_device, ops);
361 ida_remove(&net_generic_ids, *id);
363 mutex_unlock(&net_mutex);
366 EXPORT_SYMBOL_GPL(register_pernet_gen_subsys);
368 void unregister_pernet_gen_subsys(int id, struct pernet_operations *ops)
370 mutex_lock(&net_mutex);
371 unregister_pernet_operations(ops);
372 ida_remove(&net_generic_ids, id);
373 mutex_unlock(&net_mutex);
375 EXPORT_SYMBOL_GPL(unregister_pernet_gen_subsys);
378 * register_pernet_device - register a network namespace device
379 * @ops: pernet operations structure for the subsystem
381 * Register a device which has init and exit functions
382 * that are called when network namespaces are created and
383 * destroyed respectively.
385 * When registered all network namespace init functions are
386 * called for every existing network namespace. Allowing kernel
387 * modules to have a race free view of the set of network namespaces.
389 * When a new network namespace is created all of the init
390 * methods are called in the order in which they were registered.
392 * When a network namespace is destroyed all of the exit methods
393 * are called in the reverse of the order with which they were
396 int register_pernet_device(struct pernet_operations *ops)
399 mutex_lock(&net_mutex);
400 error = register_pernet_operations(&pernet_list, ops);
401 if (!error && (first_device == &pernet_list))
402 first_device = &ops->list;
403 mutex_unlock(&net_mutex);
406 EXPORT_SYMBOL_GPL(register_pernet_device);
408 int register_pernet_gen_device(int *id, struct pernet_operations *ops)
411 mutex_lock(&net_mutex);
413 error = ida_get_new_above(&net_generic_ids, 1, id);
415 if (error == -EAGAIN) {
416 ida_pre_get(&net_generic_ids, GFP_KERNEL);
421 error = register_pernet_operations(&pernet_list, ops);
423 ida_remove(&net_generic_ids, *id);
424 else if (first_device == &pernet_list)
425 first_device = &ops->list;
427 mutex_unlock(&net_mutex);
430 EXPORT_SYMBOL_GPL(register_pernet_gen_device);
433 * unregister_pernet_device - unregister a network namespace netdevice
434 * @ops: pernet operations structure to manipulate
436 * Remove the pernet operations structure from the list to be
437 * used when network namespaces are created or destroyed. In
438 * addition run the exit method for all existing network
441 void unregister_pernet_device(struct pernet_operations *ops)
443 mutex_lock(&net_mutex);
444 if (&ops->list == first_device)
445 first_device = first_device->next;
446 unregister_pernet_operations(ops);
447 mutex_unlock(&net_mutex);
449 EXPORT_SYMBOL_GPL(unregister_pernet_device);
451 void unregister_pernet_gen_device(int id, struct pernet_operations *ops)
453 mutex_lock(&net_mutex);
454 if (&ops->list == first_device)
455 first_device = first_device->next;
456 unregister_pernet_operations(ops);
457 ida_remove(&net_generic_ids, id);
458 mutex_unlock(&net_mutex);
460 EXPORT_SYMBOL_GPL(unregister_pernet_gen_device);
462 static void net_generic_release(struct rcu_head *rcu)
464 struct net_generic *ng;
466 ng = container_of(rcu, struct net_generic, rcu);
470 int net_assign_generic(struct net *net, int id, void *data)
472 struct net_generic *ng, *old_ng;
474 BUG_ON(!mutex_is_locked(&net_mutex));
477 ng = old_ng = net->gen;
478 if (old_ng->len >= id)
481 ng = kzalloc(sizeof(struct net_generic) +
482 id * sizeof(void *), GFP_KERNEL);
487 * Some synchronisation notes:
489 * The net_generic explores the net->gen array inside rcu
490 * read section. Besides once set the net->gen->ptr[x]
491 * pointer never changes (see rules in netns/generic.h).
493 * That said, we simply duplicate this array and schedule
494 * the old copy for kfree after a grace period.
498 memcpy(&ng->ptr, &old_ng->ptr, old_ng->len);
500 rcu_assign_pointer(net->gen, ng);
501 call_rcu(&old_ng->rcu, net_generic_release);
503 ng->ptr[id - 1] = data;
506 EXPORT_SYMBOL_GPL(net_assign_generic);