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 <net/net_namespace.h>
10 * Our network namespace constructor/destructor lists
13 static LIST_HEAD(pernet_list);
14 static struct list_head *first_device = &pernet_list;
15 static DEFINE_MUTEX(net_mutex);
17 static DEFINE_MUTEX(net_list_mutex);
18 LIST_HEAD(net_namespace_list);
20 static struct kmem_cache *net_cachep;
23 EXPORT_SYMBOL_GPL(init_net);
27 mutex_lock(&net_list_mutex);
32 mutex_unlock(&net_list_mutex);
35 static struct net *net_alloc(void)
37 return kmem_cache_alloc(net_cachep, GFP_KERNEL);
40 static void net_free(struct net *net)
45 if (unlikely(atomic_read(&net->use_count) != 0)) {
46 printk(KERN_EMERG "network namespace not free! Usage: %d\n",
47 atomic_read(&net->use_count));
51 kmem_cache_free(net_cachep, net);
54 static void cleanup_net(struct work_struct *work)
56 struct pernet_operations *ops;
57 struct list_head *ptr;
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_prev(ptr, &pernet_list) {
71 ops = list_entry(ptr, struct pernet_operations, list);
76 mutex_unlock(&net_mutex);
78 /* Ensure there are no outstanding rcu callbacks using this
83 /* Finally it is safe to free my network namespace structure */
88 void __put_net(struct net *net)
90 /* Cleanup the network namespace in process context */
91 INIT_WORK(&net->work, cleanup_net);
92 schedule_work(&net->work);
94 EXPORT_SYMBOL_GPL(__put_net);
97 * setup_net runs the initializers for the network namespace object.
99 static int setup_net(struct net *net)
101 /* Must be called with net_mutex held */
102 struct pernet_operations *ops;
103 struct list_head *ptr;
106 memset(net, 0, sizeof(struct net));
107 atomic_set(&net->count, 1);
108 atomic_set(&net->use_count, 0);
111 list_for_each(ptr, &pernet_list) {
112 ops = list_entry(ptr, struct pernet_operations, list);
114 error = ops->init(net);
122 /* Walk through the list backwards calling the exit functions
123 * for the pernet modules whose init functions did not fail.
125 for (ptr = ptr->prev; ptr != &pernet_list; ptr = ptr->prev) {
126 ops = list_entry(ptr, struct pernet_operations, list);
133 static int __init net_ns_init(void)
137 printk(KERN_INFO "net_namespace: %zd bytes\n", sizeof(struct net));
138 net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
141 mutex_lock(&net_mutex);
142 err = setup_net(&init_net);
145 list_add_tail(&init_net.list, &net_namespace_list);
148 mutex_unlock(&net_mutex);
150 panic("Could not setup the initial network namespace");
155 pure_initcall(net_ns_init);
157 static int register_pernet_operations(struct list_head *list,
158 struct pernet_operations *ops)
160 struct net *net, *undo_net;
164 list_add_tail(&ops->list, list);
167 error = ops->init(net);
176 /* If I have an error cleanup all namespaces I initialized */
177 list_del(&ops->list);
178 for_each_net(undo_net) {
188 static void unregister_pernet_operations(struct pernet_operations *ops)
192 list_del(&ops->list);
199 * register_pernet_subsys - register a network namespace subsystem
200 * @ops: pernet operations structure for the subsystem
202 * Register a subsystem which has init and exit functions
203 * that are called when network namespaces are created and
204 * destroyed respectively.
206 * When registered all network namespace init functions are
207 * called for every existing network namespace. Allowing kernel
208 * modules to have a race free view of the set of network namespaces.
210 * When a new network namespace is created all of the init
211 * methods are called in the order in which they were registered.
213 * When a network namespace is destroyed all of the exit methods
214 * are called in the reverse of the order with which they were
217 int register_pernet_subsys(struct pernet_operations *ops)
220 mutex_lock(&net_mutex);
221 error = register_pernet_operations(first_device, ops);
222 mutex_unlock(&net_mutex);
225 EXPORT_SYMBOL_GPL(register_pernet_subsys);
228 * unregister_pernet_subsys - unregister a network namespace subsystem
229 * @ops: pernet operations structure to manipulate
231 * Remove the pernet operations structure from the list to be
232 * used when network namespaces are created or destoryed. In
233 * addition run the exit method for all existing network
236 void unregister_pernet_subsys(struct pernet_operations *module)
238 mutex_lock(&net_mutex);
239 unregister_pernet_operations(module);
240 mutex_unlock(&net_mutex);
242 EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
245 * register_pernet_device - register a network namespace device
246 * @ops: pernet operations structure for the subsystem
248 * Register a device which has init and exit functions
249 * that are called when network namespaces are created and
250 * destroyed respectively.
252 * When registered all network namespace init functions are
253 * called for every existing network namespace. Allowing kernel
254 * modules to have a race free view of the set of network namespaces.
256 * When a new network namespace is created all of the init
257 * methods are called in the order in which they were registered.
259 * When a network namespace is destroyed all of the exit methods
260 * are called in the reverse of the order with which they were
263 int register_pernet_device(struct pernet_operations *ops)
266 mutex_lock(&net_mutex);
267 error = register_pernet_operations(&pernet_list, ops);
268 if (!error && (first_device == &pernet_list))
269 first_device = &ops->list;
270 mutex_unlock(&net_mutex);
273 EXPORT_SYMBOL_GPL(register_pernet_device);
276 * unregister_pernet_device - unregister a network namespace netdevice
277 * @ops: pernet operations structure to manipulate
279 * Remove the pernet operations structure from the list to be
280 * used when network namespaces are created or destoryed. In
281 * addition run the exit method for all existing network
284 void unregister_pernet_device(struct pernet_operations *ops)
286 mutex_lock(&net_mutex);
287 if (&ops->list == first_device)
288 first_device = first_device->next;
289 unregister_pernet_operations(ops);
290 mutex_unlock(&net_mutex);
292 EXPORT_SYMBOL_GPL(unregister_pernet_device);