blob: ec2870b44c1f920b734a4bccaf09824e99250742 [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. Biederman038e7332012-06-14 02:31:10 -070016#include <linux/user_namespace.h>
Eric W. Biederman5f256be2007-09-12 11:50:50 +020017#include <net/net_namespace.h>
Pavel Emelyanovdec827d2008-04-15 00:36:08 -070018#include <net/netns/generic.h>
Eric W. Biederman5f256be2007-09-12 11:50:50 +020019
20/*
21 * Our network namespace constructor/destructor lists
22 */
23
24static LIST_HEAD(pernet_list);
25static struct list_head *first_device = &pernet_list;
26static DEFINE_MUTEX(net_mutex);
27
Eric W. Biederman5f256be2007-09-12 11:50:50 +020028LIST_HEAD(net_namespace_list);
Alexey Dobriyanb76a4612008-10-08 11:35:06 +020029EXPORT_SYMBOL_GPL(net_namespace_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +020030
Rustad, Mark D734b6542012-07-18 09:06:07 +000031struct net init_net = {
32 .dev_base_head = LIST_HEAD_INIT(init_net.dev_base_head),
33};
Denis V. Lunevff4b9502008-01-22 22:05:33 -080034EXPORT_SYMBOL(init_net);
Eric W. Biederman5f256be2007-09-12 11:50:50 +020035
Pavel Emelyanovdec827d2008-04-15 00:36:08 -070036#define INITIAL_NET_GEN_PTRS 13 /* +1 for len +2 for rcu_head */
37
Eric Dumazet073862b2012-01-26 00:41:38 +000038static unsigned int max_gen_ptrs = INITIAL_NET_GEN_PTRS;
39
40static struct net_generic *net_alloc_generic(void)
41{
42 struct net_generic *ng;
43 size_t generic_size = offsetof(struct net_generic, ptr[max_gen_ptrs]);
44
45 ng = kzalloc(generic_size, GFP_KERNEL);
46 if (ng)
47 ng->len = max_gen_ptrs;
48
49 return ng;
50}
51
Jiri Pirko05fceb42010-04-23 01:40:47 +000052static int net_assign_generic(struct net *net, int id, void *data)
53{
54 struct net_generic *ng, *old_ng;
55
56 BUG_ON(!mutex_is_locked(&net_mutex));
57 BUG_ON(id == 0);
58
Eric Dumazet1c877332010-10-25 03:20:11 +000059 old_ng = rcu_dereference_protected(net->gen,
60 lockdep_is_held(&net_mutex));
61 ng = old_ng;
Jiri Pirko05fceb42010-04-23 01:40:47 +000062 if (old_ng->len >= id)
63 goto assign;
64
Eric Dumazet073862b2012-01-26 00:41:38 +000065 ng = net_alloc_generic();
Jiri Pirko05fceb42010-04-23 01:40:47 +000066 if (ng == NULL)
67 return -ENOMEM;
68
69 /*
70 * Some synchronisation notes:
71 *
72 * The net_generic explores the net->gen array inside rcu
73 * read section. Besides once set the net->gen->ptr[x]
74 * pointer never changes (see rules in netns/generic.h).
75 *
76 * That said, we simply duplicate this array and schedule
77 * the old copy for kfree after a grace period.
78 */
79
Jiri Pirko05fceb42010-04-23 01:40:47 +000080 memcpy(&ng->ptr, &old_ng->ptr, old_ng->len * sizeof(void*));
81
82 rcu_assign_pointer(net->gen, ng);
Lai Jiangshan04d4dfe2011-03-18 12:06:32 +080083 kfree_rcu(old_ng, rcu);
Jiri Pirko05fceb42010-04-23 01:40:47 +000084assign:
85 ng->ptr[id - 1] = data;
86 return 0;
87}
88
Eric W. Biedermanf875bae2009-11-29 22:25:28 +000089static int ops_init(const struct pernet_operations *ops, struct net *net)
90{
Julian Anastasovb9229342012-04-16 04:43:15 +000091 int err = -ENOMEM;
92 void *data = NULL;
93
Eric W. Biedermanf875bae2009-11-29 22:25:28 +000094 if (ops->id && ops->size) {
Julian Anastasovb9229342012-04-16 04:43:15 +000095 data = kzalloc(ops->size, GFP_KERNEL);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +000096 if (!data)
Julian Anastasovb9229342012-04-16 04:43:15 +000097 goto out;
Eric W. Biedermanf875bae2009-11-29 22:25:28 +000098
99 err = net_assign_generic(net, *ops->id, data);
Julian Anastasovb9229342012-04-16 04:43:15 +0000100 if (err)
101 goto cleanup;
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000102 }
Julian Anastasovb9229342012-04-16 04:43:15 +0000103 err = 0;
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000104 if (ops->init)
Julian Anastasovb9229342012-04-16 04:43:15 +0000105 err = ops->init(net);
106 if (!err)
107 return 0;
108
109cleanup:
110 kfree(data);
111
112out:
113 return err;
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000114}
115
116static void ops_free(const struct pernet_operations *ops, struct net *net)
117{
118 if (ops->id && ops->size) {
119 int id = *ops->id;
120 kfree(net_generic(net, id));
121 }
122}
123
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000124static void ops_exit_list(const struct pernet_operations *ops,
125 struct list_head *net_exit_list)
126{
127 struct net *net;
128 if (ops->exit) {
129 list_for_each_entry(net, net_exit_list, exit_list)
130 ops->exit(net);
131 }
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000132 if (ops->exit_batch)
133 ops->exit_batch(net_exit_list);
134}
135
136static void ops_free_list(const struct pernet_operations *ops,
137 struct list_head *net_exit_list)
138{
139 struct net *net;
140 if (ops->size && ops->id) {
141 list_for_each_entry(net, net_exit_list, exit_list)
142 ops_free(ops, net);
143 }
144}
145
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700146/*
147 * setup_net runs the initializers for the network namespace object.
148 */
Eric W. Biederman038e7332012-06-14 02:31:10 -0700149static __net_init int setup_net(struct net *net, struct user_namespace *user_ns)
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700150{
151 /* Must be called with net_mutex held */
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000152 const struct pernet_operations *ops, *saved_ops;
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800153 int error = 0;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000154 LIST_HEAD(net_exit_list);
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700155
156 atomic_set(&net->count, 1);
Al Viroa685e082011-06-08 21:13:01 -0400157 atomic_set(&net->passive, 1);
Thomas Graf4e985ad2011-06-21 03:11:20 +0000158 net->dev_base_seq = 1;
Eric W. Biederman038e7332012-06-14 02:31:10 -0700159 net->user_ns = user_ns;
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800160
Denis V. Lunev5d1e4462008-04-16 01:58:04 -0700161#ifdef NETNS_REFCNT_DEBUG
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700162 atomic_set(&net->use_count, 0);
Denis V. Lunev5d1e4462008-04-16 01:58:04 -0700163#endif
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700164
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700165 list_for_each_entry(ops, &pernet_list, list) {
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000166 error = ops_init(ops, net);
167 if (error < 0)
168 goto out_undo;
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700169 }
170out:
171 return error;
172
173out_undo:
174 /* Walk through the list backwards calling the exit functions
175 * for the pernet modules whose init functions did not fail.
176 */
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000177 list_add(&net->exit_list, &net_exit_list);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000178 saved_ops = ops;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000179 list_for_each_entry_continue_reverse(ops, &pernet_list, list)
180 ops_exit_list(ops, &net_exit_list);
181
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000182 ops = saved_ops;
183 list_for_each_entry_continue_reverse(ops, &pernet_list, list)
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000184 ops_free_list(ops, &net_exit_list);
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700185
186 rcu_barrier();
187 goto out;
188}
189
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800190
Clemens Nossebe47d42009-02-23 15:37:35 -0800191#ifdef CONFIG_NET_NS
192static struct kmem_cache *net_cachep;
193static struct workqueue_struct *netns_wq;
194
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200195static struct net *net_alloc(void)
196{
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800197 struct net *net = NULL;
198 struct net_generic *ng;
199
200 ng = net_alloc_generic();
201 if (!ng)
202 goto out;
203
204 net = kmem_cache_zalloc(net_cachep, GFP_KERNEL);
205 if (!net)
206 goto out_free;
207
208 rcu_assign_pointer(net->gen, ng);
209out:
210 return net;
211
212out_free:
213 kfree(ng);
214 goto out;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200215}
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200216
Johann Felix Soden45a19b02007-11-07 01:30:30 -0800217static void net_free(struct net *net)
218{
Denis V. Lunev5d1e4462008-04-16 01:58:04 -0700219#ifdef NETNS_REFCNT_DEBUG
Johann Felix Soden45a19b02007-11-07 01:30:30 -0800220 if (unlikely(atomic_read(&net->use_count) != 0)) {
Joe Perchese005d192012-05-16 19:58:40 +0000221 pr_emerg("network namespace not free! Usage: %d\n",
222 atomic_read(&net->use_count));
Johann Felix Soden45a19b02007-11-07 01:30:30 -0800223 return;
224 }
Denis V. Lunev5d1e4462008-04-16 01:58:04 -0700225#endif
Alexey Dobriyan4ef079c2008-10-14 22:54:48 -0700226 kfree(net->gen);
Johann Felix Soden45a19b02007-11-07 01:30:30 -0800227 kmem_cache_free(net_cachep, net);
228}
229
Al Viroa685e082011-06-08 21:13:01 -0400230void net_drop_ns(void *p)
231{
232 struct net *ns = p;
233 if (ns && atomic_dec_and_test(&ns->passive))
234 net_free(ns);
235}
236
Eric W. Biederman038e7332012-06-14 02:31:10 -0700237struct net *copy_net_ns(unsigned long flags,
238 struct user_namespace *user_ns, struct net *old_net)
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700239{
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700240 struct net *net;
241 int rv;
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700242
Rob Landley911cb192011-04-15 02:26:25 +0000243 if (!(flags & CLONE_NEWNET))
244 return get_net(old_net);
245
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700246 net = net_alloc();
247 if (!net)
248 return ERR_PTR(-ENOMEM);
Eric W. Biederman038e7332012-06-14 02:31:10 -0700249
250 get_user_ns(user_ns);
251
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700252 mutex_lock(&net_mutex);
Eric W. Biederman038e7332012-06-14 02:31:10 -0700253 rv = setup_net(net, user_ns);
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700254 if (rv == 0) {
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800255 rtnl_lock();
Johannes Berg11a28d32009-07-10 09:51:33 +0000256 list_add_tail_rcu(&net->list, &net_namespace_list);
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800257 rtnl_unlock();
258 }
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700259 mutex_unlock(&net_mutex);
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700260 if (rv < 0) {
Eric W. Biederman038e7332012-06-14 02:31:10 -0700261 put_user_ns(user_ns);
Al Viroa685e082011-06-08 21:13:01 -0400262 net_drop_ns(net);
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700263 return ERR_PTR(rv);
264 }
265 return net;
266}
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800267
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000268static DEFINE_SPINLOCK(cleanup_list_lock);
269static LIST_HEAD(cleanup_list); /* Must hold cleanup_list_lock to touch */
270
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200271static void cleanup_net(struct work_struct *work)
272{
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000273 const struct pernet_operations *ops;
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000274 struct net *net, *tmp;
275 LIST_HEAD(net_kill_list);
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000276 LIST_HEAD(net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200277
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000278 /* Atomically snapshot the list of namespaces to cleanup */
279 spin_lock_irq(&cleanup_list_lock);
280 list_replace_init(&cleanup_list, &net_kill_list);
281 spin_unlock_irq(&cleanup_list_lock);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200282
283 mutex_lock(&net_mutex);
284
285 /* Don't let anyone else find us. */
Eric W. Biedermanf4618d32007-09-26 22:40:08 -0700286 rtnl_lock();
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000287 list_for_each_entry(net, &net_kill_list, cleanup_list) {
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000288 list_del_rcu(&net->list);
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000289 list_add_tail(&net->exit_list, &net_exit_list);
290 }
Eric W. Biedermanf4618d32007-09-26 22:40:08 -0700291 rtnl_unlock();
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200292
Johannes Berg11a28d32009-07-10 09:51:33 +0000293 /*
294 * Another CPU might be rcu-iterating the list, wait for it.
295 * This needs to be before calling the exit() notifiers, so
296 * the rcu_barrier() below isn't sufficient alone.
297 */
298 synchronize_rcu();
299
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200300 /* Run all of the network namespace exit methods */
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000301 list_for_each_entry_reverse(ops, &pernet_list, list)
302 ops_exit_list(ops, &net_exit_list);
303
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000304 /* Free the net generic variables */
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000305 list_for_each_entry_reverse(ops, &pernet_list, list)
306 ops_free_list(ops, &net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200307
308 mutex_unlock(&net_mutex);
309
310 /* Ensure there are no outstanding rcu callbacks using this
311 * network namespace.
312 */
313 rcu_barrier();
314
315 /* Finally it is safe to free my network namespace structure */
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000316 list_for_each_entry_safe(net, tmp, &net_exit_list, exit_list) {
317 list_del_init(&net->exit_list);
Eric W. Biederman038e7332012-06-14 02:31:10 -0700318 put_user_ns(net->user_ns);
Al Viroa685e082011-06-08 21:13:01 -0400319 net_drop_ns(net);
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000320 }
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200321}
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000322static DECLARE_WORK(net_cleanup_work, cleanup_net);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200323
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200324void __put_net(struct net *net)
325{
326 /* Cleanup the network namespace in process context */
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000327 unsigned long flags;
328
329 spin_lock_irqsave(&cleanup_list_lock, flags);
330 list_add(&net->cleanup_list, &cleanup_list);
331 spin_unlock_irqrestore(&cleanup_list_lock, flags);
332
333 queue_work(netns_wq, &net_cleanup_work);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200334}
335EXPORT_SYMBOL_GPL(__put_net);
336
Stephen Rothwell956c9202011-05-12 13:51:13 +1000337struct net *get_net_ns_by_fd(int fd)
338{
339 struct proc_inode *ei;
340 struct file *file;
341 struct net *net;
342
Stephen Rothwell956c9202011-05-12 13:51:13 +1000343 file = proc_ns_fget(fd);
Al Viroc316e6a2011-06-05 00:37:35 +0000344 if (IS_ERR(file))
345 return ERR_CAST(file);
Stephen Rothwell956c9202011-05-12 13:51:13 +1000346
347 ei = PROC_I(file->f_dentry->d_inode);
Al Viroc316e6a2011-06-05 00:37:35 +0000348 if (ei->ns_ops == &netns_operations)
349 net = get_net(ei->ns);
350 else
351 net = ERR_PTR(-EINVAL);
Stephen Rothwell956c9202011-05-12 13:51:13 +1000352
Al Viroc316e6a2011-06-05 00:37:35 +0000353 fput(file);
Stephen Rothwell956c9202011-05-12 13:51:13 +1000354 return net;
355}
356
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700357#else
Stephen Rothwell956c9202011-05-12 13:51:13 +1000358struct net *get_net_ns_by_fd(int fd)
359{
360 return ERR_PTR(-EINVAL);
361}
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700362#endif
Eric W. Biederman9dd776b2007-09-26 22:04:26 -0700363
Johannes Berg30ffee82009-07-10 09:51:35 +0000364struct net *get_net_ns_by_pid(pid_t pid)
365{
366 struct task_struct *tsk;
367 struct net *net;
368
369 /* Lookup the network namespace */
370 net = ERR_PTR(-ESRCH);
371 rcu_read_lock();
372 tsk = find_task_by_vpid(pid);
373 if (tsk) {
374 struct nsproxy *nsproxy;
375 nsproxy = task_nsproxy(tsk);
376 if (nsproxy)
377 net = get_net(nsproxy->net_ns);
378 }
379 rcu_read_unlock();
380 return net;
381}
382EXPORT_SYMBOL_GPL(get_net_ns_by_pid);
383
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200384static int __init net_ns_init(void)
385{
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800386 struct net_generic *ng;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200387
Pavel Emelyanovd57a9212007-11-01 00:46:50 -0700388#ifdef CONFIG_NET_NS
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200389 net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
390 SMP_CACHE_BYTES,
391 SLAB_PANIC, NULL);
Benjamin Thery3ef13552007-11-19 23:18:16 -0800392
393 /* Create workqueue for cleanup */
394 netns_wq = create_singlethread_workqueue("netns");
395 if (!netns_wq)
396 panic("Could not create netns workq");
Pavel Emelyanovd57a9212007-11-01 00:46:50 -0700397#endif
Benjamin Thery3ef13552007-11-19 23:18:16 -0800398
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800399 ng = net_alloc_generic();
400 if (!ng)
401 panic("Could not allocate generic netns");
402
403 rcu_assign_pointer(init_net.gen, ng);
404
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200405 mutex_lock(&net_mutex);
Eric W. Biederman038e7332012-06-14 02:31:10 -0700406 if (setup_net(&init_net, &init_user_ns))
Stephen Hemmingerca0f3112009-05-21 15:10:31 -0700407 panic("Could not setup the initial network namespace");
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200408
Eric W. Biedermanf4618d32007-09-26 22:40:08 -0700409 rtnl_lock();
Johannes Berg11a28d32009-07-10 09:51:33 +0000410 list_add_tail_rcu(&init_net.list, &net_namespace_list);
Eric W. Biedermanf4618d32007-09-26 22:40:08 -0700411 rtnl_unlock();
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200412
413 mutex_unlock(&net_mutex);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200414
415 return 0;
416}
417
418pure_initcall(net_ns_init);
419
Denis V. Luneved160e82007-11-13 03:23:21 -0800420#ifdef CONFIG_NET_NS
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000421static int __register_pernet_operations(struct list_head *list,
422 struct pernet_operations *ops)
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200423{
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000424 struct net *net;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200425 int error;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000426 LIST_HEAD(net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200427
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200428 list_add_tail(&ops->list, list);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000429 if (ops->init || (ops->id && ops->size)) {
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700430 for_each_net(net) {
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000431 error = ops_init(ops, net);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200432 if (error)
433 goto out_undo;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000434 list_add_tail(&net->exit_list, &net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200435 }
436 }
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700437 return 0;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200438
439out_undo:
440 /* If I have an error cleanup all namespaces I initialized */
441 list_del(&ops->list);
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000442 ops_exit_list(ops, &net_exit_list);
443 ops_free_list(ops, &net_exit_list);
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700444 return error;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200445}
446
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000447static void __unregister_pernet_operations(struct pernet_operations *ops)
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200448{
449 struct net *net;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000450 LIST_HEAD(net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200451
452 list_del(&ops->list);
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000453 for_each_net(net)
454 list_add_tail(&net->exit_list, &net_exit_list);
455 ops_exit_list(ops, &net_exit_list);
456 ops_free_list(ops, &net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200457}
458
Denis V. Luneved160e82007-11-13 03:23:21 -0800459#else
460
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000461static int __register_pernet_operations(struct list_head *list,
462 struct pernet_operations *ops)
463{
Julian Anastasovb9229342012-04-16 04:43:15 +0000464 return ops_init(ops, &init_net);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000465}
466
467static void __unregister_pernet_operations(struct pernet_operations *ops)
468{
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000469 LIST_HEAD(net_exit_list);
470 list_add(&init_net.exit_list, &net_exit_list);
471 ops_exit_list(ops, &net_exit_list);
472 ops_free_list(ops, &net_exit_list);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000473}
474
475#endif /* CONFIG_NET_NS */
476
477static DEFINE_IDA(net_generic_ids);
478
Denis V. Luneved160e82007-11-13 03:23:21 -0800479static int register_pernet_operations(struct list_head *list,
480 struct pernet_operations *ops)
481{
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000482 int error;
483
484 if (ops->id) {
485again:
486 error = ida_get_new_above(&net_generic_ids, 1, ops->id);
487 if (error < 0) {
488 if (error == -EAGAIN) {
489 ida_pre_get(&net_generic_ids, GFP_KERNEL);
490 goto again;
491 }
492 return error;
493 }
Eric Dumazet073862b2012-01-26 00:41:38 +0000494 max_gen_ptrs = max_t(unsigned int, max_gen_ptrs, *ops->id);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000495 }
496 error = __register_pernet_operations(list, ops);
Eric W. Biederman3a765ed2009-12-03 02:29:06 +0000497 if (error) {
498 rcu_barrier();
499 if (ops->id)
500 ida_remove(&net_generic_ids, *ops->id);
501 }
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000502
503 return error;
Denis V. Luneved160e82007-11-13 03:23:21 -0800504}
505
506static void unregister_pernet_operations(struct pernet_operations *ops)
507{
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000508
509 __unregister_pernet_operations(ops);
Eric W. Biederman3a765ed2009-12-03 02:29:06 +0000510 rcu_barrier();
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000511 if (ops->id)
512 ida_remove(&net_generic_ids, *ops->id);
Denis V. Luneved160e82007-11-13 03:23:21 -0800513}
Pavel Emelyanovc93cf612008-04-15 00:35:23 -0700514
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200515/**
516 * register_pernet_subsys - register a network namespace subsystem
517 * @ops: pernet operations structure for the subsystem
518 *
519 * Register a subsystem which has init and exit functions
520 * that are called when network namespaces are created and
521 * destroyed respectively.
522 *
523 * When registered all network namespace init functions are
524 * called for every existing network namespace. Allowing kernel
525 * modules to have a race free view of the set of network namespaces.
526 *
527 * When a new network namespace is created all of the init
528 * methods are called in the order in which they were registered.
529 *
530 * When a network namespace is destroyed all of the exit methods
531 * are called in the reverse of the order with which they were
532 * registered.
533 */
534int register_pernet_subsys(struct pernet_operations *ops)
535{
536 int error;
537 mutex_lock(&net_mutex);
538 error = register_pernet_operations(first_device, ops);
539 mutex_unlock(&net_mutex);
540 return error;
541}
542EXPORT_SYMBOL_GPL(register_pernet_subsys);
543
544/**
545 * unregister_pernet_subsys - unregister a network namespace subsystem
546 * @ops: pernet operations structure to manipulate
547 *
548 * Remove the pernet operations structure from the list to be
Oliver Pinter53379e52008-02-03 17:56:48 +0200549 * used when network namespaces are created or destroyed. In
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200550 * addition run the exit method for all existing network
551 * namespaces.
552 */
Jiri Pirkob3c981d2010-04-25 00:49:56 -0700553void unregister_pernet_subsys(struct pernet_operations *ops)
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200554{
555 mutex_lock(&net_mutex);
Jiri Pirkob3c981d2010-04-25 00:49:56 -0700556 unregister_pernet_operations(ops);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200557 mutex_unlock(&net_mutex);
558}
559EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
560
561/**
562 * register_pernet_device - register a network namespace device
563 * @ops: pernet operations structure for the subsystem
564 *
565 * Register a device which has init and exit functions
566 * that are called when network namespaces are created and
567 * destroyed respectively.
568 *
569 * When registered all network namespace init functions are
570 * called for every existing network namespace. Allowing kernel
571 * modules to have a race free view of the set of network namespaces.
572 *
573 * When a new network namespace is created all of the init
574 * methods are called in the order in which they were registered.
575 *
576 * When a network namespace is destroyed all of the exit methods
577 * are called in the reverse of the order with which they were
578 * registered.
579 */
580int register_pernet_device(struct pernet_operations *ops)
581{
582 int error;
583 mutex_lock(&net_mutex);
584 error = register_pernet_operations(&pernet_list, ops);
585 if (!error && (first_device == &pernet_list))
586 first_device = &ops->list;
587 mutex_unlock(&net_mutex);
588 return error;
589}
590EXPORT_SYMBOL_GPL(register_pernet_device);
591
592/**
593 * unregister_pernet_device - unregister a network namespace netdevice
594 * @ops: pernet operations structure to manipulate
595 *
596 * Remove the pernet operations structure from the list to be
Oliver Pinter53379e52008-02-03 17:56:48 +0200597 * used when network namespaces are created or destroyed. In
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200598 * addition run the exit method for all existing network
599 * namespaces.
600 */
601void unregister_pernet_device(struct pernet_operations *ops)
602{
603 mutex_lock(&net_mutex);
604 if (&ops->list == first_device)
605 first_device = first_device->next;
606 unregister_pernet_operations(ops);
607 mutex_unlock(&net_mutex);
608}
609EXPORT_SYMBOL_GPL(unregister_pernet_device);
Eric W. Biederman13b6f572010-03-07 18:14:23 -0800610
611#ifdef CONFIG_NET_NS
612static void *netns_get(struct task_struct *task)
613{
Eric W. Biedermanf0630522011-05-04 17:51:50 -0700614 struct net *net = NULL;
615 struct nsproxy *nsproxy;
616
Eric W. Biederman13b6f572010-03-07 18:14:23 -0800617 rcu_read_lock();
Eric W. Biedermanf0630522011-05-04 17:51:50 -0700618 nsproxy = task_nsproxy(task);
619 if (nsproxy)
620 net = get_net(nsproxy->net_ns);
Eric W. Biederman13b6f572010-03-07 18:14:23 -0800621 rcu_read_unlock();
Eric W. Biedermanf0630522011-05-04 17:51:50 -0700622
Eric W. Biederman13b6f572010-03-07 18:14:23 -0800623 return net;
624}
625
626static void netns_put(void *ns)
627{
628 put_net(ns);
629}
630
631static int netns_install(struct nsproxy *nsproxy, void *ns)
632{
Eric W. Biederman142e1d12012-07-26 01:13:20 -0700633 struct net *net = ns;
634
635 if (!ns_capable(net->user_ns, CAP_SYS_ADMIN))
636 return -EPERM;
637
Eric W. Biederman13b6f572010-03-07 18:14:23 -0800638 put_net(nsproxy->net_ns);
Eric W. Biederman142e1d12012-07-26 01:13:20 -0700639 nsproxy->net_ns = get_net(net);
Eric W. Biederman13b6f572010-03-07 18:14:23 -0800640 return 0;
641}
642
643const struct proc_ns_operations netns_operations = {
644 .name = "net",
645 .type = CLONE_NEWNET,
646 .get = netns_get,
647 .put = netns_put,
648 .install = netns_install,
649};
650#endif