[PATCH] Restore tuning capabilities in V4L2 MXB driver
[linux/fpc-iii.git] / net / netfilter / xt_tcpudp.c
blob669c8113cc601a57ea696f0fd965cb16015e8b49
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>
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");
22 #ifdef DEBUG_IP_FIREWALL_USER
23 #define duprintf(format, args...) printk(format , ## args)
24 #else
25 #define duprintf(format, args...)
26 #endif
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)
33 int ret;
35 ret = (port >= min && port <= max) ^ invert;
36 return ret;
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)
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;
51 duprintf("tcp_match: finding option\n");
53 if (!optlen)
54 return invert;
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;
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;
70 return invert;
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)
82 struct tcphdr _tcph, *th;
83 const struct xt_tcp *tcpinfo = matchinfo;
85 if (offset) {
86 /* To quote Alan:
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.
92 if (offset == 1) {
93 duprintf("Dropping evil TCP offset=1 frag.\n");
94 *hotdrop = 1;
96 /* Must not be a fragment. */
97 return 0;
100 #define FWINVTCP(bool,invflg) ((bool) ^ !!(tcpinfo->invflags & invflg))
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;
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;
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;
134 return 1;
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)
145 const struct ipt_ip *ip = info;
146 const struct xt_tcp *tcpinfo = matchinfo;
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);
155 /* Called when user tries to insert an entry of this type. */
156 static int
157 tcp6_checkentry(const char *tablename,
158 const void *entry,
159 void *matchinfo,
160 unsigned int matchsize,
161 unsigned int hook_mask)
163 const struct ip6t_ip6 *ipv6 = entry;
164 const struct xt_tcp *tcpinfo = matchinfo;
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);
174 static int
175 udp_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)
183 struct udphdr _udph, *uh;
184 const struct xt_udp *udpinfo = matchinfo;
186 /* Must not be a fragment. */
187 if (offset)
188 return 0;
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;
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));
207 /* Called when user tries to insert an entry of this type. */
208 static int
209 udp_checkentry(const char *tablename,
210 const void *info,
211 void *matchinfo,
212 unsigned int matchinfosize,
213 unsigned int hook_mask)
215 const struct ipt_ip *ip = info;
216 const struct xt_udp *udpinfo = matchinfo;
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;
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;
229 if (udpinfo->invflags & ~XT_UDP_INV_MASK) {
230 duprintf("ipt_udp: unknown flags %X\n",
231 udpinfo->invflags);
232 return 0;
235 return 1;
238 /* Called when user tries to insert an entry of this type. */
239 static int
240 udp6_checkentry(const char *tablename,
241 const void *entry,
242 void *matchinfo,
243 unsigned int matchinfosize,
244 unsigned int hook_mask)
246 const struct ip6t_ip6 *ipv6 = entry;
247 const struct xt_udp *udpinfo = matchinfo;
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;
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;
260 if (udpinfo->invflags & ~XT_UDP_INV_MASK) {
261 duprintf("ip6t_udp: unknown flags %X\n",
262 udpinfo->invflags);
263 return 0;
266 return 1;
269 static struct xt_match tcp_matchstruct = {
270 .name = "tcp",
271 .match = &tcp_match,
272 .checkentry = &tcp_checkentry,
273 .me = THIS_MODULE,
275 static struct xt_match tcp6_matchstruct = {
276 .name = "tcp",
277 .match = &tcp_match,
278 .checkentry = &tcp6_checkentry,
279 .me = THIS_MODULE,
282 static struct xt_match udp_matchstruct = {
283 .name = "udp",
284 .match = &udp_match,
285 .checkentry = &udp_checkentry,
286 .me = THIS_MODULE,
288 static struct xt_match udp6_matchstruct = {
289 .name = "udp",
290 .match = &udp_match,
291 .checkentry = &udp6_checkentry,
292 .me = THIS_MODULE,
295 static int __init init(void)
297 int ret;
298 ret = xt_register_match(AF_INET, &tcp_matchstruct);
299 if (ret)
300 return ret;
302 ret = xt_register_match(AF_INET6, &tcp6_matchstruct);
303 if (ret)
304 goto out_unreg_tcp;
306 ret = xt_register_match(AF_INET, &udp_matchstruct);
307 if (ret)
308 goto out_unreg_tcp6;
310 ret = xt_register_match(AF_INET6, &udp6_matchstruct);
311 if (ret)
312 goto out_unreg_udp;
314 return ret;
316 out_unreg_udp:
317 xt_unregister_match(AF_INET, &tcp_matchstruct);
318 out_unreg_tcp6:
319 xt_unregister_match(AF_INET6, &tcp6_matchstruct);
320 out_unreg_tcp:
321 xt_unregister_match(AF_INET, &tcp_matchstruct);
322 return ret;
325 static void __exit fini(void)
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);
333 module_init(init);
334 module_exit(fini);