blob: 4e52921ade098bf97c7889898a90ddac45184554 [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
Eric W. Biederman5f256be2007-09-12 11:50:50 +020025static struct net *net_alloc(void)
26{
Pavel Emelyanov32f0c4c2007-10-09 13:02:17 -070027 return kmem_cache_zalloc(net_cachep, GFP_KERNEL);
Eric W. Biederman5f256be2007-09-12 11:50:50 +020028}
Eric W. Biederman5f256be2007-09-12 11:50:50 +020029
30static void net_free(struct net *net)
31{
32 if (!net)
33 return;
34
35 if (unlikely(atomic_read(&net->use_count) != 0)) {
36 printk(KERN_EMERG "network namespace not free! Usage: %d\n",
37 atomic_read(&net->use_count));
38 return;
39 }
40
41 kmem_cache_free(net_cachep, net);
42}
43
44static void cleanup_net(struct work_struct *work)
45{
46 struct pernet_operations *ops;
Eric W. Biederman5f256be2007-09-12 11:50:50 +020047 struct net *net;
48
49 net = container_of(work, struct net, work);
50
51 mutex_lock(&net_mutex);
52
53 /* Don't let anyone else find us. */
Eric W. Biedermanf4618d32007-09-26 22:40:08 -070054 rtnl_lock();
Eric W. Biederman5f256be2007-09-12 11:50:50 +020055 list_del(&net->list);
Eric W. Biedermanf4618d32007-09-26 22:40:08 -070056 rtnl_unlock();
Eric W. Biederman5f256be2007-09-12 11:50:50 +020057
58 /* Run all of the network namespace exit methods */
Pavel Emelyanov768f35912007-09-18 13:20:41 -070059 list_for_each_entry_reverse(ops, &pernet_list, list) {
Eric W. Biederman5f256be2007-09-12 11:50:50 +020060 if (ops->exit)
61 ops->exit(net);
62 }
63
64 mutex_unlock(&net_mutex);
65
66 /* Ensure there are no outstanding rcu callbacks using this
67 * network namespace.
68 */
69 rcu_barrier();
70
71 /* Finally it is safe to free my network namespace structure */
72 net_free(net);
73}
74
75
76void __put_net(struct net *net)
77{
78 /* Cleanup the network namespace in process context */
79 INIT_WORK(&net->work, cleanup_net);
80 schedule_work(&net->work);
81}
82EXPORT_SYMBOL_GPL(__put_net);
83
84/*
85 * setup_net runs the initializers for the network namespace object.
86 */
87static int setup_net(struct net *net)
88{
89 /* Must be called with net_mutex held */
90 struct pernet_operations *ops;
Eric W. Biederman5f256be2007-09-12 11:50:50 +020091 int error;
92
Eric W. Biederman5f256be2007-09-12 11:50:50 +020093 atomic_set(&net->count, 1);
94 atomic_set(&net->use_count, 0);
95
96 error = 0;
Pavel Emelyanov768f35912007-09-18 13:20:41 -070097 list_for_each_entry(ops, &pernet_list, list) {
Eric W. Biederman5f256be2007-09-12 11:50:50 +020098 if (ops->init) {
99 error = ops->init(net);
100 if (error < 0)
101 goto out_undo;
102 }
103 }
104out:
105 return error;
Pavel Emelyanov768f35912007-09-18 13:20:41 -0700106
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200107out_undo:
108 /* Walk through the list backwards calling the exit functions
109 * for the pernet modules whose init functions did not fail.
110 */
Pavel Emelyanov768f35912007-09-18 13:20:41 -0700111 list_for_each_entry_continue_reverse(ops, &pernet_list, list) {
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200112 if (ops->exit)
113 ops->exit(net);
114 }
Daniel Lezcano310928d2007-10-30 15:38:57 -0700115
116 rcu_barrier();
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200117 goto out;
118}
119
Eric W. Biederman9dd776b2007-09-26 22:04:26 -0700120struct net *copy_net_ns(unsigned long flags, struct net *old_net)
121{
122 struct net *new_net = NULL;
123 int err;
124
125 get_net(old_net);
126
127 if (!(flags & CLONE_NEWNET))
128 return old_net;
129
130#ifndef CONFIG_NET_NS
131 return ERR_PTR(-EINVAL);
132#endif
133
134 err = -ENOMEM;
135 new_net = net_alloc();
136 if (!new_net)
137 goto out;
138
139 mutex_lock(&net_mutex);
140 err = setup_net(new_net);
141 if (err)
142 goto out_unlock;
143
Eric W. Biedermanf4618d32007-09-26 22:40:08 -0700144 rtnl_lock();
Eric W. Biederman9dd776b2007-09-26 22:04:26 -0700145 list_add_tail(&new_net->list, &net_namespace_list);
Eric W. Biedermanf4618d32007-09-26 22:40:08 -0700146 rtnl_unlock();
Eric W. Biederman9dd776b2007-09-26 22:04:26 -0700147
148
149out_unlock:
150 mutex_unlock(&net_mutex);
151out:
152 put_net(old_net);
153 if (err) {
154 net_free(new_net);
155 new_net = ERR_PTR(err);
156 }
157 return new_net;
158}
159
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200160static int __init net_ns_init(void)
161{
162 int err;
163
164 printk(KERN_INFO "net_namespace: %zd bytes\n", sizeof(struct net));
165 net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
166 SMP_CACHE_BYTES,
167 SLAB_PANIC, NULL);
168 mutex_lock(&net_mutex);
169 err = setup_net(&init_net);
170
Eric W. Biedermanf4618d32007-09-26 22:40:08 -0700171 rtnl_lock();
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200172 list_add_tail(&init_net.list, &net_namespace_list);
Eric W. Biedermanf4618d32007-09-26 22:40:08 -0700173 rtnl_unlock();
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200174
175 mutex_unlock(&net_mutex);
176 if (err)
177 panic("Could not setup the initial network namespace");
178
179 return 0;
180}
181
182pure_initcall(net_ns_init);
183
184static int register_pernet_operations(struct list_head *list,
185 struct pernet_operations *ops)
186{
187 struct net *net, *undo_net;
188 int error;
189
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200190 list_add_tail(&ops->list, list);
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700191 if (ops->init) {
192 for_each_net(net) {
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200193 error = ops->init(net);
194 if (error)
195 goto out_undo;
196 }
197 }
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700198 return 0;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200199
200out_undo:
201 /* If I have an error cleanup all namespaces I initialized */
202 list_del(&ops->list);
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700203 if (ops->exit) {
204 for_each_net(undo_net) {
205 if (undo_net == net)
206 goto undone;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200207 ops->exit(undo_net);
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700208 }
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200209 }
210undone:
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700211 return error;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200212}
213
214static void unregister_pernet_operations(struct pernet_operations *ops)
215{
216 struct net *net;
217
218 list_del(&ops->list);
Pavel Emelyanov1dba3232007-11-01 00:42:43 -0700219 if (ops->exit)
220 for_each_net(net)
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200221 ops->exit(net);
222}
223
224/**
225 * register_pernet_subsys - register a network namespace subsystem
226 * @ops: pernet operations structure for the subsystem
227 *
228 * Register a subsystem which has init and exit functions
229 * that are called when network namespaces are created and
230 * destroyed respectively.
231 *
232 * When registered all network namespace init functions are
233 * called for every existing network namespace. Allowing kernel
234 * modules to have a race free view of the set of network namespaces.
235 *
236 * When a new network namespace is created all of the init
237 * methods are called in the order in which they were registered.
238 *
239 * When a network namespace is destroyed all of the exit methods
240 * are called in the reverse of the order with which they were
241 * registered.
242 */
243int register_pernet_subsys(struct pernet_operations *ops)
244{
245 int error;
246 mutex_lock(&net_mutex);
247 error = register_pernet_operations(first_device, ops);
248 mutex_unlock(&net_mutex);
249 return error;
250}
251EXPORT_SYMBOL_GPL(register_pernet_subsys);
252
253/**
254 * unregister_pernet_subsys - unregister a network namespace subsystem
255 * @ops: pernet operations structure to manipulate
256 *
257 * Remove the pernet operations structure from the list to be
258 * used when network namespaces are created or destoryed. In
259 * addition run the exit method for all existing network
260 * namespaces.
261 */
262void unregister_pernet_subsys(struct pernet_operations *module)
263{
264 mutex_lock(&net_mutex);
265 unregister_pernet_operations(module);
266 mutex_unlock(&net_mutex);
267}
268EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
269
270/**
271 * register_pernet_device - register a network namespace device
272 * @ops: pernet operations structure for the subsystem
273 *
274 * Register a device which has init and exit functions
275 * that are called when network namespaces are created and
276 * destroyed respectively.
277 *
278 * When registered all network namespace init functions are
279 * called for every existing network namespace. Allowing kernel
280 * modules to have a race free view of the set of network namespaces.
281 *
282 * When a new network namespace is created all of the init
283 * methods are called in the order in which they were registered.
284 *
285 * When a network namespace is destroyed all of the exit methods
286 * are called in the reverse of the order with which they were
287 * registered.
288 */
289int register_pernet_device(struct pernet_operations *ops)
290{
291 int error;
292 mutex_lock(&net_mutex);
293 error = register_pernet_operations(&pernet_list, ops);
294 if (!error && (first_device == &pernet_list))
295 first_device = &ops->list;
296 mutex_unlock(&net_mutex);
297 return error;
298}
299EXPORT_SYMBOL_GPL(register_pernet_device);
300
301/**
302 * unregister_pernet_device - unregister a network namespace netdevice
303 * @ops: pernet operations structure to manipulate
304 *
305 * Remove the pernet operations structure from the list to be
306 * used when network namespaces are created or destoryed. In
307 * addition run the exit method for all existing network
308 * namespaces.
309 */
310void unregister_pernet_device(struct pernet_operations *ops)
311{
312 mutex_lock(&net_mutex);
313 if (&ops->list == first_device)
314 first_device = first_device->next;
315 unregister_pernet_operations(ops);
316 mutex_unlock(&net_mutex);
317}
318EXPORT_SYMBOL_GPL(unregister_pernet_device);