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 | 29 | #include <net/netfilter/nf_conntrack_core.h> |
c4f3db15 | 30 | #include <net/netfilter/nf_log.h> |
8f03dea5 | 31 | |
b7b5fda4 FW |
32 | static struct nf_conntrack_l4proto __rcu **nf_ct_protos[NFPROTO_NUMPROTO] __read_mostly; |
33 | struct nf_conntrack_l3proto __rcu *nf_ct_l3protos[NFPROTO_NUMPROTO] __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 |
2c352f44 G |
40 | nf_ct_register_sysctl(struct net *net, |
41 | struct ctl_table_header **header, | |
42 | const char *path, | |
fa34fff5 | 43 | struct ctl_table *table) |
d62f9ed4 PM |
44 | { |
45 | if (*header == NULL) { | |
2c352f44 | 46 | *header = register_net_sysctl(net, path, table); |
d62f9ed4 PM |
47 | if (*header == NULL) |
48 | return -ENOMEM; | |
49 | } | |
2c352f44 | 50 | |
d62f9ed4 PM |
51 | return 0; |
52 | } | |
53 | ||
54 | static void | |
55 | nf_ct_unregister_sysctl(struct ctl_table_header **header, | |
2c352f44 | 56 | struct ctl_table **table, |
fa34fff5 | 57 | unsigned int users) |
d62f9ed4 | 58 | { |
fa34fff5 | 59 | if (users > 0) |
d62f9ed4 | 60 | return; |
b3fd3ffe | 61 | |
5dd3df10 | 62 | unregister_net_sysctl_table(*header); |
2c352f44 | 63 | kfree(*table); |
d62f9ed4 | 64 | *header = NULL; |
2c352f44 | 65 | *table = NULL; |
d62f9ed4 | 66 | } |
c4f3db15 FW |
67 | |
68 | __printf(5, 6) | |
69 | void nf_l4proto_log_invalid(const struct sk_buff *skb, | |
70 | struct net *net, | |
71 | u16 pf, u8 protonum, | |
72 | const char *fmt, ...) | |
73 | { | |
74 | struct va_format vaf; | |
75 | va_list args; | |
76 | ||
77 | if (net->ct.sysctl_log_invalid != protonum || | |
78 | net->ct.sysctl_log_invalid != IPPROTO_RAW) | |
79 | return; | |
80 | ||
81 | va_start(args, fmt); | |
82 | vaf.fmt = fmt; | |
83 | vaf.va = &args; | |
84 | ||
85 | nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL, | |
86 | "nf_ct_proto_%d: %pV ", protonum, &vaf); | |
87 | va_end(args); | |
88 | } | |
89 | EXPORT_SYMBOL_GPL(nf_l4proto_log_invalid); | |
3d0b527b FW |
90 | |
91 | __printf(3, 4) | |
92 | void nf_ct_l4proto_log_invalid(const struct sk_buff *skb, | |
93 | const struct nf_conn *ct, | |
94 | const char *fmt, ...) | |
95 | { | |
96 | struct va_format vaf; | |
97 | struct net *net; | |
98 | va_list args; | |
99 | ||
100 | net = nf_ct_net(ct); | |
101 | if (likely(net->ct.sysctl_log_invalid == 0)) | |
102 | return; | |
103 | ||
104 | va_start(args, fmt); | |
105 | vaf.fmt = fmt; | |
106 | vaf.va = &args; | |
107 | ||
108 | nf_l4proto_log_invalid(skb, net, nf_ct_l3num(ct), | |
109 | nf_ct_protonum(ct), "%pV", &vaf); | |
110 | va_end(args); | |
111 | } | |
112 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_log_invalid); | |
d62f9ed4 PM |
113 | #endif |
114 | ||
b3480fe0 | 115 | const struct nf_conntrack_l4proto * |
605dcad6 | 116 | __nf_ct_l4proto_find(u_int16_t l3proto, u_int8_t l4proto) |
8f03dea5 | 117 | { |
b7b5fda4 | 118 | if (unlikely(l3proto >= NFPROTO_NUMPROTO || nf_ct_protos[l3proto] == NULL)) |
605dcad6 | 119 | return &nf_conntrack_l4proto_generic; |
8f03dea5 | 120 | |
923f4902 | 121 | return rcu_dereference(nf_ct_protos[l3proto][l4proto]); |
8f03dea5 | 122 | } |
13b18339 | 123 | EXPORT_SYMBOL_GPL(__nf_ct_l4proto_find); |
8f03dea5 MJ |
124 | |
125 | /* this is guaranteed to always return a valid protocol helper, since | |
126 | * it falls back to generic_protocol */ | |
b3480fe0 | 127 | const struct nf_conntrack_l3proto * |
8f03dea5 MJ |
128 | nf_ct_l3proto_find_get(u_int16_t l3proto) |
129 | { | |
130 | struct nf_conntrack_l3proto *p; | |
131 | ||
923f4902 | 132 | rcu_read_lock(); |
8f03dea5 MJ |
133 | p = __nf_ct_l3proto_find(l3proto); |
134 | if (!try_module_get(p->me)) | |
605dcad6 | 135 | p = &nf_conntrack_l3proto_generic; |
923f4902 | 136 | rcu_read_unlock(); |
8f03dea5 MJ |
137 | |
138 | return p; | |
139 | } | |
13b18339 | 140 | EXPORT_SYMBOL_GPL(nf_ct_l3proto_find_get); |
8f03dea5 | 141 | |
8f03dea5 MJ |
142 | int |
143 | nf_ct_l3proto_try_module_get(unsigned short l3proto) | |
144 | { | |
b3480fe0 | 145 | const struct nf_conntrack_l3proto *p; |
8f03dea5 | 146 | int ret; |
8f03dea5 MJ |
147 | |
148 | retry: p = nf_ct_l3proto_find_get(l3proto); | |
605dcad6 | 149 | if (p == &nf_conntrack_l3proto_generic) { |
8f03dea5 MJ |
150 | ret = request_module("nf_conntrack-%d", l3proto); |
151 | if (!ret) | |
152 | goto retry; | |
153 | ||
154 | return -EPROTOTYPE; | |
155 | } | |
156 | ||
157 | return 0; | |
158 | } | |
13b18339 | 159 | EXPORT_SYMBOL_GPL(nf_ct_l3proto_try_module_get); |
8f03dea5 MJ |
160 | |
161 | void nf_ct_l3proto_module_put(unsigned short l3proto) | |
162 | { | |
163 | struct nf_conntrack_l3proto *p; | |
164 | ||
3b254c54 PM |
165 | /* rcu_read_lock not necessary since the caller holds a reference, but |
166 | * taken anyways to avoid lockdep warnings in __nf_ct_l3proto_find() | |
167 | */ | |
168 | rcu_read_lock(); | |
8f03dea5 | 169 | p = __nf_ct_l3proto_find(l3proto); |
8f03dea5 | 170 | module_put(p->me); |
3b254c54 | 171 | rcu_read_unlock(); |
8f03dea5 | 172 | } |
13b18339 | 173 | EXPORT_SYMBOL_GPL(nf_ct_l3proto_module_put); |
8f03dea5 | 174 | |
7e35ec0e | 175 | static int nf_ct_netns_do_get(struct net *net, u8 nfproto) |
ecb2421b | 176 | { |
0c66dc1e FW |
177 | const struct nf_conntrack_l3proto *l3proto; |
178 | int ret; | |
179 | ||
180 | might_sleep(); | |
181 | ||
182 | ret = nf_ct_l3proto_try_module_get(nfproto); | |
183 | if (ret < 0) | |
184 | return ret; | |
185 | ||
186 | /* we already have a reference, can't fail */ | |
187 | rcu_read_lock(); | |
188 | l3proto = __nf_ct_l3proto_find(nfproto); | |
189 | rcu_read_unlock(); | |
190 | ||
191 | if (!l3proto->net_ns_get) | |
192 | return 0; | |
193 | ||
194 | ret = l3proto->net_ns_get(net); | |
195 | if (ret < 0) | |
196 | nf_ct_l3proto_module_put(nfproto); | |
197 | ||
198 | return ret; | |
ecb2421b | 199 | } |
7e35ec0e PNA |
200 | |
201 | int nf_ct_netns_get(struct net *net, u8 nfproto) | |
202 | { | |
203 | int err; | |
204 | ||
205 | if (nfproto == NFPROTO_INET) { | |
206 | err = nf_ct_netns_do_get(net, NFPROTO_IPV4); | |
207 | if (err < 0) | |
208 | goto err1; | |
209 | err = nf_ct_netns_do_get(net, NFPROTO_IPV6); | |
210 | if (err < 0) | |
211 | goto err2; | |
212 | } else { | |
213 | err = nf_ct_netns_do_get(net, nfproto); | |
214 | if (err < 0) | |
215 | goto err1; | |
216 | } | |
217 | return 0; | |
218 | ||
219 | err2: | |
220 | nf_ct_netns_put(net, NFPROTO_IPV4); | |
221 | err1: | |
222 | return err; | |
223 | } | |
ecb2421b FW |
224 | EXPORT_SYMBOL_GPL(nf_ct_netns_get); |
225 | ||
7e35ec0e | 226 | static void nf_ct_netns_do_put(struct net *net, u8 nfproto) |
ecb2421b | 227 | { |
0c66dc1e FW |
228 | const struct nf_conntrack_l3proto *l3proto; |
229 | ||
230 | might_sleep(); | |
231 | ||
232 | /* same as nf_conntrack_netns_get(), reference assumed */ | |
233 | rcu_read_lock(); | |
234 | l3proto = __nf_ct_l3proto_find(nfproto); | |
235 | rcu_read_unlock(); | |
236 | ||
237 | if (WARN_ON(!l3proto)) | |
238 | return; | |
239 | ||
240 | if (l3proto->net_ns_put) | |
241 | l3proto->net_ns_put(net); | |
242 | ||
ecb2421b FW |
243 | nf_ct_l3proto_module_put(nfproto); |
244 | } | |
7e35ec0e PNA |
245 | |
246 | void nf_ct_netns_put(struct net *net, uint8_t nfproto) | |
247 | { | |
248 | if (nfproto == NFPROTO_INET) { | |
249 | nf_ct_netns_do_put(net, NFPROTO_IPV4); | |
250 | nf_ct_netns_do_put(net, NFPROTO_IPV6); | |
251 | } else | |
252 | nf_ct_netns_do_put(net, nfproto); | |
253 | } | |
ecb2421b FW |
254 | EXPORT_SYMBOL_GPL(nf_ct_netns_put); |
255 | ||
b3480fe0 | 256 | const struct nf_conntrack_l4proto * |
c1ebd7df PNA |
257 | nf_ct_l4proto_find_get(u_int16_t l3num, u_int8_t l4num) |
258 | { | |
b3480fe0 | 259 | const struct nf_conntrack_l4proto *p; |
c1ebd7df PNA |
260 | |
261 | rcu_read_lock(); | |
262 | p = __nf_ct_l4proto_find(l3num, l4num); | |
263 | if (!try_module_get(p->me)) | |
264 | p = &nf_conntrack_l4proto_generic; | |
265 | rcu_read_unlock(); | |
266 | ||
267 | return p; | |
268 | } | |
269 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_find_get); | |
270 | ||
2a04aabf | 271 | void nf_ct_l4proto_put(const struct nf_conntrack_l4proto *p) |
c1ebd7df PNA |
272 | { |
273 | module_put(p->me); | |
274 | } | |
275 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_put); | |
276 | ||
8f03dea5 MJ |
277 | static int kill_l3proto(struct nf_conn *i, void *data) |
278 | { | |
b3480fe0 | 279 | return nf_ct_l3num(i) == ((const struct nf_conntrack_l3proto *)data)->l3proto; |
8f03dea5 MJ |
280 | } |
281 | ||
605dcad6 | 282 | static int kill_l4proto(struct nf_conn *i, void *data) |
8f03dea5 | 283 | { |
b3480fe0 | 284 | const struct nf_conntrack_l4proto *l4proto; |
68ad546a | 285 | l4proto = data; |
5e8fbe2a PM |
286 | return nf_ct_protonum(i) == l4proto->l4proto && |
287 | nf_ct_l3num(i) == l4proto->l3proto; | |
8f03dea5 MJ |
288 | } |
289 | ||
b3480fe0 | 290 | int nf_ct_l3proto_register(const struct nf_conntrack_l3proto *proto) |
8f03dea5 MJ |
291 | { |
292 | int ret = 0; | |
0e60ebe0 | 293 | struct nf_conntrack_l3proto *old; |
8f03dea5 | 294 | |
b7b5fda4 | 295 | if (proto->l3proto >= NFPROTO_NUMPROTO) |
0661cca9 | 296 | return -EBUSY; |
0d035100 FW |
297 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK) |
298 | if (proto->tuple_to_nlattr && proto->nla_size == 0) | |
d0dba725 | 299 | return -EINVAL; |
0d035100 | 300 | #endif |
b19caa0c | 301 | mutex_lock(&nf_ct_proto_mutex); |
0e60ebe0 ED |
302 | old = rcu_dereference_protected(nf_ct_l3protos[proto->l3proto], |
303 | lockdep_is_held(&nf_ct_proto_mutex)); | |
304 | if (old != &nf_conntrack_l3proto_generic) { | |
8f03dea5 | 305 | ret = -EBUSY; |
ae5718fb | 306 | goto out_unlock; |
8f03dea5 | 307 | } |
d62f9ed4 | 308 | |
0661cca9 | 309 | rcu_assign_pointer(nf_ct_l3protos[proto->l3proto], proto); |
8f03dea5 | 310 | |
ae5718fb | 311 | out_unlock: |
b19caa0c | 312 | mutex_unlock(&nf_ct_proto_mutex); |
8f03dea5 | 313 | return ret; |
524a53e5 | 314 | |
8f03dea5 | 315 | } |
6330750d | 316 | EXPORT_SYMBOL_GPL(nf_ct_l3proto_register); |
8f03dea5 | 317 | |
b3480fe0 | 318 | void nf_ct_l3proto_unregister(const struct nf_conntrack_l3proto *proto) |
524a53e5 | 319 | { |
b7b5fda4 | 320 | BUG_ON(proto->l3proto >= NFPROTO_NUMPROTO); |
ae5718fb | 321 | |
b19caa0c | 322 | mutex_lock(&nf_ct_proto_mutex); |
0e60ebe0 ED |
323 | BUG_ON(rcu_dereference_protected(nf_ct_l3protos[proto->l3proto], |
324 | lockdep_is_held(&nf_ct_proto_mutex) | |
325 | ) != proto); | |
923f4902 PM |
326 | rcu_assign_pointer(nf_ct_l3protos[proto->l3proto], |
327 | &nf_conntrack_l3proto_generic); | |
b19caa0c | 328 | mutex_unlock(&nf_ct_proto_mutex); |
8f03dea5 | 329 | |
0661cca9 | 330 | synchronize_rcu(); |
2c41f33c | 331 | /* Remove all contrack entries for this protocol */ |
b3480fe0 | 332 | nf_ct_iterate_destroy(kill_l3proto, (void*)proto); |
524a53e5 | 333 | } |
6330750d | 334 | EXPORT_SYMBOL_GPL(nf_ct_l3proto_unregister); |
524a53e5 | 335 | |
2c352f44 | 336 | static struct nf_proto_net *nf_ct_l4proto_net(struct net *net, |
2a04aabf | 337 | const struct nf_conntrack_l4proto *l4proto) |
2c352f44 | 338 | { |
08911475 PNA |
339 | if (l4proto->get_net_proto) { |
340 | /* statically built-in protocols use static per-net */ | |
341 | return l4proto->get_net_proto(net); | |
342 | } else if (l4proto->net_id) { | |
343 | /* ... and loadable protocols use dynamic per-net */ | |
344 | return net_generic(net, *l4proto->net_id); | |
15f585bd G |
345 | } |
346 | return NULL; | |
2c352f44 G |
347 | } |
348 | ||
349 | static | |
350 | int nf_ct_l4proto_register_sysctl(struct net *net, | |
fa34fff5 | 351 | struct nf_proto_net *pn, |
2a04aabf | 352 | const struct nf_conntrack_l4proto *l4proto) |
d62f9ed4 PM |
353 | { |
354 | int err = 0; | |
355 | ||
356 | #ifdef CONFIG_SYSCTL | |
2c352f44 G |
357 | if (pn->ctl_table != NULL) { |
358 | err = nf_ct_register_sysctl(net, | |
359 | &pn->ctl_table_header, | |
f99e8f71 | 360 | "net/netfilter", |
fa34fff5 | 361 | pn->ctl_table); |
2c352f44 G |
362 | if (err < 0) { |
363 | if (!pn->users) { | |
364 | kfree(pn->ctl_table); | |
365 | pn->ctl_table = NULL; | |
366 | } | |
2c352f44 | 367 | } |
d62f9ed4 | 368 | } |
933a41e7 | 369 | #endif /* CONFIG_SYSCTL */ |
d62f9ed4 PM |
370 | return err; |
371 | } | |
372 | ||
2c352f44 G |
373 | static |
374 | void nf_ct_l4proto_unregister_sysctl(struct net *net, | |
2a04aabf JL |
375 | struct nf_proto_net *pn, |
376 | const struct nf_conntrack_l4proto *l4proto) | |
d62f9ed4 PM |
377 | { |
378 | #ifdef CONFIG_SYSCTL | |
2c352f44 G |
379 | if (pn->ctl_table_header != NULL) |
380 | nf_ct_unregister_sysctl(&pn->ctl_table_header, | |
381 | &pn->ctl_table, | |
fa34fff5 | 382 | pn->users); |
933a41e7 | 383 | #endif /* CONFIG_SYSCTL */ |
d62f9ed4 PM |
384 | } |
385 | ||
8f03dea5 MJ |
386 | /* FIXME: Allow NULL functions and sub in pointers to generic for |
387 | them. --RR */ | |
cd9ceafc | 388 | int nf_ct_l4proto_register_one(const struct nf_conntrack_l4proto *l4proto) |
8f03dea5 MJ |
389 | { |
390 | int ret = 0; | |
391 | ||
b7b5fda4 | 392 | if (l4proto->l3proto >= ARRAY_SIZE(nf_ct_protos)) |
0661cca9 | 393 | return -EBUSY; |
ae5718fb | 394 | |
39215846 | 395 | if ((l4proto->to_nlattr && l4proto->nlattr_size == 0) || |
0e54d217 | 396 | (l4proto->tuple_to_nlattr && !l4proto->nlattr_tuple_size)) |
d0dba725 HE |
397 | return -EINVAL; |
398 | ||
b19caa0c | 399 | mutex_lock(&nf_ct_proto_mutex); |
c6a1e615 | 400 | if (!nf_ct_protos[l4proto->l3proto]) { |
8f03dea5 | 401 | /* l3proto may be loaded latter. */ |
c5d277d2 | 402 | struct nf_conntrack_l4proto __rcu **proto_array; |
8f03dea5 MJ |
403 | int i; |
404 | ||
c6a1e615 PM |
405 | proto_array = kmalloc(MAX_NF_CT_PROTO * |
406 | sizeof(struct nf_conntrack_l4proto *), | |
407 | GFP_KERNEL); | |
8f03dea5 MJ |
408 | if (proto_array == NULL) { |
409 | ret = -ENOMEM; | |
b19caa0c | 410 | goto out_unlock; |
8f03dea5 | 411 | } |
c6a1e615 | 412 | |
8f03dea5 | 413 | for (i = 0; i < MAX_NF_CT_PROTO; i++) |
0e54d217 DC |
414 | RCU_INIT_POINTER(proto_array[i], |
415 | &nf_conntrack_l4proto_generic); | |
d817d29d ED |
416 | |
417 | /* Before making proto_array visible to lockless readers, | |
418 | * we must make sure its content is committed to memory. | |
419 | */ | |
420 | smp_wmb(); | |
421 | ||
c6a1e615 | 422 | nf_ct_protos[l4proto->l3proto] = proto_array; |
0e60ebe0 ED |
423 | } else if (rcu_dereference_protected( |
424 | nf_ct_protos[l4proto->l3proto][l4proto->l4proto], | |
425 | lockdep_is_held(&nf_ct_proto_mutex) | |
426 | ) != &nf_conntrack_l4proto_generic) { | |
c6a1e615 PM |
427 | ret = -EBUSY; |
428 | goto out_unlock; | |
8f03dea5 MJ |
429 | } |
430 | ||
c6a1e615 PM |
431 | rcu_assign_pointer(nf_ct_protos[l4proto->l3proto][l4proto->l4proto], |
432 | l4proto); | |
8f03dea5 | 433 | out_unlock: |
b19caa0c | 434 | mutex_unlock(&nf_ct_proto_mutex); |
8f03dea5 MJ |
435 | return ret; |
436 | } | |
0e54d217 | 437 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_register_one); |
8f03dea5 | 438 | |
0e54d217 | 439 | int nf_ct_l4proto_pernet_register_one(struct net *net, |
2a04aabf | 440 | const struct nf_conntrack_l4proto *l4proto) |
8f03dea5 | 441 | { |
2c352f44 | 442 | int ret = 0; |
fa34fff5 | 443 | struct nf_proto_net *pn = NULL; |
2c352f44 | 444 | |
fa0f61f0 | 445 | if (l4proto->init_net) { |
f1caad27 | 446 | ret = l4proto->init_net(net, l4proto->l3proto); |
fa0f61f0 | 447 | if (ret < 0) |
fa34fff5 | 448 | goto out; |
fa0f61f0 | 449 | } |
678d6675 | 450 | |
fa34fff5 G |
451 | pn = nf_ct_l4proto_net(net, l4proto); |
452 | if (pn == NULL) | |
453 | goto out; | |
454 | ||
455 | ret = nf_ct_l4proto_register_sysctl(net, pn, l4proto); | |
2c352f44 | 456 | if (ret < 0) |
fa34fff5 | 457 | goto out; |
2c352f44 | 458 | |
fa34fff5 G |
459 | pn->users++; |
460 | out: | |
fa0f61f0 | 461 | return ret; |
2c352f44 | 462 | } |
0e54d217 | 463 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_register_one); |
2c352f44 | 464 | |
2a04aabf | 465 | static void __nf_ct_l4proto_unregister_one(const struct nf_conntrack_l4proto *l4proto) |
2c41f33c | 466 | |
2c352f44 | 467 | { |
b7b5fda4 | 468 | BUG_ON(l4proto->l3proto >= ARRAY_SIZE(nf_ct_protos)); |
ae5718fb | 469 | |
0e60ebe0 ED |
470 | BUG_ON(rcu_dereference_protected( |
471 | nf_ct_protos[l4proto->l3proto][l4proto->l4proto], | |
472 | lockdep_is_held(&nf_ct_proto_mutex) | |
473 | ) != l4proto); | |
923f4902 PM |
474 | rcu_assign_pointer(nf_ct_protos[l4proto->l3proto][l4proto->l4proto], |
475 | &nf_conntrack_l4proto_generic); | |
2c41f33c FW |
476 | } |
477 | ||
2a04aabf | 478 | void nf_ct_l4proto_unregister_one(const struct nf_conntrack_l4proto *l4proto) |
2c41f33c FW |
479 | { |
480 | mutex_lock(&nf_ct_proto_mutex); | |
481 | __nf_ct_l4proto_unregister_one(l4proto); | |
b19caa0c | 482 | mutex_unlock(&nf_ct_proto_mutex); |
8f03dea5 | 483 | |
0661cca9 | 484 | synchronize_rcu(); |
2c352f44 | 485 | } |
0e54d217 | 486 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_unregister_one); |
d62f9ed4 | 487 | |
0e54d217 | 488 | void nf_ct_l4proto_pernet_unregister_one(struct net *net, |
2a04aabf | 489 | const struct nf_conntrack_l4proto *l4proto) |
2c352f44 | 490 | { |
809c2d9a | 491 | struct nf_proto_net *pn = nf_ct_l4proto_net(net, l4proto); |
fa34fff5 | 492 | |
fa34fff5 G |
493 | if (pn == NULL) |
494 | return; | |
495 | ||
496 | pn->users--; | |
497 | nf_ct_l4proto_unregister_sysctl(net, pn, l4proto); | |
8f03dea5 | 498 | } |
0e54d217 DC |
499 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_unregister_one); |
500 | ||
cd9ceafc | 501 | int nf_ct_l4proto_register(const struct nf_conntrack_l4proto * const l4proto[], |
0e54d217 DC |
502 | unsigned int num_proto) |
503 | { | |
504 | int ret = -EINVAL, ver; | |
505 | unsigned int i; | |
506 | ||
507 | for (i = 0; i < num_proto; i++) { | |
508 | ret = nf_ct_l4proto_register_one(l4proto[i]); | |
509 | if (ret < 0) | |
510 | break; | |
511 | } | |
512 | if (i != num_proto) { | |
513 | ver = l4proto[i]->l3proto == PF_INET6 ? 6 : 4; | |
09ec82f5 FW |
514 | pr_err("nf_conntrack_ipv%d: can't register l4 %d proto.\n", |
515 | ver, l4proto[i]->l4proto); | |
0e54d217 DC |
516 | nf_ct_l4proto_unregister(l4proto, i); |
517 | } | |
518 | return ret; | |
519 | } | |
520 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_register); | |
521 | ||
522 | int nf_ct_l4proto_pernet_register(struct net *net, | |
cd9ceafc | 523 | const struct nf_conntrack_l4proto *const l4proto[], |
0e54d217 DC |
524 | unsigned int num_proto) |
525 | { | |
526 | int ret = -EINVAL; | |
527 | unsigned int i; | |
528 | ||
529 | for (i = 0; i < num_proto; i++) { | |
530 | ret = nf_ct_l4proto_pernet_register_one(net, l4proto[i]); | |
531 | if (ret < 0) | |
532 | break; | |
533 | } | |
534 | if (i != num_proto) { | |
09ec82f5 FW |
535 | pr_err("nf_conntrack_proto_%d %d: pernet registration failed\n", |
536 | l4proto[i]->l4proto, | |
0e54d217 DC |
537 | l4proto[i]->l3proto == PF_INET6 ? 6 : 4); |
538 | nf_ct_l4proto_pernet_unregister(net, l4proto, i); | |
539 | } | |
540 | return ret; | |
541 | } | |
542 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_register); | |
543 | ||
cd9ceafc | 544 | void nf_ct_l4proto_unregister(const struct nf_conntrack_l4proto * const l4proto[], |
0e54d217 DC |
545 | unsigned int num_proto) |
546 | { | |
2c41f33c | 547 | mutex_lock(&nf_ct_proto_mutex); |
0e54d217 | 548 | while (num_proto-- != 0) |
2c41f33c FW |
549 | __nf_ct_l4proto_unregister_one(l4proto[num_proto]); |
550 | mutex_unlock(&nf_ct_proto_mutex); | |
551 | ||
552 | synchronize_net(); | |
553 | /* Remove all contrack entries for this protocol */ | |
cd9ceafc | 554 | nf_ct_iterate_destroy(kill_l4proto, (void *)l4proto); |
0e54d217 DC |
555 | } |
556 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_unregister); | |
557 | ||
558 | void nf_ct_l4proto_pernet_unregister(struct net *net, | |
cd9ceafc | 559 | const struct nf_conntrack_l4proto *const l4proto[], |
2a04aabf | 560 | unsigned int num_proto) |
0e54d217 DC |
561 | { |
562 | while (num_proto-- != 0) | |
563 | nf_ct_l4proto_pernet_unregister_one(net, l4proto[num_proto]); | |
564 | } | |
c296bb4d | 565 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_unregister); |
ac5357eb | 566 | |
04d87001 | 567 | int nf_conntrack_proto_pernet_init(struct net *net) |
ac5357eb | 568 | { |
ac5357eb | 569 | int err; |
fa34fff5 G |
570 | struct nf_proto_net *pn = nf_ct_l4proto_net(net, |
571 | &nf_conntrack_l4proto_generic); | |
572 | ||
f1caad27 G |
573 | err = nf_conntrack_l4proto_generic.init_net(net, |
574 | nf_conntrack_l4proto_generic.l3proto); | |
15f585bd G |
575 | if (err < 0) |
576 | return err; | |
577 | err = nf_ct_l4proto_register_sysctl(net, | |
fa34fff5 | 578 | pn, |
15f585bd | 579 | &nf_conntrack_l4proto_generic); |
ac5357eb PM |
580 | if (err < 0) |
581 | return err; | |
582 | ||
fa34fff5 | 583 | pn->users++; |
ac5357eb PM |
584 | return 0; |
585 | } | |
586 | ||
04d87001 | 587 | void nf_conntrack_proto_pernet_fini(struct net *net) |
ac5357eb | 588 | { |
fa34fff5 G |
589 | struct nf_proto_net *pn = nf_ct_l4proto_net(net, |
590 | &nf_conntrack_l4proto_generic); | |
591 | ||
592 | pn->users--; | |
15f585bd | 593 | nf_ct_l4proto_unregister_sysctl(net, |
fa34fff5 | 594 | pn, |
15f585bd | 595 | &nf_conntrack_l4proto_generic); |
04d87001 G |
596 | } |
597 | ||
598 | int nf_conntrack_proto_init(void) | |
599 | { | |
600 | unsigned int i; | |
b7b5fda4 | 601 | for (i = 0; i < NFPROTO_NUMPROTO; i++) |
04d87001 G |
602 | rcu_assign_pointer(nf_ct_l3protos[i], |
603 | &nf_conntrack_l3proto_generic); | |
604 | return 0; | |
605 | } | |
606 | ||
607 | void nf_conntrack_proto_fini(void) | |
608 | { | |
609 | unsigned int i; | |
610 | /* free l3proto protocol tables */ | |
b7b5fda4 | 611 | for (i = 0; i < ARRAY_SIZE(nf_ct_protos); i++) |
04d87001 | 612 | kfree(nf_ct_protos[i]); |
ac5357eb | 613 | } |