blob: 0e6cb02d7b77dce2016c15f18147ad92a049fb9d [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>
7#include <net/net_namespace.h>
8
9/*
10 * Our network namespace constructor/destructor lists
11 */
12
13static LIST_HEAD(pernet_list);
14static struct list_head *first_device = &pernet_list;
15static DEFINE_MUTEX(net_mutex);
16
17static DEFINE_MUTEX(net_list_mutex);
18LIST_HEAD(net_namespace_list);
19
20static struct kmem_cache *net_cachep;
21
22struct net init_net;
23EXPORT_SYMBOL_GPL(init_net);
24
25void net_lock(void)
26{
27 mutex_lock(&net_list_mutex);
28}
29
30void net_unlock(void)
31{
32 mutex_unlock(&net_list_mutex);
33}
34
David S. Miller678aa8e2007-09-12 14:01:08 +020035#if 0
Eric W. Biederman5f256be2007-09-12 11:50:50 +020036static struct net *net_alloc(void)
37{
38 return kmem_cache_alloc(net_cachep, GFP_KERNEL);
39}
David S. Miller678aa8e2007-09-12 14:01:08 +020040#endif
Eric W. Biederman5f256be2007-09-12 11:50:50 +020041
42static void net_free(struct net *net)
43{
44 if (!net)
45 return;
46
47 if (unlikely(atomic_read(&net->use_count) != 0)) {
48 printk(KERN_EMERG "network namespace not free! Usage: %d\n",
49 atomic_read(&net->use_count));
50 return;
51 }
52
53 kmem_cache_free(net_cachep, net);
54}
55
56static void cleanup_net(struct work_struct *work)
57{
58 struct pernet_operations *ops;
Eric W. Biederman5f256be2007-09-12 11:50:50 +020059 struct net *net;
60
61 net = container_of(work, struct net, work);
62
63 mutex_lock(&net_mutex);
64
65 /* Don't let anyone else find us. */
66 net_lock();
67 list_del(&net->list);
68 net_unlock();
69
70 /* Run all of the network namespace exit methods */
Pavel Emelyanov768f35912007-09-18 13:20:41 -070071 list_for_each_entry_reverse(ops, &pernet_list, list) {
Eric W. Biederman5f256be2007-09-12 11:50:50 +020072 if (ops->exit)
73 ops->exit(net);
74 }
75
76 mutex_unlock(&net_mutex);
77
78 /* Ensure there are no outstanding rcu callbacks using this
79 * network namespace.
80 */
81 rcu_barrier();
82
83 /* Finally it is safe to free my network namespace structure */
84 net_free(net);
85}
86
87
88void __put_net(struct net *net)
89{
90 /* Cleanup the network namespace in process context */
91 INIT_WORK(&net->work, cleanup_net);
92 schedule_work(&net->work);
93}
94EXPORT_SYMBOL_GPL(__put_net);
95
96/*
97 * setup_net runs the initializers for the network namespace object.
98 */
99static int setup_net(struct net *net)
100{
101 /* Must be called with net_mutex held */
102 struct pernet_operations *ops;
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200103 int error;
104
105 memset(net, 0, sizeof(struct net));
106 atomic_set(&net->count, 1);
107 atomic_set(&net->use_count, 0);
108
109 error = 0;
Pavel Emelyanov768f35912007-09-18 13:20:41 -0700110 list_for_each_entry(ops, &pernet_list, list) {
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200111 if (ops->init) {
112 error = ops->init(net);
113 if (error < 0)
114 goto out_undo;
115 }
116 }
117out:
118 return error;
Pavel Emelyanov768f35912007-09-18 13:20:41 -0700119
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200120out_undo:
121 /* Walk through the list backwards calling the exit functions
122 * for the pernet modules whose init functions did not fail.
123 */
Pavel Emelyanov768f35912007-09-18 13:20:41 -0700124 list_for_each_entry_continue_reverse(ops, &pernet_list, list) {
Eric W. Biederman5f256be2007-09-12 11:50:50 +0200125 if (ops->exit)
126 ops->exit(net);
127 }
128 goto out;
129}
130
131static int __init net_ns_init(void)
132{
133 int err;
134
135 printk(KERN_INFO "net_namespace: %zd bytes\n", sizeof(struct net));
136 net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
137 SMP_CACHE_BYTES,
138 SLAB_PANIC, NULL);
139 mutex_lock(&net_mutex);
140 err = setup_net(&init_net);
141
142 net_lock();
143 list_add_tail(&init_net.list, &net_namespace_list);
144 net_unlock();
145
146 mutex_unlock(&net_mutex);
147 if (err)
148 panic("Could not setup the initial network namespace");
149
150 return 0;
151}
152
153pure_initcall(net_ns_init);
154
155static int register_pernet_operations(struct list_head *list,
156 struct pernet_operations *ops)
157{
158 struct net *net, *undo_net;
159 int error;
160
161 error = 0;
162 list_add_tail(&ops->list, list);
163 for_each_net(net) {
164 if (ops->init) {
165 error = ops->init(net);
166 if (error)
167 goto out_undo;
168 }
169 }
170out:
171 return error;
172
173out_undo:
174 /* If I have an error cleanup all namespaces I initialized */
175 list_del(&ops->list);
176 for_each_net(undo_net) {
177 if (undo_net == net)
178 goto undone;
179 if (ops->exit)
180 ops->exit(undo_net);
181 }
182undone:
183 goto out;
184}
185
186static void unregister_pernet_operations(struct pernet_operations *ops)
187{
188 struct net *net;
189
190 list_del(&ops->list);
191 for_each_net(net)
192 if (ops->exit)
193 ops->exit(net);
194}
195
196/**
197 * register_pernet_subsys - register a network namespace subsystem
198 * @ops: pernet operations structure for the subsystem
199 *
200 * Register a subsystem which has init and exit functions
201 * that are called when network namespaces are created and
202 * destroyed respectively.
203 *
204 * When registered all network namespace init functions are
205 * called for every existing network namespace. Allowing kernel
206 * modules to have a race free view of the set of network namespaces.
207 *
208 * When a new network namespace is created all of the init
209 * methods are called in the order in which they were registered.
210 *
211 * When a network namespace is destroyed all of the exit methods
212 * are called in the reverse of the order with which they were
213 * registered.
214 */
215int register_pernet_subsys(struct pernet_operations *ops)
216{
217 int error;
218 mutex_lock(&net_mutex);
219 error = register_pernet_operations(first_device, ops);
220 mutex_unlock(&net_mutex);
221 return error;
222}
223EXPORT_SYMBOL_GPL(register_pernet_subsys);
224
225/**
226 * unregister_pernet_subsys - unregister a network namespace subsystem
227 * @ops: pernet operations structure to manipulate
228 *
229 * Remove the pernet operations structure from the list to be
230 * used when network namespaces are created or destoryed. In
231 * addition run the exit method for all existing network
232 * namespaces.
233 */
234void unregister_pernet_subsys(struct pernet_operations *module)
235{
236 mutex_lock(&net_mutex);
237 unregister_pernet_operations(module);
238 mutex_unlock(&net_mutex);
239}
240EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
241
242/**
243 * register_pernet_device - register a network namespace device
244 * @ops: pernet operations structure for the subsystem
245 *
246 * Register a device which has init and exit functions
247 * that are called when network namespaces are created and
248 * destroyed respectively.
249 *
250 * When registered all network namespace init functions are
251 * called for every existing network namespace. Allowing kernel
252 * modules to have a race free view of the set of network namespaces.
253 *
254 * When a new network namespace is created all of the init
255 * methods are called in the order in which they were registered.
256 *
257 * When a network namespace is destroyed all of the exit methods
258 * are called in the reverse of the order with which they were
259 * registered.
260 */
261int register_pernet_device(struct pernet_operations *ops)
262{
263 int error;
264 mutex_lock(&net_mutex);
265 error = register_pernet_operations(&pernet_list, ops);
266 if (!error && (first_device == &pernet_list))
267 first_device = &ops->list;
268 mutex_unlock(&net_mutex);
269 return error;
270}
271EXPORT_SYMBOL_GPL(register_pernet_device);
272
273/**
274 * unregister_pernet_device - unregister a network namespace netdevice
275 * @ops: pernet operations structure to manipulate
276 *
277 * Remove the pernet operations structure from the list to be
278 * used when network namespaces are created or destoryed. In
279 * addition run the exit method for all existing network
280 * namespaces.
281 */
282void unregister_pernet_device(struct pernet_operations *ops)
283{
284 mutex_lock(&net_mutex);
285 if (&ops->list == first_device)
286 first_device = first_device->next;
287 unregister_pernet_operations(ops);
288 mutex_unlock(&net_mutex);
289}
290EXPORT_SYMBOL_GPL(unregister_pernet_device);