]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/netfilter/xt_tcpudp.c
netfilter: xtables: substitute temporary defines by final name
[net-next-2.6.git] / net / netfilter / xt_tcpudp.c
CommitLineData
be91fd5e 1#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2e4e6a17
HW
2#include <linux/types.h>
3#include <linux/module.h>
4#include <net/ip.h>
37d8dc82 5#include <linux/ipv6.h>
2e4e6a17
HW
6#include <net/ipv6.h>
7#include <net/tcp.h>
8#include <net/udp.h>
9#include <linux/netfilter/x_tables.h>
10#include <linux/netfilter/xt_tcpudp.h>
11#include <linux/netfilter_ipv4/ip_tables.h>
12#include <linux/netfilter_ipv6/ip6_tables.h>
13
2ae15b64 14MODULE_DESCRIPTION("Xtables: TCP, UDP and UDP-Lite match");
2e4e6a17
HW
15MODULE_LICENSE("GPL");
16MODULE_ALIAS("xt_tcp");
17MODULE_ALIAS("xt_udp");
18MODULE_ALIAS("ipt_udp");
19MODULE_ALIAS("ipt_tcp");
20MODULE_ALIAS("ip6t_udp");
21MODULE_ALIAS("ip6t_tcp");
22
2e4e6a17 23/* Returns 1 if the port is matched by the range, 0 otherwise */
1d93a9cb
JE
24static inline bool
25port_match(u_int16_t min, u_int16_t max, u_int16_t port, bool invert)
2e4e6a17 26{
1d93a9cb 27 return (port >= min && port <= max) ^ invert;
2e4e6a17
HW
28}
29
1d93a9cb 30static bool
2e4e6a17
HW
31tcp_find_option(u_int8_t option,
32 const struct sk_buff *skb,
33 unsigned int protoff,
34 unsigned int optlen,
1d93a9cb 35 bool invert,
cff533ac 36 bool *hotdrop)
2e4e6a17
HW
37{
38 /* tcp.doff is only 4 bits, ie. max 15 * 4 bytes */
3cf93c96
JE
39 const u_int8_t *op;
40 u_int8_t _opt[60 - sizeof(struct tcphdr)];
2e4e6a17
HW
41 unsigned int i;
42
be91fd5e 43 pr_debug("finding option\n");
2e4e6a17
HW
44
45 if (!optlen)
46 return invert;
47
48 /* If we don't have the whole header, drop packet. */
49 op = skb_header_pointer(skb, protoff + sizeof(struct tcphdr),
50 optlen, _opt);
51 if (op == NULL) {
cff533ac 52 *hotdrop = true;
1d93a9cb 53 return false;
2e4e6a17
HW
54 }
55
56 for (i = 0; i < optlen; ) {
57 if (op[i] == option) return !invert;
58 if (op[i] < 2) i++;
59 else i += op[i+1]?:1;
60 }
61
62 return invert;
63}
64
4b560b44
JE
65static bool tcp_mt(const struct sk_buff *skb,
66 const struct xt_action_param *par)
2e4e6a17 67{
3cf93c96
JE
68 const struct tcphdr *th;
69 struct tcphdr _tcph;
f7108a20 70 const struct xt_tcp *tcpinfo = par->matchinfo;
2e4e6a17 71
f7108a20 72 if (par->fragoff != 0) {
2e4e6a17
HW
73 /* To quote Alan:
74
75 Don't allow a fragment of TCP 8 bytes in. Nobody normal
76 causes this. Its a cracker trying to break in by doing a
77 flag overwrite to pass the direction checks.
78 */
f7108a20 79 if (par->fragoff == 1) {
be91fd5e 80 pr_debug("Dropping evil TCP offset=1 frag.\n");
f7108a20 81 *par->hotdrop = true;
2e4e6a17
HW
82 }
83 /* Must not be a fragment. */
1d93a9cb 84 return false;
2e4e6a17
HW
85 }
86
7c4e36bc 87#define FWINVTCP(bool, invflg) ((bool) ^ !!(tcpinfo->invflags & (invflg)))
2e4e6a17 88
f7108a20 89 th = skb_header_pointer(skb, par->thoff, sizeof(_tcph), &_tcph);
2e4e6a17
HW
90 if (th == NULL) {
91 /* We've been asked to examine this packet, and we
92 can't. Hence, no choice but to drop. */
be91fd5e 93 pr_debug("Dropping evil TCP offset=0 tinygram.\n");
f7108a20 94 *par->hotdrop = true;
1d93a9cb 95 return false;
2e4e6a17
HW
96 }
97
98 if (!port_match(tcpinfo->spts[0], tcpinfo->spts[1],
99 ntohs(th->source),
100 !!(tcpinfo->invflags & XT_TCP_INV_SRCPT)))
1d93a9cb 101 return false;
2e4e6a17
HW
102 if (!port_match(tcpinfo->dpts[0], tcpinfo->dpts[1],
103 ntohs(th->dest),
104 !!(tcpinfo->invflags & XT_TCP_INV_DSTPT)))
1d93a9cb 105 return false;
2e4e6a17
HW
106 if (!FWINVTCP((((unsigned char *)th)[13] & tcpinfo->flg_mask)
107 == tcpinfo->flg_cmp,
108 XT_TCP_INV_FLAGS))
1d93a9cb 109 return false;
2e4e6a17
HW
110 if (tcpinfo->option) {
111 if (th->doff * 4 < sizeof(_tcph)) {
f7108a20 112 *par->hotdrop = true;
1d93a9cb 113 return false;
2e4e6a17 114 }
f7108a20 115 if (!tcp_find_option(tcpinfo->option, skb, par->thoff,
2e4e6a17
HW
116 th->doff*4 - sizeof(_tcph),
117 tcpinfo->invflags & XT_TCP_INV_OPTION,
f7108a20 118 par->hotdrop))
1d93a9cb 119 return false;
2e4e6a17 120 }
1d93a9cb 121 return true;
2e4e6a17
HW
122}
123
b0f38452 124static int tcp_mt_check(const struct xt_mtchk_param *par)
2e4e6a17 125{
9b4fce7a 126 const struct xt_tcp *tcpinfo = par->matchinfo;
2e4e6a17 127
5d04bff0 128 /* Must specify no unknown invflags */
bd414ee6 129 return (tcpinfo->invflags & ~XT_TCP_INV_MASK) ? -EINVAL : 0;
2e4e6a17
HW
130}
131
4b560b44
JE
132static bool udp_mt(const struct sk_buff *skb,
133 const struct xt_action_param *par)
2e4e6a17 134{
3cf93c96
JE
135 const struct udphdr *uh;
136 struct udphdr _udph;
f7108a20 137 const struct xt_udp *udpinfo = par->matchinfo;
2e4e6a17
HW
138
139 /* Must not be a fragment. */
f7108a20 140 if (par->fragoff != 0)
1d93a9cb 141 return false;
2e4e6a17 142
f7108a20 143 uh = skb_header_pointer(skb, par->thoff, sizeof(_udph), &_udph);
2e4e6a17
HW
144 if (uh == NULL) {
145 /* We've been asked to examine this packet, and we
146 can't. Hence, no choice but to drop. */
be91fd5e 147 pr_debug("Dropping evil UDP tinygram.\n");
f7108a20 148 *par->hotdrop = true;
1d93a9cb 149 return false;
2e4e6a17
HW
150 }
151
152 return port_match(udpinfo->spts[0], udpinfo->spts[1],
153 ntohs(uh->source),
154 !!(udpinfo->invflags & XT_UDP_INV_SRCPT))
155 && port_match(udpinfo->dpts[0], udpinfo->dpts[1],
156 ntohs(uh->dest),
157 !!(udpinfo->invflags & XT_UDP_INV_DSTPT));
158}
159
b0f38452 160static int udp_mt_check(const struct xt_mtchk_param *par)
2e4e6a17 161{
9b4fce7a 162 const struct xt_udp *udpinfo = par->matchinfo;
2e4e6a17 163
5d04bff0 164 /* Must specify no unknown invflags */
bd414ee6 165 return (udpinfo->invflags & ~XT_UDP_INV_MASK) ? -EINVAL : 0;
2e4e6a17
HW
166}
167
d3c5ee6d 168static struct xt_match tcpudp_mt_reg[] __read_mostly = {
4470bbc7
PM
169 {
170 .name = "tcp",
ee999d8b 171 .family = NFPROTO_IPV4,
d3c5ee6d
JE
172 .checkentry = tcp_mt_check,
173 .match = tcp_mt,
4470bbc7
PM
174 .matchsize = sizeof(struct xt_tcp),
175 .proto = IPPROTO_TCP,
176 .me = THIS_MODULE,
177 },
178 {
179 .name = "tcp",
ee999d8b 180 .family = NFPROTO_IPV6,
d3c5ee6d
JE
181 .checkentry = tcp_mt_check,
182 .match = tcp_mt,
4470bbc7
PM
183 .matchsize = sizeof(struct xt_tcp),
184 .proto = IPPROTO_TCP,
185 .me = THIS_MODULE,
186 },
187 {
188 .name = "udp",
ee999d8b 189 .family = NFPROTO_IPV4,
d3c5ee6d
JE
190 .checkentry = udp_mt_check,
191 .match = udp_mt,
4470bbc7
PM
192 .matchsize = sizeof(struct xt_udp),
193 .proto = IPPROTO_UDP,
194 .me = THIS_MODULE,
195 },
196 {
197 .name = "udp",
ee999d8b 198 .family = NFPROTO_IPV6,
d3c5ee6d
JE
199 .checkentry = udp_mt_check,
200 .match = udp_mt,
4470bbc7
PM
201 .matchsize = sizeof(struct xt_udp),
202 .proto = IPPROTO_UDP,
203 .me = THIS_MODULE,
204 },
ba4e58ec
GR
205 {
206 .name = "udplite",
ee999d8b 207 .family = NFPROTO_IPV4,
d3c5ee6d
JE
208 .checkentry = udp_mt_check,
209 .match = udp_mt,
ba4e58ec
GR
210 .matchsize = sizeof(struct xt_udp),
211 .proto = IPPROTO_UDPLITE,
212 .me = THIS_MODULE,
213 },
214 {
215 .name = "udplite",
ee999d8b 216 .family = NFPROTO_IPV6,
d3c5ee6d
JE
217 .checkentry = udp_mt_check,
218 .match = udp_mt,
ba4e58ec
GR
219 .matchsize = sizeof(struct xt_udp),
220 .proto = IPPROTO_UDPLITE,
221 .me = THIS_MODULE,
222 },
2e4e6a17
HW
223};
224
d3c5ee6d 225static int __init tcpudp_mt_init(void)
2e4e6a17 226{
d3c5ee6d 227 return xt_register_matches(tcpudp_mt_reg, ARRAY_SIZE(tcpudp_mt_reg));
2e4e6a17
HW
228}
229
d3c5ee6d 230static void __exit tcpudp_mt_exit(void)
2e4e6a17 231{
d3c5ee6d 232 xt_unregister_matches(tcpudp_mt_reg, ARRAY_SIZE(tcpudp_mt_reg));
2e4e6a17
HW
233}
234
d3c5ee6d
JE
235module_init(tcpudp_mt_init);
236module_exit(tcpudp_mt_exit);