diff -urN --exclude-from=../diff.exclude linux-2.5.74-proc/include/linux/netfilter_ipv4/ip_conntrack.h linux-2.5.74-locking/include/linux/netfilter_ipv4/ip_conntrack.h
--- linux-2.5.74-proc/include/linux/netfilter_ipv4/ip_conntrack.h	2003-07-02 22:56:01.000000000 +0200
+++ linux-2.5.74-locking/include/linux/netfilter_ipv4/ip_conntrack.h	2003-07-17 10:21:59.000000000 +0200
@@ -127,11 +127,11 @@
 	/* expectation list for this master */
 	struct list_head expected_list;
 
-	/* The conntrack of the master connection */
+	/* The conntrack of the master connection: we hold a reference to it */
 	struct ip_conntrack *expectant;
 
 	/* The conntrack of the sibling connection, set after
-	 * expectation arrived */
+	 * expectation arrived: it holds a reference to us */
 	struct ip_conntrack *sibling;
 
 	/* Tuple saved for conntrack */
@@ -166,6 +166,13 @@
 	/* These are my tuples; original and reply */
 	struct ip_conntrack_tuple_hash tuplehash[IP_CT_DIR_MAX];
 
+	/* Hash keys to avoid recalculations */
+#ifdef CONFIG_IP_NF_NAT_NEEDED
+	u_int32_t key[IP_CT_DIR_MAX];
+#else
+	u_int32_t key[IP_CT_DIR_REPLY];
+#endif
+	
 	/* Have we seen traffic both ways yet? (bitset) */
 	unsigned long status;
 
diff -urN --exclude-from=../diff.exclude linux-2.5.74-proc/include/linux/netfilter_ipv4/ip_conntrack_core.h linux-2.5.74-locking/include/linux/netfilter_ipv4/ip_conntrack_core.h
--- linux-2.5.74-proc/include/linux/netfilter_ipv4/ip_conntrack_core.h	2003-07-02 22:58:10.000000000 +0200
+++ linux-2.5.74-locking/include/linux/netfilter_ipv4/ip_conntrack_core.h	2003-07-18 14:31:06.000000000 +0200
@@ -16,8 +16,6 @@
 
 struct ip_conntrack_protocol;
 extern struct ip_conntrack_protocol *ip_ct_find_proto(u_int8_t protocol);
-/* Like above, but you already have conntrack read lock. */
-extern struct ip_conntrack_protocol *__ip_ct_find_proto(u_int8_t protocol);
 extern struct list_head protocol_list;
 
 /* Returns conntrack if it dealt with ICMP, and filled in skb->nfct */
@@ -46,8 +44,94 @@
 	return NF_ACCEPT;
 }
 
-extern struct list_head *ip_conntrack_hash;
+struct ip_conntrack_hash
+{
+	rwlock_t lock;
+	struct list_head list;
+};
+
+extern struct ip_conntrack_hash *ip_conntrack_hash;
 extern struct list_head ip_conntrack_expect_list;
 DECLARE_RWLOCK_EXTERN(ip_conntrack_lock);
+DECLARE_RWLOCK_EXTERN(ip_conntrack_expect_lock);
+
+#ifdef CONFIG_IP_NF_NAT_NEEDED
+static inline void write_lock_key(u_int32_t hash_a, u_int32_t hash_b)
+{
+	if (hash_a < hash_b) {
+		WRITE_LOCK(&ip_conntrack_hash[hash_a].lock);
+		WRITE_LOCK(&ip_conntrack_hash[hash_b].lock);
+	} else if (hash_b < hash_a) {
+		WRITE_LOCK(&ip_conntrack_hash[hash_b].lock);
+		WRITE_LOCK(&ip_conntrack_hash[hash_a].lock);
+	} else
+		WRITE_LOCK(&ip_conntrack_hash[hash_a].lock);
+}
+
+static inline void write_unlock_key(u_int32_t hash_a, u_int32_t hash_b)
+{
+	if (hash_a < hash_b) {
+		WRITE_UNLOCK(&ip_conntrack_hash[hash_b].lock);
+		WRITE_UNLOCK(&ip_conntrack_hash[hash_a].lock);
+	} else if (hash_b < hash_a) {
+		WRITE_UNLOCK(&ip_conntrack_hash[hash_a].lock);
+		WRITE_UNLOCK(&ip_conntrack_hash[hash_b].lock);
+	} else
+		WRITE_UNLOCK(&ip_conntrack_hash[hash_a].lock);
+}
+
+static inline void write_lock_ct(struct ip_conntrack *ct)
+{
+	write_lock_key(ct->key[IP_CT_DIR_ORIGINAL],
+		       ct->key[IP_CT_DIR_REPLY]);
+}
+
+static inline void write_unlock_ct(struct ip_conntrack *ct)
+{
+	write_unlock_key(ct->key[IP_CT_DIR_ORIGINAL],
+		         ct->key[IP_CT_DIR_REPLY]);
+}
+#else
+static inline void write_lock_key(u_int32_t hash_a, u_int32_t hash_b)
+{
+	WRITE_LOCK(&ip_conntrack_hash[hash_a].lock);
+}
+
+static inline void write_unlock_key(u_int32_t hash_a, u_int32_t hash_b)
+{
+	WRITE_UNLOCK(&ip_conntrack_hash[hash_a].lock);
+}
+
+static inline void write_lock_ct(struct ip_conntrack *ct)
+{
+	write_lock_key(ct->key[IP_CT_DIR_ORIGINAL],
+		       ct->key[IP_CT_DIR_ORIGINAL]);
+}
+
+static inline void write_unlock_ct(struct ip_conntrack *ct)
+{
+	write_unlock_key(ct->key[IP_CT_DIR_ORIGINAL],
+		         ct->key[IP_CT_DIR_ORIGINAL]);
+}
+#endif /* CONFIG_IP_NF_NAT_NEEDED */
+static inline void read_lock_key(u_int32_t hash_a)
+{
+	READ_LOCK(&ip_conntrack_hash[hash_a].lock);
+}
+
+static inline void read_unlock_key(u_int32_t hash_a)
+{
+	READ_UNLOCK(&ip_conntrack_hash[hash_a].lock);
+}
+
+static inline void read_lock_ct(const struct ip_conntrack *ct)
+{
+	read_lock_key(ct->key[IP_CT_DIR_ORIGINAL]);
+}
+
+static inline void read_unlock_ct(const struct ip_conntrack *ct)
+{
+	read_unlock_key(ct->key[IP_CT_DIR_ORIGINAL]);
+}
 #endif /* _IP_CONNTRACK_CORE_H */
 
diff -urN --exclude-from=../diff.exclude linux-2.5.74-proc/net/ipv4/netfilter/ip_conntrack_core.c linux-2.5.74-locking/net/ipv4/netfilter/ip_conntrack_core.c
--- linux-2.5.74-proc/net/ipv4/netfilter/ip_conntrack_core.c	2003-07-16 08:09:00.000000000 +0200
+++ linux-2.5.74-locking/net/ipv4/netfilter/ip_conntrack_core.c	2003-07-19 09:52:48.000000000 +0200
@@ -33,8 +33,6 @@
 /* For ERR_PTR().  Yeah, I know... --RR */
 #include <linux/fs.h>
 
-/* This rwlock protects the main hash table, protocol/helper/expected
-   registrations, conntrack timers*/
 #define ASSERT_READ_LOCK(x) MUST_BE_READ_LOCKED(&ip_conntrack_lock)
 #define ASSERT_WRITE_LOCK(x) MUST_BE_WRITE_LOCKED(&ip_conntrack_lock)
 
@@ -52,8 +50,11 @@
 #define DEBUGP(format, args...)
 #endif
 
+/* This lock protects the protocol/helper registrations */
 DECLARE_RWLOCK(ip_conntrack_lock);
-DECLARE_RWLOCK(ip_conntrack_expect_tuple_lock);
+/* This lock protects the global and local expectation lists
+ * and the expectation related data in the conntrack entries */
+DECLARE_RWLOCK(ip_conntrack_expect_lock);
 
 void (*ip_conntrack_destroyed)(struct ip_conntrack *conntrack) = NULL;
 LIST_HEAD(ip_conntrack_expect_list);
@@ -62,38 +63,26 @@
 unsigned int ip_conntrack_htable_size = 0;
 int ip_conntrack_max = 0;
 static atomic_t ip_conntrack_count = ATOMIC_INIT(0);
-struct list_head *ip_conntrack_hash;
+struct ip_conntrack_hash *ip_conntrack_hash;
+static unsigned int ip_conntrack_hash_rnd;
 static kmem_cache_t *ip_conntrack_cachep;
 
 extern struct ip_conntrack_protocol ip_conntrack_generic_protocol;
 
-static inline int proto_cmpfn(const struct ip_conntrack_protocol *curr,
-			      u_int8_t protocol)
-{
-	return protocol == curr->proto;
-}
-
-struct ip_conntrack_protocol *__ip_ct_find_proto(u_int8_t protocol)
-{
-	struct ip_conntrack_protocol *p;
-
-	MUST_BE_READ_LOCKED(&ip_conntrack_lock);
-	p = LIST_FIND(&protocol_list, proto_cmpfn,
-		      struct ip_conntrack_protocol *, protocol);
-	if (!p)
-		p = &ip_conntrack_generic_protocol;
-
-	return p;
-}
-
 struct ip_conntrack_protocol *ip_ct_find_proto(u_int8_t protocol)
 {
 	struct ip_conntrack_protocol *p;
 
-	READ_LOCK(&ip_conntrack_lock);
-	p = __ip_ct_find_proto(protocol);
-	READ_UNLOCK(&ip_conntrack_lock);
-	return p;
+	rcu_read_lock();
+	list_for_each_entry_rcu(p, &protocol_list, list) {
+		if (protocol == p->proto) {
+			rcu_read_unlock();
+			return p;
+		}
+	}
+	rcu_read_unlock();
+	
+	return &ip_conntrack_generic_protocol;
 }
 
 inline void 
@@ -106,19 +95,35 @@
 	nf_conntrack_put(&ct->infos[0]);
 }
 
-static int ip_conntrack_hash_rnd_initted;
-static unsigned int ip_conntrack_hash_rnd;
-
 static u_int32_t
 hash_conntrack(const struct ip_conntrack_tuple *tuple)
 {
-#if 0
-	dump_tuple(tuple);
-#endif
+#ifdef CONFIG_IP_NF_NAT_NEEDED
 	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);
+#else
+	u_int32_t a, b;
+	u_int16_t x, y;
+	
+	if (tuple->src.ip < tuple->dst.ip) {
+		a = tuple->src.ip;
+		b = tuple->dst.ip;
+	} else {
+		b = tuple->src.ip;
+		a = tuple->dst.ip;
+	}
+	if (tuple->src.u.all < tuple->dst.u.all) {
+		x = tuple->src.u.all;
+		y = tuple->dst.u.all;
+	} else {
+		y = tuple->src.u.all;
+		x = tuple->dst.u.all;
+	}
+	return (jhash_3words(a, (b ^ tuple->dst.protonum), (x | (y << 16)),
+	                     ip_conntrack_hash_rnd) % ip_conntrack_htable_size);
+#endif
 }
 
 int
@@ -157,25 +162,20 @@
 
 /* ip_conntrack_expect helper functions */
 
-/* Compare tuple parts depending on mask. */
-static inline int expect_cmp(const struct ip_conntrack_expect *i,
-			     const struct ip_conntrack_tuple *tuple)
-{
-	MUST_BE_READ_LOCKED(&ip_conntrack_expect_tuple_lock);
-	return ip_ct_tuple_mask_cmp(tuple, &i->tuple, &i->mask);
-}
-
 static void
 destroy_expect(struct ip_conntrack_expect *exp)
 {
 	DEBUGP("destroy_expect(%p) use=%d\n", exp, atomic_read(&exp->use));
 	IP_NF_ASSERT(atomic_read(&exp->use));
 	IP_NF_ASSERT(!timer_pending(&exp->timeout));
-
+	MUST_BE_READ_WRITE_UNLOCKED(&ip_conntrack_expect_lock);
+	
+	/* Release refcnt we hold on our creator */
+	ip_conntrack_put(exp->expectant);
+	
 	kfree(exp);
 }
 
-
 inline void ip_conntrack_expect_put(struct ip_conntrack_expect *exp)
 {
 	IP_NF_ASSERT(exp);
@@ -189,10 +189,16 @@
 static inline struct ip_conntrack_expect *
 __ip_ct_expect_find(const struct ip_conntrack_tuple *tuple)
 {
-	MUST_BE_READ_LOCKED(&ip_conntrack_lock);
-	MUST_BE_READ_LOCKED(&ip_conntrack_expect_tuple_lock);
-	return LIST_FIND(&ip_conntrack_expect_list, expect_cmp, 
-			 struct ip_conntrack_expect *, tuple);
+	struct ip_conntrack_expect *exp;
+	MUST_BE_LOCKED(&ip_conntrack_expect_lock);
+	
+	list_for_each_entry(exp, &ip_conntrack_expect_list, list) {
+		/* Compare tuple parts depending on mask. */
+		if (ip_ct_tuple_mask_cmp(tuple, &exp->tuple, &exp->mask))
+			return exp;
+	}
+	
+	return NULL;
 }
 
 /* Find a expectation corresponding to a tuple. */
@@ -201,34 +207,37 @@
 {
 	struct ip_conntrack_expect *exp;
 
-	READ_LOCK(&ip_conntrack_lock);
-	READ_LOCK(&ip_conntrack_expect_tuple_lock);
+	READ_LOCK(&ip_conntrack_expect_lock);
 	exp = __ip_ct_expect_find(tuple);
 	if (exp)
 		atomic_inc(&exp->use);
-	READ_UNLOCK(&ip_conntrack_expect_tuple_lock);
-	READ_UNLOCK(&ip_conntrack_lock);
+	READ_UNLOCK(&ip_conntrack_expect_lock);
 
 	return exp;
 }
 
-/* remove one specific expectation from all lists and drop refcount,
- * does _NOT_ delete the timer. */
+static inline void del_expect_from_lists(struct ip_conntrack_expect *expect)
+{
+	/* delete from global and local lists */
+	list_del(&expect->list);
+	list_del(&expect->expected_list);
+
+	/* decrement expect-count of master conntrack */
+	expect->expectant->expecting--;
+}
+
+/* Remove one specific expectation from all lists and drop refcount.
+ * May be called only when the timer is deleted or off. */
 static void __unexpect_related(struct ip_conntrack_expect *expect)
 {
 	DEBUGP("unexpect_related(%p)\n", expect);
-	MUST_BE_WRITE_LOCKED(&ip_conntrack_lock);
 
 	/* we're not allowed to unexpect a confirmed expectation! */
 	IP_NF_ASSERT(!expect->sibling);
 
-	/* delete from global and local lists */
-	list_del(&expect->list);
-	list_del(&expect->expected_list);
-
-	/* decrement expect-count of master conntrack */
-	if (expect->expectant)
-		expect->expectant->expecting--;
+	WRITE_LOCK(&ip_conntrack_expect_lock);
+	del_expect_from_lists(expect);
+	WRITE_UNLOCK(&ip_conntrack_expect_lock);
 
 	ip_conntrack_expect_put(expect);
 }
@@ -240,59 +249,57 @@
 {
 	IP_NF_ASSERT(expect->expectant);
 	IP_NF_ASSERT(expect->expectant->helper);
-	/* if we are supposed to have a timer, but we can't delete
-	 * it: race condition.  __unexpect_related will
-	 * be calledd by timeout function */
-	if (expect->expectant->helper->timeout
-	    && !del_timer(&expect->timeout))
-		return;
 
-	__unexpect_related(expect);
+	/* Might already be dying */
+	if (del_timer(&expect->timeout))
+		__unexpect_related(expect);
 }
 
 /* delete all unconfirmed expectations for this conntrack */
 static void remove_expectations(struct ip_conntrack *ct)
 {
-	struct list_head *exp_entry, *next;
-	struct ip_conntrack_expect *exp;
+	struct list_head *exp_entry, *tmp;
+	LIST_HEAD(unconfirmed);
 
 	DEBUGP("remove_expectations(%p)\n", ct);
 
-	list_for_each_safe(exp_entry, next, &ct->sibling_list) {
+	WRITE_LOCK(&ip_conntrack_expect_lock);
+	list_for_each_safe(exp_entry, tmp, &ct->sibling_list) {
+		struct ip_conntrack_expect *exp;
 		exp = list_entry(exp_entry, struct ip_conntrack_expect,
 				 expected_list);
 
+		DEBUGP("remove_expectations: found %p of %p, use=%d\n",
+			exp, ct, atomic_read(&exp->use));
 		/* we skip established expectations, as we want to delete
 		 * the un-established ones only */
 		if (exp->sibling) {
-			DEBUGP("remove_expectations: skipping established %p of %p\n", exp->sibling, ct);
-			/* Indicate that this expectations parent is dead */
-			exp->expectant = NULL;
+			DEBUGP("remove_expectations: skipping established %p of %p\n", 
+				exp->sibling, ct);
 			continue;
 		}
 
 		IP_NF_ASSERT(list_inlist(&ip_conntrack_expect_list, exp));
 		IP_NF_ASSERT(exp->expectant == ct);
 
-		/* delete expectation from global and private lists */
-		unexpect_related(exp);
+		/* Might already be dying... */
+		if (del_timer(&exp->timeout)) {
+			del_expect_from_lists(exp);
+			list_add(&exp->expected_list, &unconfirmed);
+		}
+	}
+	WRITE_UNLOCK(&ip_conntrack_expect_lock);
+	
+	/* Now destroy them all, without the need to hold the lock. */
+	while (!list_empty(&unconfirmed)) {
+		struct ip_conntrack_expect *exp;
+		
+		exp = list_entry(unconfirmed.next, struct ip_conntrack_expect,
+				 expected_list);
+		list_del(unconfirmed.next);
+		
+		ip_conntrack_expect_put(exp);
 	}
-}
-
-static void
-clean_from_lists(struct ip_conntrack *ct)
-{
-	DEBUGP("clean_from_lists(%p)\n", ct);
-	MUST_BE_WRITE_LOCKED(&ip_conntrack_lock);
-	LIST_DELETE(&ip_conntrack_hash
-		    [hash_conntrack(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple)],
-		    &ct->tuplehash[IP_CT_DIR_ORIGINAL]);
-	LIST_DELETE(&ip_conntrack_hash
-		    [hash_conntrack(&ct->tuplehash[IP_CT_DIR_REPLY].tuple)],
-		    &ct->tuplehash[IP_CT_DIR_REPLY]);
-
-	/* Destroy all un-established, pending expectations */
-	remove_expectations(ct);
 }
 
 static void
@@ -305,6 +312,28 @@
 	IP_NF_ASSERT(atomic_read(&nfct->use) == 0);
 	IP_NF_ASSERT(!timer_pending(&ct->timeout));
 
+	/* Destroy all un-established, pending expectations */
+	remove_expectations(ct);
+
+	/* If we were expected, delete our master expectation */
+	if (ct->master) {
+		DEBUGP("destroy_conntrack(%p), expected by %p\n", 
+			ct, ct->master);
+		IP_NF_ASSERT(ct->master->sibling);
+		/* Can't call __unexpect_related here,
+		 * since it would screw up the global list */
+		WRITE_LOCK(&ip_conntrack_expect_lock);
+		list_del(&ct->master->expected_list);
+		WRITE_UNLOCK(&ip_conntrack_expect_lock);
+		
+		/* release our refcount */
+		ip_conntrack_expect_put(ct->master);
+		
+		/* delete our master expectation */
+		IP_NF_ASSERT(atomic_read(&ct->master->use) == 1);
+		ip_conntrack_expect_put(ct->master);
+	}
+  
 	/* To make sure we don't get any weird locking issues here:
 	 * destroy_conntrack() MUST NOT be called with a write lock
 	 * to ip_conntrack_lock!!! -HW */
@@ -315,22 +344,6 @@
 	if (ip_conntrack_destroyed)
 		ip_conntrack_destroyed(ct);
 
-	WRITE_LOCK(&ip_conntrack_lock);
-	/* Delete us from our own list to prevent corruption later */
-	list_del(&ct->sibling_list);
-
-	/* Delete our master expectation */
-	if (ct->master) {
-		if (ct->master->expectant) {
-			/* can't call __unexpect_related here,
-			 * since it would screw up expect_list */
-			list_del(&ct->master->expected_list);
-			ip_conntrack_put(ct->master->expectant);
-		}
-		kfree(ct->master);
-	}
-	WRITE_UNLOCK(&ip_conntrack_lock);
-
 	DEBUGP("destroy_conntrack: returning ct=%p to slab\n", ct);
 	kmem_cache_free(ip_conntrack_cachep, ct);
 	atomic_dec(&ip_conntrack_count);
@@ -340,34 +353,37 @@
 {
 	struct ip_conntrack *ct = (void *)ul_conntrack;
 
-	WRITE_LOCK(&ip_conntrack_lock);
-	clean_from_lists(ct);
-	WRITE_UNLOCK(&ip_conntrack_lock);
-	ip_conntrack_put(ct);
-}
+	DEBUGP("death_by_timeout(%p)\n", ct);
 
-static inline int
-conntrack_tuple_cmp(const struct ip_conntrack_tuple_hash *i,
-		    const struct ip_conntrack_tuple *tuple,
-		    const struct ip_conntrack *ignored_conntrack)
-{
-	MUST_BE_READ_LOCKED(&ip_conntrack_lock);
-	return i->ctrack != ignored_conntrack
-		&& ip_ct_tuple_equal(tuple, &i->tuple);
+ 	write_lock_ct(ct);
+	LIST_DELETE(&ip_conntrack_hash
+		    [ct->key[IP_CT_DIR_ORIGINAL]],
+		    &ct->tuplehash[IP_CT_DIR_ORIGINAL]);
+#ifdef CONFIG_IP_NF_NAT_NEEDED
+	LIST_DELETE(&ip_conntrack_hash
+		    [ct->key[IP_CT_DIR_REPLY]],
+		    &ct->tuplehash[IP_CT_DIR_REPLY]);
+#endif
+ 	write_unlock_ct(ct);
+
+	ip_conntrack_put(ct);
 }
 
 static struct ip_conntrack_tuple_hash *
-__ip_conntrack_find(const struct ip_conntrack_tuple *tuple,
+__ip_conntrack_find(u_int32_t key,
+		    const struct ip_conntrack_tuple *tuple,
 		    const struct ip_conntrack *ignored_conntrack)
 {
 	struct ip_conntrack_tuple_hash *h;
 
-	MUST_BE_READ_LOCKED(&ip_conntrack_lock);
-	h = LIST_FIND(&ip_conntrack_hash[hash_conntrack(tuple)],
-		      conntrack_tuple_cmp,
-		      struct ip_conntrack_tuple_hash *,
-		      tuple, ignored_conntrack);
-	return h;
+	MUST_BE_LOCKED(&ip_conntrack_hash[key].lock);
+	list_for_each_entry(h, &ip_conntrack_hash[key].list, list) {
+		if (h->ctrack != ignored_conntrack
+		    && ip_ct_tuple_equal(tuple, &h->tuple))
+		    	return h;
+	}
+	
+	return NULL;
 }
 
 /* Find a connection corresponding to a tuple. */
@@ -376,12 +392,13 @@
 		      const struct ip_conntrack *ignored_conntrack)
 {
 	struct ip_conntrack_tuple_hash *h;
+	u_int32_t key = hash_conntrack(tuple);
 
-	READ_LOCK(&ip_conntrack_lock);
-	h = __ip_conntrack_find(tuple, ignored_conntrack);
+	read_lock_key(key);
+	h = __ip_conntrack_find(key, tuple, ignored_conntrack);
 	if (h)
 		atomic_inc(&h->ctrack->ct_general.use);
-	READ_UNLOCK(&ip_conntrack_lock);
+	read_unlock_key(key);
 
 	return h;
 }
@@ -411,9 +428,9 @@
 int
 __ip_conntrack_confirm(struct nf_ct_info *nfct)
 {
-	unsigned int hash, repl_hash;
 	struct ip_conntrack *ct;
 	enum ip_conntrack_info ctinfo;
+	struct ip_conntrack_tuple_hash *h;
 
 	ct = __ip_conntrack_get(nfct, &ctinfo);
 
@@ -424,9 +441,6 @@
 	if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
 		return NF_ACCEPT;
 
-	hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
-	repl_hash = hash_conntrack(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
-
 	/* We're not in hash table, and we refuse to set up related
 	   connections for unconfirmed conns.  But packet copies and
 	   REJECT will give spurious warnings here. */
@@ -437,34 +451,45 @@
 	IP_NF_ASSERT(!is_confirmed(ct));
 	DEBUGP("Confirming conntrack %p\n", ct);
 
-	WRITE_LOCK(&ip_conntrack_lock);
+	write_lock_ct(ct);
 	/* See if there's one in the list already, including reverse:
            NAT could have grabbed it without realizing, since we're
            not in the hash.  If there is, we lost race. */
-	if (!LIST_FIND(&ip_conntrack_hash[hash],
-		       conntrack_tuple_cmp,
-		       struct ip_conntrack_tuple_hash *,
-		       &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple, NULL)
-	    && !LIST_FIND(&ip_conntrack_hash[repl_hash],
-			  conntrack_tuple_cmp,
-			  struct ip_conntrack_tuple_hash *,
-			  &ct->tuplehash[IP_CT_DIR_REPLY].tuple, NULL)) {
-		list_prepend(&ip_conntrack_hash[hash],
-			     &ct->tuplehash[IP_CT_DIR_ORIGINAL]);
-		list_prepend(&ip_conntrack_hash[repl_hash],
-			     &ct->tuplehash[IP_CT_DIR_REPLY]);
-		/* Timer relative to confirmation time, not original
-		   setting time, otherwise we'd get timer wrap in
-		   weird delay cases. */
-		ct->timeout.expires += jiffies;
-		add_timer(&ct->timeout);
-		atomic_inc(&ct->ct_general.use);
-		set_bit(IPS_CONFIRMED_BIT, &ct->status);
-		WRITE_UNLOCK(&ip_conntrack_lock);
-		return NF_ACCEPT;
-	}
+        list_for_each_entry(h, &ip_conntrack_hash[ct->key[IP_CT_DIR_ORIGINAL]].list, list) {
+        	if (ip_ct_tuple_equal(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
+        			      &h->tuple))
+        		goto already_taken;
+        }
+#ifdef CONFIG_IP_NF_NAT_NEEDED
+        list_for_each_entry(h, &ip_conntrack_hash[ct->key[IP_CT_DIR_REPLY]].list, list) {
+        	if (ip_ct_tuple_equal(&ct->tuplehash[IP_CT_DIR_REPLY].tuple,
+        			      &h->tuple))
+        		goto already_taken;
+        }
+#endif
+	list_prepend(&ip_conntrack_hash[ct->key[IP_CT_DIR_ORIGINAL]].list,
+		     &ct->tuplehash[IP_CT_DIR_ORIGINAL]);
+#ifdef CONFIG_IP_NF_NAT_NEEDED
+	list_prepend(&ip_conntrack_hash[ct->key[IP_CT_DIR_REPLY]].list,
+		     &ct->tuplehash[IP_CT_DIR_REPLY]);
+#endif
+	/* Timer relative to confirmation time, not original
+	   setting time, otherwise we'd get timer wrap in
+	   weird delay cases. */
+	ct->timeout.expires += jiffies;
+	add_timer(&ct->timeout);
+	atomic_inc(&ct->ct_general.use);
+	set_bit(IPS_CONFIRMED_BIT, &ct->status);
+	write_unlock_ct(ct);
 
-	WRITE_UNLOCK(&ip_conntrack_lock);
+	/* Finally, callback to say expectation is done. */
+	if (ct->master && ct->master->expectfn)
+		return ct->master->expectfn(ct);
+
+	return NF_ACCEPT;
+
+already_taken:
+	write_unlock_ct(ct);
 	return NF_DROP;
 }
 
@@ -475,10 +500,11 @@
 			 const struct ip_conntrack *ignored_conntrack)
 {
 	struct ip_conntrack_tuple_hash *h;
+	u_int32_t key = hash_conntrack(tuple);
 
-	READ_LOCK(&ip_conntrack_lock);
-	h = __ip_conntrack_find(tuple, ignored_conntrack);
-	READ_UNLOCK(&ip_conntrack_lock);
+	read_lock_key(key);
+	h = __ip_conntrack_find(key, tuple, ignored_conntrack);
+	read_unlock_key(key);
 
 	return h != NULL;
 }
@@ -560,47 +586,48 @@
 	return h->ctrack;
 }
 
-/* There's a small race here where we may free a just-assured
-   connection.  Too bad: we're in trouble anyway. */
-static inline int unreplied(const struct ip_conntrack_tuple_hash *i)
-{
-	return !(test_bit(IPS_ASSURED_BIT, &i->ctrack->status));
-}
-
-static int early_drop(struct list_head *chain)
+static int early_drop(u_int32_t key)
 {
-	/* Traverse backwards: gives us oldest, which is roughly LRU */
+	struct list_head *i;
 	struct ip_conntrack_tuple_hash *h;
 	int dropped = 0;
 
-	READ_LOCK(&ip_conntrack_lock);
-	h = LIST_FIND_B(chain, unreplied, struct ip_conntrack_tuple_hash *);
-	if (h)
-		atomic_inc(&h->ctrack->ct_general.use);
-	READ_UNLOCK(&ip_conntrack_lock);
-
-	if (!h)
-		return dropped;
-
-	if (del_timer(&h->ctrack->timeout)) {
-		death_by_timeout((unsigned long)h->ctrack);
-		dropped = 1;
-	}
-	ip_conntrack_put(h->ctrack);
+ 	read_lock_key(key);
+ 	/* Traverse backwards: gives us oldest, which is roughly LRU */
+ 	list_for_each_prev(i, &ip_conntrack_hash[key].list) {
+ 		h = list_entry(i, struct ip_conntrack_tuple_hash, list);
+ 		/* There's a small race here where we may free a just-assured
+ 		   connection.  Too bad: we're in trouble anyway. */
+ 		if (!(test_bit(IPS_ASSURED_BIT, &h->ctrack->status))) {
+ 			/* Grab it, unlock, try to kill. */
+ 			atomic_inc(&h->ctrack->ct_general.use);
+ 			read_unlock_key(key);
+ 			if (del_timer(&h->ctrack->timeout)) {
+ 				death_by_timeout((unsigned long)h->ctrack);
+ 				dropped = 1;
+ 			}
+ 			ip_conntrack_put(h->ctrack);
+ 			return dropped;
+ 		}
+ 	}
+ 	read_unlock_key(key);
 	return dropped;
 }
 
-static inline int helper_cmp(const struct ip_conntrack_helper *i,
-			     const struct ip_conntrack_tuple *rtuple)
-{
-	return ip_ct_tuple_mask_cmp(rtuple, &i->tuple, &i->mask);
-}
-
 struct ip_conntrack_helper *ip_ct_find_helper(const struct ip_conntrack_tuple *tuple)
 {
-	return LIST_FIND(&helpers, helper_cmp,
-			 struct ip_conntrack_helper *,
-			 tuple);
+ 	struct ip_conntrack_helper *h;
+ 	
+ 	rcu_read_lock();
+ 	list_for_each_entry_rcu(h, &helpers, list) {
+ 		if (ip_ct_tuple_mask_cmp(tuple, &h->tuple, &h->mask)) {
+ 			rcu_read_unlock();
+ 			return h;
+ 		}
+ 	}
+ 	rcu_read_unlock();
+ 	
+ 	return NULL;
 }
 
 /* Allocate a new conntrack: we return -ENOMEM if classification
@@ -617,11 +644,6 @@
 	int i;
 	static unsigned int drop_next = 0;
 
-	if (!ip_conntrack_hash_rnd_initted) {
-		get_random_bytes(&ip_conntrack_hash_rnd, 4);
-		ip_conntrack_hash_rnd_initted = 1;
-	}
-
 	hash = hash_conntrack(tuple);
 
 	if (ip_conntrack_max &&
@@ -631,8 +653,7 @@
                    bomb one hash chain). */
 		unsigned int next = (drop_next++)%ip_conntrack_htable_size;
 
-		if (!early_drop(&ip_conntrack_hash[next])
-		    && !early_drop(&ip_conntrack_hash[hash])) {
+		if (!early_drop(next) && !early_drop(hash)) {
 			if (net_ratelimit())
 				printk(KERN_WARNING
 				       "ip_conntrack: table full, dropping"
@@ -659,6 +680,10 @@
 	conntrack->tuplehash[IP_CT_DIR_ORIGINAL].ctrack = conntrack;
 	conntrack->tuplehash[IP_CT_DIR_REPLY].tuple = repl_tuple;
 	conntrack->tuplehash[IP_CT_DIR_REPLY].ctrack = conntrack;
+	conntrack->key[IP_CT_DIR_ORIGINAL] = hash;
+#ifdef CONFIG_IP_NF_NAT_NEEDED
+	conntrack->key[IP_CT_DIR_REPLY] = hash_conntrack(&repl_tuple);
+#endif
 	for (i=0; i < IP_CT_NUMBER; i++)
 		conntrack->infos[i].master = &conntrack->ct_general;
 
@@ -673,32 +698,21 @@
 
 	INIT_LIST_HEAD(&conntrack->sibling_list);
 
-	WRITE_LOCK(&ip_conntrack_lock);
-	/* Need finding and deleting of expected ONLY if we win race */
-	READ_LOCK(&ip_conntrack_expect_tuple_lock);
-	expected = LIST_FIND(&ip_conntrack_expect_list, expect_cmp,
-			     struct ip_conntrack_expect *, tuple);
-	READ_UNLOCK(&ip_conntrack_expect_tuple_lock);
+	/* Write lock required for deletion of expected.  Without
+	   this, a read-lock would do. */
+	WRITE_LOCK(&ip_conntrack_expect_lock);
+	expected = __ip_ct_expect_find(tuple);
 
 	/* If master is not in hash table yet (ie. packet hasn't left
 	   this machine yet), how can other end know about expected?
 	   Hence these are not the droids you are looking for (if
 	   master ct never got confirmed, we'd hold a reference to it
-	   and weird things would happen to future packets). */
-	if (expected && !is_confirmed(expected->expectant))
-		expected = NULL;
-
-	/* Look up the conntrack helper for master connections only */
-	if (!expected)
-		conntrack->helper = ip_ct_find_helper(&repl_tuple);
-
-	/* If the expectation is dying, then this is a loser. */
-	if (expected
-	    && expected->expectant->helper->timeout
-	    && ! del_timer(&expected->timeout))
-		expected = NULL;
-
-	if (expected) {
+	   and weird things would happen to future packets).
+	   
+	   Need deleting of expected ONLY if we win race. */
+	if (expected 
+	    && is_confirmed(expected->expectant)
+	    && del_timer(&expected->timeout)) {
 		DEBUGP("conntrack: expectation arrives ct=%p exp=%p\n",
 			conntrack, expected);
 		/* Welcome, Mr. Bond.  We've been expecting you... */
@@ -708,13 +722,15 @@
 		expected->sibling = conntrack;
 		LIST_DELETE(&ip_conntrack_expect_list, expected);
 		expected->expectant->expecting--;
-		nf_conntrack_get(&master_ct(conntrack)->infos[0]);
+		atomic_inc(&expected->use);
 	}
+	WRITE_UNLOCK(&ip_conntrack_expect_lock);
 	atomic_inc(&ip_conntrack_count);
-	WRITE_UNLOCK(&ip_conntrack_lock);
 
-	if (expected && expected->expectfn)
-		expected->expectfn(conntrack);
+	/* Look up the conntrack helper for master connections only */
+	if (!expected)
+		conntrack->helper = ip_ct_find_helper(&repl_tuple);
+
 	return &conntrack->tuplehash[IP_CT_DIR_ORIGINAL];
 }
 
@@ -891,9 +907,7 @@
 
 inline void ip_conntrack_unexpect_related(struct ip_conntrack_expect *expect)
 {
-	WRITE_LOCK(&ip_conntrack_lock);
 	unexpect_related(expect);
-	WRITE_UNLOCK(&ip_conntrack_lock);
 }
 	
 static void expectation_timed_out(unsigned long ul_expect)
@@ -901,56 +915,49 @@
 	struct ip_conntrack_expect *expect = (void *) ul_expect;
 
 	DEBUGP("expectation %p timed out\n", expect);	
-	WRITE_LOCK(&ip_conntrack_lock);
 	__unexpect_related(expect);
-	WRITE_UNLOCK(&ip_conntrack_lock);
 }
 
 /* Add a related connection. */
 int ip_conntrack_expect_related(struct ip_conntrack *related_to,
 				struct ip_conntrack_expect *expect)
 {
-	struct ip_conntrack_expect *old, *new;
-	int ret = 0;
-
-	WRITE_LOCK(&ip_conntrack_lock);
-	/* Because of the write lock, no reader can walk the lists,
-	 * so there is no need to use the tuple lock too */
+	struct ip_conntrack_expect *exp;
 
 	DEBUGP("ip_conntrack_expect_related %p\n", related_to);
 	DEBUGP("tuple: "); DUMP_TUPLE(&expect->tuple);
 	DEBUGP("mask:  "); DUMP_TUPLE(&expect->mask);
 
-	old = LIST_FIND(&ip_conntrack_expect_list, resent_expect,
-		        struct ip_conntrack_expect *, &expect->tuple, 
-			&expect->mask);
-	if (old) {
-		/* Helper private data may contain offsets but no pointers
-		   pointing into the payload - otherwise we should have to copy 
-		   the data filled out by the helper over the old one */
-		DEBUGP("expect_related: resent packet\n");
-		if (related_to->helper->timeout) {
-			if (!del_timer(&old->timeout)) {
-				/* expectation is dying. Fall through */
-				old = NULL;
-			} else {
-				old->timeout.expires = jiffies + 
-					related_to->helper->timeout * HZ;
-				add_timer(&old->timeout);
+	/* We can't really read-lock, search, unlock, call unexpect_related
+	 * and then write lock, because it'd be racy. */
+	WRITE_LOCK(&ip_conntrack_expect_lock);
+	/* Check resent packets */
+	list_for_each_entry(exp, &ip_conntrack_expect_list, list) {
+		if (resent_expect(exp, &expect->tuple, &expect->mask)) {
+			if (exp->sibling) {
+				WRITE_UNLOCK(&ip_conntrack_expect_lock);
+				DEBUGP("expect_related: overy delayed resent packet!\n");
+				return -EBUSY;
 			}
+			goto reuse;
 		}
-
-		if (old) {
-			WRITE_UNLOCK(&ip_conntrack_lock);
-			return -EEXIST;
+	}
+	/* Check expectation clashes */
+	list_for_each_entry(exp, &ip_conntrack_expect_list, list) {
+		if (expect_clash (exp, &expect->tuple, &expect->mask)) {
+			WRITE_UNLOCK(&ip_conntrack_expect_lock);
+			DEBUGP("expect_related: busy!\n");
+			return -EBUSY;
 		}
-	} else if (related_to->helper->max_expected && 
-		   related_to->expecting >= related_to->helper->max_expected) {
+	}
+	exp = NULL;
+	/* Check max number of expectations */
+	if (related_to->helper->max_expected && 
+	    related_to->expecting >= related_to->helper->max_expected) {
 		struct list_head *cur_item;
-		/* old == NULL */
 		if (!(related_to->helper->flags & 
 		      IP_CT_HELPER_F_REUSE_EXPECT)) {
-			WRITE_UNLOCK(&ip_conntrack_lock);
+			WRITE_UNLOCK(&ip_conntrack_expect_lock);
  		    	if (net_ratelimit())
  			    	printk(KERN_WARNING
 				       "ip_conntrack: max number of expected "
@@ -978,62 +985,65 @@
 					 struct ip_conntrack_expect,
 					 expected_list);
 			if (cur->sibling == NULL) {
-				old = cur;
+				exp = cur;
 				break;
 			}
 		}
 
-		/* (!old) cannot happen, since related_to->expecting is the
+		/* (!exp) cannot happen, since related_to->expecting is the
 		 * number of unconfirmed expects */
-		IP_NF_ASSERT(old);
-
-		/* newnat14 does not reuse the real allocated memory
-		 * structures but rather unexpects the old and
-		 * allocates a new.  unexpect_related will decrement
-		 * related_to->expecting. 
-		 */
-		unexpect_related(old);
-		ret = -EPERM;
-	} else if (LIST_FIND(&ip_conntrack_expect_list, expect_clash,
-			     struct ip_conntrack_expect *, &expect->tuple, 
-			     &expect->mask)) {
-		WRITE_UNLOCK(&ip_conntrack_lock);
-		DEBUGP("expect_related: busy!\n");
-		return -EBUSY;
-	}
-	
-	new = (struct ip_conntrack_expect *) 
-	      kmalloc(sizeof(struct ip_conntrack_expect), GFP_ATOMIC);
-	if (!new) {
-		WRITE_UNLOCK(&ip_conntrack_lock);
-		DEBUGP("expect_relaed: OOM allocating expect\n");
-		return -ENOMEM;
+		IP_NF_ASSERT(exp);
+		/* Who wins the race? */
+reuse:		if (del_timer(&exp->timeout)) {
+			del_expect_from_lists(exp);
+			atomic_dec(&related_to->ct_general.use);
+		} else
+			exp = NULL;
+	}
+	if (!exp) {
+		exp = (struct ip_conntrack_expect *) 
+		      kmalloc(sizeof(struct ip_conntrack_expect), GFP_ATOMIC);
+		if (!exp) {
+			WRITE_UNLOCK(&ip_conntrack_expect_lock);
+			DEBUGP("expect_related: OOM allocating expect\n");
+			return -ENOMEM;
+		}
 	}
 	
-	DEBUGP("new expectation %p of conntrack %p\n", new, related_to);
-	memcpy(new, expect, sizeof(*expect));
-	new->expectant = related_to;
-	new->sibling = NULL;
-	atomic_set(&new->use, 1);
-	
+	/* Initialize the data */
+	DEBUGP("new expectation %p of conntrack %p\n", exp, related_to);
+	memcpy(exp, expect, sizeof(*exp));
+	INIT_LIST_HEAD(&exp->list);
+	INIT_LIST_HEAD(&exp->expected_list);
+	atomic_set(&exp->use, 1);
+	exp->expectant = related_to;
+	exp->sibling = NULL;
+	memset(&exp->ct_tuple, 0, sizeof(exp->ct_tuple));
+
+/* Default timeout: one day */
+#define DEFAULT_EXPECTATION_TIMEOUT            1*24*60*60
+
+	init_timer(&exp->timeout);
+	exp->timeout.data = (unsigned long)exp;
+	exp->timeout.function = expectation_timed_out;
+	exp->timeout.expires = jiffies + 
+		(related_to->helper->timeout ? related_to->helper->timeout
+					     : DEFAULT_EXPECTATION_TIMEOUT) * HZ;
+
+	/* increase refcount of master */
+	atomic_inc(&related_to->ct_general.use);
 	/* add to expected list for this connection */	
-	list_add(&new->expected_list, &related_to->sibling_list);
+	list_add(&exp->expected_list, &related_to->sibling_list);
 	/* add to global list of expectations */
-	list_prepend(&ip_conntrack_expect_list, &new->list);
-	/* add and start timer if required */
-	if (related_to->helper->timeout) {
-		init_timer(&new->timeout);
-		new->timeout.data = (unsigned long)new;
-		new->timeout.function = expectation_timed_out;
-		new->timeout.expires = jiffies + 
-					related_to->helper->timeout * HZ;
-		add_timer(&new->timeout);
-	}
-	related_to->expecting++;
+	list_add(&exp->list, &ip_conntrack_expect_list);
 
-	WRITE_UNLOCK(&ip_conntrack_lock);
+	related_to->expecting++;
+	WRITE_UNLOCK(&ip_conntrack_expect_lock);
 
-	return ret;
+	/* Finally start timer */
+	add_timer(&exp->timeout);
+  
+	return 0;
 }
 
 /* Change tuple in an existing expectation */
@@ -1042,29 +1052,31 @@
 {
 	int ret;
 
-	MUST_BE_READ_LOCKED(&ip_conntrack_lock);
-	WRITE_LOCK(&ip_conntrack_expect_tuple_lock);
+	WRITE_LOCK(&ip_conntrack_expect_lock);
 
 	DEBUGP("change_expect:\n");
 	DEBUGP("exp tuple: "); DUMP_TUPLE(&expect->tuple);
 	DEBUGP("exp mask:  "); DUMP_TUPLE(&expect->mask);
 	DEBUGP("newtuple:  "); DUMP_TUPLE(newtuple);
 	if (expect->ct_tuple.dst.protonum == 0) {
+		struct ip_conntrack_expect *exp;
 		/* Never seen before */
-		DEBUGP("change expect: never seen before\n");
-		if (!ip_ct_tuple_equal(&expect->tuple, newtuple) 
-		    && LIST_FIND(&ip_conntrack_expect_list, expect_clash,
-			         struct ip_conntrack_expect *, newtuple, &expect->mask)) {
-			/* Force NAT to find an unused tuple */
-			ret = -1;
-		} else {
-			memcpy(&expect->ct_tuple, &expect->tuple, sizeof(expect->tuple));
-			memcpy(&expect->tuple, newtuple, sizeof(expect->tuple));
-			ret = 0;
+		DEBUGP("change_expect: never seen before\n");
+		if (!ip_ct_tuple_equal(&expect->tuple, newtuple)) {
+			list_for_each_entry(exp, &ip_conntrack_expect_list, list) {
+				if (expect_clash(exp, newtuple, &expect->mask)) {
+					WRITE_UNLOCK(&ip_conntrack_expect_lock);
+					/* Force NAT to find an unused tuple */
+					return -1;
+				}
+			}
 		}
+		memcpy(&expect->ct_tuple, &expect->tuple, sizeof(expect->tuple));
+		memcpy(&expect->tuple, newtuple, sizeof(expect->tuple));
+		ret = 0;
 	} else {
 		/* Resent packet */
-		DEBUGP("change expect: resent packet\n");
+		DEBUGP("change_expect: resent packet\n");
 		if (ip_ct_tuple_equal(&expect->tuple, newtuple)) {
 			ret = 0;
 		} else {
@@ -1072,7 +1084,7 @@
 			ret = -1;
 		}
 	}
-	WRITE_UNLOCK(&ip_conntrack_expect_tuple_lock);
+	WRITE_UNLOCK(&ip_conntrack_expect_lock);
 	
 	return ret;
 }
@@ -1082,9 +1094,8 @@
 int ip_conntrack_alter_reply(struct ip_conntrack *conntrack,
 			     const struct ip_conntrack_tuple *newreply)
 {
-	WRITE_LOCK(&ip_conntrack_lock);
-	if (__ip_conntrack_find(newreply, conntrack)) {
-		WRITE_UNLOCK(&ip_conntrack_lock);
+	if (ip_conntrack_find_get(newreply, conntrack)) {
+		ip_conntrack_put(conntrack);
 		return 0;
 	}
 	/* Should be unconfirmed, so not in hash table yet */
@@ -1094,11 +1105,11 @@
 	DUMP_TUPLE(newreply);
 
 	conntrack->tuplehash[IP_CT_DIR_REPLY].tuple = *newreply;
+#ifdef CONFIG_IP_NF_NAT_NEEDED
+	conntrack->key[IP_CT_DIR_REPLY] = hash_conntrack(newreply);
+#endif
 	if (!conntrack->master)
-		conntrack->helper = LIST_FIND(&helpers, helper_cmp,
-					      struct ip_conntrack_helper *,
-					      newreply);
-	WRITE_UNLOCK(&ip_conntrack_lock);
+		conntrack->helper = ip_ct_find_helper(newreply);
 
 	return 1;
 }
@@ -1106,37 +1117,35 @@
 int ip_conntrack_helper_register(struct ip_conntrack_helper *me)
 {
 	WRITE_LOCK(&ip_conntrack_lock);
-	list_prepend(&helpers, me);
+	list_add_rcu(&me->list, &helpers);
 	WRITE_UNLOCK(&ip_conntrack_lock);
 
 	return 0;
 }
 
-static inline int unhelp(struct ip_conntrack_tuple_hash *i,
-			 const struct ip_conntrack_helper *me)
-{
-	if (i->ctrack->helper == me) {
-		/* Get rid of any expected. */
-		remove_expectations(i->ctrack);
-		/* And *then* set helper to NULL */
-		i->ctrack->helper = NULL;
-	}
-	return 0;
-}
-
 void ip_conntrack_helper_unregister(struct ip_conntrack_helper *me)
 {
 	unsigned int i;
+	struct ip_conntrack_tuple_hash *h;
 
 	/* Need write lock here, to delete helper. */
 	WRITE_LOCK(&ip_conntrack_lock);
-	LIST_DELETE(&helpers, me);
+	list_del_rcu(&me->list);
+	WRITE_UNLOCK(&ip_conntrack_lock);
 
 	/* Get rid of expecteds, set helpers to NULL. */
-	for (i = 0; i < ip_conntrack_htable_size; i++)
-		LIST_FIND_W(&ip_conntrack_hash[i], unhelp,
-			    struct ip_conntrack_tuple_hash *, me);
-	WRITE_UNLOCK(&ip_conntrack_lock);
+	for (i = 0; i < ip_conntrack_htable_size; i++) {
+		read_lock_key(i);
+		list_for_each_entry(h, &ip_conntrack_hash[i].list, list) {
+			if (h->ctrack->helper == me) {
+				/* Get rid of any expected. */
+				remove_expectations(h->ctrack);
+				/* And *then* set helper to NULL */
+				h->ctrack->helper = NULL;
+			}
+		}
+		read_unlock_key(i);
+	}
 
 	/* Someone could be still looking at the helper in a bh. */
 	synchronize_net();
@@ -1147,7 +1156,7 @@
 {
 	IP_NF_ASSERT(ct->timeout.data == (unsigned long)ct);
 
-	WRITE_LOCK(&ip_conntrack_lock);
+	write_lock_ct(ct);
 	/* If not in hash table, timer will not be active yet */
 	if (!is_confirmed(ct))
 		ct->timeout.expires = extra_jiffies;
@@ -1158,7 +1167,7 @@
 			add_timer(&ct->timeout);
 		}
 	}
-	WRITE_UNLOCK(&ip_conntrack_lock);
+	write_unlock_ct(ct);
 }
 
 /* Returns new sk_buff, or NULL */
@@ -1218,32 +1227,27 @@
 	atomic_inc(&ct->ct_general.use);
 }
 
-static inline int
-do_kill(const struct ip_conntrack_tuple_hash *i,
-	int (*kill)(const struct ip_conntrack *i, void *data),
-	void *data)
-{
-	return kill(i->ctrack, data);
-}
-
 /* Bring out ya dead! */
 static struct ip_conntrack_tuple_hash *
 get_next_corpse(int (*kill)(const struct ip_conntrack *i, void *data),
-		void *data)
+		void *data, unsigned int *bucket)
 {
-	struct ip_conntrack_tuple_hash *h = NULL;
-	unsigned int i;
+	struct ip_conntrack_tuple_hash *h;
 
-	READ_LOCK(&ip_conntrack_lock);
-	for (i = 0; !h && i < ip_conntrack_htable_size; i++) {
-		h = LIST_FIND(&ip_conntrack_hash[i], do_kill,
-			      struct ip_conntrack_tuple_hash *, kill, data);
+	for (; *bucket < ip_conntrack_htable_size; (*bucket)++) {
+		read_lock_key(*bucket);
+		list_for_each_entry(h, &ip_conntrack_hash[*bucket].list, list) {
+			if (kill(h->ctrack, data)) {
+				read_unlock_key(*bucket);
+				atomic_inc(&h->ctrack->ct_general.use);
+				
+				return h;
+			}
+		}
+		read_unlock_key(*bucket);
 	}
-	if (h)
-		atomic_inc(&h->ctrack->ct_general.use);
-	READ_UNLOCK(&ip_conntrack_lock);
 
-	return h;
+	return NULL;
 }
 
 void
@@ -1251,9 +1255,9 @@
 			void *data)
 {
 	struct ip_conntrack_tuple_hash *h;
+	unsigned int bucket = 0;
 
-	/* This is order n^2, by the way. */
-	while ((h = get_next_corpse(kill, data)) != NULL) {
+	while ((h = get_next_corpse(kill, data, &bucket)) != NULL) {
 		/* Time to push up daises... */
 		if (del_timer(&h->ctrack->timeout))
 			death_by_timeout((unsigned long)h->ctrack);
@@ -1356,6 +1360,8 @@
 	unsigned int i;
 	int ret;
 
+	get_random_bytes(&ip_conntrack_hash_rnd, 4);
+
 	/* Idea from tcp.c: use 1/16384 of memory.  On i386: 32MB
 	 * machine has 256 buckets.  >= 1GB machines have 8192 buckets. */
  	if (hashsize) {
@@ -1382,7 +1388,7 @@
 		return ret;
 	}
 
-	ip_conntrack_hash = vmalloc(sizeof(struct list_head)
+	ip_conntrack_hash = vmalloc(sizeof(struct ip_conntrack_hash)
 				    * ip_conntrack_htable_size);
 	if (!ip_conntrack_hash) {
 		printk(KERN_ERR "Unable to create ip_conntrack_hash\n");
@@ -1399,13 +1405,15 @@
 	/* Don't NEED lock here, but good form anyway. */
 	WRITE_LOCK(&ip_conntrack_lock);
 	/* Sew in builtin protocols. */
-	list_append(&protocol_list, &ip_conntrack_protocol_tcp);
-	list_append(&protocol_list, &ip_conntrack_protocol_udp);
-	list_append(&protocol_list, &ip_conntrack_protocol_icmp);
+	list_add_tail_rcu(&ip_conntrack_protocol_tcp.list, &protocol_list);
+	list_add_tail_rcu(&ip_conntrack_protocol_udp.list, &protocol_list);
+	list_add_tail_rcu(&ip_conntrack_protocol_icmp.list, &protocol_list);
 	WRITE_UNLOCK(&ip_conntrack_lock);
 
-	for (i = 0; i < ip_conntrack_htable_size; i++)
-		INIT_LIST_HEAD(&ip_conntrack_hash[i]);
+	for (i = 0; i < ip_conntrack_htable_size; i++) {
+		rwlock_init(&ip_conntrack_hash[i].lock);
+		INIT_LIST_HEAD(&ip_conntrack_hash[i].list);
+	}
 
 	/* For use by ipt_REJECT */
 	ip_ct_attach = ip_conntrack_attach;
diff -urN --exclude-from=../diff.exclude linux-2.5.74-proc/net/ipv4/netfilter/ip_conntrack_proto_tcp.c linux-2.5.74-locking/net/ipv4/netfilter/ip_conntrack_proto_tcp.c
--- linux-2.5.74-proc/net/ipv4/netfilter/ip_conntrack_proto_tcp.c	2003-07-16 08:06:55.000000000 +0200
+++ linux-2.5.74-locking/net/ipv4/netfilter/ip_conntrack_proto_tcp.c	2003-07-18 09:02:59.000000000 +0200
@@ -11,6 +11,7 @@
 #include <net/tcp.h>
 
 #include <linux/netfilter_ipv4/ip_conntrack.h>
+#include <linux/netfilter_ipv4/ip_conntrack_core.h>
 #include <linux/netfilter_ipv4/ip_conntrack_protocol.h>
 #include <linux/netfilter_ipv4/lockhelp.h>
 
@@ -20,9 +21,6 @@
 #define DEBUGP(format, args...)
 #endif
 
-/* Protects conntrack->proto.tcp */
-static DECLARE_RWLOCK(tcp_lock);
-
 /* FIXME: Examine ipfilter's timeouts and conntrack transitions more
    closely.  They're more complex. --RR */
 
@@ -143,9 +141,9 @@
 {
 	enum tcp_conntrack state;
 
-	READ_LOCK(&tcp_lock);
+	read_lock_ct(conntrack);
 	state = conntrack->proto.tcp.state;
-	READ_UNLOCK(&tcp_lock);
+	read_unlock_ct(conntrack);
 
 	return sprintf(buffer, "%s ", tcp_conntrack_names[state]);
 }
@@ -170,7 +168,7 @@
 	if (skb_copy_bits(skb, skb->nh.iph->ihl * 4, &tcph, sizeof(tcph)) != 0)
 		return -1;
 
-	WRITE_LOCK(&tcp_lock);
+	write_lock_ct(conntrack);
 	oldtcpstate = conntrack->proto.tcp.state;
 	newconntrack
 		= tcp_conntracks
@@ -182,7 +180,7 @@
 		DEBUGP("ip_conntrack_tcp: Invalid dir=%i index=%u conntrack=%u\n",
 		       CTINFO2DIR(ctinfo), get_conntrack_index(&tcph),
 		       conntrack->proto.tcp.state);
-		WRITE_UNLOCK(&tcp_lock);
+		write_unlock_ct(conntrack);
 		return -1;
 	}
 
@@ -200,7 +198,7 @@
 	   problem case, so we can delete the conntrack
 	   immediately.  --RR */
 	if (!test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status) && tcph.rst) {
-		WRITE_UNLOCK(&tcp_lock);
+		write_unlock_ct(conntrack);
 		if (del_timer(&conntrack->timeout))
 			conntrack->timeout.function((unsigned long)conntrack);
 	} else {
@@ -211,7 +209,7 @@
 		    && tcph.ack_seq == conntrack->proto.tcp.handshake_ack)
 			set_bit(IPS_ASSURED_BIT, &conntrack->status);
 
-		WRITE_UNLOCK(&tcp_lock);
+		write_unlock_ct(conntrack);
 		ip_ct_refresh(conntrack, *tcp_timeouts[newconntrack]);
 	}
 
diff -urN --exclude-from=../diff.exclude linux-2.5.74-proc/net/ipv4/netfilter/ip_conntrack_standalone.c linux-2.5.74-locking/net/ipv4/netfilter/ip_conntrack_standalone.c
--- linux-2.5.74-proc/net/ipv4/netfilter/ip_conntrack_standalone.c	2003-07-16 18:47:20.000000000 +0200
+++ linux-2.5.74-locking/net/ipv4/netfilter/ip_conntrack_standalone.c	2003-07-18 09:04:16.000000000 +0200
@@ -74,7 +74,7 @@
 	len += sprintf(buffer + len, "use=%u proto=%u ",
 		      atomic_read(&expect->use), expect->tuple.dst.protonum);
 	len += print_tuple(buffer + len, &expect->tuple,
-			   __ip_ct_find_proto(expect->tuple.dst.protonum));
+			   ip_ct_find_proto(expect->tuple.dst.protonum));
 	len += sprintf(buffer + len, "\n");
 	return len;
 }
@@ -84,7 +84,7 @@
 {
 	unsigned int len;
 	struct ip_conntrack_protocol *proto
-		= __ip_ct_find_proto(conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
+		= ip_ct_find_proto(conntrack->tuplehash[IP_CT_DIR_ORIGINAL]
 			       .tuple.dst.protonum);
 
 	len = sprintf(buffer, "%-8s %u %lu ",
@@ -121,8 +121,6 @@
 	unsigned int newlen;
 	IP_NF_ASSERT(hash->ctrack);
 
-	MUST_BE_READ_LOCKED(&ip_conntrack_lock);
-
 	/* Only count originals */
 	if (DIRECTION(hash))
 		return 0;
@@ -145,17 +143,22 @@
 	unsigned int len = 0;
 	off_t upto = 0;
 	struct list_head *e;
+	struct ip_conntrack_tuple_hash *h;
 
-	READ_LOCK(&ip_conntrack_lock);
 	/* Traverse hash; print originals then reply. */
 	for (i = 0; i < ip_conntrack_htable_size; i++) {
-		if (LIST_FIND(&ip_conntrack_hash[i], conntrack_iterate,
-			      struct ip_conntrack_tuple_hash *,
-			      buffer, offset, &upto, &len, length))
-			goto finished;
+		read_lock_key(i);
+		list_for_each_entry(h, &ip_conntrack_hash[i].list, list)
+			if (conntrack_iterate(h, buffer, offset, 
+					      &upto, &len, length)) {
+				read_unlock_key(i);
+				goto finished;
+			}
+		read_unlock_key(i);
 	}
 
 	/* Now iterate through expecteds. */
+	READ_LOCK(&ip_conntrack_expect_lock);
 	for (e = ip_conntrack_expect_list.next; 
 	     e != &ip_conntrack_expect_list; e = e->next) {
 		unsigned int last_len;
@@ -167,12 +170,13 @@
 		len += print_expect(buffer + len, expect);
 		if (len > length) {
 			len = last_len;
+			READ_UNLOCK(&ip_conntrack_expect_lock);
 			goto finished;
 		}
 	}
+	READ_UNLOCK(&ip_conntrack_expect_lock);
 
  finished:
-	READ_UNLOCK(&ip_conntrack_lock);
 
 	/* `start' hack - see fs/proc/generic.c line ~165 */
 	*start = (char *)((unsigned int)upto - offset);
@@ -490,18 +494,17 @@
 int ip_conntrack_protocol_register(struct ip_conntrack_protocol *proto)
 {
 	int ret = 0;
-	struct list_head *i;
+	struct ip_conntrack_protocol *i;
 
 	WRITE_LOCK(&ip_conntrack_lock);
-	for (i = protocol_list.next; i != &protocol_list; i = i->next) {
-		if (((struct ip_conntrack_protocol *)i)->proto
-		    == proto->proto) {
+	list_for_each_entry_rcu(i, &protocol_list, list) {
+		if (i->proto == proto->proto) {
 			ret = -EBUSY;
 			goto out;
 		}
 	}
 
-	list_prepend(&protocol_list, proto);
+	list_add_rcu(&proto->list, &protocol_list);
 
  out:
 	WRITE_UNLOCK(&ip_conntrack_lock);
@@ -514,7 +517,7 @@
 
 	/* ip_ct_find_proto() returns proto_generic in case there is no protocol 
 	 * helper. So this should be enough - HW */
-	LIST_DELETE(&protocol_list, proto);
+	list_del_rcu(&proto->list);
 	WRITE_UNLOCK(&ip_conntrack_lock);
 	
 	/* Somebody could be still looking at the proto in bh. */
@@ -555,7 +558,6 @@
 EXPORT_SYMBOL(ip_ct_selective_cleanup);
 EXPORT_SYMBOL(ip_ct_refresh);
 EXPORT_SYMBOL(ip_ct_find_proto);
-EXPORT_SYMBOL(__ip_ct_find_proto);
 EXPORT_SYMBOL(ip_ct_find_helper);
 EXPORT_SYMBOL(ip_conntrack_expect_related);
 EXPORT_SYMBOL(ip_conntrack_change_expect);
@@ -567,6 +569,7 @@
 EXPORT_SYMBOL(ip_conntrack_htable_size);
 EXPORT_SYMBOL(ip_conntrack_expect_list);
 EXPORT_SYMBOL(ip_conntrack_lock);
+EXPORT_SYMBOL(ip_conntrack_expect_lock);
 EXPORT_SYMBOL(ip_conntrack_hash);
 EXPORT_SYMBOL_GPL(ip_conntrack_find_get);
 EXPORT_SYMBOL_GPL(ip_conntrack_put);
diff -urN --exclude-from=../diff.exclude linux-2.5.74-proc/net/ipv4/netfilter/ip_fw_compat_masq.c linux-2.5.74-locking/net/ipv4/netfilter/ip_fw_compat_masq.c
--- linux-2.5.74-proc/net/ipv4/netfilter/ip_fw_compat_masq.c	2003-07-02 22:53:35.000000000 +0200
+++ linux-2.5.74-locking/net/ipv4/netfilter/ip_fw_compat_masq.c	2003-07-18 09:05:16.000000000 +0200
@@ -285,15 +285,17 @@
 		offset = 1;
 	}
 
-	READ_LOCK(&ip_conntrack_lock);
 	/* Traverse hash; print originals then reply. */
 	for (i = 0; i < ip_conntrack_htable_size; i++) {
+		read_lock_key(i);
 		if (LIST_FIND(&ip_conntrack_hash[i], masq_iterate,
 			      struct ip_conntrack_tuple_hash *,
-			      buffer, offset, &upto, &len, length))
+			      buffer, offset, &upto, &len, length)) {
+			read_unlock_key(i);
 			break;
+		}
+		read_unlock_key(i);
 	}
-	READ_UNLOCK(&ip_conntrack_lock);
 
 	/* `start' hack - see fs/proc/generic.c line ~165 */
 	*start = (char *)((unsigned int)upto - offset);
diff -urN --exclude-from=../diff.exclude linux-2.5.74-proc/net/ipv4/netfilter/ip_nat_core.c linux-2.5.74-locking/net/ipv4/netfilter/ip_nat_core.c
--- linux-2.5.74-proc/net/ipv4/netfilter/ip_nat_core.c	2003-07-02 22:41:54.000000000 +0200
+++ linux-2.5.74-locking/net/ipv4/netfilter/ip_nat_core.c	2003-07-17 10:21:59.000000000 +0200
@@ -36,7 +36,7 @@
 #endif
 
 DECLARE_RWLOCK(ip_nat_lock);
-DECLARE_RWLOCK_EXTERN(ip_conntrack_lock);
+DECLARE_RWLOCK_EXTERN(ip_conntrack_expect_lock);
 
 /* Calculated at init based on memory size */
 static unsigned int ip_nat_htable_size;
@@ -742,8 +742,7 @@
 	struct ip_conntrack_protocol *proto;
 	int ret = 1;
 
-	MUST_BE_READ_LOCKED(&ip_conntrack_lock);
-	proto = __ip_ct_find_proto(skb->nh.iph->protocol);
+	proto = ip_ct_find_proto(skb->nh.iph->protocol);
 	if (proto->exp_matches_pkt)
 		ret = proto->exp_matches_pkt(exp, skb);
 
@@ -805,7 +804,7 @@
 			       & htons(IP_MF|IP_OFFSET)));
 
 		/* Have to grab read lock before sibling_list traversal */
-		READ_LOCK(&ip_conntrack_lock);
+		READ_LOCK(&ip_conntrack_expect_lock);
 		list_for_each(cur_item, &ct->sibling_list) { 
 			exp = list_entry(cur_item, struct ip_conntrack_expect, 
 					 expected_list);
@@ -821,7 +820,7 @@
 				ret = helper->help(ct, exp, info, ctinfo, 
 						   hooknum, pskb);
 				if (ret != NF_ACCEPT) {
-					READ_UNLOCK(&ip_conntrack_lock);
+					READ_UNLOCK(&ip_conntrack_expect_lock);
 					return ret;
 				}
 				helper_called = 1;
@@ -834,11 +833,11 @@
 			ret = helper->help(ct, NULL, info, ctinfo, 
 					   hooknum, pskb);
 			if (ret != NF_ACCEPT) {
-				READ_UNLOCK(&ip_conntrack_lock);
+				READ_UNLOCK(&ip_conntrack_expect_lock);
 				return ret;
 			}
 		}
-		READ_UNLOCK(&ip_conntrack_lock);
+		READ_UNLOCK(&ip_conntrack_expect_lock);
 		
 		/* Adjust sequence number only once per packet 
 		 * (helper is called at all hooks) */
