generic 2.6.24 support
[openwrt/openwrt.git] / target / linux / generic-2.6 / patches-2.6.24 / 170-netfilter_chaostables.patch
1 Index: linux-2.6.23/include/linux/netfilter/oot_conntrack.h
2 ===================================================================
3 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
4 +++ linux-2.6.23/include/linux/netfilter/oot_conntrack.h 2007-10-10 13:52:59.000000000 +0800
5 @@ -0,0 +1,5 @@
6 +#if defined(CONFIG_IP_NF_CONNTRACK) || defined(CONFIG_IP_NF_CONNTRACK_MODULE)
7 +# include <linux/netfilter_ipv4/ip_conntrack.h>
8 +#else /* linux-2.6.20+ */
9 +# include <net/netfilter/nf_nat_rule.h>
10 +#endif
11 Index: linux-2.6.23/include/linux/netfilter/oot_trans.h
12 ===================================================================
13 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
14 +++ linux-2.6.23/include/linux/netfilter/oot_trans.h 2007-10-10 13:52:59.000000000 +0800
15 @@ -0,0 +1,14 @@
16 +/* Out of tree workarounds */
17 +#include <linux/version.h>
18 +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 18)
19 +# define HAVE_MATCHINFOSIZE 1
20 +# define HAVE_TARGUSERINFO 1
21 +# define HAVE_TARGINFOSIZE 1
22 +#endif
23 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
24 +# define nfmark mark
25 +#endif
26 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 21)
27 +# define tcp_v4_check(tcph, tcph_sz, s, d, csp) \
28 + tcp_v4_check((tcph_sz), (s), (d), (csp))
29 +#endif
30 Index: linux-2.6.23/include/linux/netfilter/xt_CHAOS.h
31 ===================================================================
32 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
33 +++ linux-2.6.23/include/linux/netfilter/xt_CHAOS.h 2007-10-10 13:52:59.000000000 +0800
34 @@ -0,0 +1,14 @@
35 +#ifndef _LINUX_XT_CHAOS_H
36 +#define _LINUX_XT_CHAOS_H 1
37 +
38 +enum xt_chaos_variant {
39 + XTCHAOS_NORMAL,
40 + XTCHAOS_TARPIT,
41 + XTCHAOS_DELUDE,
42 +};
43 +
44 +struct xt_chaos_info {
45 + enum xt_chaos_variant variant;
46 +};
47 +
48 +#endif /* _LINUX_XT_CHAOS_H */
49 Index: linux-2.6.23/include/linux/netfilter/xt_portscan.h
50 ===================================================================
51 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
52 +++ linux-2.6.23/include/linux/netfilter/xt_portscan.h 2007-10-10 13:52:59.000000000 +0800
53 @@ -0,0 +1,8 @@
54 +#ifndef _LINUX_XT_PORTSCAN_H
55 +#define _LINUX_XT_PORTSCAN_H 1
56 +
57 +struct xt_portscan_info {
58 + unsigned int match_stealth, match_syn, match_cn, match_gr;
59 +};
60 +
61 +#endif /* _LINUX_XT_PORTSCAN_H */
62 Index: linux-2.6.23/net/netfilter/find_match.c
63 ===================================================================
64 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
65 +++ linux-2.6.23/net/netfilter/find_match.c 2007-10-10 13:52:59.000000000 +0800
66 @@ -0,0 +1,39 @@
67 +/*
68 + xt_request_find_match
69 + by Jan Engelhardt <jengelh [at] gmx de>, 2006 - 2007
70 +
71 + Based upon linux-2.6.18.5/net/netfilter/x_tables.c:
72 + Copyright (C) 2006-2006 Harald Welte <laforge@netfilter.org>
73 + This program is free software; you can redistribute it and/or modify
74 + it under the terms of the GNU General Public License version 2 as
75 + published by the Free Software Foundation.
76 +*/
77 +#include <linux/err.h>
78 +#include <linux/netfilter_arp.h>
79 +#include <linux/socket.h>
80 +#include <linux/netfilter/x_tables.h>
81 +
82 +/*
83 + * Yeah this code is sub-optimal, but the function is missing in
84 + * mainline so far. -jengelh
85 + */
86 +static struct xt_match *xt_request_find_match_lo(int af, const char *name,
87 + u8 revision)
88 +{
89 + static const char *const xt_prefix[] = {
90 + [AF_INET] = "ip",
91 + [AF_INET6] = "ip6",
92 + [NF_ARP] = "arp",
93 + };
94 + struct xt_match *match;
95 +
96 + match = try_then_request_module(xt_find_match(af, name, revision),
97 + "%st_%s", xt_prefix[af], name);
98 + if(IS_ERR(match) || match == NULL)
99 + return NULL;
100 +
101 + return match;
102 +}
103 +
104 +/* In case it goes into mainline, let this out-of-tree package compile */
105 +#define xt_request_find_match xt_request_find_match_lo
106 Index: linux-2.6.23/net/netfilter/Kconfig
107 ===================================================================
108 --- linux-2.6.23.orig/net/netfilter/Kconfig 2007-10-10 04:31:38.000000000 +0800
109 +++ linux-2.6.23/net/netfilter/Kconfig 2007-10-10 13:53:04.000000000 +0800
110 @@ -265,6 +265,14 @@
111
112 # alphabetically ordered list of targets
113
114 +config NETFILTER_XT_TARGET_CHAOS
115 + tristate '"CHAOS" target support'
116 + depends on NETFILTER_XTABLES
117 + help
118 + This option adds a `CHAOS' target.
119 +
120 + To compile it as a module, choose M here. If unsure, say N.
121 +
122 config NETFILTER_XT_TARGET_CLASSIFY
123 tristate '"CLASSIFY" target support'
124 depends on NETFILTER_XTABLES
125 @@ -292,6 +300,14 @@
126 <file:Documentation/kbuild/modules.txt>. The module will be called
127 ipt_CONNMARK.ko. If unsure, say `N'.
128
129 +config NETFILTER_XT_TARGET_DELUDE
130 + tristate '"DELUDE" target support'
131 + depends on NETFILTER_XTABLES
132 + help
133 + This option adds a `DELUDE' target.
134 +
135 + To compile it as a module, choose M here. If unsure, say N.
136 +
137 config NETFILTER_XT_TARGET_DSCP
138 tristate '"DSCP" target support'
139 depends on NETFILTER_XTABLES
140 @@ -556,6 +572,14 @@
141
142 To compile it as a module, choose M here. If unsure, say N.
143
144 +config NETFILTER_XT_MATCH_PORTSCAN
145 + tristate '"portscan" match support'
146 + depends on NETFILTER_XTABLES
147 + help
148 + This option adds a 'portscan' match support.
149 +
150 + To compile it as a module, choose M here. If unsure, say N.
151 +
152 config NETFILTER_XT_MATCH_MULTIPORT
153 tristate "Multiple port match support"
154 depends on NETFILTER_XTABLES
155 Index: linux-2.6.23/net/netfilter/Makefile
156 ===================================================================
157 --- linux-2.6.23.orig/net/netfilter/Makefile 2007-10-10 04:31:38.000000000 +0800
158 +++ linux-2.6.23/net/netfilter/Makefile 2007-10-10 13:52:59.000000000 +0800
159 @@ -49,6 +49,8 @@
160 obj-$(CONFIG_NETFILTER_XT_TARGET_SECMARK) += xt_SECMARK.o
161 obj-$(CONFIG_NETFILTER_XT_TARGET_TCPMSS) += xt_TCPMSS.o
162 obj-$(CONFIG_NETFILTER_XT_TARGET_TRACE) += xt_TRACE.o
163 +obj-$(CONFIG_NETFILTER_XT_TARGET_CHAOS) += xt_CHAOS.o
164 +obj-$(CONFIG_NETFILTER_XT_TARGET_DELUDE) += xt_DELUDE.o
165
166 # matches
167 obj-$(CONFIG_NETFILTER_XT_MATCH_COMMENT) += xt_comment.o
168 @@ -79,3 +81,4 @@
169 obj-$(CONFIG_NETFILTER_XT_MATCH_TCPMSS) += xt_tcpmss.o
170 obj-$(CONFIG_NETFILTER_XT_MATCH_TIME) += xt_time.o
171 obj-$(CONFIG_NETFILTER_XT_MATCH_U32) += xt_u32.o
172 +obj-$(CONFIG_NETFILTER_XT_MATCH_PORTSCAN) += xt_portscan.o
173 Index: linux-2.6.23/net/netfilter/xt_CHAOS.c
174 ===================================================================
175 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
176 +++ linux-2.6.23/net/netfilter/xt_CHAOS.c 2007-10-10 13:52:59.000000000 +0800
177 @@ -0,0 +1,205 @@
178 +/*
179 + CHAOS target for netfilter
180 +
181 + Copyright © Jan Engelhardt <jengelh [at] gmx de>, 2006 - 2007
182 + This program is free software; you can redistribute it and/or modify
183 + it under the terms of the GNU General Public License version 2 as
184 + published by the Free Software Foundation.
185 +*/
186 +#include <linux/icmp.h>
187 +#include <linux/in.h>
188 +#include <linux/ip.h>
189 +#include <linux/module.h>
190 +#include <linux/skbuff.h>
191 +#include <linux/stat.h>
192 +#include <linux/netfilter/x_tables.h>
193 +#include <linux/netfilter/xt_tcpudp.h>
194 +#include <linux/netfilter_ipv4/ipt_REJECT.h>
195 +#include <net/ip.h>
196 +#include <linux/netfilter/xt_CHAOS.h>
197 +#include "find_match.c"
198 +#include <linux/netfilter/oot_trans.h>
199 +#define PFX KBUILD_MODNAME ": "
200 +
201 +/* Module parameters */
202 +static unsigned int reject_percentage = ~0U * .01;
203 +static unsigned int delude_percentage = ~0U * .0101;
204 +module_param(reject_percentage, uint, S_IRUGO | S_IWUSR);
205 +module_param(delude_percentage, uint, S_IRUGO | S_IWUSR);
206 +
207 +/* References to other matches/targets */
208 +static struct xt_match *xm_tcp;
209 +static struct xt_target *xt_delude, *xt_reject, *xt_tarpit;
210 +
211 +static int have_delude, have_tarpit;
212 +
213 +/* Static data for other matches/targets */
214 +static const struct ipt_reject_info reject_params = {
215 + .with = ICMP_HOST_UNREACH,
216 +};
217 +
218 +static const struct xt_tcp tcp_params = {
219 + .spts = {0, ~0},
220 + .dpts = {0, ~0},
221 +};
222 +
223 +/* CHAOS functions */
224 +static void xt_chaos_total(const struct xt_chaos_info *info,
225 + struct sk_buff **pskb, const struct net_device *in,
226 + const struct net_device *out, unsigned int hooknum)
227 +{
228 + const int protoff = ip_hdrlen(*pskb);
229 + const int offset = ntohs(ip_hdr(*pskb)->frag_off) & IP_OFFSET;
230 + const struct xt_target *destiny;
231 + bool hotdrop = false;
232 + int ret;
233 +
234 + ret = xm_tcp->match(*pskb, in, out, xm_tcp, &tcp_params,
235 + offset, protoff, &hotdrop);
236 + if(!ret || hotdrop || (unsigned int)net_random() > delude_percentage)
237 + return;
238 +
239 + destiny = (info->variant == XTCHAOS_TARPIT) ? xt_tarpit : xt_delude;
240 +#ifdef HAVE_TARGUSERINFO
241 + destiny->target(pskb, in, out, hooknum, destiny, NULL, NULL);
242 +#else
243 + destiny->target(pskb, in, out, hooknum, destiny, NULL);
244 +#endif
245 + return;
246 +}
247 +
248 +static unsigned int xt_chaos_target(struct sk_buff **pskb,
249 + const struct net_device *in, const struct net_device *out,
250 + unsigned int hooknum, const struct xt_target *target, const void *targinfo
251 +#ifdef HAVE_TARGUSERINFO
252 + ,
253 + void *userinfo
254 +#endif
255 + )
256 +{
257 + /* Equivalent to:
258 + * -A chaos -m statistic --mode random --probability \
259 + * $reject_percentage -j REJECT --reject-with host-unreach;
260 + * -A chaos -p tcp -m statistic --mode random --probability \
261 + * $delude_percentage -j DELUDE;
262 + * -A chaos -j DROP;
263 + */
264 + const struct xt_chaos_info *info = targinfo;
265 +
266 + if((unsigned int)net_random() <= reject_percentage)
267 +#ifdef HAVE_TARGUSERINFO
268 + return xt_reject->target(pskb, in, out, hooknum, target,
269 + &reject_params, userinfo);
270 +#else
271 + return xt_reject->target(pskb, in, out, hooknum, target,
272 + &reject_params);
273 +#endif
274 +
275 + /* TARPIT/DELUDE may not be called from the OUTPUT chain */
276 + if(ip_hdr(*pskb)->protocol == IPPROTO_TCP &&
277 + info->variant != XTCHAOS_NORMAL && hooknum != NF_IP_LOCAL_OUT)
278 + xt_chaos_total(info, pskb, in, out, hooknum);
279 +
280 + return NF_DROP;
281 +}
282 +
283 +static bool xt_chaos_checkentry(const char *tablename, const void *entry,
284 + const struct xt_target *target, void *targinfo,
285 +#ifdef HAVE_TARGINFOSIZE
286 + unsigned int targinfosize,
287 +#endif
288 + unsigned int hook_mask)
289 +{
290 + const struct xt_chaos_info *info = targinfo;
291 + if(info->variant == XTCHAOS_DELUDE && !have_delude) {
292 + printk(KERN_WARNING PFX "Error: Cannot use --delude when "
293 + "DELUDE module not available\n");
294 + return false;
295 + }
296 + if(info->variant == XTCHAOS_TARPIT && !have_tarpit) {
297 + printk(KERN_WARNING PFX "Error: Cannot use --tarpit when "
298 + "TARPIT module not available\n");
299 + return false;
300 + }
301 + return true;
302 +}
303 +
304 +static struct xt_target xt_chaos_info = {
305 + .name = "CHAOS",
306 + .target = xt_chaos_target,
307 + .checkentry = xt_chaos_checkentry,
308 + .table = "filter",
309 + .targetsize = sizeof(struct xt_chaos_info),
310 + .hooks = (1 << NF_IP_LOCAL_IN) | (1 << NF_IP_FORWARD) |
311 + (1 << NF_IP_LOCAL_OUT),
312 + .family = AF_INET,
313 + .me = THIS_MODULE,
314 +};
315 +
316 +static int __init xt_chaos_init(void)
317 +{
318 + int ret = -EINVAL;
319 +
320 + xm_tcp = xt_request_find_match(AF_INET, "tcp", 0);
321 + if(xm_tcp == NULL) {
322 + printk(KERN_WARNING PFX "Error: Could not find or load "
323 + "\"tcp\" match\n");
324 + return -EINVAL;
325 + }
326 +
327 + xt_reject = xt_request_find_target(AF_INET, "REJECT", 0);
328 + if(xt_reject == NULL) {
329 + printk(KERN_WARNING PFX "Error: Could not find or load "
330 + "\"REJECT\" target\n");
331 + goto out2;
332 + }
333 +
334 + xt_tarpit = xt_request_find_target(AF_INET, "TARPIT", 0);
335 + have_tarpit = xt_tarpit != NULL;
336 + if(!have_tarpit)
337 + printk(KERN_WARNING PFX "Warning: Could not find or load "
338 + "\"TARPIT\" target\n");
339 +
340 + xt_delude = xt_request_find_target(AF_INET, "DELUDE", 0);
341 + have_delude = xt_delude != NULL;
342 + if(!have_delude)
343 + printk(KERN_WARNING PFX "Warning: Could not find or load "
344 + "\"DELUDE\" target\n");
345 +
346 + if((ret = xt_register_target(&xt_chaos_info)) != 0) {
347 + printk(KERN_WARNING PFX "xt_register_target returned "
348 + "error %d\n", ret);
349 + goto out3;
350 + }
351 +
352 + return 0;
353 +
354 + out3:
355 + if(have_delude)
356 + module_put(xt_delude->me);
357 + if(have_tarpit)
358 + module_put(xt_tarpit->me);
359 + module_put(xt_reject->me);
360 + out2:
361 + module_put(xm_tcp->me);
362 + return ret;
363 +}
364 +
365 +static void __exit xt_chaos_exit(void)
366 +{
367 + xt_unregister_target(&xt_chaos_info);
368 + module_put(xm_tcp->me);
369 + module_put(xt_reject->me);
370 + if(have_delude)
371 + module_put(xt_delude->me);
372 + if(have_tarpit)
373 + module_put(xt_tarpit->me);
374 + return;
375 +}
376 +
377 +module_init(xt_chaos_init);
378 +module_exit(xt_chaos_exit);
379 +MODULE_AUTHOR("Jan Engelhardt <jengelh@gmx.de>");
380 +MODULE_DESCRIPTION("netfilter CHAOS target");
381 +MODULE_LICENSE("GPL");
382 +MODULE_ALIAS("ipt_CHAOS");
383 Index: linux-2.6.23/net/netfilter/xt_DELUDE.c
384 ===================================================================
385 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
386 +++ linux-2.6.23/net/netfilter/xt_DELUDE.c 2007-10-10 13:52:59.000000000 +0800
387 @@ -0,0 +1,288 @@
388 +/*
389 + DELUDE target
390 + Copyright © Jan Engelhardt <jengelh [at] gmx de>, 2007
391 +
392 + Based upon linux-2.6.18.5/net/ipv4/netfilter/ipt_REJECT.c:
393 + (C) 1999-2001 Paul `Rusty' Russell
394 + (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
395 +
396 + xt_DELUDE acts like REJECT, but does reply with SYN-ACK on SYN.
397 +
398 + This program is free software; you can redistribute it and/or modify
399 + it under the terms of the GNU General Public License version 2 as
400 + published by the Free Software Foundation.
401 +*/
402 +#include <linux/module.h>
403 +#include <linux/skbuff.h>
404 +#include <linux/ip.h>
405 +#include <linux/random.h>
406 +#include <linux/tcp.h>
407 +#include <linux/udp.h>
408 +#include <linux/icmp.h>
409 +#include <net/icmp.h>
410 +#include <net/ip.h>
411 +#include <net/tcp.h>
412 +#include <net/route.h>
413 +#include <net/dst.h>
414 +#include <linux/netfilter_ipv4/ip_tables.h>
415 +#ifdef CONFIG_BRIDGE_NETFILTER
416 +# include <linux/netfilter_bridge.h>
417 +#endif
418 +#include <linux/netfilter/oot_trans.h>
419 +#define PFX KBUILD_MODNAME ": "
420 +
421 +static inline struct rtable *route_reverse(struct sk_buff *skb,
422 + struct tcphdr *tcph, int hook)
423 +{
424 + struct iphdr *iph = ip_hdr(skb);
425 + struct dst_entry *odst;
426 + struct flowi fl = {};
427 + struct rtable *rt;
428 +
429 + /* We don't require ip forwarding to be enabled to be able to
430 + * send a RST reply for bridged traffic. */
431 + if (hook != NF_IP_FORWARD
432 +#ifdef CONFIG_BRIDGE_NETFILTER
433 + || (skb->nf_bridge && skb->nf_bridge->mask & BRNF_BRIDGED)
434 +#endif
435 + ) {
436 + fl.nl_u.ip4_u.daddr = iph->saddr;
437 + if (hook == NF_IP_LOCAL_IN)
438 + fl.nl_u.ip4_u.saddr = iph->daddr;
439 + fl.nl_u.ip4_u.tos = RT_TOS(iph->tos);
440 +
441 + if (ip_route_output_key(&rt, &fl) != 0)
442 + return NULL;
443 + } else {
444 + /* non-local src, find valid iif to satisfy
445 + * rp-filter when calling ip_route_input. */
446 + fl.nl_u.ip4_u.daddr = iph->daddr;
447 + if (ip_route_output_key(&rt, &fl) != 0)
448 + return NULL;
449 +
450 + odst = skb->dst;
451 + if (ip_route_input(skb, iph->saddr, iph->daddr,
452 + RT_TOS(iph->tos), rt->u.dst.dev) != 0) {
453 + dst_release(&rt->u.dst);
454 + return NULL;
455 + }
456 + dst_release(&rt->u.dst);
457 + rt = (struct rtable *)skb->dst;
458 + skb->dst = odst;
459 +
460 + fl.nl_u.ip4_u.daddr = iph->saddr;
461 + fl.nl_u.ip4_u.saddr = iph->daddr;
462 + fl.nl_u.ip4_u.tos = RT_TOS(iph->tos);
463 + }
464 +
465 + if (rt->u.dst.error) {
466 + dst_release(&rt->u.dst);
467 + return NULL;
468 + }
469 +
470 + fl.proto = IPPROTO_TCP;
471 + fl.fl_ip_sport = tcph->dest;
472 + fl.fl_ip_dport = tcph->source;
473 +
474 + xfrm_lookup((struct dst_entry **)&rt, &fl, NULL, 0);
475 +
476 + return rt;
477 +}
478 +
479 +static void send_reset(struct sk_buff *oldskb, int hook)
480 +{
481 + struct sk_buff *nskb;
482 + struct iphdr *iph = ip_hdr(oldskb);
483 + struct tcphdr _otcph, *oth, *tcph;
484 + __be16 tmp_port;
485 + __be32 tmp_addr;
486 + int needs_ack;
487 + unsigned int addr_type;
488 +
489 + /* IP header checks: fragment. */
490 + if (iph->frag_off & htons(IP_OFFSET))
491 + return;
492 +
493 + oth = skb_header_pointer(oldskb, ip_hdrlen(oldskb),
494 + sizeof(_otcph), &_otcph);
495 + if (oth == NULL)
496 + return;
497 +
498 + /* No RST for RST. */
499 + if (oth->rst)
500 + return;
501 +
502 + /* Check checksum */
503 + if (nf_ip_checksum(oldskb, hook, ip_hdrlen(oldskb), IPPROTO_TCP))
504 + return;
505 +
506 + /* We need a linear, writeable skb. We also need to expand
507 + headroom in case hh_len of incoming interface < hh_len of
508 + outgoing interface */
509 + nskb = skb_copy_expand(oldskb, LL_MAX_HEADER, skb_tailroom(oldskb),
510 + GFP_ATOMIC);
511 + if (!nskb)
512 + return;
513 +
514 + /* This packet will not be the same as the other: clear nf fields */
515 + nf_reset(nskb);
516 + nskb->nfmark = 0;
517 + skb_init_secmark(nskb);
518 +
519 + skb_shinfo(nskb)->gso_size = 0;
520 + skb_shinfo(nskb)->gso_segs = 0;
521 + skb_shinfo(nskb)->gso_type = 0;
522 +
523 + tcph = tcp_hdr(nskb);
524 +
525 + /* Swap source and dest */
526 + tmp_addr = ip_hdr(nskb)->saddr;
527 + ip_hdr(nskb)->saddr = ip_hdr(nskb)->daddr;
528 + ip_hdr(nskb)->daddr = tmp_addr;
529 + tmp_port = tcph->source;
530 + tcph->source = tcph->dest;
531 + tcph->dest = tmp_port;
532 +
533 + /* Truncate to length (no data) */
534 + tcph->doff = sizeof(struct tcphdr)/4;
535 + skb_trim(nskb, ip_hdrlen(nskb) + sizeof(struct tcphdr));
536 + ip_hdr(nskb)->tot_len = htons(nskb->len);
537 +
538 + if(oth->syn && !oth->ack && !oth->rst && !oth->fin) {
539 + /* DELUDE essential part */
540 + tcph->ack_seq = htonl(ntohl(oth->seq) + oth->syn + oth->fin +
541 + oldskb->len - ip_hdrlen(oldskb) -
542 + (oth->doff << 2));
543 + tcph->seq = htonl(secure_tcp_sequence_number(
544 + ip_hdr(nskb)->saddr, ip_hdr(nskb)->daddr,
545 + tcph->source, tcph->dest));
546 + tcph->ack = 1;
547 + } else {
548 + if(!tcph->ack) {
549 + needs_ack = 1;
550 + tcph->ack_seq = htonl(ntohl(oth->seq) + oth->syn + oth->fin
551 + + oldskb->len - ip_hdrlen(oldskb)
552 + - (oth->doff<<2));
553 + tcph->seq = 0;
554 + } else {
555 + needs_ack = 0;
556 + tcph->seq = oth->ack_seq;
557 + tcph->ack_seq = 0;
558 + }
559 +
560 + /* Reset flags */
561 + ((u_int8_t *)tcph)[13] = 0;
562 + tcph->rst = 1;
563 + tcph->ack = needs_ack;
564 + }
565 +
566 +
567 + tcph->window = 0;
568 + tcph->urg_ptr = 0;
569 +
570 + /* Adjust TCP checksum */
571 + tcph->check = 0;
572 + tcph->check = tcp_v4_check(tcph, sizeof(struct tcphdr),
573 + ip_hdr(nskb)->saddr,
574 + ip_hdr(nskb)->daddr,
575 + csum_partial((char *)tcph,
576 + sizeof(struct tcphdr), 0));
577 +
578 + /* Set DF, id = 0 */
579 + ip_hdr(nskb)->frag_off = htons(IP_DF);
580 + ip_hdr(nskb)->id = 0;
581 +
582 + addr_type = RTN_UNSPEC;
583 + if (hook != NF_IP_FORWARD
584 +#ifdef CONFIG_BRIDGE_NETFILTER
585 + || (nskb->nf_bridge && nskb->nf_bridge->mask & BRNF_BRIDGED)
586 +#endif
587 + )
588 + addr_type = RTN_LOCAL;
589 +
590 + if (ip_route_me_harder(&nskb, addr_type))
591 + goto free_nskb;
592 +
593 + nskb->ip_summed = CHECKSUM_NONE;
594 +
595 + /* Adjust IP TTL */
596 + ip_hdr(nskb)->ttl = dst_metric(nskb->dst, RTAX_HOPLIMIT);
597 +
598 + /* Adjust IP checksum */
599 + ip_hdr(nskb)->check = 0;
600 + ip_hdr(nskb)->check = ip_fast_csum((unsigned char *)ip_hdr(nskb),
601 + ip_hdr(nskb)->ihl);
602 +
603 + /* "Never happens" */
604 + if (nskb->len > dst_mtu(nskb->dst))
605 + goto free_nskb;
606 +
607 + nf_ct_attach(nskb, oldskb);
608 +
609 + NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, nskb, NULL, nskb->dst->dev,
610 + dst_output);
611 + return;
612 +
613 + free_nskb:
614 + kfree_skb(nskb);
615 +}
616 +
617 +static unsigned int xt_delude_target(struct sk_buff **pskb,
618 + const struct net_device *in, const struct net_device *out,
619 + unsigned int hooknum, const struct xt_target *target, const void *targinfo
620 +#ifdef HAVE_TARGUSERINFO
621 + ,
622 + void *userinfo
623 +#endif
624 + )
625 +{
626 + /* WARNING: This code causes reentry within iptables.
627 + This means that the iptables jump stack is now crap. We
628 + must return an absolute verdict. --RR */
629 + send_reset(*pskb, hooknum);
630 + return NF_DROP;
631 +}
632 +
633 +static bool xt_delude_check(const char *tablename, const void *e_void,
634 + const struct xt_target *target, void *targinfo,
635 +#ifdef HAVE_TARGINFOSIZE
636 + unsigned int targinfosize,
637 +#endif
638 + unsigned int hook_mask)
639 +{
640 + if(hook_mask & ~((1 << NF_IP_LOCAL_IN) | (1 << NF_IP_FORWARD))) {
641 + printk(KERN_WARNING PFX "DELUDE may not be used in chains "
642 + "other than INPUT and FORWARD\n");
643 + return false;
644 + }
645 + return true;
646 +}
647 +
648 +static struct xt_target xt_delude_info = {
649 + .name = "DELUDE",
650 + .target = xt_delude_target,
651 + .checkentry = xt_delude_check,
652 + .table = "filter",
653 + .hooks = (1 << NF_IP_LOCAL_IN) | (1 << NF_IP_FORWARD) |
654 + (1 << NF_IP_LOCAL_OUT),
655 + .proto = IPPROTO_TCP,
656 + .family = AF_INET,
657 + .me = THIS_MODULE,
658 +};
659 +
660 +static int __init xt_delude_init(void)
661 +{
662 + return xt_register_target(&xt_delude_info);
663 +}
664 +
665 +static void __exit xt_delude_exit(void)
666 +{
667 + xt_unregister_target(&xt_delude_info);
668 +}
669 +
670 +module_init(xt_delude_init);
671 +module_exit(xt_delude_exit);
672 +MODULE_AUTHOR("Jan Engelhardt <jengelh@gmx.de>");
673 +MODULE_DESCRIPTION("netfilter DELUDE target");
674 +MODULE_LICENSE("GPL");
675 +MODULE_ALIAS("ipt_DELUDE");
676 Index: linux-2.6.23/net/netfilter/xt_portscan.c
677 ===================================================================
678 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
679 +++ linux-2.6.23/net/netfilter/xt_portscan.c 2007-10-10 13:52:59.000000000 +0800
680 @@ -0,0 +1,272 @@
681 +/*
682 + portscan match for netfilter
683 +
684 + Written by Jan Engelhardt, 2006 - 2007
685 + This program is free software; you can redistribute it and/or modify
686 + it under the terms of the GNU General Public License version 2 as
687 + published by the Free Software Foundation.
688 +*/
689 +#include <linux/in.h>
690 +#include <linux/ip.h>
691 +#include <linux/module.h>
692 +#include <linux/moduleparam.h>
693 +#include <linux/skbuff.h>
694 +#include <linux/stat.h>
695 +#include <linux/tcp.h>
696 +#include <linux/types.h>
697 +#include <linux/version.h>
698 +#include <linux/netfilter/x_tables.h>
699 +#include <linux/netfilter/xt_tcpudp.h>
700 +#include <linux/netfilter/oot_conntrack.h>
701 +#include <linux/netfilter/xt_portscan.h>
702 +#include <linux/netfilter/oot_trans.h>
703 +#define PFX KBUILD_MODNAME ": "
704 +
705 +enum {
706 + TCP_FLAGS_ALL3 = TCP_FLAG_FIN | TCP_FLAG_RST | TCP_FLAG_SYN,
707 + TCP_FLAGS_ALL4 = TCP_FLAGS_ALL3 | TCP_FLAG_ACK,
708 + TCP_FLAGS_ALL6 = TCP_FLAGS_ALL4 | TCP_FLAG_PSH | TCP_FLAG_URG,
709 +};
710 +
711 +/* Module parameters */
712 +static unsigned int
713 + connmark_mask = ~0,
714 + packet_mask = ~0,
715 + mark_seen = 0x9,
716 + mark_synrcv = 0x1,
717 + mark_closed = 0x2,
718 + mark_synscan = 0x3,
719 + mark_estab1 = 0x4,
720 + mark_estab2 = 0x5,
721 + mark_cnscan = 0x6,
722 + mark_grscan = 0x7,
723 + mark_valid = 0x8;
724 +
725 +module_param(connmark_mask, uint, S_IRUGO | S_IWUSR);
726 +module_param(packet_mask, uint, S_IRUGO | S_IWUSR);
727 +module_param(mark_seen, uint, S_IRUGO | S_IWUSR);
728 +module_param(mark_synrcv, uint, S_IRUGO | S_IWUSR);
729 +module_param(mark_closed, uint, S_IRUGO | S_IWUSR);
730 +module_param(mark_synscan, uint, S_IRUGO | S_IWUSR);
731 +module_param(mark_estab1, uint, S_IRUGO | S_IWUSR);
732 +module_param(mark_estab2, uint, S_IRUGO | S_IWUSR);
733 +module_param(mark_cnscan, uint, S_IRUGO | S_IWUSR);
734 +module_param(mark_grscan, uint, S_IRUGO | S_IWUSR);
735 +module_param(mark_valid, uint, S_IRUGO | S_IWUSR);
736 +MODULE_PARM_DESC(connmark_mask, "only set specified bits in connection mark");
737 +MODULE_PARM_DESC(packet_mask, "only set specified bits in packet mark");
738 +MODULE_PARM_DESC(mark_seen, "nfmark value for packet-seen state");
739 +MODULE_PARM_DESC(mark_synrcv, "connmark value for SYN Received state");
740 +MODULE_PARM_DESC(mark_closed, "connmark value for closed state");
741 +MODULE_PARM_DESC(mark_synscan, "connmark value for SYN Scan state");
742 +MODULE_PARM_DESC(mark_estab1, "connmark value for Established-1 state");
743 +MODULE_PARM_DESC(mark_estab2, "connmark value for Established-2 state");
744 +MODULE_PARM_DESC(mark_cnscan, "connmark value for Connect Scan state");
745 +MODULE_PARM_DESC(mark_grscan, "connmark value for Grab Scan state");
746 +MODULE_PARM_DESC(mark_valid, "connmark value for Valid state");
747 +
748 +/* TCP flag functions */
749 +static inline int tflg_ack4(const struct tcphdr *th)
750 +{
751 + return (tcp_flag_word(th) & TCP_FLAGS_ALL4) == TCP_FLAG_ACK;
752 +}
753 +
754 +static inline int tflg_ack6(const struct tcphdr *th)
755 +{
756 + return (tcp_flag_word(th) & TCP_FLAGS_ALL6) == TCP_FLAG_ACK;
757 +}
758 +
759 +static inline int tflg_fin(const struct tcphdr *th)
760 +{
761 + return (tcp_flag_word(th) & TCP_FLAGS_ALL3) == TCP_FLAG_FIN;
762 +}
763 +
764 +static inline int tflg_rst(const struct tcphdr *th)
765 +{
766 + return (tcp_flag_word(th) & TCP_FLAGS_ALL3) == TCP_FLAG_RST;
767 +}
768 +
769 +static inline int tflg_rstack(const struct tcphdr *th)
770 +{
771 + return (tcp_flag_word(th) & TCP_FLAGS_ALL4) ==
772 + (TCP_FLAG_ACK | TCP_FLAG_RST);
773 +}
774 +
775 +static inline int tflg_syn(const struct tcphdr *th)
776 +{
777 + return (tcp_flag_word(th) & TCP_FLAGS_ALL4) == TCP_FLAG_SYN;
778 +}
779 +
780 +static inline int tflg_synack(const struct tcphdr *th)
781 +{
782 + return (tcp_flag_word(th) & TCP_FLAGS_ALL4) ==
783 + (TCP_FLAG_SYN | TCP_FLAG_ACK);
784 +}
785 +
786 +/* portscan functions */
787 +static inline int xt_portscan_stealth(const struct tcphdr *th)
788 +{
789 + /*
790 + * "Connection refused" replies to our own probes must not be matched.
791 + */
792 + if(tflg_rstack(th))
793 + return 0;
794 +
795 + if(tflg_rst(th) && printk_ratelimit()) {
796 + printk(KERN_WARNING PFX "Warning: Pure RST received\n");
797 + return 0;
798 + }
799 +
800 + /*
801 + * -p tcp ! --syn -m conntrack --ctstate INVALID: Looking for non-start
802 + * packets that are not associated with any connection -- this will
803 + * match most scan types (NULL, XMAS, FIN) and ridiculous flag
804 + * combinations (SYN-RST, SYN-FIN, SYN-FIN-RST, FIN-RST, etc.).
805 + */
806 + return !tflg_syn(th);
807 +}
808 +
809 +static inline int xt_portscan_full(int mark, enum ip_conntrack_info ctstate,
810 + int loopback, const struct tcphdr *tcph, int payload_len)
811 +{
812 + if(mark == mark_estab2) {
813 + /*
814 + * -m connmark --mark $ESTAB2
815 + */
816 + if(tflg_ack4(tcph) && payload_len == 0)
817 + return mark; /* keep mark */
818 + else if(tflg_rst(tcph) || tflg_fin(tcph))
819 + return mark_grscan;
820 + else
821 + return mark_valid;
822 + } else if(mark == mark_estab1) {
823 + /*
824 + * -m connmark --mark $ESTAB1
825 + */
826 + if(tflg_rst(tcph) || tflg_fin(tcph))
827 + return mark_cnscan;
828 + else if(!loopback && tflg_ack4(tcph) && payload_len == 0)
829 + return mark_estab2;
830 + else
831 + return mark_valid;
832 + } else if(mark == mark_synrcv) {
833 + /*
834 + * -m connmark --mark $SYN
835 + */
836 + if(loopback && tflg_synack(tcph))
837 + return mark; /* keep mark */
838 + else if(loopback && tflg_rstack(tcph))
839 + return mark_closed;
840 + else if(tflg_ack6(tcph))
841 + return mark_estab1;
842 + else
843 + return mark_synscan;
844 + } else if(ctstate == IP_CT_NEW && tflg_syn(tcph)) {
845 + /*
846 + * -p tcp --syn --ctstate NEW
847 + */
848 + return mark_synrcv;
849 + }
850 + return mark;
851 +}
852 +
853 +static bool xt_portscan_match(const struct sk_buff *skb,
854 + const struct net_device *in, const struct net_device *out,
855 + const struct xt_match *match, const void *matchinfo, int offset,
856 + unsigned int protoff, bool *hotdrop)
857 +{
858 + const struct xt_portscan_info *info = matchinfo;
859 + enum ip_conntrack_info ctstate;
860 + struct nf_conn *ctdata;
861 + const struct tcphdr *tcph;
862 + struct tcphdr tcph_buf;
863 +
864 + tcph = skb_header_pointer(skb, protoff, sizeof(tcph_buf), &tcph_buf);
865 + if(tcph == NULL)
866 + return false;
867 +
868 + /* Check for invalid packets: -m conntrack --ctstate INVALID */
869 + if((ctdata = nf_ct_get(skb, &ctstate)) == NULL) {
870 + if(info->match_stealth)
871 + return xt_portscan_stealth(tcph);
872 + /*
873 + * If @ctdata is NULL, we cannot match the other scan
874 + * types, return.
875 + */
876 + return false;
877 + }
878 +
879 + /*
880 + * If -m portscan was previously applied to this packet, the rules we
881 + * simulate must not be run through again. And for speedup, do not call
882 + * it either when the connection is already VALID.
883 + */
884 + if((ctdata->mark & connmark_mask) == mark_valid ||
885 + (skb->nfmark & packet_mask) != mark_seen)
886 + {
887 + unsigned int n;
888 + n = xt_portscan_full(ctdata->mark & connmark_mask, ctstate,
889 + in == &loopback_dev, tcph,
890 + skb->len - protoff - 4 * tcph->doff);
891 +
892 + ctdata->mark = (ctdata->mark & ~connmark_mask) | n;
893 + ((struct sk_buff *)skb)->nfmark =
894 + (skb->nfmark & ~packet_mask) | mark_seen;
895 + }
896 +
897 + return (info->match_syn && ctdata->mark == mark_synscan) ||
898 + (info->match_cn && ctdata->mark == mark_cnscan) ||
899 + (info->match_gr && ctdata->mark == mark_grscan);
900 +}
901 +
902 +static bool xt_portscan_checkentry(const char *tablename, const void *entry,
903 + const struct xt_match *match, void *matchinfo,
904 +#ifdef HAVE_MATCHINFOSIZE
905 + unsigned int matchinfosize,
906 +#endif
907 + unsigned int hook_mask)
908 +{
909 + const struct xt_portscan_info *info = matchinfo;
910 +#ifdef HAVE_MATCHINFOSIZE
911 + if(matchinfosize != XT_ALIGN(sizeof(struct xt_portscan_info))) {
912 + printk(KERN_WARNING PFX "matchinfosize %u != %Zu\n",
913 + matchinfosize,
914 + XT_ALIGN(sizeof(struct xt_portscan_info)));
915 + return false;
916 + }
917 +#endif
918 + if((info->match_stealth & ~1) || (info->match_syn & ~1) ||
919 + (info->match_cn & ~1) || (info->match_gr & ~1)) {
920 + printk(KERN_WARNING PFX "Invalid flags\n");
921 + return false;
922 + }
923 + return true;
924 +}
925 +
926 +static struct xt_match xt_portscan = {
927 + .name = "portscan",
928 + .match = xt_portscan_match,
929 + .checkentry = xt_portscan_checkentry,
930 + .matchsize = sizeof(struct xt_portscan_info),
931 + .proto = IPPROTO_TCP,
932 + .family = AF_INET,
933 + .me = THIS_MODULE,
934 +};
935 +
936 +static int __init xt_portscan_init(void)
937 +{
938 + return xt_register_match(&xt_portscan);
939 +}
940 +
941 +static void __exit xt_portscan_exit(void)
942 +{
943 + xt_unregister_match(&xt_portscan);
944 + return;
945 +}
946 +
947 +module_init(xt_portscan_init);
948 +module_exit(xt_portscan_exit);
949 +MODULE_AUTHOR("Jan Engelhardt <jengelh@gmx.de>");
950 +MODULE_DESCRIPTION("netfilter portscan match module");
951 +MODULE_LICENSE("GPL");
952 +MODULE_ALIAS("ipt_portscan");
953 Index: linux-2.6.23/drivers/char/random.c
954 ===================================================================
955 --- linux-2.6.23.orig/drivers/char/random.c 2007-10-10 04:31:38.000000000 +0800
956 +++ linux-2.6.23/drivers/char/random.c 2007-10-10 13:52:59.000000000 +0800
957 @@ -1564,6 +1564,8 @@
958 return seq;
959 }
960
961 +EXPORT_SYMBOL(secure_tcp_sequence_number);
962 +
963 /* Generate secure starting point for ephemeral IPV4 transport port search */
964 u32 secure_ipv4_port_ephemeral(__be32 saddr, __be32 daddr, __be16 dport)
965 {