[kernel] switch to 2.6.27.12
[openwrt.git] / target / linux / generic-2.6 / patches-2.6.28 / 170-netfilter_chaostables_0.8.patch
1 --- a/include/linux/netfilter/Kbuild
2 +++ b/include/linux/netfilter/Kbuild
3 @@ -3,6 +3,7 @@ header-y += nf_conntrack_tuple_common.h
4  header-y += nfnetlink_conntrack.h
5  header-y += nfnetlink_log.h
6  header-y += nfnetlink_queue.h
7 +header-y += xt_CHAOS.h
8  header-y += xt_CLASSIFY.h
9  header-y += xt_CONNMARK.h
10  header-y += xt_CONNSECMARK.h
11 --- /dev/null
12 +++ b/include/linux/netfilter/oot_conntrack.h
13 @@ -0,0 +1,5 @@
14 +#if defined(CONFIG_IP_NF_CONNTRACK) || defined(CONFIG_IP_NF_CONNTRACK_MODULE)
15 +#      include <linux/netfilter_ipv4/ip_conntrack.h>
16 +#else /* linux-2.6.20+ */
17 +#      include <net/netfilter/nf_nat_rule.h>
18 +#endif
19 --- /dev/null
20 +++ b/include/linux/netfilter/oot_trans.h
21 @@ -0,0 +1,14 @@
22 +/* Out of tree workarounds */
23 +#include <linux/version.h>
24 +#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 18)
25 +#      define HAVE_MATCHINFOSIZE 1
26 +#      define HAVE_TARGUSERINFO 1
27 +#      define HAVE_TARGINFOSIZE 1
28 +#endif
29 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
30 +#      define nfmark mark
31 +#endif
32 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 21)
33 +#      define tcp_v4_check(tcph, tcph_sz, s, d, csp) \
34 +               tcp_v4_check((tcph_sz), (s), (d), (csp))
35 +#endif
36 --- /dev/null
37 +++ b/include/linux/netfilter/xt_CHAOS.h
38 @@ -0,0 +1,14 @@
39 +#ifndef _LINUX_NETFILTER_XT_CHAOS_H
40 +#define _LINUX_NETFILTER_XT_CHAOS_H 1
41 +
42 +enum xt_chaos_target_variant {
43 +       XTCHAOS_NORMAL,
44 +       XTCHAOS_TARPIT,
45 +       XTCHAOS_DELUDE,
46 +};
47 +
48 +struct xt_chaos_target_info {
49 +       uint8_t variant;
50 +};
51 +
52 +#endif /* _LINUX_NETFILTER_XT_CHAOS_H */
53 --- /dev/null
54 +++ b/include/linux/netfilter/xt_portscan.h
55 @@ -0,0 +1,8 @@
56 +#ifndef _LINUX_NETFILTER_XT_PORTSCAN_H
57 +#define _LINUX_NETFILTER_XT_PORTSCAN_H 1
58 +
59 +struct xt_portscan_match_info {
60 +       uint8_t match_stealth, match_syn, match_cn, match_gr;
61 +};
62 +
63 +#endif /* _LINUX_NETFILTER_XT_PORTSCAN_H */
64 --- /dev/null
65 +++ b/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 --- a/net/netfilter/Kconfig
107 +++ b/net/netfilter/Kconfig
108 @@ -303,6 +303,14 @@ if NETFILTER_XTABLES
109  
110  # alphabetically ordered list of targets
111  
112 +config NETFILTER_XT_TARGET_CHAOS
113 +       tristate '"CHAOS" target support'
114 +       depends on NETFILTER_XTABLES
115 +       help
116 +         This option adds a `CHAOS' target.
117 +
118 +         To compile it as a module, choose M here.  If unsure, say N.
119 +
120  config NETFILTER_XT_TARGET_CLASSIFY
121         tristate '"CLASSIFY" target support'
122         depends on NETFILTER_ADVANCED
123 @@ -341,6 +349,14 @@ config NETFILTER_XT_TARGET_CONNSECMARK
124  
125           To compile it as a module, choose M here.  If unsure, say N.
126  
127 +config NETFILTER_XT_TARGET_DELUDE
128 +       tristate '"DELUDE" target support'
129 +       depends on NETFILTER_XTABLES
130 +       help
131 +         This option adds a `DELUDE' target.
132 +
133 +         To compile it as a module, choose M here.  If unsure, say N.
134 +
135  config NETFILTER_XT_TARGET_DSCP
136         tristate '"DSCP" and "TOS" target support'
137         depends on IP_NF_MANGLE || IP6_NF_MANGLE
138 @@ -654,6 +670,14 @@ config NETFILTER_XT_MATCH_MARK
139  
140           To compile it as a module, choose M here.  If unsure, say N.
141  
142 +config NETFILTER_XT_MATCH_PORTSCAN
143 +       tristate '"portscan" match support'
144 +       depends on NETFILTER_XTABLES
145 +       help
146 +         This option adds a 'portscan' match support.
147 +
148 +         To compile it as a module, choose M here.  If unsure, say N.
149 +
150  config NETFILTER_XT_MATCH_MULTIPORT
151         tristate '"multiport" Multiple port match support'
152         depends on NETFILTER_ADVANCED
153 --- a/net/netfilter/Makefile
154 +++ b/net/netfilter/Makefile
155 @@ -55,6 +55,8 @@ obj-$(CONFIG_NETFILTER_XT_TARGET_TPROXY)
156  obj-$(CONFIG_NETFILTER_XT_TARGET_TCPMSS) += xt_TCPMSS.o
157  obj-$(CONFIG_NETFILTER_XT_TARGET_TCPOPTSTRIP) += xt_TCPOPTSTRIP.o
158  obj-$(CONFIG_NETFILTER_XT_TARGET_TRACE) += xt_TRACE.o
159 +obj-$(CONFIG_NETFILTER_XT_TARGET_CHAOS) += xt_CHAOS.o
160 +obj-$(CONFIG_NETFILTER_XT_TARGET_DELUDE) += xt_DELUDE.o
161  
162  # matches
163  obj-$(CONFIG_NETFILTER_XT_MATCH_COMMENT) += xt_comment.o
164 @@ -90,6 +92,7 @@ obj-$(CONFIG_NETFILTER_XT_MATCH_STRING) 
165  obj-$(CONFIG_NETFILTER_XT_MATCH_TCPMSS) += xt_tcpmss.o
166  obj-$(CONFIG_NETFILTER_XT_MATCH_TIME) += xt_time.o
167  obj-$(CONFIG_NETFILTER_XT_MATCH_U32) += xt_u32.o
168 +obj-$(CONFIG_NETFILTER_XT_MATCH_PORTSCAN) += xt_portscan.o
169  
170  # IPVS
171  obj-$(CONFIG_IP_VS) += ipvs/
172 --- /dev/null
173 +++ b/net/netfilter/xt_CHAOS.c
174 @@ -0,0 +1,200 @@
175 +/*
176 + *     CHAOS target for netfilter
177 + *     Copyright © CC Computer Consultants GmbH, 2006 - 2007
178 + *     Contact: Jan Engelhardt <jengelh@computergmbh.de>
179 + *
180 + *     This program is free software; you can redistribute it and/or modify
181 + *     it under the terms of the GNU General Public License; either version
182 + *     2 or 3 as published by the Free Software Foundation.
183 + */
184 +#include <linux/icmp.h>
185 +#include <linux/in.h>
186 +#include <linux/ip.h>
187 +#include <linux/module.h>
188 +#include <linux/skbuff.h>
189 +#include <linux/stat.h>
190 +#include <linux/netfilter/x_tables.h>
191 +#include <linux/netfilter/xt_tcpudp.h>
192 +#include <linux/netfilter_ipv4/ipt_REJECT.h>
193 +#include <net/ip.h>
194 +#if defined(_LOCAL)
195 +#      include "xt_CHAOS.h"
196 +#      include "find_match.c"
197 +#elif defined(CONFIG_NETFILTER_XT_TARGET_CHAOS) || \
198 +    defined(CONFIG_NETFILTER_XT_TARGET_CHAOS_MODULE)
199 +#      include <linux/netfilter/xt_CHAOS.h>
200 +#      include "find_match.c"
201 +#else
202 +#      include "xt_CHAOS.h"
203 +#      include "find_match.c"
204 +#endif
205 +#define PFX KBUILD_MODNAME ": "
206 +
207 +/* Module parameters */
208 +static unsigned int reject_percentage = ~0U * .01;
209 +static unsigned int delude_percentage = ~0U * .0101;
210 +module_param(reject_percentage, uint, S_IRUGO | S_IWUSR);
211 +module_param(delude_percentage, uint, S_IRUGO | S_IWUSR);
212 +
213 +/* References to other matches/targets */
214 +static struct xt_match *xm_tcp;
215 +static struct xt_target *xt_delude, *xt_reject, *xt_tarpit;
216 +
217 +static int have_delude, have_tarpit;
218 +
219 +/* Static data for other matches/targets */
220 +static const struct ipt_reject_info reject_params = {
221 +       .with = ICMP_HOST_UNREACH,
222 +};
223 +
224 +static const struct xt_tcp tcp_params = {
225 +       .spts = {0, ~0},
226 +       .dpts = {0, ~0},
227 +};
228 +
229 +/* CHAOS functions */
230 +static void xt_chaos_total(const struct xt_chaos_target_info *info,
231 +    struct sk_buff *skb, const struct net_device *in,
232 +    const struct net_device *out, unsigned int hooknum)
233 +{
234 +       const struct iphdr *iph = ip_hdr(skb);
235 +       const int protoff       = 4 * iph->ihl;
236 +       const int offset        = ntohs(iph->frag_off) & IP_OFFSET;
237 +       const struct xt_target *destiny;
238 +       bool hotdrop = false, ret;
239 +
240 +       ret = xm_tcp->match(skb, in, out, xm_tcp, &tcp_params,
241 +                           offset, protoff, &hotdrop);
242 +       if (!ret || hotdrop || (unsigned int)net_random() > delude_percentage)
243 +               return;
244 +
245 +       destiny = (info->variant == XTCHAOS_TARPIT) ? xt_tarpit : xt_delude;
246 +       destiny->target(skb, in, out, hooknum, destiny, NULL);
247 +       return;
248 +}
249 +
250 +static unsigned int chaos_tg(struct sk_buff *skb,
251 +    const struct net_device *in, const struct net_device *out,
252 +    unsigned int hooknum, const struct xt_target *target, const void *targinfo)
253 +{
254 +       /*
255 +        * Equivalent to:
256 +        * -A chaos -m statistic --mode random --probability \
257 +        *         $reject_percentage -j REJECT --reject-with host-unreach;
258 +        * -A chaos -p tcp -m statistic --mode random --probability \
259 +        *         $delude_percentage -j DELUDE;
260 +        * -A chaos -j DROP;
261 +        */
262 +       const struct xt_chaos_target_info *info = targinfo;
263 +       const struct iphdr *iph = ip_hdr(skb);
264 +
265 +       if ((unsigned int)net_random() <= reject_percentage)
266 +               return xt_reject->target(skb, in, out, hooknum, target,
267 +                      &reject_params);
268 +
269 +       /* TARPIT/DELUDE may not be called from the OUTPUT chain */
270 +       if (iph->protocol == IPPROTO_TCP &&
271 +           info->variant != XTCHAOS_NORMAL && hooknum != NF_INET_LOCAL_OUT)
272 +               xt_chaos_total(info, skb, in, out, hooknum);
273 +
274 +       return NF_DROP;
275 +}
276 +
277 +static bool chaos_tg_check(const char *tablename, const void *entry,
278 +    const struct xt_target *target, void *targinfo, unsigned int hook_mask)
279 +{
280 +       const struct xt_chaos_target_info *info = targinfo;
281 +
282 +       if (info->variant == XTCHAOS_DELUDE && !have_delude) {
283 +               printk(KERN_WARNING PFX "Error: Cannot use --delude when "
284 +                      "DELUDE module not available\n");
285 +               return false;
286 +       }
287 +       if (info->variant == XTCHAOS_TARPIT && !have_tarpit) {
288 +               printk(KERN_WARNING PFX "Error: Cannot use --tarpit when "
289 +                      "TARPIT module not available\n");
290 +               return false;
291 +       }
292 +
293 +       return true;
294 +}
295 +
296 +static struct xt_target chaos_tg_reg = {
297 +       .name       = "CHAOS",
298 +       .family     = AF_INET,
299 +       .table      = "filter",
300 +       .hooks      = (1 << NF_INET_LOCAL_IN) | (1 << NF_INET_FORWARD) |
301 +                     (1 << NF_INET_LOCAL_OUT),
302 +       .checkentry = chaos_tg_check,
303 +       .target     = chaos_tg,
304 +       .targetsize = sizeof(struct xt_chaos_target_info),
305 +       .me         = THIS_MODULE,
306 +};
307 +
308 +static int __init chaos_tg_init(void)
309 +{
310 +       int ret = -EINVAL;
311 +
312 +       xm_tcp = xt_request_find_match(AF_INET, "tcp", 0);
313 +       if (xm_tcp == NULL) {
314 +               printk(KERN_WARNING PFX "Error: Could not find or load "
315 +                      "\"tcp\" match\n");
316 +               return -EINVAL;
317 +       }
318 +
319 +       xt_reject = xt_request_find_target(AF_INET, "REJECT", 0);
320 +       if (xt_reject == NULL) {
321 +               printk(KERN_WARNING PFX "Error: Could not find or load "
322 +                      "\"REJECT\" target\n");
323 +               goto out2;
324 +       }
325 +
326 +       xt_tarpit   = xt_request_find_target(AF_INET, "TARPIT", 0);
327 +       have_tarpit = xt_tarpit != NULL;
328 +       if (!have_tarpit)
329 +               printk(KERN_WARNING PFX "Warning: Could not find or load "
330 +                      "\"TARPIT\" target\n");
331 +
332 +       xt_delude   = xt_request_find_target(AF_INET, "DELUDE", 0);
333 +       have_delude = xt_delude != NULL;
334 +       if (!have_delude)
335 +               printk(KERN_WARNING PFX "Warning: Could not find or load "
336 +                      "\"DELUDE\" target\n");
337 +
338 +       if ((ret = xt_register_target(&chaos_tg_reg)) != 0) {
339 +               printk(KERN_WARNING PFX "xt_register_target returned "
340 +                      "error %d\n", ret);
341 +               goto out3;
342 +       }
343 +
344 +       return 0;
345 +
346 + out3:
347 +       if (have_delude)
348 +               module_put(xt_delude->me);
349 +       if (have_tarpit)
350 +               module_put(xt_tarpit->me);
351 +       module_put(xt_reject->me);
352 + out2:
353 +       module_put(xm_tcp->me);
354 +       return ret;
355 +}
356 +
357 +static void __exit chaos_tg_exit(void)
358 +{
359 +       xt_unregister_target(&chaos_tg_reg);
360 +       module_put(xm_tcp->me);
361 +       module_put(xt_reject->me);
362 +       if (have_delude)
363 +               module_put(xt_delude->me);
364 +       if (have_tarpit)
365 +               module_put(xt_tarpit->me);
366 +       return;
367 +}
368 +
369 +module_init(chaos_tg_init);
370 +module_exit(chaos_tg_exit);
371 +MODULE_AUTHOR("Jan Engelhardt <jengelh@computergmbh.de>");
372 +MODULE_DESCRIPTION("netfilter \"CHAOS\" target");
373 +MODULE_LICENSE("GPL");
374 +MODULE_ALIAS("ipt_CHAOS");
375 --- /dev/null
376 +++ b/net/netfilter/xt_DELUDE.c
377 @@ -0,0 +1,197 @@
378 +/*
379 + *     DELUDE target
380 + *     Copyright © CC Computer Consultants GmbH, 2007
381 + *     Contact: Jan Engelhardt <jengelh@computergmbh.de>
382 + *
383 + *     Based upon linux-2.6.18.5/net/ipv4/netfilter/ipt_REJECT.c:
384 + *     (C) 1999-2001 Paul `Rusty' Russell
385 + *     (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
386 + *
387 + *     xt_DELUDE acts like REJECT, but does reply with SYN-ACK on SYN.
388 + *
389 + *     This program is free software; you can redistribute it and/or modify
390 + *     it under the terms of the GNU General Public License version 2 as
391 + *     published by the Free Software Foundation.
392 + */
393 +#include <linux/module.h>
394 +#include <linux/skbuff.h>
395 +#include <linux/ip.h>
396 +#include <linux/tcp.h>
397 +#include <linux/netfilter/x_tables.h>
398 +#ifdef CONFIG_BRIDGE_NETFILTER
399 +#      include <linux/netfilter_bridge.h>
400 +#endif
401 +#include <net/tcp.h>
402 +#define PFX KBUILD_MODNAME ": "
403 +
404 +static void delude_send_reset(struct sk_buff *oldskb, unsigned int hook)
405 +{
406 +       struct tcphdr _otcph, *oth, *tcph;
407 +       unsigned int addr_type;
408 +       struct sk_buff *nskb;
409 +       u_int16_t tmp_port;
410 +       u_int32_t tmp_addr;
411 +       struct iphdr *niph;
412 +       bool needs_ack;
413 +
414 +       /* IP header checks: fragment. */
415 +       if (ip_hdr(oldskb)->frag_off & htons(IP_OFFSET))
416 +               return;
417 +
418 +       oth = skb_header_pointer(oldskb, ip_hdrlen(oldskb),
419 +                                sizeof(_otcph), &_otcph);
420 +       if (oth == NULL)
421 +               return;
422 +
423 +       /* No RST for RST. */
424 +       if (oth->rst)
425 +               return;
426 +
427 +       /* Check checksum */
428 +       if (nf_ip_checksum(oldskb, hook, ip_hdrlen(oldskb), IPPROTO_TCP))
429 +               return;
430 +
431 +       /* We need a linear, writeable skb.  We also need to expand
432 +          headroom in case hh_len of incoming interface < hh_len of
433 +          outgoing interface */
434 +       nskb = skb_copy_expand(oldskb, LL_MAX_HEADER, skb_tailroom(oldskb),
435 +                              GFP_ATOMIC);
436 +       if (!nskb)
437 +               return;
438 +
439 +       /* This packet will not be the same as the other: clear nf fields */
440 +       nf_reset(nskb);
441 +       nskb->mark = 0;
442 +       skb_init_secmark(nskb);
443 +
444 +       skb_shinfo(nskb)->gso_size = 0;
445 +       skb_shinfo(nskb)->gso_segs = 0;
446 +       skb_shinfo(nskb)->gso_type = 0;
447 +
448 +       tcph = (struct tcphdr *)(skb_network_header(nskb) + ip_hdrlen(nskb));
449 +
450 +       /* Swap source and dest */
451 +       niph         = ip_hdr(nskb);
452 +       tmp_addr     = niph->saddr;
453 +       niph->saddr  = niph->daddr;
454 +       niph->daddr  = tmp_addr;
455 +       tmp_port     = tcph->source;
456 +       tcph->source = tcph->dest;
457 +       tcph->dest   = tmp_port;
458 +
459 +       /* Truncate to length (no data) */
460 +       tcph->doff    = sizeof(struct tcphdr) / 4;
461 +       skb_trim(nskb, ip_hdrlen(nskb) + sizeof(struct tcphdr));
462 +       niph->tot_len = htons(nskb->len);
463 +
464 +       if (oth->syn && !oth->ack && !oth->rst && !oth->fin) {
465 +               /* DELUDE essential part */
466 +               tcph->ack_seq = htonl(ntohl(oth->seq) + oth->syn + oth->fin +
467 +                               oldskb->len - ip_hdrlen(oldskb) -
468 +                               (oth->doff << 2));
469 +               tcph->seq     = false;
470 +               tcph->ack     = true;
471 +       } else {
472 +               if (!tcph->ack) {
473 +                       needs_ack     = true;
474 +                       tcph->ack_seq = htonl(ntohl(oth->seq) + oth->syn +
475 +                                       oth->fin + oldskb->len -
476 +                                       ip_hdrlen(oldskb) - (oth->doff<<2));
477 +                       tcph->seq     = false;
478 +               } else {
479 +                       needs_ack     = false;
480 +                       tcph->seq     = oth->ack_seq;
481 +                       tcph->ack_seq = false;
482 +               }
483 +
484 +               /* Reset flags */
485 +               ((u_int8_t *)tcph)[13] = 0;
486 +               tcph->rst = true;
487 +               tcph->ack = needs_ack;
488 +       }
489 +
490 +       tcph->window  = 0;
491 +       tcph->urg_ptr = 0;
492 +
493 +       /* Adjust TCP checksum */
494 +       tcph->check = 0;
495 +       tcph->check = tcp_v4_check(sizeof(struct tcphdr), niph->saddr,
496 +                     niph->daddr, csum_partial((char *)tcph,
497 +                     sizeof(struct tcphdr), 0));
498 +
499 +       /* Set DF, id = 0 */
500 +       niph->frag_off = htons(IP_DF);
501 +       niph->id       = 0;
502 +
503 +       addr_type = RTN_UNSPEC;
504 +#ifdef CONFIG_BRIDGE_NETFILTER
505 +       if (hook != NF_INET_FORWARD || (nskb->nf_bridge != NULL &&
506 +           nskb->nf_bridge->mask & BRNF_BRIDGED))
507 +#else
508 +       if (hook != NF_INET_FORWARD)
509 +#endif
510 +               addr_type = RTN_LOCAL;
511 +
512 +       if (ip_route_me_harder(nskb, addr_type))
513 +               goto free_nskb;
514 +
515 +       nskb->ip_summed = CHECKSUM_NONE;
516 +
517 +       /* Adjust IP TTL */
518 +       niph->ttl = dst_metric(nskb->dst, RTAX_HOPLIMIT);
519 +
520 +       /* Adjust IP checksum */
521 +       niph->check = 0;
522 +       niph->check = ip_fast_csum(skb_network_header(nskb), niph->ihl);
523 +
524 +       /* "Never happens" */
525 +       if (nskb->len > dst_mtu(nskb->dst))
526 +               goto free_nskb;
527 +
528 +       nf_ct_attach(nskb, oldskb);
529 +
530 +       NF_HOOK(PF_INET, NF_INET_LOCAL_OUT, nskb, NULL, nskb->dst->dev,
531 +               dst_output);
532 +       return;
533 +
534 + free_nskb:
535 +       kfree_skb(nskb);
536 +}
537 +
538 +static unsigned int delude_tg(struct sk_buff *skb,
539 +    const struct net_device *in, const struct net_device *out,
540 +    unsigned int hooknum, const struct xt_target *target, const void *targinfo)
541 +{
542 +       /* WARNING: This code causes reentry within iptables.
543 +          This means that the iptables jump stack is now crap.  We
544 +          must return an absolute verdict. --RR */
545 +       delude_send_reset(skb, hooknum);
546 +       return NF_DROP;
547 +}
548 +
549 +static struct xt_target delude_tg_reg = {
550 +       .name       = "DELUDE",
551 +       .family     = AF_INET,
552 +       .table      = "filter",
553 +       .hooks      = (1 << NF_INET_LOCAL_IN) | (1 << NF_INET_FORWARD),
554 +       .target     = delude_tg,
555 +       .proto      = IPPROTO_TCP,
556 +       .me         = THIS_MODULE,
557 +};
558 +
559 +static int __init delude_tg_init(void)
560 +{
561 +       return xt_register_target(&delude_tg_reg);
562 +}
563 +
564 +static void __exit delude_tg_exit(void)
565 +{
566 +       xt_unregister_target(&delude_tg_reg);
567 +}
568 +
569 +module_init(delude_tg_init);
570 +module_exit(delude_tg_exit);
571 +MODULE_AUTHOR("Jan Engelhardt <jengelh@computergmbh.de>");
572 +MODULE_DESCRIPTION("netfilter \"DELUDE\" target");
573 +MODULE_LICENSE("GPL");
574 +MODULE_ALIAS("ipt_DELUDE");
575 --- /dev/null
576 +++ b/net/netfilter/xt_portscan.c
577 @@ -0,0 +1,269 @@
578 +/*
579 + *     portscan match for netfilter
580 + *     Copyright © CC Computer Consultants GmbH, 2006 - 2007
581 + *     Contact: Jan Engelhardt <jengelh@computergmbh.de>
582 + *
583 + *     This program is free software; you can redistribute it and/or modify
584 + *     it under the terms of the GNU General Public License; either version
585 + *     2 or 3 as published by the Free Software Foundation.
586 + */
587 +#include <linux/in.h>
588 +#include <linux/ip.h>
589 +#include <linux/module.h>
590 +#include <linux/moduleparam.h>
591 +#include <linux/skbuff.h>
592 +#include <linux/stat.h>
593 +#include <linux/tcp.h>
594 +#include <linux/types.h>
595 +#include <linux/version.h>
596 +#include <linux/netfilter/x_tables.h>
597 +#include <linux/netfilter/xt_tcpudp.h>
598 +#include <net/netfilter/nf_nat_rule.h>
599 +#if defined(_LOCAL)
600 +#      include "xt_portscan.h"
601 +#elif defined(CONFIG_NETFILTER_XT_MATCH_PORTSCAN) || \
602 +    defined(CONFIG_NETFILTER_XT_MATCH_PORTSCAN_MODULE)
603 +#      include <linux/netfilter/xt_portscan.h>
604 +#else
605 +#      include "xt_portscan.h"
606 +#endif
607 +#define PFX KBUILD_MODNAME ": "
608 +
609 +enum {
610 +       TCP_FLAGS_ALL3 = TCP_FLAG_FIN | TCP_FLAG_RST | TCP_FLAG_SYN,
611 +       TCP_FLAGS_ALL4 = TCP_FLAGS_ALL3 | TCP_FLAG_ACK,
612 +       TCP_FLAGS_ALL6 = TCP_FLAGS_ALL4 | TCP_FLAG_PSH | TCP_FLAG_URG,
613 +};
614 +
615 +/* Module parameters */
616 +static unsigned int
617 +       connmark_mask = ~0,
618 +       packet_mask   = ~0,
619 +       mark_seen     = 0x9,
620 +       mark_synrcv   = 0x1,
621 +       mark_closed   = 0x2,
622 +       mark_synscan  = 0x3,
623 +       mark_estab1   = 0x4,
624 +       mark_estab2   = 0x5,
625 +       mark_cnscan   = 0x6,
626 +       mark_grscan   = 0x7,
627 +       mark_valid    = 0x8;
628 +
629 +module_param(connmark_mask, uint, S_IRUGO | S_IWUSR);
630 +module_param(packet_mask,   uint, S_IRUGO | S_IWUSR);
631 +module_param(mark_seen,     uint, S_IRUGO | S_IWUSR);
632 +module_param(mark_synrcv,   uint, S_IRUGO | S_IWUSR);
633 +module_param(mark_closed,   uint, S_IRUGO | S_IWUSR);
634 +module_param(mark_synscan,  uint, S_IRUGO | S_IWUSR);
635 +module_param(mark_estab1,   uint, S_IRUGO | S_IWUSR);
636 +module_param(mark_estab2,   uint, S_IRUGO | S_IWUSR);
637 +module_param(mark_cnscan,   uint, S_IRUGO | S_IWUSR);
638 +module_param(mark_grscan,   uint, S_IRUGO | S_IWUSR);
639 +module_param(mark_valid,    uint, S_IRUGO | S_IWUSR);
640 +MODULE_PARM_DESC(connmark_mask, "only set specified bits in connection mark");
641 +MODULE_PARM_DESC(packet_mask,   "only set specified bits in packet mark");
642 +MODULE_PARM_DESC(mark_seen,     "nfmark value for packet-seen state");
643 +MODULE_PARM_DESC(mark_synrcv,   "connmark value for SYN Received state");
644 +MODULE_PARM_DESC(mark_closed,   "connmark value for closed state");
645 +MODULE_PARM_DESC(mark_synscan,  "connmark value for SYN Scan state");
646 +MODULE_PARM_DESC(mark_estab1,   "connmark value for Established-1 state");
647 +MODULE_PARM_DESC(mark_estab2,   "connmark value for Established-2 state");
648 +MODULE_PARM_DESC(mark_cnscan,   "connmark value for Connect Scan state");
649 +MODULE_PARM_DESC(mark_grscan,   "connmark value for Grab Scan state");
650 +MODULE_PARM_DESC(mark_valid,    "connmark value for Valid state");
651 +
652 +/* TCP flag functions */
653 +static inline bool tflg_ack4(const struct tcphdr *th)
654 +{
655 +       return (tcp_flag_word(th) & TCP_FLAGS_ALL4) == TCP_FLAG_ACK;
656 +}
657 +
658 +static inline bool tflg_ack6(const struct tcphdr *th)
659 +{
660 +       return (tcp_flag_word(th) & TCP_FLAGS_ALL6) == TCP_FLAG_ACK;
661 +}
662 +
663 +static inline bool tflg_fin(const struct tcphdr *th)
664 +{
665 +       return (tcp_flag_word(th) & TCP_FLAGS_ALL3) == TCP_FLAG_FIN;
666 +}
667 +
668 +static inline bool tflg_rst(const struct tcphdr *th)
669 +{
670 +       return (tcp_flag_word(th) & TCP_FLAGS_ALL3) == TCP_FLAG_RST;
671 +}
672 +
673 +static inline bool tflg_rstack(const struct tcphdr *th)
674 +{
675 +       return (tcp_flag_word(th) & TCP_FLAGS_ALL4) ==
676 +              (TCP_FLAG_ACK | TCP_FLAG_RST);
677 +}
678 +
679 +static inline bool tflg_syn(const struct tcphdr *th)
680 +{
681 +       return (tcp_flag_word(th) & TCP_FLAGS_ALL4) == TCP_FLAG_SYN;
682 +}
683 +
684 +static inline bool tflg_synack(const struct tcphdr *th)
685 +{
686 +       return (tcp_flag_word(th) & TCP_FLAGS_ALL4) ==
687 +              (TCP_FLAG_SYN | TCP_FLAG_ACK);
688 +}
689 +
690 +/* portscan functions */
691 +static inline bool portscan_mt_stealth(const struct tcphdr *th)
692 +{
693 +       /*
694 +        * "Connection refused" replies to our own probes must not be matched.
695 +        */
696 +       if (tflg_rstack(th))
697 +               return false;
698 +
699 +       if (tflg_rst(th) && printk_ratelimit()) {
700 +               printk(KERN_WARNING PFX "Warning: Pure RST received\n");
701 +               return false;
702 +       }
703 +
704 +       /*
705 +        * -p tcp ! --syn -m conntrack --ctstate INVALID: Looking for non-start
706 +        * packets that are not associated with any connection -- this will
707 +        * match most scan types (NULL, XMAS, FIN) and ridiculous flag
708 +        * combinations (SYN-RST, SYN-FIN, SYN-FIN-RST, FIN-RST, etc.).
709 +        */
710 +       return !tflg_syn(th);
711 +}
712 +
713 +static inline unsigned int portscan_mt_full(int mark,
714 +    enum ip_conntrack_info ctstate, bool loopback, const struct tcphdr *tcph,
715 +    unsigned int payload_len)
716 +{
717 +       if (mark == mark_estab2) {
718 +               /*
719 +                * -m connmark --mark $ESTAB2
720 +                */
721 +               if (tflg_ack4(tcph) && payload_len == 0)
722 +                       return mark; /* keep mark */
723 +               else if (tflg_rst(tcph) || tflg_fin(tcph))
724 +                       return mark_grscan;
725 +               else
726 +                       return mark_valid;
727 +       } else if (mark == mark_estab1) {
728 +               /*
729 +                * -m connmark --mark $ESTAB1
730 +                */
731 +               if (tflg_rst(tcph) || tflg_fin(tcph))
732 +                       return mark_cnscan;
733 +               else if (!loopback && tflg_ack4(tcph) && payload_len == 0)
734 +                       return mark_estab2;
735 +               else
736 +                       return mark_valid;
737 +       } else if (mark == mark_synrcv) {
738 +               /*
739 +                * -m connmark --mark $SYN
740 +                */
741 +               if (loopback && tflg_synack(tcph))
742 +                       return mark; /* keep mark */
743 +               else if (loopback && tflg_rstack(tcph))
744 +                       return mark_closed;
745 +               else if (tflg_ack6(tcph))
746 +                       return mark_estab1;
747 +               else
748 +                       return mark_synscan;
749 +       } else if (ctstate == IP_CT_NEW && tflg_syn(tcph)) {
750 +               /*
751 +                * -p tcp --syn --ctstate NEW
752 +                */
753 +               return mark_synrcv;
754 +       }
755 +       return mark;
756 +}
757 +
758 +static bool portscan_mt(const struct sk_buff *skb,
759 +    const struct net_device *in, const struct net_device *out,
760 +    const struct xt_match *match, const void *matchinfo, int offset,
761 +    unsigned int protoff, bool *hotdrop)
762 +{
763 +       const struct xt_portscan_match_info *info = matchinfo;
764 +       enum ip_conntrack_info ctstate;
765 +       const struct tcphdr *tcph;
766 +       struct nf_conn *ctdata;
767 +       struct tcphdr tcph_buf;
768 +
769 +       tcph = skb_header_pointer(skb, protoff, sizeof(tcph_buf), &tcph_buf);
770 +       if (tcph == NULL)
771 +               return false;
772 +
773 +       /* Check for invalid packets: -m conntrack --ctstate INVALID */
774 +       if ((ctdata = nf_ct_get(skb, &ctstate)) == NULL) {
775 +               if (info->match_stealth)
776 +                       return portscan_mt_stealth(tcph);
777 +               /*
778 +                * If @ctdata is NULL, we cannot match the other scan
779 +                * types, return.
780 +                */
781 +               return false;
782 +       }
783 +
784 +       /*
785 +        * If -m portscan was previously applied to this packet, the rules we
786 +        * simulate must not be run through again. And for speedup, do not call
787 +        * it either when the connection is already VALID.
788 +        */
789 +       if ((ctdata->mark & connmark_mask) == mark_valid ||
790 +            (skb->mark & packet_mask) != mark_seen) {
791 +               unsigned int n;
792 +
793 +               n = portscan_mt_full(ctdata->mark & connmark_mask, ctstate,
794 +                   (in->flags & IFF_LOOPBACK) == IFF_LOOPBACK, tcph,
795 +                   skb->len - protoff - 4 * tcph->doff);
796 +
797 +               ctdata->mark = (ctdata->mark & ~connmark_mask) | n;
798 +               ((struct sk_buff *)skb)->mark =
799 +                       (skb->mark & ~packet_mask) ^ mark_seen;
800 +       }
801 +
802 +       return (info->match_syn && ctdata->mark == mark_synscan) ||
803 +              (info->match_cn && ctdata->mark == mark_cnscan) ||
804 +              (info->match_gr && ctdata->mark == mark_grscan);
805 +}
806 +
807 +static bool portscan_mt_check(const char *tablename, const void *entry,
808 +    const struct xt_match *match, void *matchinfo, unsigned int hook_mask)
809 +{
810 +       const struct xt_portscan_match_info *info = matchinfo;
811 +
812 +       if ((info->match_stealth & ~1) || (info->match_syn & ~1) ||
813 +           (info->match_cn & ~1) || (info->match_gr & ~1)) {
814 +               printk(KERN_WARNING PFX "Invalid flags\n");
815 +               return false;
816 +       }
817 +       return true;
818 +}
819 +
820 +static struct xt_match portscan_mt_reg __read_mostly = {
821 +       .name       = "portscan",
822 +       .family     = AF_INET,
823 +       .match      = portscan_mt,
824 +       .checkentry = portscan_mt_check,
825 +       .matchsize  = sizeof(struct xt_portscan_match_info),
826 +       .proto      = IPPROTO_TCP,
827 +       .me         = THIS_MODULE,
828 +};
829 +
830 +static int __init portscan_mt_init(void)
831 +{
832 +       return xt_register_match(&portscan_mt_reg);
833 +}
834 +
835 +static void __exit portscan_mt_exit(void)
836 +{
837 +       xt_unregister_match(&portscan_mt_reg);
838 +       return;
839 +}
840 +
841 +module_init(portscan_mt_init);
842 +module_exit(portscan_mt_exit);
843 +MODULE_AUTHOR("Jan Engelhardt <jengelh@computergmbh.de>");
844 +MODULE_DESCRIPTION("netfilter \"portscan\" match");
845 +MODULE_LICENSE("GPL");
846 +MODULE_ALIAS("ipt_portscan");
847 --- a/drivers/char/random.c
848 +++ b/drivers/char/random.c
849 @@ -1574,6 +1574,8 @@ __u32 secure_tcp_sequence_number(__be32 
850         return seq;
851  }
852  
853 +EXPORT_SYMBOL(secure_tcp_sequence_number);
854 +
855  /* Generate secure starting point for ephemeral IPV4 transport port search */
856  u32 secure_ipv4_port_ephemeral(__be32 saddr, __be32 daddr, __be16 dport)
857  {