1 Index: linux-2.6.24/include/linux/netfilter/oot_conntrack.h
2 ===================================================================
4 +++ linux-2.6.24/include/linux/netfilter/oot_conntrack.h
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.24/include/linux/netfilter/oot_trans.h
12 ===================================================================
14 +++ linux-2.6.24/include/linux/netfilter/oot_trans.h
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.24/include/linux/netfilter/xt_CHAOS.h
31 ===================================================================
33 +++ linux-2.6.24/include/linux/netfilter/xt_CHAOS.h
35 +#ifndef _LINUX_NETFILTER_XT_CHAOS_H
36 +#define _LINUX_NETFILTER_XT_CHAOS_H 1
38 +enum xt_chaos_target_variant {
44 +struct xt_chaos_target_info {
48 +#endif /* _LINUX_NETFILTER_XT_CHAOS_H */
49 Index: linux-2.6.24/include/linux/netfilter/xt_portscan.h
50 ===================================================================
52 +++ linux-2.6.24/include/linux/netfilter/xt_portscan.h
54 +#ifndef _LINUX_NETFILTER_XT_PORTSCAN_H
55 +#define _LINUX_NETFILTER_XT_PORTSCAN_H 1
57 +struct xt_portscan_match_info {
58 + uint8_t match_stealth, match_syn, match_cn, match_gr;
61 +#endif /* _LINUX_NETFILTER_XT_PORTSCAN_H */
62 Index: linux-2.6.24/net/netfilter/find_match.c
63 ===================================================================
65 +++ linux-2.6.24/net/netfilter/find_match.c
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.24/net/netfilter/Kconfig
107 ===================================================================
108 --- linux-2.6.24.orig/net/netfilter/Kconfig
109 +++ linux-2.6.24/net/netfilter/Kconfig
110 @@ -265,6 +265,14 @@ config NETFILTER_XTABLES
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
125 @@ -292,6 +300,14 @@ config NETFILTER_XT_TARGET_CONNMARK
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
140 @@ -556,6 +572,14 @@ config NETFILTER_XT_MATCH_POLICY
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.24/net/netfilter/Makefile
156 ===================================================================
157 --- linux-2.6.24.orig/net/netfilter/Makefile
158 +++ linux-2.6.24/net/netfilter/Makefile
159 @@ -49,6 +49,8 @@ obj-$(CONFIG_NETFILTER_XT_TARGET_NOTRACK
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
168 @@ -79,3 +81,4 @@ obj-$(CONFIG_NETFILTER_XT_MATCH_STRING)
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.24/net/netfilter/xt_CHAOS.c
174 ===================================================================
176 +++ linux-2.6.24/net/netfilter/xt_CHAOS.c
179 + * CHAOS target for netfilter
180 + * Copyright © CC Computer Consultants GmbH, 2006 - 2007
181 + * Contact: Jan Engelhardt <jengelh@computergmbh.de>
183 + * This program is free software; you can redistribute it and/or modify
184 + * it under the terms of the GNU General Public License; either version
185 + * 2 or 3 as published by the Free Software Foundation.
187 +#include <linux/icmp.h>
188 +#include <linux/in.h>
189 +#include <linux/ip.h>
190 +#include <linux/module.h>
191 +#include <linux/skbuff.h>
192 +#include <linux/stat.h>
193 +#include <linux/netfilter/x_tables.h>
194 +#include <linux/netfilter/xt_tcpudp.h>
195 +#include <linux/netfilter_ipv4/ipt_REJECT.h>
198 +# include "xt_CHAOS.h"
199 +# include "find_match.c"
200 +#elif defined(CONFIG_NETFILTER_XT_TARGET_CHAOS) || \
201 + defined(CONFIG_NETFILTER_XT_TARGET_CHAOS_MODULE)
202 +# include <linux/netfilter/xt_CHAOS.h>
203 +# include "find_match.c"
205 +# include "xt_CHAOS.h"
206 +# include "find_match.c"
208 +#define PFX KBUILD_MODNAME ": "
210 +/* Module parameters */
211 +static unsigned int reject_percentage = ~0U * .01;
212 +static unsigned int delude_percentage = ~0U * .0101;
213 +module_param(reject_percentage, uint, S_IRUGO | S_IWUSR);
214 +module_param(delude_percentage, uint, S_IRUGO | S_IWUSR);
216 +/* References to other matches/targets */
217 +static struct xt_match *xm_tcp;
218 +static struct xt_target *xt_delude, *xt_reject, *xt_tarpit;
220 +static int have_delude, have_tarpit;
222 +/* Static data for other matches/targets */
223 +static const struct ipt_reject_info reject_params = {
224 + .with = ICMP_HOST_UNREACH,
227 +static const struct xt_tcp tcp_params = {
232 +/* CHAOS functions */
233 +static void xt_chaos_total(const struct xt_chaos_target_info *info,
234 + struct sk_buff *skb, const struct net_device *in,
235 + const struct net_device *out, unsigned int hooknum)
237 + const struct iphdr *iph = ip_hdr(skb);
238 + const int protoff = 4 * iph->ihl;
239 + const int offset = ntohs(iph->frag_off) & IP_OFFSET;
240 + const struct xt_target *destiny;
241 + bool hotdrop = false, ret;
243 + ret = xm_tcp->match(skb, in, out, xm_tcp, &tcp_params,
244 + offset, protoff, &hotdrop);
245 + if (!ret || hotdrop || (unsigned int)net_random() > delude_percentage)
248 + destiny = (info->variant == XTCHAOS_TARPIT) ? xt_tarpit : xt_delude;
249 + destiny->target(skb, in, out, hooknum, destiny, NULL);
253 +static unsigned int chaos_tg(struct sk_buff *skb,
254 + const struct net_device *in, const struct net_device *out,
255 + unsigned int hooknum, const struct xt_target *target, const void *targinfo)
259 + * -A chaos -m statistic --mode random --probability \
260 + * $reject_percentage -j REJECT --reject-with host-unreach;
261 + * -A chaos -p tcp -m statistic --mode random --probability \
262 + * $delude_percentage -j DELUDE;
263 + * -A chaos -j DROP;
265 + const struct xt_chaos_target_info *info = targinfo;
266 + const struct iphdr *iph = ip_hdr(skb);
268 + if ((unsigned int)net_random() <= reject_percentage)
269 + return xt_reject->target(skb, in, out, hooknum, target,
272 + /* TARPIT/DELUDE may not be called from the OUTPUT chain */
273 + if (iph->protocol == IPPROTO_TCP &&
274 + info->variant != XTCHAOS_NORMAL && hooknum != NF_IP_LOCAL_OUT)
275 + xt_chaos_total(info, skb, in, out, hooknum);
280 +static bool chaos_tg_check(const char *tablename, const void *entry,
281 + const struct xt_target *target, void *targinfo, unsigned int hook_mask)
283 + const struct xt_chaos_target_info *info = targinfo;
285 + if (info->variant == XTCHAOS_DELUDE && !have_delude) {
286 + printk(KERN_WARNING PFX "Error: Cannot use --delude when "
287 + "DELUDE module not available\n");
290 + if (info->variant == XTCHAOS_TARPIT && !have_tarpit) {
291 + printk(KERN_WARNING PFX "Error: Cannot use --tarpit when "
292 + "TARPIT module not available\n");
299 +static struct xt_target chaos_tg_reg = {
303 + .hooks = (1 << NF_IP_LOCAL_IN) | (1 << NF_IP_FORWARD) |
304 + (1 << NF_IP_LOCAL_OUT),
305 + .checkentry = chaos_tg_check,
306 + .target = chaos_tg,
307 + .targetsize = sizeof(struct xt_chaos_target_info),
311 +static int __init chaos_tg_init(void)
315 + xm_tcp = xt_request_find_match(AF_INET, "tcp", 0);
316 + if (xm_tcp == NULL) {
317 + printk(KERN_WARNING PFX "Error: Could not find or load "
318 + "\"tcp\" match\n");
322 + xt_reject = xt_request_find_target(AF_INET, "REJECT", 0);
323 + if (xt_reject == NULL) {
324 + printk(KERN_WARNING PFX "Error: Could not find or load "
325 + "\"REJECT\" target\n");
329 + xt_tarpit = xt_request_find_target(AF_INET, "TARPIT", 0);
330 + have_tarpit = xt_tarpit != NULL;
332 + printk(KERN_WARNING PFX "Warning: Could not find or load "
333 + "\"TARPIT\" target\n");
335 + xt_delude = xt_request_find_target(AF_INET, "DELUDE", 0);
336 + have_delude = xt_delude != NULL;
338 + printk(KERN_WARNING PFX "Warning: Could not find or load "
339 + "\"DELUDE\" target\n");
341 + if ((ret = xt_register_target(&chaos_tg_reg)) != 0) {
342 + printk(KERN_WARNING PFX "xt_register_target returned "
343 + "error %d\n", ret);
351 + module_put(xt_delude->me);
353 + module_put(xt_tarpit->me);
354 + module_put(xt_reject->me);
356 + module_put(xm_tcp->me);
360 +static void __exit chaos_tg_exit(void)
362 + xt_unregister_target(&chaos_tg_reg);
363 + module_put(xm_tcp->me);
364 + module_put(xt_reject->me);
366 + module_put(xt_delude->me);
368 + module_put(xt_tarpit->me);
372 +module_init(chaos_tg_init);
373 +module_exit(chaos_tg_exit);
374 +MODULE_AUTHOR("Jan Engelhardt <jengelh@computergmbh.de>");
375 +MODULE_DESCRIPTION("netfilter \"CHAOS\" target");
376 +MODULE_LICENSE("GPL");
377 +MODULE_ALIAS("ipt_CHAOS");
378 Index: linux-2.6.24/net/netfilter/xt_DELUDE.c
379 ===================================================================
381 +++ linux-2.6.24/net/netfilter/xt_DELUDE.c
385 + * Copyright © CC Computer Consultants GmbH, 2007
386 + * Contact: Jan Engelhardt <jengelh@computergmbh.de>
388 + * Based upon linux-2.6.18.5/net/ipv4/netfilter/ipt_REJECT.c:
389 + * (C) 1999-2001 Paul `Rusty' Russell
390 + * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
392 + * xt_DELUDE acts like REJECT, but does reply with SYN-ACK on SYN.
394 + * This program is free software; you can redistribute it and/or modify
395 + * it under the terms of the GNU General Public License version 2 as
396 + * published by the Free Software Foundation.
398 +#include <linux/module.h>
399 +#include <linux/skbuff.h>
400 +#include <linux/ip.h>
401 +#include <linux/tcp.h>
402 +#include <linux/netfilter/x_tables.h>
403 +#ifdef CONFIG_BRIDGE_NETFILTER
404 +# include <linux/netfilter_bridge.h>
406 +#include <net/tcp.h>
407 +#define PFX KBUILD_MODNAME ": "
409 +static void delude_send_reset(struct sk_buff *oldskb, unsigned int hook)
411 + struct tcphdr _otcph, *oth, *tcph;
412 + unsigned int addr_type;
413 + struct sk_buff *nskb;
414 + u_int16_t tmp_port;
415 + u_int32_t tmp_addr;
416 + struct iphdr *niph;
419 + /* IP header checks: fragment. */
420 + if (ip_hdr(oldskb)->frag_off & htons(IP_OFFSET))
423 + oth = skb_header_pointer(oldskb, ip_hdrlen(oldskb),
424 + sizeof(_otcph), &_otcph);
428 + /* No RST for RST. */
432 + /* Check checksum */
433 + if (nf_ip_checksum(oldskb, hook, ip_hdrlen(oldskb), IPPROTO_TCP))
436 + /* We need a linear, writeable skb. We also need to expand
437 + headroom in case hh_len of incoming interface < hh_len of
438 + outgoing interface */
439 + nskb = skb_copy_expand(oldskb, LL_MAX_HEADER, skb_tailroom(oldskb),
444 + /* This packet will not be the same as the other: clear nf fields */
447 + skb_init_secmark(nskb);
449 + skb_shinfo(nskb)->gso_size = 0;
450 + skb_shinfo(nskb)->gso_segs = 0;
451 + skb_shinfo(nskb)->gso_type = 0;
453 + tcph = (struct tcphdr *)(skb_network_header(nskb) + ip_hdrlen(nskb));
455 + /* Swap source and dest */
456 + niph = ip_hdr(nskb);
457 + tmp_addr = niph->saddr;
458 + niph->saddr = niph->daddr;
459 + niph->daddr = tmp_addr;
460 + tmp_port = tcph->source;
461 + tcph->source = tcph->dest;
462 + tcph->dest = tmp_port;
464 + /* Truncate to length (no data) */
465 + tcph->doff = sizeof(struct tcphdr) / 4;
466 + skb_trim(nskb, ip_hdrlen(nskb) + sizeof(struct tcphdr));
467 + niph->tot_len = htons(nskb->len);
469 + if (oth->syn && !oth->ack && !oth->rst && !oth->fin) {
470 + /* DELUDE essential part */
471 + tcph->ack_seq = htonl(ntohl(oth->seq) + oth->syn + oth->fin +
472 + oldskb->len - ip_hdrlen(oldskb) -
479 + tcph->ack_seq = htonl(ntohl(oth->seq) + oth->syn +
480 + oth->fin + oldskb->len -
481 + ip_hdrlen(oldskb) - (oth->doff<<2));
485 + tcph->seq = oth->ack_seq;
486 + tcph->ack_seq = false;
490 + ((u_int8_t *)tcph)[13] = 0;
492 + tcph->ack = needs_ack;
498 + /* Adjust TCP checksum */
500 + tcph->check = tcp_v4_check(sizeof(struct tcphdr), niph->saddr,
501 + niph->daddr, csum_partial((char *)tcph,
502 + sizeof(struct tcphdr), 0));
504 + /* Set DF, id = 0 */
505 + niph->frag_off = htons(IP_DF);
508 + addr_type = RTN_UNSPEC;
509 +#ifdef CONFIG_BRIDGE_NETFILTER
510 + if (hook != NF_IP_FORWARD || (nskb->nf_bridge != NULL &&
511 + nskb->nf_bridge->mask & BRNF_BRIDGED))
513 + if (hook != NF_IP_FORWARD)
515 + addr_type = RTN_LOCAL;
517 + if (ip_route_me_harder(nskb, addr_type))
520 + nskb->ip_summed = CHECKSUM_NONE;
522 + /* Adjust IP TTL */
523 + niph->ttl = dst_metric(nskb->dst, RTAX_HOPLIMIT);
525 + /* Adjust IP checksum */
527 + niph->check = ip_fast_csum(skb_network_header(nskb), niph->ihl);
529 + /* "Never happens" */
530 + if (nskb->len > dst_mtu(nskb->dst))
533 + nf_ct_attach(nskb, oldskb);
535 + NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, nskb, NULL, nskb->dst->dev,
543 +static unsigned int delude_tg(struct sk_buff *skb,
544 + const struct net_device *in, const struct net_device *out,
545 + unsigned int hooknum, const struct xt_target *target, const void *targinfo)
547 + /* WARNING: This code causes reentry within iptables.
548 + This means that the iptables jump stack is now crap. We
549 + must return an absolute verdict. --RR */
550 + delude_send_reset(skb, hooknum);
554 +static struct xt_target delude_tg_reg = {
558 + .hooks = (1 << NF_IP_LOCAL_IN) | (1 << NF_IP_FORWARD),
559 + .target = delude_tg,
560 + .proto = IPPROTO_TCP,
564 +static int __init delude_tg_init(void)
566 + return xt_register_target(&delude_tg_reg);
569 +static void __exit delude_tg_exit(void)
571 + xt_unregister_target(&delude_tg_reg);
574 +module_init(delude_tg_init);
575 +module_exit(delude_tg_exit);
576 +MODULE_AUTHOR("Jan Engelhardt <jengelh@computergmbh.de>");
577 +MODULE_DESCRIPTION("netfilter \"DELUDE\" target");
578 +MODULE_LICENSE("GPL");
579 +MODULE_ALIAS("ipt_DELUDE");
580 Index: linux-2.6.24/net/netfilter/xt_portscan.c
581 ===================================================================
583 +++ linux-2.6.24/net/netfilter/xt_portscan.c
586 + * portscan match for netfilter
587 + * Copyright © CC Computer Consultants GmbH, 2006 - 2007
588 + * Contact: Jan Engelhardt <jengelh@computergmbh.de>
590 + * This program is free software; you can redistribute it and/or modify
591 + * it under the terms of the GNU General Public License; either version
592 + * 2 or 3 as published by the Free Software Foundation.
594 +#include <linux/in.h>
595 +#include <linux/ip.h>
596 +#include <linux/module.h>
597 +#include <linux/moduleparam.h>
598 +#include <linux/skbuff.h>
599 +#include <linux/stat.h>
600 +#include <linux/tcp.h>
601 +#include <linux/types.h>
602 +#include <linux/version.h>
603 +#include <linux/netfilter/x_tables.h>
604 +#include <linux/netfilter/xt_tcpudp.h>
605 +#include <net/netfilter/nf_nat_rule.h>
607 +# include "xt_portscan.h"
608 +#elif defined(CONFIG_NETFILTER_XT_MATCH_PORTSCAN) || \
609 + defined(CONFIG_NETFILTER_XT_MATCH_PORTSCAN_MODULE)
610 +# include <linux/netfilter/xt_portscan.h>
612 +# include "xt_portscan.h"
614 +#define PFX KBUILD_MODNAME ": "
617 + TCP_FLAGS_ALL3 = TCP_FLAG_FIN | TCP_FLAG_RST | TCP_FLAG_SYN,
618 + TCP_FLAGS_ALL4 = TCP_FLAGS_ALL3 | TCP_FLAG_ACK,
619 + TCP_FLAGS_ALL6 = TCP_FLAGS_ALL4 | TCP_FLAG_PSH | TCP_FLAG_URG,
622 +/* Module parameters */
624 + connmark_mask = ~0,
629 + mark_synscan = 0x3,
636 +module_param(connmark_mask, uint, S_IRUGO | S_IWUSR);
637 +module_param(packet_mask, uint, S_IRUGO | S_IWUSR);
638 +module_param(mark_seen, uint, S_IRUGO | S_IWUSR);
639 +module_param(mark_synrcv, uint, S_IRUGO | S_IWUSR);
640 +module_param(mark_closed, uint, S_IRUGO | S_IWUSR);
641 +module_param(mark_synscan, uint, S_IRUGO | S_IWUSR);
642 +module_param(mark_estab1, uint, S_IRUGO | S_IWUSR);
643 +module_param(mark_estab2, uint, S_IRUGO | S_IWUSR);
644 +module_param(mark_cnscan, uint, S_IRUGO | S_IWUSR);
645 +module_param(mark_grscan, uint, S_IRUGO | S_IWUSR);
646 +module_param(mark_valid, uint, S_IRUGO | S_IWUSR);
647 +MODULE_PARM_DESC(connmark_mask, "only set specified bits in connection mark");
648 +MODULE_PARM_DESC(packet_mask, "only set specified bits in packet mark");
649 +MODULE_PARM_DESC(mark_seen, "nfmark value for packet-seen state");
650 +MODULE_PARM_DESC(mark_synrcv, "connmark value for SYN Received state");
651 +MODULE_PARM_DESC(mark_closed, "connmark value for closed state");
652 +MODULE_PARM_DESC(mark_synscan, "connmark value for SYN Scan state");
653 +MODULE_PARM_DESC(mark_estab1, "connmark value for Established-1 state");
654 +MODULE_PARM_DESC(mark_estab2, "connmark value for Established-2 state");
655 +MODULE_PARM_DESC(mark_cnscan, "connmark value for Connect Scan state");
656 +MODULE_PARM_DESC(mark_grscan, "connmark value for Grab Scan state");
657 +MODULE_PARM_DESC(mark_valid, "connmark value for Valid state");
659 +/* TCP flag functions */
660 +static inline bool tflg_ack4(const struct tcphdr *th)
662 + return (tcp_flag_word(th) & TCP_FLAGS_ALL4) == TCP_FLAG_ACK;
665 +static inline bool tflg_ack6(const struct tcphdr *th)
667 + return (tcp_flag_word(th) & TCP_FLAGS_ALL6) == TCP_FLAG_ACK;
670 +static inline bool tflg_fin(const struct tcphdr *th)
672 + return (tcp_flag_word(th) & TCP_FLAGS_ALL3) == TCP_FLAG_FIN;
675 +static inline bool tflg_rst(const struct tcphdr *th)
677 + return (tcp_flag_word(th) & TCP_FLAGS_ALL3) == TCP_FLAG_RST;
680 +static inline bool tflg_rstack(const struct tcphdr *th)
682 + return (tcp_flag_word(th) & TCP_FLAGS_ALL4) ==
683 + (TCP_FLAG_ACK | TCP_FLAG_RST);
686 +static inline bool tflg_syn(const struct tcphdr *th)
688 + return (tcp_flag_word(th) & TCP_FLAGS_ALL4) == TCP_FLAG_SYN;
691 +static inline bool tflg_synack(const struct tcphdr *th)
693 + return (tcp_flag_word(th) & TCP_FLAGS_ALL4) ==
694 + (TCP_FLAG_SYN | TCP_FLAG_ACK);
697 +/* portscan functions */
698 +static inline bool portscan_mt_stealth(const struct tcphdr *th)
701 + * "Connection refused" replies to our own probes must not be matched.
703 + if (tflg_rstack(th))
706 + if (tflg_rst(th) && printk_ratelimit()) {
707 + printk(KERN_WARNING PFX "Warning: Pure RST received\n");
712 + * -p tcp ! --syn -m conntrack --ctstate INVALID: Looking for non-start
713 + * packets that are not associated with any connection -- this will
714 + * match most scan types (NULL, XMAS, FIN) and ridiculous flag
715 + * combinations (SYN-RST, SYN-FIN, SYN-FIN-RST, FIN-RST, etc.).
717 + return !tflg_syn(th);
720 +static inline unsigned int portscan_mt_full(int mark,
721 + enum ip_conntrack_info ctstate, bool loopback, const struct tcphdr *tcph,
722 + unsigned int payload_len)
724 + if (mark == mark_estab2) {
726 + * -m connmark --mark $ESTAB2
728 + if (tflg_ack4(tcph) && payload_len == 0)
729 + return mark; /* keep mark */
730 + else if (tflg_rst(tcph) || tflg_fin(tcph))
731 + return mark_grscan;
734 + } else if (mark == mark_estab1) {
736 + * -m connmark --mark $ESTAB1
738 + if (tflg_rst(tcph) || tflg_fin(tcph))
739 + return mark_cnscan;
740 + else if (!loopback && tflg_ack4(tcph) && payload_len == 0)
741 + return mark_estab2;
744 + } else if (mark == mark_synrcv) {
746 + * -m connmark --mark $SYN
748 + if (loopback && tflg_synack(tcph))
749 + return mark; /* keep mark */
750 + else if (loopback && tflg_rstack(tcph))
751 + return mark_closed;
752 + else if (tflg_ack6(tcph))
753 + return mark_estab1;
755 + return mark_synscan;
756 + } else if (ctstate == IP_CT_NEW && tflg_syn(tcph)) {
758 + * -p tcp --syn --ctstate NEW
760 + return mark_synrcv;
765 +static bool portscan_mt(const struct sk_buff *skb,
766 + const struct net_device *in, const struct net_device *out,
767 + const struct xt_match *match, const void *matchinfo, int offset,
768 + unsigned int protoff, bool *hotdrop)
770 + const struct xt_portscan_match_info *info = matchinfo;
771 + enum ip_conntrack_info ctstate;
772 + const struct tcphdr *tcph;
773 + struct nf_conn *ctdata;
774 + struct tcphdr tcph_buf;
776 + tcph = skb_header_pointer(skb, protoff, sizeof(tcph_buf), &tcph_buf);
780 + /* Check for invalid packets: -m conntrack --ctstate INVALID */
781 + if ((ctdata = nf_ct_get(skb, &ctstate)) == NULL) {
782 + if (info->match_stealth)
783 + return portscan_mt_stealth(tcph);
785 + * If @ctdata is NULL, we cannot match the other scan
792 + * If -m portscan was previously applied to this packet, the rules we
793 + * simulate must not be run through again. And for speedup, do not call
794 + * it either when the connection is already VALID.
796 + if ((ctdata->mark & connmark_mask) == mark_valid ||
797 + (skb->mark & packet_mask) != mark_seen) {
800 + n = portscan_mt_full(ctdata->mark & connmark_mask, ctstate,
801 + (in->flags & IFF_LOOPBACK) == IFF_LOOPBACK, tcph,
802 + skb->len - protoff - 4 * tcph->doff);
804 + ctdata->mark = (ctdata->mark & ~connmark_mask) | n;
805 + ((struct sk_buff *)skb)->mark =
806 + (skb->mark & ~packet_mask) ^ mark_seen;
809 + return (info->match_syn && ctdata->mark == mark_synscan) ||
810 + (info->match_cn && ctdata->mark == mark_cnscan) ||
811 + (info->match_gr && ctdata->mark == mark_grscan);
814 +static bool portscan_mt_check(const char *tablename, const void *entry,
815 + const struct xt_match *match, void *matchinfo, unsigned int hook_mask)
817 + const struct xt_portscan_match_info *info = matchinfo;
819 + if ((info->match_stealth & ~1) || (info->match_syn & ~1) ||
820 + (info->match_cn & ~1) || (info->match_gr & ~1)) {
821 + printk(KERN_WARNING PFX "Invalid flags\n");
827 +static struct xt_match portscan_mt_reg __read_mostly = {
828 + .name = "portscan",
830 + .match = portscan_mt,
831 + .checkentry = portscan_mt_check,
832 + .matchsize = sizeof(struct xt_portscan_match_info),
833 + .proto = IPPROTO_TCP,
837 +static int __init portscan_mt_init(void)
839 + return xt_register_match(&portscan_mt_reg);
842 +static void __exit portscan_mt_exit(void)
844 + xt_unregister_match(&portscan_mt_reg);
848 +module_init(portscan_mt_init);
849 +module_exit(portscan_mt_exit);
850 +MODULE_AUTHOR("Jan Engelhardt <jengelh@computergmbh.de>");
851 +MODULE_DESCRIPTION("netfilter \"portscan\" match");
852 +MODULE_LICENSE("GPL");
853 +MODULE_ALIAS("ipt_portscan");
854 Index: linux-2.6.24/drivers/char/random.c
855 ===================================================================
856 --- linux-2.6.24.orig/drivers/char/random.c
857 +++ linux-2.6.24/drivers/char/random.c
858 @@ -1564,6 +1564,8 @@ __u32 secure_tcp_sequence_number(__be32
862 +EXPORT_SYMBOL(secure_tcp_sequence_number);
864 /* Generate secure starting point for ephemeral IPV4 transport port search */
865 u32 secure_ipv4_port_ephemeral(__be32 saddr, __be32 daddr, __be16 dport)