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>
12 * Our network namespace constructor/destructor lists
15 static LIST_HEAD(pernet_list);
16 static struct list_head *first_device = &pernet_list;
17 static DEFINE_MUTEX(net_mutex);
19 LIST_HEAD(net_namespace_list);
22 EXPORT_SYMBOL(init_net);
25 * setup_net runs the initializers for the network namespace object.
27 static __net_init int setup_net(struct net *net)
29 /* Must be called with net_mutex held */
30 struct pernet_operations *ops;
33 atomic_set(&net->count, 1);
34 atomic_set(&net->use_count, 0);
37 list_for_each_entry(ops, &pernet_list, list) {
39 error = ops->init(net);
48 /* Walk through the list backwards calling the exit functions
49 * for the pernet modules whose init functions did not fail.
51 list_for_each_entry_continue_reverse(ops, &pernet_list, list) {
61 static struct kmem_cache *net_cachep;
62 static struct workqueue_struct *netns_wq;
64 static struct net *net_alloc(void)
66 return kmem_cache_zalloc(net_cachep, GFP_KERNEL);
69 static void net_free(struct net *net)
74 if (unlikely(atomic_read(&net->use_count) != 0)) {
75 printk(KERN_EMERG "network namespace not free! Usage: %d\n",
76 atomic_read(&net->use_count));
80 kmem_cache_free(net_cachep, net);
83 struct net *copy_net_ns(unsigned long flags, struct net *old_net)
85 struct net *new_net = NULL;
90 if (!(flags & CLONE_NEWNET))
94 new_net = net_alloc();
98 mutex_lock(&net_mutex);
99 err = setup_net(new_net);
104 list_add_tail(&new_net->list, &net_namespace_list);
109 mutex_unlock(&net_mutex);
114 new_net = ERR_PTR(err);
119 static void cleanup_net(struct work_struct *work)
121 struct pernet_operations *ops;
124 net = container_of(work, struct net, work);
126 mutex_lock(&net_mutex);
128 /* Don't let anyone else find us. */
130 list_del(&net->list);
133 /* Run all of the network namespace exit methods */
134 list_for_each_entry_reverse(ops, &pernet_list, list) {
139 mutex_unlock(&net_mutex);
141 /* Ensure there are no outstanding rcu callbacks using this
146 /* Finally it is safe to free my network namespace structure */
150 void __put_net(struct net *net)
152 /* Cleanup the network namespace in process context */
153 INIT_WORK(&net->work, cleanup_net);
154 queue_work(netns_wq, &net->work);
156 EXPORT_SYMBOL_GPL(__put_net);
159 struct net *copy_net_ns(unsigned long flags, struct net *old_net)
161 if (flags & CLONE_NEWNET)
162 return ERR_PTR(-EINVAL);
167 static int __init net_ns_init(void)
171 printk(KERN_INFO "net_namespace: %zd bytes\n", sizeof(struct net));
173 net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
177 /* Create workqueue for cleanup */
178 netns_wq = create_singlethread_workqueue("netns");
180 panic("Could not create netns workq");
183 mutex_lock(&net_mutex);
184 err = setup_net(&init_net);
187 list_add_tail(&init_net.list, &net_namespace_list);
190 mutex_unlock(&net_mutex);
192 panic("Could not setup the initial network namespace");
197 pure_initcall(net_ns_init);
200 static int register_pernet_operations(struct list_head *list,
201 struct pernet_operations *ops)
203 struct net *net, *undo_net;
206 list_add_tail(&ops->list, list);
209 error = ops->init(net);
217 /* If I have an error cleanup all namespaces I initialized */
218 list_del(&ops->list);
220 for_each_net(undo_net) {
230 static void unregister_pernet_operations(struct pernet_operations *ops)
234 list_del(&ops->list);
242 static int register_pernet_operations(struct list_head *list,
243 struct pernet_operations *ops)
245 if (ops->init == NULL)
247 return ops->init(&init_net);
250 static void unregister_pernet_operations(struct pernet_operations *ops)
253 ops->exit(&init_net);
257 static DEFINE_IDA(net_generic_ids);
260 * register_pernet_subsys - register a network namespace subsystem
261 * @ops: pernet operations structure for the subsystem
263 * Register a subsystem which has init and exit functions
264 * that are called when network namespaces are created and
265 * destroyed respectively.
267 * When registered all network namespace init functions are
268 * called for every existing network namespace. Allowing kernel
269 * modules to have a race free view of the set of network namespaces.
271 * When a new network namespace is created all of the init
272 * methods are called in the order in which they were registered.
274 * When a network namespace is destroyed all of the exit methods
275 * are called in the reverse of the order with which they were
278 int register_pernet_subsys(struct pernet_operations *ops)
281 mutex_lock(&net_mutex);
282 error = register_pernet_operations(first_device, ops);
283 mutex_unlock(&net_mutex);
286 EXPORT_SYMBOL_GPL(register_pernet_subsys);
289 * unregister_pernet_subsys - unregister a network namespace subsystem
290 * @ops: pernet operations structure to manipulate
292 * Remove the pernet operations structure from the list to be
293 * used when network namespaces are created or destroyed. In
294 * addition run the exit method for all existing network
297 void unregister_pernet_subsys(struct pernet_operations *module)
299 mutex_lock(&net_mutex);
300 unregister_pernet_operations(module);
301 mutex_unlock(&net_mutex);
303 EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
306 * register_pernet_device - register a network namespace device
307 * @ops: pernet operations structure for the subsystem
309 * Register a device 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_device(struct pernet_operations *ops)
327 mutex_lock(&net_mutex);
328 error = register_pernet_operations(&pernet_list, ops);
329 if (!error && (first_device == &pernet_list))
330 first_device = &ops->list;
331 mutex_unlock(&net_mutex);
334 EXPORT_SYMBOL_GPL(register_pernet_device);
336 int register_pernet_gen_device(int *id, struct pernet_operations *ops)
339 mutex_lock(&net_mutex);
341 error = ida_get_new_above(&net_generic_ids, 1, id);
343 if (error == -EAGAIN) {
344 ida_pre_get(&net_generic_ids, GFP_KERNEL);
349 error = register_pernet_operations(&pernet_list, ops);
351 ida_remove(&net_generic_ids, *id);
352 else if (first_device == &pernet_list)
353 first_device = &ops->list;
355 mutex_unlock(&net_mutex);
358 EXPORT_SYMBOL_GPL(register_pernet_gen_device);
361 * unregister_pernet_device - unregister a network namespace netdevice
362 * @ops: pernet operations structure to manipulate
364 * Remove the pernet operations structure from the list to be
365 * used when network namespaces are created or destroyed. In
366 * addition run the exit method for all existing network
369 void unregister_pernet_device(struct pernet_operations *ops)
371 mutex_lock(&net_mutex);
372 if (&ops->list == first_device)
373 first_device = first_device->next;
374 unregister_pernet_operations(ops);
375 mutex_unlock(&net_mutex);
377 EXPORT_SYMBOL_GPL(unregister_pernet_device);
379 void unregister_pernet_gen_device(int id, struct pernet_operations *ops)
381 mutex_lock(&net_mutex);
382 if (&ops->list == first_device)
383 first_device = first_device->next;
384 unregister_pernet_operations(ops);
385 ida_remove(&net_generic_ids, id);
386 mutex_unlock(&net_mutex);
388 EXPORT_SYMBOL_GPL(unregister_pernet_gen_device);