[NETFILTER]: nf_conntrack: switch protocol registration/unregistration to mutex
[linux-2.6/x86.git] / net / netfilter / nf_conntrack_proto.c
blobe2c4a58603a86200b88e159f3b1ae4438fd7ab13
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 static DEFINE_MUTEX(nf_ct_proto_mutex);
37 #ifdef CONFIG_SYSCTL
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 rcu_dereference(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 rcu_read_lock();
81 p = __nf_ct_l4proto_find(l3proto, l4proto);
82 if (!try_module_get(p->me))
83 p = &nf_conntrack_l4proto_generic;
84 rcu_read_unlock();
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 rcu_read_lock();
102 p = __nf_ct_l3proto_find(l3proto);
103 if (!try_module_get(p->me))
104 p = &nf_conntrack_l3proto_generic;
105 rcu_read_unlock();
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 /* rcu_read_lock not necessary since the caller holds a reference */
141 p = __nf_ct_l3proto_find(l3proto);
142 module_put(p->me);
144 EXPORT_SYMBOL_GPL(nf_ct_l3proto_module_put);
146 static int kill_l3proto(struct nf_conn *i, void *data)
148 return (i->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num ==
149 ((struct nf_conntrack_l3proto *)data)->l3proto);
152 static int kill_l4proto(struct nf_conn *i, void *data)
154 struct nf_conntrack_l4proto *l4proto;
155 l4proto = (struct nf_conntrack_l4proto *)data;
156 return (i->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum ==
157 l4proto->l4proto) &&
158 (i->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num ==
159 l4proto->l3proto);
162 static int nf_ct_l3proto_register_sysctl(struct nf_conntrack_l3proto *l3proto)
164 int err = 0;
166 #ifdef CONFIG_SYSCTL
167 mutex_lock(&nf_ct_proto_mutex);
168 if (l3proto->ctl_table != NULL) {
169 err = nf_ct_register_sysctl(&l3proto->ctl_table_header,
170 l3proto->ctl_table_path,
171 l3proto->ctl_table, NULL);
173 mutex_unlock(&nf_ct_proto_mutex);
174 #endif
175 return err;
178 static void nf_ct_l3proto_unregister_sysctl(struct nf_conntrack_l3proto *l3proto)
180 #ifdef CONFIG_SYSCTL
181 mutex_lock(&nf_ct_proto_mutex);
182 if (l3proto->ctl_table_header != NULL)
183 nf_ct_unregister_sysctl(&l3proto->ctl_table_header,
184 l3proto->ctl_table, NULL);
185 mutex_unlock(&nf_ct_proto_mutex);
186 #endif
189 int nf_conntrack_l3proto_register(struct nf_conntrack_l3proto *proto)
191 int ret = 0;
193 if (proto->l3proto >= AF_MAX) {
194 ret = -EBUSY;
195 goto out;
198 mutex_lock(&nf_ct_proto_mutex);
199 if (nf_ct_l3protos[proto->l3proto] != &nf_conntrack_l3proto_generic) {
200 ret = -EBUSY;
201 goto out_unlock;
203 rcu_assign_pointer(nf_ct_l3protos[proto->l3proto], proto);
204 mutex_unlock(&nf_ct_proto_mutex);
206 ret = nf_ct_l3proto_register_sysctl(proto);
207 if (ret < 0)
208 nf_conntrack_l3proto_unregister(proto);
209 return ret;
211 out_unlock:
212 mutex_unlock(&nf_ct_proto_mutex);
213 out:
214 return ret;
216 EXPORT_SYMBOL_GPL(nf_conntrack_l3proto_register);
218 void nf_conntrack_l3proto_unregister(struct nf_conntrack_l3proto *proto)
220 BUG_ON(proto->l3proto >= AF_MAX);
222 mutex_lock(&nf_ct_proto_mutex);
223 BUG_ON(nf_ct_l3protos[proto->l3proto] != proto);
224 rcu_assign_pointer(nf_ct_l3protos[proto->l3proto],
225 &nf_conntrack_l3proto_generic);
226 mutex_unlock(&nf_ct_proto_mutex);
227 synchronize_rcu();
229 nf_ct_l3proto_unregister_sysctl(proto);
231 /* Remove all contrack entries for this protocol */
232 nf_ct_iterate_cleanup(kill_l3proto, proto);
234 EXPORT_SYMBOL_GPL(nf_conntrack_l3proto_unregister);
236 static int nf_ct_l4proto_register_sysctl(struct nf_conntrack_l4proto *l4proto)
238 int err = 0;
240 #ifdef CONFIG_SYSCTL
241 mutex_lock(&nf_ct_proto_mutex);
242 if (l4proto->ctl_table != NULL) {
243 err = nf_ct_register_sysctl(l4proto->ctl_table_header,
244 nf_net_netfilter_sysctl_path,
245 l4proto->ctl_table,
246 l4proto->ctl_table_users);
247 if (err < 0)
248 goto out;
250 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
251 if (l4proto->ctl_compat_table != NULL) {
252 err = nf_ct_register_sysctl(&l4proto->ctl_compat_table_header,
253 nf_net_ipv4_netfilter_sysctl_path,
254 l4proto->ctl_compat_table, NULL);
255 if (err == 0)
256 goto out;
257 nf_ct_unregister_sysctl(l4proto->ctl_table_header,
258 l4proto->ctl_table,
259 l4proto->ctl_table_users);
261 #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
262 out:
263 mutex_unlock(&nf_ct_proto_mutex);
264 #endif /* CONFIG_SYSCTL */
265 return err;
268 static void nf_ct_l4proto_unregister_sysctl(struct nf_conntrack_l4proto *l4proto)
270 #ifdef CONFIG_SYSCTL
271 mutex_lock(&nf_ct_proto_mutex);
272 if (l4proto->ctl_table_header != NULL &&
273 *l4proto->ctl_table_header != NULL)
274 nf_ct_unregister_sysctl(l4proto->ctl_table_header,
275 l4proto->ctl_table,
276 l4proto->ctl_table_users);
277 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
278 if (l4proto->ctl_compat_table_header != NULL)
279 nf_ct_unregister_sysctl(&l4proto->ctl_compat_table_header,
280 l4proto->ctl_compat_table, NULL);
281 #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
282 mutex_unlock(&nf_ct_proto_mutex);
283 #endif /* CONFIG_SYSCTL */
286 /* FIXME: Allow NULL functions and sub in pointers to generic for
287 them. --RR */
288 int nf_conntrack_l4proto_register(struct nf_conntrack_l4proto *l4proto)
290 int ret = 0;
292 if (l4proto->l3proto >= PF_MAX) {
293 ret = -EBUSY;
294 goto out;
297 if (l4proto == &nf_conntrack_l4proto_generic)
298 return nf_ct_l4proto_register_sysctl(l4proto);
300 mutex_lock(&nf_ct_proto_mutex);
301 retry:
302 if (nf_ct_protos[l4proto->l3proto]) {
303 if (nf_ct_protos[l4proto->l3proto][l4proto->l4proto]
304 != &nf_conntrack_l4proto_generic) {
305 ret = -EBUSY;
306 goto out_unlock;
308 } else {
309 /* l3proto may be loaded latter. */
310 struct nf_conntrack_l4proto **proto_array;
311 int i;
313 proto_array = (struct nf_conntrack_l4proto **)
314 kmalloc(MAX_NF_CT_PROTO *
315 sizeof(struct nf_conntrack_l4proto *),
316 GFP_KERNEL);
317 if (proto_array == NULL) {
318 ret = -ENOMEM;
319 goto out_unlock;
321 for (i = 0; i < MAX_NF_CT_PROTO; i++)
322 proto_array[i] = &nf_conntrack_l4proto_generic;
324 if (nf_ct_protos[l4proto->l3proto])
325 /* bad timing, but no problem */
326 kfree(proto_array);
327 else
328 nf_ct_protos[l4proto->l3proto] = proto_array;
331 * Just once because array is never freed until unloading
332 * nf_conntrack.ko
334 goto retry;
337 rcu_assign_pointer(nf_ct_protos[l4proto->l3proto][l4proto->l4proto], l4proto);
338 mutex_unlock(&nf_ct_proto_mutex);
340 ret = nf_ct_l4proto_register_sysctl(l4proto);
341 if (ret < 0)
342 nf_conntrack_l4proto_unregister(l4proto);
343 return ret;
345 out_unlock:
346 mutex_unlock(&nf_ct_proto_mutex);
347 out:
348 return ret;
350 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_register);
352 void nf_conntrack_l4proto_unregister(struct nf_conntrack_l4proto *l4proto)
354 BUG_ON(l4proto->l3proto >= PF_MAX);
356 if (l4proto == &nf_conntrack_l4proto_generic) {
357 nf_ct_l4proto_unregister_sysctl(l4proto);
358 return;
361 mutex_lock(&nf_ct_proto_mutex);
362 BUG_ON(nf_ct_protos[l4proto->l3proto][l4proto->l4proto] != l4proto);
363 rcu_assign_pointer(nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
364 &nf_conntrack_l4proto_generic);
365 mutex_unlock(&nf_ct_proto_mutex);
366 synchronize_rcu();
368 nf_ct_l4proto_unregister_sysctl(l4proto);
370 /* Remove all contrack entries for this protocol */
371 nf_ct_iterate_cleanup(kill_l4proto, l4proto);
373 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_unregister);