blob: c14d4645daa3525d3ce45548344e4973763cd599 [file] [log] [blame]
Jan Engelhardtbe91fd52010-03-18 02:22:32 +01001#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Harald Welte2e4e6a12006-01-12 13:30:04 -08002#include <linux/types.h>
3#include <linux/module.h>
4#include <net/ip.h>
David S. Miller37d8dc82006-01-13 16:19:44 -08005#include <linux/ipv6.h>
Harald Welte2e4e6a12006-01-12 13:30:04 -08006#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 Engelhardt2ae15b62008-01-14 23:42:28 -080014MODULE_DESCRIPTION("Xtables: TCP, UDP and UDP-Lite match");
Harald Welte2e4e6a12006-01-12 13:30:04 -080015MODULE_LICENSE("GPL");
16MODULE_ALIAS("xt_tcp");
17MODULE_ALIAS("xt_udp");
18MODULE_ALIAS("ipt_udp");
19MODULE_ALIAS("ipt_tcp");
20MODULE_ALIAS("ip6t_udp");
21MODULE_ALIAS("ip6t_tcp");
22
Harald Welte2e4e6a12006-01-12 13:30:04 -080023/* Returns 1 if the port is matched by the range, 0 otherwise */
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070024static inline bool
25port_match(u_int16_t min, u_int16_t max, u_int16_t port, bool invert)
Harald Welte2e4e6a12006-01-12 13:30:04 -080026{
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070027 return (port >= min && port <= max) ^ invert;
Harald Welte2e4e6a12006-01-12 13:30:04 -080028}
29
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070030static bool
Harald Welte2e4e6a12006-01-12 13:30:04 -080031tcp_find_option(u_int8_t option,
32 const struct sk_buff *skb,
33 unsigned int protoff,
34 unsigned int optlen,
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070035 bool invert,
Jan Engelhardtcff533a2007-07-07 22:15:12 -070036 bool *hotdrop)
Harald Welte2e4e6a12006-01-12 13:30:04 -080037{
38 /* tcp.doff is only 4 bits, ie. max 15 * 4 bytes */
Jan Engelhardt3cf93c92008-04-14 09:56:05 +020039 const u_int8_t *op;
40 u_int8_t _opt[60 - sizeof(struct tcphdr)];
Harald Welte2e4e6a12006-01-12 13:30:04 -080041 unsigned int i;
42
Jan Engelhardtbe91fd52010-03-18 02:22:32 +010043 pr_debug("finding option\n");
Harald Welte2e4e6a12006-01-12 13:30:04 -080044
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 Engelhardtcff533a2007-07-07 22:15:12 -070052 *hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070053 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -080054 }
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 Engelhardt62fc8052009-07-07 20:42:08 +020065static bool tcp_mt(const struct sk_buff *skb, struct xt_action_param *par)
Harald Welte2e4e6a12006-01-12 13:30:04 -080066{
Jan Engelhardt3cf93c92008-04-14 09:56:05 +020067 const struct tcphdr *th;
68 struct tcphdr _tcph;
Jan Engelhardtf7108a22008-10-08 11:35:18 +020069 const struct xt_tcp *tcpinfo = par->matchinfo;
Harald Welte2e4e6a12006-01-12 13:30:04 -080070
Jan Engelhardtf7108a22008-10-08 11:35:18 +020071 if (par->fragoff != 0) {
Harald Welte2e4e6a12006-01-12 13:30:04 -080072 /* 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 Engelhardtf7108a22008-10-08 11:35:18 +020078 if (par->fragoff == 1) {
Jan Engelhardtbe91fd52010-03-18 02:22:32 +010079 pr_debug("Dropping evil TCP offset=1 frag.\n");
Jan Engelhardtb4ba2612009-07-07 20:54:30 +020080 par->hotdrop = true;
Harald Welte2e4e6a12006-01-12 13:30:04 -080081 }
82 /* Must not be a fragment. */
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070083 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -080084 }
85
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -070086#define FWINVTCP(bool, invflg) ((bool) ^ !!(tcpinfo->invflags & (invflg)))
Harald Welte2e4e6a12006-01-12 13:30:04 -080087
Jan Engelhardtf7108a22008-10-08 11:35:18 +020088 th = skb_header_pointer(skb, par->thoff, sizeof(_tcph), &_tcph);
Harald Welte2e4e6a12006-01-12 13:30:04 -080089 if (th == NULL) {
90 /* We've been asked to examine this packet, and we
91 can't. Hence, no choice but to drop. */
Jan Engelhardtbe91fd52010-03-18 02:22:32 +010092 pr_debug("Dropping evil TCP offset=0 tinygram.\n");
Jan Engelhardtb4ba2612009-07-07 20:54:30 +020093 par->hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070094 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -080095 }
96
97 if (!port_match(tcpinfo->spts[0], tcpinfo->spts[1],
98 ntohs(th->source),
99 !!(tcpinfo->invflags & XT_TCP_INV_SRCPT)))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700100 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800101 if (!port_match(tcpinfo->dpts[0], tcpinfo->dpts[1],
102 ntohs(th->dest),
103 !!(tcpinfo->invflags & XT_TCP_INV_DSTPT)))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700104 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800105 if (!FWINVTCP((((unsigned char *)th)[13] & tcpinfo->flg_mask)
106 == tcpinfo->flg_cmp,
107 XT_TCP_INV_FLAGS))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700108 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800109 if (tcpinfo->option) {
110 if (th->doff * 4 < sizeof(_tcph)) {
Jan Engelhardtb4ba2612009-07-07 20:54:30 +0200111 par->hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700112 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800113 }
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200114 if (!tcp_find_option(tcpinfo->option, skb, par->thoff,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800115 th->doff*4 - sizeof(_tcph),
116 tcpinfo->invflags & XT_TCP_INV_OPTION,
Jan Engelhardtb4ba2612009-07-07 20:54:30 +0200117 &par->hotdrop))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700118 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800119 }
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700120 return true;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800121}
122
Jan Engelhardtb0f38452010-03-19 17:16:42 +0100123static int tcp_mt_check(const struct xt_mtchk_param *par)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800124{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200125 const struct xt_tcp *tcpinfo = par->matchinfo;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800126
Patrick McHardy5d04bff2006-03-20 18:01:58 -0800127 /* Must specify no unknown invflags */
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100128 return (tcpinfo->invflags & ~XT_TCP_INV_MASK) ? -EINVAL : 0;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800129}
130
Jan Engelhardt62fc8052009-07-07 20:42:08 +0200131static bool udp_mt(const struct sk_buff *skb, struct xt_action_param *par)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800132{
Jan Engelhardt3cf93c92008-04-14 09:56:05 +0200133 const struct udphdr *uh;
134 struct udphdr _udph;
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200135 const struct xt_udp *udpinfo = par->matchinfo;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800136
137 /* Must not be a fragment. */
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200138 if (par->fragoff != 0)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700139 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800140
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200141 uh = skb_header_pointer(skb, par->thoff, sizeof(_udph), &_udph);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800142 if (uh == NULL) {
143 /* We've been asked to examine this packet, and we
144 can't. Hence, no choice but to drop. */
Jan Engelhardtbe91fd52010-03-18 02:22:32 +0100145 pr_debug("Dropping evil UDP tinygram.\n");
Jan Engelhardtb4ba2612009-07-07 20:54:30 +0200146 par->hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700147 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800148 }
149
150 return port_match(udpinfo->spts[0], udpinfo->spts[1],
151 ntohs(uh->source),
152 !!(udpinfo->invflags & XT_UDP_INV_SRCPT))
153 && port_match(udpinfo->dpts[0], udpinfo->dpts[1],
154 ntohs(uh->dest),
155 !!(udpinfo->invflags & XT_UDP_INV_DSTPT));
156}
157
Jan Engelhardtb0f38452010-03-19 17:16:42 +0100158static int udp_mt_check(const struct xt_mtchk_param *par)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800159{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200160 const struct xt_udp *udpinfo = par->matchinfo;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800161
Patrick McHardy5d04bff2006-03-20 18:01:58 -0800162 /* Must specify no unknown invflags */
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100163 return (udpinfo->invflags & ~XT_UDP_INV_MASK) ? -EINVAL : 0;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800164}
165
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800166static struct xt_match tcpudp_mt_reg[] __read_mostly = {
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700167 {
168 .name = "tcp",
Jan Engelhardtee999d82008-10-08 11:35:01 +0200169 .family = NFPROTO_IPV4,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800170 .checkentry = tcp_mt_check,
171 .match = tcp_mt,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700172 .matchsize = sizeof(struct xt_tcp),
173 .proto = IPPROTO_TCP,
174 .me = THIS_MODULE,
175 },
176 {
177 .name = "tcp",
Jan Engelhardtee999d82008-10-08 11:35:01 +0200178 .family = NFPROTO_IPV6,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800179 .checkentry = tcp_mt_check,
180 .match = tcp_mt,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700181 .matchsize = sizeof(struct xt_tcp),
182 .proto = IPPROTO_TCP,
183 .me = THIS_MODULE,
184 },
185 {
186 .name = "udp",
Jan Engelhardtee999d82008-10-08 11:35:01 +0200187 .family = NFPROTO_IPV4,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800188 .checkentry = udp_mt_check,
189 .match = udp_mt,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700190 .matchsize = sizeof(struct xt_udp),
191 .proto = IPPROTO_UDP,
192 .me = THIS_MODULE,
193 },
194 {
195 .name = "udp",
Jan Engelhardtee999d82008-10-08 11:35:01 +0200196 .family = NFPROTO_IPV6,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800197 .checkentry = udp_mt_check,
198 .match = udp_mt,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700199 .matchsize = sizeof(struct xt_udp),
200 .proto = IPPROTO_UDP,
201 .me = THIS_MODULE,
202 },
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800203 {
204 .name = "udplite",
Jan Engelhardtee999d82008-10-08 11:35:01 +0200205 .family = NFPROTO_IPV4,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800206 .checkentry = udp_mt_check,
207 .match = udp_mt,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800208 .matchsize = sizeof(struct xt_udp),
209 .proto = IPPROTO_UDPLITE,
210 .me = THIS_MODULE,
211 },
212 {
213 .name = "udplite",
Jan Engelhardtee999d82008-10-08 11:35:01 +0200214 .family = NFPROTO_IPV6,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800215 .checkentry = udp_mt_check,
216 .match = udp_mt,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800217 .matchsize = sizeof(struct xt_udp),
218 .proto = IPPROTO_UDPLITE,
219 .me = THIS_MODULE,
220 },
Harald Welte2e4e6a12006-01-12 13:30:04 -0800221};
222
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800223static int __init tcpudp_mt_init(void)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800224{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800225 return xt_register_matches(tcpudp_mt_reg, ARRAY_SIZE(tcpudp_mt_reg));
Harald Welte2e4e6a12006-01-12 13:30:04 -0800226}
227
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800228static void __exit tcpudp_mt_exit(void)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800229{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800230 xt_unregister_matches(tcpudp_mt_reg, ARRAY_SIZE(tcpudp_mt_reg));
Harald Welte2e4e6a12006-01-12 13:30:04 -0800231}
232
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800233module_init(tcpudp_mt_init);
234module_exit(tcpudp_mt_exit);