Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* iptables module to match on related connections */ |
| 3 | /* |
| 4 | * (C) 2001 Martin Josefsson <gandalf@wlug.westbo.se> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | */ |
Jan Engelhardt | 8bee4ba | 2010-03-17 16:04:40 +0100 | [diff] [blame] | 6 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include <linux/module.h> |
| 8 | #include <linux/skbuff.h> |
| 9 | #include <linux/netfilter.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 10 | #include <net/netfilter/nf_conntrack.h> |
| 11 | #include <net/netfilter/nf_conntrack_core.h> |
| 12 | #include <net/netfilter/nf_conntrack_helper.h> |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 13 | #include <linux/netfilter/x_tables.h> |
| 14 | #include <linux/netfilter/xt_helper.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
| 16 | MODULE_LICENSE("GPL"); |
| 17 | MODULE_AUTHOR("Martin Josefsson <gandalf@netfilter.org>"); |
Jan Engelhardt | 2ae15b6 | 2008-01-14 23:42:28 -0800 | [diff] [blame] | 18 | MODULE_DESCRIPTION("Xtables: Related connection matching"); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 19 | MODULE_ALIAS("ipt_helper"); |
| 20 | MODULE_ALIAS("ip6t_helper"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 23 | static bool |
Jan Engelhardt | 62fc805 | 2009-07-07 20:42:08 +0200 | [diff] [blame] | 24 | helper_mt(const struct sk_buff *skb, struct xt_action_param *par) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 25 | { |
Jan Engelhardt | f7108a20 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 26 | const struct xt_helper_info *info = par->matchinfo; |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 27 | const struct nf_conn *ct; |
| 28 | const struct nf_conn_help *master_help; |
Patrick McHardy | 342b7e3 | 2007-07-07 22:39:16 -0700 | [diff] [blame] | 29 | const struct nf_conntrack_helper *helper; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 30 | enum ip_conntrack_info ctinfo; |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 31 | bool ret = info->invert; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 32 | |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 33 | ct = nf_ct_get(skb, &ctinfo); |
Patrick McHardy | 342b7e3 | 2007-07-07 22:39:16 -0700 | [diff] [blame] | 34 | if (!ct || !ct->master) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 35 | return ret; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 36 | |
Harald Welte | dc808fe | 2006-03-20 17:56:32 -0800 | [diff] [blame] | 37 | master_help = nfct_help(ct->master); |
Patrick McHardy | 342b7e3 | 2007-07-07 22:39:16 -0700 | [diff] [blame] | 38 | if (!master_help) |
| 39 | return ret; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 40 | |
Aaron Conole | e2361cb | 2016-09-21 11:35:04 -0400 | [diff] [blame] | 41 | /* rcu_read_lock()ed by nf_hook_thresh */ |
Patrick McHardy | 342b7e3 | 2007-07-07 22:39:16 -0700 | [diff] [blame] | 42 | helper = rcu_dereference(master_help->helper); |
| 43 | if (!helper) |
| 44 | return ret; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 45 | |
| 46 | if (info->name[0] == '\0') |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 47 | ret = !ret; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 48 | else |
Jan Engelhardt | 0ff4d77 | 2008-01-10 22:41:28 -0800 | [diff] [blame] | 49 | ret ^= !strncmp(helper->name, info->name, |
| 50 | strlen(helper->name)); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 51 | return ret; |
| 52 | } |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 53 | |
Jan Engelhardt | b0f3845 | 2010-03-19 17:16:42 +0100 | [diff] [blame] | 54 | static int helper_mt_check(const struct xt_mtchk_param *par) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | { |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 56 | struct xt_helper_info *info = par->matchinfo; |
Jan Engelhardt | 4a5a5c7 | 2010-03-19 17:32:59 +0100 | [diff] [blame] | 57 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Florian Westphal | ecb2421 | 2016-11-15 21:36:40 +0100 | [diff] [blame] | 59 | ret = nf_ct_netns_get(par->net, par->family); |
Jan Engelhardt | 4a5a5c7 | 2010-03-19 17:32:59 +0100 | [diff] [blame] | 60 | if (ret < 0) { |
Florian Westphal | b260664 | 2018-02-09 15:52:07 +0100 | [diff] [blame] | 61 | pr_info_ratelimited("cannot load conntrack support for proto=%u\n", |
| 62 | par->family); |
Jan Engelhardt | 4a5a5c7 | 2010-03-19 17:32:59 +0100 | [diff] [blame] | 63 | return ret; |
Pablo Neira Ayuso | b9f78f9 | 2006-03-22 13:56:08 -0800 | [diff] [blame] | 64 | } |
Gao Feng | b9d80f8 | 2016-09-20 10:31:04 +0800 | [diff] [blame] | 65 | info->name[sizeof(info->name) - 1] = '\0'; |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 66 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Jan Engelhardt | 6be3d85 | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 69 | static void helper_mt_destroy(const struct xt_mtdtor_param *par) |
Pablo Neira Ayuso | b9f78f9 | 2006-03-22 13:56:08 -0800 | [diff] [blame] | 70 | { |
Florian Westphal | ecb2421 | 2016-11-15 21:36:40 +0100 | [diff] [blame] | 71 | nf_ct_netns_put(par->net, par->family); |
Pablo Neira Ayuso | b9f78f9 | 2006-03-22 13:56:08 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Jan Engelhardt | 92f3b2b | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 74 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | }; |
| 84 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 85 | static int __init helper_mt_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | { |
Jan Engelhardt | 92f3b2b | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 87 | return xt_register_match(&helper_mt_reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 90 | static void __exit helper_mt_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | { |
Jan Engelhardt | 92f3b2b | 2008-10-08 11:35:20 +0200 | [diff] [blame] | 92 | xt_unregister_match(&helper_mt_reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 95 | module_init(helper_mt_init); |
| 96 | module_exit(helper_mt_exit); |