]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/core/net_namespace.c
[NET]: Add Tehuti network driver.
[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>
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
678aa8e4 35#if 0
5f256bec
EB
36static struct net *net_alloc(void)
37{
38 return kmem_cache_alloc(net_cachep, GFP_KERNEL);
39}
678aa8e4 40#endif
5f256bec
EB
41
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;
59 struct list_head *ptr;
60 struct net *net;
61
62 net = container_of(work, struct net, work);
63
64 mutex_lock(&net_mutex);
65
66 /* Don't let anyone else find us. */
67 net_lock();
68 list_del(&net->list);
69 net_unlock();
70
71 /* Run all of the network namespace exit methods */
72 list_for_each_prev(ptr, &pernet_list) {
73 ops = list_entry(ptr, struct pernet_operations, list);
74 if (ops->exit)
75 ops->exit(net);
76 }
77
78 mutex_unlock(&net_mutex);
79
80 /* Ensure there are no outstanding rcu callbacks using this
81 * network namespace.
82 */
83 rcu_barrier();
84
85 /* Finally it is safe to free my network namespace structure */
86 net_free(net);
87}
88
89
90void __put_net(struct net *net)
91{
92 /* Cleanup the network namespace in process context */
93 INIT_WORK(&net->work, cleanup_net);
94 schedule_work(&net->work);
95}
96EXPORT_SYMBOL_GPL(__put_net);
97
98/*
99 * setup_net runs the initializers for the network namespace object.
100 */
101static int setup_net(struct net *net)
102{
103 /* Must be called with net_mutex held */
104 struct pernet_operations *ops;
105 struct list_head *ptr;
106 int error;
107
108 memset(net, 0, sizeof(struct net));
109 atomic_set(&net->count, 1);
110 atomic_set(&net->use_count, 0);
111
112 error = 0;
113 list_for_each(ptr, &pernet_list) {
114 ops = list_entry(ptr, struct pernet_operations, list);
115 if (ops->init) {
116 error = ops->init(net);
117 if (error < 0)
118 goto out_undo;
119 }
120 }
121out:
122 return error;
123out_undo:
124 /* Walk through the list backwards calling the exit functions
125 * for the pernet modules whose init functions did not fail.
126 */
127 for (ptr = ptr->prev; ptr != &pernet_list; ptr = ptr->prev) {
128 ops = list_entry(ptr, struct pernet_operations, list);
129 if (ops->exit)
130 ops->exit(net);
131 }
132 goto out;
133}
134
135static int __init net_ns_init(void)
136{
137 int err;
138
139 printk(KERN_INFO "net_namespace: %zd bytes\n", sizeof(struct net));
140 net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
141 SMP_CACHE_BYTES,
142 SLAB_PANIC, NULL);
143 mutex_lock(&net_mutex);
144 err = setup_net(&init_net);
145
146 net_lock();
147 list_add_tail(&init_net.list, &net_namespace_list);
148 net_unlock();
149
150 mutex_unlock(&net_mutex);
151 if (err)
152 panic("Could not setup the initial network namespace");
153
154 return 0;
155}
156
157pure_initcall(net_ns_init);
158
159static int register_pernet_operations(struct list_head *list,
160 struct pernet_operations *ops)
161{
162 struct net *net, *undo_net;
163 int error;
164
165 error = 0;
166 list_add_tail(&ops->list, list);
167 for_each_net(net) {
168 if (ops->init) {
169 error = ops->init(net);
170 if (error)
171 goto out_undo;
172 }
173 }
174out:
175 return error;
176
177out_undo:
178 /* If I have an error cleanup all namespaces I initialized */
179 list_del(&ops->list);
180 for_each_net(undo_net) {
181 if (undo_net == net)
182 goto undone;
183 if (ops->exit)
184 ops->exit(undo_net);
185 }
186undone:
187 goto out;
188}
189
190static void unregister_pernet_operations(struct pernet_operations *ops)
191{
192 struct net *net;
193
194 list_del(&ops->list);
195 for_each_net(net)
196 if (ops->exit)
197 ops->exit(net);
198}
199
200/**
201 * register_pernet_subsys - register a network namespace subsystem
202 * @ops: pernet operations structure for the subsystem
203 *
204 * Register a subsystem which has init and exit functions
205 * that are called when network namespaces are created and
206 * destroyed respectively.
207 *
208 * When registered all network namespace init functions are
209 * called for every existing network namespace. Allowing kernel
210 * modules to have a race free view of the set of network namespaces.
211 *
212 * When a new network namespace is created all of the init
213 * methods are called in the order in which they were registered.
214 *
215 * When a network namespace is destroyed all of the exit methods
216 * are called in the reverse of the order with which they were
217 * registered.
218 */
219int register_pernet_subsys(struct pernet_operations *ops)
220{
221 int error;
222 mutex_lock(&net_mutex);
223 error = register_pernet_operations(first_device, ops);
224 mutex_unlock(&net_mutex);
225 return error;
226}
227EXPORT_SYMBOL_GPL(register_pernet_subsys);
228
229/**
230 * unregister_pernet_subsys - unregister a network namespace subsystem
231 * @ops: pernet operations structure to manipulate
232 *
233 * Remove the pernet operations structure from the list to be
234 * used when network namespaces are created or destoryed. In
235 * addition run the exit method for all existing network
236 * namespaces.
237 */
238void unregister_pernet_subsys(struct pernet_operations *module)
239{
240 mutex_lock(&net_mutex);
241 unregister_pernet_operations(module);
242 mutex_unlock(&net_mutex);
243}
244EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
245
246/**
247 * register_pernet_device - register a network namespace device
248 * @ops: pernet operations structure for the subsystem
249 *
250 * Register a device which has init and exit functions
251 * that are called when network namespaces are created and
252 * destroyed respectively.
253 *
254 * When registered all network namespace init functions are
255 * called for every existing network namespace. Allowing kernel
256 * modules to have a race free view of the set of network namespaces.
257 *
258 * When a new network namespace is created all of the init
259 * methods are called in the order in which they were registered.
260 *
261 * When a network namespace is destroyed all of the exit methods
262 * are called in the reverse of the order with which they were
263 * registered.
264 */
265int register_pernet_device(struct pernet_operations *ops)
266{
267 int error;
268 mutex_lock(&net_mutex);
269 error = register_pernet_operations(&pernet_list, ops);
270 if (!error && (first_device == &pernet_list))
271 first_device = &ops->list;
272 mutex_unlock(&net_mutex);
273 return error;
274}
275EXPORT_SYMBOL_GPL(register_pernet_device);
276
277/**
278 * unregister_pernet_device - unregister a network namespace netdevice
279 * @ops: pernet operations structure to manipulate
280 *
281 * Remove the pernet operations structure from the list to be
282 * used when network namespaces are created or destoryed. In
283 * addition run the exit method for all existing network
284 * namespaces.
285 */
286void unregister_pernet_device(struct pernet_operations *ops)
287{
288 mutex_lock(&net_mutex);
289 if (&ops->list == first_device)
290 first_device = first_device->next;
291 unregister_pernet_operations(ops);
292 mutex_unlock(&net_mutex);
293}
294EXPORT_SYMBOL_GPL(unregister_pernet_device);