1 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
4 #include <linux/rtnetlink.h>
6 #include <linux/slab.h>
7 #include <linux/list.h>
9 #include <linux/sched.h>
15 #include <linux/export.h>
24 static struct list_head *first_device = &pernet_list;
35 #define INITIAL_NET_GEN_PTRS 13
46 ng->
len = max_gen_ptrs;
51 static int net_assign_generic(
struct net *
net,
int id,
void *
data)
55 BUG_ON(!mutex_is_locked(&net_mutex));
59 lockdep_is_held(&net_mutex));
61 if (old_ng->
len >=
id)
64 ng = net_alloc_generic();
93 if (ops->
id && ops->
size) {
98 err = net_assign_generic(net, *ops->
id, data);
104 err = ops->
init(net);
117 if (ops->
id && ops->
size) {
132 ops->exit_batch(net_exit_list);
139 if (ops->size && ops->id) {
157 net->dev_base_seq = 1;
159 #ifdef NETNS_REFCNT_DEBUG
164 error = ops_init(ops, net);
175 list_add(&net->exit_list, &net_exit_list);
178 ops_exit_list(ops, &net_exit_list);
182 ops_free_list(ops, &net_exit_list);
193 static struct net *net_alloc(
void)
195 struct net *net =
NULL;
198 ng = net_alloc_generic();
202 net = kmem_cache_zalloc(net_cachep,
GFP_KERNEL);
215 static void net_free(
struct net *net)
217 #ifdef NETNS_REFCNT_DEBUG
219 pr_emerg(
"network namespace not free! Usage: %d\n",
241 return get_net(old_net);
250 list_add_tail_rcu(&net->
list, &net_namespace_list);
267 struct net *net, *
tmp;
272 spin_lock_irq(&cleanup_list_lock);
274 spin_unlock_irq(&cleanup_list_lock);
281 list_del_rcu(&net->
list);
295 ops_exit_list(ops, &net_exit_list);
299 ops_free_list(ops, &net_exit_list);
310 list_del_init(&net->exit_list);
316 void __put_net(
struct net *net)
323 spin_unlock_irqrestore(&cleanup_list_lock, flags);
337 return ERR_CAST(file);
339 ei = PROC_I(file->f_dentry->d_inode);
340 if (ei->
ns_ops == &netns_operations)
341 net = get_net(ei->
ns);
352 if (flags & CLONE_NEWNET)
369 net = ERR_PTR(-
ESRCH);
374 nsproxy = task_nsproxy(tsk);
376 net = get_net(nsproxy->
net_ns);
383 static int __init net_ns_init(
void)
395 panic(
"Could not create netns workq");
398 ng = net_alloc_generic();
400 panic(
"Could not allocate generic netns");
405 if (setup_net(&init_net))
406 panic(
"Could not setup the initial network namespace");
409 list_add_tail_rcu(&init_net.
list, &net_namespace_list);
420 static int __register_pernet_operations(
struct list_head *
list,
430 error = ops_init(ops, net);
441 ops_exit_list(ops, &net_exit_list);
442 ops_free_list(ops, &net_exit_list);
454 ops_exit_list(ops, &net_exit_list);
455 ops_free_list(ops, &net_exit_list);
460 static int __register_pernet_operations(
struct list_head *list,
463 return ops_init(ops, &init_net);
469 list_add(&init_net.
exit_list, &net_exit_list);
470 ops_exit_list(ops, &net_exit_list);
471 ops_free_list(ops, &net_exit_list);
478 static int register_pernet_operations(
struct list_head *list,
493 max_gen_ptrs =
max_t(
unsigned int, max_gen_ptrs, *ops->
id);
495 error = __register_pernet_operations(list, ops);
508 __unregister_pernet_operations(ops);
537 error = register_pernet_operations(first_device, ops);
555 unregister_pernet_operations(ops);
583 error = register_pernet_operations(&pernet_list, ops);
584 if (!error && (first_device == &pernet_list))
585 first_device = &ops->
list;
603 if (&ops->
list == first_device)
604 first_device = first_device->
next;
605 unregister_pernet_operations(ops);
613 struct net *net =
NULL;
617 nsproxy = task_nsproxy(task);
619 net = get_net(nsproxy->
net_ns);
625 static void netns_put(
void *ns)
630 static int netns_install(
struct nsproxy *nsproxy,
void *ns)
633 nsproxy->
net_ns = get_net(ns);
642 .install = netns_install,