blob: 951b06b8d7018178bdfc61279ff6df541de7e960 [file] [log] [blame]
Harald Welte2e4e6a12006-01-12 13:30:04 -08001#include <linux/types.h>
2#include <linux/module.h>
3#include <net/ip.h>
David S. Miller37d8dc82006-01-13 16:19:44 -08004#include <linux/ipv6.h>
Harald Welte2e4e6a12006-01-12 13:30:04 -08005#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 Engelhardt2ae15b62008-01-14 23:42:28 -080013MODULE_DESCRIPTION("Xtables: TCP, UDP and UDP-Lite match");
Harald Welte2e4e6a12006-01-12 13:30:04 -080014MODULE_LICENSE("GPL");
15MODULE_ALIAS("xt_tcp");
16MODULE_ALIAS("xt_udp");
17MODULE_ALIAS("ipt_udp");
18MODULE_ALIAS("ipt_tcp");
19MODULE_ALIAS("ip6t_udp");
20MODULE_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 Engelhardt1d93a9c2007-07-07 22:15:35 -070030static inline bool
31port_match(u_int16_t min, u_int16_t max, u_int16_t port, bool invert)
Harald Welte2e4e6a12006-01-12 13:30:04 -080032{
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070033 return (port >= min && port <= max) ^ invert;
Harald Welte2e4e6a12006-01-12 13:30:04 -080034}
35
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070036static bool
Harald Welte2e4e6a12006-01-12 13:30:04 -080037tcp_find_option(u_int8_t option,
38 const struct sk_buff *skb,
39 unsigned int protoff,
40 unsigned int optlen,
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070041 bool invert,
Jan Engelhardtcff533a2007-07-07 22:15:12 -070042 bool *hotdrop)
Harald Welte2e4e6a12006-01-12 13:30:04 -080043{
44 /* tcp.doff is only 4 bits, ie. max 15 * 4 bytes */
Jan Engelhardt3cf93c92008-04-14 09:56:05 +020045 const u_int8_t *op;
46 u_int8_t _opt[60 - sizeof(struct tcphdr)];
Harald Welte2e4e6a12006-01-12 13:30:04 -080047 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 Engelhardtcff533a2007-07-07 22:15:12 -070058 *hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070059 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -080060 }
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 Engelhardt1d93a9c2007-07-07 22:15:35 -070071static bool
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080072tcp_mt(const struct sk_buff *skb, const struct net_device *in,
73 const struct net_device *out, const struct xt_match *match,
74 const void *matchinfo, int offset, unsigned int protoff, bool *hotdrop)
Harald Welte2e4e6a12006-01-12 13:30:04 -080075{
Jan Engelhardt3cf93c92008-04-14 09:56:05 +020076 const struct tcphdr *th;
77 struct tcphdr _tcph;
Harald Welte2e4e6a12006-01-12 13:30:04 -080078 const struct xt_tcp *tcpinfo = matchinfo;
79
80 if (offset) {
81 /* To quote Alan:
82
83 Don't allow a fragment of TCP 8 bytes in. Nobody normal
84 causes this. Its a cracker trying to break in by doing a
85 flag overwrite to pass the direction checks.
86 */
87 if (offset == 1) {
88 duprintf("Dropping evil TCP offset=1 frag.\n");
Jan Engelhardtcff533a2007-07-07 22:15:12 -070089 *hotdrop = true;
Harald Welte2e4e6a12006-01-12 13:30:04 -080090 }
91 /* Must not be a fragment. */
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070092 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -080093 }
94
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -070095#define FWINVTCP(bool, invflg) ((bool) ^ !!(tcpinfo->invflags & (invflg)))
Harald Welte2e4e6a12006-01-12 13:30:04 -080096
97 th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
98 if (th == NULL) {
99 /* We've been asked to examine this packet, and we
100 can't. Hence, no choice but to drop. */
101 duprintf("Dropping evil TCP offset=0 tinygram.\n");
Jan Engelhardtcff533a2007-07-07 22:15:12 -0700102 *hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700103 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800104 }
105
106 if (!port_match(tcpinfo->spts[0], tcpinfo->spts[1],
107 ntohs(th->source),
108 !!(tcpinfo->invflags & XT_TCP_INV_SRCPT)))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700109 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800110 if (!port_match(tcpinfo->dpts[0], tcpinfo->dpts[1],
111 ntohs(th->dest),
112 !!(tcpinfo->invflags & XT_TCP_INV_DSTPT)))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700113 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800114 if (!FWINVTCP((((unsigned char *)th)[13] & tcpinfo->flg_mask)
115 == tcpinfo->flg_cmp,
116 XT_TCP_INV_FLAGS))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700117 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800118 if (tcpinfo->option) {
119 if (th->doff * 4 < sizeof(_tcph)) {
Jan Engelhardtcff533a2007-07-07 22:15:12 -0700120 *hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700121 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800122 }
123 if (!tcp_find_option(tcpinfo->option, skb, protoff,
124 th->doff*4 - sizeof(_tcph),
125 tcpinfo->invflags & XT_TCP_INV_OPTION,
126 hotdrop))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700127 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800128 }
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700129 return true;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800130}
131
132/* Called when user tries to insert an entry of this type. */
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700133static bool
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800134tcp_mt_check(const char *tablename, const void *info,
135 const struct xt_match *match, void *matchinfo,
136 unsigned int hook_mask)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800137{
Harald Welte2e4e6a12006-01-12 13:30:04 -0800138 const struct xt_tcp *tcpinfo = matchinfo;
139
Patrick McHardy5d04bff2006-03-20 18:01:58 -0800140 /* Must specify no unknown invflags */
141 return !(tcpinfo->invflags & ~XT_TCP_INV_MASK);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800142}
143
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700144static bool
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800145udp_mt(const struct sk_buff *skb, const struct net_device *in,
146 const struct net_device *out, const struct xt_match *match,
147 const void *matchinfo, int offset, unsigned int protoff, bool *hotdrop)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800148{
Jan Engelhardt3cf93c92008-04-14 09:56:05 +0200149 const struct udphdr *uh;
150 struct udphdr _udph;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800151 const struct xt_udp *udpinfo = matchinfo;
152
153 /* Must not be a fragment. */
154 if (offset)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700155 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800156
157 uh = skb_header_pointer(skb, protoff, sizeof(_udph), &_udph);
158 if (uh == NULL) {
159 /* We've been asked to examine this packet, and we
160 can't. Hence, no choice but to drop. */
161 duprintf("Dropping evil UDP tinygram.\n");
Jan Engelhardtcff533a2007-07-07 22:15:12 -0700162 *hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700163 return false;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800164 }
165
166 return port_match(udpinfo->spts[0], udpinfo->spts[1],
167 ntohs(uh->source),
168 !!(udpinfo->invflags & XT_UDP_INV_SRCPT))
169 && port_match(udpinfo->dpts[0], udpinfo->dpts[1],
170 ntohs(uh->dest),
171 !!(udpinfo->invflags & XT_UDP_INV_DSTPT));
172}
173
174/* Called when user tries to insert an entry of this type. */
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700175static bool
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800176udp_mt_check(const char *tablename, const void *info,
177 const struct xt_match *match, void *matchinfo,
178 unsigned int hook_mask)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800179{
Jesper Bengtssona289d702007-08-30 22:36:43 -0700180 const struct xt_udp *udpinfo = matchinfo;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800181
Patrick McHardy5d04bff2006-03-20 18:01:58 -0800182 /* Must specify no unknown invflags */
183 return !(udpinfo->invflags & ~XT_UDP_INV_MASK);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800184}
185
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800186static struct xt_match tcpudp_mt_reg[] __read_mostly = {
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700187 {
188 .name = "tcp",
189 .family = AF_INET,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800190 .checkentry = tcp_mt_check,
191 .match = tcp_mt,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700192 .matchsize = sizeof(struct xt_tcp),
193 .proto = IPPROTO_TCP,
194 .me = THIS_MODULE,
195 },
196 {
197 .name = "tcp",
198 .family = AF_INET6,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800199 .checkentry = tcp_mt_check,
200 .match = tcp_mt,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700201 .matchsize = sizeof(struct xt_tcp),
202 .proto = IPPROTO_TCP,
203 .me = THIS_MODULE,
204 },
205 {
206 .name = "udp",
207 .family = AF_INET,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800208 .checkentry = udp_mt_check,
209 .match = udp_mt,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700210 .matchsize = sizeof(struct xt_udp),
211 .proto = IPPROTO_UDP,
212 .me = THIS_MODULE,
213 },
214 {
215 .name = "udp",
216 .family = AF_INET6,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800217 .checkentry = udp_mt_check,
218 .match = udp_mt,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700219 .matchsize = sizeof(struct xt_udp),
220 .proto = IPPROTO_UDP,
221 .me = THIS_MODULE,
222 },
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800223 {
224 .name = "udplite",
225 .family = AF_INET,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800226 .checkentry = udp_mt_check,
227 .match = udp_mt,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800228 .matchsize = sizeof(struct xt_udp),
229 .proto = IPPROTO_UDPLITE,
230 .me = THIS_MODULE,
231 },
232 {
233 .name = "udplite",
234 .family = AF_INET6,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800235 .checkentry = udp_mt_check,
236 .match = udp_mt,
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800237 .matchsize = sizeof(struct xt_udp),
238 .proto = IPPROTO_UDPLITE,
239 .me = THIS_MODULE,
240 },
Harald Welte2e4e6a12006-01-12 13:30:04 -0800241};
242
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800243static int __init tcpudp_mt_init(void)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800244{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800245 return xt_register_matches(tcpudp_mt_reg, ARRAY_SIZE(tcpudp_mt_reg));
Harald Welte2e4e6a12006-01-12 13:30:04 -0800246}
247
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800248static void __exit tcpudp_mt_exit(void)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800249{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800250 xt_unregister_matches(tcpudp_mt_reg, ARRAY_SIZE(tcpudp_mt_reg));
Harald Welte2e4e6a12006-01-12 13:30:04 -0800251}
252
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800253module_init(tcpudp_mt_init);
254module_exit(tcpudp_mt_exit);