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> |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 8 | #include <net/net_namespace.h> |
| 9 | |
| 10 | /* |
| 11 | * Our network namespace constructor/destructor lists |
| 12 | */ |
| 13 | |
| 14 | static LIST_HEAD(pernet_list); |
| 15 | static struct list_head *first_device = &pernet_list; |
| 16 | static DEFINE_MUTEX(net_mutex); |
| 17 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 18 | LIST_HEAD(net_namespace_list); |
| 19 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 20 | struct net init_net; |
Denis V. Lunev | ff4b950 | 2008-01-22 22:05:33 -0800 | [diff] [blame] | 21 | EXPORT_SYMBOL(init_net); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 22 | |
Pavel Emelyanov | 6a1a3b9 | 2007-11-01 00:44:50 -0700 | [diff] [blame] | 23 | /* |
| 24 | * setup_net runs the initializers for the network namespace object. |
| 25 | */ |
Pavel Emelyanov | 1a2ee93 | 2007-11-01 00:45:59 -0700 | [diff] [blame] | 26 | static __net_init int setup_net(struct net *net) |
Pavel Emelyanov | 6a1a3b9 | 2007-11-01 00:44:50 -0700 | [diff] [blame] | 27 | { |
| 28 | /* Must be called with net_mutex held */ |
| 29 | struct pernet_operations *ops; |
| 30 | int error; |
| 31 | |
| 32 | atomic_set(&net->count, 1); |
| 33 | atomic_set(&net->use_count, 0); |
| 34 | |
| 35 | error = 0; |
| 36 | list_for_each_entry(ops, &pernet_list, list) { |
| 37 | if (ops->init) { |
| 38 | error = ops->init(net); |
| 39 | if (error < 0) |
| 40 | goto out_undo; |
| 41 | } |
| 42 | } |
| 43 | out: |
| 44 | return error; |
| 45 | |
| 46 | out_undo: |
| 47 | /* Walk through the list backwards calling the exit functions |
| 48 | * for the pernet modules whose init functions did not fail. |
| 49 | */ |
| 50 | list_for_each_entry_continue_reverse(ops, &pernet_list, list) { |
| 51 | if (ops->exit) |
| 52 | ops->exit(net); |
| 53 | } |
| 54 | |
| 55 | rcu_barrier(); |
| 56 | goto out; |
| 57 | } |
| 58 | |
| 59 | #ifdef CONFIG_NET_NS |
Pavel Emelyanov | d57a921 | 2007-11-01 00:46:50 -0700 | [diff] [blame] | 60 | static struct kmem_cache *net_cachep; |
Benjamin Thery | 3ef1355 | 2007-11-19 23:18:16 -0800 | [diff] [blame] | 61 | static struct workqueue_struct *netns_wq; |
Pavel Emelyanov | d57a921 | 2007-11-01 00:46:50 -0700 | [diff] [blame] | 62 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 63 | static struct net *net_alloc(void) |
| 64 | { |
Pavel Emelyanov | 32f0c4c | 2007-10-09 13:02:17 -0700 | [diff] [blame] | 65 | return kmem_cache_zalloc(net_cachep, GFP_KERNEL); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 66 | } |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 67 | |
Johann Felix Soden | 45a19b0 | 2007-11-07 01:30:30 -0800 | [diff] [blame] | 68 | static void net_free(struct net *net) |
| 69 | { |
| 70 | if (!net) |
| 71 | return; |
| 72 | |
| 73 | if (unlikely(atomic_read(&net->use_count) != 0)) { |
| 74 | printk(KERN_EMERG "network namespace not free! Usage: %d\n", |
| 75 | atomic_read(&net->use_count)); |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | kmem_cache_free(net_cachep, net); |
| 80 | } |
| 81 | |
Pavel Emelyanov | 6a1a3b9 | 2007-11-01 00:44:50 -0700 | [diff] [blame] | 82 | struct net *copy_net_ns(unsigned long flags, struct net *old_net) |
| 83 | { |
| 84 | struct net *new_net = NULL; |
| 85 | int err; |
| 86 | |
| 87 | get_net(old_net); |
| 88 | |
| 89 | if (!(flags & CLONE_NEWNET)) |
| 90 | return old_net; |
| 91 | |
| 92 | err = -ENOMEM; |
| 93 | new_net = net_alloc(); |
| 94 | if (!new_net) |
| 95 | goto out; |
| 96 | |
| 97 | mutex_lock(&net_mutex); |
| 98 | err = setup_net(new_net); |
| 99 | if (err) |
| 100 | goto out_unlock; |
| 101 | |
| 102 | rtnl_lock(); |
| 103 | list_add_tail(&new_net->list, &net_namespace_list); |
| 104 | rtnl_unlock(); |
| 105 | |
| 106 | |
| 107 | out_unlock: |
| 108 | mutex_unlock(&net_mutex); |
| 109 | out: |
| 110 | put_net(old_net); |
| 111 | if (err) { |
| 112 | net_free(new_net); |
| 113 | new_net = ERR_PTR(err); |
| 114 | } |
| 115 | return new_net; |
| 116 | } |
| 117 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 118 | static void cleanup_net(struct work_struct *work) |
| 119 | { |
| 120 | struct pernet_operations *ops; |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 121 | struct net *net; |
| 122 | |
| 123 | net = container_of(work, struct net, work); |
| 124 | |
| 125 | mutex_lock(&net_mutex); |
| 126 | |
| 127 | /* Don't let anyone else find us. */ |
Eric W. Biederman | f4618d3 | 2007-09-26 22:40:08 -0700 | [diff] [blame] | 128 | rtnl_lock(); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 129 | list_del(&net->list); |
Eric W. Biederman | f4618d3 | 2007-09-26 22:40:08 -0700 | [diff] [blame] | 130 | rtnl_unlock(); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 131 | |
| 132 | /* Run all of the network namespace exit methods */ |
Pavel Emelyanov | 768f3591 | 2007-09-18 13:20:41 -0700 | [diff] [blame] | 133 | list_for_each_entry_reverse(ops, &pernet_list, list) { |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 134 | if (ops->exit) |
| 135 | ops->exit(net); |
| 136 | } |
| 137 | |
| 138 | mutex_unlock(&net_mutex); |
| 139 | |
| 140 | /* Ensure there are no outstanding rcu callbacks using this |
| 141 | * network namespace. |
| 142 | */ |
| 143 | rcu_barrier(); |
| 144 | |
| 145 | /* Finally it is safe to free my network namespace structure */ |
| 146 | net_free(net); |
| 147 | } |
| 148 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 149 | void __put_net(struct net *net) |
| 150 | { |
| 151 | /* Cleanup the network namespace in process context */ |
| 152 | INIT_WORK(&net->work, cleanup_net); |
Benjamin Thery | 3ef1355 | 2007-11-19 23:18:16 -0800 | [diff] [blame] | 153 | queue_work(netns_wq, &net->work); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 154 | } |
| 155 | EXPORT_SYMBOL_GPL(__put_net); |
| 156 | |
Pavel Emelyanov | 6a1a3b9 | 2007-11-01 00:44:50 -0700 | [diff] [blame] | 157 | #else |
Eric W. Biederman | 9dd776b | 2007-09-26 22:04:26 -0700 | [diff] [blame] | 158 | struct net *copy_net_ns(unsigned long flags, struct net *old_net) |
| 159 | { |
Pavel Emelyanov | 6a1a3b9 | 2007-11-01 00:44:50 -0700 | [diff] [blame] | 160 | if (flags & CLONE_NEWNET) |
| 161 | return ERR_PTR(-EINVAL); |
| 162 | return old_net; |
Eric W. Biederman | 9dd776b | 2007-09-26 22:04:26 -0700 | [diff] [blame] | 163 | } |
Pavel Emelyanov | 6a1a3b9 | 2007-11-01 00:44:50 -0700 | [diff] [blame] | 164 | #endif |
Eric W. Biederman | 9dd776b | 2007-09-26 22:04:26 -0700 | [diff] [blame] | 165 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 166 | static int __init net_ns_init(void) |
| 167 | { |
| 168 | int err; |
| 169 | |
| 170 | printk(KERN_INFO "net_namespace: %zd bytes\n", sizeof(struct net)); |
Pavel Emelyanov | d57a921 | 2007-11-01 00:46:50 -0700 | [diff] [blame] | 171 | #ifdef CONFIG_NET_NS |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 172 | net_cachep = kmem_cache_create("net_namespace", sizeof(struct net), |
| 173 | SMP_CACHE_BYTES, |
| 174 | SLAB_PANIC, NULL); |
Benjamin Thery | 3ef1355 | 2007-11-19 23:18:16 -0800 | [diff] [blame] | 175 | |
| 176 | /* Create workqueue for cleanup */ |
| 177 | netns_wq = create_singlethread_workqueue("netns"); |
| 178 | if (!netns_wq) |
| 179 | panic("Could not create netns workq"); |
Pavel Emelyanov | d57a921 | 2007-11-01 00:46:50 -0700 | [diff] [blame] | 180 | #endif |
Benjamin Thery | 3ef1355 | 2007-11-19 23:18:16 -0800 | [diff] [blame] | 181 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 182 | mutex_lock(&net_mutex); |
| 183 | err = setup_net(&init_net); |
| 184 | |
Eric W. Biederman | f4618d3 | 2007-09-26 22:40:08 -0700 | [diff] [blame] | 185 | rtnl_lock(); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 186 | list_add_tail(&init_net.list, &net_namespace_list); |
Eric W. Biederman | f4618d3 | 2007-09-26 22:40:08 -0700 | [diff] [blame] | 187 | rtnl_unlock(); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 188 | |
| 189 | mutex_unlock(&net_mutex); |
| 190 | if (err) |
| 191 | panic("Could not setup the initial network namespace"); |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | pure_initcall(net_ns_init); |
| 197 | |
Denis V. Lunev | ed160e8 | 2007-11-13 03:23:21 -0800 | [diff] [blame] | 198 | #ifdef CONFIG_NET_NS |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 199 | static int register_pernet_operations(struct list_head *list, |
| 200 | struct pernet_operations *ops) |
| 201 | { |
| 202 | struct net *net, *undo_net; |
| 203 | int error; |
| 204 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 205 | list_add_tail(&ops->list, list); |
Pavel Emelyanov | 1dba323 | 2007-11-01 00:42:43 -0700 | [diff] [blame] | 206 | if (ops->init) { |
| 207 | for_each_net(net) { |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 208 | error = ops->init(net); |
| 209 | if (error) |
| 210 | goto out_undo; |
| 211 | } |
| 212 | } |
Pavel Emelyanov | 1dba323 | 2007-11-01 00:42:43 -0700 | [diff] [blame] | 213 | return 0; |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 214 | |
| 215 | out_undo: |
| 216 | /* If I have an error cleanup all namespaces I initialized */ |
| 217 | list_del(&ops->list); |
Pavel Emelyanov | 1dba323 | 2007-11-01 00:42:43 -0700 | [diff] [blame] | 218 | if (ops->exit) { |
| 219 | for_each_net(undo_net) { |
| 220 | if (undo_net == net) |
| 221 | goto undone; |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 222 | ops->exit(undo_net); |
Pavel Emelyanov | 1dba323 | 2007-11-01 00:42:43 -0700 | [diff] [blame] | 223 | } |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 224 | } |
| 225 | undone: |
Pavel Emelyanov | 1dba323 | 2007-11-01 00:42:43 -0700 | [diff] [blame] | 226 | return error; |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | static void unregister_pernet_operations(struct pernet_operations *ops) |
| 230 | { |
| 231 | struct net *net; |
| 232 | |
| 233 | list_del(&ops->list); |
Pavel Emelyanov | 1dba323 | 2007-11-01 00:42:43 -0700 | [diff] [blame] | 234 | if (ops->exit) |
| 235 | for_each_net(net) |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 236 | ops->exit(net); |
| 237 | } |
| 238 | |
Denis V. Lunev | ed160e8 | 2007-11-13 03:23:21 -0800 | [diff] [blame] | 239 | #else |
| 240 | |
| 241 | static int register_pernet_operations(struct list_head *list, |
| 242 | struct pernet_operations *ops) |
| 243 | { |
| 244 | if (ops->init == NULL) |
| 245 | return 0; |
| 246 | return ops->init(&init_net); |
| 247 | } |
| 248 | |
| 249 | static void unregister_pernet_operations(struct pernet_operations *ops) |
| 250 | { |
| 251 | if (ops->exit) |
| 252 | ops->exit(&init_net); |
| 253 | } |
| 254 | #endif |
| 255 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 256 | /** |
| 257 | * register_pernet_subsys - register a network namespace subsystem |
| 258 | * @ops: pernet operations structure for the subsystem |
| 259 | * |
| 260 | * Register a subsystem which has init and exit functions |
| 261 | * that are called when network namespaces are created and |
| 262 | * destroyed respectively. |
| 263 | * |
| 264 | * When registered all network namespace init functions are |
| 265 | * called for every existing network namespace. Allowing kernel |
| 266 | * modules to have a race free view of the set of network namespaces. |
| 267 | * |
| 268 | * When a new network namespace is created all of the init |
| 269 | * methods are called in the order in which they were registered. |
| 270 | * |
| 271 | * When a network namespace is destroyed all of the exit methods |
| 272 | * are called in the reverse of the order with which they were |
| 273 | * registered. |
| 274 | */ |
| 275 | int register_pernet_subsys(struct pernet_operations *ops) |
| 276 | { |
| 277 | int error; |
| 278 | mutex_lock(&net_mutex); |
| 279 | error = register_pernet_operations(first_device, ops); |
| 280 | mutex_unlock(&net_mutex); |
| 281 | return error; |
| 282 | } |
| 283 | EXPORT_SYMBOL_GPL(register_pernet_subsys); |
| 284 | |
| 285 | /** |
| 286 | * unregister_pernet_subsys - unregister a network namespace subsystem |
| 287 | * @ops: pernet operations structure to manipulate |
| 288 | * |
| 289 | * Remove the pernet operations structure from the list to be |
Oliver Pinter | 53379e5 | 2008-02-03 17:56:48 +0200 | [diff] [blame] | 290 | * used when network namespaces are created or destroyed. In |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 291 | * addition run the exit method for all existing network |
| 292 | * namespaces. |
| 293 | */ |
| 294 | void unregister_pernet_subsys(struct pernet_operations *module) |
| 295 | { |
| 296 | mutex_lock(&net_mutex); |
| 297 | unregister_pernet_operations(module); |
| 298 | mutex_unlock(&net_mutex); |
| 299 | } |
| 300 | EXPORT_SYMBOL_GPL(unregister_pernet_subsys); |
| 301 | |
| 302 | /** |
| 303 | * register_pernet_device - register a network namespace device |
| 304 | * @ops: pernet operations structure for the subsystem |
| 305 | * |
| 306 | * Register a device which has init and exit functions |
| 307 | * that are called when network namespaces are created and |
| 308 | * destroyed respectively. |
| 309 | * |
| 310 | * When registered all network namespace init functions are |
| 311 | * called for every existing network namespace. Allowing kernel |
| 312 | * modules to have a race free view of the set of network namespaces. |
| 313 | * |
| 314 | * When a new network namespace is created all of the init |
| 315 | * methods are called in the order in which they were registered. |
| 316 | * |
| 317 | * When a network namespace is destroyed all of the exit methods |
| 318 | * are called in the reverse of the order with which they were |
| 319 | * registered. |
| 320 | */ |
| 321 | int register_pernet_device(struct pernet_operations *ops) |
| 322 | { |
| 323 | int error; |
| 324 | mutex_lock(&net_mutex); |
| 325 | error = register_pernet_operations(&pernet_list, ops); |
| 326 | if (!error && (first_device == &pernet_list)) |
| 327 | first_device = &ops->list; |
| 328 | mutex_unlock(&net_mutex); |
| 329 | return error; |
| 330 | } |
| 331 | EXPORT_SYMBOL_GPL(register_pernet_device); |
| 332 | |
| 333 | /** |
| 334 | * unregister_pernet_device - unregister a network namespace netdevice |
| 335 | * @ops: pernet operations structure to manipulate |
| 336 | * |
| 337 | * Remove the pernet operations structure from the list to be |
Oliver Pinter | 53379e5 | 2008-02-03 17:56:48 +0200 | [diff] [blame] | 338 | * used when network namespaces are created or destroyed. In |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 339 | * addition run the exit method for all existing network |
| 340 | * namespaces. |
| 341 | */ |
| 342 | void unregister_pernet_device(struct pernet_operations *ops) |
| 343 | { |
| 344 | mutex_lock(&net_mutex); |
| 345 | if (&ops->list == first_device) |
| 346 | first_device = first_device->next; |
| 347 | unregister_pernet_operations(ops); |
| 348 | mutex_unlock(&net_mutex); |
| 349 | } |
| 350 | EXPORT_SYMBOL_GPL(unregister_pernet_device); |