9d01f07ceb652d190c32f3ae5b91610104c50233
[safe/jmp/linux-2.6] / net / netfilter / xt_tcpudp.c
1 #include <linux/types.h>
2 #include <linux/module.h>
3 #include <net/ip.h>
4 #include <linux/ipv6.h>
5 #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
13 MODULE_DESCRIPTION("x_tables match for TCP and UDP, supports IPv4 and IPv6");
14 MODULE_LICENSE("GPL");
15 MODULE_ALIAS("xt_tcp");
16 MODULE_ALIAS("xt_udp");
17 MODULE_ALIAS("ipt_udp");
18 MODULE_ALIAS("ipt_tcp");
19 MODULE_ALIAS("ip6t_udp");
20 MODULE_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 */
30 static inline int
31 port_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
39 static int
40 tcp_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
73 static int
74 tcp_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. */
138 static int
139 tcp_checkentry(const char *tablename,
140                const void *info,
141                void *matchinfo,
142                unsigned int matchsize,
143                unsigned int hook_mask)
144 {
145         const struct xt_tcp *tcpinfo = matchinfo;
146
147         /* Must specify no unknown invflags */
148         return !(tcpinfo->invflags & ~XT_TCP_INV_MASK);
149 }
150
151 static int
152 udp_match(const struct sk_buff *skb,
153           const struct net_device *in,
154           const struct net_device *out,
155           const void *matchinfo,
156           int offset,
157           unsigned int protoff,
158           int *hotdrop)
159 {
160         struct udphdr _udph, *uh;
161         const struct xt_udp *udpinfo = matchinfo;
162
163         /* Must not be a fragment. */
164         if (offset)
165                 return 0;
166
167         uh = skb_header_pointer(skb, protoff, sizeof(_udph), &_udph);
168         if (uh == NULL) {
169                 /* We've been asked to examine this packet, and we
170                    can't.  Hence, no choice but to drop. */
171                 duprintf("Dropping evil UDP tinygram.\n");
172                 *hotdrop = 1;
173                 return 0;
174         }
175
176         return port_match(udpinfo->spts[0], udpinfo->spts[1],
177                           ntohs(uh->source),
178                           !!(udpinfo->invflags & XT_UDP_INV_SRCPT))
179                 && port_match(udpinfo->dpts[0], udpinfo->dpts[1],
180                               ntohs(uh->dest),
181                               !!(udpinfo->invflags & XT_UDP_INV_DSTPT));
182 }
183
184 /* Called when user tries to insert an entry of this type. */
185 static int
186 udp_checkentry(const char *tablename,
187                const void *info,
188                void *matchinfo,
189                unsigned int matchsize,
190                unsigned int hook_mask)
191 {
192         const struct xt_tcp *udpinfo = matchinfo;
193
194         /* Must specify no unknown invflags */
195         return !(udpinfo->invflags & ~XT_UDP_INV_MASK);
196 }
197
198 static struct xt_match tcp_matchstruct = {
199         .name           = "tcp",
200         .match          = tcp_match,
201         .matchsize      = sizeof(struct xt_tcp),
202         .proto          = IPPROTO_TCP,
203         .checkentry     = tcp_checkentry,
204         .me             = THIS_MODULE,
205 };
206
207 static struct xt_match tcp6_matchstruct = {
208         .name           = "tcp",
209         .match          = tcp_match,
210         .matchsize      = sizeof(struct xt_tcp),
211         .proto          = IPPROTO_TCP,
212         .checkentry     = tcp_checkentry,
213         .me             = THIS_MODULE,
214 };
215
216 static struct xt_match udp_matchstruct = {
217         .name           = "udp",
218         .match          = udp_match,
219         .matchsize      = sizeof(struct xt_udp),
220         .proto          = IPPROTO_UDP,
221         .checkentry     = udp_checkentry,
222         .me             = THIS_MODULE,
223 };
224 static struct xt_match udp6_matchstruct = {
225         .name           = "udp",
226         .match          = udp_match,
227         .matchsize      = sizeof(struct xt_udp),
228         .proto          = IPPROTO_UDP,
229         .checkentry     = udp_checkentry,
230         .me             = THIS_MODULE,
231 };
232
233 static int __init init(void)
234 {
235         int ret;
236         ret = xt_register_match(AF_INET, &tcp_matchstruct);
237         if (ret)
238                 return ret;
239
240         ret = xt_register_match(AF_INET6, &tcp6_matchstruct);
241         if (ret)
242                 goto out_unreg_tcp;
243
244         ret = xt_register_match(AF_INET, &udp_matchstruct);
245         if (ret)
246                 goto out_unreg_tcp6;
247         
248         ret = xt_register_match(AF_INET6, &udp6_matchstruct);
249         if (ret)
250                 goto out_unreg_udp;
251
252         return ret;
253
254 out_unreg_udp:
255         xt_unregister_match(AF_INET, &tcp_matchstruct);
256 out_unreg_tcp6:
257         xt_unregister_match(AF_INET6, &tcp6_matchstruct);
258 out_unreg_tcp:
259         xt_unregister_match(AF_INET, &tcp_matchstruct);
260         return ret;
261 }
262
263 static void __exit fini(void)
264 {
265         xt_unregister_match(AF_INET6, &udp6_matchstruct);
266         xt_unregister_match(AF_INET, &udp_matchstruct);
267         xt_unregister_match(AF_INET6, &tcp6_matchstruct);
268         xt_unregister_match(AF_INET, &tcp_matchstruct);
269 }
270
271 module_init(init);
272 module_exit(fini);