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