]>
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> | |
f229f6ce | 6 | * (C) 2006-2012 Patrick McHardy <kaber@trash.net> |
8f03dea5 MJ |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License version 2 as | |
10 | * published by the Free Software Foundation. | |
11 | */ | |
12 | ||
13 | #include <linux/types.h> | |
14 | #include <linux/netfilter.h> | |
15 | #include <linux/module.h> | |
5a0e3ad6 | 16 | #include <linux/slab.h> |
d62f9ed4 | 17 | #include <linux/mutex.h> |
8f03dea5 MJ |
18 | #include <linux/vmalloc.h> |
19 | #include <linux/stddef.h> | |
20 | #include <linux/err.h> | |
21 | #include <linux/percpu.h> | |
8f03dea5 MJ |
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> | |
605dcad6 | 28 | #include <net/netfilter/nf_conntrack_l4proto.h> |
8f03dea5 MJ |
29 | #include <net/netfilter/nf_conntrack_core.h> |
30 | ||
0906a372 AB |
31 | static struct nf_conntrack_l4proto __rcu **nf_ct_protos[PF_MAX] __read_mostly; |
32 | struct nf_conntrack_l3proto __rcu *nf_ct_l3protos[AF_MAX] __read_mostly; | |
13b18339 | 33 | EXPORT_SYMBOL_GPL(nf_ct_l3protos); |
8f03dea5 | 34 | |
b19caa0c | 35 | static DEFINE_MUTEX(nf_ct_proto_mutex); |
d62f9ed4 | 36 | |
b19caa0c | 37 | #ifdef CONFIG_SYSCTL |
d62f9ed4 | 38 | static int |
2c352f44 G |
39 | nf_ct_register_sysctl(struct net *net, |
40 | struct ctl_table_header **header, | |
41 | const char *path, | |
fa34fff5 | 42 | struct ctl_table *table) |
d62f9ed4 PM |
43 | { |
44 | if (*header == NULL) { | |
2c352f44 | 45 | *header = register_net_sysctl(net, path, table); |
d62f9ed4 PM |
46 | if (*header == NULL) |
47 | return -ENOMEM; | |
48 | } | |
2c352f44 | 49 | |
d62f9ed4 PM |
50 | return 0; |
51 | } | |
52 | ||
53 | static void | |
54 | nf_ct_unregister_sysctl(struct ctl_table_header **header, | |
2c352f44 | 55 | struct ctl_table **table, |
fa34fff5 | 56 | unsigned int users) |
d62f9ed4 | 57 | { |
fa34fff5 | 58 | if (users > 0) |
d62f9ed4 | 59 | return; |
b3fd3ffe | 60 | |
5dd3df10 | 61 | unregister_net_sysctl_table(*header); |
2c352f44 | 62 | kfree(*table); |
d62f9ed4 | 63 | *header = NULL; |
2c352f44 | 64 | *table = NULL; |
d62f9ed4 PM |
65 | } |
66 | #endif | |
67 | ||
605dcad6 MJ |
68 | struct nf_conntrack_l4proto * |
69 | __nf_ct_l4proto_find(u_int16_t l3proto, u_int8_t l4proto) | |
8f03dea5 MJ |
70 | { |
71 | if (unlikely(l3proto >= AF_MAX || nf_ct_protos[l3proto] == NULL)) | |
605dcad6 | 72 | return &nf_conntrack_l4proto_generic; |
8f03dea5 | 73 | |
923f4902 | 74 | return rcu_dereference(nf_ct_protos[l3proto][l4proto]); |
8f03dea5 | 75 | } |
13b18339 | 76 | EXPORT_SYMBOL_GPL(__nf_ct_l4proto_find); |
8f03dea5 MJ |
77 | |
78 | /* this is guaranteed to always return a valid protocol helper, since | |
79 | * it falls back to generic_protocol */ | |
8f03dea5 MJ |
80 | struct nf_conntrack_l3proto * |
81 | nf_ct_l3proto_find_get(u_int16_t l3proto) | |
82 | { | |
83 | struct nf_conntrack_l3proto *p; | |
84 | ||
923f4902 | 85 | rcu_read_lock(); |
8f03dea5 MJ |
86 | p = __nf_ct_l3proto_find(l3proto); |
87 | if (!try_module_get(p->me)) | |
605dcad6 | 88 | p = &nf_conntrack_l3proto_generic; |
923f4902 | 89 | rcu_read_unlock(); |
8f03dea5 MJ |
90 | |
91 | return p; | |
92 | } | |
13b18339 | 93 | EXPORT_SYMBOL_GPL(nf_ct_l3proto_find_get); |
8f03dea5 | 94 | |
8f03dea5 MJ |
95 | int |
96 | nf_ct_l3proto_try_module_get(unsigned short l3proto) | |
97 | { | |
98 | int ret; | |
99 | struct nf_conntrack_l3proto *p; | |
100 | ||
101 | retry: p = nf_ct_l3proto_find_get(l3proto); | |
605dcad6 | 102 | if (p == &nf_conntrack_l3proto_generic) { |
8f03dea5 MJ |
103 | ret = request_module("nf_conntrack-%d", l3proto); |
104 | if (!ret) | |
105 | goto retry; | |
106 | ||
107 | return -EPROTOTYPE; | |
108 | } | |
109 | ||
110 | return 0; | |
111 | } | |
13b18339 | 112 | EXPORT_SYMBOL_GPL(nf_ct_l3proto_try_module_get); |
8f03dea5 MJ |
113 | |
114 | void nf_ct_l3proto_module_put(unsigned short l3proto) | |
115 | { | |
116 | struct nf_conntrack_l3proto *p; | |
117 | ||
3b254c54 PM |
118 | /* rcu_read_lock not necessary since the caller holds a reference, but |
119 | * taken anyways to avoid lockdep warnings in __nf_ct_l3proto_find() | |
120 | */ | |
121 | rcu_read_lock(); | |
8f03dea5 | 122 | p = __nf_ct_l3proto_find(l3proto); |
8f03dea5 | 123 | module_put(p->me); |
3b254c54 | 124 | rcu_read_unlock(); |
8f03dea5 | 125 | } |
13b18339 | 126 | EXPORT_SYMBOL_GPL(nf_ct_l3proto_module_put); |
8f03dea5 | 127 | |
c1ebd7df PNA |
128 | struct nf_conntrack_l4proto * |
129 | nf_ct_l4proto_find_get(u_int16_t l3num, u_int8_t l4num) | |
130 | { | |
131 | struct nf_conntrack_l4proto *p; | |
132 | ||
133 | rcu_read_lock(); | |
134 | p = __nf_ct_l4proto_find(l3num, l4num); | |
135 | if (!try_module_get(p->me)) | |
136 | p = &nf_conntrack_l4proto_generic; | |
137 | rcu_read_unlock(); | |
138 | ||
139 | return p; | |
140 | } | |
141 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_find_get); | |
142 | ||
143 | void nf_ct_l4proto_put(struct nf_conntrack_l4proto *p) | |
144 | { | |
145 | module_put(p->me); | |
146 | } | |
147 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_put); | |
148 | ||
8f03dea5 MJ |
149 | static int kill_l3proto(struct nf_conn *i, void *data) |
150 | { | |
5e8fbe2a | 151 | return nf_ct_l3num(i) == ((struct nf_conntrack_l3proto *)data)->l3proto; |
8f03dea5 MJ |
152 | } |
153 | ||
605dcad6 | 154 | static int kill_l4proto(struct nf_conn *i, void *data) |
8f03dea5 | 155 | { |
605dcad6 MJ |
156 | struct nf_conntrack_l4proto *l4proto; |
157 | l4proto = (struct nf_conntrack_l4proto *)data; | |
5e8fbe2a PM |
158 | return nf_ct_protonum(i) == l4proto->l4proto && |
159 | nf_ct_l3num(i) == l4proto->l3proto; | |
8f03dea5 MJ |
160 | } |
161 | ||
6330750d | 162 | int nf_ct_l3proto_register(struct nf_conntrack_l3proto *proto) |
8f03dea5 MJ |
163 | { |
164 | int ret = 0; | |
0e60ebe0 | 165 | struct nf_conntrack_l3proto *old; |
8f03dea5 | 166 | |
0661cca9 PM |
167 | if (proto->l3proto >= AF_MAX) |
168 | return -EBUSY; | |
ae5718fb | 169 | |
d0dba725 HE |
170 | if (proto->tuple_to_nlattr && !proto->nlattr_tuple_size) |
171 | return -EINVAL; | |
172 | ||
b19caa0c | 173 | mutex_lock(&nf_ct_proto_mutex); |
0e60ebe0 ED |
174 | old = rcu_dereference_protected(nf_ct_l3protos[proto->l3proto], |
175 | lockdep_is_held(&nf_ct_proto_mutex)); | |
176 | if (old != &nf_conntrack_l3proto_generic) { | |
8f03dea5 | 177 | ret = -EBUSY; |
ae5718fb | 178 | goto out_unlock; |
8f03dea5 | 179 | } |
d62f9ed4 | 180 | |
d0dba725 HE |
181 | if (proto->nlattr_tuple_size) |
182 | proto->nla_size = 3 * proto->nlattr_tuple_size(); | |
183 | ||
0661cca9 | 184 | rcu_assign_pointer(nf_ct_l3protos[proto->l3proto], proto); |
8f03dea5 | 185 | |
ae5718fb | 186 | out_unlock: |
b19caa0c | 187 | mutex_unlock(&nf_ct_proto_mutex); |
8f03dea5 | 188 | return ret; |
524a53e5 | 189 | |
8f03dea5 | 190 | } |
6330750d | 191 | EXPORT_SYMBOL_GPL(nf_ct_l3proto_register); |
8f03dea5 | 192 | |
6330750d | 193 | int nf_ct_l3proto_pernet_register(struct net *net, |
524a53e5 | 194 | struct nf_conntrack_l3proto *proto) |
8f03dea5 | 195 | { |
adf05168 | 196 | int ret; |
524a53e5 | 197 | |
fa0f61f0 G |
198 | if (proto->init_net) { |
199 | ret = proto->init_net(net); | |
200 | if (ret < 0) | |
201 | return ret; | |
202 | } | |
524a53e5 | 203 | |
adf05168 | 204 | return 0; |
524a53e5 | 205 | } |
6330750d | 206 | EXPORT_SYMBOL_GPL(nf_ct_l3proto_pernet_register); |
678d6675 | 207 | |
6330750d | 208 | void nf_ct_l3proto_unregister(struct nf_conntrack_l3proto *proto) |
524a53e5 | 209 | { |
fe3eb20c | 210 | BUG_ON(proto->l3proto >= AF_MAX); |
ae5718fb | 211 | |
b19caa0c | 212 | mutex_lock(&nf_ct_proto_mutex); |
0e60ebe0 ED |
213 | BUG_ON(rcu_dereference_protected(nf_ct_l3protos[proto->l3proto], |
214 | lockdep_is_held(&nf_ct_proto_mutex) | |
215 | ) != proto); | |
923f4902 PM |
216 | rcu_assign_pointer(nf_ct_l3protos[proto->l3proto], |
217 | &nf_conntrack_l3proto_generic); | |
b19caa0c | 218 | mutex_unlock(&nf_ct_proto_mutex); |
8f03dea5 | 219 | |
0661cca9 | 220 | synchronize_rcu(); |
524a53e5 | 221 | } |
6330750d | 222 | EXPORT_SYMBOL_GPL(nf_ct_l3proto_unregister); |
524a53e5 | 223 | |
6330750d | 224 | void nf_ct_l3proto_pernet_unregister(struct net *net, |
524a53e5 G |
225 | struct nf_conntrack_l3proto *proto) |
226 | { | |
8f03dea5 | 227 | /* Remove all contrack entries for this protocol */ |
c655bc68 | 228 | nf_ct_iterate_cleanup(net, kill_l3proto, proto, 0, 0); |
8f03dea5 | 229 | } |
6330750d | 230 | EXPORT_SYMBOL_GPL(nf_ct_l3proto_pernet_unregister); |
8f03dea5 | 231 | |
2c352f44 G |
232 | static struct nf_proto_net *nf_ct_l4proto_net(struct net *net, |
233 | struct nf_conntrack_l4proto *l4proto) | |
234 | { | |
08911475 PNA |
235 | if (l4proto->get_net_proto) { |
236 | /* statically built-in protocols use static per-net */ | |
237 | return l4proto->get_net_proto(net); | |
238 | } else if (l4proto->net_id) { | |
239 | /* ... and loadable protocols use dynamic per-net */ | |
240 | return net_generic(net, *l4proto->net_id); | |
15f585bd G |
241 | } |
242 | return NULL; | |
2c352f44 G |
243 | } |
244 | ||
245 | static | |
246 | int nf_ct_l4proto_register_sysctl(struct net *net, | |
fa34fff5 | 247 | struct nf_proto_net *pn, |
2c352f44 | 248 | struct nf_conntrack_l4proto *l4proto) |
d62f9ed4 PM |
249 | { |
250 | int err = 0; | |
251 | ||
252 | #ifdef CONFIG_SYSCTL | |
2c352f44 G |
253 | if (pn->ctl_table != NULL) { |
254 | err = nf_ct_register_sysctl(net, | |
255 | &pn->ctl_table_header, | |
f99e8f71 | 256 | "net/netfilter", |
fa34fff5 | 257 | pn->ctl_table); |
2c352f44 G |
258 | if (err < 0) { |
259 | if (!pn->users) { | |
260 | kfree(pn->ctl_table); | |
261 | pn->ctl_table = NULL; | |
262 | } | |
2c352f44 | 263 | } |
d62f9ed4 | 264 | } |
933a41e7 | 265 | #endif /* CONFIG_SYSCTL */ |
d62f9ed4 PM |
266 | return err; |
267 | } | |
268 | ||
2c352f44 G |
269 | static |
270 | void nf_ct_l4proto_unregister_sysctl(struct net *net, | |
fa34fff5 | 271 | struct nf_proto_net *pn, |
2c352f44 | 272 | struct nf_conntrack_l4proto *l4proto) |
d62f9ed4 PM |
273 | { |
274 | #ifdef CONFIG_SYSCTL | |
2c352f44 G |
275 | if (pn->ctl_table_header != NULL) |
276 | nf_ct_unregister_sysctl(&pn->ctl_table_header, | |
277 | &pn->ctl_table, | |
fa34fff5 | 278 | pn->users); |
933a41e7 | 279 | #endif /* CONFIG_SYSCTL */ |
d62f9ed4 PM |
280 | } |
281 | ||
8f03dea5 MJ |
282 | /* FIXME: Allow NULL functions and sub in pointers to generic for |
283 | them. --RR */ | |
c296bb4d | 284 | int nf_ct_l4proto_register(struct nf_conntrack_l4proto *l4proto) |
8f03dea5 MJ |
285 | { |
286 | int ret = 0; | |
287 | ||
0661cca9 PM |
288 | if (l4proto->l3proto >= PF_MAX) |
289 | return -EBUSY; | |
ae5718fb | 290 | |
d0dba725 HE |
291 | if ((l4proto->to_nlattr && !l4proto->nlattr_size) |
292 | || (l4proto->tuple_to_nlattr && !l4proto->nlattr_tuple_size)) | |
293 | return -EINVAL; | |
294 | ||
b19caa0c | 295 | mutex_lock(&nf_ct_proto_mutex); |
c6a1e615 | 296 | if (!nf_ct_protos[l4proto->l3proto]) { |
8f03dea5 | 297 | /* l3proto may be loaded latter. */ |
c5d277d2 | 298 | struct nf_conntrack_l4proto __rcu **proto_array; |
8f03dea5 MJ |
299 | int i; |
300 | ||
c6a1e615 PM |
301 | proto_array = kmalloc(MAX_NF_CT_PROTO * |
302 | sizeof(struct nf_conntrack_l4proto *), | |
303 | GFP_KERNEL); | |
8f03dea5 MJ |
304 | if (proto_array == NULL) { |
305 | ret = -ENOMEM; | |
b19caa0c | 306 | goto out_unlock; |
8f03dea5 | 307 | } |
c6a1e615 | 308 | |
8f03dea5 | 309 | for (i = 0; i < MAX_NF_CT_PROTO; i++) |
c5d277d2 | 310 | RCU_INIT_POINTER(proto_array[i], &nf_conntrack_l4proto_generic); |
d817d29d ED |
311 | |
312 | /* Before making proto_array visible to lockless readers, | |
313 | * we must make sure its content is committed to memory. | |
314 | */ | |
315 | smp_wmb(); | |
316 | ||
c6a1e615 | 317 | nf_ct_protos[l4proto->l3proto] = proto_array; |
0e60ebe0 ED |
318 | } else if (rcu_dereference_protected( |
319 | nf_ct_protos[l4proto->l3proto][l4proto->l4proto], | |
320 | lockdep_is_held(&nf_ct_proto_mutex) | |
321 | ) != &nf_conntrack_l4proto_generic) { | |
c6a1e615 PM |
322 | ret = -EBUSY; |
323 | goto out_unlock; | |
8f03dea5 MJ |
324 | } |
325 | ||
d0dba725 HE |
326 | l4proto->nla_size = 0; |
327 | if (l4proto->nlattr_size) | |
328 | l4proto->nla_size += l4proto->nlattr_size(); | |
329 | if (l4proto->nlattr_tuple_size) | |
330 | l4proto->nla_size += 3 * l4proto->nlattr_tuple_size(); | |
331 | ||
c6a1e615 PM |
332 | rcu_assign_pointer(nf_ct_protos[l4proto->l3proto][l4proto->l4proto], |
333 | l4proto); | |
8f03dea5 | 334 | out_unlock: |
b19caa0c | 335 | mutex_unlock(&nf_ct_proto_mutex); |
8f03dea5 MJ |
336 | return ret; |
337 | } | |
c296bb4d | 338 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_register); |
8f03dea5 | 339 | |
c296bb4d | 340 | int nf_ct_l4proto_pernet_register(struct net *net, |
2c352f44 | 341 | struct nf_conntrack_l4proto *l4proto) |
8f03dea5 | 342 | { |
2c352f44 | 343 | int ret = 0; |
fa34fff5 | 344 | struct nf_proto_net *pn = NULL; |
2c352f44 | 345 | |
fa0f61f0 | 346 | if (l4proto->init_net) { |
f1caad27 | 347 | ret = l4proto->init_net(net, l4proto->l3proto); |
fa0f61f0 | 348 | if (ret < 0) |
fa34fff5 | 349 | goto out; |
fa0f61f0 | 350 | } |
678d6675 | 351 | |
fa34fff5 G |
352 | pn = nf_ct_l4proto_net(net, l4proto); |
353 | if (pn == NULL) | |
354 | goto out; | |
355 | ||
356 | ret = nf_ct_l4proto_register_sysctl(net, pn, l4proto); | |
2c352f44 | 357 | if (ret < 0) |
fa34fff5 | 358 | goto out; |
2c352f44 | 359 | |
fa34fff5 G |
360 | pn->users++; |
361 | out: | |
fa0f61f0 | 362 | return ret; |
2c352f44 | 363 | } |
c296bb4d | 364 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_register); |
2c352f44 | 365 | |
c296bb4d | 366 | void nf_ct_l4proto_unregister(struct nf_conntrack_l4proto *l4proto) |
2c352f44 | 367 | { |
fe3eb20c | 368 | BUG_ON(l4proto->l3proto >= PF_MAX); |
ae5718fb | 369 | |
b19caa0c | 370 | mutex_lock(&nf_ct_proto_mutex); |
0e60ebe0 ED |
371 | BUG_ON(rcu_dereference_protected( |
372 | nf_ct_protos[l4proto->l3proto][l4proto->l4proto], | |
373 | lockdep_is_held(&nf_ct_proto_mutex) | |
374 | ) != l4proto); | |
923f4902 PM |
375 | rcu_assign_pointer(nf_ct_protos[l4proto->l3proto][l4proto->l4proto], |
376 | &nf_conntrack_l4proto_generic); | |
b19caa0c | 377 | mutex_unlock(&nf_ct_proto_mutex); |
8f03dea5 | 378 | |
0661cca9 | 379 | synchronize_rcu(); |
2c352f44 | 380 | } |
c296bb4d | 381 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_unregister); |
d62f9ed4 | 382 | |
c296bb4d | 383 | void nf_ct_l4proto_pernet_unregister(struct net *net, |
2c352f44 G |
384 | struct nf_conntrack_l4proto *l4proto) |
385 | { | |
fa34fff5 G |
386 | struct nf_proto_net *pn = NULL; |
387 | ||
fa34fff5 G |
388 | pn = nf_ct_l4proto_net(net, l4proto); |
389 | if (pn == NULL) | |
390 | return; | |
391 | ||
392 | pn->users--; | |
393 | nf_ct_l4proto_unregister_sysctl(net, pn, l4proto); | |
394 | ||
8f03dea5 | 395 | /* Remove all contrack entries for this protocol */ |
c655bc68 | 396 | nf_ct_iterate_cleanup(net, kill_l4proto, l4proto, 0, 0); |
8f03dea5 | 397 | } |
c296bb4d | 398 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_unregister); |
ac5357eb | 399 | |
04d87001 | 400 | int nf_conntrack_proto_pernet_init(struct net *net) |
ac5357eb | 401 | { |
ac5357eb | 402 | int err; |
fa34fff5 G |
403 | struct nf_proto_net *pn = nf_ct_l4proto_net(net, |
404 | &nf_conntrack_l4proto_generic); | |
405 | ||
f1caad27 G |
406 | err = nf_conntrack_l4proto_generic.init_net(net, |
407 | nf_conntrack_l4proto_generic.l3proto); | |
15f585bd G |
408 | if (err < 0) |
409 | return err; | |
410 | err = nf_ct_l4proto_register_sysctl(net, | |
fa34fff5 | 411 | pn, |
15f585bd | 412 | &nf_conntrack_l4proto_generic); |
ac5357eb PM |
413 | if (err < 0) |
414 | return err; | |
415 | ||
fa34fff5 | 416 | pn->users++; |
ac5357eb PM |
417 | return 0; |
418 | } | |
419 | ||
04d87001 | 420 | void nf_conntrack_proto_pernet_fini(struct net *net) |
ac5357eb | 421 | { |
fa34fff5 G |
422 | struct nf_proto_net *pn = nf_ct_l4proto_net(net, |
423 | &nf_conntrack_l4proto_generic); | |
424 | ||
425 | pn->users--; | |
15f585bd | 426 | nf_ct_l4proto_unregister_sysctl(net, |
fa34fff5 | 427 | pn, |
15f585bd | 428 | &nf_conntrack_l4proto_generic); |
04d87001 G |
429 | } |
430 | ||
431 | int nf_conntrack_proto_init(void) | |
432 | { | |
433 | unsigned int i; | |
434 | for (i = 0; i < AF_MAX; i++) | |
435 | rcu_assign_pointer(nf_ct_l3protos[i], | |
436 | &nf_conntrack_l3proto_generic); | |
437 | return 0; | |
438 | } | |
439 | ||
440 | void nf_conntrack_proto_fini(void) | |
441 | { | |
442 | unsigned int i; | |
443 | /* free l3proto protocol tables */ | |
444 | for (i = 0; i < PF_MAX; i++) | |
445 | kfree(nf_ct_protos[i]); | |
ac5357eb | 446 | } |