netfilter: xtables: move extension arguments into compound structure (1/6)
[deliverable/linux.git] / net / netfilter / xt_tcpudp.c
CommitLineData
2e4e6a17
HW
1#include <linux/types.h>
2#include <linux/module.h>
3#include <net/ip.h>
37d8dc82 4#include <linux/ipv6.h>
2e4e6a17
HW
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
2ae15b64 13MODULE_DESCRIPTION("Xtables: TCP, UDP and UDP-Lite match");
2e4e6a17
HW
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 */
1d93a9cb
JE
30static inline bool
31port_match(u_int16_t min, u_int16_t max, u_int16_t port, bool invert)
2e4e6a17 32{
1d93a9cb 33 return (port >= min && port <= max) ^ invert;
2e4e6a17
HW
34}
35
1d93a9cb 36static bool
2e4e6a17
HW
37tcp_find_option(u_int8_t option,
38 const struct sk_buff *skb,
39 unsigned int protoff,
40 unsigned int optlen,
1d93a9cb 41 bool invert,
cff533ac 42 bool *hotdrop)
2e4e6a17
HW
43{
44 /* tcp.doff is only 4 bits, ie. max 15 * 4 bytes */
3cf93c96
JE
45 const u_int8_t *op;
46 u_int8_t _opt[60 - sizeof(struct tcphdr)];
2e4e6a17
HW
47 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) {
cff533ac 58 *hotdrop = true;
1d93a9cb 59 return false;
2e4e6a17
HW
60 }
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
f7108a20 71static bool tcp_mt(const struct sk_buff *skb, const struct xt_match_param *par)
2e4e6a17 72{
3cf93c96
JE
73 const struct tcphdr *th;
74 struct tcphdr _tcph;
f7108a20 75 const struct xt_tcp *tcpinfo = par->matchinfo;
2e4e6a17 76
f7108a20 77 if (par->fragoff != 0) {
2e4e6a17
HW
78 /* To quote Alan:
79
80 Don't allow a fragment of TCP 8 bytes in. Nobody normal
81 causes this. Its a cracker trying to break in by doing a
82 flag overwrite to pass the direction checks.
83 */
f7108a20 84 if (par->fragoff == 1) {
2e4e6a17 85 duprintf("Dropping evil TCP offset=1 frag.\n");
f7108a20 86 *par->hotdrop = true;
2e4e6a17
HW
87 }
88 /* Must not be a fragment. */
1d93a9cb 89 return false;
2e4e6a17
HW
90 }
91
7c4e36bc 92#define FWINVTCP(bool, invflg) ((bool) ^ !!(tcpinfo->invflags & (invflg)))
2e4e6a17 93
f7108a20 94 th = skb_header_pointer(skb, par->thoff, sizeof(_tcph), &_tcph);
2e4e6a17
HW
95 if (th == NULL) {
96 /* We've been asked to examine this packet, and we
97 can't. Hence, no choice but to drop. */
98 duprintf("Dropping evil TCP offset=0 tinygram.\n");
f7108a20 99 *par->hotdrop = true;
1d93a9cb 100 return false;
2e4e6a17
HW
101 }
102
103 if (!port_match(tcpinfo->spts[0], tcpinfo->spts[1],
104 ntohs(th->source),
105 !!(tcpinfo->invflags & XT_TCP_INV_SRCPT)))
1d93a9cb 106 return false;
2e4e6a17
HW
107 if (!port_match(tcpinfo->dpts[0], tcpinfo->dpts[1],
108 ntohs(th->dest),
109 !!(tcpinfo->invflags & XT_TCP_INV_DSTPT)))
1d93a9cb 110 return false;
2e4e6a17
HW
111 if (!FWINVTCP((((unsigned char *)th)[13] & tcpinfo->flg_mask)
112 == tcpinfo->flg_cmp,
113 XT_TCP_INV_FLAGS))
1d93a9cb 114 return false;
2e4e6a17
HW
115 if (tcpinfo->option) {
116 if (th->doff * 4 < sizeof(_tcph)) {
f7108a20 117 *par->hotdrop = true;
1d93a9cb 118 return false;
2e4e6a17 119 }
f7108a20 120 if (!tcp_find_option(tcpinfo->option, skb, par->thoff,
2e4e6a17
HW
121 th->doff*4 - sizeof(_tcph),
122 tcpinfo->invflags & XT_TCP_INV_OPTION,
f7108a20 123 par->hotdrop))
1d93a9cb 124 return false;
2e4e6a17 125 }
1d93a9cb 126 return true;
2e4e6a17
HW
127}
128
129/* Called when user tries to insert an entry of this type. */
ccb79bdc 130static bool
d3c5ee6d
JE
131tcp_mt_check(const char *tablename, const void *info,
132 const struct xt_match *match, void *matchinfo,
133 unsigned int hook_mask)
2e4e6a17 134{
2e4e6a17
HW
135 const struct xt_tcp *tcpinfo = matchinfo;
136
5d04bff0
PM
137 /* Must specify no unknown invflags */
138 return !(tcpinfo->invflags & ~XT_TCP_INV_MASK);
2e4e6a17
HW
139}
140
f7108a20 141static bool udp_mt(const struct sk_buff *skb, const struct xt_match_param *par)
2e4e6a17 142{
3cf93c96
JE
143 const struct udphdr *uh;
144 struct udphdr _udph;
f7108a20 145 const struct xt_udp *udpinfo = par->matchinfo;
2e4e6a17
HW
146
147 /* Must not be a fragment. */
f7108a20 148 if (par->fragoff != 0)
1d93a9cb 149 return false;
2e4e6a17 150
f7108a20 151 uh = skb_header_pointer(skb, par->thoff, sizeof(_udph), &_udph);
2e4e6a17
HW
152 if (uh == NULL) {
153 /* We've been asked to examine this packet, and we
154 can't. Hence, no choice but to drop. */
155 duprintf("Dropping evil UDP tinygram.\n");
f7108a20 156 *par->hotdrop = true;
1d93a9cb 157 return false;
2e4e6a17
HW
158 }
159
160 return port_match(udpinfo->spts[0], udpinfo->spts[1],
161 ntohs(uh->source),
162 !!(udpinfo->invflags & XT_UDP_INV_SRCPT))
163 && port_match(udpinfo->dpts[0], udpinfo->dpts[1],
164 ntohs(uh->dest),
165 !!(udpinfo->invflags & XT_UDP_INV_DSTPT));
166}
167
168/* Called when user tries to insert an entry of this type. */
ccb79bdc 169static bool
d3c5ee6d
JE
170udp_mt_check(const char *tablename, const void *info,
171 const struct xt_match *match, void *matchinfo,
172 unsigned int hook_mask)
2e4e6a17 173{
a289d70d 174 const struct xt_udp *udpinfo = matchinfo;
2e4e6a17 175
5d04bff0
PM
176 /* Must specify no unknown invflags */
177 return !(udpinfo->invflags & ~XT_UDP_INV_MASK);
2e4e6a17
HW
178}
179
d3c5ee6d 180static struct xt_match tcpudp_mt_reg[] __read_mostly = {
4470bbc7
PM
181 {
182 .name = "tcp",
ee999d8b 183 .family = NFPROTO_IPV4,
d3c5ee6d
JE
184 .checkentry = tcp_mt_check,
185 .match = tcp_mt,
4470bbc7
PM
186 .matchsize = sizeof(struct xt_tcp),
187 .proto = IPPROTO_TCP,
188 .me = THIS_MODULE,
189 },
190 {
191 .name = "tcp",
ee999d8b 192 .family = NFPROTO_IPV6,
d3c5ee6d
JE
193 .checkentry = tcp_mt_check,
194 .match = tcp_mt,
4470bbc7
PM
195 .matchsize = sizeof(struct xt_tcp),
196 .proto = IPPROTO_TCP,
197 .me = THIS_MODULE,
198 },
199 {
200 .name = "udp",
ee999d8b 201 .family = NFPROTO_IPV4,
d3c5ee6d
JE
202 .checkentry = udp_mt_check,
203 .match = udp_mt,
4470bbc7
PM
204 .matchsize = sizeof(struct xt_udp),
205 .proto = IPPROTO_UDP,
206 .me = THIS_MODULE,
207 },
208 {
209 .name = "udp",
ee999d8b 210 .family = NFPROTO_IPV6,
d3c5ee6d
JE
211 .checkentry = udp_mt_check,
212 .match = udp_mt,
4470bbc7
PM
213 .matchsize = sizeof(struct xt_udp),
214 .proto = IPPROTO_UDP,
215 .me = THIS_MODULE,
216 },
ba4e58ec
GR
217 {
218 .name = "udplite",
ee999d8b 219 .family = NFPROTO_IPV4,
d3c5ee6d
JE
220 .checkentry = udp_mt_check,
221 .match = udp_mt,
ba4e58ec
GR
222 .matchsize = sizeof(struct xt_udp),
223 .proto = IPPROTO_UDPLITE,
224 .me = THIS_MODULE,
225 },
226 {
227 .name = "udplite",
ee999d8b 228 .family = NFPROTO_IPV6,
d3c5ee6d
JE
229 .checkentry = udp_mt_check,
230 .match = udp_mt,
ba4e58ec
GR
231 .matchsize = sizeof(struct xt_udp),
232 .proto = IPPROTO_UDPLITE,
233 .me = THIS_MODULE,
234 },
2e4e6a17
HW
235};
236
d3c5ee6d 237static int __init tcpudp_mt_init(void)
2e4e6a17 238{
d3c5ee6d 239 return xt_register_matches(tcpudp_mt_reg, ARRAY_SIZE(tcpudp_mt_reg));
2e4e6a17
HW
240}
241
d3c5ee6d 242static void __exit tcpudp_mt_exit(void)
2e4e6a17 243{
d3c5ee6d 244 xt_unregister_matches(tcpudp_mt_reg, ARRAY_SIZE(tcpudp_mt_reg));
2e4e6a17
HW
245}
246
d3c5ee6d
JE
247module_init(tcpudp_mt_init);
248module_exit(tcpudp_mt_exit);
This page took 0.350005 seconds and 5 git commands to generate.