a6defc7936014df69e3b0cc4d70682873b2b9487
[pandora-kernel.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/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>
24 #include <linux/rtnetlink.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 static 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 static DEFINE_MUTEX(nf_ct_proto_mutex);
36
37 #ifdef CONFIG_SYSCTL
38 static int
39 nf_ct_register_sysctl(struct ctl_table_header **header, struct ctl_path *path,
40                       struct ctl_table *table, unsigned int *users)
41 {
42         if (*header == NULL) {
43                 *header = register_sysctl_paths(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
59         unregister_sysctl_table(*header);
60         *header = NULL;
61 }
62 #endif
63
64 struct nf_conntrack_l4proto *
65 __nf_ct_l4proto_find(u_int16_t l3proto, u_int8_t l4proto)
66 {
67         if (unlikely(l3proto >= AF_MAX || nf_ct_protos[l3proto] == NULL))
68                 return &nf_conntrack_l4proto_generic;
69
70         return rcu_dereference(nf_ct_protos[l3proto][l4proto]);
71 }
72 EXPORT_SYMBOL_GPL(__nf_ct_l4proto_find);
73
74 /* this is guaranteed to always return a valid protocol helper, since
75  * it falls back to generic_protocol */
76 struct nf_conntrack_l3proto *
77 nf_ct_l3proto_find_get(u_int16_t l3proto)
78 {
79         struct nf_conntrack_l3proto *p;
80
81         rcu_read_lock();
82         p = __nf_ct_l3proto_find(l3proto);
83         if (!try_module_get(p->me))
84                 p = &nf_conntrack_l3proto_generic;
85         rcu_read_unlock();
86
87         return p;
88 }
89 EXPORT_SYMBOL_GPL(nf_ct_l3proto_find_get);
90
91 void nf_ct_l3proto_put(struct nf_conntrack_l3proto *p)
92 {
93         module_put(p->me);
94 }
95 EXPORT_SYMBOL_GPL(nf_ct_l3proto_put);
96
97 int
98 nf_ct_l3proto_try_module_get(unsigned short l3proto)
99 {
100         int ret;
101         struct nf_conntrack_l3proto *p;
102
103 retry:  p = nf_ct_l3proto_find_get(l3proto);
104         if (p == &nf_conntrack_l3proto_generic) {
105                 ret = request_module("nf_conntrack-%d", l3proto);
106                 if (!ret)
107                         goto retry;
108
109                 return -EPROTOTYPE;
110         }
111
112         return 0;
113 }
114 EXPORT_SYMBOL_GPL(nf_ct_l3proto_try_module_get);
115
116 void nf_ct_l3proto_module_put(unsigned short l3proto)
117 {
118         struct nf_conntrack_l3proto *p;
119
120         /* rcu_read_lock not necessary since the caller holds a reference */
121         p = __nf_ct_l3proto_find(l3proto);
122         module_put(p->me);
123 }
124 EXPORT_SYMBOL_GPL(nf_ct_l3proto_module_put);
125
126 static int kill_l3proto(struct nf_conn *i, void *data)
127 {
128         return nf_ct_l3num(i) == ((struct nf_conntrack_l3proto *)data)->l3proto;
129 }
130
131 static int kill_l4proto(struct nf_conn *i, void *data)
132 {
133         struct nf_conntrack_l4proto *l4proto;
134         l4proto = (struct nf_conntrack_l4proto *)data;
135         return nf_ct_protonum(i) == l4proto->l4proto &&
136                nf_ct_l3num(i) == l4proto->l3proto;
137 }
138
139 static int nf_ct_l3proto_register_sysctl(struct nf_conntrack_l3proto *l3proto)
140 {
141         int err = 0;
142
143 #ifdef CONFIG_SYSCTL
144         if (l3proto->ctl_table != NULL) {
145                 err = nf_ct_register_sysctl(&l3proto->ctl_table_header,
146                                             l3proto->ctl_table_path,
147                                             l3proto->ctl_table, NULL);
148         }
149 #endif
150         return err;
151 }
152
153 static void nf_ct_l3proto_unregister_sysctl(struct nf_conntrack_l3proto *l3proto)
154 {
155 #ifdef CONFIG_SYSCTL
156         if (l3proto->ctl_table_header != NULL)
157                 nf_ct_unregister_sysctl(&l3proto->ctl_table_header,
158                                         l3proto->ctl_table, NULL);
159 #endif
160 }
161
162 int nf_conntrack_l3proto_register(struct nf_conntrack_l3proto *proto)
163 {
164         int ret = 0;
165
166         if (proto->l3proto >= AF_MAX)
167                 return -EBUSY;
168
169         if (proto->tuple_to_nlattr && !proto->nlattr_tuple_size)
170                 return -EINVAL;
171
172         mutex_lock(&nf_ct_proto_mutex);
173         if (nf_ct_l3protos[proto->l3proto] != &nf_conntrack_l3proto_generic) {
174                 ret = -EBUSY;
175                 goto out_unlock;
176         }
177
178         ret = nf_ct_l3proto_register_sysctl(proto);
179         if (ret < 0)
180                 goto out_unlock;
181
182         if (proto->nlattr_tuple_size)
183                 proto->nla_size = 3 * proto->nlattr_tuple_size();
184
185         rcu_assign_pointer(nf_ct_l3protos[proto->l3proto], proto);
186
187 out_unlock:
188         mutex_unlock(&nf_ct_proto_mutex);
189         return ret;
190 }
191 EXPORT_SYMBOL_GPL(nf_conntrack_l3proto_register);
192
193 void nf_conntrack_l3proto_unregister(struct nf_conntrack_l3proto *proto)
194 {
195         struct net *net;
196
197         BUG_ON(proto->l3proto >= AF_MAX);
198
199         mutex_lock(&nf_ct_proto_mutex);
200         BUG_ON(nf_ct_l3protos[proto->l3proto] != proto);
201         rcu_assign_pointer(nf_ct_l3protos[proto->l3proto],
202                            &nf_conntrack_l3proto_generic);
203         nf_ct_l3proto_unregister_sysctl(proto);
204         mutex_unlock(&nf_ct_proto_mutex);
205
206         synchronize_rcu();
207
208         /* Remove all contrack entries for this protocol */
209         rtnl_lock();
210         for_each_net(net)
211                 nf_ct_iterate_cleanup(net, kill_l3proto, proto);
212         rtnl_unlock();
213 }
214 EXPORT_SYMBOL_GPL(nf_conntrack_l3proto_unregister);
215
216 static int nf_ct_l4proto_register_sysctl(struct nf_conntrack_l4proto *l4proto)
217 {
218         int err = 0;
219
220 #ifdef CONFIG_SYSCTL
221         if (l4proto->ctl_table != NULL) {
222                 err = nf_ct_register_sysctl(l4proto->ctl_table_header,
223                                             nf_net_netfilter_sysctl_path,
224                                             l4proto->ctl_table,
225                                             l4proto->ctl_table_users);
226                 if (err < 0)
227                         goto out;
228         }
229 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
230         if (l4proto->ctl_compat_table != NULL) {
231                 err = nf_ct_register_sysctl(&l4proto->ctl_compat_table_header,
232                                             nf_net_ipv4_netfilter_sysctl_path,
233                                             l4proto->ctl_compat_table, NULL);
234                 if (err == 0)
235                         goto out;
236                 nf_ct_unregister_sysctl(l4proto->ctl_table_header,
237                                         l4proto->ctl_table,
238                                         l4proto->ctl_table_users);
239         }
240 #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
241 out:
242 #endif /* CONFIG_SYSCTL */
243         return err;
244 }
245
246 static void nf_ct_l4proto_unregister_sysctl(struct nf_conntrack_l4proto *l4proto)
247 {
248 #ifdef CONFIG_SYSCTL
249         if (l4proto->ctl_table_header != NULL &&
250             *l4proto->ctl_table_header != NULL)
251                 nf_ct_unregister_sysctl(l4proto->ctl_table_header,
252                                         l4proto->ctl_table,
253                                         l4proto->ctl_table_users);
254 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
255         if (l4proto->ctl_compat_table_header != NULL)
256                 nf_ct_unregister_sysctl(&l4proto->ctl_compat_table_header,
257                                         l4proto->ctl_compat_table, NULL);
258 #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
259 #endif /* CONFIG_SYSCTL */
260 }
261
262 /* FIXME: Allow NULL functions and sub in pointers to generic for
263    them. --RR */
264 int nf_conntrack_l4proto_register(struct nf_conntrack_l4proto *l4proto)
265 {
266         int ret = 0;
267
268         if (l4proto->l3proto >= PF_MAX)
269                 return -EBUSY;
270
271         if ((l4proto->to_nlattr && !l4proto->nlattr_size)
272                 || (l4proto->tuple_to_nlattr && !l4proto->nlattr_tuple_size))
273                 return -EINVAL;
274
275         mutex_lock(&nf_ct_proto_mutex);
276         if (!nf_ct_protos[l4proto->l3proto]) {
277                 /* l3proto may be loaded latter. */
278                 struct nf_conntrack_l4proto **proto_array;
279                 int i;
280
281                 proto_array = kmalloc(MAX_NF_CT_PROTO *
282                                       sizeof(struct nf_conntrack_l4proto *),
283                                       GFP_KERNEL);
284                 if (proto_array == NULL) {
285                         ret = -ENOMEM;
286                         goto out_unlock;
287                 }
288
289                 for (i = 0; i < MAX_NF_CT_PROTO; i++)
290                         proto_array[i] = &nf_conntrack_l4proto_generic;
291                 nf_ct_protos[l4proto->l3proto] = proto_array;
292         } else if (nf_ct_protos[l4proto->l3proto][l4proto->l4proto] !=
293                                         &nf_conntrack_l4proto_generic) {
294                 ret = -EBUSY;
295                 goto out_unlock;
296         }
297
298         ret = nf_ct_l4proto_register_sysctl(l4proto);
299         if (ret < 0)
300                 goto out_unlock;
301
302         l4proto->nla_size = 0;
303         if (l4proto->nlattr_size)
304                 l4proto->nla_size += l4proto->nlattr_size();
305         if (l4proto->nlattr_tuple_size)
306                 l4proto->nla_size += 3 * l4proto->nlattr_tuple_size();
307
308         rcu_assign_pointer(nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
309                            l4proto);
310
311 out_unlock:
312         mutex_unlock(&nf_ct_proto_mutex);
313         return ret;
314 }
315 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_register);
316
317 void nf_conntrack_l4proto_unregister(struct nf_conntrack_l4proto *l4proto)
318 {
319         struct net *net;
320
321         BUG_ON(l4proto->l3proto >= PF_MAX);
322
323         mutex_lock(&nf_ct_proto_mutex);
324         BUG_ON(nf_ct_protos[l4proto->l3proto][l4proto->l4proto] != l4proto);
325         rcu_assign_pointer(nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
326                            &nf_conntrack_l4proto_generic);
327         nf_ct_l4proto_unregister_sysctl(l4proto);
328         mutex_unlock(&nf_ct_proto_mutex);
329
330         synchronize_rcu();
331
332         /* Remove all contrack entries for this protocol */
333         rtnl_lock();
334         for_each_net(net)
335                 nf_ct_iterate_cleanup(net, kill_l4proto, l4proto);
336         rtnl_unlock();
337 }
338 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_unregister);
339
340 int nf_conntrack_proto_init(void)
341 {
342         unsigned int i;
343         int err;
344
345         err = nf_ct_l4proto_register_sysctl(&nf_conntrack_l4proto_generic);
346         if (err < 0)
347                 return err;
348
349         for (i = 0; i < AF_MAX; i++)
350                 rcu_assign_pointer(nf_ct_l3protos[i],
351                                    &nf_conntrack_l3proto_generic);
352         return 0;
353 }
354
355 void nf_conntrack_proto_fini(void)
356 {
357         unsigned int i;
358
359         nf_ct_l4proto_unregister_sysctl(&nf_conntrack_l4proto_generic);
360
361         /* free l3proto protocol tables */
362         for (i = 0; i < PF_MAX; i++)
363                 kfree(nf_ct_protos[i]);
364 }