Skip to contentSkip to conversation

New behaviour for early_drop()

PATCH In review Francesco Sportolari 3 participants 4 messages Apr 14, 2003

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 New behaviour for early_drop()

Hi all,
this is a little patch to modify the behaviour of the early_drop() function; now it uses the ip_conntrack_drop_list, a double-linked list of all the non-assured connections. It costs 2 bytes more for the ip_conntrack struct and a pair of list_append() and list_del() ops, but would have to do better under heavy loads and some type of attacks against the hash chains.

Ciao,

-- Francesco

net/ipv4/netfilter/ip_conntrack_core.c renamed from ../benh-kernel/net/ipv4/netfilter/ip_conntrack_core.c +41 -29
@@ -58,6 +58,7 @@
 
 void (*ip_conntrack_destroyed)(struct ip_conntrack *conntrack) = NULL;
 LIST_HEAD(ip_conntrack_expect_list);
+LIST_HEAD(ip_conntrack_drop_list);
 LIST_HEAD(protocol_list);
 static LIST_HEAD(helpers);
 unsigned int ip_conntrack_htable_size = 0;
@@ -176,7 +177,7 @@
 static void
 destroy_expect(struct ip_conntrack_expect *exp)
 {
-	DEBUGP("destroy_expect(%p) use=%d\n", exp, atomic_read(exp->use));
+	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));
 
@@ -301,7 +302,8 @@
 	LIST_DELETE(&ip_conntrack_hash
 		    [hash_conntrack(&ct->tuplehash[IP_CT_DIR_REPLY].tuple)],
 		    &ct->tuplehash[IP_CT_DIR_REPLY]);
-
+	if (ct->drop_list.next)
+		list_del(&ct->drop_list);
 	/* Destroy all un-established, pending expectations */
 	remove_expectations(ct);
 }
@@ -337,6 +339,8 @@
 		list_del(&ct->master->expected_list);
 		kfree(ct->master);
 	}
+	if (ct->drop_list.next)
+		list_del(&ct->drop_list);
 	WRITE_UNLOCK(&ip_conntrack_lock);
 
 	DEBUGP("destroy_conntrack: returning ct=%p to slab\n", ct);
@@ -581,33 +585,35 @@
 	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 !(i->ctrack->status & IPS_ASSURED);
-}
-
-static int early_drop(struct list_head *chain)
+static int early_drop(void)
 {
 	/* Traverse backwards: gives us oldest, which is roughly LRU */
-	struct ip_conntrack_tuple_hash *h;
+	/* Pop from ip_conntrack_drop_list, which is LRU */
+	struct list_head * entry;
+	struct ip_conntrack * ct;
 	int dropped = 0;
 
-	READ_LOCK(&ip_conntrack_lock);
-	h = LIST_FIND(chain, unreplied, struct ip_conntrack_tuple_hash *);
-	if (h)
-		atomic_inc(&h->ctrack->ct_general.use);
-	READ_UNLOCK(&ip_conntrack_lock);
+	DEBUGP("early_drop(): called.\n");
 
-	if (!h)
+	WRITE_LOCK(&ip_conntrack_lock);
+	entry = list_pop(&ip_conntrack_drop_list);
+	
+	if (entry) { 
+		ct = list_entry(entry, 
+				struct ip_conntrack, 
+				drop_list);
+		atomic_inc(&ct->ct_general.use);
+	}
+	WRITE_UNLOCK(&ip_conntrack_lock);
+
+	if (!entry)
 		return dropped;
 
-	if (del_timer(&h->ctrack->timeout)) {
-		death_by_timeout((unsigned long)h->ctrack);
+	if (del_timer(&ct->timeout)) {
+		death_by_timeout((unsigned long)ct);
 		dropped = 1;
 	}
-	ip_conntrack_put(h->ctrack);
+	ip_conntrack_put(ct);
 	return dropped;
 }
 
@@ -636,23 +642,19 @@
 	size_t hash, repl_hash;
 	struct ip_conntrack_expect *expected;
 	int i;
-	static unsigned int drop_next = 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(&ip_conntrack_hash[next])
-		    && !early_drop(&ip_conntrack_hash[hash])) {
+		/* Drop from ip_conntrack_drop_list if we reach
+		 * ip_conntrack_max */
+		if (!early_drop()) {
 			if (net_ratelimit())
 				printk(KERN_WARNING
 				       "ip_conntrack: table full, dropping"
-				       " packet.\n");
+				       " packet, current connections: %d.\n", 
+					atomic_read(&ip_conntrack_count));
 			return ERR_PTR(-ENOMEM);
 		}
 	}
@@ -855,6 +857,16 @@
 		return NF_ACCEPT;
 	}
 
+	/* not-assured connections LRU list management */
+	WRITE_LOCK(&ip_conntrack_lock);
+	if (!(ct->status & IPS_ASSURED)
+	    && !(ct->drop_list.next))
+		list_append(&ip_conntrack_drop_list,&ct->drop_list);
+	else if ((ct->status & IPS_ASSURED)
+	         && (ct->drop_list.next)) 
+		list_del(&ct->drop_list);
+	WRITE_UNLOCK(&ip_conntrack_lock);
+		
 	if (ret != NF_DROP && ct->helper) {
 		ret = ct->helper->help((*pskb)->nh.iph, (*pskb)->len,
 				       ct, ctinfo);
include/linux/netfilter_ipv4/ip_conntrack.h renamed from ../benh-kernel/include/linux/netfilter_ipv4/ip_conntrack.h +4 -1
@@ -5,6 +5,7 @@
    extension. */
 
 #include <linux/config.h>
+#include <linux/list.h>
 #include <linux/netfilter_ipv4/ip_conntrack_tuple.h>
 #include <asm/atomic.h>
 
@@ -112,7 +113,7 @@
 {
 	/* Internal linked list (global expectation list) */
 	struct list_head list;
-
+	
 	/* reference count */
 	atomic_t use;
 
@@ -181,6 +182,8 @@
            packet has to the conntrack */
 	struct nf_ct_info infos[IP_CT_NUMBER];
 
+	struct list_head drop_list;
+
 	/* Storage reserved for other modules: */
 
 	union ip_conntrack_proto proto;
include/linux/netfilter_ipv4/listhelp.h renamed from ../benh-kernel/include/linux/netfilter_ipv4/listhelp.h +13 -0
@@ -79,6 +79,19 @@
 	list_add(new, head);
 }
 
+/* Pop. */
+static inline void *
+list_pop(struct list_head *head)
+{
+	void * entry;
+	ASSERT_WRITE_LOCK(head);
+	if (head->next == head)
+		return NULL;
+	entry = head->next;
+	list_del(head->next);
+	return entry;
+}
+
 /* Insert according to ordering function; insert before first true. */
 #define LIST_INSERT(head, new, cmpfn)				\
 do {								\
#1 lore raw

Hi Francesco,

Quoted 5 lines

this is a little patch to modify the behaviour of the early_drop() function; now it uses the ip_conntrack_drop_list, a double-linked list of all the non-assured connections. It costs 2 bytes more for the ip_conntrack struct and a pair of list_append() and list_del() ops, but would have to do better under heavy loads and some type of attacks against the hash chains.

I have some comments. First, thanks for taking the effort to implement this idea. Regarding space overhead, it's 8 byte (two 4-byte pointers) for the struct list_head, not 2 byte. By itself, that's still acceptable. However, the additional locking (cross-CPU) will have an impact on high packet load performance. The traditional early_drop does not influence normal (nondropping case) operation in any way, so that's a new, additional serialization. I feel that's reason enough to make the whole thing optional, in some way.

Finally, a stylistic comment, also directed at the other developers here:
you add a list_pop() macro to the netfilter listhelp.h file. I notice that about half the macros in that file are written in BIG_LETTERS(), the rest is small_caps(). Why this variance? I cannot see any system!

best regards
Patrick

#2 lore raw
In reply to Francesco Sportolari
Quoted 6 lines · Francesco Sportolari

Hi all,
this is a little patch to modify the behaviour of the early_drop() function; now it uses the ip_conntrack_drop_list, a double-linked list of all the non-assured connections. It costs 2 bytes more for the ip_conntrack struct and a pair of list_append() and list_del() ops, but would have to do better under heavy loads and some type of attacks against the hash chains.

I've thought about something like this as well... The problem is that this probably makes it easier to attack a certain chain. Earlier we tried to delete an entry fromthe chain we were about to put the new into and if that failed we tried another chain and if that failed we dropped the new connection.

Now we can add the new entry into the chain as long as there's at least one entry that can be deleted in the entire hashtable. Thus we can get a _very_ long chain. But if it's a normal attack these entries will get reaped and their places reused, eventually. But we'll probably get the very long chain before they start getting reused.

Another approach might be to leave it the way it is right now but change it to purge several entries at once if the chain-length is above a certain level.

Signature · 1 line
/Martin
#3 lore raw
Quoted 2 lines · Martin Josefsson

I've thought about something like this as well... The problem is that this probably makes it easier to attack a certain chain.

I don't think that, because if a chain is under attack, and we have not reached the ip_conntrack_max limit, the hash collision will expand the list in each of the two cases (with or without my patch). It's true that my early_drop() free more slots than the previous version, but the attacker, when he's doing something nasty against a precise chain, a lot probably will re-use one of its not-assured connections, keeping the overflow list constantly of the same dimension.

-- Francesco