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