662e6ea1cecfe550929ebc6e89aaec8f9d6b001f
[deliverable/linux.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 <net/net_namespace.h>
9
10 /*
11 * Our network namespace constructor/destructor lists
12 */
13
14 static LIST_HEAD(pernet_list);
15 static struct list_head *first_device = &pernet_list;
16 static DEFINE_MUTEX(net_mutex);
17
18 LIST_HEAD(net_namespace_list);
19
20 static struct kmem_cache *net_cachep;
21
22 struct net init_net;
23 EXPORT_SYMBOL_GPL(init_net);
24
25 static struct net *net_alloc(void)
26 {
27 return kmem_cache_zalloc(net_cachep, GFP_KERNEL);
28 }
29
30 static 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
44 static void cleanup_net(struct work_struct *work)
45 {
46 struct pernet_operations *ops;
47 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. */
54 rtnl_lock();
55 list_del(&net->list);
56 rtnl_unlock();
57
58 /* Run all of the network namespace exit methods */
59 list_for_each_entry_reverse(ops, &pernet_list, list) {
60 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
76 void __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 }
82 EXPORT_SYMBOL_GPL(__put_net);
83
84 /*
85 * setup_net runs the initializers for the network namespace object.
86 */
87 static int setup_net(struct net *net)
88 {
89 /* Must be called with net_mutex held */
90 struct pernet_operations *ops;
91 int error;
92
93 atomic_set(&net->count, 1);
94 atomic_set(&net->use_count, 0);
95
96 error = 0;
97 list_for_each_entry(ops, &pernet_list, list) {
98 if (ops->init) {
99 error = ops->init(net);
100 if (error < 0)
101 goto out_undo;
102 }
103 }
104 out:
105 return error;
106
107 out_undo:
108 /* Walk through the list backwards calling the exit functions
109 * for the pernet modules whose init functions did not fail.
110 */
111 list_for_each_entry_continue_reverse(ops, &pernet_list, list) {
112 if (ops->exit)
113 ops->exit(net);
114 }
115
116 rcu_barrier();
117 goto out;
118 }
119
120 struct 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
144 rtnl_lock();
145 list_add_tail(&new_net->list, &net_namespace_list);
146 rtnl_unlock();
147
148
149 out_unlock:
150 mutex_unlock(&net_mutex);
151 out:
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
160 static 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
171 rtnl_lock();
172 list_add_tail(&init_net.list, &net_namespace_list);
173 rtnl_unlock();
174
175 mutex_unlock(&net_mutex);
176 if (err)
177 panic("Could not setup the initial network namespace");
178
179 return 0;
180 }
181
182 pure_initcall(net_ns_init);
183
184 static int register_pernet_operations(struct list_head *list,
185 struct pernet_operations *ops)
186 {
187 struct net *net, *undo_net;
188 int error;
189
190 error = 0;
191 list_add_tail(&ops->list, list);
192 for_each_net(net) {
193 if (ops->init) {
194 error = ops->init(net);
195 if (error)
196 goto out_undo;
197 }
198 }
199 out:
200 return error;
201
202 out_undo:
203 /* If I have an error cleanup all namespaces I initialized */
204 list_del(&ops->list);
205 for_each_net(undo_net) {
206 if (undo_net == net)
207 goto undone;
208 if (ops->exit)
209 ops->exit(undo_net);
210 }
211 undone:
212 goto out;
213 }
214
215 static void unregister_pernet_operations(struct pernet_operations *ops)
216 {
217 struct net *net;
218
219 list_del(&ops->list);
220 for_each_net(net)
221 if (ops->exit)
222 ops->exit(net);
223 }
224
225 /**
226 * register_pernet_subsys - register a network namespace subsystem
227 * @ops: pernet operations structure for the subsystem
228 *
229 * Register a subsystem which has init and exit functions
230 * that are called when network namespaces are created and
231 * destroyed respectively.
232 *
233 * When registered all network namespace init functions are
234 * called for every existing network namespace. Allowing kernel
235 * modules to have a race free view of the set of network namespaces.
236 *
237 * When a new network namespace is created all of the init
238 * methods are called in the order in which they were registered.
239 *
240 * When a network namespace is destroyed all of the exit methods
241 * are called in the reverse of the order with which they were
242 * registered.
243 */
244 int register_pernet_subsys(struct pernet_operations *ops)
245 {
246 int error;
247 mutex_lock(&net_mutex);
248 error = register_pernet_operations(first_device, ops);
249 mutex_unlock(&net_mutex);
250 return error;
251 }
252 EXPORT_SYMBOL_GPL(register_pernet_subsys);
253
254 /**
255 * unregister_pernet_subsys - unregister a network namespace subsystem
256 * @ops: pernet operations structure to manipulate
257 *
258 * Remove the pernet operations structure from the list to be
259 * used when network namespaces are created or destoryed. In
260 * addition run the exit method for all existing network
261 * namespaces.
262 */
263 void unregister_pernet_subsys(struct pernet_operations *module)
264 {
265 mutex_lock(&net_mutex);
266 unregister_pernet_operations(module);
267 mutex_unlock(&net_mutex);
268 }
269 EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
270
271 /**
272 * register_pernet_device - register a network namespace device
273 * @ops: pernet operations structure for the subsystem
274 *
275 * Register a device which has init and exit functions
276 * that are called when network namespaces are created and
277 * destroyed respectively.
278 *
279 * When registered all network namespace init functions are
280 * called for every existing network namespace. Allowing kernel
281 * modules to have a race free view of the set of network namespaces.
282 *
283 * When a new network namespace is created all of the init
284 * methods are called in the order in which they were registered.
285 *
286 * When a network namespace is destroyed all of the exit methods
287 * are called in the reverse of the order with which they were
288 * registered.
289 */
290 int register_pernet_device(struct pernet_operations *ops)
291 {
292 int error;
293 mutex_lock(&net_mutex);
294 error = register_pernet_operations(&pernet_list, ops);
295 if (!error && (first_device == &pernet_list))
296 first_device = &ops->list;
297 mutex_unlock(&net_mutex);
298 return error;
299 }
300 EXPORT_SYMBOL_GPL(register_pernet_device);
301
302 /**
303 * unregister_pernet_device - unregister a network namespace netdevice
304 * @ops: pernet operations structure to manipulate
305 *
306 * Remove the pernet operations structure from the list to be
307 * used when network namespaces are created or destoryed. In
308 * addition run the exit method for all existing network
309 * namespaces.
310 */
311 void unregister_pernet_device(struct pernet_operations *ops)
312 {
313 mutex_lock(&net_mutex);
314 if (&ops->list == first_device)
315 first_device = first_device->next;
316 unregister_pernet_operations(ops);
317 mutex_unlock(&net_mutex);
318 }
319 EXPORT_SYMBOL_GPL(unregister_pernet_device);
This page took 0.044297 seconds and 4 git commands to generate.