Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 822E230EF75 for ; Wed, 12 Nov 2025 11:45:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.130 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762947937; cv=none; b=rvT7fTVYEwycRB8yBpcSdBJHHR+fSFvuqc9bcOs568ztIeUNAICbTUQqTH/h+PrdRHCXfhHk/vO6O3yP6rqQmSkQL4Ekvd8MyTS3ptKHbilN4U5dtRfDDQpK6v9UD7baWbekxnXXUVTynsTGg0BBrykzMgV4MAZf9hOQBBnD8a0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762947937; c=relaxed/simple; bh=xuPEVniFmzc2GVpOqb9sy9yekSN2uoql+b1YkI7mxAE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qH/TRK2+0OoXsUyqK7KRgxBAxl3eCNUyqjN0EahRyLoMqegb/yaLuQf9MovRwZNrtksGUPCoK2bI7ciapGyt+RvC1A+s62cEsrnByvHomTHda9UJq1CREKp1mZfx09ad6mP9LHxSEp5GfVFryv2KAZjKPJtT+sEm0Fn/3a4Y1dU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de; spf=pass smtp.mailfrom=suse.de; arc=none smtp.client-ip=195.135.223.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.de Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 78B4121248; Wed, 12 Nov 2025 11:45:17 +0000 (UTC) Authentication-Results: smtp-out1.suse.de; none Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id E253E3EA61; Wed, 12 Nov 2025 11:45:16 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id cEZMNExzFGmvCQAAD6G6ig (envelope-from ); Wed, 12 Nov 2025 11:45:16 +0000 From: Fernando Fernandez Mancera To: netfilter-devel@vger.kernel.org Cc: coreteam@netfilter.org, pablo@netfilter.org, fw@strlen.de, phil@nwl.cc, aconole@redhat.com, echaudro@redhat.com, i.maximets@ovn.org, Fernando Fernandez Mancera Subject: [PATCH 6/6 nf-next v3] netfilter: nft_connlimit: update the count if add was skipped Date: Wed, 12 Nov 2025 12:43:52 +0100 Message-ID: <20251112114351.3273-8-fmancera@suse.de> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251112114351.3273-2-fmancera@suse.de> References: <20251112114351.3273-2-fmancera@suse.de> Precedence: bulk X-Mailing-List: netfilter-devel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-Spam-Level: X-Rspamd-Server: rspamd1.dmz-prg2.suse.org X-Spamd-Result: default: False [-4.00 / 50.00]; REPLY(-4.00)[] X-Rspamd-Queue-Id: 78B4121248 X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-Rspamd-Action: no action X-Spam-Flag: NO X-Spam-Score: -4.00 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. To solve this problem, check whether the connection was skipped and if so, update the list. Adjust count_tree() too so the same fix is applied for xt_connlimit. Fixes: 976afca1ceba ("netfilter: nf_conncount: Early exit in nf_conncount_lookup() and cleanup") Closes: https://lore.kernel.org/netfilter/trinity-85c72a88-d762-46c3-be97-36f10e5d9796-1761173693813@3c-app-mailcom-bs12/ Signed-off-by: Fernando Fernandez Mancera --- net/netfilter/nf_conncount.c | 10 +++++++--- net/netfilter/nft_connlimit.c | 15 ++++++++++++--- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/net/netfilter/nf_conncount.c b/net/netfilter/nf_conncount.c index 833ef7507af5..cd26ba65eb09 100644 --- a/net/netfilter/nf_conncount.c +++ b/net/netfilter/nf_conncount.c @@ -408,7 +408,7 @@ insert_tree(struct net *net, int ret; ret = nf_conncount_add_skb(net, skb, l3num, &rbconn->list); - if (ret) + if (ret && ret != -EINVAL) count = 0; /* hotdrop */ else count = rbconn->list.count; @@ -511,10 +511,14 @@ count_tree(struct net *net, /* same source network -> be counted! */ ret = __nf_conncount_add(net, skb, l3num, &rbconn->list); spin_unlock_bh(&rbconn->list.list_lock); - if (ret) + if (ret && ret != -EINVAL) { return 0; /* hotdrop */ - else + } else { + /* -EINVAL means add was skipped, update the list */ + if (ret == -EINVAL) + nf_conncount_gc_list(net, &rbconn->list); return rbconn->list.count; + } } } diff --git a/net/netfilter/nft_connlimit.c b/net/netfilter/nft_connlimit.c index 00c247601173..ba786085389b 100644 --- a/net/netfilter/nft_connlimit.c +++ b/net/netfilter/nft_connlimit.c @@ -28,9 +28,18 @@ static inline void nft_connlimit_do_eval(struct nft_connlimit *priv, int err; err = nf_conncount_add_skb(nft_net(pkt), pkt->skb, nft_pf(pkt), priv->list); - if (err != EINVAL) { - regs->verdict.code = NF_DROP; - return; + if (err) { + if (err == -EINVAL) { + /* Call gc to update the list count if any connection has + * been closed already. This is useful for softlimit + * connections like limiting bandwidth based on a number + * of open connections. + */ + nf_conncount_gc_list(nft_net(pkt), priv->list); + } else { + regs->verdict.code = NF_DROP; + return; + } } count = READ_ONCE(priv->list->count); -- 2.51.0