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
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>
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
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
23 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
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))
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
35 +#ifndef _LINUX_XT_CHAOS_H
36 +#define _LINUX_XT_CHAOS_H 1
38 +enum xt_chaos_variant {
44 +struct xt_chaos_info {
45 + enum xt_chaos_variant variant;
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
54 +#ifndef _LINUX_XT_PORTSCAN_H
55 +#define _LINUX_XT_PORTSCAN_H 1
57 +struct xt_portscan_info {
58 + unsigned int match_stealth, match_syn, match_cn, match_gr;
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
68 + xt_request_find_match
69 + by Jan Engelhardt <jengelh [at] gmx de>, 2006 - 2007
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.
77 +#include <linux/err.h>
78 +#include <linux/netfilter_arp.h>
79 +#include <linux/socket.h>
80 +#include <linux/netfilter/x_tables.h>
83 + * Yeah this code is sub-optimal, but the function is missing in
84 + * mainline so far. -jengelh
86 +static struct xt_match *xt_request_find_match_lo(int af, const char *name,
89 + static const char *const xt_prefix[] = {
94 + struct xt_match *match;
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)
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
112 # alphabetically ordered list of targets
114 +config NETFILTER_XT_TARGET_CHAOS
115 + tristate '"CHAOS" target support'
116 + depends on NETFILTER_XTABLES
118 + This option adds a `CHAOS' target.
120 + To compile it as a module, choose M here. If unsure, say N.
122 config NETFILTER_XT_TARGET_CLASSIFY
123 tristate '"CLASSIFY" target support'
124 depends on NETFILTER_XTABLES
126 <file:Documentation/kbuild/modules.txt>. The module will be called
127 ipt_CONNMARK.ko. If unsure, say `N'.
129 +config NETFILTER_XT_TARGET_DELUDE
130 + tristate '"DELUDE" target support'
131 + depends on NETFILTER_XTABLES
133 + This option adds a `DELUDE' target.
135 + To compile it as a module, choose M here. If unsure, say N.
137 config NETFILTER_XT_TARGET_DSCP
138 tristate '"DSCP" target support'
139 depends on NETFILTER_XTABLES
142 To compile it as a module, choose M here. If unsure, say N.
144 +config NETFILTER_XT_MATCH_PORTSCAN
145 + tristate '"portscan" match support'
146 + depends on NETFILTER_XTABLES
148 + This option adds a 'portscan' match support.
150 + To compile it as a module, choose M here. If unsure, say N.
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
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
167 obj-$(CONFIG_NETFILTER_XT_MATCH_COMMENT) += xt_comment.o
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
179 + CHAOS target for netfilter
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.
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>
196 +#include <linux/netfilter/xt_CHAOS.h>
197 +#include "find_match.c"
198 +#include <linux/netfilter/oot_trans.h>
199 +#define PFX KBUILD_MODNAME ": "
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);
207 +/* References to other matches/targets */
208 +static struct xt_match *xm_tcp;
209 +static struct xt_target *xt_delude, *xt_reject, *xt_tarpit;
211 +static int have_delude, have_tarpit;
213 +/* Static data for other matches/targets */
214 +static const struct ipt_reject_info reject_params = {
215 + .with = ICMP_HOST_UNREACH,
218 +static const struct xt_tcp tcp_params = {
223 +/* CHAOS functions */
224 +static void xt_chaos_total(const struct xt_chaos_info *info,
225 + struct sk_buff *skb, const struct net_device *in,
226 + const struct net_device *out, unsigned int hooknum)
228 + const int protoff = ip_hdrlen(skb);
229 + const int offset = ntohs(ip_hdr(skb)->frag_off) & IP_OFFSET;
230 + const struct xt_target *destiny;
231 + bool hotdrop = false;
234 + ret = xm_tcp->match(skb, in, out, xm_tcp, &tcp_params,
235 + offset, protoff, &hotdrop);
236 + if(!ret || hotdrop || (unsigned int)net_random() > delude_percentage)
239 + destiny = (info->variant == XTCHAOS_TARPIT) ? xt_tarpit : xt_delude;
240 +#ifdef HAVE_TARGUSERINFO
241 + destiny->target(skb, in, out, hooknum, destiny, NULL, NULL);
243 + destiny->target(skb, in, out, hooknum, destiny, NULL);
248 +static unsigned int xt_chaos_target(struct sk_buff *skb,
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
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;
264 + const struct xt_chaos_info *info = targinfo;
266 + if((unsigned int)net_random() <= reject_percentage)
267 +#ifdef HAVE_TARGUSERINFO
268 + return xt_reject->target(skb, in, out, hooknum, target,
269 + &reject_params, userinfo);
271 + return xt_reject->target(skb, in, out, hooknum, target,
275 + /* TARPIT/DELUDE may not be called from the OUTPUT chain */
276 + if(ip_hdr(skb)->protocol == IPPROTO_TCP &&
277 + info->variant != XTCHAOS_NORMAL && hooknum != NF_IP_LOCAL_OUT)
278 + xt_chaos_total(info, skb, in, out, hooknum);
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,
288 + unsigned int hook_mask)
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");
296 + if(info->variant == XTCHAOS_TARPIT && !have_tarpit) {
297 + printk(KERN_WARNING PFX "Error: Cannot use --tarpit when "
298 + "TARPIT module not available\n");
304 +static struct xt_target xt_chaos_info = {
306 + .target = xt_chaos_target,
307 + .checkentry = xt_chaos_checkentry,
309 + .targetsize = sizeof(struct xt_chaos_info),
310 + .hooks = (1 << NF_IP_LOCAL_IN) | (1 << NF_IP_FORWARD) |
311 + (1 << NF_IP_LOCAL_OUT),
316 +static int __init xt_chaos_init(void)
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");
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");
334 + xt_tarpit = xt_request_find_target(AF_INET, "TARPIT", 0);
335 + have_tarpit = xt_tarpit != NULL;
337 + printk(KERN_WARNING PFX "Warning: Could not find or load "
338 + "\"TARPIT\" target\n");
340 + xt_delude = xt_request_find_target(AF_INET, "DELUDE", 0);
341 + have_delude = xt_delude != NULL;
343 + printk(KERN_WARNING PFX "Warning: Could not find or load "
344 + "\"DELUDE\" target\n");
346 + if((ret = xt_register_target(&xt_chaos_info)) != 0) {
347 + printk(KERN_WARNING PFX "xt_register_target returned "
348 + "error %d\n", ret);
356 + module_put(xt_delude->me);
358 + module_put(xt_tarpit->me);
359 + module_put(xt_reject->me);
361 + module_put(xm_tcp->me);
365 +static void __exit xt_chaos_exit(void)
367 + xt_unregister_target(&xt_chaos_info);
368 + module_put(xm_tcp->me);
369 + module_put(xt_reject->me);
371 + module_put(xt_delude->me);
373 + module_put(xt_tarpit->me);
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
390 + Copyright © Jan Engelhardt <jengelh [at] gmx de>, 2007
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>
396 + xt_DELUDE acts like REJECT, but does reply with SYN-ACK on SYN.
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.
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>
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>
418 +#include <linux/netfilter/oot_trans.h>
419 +#define PFX KBUILD_MODNAME ": "
421 +static inline struct rtable *route_reverse(struct sk_buff *skb,
422 + struct tcphdr *tcph, int hook)
424 + struct iphdr *iph = ip_hdr(skb);
425 + struct dst_entry *odst;
426 + struct flowi fl = {};
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)
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);
441 + if (ip_route_output_key(&rt, &fl) != 0)
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)
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);
456 + dst_release(&rt->u.dst);
457 + rt = (struct rtable *)skb->dst;
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);
465 + if (rt->u.dst.error) {
466 + dst_release(&rt->u.dst);
470 + fl.proto = IPPROTO_TCP;
471 + fl.fl_ip_sport = tcph->dest;
472 + fl.fl_ip_dport = tcph->source;
474 + xfrm_lookup((struct dst_entry **)&rt, &fl, NULL, 0);
479 +static void send_reset(struct sk_buff *oldskb, int hook)
481 + struct sk_buff *nskb;
482 + struct iphdr *iph = ip_hdr(oldskb);
483 + struct tcphdr _otcph, *oth, *tcph;
487 + unsigned int addr_type;
489 + /* IP header checks: fragment. */
490 + if (iph->frag_off & htons(IP_OFFSET))
493 + oth = skb_header_pointer(oldskb, ip_hdrlen(oldskb),
494 + sizeof(_otcph), &_otcph);
498 + /* No RST for RST. */
502 + /* Check checksum */
503 + if (nf_ip_checksum(oldskb, hook, ip_hdrlen(oldskb), IPPROTO_TCP))
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),
514 + /* This packet will not be the same as the other: clear nf fields */
517 + skb_init_secmark(nskb);
519 + skb_shinfo(nskb)->gso_size = 0;
520 + skb_shinfo(nskb)->gso_segs = 0;
521 + skb_shinfo(nskb)->gso_type = 0;
523 + tcph = tcp_hdr(nskb);
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;
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);
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) -
543 + tcph->seq = htonl(secure_tcp_sequence_number(
544 + ip_hdr(nskb)->saddr, ip_hdr(nskb)->daddr,
545 + tcph->source, tcph->dest));
550 + tcph->ack_seq = htonl(ntohl(oth->seq) + oth->syn + oth->fin
551 + + oldskb->len - ip_hdrlen(oldskb)
556 + tcph->seq = oth->ack_seq;
561 + ((u_int8_t *)tcph)[13] = 0;
563 + tcph->ack = needs_ack;
570 + /* Adjust TCP checksum */
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));
578 + /* Set DF, id = 0 */
579 + ip_hdr(nskb)->frag_off = htons(IP_DF);
580 + ip_hdr(nskb)->id = 0;
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)
588 + addr_type = RTN_LOCAL;
590 + if (ip_route_me_harder(nskb, addr_type))
593 + nskb->ip_summed = CHECKSUM_NONE;
595 + /* Adjust IP TTL */
596 + ip_hdr(nskb)->ttl = dst_metric(nskb->dst, RTAX_HOPLIMIT);
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);
603 + /* "Never happens" */
604 + if (nskb->len > dst_mtu(nskb->dst))
607 + nf_ct_attach(nskb, oldskb);
609 + NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, nskb, NULL, nskb->dst->dev,
617 +static unsigned int xt_delude_target(struct sk_buff *skb,
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
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(skb, hooknum);
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,
638 + unsigned int hook_mask)
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");
648 +static struct xt_target xt_delude_info = {
650 + .target = xt_delude_target,
651 + .checkentry = xt_delude_check,
653 + .hooks = (1 << NF_IP_LOCAL_IN) | (1 << NF_IP_FORWARD) |
654 + (1 << NF_IP_LOCAL_OUT),
655 + .proto = IPPROTO_TCP,
660 +static int __init xt_delude_init(void)
662 + return xt_register_target(&xt_delude_info);
665 +static void __exit xt_delude_exit(void)
667 + xt_unregister_target(&xt_delude_info);
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
682 + portscan match for netfilter
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.
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 ": "
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,
711 +/* Module parameters */
713 + connmark_mask = ~0,
718 + mark_synscan = 0x3,
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");
748 +/* TCP flag functions */
749 +static inline int tflg_ack4(const struct tcphdr *th)
751 + return (tcp_flag_word(th) & TCP_FLAGS_ALL4) == TCP_FLAG_ACK;
754 +static inline int tflg_ack6(const struct tcphdr *th)
756 + return (tcp_flag_word(th) & TCP_FLAGS_ALL6) == TCP_FLAG_ACK;
759 +static inline int tflg_fin(const struct tcphdr *th)
761 + return (tcp_flag_word(th) & TCP_FLAGS_ALL3) == TCP_FLAG_FIN;
764 +static inline int tflg_rst(const struct tcphdr *th)
766 + return (tcp_flag_word(th) & TCP_FLAGS_ALL3) == TCP_FLAG_RST;
769 +static inline int tflg_rstack(const struct tcphdr *th)
771 + return (tcp_flag_word(th) & TCP_FLAGS_ALL4) ==
772 + (TCP_FLAG_ACK | TCP_FLAG_RST);
775 +static inline int tflg_syn(const struct tcphdr *th)
777 + return (tcp_flag_word(th) & TCP_FLAGS_ALL4) == TCP_FLAG_SYN;
780 +static inline int tflg_synack(const struct tcphdr *th)
782 + return (tcp_flag_word(th) & TCP_FLAGS_ALL4) ==
783 + (TCP_FLAG_SYN | TCP_FLAG_ACK);
786 +/* portscan functions */
787 +static inline int xt_portscan_stealth(const struct tcphdr *th)
790 + * "Connection refused" replies to our own probes must not be matched.
792 + if(tflg_rstack(th))
795 + if(tflg_rst(th) && printk_ratelimit()) {
796 + printk(KERN_WARNING PFX "Warning: Pure RST received\n");
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.).
806 + return !tflg_syn(th);
809 +static inline int xt_portscan_full(int mark, enum ip_conntrack_info ctstate,
810 + int loopback, const struct tcphdr *tcph, int payload_len)
812 + if(mark == mark_estab2) {
814 + * -m connmark --mark $ESTAB2
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;
822 + } else if(mark == mark_estab1) {
824 + * -m connmark --mark $ESTAB1
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;
832 + } else if(mark == mark_synrcv) {
834 + * -m connmark --mark $SYN
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;
843 + return mark_synscan;
844 + } else if(ctstate == IP_CT_NEW && tflg_syn(tcph)) {
846 + * -p tcp --syn --ctstate NEW
848 + return mark_synrcv;
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)
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;
864 + tcph = skb_header_pointer(skb, protoff, sizeof(tcph_buf), &tcph_buf);
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);
873 + * If @ctdata is NULL, we cannot match the other scan
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.
884 + if((ctdata->mark & connmark_mask) == mark_valid ||
885 + (skb->nfmark & packet_mask) != mark_seen)
888 + n = xt_portscan_full(ctdata->mark & connmark_mask, ctstate,
889 + (in->flags && IFF_LOOPBACK) == IFF_LOOPBACK, tcph,
890 + skb->len - protoff - 4 * tcph->doff);
892 + ctdata->mark = (ctdata->mark & ~connmark_mask) | n;
893 + ((struct sk_buff *)skb)->nfmark =
894 + (skb->nfmark & ~packet_mask) | mark_seen;
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);
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,
907 + unsigned int hook_mask)
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",
914 + XT_ALIGN(sizeof(struct xt_portscan_info)));
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");
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,
936 +static int __init xt_portscan_init(void)
938 + return xt_register_match(&xt_portscan);
941 +static void __exit xt_portscan_exit(void)
943 + xt_unregister_match(&xt_portscan);
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 @@
961 +EXPORT_SYMBOL(secure_tcp_sequence_number);
963 /* Generate secure starting point for ephemeral IPV4 transport port search */
964 u32 secure_ipv4_port_ephemeral(__be32 saddr, __be32 daddr, __be16 dport)