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;
125 if (!(flags & CLONE_NEWNET))
129 new_net = net_alloc();
133 mutex_lock(&net_mutex);
134 err = setup_net(new_net);
137 list_add_tail(&new_net->list, &net_namespace_list);
140 mutex_unlock(&net_mutex);
151 new_net = ERR_PTR(err);
155 static void cleanup_net(struct work_struct *work)
157 struct pernet_operations *ops;
160 /* Be very certain incoming network packets will not find us */
163 net = container_of(work, struct net, work);
165 mutex_lock(&net_mutex);
167 /* Don't let anyone else find us. */
169 list_del(&net->list);
172 /* Run all of the network namespace exit methods */
173 list_for_each_entry_reverse(ops, &pernet_list, list) {
178 mutex_unlock(&net_mutex);
180 /* Ensure there are no outstanding rcu callbacks using this
185 /* Finally it is safe to free my network namespace structure */
189 void __put_net(struct net *net)
191 /* Cleanup the network namespace in process context */
192 INIT_WORK(&net->work, cleanup_net);
193 queue_work(netns_wq, &net->work);
195 EXPORT_SYMBOL_GPL(__put_net);
198 struct net *copy_net_ns(unsigned long flags, struct net *old_net)
200 if (flags & CLONE_NEWNET)
201 return ERR_PTR(-EINVAL);
206 static int __init net_ns_init(void)
208 struct net_generic *ng;
211 printk(KERN_INFO "net_namespace: %zd bytes\n", sizeof(struct net));
213 net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
217 /* Create workqueue for cleanup */
218 netns_wq = create_singlethread_workqueue("netns");
220 panic("Could not create netns workq");
223 ng = net_alloc_generic();
225 panic("Could not allocate generic netns");
227 rcu_assign_pointer(init_net.gen, ng);
229 mutex_lock(&net_mutex);
230 err = setup_net(&init_net);
233 list_add_tail(&init_net.list, &net_namespace_list);
236 mutex_unlock(&net_mutex);
238 panic("Could not setup the initial network namespace");
243 pure_initcall(net_ns_init);
246 static int register_pernet_operations(struct list_head *list,
247 struct pernet_operations *ops)
249 struct net *net, *undo_net;
252 list_add_tail(&ops->list, list);
255 error = ops->init(net);
263 /* If I have an error cleanup all namespaces I initialized */
264 list_del(&ops->list);
266 for_each_net(undo_net) {
276 static void unregister_pernet_operations(struct pernet_operations *ops)
280 list_del(&ops->list);
288 static int register_pernet_operations(struct list_head *list,
289 struct pernet_operations *ops)
291 if (ops->init == NULL)
293 return ops->init(&init_net);
296 static void unregister_pernet_operations(struct pernet_operations *ops)
299 ops->exit(&init_net);
303 static DEFINE_IDA(net_generic_ids);
306 * register_pernet_subsys - register a network namespace subsystem
307 * @ops: pernet operations structure for the subsystem
309 * Register a subsystem which has init and exit functions
310 * that are called when network namespaces are created and
311 * destroyed respectively.
313 * When registered all network namespace init functions are
314 * called for every existing network namespace. Allowing kernel
315 * modules to have a race free view of the set of network namespaces.
317 * When a new network namespace is created all of the init
318 * methods are called in the order in which they were registered.
320 * When a network namespace is destroyed all of the exit methods
321 * are called in the reverse of the order with which they were
324 int register_pernet_subsys(struct pernet_operations *ops)
327 mutex_lock(&net_mutex);
328 error = register_pernet_operations(first_device, ops);
329 mutex_unlock(&net_mutex);
332 EXPORT_SYMBOL_GPL(register_pernet_subsys);
335 * unregister_pernet_subsys - unregister a network namespace subsystem
336 * @ops: pernet operations structure to manipulate
338 * Remove the pernet operations structure from the list to be
339 * used when network namespaces are created or destroyed. In
340 * addition run the exit method for all existing network
343 void unregister_pernet_subsys(struct pernet_operations *module)
345 mutex_lock(&net_mutex);
346 unregister_pernet_operations(module);
347 mutex_unlock(&net_mutex);
349 EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
351 int register_pernet_gen_subsys(int *id, struct pernet_operations *ops)
355 mutex_lock(&net_mutex);
357 rv = ida_get_new_above(&net_generic_ids, 1, id);
360 ida_pre_get(&net_generic_ids, GFP_KERNEL);
365 rv = register_pernet_operations(first_device, ops);
367 ida_remove(&net_generic_ids, *id);
369 mutex_unlock(&net_mutex);
372 EXPORT_SYMBOL_GPL(register_pernet_gen_subsys);
374 void unregister_pernet_gen_subsys(int id, struct pernet_operations *ops)
376 mutex_lock(&net_mutex);
377 unregister_pernet_operations(ops);
378 ida_remove(&net_generic_ids, id);
379 mutex_unlock(&net_mutex);
381 EXPORT_SYMBOL_GPL(unregister_pernet_gen_subsys);
384 * register_pernet_device - register a network namespace device
385 * @ops: pernet operations structure for the subsystem
387 * Register a device which has init and exit functions
388 * that are called when network namespaces are created and
389 * destroyed respectively.
391 * When registered all network namespace init functions are
392 * called for every existing network namespace. Allowing kernel
393 * modules to have a race free view of the set of network namespaces.
395 * When a new network namespace is created all of the init
396 * methods are called in the order in which they were registered.
398 * When a network namespace is destroyed all of the exit methods
399 * are called in the reverse of the order with which they were
402 int register_pernet_device(struct pernet_operations *ops)
405 mutex_lock(&net_mutex);
406 error = register_pernet_operations(&pernet_list, ops);
407 if (!error && (first_device == &pernet_list))
408 first_device = &ops->list;
409 mutex_unlock(&net_mutex);
412 EXPORT_SYMBOL_GPL(register_pernet_device);
414 int register_pernet_gen_device(int *id, struct pernet_operations *ops)
417 mutex_lock(&net_mutex);
419 error = ida_get_new_above(&net_generic_ids, 1, id);
421 if (error == -EAGAIN) {
422 ida_pre_get(&net_generic_ids, GFP_KERNEL);
427 error = register_pernet_operations(&pernet_list, ops);
429 ida_remove(&net_generic_ids, *id);
430 else if (first_device == &pernet_list)
431 first_device = &ops->list;
433 mutex_unlock(&net_mutex);
436 EXPORT_SYMBOL_GPL(register_pernet_gen_device);
439 * unregister_pernet_device - unregister a network namespace netdevice
440 * @ops: pernet operations structure to manipulate
442 * Remove the pernet operations structure from the list to be
443 * used when network namespaces are created or destroyed. In
444 * addition run the exit method for all existing network
447 void unregister_pernet_device(struct pernet_operations *ops)
449 mutex_lock(&net_mutex);
450 if (&ops->list == first_device)
451 first_device = first_device->next;
452 unregister_pernet_operations(ops);
453 mutex_unlock(&net_mutex);
455 EXPORT_SYMBOL_GPL(unregister_pernet_device);
457 void unregister_pernet_gen_device(int id, struct pernet_operations *ops)
459 mutex_lock(&net_mutex);
460 if (&ops->list == first_device)
461 first_device = first_device->next;
462 unregister_pernet_operations(ops);
463 ida_remove(&net_generic_ids, id);
464 mutex_unlock(&net_mutex);
466 EXPORT_SYMBOL_GPL(unregister_pernet_gen_device);
468 static void net_generic_release(struct rcu_head *rcu)
470 struct net_generic *ng;
472 ng = container_of(rcu, struct net_generic, rcu);
476 int net_assign_generic(struct net *net, int id, void *data)
478 struct net_generic *ng, *old_ng;
480 BUG_ON(!mutex_is_locked(&net_mutex));
483 ng = old_ng = net->gen;
484 if (old_ng->len >= id)
487 ng = kzalloc(sizeof(struct net_generic) +
488 id * sizeof(void *), GFP_KERNEL);
493 * Some synchronisation notes:
495 * The net_generic explores the net->gen array inside rcu
496 * read section. Besides once set the net->gen->ptr[x]
497 * pointer never changes (see rules in netns/generic.h).
499 * That said, we simply duplicate this array and schedule
500 * the old copy for kfree after a grace period.
504 memcpy(&ng->ptr, &old_ng->ptr, old_ng->len);
506 rcu_assign_pointer(net->gen, ng);
507 call_rcu(&old_ng->rcu, net_generic_release);
509 ng->ptr[id - 1] = data;
512 EXPORT_SYMBOL_GPL(net_assign_generic);