blob: 669c8113cc601a57ea696f0fd965cb16015e8b49 [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
13MODULE_DESCRIPTION("x_tables match for TCP and UDP, supports IPv4 and IPv6");
14MODULE_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,
45 int *hotdrop)
46{
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) {
60 *hotdrop = 1;
61 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,
77 const void *matchinfo,
78 int offset,
79 unsigned int protoff,
80 int *hotdrop)
81{
82 struct tcphdr _tcph, *th;
83 const struct xt_tcp *tcpinfo = matchinfo;
84
85 if (offset) {
86 /* To quote Alan:
87
88 Don't allow a fragment of TCP 8 bytes in. Nobody normal
89 causes this. Its a cracker trying to break in by doing a
90 flag overwrite to pass the direction checks.
91 */
92 if (offset == 1) {
93 duprintf("Dropping evil TCP offset=1 frag.\n");
94 *hotdrop = 1;
95 }
96 /* Must not be a fragment. */
97 return 0;
98 }
99
100#define FWINVTCP(bool,invflg) ((bool) ^ !!(tcpinfo->invflags & invflg))
101
102 th = skb_header_pointer(skb, protoff, sizeof(_tcph), &_tcph);
103 if (th == NULL) {
104 /* We've been asked to examine this packet, and we
105 can't. Hence, no choice but to drop. */
106 duprintf("Dropping evil TCP offset=0 tinygram.\n");
107 *hotdrop = 1;
108 return 0;
109 }
110
111 if (!port_match(tcpinfo->spts[0], tcpinfo->spts[1],
112 ntohs(th->source),
113 !!(tcpinfo->invflags & XT_TCP_INV_SRCPT)))
114 return 0;
115 if (!port_match(tcpinfo->dpts[0], tcpinfo->dpts[1],
116 ntohs(th->dest),
117 !!(tcpinfo->invflags & XT_TCP_INV_DSTPT)))
118 return 0;
119 if (!FWINVTCP((((unsigned char *)th)[13] & tcpinfo->flg_mask)
120 == tcpinfo->flg_cmp,
121 XT_TCP_INV_FLAGS))
122 return 0;
123 if (tcpinfo->option) {
124 if (th->doff * 4 < sizeof(_tcph)) {
125 *hotdrop = 1;
126 return 0;
127 }
128 if (!tcp_find_option(tcpinfo->option, skb, protoff,
129 th->doff*4 - sizeof(_tcph),
130 tcpinfo->invflags & XT_TCP_INV_OPTION,
131 hotdrop))
132 return 0;
133 }
134 return 1;
135}
136
137/* Called when user tries to insert an entry of this type. */
138static int
139tcp_checkentry(const char *tablename,
140 const void *info,
141 void *matchinfo,
142 unsigned int matchsize,
143 unsigned int hook_mask)
144{
145 const struct ipt_ip *ip = info;
146 const struct xt_tcp *tcpinfo = matchinfo;
147
148 /* Must specify proto == TCP, and no unknown invflags */
149 return ip->proto == IPPROTO_TCP
150 && !(ip->invflags & XT_INV_PROTO)
151 && matchsize == XT_ALIGN(sizeof(struct xt_tcp))
152 && !(tcpinfo->invflags & ~XT_TCP_INV_MASK);
153}
154
155/* Called when user tries to insert an entry of this type. */
156static int
157tcp6_checkentry(const char *tablename,
158 const void *entry,
159 void *matchinfo,
160 unsigned int matchsize,
161 unsigned int hook_mask)
162{
163 const struct ip6t_ip6 *ipv6 = entry;
164 const struct xt_tcp *tcpinfo = matchinfo;
165
166 /* Must specify proto == TCP, and no unknown invflags */
167 return ipv6->proto == IPPROTO_TCP
168 && !(ipv6->invflags & XT_INV_PROTO)
169 && matchsize == XT_ALIGN(sizeof(struct xt_tcp))
170 && !(tcpinfo->invflags & ~XT_TCP_INV_MASK);
171}
172
173
174static int
175udp_match(const struct sk_buff *skb,
176 const struct net_device *in,
177 const struct net_device *out,
178 const void *matchinfo,
179 int offset,
180 unsigned int protoff,
181 int *hotdrop)
182{
183 struct udphdr _udph, *uh;
184 const struct xt_udp *udpinfo = matchinfo;
185
186 /* Must not be a fragment. */
187 if (offset)
188 return 0;
189
190 uh = skb_header_pointer(skb, protoff, sizeof(_udph), &_udph);
191 if (uh == NULL) {
192 /* We've been asked to examine this packet, and we
193 can't. Hence, no choice but to drop. */
194 duprintf("Dropping evil UDP tinygram.\n");
195 *hotdrop = 1;
196 return 0;
197 }
198
199 return port_match(udpinfo->spts[0], udpinfo->spts[1],
200 ntohs(uh->source),
201 !!(udpinfo->invflags & XT_UDP_INV_SRCPT))
202 && port_match(udpinfo->dpts[0], udpinfo->dpts[1],
203 ntohs(uh->dest),
204 !!(udpinfo->invflags & XT_UDP_INV_DSTPT));
205}
206
207/* Called when user tries to insert an entry of this type. */
208static int
209udp_checkentry(const char *tablename,
210 const void *info,
211 void *matchinfo,
212 unsigned int matchinfosize,
213 unsigned int hook_mask)
214{
215 const struct ipt_ip *ip = info;
216 const struct xt_udp *udpinfo = matchinfo;
217
218 /* Must specify proto == UDP, and no unknown invflags */
219 if (ip->proto != IPPROTO_UDP || (ip->invflags & XT_INV_PROTO)) {
220 duprintf("ipt_udp: Protocol %u != %u\n", ip->proto,
221 IPPROTO_UDP);
222 return 0;
223 }
224 if (matchinfosize != XT_ALIGN(sizeof(struct xt_udp))) {
225 duprintf("ipt_udp: matchsize %u != %u\n",
226 matchinfosize, XT_ALIGN(sizeof(struct xt_udp)));
227 return 0;
228 }
229 if (udpinfo->invflags & ~XT_UDP_INV_MASK) {
230 duprintf("ipt_udp: unknown flags %X\n",
231 udpinfo->invflags);
232 return 0;
233 }
234
235 return 1;
236}
237
238/* Called when user tries to insert an entry of this type. */
239static int
240udp6_checkentry(const char *tablename,
241 const void *entry,
242 void *matchinfo,
243 unsigned int matchinfosize,
244 unsigned int hook_mask)
245{
246 const struct ip6t_ip6 *ipv6 = entry;
247 const struct xt_udp *udpinfo = matchinfo;
248
249 /* Must specify proto == UDP, and no unknown invflags */
250 if (ipv6->proto != IPPROTO_UDP || (ipv6->invflags & XT_INV_PROTO)) {
251 duprintf("ip6t_udp: Protocol %u != %u\n", ipv6->proto,
252 IPPROTO_UDP);
253 return 0;
254 }
255 if (matchinfosize != XT_ALIGN(sizeof(struct xt_udp))) {
256 duprintf("ip6t_udp: matchsize %u != %u\n",
257 matchinfosize, XT_ALIGN(sizeof(struct xt_udp)));
258 return 0;
259 }
260 if (udpinfo->invflags & ~XT_UDP_INV_MASK) {
261 duprintf("ip6t_udp: unknown flags %X\n",
262 udpinfo->invflags);
263 return 0;
264 }
265
266 return 1;
267}
268
269static struct xt_match tcp_matchstruct = {
270 .name = "tcp",
271 .match = &tcp_match,
272 .checkentry = &tcp_checkentry,
273 .me = THIS_MODULE,
274};
275static struct xt_match tcp6_matchstruct = {
276 .name = "tcp",
277 .match = &tcp_match,
278 .checkentry = &tcp6_checkentry,
279 .me = THIS_MODULE,
280};
281
282static struct xt_match udp_matchstruct = {
283 .name = "udp",
284 .match = &udp_match,
285 .checkentry = &udp_checkentry,
286 .me = THIS_MODULE,
287};
288static struct xt_match udp6_matchstruct = {
289 .name = "udp",
290 .match = &udp_match,
291 .checkentry = &udp6_checkentry,
292 .me = THIS_MODULE,
293};
294
295static int __init init(void)
296{
297 int ret;
298 ret = xt_register_match(AF_INET, &tcp_matchstruct);
299 if (ret)
300 return ret;
301
302 ret = xt_register_match(AF_INET6, &tcp6_matchstruct);
303 if (ret)
304 goto out_unreg_tcp;
305
306 ret = xt_register_match(AF_INET, &udp_matchstruct);
307 if (ret)
308 goto out_unreg_tcp6;
309
310 ret = xt_register_match(AF_INET6, &udp6_matchstruct);
311 if (ret)
312 goto out_unreg_udp;
313
314 return ret;
315
316out_unreg_udp:
317 xt_unregister_match(AF_INET, &tcp_matchstruct);
318out_unreg_tcp6:
319 xt_unregister_match(AF_INET6, &tcp6_matchstruct);
320out_unreg_tcp:
321 xt_unregister_match(AF_INET, &tcp_matchstruct);
322 return ret;
323}
324
325static void __exit fini(void)
326{
327 xt_unregister_match(AF_INET6, &udp6_matchstruct);
328 xt_unregister_match(AF_INET, &udp_matchstruct);
329 xt_unregister_match(AF_INET6, &tcp6_matchstruct);
330 xt_unregister_match(AF_INET, &tcp_matchstruct);
331}
332
333module_init(init);
334module_exit(fini);