xref: /openbmc/linux/net/core/net_namespace.c (revision 9b242610)
1 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2 
3 #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>
9 #include <linux/sched.h>
10 #include <linux/idr.h>
11 #include <linux/rculist.h>
12 #include <linux/nsproxy.h>
13 #include <linux/fs.h>
14 #include <linux/proc_ns.h>
15 #include <linux/file.h>
16 #include <linux/export.h>
17 #include <linux/user_namespace.h>
18 #include <linux/net_namespace.h>
19 #include <linux/sched/task.h>
20 #include <linux/uidgid.h>
21 
22 #include <net/sock.h>
23 #include <net/netlink.h>
24 #include <net/net_namespace.h>
25 #include <net/netns/generic.h>
26 
27 /*
28  *	Our network namespace constructor/destructor lists
29  */
30 
31 static LIST_HEAD(pernet_list);
32 static struct list_head *first_device = &pernet_list;
33 
34 LIST_HEAD(net_namespace_list);
35 EXPORT_SYMBOL_GPL(net_namespace_list);
36 
37 /* Protects net_namespace_list. Nests iside rtnl_lock() */
38 DECLARE_RWSEM(net_rwsem);
39 EXPORT_SYMBOL_GPL(net_rwsem);
40 
41 #ifdef CONFIG_KEYS
42 static struct key_tag init_net_key_domain = { .usage = REFCOUNT_INIT(1) };
43 #endif
44 
45 struct net init_net = {
46 	.count		= REFCOUNT_INIT(1),
47 	.dev_base_head	= LIST_HEAD_INIT(init_net.dev_base_head),
48 #ifdef CONFIG_KEYS
49 	.key_domain	= &init_net_key_domain,
50 #endif
51 };
52 EXPORT_SYMBOL(init_net);
53 
54 static bool init_net_initialized;
55 /*
56  * pernet_ops_rwsem: protects: pernet_list, net_generic_ids,
57  * init_net_initialized and first_device pointer.
58  * This is internal net namespace object. Please, don't use it
59  * outside.
60  */
61 DECLARE_RWSEM(pernet_ops_rwsem);
62 EXPORT_SYMBOL_GPL(pernet_ops_rwsem);
63 
64 #define MIN_PERNET_OPS_ID	\
65 	((sizeof(struct net_generic) + sizeof(void *) - 1) / sizeof(void *))
66 
67 #define INITIAL_NET_GEN_PTRS	13 /* +1 for len +2 for rcu_head */
68 
69 static unsigned int max_gen_ptrs = INITIAL_NET_GEN_PTRS;
70 
71 static struct net_generic *net_alloc_generic(void)
72 {
73 	struct net_generic *ng;
74 	unsigned int generic_size = offsetof(struct net_generic, ptr[max_gen_ptrs]);
75 
76 	ng = kzalloc(generic_size, GFP_KERNEL);
77 	if (ng)
78 		ng->s.len = max_gen_ptrs;
79 
80 	return ng;
81 }
82 
83 static int net_assign_generic(struct net *net, unsigned int id, void *data)
84 {
85 	struct net_generic *ng, *old_ng;
86 
87 	BUG_ON(id < MIN_PERNET_OPS_ID);
88 
89 	old_ng = rcu_dereference_protected(net->gen,
90 					   lockdep_is_held(&pernet_ops_rwsem));
91 	if (old_ng->s.len > id) {
92 		old_ng->ptr[id] = data;
93 		return 0;
94 	}
95 
96 	ng = net_alloc_generic();
97 	if (ng == NULL)
98 		return -ENOMEM;
99 
100 	/*
101 	 * Some synchronisation notes:
102 	 *
103 	 * The net_generic explores the net->gen array inside rcu
104 	 * read section. Besides once set the net->gen->ptr[x]
105 	 * pointer never changes (see rules in netns/generic.h).
106 	 *
107 	 * That said, we simply duplicate this array and schedule
108 	 * the old copy for kfree after a grace period.
109 	 */
110 
111 	memcpy(&ng->ptr[MIN_PERNET_OPS_ID], &old_ng->ptr[MIN_PERNET_OPS_ID],
112 	       (old_ng->s.len - MIN_PERNET_OPS_ID) * sizeof(void *));
113 	ng->ptr[id] = data;
114 
115 	rcu_assign_pointer(net->gen, ng);
116 	kfree_rcu(old_ng, s.rcu);
117 	return 0;
118 }
119 
120 static int ops_init(const struct pernet_operations *ops, struct net *net)
121 {
122 	int err = -ENOMEM;
123 	void *data = NULL;
124 
125 	if (ops->id && ops->size) {
126 		data = kzalloc(ops->size, GFP_KERNEL);
127 		if (!data)
128 			goto out;
129 
130 		err = net_assign_generic(net, *ops->id, data);
131 		if (err)
132 			goto cleanup;
133 	}
134 	err = 0;
135 	if (ops->init)
136 		err = ops->init(net);
137 	if (!err)
138 		return 0;
139 
140 cleanup:
141 	kfree(data);
142 
143 out:
144 	return err;
145 }
146 
147 static void ops_free(const struct pernet_operations *ops, struct net *net)
148 {
149 	if (ops->id && ops->size) {
150 		kfree(net_generic(net, *ops->id));
151 	}
152 }
153 
154 static void ops_exit_list(const struct pernet_operations *ops,
155 			  struct list_head *net_exit_list)
156 {
157 	struct net *net;
158 	if (ops->exit) {
159 		list_for_each_entry(net, net_exit_list, exit_list)
160 			ops->exit(net);
161 	}
162 	if (ops->exit_batch)
163 		ops->exit_batch(net_exit_list);
164 }
165 
166 static void ops_free_list(const struct pernet_operations *ops,
167 			  struct list_head *net_exit_list)
168 {
169 	struct net *net;
170 	if (ops->size && ops->id) {
171 		list_for_each_entry(net, net_exit_list, exit_list)
172 			ops_free(ops, net);
173 	}
174 }
175 
176 /* should be called with nsid_lock held */
177 static int alloc_netid(struct net *net, struct net *peer, int reqid)
178 {
179 	int min = 0, max = 0;
180 
181 	if (reqid >= 0) {
182 		min = reqid;
183 		max = reqid + 1;
184 	}
185 
186 	return idr_alloc(&net->netns_ids, peer, min, max, GFP_ATOMIC);
187 }
188 
189 /* This function is used by idr_for_each(). If net is equal to peer, the
190  * function returns the id so that idr_for_each() stops. Because we cannot
191  * returns the id 0 (idr_for_each() will not stop), we return the magic value
192  * NET_ID_ZERO (-1) for it.
193  */
194 #define NET_ID_ZERO -1
195 static int net_eq_idr(int id, void *net, void *peer)
196 {
197 	if (net_eq(net, peer))
198 		return id ? : NET_ID_ZERO;
199 	return 0;
200 }
201 
202 /* Should be called with nsid_lock held. If a new id is assigned, the bool alloc
203  * is set to true, thus the caller knows that the new id must be notified via
204  * rtnl.
205  */
206 static int __peernet2id_alloc(struct net *net, struct net *peer, bool *alloc)
207 {
208 	int id = idr_for_each(&net->netns_ids, net_eq_idr, peer);
209 	bool alloc_it = *alloc;
210 
211 	*alloc = false;
212 
213 	/* Magic value for id 0. */
214 	if (id == NET_ID_ZERO)
215 		return 0;
216 	if (id > 0)
217 		return id;
218 
219 	if (alloc_it) {
220 		id = alloc_netid(net, peer, -1);
221 		*alloc = true;
222 		return id >= 0 ? id : NETNSA_NSID_NOT_ASSIGNED;
223 	}
224 
225 	return NETNSA_NSID_NOT_ASSIGNED;
226 }
227 
228 /* should be called with nsid_lock held */
229 static int __peernet2id(struct net *net, struct net *peer)
230 {
231 	bool no = false;
232 
233 	return __peernet2id_alloc(net, peer, &no);
234 }
235 
236 static void rtnl_net_notifyid(struct net *net, int cmd, int id);
237 /* This function returns the id of a peer netns. If no id is assigned, one will
238  * be allocated and returned.
239  */
240 int peernet2id_alloc(struct net *net, struct net *peer)
241 {
242 	bool alloc = false, alive = false;
243 	int id;
244 
245 	if (refcount_read(&net->count) == 0)
246 		return NETNSA_NSID_NOT_ASSIGNED;
247 	spin_lock_bh(&net->nsid_lock);
248 	/*
249 	 * When peer is obtained from RCU lists, we may race with
250 	 * its cleanup. Check whether it's alive, and this guarantees
251 	 * we never hash a peer back to net->netns_ids, after it has
252 	 * just been idr_remove()'d from there in cleanup_net().
253 	 */
254 	if (maybe_get_net(peer))
255 		alive = alloc = true;
256 	id = __peernet2id_alloc(net, peer, &alloc);
257 	spin_unlock_bh(&net->nsid_lock);
258 	if (alloc && id >= 0)
259 		rtnl_net_notifyid(net, RTM_NEWNSID, id);
260 	if (alive)
261 		put_net(peer);
262 	return id;
263 }
264 EXPORT_SYMBOL_GPL(peernet2id_alloc);
265 
266 /* This function returns, if assigned, the id of a peer netns. */
267 int peernet2id(struct net *net, struct net *peer)
268 {
269 	int id;
270 
271 	spin_lock_bh(&net->nsid_lock);
272 	id = __peernet2id(net, peer);
273 	spin_unlock_bh(&net->nsid_lock);
274 	return id;
275 }
276 EXPORT_SYMBOL(peernet2id);
277 
278 /* This function returns true is the peer netns has an id assigned into the
279  * current netns.
280  */
281 bool peernet_has_id(struct net *net, struct net *peer)
282 {
283 	return peernet2id(net, peer) >= 0;
284 }
285 
286 struct net *get_net_ns_by_id(struct net *net, int id)
287 {
288 	struct net *peer;
289 
290 	if (id < 0)
291 		return NULL;
292 
293 	rcu_read_lock();
294 	peer = idr_find(&net->netns_ids, id);
295 	if (peer)
296 		peer = maybe_get_net(peer);
297 	rcu_read_unlock();
298 
299 	return peer;
300 }
301 
302 /*
303  * setup_net runs the initializers for the network namespace object.
304  */
305 static __net_init int setup_net(struct net *net, struct user_namespace *user_ns)
306 {
307 	/* Must be called with pernet_ops_rwsem held */
308 	const struct pernet_operations *ops, *saved_ops;
309 	int error = 0;
310 	LIST_HEAD(net_exit_list);
311 
312 	refcount_set(&net->count, 1);
313 	refcount_set(&net->passive, 1);
314 	get_random_bytes(&net->hash_mix, sizeof(u32));
315 	net->dev_base_seq = 1;
316 	net->user_ns = user_ns;
317 	idr_init(&net->netns_ids);
318 	spin_lock_init(&net->nsid_lock);
319 	mutex_init(&net->ipv4.ra_mutex);
320 
321 	list_for_each_entry(ops, &pernet_list, list) {
322 		error = ops_init(ops, net);
323 		if (error < 0)
324 			goto out_undo;
325 	}
326 	down_write(&net_rwsem);
327 	list_add_tail_rcu(&net->list, &net_namespace_list);
328 	up_write(&net_rwsem);
329 out:
330 	return error;
331 
332 out_undo:
333 	/* Walk through the list backwards calling the exit functions
334 	 * for the pernet modules whose init functions did not fail.
335 	 */
336 	list_add(&net->exit_list, &net_exit_list);
337 	saved_ops = ops;
338 	list_for_each_entry_continue_reverse(ops, &pernet_list, list)
339 		ops_exit_list(ops, &net_exit_list);
340 
341 	ops = saved_ops;
342 	list_for_each_entry_continue_reverse(ops, &pernet_list, list)
343 		ops_free_list(ops, &net_exit_list);
344 
345 	rcu_barrier();
346 	goto out;
347 }
348 
349 static int __net_init net_defaults_init_net(struct net *net)
350 {
351 	net->core.sysctl_somaxconn = SOMAXCONN;
352 	return 0;
353 }
354 
355 static struct pernet_operations net_defaults_ops = {
356 	.init = net_defaults_init_net,
357 };
358 
359 static __init int net_defaults_init(void)
360 {
361 	if (register_pernet_subsys(&net_defaults_ops))
362 		panic("Cannot initialize net default settings");
363 
364 	return 0;
365 }
366 
367 core_initcall(net_defaults_init);
368 
369 #ifdef CONFIG_NET_NS
370 static struct ucounts *inc_net_namespaces(struct user_namespace *ns)
371 {
372 	return inc_ucount(ns, current_euid(), UCOUNT_NET_NAMESPACES);
373 }
374 
375 static void dec_net_namespaces(struct ucounts *ucounts)
376 {
377 	dec_ucount(ucounts, UCOUNT_NET_NAMESPACES);
378 }
379 
380 static struct kmem_cache *net_cachep __ro_after_init;
381 static struct workqueue_struct *netns_wq;
382 
383 static struct net *net_alloc(void)
384 {
385 	struct net *net = NULL;
386 	struct net_generic *ng;
387 
388 	ng = net_alloc_generic();
389 	if (!ng)
390 		goto out;
391 
392 	net = kmem_cache_zalloc(net_cachep, GFP_KERNEL);
393 	if (!net)
394 		goto out_free;
395 
396 #ifdef CONFIG_KEYS
397 	net->key_domain = kzalloc(sizeof(struct key_tag), GFP_KERNEL);
398 	if (!net->key_domain)
399 		goto out_free_2;
400 	refcount_set(&net->key_domain->usage, 1);
401 #endif
402 
403 	rcu_assign_pointer(net->gen, ng);
404 out:
405 	return net;
406 
407 #ifdef CONFIG_KEYS
408 out_free_2:
409 	kmem_cache_free(net_cachep, net);
410 	net = NULL;
411 #endif
412 out_free:
413 	kfree(ng);
414 	goto out;
415 }
416 
417 static void net_free(struct net *net)
418 {
419 	kfree(rcu_access_pointer(net->gen));
420 	kmem_cache_free(net_cachep, net);
421 }
422 
423 void net_drop_ns(void *p)
424 {
425 	struct net *ns = p;
426 	if (ns && refcount_dec_and_test(&ns->passive))
427 		net_free(ns);
428 }
429 
430 struct net *copy_net_ns(unsigned long flags,
431 			struct user_namespace *user_ns, struct net *old_net)
432 {
433 	struct ucounts *ucounts;
434 	struct net *net;
435 	int rv;
436 
437 	if (!(flags & CLONE_NEWNET))
438 		return get_net(old_net);
439 
440 	ucounts = inc_net_namespaces(user_ns);
441 	if (!ucounts)
442 		return ERR_PTR(-ENOSPC);
443 
444 	net = net_alloc();
445 	if (!net) {
446 		rv = -ENOMEM;
447 		goto dec_ucounts;
448 	}
449 	refcount_set(&net->passive, 1);
450 	net->ucounts = ucounts;
451 	get_user_ns(user_ns);
452 
453 	rv = down_read_killable(&pernet_ops_rwsem);
454 	if (rv < 0)
455 		goto put_userns;
456 
457 	rv = setup_net(net, user_ns);
458 
459 	up_read(&pernet_ops_rwsem);
460 
461 	if (rv < 0) {
462 put_userns:
463 		put_user_ns(user_ns);
464 		net_drop_ns(net);
465 dec_ucounts:
466 		dec_net_namespaces(ucounts);
467 		return ERR_PTR(rv);
468 	}
469 	return net;
470 }
471 
472 /**
473  * net_ns_get_ownership - get sysfs ownership data for @net
474  * @net: network namespace in question (can be NULL)
475  * @uid: kernel user ID for sysfs objects
476  * @gid: kernel group ID for sysfs objects
477  *
478  * Returns the uid/gid pair of root in the user namespace associated with the
479  * given network namespace.
480  */
481 void net_ns_get_ownership(const struct net *net, kuid_t *uid, kgid_t *gid)
482 {
483 	if (net) {
484 		kuid_t ns_root_uid = make_kuid(net->user_ns, 0);
485 		kgid_t ns_root_gid = make_kgid(net->user_ns, 0);
486 
487 		if (uid_valid(ns_root_uid))
488 			*uid = ns_root_uid;
489 
490 		if (gid_valid(ns_root_gid))
491 			*gid = ns_root_gid;
492 	} else {
493 		*uid = GLOBAL_ROOT_UID;
494 		*gid = GLOBAL_ROOT_GID;
495 	}
496 }
497 EXPORT_SYMBOL_GPL(net_ns_get_ownership);
498 
499 static void unhash_nsid(struct net *net, struct net *last)
500 {
501 	struct net *tmp;
502 	/* This function is only called from cleanup_net() work,
503 	 * and this work is the only process, that may delete
504 	 * a net from net_namespace_list. So, when the below
505 	 * is executing, the list may only grow. Thus, we do not
506 	 * use for_each_net_rcu() or net_rwsem.
507 	 */
508 	for_each_net(tmp) {
509 		int id;
510 
511 		spin_lock_bh(&tmp->nsid_lock);
512 		id = __peernet2id(tmp, net);
513 		if (id >= 0)
514 			idr_remove(&tmp->netns_ids, id);
515 		spin_unlock_bh(&tmp->nsid_lock);
516 		if (id >= 0)
517 			rtnl_net_notifyid(tmp, RTM_DELNSID, id);
518 		if (tmp == last)
519 			break;
520 	}
521 	spin_lock_bh(&net->nsid_lock);
522 	idr_destroy(&net->netns_ids);
523 	spin_unlock_bh(&net->nsid_lock);
524 }
525 
526 static LLIST_HEAD(cleanup_list);
527 
528 static void cleanup_net(struct work_struct *work)
529 {
530 	const struct pernet_operations *ops;
531 	struct net *net, *tmp, *last;
532 	struct llist_node *net_kill_list;
533 	LIST_HEAD(net_exit_list);
534 
535 	/* Atomically snapshot the list of namespaces to cleanup */
536 	net_kill_list = llist_del_all(&cleanup_list);
537 
538 	down_read(&pernet_ops_rwsem);
539 
540 	/* Don't let anyone else find us. */
541 	down_write(&net_rwsem);
542 	llist_for_each_entry(net, net_kill_list, cleanup_list)
543 		list_del_rcu(&net->list);
544 	/* Cache last net. After we unlock rtnl, no one new net
545 	 * added to net_namespace_list can assign nsid pointer
546 	 * to a net from net_kill_list (see peernet2id_alloc()).
547 	 * So, we skip them in unhash_nsid().
548 	 *
549 	 * Note, that unhash_nsid() does not delete nsid links
550 	 * between net_kill_list's nets, as they've already
551 	 * deleted from net_namespace_list. But, this would be
552 	 * useless anyway, as netns_ids are destroyed there.
553 	 */
554 	last = list_last_entry(&net_namespace_list, struct net, list);
555 	up_write(&net_rwsem);
556 
557 	llist_for_each_entry(net, net_kill_list, cleanup_list) {
558 		unhash_nsid(net, last);
559 		list_add_tail(&net->exit_list, &net_exit_list);
560 	}
561 
562 	/*
563 	 * Another CPU might be rcu-iterating the list, wait for it.
564 	 * This needs to be before calling the exit() notifiers, so
565 	 * the rcu_barrier() below isn't sufficient alone.
566 	 */
567 	synchronize_rcu();
568 
569 	/* Run all of the network namespace exit methods */
570 	list_for_each_entry_reverse(ops, &pernet_list, list)
571 		ops_exit_list(ops, &net_exit_list);
572 
573 	/* Free the net generic variables */
574 	list_for_each_entry_reverse(ops, &pernet_list, list)
575 		ops_free_list(ops, &net_exit_list);
576 
577 	up_read(&pernet_ops_rwsem);
578 
579 	/* Ensure there are no outstanding rcu callbacks using this
580 	 * network namespace.
581 	 */
582 	rcu_barrier();
583 
584 	/* Finally it is safe to free my network namespace structure */
585 	list_for_each_entry_safe(net, tmp, &net_exit_list, exit_list) {
586 		list_del_init(&net->exit_list);
587 		dec_net_namespaces(net->ucounts);
588 		key_remove_domain(net->key_domain);
589 		put_user_ns(net->user_ns);
590 		net_drop_ns(net);
591 	}
592 }
593 
594 /**
595  * net_ns_barrier - wait until concurrent net_cleanup_work is done
596  *
597  * cleanup_net runs from work queue and will first remove namespaces
598  * from the global list, then run net exit functions.
599  *
600  * Call this in module exit path to make sure that all netns
601  * ->exit ops have been invoked before the function is removed.
602  */
603 void net_ns_barrier(void)
604 {
605 	down_write(&pernet_ops_rwsem);
606 	up_write(&pernet_ops_rwsem);
607 }
608 EXPORT_SYMBOL(net_ns_barrier);
609 
610 static DECLARE_WORK(net_cleanup_work, cleanup_net);
611 
612 void __put_net(struct net *net)
613 {
614 	/* Cleanup the network namespace in process context */
615 	if (llist_add(&net->cleanup_list, &cleanup_list))
616 		queue_work(netns_wq, &net_cleanup_work);
617 }
618 EXPORT_SYMBOL_GPL(__put_net);
619 
620 struct net *get_net_ns_by_fd(int fd)
621 {
622 	struct file *file;
623 	struct ns_common *ns;
624 	struct net *net;
625 
626 	file = proc_ns_fget(fd);
627 	if (IS_ERR(file))
628 		return ERR_CAST(file);
629 
630 	ns = get_proc_ns(file_inode(file));
631 	if (ns->ops == &netns_operations)
632 		net = get_net(container_of(ns, struct net, ns));
633 	else
634 		net = ERR_PTR(-EINVAL);
635 
636 	fput(file);
637 	return net;
638 }
639 
640 #else
641 struct net *get_net_ns_by_fd(int fd)
642 {
643 	return ERR_PTR(-EINVAL);
644 }
645 #endif
646 EXPORT_SYMBOL_GPL(get_net_ns_by_fd);
647 
648 struct net *get_net_ns_by_pid(pid_t pid)
649 {
650 	struct task_struct *tsk;
651 	struct net *net;
652 
653 	/* Lookup the network namespace */
654 	net = ERR_PTR(-ESRCH);
655 	rcu_read_lock();
656 	tsk = find_task_by_vpid(pid);
657 	if (tsk) {
658 		struct nsproxy *nsproxy;
659 		task_lock(tsk);
660 		nsproxy = tsk->nsproxy;
661 		if (nsproxy)
662 			net = get_net(nsproxy->net_ns);
663 		task_unlock(tsk);
664 	}
665 	rcu_read_unlock();
666 	return net;
667 }
668 EXPORT_SYMBOL_GPL(get_net_ns_by_pid);
669 
670 static __net_init int net_ns_net_init(struct net *net)
671 {
672 #ifdef CONFIG_NET_NS
673 	net->ns.ops = &netns_operations;
674 #endif
675 	return ns_alloc_inum(&net->ns);
676 }
677 
678 static __net_exit void net_ns_net_exit(struct net *net)
679 {
680 	ns_free_inum(&net->ns);
681 }
682 
683 static struct pernet_operations __net_initdata net_ns_ops = {
684 	.init = net_ns_net_init,
685 	.exit = net_ns_net_exit,
686 };
687 
688 static const struct nla_policy rtnl_net_policy[NETNSA_MAX + 1] = {
689 	[NETNSA_NONE]		= { .type = NLA_UNSPEC },
690 	[NETNSA_NSID]		= { .type = NLA_S32 },
691 	[NETNSA_PID]		= { .type = NLA_U32 },
692 	[NETNSA_FD]		= { .type = NLA_U32 },
693 	[NETNSA_TARGET_NSID]	= { .type = NLA_S32 },
694 };
695 
696 static int rtnl_net_newid(struct sk_buff *skb, struct nlmsghdr *nlh,
697 			  struct netlink_ext_ack *extack)
698 {
699 	struct net *net = sock_net(skb->sk);
700 	struct nlattr *tb[NETNSA_MAX + 1];
701 	struct nlattr *nla;
702 	struct net *peer;
703 	int nsid, err;
704 
705 	err = nlmsg_parse_deprecated(nlh, sizeof(struct rtgenmsg), tb,
706 				     NETNSA_MAX, rtnl_net_policy, extack);
707 	if (err < 0)
708 		return err;
709 	if (!tb[NETNSA_NSID]) {
710 		NL_SET_ERR_MSG(extack, "nsid is missing");
711 		return -EINVAL;
712 	}
713 	nsid = nla_get_s32(tb[NETNSA_NSID]);
714 
715 	if (tb[NETNSA_PID]) {
716 		peer = get_net_ns_by_pid(nla_get_u32(tb[NETNSA_PID]));
717 		nla = tb[NETNSA_PID];
718 	} else if (tb[NETNSA_FD]) {
719 		peer = get_net_ns_by_fd(nla_get_u32(tb[NETNSA_FD]));
720 		nla = tb[NETNSA_FD];
721 	} else {
722 		NL_SET_ERR_MSG(extack, "Peer netns reference is missing");
723 		return -EINVAL;
724 	}
725 	if (IS_ERR(peer)) {
726 		NL_SET_BAD_ATTR(extack, nla);
727 		NL_SET_ERR_MSG(extack, "Peer netns reference is invalid");
728 		return PTR_ERR(peer);
729 	}
730 
731 	spin_lock_bh(&net->nsid_lock);
732 	if (__peernet2id(net, peer) >= 0) {
733 		spin_unlock_bh(&net->nsid_lock);
734 		err = -EEXIST;
735 		NL_SET_BAD_ATTR(extack, nla);
736 		NL_SET_ERR_MSG(extack,
737 			       "Peer netns already has a nsid assigned");
738 		goto out;
739 	}
740 
741 	err = alloc_netid(net, peer, nsid);
742 	spin_unlock_bh(&net->nsid_lock);
743 	if (err >= 0) {
744 		rtnl_net_notifyid(net, RTM_NEWNSID, err);
745 		err = 0;
746 	} else if (err == -ENOSPC && nsid >= 0) {
747 		err = -EEXIST;
748 		NL_SET_BAD_ATTR(extack, tb[NETNSA_NSID]);
749 		NL_SET_ERR_MSG(extack, "The specified nsid is already used");
750 	}
751 out:
752 	put_net(peer);
753 	return err;
754 }
755 
756 static int rtnl_net_get_size(void)
757 {
758 	return NLMSG_ALIGN(sizeof(struct rtgenmsg))
759 	       + nla_total_size(sizeof(s32)) /* NETNSA_NSID */
760 	       + nla_total_size(sizeof(s32)) /* NETNSA_CURRENT_NSID */
761 	       ;
762 }
763 
764 struct net_fill_args {
765 	u32 portid;
766 	u32 seq;
767 	int flags;
768 	int cmd;
769 	int nsid;
770 	bool add_ref;
771 	int ref_nsid;
772 };
773 
774 static int rtnl_net_fill(struct sk_buff *skb, struct net_fill_args *args)
775 {
776 	struct nlmsghdr *nlh;
777 	struct rtgenmsg *rth;
778 
779 	nlh = nlmsg_put(skb, args->portid, args->seq, args->cmd, sizeof(*rth),
780 			args->flags);
781 	if (!nlh)
782 		return -EMSGSIZE;
783 
784 	rth = nlmsg_data(nlh);
785 	rth->rtgen_family = AF_UNSPEC;
786 
787 	if (nla_put_s32(skb, NETNSA_NSID, args->nsid))
788 		goto nla_put_failure;
789 
790 	if (args->add_ref &&
791 	    nla_put_s32(skb, NETNSA_CURRENT_NSID, args->ref_nsid))
792 		goto nla_put_failure;
793 
794 	nlmsg_end(skb, nlh);
795 	return 0;
796 
797 nla_put_failure:
798 	nlmsg_cancel(skb, nlh);
799 	return -EMSGSIZE;
800 }
801 
802 static int rtnl_net_valid_getid_req(struct sk_buff *skb,
803 				    const struct nlmsghdr *nlh,
804 				    struct nlattr **tb,
805 				    struct netlink_ext_ack *extack)
806 {
807 	int i, err;
808 
809 	if (!netlink_strict_get_check(skb))
810 		return nlmsg_parse_deprecated(nlh, sizeof(struct rtgenmsg),
811 					      tb, NETNSA_MAX, rtnl_net_policy,
812 					      extack);
813 
814 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct rtgenmsg), tb,
815 					    NETNSA_MAX, rtnl_net_policy,
816 					    extack);
817 	if (err)
818 		return err;
819 
820 	for (i = 0; i <= NETNSA_MAX; i++) {
821 		if (!tb[i])
822 			continue;
823 
824 		switch (i) {
825 		case NETNSA_PID:
826 		case NETNSA_FD:
827 		case NETNSA_NSID:
828 		case NETNSA_TARGET_NSID:
829 			break;
830 		default:
831 			NL_SET_ERR_MSG(extack, "Unsupported attribute in peer netns getid request");
832 			return -EINVAL;
833 		}
834 	}
835 
836 	return 0;
837 }
838 
839 static int rtnl_net_getid(struct sk_buff *skb, struct nlmsghdr *nlh,
840 			  struct netlink_ext_ack *extack)
841 {
842 	struct net *net = sock_net(skb->sk);
843 	struct nlattr *tb[NETNSA_MAX + 1];
844 	struct net_fill_args fillargs = {
845 		.portid = NETLINK_CB(skb).portid,
846 		.seq = nlh->nlmsg_seq,
847 		.cmd = RTM_NEWNSID,
848 	};
849 	struct net *peer, *target = net;
850 	struct nlattr *nla;
851 	struct sk_buff *msg;
852 	int err;
853 
854 	err = rtnl_net_valid_getid_req(skb, nlh, tb, extack);
855 	if (err < 0)
856 		return err;
857 	if (tb[NETNSA_PID]) {
858 		peer = get_net_ns_by_pid(nla_get_u32(tb[NETNSA_PID]));
859 		nla = tb[NETNSA_PID];
860 	} else if (tb[NETNSA_FD]) {
861 		peer = get_net_ns_by_fd(nla_get_u32(tb[NETNSA_FD]));
862 		nla = tb[NETNSA_FD];
863 	} else if (tb[NETNSA_NSID]) {
864 		peer = get_net_ns_by_id(net, nla_get_s32(tb[NETNSA_NSID]));
865 		if (!peer)
866 			peer = ERR_PTR(-ENOENT);
867 		nla = tb[NETNSA_NSID];
868 	} else {
869 		NL_SET_ERR_MSG(extack, "Peer netns reference is missing");
870 		return -EINVAL;
871 	}
872 
873 	if (IS_ERR(peer)) {
874 		NL_SET_BAD_ATTR(extack, nla);
875 		NL_SET_ERR_MSG(extack, "Peer netns reference is invalid");
876 		return PTR_ERR(peer);
877 	}
878 
879 	if (tb[NETNSA_TARGET_NSID]) {
880 		int id = nla_get_s32(tb[NETNSA_TARGET_NSID]);
881 
882 		target = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, id);
883 		if (IS_ERR(target)) {
884 			NL_SET_BAD_ATTR(extack, tb[NETNSA_TARGET_NSID]);
885 			NL_SET_ERR_MSG(extack,
886 				       "Target netns reference is invalid");
887 			err = PTR_ERR(target);
888 			goto out;
889 		}
890 		fillargs.add_ref = true;
891 		fillargs.ref_nsid = peernet2id(net, peer);
892 	}
893 
894 	msg = nlmsg_new(rtnl_net_get_size(), GFP_KERNEL);
895 	if (!msg) {
896 		err = -ENOMEM;
897 		goto out;
898 	}
899 
900 	fillargs.nsid = peernet2id(target, peer);
901 	err = rtnl_net_fill(msg, &fillargs);
902 	if (err < 0)
903 		goto err_out;
904 
905 	err = rtnl_unicast(msg, net, NETLINK_CB(skb).portid);
906 	goto out;
907 
908 err_out:
909 	nlmsg_free(msg);
910 out:
911 	if (fillargs.add_ref)
912 		put_net(target);
913 	put_net(peer);
914 	return err;
915 }
916 
917 struct rtnl_net_dump_cb {
918 	struct net *tgt_net;
919 	struct net *ref_net;
920 	struct sk_buff *skb;
921 	struct net_fill_args fillargs;
922 	int idx;
923 	int s_idx;
924 };
925 
926 static int rtnl_net_dumpid_one(int id, void *peer, void *data)
927 {
928 	struct rtnl_net_dump_cb *net_cb = (struct rtnl_net_dump_cb *)data;
929 	int ret;
930 
931 	if (net_cb->idx < net_cb->s_idx)
932 		goto cont;
933 
934 	net_cb->fillargs.nsid = id;
935 	if (net_cb->fillargs.add_ref)
936 		net_cb->fillargs.ref_nsid = __peernet2id(net_cb->ref_net, peer);
937 	ret = rtnl_net_fill(net_cb->skb, &net_cb->fillargs);
938 	if (ret < 0)
939 		return ret;
940 
941 cont:
942 	net_cb->idx++;
943 	return 0;
944 }
945 
946 static int rtnl_valid_dump_net_req(const struct nlmsghdr *nlh, struct sock *sk,
947 				   struct rtnl_net_dump_cb *net_cb,
948 				   struct netlink_callback *cb)
949 {
950 	struct netlink_ext_ack *extack = cb->extack;
951 	struct nlattr *tb[NETNSA_MAX + 1];
952 	int err, i;
953 
954 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct rtgenmsg), tb,
955 					    NETNSA_MAX, rtnl_net_policy,
956 					    extack);
957 	if (err < 0)
958 		return err;
959 
960 	for (i = 0; i <= NETNSA_MAX; i++) {
961 		if (!tb[i])
962 			continue;
963 
964 		if (i == NETNSA_TARGET_NSID) {
965 			struct net *net;
966 
967 			net = rtnl_get_net_ns_capable(sk, nla_get_s32(tb[i]));
968 			if (IS_ERR(net)) {
969 				NL_SET_BAD_ATTR(extack, tb[i]);
970 				NL_SET_ERR_MSG(extack,
971 					       "Invalid target network namespace id");
972 				return PTR_ERR(net);
973 			}
974 			net_cb->fillargs.add_ref = true;
975 			net_cb->ref_net = net_cb->tgt_net;
976 			net_cb->tgt_net = net;
977 		} else {
978 			NL_SET_BAD_ATTR(extack, tb[i]);
979 			NL_SET_ERR_MSG(extack,
980 				       "Unsupported attribute in dump request");
981 			return -EINVAL;
982 		}
983 	}
984 
985 	return 0;
986 }
987 
988 static int rtnl_net_dumpid(struct sk_buff *skb, struct netlink_callback *cb)
989 {
990 	struct rtnl_net_dump_cb net_cb = {
991 		.tgt_net = sock_net(skb->sk),
992 		.skb = skb,
993 		.fillargs = {
994 			.portid = NETLINK_CB(cb->skb).portid,
995 			.seq = cb->nlh->nlmsg_seq,
996 			.flags = NLM_F_MULTI,
997 			.cmd = RTM_NEWNSID,
998 		},
999 		.idx = 0,
1000 		.s_idx = cb->args[0],
1001 	};
1002 	int err = 0;
1003 
1004 	if (cb->strict_check) {
1005 		err = rtnl_valid_dump_net_req(cb->nlh, skb->sk, &net_cb, cb);
1006 		if (err < 0)
1007 			goto end;
1008 	}
1009 
1010 	spin_lock_bh(&net_cb.tgt_net->nsid_lock);
1011 	if (net_cb.fillargs.add_ref &&
1012 	    !net_eq(net_cb.ref_net, net_cb.tgt_net) &&
1013 	    !spin_trylock_bh(&net_cb.ref_net->nsid_lock)) {
1014 		spin_unlock_bh(&net_cb.tgt_net->nsid_lock);
1015 		err = -EAGAIN;
1016 		goto end;
1017 	}
1018 	idr_for_each(&net_cb.tgt_net->netns_ids, rtnl_net_dumpid_one, &net_cb);
1019 	if (net_cb.fillargs.add_ref &&
1020 	    !net_eq(net_cb.ref_net, net_cb.tgt_net))
1021 		spin_unlock_bh(&net_cb.ref_net->nsid_lock);
1022 	spin_unlock_bh(&net_cb.tgt_net->nsid_lock);
1023 
1024 	cb->args[0] = net_cb.idx;
1025 end:
1026 	if (net_cb.fillargs.add_ref)
1027 		put_net(net_cb.tgt_net);
1028 	return err < 0 ? err : skb->len;
1029 }
1030 
1031 static void rtnl_net_notifyid(struct net *net, int cmd, int id)
1032 {
1033 	struct net_fill_args fillargs = {
1034 		.cmd = cmd,
1035 		.nsid = id,
1036 	};
1037 	struct sk_buff *msg;
1038 	int err = -ENOMEM;
1039 
1040 	msg = nlmsg_new(rtnl_net_get_size(), GFP_KERNEL);
1041 	if (!msg)
1042 		goto out;
1043 
1044 	err = rtnl_net_fill(msg, &fillargs);
1045 	if (err < 0)
1046 		goto err_out;
1047 
1048 	rtnl_notify(msg, net, 0, RTNLGRP_NSID, NULL, 0);
1049 	return;
1050 
1051 err_out:
1052 	nlmsg_free(msg);
1053 out:
1054 	rtnl_set_sk_err(net, RTNLGRP_NSID, err);
1055 }
1056 
1057 static int __init net_ns_init(void)
1058 {
1059 	struct net_generic *ng;
1060 
1061 #ifdef CONFIG_NET_NS
1062 	net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
1063 					SMP_CACHE_BYTES,
1064 					SLAB_PANIC|SLAB_ACCOUNT, NULL);
1065 
1066 	/* Create workqueue for cleanup */
1067 	netns_wq = create_singlethread_workqueue("netns");
1068 	if (!netns_wq)
1069 		panic("Could not create netns workq");
1070 #endif
1071 
1072 	ng = net_alloc_generic();
1073 	if (!ng)
1074 		panic("Could not allocate generic netns");
1075 
1076 	rcu_assign_pointer(init_net.gen, ng);
1077 
1078 	down_write(&pernet_ops_rwsem);
1079 	if (setup_net(&init_net, &init_user_ns))
1080 		panic("Could not setup the initial network namespace");
1081 
1082 	init_net_initialized = true;
1083 	up_write(&pernet_ops_rwsem);
1084 
1085 	if (register_pernet_subsys(&net_ns_ops))
1086 		panic("Could not register network namespace subsystems");
1087 
1088 	rtnl_register(PF_UNSPEC, RTM_NEWNSID, rtnl_net_newid, NULL,
1089 		      RTNL_FLAG_DOIT_UNLOCKED);
1090 	rtnl_register(PF_UNSPEC, RTM_GETNSID, rtnl_net_getid, rtnl_net_dumpid,
1091 		      RTNL_FLAG_DOIT_UNLOCKED);
1092 
1093 	return 0;
1094 }
1095 
1096 pure_initcall(net_ns_init);
1097 
1098 #ifdef CONFIG_NET_NS
1099 static int __register_pernet_operations(struct list_head *list,
1100 					struct pernet_operations *ops)
1101 {
1102 	struct net *net;
1103 	int error;
1104 	LIST_HEAD(net_exit_list);
1105 
1106 	list_add_tail(&ops->list, list);
1107 	if (ops->init || (ops->id && ops->size)) {
1108 		/* We held write locked pernet_ops_rwsem, and parallel
1109 		 * setup_net() and cleanup_net() are not possible.
1110 		 */
1111 		for_each_net(net) {
1112 			error = ops_init(ops, net);
1113 			if (error)
1114 				goto out_undo;
1115 			list_add_tail(&net->exit_list, &net_exit_list);
1116 		}
1117 	}
1118 	return 0;
1119 
1120 out_undo:
1121 	/* If I have an error cleanup all namespaces I initialized */
1122 	list_del(&ops->list);
1123 	ops_exit_list(ops, &net_exit_list);
1124 	ops_free_list(ops, &net_exit_list);
1125 	return error;
1126 }
1127 
1128 static void __unregister_pernet_operations(struct pernet_operations *ops)
1129 {
1130 	struct net *net;
1131 	LIST_HEAD(net_exit_list);
1132 
1133 	list_del(&ops->list);
1134 	/* See comment in __register_pernet_operations() */
1135 	for_each_net(net)
1136 		list_add_tail(&net->exit_list, &net_exit_list);
1137 	ops_exit_list(ops, &net_exit_list);
1138 	ops_free_list(ops, &net_exit_list);
1139 }
1140 
1141 #else
1142 
1143 static int __register_pernet_operations(struct list_head *list,
1144 					struct pernet_operations *ops)
1145 {
1146 	if (!init_net_initialized) {
1147 		list_add_tail(&ops->list, list);
1148 		return 0;
1149 	}
1150 
1151 	return ops_init(ops, &init_net);
1152 }
1153 
1154 static void __unregister_pernet_operations(struct pernet_operations *ops)
1155 {
1156 	if (!init_net_initialized) {
1157 		list_del(&ops->list);
1158 	} else {
1159 		LIST_HEAD(net_exit_list);
1160 		list_add(&init_net.exit_list, &net_exit_list);
1161 		ops_exit_list(ops, &net_exit_list);
1162 		ops_free_list(ops, &net_exit_list);
1163 	}
1164 }
1165 
1166 #endif /* CONFIG_NET_NS */
1167 
1168 static DEFINE_IDA(net_generic_ids);
1169 
1170 static int register_pernet_operations(struct list_head *list,
1171 				      struct pernet_operations *ops)
1172 {
1173 	int error;
1174 
1175 	if (ops->id) {
1176 		error = ida_alloc_min(&net_generic_ids, MIN_PERNET_OPS_ID,
1177 				GFP_KERNEL);
1178 		if (error < 0)
1179 			return error;
1180 		*ops->id = error;
1181 		max_gen_ptrs = max(max_gen_ptrs, *ops->id + 1);
1182 	}
1183 	error = __register_pernet_operations(list, ops);
1184 	if (error) {
1185 		rcu_barrier();
1186 		if (ops->id)
1187 			ida_free(&net_generic_ids, *ops->id);
1188 	}
1189 
1190 	return error;
1191 }
1192 
1193 static void unregister_pernet_operations(struct pernet_operations *ops)
1194 {
1195 	__unregister_pernet_operations(ops);
1196 	rcu_barrier();
1197 	if (ops->id)
1198 		ida_free(&net_generic_ids, *ops->id);
1199 }
1200 
1201 /**
1202  *      register_pernet_subsys - register a network namespace subsystem
1203  *	@ops:  pernet operations structure for the subsystem
1204  *
1205  *	Register a subsystem which has init and exit functions
1206  *	that are called when network namespaces are created and
1207  *	destroyed respectively.
1208  *
1209  *	When registered all network namespace init functions are
1210  *	called for every existing network namespace.  Allowing kernel
1211  *	modules to have a race free view of the set of network namespaces.
1212  *
1213  *	When a new network namespace is created all of the init
1214  *	methods are called in the order in which they were registered.
1215  *
1216  *	When a network namespace is destroyed all of the exit methods
1217  *	are called in the reverse of the order with which they were
1218  *	registered.
1219  */
1220 int register_pernet_subsys(struct pernet_operations *ops)
1221 {
1222 	int error;
1223 	down_write(&pernet_ops_rwsem);
1224 	error =  register_pernet_operations(first_device, ops);
1225 	up_write(&pernet_ops_rwsem);
1226 	return error;
1227 }
1228 EXPORT_SYMBOL_GPL(register_pernet_subsys);
1229 
1230 /**
1231  *      unregister_pernet_subsys - unregister a network namespace subsystem
1232  *	@ops: pernet operations structure to manipulate
1233  *
1234  *	Remove the pernet operations structure from the list to be
1235  *	used when network namespaces are created or destroyed.  In
1236  *	addition run the exit method for all existing network
1237  *	namespaces.
1238  */
1239 void unregister_pernet_subsys(struct pernet_operations *ops)
1240 {
1241 	down_write(&pernet_ops_rwsem);
1242 	unregister_pernet_operations(ops);
1243 	up_write(&pernet_ops_rwsem);
1244 }
1245 EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
1246 
1247 /**
1248  *      register_pernet_device - register a network namespace device
1249  *	@ops:  pernet operations structure for the subsystem
1250  *
1251  *	Register a device which has init and exit functions
1252  *	that are called when network namespaces are created and
1253  *	destroyed respectively.
1254  *
1255  *	When registered all network namespace init functions are
1256  *	called for every existing network namespace.  Allowing kernel
1257  *	modules to have a race free view of the set of network namespaces.
1258  *
1259  *	When a new network namespace is created all of the init
1260  *	methods are called in the order in which they were registered.
1261  *
1262  *	When a network namespace is destroyed all of the exit methods
1263  *	are called in the reverse of the order with which they were
1264  *	registered.
1265  */
1266 int register_pernet_device(struct pernet_operations *ops)
1267 {
1268 	int error;
1269 	down_write(&pernet_ops_rwsem);
1270 	error = register_pernet_operations(&pernet_list, ops);
1271 	if (!error && (first_device == &pernet_list))
1272 		first_device = &ops->list;
1273 	up_write(&pernet_ops_rwsem);
1274 	return error;
1275 }
1276 EXPORT_SYMBOL_GPL(register_pernet_device);
1277 
1278 /**
1279  *      unregister_pernet_device - unregister a network namespace netdevice
1280  *	@ops: pernet operations structure to manipulate
1281  *
1282  *	Remove the pernet operations structure from the list to be
1283  *	used when network namespaces are created or destroyed.  In
1284  *	addition run the exit method for all existing network
1285  *	namespaces.
1286  */
1287 void unregister_pernet_device(struct pernet_operations *ops)
1288 {
1289 	down_write(&pernet_ops_rwsem);
1290 	if (&ops->list == first_device)
1291 		first_device = first_device->next;
1292 	unregister_pernet_operations(ops);
1293 	up_write(&pernet_ops_rwsem);
1294 }
1295 EXPORT_SYMBOL_GPL(unregister_pernet_device);
1296 
1297 #ifdef CONFIG_NET_NS
1298 static struct ns_common *netns_get(struct task_struct *task)
1299 {
1300 	struct net *net = NULL;
1301 	struct nsproxy *nsproxy;
1302 
1303 	task_lock(task);
1304 	nsproxy = task->nsproxy;
1305 	if (nsproxy)
1306 		net = get_net(nsproxy->net_ns);
1307 	task_unlock(task);
1308 
1309 	return net ? &net->ns : NULL;
1310 }
1311 
1312 static inline struct net *to_net_ns(struct ns_common *ns)
1313 {
1314 	return container_of(ns, struct net, ns);
1315 }
1316 
1317 static void netns_put(struct ns_common *ns)
1318 {
1319 	put_net(to_net_ns(ns));
1320 }
1321 
1322 static int netns_install(struct nsproxy *nsproxy, struct ns_common *ns)
1323 {
1324 	struct net *net = to_net_ns(ns);
1325 
1326 	if (!ns_capable(net->user_ns, CAP_SYS_ADMIN) ||
1327 	    !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
1328 		return -EPERM;
1329 
1330 	put_net(nsproxy->net_ns);
1331 	nsproxy->net_ns = get_net(net);
1332 	return 0;
1333 }
1334 
1335 static struct user_namespace *netns_owner(struct ns_common *ns)
1336 {
1337 	return to_net_ns(ns)->user_ns;
1338 }
1339 
1340 const struct proc_ns_operations netns_operations = {
1341 	.name		= "net",
1342 	.type		= CLONE_NEWNET,
1343 	.get		= netns_get,
1344 	.put		= netns_put,
1345 	.install	= netns_install,
1346 	.owner		= netns_owner,
1347 };
1348 #endif
1349