This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / net / netfilter / nf_conntrack_proto.c
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>
6  *
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.
10  */
11
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>
25
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>
30
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);
34
35 #ifdef CONFIG_SYSCTL
36 static DEFINE_MUTEX(nf_ct_proto_sysctl_mutex);
37
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)
41 {
42         if (*header == NULL) {
43                 *header = nf_register_sysctl_table(path, table);
44                 if (*header == NULL)
45                         return -ENOMEM;
46         }
47         if (users != NULL)
48                 (*users)++;
49         return 0;
50 }
51
52 static void
53 nf_ct_unregister_sysctl(struct ctl_table_header **header,
54                         struct ctl_table *table, unsigned int *users)
55 {
56         if (users != NULL && --*users > 0)
57                 return;
58         nf_unregister_sysctl_table(*header, table);
59         *header = NULL;
60 }
61 #endif
62
63 struct nf_conntrack_l4proto *
64 __nf_ct_l4proto_find(u_int16_t l3proto, u_int8_t l4proto)
65 {
66         if (unlikely(l3proto >= AF_MAX || nf_ct_protos[l3proto] == NULL))
67                 return &nf_conntrack_l4proto_generic;
68
69         return nf_ct_protos[l3proto][l4proto];
70 }
71 EXPORT_SYMBOL_GPL(__nf_ct_l4proto_find);
72
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)
77 {
78         struct nf_conntrack_l4proto *p;
79
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();
85
86         return p;
87 }
88 EXPORT_SYMBOL_GPL(nf_ct_l4proto_find_get);
89
90 void nf_ct_l4proto_put(struct nf_conntrack_l4proto *p)
91 {
92         module_put(p->me);
93 }
94 EXPORT_SYMBOL_GPL(nf_ct_l4proto_put);
95
96 struct nf_conntrack_l3proto *
97 nf_ct_l3proto_find_get(u_int16_t l3proto)
98 {
99         struct nf_conntrack_l3proto *p;
100
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();
106
107         return p;
108 }
109 EXPORT_SYMBOL_GPL(nf_ct_l3proto_find_get);
110
111 void nf_ct_l3proto_put(struct nf_conntrack_l3proto *p)
112 {
113         module_put(p->me);
114 }
115 EXPORT_SYMBOL_GPL(nf_ct_l3proto_put);
116
117 int
118 nf_ct_l3proto_try_module_get(unsigned short l3proto)
119 {
120         int ret;
121         struct nf_conntrack_l3proto *p;
122
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;
128
129                 return -EPROTOTYPE;
130         }
131
132         return 0;
133 }
134 EXPORT_SYMBOL_GPL(nf_ct_l3proto_try_module_get);
135
136 void nf_ct_l3proto_module_put(unsigned short l3proto)
137 {
138         struct nf_conntrack_l3proto *p;
139
140         preempt_disable();
141         p = __nf_ct_l3proto_find(l3proto);
142         preempt_enable();
143
144         module_put(p->me);
145 }
146 EXPORT_SYMBOL_GPL(nf_ct_l3proto_module_put);
147
148 static int kill_l3proto(struct nf_conn *i, void *data)
149 {
150         return (i->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num ==
151                         ((struct nf_conntrack_l3proto *)data)->l3proto);
152 }
153
154 static int kill_l4proto(struct nf_conn *i, void *data)
155 {
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);
162 }
163
164 static int nf_ct_l3proto_register_sysctl(struct nf_conntrack_l3proto *l3proto)
165 {
166         int err = 0;
167
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);
174         }
175         mutex_unlock(&nf_ct_proto_sysctl_mutex);
176 #endif
177         return err;
178 }
179
180 static void nf_ct_l3proto_unregister_sysctl(struct nf_conntrack_l3proto *l3proto)
181 {
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
189 }
190
191 int nf_conntrack_l3proto_register(struct nf_conntrack_l3proto *proto)
192 {
193         int ret = 0;
194
195         if (proto->l3proto >= AF_MAX) {
196                 ret = -EBUSY;
197                 goto out;
198         }
199
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;
204         }
205         nf_ct_l3protos[proto->l3proto] = proto;
206         write_unlock_bh(&nf_conntrack_lock);
207
208         ret = nf_ct_l3proto_register_sysctl(proto);
209         if (ret < 0)
210                 nf_conntrack_l3proto_unregister(proto);
211         return ret;
212
213 out_unlock:
214         write_unlock_bh(&nf_conntrack_lock);
215 out:
216         return ret;
217 }
218 EXPORT_SYMBOL_GPL(nf_conntrack_l3proto_register);
219
220 int nf_conntrack_l3proto_unregister(struct nf_conntrack_l3proto *proto)
221 {
222         int ret = 0;
223
224         if (proto->l3proto >= AF_MAX) {
225                 ret = -EBUSY;
226                 goto out;
227         }
228
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;
234         }
235
236         nf_ct_l3protos[proto->l3proto] = &nf_conntrack_l3proto_generic;
237         write_unlock_bh(&nf_conntrack_lock);
238
239         nf_ct_l3proto_unregister_sysctl(proto);
240
241         /* Somebody could be still looking at the proto in bh. */
242         synchronize_net();
243
244         /* Remove all contrack entries for this protocol */
245         nf_ct_iterate_cleanup(kill_l3proto, proto);
246
247 out:
248         return ret;
249 }
250 EXPORT_SYMBOL_GPL(nf_conntrack_l3proto_unregister);
251
252 static int nf_ct_l4proto_register_sysctl(struct nf_conntrack_l4proto *l4proto)
253 {
254         int err = 0;
255
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;
265         }
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);
276         }
277 #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
278 out:
279         mutex_unlock(&nf_ct_proto_sysctl_mutex);
280 #endif /* CONFIG_SYSCTL */
281         return err;
282 }
283
284 static void nf_ct_l4proto_unregister_sysctl(struct nf_conntrack_l4proto *l4proto)
285 {
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 */
300 }
301
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)
305 {
306         int ret = 0;
307
308         if (l4proto->l3proto >= PF_MAX) {
309                 ret = -EBUSY;
310                 goto out;
311         }
312
313         if (l4proto == &nf_conntrack_l4proto_generic)
314                 return nf_ct_l4proto_register_sysctl(l4proto);
315
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;
323                 }
324         } else {
325                 /* l3proto may be loaded latter. */
326                 struct nf_conntrack_l4proto **proto_array;
327                 int i;
328
329                 write_unlock_bh(&nf_conntrack_lock);
330
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;
338                 }
339                 for (i = 0; i < MAX_NF_CT_PROTO; i++)
340                         proto_array[i] = &nf_conntrack_l4proto_generic;
341
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);
350                 }
351
352                 /*
353                  * Just once because array is never freed until unloading
354                  * nf_conntrack.ko
355                  */
356                 goto retry;
357         }
358
359         nf_ct_protos[l4proto->l3proto][l4proto->l4proto] = l4proto;
360         write_unlock_bh(&nf_conntrack_lock);
361
362         ret = nf_ct_l4proto_register_sysctl(l4proto);
363         if (ret < 0)
364                 nf_conntrack_l4proto_unregister(l4proto);
365         return ret;
366
367 out_unlock:
368         write_unlock_bh(&nf_conntrack_lock);
369 out:
370         return ret;
371 }
372 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_register);
373
374 int nf_conntrack_l4proto_unregister(struct nf_conntrack_l4proto *l4proto)
375 {
376         int ret = 0;
377
378         if (l4proto->l3proto >= PF_MAX) {
379                 ret = -EBUSY;
380                 goto out;
381         }
382
383         if (l4proto == &nf_conntrack_l4proto_generic) {
384                 nf_ct_l4proto_unregister_sysctl(l4proto);
385                 goto out;
386         }
387
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;
394         }
395         nf_ct_protos[l4proto->l3proto][l4proto->l4proto]
396                 = &nf_conntrack_l4proto_generic;
397         write_unlock_bh(&nf_conntrack_lock);
398
399         nf_ct_l4proto_unregister_sysctl(l4proto);
400
401         /* Somebody could be still looking at the proto in bh. */
402         synchronize_net();
403
404         /* Remove all contrack entries for this protocol */
405         nf_ct_iterate_cleanup(kill_l4proto, l4proto);
406
407 out:
408         return ret;
409 }
410 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_unregister);