blob: a5a167f941e0e5b3b44016157a2f817d475e3e7d [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/* iptables module to match on related connections */
3/*
4 * (C) 2001 Martin Josefsson <gandalf@wlug.westbo.se>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
Jan Engelhardt8bee4ba2010-03-17 16:04:40 +01006#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/module.h>
8#include <linux/skbuff.h>
9#include <linux/netfilter.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080010#include <net/netfilter/nf_conntrack.h>
11#include <net/netfilter/nf_conntrack_core.h>
12#include <net/netfilter/nf_conntrack_helper.h>
Harald Welte2e4e6a12006-01-12 13:30:04 -080013#include <linux/netfilter/x_tables.h>
14#include <linux/netfilter/xt_helper.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16MODULE_LICENSE("GPL");
17MODULE_AUTHOR("Martin Josefsson <gandalf@netfilter.org>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080018MODULE_DESCRIPTION("Xtables: Related connection matching");
Harald Welte2e4e6a12006-01-12 13:30:04 -080019MODULE_ALIAS("ipt_helper");
20MODULE_ALIAS("ip6t_helper");
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070023static bool
Jan Engelhardt62fc8052009-07-07 20:42:08 +020024helper_mt(const struct sk_buff *skb, struct xt_action_param *par)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080025{
Jan Engelhardtf7108a202008-10-08 11:35:18 +020026 const struct xt_helper_info *info = par->matchinfo;
Jan Engelhardta47362a2007-07-07 22:16:55 -070027 const struct nf_conn *ct;
28 const struct nf_conn_help *master_help;
Patrick McHardy342b7e32007-07-07 22:39:16 -070029 const struct nf_conntrack_helper *helper;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080030 enum ip_conntrack_info ctinfo;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070031 bool ret = info->invert;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080032
Jan Engelhardta47362a2007-07-07 22:16:55 -070033 ct = nf_ct_get(skb, &ctinfo);
Patrick McHardy342b7e32007-07-07 22:39:16 -070034 if (!ct || !ct->master)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080035 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080036
Harald Weltedc808fe2006-03-20 17:56:32 -080037 master_help = nfct_help(ct->master);
Patrick McHardy342b7e32007-07-07 22:39:16 -070038 if (!master_help)
39 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080040
Aaron Conolee2361cb2016-09-21 11:35:04 -040041 /* rcu_read_lock()ed by nf_hook_thresh */
Patrick McHardy342b7e32007-07-07 22:39:16 -070042 helper = rcu_dereference(master_help->helper);
43 if (!helper)
44 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080045
46 if (info->name[0] == '\0')
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070047 ret = !ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080048 else
Jan Engelhardt0ff4d772008-01-10 22:41:28 -080049 ret ^= !strncmp(helper->name, info->name,
50 strlen(helper->name));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080051 return ret;
52}
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080053
Jan Engelhardtb0f38452010-03-19 17:16:42 +010054static int helper_mt_check(const struct xt_mtchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +020056 struct xt_helper_info *info = par->matchinfo;
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +010057 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Florian Westphalecb24212016-11-15 21:36:40 +010059 ret = nf_ct_netns_get(par->net, par->family);
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +010060 if (ret < 0) {
Florian Westphalb2606642018-02-09 15:52:07 +010061 pr_info_ratelimited("cannot load conntrack support for proto=%u\n",
62 par->family);
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +010063 return ret;
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -080064 }
Gao Fengb9d80f82016-09-20 10:31:04 +080065 info->name[sizeof(info->name) - 1] = '\0';
Jan Engelhardtbd414ee2010-03-23 16:35:56 +010066 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067}
68
Jan Engelhardt6be3d852008-10-08 11:35:19 +020069static void helper_mt_destroy(const struct xt_mtdtor_param *par)
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -080070{
Florian Westphalecb24212016-11-15 21:36:40 +010071 nf_ct_netns_put(par->net, par->family);
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -080072}
73
Jan Engelhardt92f3b2b2008-10-08 11:35:20 +020074static struct xt_match helper_mt_reg __read_mostly = {
75 .name = "helper",
76 .revision = 0,
77 .family = NFPROTO_UNSPEC,
78 .checkentry = helper_mt_check,
79 .match = helper_mt,
80 .destroy = helper_mt_destroy,
81 .matchsize = sizeof(struct xt_helper_info),
82 .me = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -070083};
84
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080085static int __init helper_mt_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Jan Engelhardt92f3b2b2008-10-08 11:35:20 +020087 return xt_register_match(&helper_mt_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080090static void __exit helper_mt_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Jan Engelhardt92f3b2b2008-10-08 11:35:20 +020092 xt_unregister_match(&helper_mt_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080095module_init(helper_mt_init);
96module_exit(helper_mt_exit);