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