blob: bcab9a938d6f3e229e3a9044c30ad9d9980dd446 [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;
Kirill Tkhai447cd7a2018-02-13 12:26:44 +030032/* Used only if there are !async pernet_operations registered */
Cong Wang200b9162014-05-12 15:11:20 -070033DEFINE_MUTEX(net_mutex);
Eric W. Biederman5f256be2007-09-12 11:50:50 +020034
Eric W. Biederman5f256be2007-09-12 11:50:50 +020035LIST_HEAD(net_namespace_list);
Alexey Dobriyanb76a4612008-10-08 11:35:06 +020036EXPORT_SYMBOL_GPL(net_namespace_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +020037
Rustad, Mark D734b6542012-07-18 09:06:07 +000038struct net init_net = {
Kirill Tkhai273c28b2018-01-12 18:28:31 +030039 .count = REFCOUNT_INIT(1),
David Howellsb5082df2017-04-27 22:40:23 +010040 .dev_base_head = LIST_HEAD_INIT(init_net.dev_base_head),
Rustad, Mark D734b6542012-07-18 09:06:07 +000041};
Denis V. Lunevff4b9502008-01-22 22:05:33 -080042EXPORT_SYMBOL(init_net);
Eric W. Biederman5f256be2007-09-12 11:50:50 +020043
Dmitry Torokhovf8c46cb2016-08-10 14:36:00 -070044static bool init_net_initialized;
Kirill Tkhai447cd7a2018-02-13 12:26:44 +030045static unsigned nr_sync_pernet_ops;
Kirill Tkhai1a57feb2018-02-13 12:26:23 +030046/*
Kirill Tkhai447cd7a2018-02-13 12:26:44 +030047 * net_sem: protects: pernet_list, net_generic_ids, nr_sync_pernet_ops,
Kirill Tkhai1a57feb2018-02-13 12:26:23 +030048 * init_net_initialized and first_device pointer.
49 */
50DECLARE_RWSEM(net_sem);
Dmitry Torokhovf8c46cb2016-08-10 14:36:00 -070051
Alexey Dobriyan6af2d5f2016-12-02 04:21:32 +030052#define MIN_PERNET_OPS_ID \
53 ((sizeof(struct net_generic) + sizeof(void *) - 1) / sizeof(void *))
54
Pavel Emelyanovdec827d2008-04-15 00:36:08 -070055#define INITIAL_NET_GEN_PTRS 13 /* +1 for len +2 for rcu_head */
56
Eric Dumazet073862b2012-01-26 00:41:38 +000057static unsigned int max_gen_ptrs = INITIAL_NET_GEN_PTRS;
58
59static struct net_generic *net_alloc_generic(void)
60{
61 struct net_generic *ng;
Alexey Dobriyan6af2d5f2016-12-02 04:21:32 +030062 unsigned int generic_size = offsetof(struct net_generic, ptr[max_gen_ptrs]);
Eric Dumazet073862b2012-01-26 00:41:38 +000063
64 ng = kzalloc(generic_size, GFP_KERNEL);
65 if (ng)
Alexey Dobriyan9bfc7b92016-12-02 04:12:58 +030066 ng->s.len = max_gen_ptrs;
Eric Dumazet073862b2012-01-26 00:41:38 +000067
68 return ng;
69}
70
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030071static int net_assign_generic(struct net *net, unsigned int id, void *data)
Jiri Pirko05fceb42010-04-23 01:40:47 +000072{
73 struct net_generic *ng, *old_ng;
74
Alexey Dobriyan6af2d5f2016-12-02 04:21:32 +030075 BUG_ON(id < MIN_PERNET_OPS_ID);
Jiri Pirko05fceb42010-04-23 01:40:47 +000076
Eric Dumazet1c877332010-10-25 03:20:11 +000077 old_ng = rcu_dereference_protected(net->gen,
Kirill Tkhai447cd7a2018-02-13 12:26:44 +030078 lockdep_is_held(&net_sem));
Alexey Dobriyan6af2d5f2016-12-02 04:21:32 +030079 if (old_ng->s.len > id) {
80 old_ng->ptr[id] = data;
Alexey Dobriyan1a9a05922016-12-02 04:11:34 +030081 return 0;
82 }
Jiri Pirko05fceb42010-04-23 01:40:47 +000083
Eric Dumazet073862b2012-01-26 00:41:38 +000084 ng = net_alloc_generic();
Jiri Pirko05fceb42010-04-23 01:40:47 +000085 if (ng == NULL)
86 return -ENOMEM;
87
88 /*
89 * Some synchronisation notes:
90 *
91 * The net_generic explores the net->gen array inside rcu
92 * read section. Besides once set the net->gen->ptr[x]
93 * pointer never changes (see rules in netns/generic.h).
94 *
95 * That said, we simply duplicate this array and schedule
96 * the old copy for kfree after a grace period.
97 */
98
Alexey Dobriyan6af2d5f2016-12-02 04:21:32 +030099 memcpy(&ng->ptr[MIN_PERNET_OPS_ID], &old_ng->ptr[MIN_PERNET_OPS_ID],
100 (old_ng->s.len - MIN_PERNET_OPS_ID) * sizeof(void *));
101 ng->ptr[id] = data;
Jiri Pirko05fceb42010-04-23 01:40:47 +0000102
103 rcu_assign_pointer(net->gen, ng);
Alexey Dobriyan9bfc7b92016-12-02 04:12:58 +0300104 kfree_rcu(old_ng, s.rcu);
Jiri Pirko05fceb42010-04-23 01:40:47 +0000105 return 0;
106}
107
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000108static int ops_init(const struct pernet_operations *ops, struct net *net)
109{
Julian Anastasovb9229342012-04-16 04:43:15 +0000110 int err = -ENOMEM;
111 void *data = NULL;
112
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000113 if (ops->id && ops->size) {
Julian Anastasovb9229342012-04-16 04:43:15 +0000114 data = kzalloc(ops->size, GFP_KERNEL);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000115 if (!data)
Julian Anastasovb9229342012-04-16 04:43:15 +0000116 goto out;
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000117
118 err = net_assign_generic(net, *ops->id, data);
Julian Anastasovb9229342012-04-16 04:43:15 +0000119 if (err)
120 goto cleanup;
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000121 }
Julian Anastasovb9229342012-04-16 04:43:15 +0000122 err = 0;
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000123 if (ops->init)
Julian Anastasovb9229342012-04-16 04:43:15 +0000124 err = ops->init(net);
125 if (!err)
126 return 0;
127
128cleanup:
129 kfree(data);
130
131out:
132 return err;
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000133}
134
135static void ops_free(const struct pernet_operations *ops, struct net *net)
136{
137 if (ops->id && ops->size) {
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +0300138 kfree(net_generic(net, *ops->id));
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000139 }
140}
141
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000142static void ops_exit_list(const struct pernet_operations *ops,
143 struct list_head *net_exit_list)
144{
145 struct net *net;
146 if (ops->exit) {
147 list_for_each_entry(net, net_exit_list, exit_list)
148 ops->exit(net);
149 }
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000150 if (ops->exit_batch)
151 ops->exit_batch(net_exit_list);
152}
153
154static void ops_free_list(const struct pernet_operations *ops,
155 struct list_head *net_exit_list)
156{
157 struct net *net;
158 if (ops->size && ops->id) {
159 list_for_each_entry(net, net_exit_list, exit_list)
160 ops_free(ops, net);
161 }
162}
163
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200164/* should be called with nsid_lock held */
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100165static int alloc_netid(struct net *net, struct net *peer, int reqid)
166{
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200167 int min = 0, max = 0;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100168
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100169 if (reqid >= 0) {
170 min = reqid;
171 max = reqid + 1;
172 }
173
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200174 return idr_alloc(&net->netns_ids, peer, min, max, GFP_ATOMIC);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100175}
176
177/* This function is used by idr_for_each(). If net is equal to peer, the
178 * function returns the id so that idr_for_each() stops. Because we cannot
179 * returns the id 0 (idr_for_each() will not stop), we return the magic value
180 * NET_ID_ZERO (-1) for it.
181 */
182#define NET_ID_ZERO -1
183static int net_eq_idr(int id, void *net, void *peer)
184{
185 if (net_eq(net, peer))
186 return id ? : NET_ID_ZERO;
187 return 0;
188}
189
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200190/* Should be called with nsid_lock held. If a new id is assigned, the bool alloc
191 * is set to true, thus the caller knows that the new id must be notified via
192 * rtnl.
193 */
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200194static int __peernet2id_alloc(struct net *net, struct net *peer, bool *alloc)
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100195{
196 int id = idr_for_each(&net->netns_ids, net_eq_idr, peer);
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200197 bool alloc_it = *alloc;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100198
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200199 *alloc = false;
200
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100201 /* Magic value for id 0. */
202 if (id == NET_ID_ZERO)
203 return 0;
204 if (id > 0)
205 return id;
206
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200207 if (alloc_it) {
Nicolas Dichtel109582af2015-05-07 11:02:47 +0200208 id = alloc_netid(net, peer, -1);
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200209 *alloc = true;
Nicolas Dichtel109582af2015-05-07 11:02:47 +0200210 return id >= 0 ? id : NETNSA_NSID_NOT_ASSIGNED;
211 }
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100212
Nicolas Dichtel109582af2015-05-07 11:02:47 +0200213 return NETNSA_NSID_NOT_ASSIGNED;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100214}
215
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200216/* should be called with nsid_lock held */
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200217static int __peernet2id(struct net *net, struct net *peer)
218{
219 bool no = false;
220
221 return __peernet2id_alloc(net, peer, &no);
222}
223
224static void rtnl_net_notifyid(struct net *net, int cmd, int id);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100225/* This function returns the id of a peer netns. If no id is assigned, one will
226 * be allocated and returned.
227 */
Nicolas Dichtel7a0877d2015-05-07 11:02:49 +0200228int peernet2id_alloc(struct net *net, struct net *peer)
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100229{
Kirill Tkhai0c06bea2018-01-16 12:31:41 +0300230 bool alloc = false, alive = false;
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200231 int id;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100232
Kirill Tkhai273c28b2018-01-12 18:28:31 +0300233 if (refcount_read(&net->count) == 0)
WANG Congcfc44a42016-11-16 10:27:02 -0800234 return NETNSA_NSID_NOT_ASSIGNED;
Paul Moorefba143c2016-11-29 16:57:48 -0500235 spin_lock_bh(&net->nsid_lock);
Kirill Tkhai0c06bea2018-01-16 12:31:41 +0300236 /*
237 * When peer is obtained from RCU lists, we may race with
238 * its cleanup. Check whether it's alive, and this guarantees
239 * we never hash a peer back to net->netns_ids, after it has
240 * just been idr_remove()'d from there in cleanup_net().
241 */
242 if (maybe_get_net(peer))
243 alive = alloc = true;
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200244 id = __peernet2id_alloc(net, peer, &alloc);
Paul Moorefba143c2016-11-29 16:57:48 -0500245 spin_unlock_bh(&net->nsid_lock);
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200246 if (alloc && id >= 0)
247 rtnl_net_notifyid(net, RTM_NEWNSID, id);
Kirill Tkhai0c06bea2018-01-16 12:31:41 +0300248 if (alive)
249 put_net(peer);
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200250 return id;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100251}
Jiri Benc7cbebc82017-11-02 17:04:36 -0200252EXPORT_SYMBOL_GPL(peernet2id_alloc);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100253
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200254/* This function returns, if assigned, the id of a peer netns. */
Nicolas Dichtel59324cf2015-05-07 11:02:53 +0200255int peernet2id(struct net *net, struct net *peer)
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200256{
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200257 int id;
258
Paul Moorefba143c2016-11-29 16:57:48 -0500259 spin_lock_bh(&net->nsid_lock);
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200260 id = __peernet2id(net, peer);
Paul Moorefba143c2016-11-29 16:57:48 -0500261 spin_unlock_bh(&net->nsid_lock);
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200262 return id;
263}
WANG Cong38f507f2016-09-01 21:53:44 -0700264EXPORT_SYMBOL(peernet2id);
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200265
Nicolas Dichtel59324cf2015-05-07 11:02:53 +0200266/* This function returns true is the peer netns has an id assigned into the
267 * current netns.
268 */
269bool peernet_has_id(struct net *net, struct net *peer)
270{
271 return peernet2id(net, peer) >= 0;
272}
273
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100274struct net *get_net_ns_by_id(struct net *net, int id)
275{
276 struct net *peer;
277
278 if (id < 0)
279 return NULL;
280
281 rcu_read_lock();
282 peer = idr_find(&net->netns_ids, id);
283 if (peer)
Eric W. Biederman21b59442017-12-19 11:27:56 -0600284 peer = maybe_get_net(peer);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100285 rcu_read_unlock();
286
287 return peer;
288}
289
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700290/*
291 * setup_net runs the initializers for the network namespace object.
292 */
Eric W. Biederman038e7332012-06-14 02:31:10 -0700293static __net_init int setup_net(struct net *net, struct user_namespace *user_ns)
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700294{
Kirill Tkhai1a57feb2018-02-13 12:26:23 +0300295 /* Must be called with net_sem held */
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000296 const struct pernet_operations *ops, *saved_ops;
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800297 int error = 0;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000298 LIST_HEAD(net_exit_list);
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700299
Kirill Tkhai273c28b2018-01-12 18:28:31 +0300300 refcount_set(&net->count, 1);
Reshetova, Elenac122e142017-06-30 13:08:08 +0300301 refcount_set(&net->passive, 1);
Thomas Graf4e985ad2011-06-21 03:11:20 +0000302 net->dev_base_seq = 1;
Eric W. Biederman038e7332012-06-14 02:31:10 -0700303 net->user_ns = user_ns;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100304 idr_init(&net->netns_ids);
WANG Congde133462015-05-15 14:47:32 -0700305 spin_lock_init(&net->nsid_lock);
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800306
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700307 list_for_each_entry(ops, &pernet_list, list) {
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000308 error = ops_init(ops, net);
309 if (error < 0)
310 goto out_undo;
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700311 }
Kirill Tkhai98f6c532018-02-13 12:26:02 +0300312 rtnl_lock();
313 list_add_tail_rcu(&net->list, &net_namespace_list);
314 rtnl_unlock();
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700315out:
316 return error;
317
318out_undo:
319 /* Walk through the list backwards calling the exit functions
320 * for the pernet modules whose init functions did not fail.
321 */
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000322 list_add(&net->exit_list, &net_exit_list);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000323 saved_ops = ops;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000324 list_for_each_entry_continue_reverse(ops, &pernet_list, list)
325 ops_exit_list(ops, &net_exit_list);
326
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000327 ops = saved_ops;
328 list_for_each_entry_continue_reverse(ops, &pernet_list, list)
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000329 ops_free_list(ops, &net_exit_list);
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700330
331 rcu_barrier();
332 goto out;
333}
334
Roman Kapl7c3f18752017-05-24 10:22:22 +0200335static int __net_init net_defaults_init_net(struct net *net)
336{
337 net->core.sysctl_somaxconn = SOMAXCONN;
338 return 0;
339}
340
341static struct pernet_operations net_defaults_ops = {
342 .init = net_defaults_init_net,
Kirill Tkhaiff291d02018-02-13 12:27:51 +0300343 .async = true,
Roman Kapl7c3f18752017-05-24 10:22:22 +0200344};
345
346static __init int net_defaults_init(void)
347{
348 if (register_pernet_subsys(&net_defaults_ops))
349 panic("Cannot initialize net default settings");
350
351 return 0;
352}
353
354core_initcall(net_defaults_init);
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800355
Clemens Nossebe47d42009-02-23 15:37:35 -0800356#ifdef CONFIG_NET_NS
Arnd Bergmann2ed6afd2016-09-23 18:06:12 +0200357static struct ucounts *inc_net_namespaces(struct user_namespace *ns)
358{
359 return inc_ucount(ns, current_euid(), UCOUNT_NET_NAMESPACES);
360}
361
362static void dec_net_namespaces(struct ucounts *ucounts)
363{
364 dec_ucount(ucounts, UCOUNT_NET_NAMESPACES);
365}
366
Clemens Nossebe47d42009-02-23 15:37:35 -0800367static struct kmem_cache *net_cachep;
368static struct workqueue_struct *netns_wq;
369
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200370static struct net *net_alloc(void)
371{
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800372 struct net *net = NULL;
373 struct net_generic *ng;
374
375 ng = net_alloc_generic();
376 if (!ng)
377 goto out;
378
379 net = kmem_cache_zalloc(net_cachep, GFP_KERNEL);
380 if (!net)
381 goto out_free;
382
383 rcu_assign_pointer(net->gen, ng);
384out:
385 return net;
386
387out_free:
388 kfree(ng);
389 goto out;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200390}
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200391
Johann Felix Soden45a19b02007-11-07 01:30:30 -0800392static void net_free(struct net *net)
393{
Eric Dumazet416c51e2014-09-09 08:24:53 -0700394 kfree(rcu_access_pointer(net->gen));
Johann Felix Soden45a19b02007-11-07 01:30:30 -0800395 kmem_cache_free(net_cachep, net);
396}
397
Al Viroa685e082011-06-08 21:13:01 -0400398void net_drop_ns(void *p)
399{
400 struct net *ns = p;
Reshetova, Elenac122e142017-06-30 13:08:08 +0300401 if (ns && refcount_dec_and_test(&ns->passive))
Al Viroa685e082011-06-08 21:13:01 -0400402 net_free(ns);
403}
404
Eric W. Biederman038e7332012-06-14 02:31:10 -0700405struct net *copy_net_ns(unsigned long flags,
406 struct user_namespace *user_ns, struct net *old_net)
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700407{
Eric W. Biederman70328662016-08-08 14:33:23 -0500408 struct ucounts *ucounts;
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700409 struct net *net;
410 int rv;
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700411
Rob Landley911cb192011-04-15 02:26:25 +0000412 if (!(flags & CLONE_NEWNET))
413 return get_net(old_net);
414
Eric W. Biederman70328662016-08-08 14:33:23 -0500415 ucounts = inc_net_namespaces(user_ns);
416 if (!ucounts)
Eric W. Biedermandf75e772016-09-22 13:08:36 -0500417 return ERR_PTR(-ENOSPC);
Eric W. Biederman70328662016-08-08 14:33:23 -0500418
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700419 net = net_alloc();
Eric W. Biederman70328662016-08-08 14:33:23 -0500420 if (!net) {
Kirill Tkhai5ba049a2018-02-13 12:26:13 +0300421 rv = -ENOMEM;
422 goto dec_ucounts;
Eric W. Biederman70328662016-08-08 14:33:23 -0500423 }
Kirill Tkhai5ba049a2018-02-13 12:26:13 +0300424 refcount_set(&net->passive, 1);
425 net->ucounts = ucounts;
Eric W. Biederman038e7332012-06-14 02:31:10 -0700426 get_user_ns(user_ns);
427
Kirill Tkhai1a57feb2018-02-13 12:26:23 +0300428 rv = down_read_killable(&net_sem);
Kirill Tkhai5ba049a2018-02-13 12:26:13 +0300429 if (rv < 0)
430 goto put_userns;
Kirill Tkhai447cd7a2018-02-13 12:26:44 +0300431 if (nr_sync_pernet_ops) {
432 rv = mutex_lock_killable(&net_mutex);
433 if (rv < 0)
434 goto up_read;
435 }
Eric W. Biederman038e7332012-06-14 02:31:10 -0700436 rv = setup_net(net, user_ns);
Kirill Tkhai447cd7a2018-02-13 12:26:44 +0300437 if (nr_sync_pernet_ops)
438 mutex_unlock(&net_mutex);
Kirill Tkhai1a57feb2018-02-13 12:26:23 +0300439up_read:
440 up_read(&net_sem);
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700441 if (rv < 0) {
Kirill Tkhai5ba049a2018-02-13 12:26:13 +0300442put_userns:
Eric W. Biederman038e7332012-06-14 02:31:10 -0700443 put_user_ns(user_ns);
Al Viroa685e082011-06-08 21:13:01 -0400444 net_drop_ns(net);
Kirill Tkhai5ba049a2018-02-13 12:26:13 +0300445dec_ucounts:
446 dec_net_namespaces(ucounts);
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700447 return ERR_PTR(rv);
448 }
449 return net;
450}
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800451
Kirill Tkhaifb07a822018-01-19 19:14:53 +0300452static void unhash_nsid(struct net *net, struct net *last)
453{
454 struct net *tmp;
455 /* This function is only called from cleanup_net() work,
456 * and this work is the only process, that may delete
457 * a net from net_namespace_list. So, when the below
458 * is executing, the list may only grow. Thus, we do not
459 * use for_each_net_rcu() or rtnl_lock().
460 */
461 for_each_net(tmp) {
462 int id;
463
464 spin_lock_bh(&tmp->nsid_lock);
465 id = __peernet2id(tmp, net);
466 if (id >= 0)
467 idr_remove(&tmp->netns_ids, id);
468 spin_unlock_bh(&tmp->nsid_lock);
469 if (id >= 0)
470 rtnl_net_notifyid(tmp, RTM_DELNSID, id);
471 if (tmp == last)
472 break;
473 }
474 spin_lock_bh(&net->nsid_lock);
475 idr_destroy(&net->netns_ids);
476 spin_unlock_bh(&net->nsid_lock);
477}
478
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000479static DEFINE_SPINLOCK(cleanup_list_lock);
480static LIST_HEAD(cleanup_list); /* Must hold cleanup_list_lock to touch */
481
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200482static void cleanup_net(struct work_struct *work)
483{
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000484 const struct pernet_operations *ops;
Kirill Tkhaifb07a822018-01-19 19:14:53 +0300485 struct net *net, *tmp, *last;
xiao jin1818ce42014-04-25 08:50:54 +0800486 struct list_head net_kill_list;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000487 LIST_HEAD(net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200488
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000489 /* Atomically snapshot the list of namespaces to cleanup */
490 spin_lock_irq(&cleanup_list_lock);
491 list_replace_init(&cleanup_list, &net_kill_list);
492 spin_unlock_irq(&cleanup_list_lock);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200493
Kirill Tkhai1a57feb2018-02-13 12:26:23 +0300494 down_read(&net_sem);
Kirill Tkhai447cd7a2018-02-13 12:26:44 +0300495 if (nr_sync_pernet_ops)
496 mutex_lock(&net_mutex);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200497
498 /* Don't let anyone else find us. */
Eric W. Biedermanf4618d32007-09-26 22:40:08 -0700499 rtnl_lock();
Kirill Tkhaifb07a822018-01-19 19:14:53 +0300500 list_for_each_entry(net, &net_kill_list, cleanup_list)
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000501 list_del_rcu(&net->list);
Kirill Tkhaifb07a822018-01-19 19:14:53 +0300502 /* Cache last net. After we unlock rtnl, no one new net
503 * added to net_namespace_list can assign nsid pointer
504 * to a net from net_kill_list (see peernet2id_alloc()).
505 * So, we skip them in unhash_nsid().
506 *
507 * Note, that unhash_nsid() does not delete nsid links
508 * between net_kill_list's nets, as they've already
509 * deleted from net_namespace_list. But, this would be
510 * useless anyway, as netns_ids are destroyed there.
511 */
512 last = list_last_entry(&net_namespace_list, struct net, list);
Eric W. Biedermanf4618d32007-09-26 22:40:08 -0700513 rtnl_unlock();
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200514
Kirill Tkhaifb07a822018-01-19 19:14:53 +0300515 list_for_each_entry(net, &net_kill_list, cleanup_list) {
516 unhash_nsid(net, last);
517 list_add_tail(&net->exit_list, &net_exit_list);
518 }
519
Johannes Berg11a28d32009-07-10 09:51:33 +0000520 /*
521 * Another CPU might be rcu-iterating the list, wait for it.
522 * This needs to be before calling the exit() notifiers, so
523 * the rcu_barrier() below isn't sufficient alone.
524 */
525 synchronize_rcu();
526
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200527 /* Run all of the network namespace exit methods */
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000528 list_for_each_entry_reverse(ops, &pernet_list, list)
529 ops_exit_list(ops, &net_exit_list);
530
Kirill Tkhai447cd7a2018-02-13 12:26:44 +0300531 if (nr_sync_pernet_ops)
532 mutex_unlock(&net_mutex);
Kirill Tkhaibcab1dd2018-02-13 12:26:33 +0300533
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000534 /* Free the net generic variables */
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000535 list_for_each_entry_reverse(ops, &pernet_list, list)
536 ops_free_list(ops, &net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200537
Kirill Tkhai1a57feb2018-02-13 12:26:23 +0300538 up_read(&net_sem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200539
540 /* Ensure there are no outstanding rcu callbacks using this
541 * network namespace.
542 */
543 rcu_barrier();
544
545 /* Finally it is safe to free my network namespace structure */
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000546 list_for_each_entry_safe(net, tmp, &net_exit_list, exit_list) {
547 list_del_init(&net->exit_list);
Eric W. Biederman70328662016-08-08 14:33:23 -0500548 dec_net_namespaces(net->ucounts);
Eric W. Biederman038e7332012-06-14 02:31:10 -0700549 put_user_ns(net->user_ns);
Al Viroa685e082011-06-08 21:13:01 -0400550 net_drop_ns(net);
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000551 }
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200552}
Florian Westphal7866cc52017-05-30 11:38:12 +0200553
554/**
555 * net_ns_barrier - wait until concurrent net_cleanup_work is done
556 *
557 * cleanup_net runs from work queue and will first remove namespaces
558 * from the global list, then run net exit functions.
559 *
560 * Call this in module exit path to make sure that all netns
561 * ->exit ops have been invoked before the function is removed.
562 */
563void net_ns_barrier(void)
564{
Kirill Tkhai1a57feb2018-02-13 12:26:23 +0300565 down_write(&net_sem);
Florian Westphal7866cc52017-05-30 11:38:12 +0200566 mutex_lock(&net_mutex);
567 mutex_unlock(&net_mutex);
Kirill Tkhai1a57feb2018-02-13 12:26:23 +0300568 up_write(&net_sem);
Florian Westphal7866cc52017-05-30 11:38:12 +0200569}
570EXPORT_SYMBOL(net_ns_barrier);
571
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000572static DECLARE_WORK(net_cleanup_work, cleanup_net);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200573
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200574void __put_net(struct net *net)
575{
576 /* Cleanup the network namespace in process context */
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000577 unsigned long flags;
578
579 spin_lock_irqsave(&cleanup_list_lock, flags);
580 list_add(&net->cleanup_list, &cleanup_list);
581 spin_unlock_irqrestore(&cleanup_list_lock, flags);
582
583 queue_work(netns_wq, &net_cleanup_work);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200584}
585EXPORT_SYMBOL_GPL(__put_net);
586
Stephen Rothwell956c9202011-05-12 13:51:13 +1000587struct net *get_net_ns_by_fd(int fd)
588{
Stephen Rothwell956c9202011-05-12 13:51:13 +1000589 struct file *file;
Al Viro33c42942014-11-01 02:32:53 -0400590 struct ns_common *ns;
Stephen Rothwell956c9202011-05-12 13:51:13 +1000591 struct net *net;
592
Stephen Rothwell956c9202011-05-12 13:51:13 +1000593 file = proc_ns_fget(fd);
Al Viroc316e6a2011-06-05 00:37:35 +0000594 if (IS_ERR(file))
595 return ERR_CAST(file);
Stephen Rothwell956c9202011-05-12 13:51:13 +1000596
Al Virof77c8012014-11-01 03:13:17 -0400597 ns = get_proc_ns(file_inode(file));
Al Viro33c42942014-11-01 02:32:53 -0400598 if (ns->ops == &netns_operations)
599 net = get_net(container_of(ns, struct net, ns));
Al Viroc316e6a2011-06-05 00:37:35 +0000600 else
601 net = ERR_PTR(-EINVAL);
Stephen Rothwell956c9202011-05-12 13:51:13 +1000602
Al Viroc316e6a2011-06-05 00:37:35 +0000603 fput(file);
Stephen Rothwell956c9202011-05-12 13:51:13 +1000604 return net;
605}
606
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700607#else
Stephen Rothwell956c9202011-05-12 13:51:13 +1000608struct net *get_net_ns_by_fd(int fd)
609{
610 return ERR_PTR(-EINVAL);
611}
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700612#endif
Vadim Kochan4b681c82015-01-12 16:34:05 +0200613EXPORT_SYMBOL_GPL(get_net_ns_by_fd);
Eric W. Biederman9dd776b2007-09-26 22:04:26 -0700614
Johannes Berg30ffee82009-07-10 09:51:35 +0000615struct net *get_net_ns_by_pid(pid_t pid)
616{
617 struct task_struct *tsk;
618 struct net *net;
619
620 /* Lookup the network namespace */
621 net = ERR_PTR(-ESRCH);
622 rcu_read_lock();
623 tsk = find_task_by_vpid(pid);
624 if (tsk) {
625 struct nsproxy *nsproxy;
Eric W. Biederman728dba32014-02-03 19:13:49 -0800626 task_lock(tsk);
627 nsproxy = tsk->nsproxy;
Johannes Berg30ffee82009-07-10 09:51:35 +0000628 if (nsproxy)
629 net = get_net(nsproxy->net_ns);
Eric W. Biederman728dba32014-02-03 19:13:49 -0800630 task_unlock(tsk);
Johannes Berg30ffee82009-07-10 09:51:35 +0000631 }
632 rcu_read_unlock();
633 return net;
634}
635EXPORT_SYMBOL_GPL(get_net_ns_by_pid);
636
Eric W. Biederman98f842e2011-06-15 10:21:48 -0700637static __net_init int net_ns_net_init(struct net *net)
638{
Al Viro33c42942014-11-01 02:32:53 -0400639#ifdef CONFIG_NET_NS
640 net->ns.ops = &netns_operations;
641#endif
Al Viro6344c432014-11-01 00:45:45 -0400642 return ns_alloc_inum(&net->ns);
Eric W. Biederman98f842e2011-06-15 10:21:48 -0700643}
644
645static __net_exit void net_ns_net_exit(struct net *net)
646{
Al Viro6344c432014-11-01 00:45:45 -0400647 ns_free_inum(&net->ns);
Eric W. Biederman98f842e2011-06-15 10:21:48 -0700648}
649
650static struct pernet_operations __net_initdata net_ns_ops = {
651 .init = net_ns_net_init,
652 .exit = net_ns_net_exit,
Kirill Tkhai3fc3b822018-02-13 12:27:03 +0300653 .async = true,
Eric W. Biederman98f842e2011-06-15 10:21:48 -0700654};
655
stephen hemminger3ee52562016-08-31 15:17:49 -0700656static const struct nla_policy rtnl_net_policy[NETNSA_MAX + 1] = {
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100657 [NETNSA_NONE] = { .type = NLA_UNSPEC },
658 [NETNSA_NSID] = { .type = NLA_S32 },
659 [NETNSA_PID] = { .type = NLA_U32 },
660 [NETNSA_FD] = { .type = NLA_U32 },
661};
662
David Ahernc21ef3e2017-04-16 09:48:24 -0700663static int rtnl_net_newid(struct sk_buff *skb, struct nlmsghdr *nlh,
664 struct netlink_ext_ack *extack)
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100665{
666 struct net *net = sock_net(skb->sk);
667 struct nlattr *tb[NETNSA_MAX + 1];
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200668 struct nlattr *nla;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100669 struct net *peer;
670 int nsid, err;
671
672 err = nlmsg_parse(nlh, sizeof(struct rtgenmsg), tb, NETNSA_MAX,
David Ahernc21ef3e2017-04-16 09:48:24 -0700673 rtnl_net_policy, extack);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100674 if (err < 0)
675 return err;
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200676 if (!tb[NETNSA_NSID]) {
677 NL_SET_ERR_MSG(extack, "nsid is missing");
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100678 return -EINVAL;
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200679 }
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100680 nsid = nla_get_s32(tb[NETNSA_NSID]);
681
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200682 if (tb[NETNSA_PID]) {
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100683 peer = get_net_ns_by_pid(nla_get_u32(tb[NETNSA_PID]));
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200684 nla = tb[NETNSA_PID];
685 } else if (tb[NETNSA_FD]) {
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100686 peer = get_net_ns_by_fd(nla_get_u32(tb[NETNSA_FD]));
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200687 nla = tb[NETNSA_FD];
688 } else {
689 NL_SET_ERR_MSG(extack, "Peer netns reference is missing");
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100690 return -EINVAL;
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200691 }
692 if (IS_ERR(peer)) {
693 NL_SET_BAD_ATTR(extack, nla);
694 NL_SET_ERR_MSG(extack, "Peer netns reference is invalid");
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100695 return PTR_ERR(peer);
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200696 }
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100697
Paul Moorefba143c2016-11-29 16:57:48 -0500698 spin_lock_bh(&net->nsid_lock);
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200699 if (__peernet2id(net, peer) >= 0) {
Paul Moorefba143c2016-11-29 16:57:48 -0500700 spin_unlock_bh(&net->nsid_lock);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100701 err = -EEXIST;
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200702 NL_SET_BAD_ATTR(extack, nla);
703 NL_SET_ERR_MSG(extack,
704 "Peer netns already has a nsid assigned");
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100705 goto out;
706 }
707
708 err = alloc_netid(net, peer, nsid);
Paul Moorefba143c2016-11-29 16:57:48 -0500709 spin_unlock_bh(&net->nsid_lock);
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200710 if (err >= 0) {
711 rtnl_net_notifyid(net, RTM_NEWNSID, err);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100712 err = 0;
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200713 } else if (err == -ENOSPC && nsid >= 0) {
Nicolas Dichtel10d486a2017-06-09 14:41:57 +0200714 err = -EEXIST;
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200715 NL_SET_BAD_ATTR(extack, tb[NETNSA_NSID]);
716 NL_SET_ERR_MSG(extack, "The specified nsid is already used");
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200717 }
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100718out:
719 put_net(peer);
720 return err;
721}
722
723static int rtnl_net_get_size(void)
724{
725 return NLMSG_ALIGN(sizeof(struct rtgenmsg))
726 + nla_total_size(sizeof(s32)) /* NETNSA_NSID */
727 ;
728}
729
730static int rtnl_net_fill(struct sk_buff *skb, u32 portid, u32 seq, int flags,
Nicolas Dichtelcab3c8e2015-05-07 11:02:48 +0200731 int cmd, struct net *net, int nsid)
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100732{
733 struct nlmsghdr *nlh;
734 struct rtgenmsg *rth;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100735
736 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rth), flags);
737 if (!nlh)
738 return -EMSGSIZE;
739
740 rth = nlmsg_data(nlh);
741 rth->rtgen_family = AF_UNSPEC;
742
Nicolas Dichtelcab3c8e2015-05-07 11:02:48 +0200743 if (nla_put_s32(skb, NETNSA_NSID, nsid))
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100744 goto nla_put_failure;
745
746 nlmsg_end(skb, nlh);
747 return 0;
748
749nla_put_failure:
750 nlmsg_cancel(skb, nlh);
751 return -EMSGSIZE;
752}
753
David Ahernc21ef3e2017-04-16 09:48:24 -0700754static int rtnl_net_getid(struct sk_buff *skb, struct nlmsghdr *nlh,
755 struct netlink_ext_ack *extack)
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100756{
757 struct net *net = sock_net(skb->sk);
758 struct nlattr *tb[NETNSA_MAX + 1];
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200759 struct nlattr *nla;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100760 struct sk_buff *msg;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100761 struct net *peer;
Nicolas Dichtelcab3c8e2015-05-07 11:02:48 +0200762 int err, id;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100763
764 err = nlmsg_parse(nlh, sizeof(struct rtgenmsg), tb, NETNSA_MAX,
David Ahernc21ef3e2017-04-16 09:48:24 -0700765 rtnl_net_policy, extack);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100766 if (err < 0)
767 return err;
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200768 if (tb[NETNSA_PID]) {
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100769 peer = get_net_ns_by_pid(nla_get_u32(tb[NETNSA_PID]));
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200770 nla = tb[NETNSA_PID];
771 } else if (tb[NETNSA_FD]) {
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100772 peer = get_net_ns_by_fd(nla_get_u32(tb[NETNSA_FD]));
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200773 nla = tb[NETNSA_FD];
774 } else {
775 NL_SET_ERR_MSG(extack, "Peer netns reference is missing");
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100776 return -EINVAL;
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200777 }
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100778
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200779 if (IS_ERR(peer)) {
780 NL_SET_BAD_ATTR(extack, nla);
781 NL_SET_ERR_MSG(extack, "Peer netns reference is invalid");
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100782 return PTR_ERR(peer);
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200783 }
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100784
785 msg = nlmsg_new(rtnl_net_get_size(), GFP_KERNEL);
786 if (!msg) {
787 err = -ENOMEM;
788 goto out;
789 }
790
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200791 id = peernet2id(net, peer);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100792 err = rtnl_net_fill(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
David S. Millerb04096f2015-05-13 14:31:43 -0400793 RTM_NEWNSID, net, id);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100794 if (err < 0)
795 goto err_out;
796
797 err = rtnl_unicast(msg, net, NETLINK_CB(skb).portid);
798 goto out;
799
800err_out:
801 nlmsg_free(msg);
802out:
803 put_net(peer);
804 return err;
805}
806
Nicolas Dichtela143c402015-04-07 11:51:54 +0200807struct rtnl_net_dump_cb {
808 struct net *net;
809 struct sk_buff *skb;
810 struct netlink_callback *cb;
811 int idx;
812 int s_idx;
813};
814
815static int rtnl_net_dumpid_one(int id, void *peer, void *data)
816{
817 struct rtnl_net_dump_cb *net_cb = (struct rtnl_net_dump_cb *)data;
818 int ret;
819
820 if (net_cb->idx < net_cb->s_idx)
821 goto cont;
822
823 ret = rtnl_net_fill(net_cb->skb, NETLINK_CB(net_cb->cb->skb).portid,
824 net_cb->cb->nlh->nlmsg_seq, NLM_F_MULTI,
Nicolas Dichtelcab3c8e2015-05-07 11:02:48 +0200825 RTM_NEWNSID, net_cb->net, id);
Nicolas Dichtela143c402015-04-07 11:51:54 +0200826 if (ret < 0)
827 return ret;
828
829cont:
830 net_cb->idx++;
831 return 0;
832}
833
834static int rtnl_net_dumpid(struct sk_buff *skb, struct netlink_callback *cb)
835{
836 struct net *net = sock_net(skb->sk);
837 struct rtnl_net_dump_cb net_cb = {
838 .net = net,
839 .skb = skb,
840 .cb = cb,
841 .idx = 0,
842 .s_idx = cb->args[0],
843 };
844
Paul Moorefba143c2016-11-29 16:57:48 -0500845 spin_lock_bh(&net->nsid_lock);
Nicolas Dichtela143c402015-04-07 11:51:54 +0200846 idr_for_each(&net->netns_ids, rtnl_net_dumpid_one, &net_cb);
Paul Moorefba143c2016-11-29 16:57:48 -0500847 spin_unlock_bh(&net->nsid_lock);
Nicolas Dichtela143c402015-04-07 11:51:54 +0200848
849 cb->args[0] = net_cb.idx;
850 return skb->len;
851}
852
Nicolas Dichtelcab3c8e2015-05-07 11:02:48 +0200853static void rtnl_net_notifyid(struct net *net, int cmd, int id)
Nicolas Dichtel9a963452015-04-07 11:51:53 +0200854{
855 struct sk_buff *msg;
856 int err = -ENOMEM;
857
858 msg = nlmsg_new(rtnl_net_get_size(), GFP_KERNEL);
859 if (!msg)
860 goto out;
861
Nicolas Dichtelcab3c8e2015-05-07 11:02:48 +0200862 err = rtnl_net_fill(msg, 0, 0, 0, cmd, net, id);
Nicolas Dichtel9a963452015-04-07 11:51:53 +0200863 if (err < 0)
864 goto err_out;
865
866 rtnl_notify(msg, net, 0, RTNLGRP_NSID, NULL, 0);
867 return;
868
869err_out:
870 nlmsg_free(msg);
871out:
872 rtnl_set_sk_err(net, RTNLGRP_NSID, err);
873}
874
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200875static int __init net_ns_init(void)
876{
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800877 struct net_generic *ng;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200878
Pavel Emelyanovd57a9212007-11-01 00:46:50 -0700879#ifdef CONFIG_NET_NS
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200880 net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
881 SMP_CACHE_BYTES,
882 SLAB_PANIC, NULL);
Benjamin Thery3ef13552007-11-19 23:18:16 -0800883
884 /* Create workqueue for cleanup */
885 netns_wq = create_singlethread_workqueue("netns");
886 if (!netns_wq)
887 panic("Could not create netns workq");
Pavel Emelyanovd57a9212007-11-01 00:46:50 -0700888#endif
Benjamin Thery3ef13552007-11-19 23:18:16 -0800889
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800890 ng = net_alloc_generic();
891 if (!ng)
892 panic("Could not allocate generic netns");
893
894 rcu_assign_pointer(init_net.gen, ng);
895
Kirill Tkhai1a57feb2018-02-13 12:26:23 +0300896 down_write(&net_sem);
Eric W. Biederman038e7332012-06-14 02:31:10 -0700897 if (setup_net(&init_net, &init_user_ns))
Stephen Hemmingerca0f3112009-05-21 15:10:31 -0700898 panic("Could not setup the initial network namespace");
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200899
Dmitry Torokhovf8c46cb2016-08-10 14:36:00 -0700900 init_net_initialized = true;
Kirill Tkhai1a57feb2018-02-13 12:26:23 +0300901 up_write(&net_sem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200902
Eric W. Biederman98f842e2011-06-15 10:21:48 -0700903 register_pernet_subsys(&net_ns_ops);
904
Florian Westphal165b9112017-08-09 20:41:53 +0200905 rtnl_register(PF_UNSPEC, RTM_NEWNSID, rtnl_net_newid, NULL,
906 RTNL_FLAG_DOIT_UNLOCKED);
Nicolas Dichtela143c402015-04-07 11:51:54 +0200907 rtnl_register(PF_UNSPEC, RTM_GETNSID, rtnl_net_getid, rtnl_net_dumpid,
Florian Westphal165b9112017-08-09 20:41:53 +0200908 RTNL_FLAG_DOIT_UNLOCKED);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100909
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200910 return 0;
911}
912
913pure_initcall(net_ns_init);
914
Denis V. Luneved160e82007-11-13 03:23:21 -0800915#ifdef CONFIG_NET_NS
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000916static int __register_pernet_operations(struct list_head *list,
917 struct pernet_operations *ops)
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200918{
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000919 struct net *net;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200920 int error;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000921 LIST_HEAD(net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200922
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200923 list_add_tail(&ops->list, list);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000924 if (ops->init || (ops->id && ops->size)) {
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700925 for_each_net(net) {
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000926 error = ops_init(ops, net);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200927 if (error)
928 goto out_undo;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000929 list_add_tail(&net->exit_list, &net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200930 }
931 }
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700932 return 0;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200933
934out_undo:
935 /* If I have an error cleanup all namespaces I initialized */
936 list_del(&ops->list);
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000937 ops_exit_list(ops, &net_exit_list);
938 ops_free_list(ops, &net_exit_list);
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700939 return error;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200940}
941
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000942static void __unregister_pernet_operations(struct pernet_operations *ops)
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200943{
944 struct net *net;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000945 LIST_HEAD(net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200946
947 list_del(&ops->list);
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000948 for_each_net(net)
949 list_add_tail(&net->exit_list, &net_exit_list);
950 ops_exit_list(ops, &net_exit_list);
951 ops_free_list(ops, &net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200952}
953
Denis V. Luneved160e82007-11-13 03:23:21 -0800954#else
955
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000956static int __register_pernet_operations(struct list_head *list,
957 struct pernet_operations *ops)
958{
Dmitry Torokhovf8c46cb2016-08-10 14:36:00 -0700959 if (!init_net_initialized) {
960 list_add_tail(&ops->list, list);
961 return 0;
962 }
963
Julian Anastasovb9229342012-04-16 04:43:15 +0000964 return ops_init(ops, &init_net);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000965}
966
967static void __unregister_pernet_operations(struct pernet_operations *ops)
968{
Dmitry Torokhovf8c46cb2016-08-10 14:36:00 -0700969 if (!init_net_initialized) {
970 list_del(&ops->list);
971 } else {
972 LIST_HEAD(net_exit_list);
973 list_add(&init_net.exit_list, &net_exit_list);
974 ops_exit_list(ops, &net_exit_list);
975 ops_free_list(ops, &net_exit_list);
976 }
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000977}
978
979#endif /* CONFIG_NET_NS */
980
981static DEFINE_IDA(net_generic_ids);
982
Denis V. Luneved160e82007-11-13 03:23:21 -0800983static int register_pernet_operations(struct list_head *list,
984 struct pernet_operations *ops)
985{
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000986 int error;
987
988 if (ops->id) {
989again:
Alexey Dobriyan6af2d5f2016-12-02 04:21:32 +0300990 error = ida_get_new_above(&net_generic_ids, MIN_PERNET_OPS_ID, ops->id);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000991 if (error < 0) {
992 if (error == -EAGAIN) {
993 ida_pre_get(&net_generic_ids, GFP_KERNEL);
994 goto again;
995 }
996 return error;
997 }
Alexey Dobriyan6af2d5f2016-12-02 04:21:32 +0300998 max_gen_ptrs = max(max_gen_ptrs, *ops->id + 1);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000999 }
1000 error = __register_pernet_operations(list, ops);
Eric W. Biederman3a765ed2009-12-03 02:29:06 +00001001 if (error) {
1002 rcu_barrier();
1003 if (ops->id)
1004 ida_remove(&net_generic_ids, *ops->id);
Kirill Tkhai447cd7a2018-02-13 12:26:44 +03001005 } else if (!ops->async) {
1006 pr_info_once("Pernet operations %ps are sync.\n", ops);
1007 nr_sync_pernet_ops++;
Eric W. Biederman3a765ed2009-12-03 02:29:06 +00001008 }
Eric W. Biedermanf875bae2009-11-29 22:25:28 +00001009
1010 return error;
Denis V. Luneved160e82007-11-13 03:23:21 -08001011}
1012
1013static void unregister_pernet_operations(struct pernet_operations *ops)
1014{
Kirill Tkhai447cd7a2018-02-13 12:26:44 +03001015 if (!ops->async)
1016 BUG_ON(nr_sync_pernet_ops-- == 0);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +00001017 __unregister_pernet_operations(ops);
Eric W. Biederman3a765ed2009-12-03 02:29:06 +00001018 rcu_barrier();
Eric W. Biedermanf875bae2009-11-29 22:25:28 +00001019 if (ops->id)
1020 ida_remove(&net_generic_ids, *ops->id);
Denis V. Luneved160e82007-11-13 03:23:21 -08001021}
Pavel Emelyanovc93cf612008-04-15 00:35:23 -07001022
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001023/**
1024 * register_pernet_subsys - register a network namespace subsystem
1025 * @ops: pernet operations structure for the subsystem
1026 *
1027 * Register a subsystem which has init and exit functions
1028 * that are called when network namespaces are created and
1029 * destroyed respectively.
1030 *
1031 * When registered all network namespace init functions are
1032 * called for every existing network namespace. Allowing kernel
1033 * modules to have a race free view of the set of network namespaces.
1034 *
1035 * When a new network namespace is created all of the init
1036 * methods are called in the order in which they were registered.
1037 *
1038 * When a network namespace is destroyed all of the exit methods
1039 * are called in the reverse of the order with which they were
1040 * registered.
1041 */
1042int register_pernet_subsys(struct pernet_operations *ops)
1043{
1044 int error;
Kirill Tkhai1a57feb2018-02-13 12:26:23 +03001045 down_write(&net_sem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001046 error = register_pernet_operations(first_device, ops);
Kirill Tkhai1a57feb2018-02-13 12:26:23 +03001047 up_write(&net_sem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001048 return error;
1049}
1050EXPORT_SYMBOL_GPL(register_pernet_subsys);
1051
1052/**
1053 * unregister_pernet_subsys - unregister a network namespace subsystem
1054 * @ops: pernet operations structure to manipulate
1055 *
1056 * Remove the pernet operations structure from the list to be
Oliver Pinter53379e52008-02-03 17:56:48 +02001057 * used when network namespaces are created or destroyed. In
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001058 * addition run the exit method for all existing network
1059 * namespaces.
1060 */
Jiri Pirkob3c981d2010-04-25 00:49:56 -07001061void unregister_pernet_subsys(struct pernet_operations *ops)
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001062{
Kirill Tkhai1a57feb2018-02-13 12:26:23 +03001063 down_write(&net_sem);
Jiri Pirkob3c981d2010-04-25 00:49:56 -07001064 unregister_pernet_operations(ops);
Kirill Tkhai1a57feb2018-02-13 12:26:23 +03001065 up_write(&net_sem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001066}
1067EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
1068
1069/**
1070 * register_pernet_device - register a network namespace device
1071 * @ops: pernet operations structure for the subsystem
1072 *
1073 * Register a device which has init and exit functions
1074 * that are called when network namespaces are created and
1075 * destroyed respectively.
1076 *
1077 * When registered all network namespace init functions are
1078 * called for every existing network namespace. Allowing kernel
1079 * modules to have a race free view of the set of network namespaces.
1080 *
1081 * When a new network namespace is created all of the init
1082 * methods are called in the order in which they were registered.
1083 *
1084 * When a network namespace is destroyed all of the exit methods
1085 * are called in the reverse of the order with which they were
1086 * registered.
1087 */
1088int register_pernet_device(struct pernet_operations *ops)
1089{
1090 int error;
Kirill Tkhai1a57feb2018-02-13 12:26:23 +03001091 down_write(&net_sem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001092 error = register_pernet_operations(&pernet_list, ops);
1093 if (!error && (first_device == &pernet_list))
1094 first_device = &ops->list;
Kirill Tkhai1a57feb2018-02-13 12:26:23 +03001095 up_write(&net_sem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001096 return error;
1097}
1098EXPORT_SYMBOL_GPL(register_pernet_device);
1099
1100/**
1101 * unregister_pernet_device - unregister a network namespace netdevice
1102 * @ops: pernet operations structure to manipulate
1103 *
1104 * Remove the pernet operations structure from the list to be
Oliver Pinter53379e52008-02-03 17:56:48 +02001105 * used when network namespaces are created or destroyed. In
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001106 * addition run the exit method for all existing network
1107 * namespaces.
1108 */
1109void unregister_pernet_device(struct pernet_operations *ops)
1110{
Kirill Tkhai1a57feb2018-02-13 12:26:23 +03001111 down_write(&net_sem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001112 if (&ops->list == first_device)
1113 first_device = first_device->next;
1114 unregister_pernet_operations(ops);
Kirill Tkhai1a57feb2018-02-13 12:26:23 +03001115 up_write(&net_sem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001116}
1117EXPORT_SYMBOL_GPL(unregister_pernet_device);
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001118
1119#ifdef CONFIG_NET_NS
Al Viro64964522014-11-01 00:37:32 -04001120static struct ns_common *netns_get(struct task_struct *task)
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001121{
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001122 struct net *net = NULL;
1123 struct nsproxy *nsproxy;
1124
Eric W. Biederman728dba32014-02-03 19:13:49 -08001125 task_lock(task);
1126 nsproxy = task->nsproxy;
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001127 if (nsproxy)
1128 net = get_net(nsproxy->net_ns);
Eric W. Biederman728dba32014-02-03 19:13:49 -08001129 task_unlock(task);
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001130
Al Viroff248702014-11-01 00:10:50 -04001131 return net ? &net->ns : NULL;
1132}
1133
1134static inline struct net *to_net_ns(struct ns_common *ns)
1135{
1136 return container_of(ns, struct net, ns);
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001137}
1138
Al Viro64964522014-11-01 00:37:32 -04001139static void netns_put(struct ns_common *ns)
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001140{
Al Viroff248702014-11-01 00:10:50 -04001141 put_net(to_net_ns(ns));
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001142}
1143
Al Viro64964522014-11-01 00:37:32 -04001144static int netns_install(struct nsproxy *nsproxy, struct ns_common *ns)
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001145{
Al Viroff248702014-11-01 00:10:50 -04001146 struct net *net = to_net_ns(ns);
Eric W. Biederman142e1d12012-07-26 01:13:20 -07001147
Eric W. Biederman5e4a0842012-12-14 07:55:36 -08001148 if (!ns_capable(net->user_ns, CAP_SYS_ADMIN) ||
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -07001149 !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
Eric W. Biederman142e1d12012-07-26 01:13:20 -07001150 return -EPERM;
1151
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001152 put_net(nsproxy->net_ns);
Eric W. Biederman142e1d12012-07-26 01:13:20 -07001153 nsproxy->net_ns = get_net(net);
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001154 return 0;
1155}
1156
Andrey Vaginbcac25a2016-09-06 00:47:13 -07001157static struct user_namespace *netns_owner(struct ns_common *ns)
1158{
1159 return to_net_ns(ns)->user_ns;
1160}
1161
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001162const struct proc_ns_operations netns_operations = {
1163 .name = "net",
1164 .type = CLONE_NEWNET,
1165 .get = netns_get,
1166 .put = netns_put,
1167 .install = netns_install,
Andrey Vaginbcac25a2016-09-06 00:47:13 -07001168 .owner = netns_owner,
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001169};
1170#endif