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);
23 EXPORT_SYMBOL(init_net);
25 #define INITIAL_NET_GEN_PTRS 13 /* +1 for len +2 for rcu_head */
28 * setup_net runs the initializers for the network namespace object.
30 static __net_init int setup_net(struct net *net)
32 /* Must be called with net_mutex held */
33 struct pernet_operations *ops;
35 struct net_generic *ng;
37 atomic_set(&net->count, 1);
38 atomic_set(&net->use_count, 0);
41 ng = kzalloc(sizeof(struct net_generic) +
42 INITIAL_NET_GEN_PTRS * sizeof(void *), GFP_KERNEL);
46 ng->len = INITIAL_NET_GEN_PTRS;
47 INIT_RCU_HEAD(&ng->rcu);
48 rcu_assign_pointer(net->gen, ng);
51 list_for_each_entry(ops, &pernet_list, list) {
53 error = ops->init(net);
62 /* Walk through the list backwards calling the exit functions
63 * for the pernet modules whose init functions did not fail.
65 list_for_each_entry_continue_reverse(ops, &pernet_list, list) {
76 static struct kmem_cache *net_cachep;
77 static struct workqueue_struct *netns_wq;
79 static struct net *net_alloc(void)
81 return kmem_cache_zalloc(net_cachep, GFP_KERNEL);
84 static void net_free(struct net *net)
89 if (unlikely(atomic_read(&net->use_count) != 0)) {
90 printk(KERN_EMERG "network namespace not free! Usage: %d\n",
91 atomic_read(&net->use_count));
95 kmem_cache_free(net_cachep, net);
98 struct net *copy_net_ns(unsigned long flags, struct net *old_net)
100 struct net *new_net = NULL;
105 if (!(flags & CLONE_NEWNET))
109 new_net = net_alloc();
113 mutex_lock(&net_mutex);
114 err = setup_net(new_net);
119 list_add_tail(&new_net->list, &net_namespace_list);
124 mutex_unlock(&net_mutex);
129 new_net = ERR_PTR(err);
134 static void cleanup_net(struct work_struct *work)
136 struct pernet_operations *ops;
139 net = container_of(work, struct net, work);
141 mutex_lock(&net_mutex);
143 /* Don't let anyone else find us. */
145 list_del(&net->list);
148 /* Run all of the network namespace exit methods */
149 list_for_each_entry_reverse(ops, &pernet_list, list) {
154 mutex_unlock(&net_mutex);
156 /* Ensure there are no outstanding rcu callbacks using this
161 /* Finally it is safe to free my network namespace structure */
165 void __put_net(struct net *net)
167 /* Cleanup the network namespace in process context */
168 INIT_WORK(&net->work, cleanup_net);
169 queue_work(netns_wq, &net->work);
171 EXPORT_SYMBOL_GPL(__put_net);
174 struct net *copy_net_ns(unsigned long flags, struct net *old_net)
176 if (flags & CLONE_NEWNET)
177 return ERR_PTR(-EINVAL);
182 static int __init net_ns_init(void)
186 printk(KERN_INFO "net_namespace: %zd bytes\n", sizeof(struct net));
188 net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
192 /* Create workqueue for cleanup */
193 netns_wq = create_singlethread_workqueue("netns");
195 panic("Could not create netns workq");
198 mutex_lock(&net_mutex);
199 err = setup_net(&init_net);
202 list_add_tail(&init_net.list, &net_namespace_list);
205 mutex_unlock(&net_mutex);
207 panic("Could not setup the initial network namespace");
212 pure_initcall(net_ns_init);
215 static int register_pernet_operations(struct list_head *list,
216 struct pernet_operations *ops)
218 struct net *net, *undo_net;
221 list_add_tail(&ops->list, list);
224 error = ops->init(net);
232 /* If I have an error cleanup all namespaces I initialized */
233 list_del(&ops->list);
235 for_each_net(undo_net) {
245 static void unregister_pernet_operations(struct pernet_operations *ops)
249 list_del(&ops->list);
257 static int register_pernet_operations(struct list_head *list,
258 struct pernet_operations *ops)
260 if (ops->init == NULL)
262 return ops->init(&init_net);
265 static void unregister_pernet_operations(struct pernet_operations *ops)
268 ops->exit(&init_net);
272 static DEFINE_IDA(net_generic_ids);
275 * register_pernet_subsys - register a network namespace subsystem
276 * @ops: pernet operations structure for the subsystem
278 * Register a subsystem which has init and exit functions
279 * that are called when network namespaces are created and
280 * destroyed respectively.
282 * When registered all network namespace init functions are
283 * called for every existing network namespace. Allowing kernel
284 * modules to have a race free view of the set of network namespaces.
286 * When a new network namespace is created all of the init
287 * methods are called in the order in which they were registered.
289 * When a network namespace is destroyed all of the exit methods
290 * are called in the reverse of the order with which they were
293 int register_pernet_subsys(struct pernet_operations *ops)
296 mutex_lock(&net_mutex);
297 error = register_pernet_operations(first_device, ops);
298 mutex_unlock(&net_mutex);
301 EXPORT_SYMBOL_GPL(register_pernet_subsys);
304 * unregister_pernet_subsys - unregister a network namespace subsystem
305 * @ops: pernet operations structure to manipulate
307 * Remove the pernet operations structure from the list to be
308 * used when network namespaces are created or destroyed. In
309 * addition run the exit method for all existing network
312 void unregister_pernet_subsys(struct pernet_operations *module)
314 mutex_lock(&net_mutex);
315 unregister_pernet_operations(module);
316 mutex_unlock(&net_mutex);
318 EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
321 * register_pernet_device - register a network namespace device
322 * @ops: pernet operations structure for the subsystem
324 * Register a device which has init and exit functions
325 * that are called when network namespaces are created and
326 * destroyed respectively.
328 * When registered all network namespace init functions are
329 * called for every existing network namespace. Allowing kernel
330 * modules to have a race free view of the set of network namespaces.
332 * When a new network namespace is created all of the init
333 * methods are called in the order in which they were registered.
335 * When a network namespace is destroyed all of the exit methods
336 * are called in the reverse of the order with which they were
339 int register_pernet_device(struct pernet_operations *ops)
342 mutex_lock(&net_mutex);
343 error = register_pernet_operations(&pernet_list, ops);
344 if (!error && (first_device == &pernet_list))
345 first_device = &ops->list;
346 mutex_unlock(&net_mutex);
349 EXPORT_SYMBOL_GPL(register_pernet_device);
351 int register_pernet_gen_device(int *id, struct pernet_operations *ops)
354 mutex_lock(&net_mutex);
356 error = ida_get_new_above(&net_generic_ids, 1, id);
358 if (error == -EAGAIN) {
359 ida_pre_get(&net_generic_ids, GFP_KERNEL);
364 error = register_pernet_operations(&pernet_list, ops);
366 ida_remove(&net_generic_ids, *id);
367 else if (first_device == &pernet_list)
368 first_device = &ops->list;
370 mutex_unlock(&net_mutex);
373 EXPORT_SYMBOL_GPL(register_pernet_gen_device);
376 * unregister_pernet_device - unregister a network namespace netdevice
377 * @ops: pernet operations structure to manipulate
379 * Remove the pernet operations structure from the list to be
380 * used when network namespaces are created or destroyed. In
381 * addition run the exit method for all existing network
384 void unregister_pernet_device(struct pernet_operations *ops)
386 mutex_lock(&net_mutex);
387 if (&ops->list == first_device)
388 first_device = first_device->next;
389 unregister_pernet_operations(ops);
390 mutex_unlock(&net_mutex);
392 EXPORT_SYMBOL_GPL(unregister_pernet_device);
394 void unregister_pernet_gen_device(int id, struct pernet_operations *ops)
396 mutex_lock(&net_mutex);
397 if (&ops->list == first_device)
398 first_device = first_device->next;
399 unregister_pernet_operations(ops);
400 ida_remove(&net_generic_ids, id);
401 mutex_unlock(&net_mutex);
403 EXPORT_SYMBOL_GPL(unregister_pernet_gen_device);
405 static void net_generic_release(struct rcu_head *rcu)
407 struct net_generic *ng;
409 ng = container_of(rcu, struct net_generic, rcu);
413 int net_assign_generic(struct net *net, int id, void *data)
415 struct net_generic *ng, *old_ng;
417 BUG_ON(!mutex_is_locked(&net_mutex));
420 ng = old_ng = net->gen;
421 if (old_ng->len >= id)
424 ng = kzalloc(sizeof(struct net_generic) +
425 id * sizeof(void *), GFP_KERNEL);
430 * Some synchronisation notes:
432 * The net_generic explores the net->gen array inside rcu
433 * read section. Besides once set the net->gen->ptr[x]
434 * pointer never changes (see rules in netns/generic.h).
436 * That said, we simply duplicate this array and schedule
437 * the old copy for kfree after a grace period.
441 INIT_RCU_HEAD(&ng->rcu);
442 memcpy(&ng->ptr, &old_ng->ptr, old_ng->len);
444 rcu_assign_pointer(net->gen, ng);
445 call_rcu(&old_ng->rcu, net_generic_release);
447 ng->ptr[id - 1] = data;
450 EXPORT_SYMBOL_GPL(net_assign_generic);