1 diff -Nur linux-2.6.6-rc1.org/include/linux/netfilter_ipv4/ip_pool.h linux-2.6.6-rc1/include/linux/netfilter_ipv4/ip_pool.h
2 --- linux-2.6.6-rc1.org/include/linux/netfilter_ipv4/ip_pool.h 1970-01-01 01:00:00.000000000 +0100
3 +++ linux-2.6.6-rc1/include/linux/netfilter_ipv4/ip_pool.h 2004-04-19 10:08:36.000000000 +0200
8 +/***************************************************************************/
9 +/* This program is free software; you can redistribute it and/or modify */
10 +/* it under the terms of the GNU General Public License as published by */
11 +/* the Free Software Foundation; either version 2 of the License, or */
12 +/* (at your option) any later version. */
14 +/* This program is distributed in the hope that it will be useful, */
15 +/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
16 +/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
17 +/* GNU General Public License for more details. */
19 +/* You should have received a copy of the GNU General Public License */
20 +/* along with this program; if not, write to the Free Software */
21 +/* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/
22 +/***************************************************************************/
24 +/* A sockopt of such quality has hardly ever been seen before on the open
25 + * market! This little beauty, hardly ever used: above 64, so it's
26 + * traditionally used for firewalling, not touched (even once!) by the
27 + * 2.0, 2.2 and 2.4 kernels!
29 + * Comes with its own certificate of authenticity, valid anywhere in the
34 +#define SO_IP_POOL 81
36 +typedef int ip_pool_t; /* pool index */
37 +#define IP_POOL_NONE ((ip_pool_t)-1)
39 +struct ip_pool_request {
46 +/* NOTE: I deliberately break the first cut ippool utility. Nobody uses it. */
48 +#define IP_POOL_BAD001 0x00000010
50 +#define IP_POOL_FLUSH 0x00000011 /* req.index, no arguments */
51 +#define IP_POOL_INIT 0x00000012 /* from addr to addr2 incl. */
52 +#define IP_POOL_DESTROY 0x00000013 /* req.index, no arguments */
53 +#define IP_POOL_ADD_ADDR 0x00000014 /* add addr to pool */
54 +#define IP_POOL_DEL_ADDR 0x00000015 /* del addr from pool */
55 +#define IP_POOL_HIGH_NR 0x00000016 /* result in req.index */
56 +#define IP_POOL_LOOKUP 0x00000017 /* result in addr and addr2 */
57 +#define IP_POOL_USAGE 0x00000018 /* result in addr */
58 +#define IP_POOL_TEST_ADDR 0x00000019 /* result (0/1) returned */
62 +/* NOTE: ip_pool_match() and ip_pool_mod() expect ADDR to be host byte order */
63 +extern int ip_pool_match(ip_pool_t pool, u_int32_t addr);
64 +extern int ip_pool_mod(ip_pool_t pool, u_int32_t addr, int isdel);
68 +#endif /*_IP_POOL_H*/
69 diff -Nur linux-2.6.6-rc1.org/include/linux/netfilter_ipv4/ipt_TTL.h linux-2.6.6-rc1/include/linux/netfilter_ipv4/ipt_TTL.h
70 --- linux-2.6.6-rc1.org/include/linux/netfilter_ipv4/ipt_TTL.h 1970-01-01 01:00:00.000000000 +0100
71 +++ linux-2.6.6-rc1/include/linux/netfilter_ipv4/ipt_TTL.h 2004-04-19 10:08:28.000000000 +0200
73 +/* TTL modification module for IP tables
74 + * (C) 2000 by Harald Welte <laforge@gnumonks.org> */
85 +#define IPT_TTL_MAXMODE IPT_TTL_DEC
87 +struct ipt_TTL_info {
94 diff -Nur linux-2.6.6-rc1.org/include/linux/netfilter_ipv4/ipt_connlimit.h linux-2.6.6-rc1/include/linux/netfilter_ipv4/ipt_connlimit.h
95 --- linux-2.6.6-rc1.org/include/linux/netfilter_ipv4/ipt_connlimit.h 1970-01-01 01:00:00.000000000 +0100
96 +++ linux-2.6.6-rc1/include/linux/netfilter_ipv4/ipt_connlimit.h 2004-04-19 10:08:29.000000000 +0200
98 +#ifndef _IPT_CONNLIMIT_H
99 +#define _IPT_CONNLIMIT_H
101 +struct ipt_connlimit_data;
103 +struct ipt_connlimit_info {
107 + struct ipt_connlimit_data *data;
109 +#endif /* _IPT_CONNLIMIT_H */
110 diff -Nur linux-2.6.6-rc1.org/include/linux/netfilter_ipv4/ipt_dstlimit.h linux-2.6.6-rc1/include/linux/netfilter_ipv4/ipt_dstlimit.h
111 --- linux-2.6.6-rc1.org/include/linux/netfilter_ipv4/ipt_dstlimit.h 1970-01-01 01:00:00.000000000 +0100
112 +++ linux-2.6.6-rc1/include/linux/netfilter_ipv4/ipt_dstlimit.h 2004-04-19 10:08:30.000000000 +0200
114 +#ifndef _IPT_DSTLIMIT_H
115 +#define _IPT_DSTLIMIT_H
117 +/* timings are in milliseconds. */
118 +#define IPT_DSTLIMIT_SCALE 10000
119 +/* 1/10,000 sec period => max of 10,000/sec. Min rate is then 429490
120 + seconds, or one every 59 hours. */
122 +/* details of this structure hidden by the implementation */
123 +struct ipt_dstlimit_htable;
125 +#define IPT_DSTLIMIT_HASH_DIP 0x0001
126 +#define IPT_DSTLIMIT_HASH_DPT 0x0002
127 +#define IPT_DSTLIMIT_HASH_SIP 0x0004
129 +struct dstlimit_cfg {
130 + u_int32_t mode; /* bitmask of IPT_DSTLIMIT_HASH_* */
131 + u_int32_t avg; /* Average secs between packets * scale */
132 + u_int32_t burst; /* Period multiplier for upper limit. */
134 + /* user specified */
135 + u_int32_t size; /* how many buckets */
136 + u_int32_t max; /* max number of entries */
137 + u_int32_t gc_interval; /* gc interval */
138 + u_int32_t expire; /* when do entries expire? */
141 +struct ipt_dstlimit_info {
142 + char name [IFNAMSIZ]; /* name */
143 + struct dstlimit_cfg cfg;
144 + struct ipt_dstlimit_htable *hinfo;
146 + /* Used internally by the kernel */
149 + struct ipt_dstlimit_info *master;
152 +#endif /*_IPT_DSTLIMIT_H*/
153 diff -Nur linux-2.6.6-rc1.org/include/linux/netfilter_ipv4/ipt_fuzzy.h linux-2.6.6-rc1/include/linux/netfilter_ipv4/ipt_fuzzy.h
154 --- linux-2.6.6-rc1.org/include/linux/netfilter_ipv4/ipt_fuzzy.h 1970-01-01 01:00:00.000000000 +0100
155 +++ linux-2.6.6-rc1/include/linux/netfilter_ipv4/ipt_fuzzy.h 2004-04-19 10:08:31.000000000 +0200
157 +#ifndef _IPT_FUZZY_H
158 +#define _IPT_FUZZY_H
160 +#include <linux/param.h>
161 +#include <linux/types.h>
163 +#define MAXFUZZYRATE 10000000
164 +#define MINFUZZYRATE 3
166 +struct ipt_fuzzy_info {
167 + u_int32_t minimum_rate;
168 + u_int32_t maximum_rate;
169 + u_int32_t packets_total;
170 + u_int32_t bytes_total;
171 + u_int32_t previous_time;
172 + u_int32_t present_time;
173 + u_int32_t mean_rate;
174 + u_int8_t acceptance_rate;
177 +#endif /*_IPT_FUZZY_H*/
178 diff -Nur linux-2.6.6-rc1.org/include/linux/netfilter_ipv4/ipt_ipv4options.h linux-2.6.6-rc1/include/linux/netfilter_ipv4/ipt_ipv4options.h
179 --- linux-2.6.6-rc1.org/include/linux/netfilter_ipv4/ipt_ipv4options.h 1970-01-01 01:00:00.000000000 +0100
180 +++ linux-2.6.6-rc1/include/linux/netfilter_ipv4/ipt_ipv4options.h 2004-04-19 10:08:32.000000000 +0200
182 +#ifndef __ipt_ipv4options_h_included__
183 +#define __ipt_ipv4options_h_included__
185 +#define IPT_IPV4OPTION_MATCH_SSRR 0x01 /* For strict source routing */
186 +#define IPT_IPV4OPTION_MATCH_LSRR 0x02 /* For loose source routing */
187 +#define IPT_IPV4OPTION_DONT_MATCH_SRR 0x04 /* any source routing */
188 +#define IPT_IPV4OPTION_MATCH_RR 0x08 /* For Record route */
189 +#define IPT_IPV4OPTION_DONT_MATCH_RR 0x10
190 +#define IPT_IPV4OPTION_MATCH_TIMESTAMP 0x20 /* For timestamp request */
191 +#define IPT_IPV4OPTION_DONT_MATCH_TIMESTAMP 0x40
192 +#define IPT_IPV4OPTION_MATCH_ROUTER_ALERT 0x80 /* For router-alert */
193 +#define IPT_IPV4OPTION_DONT_MATCH_ROUTER_ALERT 0x100
194 +#define IPT_IPV4OPTION_MATCH_ANY_OPT 0x200 /* match packet with any option */
195 +#define IPT_IPV4OPTION_DONT_MATCH_ANY_OPT 0x400 /* match packet with no option */
197 +struct ipt_ipv4options_info {
202 +#endif /* __ipt_ipv4options_h_included__ */
203 diff -Nur linux-2.6.6-rc1.org/include/linux/netfilter_ipv4/ipt_mport.h linux-2.6.6-rc1/include/linux/netfilter_ipv4/ipt_mport.h
204 --- linux-2.6.6-rc1.org/include/linux/netfilter_ipv4/ipt_mport.h 1970-01-01 01:00:00.000000000 +0100
205 +++ linux-2.6.6-rc1/include/linux/netfilter_ipv4/ipt_mport.h 2004-04-19 10:08:33.000000000 +0200
207 +#ifndef _IPT_MPORT_H
208 +#define _IPT_MPORT_H
209 +#include <linux/netfilter_ipv4/ip_tables.h>
211 +#define IPT_MPORT_SOURCE (1<<0)
212 +#define IPT_MPORT_DESTINATION (1<<1)
213 +#define IPT_MPORT_EITHER (IPT_MPORT_SOURCE|IPT_MPORT_DESTINATION)
215 +#define IPT_MULTI_PORTS 15
217 +/* Must fit inside union ipt_matchinfo: 32 bytes */
218 +/* every entry in ports[] except for the last one has one bit in pflags
219 + * associated with it. If this bit is set, the port is the first port of
220 + * a portrange, with the next entry being the last.
221 + * End of list is marked with pflags bit set and port=65535.
222 + * If 14 ports are used (last one does not have a pflag), the last port
223 + * is repeated to fill the last entry in ports[] */
226 + u_int8_t flags:2; /* Type of comparison */
227 + u_int16_t pflags:14; /* Port flags */
228 + u_int16_t ports[IPT_MULTI_PORTS]; /* Ports */
230 +#endif /*_IPT_MPORT_H*/
231 diff -Nur linux-2.6.6-rc1.org/include/linux/netfilter_ipv4/ipt_nth.h linux-2.6.6-rc1/include/linux/netfilter_ipv4/ipt_nth.h
232 --- linux-2.6.6-rc1.org/include/linux/netfilter_ipv4/ipt_nth.h 1970-01-01 01:00:00.000000000 +0100
233 +++ linux-2.6.6-rc1/include/linux/netfilter_ipv4/ipt_nth.h 2004-04-19 10:08:34.000000000 +0200
238 +#include <linux/param.h>
239 +#include <linux/types.h>
241 +#ifndef IPT_NTH_NUM_COUNTERS
242 +#define IPT_NTH_NUM_COUNTERS 16
245 +struct ipt_nth_info {
253 +#endif /*_IPT_NTH_H*/
254 diff -Nur linux-2.6.6-rc1.org/include/linux/netfilter_ipv4/ipt_osf.h linux-2.6.6-rc1/include/linux/netfilter_ipv4/ipt_osf.h
255 --- linux-2.6.6-rc1.org/include/linux/netfilter_ipv4/ipt_osf.h 1970-01-01 01:00:00.000000000 +0100
256 +++ linux-2.6.6-rc1/include/linux/netfilter_ipv4/ipt_osf.h 2004-04-19 10:08:35.000000000 +0200
261 + * Copyright (c) 2003 Evgeniy Polyakov <johnpol@2ka.mipt.ru>
264 + * This program is free software; you can redistribute it and/or modify
265 + * it under the terms of the GNU General Public License as published by
266 + * the Free Software Foundation; either version 2 of the License, or
267 + * (at your option) any later version.
269 + * This program is distributed in the hope that it will be useful,
270 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
271 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
272 + * GNU General Public License for more details.
274 + * You should have received a copy of the GNU General Public License
275 + * along with this program; if not, write to the Free Software
276 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
282 +#define MAXGENRELEN 32
283 +#define MAXDETLEN 64
285 +#define IPT_OSF_GENRE 1
286 +#define IPT_OSF_SMART 2
287 +#define IPT_OSF_LOG 4
288 +#define IPT_OSF_NETLINK 8
290 +#define IPT_OSF_LOGLEVEL_ALL 0
291 +#define IPT_OSF_LOGLEVEL_FIRST 1
293 +#include <linux/list.h>
296 +#include <netinet/ip.h>
297 +#include <netinet/tcp.h>
301 + struct list_head *prev, *next;
307 + char genre[MAXGENRELEN];
309 + unsigned long flags;
311 + int invert; /* UNSUPPORTED */
320 +/* This struct represents IANA options
321 + * http://www.iana.org/assignments/tcp-parameters
325 + unsigned char kind;
326 + unsigned char length;
332 + struct list_head flist;
337 + unsigned char genre[MAXGENRELEN];
338 + unsigned char version[MAXGENRELEN], subtype[MAXGENRELEN];
340 + /* Not needed, but for consistency with original table from Michal Zalewski */
341 + unsigned char details[MAXDETLEN];
344 + struct osf_opt opt[MAX_IPOPTLEN]; /* In case it is all NOP or EOL */
348 +struct ipt_osf_nlmsg
350 + struct osf_finger f;
357 +/* Defines for IANA option kinds */
359 +#define OSFOPT_EOL 0 /* End of options */
360 +#define OSFOPT_NOP 1 /* NOP */
361 +#define OSFOPT_MSS 2 /* Maximum segment size */
362 +#define OSFOPT_WSO 3 /* Window scale option */
363 +#define OSFOPT_SACKP 4 /* SACK permitted */
364 +#define OSFOPT_SACK 5 /* SACK */
365 +#define OSFOPT_ECHO 6
366 +#define OSFOPT_ECHOREPLY 7
367 +#define OSFOPT_TS 8 /* Timestamp option */
368 +#define OSFOPT_POCP 9 /* Partial Order Connection Permitted */
369 +#define OSFOPT_POSP 10 /* Partial Order Service Profile */
370 +/* Others are not used in current OSF */
372 +static struct osf_opt IANA_opts[] =
379 + {5, 1 ,}, /* SACK length is not defined */
385 + {11, 1,}, /* CC: Suppose 1 */
386 + {12, 1,}, /* the same */
387 + {13, 1,}, /* and here too */
389 + {15, 1,}, /* TCP Alternate Checksum Data. Length is not defined */
403 +#endif /* __KERNEL__ */
405 +#endif /* _IPT_OSF_H */
406 diff -Nur linux-2.6.6-rc1.org/include/linux/netfilter_ipv4/ipt_pool.h linux-2.6.6-rc1/include/linux/netfilter_ipv4/ipt_pool.h
407 --- linux-2.6.6-rc1.org/include/linux/netfilter_ipv4/ipt_pool.h 1970-01-01 01:00:00.000000000 +0100
408 +++ linux-2.6.6-rc1/include/linux/netfilter_ipv4/ipt_pool.h 2004-04-19 10:08:36.000000000 +0200
413 +#include <linux/netfilter_ipv4/ip_pool.h>
415 +#define IPT_POOL_INV_SRC 0x00000001
416 +#define IPT_POOL_INV_DST 0x00000002
417 +#define IPT_POOL_DEL_SRC 0x00000004
418 +#define IPT_POOL_DEL_DST 0x00000008
419 +#define IPT_POOL_INV_MOD_SRC 0x00000010
420 +#define IPT_POOL_INV_MOD_DST 0x00000020
421 +#define IPT_POOL_MOD_SRC_ACCEPT 0x00000040
422 +#define IPT_POOL_MOD_DST_ACCEPT 0x00000080
423 +#define IPT_POOL_MOD_SRC_DROP 0x00000100
424 +#define IPT_POOL_MOD_DST_DROP 0x00000200
427 +struct ipt_pool_info
434 +#endif /*_IPT_POOL_H*/
435 diff -Nur linux-2.6.6-rc1.org/include/linux/netfilter_ipv4/ipt_psd.h linux-2.6.6-rc1/include/linux/netfilter_ipv4/ipt_psd.h
436 --- linux-2.6.6-rc1.org/include/linux/netfilter_ipv4/ipt_psd.h 1970-01-01 01:00:00.000000000 +0100
437 +++ linux-2.6.6-rc1/include/linux/netfilter_ipv4/ipt_psd.h 2004-04-19 10:08:37.000000000 +0200
442 +#include <linux/param.h>
443 +#include <linux/types.h>
446 + * High port numbers have a lower weight to reduce the frequency of false
447 + * positives, such as from passive mode FTP transfers.
449 +#define PORT_WEIGHT_PRIV 3
450 +#define PORT_WEIGHT_HIGH 1
453 + * Port scan detection thresholds: at least COUNT ports need to be scanned
454 + * from the same source, with no longer than DELAY ticks between ports.
456 +#define SCAN_MIN_COUNT 7
457 +#define SCAN_MAX_COUNT (SCAN_MIN_COUNT * PORT_WEIGHT_PRIV)
458 +#define SCAN_WEIGHT_THRESHOLD SCAN_MAX_COUNT
459 +#define SCAN_DELAY_THRESHOLD (HZ * 3)
462 + * Keep track of up to LIST_SIZE source addresses, using a hash table of
463 + * HASH_SIZE entries for faster lookups, but limiting hash collisions to
464 + * HASH_MAX source addresses per the same hash value.
466 +#define LIST_SIZE 0x100
468 +#define HASH_SIZE (1 << HASH_LOG)
469 +#define HASH_MAX 0x10
471 +struct ipt_psd_info {
472 + unsigned int weight_threshold;
473 + unsigned int delay_threshold;
474 + unsigned short lo_ports_weight;
475 + unsigned short hi_ports_weight;
478 +#endif /*_IPT_PSD_H*/
479 diff -Nur linux-2.6.6-rc1.org/include/linux/netfilter_ipv4/ipt_quota.h linux-2.6.6-rc1/include/linux/netfilter_ipv4/ipt_quota.h
480 --- linux-2.6.6-rc1.org/include/linux/netfilter_ipv4/ipt_quota.h 1970-01-01 01:00:00.000000000 +0100
481 +++ linux-2.6.6-rc1/include/linux/netfilter_ipv4/ipt_quota.h 2004-04-19 10:08:37.000000000 +0200
483 +#ifndef _IPT_QUOTA_H
484 +#define _IPT_QUOTA_H
486 +/* print debug info in both kernel/netfilter module & iptable library */
487 +//#define DEBUG_IPT_QUOTA
489 +struct ipt_quota_info {
493 +#endif /*_IPT_QUOTA_H*/
494 diff -Nur linux-2.6.6-rc1.org/include/linux/netfilter_ipv4/ipt_random.h linux-2.6.6-rc1/include/linux/netfilter_ipv4/ipt_random.h
495 --- linux-2.6.6-rc1.org/include/linux/netfilter_ipv4/ipt_random.h 1970-01-01 01:00:00.000000000 +0100
496 +++ linux-2.6.6-rc1/include/linux/netfilter_ipv4/ipt_random.h 2004-04-19 10:08:38.000000000 +0200
501 +#include <linux/param.h>
502 +#include <linux/types.h>
504 +struct ipt_rand_info {
508 +#endif /*_IPT_RAND_H*/
509 diff -Nur linux-2.6.6-rc1.org/include/linux/netfilter_ipv4/ipt_realm.h linux-2.6.6-rc1/include/linux/netfilter_ipv4/ipt_realm.h
510 --- linux-2.6.6-rc1.org/include/linux/netfilter_ipv4/ipt_realm.h 1970-01-01 01:00:00.000000000 +0100
511 +++ linux-2.6.6-rc1/include/linux/netfilter_ipv4/ipt_realm.h 2004-04-19 10:08:39.000000000 +0200
513 +#ifndef _IPT_REALM_H
514 +#define _IPT_REALM_H
516 +struct ipt_realm_info {
521 +#endif /*_IPT_REALM_H*/
522 diff -Nur linux-2.6.6-rc1.org/include/linux/netfilter_ipv4/ipt_sctp.h linux-2.6.6-rc1/include/linux/netfilter_ipv4/ipt_sctp.h
523 --- linux-2.6.6-rc1.org/include/linux/netfilter_ipv4/ipt_sctp.h 1970-01-01 01:00:00.000000000 +0100
524 +++ linux-2.6.6-rc1/include/linux/netfilter_ipv4/ipt_sctp.h 2004-04-19 10:08:40.000000000 +0200
526 +#ifndef _IPT_SCTP_H_
527 +#define _IPT_SCTP_H_
529 +#define IPT_SCTP_SRC_PORTS 0x01
530 +#define IPT_SCTP_DEST_PORTS 0x02
531 +#define IPT_SCTP_CHUNK_TYPES 0x04
533 +#define IPT_SCTP_VALID_FLAGS 0x07
535 +#define ELEMCOUNT(x) (sizeof(x)/sizeof(x[0]))
538 +struct ipt_sctp_flag_info {
539 + u_int8_t chunktype;
541 + u_int8_t flag_mask;
544 +#define IPT_NUM_SCTP_FLAGS 4
546 +struct ipt_sctp_info {
547 + u_int16_t dpts[2]; /* Min, Max */
548 + u_int16_t spts[2]; /* Min, Max */
550 + u_int32_t chunkmap[256 / sizeof (u_int32_t)]; /* Bit mask of chunks to be matched according to RFC 2960 */
552 +#define SCTP_CHUNK_MATCH_ANY 0x01 /* Match if any of the chunk types are present */
553 +#define SCTP_CHUNK_MATCH_ALL 0x02 /* Match if all of the chunk types are present */
554 +#define SCTP_CHUNK_MATCH_ONLY 0x04 /* Match if these are the only chunk types present */
556 + u_int32_t chunk_match_type;
557 + struct ipt_sctp_flag_info flag_info[IPT_NUM_SCTP_FLAGS];
561 + u_int32_t invflags;
564 +#define bytes(type) (sizeof(type) * 8)
566 +#define SCTP_CHUNKMAP_SET(chunkmap, type) \
568 + chunkmap[type / bytes(u_int32_t)] |= \
569 + 1 << (type % bytes(u_int32_t)); \
572 +#define SCTP_CHUNKMAP_CLEAR(chunkmap, type) \
574 + chunkmap[type / bytes(u_int32_t)] &= \
575 + ~(1 << (type % bytes(u_int32_t))); \
578 +#define SCTP_CHUNKMAP_IS_SET(chunkmap, type) \
580 + (chunkmap[type / bytes (u_int32_t)] & \
581 + (1 << (type % bytes (u_int32_t)))) ? 1: 0; \
584 +#define SCTP_CHUNKMAP_RESET(chunkmap) \
587 + for (i = 0; i < ELEMCOUNT(chunkmap); i++) \
591 +#define SCTP_CHUNKMAP_SET_ALL(chunkmap) \
594 + for (i = 0; i < ELEMCOUNT(chunkmap); i++) \
595 + chunkmap[i] = ~0; \
598 +#define SCTP_CHUNKMAP_COPY(destmap, srcmap) \
601 + for (i = 0; i < ELEMCOUNT(chunkmap); i++) \
602 + destmap[i] = srcmap[i]; \
605 +#define SCTP_CHUNKMAP_IS_CLEAR(chunkmap) \
609 + for (i = 0; i < ELEMCOUNT(chunkmap); i++) { \
610 + if (chunkmap[i]) { \
618 +#define SCTP_CHUNKMAP_IS_ALL_SET(chunkmap) \
622 + for (i = 0; i < ELEMCOUNT(chunkmap); i++) { \
623 + if (chunkmap[i] != ~0) { \
631 +#endif /* _IPT_SCTP_H_ */
633 diff -Nur linux-2.6.6-rc1.org/include/linux/netfilter_ipv4/ipt_time.h linux-2.6.6-rc1/include/linux/netfilter_ipv4/ipt_time.h
634 --- linux-2.6.6-rc1.org/include/linux/netfilter_ipv4/ipt_time.h 1970-01-01 01:00:00.000000000 +0100
635 +++ linux-2.6.6-rc1/include/linux/netfilter_ipv4/ipt_time.h 2004-04-19 10:08:41.000000000 +0200
637 +#ifndef __ipt_time_h_included__
638 +#define __ipt_time_h_included__
641 +struct ipt_time_info {
642 + u_int8_t days_match; /* 1 bit per day. -SMTWTFS */
643 + u_int16_t time_start; /* 0 < time_start < 23*60+59 = 1439 */
644 + u_int16_t time_stop; /* 0:0 < time_stat < 23:59 */
645 + u_int8_t kerneltime; /* ignore skb time (and use kerneltime) or not. */
649 +#endif /* __ipt_time_h_included__ */
650 diff -Nur linux-2.6.6-rc1.org/include/linux/netfilter_ipv4/ipt_u32.h linux-2.6.6-rc1/include/linux/netfilter_ipv4/ipt_u32.h
651 --- linux-2.6.6-rc1.org/include/linux/netfilter_ipv4/ipt_u32.h 1970-01-01 01:00:00.000000000 +0100
652 +++ linux-2.6.6-rc1/include/linux/netfilter_ipv4/ipt_u32.h 2004-04-19 10:08:46.000000000 +0200
656 +#include <linux/netfilter_ipv4/ip_tables.h>
666 +struct ipt_u32_location_element
671 +struct ipt_u32_value_element
676 +/* *** any way to allow for an arbitrary number of elements?
677 + for now I settle for a limit of 10 of each */
678 +#define U32MAXSIZE 10
682 + struct ipt_u32_location_element location[U32MAXSIZE+1];
684 + struct ipt_u32_value_element value[U32MAXSIZE+1];
690 + struct ipt_u32_test tests[U32MAXSIZE+1];
693 +#endif /*_IPT_U32_H*/
694 diff -Nur linux-2.6.6-rc1.org/include/linux/netfilter_ipv6/ip6t_HL.h linux-2.6.6-rc1/include/linux/netfilter_ipv6/ip6t_HL.h
695 --- linux-2.6.6-rc1.org/include/linux/netfilter_ipv6/ip6t_HL.h 1970-01-01 01:00:00.000000000 +0100
696 +++ linux-2.6.6-rc1/include/linux/netfilter_ipv6/ip6t_HL.h 2004-04-19 10:08:26.000000000 +0200
698 +/* Hop Limit modification module for ip6tables
699 + * Maciej Soltysiak <solt@dns.toxicfilms.tv>
700 + * Based on HW's TTL module */
711 +#define IP6T_HL_MAXMODE IP6T_HL_DEC
713 +struct ip6t_HL_info {
715 + u_int8_t hop_limit;
720 diff -Nur linux-2.6.6-rc1.org/include/linux/netfilter_ipv6/ip6t_REJECT.h linux-2.6.6-rc1/include/linux/netfilter_ipv6/ip6t_REJECT.h
721 --- linux-2.6.6-rc1.org/include/linux/netfilter_ipv6/ip6t_REJECT.h 2004-04-15 03:33:49.000000000 +0200
722 +++ linux-2.6.6-rc1/include/linux/netfilter_ipv6/ip6t_REJECT.h 2004-04-19 10:08:28.000000000 +0200
724 #define _IP6T_REJECT_H
726 enum ip6t_reject_with {
727 - IP6T_ICMP_NET_UNREACHABLE,
728 - IP6T_ICMP_HOST_UNREACHABLE,
729 - IP6T_ICMP_PROT_UNREACHABLE,
730 - IP6T_ICMP_PORT_UNREACHABLE,
731 - IP6T_ICMP_ECHOREPLY
732 + IP6T_ICMP6_NO_ROUTE,
733 + IP6T_ICMP6_ADM_PROHIBITED,
734 + IP6T_ICMP6_NOT_NEIGHBOUR,
735 + IP6T_ICMP6_ADDR_UNREACH,
736 + IP6T_ICMP6_PORT_UNREACH,
737 + IP6T_ICMP6_ECHOREPLY,
741 struct ip6t_reject_info {
742 enum ip6t_reject_with with; /* reject type */
745 -#endif /*_IPT_REJECT_H*/
746 +#endif /*_IP6T_REJECT_H*/
747 diff -Nur linux-2.6.6-rc1.org/include/linux/netfilter_ipv6/ip6t_fuzzy.h linux-2.6.6-rc1/include/linux/netfilter_ipv6/ip6t_fuzzy.h
748 --- linux-2.6.6-rc1.org/include/linux/netfilter_ipv6/ip6t_fuzzy.h 1970-01-01 01:00:00.000000000 +0100
749 +++ linux-2.6.6-rc1/include/linux/netfilter_ipv6/ip6t_fuzzy.h 2004-04-19 10:08:31.000000000 +0200
751 +#ifndef _IP6T_FUZZY_H
752 +#define _IP6T_FUZZY_H
754 +#include <linux/param.h>
755 +#include <linux/types.h>
757 +#define MAXFUZZYRATE 10000000
758 +#define MINFUZZYRATE 3
760 +struct ip6t_fuzzy_info {
761 + u_int32_t minimum_rate;
762 + u_int32_t maximum_rate;
763 + u_int32_t packets_total;
764 + u_int32_t bytes_total;
765 + u_int32_t previous_time;
766 + u_int32_t present_time;
767 + u_int32_t mean_rate;
768 + u_int8_t acceptance_rate;
771 +#endif /*_IP6T_FUZZY_H*/
772 diff -Nur linux-2.6.6-rc1.org/include/linux/netfilter_ipv6/ip6t_nth.h linux-2.6.6-rc1/include/linux/netfilter_ipv6/ip6t_nth.h
773 --- linux-2.6.6-rc1.org/include/linux/netfilter_ipv6/ip6t_nth.h 1970-01-01 01:00:00.000000000 +0100
774 +++ linux-2.6.6-rc1/include/linux/netfilter_ipv6/ip6t_nth.h 2004-04-19 10:08:34.000000000 +0200
779 +#include <linux/param.h>
780 +#include <linux/types.h>
782 +#ifndef IP6T_NTH_NUM_COUNTERS
783 +#define IP6T_NTH_NUM_COUNTERS 16
786 +struct ip6t_nth_info {
794 +#endif /*_IP6T_NTH_H*/
795 diff -Nur linux-2.6.6-rc1.org/include/linux/netfilter_ipv6/ip6t_random.h linux-2.6.6-rc1/include/linux/netfilter_ipv6/ip6t_random.h
796 --- linux-2.6.6-rc1.org/include/linux/netfilter_ipv6/ip6t_random.h 1970-01-01 01:00:00.000000000 +0100
797 +++ linux-2.6.6-rc1/include/linux/netfilter_ipv6/ip6t_random.h 2004-04-19 10:08:38.000000000 +0200
799 +#ifndef _IP6T_RAND_H
800 +#define _IP6T_RAND_H
802 +#include <linux/param.h>
803 +#include <linux/types.h>
805 +struct ip6t_rand_info {
809 +#endif /*_IP6T_RAND_H*/
810 diff -Nur linux-2.6.6-rc1.org/include/linux/skbuff.h linux-2.6.6-rc1/include/linux/skbuff.h
811 --- linux-2.6.6-rc1.org/include/linux/skbuff.h 2004-04-15 03:35:04.000000000 +0200
812 +++ linux-2.6.6-rc1/include/linux/skbuff.h 2004-04-19 10:08:24.000000000 +0200
813 @@ -1201,6 +1201,14 @@
815 atomic_inc(&nfct->master->use);
817 +static inline void nf_reset(struct sk_buff *skb)
819 + nf_conntrack_put(skb->nfct);
821 +#ifdef CONFIG_NETFILTER_DEBUG
826 #ifdef CONFIG_BRIDGE_NETFILTER
827 static inline void nf_bridge_put(struct nf_bridge_info *nf_bridge)
828 @@ -1213,9 +1221,10 @@
830 atomic_inc(&nf_bridge->use);
835 +#endif /* CONFIG_BRIDGE_NETFILTER */
836 +#else /* CONFIG_NETFILTER */
837 +static inline void nf_reset(struct sk_buff *skb) {}
838 +#endif /* CONFIG_NETFILTER */
840 #endif /* __KERNEL__ */
841 #endif /* _LINUX_SKBUFF_H */
842 diff -Nur linux-2.6.6-rc1.org/net/ipv4/ip_gre.c linux-2.6.6-rc1/net/ipv4/ip_gre.c
843 --- linux-2.6.6-rc1.org/net/ipv4/ip_gre.c 2004-04-15 03:35:20.000000000 +0200
844 +++ linux-2.6.6-rc1/net/ipv4/ip_gre.c 2004-04-19 10:08:24.000000000 +0200
846 skb->dev = tunnel->dev;
847 dst_release(skb->dst);
849 -#ifdef CONFIG_NETFILTER
850 - nf_conntrack_put(skb->nfct);
852 -#ifdef CONFIG_NETFILTER_DEBUG
857 ipgre_ecn_decapsulate(iph, skb);
859 read_unlock(&ipgre_lock);
864 -#ifdef CONFIG_NETFILTER
865 - nf_conntrack_put(skb->nfct);
867 -#ifdef CONFIG_NETFILTER_DEBUG
875 diff -Nur linux-2.6.6-rc1.org/net/ipv4/ip_input.c linux-2.6.6-rc1/net/ipv4/ip_input.c
876 --- linux-2.6.6-rc1.org/net/ipv4/ip_input.c 2004-04-15 03:33:53.000000000 +0200
877 +++ linux-2.6.6-rc1/net/ipv4/ip_input.c 2004-04-19 10:08:24.000000000 +0200
878 @@ -202,17 +202,13 @@
880 #ifdef CONFIG_NETFILTER_DEBUG
881 nf_debug_ip_local_deliver(skb);
883 #endif /*CONFIG_NETFILTER_DEBUG*/
885 __skb_pull(skb, ihl);
887 -#ifdef CONFIG_NETFILTER
888 /* Free reference early: we don't need it any more, and it may
889 hold ip_conntrack module loaded indefinitely. */
890 - nf_conntrack_put(skb->nfct);
892 -#endif /*CONFIG_NETFILTER*/
895 /* Point into the IP datagram, just past the header. */
896 skb->h.raw = skb->data;
897 diff -Nur linux-2.6.6-rc1.org/net/ipv4/ipip.c linux-2.6.6-rc1/net/ipv4/ipip.c
898 --- linux-2.6.6-rc1.org/net/ipv4/ipip.c 2004-04-15 03:36:03.000000000 +0200
899 +++ linux-2.6.6-rc1/net/ipv4/ipip.c 2004-04-19 10:08:24.000000000 +0200
901 skb->dev = tunnel->dev;
902 dst_release(skb->dst);
904 -#ifdef CONFIG_NETFILTER
905 - nf_conntrack_put(skb->nfct);
907 -#ifdef CONFIG_NETFILTER_DEBUG
912 ipip_ecn_decapsulate(iph, skb);
914 read_unlock(&ipip_lock);
916 if ((iph->ttl = tiph->ttl) == 0)
917 iph->ttl = old_iph->ttl;
919 -#ifdef CONFIG_NETFILTER
920 - nf_conntrack_put(skb->nfct);
922 -#ifdef CONFIG_NETFILTER_DEBUG
930 diff -Nur linux-2.6.6-rc1.org/net/ipv4/netfilter/Kconfig linux-2.6.6-rc1/net/ipv4/netfilter/Kconfig
931 --- linux-2.6.6-rc1.org/net/ipv4/netfilter/Kconfig 2004-04-19 09:59:32.000000000 +0200
932 +++ linux-2.6.6-rc1/net/ipv4/netfilter/Kconfig 2004-04-19 10:08:46.000000000 +0200
934 <file:Documentation/modules.txt>. If unsure, say `N'.
937 +config IP_NF_TARGET_IPV4OPTSSTRIP
938 + tristate 'IPV4OPTSSTRIP target support'
939 + depends on IP_NF_MANGLE
942 +config IP_NF_TARGET_TTL
943 + tristate 'TTL target support'
944 + depends on IP_NF_MANGLE
947 +config IP_NF_MATCH_CONNLIMIT
948 + tristate 'Connections/IP limit match support'
949 + depends on IP_NF_IPTABLES
952 +config IP_NF_MATCH_DSTLIMIT
953 + tristate 'dstlimit match support'
954 + depends on IP_NF_IPTABLES
957 +config IP_NF_MATCH_FUZZY
958 + tristate 'fuzzy match support'
959 + depends on IP_NF_IPTABLES
962 +config IP_NF_MATCH_IPV4OPTIONS
963 + tristate 'IPV4OPTIONS match support'
964 + depends on IP_NF_IPTABLES
967 +config IP_NF_MATCH_MPORT
968 + tristate 'Multiple port with ranges match support'
969 + depends on IP_NF_IPTABLES
972 +config IP_NF_MATCH_NTH
973 + tristate 'Nth match support'
974 + depends on IP_NF_IPTABLES
977 +config IP_NF_MATCH_OSF
978 + tristate 'OSF match support'
979 + depends on IP_NF_IPTABLES
982 +config IP_POOL_STATISTICS
983 + bool 'enable statistics on pool usage'
984 + depends on IP_NF_POOL!=n
987 + tristate 'IP address pool support'
988 + depends on IP_NF_IPTABLES
991 +config IP_NF_MATCH_PSD
992 + tristate 'psd match support'
993 + depends on IP_NF_IPTABLES
996 +config IP_NF_MATCH_QUOTA
997 + tristate 'quota match support'
998 + depends on IP_NF_IPTABLES
1001 +config IP_NF_MATCH_RANDOM
1002 + tristate 'random match support'
1003 + depends on IP_NF_IPTABLES
1006 +config IP_NF_MATCH_REALM
1007 + tristate 'realm match support'
1008 + depends on IP_NF_IPTABLES && NET_CLS_ROUTE
1011 +config IP_NF_MATCH_SCTP
1012 + tristate 'SCTP protocol match support'
1013 + depends on IP_NF_IPTABLES
1016 +config IP_NF_MATCH_TIME
1017 + tristate 'TIME match support'
1018 + depends on IP_NF_IPTABLES
1021 +config IP_NF_MATCH_U32
1022 + tristate 'U32 match support'
1023 + depends on IP_NF_IPTABLES
1028 diff -Nur linux-2.6.6-rc1.org/net/ipv4/netfilter/Makefile linux-2.6.6-rc1/net/ipv4/netfilter/Makefile
1029 --- linux-2.6.6-rc1.org/net/ipv4/netfilter/Makefile 2004-04-19 09:59:32.000000000 +0200
1030 +++ linux-2.6.6-rc1/net/ipv4/netfilter/Makefile 2004-04-19 10:08:46.000000000 +0200
1033 obj-$(CONFIG_IP_NF_MATCH_HELPER) += ipt_helper.o
1034 obj-$(CONFIG_IP_NF_MATCH_LIMIT) += ipt_limit.o
1035 +obj-$(CONFIG_IP_NF_MATCH_SCTP) += ipt_sctp.o
1036 +obj-$(CONFIG_IP_NF_MATCH_QUOTA) += ipt_quota.o
1037 +obj-$(CONFIG_IP_NF_MATCH_DSTLIMIT) += ipt_dstlimit.o
1038 obj-$(CONFIG_IP_NF_MATCH_MARK) += ipt_mark.o
1039 +obj-$(CONFIG_IP_NF_POOL) += ipt_pool.o ipt_POOL.o ip_pool.o
1040 obj-$(CONFIG_IP_NF_MATCH_MAC) += ipt_mac.o
1041 obj-$(CONFIG_IP_NF_MATCH_IPRANGE) += ipt_iprange.o
1043 obj-$(CONFIG_IP_NF_MATCH_PKTTYPE) += ipt_pkttype.o
1044 obj-$(CONFIG_IP_NF_MATCH_MULTIPORT) += ipt_multiport.o
1046 +obj-$(CONFIG_IP_NF_MATCH_MPORT) += ipt_mport.o
1048 obj-$(CONFIG_IP_NF_MATCH_OWNER) += ipt_owner.o
1049 obj-$(CONFIG_IP_NF_MATCH_TOS) += ipt_tos.o
1051 +obj-$(CONFIG_IP_NF_MATCH_TIME) += ipt_time.o
1054 +obj-$(CONFIG_IP_NF_MATCH_RANDOM) += ipt_random.o
1056 +obj-$(CONFIG_IP_NF_MATCH_PSD) += ipt_psd.o
1058 +obj-$(CONFIG_IP_NF_MATCH_OSF) += ipt_osf.o
1061 +obj-$(CONFIG_IP_NF_MATCH_NTH) += ipt_nth.o
1063 +obj-$(CONFIG_IP_NF_MATCH_IPV4OPTIONS) += ipt_ipv4options.o
1066 +obj-$(CONFIG_IP_NF_MATCH_FUZZY) += ipt_fuzzy.o
1068 obj-$(CONFIG_IP_NF_MATCH_RECENT) += ipt_recent.o
1070 obj-$(CONFIG_IP_NF_MATCH_ECN) += ipt_ecn.o
1073 obj-$(CONFIG_IP_NF_MATCH_LENGTH) += ipt_length.o
1075 +obj-$(CONFIG_IP_NF_MATCH_U32) += ipt_u32.o
1078 obj-$(CONFIG_IP_NF_MATCH_TTL) += ipt_ttl.o
1079 obj-$(CONFIG_IP_NF_MATCH_STATE) += ipt_state.o
1080 +obj-$(CONFIG_IP_NF_MATCH_CONNLIMIT) += ipt_connlimit.o
1081 obj-$(CONFIG_IP_NF_MATCH_CONNTRACK) += ipt_conntrack.o
1082 obj-$(CONFIG_IP_NF_MATCH_TCPMSS) += ipt_tcpmss.o
1083 +obj-$(CONFIG_IP_NF_MATCH_REALM) += ipt_realm.o
1085 obj-$(CONFIG_IP_NF_MATCH_PHYSDEV) += ipt_physdev.o
1088 obj-$(CONFIG_IP_NF_TARGET_CLASSIFY) += ipt_CLASSIFY.o
1089 obj-$(CONFIG_IP_NF_NAT_SNMP_BASIC) += ip_nat_snmp_basic.o
1090 obj-$(CONFIG_IP_NF_TARGET_LOG) += ipt_LOG.o
1091 +obj-$(CONFIG_IP_NF_TARGET_TTL) += ipt_TTL.o
1092 +obj-$(CONFIG_IP_NF_TARGET_IPV4OPTSSTRIP) += ipt_IPV4OPTSSTRIP.o
1093 obj-$(CONFIG_IP_NF_TARGET_ULOG) += ipt_ULOG.o
1094 obj-$(CONFIG_IP_NF_TARGET_TCPMSS) += ipt_TCPMSS.o
1095 obj-$(CONFIG_IP_NF_TARGET_NOTRACK) += ipt_NOTRACK.o
1096 diff -Nur linux-2.6.6-rc1.org/net/ipv4/netfilter/ip_conntrack_core.c linux-2.6.6-rc1/net/ipv4/netfilter/ip_conntrack_core.c
1097 --- linux-2.6.6-rc1.org/net/ipv4/netfilter/ip_conntrack_core.c 2004-04-19 09:59:32.000000000 +0200
1098 +++ linux-2.6.6-rc1/net/ipv4/netfilter/ip_conntrack_core.c 2004-04-19 10:08:22.000000000 +0200
1099 @@ -692,42 +692,50 @@
1100 struct ip_conntrack_expect *, tuple);
1101 READ_UNLOCK(&ip_conntrack_expect_tuple_lock);
1103 - /* If master is not in hash table yet (ie. packet hasn't left
1104 - this machine yet), how can other end know about expected?
1105 - Hence these are not the droids you are looking for (if
1106 - master ct never got confirmed, we'd hold a reference to it
1107 - and weird things would happen to future packets). */
1108 - if (expected && !is_confirmed(expected->expectant))
1111 - /* Look up the conntrack helper for master connections only */
1113 - conntrack->helper = ip_ct_find_helper(&repl_tuple);
1115 - /* If the expectation is dying, then this is a loser. */
1117 - && expected->expectant->helper->timeout
1118 - && ! del_timer(&expected->timeout))
1122 - DEBUGP("conntrack: expectation arrives ct=%p exp=%p\n",
1123 - conntrack, expected);
1124 - /* Welcome, Mr. Bond. We've been expecting you... */
1125 - IP_NF_ASSERT(master_ct(conntrack));
1126 - __set_bit(IPS_EXPECTED_BIT, &conntrack->status);
1127 - conntrack->master = expected;
1128 - expected->sibling = conntrack;
1129 - LIST_DELETE(&ip_conntrack_expect_list, expected);
1130 - expected->expectant->expecting--;
1131 - nf_conntrack_get(&master_ct(conntrack)->infos[0]);
1133 - atomic_inc(&ip_conntrack_count);
1134 + /* If master is not in hash table yet (ie. packet hasn't left
1135 + this machine yet), how can other end know about expected?
1136 + Hence these are not the droids you are looking for (if
1137 + master ct never got confirmed, we'd hold a reference to it
1138 + and weird things would happen to future packets). */
1139 + if (!is_confirmed(expected->expectant)) {
1141 + conntrack->helper = ip_ct_find_helper(&repl_tuple);
1145 + /* Expectation is dying... */
1146 + if (expected->expectant->helper->timeout
1147 + && ! del_timer(&expected->timeout)) {
1151 + DEBUGP("conntrack: expectation arrives ct=%p exp=%p\n",
1152 + conntrack, expected);
1153 + /* Welcome, Mr. Bond. We've been expecting you... */
1154 + IP_NF_ASSERT(master_ct(conntrack));
1155 + __set_bit(IPS_EXPECTED_BIT, &conntrack->status);
1156 + conntrack->master = expected;
1157 + expected->sibling = conntrack;
1158 + LIST_DELETE(&ip_conntrack_expect_list, expected);
1159 + expected->expectant->expecting--;
1160 + nf_conntrack_get(&master_ct(conntrack)->infos[0]);
1162 + /* this is a braindead... --pablo */
1163 + atomic_inc(&ip_conntrack_count);
1164 + WRITE_UNLOCK(&ip_conntrack_lock);
1166 + if (expected->expectfn)
1167 + expected->expectfn(conntrack);
1171 + conntrack->helper = ip_ct_find_helper(&repl_tuple);
1173 +end: atomic_inc(&ip_conntrack_count);
1174 WRITE_UNLOCK(&ip_conntrack_lock);
1176 - if (expected && expected->expectfn)
1177 - expected->expectfn(conntrack);
1178 - return &conntrack->tuplehash[IP_CT_DIR_ORIGINAL];
1179 +ret: return &conntrack->tuplehash[IP_CT_DIR_ORIGINAL];
1182 /* On success, returns conntrack ptr, sets skb->nfct and ctinfo */
1183 diff -Nur linux-2.6.6-rc1.org/net/ipv4/netfilter/ip_conntrack_core.c.orig linux-2.6.6-rc1/net/ipv4/netfilter/ip_conntrack_core.c.orig
1184 --- linux-2.6.6-rc1.org/net/ipv4/netfilter/ip_conntrack_core.c.orig 1970-01-01 01:00:00.000000000 +0100
1185 +++ linux-2.6.6-rc1/net/ipv4/netfilter/ip_conntrack_core.c.orig 2004-04-19 10:02:28.000000000 +0200
1187 +/* Connection state tracking for netfilter. This is separated from,
1188 + but required by, the NAT layer; it can also be used by an iptables
1191 +/* (C) 1999-2001 Paul `Rusty' Russell
1192 + * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
1194 + * This program is free software; you can redistribute it and/or modify
1195 + * it under the terms of the GNU General Public License version 2 as
1196 + * published by the Free Software Foundation.
1198 + * 23 Apr 2001: Harald Welte <laforge@gnumonks.org>
1199 + * - new API and handling of conntrack/nat helpers
1200 + * - now capable of multiple expectations for one master
1201 + * 16 Jul 2002: Harald Welte <laforge@gnumonks.org>
1202 + * - add usage/reference counts to ip_conntrack_expect
1203 + * - export ip_conntrack[_expect]_{find_get,put} functions
1206 +#include <linux/config.h>
1207 +#include <linux/types.h>
1208 +#include <linux/icmp.h>
1209 +#include <linux/ip.h>
1210 +#include <linux/netfilter.h>
1211 +#include <linux/netfilter_ipv4.h>
1212 +#include <linux/module.h>
1213 +#include <linux/skbuff.h>
1214 +#include <linux/proc_fs.h>
1215 +#include <linux/vmalloc.h>
1216 +#include <net/checksum.h>
1217 +#include <linux/stddef.h>
1218 +#include <linux/sysctl.h>
1219 +#include <linux/slab.h>
1220 +#include <linux/random.h>
1221 +#include <linux/jhash.h>
1222 +/* For ERR_PTR(). Yeah, I know... --RR */
1223 +#include <linux/fs.h>
1225 +/* This rwlock protects the main hash table, protocol/helper/expected
1226 + registrations, conntrack timers*/
1227 +#define ASSERT_READ_LOCK(x) MUST_BE_READ_LOCKED(&ip_conntrack_lock)
1228 +#define ASSERT_WRITE_LOCK(x) MUST_BE_WRITE_LOCKED(&ip_conntrack_lock)
1230 +#include <linux/netfilter_ipv4/ip_conntrack.h>
1231 +#include <linux/netfilter_ipv4/ip_conntrack_protocol.h>
1232 +#include <linux/netfilter_ipv4/ip_conntrack_helper.h>
1233 +#include <linux/netfilter_ipv4/ip_conntrack_core.h>
1234 +#include <linux/netfilter_ipv4/listhelp.h>
1236 +#define IP_CONNTRACK_VERSION "2.1"
1239 +#define DEBUGP printk
1241 +#define DEBUGP(format, args...)
1244 +DECLARE_RWLOCK(ip_conntrack_lock);
1245 +DECLARE_RWLOCK(ip_conntrack_expect_tuple_lock);
1247 +void (*ip_conntrack_destroyed)(struct ip_conntrack *conntrack) = NULL;
1248 +LIST_HEAD(ip_conntrack_expect_list);
1249 +LIST_HEAD(protocol_list);
1250 +static LIST_HEAD(helpers);
1251 +unsigned int ip_conntrack_htable_size = 0;
1252 +int ip_conntrack_max;
1253 +static atomic_t ip_conntrack_count = ATOMIC_INIT(0);
1254 +struct list_head *ip_conntrack_hash;
1255 +static kmem_cache_t *ip_conntrack_cachep;
1256 +struct ip_conntrack ip_conntrack_untracked;
1258 +extern struct ip_conntrack_protocol ip_conntrack_generic_protocol;
1260 +static inline int proto_cmpfn(const struct ip_conntrack_protocol *curr,
1261 + u_int8_t protocol)
1263 + return protocol == curr->proto;
1266 +struct ip_conntrack_protocol *__ip_ct_find_proto(u_int8_t protocol)
1268 + struct ip_conntrack_protocol *p;
1270 + MUST_BE_READ_LOCKED(&ip_conntrack_lock);
1271 + p = LIST_FIND(&protocol_list, proto_cmpfn,
1272 + struct ip_conntrack_protocol *, protocol);
1274 + p = &ip_conntrack_generic_protocol;
1279 +struct ip_conntrack_protocol *ip_ct_find_proto(u_int8_t protocol)
1281 + struct ip_conntrack_protocol *p;
1283 + READ_LOCK(&ip_conntrack_lock);
1284 + p = __ip_ct_find_proto(protocol);
1285 + READ_UNLOCK(&ip_conntrack_lock);
1290 +ip_conntrack_put(struct ip_conntrack *ct)
1293 + IP_NF_ASSERT(ct->infos[0].master);
1294 + /* nf_conntrack_put wants to go via an info struct, so feed it
1296 + nf_conntrack_put(&ct->infos[0]);
1299 +static int ip_conntrack_hash_rnd_initted;
1300 +static unsigned int ip_conntrack_hash_rnd;
1303 +hash_conntrack(const struct ip_conntrack_tuple *tuple)
1306 + dump_tuple(tuple);
1308 + return (jhash_3words(tuple->src.ip,
1309 + (tuple->dst.ip ^ tuple->dst.protonum),
1310 + (tuple->src.u.all | (tuple->dst.u.all << 16)),
1311 + ip_conntrack_hash_rnd) % ip_conntrack_htable_size);
1315 +get_tuple(const struct iphdr *iph,
1316 + const struct sk_buff *skb,
1317 + unsigned int dataoff,
1318 + struct ip_conntrack_tuple *tuple,
1319 + const struct ip_conntrack_protocol *protocol)
1321 + /* Never happen */
1322 + if (iph->frag_off & htons(IP_OFFSET)) {
1323 + printk("ip_conntrack_core: Frag of proto %u.\n",
1328 + tuple->src.ip = iph->saddr;
1329 + tuple->dst.ip = iph->daddr;
1330 + tuple->dst.protonum = iph->protocol;
1332 + return protocol->pkt_to_tuple(skb, dataoff, tuple);
1336 +invert_tuple(struct ip_conntrack_tuple *inverse,
1337 + const struct ip_conntrack_tuple *orig,
1338 + const struct ip_conntrack_protocol *protocol)
1340 + inverse->src.ip = orig->dst.ip;
1341 + inverse->dst.ip = orig->src.ip;
1342 + inverse->dst.protonum = orig->dst.protonum;
1344 + return protocol->invert_tuple(inverse, orig);
1348 +/* ip_conntrack_expect helper functions */
1350 +/* Compare tuple parts depending on mask. */
1351 +static inline int expect_cmp(const struct ip_conntrack_expect *i,
1352 + const struct ip_conntrack_tuple *tuple)
1354 + MUST_BE_READ_LOCKED(&ip_conntrack_expect_tuple_lock);
1355 + return ip_ct_tuple_mask_cmp(tuple, &i->tuple, &i->mask);
1359 +destroy_expect(struct ip_conntrack_expect *exp)
1361 + DEBUGP("destroy_expect(%p) use=%d\n", exp, atomic_read(&exp->use));
1362 + IP_NF_ASSERT(atomic_read(&exp->use));
1363 + IP_NF_ASSERT(!timer_pending(&exp->timeout));
1369 +inline void ip_conntrack_expect_put(struct ip_conntrack_expect *exp)
1371 + IP_NF_ASSERT(exp);
1373 + if (atomic_dec_and_test(&exp->use)) {
1374 + /* usage count dropped to zero */
1375 + destroy_expect(exp);
1379 +static inline struct ip_conntrack_expect *
1380 +__ip_ct_expect_find(const struct ip_conntrack_tuple *tuple)
1382 + MUST_BE_READ_LOCKED(&ip_conntrack_lock);
1383 + MUST_BE_READ_LOCKED(&ip_conntrack_expect_tuple_lock);
1384 + return LIST_FIND(&ip_conntrack_expect_list, expect_cmp,
1385 + struct ip_conntrack_expect *, tuple);
1388 +/* Find a expectation corresponding to a tuple. */
1389 +struct ip_conntrack_expect *
1390 +ip_conntrack_expect_find_get(const struct ip_conntrack_tuple *tuple)
1392 + struct ip_conntrack_expect *exp;
1394 + READ_LOCK(&ip_conntrack_lock);
1395 + READ_LOCK(&ip_conntrack_expect_tuple_lock);
1396 + exp = __ip_ct_expect_find(tuple);
1398 + atomic_inc(&exp->use);
1399 + READ_UNLOCK(&ip_conntrack_expect_tuple_lock);
1400 + READ_UNLOCK(&ip_conntrack_lock);
1405 +/* remove one specific expectation from all lists and drop refcount,
1406 + * does _NOT_ delete the timer. */
1407 +static void __unexpect_related(struct ip_conntrack_expect *expect)
1409 + DEBUGP("unexpect_related(%p)\n", expect);
1410 + MUST_BE_WRITE_LOCKED(&ip_conntrack_lock);
1412 + /* we're not allowed to unexpect a confirmed expectation! */
1413 + IP_NF_ASSERT(!expect->sibling);
1415 + /* delete from global and local lists */
1416 + list_del(&expect->list);
1417 + list_del(&expect->expected_list);
1419 + /* decrement expect-count of master conntrack */
1420 + if (expect->expectant)
1421 + expect->expectant->expecting--;
1423 + ip_conntrack_expect_put(expect);
1426 +/* remove one specific expecatation from all lists, drop refcount
1427 + * and expire timer.
1428 + * This function can _NOT_ be called for confirmed expects! */
1429 +static void unexpect_related(struct ip_conntrack_expect *expect)
1431 + IP_NF_ASSERT(expect->expectant);
1432 + IP_NF_ASSERT(expect->expectant->helper);
1433 + /* if we are supposed to have a timer, but we can't delete
1434 + * it: race condition. __unexpect_related will
1435 + * be calledd by timeout function */
1436 + if (expect->expectant->helper->timeout
1437 + && !del_timer(&expect->timeout))
1440 + __unexpect_related(expect);
1443 +/* delete all unconfirmed expectations for this conntrack */
1444 +static void remove_expectations(struct ip_conntrack *ct, int drop_refcount)
1446 + struct list_head *exp_entry, *next;
1447 + struct ip_conntrack_expect *exp;
1449 + DEBUGP("remove_expectations(%p)\n", ct);
1451 + list_for_each_safe(exp_entry, next, &ct->sibling_list) {
1452 + exp = list_entry(exp_entry, struct ip_conntrack_expect,
1455 + /* we skip established expectations, as we want to delete
1456 + * the un-established ones only */
1457 + if (exp->sibling) {
1458 + DEBUGP("remove_expectations: skipping established %p of %p\n", exp->sibling, ct);
1459 + if (drop_refcount) {
1460 + /* Indicate that this expectations parent is dead */
1461 + ip_conntrack_put(exp->expectant);
1462 + exp->expectant = NULL;
1467 + IP_NF_ASSERT(list_inlist(&ip_conntrack_expect_list, exp));
1468 + IP_NF_ASSERT(exp->expectant == ct);
1470 + /* delete expectation from global and private lists */
1471 + unexpect_related(exp);
1476 +clean_from_lists(struct ip_conntrack *ct)
1478 + unsigned int ho, hr;
1480 + DEBUGP("clean_from_lists(%p)\n", ct);
1481 + MUST_BE_WRITE_LOCKED(&ip_conntrack_lock);
1483 + ho = hash_conntrack(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
1484 + hr = hash_conntrack(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
1485 + LIST_DELETE(&ip_conntrack_hash[ho], &ct->tuplehash[IP_CT_DIR_ORIGINAL]);
1486 + LIST_DELETE(&ip_conntrack_hash[hr], &ct->tuplehash[IP_CT_DIR_REPLY]);
1488 + /* Destroy all un-established, pending expectations */
1489 + remove_expectations(ct, 1);
1493 +destroy_conntrack(struct nf_conntrack *nfct)
1495 + struct ip_conntrack *ct = (struct ip_conntrack *)nfct, *master = NULL;
1496 + struct ip_conntrack_protocol *proto;
1498 + DEBUGP("destroy_conntrack(%p)\n", ct);
1499 + IP_NF_ASSERT(atomic_read(&nfct->use) == 0);
1500 + IP_NF_ASSERT(!timer_pending(&ct->timeout));
1502 + /* To make sure we don't get any weird locking issues here:
1503 + * destroy_conntrack() MUST NOT be called with a write lock
1504 + * to ip_conntrack_lock!!! -HW */
1505 + proto = ip_ct_find_proto(ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.protonum);
1506 + if (proto && proto->destroy)
1507 + proto->destroy(ct);
1509 + if (ip_conntrack_destroyed)
1510 + ip_conntrack_destroyed(ct);
1512 + WRITE_LOCK(&ip_conntrack_lock);
1513 + /* Delete us from our own list to prevent corruption later */
1514 + list_del(&ct->sibling_list);
1516 + /* Delete our master expectation */
1518 + if (ct->master->expectant) {
1519 + /* can't call __unexpect_related here,
1520 + * since it would screw up expect_list */
1521 + list_del(&ct->master->expected_list);
1522 + master = ct->master->expectant;
1524 + kfree(ct->master);
1526 + WRITE_UNLOCK(&ip_conntrack_lock);
1529 + ip_conntrack_put(master);
1531 + DEBUGP("destroy_conntrack: returning ct=%p to slab\n", ct);
1532 + kmem_cache_free(ip_conntrack_cachep, ct);
1533 + atomic_dec(&ip_conntrack_count);
1536 +static void death_by_timeout(unsigned long ul_conntrack)
1538 + struct ip_conntrack *ct = (void *)ul_conntrack;
1540 + WRITE_LOCK(&ip_conntrack_lock);
1541 + clean_from_lists(ct);
1542 + WRITE_UNLOCK(&ip_conntrack_lock);
1543 + ip_conntrack_put(ct);
1547 +conntrack_tuple_cmp(const struct ip_conntrack_tuple_hash *i,
1548 + const struct ip_conntrack_tuple *tuple,
1549 + const struct ip_conntrack *ignored_conntrack)
1551 + MUST_BE_READ_LOCKED(&ip_conntrack_lock);
1552 + return i->ctrack != ignored_conntrack
1553 + && ip_ct_tuple_equal(tuple, &i->tuple);
1556 +static struct ip_conntrack_tuple_hash *
1557 +__ip_conntrack_find(const struct ip_conntrack_tuple *tuple,
1558 + const struct ip_conntrack *ignored_conntrack)
1560 + struct ip_conntrack_tuple_hash *h;
1561 + unsigned int hash = hash_conntrack(tuple);
1563 + MUST_BE_READ_LOCKED(&ip_conntrack_lock);
1564 + h = LIST_FIND(&ip_conntrack_hash[hash],
1565 + conntrack_tuple_cmp,
1566 + struct ip_conntrack_tuple_hash *,
1567 + tuple, ignored_conntrack);
1571 +/* Find a connection corresponding to a tuple. */
1572 +struct ip_conntrack_tuple_hash *
1573 +ip_conntrack_find_get(const struct ip_conntrack_tuple *tuple,
1574 + const struct ip_conntrack *ignored_conntrack)
1576 + struct ip_conntrack_tuple_hash *h;
1578 + READ_LOCK(&ip_conntrack_lock);
1579 + h = __ip_conntrack_find(tuple, ignored_conntrack);
1581 + atomic_inc(&h->ctrack->ct_general.use);
1582 + READ_UNLOCK(&ip_conntrack_lock);
1587 +static inline struct ip_conntrack *
1588 +__ip_conntrack_get(struct nf_ct_info *nfct, enum ip_conntrack_info *ctinfo)
1590 + struct ip_conntrack *ct
1591 + = (struct ip_conntrack *)nfct->master;
1593 + /* ctinfo is the index of the nfct inside the conntrack */
1594 + *ctinfo = nfct - ct->infos;
1595 + IP_NF_ASSERT(*ctinfo >= 0 && *ctinfo < IP_CT_NUMBER);
1599 +/* Return conntrack and conntrack_info given skb->nfct->master */
1600 +struct ip_conntrack *
1601 +ip_conntrack_get(struct sk_buff *skb, enum ip_conntrack_info *ctinfo)
1604 + return __ip_conntrack_get(skb->nfct, ctinfo);
1608 +/* Confirm a connection given skb->nfct; places it in hash table */
1610 +__ip_conntrack_confirm(struct nf_ct_info *nfct)
1612 + unsigned int hash, repl_hash;
1613 + struct ip_conntrack *ct;
1614 + enum ip_conntrack_info ctinfo;
1616 + ct = __ip_conntrack_get(nfct, &ctinfo);
1618 + /* ipt_REJECT uses ip_conntrack_attach to attach related
1619 + ICMP/TCP RST packets in other direction. Actual packet
1620 + which created connection will be IP_CT_NEW or for an
1621 + expected connection, IP_CT_RELATED. */
1622 + if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
1625 + hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
1626 + repl_hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
1628 + /* We're not in hash table, and we refuse to set up related
1629 + connections for unconfirmed conns. But packet copies and
1630 + REJECT will give spurious warnings here. */
1631 + /* IP_NF_ASSERT(atomic_read(&ct->ct_general.use) == 1); */
1633 + /* No external references means noone else could have
1635 + IP_NF_ASSERT(!is_confirmed(ct));
1636 + DEBUGP("Confirming conntrack %p\n", ct);
1638 + WRITE_LOCK(&ip_conntrack_lock);
1639 + /* See if there's one in the list already, including reverse:
1640 + NAT could have grabbed it without realizing, since we're
1641 + not in the hash. If there is, we lost race. */
1642 + if (!LIST_FIND(&ip_conntrack_hash[hash],
1643 + conntrack_tuple_cmp,
1644 + struct ip_conntrack_tuple_hash *,
1645 + &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple, NULL)
1646 + && !LIST_FIND(&ip_conntrack_hash[repl_hash],
1647 + conntrack_tuple_cmp,
1648 + struct ip_conntrack_tuple_hash *,
1649 + &ct->tuplehash[IP_CT_DIR_REPLY].tuple, NULL)) {
1650 + list_prepend(&ip_conntrack_hash[hash],
1651 + &ct->tuplehash[IP_CT_DIR_ORIGINAL]);
1652 + list_prepend(&ip_conntrack_hash[repl_hash],
1653 + &ct->tuplehash[IP_CT_DIR_REPLY]);
1654 + /* Timer relative to confirmation time, not original
1655 + setting time, otherwise we'd get timer wrap in
1656 + weird delay cases. */
1657 + ct->timeout.expires += jiffies;
1658 + add_timer(&ct->timeout);
1659 + atomic_inc(&ct->ct_general.use);
1660 + set_bit(IPS_CONFIRMED_BIT, &ct->status);
1661 + WRITE_UNLOCK(&ip_conntrack_lock);
1665 + WRITE_UNLOCK(&ip_conntrack_lock);
1669 +/* Returns true if a connection correspondings to the tuple (required
1672 +ip_conntrack_tuple_taken(const struct ip_conntrack_tuple *tuple,
1673 + const struct ip_conntrack *ignored_conntrack)
1675 + struct ip_conntrack_tuple_hash *h;
1677 + READ_LOCK(&ip_conntrack_lock);
1678 + h = __ip_conntrack_find(tuple, ignored_conntrack);
1679 + READ_UNLOCK(&ip_conntrack_lock);
1684 +/* Returns conntrack if it dealt with ICMP, and filled in skb fields */
1685 +struct ip_conntrack *
1686 +icmp_error_track(struct sk_buff *skb,
1687 + enum ip_conntrack_info *ctinfo,
1688 + unsigned int hooknum)
1690 + struct ip_conntrack_tuple innertuple, origtuple;
1692 + struct icmphdr icmp;
1695 + struct ip_conntrack_protocol *innerproto;
1696 + struct ip_conntrack_tuple_hash *h;
1699 + IP_NF_ASSERT(skb->nfct == NULL);
1701 + /* Not enough header? */
1702 + if (skb_copy_bits(skb, skb->nh.iph->ihl*4, &inside, sizeof(inside))!=0)
1705 + if (inside.icmp.type != ICMP_DEST_UNREACH
1706 + && inside.icmp.type != ICMP_SOURCE_QUENCH
1707 + && inside.icmp.type != ICMP_TIME_EXCEEDED
1708 + && inside.icmp.type != ICMP_PARAMETERPROB
1709 + && inside.icmp.type != ICMP_REDIRECT)
1712 + /* Ignore ICMP's containing fragments (shouldn't happen) */
1713 + if (inside.ip.frag_off & htons(IP_OFFSET)) {
1714 + DEBUGP("icmp_error_track: fragment of proto %u\n",
1715 + inside.ip.protocol);
1719 + innerproto = ip_ct_find_proto(inside.ip.protocol);
1720 + dataoff = skb->nh.iph->ihl*4 + sizeof(inside.icmp) + inside.ip.ihl*4;
1721 + /* Are they talking about one of our connections? */
1722 + if (!get_tuple(&inside.ip, skb, dataoff, &origtuple, innerproto)) {
1723 + DEBUGP("icmp_error: ! get_tuple p=%u", inside.ip.protocol);
1727 + /* Ordinarily, we'd expect the inverted tupleproto, but it's
1728 + been preserved inside the ICMP. */
1729 + if (!invert_tuple(&innertuple, &origtuple, innerproto)) {
1730 + DEBUGP("icmp_error_track: Can't invert tuple\n");
1734 + *ctinfo = IP_CT_RELATED;
1736 + h = ip_conntrack_find_get(&innertuple, NULL);
1738 + /* Locally generated ICMPs will match inverted if they
1739 + haven't been SNAT'ed yet */
1740 + /* FIXME: NAT code has to handle half-done double NAT --RR */
1741 + if (hooknum == NF_IP_LOCAL_OUT)
1742 + h = ip_conntrack_find_get(&origtuple, NULL);
1745 + DEBUGP("icmp_error_track: no match\n");
1748 + /* Reverse direction from that found */
1749 + if (DIRECTION(h) != IP_CT_DIR_REPLY)
1750 + *ctinfo += IP_CT_IS_REPLY;
1752 + if (DIRECTION(h) == IP_CT_DIR_REPLY)
1753 + *ctinfo += IP_CT_IS_REPLY;
1756 + /* Update skb to refer to this connection */
1757 + skb->nfct = &h->ctrack->infos[*ctinfo];
1761 +/* There's a small race here where we may free a just-assured
1762 + connection. Too bad: we're in trouble anyway. */
1763 +static inline int unreplied(const struct ip_conntrack_tuple_hash *i)
1765 + return !(test_bit(IPS_ASSURED_BIT, &i->ctrack->status));
1768 +static int early_drop(struct list_head *chain)
1770 + /* Traverse backwards: gives us oldest, which is roughly LRU */
1771 + struct ip_conntrack_tuple_hash *h;
1774 + READ_LOCK(&ip_conntrack_lock);
1775 + h = LIST_FIND_B(chain, unreplied, struct ip_conntrack_tuple_hash *);
1777 + atomic_inc(&h->ctrack->ct_general.use);
1778 + READ_UNLOCK(&ip_conntrack_lock);
1783 + if (del_timer(&h->ctrack->timeout)) {
1784 + death_by_timeout((unsigned long)h->ctrack);
1787 + ip_conntrack_put(h->ctrack);
1791 +static inline int helper_cmp(const struct ip_conntrack_helper *i,
1792 + const struct ip_conntrack_tuple *rtuple)
1794 + return ip_ct_tuple_mask_cmp(rtuple, &i->tuple, &i->mask);
1797 +struct ip_conntrack_helper *ip_ct_find_helper(const struct ip_conntrack_tuple *tuple)
1799 + return LIST_FIND(&helpers, helper_cmp,
1800 + struct ip_conntrack_helper *,
1804 +/* Allocate a new conntrack: we return -ENOMEM if classification
1805 + failed due to stress. Otherwise it really is unclassifiable. */
1806 +static struct ip_conntrack_tuple_hash *
1807 +init_conntrack(const struct ip_conntrack_tuple *tuple,
1808 + struct ip_conntrack_protocol *protocol,
1809 + struct sk_buff *skb)
1811 + struct ip_conntrack *conntrack;
1812 + struct ip_conntrack_tuple repl_tuple;
1814 + struct ip_conntrack_expect *expected;
1816 + static unsigned int drop_next;
1818 + if (!ip_conntrack_hash_rnd_initted) {
1819 + get_random_bytes(&ip_conntrack_hash_rnd, 4);
1820 + ip_conntrack_hash_rnd_initted = 1;
1823 + hash = hash_conntrack(tuple);
1825 + if (ip_conntrack_max &&
1826 + atomic_read(&ip_conntrack_count) >= ip_conntrack_max) {
1827 + /* Try dropping from random chain, or else from the
1828 + chain about to put into (in case they're trying to
1829 + bomb one hash chain). */
1830 + unsigned int next = (drop_next++)%ip_conntrack_htable_size;
1832 + if (!early_drop(&ip_conntrack_hash[next])
1833 + && !early_drop(&ip_conntrack_hash[hash])) {
1834 + if (net_ratelimit())
1835 + printk(KERN_WARNING
1836 + "ip_conntrack: table full, dropping"
1838 + return ERR_PTR(-ENOMEM);
1842 + if (!invert_tuple(&repl_tuple, tuple, protocol)) {
1843 + DEBUGP("Can't invert tuple.\n");
1847 + conntrack = kmem_cache_alloc(ip_conntrack_cachep, GFP_ATOMIC);
1849 + DEBUGP("Can't allocate conntrack.\n");
1850 + return ERR_PTR(-ENOMEM);
1853 + memset(conntrack, 0, sizeof(*conntrack));
1854 + atomic_set(&conntrack->ct_general.use, 1);
1855 + conntrack->ct_general.destroy = destroy_conntrack;
1856 + conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple = *tuple;
1857 + conntrack->tuplehash[IP_CT_DIR_ORIGINAL].ctrack = conntrack;
1858 + conntrack->tuplehash[IP_CT_DIR_REPLY].tuple = repl_tuple;
1859 + conntrack->tuplehash[IP_CT_DIR_REPLY].ctrack = conntrack;
1860 + for (i=0; i < IP_CT_NUMBER; i++)
1861 + conntrack->infos[i].master = &conntrack->ct_general;
1863 + if (!protocol->new(conntrack, skb)) {
1864 + kmem_cache_free(ip_conntrack_cachep, conntrack);
1867 + /* Don't set timer yet: wait for confirmation */
1868 + init_timer(&conntrack->timeout);
1869 + conntrack->timeout.data = (unsigned long)conntrack;
1870 + conntrack->timeout.function = death_by_timeout;
1872 + INIT_LIST_HEAD(&conntrack->sibling_list);
1874 + WRITE_LOCK(&ip_conntrack_lock);
1875 + /* Need finding and deleting of expected ONLY if we win race */
1876 + READ_LOCK(&ip_conntrack_expect_tuple_lock);
1877 + expected = LIST_FIND(&ip_conntrack_expect_list, expect_cmp,
1878 + struct ip_conntrack_expect *, tuple);
1879 + READ_UNLOCK(&ip_conntrack_expect_tuple_lock);
1881 + /* If master is not in hash table yet (ie. packet hasn't left
1882 + this machine yet), how can other end know about expected?
1883 + Hence these are not the droids you are looking for (if
1884 + master ct never got confirmed, we'd hold a reference to it
1885 + and weird things would happen to future packets). */
1886 + if (expected && !is_confirmed(expected->expectant))
1889 + /* Look up the conntrack helper for master connections only */
1891 + conntrack->helper = ip_ct_find_helper(&repl_tuple);
1893 + /* If the expectation is dying, then this is a loser. */
1895 + && expected->expectant->helper->timeout
1896 + && ! del_timer(&expected->timeout))
1900 + DEBUGP("conntrack: expectation arrives ct=%p exp=%p\n",
1901 + conntrack, expected);
1902 + /* Welcome, Mr. Bond. We've been expecting you... */
1903 + IP_NF_ASSERT(master_ct(conntrack));
1904 + __set_bit(IPS_EXPECTED_BIT, &conntrack->status);
1905 + conntrack->master = expected;
1906 + expected->sibling = conntrack;
1907 + LIST_DELETE(&ip_conntrack_expect_list, expected);
1908 + expected->expectant->expecting--;
1909 + nf_conntrack_get(&master_ct(conntrack)->infos[0]);
1911 + atomic_inc(&ip_conntrack_count);
1912 + WRITE_UNLOCK(&ip_conntrack_lock);
1914 + if (expected && expected->expectfn)
1915 + expected->expectfn(conntrack);
1916 + return &conntrack->tuplehash[IP_CT_DIR_ORIGINAL];
1919 +/* On success, returns conntrack ptr, sets skb->nfct and ctinfo */
1920 +static inline struct ip_conntrack *
1921 +resolve_normal_ct(struct sk_buff *skb,
1922 + struct ip_conntrack_protocol *proto,
1924 + unsigned int hooknum,
1925 + enum ip_conntrack_info *ctinfo)
1927 + struct ip_conntrack_tuple tuple;
1928 + struct ip_conntrack_tuple_hash *h;
1930 + IP_NF_ASSERT((skb->nh.iph->frag_off & htons(IP_OFFSET)) == 0);
1932 + if (!get_tuple(skb->nh.iph, skb, skb->nh.iph->ihl*4, &tuple, proto))
1935 + /* look for tuple match */
1936 + h = ip_conntrack_find_get(&tuple, NULL);
1938 + h = init_conntrack(&tuple, proto, skb);
1945 + /* It exists; we have (non-exclusive) reference. */
1946 + if (DIRECTION(h) == IP_CT_DIR_REPLY) {
1947 + *ctinfo = IP_CT_ESTABLISHED + IP_CT_IS_REPLY;
1948 + /* Please set reply bit if this packet OK */
1951 + /* Once we've had two way comms, always ESTABLISHED. */
1952 + if (test_bit(IPS_SEEN_REPLY_BIT, &h->ctrack->status)) {
1953 + DEBUGP("ip_conntrack_in: normal packet for %p\n",
1955 + *ctinfo = IP_CT_ESTABLISHED;
1956 + } else if (test_bit(IPS_EXPECTED_BIT, &h->ctrack->status)) {
1957 + DEBUGP("ip_conntrack_in: related packet for %p\n",
1959 + *ctinfo = IP_CT_RELATED;
1961 + DEBUGP("ip_conntrack_in: new packet for %p\n",
1963 + *ctinfo = IP_CT_NEW;
1967 + skb->nfct = &h->ctrack->infos[*ctinfo];
1971 +/* Netfilter hook itself. */
1972 +unsigned int ip_conntrack_in(unsigned int hooknum,
1973 + struct sk_buff **pskb,
1974 + const struct net_device *in,
1975 + const struct net_device *out,
1976 + int (*okfn)(struct sk_buff *))
1978 + struct ip_conntrack *ct;
1979 + enum ip_conntrack_info ctinfo;
1980 + struct ip_conntrack_protocol *proto;
1984 + /* Never happen */
1985 + if ((*pskb)->nh.iph->frag_off & htons(IP_OFFSET)) {
1986 + if (net_ratelimit()) {
1987 + printk(KERN_ERR "ip_conntrack_in: Frag of proto %u (hook=%u)\n",
1988 + (*pskb)->nh.iph->protocol, hooknum);
1993 + /* FIXME: Do this right please. --RR */
1994 + (*pskb)->nfcache |= NFC_UNKNOWN;
1996 +/* Doesn't cover locally-generated broadcast, so not worth it. */
1998 + /* Ignore broadcast: no `connection'. */
1999 + if ((*pskb)->pkt_type == PACKET_BROADCAST) {
2000 + printk("Broadcast packet!\n");
2002 + } else if (((*pskb)->nh.iph->daddr & htonl(0x000000FF))
2003 + == htonl(0x000000FF)) {
2004 + printk("Should bcast: %u.%u.%u.%u->%u.%u.%u.%u (sk=%p, ptype=%u)\n",
2005 + NIPQUAD((*pskb)->nh.iph->saddr),
2006 + NIPQUAD((*pskb)->nh.iph->daddr),
2007 + (*pskb)->sk, (*pskb)->pkt_type);
2011 + /* Previously seen (loopback or untracked)? Ignore. */
2012 + if ((*pskb)->nfct)
2015 + proto = ip_ct_find_proto((*pskb)->nh.iph->protocol);
2017 + /* It may be an icmp error... */
2018 + if ((*pskb)->nh.iph->protocol == IPPROTO_ICMP
2019 + && icmp_error_track(*pskb, &ctinfo, hooknum))
2022 + if (!(ct = resolve_normal_ct(*pskb, proto,&set_reply,hooknum,&ctinfo)))
2023 + /* Not valid part of a connection */
2027 + /* Too stressed to deal. */
2030 + IP_NF_ASSERT((*pskb)->nfct);
2032 + ret = proto->packet(ct, *pskb, ctinfo);
2035 + nf_conntrack_put((*pskb)->nfct);
2036 + (*pskb)->nfct = NULL;
2040 + if (ret != NF_DROP && ct->helper) {
2041 + ret = ct->helper->help(*pskb, ct, ctinfo);
2044 + nf_conntrack_put((*pskb)->nfct);
2045 + (*pskb)->nfct = NULL;
2050 + set_bit(IPS_SEEN_REPLY_BIT, &ct->status);
2055 +int invert_tuplepr(struct ip_conntrack_tuple *inverse,
2056 + const struct ip_conntrack_tuple *orig)
2058 + return invert_tuple(inverse, orig, ip_ct_find_proto(orig->dst.protonum));
2061 +static inline int resent_expect(const struct ip_conntrack_expect *i,
2062 + const struct ip_conntrack_tuple *tuple,
2063 + const struct ip_conntrack_tuple *mask)
2065 + DEBUGP("resent_expect\n");
2066 + DEBUGP(" tuple: "); DUMP_TUPLE(&i->tuple);
2067 + DEBUGP("ct_tuple: "); DUMP_TUPLE(&i->ct_tuple);
2068 + DEBUGP("test tuple: "); DUMP_TUPLE(tuple);
2069 + return (((i->ct_tuple.dst.protonum == 0 && ip_ct_tuple_equal(&i->tuple, tuple))
2070 + || (i->ct_tuple.dst.protonum && ip_ct_tuple_equal(&i->ct_tuple, tuple)))
2071 + && ip_ct_tuple_equal(&i->mask, mask));
2074 +/* Would two expected things clash? */
2075 +static inline int expect_clash(const struct ip_conntrack_expect *i,
2076 + const struct ip_conntrack_tuple *tuple,
2077 + const struct ip_conntrack_tuple *mask)
2079 + /* Part covered by intersection of masks must be unequal,
2080 + otherwise they clash */
2081 + struct ip_conntrack_tuple intersect_mask
2082 + = { { i->mask.src.ip & mask->src.ip,
2083 + { i->mask.src.u.all & mask->src.u.all } },
2084 + { i->mask.dst.ip & mask->dst.ip,
2085 + { i->mask.dst.u.all & mask->dst.u.all },
2086 + i->mask.dst.protonum & mask->dst.protonum } };
2088 + return ip_ct_tuple_mask_cmp(&i->tuple, tuple, &intersect_mask);
2091 +inline void ip_conntrack_unexpect_related(struct ip_conntrack_expect *expect)
2093 + WRITE_LOCK(&ip_conntrack_lock);
2094 + unexpect_related(expect);
2095 + WRITE_UNLOCK(&ip_conntrack_lock);
2098 +static void expectation_timed_out(unsigned long ul_expect)
2100 + struct ip_conntrack_expect *expect = (void *) ul_expect;
2102 + DEBUGP("expectation %p timed out\n", expect);
2103 + WRITE_LOCK(&ip_conntrack_lock);
2104 + __unexpect_related(expect);
2105 + WRITE_UNLOCK(&ip_conntrack_lock);
2108 +struct ip_conntrack_expect *
2109 +ip_conntrack_expect_alloc()
2111 + struct ip_conntrack_expect *new;
2113 + new = (struct ip_conntrack_expect *)
2114 + kmalloc(sizeof(struct ip_conntrack_expect), GFP_ATOMIC);
2116 + DEBUGP("expect_related: OOM allocating expect\n");
2120 + /* tuple_cmp compares whole union, we have to initialized cleanly */
2121 + memset(new, 0, sizeof(struct ip_conntrack_expect));
2127 +ip_conntrack_expect_insert(struct ip_conntrack_expect *new,
2128 + struct ip_conntrack *related_to)
2130 + DEBUGP("new expectation %p of conntrack %p\n", new, related_to);
2131 + new->expectant = related_to;
2132 + new->sibling = NULL;
2133 + atomic_set(&new->use, 1);
2135 + /* add to expected list for this connection */
2136 + list_add(&new->expected_list, &related_to->sibling_list);
2137 + /* add to global list of expectations */
2139 + list_prepend(&ip_conntrack_expect_list, &new->list);
2140 + /* add and start timer if required */
2141 + if (related_to->helper->timeout) {
2142 + init_timer(&new->timeout);
2143 + new->timeout.data = (unsigned long)new;
2144 + new->timeout.function = expectation_timed_out;
2145 + new->timeout.expires = jiffies +
2146 + related_to->helper->timeout * HZ;
2147 + add_timer(&new->timeout);
2149 + related_to->expecting++;
2152 +/* Add a related connection. */
2153 +int ip_conntrack_expect_related(struct ip_conntrack_expect *expect,
2154 + struct ip_conntrack *related_to)
2156 + struct ip_conntrack_expect *old;
2159 + WRITE_LOCK(&ip_conntrack_lock);
2160 + /* Because of the write lock, no reader can walk the lists,
2161 + * so there is no need to use the tuple lock too */
2163 + DEBUGP("ip_conntrack_expect_related %p\n", related_to);
2164 + DEBUGP("tuple: "); DUMP_TUPLE(&expect->tuple);
2165 + DEBUGP("mask: "); DUMP_TUPLE(&expect->mask);
2167 + old = LIST_FIND(&ip_conntrack_expect_list, resent_expect,
2168 + struct ip_conntrack_expect *, &expect->tuple,
2171 + /* Helper private data may contain offsets but no pointers
2172 + pointing into the payload - otherwise we should have to copy
2173 + the data filled out by the helper over the old one */
2174 + DEBUGP("expect_related: resent packet\n");
2175 + if (related_to->helper->timeout) {
2176 + if (!del_timer(&old->timeout)) {
2177 + /* expectation is dying. Fall through */
2180 + old->timeout.expires = jiffies +
2181 + related_to->helper->timeout * HZ;
2182 + add_timer(&old->timeout);
2186 + WRITE_UNLOCK(&ip_conntrack_lock);
2190 + } else if (related_to->helper->max_expected &&
2191 + related_to->expecting >= related_to->helper->max_expected) {
2192 + struct list_head *cur_item;
2194 + if (!(related_to->helper->flags &
2195 + IP_CT_HELPER_F_REUSE_EXPECT)) {
2196 + WRITE_UNLOCK(&ip_conntrack_lock);
2197 + if (net_ratelimit())
2198 + printk(KERN_WARNING
2199 + "ip_conntrack: max number of expected "
2200 + "connections %i of %s reached for "
2201 + "%u.%u.%u.%u->%u.%u.%u.%u\n",
2202 + related_to->helper->max_expected,
2203 + related_to->helper->name,
2204 + NIPQUAD(related_to->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip),
2205 + NIPQUAD(related_to->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip));
2209 + DEBUGP("ip_conntrack: max number of expected "
2210 + "connections %i of %s reached for "
2211 + "%u.%u.%u.%u->%u.%u.%u.%u, reusing\n",
2212 + related_to->helper->max_expected,
2213 + related_to->helper->name,
2214 + NIPQUAD(related_to->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip),
2215 + NIPQUAD(related_to->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip));
2217 + /* choose the the oldest expectation to evict */
2218 + list_for_each(cur_item, &related_to->sibling_list) {
2219 + struct ip_conntrack_expect *cur;
2221 + cur = list_entry(cur_item,
2222 + struct ip_conntrack_expect,
2224 + if (cur->sibling == NULL) {
2230 + /* (!old) cannot happen, since related_to->expecting is the
2231 + * number of unconfirmed expects */
2232 + IP_NF_ASSERT(old);
2234 + /* newnat14 does not reuse the real allocated memory
2235 + * structures but rather unexpects the old and
2236 + * allocates a new. unexpect_related will decrement
2237 + * related_to->expecting.
2239 + unexpect_related(old);
2241 + } else if (LIST_FIND(&ip_conntrack_expect_list, expect_clash,
2242 + struct ip_conntrack_expect *, &expect->tuple,
2244 + WRITE_UNLOCK(&ip_conntrack_lock);
2245 + DEBUGP("expect_related: busy!\n");
2251 +out: ip_conntrack_expect_insert(expect, related_to);
2253 + WRITE_UNLOCK(&ip_conntrack_lock);
2258 +/* Change tuple in an existing expectation */
2259 +int ip_conntrack_change_expect(struct ip_conntrack_expect *expect,
2260 + struct ip_conntrack_tuple *newtuple)
2264 + MUST_BE_READ_LOCKED(&ip_conntrack_lock);
2265 + WRITE_LOCK(&ip_conntrack_expect_tuple_lock);
2267 + DEBUGP("change_expect:\n");
2268 + DEBUGP("exp tuple: "); DUMP_TUPLE(&expect->tuple);
2269 + DEBUGP("exp mask: "); DUMP_TUPLE(&expect->mask);
2270 + DEBUGP("newtuple: "); DUMP_TUPLE(newtuple);
2271 + if (expect->ct_tuple.dst.protonum == 0) {
2272 + /* Never seen before */
2273 + DEBUGP("change expect: never seen before\n");
2274 + if (!ip_ct_tuple_equal(&expect->tuple, newtuple)
2275 + && LIST_FIND(&ip_conntrack_expect_list, expect_clash,
2276 + struct ip_conntrack_expect *, newtuple, &expect->mask)) {
2277 + /* Force NAT to find an unused tuple */
2280 + memcpy(&expect->ct_tuple, &expect->tuple, sizeof(expect->tuple));
2281 + memcpy(&expect->tuple, newtuple, sizeof(expect->tuple));
2285 + /* Resent packet */
2286 + DEBUGP("change expect: resent packet\n");
2287 + if (ip_ct_tuple_equal(&expect->tuple, newtuple)) {
2290 + /* Force NAT to choose again the same port */
2294 + WRITE_UNLOCK(&ip_conntrack_expect_tuple_lock);
2299 +/* Alter reply tuple (maybe alter helper). If it's already taken,
2300 + return 0 and don't do alteration. */
2301 +int ip_conntrack_alter_reply(struct ip_conntrack *conntrack,
2302 + const struct ip_conntrack_tuple *newreply)
2304 + WRITE_LOCK(&ip_conntrack_lock);
2305 + if (__ip_conntrack_find(newreply, conntrack)) {
2306 + WRITE_UNLOCK(&ip_conntrack_lock);
2309 + /* Should be unconfirmed, so not in hash table yet */
2310 + IP_NF_ASSERT(!is_confirmed(conntrack));
2312 + DEBUGP("Altering reply tuple of %p to ", conntrack);
2313 + DUMP_TUPLE(newreply);
2315 + conntrack->tuplehash[IP_CT_DIR_REPLY].tuple = *newreply;
2316 + if (!conntrack->master)
2317 + conntrack->helper = LIST_FIND(&helpers, helper_cmp,
2318 + struct ip_conntrack_helper *,
2320 + WRITE_UNLOCK(&ip_conntrack_lock);
2325 +int ip_conntrack_helper_register(struct ip_conntrack_helper *me)
2327 + WRITE_LOCK(&ip_conntrack_lock);
2328 + list_prepend(&helpers, me);
2329 + WRITE_UNLOCK(&ip_conntrack_lock);
2334 +static inline int unhelp(struct ip_conntrack_tuple_hash *i,
2335 + const struct ip_conntrack_helper *me)
2337 + if (i->ctrack->helper == me) {
2338 + /* Get rid of any expected. */
2339 + remove_expectations(i->ctrack, 0);
2340 + /* And *then* set helper to NULL */
2341 + i->ctrack->helper = NULL;
2346 +void ip_conntrack_helper_unregister(struct ip_conntrack_helper *me)
2350 + /* Need write lock here, to delete helper. */
2351 + WRITE_LOCK(&ip_conntrack_lock);
2352 + LIST_DELETE(&helpers, me);
2354 + /* Get rid of expecteds, set helpers to NULL. */
2355 + for (i = 0; i < ip_conntrack_htable_size; i++)
2356 + LIST_FIND_W(&ip_conntrack_hash[i], unhelp,
2357 + struct ip_conntrack_tuple_hash *, me);
2358 + WRITE_UNLOCK(&ip_conntrack_lock);
2360 + /* Someone could be still looking at the helper in a bh. */
2361 + synchronize_net();
2364 +/* Refresh conntrack for this many jiffies. */
2365 +void ip_ct_refresh(struct ip_conntrack *ct, unsigned long extra_jiffies)
2367 + IP_NF_ASSERT(ct->timeout.data == (unsigned long)ct);
2369 + /* If not in hash table, timer will not be active yet */
2370 + if (!is_confirmed(ct))
2371 + ct->timeout.expires = extra_jiffies;
2373 + WRITE_LOCK(&ip_conntrack_lock);
2374 + /* Need del_timer for race avoidance (may already be dying). */
2375 + if (del_timer(&ct->timeout)) {
2376 + ct->timeout.expires = jiffies + extra_jiffies;
2377 + add_timer(&ct->timeout);
2379 + WRITE_UNLOCK(&ip_conntrack_lock);
2383 +/* Returns new sk_buff, or NULL */
2385 +ip_ct_gather_frags(struct sk_buff *skb)
2387 + struct sock *sk = skb->sk;
2388 +#ifdef CONFIG_NETFILTER_DEBUG
2389 + unsigned int olddebug = skb->nf_debug;
2396 + local_bh_disable();
2397 + skb = ip_defrag(skb);
2398 + local_bh_enable();
2407 + skb_set_owner_w(skb, sk);
2411 + ip_send_check(skb->nh.iph);
2412 + skb->nfcache |= NFC_ALTERED;
2413 +#ifdef CONFIG_NETFILTER_DEBUG
2414 + /* Packet path as if nothing had happened. */
2415 + skb->nf_debug = olddebug;
2420 +/* Used by ipt_REJECT. */
2421 +static void ip_conntrack_attach(struct sk_buff *nskb, struct nf_ct_info *nfct)
2423 + struct ip_conntrack *ct;
2424 + enum ip_conntrack_info ctinfo;
2426 + ct = __ip_conntrack_get(nfct, &ctinfo);
2428 + /* This ICMP is in reverse direction to the packet which
2430 + if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL)
2431 + ctinfo = IP_CT_RELATED + IP_CT_IS_REPLY;
2433 + ctinfo = IP_CT_RELATED;
2435 + /* Attach new skbuff, and increment count */
2436 + nskb->nfct = &ct->infos[ctinfo];
2437 + atomic_inc(&ct->ct_general.use);
2441 +do_kill(const struct ip_conntrack_tuple_hash *i,
2442 + int (*kill)(const struct ip_conntrack *i, void *data),
2445 + return kill(i->ctrack, data);
2448 +/* Bring out ya dead! */
2449 +static struct ip_conntrack_tuple_hash *
2450 +get_next_corpse(int (*kill)(const struct ip_conntrack *i, void *data),
2451 + void *data, unsigned int *bucket)
2453 + struct ip_conntrack_tuple_hash *h = NULL;
2455 + READ_LOCK(&ip_conntrack_lock);
2456 + for (; !h && *bucket < ip_conntrack_htable_size; (*bucket)++) {
2457 + h = LIST_FIND(&ip_conntrack_hash[*bucket], do_kill,
2458 + struct ip_conntrack_tuple_hash *, kill, data);
2461 + atomic_inc(&h->ctrack->ct_general.use);
2462 + READ_UNLOCK(&ip_conntrack_lock);
2468 +ip_ct_selective_cleanup(int (*kill)(const struct ip_conntrack *i, void *data),
2471 + struct ip_conntrack_tuple_hash *h;
2472 + unsigned int bucket = 0;
2474 + while ((h = get_next_corpse(kill, data, &bucket)) != NULL) {
2475 + /* Time to push up daises... */
2476 + if (del_timer(&h->ctrack->timeout))
2477 + death_by_timeout((unsigned long)h->ctrack);
2478 + /* ... else the timer will get him soon. */
2480 + ip_conntrack_put(h->ctrack);
2484 +/* Fast function for those who don't want to parse /proc (and I don't
2486 +/* Reversing the socket's dst/src point of view gives us the reply
2489 +getorigdst(struct sock *sk, int optval, void *user, int *len)
2491 + struct inet_opt *inet = inet_sk(sk);
2492 + struct ip_conntrack_tuple_hash *h;
2493 + struct ip_conntrack_tuple tuple;
2495 + IP_CT_TUPLE_U_BLANK(&tuple);
2496 + tuple.src.ip = inet->rcv_saddr;
2497 + tuple.src.u.tcp.port = inet->sport;
2498 + tuple.dst.ip = inet->daddr;
2499 + tuple.dst.u.tcp.port = inet->dport;
2500 + tuple.dst.protonum = IPPROTO_TCP;
2502 + /* We only do TCP at the moment: is there a better way? */
2503 + if (strcmp(sk->sk_prot->name, "TCP")) {
2504 + DEBUGP("SO_ORIGINAL_DST: Not a TCP socket\n");
2505 + return -ENOPROTOOPT;
2508 + if ((unsigned int) *len < sizeof(struct sockaddr_in)) {
2509 + DEBUGP("SO_ORIGINAL_DST: len %u not %u\n",
2510 + *len, sizeof(struct sockaddr_in));
2514 + h = ip_conntrack_find_get(&tuple, NULL);
2516 + struct sockaddr_in sin;
2518 + sin.sin_family = AF_INET;
2519 + sin.sin_port = h->ctrack->tuplehash[IP_CT_DIR_ORIGINAL]
2520 + .tuple.dst.u.tcp.port;
2521 + sin.sin_addr.s_addr = h->ctrack->tuplehash[IP_CT_DIR_ORIGINAL]
2524 + DEBUGP("SO_ORIGINAL_DST: %u.%u.%u.%u %u\n",
2525 + NIPQUAD(sin.sin_addr.s_addr), ntohs(sin.sin_port));
2526 + ip_conntrack_put(h->ctrack);
2527 + if (copy_to_user(user, &sin, sizeof(sin)) != 0)
2532 + DEBUGP("SO_ORIGINAL_DST: Can't find %u.%u.%u.%u/%u-%u.%u.%u.%u/%u.\n",
2533 + NIPQUAD(tuple.src.ip), ntohs(tuple.src.u.tcp.port),
2534 + NIPQUAD(tuple.dst.ip), ntohs(tuple.dst.u.tcp.port));
2538 +static struct nf_sockopt_ops so_getorigdst = {
2540 + .get_optmin = SO_ORIGINAL_DST,
2541 + .get_optmax = SO_ORIGINAL_DST+1,
2542 + .get = &getorigdst,
2545 +static int kill_all(const struct ip_conntrack *i, void *data)
2550 +/* Mishearing the voices in his head, our hero wonders how he's
2551 + supposed to kill the mall. */
2552 +void ip_conntrack_cleanup(void)
2554 + ip_ct_attach = NULL;
2555 + /* This makes sure all current packets have passed through
2556 + netfilter framework. Roll on, two-stage module
2558 + synchronize_net();
2560 + i_see_dead_people:
2561 + ip_ct_selective_cleanup(kill_all, NULL);
2562 + if (atomic_read(&ip_conntrack_count) != 0) {
2564 + goto i_see_dead_people;
2567 + kmem_cache_destroy(ip_conntrack_cachep);
2568 + vfree(ip_conntrack_hash);
2569 + nf_unregister_sockopt(&so_getorigdst);
2572 +static int hashsize;
2573 +MODULE_PARM(hashsize, "i");
2575 +int __init ip_conntrack_init(void)
2580 + /* Idea from tcp.c: use 1/16384 of memory. On i386: 32MB
2581 + * machine has 256 buckets. >= 1GB machines have 8192 buckets. */
2583 + ip_conntrack_htable_size = hashsize;
2585 + ip_conntrack_htable_size
2586 + = (((num_physpages << PAGE_SHIFT) / 16384)
2587 + / sizeof(struct list_head));
2588 + if (num_physpages > (1024 * 1024 * 1024 / PAGE_SIZE))
2589 + ip_conntrack_htable_size = 8192;
2590 + if (ip_conntrack_htable_size < 16)
2591 + ip_conntrack_htable_size = 16;
2593 + ip_conntrack_max = 8 * ip_conntrack_htable_size;
2595 + printk("ip_conntrack version %s (%u buckets, %d max)"
2596 + " - %Zd bytes per conntrack\n", IP_CONNTRACK_VERSION,
2597 + ip_conntrack_htable_size, ip_conntrack_max,
2598 + sizeof(struct ip_conntrack));
2600 + ret = nf_register_sockopt(&so_getorigdst);
2602 + printk(KERN_ERR "Unable to register netfilter socket option\n");
2606 + ip_conntrack_hash = vmalloc(sizeof(struct list_head)
2607 + * ip_conntrack_htable_size);
2608 + if (!ip_conntrack_hash) {
2609 + printk(KERN_ERR "Unable to create ip_conntrack_hash\n");
2610 + goto err_unreg_sockopt;
2613 + ip_conntrack_cachep = kmem_cache_create("ip_conntrack",
2614 + sizeof(struct ip_conntrack), 0,
2615 + SLAB_HWCACHE_ALIGN, NULL, NULL);
2616 + if (!ip_conntrack_cachep) {
2617 + printk(KERN_ERR "Unable to create ip_conntrack slab cache\n");
2618 + goto err_free_hash;
2620 + /* Don't NEED lock here, but good form anyway. */
2621 + WRITE_LOCK(&ip_conntrack_lock);
2622 + /* Sew in builtin protocols. */
2623 + list_append(&protocol_list, &ip_conntrack_protocol_tcp);
2624 + list_append(&protocol_list, &ip_conntrack_protocol_udp);
2625 + list_append(&protocol_list, &ip_conntrack_protocol_icmp);
2626 + WRITE_UNLOCK(&ip_conntrack_lock);
2628 + for (i = 0; i < ip_conntrack_htable_size; i++)
2629 + INIT_LIST_HEAD(&ip_conntrack_hash[i]);
2631 + /* For use by ipt_REJECT */
2632 + ip_ct_attach = ip_conntrack_attach;
2634 + /* Set up fake conntrack:
2635 + - to never be deleted, not in any hashes */
2636 + atomic_set(&ip_conntrack_untracked.ct_general.use, 1);
2637 + /* - and look it like as a confirmed connection */
2638 + set_bit(IPS_CONFIRMED_BIT, &ip_conntrack_untracked.status);
2639 + /* - and prepare the ctinfo field for REJECT & NAT. */
2640 + ip_conntrack_untracked.infos[IP_CT_NEW].master =
2641 + ip_conntrack_untracked.infos[IP_CT_RELATED].master =
2642 + ip_conntrack_untracked.infos[IP_CT_RELATED + IP_CT_IS_REPLY].master =
2643 + &ip_conntrack_untracked.ct_general;
2648 + vfree(ip_conntrack_hash);
2650 + nf_unregister_sockopt(&so_getorigdst);
2654 diff -Nur linux-2.6.6-rc1.org/net/ipv4/netfilter/ip_pool.c linux-2.6.6-rc1/net/ipv4/netfilter/ip_pool.c
2655 --- linux-2.6.6-rc1.org/net/ipv4/netfilter/ip_pool.c 1970-01-01 01:00:00.000000000 +0100
2656 +++ linux-2.6.6-rc1/net/ipv4/netfilter/ip_pool.c 2004-04-19 10:08:36.000000000 +0200
2658 +/* Kernel module for IP pool management */
2660 +#include <linux/module.h>
2661 +#include <linux/ip.h>
2662 +#include <linux/skbuff.h>
2663 +#include <linux/netfilter_ipv4/ip_tables.h>
2664 +#include <linux/netfilter_ipv4/ip_pool.h>
2665 +#include <linux/errno.h>
2666 +#include <asm/uaccess.h>
2667 +#include <asm/bitops.h>
2668 +#include <linux/interrupt.h>
2669 +#include <linux/spinlock.h>
2674 +#define DP(format, args...)
2677 +MODULE_LICENSE("GPL");
2680 +static int nr_pool = NR_POOL;/* overwrite this when loading module */
2683 + u_int32_t first_ip; /* host byte order, included in range */
2684 + u_int32_t last_ip; /* host byte order, included in range */
2685 + void *members; /* the bitmap proper */
2686 + int nr_use; /* total nr. of tests through this */
2687 + int nr_match; /* total nr. of matches through this */
2691 +static struct ip_pool *POOL;
2693 +static inline struct ip_pool *lookup(ip_pool_t index)
2695 + if (index < 0 || index >= nr_pool) {
2696 + DP("ip_pool:lookup: bad index %d\n", index);
2699 + return POOL+index;
2702 +int ip_pool_match(ip_pool_t index, u_int32_t addr)
2704 + struct ip_pool *pool = lookup(index);
2707 + if (!pool || !pool->members)
2709 + read_lock_bh(&pool->lock);
2710 + if (pool->members) {
2711 + if (addr >= pool->first_ip && addr <= pool->last_ip) {
2712 + addr -= pool->first_ip;
2713 + if (test_bit(addr, pool->members)) {
2715 +#ifdef CONFIG_IP_POOL_STATISTICS
2720 +#ifdef CONFIG_IP_POOL_STATISTICS
2724 + read_unlock_bh(&pool->lock);
2728 +static int pool_change(ip_pool_t index, u_int32_t addr, int isdel)
2730 + struct ip_pool *pool;
2733 + pool = lookup(index);
2734 + if ( !pool || !pool->members
2735 + || addr < pool->first_ip || addr > pool->last_ip)
2737 + read_lock_bh(&pool->lock);
2738 + if (pool->members && addr >= pool->first_ip && addr <= pool->last_ip) {
2739 + addr -= pool->first_ip;
2741 + ? (0 != test_and_clear_bit(addr, pool->members))
2742 + : (0 != test_and_set_bit(addr, pool->members));
2744 + read_unlock_bh(&pool->lock);
2748 +int ip_pool_mod(ip_pool_t index, u_int32_t addr, int isdel)
2750 + int res = pool_change(index,addr,isdel);
2752 + if (!isdel) res = !res;
2756 +static inline int bitmap_bytes(u_int32_t a, u_int32_t b)
2758 + return 4*((((b-a+8)/8)+3)/4);
2761 +static inline int poolbytes(ip_pool_t index)
2763 + struct ip_pool *pool = lookup(index);
2765 + return pool ? bitmap_bytes(pool->first_ip, pool->last_ip) : 0;
2768 +static int setpool(
2774 + struct ip_pool_request req;
2776 + DP("ip_pool:setpool: optval=%d, user=%p, len=%d\n", optval, user, len);
2777 + if (!capable(CAP_NET_ADMIN))
2779 + if (optval != SO_IP_POOL)
2781 + if (len != sizeof(req))
2783 + if (copy_from_user(&req, user, sizeof(req)) != 0)
2785 + printk("obsolete op - upgrade your ippool(8) utility.\n");
2789 +static int getpool(
2795 + struct ip_pool_request req;
2796 + struct ip_pool *pool;
2802 + DP("ip_pool:getpool: optval=%d, user=%p\n", optval, user);
2803 + if (!capable(CAP_NET_ADMIN))
2805 + if (optval != SO_IP_POOL)
2807 + if (*len != sizeof(req)) {
2810 + if (copy_from_user(&req, user, sizeof(req)) != 0)
2812 + DP("ip_pool:getpool op=%d, index=%d\n", req.op, req.index);
2813 + if (req.op < IP_POOL_BAD001) {
2814 + printk("obsolete op - upgrade your ippool(8) utility.\n");
2818 + case IP_POOL_HIGH_NR:
2819 + DP("ip_pool HIGH_NR\n");
2820 + req.index = IP_POOL_NONE;
2821 + for (i=0; i<nr_pool; i++)
2822 + if (POOL[i].members)
2824 + return copy_to_user(user, &req, sizeof(req));
2825 + case IP_POOL_LOOKUP:
2826 + DP("ip_pool LOOKUP\n");
2827 + pool = lookup(req.index);
2830 + if (!pool->members)
2832 + req.addr = htonl(pool->first_ip);
2833 + req.addr2 = htonl(pool->last_ip);
2834 + return copy_to_user(user, &req, sizeof(req));
2835 + case IP_POOL_USAGE:
2836 + DP("ip_pool USE\n");
2837 + pool = lookup(req.index);
2840 + if (!pool->members)
2842 + req.addr = pool->nr_use;
2843 + req.addr2 = pool->nr_match;
2844 + return copy_to_user(user, &req, sizeof(req));
2845 + case IP_POOL_TEST_ADDR:
2846 + DP("ip_pool TEST 0x%08x\n", req.addr);
2847 + pool = lookup(req.index);
2851 + read_lock_bh(&pool->lock);
2852 + if (!pool->members) {
2853 + DP("ip_pool TEST_ADDR no members in pool\n");
2855 + goto unlock_and_return_res;
2857 + req.addr = ntohl(req.addr);
2858 + if (req.addr < pool->first_ip) {
2859 + DP("ip_pool TEST_ADDR address < pool bounds\n");
2861 + goto unlock_and_return_res;
2863 + if (req.addr > pool->last_ip) {
2864 + DP("ip_pool TEST_ADDR address > pool bounds\n");
2866 + goto unlock_and_return_res;
2868 + req.addr = (0 != test_bit((req.addr - pool->first_ip),
2870 + read_unlock_bh(&pool->lock);
2871 + return copy_to_user(user, &req, sizeof(req));
2872 + case IP_POOL_FLUSH:
2873 + DP("ip_pool FLUSH not yet implemented.\n");
2875 + case IP_POOL_DESTROY:
2876 + DP("ip_pool DESTROY not yet implemented.\n");
2878 + case IP_POOL_INIT:
2879 + DP("ip_pool INIT 0x%08x-0x%08x\n", req.addr, req.addr2);
2880 + pool = lookup(req.index);
2883 + req.addr = ntohl(req.addr);
2884 + req.addr2 = ntohl(req.addr2);
2885 + if (req.addr > req.addr2) {
2886 + DP("ip_pool INIT bad ip range\n");
2889 + newbytes = bitmap_bytes(req.addr, req.addr2);
2890 + newmembers = kmalloc(newbytes, GFP_KERNEL);
2891 + if (!newmembers) {
2892 + DP("ip_pool INIT out of mem for %d bytes\n", newbytes);
2895 + memset(newmembers, 0, newbytes);
2896 + write_lock_bh(&pool->lock);
2897 + if (pool->members) {
2898 + DP("ip_pool INIT pool %d exists\n", req.index);
2899 + kfree(newmembers);
2901 + goto unlock_and_return_res;
2903 + pool->first_ip = req.addr;
2904 + pool->last_ip = req.addr2;
2906 + pool->nr_match = 0;
2907 + pool->members = newmembers;
2908 + write_unlock_bh(&pool->lock);
2910 + case IP_POOL_ADD_ADDR:
2911 + DP("ip_pool ADD_ADDR 0x%08x\n", req.addr);
2912 + req.addr = pool_change(req.index, ntohl(req.addr), 0);
2913 + return copy_to_user(user, &req, sizeof(req));
2914 + case IP_POOL_DEL_ADDR:
2915 + DP("ip_pool DEL_ADDR 0x%08x\n", req.addr);
2916 + req.addr = pool_change(req.index, ntohl(req.addr), 1);
2917 + return copy_to_user(user, &req, sizeof(req));
2919 + DP("ip_pool:getpool bad op %d\n", req.op);
2924 +unlock_and_return_res:
2926 + read_unlock_bh(&pool->lock);
2930 +static struct nf_sockopt_ops so_pool
2931 += { { NULL, NULL }, PF_INET,
2932 + SO_IP_POOL, SO_IP_POOL+1, &setpool,
2933 + SO_IP_POOL, SO_IP_POOL+1, &getpool,
2936 +MODULE_PARM(nr_pool, "i");
2938 +static int __init init(void)
2943 + if (nr_pool < 1) {
2944 + printk("ip_pool module init: bad nr_pool %d\n", nr_pool);
2947 + POOL = kmalloc(nr_pool * sizeof(*POOL), GFP_KERNEL);
2949 + printk("ip_pool module init: out of memory for nr_pool %d\n",
2953 + for (i=0; i<nr_pool; i++) {
2954 + POOL[i].first_ip = 0;
2955 + POOL[i].last_ip = 0;
2956 + POOL[i].members = 0;
2957 + POOL[i].nr_use = 0;
2958 + POOL[i].nr_match = 0;
2959 + POOL[i].lock = RW_LOCK_UNLOCKED;
2961 + res = nf_register_sockopt(&so_pool);
2962 + DP("ip_pool:init %d pools, result %d\n", nr_pool, res);
2970 +static void __exit fini(void)
2974 + DP("ip_pool:fini BYEBYE\n");
2975 + nf_unregister_sockopt(&so_pool);
2976 + for (i=0; i<nr_pool; i++) {
2977 + if (POOL[i].members) {
2978 + kfree(POOL[i].members);
2979 + POOL[i].members = 0;
2984 + DP("ip_pool:fini these are the famous last words\n");
2990 diff -Nur linux-2.6.6-rc1.org/net/ipv4/netfilter/ip_tables.c linux-2.6.6-rc1/net/ipv4/netfilter/ip_tables.c
2991 --- linux-2.6.6-rc1.org/net/ipv4/netfilter/ip_tables.c 2004-04-15 03:34:03.000000000 +0200
2992 +++ linux-2.6.6-rc1/net/ipv4/netfilter/ip_tables.c 2004-04-19 10:08:25.000000000 +0200
2993 @@ -1716,9 +1716,9 @@
2996 #ifdef CONFIG_PROC_FS
2997 -static inline int print_name(const char *i,
2998 - off_t start_offset, char *buffer, int length,
2999 - off_t *pos, unsigned int *count)
3000 +static int print_name(const char *i,
3001 + off_t start_offset, char *buffer, int length,
3002 + off_t *pos, unsigned int *count)
3004 if ((*count)++ >= start_offset) {
3005 unsigned int namelen;
3006 @@ -1752,6 +1752,15 @@
3010 +static inline int print_target(const struct ipt_target *t,
3011 + off_t start_offset, char *buffer, int length,
3012 + off_t *pos, unsigned int *count)
3014 + if (t != &ipt_standard_target && t != &ipt_error_target)
3016 + return print_name((char *)t, start_offset, buffer, length, pos, count);
3019 static int ipt_get_targets(char *buffer, char **start, off_t offset, int length)
3022 @@ -1760,7 +1769,7 @@
3023 if (down_interruptible(&ipt_mutex) != 0)
3026 - LIST_FIND(&ipt_target, print_name, void *,
3027 + LIST_FIND(&ipt_target, print_target, struct ipt_target *,
3028 offset, buffer, length, &pos, &count);
3031 diff -Nur linux-2.6.6-rc1.org/net/ipv4/netfilter/ipt_IPV4OPTSSTRIP.c linux-2.6.6-rc1/net/ipv4/netfilter/ipt_IPV4OPTSSTRIP.c
3032 --- linux-2.6.6-rc1.org/net/ipv4/netfilter/ipt_IPV4OPTSSTRIP.c 1970-01-01 01:00:00.000000000 +0100
3033 +++ linux-2.6.6-rc1/net/ipv4/netfilter/ipt_IPV4OPTSSTRIP.c 2004-04-19 10:08:26.000000000 +0200
3036 + * Strip all IP options in the IP packet header.
3038 + * (C) 2001 by Fabrice MARIE <fabrice@netfilter.org>
3039 + * This software is distributed under GNU GPL v2, 1991
3042 +#include <linux/module.h>
3043 +#include <linux/skbuff.h>
3044 +#include <linux/ip.h>
3045 +#include <net/checksum.h>
3047 +#include <linux/netfilter_ipv4/ip_tables.h>
3049 +MODULE_AUTHOR("Fabrice MARIE <fabrice@netfilter.org>");
3050 +MODULE_DESCRIPTION("Strip all options in IPv4 packets");
3051 +MODULE_LICENSE("GPL");
3053 +static unsigned int
3054 +target(struct sk_buff **pskb,
3055 + const struct net_device *in,
3056 + const struct net_device *out,
3057 + unsigned int hooknum,
3058 + const void *targinfo,
3061 + struct iphdr *iph;
3062 + struct sk_buff *skb;
3063 + struct ip_options *opt;
3064 + unsigned char *optiph;
3067 + if (!skb_ip_make_writable(pskb, (*pskb)->len))
3071 + iph = (*pskb)->nh.iph;
3072 + optiph = skb->nh.raw;
3073 + l = ((struct ip_options *)(&(IPCB(skb)->opt)))->optlen;
3075 + /* if no options in packet then nothing to clear. */
3076 + if (iph->ihl * 4 == sizeof(struct iphdr))
3077 + return IPT_CONTINUE;
3079 + /* else clear all options */
3080 + memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
3081 + memset(optiph+sizeof(struct iphdr), IPOPT_NOOP, l);
3082 + opt = &(IPCB(skb)->opt);
3086 + skb->nfcache |= NFC_ALTERED;
3088 + return IPT_CONTINUE;
3092 +checkentry(const char *tablename,
3093 + const struct ipt_entry *e,
3095 + unsigned int targinfosize,
3096 + unsigned int hook_mask)
3098 + if (strcmp(tablename, "mangle")) {
3099 + printk(KERN_WARNING "IPV4OPTSSTRIP: can only be called from \"mangle\" table, not \"%s\"\n", tablename);
3102 + /* nothing else to check because no parameters */
3106 +static struct ipt_target ipt_ipv4optsstrip_reg = {
3107 + .name = "IPV4OPTSSTRIP",
3109 + .checkentry = checkentry,
3110 + .me = THIS_MODULE };
3112 +static int __init init(void)
3114 + return ipt_register_target(&ipt_ipv4optsstrip_reg);
3117 +static void __exit fini(void)
3119 + ipt_unregister_target(&ipt_ipv4optsstrip_reg);
3124 diff -Nur linux-2.6.6-rc1.org/net/ipv4/netfilter/ipt_POOL.c linux-2.6.6-rc1/net/ipv4/netfilter/ipt_POOL.c
3125 --- linux-2.6.6-rc1.org/net/ipv4/netfilter/ipt_POOL.c 1970-01-01 01:00:00.000000000 +0100
3126 +++ linux-2.6.6-rc1/net/ipv4/netfilter/ipt_POOL.c 2004-04-19 10:08:36.000000000 +0200
3128 +/* ipt_POOL.c - netfilter target to manipulate IP pools
3130 + * This target can be used almost everywhere. It acts on some specified
3131 + * IP pool, adding or deleting some IP address in the pool. The address
3132 + * can be either the source (--addsrc, --delsrc), or destination (--add/deldst)
3133 + * of the packet under inspection.
3135 + * The target normally returns IPT_CONTINUE.
3138 +#include <linux/types.h>
3139 +#include <linux/ip.h>
3140 +#include <linux/timer.h>
3141 +#include <linux/module.h>
3142 +#include <linux/netfilter.h>
3143 +#include <linux/netdevice.h>
3144 +#include <linux/if.h>
3145 +#include <linux/inetdevice.h>
3146 +#include <net/protocol.h>
3147 +#include <net/checksum.h>
3148 +#include <linux/netfilter_ipv4.h>
3149 +#include <linux/netfilter_ipv4/ip_nat_rule.h>
3150 +#include <linux/netfilter_ipv4/ipt_pool.h>
3153 +#define DEBUGP printk
3155 +#define DEBUGP(format, args...)
3158 +/*** NOTE NOTE NOTE NOTE ***
3160 +** By sheer luck, I get away with using the "struct ipt_pool_info", as defined
3161 +** in <linux/netfilter_ipv4/ipt_pool.h>, both as the match and target info.
3162 +** Here, in the target implementation, ipt_pool_info.src, if not IP_POOL_NONE,
3163 +** is modified for the source IP address of the packet under inspection.
3164 +** The same way, the ipt_pool_info.dst pool is modified for the destination.
3166 +** The address is added to the pool normally. However, if IPT_POOL_DEL_dir
3167 +** flag is set in ipt_pool_info.flags, the address is deleted from the pool.
3169 +** If a modification was done to the pool, we possibly return ACCEPT or DROP,
3170 +** if the right IPT_POOL_MOD_dir_ACCEPT or _MOD_dir_DROP flags are set.
3171 +** The IPT_POOL_INV_MOD_dir flag inverts the sense of the check (i.e. the
3172 +** ACCEPT and DROP flags are evaluated when the pool was not modified.)
3176 +do_check(const char *tablename,
3177 + const struct ipt_entry *e,
3179 + unsigned int targinfosize,
3180 + unsigned int hook_mask)
3182 + const struct ipt_pool_info *ipi = targinfo;
3184 + if (targinfosize != IPT_ALIGN(sizeof(*ipi))) {
3185 + DEBUGP("POOL_check: size %u.\n", targinfosize);
3188 + DEBUGP("ipt_POOL:do_check(%d,%d,%d)\n",ipi->src,ipi->dst,ipi->flags);
3192 +static unsigned int
3193 +do_target(struct sk_buff **pskb,
3194 + unsigned int hooknum,
3195 + const struct net_device *in,
3196 + const struct net_device *out,
3197 + const void *targinfo,
3200 + const struct ipt_pool_info *ipi = targinfo;
3202 + unsigned int verdict = IPT_CONTINUE;
3204 + if (ipi->src != IP_POOL_NONE) {
3205 + modified = ip_pool_mod(ipi->src, ntohl((*pskb)->nh.iph->saddr),
3206 + ipi->flags & IPT_POOL_DEL_SRC);
3207 + if (!!modified ^ !!(ipi->flags & IPT_POOL_INV_MOD_SRC)) {
3208 + if (ipi->flags & IPT_POOL_MOD_SRC_ACCEPT)
3209 + verdict = NF_ACCEPT;
3210 + else if (ipi->flags & IPT_POOL_MOD_SRC_DROP)
3211 + verdict = NF_DROP;
3214 + if (verdict == IPT_CONTINUE && ipi->dst != IP_POOL_NONE) {
3215 + modified = ip_pool_mod(ipi->dst, ntohl((*pskb)->nh.iph->daddr),
3216 + ipi->flags & IPT_POOL_DEL_DST);
3217 + if (!!modified ^ !!(ipi->flags & IPT_POOL_INV_MOD_DST)) {
3218 + if (ipi->flags & IPT_POOL_MOD_DST_ACCEPT)
3219 + verdict = NF_ACCEPT;
3220 + else if (ipi->flags & IPT_POOL_MOD_DST_DROP)
3221 + verdict = NF_DROP;
3227 +static struct ipt_target pool_reg
3228 += { { NULL, NULL }, "POOL", do_target, do_check, NULL, THIS_MODULE };
3230 +static int __init init(void)
3232 + DEBUGP("init ipt_POOL\n");
3233 + return ipt_register_target(&pool_reg);
3236 +static void __exit fini(void)
3238 + DEBUGP("fini ipt_POOL\n");
3239 + ipt_unregister_target(&pool_reg);
3244 diff -Nur linux-2.6.6-rc1.org/net/ipv4/netfilter/ipt_TTL.c linux-2.6.6-rc1/net/ipv4/netfilter/ipt_TTL.c
3245 --- linux-2.6.6-rc1.org/net/ipv4/netfilter/ipt_TTL.c 1970-01-01 01:00:00.000000000 +0100
3246 +++ linux-2.6.6-rc1/net/ipv4/netfilter/ipt_TTL.c 2004-04-19 10:08:28.000000000 +0200
3248 +/* TTL modification target for IP tables
3249 + * (C) 2000 by Harald Welte <laforge@gnumonks.org>
3251 + * Version: $Revision$
3253 + * This software is distributed under the terms of GNU GPL
3256 +#include <linux/module.h>
3257 +#include <linux/skbuff.h>
3258 +#include <linux/ip.h>
3259 +#include <net/checksum.h>
3261 +#include <linux/netfilter_ipv4/ip_tables.h>
3262 +#include <linux/netfilter_ipv4/ipt_TTL.h>
3264 +MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
3265 +MODULE_DESCRIPTION("IP tables TTL modification module");
3266 +MODULE_LICENSE("GPL");
3268 +static unsigned int
3269 +ipt_ttl_target(struct sk_buff **pskb, const struct net_device *in,
3270 + const struct net_device *out, unsigned int hooknum,
3271 + const void *targinfo, void *userinfo)
3273 + struct iphdr *iph;
3274 + const struct ipt_TTL_info *info = targinfo;
3275 + u_int16_t diffs[2];
3278 + if (!skb_ip_make_writable(pskb, (*pskb)->len))
3281 + iph = (*pskb)->nh.iph;
3283 + switch (info->mode) {
3285 + new_ttl = info->ttl;
3288 + new_ttl = iph->ttl + info->ttl;
3289 + if (new_ttl > 255)
3293 + new_ttl = iph->ttl + info->ttl;
3298 + new_ttl = iph->ttl;
3302 + if (new_ttl != iph->ttl) {
3303 + diffs[0] = htons(((unsigned)iph->ttl) << 8) ^ 0xFFFF;
3304 + iph->ttl = new_ttl;
3305 + diffs[1] = htons(((unsigned)iph->ttl) << 8);
3306 + iph->check = csum_fold(csum_partial((char *)diffs,
3308 + iph->check^0xFFFF));
3309 + (*pskb)->nfcache |= NFC_ALTERED;
3312 + return IPT_CONTINUE;
3315 +static int ipt_ttl_checkentry(const char *tablename,
3316 + const struct ipt_entry *e,
3318 + unsigned int targinfosize,
3319 + unsigned int hook_mask)
3321 + struct ipt_TTL_info *info = targinfo;
3323 + if (targinfosize != IPT_ALIGN(sizeof(struct ipt_TTL_info))) {
3324 + printk(KERN_WARNING "TTL: targinfosize %u != %Zu\n",
3326 + IPT_ALIGN(sizeof(struct ipt_TTL_info)));
3330 + if (strcmp(tablename, "mangle")) {
3331 + printk(KERN_WARNING "TTL: can only be called from \"mangle\" table, not \"%s\"\n", tablename);
3335 + if (info->mode > IPT_TTL_MAXMODE) {
3336 + printk(KERN_WARNING "TTL: invalid or unknown Mode %u\n",
3341 + if ((info->mode != IPT_TTL_SET) && (info->ttl == 0)) {
3342 + printk(KERN_WARNING "TTL: increment/decrement doesn't make sense with value 0\n");
3349 +static struct ipt_target ipt_TTL = {
3351 + .target = ipt_ttl_target,
3352 + .checkentry = ipt_ttl_checkentry,
3356 +static int __init init(void)
3358 + return ipt_register_target(&ipt_TTL);
3361 +static void __exit fini(void)
3363 + ipt_unregister_target(&ipt_TTL);
3368 diff -Nur linux-2.6.6-rc1.org/net/ipv4/netfilter/ipt_connlimit.c linux-2.6.6-rc1/net/ipv4/netfilter/ipt_connlimit.c
3369 --- linux-2.6.6-rc1.org/net/ipv4/netfilter/ipt_connlimit.c 1970-01-01 01:00:00.000000000 +0100
3370 +++ linux-2.6.6-rc1/net/ipv4/netfilter/ipt_connlimit.c 2004-04-19 10:08:29.000000000 +0200
3373 + * netfilter module to limit the number of parallel tcp
3374 + * connections per IP address.
3375 + * (c) 2000 Gerd Knorr <kraxel@bytesex.org>
3376 + * Nov 2002: Martin Bene <martin.bene@icomedias.com>:
3377 + * only ignore TIME_WAIT or gone connections
3381 + * Kernel module to match connection tracking information.
3382 + * GPL (C) 1999 Rusty Russell (rusty@rustcorp.com.au).
3384 +#include <linux/module.h>
3385 +#include <linux/skbuff.h>
3386 +#include <linux/list.h>
3387 +#include <linux/netfilter_ipv4/ip_conntrack.h>
3388 +#include <linux/netfilter_ipv4/ip_conntrack_core.h>
3389 +#include <linux/netfilter_ipv4/ip_conntrack_tcp.h>
3390 +#include <linux/netfilter_ipv4/ip_tables.h>
3391 +#include <linux/netfilter_ipv4/ipt_connlimit.h>
3395 +MODULE_LICENSE("GPL");
3397 +/* we'll save the tuples of all connections we care about */
3398 +struct ipt_connlimit_conn
3400 + struct list_head list;
3401 + struct ip_conntrack_tuple tuple;
3404 +struct ipt_connlimit_data {
3406 + struct list_head iphash[256];
3409 +static int ipt_iphash(u_int32_t addr)
3413 + hash = addr & 0xff;
3414 + hash ^= (addr >> 8) & 0xff;
3415 + hash ^= (addr >> 16) & 0xff;
3416 + hash ^= (addr >> 24) & 0xff;
3420 +static int count_them(struct ipt_connlimit_data *data,
3421 + u_int32_t addr, u_int32_t mask,
3422 + struct ip_conntrack *ct)
3425 + const static char *tcp[] = { "none", "established", "syn_sent", "syn_recv",
3426 + "fin_wait", "time_wait", "close", "close_wait",
3427 + "last_ack", "listen" };
3429 + int addit = 1, matches = 0;
3430 + struct ip_conntrack_tuple tuple;
3431 + struct ip_conntrack_tuple_hash *found;
3432 + struct ipt_connlimit_conn *conn;
3433 + struct list_head *hash,*lh;
3435 + spin_lock(&data->lock);
3436 + tuple = ct->tuplehash[0].tuple;
3437 + hash = &data->iphash[ipt_iphash(addr & mask)];
3439 + /* check the saved connections */
3440 + for (lh = hash->next; lh != hash; lh = lh->next) {
3441 + conn = list_entry(lh,struct ipt_connlimit_conn,list);
3442 + found = ip_conntrack_find_get(&conn->tuple,ct);
3443 + if (0 == memcmp(&conn->tuple,&tuple,sizeof(tuple)) &&
3445 + found->ctrack->proto.tcp.state != TCP_CONNTRACK_TIME_WAIT) {
3446 + /* Just to be sure we have it only once in the list.
3447 + We should'nt see tuples twice unless someone hooks this
3448 + into a table without "-p tcp --syn" */
3452 + printk("ipt_connlimit [%d]: src=%u.%u.%u.%u:%d dst=%u.%u.%u.%u:%d %s\n",
3453 + ipt_iphash(addr & mask),
3454 + NIPQUAD(conn->tuple.src.ip), ntohs(conn->tuple.src.u.tcp.port),
3455 + NIPQUAD(conn->tuple.dst.ip), ntohs(conn->tuple.dst.u.tcp.port),
3456 + (NULL != found) ? tcp[found->ctrack->proto.tcp.state] : "gone");
3458 + if (NULL == found) {
3459 + /* this one is gone */
3461 + list_del(lh->next);
3465 + if (found->ctrack->proto.tcp.state == TCP_CONNTRACK_TIME_WAIT) {
3466 + /* we don't care about connections which are
3467 + closed already -> ditch it */
3469 + list_del(lh->next);
3471 + nf_conntrack_put(&found->ctrack->infos[0]);
3474 + if ((addr & mask) == (conn->tuple.src.ip & mask)) {
3475 + /* same source IP address -> be counted! */
3478 + nf_conntrack_put(&found->ctrack->infos[0]);
3481 + /* save the new connection in our list */
3483 + printk("ipt_connlimit [%d]: src=%u.%u.%u.%u:%d dst=%u.%u.%u.%u:%d new\n",
3484 + ipt_iphash(addr & mask),
3485 + NIPQUAD(tuple.src.ip), ntohs(tuple.src.u.tcp.port),
3486 + NIPQUAD(tuple.dst.ip), ntohs(tuple.dst.u.tcp.port));
3488 + conn = kmalloc(sizeof(*conn),GFP_ATOMIC);
3491 + memset(conn,0,sizeof(*conn));
3492 + INIT_LIST_HEAD(&conn->list);
3493 + conn->tuple = tuple;
3494 + list_add(&conn->list,hash);
3497 + spin_unlock(&data->lock);
3502 +match(const struct sk_buff *skb,
3503 + const struct net_device *in,
3504 + const struct net_device *out,
3505 + const void *matchinfo,
3509 + const struct ipt_connlimit_info *info = matchinfo;
3510 + int connections, match;
3511 + struct ip_conntrack *ct;
3512 + enum ip_conntrack_info ctinfo;
3514 + ct = ip_conntrack_get((struct sk_buff *)skb, &ctinfo);
3516 + printk("ipt_connlimit: Oops: invalid ct state ?\n");
3520 + connections = count_them(info->data,skb->nh.iph->saddr,info->mask,ct);
3521 + if (-1 == connections) {
3522 + printk("ipt_connlimit: Hmm, kmalloc failed :-(\n");
3523 + *hotdrop = 1; /* let's free some memory :-) */
3526 + match = (info->inverse) ? (connections <= info->limit) : (connections > info->limit);
3528 + printk("ipt_connlimit: src=%u.%u.%u.%u mask=%u.%u.%u.%u "
3529 + "connections=%d limit=%d match=%s\n",
3530 + NIPQUAD(skb->nh.iph->saddr), NIPQUAD(info->mask),
3531 + connections, info->limit, match ? "yes" : "no");
3537 +static int check(const char *tablename,
3538 + const struct ipt_ip *ip,
3540 + unsigned int matchsize,
3541 + unsigned int hook_mask)
3543 + struct ipt_connlimit_info *info = matchinfo;
3547 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_connlimit_info)))
3550 + /* refuse anything but tcp */
3551 + if (ip->proto != IPPROTO_TCP)
3554 + /* init private data */
3555 + info->data = kmalloc(sizeof(struct ipt_connlimit_data),GFP_KERNEL);
3556 + spin_lock_init(&(info->data->lock));
3557 + for (i = 0; i < 256; i++)
3558 + INIT_LIST_HEAD(&(info->data->iphash[i]));
3563 +static void destroy(void *matchinfo, unsigned int matchinfosize)
3565 + struct ipt_connlimit_info *info = matchinfo;
3566 + struct ipt_connlimit_conn *conn;
3567 + struct list_head *hash;
3571 + for (i = 0; i < 256; i++) {
3572 + hash = &(info->data->iphash[i]);
3573 + while (hash != hash->next) {
3574 + conn = list_entry(hash->next,struct ipt_connlimit_conn,list);
3575 + list_del(hash->next);
3579 + kfree(info->data);
3582 +static struct ipt_match connlimit_match = {
3583 + .name = "connlimit",
3585 + .checkentry = &check,
3586 + .destroy = &destroy,
3590 +static int __init init(void)
3592 + return ipt_register_match(&connlimit_match);
3595 +static void __exit fini(void)
3597 + ipt_unregister_match(&connlimit_match);
3602 diff -Nur linux-2.6.6-rc1.org/net/ipv4/netfilter/ipt_dstlimit.c linux-2.6.6-rc1/net/ipv4/netfilter/ipt_dstlimit.c
3603 --- linux-2.6.6-rc1.org/net/ipv4/netfilter/ipt_dstlimit.c 1970-01-01 01:00:00.000000000 +0100
3604 +++ linux-2.6.6-rc1/net/ipv4/netfilter/ipt_dstlimit.c 2004-04-19 10:08:30.000000000 +0200
3606 +/* iptables match extension to limit the number of packets per second
3607 + * seperately for each destination.
3609 + * (C) 2003 by Harald Welte <laforge@netfilter.org>
3613 + * Development of this code was funded by Astaro AG, http://www.astaro.com/
3615 + * based on ipt_limit.c by:
3616 + * Jérôme de Vivie <devivie@info.enserb.u-bordeaux.fr>
3617 + * Hervé Eychenne <eychenne@info.enserb.u-bordeaux.fr>
3618 + * Rusty Russell <rusty@rustcorp.com.au>
3620 + * The general idea is to create a hash table for every dstip and have a
3621 + * seperate limit counter per tuple. This way you can do something like 'limit
3622 + * the number of syn packets for each of my internal addresses.
3624 + * Ideally this would just be implemented as a general 'hash' match, which would
3625 + * allow us to attach any iptables target to it's hash buckets. But this is
3626 + * not possible in the current iptables architecture. As always, pkttables for
3627 + * 2.7.x will help ;)
3629 +#include <linux/module.h>
3630 +#include <linux/skbuff.h>
3631 +#include <linux/spinlock.h>
3632 +#include <linux/random.h>
3633 +#include <linux/jhash.h>
3634 +#include <linux/slab.h>
3635 +#include <linux/vmalloc.h>
3636 +#include <linux/tcp.h>
3637 +#include <linux/udp.h>
3638 +#include <linux/proc_fs.h>
3639 +#include <linux/seq_file.h>
3641 +#define ASSERT_READ_LOCK(x)
3642 +#define ASSERT_WRITE_LOCK(x)
3643 +#include <linux/netfilter_ipv4/lockhelp.h>
3644 +#include <linux/netfilter_ipv4/listhelp.h>
3646 +#include <linux/netfilter_ipv4/ip_tables.h>
3647 +#include <linux/netfilter_ipv4/ipt_dstlimit.h>
3649 +/* FIXME: this is just for IP_NF_ASSERRT */
3650 +#include <linux/netfilter_ipv4/ip_conntrack.h>
3652 +#define MS2JIFFIES(x) ((x*HZ)/1000)
3654 +MODULE_LICENSE("GPL");
3655 +MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
3656 +MODULE_DESCRIPTION("iptables match for limiting per destination");
3658 +/* need to declare this at the top */
3659 +static struct proc_dir_entry *dstlimit_procdir;
3660 +static struct file_operations dl_file_ops;
3662 +/* hash table crap */
3664 +struct dsthash_dst {
3670 +struct dsthash_ent {
3671 + /* static / read-only parts in the beginning */
3672 + struct list_head list;
3673 + struct dsthash_dst dst;
3675 + /* modified structure members in the end */
3676 + unsigned long expires; /* precalculated expiry time */
3678 + unsigned long prev; /* last modification */
3680 + u_int32_t credit_cap, cost;
3684 +struct ipt_dstlimit_htable {
3685 + struct list_head list; /* global list of all htables */
3688 + struct dstlimit_cfg cfg; /* config */
3690 + /* used internally */
3691 + spinlock_t lock; /* lock for list_head */
3692 + u_int32_t rnd; /* random seed for hash */
3693 + struct timer_list timer; /* timer for gc */
3694 + atomic_t count; /* number entries in table */
3696 + /* seq_file stuff */
3697 + struct proc_dir_entry *pde;
3699 + struct list_head hash[0]; /* hashtable itself */
3702 +DECLARE_RWLOCK(dstlimit_lock); /* protects htables list */
3703 +static LIST_HEAD(dstlimit_htables);
3704 +static kmem_cache_t *dstlimit_cachep;
3706 +static inline int dst_cmp(const struct dsthash_ent *ent, struct dsthash_dst *b)
3708 + return (ent->dst.dst_ip == b->dst_ip
3709 + && ent->dst.port == b->port
3710 + && ent->dst.src_ip == b->src_ip);
3713 +static inline u_int32_t
3714 +hash_dst(const struct ipt_dstlimit_htable *ht, const struct dsthash_dst *dst)
3716 + return (jhash_3words(dst->dst_ip, dst->port,
3717 + dst->src_ip, ht->rnd) % ht->cfg.size);
3720 +static inline struct dsthash_ent *
3721 +__dsthash_find(const struct ipt_dstlimit_htable *ht, struct dsthash_dst *dst)
3723 + struct dsthash_ent *ent;
3724 + u_int32_t hash = hash_dst(ht, dst);
3725 + MUST_BE_LOCKED(&ht->lock);
3726 + ent = LIST_FIND(&ht->hash[hash], dst_cmp, struct dsthash_ent *, dst);
3730 +/* allocate dsthash_ent, initialize dst, put in htable and lock it */
3731 +static struct dsthash_ent *
3732 +__dsthash_alloc_init(struct ipt_dstlimit_htable *ht, struct dsthash_dst *dst)
3734 + struct dsthash_ent *ent;
3736 + /* initialize hash with random val at the time we allocate
3737 + * the first hashtable entry */
3739 + get_random_bytes(&ht->rnd, 4);
3741 + if (ht->cfg.max &&
3742 + atomic_read(&ht->count) >= ht->cfg.max) {
3743 + /* FIXME: do something. question is what.. */
3744 + if (net_ratelimit())
3745 + printk(KERN_WARNING
3746 + "ipt_dstlimit: max count of %u reached\n",
3751 + ent = kmem_cache_alloc(dstlimit_cachep, GFP_ATOMIC);
3753 + if (net_ratelimit())
3755 + "ipt_dstlimit: can't allocate dsthash_ent\n");
3759 + atomic_inc(&ht->count);
3761 + ent->dst.dst_ip = dst->dst_ip;
3762 + ent->dst.port = dst->port;
3763 + ent->dst.src_ip = dst->src_ip;
3765 + list_add(&ent->list, &ht->hash[hash_dst(ht, dst)]);
3771 +__dsthash_free(struct ipt_dstlimit_htable *ht, struct dsthash_ent *ent)
3773 + MUST_BE_LOCKED(&ht->lock);
3775 + list_del(&ent->list);
3776 + kmem_cache_free(dstlimit_cachep, ent);
3777 + atomic_dec(&ht->count);
3779 +static void htable_gc(unsigned long htlong);
3781 +static int htable_create(struct ipt_dstlimit_info *minfo)
3784 + unsigned int size;
3785 + struct ipt_dstlimit_htable *hinfo;
3787 + if (minfo->cfg.size)
3788 + size = minfo->cfg.size;
3790 + size = (((num_physpages << PAGE_SHIFT) / 16384)
3791 + / sizeof(struct list_head));
3792 + if (num_physpages > (1024 * 1024 * 1024 / PAGE_SIZE))
3797 + /* FIXME: don't use vmalloc() here or anywhere else -HW */
3798 + hinfo = vmalloc(sizeof(struct ipt_dstlimit_htable)
3799 + + (sizeof(struct list_head) * size));
3801 + printk(KERN_ERR "ipt_dstlimit: Unable to create hashtable\n");
3804 + minfo->hinfo = hinfo;
3806 + /* copy match config into hashtable config */
3807 + memcpy(&hinfo->cfg, &minfo->cfg, sizeof(hinfo->cfg));
3808 + hinfo->cfg.size = size;
3809 + if (!hinfo->cfg.max)
3810 + hinfo->cfg.max = 8 * hinfo->cfg.size;
3811 + else if (hinfo->cfg.max < hinfo->cfg.size)
3812 + hinfo->cfg.max = hinfo->cfg.size;
3814 + for (i = 0; i < hinfo->cfg.size; i++)
3815 + INIT_LIST_HEAD(&hinfo->hash[i]);
3817 + atomic_set(&hinfo->count, 0);
3818 + atomic_set(&hinfo->use, 1);
3820 + hinfo->lock = SPIN_LOCK_UNLOCKED;
3821 + hinfo->pde = create_proc_entry(minfo->name, 0, dstlimit_procdir);
3822 + if (!hinfo->pde) {
3826 + hinfo->pde->proc_fops = &dl_file_ops;
3827 + hinfo->pde->data = hinfo;
3829 + init_timer(&hinfo->timer);
3830 + hinfo->timer.expires = jiffies + MS2JIFFIES(hinfo->cfg.gc_interval);
3831 + hinfo->timer.data = (unsigned long )hinfo;
3832 + hinfo->timer.function = htable_gc;
3833 + add_timer(&hinfo->timer);
3835 + WRITE_LOCK(&dstlimit_lock);
3836 + list_add(&hinfo->list, &dstlimit_htables);
3837 + WRITE_UNLOCK(&dstlimit_lock);
3842 +static int select_all(struct ipt_dstlimit_htable *ht, struct dsthash_ent *he)
3847 +static int select_gc(struct ipt_dstlimit_htable *ht, struct dsthash_ent *he)
3849 + return (jiffies >= he->expires);
3852 +static void htable_selective_cleanup(struct ipt_dstlimit_htable *ht,
3853 + int (*select)(struct ipt_dstlimit_htable *ht,
3854 + struct dsthash_ent *he))
3858 + IP_NF_ASSERT(ht->cfg.size && ht->cfg.max);
3860 + /* lock hash table and iterate over it */
3861 + LOCK_BH(&ht->lock);
3862 + for (i = 0; i < ht->cfg.size; i++) {
3863 + struct dsthash_ent *dh, *n;
3864 + list_for_each_entry_safe(dh, n, &ht->hash[i], list) {
3865 + if ((*select)(ht, dh))
3866 + __dsthash_free(ht, dh);
3869 + UNLOCK_BH(&ht->lock);
3872 +/* hash table garbage collector, run by timer */
3873 +static void htable_gc(unsigned long htlong)
3875 + struct ipt_dstlimit_htable *ht = (struct ipt_dstlimit_htable *)htlong;
3877 + htable_selective_cleanup(ht, select_gc);
3879 + /* re-add the timer accordingly */
3880 + ht->timer.expires = jiffies + MS2JIFFIES(ht->cfg.gc_interval);
3881 + add_timer(&ht->timer);
3884 +static void htable_destroy(struct ipt_dstlimit_htable *hinfo)
3886 + /* remove timer, if it is pending */
3887 + if (timer_pending(&hinfo->timer))
3888 + del_timer(&hinfo->timer);
3890 + /* remove proc entry */
3891 + remove_proc_entry(hinfo->pde->name, dstlimit_procdir);
3893 + htable_selective_cleanup(hinfo, select_all);
3897 +static struct ipt_dstlimit_htable *htable_find_get(char *name)
3899 + struct ipt_dstlimit_htable *hinfo;
3901 + READ_LOCK(&dstlimit_lock);
3902 + list_for_each_entry(hinfo, &dstlimit_htables, list) {
3903 + if (!strcmp(name, hinfo->pde->name)) {
3904 + atomic_inc(&hinfo->use);
3905 + READ_UNLOCK(&dstlimit_lock);
3909 + READ_UNLOCK(&dstlimit_lock);
3914 +static void htable_put(struct ipt_dstlimit_htable *hinfo)
3916 + if (atomic_dec_and_test(&hinfo->use)) {
3917 + WRITE_LOCK(&dstlimit_lock);
3918 + list_del(&hinfo->list);
3919 + WRITE_UNLOCK(&dstlimit_lock);
3920 + htable_destroy(hinfo);
3925 +/* The algorithm used is the Simple Token Bucket Filter (TBF)
3926 + * see net/sched/sch_tbf.c in the linux source tree
3929 +/* Rusty: This is my (non-mathematically-inclined) understanding of
3930 + this algorithm. The `average rate' in jiffies becomes your initial
3931 + amount of credit `credit' and the most credit you can ever have
3932 + `credit_cap'. The `peak rate' becomes the cost of passing the
3935 + `prev' tracks the last packet hit: you gain one credit per jiffy.
3936 + If you get credit balance more than this, the extra credit is
3937 + discarded. Every time the match passes, you lose `cost' credits;
3938 + if you don't have that many, the test fails.
3940 + See Alexey's formal explanation in net/sched/sch_tbf.c.
3942 + To get the maximum range, we multiply by this factor (ie. you get N
3943 + credits per jiffy). We want to allow a rate as low as 1 per day
3944 + (slowest userspace tool allows), which means
3945 + CREDITS_PER_JIFFY*HZ*60*60*24 < 2^32 ie.
3947 +#define MAX_CPJ (0xFFFFFFFF / (HZ*60*60*24))
3949 +/* Repeated shift and or gives us all 1s, final shift and add 1 gives
3950 + * us the power of 2 below the theoretical max, so GCC simply does a
3952 +#define _POW2_BELOW2(x) ((x)|((x)>>1))
3953 +#define _POW2_BELOW4(x) (_POW2_BELOW2(x)|_POW2_BELOW2((x)>>2))
3954 +#define _POW2_BELOW8(x) (_POW2_BELOW4(x)|_POW2_BELOW4((x)>>4))
3955 +#define _POW2_BELOW16(x) (_POW2_BELOW8(x)|_POW2_BELOW8((x)>>8))
3956 +#define _POW2_BELOW32(x) (_POW2_BELOW16(x)|_POW2_BELOW16((x)>>16))
3957 +#define POW2_BELOW32(x) ((_POW2_BELOW32(x)>>1) + 1)
3959 +#define CREDITS_PER_JIFFY POW2_BELOW32(MAX_CPJ)
3961 +/* Precision saver. */
3962 +static inline u_int32_t
3963 +user2credits(u_int32_t user)
3965 + /* If multiplying would overflow... */
3966 + if (user > 0xFFFFFFFF / (HZ*CREDITS_PER_JIFFY))
3967 + /* Divide first. */
3968 + return (user / IPT_DSTLIMIT_SCALE) * HZ * CREDITS_PER_JIFFY;
3970 + return (user * HZ * CREDITS_PER_JIFFY) / IPT_DSTLIMIT_SCALE;
3973 +static inline void rateinfo_recalc(struct dsthash_ent *dh, unsigned long now)
3975 + dh->rateinfo.credit += (now - xchg(&dh->rateinfo.prev, now))
3976 + * CREDITS_PER_JIFFY;
3977 + if (dh->rateinfo.credit > dh->rateinfo.credit_cap)
3978 + dh->rateinfo.credit = dh->rateinfo.credit_cap;
3982 +dstlimit_match(const struct sk_buff *skb,
3983 + const struct net_device *in,
3984 + const struct net_device *out,
3985 + const void *matchinfo,
3989 + struct ipt_dstlimit_info *r =
3990 + ((struct ipt_dstlimit_info *)matchinfo)->u.master;
3991 + struct ipt_dstlimit_htable *hinfo = r->hinfo;
3992 + unsigned long now = jiffies;
3993 + struct dsthash_ent *dh;
3994 + struct dsthash_dst dst;
3996 + memset(&dst, 0, sizeof(dst));
3998 + /* dest ip is always in hash */
3999 + dst.dst_ip = skb->nh.iph->daddr;
4001 + /* source ip only if respective hashmode, otherwise set to
4003 + if (hinfo->cfg.mode & IPT_DSTLIMIT_HASH_SIP)
4004 + dst.src_ip = skb->nh.iph->saddr;
4006 + /* dest port only if respective mode */
4007 + if (hinfo->cfg.mode & IPT_DSTLIMIT_HASH_DPT) {
4010 + /* Must not be a fragment. */
4014 + /* Must be big enough to read ports (both UDP and TCP have
4015 + them at the start). */
4016 + if (skb_copy_bits(skb, skb->nh.iph->ihl*4, ports, sizeof(ports)) < 0) {
4017 + /* We've been asked to examine this packet, and we
4018 + can't. Hence, no choice but to drop. */
4023 + switch (skb->nh.iph->protocol) {
4024 + struct tcphdr *th;
4025 + struct udphdr *uh;
4027 + th = (void *)skb->nh.iph+skb->nh.iph->ihl*4;
4028 + dst.port = th->dest;
4031 + uh = (void *)skb->nh.iph+skb->nh.iph->ihl*4;
4032 + dst.port = uh->dest;
4039 + LOCK_BH(&hinfo->lock);
4040 + dh = __dsthash_find(hinfo, &dst);
4042 + dh = __dsthash_alloc_init(hinfo, &dst);
4045 + /* enomem... don't match == DROP */
4046 + if (net_ratelimit())
4047 + printk(KERN_ERR "%s: ENOMEM\n", __FUNCTION__);
4048 + UNLOCK_BH(&hinfo->lock);
4052 + dh->expires = jiffies + MS2JIFFIES(hinfo->cfg.expire);
4054 + dh->rateinfo.prev = jiffies;
4055 + dh->rateinfo.credit = user2credits(hinfo->cfg.avg *
4056 + hinfo->cfg.burst);
4057 + dh->rateinfo.credit_cap = user2credits(hinfo->cfg.avg *
4058 + hinfo->cfg.burst);
4059 + dh->rateinfo.cost = user2credits(hinfo->cfg.avg);
4061 + UNLOCK_BH(&hinfo->lock);
4065 + /* update expiration timeout */
4066 + dh->expires = now + MS2JIFFIES(hinfo->cfg.expire);
4068 + rateinfo_recalc(dh, now);
4069 + if (dh->rateinfo.credit >= dh->rateinfo.cost) {
4070 + /* We're underlimit. */
4071 + dh->rateinfo.credit -= dh->rateinfo.cost;
4072 + UNLOCK_BH(&hinfo->lock);
4076 + UNLOCK_BH(&hinfo->lock);
4078 + /* default case: we're overlimit, thus don't match */
4083 +dstlimit_checkentry(const char *tablename,
4084 + const struct ipt_ip *ip,
4086 + unsigned int matchsize,
4087 + unsigned int hook_mask)
4089 + struct ipt_dstlimit_info *r = matchinfo;
4091 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_dstlimit_info)))
4094 + /* Check for overflow. */
4095 + if (r->cfg.burst == 0
4096 + || user2credits(r->cfg.avg * r->cfg.burst) <
4097 + user2credits(r->cfg.avg)) {
4098 + printk(KERN_ERR "ipt_dstlimit: Overflow, try lower: %u/%u\n",
4099 + r->cfg.avg, r->cfg.burst);
4103 + if (r->cfg.mode == 0
4104 + || r->cfg.mode > (IPT_DSTLIMIT_HASH_DPT
4105 + |IPT_DSTLIMIT_HASH_DIP
4106 + |IPT_DSTLIMIT_HASH_SIP))
4109 + if (!r->cfg.gc_interval)
4112 + if (!r->cfg.expire)
4115 + r->hinfo = htable_find_get(r->name);
4116 + if (!r->hinfo && (htable_create(r) != 0)) {
4120 + /* Ugly hack: For SMP, we only want to use one set */
4127 +dstlimit_destroy(void *matchinfo, unsigned int matchsize)
4129 + struct ipt_dstlimit_info *r = (struct ipt_dstlimit_info *) matchinfo;
4131 + htable_put(r->hinfo);
4134 +static struct ipt_match ipt_dstlimit = {
4135 + .list = { .prev = NULL, .next = NULL },
4136 + .name = "dstlimit",
4137 + .match = dstlimit_match,
4138 + .checkentry = dstlimit_checkentry,
4139 + .destroy = dstlimit_destroy,
4145 +static void *dl_seq_start(struct seq_file *s, loff_t *pos)
4147 + struct proc_dir_entry *pde = s->private;
4148 + struct ipt_dstlimit_htable *htable = pde->data;
4149 + unsigned int *bucket;
4151 + LOCK_BH(&htable->lock);
4152 + if (*pos >= htable->cfg.size)
4155 + bucket = kmalloc(sizeof(unsigned int), GFP_KERNEL);
4157 + return ERR_PTR(-ENOMEM);
4163 +static void *dl_seq_next(struct seq_file *s, void *v, loff_t *pos)
4165 + struct proc_dir_entry *pde = s->private;
4166 + struct ipt_dstlimit_htable *htable = pde->data;
4167 + unsigned int *bucket = (unsigned int *)v;
4169 + *pos = ++(*bucket);
4170 + if (*pos >= htable->cfg.size) {
4177 +static void dl_seq_stop(struct seq_file *s, void *v)
4179 + struct proc_dir_entry *pde = s->private;
4180 + struct ipt_dstlimit_htable *htable = pde->data;
4181 + unsigned int *bucket = (unsigned int *)v;
4185 + UNLOCK_BH(&htable->lock);
4188 +static inline int dl_seq_real_show(struct dsthash_ent *ent, struct seq_file *s)
4190 + /* recalculate to show accurate numbers */
4191 + rateinfo_recalc(ent, jiffies);
4193 + return seq_printf(s, "%ld %u.%u.%u.%u->%u.%u.%u.%u:%u %u %u %u\n",
4194 + (ent->expires - jiffies)/HZ,
4195 + NIPQUAD(ent->dst.src_ip),
4196 + NIPQUAD(ent->dst.dst_ip), ntohs(ent->dst.port),
4197 + ent->rateinfo.credit, ent->rateinfo.credit_cap,
4198 + ent->rateinfo.cost);
4201 +static int dl_seq_show(struct seq_file *s, void *v)
4203 + struct proc_dir_entry *pde = s->private;
4204 + struct ipt_dstlimit_htable *htable = pde->data;
4205 + unsigned int *bucket = (unsigned int *)v;
4207 + if (LIST_FIND_W(&htable->hash[*bucket], dl_seq_real_show,
4208 + struct dsthash_ent *, s)) {
4209 + /* buffer was filled and unable to print that tuple */
4215 +static struct seq_operations dl_seq_ops = {
4216 + .start = dl_seq_start,
4217 + .next = dl_seq_next,
4218 + .stop = dl_seq_stop,
4219 + .show = dl_seq_show
4222 +static int dl_proc_open(struct inode *inode, struct file *file)
4224 + int ret = seq_open(file, &dl_seq_ops);
4227 + struct seq_file *sf = file->private_data;
4228 + sf->private = PDE(inode);
4233 +static struct file_operations dl_file_ops = {
4234 + .owner = THIS_MODULE,
4235 + .open = dl_proc_open,
4237 + .llseek = seq_lseek,
4238 + .release = seq_release
4241 +static int init_or_fini(int fini)
4248 + if (ipt_register_match(&ipt_dstlimit)) {
4250 + goto cleanup_nothing;
4253 + /* FIXME: do we really want HWCACHE_ALIGN since our objects are
4254 + * quite small ? */
4255 + dstlimit_cachep = kmem_cache_create("ipt_dstlimit",
4256 + sizeof(struct dsthash_ent), 0,
4257 + SLAB_HWCACHE_ALIGN, NULL, NULL);
4258 + if (!dstlimit_cachep) {
4259 + printk(KERN_ERR "Unable to create ipt_dstlimit slab cache\n");
4261 + goto cleanup_unreg_match;
4264 + dstlimit_procdir = proc_mkdir("ipt_dstlimit", proc_net);
4265 + if (!dstlimit_procdir) {
4266 + printk(KERN_ERR "Unable to create proc dir entry\n");
4268 + goto cleanup_free_slab;
4274 + remove_proc_entry("ipt_dstlimit", proc_net);
4276 + kmem_cache_destroy(dstlimit_cachep);
4277 +cleanup_unreg_match:
4278 + ipt_unregister_match(&ipt_dstlimit);
4284 +static int __init init(void)
4286 + return init_or_fini(0);
4289 +static void __exit fini(void)
4296 diff -Nur linux-2.6.6-rc1.org/net/ipv4/netfilter/ipt_fuzzy.c linux-2.6.6-rc1/net/ipv4/netfilter/ipt_fuzzy.c
4297 --- linux-2.6.6-rc1.org/net/ipv4/netfilter/ipt_fuzzy.c 1970-01-01 01:00:00.000000000 +0100
4298 +++ linux-2.6.6-rc1/net/ipv4/netfilter/ipt_fuzzy.c 2004-04-19 10:08:31.000000000 +0200
4301 + * This module implements a simple TSK FLC
4302 + * (Takagi-Sugeno-Kang Fuzzy Logic Controller) that aims
4303 + * to limit , in an adaptive and flexible way , the packet rate crossing
4304 + * a given stream . It serves as an initial and very simple (but effective)
4305 + * example of how Fuzzy Logic techniques can be applied to defeat DoS attacks.
4306 + * As a matter of fact , Fuzzy Logic can help us to insert any "behavior"
4307 + * into our code in a precise , adaptive and efficient manner.
4308 + * The goal is very similar to that of "limit" match , but using techniques of
4309 + * Fuzzy Control , that allow us to shape the transfer functions precisely ,
4310 + * avoiding over and undershoots - and stuff like that .
4313 + * 2002-08-10 Hime Aguiar e Oliveira Jr. <hime@engineer.com> : Initial version.
4314 + * 2002-08-17 : Changed to eliminate floating point operations .
4315 + * 2002-08-23 : Coding style changes .
4318 +#include <linux/module.h>
4319 +#include <linux/skbuff.h>
4320 +#include <linux/ip.h>
4321 +#include <linux/random.h>
4322 +#include <net/tcp.h>
4323 +#include <linux/spinlock.h>
4324 +#include <linux/netfilter_ipv4/ip_tables.h>
4325 +#include <linux/netfilter_ipv4/ipt_fuzzy.h>
4328 + Packet Acceptance Rate - LOW and Packet Acceptance Rate - HIGH
4329 + Expressed in percentage
4332 +#define PAR_LOW 1/100
4335 +static spinlock_t fuzzy_lock = SPIN_LOCK_UNLOCKED ;
4337 +MODULE_AUTHOR("Hime Aguiar e Oliveira Junior <hime@engineer.com>");
4338 +MODULE_DESCRIPTION("IP tables Fuzzy Logic Controller match module");
4339 +MODULE_LICENSE("GPL");
4341 +static u_int8_t mf_high(u_int32_t tx,u_int32_t mini,u_int32_t maxi)
4349 + return ( (100*(tx-mini)) / (maxi-mini) );
4352 +static u_int8_t mf_low(u_int32_t tx,u_int32_t mini,u_int32_t maxi)
4360 + return ( (100*( maxi - tx )) / ( maxi - mini ) );
4364 +ipt_fuzzy_match(const struct sk_buff *pskb,
4365 + const struct net_device *in,
4366 + const struct net_device *out,
4367 + const void *matchinfo,
4371 + /* From userspace */
4373 + struct ipt_fuzzy_info *info = (struct ipt_fuzzy_info *) matchinfo;
4375 + u_int8_t random_number;
4376 + unsigned long amount;
4377 + u_int8_t howhigh, howlow;
4380 + spin_lock_bh(&fuzzy_lock); /* Rise the lock */
4382 + info->bytes_total += pskb->len;
4383 + info->packets_total++;
4385 + info->present_time = jiffies;
4387 + if (info->present_time >= info->previous_time)
4388 + amount = info->present_time - info->previous_time;
4390 + /* There was a transition : I choose to re-sample
4391 + and keep the old acceptance rate...
4395 + info->previous_time = info->present_time;
4396 + info->bytes_total = info->packets_total = 0;
4399 + if (amount > HZ/10) /* More than 100 ms elapsed ... */
4402 + info->mean_rate = (u_int32_t) ((HZ*info->packets_total) \
4405 + info->previous_time = info->present_time;
4406 + info->bytes_total = info->packets_total = 0;
4408 + howhigh = mf_high(info->mean_rate,info->minimum_rate,info->maximum_rate);
4409 + howlow = mf_low(info->mean_rate,info->minimum_rate,info->maximum_rate);
4411 + info->acceptance_rate = (u_int8_t) \
4412 + (howhigh*PAR_LOW + PAR_HIGH*howlow);
4414 + /* In fact , the above defuzzification would require a denominator
4415 + proportional to (howhigh+howlow) but , in this particular case ,
4416 + that expression is constant .
4417 + An imediate consequence is that it isn't necessary to call
4418 + both mf_high and mf_low - but to keep things understandable ,
4423 + spin_unlock_bh(&fuzzy_lock); /* Release the lock */
4426 + if ( info->acceptance_rate < 100 )
4428 + get_random_bytes((void *)(&random_number), 1);
4430 + /* If within the acceptance , it can pass => don't match */
4431 + if (random_number <= (255 * info->acceptance_rate) / 100)
4434 + return 1; /* It can't pass ( It matches ) */
4437 + return 0; /* acceptance_rate == 100 % => Everything passes ... */
4442 +ipt_fuzzy_checkentry(const char *tablename,
4443 + const struct ipt_ip *e,
4445 + unsigned int matchsize,
4446 + unsigned int hook_mask)
4449 + const struct ipt_fuzzy_info *info = matchinfo;
4451 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_fuzzy_info))) {
4452 + printk("ipt_fuzzy: matchsize %u != %u\n", matchsize,
4453 + IPT_ALIGN(sizeof(struct ipt_fuzzy_info)));
4457 + if ((info->minimum_rate < MINFUZZYRATE ) || (info->maximum_rate > MAXFUZZYRATE)
4458 + || (info->minimum_rate >= info->maximum_rate )) {
4459 + printk("ipt_fuzzy: BAD limits , please verify !!!\n");
4466 +static struct ipt_match ipt_fuzzy_reg = {
4468 + .match = ipt_fuzzy_match,
4469 + .checkentry = ipt_fuzzy_checkentry,
4473 +static int __init init(void)
4475 + return ipt_register_match(&ipt_fuzzy_reg);
4478 +static void __exit fini(void)
4480 + ipt_unregister_match(&ipt_fuzzy_reg);
4485 diff -Nur linux-2.6.6-rc1.org/net/ipv4/netfilter/ipt_helper.c linux-2.6.6-rc1/net/ipv4/netfilter/ipt_helper.c
4486 --- linux-2.6.6-rc1.org/net/ipv4/netfilter/ipt_helper.c 2004-04-15 03:35:20.000000000 +0200
4487 +++ linux-2.6.6-rc1/net/ipv4/netfilter/ipt_helper.c 2004-04-19 10:08:23.000000000 +0200
4489 struct ip_conntrack_expect *exp;
4490 struct ip_conntrack *ct;
4491 enum ip_conntrack_info ctinfo;
4493 + int ret = info->invert;
4495 ct = ip_conntrack_get((struct sk_buff *)skb, &ctinfo);
4497 DEBUGP("ipt_helper: Eek! invalid conntrack?\n");
4503 DEBUGP("ipt_helper: conntrack %p has no master\n", ct);
4510 DEBUGP("master's name = %s , info->name = %s\n",
4511 exp->expectant->helper->name, info->name);
4513 - ret = !strncmp(exp->expectant->helper->name, info->name,
4514 - strlen(exp->expectant->helper->name)) ^ info->invert;
4515 + ret ^= !strncmp(exp->expectant->helper->name, info->name,
4516 + strlen(exp->expectant->helper->name));
4518 READ_UNLOCK(&ip_conntrack_lock);
4522 static int __init init(void)
4524 - need_ip_conntrack();
4525 return ipt_register_match(&helper_match);
4528 diff -Nur linux-2.6.6-rc1.org/net/ipv4/netfilter/ipt_ipv4options.c linux-2.6.6-rc1/net/ipv4/netfilter/ipt_ipv4options.c
4529 --- linux-2.6.6-rc1.org/net/ipv4/netfilter/ipt_ipv4options.c 1970-01-01 01:00:00.000000000 +0100
4530 +++ linux-2.6.6-rc1/net/ipv4/netfilter/ipt_ipv4options.c 2004-04-19 10:08:32.000000000 +0200
4533 + This is a module which is used to match ipv4 options.
4534 + This file is distributed under the terms of the GNU General Public
4535 + License (GPL). Copies of the GPL can be obtained from:
4536 + ftp://prep.ai.mit.edu/pub/gnu/GPL
4538 + 11-mars-2001 Fabrice MARIE <fabrice@netfilter.org> : initial development.
4539 + 12-july-2001 Fabrice MARIE <fabrice@netfilter.org> : added router-alert otions matching. Fixed a bug with no-srr
4540 + 12-august-2001 Imran Patel <ipatel@crosswinds.net> : optimization of the match.
4541 + 18-november-2001 Fabrice MARIE <fabrice@netfilter.org> : added [!] 'any' option match.
4542 + 19-february-2004 Harald Welte <laforge@netfilter.org> : merge with 2.6.x
4545 +#include <linux/module.h>
4546 +#include <linux/skbuff.h>
4547 +#include <net/ip.h>
4549 +#include <linux/netfilter_ipv4/ip_tables.h>
4550 +#include <linux/netfilter_ipv4/ipt_ipv4options.h>
4552 +MODULE_LICENSE("GPL");
4553 +MODULE_AUTHOR("Fabrice Marie <fabrice@netfilter.org>");
4556 +match(const struct sk_buff *skb,
4557 + const struct net_device *in,
4558 + const struct net_device *out,
4559 + const void *matchinfo,
4563 + const struct ipt_ipv4options_info *info = matchinfo; /* match info for rule */
4564 + const struct iphdr *iph = skb->nh.iph;
4565 + const struct ip_options *opt;
4567 + if (iph->ihl * 4 == sizeof(struct iphdr)) {
4568 + /* No options, so we match only the "DONTs" and the "IGNOREs" */
4570 + if (((info->options & IPT_IPV4OPTION_MATCH_ANY_OPT) == IPT_IPV4OPTION_MATCH_ANY_OPT) ||
4571 + ((info->options & IPT_IPV4OPTION_MATCH_SSRR) == IPT_IPV4OPTION_MATCH_SSRR) ||
4572 + ((info->options & IPT_IPV4OPTION_MATCH_LSRR) == IPT_IPV4OPTION_MATCH_LSRR) ||
4573 + ((info->options & IPT_IPV4OPTION_MATCH_RR) == IPT_IPV4OPTION_MATCH_RR) ||
4574 + ((info->options & IPT_IPV4OPTION_MATCH_TIMESTAMP) == IPT_IPV4OPTION_MATCH_TIMESTAMP) ||
4575 + ((info->options & IPT_IPV4OPTION_MATCH_ROUTER_ALERT) == IPT_IPV4OPTION_MATCH_ROUTER_ALERT))
4580 + if ((info->options & IPT_IPV4OPTION_MATCH_ANY_OPT) == IPT_IPV4OPTION_MATCH_ANY_OPT)
4581 + /* there are options, and we don't need to care which one */
4584 + if ((info->options & IPT_IPV4OPTION_DONT_MATCH_ANY_OPT) == IPT_IPV4OPTION_DONT_MATCH_ANY_OPT)
4585 + /* there are options but we don't want any ! */
4590 + opt = &(IPCB(skb)->opt);
4592 + /* source routing */
4593 + if ((info->options & IPT_IPV4OPTION_MATCH_SSRR) == IPT_IPV4OPTION_MATCH_SSRR) {
4594 + if (!((opt->srr) & (opt->is_strictroute)))
4597 + else if ((info->options & IPT_IPV4OPTION_MATCH_LSRR) == IPT_IPV4OPTION_MATCH_LSRR) {
4598 + if (!((opt->srr) & (!opt->is_strictroute)))
4601 + else if ((info->options & IPT_IPV4OPTION_DONT_MATCH_SRR) == IPT_IPV4OPTION_DONT_MATCH_SRR) {
4605 + /* record route */
4606 + if ((info->options & IPT_IPV4OPTION_MATCH_RR) == IPT_IPV4OPTION_MATCH_RR) {
4610 + else if ((info->options & IPT_IPV4OPTION_DONT_MATCH_RR) == IPT_IPV4OPTION_DONT_MATCH_RR) {
4615 + if ((info->options & IPT_IPV4OPTION_MATCH_TIMESTAMP) == IPT_IPV4OPTION_MATCH_TIMESTAMP) {
4619 + else if ((info->options & IPT_IPV4OPTION_DONT_MATCH_TIMESTAMP) == IPT_IPV4OPTION_DONT_MATCH_TIMESTAMP) {
4623 + /* router-alert option */
4624 + if ((info->options & IPT_IPV4OPTION_MATCH_ROUTER_ALERT) == IPT_IPV4OPTION_MATCH_ROUTER_ALERT) {
4625 + if (!opt->router_alert)
4628 + else if ((info->options & IPT_IPV4OPTION_DONT_MATCH_ROUTER_ALERT) == IPT_IPV4OPTION_DONT_MATCH_ROUTER_ALERT) {
4629 + if (opt->router_alert)
4638 +checkentry(const char *tablename,
4639 + const struct ipt_ip *ip,
4641 + unsigned int matchsize,
4642 + unsigned int hook_mask)
4644 + const struct ipt_ipv4options_info *info = matchinfo; /* match info for rule */
4645 + /* Check the size */
4646 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_ipv4options_info)))
4648 + /* Now check the coherence of the data ... */
4649 + if (((info->options & IPT_IPV4OPTION_MATCH_ANY_OPT) == IPT_IPV4OPTION_MATCH_ANY_OPT) &&
4650 + (((info->options & IPT_IPV4OPTION_DONT_MATCH_SRR) == IPT_IPV4OPTION_DONT_MATCH_SRR) ||
4651 + ((info->options & IPT_IPV4OPTION_DONT_MATCH_RR) == IPT_IPV4OPTION_DONT_MATCH_RR) ||
4652 + ((info->options & IPT_IPV4OPTION_DONT_MATCH_TIMESTAMP) == IPT_IPV4OPTION_DONT_MATCH_TIMESTAMP) ||
4653 + ((info->options & IPT_IPV4OPTION_DONT_MATCH_ROUTER_ALERT) == IPT_IPV4OPTION_DONT_MATCH_ROUTER_ALERT) ||
4654 + ((info->options & IPT_IPV4OPTION_DONT_MATCH_ANY_OPT) == IPT_IPV4OPTION_DONT_MATCH_ANY_OPT)))
4655 + return 0; /* opposites */
4656 + if (((info->options & IPT_IPV4OPTION_DONT_MATCH_ANY_OPT) == IPT_IPV4OPTION_DONT_MATCH_ANY_OPT) &&
4657 + (((info->options & IPT_IPV4OPTION_MATCH_LSRR) == IPT_IPV4OPTION_MATCH_LSRR) ||
4658 + ((info->options & IPT_IPV4OPTION_MATCH_SSRR) == IPT_IPV4OPTION_MATCH_SSRR) ||
4659 + ((info->options & IPT_IPV4OPTION_MATCH_RR) == IPT_IPV4OPTION_MATCH_RR) ||
4660 + ((info->options & IPT_IPV4OPTION_MATCH_TIMESTAMP) == IPT_IPV4OPTION_MATCH_TIMESTAMP) ||
4661 + ((info->options & IPT_IPV4OPTION_MATCH_ROUTER_ALERT) == IPT_IPV4OPTION_MATCH_ROUTER_ALERT) ||
4662 + ((info->options & IPT_IPV4OPTION_MATCH_ANY_OPT) == IPT_IPV4OPTION_MATCH_ANY_OPT)))
4663 + return 0; /* opposites */
4664 + if (((info->options & IPT_IPV4OPTION_MATCH_SSRR) == IPT_IPV4OPTION_MATCH_SSRR) &&
4665 + ((info->options & IPT_IPV4OPTION_MATCH_LSRR) == IPT_IPV4OPTION_MATCH_LSRR))
4666 + return 0; /* cannot match in the same time loose and strict source routing */
4667 + if ((((info->options & IPT_IPV4OPTION_MATCH_SSRR) == IPT_IPV4OPTION_MATCH_SSRR) ||
4668 + ((info->options & IPT_IPV4OPTION_MATCH_LSRR) == IPT_IPV4OPTION_MATCH_LSRR)) &&
4669 + ((info->options & IPT_IPV4OPTION_DONT_MATCH_SRR) == IPT_IPV4OPTION_DONT_MATCH_SRR))
4670 + return 0; /* opposites */
4671 + if (((info->options & IPT_IPV4OPTION_MATCH_RR) == IPT_IPV4OPTION_MATCH_RR) &&
4672 + ((info->options & IPT_IPV4OPTION_DONT_MATCH_RR) == IPT_IPV4OPTION_DONT_MATCH_RR))
4673 + return 0; /* opposites */
4674 + if (((info->options & IPT_IPV4OPTION_MATCH_TIMESTAMP) == IPT_IPV4OPTION_MATCH_TIMESTAMP) &&
4675 + ((info->options & IPT_IPV4OPTION_DONT_MATCH_TIMESTAMP) == IPT_IPV4OPTION_DONT_MATCH_TIMESTAMP))
4676 + return 0; /* opposites */
4677 + if (((info->options & IPT_IPV4OPTION_MATCH_ROUTER_ALERT) == IPT_IPV4OPTION_MATCH_ROUTER_ALERT) &&
4678 + ((info->options & IPT_IPV4OPTION_DONT_MATCH_ROUTER_ALERT) == IPT_IPV4OPTION_DONT_MATCH_ROUTER_ALERT))
4679 + return 0; /* opposites */
4681 + /* everything looks ok. */
4685 +static struct ipt_match ipv4options_match = {
4686 + .name = "ipv4options",
4688 + .checkentry = checkentry,
4692 +static int __init init(void)
4694 + return ipt_register_match(&ipv4options_match);
4697 +static void __exit fini(void)
4699 + ipt_unregister_match(&ipv4options_match);
4704 diff -Nur linux-2.6.6-rc1.org/net/ipv4/netfilter/ipt_mport.c linux-2.6.6-rc1/net/ipv4/netfilter/ipt_mport.c
4705 --- linux-2.6.6-rc1.org/net/ipv4/netfilter/ipt_mport.c 1970-01-01 01:00:00.000000000 +0100
4706 +++ linux-2.6.6-rc1/net/ipv4/netfilter/ipt_mport.c 2004-04-19 10:08:33.000000000 +0200
4708 +/* Kernel module to match one of a list of TCP/UDP ports: ports are in
4709 + the same place so we can treat them as equal. */
4710 +#include <linux/module.h>
4711 +#include <linux/types.h>
4712 +#include <linux/udp.h>
4713 +#include <linux/skbuff.h>
4715 +#include <linux/netfilter_ipv4/ipt_mport.h>
4716 +#include <linux/netfilter_ipv4/ip_tables.h>
4718 +MODULE_LICENSE("GPL");
4721 +#define duprintf(format, args...) printk(format , ## args)
4723 +#define duprintf(format, args...)
4726 +/* Returns 1 if the port is matched by the test, 0 otherwise. */
4728 +ports_match(const struct ipt_mport *minfo, u_int16_t src, u_int16_t dst)
4732 + u_int16_t pflags = minfo->pflags;
4733 + for (i=0, m=1; i<IPT_MULTI_PORTS; i++, m<<=1) {
4737 + && minfo->ports[i] == 65535)
4740 + s = minfo->ports[i];
4743 + e = minfo->ports[++i];
4748 + if (minfo->flags & IPT_MPORT_SOURCE
4749 + && src >= s && src <= e)
4752 + if (minfo->flags & IPT_MPORT_DESTINATION
4753 + && dst >= s && dst <= e)
4761 +match(const struct sk_buff *skb,
4762 + const struct net_device *in,
4763 + const struct net_device *out,
4764 + const void *matchinfo,
4769 + const struct ipt_mport *minfo = matchinfo;
4774 + /* Must be big enough to read ports (both UDP and TCP have
4775 + them at the start). */
4776 + if (skb_copy_bits(skb, skb->nh.iph->ihl*4, ports, sizeof(ports)) < 0) {
4777 + /* We've been asked to examine this packet, and we
4778 + can't. Hence, no choice but to drop. */
4779 + duprintf("ipt_multiport:"
4780 + " Dropping evil offset=0 tinygram.\n");
4785 + return ports_match(minfo, ntohs(ports[0]), ntohs(ports[1]));
4788 +/* Called when user tries to insert an entry of this type. */
4790 +checkentry(const char *tablename,
4791 + const struct ipt_ip *ip,
4793 + unsigned int matchsize,
4794 + unsigned int hook_mask)
4796 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_mport)))
4799 + /* Must specify proto == TCP/UDP, no unknown flags or bad count */
4800 + return (ip->proto == IPPROTO_TCP || ip->proto == IPPROTO_UDP)
4801 + && !(ip->invflags & IPT_INV_PROTO)
4802 + && matchsize == IPT_ALIGN(sizeof(struct ipt_mport));
4805 +static struct ipt_match mport_match = {
4808 + .checkentry = &checkentry,
4812 +static int __init init(void)
4814 + return ipt_register_match(&mport_match);
4817 +static void __exit fini(void)
4819 + ipt_unregister_match(&mport_match);
4824 diff -Nur linux-2.6.6-rc1.org/net/ipv4/netfilter/ipt_nth.c linux-2.6.6-rc1/net/ipv4/netfilter/ipt_nth.c
4825 --- linux-2.6.6-rc1.org/net/ipv4/netfilter/ipt_nth.c 1970-01-01 01:00:00.000000000 +0100
4826 +++ linux-2.6.6-rc1/net/ipv4/netfilter/ipt_nth.c 2004-04-19 10:08:34.000000000 +0200
4829 + This is a module which is used for match support for every Nth packet
4830 + This file is distributed under the terms of the GNU General Public
4831 + License (GPL). Copies of the GPL can be obtained from:
4832 + ftp://prep.ai.mit.edu/pub/gnu/GPL
4834 + 2001-07-18 Fabrice MARIE <fabrice@netfilter.org> : initial implementation.
4835 + 2001-09-20 Richard Wagner (rwagner@cloudnet.com)
4836 + * added support for multiple counters
4837 + * added support for matching on individual packets
4838 + in the counter cycle
4839 + 2004-02-19 Harald Welte <laforge@netfilter.org>
4844 +#include <linux/module.h>
4845 +#include <linux/skbuff.h>
4846 +#include <linux/ip.h>
4847 +#include <net/tcp.h>
4848 +#include <linux/spinlock.h>
4849 +#include <linux/netfilter_ipv4/ip_tables.h>
4850 +#include <linux/netfilter_ipv4/ipt_nth.h>
4852 +MODULE_LICENSE("GPL");
4853 +MODULE_AUTHOR("Fabrice Marie <fabrice@netfilter.org>");
4856 + * State information.
4863 +static struct state states[IPT_NTH_NUM_COUNTERS];
4866 +ipt_nth_match(const struct sk_buff *pskb,
4867 + const struct net_device *in,
4868 + const struct net_device *out,
4869 + const void *matchinfo,
4873 + /* Parameters from userspace */
4874 + const struct ipt_nth_info *info = matchinfo;
4875 + unsigned counter = info->counter;
4876 + if((counter < 0) || (counter >= IPT_NTH_NUM_COUNTERS))
4878 + printk(KERN_WARNING "nth: invalid counter %u. counter between 0 and %u\n", counter, IPT_NTH_NUM_COUNTERS-1);
4882 + spin_lock(&states[counter].lock);
4884 + /* Are we matching every nth packet?*/
4885 + if (info->packet == 0xFF)
4887 + /* We're matching every nth packet and only every nth packet*/
4888 + /* Do we match or invert match? */
4889 + if (info->not == 0)
4891 + if (states[counter].number == 0)
4893 + ++states[counter].number;
4896 + if (states[counter].number >= info->every)
4897 + states[counter].number = 0; /* reset the counter */
4899 + ++states[counter].number;
4904 + if (states[counter].number == 0)
4906 + ++states[counter].number;
4909 + if (states[counter].number >= info->every)
4910 + states[counter].number = 0;
4912 + ++states[counter].number;
4918 + /* We're using the --packet, so there must be a rule for every value */
4919 + if (states[counter].number == info->packet)
4921 + /* only increment the counter when a match happens */
4922 + if (states[counter].number >= info->every)
4923 + states[counter].number = 0; /* reset the counter */
4925 + ++states[counter].number;
4934 + spin_unlock(&states[counter].lock);
4938 + spin_unlock(&states[counter].lock);
4943 +ipt_nth_checkentry(const char *tablename,
4944 + const struct ipt_ip *e,
4946 + unsigned int matchsize,
4947 + unsigned int hook_mask)
4949 + /* Parameters from userspace */
4950 + const struct ipt_nth_info *info = matchinfo;
4951 + unsigned counter = info->counter;
4952 + if((counter < 0) || (counter >= IPT_NTH_NUM_COUNTERS))
4954 + printk(KERN_WARNING "nth: invalid counter %u. counter between 0 and %u\n", counter, IPT_NTH_NUM_COUNTERS-1);
4958 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_nth_info))) {
4959 + printk("nth: matchsize %u != %u\n", matchsize,
4960 + IPT_ALIGN(sizeof(struct ipt_nth_info)));
4964 + states[counter].number = info->startat;
4969 +static struct ipt_match ipt_nth_reg = {
4971 + .match = ipt_nth_match,
4972 + .checkentry = ipt_nth_checkentry,
4976 +static int __init init(void)
4980 + memset(&states, 0, sizeof(states));
4981 + for (counter = 0; counter < IPT_NTH_NUM_COUNTERS; counter++)
4982 + spin_lock_init(&(states[counter].lock));
4984 + return ipt_register_match(&ipt_nth_reg);
4987 +static void __exit fini(void)
4989 + ipt_unregister_match(&ipt_nth_reg);
4994 diff -Nur linux-2.6.6-rc1.org/net/ipv4/netfilter/ipt_osf.c linux-2.6.6-rc1/net/ipv4/netfilter/ipt_osf.c
4995 --- linux-2.6.6-rc1.org/net/ipv4/netfilter/ipt_osf.c 1970-01-01 01:00:00.000000000 +0100
4996 +++ linux-2.6.6-rc1/net/ipv4/netfilter/ipt_osf.c 2004-04-19 10:08:35.000000000 +0200
5001 + * Copyright (c) 2003 Evgeniy Polyakov <johnpol@2ka.mipt.ru>
5004 + * This program is free software; you can redistribute it and/or modify
5005 + * it under the terms of the GNU General Public License as published by
5006 + * the Free Software Foundation; either version 2 of the License, or
5007 + * (at your option) any later version.
5009 + * This program is distributed in the hope that it will be useful,
5010 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5011 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5012 + * GNU General Public License for more details.
5014 + * You should have received a copy of the GNU General Public License
5015 + * along with this program; if not, write to the Free Software
5016 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
5020 + * OS fingerprint matching module.
5021 + * It simply compares various parameters from SYN packet with
5022 + * some hardcoded ones.
5024 + * Original table was created by Michal Zalewski <lcamtuf@coredump.cx>
5028 +#include <linux/config.h>
5029 +#include <linux/kernel.h>
5030 +#include <linux/types.h>
5031 +#include <linux/string.h>
5032 +#include <linux/smp.h>
5033 +#include <linux/module.h>
5034 +#include <linux/skbuff.h>
5035 +#include <linux/file.h>
5036 +#include <linux/ip.h>
5037 +#include <linux/proc_fs.h>
5038 +#include <linux/fs.h>
5039 +#include <linux/slab.h>
5040 +#include <linux/spinlock.h>
5041 +#include <linux/ctype.h>
5042 +#include <linux/list.h>
5043 +#include <linux/if.h>
5044 +#include <linux/tcp.h>
5046 +#include <net/sock.h>
5047 +#include <net/ip.h>
5049 +#include <linux/netfilter_ipv4/ip_tables.h>
5051 +#include <linux/netfilter_ipv4/ipt_osf.h>
5056 +#define log(x...) printk(KERN_INFO "ipt_osf: " x)
5057 +#define loga(x...) printk(x)
5059 +#define log(x...) do {} while(0)
5060 +#define loga(x...) do {} while(0)
5063 +#define FMATCH_WRONG 0
5064 +#define FMATCH_OK 1
5065 +#define FMATCH_OPT_WRONG 2
5068 +#define OSFPDEL ':'
5069 +#define MAXOPTSTRLEN 128
5070 +#define OSFFLUSH "FLUSH"
5072 +static rwlock_t osf_lock = RW_LOCK_UNLOCKED;
5073 +static spinlock_t ipt_osf_netlink_lock = SPIN_LOCK_UNLOCKED;
5074 +static struct list_head finger_list;
5075 +static int match(const struct sk_buff *, const struct net_device *, const struct net_device *,
5076 + const void *, int,
5077 + const void *, u_int16_t,
5079 +static int checkentry(const char *, const struct ipt_ip *, void *,
5080 + unsigned int, unsigned int);
5082 +static unsigned long seq, ipt_osf_groups = 1;
5083 +static struct sock *nts;
5085 +static struct ipt_match osf_match =
5095 +static void ipt_osf_nlsend(struct osf_finger *f, const struct sk_buff *sk)
5097 + unsigned int size;
5098 + struct sk_buff *skb;
5099 + struct ipt_osf_nlmsg *data;
5100 + struct nlmsghdr *nlh;
5102 + size = NLMSG_SPACE(sizeof(struct ipt_osf_nlmsg));
5104 + skb = alloc_skb(size, GFP_ATOMIC);
5107 + log("skb_alloc() failed.\n");
5111 + nlh = NLMSG_PUT(skb, 0, seq++, NLMSG_DONE, size - sizeof(*nlh));
5113 + data = (struct ipt_osf_nlmsg *)NLMSG_DATA(nlh);
5115 + memcpy(&data->f, f, sizeof(struct osf_finger));
5116 + memcpy(&data->ip, sk->nh.iph, sizeof(struct iphdr));
5117 + memcpy(&data->tcp, (struct tcphdr *)((u_int32_t *)sk->nh.iph + sk->nh.iph->ihl), sizeof(struct tcphdr));
5119 + NETLINK_CB(skb).dst_groups = ipt_osf_groups;
5120 + netlink_broadcast(nts, skb, 0, ipt_osf_groups, GFP_ATOMIC);
5126 +static inline int smart_dec(const struct sk_buff *skb, unsigned long flags, unsigned char f_ttl)
5128 + struct iphdr *ip = skb->nh.iph;
5130 + if (flags & IPT_OSF_SMART)
5132 + struct in_device *in_dev = in_dev_get(skb->dev);
5136 + if (inet_ifa_match(ip->saddr, ifa))
5138 + in_dev_put(in_dev);
5139 + return (ip->ttl == f_ttl);
5142 + endfor_ifa(in_dev);
5144 + in_dev_put(in_dev);
5145 + return (ip->ttl <= f_ttl);
5148 + return (ip->ttl == f_ttl);
5152 +match(const struct sk_buff *skb, const struct net_device *in, const struct net_device *out,
5153 + const void *matchinfo, int offset,
5154 + const void *hdr, u_int16_t datalen,
5157 + struct ipt_osf_info *info = (struct ipt_osf_info *)matchinfo;
5158 + struct iphdr *ip = skb->nh.iph;
5159 + struct tcphdr *tcp;
5160 + int fmatch = FMATCH_WRONG, fcount = 0;
5161 + unsigned long totlen, optsize = 0, window;
5162 + unsigned char df, *optp = NULL, *_optp = NULL;
5163 + char check_WSS = 0;
5164 + struct list_head *ent;
5165 + struct osf_finger *f;
5170 + tcp = (struct tcphdr *)((u_int32_t *)ip + ip->ihl);
5175 + totlen = ntohs(ip->tot_len);
5176 + df = ((ntohs(ip->frag_off) & IP_DF)?1:0);
5177 + window = ntohs(tcp->window);
5179 + if (tcp->doff*4 > sizeof(struct tcphdr))
5181 + _optp = optp = (char *)(tcp+1);
5182 + optsize = tcp->doff*4 - sizeof(struct tcphdr);
5186 + /* Actually we can create hash/table of all genres and search
5187 + * only in appropriate part, but here is initial variant,
5188 + * so will use slow path.
5190 + read_lock(&osf_lock);
5191 + list_for_each(ent, &finger_list)
5193 + f = list_entry(ent, struct osf_finger, flist);
5195 + if (!(info->flags & IPT_OSF_LOG) && strcmp(info->genre, f->genre))
5199 + fmatch = FMATCH_WRONG;
5201 + if (totlen == f->ss && df == f->df &&
5202 + smart_dec(skb, info->flags, f->ttl))
5204 + unsigned long foptsize;
5206 + unsigned short mss = 0;
5210 + switch (f->wss.wc)
5212 + case 0: check_WSS = 0; break;
5213 + case 'S': check_WSS = 1; break;
5214 + case 'T': check_WSS = 2; break;
5215 + case '%': check_WSS = 3; break;
5216 + default: log("Wrong fingerprint wss.wc=%d, %s - %s\n",
5217 + f->wss.wc, f->genre, f->details);
5221 + if (check_WSS == 4)
5224 + /* Check options */
5227 + for (optnum=0; optnum<f->opt_num; ++optnum)
5228 + foptsize += f->opt[optnum].length;
5231 + if (foptsize > MAX_IPOPTLEN || optsize > MAX_IPOPTLEN || optsize != foptsize)
5236 + fmatch = FMATCH_OK;
5237 + loga("\tYEP : matching without options.\n");
5238 + if ((info->flags & IPT_OSF_LOG) &&
5239 + info->loglevel == IPT_OSF_LOGLEVEL_FIRST)
5246 + for (optnum=0; optnum<f->opt_num; ++optnum)
5248 + if (f->opt[optnum].kind == (*optp))
5250 + unsigned char len = f->opt[optnum].length;
5251 + unsigned char *optend = optp + len;
5252 + int loop_cont = 0;
5254 + fmatch = FMATCH_OK;
5260 + mss = ntohs(*(unsigned short *)(optp+2));
5275 + /* Skip kind and length fields*/
5278 + if (f->opt[optnum].wc.val != 0)
5280 + unsigned long tmp = 0;
5282 + /* Hmmm... It looks a bit ugly. :) */
5283 + memcpy(&tmp, optp,
5284 + (len > sizeof(unsigned long)?
5285 + sizeof(unsigned long):len));
5286 + /* 2 + 2: optlen(2 bytes) +
5287 + * kind(1 byte) + length(1 byte) */
5293 + if (f->opt[optnum].wc.wc == '%')
5295 + if ((tmp % f->opt[optnum].wc.val) != 0)
5296 + fmatch = FMATCH_OPT_WRONG;
5298 + else if (tmp != f->opt[optnum].wc.val)
5299 + fmatch = FMATCH_OPT_WRONG;
5306 + fmatch = FMATCH_OPT_WRONG;
5308 + if (fmatch != FMATCH_OK)
5312 + if (fmatch != FMATCH_OPT_WRONG)
5314 + fmatch = FMATCH_WRONG;
5316 + switch (check_WSS)
5319 + if (f->wss.val == 0 || window == f->wss.val)
5320 + fmatch = FMATCH_OK;
5323 +/* Lurked in OpenBSD */
5324 +#define SMART_MSS 1460
5325 + if (window == f->wss.val*mss ||
5326 + window == f->wss.val*SMART_MSS)
5327 + fmatch = FMATCH_OK;
5330 + if (window == f->wss.val*(mss+40) ||
5331 + window == f->wss.val*(SMART_MSS+40))
5332 + fmatch = FMATCH_OK;
5335 + if ((window % f->wss.val) == 0)
5336 + fmatch = FMATCH_OK;
5342 + if (fmatch == FMATCH_OK)
5345 + log("%s [%s:%s:%s] : %u.%u.%u.%u:%u -> %u.%u.%u.%u:%u hops=%d\n",
5346 + f->genre, f->version,
5347 + f->subtype, f->details,
5348 + NIPQUAD(ip->saddr), ntohs(tcp->source),
5349 + NIPQUAD(ip->daddr), ntohs(tcp->dest),
5350 + f->ttl - ip->ttl);
5351 + if (info->flags & IPT_OSF_NETLINK)
5353 + spin_lock_bh(&ipt_osf_netlink_lock);
5354 + ipt_osf_nlsend(f, skb);
5355 + spin_unlock_bh(&ipt_osf_netlink_lock);
5357 + if ((info->flags & IPT_OSF_LOG) &&
5358 + info->loglevel == IPT_OSF_LOGLEVEL_FIRST)
5363 + if (!fcount && (info->flags & (IPT_OSF_LOG | IPT_OSF_NETLINK)))
5365 + unsigned char opt[4 * 15 - sizeof(struct tcphdr)];
5366 + unsigned int i, optsize;
5367 + struct osf_finger fg;
5369 + memset(&fg, 0, sizeof(fg));
5371 + if ((info->flags & IPT_OSF_LOG))
5372 + log("Unknown: %lu:%d:%d:%lu:", window, ip->ttl, df, totlen);
5375 + optsize = tcp->doff * 4 - sizeof(struct tcphdr);
5376 + if (skb_copy_bits(skb, ip->ihl*4 + sizeof(struct tcphdr),
5377 + opt, optsize) < 0)
5379 + if (info->flags & IPT_OSF_LOG)
5380 + loga("TRUNCATED");
5381 + if (info->flags & IPT_OSF_NETLINK)
5382 + strcpy(fg.details, "TRUNCATED");
5386 + for (i = 0; i < optsize; i++)
5388 + if (info->flags & IPT_OSF_LOG)
5389 + loga("%02X", opt[i]);
5391 + if (info->flags & IPT_OSF_NETLINK)
5392 + memcpy(fg.details, opt, MAXDETLEN);
5395 + if ((info->flags & IPT_OSF_LOG))
5396 + loga(" %u.%u.%u.%u:%u -> %u.%u.%u.%u:%u\n",
5397 + NIPQUAD(ip->saddr), ntohs(tcp->source),
5398 + NIPQUAD(ip->daddr), ntohs(tcp->dest));
5400 + if (info->flags & IPT_OSF_NETLINK)
5402 + fg.wss.val = window;
5406 + strncpy(fg.genre, "Unknown", MAXGENRELEN);
5408 + spin_lock_bh(&ipt_osf_netlink_lock);
5409 + ipt_osf_nlsend(&fg, skb);
5410 + spin_unlock_bh(&ipt_osf_netlink_lock);
5414 + read_unlock(&osf_lock);
5416 + return (fmatch == FMATCH_OK)?1:0;
5420 +checkentry(const char *tablename,
5421 + const struct ipt_ip *ip,
5423 + unsigned int matchsize,
5424 + unsigned int hook_mask)
5426 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_osf_info)))
5428 + if (ip->proto != IPPROTO_TCP)
5434 +static char * osf_strchr(char *ptr, char c)
5438 + tmp = strchr(ptr, c);
5440 + while (tmp && tmp+1 && isspace(*(tmp+1)))
5446 +static struct osf_finger * finger_alloc(void)
5448 + struct osf_finger *f;
5450 + f = kmalloc(sizeof(struct osf_finger), GFP_KERNEL);
5452 + memset(f, 0, sizeof(struct osf_finger));
5457 +static void finger_free(struct osf_finger *f)
5459 + memset(f, 0, sizeof(struct osf_finger));
5464 +static void osf_parse_opt(struct osf_opt *opt, int *optnum, char *obuf, int olen)
5468 + unsigned long val;
5472 + while (ptr != NULL && i < olen)
5481 + ptr = osf_strchr(&obuf[i], OPTDEL);
5486 + i += (int)(ptr-&obuf[i]);
5493 + op = OSFOPT_SACKP;
5494 + ptr = osf_strchr(&obuf[i], OPTDEL);
5499 + i += (int)(ptr-&obuf[i]);
5507 + ptr = osf_strchr(&obuf[i], OPTDEL);
5512 + i += (int)(ptr-&obuf[i]);
5520 + ptr = osf_strchr(&obuf[i], OPTDEL);
5523 + switch (obuf[i+1])
5525 + case '%': wc = '%'; break;
5526 + case 'S': wc = 'S'; break;
5527 + case 'T': wc = 'T'; break;
5528 + default: wc = 0; break;
5534 + val = simple_strtoul(&obuf[i+2], NULL, 10);
5536 + val = simple_strtoul(&obuf[i+1], NULL, 10);
5537 + i += (int)(ptr-&obuf[i]);
5545 + ptr = osf_strchr(&obuf[i], OPTDEL);
5548 + if (obuf[i+1] == '%')
5553 + val = simple_strtoul(&obuf[i+2], NULL, 10);
5555 + val = simple_strtoul(&obuf[i+1], NULL, 10);
5556 + i += (int)(ptr-&obuf[i]);
5564 + ptr = osf_strchr(&obuf[i], OPTDEL);
5569 + i += (int)(ptr-&obuf[i]);
5576 + ptr = osf_strchr(&obuf[i], OPTDEL);
5580 + i += (int)(ptr-&obuf[i]);
5588 + opt[*optnum].kind = IANA_opts[op].kind;
5589 + opt[*optnum].length = IANA_opts[op].length;
5590 + opt[*optnum].wc.wc = wc;
5591 + opt[*optnum].wc.val = val;
5597 +static int osf_proc_read(char *buf, char **start, off_t off, int count, int *eof, void *data)
5599 + struct list_head *ent;
5600 + struct osf_finger *f = NULL;
5606 + read_lock_bh(&osf_lock);
5607 + list_for_each(ent, &finger_list)
5609 + f = list_entry(ent, struct osf_finger, flist);
5611 + log("%s [%s]", f->genre, f->details);
5613 + count += sprintf(buf+count, "%s - %s[%s] : %s",
5614 + f->genre, f->version,
5615 + f->subtype, f->details);
5620 + //count += sprintf(buf+count, " OPT: ");
5621 + for (i=0; i<f->opt_num; ++i)
5623 + //count += sprintf(buf+count, "%d.%c%lu; ",
5624 + // f->opt[i].kind, (f->opt[i].wc.wc)?f->opt[i].wc.wc:' ', f->opt[i].wc.val);
5625 + loga("%d.%c%lu; ",
5626 + f->opt[i].kind, (f->opt[i].wc.wc)?f->opt[i].wc.wc:' ', f->opt[i].wc.val);
5630 + count += sprintf(buf+count, "\n");
5632 + read_unlock_bh(&osf_lock);
5637 +static int osf_proc_write(struct file *file, const char *buffer, unsigned long count, void *data)
5641 + char obuf[MAXOPTSTRLEN];
5642 + struct osf_finger *finger;
5643 + struct list_head *ent, *n;
5645 + char *pbeg, *pend;
5647 + if (count == strlen(OSFFLUSH) && !strncmp(buffer, OSFFLUSH, strlen(OSFFLUSH)))
5650 + write_lock_bh(&osf_lock);
5651 + list_for_each_safe(ent, n, &finger_list)
5654 + finger = list_entry(ent, struct osf_finger, flist);
5655 + list_del(&finger->flist);
5656 + finger_free(finger);
5658 + write_unlock_bh(&osf_lock);
5660 + log("Flushed %d entries.\n", i);
5667 + for (i=0; i<count && buffer[i] != '\0'; ++i)
5668 + if (buffer[i] == ':')
5671 + if (cnt != 8 || i != count)
5673 + log("Wrong input line cnt=%d[8], len=%lu[%lu]\n",
5678 + memset(obuf, 0, sizeof(obuf));
5680 + finger = finger_alloc();
5683 + log("Failed to allocate new fingerprint entry.\n");
5687 + pbeg = (char *)buffer;
5688 + pend = osf_strchr(pbeg, OSFPDEL);
5692 + if (pbeg[0] == 'S')
5694 + finger->wss.wc = 'S';
5695 + if (pbeg[1] == '%')
5696 + finger->wss.val = simple_strtoul(pbeg+2, NULL, 10);
5697 + else if (pbeg[1] == '*')
5698 + finger->wss.val = 0;
5700 + finger->wss.val = simple_strtoul(pbeg+1, NULL, 10);
5702 + else if (pbeg[0] == 'T')
5704 + finger->wss.wc = 'T';
5705 + if (pbeg[1] == '%')
5706 + finger->wss.val = simple_strtoul(pbeg+2, NULL, 10);
5707 + else if (pbeg[1] == '*')
5708 + finger->wss.val = 0;
5710 + finger->wss.val = simple_strtoul(pbeg+1, NULL, 10);
5712 + else if (pbeg[0] == '%')
5714 + finger->wss.wc = '%';
5715 + finger->wss.val = simple_strtoul(pbeg+1, NULL, 10);
5717 + else if (isdigit(pbeg[0]))
5719 + finger->wss.wc = 0;
5720 + finger->wss.val = simple_strtoul(pbeg, NULL, 10);
5725 + pend = osf_strchr(pbeg, OSFPDEL);
5729 + finger->ttl = simple_strtoul(pbeg, NULL, 10);
5732 + pend = osf_strchr(pbeg, OSFPDEL);
5736 + finger->df = simple_strtoul(pbeg, NULL, 10);
5739 + pend = osf_strchr(pbeg, OSFPDEL);
5743 + finger->ss = simple_strtoul(pbeg, NULL, 10);
5747 + pend = osf_strchr(pbeg, OSFPDEL);
5751 + cnt = snprintf(obuf, sizeof(obuf), "%s", pbeg);
5755 + pend = osf_strchr(pbeg, OSFPDEL);
5759 + if (pbeg[0] == '@' || pbeg[0] == '*')
5760 + cnt = snprintf(finger->genre, sizeof(finger->genre), "%s", pbeg+1);
5762 + cnt = snprintf(finger->genre, sizeof(finger->genre), "%s", pbeg);
5766 + pend = osf_strchr(pbeg, OSFPDEL);
5770 + cnt = snprintf(finger->version, sizeof(finger->version), "%s", pbeg);
5774 + pend = osf_strchr(pbeg, OSFPDEL);
5778 + cnt = snprintf(finger->subtype, sizeof(finger->subtype), "%s", pbeg);
5782 + cnt = snprintf(finger->details,
5783 + ((count - (pbeg - buffer)+1) > MAXDETLEN)?MAXDETLEN:(count - (pbeg - buffer)+1),
5786 + log("%s - %s[%s] : %s\n",
5787 + finger->genre, finger->version,
5788 + finger->subtype, finger->details);
5790 + osf_parse_opt(finger->opt, &finger->opt_num, obuf, sizeof(obuf));
5793 + write_lock_bh(&osf_lock);
5794 + list_add_tail(&finger->flist, &finger_list);
5795 + write_unlock_bh(&osf_lock);
5800 +static int __init osf_init(void)
5803 + struct proc_dir_entry *p;
5805 + log("Startng OS fingerprint matching module.\n");
5807 + INIT_LIST_HEAD(&finger_list);
5809 + err = ipt_register_match(&osf_match);
5812 + log("Failed to register OS fingerprint matching module.\n");
5816 + p = create_proc_entry("sys/net/ipv4/osf", S_IFREG | 0644, NULL);
5819 + ipt_unregister_match(&osf_match);
5823 + p->write_proc = osf_proc_write;
5824 + p->read_proc = osf_proc_read;
5826 + nts = netlink_kernel_create(NETLINK_NFLOG, NULL);
5829 + log("netlink_kernel_create() failed\n");
5830 + remove_proc_entry("sys/net/ipv4/osf", NULL);
5831 + ipt_unregister_match(&osf_match);
5838 +static void __exit osf_fini(void)
5840 + struct list_head *ent, *n;
5841 + struct osf_finger *f;
5843 + remove_proc_entry("sys/net/ipv4/osf", NULL);
5844 + ipt_unregister_match(&osf_match);
5845 +// if (nts && nts->socket)
5846 +// sock_release(nts->socket);
5848 + list_for_each_safe(ent, n, &finger_list)
5850 + f = list_entry(ent, struct osf_finger, flist);
5851 + list_del(&f->flist);
5855 + log("OS fingerprint matching module finished.\n");
5858 +module_init(osf_init);
5859 +module_exit(osf_fini);
5861 +MODULE_LICENSE("GPL");
5862 +MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
5863 +MODULE_DESCRIPTION("Passive OS fingerprint matching.");
5864 diff -Nur linux-2.6.6-rc1.org/net/ipv4/netfilter/ipt_pool.c linux-2.6.6-rc1/net/ipv4/netfilter/ipt_pool.c
5865 --- linux-2.6.6-rc1.org/net/ipv4/netfilter/ipt_pool.c 1970-01-01 01:00:00.000000000 +0100
5866 +++ linux-2.6.6-rc1/net/ipv4/netfilter/ipt_pool.c 2004-04-19 10:08:36.000000000 +0200
5868 +/* Kernel module to match an IP address pool. */
5870 +#include <linux/module.h>
5871 +#include <linux/ip.h>
5872 +#include <linux/skbuff.h>
5874 +#include <linux/netfilter_ipv4/ip_tables.h>
5875 +#include <linux/netfilter_ipv4/ip_pool.h>
5876 +#include <linux/netfilter_ipv4/ipt_pool.h>
5878 +static inline int match_pool(
5883 + if (ip_pool_match(index, ntohl(addr)))
5889 + const struct sk_buff *skb,
5890 + const struct net_device *in,
5891 + const struct net_device *out,
5892 + const void *matchinfo,
5895 + u_int16_t datalen,
5898 + const struct ipt_pool_info *info = matchinfo;
5899 + const struct iphdr *iph = skb->nh.iph;
5901 + if (info->src != IP_POOL_NONE && !match_pool(info->src, iph->saddr,
5902 + info->flags&IPT_POOL_INV_SRC))
5905 + if (info->dst != IP_POOL_NONE && !match_pool(info->dst, iph->daddr,
5906 + info->flags&IPT_POOL_INV_DST))
5912 +static int checkentry(
5913 + const char *tablename,
5914 + const struct ipt_ip *ip,
5916 + unsigned int matchsize,
5917 + unsigned int hook_mask
5919 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_pool_info)))
5924 +static struct ipt_match pool_match
5925 += { { NULL, NULL }, "pool", &match, &checkentry, NULL, THIS_MODULE };
5927 +static int __init init(void)
5929 + return ipt_register_match(&pool_match);
5932 +static void __exit fini(void)
5934 + ipt_unregister_match(&pool_match);
5939 diff -Nur linux-2.6.6-rc1.org/net/ipv4/netfilter/ipt_psd.c linux-2.6.6-rc1/net/ipv4/netfilter/ipt_psd.c
5940 --- linux-2.6.6-rc1.org/net/ipv4/netfilter/ipt_psd.c 1970-01-01 01:00:00.000000000 +0100
5941 +++ linux-2.6.6-rc1/net/ipv4/netfilter/ipt_psd.c 2004-04-19 10:08:37.000000000 +0200
5944 + This is a module which is used for PSD (portscan detection)
5945 + Derived from scanlogd v2.1 written by Solar Designer <solar@false.com>
5946 + and LOG target module.
5948 + Copyright (C) 2000,2001 astaro AG
5950 + This file is distributed under the terms of the GNU General Public
5951 + License (GPL). Copies of the GPL can be obtained from:
5952 + ftp://prep.ai.mit.edu/pub/gnu/GPL
5954 + 2000-05-04 Markus Hennig <hennig@astaro.de> : initial
5955 + 2000-08-18 Dennis Koslowski <koslowski@astaro.de> : first release
5956 + 2000-12-01 Dennis Koslowski <koslowski@astaro.de> : UDP scans detection added
5957 + 2001-01-02 Dennis Koslowski <koslowski@astaro.de> : output modified
5958 + 2001-02-04 Jan Rekorajski <baggins@pld.org.pl> : converted from target to match
5961 +#include <linux/module.h>
5962 +#include <linux/skbuff.h>
5963 +#include <linux/ip.h>
5964 +#include <net/tcp.h>
5965 +#include <linux/spinlock.h>
5966 +#include <linux/netfilter_ipv4/ip_tables.h>
5967 +#include <linux/netfilter_ipv4/ipt_psd.h>
5970 +#define DEBUGP printk
5972 +#define DEBUGP(format, args...)
5975 +MODULE_LICENSE("GPL");
5976 +MODULE_AUTHOR("Dennis Koslowski <koslowski@astaro.com>");
5978 +#define HF_DADDR_CHANGING 0x01
5979 +#define HF_SPORT_CHANGING 0x02
5980 +#define HF_TOS_CHANGING 0x04
5981 +#define HF_TTL_CHANGING 0x08
5984 + * Information we keep per each target port
5987 + u_int16_t number; /* port number */
5988 + u_int8_t proto; /* protocol number */
5989 + u_int8_t and_flags; /* tcp ANDed flags */
5990 + u_int8_t or_flags; /* tcp ORed flags */
5994 + * Information we keep per each source address.
5997 + struct host *next; /* Next entry with the same hash */
5998 + clock_t timestamp; /* Last update time */
5999 + struct in_addr src_addr; /* Source address */
6000 + struct in_addr dest_addr; /* Destination address */
6001 + unsigned short src_port; /* Source port */
6002 + int count; /* Number of ports in the list */
6003 + int weight; /* Total weight of ports in the list */
6004 + struct port ports[SCAN_MAX_COUNT - 1]; /* List of ports */
6005 + unsigned char tos; /* TOS */
6006 + unsigned char ttl; /* TTL */
6007 + unsigned char flags; /* HF_ flags bitmask */
6011 + * State information.
6015 + struct host list[LIST_SIZE]; /* List of source addresses */
6016 + struct host *hash[HASH_SIZE]; /* Hash: pointers into the list */
6017 + int index; /* Oldest entry to be replaced */
6021 + * Convert an IP address into a hash table index.
6023 +static inline int hashfunc(struct in_addr addr)
6025 + unsigned int value;
6028 + value = addr.s_addr;
6032 + } while ((value >>= HASH_LOG));
6034 + return hash & (HASH_SIZE - 1);
6038 +ipt_psd_match(const struct sk_buff *pskb,
6039 + const struct net_device *in,
6040 + const struct net_device *out,
6041 + const void *matchinfo,
6044 + u_int16_t datalen,
6047 + struct iphdr *ip_hdr;
6048 + struct tcphdr *tcp_hdr;
6049 + struct in_addr addr;
6050 + u_int16_t src_port,dest_port;
6051 + u_int8_t tcp_flags, proto;
6053 + struct host *curr, *last, **head;
6054 + int hash, index, count;
6056 + /* Parameters from userspace */
6057 + const struct ipt_psd_info *psdinfo = matchinfo;
6060 + ip_hdr = pskb->nh.iph;
6062 + /* Sanity check */
6063 + if (ntohs(ip_hdr->frag_off) & IP_OFFSET) {
6064 + DEBUGP("PSD: sanity check failed\n");
6068 + /* TCP or UDP ? */
6069 + proto = ip_hdr->protocol;
6071 + if (proto != IPPROTO_TCP && proto != IPPROTO_UDP) {
6072 + DEBUGP("PSD: protocol not supported\n");
6076 + /* Get the source address, source & destination ports, and TCP flags */
6078 + addr.s_addr = ip_hdr->saddr;
6080 + tcp_hdr = (struct tcphdr*)((u_int32_t *)ip_hdr + ip_hdr->ihl);
6082 + /* Yep, it´s dirty */
6083 + src_port = tcp_hdr->source;
6084 + dest_port = tcp_hdr->dest;
6086 + if (proto == IPPROTO_TCP) {
6087 + tcp_flags = *((u_int8_t*)tcp_hdr + 13);
6093 + /* We're using IP address 0.0.0.0 for a special purpose here, so don't let
6094 + * them spoof us. [DHCP needs this feature - HW] */
6095 + if (!addr.s_addr) {
6096 + DEBUGP("PSD: spoofed source address (0.0.0.0)\n");
6100 + /* Use jiffies here not to depend on someone setting the time while we're
6101 + * running; we need to be careful with possible return value overflows. */
6104 + spin_lock(&state.lock);
6106 + /* Do we know this source address already? */
6109 + if ((curr = *(head = &state.hash[hash = hashfunc(addr)])))
6111 + if (curr->src_addr.s_addr == addr.s_addr) break;
6113 + if (curr->next) last = curr;
6114 + } while ((curr = curr->next));
6118 + /* We know this address, and the entry isn't too old. Update it. */
6119 + if (now - curr->timestamp <= (psdinfo->delay_threshold*HZ)/100 &&
6120 + time_after_eq(now, curr->timestamp)) {
6122 + /* Just update the appropriate list entry if we've seen this port already */
6123 + for (index = 0; index < curr->count; index++) {
6124 + if (curr->ports[index].number == dest_port) {
6125 + curr->ports[index].proto = proto;
6126 + curr->ports[index].and_flags &= tcp_flags;
6127 + curr->ports[index].or_flags |= tcp_flags;
6128 + goto out_no_match;
6132 + /* TCP/ACK and/or TCP/RST to a new port? This could be an outgoing connection. */
6133 + if (proto == IPPROTO_TCP && (tcp_hdr->ack || tcp_hdr->rst))
6134 + goto out_no_match;
6136 + /* Packet to a new port, and not TCP/ACK: update the timestamp */
6137 + curr->timestamp = now;
6139 + /* Logged this scan already? Then drop the packet. */
6140 + if (curr->weight >= psdinfo->weight_threshold)
6143 + /* Specify if destination address, source port, TOS or TTL are not fixed */
6144 + if (curr->dest_addr.s_addr != ip_hdr->daddr)
6145 + curr->flags |= HF_DADDR_CHANGING;
6146 + if (curr->src_port != src_port)
6147 + curr->flags |= HF_SPORT_CHANGING;
6148 + if (curr->tos != ip_hdr->tos)
6149 + curr->flags |= HF_TOS_CHANGING;
6150 + if (curr->ttl != ip_hdr->ttl)
6151 + curr->flags |= HF_TTL_CHANGING;
6153 + /* Update the total weight */
6154 + curr->weight += (ntohs(dest_port) < 1024) ?
6155 + psdinfo->lo_ports_weight : psdinfo->hi_ports_weight;
6157 + /* Got enough destination ports to decide that this is a scan? */
6158 + /* Then log it and drop the packet. */
6159 + if (curr->weight >= psdinfo->weight_threshold)
6162 + /* Remember the new port */
6163 + if (curr->count < SCAN_MAX_COUNT) {
6164 + curr->ports[curr->count].number = dest_port;
6165 + curr->ports[curr->count].proto = proto;
6166 + curr->ports[curr->count].and_flags = tcp_flags;
6167 + curr->ports[curr->count].or_flags = tcp_flags;
6171 + goto out_no_match;
6174 + /* We know this address, but the entry is outdated. Mark it unused, and
6175 + * remove from the hash table. We'll allocate a new entry instead since
6176 + * this one might get re-used too soon. */
6177 + curr->src_addr.s_addr = 0;
6179 + last->next = last->next->next;
6181 + *head = (*head)->next;
6185 + /* We don't need an ACK from a new source address */
6186 + if (proto == IPPROTO_TCP && tcp_hdr->ack)
6187 + goto out_no_match;
6189 + /* Got too many source addresses with the same hash value? Then remove the
6190 + * oldest one from the hash table, so that they can't take too much of our
6191 + * CPU time even with carefully chosen spoofed IP addresses. */
6192 + if (count >= HASH_MAX && last) last->next = NULL;
6194 + /* We're going to re-use the oldest list entry, so remove it from the hash
6195 + * table first (if it is really already in use, and isn't removed from the
6196 + * hash table already because of the HASH_MAX check above). */
6198 + /* First, find it */
6199 + if (state.list[state.index].src_addr.s_addr)
6200 + head = &state.hash[hashfunc(state.list[state.index].src_addr)];
6204 + if ((curr = *head))
6206 + if (curr == &state.list[state.index]) break;
6208 + } while ((curr = curr->next));
6210 + /* Then, remove it */
6213 + last->next = last->next->next;
6215 + *head = (*head)->next;
6218 + /* Get our list entry */
6219 + curr = &state.list[state.index++];
6220 + if (state.index >= LIST_SIZE) state.index = 0;
6222 + /* Link it into the hash table */
6223 + head = &state.hash[hash];
6224 + curr->next = *head;
6227 + /* And fill in the fields */
6228 + curr->timestamp = now;
6229 + curr->src_addr = addr;
6230 + curr->dest_addr.s_addr = ip_hdr->daddr;
6231 + curr->src_port = src_port;
6233 + curr->weight = (ntohs(dest_port) < 1024) ?
6234 + psdinfo->lo_ports_weight : psdinfo->hi_ports_weight;
6235 + curr->ports[0].number = dest_port;
6236 + curr->ports[0].proto = proto;
6237 + curr->ports[0].and_flags = tcp_flags;
6238 + curr->ports[0].or_flags = tcp_flags;
6239 + curr->tos = ip_hdr->tos;
6240 + curr->ttl = ip_hdr->ttl;
6243 + spin_unlock(&state.lock);
6247 + spin_unlock(&state.lock);
6251 +static int ipt_psd_checkentry(const char *tablename,
6252 + const struct ipt_ip *e,
6254 + unsigned int matchsize,
6255 + unsigned int hook_mask)
6257 +/* const struct ipt_psd_info *psdinfo = targinfo;*/
6259 + /* we accept TCP only */
6260 +/* if (e->ip.proto != IPPROTO_TCP) { */
6261 +/* DEBUGP("PSD: specified protocol may be TCP only\n"); */
6265 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_psd_info))) {
6266 + DEBUGP("PSD: matchsize %u != %u\n",
6268 + IPT_ALIGN(sizeof(struct ipt_psd_info)));
6275 +static struct ipt_match ipt_psd_reg = {
6279 + ipt_psd_checkentry,
6283 +static int __init init(void)
6285 + if (ipt_register_match(&ipt_psd_reg))
6288 + memset(&state, 0, sizeof(state));
6290 + spin_lock_init(&(state.lock));
6292 + printk("netfilter PSD loaded - (c) astaro AG\n");
6296 +static void __exit fini(void)
6298 + ipt_unregister_match(&ipt_psd_reg);
6299 + printk("netfilter PSD unloaded - (c) astaro AG\n");
6304 diff -Nur linux-2.6.6-rc1.org/net/ipv4/netfilter/ipt_quota.c linux-2.6.6-rc1/net/ipv4/netfilter/ipt_quota.c
6305 --- linux-2.6.6-rc1.org/net/ipv4/netfilter/ipt_quota.c 1970-01-01 01:00:00.000000000 +0100
6306 +++ linux-2.6.6-rc1/net/ipv4/netfilter/ipt_quota.c 2004-04-19 10:08:37.000000000 +0200
6309 + * netfilter module to enforce network quotas
6311 + * Sam Johnston <samj@samj.net>
6313 +#include <linux/module.h>
6314 +#include <linux/skbuff.h>
6315 +#include <linux/spinlock.h>
6316 +#include <linux/interrupt.h>
6318 +#include <linux/netfilter_ipv4/ip_tables.h>
6319 +#include <linux/netfilter_ipv4/ipt_quota.h>
6321 +MODULE_LICENSE("GPL");
6322 +MODULE_AUTHOR("Sam Johnston <samj@samj.net>");
6324 +static spinlock_t quota_lock = SPIN_LOCK_UNLOCKED;
6327 +match(const struct sk_buff *skb,
6328 + const struct net_device *in,
6329 + const struct net_device *out,
6330 + const void *matchinfo,
6331 + int offset, int *hotdrop)
6333 + struct ipt_quota_info *q = (struct ipt_quota_info *) matchinfo;
6334 + unsigned int datalen;
6336 + if (skb->len < sizeof(struct iphdr))
6339 + datalen = skb->len - skb->nh.iph->ihl*4;
6341 + spin_lock_bh("a_lock);
6343 + if (q->quota >= datalen) {
6344 + /* we can afford this one */
6345 + q->quota -= datalen;
6346 + spin_unlock_bh("a_lock);
6348 +#ifdef DEBUG_IPT_QUOTA
6349 + printk("IPT Quota OK: %llu datlen %d \n", q->quota, datalen);
6354 + /* so we do not allow even small packets from now on */
6357 +#ifdef DEBUG_IPT_QUOTA
6358 + printk("IPT Quota Failed: %llu datlen %d \n", q->quota, datalen);
6361 + spin_unlock_bh("a_lock);
6366 +checkentry(const char *tablename,
6367 + const struct ipt_ip *ip,
6368 + void *matchinfo, unsigned int matchsize, unsigned int hook_mask)
6370 + /* TODO: spinlocks? sanity checks? */
6371 + if (matchsize != IPT_ALIGN(sizeof (struct ipt_quota_info)))
6377 +static struct ipt_match quota_match = {
6380 + .checkentry = checkentry,
6387 + return ipt_register_match("a_match);
6393 + ipt_unregister_match("a_match);
6399 diff -Nur linux-2.6.6-rc1.org/net/ipv4/netfilter/ipt_random.c linux-2.6.6-rc1/net/ipv4/netfilter/ipt_random.c
6400 --- linux-2.6.6-rc1.org/net/ipv4/netfilter/ipt_random.c 1970-01-01 01:00:00.000000000 +0100
6401 +++ linux-2.6.6-rc1/net/ipv4/netfilter/ipt_random.c 2004-04-19 10:08:38.000000000 +0200
6404 + This is a module which is used for a "random" match support.
6405 + This file is distributed under the terms of the GNU General Public
6406 + License (GPL). Copies of the GPL can be obtained from:
6407 + ftp://prep.ai.mit.edu/pub/gnu/GPL
6409 + 2001-10-14 Fabrice MARIE <fabrice@netfilter.org> : initial implementation.
6412 +#include <linux/module.h>
6413 +#include <linux/skbuff.h>
6414 +#include <linux/ip.h>
6415 +#include <linux/random.h>
6416 +#include <net/tcp.h>
6417 +#include <linux/spinlock.h>
6418 +#include <linux/netfilter_ipv4/ip_tables.h>
6419 +#include <linux/netfilter_ipv4/ipt_random.h>
6421 +MODULE_LICENSE("GPL");
6424 +ipt_rand_match(const struct sk_buff *pskb,
6425 + const struct net_device *in,
6426 + const struct net_device *out,
6427 + const void *matchinfo,
6430 + u_int16_t datalen,
6433 + /* Parameters from userspace */
6434 + const struct ipt_rand_info *info = matchinfo;
6435 + u_int8_t random_number;
6437 + /* get 1 random number from the kernel random number generation routine */
6438 + get_random_bytes((void *)(&random_number), 1);
6440 + /* Do we match ? */
6441 + if (random_number <= info->average)
6448 +ipt_rand_checkentry(const char *tablename,
6449 + const struct ipt_ip *e,
6451 + unsigned int matchsize,
6452 + unsigned int hook_mask)
6454 + /* Parameters from userspace */
6455 + const struct ipt_rand_info *info = matchinfo;
6457 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_rand_info))) {
6458 + printk("ipt_random: matchsize %u != %u\n", matchsize,
6459 + IPT_ALIGN(sizeof(struct ipt_rand_info)));
6463 + /* must be 1 <= average % <= 99 */
6464 + /* 1 x 2.55 = 2 */
6465 + /* 99 x 2.55 = 252 */
6466 + if ((info->average < 2) || (info->average > 252)) {
6467 + printk("ipt_random: invalid average %u\n", info->average);
6474 +static struct ipt_match ipt_rand_reg = {
6478 + ipt_rand_checkentry,
6482 +static int __init init(void)
6484 + if (ipt_register_match(&ipt_rand_reg))
6487 + printk("ipt_random match loaded\n");
6491 +static void __exit fini(void)
6493 + ipt_unregister_match(&ipt_rand_reg);
6494 + printk("ipt_random match unloaded\n");
6499 diff -Nur linux-2.6.6-rc1.org/net/ipv4/netfilter/ipt_realm.c linux-2.6.6-rc1/net/ipv4/netfilter/ipt_realm.c
6500 --- linux-2.6.6-rc1.org/net/ipv4/netfilter/ipt_realm.c 1970-01-01 01:00:00.000000000 +0100
6501 +++ linux-2.6.6-rc1/net/ipv4/netfilter/ipt_realm.c 2004-04-19 10:08:39.000000000 +0200
6503 +/* IP tables module for matching the routing realm
6507 + * (C) 2003 by Sampsa Ranta <sampsa@netsonic.fi>
6509 + * This program is free software; you can redistribute it and/or modify
6510 + * it under the terms of the GNU General Public License version 2 as
6511 + * published by the Free Software Foundation.
6514 +#include <linux/module.h>
6515 +#include <linux/skbuff.h>
6516 +#include <linux/netdevice.h>
6517 +#include <net/route.h>
6519 +#include <linux/netfilter_ipv4/ipt_realm.h>
6520 +#include <linux/netfilter_ipv4/ip_tables.h>
6522 +MODULE_AUTHOR("Sampsa Ranta <sampsa@netsonic.fi>");
6523 +MODULE_LICENSE("GPL");
6526 +match(const struct sk_buff *skb,
6527 + const struct net_device *in,
6528 + const struct net_device *out,
6529 + const void *matchinfo,
6533 + const struct ipt_realm_info *info = matchinfo;
6534 + struct dst_entry *dst = skb->dst;
6539 + return (info->id == (dst->tclassid & info->mask)) ^ info->invert;
6542 +static int check(const char *tablename,
6543 + const struct ipt_ip *ip,
6545 + unsigned int matchsize,
6546 + unsigned int hook_mask)
6549 + & ~((1 << NF_IP_POST_ROUTING) | (1 << NF_IP_FORWARD) |
6550 + (1 << NF_IP_LOCAL_OUT)| (1 << NF_IP_LOCAL_IN))) {
6551 + printk("ipt_realm: only valid for POST_ROUTING, LOCAL_OUT, "
6552 + "LOCAL_IN or FORWARD.\n");
6556 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_realm_info)))
6562 +static struct ipt_match realm_match = {
6565 + .checkentry = check,
6569 +static int __init init(void)
6571 + return ipt_register_match(&realm_match);
6574 +static void __exit fini(void)
6576 + ipt_unregister_match(&realm_match);
6581 diff -Nur linux-2.6.6-rc1.org/net/ipv4/netfilter/ipt_sctp.c linux-2.6.6-rc1/net/ipv4/netfilter/ipt_sctp.c
6582 --- linux-2.6.6-rc1.org/net/ipv4/netfilter/ipt_sctp.c 1970-01-01 01:00:00.000000000 +0100
6583 +++ linux-2.6.6-rc1/net/ipv4/netfilter/ipt_sctp.c 2004-04-19 10:08:40.000000000 +0200
6585 +#include <linux/module.h>
6586 +#include <linux/skbuff.h>
6587 +#include <net/ip.h>
6588 +#include <linux/sctp.h>
6590 +#include <linux/netfilter_ipv4/ip_tables.h>
6591 +#include <linux/netfilter_ipv4/ipt_sctp.h>
6594 +#define duprintf(format, args...) printk(format , ## args)
6596 +#define duprintf(format, args...)
6599 +#define SCCHECK(cond, option, flag, invflag) (!((flag) & (option)) \
6600 + || (!!((invflag) & (option)) ^ (cond)))
6603 +match_flags(const struct ipt_sctp_flag_info *flag_info,
6604 + const int flag_count,
6605 + u_int8_t chunktype,
6606 + u_int8_t chunkflags)
6610 + for (i = 0; i < flag_count; i++) {
6611 + if (flag_info[i].chunktype == chunktype) {
6612 + return (chunkflags & flag_info[i].flag_mask) == flag_info[i].flag;
6620 +match_packet(const struct sk_buff *skb,
6621 + const u_int32_t *chunkmap,
6622 + int chunk_match_type,
6623 + const struct ipt_sctp_flag_info *flag_info,
6624 + const int flag_count,
6628 + u_int32_t chunkmapcopy[256 / sizeof (u_int32_t)];
6629 + sctp_chunkhdr_t sch;
6633 + if (chunk_match_type == SCTP_CHUNK_MATCH_ALL) {
6634 + SCTP_CHUNKMAP_COPY(chunkmapcopy, chunkmap);
6637 + offset = skb->nh.iph->ihl * 4 + sizeof (sctp_sctphdr_t);
6639 + if (skb_copy_bits(skb, offset, &sch, sizeof(sch)) < 0) {
6640 + duprintf("Dropping invalid SCTP packet.\n");
6645 + duprintf("Chunk num: %d\toffset: %d\ttype: %d\tlength: %d\tflags: %x\n",
6646 + ++i, offset, sch.type, htons(sch.length), sch.flags);
6648 + offset += (htons(sch.length) + 3) & ~3;
6650 + duprintf("skb->len: %d\toffset: %d\n", skb->len, offset);
6652 + if (SCTP_CHUNKMAP_IS_SET(chunkmap, sch.type)) {
6653 + switch (chunk_match_type) {
6654 + case SCTP_CHUNK_MATCH_ANY:
6655 + if (match_flags(flag_info, flag_count,
6656 + sch.type, sch.flags)) {
6661 + case SCTP_CHUNK_MATCH_ALL:
6662 + if (match_flags(flag_info, flag_count,
6663 + sch.type, sch.flags)) {
6664 + SCTP_CHUNKMAP_CLEAR(chunkmapcopy, sch.type);
6668 + case SCTP_CHUNK_MATCH_ONLY:
6669 + if (!match_flags(flag_info, flag_count,
6670 + sch.type, sch.flags)) {
6676 + switch (chunk_match_type) {
6677 + case SCTP_CHUNK_MATCH_ONLY:
6681 + } while (offset < skb->len);
6683 + switch (chunk_match_type) {
6684 + case SCTP_CHUNK_MATCH_ALL:
6685 + return SCTP_CHUNKMAP_IS_CLEAR(chunkmap);
6686 + case SCTP_CHUNK_MATCH_ANY:
6688 + case SCTP_CHUNK_MATCH_ONLY:
6692 + /* This will never be reached, but required to stop compiler whine */
6697 +match(const struct sk_buff *skb,
6698 + const struct net_device *in,
6699 + const struct net_device *out,
6700 + const void *matchinfo,
6704 + const struct ipt_sctp_info *info;
6705 + sctp_sctphdr_t sh;
6707 + info = (const struct ipt_sctp_info *)matchinfo;
6710 + duprintf("Dropping non-first fragment.. FIXME\n");
6714 + if (skb_copy_bits(skb, skb->nh.iph->ihl*4, &sh, sizeof(sh)) < 0) {
6715 + duprintf("Dropping evil TCP offset=0 tinygram.\n");
6719 + duprintf("spt: %d\tdpt: %d\n", ntohs(sh.source), ntohs(sh.dest));
6721 + return SCCHECK(((ntohs(sh.source) >= info->spts[0])
6722 + && (ntohs(sh.source) <= info->spts[1])),
6723 + IPT_SCTP_SRC_PORTS, info->flags, info->invflags)
6724 + && SCCHECK(((ntohs(sh.dest) >= info->dpts[0])
6725 + && (ntohs(sh.dest) <= info->dpts[1])),
6726 + IPT_SCTP_DEST_PORTS, info->flags, info->invflags)
6727 + && SCCHECK(match_packet(skb, info->chunkmap, info->chunk_match_type,
6728 + info->flag_info, info->flag_count,
6730 + IPT_SCTP_CHUNK_TYPES, info->flags, info->invflags);
6734 +checkentry(const char *tablename,
6735 + const struct ipt_ip *ip,
6737 + unsigned int matchsize,
6738 + unsigned int hook_mask)
6740 + const struct ipt_sctp_info *info;
6742 + info = (const struct ipt_sctp_info *)matchinfo;
6744 + return ip->proto == IPPROTO_SCTP
6745 + && !(ip->invflags & IPT_INV_PROTO)
6746 + && matchsize == IPT_ALIGN(sizeof(struct ipt_sctp_info))
6747 + && !(info->flags & ~IPT_SCTP_VALID_FLAGS)
6748 + && !(info->invflags & ~IPT_SCTP_VALID_FLAGS)
6749 + && !(info->invflags & ~info->flags)
6750 + && ((!(info->flags & IPT_SCTP_CHUNK_TYPES)) ||
6751 + (info->chunk_match_type &
6752 + (SCTP_CHUNK_MATCH_ALL
6753 + | SCTP_CHUNK_MATCH_ANY
6754 + | SCTP_CHUNK_MATCH_ONLY)));
6757 +static struct ipt_match sctp_match =
6759 + .list = { NULL, NULL},
6762 + .checkentry = &checkentry,
6767 +static int __init init(void)
6769 + return ipt_register_match(&sctp_match);
6772 +static void __exit fini(void)
6774 + ipt_unregister_match(&sctp_match);
6780 +MODULE_LICENSE("GPL");
6781 +MODULE_AUTHOR("Kiran Kumar Immidi");
6782 +MODULE_DESCRIPTION("Match for SCTP protocol packets");
6784 diff -Nur linux-2.6.6-rc1.org/net/ipv4/netfilter/ipt_time.c linux-2.6.6-rc1/net/ipv4/netfilter/ipt_time.c
6785 --- linux-2.6.6-rc1.org/net/ipv4/netfilter/ipt_time.c 1970-01-01 01:00:00.000000000 +0100
6786 +++ linux-2.6.6-rc1/net/ipv4/netfilter/ipt_time.c 2004-04-19 10:08:41.000000000 +0200
6789 + This is a module which is used for time matching
6790 + It is using some modified code from dietlibc (localtime() function)
6791 + that you can find at http://www.fefe.de/dietlibc/
6792 + This file is distributed under the terms of the GNU General Public
6793 + License (GPL). Copies of the GPL can be obtained from: ftp://prep.ai.mit.edu/pub/gnu/GPL
6794 + 2001-05-04 Fabrice MARIE <fabrice@netfilter.org> : initial development.
6795 + 2001-21-05 Fabrice MARIE <fabrice@netfilter.org> : bug fix in the match code,
6796 + thanks to "Zeng Yu" <zengy@capitel.com.cn> for bug report.
6797 + 2001-26-09 Fabrice MARIE <fabrice@netfilter.org> : force the match to be in LOCAL_IN or PRE_ROUTING only.
6798 + 2001-30-11 Fabrice : added the possibility to use the match in FORWARD/OUTPUT with a little hack,
6799 + added Nguyen Dang Phuoc Dong <dongnd@tlnet.com.vn> patch to support timezones.
6802 +#include <linux/module.h>
6803 +#include <linux/skbuff.h>
6804 +#include <linux/netfilter_ipv4/ip_tables.h>
6805 +#include <linux/netfilter_ipv4/ipt_time.h>
6806 +#include <linux/time.h>
6808 +MODULE_AUTHOR("Fabrice MARIE <fabrice@netfilter.org>");
6809 +MODULE_DESCRIPTION("Match arrival timestamp");
6810 +MODULE_LICENSE("GPL");
6814 + int tm_sec; /* Seconds. [0-60] (1 leap second) */
6815 + int tm_min; /* Minutes. [0-59] */
6816 + int tm_hour; /* Hours. [0-23] */
6817 + int tm_mday; /* Day. [1-31] */
6818 + int tm_mon; /* Month. [0-11] */
6819 + int tm_year; /* Year - 1900. */
6820 + int tm_wday; /* Day of week. [0-6] */
6821 + int tm_yday; /* Days in year.[0-365] */
6822 + int tm_isdst; /* DST. [-1/0/1]*/
6824 + long int tm_gmtoff; /* we don't care, we count from GMT */
6825 + const char *tm_zone; /* we don't care, we count from GMT */
6829 +localtime(const time_t *timepr, struct tm *r);
6832 +match(const struct sk_buff *skb,
6833 + const struct net_device *in,
6834 + const struct net_device *out,
6835 + const void *matchinfo,
6838 + u_int16_t datalen,
6841 + const struct ipt_time_info *info = matchinfo; /* match info for rule */
6842 + struct tm currenttime; /* time human readable */
6843 + u_int8_t days_of_week[7] = {64, 32, 16, 8, 4, 2, 1};
6844 + u_int16_t packet_time;
6845 + struct timeval kerneltimeval;
6846 + time_t packet_local_time;
6848 + /* if kerneltime=1, we don't read the skb->timestamp but kernel time instead */
6849 + if (info->kerneltime)
6851 + do_gettimeofday(&kerneltimeval);
6852 + packet_local_time = kerneltimeval.tv_sec;
6855 + packet_local_time = skb->stamp.tv_sec;
6857 + /* Transform the timestamp of the packet, in a human readable form */
6858 + localtime(&packet_local_time, ¤ttime);
6860 + /* check if we match this timestamp, we start by the days... */
6861 + if ((days_of_week[currenttime.tm_wday] & info->days_match) != days_of_week[currenttime.tm_wday])
6862 + return 0; /* the day doesn't match */
6864 + /* ... check the time now */
6865 + packet_time = (currenttime.tm_hour * 60) + currenttime.tm_min;
6866 + if ((packet_time < info->time_start) || (packet_time > info->time_stop))
6869 + /* here we match ! */
6874 +checkentry(const char *tablename,
6875 + const struct ipt_ip *ip,
6877 + unsigned int matchsize,
6878 + unsigned int hook_mask)
6880 + struct ipt_time_info *info = matchinfo; /* match info for rule */
6882 + /* First, check that we are in the correct hook */
6883 + /* PRE_ROUTING, LOCAL_IN or FROWARD */
6885 + & ~((1 << NF_IP_PRE_ROUTING) | (1 << NF_IP_LOCAL_IN) | (1 << NF_IP_FORWARD) | (1 << NF_IP_LOCAL_OUT)))
6887 + printk("ipt_time: error, only valid for PRE_ROUTING, LOCAL_IN, FORWARD and OUTPUT)\n");
6890 + /* we use the kerneltime if we are in forward or output */
6891 + info->kerneltime = 1;
6892 + if (hook_mask & ~((1 << NF_IP_FORWARD) | (1 << NF_IP_LOCAL_OUT)))
6893 + /* if not, we use the skb time */
6894 + info->kerneltime = 0;
6896 + /* Check the size */
6897 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_time_info)))
6899 + /* Now check the coherence of the data ... */
6900 + if ((info->time_start > 1439) || /* 23*60+59 = 1439*/
6901 + (info->time_stop > 1439))
6903 + printk(KERN_WARNING "ipt_time: invalid argument\n");
6910 +static struct ipt_match time_match
6911 += { { NULL, NULL }, "time", &match, &checkentry, NULL, THIS_MODULE };
6913 +static int __init init(void)
6915 + printk("ipt_time loading\n");
6916 + return ipt_register_match(&time_match);
6919 +static void __exit fini(void)
6921 + ipt_unregister_match(&time_match);
6922 + printk("ipt_time unloaded\n");
6929 +/* The part below is borowed and modified from dietlibc */
6931 +/* seconds per day */
6932 +#define SPD 24*60*60
6935 +localtime(const time_t *timepr, struct tm *r) {
6938 + extern struct timezone sys_tz;
6939 + const unsigned int __spm[12] =
6946 + (31+28+31+30+31+30),
6947 + (31+28+31+30+31+30+31),
6948 + (31+28+31+30+31+30+31+31),
6949 + (31+28+31+30+31+30+31+31+30),
6950 + (31+28+31+30+31+30+31+31+30+31),
6951 + (31+28+31+30+31+30+31+31+30+31+30),
6953 + register time_t work;
6955 + timep = (*timepr) - (sys_tz.tz_minuteswest * 60);
6957 + r->tm_sec=work%60; work/=60;
6958 + r->tm_min=work%60; r->tm_hour=work/60;
6960 + r->tm_wday=(4+work)%7;
6961 + for (i=1970; ; ++i) {
6962 + register time_t k= (!(i%4) && ((i%100) || !(i%400)))?366:365;
6968 + r->tm_year=i-1900;
6969 + for (i=11; i && __spm[i]>work; --i) ;
6971 + r->tm_mday=work-__spm[i]+1;
6973 diff -Nur linux-2.6.6-rc1.org/net/ipv4/netfilter/ipt_u32.c linux-2.6.6-rc1/net/ipv4/netfilter/ipt_u32.c
6974 --- linux-2.6.6-rc1.org/net/ipv4/netfilter/ipt_u32.c 1970-01-01 01:00:00.000000000 +0100
6975 +++ linux-2.6.6-rc1/net/ipv4/netfilter/ipt_u32.c 2004-04-19 10:08:46.000000000 +0200
6977 +/* Kernel module to match u32 packet content. */
6980 +U32 tests whether quantities of up to 4 bytes extracted from a packet
6981 +have specified values. The specification of what to extract is general
6982 +enough to find data at given offsets from tcp headers or payloads.
6985 + The argument amounts to a program in a small language described below.
6986 + tests := location = value | tests && location = value
6987 + value := range | value , range
6988 + range := number | number : number
6989 + a single number, n, is interpreted the same as n:n
6990 + n:m is interpreted as the range of numbers >=n and <=m
6991 + location := number | location operator number
6992 + operator := & | << | >> | @
6994 + The operators &, <<, >>, && mean the same as in c. The = is really a set
6995 + membership operator and the value syntax describes a set. The @ operator
6996 + is what allows moving to the next header and is described further below.
6998 + *** Until I can find out how to avoid it, there are some artificial limits
6999 + on the size of the tests:
7000 + - no more than 10 ='s (and 9 &&'s) in the u32 argument
7001 + - no more than 10 ranges (and 9 commas) per value
7002 + - no more than 10 numbers (and 9 operators) per location
7004 + To describe the meaning of location, imagine the following machine that
7005 + interprets it. There are three registers:
7006 + A is of type char*, initially the address of the IP header
7007 + B and C are unsigned 32 bit integers, initially zero
7009 + The instructions are:
7010 + number B = number;
7011 + C = (*(A+B)<<24)+(*(A+B+1)<<16)+(*(A+B+2)<<8)+*(A+B+3)
7012 + &number C = C&number
7013 + <<number C = C<<number
7014 + >>number C = C>>number
7015 + @number A = A+C; then do the instruction number
7016 + Any access of memory outside [skb->head,skb->end] causes the match to fail.
7017 + Otherwise the result of the computation is the final value of C.
7019 + Whitespace is allowed but not required in the tests.
7020 + However the characters that do occur there are likely to require
7021 + shell quoting, so it's a good idea to enclose the arguments in quotes.
7024 + match IP packets with total length >= 256
7025 + The IP header contains a total length field in bytes 2-3.
7026 + --u32 "0&0xFFFF=0x100:0xFFFF"
7028 + AND that with FFFF (giving bytes 2-3),
7029 + and test whether that's in the range [0x100:0xFFFF]
7031 +Example: (more realistic, hence more complicated)
7032 + match icmp packets with icmp type 0
7033 + First test that it's an icmp packet, true iff byte 9 (protocol) = 1
7034 + --u32 "6&0xFF=1 && ...
7035 + read bytes 6-9, use & to throw away bytes 6-8 and compare the result to 1
7036 + Next test that it's not a fragment.
7037 + (If so it might be part of such a packet but we can't always tell.)
7038 + n.b. This test is generally needed if you want to match anything
7039 + beyond the IP header.
7040 + The last 6 bits of byte 6 and all of byte 7 are 0 iff this is a complete
7041 + packet (not a fragment). Alternatively, you can allow first fragments
7042 + by only testing the last 5 bits of byte 6.
7043 + ... 4&0x3FFF=0 && ...
7044 + Last test: the first byte past the IP header (the type) is 0
7045 + This is where we have to use the @syntax. The length of the IP header
7046 + (IHL) in 32 bit words is stored in the right half of byte 0 of the
7048 + ... 0>>22&0x3C@0>>24=0"
7049 + The first 0 means read bytes 0-3,
7050 + >>22 means shift that 22 bits to the right. Shifting 24 bits would give
7051 + the first byte, so only 22 bits is four times that plus a few more bits.
7052 + &3C then eliminates the two extra bits on the right and the first four
7053 + bits of the first byte.
7054 + For instance, if IHL=5 then the IP header is 20 (4 x 5) bytes long.
7055 + In this case bytes 0-1 are (in binary) xxxx0101 yyzzzzzz,
7056 + >>22 gives the 10 bit value xxxx0101yy and &3C gives 010100.
7057 + @ means to use this number as a new offset into the packet, and read
7058 + four bytes starting from there. This is the first 4 bytes of the icmp
7059 + payload, of which byte 0 is the icmp type. Therefore we simply shift
7060 + the value 24 to the right to throw out all but the first byte and compare
7061 + the result with 0.
7064 + tcp payload bytes 8-12 is any of 1, 2, 5 or 8
7065 + First we test that the packet is a tcp packet (similar to icmp).
7066 + --u32 "6&0xFF=6 && ...
7067 + Next, test that it's not a fragment (same as above).
7068 + ... 0>>22&0x3C@12>>26&0x3C@8=1,2,5,8"
7069 + 0>>22&3C as above computes the number of bytes in the IP header.
7070 + @ makes this the new offset into the packet, which is the start of the
7071 + tcp header. The length of the tcp header (again in 32 bit words) is
7072 + the left half of byte 12 of the tcp header. The 12>>26&3C
7073 + computes this length in bytes (similar to the IP header before).
7074 + @ makes this the new offset, which is the start of the tcp payload.
7075 + Finally 8 reads bytes 8-12 of the payload and = checks whether the
7076 + result is any of 1, 2, 5 or 8
7079 +#include <linux/module.h>
7080 +#include <linux/skbuff.h>
7082 +#include <linux/netfilter_ipv4/ipt_u32.h>
7083 +#include <linux/netfilter_ipv4/ip_tables.h>
7085 +/* #include <asm-i386/timex.h> for timing */
7087 +MODULE_AUTHOR("Don Cohen <don@isis.cs3-inc.com>");
7088 +MODULE_DESCRIPTION("IP tables u32 matching module");
7089 +MODULE_LICENSE("GPL");
7092 +match(const struct sk_buff *skb,
7093 + const struct net_device *in,
7094 + const struct net_device *out,
7095 + const void *matchinfo,
7098 + u_int16_t datalen,
7101 + const struct ipt_u32 *data = matchinfo;
7103 + unsigned char* origbase = (char*)skb->nh.iph;
7104 + unsigned char* base = origbase;
7105 + unsigned char* head = skb->head;
7106 + unsigned char* end = skb->end;
7108 + u_int32_t pos, val;
7109 + /* unsigned long long cycles1, cycles2, cycles3, cycles4;
7110 + cycles1 = get_cycles(); */
7112 + for (testind=0; testind < data->ntests; testind++) {
7113 + base = origbase; /* reset for each test */
7114 + pos = data->tests[testind].location[0].number;
7115 + if (base+pos+3 > end || base+pos < head)
7117 + val = (base[pos]<<24) + (base[pos+1]<<16) +
7118 + (base[pos+2]<<8) + base[pos+3];
7119 + nnums = data->tests[testind].nnums;
7120 + for (i=1; i < nnums; i++) {
7121 + u_int32_t number = data->tests[testind].location[i].number;
7122 + switch (data->tests[testind].location[i].nextop) {
7124 + val = val & number;
7126 + case IPT_U32_LEFTSH:
7127 + val = val << number;
7129 + case IPT_U32_RIGHTSH:
7130 + val = val >> number;
7133 + base = base + val;
7135 + if (base+pos+3 > end || base+pos < head)
7137 + val = (base[pos]<<24) + (base[pos+1]<<16) +
7138 + (base[pos+2]<<8) + base[pos+3];
7142 + nvals = data->tests[testind].nvalues;
7143 + for (i=0; i < nvals; i++) {
7144 + if ((data->tests[testind].value[i].min <= val) &&
7145 + (val <= data->tests[testind].value[i].max)) {
7149 + if (i >= data->tests[testind].nvalues) {
7150 + /* cycles2 = get_cycles();
7151 + printk("failed %d in %d cycles\n", testind,
7152 + cycles2-cycles1); */
7156 + /* cycles2 = get_cycles();
7157 + printk("succeeded in %d cycles\n", cycles2-cycles1); */
7162 +checkentry(const char *tablename,
7163 + const struct ipt_ip *ip,
7165 + unsigned int matchsize,
7166 + unsigned int hook_mask)
7168 + if (matchsize != IPT_ALIGN(sizeof(struct ipt_u32)))
7173 +static struct ipt_match u32_match
7174 += { { NULL, NULL }, "u32", &match, &checkentry, NULL, THIS_MODULE };
7176 +static int __init init(void)
7178 + return ipt_register_match(&u32_match);
7181 +static void __exit fini(void)
7183 + ipt_unregister_match(&u32_match);
7188 diff -Nur linux-2.6.6-rc1.org/net/ipv6/ip6_tunnel.c linux-2.6.6-rc1/net/ipv6/ip6_tunnel.c
7189 --- linux-2.6.6-rc1.org/net/ipv6/ip6_tunnel.c 2004-04-15 03:33:53.000000000 +0200
7190 +++ linux-2.6.6-rc1/net/ipv6/ip6_tunnel.c 2004-04-19 10:08:24.000000000 +0200
7191 @@ -715,13 +715,7 @@
7192 ipv6h->nexthdr = proto;
7193 ipv6_addr_copy(&ipv6h->saddr, &fl.fl6_src);
7194 ipv6_addr_copy(&ipv6h->daddr, &fl.fl6_dst);
7195 -#ifdef CONFIG_NETFILTER
7196 - nf_conntrack_put(skb->nfct);
7198 -#ifdef CONFIG_NETFILTER_DEBUG
7199 - skb->nf_debug = 0;
7204 err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL,
7205 skb->dst->dev, dst_output);
7206 diff -Nur linux-2.6.6-rc1.org/net/ipv6/netfilter/Kconfig linux-2.6.6-rc1/net/ipv6/netfilter/Kconfig
7207 --- linux-2.6.6-rc1.org/net/ipv6/netfilter/Kconfig 2004-04-19 09:59:33.000000000 +0200
7208 +++ linux-2.6.6-rc1/net/ipv6/netfilter/Kconfig 2004-04-19 10:08:38.000000000 +0200
7209 @@ -230,5 +230,30 @@
7210 <file:Documentation/modules.txt>. If unsure, say `N'.
7213 +config IP6_NF_TARGET_HL
7214 + tristate 'HL target support'
7215 + depends on IP6_NF_MANGLE
7218 +config IP6_NF_TARGET_REJECT
7219 + tristate 'REJECT target support'
7220 + depends on IP6_NF_FILTER
7223 +config IP6_NF_MATCH_FUZZY
7224 + tristate 'Fuzzy match support'
7225 + depends on IP6_NF_FILTER
7228 +config IP6_NF_MATCH_NTH
7229 + tristate 'Nth match support'
7230 + depends on IP6_NF_IPTABLES
7233 +config IP6_NF_MATCH_RANDOM
7234 + tristate 'Random match support'
7235 + depends on IP6_NF_IPTABLES
7240 diff -Nur linux-2.6.6-rc1.org/net/ipv6/netfilter/Makefile linux-2.6.6-rc1/net/ipv6/netfilter/Makefile
7241 --- linux-2.6.6-rc1.org/net/ipv6/netfilter/Makefile 2004-04-19 09:59:33.000000000 +0200
7242 +++ linux-2.6.6-rc1/net/ipv6/netfilter/Makefile 2004-04-19 10:08:38.000000000 +0200
7244 obj-$(CONFIG_IP6_NF_MATCH_MARK) += ip6t_mark.o
7245 obj-$(CONFIG_IP6_NF_MATCH_LENGTH) += ip6t_length.o
7246 obj-$(CONFIG_IP6_NF_MATCH_MAC) += ip6t_mac.o
7247 +obj-$(CONFIG_IP6_NF_MATCH_FUZZY) += ip6t_fuzzy.o
7248 obj-$(CONFIG_IP6_NF_MATCH_RT) += ip6t_rt.o
7249 obj-$(CONFIG_IP6_NF_MATCH_OPTS) += ip6t_hbh.o ip6t_dst.o
7250 obj-$(CONFIG_IP6_NF_MATCH_IPV6HEADER) += ip6t_ipv6header.o
7252 obj-$(CONFIG_IP6_NF_FILTER) += ip6table_filter.o
7253 obj-$(CONFIG_IP6_NF_MANGLE) += ip6table_mangle.o
7254 obj-$(CONFIG_IP6_NF_TARGET_MARK) += ip6t_MARK.o
7255 +obj-$(CONFIG_IP6_NF_TARGET_REJECT) += ip6t_REJECT.o
7256 obj-$(CONFIG_IP6_NF_QUEUE) += ip6_queue.o
7257 obj-$(CONFIG_IP6_NF_TARGET_LOG) += ip6t_LOG.o
7259 +obj-$(CONFIG_IP6_NF_MATCH_RANDOM) += ip6t_random.o
7261 +obj-$(CONFIG_IP6_NF_MATCH_NTH) += ip6t_nth.o
7262 +obj-$(CONFIG_IP6_NF_TARGET_HL) += ip6t_HL.o
7263 obj-$(CONFIG_IP6_NF_RAW) += ip6table_raw.o
7264 obj-$(CONFIG_IP6_NF_MATCH_HL) += ip6t_hl.o
7265 diff -Nur linux-2.6.6-rc1.org/net/ipv6/netfilter/ip6t_HL.c linux-2.6.6-rc1/net/ipv6/netfilter/ip6t_HL.c
7266 --- linux-2.6.6-rc1.org/net/ipv6/netfilter/ip6t_HL.c 1970-01-01 01:00:00.000000000 +0100
7267 +++ linux-2.6.6-rc1/net/ipv6/netfilter/ip6t_HL.c 2004-04-19 10:08:26.000000000 +0200
7270 + * Hop Limit modification target for ip6tables
7271 + * Maciej Soltysiak <solt@dns.toxicfilms.tv>
7272 + * Based on HW's TTL module
7274 + * This software is distributed under the terms of GNU GPL
7277 +#include <linux/module.h>
7278 +#include <linux/skbuff.h>
7279 +#include <linux/ip.h>
7281 +#include <linux/netfilter_ipv6/ip6_tables.h>
7282 +#include <linux/netfilter_ipv6/ip6t_HL.h>
7284 +MODULE_AUTHOR("Maciej Soltysiak <solt@dns.toxicfilms.tv>");
7285 +MODULE_DESCRIPTION("IP tables Hop Limit modification module");
7286 +MODULE_LICENSE("GPL");
7288 +static unsigned int ip6t_hl_target(struct sk_buff **pskb, unsigned int hooknum,
7289 + const struct net_device *in, const struct net_device *out,
7290 + const void *targinfo, void *userinfo)
7292 + struct ipv6hdr *ip6h = (*pskb)->nh.ipv6h;
7293 + const struct ip6t_HL_info *info = targinfo;
7294 + u_int16_t diffs[2];
7297 + switch (info->mode) {
7299 + new_hl = info->hop_limit;
7302 + new_hl = ip6h->hop_limit + info->hop_limit;
7307 + new_hl = ip6h->hop_limit + info->hop_limit;
7312 + new_hl = ip6h->hop_limit;
7316 + if (new_hl != ip6h->hop_limit) {
7317 + diffs[0] = htons(((unsigned)ip6h->hop_limit) << 8) ^ 0xFFFF;
7318 + ip6h->hop_limit = new_hl;
7319 + diffs[1] = htons(((unsigned)ip6h->hop_limit) << 8);
7322 + return IP6T_CONTINUE;
7325 +static int ip6t_hl_checkentry(const char *tablename,
7326 + const struct ip6t_entry *e,
7328 + unsigned int targinfosize,
7329 + unsigned int hook_mask)
7331 + struct ip6t_HL_info *info = targinfo;
7333 + if (targinfosize != IP6T_ALIGN(sizeof(struct ip6t_HL_info))) {
7334 + printk(KERN_WARNING "HL: targinfosize %u != %Zu\n",
7336 + IP6T_ALIGN(sizeof(struct ip6t_HL_info)));
7340 + if (strcmp(tablename, "mangle")) {
7341 + printk(KERN_WARNING "HL: can only be called from \"mangle\" table, not \"%s\"\n", tablename);
7345 + if (info->mode > IP6T_HL_MAXMODE) {
7346 + printk(KERN_WARNING "HL: invalid or unknown Mode %u\n",
7351 + if ((info->mode != IP6T_HL_SET) && (info->hop_limit == 0)) {
7352 + printk(KERN_WARNING "HL: increment/decrement doesn't make sense with value 0\n");
7359 +static struct ip6t_target ip6t_HL = { { NULL, NULL }, "HL",
7360 + ip6t_hl_target, ip6t_hl_checkentry, NULL, THIS_MODULE };
7362 +static int __init init(void)
7364 + return ip6t_register_target(&ip6t_HL);
7367 +static void __exit fini(void)
7369 + ip6t_unregister_target(&ip6t_HL);
7374 diff -Nur linux-2.6.6-rc1.org/net/ipv6/netfilter/ip6t_REJECT.c linux-2.6.6-rc1/net/ipv6/netfilter/ip6t_REJECT.c
7375 --- linux-2.6.6-rc1.org/net/ipv6/netfilter/ip6t_REJECT.c 1970-01-01 01:00:00.000000000 +0100
7376 +++ linux-2.6.6-rc1/net/ipv6/netfilter/ip6t_REJECT.c 2004-04-19 10:08:28.000000000 +0200
7379 + * IP6 tables REJECT target module
7380 + * Linux INET6 implementation
7382 + * Copyright (C)2003 USAGI/WIDE Project
7385 + * Yasuyuki Kozakai <yasuyuki.kozakai@toshiba.co.jp>
7387 + * Based on net/ipv4/netfilter/ipt_REJECT.c
7389 + * This program is free software; you can redistribute it and/or
7390 + * modify it under the terms of the GNU General Public License
7391 + * as published by the Free Software Foundation; either version
7392 + * 2 of the License, or (at your option) any later version.
7395 +#include <linux/config.h>
7396 +#include <linux/module.h>
7397 +#include <linux/skbuff.h>
7398 +#include <linux/icmpv6.h>
7399 +#include <net/ipv6.h>
7400 +#include <net/tcp.h>
7401 +#include <net/icmp.h>
7402 +#include <net/ip6_fib.h>
7403 +#include <net/ip6_route.h>
7404 +#include <net/flow.h>
7405 +#include <linux/netfilter_ipv6/ip6_tables.h>
7406 +#include <linux/netfilter_ipv6/ip6t_REJECT.h>
7408 +MODULE_AUTHOR("Yasuyuki KOZAKAI <yasuyuki.kozakai@toshiba.co.jp>");
7409 +MODULE_DESCRIPTION("IP6 tables REJECT target module");
7410 +MODULE_LICENSE("GPL");
7413 +#define DEBUGP printk
7415 +#define DEBUGP(format, args...)
7419 +static void connection_attach(struct sk_buff *new_skb, struct nf_ct_info *nfct)
7421 + void (*attach)(struct sk_buff *, struct nf_ct_info *);
7422 + if (nfct && (attach = ip6_ct_attach) != NULL) {
7424 + attach(new_skb, nfct);
7429 +static int maybe_reroute(struct sk_buff *skb)
7431 + if (skb->nfcache & NFC_ALTERED){
7432 + if (ip6_route_me_harder(skb) != 0){
7438 + return dst_output(skb);
7441 +/* Send RST reply */
7442 +static void send_reset(struct sk_buff *oldskb)
7444 + struct sk_buff *nskb;
7445 + struct tcphdr otcph, *tcph;
7446 + unsigned int otcplen, tcphoff, hh_len;
7448 + struct ipv6hdr *oip6h = oldskb->nh.ipv6h, *ip6h;
7449 + struct dst_entry *dst = NULL;
7452 + proto = oip6h->nexthdr;
7455 + if ((!(ipv6_addr_type(&oip6h->saddr) & IPV6_ADDR_UNICAST)) ||
7456 + (!(ipv6_addr_type(&oip6h->daddr) & IPV6_ADDR_UNICAST))) {
7457 + DEBUGP("ip6t_REJECT: addr is not unicast.\n");
7461 + tcphoff = ipv6_skip_exthdr(oldskb, ((u8*)(oip6h+1) - oldskb->data),
7462 + &proto, oldskb->len - ((u8*)(oip6h+1)
7465 + if ((tcphoff < 0) || (tcphoff > oldskb->len)) {
7466 + DEBUGP("ip6t_REJECT: Can't get TCP header.\n");
7470 + otcplen = oldskb->len - tcphoff;
7472 + /* IP header checks: fragment, too short. */
7473 + if ((proto != IPPROTO_TCP) || (otcplen < sizeof(struct tcphdr))) {
7474 + DEBUGP("ip6t_REJECT: proto(%d) != IPPROTO_TCP, or too short. otcplen = %d\n",
7479 + if (skb_copy_bits(oldskb, tcphoff, &otcph, sizeof(struct tcphdr))) {
7480 + if (net_ratelimit())
7481 + printk("ip6t_REJECT: Can't copy tcp header\n");
7485 + /* No RST for RST. */
7487 + DEBUGP("ip6t_REJECT: RST is set\n");
7491 + /* Check checksum. */
7492 + if (csum_ipv6_magic(&oip6h->saddr, &oip6h->daddr, otcplen, IPPROTO_TCP,
7493 + skb_checksum(oldskb, tcphoff, otcplen, 0))) {
7494 + DEBUGP("ip6t_REJECT: TCP checksum is invalid\n");
7498 + memset(&fl, 0, sizeof(fl));
7499 + fl.proto = IPPROTO_TCP;
7500 + ipv6_addr_copy(&fl.fl6_src, &oip6h->daddr);
7501 + ipv6_addr_copy(&fl.fl6_dst, &oip6h->saddr);
7502 + fl.fl_ip_sport = otcph.dest;
7503 + fl.fl_ip_dport = otcph.source;
7504 + err = ip6_dst_lookup(NULL, &dst, &fl);
7506 + if (net_ratelimit())
7507 + printk("ip6t_REJECT: can't find dst. err = %d\n", err);
7511 + hh_len = (dst->dev->hard_header_len + 15)&~15;
7512 + nskb = alloc_skb(hh_len + 15 + dst->header_len + sizeof(struct ipv6hdr)
7513 + + sizeof(struct tcphdr) + dst->trailer_len,
7517 + if (net_ratelimit())
7518 + printk("ip6t_REJECT: Can't alloc skb\n");
7526 + skb_reserve(nskb, hh_len + dst->header_len);
7528 + ip6h = nskb->nh.ipv6h = (struct ipv6hdr *)
7529 + skb_put(nskb, sizeof(struct ipv6hdr));
7530 + ip6h->version = 6;
7531 + ip6h->hop_limit = dst_metric(dst, RTAX_HOPLIMIT);
7532 + ip6h->nexthdr = IPPROTO_TCP;
7533 + ip6h->payload_len = htons(sizeof(struct tcphdr));
7534 + ipv6_addr_copy(&ip6h->saddr, &oip6h->daddr);
7535 + ipv6_addr_copy(&ip6h->daddr, &oip6h->saddr);
7537 + tcph = (struct tcphdr *)skb_put(nskb, sizeof(struct tcphdr));
7538 + /* Truncate to length (no data) */
7539 + tcph->doff = sizeof(struct tcphdr)/4;
7540 + tcph->source = otcph.dest;
7541 + tcph->dest = otcph.source;
7545 + tcph->seq = otcph.ack_seq;
7546 + tcph->ack_seq = 0;
7549 + tcph->ack_seq = htonl(ntohl(otcph.seq) + otcph.syn + otcph.fin
7550 + + otcplen - (otcph.doff<<2));
7555 + ((u_int8_t *)tcph)[13] = 0;
7557 + tcph->ack = needs_ack;
7559 + tcph->urg_ptr = 0;
7562 + /* Adjust TCP checksum */
7563 + tcph->check = csum_ipv6_magic(&nskb->nh.ipv6h->saddr,
7564 + &nskb->nh.ipv6h->daddr,
7565 + sizeof(struct tcphdr), IPPROTO_TCP,
7566 + csum_partial((char *)tcph,
7567 + sizeof(struct tcphdr), 0));
7570 + connection_attach(nskb, oldskb->nfct);
7573 + NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, nskb, NULL, nskb->dst->dev,
7579 +static void send_unreach(struct sk_buff *skb_in, unsigned char code)
7581 + struct ipv6hdr *ip6h, *hdr = skb_in->nh.ipv6h;
7582 + struct icmp6hdr *icmp6h;
7583 + struct dst_entry *dst = NULL;
7584 + struct rt6_info *rt;
7587 + unsigned int len, datalen, hh_len;
7588 + int saddr_type, daddr_type;
7589 + unsigned int ptr, ip6off;
7592 + struct sk_buff *nskb;
7595 + saddr_type = ipv6_addr_type(&hdr->saddr);
7596 + daddr_type = ipv6_addr_type(&hdr->daddr);
7598 + if ((!(saddr_type & IPV6_ADDR_UNICAST)) ||
7599 + (!(daddr_type & IPV6_ADDR_UNICAST))) {
7600 + DEBUGP("ip6t_REJECT: addr is not unicast.\n");
7604 + ip6off = skb_in->nh.raw - skb_in->data;
7605 + proto = hdr->nexthdr;
7606 + ptr = ipv6_skip_exthdr(skb_in, ip6off + sizeof(struct ipv6hdr), &proto,
7607 + skb_in->len - ip6off);
7609 + if ((ptr < 0) || (ptr > skb_in->len)) {
7610 + ptr = ip6off + sizeof(struct ipv6hdr);
7611 + proto = hdr->nexthdr;
7612 + } else if (proto == IPPROTO_ICMPV6) {
7615 + if (skb_copy_bits(skb_in, ptr + offsetof(struct icmp6hdr,
7616 + icmp6_type), &type, 1)) {
7617 + DEBUGP("ip6t_REJECT: Can't get ICMPv6 type\n");
7621 + if (!(type & ICMPV6_INFOMSG_MASK)) {
7622 + DEBUGP("ip6t_REJECT: no reply to icmp error\n");
7625 + } else if (proto == IPPROTO_UDP) {
7626 + int plen = skb_in->len - (ptr - ip6off);
7629 + if (plen < sizeof(struct udphdr)) {
7630 + DEBUGP("ip6t_REJECT: too short\n");
7634 + if (skb_copy_bits(skb_in, ptr + offsetof(struct udphdr, check),
7636 + if (net_ratelimit())
7637 + printk("ip6t_REJECT: can't get copy from skb");
7642 + csum_ipv6_magic(&hdr->saddr, &hdr->daddr, plen,
7644 + skb_checksum(skb_in, ptr, plen, 0))) {
7645 + DEBUGP("ip6t_REJECT: UDP checksum is invalid.\n");
7650 + memset(&fl, 0, sizeof(fl));
7651 + fl.proto = IPPROTO_ICMPV6;
7652 + ipv6_addr_copy(&fl.fl6_src, &hdr->daddr);
7653 + ipv6_addr_copy(&fl.fl6_dst, &hdr->saddr);
7654 + fl.fl_icmp_type = ICMPV6_DEST_UNREACH;
7655 + fl.fl_icmp_code = code;
7657 + if (ip6_dst_lookup(NULL, &dst, &fl)) {
7661 + rt = (struct rt6_info *)dst;
7664 + if (rt->rt6i_dst.plen < 128)
7665 + tmo >>= ((128 - rt->rt6i_dst.plen)>>5);
7667 + if (!xrlim_allow(dst, tmo)) {
7668 + if (net_ratelimit())
7669 + printk("ip6t_REJECT: rate limitted\n");
7670 + goto dst_release_out;
7673 + len = skb_in->len + sizeof(struct ipv6hdr) + sizeof(struct icmp6hdr);
7675 + if (len > dst_pmtu(dst))
7676 + len = dst_pmtu(dst);
7677 + if (len > IPV6_MIN_MTU)
7678 + len = IPV6_MIN_MTU;
7680 + datalen = len - sizeof(struct ipv6hdr) - sizeof(struct icmp6hdr);
7681 + hh_len = (rt->u.dst.dev->hard_header_len + 15)&~15;
7683 + nskb = alloc_skb(hh_len + 15 + dst->header_len + dst->trailer_len + len,
7687 + if (net_ratelimit())
7688 + printk("ip6t_REJECT: can't alloc skb\n");
7689 + goto dst_release_out;
7692 + nskb->priority = 0;
7696 + skb_reserve(nskb, hh_len + dst->header_len);
7698 + ip6h = nskb->nh.ipv6h = (struct ipv6hdr *)
7699 + skb_put(nskb, sizeof(struct ipv6hdr));
7700 + ip6h->version = 6;
7701 + ip6h->hop_limit = dst_metric(dst, RTAX_HOPLIMIT);
7702 + ip6h->nexthdr = IPPROTO_ICMPV6;
7703 + ip6h->payload_len = htons(datalen + sizeof(struct icmp6hdr));
7704 + ipv6_addr_copy(&ip6h->saddr, &hdr->daddr);
7705 + ipv6_addr_copy(&ip6h->daddr, &hdr->saddr);
7707 + icmp6h = (struct icmp6hdr *) skb_put(nskb, sizeof(struct icmp6hdr));
7708 + icmp6h->icmp6_type = ICMPV6_DEST_UNREACH;
7709 + icmp6h->icmp6_code = code;
7710 + icmp6h->icmp6_cksum = 0;
7712 + data = skb_put(nskb, datalen);
7714 + csum = csum_partial((unsigned char *)icmp6h, sizeof(struct icmp6hdr), 0);
7715 + csum = skb_copy_and_csum_bits(skb_in, ip6off, data, datalen, csum);
7716 + icmp6h->icmp6_cksum = csum_ipv6_magic(&hdr->saddr, &hdr->daddr,
7717 + datalen + sizeof(struct icmp6hdr),
7718 + IPPROTO_ICMPV6, csum);
7721 + connection_attach(nskb, skb_in->nfct);
7723 + NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, nskb, NULL, nskb->dst->dev,
7730 +static unsigned int reject6_target(struct sk_buff **pskb,
7731 + unsigned int hooknum,
7732 + const struct net_device *in,
7733 + const struct net_device *out,
7734 + const void *targinfo,
7737 + const struct ip6t_reject_info *reject = targinfo;
7739 + DEBUGP(KERN_DEBUG "%s: medium point\n", __FUNCTION__);
7740 + /* WARNING: This code causes reentry within ip6tables.
7741 + This means that the ip6tables jump stack is now crap. We
7742 + must return an absolute verdict. --RR */
7743 + switch (reject->with) {
7744 + case IP6T_ICMP6_NO_ROUTE:
7745 + send_unreach(*pskb, ICMPV6_NOROUTE);
7747 + case IP6T_ICMP6_ADM_PROHIBITED:
7748 + send_unreach(*pskb, ICMPV6_ADM_PROHIBITED);
7750 + case IP6T_ICMP6_NOT_NEIGHBOUR:
7751 + send_unreach(*pskb, ICMPV6_NOT_NEIGHBOUR);
7753 + case IP6T_ICMP6_ADDR_UNREACH:
7754 + send_unreach(*pskb, ICMPV6_ADDR_UNREACH);
7756 + case IP6T_ICMP6_PORT_UNREACH:
7757 + send_unreach(*pskb, ICMPV6_PORT_UNREACH);
7759 + case IP6T_ICMP6_ECHOREPLY:
7762 + case IP6T_TCP_RESET:
7763 + send_reset(*pskb);
7766 + if (net_ratelimit())
7767 + printk(KERN_WARNING "ip6t_REJECT: case %u not handled yet\n", reject->with);
7774 +static int check(const char *tablename,
7775 + const struct ip6t_entry *e,
7777 + unsigned int targinfosize,
7778 + unsigned int hook_mask)
7780 + const struct ip6t_reject_info *rejinfo = targinfo;
7782 + if (targinfosize != IP6T_ALIGN(sizeof(struct ip6t_reject_info))) {
7783 + DEBUGP("ip6t_REJECT: targinfosize %u != 0\n", targinfosize);
7787 + /* Only allow these for packet filtering. */
7788 + if (strcmp(tablename, "filter") != 0) {
7789 + DEBUGP("ip6t_REJECT: bad table `%s'.\n", tablename);
7793 + if ((hook_mask & ~((1 << NF_IP6_LOCAL_IN)
7794 + | (1 << NF_IP6_FORWARD)
7795 + | (1 << NF_IP6_LOCAL_OUT))) != 0) {
7796 + DEBUGP("ip6t_REJECT: bad hook mask %X\n", hook_mask);
7800 + if (rejinfo->with == IP6T_ICMP6_ECHOREPLY) {
7801 + printk("ip6t_REJECT: ECHOREPLY is not supported.\n");
7803 + } else if (rejinfo->with == IP6T_TCP_RESET) {
7804 + /* Must specify that it's a TCP packet */
7805 + if (e->ipv6.proto != IPPROTO_TCP
7806 + || (e->ipv6.invflags & IP6T_INV_PROTO)) {
7807 + DEBUGP("ip6t_REJECT: TCP_RESET illegal for non-tcp\n");
7815 +static struct ip6t_target ip6t_reject_reg = {
7817 + .target = reject6_target,
7818 + .checkentry = check,
7822 +static int __init init(void)
7824 + if (ip6t_register_target(&ip6t_reject_reg))
7829 +static void __exit fini(void)
7831 + ip6t_unregister_target(&ip6t_reject_reg);
7836 diff -Nur linux-2.6.6-rc1.org/net/ipv6/netfilter/ip6t_fuzzy.c linux-2.6.6-rc1/net/ipv6/netfilter/ip6t_fuzzy.c
7837 --- linux-2.6.6-rc1.org/net/ipv6/netfilter/ip6t_fuzzy.c 1970-01-01 01:00:00.000000000 +0100
7838 +++ linux-2.6.6-rc1/net/ipv6/netfilter/ip6t_fuzzy.c 2004-04-19 10:08:31.000000000 +0200
7841 + * This module implements a simple TSK FLC
7842 + * (Takagi-Sugeno-Kang Fuzzy Logic Controller) that aims
7843 + * to limit , in an adaptive and flexible way , the packet rate crossing
7844 + * a given stream . It serves as an initial and very simple (but effective)
7845 + * example of how Fuzzy Logic techniques can be applied to defeat DoS attacks.
7846 + * As a matter of fact , Fuzzy Logic can help us to insert any "behavior"
7847 + * into our code in a precise , adaptive and efficient manner.
7848 + * The goal is very similar to that of "limit" match , but using techniques of
7849 + * Fuzzy Control , that allow us to shape the transfer functions precisely ,
7850 + * avoiding over and undershoots - and stuff like that .
7853 + * 2002-08-10 Hime Aguiar e Oliveira Jr. <hime@engineer.com> : Initial version.
7854 + * 2002-08-17 : Changed to eliminate floating point operations .
7855 + * 2002-08-23 : Coding style changes .
7856 + * 2003-04-08 Maciej Soltysiak <solt@dns.toxicilms.tv> : IPv6 Port
7859 +#include <linux/module.h>
7860 +#include <linux/skbuff.h>
7861 +#include <linux/ipv6.h>
7862 +#include <linux/random.h>
7863 +#include <net/tcp.h>
7864 +#include <linux/spinlock.h>
7865 +#include <linux/netfilter_ipv6/ip6_tables.h>
7866 +#include <linux/netfilter_ipv6/ip6t_fuzzy.h>
7869 + Packet Acceptance Rate - LOW and Packet Acceptance Rate - HIGH
7870 + Expressed in percentage
7873 +#define PAR_LOW 1/100
7876 +static spinlock_t fuzzy_lock = SPIN_LOCK_UNLOCKED;
7878 +MODULE_AUTHOR("Hime Aguiar e Oliveira Junior <hime@engineer.com>");
7879 +MODULE_DESCRIPTION("IP tables Fuzzy Logic Controller match module");
7880 +MODULE_LICENSE("GPL");
7882 +static u_int8_t mf_high(u_int32_t tx,u_int32_t mini,u_int32_t maxi)
7884 + if (tx >= maxi) return 100;
7886 + if (tx <= mini) return 0;
7888 + return ((100 * (tx-mini)) / (maxi-mini));
7891 +static u_int8_t mf_low(u_int32_t tx,u_int32_t mini,u_int32_t maxi)
7893 + if (tx <= mini) return 100;
7895 + if (tx >= maxi) return 0;
7897 + return ((100 * (maxi - tx)) / (maxi - mini));
7902 +ip6t_fuzzy_match(const struct sk_buff *pskb,
7903 + const struct net_device *in,
7904 + const struct net_device *out,
7905 + const void *matchinfo,
7908 + u_int16_t datalen,
7911 + /* From userspace */
7913 + struct ip6t_fuzzy_info *info = (struct ip6t_fuzzy_info *) matchinfo;
7915 + u_int8_t random_number;
7916 + unsigned long amount;
7917 + u_int8_t howhigh, howlow;
7920 + spin_lock_bh(&fuzzy_lock); /* Rise the lock */
7922 + info->bytes_total += pskb->len;
7923 + info->packets_total++;
7925 + info->present_time = jiffies;
7927 + if (info->present_time >= info->previous_time)
7928 + amount = info->present_time - info->previous_time;
7930 + /* There was a transition : I choose to re-sample
7931 + and keep the old acceptance rate...
7935 + info->previous_time = info->present_time;
7936 + info->bytes_total = info->packets_total = 0;
7939 + if ( amount > HZ/10) {/* More than 100 ms elapsed ... */
7941 + info->mean_rate = (u_int32_t) ((HZ * info->packets_total) \
7944 + info->previous_time = info->present_time;
7945 + info->bytes_total = info->packets_total = 0;
7947 + howhigh = mf_high(info->mean_rate,info->minimum_rate,info->maximum_rate);
7948 + howlow = mf_low(info->mean_rate,info->minimum_rate,info->maximum_rate);
7950 + info->acceptance_rate = (u_int8_t) \
7951 + (howhigh * PAR_LOW + PAR_HIGH * howlow);
7953 + /* In fact, the above defuzzification would require a denominator
7954 + * proportional to (howhigh+howlow) but, in this particular case,
7955 + * that expression is constant.
7956 + * An imediate consequence is that it is not necessary to call
7957 + * both mf_high and mf_low - but to keep things understandable,
7963 + spin_unlock_bh(&fuzzy_lock); /* Release the lock */
7966 + if (info->acceptance_rate < 100)
7968 + get_random_bytes((void *)(&random_number), 1);
7970 + /* If within the acceptance , it can pass => don't match */
7971 + if (random_number <= (255 * info->acceptance_rate) / 100)
7974 + return 1; /* It can't pass (It matches) */
7977 + return 0; /* acceptance_rate == 100 % => Everything passes ... */
7982 +ip6t_fuzzy_checkentry(const char *tablename,
7983 + const struct ip6t_ip6 *ip,
7985 + unsigned int matchsize,
7986 + unsigned int hook_mask)
7989 + const struct ip6t_fuzzy_info *info = matchinfo;
7991 + if (matchsize != IP6T_ALIGN(sizeof(struct ip6t_fuzzy_info))) {
7992 + printk("ip6t_fuzzy: matchsize %u != %u\n", matchsize,
7993 + IP6T_ALIGN(sizeof(struct ip6t_fuzzy_info)));
7997 + if ((info->minimum_rate < MINFUZZYRATE) || (info->maximum_rate > MAXFUZZYRATE)
7998 + || (info->minimum_rate >= info->maximum_rate)) {
7999 + printk("ip6t_fuzzy: BAD limits , please verify !!!\n");
8006 +static struct ip6t_match ip6t_fuzzy_reg = {
8010 + ip6t_fuzzy_checkentry,
8014 +static int __init init(void)
8016 + if (ip6t_register_match(&ip6t_fuzzy_reg))
8022 +static void __exit fini(void)
8024 + ip6t_unregister_match(&ip6t_fuzzy_reg);
8029 diff -Nur linux-2.6.6-rc1.org/net/ipv6/netfilter/ip6t_nth.c linux-2.6.6-rc1/net/ipv6/netfilter/ip6t_nth.c
8030 --- linux-2.6.6-rc1.org/net/ipv6/netfilter/ip6t_nth.c 1970-01-01 01:00:00.000000000 +0100
8031 +++ linux-2.6.6-rc1/net/ipv6/netfilter/ip6t_nth.c 2004-04-19 10:08:34.000000000 +0200
8034 + This is a module which is used for match support for every Nth packet
8035 + This file is distributed under the terms of the GNU General Public
8036 + License (GPL). Copies of the GPL can be obtained from:
8037 + ftp://prep.ai.mit.edu/pub/gnu/GPL
8039 + 2001-07-18 Fabrice MARIE <fabrice@netfilter.org> : initial implementation.
8040 + 2001-09-20 Richard Wagner (rwagner@cloudnet.com)
8041 + * added support for multiple counters
8042 + * added support for matching on individual packets
8043 + in the counter cycle
8044 + 2003-04-30 Maciej Soltysiak <solt@dns.toxicfilms.tv> : IPv6 Port
8048 +#include <linux/module.h>
8049 +#include <linux/skbuff.h>
8050 +#include <linux/ip.h>
8051 +#include <net/tcp.h>
8052 +#include <linux/spinlock.h>
8053 +#include <linux/netfilter_ipv6/ip6_tables.h>
8054 +#include <linux/netfilter_ipv6/ip6t_nth.h>
8056 +MODULE_LICENSE("GPL");
8059 + * State information.
8066 +static struct state states[IP6T_NTH_NUM_COUNTERS];
8069 +ip6t_nth_match(const struct sk_buff *pskb,
8070 + const struct net_device *in,
8071 + const struct net_device *out,
8072 + const void *matchinfo,
8075 + u_int16_t datalen,
8078 + /* Parameters from userspace */
8079 + const struct ip6t_nth_info *info = matchinfo;
8080 + unsigned counter = info->counter;
8081 + if((counter < 0) || (counter >= IP6T_NTH_NUM_COUNTERS))
8083 + printk(KERN_WARNING "nth: invalid counter %u. counter between 0 and %u\n", counter, IP6T_NTH_NUM_COUNTERS-1);
8087 + spin_lock(&states[counter].lock);
8089 + /* Are we matching every nth packet?*/
8090 + if (info->packet == 0xFF)
8092 + /* We're matching every nth packet and only every nth packet*/
8093 + /* Do we match or invert match? */
8094 + if (info->not == 0)
8096 + if (states[counter].number == 0)
8098 + ++states[counter].number;
8101 + if (states[counter].number >= info->every)
8102 + states[counter].number = 0; /* reset the counter */
8104 + ++states[counter].number;
8109 + if (states[counter].number == 0)
8111 + ++states[counter].number;
8114 + if (states[counter].number >= info->every)
8115 + states[counter].number = 0;
8117 + ++states[counter].number;
8123 + /* We're using the --packet, so there must be a rule for every value */
8124 + if (states[counter].number == info->packet)
8126 + /* only increment the counter when a match happens */
8127 + if (states[counter].number >= info->every)
8128 + states[counter].number = 0; /* reset the counter */
8130 + ++states[counter].number;
8139 + spin_unlock(&states[counter].lock);
8143 + spin_unlock(&states[counter].lock);
8148 +ip6t_nth_checkentry(const char *tablename,
8149 + const struct ip6t_ip6 *e,
8151 + unsigned int matchsize,
8152 + unsigned int hook_mask)
8154 + /* Parameters from userspace */
8155 + const struct ip6t_nth_info *info = matchinfo;
8156 + unsigned counter = info->counter;
8157 + if((counter < 0) || (counter >= IP6T_NTH_NUM_COUNTERS))
8159 + printk(KERN_WARNING "nth: invalid counter %u. counter between 0 and %u\n", counter, IP6T_NTH_NUM_COUNTERS-1);
8163 + if (matchsize != IP6T_ALIGN(sizeof(struct ip6t_nth_info))) {
8164 + printk("nth: matchsize %u != %u\n", matchsize,
8165 + IP6T_ALIGN(sizeof(struct ip6t_nth_info)));
8169 + states[counter].number = info->startat;
8174 +static struct ip6t_match ip6t_nth_reg = {
8178 + ip6t_nth_checkentry,
8182 +static int __init init(void)
8185 + memset(&states, 0, sizeof(states));
8186 + if (ip6t_register_match(&ip6t_nth_reg))
8189 + for(counter = 0; counter < IP6T_NTH_NUM_COUNTERS; counter++)
8191 + spin_lock_init(&(states[counter].lock));
8194 + printk("ip6t_nth match loaded\n");
8198 +static void __exit fini(void)
8200 + ip6t_unregister_match(&ip6t_nth_reg);
8201 + printk("ip6t_nth match unloaded\n");
8206 diff -Nur linux-2.6.6-rc1.org/net/ipv6/netfilter/ip6t_random.c linux-2.6.6-rc1/net/ipv6/netfilter/ip6t_random.c
8207 --- linux-2.6.6-rc1.org/net/ipv6/netfilter/ip6t_random.c 1970-01-01 01:00:00.000000000 +0100
8208 +++ linux-2.6.6-rc1/net/ipv6/netfilter/ip6t_random.c 2004-04-19 10:08:38.000000000 +0200
8211 + This is a module which is used for a "random" match support.
8212 + This file is distributed under the terms of the GNU General Public
8213 + License (GPL). Copies of the GPL can be obtained from:
8214 + ftp://prep.ai.mit.edu/pub/gnu/GPL
8216 + 2001-10-14 Fabrice MARIE <fabrice@netfilter.org> : initial implementation.
8217 + 2003-04-30 Maciej Soltysiak <solt@dns.toxicfilms.tv> : IPv6 Port
8220 +#include <linux/module.h>
8221 +#include <linux/skbuff.h>
8222 +#include <linux/ip.h>
8223 +#include <linux/random.h>
8224 +#include <net/tcp.h>
8225 +#include <linux/spinlock.h>
8226 +#include <linux/netfilter_ipv6/ip6_tables.h>
8227 +#include <linux/netfilter_ipv6/ip6t_random.h>
8229 +MODULE_LICENSE("GPL");
8232 +ip6t_rand_match(const struct sk_buff *pskb,
8233 + const struct net_device *in,
8234 + const struct net_device *out,
8235 + const void *matchinfo,
8238 + u_int16_t datalen,
8241 + /* Parameters from userspace */
8242 + const struct ip6t_rand_info *info = matchinfo;
8243 + u_int8_t random_number;
8245 + /* get 1 random number from the kernel random number generation routine */
8246 + get_random_bytes((void *)(&random_number), 1);
8248 + /* Do we match ? */
8249 + if (random_number <= info->average)
8256 +ip6t_rand_checkentry(const char *tablename,
8257 + const struct ip6t_ip6 *e,
8259 + unsigned int matchsize,
8260 + unsigned int hook_mask)
8262 + /* Parameters from userspace */
8263 + const struct ip6t_rand_info *info = matchinfo;
8265 + if (matchsize != IP6T_ALIGN(sizeof(struct ip6t_rand_info))) {
8266 + printk("ip6t_random: matchsize %u != %u\n", matchsize,
8267 + IP6T_ALIGN(sizeof(struct ip6t_rand_info)));
8271 + /* must be 1 <= average % <= 99 */
8272 + /* 1 x 2.55 = 2 */
8273 + /* 99 x 2.55 = 252 */
8274 + if ((info->average < 2) || (info->average > 252)) {
8275 + printk("ip6t_random: invalid average %u\n", info->average);
8282 +static struct ip6t_match ip6t_rand_reg = {
8286 + ip6t_rand_checkentry,
8290 +static int __init init(void)
8292 + if (ip6t_register_match(&ip6t_rand_reg))
8295 + printk("ip6t_random match loaded\n");
8299 +static void __exit fini(void)
8301 + ip6t_unregister_match(&ip6t_rand_reg);
8302 + printk("ip6t_random match unloaded\n");
8307 diff -Nur linux-2.6.6-rc1.org/net/ipv6/sit.c linux-2.6.6-rc1/net/ipv6/sit.c
8308 --- linux-2.6.6-rc1.org/net/ipv6/sit.c 2004-04-15 03:35:37.000000000 +0200
8309 +++ linux-2.6.6-rc1/net/ipv6/sit.c 2004-04-19 10:08:24.000000000 +0200
8310 @@ -388,13 +388,7 @@
8311 skb->dev = tunnel->dev;
8312 dst_release(skb->dst);
8314 -#ifdef CONFIG_NETFILTER
8315 - nf_conntrack_put(skb->nfct);
8317 -#ifdef CONFIG_NETFILTER_DEBUG
8318 - skb->nf_debug = 0;
8322 ipip6_ecn_decapsulate(iph, skb);
8324 read_unlock(&ipip6_lock);
8325 @@ -580,13 +574,7 @@
8326 if ((iph->ttl = tiph->ttl) == 0)
8327 iph->ttl = iph6->hop_limit;
8329 -#ifdef CONFIG_NETFILTER
8330 - nf_conntrack_put(skb->nfct);
8332 -#ifdef CONFIG_NETFILTER_DEBUG
8333 - skb->nf_debug = 0;
8339 tunnel->recursion--;