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
12 +++ b/include/linux/netfilter/oot_conntrack.h
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>
20 +++ b/include/linux/netfilter/oot_trans.h
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
29 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
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))
37 +++ b/include/linux/netfilter/xt_CHAOS.h
39 +#ifndef _LINUX_NETFILTER_XT_CHAOS_H
40 +#define _LINUX_NETFILTER_XT_CHAOS_H 1
42 +enum xt_chaos_target_variant {
48 +struct xt_chaos_target_info {
52 +#endif /* _LINUX_NETFILTER_XT_CHAOS_H */
54 +++ b/include/linux/netfilter/xt_portscan.h
56 +#ifndef _LINUX_NETFILTER_XT_PORTSCAN_H
57 +#define _LINUX_NETFILTER_XT_PORTSCAN_H 1
59 +struct xt_portscan_match_info {
60 + uint8_t match_stealth, match_syn, match_cn, match_gr;
63 +#endif /* _LINUX_NETFILTER_XT_PORTSCAN_H */
65 +++ b/net/netfilter/find_match.c
68 + xt_request_find_match
69 + by Jan Engelhardt <jengelh [at] gmx de>, 2006 - 2007
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.
77 +#include <linux/err.h>
78 +#include <linux/netfilter_arp.h>
79 +#include <linux/socket.h>
80 +#include <linux/netfilter/x_tables.h>
83 + * Yeah this code is sub-optimal, but the function is missing in
84 + * mainline so far. -jengelh
86 +static struct xt_match *xt_request_find_match_lo(int af, const char *name,
89 + static const char *const xt_prefix[] = {
94 + struct xt_match *match;
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)
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 @@ -296,6 +296,14 @@ config NETFILTER_XTABLES
110 # alphabetically ordered list of targets
112 +config NETFILTER_XT_TARGET_CHAOS
113 + tristate '"CHAOS" target support'
114 + depends on NETFILTER_XTABLES
116 + This option adds a `CHAOS' target.
118 + To compile it as a module, choose M here. If unsure, say N.
120 config NETFILTER_XT_TARGET_CLASSIFY
121 tristate '"CLASSIFY" target support'
122 depends on NETFILTER_XTABLES
123 @@ -325,6 +333,14 @@ config NETFILTER_XT_TARGET_CONNMARK
124 <file:Documentation/kbuild/modules.txt>. The module will be called
125 ipt_CONNMARK.ko. If unsure, say `N'.
127 +config NETFILTER_XT_TARGET_DELUDE
128 + tristate '"DELUDE" target support'
129 + depends on NETFILTER_XTABLES
131 + This option adds a `DELUDE' target.
133 + To compile it as a module, choose M here. If unsure, say N.
135 config NETFILTER_XT_TARGET_DSCP
136 tristate '"DSCP" and "TOS" target support'
137 depends on NETFILTER_XTABLES
138 @@ -661,6 +677,14 @@ config NETFILTER_XT_MATCH_POLICY
140 To compile it as a module, choose M here. If unsure, say N.
142 +config NETFILTER_XT_MATCH_PORTSCAN
143 + tristate '"portscan" match support'
144 + depends on NETFILTER_XTABLES
146 + This option adds a 'portscan' match support.
148 + To compile it as a module, choose M here. If unsure, say N.
150 config NETFILTER_XT_MATCH_MULTIPORT
151 tristate '"multiport" Multiple port match support'
152 depends on NETFILTER_XTABLES
153 --- a/net/netfilter/Makefile
154 +++ b/net/netfilter/Makefile
155 @@ -51,6 +51,8 @@ obj-$(CONFIG_NETFILTER_XT_TARGET_SECMARK
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
163 obj-$(CONFIG_NETFILTER_XT_MATCH_COMMENT) += xt_comment.o
164 @@ -84,3 +86,4 @@ 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
170 +++ b/net/netfilter/xt_CHAOS.c
173 + * CHAOS target for netfilter
174 + * Copyright © CC Computer Consultants GmbH, 2006 - 2007
175 + * Contact: Jan Engelhardt <jengelh@computergmbh.de>
177 + * This program is free software; you can redistribute it and/or modify
178 + * it under the terms of the GNU General Public License; either version
179 + * 2 or 3 as published by the Free Software Foundation.
181 +#include <linux/icmp.h>
182 +#include <linux/in.h>
183 +#include <linux/ip.h>
184 +#include <linux/module.h>
185 +#include <linux/skbuff.h>
186 +#include <linux/stat.h>
187 +#include <linux/netfilter/x_tables.h>
188 +#include <linux/netfilter/xt_tcpudp.h>
189 +#include <linux/netfilter_ipv4/ipt_REJECT.h>
192 +# include "xt_CHAOS.h"
193 +# include "find_match.c"
194 +#elif defined(CONFIG_NETFILTER_XT_TARGET_CHAOS) || \
195 + defined(CONFIG_NETFILTER_XT_TARGET_CHAOS_MODULE)
196 +# include <linux/netfilter/xt_CHAOS.h>
197 +# include "find_match.c"
199 +# include "xt_CHAOS.h"
200 +# include "find_match.c"
202 +#define PFX KBUILD_MODNAME ": "
204 +/* Module parameters */
205 +static unsigned int reject_percentage = ~0U * .01;
206 +static unsigned int delude_percentage = ~0U * .0101;
207 +module_param(reject_percentage, uint, S_IRUGO | S_IWUSR);
208 +module_param(delude_percentage, uint, S_IRUGO | S_IWUSR);
210 +/* References to other matches/targets */
211 +static struct xt_match *xm_tcp;
212 +static struct xt_target *xt_delude, *xt_reject, *xt_tarpit;
214 +static int have_delude, have_tarpit;
216 +/* Static data for other matches/targets */
217 +static const struct ipt_reject_info reject_params = {
218 + .with = ICMP_HOST_UNREACH,
221 +static const struct xt_tcp tcp_params = {
226 +/* CHAOS functions */
227 +static void xt_chaos_total(const struct xt_chaos_target_info *info,
228 + struct sk_buff *skb, const struct net_device *in,
229 + const struct net_device *out, unsigned int hooknum)
231 + const struct iphdr *iph = ip_hdr(skb);
232 + const int protoff = 4 * iph->ihl;
233 + const int offset = ntohs(iph->frag_off) & IP_OFFSET;
234 + const struct xt_target *destiny;
235 + bool hotdrop = false, ret;
237 + ret = xm_tcp->match(skb, in, out, xm_tcp, &tcp_params,
238 + offset, protoff, &hotdrop);
239 + if (!ret || hotdrop || (unsigned int)net_random() > delude_percentage)
242 + destiny = (info->variant == XTCHAOS_TARPIT) ? xt_tarpit : xt_delude;
243 + destiny->target(skb, in, out, hooknum, destiny, NULL);
247 +static unsigned int chaos_tg(struct sk_buff *skb,
248 + const struct net_device *in, const struct net_device *out,
249 + unsigned int hooknum, const struct xt_target *target, const void *targinfo)
253 + * -A chaos -m statistic --mode random --probability \
254 + * $reject_percentage -j REJECT --reject-with host-unreach;
255 + * -A chaos -p tcp -m statistic --mode random --probability \
256 + * $delude_percentage -j DELUDE;
257 + * -A chaos -j DROP;
259 + const struct xt_chaos_target_info *info = targinfo;
260 + const struct iphdr *iph = ip_hdr(skb);
262 + if ((unsigned int)net_random() <= reject_percentage)
263 + return xt_reject->target(skb, in, out, hooknum, target,
266 + /* TARPIT/DELUDE may not be called from the OUTPUT chain */
267 + if (iph->protocol == IPPROTO_TCP &&
268 + info->variant != XTCHAOS_NORMAL && hooknum != NF_INET_LOCAL_OUT)
269 + xt_chaos_total(info, skb, in, out, hooknum);
274 +static bool chaos_tg_check(const char *tablename, const void *entry,
275 + const struct xt_target *target, void *targinfo, unsigned int hook_mask)
277 + const struct xt_chaos_target_info *info = targinfo;
279 + if (info->variant == XTCHAOS_DELUDE && !have_delude) {
280 + printk(KERN_WARNING PFX "Error: Cannot use --delude when "
281 + "DELUDE module not available\n");
284 + if (info->variant == XTCHAOS_TARPIT && !have_tarpit) {
285 + printk(KERN_WARNING PFX "Error: Cannot use --tarpit when "
286 + "TARPIT module not available\n");
293 +static struct xt_target chaos_tg_reg = {
297 + .hooks = (1 << NF_INET_LOCAL_IN) | (1 << NF_INET_FORWARD) |
298 + (1 << NF_INET_LOCAL_OUT),
299 + .checkentry = chaos_tg_check,
300 + .target = chaos_tg,
301 + .targetsize = sizeof(struct xt_chaos_target_info),
305 +static int __init chaos_tg_init(void)
309 + xm_tcp = xt_request_find_match(AF_INET, "tcp", 0);
310 + if (xm_tcp == NULL) {
311 + printk(KERN_WARNING PFX "Error: Could not find or load "
312 + "\"tcp\" match\n");
316 + xt_reject = xt_request_find_target(AF_INET, "REJECT", 0);
317 + if (xt_reject == NULL) {
318 + printk(KERN_WARNING PFX "Error: Could not find or load "
319 + "\"REJECT\" target\n");
323 + xt_tarpit = xt_request_find_target(AF_INET, "TARPIT", 0);
324 + have_tarpit = xt_tarpit != NULL;
326 + printk(KERN_WARNING PFX "Warning: Could not find or load "
327 + "\"TARPIT\" target\n");
329 + xt_delude = xt_request_find_target(AF_INET, "DELUDE", 0);
330 + have_delude = xt_delude != NULL;
332 + printk(KERN_WARNING PFX "Warning: Could not find or load "
333 + "\"DELUDE\" target\n");
335 + if ((ret = xt_register_target(&chaos_tg_reg)) != 0) {
336 + printk(KERN_WARNING PFX "xt_register_target returned "
337 + "error %d\n", ret);
345 + module_put(xt_delude->me);
347 + module_put(xt_tarpit->me);
348 + module_put(xt_reject->me);
350 + module_put(xm_tcp->me);
354 +static void __exit chaos_tg_exit(void)
356 + xt_unregister_target(&chaos_tg_reg);
357 + module_put(xm_tcp->me);
358 + module_put(xt_reject->me);
360 + module_put(xt_delude->me);
362 + module_put(xt_tarpit->me);
366 +module_init(chaos_tg_init);
367 +module_exit(chaos_tg_exit);
368 +MODULE_AUTHOR("Jan Engelhardt <jengelh@computergmbh.de>");
369 +MODULE_DESCRIPTION("netfilter \"CHAOS\" target");
370 +MODULE_LICENSE("GPL");
371 +MODULE_ALIAS("ipt_CHAOS");
373 +++ b/net/netfilter/xt_DELUDE.c
377 + * Copyright © CC Computer Consultants GmbH, 2007
378 + * Contact: Jan Engelhardt <jengelh@computergmbh.de>
380 + * Based upon linux-2.6.18.5/net/ipv4/netfilter/ipt_REJECT.c:
381 + * (C) 1999-2001 Paul `Rusty' Russell
382 + * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
384 + * xt_DELUDE acts like REJECT, but does reply with SYN-ACK on SYN.
386 + * This program is free software; you can redistribute it and/or modify
387 + * it under the terms of the GNU General Public License version 2 as
388 + * published by the Free Software Foundation.
390 +#include <linux/module.h>
391 +#include <linux/skbuff.h>
392 +#include <linux/ip.h>
393 +#include <linux/tcp.h>
394 +#include <linux/netfilter/x_tables.h>
395 +#ifdef CONFIG_BRIDGE_NETFILTER
396 +# include <linux/netfilter_bridge.h>
398 +#include <net/tcp.h>
399 +#define PFX KBUILD_MODNAME ": "
401 +static void delude_send_reset(struct sk_buff *oldskb, unsigned int hook)
403 + struct tcphdr _otcph, *oth, *tcph;
404 + unsigned int addr_type;
405 + struct sk_buff *nskb;
406 + u_int16_t tmp_port;
407 + u_int32_t tmp_addr;
408 + struct iphdr *niph;
411 + /* IP header checks: fragment. */
412 + if (ip_hdr(oldskb)->frag_off & htons(IP_OFFSET))
415 + oth = skb_header_pointer(oldskb, ip_hdrlen(oldskb),
416 + sizeof(_otcph), &_otcph);
420 + /* No RST for RST. */
424 + /* Check checksum */
425 + if (nf_ip_checksum(oldskb, hook, ip_hdrlen(oldskb), IPPROTO_TCP))
428 + /* We need a linear, writeable skb. We also need to expand
429 + headroom in case hh_len of incoming interface < hh_len of
430 + outgoing interface */
431 + nskb = skb_copy_expand(oldskb, LL_MAX_HEADER, skb_tailroom(oldskb),
436 + /* This packet will not be the same as the other: clear nf fields */
439 + skb_init_secmark(nskb);
441 + skb_shinfo(nskb)->gso_size = 0;
442 + skb_shinfo(nskb)->gso_segs = 0;
443 + skb_shinfo(nskb)->gso_type = 0;
445 + tcph = (struct tcphdr *)(skb_network_header(nskb) + ip_hdrlen(nskb));
447 + /* Swap source and dest */
448 + niph = ip_hdr(nskb);
449 + tmp_addr = niph->saddr;
450 + niph->saddr = niph->daddr;
451 + niph->daddr = tmp_addr;
452 + tmp_port = tcph->source;
453 + tcph->source = tcph->dest;
454 + tcph->dest = tmp_port;
456 + /* Truncate to length (no data) */
457 + tcph->doff = sizeof(struct tcphdr) / 4;
458 + skb_trim(nskb, ip_hdrlen(nskb) + sizeof(struct tcphdr));
459 + niph->tot_len = htons(nskb->len);
461 + if (oth->syn && !oth->ack && !oth->rst && !oth->fin) {
462 + /* DELUDE essential part */
463 + tcph->ack_seq = htonl(ntohl(oth->seq) + oth->syn + oth->fin +
464 + oldskb->len - ip_hdrlen(oldskb) -
471 + tcph->ack_seq = htonl(ntohl(oth->seq) + oth->syn +
472 + oth->fin + oldskb->len -
473 + ip_hdrlen(oldskb) - (oth->doff<<2));
477 + tcph->seq = oth->ack_seq;
478 + tcph->ack_seq = false;
482 + ((u_int8_t *)tcph)[13] = 0;
484 + tcph->ack = needs_ack;
490 + /* Adjust TCP checksum */
492 + tcph->check = tcp_v4_check(sizeof(struct tcphdr), niph->saddr,
493 + niph->daddr, csum_partial((char *)tcph,
494 + sizeof(struct tcphdr), 0));
496 + /* Set DF, id = 0 */
497 + niph->frag_off = htons(IP_DF);
500 + addr_type = RTN_UNSPEC;
501 +#ifdef CONFIG_BRIDGE_NETFILTER
502 + if (hook != NF_INET_FORWARD || (nskb->nf_bridge != NULL &&
503 + nskb->nf_bridge->mask & BRNF_BRIDGED))
505 + if (hook != NF_INET_FORWARD)
507 + addr_type = RTN_LOCAL;
509 + if (ip_route_me_harder(nskb, addr_type))
512 + nskb->ip_summed = CHECKSUM_NONE;
514 + /* Adjust IP TTL */
515 + niph->ttl = dst_metric(nskb->dst, RTAX_HOPLIMIT);
517 + /* Adjust IP checksum */
519 + niph->check = ip_fast_csum(skb_network_header(nskb), niph->ihl);
521 + /* "Never happens" */
522 + if (nskb->len > dst_mtu(nskb->dst))
525 + nf_ct_attach(nskb, oldskb);
527 + NF_HOOK(PF_INET, NF_INET_LOCAL_OUT, nskb, NULL, nskb->dst->dev,
535 +static unsigned int delude_tg(struct sk_buff *skb,
536 + const struct net_device *in, const struct net_device *out,
537 + unsigned int hooknum, const struct xt_target *target, const void *targinfo)
539 + /* WARNING: This code causes reentry within iptables.
540 + This means that the iptables jump stack is now crap. We
541 + must return an absolute verdict. --RR */
542 + delude_send_reset(skb, hooknum);
546 +static struct xt_target delude_tg_reg = {
550 + .hooks = (1 << NF_INET_LOCAL_IN) | (1 << NF_INET_FORWARD),
551 + .target = delude_tg,
552 + .proto = IPPROTO_TCP,
556 +static int __init delude_tg_init(void)
558 + return xt_register_target(&delude_tg_reg);
561 +static void __exit delude_tg_exit(void)
563 + xt_unregister_target(&delude_tg_reg);
566 +module_init(delude_tg_init);
567 +module_exit(delude_tg_exit);
568 +MODULE_AUTHOR("Jan Engelhardt <jengelh@computergmbh.de>");
569 +MODULE_DESCRIPTION("netfilter \"DELUDE\" target");
570 +MODULE_LICENSE("GPL");
571 +MODULE_ALIAS("ipt_DELUDE");
573 +++ b/net/netfilter/xt_portscan.c
576 + * portscan match for netfilter
577 + * Copyright © CC Computer Consultants GmbH, 2006 - 2007
578 + * Contact: Jan Engelhardt <jengelh@computergmbh.de>
580 + * This program is free software; you can redistribute it and/or modify
581 + * it under the terms of the GNU General Public License; either version
582 + * 2 or 3 as published by the Free Software Foundation.
584 +#include <linux/in.h>
585 +#include <linux/ip.h>
586 +#include <linux/module.h>
587 +#include <linux/moduleparam.h>
588 +#include <linux/skbuff.h>
589 +#include <linux/stat.h>
590 +#include <linux/tcp.h>
591 +#include <linux/types.h>
592 +#include <linux/version.h>
593 +#include <linux/netfilter/x_tables.h>
594 +#include <linux/netfilter/xt_tcpudp.h>
595 +#include <net/netfilter/nf_nat_rule.h>
597 +# include "xt_portscan.h"
598 +#elif defined(CONFIG_NETFILTER_XT_MATCH_PORTSCAN) || \
599 + defined(CONFIG_NETFILTER_XT_MATCH_PORTSCAN_MODULE)
600 +# include <linux/netfilter/xt_portscan.h>
602 +# include "xt_portscan.h"
604 +#define PFX KBUILD_MODNAME ": "
607 + TCP_FLAGS_ALL3 = TCP_FLAG_FIN | TCP_FLAG_RST | TCP_FLAG_SYN,
608 + TCP_FLAGS_ALL4 = TCP_FLAGS_ALL3 | TCP_FLAG_ACK,
609 + TCP_FLAGS_ALL6 = TCP_FLAGS_ALL4 | TCP_FLAG_PSH | TCP_FLAG_URG,
612 +/* Module parameters */
614 + connmark_mask = ~0,
619 + mark_synscan = 0x3,
626 +module_param(connmark_mask, uint, S_IRUGO | S_IWUSR);
627 +module_param(packet_mask, uint, S_IRUGO | S_IWUSR);
628 +module_param(mark_seen, uint, S_IRUGO | S_IWUSR);
629 +module_param(mark_synrcv, uint, S_IRUGO | S_IWUSR);
630 +module_param(mark_closed, uint, S_IRUGO | S_IWUSR);
631 +module_param(mark_synscan, uint, S_IRUGO | S_IWUSR);
632 +module_param(mark_estab1, uint, S_IRUGO | S_IWUSR);
633 +module_param(mark_estab2, uint, S_IRUGO | S_IWUSR);
634 +module_param(mark_cnscan, uint, S_IRUGO | S_IWUSR);
635 +module_param(mark_grscan, uint, S_IRUGO | S_IWUSR);
636 +module_param(mark_valid, uint, S_IRUGO | S_IWUSR);
637 +MODULE_PARM_DESC(connmark_mask, "only set specified bits in connection mark");
638 +MODULE_PARM_DESC(packet_mask, "only set specified bits in packet mark");
639 +MODULE_PARM_DESC(mark_seen, "nfmark value for packet-seen state");
640 +MODULE_PARM_DESC(mark_synrcv, "connmark value for SYN Received state");
641 +MODULE_PARM_DESC(mark_closed, "connmark value for closed state");
642 +MODULE_PARM_DESC(mark_synscan, "connmark value for SYN Scan state");
643 +MODULE_PARM_DESC(mark_estab1, "connmark value for Established-1 state");
644 +MODULE_PARM_DESC(mark_estab2, "connmark value for Established-2 state");
645 +MODULE_PARM_DESC(mark_cnscan, "connmark value for Connect Scan state");
646 +MODULE_PARM_DESC(mark_grscan, "connmark value for Grab Scan state");
647 +MODULE_PARM_DESC(mark_valid, "connmark value for Valid state");
649 +/* TCP flag functions */
650 +static inline bool tflg_ack4(const struct tcphdr *th)
652 + return (tcp_flag_word(th) & TCP_FLAGS_ALL4) == TCP_FLAG_ACK;
655 +static inline bool tflg_ack6(const struct tcphdr *th)
657 + return (tcp_flag_word(th) & TCP_FLAGS_ALL6) == TCP_FLAG_ACK;
660 +static inline bool tflg_fin(const struct tcphdr *th)
662 + return (tcp_flag_word(th) & TCP_FLAGS_ALL3) == TCP_FLAG_FIN;
665 +static inline bool tflg_rst(const struct tcphdr *th)
667 + return (tcp_flag_word(th) & TCP_FLAGS_ALL3) == TCP_FLAG_RST;
670 +static inline bool tflg_rstack(const struct tcphdr *th)
672 + return (tcp_flag_word(th) & TCP_FLAGS_ALL4) ==
673 + (TCP_FLAG_ACK | TCP_FLAG_RST);
676 +static inline bool tflg_syn(const struct tcphdr *th)
678 + return (tcp_flag_word(th) & TCP_FLAGS_ALL4) == TCP_FLAG_SYN;
681 +static inline bool tflg_synack(const struct tcphdr *th)
683 + return (tcp_flag_word(th) & TCP_FLAGS_ALL4) ==
684 + (TCP_FLAG_SYN | TCP_FLAG_ACK);
687 +/* portscan functions */
688 +static inline bool portscan_mt_stealth(const struct tcphdr *th)
691 + * "Connection refused" replies to our own probes must not be matched.
693 + if (tflg_rstack(th))
696 + if (tflg_rst(th) && printk_ratelimit()) {
697 + printk(KERN_WARNING PFX "Warning: Pure RST received\n");
702 + * -p tcp ! --syn -m conntrack --ctstate INVALID: Looking for non-start
703 + * packets that are not associated with any connection -- this will
704 + * match most scan types (NULL, XMAS, FIN) and ridiculous flag
705 + * combinations (SYN-RST, SYN-FIN, SYN-FIN-RST, FIN-RST, etc.).
707 + return !tflg_syn(th);
710 +static inline unsigned int portscan_mt_full(int mark,
711 + enum ip_conntrack_info ctstate, bool loopback, const struct tcphdr *tcph,
712 + unsigned int payload_len)
714 + if (mark == mark_estab2) {
716 + * -m connmark --mark $ESTAB2
718 + if (tflg_ack4(tcph) && payload_len == 0)
719 + return mark; /* keep mark */
720 + else if (tflg_rst(tcph) || tflg_fin(tcph))
721 + return mark_grscan;
724 + } else if (mark == mark_estab1) {
726 + * -m connmark --mark $ESTAB1
728 + if (tflg_rst(tcph) || tflg_fin(tcph))
729 + return mark_cnscan;
730 + else if (!loopback && tflg_ack4(tcph) && payload_len == 0)
731 + return mark_estab2;
734 + } else if (mark == mark_synrcv) {
736 + * -m connmark --mark $SYN
738 + if (loopback && tflg_synack(tcph))
739 + return mark; /* keep mark */
740 + else if (loopback && tflg_rstack(tcph))
741 + return mark_closed;
742 + else if (tflg_ack6(tcph))
743 + return mark_estab1;
745 + return mark_synscan;
746 + } else if (ctstate == IP_CT_NEW && tflg_syn(tcph)) {
748 + * -p tcp --syn --ctstate NEW
750 + return mark_synrcv;
755 +static bool portscan_mt(const struct sk_buff *skb,
756 + const struct net_device *in, const struct net_device *out,
757 + const struct xt_match *match, const void *matchinfo, int offset,
758 + unsigned int protoff, bool *hotdrop)
760 + const struct xt_portscan_match_info *info = matchinfo;
761 + enum ip_conntrack_info ctstate;
762 + const struct tcphdr *tcph;
763 + struct nf_conn *ctdata;
764 + struct tcphdr tcph_buf;
766 + tcph = skb_header_pointer(skb, protoff, sizeof(tcph_buf), &tcph_buf);
770 + /* Check for invalid packets: -m conntrack --ctstate INVALID */
771 + if ((ctdata = nf_ct_get(skb, &ctstate)) == NULL) {
772 + if (info->match_stealth)
773 + return portscan_mt_stealth(tcph);
775 + * If @ctdata is NULL, we cannot match the other scan
782 + * If -m portscan was previously applied to this packet, the rules we
783 + * simulate must not be run through again. And for speedup, do not call
784 + * it either when the connection is already VALID.
786 + if ((ctdata->mark & connmark_mask) == mark_valid ||
787 + (skb->mark & packet_mask) != mark_seen) {
790 + n = portscan_mt_full(ctdata->mark & connmark_mask, ctstate,
791 + (in->flags & IFF_LOOPBACK) == IFF_LOOPBACK, tcph,
792 + skb->len - protoff - 4 * tcph->doff);
794 + ctdata->mark = (ctdata->mark & ~connmark_mask) | n;
795 + ((struct sk_buff *)skb)->mark =
796 + (skb->mark & ~packet_mask) ^ mark_seen;
799 + return (info->match_syn && ctdata->mark == mark_synscan) ||
800 + (info->match_cn && ctdata->mark == mark_cnscan) ||
801 + (info->match_gr && ctdata->mark == mark_grscan);
804 +static bool portscan_mt_check(const char *tablename, const void *entry,
805 + const struct xt_match *match, void *matchinfo, unsigned int hook_mask)
807 + const struct xt_portscan_match_info *info = matchinfo;
809 + if ((info->match_stealth & ~1) || (info->match_syn & ~1) ||
810 + (info->match_cn & ~1) || (info->match_gr & ~1)) {
811 + printk(KERN_WARNING PFX "Invalid flags\n");
817 +static struct xt_match portscan_mt_reg __read_mostly = {
818 + .name = "portscan",
820 + .match = portscan_mt,
821 + .checkentry = portscan_mt_check,
822 + .matchsize = sizeof(struct xt_portscan_match_info),
823 + .proto = IPPROTO_TCP,
827 +static int __init portscan_mt_init(void)
829 + return xt_register_match(&portscan_mt_reg);
832 +static void __exit portscan_mt_exit(void)
834 + xt_unregister_match(&portscan_mt_reg);
838 +module_init(portscan_mt_init);
839 +module_exit(portscan_mt_exit);
840 +MODULE_AUTHOR("Jan Engelhardt <jengelh@computergmbh.de>");
841 +MODULE_DESCRIPTION("netfilter \"portscan\" match");
842 +MODULE_LICENSE("GPL");
843 +MODULE_ALIAS("ipt_portscan");
844 --- a/drivers/char/random.c
845 +++ b/drivers/char/random.c
846 @@ -1555,6 +1555,8 @@ __u32 secure_tcp_sequence_number(__be32
850 +EXPORT_SYMBOL(secure_tcp_sequence_number);
852 /* Generate secure starting point for ephemeral IPV4 transport port search */
853 u32 secure_ipv4_port_ephemeral(__be32 saddr, __be32 daddr, __be16 dport)