Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1da177e4 LT |
2 | /* iptables module to match on related connections */ |
3 | /* | |
4 | * (C) 2001 Martin Josefsson <gandalf@wlug.westbo.se> | |
1da177e4 | 5 | */ |
8bee4bad | 6 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
1da177e4 LT |
7 | #include <linux/module.h> |
8 | #include <linux/skbuff.h> | |
9 | #include <linux/netfilter.h> | |
9fb9cbb1 YK |
10 | #include <net/netfilter/nf_conntrack.h> |
11 | #include <net/netfilter/nf_conntrack_core.h> | |
12 | #include <net/netfilter/nf_conntrack_helper.h> | |
2e4e6a17 HW |
13 | #include <linux/netfilter/x_tables.h> |
14 | #include <linux/netfilter/xt_helper.h> | |
1da177e4 LT |
15 | |
16 | MODULE_LICENSE("GPL"); | |
17 | MODULE_AUTHOR("Martin Josefsson <gandalf@netfilter.org>"); | |
2ae15b64 | 18 | MODULE_DESCRIPTION("Xtables: Related connection matching"); |
2e4e6a17 HW |
19 | MODULE_ALIAS("ipt_helper"); |
20 | MODULE_ALIAS("ip6t_helper"); | |
1da177e4 | 21 | |
1da177e4 | 22 | |
1d93a9cb | 23 | static bool |
62fc8051 | 24 | helper_mt(const struct sk_buff *skb, struct xt_action_param *par) |
9fb9cbb1 | 25 | { |
f7108a20 | 26 | const struct xt_helper_info *info = par->matchinfo; |
a47362a2 JE |
27 | const struct nf_conn *ct; |
28 | const struct nf_conn_help *master_help; | |
342b7e3c | 29 | const struct nf_conntrack_helper *helper; |
9fb9cbb1 | 30 | enum ip_conntrack_info ctinfo; |
1d93a9cb | 31 | bool ret = info->invert; |
601e68e1 | 32 | |
a47362a2 | 33 | ct = nf_ct_get(skb, &ctinfo); |
342b7e3c | 34 | if (!ct || !ct->master) |
9fb9cbb1 | 35 | return ret; |
9fb9cbb1 | 36 | |
dc808fe2 | 37 | master_help = nfct_help(ct->master); |
342b7e3c PM |
38 | if (!master_help) |
39 | return ret; | |
9fb9cbb1 | 40 | |
e2361cb9 | 41 | /* rcu_read_lock()ed by nf_hook_thresh */ |
342b7e3c PM |
42 | helper = rcu_dereference(master_help->helper); |
43 | if (!helper) | |
44 | return ret; | |
9fb9cbb1 YK |
45 | |
46 | if (info->name[0] == '\0') | |
1d93a9cb | 47 | ret = !ret; |
9fb9cbb1 | 48 | else |
0ff4d77b JE |
49 | ret ^= !strncmp(helper->name, info->name, |
50 | strlen(helper->name)); | |
9fb9cbb1 YK |
51 | return ret; |
52 | } | |
9fb9cbb1 | 53 | |
b0f38452 | 54 | static int helper_mt_check(const struct xt_mtchk_param *par) |
1da177e4 | 55 | { |
9b4fce7a | 56 | struct xt_helper_info *info = par->matchinfo; |
4a5a5c73 | 57 | int ret; |
1da177e4 | 58 | |
ecb2421b | 59 | ret = nf_ct_netns_get(par->net, par->family); |
4a5a5c73 | 60 | if (ret < 0) { |
b2606644 FW |
61 | pr_info_ratelimited("cannot load conntrack support for proto=%u\n", |
62 | par->family); | |
4a5a5c73 | 63 | return ret; |
b9f78f9f | 64 | } |
b9d80f83 | 65 | info->name[sizeof(info->name) - 1] = '\0'; |
bd414ee6 | 66 | return 0; |
1da177e4 LT |
67 | } |
68 | ||
6be3d859 | 69 | static void helper_mt_destroy(const struct xt_mtdtor_param *par) |
b9f78f9f | 70 | { |
ecb2421b | 71 | nf_ct_netns_put(par->net, par->family); |
b9f78f9f PNA |
72 | } |
73 | ||
92f3b2b1 JE |
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, | |
1da177e4 LT |
83 | }; |
84 | ||
d3c5ee6d | 85 | static int __init helper_mt_init(void) |
1da177e4 | 86 | { |
92f3b2b1 | 87 | return xt_register_match(&helper_mt_reg); |
1da177e4 LT |
88 | } |
89 | ||
d3c5ee6d | 90 | static void __exit helper_mt_exit(void) |
1da177e4 | 91 | { |
92f3b2b1 | 92 | xt_unregister_match(&helper_mt_reg); |
1da177e4 LT |
93 | } |
94 | ||
d3c5ee6d JE |
95 | module_init(helper_mt_init); |
96 | module_exit(helper_mt_exit); |