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> |
Eric W. Biederman | f063052 | 2011-05-04 17:51:50 -0700 | [diff] [blame] | 13 | #include <linux/proc_fs.h> |
| 14 | #include <linux/file.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 15 | #include <linux/export.h> |
Eric W. Biederman | 038e733 | 2012-06-14 02:31:10 -0700 | [diff] [blame] | 16 | #include <linux/user_namespace.h> |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 17 | #include <net/net_namespace.h> |
Pavel Emelyanov | dec827d | 2008-04-15 00:36:08 -0700 | [diff] [blame] | 18 | #include <net/netns/generic.h> |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 19 | |
| 20 | /* |
| 21 | * Our network namespace constructor/destructor lists |
| 22 | */ |
| 23 | |
| 24 | static LIST_HEAD(pernet_list); |
| 25 | static struct list_head *first_device = &pernet_list; |
| 26 | static DEFINE_MUTEX(net_mutex); |
| 27 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 28 | LIST_HEAD(net_namespace_list); |
Alexey Dobriyan | b76a461 | 2008-10-08 11:35:06 +0200 | [diff] [blame] | 29 | EXPORT_SYMBOL_GPL(net_namespace_list); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 30 | |
Rustad, Mark D | 734b654 | 2012-07-18 09:06:07 +0000 | [diff] [blame] | 31 | struct net init_net = { |
| 32 | .dev_base_head = LIST_HEAD_INIT(init_net.dev_base_head), |
| 33 | }; |
Denis V. Lunev | ff4b950 | 2008-01-22 22:05:33 -0800 | [diff] [blame] | 34 | EXPORT_SYMBOL(init_net); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 35 | |
Pavel Emelyanov | dec827d | 2008-04-15 00:36:08 -0700 | [diff] [blame] | 36 | #define INITIAL_NET_GEN_PTRS 13 /* +1 for len +2 for rcu_head */ |
| 37 | |
Eric Dumazet | 073862b | 2012-01-26 00:41:38 +0000 | [diff] [blame] | 38 | static unsigned int max_gen_ptrs = INITIAL_NET_GEN_PTRS; |
| 39 | |
| 40 | static struct net_generic *net_alloc_generic(void) |
| 41 | { |
| 42 | struct net_generic *ng; |
| 43 | size_t generic_size = offsetof(struct net_generic, ptr[max_gen_ptrs]); |
| 44 | |
| 45 | ng = kzalloc(generic_size, GFP_KERNEL); |
| 46 | if (ng) |
| 47 | ng->len = max_gen_ptrs; |
| 48 | |
| 49 | return ng; |
| 50 | } |
| 51 | |
Jiri Pirko | 05fceb4 | 2010-04-23 01:40:47 +0000 | [diff] [blame] | 52 | static int net_assign_generic(struct net *net, int id, void *data) |
| 53 | { |
| 54 | struct net_generic *ng, *old_ng; |
| 55 | |
| 56 | BUG_ON(!mutex_is_locked(&net_mutex)); |
| 57 | BUG_ON(id == 0); |
| 58 | |
Eric Dumazet | 1c87733 | 2010-10-25 03:20:11 +0000 | [diff] [blame] | 59 | old_ng = rcu_dereference_protected(net->gen, |
| 60 | lockdep_is_held(&net_mutex)); |
| 61 | ng = old_ng; |
Jiri Pirko | 05fceb4 | 2010-04-23 01:40:47 +0000 | [diff] [blame] | 62 | if (old_ng->len >= id) |
| 63 | goto assign; |
| 64 | |
Eric Dumazet | 073862b | 2012-01-26 00:41:38 +0000 | [diff] [blame] | 65 | ng = net_alloc_generic(); |
Jiri Pirko | 05fceb4 | 2010-04-23 01:40:47 +0000 | [diff] [blame] | 66 | if (ng == NULL) |
| 67 | return -ENOMEM; |
| 68 | |
| 69 | /* |
| 70 | * Some synchronisation notes: |
| 71 | * |
| 72 | * The net_generic explores the net->gen array inside rcu |
| 73 | * read section. Besides once set the net->gen->ptr[x] |
| 74 | * pointer never changes (see rules in netns/generic.h). |
| 75 | * |
| 76 | * That said, we simply duplicate this array and schedule |
| 77 | * the old copy for kfree after a grace period. |
| 78 | */ |
| 79 | |
Jiri Pirko | 05fceb4 | 2010-04-23 01:40:47 +0000 | [diff] [blame] | 80 | memcpy(&ng->ptr, &old_ng->ptr, old_ng->len * sizeof(void*)); |
| 81 | |
| 82 | rcu_assign_pointer(net->gen, ng); |
Lai Jiangshan | 04d4dfe | 2011-03-18 12:06:32 +0800 | [diff] [blame] | 83 | kfree_rcu(old_ng, rcu); |
Jiri Pirko | 05fceb4 | 2010-04-23 01:40:47 +0000 | [diff] [blame] | 84 | assign: |
| 85 | ng->ptr[id - 1] = data; |
| 86 | return 0; |
| 87 | } |
| 88 | |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 89 | static int ops_init(const struct pernet_operations *ops, struct net *net) |
| 90 | { |
Julian Anastasov | b922934 | 2012-04-16 04:43:15 +0000 | [diff] [blame] | 91 | int err = -ENOMEM; |
| 92 | void *data = NULL; |
| 93 | |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 94 | if (ops->id && ops->size) { |
Julian Anastasov | b922934 | 2012-04-16 04:43:15 +0000 | [diff] [blame] | 95 | data = kzalloc(ops->size, GFP_KERNEL); |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 96 | if (!data) |
Julian Anastasov | b922934 | 2012-04-16 04:43:15 +0000 | [diff] [blame] | 97 | goto out; |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 98 | |
| 99 | err = net_assign_generic(net, *ops->id, data); |
Julian Anastasov | b922934 | 2012-04-16 04:43:15 +0000 | [diff] [blame] | 100 | if (err) |
| 101 | goto cleanup; |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 102 | } |
Julian Anastasov | b922934 | 2012-04-16 04:43:15 +0000 | [diff] [blame] | 103 | err = 0; |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 104 | if (ops->init) |
Julian Anastasov | b922934 | 2012-04-16 04:43:15 +0000 | [diff] [blame] | 105 | err = ops->init(net); |
| 106 | if (!err) |
| 107 | return 0; |
| 108 | |
| 109 | cleanup: |
| 110 | kfree(data); |
| 111 | |
| 112 | out: |
| 113 | return err; |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static void ops_free(const struct pernet_operations *ops, struct net *net) |
| 117 | { |
| 118 | if (ops->id && ops->size) { |
| 119 | int id = *ops->id; |
| 120 | kfree(net_generic(net, id)); |
| 121 | } |
| 122 | } |
| 123 | |
Eric W. Biederman | 72ad937 | 2009-12-03 02:29:03 +0000 | [diff] [blame] | 124 | static void ops_exit_list(const struct pernet_operations *ops, |
| 125 | struct list_head *net_exit_list) |
| 126 | { |
| 127 | struct net *net; |
| 128 | if (ops->exit) { |
| 129 | list_for_each_entry(net, net_exit_list, exit_list) |
| 130 | ops->exit(net); |
| 131 | } |
Eric W. Biederman | 72ad937 | 2009-12-03 02:29:03 +0000 | [diff] [blame] | 132 | if (ops->exit_batch) |
| 133 | ops->exit_batch(net_exit_list); |
| 134 | } |
| 135 | |
| 136 | static void ops_free_list(const struct pernet_operations *ops, |
| 137 | struct list_head *net_exit_list) |
| 138 | { |
| 139 | struct net *net; |
| 140 | if (ops->size && ops->id) { |
| 141 | list_for_each_entry(net, net_exit_list, exit_list) |
| 142 | ops_free(ops, net); |
| 143 | } |
| 144 | } |
| 145 | |
Pavel Emelyanov | 6a1a3b9 | 2007-11-01 00:44:50 -0700 | [diff] [blame] | 146 | /* |
| 147 | * setup_net runs the initializers for the network namespace object. |
| 148 | */ |
Eric W. Biederman | 038e733 | 2012-06-14 02:31:10 -0700 | [diff] [blame] | 149 | 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] | 150 | { |
| 151 | /* Must be called with net_mutex held */ |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 152 | const struct pernet_operations *ops, *saved_ops; |
Daniel Lezcano | 486a87f | 2009-02-22 00:07:53 -0800 | [diff] [blame] | 153 | int error = 0; |
Eric W. Biederman | 72ad937 | 2009-12-03 02:29:03 +0000 | [diff] [blame] | 154 | LIST_HEAD(net_exit_list); |
Pavel Emelyanov | 6a1a3b9 | 2007-11-01 00:44:50 -0700 | [diff] [blame] | 155 | |
| 156 | atomic_set(&net->count, 1); |
Al Viro | a685e08 | 2011-06-08 21:13:01 -0400 | [diff] [blame] | 157 | atomic_set(&net->passive, 1); |
Thomas Graf | 4e985ad | 2011-06-21 03:11:20 +0000 | [diff] [blame] | 158 | net->dev_base_seq = 1; |
Eric W. Biederman | 038e733 | 2012-06-14 02:31:10 -0700 | [diff] [blame] | 159 | net->user_ns = user_ns; |
Daniel Lezcano | 486a87f | 2009-02-22 00:07:53 -0800 | [diff] [blame] | 160 | |
Denis V. Lunev | 5d1e446 | 2008-04-16 01:58:04 -0700 | [diff] [blame] | 161 | #ifdef NETNS_REFCNT_DEBUG |
Pavel Emelyanov | 6a1a3b9 | 2007-11-01 00:44:50 -0700 | [diff] [blame] | 162 | atomic_set(&net->use_count, 0); |
Denis V. Lunev | 5d1e446 | 2008-04-16 01:58:04 -0700 | [diff] [blame] | 163 | #endif |
Pavel Emelyanov | 6a1a3b9 | 2007-11-01 00:44:50 -0700 | [diff] [blame] | 164 | |
Pavel Emelyanov | 6a1a3b9 | 2007-11-01 00:44:50 -0700 | [diff] [blame] | 165 | list_for_each_entry(ops, &pernet_list, list) { |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 166 | error = ops_init(ops, net); |
| 167 | if (error < 0) |
| 168 | goto out_undo; |
Pavel Emelyanov | 6a1a3b9 | 2007-11-01 00:44:50 -0700 | [diff] [blame] | 169 | } |
| 170 | out: |
| 171 | return error; |
| 172 | |
| 173 | out_undo: |
| 174 | /* Walk through the list backwards calling the exit functions |
| 175 | * for the pernet modules whose init functions did not fail. |
| 176 | */ |
Eric W. Biederman | 72ad937 | 2009-12-03 02:29:03 +0000 | [diff] [blame] | 177 | list_add(&net->exit_list, &net_exit_list); |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 178 | saved_ops = ops; |
Eric W. Biederman | 72ad937 | 2009-12-03 02:29:03 +0000 | [diff] [blame] | 179 | list_for_each_entry_continue_reverse(ops, &pernet_list, list) |
| 180 | ops_exit_list(ops, &net_exit_list); |
| 181 | |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 182 | ops = saved_ops; |
| 183 | list_for_each_entry_continue_reverse(ops, &pernet_list, list) |
Eric W. Biederman | 72ad937 | 2009-12-03 02:29:03 +0000 | [diff] [blame] | 184 | ops_free_list(ops, &net_exit_list); |
Pavel Emelyanov | 6a1a3b9 | 2007-11-01 00:44:50 -0700 | [diff] [blame] | 185 | |
| 186 | rcu_barrier(); |
| 187 | goto out; |
| 188 | } |
| 189 | |
Daniel Lezcano | 486a87f | 2009-02-22 00:07:53 -0800 | [diff] [blame] | 190 | |
Clemens Noss | ebe47d4 | 2009-02-23 15:37:35 -0800 | [diff] [blame] | 191 | #ifdef CONFIG_NET_NS |
| 192 | static struct kmem_cache *net_cachep; |
| 193 | static struct workqueue_struct *netns_wq; |
| 194 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 195 | static struct net *net_alloc(void) |
| 196 | { |
Daniel Lezcano | 486a87f | 2009-02-22 00:07:53 -0800 | [diff] [blame] | 197 | struct net *net = NULL; |
| 198 | struct net_generic *ng; |
| 199 | |
| 200 | ng = net_alloc_generic(); |
| 201 | if (!ng) |
| 202 | goto out; |
| 203 | |
| 204 | net = kmem_cache_zalloc(net_cachep, GFP_KERNEL); |
| 205 | if (!net) |
| 206 | goto out_free; |
| 207 | |
| 208 | rcu_assign_pointer(net->gen, ng); |
| 209 | out: |
| 210 | return net; |
| 211 | |
| 212 | out_free: |
| 213 | kfree(ng); |
| 214 | goto out; |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 215 | } |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 216 | |
Johann Felix Soden | 45a19b0 | 2007-11-07 01:30:30 -0800 | [diff] [blame] | 217 | static void net_free(struct net *net) |
| 218 | { |
Denis V. Lunev | 5d1e446 | 2008-04-16 01:58:04 -0700 | [diff] [blame] | 219 | #ifdef NETNS_REFCNT_DEBUG |
Johann Felix Soden | 45a19b0 | 2007-11-07 01:30:30 -0800 | [diff] [blame] | 220 | if (unlikely(atomic_read(&net->use_count) != 0)) { |
Joe Perches | e005d19 | 2012-05-16 19:58:40 +0000 | [diff] [blame] | 221 | pr_emerg("network namespace not free! Usage: %d\n", |
| 222 | atomic_read(&net->use_count)); |
Johann Felix Soden | 45a19b0 | 2007-11-07 01:30:30 -0800 | [diff] [blame] | 223 | return; |
| 224 | } |
Denis V. Lunev | 5d1e446 | 2008-04-16 01:58:04 -0700 | [diff] [blame] | 225 | #endif |
Alexey Dobriyan | 4ef079c | 2008-10-14 22:54:48 -0700 | [diff] [blame] | 226 | kfree(net->gen); |
Johann Felix Soden | 45a19b0 | 2007-11-07 01:30:30 -0800 | [diff] [blame] | 227 | kmem_cache_free(net_cachep, net); |
| 228 | } |
| 229 | |
Al Viro | a685e08 | 2011-06-08 21:13:01 -0400 | [diff] [blame] | 230 | void net_drop_ns(void *p) |
| 231 | { |
| 232 | struct net *ns = p; |
| 233 | if (ns && atomic_dec_and_test(&ns->passive)) |
| 234 | net_free(ns); |
| 235 | } |
| 236 | |
Eric W. Biederman | 038e733 | 2012-06-14 02:31:10 -0700 | [diff] [blame] | 237 | struct net *copy_net_ns(unsigned long flags, |
| 238 | struct user_namespace *user_ns, struct net *old_net) |
Pavel Emelyanov | 6a1a3b9 | 2007-11-01 00:44:50 -0700 | [diff] [blame] | 239 | { |
Alexey Dobriyan | 088eb2d | 2009-05-04 11:12:14 -0700 | [diff] [blame] | 240 | struct net *net; |
| 241 | int rv; |
Pavel Emelyanov | 6a1a3b9 | 2007-11-01 00:44:50 -0700 | [diff] [blame] | 242 | |
Rob Landley | 911cb19 | 2011-04-15 02:26:25 +0000 | [diff] [blame] | 243 | if (!(flags & CLONE_NEWNET)) |
| 244 | return get_net(old_net); |
| 245 | |
Alexey Dobriyan | 088eb2d | 2009-05-04 11:12:14 -0700 | [diff] [blame] | 246 | net = net_alloc(); |
| 247 | if (!net) |
| 248 | return ERR_PTR(-ENOMEM); |
Eric W. Biederman | 038e733 | 2012-06-14 02:31:10 -0700 | [diff] [blame] | 249 | |
| 250 | get_user_ns(user_ns); |
| 251 | |
Pavel Emelyanov | 6a1a3b9 | 2007-11-01 00:44:50 -0700 | [diff] [blame] | 252 | mutex_lock(&net_mutex); |
Eric W. Biederman | 038e733 | 2012-06-14 02:31:10 -0700 | [diff] [blame] | 253 | rv = setup_net(net, user_ns); |
Alexey Dobriyan | 088eb2d | 2009-05-04 11:12:14 -0700 | [diff] [blame] | 254 | if (rv == 0) { |
Daniel Lezcano | 486a87f | 2009-02-22 00:07:53 -0800 | [diff] [blame] | 255 | rtnl_lock(); |
Johannes Berg | 11a28d3 | 2009-07-10 09:51:33 +0000 | [diff] [blame] | 256 | list_add_tail_rcu(&net->list, &net_namespace_list); |
Daniel Lezcano | 486a87f | 2009-02-22 00:07:53 -0800 | [diff] [blame] | 257 | rtnl_unlock(); |
| 258 | } |
Pavel Emelyanov | 6a1a3b9 | 2007-11-01 00:44:50 -0700 | [diff] [blame] | 259 | mutex_unlock(&net_mutex); |
Alexey Dobriyan | 088eb2d | 2009-05-04 11:12:14 -0700 | [diff] [blame] | 260 | if (rv < 0) { |
Eric W. Biederman | 038e733 | 2012-06-14 02:31:10 -0700 | [diff] [blame] | 261 | put_user_ns(user_ns); |
Al Viro | a685e08 | 2011-06-08 21:13:01 -0400 | [diff] [blame] | 262 | net_drop_ns(net); |
Alexey Dobriyan | 088eb2d | 2009-05-04 11:12:14 -0700 | [diff] [blame] | 263 | return ERR_PTR(rv); |
| 264 | } |
| 265 | return net; |
| 266 | } |
Daniel Lezcano | 486a87f | 2009-02-22 00:07:53 -0800 | [diff] [blame] | 267 | |
Eric W. Biederman | 2b035b3 | 2009-11-29 22:25:27 +0000 | [diff] [blame] | 268 | static DEFINE_SPINLOCK(cleanup_list_lock); |
| 269 | static LIST_HEAD(cleanup_list); /* Must hold cleanup_list_lock to touch */ |
| 270 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 271 | static void cleanup_net(struct work_struct *work) |
| 272 | { |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 273 | const struct pernet_operations *ops; |
Eric W. Biederman | 2b035b3 | 2009-11-29 22:25:27 +0000 | [diff] [blame] | 274 | struct net *net, *tmp; |
| 275 | LIST_HEAD(net_kill_list); |
Eric W. Biederman | 72ad937 | 2009-12-03 02:29:03 +0000 | [diff] [blame] | 276 | LIST_HEAD(net_exit_list); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 277 | |
Eric W. Biederman | 2b035b3 | 2009-11-29 22:25:27 +0000 | [diff] [blame] | 278 | /* Atomically snapshot the list of namespaces to cleanup */ |
| 279 | spin_lock_irq(&cleanup_list_lock); |
| 280 | list_replace_init(&cleanup_list, &net_kill_list); |
| 281 | spin_unlock_irq(&cleanup_list_lock); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 282 | |
| 283 | mutex_lock(&net_mutex); |
| 284 | |
| 285 | /* Don't let anyone else find us. */ |
Eric W. Biederman | f4618d3 | 2007-09-26 22:40:08 -0700 | [diff] [blame] | 286 | rtnl_lock(); |
Eric W. Biederman | 72ad937 | 2009-12-03 02:29:03 +0000 | [diff] [blame] | 287 | list_for_each_entry(net, &net_kill_list, cleanup_list) { |
Eric W. Biederman | 2b035b3 | 2009-11-29 22:25:27 +0000 | [diff] [blame] | 288 | list_del_rcu(&net->list); |
Eric W. Biederman | 72ad937 | 2009-12-03 02:29:03 +0000 | [diff] [blame] | 289 | list_add_tail(&net->exit_list, &net_exit_list); |
| 290 | } |
Eric W. Biederman | f4618d3 | 2007-09-26 22:40:08 -0700 | [diff] [blame] | 291 | rtnl_unlock(); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 292 | |
Johannes Berg | 11a28d3 | 2009-07-10 09:51:33 +0000 | [diff] [blame] | 293 | /* |
| 294 | * Another CPU might be rcu-iterating the list, wait for it. |
| 295 | * This needs to be before calling the exit() notifiers, so |
| 296 | * the rcu_barrier() below isn't sufficient alone. |
| 297 | */ |
| 298 | synchronize_rcu(); |
| 299 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 300 | /* Run all of the network namespace exit methods */ |
Eric W. Biederman | 72ad937 | 2009-12-03 02:29:03 +0000 | [diff] [blame] | 301 | list_for_each_entry_reverse(ops, &pernet_list, list) |
| 302 | ops_exit_list(ops, &net_exit_list); |
| 303 | |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 304 | /* Free the net generic variables */ |
Eric W. Biederman | 72ad937 | 2009-12-03 02:29:03 +0000 | [diff] [blame] | 305 | list_for_each_entry_reverse(ops, &pernet_list, list) |
| 306 | ops_free_list(ops, &net_exit_list); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 307 | |
| 308 | mutex_unlock(&net_mutex); |
| 309 | |
| 310 | /* Ensure there are no outstanding rcu callbacks using this |
| 311 | * network namespace. |
| 312 | */ |
| 313 | rcu_barrier(); |
| 314 | |
| 315 | /* Finally it is safe to free my network namespace structure */ |
Eric W. Biederman | 72ad937 | 2009-12-03 02:29:03 +0000 | [diff] [blame] | 316 | list_for_each_entry_safe(net, tmp, &net_exit_list, exit_list) { |
| 317 | list_del_init(&net->exit_list); |
Eric W. Biederman | 038e733 | 2012-06-14 02:31:10 -0700 | [diff] [blame] | 318 | put_user_ns(net->user_ns); |
Al Viro | a685e08 | 2011-06-08 21:13:01 -0400 | [diff] [blame] | 319 | net_drop_ns(net); |
Eric W. Biederman | 2b035b3 | 2009-11-29 22:25:27 +0000 | [diff] [blame] | 320 | } |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 321 | } |
Eric W. Biederman | 2b035b3 | 2009-11-29 22:25:27 +0000 | [diff] [blame] | 322 | static DECLARE_WORK(net_cleanup_work, cleanup_net); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 323 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 324 | void __put_net(struct net *net) |
| 325 | { |
| 326 | /* Cleanup the network namespace in process context */ |
Eric W. Biederman | 2b035b3 | 2009-11-29 22:25:27 +0000 | [diff] [blame] | 327 | unsigned long flags; |
| 328 | |
| 329 | spin_lock_irqsave(&cleanup_list_lock, flags); |
| 330 | list_add(&net->cleanup_list, &cleanup_list); |
| 331 | spin_unlock_irqrestore(&cleanup_list_lock, flags); |
| 332 | |
| 333 | queue_work(netns_wq, &net_cleanup_work); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 334 | } |
| 335 | EXPORT_SYMBOL_GPL(__put_net); |
| 336 | |
Stephen Rothwell | 956c920 | 2011-05-12 13:51:13 +1000 | [diff] [blame] | 337 | struct net *get_net_ns_by_fd(int fd) |
| 338 | { |
| 339 | struct proc_inode *ei; |
| 340 | struct file *file; |
| 341 | struct net *net; |
| 342 | |
Stephen Rothwell | 956c920 | 2011-05-12 13:51:13 +1000 | [diff] [blame] | 343 | file = proc_ns_fget(fd); |
Al Viro | c316e6a | 2011-06-05 00:37:35 +0000 | [diff] [blame] | 344 | if (IS_ERR(file)) |
| 345 | return ERR_CAST(file); |
Stephen Rothwell | 956c920 | 2011-05-12 13:51:13 +1000 | [diff] [blame] | 346 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 347 | ei = PROC_I(file_inode(file)); |
Al Viro | c316e6a | 2011-06-05 00:37:35 +0000 | [diff] [blame] | 348 | if (ei->ns_ops == &netns_operations) |
| 349 | net = get_net(ei->ns); |
| 350 | else |
| 351 | net = ERR_PTR(-EINVAL); |
Stephen Rothwell | 956c920 | 2011-05-12 13:51:13 +1000 | [diff] [blame] | 352 | |
Al Viro | c316e6a | 2011-06-05 00:37:35 +0000 | [diff] [blame] | 353 | fput(file); |
Stephen Rothwell | 956c920 | 2011-05-12 13:51:13 +1000 | [diff] [blame] | 354 | return net; |
| 355 | } |
| 356 | |
Pavel Emelyanov | 6a1a3b9 | 2007-11-01 00:44:50 -0700 | [diff] [blame] | 357 | #else |
Stephen Rothwell | 956c920 | 2011-05-12 13:51:13 +1000 | [diff] [blame] | 358 | struct net *get_net_ns_by_fd(int fd) |
| 359 | { |
| 360 | return ERR_PTR(-EINVAL); |
| 361 | } |
Pavel Emelyanov | 6a1a3b9 | 2007-11-01 00:44:50 -0700 | [diff] [blame] | 362 | #endif |
Eric W. Biederman | 9dd776b | 2007-09-26 22:04:26 -0700 | [diff] [blame] | 363 | |
Johannes Berg | 30ffee8 | 2009-07-10 09:51:35 +0000 | [diff] [blame] | 364 | struct net *get_net_ns_by_pid(pid_t pid) |
| 365 | { |
| 366 | struct task_struct *tsk; |
| 367 | struct net *net; |
| 368 | |
| 369 | /* Lookup the network namespace */ |
| 370 | net = ERR_PTR(-ESRCH); |
| 371 | rcu_read_lock(); |
| 372 | tsk = find_task_by_vpid(pid); |
| 373 | if (tsk) { |
| 374 | struct nsproxy *nsproxy; |
| 375 | nsproxy = task_nsproxy(tsk); |
| 376 | if (nsproxy) |
| 377 | net = get_net(nsproxy->net_ns); |
| 378 | } |
| 379 | rcu_read_unlock(); |
| 380 | return net; |
| 381 | } |
| 382 | EXPORT_SYMBOL_GPL(get_net_ns_by_pid); |
| 383 | |
Eric W. Biederman | 98f842e | 2011-06-15 10:21:48 -0700 | [diff] [blame] | 384 | static __net_init int net_ns_net_init(struct net *net) |
| 385 | { |
| 386 | return proc_alloc_inum(&net->proc_inum); |
| 387 | } |
| 388 | |
| 389 | static __net_exit void net_ns_net_exit(struct net *net) |
| 390 | { |
| 391 | proc_free_inum(net->proc_inum); |
| 392 | } |
| 393 | |
| 394 | static struct pernet_operations __net_initdata net_ns_ops = { |
| 395 | .init = net_ns_net_init, |
| 396 | .exit = net_ns_net_exit, |
| 397 | }; |
| 398 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 399 | static int __init net_ns_init(void) |
| 400 | { |
Daniel Lezcano | 486a87f | 2009-02-22 00:07:53 -0800 | [diff] [blame] | 401 | struct net_generic *ng; |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 402 | |
Pavel Emelyanov | d57a921 | 2007-11-01 00:46:50 -0700 | [diff] [blame] | 403 | #ifdef CONFIG_NET_NS |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 404 | net_cachep = kmem_cache_create("net_namespace", sizeof(struct net), |
| 405 | SMP_CACHE_BYTES, |
| 406 | SLAB_PANIC, NULL); |
Benjamin Thery | 3ef1355 | 2007-11-19 23:18:16 -0800 | [diff] [blame] | 407 | |
| 408 | /* Create workqueue for cleanup */ |
| 409 | netns_wq = create_singlethread_workqueue("netns"); |
| 410 | if (!netns_wq) |
| 411 | panic("Could not create netns workq"); |
Pavel Emelyanov | d57a921 | 2007-11-01 00:46:50 -0700 | [diff] [blame] | 412 | #endif |
Benjamin Thery | 3ef1355 | 2007-11-19 23:18:16 -0800 | [diff] [blame] | 413 | |
Daniel Lezcano | 486a87f | 2009-02-22 00:07:53 -0800 | [diff] [blame] | 414 | ng = net_alloc_generic(); |
| 415 | if (!ng) |
| 416 | panic("Could not allocate generic netns"); |
| 417 | |
| 418 | rcu_assign_pointer(init_net.gen, ng); |
| 419 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 420 | mutex_lock(&net_mutex); |
Eric W. Biederman | 038e733 | 2012-06-14 02:31:10 -0700 | [diff] [blame] | 421 | if (setup_net(&init_net, &init_user_ns)) |
Stephen Hemminger | ca0f311 | 2009-05-21 15:10:31 -0700 | [diff] [blame] | 422 | panic("Could not setup the initial network namespace"); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 423 | |
Eric W. Biederman | f4618d3 | 2007-09-26 22:40:08 -0700 | [diff] [blame] | 424 | rtnl_lock(); |
Johannes Berg | 11a28d3 | 2009-07-10 09:51:33 +0000 | [diff] [blame] | 425 | list_add_tail_rcu(&init_net.list, &net_namespace_list); |
Eric W. Biederman | f4618d3 | 2007-09-26 22:40:08 -0700 | [diff] [blame] | 426 | rtnl_unlock(); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 427 | |
| 428 | mutex_unlock(&net_mutex); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 429 | |
Eric W. Biederman | 98f842e | 2011-06-15 10:21:48 -0700 | [diff] [blame] | 430 | register_pernet_subsys(&net_ns_ops); |
| 431 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | pure_initcall(net_ns_init); |
| 436 | |
Denis V. Lunev | ed160e8 | 2007-11-13 03:23:21 -0800 | [diff] [blame] | 437 | #ifdef CONFIG_NET_NS |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 438 | static int __register_pernet_operations(struct list_head *list, |
| 439 | struct pernet_operations *ops) |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 440 | { |
Eric W. Biederman | 72ad937 | 2009-12-03 02:29:03 +0000 | [diff] [blame] | 441 | struct net *net; |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 442 | int error; |
Eric W. Biederman | 72ad937 | 2009-12-03 02:29:03 +0000 | [diff] [blame] | 443 | LIST_HEAD(net_exit_list); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 444 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 445 | list_add_tail(&ops->list, list); |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 446 | if (ops->init || (ops->id && ops->size)) { |
Pavel Emelyanov | 1dba323 | 2007-11-01 00:42:43 -0700 | [diff] [blame] | 447 | for_each_net(net) { |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 448 | error = ops_init(ops, net); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 449 | if (error) |
| 450 | goto out_undo; |
Eric W. Biederman | 72ad937 | 2009-12-03 02:29:03 +0000 | [diff] [blame] | 451 | list_add_tail(&net->exit_list, &net_exit_list); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 452 | } |
| 453 | } |
Pavel Emelyanov | 1dba323 | 2007-11-01 00:42:43 -0700 | [diff] [blame] | 454 | return 0; |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 455 | |
| 456 | out_undo: |
| 457 | /* If I have an error cleanup all namespaces I initialized */ |
| 458 | list_del(&ops->list); |
Eric W. Biederman | 72ad937 | 2009-12-03 02:29:03 +0000 | [diff] [blame] | 459 | ops_exit_list(ops, &net_exit_list); |
| 460 | ops_free_list(ops, &net_exit_list); |
Pavel Emelyanov | 1dba323 | 2007-11-01 00:42:43 -0700 | [diff] [blame] | 461 | return error; |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 462 | } |
| 463 | |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 464 | static void __unregister_pernet_operations(struct pernet_operations *ops) |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 465 | { |
| 466 | struct net *net; |
Eric W. Biederman | 72ad937 | 2009-12-03 02:29:03 +0000 | [diff] [blame] | 467 | LIST_HEAD(net_exit_list); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 468 | |
| 469 | list_del(&ops->list); |
Eric W. Biederman | 72ad937 | 2009-12-03 02:29:03 +0000 | [diff] [blame] | 470 | for_each_net(net) |
| 471 | list_add_tail(&net->exit_list, &net_exit_list); |
| 472 | ops_exit_list(ops, &net_exit_list); |
| 473 | ops_free_list(ops, &net_exit_list); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 474 | } |
| 475 | |
Denis V. Lunev | ed160e8 | 2007-11-13 03:23:21 -0800 | [diff] [blame] | 476 | #else |
| 477 | |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 478 | static int __register_pernet_operations(struct list_head *list, |
| 479 | struct pernet_operations *ops) |
| 480 | { |
Julian Anastasov | b922934 | 2012-04-16 04:43:15 +0000 | [diff] [blame] | 481 | return ops_init(ops, &init_net); |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | static void __unregister_pernet_operations(struct pernet_operations *ops) |
| 485 | { |
Eric W. Biederman | 72ad937 | 2009-12-03 02:29:03 +0000 | [diff] [blame] | 486 | LIST_HEAD(net_exit_list); |
| 487 | list_add(&init_net.exit_list, &net_exit_list); |
| 488 | ops_exit_list(ops, &net_exit_list); |
| 489 | ops_free_list(ops, &net_exit_list); |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | #endif /* CONFIG_NET_NS */ |
| 493 | |
| 494 | static DEFINE_IDA(net_generic_ids); |
| 495 | |
Denis V. Lunev | ed160e8 | 2007-11-13 03:23:21 -0800 | [diff] [blame] | 496 | static int register_pernet_operations(struct list_head *list, |
| 497 | struct pernet_operations *ops) |
| 498 | { |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 499 | int error; |
| 500 | |
| 501 | if (ops->id) { |
| 502 | again: |
| 503 | error = ida_get_new_above(&net_generic_ids, 1, ops->id); |
| 504 | if (error < 0) { |
| 505 | if (error == -EAGAIN) { |
| 506 | ida_pre_get(&net_generic_ids, GFP_KERNEL); |
| 507 | goto again; |
| 508 | } |
| 509 | return error; |
| 510 | } |
Eric Dumazet | 073862b | 2012-01-26 00:41:38 +0000 | [diff] [blame] | 511 | max_gen_ptrs = max_t(unsigned int, max_gen_ptrs, *ops->id); |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 512 | } |
| 513 | error = __register_pernet_operations(list, ops); |
Eric W. Biederman | 3a765ed | 2009-12-03 02:29:06 +0000 | [diff] [blame] | 514 | if (error) { |
| 515 | rcu_barrier(); |
| 516 | if (ops->id) |
| 517 | ida_remove(&net_generic_ids, *ops->id); |
| 518 | } |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 519 | |
| 520 | return error; |
Denis V. Lunev | ed160e8 | 2007-11-13 03:23:21 -0800 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | static void unregister_pernet_operations(struct pernet_operations *ops) |
| 524 | { |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 525 | |
| 526 | __unregister_pernet_operations(ops); |
Eric W. Biederman | 3a765ed | 2009-12-03 02:29:06 +0000 | [diff] [blame] | 527 | rcu_barrier(); |
Eric W. Biederman | f875bae | 2009-11-29 22:25:28 +0000 | [diff] [blame] | 528 | if (ops->id) |
| 529 | ida_remove(&net_generic_ids, *ops->id); |
Denis V. Lunev | ed160e8 | 2007-11-13 03:23:21 -0800 | [diff] [blame] | 530 | } |
Pavel Emelyanov | c93cf61 | 2008-04-15 00:35:23 -0700 | [diff] [blame] | 531 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 532 | /** |
| 533 | * register_pernet_subsys - register a network namespace subsystem |
| 534 | * @ops: pernet operations structure for the subsystem |
| 535 | * |
| 536 | * Register a subsystem which has init and exit functions |
| 537 | * that are called when network namespaces are created and |
| 538 | * destroyed respectively. |
| 539 | * |
| 540 | * When registered all network namespace init functions are |
| 541 | * called for every existing network namespace. Allowing kernel |
| 542 | * modules to have a race free view of the set of network namespaces. |
| 543 | * |
| 544 | * When a new network namespace is created all of the init |
| 545 | * methods are called in the order in which they were registered. |
| 546 | * |
| 547 | * When a network namespace is destroyed all of the exit methods |
| 548 | * are called in the reverse of the order with which they were |
| 549 | * registered. |
| 550 | */ |
| 551 | int register_pernet_subsys(struct pernet_operations *ops) |
| 552 | { |
| 553 | int error; |
| 554 | mutex_lock(&net_mutex); |
| 555 | error = register_pernet_operations(first_device, ops); |
| 556 | mutex_unlock(&net_mutex); |
| 557 | return error; |
| 558 | } |
| 559 | EXPORT_SYMBOL_GPL(register_pernet_subsys); |
| 560 | |
| 561 | /** |
| 562 | * unregister_pernet_subsys - unregister a network namespace subsystem |
| 563 | * @ops: pernet operations structure to manipulate |
| 564 | * |
| 565 | * Remove the pernet operations structure from the list to be |
Oliver Pinter | 53379e5 | 2008-02-03 17:56:48 +0200 | [diff] [blame] | 566 | * used when network namespaces are created or destroyed. In |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 567 | * addition run the exit method for all existing network |
| 568 | * namespaces. |
| 569 | */ |
Jiri Pirko | b3c981d | 2010-04-25 00:49:56 -0700 | [diff] [blame] | 570 | void unregister_pernet_subsys(struct pernet_operations *ops) |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 571 | { |
| 572 | mutex_lock(&net_mutex); |
Jiri Pirko | b3c981d | 2010-04-25 00:49:56 -0700 | [diff] [blame] | 573 | unregister_pernet_operations(ops); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 574 | mutex_unlock(&net_mutex); |
| 575 | } |
| 576 | EXPORT_SYMBOL_GPL(unregister_pernet_subsys); |
| 577 | |
| 578 | /** |
| 579 | * register_pernet_device - register a network namespace device |
| 580 | * @ops: pernet operations structure for the subsystem |
| 581 | * |
| 582 | * Register a device which has init and exit functions |
| 583 | * that are called when network namespaces are created and |
| 584 | * destroyed respectively. |
| 585 | * |
| 586 | * When registered all network namespace init functions are |
| 587 | * called for every existing network namespace. Allowing kernel |
| 588 | * modules to have a race free view of the set of network namespaces. |
| 589 | * |
| 590 | * When a new network namespace is created all of the init |
| 591 | * methods are called in the order in which they were registered. |
| 592 | * |
| 593 | * When a network namespace is destroyed all of the exit methods |
| 594 | * are called in the reverse of the order with which they were |
| 595 | * registered. |
| 596 | */ |
| 597 | int register_pernet_device(struct pernet_operations *ops) |
| 598 | { |
| 599 | int error; |
| 600 | mutex_lock(&net_mutex); |
| 601 | error = register_pernet_operations(&pernet_list, ops); |
| 602 | if (!error && (first_device == &pernet_list)) |
| 603 | first_device = &ops->list; |
| 604 | mutex_unlock(&net_mutex); |
| 605 | return error; |
| 606 | } |
| 607 | EXPORT_SYMBOL_GPL(register_pernet_device); |
| 608 | |
| 609 | /** |
| 610 | * unregister_pernet_device - unregister a network namespace netdevice |
| 611 | * @ops: pernet operations structure to manipulate |
| 612 | * |
| 613 | * Remove the pernet operations structure from the list to be |
Oliver Pinter | 53379e5 | 2008-02-03 17:56:48 +0200 | [diff] [blame] | 614 | * used when network namespaces are created or destroyed. In |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 615 | * addition run the exit method for all existing network |
| 616 | * namespaces. |
| 617 | */ |
| 618 | void unregister_pernet_device(struct pernet_operations *ops) |
| 619 | { |
| 620 | mutex_lock(&net_mutex); |
| 621 | if (&ops->list == first_device) |
| 622 | first_device = first_device->next; |
| 623 | unregister_pernet_operations(ops); |
| 624 | mutex_unlock(&net_mutex); |
| 625 | } |
| 626 | EXPORT_SYMBOL_GPL(unregister_pernet_device); |
Eric W. Biederman | 13b6f57 | 2010-03-07 18:14:23 -0800 | [diff] [blame] | 627 | |
| 628 | #ifdef CONFIG_NET_NS |
| 629 | static void *netns_get(struct task_struct *task) |
| 630 | { |
Eric W. Biederman | f063052 | 2011-05-04 17:51:50 -0700 | [diff] [blame] | 631 | struct net *net = NULL; |
| 632 | struct nsproxy *nsproxy; |
| 633 | |
Eric W. Biederman | 13b6f57 | 2010-03-07 18:14:23 -0800 | [diff] [blame] | 634 | rcu_read_lock(); |
Eric W. Biederman | f063052 | 2011-05-04 17:51:50 -0700 | [diff] [blame] | 635 | nsproxy = task_nsproxy(task); |
| 636 | if (nsproxy) |
| 637 | net = get_net(nsproxy->net_ns); |
Eric W. Biederman | 13b6f57 | 2010-03-07 18:14:23 -0800 | [diff] [blame] | 638 | rcu_read_unlock(); |
Eric W. Biederman | f063052 | 2011-05-04 17:51:50 -0700 | [diff] [blame] | 639 | |
Eric W. Biederman | 13b6f57 | 2010-03-07 18:14:23 -0800 | [diff] [blame] | 640 | return net; |
| 641 | } |
| 642 | |
| 643 | static void netns_put(void *ns) |
| 644 | { |
| 645 | put_net(ns); |
| 646 | } |
| 647 | |
| 648 | static int netns_install(struct nsproxy *nsproxy, void *ns) |
| 649 | { |
Eric W. Biederman | 142e1d1 | 2012-07-26 01:13:20 -0700 | [diff] [blame] | 650 | struct net *net = ns; |
| 651 | |
Eric W. Biederman | 5e4a084 | 2012-12-14 07:55:36 -0800 | [diff] [blame] | 652 | if (!ns_capable(net->user_ns, CAP_SYS_ADMIN) || |
| 653 | !nsown_capable(CAP_SYS_ADMIN)) |
Eric W. Biederman | 142e1d1 | 2012-07-26 01:13:20 -0700 | [diff] [blame] | 654 | return -EPERM; |
| 655 | |
Eric W. Biederman | 13b6f57 | 2010-03-07 18:14:23 -0800 | [diff] [blame] | 656 | put_net(nsproxy->net_ns); |
Eric W. Biederman | 142e1d1 | 2012-07-26 01:13:20 -0700 | [diff] [blame] | 657 | nsproxy->net_ns = get_net(net); |
Eric W. Biederman | 13b6f57 | 2010-03-07 18:14:23 -0800 | [diff] [blame] | 658 | return 0; |
| 659 | } |
| 660 | |
Eric W. Biederman | 98f842e | 2011-06-15 10:21:48 -0700 | [diff] [blame] | 661 | static unsigned int netns_inum(void *ns) |
| 662 | { |
| 663 | struct net *net = ns; |
| 664 | return net->proc_inum; |
| 665 | } |
| 666 | |
Eric W. Biederman | 13b6f57 | 2010-03-07 18:14:23 -0800 | [diff] [blame] | 667 | const struct proc_ns_operations netns_operations = { |
| 668 | .name = "net", |
| 669 | .type = CLONE_NEWNET, |
| 670 | .get = netns_get, |
| 671 | .put = netns_put, |
| 672 | .install = netns_install, |
Eric W. Biederman | 98f842e | 2011-06-15 10:21:48 -0700 | [diff] [blame] | 673 | .inum = netns_inum, |
Eric W. Biederman | 13b6f57 | 2010-03-07 18:14:23 -0800 | [diff] [blame] | 674 | }; |
| 675 | #endif |