[ARM] Add more syscalls
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / netfilter / nf_conntrack_proto.c
blob1a61b72712cd53bbc36a238454959227bbaefb45
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);
35 #ifdef CONFIG_SYSCTL
36 static DEFINE_MUTEX(nf_ct_proto_sysctl_mutex);
38 static int
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);
44 if (*header == NULL)
45 return -ENOMEM;
47 if (users != NULL)
48 (*users)++;
49 return 0;
52 static void
53 nf_ct_unregister_sysctl(struct ctl_table_header **header,
54 struct ctl_table *table, unsigned int *users)
56 if (users != NULL && --*users > 0)
57 return;
58 nf_unregister_sysctl_table(*header, table);
59 *header = NULL;
61 #endif
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;
80 preempt_disable();
81 p = __nf_ct_l4proto_find(l3proto, l4proto);
82 if (!try_module_get(p->me))
83 p = &nf_conntrack_l4proto_generic;
84 preempt_enable();
86 return p;
88 EXPORT_SYMBOL_GPL(nf_ct_l4proto_find_get);
90 void nf_ct_l4proto_put(struct nf_conntrack_l4proto *p)
92 module_put(p->me);
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;
101 preempt_disable();
102 p = __nf_ct_l3proto_find(l3proto);
103 if (!try_module_get(p->me))
104 p = &nf_conntrack_l3proto_generic;
105 preempt_enable();
107 return p;
109 EXPORT_SYMBOL_GPL(nf_ct_l3proto_find_get);
111 void nf_ct_l3proto_put(struct nf_conntrack_l3proto *p)
113 module_put(p->me);
115 EXPORT_SYMBOL_GPL(nf_ct_l3proto_put);
118 nf_ct_l3proto_try_module_get(unsigned short l3proto)
120 int ret;
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);
126 if (!ret)
127 goto retry;
129 return -EPROTOTYPE;
132 return 0;
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;
140 preempt_disable();
141 p = __nf_ct_l3proto_find(l3proto);
142 preempt_enable();
144 module_put(p->me);
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 ==
159 l4proto->l4proto) &&
160 (i->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num ==
161 l4proto->l3proto);
164 static int nf_ct_l3proto_register_sysctl(struct nf_conntrack_l3proto *l3proto)
166 int err = 0;
168 #ifdef CONFIG_SYSCTL
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);
176 #endif
177 return err;
180 static void nf_ct_l3proto_unregister_sysctl(struct nf_conntrack_l3proto *l3proto)
182 #ifdef CONFIG_SYSCTL
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);
188 #endif
191 int nf_conntrack_l3proto_register(struct nf_conntrack_l3proto *proto)
193 int ret = 0;
195 if (proto->l3proto >= AF_MAX) {
196 ret = -EBUSY;
197 goto out;
200 write_lock_bh(&nf_conntrack_lock);
201 if (nf_ct_l3protos[proto->l3proto] != &nf_conntrack_l3proto_generic) {
202 ret = -EBUSY;
203 goto out_unlock;
205 nf_ct_l3protos[proto->l3proto] = proto;
206 write_unlock_bh(&nf_conntrack_lock);
208 ret = nf_ct_l3proto_register_sysctl(proto);
209 if (ret < 0)
210 nf_conntrack_l3proto_unregister(proto);
211 return ret;
213 out_unlock:
214 write_unlock_bh(&nf_conntrack_lock);
215 out:
216 return ret;
218 EXPORT_SYMBOL_GPL(nf_conntrack_l3proto_register);
220 int nf_conntrack_l3proto_unregister(struct nf_conntrack_l3proto *proto)
222 int ret = 0;
224 if (proto->l3proto >= AF_MAX) {
225 ret = -EBUSY;
226 goto out;
229 write_lock_bh(&nf_conntrack_lock);
230 if (nf_ct_l3protos[proto->l3proto] != proto) {
231 write_unlock_bh(&nf_conntrack_lock);
232 ret = -EBUSY;
233 goto out;
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. */
242 synchronize_net();
244 /* Remove all contrack entries for this protocol */
245 nf_ct_iterate_cleanup(kill_l3proto, proto);
247 out:
248 return ret;
250 EXPORT_SYMBOL_GPL(nf_conntrack_l3proto_unregister);
252 static int nf_ct_l4proto_register_sysctl(struct nf_conntrack_l4proto *l4proto)
254 int err = 0;
256 #ifdef CONFIG_SYSCTL
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,
261 l4proto->ctl_table,
262 l4proto->ctl_table_users);
263 if (err < 0)
264 goto out;
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);
271 if (err == 0)
272 goto out;
273 nf_ct_unregister_sysctl(l4proto->ctl_table_header,
274 l4proto->ctl_table,
275 l4proto->ctl_table_users);
277 #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
278 out:
279 mutex_unlock(&nf_ct_proto_sysctl_mutex);
280 #endif /* CONFIG_SYSCTL */
281 return err;
284 static void nf_ct_l4proto_unregister_sysctl(struct nf_conntrack_l4proto *l4proto)
286 #ifdef CONFIG_SYSCTL
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,
291 l4proto->ctl_table,
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
303 them. --RR */
304 int nf_conntrack_l4proto_register(struct nf_conntrack_l4proto *l4proto)
306 int ret = 0;
308 if (l4proto->l3proto >= PF_MAX) {
309 ret = -EBUSY;
310 goto out;
313 if (l4proto == &nf_conntrack_l4proto_generic)
314 return nf_ct_l4proto_register_sysctl(l4proto);
316 retry:
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) {
321 ret = -EBUSY;
322 goto out_unlock;
324 } else {
325 /* l3proto may be loaded latter. */
326 struct nf_conntrack_l4proto **proto_array;
327 int i;
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 *),
334 GFP_KERNEL);
335 if (proto_array == NULL) {
336 ret = -ENOMEM;
337 goto out;
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);
346 kfree(proto_array);
347 } else {
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
354 * nf_conntrack.ko
356 goto retry;
359 nf_ct_protos[l4proto->l3proto][l4proto->l4proto] = l4proto;
360 write_unlock_bh(&nf_conntrack_lock);
362 ret = nf_ct_l4proto_register_sysctl(l4proto);
363 if (ret < 0)
364 nf_conntrack_l4proto_unregister(l4proto);
365 return ret;
367 out_unlock:
368 write_unlock_bh(&nf_conntrack_lock);
369 out:
370 return ret;
372 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_register);
374 int nf_conntrack_l4proto_unregister(struct nf_conntrack_l4proto *l4proto)
376 int ret = 0;
378 if (l4proto->l3proto >= PF_MAX) {
379 ret = -EBUSY;
380 goto out;
383 if (l4proto == &nf_conntrack_l4proto_generic) {
384 nf_ct_l4proto_unregister_sysctl(l4proto);
385 goto out;
388 write_lock_bh(&nf_conntrack_lock);
389 if (nf_ct_protos[l4proto->l3proto][l4proto->l4proto]
390 != l4proto) {
391 write_unlock_bh(&nf_conntrack_lock);
392 ret = -EBUSY;
393 goto out;
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. */
402 synchronize_net();
404 /* Remove all contrack entries for this protocol */
405 nf_ct_iterate_cleanup(kill_l4proto, l4proto);
407 out:
408 return ret;
410 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_unregister);