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>
11 * Our network namespace constructor/destructor lists
14 static LIST_HEAD(pernet_list
);
15 static struct list_head
*first_device
= &pernet_list
;
16 static DEFINE_MUTEX(net_mutex
);
18 LIST_HEAD(net_namespace_list
);
21 EXPORT_SYMBOL(init_net
);
24 * setup_net runs the initializers for the network namespace object.
26 static __net_init
int setup_net(struct net
*net
)
28 /* Must be called with net_mutex held */
29 struct pernet_operations
*ops
;
32 atomic_set(&net
->count
, 1);
33 atomic_set(&net
->use_count
, 0);
36 list_for_each_entry(ops
, &pernet_list
, list
) {
38 error
= ops
->init(net
);
47 /* Walk through the list backwards calling the exit functions
48 * for the pernet modules whose init functions did not fail.
50 list_for_each_entry_continue_reverse(ops
, &pernet_list
, list
) {
60 static struct kmem_cache
*net_cachep
;
61 static struct workqueue_struct
*netns_wq
;
63 static struct net
*net_alloc(void)
65 return kmem_cache_zalloc(net_cachep
, GFP_KERNEL
);
68 static void net_free(struct net
*net
)
73 if (unlikely(atomic_read(&net
->use_count
) != 0)) {
74 printk(KERN_EMERG
"network namespace not free! Usage: %d\n",
75 atomic_read(&net
->use_count
));
79 kmem_cache_free(net_cachep
, net
);
82 struct net
*copy_net_ns(unsigned long flags
, struct net
*old_net
)
84 struct net
*new_net
= NULL
;
89 if (!(flags
& CLONE_NEWNET
))
93 new_net
= net_alloc();
97 mutex_lock(&net_mutex
);
98 err
= setup_net(new_net
);
103 list_add_tail(&new_net
->list
, &net_namespace_list
);
108 mutex_unlock(&net_mutex
);
113 new_net
= ERR_PTR(err
);
118 static void cleanup_net(struct work_struct
*work
)
120 struct pernet_operations
*ops
;
123 net
= container_of(work
, struct net
, work
);
125 mutex_lock(&net_mutex
);
127 /* Don't let anyone else find us. */
129 list_del(&net
->list
);
132 /* Run all of the network namespace exit methods */
133 list_for_each_entry_reverse(ops
, &pernet_list
, list
) {
138 mutex_unlock(&net_mutex
);
140 /* Ensure there are no outstanding rcu callbacks using this
145 /* Finally it is safe to free my network namespace structure */
149 void __put_net(struct net
*net
)
151 /* Cleanup the network namespace in process context */
152 INIT_WORK(&net
->work
, cleanup_net
);
153 queue_work(netns_wq
, &net
->work
);
155 EXPORT_SYMBOL_GPL(__put_net
);
158 struct net
*copy_net_ns(unsigned long flags
, struct net
*old_net
)
160 if (flags
& CLONE_NEWNET
)
161 return ERR_PTR(-EINVAL
);
166 static int __init
net_ns_init(void)
170 printk(KERN_INFO
"net_namespace: %zd bytes\n", sizeof(struct net
));
172 net_cachep
= kmem_cache_create("net_namespace", sizeof(struct net
),
176 /* Create workqueue for cleanup */
177 netns_wq
= create_singlethread_workqueue("netns");
179 panic("Could not create netns workq");
182 mutex_lock(&net_mutex
);
183 err
= setup_net(&init_net
);
186 list_add_tail(&init_net
.list
, &net_namespace_list
);
189 mutex_unlock(&net_mutex
);
191 panic("Could not setup the initial network namespace");
196 pure_initcall(net_ns_init
);
199 static int register_pernet_operations(struct list_head
*list
,
200 struct pernet_operations
*ops
)
202 struct net
*net
, *undo_net
;
205 list_add_tail(&ops
->list
, list
);
208 error
= ops
->init(net
);
216 /* If I have an error cleanup all namespaces I initialized */
217 list_del(&ops
->list
);
219 for_each_net(undo_net
) {
229 static void unregister_pernet_operations(struct pernet_operations
*ops
)
233 list_del(&ops
->list
);
241 static int register_pernet_operations(struct list_head
*list
,
242 struct pernet_operations
*ops
)
244 if (ops
->init
== NULL
)
246 return ops
->init(&init_net
);
249 static void unregister_pernet_operations(struct pernet_operations
*ops
)
252 ops
->exit(&init_net
);
257 * register_pernet_subsys - register a network namespace subsystem
258 * @ops: pernet operations structure for the subsystem
260 * Register a subsystem which has init and exit functions
261 * that are called when network namespaces are created and
262 * destroyed respectively.
264 * When registered all network namespace init functions are
265 * called for every existing network namespace. Allowing kernel
266 * modules to have a race free view of the set of network namespaces.
268 * When a new network namespace is created all of the init
269 * methods are called in the order in which they were registered.
271 * When a network namespace is destroyed all of the exit methods
272 * are called in the reverse of the order with which they were
275 int register_pernet_subsys(struct pernet_operations
*ops
)
278 mutex_lock(&net_mutex
);
279 error
= register_pernet_operations(first_device
, ops
);
280 mutex_unlock(&net_mutex
);
283 EXPORT_SYMBOL_GPL(register_pernet_subsys
);
286 * unregister_pernet_subsys - unregister a network namespace subsystem
287 * @ops: pernet operations structure to manipulate
289 * Remove the pernet operations structure from the list to be
290 * used when network namespaces are created or destoryed. In
291 * addition run the exit method for all existing network
294 void unregister_pernet_subsys(struct pernet_operations
*module
)
296 mutex_lock(&net_mutex
);
297 unregister_pernet_operations(module
);
298 mutex_unlock(&net_mutex
);
300 EXPORT_SYMBOL_GPL(unregister_pernet_subsys
);
303 * register_pernet_device - register a network namespace device
304 * @ops: pernet operations structure for the subsystem
306 * Register a device which has init and exit functions
307 * that are called when network namespaces are created and
308 * destroyed respectively.
310 * When registered all network namespace init functions are
311 * called for every existing network namespace. Allowing kernel
312 * modules to have a race free view of the set of network namespaces.
314 * When a new network namespace is created all of the init
315 * methods are called in the order in which they were registered.
317 * When a network namespace is destroyed all of the exit methods
318 * are called in the reverse of the order with which they were
321 int register_pernet_device(struct pernet_operations
*ops
)
324 mutex_lock(&net_mutex
);
325 error
= register_pernet_operations(&pernet_list
, ops
);
326 if (!error
&& (first_device
== &pernet_list
))
327 first_device
= &ops
->list
;
328 mutex_unlock(&net_mutex
);
331 EXPORT_SYMBOL_GPL(register_pernet_device
);
334 * unregister_pernet_device - unregister a network namespace netdevice
335 * @ops: pernet operations structure to manipulate
337 * Remove the pernet operations structure from the list to be
338 * used when network namespaces are created or destoryed. In
339 * addition run the exit method for all existing network
342 void unregister_pernet_device(struct pernet_operations
*ops
)
344 mutex_lock(&net_mutex
);
345 if (&ops
->list
== first_device
)
346 first_device
= first_device
->next
;
347 unregister_pernet_operations(ops
);
348 mutex_unlock(&net_mutex
);
350 EXPORT_SYMBOL_GPL(unregister_pernet_device
);