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);
36 static struct net *net_alloc(void)
38 return kmem_cache_alloc(net_cachep, GFP_KERNEL);
42 static void net_free(struct net *net)
47 if (unlikely(atomic_read(&net->use_count) != 0)) {
48 printk(KERN_EMERG "network namespace not free! Usage: %d\n",
49 atomic_read(&net->use_count));
53 kmem_cache_free(net_cachep, net);
56 static void cleanup_net(struct work_struct *work)
58 struct pernet_operations *ops;
61 net = container_of(work, struct net, work);
63 mutex_lock(&net_mutex);
65 /* Don't let anyone else find us. */
70 /* Run all of the network namespace exit methods */
71 list_for_each_entry_reverse(ops, &pernet_list, 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;
105 memset(net, 0, sizeof(struct net));
106 atomic_set(&net->count, 1);
107 atomic_set(&net->use_count, 0);
110 list_for_each_entry(ops, &pernet_list, list) {
112 error = ops->init(net);
121 /* Walk through the list backwards calling the exit functions
122 * for the pernet modules whose init functions did not fail.
124 list_for_each_entry_continue_reverse(ops, &pernet_list, list) {
131 static int __init net_ns_init(void)
135 printk(KERN_INFO "net_namespace: %zd bytes\n", sizeof(struct net));
136 net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
139 mutex_lock(&net_mutex);
140 err = setup_net(&init_net);
143 list_add_tail(&init_net.list, &net_namespace_list);
146 mutex_unlock(&net_mutex);
148 panic("Could not setup the initial network namespace");
153 pure_initcall(net_ns_init);
155 static int register_pernet_operations(struct list_head *list,
156 struct pernet_operations *ops)
158 struct net *net, *undo_net;
162 list_add_tail(&ops->list, list);
165 error = ops->init(net);
174 /* If I have an error cleanup all namespaces I initialized */
175 list_del(&ops->list);
176 for_each_net(undo_net) {
186 static void unregister_pernet_operations(struct pernet_operations *ops)
190 list_del(&ops->list);
197 * register_pernet_subsys - register a network namespace subsystem
198 * @ops: pernet operations structure for the subsystem
200 * Register a subsystem which has init and exit functions
201 * that are called when network namespaces are created and
202 * destroyed respectively.
204 * When registered all network namespace init functions are
205 * called for every existing network namespace. Allowing kernel
206 * modules to have a race free view of the set of network namespaces.
208 * When a new network namespace is created all of the init
209 * methods are called in the order in which they were registered.
211 * When a network namespace is destroyed all of the exit methods
212 * are called in the reverse of the order with which they were
215 int register_pernet_subsys(struct pernet_operations *ops)
218 mutex_lock(&net_mutex);
219 error = register_pernet_operations(first_device, ops);
220 mutex_unlock(&net_mutex);
223 EXPORT_SYMBOL_GPL(register_pernet_subsys);
226 * unregister_pernet_subsys - unregister a network namespace subsystem
227 * @ops: pernet operations structure to manipulate
229 * Remove the pernet operations structure from the list to be
230 * used when network namespaces are created or destoryed. In
231 * addition run the exit method for all existing network
234 void unregister_pernet_subsys(struct pernet_operations *module)
236 mutex_lock(&net_mutex);
237 unregister_pernet_operations(module);
238 mutex_unlock(&net_mutex);
240 EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
243 * register_pernet_device - register a network namespace device
244 * @ops: pernet operations structure for the subsystem
246 * Register a device which has init and exit functions
247 * that are called when network namespaces are created and
248 * destroyed respectively.
250 * When registered all network namespace init functions are
251 * called for every existing network namespace. Allowing kernel
252 * modules to have a race free view of the set of network namespaces.
254 * When a new network namespace is created all of the init
255 * methods are called in the order in which they were registered.
257 * When a network namespace is destroyed all of the exit methods
258 * are called in the reverse of the order with which they were
261 int register_pernet_device(struct pernet_operations *ops)
264 mutex_lock(&net_mutex);
265 error = register_pernet_operations(&pernet_list, ops);
266 if (!error && (first_device == &pernet_list))
267 first_device = &ops->list;
268 mutex_unlock(&net_mutex);
271 EXPORT_SYMBOL_GPL(register_pernet_device);
274 * unregister_pernet_device - unregister a network namespace netdevice
275 * @ops: pernet operations structure to manipulate
277 * Remove the pernet operations structure from the list to be
278 * used when network namespaces are created or destoryed. In
279 * addition run the exit method for all existing network
282 void unregister_pernet_device(struct pernet_operations *ops)
284 mutex_lock(&net_mutex);
285 if (&ops->list == first_device)
286 first_device = first_device->next;
287 unregister_pernet_operations(ops);
288 mutex_unlock(&net_mutex);
290 EXPORT_SYMBOL_GPL(unregister_pernet_device);