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