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