1 diff -urN linux-2.6.21.1.old/include/linux/netfilter/oot_conntrack.h linux-2.6.21.1.dev/include/linux/netfilter/oot_conntrack.h
2 --- linux-2.6.21.1.old/include/linux/netfilter/oot_conntrack.h 1970-01-01 01:00:00.000000000 +0100
3 +++ linux-2.6.21.1.dev/include/linux/netfilter/oot_conntrack.h 2007-05-26 20:40:10.922195992 +0200
5 +#if defined(CONFIG_IP_NF_CONNTRACK) || defined(CONFIG_IP_NF_CONNTRACK_MODULE)
6 +# include <linux/netfilter_ipv4/ip_conntrack.h>
7 +#else /* linux-2.6.20+ */
8 +# include <net/netfilter/nf_nat_rule.h>
10 diff -urN linux-2.6.21.1.old/include/linux/netfilter/oot_trans.h linux-2.6.21.1.dev/include/linux/netfilter/oot_trans.h
11 --- linux-2.6.21.1.old/include/linux/netfilter/oot_trans.h 1970-01-01 01:00:00.000000000 +0100
12 +++ linux-2.6.21.1.dev/include/linux/netfilter/oot_trans.h 2007-05-26 20:40:10.940193256 +0200
14 +/* Out of tree workarounds */
15 +#include <linux/version.h>
16 +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 18)
17 +# define HAVE_MATCHINFOSIZE 1
18 +# define HAVE_TARGUSERINFO 1
19 +# define HAVE_TARGINFOSIZE 1
21 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
24 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 21)
25 +# define tcp_v4_check(tcph, tcph_sz, s, d, csp) \
26 + tcp_v4_check((tcph_sz), (s), (d), (csp))
28 diff -urN linux-2.6.21.1.old/include/linux/netfilter/xt_CHAOS.h linux-2.6.21.1.dev/include/linux/netfilter/xt_CHAOS.h
29 --- linux-2.6.21.1.old/include/linux/netfilter/xt_CHAOS.h 1970-01-01 01:00:00.000000000 +0100
30 +++ linux-2.6.21.1.dev/include/linux/netfilter/xt_CHAOS.h 2007-05-26 20:40:10.940193256 +0200
32 +#ifndef _LINUX_XT_CHAOS_H
33 +#define _LINUX_XT_CHAOS_H 1
35 +enum xt_chaos_variant {
41 +struct xt_chaos_info {
42 + enum xt_chaos_variant variant;
45 +#endif /* _LINUX_XT_CHAOS_H */
46 diff -urN linux-2.6.21.1.old/include/linux/netfilter/xt_portscan.h linux-2.6.21.1.dev/include/linux/netfilter/xt_portscan.h
47 --- linux-2.6.21.1.old/include/linux/netfilter/xt_portscan.h 1970-01-01 01:00:00.000000000 +0100
48 +++ linux-2.6.21.1.dev/include/linux/netfilter/xt_portscan.h 2007-05-26 20:40:10.940193256 +0200
50 +#ifndef _LINUX_XT_PORTSCAN_H
51 +#define _LINUX_XT_PORTSCAN_H 1
53 +struct xt_portscan_info {
54 + unsigned int match_stealth, match_syn, match_cn, match_gr;
57 +#endif /* _LINUX_XT_PORTSCAN_H */
58 diff -urN linux-2.6.21.1.old/net/netfilter/find_match.c linux-2.6.21.1.dev/net/netfilter/find_match.c
59 --- linux-2.6.21.1.old/net/netfilter/find_match.c 1970-01-01 01:00:00.000000000 +0100
60 +++ linux-2.6.21.1.dev/net/netfilter/find_match.c 2007-05-26 20:40:10.970188696 +0200
63 + xt_request_find_match
64 + by Jan Engelhardt <jengelh [at] gmx de>, 2006 - 2007
66 + Based upon linux-2.6.18.5/net/netfilter/x_tables.c:
67 + Copyright (C) 2006-2006 Harald Welte <laforge@netfilter.org>
68 + This program is free software; you can redistribute it and/or modify
69 + it under the terms of the GNU General Public License version 2 as
70 + published by the Free Software Foundation.
72 +#include <linux/err.h>
73 +#include <linux/netfilter_arp.h>
74 +#include <linux/socket.h>
75 +#include <linux/netfilter/x_tables.h>
78 + * Yeah this code is sub-optimal, but the function is missing in
79 + * mainline so far. -jengelh
81 +static struct xt_match *xt_request_find_match_lo(int af, const char *name,
84 + static const char *const xt_prefix[] = {
89 + struct xt_match *match;
91 + match = try_then_request_module(xt_find_match(af, name, revision),
92 + "%st_%s", xt_prefix[af], name);
93 + if(IS_ERR(match) || match == NULL)
99 +/* In case it goes into mainline, let this out-of-tree package compile */
100 +#define xt_request_find_match xt_request_find_match_lo
101 diff -urN linux-2.6.21.1.old/net/netfilter/Kconfig linux-2.6.21.1.dev/net/netfilter/Kconfig
102 --- linux-2.6.21.1.old/net/netfilter/Kconfig 2007-04-27 23:49:26.000000000 +0200
103 +++ linux-2.6.21.1.dev/net/netfilter/Kconfig 2007-05-26 20:40:11.003183680 +0200
106 # alphabetically ordered list of targets
108 +config NETFILTER_XT_TARGET_CHAOS
109 + tristate '"CHAOS" target support'
110 + depends on NETFILTER_XTABLES
112 + This option adds a `CHAOS' target.
114 + To compile it as a module, choose M here. If unsure, say N.
116 config NETFILTER_XT_TARGET_CLASSIFY
117 tristate '"CLASSIFY" target support'
118 depends on NETFILTER_XTABLES
120 <file:Documentation/modules.txt>. The module will be called
121 ipt_CONNMARK.o. If unsure, say `N'.
123 +config NETFILTER_XT_TARGET_DELUDE
124 + tristate '"DELUDE" target support'
125 + depends on NETFILTER_XTABLES
127 + This option adds a `DELUDE' target.
129 + To compile it as a module, choose M here. If unsure, say N.
131 config NETFILTER_XT_TARGET_DSCP
132 tristate '"DSCP" target support'
133 depends on NETFILTER_XTABLES
136 To compile it as a module, choose M here. If unsure, say N.
138 +config NETFILTER_XT_MATCH_PORTSCAN
139 + tristate '"portscan" match support'
140 + depends on NETFILTER_XTABLES
142 + This option adds a 'portscan' match support.
144 + To compile it as a module, choose M here. If unsure, say N.
146 config NETFILTER_XT_MATCH_MULTIPORT
147 tristate "Multiple port match support"
148 depends on NETFILTER_XTABLES
149 diff -urN linux-2.6.21.1.old/net/netfilter/Makefile linux-2.6.21.1.dev/net/netfilter/Makefile
150 --- linux-2.6.21.1.old/net/netfilter/Makefile 2007-04-27 23:49:26.000000000 +0200
151 +++ linux-2.6.21.1.dev/net/netfilter/Makefile 2007-05-26 20:40:11.003183680 +0200
153 obj-$(CONFIG_NETFILTER_XTABLES) += x_tables.o xt_tcpudp.o
156 +obj-$(CONFIG_NETFILTER_XT_TARGET_CHAOS) += xt_CHAOS.o
157 obj-$(CONFIG_NETFILTER_XT_TARGET_CLASSIFY) += xt_CLASSIFY.o
158 obj-$(CONFIG_NETFILTER_XT_TARGET_CONNMARK) += xt_CONNMARK.o
159 +obj-$(CONFIG_NETFILTER_XT_TARGET_DELUDE) += xt_DELUDE.o
160 obj-$(CONFIG_NETFILTER_XT_TARGET_DSCP) += xt_DSCP.o
161 obj-$(CONFIG_NETFILTER_XT_TARGET_MARK) += xt_MARK.o
162 obj-$(CONFIG_NETFILTER_XT_TARGET_NFQUEUE) += xt_NFQUEUE.o
164 obj-$(CONFIG_NETFILTER_XT_MATCH_MARK) += xt_mark.o
165 obj-$(CONFIG_NETFILTER_XT_MATCH_MULTIPORT) += xt_multiport.o
166 obj-$(CONFIG_NETFILTER_XT_MATCH_POLICY) += xt_policy.o
167 +obj-$(CONFIG_NETFILTER_XT_MATCH_PORTSCAN) += xt_portscan.o
168 obj-$(CONFIG_NETFILTER_XT_MATCH_PKTTYPE) += xt_pkttype.o
169 obj-$(CONFIG_NETFILTER_XT_MATCH_QUOTA) += xt_quota.o
170 obj-$(CONFIG_NETFILTER_XT_MATCH_REALM) += xt_realm.o
171 diff -urN linux-2.6.21.1.old/net/netfilter/xt_CHAOS.c linux-2.6.21.1.dev/net/netfilter/xt_CHAOS.c
172 --- linux-2.6.21.1.old/net/netfilter/xt_CHAOS.c 1970-01-01 01:00:00.000000000 +0100
173 +++ linux-2.6.21.1.dev/net/netfilter/xt_CHAOS.c 2007-05-26 20:40:11.004183528 +0200
176 + CHAOS target for netfilter
178 + Copyright © Jan Engelhardt <jengelh [at] gmx de>, 2006 - 2007
179 + This program is free software; you can redistribute it and/or modify
180 + it under the terms of the GNU General Public License version 2 as
181 + published by the Free Software Foundation.
183 +#include <linux/icmp.h>
184 +#include <linux/in.h>
185 +#include <linux/ip.h>
186 +#include <linux/module.h>
187 +#include <linux/skbuff.h>
188 +#include <linux/stat.h>
189 +#include <linux/netfilter/x_tables.h>
190 +#include <linux/netfilter/xt_tcpudp.h>
191 +#include <linux/netfilter_ipv4/ipt_REJECT.h>
193 +#include <linux/netfilter/xt_CHAOS.h>
194 +#include "find_match.c"
195 +#include <linux/netfilter/oot_trans.h>
196 +#define PFX KBUILD_MODNAME ": "
198 +/* Module parameters */
199 +static unsigned int reject_percentage = ~0U * .01;
200 +static unsigned int delude_percentage = ~0U * .0101;
201 +module_param(reject_percentage, uint, S_IRUGO | S_IWUSR);
202 +module_param(delude_percentage, uint, S_IRUGO | S_IWUSR);
204 +/* References to other matches/targets */
205 +static struct xt_match *xm_tcp;
206 +static struct xt_target *xt_delude, *xt_reject, *xt_tarpit;
208 +static int have_delude, have_tarpit;
210 +/* Static data for other matches/targets */
211 +static const struct ipt_reject_info reject_params = {
212 + .with = ICMP_HOST_UNREACH,
215 +static const struct xt_tcp tcp_params = {
220 +/* CHAOS functions */
221 +static void xt_chaos_total(const struct xt_chaos_info *info,
222 + struct sk_buff **pskb, const struct net_device *in,
223 + const struct net_device *out, unsigned int hooknum)
225 + const int protoff = 4 * (*pskb)->nh.iph->ihl;
226 + const int offset = ntohs((*pskb)->nh.iph->frag_off) & IP_OFFSET;
227 + const struct xt_target *destiny;
228 + int hotdrop = 0, ret;
230 + ret = xm_tcp->match(*pskb, in, out, xm_tcp, &tcp_params,
231 + offset, protoff, &hotdrop);
232 + if(!ret || hotdrop || (unsigned int)net_random() > delude_percentage)
235 + destiny = (info->variant == XTCHAOS_TARPIT) ? xt_tarpit : xt_delude;
236 +#ifdef HAVE_TARGUSERINFO
237 + destiny->target(pskb, in, out, hooknum, destiny, NULL, NULL);
239 + destiny->target(pskb, in, out, hooknum, destiny, NULL);
244 +static unsigned int xt_chaos_target(struct sk_buff **pskb,
245 + const struct net_device *in, const struct net_device *out,
246 + unsigned int hooknum, const struct xt_target *target, const void *targinfo
247 +#ifdef HAVE_TARGUSERINFO
254 + * -A chaos -m statistic --mode random --probability \
255 + * $reject_percentage -j REJECT --reject-with host-unreach;
256 + * -A chaos -p tcp -m statistic --mode random --probability \
257 + * $delude_percentage -j DELUDE;
258 + * -A chaos -j DROP;
260 + const struct xt_chaos_info *info = targinfo;
262 + if((unsigned int)net_random() <= reject_percentage)
263 +#ifdef HAVE_TARGUSERINFO
264 + return xt_reject->target(pskb, in, out, hooknum, target,
265 + &reject_params, userinfo);
267 + return xt_reject->target(pskb, in, out, hooknum, target,
271 + /* TARPIT/DELUDE may not be called from the OUTPUT chain */
272 + if((*pskb)->nh.iph->protocol == IPPROTO_TCP &&
273 + info->variant != XTCHAOS_NORMAL && hooknum != NF_IP_LOCAL_OUT)
274 + xt_chaos_total(info, pskb, in, out, hooknum);
279 +static int xt_chaos_checkentry(const char *tablename, const void *entry,
280 + const struct xt_target *target, void *targinfo,
281 +#ifdef HAVE_TARGINFOSIZE
282 + unsigned int targinfosize,
284 + unsigned int hook_mask)
286 + const struct xt_chaos_info *info = targinfo;
287 + if(info->variant == XTCHAOS_DELUDE && !have_delude) {
288 + printk(KERN_WARNING PFX "Error: Cannot use --delude when "
289 + "DELUDE module not available\n");
292 + if(info->variant == XTCHAOS_TARPIT && !have_tarpit) {
293 + printk(KERN_WARNING PFX "Error: Cannot use --tarpit when "
294 + "TARPIT module not available\n");
300 +static struct xt_target xt_chaos_info = {
302 + .target = xt_chaos_target,
303 + .checkentry = xt_chaos_checkentry,
305 + .targetsize = sizeof(struct xt_chaos_info),
306 + .hooks = (1 << NF_IP_LOCAL_IN) | (1 << NF_IP_FORWARD) |
307 + (1 << NF_IP_LOCAL_OUT),
312 +static int __init xt_chaos_init(void)
316 + xm_tcp = xt_request_find_match(AF_INET, "tcp", 0);
317 + if(xm_tcp == NULL) {
318 + printk(KERN_WARNING PFX "Error: Could not find or load "
319 + "\"tcp\" match\n");
323 + xt_reject = xt_request_find_target(AF_INET, "REJECT", 0);
324 + if(xt_reject == NULL) {
325 + printk(KERN_WARNING PFX "Error: Could not find or load "
326 + "\"REJECT\" target\n");
330 + xt_tarpit = xt_request_find_target(AF_INET, "TARPIT", 0);
331 + have_tarpit = xt_tarpit != NULL;
333 + printk(KERN_WARNING PFX "Warning: Could not find or load "
334 + "\"TARPIT\" target\n");
336 + xt_delude = xt_request_find_target(AF_INET, "DELUDE", 0);
337 + have_delude = xt_delude != NULL;
339 + printk(KERN_WARNING PFX "Warning: Could not find or load "
340 + "\"DELUDE\" target\n");
342 + if((ret = xt_register_target(&xt_chaos_info)) != 0) {
343 + printk(KERN_WARNING PFX "xt_register_target returned "
344 + "error %d\n", ret);
352 + module_put(xt_delude->me);
354 + module_put(xt_tarpit->me);
355 + module_put(xt_reject->me);
357 + module_put(xm_tcp->me);
361 +static void __exit xt_chaos_exit(void)
363 + xt_unregister_target(&xt_chaos_info);
364 + module_put(xm_tcp->me);
365 + module_put(xt_reject->me);
367 + module_put(xt_delude->me);
369 + module_put(xt_tarpit->me);
373 +module_init(xt_chaos_init);
374 +module_exit(xt_chaos_exit);
375 +MODULE_AUTHOR("Jan Engelhardt <jengelh@gmx.de>");
376 +MODULE_DESCRIPTION("netfilter CHAOS target");
377 +MODULE_LICENSE("GPL");
378 +MODULE_ALIAS("ipt_CHAOS");
379 diff -urN linux-2.6.21.1.old/net/netfilter/xt_DELUDE.c linux-2.6.21.1.dev/net/netfilter/xt_DELUDE.c
380 --- linux-2.6.21.1.old/net/netfilter/xt_DELUDE.c 1970-01-01 01:00:00.000000000 +0100
381 +++ linux-2.6.21.1.dev/net/netfilter/xt_DELUDE.c 2007-05-26 20:40:11.004183528 +0200
385 + Copyright © Jan Engelhardt <jengelh [at] gmx de>, 2007
387 + Based upon linux-2.6.18.5/net/ipv4/netfilter/ipt_REJECT.c:
388 + (C) 1999-2001 Paul `Rusty' Russell
389 + (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
391 + xt_DELUDE acts like REJECT, but does reply with SYN-ACK on SYN.
393 + This program is free software; you can redistribute it and/or modify
394 + it under the terms of the GNU General Public License version 2 as
395 + published by the Free Software Foundation.
397 +#include <linux/module.h>
398 +#include <linux/skbuff.h>
399 +#include <linux/ip.h>
400 +#include <linux/random.h>
401 +#include <linux/tcp.h>
402 +#include <linux/udp.h>
403 +#include <linux/icmp.h>
404 +#include <net/icmp.h>
406 +#include <net/tcp.h>
407 +#include <net/route.h>
408 +#include <net/dst.h>
409 +#include <linux/netfilter_ipv4/ip_tables.h>
410 +#ifdef CONFIG_BRIDGE_NETFILTER
411 +# include <linux/netfilter_bridge.h>
413 +#include <linux/netfilter/oot_trans.h>
414 +#define PFX KBUILD_MODNAME ": "
416 +static inline struct rtable *route_reverse(struct sk_buff *skb,
417 + struct tcphdr *tcph, int hook)
419 + struct iphdr *iph = skb->nh.iph;
420 + struct dst_entry *odst;
421 + struct flowi fl = {};
424 + /* We don't require ip forwarding to be enabled to be able to
425 + * send a RST reply for bridged traffic. */
426 + if (hook != NF_IP_FORWARD
427 +#ifdef CONFIG_BRIDGE_NETFILTER
428 + || (skb->nf_bridge && skb->nf_bridge->mask & BRNF_BRIDGED)
431 + fl.nl_u.ip4_u.daddr = iph->saddr;
432 + if (hook == NF_IP_LOCAL_IN)
433 + fl.nl_u.ip4_u.saddr = iph->daddr;
434 + fl.nl_u.ip4_u.tos = RT_TOS(iph->tos);
436 + if (ip_route_output_key(&rt, &fl) != 0)
439 + /* non-local src, find valid iif to satisfy
440 + * rp-filter when calling ip_route_input. */
441 + fl.nl_u.ip4_u.daddr = iph->daddr;
442 + if (ip_route_output_key(&rt, &fl) != 0)
446 + if (ip_route_input(skb, iph->saddr, iph->daddr,
447 + RT_TOS(iph->tos), rt->u.dst.dev) != 0) {
448 + dst_release(&rt->u.dst);
451 + dst_release(&rt->u.dst);
452 + rt = (struct rtable *)skb->dst;
455 + fl.nl_u.ip4_u.daddr = iph->saddr;
456 + fl.nl_u.ip4_u.saddr = iph->daddr;
457 + fl.nl_u.ip4_u.tos = RT_TOS(iph->tos);
460 + if (rt->u.dst.error) {
461 + dst_release(&rt->u.dst);
465 + fl.proto = IPPROTO_TCP;
466 + fl.fl_ip_sport = tcph->dest;
467 + fl.fl_ip_dport = tcph->source;
469 + xfrm_lookup((struct dst_entry **)&rt, &fl, NULL, 0);
474 +static void send_reset(struct sk_buff *oldskb, int hook)
476 + struct sk_buff *nskb;
477 + struct iphdr *iph = oldskb->nh.iph;
478 + struct tcphdr _otcph, *oth, *tcph;
482 + unsigned int addr_type;
484 + /* IP header checks: fragment. */
485 + if (oldskb->nh.iph->frag_off & htons(IP_OFFSET))
488 + oth = skb_header_pointer(oldskb, oldskb->nh.iph->ihl * 4,
489 + sizeof(_otcph), &_otcph);
493 + /* No RST for RST. */
497 + /* Check checksum */
498 + if (nf_ip_checksum(oldskb, hook, iph->ihl * 4, IPPROTO_TCP))
501 + /* We need a linear, writeable skb. We also need to expand
502 + headroom in case hh_len of incoming interface < hh_len of
503 + outgoing interface */
504 + nskb = skb_copy_expand(oldskb, LL_MAX_HEADER, skb_tailroom(oldskb),
509 + /* This packet will not be the same as the other: clear nf fields */
512 + skb_init_secmark(nskb);
514 + skb_shinfo(nskb)->gso_size = 0;
515 + skb_shinfo(nskb)->gso_segs = 0;
516 + skb_shinfo(nskb)->gso_type = 0;
518 + tcph = (struct tcphdr *)((u_int32_t*)nskb->nh.iph + nskb->nh.iph->ihl);
520 + /* Swap source and dest */
521 + tmp_addr = nskb->nh.iph->saddr;
522 + nskb->nh.iph->saddr = nskb->nh.iph->daddr;
523 + nskb->nh.iph->daddr = tmp_addr;
524 + tmp_port = tcph->source;
525 + tcph->source = tcph->dest;
526 + tcph->dest = tmp_port;
528 + /* Truncate to length (no data) */
529 + tcph->doff = sizeof(struct tcphdr)/4;
530 + skb_trim(nskb, nskb->nh.iph->ihl*4 + sizeof(struct tcphdr));
531 + nskb->nh.iph->tot_len = htons(nskb->len);
533 + if(oth->syn && !oth->ack && !oth->rst && !oth->fin) {
534 + /* DELUDE essential part */
535 + tcph->ack_seq = htonl(ntohl(oth->seq) + oth->syn + oth->fin +
536 + oldskb->len - oldskb->nh.iph->ihl * 4 -
538 + tcph->seq = htonl(secure_tcp_sequence_number(
539 + nskb->nh.iph->saddr, nskb->nh.iph->daddr,
540 + tcph->source, tcph->dest));
545 + tcph->ack_seq = htonl(ntohl(oth->seq) + oth->syn + oth->fin
546 + + oldskb->len - oldskb->nh.iph->ihl*4
551 + tcph->seq = oth->ack_seq;
556 + ((u_int8_t *)tcph)[13] = 0;
558 + tcph->ack = needs_ack;
565 + /* Adjust TCP checksum */
567 + tcph->check = tcp_v4_check(tcph, sizeof(struct tcphdr),
568 + nskb->nh.iph->saddr,
569 + nskb->nh.iph->daddr,
570 + csum_partial((char *)tcph,
571 + sizeof(struct tcphdr), 0));
573 + /* Set DF, id = 0 */
574 + nskb->nh.iph->frag_off = htons(IP_DF);
575 + nskb->nh.iph->id = 0;
577 + addr_type = RTN_UNSPEC;
578 + if (hook != NF_IP_FORWARD
579 +#ifdef CONFIG_BRIDGE_NETFILTER
580 + || (nskb->nf_bridge && nskb->nf_bridge->mask & BRNF_BRIDGED)
583 + addr_type = RTN_LOCAL;
585 + if (ip_route_me_harder(&nskb, addr_type))
588 + nskb->ip_summed = CHECKSUM_NONE;
590 + /* Adjust IP TTL */
591 + nskb->nh.iph->ttl = dst_metric(nskb->dst, RTAX_HOPLIMIT);
593 + /* Adjust IP checksum */
594 + nskb->nh.iph->check = 0;
595 + nskb->nh.iph->check = ip_fast_csum((unsigned char *)nskb->nh.iph,
596 + nskb->nh.iph->ihl);
598 + /* "Never happens" */
599 + if (nskb->len > dst_mtu(nskb->dst))
602 + nf_ct_attach(nskb, oldskb);
604 + NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, nskb, NULL, nskb->dst->dev,
612 +static unsigned int xt_delude_target(struct sk_buff **pskb,
613 + const struct net_device *in, const struct net_device *out,
614 + unsigned int hooknum, const struct xt_target *target, const void *targinfo
615 +#ifdef HAVE_TARGUSERINFO
621 + /* WARNING: This code causes reentry within iptables.
622 + This means that the iptables jump stack is now crap. We
623 + must return an absolute verdict. --RR */
624 + send_reset(*pskb, hooknum);
628 +static int xt_delude_check(const char *tablename, const void *e_void,
629 + const struct xt_target *target, void *targinfo,
630 +#ifdef HAVE_TARGINFOSIZE
631 + unsigned int targinfosize,
633 + unsigned int hook_mask)
635 + if(hook_mask & ~((1 << NF_IP_LOCAL_IN) | (1 << NF_IP_FORWARD))) {
636 + printk(KERN_WARNING PFX "DELUDE may not be used in chains "
637 + "other than INPUT and FORWARD\n");
643 +static struct xt_target xt_delude_info = {
645 + .target = xt_delude_target,
646 + .checkentry = xt_delude_check,
648 + .hooks = (1 << NF_IP_LOCAL_IN) | (1 << NF_IP_FORWARD) |
649 + (1 << NF_IP_LOCAL_OUT),
650 + .proto = IPPROTO_TCP,
655 +static int __init xt_delude_init(void)
657 + return xt_register_target(&xt_delude_info);
660 +static void __exit xt_delude_exit(void)
662 + xt_unregister_target(&xt_delude_info);
665 +module_init(xt_delude_init);
666 +module_exit(xt_delude_exit);
667 +MODULE_AUTHOR("Jan Engelhardt <jengelh@gmx.de>");
668 +MODULE_DESCRIPTION("netfilter DELUDE target");
669 +MODULE_LICENSE("GPL");
670 +MODULE_ALIAS("ipt_DELUDE");
671 diff -urN linux-2.6.21.1.old/net/netfilter/xt_portscan.c linux-2.6.21.1.dev/net/netfilter/xt_portscan.c
672 --- linux-2.6.21.1.old/net/netfilter/xt_portscan.c 1970-01-01 01:00:00.000000000 +0100
673 +++ linux-2.6.21.1.dev/net/netfilter/xt_portscan.c 2007-05-26 20:40:11.004183528 +0200
676 + portscan match for netfilter
678 + Written by Jan Engelhardt, 2006 - 2007
679 + This program is free software; you can redistribute it and/or modify
680 + it under the terms of the GNU General Public License version 2 as
681 + published by the Free Software Foundation.
683 +#include <linux/in.h>
684 +#include <linux/ip.h>
685 +#include <linux/module.h>
686 +#include <linux/moduleparam.h>
687 +#include <linux/skbuff.h>
688 +#include <linux/stat.h>
689 +#include <linux/tcp.h>
690 +#include <linux/types.h>
691 +#include <linux/version.h>
692 +#include <linux/netfilter/x_tables.h>
693 +#include <linux/netfilter/xt_tcpudp.h>
694 +#include <linux/netfilter/oot_conntrack.h>
695 +#include <linux/netfilter/xt_portscan.h>
696 +#include <linux/netfilter/oot_trans.h>
697 +#define PFX KBUILD_MODNAME ": "
700 + TCP_FLAGS_ALL3 = TCP_FLAG_FIN | TCP_FLAG_RST | TCP_FLAG_SYN,
701 + TCP_FLAGS_ALL4 = TCP_FLAGS_ALL3 | TCP_FLAG_ACK,
702 + TCP_FLAGS_ALL6 = TCP_FLAGS_ALL4 | TCP_FLAG_PSH | TCP_FLAG_URG,
705 +/* Module parameters */
707 + connmark_mask = ~0,
712 + mark_synscan = 0x3,
719 +module_param(connmark_mask, uint, S_IRUGO | S_IWUSR);
720 +module_param(packet_mask, uint, S_IRUGO | S_IWUSR);
721 +module_param(mark_seen, uint, S_IRUGO | S_IWUSR);
722 +module_param(mark_synrcv, uint, S_IRUGO | S_IWUSR);
723 +module_param(mark_closed, uint, S_IRUGO | S_IWUSR);
724 +module_param(mark_synscan, uint, S_IRUGO | S_IWUSR);
725 +module_param(mark_estab1, uint, S_IRUGO | S_IWUSR);
726 +module_param(mark_estab2, uint, S_IRUGO | S_IWUSR);
727 +module_param(mark_cnscan, uint, S_IRUGO | S_IWUSR);
728 +module_param(mark_grscan, uint, S_IRUGO | S_IWUSR);
729 +module_param(mark_valid, uint, S_IRUGO | S_IWUSR);
730 +MODULE_PARM_DESC(connmark_mask, "only set specified bits in connection mark");
731 +MODULE_PARM_DESC(packet_mask, "only set specified bits in packet mark");
732 +MODULE_PARM_DESC(mark_seen, "nfmark value for packet-seen state");
733 +MODULE_PARM_DESC(mark_synrcv, "connmark value for SYN Received state");
734 +MODULE_PARM_DESC(mark_closed, "connmark value for closed state");
735 +MODULE_PARM_DESC(mark_synscan, "connmark value for SYN Scan state");
736 +MODULE_PARM_DESC(mark_estab1, "connmark value for Established-1 state");
737 +MODULE_PARM_DESC(mark_estab2, "connmark value for Established-2 state");
738 +MODULE_PARM_DESC(mark_cnscan, "connmark value for Connect Scan state");
739 +MODULE_PARM_DESC(mark_grscan, "connmark value for Grab Scan state");
740 +MODULE_PARM_DESC(mark_valid, "connmark value for Valid state");
742 +/* TCP flag functions */
743 +static inline int tflg_ack4(const struct tcphdr *th)
745 + return (tcp_flag_word(th) & TCP_FLAGS_ALL4) == TCP_FLAG_ACK;
748 +static inline int tflg_ack6(const struct tcphdr *th)
750 + return (tcp_flag_word(th) & TCP_FLAGS_ALL6) == TCP_FLAG_ACK;
753 +static inline int tflg_fin(const struct tcphdr *th)
755 + return (tcp_flag_word(th) & TCP_FLAGS_ALL3) == TCP_FLAG_FIN;
758 +static inline int tflg_rst(const struct tcphdr *th)
760 + return (tcp_flag_word(th) & TCP_FLAGS_ALL3) == TCP_FLAG_RST;
763 +static inline int tflg_rstack(const struct tcphdr *th)
765 + return (tcp_flag_word(th) & TCP_FLAGS_ALL4) ==
766 + (TCP_FLAG_ACK | TCP_FLAG_RST);
769 +static inline int tflg_syn(const struct tcphdr *th)
771 + return (tcp_flag_word(th) & TCP_FLAGS_ALL4) == TCP_FLAG_SYN;
774 +static inline int tflg_synack(const struct tcphdr *th)
776 + return (tcp_flag_word(th) & TCP_FLAGS_ALL4) ==
777 + (TCP_FLAG_SYN | TCP_FLAG_ACK);
780 +/* portscan functions */
781 +static inline int xt_portscan_stealth(const struct tcphdr *th)
784 + * "Connection refused" replies to our own probes must not be matched.
786 + if(tflg_rstack(th))
789 + if(tflg_rst(th) && printk_ratelimit()) {
790 + printk(KERN_WARNING PFX "Warning: Pure RST received\n");
795 + * -p tcp ! --syn -m conntrack --ctstate INVALID: Looking for non-start
796 + * packets that are not associated with any connection -- this will
797 + * match most scan types (NULL, XMAS, FIN) and ridiculous flag
798 + * combinations (SYN-RST, SYN-FIN, SYN-FIN-RST, FIN-RST, etc.).
800 + return !tflg_syn(th);
803 +static inline int xt_portscan_full(int mark, enum ip_conntrack_info ctstate,
804 + int loopback, const struct tcphdr *tcph, int payload_len)
806 + if(mark == mark_estab2) {
808 + * -m connmark --mark $ESTAB2
810 + if(tflg_ack4(tcph) && payload_len == 0)
811 + return mark; /* keep mark */
812 + else if(tflg_rst(tcph) || tflg_fin(tcph))
813 + return mark_grscan;
816 + } else if(mark == mark_estab1) {
818 + * -m connmark --mark $ESTAB1
820 + if(tflg_rst(tcph) || tflg_fin(tcph))
821 + return mark_cnscan;
822 + else if(!loopback && tflg_ack4(tcph) && payload_len == 0)
823 + return mark_estab2;
826 + } else if(mark == mark_synrcv) {
828 + * -m connmark --mark $SYN
830 + if(loopback && tflg_synack(tcph))
831 + return mark; /* keep mark */
832 + else if(loopback && tflg_rstack(tcph))
833 + return mark_closed;
834 + else if(tflg_ack6(tcph))
835 + return mark_estab1;
837 + return mark_synscan;
838 + } else if(ctstate == IP_CT_NEW && tflg_syn(tcph)) {
840 + * -p tcp --syn --ctstate NEW
842 + return mark_synrcv;
847 +static int xt_portscan_match(const struct sk_buff *skb,
848 + const struct net_device *in, const struct net_device *out,
849 + const struct xt_match *match, const void *matchinfo, int offset,
850 + unsigned int protoff, int *hotdrop)
852 + const struct xt_portscan_info *info = matchinfo;
853 + enum ip_conntrack_info ctstate;
854 + struct ip_conntrack *ctdata;
855 + const struct tcphdr *tcph;
856 + struct tcphdr tcph_buf;
858 + tcph = skb_header_pointer(skb, protoff, sizeof(tcph_buf), &tcph_buf);
862 + /* Check for invalid packets: -m conntrack --ctstate INVALID */
863 + if((ctdata = ip_conntrack_get(skb, &ctstate)) == NULL) {
864 + if(info->match_stealth)
865 + return xt_portscan_stealth(tcph);
867 + * If @ctdata is NULL, we cannot match the other scan
874 + * If -m portscan was previously applied to this packet, the rules we
875 + * simulate must not be run through again. And for speedup, do not call
876 + * it either when the connection is already VALID.
878 + if((ctdata->mark & connmark_mask) == mark_valid ||
879 + (skb->nfmark & packet_mask) != mark_seen)
882 + n = xt_portscan_full(ctdata->mark & connmark_mask, ctstate,
883 + in == &loopback_dev, tcph,
884 + skb->len - protoff - 4 * tcph->doff);
886 + ctdata->mark = (ctdata->mark & ~connmark_mask) | n;
887 + ((struct sk_buff *)skb)->nfmark =
888 + (skb->nfmark & ~packet_mask) | mark_seen;
891 + return (info->match_syn && ctdata->mark == mark_synscan) ||
892 + (info->match_cn && ctdata->mark == mark_cnscan) ||
893 + (info->match_gr && ctdata->mark == mark_grscan);
896 +static int xt_portscan_checkentry(const char *tablename, const void *entry,
897 + const struct xt_match *match, void *matchinfo,
898 +#ifdef HAVE_MATCHINFOSIZE
899 + unsigned int matchinfosize,
901 + unsigned int hook_mask)
903 + const struct xt_portscan_info *info = matchinfo;
904 +#ifdef HAVE_MATCHINFOSIZE
905 + if(matchinfosize != XT_ALIGN(sizeof(struct xt_portscan_info))) {
906 + printk(KERN_WARNING PFX "matchinfosize %u != %Zu\n",
908 + XT_ALIGN(sizeof(struct xt_portscan_info)));
912 + if((info->match_stealth & ~1) || (info->match_syn & ~1) ||
913 + (info->match_cn & ~1) || (info->match_gr & ~1)) {
914 + printk(KERN_WARNING PFX "Invalid flags\n");
920 +static struct xt_match xt_portscan = {
921 + .name = "portscan",
922 + .match = xt_portscan_match,
923 + .checkentry = xt_portscan_checkentry,
924 + .matchsize = sizeof(struct xt_portscan_info),
925 + .proto = IPPROTO_TCP,
930 +static int __init xt_portscan_init(void)
932 + return xt_register_match(&xt_portscan);
935 +static void __exit xt_portscan_exit(void)
937 + xt_unregister_match(&xt_portscan);
941 +module_init(xt_portscan_init);
942 +module_exit(xt_portscan_exit);
943 +MODULE_AUTHOR("Jan Engelhardt <jengelh@gmx.de>");
944 +MODULE_DESCRIPTION("netfilter portscan match module");
945 +MODULE_LICENSE("GPL");
946 +MODULE_ALIAS("ipt_portscan");