remove empty directory
[openwrt/svn-archive/archive.git] / target / linux / generic-2.6 / patches / 170-netfilter_chaostables_0.8.patch
1 Index: linux-2.6.21.7/include/linux/netfilter/oot_conntrack.h
2 ===================================================================
3 --- /dev/null
4 +++ linux-2.6.21.7/include/linux/netfilter/oot_conntrack.h
5 @@ -0,0 +1,5 @@
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>
10 +#endif
11 Index: linux-2.6.21.7/include/linux/netfilter/oot_trans.h
12 ===================================================================
13 --- /dev/null
14 +++ linux-2.6.21.7/include/linux/netfilter/oot_trans.h
15 @@ -0,0 +1,14 @@
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
22 +#endif
23 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
24 +# define nfmark mark
25 +#endif
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))
29 +#endif
30 Index: linux-2.6.21.7/include/linux/netfilter/xt_CHAOS.h
31 ===================================================================
32 --- /dev/null
33 +++ linux-2.6.21.7/include/linux/netfilter/xt_CHAOS.h
34 @@ -0,0 +1,14 @@
35 +#ifndef _LINUX_NETFILTER_XT_CHAOS_H
36 +#define _LINUX_NETFILTER_XT_CHAOS_H 1
37 +
38 +enum xt_chaos_target_variant {
39 + XTCHAOS_NORMAL,
40 + XTCHAOS_TARPIT,
41 + XTCHAOS_DELUDE,
42 +};
43 +
44 +struct xt_chaos_target_info {
45 + uint8_t variant;
46 +};
47 +
48 +#endif /* _LINUX_NETFILTER_XT_CHAOS_H */
49 Index: linux-2.6.21.7/include/linux/netfilter/xt_portscan.h
50 ===================================================================
51 --- /dev/null
52 +++ linux-2.6.21.7/include/linux/netfilter/xt_portscan.h
53 @@ -0,0 +1,8 @@
54 +#ifndef _LINUX_NETFILTER_XT_PORTSCAN_H
55 +#define _LINUX_NETFILTER_XT_PORTSCAN_H 1
56 +
57 +struct xt_portscan_match_info {
58 + uint8_t match_stealth, match_syn, match_cn, match_gr;
59 +};
60 +
61 +#endif /* _LINUX_NETFILTER_XT_PORTSCAN_H */
62 Index: linux-2.6.21.7/net/netfilter/find_match.c
63 ===================================================================
64 --- /dev/null
65 +++ linux-2.6.21.7/net/netfilter/find_match.c
66 @@ -0,0 +1,39 @@
67 +/*
68 + xt_request_find_match
69 + by Jan Engelhardt <jengelh [at] gmx de>, 2006 - 2007
70 +
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.
76 +*/
77 +#include <linux/err.h>
78 +#include <linux/netfilter_arp.h>
79 +#include <linux/socket.h>
80 +#include <linux/netfilter/x_tables.h>
81 +
82 +/*
83 + * Yeah this code is sub-optimal, but the function is missing in
84 + * mainline so far. -jengelh
85 + */
86 +static struct xt_match *xt_request_find_match_lo(int af, const char *name,
87 + u8 revision)
88 +{
89 + static const char *const xt_prefix[] = {
90 + [AF_INET] = "ip",
91 + [AF_INET6] = "ip6",
92 + [NF_ARP] = "arp",
93 + };
94 + struct xt_match *match;
95 +
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)
99 + return NULL;
100 +
101 + return match;
102 +}
103 +
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.21.7/net/netfilter/Kconfig
107 ===================================================================
108 --- linux-2.6.21.7.orig/net/netfilter/Kconfig
109 +++ linux-2.6.21.7/net/netfilter/Kconfig
110 @@ -287,6 +287,14 @@ config NETFILTER_XTABLES
111
112 # alphabetically ordered list of targets
113
114 +config NETFILTER_XT_TARGET_CHAOS
115 + tristate '"CHAOS" target support'
116 + depends on NETFILTER_XTABLES
117 + help
118 + This option adds a `CHAOS' target.
119 +
120 + To compile it as a module, choose M here. If unsure, say N.
121 +
122 config NETFILTER_XT_TARGET_CLASSIFY
123 tristate '"CLASSIFY" target support'
124 depends on NETFILTER_XTABLES
125 @@ -315,6 +323,14 @@ config NETFILTER_XT_TARGET_CONNMARK
126 <file:Documentation/modules.txt>. The module will be called
127 ipt_CONNMARK.o. If unsure, say `N'.
128
129 +config NETFILTER_XT_TARGET_DELUDE
130 + tristate '"DELUDE" target support'
131 + depends on NETFILTER_XTABLES
132 + help
133 + This option adds a `DELUDE' target.
134 +
135 + To compile it as a module, choose M here. If unsure, say N.
136 +
137 config NETFILTER_XT_TARGET_DSCP
138 tristate '"DSCP" target support'
139 depends on NETFILTER_XTABLES
140 @@ -563,6 +579,14 @@ config NETFILTER_XT_MATCH_POLICY
141
142 To compile it as a module, choose M here. If unsure, say N.
143
144 +config NETFILTER_XT_MATCH_PORTSCAN
145 + tristate '"portscan" match support'
146 + depends on NETFILTER_XTABLES
147 + help
148 + This option adds a 'portscan' match support.
149 +
150 + To compile it as a module, choose M here. If unsure, say N.
151 +
152 config NETFILTER_XT_MATCH_MULTIPORT
153 tristate "Multiple port match support"
154 depends on NETFILTER_XTABLES
155 Index: linux-2.6.21.7/net/netfilter/Makefile
156 ===================================================================
157 --- linux-2.6.21.7.orig/net/netfilter/Makefile
158 +++ linux-2.6.21.7/net/netfilter/Makefile
159 @@ -47,6 +47,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_CONNSECMARK) += xt_CONNSECMARK.o
163 +obj-$(CONFIG_NETFILTER_XT_TARGET_CHAOS) += xt_CHAOS.o
164 +obj-$(CONFIG_NETFILTER_XT_TARGET_DELUDE) += xt_DELUDE.o
165
166 # matches
167 obj-$(CONFIG_NETFILTER_XT_MATCH_COMMENT) += xt_comment.o
168 @@ -74,3 +76,4 @@ obj-$(CONFIG_NETFILTER_XT_MATCH_STRING)
169 obj-$(CONFIG_NETFILTER_XT_MATCH_TCPMSS) += xt_tcpmss.o
170 obj-$(CONFIG_NETFILTER_XT_MATCH_PHYSDEV) += xt_physdev.o
171 obj-$(CONFIG_NETFILTER_XT_MATCH_HASHLIMIT) += xt_hashlimit.o
172 +obj-$(CONFIG_NETFILTER_XT_MATCH_PORTSCAN) += xt_portscan.o
173 Index: linux-2.6.21.7/net/netfilter/xt_CHAOS.c
174 ===================================================================
175 --- /dev/null
176 +++ linux-2.6.21.7/net/netfilter/xt_CHAOS.c
177 @@ -0,0 +1,200 @@
178 +/*
179 + * CHAOS target for netfilter
180 + * Copyright © CC Computer Consultants GmbH, 2006 - 2007
181 + * Contact: Jan Engelhardt <jengelh@computergmbh.de>
182 + *
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.
186 + */
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>
196 +#include <net/ip.h>
197 +#if defined(_LOCAL)
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"
204 +#else
205 +# include "xt_CHAOS.h"
206 +# include "find_match.c"
207 +#endif
208 +#define PFX KBUILD_MODNAME ": "
209 +
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);
215 +
216 +/* References to other matches/targets */
217 +static struct xt_match *xm_tcp;
218 +static struct xt_target *xt_delude, *xt_reject, *xt_tarpit;
219 +
220 +static int have_delude, have_tarpit;
221 +
222 +/* Static data for other matches/targets */
223 +static const struct ipt_reject_info reject_params = {
224 + .with = ICMP_HOST_UNREACH,
225 +};
226 +
227 +static const struct xt_tcp tcp_params = {
228 + .spts = {0, ~0},
229 + .dpts = {0, ~0},
230 +};
231 +
232 +/* CHAOS functions */
233 +static void xt_chaos_total(const struct xt_chaos_target_info *info,
234 + struct sk_buff **pskb, const struct net_device *in,
235 + const struct net_device *out, unsigned int hooknum)
236 +{
237 + const struct iphdr *iph = ip_hdr(*pskb);
238 + const int protoff = 4 * iph->ihl;
239 + const int offset = ntohs(iph->frag_off) & IP_OFFSET;
240 + const struct xt_target *destiny;
241 + int hotdrop = 0, ret;
242 +
243 + ret = xm_tcp->match(*pskb, in, out, xm_tcp, &tcp_params,
244 + offset, protoff, &hotdrop);
245 + if (!ret || hotdrop || (unsigned int)net_random() > delude_percentage)
246 + return;
247 +
248 + destiny = (info->variant == XTCHAOS_TARPIT) ? xt_tarpit : xt_delude;
249 + destiny->target(pskb, in, out, hooknum, destiny, NULL);
250 + return;
251 +}
252 +
253 +static unsigned int chaos_tg(struct sk_buff **pskb,
254 + const struct net_device *in, const struct net_device *out,
255 + unsigned int hooknum, const struct xt_target *target, const void *targinfo)
256 +{
257 + /*
258 + * Equivalent to:
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;
264 + */
265 + const struct xt_chaos_target_info *info = targinfo;
266 + const struct iphdr *iph = ip_hdr(*pskb);
267 +
268 + if ((unsigned int)net_random() <= reject_percentage)
269 + return xt_reject->target(pskb, in, out, hooknum, target,
270 + &reject_params);
271 +
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, pskb, in, out, hooknum);
276 +
277 + return NF_DROP;
278 +}
279 +
280 +static int chaos_tg_check(const char *tablename, const void *entry,
281 + const struct xt_target *target, void *targinfo, unsigned int hook_mask)
282 +{
283 + const struct xt_chaos_target_info *info = targinfo;
284 +
285 + if (info->variant == XTCHAOS_DELUDE && !have_delude) {
286 + printk(KERN_WARNING PFX "Error: Cannot use --delude when "
287 + "DELUDE module not available\n");
288 + return false;
289 + }
290 + if (info->variant == XTCHAOS_TARPIT && !have_tarpit) {
291 + printk(KERN_WARNING PFX "Error: Cannot use --tarpit when "
292 + "TARPIT module not available\n");
293 + return false;
294 + }
295 +
296 + return true;
297 +}
298 +
299 +static struct xt_target chaos_tg_reg = {
300 + .name = "CHAOS",
301 + .family = AF_INET,
302 + .table = "filter",
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),
308 + .me = THIS_MODULE,
309 +};
310 +
311 +static int __init chaos_tg_init(void)
312 +{
313 + int ret = -EINVAL;
314 +
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");
319 + return -EINVAL;
320 + }
321 +
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");
326 + goto out2;
327 + }
328 +
329 + xt_tarpit = xt_request_find_target(AF_INET, "TARPIT", 0);
330 + have_tarpit = xt_tarpit != NULL;
331 + if (!have_tarpit)
332 + printk(KERN_WARNING PFX "Warning: Could not find or load "
333 + "\"TARPIT\" target\n");
334 +
335 + xt_delude = xt_request_find_target(AF_INET, "DELUDE", 0);
336 + have_delude = xt_delude != NULL;
337 + if (!have_delude)
338 + printk(KERN_WARNING PFX "Warning: Could not find or load "
339 + "\"DELUDE\" target\n");
340 +
341 + if ((ret = xt_register_target(&chaos_tg_reg)) != 0) {
342 + printk(KERN_WARNING PFX "xt_register_target returned "
343 + "error %d\n", ret);
344 + goto out3;
345 + }
346 +
347 + return 0;
348 +
349 + out3:
350 + if (have_delude)
351 + module_put(xt_delude->me);
352 + if (have_tarpit)
353 + module_put(xt_tarpit->me);
354 + module_put(xt_reject->me);
355 + out2:
356 + module_put(xm_tcp->me);
357 + return ret;
358 +}
359 +
360 +static void __exit chaos_tg_exit(void)
361 +{
362 + xt_unregister_target(&chaos_tg_reg);
363 + module_put(xm_tcp->me);
364 + module_put(xt_reject->me);
365 + if (have_delude)
366 + module_put(xt_delude->me);
367 + if (have_tarpit)
368 + module_put(xt_tarpit->me);
369 + return;
370 +}
371 +
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.21.7/net/netfilter/xt_DELUDE.c
379 ===================================================================
380 --- /dev/null
381 +++ linux-2.6.21.7/net/netfilter/xt_DELUDE.c
382 @@ -0,0 +1,197 @@
383 +/*
384 + * DELUDE target
385 + * Copyright © CC Computer Consultants GmbH, 2007
386 + * Contact: Jan Engelhardt <jengelh@computergmbh.de>
387 + *
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>
391 + *
392 + * xt_DELUDE acts like REJECT, but does reply with SYN-ACK on SYN.
393 + *
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.
397 + */
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>
405 +#endif
406 +#include <net/tcp.h>
407 +#define PFX KBUILD_MODNAME ": "
408 +
409 +static void delude_send_reset(struct sk_buff *oldskb, unsigned int hook)
410 +{
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;
417 + bool needs_ack;
418 +
419 + /* IP header checks: fragment. */
420 + if (ip_hdr(oldskb)->frag_off & htons(IP_OFFSET))
421 + return;
422 +
423 + oth = skb_header_pointer(oldskb, ip_hdrlen(oldskb),
424 + sizeof(_otcph), &_otcph);
425 + if (oth == NULL)
426 + return;
427 +
428 + /* No RST for RST. */
429 + if (oth->rst)
430 + return;
431 +
432 + /* Check checksum */
433 + if (nf_ip_checksum(oldskb, hook, ip_hdrlen(oldskb), IPPROTO_TCP))
434 + return;
435 +
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),
440 + GFP_ATOMIC);
441 + if (!nskb)
442 + return;
443 +
444 + /* This packet will not be the same as the other: clear nf fields */
445 + nf_reset(nskb);
446 + nskb->mark = 0;
447 + skb_init_secmark(nskb);
448 +
449 + skb_shinfo(nskb)->gso_size = 0;
450 + skb_shinfo(nskb)->gso_segs = 0;
451 + skb_shinfo(nskb)->gso_type = 0;
452 +
453 + tcph = (struct tcphdr *)(skb_network_header(nskb) + ip_hdrlen(nskb));
454 +
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;
463 +
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);
468 +
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) -
473 + (oth->doff << 2));
474 + tcph->seq = false;
475 + tcph->ack = true;
476 + } else {
477 + if (!tcph->ack) {
478 + needs_ack = true;
479 + tcph->ack_seq = htonl(ntohl(oth->seq) + oth->syn +
480 + oth->fin + oldskb->len -
481 + ip_hdrlen(oldskb) - (oth->doff<<2));
482 + tcph->seq = false;
483 + } else {
484 + needs_ack = false;
485 + tcph->seq = oth->ack_seq;
486 + tcph->ack_seq = false;
487 + }
488 +
489 + /* Reset flags */
490 + ((u_int8_t *)tcph)[13] = 0;
491 + tcph->rst = true;
492 + tcph->ack = needs_ack;
493 + }
494 +
495 + tcph->window = 0;
496 + tcph->urg_ptr = 0;
497 +
498 + /* Adjust TCP checksum */
499 + tcph->check = 0;
500 + tcph->check = tcp_v4_check(sizeof(struct tcphdr), niph->saddr,
501 + niph->daddr, csum_partial((char *)tcph,
502 + sizeof(struct tcphdr), 0));
503 +
504 + /* Set DF, id = 0 */
505 + niph->frag_off = htons(IP_DF);
506 + niph->id = 0;
507 +
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))
512 +#else
513 + if (hook != NF_IP_FORWARD)
514 +#endif
515 + addr_type = RTN_LOCAL;
516 +
517 + if (ip_route_me_harder(&nskb, addr_type))
518 + goto free_nskb;
519 +
520 + nskb->ip_summed = CHECKSUM_NONE;
521 +
522 + /* Adjust IP TTL */
523 + niph->ttl = dst_metric(nskb->dst, RTAX_HOPLIMIT);
524 +
525 + /* Adjust IP checksum */
526 + niph->check = 0;
527 + niph->check = ip_fast_csum(skb_network_header(nskb), niph->ihl);
528 +
529 + /* "Never happens" */
530 + if (nskb->len > dst_mtu(nskb->dst))
531 + goto free_nskb;
532 +
533 + nf_ct_attach(nskb, oldskb);
534 +
535 + NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, nskb, NULL, nskb->dst->dev,
536 + dst_output);
537 + return;
538 +
539 + free_nskb:
540 + kfree_skb(nskb);
541 +}
542 +
543 +static unsigned int delude_tg(struct sk_buff **pskb,
544 + const struct net_device *in, const struct net_device *out,
545 + unsigned int hooknum, const struct xt_target *target, const void *targinfo)
546 +{
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(*pskb, hooknum);
551 + return NF_DROP;
552 +}
553 +
554 +static struct xt_target delude_tg_reg = {
555 + .name = "DELUDE",
556 + .family = AF_INET,
557 + .table = "filter",
558 + .hooks = (1 << NF_IP_LOCAL_IN) | (1 << NF_IP_FORWARD),
559 + .target = delude_tg,
560 + .proto = IPPROTO_TCP,
561 + .me = THIS_MODULE,
562 +};
563 +
564 +static int __init delude_tg_init(void)
565 +{
566 + return xt_register_target(&delude_tg_reg);
567 +}
568 +
569 +static void __exit delude_tg_exit(void)
570 +{
571 + xt_unregister_target(&delude_tg_reg);
572 +}
573 +
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.21.7/net/netfilter/xt_portscan.c
581 ===================================================================
582 --- /dev/null
583 +++ linux-2.6.21.7/net/netfilter/xt_portscan.c
584 @@ -0,0 +1,269 @@
585 +/*
586 + * portscan match for netfilter
587 + * Copyright © CC Computer Consultants GmbH, 2006 - 2007
588 + * Contact: Jan Engelhardt <jengelh@computergmbh.de>
589 + *
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.
593 + */
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>
606 +#if defined(_LOCAL)
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>
611 +#else
612 +# include "xt_portscan.h"
613 +#endif
614 +#define PFX KBUILD_MODNAME ": "
615 +
616 +enum {
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,
620 +};
621 +
622 +/* Module parameters */
623 +static unsigned int
624 + connmark_mask = ~0,
625 + packet_mask = ~0,
626 + mark_seen = 0x9,
627 + mark_synrcv = 0x1,
628 + mark_closed = 0x2,
629 + mark_synscan = 0x3,
630 + mark_estab1 = 0x4,
631 + mark_estab2 = 0x5,
632 + mark_cnscan = 0x6,
633 + mark_grscan = 0x7,
634 + mark_valid = 0x8;
635 +
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");
658 +
659 +/* TCP flag functions */
660 +static inline bool tflg_ack4(const struct tcphdr *th)
661 +{
662 + return (tcp_flag_word(th) & TCP_FLAGS_ALL4) == TCP_FLAG_ACK;
663 +}
664 +
665 +static inline bool tflg_ack6(const struct tcphdr *th)
666 +{
667 + return (tcp_flag_word(th) & TCP_FLAGS_ALL6) == TCP_FLAG_ACK;
668 +}
669 +
670 +static inline bool tflg_fin(const struct tcphdr *th)
671 +{
672 + return (tcp_flag_word(th) & TCP_FLAGS_ALL3) == TCP_FLAG_FIN;
673 +}
674 +
675 +static inline bool tflg_rst(const struct tcphdr *th)
676 +{
677 + return (tcp_flag_word(th) & TCP_FLAGS_ALL3) == TCP_FLAG_RST;
678 +}
679 +
680 +static inline bool tflg_rstack(const struct tcphdr *th)
681 +{
682 + return (tcp_flag_word(th) & TCP_FLAGS_ALL4) ==
683 + (TCP_FLAG_ACK | TCP_FLAG_RST);
684 +}
685 +
686 +static inline bool tflg_syn(const struct tcphdr *th)
687 +{
688 + return (tcp_flag_word(th) & TCP_FLAGS_ALL4) == TCP_FLAG_SYN;
689 +}
690 +
691 +static inline bool tflg_synack(const struct tcphdr *th)
692 +{
693 + return (tcp_flag_word(th) & TCP_FLAGS_ALL4) ==
694 + (TCP_FLAG_SYN | TCP_FLAG_ACK);
695 +}
696 +
697 +/* portscan functions */
698 +static inline bool portscan_mt_stealth(const struct tcphdr *th)
699 +{
700 + /*
701 + * "Connection refused" replies to our own probes must not be matched.
702 + */
703 + if (tflg_rstack(th))
704 + return false;
705 +
706 + if (tflg_rst(th) && printk_ratelimit()) {
707 + printk(KERN_WARNING PFX "Warning: Pure RST received\n");
708 + return false;
709 + }
710 +
711 + /*
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.).
716 + */
717 + return !tflg_syn(th);
718 +}
719 +
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)
723 +{
724 + if (mark == mark_estab2) {
725 + /*
726 + * -m connmark --mark $ESTAB2
727 + */
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;
732 + else
733 + return mark_valid;
734 + } else if (mark == mark_estab1) {
735 + /*
736 + * -m connmark --mark $ESTAB1
737 + */
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;
742 + else
743 + return mark_valid;
744 + } else if (mark == mark_synrcv) {
745 + /*
746 + * -m connmark --mark $SYN
747 + */
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;
754 + else
755 + return mark_synscan;
756 + } else if (ctstate == IP_CT_NEW && tflg_syn(tcph)) {
757 + /*
758 + * -p tcp --syn --ctstate NEW
759 + */
760 + return mark_synrcv;
761 + }
762 + return mark;
763 +}
764 +
765 +static int 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, int *hotdrop)
769 +{
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;
775 +
776 + tcph = skb_header_pointer(skb, protoff, sizeof(tcph_buf), &tcph_buf);
777 + if (tcph == NULL)
778 + return false;
779 +
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);
784 + /*
785 + * If @ctdata is NULL, we cannot match the other scan
786 + * types, return.
787 + */
788 + return false;
789 + }
790 +
791 + /*
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.
795 + */
796 + if ((ctdata->mark & connmark_mask) == mark_valid ||
797 + (skb->mark & packet_mask) != mark_seen) {
798 + unsigned int n;
799 +
800 + n = portscan_mt_full(ctdata->mark & connmark_mask, ctstate,
801 + in == &loopback_dev, tcph,
802 + skb->len - protoff - 4 * tcph->doff);
803 +
804 + ctdata->mark = (ctdata->mark & ~connmark_mask) | n;
805 + ((struct sk_buff *)skb)->mark =
806 + (skb->mark & ~packet_mask) ^ mark_seen;
807 + }
808 +
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);
812 +}
813 +
814 +static int portscan_mt_check(const char *tablename, const void *entry,
815 + const struct xt_match *match, void *matchinfo, unsigned int hook_mask)
816 +{
817 + const struct xt_portscan_match_info *info = matchinfo;
818 +
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");
822 + return false;
823 + }
824 + return true;
825 +}
826 +
827 +static struct xt_match portscan_mt_reg __read_mostly = {
828 + .name = "portscan",
829 + .family = AF_INET,
830 + .match = portscan_mt,
831 + .checkentry = portscan_mt_check,
832 + .matchsize = sizeof(struct xt_portscan_match_info),
833 + .proto = IPPROTO_TCP,
834 + .me = THIS_MODULE,
835 +};
836 +
837 +static int __init portscan_mt_init(void)
838 +{
839 + return xt_register_match(&portscan_mt_reg);
840 +}
841 +
842 +static void __exit portscan_mt_exit(void)
843 +{
844 + xt_unregister_match(&portscan_mt_reg);
845 + return;
846 +}
847 +
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");