Joe Perches | e005d19 | 2012-05-16 19:58:40 +0000 | [diff] [blame] | 1 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 2 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 3 | #include <linux/workqueue.h> |
| 4 | #include <linux/rtnetlink.h> |
| 5 | #include <linux/cache.h> |
| 6 | #include <linux/slab.h> |
| 7 | #include <linux/list.h> |
| 8 | #include <linux/delay.h> |
Eric W. Biederman | 9dd776b | 2007-09-26 22:04:26 -0700 | [diff] [blame] | 9 | #include <linux/sched.h> |
Pavel Emelyanov | c93cf61 | 2008-04-15 00:35:23 -0700 | [diff] [blame] | 10 | #include <linux/idr.h> |
Johannes Berg | 11a28d3 | 2009-07-10 09:51:33 +0000 | [diff] [blame] | 11 | #include <linux/rculist.h> |
Johannes Berg | 30ffee8 | 2009-07-10 09:51:35 +0000 | [diff] [blame] | 12 | #include <linux/nsproxy.h> |
David Howells | 0bb80f2 | 2013-04-12 01:50:06 +0100 | [diff] [blame] | 13 | #include <linux/fs.h> |
| 14 | #include <linux/proc_ns.h> |
Eric W. Biederman | f063052 | 2011-05-04 17:51:50 -0700 | [diff] [blame] | 15 | #include <linux/file.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 16 | #include <linux/export.h> |
Eric W. Biederman | 038e733 | 2012-06-14 02:31:10 -0700 | [diff] [blame] | 17 | #include <linux/user_namespace.h> |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 18 | #include <linux/net_namespace.h> |
Ingo Molnar | f719ff9b | 2017-02-06 10:57:33 +0100 | [diff] [blame] | 19 | #include <linux/sched/task.h> |
| 20 | |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 21 | #include <net/sock.h> |
| 22 | #include <net/netlink.h> |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 23 | #include <net/net_namespace.h> |
Pavel Emelyanov | dec827d | 2008-04-15 00:36:08 -0700 | [diff] [blame] | 24 | #include <net/netns/generic.h> |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 25 | |
| 26 | /* |
| 27 | * Our network namespace constructor/destructor lists |
| 28 | */ |
| 29 | |
| 30 | static LIST_HEAD(pernet_list); |
| 31 | static struct list_head *first_device = &pernet_list; |
Cong Wang | 200b916 | 2014-05-12 15:11:20 -0700 | [diff] [blame] | 32 | DEFINE_MUTEX(net_mutex); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 33 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 34 | LIST_HEAD(net_namespace_list); |
Alexey Dobriyan | b76a461 | 2008-10-08 11:35:06 +0200 | [diff] [blame] | 35 | EXPORT_SYMBOL_GPL(net_namespace_list); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 36 | |
Rustad, Mark D | 734b654 | 2012-07-18 09:06:07 +0000 | [diff] [blame] | 37 | struct net init_net = { |
Kirill Tkhai | 273c28b | 2018-01-12 18:28:31 +0300 | [diff] [blame] | 38 | .count = REFCOUNT_INIT(1), |
David Howells | b5082df | 2017-04-27 22:40:23 +0100 | [diff] [blame] | 39 | .dev_base_head = LIST_HEAD_INIT(init_net.dev_base_head), |
Rustad, Mark D | 734b654 | 2012-07-18 09:06:07 +0000 | [diff] [blame] | 40 | }; |
Denis V. Lunev | ff4b950 | 2008-01-22 22:05:33 -0800 | [diff] [blame] | 41 | EXPORT_SYMBOL(init_net); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 42 | |
Dmitry Torokhov | f8c46cb | 2016-08-10 14:36:00 -0700 | [diff] [blame] | 43 | static bool init_net_initialized; |
Kirill Tkhai | 1a57feb | 2018-02-13 12:26:23 +0300 | [diff] [blame] | 44 | /* |
| 45 | * net_sem: protects: pernet_list, net_generic_ids, |
| 46 | * init_net_initialized and first_device pointer. |
| 47 | */ |
| 48 | DECLARE_RWSEM(net_sem); |
Dmitry Torokhov | f8c46cb | 2016-08-10 14:36:00 -0700 | [diff] [blame] | 49 | |
Alexey Dobriyan | 6af2d5f | 2016-12-02 04:21:32 +0300 | [diff] [blame] | 50 | #define MIN_PERNET_OPS_ID \ |
| 51 | ((sizeof(struct net_generic) + sizeof(void *) - 1) / sizeof(void *)) |
| 52 | |
Pavel Emelyanov | dec827d | 2008-04-15 00:36:08 -0700 | [diff] [blame] | 53 | #define INITIAL_NET_GEN_PTRS 13 /* +1 for len +2 for rcu_head */ |
| 54 | |
Eric Dumazet | 073862b | 2012-01-26 00:41:38 +0000 | [diff] [blame] | 55 | static unsigned int max_gen_ptrs = INITIAL_NET_GEN_PTRS; |
| 56 | |
| 57 | static struct net_generic *net_alloc_generic(void) |
| 58 | { |
| 59 | struct net_generic *ng; |
Alexey Dobriyan | 6af2d5f | 2016-12-02 04:21:32 +0300 | [diff] [blame] | 60 | unsigned int generic_size = offsetof(struct net_generic, ptr[max_gen_ptrs]); |
Eric Dumazet | 073862b | 2012-01-26 00:41:38 +0000 | [diff] [blame] | 61 | |
| 62 | ng = kzalloc(generic_size, GFP_KERNEL); |
| 63 | if (ng) |
Alexey Dobriyan | 9bfc7b9 | 2016-12-02 04:12:58 +0300 | [diff] [blame] | 64 | ng->s.len = max_gen_ptrs; |
Eric Dumazet | 073862b | 2012-01-26 00:41:38 +0000 | [diff] [blame] | 65 | |
| 66 | return ng; |
| 67 | } |
| 68 | |
Alexey Dobriyan | c7d03a0 | 2016-11-17 04:58:21 +0300 | [diff] [blame] | 69 | static int net_assign_generic(struct net *net, unsigned int id, void *data) |
Jiri Pirko | 05fceb4 | 2010-04-23 01:40:47 +0000 | [diff] [blame] | 70 | { |
| 71 | struct net_generic *ng, *old_ng; |
| 72 | |
| 73 | BUG_ON(!mutex_is_locked(&net_mutex)); |
Alexey Dobriyan | 6af2d5f | 2016-12-02 04:21:32 +0300 | [diff] [blame] | 74 | BUG_ON(id < MIN_PERNET_OPS_ID); |
Jiri Pirko | 05fceb4 | 2010-04-23 01:40:47 +0000 | [diff] [blame] | 75 | |
Eric Dumazet | 1c87733 | 2010-10-25 03:20:11 +0000 | [diff] [blame] | 76 | old_ng = rcu_dereference_protected(net->gen, |
| 77 | lockdep_is_held(&net_mutex)); |
Alexey Dobriyan | 6af2d5f | 2016-12-02 04:21:32 +0300 | [diff] [blame] | 78 | if (old_ng->s.len > id) { |
| 79 | old_ng->ptr[id] = data; |
Alexey Dobriyan | 1a9a0592 | 2016-12-02 04:11:34 +0300 | [diff] [blame] | 80 | return 0; |
| 81 | } |
Jiri Pirko | 05fceb4 | 2010-04-23 01:40:47 +0000 | [diff] [blame] | 82 | |
Eric Dumazet | 073862b | 2012-01-26 00:41:38 +0000 | [diff] [blame] | 83 | ng = net_alloc_generic(); |
Jiri Pirko | 05fceb4 | 2010-04-23 01:40:47 +0000 | [diff] [blame] | 84 | if (ng == NULL) |
| 85 | return -ENOMEM; |
| 86 | |
| 87 | /* |
| 88 | * Some synchronisation notes: |
| 89 | * |
| 90 | * The net_generic explores the net->gen array inside rcu |
| 91 | * read section. Besides once set the net->gen->ptr[x] |
| 92 | * pointer never changes (see rules in netns/generic.h). |
| 93 | * |
| 94 | * That said, we simply duplicate this array and schedule |
| 95 | * the old copy for kfree after a grace period. |
| 96 | */ |
| 97 | |
Alexey Dobriyan | 6af2d5f | 2016-12-02 04:21:32 +0300 | [diff] [blame] | 98 | memcpy(&ng->ptr[MIN_PERNET_OPS_ID], &old_ng->ptr[MIN_PERNET_OPS_ID], |
| 99 | (old_ng->s.len - MIN_PERNET_OPS_ID) * sizeof(void *)); |
| 100 | ng->ptr[id] = data; |
Jiri Pirko | 05fceb4 | 2010-04-23 01:40:47 +0000 | [diff] [blame] | 101 | |
| 102 | rcu_assign_pointer(net->gen, ng); |
Alexey Dobriyan | 9bfc7b9 | 2016-12-02 04:12:58 +0300 | [diff] [blame] | 103 | kfree_rcu(old_ng, s.rcu); |
Jiri Pirko | 05fceb4 | 2010-04-23 01:40:47 +0000 | [diff] [blame] | 104 | return 0; |
| 105 | } |
| 106 | |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 107 | static int ops_init(const struct pernet_operations *ops, struct net *net) |
| 108 | { |
Julian Anastasov | b922934 | 2012-04-16 04:43:15 +0000 | [diff] [blame] | 109 | int err = -ENOMEM; |
| 110 | void *data = NULL; |
| 111 | |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 112 | if (ops->id && ops->size) { |
Julian Anastasov | b922934 | 2012-04-16 04:43:15 +0000 | [diff] [blame] | 113 | data = kzalloc(ops->size, GFP_KERNEL); |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 114 | if (!data) |
Julian Anastasov | b922934 | 2012-04-16 04:43:15 +0000 | [diff] [blame] | 115 | goto out; |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 116 | |
| 117 | err = net_assign_generic(net, *ops->id, data); |
Julian Anastasov | b922934 | 2012-04-16 04:43:15 +0000 | [diff] [blame] | 118 | if (err) |
| 119 | goto cleanup; |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 120 | } |
Julian Anastasov | b922934 | 2012-04-16 04:43:15 +0000 | [diff] [blame] | 121 | err = 0; |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 122 | if (ops->init) |
Julian Anastasov | b922934 | 2012-04-16 04:43:15 +0000 | [diff] [blame] | 123 | err = ops->init(net); |
| 124 | if (!err) |
| 125 | return 0; |
| 126 | |
| 127 | cleanup: |
| 128 | kfree(data); |
| 129 | |
| 130 | out: |
| 131 | return err; |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | static void ops_free(const struct pernet_operations *ops, struct net *net) |
| 135 | { |
| 136 | if (ops->id && ops->size) { |
Alexey Dobriyan | c7d03a0 | 2016-11-17 04:58:21 +0300 | [diff] [blame] | 137 | kfree(net_generic(net, *ops->id)); |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
Eric W. Biederman | 72ad937 | 2009-12-03 02:29:03 +0000 | [diff] [blame] | 141 | static void ops_exit_list(const struct pernet_operations *ops, |
| 142 | struct list_head *net_exit_list) |
| 143 | { |
| 144 | struct net *net; |
| 145 | if (ops->exit) { |
| 146 | list_for_each_entry(net, net_exit_list, exit_list) |
| 147 | ops->exit(net); |
| 148 | } |
Eric W. Biederman | 72ad937 | 2009-12-03 02:29:03 +0000 | [diff] [blame] | 149 | if (ops->exit_batch) |
| 150 | ops->exit_batch(net_exit_list); |
| 151 | } |
| 152 | |
| 153 | static void ops_free_list(const struct pernet_operations *ops, |
| 154 | struct list_head *net_exit_list) |
| 155 | { |
| 156 | struct net *net; |
| 157 | if (ops->size && ops->id) { |
| 158 | list_for_each_entry(net, net_exit_list, exit_list) |
| 159 | ops_free(ops, net); |
| 160 | } |
| 161 | } |
| 162 | |
Nicolas Dichtel | 95f3841 | 2015-05-07 11:02:51 +0200 | [diff] [blame] | 163 | /* should be called with nsid_lock held */ |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 164 | static int alloc_netid(struct net *net, struct net *peer, int reqid) |
| 165 | { |
Nicolas Dichtel | 3138dbf | 2015-05-07 11:02:50 +0200 | [diff] [blame] | 166 | int min = 0, max = 0; |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 167 | |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 168 | if (reqid >= 0) { |
| 169 | min = reqid; |
| 170 | max = reqid + 1; |
| 171 | } |
| 172 | |
Nicolas Dichtel | 95f3841 | 2015-05-07 11:02:51 +0200 | [diff] [blame] | 173 | return idr_alloc(&net->netns_ids, peer, min, max, GFP_ATOMIC); |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /* This function is used by idr_for_each(). If net is equal to peer, the |
| 177 | * function returns the id so that idr_for_each() stops. Because we cannot |
| 178 | * returns the id 0 (idr_for_each() will not stop), we return the magic value |
| 179 | * NET_ID_ZERO (-1) for it. |
| 180 | */ |
| 181 | #define NET_ID_ZERO -1 |
| 182 | static int net_eq_idr(int id, void *net, void *peer) |
| 183 | { |
| 184 | if (net_eq(net, peer)) |
| 185 | return id ? : NET_ID_ZERO; |
| 186 | return 0; |
| 187 | } |
| 188 | |
Nicolas Dichtel | 95f3841 | 2015-05-07 11:02:51 +0200 | [diff] [blame] | 189 | /* Should be called with nsid_lock held. If a new id is assigned, the bool alloc |
| 190 | * is set to true, thus the caller knows that the new id must be notified via |
| 191 | * rtnl. |
| 192 | */ |
Nicolas Dichtel | 3138dbf | 2015-05-07 11:02:50 +0200 | [diff] [blame] | 193 | static int __peernet2id_alloc(struct net *net, struct net *peer, bool *alloc) |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 194 | { |
| 195 | int id = idr_for_each(&net->netns_ids, net_eq_idr, peer); |
Nicolas Dichtel | 3138dbf | 2015-05-07 11:02:50 +0200 | [diff] [blame] | 196 | bool alloc_it = *alloc; |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 197 | |
Nicolas Dichtel | 3138dbf | 2015-05-07 11:02:50 +0200 | [diff] [blame] | 198 | *alloc = false; |
| 199 | |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 200 | /* Magic value for id 0. */ |
| 201 | if (id == NET_ID_ZERO) |
| 202 | return 0; |
| 203 | if (id > 0) |
| 204 | return id; |
| 205 | |
Nicolas Dichtel | 3138dbf | 2015-05-07 11:02:50 +0200 | [diff] [blame] | 206 | if (alloc_it) { |
Nicolas Dichtel | 109582af | 2015-05-07 11:02:47 +0200 | [diff] [blame] | 207 | id = alloc_netid(net, peer, -1); |
Nicolas Dichtel | 3138dbf | 2015-05-07 11:02:50 +0200 | [diff] [blame] | 208 | *alloc = true; |
Nicolas Dichtel | 109582af | 2015-05-07 11:02:47 +0200 | [diff] [blame] | 209 | return id >= 0 ? id : NETNSA_NSID_NOT_ASSIGNED; |
| 210 | } |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 211 | |
Nicolas Dichtel | 109582af | 2015-05-07 11:02:47 +0200 | [diff] [blame] | 212 | return NETNSA_NSID_NOT_ASSIGNED; |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 213 | } |
| 214 | |
Nicolas Dichtel | 95f3841 | 2015-05-07 11:02:51 +0200 | [diff] [blame] | 215 | /* should be called with nsid_lock held */ |
Nicolas Dichtel | 3138dbf | 2015-05-07 11:02:50 +0200 | [diff] [blame] | 216 | static int __peernet2id(struct net *net, struct net *peer) |
| 217 | { |
| 218 | bool no = false; |
| 219 | |
| 220 | return __peernet2id_alloc(net, peer, &no); |
| 221 | } |
| 222 | |
| 223 | static void rtnl_net_notifyid(struct net *net, int cmd, int id); |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 224 | /* This function returns the id of a peer netns. If no id is assigned, one will |
| 225 | * be allocated and returned. |
| 226 | */ |
Nicolas Dichtel | 7a0877d | 2015-05-07 11:02:49 +0200 | [diff] [blame] | 227 | int peernet2id_alloc(struct net *net, struct net *peer) |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 228 | { |
Kirill Tkhai | 0c06bea | 2018-01-16 12:31:41 +0300 | [diff] [blame] | 229 | bool alloc = false, alive = false; |
Nicolas Dichtel | 3138dbf | 2015-05-07 11:02:50 +0200 | [diff] [blame] | 230 | int id; |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 231 | |
Kirill Tkhai | 273c28b | 2018-01-12 18:28:31 +0300 | [diff] [blame] | 232 | if (refcount_read(&net->count) == 0) |
WANG Cong | cfc44a4 | 2016-11-16 10:27:02 -0800 | [diff] [blame] | 233 | return NETNSA_NSID_NOT_ASSIGNED; |
Paul Moore | fba143c | 2016-11-29 16:57:48 -0500 | [diff] [blame] | 234 | spin_lock_bh(&net->nsid_lock); |
Kirill Tkhai | 0c06bea | 2018-01-16 12:31:41 +0300 | [diff] [blame] | 235 | /* |
| 236 | * When peer is obtained from RCU lists, we may race with |
| 237 | * its cleanup. Check whether it's alive, and this guarantees |
| 238 | * we never hash a peer back to net->netns_ids, after it has |
| 239 | * just been idr_remove()'d from there in cleanup_net(). |
| 240 | */ |
| 241 | if (maybe_get_net(peer)) |
| 242 | alive = alloc = true; |
Nicolas Dichtel | 3138dbf | 2015-05-07 11:02:50 +0200 | [diff] [blame] | 243 | id = __peernet2id_alloc(net, peer, &alloc); |
Paul Moore | fba143c | 2016-11-29 16:57:48 -0500 | [diff] [blame] | 244 | spin_unlock_bh(&net->nsid_lock); |
Nicolas Dichtel | 3138dbf | 2015-05-07 11:02:50 +0200 | [diff] [blame] | 245 | if (alloc && id >= 0) |
| 246 | rtnl_net_notifyid(net, RTM_NEWNSID, id); |
Kirill Tkhai | 0c06bea | 2018-01-16 12:31:41 +0300 | [diff] [blame] | 247 | if (alive) |
| 248 | put_net(peer); |
Nicolas Dichtel | 3138dbf | 2015-05-07 11:02:50 +0200 | [diff] [blame] | 249 | return id; |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 250 | } |
Jiri Benc | 7cbebc8 | 2017-11-02 17:04:36 -0200 | [diff] [blame] | 251 | EXPORT_SYMBOL_GPL(peernet2id_alloc); |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 252 | |
Nicolas Dichtel | 95f3841 | 2015-05-07 11:02:51 +0200 | [diff] [blame] | 253 | /* This function returns, if assigned, the id of a peer netns. */ |
Nicolas Dichtel | 59324cf | 2015-05-07 11:02:53 +0200 | [diff] [blame] | 254 | int peernet2id(struct net *net, struct net *peer) |
Nicolas Dichtel | 95f3841 | 2015-05-07 11:02:51 +0200 | [diff] [blame] | 255 | { |
Nicolas Dichtel | 95f3841 | 2015-05-07 11:02:51 +0200 | [diff] [blame] | 256 | int id; |
| 257 | |
Paul Moore | fba143c | 2016-11-29 16:57:48 -0500 | [diff] [blame] | 258 | spin_lock_bh(&net->nsid_lock); |
Nicolas Dichtel | 95f3841 | 2015-05-07 11:02:51 +0200 | [diff] [blame] | 259 | id = __peernet2id(net, peer); |
Paul Moore | fba143c | 2016-11-29 16:57:48 -0500 | [diff] [blame] | 260 | spin_unlock_bh(&net->nsid_lock); |
Nicolas Dichtel | 95f3841 | 2015-05-07 11:02:51 +0200 | [diff] [blame] | 261 | return id; |
| 262 | } |
WANG Cong | 38f507f | 2016-09-01 21:53:44 -0700 | [diff] [blame] | 263 | EXPORT_SYMBOL(peernet2id); |
Nicolas Dichtel | 95f3841 | 2015-05-07 11:02:51 +0200 | [diff] [blame] | 264 | |
Nicolas Dichtel | 59324cf | 2015-05-07 11:02:53 +0200 | [diff] [blame] | 265 | /* This function returns true is the peer netns has an id assigned into the |
| 266 | * current netns. |
| 267 | */ |
| 268 | bool peernet_has_id(struct net *net, struct net *peer) |
| 269 | { |
| 270 | return peernet2id(net, peer) >= 0; |
| 271 | } |
| 272 | |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 273 | struct net *get_net_ns_by_id(struct net *net, int id) |
| 274 | { |
| 275 | struct net *peer; |
| 276 | |
| 277 | if (id < 0) |
| 278 | return NULL; |
| 279 | |
| 280 | rcu_read_lock(); |
| 281 | peer = idr_find(&net->netns_ids, id); |
| 282 | if (peer) |
Eric W. Biederman | 21b5944 | 2017-12-19 11:27:56 -0600 | [diff] [blame] | 283 | peer = maybe_get_net(peer); |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 284 | rcu_read_unlock(); |
| 285 | |
| 286 | return peer; |
| 287 | } |
| 288 | |
Pavel Emelyanov | 6a1a3b9 | 2007-11-01 00:44:50 -0700 | [diff] [blame] | 289 | /* |
| 290 | * setup_net runs the initializers for the network namespace object. |
| 291 | */ |
Eric W. Biederman | 038e733 | 2012-06-14 02:31:10 -0700 | [diff] [blame] | 292 | static __net_init int setup_net(struct net *net, struct user_namespace *user_ns) |
Pavel Emelyanov | 6a1a3b9 | 2007-11-01 00:44:50 -0700 | [diff] [blame] | 293 | { |
Kirill Tkhai | 1a57feb | 2018-02-13 12:26:23 +0300 | [diff] [blame] | 294 | /* Must be called with net_sem held */ |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 295 | const struct pernet_operations *ops, *saved_ops; |
Daniel Lezcano | 486a87f | 2009-02-22 00:07:53 -0800 | [diff] [blame] | 296 | int error = 0; |
Eric W. Biederman | 72ad937 | 2009-12-03 02:29:03 +0000 | [diff] [blame] | 297 | LIST_HEAD(net_exit_list); |
Pavel Emelyanov | 6a1a3b9 | 2007-11-01 00:44:50 -0700 | [diff] [blame] | 298 | |
Kirill Tkhai | 273c28b | 2018-01-12 18:28:31 +0300 | [diff] [blame] | 299 | refcount_set(&net->count, 1); |
Reshetova, Elena | c122e14 | 2017-06-30 13:08:08 +0300 | [diff] [blame] | 300 | refcount_set(&net->passive, 1); |
Thomas Graf | 4e985ad | 2011-06-21 03:11:20 +0000 | [diff] [blame] | 301 | net->dev_base_seq = 1; |
Eric W. Biederman | 038e733 | 2012-06-14 02:31:10 -0700 | [diff] [blame] | 302 | net->user_ns = user_ns; |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 303 | idr_init(&net->netns_ids); |
WANG Cong | de13346 | 2015-05-15 14:47:32 -0700 | [diff] [blame] | 304 | spin_lock_init(&net->nsid_lock); |
Daniel Lezcano | 486a87f | 2009-02-22 00:07:53 -0800 | [diff] [blame] | 305 | |
Pavel Emelyanov | 6a1a3b9 | 2007-11-01 00:44:50 -0700 | [diff] [blame] | 306 | list_for_each_entry(ops, &pernet_list, list) { |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 307 | error = ops_init(ops, net); |
| 308 | if (error < 0) |
| 309 | goto out_undo; |
Pavel Emelyanov | 6a1a3b9 | 2007-11-01 00:44:50 -0700 | [diff] [blame] | 310 | } |
Kirill Tkhai | 98f6c53 | 2018-02-13 12:26:02 +0300 | [diff] [blame] | 311 | rtnl_lock(); |
| 312 | list_add_tail_rcu(&net->list, &net_namespace_list); |
| 313 | rtnl_unlock(); |
Pavel Emelyanov | 6a1a3b9 | 2007-11-01 00:44:50 -0700 | [diff] [blame] | 314 | out: |
| 315 | return error; |
| 316 | |
| 317 | out_undo: |
| 318 | /* Walk through the list backwards calling the exit functions |
| 319 | * for the pernet modules whose init functions did not fail. |
| 320 | */ |
Eric W. Biederman | 72ad937 | 2009-12-03 02:29:03 +0000 | [diff] [blame] | 321 | list_add(&net->exit_list, &net_exit_list); |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 322 | saved_ops = ops; |
Eric W. Biederman | 72ad937 | 2009-12-03 02:29:03 +0000 | [diff] [blame] | 323 | list_for_each_entry_continue_reverse(ops, &pernet_list, list) |
| 324 | ops_exit_list(ops, &net_exit_list); |
| 325 | |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 326 | ops = saved_ops; |
| 327 | list_for_each_entry_continue_reverse(ops, &pernet_list, list) |
Eric W. Biederman | 72ad937 | 2009-12-03 02:29:03 +0000 | [diff] [blame] | 328 | ops_free_list(ops, &net_exit_list); |
Pavel Emelyanov | 6a1a3b9 | 2007-11-01 00:44:50 -0700 | [diff] [blame] | 329 | |
| 330 | rcu_barrier(); |
| 331 | goto out; |
| 332 | } |
| 333 | |
Roman Kapl | 7c3f1875 | 2017-05-24 10:22:22 +0200 | [diff] [blame] | 334 | static int __net_init net_defaults_init_net(struct net *net) |
| 335 | { |
| 336 | net->core.sysctl_somaxconn = SOMAXCONN; |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | static struct pernet_operations net_defaults_ops = { |
| 341 | .init = net_defaults_init_net, |
| 342 | }; |
| 343 | |
| 344 | static __init int net_defaults_init(void) |
| 345 | { |
| 346 | if (register_pernet_subsys(&net_defaults_ops)) |
| 347 | panic("Cannot initialize net default settings"); |
| 348 | |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | core_initcall(net_defaults_init); |
Daniel Lezcano | 486a87f | 2009-02-22 00:07:53 -0800 | [diff] [blame] | 353 | |
Clemens Noss | ebe47d4 | 2009-02-23 15:37:35 -0800 | [diff] [blame] | 354 | #ifdef CONFIG_NET_NS |
Arnd Bergmann | 2ed6afd | 2016-09-23 18:06:12 +0200 | [diff] [blame] | 355 | static struct ucounts *inc_net_namespaces(struct user_namespace *ns) |
| 356 | { |
| 357 | return inc_ucount(ns, current_euid(), UCOUNT_NET_NAMESPACES); |
| 358 | } |
| 359 | |
| 360 | static void dec_net_namespaces(struct ucounts *ucounts) |
| 361 | { |
| 362 | dec_ucount(ucounts, UCOUNT_NET_NAMESPACES); |
| 363 | } |
| 364 | |
Clemens Noss | ebe47d4 | 2009-02-23 15:37:35 -0800 | [diff] [blame] | 365 | static struct kmem_cache *net_cachep; |
| 366 | static struct workqueue_struct *netns_wq; |
| 367 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 368 | static struct net *net_alloc(void) |
| 369 | { |
Daniel Lezcano | 486a87f | 2009-02-22 00:07:53 -0800 | [diff] [blame] | 370 | struct net *net = NULL; |
| 371 | struct net_generic *ng; |
| 372 | |
| 373 | ng = net_alloc_generic(); |
| 374 | if (!ng) |
| 375 | goto out; |
| 376 | |
| 377 | net = kmem_cache_zalloc(net_cachep, GFP_KERNEL); |
| 378 | if (!net) |
| 379 | goto out_free; |
| 380 | |
| 381 | rcu_assign_pointer(net->gen, ng); |
| 382 | out: |
| 383 | return net; |
| 384 | |
| 385 | out_free: |
| 386 | kfree(ng); |
| 387 | goto out; |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 388 | } |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 389 | |
Johann Felix Soden | 45a19b0 | 2007-11-07 01:30:30 -0800 | [diff] [blame] | 390 | static void net_free(struct net *net) |
| 391 | { |
Eric Dumazet | 416c51e | 2014-09-09 08:24:53 -0700 | [diff] [blame] | 392 | kfree(rcu_access_pointer(net->gen)); |
Johann Felix Soden | 45a19b0 | 2007-11-07 01:30:30 -0800 | [diff] [blame] | 393 | kmem_cache_free(net_cachep, net); |
| 394 | } |
| 395 | |
Al Viro | a685e08 | 2011-06-08 21:13:01 -0400 | [diff] [blame] | 396 | void net_drop_ns(void *p) |
| 397 | { |
| 398 | struct net *ns = p; |
Reshetova, Elena | c122e14 | 2017-06-30 13:08:08 +0300 | [diff] [blame] | 399 | if (ns && refcount_dec_and_test(&ns->passive)) |
Al Viro | a685e08 | 2011-06-08 21:13:01 -0400 | [diff] [blame] | 400 | net_free(ns); |
| 401 | } |
| 402 | |
Eric W. Biederman | 038e733 | 2012-06-14 02:31:10 -0700 | [diff] [blame] | 403 | struct net *copy_net_ns(unsigned long flags, |
| 404 | struct user_namespace *user_ns, struct net *old_net) |
Pavel Emelyanov | 6a1a3b9 | 2007-11-01 00:44:50 -0700 | [diff] [blame] | 405 | { |
Eric W. Biederman | 7032866 | 2016-08-08 14:33:23 -0500 | [diff] [blame] | 406 | struct ucounts *ucounts; |
Alexey Dobriyan | 088eb2d | 2009-05-04 11:12:14 -0700 | [diff] [blame] | 407 | struct net *net; |
| 408 | int rv; |
Pavel Emelyanov | 6a1a3b9 | 2007-11-01 00:44:50 -0700 | [diff] [blame] | 409 | |
Rob Landley | 911cb19 | 2011-04-15 02:26:25 +0000 | [diff] [blame] | 410 | if (!(flags & CLONE_NEWNET)) |
| 411 | return get_net(old_net); |
| 412 | |
Eric W. Biederman | 7032866 | 2016-08-08 14:33:23 -0500 | [diff] [blame] | 413 | ucounts = inc_net_namespaces(user_ns); |
| 414 | if (!ucounts) |
Eric W. Biederman | df75e77 | 2016-09-22 13:08:36 -0500 | [diff] [blame] | 415 | return ERR_PTR(-ENOSPC); |
Eric W. Biederman | 7032866 | 2016-08-08 14:33:23 -0500 | [diff] [blame] | 416 | |
Alexey Dobriyan | 088eb2d | 2009-05-04 11:12:14 -0700 | [diff] [blame] | 417 | net = net_alloc(); |
Eric W. Biederman | 7032866 | 2016-08-08 14:33:23 -0500 | [diff] [blame] | 418 | if (!net) { |
Kirill Tkhai | 5ba049a | 2018-02-13 12:26:13 +0300 | [diff] [blame] | 419 | rv = -ENOMEM; |
| 420 | goto dec_ucounts; |
Eric W. Biederman | 7032866 | 2016-08-08 14:33:23 -0500 | [diff] [blame] | 421 | } |
Kirill Tkhai | 5ba049a | 2018-02-13 12:26:13 +0300 | [diff] [blame] | 422 | refcount_set(&net->passive, 1); |
| 423 | net->ucounts = ucounts; |
Eric W. Biederman | 038e733 | 2012-06-14 02:31:10 -0700 | [diff] [blame] | 424 | get_user_ns(user_ns); |
| 425 | |
Kirill Tkhai | 1a57feb | 2018-02-13 12:26:23 +0300 | [diff] [blame] | 426 | rv = down_read_killable(&net_sem); |
Kirill Tkhai | 5ba049a | 2018-02-13 12:26:13 +0300 | [diff] [blame] | 427 | if (rv < 0) |
| 428 | goto put_userns; |
Kirill Tkhai | 1a57feb | 2018-02-13 12:26:23 +0300 | [diff] [blame] | 429 | rv = mutex_lock_killable(&net_mutex); |
| 430 | if (rv < 0) |
| 431 | goto up_read; |
Eric W. Biederman | 038e733 | 2012-06-14 02:31:10 -0700 | [diff] [blame] | 432 | rv = setup_net(net, user_ns); |
Pavel Emelyanov | 6a1a3b9 | 2007-11-01 00:44:50 -0700 | [diff] [blame] | 433 | mutex_unlock(&net_mutex); |
Kirill Tkhai | 1a57feb | 2018-02-13 12:26:23 +0300 | [diff] [blame] | 434 | up_read: |
| 435 | up_read(&net_sem); |
Alexey Dobriyan | 088eb2d | 2009-05-04 11:12:14 -0700 | [diff] [blame] | 436 | if (rv < 0) { |
Kirill Tkhai | 5ba049a | 2018-02-13 12:26:13 +0300 | [diff] [blame] | 437 | put_userns: |
Eric W. Biederman | 038e733 | 2012-06-14 02:31:10 -0700 | [diff] [blame] | 438 | put_user_ns(user_ns); |
Al Viro | a685e08 | 2011-06-08 21:13:01 -0400 | [diff] [blame] | 439 | net_drop_ns(net); |
Kirill Tkhai | 5ba049a | 2018-02-13 12:26:13 +0300 | [diff] [blame] | 440 | dec_ucounts: |
| 441 | dec_net_namespaces(ucounts); |
Alexey Dobriyan | 088eb2d | 2009-05-04 11:12:14 -0700 | [diff] [blame] | 442 | return ERR_PTR(rv); |
| 443 | } |
| 444 | return net; |
| 445 | } |
Daniel Lezcano | 486a87f | 2009-02-22 00:07:53 -0800 | [diff] [blame] | 446 | |
Kirill Tkhai | fb07a82 | 2018-01-19 19:14:53 +0300 | [diff] [blame] | 447 | static void unhash_nsid(struct net *net, struct net *last) |
| 448 | { |
| 449 | struct net *tmp; |
| 450 | /* This function is only called from cleanup_net() work, |
| 451 | * and this work is the only process, that may delete |
| 452 | * a net from net_namespace_list. So, when the below |
| 453 | * is executing, the list may only grow. Thus, we do not |
| 454 | * use for_each_net_rcu() or rtnl_lock(). |
| 455 | */ |
| 456 | for_each_net(tmp) { |
| 457 | int id; |
| 458 | |
| 459 | spin_lock_bh(&tmp->nsid_lock); |
| 460 | id = __peernet2id(tmp, net); |
| 461 | if (id >= 0) |
| 462 | idr_remove(&tmp->netns_ids, id); |
| 463 | spin_unlock_bh(&tmp->nsid_lock); |
| 464 | if (id >= 0) |
| 465 | rtnl_net_notifyid(tmp, RTM_DELNSID, id); |
| 466 | if (tmp == last) |
| 467 | break; |
| 468 | } |
| 469 | spin_lock_bh(&net->nsid_lock); |
| 470 | idr_destroy(&net->netns_ids); |
| 471 | spin_unlock_bh(&net->nsid_lock); |
| 472 | } |
| 473 | |
Eric W. Biederman | 2b035b3 | 2009-11-29 22:25:27 +0000 | [diff] [blame] | 474 | static DEFINE_SPINLOCK(cleanup_list_lock); |
| 475 | static LIST_HEAD(cleanup_list); /* Must hold cleanup_list_lock to touch */ |
| 476 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 477 | static void cleanup_net(struct work_struct *work) |
| 478 | { |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 479 | const struct pernet_operations *ops; |
Kirill Tkhai | fb07a82 | 2018-01-19 19:14:53 +0300 | [diff] [blame] | 480 | struct net *net, *tmp, *last; |
xiao jin | 1818ce4 | 2014-04-25 08:50:54 +0800 | [diff] [blame] | 481 | struct list_head net_kill_list; |
Eric W. Biederman | 72ad937 | 2009-12-03 02:29:03 +0000 | [diff] [blame] | 482 | LIST_HEAD(net_exit_list); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 483 | |
Eric W. Biederman | 2b035b3 | 2009-11-29 22:25:27 +0000 | [diff] [blame] | 484 | /* Atomically snapshot the list of namespaces to cleanup */ |
| 485 | spin_lock_irq(&cleanup_list_lock); |
| 486 | list_replace_init(&cleanup_list, &net_kill_list); |
| 487 | spin_unlock_irq(&cleanup_list_lock); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 488 | |
Kirill Tkhai | 1a57feb | 2018-02-13 12:26:23 +0300 | [diff] [blame] | 489 | down_read(&net_sem); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 490 | mutex_lock(&net_mutex); |
| 491 | |
| 492 | /* Don't let anyone else find us. */ |
Eric W. Biederman | f4618d3 | 2007-09-26 22:40:08 -0700 | [diff] [blame] | 493 | rtnl_lock(); |
Kirill Tkhai | fb07a82 | 2018-01-19 19:14:53 +0300 | [diff] [blame] | 494 | list_for_each_entry(net, &net_kill_list, cleanup_list) |
Eric W. Biederman | 2b035b3 | 2009-11-29 22:25:27 +0000 | [diff] [blame] | 495 | list_del_rcu(&net->list); |
Kirill Tkhai | fb07a82 | 2018-01-19 19:14:53 +0300 | [diff] [blame] | 496 | /* Cache last net. After we unlock rtnl, no one new net |
| 497 | * added to net_namespace_list can assign nsid pointer |
| 498 | * to a net from net_kill_list (see peernet2id_alloc()). |
| 499 | * So, we skip them in unhash_nsid(). |
| 500 | * |
| 501 | * Note, that unhash_nsid() does not delete nsid links |
| 502 | * between net_kill_list's nets, as they've already |
| 503 | * deleted from net_namespace_list. But, this would be |
| 504 | * useless anyway, as netns_ids are destroyed there. |
| 505 | */ |
| 506 | last = list_last_entry(&net_namespace_list, struct net, list); |
Eric W. Biederman | f4618d3 | 2007-09-26 22:40:08 -0700 | [diff] [blame] | 507 | rtnl_unlock(); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 508 | |
Kirill Tkhai | fb07a82 | 2018-01-19 19:14:53 +0300 | [diff] [blame] | 509 | list_for_each_entry(net, &net_kill_list, cleanup_list) { |
| 510 | unhash_nsid(net, last); |
| 511 | list_add_tail(&net->exit_list, &net_exit_list); |
| 512 | } |
| 513 | |
Johannes Berg | 11a28d3 | 2009-07-10 09:51:33 +0000 | [diff] [blame] | 514 | /* |
| 515 | * Another CPU might be rcu-iterating the list, wait for it. |
| 516 | * This needs to be before calling the exit() notifiers, so |
| 517 | * the rcu_barrier() below isn't sufficient alone. |
| 518 | */ |
| 519 | synchronize_rcu(); |
| 520 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 521 | /* Run all of the network namespace exit methods */ |
Eric W. Biederman | 72ad937 | 2009-12-03 02:29:03 +0000 | [diff] [blame] | 522 | list_for_each_entry_reverse(ops, &pernet_list, list) |
| 523 | ops_exit_list(ops, &net_exit_list); |
| 524 | |
Kirill Tkhai | bcab1dd | 2018-02-13 12:26:33 +0300 | [diff] [blame^] | 525 | mutex_unlock(&net_mutex); |
| 526 | |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 527 | /* Free the net generic variables */ |
Eric W. Biederman | 72ad937 | 2009-12-03 02:29:03 +0000 | [diff] [blame] | 528 | list_for_each_entry_reverse(ops, &pernet_list, list) |
| 529 | ops_free_list(ops, &net_exit_list); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 530 | |
Kirill Tkhai | 1a57feb | 2018-02-13 12:26:23 +0300 | [diff] [blame] | 531 | up_read(&net_sem); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 532 | |
| 533 | /* Ensure there are no outstanding rcu callbacks using this |
| 534 | * network namespace. |
| 535 | */ |
| 536 | rcu_barrier(); |
| 537 | |
| 538 | /* Finally it is safe to free my network namespace structure */ |
Eric W. Biederman | 72ad937 | 2009-12-03 02:29:03 +0000 | [diff] [blame] | 539 | list_for_each_entry_safe(net, tmp, &net_exit_list, exit_list) { |
| 540 | list_del_init(&net->exit_list); |
Eric W. Biederman | 7032866 | 2016-08-08 14:33:23 -0500 | [diff] [blame] | 541 | dec_net_namespaces(net->ucounts); |
Eric W. Biederman | 038e733 | 2012-06-14 02:31:10 -0700 | [diff] [blame] | 542 | put_user_ns(net->user_ns); |
Al Viro | a685e08 | 2011-06-08 21:13:01 -0400 | [diff] [blame] | 543 | net_drop_ns(net); |
Eric W. Biederman | 2b035b3 | 2009-11-29 22:25:27 +0000 | [diff] [blame] | 544 | } |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 545 | } |
Florian Westphal | 7866cc5 | 2017-05-30 11:38:12 +0200 | [diff] [blame] | 546 | |
| 547 | /** |
| 548 | * net_ns_barrier - wait until concurrent net_cleanup_work is done |
| 549 | * |
| 550 | * cleanup_net runs from work queue and will first remove namespaces |
| 551 | * from the global list, then run net exit functions. |
| 552 | * |
| 553 | * Call this in module exit path to make sure that all netns |
| 554 | * ->exit ops have been invoked before the function is removed. |
| 555 | */ |
| 556 | void net_ns_barrier(void) |
| 557 | { |
Kirill Tkhai | 1a57feb | 2018-02-13 12:26:23 +0300 | [diff] [blame] | 558 | down_write(&net_sem); |
Florian Westphal | 7866cc5 | 2017-05-30 11:38:12 +0200 | [diff] [blame] | 559 | mutex_lock(&net_mutex); |
| 560 | mutex_unlock(&net_mutex); |
Kirill Tkhai | 1a57feb | 2018-02-13 12:26:23 +0300 | [diff] [blame] | 561 | up_write(&net_sem); |
Florian Westphal | 7866cc5 | 2017-05-30 11:38:12 +0200 | [diff] [blame] | 562 | } |
| 563 | EXPORT_SYMBOL(net_ns_barrier); |
| 564 | |
Eric W. Biederman | 2b035b3 | 2009-11-29 22:25:27 +0000 | [diff] [blame] | 565 | static DECLARE_WORK(net_cleanup_work, cleanup_net); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 566 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 567 | void __put_net(struct net *net) |
| 568 | { |
| 569 | /* Cleanup the network namespace in process context */ |
Eric W. Biederman | 2b035b3 | 2009-11-29 22:25:27 +0000 | [diff] [blame] | 570 | unsigned long flags; |
| 571 | |
| 572 | spin_lock_irqsave(&cleanup_list_lock, flags); |
| 573 | list_add(&net->cleanup_list, &cleanup_list); |
| 574 | spin_unlock_irqrestore(&cleanup_list_lock, flags); |
| 575 | |
| 576 | queue_work(netns_wq, &net_cleanup_work); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 577 | } |
| 578 | EXPORT_SYMBOL_GPL(__put_net); |
| 579 | |
Stephen Rothwell | 956c920 | 2011-05-12 13:51:13 +1000 | [diff] [blame] | 580 | struct net *get_net_ns_by_fd(int fd) |
| 581 | { |
Stephen Rothwell | 956c920 | 2011-05-12 13:51:13 +1000 | [diff] [blame] | 582 | struct file *file; |
Al Viro | 33c4294 | 2014-11-01 02:32:53 -0400 | [diff] [blame] | 583 | struct ns_common *ns; |
Stephen Rothwell | 956c920 | 2011-05-12 13:51:13 +1000 | [diff] [blame] | 584 | struct net *net; |
| 585 | |
Stephen Rothwell | 956c920 | 2011-05-12 13:51:13 +1000 | [diff] [blame] | 586 | file = proc_ns_fget(fd); |
Al Viro | c316e6a | 2011-06-05 00:37:35 +0000 | [diff] [blame] | 587 | if (IS_ERR(file)) |
| 588 | return ERR_CAST(file); |
Stephen Rothwell | 956c920 | 2011-05-12 13:51:13 +1000 | [diff] [blame] | 589 | |
Al Viro | f77c801 | 2014-11-01 03:13:17 -0400 | [diff] [blame] | 590 | ns = get_proc_ns(file_inode(file)); |
Al Viro | 33c4294 | 2014-11-01 02:32:53 -0400 | [diff] [blame] | 591 | if (ns->ops == &netns_operations) |
| 592 | net = get_net(container_of(ns, struct net, ns)); |
Al Viro | c316e6a | 2011-06-05 00:37:35 +0000 | [diff] [blame] | 593 | else |
| 594 | net = ERR_PTR(-EINVAL); |
Stephen Rothwell | 956c920 | 2011-05-12 13:51:13 +1000 | [diff] [blame] | 595 | |
Al Viro | c316e6a | 2011-06-05 00:37:35 +0000 | [diff] [blame] | 596 | fput(file); |
Stephen Rothwell | 956c920 | 2011-05-12 13:51:13 +1000 | [diff] [blame] | 597 | return net; |
| 598 | } |
| 599 | |
Pavel Emelyanov | 6a1a3b9 | 2007-11-01 00:44:50 -0700 | [diff] [blame] | 600 | #else |
Stephen Rothwell | 956c920 | 2011-05-12 13:51:13 +1000 | [diff] [blame] | 601 | struct net *get_net_ns_by_fd(int fd) |
| 602 | { |
| 603 | return ERR_PTR(-EINVAL); |
| 604 | } |
Pavel Emelyanov | 6a1a3b9 | 2007-11-01 00:44:50 -0700 | [diff] [blame] | 605 | #endif |
Vadim Kochan | 4b681c8 | 2015-01-12 16:34:05 +0200 | [diff] [blame] | 606 | EXPORT_SYMBOL_GPL(get_net_ns_by_fd); |
Eric W. Biederman | 9dd776b | 2007-09-26 22:04:26 -0700 | [diff] [blame] | 607 | |
Johannes Berg | 30ffee8 | 2009-07-10 09:51:35 +0000 | [diff] [blame] | 608 | struct net *get_net_ns_by_pid(pid_t pid) |
| 609 | { |
| 610 | struct task_struct *tsk; |
| 611 | struct net *net; |
| 612 | |
| 613 | /* Lookup the network namespace */ |
| 614 | net = ERR_PTR(-ESRCH); |
| 615 | rcu_read_lock(); |
| 616 | tsk = find_task_by_vpid(pid); |
| 617 | if (tsk) { |
| 618 | struct nsproxy *nsproxy; |
Eric W. Biederman | 728dba3 | 2014-02-03 19:13:49 -0800 | [diff] [blame] | 619 | task_lock(tsk); |
| 620 | nsproxy = tsk->nsproxy; |
Johannes Berg | 30ffee8 | 2009-07-10 09:51:35 +0000 | [diff] [blame] | 621 | if (nsproxy) |
| 622 | net = get_net(nsproxy->net_ns); |
Eric W. Biederman | 728dba3 | 2014-02-03 19:13:49 -0800 | [diff] [blame] | 623 | task_unlock(tsk); |
Johannes Berg | 30ffee8 | 2009-07-10 09:51:35 +0000 | [diff] [blame] | 624 | } |
| 625 | rcu_read_unlock(); |
| 626 | return net; |
| 627 | } |
| 628 | EXPORT_SYMBOL_GPL(get_net_ns_by_pid); |
| 629 | |
Eric W. Biederman | 98f842e | 2011-06-15 10:21:48 -0700 | [diff] [blame] | 630 | static __net_init int net_ns_net_init(struct net *net) |
| 631 | { |
Al Viro | 33c4294 | 2014-11-01 02:32:53 -0400 | [diff] [blame] | 632 | #ifdef CONFIG_NET_NS |
| 633 | net->ns.ops = &netns_operations; |
| 634 | #endif |
Al Viro | 6344c43 | 2014-11-01 00:45:45 -0400 | [diff] [blame] | 635 | return ns_alloc_inum(&net->ns); |
Eric W. Biederman | 98f842e | 2011-06-15 10:21:48 -0700 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | static __net_exit void net_ns_net_exit(struct net *net) |
| 639 | { |
Al Viro | 6344c43 | 2014-11-01 00:45:45 -0400 | [diff] [blame] | 640 | ns_free_inum(&net->ns); |
Eric W. Biederman | 98f842e | 2011-06-15 10:21:48 -0700 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | static struct pernet_operations __net_initdata net_ns_ops = { |
| 644 | .init = net_ns_net_init, |
| 645 | .exit = net_ns_net_exit, |
| 646 | }; |
| 647 | |
stephen hemminger | 3ee5256 | 2016-08-31 15:17:49 -0700 | [diff] [blame] | 648 | static const struct nla_policy rtnl_net_policy[NETNSA_MAX + 1] = { |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 649 | [NETNSA_NONE] = { .type = NLA_UNSPEC }, |
| 650 | [NETNSA_NSID] = { .type = NLA_S32 }, |
| 651 | [NETNSA_PID] = { .type = NLA_U32 }, |
| 652 | [NETNSA_FD] = { .type = NLA_U32 }, |
| 653 | }; |
| 654 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 655 | static int rtnl_net_newid(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 656 | struct netlink_ext_ack *extack) |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 657 | { |
| 658 | struct net *net = sock_net(skb->sk); |
| 659 | struct nlattr *tb[NETNSA_MAX + 1]; |
Nicolas Dichtel | 4a7f7bc | 2017-06-09 14:41:56 +0200 | [diff] [blame] | 660 | struct nlattr *nla; |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 661 | struct net *peer; |
| 662 | int nsid, err; |
| 663 | |
| 664 | err = nlmsg_parse(nlh, sizeof(struct rtgenmsg), tb, NETNSA_MAX, |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 665 | rtnl_net_policy, extack); |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 666 | if (err < 0) |
| 667 | return err; |
Nicolas Dichtel | 4a7f7bc | 2017-06-09 14:41:56 +0200 | [diff] [blame] | 668 | if (!tb[NETNSA_NSID]) { |
| 669 | NL_SET_ERR_MSG(extack, "nsid is missing"); |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 670 | return -EINVAL; |
Nicolas Dichtel | 4a7f7bc | 2017-06-09 14:41:56 +0200 | [diff] [blame] | 671 | } |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 672 | nsid = nla_get_s32(tb[NETNSA_NSID]); |
| 673 | |
Nicolas Dichtel | 4a7f7bc | 2017-06-09 14:41:56 +0200 | [diff] [blame] | 674 | if (tb[NETNSA_PID]) { |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 675 | peer = get_net_ns_by_pid(nla_get_u32(tb[NETNSA_PID])); |
Nicolas Dichtel | 4a7f7bc | 2017-06-09 14:41:56 +0200 | [diff] [blame] | 676 | nla = tb[NETNSA_PID]; |
| 677 | } else if (tb[NETNSA_FD]) { |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 678 | peer = get_net_ns_by_fd(nla_get_u32(tb[NETNSA_FD])); |
Nicolas Dichtel | 4a7f7bc | 2017-06-09 14:41:56 +0200 | [diff] [blame] | 679 | nla = tb[NETNSA_FD]; |
| 680 | } else { |
| 681 | NL_SET_ERR_MSG(extack, "Peer netns reference is missing"); |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 682 | return -EINVAL; |
Nicolas Dichtel | 4a7f7bc | 2017-06-09 14:41:56 +0200 | [diff] [blame] | 683 | } |
| 684 | if (IS_ERR(peer)) { |
| 685 | NL_SET_BAD_ATTR(extack, nla); |
| 686 | NL_SET_ERR_MSG(extack, "Peer netns reference is invalid"); |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 687 | return PTR_ERR(peer); |
Nicolas Dichtel | 4a7f7bc | 2017-06-09 14:41:56 +0200 | [diff] [blame] | 688 | } |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 689 | |
Paul Moore | fba143c | 2016-11-29 16:57:48 -0500 | [diff] [blame] | 690 | spin_lock_bh(&net->nsid_lock); |
Nicolas Dichtel | 3138dbf | 2015-05-07 11:02:50 +0200 | [diff] [blame] | 691 | if (__peernet2id(net, peer) >= 0) { |
Paul Moore | fba143c | 2016-11-29 16:57:48 -0500 | [diff] [blame] | 692 | spin_unlock_bh(&net->nsid_lock); |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 693 | err = -EEXIST; |
Nicolas Dichtel | 4a7f7bc | 2017-06-09 14:41:56 +0200 | [diff] [blame] | 694 | NL_SET_BAD_ATTR(extack, nla); |
| 695 | NL_SET_ERR_MSG(extack, |
| 696 | "Peer netns already has a nsid assigned"); |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 697 | goto out; |
| 698 | } |
| 699 | |
| 700 | err = alloc_netid(net, peer, nsid); |
Paul Moore | fba143c | 2016-11-29 16:57:48 -0500 | [diff] [blame] | 701 | spin_unlock_bh(&net->nsid_lock); |
Nicolas Dichtel | 3138dbf | 2015-05-07 11:02:50 +0200 | [diff] [blame] | 702 | if (err >= 0) { |
| 703 | rtnl_net_notifyid(net, RTM_NEWNSID, err); |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 704 | err = 0; |
Nicolas Dichtel | 4a7f7bc | 2017-06-09 14:41:56 +0200 | [diff] [blame] | 705 | } else if (err == -ENOSPC && nsid >= 0) { |
Nicolas Dichtel | 10d486a | 2017-06-09 14:41:57 +0200 | [diff] [blame] | 706 | err = -EEXIST; |
Nicolas Dichtel | 4a7f7bc | 2017-06-09 14:41:56 +0200 | [diff] [blame] | 707 | NL_SET_BAD_ATTR(extack, tb[NETNSA_NSID]); |
| 708 | NL_SET_ERR_MSG(extack, "The specified nsid is already used"); |
Nicolas Dichtel | 3138dbf | 2015-05-07 11:02:50 +0200 | [diff] [blame] | 709 | } |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 710 | out: |
| 711 | put_net(peer); |
| 712 | return err; |
| 713 | } |
| 714 | |
| 715 | static int rtnl_net_get_size(void) |
| 716 | { |
| 717 | return NLMSG_ALIGN(sizeof(struct rtgenmsg)) |
| 718 | + nla_total_size(sizeof(s32)) /* NETNSA_NSID */ |
| 719 | ; |
| 720 | } |
| 721 | |
| 722 | static int rtnl_net_fill(struct sk_buff *skb, u32 portid, u32 seq, int flags, |
Nicolas Dichtel | cab3c8e | 2015-05-07 11:02:48 +0200 | [diff] [blame] | 723 | int cmd, struct net *net, int nsid) |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 724 | { |
| 725 | struct nlmsghdr *nlh; |
| 726 | struct rtgenmsg *rth; |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 727 | |
| 728 | nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rth), flags); |
| 729 | if (!nlh) |
| 730 | return -EMSGSIZE; |
| 731 | |
| 732 | rth = nlmsg_data(nlh); |
| 733 | rth->rtgen_family = AF_UNSPEC; |
| 734 | |
Nicolas Dichtel | cab3c8e | 2015-05-07 11:02:48 +0200 | [diff] [blame] | 735 | if (nla_put_s32(skb, NETNSA_NSID, nsid)) |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 736 | goto nla_put_failure; |
| 737 | |
| 738 | nlmsg_end(skb, nlh); |
| 739 | return 0; |
| 740 | |
| 741 | nla_put_failure: |
| 742 | nlmsg_cancel(skb, nlh); |
| 743 | return -EMSGSIZE; |
| 744 | } |
| 745 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 746 | static int rtnl_net_getid(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 747 | struct netlink_ext_ack *extack) |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 748 | { |
| 749 | struct net *net = sock_net(skb->sk); |
| 750 | struct nlattr *tb[NETNSA_MAX + 1]; |
Nicolas Dichtel | 4a7f7bc | 2017-06-09 14:41:56 +0200 | [diff] [blame] | 751 | struct nlattr *nla; |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 752 | struct sk_buff *msg; |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 753 | struct net *peer; |
Nicolas Dichtel | cab3c8e | 2015-05-07 11:02:48 +0200 | [diff] [blame] | 754 | int err, id; |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 755 | |
| 756 | err = nlmsg_parse(nlh, sizeof(struct rtgenmsg), tb, NETNSA_MAX, |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 757 | rtnl_net_policy, extack); |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 758 | if (err < 0) |
| 759 | return err; |
Nicolas Dichtel | 4a7f7bc | 2017-06-09 14:41:56 +0200 | [diff] [blame] | 760 | if (tb[NETNSA_PID]) { |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 761 | peer = get_net_ns_by_pid(nla_get_u32(tb[NETNSA_PID])); |
Nicolas Dichtel | 4a7f7bc | 2017-06-09 14:41:56 +0200 | [diff] [blame] | 762 | nla = tb[NETNSA_PID]; |
| 763 | } else if (tb[NETNSA_FD]) { |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 764 | peer = get_net_ns_by_fd(nla_get_u32(tb[NETNSA_FD])); |
Nicolas Dichtel | 4a7f7bc | 2017-06-09 14:41:56 +0200 | [diff] [blame] | 765 | nla = tb[NETNSA_FD]; |
| 766 | } else { |
| 767 | NL_SET_ERR_MSG(extack, "Peer netns reference is missing"); |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 768 | return -EINVAL; |
Nicolas Dichtel | 4a7f7bc | 2017-06-09 14:41:56 +0200 | [diff] [blame] | 769 | } |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 770 | |
Nicolas Dichtel | 4a7f7bc | 2017-06-09 14:41:56 +0200 | [diff] [blame] | 771 | if (IS_ERR(peer)) { |
| 772 | NL_SET_BAD_ATTR(extack, nla); |
| 773 | NL_SET_ERR_MSG(extack, "Peer netns reference is invalid"); |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 774 | return PTR_ERR(peer); |
Nicolas Dichtel | 4a7f7bc | 2017-06-09 14:41:56 +0200 | [diff] [blame] | 775 | } |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 776 | |
| 777 | msg = nlmsg_new(rtnl_net_get_size(), GFP_KERNEL); |
| 778 | if (!msg) { |
| 779 | err = -ENOMEM; |
| 780 | goto out; |
| 781 | } |
| 782 | |
Nicolas Dichtel | 95f3841 | 2015-05-07 11:02:51 +0200 | [diff] [blame] | 783 | id = peernet2id(net, peer); |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 784 | err = rtnl_net_fill(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0, |
David S. Miller | b04096f | 2015-05-13 14:31:43 -0400 | [diff] [blame] | 785 | RTM_NEWNSID, net, id); |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 786 | if (err < 0) |
| 787 | goto err_out; |
| 788 | |
| 789 | err = rtnl_unicast(msg, net, NETLINK_CB(skb).portid); |
| 790 | goto out; |
| 791 | |
| 792 | err_out: |
| 793 | nlmsg_free(msg); |
| 794 | out: |
| 795 | put_net(peer); |
| 796 | return err; |
| 797 | } |
| 798 | |
Nicolas Dichtel | a143c40 | 2015-04-07 11:51:54 +0200 | [diff] [blame] | 799 | struct rtnl_net_dump_cb { |
| 800 | struct net *net; |
| 801 | struct sk_buff *skb; |
| 802 | struct netlink_callback *cb; |
| 803 | int idx; |
| 804 | int s_idx; |
| 805 | }; |
| 806 | |
| 807 | static int rtnl_net_dumpid_one(int id, void *peer, void *data) |
| 808 | { |
| 809 | struct rtnl_net_dump_cb *net_cb = (struct rtnl_net_dump_cb *)data; |
| 810 | int ret; |
| 811 | |
| 812 | if (net_cb->idx < net_cb->s_idx) |
| 813 | goto cont; |
| 814 | |
| 815 | ret = rtnl_net_fill(net_cb->skb, NETLINK_CB(net_cb->cb->skb).portid, |
| 816 | net_cb->cb->nlh->nlmsg_seq, NLM_F_MULTI, |
Nicolas Dichtel | cab3c8e | 2015-05-07 11:02:48 +0200 | [diff] [blame] | 817 | RTM_NEWNSID, net_cb->net, id); |
Nicolas Dichtel | a143c40 | 2015-04-07 11:51:54 +0200 | [diff] [blame] | 818 | if (ret < 0) |
| 819 | return ret; |
| 820 | |
| 821 | cont: |
| 822 | net_cb->idx++; |
| 823 | return 0; |
| 824 | } |
| 825 | |
| 826 | static int rtnl_net_dumpid(struct sk_buff *skb, struct netlink_callback *cb) |
| 827 | { |
| 828 | struct net *net = sock_net(skb->sk); |
| 829 | struct rtnl_net_dump_cb net_cb = { |
| 830 | .net = net, |
| 831 | .skb = skb, |
| 832 | .cb = cb, |
| 833 | .idx = 0, |
| 834 | .s_idx = cb->args[0], |
| 835 | }; |
| 836 | |
Paul Moore | fba143c | 2016-11-29 16:57:48 -0500 | [diff] [blame] | 837 | spin_lock_bh(&net->nsid_lock); |
Nicolas Dichtel | a143c40 | 2015-04-07 11:51:54 +0200 | [diff] [blame] | 838 | idr_for_each(&net->netns_ids, rtnl_net_dumpid_one, &net_cb); |
Paul Moore | fba143c | 2016-11-29 16:57:48 -0500 | [diff] [blame] | 839 | spin_unlock_bh(&net->nsid_lock); |
Nicolas Dichtel | a143c40 | 2015-04-07 11:51:54 +0200 | [diff] [blame] | 840 | |
| 841 | cb->args[0] = net_cb.idx; |
| 842 | return skb->len; |
| 843 | } |
| 844 | |
Nicolas Dichtel | cab3c8e | 2015-05-07 11:02:48 +0200 | [diff] [blame] | 845 | static void rtnl_net_notifyid(struct net *net, int cmd, int id) |
Nicolas Dichtel | 9a96345 | 2015-04-07 11:51:53 +0200 | [diff] [blame] | 846 | { |
| 847 | struct sk_buff *msg; |
| 848 | int err = -ENOMEM; |
| 849 | |
| 850 | msg = nlmsg_new(rtnl_net_get_size(), GFP_KERNEL); |
| 851 | if (!msg) |
| 852 | goto out; |
| 853 | |
Nicolas Dichtel | cab3c8e | 2015-05-07 11:02:48 +0200 | [diff] [blame] | 854 | err = rtnl_net_fill(msg, 0, 0, 0, cmd, net, id); |
Nicolas Dichtel | 9a96345 | 2015-04-07 11:51:53 +0200 | [diff] [blame] | 855 | if (err < 0) |
| 856 | goto err_out; |
| 857 | |
| 858 | rtnl_notify(msg, net, 0, RTNLGRP_NSID, NULL, 0); |
| 859 | return; |
| 860 | |
| 861 | err_out: |
| 862 | nlmsg_free(msg); |
| 863 | out: |
| 864 | rtnl_set_sk_err(net, RTNLGRP_NSID, err); |
| 865 | } |
| 866 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 867 | static int __init net_ns_init(void) |
| 868 | { |
Daniel Lezcano | 486a87f | 2009-02-22 00:07:53 -0800 | [diff] [blame] | 869 | struct net_generic *ng; |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 870 | |
Pavel Emelyanov | d57a921 | 2007-11-01 00:46:50 -0700 | [diff] [blame] | 871 | #ifdef CONFIG_NET_NS |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 872 | net_cachep = kmem_cache_create("net_namespace", sizeof(struct net), |
| 873 | SMP_CACHE_BYTES, |
| 874 | SLAB_PANIC, NULL); |
Benjamin Thery | 3ef1355 | 2007-11-19 23:18:16 -0800 | [diff] [blame] | 875 | |
| 876 | /* Create workqueue for cleanup */ |
| 877 | netns_wq = create_singlethread_workqueue("netns"); |
| 878 | if (!netns_wq) |
| 879 | panic("Could not create netns workq"); |
Pavel Emelyanov | d57a921 | 2007-11-01 00:46:50 -0700 | [diff] [blame] | 880 | #endif |
Benjamin Thery | 3ef1355 | 2007-11-19 23:18:16 -0800 | [diff] [blame] | 881 | |
Daniel Lezcano | 486a87f | 2009-02-22 00:07:53 -0800 | [diff] [blame] | 882 | ng = net_alloc_generic(); |
| 883 | if (!ng) |
| 884 | panic("Could not allocate generic netns"); |
| 885 | |
| 886 | rcu_assign_pointer(init_net.gen, ng); |
| 887 | |
Kirill Tkhai | 1a57feb | 2018-02-13 12:26:23 +0300 | [diff] [blame] | 888 | down_write(&net_sem); |
Eric W. Biederman | 038e733 | 2012-06-14 02:31:10 -0700 | [diff] [blame] | 889 | if (setup_net(&init_net, &init_user_ns)) |
Stephen Hemminger | ca0f311 | 2009-05-21 15:10:31 -0700 | [diff] [blame] | 890 | panic("Could not setup the initial network namespace"); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 891 | |
Dmitry Torokhov | f8c46cb | 2016-08-10 14:36:00 -0700 | [diff] [blame] | 892 | init_net_initialized = true; |
Kirill Tkhai | 1a57feb | 2018-02-13 12:26:23 +0300 | [diff] [blame] | 893 | up_write(&net_sem); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 894 | |
Eric W. Biederman | 98f842e | 2011-06-15 10:21:48 -0700 | [diff] [blame] | 895 | register_pernet_subsys(&net_ns_ops); |
| 896 | |
Florian Westphal | 165b911 | 2017-08-09 20:41:53 +0200 | [diff] [blame] | 897 | rtnl_register(PF_UNSPEC, RTM_NEWNSID, rtnl_net_newid, NULL, |
| 898 | RTNL_FLAG_DOIT_UNLOCKED); |
Nicolas Dichtel | a143c40 | 2015-04-07 11:51:54 +0200 | [diff] [blame] | 899 | rtnl_register(PF_UNSPEC, RTM_GETNSID, rtnl_net_getid, rtnl_net_dumpid, |
Florian Westphal | 165b911 | 2017-08-09 20:41:53 +0200 | [diff] [blame] | 900 | RTNL_FLAG_DOIT_UNLOCKED); |
Nicolas Dichtel | 0c7aecd | 2015-01-15 15:11:15 +0100 | [diff] [blame] | 901 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 902 | return 0; |
| 903 | } |
| 904 | |
| 905 | pure_initcall(net_ns_init); |
| 906 | |
Denis V. Lunev | ed160e8 | 2007-11-13 03:23:21 -0800 | [diff] [blame] | 907 | #ifdef CONFIG_NET_NS |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 908 | static int __register_pernet_operations(struct list_head *list, |
| 909 | struct pernet_operations *ops) |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 910 | { |
Eric W. Biederman | 72ad937 | 2009-12-03 02:29:03 +0000 | [diff] [blame] | 911 | struct net *net; |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 912 | int error; |
Eric W. Biederman | 72ad937 | 2009-12-03 02:29:03 +0000 | [diff] [blame] | 913 | LIST_HEAD(net_exit_list); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 914 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 915 | list_add_tail(&ops->list, list); |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 916 | if (ops->init || (ops->id && ops->size)) { |
Pavel Emelyanov | 1dba323 | 2007-11-01 00:42:43 -0700 | [diff] [blame] | 917 | for_each_net(net) { |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 918 | error = ops_init(ops, net); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 919 | if (error) |
| 920 | goto out_undo; |
Eric W. Biederman | 72ad937 | 2009-12-03 02:29:03 +0000 | [diff] [blame] | 921 | list_add_tail(&net->exit_list, &net_exit_list); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 922 | } |
| 923 | } |
Pavel Emelyanov | 1dba323 | 2007-11-01 00:42:43 -0700 | [diff] [blame] | 924 | return 0; |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 925 | |
| 926 | out_undo: |
| 927 | /* If I have an error cleanup all namespaces I initialized */ |
| 928 | list_del(&ops->list); |
Eric W. Biederman | 72ad937 | 2009-12-03 02:29:03 +0000 | [diff] [blame] | 929 | ops_exit_list(ops, &net_exit_list); |
| 930 | ops_free_list(ops, &net_exit_list); |
Pavel Emelyanov | 1dba323 | 2007-11-01 00:42:43 -0700 | [diff] [blame] | 931 | return error; |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 932 | } |
| 933 | |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 934 | static void __unregister_pernet_operations(struct pernet_operations *ops) |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 935 | { |
| 936 | struct net *net; |
Eric W. Biederman | 72ad937 | 2009-12-03 02:29:03 +0000 | [diff] [blame] | 937 | LIST_HEAD(net_exit_list); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 938 | |
| 939 | list_del(&ops->list); |
Eric W. Biederman | 72ad937 | 2009-12-03 02:29:03 +0000 | [diff] [blame] | 940 | for_each_net(net) |
| 941 | list_add_tail(&net->exit_list, &net_exit_list); |
| 942 | ops_exit_list(ops, &net_exit_list); |
| 943 | ops_free_list(ops, &net_exit_list); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 944 | } |
| 945 | |
Denis V. Lunev | ed160e8 | 2007-11-13 03:23:21 -0800 | [diff] [blame] | 946 | #else |
| 947 | |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 948 | static int __register_pernet_operations(struct list_head *list, |
| 949 | struct pernet_operations *ops) |
| 950 | { |
Dmitry Torokhov | f8c46cb | 2016-08-10 14:36:00 -0700 | [diff] [blame] | 951 | if (!init_net_initialized) { |
| 952 | list_add_tail(&ops->list, list); |
| 953 | return 0; |
| 954 | } |
| 955 | |
Julian Anastasov | b922934 | 2012-04-16 04:43:15 +0000 | [diff] [blame] | 956 | return ops_init(ops, &init_net); |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | static void __unregister_pernet_operations(struct pernet_operations *ops) |
| 960 | { |
Dmitry Torokhov | f8c46cb | 2016-08-10 14:36:00 -0700 | [diff] [blame] | 961 | if (!init_net_initialized) { |
| 962 | list_del(&ops->list); |
| 963 | } else { |
| 964 | LIST_HEAD(net_exit_list); |
| 965 | list_add(&init_net.exit_list, &net_exit_list); |
| 966 | ops_exit_list(ops, &net_exit_list); |
| 967 | ops_free_list(ops, &net_exit_list); |
| 968 | } |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | #endif /* CONFIG_NET_NS */ |
| 972 | |
| 973 | static DEFINE_IDA(net_generic_ids); |
| 974 | |
Denis V. Lunev | ed160e8 | 2007-11-13 03:23:21 -0800 | [diff] [blame] | 975 | static int register_pernet_operations(struct list_head *list, |
| 976 | struct pernet_operations *ops) |
| 977 | { |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 978 | int error; |
| 979 | |
| 980 | if (ops->id) { |
| 981 | again: |
Alexey Dobriyan | 6af2d5f | 2016-12-02 04:21:32 +0300 | [diff] [blame] | 982 | error = ida_get_new_above(&net_generic_ids, MIN_PERNET_OPS_ID, ops->id); |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 983 | if (error < 0) { |
| 984 | if (error == -EAGAIN) { |
| 985 | ida_pre_get(&net_generic_ids, GFP_KERNEL); |
| 986 | goto again; |
| 987 | } |
| 988 | return error; |
| 989 | } |
Alexey Dobriyan | 6af2d5f | 2016-12-02 04:21:32 +0300 | [diff] [blame] | 990 | max_gen_ptrs = max(max_gen_ptrs, *ops->id + 1); |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 991 | } |
| 992 | error = __register_pernet_operations(list, ops); |
Eric W. Biederman | 3a765ed | 2009-12-03 02:29:06 +0000 | [diff] [blame] | 993 | if (error) { |
| 994 | rcu_barrier(); |
| 995 | if (ops->id) |
| 996 | ida_remove(&net_generic_ids, *ops->id); |
| 997 | } |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 998 | |
| 999 | return error; |
Denis V. Lunev | ed160e8 | 2007-11-13 03:23:21 -0800 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | static void unregister_pernet_operations(struct pernet_operations *ops) |
| 1003 | { |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 1004 | |
| 1005 | __unregister_pernet_operations(ops); |
Eric W. Biederman | 3a765ed | 2009-12-03 02:29:06 +0000 | [diff] [blame] | 1006 | rcu_barrier(); |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 1007 | if (ops->id) |
| 1008 | ida_remove(&net_generic_ids, *ops->id); |
Denis V. Lunev | ed160e8 | 2007-11-13 03:23:21 -0800 | [diff] [blame] | 1009 | } |
Pavel Emelyanov | c93cf61 | 2008-04-15 00:35:23 -0700 | [diff] [blame] | 1010 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 1011 | /** |
| 1012 | * register_pernet_subsys - register a network namespace subsystem |
| 1013 | * @ops: pernet operations structure for the subsystem |
| 1014 | * |
| 1015 | * Register a subsystem which has init and exit functions |
| 1016 | * that are called when network namespaces are created and |
| 1017 | * destroyed respectively. |
| 1018 | * |
| 1019 | * When registered all network namespace init functions are |
| 1020 | * called for every existing network namespace. Allowing kernel |
| 1021 | * modules to have a race free view of the set of network namespaces. |
| 1022 | * |
| 1023 | * When a new network namespace is created all of the init |
| 1024 | * methods are called in the order in which they were registered. |
| 1025 | * |
| 1026 | * When a network namespace is destroyed all of the exit methods |
| 1027 | * are called in the reverse of the order with which they were |
| 1028 | * registered. |
| 1029 | */ |
| 1030 | int register_pernet_subsys(struct pernet_operations *ops) |
| 1031 | { |
| 1032 | int error; |
Kirill Tkhai | 1a57feb | 2018-02-13 12:26:23 +0300 | [diff] [blame] | 1033 | down_write(&net_sem); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 1034 | error = register_pernet_operations(first_device, ops); |
Kirill Tkhai | 1a57feb | 2018-02-13 12:26:23 +0300 | [diff] [blame] | 1035 | up_write(&net_sem); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 1036 | return error; |
| 1037 | } |
| 1038 | EXPORT_SYMBOL_GPL(register_pernet_subsys); |
| 1039 | |
| 1040 | /** |
| 1041 | * unregister_pernet_subsys - unregister a network namespace subsystem |
| 1042 | * @ops: pernet operations structure to manipulate |
| 1043 | * |
| 1044 | * Remove the pernet operations structure from the list to be |
Oliver Pinter | 53379e5 | 2008-02-03 17:56:48 +0200 | [diff] [blame] | 1045 | * used when network namespaces are created or destroyed. In |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 1046 | * addition run the exit method for all existing network |
| 1047 | * namespaces. |
| 1048 | */ |
Jiri Pirko | b3c981d | 2010-04-25 00:49:56 -0700 | [diff] [blame] | 1049 | void unregister_pernet_subsys(struct pernet_operations *ops) |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 1050 | { |
Kirill Tkhai | 1a57feb | 2018-02-13 12:26:23 +0300 | [diff] [blame] | 1051 | down_write(&net_sem); |
Jiri Pirko | b3c981d | 2010-04-25 00:49:56 -0700 | [diff] [blame] | 1052 | unregister_pernet_operations(ops); |
Kirill Tkhai | 1a57feb | 2018-02-13 12:26:23 +0300 | [diff] [blame] | 1053 | up_write(&net_sem); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 1054 | } |
| 1055 | EXPORT_SYMBOL_GPL(unregister_pernet_subsys); |
| 1056 | |
| 1057 | /** |
| 1058 | * register_pernet_device - register a network namespace device |
| 1059 | * @ops: pernet operations structure for the subsystem |
| 1060 | * |
| 1061 | * Register a device which has init and exit functions |
| 1062 | * that are called when network namespaces are created and |
| 1063 | * destroyed respectively. |
| 1064 | * |
| 1065 | * When registered all network namespace init functions are |
| 1066 | * called for every existing network namespace. Allowing kernel |
| 1067 | * modules to have a race free view of the set of network namespaces. |
| 1068 | * |
| 1069 | * When a new network namespace is created all of the init |
| 1070 | * methods are called in the order in which they were registered. |
| 1071 | * |
| 1072 | * When a network namespace is destroyed all of the exit methods |
| 1073 | * are called in the reverse of the order with which they were |
| 1074 | * registered. |
| 1075 | */ |
| 1076 | int register_pernet_device(struct pernet_operations *ops) |
| 1077 | { |
| 1078 | int error; |
Kirill Tkhai | 1a57feb | 2018-02-13 12:26:23 +0300 | [diff] [blame] | 1079 | down_write(&net_sem); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 1080 | error = register_pernet_operations(&pernet_list, ops); |
| 1081 | if (!error && (first_device == &pernet_list)) |
| 1082 | first_device = &ops->list; |
Kirill Tkhai | 1a57feb | 2018-02-13 12:26:23 +0300 | [diff] [blame] | 1083 | up_write(&net_sem); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 1084 | return error; |
| 1085 | } |
| 1086 | EXPORT_SYMBOL_GPL(register_pernet_device); |
| 1087 | |
| 1088 | /** |
| 1089 | * unregister_pernet_device - unregister a network namespace netdevice |
| 1090 | * @ops: pernet operations structure to manipulate |
| 1091 | * |
| 1092 | * Remove the pernet operations structure from the list to be |
Oliver Pinter | 53379e5 | 2008-02-03 17:56:48 +0200 | [diff] [blame] | 1093 | * used when network namespaces are created or destroyed. In |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 1094 | * addition run the exit method for all existing network |
| 1095 | * namespaces. |
| 1096 | */ |
| 1097 | void unregister_pernet_device(struct pernet_operations *ops) |
| 1098 | { |
Kirill Tkhai | 1a57feb | 2018-02-13 12:26:23 +0300 | [diff] [blame] | 1099 | down_write(&net_sem); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 1100 | if (&ops->list == first_device) |
| 1101 | first_device = first_device->next; |
| 1102 | unregister_pernet_operations(ops); |
Kirill Tkhai | 1a57feb | 2018-02-13 12:26:23 +0300 | [diff] [blame] | 1103 | up_write(&net_sem); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 1104 | } |
| 1105 | EXPORT_SYMBOL_GPL(unregister_pernet_device); |
Eric W. Biederman | 13b6f57 | 2010-03-07 18:14:23 -0800 | [diff] [blame] | 1106 | |
| 1107 | #ifdef CONFIG_NET_NS |
Al Viro | 6496452 | 2014-11-01 00:37:32 -0400 | [diff] [blame] | 1108 | static struct ns_common *netns_get(struct task_struct *task) |
Eric W. Biederman | 13b6f57 | 2010-03-07 18:14:23 -0800 | [diff] [blame] | 1109 | { |
Eric W. Biederman | f063052 | 2011-05-04 17:51:50 -0700 | [diff] [blame] | 1110 | struct net *net = NULL; |
| 1111 | struct nsproxy *nsproxy; |
| 1112 | |
Eric W. Biederman | 728dba3 | 2014-02-03 19:13:49 -0800 | [diff] [blame] | 1113 | task_lock(task); |
| 1114 | nsproxy = task->nsproxy; |
Eric W. Biederman | f063052 | 2011-05-04 17:51:50 -0700 | [diff] [blame] | 1115 | if (nsproxy) |
| 1116 | net = get_net(nsproxy->net_ns); |
Eric W. Biederman | 728dba3 | 2014-02-03 19:13:49 -0800 | [diff] [blame] | 1117 | task_unlock(task); |
Eric W. Biederman | f063052 | 2011-05-04 17:51:50 -0700 | [diff] [blame] | 1118 | |
Al Viro | ff24870 | 2014-11-01 00:10:50 -0400 | [diff] [blame] | 1119 | return net ? &net->ns : NULL; |
| 1120 | } |
| 1121 | |
| 1122 | static inline struct net *to_net_ns(struct ns_common *ns) |
| 1123 | { |
| 1124 | return container_of(ns, struct net, ns); |
Eric W. Biederman | 13b6f57 | 2010-03-07 18:14:23 -0800 | [diff] [blame] | 1125 | } |
| 1126 | |
Al Viro | 6496452 | 2014-11-01 00:37:32 -0400 | [diff] [blame] | 1127 | static void netns_put(struct ns_common *ns) |
Eric W. Biederman | 13b6f57 | 2010-03-07 18:14:23 -0800 | [diff] [blame] | 1128 | { |
Al Viro | ff24870 | 2014-11-01 00:10:50 -0400 | [diff] [blame] | 1129 | put_net(to_net_ns(ns)); |
Eric W. Biederman | 13b6f57 | 2010-03-07 18:14:23 -0800 | [diff] [blame] | 1130 | } |
| 1131 | |
Al Viro | 6496452 | 2014-11-01 00:37:32 -0400 | [diff] [blame] | 1132 | static int netns_install(struct nsproxy *nsproxy, struct ns_common *ns) |
Eric W. Biederman | 13b6f57 | 2010-03-07 18:14:23 -0800 | [diff] [blame] | 1133 | { |
Al Viro | ff24870 | 2014-11-01 00:10:50 -0400 | [diff] [blame] | 1134 | struct net *net = to_net_ns(ns); |
Eric W. Biederman | 142e1d1 | 2012-07-26 01:13:20 -0700 | [diff] [blame] | 1135 | |
Eric W. Biederman | 5e4a084 | 2012-12-14 07:55:36 -0800 | [diff] [blame] | 1136 | if (!ns_capable(net->user_ns, CAP_SYS_ADMIN) || |
Eric W. Biederman | c7b96ac | 2013-03-20 12:49:49 -0700 | [diff] [blame] | 1137 | !ns_capable(current_user_ns(), CAP_SYS_ADMIN)) |
Eric W. Biederman | 142e1d1 | 2012-07-26 01:13:20 -0700 | [diff] [blame] | 1138 | return -EPERM; |
| 1139 | |
Eric W. Biederman | 13b6f57 | 2010-03-07 18:14:23 -0800 | [diff] [blame] | 1140 | put_net(nsproxy->net_ns); |
Eric W. Biederman | 142e1d1 | 2012-07-26 01:13:20 -0700 | [diff] [blame] | 1141 | nsproxy->net_ns = get_net(net); |
Eric W. Biederman | 13b6f57 | 2010-03-07 18:14:23 -0800 | [diff] [blame] | 1142 | return 0; |
| 1143 | } |
| 1144 | |
Andrey Vagin | bcac25a | 2016-09-06 00:47:13 -0700 | [diff] [blame] | 1145 | static struct user_namespace *netns_owner(struct ns_common *ns) |
| 1146 | { |
| 1147 | return to_net_ns(ns)->user_ns; |
| 1148 | } |
| 1149 | |
Eric W. Biederman | 13b6f57 | 2010-03-07 18:14:23 -0800 | [diff] [blame] | 1150 | const struct proc_ns_operations netns_operations = { |
| 1151 | .name = "net", |
| 1152 | .type = CLONE_NEWNET, |
| 1153 | .get = netns_get, |
| 1154 | .put = netns_put, |
| 1155 | .install = netns_install, |
Andrey Vagin | bcac25a | 2016-09-06 00:47:13 -0700 | [diff] [blame] | 1156 | .owner = netns_owner, |
Eric W. Biederman | 13b6f57 | 2010-03-07 18:14:23 -0800 | [diff] [blame] | 1157 | }; |
| 1158 | #endif |