Skip to contentSkip to conversation

netfilter: nft_connlimit: fix duplicated tracking of a connection

PATCH Superseded Fernando Fernandez Mancera 3 participants 14 messages Oct 27, 2025 – Oct 29, 2025

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 netfilter: nft_connlimit: fix duplicated tracking of a connection

Connlimit expression can be used for all kind of packets and not only for packets with connection state new. See this ruleset as example:

table ip filter {
        chain input {
                type filter hook input priority filter; policy accept;
                tcp dport 22 ct count over 4 counter
        }
}

Currently, if the connection count goes over the limit the counter will count the packets. When a connection is closed, the connection count won't decrement as it should because it is only updated for new connections due to an optimization on __nf_conncount_add() that prevents updating the list if the connection is duplicated.

In addition, since commit d265929930e2 ("netfilter: nf_conncount: reduce unnecessary GC") there can be situations where a duplicated connection is added to the list. This is caused by two packets from the same connection being processed during the same jiffy.

To solve these problems, check whether this is a new connection and only add the connection to the list if that is the case during connlimit evaluation. Otherwise run a GC to update the count. This doesn't yield a performance degradation.

Fixes d265929930e2 ("netfilter: nf_conncount: reduce unnecessary GC")
Fixes 976afca1ceba ("netfilter: nf_conncount: Early exit in nf_conncount_lookup() and cleanup")
---
 net/netfilter/nft_connlimit.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
net/netfilter/nft_connlimit.c +9 -3
index fc35a11cdca2..19c8b5377e35 100644
@@ -43,9 +43,15 @@ static inline void nft_connlimit_do_eval(struct nft_connlimit *priv,
 		return;
 	}
 
-	if (nf_conncount_add(nft_net(pkt), priv->list, tuple_ptr, zone)) {
-		regs->verdict.code = NF_DROP;
-		return;
+	if (ctinfo == IP_CT_NEW) {
+		if (nf_conncount_add(nft_net(pkt), priv->list, tuple_ptr, zone)) {
+			regs->verdict.code = NF_DROP;
+			return;
+		}
+	} else {
+		local_bh_disable();
+		nf_conncount_gc_list(nft_net(pkt), priv->list);
+		local_bh_enable();
 	}
 
 	count = READ_ONCE(priv->list->count);
Signature · 1 line
2.51.0
maintainer
#1 lore raw
Quoted 9 lines · Fernando Fernandez Mancera

Connlimit expression can be used for all kind of packets and not only for packets with connection state new. See this ruleset as example:

table ip filter {
        chain input {
                type filter hook input priority filter; policy accept;
                tcp dport 22 ct count over 4 counter
        }
}

Right. Would you mind sending a patch for nftables documentation to recommend combining this with ct state new or similar?

net/netfilter/nft_connlimit.c static inline void nft_connlimit_do_eval(struct nft_connlimit *priv, · line 54

Quoted 9 lines
net/netfilter/nft_connlimit.c +9 -0
+	if (ctinfo == IP_CT_NEW) {
+		if (nf_conncount_add(nft_net(pkt), priv->list, tuple_ptr, zone)) {
+			regs->verdict.code = NF_DROP;
+			return;
+		}
+	} else {
+		local_bh_disable();
+		nf_conncount_gc_list(nft_net(pkt), priv->list);
+		local_bh_enable();

LGTM, we could make a __nf_conncount_gc_list and make nf_conncount_gc_list not need BH disable (and do the jiffies check first w.o. BH disable) but I think in the interest of a small patch and given that we should not be dealing with !new packets anyway I think this change is fine.

Thanks Fernando for fixing this up.

#2 lore raw
Quoted 11 lines · Florian Westphal
Quoted 9 lines · Fernando Fernandez Mancera

Connlimit expression can be used for all kind of packets and not only for packets with connection state new. See this ruleset as example:

table ip filter {
         chain input {
                 type filter hook input priority filter; policy accept;
                 tcp dport 22 ct count over 4 counter
         }
}
Quoted 3 lines

Right. Would you mind sending a patch for nftables documentation to recommend combining this with ct state new or similar?

Sure, this also requires a documentation fix on the nftables wiki [1] but I believe I do not have an account. How could create one for me? :)

[1] https://wiki.nftables.org/wiki-nftables/index.php/Connlimits

Thanks,
Fernando.

#3 lore raw
In reply to Fernando Fernandez Mancera

net/netfilter/nft_connlimit.c static inline void nft_connlimit_do_eval(struct nft_connlimit *priv, · line 46

Quoted 12 lines · Fernando Fernandez Mancera
net/netfilter/nft_connlimit.c +1 -3
index fc35a11cdca2..19c8b5377e35 100644
@@ -43,9 +43,15 @@ static inline void nft_connlimit_do_eval(struct nft_connlimit *priv,
 		return;
 	}
 
-	if (nf_conncount_add(nft_net(pkt), priv->list, tuple_ptr, zone)) {
-		regs->verdict.code = NF_DROP;
-		return;
+	if (ctinfo == IP_CT_NEW) {

Quick question: Is this good enough to deal with retransmitted packets while in NEW state?

Quoted 15 lines
net/netfilter/nft_connlimit.c +8 -0
+		if (nf_conncount_add(nft_net(pkt), priv->list, tuple_ptr, zone)) {
+			regs->verdict.code = NF_DROP;
+			return;
+		}
+	} else {
+		local_bh_disable();
+		nf_conncount_gc_list(nft_net(pkt), priv->list);
+		local_bh_enable();
 	}
 
 	count = READ_ONCE(priv->list->count);
Signature · 1 line
2.51.0
maintainer
#4 lore raw
Quoted 7 lines · Pablo Neira Ayuso

net/netfilter/nft_connlimit.c static inline void nft_connlimit_do_eval(struct nft_connlimit *priv, · line 46

Quoted 4 lines
net/netfilter/nft_connlimit.c +1 -3
-	if (nf_conncount_add(nft_net(pkt), priv->list, tuple_ptr, zone)) {
-		regs->verdict.code = NF_DROP;
-		return;
+	if (ctinfo == IP_CT_NEW) {

Quick question: Is this good enough to deal with retransmitted packets while in NEW state?

Good point, I did not consider retransmit case.
What about if (!nf_ct_is_confirmed(ct)) { ..?

Would need a small comment that this is there instead of NEW check due to rexmit.

Quoted 9 lines

net/netfilter/nft_connlimit.c static inline void nft_connlimit_do_eval(struct nft_connlimit *priv, · line 55

Quoted 9 lines
net/netfilter/nft_connlimit.c +8 -0
+		if (nf_conncount_add(nft_net(pkt), priv->list, tuple_ptr, zone)) {
+			regs->verdict.code = NF_DROP;
+			return;
+		}
+	} else {
+		local_bh_disable();
+		nf_conncount_gc_list(nft_net(pkt), priv->list);
+		local_bh_enable();
 	}

As this needs a respin anyway, what about removing the else clause altogether? Resp. what was the rationale for doing a gc call here?

And, do we want same check in xt_connlimit?

#5 lore raw
Quoted 15 lines · Florian Westphal
Quoted 7 lines · Pablo Neira Ayuso

net/netfilter/nft_connlimit.c static inline void nft_connlimit_do_eval(struct nft_connlimit *priv, · line 46

Quoted 4 lines
net/netfilter/nft_connlimit.c +1 -3
-	if (nf_conncount_add(nft_net(pkt), priv->list, tuple_ptr, zone)) {
-		regs->verdict.code = NF_DROP;
-		return;
+	if (ctinfo == IP_CT_NEW) {

Quick question: Is this good enough to deal with retransmitted packets while in NEW state?

Good point, I did not consider retransmit case.
What about if (!nf_ct_is_confirmed(ct)) { ..?

Would need a small comment that this is there instead of NEW check due to rexmit.

Quoted 13 lines
Quoted 9 lines

net/netfilter/nft_connlimit.c static inline void nft_connlimit_do_eval(struct nft_connlimit *priv, · line 55

Quoted 9 lines
net/netfilter/nft_connlimit.c +8 -0
+		if (nf_conncount_add(nft_net(pkt), priv->list, tuple_ptr, zone)) {
+			regs->verdict.code = NF_DROP;
+			return;
+		}
+	} else {
+		local_bh_disable();
+		nf_conncount_gc_list(nft_net(pkt), priv->list);
+		local_bh_enable();
 	}

As this needs a respin anyway, what about removing the else clause altogether? Resp. what was the rationale for doing a gc call here?

You mean, call gc for both cases, correct?

Quoted 1 line

And, do we want same check in xt_connlimit?

Are you referring to the !nf_ct_is_confirmed() check?

maintainer
#6 lore raw
Quoted 30 lines · Pablo Neira Ayuso
Quoted 15 lines · Florian Westphal
Quoted 7 lines · Pablo Neira Ayuso

net/netfilter/nft_connlimit.c static inline void nft_connlimit_do_eval(struct nft_connlimit *priv, · line 46

Quoted 4 lines
net/netfilter/nft_connlimit.c +1 -3
-	if (nf_conncount_add(nft_net(pkt), priv->list, tuple_ptr, zone)) {
-		regs->verdict.code = NF_DROP;
-		return;
+	if (ctinfo == IP_CT_NEW) {

Quick question: Is this good enough to deal with retransmitted packets while in NEW state?

Good point, I did not consider retransmit case.
What about if (!nf_ct_is_confirmed(ct)) { ..?

Would need a small comment that this is there instead of NEW check due to rexmit.

Quoted 13 lines
Quoted 9 lines

net/netfilter/nft_connlimit.c static inline void nft_connlimit_do_eval(struct nft_connlimit *priv, · line 55

Quoted 9 lines
net/netfilter/nft_connlimit.c +8 -0
+		if (nf_conncount_add(nft_net(pkt), priv->list, tuple_ptr, zone)) {
+			regs->verdict.code = NF_DROP;
+			return;
+		}
+	} else {
+		local_bh_disable();
+		nf_conncount_gc_list(nft_net(pkt), priv->list);
+		local_bh_enable();
 	}

As this needs a respin anyway, what about removing the else clause altogether? Resp. what was the rationale for doing a gc call here?

Quoted 1 line

You mean, call gc for both cases, correct?

No, i meant this:

/* comment that explains this */
if (nf_ct_is_confirmed(ct))

	return;
if (nf_conncount_add(nft_net(pkt), priv->list, tuple_ptr, zone)) {
	regs->verdict.code = NF_DROP;
	return;
}

As we don't add anything for 'is confirmed' case, I don't see why we would need to prune the existing list.

Quoted 3 lines
Quoted 1 line

And, do we want same check in xt_connlimit?

Are you referring to the !nf_ct_is_confirmed() check?

Yes, AFAICS xt_connlimit has same issue as nft_conncount given they use same backend.

Backend doesn't have access to nf_conn/ctinfo so no way to solve it there.

#7 lore raw
Quoted 39 lines · Florian Westphal
Quoted 30 lines · Pablo Neira Ayuso
Quoted 15 lines · Florian Westphal
Quoted 7 lines · Pablo Neira Ayuso

net/netfilter/nft_connlimit.c static inline void nft_connlimit_do_eval(struct nft_connlimit *priv, · line 46

Quoted 4 lines
net/netfilter/nft_connlimit.c +1 -3
-	if (nf_conncount_add(nft_net(pkt), priv->list, tuple_ptr, zone)) {
-		regs->verdict.code = NF_DROP;
-		return;
+	if (ctinfo == IP_CT_NEW) {

Quick question: Is this good enough to deal with retransmitted packets while in NEW state?

Good point, I did not consider retransmit case.
What about if (!nf_ct_is_confirmed(ct)) { ..?

Would need a small comment that this is there instead of NEW check due to rexmit.

Quoted 13 lines
Quoted 9 lines

net/netfilter/nft_connlimit.c static inline void nft_connlimit_do_eval(struct nft_connlimit *priv, · line 55

Quoted 9 lines
net/netfilter/nft_connlimit.c +8 -0
+		if (nf_conncount_add(nft_net(pkt), priv->list, tuple_ptr, zone)) {
+			regs->verdict.code = NF_DROP;
+			return;
+		}
+	} else {
+		local_bh_disable();
+		nf_conncount_gc_list(nft_net(pkt), priv->list);
+		local_bh_enable();
  	}

As this needs a respin anyway, what about removing the else clause altogether? Resp. what was the rationale for doing a gc call here?

Quoted 1 line

You mean, call gc for both cases, correct?

No, i meant this:

/* comment that explains this */
if (nf_ct_is_confirmed(ct))

	return;
Quoted 8 lines
if (nf_conncount_add(nft_net(pkt), priv->list, tuple_ptr, zone)) {
	regs->verdict.code = NF_DROP;
	return;
}

As we don't add anything for 'is confirmed' case, I don't see why we would need to prune the existing list.

We need this gc call, it is what fixes the use-case reported by the user. If the user is using this expression without a ct state new check, we must check if some connection closed already and update the connection count properly, then evaluate if the connection count greater than the limit for all the packets.

Otherwise, this change will introduce a change in behavior.. AFAICT

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

And, do we want same check in xt_connlimit?

Are you referring to the !nf_ct_is_confirmed() check?

Yes, AFAICS xt_connlimit has same issue as nft_conncount given they use same backend.

Backend doesn't have access to nf_conn/ctinfo so no way to solve it there.

maintainer
#8 lore raw
Quoted 5 lines · Fernando Fernandez Mancera

We need this gc call, it is what fixes the use-case reported by the user. If the user is using this expression without a ct state new check, we must check if some connection closed already and update the connection count properly, then evaluate if the connection count greater than the limit for all the packets.

I don't think so. AFAICS the NEW/!confirmed check is enough, a midstream packet (established connection) isn't added anymore so 'ct count' can't go over the budget.

If last real-add brought us over the budget, then it wasn't added (we were over budget), so next packet of existing flow will still be within budget.

Does that make sense to you?

Quoted 1 line

Otherwise, this change will introduce a change in behavior.. AFAICT

Yes, the existing behaviour is random? (Due to multiple re-adds). With NEW/!confirmed check: no multiadd possible, so no need to ad-hoc gc.

Did I miss anything?

#9 lore raw
Quoted 11 lines · Florian Westphal
Quoted 5 lines · Fernando Fernandez Mancera

We need this gc call, it is what fixes the use-case reported by the user. If the user is using this expression without a ct state new check, we must check if some connection closed already and update the connection count properly, then evaluate if the connection count greater than the limit for all the packets.

I don't think so. AFAICS the NEW/!confirmed check is enough, a midstream packet (established connection) isn't added anymore so 'ct count' can't go over the budget.

Quoted 6 lines

If last real-add brought us over the budget, then it wasn't added (we were over budget), so next packet of existing flow will still be within budget.

Does that make sense to you?

It does for standard use case but not for "inverted" flag - the expression will continue matching and letting packets pass even if count is NOT over the limit anymore because the count is not being updated until a new connection arrives.

Quoted 7 lines
Quoted 1 line

Otherwise, this change will introduce a change in behavior.. AFAICT

Yes, the existing behaviour is random? (Due to multiple re-adds). With NEW/!confirmed check: no multiadd possible, so no need to ad-hoc gc.

Did I miss anything?

maintainer
#10 lore raw
Quoted 16 lines · Fernando Fernandez Mancera
Quoted 15 lines · Florian Westphal
Quoted 5 lines · Fernando Fernandez Mancera

We need this gc call, it is what fixes the use-case reported by the user. If the user is using this expression without a ct state new check, we must check if some connection closed already and update the connection count properly, then evaluate if the connection count greater than the limit for all the packets.

I don't think so. AFAICS the NEW/!confirmed check is enough, a midstream packet (established connection) isn't added anymore so 'ct count' can't go over the budget.

If last real-add brought us over the budget, then it wasn't added (we were over budget), so next packet of existing flow will still be within budget.

Quoted 7 lines
Quoted 2 lines

Does that make sense to you?

It does for standard use case but not for "inverted" flag - the expression will continue matching and letting packets pass even if count is NOT over the limit anymore because the count is not being updated until a new connection arrives.

I don't really see how. Empty ruleset with single

'ct count over 3 reject'

... is broken flat out broken. I mean, whats that supposed to do?

1st connection comes in -> 1
2nd connection comes in -> 2
3rd connection comes in -> 3
4th connection comes in -> 4 -> rejects happen (for all matching _packets_!)

The extra gc doesn't change anything here except that when one connection has closed this gets 'healed'. But I argue that this is nonsensical ruleset, given the connection has to time out (even fin/rst etc. won't pass, so normal closing possible).

If we look at a better example:

ct state new ct count over 3 reject
(or same as before but with earlier 'ct state established accept'):

1st connection comes in -> 1
..
4th connection comes in -> 4 -> new connection attempt gets rejected

... some connection closes ...

-> next connection attempt passes, as one entry is reaped and replaced with another one.

Can you describe a sane failing case?
It should be in a comment if we need to retain the else branch since I can't come up with a single example of where its needed.

#11 lore raw
Quoted 36 lines · Florian Westphal
Quoted 16 lines · Fernando Fernandez Mancera
Quoted 15 lines · Florian Westphal
Quoted 5 lines · Fernando Fernandez Mancera

We need this gc call, it is what fixes the use-case reported by the user. If the user is using this expression without a ct state new check, we must check if some connection closed already and update the connection count properly, then evaluate if the connection count greater than the limit for all the packets.

I don't think so. AFAICS the NEW/!confirmed check is enough, a midstream packet (established connection) isn't added anymore so 'ct count' can't go over the budget.

If last real-add brought us over the budget, then it wasn't added (we were over budget), so next packet of existing flow will still be within budget.

Quoted 7 lines
Quoted 2 lines

Does that make sense to you?

It does for standard use case but not for "inverted" flag - the expression will continue matching and letting packets pass even if count is NOT over the limit anymore because the count is not being updated until a new connection arrives.

I don't really see how. Empty ruleset with single

'ct count over 3 reject'

... is broken flat out broken. I mean, whats that supposed to do?

1st connection comes in -> 1
2nd connection comes in -> 2
3rd connection comes in -> 3
4th connection comes in -> 4 -> rejects happen (for all matching _packets_!)

Quoted 5 lines

The extra gc doesn't change anything here except that when one connection has closed this gets 'healed'. But I argue that this is nonsensical ruleset, given the connection has to time out (even fin/rst etc. won't pass, so normal closing possible).

The use-case I have on mind (which is similar to what user described, but he uses a counter which I guess is just for debugging):

ip saddr 192.168.1.100 tcp dport 22 ct counter over 4 mark set 0x1

later, the mark can be used for tc or policy-based routing - e.g limiting bandwidth if the ip address has too many connections open.

To me this seems a valid use case..

1st connection comes in -> 1
2nd connection comes in -> 2
3rd connection comes in -> 3
4th connection comes in -> 4

all the packets are now being marked with 0x1 and QoS is being performed with tc (limiting the bandwidth). Later, 1st and 2nd connection are closed but the packets from 3rd and 4th connection are still being marked as 0x1 until a new connnection arrives and the count is updated.

This behavior seems wrong to me.. and while I know it isn't supposed to be used without "ct state new" check, I could find some examples doing this on the internet..

maintainer
#12 lore raw
Quoted 9 lines · Fernando Fernandez Mancera

The use-case I have on mind (which is similar to what user described, but he uses a counter which I guess is just for debugging):

ip saddr 192.168.1.100 tcp dport 22 ct counter over 4 mark set 0x1

later, the mark can be used for tc or policy-based routing - e.g limiting bandwidth if the ip address has too many connections open.

To me this seems a valid use case..

It is. Please add a comment as to why the extra gc is needed.

Its not needed for the 'limit this address/network to only have x concurrent connections'.

But it is for 'softlimit-like' things as you explained above (which I failed to consider). Thanks Fernando.

#13 lore raw
Quoted 11 lines · Florian Westphal
Quoted 9 lines · Fernando Fernandez Mancera

The use-case I have on mind (which is similar to what user described, but he uses a counter which I guess is just for debugging):

ip saddr 192.168.1.100 tcp dport 22 ct counter over 4 mark set 0x1

later, the mark can be used for tc or policy-based routing - e.g limiting bandwidth if the ip address has too many connections open.

To me this seems a valid use case..

Quoted 8 lines

It is. Please add a comment as to why the extra gc is needed.

Its not needed for the 'limit this address/network to only have x concurrent connections'.

But it is for 'softlimit-like' things as you explained above (which I failed to consider). Thanks Fernando.

Will send a v2, thank you Florian & Pablo!