Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
7e5d03bb MJ |
2 | /* Helper handling for netfilter. */ |
3 | ||
4 | /* (C) 1999-2001 Paul `Rusty' Russell | |
5 | * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org> | |
6 | * (C) 2003,2004 USAGI/WIDE Project <http://www.linux-ipv6.org> | |
f229f6ce | 7 | * (C) 2006-2012 Patrick McHardy <kaber@trash.net> |
7e5d03bb MJ |
8 | */ |
9 | ||
10 | #include <linux/types.h> | |
11 | #include <linux/netfilter.h> | |
12 | #include <linux/module.h> | |
13 | #include <linux/skbuff.h> | |
14 | #include <linux/vmalloc.h> | |
15 | #include <linux/stddef.h> | |
7e5d03bb MJ |
16 | #include <linux/random.h> |
17 | #include <linux/err.h> | |
18 | #include <linux/kernel.h> | |
19 | #include <linux/netdevice.h> | |
2ba4cc31 | 20 | #include <linux/rculist.h> |
efb9a8c2 | 21 | #include <linux/rtnetlink.h> |
7e5d03bb | 22 | |
7e5d03bb | 23 | #include <net/netfilter/nf_conntrack.h> |
7e5d03bb | 24 | #include <net/netfilter/nf_conntrack_core.h> |
40d102cd | 25 | #include <net/netfilter/nf_conntrack_ecache.h> |
ceceae1b | 26 | #include <net/netfilter/nf_conntrack_extend.h> |
40d102cd JS |
27 | #include <net/netfilter/nf_conntrack_helper.h> |
28 | #include <net/netfilter/nf_conntrack_l4proto.h> | |
ca71277f | 29 | #include <net/netfilter/nf_conntrack_seqadj.h> |
b20ab9cc | 30 | #include <net/netfilter/nf_log.h> |
ca71277f | 31 | #include <net/ip.h> |
7e5d03bb | 32 | |
58a3c9bb | 33 | static DEFINE_MUTEX(nf_ct_helper_mutex); |
12f7a505 PNA |
34 | struct hlist_head *nf_ct_helper_hash __read_mostly; |
35 | EXPORT_SYMBOL_GPL(nf_ct_helper_hash); | |
36 | unsigned int nf_ct_helper_hsize __read_mostly; | |
37 | EXPORT_SYMBOL_GPL(nf_ct_helper_hsize); | |
b8a7fe6c | 38 | static unsigned int nf_ct_helper_count __read_mostly; |
b8a7fe6c | 39 | |
08010a21 FL |
40 | static DEFINE_MUTEX(nf_ct_nat_helpers_mutex); |
41 | static struct list_head nf_ct_nat_helpers __read_mostly; | |
42 | ||
b8a7fe6c PM |
43 | /* Stupid hash, but collision free for the default registrations of the |
44 | * helpers currently in the kernel. */ | |
45 | static unsigned int helper_hash(const struct nf_conntrack_tuple *tuple) | |
46 | { | |
47 | return (((tuple->src.l3num << 8) | tuple->dst.protonum) ^ | |
a34c4589 | 48 | (__force __u16)tuple->src.u.all) % nf_ct_helper_hsize; |
b8a7fe6c | 49 | } |
7e5d03bb | 50 | |
7e5d03bb | 51 | struct nf_conntrack_helper * |
794e6871 | 52 | __nf_conntrack_helper_find(const char *name, u16 l3num, u8 protonum) |
7e5d03bb MJ |
53 | { |
54 | struct nf_conntrack_helper *h; | |
b8a7fe6c | 55 | unsigned int i; |
7e5d03bb | 56 | |
b8a7fe6c | 57 | for (i = 0; i < nf_ct_helper_hsize; i++) { |
b67bfe0d | 58 | hlist_for_each_entry_rcu(h, &nf_ct_helper_hash[i], hnode) { |
6114cc51 FW |
59 | if (strcmp(h->name, name)) |
60 | continue; | |
61 | ||
62 | if (h->tuple.src.l3num != NFPROTO_UNSPEC && | |
63 | h->tuple.src.l3num != l3num) | |
64 | continue; | |
65 | ||
66 | if (h->tuple.dst.protonum == protonum) | |
b8a7fe6c PM |
67 | return h; |
68 | } | |
7e5d03bb | 69 | } |
7e5d03bb MJ |
70 | return NULL; |
71 | } | |
794e6871 | 72 | EXPORT_SYMBOL_GPL(__nf_conntrack_helper_find); |
7e5d03bb | 73 | |
84f3bb9a PM |
74 | struct nf_conntrack_helper * |
75 | nf_conntrack_helper_try_module_get(const char *name, u16 l3num, u8 protonum) | |
76 | { | |
77 | struct nf_conntrack_helper *h; | |
78 | ||
8b5995d0 GF |
79 | rcu_read_lock(); |
80 | ||
84f3bb9a PM |
81 | h = __nf_conntrack_helper_find(name, l3num, protonum); |
82 | #ifdef CONFIG_MODULES | |
83 | if (h == NULL) { | |
8b5995d0 GF |
84 | rcu_read_unlock(); |
85 | if (request_module("nfct-helper-%s", name) == 0) { | |
86 | rcu_read_lock(); | |
84f3bb9a | 87 | h = __nf_conntrack_helper_find(name, l3num, protonum); |
8b5995d0 GF |
88 | } else { |
89 | return h; | |
90 | } | |
84f3bb9a PM |
91 | } |
92 | #endif | |
93 | if (h != NULL && !try_module_get(h->me)) | |
94 | h = NULL; | |
9338d7b4 LZ |
95 | if (h != NULL && !refcount_inc_not_zero(&h->refcnt)) { |
96 | module_put(h->me); | |
97 | h = NULL; | |
98 | } | |
84f3bb9a | 99 | |
8b5995d0 GF |
100 | rcu_read_unlock(); |
101 | ||
84f3bb9a PM |
102 | return h; |
103 | } | |
104 | EXPORT_SYMBOL_GPL(nf_conntrack_helper_try_module_get); | |
105 | ||
d91fc59c LZ |
106 | void nf_conntrack_helper_put(struct nf_conntrack_helper *helper) |
107 | { | |
9338d7b4 | 108 | refcount_dec(&helper->refcnt); |
d91fc59c LZ |
109 | module_put(helper->me); |
110 | } | |
111 | EXPORT_SYMBOL_GPL(nf_conntrack_helper_put); | |
112 | ||
08010a21 FL |
113 | static struct nf_conntrack_nat_helper * |
114 | nf_conntrack_nat_helper_find(const char *mod_name) | |
115 | { | |
116 | struct nf_conntrack_nat_helper *cur; | |
117 | bool found = false; | |
118 | ||
119 | list_for_each_entry_rcu(cur, &nf_ct_nat_helpers, list) { | |
120 | if (!strcmp(cur->mod_name, mod_name)) { | |
121 | found = true; | |
122 | break; | |
123 | } | |
124 | } | |
125 | return found ? cur : NULL; | |
126 | } | |
127 | ||
128 | int | |
129 | nf_nat_helper_try_module_get(const char *name, u16 l3num, u8 protonum) | |
130 | { | |
131 | struct nf_conntrack_helper *h; | |
132 | struct nf_conntrack_nat_helper *nat; | |
133 | char mod_name[NF_CT_HELPER_NAME_LEN]; | |
134 | int ret = 0; | |
135 | ||
136 | rcu_read_lock(); | |
137 | h = __nf_conntrack_helper_find(name, l3num, protonum); | |
138 | if (!h) { | |
139 | rcu_read_unlock(); | |
140 | return -ENOENT; | |
141 | } | |
142 | ||
143 | nat = nf_conntrack_nat_helper_find(h->nat_mod_name); | |
144 | if (!nat) { | |
145 | snprintf(mod_name, sizeof(mod_name), "%s", h->nat_mod_name); | |
146 | rcu_read_unlock(); | |
b8acd431 | 147 | request_module("%s", mod_name); |
08010a21 FL |
148 | |
149 | rcu_read_lock(); | |
150 | nat = nf_conntrack_nat_helper_find(mod_name); | |
151 | if (!nat) { | |
152 | rcu_read_unlock(); | |
153 | return -ENOENT; | |
154 | } | |
155 | } | |
156 | ||
157 | if (!try_module_get(nat->module)) | |
158 | ret = -ENOENT; | |
159 | ||
160 | rcu_read_unlock(); | |
161 | return ret; | |
162 | } | |
163 | EXPORT_SYMBOL_GPL(nf_nat_helper_try_module_get); | |
164 | ||
165 | void nf_nat_helper_put(struct nf_conntrack_helper *helper) | |
166 | { | |
167 | struct nf_conntrack_nat_helper *nat; | |
168 | ||
169 | nat = nf_conntrack_nat_helper_find(helper->nat_mod_name); | |
170 | if (WARN_ON_ONCE(!nat)) | |
171 | return; | |
172 | ||
173 | module_put(nat->module); | |
174 | } | |
175 | EXPORT_SYMBOL_GPL(nf_nat_helper_put); | |
176 | ||
1afc5679 | 177 | struct nf_conn_help * |
440534d3 | 178 | nf_ct_helper_ext_add(struct nf_conn *ct, gfp_t gfp) |
b560580a PM |
179 | { |
180 | struct nf_conn_help *help; | |
181 | ||
9f0f3ebe | 182 | help = nf_ct_ext_add(ct, NF_CT_EXT_HELPER, gfp); |
b560580a PM |
183 | if (help) |
184 | INIT_HLIST_HEAD(&help->expectations); | |
185 | else | |
186 | pr_debug("failed to add helper extension area"); | |
187 | return help; | |
188 | } | |
189 | EXPORT_SYMBOL_GPL(nf_ct_helper_ext_add); | |
190 | ||
b2a15a60 PM |
191 | int __nf_ct_try_assign_helper(struct nf_conn *ct, struct nf_conn *tmpl, |
192 | gfp_t flags) | |
226c0c0e | 193 | { |
b2a15a60 PM |
194 | struct nf_conntrack_helper *helper = NULL; |
195 | struct nf_conn_help *help; | |
226c0c0e | 196 | |
6714cf54 PNA |
197 | /* We already got a helper explicitly attached. The function |
198 | * nf_conntrack_alter_reply - in case NAT is in use - asks for looking | |
199 | * the helper up again. Since now the user is in full control of | |
200 | * making consistent helper configurations, skip this automatic | |
201 | * re-lookup, otherwise we'll lose the helper. | |
202 | */ | |
203 | if (test_bit(IPS_HELPER_BIT, &ct->status)) | |
204 | return 0; | |
205 | ||
b1185090 PNA |
206 | if (WARN_ON_ONCE(!tmpl)) |
207 | return 0; | |
208 | ||
209 | help = nfct_help(tmpl); | |
210 | if (help != NULL) { | |
211 | helper = rcu_dereference(help->helper); | |
212 | set_bit(IPS_HELPER_BIT, &ct->status); | |
b2a15a60 PM |
213 | } |
214 | ||
215 | help = nfct_help(ct); | |
a9006892 | 216 | |
226c0c0e | 217 | if (helper == NULL) { |
b1185090 PNA |
218 | if (help) |
219 | RCU_INIT_POINTER(help->helper, NULL); | |
220 | return 0; | |
226c0c0e PNA |
221 | } |
222 | ||
223 | if (help == NULL) { | |
440534d3 | 224 | help = nf_ct_helper_ext_add(ct, flags); |
cf71c03e PN |
225 | if (help == NULL) |
226 | return -ENOMEM; | |
226c0c0e | 227 | } else { |
32f53760 PNA |
228 | /* We only allow helper re-assignment of the same sort since |
229 | * we cannot reallocate the helper extension area. | |
230 | */ | |
6e2f0aa8 FW |
231 | struct nf_conntrack_helper *tmp = rcu_dereference(help->helper); |
232 | ||
233 | if (tmp && tmp->help != helper->help) { | |
32f53760 | 234 | RCU_INIT_POINTER(help->helper, NULL); |
cf71c03e | 235 | return 0; |
32f53760 | 236 | } |
226c0c0e PNA |
237 | } |
238 | ||
cf778b00 | 239 | rcu_assign_pointer(help->helper, helper); |
cf71c03e PN |
240 | |
241 | return 0; | |
226c0c0e PNA |
242 | } |
243 | EXPORT_SYMBOL_GPL(__nf_ct_try_assign_helper); | |
244 | ||
ca71277f XL |
245 | /* 'skb' should already be pulled to nh_ofs. */ |
246 | int nf_ct_helper(struct sk_buff *skb, struct nf_conn *ct, | |
247 | enum ip_conntrack_info ctinfo, u16 proto) | |
248 | { | |
249 | const struct nf_conntrack_helper *helper; | |
250 | const struct nf_conn_help *help; | |
251 | unsigned int protoff; | |
252 | int err; | |
253 | ||
254 | if (ctinfo == IP_CT_RELATED_REPLY) | |
255 | return NF_ACCEPT; | |
256 | ||
257 | help = nfct_help(ct); | |
258 | if (!help) | |
259 | return NF_ACCEPT; | |
260 | ||
261 | helper = rcu_dereference(help->helper); | |
262 | if (!helper) | |
263 | return NF_ACCEPT; | |
264 | ||
265 | if (helper->tuple.src.l3num != NFPROTO_UNSPEC && | |
266 | helper->tuple.src.l3num != proto) | |
267 | return NF_ACCEPT; | |
268 | ||
269 | switch (proto) { | |
270 | case NFPROTO_IPV4: | |
271 | protoff = ip_hdrlen(skb); | |
272 | proto = ip_hdr(skb)->protocol; | |
273 | break; | |
274 | case NFPROTO_IPV6: { | |
275 | u8 nexthdr = ipv6_hdr(skb)->nexthdr; | |
276 | __be16 frag_off; | |
277 | int ofs; | |
278 | ||
279 | ofs = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr, | |
280 | &frag_off); | |
281 | if (ofs < 0 || (frag_off & htons(~0x7)) != 0) { | |
282 | pr_debug("proto header not found\n"); | |
283 | return NF_ACCEPT; | |
284 | } | |
285 | protoff = ofs; | |
286 | proto = nexthdr; | |
287 | break; | |
288 | } | |
289 | default: | |
290 | WARN_ONCE(1, "helper invoked on non-IP family!"); | |
291 | return NF_DROP; | |
292 | } | |
293 | ||
294 | if (helper->tuple.dst.protonum != proto) | |
295 | return NF_ACCEPT; | |
296 | ||
297 | err = helper->help(skb, protoff, ct, ctinfo); | |
298 | if (err != NF_ACCEPT) | |
299 | return err; | |
300 | ||
301 | /* Adjust seqs after helper. This is needed due to some helpers (e.g., | |
302 | * FTP with NAT) adusting the TCP payload size when mangling IP | |
303 | * addresses and/or port numbers in the text-based control connection. | |
304 | */ | |
305 | if (test_bit(IPS_SEQ_ADJUST_BIT, &ct->status) && | |
306 | !nf_ct_seq_adjust(skb, ct, ctinfo, protoff)) | |
307 | return NF_DROP; | |
308 | return NF_ACCEPT; | |
309 | } | |
310 | EXPORT_SYMBOL_GPL(nf_ct_helper); | |
311 | ||
f96cba2e XL |
312 | int nf_ct_add_helper(struct nf_conn *ct, const char *name, u8 family, |
313 | u8 proto, bool nat, struct nf_conntrack_helper **hp) | |
314 | { | |
315 | struct nf_conntrack_helper *helper; | |
316 | struct nf_conn_help *help; | |
317 | int ret = 0; | |
318 | ||
319 | helper = nf_conntrack_helper_try_module_get(name, family, proto); | |
320 | if (!helper) | |
321 | return -EINVAL; | |
322 | ||
323 | help = nf_ct_helper_ext_add(ct, GFP_KERNEL); | |
324 | if (!help) { | |
325 | nf_conntrack_helper_put(helper); | |
326 | return -ENOMEM; | |
327 | } | |
328 | #if IS_ENABLED(CONFIG_NF_NAT) | |
329 | if (nat) { | |
330 | ret = nf_nat_helper_try_module_get(name, family, proto); | |
331 | if (ret) { | |
332 | nf_conntrack_helper_put(helper); | |
333 | return ret; | |
334 | } | |
335 | } | |
336 | #endif | |
337 | rcu_assign_pointer(help->helper, helper); | |
338 | *hp = helper; | |
339 | return ret; | |
340 | } | |
341 | EXPORT_SYMBOL_GPL(nf_ct_add_helper); | |
342 | ||
01cfa0a4 | 343 | /* appropriate ct lock protecting must be taken by caller */ |
ff1acc49 | 344 | static int unhelp(struct nf_conn *ct, void *me) |
7e5d03bb | 345 | { |
7e5d03bb MJ |
346 | struct nf_conn_help *help = nfct_help(ct); |
347 | ||
ca7433df | 348 | if (help && rcu_dereference_raw(help->helper) == me) { |
7e5d03bb | 349 | nf_conntrack_event(IPCT_HELPER, ct); |
a9b3cd7f | 350 | RCU_INIT_POINTER(help->helper, NULL); |
7e5d03bb | 351 | } |
ff1acc49 LZ |
352 | |
353 | /* We are not intended to delete this conntrack. */ | |
7e5d03bb MJ |
354 | return 0; |
355 | } | |
356 | ||
9858a3ae PNA |
357 | void nf_ct_helper_destroy(struct nf_conn *ct) |
358 | { | |
359 | struct nf_conn_help *help = nfct_help(ct); | |
360 | struct nf_conntrack_helper *helper; | |
361 | ||
362 | if (help) { | |
363 | rcu_read_lock(); | |
364 | helper = rcu_dereference(help->helper); | |
365 | if (helper && helper->destroy) | |
366 | helper->destroy(ct); | |
367 | rcu_read_unlock(); | |
368 | } | |
369 | } | |
370 | ||
544d5c7d PNA |
371 | static LIST_HEAD(nf_ct_helper_expectfn_list); |
372 | ||
373 | void nf_ct_helper_expectfn_register(struct nf_ct_helper_expectfn *n) | |
374 | { | |
ca7433df | 375 | spin_lock_bh(&nf_conntrack_expect_lock); |
544d5c7d | 376 | list_add_rcu(&n->head, &nf_ct_helper_expectfn_list); |
ca7433df | 377 | spin_unlock_bh(&nf_conntrack_expect_lock); |
544d5c7d PNA |
378 | } |
379 | EXPORT_SYMBOL_GPL(nf_ct_helper_expectfn_register); | |
380 | ||
381 | void nf_ct_helper_expectfn_unregister(struct nf_ct_helper_expectfn *n) | |
382 | { | |
ca7433df | 383 | spin_lock_bh(&nf_conntrack_expect_lock); |
544d5c7d | 384 | list_del_rcu(&n->head); |
ca7433df | 385 | spin_unlock_bh(&nf_conntrack_expect_lock); |
544d5c7d PNA |
386 | } |
387 | EXPORT_SYMBOL_GPL(nf_ct_helper_expectfn_unregister); | |
388 | ||
8b5995d0 | 389 | /* Caller should hold the rcu lock */ |
544d5c7d PNA |
390 | struct nf_ct_helper_expectfn * |
391 | nf_ct_helper_expectfn_find_by_name(const char *name) | |
392 | { | |
393 | struct nf_ct_helper_expectfn *cur; | |
394 | bool found = false; | |
395 | ||
544d5c7d PNA |
396 | list_for_each_entry_rcu(cur, &nf_ct_helper_expectfn_list, head) { |
397 | if (!strcmp(cur->name, name)) { | |
398 | found = true; | |
399 | break; | |
400 | } | |
401 | } | |
544d5c7d PNA |
402 | return found ? cur : NULL; |
403 | } | |
404 | EXPORT_SYMBOL_GPL(nf_ct_helper_expectfn_find_by_name); | |
405 | ||
8b5995d0 | 406 | /* Caller should hold the rcu lock */ |
544d5c7d PNA |
407 | struct nf_ct_helper_expectfn * |
408 | nf_ct_helper_expectfn_find_by_symbol(const void *symbol) | |
409 | { | |
410 | struct nf_ct_helper_expectfn *cur; | |
411 | bool found = false; | |
412 | ||
544d5c7d PNA |
413 | list_for_each_entry_rcu(cur, &nf_ct_helper_expectfn_list, head) { |
414 | if (cur->expectfn == symbol) { | |
415 | found = true; | |
416 | break; | |
417 | } | |
418 | } | |
544d5c7d PNA |
419 | return found ? cur : NULL; |
420 | } | |
421 | EXPORT_SYMBOL_GPL(nf_ct_helper_expectfn_find_by_symbol); | |
422 | ||
b20ab9cc PNA |
423 | __printf(3, 4) |
424 | void nf_ct_helper_log(struct sk_buff *skb, const struct nf_conn *ct, | |
425 | const char *fmt, ...) | |
426 | { | |
427 | const struct nf_conn_help *help; | |
428 | const struct nf_conntrack_helper *helper; | |
f9caed59 JP |
429 | struct va_format vaf; |
430 | va_list args; | |
431 | ||
432 | va_start(args, fmt); | |
433 | ||
434 | vaf.fmt = fmt; | |
435 | vaf.va = &args; | |
b20ab9cc PNA |
436 | |
437 | /* Called from the helper function, this call never fails */ | |
438 | help = nfct_help(ct); | |
439 | ||
e2361cb9 | 440 | /* rcu_read_lock()ed by nf_hook_thresh */ |
b20ab9cc PNA |
441 | helper = rcu_dereference(help->helper); |
442 | ||
30e0c6a6 | 443 | nf_log_packet(nf_ct_net(ct), nf_ct_l3num(ct), 0, skb, NULL, NULL, NULL, |
f9caed59 JP |
444 | "nf_ct_%s: dropping packet: %pV ", helper->name, &vaf); |
445 | ||
446 | va_end(args); | |
b20ab9cc PNA |
447 | } |
448 | EXPORT_SYMBOL_GPL(nf_ct_helper_log); | |
449 | ||
7e5d03bb MJ |
450 | int nf_conntrack_helper_register(struct nf_conntrack_helper *me) |
451 | { | |
893e093c | 452 | struct nf_conntrack_tuple_mask mask = { .src.u.all = htons(0xFFFF) }; |
b8a7fe6c | 453 | unsigned int h = helper_hash(&me->tuple); |
893e093c | 454 | struct nf_conntrack_helper *cur; |
66e5a6b1 | 455 | int ret = 0, i; |
b8a7fe6c | 456 | |
6002f266 PM |
457 | BUG_ON(me->expect_policy == NULL); |
458 | BUG_ON(me->expect_class_max >= NF_CT_MAX_EXPECT_CLASSES); | |
af9d32ad | 459 | BUG_ON(strlen(me->name) > NF_CT_HELPER_NAME_LEN - 1); |
7e5d03bb | 460 | |
92f73221 GF |
461 | if (me->expect_policy->max_expected > NF_CT_EXPECT_MAX_CNT) |
462 | return -EINVAL; | |
463 | ||
58a3c9bb | 464 | mutex_lock(&nf_ct_helper_mutex); |
66e5a6b1 LZ |
465 | for (i = 0; i < nf_ct_helper_hsize; i++) { |
466 | hlist_for_each_entry(cur, &nf_ct_helper_hash[i], hnode) { | |
467 | if (!strcmp(cur->name, me->name) && | |
468 | (cur->tuple.src.l3num == NFPROTO_UNSPEC || | |
469 | cur->tuple.src.l3num == me->tuple.src.l3num) && | |
470 | cur->tuple.dst.protonum == me->tuple.dst.protonum) { | |
471 | ret = -EEXIST; | |
472 | goto out; | |
473 | } | |
474 | } | |
475 | } | |
476 | ||
477 | /* avoid unpredictable behaviour for auto_assign_helper */ | |
478 | if (!(me->flags & NF_CT_HELPER_F_USERSPACE)) { | |
479 | hlist_for_each_entry(cur, &nf_ct_helper_hash[h], hnode) { | |
480 | if (nf_ct_tuple_src_mask_cmp(&cur->tuple, &me->tuple, | |
481 | &mask)) { | |
482 | ret = -EEXIST; | |
483 | goto out; | |
484 | } | |
12f7a505 PNA |
485 | } |
486 | } | |
9338d7b4 | 487 | refcount_set(&me->refcnt, 1); |
58a3c9bb | 488 | hlist_add_head_rcu(&me->hnode, &nf_ct_helper_hash[h]); |
b8a7fe6c | 489 | nf_ct_helper_count++; |
12f7a505 | 490 | out: |
58a3c9bb | 491 | mutex_unlock(&nf_ct_helper_mutex); |
12f7a505 | 492 | return ret; |
7e5d03bb | 493 | } |
13b18339 | 494 | EXPORT_SYMBOL_GPL(nf_conntrack_helper_register); |
7e5d03bb | 495 | |
ac7b8483 | 496 | static bool expect_iter_me(struct nf_conntrack_expect *exp, void *data) |
7e5d03bb | 497 | { |
ac7b8483 FW |
498 | struct nf_conn_help *help = nfct_help(exp->master); |
499 | const struct nf_conntrack_helper *me = data; | |
500 | const struct nf_conntrack_helper *this; | |
501 | ||
502 | if (exp->helper == me) | |
503 | return true; | |
436a850d | 504 | |
ac7b8483 FW |
505 | this = rcu_dereference_protected(help->helper, |
506 | lockdep_is_held(&nf_conntrack_expect_lock)); | |
507 | return this == me; | |
508 | } | |
509 | ||
510 | void nf_conntrack_helper_unregister(struct nf_conntrack_helper *me) | |
511 | { | |
436a850d FW |
512 | mutex_lock(&nf_ct_helper_mutex); |
513 | hlist_del_rcu(&me->hnode); | |
514 | nf_ct_helper_count--; | |
515 | mutex_unlock(&nf_ct_helper_mutex); | |
516 | ||
517 | /* Make sure every nothing is still using the helper unless its a | |
518 | * connection in the hash. | |
519 | */ | |
520 | synchronize_rcu(); | |
7e5d03bb | 521 | |
ac7b8483 | 522 | nf_ct_expect_iterate_destroy(expect_iter_me, NULL); |
ff1acc49 | 523 | nf_ct_iterate_destroy(unhelp, me); |
68047937 | 524 | } |
13b18339 | 525 | EXPORT_SYMBOL_GPL(nf_conntrack_helper_unregister); |
ceceae1b | 526 | |
82de0be6 GF |
527 | void nf_ct_helper_init(struct nf_conntrack_helper *helper, |
528 | u16 l3num, u16 protonum, const char *name, | |
529 | u16 default_port, u16 spec_port, u32 id, | |
530 | const struct nf_conntrack_expect_policy *exp_pol, | |
9f0f3ebe | 531 | u32 expect_class_max, |
82de0be6 GF |
532 | int (*help)(struct sk_buff *skb, unsigned int protoff, |
533 | struct nf_conn *ct, | |
534 | enum ip_conntrack_info ctinfo), | |
535 | int (*from_nlattr)(struct nlattr *attr, | |
536 | struct nf_conn *ct), | |
537 | struct module *module) | |
538 | { | |
539 | helper->tuple.src.l3num = l3num; | |
540 | helper->tuple.dst.protonum = protonum; | |
541 | helper->tuple.src.u.all = htons(spec_port); | |
542 | helper->expect_policy = exp_pol; | |
543 | helper->expect_class_max = expect_class_max; | |
82de0be6 GF |
544 | helper->help = help; |
545 | helper->from_nlattr = from_nlattr; | |
546 | helper->me = module; | |
08010a21 FL |
547 | snprintf(helper->nat_mod_name, sizeof(helper->nat_mod_name), |
548 | NF_NAT_HELPER_PREFIX "%s", name); | |
82de0be6 GF |
549 | |
550 | if (spec_port == default_port) | |
551 | snprintf(helper->name, sizeof(helper->name), "%s", name); | |
552 | else | |
553 | snprintf(helper->name, sizeof(helper->name), "%s-%u", name, id); | |
554 | } | |
555 | EXPORT_SYMBOL_GPL(nf_ct_helper_init); | |
556 | ||
557 | int nf_conntrack_helpers_register(struct nf_conntrack_helper *helper, | |
558 | unsigned int n) | |
559 | { | |
560 | unsigned int i; | |
561 | int err = 0; | |
562 | ||
563 | for (i = 0; i < n; i++) { | |
564 | err = nf_conntrack_helper_register(&helper[i]); | |
565 | if (err < 0) | |
566 | goto err; | |
567 | } | |
568 | ||
569 | return err; | |
570 | err: | |
571 | if (i > 0) | |
572 | nf_conntrack_helpers_unregister(helper, i); | |
573 | return err; | |
574 | } | |
575 | EXPORT_SYMBOL_GPL(nf_conntrack_helpers_register); | |
576 | ||
577 | void nf_conntrack_helpers_unregister(struct nf_conntrack_helper *helper, | |
578 | unsigned int n) | |
579 | { | |
580 | while (n-- > 0) | |
581 | nf_conntrack_helper_unregister(&helper[n]); | |
582 | } | |
583 | EXPORT_SYMBOL_GPL(nf_conntrack_helpers_unregister); | |
584 | ||
08010a21 FL |
585 | void nf_nat_helper_register(struct nf_conntrack_nat_helper *nat) |
586 | { | |
587 | mutex_lock(&nf_ct_nat_helpers_mutex); | |
588 | list_add_rcu(&nat->list, &nf_ct_nat_helpers); | |
589 | mutex_unlock(&nf_ct_nat_helpers_mutex); | |
590 | } | |
591 | EXPORT_SYMBOL_GPL(nf_nat_helper_register); | |
592 | ||
593 | void nf_nat_helper_unregister(struct nf_conntrack_nat_helper *nat) | |
594 | { | |
595 | mutex_lock(&nf_ct_nat_helpers_mutex); | |
596 | list_del_rcu(&nat->list); | |
597 | mutex_unlock(&nf_ct_nat_helpers_mutex); | |
598 | } | |
599 | EXPORT_SYMBOL_GPL(nf_nat_helper_unregister); | |
600 | ||
5e615b22 G |
601 | int nf_conntrack_helper_init(void) |
602 | { | |
5e615b22 G |
603 | nf_ct_helper_hsize = 1; /* gets rounded up to use one page */ |
604 | nf_ct_helper_hash = | |
605 | nf_ct_alloc_hashtable(&nf_ct_helper_hsize, 0); | |
606 | if (!nf_ct_helper_hash) | |
607 | return -ENOMEM; | |
608 | ||
08010a21 | 609 | INIT_LIST_HEAD(&nf_ct_nat_helpers); |
b8a7fe6c | 610 | return 0; |
ceceae1b YK |
611 | } |
612 | ||
5e615b22 | 613 | void nf_conntrack_helper_fini(void) |
ceceae1b | 614 | { |
285189c7 | 615 | kvfree(nf_ct_helper_hash); |
ceceae1b | 616 | } |