1 /* L3/L4 protocol support for nf_conntrack. */
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>
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.
12 #include <linux/types.h>
13 #include <linux/netfilter.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/mutex.h>
17 #include <linux/vmalloc.h>
18 #include <linux/stddef.h>
19 #include <linux/err.h>
20 #include <linux/percpu.h>
21 #include <linux/notifier.h>
22 #include <linux/kernel.h>
23 #include <linux/netdevice.h>
25 #include <net/netfilter/nf_conntrack.h>
26 #include <net/netfilter/nf_conntrack_l3proto.h>
27 #include <net/netfilter/nf_conntrack_l4proto.h>
28 #include <net/netfilter/nf_conntrack_core.h>
30 static struct nf_conntrack_l4proto __rcu
**nf_ct_protos
[PF_MAX
] __read_mostly
;
31 struct nf_conntrack_l3proto __rcu
*nf_ct_l3protos
[AF_MAX
] __read_mostly
;
32 EXPORT_SYMBOL_GPL(nf_ct_l3protos
);
34 static DEFINE_MUTEX(nf_ct_proto_mutex
);
38 nf_ct_register_sysctl(struct net
*net
,
39 struct ctl_table_header
**header
,
41 struct ctl_table
*table
)
43 if (*header
== NULL
) {
44 *header
= register_net_sysctl(net
, path
, table
);
53 nf_ct_unregister_sysctl(struct ctl_table_header
**header
,
54 struct ctl_table
**table
,
60 unregister_net_sysctl_table(*header
);
67 struct nf_conntrack_l4proto
*
68 __nf_ct_l4proto_find(u_int16_t l3proto
, u_int8_t l4proto
)
70 if (unlikely(l3proto
>= AF_MAX
|| nf_ct_protos
[l3proto
] == NULL
))
71 return &nf_conntrack_l4proto_generic
;
73 return rcu_dereference(nf_ct_protos
[l3proto
][l4proto
]);
75 EXPORT_SYMBOL_GPL(__nf_ct_l4proto_find
);
77 /* this is guaranteed to always return a valid protocol helper, since
78 * it falls back to generic_protocol */
79 struct nf_conntrack_l3proto
*
80 nf_ct_l3proto_find_get(u_int16_t l3proto
)
82 struct nf_conntrack_l3proto
*p
;
85 p
= __nf_ct_l3proto_find(l3proto
);
86 if (!try_module_get(p
->me
))
87 p
= &nf_conntrack_l3proto_generic
;
92 EXPORT_SYMBOL_GPL(nf_ct_l3proto_find_get
);
94 void nf_ct_l3proto_put(struct nf_conntrack_l3proto
*p
)
98 EXPORT_SYMBOL_GPL(nf_ct_l3proto_put
);
101 nf_ct_l3proto_try_module_get(unsigned short l3proto
)
104 struct nf_conntrack_l3proto
*p
;
106 retry
: p
= nf_ct_l3proto_find_get(l3proto
);
107 if (p
== &nf_conntrack_l3proto_generic
) {
108 ret
= request_module("nf_conntrack-%d", l3proto
);
117 EXPORT_SYMBOL_GPL(nf_ct_l3proto_try_module_get
);
119 void nf_ct_l3proto_module_put(unsigned short l3proto
)
121 struct nf_conntrack_l3proto
*p
;
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()
127 p
= __nf_ct_l3proto_find(l3proto
);
131 EXPORT_SYMBOL_GPL(nf_ct_l3proto_module_put
);
133 struct nf_conntrack_l4proto
*
134 nf_ct_l4proto_find_get(u_int16_t l3num
, u_int8_t l4num
)
136 struct nf_conntrack_l4proto
*p
;
139 p
= __nf_ct_l4proto_find(l3num
, l4num
);
140 if (!try_module_get(p
->me
))
141 p
= &nf_conntrack_l4proto_generic
;
146 EXPORT_SYMBOL_GPL(nf_ct_l4proto_find_get
);
148 void nf_ct_l4proto_put(struct nf_conntrack_l4proto
*p
)
152 EXPORT_SYMBOL_GPL(nf_ct_l4proto_put
);
154 static int kill_l3proto(struct nf_conn
*i
, void *data
)
156 return nf_ct_l3num(i
) == ((struct nf_conntrack_l3proto
*)data
)->l3proto
;
159 static int kill_l4proto(struct nf_conn
*i
, void *data
)
161 struct nf_conntrack_l4proto
*l4proto
;
162 l4proto
= (struct nf_conntrack_l4proto
*)data
;
163 return nf_ct_protonum(i
) == l4proto
->l4proto
&&
164 nf_ct_l3num(i
) == l4proto
->l3proto
;
167 static struct nf_ip_net
*nf_ct_l3proto_net(struct net
*net
,
168 struct nf_conntrack_l3proto
*l3proto
)
170 if (l3proto
->l3proto
== PF_INET
)
171 return &net
->ct
.nf_ct_proto
;
176 static int nf_ct_l3proto_register_sysctl(struct net
*net
,
177 struct nf_conntrack_l3proto
*l3proto
)
180 struct nf_ip_net
*in
= nf_ct_l3proto_net(net
, l3proto
);
181 /* nf_conntrack_l3proto_ipv6 doesn't support sysctl */
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
,
189 l3proto
->ctl_table_path
,
192 kfree(in
->ctl_table
);
193 in
->ctl_table
= NULL
;
200 static void nf_ct_l3proto_unregister_sysctl(struct net
*net
,
201 struct nf_conntrack_l3proto
*l3proto
)
203 struct nf_ip_net
*in
= nf_ct_l3proto_net(net
, l3proto
);
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
,
216 nf_conntrack_l3proto_register_net(struct nf_conntrack_l3proto
*proto
)
219 struct nf_conntrack_l3proto
*old
;
221 if (proto
->l3proto
>= AF_MAX
)
224 if (proto
->tuple_to_nlattr
&& !proto
->nlattr_tuple_size
)
227 mutex_lock(&nf_ct_proto_mutex
);
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
) {
235 if (proto
->nlattr_tuple_size
)
236 proto
->nla_size
= 3 * proto
->nlattr_tuple_size();
238 rcu_assign_pointer(nf_ct_l3protos
[proto
->l3proto
], proto
);
241 mutex_unlock(&nf_ct_proto_mutex
);
246 int nf_conntrack_l3proto_register(struct net
*net
,
247 struct nf_conntrack_l3proto
*proto
)
251 if (proto
->init_net
) {
252 ret
= proto
->init_net(net
);
257 ret
= nf_ct_l3proto_register_sysctl(net
, proto
);
261 if (net
== &init_net
) {
262 ret
= nf_conntrack_l3proto_register_net(proto
);
264 nf_ct_l3proto_unregister_sysctl(net
, proto
);
269 EXPORT_SYMBOL_GPL(nf_conntrack_l3proto_register
);
272 nf_conntrack_l3proto_unregister_net(struct nf_conntrack_l3proto
*proto
)
274 BUG_ON(proto
->l3proto
>= AF_MAX
);
276 mutex_lock(&nf_ct_proto_mutex
);
277 BUG_ON(rcu_dereference_protected(nf_ct_l3protos
[proto
->l3proto
],
278 lockdep_is_held(&nf_ct_proto_mutex
)
280 rcu_assign_pointer(nf_ct_l3protos
[proto
->l3proto
],
281 &nf_conntrack_l3proto_generic
);
282 mutex_unlock(&nf_ct_proto_mutex
);
287 void nf_conntrack_l3proto_unregister(struct net
*net
,
288 struct nf_conntrack_l3proto
*proto
)
290 if (net
== &init_net
)
291 nf_conntrack_l3proto_unregister_net(proto
);
293 nf_ct_l3proto_unregister_sysctl(net
, proto
);
295 /* Remove all contrack entries for this protocol */
296 nf_ct_iterate_cleanup(net
, kill_l3proto
, proto
);
298 EXPORT_SYMBOL_GPL(nf_conntrack_l3proto_unregister
);
300 static struct nf_proto_net
*nf_ct_l4proto_net(struct net
*net
,
301 struct nf_conntrack_l4proto
*l4proto
)
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
);
314 int nf_ct_l4proto_register_sysctl(struct net
*net
,
315 struct nf_proto_net
*pn
,
316 struct nf_conntrack_l4proto
*l4proto
)
321 if (pn
->ctl_table
!= NULL
) {
322 err
= nf_ct_register_sysctl(net
,
323 &pn
->ctl_table_header
,
328 kfree(pn
->ctl_table
);
329 pn
->ctl_table
= NULL
;
333 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
334 if (l4proto
->l3proto
!= AF_INET6
&& pn
->ctl_compat_table
!= NULL
) {
336 nf_ct_kfree_compat_sysctl_table(pn
);
339 err
= nf_ct_register_sysctl(net
,
340 &pn
->ctl_compat_header
,
341 "net/ipv4/netfilter",
342 pn
->ctl_compat_table
);
346 nf_ct_kfree_compat_sysctl_table(pn
);
347 nf_ct_unregister_sysctl(&pn
->ctl_table_header
,
352 #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
353 #endif /* CONFIG_SYSCTL */
358 void nf_ct_l4proto_unregister_sysctl(struct net
*net
,
359 struct nf_proto_net
*pn
,
360 struct nf_conntrack_l4proto
*l4proto
)
363 if (pn
->ctl_table_header
!= NULL
)
364 nf_ct_unregister_sysctl(&pn
->ctl_table_header
,
368 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
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
,
373 #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
374 #endif /* CONFIG_SYSCTL */
377 /* FIXME: Allow NULL functions and sub in pointers to generic for
380 nf_conntrack_l4proto_register_net(struct nf_conntrack_l4proto
*l4proto
)
384 if (l4proto
->l3proto
>= PF_MAX
)
387 if ((l4proto
->to_nlattr
&& !l4proto
->nlattr_size
)
388 || (l4proto
->tuple_to_nlattr
&& !l4proto
->nlattr_tuple_size
))
391 mutex_lock(&nf_ct_proto_mutex
);
392 if (!nf_ct_protos
[l4proto
->l3proto
]) {
393 /* l3proto may be loaded latter. */
394 struct nf_conntrack_l4proto __rcu
**proto_array
;
397 proto_array
= kmalloc(MAX_NF_CT_PROTO
*
398 sizeof(struct nf_conntrack_l4proto
*),
400 if (proto_array
== NULL
) {
405 for (i
= 0; i
< MAX_NF_CT_PROTO
; i
++)
406 RCU_INIT_POINTER(proto_array
[i
], &nf_conntrack_l4proto_generic
);
408 /* Before making proto_array visible to lockless readers,
409 * we must make sure its content is committed to memory.
413 nf_ct_protos
[l4proto
->l3proto
] = proto_array
;
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
) {
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();
428 rcu_assign_pointer(nf_ct_protos
[l4proto
->l3proto
][l4proto
->l4proto
],
431 mutex_unlock(&nf_ct_proto_mutex
);
435 int nf_conntrack_l4proto_register(struct net
*net
,
436 struct nf_conntrack_l4proto
*l4proto
)
439 struct nf_proto_net
*pn
= NULL
;
441 if (l4proto
->init_net
) {
442 ret
= l4proto
->init_net(net
, l4proto
->l3proto
);
447 pn
= nf_ct_l4proto_net(net
, l4proto
);
451 ret
= nf_ct_l4proto_register_sysctl(net
, pn
, l4proto
);
455 if (net
== &init_net
) {
456 ret
= nf_conntrack_l4proto_register_net(l4proto
);
458 nf_ct_l4proto_unregister_sysctl(net
, pn
, l4proto
);
467 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_register
);
470 nf_conntrack_l4proto_unregister_net(struct nf_conntrack_l4proto
*l4proto
)
472 BUG_ON(l4proto
->l3proto
>= PF_MAX
);
474 mutex_lock(&nf_ct_proto_mutex
);
475 BUG_ON(rcu_dereference_protected(
476 nf_ct_protos
[l4proto
->l3proto
][l4proto
->l4proto
],
477 lockdep_is_held(&nf_ct_proto_mutex
)
479 rcu_assign_pointer(nf_ct_protos
[l4proto
->l3proto
][l4proto
->l4proto
],
480 &nf_conntrack_l4proto_generic
);
481 mutex_unlock(&nf_ct_proto_mutex
);
486 void nf_conntrack_l4proto_unregister(struct net
*net
,
487 struct nf_conntrack_l4proto
*l4proto
)
489 struct nf_proto_net
*pn
= NULL
;
491 if (net
== &init_net
)
492 nf_conntrack_l4proto_unregister_net(l4proto
);
494 pn
= nf_ct_l4proto_net(net
, l4proto
);
499 nf_ct_l4proto_unregister_sysctl(net
, pn
, l4proto
);
501 /* Remove all contrack entries for this protocol */
502 nf_ct_iterate_cleanup(net
, kill_l4proto
, l4proto
);
504 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_unregister
);
506 int nf_conntrack_proto_init(struct net
*net
)
510 struct nf_proto_net
*pn
= nf_ct_l4proto_net(net
,
511 &nf_conntrack_l4proto_generic
);
513 err
= nf_conntrack_l4proto_generic
.init_net(net
,
514 nf_conntrack_l4proto_generic
.l3proto
);
517 err
= nf_ct_l4proto_register_sysctl(net
,
519 &nf_conntrack_l4proto_generic
);
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
);
533 void nf_conntrack_proto_fini(struct net
*net
)
536 struct nf_proto_net
*pn
= nf_ct_l4proto_net(net
,
537 &nf_conntrack_l4proto_generic
);
540 nf_ct_l4proto_unregister_sysctl(net
,
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
]);