blob: f8453c438798992dec134ea18be17f0b70a04674 [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>
David Howells0bb80f22013-04-12 01:50:06 +010013#include <linux/fs.h>
14#include <linux/proc_ns.h>
Eric W. Biedermanf0630522011-05-04 17:51:50 -070015#include <linux/file.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040016#include <linux/export.h>
Eric W. Biederman038e7332012-06-14 02:31:10 -070017#include <linux/user_namespace.h>
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +010018#include <linux/net_namespace.h>
Ingo Molnarf719ff9b2017-02-06 10:57:33 +010019#include <linux/sched/task.h>
20
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +010021#include <net/sock.h>
22#include <net/netlink.h>
Eric W. Biederman5f256be2007-09-12 11:50:50 +020023#include <net/net_namespace.h>
Pavel Emelyanovdec827d2008-04-15 00:36:08 -070024#include <net/netns/generic.h>
Eric W. Biederman5f256be2007-09-12 11:50:50 +020025
26/*
27 * Our network namespace constructor/destructor lists
28 */
29
30static LIST_HEAD(pernet_list);
31static struct list_head *first_device = &pernet_list;
Cong Wang200b9162014-05-12 15:11:20 -070032DEFINE_MUTEX(net_mutex);
Eric W. Biederman5f256be2007-09-12 11:50:50 +020033
Eric W. Biederman5f256be2007-09-12 11:50:50 +020034LIST_HEAD(net_namespace_list);
Alexey Dobriyanb76a4612008-10-08 11:35:06 +020035EXPORT_SYMBOL_GPL(net_namespace_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +020036
Rustad, Mark D734b6542012-07-18 09:06:07 +000037struct net init_net = {
Kirill Tkhai273c28b2018-01-12 18:28:31 +030038 .count = REFCOUNT_INIT(1),
David Howellsb5082df2017-04-27 22:40:23 +010039 .dev_base_head = LIST_HEAD_INIT(init_net.dev_base_head),
Rustad, Mark D734b6542012-07-18 09:06:07 +000040};
Denis V. Lunevff4b9502008-01-22 22:05:33 -080041EXPORT_SYMBOL(init_net);
Eric W. Biederman5f256be2007-09-12 11:50:50 +020042
Dmitry Torokhovf8c46cb2016-08-10 14:36:00 -070043static bool init_net_initialized;
Kirill Tkhai1a57feb2018-02-13 12:26:23 +030044/*
45 * net_sem: protects: pernet_list, net_generic_ids,
46 * init_net_initialized and first_device pointer.
47 */
48DECLARE_RWSEM(net_sem);
Dmitry Torokhovf8c46cb2016-08-10 14:36:00 -070049
Alexey Dobriyan6af2d5f2016-12-02 04:21:32 +030050#define MIN_PERNET_OPS_ID \
51 ((sizeof(struct net_generic) + sizeof(void *) - 1) / sizeof(void *))
52
Pavel Emelyanovdec827d2008-04-15 00:36:08 -070053#define INITIAL_NET_GEN_PTRS 13 /* +1 for len +2 for rcu_head */
54
Eric Dumazet073862b2012-01-26 00:41:38 +000055static unsigned int max_gen_ptrs = INITIAL_NET_GEN_PTRS;
56
57static struct net_generic *net_alloc_generic(void)
58{
59 struct net_generic *ng;
Alexey Dobriyan6af2d5f2016-12-02 04:21:32 +030060 unsigned int generic_size = offsetof(struct net_generic, ptr[max_gen_ptrs]);
Eric Dumazet073862b2012-01-26 00:41:38 +000061
62 ng = kzalloc(generic_size, GFP_KERNEL);
63 if (ng)
Alexey Dobriyan9bfc7b92016-12-02 04:12:58 +030064 ng->s.len = max_gen_ptrs;
Eric Dumazet073862b2012-01-26 00:41:38 +000065
66 return ng;
67}
68
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030069static int net_assign_generic(struct net *net, unsigned int id, void *data)
Jiri Pirko05fceb42010-04-23 01:40:47 +000070{
71 struct net_generic *ng, *old_ng;
72
73 BUG_ON(!mutex_is_locked(&net_mutex));
Alexey Dobriyan6af2d5f2016-12-02 04:21:32 +030074 BUG_ON(id < MIN_PERNET_OPS_ID);
Jiri Pirko05fceb42010-04-23 01:40:47 +000075
Eric Dumazet1c877332010-10-25 03:20:11 +000076 old_ng = rcu_dereference_protected(net->gen,
77 lockdep_is_held(&net_mutex));
Alexey Dobriyan6af2d5f2016-12-02 04:21:32 +030078 if (old_ng->s.len > id) {
79 old_ng->ptr[id] = data;
Alexey Dobriyan1a9a05922016-12-02 04:11:34 +030080 return 0;
81 }
Jiri Pirko05fceb42010-04-23 01:40:47 +000082
Eric Dumazet073862b2012-01-26 00:41:38 +000083 ng = net_alloc_generic();
Jiri Pirko05fceb42010-04-23 01:40:47 +000084 if (ng == NULL)
85 return -ENOMEM;
86
87 /*
88 * Some synchronisation notes:
89 *
90 * The net_generic explores the net->gen array inside rcu
91 * read section. Besides once set the net->gen->ptr[x]
92 * pointer never changes (see rules in netns/generic.h).
93 *
94 * That said, we simply duplicate this array and schedule
95 * the old copy for kfree after a grace period.
96 */
97
Alexey Dobriyan6af2d5f2016-12-02 04:21:32 +030098 memcpy(&ng->ptr[MIN_PERNET_OPS_ID], &old_ng->ptr[MIN_PERNET_OPS_ID],
99 (old_ng->s.len - MIN_PERNET_OPS_ID) * sizeof(void *));
100 ng->ptr[id] = data;
Jiri Pirko05fceb42010-04-23 01:40:47 +0000101
102 rcu_assign_pointer(net->gen, ng);
Alexey Dobriyan9bfc7b92016-12-02 04:12:58 +0300103 kfree_rcu(old_ng, s.rcu);
Jiri Pirko05fceb42010-04-23 01:40:47 +0000104 return 0;
105}
106
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000107static int ops_init(const struct pernet_operations *ops, struct net *net)
108{
Julian Anastasovb9229342012-04-16 04:43:15 +0000109 int err = -ENOMEM;
110 void *data = NULL;
111
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000112 if (ops->id && ops->size) {
Julian Anastasovb9229342012-04-16 04:43:15 +0000113 data = kzalloc(ops->size, GFP_KERNEL);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000114 if (!data)
Julian Anastasovb9229342012-04-16 04:43:15 +0000115 goto out;
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000116
117 err = net_assign_generic(net, *ops->id, data);
Julian Anastasovb9229342012-04-16 04:43:15 +0000118 if (err)
119 goto cleanup;
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000120 }
Julian Anastasovb9229342012-04-16 04:43:15 +0000121 err = 0;
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000122 if (ops->init)
Julian Anastasovb9229342012-04-16 04:43:15 +0000123 err = ops->init(net);
124 if (!err)
125 return 0;
126
127cleanup:
128 kfree(data);
129
130out:
131 return err;
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000132}
133
134static void ops_free(const struct pernet_operations *ops, struct net *net)
135{
136 if (ops->id && ops->size) {
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +0300137 kfree(net_generic(net, *ops->id));
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000138 }
139}
140
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000141static void ops_exit_list(const struct pernet_operations *ops,
142 struct list_head *net_exit_list)
143{
144 struct net *net;
145 if (ops->exit) {
146 list_for_each_entry(net, net_exit_list, exit_list)
147 ops->exit(net);
148 }
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000149 if (ops->exit_batch)
150 ops->exit_batch(net_exit_list);
151}
152
153static void ops_free_list(const struct pernet_operations *ops,
154 struct list_head *net_exit_list)
155{
156 struct net *net;
157 if (ops->size && ops->id) {
158 list_for_each_entry(net, net_exit_list, exit_list)
159 ops_free(ops, net);
160 }
161}
162
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200163/* should be called with nsid_lock held */
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100164static int alloc_netid(struct net *net, struct net *peer, int reqid)
165{
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200166 int min = 0, max = 0;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100167
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100168 if (reqid >= 0) {
169 min = reqid;
170 max = reqid + 1;
171 }
172
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200173 return idr_alloc(&net->netns_ids, peer, min, max, GFP_ATOMIC);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100174}
175
176/* This function is used by idr_for_each(). If net is equal to peer, the
177 * function returns the id so that idr_for_each() stops. Because we cannot
178 * returns the id 0 (idr_for_each() will not stop), we return the magic value
179 * NET_ID_ZERO (-1) for it.
180 */
181#define NET_ID_ZERO -1
182static int net_eq_idr(int id, void *net, void *peer)
183{
184 if (net_eq(net, peer))
185 return id ? : NET_ID_ZERO;
186 return 0;
187}
188
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200189/* Should be called with nsid_lock held. If a new id is assigned, the bool alloc
190 * is set to true, thus the caller knows that the new id must be notified via
191 * rtnl.
192 */
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200193static int __peernet2id_alloc(struct net *net, struct net *peer, bool *alloc)
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100194{
195 int id = idr_for_each(&net->netns_ids, net_eq_idr, peer);
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200196 bool alloc_it = *alloc;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100197
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200198 *alloc = false;
199
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100200 /* Magic value for id 0. */
201 if (id == NET_ID_ZERO)
202 return 0;
203 if (id > 0)
204 return id;
205
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200206 if (alloc_it) {
Nicolas Dichtel109582af2015-05-07 11:02:47 +0200207 id = alloc_netid(net, peer, -1);
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200208 *alloc = true;
Nicolas Dichtel109582af2015-05-07 11:02:47 +0200209 return id >= 0 ? id : NETNSA_NSID_NOT_ASSIGNED;
210 }
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100211
Nicolas Dichtel109582af2015-05-07 11:02:47 +0200212 return NETNSA_NSID_NOT_ASSIGNED;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100213}
214
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200215/* should be called with nsid_lock held */
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200216static int __peernet2id(struct net *net, struct net *peer)
217{
218 bool no = false;
219
220 return __peernet2id_alloc(net, peer, &no);
221}
222
223static void rtnl_net_notifyid(struct net *net, int cmd, int id);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100224/* This function returns the id of a peer netns. If no id is assigned, one will
225 * be allocated and returned.
226 */
Nicolas Dichtel7a0877d2015-05-07 11:02:49 +0200227int peernet2id_alloc(struct net *net, struct net *peer)
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100228{
Kirill Tkhai0c06bea2018-01-16 12:31:41 +0300229 bool alloc = false, alive = false;
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200230 int id;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100231
Kirill Tkhai273c28b2018-01-12 18:28:31 +0300232 if (refcount_read(&net->count) == 0)
WANG Congcfc44a42016-11-16 10:27:02 -0800233 return NETNSA_NSID_NOT_ASSIGNED;
Paul Moorefba143c2016-11-29 16:57:48 -0500234 spin_lock_bh(&net->nsid_lock);
Kirill Tkhai0c06bea2018-01-16 12:31:41 +0300235 /*
236 * When peer is obtained from RCU lists, we may race with
237 * its cleanup. Check whether it's alive, and this guarantees
238 * we never hash a peer back to net->netns_ids, after it has
239 * just been idr_remove()'d from there in cleanup_net().
240 */
241 if (maybe_get_net(peer))
242 alive = alloc = true;
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200243 id = __peernet2id_alloc(net, peer, &alloc);
Paul Moorefba143c2016-11-29 16:57:48 -0500244 spin_unlock_bh(&net->nsid_lock);
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200245 if (alloc && id >= 0)
246 rtnl_net_notifyid(net, RTM_NEWNSID, id);
Kirill Tkhai0c06bea2018-01-16 12:31:41 +0300247 if (alive)
248 put_net(peer);
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200249 return id;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100250}
Jiri Benc7cbebc82017-11-02 17:04:36 -0200251EXPORT_SYMBOL_GPL(peernet2id_alloc);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100252
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200253/* This function returns, if assigned, the id of a peer netns. */
Nicolas Dichtel59324cf2015-05-07 11:02:53 +0200254int peernet2id(struct net *net, struct net *peer)
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200255{
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200256 int id;
257
Paul Moorefba143c2016-11-29 16:57:48 -0500258 spin_lock_bh(&net->nsid_lock);
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200259 id = __peernet2id(net, peer);
Paul Moorefba143c2016-11-29 16:57:48 -0500260 spin_unlock_bh(&net->nsid_lock);
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200261 return id;
262}
WANG Cong38f507f2016-09-01 21:53:44 -0700263EXPORT_SYMBOL(peernet2id);
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200264
Nicolas Dichtel59324cf2015-05-07 11:02:53 +0200265/* This function returns true is the peer netns has an id assigned into the
266 * current netns.
267 */
268bool peernet_has_id(struct net *net, struct net *peer)
269{
270 return peernet2id(net, peer) >= 0;
271}
272
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100273struct net *get_net_ns_by_id(struct net *net, int id)
274{
275 struct net *peer;
276
277 if (id < 0)
278 return NULL;
279
280 rcu_read_lock();
281 peer = idr_find(&net->netns_ids, id);
282 if (peer)
Eric W. Biederman21b59442017-12-19 11:27:56 -0600283 peer = maybe_get_net(peer);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100284 rcu_read_unlock();
285
286 return peer;
287}
288
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700289/*
290 * setup_net runs the initializers for the network namespace object.
291 */
Eric W. Biederman038e7332012-06-14 02:31:10 -0700292static __net_init int setup_net(struct net *net, struct user_namespace *user_ns)
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700293{
Kirill Tkhai1a57feb2018-02-13 12:26:23 +0300294 /* Must be called with net_sem held */
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000295 const struct pernet_operations *ops, *saved_ops;
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800296 int error = 0;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000297 LIST_HEAD(net_exit_list);
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700298
Kirill Tkhai273c28b2018-01-12 18:28:31 +0300299 refcount_set(&net->count, 1);
Reshetova, Elenac122e142017-06-30 13:08:08 +0300300 refcount_set(&net->passive, 1);
Thomas Graf4e985ad2011-06-21 03:11:20 +0000301 net->dev_base_seq = 1;
Eric W. Biederman038e7332012-06-14 02:31:10 -0700302 net->user_ns = user_ns;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100303 idr_init(&net->netns_ids);
WANG Congde133462015-05-15 14:47:32 -0700304 spin_lock_init(&net->nsid_lock);
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800305
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700306 list_for_each_entry(ops, &pernet_list, list) {
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000307 error = ops_init(ops, net);
308 if (error < 0)
309 goto out_undo;
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700310 }
Kirill Tkhai98f6c532018-02-13 12:26:02 +0300311 rtnl_lock();
312 list_add_tail_rcu(&net->list, &net_namespace_list);
313 rtnl_unlock();
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700314out:
315 return error;
316
317out_undo:
318 /* Walk through the list backwards calling the exit functions
319 * for the pernet modules whose init functions did not fail.
320 */
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000321 list_add(&net->exit_list, &net_exit_list);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000322 saved_ops = ops;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000323 list_for_each_entry_continue_reverse(ops, &pernet_list, list)
324 ops_exit_list(ops, &net_exit_list);
325
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000326 ops = saved_ops;
327 list_for_each_entry_continue_reverse(ops, &pernet_list, list)
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000328 ops_free_list(ops, &net_exit_list);
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700329
330 rcu_barrier();
331 goto out;
332}
333
Roman Kapl7c3f18752017-05-24 10:22:22 +0200334static int __net_init net_defaults_init_net(struct net *net)
335{
336 net->core.sysctl_somaxconn = SOMAXCONN;
337 return 0;
338}
339
340static struct pernet_operations net_defaults_ops = {
341 .init = net_defaults_init_net,
342};
343
344static __init int net_defaults_init(void)
345{
346 if (register_pernet_subsys(&net_defaults_ops))
347 panic("Cannot initialize net default settings");
348
349 return 0;
350}
351
352core_initcall(net_defaults_init);
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800353
Clemens Nossebe47d42009-02-23 15:37:35 -0800354#ifdef CONFIG_NET_NS
Arnd Bergmann2ed6afd2016-09-23 18:06:12 +0200355static struct ucounts *inc_net_namespaces(struct user_namespace *ns)
356{
357 return inc_ucount(ns, current_euid(), UCOUNT_NET_NAMESPACES);
358}
359
360static void dec_net_namespaces(struct ucounts *ucounts)
361{
362 dec_ucount(ucounts, UCOUNT_NET_NAMESPACES);
363}
364
Clemens Nossebe47d42009-02-23 15:37:35 -0800365static struct kmem_cache *net_cachep;
366static struct workqueue_struct *netns_wq;
367
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200368static struct net *net_alloc(void)
369{
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800370 struct net *net = NULL;
371 struct net_generic *ng;
372
373 ng = net_alloc_generic();
374 if (!ng)
375 goto out;
376
377 net = kmem_cache_zalloc(net_cachep, GFP_KERNEL);
378 if (!net)
379 goto out_free;
380
381 rcu_assign_pointer(net->gen, ng);
382out:
383 return net;
384
385out_free:
386 kfree(ng);
387 goto out;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200388}
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200389
Johann Felix Soden45a19b02007-11-07 01:30:30 -0800390static void net_free(struct net *net)
391{
Eric Dumazet416c51e2014-09-09 08:24:53 -0700392 kfree(rcu_access_pointer(net->gen));
Johann Felix Soden45a19b02007-11-07 01:30:30 -0800393 kmem_cache_free(net_cachep, net);
394}
395
Al Viroa685e082011-06-08 21:13:01 -0400396void net_drop_ns(void *p)
397{
398 struct net *ns = p;
Reshetova, Elenac122e142017-06-30 13:08:08 +0300399 if (ns && refcount_dec_and_test(&ns->passive))
Al Viroa685e082011-06-08 21:13:01 -0400400 net_free(ns);
401}
402
Eric W. Biederman038e7332012-06-14 02:31:10 -0700403struct net *copy_net_ns(unsigned long flags,
404 struct user_namespace *user_ns, struct net *old_net)
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700405{
Eric W. Biederman70328662016-08-08 14:33:23 -0500406 struct ucounts *ucounts;
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700407 struct net *net;
408 int rv;
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700409
Rob Landley911cb192011-04-15 02:26:25 +0000410 if (!(flags & CLONE_NEWNET))
411 return get_net(old_net);
412
Eric W. Biederman70328662016-08-08 14:33:23 -0500413 ucounts = inc_net_namespaces(user_ns);
414 if (!ucounts)
Eric W. Biedermandf75e772016-09-22 13:08:36 -0500415 return ERR_PTR(-ENOSPC);
Eric W. Biederman70328662016-08-08 14:33:23 -0500416
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700417 net = net_alloc();
Eric W. Biederman70328662016-08-08 14:33:23 -0500418 if (!net) {
Kirill Tkhai5ba049a2018-02-13 12:26:13 +0300419 rv = -ENOMEM;
420 goto dec_ucounts;
Eric W. Biederman70328662016-08-08 14:33:23 -0500421 }
Kirill Tkhai5ba049a2018-02-13 12:26:13 +0300422 refcount_set(&net->passive, 1);
423 net->ucounts = ucounts;
Eric W. Biederman038e7332012-06-14 02:31:10 -0700424 get_user_ns(user_ns);
425
Kirill Tkhai1a57feb2018-02-13 12:26:23 +0300426 rv = down_read_killable(&net_sem);
Kirill Tkhai5ba049a2018-02-13 12:26:13 +0300427 if (rv < 0)
428 goto put_userns;
Kirill Tkhai1a57feb2018-02-13 12:26:23 +0300429 rv = mutex_lock_killable(&net_mutex);
430 if (rv < 0)
431 goto up_read;
Eric W. Biederman038e7332012-06-14 02:31:10 -0700432 rv = setup_net(net, user_ns);
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700433 mutex_unlock(&net_mutex);
Kirill Tkhai1a57feb2018-02-13 12:26:23 +0300434up_read:
435 up_read(&net_sem);
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700436 if (rv < 0) {
Kirill Tkhai5ba049a2018-02-13 12:26:13 +0300437put_userns:
Eric W. Biederman038e7332012-06-14 02:31:10 -0700438 put_user_ns(user_ns);
Al Viroa685e082011-06-08 21:13:01 -0400439 net_drop_ns(net);
Kirill Tkhai5ba049a2018-02-13 12:26:13 +0300440dec_ucounts:
441 dec_net_namespaces(ucounts);
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700442 return ERR_PTR(rv);
443 }
444 return net;
445}
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800446
Kirill Tkhaifb07a822018-01-19 19:14:53 +0300447static void unhash_nsid(struct net *net, struct net *last)
448{
449 struct net *tmp;
450 /* This function is only called from cleanup_net() work,
451 * and this work is the only process, that may delete
452 * a net from net_namespace_list. So, when the below
453 * is executing, the list may only grow. Thus, we do not
454 * use for_each_net_rcu() or rtnl_lock().
455 */
456 for_each_net(tmp) {
457 int id;
458
459 spin_lock_bh(&tmp->nsid_lock);
460 id = __peernet2id(tmp, net);
461 if (id >= 0)
462 idr_remove(&tmp->netns_ids, id);
463 spin_unlock_bh(&tmp->nsid_lock);
464 if (id >= 0)
465 rtnl_net_notifyid(tmp, RTM_DELNSID, id);
466 if (tmp == last)
467 break;
468 }
469 spin_lock_bh(&net->nsid_lock);
470 idr_destroy(&net->netns_ids);
471 spin_unlock_bh(&net->nsid_lock);
472}
473
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000474static DEFINE_SPINLOCK(cleanup_list_lock);
475static LIST_HEAD(cleanup_list); /* Must hold cleanup_list_lock to touch */
476
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200477static void cleanup_net(struct work_struct *work)
478{
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000479 const struct pernet_operations *ops;
Kirill Tkhaifb07a822018-01-19 19:14:53 +0300480 struct net *net, *tmp, *last;
xiao jin1818ce42014-04-25 08:50:54 +0800481 struct list_head net_kill_list;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000482 LIST_HEAD(net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200483
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000484 /* Atomically snapshot the list of namespaces to cleanup */
485 spin_lock_irq(&cleanup_list_lock);
486 list_replace_init(&cleanup_list, &net_kill_list);
487 spin_unlock_irq(&cleanup_list_lock);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200488
Kirill Tkhai1a57feb2018-02-13 12:26:23 +0300489 down_read(&net_sem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200490 mutex_lock(&net_mutex);
491
492 /* Don't let anyone else find us. */
Eric W. Biedermanf4618d32007-09-26 22:40:08 -0700493 rtnl_lock();
Kirill Tkhaifb07a822018-01-19 19:14:53 +0300494 list_for_each_entry(net, &net_kill_list, cleanup_list)
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000495 list_del_rcu(&net->list);
Kirill Tkhaifb07a822018-01-19 19:14:53 +0300496 /* Cache last net. After we unlock rtnl, no one new net
497 * added to net_namespace_list can assign nsid pointer
498 * to a net from net_kill_list (see peernet2id_alloc()).
499 * So, we skip them in unhash_nsid().
500 *
501 * Note, that unhash_nsid() does not delete nsid links
502 * between net_kill_list's nets, as they've already
503 * deleted from net_namespace_list. But, this would be
504 * useless anyway, as netns_ids are destroyed there.
505 */
506 last = list_last_entry(&net_namespace_list, struct net, list);
Eric W. Biedermanf4618d32007-09-26 22:40:08 -0700507 rtnl_unlock();
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200508
Kirill Tkhaifb07a822018-01-19 19:14:53 +0300509 list_for_each_entry(net, &net_kill_list, cleanup_list) {
510 unhash_nsid(net, last);
511 list_add_tail(&net->exit_list, &net_exit_list);
512 }
513
Johannes Berg11a28d32009-07-10 09:51:33 +0000514 /*
515 * Another CPU might be rcu-iterating the list, wait for it.
516 * This needs to be before calling the exit() notifiers, so
517 * the rcu_barrier() below isn't sufficient alone.
518 */
519 synchronize_rcu();
520
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200521 /* Run all of the network namespace exit methods */
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000522 list_for_each_entry_reverse(ops, &pernet_list, list)
523 ops_exit_list(ops, &net_exit_list);
524
Kirill Tkhaibcab1dd2018-02-13 12:26:33 +0300525 mutex_unlock(&net_mutex);
526
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000527 /* Free the net generic variables */
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000528 list_for_each_entry_reverse(ops, &pernet_list, list)
529 ops_free_list(ops, &net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200530
Kirill Tkhai1a57feb2018-02-13 12:26:23 +0300531 up_read(&net_sem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200532
533 /* Ensure there are no outstanding rcu callbacks using this
534 * network namespace.
535 */
536 rcu_barrier();
537
538 /* Finally it is safe to free my network namespace structure */
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000539 list_for_each_entry_safe(net, tmp, &net_exit_list, exit_list) {
540 list_del_init(&net->exit_list);
Eric W. Biederman70328662016-08-08 14:33:23 -0500541 dec_net_namespaces(net->ucounts);
Eric W. Biederman038e7332012-06-14 02:31:10 -0700542 put_user_ns(net->user_ns);
Al Viroa685e082011-06-08 21:13:01 -0400543 net_drop_ns(net);
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000544 }
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200545}
Florian Westphal7866cc52017-05-30 11:38:12 +0200546
547/**
548 * net_ns_barrier - wait until concurrent net_cleanup_work is done
549 *
550 * cleanup_net runs from work queue and will first remove namespaces
551 * from the global list, then run net exit functions.
552 *
553 * Call this in module exit path to make sure that all netns
554 * ->exit ops have been invoked before the function is removed.
555 */
556void net_ns_barrier(void)
557{
Kirill Tkhai1a57feb2018-02-13 12:26:23 +0300558 down_write(&net_sem);
Florian Westphal7866cc52017-05-30 11:38:12 +0200559 mutex_lock(&net_mutex);
560 mutex_unlock(&net_mutex);
Kirill Tkhai1a57feb2018-02-13 12:26:23 +0300561 up_write(&net_sem);
Florian Westphal7866cc52017-05-30 11:38:12 +0200562}
563EXPORT_SYMBOL(net_ns_barrier);
564
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000565static DECLARE_WORK(net_cleanup_work, cleanup_net);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200566
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200567void __put_net(struct net *net)
568{
569 /* Cleanup the network namespace in process context */
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000570 unsigned long flags;
571
572 spin_lock_irqsave(&cleanup_list_lock, flags);
573 list_add(&net->cleanup_list, &cleanup_list);
574 spin_unlock_irqrestore(&cleanup_list_lock, flags);
575
576 queue_work(netns_wq, &net_cleanup_work);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200577}
578EXPORT_SYMBOL_GPL(__put_net);
579
Stephen Rothwell956c9202011-05-12 13:51:13 +1000580struct net *get_net_ns_by_fd(int fd)
581{
Stephen Rothwell956c9202011-05-12 13:51:13 +1000582 struct file *file;
Al Viro33c42942014-11-01 02:32:53 -0400583 struct ns_common *ns;
Stephen Rothwell956c9202011-05-12 13:51:13 +1000584 struct net *net;
585
Stephen Rothwell956c9202011-05-12 13:51:13 +1000586 file = proc_ns_fget(fd);
Al Viroc316e6a2011-06-05 00:37:35 +0000587 if (IS_ERR(file))
588 return ERR_CAST(file);
Stephen Rothwell956c9202011-05-12 13:51:13 +1000589
Al Virof77c8012014-11-01 03:13:17 -0400590 ns = get_proc_ns(file_inode(file));
Al Viro33c42942014-11-01 02:32:53 -0400591 if (ns->ops == &netns_operations)
592 net = get_net(container_of(ns, struct net, ns));
Al Viroc316e6a2011-06-05 00:37:35 +0000593 else
594 net = ERR_PTR(-EINVAL);
Stephen Rothwell956c9202011-05-12 13:51:13 +1000595
Al Viroc316e6a2011-06-05 00:37:35 +0000596 fput(file);
Stephen Rothwell956c9202011-05-12 13:51:13 +1000597 return net;
598}
599
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700600#else
Stephen Rothwell956c9202011-05-12 13:51:13 +1000601struct net *get_net_ns_by_fd(int fd)
602{
603 return ERR_PTR(-EINVAL);
604}
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700605#endif
Vadim Kochan4b681c82015-01-12 16:34:05 +0200606EXPORT_SYMBOL_GPL(get_net_ns_by_fd);
Eric W. Biederman9dd776b2007-09-26 22:04:26 -0700607
Johannes Berg30ffee82009-07-10 09:51:35 +0000608struct net *get_net_ns_by_pid(pid_t pid)
609{
610 struct task_struct *tsk;
611 struct net *net;
612
613 /* Lookup the network namespace */
614 net = ERR_PTR(-ESRCH);
615 rcu_read_lock();
616 tsk = find_task_by_vpid(pid);
617 if (tsk) {
618 struct nsproxy *nsproxy;
Eric W. Biederman728dba32014-02-03 19:13:49 -0800619 task_lock(tsk);
620 nsproxy = tsk->nsproxy;
Johannes Berg30ffee82009-07-10 09:51:35 +0000621 if (nsproxy)
622 net = get_net(nsproxy->net_ns);
Eric W. Biederman728dba32014-02-03 19:13:49 -0800623 task_unlock(tsk);
Johannes Berg30ffee82009-07-10 09:51:35 +0000624 }
625 rcu_read_unlock();
626 return net;
627}
628EXPORT_SYMBOL_GPL(get_net_ns_by_pid);
629
Eric W. Biederman98f842e2011-06-15 10:21:48 -0700630static __net_init int net_ns_net_init(struct net *net)
631{
Al Viro33c42942014-11-01 02:32:53 -0400632#ifdef CONFIG_NET_NS
633 net->ns.ops = &netns_operations;
634#endif
Al Viro6344c432014-11-01 00:45:45 -0400635 return ns_alloc_inum(&net->ns);
Eric W. Biederman98f842e2011-06-15 10:21:48 -0700636}
637
638static __net_exit void net_ns_net_exit(struct net *net)
639{
Al Viro6344c432014-11-01 00:45:45 -0400640 ns_free_inum(&net->ns);
Eric W. Biederman98f842e2011-06-15 10:21:48 -0700641}
642
643static struct pernet_operations __net_initdata net_ns_ops = {
644 .init = net_ns_net_init,
645 .exit = net_ns_net_exit,
646};
647
stephen hemminger3ee52562016-08-31 15:17:49 -0700648static const struct nla_policy rtnl_net_policy[NETNSA_MAX + 1] = {
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100649 [NETNSA_NONE] = { .type = NLA_UNSPEC },
650 [NETNSA_NSID] = { .type = NLA_S32 },
651 [NETNSA_PID] = { .type = NLA_U32 },
652 [NETNSA_FD] = { .type = NLA_U32 },
653};
654
David Ahernc21ef3e2017-04-16 09:48:24 -0700655static int rtnl_net_newid(struct sk_buff *skb, struct nlmsghdr *nlh,
656 struct netlink_ext_ack *extack)
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100657{
658 struct net *net = sock_net(skb->sk);
659 struct nlattr *tb[NETNSA_MAX + 1];
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200660 struct nlattr *nla;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100661 struct net *peer;
662 int nsid, err;
663
664 err = nlmsg_parse(nlh, sizeof(struct rtgenmsg), tb, NETNSA_MAX,
David Ahernc21ef3e2017-04-16 09:48:24 -0700665 rtnl_net_policy, extack);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100666 if (err < 0)
667 return err;
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200668 if (!tb[NETNSA_NSID]) {
669 NL_SET_ERR_MSG(extack, "nsid is missing");
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100670 return -EINVAL;
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200671 }
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100672 nsid = nla_get_s32(tb[NETNSA_NSID]);
673
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200674 if (tb[NETNSA_PID]) {
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100675 peer = get_net_ns_by_pid(nla_get_u32(tb[NETNSA_PID]));
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200676 nla = tb[NETNSA_PID];
677 } else if (tb[NETNSA_FD]) {
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100678 peer = get_net_ns_by_fd(nla_get_u32(tb[NETNSA_FD]));
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200679 nla = tb[NETNSA_FD];
680 } else {
681 NL_SET_ERR_MSG(extack, "Peer netns reference is missing");
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100682 return -EINVAL;
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200683 }
684 if (IS_ERR(peer)) {
685 NL_SET_BAD_ATTR(extack, nla);
686 NL_SET_ERR_MSG(extack, "Peer netns reference is invalid");
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100687 return PTR_ERR(peer);
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200688 }
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100689
Paul Moorefba143c2016-11-29 16:57:48 -0500690 spin_lock_bh(&net->nsid_lock);
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200691 if (__peernet2id(net, peer) >= 0) {
Paul Moorefba143c2016-11-29 16:57:48 -0500692 spin_unlock_bh(&net->nsid_lock);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100693 err = -EEXIST;
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200694 NL_SET_BAD_ATTR(extack, nla);
695 NL_SET_ERR_MSG(extack,
696 "Peer netns already has a nsid assigned");
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100697 goto out;
698 }
699
700 err = alloc_netid(net, peer, nsid);
Paul Moorefba143c2016-11-29 16:57:48 -0500701 spin_unlock_bh(&net->nsid_lock);
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200702 if (err >= 0) {
703 rtnl_net_notifyid(net, RTM_NEWNSID, err);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100704 err = 0;
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200705 } else if (err == -ENOSPC && nsid >= 0) {
Nicolas Dichtel10d486a2017-06-09 14:41:57 +0200706 err = -EEXIST;
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200707 NL_SET_BAD_ATTR(extack, tb[NETNSA_NSID]);
708 NL_SET_ERR_MSG(extack, "The specified nsid is already used");
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200709 }
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100710out:
711 put_net(peer);
712 return err;
713}
714
715static int rtnl_net_get_size(void)
716{
717 return NLMSG_ALIGN(sizeof(struct rtgenmsg))
718 + nla_total_size(sizeof(s32)) /* NETNSA_NSID */
719 ;
720}
721
722static int rtnl_net_fill(struct sk_buff *skb, u32 portid, u32 seq, int flags,
Nicolas Dichtelcab3c8e2015-05-07 11:02:48 +0200723 int cmd, struct net *net, int nsid)
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100724{
725 struct nlmsghdr *nlh;
726 struct rtgenmsg *rth;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100727
728 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rth), flags);
729 if (!nlh)
730 return -EMSGSIZE;
731
732 rth = nlmsg_data(nlh);
733 rth->rtgen_family = AF_UNSPEC;
734
Nicolas Dichtelcab3c8e2015-05-07 11:02:48 +0200735 if (nla_put_s32(skb, NETNSA_NSID, nsid))
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100736 goto nla_put_failure;
737
738 nlmsg_end(skb, nlh);
739 return 0;
740
741nla_put_failure:
742 nlmsg_cancel(skb, nlh);
743 return -EMSGSIZE;
744}
745
David Ahernc21ef3e2017-04-16 09:48:24 -0700746static int rtnl_net_getid(struct sk_buff *skb, struct nlmsghdr *nlh,
747 struct netlink_ext_ack *extack)
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100748{
749 struct net *net = sock_net(skb->sk);
750 struct nlattr *tb[NETNSA_MAX + 1];
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200751 struct nlattr *nla;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100752 struct sk_buff *msg;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100753 struct net *peer;
Nicolas Dichtelcab3c8e2015-05-07 11:02:48 +0200754 int err, id;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100755
756 err = nlmsg_parse(nlh, sizeof(struct rtgenmsg), tb, NETNSA_MAX,
David Ahernc21ef3e2017-04-16 09:48:24 -0700757 rtnl_net_policy, extack);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100758 if (err < 0)
759 return err;
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200760 if (tb[NETNSA_PID]) {
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100761 peer = get_net_ns_by_pid(nla_get_u32(tb[NETNSA_PID]));
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200762 nla = tb[NETNSA_PID];
763 } else if (tb[NETNSA_FD]) {
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100764 peer = get_net_ns_by_fd(nla_get_u32(tb[NETNSA_FD]));
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200765 nla = tb[NETNSA_FD];
766 } else {
767 NL_SET_ERR_MSG(extack, "Peer netns reference is missing");
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100768 return -EINVAL;
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200769 }
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100770
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200771 if (IS_ERR(peer)) {
772 NL_SET_BAD_ATTR(extack, nla);
773 NL_SET_ERR_MSG(extack, "Peer netns reference is invalid");
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100774 return PTR_ERR(peer);
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200775 }
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100776
777 msg = nlmsg_new(rtnl_net_get_size(), GFP_KERNEL);
778 if (!msg) {
779 err = -ENOMEM;
780 goto out;
781 }
782
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200783 id = peernet2id(net, peer);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100784 err = rtnl_net_fill(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
David S. Millerb04096f2015-05-13 14:31:43 -0400785 RTM_NEWNSID, net, id);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100786 if (err < 0)
787 goto err_out;
788
789 err = rtnl_unicast(msg, net, NETLINK_CB(skb).portid);
790 goto out;
791
792err_out:
793 nlmsg_free(msg);
794out:
795 put_net(peer);
796 return err;
797}
798
Nicolas Dichtela143c402015-04-07 11:51:54 +0200799struct rtnl_net_dump_cb {
800 struct net *net;
801 struct sk_buff *skb;
802 struct netlink_callback *cb;
803 int idx;
804 int s_idx;
805};
806
807static int rtnl_net_dumpid_one(int id, void *peer, void *data)
808{
809 struct rtnl_net_dump_cb *net_cb = (struct rtnl_net_dump_cb *)data;
810 int ret;
811
812 if (net_cb->idx < net_cb->s_idx)
813 goto cont;
814
815 ret = rtnl_net_fill(net_cb->skb, NETLINK_CB(net_cb->cb->skb).portid,
816 net_cb->cb->nlh->nlmsg_seq, NLM_F_MULTI,
Nicolas Dichtelcab3c8e2015-05-07 11:02:48 +0200817 RTM_NEWNSID, net_cb->net, id);
Nicolas Dichtela143c402015-04-07 11:51:54 +0200818 if (ret < 0)
819 return ret;
820
821cont:
822 net_cb->idx++;
823 return 0;
824}
825
826static int rtnl_net_dumpid(struct sk_buff *skb, struct netlink_callback *cb)
827{
828 struct net *net = sock_net(skb->sk);
829 struct rtnl_net_dump_cb net_cb = {
830 .net = net,
831 .skb = skb,
832 .cb = cb,
833 .idx = 0,
834 .s_idx = cb->args[0],
835 };
836
Paul Moorefba143c2016-11-29 16:57:48 -0500837 spin_lock_bh(&net->nsid_lock);
Nicolas Dichtela143c402015-04-07 11:51:54 +0200838 idr_for_each(&net->netns_ids, rtnl_net_dumpid_one, &net_cb);
Paul Moorefba143c2016-11-29 16:57:48 -0500839 spin_unlock_bh(&net->nsid_lock);
Nicolas Dichtela143c402015-04-07 11:51:54 +0200840
841 cb->args[0] = net_cb.idx;
842 return skb->len;
843}
844
Nicolas Dichtelcab3c8e2015-05-07 11:02:48 +0200845static void rtnl_net_notifyid(struct net *net, int cmd, int id)
Nicolas Dichtel9a963452015-04-07 11:51:53 +0200846{
847 struct sk_buff *msg;
848 int err = -ENOMEM;
849
850 msg = nlmsg_new(rtnl_net_get_size(), GFP_KERNEL);
851 if (!msg)
852 goto out;
853
Nicolas Dichtelcab3c8e2015-05-07 11:02:48 +0200854 err = rtnl_net_fill(msg, 0, 0, 0, cmd, net, id);
Nicolas Dichtel9a963452015-04-07 11:51:53 +0200855 if (err < 0)
856 goto err_out;
857
858 rtnl_notify(msg, net, 0, RTNLGRP_NSID, NULL, 0);
859 return;
860
861err_out:
862 nlmsg_free(msg);
863out:
864 rtnl_set_sk_err(net, RTNLGRP_NSID, err);
865}
866
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200867static int __init net_ns_init(void)
868{
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800869 struct net_generic *ng;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200870
Pavel Emelyanovd57a9212007-11-01 00:46:50 -0700871#ifdef CONFIG_NET_NS
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200872 net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
873 SMP_CACHE_BYTES,
874 SLAB_PANIC, NULL);
Benjamin Thery3ef13552007-11-19 23:18:16 -0800875
876 /* Create workqueue for cleanup */
877 netns_wq = create_singlethread_workqueue("netns");
878 if (!netns_wq)
879 panic("Could not create netns workq");
Pavel Emelyanovd57a9212007-11-01 00:46:50 -0700880#endif
Benjamin Thery3ef13552007-11-19 23:18:16 -0800881
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800882 ng = net_alloc_generic();
883 if (!ng)
884 panic("Could not allocate generic netns");
885
886 rcu_assign_pointer(init_net.gen, ng);
887
Kirill Tkhai1a57feb2018-02-13 12:26:23 +0300888 down_write(&net_sem);
Eric W. Biederman038e7332012-06-14 02:31:10 -0700889 if (setup_net(&init_net, &init_user_ns))
Stephen Hemmingerca0f3112009-05-21 15:10:31 -0700890 panic("Could not setup the initial network namespace");
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200891
Dmitry Torokhovf8c46cb2016-08-10 14:36:00 -0700892 init_net_initialized = true;
Kirill Tkhai1a57feb2018-02-13 12:26:23 +0300893 up_write(&net_sem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200894
Eric W. Biederman98f842e2011-06-15 10:21:48 -0700895 register_pernet_subsys(&net_ns_ops);
896
Florian Westphal165b9112017-08-09 20:41:53 +0200897 rtnl_register(PF_UNSPEC, RTM_NEWNSID, rtnl_net_newid, NULL,
898 RTNL_FLAG_DOIT_UNLOCKED);
Nicolas Dichtela143c402015-04-07 11:51:54 +0200899 rtnl_register(PF_UNSPEC, RTM_GETNSID, rtnl_net_getid, rtnl_net_dumpid,
Florian Westphal165b9112017-08-09 20:41:53 +0200900 RTNL_FLAG_DOIT_UNLOCKED);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100901
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200902 return 0;
903}
904
905pure_initcall(net_ns_init);
906
Denis V. Luneved160e82007-11-13 03:23:21 -0800907#ifdef CONFIG_NET_NS
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000908static int __register_pernet_operations(struct list_head *list,
909 struct pernet_operations *ops)
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200910{
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000911 struct net *net;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200912 int error;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000913 LIST_HEAD(net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200914
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200915 list_add_tail(&ops->list, list);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000916 if (ops->init || (ops->id && ops->size)) {
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700917 for_each_net(net) {
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000918 error = ops_init(ops, net);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200919 if (error)
920 goto out_undo;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000921 list_add_tail(&net->exit_list, &net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200922 }
923 }
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700924 return 0;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200925
926out_undo:
927 /* If I have an error cleanup all namespaces I initialized */
928 list_del(&ops->list);
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000929 ops_exit_list(ops, &net_exit_list);
930 ops_free_list(ops, &net_exit_list);
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700931 return error;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200932}
933
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000934static void __unregister_pernet_operations(struct pernet_operations *ops)
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200935{
936 struct net *net;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000937 LIST_HEAD(net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200938
939 list_del(&ops->list);
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000940 for_each_net(net)
941 list_add_tail(&net->exit_list, &net_exit_list);
942 ops_exit_list(ops, &net_exit_list);
943 ops_free_list(ops, &net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200944}
945
Denis V. Luneved160e82007-11-13 03:23:21 -0800946#else
947
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000948static int __register_pernet_operations(struct list_head *list,
949 struct pernet_operations *ops)
950{
Dmitry Torokhovf8c46cb2016-08-10 14:36:00 -0700951 if (!init_net_initialized) {
952 list_add_tail(&ops->list, list);
953 return 0;
954 }
955
Julian Anastasovb9229342012-04-16 04:43:15 +0000956 return ops_init(ops, &init_net);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000957}
958
959static void __unregister_pernet_operations(struct pernet_operations *ops)
960{
Dmitry Torokhovf8c46cb2016-08-10 14:36:00 -0700961 if (!init_net_initialized) {
962 list_del(&ops->list);
963 } else {
964 LIST_HEAD(net_exit_list);
965 list_add(&init_net.exit_list, &net_exit_list);
966 ops_exit_list(ops, &net_exit_list);
967 ops_free_list(ops, &net_exit_list);
968 }
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000969}
970
971#endif /* CONFIG_NET_NS */
972
973static DEFINE_IDA(net_generic_ids);
974
Denis V. Luneved160e82007-11-13 03:23:21 -0800975static int register_pernet_operations(struct list_head *list,
976 struct pernet_operations *ops)
977{
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000978 int error;
979
980 if (ops->id) {
981again:
Alexey Dobriyan6af2d5f2016-12-02 04:21:32 +0300982 error = ida_get_new_above(&net_generic_ids, MIN_PERNET_OPS_ID, ops->id);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000983 if (error < 0) {
984 if (error == -EAGAIN) {
985 ida_pre_get(&net_generic_ids, GFP_KERNEL);
986 goto again;
987 }
988 return error;
989 }
Alexey Dobriyan6af2d5f2016-12-02 04:21:32 +0300990 max_gen_ptrs = max(max_gen_ptrs, *ops->id + 1);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000991 }
992 error = __register_pernet_operations(list, ops);
Eric W. Biederman3a765ed2009-12-03 02:29:06 +0000993 if (error) {
994 rcu_barrier();
995 if (ops->id)
996 ida_remove(&net_generic_ids, *ops->id);
997 }
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000998
999 return error;
Denis V. Luneved160e82007-11-13 03:23:21 -08001000}
1001
1002static void unregister_pernet_operations(struct pernet_operations *ops)
1003{
Eric W. Biedermanf875bae2009-11-29 22:25:28 +00001004
1005 __unregister_pernet_operations(ops);
Eric W. Biederman3a765ed2009-12-03 02:29:06 +00001006 rcu_barrier();
Eric W. Biedermanf875bae2009-11-29 22:25:28 +00001007 if (ops->id)
1008 ida_remove(&net_generic_ids, *ops->id);
Denis V. Luneved160e82007-11-13 03:23:21 -08001009}
Pavel Emelyanovc93cf612008-04-15 00:35:23 -07001010
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001011/**
1012 * register_pernet_subsys - register a network namespace subsystem
1013 * @ops: pernet operations structure for the subsystem
1014 *
1015 * Register a subsystem which has init and exit functions
1016 * that are called when network namespaces are created and
1017 * destroyed respectively.
1018 *
1019 * When registered all network namespace init functions are
1020 * called for every existing network namespace. Allowing kernel
1021 * modules to have a race free view of the set of network namespaces.
1022 *
1023 * When a new network namespace is created all of the init
1024 * methods are called in the order in which they were registered.
1025 *
1026 * When a network namespace is destroyed all of the exit methods
1027 * are called in the reverse of the order with which they were
1028 * registered.
1029 */
1030int register_pernet_subsys(struct pernet_operations *ops)
1031{
1032 int error;
Kirill Tkhai1a57feb2018-02-13 12:26:23 +03001033 down_write(&net_sem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001034 error = register_pernet_operations(first_device, ops);
Kirill Tkhai1a57feb2018-02-13 12:26:23 +03001035 up_write(&net_sem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001036 return error;
1037}
1038EXPORT_SYMBOL_GPL(register_pernet_subsys);
1039
1040/**
1041 * unregister_pernet_subsys - unregister a network namespace subsystem
1042 * @ops: pernet operations structure to manipulate
1043 *
1044 * Remove the pernet operations structure from the list to be
Oliver Pinter53379e52008-02-03 17:56:48 +02001045 * used when network namespaces are created or destroyed. In
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001046 * addition run the exit method for all existing network
1047 * namespaces.
1048 */
Jiri Pirkob3c981d2010-04-25 00:49:56 -07001049void unregister_pernet_subsys(struct pernet_operations *ops)
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001050{
Kirill Tkhai1a57feb2018-02-13 12:26:23 +03001051 down_write(&net_sem);
Jiri Pirkob3c981d2010-04-25 00:49:56 -07001052 unregister_pernet_operations(ops);
Kirill Tkhai1a57feb2018-02-13 12:26:23 +03001053 up_write(&net_sem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001054}
1055EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
1056
1057/**
1058 * register_pernet_device - register a network namespace device
1059 * @ops: pernet operations structure for the subsystem
1060 *
1061 * Register a device which has init and exit functions
1062 * that are called when network namespaces are created and
1063 * destroyed respectively.
1064 *
1065 * When registered all network namespace init functions are
1066 * called for every existing network namespace. Allowing kernel
1067 * modules to have a race free view of the set of network namespaces.
1068 *
1069 * When a new network namespace is created all of the init
1070 * methods are called in the order in which they were registered.
1071 *
1072 * When a network namespace is destroyed all of the exit methods
1073 * are called in the reverse of the order with which they were
1074 * registered.
1075 */
1076int register_pernet_device(struct pernet_operations *ops)
1077{
1078 int error;
Kirill Tkhai1a57feb2018-02-13 12:26:23 +03001079 down_write(&net_sem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001080 error = register_pernet_operations(&pernet_list, ops);
1081 if (!error && (first_device == &pernet_list))
1082 first_device = &ops->list;
Kirill Tkhai1a57feb2018-02-13 12:26:23 +03001083 up_write(&net_sem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001084 return error;
1085}
1086EXPORT_SYMBOL_GPL(register_pernet_device);
1087
1088/**
1089 * unregister_pernet_device - unregister a network namespace netdevice
1090 * @ops: pernet operations structure to manipulate
1091 *
1092 * Remove the pernet operations structure from the list to be
Oliver Pinter53379e52008-02-03 17:56:48 +02001093 * used when network namespaces are created or destroyed. In
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001094 * addition run the exit method for all existing network
1095 * namespaces.
1096 */
1097void unregister_pernet_device(struct pernet_operations *ops)
1098{
Kirill Tkhai1a57feb2018-02-13 12:26:23 +03001099 down_write(&net_sem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001100 if (&ops->list == first_device)
1101 first_device = first_device->next;
1102 unregister_pernet_operations(ops);
Kirill Tkhai1a57feb2018-02-13 12:26:23 +03001103 up_write(&net_sem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001104}
1105EXPORT_SYMBOL_GPL(unregister_pernet_device);
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001106
1107#ifdef CONFIG_NET_NS
Al Viro64964522014-11-01 00:37:32 -04001108static struct ns_common *netns_get(struct task_struct *task)
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001109{
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001110 struct net *net = NULL;
1111 struct nsproxy *nsproxy;
1112
Eric W. Biederman728dba32014-02-03 19:13:49 -08001113 task_lock(task);
1114 nsproxy = task->nsproxy;
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001115 if (nsproxy)
1116 net = get_net(nsproxy->net_ns);
Eric W. Biederman728dba32014-02-03 19:13:49 -08001117 task_unlock(task);
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001118
Al Viroff248702014-11-01 00:10:50 -04001119 return net ? &net->ns : NULL;
1120}
1121
1122static inline struct net *to_net_ns(struct ns_common *ns)
1123{
1124 return container_of(ns, struct net, ns);
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001125}
1126
Al Viro64964522014-11-01 00:37:32 -04001127static void netns_put(struct ns_common *ns)
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001128{
Al Viroff248702014-11-01 00:10:50 -04001129 put_net(to_net_ns(ns));
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001130}
1131
Al Viro64964522014-11-01 00:37:32 -04001132static int netns_install(struct nsproxy *nsproxy, struct ns_common *ns)
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001133{
Al Viroff248702014-11-01 00:10:50 -04001134 struct net *net = to_net_ns(ns);
Eric W. Biederman142e1d12012-07-26 01:13:20 -07001135
Eric W. Biederman5e4a0842012-12-14 07:55:36 -08001136 if (!ns_capable(net->user_ns, CAP_SYS_ADMIN) ||
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -07001137 !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
Eric W. Biederman142e1d12012-07-26 01:13:20 -07001138 return -EPERM;
1139
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001140 put_net(nsproxy->net_ns);
Eric W. Biederman142e1d12012-07-26 01:13:20 -07001141 nsproxy->net_ns = get_net(net);
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001142 return 0;
1143}
1144
Andrey Vaginbcac25a2016-09-06 00:47:13 -07001145static struct user_namespace *netns_owner(struct ns_common *ns)
1146{
1147 return to_net_ns(ns)->user_ns;
1148}
1149
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001150const struct proc_ns_operations netns_operations = {
1151 .name = "net",
1152 .type = CLONE_NEWNET,
1153 .get = netns_get,
1154 .put = netns_put,
1155 .install = netns_install,
Andrey Vaginbcac25a2016-09-06 00:47:13 -07001156 .owner = netns_owner,
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001157};
1158#endif