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;
59 struct list_head *ptr;
62 net = container_of(work, struct net, work);
64 mutex_lock(&net_mutex);
66 /* Don't let anyone else find us. */
71 /* Run all of the network namespace exit methods */
72 list_for_each_prev(ptr, &pernet_list) {
73 ops = list_entry(ptr, struct pernet_operations, list);
78 mutex_unlock(&net_mutex);
80 /* Ensure there are no outstanding rcu callbacks using this
85 /* Finally it is safe to free my network namespace structure */
90 void __put_net(struct net *net)
92 /* Cleanup the network namespace in process context */
93 INIT_WORK(&net->work, cleanup_net);
94 schedule_work(&net->work);
96 EXPORT_SYMBOL_GPL(__put_net);
99 * setup_net runs the initializers for the network namespace object.
101 static int setup_net(struct net *net)
103 /* Must be called with net_mutex held */
104 struct pernet_operations *ops;
105 struct list_head *ptr;
108 memset(net, 0, sizeof(struct net));
109 atomic_set(&net->count, 1);
110 atomic_set(&net->use_count, 0);
113 list_for_each(ptr, &pernet_list) {
114 ops = list_entry(ptr, struct pernet_operations, list);
116 error = ops->init(net);
124 /* Walk through the list backwards calling the exit functions
125 * for the pernet modules whose init functions did not fail.
127 for (ptr = ptr->prev; ptr != &pernet_list; ptr = ptr->prev) {
128 ops = list_entry(ptr, struct pernet_operations, list);
135 static int __init net_ns_init(void)
139 printk(KERN_INFO "net_namespace: %zd bytes\n", sizeof(struct net));
140 net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
143 mutex_lock(&net_mutex);
144 err = setup_net(&init_net);
147 list_add_tail(&init_net.list, &net_namespace_list);
150 mutex_unlock(&net_mutex);
152 panic("Could not setup the initial network namespace");
157 pure_initcall(net_ns_init);
159 static int register_pernet_operations(struct list_head *list,
160 struct pernet_operations *ops)
162 struct net *net, *undo_net;
166 list_add_tail(&ops->list, list);
169 error = ops->init(net);
178 /* If I have an error cleanup all namespaces I initialized */
179 list_del(&ops->list);
180 for_each_net(undo_net) {
190 static void unregister_pernet_operations(struct pernet_operations *ops)
194 list_del(&ops->list);
201 * register_pernet_subsys - register a network namespace subsystem
202 * @ops: pernet operations structure for the subsystem
204 * Register a subsystem which has init and exit functions
205 * that are called when network namespaces are created and
206 * destroyed respectively.
208 * When registered all network namespace init functions are
209 * called for every existing network namespace. Allowing kernel
210 * modules to have a race free view of the set of network namespaces.
212 * When a new network namespace is created all of the init
213 * methods are called in the order in which they were registered.
215 * When a network namespace is destroyed all of the exit methods
216 * are called in the reverse of the order with which they were
219 int register_pernet_subsys(struct pernet_operations *ops)
222 mutex_lock(&net_mutex);
223 error = register_pernet_operations(first_device, ops);
224 mutex_unlock(&net_mutex);
227 EXPORT_SYMBOL_GPL(register_pernet_subsys);
230 * unregister_pernet_subsys - unregister a network namespace subsystem
231 * @ops: pernet operations structure to manipulate
233 * Remove the pernet operations structure from the list to be
234 * used when network namespaces are created or destoryed. In
235 * addition run the exit method for all existing network
238 void unregister_pernet_subsys(struct pernet_operations *module)
240 mutex_lock(&net_mutex);
241 unregister_pernet_operations(module);
242 mutex_unlock(&net_mutex);
244 EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
247 * register_pernet_device - register a network namespace device
248 * @ops: pernet operations structure for the subsystem
250 * Register a device which has init and exit functions
251 * that are called when network namespaces are created and
252 * destroyed respectively.
254 * When registered all network namespace init functions are
255 * called for every existing network namespace. Allowing kernel
256 * modules to have a race free view of the set of network namespaces.
258 * When a new network namespace is created all of the init
259 * methods are called in the order in which they were registered.
261 * When a network namespace is destroyed all of the exit methods
262 * are called in the reverse of the order with which they were
265 int register_pernet_device(struct pernet_operations *ops)
268 mutex_lock(&net_mutex);
269 error = register_pernet_operations(&pernet_list, ops);
270 if (!error && (first_device == &pernet_list))
271 first_device = &ops->list;
272 mutex_unlock(&net_mutex);
275 EXPORT_SYMBOL_GPL(register_pernet_device);
278 * unregister_pernet_device - unregister a network namespace netdevice
279 * @ops: pernet operations structure to manipulate
281 * Remove the pernet operations structure from the list to be
282 * used when network namespaces are created or destoryed. In
283 * addition run the exit method for all existing network
286 void unregister_pernet_device(struct pernet_operations *ops)
288 mutex_lock(&net_mutex);
289 if (&ops->list == first_device)
290 first_device = first_device->next;
291 unregister_pernet_operations(ops);
292 mutex_unlock(&net_mutex);
294 EXPORT_SYMBOL_GPL(unregister_pernet_device);