]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/core/net_namespace.c
[NET]: Mark the setup_net as __net_init
[mirror_ubuntu-artful-kernel.git] / net / core / net_namespace.c
CommitLineData
5f256bec
EB
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>
9dd776b6 7#include <linux/sched.h>
5f256bec
EB
8#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
5f256bec
EB
18LIST_HEAD(net_namespace_list);
19
20static struct kmem_cache *net_cachep;
21
22struct net init_net;
23EXPORT_SYMBOL_GPL(init_net);
24
5f256bec
EB
25/*
26 * setup_net runs the initializers for the network namespace object.
27 */
1a2ee93d 28static __net_init int setup_net(struct net *net)
5f256bec
EB
29{
30 /* Must be called with net_mutex held */
31 struct pernet_operations *ops;
5f256bec
EB
32 int error;
33
5f256bec
EB
34 atomic_set(&net->count, 1);
35 atomic_set(&net->use_count, 0);
36
37 error = 0;
768f3591 38 list_for_each_entry(ops, &pernet_list, list) {
5f256bec
EB
39 if (ops->init) {
40 error = ops->init(net);
41 if (error < 0)
42 goto out_undo;
43 }
44 }
45out:
46 return error;
768f3591 47
5f256bec
EB
48out_undo:
49 /* Walk through the list backwards calling the exit functions
50 * for the pernet modules whose init functions did not fail.
51 */
768f3591 52 list_for_each_entry_continue_reverse(ops, &pernet_list, list) {
5f256bec
EB
53 if (ops->exit)
54 ops->exit(net);
55 }
310928d9
DL
56
57 rcu_barrier();
5f256bec
EB
58 goto out;
59}
60
6a1a3b9f
PE
61#ifdef CONFIG_NET_NS
62static struct net *net_alloc(void)
63{
64 return kmem_cache_zalloc(net_cachep, GFP_KERNEL);
65}
66
9dd776b6
EB
67struct 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
9dd776b6
EB
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
f4618d39 87 rtnl_lock();
9dd776b6 88 list_add_tail(&new_net->list, &net_namespace_list);
f4618d39 89 rtnl_unlock();
9dd776b6
EB
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
6a1a3b9f
PE
103static 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;
120 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. */
127 rtnl_lock();
128 list_del(&net->list);
129 rtnl_unlock();
130
131 /* Run all of the network namespace exit methods */
132 list_for_each_entry_reverse(ops, &pernet_list, list) {
133 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
148void __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
156#else
157struct net *copy_net_ns(unsigned long flags, struct net *old_net)
158{
159 if (flags & CLONE_NEWNET)
160 return ERR_PTR(-EINVAL);
161 return old_net;
162}
163#endif
164
5f256bec
EB
165static 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
f4618d39 176 rtnl_lock();
5f256bec 177 list_add_tail(&init_net.list, &net_namespace_list);
f4618d39 178 rtnl_unlock();
5f256bec
EB
179
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
5f256bec 195 list_add_tail(&ops->list, list);
1dba323b
PE
196 if (ops->init) {
197 for_each_net(net) {
5f256bec
EB
198 error = ops->init(net);
199 if (error)
200 goto out_undo;
201 }
202 }
1dba323b 203 return 0;
5f256bec
EB
204
205out_undo:
206 /* If I have an error cleanup all namespaces I initialized */
207 list_del(&ops->list);
1dba323b
PE
208 if (ops->exit) {
209 for_each_net(undo_net) {
210 if (undo_net == net)
211 goto undone;
5f256bec 212 ops->exit(undo_net);
1dba323b 213 }
5f256bec
EB
214 }
215undone:
1dba323b 216 return error;
5f256bec
EB
217}
218
219static void unregister_pernet_operations(struct pernet_operations *ops)
220{
221 struct net *net;
222
223 list_del(&ops->list);
1dba323b
PE
224 if (ops->exit)
225 for_each_net(net)
5f256bec
EB
226 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);