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/mutex.h>
16 #include <linux/skbuff.h>
17 #include <linux/vmalloc.h>
18 #include <linux/stddef.h>
19 #include <linux/err.h>
20 #include <linux/percpu.h>
21 #include <linux/moduleparam.h>
22 #include <linux/notifier.h>
23 #include <linux/kernel.h>
24 #include <linux/netdevice.h>
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>
31 struct nf_conntrack_l4proto
**nf_ct_protos
[PF_MAX
] __read_mostly
;
32 struct nf_conntrack_l3proto
*nf_ct_l3protos
[AF_MAX
] __read_mostly
;
33 EXPORT_SYMBOL_GPL(nf_ct_l3protos
);
36 static DEFINE_MUTEX(nf_ct_proto_sysctl_mutex
);
39 nf_ct_register_sysctl(struct ctl_table_header
**header
, struct ctl_table
*path
,
40 struct ctl_table
*table
, unsigned int *users
)
42 if (*header
== NULL
) {
43 *header
= nf_register_sysctl_table(path
, table
);
53 nf_ct_unregister_sysctl(struct ctl_table_header
**header
,
54 struct ctl_table
*table
, unsigned int *users
)
56 if (users
!= NULL
&& --*users
> 0)
58 nf_unregister_sysctl_table(*header
, table
);
63 struct nf_conntrack_l4proto
*
64 __nf_ct_l4proto_find(u_int16_t l3proto
, u_int8_t l4proto
)
66 if (unlikely(l3proto
>= AF_MAX
|| nf_ct_protos
[l3proto
] == NULL
))
67 return &nf_conntrack_l4proto_generic
;
69 return nf_ct_protos
[l3proto
][l4proto
];
71 EXPORT_SYMBOL_GPL(__nf_ct_l4proto_find
);
73 /* this is guaranteed to always return a valid protocol helper, since
74 * it falls back to generic_protocol */
75 struct nf_conntrack_l4proto
*
76 nf_ct_l4proto_find_get(u_int16_t l3proto
, u_int8_t l4proto
)
78 struct nf_conntrack_l4proto
*p
;
81 p
= __nf_ct_l4proto_find(l3proto
, l4proto
);
82 if (!try_module_get(p
->me
))
83 p
= &nf_conntrack_l4proto_generic
;
88 EXPORT_SYMBOL_GPL(nf_ct_l4proto_find_get
);
90 void nf_ct_l4proto_put(struct nf_conntrack_l4proto
*p
)
94 EXPORT_SYMBOL_GPL(nf_ct_l4proto_put
);
96 struct nf_conntrack_l3proto
*
97 nf_ct_l3proto_find_get(u_int16_t l3proto
)
99 struct nf_conntrack_l3proto
*p
;
102 p
= __nf_ct_l3proto_find(l3proto
);
103 if (!try_module_get(p
->me
))
104 p
= &nf_conntrack_l3proto_generic
;
109 EXPORT_SYMBOL_GPL(nf_ct_l3proto_find_get
);
111 void nf_ct_l3proto_put(struct nf_conntrack_l3proto
*p
)
115 EXPORT_SYMBOL_GPL(nf_ct_l3proto_put
);
118 nf_ct_l3proto_try_module_get(unsigned short l3proto
)
121 struct nf_conntrack_l3proto
*p
;
123 retry
: p
= nf_ct_l3proto_find_get(l3proto
);
124 if (p
== &nf_conntrack_l3proto_generic
) {
125 ret
= request_module("nf_conntrack-%d", l3proto
);
134 EXPORT_SYMBOL_GPL(nf_ct_l3proto_try_module_get
);
136 void nf_ct_l3proto_module_put(unsigned short l3proto
)
138 struct nf_conntrack_l3proto
*p
;
141 p
= __nf_ct_l3proto_find(l3proto
);
146 EXPORT_SYMBOL_GPL(nf_ct_l3proto_module_put
);
148 static int kill_l3proto(struct nf_conn
*i
, void *data
)
150 return (i
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.l3num
==
151 ((struct nf_conntrack_l3proto
*)data
)->l3proto
);
154 static int kill_l4proto(struct nf_conn
*i
, void *data
)
156 struct nf_conntrack_l4proto
*l4proto
;
157 l4proto
= (struct nf_conntrack_l4proto
*)data
;
158 return (i
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.dst
.protonum
==
160 (i
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.l3num
==
164 static int nf_ct_l3proto_register_sysctl(struct nf_conntrack_l3proto
*l3proto
)
169 mutex_lock(&nf_ct_proto_sysctl_mutex
);
170 if (l3proto
->ctl_table
!= NULL
) {
171 err
= nf_ct_register_sysctl(&l3proto
->ctl_table_header
,
172 l3proto
->ctl_table_path
,
173 l3proto
->ctl_table
, NULL
);
175 mutex_unlock(&nf_ct_proto_sysctl_mutex
);
180 static void nf_ct_l3proto_unregister_sysctl(struct nf_conntrack_l3proto
*l3proto
)
183 mutex_lock(&nf_ct_proto_sysctl_mutex
);
184 if (l3proto
->ctl_table_header
!= NULL
)
185 nf_ct_unregister_sysctl(&l3proto
->ctl_table_header
,
186 l3proto
->ctl_table
, NULL
);
187 mutex_unlock(&nf_ct_proto_sysctl_mutex
);
191 int nf_conntrack_l3proto_register(struct nf_conntrack_l3proto
*proto
)
195 if (proto
->l3proto
>= AF_MAX
) {
200 write_lock_bh(&nf_conntrack_lock
);
201 if (nf_ct_l3protos
[proto
->l3proto
] != &nf_conntrack_l3proto_generic
) {
205 nf_ct_l3protos
[proto
->l3proto
] = proto
;
206 write_unlock_bh(&nf_conntrack_lock
);
208 ret
= nf_ct_l3proto_register_sysctl(proto
);
210 nf_conntrack_l3proto_unregister(proto
);
214 write_unlock_bh(&nf_conntrack_lock
);
218 EXPORT_SYMBOL_GPL(nf_conntrack_l3proto_register
);
220 int nf_conntrack_l3proto_unregister(struct nf_conntrack_l3proto
*proto
)
224 if (proto
->l3proto
>= AF_MAX
) {
229 write_lock_bh(&nf_conntrack_lock
);
230 if (nf_ct_l3protos
[proto
->l3proto
] != proto
) {
231 write_unlock_bh(&nf_conntrack_lock
);
236 nf_ct_l3protos
[proto
->l3proto
] = &nf_conntrack_l3proto_generic
;
237 write_unlock_bh(&nf_conntrack_lock
);
239 nf_ct_l3proto_unregister_sysctl(proto
);
241 /* Somebody could be still looking at the proto in bh. */
244 /* Remove all contrack entries for this protocol */
245 nf_ct_iterate_cleanup(kill_l3proto
, proto
);
250 EXPORT_SYMBOL_GPL(nf_conntrack_l3proto_unregister
);
252 static int nf_ct_l4proto_register_sysctl(struct nf_conntrack_l4proto
*l4proto
)
257 mutex_lock(&nf_ct_proto_sysctl_mutex
);
258 if (l4proto
->ctl_table
!= NULL
) {
259 err
= nf_ct_register_sysctl(l4proto
->ctl_table_header
,
260 nf_net_netfilter_sysctl_path
,
262 l4proto
->ctl_table_users
);
266 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
267 if (l4proto
->ctl_compat_table
!= NULL
) {
268 err
= nf_ct_register_sysctl(&l4proto
->ctl_compat_table_header
,
269 nf_net_ipv4_netfilter_sysctl_path
,
270 l4proto
->ctl_compat_table
, NULL
);
273 nf_ct_unregister_sysctl(l4proto
->ctl_table_header
,
275 l4proto
->ctl_table_users
);
277 #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
279 mutex_unlock(&nf_ct_proto_sysctl_mutex
);
280 #endif /* CONFIG_SYSCTL */
284 static void nf_ct_l4proto_unregister_sysctl(struct nf_conntrack_l4proto
*l4proto
)
287 mutex_lock(&nf_ct_proto_sysctl_mutex
);
288 if (l4proto
->ctl_table_header
!= NULL
&&
289 *l4proto
->ctl_table_header
!= NULL
)
290 nf_ct_unregister_sysctl(l4proto
->ctl_table_header
,
292 l4proto
->ctl_table_users
);
293 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
294 if (l4proto
->ctl_compat_table_header
!= NULL
)
295 nf_ct_unregister_sysctl(&l4proto
->ctl_compat_table_header
,
296 l4proto
->ctl_compat_table
, NULL
);
297 #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
298 mutex_unlock(&nf_ct_proto_sysctl_mutex
);
299 #endif /* CONFIG_SYSCTL */
302 /* FIXME: Allow NULL functions and sub in pointers to generic for
304 int nf_conntrack_l4proto_register(struct nf_conntrack_l4proto
*l4proto
)
308 if (l4proto
->l3proto
>= PF_MAX
) {
313 if (l4proto
== &nf_conntrack_l4proto_generic
)
314 return nf_ct_l4proto_register_sysctl(l4proto
);
317 write_lock_bh(&nf_conntrack_lock
);
318 if (nf_ct_protos
[l4proto
->l3proto
]) {
319 if (nf_ct_protos
[l4proto
->l3proto
][l4proto
->l4proto
]
320 != &nf_conntrack_l4proto_generic
) {
325 /* l3proto may be loaded latter. */
326 struct nf_conntrack_l4proto
**proto_array
;
329 write_unlock_bh(&nf_conntrack_lock
);
331 proto_array
= (struct nf_conntrack_l4proto
**)
332 kmalloc(MAX_NF_CT_PROTO
*
333 sizeof(struct nf_conntrack_l4proto
*),
335 if (proto_array
== NULL
) {
339 for (i
= 0; i
< MAX_NF_CT_PROTO
; i
++)
340 proto_array
[i
] = &nf_conntrack_l4proto_generic
;
342 write_lock_bh(&nf_conntrack_lock
);
343 if (nf_ct_protos
[l4proto
->l3proto
]) {
344 /* bad timing, but no problem */
345 write_unlock_bh(&nf_conntrack_lock
);
348 nf_ct_protos
[l4proto
->l3proto
] = proto_array
;
349 write_unlock_bh(&nf_conntrack_lock
);
353 * Just once because array is never freed until unloading
359 nf_ct_protos
[l4proto
->l3proto
][l4proto
->l4proto
] = l4proto
;
360 write_unlock_bh(&nf_conntrack_lock
);
362 ret
= nf_ct_l4proto_register_sysctl(l4proto
);
364 nf_conntrack_l4proto_unregister(l4proto
);
368 write_unlock_bh(&nf_conntrack_lock
);
372 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_register
);
374 int nf_conntrack_l4proto_unregister(struct nf_conntrack_l4proto
*l4proto
)
378 if (l4proto
->l3proto
>= PF_MAX
) {
383 if (l4proto
== &nf_conntrack_l4proto_generic
) {
384 nf_ct_l4proto_unregister_sysctl(l4proto
);
388 write_lock_bh(&nf_conntrack_lock
);
389 if (nf_ct_protos
[l4proto
->l3proto
][l4proto
->l4proto
]
391 write_unlock_bh(&nf_conntrack_lock
);
395 nf_ct_protos
[l4proto
->l3proto
][l4proto
->l4proto
]
396 = &nf_conntrack_l4proto_generic
;
397 write_unlock_bh(&nf_conntrack_lock
);
399 nf_ct_l4proto_unregister_sysctl(l4proto
);
401 /* Somebody could be still looking at the proto in bh. */
404 /* Remove all contrack entries for this protocol */
405 nf_ct_iterate_cleanup(kill_l4proto
, l4proto
);
410 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_unregister
);