blob: 738871af5efaa246b55c1803259e98f67a60ae54 [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>
Tyler Hicksfbdeaed2018-07-20 21:56:53 +000020#include <linux/uidgid.h>
Ingo Molnarf719ff9b2017-02-06 10:57:33 +010021
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +010022#include <net/sock.h>
23#include <net/netlink.h>
Eric W. Biederman5f256be2007-09-12 11:50:50 +020024#include <net/net_namespace.h>
Pavel Emelyanovdec827d2008-04-15 00:36:08 -070025#include <net/netns/generic.h>
Eric W. Biederman5f256be2007-09-12 11:50:50 +020026
27/*
28 * Our network namespace constructor/destructor lists
29 */
30
31static LIST_HEAD(pernet_list);
32static struct list_head *first_device = &pernet_list;
Eric W. Biederman5f256be2007-09-12 11:50:50 +020033
Eric W. Biederman5f256be2007-09-12 11:50:50 +020034LIST_HEAD(net_namespace_list);
Alexey Dobriyanb76a4612008-10-08 11:35:06 +020035EXPORT_SYMBOL_GPL(net_namespace_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +020036
Kirill Tkhaif0b07bb12018-03-29 19:20:32 +030037/* Protects net_namespace_list. Nests iside rtnl_lock() */
38DECLARE_RWSEM(net_rwsem);
39EXPORT_SYMBOL_GPL(net_rwsem);
40
Rustad, Mark D734b6542012-07-18 09:06:07 +000041struct net init_net = {
Kirill Tkhai273c28b2018-01-12 18:28:31 +030042 .count = REFCOUNT_INIT(1),
David Howellsb5082df2017-04-27 22:40:23 +010043 .dev_base_head = LIST_HEAD_INIT(init_net.dev_base_head),
Rustad, Mark D734b6542012-07-18 09:06:07 +000044};
Denis V. Lunevff4b9502008-01-22 22:05:33 -080045EXPORT_SYMBOL(init_net);
Eric W. Biederman5f256be2007-09-12 11:50:50 +020046
Dmitry Torokhovf8c46cb2016-08-10 14:36:00 -070047static bool init_net_initialized;
Kirill Tkhai1a57feb2018-02-13 12:26:23 +030048/*
Kirill Tkhai4420bf22018-03-27 18:02:23 +030049 * pernet_ops_rwsem: protects: pernet_list, net_generic_ids,
Kirill Tkhai1a57feb2018-02-13 12:26:23 +030050 * init_net_initialized and first_device pointer.
Kirill Tkhai8518e9b2018-03-27 18:02:32 +030051 * This is internal net namespace object. Please, don't use it
52 * outside.
Kirill Tkhai1a57feb2018-02-13 12:26:23 +030053 */
Kirill Tkhai4420bf22018-03-27 18:02:23 +030054DECLARE_RWSEM(pernet_ops_rwsem);
Kirill Tkhai554873e2018-03-30 19:38:37 +030055EXPORT_SYMBOL_GPL(pernet_ops_rwsem);
Dmitry Torokhovf8c46cb2016-08-10 14:36:00 -070056
Alexey Dobriyan6af2d5f2016-12-02 04:21:32 +030057#define MIN_PERNET_OPS_ID \
58 ((sizeof(struct net_generic) + sizeof(void *) - 1) / sizeof(void *))
59
Pavel Emelyanovdec827d2008-04-15 00:36:08 -070060#define INITIAL_NET_GEN_PTRS 13 /* +1 for len +2 for rcu_head */
61
Eric Dumazet073862b2012-01-26 00:41:38 +000062static unsigned int max_gen_ptrs = INITIAL_NET_GEN_PTRS;
63
64static struct net_generic *net_alloc_generic(void)
65{
66 struct net_generic *ng;
Alexey Dobriyan6af2d5f2016-12-02 04:21:32 +030067 unsigned int generic_size = offsetof(struct net_generic, ptr[max_gen_ptrs]);
Eric Dumazet073862b2012-01-26 00:41:38 +000068
69 ng = kzalloc(generic_size, GFP_KERNEL);
70 if (ng)
Alexey Dobriyan9bfc7b92016-12-02 04:12:58 +030071 ng->s.len = max_gen_ptrs;
Eric Dumazet073862b2012-01-26 00:41:38 +000072
73 return ng;
74}
75
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030076static int net_assign_generic(struct net *net, unsigned int id, void *data)
Jiri Pirko05fceb42010-04-23 01:40:47 +000077{
78 struct net_generic *ng, *old_ng;
79
Alexey Dobriyan6af2d5f2016-12-02 04:21:32 +030080 BUG_ON(id < MIN_PERNET_OPS_ID);
Jiri Pirko05fceb42010-04-23 01:40:47 +000081
Eric Dumazet1c877332010-10-25 03:20:11 +000082 old_ng = rcu_dereference_protected(net->gen,
Kirill Tkhai4420bf22018-03-27 18:02:23 +030083 lockdep_is_held(&pernet_ops_rwsem));
Alexey Dobriyan6af2d5f2016-12-02 04:21:32 +030084 if (old_ng->s.len > id) {
85 old_ng->ptr[id] = data;
Alexey Dobriyan1a9a05922016-12-02 04:11:34 +030086 return 0;
87 }
Jiri Pirko05fceb42010-04-23 01:40:47 +000088
Eric Dumazet073862b2012-01-26 00:41:38 +000089 ng = net_alloc_generic();
Jiri Pirko05fceb42010-04-23 01:40:47 +000090 if (ng == NULL)
91 return -ENOMEM;
92
93 /*
94 * Some synchronisation notes:
95 *
96 * The net_generic explores the net->gen array inside rcu
97 * read section. Besides once set the net->gen->ptr[x]
98 * pointer never changes (see rules in netns/generic.h).
99 *
100 * That said, we simply duplicate this array and schedule
101 * the old copy for kfree after a grace period.
102 */
103
Alexey Dobriyan6af2d5f2016-12-02 04:21:32 +0300104 memcpy(&ng->ptr[MIN_PERNET_OPS_ID], &old_ng->ptr[MIN_PERNET_OPS_ID],
105 (old_ng->s.len - MIN_PERNET_OPS_ID) * sizeof(void *));
106 ng->ptr[id] = data;
Jiri Pirko05fceb42010-04-23 01:40:47 +0000107
108 rcu_assign_pointer(net->gen, ng);
Alexey Dobriyan9bfc7b92016-12-02 04:12:58 +0300109 kfree_rcu(old_ng, s.rcu);
Jiri Pirko05fceb42010-04-23 01:40:47 +0000110 return 0;
111}
112
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000113static int ops_init(const struct pernet_operations *ops, struct net *net)
114{
Julian Anastasovb9229342012-04-16 04:43:15 +0000115 int err = -ENOMEM;
116 void *data = NULL;
117
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000118 if (ops->id && ops->size) {
Julian Anastasovb9229342012-04-16 04:43:15 +0000119 data = kzalloc(ops->size, GFP_KERNEL);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000120 if (!data)
Julian Anastasovb9229342012-04-16 04:43:15 +0000121 goto out;
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000122
123 err = net_assign_generic(net, *ops->id, data);
Julian Anastasovb9229342012-04-16 04:43:15 +0000124 if (err)
125 goto cleanup;
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000126 }
Julian Anastasovb9229342012-04-16 04:43:15 +0000127 err = 0;
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000128 if (ops->init)
Julian Anastasovb9229342012-04-16 04:43:15 +0000129 err = ops->init(net);
130 if (!err)
131 return 0;
132
133cleanup:
134 kfree(data);
135
136out:
137 return err;
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000138}
139
140static void ops_free(const struct pernet_operations *ops, struct net *net)
141{
142 if (ops->id && ops->size) {
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +0300143 kfree(net_generic(net, *ops->id));
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000144 }
145}
146
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000147static void ops_exit_list(const struct pernet_operations *ops,
148 struct list_head *net_exit_list)
149{
150 struct net *net;
151 if (ops->exit) {
152 list_for_each_entry(net, net_exit_list, exit_list)
153 ops->exit(net);
154 }
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000155 if (ops->exit_batch)
156 ops->exit_batch(net_exit_list);
157}
158
159static void ops_free_list(const struct pernet_operations *ops,
160 struct list_head *net_exit_list)
161{
162 struct net *net;
163 if (ops->size && ops->id) {
164 list_for_each_entry(net, net_exit_list, exit_list)
165 ops_free(ops, net);
166 }
167}
168
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200169/* should be called with nsid_lock held */
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100170static int alloc_netid(struct net *net, struct net *peer, int reqid)
171{
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200172 int min = 0, max = 0;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100173
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100174 if (reqid >= 0) {
175 min = reqid;
176 max = reqid + 1;
177 }
178
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200179 return idr_alloc(&net->netns_ids, peer, min, max, GFP_ATOMIC);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100180}
181
182/* This function is used by idr_for_each(). If net is equal to peer, the
183 * function returns the id so that idr_for_each() stops. Because we cannot
184 * returns the id 0 (idr_for_each() will not stop), we return the magic value
185 * NET_ID_ZERO (-1) for it.
186 */
187#define NET_ID_ZERO -1
188static int net_eq_idr(int id, void *net, void *peer)
189{
190 if (net_eq(net, peer))
191 return id ? : NET_ID_ZERO;
192 return 0;
193}
194
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200195/* Should be called with nsid_lock held. If a new id is assigned, the bool alloc
196 * is set to true, thus the caller knows that the new id must be notified via
197 * rtnl.
198 */
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200199static int __peernet2id_alloc(struct net *net, struct net *peer, bool *alloc)
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100200{
201 int id = idr_for_each(&net->netns_ids, net_eq_idr, peer);
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200202 bool alloc_it = *alloc;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100203
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200204 *alloc = false;
205
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100206 /* Magic value for id 0. */
207 if (id == NET_ID_ZERO)
208 return 0;
209 if (id > 0)
210 return id;
211
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200212 if (alloc_it) {
Nicolas Dichtel109582af2015-05-07 11:02:47 +0200213 id = alloc_netid(net, peer, -1);
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200214 *alloc = true;
Nicolas Dichtel109582af2015-05-07 11:02:47 +0200215 return id >= 0 ? id : NETNSA_NSID_NOT_ASSIGNED;
216 }
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100217
Nicolas Dichtel109582af2015-05-07 11:02:47 +0200218 return NETNSA_NSID_NOT_ASSIGNED;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100219}
220
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200221/* should be called with nsid_lock held */
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200222static int __peernet2id(struct net *net, struct net *peer)
223{
224 bool no = false;
225
226 return __peernet2id_alloc(net, peer, &no);
227}
228
229static void rtnl_net_notifyid(struct net *net, int cmd, int id);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100230/* This function returns the id of a peer netns. If no id is assigned, one will
231 * be allocated and returned.
232 */
Nicolas Dichtel7a0877d2015-05-07 11:02:49 +0200233int peernet2id_alloc(struct net *net, struct net *peer)
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100234{
Kirill Tkhai0c06bea2018-01-16 12:31:41 +0300235 bool alloc = false, alive = false;
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200236 int id;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100237
Kirill Tkhai273c28b2018-01-12 18:28:31 +0300238 if (refcount_read(&net->count) == 0)
WANG Congcfc44a42016-11-16 10:27:02 -0800239 return NETNSA_NSID_NOT_ASSIGNED;
Paul Moorefba143c2016-11-29 16:57:48 -0500240 spin_lock_bh(&net->nsid_lock);
Kirill Tkhai0c06bea2018-01-16 12:31:41 +0300241 /*
242 * When peer is obtained from RCU lists, we may race with
243 * its cleanup. Check whether it's alive, and this guarantees
244 * we never hash a peer back to net->netns_ids, after it has
245 * just been idr_remove()'d from there in cleanup_net().
246 */
247 if (maybe_get_net(peer))
248 alive = alloc = true;
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200249 id = __peernet2id_alloc(net, peer, &alloc);
Paul Moorefba143c2016-11-29 16:57:48 -0500250 spin_unlock_bh(&net->nsid_lock);
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200251 if (alloc && id >= 0)
252 rtnl_net_notifyid(net, RTM_NEWNSID, id);
Kirill Tkhai0c06bea2018-01-16 12:31:41 +0300253 if (alive)
254 put_net(peer);
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200255 return id;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100256}
Jiri Benc7cbebc82017-11-02 17:04:36 -0200257EXPORT_SYMBOL_GPL(peernet2id_alloc);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100258
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200259/* This function returns, if assigned, the id of a peer netns. */
Nicolas Dichtel59324cf2015-05-07 11:02:53 +0200260int peernet2id(struct net *net, struct net *peer)
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200261{
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200262 int id;
263
Paul Moorefba143c2016-11-29 16:57:48 -0500264 spin_lock_bh(&net->nsid_lock);
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200265 id = __peernet2id(net, peer);
Paul Moorefba143c2016-11-29 16:57:48 -0500266 spin_unlock_bh(&net->nsid_lock);
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200267 return id;
268}
WANG Cong38f507f2016-09-01 21:53:44 -0700269EXPORT_SYMBOL(peernet2id);
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200270
Nicolas Dichtel59324cf2015-05-07 11:02:53 +0200271/* This function returns true is the peer netns has an id assigned into the
272 * current netns.
273 */
274bool peernet_has_id(struct net *net, struct net *peer)
275{
276 return peernet2id(net, peer) >= 0;
277}
278
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100279struct net *get_net_ns_by_id(struct net *net, int id)
280{
281 struct net *peer;
282
283 if (id < 0)
284 return NULL;
285
286 rcu_read_lock();
287 peer = idr_find(&net->netns_ids, id);
288 if (peer)
Eric W. Biederman21b59442017-12-19 11:27:56 -0600289 peer = maybe_get_net(peer);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100290 rcu_read_unlock();
291
292 return peer;
293}
294
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700295/*
296 * setup_net runs the initializers for the network namespace object.
297 */
Eric W. Biederman038e7332012-06-14 02:31:10 -0700298static __net_init int setup_net(struct net *net, struct user_namespace *user_ns)
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700299{
Kirill Tkhai4420bf22018-03-27 18:02:23 +0300300 /* Must be called with pernet_ops_rwsem held */
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000301 const struct pernet_operations *ops, *saved_ops;
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800302 int error = 0;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000303 LIST_HEAD(net_exit_list);
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700304
Kirill Tkhai273c28b2018-01-12 18:28:31 +0300305 refcount_set(&net->count, 1);
Reshetova, Elenac122e142017-06-30 13:08:08 +0300306 refcount_set(&net->passive, 1);
Thomas Graf4e985ad2011-06-21 03:11:20 +0000307 net->dev_base_seq = 1;
Eric W. Biederman038e7332012-06-14 02:31:10 -0700308 net->user_ns = user_ns;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100309 idr_init(&net->netns_ids);
WANG Congde133462015-05-15 14:47:32 -0700310 spin_lock_init(&net->nsid_lock);
Kirill Tkhaid9ff3042018-03-22 12:45:40 +0300311 mutex_init(&net->ipv4.ra_mutex);
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800312
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700313 list_for_each_entry(ops, &pernet_list, list) {
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000314 error = ops_init(ops, net);
315 if (error < 0)
316 goto out_undo;
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700317 }
Kirill Tkhaif0b07bb12018-03-29 19:20:32 +0300318 down_write(&net_rwsem);
Kirill Tkhai98f6c532018-02-13 12:26:02 +0300319 list_add_tail_rcu(&net->list, &net_namespace_list);
Kirill Tkhaif0b07bb12018-03-29 19:20:32 +0300320 up_write(&net_rwsem);
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700321out:
322 return error;
323
324out_undo:
325 /* Walk through the list backwards calling the exit functions
326 * for the pernet modules whose init functions did not fail.
327 */
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000328 list_add(&net->exit_list, &net_exit_list);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000329 saved_ops = ops;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000330 list_for_each_entry_continue_reverse(ops, &pernet_list, list)
331 ops_exit_list(ops, &net_exit_list);
332
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000333 ops = saved_ops;
334 list_for_each_entry_continue_reverse(ops, &pernet_list, list)
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000335 ops_free_list(ops, &net_exit_list);
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700336
337 rcu_barrier();
338 goto out;
339}
340
Roman Kapl7c3f18752017-05-24 10:22:22 +0200341static int __net_init net_defaults_init_net(struct net *net)
342{
343 net->core.sysctl_somaxconn = SOMAXCONN;
344 return 0;
345}
346
347static struct pernet_operations net_defaults_ops = {
348 .init = net_defaults_init_net,
349};
350
351static __init int net_defaults_init(void)
352{
353 if (register_pernet_subsys(&net_defaults_ops))
354 panic("Cannot initialize net default settings");
355
356 return 0;
357}
358
359core_initcall(net_defaults_init);
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800360
Clemens Nossebe47d42009-02-23 15:37:35 -0800361#ifdef CONFIG_NET_NS
Arnd Bergmann2ed6afd2016-09-23 18:06:12 +0200362static struct ucounts *inc_net_namespaces(struct user_namespace *ns)
363{
364 return inc_ucount(ns, current_euid(), UCOUNT_NET_NAMESPACES);
365}
366
367static void dec_net_namespaces(struct ucounts *ucounts)
368{
369 dec_ucount(ucounts, UCOUNT_NET_NAMESPACES);
370}
371
Alexey Dobriyan08009a72018-02-24 21:20:33 +0300372static struct kmem_cache *net_cachep __ro_after_init;
Clemens Nossebe47d42009-02-23 15:37:35 -0800373static struct workqueue_struct *netns_wq;
374
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200375static struct net *net_alloc(void)
376{
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800377 struct net *net = NULL;
378 struct net_generic *ng;
379
380 ng = net_alloc_generic();
381 if (!ng)
382 goto out;
383
384 net = kmem_cache_zalloc(net_cachep, GFP_KERNEL);
385 if (!net)
386 goto out_free;
387
388 rcu_assign_pointer(net->gen, ng);
389out:
390 return net;
391
392out_free:
393 kfree(ng);
394 goto out;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200395}
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200396
Johann Felix Soden45a19b02007-11-07 01:30:30 -0800397static void net_free(struct net *net)
398{
Eric Dumazet416c51e2014-09-09 08:24:53 -0700399 kfree(rcu_access_pointer(net->gen));
Johann Felix Soden45a19b02007-11-07 01:30:30 -0800400 kmem_cache_free(net_cachep, net);
401}
402
Al Viroa685e082011-06-08 21:13:01 -0400403void net_drop_ns(void *p)
404{
405 struct net *ns = p;
Reshetova, Elenac122e142017-06-30 13:08:08 +0300406 if (ns && refcount_dec_and_test(&ns->passive))
Al Viroa685e082011-06-08 21:13:01 -0400407 net_free(ns);
408}
409
Eric W. Biederman038e7332012-06-14 02:31:10 -0700410struct net *copy_net_ns(unsigned long flags,
411 struct user_namespace *user_ns, struct net *old_net)
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700412{
Eric W. Biederman70328662016-08-08 14:33:23 -0500413 struct ucounts *ucounts;
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700414 struct net *net;
415 int rv;
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700416
Rob Landley911cb192011-04-15 02:26:25 +0000417 if (!(flags & CLONE_NEWNET))
418 return get_net(old_net);
419
Eric W. Biederman70328662016-08-08 14:33:23 -0500420 ucounts = inc_net_namespaces(user_ns);
421 if (!ucounts)
Eric W. Biedermandf75e772016-09-22 13:08:36 -0500422 return ERR_PTR(-ENOSPC);
Eric W. Biederman70328662016-08-08 14:33:23 -0500423
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700424 net = net_alloc();
Eric W. Biederman70328662016-08-08 14:33:23 -0500425 if (!net) {
Kirill Tkhai5ba049a2018-02-13 12:26:13 +0300426 rv = -ENOMEM;
427 goto dec_ucounts;
Eric W. Biederman70328662016-08-08 14:33:23 -0500428 }
Kirill Tkhai5ba049a2018-02-13 12:26:13 +0300429 refcount_set(&net->passive, 1);
430 net->ucounts = ucounts;
Eric W. Biederman038e7332012-06-14 02:31:10 -0700431 get_user_ns(user_ns);
Kirill Tkhai094374e2018-03-27 18:02:01 +0300432
Kirill Tkhai4420bf22018-03-27 18:02:23 +0300433 rv = down_read_killable(&pernet_ops_rwsem);
Kirill Tkhai5ba049a2018-02-13 12:26:13 +0300434 if (rv < 0)
435 goto put_userns;
Kirill Tkhai19efbd92018-02-19 12:58:38 +0300436
Eric W. Biederman038e7332012-06-14 02:31:10 -0700437 rv = setup_net(net, user_ns);
Kirill Tkhai19efbd92018-02-19 12:58:38 +0300438
Kirill Tkhai4420bf22018-03-27 18:02:23 +0300439 up_read(&pernet_ops_rwsem);
Kirill Tkhai19efbd92018-02-19 12:58:38 +0300440
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700441 if (rv < 0) {
Kirill Tkhai5ba049a2018-02-13 12:26:13 +0300442put_userns:
Eric W. Biederman038e7332012-06-14 02:31:10 -0700443 put_user_ns(user_ns);
Al Viroa685e082011-06-08 21:13:01 -0400444 net_drop_ns(net);
Kirill Tkhai5ba049a2018-02-13 12:26:13 +0300445dec_ucounts:
446 dec_net_namespaces(ucounts);
Alexey Dobriyan088eb2d2009-05-04 11:12:14 -0700447 return ERR_PTR(rv);
448 }
449 return net;
450}
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800451
Tyler Hicksfbdeaed2018-07-20 21:56:53 +0000452/**
453 * net_ns_get_ownership - get sysfs ownership data for @net
454 * @net: network namespace in question (can be NULL)
455 * @uid: kernel user ID for sysfs objects
456 * @gid: kernel group ID for sysfs objects
457 *
458 * Returns the uid/gid pair of root in the user namespace associated with the
459 * given network namespace.
460 */
461void net_ns_get_ownership(const struct net *net, kuid_t *uid, kgid_t *gid)
462{
463 if (net) {
464 kuid_t ns_root_uid = make_kuid(net->user_ns, 0);
465 kgid_t ns_root_gid = make_kgid(net->user_ns, 0);
466
467 if (uid_valid(ns_root_uid))
468 *uid = ns_root_uid;
469
470 if (gid_valid(ns_root_gid))
471 *gid = ns_root_gid;
472 } else {
473 *uid = GLOBAL_ROOT_UID;
474 *gid = GLOBAL_ROOT_GID;
475 }
476}
477EXPORT_SYMBOL_GPL(net_ns_get_ownership);
478
Kirill Tkhaifb07a822018-01-19 19:14:53 +0300479static void unhash_nsid(struct net *net, struct net *last)
480{
481 struct net *tmp;
482 /* This function is only called from cleanup_net() work,
483 * and this work is the only process, that may delete
484 * a net from net_namespace_list. So, when the below
485 * is executing, the list may only grow. Thus, we do not
Kirill Tkhaif0b07bb12018-03-29 19:20:32 +0300486 * use for_each_net_rcu() or net_rwsem.
Kirill Tkhaifb07a822018-01-19 19:14:53 +0300487 */
488 for_each_net(tmp) {
489 int id;
490
491 spin_lock_bh(&tmp->nsid_lock);
492 id = __peernet2id(tmp, net);
493 if (id >= 0)
494 idr_remove(&tmp->netns_ids, id);
495 spin_unlock_bh(&tmp->nsid_lock);
496 if (id >= 0)
497 rtnl_net_notifyid(tmp, RTM_DELNSID, id);
498 if (tmp == last)
499 break;
500 }
501 spin_lock_bh(&net->nsid_lock);
502 idr_destroy(&net->netns_ids);
503 spin_unlock_bh(&net->nsid_lock);
504}
505
Kirill Tkhai65b7b5b2018-02-19 12:58:45 +0300506static LLIST_HEAD(cleanup_list);
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000507
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200508static void cleanup_net(struct work_struct *work)
509{
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000510 const struct pernet_operations *ops;
Kirill Tkhaifb07a822018-01-19 19:14:53 +0300511 struct net *net, *tmp, *last;
Kirill Tkhai65b7b5b2018-02-19 12:58:45 +0300512 struct llist_node *net_kill_list;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000513 LIST_HEAD(net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200514
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000515 /* Atomically snapshot the list of namespaces to cleanup */
Kirill Tkhai65b7b5b2018-02-19 12:58:45 +0300516 net_kill_list = llist_del_all(&cleanup_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200517
Kirill Tkhai4420bf22018-03-27 18:02:23 +0300518 down_read(&pernet_ops_rwsem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200519
520 /* Don't let anyone else find us. */
Kirill Tkhaif0b07bb12018-03-29 19:20:32 +0300521 down_write(&net_rwsem);
Kirill Tkhai65b7b5b2018-02-19 12:58:45 +0300522 llist_for_each_entry(net, net_kill_list, cleanup_list)
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000523 list_del_rcu(&net->list);
Kirill Tkhaifb07a822018-01-19 19:14:53 +0300524 /* Cache last net. After we unlock rtnl, no one new net
525 * added to net_namespace_list can assign nsid pointer
526 * to a net from net_kill_list (see peernet2id_alloc()).
527 * So, we skip them in unhash_nsid().
528 *
529 * Note, that unhash_nsid() does not delete nsid links
530 * between net_kill_list's nets, as they've already
531 * deleted from net_namespace_list. But, this would be
532 * useless anyway, as netns_ids are destroyed there.
533 */
534 last = list_last_entry(&net_namespace_list, struct net, list);
Kirill Tkhaif0b07bb12018-03-29 19:20:32 +0300535 up_write(&net_rwsem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200536
Kirill Tkhai65b7b5b2018-02-19 12:58:45 +0300537 llist_for_each_entry(net, net_kill_list, cleanup_list) {
Kirill Tkhaifb07a822018-01-19 19:14:53 +0300538 unhash_nsid(net, last);
539 list_add_tail(&net->exit_list, &net_exit_list);
540 }
541
Johannes Berg11a28d32009-07-10 09:51:33 +0000542 /*
543 * Another CPU might be rcu-iterating the list, wait for it.
544 * This needs to be before calling the exit() notifiers, so
545 * the rcu_barrier() below isn't sufficient alone.
546 */
547 synchronize_rcu();
548
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200549 /* Run all of the network namespace exit methods */
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000550 list_for_each_entry_reverse(ops, &pernet_list, list)
551 ops_exit_list(ops, &net_exit_list);
552
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000553 /* Free the net generic variables */
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000554 list_for_each_entry_reverse(ops, &pernet_list, list)
555 ops_free_list(ops, &net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200556
Kirill Tkhai4420bf22018-03-27 18:02:23 +0300557 up_read(&pernet_ops_rwsem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200558
559 /* Ensure there are no outstanding rcu callbacks using this
560 * network namespace.
561 */
562 rcu_barrier();
563
564 /* Finally it is safe to free my network namespace structure */
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000565 list_for_each_entry_safe(net, tmp, &net_exit_list, exit_list) {
566 list_del_init(&net->exit_list);
Eric W. Biederman70328662016-08-08 14:33:23 -0500567 dec_net_namespaces(net->ucounts);
Eric W. Biederman038e7332012-06-14 02:31:10 -0700568 put_user_ns(net->user_ns);
Al Viroa685e082011-06-08 21:13:01 -0400569 net_drop_ns(net);
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000570 }
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200571}
Florian Westphal7866cc52017-05-30 11:38:12 +0200572
573/**
574 * net_ns_barrier - wait until concurrent net_cleanup_work is done
575 *
576 * cleanup_net runs from work queue and will first remove namespaces
577 * from the global list, then run net exit functions.
578 *
579 * Call this in module exit path to make sure that all netns
580 * ->exit ops have been invoked before the function is removed.
581 */
582void net_ns_barrier(void)
583{
Kirill Tkhai4420bf22018-03-27 18:02:23 +0300584 down_write(&pernet_ops_rwsem);
585 up_write(&pernet_ops_rwsem);
Florian Westphal7866cc52017-05-30 11:38:12 +0200586}
587EXPORT_SYMBOL(net_ns_barrier);
588
Eric W. Biederman2b035b32009-11-29 22:25:27 +0000589static DECLARE_WORK(net_cleanup_work, cleanup_net);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200590
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200591void __put_net(struct net *net)
592{
593 /* Cleanup the network namespace in process context */
Kirill Tkhai8349efd2018-02-19 12:58:54 +0300594 if (llist_add(&net->cleanup_list, &cleanup_list))
595 queue_work(netns_wq, &net_cleanup_work);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200596}
597EXPORT_SYMBOL_GPL(__put_net);
598
Stephen Rothwell956c9202011-05-12 13:51:13 +1000599struct net *get_net_ns_by_fd(int fd)
600{
Stephen Rothwell956c9202011-05-12 13:51:13 +1000601 struct file *file;
Al Viro33c42942014-11-01 02:32:53 -0400602 struct ns_common *ns;
Stephen Rothwell956c9202011-05-12 13:51:13 +1000603 struct net *net;
604
Stephen Rothwell956c9202011-05-12 13:51:13 +1000605 file = proc_ns_fget(fd);
Al Viroc316e6a2011-06-05 00:37:35 +0000606 if (IS_ERR(file))
607 return ERR_CAST(file);
Stephen Rothwell956c9202011-05-12 13:51:13 +1000608
Al Virof77c8012014-11-01 03:13:17 -0400609 ns = get_proc_ns(file_inode(file));
Al Viro33c42942014-11-01 02:32:53 -0400610 if (ns->ops == &netns_operations)
611 net = get_net(container_of(ns, struct net, ns));
Al Viroc316e6a2011-06-05 00:37:35 +0000612 else
613 net = ERR_PTR(-EINVAL);
Stephen Rothwell956c9202011-05-12 13:51:13 +1000614
Al Viroc316e6a2011-06-05 00:37:35 +0000615 fput(file);
Stephen Rothwell956c9202011-05-12 13:51:13 +1000616 return net;
617}
618
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700619#else
Stephen Rothwell956c9202011-05-12 13:51:13 +1000620struct net *get_net_ns_by_fd(int fd)
621{
622 return ERR_PTR(-EINVAL);
623}
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700624#endif
Vadim Kochan4b681c82015-01-12 16:34:05 +0200625EXPORT_SYMBOL_GPL(get_net_ns_by_fd);
Eric W. Biederman9dd776b2007-09-26 22:04:26 -0700626
Johannes Berg30ffee82009-07-10 09:51:35 +0000627struct net *get_net_ns_by_pid(pid_t pid)
628{
629 struct task_struct *tsk;
630 struct net *net;
631
632 /* Lookup the network namespace */
633 net = ERR_PTR(-ESRCH);
634 rcu_read_lock();
635 tsk = find_task_by_vpid(pid);
636 if (tsk) {
637 struct nsproxy *nsproxy;
Eric W. Biederman728dba32014-02-03 19:13:49 -0800638 task_lock(tsk);
639 nsproxy = tsk->nsproxy;
Johannes Berg30ffee82009-07-10 09:51:35 +0000640 if (nsproxy)
641 net = get_net(nsproxy->net_ns);
Eric W. Biederman728dba32014-02-03 19:13:49 -0800642 task_unlock(tsk);
Johannes Berg30ffee82009-07-10 09:51:35 +0000643 }
644 rcu_read_unlock();
645 return net;
646}
647EXPORT_SYMBOL_GPL(get_net_ns_by_pid);
648
Eric W. Biederman98f842e2011-06-15 10:21:48 -0700649static __net_init int net_ns_net_init(struct net *net)
650{
Al Viro33c42942014-11-01 02:32:53 -0400651#ifdef CONFIG_NET_NS
652 net->ns.ops = &netns_operations;
653#endif
Al Viro6344c432014-11-01 00:45:45 -0400654 return ns_alloc_inum(&net->ns);
Eric W. Biederman98f842e2011-06-15 10:21:48 -0700655}
656
657static __net_exit void net_ns_net_exit(struct net *net)
658{
Al Viro6344c432014-11-01 00:45:45 -0400659 ns_free_inum(&net->ns);
Eric W. Biederman98f842e2011-06-15 10:21:48 -0700660}
661
662static struct pernet_operations __net_initdata net_ns_ops = {
663 .init = net_ns_net_init,
664 .exit = net_ns_net_exit,
665};
666
stephen hemminger3ee52562016-08-31 15:17:49 -0700667static const struct nla_policy rtnl_net_policy[NETNSA_MAX + 1] = {
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100668 [NETNSA_NONE] = { .type = NLA_UNSPEC },
669 [NETNSA_NSID] = { .type = NLA_S32 },
670 [NETNSA_PID] = { .type = NLA_U32 },
671 [NETNSA_FD] = { .type = NLA_U32 },
672};
673
David Ahernc21ef3e2017-04-16 09:48:24 -0700674static int rtnl_net_newid(struct sk_buff *skb, struct nlmsghdr *nlh,
675 struct netlink_ext_ack *extack)
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100676{
677 struct net *net = sock_net(skb->sk);
678 struct nlattr *tb[NETNSA_MAX + 1];
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200679 struct nlattr *nla;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100680 struct net *peer;
681 int nsid, err;
682
683 err = nlmsg_parse(nlh, sizeof(struct rtgenmsg), tb, NETNSA_MAX,
David Ahernc21ef3e2017-04-16 09:48:24 -0700684 rtnl_net_policy, extack);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100685 if (err < 0)
686 return err;
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200687 if (!tb[NETNSA_NSID]) {
688 NL_SET_ERR_MSG(extack, "nsid is missing");
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100689 return -EINVAL;
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200690 }
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100691 nsid = nla_get_s32(tb[NETNSA_NSID]);
692
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200693 if (tb[NETNSA_PID]) {
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100694 peer = get_net_ns_by_pid(nla_get_u32(tb[NETNSA_PID]));
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200695 nla = tb[NETNSA_PID];
696 } else if (tb[NETNSA_FD]) {
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100697 peer = get_net_ns_by_fd(nla_get_u32(tb[NETNSA_FD]));
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200698 nla = tb[NETNSA_FD];
699 } else {
700 NL_SET_ERR_MSG(extack, "Peer netns reference is missing");
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100701 return -EINVAL;
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200702 }
703 if (IS_ERR(peer)) {
704 NL_SET_BAD_ATTR(extack, nla);
705 NL_SET_ERR_MSG(extack, "Peer netns reference is invalid");
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100706 return PTR_ERR(peer);
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200707 }
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100708
Paul Moorefba143c2016-11-29 16:57:48 -0500709 spin_lock_bh(&net->nsid_lock);
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200710 if (__peernet2id(net, peer) >= 0) {
Paul Moorefba143c2016-11-29 16:57:48 -0500711 spin_unlock_bh(&net->nsid_lock);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100712 err = -EEXIST;
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200713 NL_SET_BAD_ATTR(extack, nla);
714 NL_SET_ERR_MSG(extack,
715 "Peer netns already has a nsid assigned");
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100716 goto out;
717 }
718
719 err = alloc_netid(net, peer, nsid);
Paul Moorefba143c2016-11-29 16:57:48 -0500720 spin_unlock_bh(&net->nsid_lock);
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200721 if (err >= 0) {
722 rtnl_net_notifyid(net, RTM_NEWNSID, err);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100723 err = 0;
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200724 } else if (err == -ENOSPC && nsid >= 0) {
Nicolas Dichtel10d486a2017-06-09 14:41:57 +0200725 err = -EEXIST;
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200726 NL_SET_BAD_ATTR(extack, tb[NETNSA_NSID]);
727 NL_SET_ERR_MSG(extack, "The specified nsid is already used");
Nicolas Dichtel3138dbf2015-05-07 11:02:50 +0200728 }
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100729out:
730 put_net(peer);
731 return err;
732}
733
734static int rtnl_net_get_size(void)
735{
736 return NLMSG_ALIGN(sizeof(struct rtgenmsg))
737 + nla_total_size(sizeof(s32)) /* NETNSA_NSID */
738 ;
739}
740
741static int rtnl_net_fill(struct sk_buff *skb, u32 portid, u32 seq, int flags,
Nicolas Dichtelcab3c8e2015-05-07 11:02:48 +0200742 int cmd, struct net *net, int nsid)
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100743{
744 struct nlmsghdr *nlh;
745 struct rtgenmsg *rth;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100746
747 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rth), flags);
748 if (!nlh)
749 return -EMSGSIZE;
750
751 rth = nlmsg_data(nlh);
752 rth->rtgen_family = AF_UNSPEC;
753
Nicolas Dichtelcab3c8e2015-05-07 11:02:48 +0200754 if (nla_put_s32(skb, NETNSA_NSID, nsid))
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100755 goto nla_put_failure;
756
757 nlmsg_end(skb, nlh);
758 return 0;
759
760nla_put_failure:
761 nlmsg_cancel(skb, nlh);
762 return -EMSGSIZE;
763}
764
David Ahernc21ef3e2017-04-16 09:48:24 -0700765static int rtnl_net_getid(struct sk_buff *skb, struct nlmsghdr *nlh,
766 struct netlink_ext_ack *extack)
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100767{
768 struct net *net = sock_net(skb->sk);
769 struct nlattr *tb[NETNSA_MAX + 1];
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200770 struct nlattr *nla;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100771 struct sk_buff *msg;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100772 struct net *peer;
Nicolas Dichtelcab3c8e2015-05-07 11:02:48 +0200773 int err, id;
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100774
775 err = nlmsg_parse(nlh, sizeof(struct rtgenmsg), tb, NETNSA_MAX,
David Ahernc21ef3e2017-04-16 09:48:24 -0700776 rtnl_net_policy, extack);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100777 if (err < 0)
778 return err;
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200779 if (tb[NETNSA_PID]) {
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100780 peer = get_net_ns_by_pid(nla_get_u32(tb[NETNSA_PID]));
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200781 nla = tb[NETNSA_PID];
782 } else if (tb[NETNSA_FD]) {
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100783 peer = get_net_ns_by_fd(nla_get_u32(tb[NETNSA_FD]));
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200784 nla = tb[NETNSA_FD];
785 } else {
786 NL_SET_ERR_MSG(extack, "Peer netns reference is missing");
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100787 return -EINVAL;
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200788 }
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100789
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200790 if (IS_ERR(peer)) {
791 NL_SET_BAD_ATTR(extack, nla);
792 NL_SET_ERR_MSG(extack, "Peer netns reference is invalid");
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100793 return PTR_ERR(peer);
Nicolas Dichtel4a7f7bc2017-06-09 14:41:56 +0200794 }
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100795
796 msg = nlmsg_new(rtnl_net_get_size(), GFP_KERNEL);
797 if (!msg) {
798 err = -ENOMEM;
799 goto out;
800 }
801
Nicolas Dichtel95f38412015-05-07 11:02:51 +0200802 id = peernet2id(net, peer);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100803 err = rtnl_net_fill(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
David S. Millerb04096f2015-05-13 14:31:43 -0400804 RTM_NEWNSID, net, id);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100805 if (err < 0)
806 goto err_out;
807
808 err = rtnl_unicast(msg, net, NETLINK_CB(skb).portid);
809 goto out;
810
811err_out:
812 nlmsg_free(msg);
813out:
814 put_net(peer);
815 return err;
816}
817
Nicolas Dichtela143c402015-04-07 11:51:54 +0200818struct rtnl_net_dump_cb {
819 struct net *net;
820 struct sk_buff *skb;
821 struct netlink_callback *cb;
822 int idx;
823 int s_idx;
824};
825
826static int rtnl_net_dumpid_one(int id, void *peer, void *data)
827{
828 struct rtnl_net_dump_cb *net_cb = (struct rtnl_net_dump_cb *)data;
829 int ret;
830
831 if (net_cb->idx < net_cb->s_idx)
832 goto cont;
833
834 ret = rtnl_net_fill(net_cb->skb, NETLINK_CB(net_cb->cb->skb).portid,
835 net_cb->cb->nlh->nlmsg_seq, NLM_F_MULTI,
Nicolas Dichtelcab3c8e2015-05-07 11:02:48 +0200836 RTM_NEWNSID, net_cb->net, id);
Nicolas Dichtela143c402015-04-07 11:51:54 +0200837 if (ret < 0)
838 return ret;
839
840cont:
841 net_cb->idx++;
842 return 0;
843}
844
845static int rtnl_net_dumpid(struct sk_buff *skb, struct netlink_callback *cb)
846{
847 struct net *net = sock_net(skb->sk);
848 struct rtnl_net_dump_cb net_cb = {
849 .net = net,
850 .skb = skb,
851 .cb = cb,
852 .idx = 0,
853 .s_idx = cb->args[0],
854 };
855
Paul Moorefba143c2016-11-29 16:57:48 -0500856 spin_lock_bh(&net->nsid_lock);
Nicolas Dichtela143c402015-04-07 11:51:54 +0200857 idr_for_each(&net->netns_ids, rtnl_net_dumpid_one, &net_cb);
Paul Moorefba143c2016-11-29 16:57:48 -0500858 spin_unlock_bh(&net->nsid_lock);
Nicolas Dichtela143c402015-04-07 11:51:54 +0200859
860 cb->args[0] = net_cb.idx;
861 return skb->len;
862}
863
Nicolas Dichtelcab3c8e2015-05-07 11:02:48 +0200864static void rtnl_net_notifyid(struct net *net, int cmd, int id)
Nicolas Dichtel9a963452015-04-07 11:51:53 +0200865{
866 struct sk_buff *msg;
867 int err = -ENOMEM;
868
869 msg = nlmsg_new(rtnl_net_get_size(), GFP_KERNEL);
870 if (!msg)
871 goto out;
872
Nicolas Dichtelcab3c8e2015-05-07 11:02:48 +0200873 err = rtnl_net_fill(msg, 0, 0, 0, cmd, net, id);
Nicolas Dichtel9a963452015-04-07 11:51:53 +0200874 if (err < 0)
875 goto err_out;
876
877 rtnl_notify(msg, net, 0, RTNLGRP_NSID, NULL, 0);
878 return;
879
880err_out:
881 nlmsg_free(msg);
882out:
883 rtnl_set_sk_err(net, RTNLGRP_NSID, err);
884}
885
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200886static int __init net_ns_init(void)
887{
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800888 struct net_generic *ng;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200889
Pavel Emelyanovd57a9212007-11-01 00:46:50 -0700890#ifdef CONFIG_NET_NS
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200891 net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
892 SMP_CACHE_BYTES,
Kirill Tkhai30855ff2018-03-01 15:23:28 +0300893 SLAB_PANIC|SLAB_ACCOUNT, NULL);
Benjamin Thery3ef13552007-11-19 23:18:16 -0800894
895 /* Create workqueue for cleanup */
896 netns_wq = create_singlethread_workqueue("netns");
897 if (!netns_wq)
898 panic("Could not create netns workq");
Pavel Emelyanovd57a9212007-11-01 00:46:50 -0700899#endif
Benjamin Thery3ef13552007-11-19 23:18:16 -0800900
Daniel Lezcano486a87f2009-02-22 00:07:53 -0800901 ng = net_alloc_generic();
902 if (!ng)
903 panic("Could not allocate generic netns");
904
905 rcu_assign_pointer(init_net.gen, ng);
906
Kirill Tkhai4420bf22018-03-27 18:02:23 +0300907 down_write(&pernet_ops_rwsem);
Eric W. Biederman038e7332012-06-14 02:31:10 -0700908 if (setup_net(&init_net, &init_user_ns))
Stephen Hemmingerca0f3112009-05-21 15:10:31 -0700909 panic("Could not setup the initial network namespace");
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200910
Dmitry Torokhovf8c46cb2016-08-10 14:36:00 -0700911 init_net_initialized = true;
Kirill Tkhai4420bf22018-03-27 18:02:23 +0300912 up_write(&pernet_ops_rwsem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200913
Eric W. Biederman98f842e2011-06-15 10:21:48 -0700914 register_pernet_subsys(&net_ns_ops);
915
Florian Westphal165b9112017-08-09 20:41:53 +0200916 rtnl_register(PF_UNSPEC, RTM_NEWNSID, rtnl_net_newid, NULL,
917 RTNL_FLAG_DOIT_UNLOCKED);
Nicolas Dichtela143c402015-04-07 11:51:54 +0200918 rtnl_register(PF_UNSPEC, RTM_GETNSID, rtnl_net_getid, rtnl_net_dumpid,
Florian Westphal165b9112017-08-09 20:41:53 +0200919 RTNL_FLAG_DOIT_UNLOCKED);
Nicolas Dichtel0c7aecd2015-01-15 15:11:15 +0100920
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200921 return 0;
922}
923
924pure_initcall(net_ns_init);
925
Denis V. Luneved160e82007-11-13 03:23:21 -0800926#ifdef CONFIG_NET_NS
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000927static int __register_pernet_operations(struct list_head *list,
928 struct pernet_operations *ops)
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200929{
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000930 struct net *net;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200931 int error;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000932 LIST_HEAD(net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200933
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200934 list_add_tail(&ops->list, list);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000935 if (ops->init || (ops->id && ops->size)) {
Kirill Tkhaif0b07bb12018-03-29 19:20:32 +0300936 /* We held write locked pernet_ops_rwsem, and parallel
937 * setup_net() and cleanup_net() are not possible.
938 */
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700939 for_each_net(net) {
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000940 error = ops_init(ops, net);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200941 if (error)
942 goto out_undo;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000943 list_add_tail(&net->exit_list, &net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200944 }
945 }
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700946 return 0;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200947
948out_undo:
949 /* If I have an error cleanup all namespaces I initialized */
950 list_del(&ops->list);
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000951 ops_exit_list(ops, &net_exit_list);
952 ops_free_list(ops, &net_exit_list);
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700953 return error;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200954}
955
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000956static void __unregister_pernet_operations(struct pernet_operations *ops)
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200957{
958 struct net *net;
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000959 LIST_HEAD(net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200960
961 list_del(&ops->list);
Kirill Tkhaif0b07bb12018-03-29 19:20:32 +0300962 /* See comment in __register_pernet_operations() */
Eric W. Biederman72ad9372009-12-03 02:29:03 +0000963 for_each_net(net)
964 list_add_tail(&net->exit_list, &net_exit_list);
965 ops_exit_list(ops, &net_exit_list);
966 ops_free_list(ops, &net_exit_list);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200967}
968
Denis V. Luneved160e82007-11-13 03:23:21 -0800969#else
970
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000971static int __register_pernet_operations(struct list_head *list,
972 struct pernet_operations *ops)
973{
Dmitry Torokhovf8c46cb2016-08-10 14:36:00 -0700974 if (!init_net_initialized) {
975 list_add_tail(&ops->list, list);
976 return 0;
977 }
978
Julian Anastasovb9229342012-04-16 04:43:15 +0000979 return ops_init(ops, &init_net);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000980}
981
982static void __unregister_pernet_operations(struct pernet_operations *ops)
983{
Dmitry Torokhovf8c46cb2016-08-10 14:36:00 -0700984 if (!init_net_initialized) {
985 list_del(&ops->list);
986 } else {
987 LIST_HEAD(net_exit_list);
988 list_add(&init_net.exit_list, &net_exit_list);
989 ops_exit_list(ops, &net_exit_list);
990 ops_free_list(ops, &net_exit_list);
991 }
Eric W. Biedermanf875bae2009-11-29 22:25:28 +0000992}
993
994#endif /* CONFIG_NET_NS */
995
996static DEFINE_IDA(net_generic_ids);
997
Denis V. Luneved160e82007-11-13 03:23:21 -0800998static int register_pernet_operations(struct list_head *list,
999 struct pernet_operations *ops)
1000{
Eric W. Biedermanf875bae2009-11-29 22:25:28 +00001001 int error;
1002
1003 if (ops->id) {
1004again:
Alexey Dobriyan6af2d5f2016-12-02 04:21:32 +03001005 error = ida_get_new_above(&net_generic_ids, MIN_PERNET_OPS_ID, ops->id);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +00001006 if (error < 0) {
1007 if (error == -EAGAIN) {
1008 ida_pre_get(&net_generic_ids, GFP_KERNEL);
1009 goto again;
1010 }
1011 return error;
1012 }
Alexey Dobriyan6af2d5f2016-12-02 04:21:32 +03001013 max_gen_ptrs = max(max_gen_ptrs, *ops->id + 1);
Eric W. Biedermanf875bae2009-11-29 22:25:28 +00001014 }
1015 error = __register_pernet_operations(list, ops);
Eric W. Biederman3a765ed2009-12-03 02:29:06 +00001016 if (error) {
1017 rcu_barrier();
1018 if (ops->id)
1019 ida_remove(&net_generic_ids, *ops->id);
1020 }
Eric W. Biedermanf875bae2009-11-29 22:25:28 +00001021
1022 return error;
Denis V. Luneved160e82007-11-13 03:23:21 -08001023}
1024
1025static void unregister_pernet_operations(struct pernet_operations *ops)
1026{
Eric W. Biedermanf875bae2009-11-29 22:25:28 +00001027 __unregister_pernet_operations(ops);
Eric W. Biederman3a765ed2009-12-03 02:29:06 +00001028 rcu_barrier();
Eric W. Biedermanf875bae2009-11-29 22:25:28 +00001029 if (ops->id)
1030 ida_remove(&net_generic_ids, *ops->id);
Denis V. Luneved160e82007-11-13 03:23:21 -08001031}
Pavel Emelyanovc93cf612008-04-15 00:35:23 -07001032
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001033/**
1034 * register_pernet_subsys - register a network namespace subsystem
1035 * @ops: pernet operations structure for the subsystem
1036 *
1037 * Register a subsystem which has init and exit functions
1038 * that are called when network namespaces are created and
1039 * destroyed respectively.
1040 *
1041 * When registered all network namespace init functions are
1042 * called for every existing network namespace. Allowing kernel
1043 * modules to have a race free view of the set of network namespaces.
1044 *
1045 * When a new network namespace is created all of the init
1046 * methods are called in the order in which they were registered.
1047 *
1048 * When a network namespace is destroyed all of the exit methods
1049 * are called in the reverse of the order with which they were
1050 * registered.
1051 */
1052int register_pernet_subsys(struct pernet_operations *ops)
1053{
1054 int error;
Kirill Tkhai4420bf22018-03-27 18:02:23 +03001055 down_write(&pernet_ops_rwsem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001056 error = register_pernet_operations(first_device, ops);
Kirill Tkhai4420bf22018-03-27 18:02:23 +03001057 up_write(&pernet_ops_rwsem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001058 return error;
1059}
1060EXPORT_SYMBOL_GPL(register_pernet_subsys);
1061
1062/**
1063 * unregister_pernet_subsys - unregister a network namespace subsystem
1064 * @ops: pernet operations structure to manipulate
1065 *
1066 * Remove the pernet operations structure from the list to be
Oliver Pinter53379e52008-02-03 17:56:48 +02001067 * used when network namespaces are created or destroyed. In
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001068 * addition run the exit method for all existing network
1069 * namespaces.
1070 */
Jiri Pirkob3c981d2010-04-25 00:49:56 -07001071void unregister_pernet_subsys(struct pernet_operations *ops)
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001072{
Kirill Tkhai4420bf22018-03-27 18:02:23 +03001073 down_write(&pernet_ops_rwsem);
Jiri Pirkob3c981d2010-04-25 00:49:56 -07001074 unregister_pernet_operations(ops);
Kirill Tkhai4420bf22018-03-27 18:02:23 +03001075 up_write(&pernet_ops_rwsem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001076}
1077EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
1078
1079/**
1080 * register_pernet_device - register a network namespace device
1081 * @ops: pernet operations structure for the subsystem
1082 *
1083 * Register a device which has init and exit functions
1084 * that are called when network namespaces are created and
1085 * destroyed respectively.
1086 *
1087 * When registered all network namespace init functions are
1088 * called for every existing network namespace. Allowing kernel
1089 * modules to have a race free view of the set of network namespaces.
1090 *
1091 * When a new network namespace is created all of the init
1092 * methods are called in the order in which they were registered.
1093 *
1094 * When a network namespace is destroyed all of the exit methods
1095 * are called in the reverse of the order with which they were
1096 * registered.
1097 */
1098int register_pernet_device(struct pernet_operations *ops)
1099{
1100 int error;
Kirill Tkhai4420bf22018-03-27 18:02:23 +03001101 down_write(&pernet_ops_rwsem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001102 error = register_pernet_operations(&pernet_list, ops);
1103 if (!error && (first_device == &pernet_list))
1104 first_device = &ops->list;
Kirill Tkhai4420bf22018-03-27 18:02:23 +03001105 up_write(&pernet_ops_rwsem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001106 return error;
1107}
1108EXPORT_SYMBOL_GPL(register_pernet_device);
1109
1110/**
1111 * unregister_pernet_device - unregister a network namespace netdevice
1112 * @ops: pernet operations structure to manipulate
1113 *
1114 * Remove the pernet operations structure from the list to be
Oliver Pinter53379e52008-02-03 17:56:48 +02001115 * used when network namespaces are created or destroyed. In
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001116 * addition run the exit method for all existing network
1117 * namespaces.
1118 */
1119void unregister_pernet_device(struct pernet_operations *ops)
1120{
Kirill Tkhai4420bf22018-03-27 18:02:23 +03001121 down_write(&pernet_ops_rwsem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001122 if (&ops->list == first_device)
1123 first_device = first_device->next;
1124 unregister_pernet_operations(ops);
Kirill Tkhai4420bf22018-03-27 18:02:23 +03001125 up_write(&pernet_ops_rwsem);
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001126}
1127EXPORT_SYMBOL_GPL(unregister_pernet_device);
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001128
1129#ifdef CONFIG_NET_NS
Al Viro64964522014-11-01 00:37:32 -04001130static struct ns_common *netns_get(struct task_struct *task)
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001131{
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001132 struct net *net = NULL;
1133 struct nsproxy *nsproxy;
1134
Eric W. Biederman728dba32014-02-03 19:13:49 -08001135 task_lock(task);
1136 nsproxy = task->nsproxy;
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001137 if (nsproxy)
1138 net = get_net(nsproxy->net_ns);
Eric W. Biederman728dba32014-02-03 19:13:49 -08001139 task_unlock(task);
Eric W. Biedermanf0630522011-05-04 17:51:50 -07001140
Al Viroff248702014-11-01 00:10:50 -04001141 return net ? &net->ns : NULL;
1142}
1143
1144static inline struct net *to_net_ns(struct ns_common *ns)
1145{
1146 return container_of(ns, struct net, ns);
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001147}
1148
Al Viro64964522014-11-01 00:37:32 -04001149static void netns_put(struct ns_common *ns)
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001150{
Al Viroff248702014-11-01 00:10:50 -04001151 put_net(to_net_ns(ns));
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001152}
1153
Al Viro64964522014-11-01 00:37:32 -04001154static int netns_install(struct nsproxy *nsproxy, struct ns_common *ns)
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001155{
Al Viroff248702014-11-01 00:10:50 -04001156 struct net *net = to_net_ns(ns);
Eric W. Biederman142e1d12012-07-26 01:13:20 -07001157
Eric W. Biederman5e4a0842012-12-14 07:55:36 -08001158 if (!ns_capable(net->user_ns, CAP_SYS_ADMIN) ||
Eric W. Biedermanc7b96ac2013-03-20 12:49:49 -07001159 !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
Eric W. Biederman142e1d12012-07-26 01:13:20 -07001160 return -EPERM;
1161
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001162 put_net(nsproxy->net_ns);
Eric W. Biederman142e1d12012-07-26 01:13:20 -07001163 nsproxy->net_ns = get_net(net);
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001164 return 0;
1165}
1166
Andrey Vaginbcac25a2016-09-06 00:47:13 -07001167static struct user_namespace *netns_owner(struct ns_common *ns)
1168{
1169 return to_net_ns(ns)->user_ns;
1170}
1171
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001172const struct proc_ns_operations netns_operations = {
1173 .name = "net",
1174 .type = CLONE_NEWNET,
1175 .get = netns_get,
1176 .put = netns_put,
1177 .install = netns_install,
Andrey Vaginbcac25a2016-09-06 00:47:13 -07001178 .owner = netns_owner,
Eric W. Biederman13b6f572010-03-07 18:14:23 -08001179};
1180#endif