ath10k: remove htt rx amsdu clear retry bit hack
[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>
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
MJ
94
95void nf_ct_l3proto_put(struct nf_conntrack_l3proto *p)
96{
97 module_put(p->me);
98}
13b18339 99EXPORT_SYMBOL_GPL(nf_ct_l3proto_put);
8f03dea5
MJ
100
101int
102nf_ct_l3proto_try_module_get(unsigned short l3proto)
103{
104 int ret;
105 struct nf_conntrack_l3proto *p;
106
107retry: p = nf_ct_l3proto_find_get(l3proto);
605dcad6 108 if (p == &nf_conntrack_l3proto_generic) {
8f03dea5
MJ
109 ret = request_module("nf_conntrack-%d", l3proto);
110 if (!ret)
111 goto retry;
112
113 return -EPROTOTYPE;
114 }
115
116 return 0;
117}
13b18339 118EXPORT_SYMBOL_GPL(nf_ct_l3proto_try_module_get);
8f03dea5
MJ
119
120void nf_ct_l3proto_module_put(unsigned short l3proto)
121{
122 struct nf_conntrack_l3proto *p;
123
3b254c54
PM
124 /* rcu_read_lock not necessary since the caller holds a reference, but
125 * taken anyways to avoid lockdep warnings in __nf_ct_l3proto_find()
126 */
127 rcu_read_lock();
8f03dea5 128 p = __nf_ct_l3proto_find(l3proto);
8f03dea5 129 module_put(p->me);
3b254c54 130 rcu_read_unlock();
8f03dea5 131}
13b18339 132EXPORT_SYMBOL_GPL(nf_ct_l3proto_module_put);
8f03dea5 133
c1ebd7df
PNA
134struct nf_conntrack_l4proto *
135nf_ct_l4proto_find_get(u_int16_t l3num, u_int8_t l4num)
136{
137 struct nf_conntrack_l4proto *p;
138
139 rcu_read_lock();
140 p = __nf_ct_l4proto_find(l3num, l4num);
141 if (!try_module_get(p->me))
142 p = &nf_conntrack_l4proto_generic;
143 rcu_read_unlock();
144
145 return p;
146}
147EXPORT_SYMBOL_GPL(nf_ct_l4proto_find_get);
148
149void nf_ct_l4proto_put(struct nf_conntrack_l4proto *p)
150{
151 module_put(p->me);
152}
153EXPORT_SYMBOL_GPL(nf_ct_l4proto_put);
154
8f03dea5
MJ
155static int kill_l3proto(struct nf_conn *i, void *data)
156{
5e8fbe2a 157 return nf_ct_l3num(i) == ((struct nf_conntrack_l3proto *)data)->l3proto;
8f03dea5
MJ
158}
159
605dcad6 160static int kill_l4proto(struct nf_conn *i, void *data)
8f03dea5 161{
605dcad6
MJ
162 struct nf_conntrack_l4proto *l4proto;
163 l4proto = (struct nf_conntrack_l4proto *)data;
5e8fbe2a
PM
164 return nf_ct_protonum(i) == l4proto->l4proto &&
165 nf_ct_l3num(i) == l4proto->l3proto;
8f03dea5
MJ
166}
167
524a53e5
G
168static struct nf_ip_net *nf_ct_l3proto_net(struct net *net,
169 struct nf_conntrack_l3proto *l3proto)
d62f9ed4 170{
524a53e5
G
171 if (l3proto->l3proto == PF_INET)
172 return &net->ct.nf_ct_proto;
173 else
174 return NULL;
175}
d62f9ed4 176
524a53e5
G
177static int nf_ct_l3proto_register_sysctl(struct net *net,
178 struct nf_conntrack_l3proto *l3proto)
179{
180 int err = 0;
181 struct nf_ip_net *in = nf_ct_l3proto_net(net, l3proto);
182 /* nf_conntrack_l3proto_ipv6 doesn't support sysctl */
183 if (in == NULL)
184 return 0;
185
186#if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
187 if (in->ctl_table != NULL) {
188 err = nf_ct_register_sysctl(net,
189 &in->ctl_table_header,
d62f9ed4 190 l3proto->ctl_table_path,
fa34fff5 191 in->ctl_table);
524a53e5
G
192 if (err < 0) {
193 kfree(in->ctl_table);
194 in->ctl_table = NULL;
195 }
d62f9ed4 196 }
d62f9ed4
PM
197#endif
198 return err;
199}
200
524a53e5
G
201static void nf_ct_l3proto_unregister_sysctl(struct net *net,
202 struct nf_conntrack_l3proto *l3proto)
d62f9ed4 203{
524a53e5
G
204 struct nf_ip_net *in = nf_ct_l3proto_net(net, l3proto);
205
206 if (in == NULL)
207 return;
208#if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
209 if (in->ctl_table_header != NULL)
210 nf_ct_unregister_sysctl(&in->ctl_table_header,
211 &in->ctl_table,
fa34fff5 212 0);
d62f9ed4
PM
213#endif
214}
215
6330750d 216int nf_ct_l3proto_register(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 244}
6330750d 245EXPORT_SYMBOL_GPL(nf_ct_l3proto_register);
8f03dea5 246
6330750d 247int nf_ct_l3proto_pernet_register(struct net *net,
524a53e5 248 struct nf_conntrack_l3proto *proto)
8f03dea5 249{
524a53e5
G
250 int ret = 0;
251
fa0f61f0
G
252 if (proto->init_net) {
253 ret = proto->init_net(net);
254 if (ret < 0)
255 return ret;
256 }
524a53e5 257
6330750d 258 return nf_ct_l3proto_register_sysctl(net, proto);
524a53e5 259}
6330750d 260EXPORT_SYMBOL_GPL(nf_ct_l3proto_pernet_register);
678d6675 261
6330750d 262void nf_ct_l3proto_unregister(struct nf_conntrack_l3proto *proto)
524a53e5 263{
fe3eb20c 264 BUG_ON(proto->l3proto >= AF_MAX);
ae5718fb 265
b19caa0c 266 mutex_lock(&nf_ct_proto_mutex);
0e60ebe0
ED
267 BUG_ON(rcu_dereference_protected(nf_ct_l3protos[proto->l3proto],
268 lockdep_is_held(&nf_ct_proto_mutex)
269 ) != proto);
923f4902
PM
270 rcu_assign_pointer(nf_ct_l3protos[proto->l3proto],
271 &nf_conntrack_l3proto_generic);
b19caa0c 272 mutex_unlock(&nf_ct_proto_mutex);
8f03dea5 273
0661cca9 274 synchronize_rcu();
524a53e5 275}
6330750d 276EXPORT_SYMBOL_GPL(nf_ct_l3proto_unregister);
524a53e5 277
6330750d 278void nf_ct_l3proto_pernet_unregister(struct net *net,
524a53e5
G
279 struct nf_conntrack_l3proto *proto)
280{
524a53e5 281 nf_ct_l3proto_unregister_sysctl(net, proto);
d62f9ed4 282
8f03dea5 283 /* Remove all contrack entries for this protocol */
c655bc68 284 nf_ct_iterate_cleanup(net, kill_l3proto, proto, 0, 0);
8f03dea5 285}
6330750d 286EXPORT_SYMBOL_GPL(nf_ct_l3proto_pernet_unregister);
8f03dea5 287
2c352f44
G
288static struct nf_proto_net *nf_ct_l4proto_net(struct net *net,
289 struct nf_conntrack_l4proto *l4proto)
290{
08911475
PNA
291 if (l4proto->get_net_proto) {
292 /* statically built-in protocols use static per-net */
293 return l4proto->get_net_proto(net);
294 } else if (l4proto->net_id) {
295 /* ... and loadable protocols use dynamic per-net */
296 return net_generic(net, *l4proto->net_id);
15f585bd
G
297 }
298 return NULL;
2c352f44
G
299}
300
301static
302int nf_ct_l4proto_register_sysctl(struct net *net,
fa34fff5 303 struct nf_proto_net *pn,
2c352f44 304 struct nf_conntrack_l4proto *l4proto)
d62f9ed4
PM
305{
306 int err = 0;
307
308#ifdef CONFIG_SYSCTL
2c352f44
G
309 if (pn->ctl_table != NULL) {
310 err = nf_ct_register_sysctl(net,
311 &pn->ctl_table_header,
f99e8f71 312 "net/netfilter",
fa34fff5 313 pn->ctl_table);
2c352f44
G
314 if (err < 0) {
315 if (!pn->users) {
316 kfree(pn->ctl_table);
317 pn->ctl_table = NULL;
318 }
2c352f44 319 }
d62f9ed4 320 }
a999e683 321#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
2c352f44 322 if (l4proto->l3proto != AF_INET6 && pn->ctl_compat_table != NULL) {
12c26df3
G
323 if (err < 0) {
324 nf_ct_kfree_compat_sysctl_table(pn);
325 goto out;
326 }
2c352f44
G
327 err = nf_ct_register_sysctl(net,
328 &pn->ctl_compat_header,
f99e8f71 329 "net/ipv4/netfilter",
fa34fff5 330 pn->ctl_compat_table);
a999e683
PM
331 if (err == 0)
332 goto out;
2c352f44 333
f28997e2 334 nf_ct_kfree_compat_sysctl_table(pn);
2c352f44
G
335 nf_ct_unregister_sysctl(&pn->ctl_table_header,
336 &pn->ctl_table,
fa34fff5 337 pn->users);
a999e683 338 }
a999e683 339out:
12c26df3 340#endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
933a41e7 341#endif /* CONFIG_SYSCTL */
d62f9ed4
PM
342 return err;
343}
344
2c352f44
G
345static
346void nf_ct_l4proto_unregister_sysctl(struct net *net,
fa34fff5 347 struct nf_proto_net *pn,
2c352f44 348 struct nf_conntrack_l4proto *l4proto)
d62f9ed4
PM
349{
350#ifdef CONFIG_SYSCTL
2c352f44
G
351 if (pn->ctl_table_header != NULL)
352 nf_ct_unregister_sysctl(&pn->ctl_table_header,
353 &pn->ctl_table,
fa34fff5 354 pn->users);
2c352f44 355
a999e683 356#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
2c352f44
G
357 if (l4proto->l3proto != AF_INET6 && pn->ctl_compat_header != NULL)
358 nf_ct_unregister_sysctl(&pn->ctl_compat_header,
359 &pn->ctl_compat_table,
fa34fff5 360 0);
a999e683 361#endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
933a41e7 362#endif /* CONFIG_SYSCTL */
d62f9ed4
PM
363}
364
8f03dea5
MJ
365/* FIXME: Allow NULL functions and sub in pointers to generic for
366 them. --RR */
c296bb4d 367int nf_ct_l4proto_register(struct nf_conntrack_l4proto *l4proto)
8f03dea5
MJ
368{
369 int ret = 0;
370
0661cca9
PM
371 if (l4proto->l3proto >= PF_MAX)
372 return -EBUSY;
ae5718fb 373
d0dba725
HE
374 if ((l4proto->to_nlattr && !l4proto->nlattr_size)
375 || (l4proto->tuple_to_nlattr && !l4proto->nlattr_tuple_size))
376 return -EINVAL;
377
b19caa0c 378 mutex_lock(&nf_ct_proto_mutex);
c6a1e615 379 if (!nf_ct_protos[l4proto->l3proto]) {
8f03dea5 380 /* l3proto may be loaded latter. */
c5d277d2 381 struct nf_conntrack_l4proto __rcu **proto_array;
8f03dea5
MJ
382 int i;
383
c6a1e615
PM
384 proto_array = kmalloc(MAX_NF_CT_PROTO *
385 sizeof(struct nf_conntrack_l4proto *),
386 GFP_KERNEL);
8f03dea5
MJ
387 if (proto_array == NULL) {
388 ret = -ENOMEM;
b19caa0c 389 goto out_unlock;
8f03dea5 390 }
c6a1e615 391
8f03dea5 392 for (i = 0; i < MAX_NF_CT_PROTO; i++)
c5d277d2 393 RCU_INIT_POINTER(proto_array[i], &nf_conntrack_l4proto_generic);
d817d29d
ED
394
395 /* Before making proto_array visible to lockless readers,
396 * we must make sure its content is committed to memory.
397 */
398 smp_wmb();
399
c6a1e615 400 nf_ct_protos[l4proto->l3proto] = proto_array;
0e60ebe0
ED
401 } else if (rcu_dereference_protected(
402 nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
403 lockdep_is_held(&nf_ct_proto_mutex)
404 ) != &nf_conntrack_l4proto_generic) {
c6a1e615
PM
405 ret = -EBUSY;
406 goto out_unlock;
8f03dea5
MJ
407 }
408
d0dba725
HE
409 l4proto->nla_size = 0;
410 if (l4proto->nlattr_size)
411 l4proto->nla_size += l4proto->nlattr_size();
412 if (l4proto->nlattr_tuple_size)
413 l4proto->nla_size += 3 * l4proto->nlattr_tuple_size();
414
c6a1e615
PM
415 rcu_assign_pointer(nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
416 l4proto);
8f03dea5 417out_unlock:
b19caa0c 418 mutex_unlock(&nf_ct_proto_mutex);
8f03dea5
MJ
419 return ret;
420}
c296bb4d 421EXPORT_SYMBOL_GPL(nf_ct_l4proto_register);
8f03dea5 422
c296bb4d 423int nf_ct_l4proto_pernet_register(struct net *net,
2c352f44 424 struct nf_conntrack_l4proto *l4proto)
8f03dea5 425{
2c352f44 426 int ret = 0;
fa34fff5 427 struct nf_proto_net *pn = NULL;
2c352f44 428
fa0f61f0 429 if (l4proto->init_net) {
f1caad27 430 ret = l4proto->init_net(net, l4proto->l3proto);
fa0f61f0 431 if (ret < 0)
fa34fff5 432 goto out;
fa0f61f0 433 }
678d6675 434
fa34fff5
G
435 pn = nf_ct_l4proto_net(net, l4proto);
436 if (pn == NULL)
437 goto out;
438
439 ret = nf_ct_l4proto_register_sysctl(net, pn, l4proto);
2c352f44 440 if (ret < 0)
fa34fff5 441 goto out;
2c352f44 442
fa34fff5
G
443 pn->users++;
444out:
fa0f61f0 445 return ret;
2c352f44 446}
c296bb4d 447EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_register);
2c352f44 448
c296bb4d 449void nf_ct_l4proto_unregister(struct nf_conntrack_l4proto *l4proto)
2c352f44 450{
fe3eb20c 451 BUG_ON(l4proto->l3proto >= PF_MAX);
ae5718fb 452
b19caa0c 453 mutex_lock(&nf_ct_proto_mutex);
0e60ebe0
ED
454 BUG_ON(rcu_dereference_protected(
455 nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
456 lockdep_is_held(&nf_ct_proto_mutex)
457 ) != l4proto);
923f4902
PM
458 rcu_assign_pointer(nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
459 &nf_conntrack_l4proto_generic);
b19caa0c 460 mutex_unlock(&nf_ct_proto_mutex);
8f03dea5 461
0661cca9 462 synchronize_rcu();
2c352f44 463}
c296bb4d 464EXPORT_SYMBOL_GPL(nf_ct_l4proto_unregister);
d62f9ed4 465
c296bb4d 466void nf_ct_l4proto_pernet_unregister(struct net *net,
2c352f44
G
467 struct nf_conntrack_l4proto *l4proto)
468{
fa34fff5
G
469 struct nf_proto_net *pn = NULL;
470
fa34fff5
G
471 pn = nf_ct_l4proto_net(net, l4proto);
472 if (pn == NULL)
473 return;
474
475 pn->users--;
476 nf_ct_l4proto_unregister_sysctl(net, pn, l4proto);
477
8f03dea5 478 /* Remove all contrack entries for this protocol */
c655bc68 479 nf_ct_iterate_cleanup(net, kill_l4proto, l4proto, 0, 0);
8f03dea5 480}
c296bb4d 481EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_unregister);
ac5357eb 482
04d87001 483int nf_conntrack_proto_pernet_init(struct net *net)
ac5357eb 484{
ac5357eb 485 int err;
fa34fff5
G
486 struct nf_proto_net *pn = nf_ct_l4proto_net(net,
487 &nf_conntrack_l4proto_generic);
488
f1caad27
G
489 err = nf_conntrack_l4proto_generic.init_net(net,
490 nf_conntrack_l4proto_generic.l3proto);
15f585bd
G
491 if (err < 0)
492 return err;
493 err = nf_ct_l4proto_register_sysctl(net,
fa34fff5 494 pn,
15f585bd 495 &nf_conntrack_l4proto_generic);
ac5357eb
PM
496 if (err < 0)
497 return err;
498
fa34fff5 499 pn->users++;
ac5357eb
PM
500 return 0;
501}
502
04d87001 503void nf_conntrack_proto_pernet_fini(struct net *net)
ac5357eb 504{
fa34fff5
G
505 struct nf_proto_net *pn = nf_ct_l4proto_net(net,
506 &nf_conntrack_l4proto_generic);
507
508 pn->users--;
15f585bd 509 nf_ct_l4proto_unregister_sysctl(net,
fa34fff5 510 pn,
15f585bd 511 &nf_conntrack_l4proto_generic);
04d87001
G
512}
513
514int nf_conntrack_proto_init(void)
515{
516 unsigned int i;
517 for (i = 0; i < AF_MAX; i++)
518 rcu_assign_pointer(nf_ct_l3protos[i],
519 &nf_conntrack_l3proto_generic);
520 return 0;
521}
522
523void nf_conntrack_proto_fini(void)
524{
525 unsigned int i;
526 /* free l3proto protocol tables */
527 for (i = 0; i < PF_MAX; i++)
528 kfree(nf_ct_protos[i]);
ac5357eb 529}
This page took 0.530397 seconds and 5 git commands to generate.