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