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