]>
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 | ||
b7b5fda4 FW |
31 | static struct nf_conntrack_l4proto __rcu **nf_ct_protos[NFPROTO_NUMPROTO] __read_mostly; |
32 | struct nf_conntrack_l3proto __rcu *nf_ct_l3protos[NFPROTO_NUMPROTO] __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 | 70 | { |
b7b5fda4 | 71 | if (unlikely(l3proto >= NFPROTO_NUMPROTO || 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 | |
ecb2421b FW |
128 | int nf_ct_netns_get(struct net *net, u8 nfproto) |
129 | { | |
0c66dc1e FW |
130 | const struct nf_conntrack_l3proto *l3proto; |
131 | int ret; | |
132 | ||
133 | might_sleep(); | |
134 | ||
135 | ret = nf_ct_l3proto_try_module_get(nfproto); | |
136 | if (ret < 0) | |
137 | return ret; | |
138 | ||
139 | /* we already have a reference, can't fail */ | |
140 | rcu_read_lock(); | |
141 | l3proto = __nf_ct_l3proto_find(nfproto); | |
142 | rcu_read_unlock(); | |
143 | ||
144 | if (!l3proto->net_ns_get) | |
145 | return 0; | |
146 | ||
147 | ret = l3proto->net_ns_get(net); | |
148 | if (ret < 0) | |
149 | nf_ct_l3proto_module_put(nfproto); | |
150 | ||
151 | return ret; | |
ecb2421b FW |
152 | } |
153 | EXPORT_SYMBOL_GPL(nf_ct_netns_get); | |
154 | ||
155 | void nf_ct_netns_put(struct net *net, u8 nfproto) | |
156 | { | |
0c66dc1e FW |
157 | const struct nf_conntrack_l3proto *l3proto; |
158 | ||
159 | might_sleep(); | |
160 | ||
161 | /* same as nf_conntrack_netns_get(), reference assumed */ | |
162 | rcu_read_lock(); | |
163 | l3proto = __nf_ct_l3proto_find(nfproto); | |
164 | rcu_read_unlock(); | |
165 | ||
166 | if (WARN_ON(!l3proto)) | |
167 | return; | |
168 | ||
169 | if (l3proto->net_ns_put) | |
170 | l3proto->net_ns_put(net); | |
171 | ||
ecb2421b FW |
172 | nf_ct_l3proto_module_put(nfproto); |
173 | } | |
174 | EXPORT_SYMBOL_GPL(nf_ct_netns_put); | |
175 | ||
c1ebd7df PNA |
176 | struct nf_conntrack_l4proto * |
177 | nf_ct_l4proto_find_get(u_int16_t l3num, u_int8_t l4num) | |
178 | { | |
179 | struct nf_conntrack_l4proto *p; | |
180 | ||
181 | rcu_read_lock(); | |
182 | p = __nf_ct_l4proto_find(l3num, l4num); | |
183 | if (!try_module_get(p->me)) | |
184 | p = &nf_conntrack_l4proto_generic; | |
185 | rcu_read_unlock(); | |
186 | ||
187 | return p; | |
188 | } | |
189 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_find_get); | |
190 | ||
191 | void nf_ct_l4proto_put(struct nf_conntrack_l4proto *p) | |
192 | { | |
193 | module_put(p->me); | |
194 | } | |
195 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_put); | |
196 | ||
8f03dea5 MJ |
197 | static int kill_l3proto(struct nf_conn *i, void *data) |
198 | { | |
5e8fbe2a | 199 | return nf_ct_l3num(i) == ((struct nf_conntrack_l3proto *)data)->l3proto; |
8f03dea5 MJ |
200 | } |
201 | ||
605dcad6 | 202 | static int kill_l4proto(struct nf_conn *i, void *data) |
8f03dea5 | 203 | { |
605dcad6 | 204 | struct nf_conntrack_l4proto *l4proto; |
68ad546a | 205 | l4proto = data; |
5e8fbe2a PM |
206 | return nf_ct_protonum(i) == l4proto->l4proto && |
207 | nf_ct_l3num(i) == l4proto->l3proto; | |
8f03dea5 MJ |
208 | } |
209 | ||
6330750d | 210 | int nf_ct_l3proto_register(struct nf_conntrack_l3proto *proto) |
8f03dea5 MJ |
211 | { |
212 | int ret = 0; | |
0e60ebe0 | 213 | struct nf_conntrack_l3proto *old; |
8f03dea5 | 214 | |
b7b5fda4 | 215 | if (proto->l3proto >= NFPROTO_NUMPROTO) |
0661cca9 | 216 | return -EBUSY; |
ae5718fb | 217 | |
d0dba725 HE |
218 | if (proto->tuple_to_nlattr && !proto->nlattr_tuple_size) |
219 | return -EINVAL; | |
220 | ||
b19caa0c | 221 | mutex_lock(&nf_ct_proto_mutex); |
0e60ebe0 ED |
222 | old = rcu_dereference_protected(nf_ct_l3protos[proto->l3proto], |
223 | lockdep_is_held(&nf_ct_proto_mutex)); | |
224 | if (old != &nf_conntrack_l3proto_generic) { | |
8f03dea5 | 225 | ret = -EBUSY; |
ae5718fb | 226 | goto out_unlock; |
8f03dea5 | 227 | } |
d62f9ed4 | 228 | |
d0dba725 HE |
229 | if (proto->nlattr_tuple_size) |
230 | proto->nla_size = 3 * proto->nlattr_tuple_size(); | |
231 | ||
0661cca9 | 232 | rcu_assign_pointer(nf_ct_l3protos[proto->l3proto], proto); |
8f03dea5 | 233 | |
ae5718fb | 234 | out_unlock: |
b19caa0c | 235 | mutex_unlock(&nf_ct_proto_mutex); |
8f03dea5 | 236 | return ret; |
524a53e5 | 237 | |
8f03dea5 | 238 | } |
6330750d | 239 | EXPORT_SYMBOL_GPL(nf_ct_l3proto_register); |
8f03dea5 | 240 | |
481fa373 FW |
241 | #ifdef CONFIG_SYSCTL |
242 | extern unsigned int nf_conntrack_default_on; | |
243 | ||
6330750d | 244 | int nf_ct_l3proto_pernet_register(struct net *net, |
524a53e5 | 245 | struct nf_conntrack_l3proto *proto) |
8f03dea5 | 246 | { |
481fa373 FW |
247 | if (nf_conntrack_default_on == 0) |
248 | return 0; | |
249 | ||
250 | return proto->net_ns_get ? proto->net_ns_get(net) : 0; | |
524a53e5 | 251 | } |
6330750d | 252 | EXPORT_SYMBOL_GPL(nf_ct_l3proto_pernet_register); |
481fa373 | 253 | #endif |
678d6675 | 254 | |
6330750d | 255 | void nf_ct_l3proto_unregister(struct nf_conntrack_l3proto *proto) |
524a53e5 | 256 | { |
b7b5fda4 | 257 | BUG_ON(proto->l3proto >= NFPROTO_NUMPROTO); |
ae5718fb | 258 | |
b19caa0c | 259 | mutex_lock(&nf_ct_proto_mutex); |
0e60ebe0 ED |
260 | BUG_ON(rcu_dereference_protected(nf_ct_l3protos[proto->l3proto], |
261 | lockdep_is_held(&nf_ct_proto_mutex) | |
262 | ) != proto); | |
923f4902 PM |
263 | rcu_assign_pointer(nf_ct_l3protos[proto->l3proto], |
264 | &nf_conntrack_l3proto_generic); | |
b19caa0c | 265 | mutex_unlock(&nf_ct_proto_mutex); |
8f03dea5 | 266 | |
0661cca9 | 267 | synchronize_rcu(); |
2c41f33c FW |
268 | /* Remove all contrack entries for this protocol */ |
269 | nf_ct_iterate_destroy(kill_l3proto, proto); | |
524a53e5 | 270 | } |
6330750d | 271 | EXPORT_SYMBOL_GPL(nf_ct_l3proto_unregister); |
524a53e5 | 272 | |
6330750d | 273 | void nf_ct_l3proto_pernet_unregister(struct net *net, |
524a53e5 G |
274 | struct nf_conntrack_l3proto *proto) |
275 | { | |
481fa373 FW |
276 | /* |
277 | * nf_conntrack_default_on *might* have registered hooks. | |
278 | * ->net_ns_put must cope with more puts() than get(), i.e. | |
279 | * if nf_conntrack_default_on was 0 at time of | |
280 | * nf_ct_l3proto_pernet_register invocation this net_ns_put() | |
281 | * should be a noop. | |
282 | */ | |
283 | if (proto->net_ns_put) | |
284 | proto->net_ns_put(net); | |
8f03dea5 | 285 | } |
6330750d | 286 | EXPORT_SYMBOL_GPL(nf_ct_l3proto_pernet_unregister); |
8f03dea5 | 287 | |
2c352f44 G |
288 | static struct nf_proto_net *nf_ct_l4proto_net(struct net *net, |
289 | struct nf_conntrack_l4proto *l4proto) | |
290 | { | |
08911475 PNA |
291 | if (l4proto->get_net_proto) { |
292 | /* statically built-in protocols use static per-net */ | |
293 | return l4proto->get_net_proto(net); | |
294 | } else if (l4proto->net_id) { | |
295 | /* ... and loadable protocols use dynamic per-net */ | |
296 | return net_generic(net, *l4proto->net_id); | |
15f585bd G |
297 | } |
298 | return NULL; | |
2c352f44 G |
299 | } |
300 | ||
301 | static | |
302 | int nf_ct_l4proto_register_sysctl(struct net *net, | |
fa34fff5 | 303 | struct nf_proto_net *pn, |
2c352f44 | 304 | struct nf_conntrack_l4proto *l4proto) |
d62f9ed4 PM |
305 | { |
306 | int err = 0; | |
307 | ||
308 | #ifdef CONFIG_SYSCTL | |
2c352f44 G |
309 | if (pn->ctl_table != NULL) { |
310 | err = nf_ct_register_sysctl(net, | |
311 | &pn->ctl_table_header, | |
f99e8f71 | 312 | "net/netfilter", |
fa34fff5 | 313 | pn->ctl_table); |
2c352f44 G |
314 | if (err < 0) { |
315 | if (!pn->users) { | |
316 | kfree(pn->ctl_table); | |
317 | pn->ctl_table = NULL; | |
318 | } | |
2c352f44 | 319 | } |
d62f9ed4 | 320 | } |
933a41e7 | 321 | #endif /* CONFIG_SYSCTL */ |
d62f9ed4 PM |
322 | return err; |
323 | } | |
324 | ||
2c352f44 G |
325 | static |
326 | void nf_ct_l4proto_unregister_sysctl(struct net *net, | |
fa34fff5 | 327 | struct nf_proto_net *pn, |
2c352f44 | 328 | struct nf_conntrack_l4proto *l4proto) |
d62f9ed4 PM |
329 | { |
330 | #ifdef CONFIG_SYSCTL | |
2c352f44 G |
331 | if (pn->ctl_table_header != NULL) |
332 | nf_ct_unregister_sysctl(&pn->ctl_table_header, | |
333 | &pn->ctl_table, | |
fa34fff5 | 334 | pn->users); |
933a41e7 | 335 | #endif /* CONFIG_SYSCTL */ |
d62f9ed4 PM |
336 | } |
337 | ||
8f03dea5 MJ |
338 | /* FIXME: Allow NULL functions and sub in pointers to generic for |
339 | them. --RR */ | |
0e54d217 | 340 | int nf_ct_l4proto_register_one(struct nf_conntrack_l4proto *l4proto) |
8f03dea5 MJ |
341 | { |
342 | int ret = 0; | |
343 | ||
b7b5fda4 | 344 | if (l4proto->l3proto >= ARRAY_SIZE(nf_ct_protos)) |
0661cca9 | 345 | return -EBUSY; |
ae5718fb | 346 | |
0e54d217 DC |
347 | if ((l4proto->to_nlattr && !l4proto->nlattr_size) || |
348 | (l4proto->tuple_to_nlattr && !l4proto->nlattr_tuple_size)) | |
d0dba725 HE |
349 | return -EINVAL; |
350 | ||
b19caa0c | 351 | mutex_lock(&nf_ct_proto_mutex); |
c6a1e615 | 352 | if (!nf_ct_protos[l4proto->l3proto]) { |
8f03dea5 | 353 | /* l3proto may be loaded latter. */ |
c5d277d2 | 354 | struct nf_conntrack_l4proto __rcu **proto_array; |
8f03dea5 MJ |
355 | int i; |
356 | ||
c6a1e615 PM |
357 | proto_array = kmalloc(MAX_NF_CT_PROTO * |
358 | sizeof(struct nf_conntrack_l4proto *), | |
359 | GFP_KERNEL); | |
8f03dea5 MJ |
360 | if (proto_array == NULL) { |
361 | ret = -ENOMEM; | |
b19caa0c | 362 | goto out_unlock; |
8f03dea5 | 363 | } |
c6a1e615 | 364 | |
8f03dea5 | 365 | for (i = 0; i < MAX_NF_CT_PROTO; i++) |
0e54d217 DC |
366 | RCU_INIT_POINTER(proto_array[i], |
367 | &nf_conntrack_l4proto_generic); | |
d817d29d ED |
368 | |
369 | /* Before making proto_array visible to lockless readers, | |
370 | * we must make sure its content is committed to memory. | |
371 | */ | |
372 | smp_wmb(); | |
373 | ||
c6a1e615 | 374 | nf_ct_protos[l4proto->l3proto] = proto_array; |
0e60ebe0 ED |
375 | } else if (rcu_dereference_protected( |
376 | nf_ct_protos[l4proto->l3proto][l4proto->l4proto], | |
377 | lockdep_is_held(&nf_ct_proto_mutex) | |
378 | ) != &nf_conntrack_l4proto_generic) { | |
c6a1e615 PM |
379 | ret = -EBUSY; |
380 | goto out_unlock; | |
8f03dea5 MJ |
381 | } |
382 | ||
d0dba725 HE |
383 | l4proto->nla_size = 0; |
384 | if (l4proto->nlattr_size) | |
385 | l4proto->nla_size += l4proto->nlattr_size(); | |
386 | if (l4proto->nlattr_tuple_size) | |
387 | l4proto->nla_size += 3 * l4proto->nlattr_tuple_size(); | |
388 | ||
c6a1e615 PM |
389 | rcu_assign_pointer(nf_ct_protos[l4proto->l3proto][l4proto->l4proto], |
390 | l4proto); | |
8f03dea5 | 391 | out_unlock: |
b19caa0c | 392 | mutex_unlock(&nf_ct_proto_mutex); |
8f03dea5 MJ |
393 | return ret; |
394 | } | |
0e54d217 | 395 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_register_one); |
8f03dea5 | 396 | |
0e54d217 DC |
397 | int nf_ct_l4proto_pernet_register_one(struct net *net, |
398 | struct nf_conntrack_l4proto *l4proto) | |
8f03dea5 | 399 | { |
2c352f44 | 400 | int ret = 0; |
fa34fff5 | 401 | struct nf_proto_net *pn = NULL; |
2c352f44 | 402 | |
fa0f61f0 | 403 | if (l4proto->init_net) { |
f1caad27 | 404 | ret = l4proto->init_net(net, l4proto->l3proto); |
fa0f61f0 | 405 | if (ret < 0) |
fa34fff5 | 406 | goto out; |
fa0f61f0 | 407 | } |
678d6675 | 408 | |
fa34fff5 G |
409 | pn = nf_ct_l4proto_net(net, l4proto); |
410 | if (pn == NULL) | |
411 | goto out; | |
412 | ||
413 | ret = nf_ct_l4proto_register_sysctl(net, pn, l4proto); | |
2c352f44 | 414 | if (ret < 0) |
fa34fff5 | 415 | goto out; |
2c352f44 | 416 | |
fa34fff5 G |
417 | pn->users++; |
418 | out: | |
fa0f61f0 | 419 | return ret; |
2c352f44 | 420 | } |
0e54d217 | 421 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_register_one); |
2c352f44 | 422 | |
2c41f33c FW |
423 | static void __nf_ct_l4proto_unregister_one(struct nf_conntrack_l4proto *l4proto) |
424 | ||
2c352f44 | 425 | { |
b7b5fda4 | 426 | BUG_ON(l4proto->l3proto >= ARRAY_SIZE(nf_ct_protos)); |
ae5718fb | 427 | |
0e60ebe0 ED |
428 | BUG_ON(rcu_dereference_protected( |
429 | nf_ct_protos[l4proto->l3proto][l4proto->l4proto], | |
430 | lockdep_is_held(&nf_ct_proto_mutex) | |
431 | ) != l4proto); | |
923f4902 PM |
432 | rcu_assign_pointer(nf_ct_protos[l4proto->l3proto][l4proto->l4proto], |
433 | &nf_conntrack_l4proto_generic); | |
2c41f33c FW |
434 | } |
435 | ||
436 | void nf_ct_l4proto_unregister_one(struct nf_conntrack_l4proto *l4proto) | |
437 | { | |
438 | mutex_lock(&nf_ct_proto_mutex); | |
439 | __nf_ct_l4proto_unregister_one(l4proto); | |
b19caa0c | 440 | mutex_unlock(&nf_ct_proto_mutex); |
8f03dea5 | 441 | |
0661cca9 | 442 | synchronize_rcu(); |
2c352f44 | 443 | } |
0e54d217 | 444 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_unregister_one); |
d62f9ed4 | 445 | |
0e54d217 DC |
446 | void nf_ct_l4proto_pernet_unregister_one(struct net *net, |
447 | struct nf_conntrack_l4proto *l4proto) | |
2c352f44 | 448 | { |
809c2d9a | 449 | struct nf_proto_net *pn = nf_ct_l4proto_net(net, l4proto); |
fa34fff5 | 450 | |
fa34fff5 G |
451 | if (pn == NULL) |
452 | return; | |
453 | ||
454 | pn->users--; | |
455 | nf_ct_l4proto_unregister_sysctl(net, pn, l4proto); | |
8f03dea5 | 456 | } |
0e54d217 DC |
457 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_unregister_one); |
458 | ||
459 | int nf_ct_l4proto_register(struct nf_conntrack_l4proto *l4proto[], | |
460 | unsigned int num_proto) | |
461 | { | |
462 | int ret = -EINVAL, ver; | |
463 | unsigned int i; | |
464 | ||
465 | for (i = 0; i < num_proto; i++) { | |
466 | ret = nf_ct_l4proto_register_one(l4proto[i]); | |
467 | if (ret < 0) | |
468 | break; | |
469 | } | |
470 | if (i != num_proto) { | |
471 | ver = l4proto[i]->l3proto == PF_INET6 ? 6 : 4; | |
472 | pr_err("nf_conntrack_ipv%d: can't register %s%d proto.\n", | |
473 | ver, l4proto[i]->name, ver); | |
474 | nf_ct_l4proto_unregister(l4proto, i); | |
475 | } | |
476 | return ret; | |
477 | } | |
478 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_register); | |
479 | ||
480 | int nf_ct_l4proto_pernet_register(struct net *net, | |
481 | struct nf_conntrack_l4proto *l4proto[], | |
482 | unsigned int num_proto) | |
483 | { | |
484 | int ret = -EINVAL; | |
485 | unsigned int i; | |
486 | ||
487 | for (i = 0; i < num_proto; i++) { | |
488 | ret = nf_ct_l4proto_pernet_register_one(net, l4proto[i]); | |
489 | if (ret < 0) | |
490 | break; | |
491 | } | |
492 | if (i != num_proto) { | |
493 | pr_err("nf_conntrack_%s%d: pernet registration failed\n", | |
494 | l4proto[i]->name, | |
495 | l4proto[i]->l3proto == PF_INET6 ? 6 : 4); | |
496 | nf_ct_l4proto_pernet_unregister(net, l4proto, i); | |
497 | } | |
498 | return ret; | |
499 | } | |
500 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_register); | |
501 | ||
502 | void nf_ct_l4proto_unregister(struct nf_conntrack_l4proto *l4proto[], | |
503 | unsigned int num_proto) | |
504 | { | |
2c41f33c | 505 | mutex_lock(&nf_ct_proto_mutex); |
0e54d217 | 506 | while (num_proto-- != 0) |
2c41f33c FW |
507 | __nf_ct_l4proto_unregister_one(l4proto[num_proto]); |
508 | mutex_unlock(&nf_ct_proto_mutex); | |
509 | ||
510 | synchronize_net(); | |
511 | /* Remove all contrack entries for this protocol */ | |
512 | nf_ct_iterate_destroy(kill_l4proto, l4proto); | |
0e54d217 DC |
513 | } |
514 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_unregister); | |
515 | ||
516 | void nf_ct_l4proto_pernet_unregister(struct net *net, | |
517 | struct nf_conntrack_l4proto *l4proto[], | |
518 | unsigned int num_proto) | |
519 | { | |
520 | while (num_proto-- != 0) | |
521 | nf_ct_l4proto_pernet_unregister_one(net, l4proto[num_proto]); | |
522 | } | |
c296bb4d | 523 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_unregister); |
ac5357eb | 524 | |
04d87001 | 525 | int nf_conntrack_proto_pernet_init(struct net *net) |
ac5357eb | 526 | { |
ac5357eb | 527 | int err; |
fa34fff5 G |
528 | struct nf_proto_net *pn = nf_ct_l4proto_net(net, |
529 | &nf_conntrack_l4proto_generic); | |
530 | ||
f1caad27 G |
531 | err = nf_conntrack_l4proto_generic.init_net(net, |
532 | nf_conntrack_l4proto_generic.l3proto); | |
15f585bd G |
533 | if (err < 0) |
534 | return err; | |
535 | err = nf_ct_l4proto_register_sysctl(net, | |
fa34fff5 | 536 | pn, |
15f585bd | 537 | &nf_conntrack_l4proto_generic); |
ac5357eb PM |
538 | if (err < 0) |
539 | return err; | |
540 | ||
fa34fff5 | 541 | pn->users++; |
ac5357eb PM |
542 | return 0; |
543 | } | |
544 | ||
04d87001 | 545 | void nf_conntrack_proto_pernet_fini(struct net *net) |
ac5357eb | 546 | { |
fa34fff5 G |
547 | struct nf_proto_net *pn = nf_ct_l4proto_net(net, |
548 | &nf_conntrack_l4proto_generic); | |
549 | ||
550 | pn->users--; | |
15f585bd | 551 | nf_ct_l4proto_unregister_sysctl(net, |
fa34fff5 | 552 | pn, |
15f585bd | 553 | &nf_conntrack_l4proto_generic); |
04d87001 G |
554 | } |
555 | ||
556 | int nf_conntrack_proto_init(void) | |
557 | { | |
558 | unsigned int i; | |
b7b5fda4 | 559 | for (i = 0; i < NFPROTO_NUMPROTO; i++) |
04d87001 G |
560 | rcu_assign_pointer(nf_ct_l3protos[i], |
561 | &nf_conntrack_l3proto_generic); | |
562 | return 0; | |
563 | } | |
564 | ||
565 | void nf_conntrack_proto_fini(void) | |
566 | { | |
567 | unsigned int i; | |
568 | /* free l3proto protocol tables */ | |
b7b5fda4 | 569 | for (i = 0; i < ARRAY_SIZE(nf_ct_protos); i++) |
04d87001 | 570 | kfree(nf_ct_protos[i]); |
ac5357eb | 571 | } |