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>
8 #include <net/net_namespace.h>
11 * Our network namespace constructor/destructor lists
14 static LIST_HEAD(pernet_list);
15 static struct list_head *first_device = &pernet_list;
16 static DEFINE_MUTEX(net_mutex);
18 static DEFINE_MUTEX(net_list_mutex);
19 LIST_HEAD(net_namespace_list);
21 static struct kmem_cache *net_cachep;
24 EXPORT_SYMBOL_GPL(init_net);
28 mutex_lock(&net_list_mutex);
33 mutex_unlock(&net_list_mutex);
36 static struct net *net_alloc(void)
38 return kmem_cache_alloc(net_cachep, GFP_KERNEL);
41 static void net_free(struct net *net)
46 if (unlikely(atomic_read(&net->use_count) != 0)) {
47 printk(KERN_EMERG "network namespace not free! Usage: %d\n",
48 atomic_read(&net->use_count));
52 kmem_cache_free(net_cachep, net);
55 static void cleanup_net(struct work_struct *work)
57 struct pernet_operations *ops;
60 net = container_of(work, struct net, work);
62 mutex_lock(&net_mutex);
64 /* Don't let anyone else find us. */
69 /* Run all of the network namespace exit methods */
70 list_for_each_entry_reverse(ops, &pernet_list, list) {
75 mutex_unlock(&net_mutex);
77 /* Ensure there are no outstanding rcu callbacks using this
82 /* Finally it is safe to free my network namespace structure */
87 void __put_net(struct net *net)
89 /* Cleanup the network namespace in process context */
90 INIT_WORK(&net->work, cleanup_net);
91 schedule_work(&net->work);
93 EXPORT_SYMBOL_GPL(__put_net);
96 * setup_net runs the initializers for the network namespace object.
98 static int setup_net(struct net *net)
100 /* Must be called with net_mutex held */
101 struct pernet_operations *ops;
104 memset(net, 0, sizeof(struct net));
105 atomic_set(&net->count, 1);
106 atomic_set(&net->use_count, 0);
109 list_for_each_entry(ops, &pernet_list, list) {
111 error = ops->init(net);
120 /* Walk through the list backwards calling the exit functions
121 * for the pernet modules whose init functions did not fail.
123 list_for_each_entry_continue_reverse(ops, &pernet_list, list) {
130 struct net *copy_net_ns(unsigned long flags, struct net *old_net)
132 struct net *new_net = NULL;
137 if (!(flags & CLONE_NEWNET))
140 #ifndef CONFIG_NET_NS
141 return ERR_PTR(-EINVAL);
145 new_net = net_alloc();
149 mutex_lock(&net_mutex);
150 err = setup_net(new_net);
155 list_add_tail(&new_net->list, &net_namespace_list);
160 mutex_unlock(&net_mutex);
165 new_net = ERR_PTR(err);
170 static int __init net_ns_init(void)
174 printk(KERN_INFO "net_namespace: %zd bytes\n", sizeof(struct net));
175 net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
178 mutex_lock(&net_mutex);
179 err = setup_net(&init_net);
182 list_add_tail(&init_net.list, &net_namespace_list);
185 mutex_unlock(&net_mutex);
187 panic("Could not setup the initial network namespace");
192 pure_initcall(net_ns_init);
194 static int register_pernet_operations(struct list_head *list,
195 struct pernet_operations *ops)
197 struct net *net, *undo_net;
201 list_add_tail(&ops->list, list);
204 error = ops->init(net);
213 /* If I have an error cleanup all namespaces I initialized */
214 list_del(&ops->list);
215 for_each_net(undo_net) {
225 static void unregister_pernet_operations(struct pernet_operations *ops)
229 list_del(&ops->list);
236 * register_pernet_subsys - register a network namespace subsystem
237 * @ops: pernet operations structure for the subsystem
239 * Register a subsystem which has init and exit functions
240 * that are called when network namespaces are created and
241 * destroyed respectively.
243 * When registered all network namespace init functions are
244 * called for every existing network namespace. Allowing kernel
245 * modules to have a race free view of the set of network namespaces.
247 * When a new network namespace is created all of the init
248 * methods are called in the order in which they were registered.
250 * When a network namespace is destroyed all of the exit methods
251 * are called in the reverse of the order with which they were
254 int register_pernet_subsys(struct pernet_operations *ops)
257 mutex_lock(&net_mutex);
258 error = register_pernet_operations(first_device, ops);
259 mutex_unlock(&net_mutex);
262 EXPORT_SYMBOL_GPL(register_pernet_subsys);
265 * unregister_pernet_subsys - unregister a network namespace subsystem
266 * @ops: pernet operations structure to manipulate
268 * Remove the pernet operations structure from the list to be
269 * used when network namespaces are created or destoryed. In
270 * addition run the exit method for all existing network
273 void unregister_pernet_subsys(struct pernet_operations *module)
275 mutex_lock(&net_mutex);
276 unregister_pernet_operations(module);
277 mutex_unlock(&net_mutex);
279 EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
282 * register_pernet_device - register a network namespace device
283 * @ops: pernet operations structure for the subsystem
285 * Register a device which has init and exit functions
286 * that are called when network namespaces are created and
287 * destroyed respectively.
289 * When registered all network namespace init functions are
290 * called for every existing network namespace. Allowing kernel
291 * modules to have a race free view of the set of network namespaces.
293 * When a new network namespace is created all of the init
294 * methods are called in the order in which they were registered.
296 * When a network namespace is destroyed all of the exit methods
297 * are called in the reverse of the order with which they were
300 int register_pernet_device(struct pernet_operations *ops)
303 mutex_lock(&net_mutex);
304 error = register_pernet_operations(&pernet_list, ops);
305 if (!error && (first_device == &pernet_list))
306 first_device = &ops->list;
307 mutex_unlock(&net_mutex);
310 EXPORT_SYMBOL_GPL(register_pernet_device);
313 * unregister_pernet_device - unregister a network namespace netdevice
314 * @ops: pernet operations structure to manipulate
316 * Remove the pernet operations structure from the list to be
317 * used when network namespaces are created or destoryed. In
318 * addition run the exit method for all existing network
321 void unregister_pernet_device(struct pernet_operations *ops)
323 mutex_lock(&net_mutex);
324 if (&ops->list == first_device)
325 first_device = first_device->next;
326 unregister_pernet_operations(ops);
327 mutex_unlock(&net_mutex);
329 EXPORT_SYMBOL_GPL(unregister_pernet_device);