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>
9 #include <net/net_namespace.h>
10 #include <net/netns/generic.h>
13 * Our network namespace constructor/destructor lists
16 static LIST_HEAD(pernet_list
);
17 static struct list_head
*first_device
= &pernet_list
;
18 static DEFINE_MUTEX(net_mutex
);
20 LIST_HEAD(net_namespace_list
);
23 EXPORT_SYMBOL(init_net
);
25 #define INITIAL_NET_GEN_PTRS 13 /* +1 for len +2 for rcu_head */
28 * setup_net runs the initializers for the network namespace object.
30 static __net_init
int setup_net(struct net
*net
)
32 /* Must be called with net_mutex held */
33 struct pernet_operations
*ops
;
35 struct net_generic
*ng
;
37 atomic_set(&net
->count
, 1);
38 #ifdef NETNS_REFCNT_DEBUG
39 atomic_set(&net
->use_count
, 0);
43 ng
= kzalloc(sizeof(struct net_generic
) +
44 INITIAL_NET_GEN_PTRS
* sizeof(void *), GFP_KERNEL
);
48 ng
->len
= INITIAL_NET_GEN_PTRS
;
49 INIT_RCU_HEAD(&ng
->rcu
);
50 rcu_assign_pointer(net
->gen
, ng
);
53 list_for_each_entry(ops
, &pernet_list
, list
) {
55 error
= ops
->init(net
);
64 /* Walk through the list backwards calling the exit functions
65 * for the pernet modules whose init functions did not fail.
67 list_for_each_entry_continue_reverse(ops
, &pernet_list
, list
) {
78 static struct kmem_cache
*net_cachep
;
79 static struct workqueue_struct
*netns_wq
;
81 static struct net
*net_alloc(void)
83 return kmem_cache_zalloc(net_cachep
, GFP_KERNEL
);
86 static void net_free(struct net
*net
)
91 #ifdef NETNS_REFCNT_DEBUG
92 if (unlikely(atomic_read(&net
->use_count
) != 0)) {
93 printk(KERN_EMERG
"network namespace not free! Usage: %d\n",
94 atomic_read(&net
->use_count
));
99 kmem_cache_free(net_cachep
, net
);
102 struct net
*copy_net_ns(unsigned long flags
, struct net
*old_net
)
104 struct net
*new_net
= NULL
;
109 if (!(flags
& CLONE_NEWNET
))
113 new_net
= net_alloc();
117 mutex_lock(&net_mutex
);
118 err
= setup_net(new_net
);
123 list_add_tail(&new_net
->list
, &net_namespace_list
);
128 mutex_unlock(&net_mutex
);
133 new_net
= ERR_PTR(err
);
138 static void cleanup_net(struct work_struct
*work
)
140 struct pernet_operations
*ops
;
143 net
= container_of(work
, struct net
, work
);
145 mutex_lock(&net_mutex
);
147 /* Don't let anyone else find us. */
149 list_del(&net
->list
);
152 /* Run all of the network namespace exit methods */
153 list_for_each_entry_reverse(ops
, &pernet_list
, list
) {
158 mutex_unlock(&net_mutex
);
160 /* Ensure there are no outstanding rcu callbacks using this
165 /* Finally it is safe to free my network namespace structure */
169 void __put_net(struct net
*net
)
171 /* Cleanup the network namespace in process context */
172 INIT_WORK(&net
->work
, cleanup_net
);
173 queue_work(netns_wq
, &net
->work
);
175 EXPORT_SYMBOL_GPL(__put_net
);
178 struct net
*copy_net_ns(unsigned long flags
, struct net
*old_net
)
180 if (flags
& CLONE_NEWNET
)
181 return ERR_PTR(-EINVAL
);
186 static int __init
net_ns_init(void)
190 printk(KERN_INFO
"net_namespace: %zd bytes\n", sizeof(struct net
));
192 net_cachep
= kmem_cache_create("net_namespace", sizeof(struct net
),
196 /* Create workqueue for cleanup */
197 netns_wq
= create_singlethread_workqueue("netns");
199 panic("Could not create netns workq");
202 mutex_lock(&net_mutex
);
203 err
= setup_net(&init_net
);
206 list_add_tail(&init_net
.list
, &net_namespace_list
);
209 mutex_unlock(&net_mutex
);
211 panic("Could not setup the initial network namespace");
216 pure_initcall(net_ns_init
);
219 static int register_pernet_operations(struct list_head
*list
,
220 struct pernet_operations
*ops
)
222 struct net
*net
, *undo_net
;
225 list_add_tail(&ops
->list
, list
);
228 error
= ops
->init(net
);
236 /* If I have an error cleanup all namespaces I initialized */
237 list_del(&ops
->list
);
239 for_each_net(undo_net
) {
249 static void unregister_pernet_operations(struct pernet_operations
*ops
)
253 list_del(&ops
->list
);
261 static int register_pernet_operations(struct list_head
*list
,
262 struct pernet_operations
*ops
)
264 if (ops
->init
== NULL
)
266 return ops
->init(&init_net
);
269 static void unregister_pernet_operations(struct pernet_operations
*ops
)
272 ops
->exit(&init_net
);
276 static DEFINE_IDA(net_generic_ids
);
279 * register_pernet_subsys - register a network namespace subsystem
280 * @ops: pernet operations structure for the subsystem
282 * Register a subsystem which has init and exit functions
283 * that are called when network namespaces are created and
284 * destroyed respectively.
286 * When registered all network namespace init functions are
287 * called for every existing network namespace. Allowing kernel
288 * modules to have a race free view of the set of network namespaces.
290 * When a new network namespace is created all of the init
291 * methods are called in the order in which they were registered.
293 * When a network namespace is destroyed all of the exit methods
294 * are called in the reverse of the order with which they were
297 int register_pernet_subsys(struct pernet_operations
*ops
)
300 mutex_lock(&net_mutex
);
301 error
= register_pernet_operations(first_device
, ops
);
302 mutex_unlock(&net_mutex
);
305 EXPORT_SYMBOL_GPL(register_pernet_subsys
);
308 * unregister_pernet_subsys - unregister a network namespace subsystem
309 * @ops: pernet operations structure to manipulate
311 * Remove the pernet operations structure from the list to be
312 * used when network namespaces are created or destroyed. In
313 * addition run the exit method for all existing network
316 void unregister_pernet_subsys(struct pernet_operations
*module
)
318 mutex_lock(&net_mutex
);
319 unregister_pernet_operations(module
);
320 mutex_unlock(&net_mutex
);
322 EXPORT_SYMBOL_GPL(unregister_pernet_subsys
);
325 * register_pernet_device - register a network namespace device
326 * @ops: pernet operations structure for the subsystem
328 * Register a device which has init and exit functions
329 * that are called when network namespaces are created and
330 * destroyed respectively.
332 * When registered all network namespace init functions are
333 * called for every existing network namespace. Allowing kernel
334 * modules to have a race free view of the set of network namespaces.
336 * When a new network namespace is created all of the init
337 * methods are called in the order in which they were registered.
339 * When a network namespace is destroyed all of the exit methods
340 * are called in the reverse of the order with which they were
343 int register_pernet_device(struct pernet_operations
*ops
)
346 mutex_lock(&net_mutex
);
347 error
= register_pernet_operations(&pernet_list
, ops
);
348 if (!error
&& (first_device
== &pernet_list
))
349 first_device
= &ops
->list
;
350 mutex_unlock(&net_mutex
);
353 EXPORT_SYMBOL_GPL(register_pernet_device
);
355 int register_pernet_gen_device(int *id
, struct pernet_operations
*ops
)
358 mutex_lock(&net_mutex
);
360 error
= ida_get_new_above(&net_generic_ids
, 1, id
);
362 if (error
== -EAGAIN
) {
363 ida_pre_get(&net_generic_ids
, GFP_KERNEL
);
368 error
= register_pernet_operations(&pernet_list
, ops
);
370 ida_remove(&net_generic_ids
, *id
);
371 else if (first_device
== &pernet_list
)
372 first_device
= &ops
->list
;
374 mutex_unlock(&net_mutex
);
377 EXPORT_SYMBOL_GPL(register_pernet_gen_device
);
380 * unregister_pernet_device - unregister a network namespace netdevice
381 * @ops: pernet operations structure to manipulate
383 * Remove the pernet operations structure from the list to be
384 * used when network namespaces are created or destroyed. In
385 * addition run the exit method for all existing network
388 void unregister_pernet_device(struct pernet_operations
*ops
)
390 mutex_lock(&net_mutex
);
391 if (&ops
->list
== first_device
)
392 first_device
= first_device
->next
;
393 unregister_pernet_operations(ops
);
394 mutex_unlock(&net_mutex
);
396 EXPORT_SYMBOL_GPL(unregister_pernet_device
);
398 void unregister_pernet_gen_device(int id
, struct pernet_operations
*ops
)
400 mutex_lock(&net_mutex
);
401 if (&ops
->list
== first_device
)
402 first_device
= first_device
->next
;
403 unregister_pernet_operations(ops
);
404 ida_remove(&net_generic_ids
, id
);
405 mutex_unlock(&net_mutex
);
407 EXPORT_SYMBOL_GPL(unregister_pernet_gen_device
);
409 static void net_generic_release(struct rcu_head
*rcu
)
411 struct net_generic
*ng
;
413 ng
= container_of(rcu
, struct net_generic
, rcu
);
417 int net_assign_generic(struct net
*net
, int id
, void *data
)
419 struct net_generic
*ng
, *old_ng
;
421 BUG_ON(!mutex_is_locked(&net_mutex
));
424 ng
= old_ng
= net
->gen
;
425 if (old_ng
->len
>= id
)
428 ng
= kzalloc(sizeof(struct net_generic
) +
429 id
* sizeof(void *), GFP_KERNEL
);
434 * Some synchronisation notes:
436 * The net_generic explores the net->gen array inside rcu
437 * read section. Besides once set the net->gen->ptr[x]
438 * pointer never changes (see rules in netns/generic.h).
440 * That said, we simply duplicate this array and schedule
441 * the old copy for kfree after a grace period.
445 INIT_RCU_HEAD(&ng
->rcu
);
446 memcpy(&ng
->ptr
, &old_ng
->ptr
, old_ng
->len
);
448 rcu_assign_pointer(net
->gen
, ng
);
449 call_rcu(&old_ng
->rcu
, net_generic_release
);
451 ng
->ptr
[id
- 1] = data
;
454 EXPORT_SYMBOL_GPL(net_assign_generic
);