break trunk temporary - upgrade to 2.6.21.1 and iptables 1.3.7
[openwrt/svn-archive/archive.git] / target / linux / generic-2.6 / patches / 170-netfilter_chaostables.patch
1 diff -Nur linux-2.6.21.1/include/linux/netfilter/oot_conntrack.h linux-2.6.21.1-owrt/include/linux/netfilter/oot_conntrack.h
2 --- linux-2.6.21.1/include/linux/netfilter/oot_conntrack.h 1970-01-01 01:00:00.000000000 +0100
3 +++ linux-2.6.21.1-owrt/include/linux/netfilter/oot_conntrack.h 2007-05-14 14:18:54.000000000 +0200
4 @@ -0,0 +1,5 @@
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>
9 +#endif
10 diff -Nur linux-2.6.21.1/include/linux/netfilter/oot_trans.h linux-2.6.21.1-owrt/include/linux/netfilter/oot_trans.h
11 --- linux-2.6.21.1/include/linux/netfilter/oot_trans.h 1970-01-01 01:00:00.000000000 +0100
12 +++ linux-2.6.21.1-owrt/include/linux/netfilter/oot_trans.h 2007-05-14 14:18:54.000000000 +0200
13 @@ -0,0 +1,14 @@
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
20 +#endif
21 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
22 +# define nfmark mark
23 +#endif
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))
27 +#endif
28 diff -Nur linux-2.6.21.1/include/linux/netfilter/xt_CHAOS.h linux-2.6.21.1-owrt/include/linux/netfilter/xt_CHAOS.h
29 --- linux-2.6.21.1/include/linux/netfilter/xt_CHAOS.h 1970-01-01 01:00:00.000000000 +0100
30 +++ linux-2.6.21.1-owrt/include/linux/netfilter/xt_CHAOS.h 2007-05-14 14:18:54.000000000 +0200
31 @@ -0,0 +1,14 @@
32 +#ifndef _LINUX_XT_CHAOS_H
33 +#define _LINUX_XT_CHAOS_H 1
34 +
35 +enum xt_chaos_variant {
36 + XTCHAOS_NORMAL,
37 + XTCHAOS_TARPIT,
38 + XTCHAOS_DELUDE,
39 +};
40 +
41 +struct xt_chaos_info {
42 + enum xt_chaos_variant variant;
43 +};
44 +
45 +#endif /* _LINUX_XT_CHAOS_H */
46 diff -Nur linux-2.6.21.1/include/linux/netfilter/xt_portscan.h linux-2.6.21.1-owrt/include/linux/netfilter/xt_portscan.h
47 --- linux-2.6.21.1/include/linux/netfilter/xt_portscan.h 1970-01-01 01:00:00.000000000 +0100
48 +++ linux-2.6.21.1-owrt/include/linux/netfilter/xt_portscan.h 2007-05-14 14:18:54.000000000 +0200
49 @@ -0,0 +1,8 @@
50 +#ifndef _LINUX_XT_PORTSCAN_H
51 +#define _LINUX_XT_PORTSCAN_H 1
52 +
53 +struct xt_portscan_info {
54 + unsigned int match_stealth, match_syn, match_cn, match_gr;
55 +};
56 +
57 +#endif /* _LINUX_XT_PORTSCAN_H */
58 diff -Nur linux-2.6.21.1/net/netfilter/find_match.c linux-2.6.21.1-owrt/net/netfilter/find_match.c
59 --- linux-2.6.21.1/net/netfilter/find_match.c 1970-01-01 01:00:00.000000000 +0100
60 +++ linux-2.6.21.1-owrt/net/netfilter/find_match.c 2007-05-14 14:18:54.000000000 +0200
61 @@ -0,0 +1,39 @@
62 +/*
63 + xt_request_find_match
64 + by Jan Engelhardt <jengelh [at] gmx de>, 2006 - 2007
65 +
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.
71 +*/
72 +#include <linux/err.h>
73 +#include <linux/netfilter_arp.h>
74 +#include <linux/socket.h>
75 +#include <linux/netfilter/x_tables.h>
76 +
77 +/*
78 + * Yeah this code is sub-optimal, but the function is missing in
79 + * mainline so far. -jengelh
80 + */
81 +static struct xt_match *xt_request_find_match_lo(int af, const char *name,
82 + u8 revision)
83 +{
84 + static const char *const xt_prefix[] = {
85 + [AF_INET] = "ip",
86 + [AF_INET6] = "ip6",
87 + [NF_ARP] = "arp",
88 + };
89 + struct xt_match *match;
90 +
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)
94 + return NULL;
95 +
96 + return match;
97 +}
98 +
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 -Nur linux-2.6.21.1/net/netfilter/Kconfig linux-2.6.21.1-owrt/net/netfilter/Kconfig
102 --- linux-2.6.21.1/net/netfilter/Kconfig 2007-04-27 23:49:26.000000000 +0200
103 +++ linux-2.6.21.1-owrt/net/netfilter/Kconfig 2007-05-14 14:30:47.000000000 +0200
104 @@ -287,6 +287,14 @@
105
106 # alphabetically ordered list of targets
107
108 +config NETFILTER_XT_TARGET_CHAOS
109 + tristate '"CHAOS" target support'
110 + depends on NETFILTER_XTABLES
111 + help
112 + This option adds a `CHAOS' target.
113 +
114 + To compile it as a module, choose M here. If unsure, say N.
115 +
116 config NETFILTER_XT_TARGET_CLASSIFY
117 tristate '"CLASSIFY" target support'
118 depends on NETFILTER_XTABLES
119 @@ -315,6 +323,14 @@
120 <file:Documentation/modules.txt>. The module will be called
121 ipt_CONNMARK.o. If unsure, say `N'.
122
123 +config NETFILTER_XT_TARGET_DELUDE
124 + tristate '"DELUDE" target support'
125 + depends on NETFILTER_XTABLES
126 + help
127 + This option adds a `DELUDE' target.
128 +
129 + To compile it as a module, choose M here. If unsure, say N.
130 +
131 config NETFILTER_XT_TARGET_DSCP
132 tristate '"DSCP" target support'
133 depends on NETFILTER_XTABLES
134 @@ -563,6 +579,14 @@
135
136 To compile it as a module, choose M here. If unsure, say N.
137
138 +config NETFILTER_XT_MATCH_PORTSCAN
139 + tristate '"portscan" match support'
140 + depends on NETFILTER_XTABLES
141 + help
142 + This option adds a 'portscan' match support.
143 +
144 + To compile it as a module, choose M here. If unsure, say N.
145 +
146 config NETFILTER_XT_MATCH_MULTIPORT
147 tristate "Multiple port match support"
148 depends on NETFILTER_XTABLES
149 diff -Nur linux-2.6.21.1/net/netfilter/Makefile linux-2.6.21.1-owrt/net/netfilter/Makefile
150 --- linux-2.6.21.1/net/netfilter/Makefile 2007-04-27 23:49:26.000000000 +0200
151 +++ linux-2.6.21.1-owrt/net/netfilter/Makefile 2007-05-14 14:30:47.000000000 +0200
152 @@ -37,8 +37,10 @@
153 obj-$(CONFIG_NETFILTER_XTABLES) += x_tables.o xt_tcpudp.o
154
155 # targets
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
163 @@ -63,6 +65,7 @@
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 -Nur linux-2.6.21.1/net/netfilter/xt_CHAOS.c linux-2.6.21.1-owrt/net/netfilter/xt_CHAOS.c
172 --- linux-2.6.21.1/net/netfilter/xt_CHAOS.c 1970-01-01 01:00:00.000000000 +0100
173 +++ linux-2.6.21.1-owrt/net/netfilter/xt_CHAOS.c 2007-05-14 14:36:58.000000000 +0200
174 @@ -0,0 +1,204 @@
175 +/*
176 + CHAOS target for netfilter
177 +
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.
182 +*/
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>
192 +#include <net/ip.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 ": "
197 +
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);
203 +
204 +/* References to other matches/targets */
205 +static struct xt_match *xm_tcp;
206 +static struct xt_target *xt_delude, *xt_reject, *xt_tarpit;
207 +
208 +static int have_delude, have_tarpit;
209 +
210 +/* Static data for other matches/targets */
211 +static const struct ipt_reject_info reject_params = {
212 + .with = ICMP_HOST_UNREACH,
213 +};
214 +
215 +static const struct xt_tcp tcp_params = {
216 + .spts = {0, ~0},
217 + .dpts = {0, ~0},
218 +};
219 +
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)
224 +{
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;
229 +
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)
233 + return;
234 +
235 + destiny = (info->variant == XTCHAOS_TARPIT) ? xt_tarpit : xt_delude;
236 +#ifdef HAVE_TARGUSERINFO
237 + destiny->target(pskb, in, out, hooknum, destiny, NULL, NULL);
238 +#else
239 + destiny->target(pskb, in, out, hooknum, destiny, NULL);
240 +#endif
241 + return;
242 +}
243 +
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
248 + ,
249 + void *userinfo
250 +#endif
251 + )
252 +{
253 + /* Equivalent to:
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;
259 + */
260 + const struct xt_chaos_info *info = targinfo;
261 +
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);
266 +#else
267 + return xt_reject->target(pskb, in, out, hooknum, target,
268 + &reject_params);
269 +#endif
270 +
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);
275 +
276 + return NF_DROP;
277 +}
278 +
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,
283 +#endif
284 + unsigned int hook_mask)
285 +{
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");
290 + return 0;
291 + }
292 + if(info->variant == XTCHAOS_TARPIT && !have_tarpit) {
293 + printk(KERN_WARNING PFX "Error: Cannot use --tarpit when "
294 + "TARPIT module not available\n");
295 + return 0;
296 + }
297 + return 1;
298 +}
299 +
300 +static struct xt_target xt_chaos_info = {
301 + .name = "CHAOS",
302 + .target = xt_chaos_target,
303 + .checkentry = xt_chaos_checkentry,
304 + .table = "filter",
305 + .targetsize = sizeof(struct xt_chaos_info),
306 + .hooks = (1 << NF_IP_LOCAL_IN) | (1 << NF_IP_FORWARD) |
307 + (1 << NF_IP_LOCAL_OUT),
308 + .family = AF_INET,
309 + .me = THIS_MODULE,
310 +};
311 +
312 +static int __init xt_chaos_init(void)
313 +{
314 + int ret = -EINVAL;
315 +
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");
320 + return -EINVAL;
321 + }
322 +
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");
327 + goto out2;
328 + }
329 +
330 + xt_tarpit = xt_request_find_target(AF_INET, "TARPIT", 0);
331 + have_tarpit = xt_tarpit != NULL;
332 + if(!have_tarpit)
333 + printk(KERN_WARNING PFX "Warning: Could not find or load "
334 + "\"TARPIT\" target\n");
335 +
336 + xt_delude = xt_request_find_target(AF_INET, "DELUDE", 0);
337 + have_delude = xt_delude != NULL;
338 + if(!have_delude)
339 + printk(KERN_WARNING PFX "Warning: Could not find or load "
340 + "\"DELUDE\" target\n");
341 +
342 + if((ret = xt_register_target(&xt_chaos_info)) != 0) {
343 + printk(KERN_WARNING PFX "xt_register_target returned "
344 + "error %d\n", ret);
345 + goto out3;
346 + }
347 +
348 + return 0;
349 +
350 + out3:
351 + if(have_delude)
352 + module_put(xt_delude->me);
353 + if(have_tarpit)
354 + module_put(xt_tarpit->me);
355 + module_put(xt_reject->me);
356 + out2:
357 + module_put(xm_tcp->me);
358 + return ret;
359 +}
360 +
361 +static void __exit xt_chaos_exit(void)
362 +{
363 + xt_unregister_target(&xt_chaos_info);
364 + module_put(xm_tcp->me);
365 + module_put(xt_reject->me);
366 + if(have_delude)
367 + module_put(xt_delude->me);
368 + if(have_tarpit)
369 + module_put(xt_tarpit->me);
370 + return;
371 +}
372 +
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 -Nur linux-2.6.21.1/net/netfilter/xt_DELUDE.c linux-2.6.21.1-owrt/net/netfilter/xt_DELUDE.c
380 --- linux-2.6.21.1/net/netfilter/xt_DELUDE.c 1970-01-01 01:00:00.000000000 +0100
381 +++ linux-2.6.21.1-owrt/net/netfilter/xt_DELUDE.c 2007-05-14 14:53:12.000000000 +0200
382 @@ -0,0 +1,288 @@
383 +/*
384 + DELUDE target
385 + Copyright © Jan Engelhardt <jengelh [at] gmx de>, 2007
386 +
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>
390 +
391 + xt_DELUDE acts like REJECT, but does reply with SYN-ACK on SYN.
392 +
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.
396 +*/
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>
405 +#include <net/ip.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>
412 +#endif
413 +#include <linux/netfilter/oot_trans.h>
414 +#define PFX KBUILD_MODNAME ": "
415 +
416 +static inline struct rtable *route_reverse(struct sk_buff *skb,
417 + struct tcphdr *tcph, int hook)
418 +{
419 + struct iphdr *iph = skb->nh.iph;
420 + struct dst_entry *odst;
421 + struct flowi fl = {};
422 + struct rtable *rt;
423 +
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)
429 +#endif
430 + ) {
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);
435 +
436 + if (ip_route_output_key(&rt, &fl) != 0)
437 + return NULL;
438 + } else {
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)
443 + return NULL;
444 +
445 + odst = skb->dst;
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);
449 + return NULL;
450 + }
451 + dst_release(&rt->u.dst);
452 + rt = (struct rtable *)skb->dst;
453 + skb->dst = odst;
454 +
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);
458 + }
459 +
460 + if (rt->u.dst.error) {
461 + dst_release(&rt->u.dst);
462 + return NULL;
463 + }
464 +
465 + fl.proto = IPPROTO_TCP;
466 + fl.fl_ip_sport = tcph->dest;
467 + fl.fl_ip_dport = tcph->source;
468 +
469 + xfrm_lookup((struct dst_entry **)&rt, &fl, NULL, 0);
470 +
471 + return rt;
472 +}
473 +
474 +static void send_reset(struct sk_buff *oldskb, int hook)
475 +{
476 + struct sk_buff *nskb;
477 + struct iphdr *iph = oldskb->nh.iph;
478 + struct tcphdr _otcph, *oth, *tcph;
479 + __be16 tmp_port;
480 + __be32 tmp_addr;
481 + int needs_ack;
482 + unsigned int addr_type;
483 +
484 + /* IP header checks: fragment. */
485 + if (oldskb->nh.iph->frag_off & htons(IP_OFFSET))
486 + return;
487 +
488 + oth = skb_header_pointer(oldskb, oldskb->nh.iph->ihl * 4,
489 + sizeof(_otcph), &_otcph);
490 + if (oth == NULL)
491 + return;
492 +
493 + /* No RST for RST. */
494 + if (oth->rst)
495 + return;
496 +
497 + /* Check checksum */
498 + if (nf_ip_checksum(oldskb, hook, iph->ihl * 4, IPPROTO_TCP))
499 + return;
500 +
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),
505 + GFP_ATOMIC);
506 + if (!nskb)
507 + return;
508 +
509 + /* This packet will not be the same as the other: clear nf fields */
510 + nf_reset(nskb);
511 + nskb->nfmark = 0;
512 + skb_init_secmark(nskb);
513 +
514 + skb_shinfo(nskb)->gso_size = 0;
515 + skb_shinfo(nskb)->gso_segs = 0;
516 + skb_shinfo(nskb)->gso_type = 0;
517 +
518 + tcph = (struct tcphdr *)((u_int32_t*)nskb->nh.iph + nskb->nh.iph->ihl);
519 +
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;
527 +
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);
532 +
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 -
537 + (oth->doff << 2));
538 + tcph->seq = htonl(secure_tcp_sequence_number(
539 + nskb->nh.iph->saddr, nskb->nh.iph->daddr,
540 + tcph->source, tcph->dest));
541 + tcph->ack = 1;
542 + } else {
543 + if(!tcph->ack) {
544 + needs_ack = 1;
545 + tcph->ack_seq = htonl(ntohl(oth->seq) + oth->syn + oth->fin
546 + + oldskb->len - oldskb->nh.iph->ihl*4
547 + - (oth->doff<<2));
548 + tcph->seq = 0;
549 + } else {
550 + needs_ack = 0;
551 + tcph->seq = oth->ack_seq;
552 + tcph->ack_seq = 0;
553 + }
554 +
555 + /* Reset flags */
556 + ((u_int8_t *)tcph)[13] = 0;
557 + tcph->rst = 1;
558 + tcph->ack = needs_ack;
559 + }
560 +
561 +
562 + tcph->window = 0;
563 + tcph->urg_ptr = 0;
564 +
565 + /* Adjust TCP checksum */
566 + tcph->check = 0;
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));
572 +
573 + /* Set DF, id = 0 */
574 + nskb->nh.iph->frag_off = htons(IP_DF);
575 + nskb->nh.iph->id = 0;
576 +
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)
581 +#endif
582 + )
583 + addr_type = RTN_LOCAL;
584 +
585 + if (ip_route_me_harder(&nskb, addr_type))
586 + goto free_nskb;
587 +
588 + nskb->ip_summed = CHECKSUM_NONE;
589 +
590 + /* Adjust IP TTL */
591 + nskb->nh.iph->ttl = dst_metric(nskb->dst, RTAX_HOPLIMIT);
592 +
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);
597 +
598 + /* "Never happens" */
599 + if (nskb->len > dst_mtu(nskb->dst))
600 + goto free_nskb;
601 +
602 + nf_ct_attach(nskb, oldskb);
603 +
604 + NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, nskb, NULL, nskb->dst->dev,
605 + dst_output);
606 + return;
607 +
608 + free_nskb:
609 + kfree_skb(nskb);
610 +}
611 +
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
616 + ,
617 + void *userinfo
618 +#endif
619 + )
620 +{
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);
625 + return NF_DROP;
626 +}
627 +
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,
632 +#endif
633 + unsigned int hook_mask)
634 +{
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");
638 + return 0;
639 + }
640 + return 1;
641 +}
642 +
643 +static struct xt_target xt_delude_info = {
644 + .name = "DELUDE",
645 + .target = xt_delude_target,
646 + .checkentry = xt_delude_check,
647 + .table = "filter",
648 + .hooks = (1 << NF_IP_LOCAL_IN) | (1 << NF_IP_FORWARD) |
649 + (1 << NF_IP_LOCAL_OUT),
650 + .proto = IPPROTO_TCP,
651 + .family = AF_INET,
652 + .me = THIS_MODULE,
653 +};
654 +
655 +static int __init xt_delude_init(void)
656 +{
657 + return xt_register_target(&xt_delude_info);
658 +}
659 +
660 +static void __exit xt_delude_exit(void)
661 +{
662 + xt_unregister_target(&xt_delude_info);
663 +}
664 +
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 -Nur linux-2.6.21.1/net/netfilter/xt_portscan.c linux-2.6.21.1-owrt/net/netfilter/xt_portscan.c
672 --- linux-2.6.21.1/net/netfilter/xt_portscan.c 1970-01-01 01:00:00.000000000 +0100
673 +++ linux-2.6.21.1-owrt/net/netfilter/xt_portscan.c 2007-05-14 14:37:35.000000000 +0200
674 @@ -0,0 +1,272 @@
675 +/*
676 + portscan match for netfilter
677 +
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.
682 +*/
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 ": "
698 +
699 +enum {
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,
703 +};
704 +
705 +/* Module parameters */
706 +static unsigned int
707 + connmark_mask = ~0,
708 + packet_mask = ~0,
709 + mark_seen = 0x9,
710 + mark_synrcv = 0x1,
711 + mark_closed = 0x2,
712 + mark_synscan = 0x3,
713 + mark_estab1 = 0x4,
714 + mark_estab2 = 0x5,
715 + mark_cnscan = 0x6,
716 + mark_grscan = 0x7,
717 + mark_valid = 0x8;
718 +
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");
741 +
742 +/* TCP flag functions */
743 +static inline int tflg_ack4(const struct tcphdr *th)
744 +{
745 + return (tcp_flag_word(th) & TCP_FLAGS_ALL4) == TCP_FLAG_ACK;
746 +}
747 +
748 +static inline int tflg_ack6(const struct tcphdr *th)
749 +{
750 + return (tcp_flag_word(th) & TCP_FLAGS_ALL6) == TCP_FLAG_ACK;
751 +}
752 +
753 +static inline int tflg_fin(const struct tcphdr *th)
754 +{
755 + return (tcp_flag_word(th) & TCP_FLAGS_ALL3) == TCP_FLAG_FIN;
756 +}
757 +
758 +static inline int tflg_rst(const struct tcphdr *th)
759 +{
760 + return (tcp_flag_word(th) & TCP_FLAGS_ALL3) == TCP_FLAG_RST;
761 +}
762 +
763 +static inline int tflg_rstack(const struct tcphdr *th)
764 +{
765 + return (tcp_flag_word(th) & TCP_FLAGS_ALL4) ==
766 + (TCP_FLAG_ACK | TCP_FLAG_RST);
767 +}
768 +
769 +static inline int tflg_syn(const struct tcphdr *th)
770 +{
771 + return (tcp_flag_word(th) & TCP_FLAGS_ALL4) == TCP_FLAG_SYN;
772 +}
773 +
774 +static inline int tflg_synack(const struct tcphdr *th)
775 +{
776 + return (tcp_flag_word(th) & TCP_FLAGS_ALL4) ==
777 + (TCP_FLAG_SYN | TCP_FLAG_ACK);
778 +}
779 +
780 +/* portscan functions */
781 +static inline int xt_portscan_stealth(const struct tcphdr *th)
782 +{
783 + /*
784 + * "Connection refused" replies to our own probes must not be matched.
785 + */
786 + if(tflg_rstack(th))
787 + return 0;
788 +
789 + if(tflg_rst(th) && printk_ratelimit()) {
790 + printk(KERN_WARNING PFX "Warning: Pure RST received\n");
791 + return 0;
792 + }
793 +
794 + /*
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.).
799 + */
800 + return !tflg_syn(th);
801 +}
802 +
803 +static inline int xt_portscan_full(int mark, enum ip_conntrack_info ctstate,
804 + int loopback, const struct tcphdr *tcph, int payload_len)
805 +{
806 + if(mark == mark_estab2) {
807 + /*
808 + * -m connmark --mark $ESTAB2
809 + */
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;
814 + else
815 + return mark_valid;
816 + } else if(mark == mark_estab1) {
817 + /*
818 + * -m connmark --mark $ESTAB1
819 + */
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;
824 + else
825 + return mark_valid;
826 + } else if(mark == mark_synrcv) {
827 + /*
828 + * -m connmark --mark $SYN
829 + */
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;
836 + else
837 + return mark_synscan;
838 + } else if(ctstate == IP_CT_NEW && tflg_syn(tcph)) {
839 + /*
840 + * -p tcp --syn --ctstate NEW
841 + */
842 + return mark_synrcv;
843 + }
844 + return mark;
845 +}
846 +
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)
851 +{
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;
857 +
858 + tcph = skb_header_pointer(skb, protoff, sizeof(tcph_buf), &tcph_buf);
859 + if(tcph == NULL)
860 + return 0;
861 +
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);
866 + /*
867 + * If @ctdata is NULL, we cannot match the other scan
868 + * types, return.
869 + */
870 + return 0;
871 + }
872 +
873 + /*
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.
877 + */
878 + if((ctdata->mark & connmark_mask) == mark_valid ||
879 + (skb->nfmark & packet_mask) != mark_seen)
880 + {
881 + unsigned int n;
882 + n = xt_portscan_full(ctdata->mark & connmark_mask, ctstate,
883 + in == &loopback_dev, tcph,
884 + skb->len - protoff - 4 * tcph->doff);
885 +
886 + ctdata->mark = (ctdata->mark & ~connmark_mask) | n;
887 + ((struct sk_buff *)skb)->nfmark =
888 + (skb->nfmark & ~packet_mask) | mark_seen;
889 + }
890 +
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);
894 +}
895 +
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,
900 +#endif
901 + unsigned int hook_mask)
902 +{
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",
907 + matchinfosize,
908 + XT_ALIGN(sizeof(struct xt_portscan_info)));
909 + return 0;
910 + }
911 +#endif
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");
915 + return 0;
916 + }
917 + return 1;
918 +}
919 +
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,
926 + .family = AF_INET,
927 + .me = THIS_MODULE,
928 +};
929 +
930 +static int __init xt_portscan_init(void)
931 +{
932 + return xt_register_match(&xt_portscan);
933 +}
934 +
935 +static void __exit xt_portscan_exit(void)
936 +{
937 + xt_unregister_match(&xt_portscan);
938 + return;
939 +}
940 +
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");