blob: ca9ccdd931bccc788327320008c3de5264fc9dc6 [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
Gerrit Renkerba4e58e2006-11-27 11:10:57 -080013MODULE_DESCRIPTION("x_tables match for TCP and UDP(-Lite), supports IPv4 and IPv6");
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 */
30static inline int
31port_match(u_int16_t min, u_int16_t max, u_int16_t port, int invert)
32{
33 int ret;
34
35 ret = (port >= min && port <= max) ^ invert;
36 return ret;
37}
38
39static int
40tcp_find_option(u_int8_t option,
41 const struct sk_buff *skb,
42 unsigned int protoff,
43 unsigned int optlen,
44 int invert,
Jan Engelhardtcff533a2007-07-07 22:15:12 -070045 bool *hotdrop)
Harald Welte2e4e6a12006-01-12 13:30:04 -080046{
47 /* tcp.doff is only 4 bits, ie. max 15 * 4 bytes */
48 u_int8_t _opt[60 - sizeof(struct tcphdr)], *op;
49 unsigned int i;
50
51 duprintf("tcp_match: finding option\n");
52
53 if (!optlen)
54 return invert;
55
56 /* If we don't have the whole header, drop packet. */
57 op = skb_header_pointer(skb, protoff + sizeof(struct tcphdr),
58 optlen, _opt);
59 if (op == NULL) {
Jan Engelhardtcff533a2007-07-07 22:15:12 -070060 *hotdrop = true;
Harald Welte2e4e6a12006-01-12 13:30:04 -080061 return 0;
62 }
63
64 for (i = 0; i < optlen; ) {
65 if (op[i] == option) return !invert;
66 if (op[i] < 2) i++;
67 else i += op[i+1]?:1;
68 }
69
70 return invert;
71}
72
73static int
74tcp_match(const struct sk_buff *skb,
75 const struct net_device *in,
76 const struct net_device *out,
Patrick McHardyc4986732006-03-20 18:02:56 -080077 const struct xt_match *match,
Harald Welte2e4e6a12006-01-12 13:30:04 -080078 const void *matchinfo,
79 int offset,
80 unsigned int protoff,
Jan Engelhardtcff533a2007-07-07 22:15:12 -070081 bool *hotdrop)
Harald Welte2e4e6a12006-01-12 13:30:04 -080082{
83 struct tcphdr _tcph, *th;
84 const struct xt_tcp *tcpinfo = matchinfo;
85
86 if (offset) {
87 /* To quote Alan:
88
89 Don't allow a fragment of TCP 8 bytes in. Nobody normal
90 causes this. Its a cracker trying to break in by doing a
91 flag overwrite to pass the direction checks.
92 */
93 if (offset == 1) {
94 duprintf("Dropping evil TCP offset=1 frag.\n");
Jan Engelhardtcff533a2007-07-07 22:15:12 -070095 *hotdrop = true;
Harald Welte2e4e6a12006-01-12 13:30:04 -080096 }
97 /* Must not be a fragment. */
98 return 0;
99 }
100
101#define FWINVTCP(bool,invflg) ((bool) ^ !!(tcpinfo->invflags & invflg))
102
103 th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
104 if (th == NULL) {
105 /* We've been asked to examine this packet, and we
106 can't. Hence, no choice but to drop. */
107 duprintf("Dropping evil TCP offset=0 tinygram.\n");
Jan Engelhardtcff533a2007-07-07 22:15:12 -0700108 *hotdrop = true;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800109 return 0;
110 }
111
112 if (!port_match(tcpinfo->spts[0], tcpinfo->spts[1],
113 ntohs(th->source),
114 !!(tcpinfo->invflags & XT_TCP_INV_SRCPT)))
115 return 0;
116 if (!port_match(tcpinfo->dpts[0], tcpinfo->dpts[1],
117 ntohs(th->dest),
118 !!(tcpinfo->invflags & XT_TCP_INV_DSTPT)))
119 return 0;
120 if (!FWINVTCP((((unsigned char *)th)[13] & tcpinfo->flg_mask)
121 == tcpinfo->flg_cmp,
122 XT_TCP_INV_FLAGS))
123 return 0;
124 if (tcpinfo->option) {
125 if (th->doff * 4 < sizeof(_tcph)) {
Jan Engelhardtcff533a2007-07-07 22:15:12 -0700126 *hotdrop = true;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800127 return 0;
128 }
129 if (!tcp_find_option(tcpinfo->option, skb, protoff,
130 th->doff*4 - sizeof(_tcph),
131 tcpinfo->invflags & XT_TCP_INV_OPTION,
132 hotdrop))
133 return 0;
134 }
135 return 1;
136}
137
138/* Called when user tries to insert an entry of this type. */
139static int
140tcp_checkentry(const char *tablename,
141 const void *info,
Patrick McHardyc4986732006-03-20 18:02:56 -0800142 const struct xt_match *match,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800143 void *matchinfo,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800144 unsigned int hook_mask)
145{
Harald Welte2e4e6a12006-01-12 13:30:04 -0800146 const struct xt_tcp *tcpinfo = matchinfo;
147
Patrick McHardy5d04bff2006-03-20 18:01:58 -0800148 /* Must specify no unknown invflags */
149 return !(tcpinfo->invflags & ~XT_TCP_INV_MASK);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800150}
151
Harald Welte2e4e6a12006-01-12 13:30:04 -0800152static int
153udp_match(const struct sk_buff *skb,
154 const struct net_device *in,
155 const struct net_device *out,
Patrick McHardyc4986732006-03-20 18:02:56 -0800156 const struct xt_match *match,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800157 const void *matchinfo,
158 int offset,
159 unsigned int protoff,
Jan Engelhardtcff533a2007-07-07 22:15:12 -0700160 bool *hotdrop)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800161{
162 struct udphdr _udph, *uh;
163 const struct xt_udp *udpinfo = matchinfo;
164
165 /* Must not be a fragment. */
166 if (offset)
167 return 0;
168
169 uh = skb_header_pointer(skb, protoff, sizeof(_udph), &_udph);
170 if (uh == NULL) {
171 /* We've been asked to examine this packet, and we
172 can't. Hence, no choice but to drop. */
173 duprintf("Dropping evil UDP tinygram.\n");
Jan Engelhardtcff533a2007-07-07 22:15:12 -0700174 *hotdrop = true;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800175 return 0;
176 }
177
178 return port_match(udpinfo->spts[0], udpinfo->spts[1],
179 ntohs(uh->source),
180 !!(udpinfo->invflags & XT_UDP_INV_SRCPT))
181 && port_match(udpinfo->dpts[0], udpinfo->dpts[1],
182 ntohs(uh->dest),
183 !!(udpinfo->invflags & XT_UDP_INV_DSTPT));
184}
185
186/* Called when user tries to insert an entry of this type. */
187static int
188udp_checkentry(const char *tablename,
189 const void *info,
Patrick McHardyc4986732006-03-20 18:02:56 -0800190 const struct xt_match *match,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800191 void *matchinfo,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800192 unsigned int hook_mask)
193{
Patrick McHardy5d04bff2006-03-20 18:01:58 -0800194 const struct xt_tcp *udpinfo = matchinfo;
Harald Welte2e4e6a12006-01-12 13:30:04 -0800195
Patrick McHardy5d04bff2006-03-20 18:01:58 -0800196 /* Must specify no unknown invflags */
197 return !(udpinfo->invflags & ~XT_UDP_INV_MASK);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800198}
199
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700200static struct xt_match xt_tcpudp_match[] = {
201 {
202 .name = "tcp",
203 .family = AF_INET,
204 .checkentry = tcp_checkentry,
205 .match = tcp_match,
206 .matchsize = sizeof(struct xt_tcp),
207 .proto = IPPROTO_TCP,
208 .me = THIS_MODULE,
209 },
210 {
211 .name = "tcp",
212 .family = AF_INET6,
213 .checkentry = tcp_checkentry,
214 .match = tcp_match,
215 .matchsize = sizeof(struct xt_tcp),
216 .proto = IPPROTO_TCP,
217 .me = THIS_MODULE,
218 },
219 {
220 .name = "udp",
221 .family = AF_INET,
222 .checkentry = udp_checkentry,
223 .match = udp_match,
224 .matchsize = sizeof(struct xt_udp),
225 .proto = IPPROTO_UDP,
226 .me = THIS_MODULE,
227 },
228 {
229 .name = "udp",
230 .family = AF_INET6,
231 .checkentry = udp_checkentry,
232 .match = udp_match,
233 .matchsize = sizeof(struct xt_udp),
234 .proto = IPPROTO_UDP,
235 .me = THIS_MODULE,
236 },
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800237 {
238 .name = "udplite",
239 .family = AF_INET,
240 .checkentry = udp_checkentry,
241 .match = udp_match,
242 .matchsize = sizeof(struct xt_udp),
243 .proto = IPPROTO_UDPLITE,
244 .me = THIS_MODULE,
245 },
246 {
247 .name = "udplite",
248 .family = AF_INET6,
249 .checkentry = udp_checkentry,
250 .match = udp_match,
251 .matchsize = sizeof(struct xt_udp),
252 .proto = IPPROTO_UDPLITE,
253 .me = THIS_MODULE,
254 },
Harald Welte2e4e6a12006-01-12 13:30:04 -0800255};
256
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800257static int __init xt_tcpudp_init(void)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800258{
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700259 return xt_register_matches(xt_tcpudp_match,
260 ARRAY_SIZE(xt_tcpudp_match));
Harald Welte2e4e6a12006-01-12 13:30:04 -0800261}
262
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800263static void __exit xt_tcpudp_fini(void)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800264{
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700265 xt_unregister_matches(xt_tcpudp_match, ARRAY_SIZE(xt_tcpudp_match));
Harald Welte2e4e6a12006-01-12 13:30:04 -0800266}
267
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800268module_init(xt_tcpudp_init);
269module_exit(xt_tcpudp_fini);