1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Helper handling for netfilter. */
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>
7 * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
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>
16 #include <linux/random.h>
17 #include <linux/err.h>
18 #include <linux/kernel.h>
19 #include <linux/netdevice.h>
20 #include <linux/rculist.h>
21 #include <linux/rtnetlink.h>
23 #include <net/netfilter/nf_conntrack.h>
24 #include <net/netfilter/nf_conntrack_core.h>
25 #include <net/netfilter/nf_conntrack_ecache.h>
26 #include <net/netfilter/nf_conntrack_extend.h>
27 #include <net/netfilter/nf_conntrack_helper.h>
28 #include <net/netfilter/nf_conntrack_l4proto.h>
29 #include <net/netfilter/nf_log.h>
31 static DEFINE_MUTEX(nf_ct_helper_mutex);
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);
36 static unsigned int nf_ct_helper_count __read_mostly;
38 static bool nf_ct_auto_assign_helper __read_mostly = false;
39 module_param_named(nf_conntrack_helper, nf_ct_auto_assign_helper, bool, 0644);
40 MODULE_PARM_DESC(nf_conntrack_helper,
41 "Enable automatic conntrack helper assignment (default 0)");
43 static DEFINE_MUTEX(nf_ct_nat_helpers_mutex);
44 static struct list_head nf_ct_nat_helpers __read_mostly;
46 extern unsigned int nf_conntrack_net_id;
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)
52 return (((tuple->src.l3num << 8) | tuple->dst.protonum) ^
53 (__force __u16)tuple->src.u.all) % nf_ct_helper_hsize;
56 static struct nf_conntrack_helper *
57 __nf_ct_helper_find(const struct nf_conntrack_tuple *tuple)
59 struct nf_conntrack_helper *helper;
60 struct nf_conntrack_tuple_mask mask = { .src.u.all = htons(0xFFFF) };
63 if (!nf_ct_helper_count)
66 h = helper_hash(tuple);
67 hlist_for_each_entry_rcu(helper, &nf_ct_helper_hash[h], hnode) {
68 if (nf_ct_tuple_src_mask_cmp(tuple, &helper->tuple, &mask))
74 struct nf_conntrack_helper *
75 __nf_conntrack_helper_find(const char *name, u16 l3num, u8 protonum)
77 struct nf_conntrack_helper *h;
80 for (i = 0; i < nf_ct_helper_hsize; i++) {
81 hlist_for_each_entry_rcu(h, &nf_ct_helper_hash[i], hnode) {
82 if (strcmp(h->name, name))
85 if (h->tuple.src.l3num != NFPROTO_UNSPEC &&
86 h->tuple.src.l3num != l3num)
89 if (h->tuple.dst.protonum == protonum)
95 EXPORT_SYMBOL_GPL(__nf_conntrack_helper_find);
97 struct nf_conntrack_helper *
98 nf_conntrack_helper_try_module_get(const char *name, u16 l3num, u8 protonum)
100 struct nf_conntrack_helper *h;
104 h = __nf_conntrack_helper_find(name, l3num, protonum);
105 #ifdef CONFIG_MODULES
108 if (request_module("nfct-helper-%s", name) == 0) {
110 h = __nf_conntrack_helper_find(name, l3num, protonum);
116 if (h != NULL && !try_module_get(h->me))
118 if (h != NULL && !refcount_inc_not_zero(&h->refcnt)) {
127 EXPORT_SYMBOL_GPL(nf_conntrack_helper_try_module_get);
129 void nf_conntrack_helper_put(struct nf_conntrack_helper *helper)
131 refcount_dec(&helper->refcnt);
132 module_put(helper->me);
134 EXPORT_SYMBOL_GPL(nf_conntrack_helper_put);
136 static struct nf_conntrack_nat_helper *
137 nf_conntrack_nat_helper_find(const char *mod_name)
139 struct nf_conntrack_nat_helper *cur;
142 list_for_each_entry_rcu(cur, &nf_ct_nat_helpers, list) {
143 if (!strcmp(cur->mod_name, mod_name)) {
148 return found ? cur : NULL;
152 nf_nat_helper_try_module_get(const char *name, u16 l3num, u8 protonum)
154 struct nf_conntrack_helper *h;
155 struct nf_conntrack_nat_helper *nat;
156 char mod_name[NF_CT_HELPER_NAME_LEN];
160 h = __nf_conntrack_helper_find(name, l3num, protonum);
166 nat = nf_conntrack_nat_helper_find(h->nat_mod_name);
168 snprintf(mod_name, sizeof(mod_name), "%s", h->nat_mod_name);
170 request_module(mod_name);
173 nat = nf_conntrack_nat_helper_find(mod_name);
180 if (!try_module_get(nat->module))
186 EXPORT_SYMBOL_GPL(nf_nat_helper_try_module_get);
188 void nf_nat_helper_put(struct nf_conntrack_helper *helper)
190 struct nf_conntrack_nat_helper *nat;
192 nat = nf_conntrack_nat_helper_find(helper->nat_mod_name);
193 if (WARN_ON_ONCE(!nat))
196 module_put(nat->module);
198 EXPORT_SYMBOL_GPL(nf_nat_helper_put);
200 struct nf_conn_help *
201 nf_ct_helper_ext_add(struct nf_conn *ct, gfp_t gfp)
203 struct nf_conn_help *help;
205 help = nf_ct_ext_add(ct, NF_CT_EXT_HELPER, gfp);
207 INIT_HLIST_HEAD(&help->expectations);
209 pr_debug("failed to add helper extension area");
212 EXPORT_SYMBOL_GPL(nf_ct_helper_ext_add);
214 static struct nf_conntrack_helper *
215 nf_ct_lookup_helper(struct nf_conn *ct, struct net *net)
217 struct nf_conntrack_net *cnet = net_generic(net, nf_conntrack_net_id);
219 if (!cnet->sysctl_auto_assign_helper) {
220 if (cnet->auto_assign_helper_warned)
222 if (!__nf_ct_helper_find(&ct->tuplehash[IP_CT_DIR_REPLY].tuple))
224 pr_info("nf_conntrack: default automatic helper assignment "
225 "has been turned off for security reasons and CT-based "
226 "firewall rule not found. Use the iptables CT target "
227 "to attach helpers instead.\n");
228 cnet->auto_assign_helper_warned = true;
232 return __nf_ct_helper_find(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
235 int __nf_ct_try_assign_helper(struct nf_conn *ct, struct nf_conn *tmpl,
238 struct nf_conntrack_helper *helper = NULL;
239 struct nf_conn_help *help;
240 struct net *net = nf_ct_net(ct);
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.
248 if (test_bit(IPS_HELPER_BIT, &ct->status))
252 help = nfct_help(tmpl);
254 helper = help->helper;
255 set_bit(IPS_HELPER_BIT, &ct->status);
259 help = nfct_help(ct);
261 if (helper == NULL) {
262 helper = nf_ct_lookup_helper(ct, net);
263 if (helper == NULL) {
265 RCU_INIT_POINTER(help->helper, NULL);
271 help = nf_ct_helper_ext_add(ct, flags);
275 /* We only allow helper re-assignment of the same sort since
276 * we cannot reallocate the helper extension area.
278 struct nf_conntrack_helper *tmp = rcu_dereference(help->helper);
280 if (tmp && tmp->help != helper->help) {
281 RCU_INIT_POINTER(help->helper, NULL);
286 rcu_assign_pointer(help->helper, helper);
290 EXPORT_SYMBOL_GPL(__nf_ct_try_assign_helper);
292 /* appropriate ct lock protecting must be taken by caller */
293 static int unhelp(struct nf_conn *ct, void *me)
295 struct nf_conn_help *help = nfct_help(ct);
297 if (help && rcu_dereference_raw(help->helper) == me) {
298 nf_conntrack_event(IPCT_HELPER, ct);
299 RCU_INIT_POINTER(help->helper, NULL);
302 /* We are not intended to delete this conntrack. */
306 void nf_ct_helper_destroy(struct nf_conn *ct)
308 struct nf_conn_help *help = nfct_help(ct);
309 struct nf_conntrack_helper *helper;
313 helper = rcu_dereference(help->helper);
314 if (helper && helper->destroy)
320 static LIST_HEAD(nf_ct_helper_expectfn_list);
322 void nf_ct_helper_expectfn_register(struct nf_ct_helper_expectfn *n)
324 spin_lock_bh(&nf_conntrack_expect_lock);
325 list_add_rcu(&n->head, &nf_ct_helper_expectfn_list);
326 spin_unlock_bh(&nf_conntrack_expect_lock);
328 EXPORT_SYMBOL_GPL(nf_ct_helper_expectfn_register);
330 void nf_ct_helper_expectfn_unregister(struct nf_ct_helper_expectfn *n)
332 spin_lock_bh(&nf_conntrack_expect_lock);
333 list_del_rcu(&n->head);
334 spin_unlock_bh(&nf_conntrack_expect_lock);
336 EXPORT_SYMBOL_GPL(nf_ct_helper_expectfn_unregister);
338 /* Caller should hold the rcu lock */
339 struct nf_ct_helper_expectfn *
340 nf_ct_helper_expectfn_find_by_name(const char *name)
342 struct nf_ct_helper_expectfn *cur;
345 list_for_each_entry_rcu(cur, &nf_ct_helper_expectfn_list, head) {
346 if (!strcmp(cur->name, name)) {
351 return found ? cur : NULL;
353 EXPORT_SYMBOL_GPL(nf_ct_helper_expectfn_find_by_name);
355 /* Caller should hold the rcu lock */
356 struct nf_ct_helper_expectfn *
357 nf_ct_helper_expectfn_find_by_symbol(const void *symbol)
359 struct nf_ct_helper_expectfn *cur;
362 list_for_each_entry_rcu(cur, &nf_ct_helper_expectfn_list, head) {
363 if (cur->expectfn == symbol) {
368 return found ? cur : NULL;
370 EXPORT_SYMBOL_GPL(nf_ct_helper_expectfn_find_by_symbol);
373 void nf_ct_helper_log(struct sk_buff *skb, const struct nf_conn *ct,
374 const char *fmt, ...)
376 const struct nf_conn_help *help;
377 const struct nf_conntrack_helper *helper;
378 struct va_format vaf;
386 /* Called from the helper function, this call never fails */
387 help = nfct_help(ct);
389 /* rcu_read_lock()ed by nf_hook_thresh */
390 helper = rcu_dereference(help->helper);
392 nf_log_packet(nf_ct_net(ct), nf_ct_l3num(ct), 0, skb, NULL, NULL, NULL,
393 "nf_ct_%s: dropping packet: %pV ", helper->name, &vaf);
397 EXPORT_SYMBOL_GPL(nf_ct_helper_log);
399 int nf_conntrack_helper_register(struct nf_conntrack_helper *me)
401 struct nf_conntrack_tuple_mask mask = { .src.u.all = htons(0xFFFF) };
402 unsigned int h = helper_hash(&me->tuple);
403 struct nf_conntrack_helper *cur;
406 BUG_ON(me->expect_policy == NULL);
407 BUG_ON(me->expect_class_max >= NF_CT_MAX_EXPECT_CLASSES);
408 BUG_ON(strlen(me->name) > NF_CT_HELPER_NAME_LEN - 1);
410 if (me->expect_policy->max_expected > NF_CT_EXPECT_MAX_CNT)
413 mutex_lock(&nf_ct_helper_mutex);
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) {
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,
436 refcount_set(&me->refcnt, 1);
437 hlist_add_head_rcu(&me->hnode, &nf_ct_helper_hash[h]);
438 nf_ct_helper_count++;
440 mutex_unlock(&nf_ct_helper_mutex);
443 EXPORT_SYMBOL_GPL(nf_conntrack_helper_register);
445 static bool expect_iter_me(struct nf_conntrack_expect *exp, void *data)
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;
451 if (exp->helper == me)
454 this = rcu_dereference_protected(help->helper,
455 lockdep_is_held(&nf_conntrack_expect_lock));
459 void nf_conntrack_helper_unregister(struct nf_conntrack_helper *me)
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);
466 /* Make sure every nothing is still using the helper unless its a
467 * connection in the hash.
471 nf_ct_expect_iterate_destroy(expect_iter_me, NULL);
472 nf_ct_iterate_destroy(unhelp, me);
474 /* Maybe someone has gotten the helper already when unhelp above.
475 * So need to wait it.
479 EXPORT_SYMBOL_GPL(nf_conntrack_helper_unregister);
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,
485 u32 expect_class_max,
486 int (*help)(struct sk_buff *skb, unsigned int protoff,
488 enum ip_conntrack_info ctinfo),
489 int (*from_nlattr)(struct nlattr *attr,
491 struct module *module)
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;
499 helper->from_nlattr = from_nlattr;
501 snprintf(helper->nat_mod_name, sizeof(helper->nat_mod_name),
502 NF_NAT_HELPER_PREFIX "%s", name);
504 if (spec_port == default_port)
505 snprintf(helper->name, sizeof(helper->name), "%s", name);
507 snprintf(helper->name, sizeof(helper->name), "%s-%u", name, id);
509 EXPORT_SYMBOL_GPL(nf_ct_helper_init);
511 int nf_conntrack_helpers_register(struct nf_conntrack_helper *helper,
517 for (i = 0; i < n; i++) {
518 err = nf_conntrack_helper_register(&helper[i]);
526 nf_conntrack_helpers_unregister(helper, i);
529 EXPORT_SYMBOL_GPL(nf_conntrack_helpers_register);
531 void nf_conntrack_helpers_unregister(struct nf_conntrack_helper *helper,
535 nf_conntrack_helper_unregister(&helper[n]);
537 EXPORT_SYMBOL_GPL(nf_conntrack_helpers_unregister);
539 void nf_nat_helper_register(struct nf_conntrack_nat_helper *nat)
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);
545 EXPORT_SYMBOL_GPL(nf_nat_helper_register);
547 void nf_nat_helper_unregister(struct nf_conntrack_nat_helper *nat)
549 mutex_lock(&nf_ct_nat_helpers_mutex);
550 list_del_rcu(&nat->list);
551 mutex_unlock(&nf_ct_nat_helpers_mutex);
553 EXPORT_SYMBOL_GPL(nf_nat_helper_unregister);
555 static const struct nf_ct_ext_type helper_extend = {
556 .len = sizeof(struct nf_conn_help),
557 .align = __alignof__(struct nf_conn_help),
558 .id = NF_CT_EXT_HELPER,
561 void nf_conntrack_helper_pernet_init(struct net *net)
563 struct nf_conntrack_net *cnet = net_generic(net, nf_conntrack_net_id);
565 cnet->sysctl_auto_assign_helper = nf_ct_auto_assign_helper;
568 int nf_conntrack_helper_init(void)
571 nf_ct_helper_hsize = 1; /* gets rounded up to use one page */
573 nf_ct_alloc_hashtable(&nf_ct_helper_hsize, 0);
574 if (!nf_ct_helper_hash)
577 ret = nf_ct_extend_register(&helper_extend);
579 pr_err("nf_ct_helper: Unable to register helper extension.\n");
583 INIT_LIST_HEAD(&nf_ct_nat_helpers);
586 kvfree(nf_ct_helper_hash);
590 void nf_conntrack_helper_fini(void)
592 nf_ct_extend_unregister(&helper_extend);
593 kvfree(nf_ct_helper_hash);