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