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> | |
5a0e3ad6 | 15 | #include <linux/slab.h> |
d62f9ed4 | 16 | #include <linux/mutex.h> |
8f03dea5 MJ |
17 | #include <linux/vmalloc.h> |
18 | #include <linux/stddef.h> | |
19 | #include <linux/err.h> | |
20 | #include <linux/percpu.h> | |
8f03dea5 MJ |
21 | #include <linux/notifier.h> |
22 | #include <linux/kernel.h> | |
23 | #include <linux/netdevice.h> | |
efb9a8c2 | 24 | #include <linux/rtnetlink.h> |
8f03dea5 MJ |
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 MJ |
94 | |
95 | void nf_ct_l3proto_put(struct nf_conntrack_l3proto *p) | |
96 | { | |
97 | module_put(p->me); | |
98 | } | |
13b18339 | 99 | EXPORT_SYMBOL_GPL(nf_ct_l3proto_put); |
8f03dea5 MJ |
100 | |
101 | int | |
102 | nf_ct_l3proto_try_module_get(unsigned short l3proto) | |
103 | { | |
104 | int ret; | |
105 | struct nf_conntrack_l3proto *p; | |
106 | ||
107 | retry: p = nf_ct_l3proto_find_get(l3proto); | |
605dcad6 | 108 | if (p == &nf_conntrack_l3proto_generic) { |
8f03dea5 MJ |
109 | ret = request_module("nf_conntrack-%d", l3proto); |
110 | if (!ret) | |
111 | goto retry; | |
112 | ||
113 | return -EPROTOTYPE; | |
114 | } | |
115 | ||
116 | return 0; | |
117 | } | |
13b18339 | 118 | EXPORT_SYMBOL_GPL(nf_ct_l3proto_try_module_get); |
8f03dea5 MJ |
119 | |
120 | void nf_ct_l3proto_module_put(unsigned short l3proto) | |
121 | { | |
122 | struct nf_conntrack_l3proto *p; | |
123 | ||
3b254c54 PM |
124 | /* rcu_read_lock not necessary since the caller holds a reference, but |
125 | * taken anyways to avoid lockdep warnings in __nf_ct_l3proto_find() | |
126 | */ | |
127 | rcu_read_lock(); | |
8f03dea5 | 128 | p = __nf_ct_l3proto_find(l3proto); |
8f03dea5 | 129 | module_put(p->me); |
3b254c54 | 130 | rcu_read_unlock(); |
8f03dea5 | 131 | } |
13b18339 | 132 | EXPORT_SYMBOL_GPL(nf_ct_l3proto_module_put); |
8f03dea5 | 133 | |
c1ebd7df PNA |
134 | struct nf_conntrack_l4proto * |
135 | nf_ct_l4proto_find_get(u_int16_t l3num, u_int8_t l4num) | |
136 | { | |
137 | struct nf_conntrack_l4proto *p; | |
138 | ||
139 | rcu_read_lock(); | |
140 | p = __nf_ct_l4proto_find(l3num, l4num); | |
141 | if (!try_module_get(p->me)) | |
142 | p = &nf_conntrack_l4proto_generic; | |
143 | rcu_read_unlock(); | |
144 | ||
145 | return p; | |
146 | } | |
147 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_find_get); | |
148 | ||
149 | void nf_ct_l4proto_put(struct nf_conntrack_l4proto *p) | |
150 | { | |
151 | module_put(p->me); | |
152 | } | |
153 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_put); | |
154 | ||
8f03dea5 MJ |
155 | static int kill_l3proto(struct nf_conn *i, void *data) |
156 | { | |
5e8fbe2a | 157 | return nf_ct_l3num(i) == ((struct nf_conntrack_l3proto *)data)->l3proto; |
8f03dea5 MJ |
158 | } |
159 | ||
605dcad6 | 160 | static int kill_l4proto(struct nf_conn *i, void *data) |
8f03dea5 | 161 | { |
605dcad6 MJ |
162 | struct nf_conntrack_l4proto *l4proto; |
163 | l4proto = (struct nf_conntrack_l4proto *)data; | |
5e8fbe2a PM |
164 | return nf_ct_protonum(i) == l4proto->l4proto && |
165 | nf_ct_l3num(i) == l4proto->l3proto; | |
8f03dea5 MJ |
166 | } |
167 | ||
524a53e5 G |
168 | static struct nf_ip_net *nf_ct_l3proto_net(struct net *net, |
169 | struct nf_conntrack_l3proto *l3proto) | |
d62f9ed4 | 170 | { |
524a53e5 G |
171 | if (l3proto->l3proto == PF_INET) |
172 | return &net->ct.nf_ct_proto; | |
173 | else | |
174 | return NULL; | |
175 | } | |
d62f9ed4 | 176 | |
524a53e5 G |
177 | static int nf_ct_l3proto_register_sysctl(struct net *net, |
178 | struct nf_conntrack_l3proto *l3proto) | |
179 | { | |
180 | int err = 0; | |
181 | struct nf_ip_net *in = nf_ct_l3proto_net(net, l3proto); | |
182 | /* nf_conntrack_l3proto_ipv6 doesn't support sysctl */ | |
183 | if (in == NULL) | |
184 | return 0; | |
185 | ||
186 | #if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT) | |
187 | if (in->ctl_table != NULL) { | |
188 | err = nf_ct_register_sysctl(net, | |
189 | &in->ctl_table_header, | |
d62f9ed4 | 190 | l3proto->ctl_table_path, |
fa34fff5 | 191 | in->ctl_table); |
524a53e5 G |
192 | if (err < 0) { |
193 | kfree(in->ctl_table); | |
194 | in->ctl_table = NULL; | |
195 | } | |
d62f9ed4 | 196 | } |
d62f9ed4 PM |
197 | #endif |
198 | return err; | |
199 | } | |
200 | ||
524a53e5 G |
201 | static void nf_ct_l3proto_unregister_sysctl(struct net *net, |
202 | struct nf_conntrack_l3proto *l3proto) | |
d62f9ed4 | 203 | { |
524a53e5 G |
204 | struct nf_ip_net *in = nf_ct_l3proto_net(net, l3proto); |
205 | ||
206 | if (in == NULL) | |
207 | return; | |
208 | #if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT) | |
209 | if (in->ctl_table_header != NULL) | |
210 | nf_ct_unregister_sysctl(&in->ctl_table_header, | |
211 | &in->ctl_table, | |
fa34fff5 | 212 | 0); |
d62f9ed4 PM |
213 | #endif |
214 | } | |
215 | ||
524a53e5 G |
216 | static int |
217 | nf_conntrack_l3proto_register_net(struct nf_conntrack_l3proto *proto) | |
8f03dea5 MJ |
218 | { |
219 | int ret = 0; | |
0e60ebe0 | 220 | struct nf_conntrack_l3proto *old; |
8f03dea5 | 221 | |
0661cca9 PM |
222 | if (proto->l3proto >= AF_MAX) |
223 | return -EBUSY; | |
ae5718fb | 224 | |
d0dba725 HE |
225 | if (proto->tuple_to_nlattr && !proto->nlattr_tuple_size) |
226 | return -EINVAL; | |
227 | ||
b19caa0c | 228 | mutex_lock(&nf_ct_proto_mutex); |
0e60ebe0 ED |
229 | old = rcu_dereference_protected(nf_ct_l3protos[proto->l3proto], |
230 | lockdep_is_held(&nf_ct_proto_mutex)); | |
231 | if (old != &nf_conntrack_l3proto_generic) { | |
8f03dea5 | 232 | ret = -EBUSY; |
ae5718fb | 233 | goto out_unlock; |
8f03dea5 | 234 | } |
d62f9ed4 | 235 | |
d0dba725 HE |
236 | if (proto->nlattr_tuple_size) |
237 | proto->nla_size = 3 * proto->nlattr_tuple_size(); | |
238 | ||
0661cca9 | 239 | rcu_assign_pointer(nf_ct_l3protos[proto->l3proto], proto); |
8f03dea5 | 240 | |
ae5718fb | 241 | out_unlock: |
b19caa0c | 242 | mutex_unlock(&nf_ct_proto_mutex); |
8f03dea5 | 243 | return ret; |
524a53e5 | 244 | |
8f03dea5 MJ |
245 | } |
246 | ||
524a53e5 G |
247 | int nf_conntrack_l3proto_register(struct net *net, |
248 | struct nf_conntrack_l3proto *proto) | |
8f03dea5 | 249 | { |
524a53e5 G |
250 | int ret = 0; |
251 | ||
fa0f61f0 G |
252 | if (proto->init_net) { |
253 | ret = proto->init_net(net); | |
254 | if (ret < 0) | |
255 | return ret; | |
256 | } | |
524a53e5 | 257 | |
fa0f61f0 | 258 | ret = nf_ct_l3proto_register_sysctl(net, proto); |
524a53e5 G |
259 | if (ret < 0) |
260 | return ret; | |
261 | ||
fa0f61f0 G |
262 | if (net == &init_net) { |
263 | ret = nf_conntrack_l3proto_register_net(proto); | |
524a53e5 | 264 | if (ret < 0) |
fa0f61f0 | 265 | nf_ct_l3proto_unregister_sysctl(net, proto); |
524a53e5 | 266 | } |
fa0f61f0 G |
267 | |
268 | return ret; | |
524a53e5 G |
269 | } |
270 | EXPORT_SYMBOL_GPL(nf_conntrack_l3proto_register); | |
678d6675 | 271 | |
524a53e5 G |
272 | static void |
273 | nf_conntrack_l3proto_unregister_net(struct nf_conntrack_l3proto *proto) | |
274 | { | |
fe3eb20c | 275 | BUG_ON(proto->l3proto >= AF_MAX); |
ae5718fb | 276 | |
b19caa0c | 277 | mutex_lock(&nf_ct_proto_mutex); |
0e60ebe0 ED |
278 | BUG_ON(rcu_dereference_protected(nf_ct_l3protos[proto->l3proto], |
279 | lockdep_is_held(&nf_ct_proto_mutex) | |
280 | ) != proto); | |
923f4902 PM |
281 | rcu_assign_pointer(nf_ct_l3protos[proto->l3proto], |
282 | &nf_conntrack_l3proto_generic); | |
b19caa0c | 283 | mutex_unlock(&nf_ct_proto_mutex); |
8f03dea5 | 284 | |
0661cca9 | 285 | synchronize_rcu(); |
524a53e5 G |
286 | } |
287 | ||
288 | void nf_conntrack_l3proto_unregister(struct net *net, | |
289 | struct nf_conntrack_l3proto *proto) | |
290 | { | |
291 | if (net == &init_net) | |
292 | nf_conntrack_l3proto_unregister_net(proto); | |
293 | ||
294 | nf_ct_l3proto_unregister_sysctl(net, proto); | |
d62f9ed4 | 295 | |
8f03dea5 | 296 | /* Remove all contrack entries for this protocol */ |
efb9a8c2 | 297 | rtnl_lock(); |
524a53e5 | 298 | nf_ct_iterate_cleanup(net, kill_l3proto, proto); |
efb9a8c2 | 299 | rtnl_unlock(); |
8f03dea5 | 300 | } |
13b18339 | 301 | EXPORT_SYMBOL_GPL(nf_conntrack_l3proto_unregister); |
8f03dea5 | 302 | |
2c352f44 G |
303 | static struct nf_proto_net *nf_ct_l4proto_net(struct net *net, |
304 | struct nf_conntrack_l4proto *l4proto) | |
305 | { | |
15f585bd | 306 | switch (l4proto->l4proto) { |
d2ba1fde G |
307 | case IPPROTO_TCP: |
308 | return (struct nf_proto_net *)&net->ct.nf_ct_proto.tcp; | |
0ce490ad G |
309 | case IPPROTO_UDP: |
310 | return (struct nf_proto_net *)&net->ct.nf_ct_proto.udp; | |
4b626b9c G |
311 | case IPPROTO_ICMP: |
312 | return (struct nf_proto_net *)&net->ct.nf_ct_proto.icmp; | |
7080ba09 G |
313 | case IPPROTO_ICMPV6: |
314 | return (struct nf_proto_net *)&net->ct.nf_ct_proto.icmpv6; | |
15f585bd G |
315 | case 255: /* l4proto_generic */ |
316 | return (struct nf_proto_net *)&net->ct.nf_ct_proto.generic; | |
317 | default: | |
318 | if (l4proto->net_id) | |
319 | return net_generic(net, *l4proto->net_id); | |
320 | else | |
321 | return NULL; | |
322 | } | |
323 | return NULL; | |
2c352f44 G |
324 | } |
325 | ||
326 | static | |
327 | int nf_ct_l4proto_register_sysctl(struct net *net, | |
fa34fff5 | 328 | struct nf_proto_net *pn, |
2c352f44 | 329 | struct nf_conntrack_l4proto *l4proto) |
d62f9ed4 PM |
330 | { |
331 | int err = 0; | |
332 | ||
333 | #ifdef CONFIG_SYSCTL | |
2c352f44 G |
334 | if (pn->ctl_table != NULL) { |
335 | err = nf_ct_register_sysctl(net, | |
336 | &pn->ctl_table_header, | |
f99e8f71 | 337 | "net/netfilter", |
fa34fff5 | 338 | pn->ctl_table); |
2c352f44 G |
339 | if (err < 0) { |
340 | if (!pn->users) { | |
341 | kfree(pn->ctl_table); | |
342 | pn->ctl_table = NULL; | |
343 | } | |
a999e683 | 344 | goto out; |
2c352f44 | 345 | } |
d62f9ed4 | 346 | } |
a999e683 | 347 | #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT |
2c352f44 G |
348 | if (l4proto->l3proto != AF_INET6 && pn->ctl_compat_table != NULL) { |
349 | err = nf_ct_register_sysctl(net, | |
350 | &pn->ctl_compat_header, | |
f99e8f71 | 351 | "net/ipv4/netfilter", |
fa34fff5 | 352 | pn->ctl_compat_table); |
a999e683 PM |
353 | if (err == 0) |
354 | goto out; | |
2c352f44 | 355 | |
f28997e2 | 356 | nf_ct_kfree_compat_sysctl_table(pn); |
2c352f44 G |
357 | nf_ct_unregister_sysctl(&pn->ctl_table_header, |
358 | &pn->ctl_table, | |
fa34fff5 | 359 | pn->users); |
a999e683 PM |
360 | } |
361 | #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */ | |
362 | out: | |
933a41e7 | 363 | #endif /* CONFIG_SYSCTL */ |
d62f9ed4 PM |
364 | return err; |
365 | } | |
366 | ||
2c352f44 G |
367 | static |
368 | void nf_ct_l4proto_unregister_sysctl(struct net *net, | |
fa34fff5 | 369 | struct nf_proto_net *pn, |
2c352f44 | 370 | struct nf_conntrack_l4proto *l4proto) |
d62f9ed4 PM |
371 | { |
372 | #ifdef CONFIG_SYSCTL | |
2c352f44 G |
373 | if (pn->ctl_table_header != NULL) |
374 | nf_ct_unregister_sysctl(&pn->ctl_table_header, | |
375 | &pn->ctl_table, | |
fa34fff5 | 376 | pn->users); |
2c352f44 | 377 | |
a999e683 | 378 | #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT |
2c352f44 G |
379 | if (l4proto->l3proto != AF_INET6 && pn->ctl_compat_header != NULL) |
380 | nf_ct_unregister_sysctl(&pn->ctl_compat_header, | |
381 | &pn->ctl_compat_table, | |
fa34fff5 | 382 | 0); |
a999e683 | 383 | #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */ |
933a41e7 | 384 | #endif /* CONFIG_SYSCTL */ |
d62f9ed4 PM |
385 | } |
386 | ||
8f03dea5 MJ |
387 | /* FIXME: Allow NULL functions and sub in pointers to generic for |
388 | them. --RR */ | |
2c352f44 G |
389 | static int |
390 | nf_conntrack_l4proto_register_net(struct nf_conntrack_l4proto *l4proto) | |
8f03dea5 MJ |
391 | { |
392 | int ret = 0; | |
393 | ||
0661cca9 PM |
394 | if (l4proto->l3proto >= PF_MAX) |
395 | return -EBUSY; | |
ae5718fb | 396 | |
d0dba725 HE |
397 | if ((l4proto->to_nlattr && !l4proto->nlattr_size) |
398 | || (l4proto->tuple_to_nlattr && !l4proto->nlattr_tuple_size)) | |
399 | return -EINVAL; | |
400 | ||
b19caa0c | 401 | mutex_lock(&nf_ct_proto_mutex); |
c6a1e615 | 402 | if (!nf_ct_protos[l4proto->l3proto]) { |
8f03dea5 | 403 | /* l3proto may be loaded latter. */ |
c5d277d2 | 404 | struct nf_conntrack_l4proto __rcu **proto_array; |
8f03dea5 MJ |
405 | int i; |
406 | ||
c6a1e615 PM |
407 | proto_array = kmalloc(MAX_NF_CT_PROTO * |
408 | sizeof(struct nf_conntrack_l4proto *), | |
409 | GFP_KERNEL); | |
8f03dea5 MJ |
410 | if (proto_array == NULL) { |
411 | ret = -ENOMEM; | |
b19caa0c | 412 | goto out_unlock; |
8f03dea5 | 413 | } |
c6a1e615 | 414 | |
8f03dea5 | 415 | for (i = 0; i < MAX_NF_CT_PROTO; i++) |
c5d277d2 | 416 | RCU_INIT_POINTER(proto_array[i], &nf_conntrack_l4proto_generic); |
d817d29d ED |
417 | |
418 | /* Before making proto_array visible to lockless readers, | |
419 | * we must make sure its content is committed to memory. | |
420 | */ | |
421 | smp_wmb(); | |
422 | ||
c6a1e615 | 423 | nf_ct_protos[l4proto->l3proto] = proto_array; |
0e60ebe0 ED |
424 | } else if (rcu_dereference_protected( |
425 | nf_ct_protos[l4proto->l3proto][l4proto->l4proto], | |
426 | lockdep_is_held(&nf_ct_proto_mutex) | |
427 | ) != &nf_conntrack_l4proto_generic) { | |
c6a1e615 PM |
428 | ret = -EBUSY; |
429 | goto out_unlock; | |
8f03dea5 MJ |
430 | } |
431 | ||
d0dba725 HE |
432 | l4proto->nla_size = 0; |
433 | if (l4proto->nlattr_size) | |
434 | l4proto->nla_size += l4proto->nlattr_size(); | |
435 | if (l4proto->nlattr_tuple_size) | |
436 | l4proto->nla_size += 3 * l4proto->nlattr_tuple_size(); | |
437 | ||
c6a1e615 PM |
438 | rcu_assign_pointer(nf_ct_protos[l4proto->l3proto][l4proto->l4proto], |
439 | l4proto); | |
8f03dea5 | 440 | out_unlock: |
b19caa0c | 441 | mutex_unlock(&nf_ct_proto_mutex); |
8f03dea5 MJ |
442 | return ret; |
443 | } | |
444 | ||
2c352f44 G |
445 | int nf_conntrack_l4proto_register(struct net *net, |
446 | struct nf_conntrack_l4proto *l4proto) | |
8f03dea5 | 447 | { |
2c352f44 | 448 | int ret = 0; |
fa34fff5 | 449 | struct nf_proto_net *pn = NULL; |
2c352f44 | 450 | |
fa0f61f0 | 451 | if (l4proto->init_net) { |
f1caad27 | 452 | ret = l4proto->init_net(net, l4proto->l3proto); |
fa0f61f0 | 453 | if (ret < 0) |
fa34fff5 | 454 | goto out; |
fa0f61f0 | 455 | } |
678d6675 | 456 | |
fa34fff5 G |
457 | pn = nf_ct_l4proto_net(net, l4proto); |
458 | if (pn == NULL) | |
459 | goto out; | |
460 | ||
461 | ret = nf_ct_l4proto_register_sysctl(net, pn, l4proto); | |
2c352f44 | 462 | if (ret < 0) |
fa34fff5 | 463 | goto out; |
2c352f44 | 464 | |
fa0f61f0 G |
465 | if (net == &init_net) { |
466 | ret = nf_conntrack_l4proto_register_net(l4proto); | |
fa34fff5 G |
467 | if (ret < 0) { |
468 | nf_ct_l4proto_unregister_sysctl(net, pn, l4proto); | |
469 | goto out; | |
470 | } | |
fa0f61f0 G |
471 | } |
472 | ||
fa34fff5 G |
473 | pn->users++; |
474 | out: | |
fa0f61f0 | 475 | return ret; |
2c352f44 G |
476 | } |
477 | EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_register); | |
478 | ||
479 | static void | |
480 | nf_conntrack_l4proto_unregister_net(struct nf_conntrack_l4proto *l4proto) | |
481 | { | |
fe3eb20c | 482 | BUG_ON(l4proto->l3proto >= PF_MAX); |
ae5718fb | 483 | |
b19caa0c | 484 | mutex_lock(&nf_ct_proto_mutex); |
0e60ebe0 ED |
485 | BUG_ON(rcu_dereference_protected( |
486 | nf_ct_protos[l4proto->l3proto][l4proto->l4proto], | |
487 | lockdep_is_held(&nf_ct_proto_mutex) | |
488 | ) != l4proto); | |
923f4902 PM |
489 | rcu_assign_pointer(nf_ct_protos[l4proto->l3proto][l4proto->l4proto], |
490 | &nf_conntrack_l4proto_generic); | |
b19caa0c | 491 | mutex_unlock(&nf_ct_proto_mutex); |
8f03dea5 | 492 | |
0661cca9 | 493 | synchronize_rcu(); |
2c352f44 | 494 | } |
d62f9ed4 | 495 | |
2c352f44 G |
496 | void nf_conntrack_l4proto_unregister(struct net *net, |
497 | struct nf_conntrack_l4proto *l4proto) | |
498 | { | |
fa34fff5 G |
499 | struct nf_proto_net *pn = NULL; |
500 | ||
2c352f44 G |
501 | if (net == &init_net) |
502 | nf_conntrack_l4proto_unregister_net(l4proto); | |
503 | ||
fa34fff5 G |
504 | pn = nf_ct_l4proto_net(net, l4proto); |
505 | if (pn == NULL) | |
506 | return; | |
507 | ||
508 | pn->users--; | |
509 | nf_ct_l4proto_unregister_sysctl(net, pn, l4proto); | |
510 | ||
8f03dea5 | 511 | /* Remove all contrack entries for this protocol */ |
efb9a8c2 | 512 | rtnl_lock(); |
2c352f44 | 513 | nf_ct_iterate_cleanup(net, kill_l4proto, l4proto); |
efb9a8c2 | 514 | rtnl_unlock(); |
8f03dea5 | 515 | } |
13b18339 | 516 | EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_unregister); |
ac5357eb | 517 | |
15f585bd | 518 | int nf_conntrack_proto_init(struct net *net) |
ac5357eb PM |
519 | { |
520 | unsigned int i; | |
521 | int err; | |
fa34fff5 G |
522 | struct nf_proto_net *pn = nf_ct_l4proto_net(net, |
523 | &nf_conntrack_l4proto_generic); | |
524 | ||
f1caad27 G |
525 | err = nf_conntrack_l4proto_generic.init_net(net, |
526 | nf_conntrack_l4proto_generic.l3proto); | |
15f585bd G |
527 | if (err < 0) |
528 | return err; | |
529 | err = nf_ct_l4proto_register_sysctl(net, | |
fa34fff5 | 530 | pn, |
15f585bd | 531 | &nf_conntrack_l4proto_generic); |
ac5357eb PM |
532 | if (err < 0) |
533 | return err; | |
534 | ||
15f585bd G |
535 | if (net == &init_net) { |
536 | for (i = 0; i < AF_MAX; i++) | |
537 | rcu_assign_pointer(nf_ct_l3protos[i], | |
538 | &nf_conntrack_l3proto_generic); | |
539 | } | |
fa34fff5 G |
540 | |
541 | pn->users++; | |
ac5357eb PM |
542 | return 0; |
543 | } | |
544 | ||
15f585bd | 545 | void nf_conntrack_proto_fini(struct net *net) |
ac5357eb PM |
546 | { |
547 | unsigned int i; | |
fa34fff5 G |
548 | struct nf_proto_net *pn = nf_ct_l4proto_net(net, |
549 | &nf_conntrack_l4proto_generic); | |
550 | ||
551 | pn->users--; | |
15f585bd | 552 | nf_ct_l4proto_unregister_sysctl(net, |
fa34fff5 | 553 | pn, |
15f585bd G |
554 | &nf_conntrack_l4proto_generic); |
555 | if (net == &init_net) { | |
556 | /* free l3proto protocol tables */ | |
557 | for (i = 0; i < PF_MAX; i++) | |
558 | kfree(nf_ct_protos[i]); | |
559 | } | |
ac5357eb | 560 | } |