blob: 690e78c6af4576e147a0a1b7bb9a24592d46f729 [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;
Eric W. Biederman5f256be2007-09-12 11:50:50 +020032
Eric W. Biederman5f256be2007-09-12 11:50:50 +020033LIST_HEAD(net_namespace_list);
Alexey Dobriyanb76a4612008-10-08 11:35:06 +020034EXPORT_SYMBOL_GPL(net_namespace_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +020035
Rustad, Mark D734b6542012-07-18 09:06:07 +000036struct net init_net = {
Kirill Tkhai273c28b2018-01-12 18:28:31 +030037 .count = REFCOUNT_INIT(1),
David Howellsb5082df2017-04-27 22:40:23 +010038 .dev_base_head = LIST_HEAD_INIT(init_net.dev_base_head),
Rustad, Mark D734b6542012-07-18 09:06:07 +000039};
Denis V. Lunevff4b9502008-01-22 22:05:33 -080040EXPORT_SYMBOL(init_net);
Eric W. Biederman5f256be2007-09-12 11:50:50 +020041
Dmitry Torokhovf8c46cb2016-08-10 14:36:00 -070042static bool init_net_initialized;
Kirill Tkhai447cd7a2018-02-13 12:26:44 +030043static unsigned nr_sync_pernet_ops;
Kirill Tkhai1a57feb2018-02-13 12:26:23 +030044/*
Kirill Tkhai447cd7a2018-02-13 12:26:44 +030045 * net_sem: protects: pernet_list, net_generic_ids, nr_sync_pernet_ops,
Kirill Tkhai1a57feb2018-02-13 12:26:23 +030046 * init_net_initialized and first_device pointer.
47 */
48DECLARE_RWSEM(net_sem);
Dmitry Torokhovf8c46cb2016-08-10 14:36:00 -070049
Alexey Dobriyan6af2d5f2016-12-02 04:21:32 +030050#define MIN_PERNET_OPS_ID \
51 ((sizeof(struct net_generic) + sizeof(void *) - 1) / sizeof(void *))
52
Pavel Emelyanovdec827d2008-04-15 00:36:08 -070053#define INITIAL_NET_GEN_PTRS 13 /* +1 for len +2 for rcu_head */
54
Eric Dumazet073862b2012-01-26 00:41:38 +000055static unsigned int max_gen_ptrs = INITIAL_NET_GEN_PTRS;
56
57static struct net_generic *net_alloc_generic(void)
58{
59 struct net_generic *ng;
Alexey Dobriyan6af2d5f2016-12-02 04:21:32 +030060 unsigned int generic_size = offsetof(struct net_generic, ptr[max_gen_ptrs]);
Eric Dumazet073862b2012-01-26 00:41:38 +000061
62 ng = kzalloc(generic_size, GFP_KERNEL);
63 if (ng)
Alexey Dobriyan9bfc7b92016-12-02 04:12:58 +030064 ng->s.len = max_gen_ptrs;
Eric Dumazet073862b2012-01-26 00:41:38 +000065
66 return ng;
67}
68
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030069static int net_assign_generic(struct net *net, unsigned int id, void *data)
Jiri Pirko05fceb42010-04-23 01:40:47 +000070{
71 struct net_generic *ng, *old_ng;
72
Alexey Dobriyan6af2d5f2016-12-02 04:21:32 +030073 BUG_ON(id < MIN_PERNET_OPS_ID);
Jiri Pirko05fceb42010-04-23 01:40:47 +000074
Eric Dumazet1c877332010-10-25 03:20:11 +000075 old_ng = rcu_dereference_protected(net->gen,
Kirill Tkhai447cd7a2018-02-13 12:26:44 +030076 lockdep_is_held(&net_sem));
Alexey Dobriyan6af2d5f2016-12-02 04:21:32 +030077 if (old_ng->s.len > id) {
78 old_ng->ptr[id] = data;
Alexey Dobriyan1a9a05922016-12-02 04:11:34 +030079 return 0;
80 }
Jiri Pirko05fceb42010-04-23 01:40:47 +000081
Eric Dumazet073862b2012-01-26 00:41:38 +000082 ng = net_alloc_generic();
Jiri Pirko05fceb42010-04-23 01:40:47 +000083 if (ng == NULL)
84 return -ENOMEM;
85
86 /*
87 * Some synchronisation notes:
88 *
89 * The net_generic explores the net->gen array inside rcu
90 * read section. Besides once set the net->gen->ptr[x]
91 * pointer never changes (see rules in netns/generic.h).
92 *
93 * That said, we simply duplicate this array and schedule
94 * the old copy for kfree after a grace period.
95 */
96
Alexey Dobriyan6af2d5f2016-12-02 04:21:32 +030097 memcpy(&ng->ptr[MIN_PERNET_OPS_ID], &old_ng->ptr[MIN_PERNET_OPS_ID],
98 (old_ng->s.len - MIN_PERNET_OPS_ID) * sizeof(void *));
99 ng->ptr[id] = data;
Jiri Pirko05fceb42010-04-23 01:40:47 +0000100
101 rcu_assign_pointer(net->gen, ng);
Alexey Dobriyan9bfc7b92016-12-02 04:12:58 +0300102 kfree_rcu(old_ng, s.rcu);
Jiri Pirko05fceb42010-04-23 01:40:47 +0000103 return 0;
104}
105
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000106static int ops_init(const struct pernet_operations *ops, struct net *net)
107{
Julian Anastasovb9229342012-04-16 04:43:15 +0000108 int err = -ENOMEM;
109 void *data = NULL;
110
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000111 if (ops->id && ops->size) {
Julian Anastasovb9229342012-04-16 04:43:15 +0000112 data = kzalloc(ops->size, GFP_KERNEL);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000113 if (!data)
Julian Anastasovb9229342012-04-16 04:43:15 +0000114 goto out;
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000115
116 err = net_assign_generic(net, *ops->id, data);
Julian Anastasovb9229342012-04-16 04:43:15 +0000117 if (err)
118 goto cleanup;
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000119 }
Julian Anastasovb9229342012-04-16 04:43:15 +0000120 err = 0;
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000121 if (ops->init)
Julian Anastasovb9229342012-04-16 04:43:15 +0000122 err = ops->init(net);
123 if (!err)
124 return 0;
125
126cleanup:
127 kfree(data);
128
129out:
130 return err;
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000131}
132
133static void ops_free(const struct pernet_operations *ops, struct net *net)
134{
135 if (ops->id && ops->size) {
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +0300136 kfree(net_generic(net, *ops->id));
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000137 }
138}
139
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000140static void ops_exit_list(const struct pernet_operations *ops,
141 struct list_head *net_exit_list)
142{
143 struct net *net;
144 if (ops->exit) {
145 list_for_each_entry(net, net_exit_list, exit_list)
146 ops->exit(net);
147 }
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000148 if (ops->exit_batch)
149 ops->exit_batch(net_exit_list);
150}
151
152static void ops_free_list(const struct pernet_operations *ops,
153 struct list_head *net_exit_list)
154{
155 struct net *net;
156 if (ops->size && ops->id) {
157 list_for_each_entry(net, net_exit_list, exit_list)
158 ops_free(ops, net);
159 }
160}
161
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200162/* should be called with nsid_lock held */
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100163static int alloc_netid(struct net *net, struct net *peer, int reqid)
164{
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200165 int min = 0, max = 0;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100166
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100167 if (reqid >= 0) {
168 min = reqid;
169 max = reqid + 1;
170 }
171
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200172 return idr_alloc(&net->netns_ids, peer, min, max, GFP_ATOMIC);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100173}
174
175/* This function is used by idr_for_each(). If net is equal to peer, the
176 * function returns the id so that idr_for_each() stops. Because we cannot
177 * returns the id 0 (idr_for_each() will not stop), we return the magic value
178 * NET_ID_ZERO (-1) for it.
179 */
180#define NET_ID_ZERO -1
181static int net_eq_idr(int id, void *net, void *peer)
182{
183 if (net_eq(net, peer))
184 return id ? : NET_ID_ZERO;
185 return 0;
186}
187
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200188/* Should be called with nsid_lock held. If a new id is assigned, the bool alloc
189 * is set to true, thus the caller knows that the new id must be notified via
190 * rtnl.
191 */
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200192static int __peernet2id_alloc(struct net *net, struct net *peer, bool *alloc)
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100193{
194 int id = idr_for_each(&net->netns_ids, net_eq_idr, peer);
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200195 bool alloc_it = *alloc;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100196
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200197 *alloc = false;
198
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100199 /* Magic value for id 0. */
200 if (id == NET_ID_ZERO)
201 return 0;
202 if (id > 0)
203 return id;
204
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200205 if (alloc_it) {
Nicolas Dichtel109582af2015-05-07 11:02:47 +0200206 id = alloc_netid(net, peer, -1);
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200207 *alloc = true;
Nicolas Dichtel109582af2015-05-07 11:02:47 +0200208 return id >= 0 ? id : NETNSA_NSID_NOT_ASSIGNED;
209 }
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100210
Nicolas Dichtel109582af2015-05-07 11:02:47 +0200211 return NETNSA_NSID_NOT_ASSIGNED;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100212}
213
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200214/* should be called with nsid_lock held */
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200215static int __peernet2id(struct net *net, struct net *peer)
216{
217 bool no = false;
218
219 return __peernet2id_alloc(net, peer, &no);
220}
221
222static void rtnl_net_notifyid(struct net *net, int cmd, int id);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100223/* This function returns the id of a peer netns. If no id is assigned, one will
224 * be allocated and returned.
225 */
Nicolas Dichtel7a0877d2015-05-07 11:02:49 +0200226int peernet2id_alloc(struct net *net, struct net *peer)
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100227{
Kirill Tkhai0c06bea2018-01-16 12:31:41 +0300228 bool alloc = false, alive = false;
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200229 int id;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100230
Kirill Tkhai273c28b2018-01-12 18:28:31 +0300231 if (refcount_read(&net->count) == 0)
WANG Congcfc44a42016-11-16 10:27:02 -0800232 return NETNSA_NSID_NOT_ASSIGNED;
Paul Moorefba143c2016-11-29 16:57:48 -0500233 spin_lock_bh(&net->nsid_lock);
Kirill Tkhai0c06bea2018-01-16 12:31:41 +0300234 /*
235 * When peer is obtained from RCU lists, we may race with
236 * its cleanup. Check whether it's alive, and this guarantees
237 * we never hash a peer back to net->netns_ids, after it has
238 * just been idr_remove()'d from there in cleanup_net().
239 */
240 if (maybe_get_net(peer))
241 alive = alloc = true;
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200242 id = __peernet2id_alloc(net, peer, &alloc);
Paul Moorefba143c2016-11-29 16:57:48 -0500243 spin_unlock_bh(&net->nsid_lock);
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200244 if (alloc && id >= 0)
245 rtnl_net_notifyid(net, RTM_NEWNSID, id);
Kirill Tkhai0c06bea2018-01-16 12:31:41 +0300246 if (alive)
247 put_net(peer);
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200248 return id;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100249}
Jiri Benc7cbebc82017-11-02 17:04:36 -0200250EXPORT_SYMBOL_GPL(peernet2id_alloc);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100251
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200252/* This function returns, if assigned, the id of a peer netns. */
Nicolas Dichtel59324cf2015-05-07 11:02:53 +0200253int peernet2id(struct net *net, struct net *peer)
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200254{
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200255 int id;
256
Paul Moorefba143c2016-11-29 16:57:48 -0500257 spin_lock_bh(&net->nsid_lock);
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200258 id = __peernet2id(net, peer);
Paul Moorefba143c2016-11-29 16:57:48 -0500259 spin_unlock_bh(&net->nsid_lock);
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200260 return id;
261}
WANG Cong38f507f2016-09-01 21:53:44 -0700262EXPORT_SYMBOL(peernet2id);
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200263
Nicolas Dichtel59324cf2015-05-07 11:02:53 +0200264/* This function returns true is the peer netns has an id assigned into the
265 * current netns.
266 */
267bool peernet_has_id(struct net *net, struct net *peer)
268{
269 return peernet2id(net, peer) >= 0;
270}
271
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100272struct net *get_net_ns_by_id(struct net *net, int id)
273{
274 struct net *peer;
275
276 if (id < 0)
277 return NULL;
278
279 rcu_read_lock();
280 peer = idr_find(&net->netns_ids, id);
281 if (peer)
Eric W. Biederman21b59442017-12-19 11:27:56 -0600282 peer = maybe_get_net(peer);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100283 rcu_read_unlock();
284
285 return peer;
286}
287
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700288/*
289 * setup_net runs the initializers for the network namespace object.
290 */
Eric W. Biederman038e7332012-06-14 02:31:10 -0700291static __net_init int setup_net(struct net *net, struct user_namespace *user_ns)
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700292{
Kirill Tkhai1a57feb2018-02-13 12:26:23 +0300293 /* Must be called with net_sem held */
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000294 const struct pernet_operations *ops, *saved_ops;
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800295 int error = 0;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000296 LIST_HEAD(net_exit_list);
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700297
Kirill Tkhai273c28b2018-01-12 18:28:31 +0300298 refcount_set(&net->count, 1);
Reshetova, Elenac122e142017-06-30 13:08:08 +0300299 refcount_set(&net->passive, 1);
Thomas Graf4e985ad2011-06-21 03:11:20 +0000300 net->dev_base_seq = 1;
Eric W. Biederman038e7332012-06-14 02:31:10 -0700301 net->user_ns = user_ns;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100302 idr_init(&net->netns_ids);
WANG Congde133462015-05-15 14:47:32 -0700303 spin_lock_init(&net->nsid_lock);
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800304
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700305 list_for_each_entry(ops, &pernet_list, list) {
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000306 error = ops_init(ops, net);
307 if (error < 0)
308 goto out_undo;
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700309 }
Kirill Tkhai98f6c532018-02-13 12:26:02 +0300310 rtnl_lock();
311 list_add_tail_rcu(&net->list, &net_namespace_list);
312 rtnl_unlock();
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700313out:
314 return error;
315
316out_undo:
317 /* Walk through the list backwards calling the exit functions
318 * for the pernet modules whose init functions did not fail.
319 */
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000320 list_add(&net->exit_list, &net_exit_list);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000321 saved_ops = ops;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000322 list_for_each_entry_continue_reverse(ops, &pernet_list, list)
323 ops_exit_list(ops, &net_exit_list);
324
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000325 ops = saved_ops;
326 list_for_each_entry_continue_reverse(ops, &pernet_list, list)
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000327 ops_free_list(ops, &net_exit_list);
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700328
329 rcu_barrier();
330 goto out;
331}
332
Roman Kapl7c3f18752017-05-24 10:22:22 +0200333static int __net_init net_defaults_init_net(struct net *net)
334{
335 net->core.sysctl_somaxconn = SOMAXCONN;
336 return 0;
337}
338
339static struct pernet_operations net_defaults_ops = {
340 .init = net_defaults_init_net,
Kirill Tkhaiff291d02018-02-13 12:27:51 +0300341 .async = true,
Roman Kapl7c3f18752017-05-24 10:22:22 +0200342};
343
344static __init int net_defaults_init(void)
345{
346 if (register_pernet_subsys(&net_defaults_ops))
347 panic("Cannot initialize net default settings");
348
349 return 0;
350}
351
352core_initcall(net_defaults_init);
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800353
Clemens Nossebe47d42009-02-23 15:37:35 -0800354#ifdef CONFIG_NET_NS
Arnd Bergmann2ed6afd2016-09-23 18:06:12 +0200355static struct ucounts *inc_net_namespaces(struct user_namespace *ns)
356{
357 return inc_ucount(ns, current_euid(), UCOUNT_NET_NAMESPACES);
358}
359
360static void dec_net_namespaces(struct ucounts *ucounts)
361{
362 dec_ucount(ucounts, UCOUNT_NET_NAMESPACES);
363}
364
Alexey Dobriyan08009a72018-02-24 21:20:33 +0300365static struct kmem_cache *net_cachep __ro_after_init;
Clemens Nossebe47d42009-02-23 15:37:35 -0800366static struct workqueue_struct *netns_wq;
367
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200368static struct net *net_alloc(void)
369{
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800370 struct net *net = NULL;
371 struct net_generic *ng;
372
373 ng = net_alloc_generic();
374 if (!ng)
375 goto out;
376
377 net = kmem_cache_zalloc(net_cachep, GFP_KERNEL);
378 if (!net)
379 goto out_free;
380
381 rcu_assign_pointer(net->gen, ng);
382out:
383 return net;
384
385out_free:
386 kfree(ng);
387 goto out;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200388}
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200389
Johann Felix Soden45a19b02007-11-07 01:30:30 -0800390static void net_free(struct net *net)
391{
Eric Dumazet416c51e2014-09-09 08:24:53 -0700392 kfree(rcu_access_pointer(net->gen));
Johann Felix Soden45a19b02007-11-07 01:30:30 -0800393 kmem_cache_free(net_cachep, net);
394}
395
Al Viroa685e082011-06-08 21:13:01 -0400396void net_drop_ns(void *p)
397{
398 struct net *ns = p;
Reshetova, Elenac122e142017-06-30 13:08:08 +0300399 if (ns && refcount_dec_and_test(&ns->passive))
Al Viroa685e082011-06-08 21:13:01 -0400400 net_free(ns);
401}
402
Eric W. Biederman038e7332012-06-14 02:31:10 -0700403struct net *copy_net_ns(unsigned long flags,
404 struct user_namespace *user_ns, struct net *old_net)
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700405{
Eric W. Biederman70328662016-08-08 14:33:23 -0500406 struct ucounts *ucounts;
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700407 struct net *net;
Kirill Tkhai19efbd92018-02-19 12:58:38 +0300408 unsigned write;
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700409 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);
Kirill Tkhai19efbd92018-02-19 12:58:38 +0300426again:
427 write = READ_ONCE(nr_sync_pernet_ops);
428 if (write)
429 rv = down_write_killable(&net_sem);
430 else
431 rv = down_read_killable(&net_sem);
Kirill Tkhai5ba049a2018-02-13 12:26:13 +0300432 if (rv < 0)
433 goto put_userns;
Kirill Tkhai19efbd92018-02-19 12:58:38 +0300434
435 if (!write && unlikely(READ_ONCE(nr_sync_pernet_ops))) {
436 up_read(&net_sem);
437 goto again;
Kirill Tkhai447cd7a2018-02-13 12:26:44 +0300438 }
Eric W. Biederman038e7332012-06-14 02:31:10 -0700439 rv = setup_net(net, user_ns);
Kirill Tkhai19efbd92018-02-19 12:58:38 +0300440
441 if (write)
442 up_write(&net_sem);
443 else
444 up_read(&net_sem);
445
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700446 if (rv < 0) {
Kirill Tkhai5ba049a2018-02-13 12:26:13 +0300447put_userns:
Eric W. Biederman038e7332012-06-14 02:31:10 -0700448 put_user_ns(user_ns);
Al Viroa685e082011-06-08 21:13:01 -0400449 net_drop_ns(net);
Kirill Tkhai5ba049a2018-02-13 12:26:13 +0300450dec_ucounts:
451 dec_net_namespaces(ucounts);
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700452 return ERR_PTR(rv);
453 }
454 return net;
455}
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800456
Kirill Tkhaifb07a822018-01-19 19:14:53 +0300457static void unhash_nsid(struct net *net, struct net *last)
458{
459 struct net *tmp;
460 /* This function is only called from cleanup_net() work,
461 * and this work is the only process, that may delete
462 * a net from net_namespace_list. So, when the below
463 * is executing, the list may only grow. Thus, we do not
464 * use for_each_net_rcu() or rtnl_lock().
465 */
466 for_each_net(tmp) {
467 int id;
468
469 spin_lock_bh(&tmp->nsid_lock);
470 id = __peernet2id(tmp, net);
471 if (id >= 0)
472 idr_remove(&tmp->netns_ids, id);
473 spin_unlock_bh(&tmp->nsid_lock);
474 if (id >= 0)
475 rtnl_net_notifyid(tmp, RTM_DELNSID, id);
476 if (tmp == last)
477 break;
478 }
479 spin_lock_bh(&net->nsid_lock);
480 idr_destroy(&net->netns_ids);
481 spin_unlock_bh(&net->nsid_lock);
482}
483
Kirill Tkhai65b7b5b2018-02-19 12:58:45 +0300484static LLIST_HEAD(cleanup_list);
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000485
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200486static void cleanup_net(struct work_struct *work)
487{
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000488 const struct pernet_operations *ops;
Kirill Tkhaifb07a822018-01-19 19:14:53 +0300489 struct net *net, *tmp, *last;
Kirill Tkhai65b7b5b2018-02-19 12:58:45 +0300490 struct llist_node *net_kill_list;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000491 LIST_HEAD(net_exit_list);
Kirill Tkhai19efbd92018-02-19 12:58:38 +0300492 unsigned write;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200493
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000494 /* Atomically snapshot the list of namespaces to cleanup */
Kirill Tkhai65b7b5b2018-02-19 12:58:45 +0300495 net_kill_list = llist_del_all(&cleanup_list);
Kirill Tkhai19efbd92018-02-19 12:58:38 +0300496again:
497 write = READ_ONCE(nr_sync_pernet_ops);
498 if (write)
499 down_write(&net_sem);
500 else
501 down_read(&net_sem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200502
Kirill Tkhai19efbd92018-02-19 12:58:38 +0300503 if (!write && unlikely(READ_ONCE(nr_sync_pernet_ops))) {
504 up_read(&net_sem);
505 goto again;
506 }
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200507
508 /* Don't let anyone else find us. */
Eric W. Biedermanf4618d32007-09-26 22:40:08 -0700509 rtnl_lock();
Kirill Tkhai65b7b5b2018-02-19 12:58:45 +0300510 llist_for_each_entry(net, net_kill_list, cleanup_list)
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000511 list_del_rcu(&net->list);
Kirill Tkhaifb07a822018-01-19 19:14:53 +0300512 /* Cache last net. After we unlock rtnl, no one new net
513 * added to net_namespace_list can assign nsid pointer
514 * to a net from net_kill_list (see peernet2id_alloc()).
515 * So, we skip them in unhash_nsid().
516 *
517 * Note, that unhash_nsid() does not delete nsid links
518 * between net_kill_list's nets, as they've already
519 * deleted from net_namespace_list. But, this would be
520 * useless anyway, as netns_ids are destroyed there.
521 */
522 last = list_last_entry(&net_namespace_list, struct net, list);
Eric W. Biedermanf4618d32007-09-26 22:40:08 -0700523 rtnl_unlock();
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200524
Kirill Tkhai65b7b5b2018-02-19 12:58:45 +0300525 llist_for_each_entry(net, net_kill_list, cleanup_list) {
Kirill Tkhaifb07a822018-01-19 19:14:53 +0300526 unhash_nsid(net, last);
527 list_add_tail(&net->exit_list, &net_exit_list);
528 }
529
Johannes Berg11a28d32009-07-10 09:51:33 +0000530 /*
531 * Another CPU might be rcu-iterating the list, wait for it.
532 * This needs to be before calling the exit() notifiers, so
533 * the rcu_barrier() below isn't sufficient alone.
534 */
535 synchronize_rcu();
536
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200537 /* Run all of the network namespace exit methods */
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000538 list_for_each_entry_reverse(ops, &pernet_list, list)
539 ops_exit_list(ops, &net_exit_list);
540
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000541 /* Free the net generic variables */
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000542 list_for_each_entry_reverse(ops, &pernet_list, list)
543 ops_free_list(ops, &net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200544
Kirill Tkhai19efbd92018-02-19 12:58:38 +0300545 if (write)
546 up_write(&net_sem);
547 else
548 up_read(&net_sem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200549
550 /* Ensure there are no outstanding rcu callbacks using this
551 * network namespace.
552 */
553 rcu_barrier();
554
555 /* Finally it is safe to free my network namespace structure */
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000556 list_for_each_entry_safe(net, tmp, &net_exit_list, exit_list) {
557 list_del_init(&net->exit_list);
Eric W. Biederman70328662016-08-08 14:33:23 -0500558 dec_net_namespaces(net->ucounts);
Eric W. Biederman038e7332012-06-14 02:31:10 -0700559 put_user_ns(net->user_ns);
Al Viroa685e082011-06-08 21:13:01 -0400560 net_drop_ns(net);
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000561 }
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200562}
Florian Westphal7866cc52017-05-30 11:38:12 +0200563
564/**
565 * net_ns_barrier - wait until concurrent net_cleanup_work is done
566 *
567 * cleanup_net runs from work queue and will first remove namespaces
568 * from the global list, then run net exit functions.
569 *
570 * Call this in module exit path to make sure that all netns
571 * ->exit ops have been invoked before the function is removed.
572 */
573void net_ns_barrier(void)
574{
Kirill Tkhai1a57feb2018-02-13 12:26:23 +0300575 down_write(&net_sem);
Kirill Tkhai1a57feb2018-02-13 12:26:23 +0300576 up_write(&net_sem);
Florian Westphal7866cc52017-05-30 11:38:12 +0200577}
578EXPORT_SYMBOL(net_ns_barrier);
579
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000580static DECLARE_WORK(net_cleanup_work, cleanup_net);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200581
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200582void __put_net(struct net *net)
583{
584 /* Cleanup the network namespace in process context */
Kirill Tkhai8349efd2018-02-19 12:58:54 +0300585 if (llist_add(&net->cleanup_list, &cleanup_list))
586 queue_work(netns_wq, &net_cleanup_work);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200587}
588EXPORT_SYMBOL_GPL(__put_net);
589
Stephen Rothwell956c9202011-05-12 13:51:13 +1000590struct net *get_net_ns_by_fd(int fd)
591{
Stephen Rothwell956c9202011-05-12 13:51:13 +1000592 struct file *file;
Al Viro33c42942014-11-01 02:32:53 -0400593 struct ns_common *ns;
Stephen Rothwell956c9202011-05-12 13:51:13 +1000594 struct net *net;
595
Stephen Rothwell956c9202011-05-12 13:51:13 +1000596 file = proc_ns_fget(fd);
Al Viroc316e6a2011-06-05 00:37:35 +0000597 if (IS_ERR(file))
598 return ERR_CAST(file);
Stephen Rothwell956c9202011-05-12 13:51:13 +1000599
Al Virof77c8012014-11-01 03:13:17 -0400600 ns = get_proc_ns(file_inode(file));
Al Viro33c42942014-11-01 02:32:53 -0400601 if (ns->ops == &netns_operations)
602 net = get_net(container_of(ns, struct net, ns));
Al Viroc316e6a2011-06-05 00:37:35 +0000603 else
604 net = ERR_PTR(-EINVAL);
Stephen Rothwell956c9202011-05-12 13:51:13 +1000605
Al Viroc316e6a2011-06-05 00:37:35 +0000606 fput(file);
Stephen Rothwell956c9202011-05-12 13:51:13 +1000607 return net;
608}
609
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700610#else
Stephen Rothwell956c9202011-05-12 13:51:13 +1000611struct net *get_net_ns_by_fd(int fd)
612{
613 return ERR_PTR(-EINVAL);
614}
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700615#endif
Vadim Kochan4b681c82015-01-12 16:34:05 +0200616EXPORT_SYMBOL_GPL(get_net_ns_by_fd);
Eric W. Biederman9dd776b2007-09-26 22:04:26 -0700617
Johannes Berg30ffee82009-07-10 09:51:35 +0000618struct net *get_net_ns_by_pid(pid_t pid)
619{
620 struct task_struct *tsk;
621 struct net *net;
622
623 /* Lookup the network namespace */
624 net = ERR_PTR(-ESRCH);
625 rcu_read_lock();
626 tsk = find_task_by_vpid(pid);
627 if (tsk) {
628 struct nsproxy *nsproxy;
Eric W. Biederman728dba32014-02-03 19:13:49 -0800629 task_lock(tsk);
630 nsproxy = tsk->nsproxy;
Johannes Berg30ffee82009-07-10 09:51:35 +0000631 if (nsproxy)
632 net = get_net(nsproxy->net_ns);
Eric W. Biederman728dba32014-02-03 19:13:49 -0800633 task_unlock(tsk);
Johannes Berg30ffee82009-07-10 09:51:35 +0000634 }
635 rcu_read_unlock();
636 return net;
637}
638EXPORT_SYMBOL_GPL(get_net_ns_by_pid);
639
Eric W. Biederman98f842e2011-06-15 10:21:48 -0700640static __net_init int net_ns_net_init(struct net *net)
641{
Al Viro33c42942014-11-01 02:32:53 -0400642#ifdef CONFIG_NET_NS
643 net->ns.ops = &netns_operations;
644#endif
Al Viro6344c432014-11-01 00:45:45 -0400645 return ns_alloc_inum(&net->ns);
Eric W. Biederman98f842e2011-06-15 10:21:48 -0700646}
647
648static __net_exit void net_ns_net_exit(struct net *net)
649{
Al Viro6344c432014-11-01 00:45:45 -0400650 ns_free_inum(&net->ns);
Eric W. Biederman98f842e2011-06-15 10:21:48 -0700651}
652
653static struct pernet_operations __net_initdata net_ns_ops = {
654 .init = net_ns_net_init,
655 .exit = net_ns_net_exit,
Kirill Tkhai3fc3b822018-02-13 12:27:03 +0300656 .async = true,
Eric W. Biederman98f842e2011-06-15 10:21:48 -0700657};
658
stephen hemminger3ee52562016-08-31 15:17:49 -0700659static const struct nla_policy rtnl_net_policy[NETNSA_MAX + 1] = {
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100660 [NETNSA_NONE] = { .type = NLA_UNSPEC },
661 [NETNSA_NSID] = { .type = NLA_S32 },
662 [NETNSA_PID] = { .type = NLA_U32 },
663 [NETNSA_FD] = { .type = NLA_U32 },
664};
665
David Ahernc21ef3e2017-04-16 09:48:24 -0700666static int rtnl_net_newid(struct sk_buff *skb, struct nlmsghdr *nlh,
667 struct netlink_ext_ack *extack)
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100668{
669 struct net *net = sock_net(skb->sk);
670 struct nlattr *tb[NETNSA_MAX + 1];
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200671 struct nlattr *nla;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100672 struct net *peer;
673 int nsid, err;
674
675 err = nlmsg_parse(nlh, sizeof(struct rtgenmsg), tb, NETNSA_MAX,
David Ahernc21ef3e2017-04-16 09:48:24 -0700676 rtnl_net_policy, extack);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100677 if (err < 0)
678 return err;
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200679 if (!tb[NETNSA_NSID]) {
680 NL_SET_ERR_MSG(extack, "nsid is missing");
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100681 return -EINVAL;
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200682 }
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100683 nsid = nla_get_s32(tb[NETNSA_NSID]);
684
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200685 if (tb[NETNSA_PID]) {
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100686 peer = get_net_ns_by_pid(nla_get_u32(tb[NETNSA_PID]));
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200687 nla = tb[NETNSA_PID];
688 } else if (tb[NETNSA_FD]) {
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100689 peer = get_net_ns_by_fd(nla_get_u32(tb[NETNSA_FD]));
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200690 nla = tb[NETNSA_FD];
691 } else {
692 NL_SET_ERR_MSG(extack, "Peer netns reference is missing");
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100693 return -EINVAL;
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200694 }
695 if (IS_ERR(peer)) {
696 NL_SET_BAD_ATTR(extack, nla);
697 NL_SET_ERR_MSG(extack, "Peer netns reference is invalid");
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100698 return PTR_ERR(peer);
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200699 }
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100700
Paul Moorefba143c2016-11-29 16:57:48 -0500701 spin_lock_bh(&net->nsid_lock);
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200702 if (__peernet2id(net, peer) >= 0) {
Paul Moorefba143c2016-11-29 16:57:48 -0500703 spin_unlock_bh(&net->nsid_lock);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100704 err = -EEXIST;
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200705 NL_SET_BAD_ATTR(extack, nla);
706 NL_SET_ERR_MSG(extack,
707 "Peer netns already has a nsid assigned");
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100708 goto out;
709 }
710
711 err = alloc_netid(net, peer, nsid);
Paul Moorefba143c2016-11-29 16:57:48 -0500712 spin_unlock_bh(&net->nsid_lock);
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200713 if (err >= 0) {
714 rtnl_net_notifyid(net, RTM_NEWNSID, err);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100715 err = 0;
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200716 } else if (err == -ENOSPC && nsid >= 0) {
Nicolas Dichtel10d486a2017-06-09 14:41:57 +0200717 err = -EEXIST;
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200718 NL_SET_BAD_ATTR(extack, tb[NETNSA_NSID]);
719 NL_SET_ERR_MSG(extack, "The specified nsid is already used");
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200720 }
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100721out:
722 put_net(peer);
723 return err;
724}
725
726static int rtnl_net_get_size(void)
727{
728 return NLMSG_ALIGN(sizeof(struct rtgenmsg))
729 + nla_total_size(sizeof(s32)) /* NETNSA_NSID */
730 ;
731}
732
733static int rtnl_net_fill(struct sk_buff *skb, u32 portid, u32 seq, int flags,
Nicolas Dichtelcab3c8e2015-05-07 11:02:48 +0200734 int cmd, struct net *net, int nsid)
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100735{
736 struct nlmsghdr *nlh;
737 struct rtgenmsg *rth;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100738
739 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rth), flags);
740 if (!nlh)
741 return -EMSGSIZE;
742
743 rth = nlmsg_data(nlh);
744 rth->rtgen_family = AF_UNSPEC;
745
Nicolas Dichtelcab3c8e2015-05-07 11:02:48 +0200746 if (nla_put_s32(skb, NETNSA_NSID, nsid))
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100747 goto nla_put_failure;
748
749 nlmsg_end(skb, nlh);
750 return 0;
751
752nla_put_failure:
753 nlmsg_cancel(skb, nlh);
754 return -EMSGSIZE;
755}
756
David Ahernc21ef3e2017-04-16 09:48:24 -0700757static int rtnl_net_getid(struct sk_buff *skb, struct nlmsghdr *nlh,
758 struct netlink_ext_ack *extack)
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100759{
760 struct net *net = sock_net(skb->sk);
761 struct nlattr *tb[NETNSA_MAX + 1];
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200762 struct nlattr *nla;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100763 struct sk_buff *msg;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100764 struct net *peer;
Nicolas Dichtelcab3c8e2015-05-07 11:02:48 +0200765 int err, id;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100766
767 err = nlmsg_parse(nlh, sizeof(struct rtgenmsg), tb, NETNSA_MAX,
David Ahernc21ef3e2017-04-16 09:48:24 -0700768 rtnl_net_policy, extack);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100769 if (err < 0)
770 return err;
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200771 if (tb[NETNSA_PID]) {
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100772 peer = get_net_ns_by_pid(nla_get_u32(tb[NETNSA_PID]));
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200773 nla = tb[NETNSA_PID];
774 } else if (tb[NETNSA_FD]) {
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100775 peer = get_net_ns_by_fd(nla_get_u32(tb[NETNSA_FD]));
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200776 nla = tb[NETNSA_FD];
777 } else {
778 NL_SET_ERR_MSG(extack, "Peer netns reference is missing");
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100779 return -EINVAL;
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200780 }
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100781
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200782 if (IS_ERR(peer)) {
783 NL_SET_BAD_ATTR(extack, nla);
784 NL_SET_ERR_MSG(extack, "Peer netns reference is invalid");
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100785 return PTR_ERR(peer);
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200786 }
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100787
788 msg = nlmsg_new(rtnl_net_get_size(), GFP_KERNEL);
789 if (!msg) {
790 err = -ENOMEM;
791 goto out;
792 }
793
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200794 id = peernet2id(net, peer);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100795 err = rtnl_net_fill(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
David S. Millerb04096f2015-05-13 14:31:43 -0400796 RTM_NEWNSID, net, id);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100797 if (err < 0)
798 goto err_out;
799
800 err = rtnl_unicast(msg, net, NETLINK_CB(skb).portid);
801 goto out;
802
803err_out:
804 nlmsg_free(msg);
805out:
806 put_net(peer);
807 return err;
808}
809
Nicolas Dichtela143c402015-04-07 11:51:54 +0200810struct rtnl_net_dump_cb {
811 struct net *net;
812 struct sk_buff *skb;
813 struct netlink_callback *cb;
814 int idx;
815 int s_idx;
816};
817
818static int rtnl_net_dumpid_one(int id, void *peer, void *data)
819{
820 struct rtnl_net_dump_cb *net_cb = (struct rtnl_net_dump_cb *)data;
821 int ret;
822
823 if (net_cb->idx < net_cb->s_idx)
824 goto cont;
825
826 ret = rtnl_net_fill(net_cb->skb, NETLINK_CB(net_cb->cb->skb).portid,
827 net_cb->cb->nlh->nlmsg_seq, NLM_F_MULTI,
Nicolas Dichtelcab3c8e2015-05-07 11:02:48 +0200828 RTM_NEWNSID, net_cb->net, id);
Nicolas Dichtela143c402015-04-07 11:51:54 +0200829 if (ret < 0)
830 return ret;
831
832cont:
833 net_cb->idx++;
834 return 0;
835}
836
837static int rtnl_net_dumpid(struct sk_buff *skb, struct netlink_callback *cb)
838{
839 struct net *net = sock_net(skb->sk);
840 struct rtnl_net_dump_cb net_cb = {
841 .net = net,
842 .skb = skb,
843 .cb = cb,
844 .idx = 0,
845 .s_idx = cb->args[0],
846 };
847
Paul Moorefba143c2016-11-29 16:57:48 -0500848 spin_lock_bh(&net->nsid_lock);
Nicolas Dichtela143c402015-04-07 11:51:54 +0200849 idr_for_each(&net->netns_ids, rtnl_net_dumpid_one, &net_cb);
Paul Moorefba143c2016-11-29 16:57:48 -0500850 spin_unlock_bh(&net->nsid_lock);
Nicolas Dichtela143c402015-04-07 11:51:54 +0200851
852 cb->args[0] = net_cb.idx;
853 return skb->len;
854}
855
Nicolas Dichtelcab3c8e2015-05-07 11:02:48 +0200856static void rtnl_net_notifyid(struct net *net, int cmd, int id)
Nicolas Dichtel9a963452015-04-07 11:51:53 +0200857{
858 struct sk_buff *msg;
859 int err = -ENOMEM;
860
861 msg = nlmsg_new(rtnl_net_get_size(), GFP_KERNEL);
862 if (!msg)
863 goto out;
864
Nicolas Dichtelcab3c8e2015-05-07 11:02:48 +0200865 err = rtnl_net_fill(msg, 0, 0, 0, cmd, net, id);
Nicolas Dichtel9a963452015-04-07 11:51:53 +0200866 if (err < 0)
867 goto err_out;
868
869 rtnl_notify(msg, net, 0, RTNLGRP_NSID, NULL, 0);
870 return;
871
872err_out:
873 nlmsg_free(msg);
874out:
875 rtnl_set_sk_err(net, RTNLGRP_NSID, err);
876}
877
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200878static int __init net_ns_init(void)
879{
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800880 struct net_generic *ng;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200881
Pavel Emelyanovd57a9212007-11-01 00:46:50 -0700882#ifdef CONFIG_NET_NS
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200883 net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
884 SMP_CACHE_BYTES,
885 SLAB_PANIC, NULL);
Benjamin Thery3ef13552007-11-19 23:18:16 -0800886
887 /* Create workqueue for cleanup */
888 netns_wq = create_singlethread_workqueue("netns");
889 if (!netns_wq)
890 panic("Could not create netns workq");
Pavel Emelyanovd57a9212007-11-01 00:46:50 -0700891#endif
Benjamin Thery3ef13552007-11-19 23:18:16 -0800892
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800893 ng = net_alloc_generic();
894 if (!ng)
895 panic("Could not allocate generic netns");
896
897 rcu_assign_pointer(init_net.gen, ng);
898
Kirill Tkhai1a57feb2018-02-13 12:26:23 +0300899 down_write(&net_sem);
Eric W. Biederman038e7332012-06-14 02:31:10 -0700900 if (setup_net(&init_net, &init_user_ns))
Stephen Hemmingerca0f3112009-05-21 15:10:31 -0700901 panic("Could not setup the initial network namespace");
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200902
Dmitry Torokhovf8c46cb2016-08-10 14:36:00 -0700903 init_net_initialized = true;
Kirill Tkhai1a57feb2018-02-13 12:26:23 +0300904 up_write(&net_sem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200905
Eric W. Biederman98f842e2011-06-15 10:21:48 -0700906 register_pernet_subsys(&net_ns_ops);
907
Florian Westphal165b9112017-08-09 20:41:53 +0200908 rtnl_register(PF_UNSPEC, RTM_NEWNSID, rtnl_net_newid, NULL,
909 RTNL_FLAG_DOIT_UNLOCKED);
Nicolas Dichtela143c402015-04-07 11:51:54 +0200910 rtnl_register(PF_UNSPEC, RTM_GETNSID, rtnl_net_getid, rtnl_net_dumpid,
Florian Westphal165b9112017-08-09 20:41:53 +0200911 RTNL_FLAG_DOIT_UNLOCKED);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100912
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200913 return 0;
914}
915
916pure_initcall(net_ns_init);
917
Denis V. Luneved160e82007-11-13 03:23:21 -0800918#ifdef CONFIG_NET_NS
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000919static int __register_pernet_operations(struct list_head *list,
920 struct pernet_operations *ops)
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200921{
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000922 struct net *net;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200923 int error;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000924 LIST_HEAD(net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200925
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200926 list_add_tail(&ops->list, list);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000927 if (ops->init || (ops->id && ops->size)) {
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700928 for_each_net(net) {
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000929 error = ops_init(ops, net);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200930 if (error)
931 goto out_undo;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000932 list_add_tail(&net->exit_list, &net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200933 }
934 }
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700935 return 0;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200936
937out_undo:
938 /* If I have an error cleanup all namespaces I initialized */
939 list_del(&ops->list);
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000940 ops_exit_list(ops, &net_exit_list);
941 ops_free_list(ops, &net_exit_list);
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700942 return error;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200943}
944
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000945static void __unregister_pernet_operations(struct pernet_operations *ops)
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200946{
947 struct net *net;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000948 LIST_HEAD(net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200949
950 list_del(&ops->list);
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000951 for_each_net(net)
952 list_add_tail(&net->exit_list, &net_exit_list);
953 ops_exit_list(ops, &net_exit_list);
954 ops_free_list(ops, &net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200955}
956
Denis V. Luneved160e82007-11-13 03:23:21 -0800957#else
958
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000959static int __register_pernet_operations(struct list_head *list,
960 struct pernet_operations *ops)
961{
Dmitry Torokhovf8c46cb2016-08-10 14:36:00 -0700962 if (!init_net_initialized) {
963 list_add_tail(&ops->list, list);
964 return 0;
965 }
966
Julian Anastasovb9229342012-04-16 04:43:15 +0000967 return ops_init(ops, &init_net);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000968}
969
970static void __unregister_pernet_operations(struct pernet_operations *ops)
971{
Dmitry Torokhovf8c46cb2016-08-10 14:36:00 -0700972 if (!init_net_initialized) {
973 list_del(&ops->list);
974 } else {
975 LIST_HEAD(net_exit_list);
976 list_add(&init_net.exit_list, &net_exit_list);
977 ops_exit_list(ops, &net_exit_list);
978 ops_free_list(ops, &net_exit_list);
979 }
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000980}
981
982#endif /* CONFIG_NET_NS */
983
984static DEFINE_IDA(net_generic_ids);
985
Denis V. Luneved160e82007-11-13 03:23:21 -0800986static int register_pernet_operations(struct list_head *list,
987 struct pernet_operations *ops)
988{
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000989 int error;
990
991 if (ops->id) {
992again:
Alexey Dobriyan6af2d5f2016-12-02 04:21:32 +0300993 error = ida_get_new_above(&net_generic_ids, MIN_PERNET_OPS_ID, ops->id);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000994 if (error < 0) {
995 if (error == -EAGAIN) {
996 ida_pre_get(&net_generic_ids, GFP_KERNEL);
997 goto again;
998 }
999 return error;
1000 }
Alexey Dobriyan6af2d5f2016-12-02 04:21:32 +03001001 max_gen_ptrs = max(max_gen_ptrs, *ops->id + 1);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +00001002 }
1003 error = __register_pernet_operations(list, ops);
Eric W. Biederman3a765ed2009-12-03 02:29:06 +00001004 if (error) {
1005 rcu_barrier();
1006 if (ops->id)
1007 ida_remove(&net_generic_ids, *ops->id);
Kirill Tkhai447cd7a2018-02-13 12:26:44 +03001008 } else if (!ops->async) {
1009 pr_info_once("Pernet operations %ps are sync.\n", ops);
1010 nr_sync_pernet_ops++;
Eric W. Biederman3a765ed2009-12-03 02:29:06 +00001011 }
Eric W. Biedermanf875bae2009-11-29 22:25:28 +00001012
1013 return error;
Denis V. Luneved160e82007-11-13 03:23:21 -08001014}
1015
1016static void unregister_pernet_operations(struct pernet_operations *ops)
1017{
Kirill Tkhai447cd7a2018-02-13 12:26:44 +03001018 if (!ops->async)
1019 BUG_ON(nr_sync_pernet_ops-- == 0);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +00001020 __unregister_pernet_operations(ops);
Eric W. Biederman3a765ed2009-12-03 02:29:06 +00001021 rcu_barrier();
Eric W. Biedermanf875bae2009-11-29 22:25:28 +00001022 if (ops->id)
1023 ida_remove(&net_generic_ids, *ops->id);
Denis V. Luneved160e82007-11-13 03:23:21 -08001024}
Pavel Emelyanovc93cf612008-04-15 00:35:23 -07001025
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001026/**
1027 * register_pernet_subsys - register a network namespace subsystem
1028 * @ops: pernet operations structure for the subsystem
1029 *
1030 * Register a subsystem which has init and exit functions
1031 * that are called when network namespaces are created and
1032 * destroyed respectively.
1033 *
1034 * When registered all network namespace init functions are
1035 * called for every existing network namespace. Allowing kernel
1036 * modules to have a race free view of the set of network namespaces.
1037 *
1038 * When a new network namespace is created all of the init
1039 * methods are called in the order in which they were registered.
1040 *
1041 * When a network namespace is destroyed all of the exit methods
1042 * are called in the reverse of the order with which they were
1043 * registered.
1044 */
1045int register_pernet_subsys(struct pernet_operations *ops)
1046{
1047 int error;
Kirill Tkhai1a57feb2018-02-13 12:26:23 +03001048 down_write(&net_sem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001049 error = register_pernet_operations(first_device, ops);
Kirill Tkhai1a57feb2018-02-13 12:26:23 +03001050 up_write(&net_sem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001051 return error;
1052}
1053EXPORT_SYMBOL_GPL(register_pernet_subsys);
1054
1055/**
1056 * unregister_pernet_subsys - unregister a network namespace subsystem
1057 * @ops: pernet operations structure to manipulate
1058 *
1059 * Remove the pernet operations structure from the list to be
Oliver Pinter53379e52008-02-03 17:56:48 +02001060 * used when network namespaces are created or destroyed. In
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001061 * addition run the exit method for all existing network
1062 * namespaces.
1063 */
Jiri Pirkob3c981d2010-04-25 00:49:56 -07001064void unregister_pernet_subsys(struct pernet_operations *ops)
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001065{
Kirill Tkhai1a57feb2018-02-13 12:26:23 +03001066 down_write(&net_sem);
Jiri Pirkob3c981d2010-04-25 00:49:56 -07001067 unregister_pernet_operations(ops);
Kirill Tkhai1a57feb2018-02-13 12:26:23 +03001068 up_write(&net_sem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001069}
1070EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
1071
1072/**
1073 * register_pernet_device - register a network namespace device
1074 * @ops: pernet operations structure for the subsystem
1075 *
1076 * Register a device which has init and exit functions
1077 * that are called when network namespaces are created and
1078 * destroyed respectively.
1079 *
1080 * When registered all network namespace init functions are
1081 * called for every existing network namespace. Allowing kernel
1082 * modules to have a race free view of the set of network namespaces.
1083 *
1084 * When a new network namespace is created all of the init
1085 * methods are called in the order in which they were registered.
1086 *
1087 * When a network namespace is destroyed all of the exit methods
1088 * are called in the reverse of the order with which they were
1089 * registered.
1090 */
1091int register_pernet_device(struct pernet_operations *ops)
1092{
1093 int error;
Kirill Tkhai1a57feb2018-02-13 12:26:23 +03001094 down_write(&net_sem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001095 error = register_pernet_operations(&pernet_list, ops);
1096 if (!error && (first_device == &pernet_list))
1097 first_device = &ops->list;
Kirill Tkhai1a57feb2018-02-13 12:26:23 +03001098 up_write(&net_sem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001099 return error;
1100}
1101EXPORT_SYMBOL_GPL(register_pernet_device);
1102
1103/**
1104 * unregister_pernet_device - unregister a network namespace netdevice
1105 * @ops: pernet operations structure to manipulate
1106 *
1107 * Remove the pernet operations structure from the list to be
Oliver Pinter53379e52008-02-03 17:56:48 +02001108 * used when network namespaces are created or destroyed. In
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001109 * addition run the exit method for all existing network
1110 * namespaces.
1111 */
1112void unregister_pernet_device(struct pernet_operations *ops)
1113{
Kirill Tkhai1a57feb2018-02-13 12:26:23 +03001114 down_write(&net_sem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001115 if (&ops->list == first_device)
1116 first_device = first_device->next;
1117 unregister_pernet_operations(ops);
Kirill Tkhai1a57feb2018-02-13 12:26:23 +03001118 up_write(&net_sem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001119}
1120EXPORT_SYMBOL_GPL(unregister_pernet_device);
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001121
1122#ifdef CONFIG_NET_NS
Al Viro64964522014-11-01 00:37:32 -04001123static struct ns_common *netns_get(struct task_struct *task)
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001124{
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001125 struct net *net = NULL;
1126 struct nsproxy *nsproxy;
1127
Eric W. Biederman728dba32014-02-03 19:13:49 -08001128 task_lock(task);
1129 nsproxy = task->nsproxy;
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001130 if (nsproxy)
1131 net = get_net(nsproxy->net_ns);
Eric W. Biederman728dba32014-02-03 19:13:49 -08001132 task_unlock(task);
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001133
Al Viroff248702014-11-01 00:10:50 -04001134 return net ? &net->ns : NULL;
1135}
1136
1137static inline struct net *to_net_ns(struct ns_common *ns)
1138{
1139 return container_of(ns, struct net, ns);
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001140}
1141
Al Viro64964522014-11-01 00:37:32 -04001142static void netns_put(struct ns_common *ns)
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001143{
Al Viroff248702014-11-01 00:10:50 -04001144 put_net(to_net_ns(ns));
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001145}
1146
Al Viro64964522014-11-01 00:37:32 -04001147static int netns_install(struct nsproxy *nsproxy, struct ns_common *ns)
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001148{
Al Viroff248702014-11-01 00:10:50 -04001149 struct net *net = to_net_ns(ns);
Eric W. Biederman142e1d12012-07-26 01:13:20 -07001150
Eric W. Biederman5e4a0842012-12-14 07:55:36 -08001151 if (!ns_capable(net->user_ns, CAP_SYS_ADMIN) ||
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -07001152 !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
Eric W. Biederman142e1d12012-07-26 01:13:20 -07001153 return -EPERM;
1154
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001155 put_net(nsproxy->net_ns);
Eric W. Biederman142e1d12012-07-26 01:13:20 -07001156 nsproxy->net_ns = get_net(net);
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001157 return 0;
1158}
1159
Andrey Vaginbcac25a2016-09-06 00:47:13 -07001160static struct user_namespace *netns_owner(struct ns_common *ns)
1161{
1162 return to_net_ns(ns)->user_ns;
1163}
1164
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001165const struct proc_ns_operations netns_operations = {
1166 .name = "net",
1167 .type = CLONE_NEWNET,
1168 .get = netns_get,
1169 .put = netns_put,
1170 .install = netns_install,
Andrey Vaginbcac25a2016-09-06 00:47:13 -07001171 .owner = netns_owner,
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001172};
1173#endif