Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* iptables module to match on related connections */ |
2 | /* | |
3 | * (C) 2001 Martin Josefsson <gandalf@wlug.westbo.se> | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License version 2 as | |
7 | * published by the Free Software Foundation. | |
1da177e4 LT |
8 | */ |
9 | ||
10 | #include <linux/module.h> | |
11 | #include <linux/skbuff.h> | |
12 | #include <linux/netfilter.h> | |
9fb9cbb1 YK |
13 | #include <net/netfilter/nf_conntrack.h> |
14 | #include <net/netfilter/nf_conntrack_core.h> | |
15 | #include <net/netfilter/nf_conntrack_helper.h> | |
2e4e6a17 HW |
16 | #include <linux/netfilter/x_tables.h> |
17 | #include <linux/netfilter/xt_helper.h> | |
1da177e4 LT |
18 | |
19 | MODULE_LICENSE("GPL"); | |
20 | MODULE_AUTHOR("Martin Josefsson <gandalf@netfilter.org>"); | |
21 | MODULE_DESCRIPTION("iptables helper match module"); | |
2e4e6a17 HW |
22 | MODULE_ALIAS("ipt_helper"); |
23 | MODULE_ALIAS("ip6t_helper"); | |
1da177e4 | 24 | |
1da177e4 | 25 | |
1d93a9cb | 26 | static bool |
d3c5ee6d JE |
27 | helper_mt(const struct sk_buff *skb, const struct net_device *in, |
28 | const struct net_device *out, const struct xt_match *match, | |
29 | const void *matchinfo, int offset, unsigned int protoff, | |
30 | bool *hotdrop) | |
9fb9cbb1 | 31 | { |
2e4e6a17 | 32 | const struct xt_helper_info *info = matchinfo; |
a47362a2 JE |
33 | const struct nf_conn *ct; |
34 | const struct nf_conn_help *master_help; | |
342b7e3c | 35 | const struct nf_conntrack_helper *helper; |
9fb9cbb1 | 36 | enum ip_conntrack_info ctinfo; |
1d93a9cb | 37 | bool ret = info->invert; |
601e68e1 | 38 | |
a47362a2 | 39 | ct = nf_ct_get(skb, &ctinfo); |
342b7e3c | 40 | if (!ct || !ct->master) |
9fb9cbb1 | 41 | return ret; |
9fb9cbb1 | 42 | |
dc808fe2 | 43 | master_help = nfct_help(ct->master); |
342b7e3c PM |
44 | if (!master_help) |
45 | return ret; | |
9fb9cbb1 | 46 | |
342b7e3c PM |
47 | /* rcu_read_lock()ed by nf_hook_slow */ |
48 | helper = rcu_dereference(master_help->helper); | |
49 | if (!helper) | |
50 | return ret; | |
9fb9cbb1 YK |
51 | |
52 | if (info->name[0] == '\0') | |
1d93a9cb | 53 | ret = !ret; |
9fb9cbb1 | 54 | else |
0ff4d77b JE |
55 | ret ^= !strncmp(helper->name, info->name, |
56 | strlen(helper->name)); | |
9fb9cbb1 YK |
57 | return ret; |
58 | } | |
9fb9cbb1 | 59 | |
d3c5ee6d JE |
60 | static bool |
61 | helper_mt_check(const char *tablename, const void *inf, | |
62 | const struct xt_match *match, void *matchinfo, | |
63 | unsigned int hook_mask) | |
1da177e4 | 64 | { |
2e4e6a17 | 65 | struct xt_helper_info *info = matchinfo; |
1da177e4 | 66 | |
b9f78f9f | 67 | if (nf_ct_l3proto_try_module_get(match->family) < 0) { |
fe0b9294 | 68 | printk(KERN_WARNING "can't load conntrack support for " |
df54aae0 | 69 | "proto=%u\n", match->family); |
ccb79bdc | 70 | return false; |
b9f78f9f | 71 | } |
1da177e4 | 72 | info->name[29] = '\0'; |
ccb79bdc | 73 | return true; |
1da177e4 LT |
74 | } |
75 | ||
d3c5ee6d | 76 | static void helper_mt_destroy(const struct xt_match *match, void *matchinfo) |
b9f78f9f | 77 | { |
b9f78f9f | 78 | nf_ct_l3proto_module_put(match->family); |
b9f78f9f PNA |
79 | } |
80 | ||
d3c5ee6d | 81 | static struct xt_match helper_mt_reg[] __read_mostly = { |
4470bbc7 PM |
82 | { |
83 | .name = "helper", | |
84 | .family = AF_INET, | |
d3c5ee6d JE |
85 | .checkentry = helper_mt_check, |
86 | .match = helper_mt, | |
87 | .destroy = helper_mt_destroy, | |
4470bbc7 PM |
88 | .matchsize = sizeof(struct xt_helper_info), |
89 | .me = THIS_MODULE, | |
90 | }, | |
91 | { | |
92 | .name = "helper", | |
93 | .family = AF_INET6, | |
d3c5ee6d JE |
94 | .checkentry = helper_mt_check, |
95 | .match = helper_mt, | |
96 | .destroy = helper_mt_destroy, | |
4470bbc7 PM |
97 | .matchsize = sizeof(struct xt_helper_info), |
98 | .me = THIS_MODULE, | |
99 | }, | |
1da177e4 LT |
100 | }; |
101 | ||
d3c5ee6d | 102 | static int __init helper_mt_init(void) |
1da177e4 | 103 | { |
d3c5ee6d | 104 | return xt_register_matches(helper_mt_reg, ARRAY_SIZE(helper_mt_reg)); |
1da177e4 LT |
105 | } |
106 | ||
d3c5ee6d | 107 | static void __exit helper_mt_exit(void) |
1da177e4 | 108 | { |
d3c5ee6d | 109 | xt_unregister_matches(helper_mt_reg, ARRAY_SIZE(helper_mt_reg)); |
1da177e4 LT |
110 | } |
111 | ||
d3c5ee6d JE |
112 | module_init(helper_mt_init); |
113 | module_exit(helper_mt_exit); |