blob: 2197d51aef3bfed67c29d14bcd1e16120fa28510 [file] [log] [blame]
Eric W. Biederman5f256be2007-09-12 11:50:50 +02001#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. Biederman9dd776b2007-09-26 22:04:26 -07007#include <linux/sched.h>
Pavel Emelyanovc93cf612008-04-15 00:35:23 -07008#include <linux/idr.h>
Eric W. Biederman5f256be2007-09-12 11:50:50 +02009#include <net/net_namespace.h>
10
11/*
12 * Our network namespace constructor/destructor lists
13 */
14
15static LIST_HEAD(pernet_list);
16static struct list_head *first_device = &pernet_list;
17static DEFINE_MUTEX(net_mutex);
18
Eric W. Biederman5f256be2007-09-12 11:50:50 +020019LIST_HEAD(net_namespace_list);
20
Eric W. Biederman5f256be2007-09-12 11:50:50 +020021struct net init_net;
Denis V. Lunevff4b9502008-01-22 22:05:33 -080022EXPORT_SYMBOL(init_net);
Eric W. Biederman5f256be2007-09-12 11:50:50 +020023
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -070024/*
25 * setup_net runs the initializers for the network namespace object.
26 */
Pavel Emelyanov1a2ee932007-11-01 00:45:59 -070027static __net_init int setup_net(struct net *net)
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -070028{
29 /* Must be called with net_mutex held */
30 struct pernet_operations *ops;
31 int error;
32
33 atomic_set(&net->count, 1);
34 atomic_set(&net->use_count, 0);
35
36 error = 0;
37 list_for_each_entry(ops, &pernet_list, list) {
38 if (ops->init) {
39 error = ops->init(net);
40 if (error < 0)
41 goto out_undo;
42 }
43 }
44out:
45 return error;
46
47out_undo:
48 /* Walk through the list backwards calling the exit functions
49 * for the pernet modules whose init functions did not fail.
50 */
51 list_for_each_entry_continue_reverse(ops, &pernet_list, list) {
52 if (ops->exit)
53 ops->exit(net);
54 }
55
56 rcu_barrier();
57 goto out;
58}
59
60#ifdef CONFIG_NET_NS
Pavel Emelyanovd57a9212007-11-01 00:46:50 -070061static struct kmem_cache *net_cachep;
Benjamin Thery3ef13552007-11-19 23:18:16 -080062static struct workqueue_struct *netns_wq;
Pavel Emelyanovd57a9212007-11-01 00:46:50 -070063
Eric W. Biederman5f256be2007-09-12 11:50:50 +020064static struct net *net_alloc(void)
65{
Pavel Emelyanov32f0c4c2007-10-09 13:02:17 -070066 return kmem_cache_zalloc(net_cachep, GFP_KERNEL);
Eric W. Biederman5f256be2007-09-12 11:50:50 +020067}
Eric W. Biederman5f256be2007-09-12 11:50:50 +020068
Johann Felix Soden45a19b02007-11-07 01:30:30 -080069static void net_free(struct net *net)
70{
71 if (!net)
72 return;
73
74 if (unlikely(atomic_read(&net->use_count) != 0)) {
75 printk(KERN_EMERG "network namespace not free! Usage: %d\n",
76 atomic_read(&net->use_count));
77 return;
78 }
79
80 kmem_cache_free(net_cachep, net);
81}
82
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -070083struct net *copy_net_ns(unsigned long flags, struct net *old_net)
84{
85 struct net *new_net = NULL;
86 int err;
87
88 get_net(old_net);
89
90 if (!(flags & CLONE_NEWNET))
91 return old_net;
92
93 err = -ENOMEM;
94 new_net = net_alloc();
95 if (!new_net)
96 goto out;
97
98 mutex_lock(&net_mutex);
99 err = setup_net(new_net);
100 if (err)
101 goto out_unlock;
102
103 rtnl_lock();
104 list_add_tail(&new_net->list, &net_namespace_list);
105 rtnl_unlock();
106
107
108out_unlock:
109 mutex_unlock(&net_mutex);
110out:
111 put_net(old_net);
112 if (err) {
113 net_free(new_net);
114 new_net = ERR_PTR(err);
115 }
116 return new_net;
117}
118
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200119static void cleanup_net(struct work_struct *work)
120{
121 struct pernet_operations *ops;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200122 struct net *net;
123
124 net = container_of(work, struct net, work);
125
126 mutex_lock(&net_mutex);
127
128 /* Don't let anyone else find us. */
Eric W. Biedermanf4618d32007-09-26 22:40:08 -0700129 rtnl_lock();
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200130 list_del(&net->list);
Eric W. Biedermanf4618d32007-09-26 22:40:08 -0700131 rtnl_unlock();
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200132
133 /* Run all of the network namespace exit methods */
Pavel Emelyanov768f35912007-09-18 13:20:41 -0700134 list_for_each_entry_reverse(ops, &pernet_list, list) {
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200135 if (ops->exit)
136 ops->exit(net);
137 }
138
139 mutex_unlock(&net_mutex);
140
141 /* Ensure there are no outstanding rcu callbacks using this
142 * network namespace.
143 */
144 rcu_barrier();
145
146 /* Finally it is safe to free my network namespace structure */
147 net_free(net);
148}
149
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200150void __put_net(struct net *net)
151{
152 /* Cleanup the network namespace in process context */
153 INIT_WORK(&net->work, cleanup_net);
Benjamin Thery3ef13552007-11-19 23:18:16 -0800154 queue_work(netns_wq, &net->work);
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200155}
156EXPORT_SYMBOL_GPL(__put_net);
157
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700158#else
Eric W. Biederman9dd776b2007-09-26 22:04:26 -0700159struct net *copy_net_ns(unsigned long flags, struct net *old_net)
160{
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700161 if (flags & CLONE_NEWNET)
162 return ERR_PTR(-EINVAL);
163 return old_net;
Eric W. Biederman9dd776b2007-09-26 22:04:26 -0700164}
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700165#endif
Eric W. Biederman9dd776b2007-09-26 22:04:26 -0700166
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200167static int __init net_ns_init(void)
168{
169 int err;
170
171 printk(KERN_INFO "net_namespace: %zd bytes\n", sizeof(struct net));
Pavel Emelyanovd57a9212007-11-01 00:46:50 -0700172#ifdef CONFIG_NET_NS
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200173 net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
174 SMP_CACHE_BYTES,
175 SLAB_PANIC, NULL);
Benjamin Thery3ef13552007-11-19 23:18:16 -0800176
177 /* Create workqueue for cleanup */
178 netns_wq = create_singlethread_workqueue("netns");
179 if (!netns_wq)
180 panic("Could not create netns workq");
Pavel Emelyanovd57a9212007-11-01 00:46:50 -0700181#endif
Benjamin Thery3ef13552007-11-19 23:18:16 -0800182
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200183 mutex_lock(&net_mutex);
184 err = setup_net(&init_net);
185
Eric W. Biedermanf4618d32007-09-26 22:40:08 -0700186 rtnl_lock();
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200187 list_add_tail(&init_net.list, &net_namespace_list);
Eric W. Biedermanf4618d32007-09-26 22:40:08 -0700188 rtnl_unlock();
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200189
190 mutex_unlock(&net_mutex);
191 if (err)
192 panic("Could not setup the initial network namespace");
193
194 return 0;
195}
196
197pure_initcall(net_ns_init);
198
Denis V. Luneved160e82007-11-13 03:23:21 -0800199#ifdef CONFIG_NET_NS
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200200static int register_pernet_operations(struct list_head *list,
201 struct pernet_operations *ops)
202{
203 struct net *net, *undo_net;
204 int error;
205
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200206 list_add_tail(&ops->list, list);
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700207 if (ops->init) {
208 for_each_net(net) {
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200209 error = ops->init(net);
210 if (error)
211 goto out_undo;
212 }
213 }
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700214 return 0;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200215
216out_undo:
217 /* If I have an error cleanup all namespaces I initialized */
218 list_del(&ops->list);
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700219 if (ops->exit) {
220 for_each_net(undo_net) {
221 if (undo_net == net)
222 goto undone;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200223 ops->exit(undo_net);
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700224 }
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200225 }
226undone:
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700227 return error;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200228}
229
230static void unregister_pernet_operations(struct pernet_operations *ops)
231{
232 struct net *net;
233
234 list_del(&ops->list);
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700235 if (ops->exit)
236 for_each_net(net)
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200237 ops->exit(net);
238}
239
Denis V. Luneved160e82007-11-13 03:23:21 -0800240#else
241
242static int register_pernet_operations(struct list_head *list,
243 struct pernet_operations *ops)
244{
245 if (ops->init == NULL)
246 return 0;
247 return ops->init(&init_net);
248}
249
250static void unregister_pernet_operations(struct pernet_operations *ops)
251{
252 if (ops->exit)
253 ops->exit(&init_net);
254}
255#endif
256
Pavel Emelyanovc93cf612008-04-15 00:35:23 -0700257static DEFINE_IDA(net_generic_ids);
258
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200259/**
260 * register_pernet_subsys - register a network namespace subsystem
261 * @ops: pernet operations structure for the subsystem
262 *
263 * Register a subsystem which has init and exit functions
264 * that are called when network namespaces are created and
265 * destroyed respectively.
266 *
267 * When registered all network namespace init functions are
268 * called for every existing network namespace. Allowing kernel
269 * modules to have a race free view of the set of network namespaces.
270 *
271 * When a new network namespace is created all of the init
272 * methods are called in the order in which they were registered.
273 *
274 * When a network namespace is destroyed all of the exit methods
275 * are called in the reverse of the order with which they were
276 * registered.
277 */
278int register_pernet_subsys(struct pernet_operations *ops)
279{
280 int error;
281 mutex_lock(&net_mutex);
282 error = register_pernet_operations(first_device, ops);
283 mutex_unlock(&net_mutex);
284 return error;
285}
286EXPORT_SYMBOL_GPL(register_pernet_subsys);
287
288/**
289 * unregister_pernet_subsys - unregister a network namespace subsystem
290 * @ops: pernet operations structure to manipulate
291 *
292 * Remove the pernet operations structure from the list to be
Oliver Pinter53379e52008-02-03 17:56:48 +0200293 * used when network namespaces are created or destroyed. In
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200294 * addition run the exit method for all existing network
295 * namespaces.
296 */
297void unregister_pernet_subsys(struct pernet_operations *module)
298{
299 mutex_lock(&net_mutex);
300 unregister_pernet_operations(module);
301 mutex_unlock(&net_mutex);
302}
303EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
304
305/**
306 * register_pernet_device - register a network namespace device
307 * @ops: pernet operations structure for the subsystem
308 *
309 * Register a device which has init and exit functions
310 * that are called when network namespaces are created and
311 * destroyed respectively.
312 *
313 * When registered all network namespace init functions are
314 * called for every existing network namespace. Allowing kernel
315 * modules to have a race free view of the set of network namespaces.
316 *
317 * When a new network namespace is created all of the init
318 * methods are called in the order in which they were registered.
319 *
320 * When a network namespace is destroyed all of the exit methods
321 * are called in the reverse of the order with which they were
322 * registered.
323 */
324int register_pernet_device(struct pernet_operations *ops)
325{
326 int error;
327 mutex_lock(&net_mutex);
328 error = register_pernet_operations(&pernet_list, ops);
329 if (!error && (first_device == &pernet_list))
330 first_device = &ops->list;
331 mutex_unlock(&net_mutex);
332 return error;
333}
334EXPORT_SYMBOL_GPL(register_pernet_device);
335
Pavel Emelyanovc93cf612008-04-15 00:35:23 -0700336int register_pernet_gen_device(int *id, struct pernet_operations *ops)
337{
338 int error;
339 mutex_lock(&net_mutex);
340again:
341 error = ida_get_new_above(&net_generic_ids, 1, id);
342 if (error) {
343 if (error == -EAGAIN) {
344 ida_pre_get(&net_generic_ids, GFP_KERNEL);
345 goto again;
346 }
347 goto out;
348 }
349 error = register_pernet_operations(&pernet_list, ops);
350 if (error)
351 ida_remove(&net_generic_ids, *id);
352 else if (first_device == &pernet_list)
353 first_device = &ops->list;
354out:
355 mutex_unlock(&net_mutex);
356 return error;
357}
358EXPORT_SYMBOL_GPL(register_pernet_gen_device);
359
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200360/**
361 * unregister_pernet_device - unregister a network namespace netdevice
362 * @ops: pernet operations structure to manipulate
363 *
364 * Remove the pernet operations structure from the list to be
Oliver Pinter53379e52008-02-03 17:56:48 +0200365 * used when network namespaces are created or destroyed. In
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200366 * addition run the exit method for all existing network
367 * namespaces.
368 */
369void unregister_pernet_device(struct pernet_operations *ops)
370{
371 mutex_lock(&net_mutex);
372 if (&ops->list == first_device)
373 first_device = first_device->next;
374 unregister_pernet_operations(ops);
375 mutex_unlock(&net_mutex);
376}
377EXPORT_SYMBOL_GPL(unregister_pernet_device);
Pavel Emelyanovc93cf612008-04-15 00:35:23 -0700378
379void unregister_pernet_gen_device(int id, struct pernet_operations *ops)
380{
381 mutex_lock(&net_mutex);
382 if (&ops->list == first_device)
383 first_device = first_device->next;
384 unregister_pernet_operations(ops);
385 ida_remove(&net_generic_ids, id);
386 mutex_unlock(&net_mutex);
387}
388EXPORT_SYMBOL_GPL(unregister_pernet_gen_device);