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