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
);
21 EXPORT_SYMBOL_GPL(net_namespace_list
);
24 EXPORT_SYMBOL(init_net
);
26 #define INITIAL_NET_GEN_PTRS 13 /* +1 for len +2 for rcu_head */
29 * setup_net runs the initializers for the network namespace object.
31 static __net_init
int setup_net(struct net
*net
)
33 /* Must be called with net_mutex held */
34 struct pernet_operations
*ops
;
36 struct net_generic
*ng
;
38 atomic_set(&net
->count
, 1);
39 #ifdef NETNS_REFCNT_DEBUG
40 atomic_set(&net
->use_count
, 0);
44 ng
= kzalloc(sizeof(struct net_generic
) +
45 INITIAL_NET_GEN_PTRS
* sizeof(void *), GFP_KERNEL
);
49 ng
->len
= INITIAL_NET_GEN_PTRS
;
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 /* Be very certain incoming network packets will not find us */
146 net
= container_of(work
, struct net
, work
);
148 mutex_lock(&net_mutex
);
150 /* Don't let anyone else find us. */
152 list_del(&net
->list
);
155 /* Run all of the network namespace exit methods */
156 list_for_each_entry_reverse(ops
, &pernet_list
, list
) {
161 mutex_unlock(&net_mutex
);
163 /* Ensure there are no outstanding rcu callbacks using this
168 /* Finally it is safe to free my network namespace structure */
172 void __put_net(struct net
*net
)
174 /* Cleanup the network namespace in process context */
175 INIT_WORK(&net
->work
, cleanup_net
);
176 queue_work(netns_wq
, &net
->work
);
178 EXPORT_SYMBOL_GPL(__put_net
);
181 struct net
*copy_net_ns(unsigned long flags
, struct net
*old_net
)
183 if (flags
& CLONE_NEWNET
)
184 return ERR_PTR(-EINVAL
);
189 static int __init
net_ns_init(void)
193 printk(KERN_INFO
"net_namespace: %zd bytes\n", sizeof(struct net
));
195 net_cachep
= kmem_cache_create("net_namespace", sizeof(struct net
),
199 /* Create workqueue for cleanup */
200 netns_wq
= create_singlethread_workqueue("netns");
202 panic("Could not create netns workq");
205 mutex_lock(&net_mutex
);
206 err
= setup_net(&init_net
);
209 list_add_tail(&init_net
.list
, &net_namespace_list
);
212 mutex_unlock(&net_mutex
);
214 panic("Could not setup the initial network namespace");
219 pure_initcall(net_ns_init
);
222 static int register_pernet_operations(struct list_head
*list
,
223 struct pernet_operations
*ops
)
225 struct net
*net
, *undo_net
;
228 list_add_tail(&ops
->list
, list
);
231 error
= ops
->init(net
);
239 /* If I have an error cleanup all namespaces I initialized */
240 list_del(&ops
->list
);
242 for_each_net(undo_net
) {
252 static void unregister_pernet_operations(struct pernet_operations
*ops
)
256 list_del(&ops
->list
);
264 static int register_pernet_operations(struct list_head
*list
,
265 struct pernet_operations
*ops
)
267 if (ops
->init
== NULL
)
269 return ops
->init(&init_net
);
272 static void unregister_pernet_operations(struct pernet_operations
*ops
)
275 ops
->exit(&init_net
);
279 static DEFINE_IDA(net_generic_ids
);
282 * register_pernet_subsys - register a network namespace subsystem
283 * @ops: pernet operations structure for the subsystem
285 * Register a subsystem which has init and exit functions
286 * that are called when network namespaces are created and
287 * destroyed respectively.
289 * When registered all network namespace init functions are
290 * called for every existing network namespace. Allowing kernel
291 * modules to have a race free view of the set of network namespaces.
293 * When a new network namespace is created all of the init
294 * methods are called in the order in which they were registered.
296 * When a network namespace is destroyed all of the exit methods
297 * are called in the reverse of the order with which they were
300 int register_pernet_subsys(struct pernet_operations
*ops
)
303 mutex_lock(&net_mutex
);
304 error
= register_pernet_operations(first_device
, ops
);
305 mutex_unlock(&net_mutex
);
308 EXPORT_SYMBOL_GPL(register_pernet_subsys
);
311 * unregister_pernet_subsys - unregister a network namespace subsystem
312 * @ops: pernet operations structure to manipulate
314 * Remove the pernet operations structure from the list to be
315 * used when network namespaces are created or destroyed. In
316 * addition run the exit method for all existing network
319 void unregister_pernet_subsys(struct pernet_operations
*module
)
321 mutex_lock(&net_mutex
);
322 unregister_pernet_operations(module
);
323 mutex_unlock(&net_mutex
);
325 EXPORT_SYMBOL_GPL(unregister_pernet_subsys
);
327 int register_pernet_gen_subsys(int *id
, struct pernet_operations
*ops
)
331 mutex_lock(&net_mutex
);
333 rv
= ida_get_new_above(&net_generic_ids
, 1, id
);
336 ida_pre_get(&net_generic_ids
, GFP_KERNEL
);
341 rv
= register_pernet_operations(first_device
, ops
);
343 ida_remove(&net_generic_ids
, *id
);
345 mutex_unlock(&net_mutex
);
348 EXPORT_SYMBOL_GPL(register_pernet_gen_subsys
);
350 void unregister_pernet_gen_subsys(int id
, struct pernet_operations
*ops
)
352 mutex_lock(&net_mutex
);
353 unregister_pernet_operations(ops
);
354 ida_remove(&net_generic_ids
, id
);
355 mutex_unlock(&net_mutex
);
357 EXPORT_SYMBOL_GPL(unregister_pernet_gen_subsys
);
360 * register_pernet_device - register a network namespace device
361 * @ops: pernet operations structure for the subsystem
363 * Register a device which has init and exit functions
364 * that are called when network namespaces are created and
365 * destroyed respectively.
367 * When registered all network namespace init functions are
368 * called for every existing network namespace. Allowing kernel
369 * modules to have a race free view of the set of network namespaces.
371 * When a new network namespace is created all of the init
372 * methods are called in the order in which they were registered.
374 * When a network namespace is destroyed all of the exit methods
375 * are called in the reverse of the order with which they were
378 int register_pernet_device(struct pernet_operations
*ops
)
381 mutex_lock(&net_mutex
);
382 error
= register_pernet_operations(&pernet_list
, ops
);
383 if (!error
&& (first_device
== &pernet_list
))
384 first_device
= &ops
->list
;
385 mutex_unlock(&net_mutex
);
388 EXPORT_SYMBOL_GPL(register_pernet_device
);
390 int register_pernet_gen_device(int *id
, struct pernet_operations
*ops
)
393 mutex_lock(&net_mutex
);
395 error
= ida_get_new_above(&net_generic_ids
, 1, id
);
397 if (error
== -EAGAIN
) {
398 ida_pre_get(&net_generic_ids
, GFP_KERNEL
);
403 error
= register_pernet_operations(&pernet_list
, ops
);
405 ida_remove(&net_generic_ids
, *id
);
406 else if (first_device
== &pernet_list
)
407 first_device
= &ops
->list
;
409 mutex_unlock(&net_mutex
);
412 EXPORT_SYMBOL_GPL(register_pernet_gen_device
);
415 * unregister_pernet_device - unregister a network namespace netdevice
416 * @ops: pernet operations structure to manipulate
418 * Remove the pernet operations structure from the list to be
419 * used when network namespaces are created or destroyed. In
420 * addition run the exit method for all existing network
423 void unregister_pernet_device(struct pernet_operations
*ops
)
425 mutex_lock(&net_mutex
);
426 if (&ops
->list
== first_device
)
427 first_device
= first_device
->next
;
428 unregister_pernet_operations(ops
);
429 mutex_unlock(&net_mutex
);
431 EXPORT_SYMBOL_GPL(unregister_pernet_device
);
433 void unregister_pernet_gen_device(int id
, struct pernet_operations
*ops
)
435 mutex_lock(&net_mutex
);
436 if (&ops
->list
== first_device
)
437 first_device
= first_device
->next
;
438 unregister_pernet_operations(ops
);
439 ida_remove(&net_generic_ids
, id
);
440 mutex_unlock(&net_mutex
);
442 EXPORT_SYMBOL_GPL(unregister_pernet_gen_device
);
444 static void net_generic_release(struct rcu_head
*rcu
)
446 struct net_generic
*ng
;
448 ng
= container_of(rcu
, struct net_generic
, rcu
);
452 int net_assign_generic(struct net
*net
, int id
, void *data
)
454 struct net_generic
*ng
, *old_ng
;
456 BUG_ON(!mutex_is_locked(&net_mutex
));
459 ng
= old_ng
= net
->gen
;
460 if (old_ng
->len
>= id
)
463 ng
= kzalloc(sizeof(struct net_generic
) +
464 id
* sizeof(void *), GFP_KERNEL
);
469 * Some synchronisation notes:
471 * The net_generic explores the net->gen array inside rcu
472 * read section. Besides once set the net->gen->ptr[x]
473 * pointer never changes (see rules in netns/generic.h).
475 * That said, we simply duplicate this array and schedule
476 * the old copy for kfree after a grace period.
480 memcpy(&ng
->ptr
, &old_ng
->ptr
, old_ng
->len
);
482 rcu_assign_pointer(net
->gen
, ng
);
483 call_rcu(&old_ng
->rcu
, net_generic_release
);
485 ng
->ptr
[id
- 1] = data
;
488 EXPORT_SYMBOL_GPL(net_assign_generic
);