blob: dddbacb8f28ccba180cd20855476d5d7e351b1b8 [file] [log] [blame]
Joe Perchese005d192012-05-16 19:58:40 +00001#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2
Eric W. Biederman5f256be2007-09-12 11:50:50 +02003#include <linux/workqueue.h>
4#include <linux/rtnetlink.h>
5#include <linux/cache.h>
6#include <linux/slab.h>
7#include <linux/list.h>
8#include <linux/delay.h>
Eric W. Biederman9dd776b2007-09-26 22:04:26 -07009#include <linux/sched.h>
Pavel Emelyanovc93cf612008-04-15 00:35:23 -070010#include <linux/idr.h>
Johannes Berg11a28d32009-07-10 09:51:33 +000011#include <linux/rculist.h>
Johannes Berg30ffee82009-07-10 09:51:35 +000012#include <linux/nsproxy.h>
Eric W. Biedermanf0630522011-05-04 17:51:50 -070013#include <linux/proc_fs.h>
14#include <linux/file.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040015#include <linux/export.h>
Eric W. Biederman5f256be2007-09-12 11:50:50 +020016#include <net/net_namespace.h>
Pavel Emelyanovdec827d2008-04-15 00:36:08 -070017#include <net/netns/generic.h>
Eric W. Biederman5f256be2007-09-12 11:50:50 +020018
19/*
20 * Our network namespace constructor/destructor lists
21 */
22
23static LIST_HEAD(pernet_list);
24static struct list_head *first_device = &pernet_list;
25static DEFINE_MUTEX(net_mutex);
26
Eric W. Biederman5f256be2007-09-12 11:50:50 +020027LIST_HEAD(net_namespace_list);
Alexey Dobriyanb76a4612008-10-08 11:35:06 +020028EXPORT_SYMBOL_GPL(net_namespace_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +020029
Eric W. Biederman5f256be2007-09-12 11:50:50 +020030struct net init_net;
Denis V. Lunevff4b9502008-01-22 22:05:33 -080031EXPORT_SYMBOL(init_net);
Eric W. Biederman5f256be2007-09-12 11:50:50 +020032
Pavel Emelyanovdec827d2008-04-15 00:36:08 -070033#define INITIAL_NET_GEN_PTRS 13 /* +1 for len +2 for rcu_head */
34
Eric Dumazet073862b2012-01-26 00:41:38 +000035static unsigned int max_gen_ptrs = INITIAL_NET_GEN_PTRS;
36
37static struct net_generic *net_alloc_generic(void)
38{
39 struct net_generic *ng;
40 size_t generic_size = offsetof(struct net_generic, ptr[max_gen_ptrs]);
41
42 ng = kzalloc(generic_size, GFP_KERNEL);
43 if (ng)
44 ng->len = max_gen_ptrs;
45
46 return ng;
47}
48
Jiri Pirko05fceb42010-04-23 01:40:47 +000049static int net_assign_generic(struct net *net, int id, void *data)
50{
51 struct net_generic *ng, *old_ng;
52
53 BUG_ON(!mutex_is_locked(&net_mutex));
54 BUG_ON(id == 0);
55
Eric Dumazet1c877332010-10-25 03:20:11 +000056 old_ng = rcu_dereference_protected(net->gen,
57 lockdep_is_held(&net_mutex));
58 ng = old_ng;
Jiri Pirko05fceb42010-04-23 01:40:47 +000059 if (old_ng->len >= id)
60 goto assign;
61
Eric Dumazet073862b2012-01-26 00:41:38 +000062 ng = net_alloc_generic();
Jiri Pirko05fceb42010-04-23 01:40:47 +000063 if (ng == NULL)
64 return -ENOMEM;
65
66 /*
67 * Some synchronisation notes:
68 *
69 * The net_generic explores the net->gen array inside rcu
70 * read section. Besides once set the net->gen->ptr[x]
71 * pointer never changes (see rules in netns/generic.h).
72 *
73 * That said, we simply duplicate this array and schedule
74 * the old copy for kfree after a grace period.
75 */
76
Jiri Pirko05fceb42010-04-23 01:40:47 +000077 memcpy(&ng->ptr, &old_ng->ptr, old_ng->len * sizeof(void*));
78
79 rcu_assign_pointer(net->gen, ng);
Lai Jiangshan04d4dfe2011-03-18 12:06:32 +080080 kfree_rcu(old_ng, rcu);
Jiri Pirko05fceb42010-04-23 01:40:47 +000081assign:
82 ng->ptr[id - 1] = data;
83 return 0;
84}
85
Eric W. Biedermanf875bae2009-11-29 22:25:28 +000086static int ops_init(const struct pernet_operations *ops, struct net *net)
87{
Julian Anastasovb9229342012-04-16 04:43:15 +000088 int err = -ENOMEM;
89 void *data = NULL;
90
Eric W. Biedermanf875bae2009-11-29 22:25:28 +000091 if (ops->id && ops->size) {
Julian Anastasovb9229342012-04-16 04:43:15 +000092 data = kzalloc(ops->size, GFP_KERNEL);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +000093 if (!data)
Julian Anastasovb9229342012-04-16 04:43:15 +000094 goto out;
Eric W. Biedermanf875bae2009-11-29 22:25:28 +000095
96 err = net_assign_generic(net, *ops->id, data);
Julian Anastasovb9229342012-04-16 04:43:15 +000097 if (err)
98 goto cleanup;
Eric W. Biedermanf875bae2009-11-29 22:25:28 +000099 }
Julian Anastasovb9229342012-04-16 04:43:15 +0000100 err = 0;
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000101 if (ops->init)
Julian Anastasovb9229342012-04-16 04:43:15 +0000102 err = ops->init(net);
103 if (!err)
104 return 0;
105
106cleanup:
107 kfree(data);
108
109out:
110 return err;
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000111}
112
113static void ops_free(const struct pernet_operations *ops, struct net *net)
114{
115 if (ops->id && ops->size) {
116 int id = *ops->id;
117 kfree(net_generic(net, id));
118 }
119}
120
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000121static void ops_exit_list(const struct pernet_operations *ops,
122 struct list_head *net_exit_list)
123{
124 struct net *net;
125 if (ops->exit) {
126 list_for_each_entry(net, net_exit_list, exit_list)
127 ops->exit(net);
128 }
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000129 if (ops->exit_batch)
130 ops->exit_batch(net_exit_list);
131}
132
133static void ops_free_list(const struct pernet_operations *ops,
134 struct list_head *net_exit_list)
135{
136 struct net *net;
137 if (ops->size && ops->id) {
138 list_for_each_entry(net, net_exit_list, exit_list)
139 ops_free(ops, net);
140 }
141}
142
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700143/*
144 * setup_net runs the initializers for the network namespace object.
145 */
Pavel Emelyanov1a2ee932007-11-01 00:45:59 -0700146static __net_init int setup_net(struct net *net)
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700147{
148 /* Must be called with net_mutex held */
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000149 const struct pernet_operations *ops, *saved_ops;
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800150 int error = 0;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000151 LIST_HEAD(net_exit_list);
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700152
153 atomic_set(&net->count, 1);
Al Viroa685e082011-06-08 21:13:01 -0400154 atomic_set(&net->passive, 1);
Thomas Graf4e985ad2011-06-21 03:11:20 +0000155 net->dev_base_seq = 1;
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800156
Denis V. Lunev5d1e4462008-04-16 01:58:04 -0700157#ifdef NETNS_REFCNT_DEBUG
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700158 atomic_set(&net->use_count, 0);
Denis V. Lunev5d1e4462008-04-16 01:58:04 -0700159#endif
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700160
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700161 list_for_each_entry(ops, &pernet_list, list) {
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000162 error = ops_init(ops, net);
163 if (error < 0)
164 goto out_undo;
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700165 }
166out:
167 return error;
168
169out_undo:
170 /* Walk through the list backwards calling the exit functions
171 * for the pernet modules whose init functions did not fail.
172 */
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000173 list_add(&net->exit_list, &net_exit_list);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000174 saved_ops = ops;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000175 list_for_each_entry_continue_reverse(ops, &pernet_list, list)
176 ops_exit_list(ops, &net_exit_list);
177
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000178 ops = saved_ops;
179 list_for_each_entry_continue_reverse(ops, &pernet_list, list)
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000180 ops_free_list(ops, &net_exit_list);
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700181
182 rcu_barrier();
183 goto out;
184}
185
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800186
Clemens Nossebe47d42009-02-23 15:37:35 -0800187#ifdef CONFIG_NET_NS
188static struct kmem_cache *net_cachep;
189static struct workqueue_struct *netns_wq;
190
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200191static struct net *net_alloc(void)
192{
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800193 struct net *net = NULL;
194 struct net_generic *ng;
195
196 ng = net_alloc_generic();
197 if (!ng)
198 goto out;
199
200 net = kmem_cache_zalloc(net_cachep, GFP_KERNEL);
201 if (!net)
202 goto out_free;
203
204 rcu_assign_pointer(net->gen, ng);
205out:
206 return net;
207
208out_free:
209 kfree(ng);
210 goto out;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200211}
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200212
Johann Felix Soden45a19b02007-11-07 01:30:30 -0800213static void net_free(struct net *net)
214{
Denis V. Lunev5d1e4462008-04-16 01:58:04 -0700215#ifdef NETNS_REFCNT_DEBUG
Johann Felix Soden45a19b02007-11-07 01:30:30 -0800216 if (unlikely(atomic_read(&net->use_count) != 0)) {
Joe Perchese005d192012-05-16 19:58:40 +0000217 pr_emerg("network namespace not free! Usage: %d\n",
218 atomic_read(&net->use_count));
Johann Felix Soden45a19b02007-11-07 01:30:30 -0800219 return;
220 }
Denis V. Lunev5d1e4462008-04-16 01:58:04 -0700221#endif
Alexey Dobriyan4ef079c2008-10-14 22:54:48 -0700222 kfree(net->gen);
Johann Felix Soden45a19b02007-11-07 01:30:30 -0800223 kmem_cache_free(net_cachep, net);
224}
225
Al Viroa685e082011-06-08 21:13:01 -0400226void net_drop_ns(void *p)
227{
228 struct net *ns = p;
229 if (ns && atomic_dec_and_test(&ns->passive))
230 net_free(ns);
231}
232
Rob Landley911cb192011-04-15 02:26:25 +0000233struct net *copy_net_ns(unsigned long flags, struct net *old_net)
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700234{
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700235 struct net *net;
236 int rv;
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700237
Rob Landley911cb192011-04-15 02:26:25 +0000238 if (!(flags & CLONE_NEWNET))
239 return get_net(old_net);
240
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700241 net = net_alloc();
242 if (!net)
243 return ERR_PTR(-ENOMEM);
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700244 mutex_lock(&net_mutex);
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700245 rv = setup_net(net);
246 if (rv == 0) {
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800247 rtnl_lock();
Johannes Berg11a28d32009-07-10 09:51:33 +0000248 list_add_tail_rcu(&net->list, &net_namespace_list);
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800249 rtnl_unlock();
250 }
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700251 mutex_unlock(&net_mutex);
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700252 if (rv < 0) {
Al Viroa685e082011-06-08 21:13:01 -0400253 net_drop_ns(net);
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700254 return ERR_PTR(rv);
255 }
256 return net;
257}
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800258
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000259static DEFINE_SPINLOCK(cleanup_list_lock);
260static LIST_HEAD(cleanup_list); /* Must hold cleanup_list_lock to touch */
261
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200262static void cleanup_net(struct work_struct *work)
263{
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000264 const struct pernet_operations *ops;
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000265 struct net *net, *tmp;
266 LIST_HEAD(net_kill_list);
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000267 LIST_HEAD(net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200268
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000269 /* Atomically snapshot the list of namespaces to cleanup */
270 spin_lock_irq(&cleanup_list_lock);
271 list_replace_init(&cleanup_list, &net_kill_list);
272 spin_unlock_irq(&cleanup_list_lock);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200273
274 mutex_lock(&net_mutex);
275
276 /* Don't let anyone else find us. */
Eric W. Biedermanf4618d32007-09-26 22:40:08 -0700277 rtnl_lock();
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000278 list_for_each_entry(net, &net_kill_list, cleanup_list) {
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000279 list_del_rcu(&net->list);
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000280 list_add_tail(&net->exit_list, &net_exit_list);
281 }
Eric W. Biedermanf4618d32007-09-26 22:40:08 -0700282 rtnl_unlock();
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200283
Johannes Berg11a28d32009-07-10 09:51:33 +0000284 /*
285 * Another CPU might be rcu-iterating the list, wait for it.
286 * This needs to be before calling the exit() notifiers, so
287 * the rcu_barrier() below isn't sufficient alone.
288 */
289 synchronize_rcu();
290
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200291 /* Run all of the network namespace exit methods */
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000292 list_for_each_entry_reverse(ops, &pernet_list, list)
293 ops_exit_list(ops, &net_exit_list);
294
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000295 /* Free the net generic variables */
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000296 list_for_each_entry_reverse(ops, &pernet_list, list)
297 ops_free_list(ops, &net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200298
299 mutex_unlock(&net_mutex);
300
301 /* Ensure there are no outstanding rcu callbacks using this
302 * network namespace.
303 */
304 rcu_barrier();
305
306 /* Finally it is safe to free my network namespace structure */
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000307 list_for_each_entry_safe(net, tmp, &net_exit_list, exit_list) {
308 list_del_init(&net->exit_list);
Al Viroa685e082011-06-08 21:13:01 -0400309 net_drop_ns(net);
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000310 }
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200311}
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000312static DECLARE_WORK(net_cleanup_work, cleanup_net);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200313
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200314void __put_net(struct net *net)
315{
316 /* Cleanup the network namespace in process context */
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000317 unsigned long flags;
318
319 spin_lock_irqsave(&cleanup_list_lock, flags);
320 list_add(&net->cleanup_list, &cleanup_list);
321 spin_unlock_irqrestore(&cleanup_list_lock, flags);
322
323 queue_work(netns_wq, &net_cleanup_work);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200324}
325EXPORT_SYMBOL_GPL(__put_net);
326
Stephen Rothwell956c9202011-05-12 13:51:13 +1000327struct net *get_net_ns_by_fd(int fd)
328{
329 struct proc_inode *ei;
330 struct file *file;
331 struct net *net;
332
Stephen Rothwell956c9202011-05-12 13:51:13 +1000333 file = proc_ns_fget(fd);
Al Viroc316e6a2011-06-05 00:37:35 +0000334 if (IS_ERR(file))
335 return ERR_CAST(file);
Stephen Rothwell956c9202011-05-12 13:51:13 +1000336
337 ei = PROC_I(file->f_dentry->d_inode);
Al Viroc316e6a2011-06-05 00:37:35 +0000338 if (ei->ns_ops == &netns_operations)
339 net = get_net(ei->ns);
340 else
341 net = ERR_PTR(-EINVAL);
Stephen Rothwell956c9202011-05-12 13:51:13 +1000342
Al Viroc316e6a2011-06-05 00:37:35 +0000343 fput(file);
Stephen Rothwell956c9202011-05-12 13:51:13 +1000344 return net;
345}
346
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700347#else
Eric W. Biederman9dd776b2007-09-26 22:04:26 -0700348struct net *copy_net_ns(unsigned long flags, struct net *old_net)
349{
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700350 if (flags & CLONE_NEWNET)
351 return ERR_PTR(-EINVAL);
352 return old_net;
Eric W. Biederman9dd776b2007-09-26 22:04:26 -0700353}
Stephen Rothwell956c9202011-05-12 13:51:13 +1000354
355struct net *get_net_ns_by_fd(int fd)
356{
357 return ERR_PTR(-EINVAL);
358}
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700359#endif
Eric W. Biederman9dd776b2007-09-26 22:04:26 -0700360
Johannes Berg30ffee82009-07-10 09:51:35 +0000361struct net *get_net_ns_by_pid(pid_t pid)
362{
363 struct task_struct *tsk;
364 struct net *net;
365
366 /* Lookup the network namespace */
367 net = ERR_PTR(-ESRCH);
368 rcu_read_lock();
369 tsk = find_task_by_vpid(pid);
370 if (tsk) {
371 struct nsproxy *nsproxy;
372 nsproxy = task_nsproxy(tsk);
373 if (nsproxy)
374 net = get_net(nsproxy->net_ns);
375 }
376 rcu_read_unlock();
377 return net;
378}
379EXPORT_SYMBOL_GPL(get_net_ns_by_pid);
380
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200381static int __init net_ns_init(void)
382{
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800383 struct net_generic *ng;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200384
Pavel Emelyanovd57a9212007-11-01 00:46:50 -0700385#ifdef CONFIG_NET_NS
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200386 net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
387 SMP_CACHE_BYTES,
388 SLAB_PANIC, NULL);
Benjamin Thery3ef13552007-11-19 23:18:16 -0800389
390 /* Create workqueue for cleanup */
391 netns_wq = create_singlethread_workqueue("netns");
392 if (!netns_wq)
393 panic("Could not create netns workq");
Pavel Emelyanovd57a9212007-11-01 00:46:50 -0700394#endif
Benjamin Thery3ef13552007-11-19 23:18:16 -0800395
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800396 ng = net_alloc_generic();
397 if (!ng)
398 panic("Could not allocate generic netns");
399
400 rcu_assign_pointer(init_net.gen, ng);
401
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200402 mutex_lock(&net_mutex);
Stephen Hemmingerca0f3112009-05-21 15:10:31 -0700403 if (setup_net(&init_net))
404 panic("Could not setup the initial network namespace");
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200405
Eric W. Biedermanf4618d32007-09-26 22:40:08 -0700406 rtnl_lock();
Johannes Berg11a28d32009-07-10 09:51:33 +0000407 list_add_tail_rcu(&init_net.list, &net_namespace_list);
Eric W. Biedermanf4618d32007-09-26 22:40:08 -0700408 rtnl_unlock();
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200409
410 mutex_unlock(&net_mutex);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200411
412 return 0;
413}
414
415pure_initcall(net_ns_init);
416
Denis V. Luneved160e82007-11-13 03:23:21 -0800417#ifdef CONFIG_NET_NS
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000418static int __register_pernet_operations(struct list_head *list,
419 struct pernet_operations *ops)
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200420{
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000421 struct net *net;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200422 int error;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000423 LIST_HEAD(net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200424
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200425 list_add_tail(&ops->list, list);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000426 if (ops->init || (ops->id && ops->size)) {
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700427 for_each_net(net) {
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000428 error = ops_init(ops, net);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200429 if (error)
430 goto out_undo;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000431 list_add_tail(&net->exit_list, &net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200432 }
433 }
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700434 return 0;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200435
436out_undo:
437 /* If I have an error cleanup all namespaces I initialized */
438 list_del(&ops->list);
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000439 ops_exit_list(ops, &net_exit_list);
440 ops_free_list(ops, &net_exit_list);
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700441 return error;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200442}
443
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000444static void __unregister_pernet_operations(struct pernet_operations *ops)
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200445{
446 struct net *net;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000447 LIST_HEAD(net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200448
449 list_del(&ops->list);
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000450 for_each_net(net)
451 list_add_tail(&net->exit_list, &net_exit_list);
452 ops_exit_list(ops, &net_exit_list);
453 ops_free_list(ops, &net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200454}
455
Denis V. Luneved160e82007-11-13 03:23:21 -0800456#else
457
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000458static int __register_pernet_operations(struct list_head *list,
459 struct pernet_operations *ops)
460{
Julian Anastasovb9229342012-04-16 04:43:15 +0000461 return ops_init(ops, &init_net);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000462}
463
464static void __unregister_pernet_operations(struct pernet_operations *ops)
465{
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000466 LIST_HEAD(net_exit_list);
467 list_add(&init_net.exit_list, &net_exit_list);
468 ops_exit_list(ops, &net_exit_list);
469 ops_free_list(ops, &net_exit_list);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000470}
471
472#endif /* CONFIG_NET_NS */
473
474static DEFINE_IDA(net_generic_ids);
475
Denis V. Luneved160e82007-11-13 03:23:21 -0800476static int register_pernet_operations(struct list_head *list,
477 struct pernet_operations *ops)
478{
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000479 int error;
480
481 if (ops->id) {
482again:
483 error = ida_get_new_above(&net_generic_ids, 1, ops->id);
484 if (error < 0) {
485 if (error == -EAGAIN) {
486 ida_pre_get(&net_generic_ids, GFP_KERNEL);
487 goto again;
488 }
489 return error;
490 }
Eric Dumazet073862b2012-01-26 00:41:38 +0000491 max_gen_ptrs = max_t(unsigned int, max_gen_ptrs, *ops->id);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000492 }
493 error = __register_pernet_operations(list, ops);
Eric W. Biederman3a765ed2009-12-03 02:29:06 +0000494 if (error) {
495 rcu_barrier();
496 if (ops->id)
497 ida_remove(&net_generic_ids, *ops->id);
498 }
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000499
500 return error;
Denis V. Luneved160e82007-11-13 03:23:21 -0800501}
502
503static void unregister_pernet_operations(struct pernet_operations *ops)
504{
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000505
506 __unregister_pernet_operations(ops);
Eric W. Biederman3a765ed2009-12-03 02:29:06 +0000507 rcu_barrier();
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000508 if (ops->id)
509 ida_remove(&net_generic_ids, *ops->id);
Denis V. Luneved160e82007-11-13 03:23:21 -0800510}
Pavel Emelyanovc93cf612008-04-15 00:35:23 -0700511
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200512/**
513 * register_pernet_subsys - register a network namespace subsystem
514 * @ops: pernet operations structure for the subsystem
515 *
516 * Register a subsystem which has init and exit functions
517 * that are called when network namespaces are created and
518 * destroyed respectively.
519 *
520 * When registered all network namespace init functions are
521 * called for every existing network namespace. Allowing kernel
522 * modules to have a race free view of the set of network namespaces.
523 *
524 * When a new network namespace is created all of the init
525 * methods are called in the order in which they were registered.
526 *
527 * When a network namespace is destroyed all of the exit methods
528 * are called in the reverse of the order with which they were
529 * registered.
530 */
531int register_pernet_subsys(struct pernet_operations *ops)
532{
533 int error;
534 mutex_lock(&net_mutex);
535 error = register_pernet_operations(first_device, ops);
536 mutex_unlock(&net_mutex);
537 return error;
538}
539EXPORT_SYMBOL_GPL(register_pernet_subsys);
540
541/**
542 * unregister_pernet_subsys - unregister a network namespace subsystem
543 * @ops: pernet operations structure to manipulate
544 *
545 * Remove the pernet operations structure from the list to be
Oliver Pinter53379e52008-02-03 17:56:48 +0200546 * used when network namespaces are created or destroyed. In
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200547 * addition run the exit method for all existing network
548 * namespaces.
549 */
Jiri Pirkob3c981d2010-04-25 00:49:56 -0700550void unregister_pernet_subsys(struct pernet_operations *ops)
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200551{
552 mutex_lock(&net_mutex);
Jiri Pirkob3c981d2010-04-25 00:49:56 -0700553 unregister_pernet_operations(ops);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200554 mutex_unlock(&net_mutex);
555}
556EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
557
558/**
559 * register_pernet_device - register a network namespace device
560 * @ops: pernet operations structure for the subsystem
561 *
562 * Register a device which has init and exit functions
563 * that are called when network namespaces are created and
564 * destroyed respectively.
565 *
566 * When registered all network namespace init functions are
567 * called for every existing network namespace. Allowing kernel
568 * modules to have a race free view of the set of network namespaces.
569 *
570 * When a new network namespace is created all of the init
571 * methods are called in the order in which they were registered.
572 *
573 * When a network namespace is destroyed all of the exit methods
574 * are called in the reverse of the order with which they were
575 * registered.
576 */
577int register_pernet_device(struct pernet_operations *ops)
578{
579 int error;
580 mutex_lock(&net_mutex);
581 error = register_pernet_operations(&pernet_list, ops);
582 if (!error && (first_device == &pernet_list))
583 first_device = &ops->list;
584 mutex_unlock(&net_mutex);
585 return error;
586}
587EXPORT_SYMBOL_GPL(register_pernet_device);
588
589/**
590 * unregister_pernet_device - unregister a network namespace netdevice
591 * @ops: pernet operations structure to manipulate
592 *
593 * Remove the pernet operations structure from the list to be
Oliver Pinter53379e52008-02-03 17:56:48 +0200594 * used when network namespaces are created or destroyed. In
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200595 * addition run the exit method for all existing network
596 * namespaces.
597 */
598void unregister_pernet_device(struct pernet_operations *ops)
599{
600 mutex_lock(&net_mutex);
601 if (&ops->list == first_device)
602 first_device = first_device->next;
603 unregister_pernet_operations(ops);
604 mutex_unlock(&net_mutex);
605}
606EXPORT_SYMBOL_GPL(unregister_pernet_device);
Eric W. Biederman13b6f572010-03-07 18:14:23 -0800607
608#ifdef CONFIG_NET_NS
609static void *netns_get(struct task_struct *task)
610{
Eric W. Biedermanf0630522011-05-04 17:51:50 -0700611 struct net *net = NULL;
612 struct nsproxy *nsproxy;
613
Eric W. Biederman13b6f572010-03-07 18:14:23 -0800614 rcu_read_lock();
Eric W. Biedermanf0630522011-05-04 17:51:50 -0700615 nsproxy = task_nsproxy(task);
616 if (nsproxy)
617 net = get_net(nsproxy->net_ns);
Eric W. Biederman13b6f572010-03-07 18:14:23 -0800618 rcu_read_unlock();
Eric W. Biedermanf0630522011-05-04 17:51:50 -0700619
Eric W. Biederman13b6f572010-03-07 18:14:23 -0800620 return net;
621}
622
623static void netns_put(void *ns)
624{
625 put_net(ns);
626}
627
628static int netns_install(struct nsproxy *nsproxy, void *ns)
629{
630 put_net(nsproxy->net_ns);
631 nsproxy->net_ns = get_net(ns);
632 return 0;
633}
634
635const struct proc_ns_operations netns_operations = {
636 .name = "net",
637 .type = CLONE_NEWNET,
638 .get = netns_get,
639 .put = netns_put,
640 .install = netns_install,
641};
642#endif