blob: 2a01ff32d9c79b8e128775de537076452f9aa997 [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,
343};
344
345static __init int net_defaults_init(void)
346{
347 if (register_pernet_subsys(&net_defaults_ops))
348 panic("Cannot initialize net default settings");
349
350 return 0;
351}
352
353core_initcall(net_defaults_init);
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800354
Clemens Nossebe47d42009-02-23 15:37:35 -0800355#ifdef CONFIG_NET_NS
Arnd Bergmann2ed6afd2016-09-23 18:06:12 +0200356static struct ucounts *inc_net_namespaces(struct user_namespace *ns)
357{
358 return inc_ucount(ns, current_euid(), UCOUNT_NET_NAMESPACES);
359}
360
361static void dec_net_namespaces(struct ucounts *ucounts)
362{
363 dec_ucount(ucounts, UCOUNT_NET_NAMESPACES);
364}
365
Clemens Nossebe47d42009-02-23 15:37:35 -0800366static struct kmem_cache *net_cachep;
367static struct workqueue_struct *netns_wq;
368
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200369static struct net *net_alloc(void)
370{
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800371 struct net *net = NULL;
372 struct net_generic *ng;
373
374 ng = net_alloc_generic();
375 if (!ng)
376 goto out;
377
378 net = kmem_cache_zalloc(net_cachep, GFP_KERNEL);
379 if (!net)
380 goto out_free;
381
382 rcu_assign_pointer(net->gen, ng);
383out:
384 return net;
385
386out_free:
387 kfree(ng);
388 goto out;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200389}
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200390
Johann Felix Soden45a19b02007-11-07 01:30:30 -0800391static void net_free(struct net *net)
392{
Eric Dumazet416c51e2014-09-09 08:24:53 -0700393 kfree(rcu_access_pointer(net->gen));
Johann Felix Soden45a19b02007-11-07 01:30:30 -0800394 kmem_cache_free(net_cachep, net);
395}
396
Al Viroa685e082011-06-08 21:13:01 -0400397void net_drop_ns(void *p)
398{
399 struct net *ns = p;
Reshetova, Elenac122e142017-06-30 13:08:08 +0300400 if (ns && refcount_dec_and_test(&ns->passive))
Al Viroa685e082011-06-08 21:13:01 -0400401 net_free(ns);
402}
403
Eric W. Biederman038e7332012-06-14 02:31:10 -0700404struct net *copy_net_ns(unsigned long flags,
405 struct user_namespace *user_ns, struct net *old_net)
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700406{
Eric W. Biederman70328662016-08-08 14:33:23 -0500407 struct ucounts *ucounts;
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700408 struct net *net;
409 int rv;
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700410
Rob Landley911cb192011-04-15 02:26:25 +0000411 if (!(flags & CLONE_NEWNET))
412 return get_net(old_net);
413
Eric W. Biederman70328662016-08-08 14:33:23 -0500414 ucounts = inc_net_namespaces(user_ns);
415 if (!ucounts)
Eric W. Biedermandf75e772016-09-22 13:08:36 -0500416 return ERR_PTR(-ENOSPC);
Eric W. Biederman70328662016-08-08 14:33:23 -0500417
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700418 net = net_alloc();
Eric W. Biederman70328662016-08-08 14:33:23 -0500419 if (!net) {
Kirill Tkhai5ba049a2018-02-13 12:26:13 +0300420 rv = -ENOMEM;
421 goto dec_ucounts;
Eric W. Biederman70328662016-08-08 14:33:23 -0500422 }
Kirill Tkhai5ba049a2018-02-13 12:26:13 +0300423 refcount_set(&net->passive, 1);
424 net->ucounts = ucounts;
Eric W. Biederman038e7332012-06-14 02:31:10 -0700425 get_user_ns(user_ns);
426
Kirill Tkhai1a57feb2018-02-13 12:26:23 +0300427 rv = down_read_killable(&net_sem);
Kirill Tkhai5ba049a2018-02-13 12:26:13 +0300428 if (rv < 0)
429 goto put_userns;
Kirill Tkhai447cd7a2018-02-13 12:26:44 +0300430 if (nr_sync_pernet_ops) {
431 rv = mutex_lock_killable(&net_mutex);
432 if (rv < 0)
433 goto up_read;
434 }
Eric W. Biederman038e7332012-06-14 02:31:10 -0700435 rv = setup_net(net, user_ns);
Kirill Tkhai447cd7a2018-02-13 12:26:44 +0300436 if (nr_sync_pernet_ops)
437 mutex_unlock(&net_mutex);
Kirill Tkhai1a57feb2018-02-13 12:26:23 +0300438up_read:
439 up_read(&net_sem);
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700440 if (rv < 0) {
Kirill Tkhai5ba049a2018-02-13 12:26:13 +0300441put_userns:
Eric W. Biederman038e7332012-06-14 02:31:10 -0700442 put_user_ns(user_ns);
Al Viroa685e082011-06-08 21:13:01 -0400443 net_drop_ns(net);
Kirill Tkhai5ba049a2018-02-13 12:26:13 +0300444dec_ucounts:
445 dec_net_namespaces(ucounts);
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700446 return ERR_PTR(rv);
447 }
448 return net;
449}
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800450
Kirill Tkhaifb07a822018-01-19 19:14:53 +0300451static void unhash_nsid(struct net *net, struct net *last)
452{
453 struct net *tmp;
454 /* This function is only called from cleanup_net() work,
455 * and this work is the only process, that may delete
456 * a net from net_namespace_list. So, when the below
457 * is executing, the list may only grow. Thus, we do not
458 * use for_each_net_rcu() or rtnl_lock().
459 */
460 for_each_net(tmp) {
461 int id;
462
463 spin_lock_bh(&tmp->nsid_lock);
464 id = __peernet2id(tmp, net);
465 if (id >= 0)
466 idr_remove(&tmp->netns_ids, id);
467 spin_unlock_bh(&tmp->nsid_lock);
468 if (id >= 0)
469 rtnl_net_notifyid(tmp, RTM_DELNSID, id);
470 if (tmp == last)
471 break;
472 }
473 spin_lock_bh(&net->nsid_lock);
474 idr_destroy(&net->netns_ids);
475 spin_unlock_bh(&net->nsid_lock);
476}
477
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000478static DEFINE_SPINLOCK(cleanup_list_lock);
479static LIST_HEAD(cleanup_list); /* Must hold cleanup_list_lock to touch */
480
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200481static void cleanup_net(struct work_struct *work)
482{
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000483 const struct pernet_operations *ops;
Kirill Tkhaifb07a822018-01-19 19:14:53 +0300484 struct net *net, *tmp, *last;
xiao jin1818ce42014-04-25 08:50:54 +0800485 struct list_head net_kill_list;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000486 LIST_HEAD(net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200487
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000488 /* Atomically snapshot the list of namespaces to cleanup */
489 spin_lock_irq(&cleanup_list_lock);
490 list_replace_init(&cleanup_list, &net_kill_list);
491 spin_unlock_irq(&cleanup_list_lock);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200492
Kirill Tkhai1a57feb2018-02-13 12:26:23 +0300493 down_read(&net_sem);
Kirill Tkhai447cd7a2018-02-13 12:26:44 +0300494 if (nr_sync_pernet_ops)
495 mutex_lock(&net_mutex);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200496
497 /* Don't let anyone else find us. */
Eric W. Biedermanf4618d32007-09-26 22:40:08 -0700498 rtnl_lock();
Kirill Tkhaifb07a822018-01-19 19:14:53 +0300499 list_for_each_entry(net, &net_kill_list, cleanup_list)
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000500 list_del_rcu(&net->list);
Kirill Tkhaifb07a822018-01-19 19:14:53 +0300501 /* Cache last net. After we unlock rtnl, no one new net
502 * added to net_namespace_list can assign nsid pointer
503 * to a net from net_kill_list (see peernet2id_alloc()).
504 * So, we skip them in unhash_nsid().
505 *
506 * Note, that unhash_nsid() does not delete nsid links
507 * between net_kill_list's nets, as they've already
508 * deleted from net_namespace_list. But, this would be
509 * useless anyway, as netns_ids are destroyed there.
510 */
511 last = list_last_entry(&net_namespace_list, struct net, list);
Eric W. Biedermanf4618d32007-09-26 22:40:08 -0700512 rtnl_unlock();
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200513
Kirill Tkhaifb07a822018-01-19 19:14:53 +0300514 list_for_each_entry(net, &net_kill_list, cleanup_list) {
515 unhash_nsid(net, last);
516 list_add_tail(&net->exit_list, &net_exit_list);
517 }
518
Johannes Berg11a28d32009-07-10 09:51:33 +0000519 /*
520 * Another CPU might be rcu-iterating the list, wait for it.
521 * This needs to be before calling the exit() notifiers, so
522 * the rcu_barrier() below isn't sufficient alone.
523 */
524 synchronize_rcu();
525
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200526 /* Run all of the network namespace exit methods */
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000527 list_for_each_entry_reverse(ops, &pernet_list, list)
528 ops_exit_list(ops, &net_exit_list);
529
Kirill Tkhai447cd7a2018-02-13 12:26:44 +0300530 if (nr_sync_pernet_ops)
531 mutex_unlock(&net_mutex);
Kirill Tkhaibcab1dd2018-02-13 12:26:33 +0300532
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000533 /* Free the net generic variables */
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000534 list_for_each_entry_reverse(ops, &pernet_list, list)
535 ops_free_list(ops, &net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200536
Kirill Tkhai1a57feb2018-02-13 12:26:23 +0300537 up_read(&net_sem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200538
539 /* Ensure there are no outstanding rcu callbacks using this
540 * network namespace.
541 */
542 rcu_barrier();
543
544 /* Finally it is safe to free my network namespace structure */
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000545 list_for_each_entry_safe(net, tmp, &net_exit_list, exit_list) {
546 list_del_init(&net->exit_list);
Eric W. Biederman70328662016-08-08 14:33:23 -0500547 dec_net_namespaces(net->ucounts);
Eric W. Biederman038e7332012-06-14 02:31:10 -0700548 put_user_ns(net->user_ns);
Al Viroa685e082011-06-08 21:13:01 -0400549 net_drop_ns(net);
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000550 }
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200551}
Florian Westphal7866cc52017-05-30 11:38:12 +0200552
553/**
554 * net_ns_barrier - wait until concurrent net_cleanup_work is done
555 *
556 * cleanup_net runs from work queue and will first remove namespaces
557 * from the global list, then run net exit functions.
558 *
559 * Call this in module exit path to make sure that all netns
560 * ->exit ops have been invoked before the function is removed.
561 */
562void net_ns_barrier(void)
563{
Kirill Tkhai1a57feb2018-02-13 12:26:23 +0300564 down_write(&net_sem);
Florian Westphal7866cc52017-05-30 11:38:12 +0200565 mutex_lock(&net_mutex);
566 mutex_unlock(&net_mutex);
Kirill Tkhai1a57feb2018-02-13 12:26:23 +0300567 up_write(&net_sem);
Florian Westphal7866cc52017-05-30 11:38:12 +0200568}
569EXPORT_SYMBOL(net_ns_barrier);
570
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000571static DECLARE_WORK(net_cleanup_work, cleanup_net);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200572
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200573void __put_net(struct net *net)
574{
575 /* Cleanup the network namespace in process context */
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000576 unsigned long flags;
577
578 spin_lock_irqsave(&cleanup_list_lock, flags);
579 list_add(&net->cleanup_list, &cleanup_list);
580 spin_unlock_irqrestore(&cleanup_list_lock, flags);
581
582 queue_work(netns_wq, &net_cleanup_work);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200583}
584EXPORT_SYMBOL_GPL(__put_net);
585
Stephen Rothwell956c9202011-05-12 13:51:13 +1000586struct net *get_net_ns_by_fd(int fd)
587{
Stephen Rothwell956c9202011-05-12 13:51:13 +1000588 struct file *file;
Al Viro33c42942014-11-01 02:32:53 -0400589 struct ns_common *ns;
Stephen Rothwell956c9202011-05-12 13:51:13 +1000590 struct net *net;
591
Stephen Rothwell956c9202011-05-12 13:51:13 +1000592 file = proc_ns_fget(fd);
Al Viroc316e6a2011-06-05 00:37:35 +0000593 if (IS_ERR(file))
594 return ERR_CAST(file);
Stephen Rothwell956c9202011-05-12 13:51:13 +1000595
Al Virof77c8012014-11-01 03:13:17 -0400596 ns = get_proc_ns(file_inode(file));
Al Viro33c42942014-11-01 02:32:53 -0400597 if (ns->ops == &netns_operations)
598 net = get_net(container_of(ns, struct net, ns));
Al Viroc316e6a2011-06-05 00:37:35 +0000599 else
600 net = ERR_PTR(-EINVAL);
Stephen Rothwell956c9202011-05-12 13:51:13 +1000601
Al Viroc316e6a2011-06-05 00:37:35 +0000602 fput(file);
Stephen Rothwell956c9202011-05-12 13:51:13 +1000603 return net;
604}
605
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700606#else
Stephen Rothwell956c9202011-05-12 13:51:13 +1000607struct net *get_net_ns_by_fd(int fd)
608{
609 return ERR_PTR(-EINVAL);
610}
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700611#endif
Vadim Kochan4b681c82015-01-12 16:34:05 +0200612EXPORT_SYMBOL_GPL(get_net_ns_by_fd);
Eric W. Biederman9dd776b2007-09-26 22:04:26 -0700613
Johannes Berg30ffee82009-07-10 09:51:35 +0000614struct net *get_net_ns_by_pid(pid_t pid)
615{
616 struct task_struct *tsk;
617 struct net *net;
618
619 /* Lookup the network namespace */
620 net = ERR_PTR(-ESRCH);
621 rcu_read_lock();
622 tsk = find_task_by_vpid(pid);
623 if (tsk) {
624 struct nsproxy *nsproxy;
Eric W. Biederman728dba32014-02-03 19:13:49 -0800625 task_lock(tsk);
626 nsproxy = tsk->nsproxy;
Johannes Berg30ffee82009-07-10 09:51:35 +0000627 if (nsproxy)
628 net = get_net(nsproxy->net_ns);
Eric W. Biederman728dba32014-02-03 19:13:49 -0800629 task_unlock(tsk);
Johannes Berg30ffee82009-07-10 09:51:35 +0000630 }
631 rcu_read_unlock();
632 return net;
633}
634EXPORT_SYMBOL_GPL(get_net_ns_by_pid);
635
Eric W. Biederman98f842e2011-06-15 10:21:48 -0700636static __net_init int net_ns_net_init(struct net *net)
637{
Al Viro33c42942014-11-01 02:32:53 -0400638#ifdef CONFIG_NET_NS
639 net->ns.ops = &netns_operations;
640#endif
Al Viro6344c432014-11-01 00:45:45 -0400641 return ns_alloc_inum(&net->ns);
Eric W. Biederman98f842e2011-06-15 10:21:48 -0700642}
643
644static __net_exit void net_ns_net_exit(struct net *net)
645{
Al Viro6344c432014-11-01 00:45:45 -0400646 ns_free_inum(&net->ns);
Eric W. Biederman98f842e2011-06-15 10:21:48 -0700647}
648
649static struct pernet_operations __net_initdata net_ns_ops = {
650 .init = net_ns_net_init,
651 .exit = net_ns_net_exit,
652};
653
stephen hemminger3ee52562016-08-31 15:17:49 -0700654static const struct nla_policy rtnl_net_policy[NETNSA_MAX + 1] = {
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100655 [NETNSA_NONE] = { .type = NLA_UNSPEC },
656 [NETNSA_NSID] = { .type = NLA_S32 },
657 [NETNSA_PID] = { .type = NLA_U32 },
658 [NETNSA_FD] = { .type = NLA_U32 },
659};
660
David Ahernc21ef3e2017-04-16 09:48:24 -0700661static int rtnl_net_newid(struct sk_buff *skb, struct nlmsghdr *nlh,
662 struct netlink_ext_ack *extack)
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100663{
664 struct net *net = sock_net(skb->sk);
665 struct nlattr *tb[NETNSA_MAX + 1];
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200666 struct nlattr *nla;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100667 struct net *peer;
668 int nsid, err;
669
670 err = nlmsg_parse(nlh, sizeof(struct rtgenmsg), tb, NETNSA_MAX,
David Ahernc21ef3e2017-04-16 09:48:24 -0700671 rtnl_net_policy, extack);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100672 if (err < 0)
673 return err;
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200674 if (!tb[NETNSA_NSID]) {
675 NL_SET_ERR_MSG(extack, "nsid is missing");
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100676 return -EINVAL;
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200677 }
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100678 nsid = nla_get_s32(tb[NETNSA_NSID]);
679
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200680 if (tb[NETNSA_PID]) {
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100681 peer = get_net_ns_by_pid(nla_get_u32(tb[NETNSA_PID]));
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200682 nla = tb[NETNSA_PID];
683 } else if (tb[NETNSA_FD]) {
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100684 peer = get_net_ns_by_fd(nla_get_u32(tb[NETNSA_FD]));
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200685 nla = tb[NETNSA_FD];
686 } else {
687 NL_SET_ERR_MSG(extack, "Peer netns reference is missing");
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100688 return -EINVAL;
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200689 }
690 if (IS_ERR(peer)) {
691 NL_SET_BAD_ATTR(extack, nla);
692 NL_SET_ERR_MSG(extack, "Peer netns reference is invalid");
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100693 return PTR_ERR(peer);
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200694 }
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100695
Paul Moorefba143c2016-11-29 16:57:48 -0500696 spin_lock_bh(&net->nsid_lock);
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200697 if (__peernet2id(net, peer) >= 0) {
Paul Moorefba143c2016-11-29 16:57:48 -0500698 spin_unlock_bh(&net->nsid_lock);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100699 err = -EEXIST;
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200700 NL_SET_BAD_ATTR(extack, nla);
701 NL_SET_ERR_MSG(extack,
702 "Peer netns already has a nsid assigned");
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100703 goto out;
704 }
705
706 err = alloc_netid(net, peer, nsid);
Paul Moorefba143c2016-11-29 16:57:48 -0500707 spin_unlock_bh(&net->nsid_lock);
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200708 if (err >= 0) {
709 rtnl_net_notifyid(net, RTM_NEWNSID, err);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100710 err = 0;
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200711 } else if (err == -ENOSPC && nsid >= 0) {
Nicolas Dichtel10d486a2017-06-09 14:41:57 +0200712 err = -EEXIST;
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200713 NL_SET_BAD_ATTR(extack, tb[NETNSA_NSID]);
714 NL_SET_ERR_MSG(extack, "The specified nsid is already used");
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200715 }
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100716out:
717 put_net(peer);
718 return err;
719}
720
721static int rtnl_net_get_size(void)
722{
723 return NLMSG_ALIGN(sizeof(struct rtgenmsg))
724 + nla_total_size(sizeof(s32)) /* NETNSA_NSID */
725 ;
726}
727
728static int rtnl_net_fill(struct sk_buff *skb, u32 portid, u32 seq, int flags,
Nicolas Dichtelcab3c8e2015-05-07 11:02:48 +0200729 int cmd, struct net *net, int nsid)
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100730{
731 struct nlmsghdr *nlh;
732 struct rtgenmsg *rth;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100733
734 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rth), flags);
735 if (!nlh)
736 return -EMSGSIZE;
737
738 rth = nlmsg_data(nlh);
739 rth->rtgen_family = AF_UNSPEC;
740
Nicolas Dichtelcab3c8e2015-05-07 11:02:48 +0200741 if (nla_put_s32(skb, NETNSA_NSID, nsid))
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100742 goto nla_put_failure;
743
744 nlmsg_end(skb, nlh);
745 return 0;
746
747nla_put_failure:
748 nlmsg_cancel(skb, nlh);
749 return -EMSGSIZE;
750}
751
David Ahernc21ef3e2017-04-16 09:48:24 -0700752static int rtnl_net_getid(struct sk_buff *skb, struct nlmsghdr *nlh,
753 struct netlink_ext_ack *extack)
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100754{
755 struct net *net = sock_net(skb->sk);
756 struct nlattr *tb[NETNSA_MAX + 1];
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200757 struct nlattr *nla;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100758 struct sk_buff *msg;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100759 struct net *peer;
Nicolas Dichtelcab3c8e2015-05-07 11:02:48 +0200760 int err, id;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100761
762 err = nlmsg_parse(nlh, sizeof(struct rtgenmsg), tb, NETNSA_MAX,
David Ahernc21ef3e2017-04-16 09:48:24 -0700763 rtnl_net_policy, extack);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100764 if (err < 0)
765 return err;
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200766 if (tb[NETNSA_PID]) {
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100767 peer = get_net_ns_by_pid(nla_get_u32(tb[NETNSA_PID]));
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200768 nla = tb[NETNSA_PID];
769 } else if (tb[NETNSA_FD]) {
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100770 peer = get_net_ns_by_fd(nla_get_u32(tb[NETNSA_FD]));
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200771 nla = tb[NETNSA_FD];
772 } else {
773 NL_SET_ERR_MSG(extack, "Peer netns reference is missing");
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100774 return -EINVAL;
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200775 }
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100776
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200777 if (IS_ERR(peer)) {
778 NL_SET_BAD_ATTR(extack, nla);
779 NL_SET_ERR_MSG(extack, "Peer netns reference is invalid");
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100780 return PTR_ERR(peer);
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200781 }
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100782
783 msg = nlmsg_new(rtnl_net_get_size(), GFP_KERNEL);
784 if (!msg) {
785 err = -ENOMEM;
786 goto out;
787 }
788
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200789 id = peernet2id(net, peer);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100790 err = rtnl_net_fill(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
David S. Millerb04096f2015-05-13 14:31:43 -0400791 RTM_NEWNSID, net, id);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100792 if (err < 0)
793 goto err_out;
794
795 err = rtnl_unicast(msg, net, NETLINK_CB(skb).portid);
796 goto out;
797
798err_out:
799 nlmsg_free(msg);
800out:
801 put_net(peer);
802 return err;
803}
804
Nicolas Dichtela143c402015-04-07 11:51:54 +0200805struct rtnl_net_dump_cb {
806 struct net *net;
807 struct sk_buff *skb;
808 struct netlink_callback *cb;
809 int idx;
810 int s_idx;
811};
812
813static int rtnl_net_dumpid_one(int id, void *peer, void *data)
814{
815 struct rtnl_net_dump_cb *net_cb = (struct rtnl_net_dump_cb *)data;
816 int ret;
817
818 if (net_cb->idx < net_cb->s_idx)
819 goto cont;
820
821 ret = rtnl_net_fill(net_cb->skb, NETLINK_CB(net_cb->cb->skb).portid,
822 net_cb->cb->nlh->nlmsg_seq, NLM_F_MULTI,
Nicolas Dichtelcab3c8e2015-05-07 11:02:48 +0200823 RTM_NEWNSID, net_cb->net, id);
Nicolas Dichtela143c402015-04-07 11:51:54 +0200824 if (ret < 0)
825 return ret;
826
827cont:
828 net_cb->idx++;
829 return 0;
830}
831
832static int rtnl_net_dumpid(struct sk_buff *skb, struct netlink_callback *cb)
833{
834 struct net *net = sock_net(skb->sk);
835 struct rtnl_net_dump_cb net_cb = {
836 .net = net,
837 .skb = skb,
838 .cb = cb,
839 .idx = 0,
840 .s_idx = cb->args[0],
841 };
842
Paul Moorefba143c2016-11-29 16:57:48 -0500843 spin_lock_bh(&net->nsid_lock);
Nicolas Dichtela143c402015-04-07 11:51:54 +0200844 idr_for_each(&net->netns_ids, rtnl_net_dumpid_one, &net_cb);
Paul Moorefba143c2016-11-29 16:57:48 -0500845 spin_unlock_bh(&net->nsid_lock);
Nicolas Dichtela143c402015-04-07 11:51:54 +0200846
847 cb->args[0] = net_cb.idx;
848 return skb->len;
849}
850
Nicolas Dichtelcab3c8e2015-05-07 11:02:48 +0200851static void rtnl_net_notifyid(struct net *net, int cmd, int id)
Nicolas Dichtel9a963452015-04-07 11:51:53 +0200852{
853 struct sk_buff *msg;
854 int err = -ENOMEM;
855
856 msg = nlmsg_new(rtnl_net_get_size(), GFP_KERNEL);
857 if (!msg)
858 goto out;
859
Nicolas Dichtelcab3c8e2015-05-07 11:02:48 +0200860 err = rtnl_net_fill(msg, 0, 0, 0, cmd, net, id);
Nicolas Dichtel9a963452015-04-07 11:51:53 +0200861 if (err < 0)
862 goto err_out;
863
864 rtnl_notify(msg, net, 0, RTNLGRP_NSID, NULL, 0);
865 return;
866
867err_out:
868 nlmsg_free(msg);
869out:
870 rtnl_set_sk_err(net, RTNLGRP_NSID, err);
871}
872
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200873static int __init net_ns_init(void)
874{
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800875 struct net_generic *ng;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200876
Pavel Emelyanovd57a9212007-11-01 00:46:50 -0700877#ifdef CONFIG_NET_NS
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200878 net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
879 SMP_CACHE_BYTES,
880 SLAB_PANIC, NULL);
Benjamin Thery3ef13552007-11-19 23:18:16 -0800881
882 /* Create workqueue for cleanup */
883 netns_wq = create_singlethread_workqueue("netns");
884 if (!netns_wq)
885 panic("Could not create netns workq");
Pavel Emelyanovd57a9212007-11-01 00:46:50 -0700886#endif
Benjamin Thery3ef13552007-11-19 23:18:16 -0800887
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800888 ng = net_alloc_generic();
889 if (!ng)
890 panic("Could not allocate generic netns");
891
892 rcu_assign_pointer(init_net.gen, ng);
893
Kirill Tkhai1a57feb2018-02-13 12:26:23 +0300894 down_write(&net_sem);
Eric W. Biederman038e7332012-06-14 02:31:10 -0700895 if (setup_net(&init_net, &init_user_ns))
Stephen Hemmingerca0f3112009-05-21 15:10:31 -0700896 panic("Could not setup the initial network namespace");
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200897
Dmitry Torokhovf8c46cb2016-08-10 14:36:00 -0700898 init_net_initialized = true;
Kirill Tkhai1a57feb2018-02-13 12:26:23 +0300899 up_write(&net_sem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200900
Eric W. Biederman98f842e2011-06-15 10:21:48 -0700901 register_pernet_subsys(&net_ns_ops);
902
Florian Westphal165b9112017-08-09 20:41:53 +0200903 rtnl_register(PF_UNSPEC, RTM_NEWNSID, rtnl_net_newid, NULL,
904 RTNL_FLAG_DOIT_UNLOCKED);
Nicolas Dichtela143c402015-04-07 11:51:54 +0200905 rtnl_register(PF_UNSPEC, RTM_GETNSID, rtnl_net_getid, rtnl_net_dumpid,
Florian Westphal165b9112017-08-09 20:41:53 +0200906 RTNL_FLAG_DOIT_UNLOCKED);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100907
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200908 return 0;
909}
910
911pure_initcall(net_ns_init);
912
Denis V. Luneved160e82007-11-13 03:23:21 -0800913#ifdef CONFIG_NET_NS
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000914static int __register_pernet_operations(struct list_head *list,
915 struct pernet_operations *ops)
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200916{
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000917 struct net *net;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200918 int error;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000919 LIST_HEAD(net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200920
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200921 list_add_tail(&ops->list, list);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000922 if (ops->init || (ops->id && ops->size)) {
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700923 for_each_net(net) {
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000924 error = ops_init(ops, net);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200925 if (error)
926 goto out_undo;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000927 list_add_tail(&net->exit_list, &net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200928 }
929 }
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700930 return 0;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200931
932out_undo:
933 /* If I have an error cleanup all namespaces I initialized */
934 list_del(&ops->list);
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000935 ops_exit_list(ops, &net_exit_list);
936 ops_free_list(ops, &net_exit_list);
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700937 return error;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200938}
939
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000940static void __unregister_pernet_operations(struct pernet_operations *ops)
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200941{
942 struct net *net;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000943 LIST_HEAD(net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200944
945 list_del(&ops->list);
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000946 for_each_net(net)
947 list_add_tail(&net->exit_list, &net_exit_list);
948 ops_exit_list(ops, &net_exit_list);
949 ops_free_list(ops, &net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200950}
951
Denis V. Luneved160e82007-11-13 03:23:21 -0800952#else
953
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000954static int __register_pernet_operations(struct list_head *list,
955 struct pernet_operations *ops)
956{
Dmitry Torokhovf8c46cb2016-08-10 14:36:00 -0700957 if (!init_net_initialized) {
958 list_add_tail(&ops->list, list);
959 return 0;
960 }
961
Julian Anastasovb9229342012-04-16 04:43:15 +0000962 return ops_init(ops, &init_net);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000963}
964
965static void __unregister_pernet_operations(struct pernet_operations *ops)
966{
Dmitry Torokhovf8c46cb2016-08-10 14:36:00 -0700967 if (!init_net_initialized) {
968 list_del(&ops->list);
969 } else {
970 LIST_HEAD(net_exit_list);
971 list_add(&init_net.exit_list, &net_exit_list);
972 ops_exit_list(ops, &net_exit_list);
973 ops_free_list(ops, &net_exit_list);
974 }
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000975}
976
977#endif /* CONFIG_NET_NS */
978
979static DEFINE_IDA(net_generic_ids);
980
Denis V. Luneved160e82007-11-13 03:23:21 -0800981static int register_pernet_operations(struct list_head *list,
982 struct pernet_operations *ops)
983{
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000984 int error;
985
986 if (ops->id) {
987again:
Alexey Dobriyan6af2d5f2016-12-02 04:21:32 +0300988 error = ida_get_new_above(&net_generic_ids, MIN_PERNET_OPS_ID, ops->id);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000989 if (error < 0) {
990 if (error == -EAGAIN) {
991 ida_pre_get(&net_generic_ids, GFP_KERNEL);
992 goto again;
993 }
994 return error;
995 }
Alexey Dobriyan6af2d5f2016-12-02 04:21:32 +0300996 max_gen_ptrs = max(max_gen_ptrs, *ops->id + 1);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000997 }
998 error = __register_pernet_operations(list, ops);
Eric W. Biederman3a765ed2009-12-03 02:29:06 +0000999 if (error) {
1000 rcu_barrier();
1001 if (ops->id)
1002 ida_remove(&net_generic_ids, *ops->id);
Kirill Tkhai447cd7a2018-02-13 12:26:44 +03001003 } else if (!ops->async) {
1004 pr_info_once("Pernet operations %ps are sync.\n", ops);
1005 nr_sync_pernet_ops++;
Eric W. Biederman3a765ed2009-12-03 02:29:06 +00001006 }
Eric W. Biedermanf875bae2009-11-29 22:25:28 +00001007
1008 return error;
Denis V. Luneved160e82007-11-13 03:23:21 -08001009}
1010
1011static void unregister_pernet_operations(struct pernet_operations *ops)
1012{
Kirill Tkhai447cd7a2018-02-13 12:26:44 +03001013 if (!ops->async)
1014 BUG_ON(nr_sync_pernet_ops-- == 0);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +00001015 __unregister_pernet_operations(ops);
Eric W. Biederman3a765ed2009-12-03 02:29:06 +00001016 rcu_barrier();
Eric W. Biedermanf875bae2009-11-29 22:25:28 +00001017 if (ops->id)
1018 ida_remove(&net_generic_ids, *ops->id);
Denis V. Luneved160e82007-11-13 03:23:21 -08001019}
Pavel Emelyanovc93cf612008-04-15 00:35:23 -07001020
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001021/**
1022 * register_pernet_subsys - register a network namespace subsystem
1023 * @ops: pernet operations structure for the subsystem
1024 *
1025 * Register a subsystem which has init and exit functions
1026 * that are called when network namespaces are created and
1027 * destroyed respectively.
1028 *
1029 * When registered all network namespace init functions are
1030 * called for every existing network namespace. Allowing kernel
1031 * modules to have a race free view of the set of network namespaces.
1032 *
1033 * When a new network namespace is created all of the init
1034 * methods are called in the order in which they were registered.
1035 *
1036 * When a network namespace is destroyed all of the exit methods
1037 * are called in the reverse of the order with which they were
1038 * registered.
1039 */
1040int register_pernet_subsys(struct pernet_operations *ops)
1041{
1042 int error;
Kirill Tkhai1a57feb2018-02-13 12:26:23 +03001043 down_write(&net_sem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001044 error = register_pernet_operations(first_device, ops);
Kirill Tkhai1a57feb2018-02-13 12:26:23 +03001045 up_write(&net_sem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001046 return error;
1047}
1048EXPORT_SYMBOL_GPL(register_pernet_subsys);
1049
1050/**
1051 * unregister_pernet_subsys - unregister a network namespace subsystem
1052 * @ops: pernet operations structure to manipulate
1053 *
1054 * Remove the pernet operations structure from the list to be
Oliver Pinter53379e52008-02-03 17:56:48 +02001055 * used when network namespaces are created or destroyed. In
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001056 * addition run the exit method for all existing network
1057 * namespaces.
1058 */
Jiri Pirkob3c981d2010-04-25 00:49:56 -07001059void unregister_pernet_subsys(struct pernet_operations *ops)
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001060{
Kirill Tkhai1a57feb2018-02-13 12:26:23 +03001061 down_write(&net_sem);
Jiri Pirkob3c981d2010-04-25 00:49:56 -07001062 unregister_pernet_operations(ops);
Kirill Tkhai1a57feb2018-02-13 12:26:23 +03001063 up_write(&net_sem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001064}
1065EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
1066
1067/**
1068 * register_pernet_device - register a network namespace device
1069 * @ops: pernet operations structure for the subsystem
1070 *
1071 * Register a device which has init and exit functions
1072 * that are called when network namespaces are created and
1073 * destroyed respectively.
1074 *
1075 * When registered all network namespace init functions are
1076 * called for every existing network namespace. Allowing kernel
1077 * modules to have a race free view of the set of network namespaces.
1078 *
1079 * When a new network namespace is created all of the init
1080 * methods are called in the order in which they were registered.
1081 *
1082 * When a network namespace is destroyed all of the exit methods
1083 * are called in the reverse of the order with which they were
1084 * registered.
1085 */
1086int register_pernet_device(struct pernet_operations *ops)
1087{
1088 int error;
Kirill Tkhai1a57feb2018-02-13 12:26:23 +03001089 down_write(&net_sem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001090 error = register_pernet_operations(&pernet_list, ops);
1091 if (!error && (first_device == &pernet_list))
1092 first_device = &ops->list;
Kirill Tkhai1a57feb2018-02-13 12:26:23 +03001093 up_write(&net_sem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001094 return error;
1095}
1096EXPORT_SYMBOL_GPL(register_pernet_device);
1097
1098/**
1099 * unregister_pernet_device - unregister a network namespace netdevice
1100 * @ops: pernet operations structure to manipulate
1101 *
1102 * Remove the pernet operations structure from the list to be
Oliver Pinter53379e52008-02-03 17:56:48 +02001103 * used when network namespaces are created or destroyed. In
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001104 * addition run the exit method for all existing network
1105 * namespaces.
1106 */
1107void unregister_pernet_device(struct pernet_operations *ops)
1108{
Kirill Tkhai1a57feb2018-02-13 12:26:23 +03001109 down_write(&net_sem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001110 if (&ops->list == first_device)
1111 first_device = first_device->next;
1112 unregister_pernet_operations(ops);
Kirill Tkhai1a57feb2018-02-13 12:26:23 +03001113 up_write(&net_sem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001114}
1115EXPORT_SYMBOL_GPL(unregister_pernet_device);
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001116
1117#ifdef CONFIG_NET_NS
Al Viro64964522014-11-01 00:37:32 -04001118static struct ns_common *netns_get(struct task_struct *task)
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001119{
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001120 struct net *net = NULL;
1121 struct nsproxy *nsproxy;
1122
Eric W. Biederman728dba32014-02-03 19:13:49 -08001123 task_lock(task);
1124 nsproxy = task->nsproxy;
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001125 if (nsproxy)
1126 net = get_net(nsproxy->net_ns);
Eric W. Biederman728dba32014-02-03 19:13:49 -08001127 task_unlock(task);
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001128
Al Viroff248702014-11-01 00:10:50 -04001129 return net ? &net->ns : NULL;
1130}
1131
1132static inline struct net *to_net_ns(struct ns_common *ns)
1133{
1134 return container_of(ns, struct net, ns);
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001135}
1136
Al Viro64964522014-11-01 00:37:32 -04001137static void netns_put(struct ns_common *ns)
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001138{
Al Viroff248702014-11-01 00:10:50 -04001139 put_net(to_net_ns(ns));
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001140}
1141
Al Viro64964522014-11-01 00:37:32 -04001142static int netns_install(struct nsproxy *nsproxy, struct ns_common *ns)
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001143{
Al Viroff248702014-11-01 00:10:50 -04001144 struct net *net = to_net_ns(ns);
Eric W. Biederman142e1d12012-07-26 01:13:20 -07001145
Eric W. Biederman5e4a0842012-12-14 07:55:36 -08001146 if (!ns_capable(net->user_ns, CAP_SYS_ADMIN) ||
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -07001147 !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
Eric W. Biederman142e1d12012-07-26 01:13:20 -07001148 return -EPERM;
1149
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001150 put_net(nsproxy->net_ns);
Eric W. Biederman142e1d12012-07-26 01:13:20 -07001151 nsproxy->net_ns = get_net(net);
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001152 return 0;
1153}
1154
Andrey Vaginbcac25a2016-09-06 00:47:13 -07001155static struct user_namespace *netns_owner(struct ns_common *ns)
1156{
1157 return to_net_ns(ns)->user_ns;
1158}
1159
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001160const struct proc_ns_operations netns_operations = {
1161 .name = "net",
1162 .type = CLONE_NEWNET,
1163 .get = netns_get,
1164 .put = netns_put,
1165 .install = netns_install,
Andrey Vaginbcac25a2016-09-06 00:47:13 -07001166 .owner = netns_owner,
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001167};
1168#endif