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 | |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 13 | MODULE_DESCRIPTION("x_tables match for TCP and UDP(-Lite), supports IPv4 and IPv6"); |
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 */ |
| 45 | u_int8_t _opt[60 - sizeof(struct tcphdr)], *op; |
| 46 | unsigned int i; |
| 47 | |
| 48 | duprintf("tcp_match: finding option\n"); |
| 49 | |
| 50 | if (!optlen) |
| 51 | return invert; |
| 52 | |
| 53 | /* If we don't have the whole header, drop packet. */ |
| 54 | op = skb_header_pointer(skb, protoff + sizeof(struct tcphdr), |
| 55 | optlen, _opt); |
| 56 | if (op == NULL) { |
Jan Engelhardt | cff533a | 2007-07-07 22:15:12 -0700 | [diff] [blame] | 57 | *hotdrop = true; |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 58 | return false; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | for (i = 0; i < optlen; ) { |
| 62 | if (op[i] == option) return !invert; |
| 63 | if (op[i] < 2) i++; |
| 64 | else i += op[i+1]?:1; |
| 65 | } |
| 66 | |
| 67 | return invert; |
| 68 | } |
| 69 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 70 | static bool |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 71 | tcp_match(const struct sk_buff *skb, |
| 72 | const struct net_device *in, |
| 73 | const struct net_device *out, |
Patrick McHardy | c498673 | 2006-03-20 18:02:56 -0800 | [diff] [blame] | 74 | const struct xt_match *match, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 75 | const void *matchinfo, |
| 76 | int offset, |
| 77 | unsigned int protoff, |
Jan Engelhardt | cff533a | 2007-07-07 22:15:12 -0700 | [diff] [blame] | 78 | bool *hotdrop) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 79 | { |
| 80 | struct tcphdr _tcph, *th; |
| 81 | const struct xt_tcp *tcpinfo = matchinfo; |
| 82 | |
| 83 | if (offset) { |
| 84 | /* To quote Alan: |
| 85 | |
| 86 | Don't allow a fragment of TCP 8 bytes in. Nobody normal |
| 87 | causes this. Its a cracker trying to break in by doing a |
| 88 | flag overwrite to pass the direction checks. |
| 89 | */ |
| 90 | if (offset == 1) { |
| 91 | duprintf("Dropping evil TCP offset=1 frag.\n"); |
Jan Engelhardt | cff533a | 2007-07-07 22:15:12 -0700 | [diff] [blame] | 92 | *hotdrop = true; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 93 | } |
| 94 | /* Must not be a fragment. */ |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 95 | return false; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Jan Engelhardt | 7c4e36b | 2007-07-07 22:19:08 -0700 | [diff] [blame] | 98 | #define FWINVTCP(bool, invflg) ((bool) ^ !!(tcpinfo->invflags & (invflg))) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 99 | |
| 100 | th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph); |
| 101 | if (th == NULL) { |
| 102 | /* We've been asked to examine this packet, and we |
| 103 | can't. Hence, no choice but to drop. */ |
| 104 | duprintf("Dropping evil TCP offset=0 tinygram.\n"); |
Jan Engelhardt | cff533a | 2007-07-07 22:15:12 -0700 | [diff] [blame] | 105 | *hotdrop = true; |
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 | } |
| 108 | |
| 109 | if (!port_match(tcpinfo->spts[0], tcpinfo->spts[1], |
| 110 | ntohs(th->source), |
| 111 | !!(tcpinfo->invflags & XT_TCP_INV_SRCPT))) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 112 | return false; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 113 | if (!port_match(tcpinfo->dpts[0], tcpinfo->dpts[1], |
| 114 | ntohs(th->dest), |
| 115 | !!(tcpinfo->invflags & XT_TCP_INV_DSTPT))) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 116 | return false; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 117 | if (!FWINVTCP((((unsigned char *)th)[13] & tcpinfo->flg_mask) |
| 118 | == tcpinfo->flg_cmp, |
| 119 | XT_TCP_INV_FLAGS)) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 120 | return false; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 121 | if (tcpinfo->option) { |
| 122 | if (th->doff * 4 < sizeof(_tcph)) { |
Jan Engelhardt | cff533a | 2007-07-07 22:15:12 -0700 | [diff] [blame] | 123 | *hotdrop = true; |
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 | } |
| 126 | if (!tcp_find_option(tcpinfo->option, skb, protoff, |
| 127 | th->doff*4 - sizeof(_tcph), |
| 128 | tcpinfo->invflags & XT_TCP_INV_OPTION, |
| 129 | hotdrop)) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 130 | return false; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 131 | } |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 132 | return true; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /* Called when user tries to insert an entry of this type. */ |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 136 | static bool |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 137 | tcp_checkentry(const char *tablename, |
| 138 | const void *info, |
Patrick McHardy | c498673 | 2006-03-20 18:02:56 -0800 | [diff] [blame] | 139 | const struct xt_match *match, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 140 | void *matchinfo, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 141 | unsigned int hook_mask) |
| 142 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 143 | const struct xt_tcp *tcpinfo = matchinfo; |
| 144 | |
Patrick McHardy | 5d04bff | 2006-03-20 18:01:58 -0800 | [diff] [blame] | 145 | /* Must specify no unknown invflags */ |
| 146 | return !(tcpinfo->invflags & ~XT_TCP_INV_MASK); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 149 | static bool |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 150 | udp_match(const struct sk_buff *skb, |
| 151 | const struct net_device *in, |
| 152 | const struct net_device *out, |
Patrick McHardy | c498673 | 2006-03-20 18:02:56 -0800 | [diff] [blame] | 153 | const struct xt_match *match, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 154 | const void *matchinfo, |
| 155 | int offset, |
| 156 | unsigned int protoff, |
Jan Engelhardt | cff533a | 2007-07-07 22:15:12 -0700 | [diff] [blame] | 157 | bool *hotdrop) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 158 | { |
| 159 | struct udphdr _udph, *uh; |
| 160 | const struct xt_udp *udpinfo = matchinfo; |
| 161 | |
| 162 | /* Must not be a fragment. */ |
| 163 | if (offset) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 164 | return false; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 165 | |
| 166 | uh = skb_header_pointer(skb, protoff, sizeof(_udph), &_udph); |
| 167 | if (uh == NULL) { |
| 168 | /* We've been asked to examine this packet, and we |
| 169 | can't. Hence, no choice but to drop. */ |
| 170 | duprintf("Dropping evil UDP tinygram.\n"); |
Jan Engelhardt | cff533a | 2007-07-07 22:15:12 -0700 | [diff] [blame] | 171 | *hotdrop = true; |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 172 | return false; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | return port_match(udpinfo->spts[0], udpinfo->spts[1], |
| 176 | ntohs(uh->source), |
| 177 | !!(udpinfo->invflags & XT_UDP_INV_SRCPT)) |
| 178 | && port_match(udpinfo->dpts[0], udpinfo->dpts[1], |
| 179 | ntohs(uh->dest), |
| 180 | !!(udpinfo->invflags & XT_UDP_INV_DSTPT)); |
| 181 | } |
| 182 | |
| 183 | /* Called when user tries to insert an entry of this type. */ |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 184 | static bool |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 185 | udp_checkentry(const char *tablename, |
| 186 | const void *info, |
Patrick McHardy | c498673 | 2006-03-20 18:02:56 -0800 | [diff] [blame] | 187 | const struct xt_match *match, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 188 | void *matchinfo, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 189 | unsigned int hook_mask) |
| 190 | { |
Patrick McHardy | 5d04bff | 2006-03-20 18:01:58 -0800 | [diff] [blame] | 191 | const struct xt_tcp *udpinfo = matchinfo; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 192 | |
Patrick McHardy | 5d04bff | 2006-03-20 18:01:58 -0800 | [diff] [blame] | 193 | /* Must specify no unknown invflags */ |
| 194 | return !(udpinfo->invflags & ~XT_UDP_INV_MASK); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 195 | } |
| 196 | |
Patrick McHardy | 9f15c53 | 2007-07-07 22:22:02 -0700 | [diff] [blame] | 197 | static struct xt_match xt_tcpudp_match[] __read_mostly = { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 198 | { |
| 199 | .name = "tcp", |
| 200 | .family = AF_INET, |
| 201 | .checkentry = tcp_checkentry, |
| 202 | .match = tcp_match, |
| 203 | .matchsize = sizeof(struct xt_tcp), |
| 204 | .proto = IPPROTO_TCP, |
| 205 | .me = THIS_MODULE, |
| 206 | }, |
| 207 | { |
| 208 | .name = "tcp", |
| 209 | .family = AF_INET6, |
| 210 | .checkentry = tcp_checkentry, |
| 211 | .match = tcp_match, |
| 212 | .matchsize = sizeof(struct xt_tcp), |
| 213 | .proto = IPPROTO_TCP, |
| 214 | .me = THIS_MODULE, |
| 215 | }, |
| 216 | { |
| 217 | .name = "udp", |
| 218 | .family = AF_INET, |
| 219 | .checkentry = udp_checkentry, |
| 220 | .match = udp_match, |
| 221 | .matchsize = sizeof(struct xt_udp), |
| 222 | .proto = IPPROTO_UDP, |
| 223 | .me = THIS_MODULE, |
| 224 | }, |
| 225 | { |
| 226 | .name = "udp", |
| 227 | .family = AF_INET6, |
| 228 | .checkentry = udp_checkentry, |
| 229 | .match = udp_match, |
| 230 | .matchsize = sizeof(struct xt_udp), |
| 231 | .proto = IPPROTO_UDP, |
| 232 | .me = THIS_MODULE, |
| 233 | }, |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 234 | { |
| 235 | .name = "udplite", |
| 236 | .family = AF_INET, |
| 237 | .checkentry = udp_checkentry, |
| 238 | .match = udp_match, |
| 239 | .matchsize = sizeof(struct xt_udp), |
| 240 | .proto = IPPROTO_UDPLITE, |
| 241 | .me = THIS_MODULE, |
| 242 | }, |
| 243 | { |
| 244 | .name = "udplite", |
| 245 | .family = AF_INET6, |
| 246 | .checkentry = udp_checkentry, |
| 247 | .match = udp_match, |
| 248 | .matchsize = sizeof(struct xt_udp), |
| 249 | .proto = IPPROTO_UDPLITE, |
| 250 | .me = THIS_MODULE, |
| 251 | }, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 252 | }; |
| 253 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 254 | static int __init xt_tcpudp_init(void) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 255 | { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 256 | return xt_register_matches(xt_tcpudp_match, |
| 257 | ARRAY_SIZE(xt_tcpudp_match)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 258 | } |
| 259 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 260 | static void __exit xt_tcpudp_fini(void) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 261 | { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 262 | xt_unregister_matches(xt_tcpudp_match, ARRAY_SIZE(xt_tcpudp_match)); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 263 | } |
| 264 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 265 | module_init(xt_tcpudp_init); |
| 266 | module_exit(xt_tcpudp_fini); |