1 // SPDX-License-Identifier: GPL-2.0-only
2 /* iptables module to match on related connections */
4 * (C) 2001 Martin Josefsson <gandalf@wlug.westbo.se>
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 #include <linux/module.h>
8 #include <linux/skbuff.h>
9 #include <linux/netfilter.h>
10 #include <net/netfilter/nf_conntrack.h>
11 #include <net/netfilter/nf_conntrack_core.h>
12 #include <net/netfilter/nf_conntrack_helper.h>
13 #include <linux/netfilter/x_tables.h>
14 #include <linux/netfilter/xt_helper.h>
16 MODULE_LICENSE("GPL");
17 MODULE_AUTHOR("Martin Josefsson <gandalf@netfilter.org>");
18 MODULE_DESCRIPTION("Xtables: Related connection matching");
19 MODULE_ALIAS("ipt_helper");
20 MODULE_ALIAS("ip6t_helper");
24 helper_mt(const struct sk_buff
*skb
, struct xt_action_param
*par
)
26 const struct xt_helper_info
*info
= par
->matchinfo
;
27 const struct nf_conn
*ct
;
28 const struct nf_conn_help
*master_help
;
29 const struct nf_conntrack_helper
*helper
;
30 enum ip_conntrack_info ctinfo
;
31 bool ret
= info
->invert
;
33 ct
= nf_ct_get(skb
, &ctinfo
);
34 if (!ct
|| !ct
->master
)
37 master_help
= nfct_help(ct
->master
);
41 /* rcu_read_lock()ed by nf_hook_thresh */
42 helper
= rcu_dereference(master_help
->helper
);
46 if (info
->name
[0] == '\0')
49 ret
^= !strncmp(helper
->name
, info
->name
,
50 strlen(helper
->name
));
54 static int helper_mt_check(const struct xt_mtchk_param
*par
)
56 struct xt_helper_info
*info
= par
->matchinfo
;
59 ret
= nf_ct_netns_get(par
->net
, par
->family
);
61 pr_info_ratelimited("cannot load conntrack support for proto=%u\n",
65 info
->name
[sizeof(info
->name
) - 1] = '\0';
69 static void helper_mt_destroy(const struct xt_mtdtor_param
*par
)
71 nf_ct_netns_put(par
->net
, par
->family
);
74 static struct xt_match helper_mt_reg __read_mostly
= {
77 .family
= NFPROTO_UNSPEC
,
78 .checkentry
= helper_mt_check
,
80 .destroy
= helper_mt_destroy
,
81 .matchsize
= sizeof(struct xt_helper_info
),
85 static int __init
helper_mt_init(void)
87 return xt_register_match(&helper_mt_reg
);
90 static void __exit
helper_mt_exit(void)
92 xt_unregister_match(&helper_mt_reg
);
95 module_init(helper_mt_init
);
96 module_exit(helper_mt_exit
);