fix chaostables patch to have headers installed when using 'make -C $(KDIR) headers_i...
[openwrt/svn-archive/archive.git] / target / linux / generic-2.6 / patches-2.6.25 / 170-netfilter_chaostables_0.8.patch
1 --- a/include/linux/netfilter/Kbuild
2 +++ b/include/linux/netfilter/Kbuild
3 @@ -3,6 +3,7 @@
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 @@ -276,6 +276,14 @@ config 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_XTABLES
123 @@ -305,6 +313,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'.
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 NETFILTER_XTABLES
138 @@ -640,6 +656,14 @@ config NETFILTER_XT_MATCH_POLICY
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_XTABLES
153 --- a/net/netfilter/Makefile
154 +++ b/net/netfilter/Makefile
155 @@ -50,6 +50,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
161
162 # matches
163 obj-$(CONFIG_NETFILTER_XT_MATCH_COMMENT) += xt_comment.o
164 @@ -83,3 +85,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
169 --- /dev/null
170 +++ b/net/netfilter/xt_CHAOS.c
171 @@ -0,0 +1,200 @@
172 +/*
173 + * CHAOS target for netfilter
174 + * Copyright © CC Computer Consultants GmbH, 2006 - 2007
175 + * Contact: Jan Engelhardt <jengelh@computergmbh.de>
176 + *
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.
180 + */
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>
190 +#include <net/ip.h>
191 +#if defined(_LOCAL)
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"
198 +#else
199 +# include "xt_CHAOS.h"
200 +# include "find_match.c"
201 +#endif
202 +#define PFX KBUILD_MODNAME ": "
203 +
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);
209 +
210 +/* References to other matches/targets */
211 +static struct xt_match *xm_tcp;
212 +static struct xt_target *xt_delude, *xt_reject, *xt_tarpit;
213 +
214 +static int have_delude, have_tarpit;
215 +
216 +/* Static data for other matches/targets */
217 +static const struct ipt_reject_info reject_params = {
218 + .with = ICMP_HOST_UNREACH,
219 +};
220 +
221 +static const struct xt_tcp tcp_params = {
222 + .spts = {0, ~0},
223 + .dpts = {0, ~0},
224 +};
225 +
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)
230 +{
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;
236 +
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)
240 + return;
241 +
242 + destiny = (info->variant == XTCHAOS_TARPIT) ? xt_tarpit : xt_delude;
243 + destiny->target(skb, in, out, hooknum, destiny, NULL);
244 + return;
245 +}
246 +
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)
250 +{
251 + /*
252 + * Equivalent to:
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;
258 + */
259 + const struct xt_chaos_target_info *info = targinfo;
260 + const struct iphdr *iph = ip_hdr(skb);
261 +
262 + if ((unsigned int)net_random() <= reject_percentage)
263 + return xt_reject->target(skb, in, out, hooknum, target,
264 + &reject_params);
265 +
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);
270 +
271 + return NF_DROP;
272 +}
273 +
274 +static bool chaos_tg_check(const char *tablename, const void *entry,
275 + const struct xt_target *target, void *targinfo, unsigned int hook_mask)
276 +{
277 + const struct xt_chaos_target_info *info = targinfo;
278 +
279 + if (info->variant == XTCHAOS_DELUDE && !have_delude) {
280 + printk(KERN_WARNING PFX "Error: Cannot use --delude when "
281 + "DELUDE module not available\n");
282 + return false;
283 + }
284 + if (info->variant == XTCHAOS_TARPIT && !have_tarpit) {
285 + printk(KERN_WARNING PFX "Error: Cannot use --tarpit when "
286 + "TARPIT module not available\n");
287 + return false;
288 + }
289 +
290 + return true;
291 +}
292 +
293 +static struct xt_target chaos_tg_reg = {
294 + .name = "CHAOS",
295 + .family = AF_INET,
296 + .table = "filter",
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),
302 + .me = THIS_MODULE,
303 +};
304 +
305 +static int __init chaos_tg_init(void)
306 +{
307 + int ret = -EINVAL;
308 +
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");
313 + return -EINVAL;
314 + }
315 +
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");
320 + goto out2;
321 + }
322 +
323 + xt_tarpit = xt_request_find_target(AF_INET, "TARPIT", 0);
324 + have_tarpit = xt_tarpit != NULL;
325 + if (!have_tarpit)
326 + printk(KERN_WARNING PFX "Warning: Could not find or load "
327 + "\"TARPIT\" target\n");
328 +
329 + xt_delude = xt_request_find_target(AF_INET, "DELUDE", 0);
330 + have_delude = xt_delude != NULL;
331 + if (!have_delude)
332 + printk(KERN_WARNING PFX "Warning: Could not find or load "
333 + "\"DELUDE\" target\n");
334 +
335 + if ((ret = xt_register_target(&chaos_tg_reg)) != 0) {
336 + printk(KERN_WARNING PFX "xt_register_target returned "
337 + "error %d\n", ret);
338 + goto out3;
339 + }
340 +
341 + return 0;
342 +
343 + out3:
344 + if (have_delude)
345 + module_put(xt_delude->me);
346 + if (have_tarpit)
347 + module_put(xt_tarpit->me);
348 + module_put(xt_reject->me);
349 + out2:
350 + module_put(xm_tcp->me);
351 + return ret;
352 +}
353 +
354 +static void __exit chaos_tg_exit(void)
355 +{
356 + xt_unregister_target(&chaos_tg_reg);
357 + module_put(xm_tcp->me);
358 + module_put(xt_reject->me);
359 + if (have_delude)
360 + module_put(xt_delude->me);
361 + if (have_tarpit)
362 + module_put(xt_tarpit->me);
363 + return;
364 +}
365 +
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");
372 --- /dev/null
373 +++ b/net/netfilter/xt_DELUDE.c
374 @@ -0,0 +1,197 @@
375 +/*
376 + * DELUDE target
377 + * Copyright © CC Computer Consultants GmbH, 2007
378 + * Contact: Jan Engelhardt <jengelh@computergmbh.de>
379 + *
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>
383 + *
384 + * xt_DELUDE acts like REJECT, but does reply with SYN-ACK on SYN.
385 + *
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.
389 + */
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>
397 +#endif
398 +#include <net/tcp.h>
399 +#define PFX KBUILD_MODNAME ": "
400 +
401 +static void delude_send_reset(struct sk_buff *oldskb, unsigned int hook)
402 +{
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;
409 + bool needs_ack;
410 +
411 + /* IP header checks: fragment. */
412 + if (ip_hdr(oldskb)->frag_off & htons(IP_OFFSET))
413 + return;
414 +
415 + oth = skb_header_pointer(oldskb, ip_hdrlen(oldskb),
416 + sizeof(_otcph), &_otcph);
417 + if (oth == NULL)
418 + return;
419 +
420 + /* No RST for RST. */
421 + if (oth->rst)
422 + return;
423 +
424 + /* Check checksum */
425 + if (nf_ip_checksum(oldskb, hook, ip_hdrlen(oldskb), IPPROTO_TCP))
426 + return;
427 +
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),
432 + GFP_ATOMIC);
433 + if (!nskb)
434 + return;
435 +
436 + /* This packet will not be the same as the other: clear nf fields */
437 + nf_reset(nskb);
438 + nskb->mark = 0;
439 + skb_init_secmark(nskb);
440 +
441 + skb_shinfo(nskb)->gso_size = 0;
442 + skb_shinfo(nskb)->gso_segs = 0;
443 + skb_shinfo(nskb)->gso_type = 0;
444 +
445 + tcph = (struct tcphdr *)(skb_network_header(nskb) + ip_hdrlen(nskb));
446 +
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;
455 +
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);
460 +
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) -
465 + (oth->doff << 2));
466 + tcph->seq = false;
467 + tcph->ack = true;
468 + } else {
469 + if (!tcph->ack) {
470 + needs_ack = true;
471 + tcph->ack_seq = htonl(ntohl(oth->seq) + oth->syn +
472 + oth->fin + oldskb->len -
473 + ip_hdrlen(oldskb) - (oth->doff<<2));
474 + tcph->seq = false;
475 + } else {
476 + needs_ack = false;
477 + tcph->seq = oth->ack_seq;
478 + tcph->ack_seq = false;
479 + }
480 +
481 + /* Reset flags */
482 + ((u_int8_t *)tcph)[13] = 0;
483 + tcph->rst = true;
484 + tcph->ack = needs_ack;
485 + }
486 +
487 + tcph->window = 0;
488 + tcph->urg_ptr = 0;
489 +
490 + /* Adjust TCP checksum */
491 + tcph->check = 0;
492 + tcph->check = tcp_v4_check(sizeof(struct tcphdr), niph->saddr,
493 + niph->daddr, csum_partial((char *)tcph,
494 + sizeof(struct tcphdr), 0));
495 +
496 + /* Set DF, id = 0 */
497 + niph->frag_off = htons(IP_DF);
498 + niph->id = 0;
499 +
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))
504 +#else
505 + if (hook != NF_INET_FORWARD)
506 +#endif
507 + addr_type = RTN_LOCAL;
508 +
509 + if (ip_route_me_harder(nskb, addr_type))
510 + goto free_nskb;
511 +
512 + nskb->ip_summed = CHECKSUM_NONE;
513 +
514 + /* Adjust IP TTL */
515 + niph->ttl = dst_metric(nskb->dst, RTAX_HOPLIMIT);
516 +
517 + /* Adjust IP checksum */
518 + niph->check = 0;
519 + niph->check = ip_fast_csum(skb_network_header(nskb), niph->ihl);
520 +
521 + /* "Never happens" */
522 + if (nskb->len > dst_mtu(nskb->dst))
523 + goto free_nskb;
524 +
525 + nf_ct_attach(nskb, oldskb);
526 +
527 + NF_HOOK(PF_INET, NF_INET_LOCAL_OUT, nskb, NULL, nskb->dst->dev,
528 + dst_output);
529 + return;
530 +
531 + free_nskb:
532 + kfree_skb(nskb);
533 +}
534 +
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)
538 +{
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);
543 + return NF_DROP;
544 +}
545 +
546 +static struct xt_target delude_tg_reg = {
547 + .name = "DELUDE",
548 + .family = AF_INET,
549 + .table = "filter",
550 + .hooks = (1 << NF_INET_LOCAL_IN) | (1 << NF_INET_FORWARD),
551 + .target = delude_tg,
552 + .proto = IPPROTO_TCP,
553 + .me = THIS_MODULE,
554 +};
555 +
556 +static int __init delude_tg_init(void)
557 +{
558 + return xt_register_target(&delude_tg_reg);
559 +}
560 +
561 +static void __exit delude_tg_exit(void)
562 +{
563 + xt_unregister_target(&delude_tg_reg);
564 +}
565 +
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");
572 --- /dev/null
573 +++ b/net/netfilter/xt_portscan.c
574 @@ -0,0 +1,269 @@
575 +/*
576 + * portscan match for netfilter
577 + * Copyright © CC Computer Consultants GmbH, 2006 - 2007
578 + * Contact: Jan Engelhardt <jengelh@computergmbh.de>
579 + *
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.
583 + */
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>
596 +#if defined(_LOCAL)
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>
601 +#else
602 +# include "xt_portscan.h"
603 +#endif
604 +#define PFX KBUILD_MODNAME ": "
605 +
606 +enum {
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,
610 +};
611 +
612 +/* Module parameters */
613 +static unsigned int
614 + connmark_mask = ~0,
615 + packet_mask = ~0,
616 + mark_seen = 0x9,
617 + mark_synrcv = 0x1,
618 + mark_closed = 0x2,
619 + mark_synscan = 0x3,
620 + mark_estab1 = 0x4,
621 + mark_estab2 = 0x5,
622 + mark_cnscan = 0x6,
623 + mark_grscan = 0x7,
624 + mark_valid = 0x8;
625 +
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");
648 +
649 +/* TCP flag functions */
650 +static inline bool tflg_ack4(const struct tcphdr *th)
651 +{
652 + return (tcp_flag_word(th) & TCP_FLAGS_ALL4) == TCP_FLAG_ACK;
653 +}
654 +
655 +static inline bool tflg_ack6(const struct tcphdr *th)
656 +{
657 + return (tcp_flag_word(th) & TCP_FLAGS_ALL6) == TCP_FLAG_ACK;
658 +}
659 +
660 +static inline bool tflg_fin(const struct tcphdr *th)
661 +{
662 + return (tcp_flag_word(th) & TCP_FLAGS_ALL3) == TCP_FLAG_FIN;
663 +}
664 +
665 +static inline bool tflg_rst(const struct tcphdr *th)
666 +{
667 + return (tcp_flag_word(th) & TCP_FLAGS_ALL3) == TCP_FLAG_RST;
668 +}
669 +
670 +static inline bool tflg_rstack(const struct tcphdr *th)
671 +{
672 + return (tcp_flag_word(th) & TCP_FLAGS_ALL4) ==
673 + (TCP_FLAG_ACK | TCP_FLAG_RST);
674 +}
675 +
676 +static inline bool tflg_syn(const struct tcphdr *th)
677 +{
678 + return (tcp_flag_word(th) & TCP_FLAGS_ALL4) == TCP_FLAG_SYN;
679 +}
680 +
681 +static inline bool tflg_synack(const struct tcphdr *th)
682 +{
683 + return (tcp_flag_word(th) & TCP_FLAGS_ALL4) ==
684 + (TCP_FLAG_SYN | TCP_FLAG_ACK);
685 +}
686 +
687 +/* portscan functions */
688 +static inline bool portscan_mt_stealth(const struct tcphdr *th)
689 +{
690 + /*
691 + * "Connection refused" replies to our own probes must not be matched.
692 + */
693 + if (tflg_rstack(th))
694 + return false;
695 +
696 + if (tflg_rst(th) && printk_ratelimit()) {
697 + printk(KERN_WARNING PFX "Warning: Pure RST received\n");
698 + return false;
699 + }
700 +
701 + /*
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.).
706 + */
707 + return !tflg_syn(th);
708 +}
709 +
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)
713 +{
714 + if (mark == mark_estab2) {
715 + /*
716 + * -m connmark --mark $ESTAB2
717 + */
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;
722 + else
723 + return mark_valid;
724 + } else if (mark == mark_estab1) {
725 + /*
726 + * -m connmark --mark $ESTAB1
727 + */
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;
732 + else
733 + return mark_valid;
734 + } else if (mark == mark_synrcv) {
735 + /*
736 + * -m connmark --mark $SYN
737 + */
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;
744 + else
745 + return mark_synscan;
746 + } else if (ctstate == IP_CT_NEW && tflg_syn(tcph)) {
747 + /*
748 + * -p tcp --syn --ctstate NEW
749 + */
750 + return mark_synrcv;
751 + }
752 + return mark;
753 +}
754 +
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)
759 +{
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;
765 +
766 + tcph = skb_header_pointer(skb, protoff, sizeof(tcph_buf), &tcph_buf);
767 + if (tcph == NULL)
768 + return false;
769 +
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);
774 + /*
775 + * If @ctdata is NULL, we cannot match the other scan
776 + * types, return.
777 + */
778 + return false;
779 + }
780 +
781 + /*
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.
785 + */
786 + if ((ctdata->mark & connmark_mask) == mark_valid ||
787 + (skb->mark & packet_mask) != mark_seen) {
788 + unsigned int n;
789 +
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);
793 +
794 + ctdata->mark = (ctdata->mark & ~connmark_mask) | n;
795 + ((struct sk_buff *)skb)->mark =
796 + (skb->mark & ~packet_mask) ^ mark_seen;
797 + }
798 +
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);
802 +}
803 +
804 +static bool portscan_mt_check(const char *tablename, const void *entry,
805 + const struct xt_match *match, void *matchinfo, unsigned int hook_mask)
806 +{
807 + const struct xt_portscan_match_info *info = matchinfo;
808 +
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");
812 + return false;
813 + }
814 + return true;
815 +}
816 +
817 +static struct xt_match portscan_mt_reg __read_mostly = {
818 + .name = "portscan",
819 + .family = AF_INET,
820 + .match = portscan_mt,
821 + .checkentry = portscan_mt_check,
822 + .matchsize = sizeof(struct xt_portscan_match_info),
823 + .proto = IPPROTO_TCP,
824 + .me = THIS_MODULE,
825 +};
826 +
827 +static int __init portscan_mt_init(void)
828 +{
829 + return xt_register_match(&portscan_mt_reg);
830 +}
831 +
832 +static void __exit portscan_mt_exit(void)
833 +{
834 + xt_unregister_match(&portscan_mt_reg);
835 + return;
836 +}
837 +
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 @@ -1563,6 +1563,8 @@ __u32 secure_tcp_sequence_number(__be32
847 return seq;
848 }
849
850 +EXPORT_SYMBOL(secure_tcp_sequence_number);
851 +
852 /* Generate secure starting point for ephemeral IPV4 transport port search */
853 u32 secure_ipv4_port_ephemeral(__be32 saddr, __be32 daddr, __be16 dport)
854 {