Skip to contentSkip to conversation

connlimit-above early loop termination

PATCH Applied Stefan Berger 2 participants 3 messages Feb 11, 2011 – Feb 13, 2011

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

PATCH 1/1 connlimit-above early loop termination

The patch below introduces an early termination of the loop that is counting matches. It terminates once the counter has exceeded the threshold provided by the user. There's no point in continuing the loop afterwards and looking at other entries.

It plays together with the following code further below:

return (connections > info->limit) ^ info->inverse;

where connections is the result of the counted connection, which in turn is the matches variable in the loop. So once

        -> matches = info->limit + 1    

alias -> matches > info->limit
alias -> matches > threshold

we can terminate the loop.

net/netfilter/xt_connlimit.c +10 -3
index 5c5b6b9..f3ced9c 100644
@@ -97,7 +97,8 @@ static int count_them(struct net *net,
 		      const struct nf_conntrack_tuple *tuple,
 		      const union nf_inet_addr *addr,
 		      const union nf_inet_addr *mask,
-		      u_int8_t family)
+		      u_int8_t family,
+		      unsigned int threshold)
 {
 	const struct nf_conntrack_tuple_hash *found;
 	struct xt_connlimit_conn *conn;
@@ -151,9 +152,14 @@ static int count_them(struct net *net,
 			continue;
 		}
 
-		if (same_source_net(addr, mask, &conn->tuple.src.u3, family))
+		if (same_source_net(addr, mask, &conn->tuple.src.u3, family)) {
 			/* same source network -> be counted! */
 			++matches;
+			if (matches > threshold) {
+				nf_ct_put(found_ct);
+				break;
+			}
+		}
 		nf_ct_put(found_ct);
 	}
 
@@ -201,7 +207,8 @@ connlimit_mt(const struct sk_buff *skb, struct xt_action_param *par)
 
 	spin_lock_bh(&info->data->lock);
 	connections = count_them(net, info->data, tuple_ptr, &addr,
-	                         &info->mask, par->family);
+	                         &info->mask, par->family,
+	                         info->limit);
 	spin_unlock_bh(&info->data->lock);
 
 	if (connections < 0) {
#1 lore raw
Quoted 9 lines · Stefan Berger

The patch below introduces an early termination of the loop that is counting matches. It terminates once the counter has exceeded the threshold provided by the user. There's no point in continuing the loop afterwards and looking at other entries.

It plays together with the following code further below:

return (connections > info->limit) ^ info->inverse;

Quoted 9 lines

where connections is the result of the counted connection, which in turn is the matches variable in the loop. So once

        -> matches = info->limit + 1    

alias -> matches > info->limit
alias -> matches > threshold

we can terminate the loop.

Applied, thanks Stefan.

#2 lore raw
Quoted 13 lines · Patrick McHardy
Quoted 12 lines · Stefan Berger

The patch below introduces an early termination of the loop that is counting matches. It terminates once the counter has exceeded the threshold provided by the user. There's no point in continuing the loop afterwards and looking at other entries.

It plays together with the following code further below:

return (connections> info->limit) ^ info->inverse;

where connections is the result of the counted connection, which in turn is the matches variable in the loop. So once

Quoted 7 lines
Quoted 6 lines
         ->  matches = info->limit + 1
alias   ->  matches>  info->limit
alias   ->  matches>  threshold

we can terminate the loop.

Applied, thanks Stefan.

I am currently creating a derivative of this module for a slightly different purpose. While testing that one and not using the -m state --state -NEW in front of the -m connlimit, I saw that that shortcut doesn't work properly but keeps on adding entries into the list. So, unfortunately I have to withdraw that patch. I apologize and I'll send a patch for this.

Regards,
Stefan