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