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