Commit | Line | Data |
---|---|---|
5f256bec EB |
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> | |
9dd776b6 | 7 | #include <linux/sched.h> |
c93cf61f | 8 | #include <linux/idr.h> |
5f256bec | 9 | #include <net/net_namespace.h> |
dec827d1 | 10 | #include <net/netns/generic.h> |
5f256bec EB |
11 | |
12 | /* | |
13 | * Our network namespace constructor/destructor lists | |
14 | */ | |
15 | ||
16 | static LIST_HEAD(pernet_list); | |
17 | static struct list_head *first_device = &pernet_list; | |
18 | static DEFINE_MUTEX(net_mutex); | |
19 | ||
5f256bec | 20 | LIST_HEAD(net_namespace_list); |
b76a461f | 21 | EXPORT_SYMBOL_GPL(net_namespace_list); |
5f256bec | 22 | |
5f256bec | 23 | struct net init_net; |
ff4b9502 | 24 | EXPORT_SYMBOL(init_net); |
5f256bec | 25 | |
dec827d1 PE |
26 | #define INITIAL_NET_GEN_PTRS 13 /* +1 for len +2 for rcu_head */ |
27 | ||
5f256bec EB |
28 | /* |
29 | * setup_net runs the initializers for the network namespace object. | |
30 | */ | |
1a2ee93d | 31 | static __net_init int setup_net(struct net *net) |
5f256bec EB |
32 | { |
33 | /* Must be called with net_mutex held */ | |
34 | struct pernet_operations *ops; | |
486a87f1 | 35 | int error = 0; |
5f256bec | 36 | |
5f256bec | 37 | atomic_set(&net->count, 1); |
486a87f1 | 38 | |
5d1e4468 | 39 | #ifdef NETNS_REFCNT_DEBUG |
5f256bec | 40 | atomic_set(&net->use_count, 0); |
5d1e4468 | 41 | #endif |
5f256bec | 42 | |
768f3591 | 43 | list_for_each_entry(ops, &pernet_list, list) { |
5f256bec EB |
44 | if (ops->init) { |
45 | error = ops->init(net); | |
46 | if (error < 0) | |
47 | goto out_undo; | |
48 | } | |
49 | } | |
50 | out: | |
51 | return error; | |
768f3591 | 52 | |
5f256bec EB |
53 | out_undo: |
54 | /* Walk through the list backwards calling the exit functions | |
55 | * for the pernet modules whose init functions did not fail. | |
56 | */ | |
768f3591 | 57 | list_for_each_entry_continue_reverse(ops, &pernet_list, list) { |
5f256bec EB |
58 | if (ops->exit) |
59 | ops->exit(net); | |
60 | } | |
310928d9 DL |
61 | |
62 | rcu_barrier(); | |
5f256bec EB |
63 | goto out; |
64 | } | |
65 | ||
486a87f1 | 66 | static struct net_generic *net_alloc_generic(void) |
6a1a3b9f | 67 | { |
486a87f1 DL |
68 | struct net_generic *ng; |
69 | size_t generic_size = sizeof(struct net_generic) + | |
70 | INITIAL_NET_GEN_PTRS * sizeof(void *); | |
71 | ||
72 | ng = kzalloc(generic_size, GFP_KERNEL); | |
73 | if (ng) | |
74 | ng->len = INITIAL_NET_GEN_PTRS; | |
75 | ||
76 | return ng; | |
6a1a3b9f PE |
77 | } |
78 | ||
ebe47d47 CN |
79 | #ifdef CONFIG_NET_NS |
80 | static struct kmem_cache *net_cachep; | |
81 | static struct workqueue_struct *netns_wq; | |
82 | ||
486a87f1 | 83 | static struct net *net_alloc(void) |
45a19b0a | 84 | { |
486a87f1 DL |
85 | struct net *net = NULL; |
86 | struct net_generic *ng; | |
87 | ||
88 | ng = net_alloc_generic(); | |
89 | if (!ng) | |
90 | goto out; | |
91 | ||
92 | net = kmem_cache_zalloc(net_cachep, GFP_KERNEL); | |
45a19b0a | 93 | if (!net) |
486a87f1 | 94 | goto out_free; |
45a19b0a | 95 | |
486a87f1 DL |
96 | rcu_assign_pointer(net->gen, ng); |
97 | out: | |
98 | return net; | |
99 | ||
100 | out_free: | |
101 | kfree(ng); | |
102 | goto out; | |
103 | } | |
104 | ||
105 | static void net_free(struct net *net) | |
106 | { | |
5d1e4468 | 107 | #ifdef NETNS_REFCNT_DEBUG |
45a19b0a JFS |
108 | if (unlikely(atomic_read(&net->use_count) != 0)) { |
109 | printk(KERN_EMERG "network namespace not free! Usage: %d\n", | |
110 | atomic_read(&net->use_count)); | |
111 | return; | |
112 | } | |
5d1e4468 | 113 | #endif |
4ef079cc | 114 | kfree(net->gen); |
45a19b0a JFS |
115 | kmem_cache_free(net_cachep, net); |
116 | } | |
117 | ||
9dd776b6 EB |
118 | struct net *copy_net_ns(unsigned long flags, struct net *old_net) |
119 | { | |
120 | struct net *new_net = NULL; | |
121 | int err; | |
122 | ||
9dd776b6 | 123 | if (!(flags & CLONE_NEWNET)) |
4a84822c | 124 | return get_net(old_net); |
9dd776b6 | 125 | |
9dd776b6 EB |
126 | err = -ENOMEM; |
127 | new_net = net_alloc(); | |
128 | if (!new_net) | |
486a87f1 | 129 | goto out_err; |
9dd776b6 EB |
130 | |
131 | mutex_lock(&net_mutex); | |
132 | err = setup_net(new_net); | |
486a87f1 DL |
133 | if (!err) { |
134 | rtnl_lock(); | |
135 | list_add_tail(&new_net->list, &net_namespace_list); | |
136 | rtnl_unlock(); | |
137 | } | |
9dd776b6 | 138 | mutex_unlock(&net_mutex); |
486a87f1 DL |
139 | |
140 | if (err) | |
141 | goto out_free; | |
9dd776b6 | 142 | out: |
9dd776b6 | 143 | return new_net; |
486a87f1 DL |
144 | |
145 | out_free: | |
146 | net_free(new_net); | |
147 | out_err: | |
148 | new_net = ERR_PTR(err); | |
149 | goto out; | |
9dd776b6 EB |
150 | } |
151 | ||
6a1a3b9f PE |
152 | static void cleanup_net(struct work_struct *work) |
153 | { | |
154 | struct pernet_operations *ops; | |
155 | struct net *net; | |
156 | ||
157 | net = container_of(work, struct net, work); | |
158 | ||
159 | mutex_lock(&net_mutex); | |
160 | ||
161 | /* Don't let anyone else find us. */ | |
162 | rtnl_lock(); | |
163 | list_del(&net->list); | |
164 | rtnl_unlock(); | |
165 | ||
166 | /* Run all of the network namespace exit methods */ | |
167 | list_for_each_entry_reverse(ops, &pernet_list, list) { | |
168 | if (ops->exit) | |
169 | ops->exit(net); | |
170 | } | |
171 | ||
172 | mutex_unlock(&net_mutex); | |
173 | ||
174 | /* Ensure there are no outstanding rcu callbacks using this | |
175 | * network namespace. | |
176 | */ | |
177 | rcu_barrier(); | |
178 | ||
179 | /* Finally it is safe to free my network namespace structure */ | |
180 | net_free(net); | |
181 | } | |
182 | ||
183 | void __put_net(struct net *net) | |
184 | { | |
185 | /* Cleanup the network namespace in process context */ | |
186 | INIT_WORK(&net->work, cleanup_net); | |
3ef1355d | 187 | queue_work(netns_wq, &net->work); |
6a1a3b9f PE |
188 | } |
189 | EXPORT_SYMBOL_GPL(__put_net); | |
190 | ||
191 | #else | |
192 | struct net *copy_net_ns(unsigned long flags, struct net *old_net) | |
193 | { | |
194 | if (flags & CLONE_NEWNET) | |
195 | return ERR_PTR(-EINVAL); | |
196 | return old_net; | |
197 | } | |
198 | #endif | |
199 | ||
5f256bec EB |
200 | static int __init net_ns_init(void) |
201 | { | |
486a87f1 | 202 | struct net_generic *ng; |
5f256bec EB |
203 | int err; |
204 | ||
205 | printk(KERN_INFO "net_namespace: %zd bytes\n", sizeof(struct net)); | |
d57a9212 | 206 | #ifdef CONFIG_NET_NS |
5f256bec EB |
207 | net_cachep = kmem_cache_create("net_namespace", sizeof(struct net), |
208 | SMP_CACHE_BYTES, | |
209 | SLAB_PANIC, NULL); | |
3ef1355d BT |
210 | |
211 | /* Create workqueue for cleanup */ | |
212 | netns_wq = create_singlethread_workqueue("netns"); | |
213 | if (!netns_wq) | |
214 | panic("Could not create netns workq"); | |
d57a9212 | 215 | #endif |
3ef1355d | 216 | |
486a87f1 DL |
217 | ng = net_alloc_generic(); |
218 | if (!ng) | |
219 | panic("Could not allocate generic netns"); | |
220 | ||
221 | rcu_assign_pointer(init_net.gen, ng); | |
222 | ||
5f256bec EB |
223 | mutex_lock(&net_mutex); |
224 | err = setup_net(&init_net); | |
225 | ||
f4618d39 | 226 | rtnl_lock(); |
5f256bec | 227 | list_add_tail(&init_net.list, &net_namespace_list); |
f4618d39 | 228 | rtnl_unlock(); |
5f256bec EB |
229 | |
230 | mutex_unlock(&net_mutex); | |
231 | if (err) | |
232 | panic("Could not setup the initial network namespace"); | |
233 | ||
234 | return 0; | |
235 | } | |
236 | ||
237 | pure_initcall(net_ns_init); | |
238 | ||
ed160e83 | 239 | #ifdef CONFIG_NET_NS |
5f256bec EB |
240 | static int register_pernet_operations(struct list_head *list, |
241 | struct pernet_operations *ops) | |
242 | { | |
243 | struct net *net, *undo_net; | |
244 | int error; | |
245 | ||
5f256bec | 246 | list_add_tail(&ops->list, list); |
1dba323b PE |
247 | if (ops->init) { |
248 | for_each_net(net) { | |
5f256bec EB |
249 | error = ops->init(net); |
250 | if (error) | |
251 | goto out_undo; | |
252 | } | |
253 | } | |
1dba323b | 254 | return 0; |
5f256bec EB |
255 | |
256 | out_undo: | |
257 | /* If I have an error cleanup all namespaces I initialized */ | |
258 | list_del(&ops->list); | |
1dba323b PE |
259 | if (ops->exit) { |
260 | for_each_net(undo_net) { | |
261 | if (undo_net == net) | |
262 | goto undone; | |
5f256bec | 263 | ops->exit(undo_net); |
1dba323b | 264 | } |
5f256bec EB |
265 | } |
266 | undone: | |
1dba323b | 267 | return error; |
5f256bec EB |
268 | } |
269 | ||
270 | static void unregister_pernet_operations(struct pernet_operations *ops) | |
271 | { | |
272 | struct net *net; | |
273 | ||
274 | list_del(&ops->list); | |
1dba323b PE |
275 | if (ops->exit) |
276 | for_each_net(net) | |
5f256bec EB |
277 | ops->exit(net); |
278 | } | |
279 | ||
ed160e83 DL |
280 | #else |
281 | ||
282 | static int register_pernet_operations(struct list_head *list, | |
283 | struct pernet_operations *ops) | |
284 | { | |
285 | if (ops->init == NULL) | |
286 | return 0; | |
287 | return ops->init(&init_net); | |
288 | } | |
289 | ||
290 | static void unregister_pernet_operations(struct pernet_operations *ops) | |
291 | { | |
292 | if (ops->exit) | |
293 | ops->exit(&init_net); | |
294 | } | |
295 | #endif | |
296 | ||
c93cf61f PE |
297 | static DEFINE_IDA(net_generic_ids); |
298 | ||
5f256bec EB |
299 | /** |
300 | * register_pernet_subsys - register a network namespace subsystem | |
301 | * @ops: pernet operations structure for the subsystem | |
302 | * | |
303 | * Register a subsystem which has init and exit functions | |
304 | * that are called when network namespaces are created and | |
305 | * destroyed respectively. | |
306 | * | |
307 | * When registered all network namespace init functions are | |
308 | * called for every existing network namespace. Allowing kernel | |
309 | * modules to have a race free view of the set of network namespaces. | |
310 | * | |
311 | * When a new network namespace is created all of the init | |
312 | * methods are called in the order in which they were registered. | |
313 | * | |
314 | * When a network namespace is destroyed all of the exit methods | |
315 | * are called in the reverse of the order with which they were | |
316 | * registered. | |
317 | */ | |
318 | int register_pernet_subsys(struct pernet_operations *ops) | |
319 | { | |
320 | int error; | |
321 | mutex_lock(&net_mutex); | |
322 | error = register_pernet_operations(first_device, ops); | |
323 | mutex_unlock(&net_mutex); | |
324 | return error; | |
325 | } | |
326 | EXPORT_SYMBOL_GPL(register_pernet_subsys); | |
327 | ||
328 | /** | |
329 | * unregister_pernet_subsys - unregister a network namespace subsystem | |
330 | * @ops: pernet operations structure to manipulate | |
331 | * | |
332 | * Remove the pernet operations structure from the list to be | |
53379e57 | 333 | * used when network namespaces are created or destroyed. In |
5f256bec EB |
334 | * addition run the exit method for all existing network |
335 | * namespaces. | |
336 | */ | |
337 | void unregister_pernet_subsys(struct pernet_operations *module) | |
338 | { | |
339 | mutex_lock(&net_mutex); | |
340 | unregister_pernet_operations(module); | |
341 | mutex_unlock(&net_mutex); | |
342 | } | |
343 | EXPORT_SYMBOL_GPL(unregister_pernet_subsys); | |
344 | ||
485ac57b AD |
345 | int register_pernet_gen_subsys(int *id, struct pernet_operations *ops) |
346 | { | |
347 | int rv; | |
348 | ||
349 | mutex_lock(&net_mutex); | |
350 | again: | |
351 | rv = ida_get_new_above(&net_generic_ids, 1, id); | |
352 | if (rv < 0) { | |
353 | if (rv == -EAGAIN) { | |
354 | ida_pre_get(&net_generic_ids, GFP_KERNEL); | |
355 | goto again; | |
356 | } | |
357 | goto out; | |
358 | } | |
359 | rv = register_pernet_operations(first_device, ops); | |
360 | if (rv < 0) | |
361 | ida_remove(&net_generic_ids, *id); | |
485ac57b | 362 | out: |
357f5b0b | 363 | mutex_unlock(&net_mutex); |
485ac57b AD |
364 | return rv; |
365 | } | |
366 | EXPORT_SYMBOL_GPL(register_pernet_gen_subsys); | |
367 | ||
368 | void unregister_pernet_gen_subsys(int id, struct pernet_operations *ops) | |
369 | { | |
370 | mutex_lock(&net_mutex); | |
371 | unregister_pernet_operations(ops); | |
372 | ida_remove(&net_generic_ids, id); | |
373 | mutex_unlock(&net_mutex); | |
374 | } | |
375 | EXPORT_SYMBOL_GPL(unregister_pernet_gen_subsys); | |
376 | ||
5f256bec EB |
377 | /** |
378 | * register_pernet_device - register a network namespace device | |
379 | * @ops: pernet operations structure for the subsystem | |
380 | * | |
381 | * Register a device which has init and exit functions | |
382 | * that are called when network namespaces are created and | |
383 | * destroyed respectively. | |
384 | * | |
385 | * When registered all network namespace init functions are | |
386 | * called for every existing network namespace. Allowing kernel | |
387 | * modules to have a race free view of the set of network namespaces. | |
388 | * | |
389 | * When a new network namespace is created all of the init | |
390 | * methods are called in the order in which they were registered. | |
391 | * | |
392 | * When a network namespace is destroyed all of the exit methods | |
393 | * are called in the reverse of the order with which they were | |
394 | * registered. | |
395 | */ | |
396 | int register_pernet_device(struct pernet_operations *ops) | |
397 | { | |
398 | int error; | |
399 | mutex_lock(&net_mutex); | |
400 | error = register_pernet_operations(&pernet_list, ops); | |
401 | if (!error && (first_device == &pernet_list)) | |
402 | first_device = &ops->list; | |
403 | mutex_unlock(&net_mutex); | |
404 | return error; | |
405 | } | |
406 | EXPORT_SYMBOL_GPL(register_pernet_device); | |
407 | ||
c93cf61f PE |
408 | int register_pernet_gen_device(int *id, struct pernet_operations *ops) |
409 | { | |
410 | int error; | |
411 | mutex_lock(&net_mutex); | |
412 | again: | |
413 | error = ida_get_new_above(&net_generic_ids, 1, id); | |
414 | if (error) { | |
415 | if (error == -EAGAIN) { | |
416 | ida_pre_get(&net_generic_ids, GFP_KERNEL); | |
417 | goto again; | |
418 | } | |
419 | goto out; | |
420 | } | |
421 | error = register_pernet_operations(&pernet_list, ops); | |
422 | if (error) | |
423 | ida_remove(&net_generic_ids, *id); | |
424 | else if (first_device == &pernet_list) | |
425 | first_device = &ops->list; | |
426 | out: | |
427 | mutex_unlock(&net_mutex); | |
428 | return error; | |
429 | } | |
430 | EXPORT_SYMBOL_GPL(register_pernet_gen_device); | |
431 | ||
5f256bec EB |
432 | /** |
433 | * unregister_pernet_device - unregister a network namespace netdevice | |
434 | * @ops: pernet operations structure to manipulate | |
435 | * | |
436 | * Remove the pernet operations structure from the list to be | |
53379e57 | 437 | * used when network namespaces are created or destroyed. In |
5f256bec EB |
438 | * addition run the exit method for all existing network |
439 | * namespaces. | |
440 | */ | |
441 | void unregister_pernet_device(struct pernet_operations *ops) | |
442 | { | |
443 | mutex_lock(&net_mutex); | |
444 | if (&ops->list == first_device) | |
445 | first_device = first_device->next; | |
446 | unregister_pernet_operations(ops); | |
447 | mutex_unlock(&net_mutex); | |
448 | } | |
449 | EXPORT_SYMBOL_GPL(unregister_pernet_device); | |
c93cf61f PE |
450 | |
451 | void unregister_pernet_gen_device(int id, struct pernet_operations *ops) | |
452 | { | |
453 | mutex_lock(&net_mutex); | |
454 | if (&ops->list == first_device) | |
455 | first_device = first_device->next; | |
456 | unregister_pernet_operations(ops); | |
457 | ida_remove(&net_generic_ids, id); | |
458 | mutex_unlock(&net_mutex); | |
459 | } | |
460 | EXPORT_SYMBOL_GPL(unregister_pernet_gen_device); | |
dec827d1 PE |
461 | |
462 | static void net_generic_release(struct rcu_head *rcu) | |
463 | { | |
464 | struct net_generic *ng; | |
465 | ||
466 | ng = container_of(rcu, struct net_generic, rcu); | |
467 | kfree(ng); | |
468 | } | |
469 | ||
470 | int net_assign_generic(struct net *net, int id, void *data) | |
471 | { | |
472 | struct net_generic *ng, *old_ng; | |
473 | ||
474 | BUG_ON(!mutex_is_locked(&net_mutex)); | |
475 | BUG_ON(id == 0); | |
476 | ||
477 | ng = old_ng = net->gen; | |
478 | if (old_ng->len >= id) | |
479 | goto assign; | |
480 | ||
481 | ng = kzalloc(sizeof(struct net_generic) + | |
482 | id * sizeof(void *), GFP_KERNEL); | |
483 | if (ng == NULL) | |
484 | return -ENOMEM; | |
485 | ||
486 | /* | |
487 | * Some synchronisation notes: | |
488 | * | |
489 | * The net_generic explores the net->gen array inside rcu | |
490 | * read section. Besides once set the net->gen->ptr[x] | |
491 | * pointer never changes (see rules in netns/generic.h). | |
492 | * | |
493 | * That said, we simply duplicate this array and schedule | |
494 | * the old copy for kfree after a grace period. | |
495 | */ | |
496 | ||
497 | ng->len = id; | |
dec827d1 PE |
498 | memcpy(&ng->ptr, &old_ng->ptr, old_ng->len); |
499 | ||
500 | rcu_assign_pointer(net->gen, ng); | |
501 | call_rcu(&old_ng->rcu, net_generic_release); | |
502 | assign: | |
503 | ng->ptr[id - 1] = data; | |
504 | return 0; | |
505 | } | |
506 | EXPORT_SYMBOL_GPL(net_assign_generic); |