Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 1 | #include <linux/types.h> |
| 2 | #include <linux/module.h> |
| 3 | #include <net/ip.h> |
David S. Miller | 37d8dc8 | 2006-01-13 16:19:44 -0800 | [diff] [blame] | 4 | #include <linux/ipv6.h> |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 5 | #include <net/ipv6.h> |
| 6 | #include <net/tcp.h> |
| 7 | #include <net/udp.h> |
| 8 | #include <linux/netfilter/x_tables.h> |
| 9 | #include <linux/netfilter/xt_tcpudp.h> |
| 10 | #include <linux/netfilter_ipv4/ip_tables.h> |
| 11 | #include <linux/netfilter_ipv6/ip6_tables.h> |
| 12 | |
Jan Engelhardt | 2ae15b6 | 2008-01-14 23:42:28 -0800 | [diff] [blame] | 13 | MODULE_DESCRIPTION("Xtables: TCP, UDP and UDP-Lite match"); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 14 | MODULE_LICENSE("GPL"); |
| 15 | MODULE_ALIAS("xt_tcp"); |
| 16 | MODULE_ALIAS("xt_udp"); |
| 17 | MODULE_ALIAS("ipt_udp"); |
| 18 | MODULE_ALIAS("ipt_tcp"); |
| 19 | MODULE_ALIAS("ip6t_udp"); |
| 20 | MODULE_ALIAS("ip6t_tcp"); |
| 21 | |
| 22 | #ifdef DEBUG_IP_FIREWALL_USER |
| 23 | #define duprintf(format, args...) printk(format , ## args) |
| 24 | #else |
| 25 | #define duprintf(format, args...) |
| 26 | #endif |
| 27 | |
| 28 | |
| 29 | /* Returns 1 if the port is matched by the range, 0 otherwise */ |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 30 | static inline bool |
| 31 | port_match(u_int16_t min, u_int16_t max, u_int16_t port, bool invert) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 32 | { |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 33 | return (port >= min && port <= max) ^ invert; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 34 | } |
| 35 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 36 | static bool |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 37 | tcp_find_option(u_int8_t option, |
| 38 | const struct sk_buff *skb, |
| 39 | unsigned int protoff, |
| 40 | unsigned int optlen, |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 41 | bool invert, |
Jan Engelhardt | cff533a | 2007-07-07 22:15:12 -0700 | [diff] [blame] | 42 | bool *hotdrop) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 43 | { |
| 44 | /* tcp.doff is only 4 bits, ie. max 15 * 4 bytes */ |
Jan Engelhardt | 3cf93c9 | 2008-04-14 09:56:05 +0200 | [diff] [blame] | 45 | const u_int8_t *op; |
| 46 | u_int8_t _opt[60 - sizeof(struct tcphdr)]; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 47 | unsigned int i; |
| 48 | |
| 49 | duprintf("tcp_match: finding option\n"); |
| 50 | |
| 51 | if (!optlen) |
| 52 | return invert; |
| 53 | |
| 54 | /* If we don't have the whole header, drop packet. */ |
| 55 | op = skb_header_pointer(skb, protoff + sizeof(struct tcphdr), |
| 56 | optlen, _opt); |
| 57 | if (op == NULL) { |
Jan Engelhardt | cff533a | 2007-07-07 22:15:12 -0700 | [diff] [blame] | 58 | *hotdrop = true; |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 59 | return false; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | for (i = 0; i < optlen; ) { |
| 63 | if (op[i] == option) return !invert; |
| 64 | if (op[i] < 2) i++; |
| 65 | else i += op[i+1]?:1; |
| 66 | } |
| 67 | |
| 68 | return invert; |
| 69 | } |
| 70 | |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 71 | static bool tcp_mt(const struct sk_buff *skb, const struct xt_match_param *par) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 72 | { |
Jan Engelhardt | 3cf93c9 | 2008-04-14 09:56:05 +0200 | [diff] [blame] | 73 | const struct tcphdr *th; |
| 74 | struct tcphdr _tcph; |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 75 | const struct xt_tcp *tcpinfo = par->matchinfo; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 76 | |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 77 | if (par->fragoff != 0) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 78 | /* To quote Alan: |
| 79 | |
| 80 | Don't allow a fragment of TCP 8 bytes in. Nobody normal |
| 81 | causes this. Its a cracker trying to break in by doing a |
| 82 | flag overwrite to pass the direction checks. |
| 83 | */ |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 84 | if (par->fragoff == 1) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 85 | duprintf("Dropping evil TCP offset=1 frag.\n"); |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 86 | *par->hotdrop = true; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 87 | } |
| 88 | /* Must not be a fragment. */ |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 89 | return false; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Jan Engelhardt | 7c4e36b | 2007-07-07 22:19:08 -0700 | [diff] [blame] | 92 | #define FWINVTCP(bool, invflg) ((bool) ^ !!(tcpinfo->invflags & (invflg))) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 93 | |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 94 | th = skb_header_pointer(skb, par->thoff, sizeof(_tcph), &_tcph); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 95 | if (th == NULL) { |
| 96 | /* We've been asked to examine this packet, and we |
| 97 | can't. Hence, no choice but to drop. */ |
| 98 | duprintf("Dropping evil TCP offset=0 tinygram.\n"); |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 99 | *par->hotdrop = true; |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 100 | return false; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | if (!port_match(tcpinfo->spts[0], tcpinfo->spts[1], |
| 104 | ntohs(th->source), |
| 105 | !!(tcpinfo->invflags & XT_TCP_INV_SRCPT))) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 106 | return false; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 107 | if (!port_match(tcpinfo->dpts[0], tcpinfo->dpts[1], |
| 108 | ntohs(th->dest), |
| 109 | !!(tcpinfo->invflags & XT_TCP_INV_DSTPT))) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 110 | return false; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 111 | if (!FWINVTCP((((unsigned char *)th)[13] & tcpinfo->flg_mask) |
| 112 | == tcpinfo->flg_cmp, |
| 113 | XT_TCP_INV_FLAGS)) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 114 | return false; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 115 | if (tcpinfo->option) { |
| 116 | if (th->doff * 4 < sizeof(_tcph)) { |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 117 | *par->hotdrop = true; |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 118 | return false; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 119 | } |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 120 | if (!tcp_find_option(tcpinfo->option, skb, par->thoff, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 121 | th->doff*4 - sizeof(_tcph), |
| 122 | tcpinfo->invflags & XT_TCP_INV_OPTION, |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 123 | par->hotdrop)) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 124 | return false; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 125 | } |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 126 | return true; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 129 | static bool tcp_mt_check(const struct xt_mtchk_param *par) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 130 | { |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 131 | const struct xt_tcp *tcpinfo = par->matchinfo; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 132 | |
Patrick McHardy | 5d04bff | 2006-03-20 18:01:58 -0800 | [diff] [blame] | 133 | /* Must specify no unknown invflags */ |
| 134 | return !(tcpinfo->invflags & ~XT_TCP_INV_MASK); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 137 | static bool udp_mt(const struct sk_buff *skb, const struct xt_match_param *par) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 138 | { |
Jan Engelhardt | 3cf93c9 | 2008-04-14 09:56:05 +0200 | [diff] [blame] | 139 | const struct udphdr *uh; |
| 140 | struct udphdr _udph; |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 141 | const struct xt_udp *udpinfo = par->matchinfo; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 142 | |
| 143 | /* Must not be a fragment. */ |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 144 | if (par->fragoff != 0) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 145 | return false; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 146 | |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 147 | uh = skb_header_pointer(skb, par->thoff, sizeof(_udph), &_udph); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 148 | if (uh == NULL) { |
| 149 | /* We've been asked to examine this packet, and we |
| 150 | can't. Hence, no choice but to drop. */ |
| 151 | duprintf("Dropping evil UDP tinygram.\n"); |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 152 | *par->hotdrop = true; |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 153 | return false; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | return port_match(udpinfo->spts[0], udpinfo->spts[1], |
| 157 | ntohs(uh->source), |
| 158 | !!(udpinfo->invflags & XT_UDP_INV_SRCPT)) |
| 159 | && port_match(udpinfo->dpts[0], udpinfo->dpts[1], |
| 160 | ntohs(uh->dest), |
| 161 | !!(udpinfo->invflags & XT_UDP_INV_DSTPT)); |
| 162 | } |
| 163 | |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 164 | static bool udp_mt_check(const struct xt_mtchk_param *par) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 165 | { |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 166 | const struct xt_udp *udpinfo = par->matchinfo; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 167 | |
Patrick McHardy | 5d04bff | 2006-03-20 18:01:58 -0800 | [diff] [blame] | 168 | /* Must specify no unknown invflags */ |
| 169 | return !(udpinfo->invflags & ~XT_UDP_INV_MASK); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 172 | static struct xt_match tcpudp_mt_reg[] __read_mostly = { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 173 | { |
| 174 | .name = "tcp", |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 175 | .family = NFPROTO_IPV4, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 176 | .checkentry = tcp_mt_check, |
| 177 | .match = tcp_mt, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 178 | .matchsize = sizeof(struct xt_tcp), |
| 179 | .proto = IPPROTO_TCP, |
| 180 | .me = THIS_MODULE, |
| 181 | }, |
| 182 | { |
| 183 | .name = "tcp", |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 184 | .family = NFPROTO_IPV6, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 185 | .checkentry = tcp_mt_check, |
| 186 | .match = tcp_mt, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 187 | .matchsize = sizeof(struct xt_tcp), |
| 188 | .proto = IPPROTO_TCP, |
| 189 | .me = THIS_MODULE, |
| 190 | }, |
| 191 | { |
| 192 | .name = "udp", |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 193 | .family = NFPROTO_IPV4, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 194 | .checkentry = udp_mt_check, |
| 195 | .match = udp_mt, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 196 | .matchsize = sizeof(struct xt_udp), |
| 197 | .proto = IPPROTO_UDP, |
| 198 | .me = THIS_MODULE, |
| 199 | }, |
| 200 | { |
| 201 | .name = "udp", |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 202 | .family = NFPROTO_IPV6, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 203 | .checkentry = udp_mt_check, |
| 204 | .match = udp_mt, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 205 | .matchsize = sizeof(struct xt_udp), |
| 206 | .proto = IPPROTO_UDP, |
| 207 | .me = THIS_MODULE, |
| 208 | }, |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 209 | { |
| 210 | .name = "udplite", |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 211 | .family = NFPROTO_IPV4, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 212 | .checkentry = udp_mt_check, |
| 213 | .match = udp_mt, |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 214 | .matchsize = sizeof(struct xt_udp), |
| 215 | .proto = IPPROTO_UDPLITE, |
| 216 | .me = THIS_MODULE, |
| 217 | }, |
| 218 | { |
| 219 | .name = "udplite", |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 220 | .family = NFPROTO_IPV6, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 221 | .checkentry = udp_mt_check, |
| 222 | .match = udp_mt, |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 223 | .matchsize = sizeof(struct xt_udp), |
| 224 | .proto = IPPROTO_UDPLITE, |
| 225 | .me = THIS_MODULE, |
| 226 | }, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 227 | }; |
| 228 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 229 | static int __init tcpudp_mt_init(void) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 230 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 231 | return xt_register_matches(tcpudp_mt_reg, ARRAY_SIZE(tcpudp_mt_reg)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 232 | } |
| 233 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 234 | static void __exit tcpudp_mt_exit(void) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 235 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 236 | xt_unregister_matches(tcpudp_mt_reg, ARRAY_SIZE(tcpudp_mt_reg)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 237 | } |
| 238 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 239 | module_init(tcpudp_mt_init); |
| 240 | module_exit(tcpudp_mt_exit); |