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