0001
0002 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0003
0004 #include <linux/workqueue.h>
0005 #include <linux/rtnetlink.h>
0006 #include <linux/cache.h>
0007 #include <linux/slab.h>
0008 #include <linux/list.h>
0009 #include <linux/delay.h>
0010 #include <linux/sched.h>
0011 #include <linux/idr.h>
0012 #include <linux/rculist.h>
0013 #include <linux/nsproxy.h>
0014 #include <linux/fs.h>
0015 #include <linux/proc_ns.h>
0016 #include <linux/file.h>
0017 #include <linux/export.h>
0018 #include <linux/user_namespace.h>
0019 #include <linux/net_namespace.h>
0020 #include <linux/sched/task.h>
0021 #include <linux/uidgid.h>
0022 #include <linux/cookie.h>
0023
0024 #include <net/sock.h>
0025 #include <net/netlink.h>
0026 #include <net/net_namespace.h>
0027 #include <net/netns/generic.h>
0028
0029
0030
0031
0032
0033 static LIST_HEAD(pernet_list);
0034 static struct list_head *first_device = &pernet_list;
0035
0036 LIST_HEAD(net_namespace_list);
0037 EXPORT_SYMBOL_GPL(net_namespace_list);
0038
0039
0040 DECLARE_RWSEM(net_rwsem);
0041 EXPORT_SYMBOL_GPL(net_rwsem);
0042
0043 #ifdef CONFIG_KEYS
0044 static struct key_tag init_net_key_domain = { .usage = REFCOUNT_INIT(1) };
0045 #endif
0046
0047 struct net init_net;
0048 EXPORT_SYMBOL(init_net);
0049
0050 static bool init_net_initialized;
0051
0052
0053
0054
0055
0056
0057 DECLARE_RWSEM(pernet_ops_rwsem);
0058 EXPORT_SYMBOL_GPL(pernet_ops_rwsem);
0059
0060 #define MIN_PERNET_OPS_ID \
0061 ((sizeof(struct net_generic) + sizeof(void *) - 1) / sizeof(void *))
0062
0063 #define INITIAL_NET_GEN_PTRS 13
0064
0065 static unsigned int max_gen_ptrs = INITIAL_NET_GEN_PTRS;
0066
0067 DEFINE_COOKIE(net_cookie);
0068
0069 static struct net_generic *net_alloc_generic(void)
0070 {
0071 struct net_generic *ng;
0072 unsigned int generic_size = offsetof(struct net_generic, ptr[max_gen_ptrs]);
0073
0074 ng = kzalloc(generic_size, GFP_KERNEL);
0075 if (ng)
0076 ng->s.len = max_gen_ptrs;
0077
0078 return ng;
0079 }
0080
0081 static int net_assign_generic(struct net *net, unsigned int id, void *data)
0082 {
0083 struct net_generic *ng, *old_ng;
0084
0085 BUG_ON(id < MIN_PERNET_OPS_ID);
0086
0087 old_ng = rcu_dereference_protected(net->gen,
0088 lockdep_is_held(&pernet_ops_rwsem));
0089 if (old_ng->s.len > id) {
0090 old_ng->ptr[id] = data;
0091 return 0;
0092 }
0093
0094 ng = net_alloc_generic();
0095 if (!ng)
0096 return -ENOMEM;
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109 memcpy(&ng->ptr[MIN_PERNET_OPS_ID], &old_ng->ptr[MIN_PERNET_OPS_ID],
0110 (old_ng->s.len - MIN_PERNET_OPS_ID) * sizeof(void *));
0111 ng->ptr[id] = data;
0112
0113 rcu_assign_pointer(net->gen, ng);
0114 kfree_rcu(old_ng, s.rcu);
0115 return 0;
0116 }
0117
0118 static int ops_init(const struct pernet_operations *ops, struct net *net)
0119 {
0120 int err = -ENOMEM;
0121 void *data = NULL;
0122
0123 if (ops->id && ops->size) {
0124 data = kzalloc(ops->size, GFP_KERNEL);
0125 if (!data)
0126 goto out;
0127
0128 err = net_assign_generic(net, *ops->id, data);
0129 if (err)
0130 goto cleanup;
0131 }
0132 err = 0;
0133 if (ops->init)
0134 err = ops->init(net);
0135 if (!err)
0136 return 0;
0137
0138 cleanup:
0139 kfree(data);
0140
0141 out:
0142 return err;
0143 }
0144
0145 static void ops_pre_exit_list(const struct pernet_operations *ops,
0146 struct list_head *net_exit_list)
0147 {
0148 struct net *net;
0149
0150 if (ops->pre_exit) {
0151 list_for_each_entry(net, net_exit_list, exit_list)
0152 ops->pre_exit(net);
0153 }
0154 }
0155
0156 static void ops_exit_list(const struct pernet_operations *ops,
0157 struct list_head *net_exit_list)
0158 {
0159 struct net *net;
0160 if (ops->exit) {
0161 list_for_each_entry(net, net_exit_list, exit_list) {
0162 ops->exit(net);
0163 cond_resched();
0164 }
0165 }
0166 if (ops->exit_batch)
0167 ops->exit_batch(net_exit_list);
0168 }
0169
0170 static void ops_free_list(const struct pernet_operations *ops,
0171 struct list_head *net_exit_list)
0172 {
0173 struct net *net;
0174 if (ops->size && ops->id) {
0175 list_for_each_entry(net, net_exit_list, exit_list)
0176 kfree(net_generic(net, *ops->id));
0177 }
0178 }
0179
0180
0181 static int alloc_netid(struct net *net, struct net *peer, int reqid)
0182 {
0183 int min = 0, max = 0;
0184
0185 if (reqid >= 0) {
0186 min = reqid;
0187 max = reqid + 1;
0188 }
0189
0190 return idr_alloc(&net->netns_ids, peer, min, max, GFP_ATOMIC);
0191 }
0192
0193
0194
0195
0196
0197
0198 #define NET_ID_ZERO -1
0199 static int net_eq_idr(int id, void *net, void *peer)
0200 {
0201 if (net_eq(net, peer))
0202 return id ? : NET_ID_ZERO;
0203 return 0;
0204 }
0205
0206
0207 static int __peernet2id(const struct net *net, struct net *peer)
0208 {
0209 int id = idr_for_each(&net->netns_ids, net_eq_idr, peer);
0210
0211
0212 if (id == NET_ID_ZERO)
0213 return 0;
0214 if (id > 0)
0215 return id;
0216
0217 return NETNSA_NSID_NOT_ASSIGNED;
0218 }
0219
0220 static void rtnl_net_notifyid(struct net *net, int cmd, int id, u32 portid,
0221 struct nlmsghdr *nlh, gfp_t gfp);
0222
0223
0224
0225 int peernet2id_alloc(struct net *net, struct net *peer, gfp_t gfp)
0226 {
0227 int id;
0228
0229 if (refcount_read(&net->ns.count) == 0)
0230 return NETNSA_NSID_NOT_ASSIGNED;
0231
0232 spin_lock_bh(&net->nsid_lock);
0233 id = __peernet2id(net, peer);
0234 if (id >= 0) {
0235 spin_unlock_bh(&net->nsid_lock);
0236 return id;
0237 }
0238
0239
0240
0241
0242
0243
0244 if (!maybe_get_net(peer)) {
0245 spin_unlock_bh(&net->nsid_lock);
0246 return NETNSA_NSID_NOT_ASSIGNED;
0247 }
0248
0249 id = alloc_netid(net, peer, -1);
0250 spin_unlock_bh(&net->nsid_lock);
0251
0252 put_net(peer);
0253 if (id < 0)
0254 return NETNSA_NSID_NOT_ASSIGNED;
0255
0256 rtnl_net_notifyid(net, RTM_NEWNSID, id, 0, NULL, gfp);
0257
0258 return id;
0259 }
0260 EXPORT_SYMBOL_GPL(peernet2id_alloc);
0261
0262
0263 int peernet2id(const struct net *net, struct net *peer)
0264 {
0265 int id;
0266
0267 rcu_read_lock();
0268 id = __peernet2id(net, peer);
0269 rcu_read_unlock();
0270
0271 return id;
0272 }
0273 EXPORT_SYMBOL(peernet2id);
0274
0275
0276
0277
0278 bool peernet_has_id(const struct net *net, struct net *peer)
0279 {
0280 return peernet2id(net, peer) >= 0;
0281 }
0282
0283 struct net *get_net_ns_by_id(const struct net *net, int id)
0284 {
0285 struct net *peer;
0286
0287 if (id < 0)
0288 return NULL;
0289
0290 rcu_read_lock();
0291 peer = idr_find(&net->netns_ids, id);
0292 if (peer)
0293 peer = maybe_get_net(peer);
0294 rcu_read_unlock();
0295
0296 return peer;
0297 }
0298 EXPORT_SYMBOL_GPL(get_net_ns_by_id);
0299
0300
0301
0302
0303 static __net_init int setup_net(struct net *net, struct user_namespace *user_ns)
0304 {
0305
0306 const struct pernet_operations *ops, *saved_ops;
0307 int error = 0;
0308 LIST_HEAD(net_exit_list);
0309
0310 refcount_set(&net->ns.count, 1);
0311 ref_tracker_dir_init(&net->refcnt_tracker, 128);
0312
0313 refcount_set(&net->passive, 1);
0314 get_random_bytes(&net->hash_mix, sizeof(u32));
0315 preempt_disable();
0316 net->net_cookie = gen_cookie_next(&net_cookie);
0317 preempt_enable();
0318 net->dev_base_seq = 1;
0319 net->user_ns = user_ns;
0320 idr_init(&net->netns_ids);
0321 spin_lock_init(&net->nsid_lock);
0322 mutex_init(&net->ipv4.ra_mutex);
0323
0324 list_for_each_entry(ops, &pernet_list, list) {
0325 error = ops_init(ops, net);
0326 if (error < 0)
0327 goto out_undo;
0328 }
0329 down_write(&net_rwsem);
0330 list_add_tail_rcu(&net->list, &net_namespace_list);
0331 up_write(&net_rwsem);
0332 out:
0333 return error;
0334
0335 out_undo:
0336
0337
0338
0339 list_add(&net->exit_list, &net_exit_list);
0340 saved_ops = ops;
0341 list_for_each_entry_continue_reverse(ops, &pernet_list, list)
0342 ops_pre_exit_list(ops, &net_exit_list);
0343
0344 synchronize_rcu();
0345
0346 ops = saved_ops;
0347 list_for_each_entry_continue_reverse(ops, &pernet_list, list)
0348 ops_exit_list(ops, &net_exit_list);
0349
0350 ops = saved_ops;
0351 list_for_each_entry_continue_reverse(ops, &pernet_list, list)
0352 ops_free_list(ops, &net_exit_list);
0353
0354 rcu_barrier();
0355 goto out;
0356 }
0357
0358 static int __net_init net_defaults_init_net(struct net *net)
0359 {
0360 net->core.sysctl_somaxconn = SOMAXCONN;
0361 net->core.sysctl_txrehash = SOCK_TXREHASH_ENABLED;
0362
0363 return 0;
0364 }
0365
0366 static struct pernet_operations net_defaults_ops = {
0367 .init = net_defaults_init_net,
0368 };
0369
0370 static __init int net_defaults_init(void)
0371 {
0372 if (register_pernet_subsys(&net_defaults_ops))
0373 panic("Cannot initialize net default settings");
0374
0375 return 0;
0376 }
0377
0378 core_initcall(net_defaults_init);
0379
0380 #ifdef CONFIG_NET_NS
0381 static struct ucounts *inc_net_namespaces(struct user_namespace *ns)
0382 {
0383 return inc_ucount(ns, current_euid(), UCOUNT_NET_NAMESPACES);
0384 }
0385
0386 static void dec_net_namespaces(struct ucounts *ucounts)
0387 {
0388 dec_ucount(ucounts, UCOUNT_NET_NAMESPACES);
0389 }
0390
0391 static struct kmem_cache *net_cachep __ro_after_init;
0392 static struct workqueue_struct *netns_wq;
0393
0394 static struct net *net_alloc(void)
0395 {
0396 struct net *net = NULL;
0397 struct net_generic *ng;
0398
0399 ng = net_alloc_generic();
0400 if (!ng)
0401 goto out;
0402
0403 net = kmem_cache_zalloc(net_cachep, GFP_KERNEL);
0404 if (!net)
0405 goto out_free;
0406
0407 #ifdef CONFIG_KEYS
0408 net->key_domain = kzalloc(sizeof(struct key_tag), GFP_KERNEL);
0409 if (!net->key_domain)
0410 goto out_free_2;
0411 refcount_set(&net->key_domain->usage, 1);
0412 #endif
0413
0414 rcu_assign_pointer(net->gen, ng);
0415 out:
0416 return net;
0417
0418 #ifdef CONFIG_KEYS
0419 out_free_2:
0420 kmem_cache_free(net_cachep, net);
0421 net = NULL;
0422 #endif
0423 out_free:
0424 kfree(ng);
0425 goto out;
0426 }
0427
0428 static void net_free(struct net *net)
0429 {
0430 if (refcount_dec_and_test(&net->passive)) {
0431 kfree(rcu_access_pointer(net->gen));
0432 kmem_cache_free(net_cachep, net);
0433 }
0434 }
0435
0436 void net_drop_ns(void *p)
0437 {
0438 struct net *net = (struct net *)p;
0439
0440 if (net)
0441 net_free(net);
0442 }
0443
0444 struct net *copy_net_ns(unsigned long flags,
0445 struct user_namespace *user_ns, struct net *old_net)
0446 {
0447 struct ucounts *ucounts;
0448 struct net *net;
0449 int rv;
0450
0451 if (!(flags & CLONE_NEWNET))
0452 return get_net(old_net);
0453
0454 ucounts = inc_net_namespaces(user_ns);
0455 if (!ucounts)
0456 return ERR_PTR(-ENOSPC);
0457
0458 net = net_alloc();
0459 if (!net) {
0460 rv = -ENOMEM;
0461 goto dec_ucounts;
0462 }
0463 refcount_set(&net->passive, 1);
0464 net->ucounts = ucounts;
0465 get_user_ns(user_ns);
0466
0467 rv = down_read_killable(&pernet_ops_rwsem);
0468 if (rv < 0)
0469 goto put_userns;
0470
0471 rv = setup_net(net, user_ns);
0472
0473 up_read(&pernet_ops_rwsem);
0474
0475 if (rv < 0) {
0476 put_userns:
0477 #ifdef CONFIG_KEYS
0478 key_remove_domain(net->key_domain);
0479 #endif
0480 put_user_ns(user_ns);
0481 net_free(net);
0482 dec_ucounts:
0483 dec_net_namespaces(ucounts);
0484 return ERR_PTR(rv);
0485 }
0486 return net;
0487 }
0488
0489
0490
0491
0492
0493
0494
0495
0496
0497
0498 void net_ns_get_ownership(const struct net *net, kuid_t *uid, kgid_t *gid)
0499 {
0500 if (net) {
0501 kuid_t ns_root_uid = make_kuid(net->user_ns, 0);
0502 kgid_t ns_root_gid = make_kgid(net->user_ns, 0);
0503
0504 if (uid_valid(ns_root_uid))
0505 *uid = ns_root_uid;
0506
0507 if (gid_valid(ns_root_gid))
0508 *gid = ns_root_gid;
0509 } else {
0510 *uid = GLOBAL_ROOT_UID;
0511 *gid = GLOBAL_ROOT_GID;
0512 }
0513 }
0514 EXPORT_SYMBOL_GPL(net_ns_get_ownership);
0515
0516 static void unhash_nsid(struct net *net, struct net *last)
0517 {
0518 struct net *tmp;
0519
0520
0521
0522
0523
0524
0525 for_each_net(tmp) {
0526 int id;
0527
0528 spin_lock_bh(&tmp->nsid_lock);
0529 id = __peernet2id(tmp, net);
0530 if (id >= 0)
0531 idr_remove(&tmp->netns_ids, id);
0532 spin_unlock_bh(&tmp->nsid_lock);
0533 if (id >= 0)
0534 rtnl_net_notifyid(tmp, RTM_DELNSID, id, 0, NULL,
0535 GFP_KERNEL);
0536 if (tmp == last)
0537 break;
0538 }
0539 spin_lock_bh(&net->nsid_lock);
0540 idr_destroy(&net->netns_ids);
0541 spin_unlock_bh(&net->nsid_lock);
0542 }
0543
0544 static LLIST_HEAD(cleanup_list);
0545
0546 static void cleanup_net(struct work_struct *work)
0547 {
0548 const struct pernet_operations *ops;
0549 struct net *net, *tmp, *last;
0550 struct llist_node *net_kill_list;
0551 LIST_HEAD(net_exit_list);
0552
0553
0554 net_kill_list = llist_del_all(&cleanup_list);
0555
0556 down_read(&pernet_ops_rwsem);
0557
0558
0559 down_write(&net_rwsem);
0560 llist_for_each_entry(net, net_kill_list, cleanup_list)
0561 list_del_rcu(&net->list);
0562
0563
0564
0565
0566
0567
0568
0569
0570
0571
0572 last = list_last_entry(&net_namespace_list, struct net, list);
0573 up_write(&net_rwsem);
0574
0575 llist_for_each_entry(net, net_kill_list, cleanup_list) {
0576 unhash_nsid(net, last);
0577 list_add_tail(&net->exit_list, &net_exit_list);
0578 }
0579
0580
0581 list_for_each_entry_reverse(ops, &pernet_list, list)
0582 ops_pre_exit_list(ops, &net_exit_list);
0583
0584
0585
0586
0587
0588
0589
0590 synchronize_rcu();
0591
0592
0593 list_for_each_entry_reverse(ops, &pernet_list, list)
0594 ops_exit_list(ops, &net_exit_list);
0595
0596
0597 list_for_each_entry_reverse(ops, &pernet_list, list)
0598 ops_free_list(ops, &net_exit_list);
0599
0600 up_read(&pernet_ops_rwsem);
0601
0602
0603
0604
0605 rcu_barrier();
0606
0607
0608 list_for_each_entry_safe(net, tmp, &net_exit_list, exit_list) {
0609 list_del_init(&net->exit_list);
0610 dec_net_namespaces(net->ucounts);
0611 #ifdef CONFIG_KEYS
0612 key_remove_domain(net->key_domain);
0613 #endif
0614 put_user_ns(net->user_ns);
0615 net_free(net);
0616 }
0617 }
0618
0619
0620
0621
0622
0623
0624
0625
0626
0627
0628 void net_ns_barrier(void)
0629 {
0630 down_write(&pernet_ops_rwsem);
0631 up_write(&pernet_ops_rwsem);
0632 }
0633 EXPORT_SYMBOL(net_ns_barrier);
0634
0635 static DECLARE_WORK(net_cleanup_work, cleanup_net);
0636
0637 void __put_net(struct net *net)
0638 {
0639 ref_tracker_dir_exit(&net->refcnt_tracker);
0640
0641 if (llist_add(&net->cleanup_list, &cleanup_list))
0642 queue_work(netns_wq, &net_cleanup_work);
0643 }
0644 EXPORT_SYMBOL_GPL(__put_net);
0645
0646
0647
0648
0649
0650
0651
0652 struct ns_common *get_net_ns(struct ns_common *ns)
0653 {
0654 return &get_net(container_of(ns, struct net, ns))->ns;
0655 }
0656 EXPORT_SYMBOL_GPL(get_net_ns);
0657
0658 struct net *get_net_ns_by_fd(int fd)
0659 {
0660 struct file *file;
0661 struct ns_common *ns;
0662 struct net *net;
0663
0664 file = proc_ns_fget(fd);
0665 if (IS_ERR(file))
0666 return ERR_CAST(file);
0667
0668 ns = get_proc_ns(file_inode(file));
0669 if (ns->ops == &netns_operations)
0670 net = get_net(container_of(ns, struct net, ns));
0671 else
0672 net = ERR_PTR(-EINVAL);
0673
0674 fput(file);
0675 return net;
0676 }
0677 EXPORT_SYMBOL_GPL(get_net_ns_by_fd);
0678 #endif
0679
0680 struct net *get_net_ns_by_pid(pid_t pid)
0681 {
0682 struct task_struct *tsk;
0683 struct net *net;
0684
0685
0686 net = ERR_PTR(-ESRCH);
0687 rcu_read_lock();
0688 tsk = find_task_by_vpid(pid);
0689 if (tsk) {
0690 struct nsproxy *nsproxy;
0691 task_lock(tsk);
0692 nsproxy = tsk->nsproxy;
0693 if (nsproxy)
0694 net = get_net(nsproxy->net_ns);
0695 task_unlock(tsk);
0696 }
0697 rcu_read_unlock();
0698 return net;
0699 }
0700 EXPORT_SYMBOL_GPL(get_net_ns_by_pid);
0701
0702 static __net_init int net_ns_net_init(struct net *net)
0703 {
0704 #ifdef CONFIG_NET_NS
0705 net->ns.ops = &netns_operations;
0706 #endif
0707 return ns_alloc_inum(&net->ns);
0708 }
0709
0710 static __net_exit void net_ns_net_exit(struct net *net)
0711 {
0712 ns_free_inum(&net->ns);
0713 }
0714
0715 static struct pernet_operations __net_initdata net_ns_ops = {
0716 .init = net_ns_net_init,
0717 .exit = net_ns_net_exit,
0718 };
0719
0720 static const struct nla_policy rtnl_net_policy[NETNSA_MAX + 1] = {
0721 [NETNSA_NONE] = { .type = NLA_UNSPEC },
0722 [NETNSA_NSID] = { .type = NLA_S32 },
0723 [NETNSA_PID] = { .type = NLA_U32 },
0724 [NETNSA_FD] = { .type = NLA_U32 },
0725 [NETNSA_TARGET_NSID] = { .type = NLA_S32 },
0726 };
0727
0728 static int rtnl_net_newid(struct sk_buff *skb, struct nlmsghdr *nlh,
0729 struct netlink_ext_ack *extack)
0730 {
0731 struct net *net = sock_net(skb->sk);
0732 struct nlattr *tb[NETNSA_MAX + 1];
0733 struct nlattr *nla;
0734 struct net *peer;
0735 int nsid, err;
0736
0737 err = nlmsg_parse_deprecated(nlh, sizeof(struct rtgenmsg), tb,
0738 NETNSA_MAX, rtnl_net_policy, extack);
0739 if (err < 0)
0740 return err;
0741 if (!tb[NETNSA_NSID]) {
0742 NL_SET_ERR_MSG(extack, "nsid is missing");
0743 return -EINVAL;
0744 }
0745 nsid = nla_get_s32(tb[NETNSA_NSID]);
0746
0747 if (tb[NETNSA_PID]) {
0748 peer = get_net_ns_by_pid(nla_get_u32(tb[NETNSA_PID]));
0749 nla = tb[NETNSA_PID];
0750 } else if (tb[NETNSA_FD]) {
0751 peer = get_net_ns_by_fd(nla_get_u32(tb[NETNSA_FD]));
0752 nla = tb[NETNSA_FD];
0753 } else {
0754 NL_SET_ERR_MSG(extack, "Peer netns reference is missing");
0755 return -EINVAL;
0756 }
0757 if (IS_ERR(peer)) {
0758 NL_SET_BAD_ATTR(extack, nla);
0759 NL_SET_ERR_MSG(extack, "Peer netns reference is invalid");
0760 return PTR_ERR(peer);
0761 }
0762
0763 spin_lock_bh(&net->nsid_lock);
0764 if (__peernet2id(net, peer) >= 0) {
0765 spin_unlock_bh(&net->nsid_lock);
0766 err = -EEXIST;
0767 NL_SET_BAD_ATTR(extack, nla);
0768 NL_SET_ERR_MSG(extack,
0769 "Peer netns already has a nsid assigned");
0770 goto out;
0771 }
0772
0773 err = alloc_netid(net, peer, nsid);
0774 spin_unlock_bh(&net->nsid_lock);
0775 if (err >= 0) {
0776 rtnl_net_notifyid(net, RTM_NEWNSID, err, NETLINK_CB(skb).portid,
0777 nlh, GFP_KERNEL);
0778 err = 0;
0779 } else if (err == -ENOSPC && nsid >= 0) {
0780 err = -EEXIST;
0781 NL_SET_BAD_ATTR(extack, tb[NETNSA_NSID]);
0782 NL_SET_ERR_MSG(extack, "The specified nsid is already used");
0783 }
0784 out:
0785 put_net(peer);
0786 return err;
0787 }
0788
0789 static int rtnl_net_get_size(void)
0790 {
0791 return NLMSG_ALIGN(sizeof(struct rtgenmsg))
0792 + nla_total_size(sizeof(s32))
0793 + nla_total_size(sizeof(s32))
0794 ;
0795 }
0796
0797 struct net_fill_args {
0798 u32 portid;
0799 u32 seq;
0800 int flags;
0801 int cmd;
0802 int nsid;
0803 bool add_ref;
0804 int ref_nsid;
0805 };
0806
0807 static int rtnl_net_fill(struct sk_buff *skb, struct net_fill_args *args)
0808 {
0809 struct nlmsghdr *nlh;
0810 struct rtgenmsg *rth;
0811
0812 nlh = nlmsg_put(skb, args->portid, args->seq, args->cmd, sizeof(*rth),
0813 args->flags);
0814 if (!nlh)
0815 return -EMSGSIZE;
0816
0817 rth = nlmsg_data(nlh);
0818 rth->rtgen_family = AF_UNSPEC;
0819
0820 if (nla_put_s32(skb, NETNSA_NSID, args->nsid))
0821 goto nla_put_failure;
0822
0823 if (args->add_ref &&
0824 nla_put_s32(skb, NETNSA_CURRENT_NSID, args->ref_nsid))
0825 goto nla_put_failure;
0826
0827 nlmsg_end(skb, nlh);
0828 return 0;
0829
0830 nla_put_failure:
0831 nlmsg_cancel(skb, nlh);
0832 return -EMSGSIZE;
0833 }
0834
0835 static int rtnl_net_valid_getid_req(struct sk_buff *skb,
0836 const struct nlmsghdr *nlh,
0837 struct nlattr **tb,
0838 struct netlink_ext_ack *extack)
0839 {
0840 int i, err;
0841
0842 if (!netlink_strict_get_check(skb))
0843 return nlmsg_parse_deprecated(nlh, sizeof(struct rtgenmsg),
0844 tb, NETNSA_MAX, rtnl_net_policy,
0845 extack);
0846
0847 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct rtgenmsg), tb,
0848 NETNSA_MAX, rtnl_net_policy,
0849 extack);
0850 if (err)
0851 return err;
0852
0853 for (i = 0; i <= NETNSA_MAX; i++) {
0854 if (!tb[i])
0855 continue;
0856
0857 switch (i) {
0858 case NETNSA_PID:
0859 case NETNSA_FD:
0860 case NETNSA_NSID:
0861 case NETNSA_TARGET_NSID:
0862 break;
0863 default:
0864 NL_SET_ERR_MSG(extack, "Unsupported attribute in peer netns getid request");
0865 return -EINVAL;
0866 }
0867 }
0868
0869 return 0;
0870 }
0871
0872 static int rtnl_net_getid(struct sk_buff *skb, struct nlmsghdr *nlh,
0873 struct netlink_ext_ack *extack)
0874 {
0875 struct net *net = sock_net(skb->sk);
0876 struct nlattr *tb[NETNSA_MAX + 1];
0877 struct net_fill_args fillargs = {
0878 .portid = NETLINK_CB(skb).portid,
0879 .seq = nlh->nlmsg_seq,
0880 .cmd = RTM_NEWNSID,
0881 };
0882 struct net *peer, *target = net;
0883 struct nlattr *nla;
0884 struct sk_buff *msg;
0885 int err;
0886
0887 err = rtnl_net_valid_getid_req(skb, nlh, tb, extack);
0888 if (err < 0)
0889 return err;
0890 if (tb[NETNSA_PID]) {
0891 peer = get_net_ns_by_pid(nla_get_u32(tb[NETNSA_PID]));
0892 nla = tb[NETNSA_PID];
0893 } else if (tb[NETNSA_FD]) {
0894 peer = get_net_ns_by_fd(nla_get_u32(tb[NETNSA_FD]));
0895 nla = tb[NETNSA_FD];
0896 } else if (tb[NETNSA_NSID]) {
0897 peer = get_net_ns_by_id(net, nla_get_s32(tb[NETNSA_NSID]));
0898 if (!peer)
0899 peer = ERR_PTR(-ENOENT);
0900 nla = tb[NETNSA_NSID];
0901 } else {
0902 NL_SET_ERR_MSG(extack, "Peer netns reference is missing");
0903 return -EINVAL;
0904 }
0905
0906 if (IS_ERR(peer)) {
0907 NL_SET_BAD_ATTR(extack, nla);
0908 NL_SET_ERR_MSG(extack, "Peer netns reference is invalid");
0909 return PTR_ERR(peer);
0910 }
0911
0912 if (tb[NETNSA_TARGET_NSID]) {
0913 int id = nla_get_s32(tb[NETNSA_TARGET_NSID]);
0914
0915 target = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, id);
0916 if (IS_ERR(target)) {
0917 NL_SET_BAD_ATTR(extack, tb[NETNSA_TARGET_NSID]);
0918 NL_SET_ERR_MSG(extack,
0919 "Target netns reference is invalid");
0920 err = PTR_ERR(target);
0921 goto out;
0922 }
0923 fillargs.add_ref = true;
0924 fillargs.ref_nsid = peernet2id(net, peer);
0925 }
0926
0927 msg = nlmsg_new(rtnl_net_get_size(), GFP_KERNEL);
0928 if (!msg) {
0929 err = -ENOMEM;
0930 goto out;
0931 }
0932
0933 fillargs.nsid = peernet2id(target, peer);
0934 err = rtnl_net_fill(msg, &fillargs);
0935 if (err < 0)
0936 goto err_out;
0937
0938 err = rtnl_unicast(msg, net, NETLINK_CB(skb).portid);
0939 goto out;
0940
0941 err_out:
0942 nlmsg_free(msg);
0943 out:
0944 if (fillargs.add_ref)
0945 put_net(target);
0946 put_net(peer);
0947 return err;
0948 }
0949
0950 struct rtnl_net_dump_cb {
0951 struct net *tgt_net;
0952 struct net *ref_net;
0953 struct sk_buff *skb;
0954 struct net_fill_args fillargs;
0955 int idx;
0956 int s_idx;
0957 };
0958
0959
0960 static int rtnl_net_dumpid_one(int id, void *peer, void *data)
0961 {
0962 struct rtnl_net_dump_cb *net_cb = (struct rtnl_net_dump_cb *)data;
0963 int ret;
0964
0965 if (net_cb->idx < net_cb->s_idx)
0966 goto cont;
0967
0968 net_cb->fillargs.nsid = id;
0969 if (net_cb->fillargs.add_ref)
0970 net_cb->fillargs.ref_nsid = __peernet2id(net_cb->ref_net, peer);
0971 ret = rtnl_net_fill(net_cb->skb, &net_cb->fillargs);
0972 if (ret < 0)
0973 return ret;
0974
0975 cont:
0976 net_cb->idx++;
0977 return 0;
0978 }
0979
0980 static int rtnl_valid_dump_net_req(const struct nlmsghdr *nlh, struct sock *sk,
0981 struct rtnl_net_dump_cb *net_cb,
0982 struct netlink_callback *cb)
0983 {
0984 struct netlink_ext_ack *extack = cb->extack;
0985 struct nlattr *tb[NETNSA_MAX + 1];
0986 int err, i;
0987
0988 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct rtgenmsg), tb,
0989 NETNSA_MAX, rtnl_net_policy,
0990 extack);
0991 if (err < 0)
0992 return err;
0993
0994 for (i = 0; i <= NETNSA_MAX; i++) {
0995 if (!tb[i])
0996 continue;
0997
0998 if (i == NETNSA_TARGET_NSID) {
0999 struct net *net;
1000
1001 net = rtnl_get_net_ns_capable(sk, nla_get_s32(tb[i]));
1002 if (IS_ERR(net)) {
1003 NL_SET_BAD_ATTR(extack, tb[i]);
1004 NL_SET_ERR_MSG(extack,
1005 "Invalid target network namespace id");
1006 return PTR_ERR(net);
1007 }
1008 net_cb->fillargs.add_ref = true;
1009 net_cb->ref_net = net_cb->tgt_net;
1010 net_cb->tgt_net = net;
1011 } else {
1012 NL_SET_BAD_ATTR(extack, tb[i]);
1013 NL_SET_ERR_MSG(extack,
1014 "Unsupported attribute in dump request");
1015 return -EINVAL;
1016 }
1017 }
1018
1019 return 0;
1020 }
1021
1022 static int rtnl_net_dumpid(struct sk_buff *skb, struct netlink_callback *cb)
1023 {
1024 struct rtnl_net_dump_cb net_cb = {
1025 .tgt_net = sock_net(skb->sk),
1026 .skb = skb,
1027 .fillargs = {
1028 .portid = NETLINK_CB(cb->skb).portid,
1029 .seq = cb->nlh->nlmsg_seq,
1030 .flags = NLM_F_MULTI,
1031 .cmd = RTM_NEWNSID,
1032 },
1033 .idx = 0,
1034 .s_idx = cb->args[0],
1035 };
1036 int err = 0;
1037
1038 if (cb->strict_check) {
1039 err = rtnl_valid_dump_net_req(cb->nlh, skb->sk, &net_cb, cb);
1040 if (err < 0)
1041 goto end;
1042 }
1043
1044 rcu_read_lock();
1045 idr_for_each(&net_cb.tgt_net->netns_ids, rtnl_net_dumpid_one, &net_cb);
1046 rcu_read_unlock();
1047
1048 cb->args[0] = net_cb.idx;
1049 end:
1050 if (net_cb.fillargs.add_ref)
1051 put_net(net_cb.tgt_net);
1052 return err < 0 ? err : skb->len;
1053 }
1054
1055 static void rtnl_net_notifyid(struct net *net, int cmd, int id, u32 portid,
1056 struct nlmsghdr *nlh, gfp_t gfp)
1057 {
1058 struct net_fill_args fillargs = {
1059 .portid = portid,
1060 .seq = nlh ? nlh->nlmsg_seq : 0,
1061 .cmd = cmd,
1062 .nsid = id,
1063 };
1064 struct sk_buff *msg;
1065 int err = -ENOMEM;
1066
1067 msg = nlmsg_new(rtnl_net_get_size(), gfp);
1068 if (!msg)
1069 goto out;
1070
1071 err = rtnl_net_fill(msg, &fillargs);
1072 if (err < 0)
1073 goto err_out;
1074
1075 rtnl_notify(msg, net, portid, RTNLGRP_NSID, nlh, gfp);
1076 return;
1077
1078 err_out:
1079 nlmsg_free(msg);
1080 out:
1081 rtnl_set_sk_err(net, RTNLGRP_NSID, err);
1082 }
1083
1084 void __init net_ns_init(void)
1085 {
1086 struct net_generic *ng;
1087
1088 #ifdef CONFIG_NET_NS
1089 net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
1090 SMP_CACHE_BYTES,
1091 SLAB_PANIC|SLAB_ACCOUNT, NULL);
1092
1093
1094 netns_wq = create_singlethread_workqueue("netns");
1095 if (!netns_wq)
1096 panic("Could not create netns workq");
1097 #endif
1098
1099 ng = net_alloc_generic();
1100 if (!ng)
1101 panic("Could not allocate generic netns");
1102
1103 rcu_assign_pointer(init_net.gen, ng);
1104
1105 #ifdef CONFIG_KEYS
1106 init_net.key_domain = &init_net_key_domain;
1107 #endif
1108 down_write(&pernet_ops_rwsem);
1109 if (setup_net(&init_net, &init_user_ns))
1110 panic("Could not setup the initial network namespace");
1111
1112 init_net_initialized = true;
1113 up_write(&pernet_ops_rwsem);
1114
1115 if (register_pernet_subsys(&net_ns_ops))
1116 panic("Could not register network namespace subsystems");
1117
1118 rtnl_register(PF_UNSPEC, RTM_NEWNSID, rtnl_net_newid, NULL,
1119 RTNL_FLAG_DOIT_UNLOCKED);
1120 rtnl_register(PF_UNSPEC, RTM_GETNSID, rtnl_net_getid, rtnl_net_dumpid,
1121 RTNL_FLAG_DOIT_UNLOCKED);
1122 }
1123
1124 static void free_exit_list(struct pernet_operations *ops, struct list_head *net_exit_list)
1125 {
1126 ops_pre_exit_list(ops, net_exit_list);
1127 synchronize_rcu();
1128 ops_exit_list(ops, net_exit_list);
1129 ops_free_list(ops, net_exit_list);
1130 }
1131
1132 #ifdef CONFIG_NET_NS
1133 static int __register_pernet_operations(struct list_head *list,
1134 struct pernet_operations *ops)
1135 {
1136 struct net *net;
1137 int error;
1138 LIST_HEAD(net_exit_list);
1139
1140 list_add_tail(&ops->list, list);
1141 if (ops->init || (ops->id && ops->size)) {
1142
1143
1144
1145 for_each_net(net) {
1146 error = ops_init(ops, net);
1147 if (error)
1148 goto out_undo;
1149 list_add_tail(&net->exit_list, &net_exit_list);
1150 }
1151 }
1152 return 0;
1153
1154 out_undo:
1155
1156 list_del(&ops->list);
1157 free_exit_list(ops, &net_exit_list);
1158 return error;
1159 }
1160
1161 static void __unregister_pernet_operations(struct pernet_operations *ops)
1162 {
1163 struct net *net;
1164 LIST_HEAD(net_exit_list);
1165
1166 list_del(&ops->list);
1167
1168 for_each_net(net)
1169 list_add_tail(&net->exit_list, &net_exit_list);
1170
1171 free_exit_list(ops, &net_exit_list);
1172 }
1173
1174 #else
1175
1176 static int __register_pernet_operations(struct list_head *list,
1177 struct pernet_operations *ops)
1178 {
1179 if (!init_net_initialized) {
1180 list_add_tail(&ops->list, list);
1181 return 0;
1182 }
1183
1184 return ops_init(ops, &init_net);
1185 }
1186
1187 static void __unregister_pernet_operations(struct pernet_operations *ops)
1188 {
1189 if (!init_net_initialized) {
1190 list_del(&ops->list);
1191 } else {
1192 LIST_HEAD(net_exit_list);
1193 list_add(&init_net.exit_list, &net_exit_list);
1194 free_exit_list(ops, &net_exit_list);
1195 }
1196 }
1197
1198 #endif
1199
1200 static DEFINE_IDA(net_generic_ids);
1201
1202 static int register_pernet_operations(struct list_head *list,
1203 struct pernet_operations *ops)
1204 {
1205 int error;
1206
1207 if (ops->id) {
1208 error = ida_alloc_min(&net_generic_ids, MIN_PERNET_OPS_ID,
1209 GFP_KERNEL);
1210 if (error < 0)
1211 return error;
1212 *ops->id = error;
1213 max_gen_ptrs = max(max_gen_ptrs, *ops->id + 1);
1214 }
1215 error = __register_pernet_operations(list, ops);
1216 if (error) {
1217 rcu_barrier();
1218 if (ops->id)
1219 ida_free(&net_generic_ids, *ops->id);
1220 }
1221
1222 return error;
1223 }
1224
1225 static void unregister_pernet_operations(struct pernet_operations *ops)
1226 {
1227 __unregister_pernet_operations(ops);
1228 rcu_barrier();
1229 if (ops->id)
1230 ida_free(&net_generic_ids, *ops->id);
1231 }
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252 int register_pernet_subsys(struct pernet_operations *ops)
1253 {
1254 int error;
1255 down_write(&pernet_ops_rwsem);
1256 error = register_pernet_operations(first_device, ops);
1257 up_write(&pernet_ops_rwsem);
1258 return error;
1259 }
1260 EXPORT_SYMBOL_GPL(register_pernet_subsys);
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271 void unregister_pernet_subsys(struct pernet_operations *ops)
1272 {
1273 down_write(&pernet_ops_rwsem);
1274 unregister_pernet_operations(ops);
1275 up_write(&pernet_ops_rwsem);
1276 }
1277 EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298 int register_pernet_device(struct pernet_operations *ops)
1299 {
1300 int error;
1301 down_write(&pernet_ops_rwsem);
1302 error = register_pernet_operations(&pernet_list, ops);
1303 if (!error && (first_device == &pernet_list))
1304 first_device = &ops->list;
1305 up_write(&pernet_ops_rwsem);
1306 return error;
1307 }
1308 EXPORT_SYMBOL_GPL(register_pernet_device);
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319 void unregister_pernet_device(struct pernet_operations *ops)
1320 {
1321 down_write(&pernet_ops_rwsem);
1322 if (&ops->list == first_device)
1323 first_device = first_device->next;
1324 unregister_pernet_operations(ops);
1325 up_write(&pernet_ops_rwsem);
1326 }
1327 EXPORT_SYMBOL_GPL(unregister_pernet_device);
1328
1329 #ifdef CONFIG_NET_NS
1330 static struct ns_common *netns_get(struct task_struct *task)
1331 {
1332 struct net *net = NULL;
1333 struct nsproxy *nsproxy;
1334
1335 task_lock(task);
1336 nsproxy = task->nsproxy;
1337 if (nsproxy)
1338 net = get_net(nsproxy->net_ns);
1339 task_unlock(task);
1340
1341 return net ? &net->ns : NULL;
1342 }
1343
1344 static inline struct net *to_net_ns(struct ns_common *ns)
1345 {
1346 return container_of(ns, struct net, ns);
1347 }
1348
1349 static void netns_put(struct ns_common *ns)
1350 {
1351 put_net(to_net_ns(ns));
1352 }
1353
1354 static int netns_install(struct nsset *nsset, struct ns_common *ns)
1355 {
1356 struct nsproxy *nsproxy = nsset->nsproxy;
1357 struct net *net = to_net_ns(ns);
1358
1359 if (!ns_capable(net->user_ns, CAP_SYS_ADMIN) ||
1360 !ns_capable(nsset->cred->user_ns, CAP_SYS_ADMIN))
1361 return -EPERM;
1362
1363 put_net(nsproxy->net_ns);
1364 nsproxy->net_ns = get_net(net);
1365 return 0;
1366 }
1367
1368 static struct user_namespace *netns_owner(struct ns_common *ns)
1369 {
1370 return to_net_ns(ns)->user_ns;
1371 }
1372
1373 const struct proc_ns_operations netns_operations = {
1374 .name = "net",
1375 .type = CLONE_NEWNET,
1376 .get = netns_get,
1377 .put = netns_put,
1378 .install = netns_install,
1379 .owner = netns_owner,
1380 };
1381 #endif