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 LIST_HEAD(net_namespace_list
);
21 EXPORT_SYMBOL_GPL(init_net
);
24 * setup_net runs the initializers for the network namespace object.
26 static __net_init
int setup_net(struct net
*net
)
28 /* Must be called with net_mutex held */
29 struct pernet_operations
*ops
;
32 atomic_set(&net
->count
, 1);
33 atomic_set(&net
->use_count
, 0);
36 list_for_each_entry(ops
, &pernet_list
, list
) {
38 error
= ops
->init(net
);
47 /* Walk through the list backwards calling the exit functions
48 * for the pernet modules whose init functions did not fail.
50 list_for_each_entry_continue_reverse(ops
, &pernet_list
, list
) {
60 static struct kmem_cache
*net_cachep
;
62 static struct net
*net_alloc(void)
64 return kmem_cache_zalloc(net_cachep
, GFP_KERNEL
);
67 static void net_free(struct net
*net
)
72 if (unlikely(atomic_read(&net
->use_count
) != 0)) {
73 printk(KERN_EMERG
"network namespace not free! Usage: %d\n",
74 atomic_read(&net
->use_count
));
78 kmem_cache_free(net_cachep
, net
);
81 struct net
*copy_net_ns(unsigned long flags
, struct net
*old_net
)
83 struct net
*new_net
= NULL
;
88 if (!(flags
& CLONE_NEWNET
))
92 new_net
= net_alloc();
96 mutex_lock(&net_mutex
);
97 err
= setup_net(new_net
);
102 list_add_tail(&new_net
->list
, &net_namespace_list
);
107 mutex_unlock(&net_mutex
);
112 new_net
= ERR_PTR(err
);
117 static void cleanup_net(struct work_struct
*work
)
119 struct pernet_operations
*ops
;
122 net
= container_of(work
, struct net
, work
);
124 mutex_lock(&net_mutex
);
126 /* Don't let anyone else find us. */
128 list_del(&net
->list
);
131 /* Run all of the network namespace exit methods */
132 list_for_each_entry_reverse(ops
, &pernet_list
, list
) {
137 mutex_unlock(&net_mutex
);
139 /* Ensure there are no outstanding rcu callbacks using this
144 /* Finally it is safe to free my network namespace structure */
148 void __put_net(struct net
*net
)
150 /* Cleanup the network namespace in process context */
151 INIT_WORK(&net
->work
, cleanup_net
);
152 schedule_work(&net
->work
);
154 EXPORT_SYMBOL_GPL(__put_net
);
157 struct net
*copy_net_ns(unsigned long flags
, struct net
*old_net
)
159 if (flags
& CLONE_NEWNET
)
160 return ERR_PTR(-EINVAL
);
165 static int __init
net_ns_init(void)
169 printk(KERN_INFO
"net_namespace: %zd bytes\n", sizeof(struct net
));
171 net_cachep
= kmem_cache_create("net_namespace", sizeof(struct net
),
175 mutex_lock(&net_mutex
);
176 err
= setup_net(&init_net
);
179 list_add_tail(&init_net
.list
, &net_namespace_list
);
182 mutex_unlock(&net_mutex
);
184 panic("Could not setup the initial network namespace");
189 pure_initcall(net_ns_init
);
192 static int register_pernet_operations(struct list_head
*list
,
193 struct pernet_operations
*ops
)
195 struct net
*net
, *undo_net
;
198 list_add_tail(&ops
->list
, list
);
201 error
= ops
->init(net
);
209 /* If I have an error cleanup all namespaces I initialized */
210 list_del(&ops
->list
);
212 for_each_net(undo_net
) {
222 static void unregister_pernet_operations(struct pernet_operations
*ops
)
226 list_del(&ops
->list
);
234 static int register_pernet_operations(struct list_head
*list
,
235 struct pernet_operations
*ops
)
237 if (ops
->init
== NULL
)
239 return ops
->init(&init_net
);
242 static void unregister_pernet_operations(struct pernet_operations
*ops
)
245 ops
->exit(&init_net
);
250 * register_pernet_subsys - register a network namespace subsystem
251 * @ops: pernet operations structure for the subsystem
253 * Register a subsystem which has init and exit functions
254 * that are called when network namespaces are created and
255 * destroyed respectively.
257 * When registered all network namespace init functions are
258 * called for every existing network namespace. Allowing kernel
259 * modules to have a race free view of the set of network namespaces.
261 * When a new network namespace is created all of the init
262 * methods are called in the order in which they were registered.
264 * When a network namespace is destroyed all of the exit methods
265 * are called in the reverse of the order with which they were
268 int register_pernet_subsys(struct pernet_operations
*ops
)
271 mutex_lock(&net_mutex
);
272 error
= register_pernet_operations(first_device
, ops
);
273 mutex_unlock(&net_mutex
);
276 EXPORT_SYMBOL_GPL(register_pernet_subsys
);
279 * unregister_pernet_subsys - unregister a network namespace subsystem
280 * @ops: pernet operations structure to manipulate
282 * Remove the pernet operations structure from the list to be
283 * used when network namespaces are created or destoryed. In
284 * addition run the exit method for all existing network
287 void unregister_pernet_subsys(struct pernet_operations
*module
)
289 mutex_lock(&net_mutex
);
290 unregister_pernet_operations(module
);
291 mutex_unlock(&net_mutex
);
293 EXPORT_SYMBOL_GPL(unregister_pernet_subsys
);
296 * register_pernet_device - register a network namespace device
297 * @ops: pernet operations structure for the subsystem
299 * Register a device which has init and exit functions
300 * that are called when network namespaces are created and
301 * destroyed respectively.
303 * When registered all network namespace init functions are
304 * called for every existing network namespace. Allowing kernel
305 * modules to have a race free view of the set of network namespaces.
307 * When a new network namespace is created all of the init
308 * methods are called in the order in which they were registered.
310 * When a network namespace is destroyed all of the exit methods
311 * are called in the reverse of the order with which they were
314 int register_pernet_device(struct pernet_operations
*ops
)
317 mutex_lock(&net_mutex
);
318 error
= register_pernet_operations(&pernet_list
, ops
);
319 if (!error
&& (first_device
== &pernet_list
))
320 first_device
= &ops
->list
;
321 mutex_unlock(&net_mutex
);
324 EXPORT_SYMBOL_GPL(register_pernet_device
);
327 * unregister_pernet_device - unregister a network namespace netdevice
328 * @ops: pernet operations structure to manipulate
330 * Remove the pernet operations structure from the list to be
331 * used when network namespaces are created or destoryed. In
332 * addition run the exit method for all existing network
335 void unregister_pernet_device(struct pernet_operations
*ops
)
337 mutex_lock(&net_mutex
);
338 if (&ops
->list
== first_device
)
339 first_device
= first_device
->next
;
340 unregister_pernet_operations(ops
);
341 mutex_unlock(&net_mutex
);
343 EXPORT_SYMBOL_GPL(unregister_pernet_device
);