]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - net/netfilter/nf_conntrack_proto.c
9bd34647225a2a22304ab780ff80221642d27251
[mirror_ubuntu-artful-kernel.git] / net / netfilter / nf_conntrack_proto.c
1 /* L3/L4 protocol support for nf_conntrack. */
2
3 /* (C) 1999-2001 Paul `Rusty' Russell
4 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
5 * (C) 2003,2004 USAGI/WIDE Project <http://www.linux-ipv6.org>
6 * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13 #include <linux/types.h>
14 #include <linux/netfilter.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/mutex.h>
18 #include <linux/vmalloc.h>
19 #include <linux/stddef.h>
20 #include <linux/err.h>
21 #include <linux/percpu.h>
22 #include <linux/notifier.h>
23 #include <linux/kernel.h>
24 #include <linux/netdevice.h>
25
26 #include <net/netfilter/nf_conntrack.h>
27 #include <net/netfilter/nf_conntrack_l3proto.h>
28 #include <net/netfilter/nf_conntrack_l4proto.h>
29 #include <net/netfilter/nf_conntrack_core.h>
30
31 static struct nf_conntrack_l4proto __rcu **nf_ct_protos[PF_MAX] __read_mostly;
32 struct nf_conntrack_l3proto __rcu *nf_ct_l3protos[AF_MAX] __read_mostly;
33 EXPORT_SYMBOL_GPL(nf_ct_l3protos);
34
35 static DEFINE_MUTEX(nf_ct_proto_mutex);
36
37 #ifdef CONFIG_SYSCTL
38 static int
39 nf_ct_register_sysctl(struct net *net,
40 struct ctl_table_header **header,
41 const char *path,
42 struct ctl_table *table)
43 {
44 if (*header == NULL) {
45 *header = register_net_sysctl(net, path, table);
46 if (*header == NULL)
47 return -ENOMEM;
48 }
49
50 return 0;
51 }
52
53 static void
54 nf_ct_unregister_sysctl(struct ctl_table_header **header,
55 struct ctl_table **table,
56 unsigned int users)
57 {
58 if (users > 0)
59 return;
60
61 unregister_net_sysctl_table(*header);
62 kfree(*table);
63 *header = NULL;
64 *table = NULL;
65 }
66 #endif
67
68 struct nf_conntrack_l4proto *
69 __nf_ct_l4proto_find(u_int16_t l3proto, u_int8_t l4proto)
70 {
71 if (unlikely(l3proto >= AF_MAX || nf_ct_protos[l3proto] == NULL))
72 return &nf_conntrack_l4proto_generic;
73
74 return rcu_dereference(nf_ct_protos[l3proto][l4proto]);
75 }
76 EXPORT_SYMBOL_GPL(__nf_ct_l4proto_find);
77
78 /* this is guaranteed to always return a valid protocol helper, since
79 * it falls back to generic_protocol */
80 struct nf_conntrack_l3proto *
81 nf_ct_l3proto_find_get(u_int16_t l3proto)
82 {
83 struct nf_conntrack_l3proto *p;
84
85 rcu_read_lock();
86 p = __nf_ct_l3proto_find(l3proto);
87 if (!try_module_get(p->me))
88 p = &nf_conntrack_l3proto_generic;
89 rcu_read_unlock();
90
91 return p;
92 }
93 EXPORT_SYMBOL_GPL(nf_ct_l3proto_find_get);
94
95 int
96 nf_ct_l3proto_try_module_get(unsigned short l3proto)
97 {
98 int ret;
99 struct nf_conntrack_l3proto *p;
100
101 retry: p = nf_ct_l3proto_find_get(l3proto);
102 if (p == &nf_conntrack_l3proto_generic) {
103 ret = request_module("nf_conntrack-%d", l3proto);
104 if (!ret)
105 goto retry;
106
107 return -EPROTOTYPE;
108 }
109
110 return 0;
111 }
112 EXPORT_SYMBOL_GPL(nf_ct_l3proto_try_module_get);
113
114 void nf_ct_l3proto_module_put(unsigned short l3proto)
115 {
116 struct nf_conntrack_l3proto *p;
117
118 /* rcu_read_lock not necessary since the caller holds a reference, but
119 * taken anyways to avoid lockdep warnings in __nf_ct_l3proto_find()
120 */
121 rcu_read_lock();
122 p = __nf_ct_l3proto_find(l3proto);
123 module_put(p->me);
124 rcu_read_unlock();
125 }
126 EXPORT_SYMBOL_GPL(nf_ct_l3proto_module_put);
127
128 struct nf_conntrack_l4proto *
129 nf_ct_l4proto_find_get(u_int16_t l3num, u_int8_t l4num)
130 {
131 struct nf_conntrack_l4proto *p;
132
133 rcu_read_lock();
134 p = __nf_ct_l4proto_find(l3num, l4num);
135 if (!try_module_get(p->me))
136 p = &nf_conntrack_l4proto_generic;
137 rcu_read_unlock();
138
139 return p;
140 }
141 EXPORT_SYMBOL_GPL(nf_ct_l4proto_find_get);
142
143 void nf_ct_l4proto_put(struct nf_conntrack_l4proto *p)
144 {
145 module_put(p->me);
146 }
147 EXPORT_SYMBOL_GPL(nf_ct_l4proto_put);
148
149 static int kill_l3proto(struct nf_conn *i, void *data)
150 {
151 return nf_ct_l3num(i) == ((struct nf_conntrack_l3proto *)data)->l3proto;
152 }
153
154 static int kill_l4proto(struct nf_conn *i, void *data)
155 {
156 struct nf_conntrack_l4proto *l4proto;
157 l4proto = (struct nf_conntrack_l4proto *)data;
158 return nf_ct_protonum(i) == l4proto->l4proto &&
159 nf_ct_l3num(i) == l4proto->l3proto;
160 }
161
162 int nf_ct_l3proto_register(struct nf_conntrack_l3proto *proto)
163 {
164 int ret = 0;
165 struct nf_conntrack_l3proto *old;
166
167 if (proto->l3proto >= AF_MAX)
168 return -EBUSY;
169
170 if (proto->tuple_to_nlattr && !proto->nlattr_tuple_size)
171 return -EINVAL;
172
173 mutex_lock(&nf_ct_proto_mutex);
174 old = rcu_dereference_protected(nf_ct_l3protos[proto->l3proto],
175 lockdep_is_held(&nf_ct_proto_mutex));
176 if (old != &nf_conntrack_l3proto_generic) {
177 ret = -EBUSY;
178 goto out_unlock;
179 }
180
181 if (proto->nlattr_tuple_size)
182 proto->nla_size = 3 * proto->nlattr_tuple_size();
183
184 rcu_assign_pointer(nf_ct_l3protos[proto->l3proto], proto);
185
186 out_unlock:
187 mutex_unlock(&nf_ct_proto_mutex);
188 return ret;
189
190 }
191 EXPORT_SYMBOL_GPL(nf_ct_l3proto_register);
192
193 int nf_ct_l3proto_pernet_register(struct net *net,
194 struct nf_conntrack_l3proto *proto)
195 {
196 int ret;
197
198 if (proto->init_net) {
199 ret = proto->init_net(net);
200 if (ret < 0)
201 return ret;
202 }
203
204 return 0;
205 }
206 EXPORT_SYMBOL_GPL(nf_ct_l3proto_pernet_register);
207
208 void nf_ct_l3proto_unregister(struct nf_conntrack_l3proto *proto)
209 {
210 BUG_ON(proto->l3proto >= AF_MAX);
211
212 mutex_lock(&nf_ct_proto_mutex);
213 BUG_ON(rcu_dereference_protected(nf_ct_l3protos[proto->l3proto],
214 lockdep_is_held(&nf_ct_proto_mutex)
215 ) != proto);
216 rcu_assign_pointer(nf_ct_l3protos[proto->l3proto],
217 &nf_conntrack_l3proto_generic);
218 mutex_unlock(&nf_ct_proto_mutex);
219
220 synchronize_rcu();
221 }
222 EXPORT_SYMBOL_GPL(nf_ct_l3proto_unregister);
223
224 void nf_ct_l3proto_pernet_unregister(struct net *net,
225 struct nf_conntrack_l3proto *proto)
226 {
227 /* Remove all contrack entries for this protocol */
228 nf_ct_iterate_cleanup(net, kill_l3proto, proto, 0, 0);
229 }
230 EXPORT_SYMBOL_GPL(nf_ct_l3proto_pernet_unregister);
231
232 static struct nf_proto_net *nf_ct_l4proto_net(struct net *net,
233 struct nf_conntrack_l4proto *l4proto)
234 {
235 if (l4proto->get_net_proto) {
236 /* statically built-in protocols use static per-net */
237 return l4proto->get_net_proto(net);
238 } else if (l4proto->net_id) {
239 /* ... and loadable protocols use dynamic per-net */
240 return net_generic(net, *l4proto->net_id);
241 }
242 return NULL;
243 }
244
245 static
246 int nf_ct_l4proto_register_sysctl(struct net *net,
247 struct nf_proto_net *pn,
248 struct nf_conntrack_l4proto *l4proto)
249 {
250 int err = 0;
251
252 #ifdef CONFIG_SYSCTL
253 if (pn->ctl_table != NULL) {
254 err = nf_ct_register_sysctl(net,
255 &pn->ctl_table_header,
256 "net/netfilter",
257 pn->ctl_table);
258 if (err < 0) {
259 if (!pn->users) {
260 kfree(pn->ctl_table);
261 pn->ctl_table = NULL;
262 }
263 }
264 }
265 #endif /* CONFIG_SYSCTL */
266 return err;
267 }
268
269 static
270 void nf_ct_l4proto_unregister_sysctl(struct net *net,
271 struct nf_proto_net *pn,
272 struct nf_conntrack_l4proto *l4proto)
273 {
274 #ifdef CONFIG_SYSCTL
275 if (pn->ctl_table_header != NULL)
276 nf_ct_unregister_sysctl(&pn->ctl_table_header,
277 &pn->ctl_table,
278 pn->users);
279 #endif /* CONFIG_SYSCTL */
280 }
281
282 /* FIXME: Allow NULL functions and sub in pointers to generic for
283 them. --RR */
284 int nf_ct_l4proto_register_one(struct nf_conntrack_l4proto *l4proto)
285 {
286 int ret = 0;
287
288 if (l4proto->l3proto >= PF_MAX)
289 return -EBUSY;
290
291 if ((l4proto->to_nlattr && !l4proto->nlattr_size) ||
292 (l4proto->tuple_to_nlattr && !l4proto->nlattr_tuple_size))
293 return -EINVAL;
294
295 mutex_lock(&nf_ct_proto_mutex);
296 if (!nf_ct_protos[l4proto->l3proto]) {
297 /* l3proto may be loaded latter. */
298 struct nf_conntrack_l4proto __rcu **proto_array;
299 int i;
300
301 proto_array = kmalloc(MAX_NF_CT_PROTO *
302 sizeof(struct nf_conntrack_l4proto *),
303 GFP_KERNEL);
304 if (proto_array == NULL) {
305 ret = -ENOMEM;
306 goto out_unlock;
307 }
308
309 for (i = 0; i < MAX_NF_CT_PROTO; i++)
310 RCU_INIT_POINTER(proto_array[i],
311 &nf_conntrack_l4proto_generic);
312
313 /* Before making proto_array visible to lockless readers,
314 * we must make sure its content is committed to memory.
315 */
316 smp_wmb();
317
318 nf_ct_protos[l4proto->l3proto] = proto_array;
319 } else if (rcu_dereference_protected(
320 nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
321 lockdep_is_held(&nf_ct_proto_mutex)
322 ) != &nf_conntrack_l4proto_generic) {
323 ret = -EBUSY;
324 goto out_unlock;
325 }
326
327 l4proto->nla_size = 0;
328 if (l4proto->nlattr_size)
329 l4proto->nla_size += l4proto->nlattr_size();
330 if (l4proto->nlattr_tuple_size)
331 l4proto->nla_size += 3 * l4proto->nlattr_tuple_size();
332
333 rcu_assign_pointer(nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
334 l4proto);
335 out_unlock:
336 mutex_unlock(&nf_ct_proto_mutex);
337 return ret;
338 }
339 EXPORT_SYMBOL_GPL(nf_ct_l4proto_register_one);
340
341 int nf_ct_l4proto_pernet_register_one(struct net *net,
342 struct nf_conntrack_l4proto *l4proto)
343 {
344 int ret = 0;
345 struct nf_proto_net *pn = NULL;
346
347 if (l4proto->init_net) {
348 ret = l4proto->init_net(net, l4proto->l3proto);
349 if (ret < 0)
350 goto out;
351 }
352
353 pn = nf_ct_l4proto_net(net, l4proto);
354 if (pn == NULL)
355 goto out;
356
357 ret = nf_ct_l4proto_register_sysctl(net, pn, l4proto);
358 if (ret < 0)
359 goto out;
360
361 pn->users++;
362 out:
363 return ret;
364 }
365 EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_register_one);
366
367 void nf_ct_l4proto_unregister_one(struct nf_conntrack_l4proto *l4proto)
368 {
369 BUG_ON(l4proto->l3proto >= PF_MAX);
370
371 mutex_lock(&nf_ct_proto_mutex);
372 BUG_ON(rcu_dereference_protected(
373 nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
374 lockdep_is_held(&nf_ct_proto_mutex)
375 ) != l4proto);
376 rcu_assign_pointer(nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
377 &nf_conntrack_l4proto_generic);
378 mutex_unlock(&nf_ct_proto_mutex);
379
380 synchronize_rcu();
381 }
382 EXPORT_SYMBOL_GPL(nf_ct_l4proto_unregister_one);
383
384 void nf_ct_l4proto_pernet_unregister_one(struct net *net,
385 struct nf_conntrack_l4proto *l4proto)
386 {
387 struct nf_proto_net *pn = NULL;
388
389 pn = nf_ct_l4proto_net(net, l4proto);
390 if (pn == NULL)
391 return;
392
393 pn->users--;
394 nf_ct_l4proto_unregister_sysctl(net, pn, l4proto);
395
396 /* Remove all contrack entries for this protocol */
397 nf_ct_iterate_cleanup(net, kill_l4proto, l4proto, 0, 0);
398 }
399 EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_unregister_one);
400
401 int nf_ct_l4proto_register(struct nf_conntrack_l4proto *l4proto[],
402 unsigned int num_proto)
403 {
404 int ret = -EINVAL, ver;
405 unsigned int i;
406
407 for (i = 0; i < num_proto; i++) {
408 ret = nf_ct_l4proto_register_one(l4proto[i]);
409 if (ret < 0)
410 break;
411 }
412 if (i != num_proto) {
413 ver = l4proto[i]->l3proto == PF_INET6 ? 6 : 4;
414 pr_err("nf_conntrack_ipv%d: can't register %s%d proto.\n",
415 ver, l4proto[i]->name, ver);
416 nf_ct_l4proto_unregister(l4proto, i);
417 }
418 return ret;
419 }
420 EXPORT_SYMBOL_GPL(nf_ct_l4proto_register);
421
422 int nf_ct_l4proto_pernet_register(struct net *net,
423 struct nf_conntrack_l4proto *l4proto[],
424 unsigned int num_proto)
425 {
426 int ret = -EINVAL;
427 unsigned int i;
428
429 for (i = 0; i < num_proto; i++) {
430 ret = nf_ct_l4proto_pernet_register_one(net, l4proto[i]);
431 if (ret < 0)
432 break;
433 }
434 if (i != num_proto) {
435 pr_err("nf_conntrack_%s%d: pernet registration failed\n",
436 l4proto[i]->name,
437 l4proto[i]->l3proto == PF_INET6 ? 6 : 4);
438 nf_ct_l4proto_pernet_unregister(net, l4proto, i);
439 }
440 return ret;
441 }
442 EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_register);
443
444 void nf_ct_l4proto_unregister(struct nf_conntrack_l4proto *l4proto[],
445 unsigned int num_proto)
446 {
447 while (num_proto-- != 0)
448 nf_ct_l4proto_unregister_one(l4proto[num_proto]);
449 }
450 EXPORT_SYMBOL_GPL(nf_ct_l4proto_unregister);
451
452 void nf_ct_l4proto_pernet_unregister(struct net *net,
453 struct nf_conntrack_l4proto *l4proto[],
454 unsigned int num_proto)
455 {
456 while (num_proto-- != 0)
457 nf_ct_l4proto_pernet_unregister_one(net, l4proto[num_proto]);
458 }
459 EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_unregister);
460
461 int nf_conntrack_proto_pernet_init(struct net *net)
462 {
463 int err;
464 struct nf_proto_net *pn = nf_ct_l4proto_net(net,
465 &nf_conntrack_l4proto_generic);
466
467 err = nf_conntrack_l4proto_generic.init_net(net,
468 nf_conntrack_l4proto_generic.l3proto);
469 if (err < 0)
470 return err;
471 err = nf_ct_l4proto_register_sysctl(net,
472 pn,
473 &nf_conntrack_l4proto_generic);
474 if (err < 0)
475 return err;
476
477 pn->users++;
478 return 0;
479 }
480
481 void nf_conntrack_proto_pernet_fini(struct net *net)
482 {
483 struct nf_proto_net *pn = nf_ct_l4proto_net(net,
484 &nf_conntrack_l4proto_generic);
485
486 pn->users--;
487 nf_ct_l4proto_unregister_sysctl(net,
488 pn,
489 &nf_conntrack_l4proto_generic);
490 }
491
492 int nf_conntrack_proto_init(void)
493 {
494 unsigned int i;
495 for (i = 0; i < AF_MAX; i++)
496 rcu_assign_pointer(nf_ct_l3protos[i],
497 &nf_conntrack_l3proto_generic);
498 return 0;
499 }
500
501 void nf_conntrack_proto_fini(void)
502 {
503 unsigned int i;
504 /* free l3proto protocol tables */
505 for (i = 0; i < PF_MAX; i++)
506 kfree(nf_ct_protos[i]);
507 }