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