diff -urN --exclude-from=../diff.exclude linux-2.5.74-tcp-window/include/linux/sysctl.h linux-2.5.74-dos/include/linux/sysctl.h
--- linux-2.5.74-tcp-window/include/linux/sysctl.h	2003-07-17 12:07:46.000000000 +0200
+++ linux-2.5.74-dos/include/linux/sysctl.h	2003-07-17 21:47:58.000000000 +0200
@@ -385,6 +385,7 @@
 	NET_IPV4_NF_CONNTRACK_TCP_BE_LIBERAL=18,
 	NET_IPV4_NF_CONNTRACK_TCP_MAX_RETRANS=19,
 	NET_IPV4_NF_CONNTRACK_TCP_MAY_EXCEED_WINDOW=20,
+	NET_IPV4_NF_CONNTRACK_THRESHOLD=21,
 };
  
 /* /proc/sys/net/ipv6 */
diff -urN --exclude-from=../diff.exclude linux-2.5.74-tcp-window/net/ipv4/netfilter/Kconfig linux-2.5.74-dos/net/ipv4/netfilter/Kconfig
--- linux-2.5.74-tcp-window/net/ipv4/netfilter/Kconfig	2003-07-02 22:53:49.000000000 +0200
+++ linux-2.5.74-dos/net/ipv4/netfilter/Kconfig	2003-07-18 12:52:23.000000000 +0200
@@ -20,6 +20,15 @@
 	  If you want to compile it as a module, say M here and read
 	  <file:Documentation/modules.txt>.  If unsure, say `N'.
 
+config IP_NF_CONNTRACK_HASH_MASK
+	bool "Conntrack hash value calculated by shift mask"
+	depends on IP_NF_CONNTRACK
+	help
+	  Conntrack hash value is calculated using modulo arithmetic
+	  by default. Here you can choose the little bit faster mask 
+	  method, with paying the price that the hash bucket size 
+	  then must be a power of two. 
+
 config IP_NF_FTP
 	tristate "FTP protocol support"
 	depends on IP_NF_CONNTRACK
diff -urN --exclude-from=../diff.exclude linux-2.5.74-tcp-window/net/ipv4/netfilter/ip_conntrack_core.c linux-2.5.74-dos/net/ipv4/netfilter/ip_conntrack_core.c
--- linux-2.5.74-tcp-window/net/ipv4/netfilter/ip_conntrack_core.c	2003-07-19 09:53:09.000000000 +0200
+++ linux-2.5.74-dos/net/ipv4/netfilter/ip_conntrack_core.c	2003-07-21 09:17:51.000000000 +0200
@@ -62,6 +62,12 @@
 static LIST_HEAD(helpers);
 unsigned int ip_conntrack_htable_size = 0;
 int ip_conntrack_max = 0;
+/* Threshold (in percent) over which we scan the advisory hotlist
+   to evict unassured connections, assuming a DoS attack. */
+int ip_conntrack_threshold = 90;
+static int hotlist_length = 0;
+static int hotlist_next = 0;
+static u_int32_t *hotlist;
 static atomic_t ip_conntrack_count = ATOMIC_INIT(0);
 struct ip_conntrack_hash *ip_conntrack_hash;
 static unsigned int ip_conntrack_hash_rnd;
@@ -95,6 +101,12 @@
 	nf_conntrack_put(&ct->infos[0]);
 }
 
+#ifdef CONFIG_IP_NF_CONNTRACK_HASH_MASK
+#define HASH_MASK	& (ip_conntrack_htable_size - 1)
+#else
+#define HASH_MASK	% ip_conntrack_htable_size
+#endif
+
 static u_int32_t
 hash_conntrack(const struct ip_conntrack_tuple *tuple)
 {
@@ -102,7 +114,7 @@
 	return (jhash_3words(tuple->src.ip,
 	                     (tuple->dst.ip ^ tuple->dst.protonum),
 	                     (tuple->src.u.all | (tuple->dst.u.all << 16)),
-	                     ip_conntrack_hash_rnd) % ip_conntrack_htable_size);
+	                     ip_conntrack_hash_rnd) HASH_MASK);
 #else
 	u_int32_t a, b;
 	u_int16_t x, y;
@@ -122,7 +134,7 @@
 		x = tuple->dst.u.all;
 	}
 	return (jhash_3words(a, (b ^ tuple->dst.protonum), (x | (y << 16)),
-	                     ip_conntrack_hash_rnd) % ip_conntrack_htable_size);
+	                     ip_conntrack_hash_rnd) HASH_MASK);
 #endif
 }
 
@@ -424,6 +436,8 @@
 	return NULL;
 }
 
+#define	HOTLIST_NEXT(next,curr)	(next = ((curr) + 1) % hotlist_length)
+
 /* Confirm a connection given skb->nfct; places it in hash table */
 int
 __ip_conntrack_confirm(struct nf_ct_info *nfct)
@@ -431,6 +445,7 @@
 	struct ip_conntrack *ct;
 	enum ip_conntrack_info ctinfo;
 	struct ip_conntrack_tuple_hash *h;
+	int hotlist_curr;
 
 	ct = __ip_conntrack_get(nfct, &ctinfo);
 
@@ -480,6 +495,8 @@
 	add_timer(&ct->timeout);
 	atomic_inc(&ct->ct_general.use);
 	set_bit(IPS_CONFIRMED_BIT, &ct->status);
+	HOTLIST_NEXT(hotlist_next, hotlist_curr = hotlist_next);
+	hotlist[hotlist_curr] = ct->key[IP_CT_DIR_ORIGINAL];
 	write_unlock_ct(ct);
 
 	/* Finally, callback to say expectation is done. */
@@ -641,19 +658,22 @@
 	struct ip_conntrack_tuple repl_tuple;
 	size_t hash;
 	struct ip_conntrack_expect *expected;
-	int i;
-	static unsigned int drop_next = 0;
+	int i, j, dropped = 0;
 
 	hash = hash_conntrack(tuple);
 
-	if (ip_conntrack_max &&
-	    atomic_read(&ip_conntrack_count) >= ip_conntrack_max) {
-		/* Try dropping from random chain, or else from the
-                   chain about to put into (in case they're trying to
-                   bomb one hash chain). */
-		unsigned int next = (drop_next++)%ip_conntrack_htable_size;
-
-		if (!early_drop(next) && !early_drop(hash)) {
+	if (ip_conntrack_max 
+	    && atomic_read(&ip_conntrack_count) >= 
+	       (ip_conntrack_max * ip_conntrack_threshold) / 100) {
+		DEBUGP("Conntrack count %u, threshold %u\n",
+			atomic_read(&ip_conntrack_count),
+			(ip_conntrack_max * ip_conntrack_threshold) / 100);
+		/* Try dropping using the advisory hotlist. */
+		for (i = hotlist_next; 
+		     !dropped && HOTLIST_NEXT(j, i) != hotlist_next; 
+		     i = j)
+			dropped = early_drop(hotlist[i]);
+		if (!dropped) { 
 			if (net_ratelimit())
 				printk(KERN_WARNING
 				       "ip_conntrack: table full, dropping"
@@ -1354,6 +1374,9 @@
 
 static int hashsize = 0;
 MODULE_PARM(hashsize, "i");
+MODULE_PARM_DESC(hashsize, "size of the conntrack hash");
+MODULE_PARM(hotlist_length, "i");
+MODULE_PARM_DESC(hotlist_length, "length of the hotlist");
 
 int __init ip_conntrack_init(void)
 {
@@ -1375,13 +1398,35 @@
 		if (ip_conntrack_htable_size < 16)
 			ip_conntrack_htable_size = 16;
 	}
+#ifdef CONFIG_IP_NF_CONNTRACK_HASH_MASK
+	for (i = 4; (1 << i) < ip_conntrack_htable_size; i++)
+		;
+	if (ip_conntrack_htable_size != (1 << i))
+	    	ip_conntrack_htable_size = (1 << (i - 1));
+#endif
 	ip_conntrack_max = 8 * ip_conntrack_htable_size;
 
-	printk("ip_conntrack version %s (%u buckets, %d max)"
+	if (hotlist_length) {
+		if (hotlist_length > ip_conntrack_htable_size)
+			hotlist_length = ip_conntrack_htable_size;
+	} else if (ip_conntrack_htable_size <= 64)
+		hotlist_length = 4;
+	else
+		hotlist_length = (10 * ip_conntrack_htable_size) / 100;
+	
+	printk("ip_conntrack version %s (%u buckets, %d max, hotlist length %u)"
 	       " - %Zd bytes per conntrack\n", IP_CONNTRACK_VERSION,
-	       ip_conntrack_htable_size, ip_conntrack_max,
+	       ip_conntrack_htable_size, ip_conntrack_max, hotlist_length,
 	       sizeof(struct ip_conntrack));
 
+	hotlist = (u_int32_t *) 
+		      kmalloc(sizeof(u_int32_t) * hotlist_length, GFP_ATOMIC);
+	if (!hotlist) {
+		printk(KERN_ERR "Unable to allocat memory to hotlist\n");
+			return -ENOMEM;
+	}
+	memset(hotlist, 0, sizeof(u_int32_t) * hotlist_length);
+	
 	ret = nf_register_sockopt(&so_getorigdst);
 	if (ret != 0) {
 		printk(KERN_ERR "Unable to register netfilter socket option\n");
diff -urN --exclude-from=../diff.exclude linux-2.5.74-tcp-window/net/ipv4/netfilter/ip_conntrack_standalone.c linux-2.5.74-dos/net/ipv4/netfilter/ip_conntrack_standalone.c
--- linux-2.5.74-tcp-window/net/ipv4/netfilter/ip_conntrack_standalone.c	2003-07-18 09:17:47.000000000 +0200
+++ linux-2.5.74-dos/net/ipv4/netfilter/ip_conntrack_standalone.c	2003-07-18 10:04:28.000000000 +0200
@@ -273,6 +273,7 @@
 
 /* From ip_conntrack_core.c */
 extern int ip_conntrack_max;
+extern int ip_conntrack_threshold;
 
 /* From ip_conntrack_proto_tcp.c */
 extern unsigned long ip_ct_tcp_timeout_syn_sent;
@@ -303,6 +304,10 @@
 
 static struct ctl_table_header *ip_ct_sysctl_header;
 
+/* Minimum and maximum thresholds. */
+static int min_threshold = 50;
+static int max_threshold = 100;
+
 static ctl_table ip_ct_sysctl_table[] = {
 	{
 		.ctl_name	= NET_IPV4_NF_CONNTRACK_MAX, 
@@ -445,6 +450,16 @@
 		.mode		= 0644,
 		.proc_handler	&proc_dointvec
 	},
+	{
+		.ctl_name	= NET_IPV4_NF_CONNTRACK_THRESHOLD, 
+		.procname	= "ip_conntrack_threshold",
+	 	.data		= &ip_conntrack_threshold, 
+	 	.maxlen		= sizeof(ip_conntrack_threshold), 
+	 	.mode		= 0644,
+	 	.proc_handler	= &proc_dointvec_minmax,
+	 	.extra1		= &min_threshold,
+	 	.extra2		= &max_threshold,
+	},
 	{	.ctl_name	= 0 }
 };
 
