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