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