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