Merge branch 'drm-intel-fixes' of git://people.freedesktop.org/~danvet/drm-intel...
[deliverable/linux.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>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#include <linux/types.h>
13#include <linux/netfilter.h>
14#include <linux/module.h>
5a0e3ad6 15#include <linux/slab.h>
d62f9ed4 16#include <linux/mutex.h>
8f03dea5
MJ
17#include <linux/vmalloc.h>
18#include <linux/stddef.h>
19#include <linux/err.h>
20#include <linux/percpu.h>
8f03dea5
MJ
21#include <linux/notifier.h>
22#include <linux/kernel.h>
23#include <linux/netdevice.h>
24
25#include <net/netfilter/nf_conntrack.h>
26#include <net/netfilter/nf_conntrack_l3proto.h>
605dcad6 27#include <net/netfilter/nf_conntrack_l4proto.h>
8f03dea5
MJ
28#include <net/netfilter/nf_conntrack_core.h>
29
0906a372
AB
30static struct nf_conntrack_l4proto __rcu **nf_ct_protos[PF_MAX] __read_mostly;
31struct nf_conntrack_l3proto __rcu *nf_ct_l3protos[AF_MAX] __read_mostly;
13b18339 32EXPORT_SYMBOL_GPL(nf_ct_l3protos);
8f03dea5 33
b19caa0c 34static DEFINE_MUTEX(nf_ct_proto_mutex);
d62f9ed4 35
b19caa0c 36#ifdef CONFIG_SYSCTL
d62f9ed4 37static int
2c352f44
G
38nf_ct_register_sysctl(struct net *net,
39 struct ctl_table_header **header,
40 const char *path,
fa34fff5 41 struct ctl_table *table)
d62f9ed4
PM
42{
43 if (*header == NULL) {
2c352f44 44 *header = register_net_sysctl(net, path, table);
d62f9ed4
PM
45 if (*header == NULL)
46 return -ENOMEM;
47 }
2c352f44 48
d62f9ed4
PM
49 return 0;
50}
51
52static void
53nf_ct_unregister_sysctl(struct ctl_table_header **header,
2c352f44 54 struct ctl_table **table,
fa34fff5 55 unsigned int users)
d62f9ed4 56{
fa34fff5 57 if (users > 0)
d62f9ed4 58 return;
b3fd3ffe 59
5dd3df10 60 unregister_net_sysctl_table(*header);
2c352f44 61 kfree(*table);
d62f9ed4 62 *header = NULL;
2c352f44 63 *table = NULL;
d62f9ed4
PM
64}
65#endif
66
605dcad6
MJ
67struct nf_conntrack_l4proto *
68__nf_ct_l4proto_find(u_int16_t l3proto, u_int8_t l4proto)
8f03dea5
MJ
69{
70 if (unlikely(l3proto >= AF_MAX || nf_ct_protos[l3proto] == NULL))
605dcad6 71 return &nf_conntrack_l4proto_generic;
8f03dea5 72
923f4902 73 return rcu_dereference(nf_ct_protos[l3proto][l4proto]);
8f03dea5 74}
13b18339 75EXPORT_SYMBOL_GPL(__nf_ct_l4proto_find);
8f03dea5
MJ
76
77/* this is guaranteed to always return a valid protocol helper, since
78 * it falls back to generic_protocol */
8f03dea5
MJ
79struct nf_conntrack_l3proto *
80nf_ct_l3proto_find_get(u_int16_t l3proto)
81{
82 struct nf_conntrack_l3proto *p;
83
923f4902 84 rcu_read_lock();
8f03dea5
MJ
85 p = __nf_ct_l3proto_find(l3proto);
86 if (!try_module_get(p->me))
605dcad6 87 p = &nf_conntrack_l3proto_generic;
923f4902 88 rcu_read_unlock();
8f03dea5
MJ
89
90 return p;
91}
13b18339 92EXPORT_SYMBOL_GPL(nf_ct_l3proto_find_get);
8f03dea5
MJ
93
94void nf_ct_l3proto_put(struct nf_conntrack_l3proto *p)
95{
96 module_put(p->me);
97}
13b18339 98EXPORT_SYMBOL_GPL(nf_ct_l3proto_put);
8f03dea5
MJ
99
100int
101nf_ct_l3proto_try_module_get(unsigned short l3proto)
102{
103 int ret;
104 struct nf_conntrack_l3proto *p;
105
106retry: p = nf_ct_l3proto_find_get(l3proto);
605dcad6 107 if (p == &nf_conntrack_l3proto_generic) {
8f03dea5
MJ
108 ret = request_module("nf_conntrack-%d", l3proto);
109 if (!ret)
110 goto retry;
111
112 return -EPROTOTYPE;
113 }
114
115 return 0;
116}
13b18339 117EXPORT_SYMBOL_GPL(nf_ct_l3proto_try_module_get);
8f03dea5
MJ
118
119void nf_ct_l3proto_module_put(unsigned short l3proto)
120{
121 struct nf_conntrack_l3proto *p;
122
3b254c54
PM
123 /* rcu_read_lock not necessary since the caller holds a reference, but
124 * taken anyways to avoid lockdep warnings in __nf_ct_l3proto_find()
125 */
126 rcu_read_lock();
8f03dea5 127 p = __nf_ct_l3proto_find(l3proto);
8f03dea5 128 module_put(p->me);
3b254c54 129 rcu_read_unlock();
8f03dea5 130}
13b18339 131EXPORT_SYMBOL_GPL(nf_ct_l3proto_module_put);
8f03dea5 132
c1ebd7df
PNA
133struct nf_conntrack_l4proto *
134nf_ct_l4proto_find_get(u_int16_t l3num, u_int8_t l4num)
135{
136 struct nf_conntrack_l4proto *p;
137
138 rcu_read_lock();
139 p = __nf_ct_l4proto_find(l3num, l4num);
140 if (!try_module_get(p->me))
141 p = &nf_conntrack_l4proto_generic;
142 rcu_read_unlock();
143
144 return p;
145}
146EXPORT_SYMBOL_GPL(nf_ct_l4proto_find_get);
147
148void nf_ct_l4proto_put(struct nf_conntrack_l4proto *p)
149{
150 module_put(p->me);
151}
152EXPORT_SYMBOL_GPL(nf_ct_l4proto_put);
153
8f03dea5
MJ
154static int kill_l3proto(struct nf_conn *i, void *data)
155{
5e8fbe2a 156 return nf_ct_l3num(i) == ((struct nf_conntrack_l3proto *)data)->l3proto;
8f03dea5
MJ
157}
158
605dcad6 159static int kill_l4proto(struct nf_conn *i, void *data)
8f03dea5 160{
605dcad6
MJ
161 struct nf_conntrack_l4proto *l4proto;
162 l4proto = (struct nf_conntrack_l4proto *)data;
5e8fbe2a
PM
163 return nf_ct_protonum(i) == l4proto->l4proto &&
164 nf_ct_l3num(i) == l4proto->l3proto;
8f03dea5
MJ
165}
166
524a53e5
G
167static struct nf_ip_net *nf_ct_l3proto_net(struct net *net,
168 struct nf_conntrack_l3proto *l3proto)
d62f9ed4 169{
524a53e5
G
170 if (l3proto->l3proto == PF_INET)
171 return &net->ct.nf_ct_proto;
172 else
173 return NULL;
174}
d62f9ed4 175
524a53e5
G
176static int nf_ct_l3proto_register_sysctl(struct net *net,
177 struct nf_conntrack_l3proto *l3proto)
178{
179 int err = 0;
180 struct nf_ip_net *in = nf_ct_l3proto_net(net, l3proto);
181 /* nf_conntrack_l3proto_ipv6 doesn't support sysctl */
182 if (in == NULL)
183 return 0;
184
185#if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
186 if (in->ctl_table != NULL) {
187 err = nf_ct_register_sysctl(net,
188 &in->ctl_table_header,
d62f9ed4 189 l3proto->ctl_table_path,
fa34fff5 190 in->ctl_table);
524a53e5
G
191 if (err < 0) {
192 kfree(in->ctl_table);
193 in->ctl_table = NULL;
194 }
d62f9ed4 195 }
d62f9ed4
PM
196#endif
197 return err;
198}
199
524a53e5
G
200static void nf_ct_l3proto_unregister_sysctl(struct net *net,
201 struct nf_conntrack_l3proto *l3proto)
d62f9ed4 202{
524a53e5
G
203 struct nf_ip_net *in = nf_ct_l3proto_net(net, l3proto);
204
205 if (in == NULL)
206 return;
207#if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
208 if (in->ctl_table_header != NULL)
209 nf_ct_unregister_sysctl(&in->ctl_table_header,
210 &in->ctl_table,
fa34fff5 211 0);
d62f9ed4
PM
212#endif
213}
214
524a53e5
G
215static int
216nf_conntrack_l3proto_register_net(struct nf_conntrack_l3proto *proto)
8f03dea5
MJ
217{
218 int ret = 0;
0e60ebe0 219 struct nf_conntrack_l3proto *old;
8f03dea5 220
0661cca9
PM
221 if (proto->l3proto >= AF_MAX)
222 return -EBUSY;
ae5718fb 223
d0dba725
HE
224 if (proto->tuple_to_nlattr && !proto->nlattr_tuple_size)
225 return -EINVAL;
226
b19caa0c 227 mutex_lock(&nf_ct_proto_mutex);
0e60ebe0
ED
228 old = rcu_dereference_protected(nf_ct_l3protos[proto->l3proto],
229 lockdep_is_held(&nf_ct_proto_mutex));
230 if (old != &nf_conntrack_l3proto_generic) {
8f03dea5 231 ret = -EBUSY;
ae5718fb 232 goto out_unlock;
8f03dea5 233 }
d62f9ed4 234
d0dba725
HE
235 if (proto->nlattr_tuple_size)
236 proto->nla_size = 3 * proto->nlattr_tuple_size();
237
0661cca9 238 rcu_assign_pointer(nf_ct_l3protos[proto->l3proto], proto);
8f03dea5 239
ae5718fb 240out_unlock:
b19caa0c 241 mutex_unlock(&nf_ct_proto_mutex);
8f03dea5 242 return ret;
524a53e5 243
8f03dea5
MJ
244}
245
524a53e5
G
246int nf_conntrack_l3proto_register(struct net *net,
247 struct nf_conntrack_l3proto *proto)
8f03dea5 248{
524a53e5
G
249 int ret = 0;
250
fa0f61f0
G
251 if (proto->init_net) {
252 ret = proto->init_net(net);
253 if (ret < 0)
254 return ret;
255 }
524a53e5 256
fa0f61f0 257 ret = nf_ct_l3proto_register_sysctl(net, proto);
524a53e5
G
258 if (ret < 0)
259 return ret;
260
fa0f61f0
G
261 if (net == &init_net) {
262 ret = nf_conntrack_l3proto_register_net(proto);
524a53e5 263 if (ret < 0)
fa0f61f0 264 nf_ct_l3proto_unregister_sysctl(net, proto);
524a53e5 265 }
fa0f61f0
G
266
267 return ret;
524a53e5
G
268}
269EXPORT_SYMBOL_GPL(nf_conntrack_l3proto_register);
678d6675 270
524a53e5
G
271static void
272nf_conntrack_l3proto_unregister_net(struct nf_conntrack_l3proto *proto)
273{
fe3eb20c 274 BUG_ON(proto->l3proto >= AF_MAX);
ae5718fb 275
b19caa0c 276 mutex_lock(&nf_ct_proto_mutex);
0e60ebe0
ED
277 BUG_ON(rcu_dereference_protected(nf_ct_l3protos[proto->l3proto],
278 lockdep_is_held(&nf_ct_proto_mutex)
279 ) != proto);
923f4902
PM
280 rcu_assign_pointer(nf_ct_l3protos[proto->l3proto],
281 &nf_conntrack_l3proto_generic);
b19caa0c 282 mutex_unlock(&nf_ct_proto_mutex);
8f03dea5 283
0661cca9 284 synchronize_rcu();
524a53e5
G
285}
286
287void nf_conntrack_l3proto_unregister(struct net *net,
288 struct nf_conntrack_l3proto *proto)
289{
290 if (net == &init_net)
291 nf_conntrack_l3proto_unregister_net(proto);
292
293 nf_ct_l3proto_unregister_sysctl(net, proto);
d62f9ed4 294
8f03dea5 295 /* Remove all contrack entries for this protocol */
524a53e5 296 nf_ct_iterate_cleanup(net, kill_l3proto, proto);
8f03dea5 297}
13b18339 298EXPORT_SYMBOL_GPL(nf_conntrack_l3proto_unregister);
8f03dea5 299
2c352f44
G
300static struct nf_proto_net *nf_ct_l4proto_net(struct net *net,
301 struct nf_conntrack_l4proto *l4proto)
302{
08911475
PNA
303 if (l4proto->get_net_proto) {
304 /* statically built-in protocols use static per-net */
305 return l4proto->get_net_proto(net);
306 } else if (l4proto->net_id) {
307 /* ... and loadable protocols use dynamic per-net */
308 return net_generic(net, *l4proto->net_id);
15f585bd
G
309 }
310 return NULL;
2c352f44
G
311}
312
313static
314int nf_ct_l4proto_register_sysctl(struct net *net,
fa34fff5 315 struct nf_proto_net *pn,
2c352f44 316 struct nf_conntrack_l4proto *l4proto)
d62f9ed4
PM
317{
318 int err = 0;
319
320#ifdef CONFIG_SYSCTL
2c352f44
G
321 if (pn->ctl_table != NULL) {
322 err = nf_ct_register_sysctl(net,
323 &pn->ctl_table_header,
f99e8f71 324 "net/netfilter",
fa34fff5 325 pn->ctl_table);
2c352f44
G
326 if (err < 0) {
327 if (!pn->users) {
328 kfree(pn->ctl_table);
329 pn->ctl_table = NULL;
330 }
2c352f44 331 }
d62f9ed4 332 }
a999e683 333#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
2c352f44 334 if (l4proto->l3proto != AF_INET6 && pn->ctl_compat_table != NULL) {
12c26df3
G
335 if (err < 0) {
336 nf_ct_kfree_compat_sysctl_table(pn);
337 goto out;
338 }
2c352f44
G
339 err = nf_ct_register_sysctl(net,
340 &pn->ctl_compat_header,
f99e8f71 341 "net/ipv4/netfilter",
fa34fff5 342 pn->ctl_compat_table);
a999e683
PM
343 if (err == 0)
344 goto out;
2c352f44 345
f28997e2 346 nf_ct_kfree_compat_sysctl_table(pn);
2c352f44
G
347 nf_ct_unregister_sysctl(&pn->ctl_table_header,
348 &pn->ctl_table,
fa34fff5 349 pn->users);
a999e683 350 }
a999e683 351out:
12c26df3 352#endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
933a41e7 353#endif /* CONFIG_SYSCTL */
d62f9ed4
PM
354 return err;
355}
356
2c352f44
G
357static
358void nf_ct_l4proto_unregister_sysctl(struct net *net,
fa34fff5 359 struct nf_proto_net *pn,
2c352f44 360 struct nf_conntrack_l4proto *l4proto)
d62f9ed4
PM
361{
362#ifdef CONFIG_SYSCTL
2c352f44
G
363 if (pn->ctl_table_header != NULL)
364 nf_ct_unregister_sysctl(&pn->ctl_table_header,
365 &pn->ctl_table,
fa34fff5 366 pn->users);
2c352f44 367
a999e683 368#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
2c352f44
G
369 if (l4proto->l3proto != AF_INET6 && pn->ctl_compat_header != NULL)
370 nf_ct_unregister_sysctl(&pn->ctl_compat_header,
371 &pn->ctl_compat_table,
fa34fff5 372 0);
a999e683 373#endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
933a41e7 374#endif /* CONFIG_SYSCTL */
d62f9ed4
PM
375}
376
8f03dea5
MJ
377/* FIXME: Allow NULL functions and sub in pointers to generic for
378 them. --RR */
2c352f44
G
379static int
380nf_conntrack_l4proto_register_net(struct nf_conntrack_l4proto *l4proto)
8f03dea5
MJ
381{
382 int ret = 0;
383
0661cca9
PM
384 if (l4proto->l3proto >= PF_MAX)
385 return -EBUSY;
ae5718fb 386
d0dba725
HE
387 if ((l4proto->to_nlattr && !l4proto->nlattr_size)
388 || (l4proto->tuple_to_nlattr && !l4proto->nlattr_tuple_size))
389 return -EINVAL;
390
b19caa0c 391 mutex_lock(&nf_ct_proto_mutex);
c6a1e615 392 if (!nf_ct_protos[l4proto->l3proto]) {
8f03dea5 393 /* l3proto may be loaded latter. */
c5d277d2 394 struct nf_conntrack_l4proto __rcu **proto_array;
8f03dea5
MJ
395 int i;
396
c6a1e615
PM
397 proto_array = kmalloc(MAX_NF_CT_PROTO *
398 sizeof(struct nf_conntrack_l4proto *),
399 GFP_KERNEL);
8f03dea5
MJ
400 if (proto_array == NULL) {
401 ret = -ENOMEM;
b19caa0c 402 goto out_unlock;
8f03dea5 403 }
c6a1e615 404
8f03dea5 405 for (i = 0; i < MAX_NF_CT_PROTO; i++)
c5d277d2 406 RCU_INIT_POINTER(proto_array[i], &nf_conntrack_l4proto_generic);
d817d29d
ED
407
408 /* Before making proto_array visible to lockless readers,
409 * we must make sure its content is committed to memory.
410 */
411 smp_wmb();
412
c6a1e615 413 nf_ct_protos[l4proto->l3proto] = proto_array;
0e60ebe0
ED
414 } else if (rcu_dereference_protected(
415 nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
416 lockdep_is_held(&nf_ct_proto_mutex)
417 ) != &nf_conntrack_l4proto_generic) {
c6a1e615
PM
418 ret = -EBUSY;
419 goto out_unlock;
8f03dea5
MJ
420 }
421
d0dba725
HE
422 l4proto->nla_size = 0;
423 if (l4proto->nlattr_size)
424 l4proto->nla_size += l4proto->nlattr_size();
425 if (l4proto->nlattr_tuple_size)
426 l4proto->nla_size += 3 * l4proto->nlattr_tuple_size();
427
c6a1e615
PM
428 rcu_assign_pointer(nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
429 l4proto);
8f03dea5 430out_unlock:
b19caa0c 431 mutex_unlock(&nf_ct_proto_mutex);
8f03dea5
MJ
432 return ret;
433}
434
2c352f44
G
435int nf_conntrack_l4proto_register(struct net *net,
436 struct nf_conntrack_l4proto *l4proto)
8f03dea5 437{
2c352f44 438 int ret = 0;
fa34fff5 439 struct nf_proto_net *pn = NULL;
2c352f44 440
fa0f61f0 441 if (l4proto->init_net) {
f1caad27 442 ret = l4proto->init_net(net, l4proto->l3proto);
fa0f61f0 443 if (ret < 0)
fa34fff5 444 goto out;
fa0f61f0 445 }
678d6675 446
fa34fff5
G
447 pn = nf_ct_l4proto_net(net, l4proto);
448 if (pn == NULL)
449 goto out;
450
451 ret = nf_ct_l4proto_register_sysctl(net, pn, l4proto);
2c352f44 452 if (ret < 0)
fa34fff5 453 goto out;
2c352f44 454
fa0f61f0
G
455 if (net == &init_net) {
456 ret = nf_conntrack_l4proto_register_net(l4proto);
fa34fff5
G
457 if (ret < 0) {
458 nf_ct_l4proto_unregister_sysctl(net, pn, l4proto);
459 goto out;
460 }
fa0f61f0
G
461 }
462
fa34fff5
G
463 pn->users++;
464out:
fa0f61f0 465 return ret;
2c352f44
G
466}
467EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_register);
468
469static void
470nf_conntrack_l4proto_unregister_net(struct nf_conntrack_l4proto *l4proto)
471{
fe3eb20c 472 BUG_ON(l4proto->l3proto >= PF_MAX);
ae5718fb 473
b19caa0c 474 mutex_lock(&nf_ct_proto_mutex);
0e60ebe0
ED
475 BUG_ON(rcu_dereference_protected(
476 nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
477 lockdep_is_held(&nf_ct_proto_mutex)
478 ) != l4proto);
923f4902
PM
479 rcu_assign_pointer(nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
480 &nf_conntrack_l4proto_generic);
b19caa0c 481 mutex_unlock(&nf_ct_proto_mutex);
8f03dea5 482
0661cca9 483 synchronize_rcu();
2c352f44 484}
d62f9ed4 485
2c352f44
G
486void nf_conntrack_l4proto_unregister(struct net *net,
487 struct nf_conntrack_l4proto *l4proto)
488{
fa34fff5
G
489 struct nf_proto_net *pn = NULL;
490
2c352f44
G
491 if (net == &init_net)
492 nf_conntrack_l4proto_unregister_net(l4proto);
493
fa34fff5
G
494 pn = nf_ct_l4proto_net(net, l4proto);
495 if (pn == NULL)
496 return;
497
498 pn->users--;
499 nf_ct_l4proto_unregister_sysctl(net, pn, l4proto);
500
8f03dea5 501 /* Remove all contrack entries for this protocol */
2c352f44 502 nf_ct_iterate_cleanup(net, kill_l4proto, l4proto);
8f03dea5 503}
13b18339 504EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_unregister);
ac5357eb 505
15f585bd 506int nf_conntrack_proto_init(struct net *net)
ac5357eb
PM
507{
508 unsigned int i;
509 int err;
fa34fff5
G
510 struct nf_proto_net *pn = nf_ct_l4proto_net(net,
511 &nf_conntrack_l4proto_generic);
512
f1caad27
G
513 err = nf_conntrack_l4proto_generic.init_net(net,
514 nf_conntrack_l4proto_generic.l3proto);
15f585bd
G
515 if (err < 0)
516 return err;
517 err = nf_ct_l4proto_register_sysctl(net,
fa34fff5 518 pn,
15f585bd 519 &nf_conntrack_l4proto_generic);
ac5357eb
PM
520 if (err < 0)
521 return err;
522
15f585bd
G
523 if (net == &init_net) {
524 for (i = 0; i < AF_MAX; i++)
525 rcu_assign_pointer(nf_ct_l3protos[i],
526 &nf_conntrack_l3proto_generic);
527 }
fa34fff5
G
528
529 pn->users++;
ac5357eb
PM
530 return 0;
531}
532
15f585bd 533void nf_conntrack_proto_fini(struct net *net)
ac5357eb
PM
534{
535 unsigned int i;
fa34fff5
G
536 struct nf_proto_net *pn = nf_ct_l4proto_net(net,
537 &nf_conntrack_l4proto_generic);
538
539 pn->users--;
15f585bd 540 nf_ct_l4proto_unregister_sysctl(net,
fa34fff5 541 pn,
15f585bd
G
542 &nf_conntrack_l4proto_generic);
543 if (net == &init_net) {
544 /* free l3proto protocol tables */
545 for (i = 0; i < PF_MAX; i++)
546 kfree(nf_ct_protos[i]);
547 }
ac5357eb 548}
This page took 0.528243 seconds and 5 git commands to generate.