blob: a044e2d9a8f0ef7e988d394066f7aea1574d58cc [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>
Eric W. Biederman5f256be2007-09-12 11:50:50 +02008#include <net/net_namespace.h>
9
10/*
11 * Our network namespace constructor/destructor lists
12 */
13
14static LIST_HEAD(pernet_list);
15static struct list_head *first_device = &pernet_list;
16static DEFINE_MUTEX(net_mutex);
17
Eric W. Biederman5f256be2007-09-12 11:50:50 +020018LIST_HEAD(net_namespace_list);
19
20static struct kmem_cache *net_cachep;
21
22struct net init_net;
23EXPORT_SYMBOL_GPL(init_net);
24
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -070025/*
26 * setup_net runs the initializers for the network namespace object.
27 */
Pavel Emelyanov1a2ee932007-11-01 00:45:59 -070028static __net_init int setup_net(struct net *net)
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -070029{
30 /* Must be called with net_mutex held */
31 struct pernet_operations *ops;
32 int error;
33
34 atomic_set(&net->count, 1);
35 atomic_set(&net->use_count, 0);
36
37 error = 0;
38 list_for_each_entry(ops, &pernet_list, list) {
39 if (ops->init) {
40 error = ops->init(net);
41 if (error < 0)
42 goto out_undo;
43 }
44 }
45out:
46 return error;
47
48out_undo:
49 /* Walk through the list backwards calling the exit functions
50 * for the pernet modules whose init functions did not fail.
51 */
52 list_for_each_entry_continue_reverse(ops, &pernet_list, list) {
53 if (ops->exit)
54 ops->exit(net);
55 }
56
57 rcu_barrier();
58 goto out;
59}
60
61#ifdef CONFIG_NET_NS
Eric W. Biederman5f256be2007-09-12 11:50:50 +020062static struct net *net_alloc(void)
63{
Pavel Emelyanov32f0c4c2007-10-09 13:02:17 -070064 return kmem_cache_zalloc(net_cachep, GFP_KERNEL);
Eric W. Biederman5f256be2007-09-12 11:50:50 +020065}
Eric W. Biederman5f256be2007-09-12 11:50:50 +020066
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -070067struct net *copy_net_ns(unsigned long flags, struct net *old_net)
68{
69 struct net *new_net = NULL;
70 int err;
71
72 get_net(old_net);
73
74 if (!(flags & CLONE_NEWNET))
75 return old_net;
76
77 err = -ENOMEM;
78 new_net = net_alloc();
79 if (!new_net)
80 goto out;
81
82 mutex_lock(&net_mutex);
83 err = setup_net(new_net);
84 if (err)
85 goto out_unlock;
86
87 rtnl_lock();
88 list_add_tail(&new_net->list, &net_namespace_list);
89 rtnl_unlock();
90
91
92out_unlock:
93 mutex_unlock(&net_mutex);
94out:
95 put_net(old_net);
96 if (err) {
97 net_free(new_net);
98 new_net = ERR_PTR(err);
99 }
100 return new_net;
101}
102
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200103static void net_free(struct net *net)
104{
105 if (!net)
106 return;
107
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 }
113
114 kmem_cache_free(net_cachep, net);
115}
116
117static void cleanup_net(struct work_struct *work)
118{
119 struct pernet_operations *ops;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200120 struct net *net;
121
122 net = container_of(work, struct net, work);
123
124 mutex_lock(&net_mutex);
125
126 /* Don't let anyone else find us. */
Eric W. Biedermanf4618d32007-09-26 22:40:08 -0700127 rtnl_lock();
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200128 list_del(&net->list);
Eric W. Biedermanf4618d32007-09-26 22:40:08 -0700129 rtnl_unlock();
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200130
131 /* Run all of the network namespace exit methods */
Pavel Emelyanov768f35912007-09-18 13:20:41 -0700132 list_for_each_entry_reverse(ops, &pernet_list, list) {
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200133 if (ops->exit)
134 ops->exit(net);
135 }
136
137 mutex_unlock(&net_mutex);
138
139 /* Ensure there are no outstanding rcu callbacks using this
140 * network namespace.
141 */
142 rcu_barrier();
143
144 /* Finally it is safe to free my network namespace structure */
145 net_free(net);
146}
147
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200148void __put_net(struct net *net)
149{
150 /* Cleanup the network namespace in process context */
151 INIT_WORK(&net->work, cleanup_net);
152 schedule_work(&net->work);
153}
154EXPORT_SYMBOL_GPL(__put_net);
155
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700156#else
Eric W. Biederman9dd776b2007-09-26 22:04:26 -0700157struct net *copy_net_ns(unsigned long flags, struct net *old_net)
158{
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700159 if (flags & CLONE_NEWNET)
160 return ERR_PTR(-EINVAL);
161 return old_net;
Eric W. Biederman9dd776b2007-09-26 22:04:26 -0700162}
Pavel Emelyanov6a1a3b92007-11-01 00:44:50 -0700163#endif
Eric W. Biederman9dd776b2007-09-26 22:04:26 -0700164
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200165static int __init net_ns_init(void)
166{
167 int err;
168
169 printk(KERN_INFO "net_namespace: %zd bytes\n", sizeof(struct net));
170 net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
171 SMP_CACHE_BYTES,
172 SLAB_PANIC, NULL);
173 mutex_lock(&net_mutex);
174 err = setup_net(&init_net);
175
Eric W. Biedermanf4618d32007-09-26 22:40:08 -0700176 rtnl_lock();
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200177 list_add_tail(&init_net.list, &net_namespace_list);
Eric W. Biedermanf4618d32007-09-26 22:40:08 -0700178 rtnl_unlock();
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200179
180 mutex_unlock(&net_mutex);
181 if (err)
182 panic("Could not setup the initial network namespace");
183
184 return 0;
185}
186
187pure_initcall(net_ns_init);
188
189static int register_pernet_operations(struct list_head *list,
190 struct pernet_operations *ops)
191{
192 struct net *net, *undo_net;
193 int error;
194
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200195 list_add_tail(&ops->list, list);
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700196 if (ops->init) {
197 for_each_net(net) {
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200198 error = ops->init(net);
199 if (error)
200 goto out_undo;
201 }
202 }
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700203 return 0;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200204
205out_undo:
206 /* If I have an error cleanup all namespaces I initialized */
207 list_del(&ops->list);
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700208 if (ops->exit) {
209 for_each_net(undo_net) {
210 if (undo_net == net)
211 goto undone;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200212 ops->exit(undo_net);
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700213 }
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200214 }
215undone:
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700216 return error;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200217}
218
219static void unregister_pernet_operations(struct pernet_operations *ops)
220{
221 struct net *net;
222
223 list_del(&ops->list);
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700224 if (ops->exit)
225 for_each_net(net)
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200226 ops->exit(net);
227}
228
229/**
230 * register_pernet_subsys - register a network namespace subsystem
231 * @ops: pernet operations structure for the subsystem
232 *
233 * Register a subsystem which has init and exit functions
234 * that are called when network namespaces are created and
235 * destroyed respectively.
236 *
237 * When registered all network namespace init functions are
238 * called for every existing network namespace. Allowing kernel
239 * modules to have a race free view of the set of network namespaces.
240 *
241 * When a new network namespace is created all of the init
242 * methods are called in the order in which they were registered.
243 *
244 * When a network namespace is destroyed all of the exit methods
245 * are called in the reverse of the order with which they were
246 * registered.
247 */
248int register_pernet_subsys(struct pernet_operations *ops)
249{
250 int error;
251 mutex_lock(&net_mutex);
252 error = register_pernet_operations(first_device, ops);
253 mutex_unlock(&net_mutex);
254 return error;
255}
256EXPORT_SYMBOL_GPL(register_pernet_subsys);
257
258/**
259 * unregister_pernet_subsys - unregister a network namespace subsystem
260 * @ops: pernet operations structure to manipulate
261 *
262 * Remove the pernet operations structure from the list to be
263 * used when network namespaces are created or destoryed. In
264 * addition run the exit method for all existing network
265 * namespaces.
266 */
267void unregister_pernet_subsys(struct pernet_operations *module)
268{
269 mutex_lock(&net_mutex);
270 unregister_pernet_operations(module);
271 mutex_unlock(&net_mutex);
272}
273EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
274
275/**
276 * register_pernet_device - register a network namespace device
277 * @ops: pernet operations structure for the subsystem
278 *
279 * Register a device which has init and exit functions
280 * that are called when network namespaces are created and
281 * destroyed respectively.
282 *
283 * When registered all network namespace init functions are
284 * called for every existing network namespace. Allowing kernel
285 * modules to have a race free view of the set of network namespaces.
286 *
287 * When a new network namespace is created all of the init
288 * methods are called in the order in which they were registered.
289 *
290 * When a network namespace is destroyed all of the exit methods
291 * are called in the reverse of the order with which they were
292 * registered.
293 */
294int register_pernet_device(struct pernet_operations *ops)
295{
296 int error;
297 mutex_lock(&net_mutex);
298 error = register_pernet_operations(&pernet_list, ops);
299 if (!error && (first_device == &pernet_list))
300 first_device = &ops->list;
301 mutex_unlock(&net_mutex);
302 return error;
303}
304EXPORT_SYMBOL_GPL(register_pernet_device);
305
306/**
307 * unregister_pernet_device - unregister a network namespace netdevice
308 * @ops: pernet operations structure to manipulate
309 *
310 * Remove the pernet operations structure from the list to be
311 * used when network namespaces are created or destoryed. In
312 * addition run the exit method for all existing network
313 * namespaces.
314 */
315void unregister_pernet_device(struct pernet_operations *ops)
316{
317 mutex_lock(&net_mutex);
318 if (&ops->list == first_device)
319 first_device = first_device->next;
320 unregister_pernet_operations(ops);
321 mutex_unlock(&net_mutex);
322}
323EXPORT_SYMBOL_GPL(unregister_pernet_device);