]>
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 */ | |
0e54d217 | 388 | int nf_ct_l4proto_register_one(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 | |
0e54d217 DC |
395 | if ((l4proto->to_nlattr && !l4proto->nlattr_size) || |
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 | ||
d0dba725 HE |
431 | l4proto->nla_size = 0; |
432 | if (l4proto->nlattr_size) | |
433 | l4proto->nla_size += l4proto->nlattr_size(); | |
d0dba725 | 434 | |
c6a1e615 PM |
435 | rcu_assign_pointer(nf_ct_protos[l4proto->l3proto][l4proto->l4proto], |
436 | l4proto); | |
8f03dea5 | 437 | out_unlock: |
b19caa0c | 438 | mutex_unlock(&nf_ct_proto_mutex); |
8f03dea5 MJ |
439 | return ret; |
440 | } | |
0e54d217 | 441 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_register_one); |
8f03dea5 | 442 | |
0e54d217 | 443 | int nf_ct_l4proto_pernet_register_one(struct net *net, |
2a04aabf | 444 | const struct nf_conntrack_l4proto *l4proto) |
8f03dea5 | 445 | { |
2c352f44 | 446 | int ret = 0; |
fa34fff5 | 447 | struct nf_proto_net *pn = NULL; |
2c352f44 | 448 | |
fa0f61f0 | 449 | if (l4proto->init_net) { |
f1caad27 | 450 | ret = l4proto->init_net(net, l4proto->l3proto); |
fa0f61f0 | 451 | if (ret < 0) |
fa34fff5 | 452 | goto out; |
fa0f61f0 | 453 | } |
678d6675 | 454 | |
fa34fff5 G |
455 | pn = nf_ct_l4proto_net(net, l4proto); |
456 | if (pn == NULL) | |
457 | goto out; | |
458 | ||
459 | ret = nf_ct_l4proto_register_sysctl(net, pn, l4proto); | |
2c352f44 | 460 | if (ret < 0) |
fa34fff5 | 461 | goto out; |
2c352f44 | 462 | |
fa34fff5 G |
463 | pn->users++; |
464 | out: | |
fa0f61f0 | 465 | return ret; |
2c352f44 | 466 | } |
0e54d217 | 467 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_register_one); |
2c352f44 | 468 | |
2a04aabf | 469 | static void __nf_ct_l4proto_unregister_one(const struct nf_conntrack_l4proto *l4proto) |
2c41f33c | 470 | |
2c352f44 | 471 | { |
b7b5fda4 | 472 | BUG_ON(l4proto->l3proto >= ARRAY_SIZE(nf_ct_protos)); |
ae5718fb | 473 | |
0e60ebe0 ED |
474 | BUG_ON(rcu_dereference_protected( |
475 | nf_ct_protos[l4proto->l3proto][l4proto->l4proto], | |
476 | lockdep_is_held(&nf_ct_proto_mutex) | |
477 | ) != l4proto); | |
923f4902 PM |
478 | rcu_assign_pointer(nf_ct_protos[l4proto->l3proto][l4proto->l4proto], |
479 | &nf_conntrack_l4proto_generic); | |
2c41f33c FW |
480 | } |
481 | ||
2a04aabf | 482 | void nf_ct_l4proto_unregister_one(const struct nf_conntrack_l4proto *l4proto) |
2c41f33c FW |
483 | { |
484 | mutex_lock(&nf_ct_proto_mutex); | |
485 | __nf_ct_l4proto_unregister_one(l4proto); | |
b19caa0c | 486 | mutex_unlock(&nf_ct_proto_mutex); |
8f03dea5 | 487 | |
0661cca9 | 488 | synchronize_rcu(); |
2c352f44 | 489 | } |
0e54d217 | 490 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_unregister_one); |
d62f9ed4 | 491 | |
0e54d217 | 492 | void nf_ct_l4proto_pernet_unregister_one(struct net *net, |
2a04aabf | 493 | const struct nf_conntrack_l4proto *l4proto) |
2c352f44 | 494 | { |
809c2d9a | 495 | struct nf_proto_net *pn = nf_ct_l4proto_net(net, l4proto); |
fa34fff5 | 496 | |
fa34fff5 G |
497 | if (pn == NULL) |
498 | return; | |
499 | ||
500 | pn->users--; | |
501 | nf_ct_l4proto_unregister_sysctl(net, pn, l4proto); | |
8f03dea5 | 502 | } |
0e54d217 DC |
503 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_unregister_one); |
504 | ||
505 | int nf_ct_l4proto_register(struct nf_conntrack_l4proto *l4proto[], | |
506 | unsigned int num_proto) | |
507 | { | |
508 | int ret = -EINVAL, ver; | |
509 | unsigned int i; | |
510 | ||
511 | for (i = 0; i < num_proto; i++) { | |
512 | ret = nf_ct_l4proto_register_one(l4proto[i]); | |
513 | if (ret < 0) | |
514 | break; | |
515 | } | |
516 | if (i != num_proto) { | |
517 | ver = l4proto[i]->l3proto == PF_INET6 ? 6 : 4; | |
09ec82f5 FW |
518 | pr_err("nf_conntrack_ipv%d: can't register l4 %d proto.\n", |
519 | ver, l4proto[i]->l4proto); | |
0e54d217 DC |
520 | nf_ct_l4proto_unregister(l4proto, i); |
521 | } | |
522 | return ret; | |
523 | } | |
524 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_register); | |
525 | ||
526 | int nf_ct_l4proto_pernet_register(struct net *net, | |
2a04aabf | 527 | struct nf_conntrack_l4proto *const l4proto[], |
0e54d217 DC |
528 | unsigned int num_proto) |
529 | { | |
530 | int ret = -EINVAL; | |
531 | unsigned int i; | |
532 | ||
533 | for (i = 0; i < num_proto; i++) { | |
534 | ret = nf_ct_l4proto_pernet_register_one(net, l4proto[i]); | |
535 | if (ret < 0) | |
536 | break; | |
537 | } | |
538 | if (i != num_proto) { | |
09ec82f5 FW |
539 | pr_err("nf_conntrack_proto_%d %d: pernet registration failed\n", |
540 | l4proto[i]->l4proto, | |
0e54d217 DC |
541 | l4proto[i]->l3proto == PF_INET6 ? 6 : 4); |
542 | nf_ct_l4proto_pernet_unregister(net, l4proto, i); | |
543 | } | |
544 | return ret; | |
545 | } | |
546 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_register); | |
547 | ||
548 | void nf_ct_l4proto_unregister(struct nf_conntrack_l4proto *l4proto[], | |
549 | unsigned int num_proto) | |
550 | { | |
2c41f33c | 551 | mutex_lock(&nf_ct_proto_mutex); |
0e54d217 | 552 | while (num_proto-- != 0) |
2c41f33c FW |
553 | __nf_ct_l4proto_unregister_one(l4proto[num_proto]); |
554 | mutex_unlock(&nf_ct_proto_mutex); | |
555 | ||
556 | synchronize_net(); | |
557 | /* Remove all contrack entries for this protocol */ | |
558 | nf_ct_iterate_destroy(kill_l4proto, l4proto); | |
0e54d217 DC |
559 | } |
560 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_unregister); | |
561 | ||
562 | void nf_ct_l4proto_pernet_unregister(struct net *net, | |
2a04aabf JL |
563 | struct nf_conntrack_l4proto *const l4proto[], |
564 | unsigned int num_proto) | |
0e54d217 DC |
565 | { |
566 | while (num_proto-- != 0) | |
567 | nf_ct_l4proto_pernet_unregister_one(net, l4proto[num_proto]); | |
568 | } | |
c296bb4d | 569 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_unregister); |
ac5357eb | 570 | |
04d87001 | 571 | int nf_conntrack_proto_pernet_init(struct net *net) |
ac5357eb | 572 | { |
ac5357eb | 573 | int err; |
fa34fff5 G |
574 | struct nf_proto_net *pn = nf_ct_l4proto_net(net, |
575 | &nf_conntrack_l4proto_generic); | |
576 | ||
f1caad27 G |
577 | err = nf_conntrack_l4proto_generic.init_net(net, |
578 | nf_conntrack_l4proto_generic.l3proto); | |
15f585bd G |
579 | if (err < 0) |
580 | return err; | |
581 | err = nf_ct_l4proto_register_sysctl(net, | |
fa34fff5 | 582 | pn, |
15f585bd | 583 | &nf_conntrack_l4proto_generic); |
ac5357eb PM |
584 | if (err < 0) |
585 | return err; | |
586 | ||
fa34fff5 | 587 | pn->users++; |
ac5357eb PM |
588 | return 0; |
589 | } | |
590 | ||
04d87001 | 591 | void nf_conntrack_proto_pernet_fini(struct net *net) |
ac5357eb | 592 | { |
fa34fff5 G |
593 | struct nf_proto_net *pn = nf_ct_l4proto_net(net, |
594 | &nf_conntrack_l4proto_generic); | |
595 | ||
596 | pn->users--; | |
15f585bd | 597 | nf_ct_l4proto_unregister_sysctl(net, |
fa34fff5 | 598 | pn, |
15f585bd | 599 | &nf_conntrack_l4proto_generic); |
04d87001 G |
600 | } |
601 | ||
602 | int nf_conntrack_proto_init(void) | |
603 | { | |
604 | unsigned int i; | |
b7b5fda4 | 605 | for (i = 0; i < NFPROTO_NUMPROTO; i++) |
04d87001 G |
606 | rcu_assign_pointer(nf_ct_l3protos[i], |
607 | &nf_conntrack_l3proto_generic); | |
608 | return 0; | |
609 | } | |
610 | ||
611 | void nf_conntrack_proto_fini(void) | |
612 | { | |
613 | unsigned int i; | |
614 | /* free l3proto protocol tables */ | |
b7b5fda4 | 615 | for (i = 0; i < ARRAY_SIZE(nf_ct_protos); i++) |
04d87001 | 616 | kfree(nf_ct_protos[i]); |
ac5357eb | 617 | } |