Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
| 8 | * |
| 9 | * 19 Mar 2002 Harald Welte <laforge@gnumonks.org>: |
| 10 | * - Port to newnat infrastructure |
| 11 | */ |
| 12 | |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/skbuff.h> |
| 15 | #include <linux/netfilter.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 16 | #if defined(CONFIG_IP_NF_CONNTRACK) || defined(CONFIG_IP_NF_CONNTRACK_MODULE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/netfilter_ipv4/ip_conntrack.h> |
| 18 | #include <linux/netfilter_ipv4/ip_conntrack_core.h> |
| 19 | #include <linux/netfilter_ipv4/ip_conntrack_helper.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 20 | #else |
| 21 | #include <net/netfilter/nf_conntrack.h> |
| 22 | #include <net/netfilter/nf_conntrack_core.h> |
| 23 | #include <net/netfilter/nf_conntrack_helper.h> |
| 24 | #endif |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 25 | #include <linux/netfilter/x_tables.h> |
| 26 | #include <linux/netfilter/xt_helper.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
| 28 | MODULE_LICENSE("GPL"); |
| 29 | MODULE_AUTHOR("Martin Josefsson <gandalf@netfilter.org>"); |
| 30 | MODULE_DESCRIPTION("iptables helper match module"); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 31 | MODULE_ALIAS("ipt_helper"); |
| 32 | MODULE_ALIAS("ip6t_helper"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | #if 0 |
| 35 | #define DEBUGP printk |
| 36 | #else |
| 37 | #define DEBUGP(format, args...) |
| 38 | #endif |
| 39 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 40 | #if defined(CONFIG_IP_NF_CONNTRACK) || defined(CONFIG_IP_NF_CONNTRACK_MODULE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | static int |
| 42 | match(const struct sk_buff *skb, |
| 43 | const struct net_device *in, |
| 44 | const struct net_device *out, |
Patrick McHardy | c498673 | 2006-03-20 18:02:56 -0800 | [diff] [blame] | 45 | const struct xt_match *match, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | const void *matchinfo, |
| 47 | int offset, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 48 | unsigned int protoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | int *hotdrop) |
| 50 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 51 | const struct xt_helper_info *info = matchinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | struct ip_conntrack *ct; |
| 53 | enum ip_conntrack_info ctinfo; |
| 54 | int ret = info->invert; |
| 55 | |
| 56 | ct = ip_conntrack_get((struct sk_buff *)skb, &ctinfo); |
| 57 | if (!ct) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 58 | DEBUGP("xt_helper: Eek! invalid conntrack?\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | return ret; |
| 60 | } |
| 61 | |
| 62 | if (!ct->master) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 63 | DEBUGP("xt_helper: conntrack %p has no master\n", ct); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | return ret; |
| 65 | } |
| 66 | |
Patrick McHardy | e45b1be | 2005-06-21 14:01:30 -0700 | [diff] [blame] | 67 | read_lock_bh(&ip_conntrack_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | if (!ct->master->helper) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 69 | DEBUGP("xt_helper: master ct %p has no helper\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | exp->expectant); |
| 71 | goto out_unlock; |
| 72 | } |
| 73 | |
| 74 | DEBUGP("master's name = %s , info->name = %s\n", |
| 75 | ct->master->helper->name, info->name); |
| 76 | |
| 77 | if (info->name[0] == '\0') |
| 78 | ret ^= 1; |
| 79 | else |
| 80 | ret ^= !strncmp(ct->master->helper->name, info->name, |
| 81 | strlen(ct->master->helper->name)); |
| 82 | out_unlock: |
Patrick McHardy | e45b1be | 2005-06-21 14:01:30 -0700 | [diff] [blame] | 83 | read_unlock_bh(&ip_conntrack_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | return ret; |
| 85 | } |
| 86 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 87 | #else /* CONFIG_IP_NF_CONNTRACK */ |
| 88 | |
| 89 | static int |
| 90 | match(const struct sk_buff *skb, |
| 91 | const struct net_device *in, |
| 92 | const struct net_device *out, |
Patrick McHardy | c498673 | 2006-03-20 18:02:56 -0800 | [diff] [blame] | 93 | const struct xt_match *match, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 94 | const void *matchinfo, |
| 95 | int offset, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 96 | unsigned int protoff, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 97 | int *hotdrop) |
| 98 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 99 | const struct xt_helper_info *info = matchinfo; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 100 | struct nf_conn *ct; |
Harald Welte | dc808fe | 2006-03-20 17:56:32 -0800 | [diff] [blame] | 101 | struct nf_conn_help *master_help; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 102 | enum ip_conntrack_info ctinfo; |
| 103 | int ret = info->invert; |
| 104 | |
| 105 | ct = nf_ct_get((struct sk_buff *)skb, &ctinfo); |
| 106 | if (!ct) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 107 | DEBUGP("xt_helper: Eek! invalid conntrack?\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 108 | return ret; |
| 109 | } |
| 110 | |
| 111 | if (!ct->master) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 112 | DEBUGP("xt_helper: conntrack %p has no master\n", ct); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 113 | return ret; |
| 114 | } |
| 115 | |
| 116 | read_lock_bh(&nf_conntrack_lock); |
Harald Welte | dc808fe | 2006-03-20 17:56:32 -0800 | [diff] [blame] | 117 | master_help = nfct_help(ct->master); |
| 118 | if (!master_help || !master_help->helper) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 119 | DEBUGP("xt_helper: master ct %p has no helper\n", |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 120 | exp->expectant); |
| 121 | goto out_unlock; |
| 122 | } |
| 123 | |
| 124 | DEBUGP("master's name = %s , info->name = %s\n", |
| 125 | ct->master->helper->name, info->name); |
| 126 | |
| 127 | if (info->name[0] == '\0') |
| 128 | ret ^= 1; |
| 129 | else |
Harald Welte | dc808fe | 2006-03-20 17:56:32 -0800 | [diff] [blame] | 130 | ret ^= !strncmp(master_help->helper->name, info->name, |
| 131 | strlen(master_help->helper->name)); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 132 | out_unlock: |
| 133 | read_unlock_bh(&nf_conntrack_lock); |
| 134 | return ret; |
| 135 | } |
| 136 | #endif |
| 137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | static int check(const char *tablename, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 139 | const void *inf, |
Patrick McHardy | c498673 | 2006-03-20 18:02:56 -0800 | [diff] [blame] | 140 | const struct xt_match *match, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | void *matchinfo, |
| 142 | unsigned int matchsize, |
| 143 | unsigned int hook_mask) |
| 144 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 145 | struct xt_helper_info *info = matchinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
| 147 | info->name[29] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | return 1; |
| 149 | } |
| 150 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 151 | static struct xt_match helper_match = { |
| 152 | .name = "helper", |
Patrick McHardy | 5d04bff | 2006-03-20 18:01:58 -0800 | [diff] [blame] | 153 | .match = match, |
| 154 | .matchsize = sizeof(struct xt_helper_info), |
| 155 | .checkentry = check, |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame^] | 156 | .family = AF_INET, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 157 | .me = THIS_MODULE, |
| 158 | }; |
| 159 | static struct xt_match helper6_match = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | .name = "helper", |
Patrick McHardy | 5d04bff | 2006-03-20 18:01:58 -0800 | [diff] [blame] | 161 | .match = match, |
| 162 | .matchsize = sizeof(struct xt_helper_info), |
| 163 | .checkentry = check, |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame^] | 164 | .family = AF_INET6, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | .me = THIS_MODULE, |
| 166 | }; |
| 167 | |
| 168 | static int __init init(void) |
| 169 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 170 | int ret; |
| 171 | need_conntrack(); |
| 172 | |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame^] | 173 | ret = xt_register_match(&helper_match); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 174 | if (ret < 0) |
| 175 | return ret; |
| 176 | |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame^] | 177 | ret = xt_register_match(&helper6_match); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 178 | if (ret < 0) |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame^] | 179 | xt_unregister_match(&helper_match); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 180 | |
| 181 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | static void __exit fini(void) |
| 185 | { |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame^] | 186 | xt_unregister_match(&helper_match); |
| 187 | xt_unregister_match(&helper6_match); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | module_init(init); |
| 191 | module_exit(fini); |
| 192 | |