Article View: linux.kernel.netdev
Article #14441[PATCH nf-next] netfilter: nf_nat: skip the xfrm re-lookup without outbound policies
From: Antonio Quartulli
Date: Wed, 11 Nov 2026 08:44
Date: Wed, 11 Nov 2026 08:44
55 lines
10965 bytes
10965 bytes
nf_xfrm_me_harder() decodes the session and redoes the xfrm lookup for
every packet whose source or ports NAT changed, even when there is no
policy that could match. The session decode alone is a sizeable part of
the NAT path.
Skip it when there is no outbound policy, global or per socket, the
default outbound policy accepts and the dst is not a bundle: the lookup
would return the same route. xfrm_route_forward() takes the same
shortcut for forwarded packets.
This is the common case for masquerading routers built with
CONFIG_XFRM that never use IPsec. Forwarding a 1400-byte packet through
conntrack and masquerade (x86, qemu -icount) drops from 7533 to 7091
instructions for UDP (-5.9%) and from 7835 to 7395 for TCP (-5.6%).
With an outbound policy installed the full path still runs, and a
block policy matching only the masqueraded flow still drops it.
Signed-off-by: Antonio Quartulli <antonio@mandelbit.com>
---
net/netfilter/nf_nat_proto.c | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/net/netfilter/nf_nat_proto.c b/net/netfilter/nf_nat_proto.c
index 64b9bac228ea..69849d805984 100644
--- a/net/netfilter/nf_nat_proto.c
+++ b/net/netfilter/nf_nat_proto.c
@@ -600,12 +600,25 @@ nf_nat_ipv4_pre_routing(void *priv, struct sk_buff *skb,
#ifdef CONFIG_XFRM
static int nf_xfrm_me_harder(struct net *net, struct sk_buff *skb, unsigned int family)
{
+ const struct netns_xfrm *xfrm = &net->xfrm;
struct sock *sk = skb->sk;
struct dst_entry *dst;
unsigned int hh_len;
struct flowi fl;
int err;
+ /* Without any outbound policy, global or per socket, and with the
+ * default outbound policy accepting, the lookup below cannot change
+ * the route unless the dst is already a bundle: skip it, and the
+ * session decode it needs.
+ */
+ if (!READ_ONCE(xfrm->policy_count[XFRM_POLICY_OUT]) &&
+ !READ_ONCE(xfrm->policy_count[XFRM_POLICY_MAX + XFRM_POLICY_OUT]) &&
+ READ_ONCE(xfrm->policy_default[XFRM_POLICY_OUT]) ==
+ XFRM_USERPOLICY_ACCEPT &&
+ !skb_dst(skb)->xfrm)
+ return 0;
+
err = xfrm_decode_session(net, skb, &fl, family);
if (err < 0)
return err;
--
2.56.0
Technical Details
Message-ID:
<20261111084412.2702283-1-antonio@mandelbit.com>Path:
mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!newsfeed.bofh.team!nntp.comgw.net!eternal-september.org!feeder.eternal-september.org!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-360923-linux-kernel-netdev=dewy.ruShow Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!newsfeed.bofh.team!nntp.comgw.net!eternal-september.org!feeder.eternal-september.org!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-360923-linux-kernel-netdev=dewy.ru From: antonio@mandelbit.com (Antonio Quartulli) Newsgroups: linux.kernel.netdev Subject: [PATCH nf-next] netfilter: nf_nat: skip the xfrm re-lookup without outbound policies Date: Wed, 11 Nov 2026 09:44:10 +0100 Organization: Usenet Network Message-ID: <20261111084412.2702283-1-antonio@mandelbit.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Injection-Info: usenet.network; logging-data="814322"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:zwQ4sAqX2ENzdB7mpfOQN+M60mM= sha256:X5zuR41aQtbhHhHJSZ5q0ZFGaS5slAtUDxk1Jc6V0cY= sha1:5hUC2RY5j7i5l4g3VXDVi380OxA= sha256:gDNvrfVJ1Hxn+4QMQQrEaINLdOqjZ2O0fZ49+8iisQM= List-ID: <netdev.vger.kernel.org> X-Mailpost-Empty-Hdrs: X-Spam-Level X-Received: from localhost (localhost [127.0.0.1]) by mail.dewy.ru (Postfix) with ESMTP id 97E76113E54; Wed, 11 Nov 2026 11:58:06 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at usenet.network X-Spam-Flag: NO X-Spam-Score: -3.051 X-Spam-Status: No, score=-3.051 tagged_above=-9999 required=6.31 tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.249, MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001] autolearn=ham autolearn_force=no X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key) header.d=mandelbit.com X-Received: from mail.dewy.ru ([127.0.0.1]) by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id aPHz30tdxBDv; Wed, 11 Nov 2026 11:57:58 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=104.64.211.4; helo=sin.lore.kernel.org; envelope-from=netdev+bounces-360923-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> X-Authentication-Results: OpenDMARC; dmarc=none (p=none dis=none) header.from=mandelbit.com X-Received: from sin.lore.kernel.org (sin.lore.kernel.org [104.64.211.4]) by mail.dewy.ru (Postfix) with ESMTPS id 3E939113777; Wed, 11 Nov 2026 11:57:49 +0300 (MSK) X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by sin.lore.kernel.org (Postfix) with ESMTP id 791D5A4139; Wed, 11 Nov 2026 08:44:28 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1794386668; b=eZG+ESkQGaPpONUWeP6oQDBf7fifO/kjl8LfPpmW1DvqK7DUo/LtgD5XD4WyvRcmUA9X kNzZMZPykLo8w/saMDZTfXBXly8+BA8BVU9QmaEi0zAm9ZB9GiGKsyHgJ1pHiIwugQNUS 2nZt0wFTTE41QgdKFwhJXBFEyAR2javTyrUykJblQWwuZotlv/W6g8eBvHOOTqcWjhPJn F9YXfzygOiRvuAHDwHRHdPn+2a7zT7bc9SZj59W9dCQHAZIGJ7T5ill9e0/QZ4jn5aHQQ PxZnJlD9C79SMt4Wx9/dvCVMeg62Nz/F0kMI+rA6zX7T55xtc7INmm/DV4FOxs8dn9Q== X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794386668; h=X-Original-To:Received:DMARC-Filter:Received:DKIM-Signature:From:To: Cc:Subject:Date:Message-ID:X-Mailer:X-Mailing-List:List-Id:List-Post: List-Help:List-Subscribe:List-Unsubscribe:List-Owner:Precedence: MIME-Version:Content-Transfer-Encoding:X-Rspamd-Queue-Id; bh=0TaZ5JCUftcvgABnCcPfKOwKyYVdTvJYp612kagBhak=; b=cAnNp5NpyXXlhsnH9euSI6YUNw7tk07RPt3avny9sWgrjkutw5J1lOveNWo+4tZh+E/r OKXExtu86KpAFCgn8DvzrBoh50/Si3FpoML9HzD55INpCfxaAAqXBVWzxwYG0JNk3RRNv yP/UmKh0d24z6XzHk10wBYY4UOWgT+00TY7HE3/mLQLKv/2z5OTVF7lhBs0A0CqFE61D8 D5W+n34sIllgOKOVpay/VAjXrwCGmWP3PEnb2Oe6NOTacNqI74XEbe0zwyqVDLyqAOEjx vd56ru1IH7DyYlkgOOHV2Jv5xmwGvpGugQ5IM5CK4RGfrrPQSyevgPyWXIvkTOoHWcw== X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=mandelbit.com header.i=@mandelbit.com header.a=rsa-sha256 header.s=MBO0001 header.b=ATF2+Wf+; dmarc=none header.from=mandelbit.com; spf=pass smtp.mailfrom=mandelbit.com; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DCB4C606A32A; Wed, 11 Nov 2026 08:44:27 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=mandelbit.com header.i=@mandelbit.com header.a=rsa-sha256 header.s=MBO0001 header.b=ATF2+Wf+ X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=195.10.208.52 X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794386667; b=5Fhg1PDAbwL9YwiHB4eiU5i+rHg3yorNkWVgihpOkWr27u4bxSsdK3hZ0r8F7P6LSXVf Yn68oJ7PWI/MbtgU6k6n+vqqWFNqoTyWEafBEqUn8bOpZ6aQk+jzmGxdnxh1LLmArA496 1K5OoJzUQGSckox5WuAY8+Eswa+UtyKIWs9sJ95DN1/07n95P1tfc2a1iySZwXBraUh98 nBnxhlURS+GQX4328SkT0Pej1MQpcCKZMNDLqyPzBVLyWyc1PalmFZzKXjW667cH3sipC ijOpfo/gAy76Ex0dlnEvUmBotKMZ3ixcLhf1w8e0ZMbbI8vmEFTJ0rs/4bGCdnBjR/g== X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794386667; h=DMARC-Filter:Received:DKIM-Signature:From:To:Cc:Subject:Date: Message-ID:X-Mailer:MIME-Version:Content-Transfer-Encoding: X-Rspamd-Queue-Id; bh=0TaZ5JCUftcvgABnCcPfKOwKyYVdTvJYp612kagBhak=; b=Sasjr0GjKB57705BBvRdoXW1hRckXJQrMtxAtfJkpsLT4BZ8wjsI4SEn1Nzdtc2al6dd xi9K9ItG9KDGcL9u9I4jdQQgMHmGzaES/102HyiaYPC+EZG0DouHgv3YE2XS1d02Jn964 u2o65q41h67z986kz5qkAZWF/ci9kdhNGGPDNk9Z6Fmj+QWTo8/uzSCuAhDypBwy4/PSw gPh4/ZiqHd46mqbHxvY2+5DWNFXrlj2K54VinoyxKbwzabUzQvPuJgilZ0Kc0H0QnGVvl MqtDkPQW4D1aMSJhGZj9JT3vibi/eHY6IP0AK0J1RabyF2cQSRZyr0vSCd5Ebr93AhQ== X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=mandelbit.com; spf=pass smtp.mailfrom=mandelbit.com; dkim=pass header.d=mandelbit.com header.i=@mandelbit.com header.a=rsa-sha256 header.s=MBO0001 header.b=ATF2+Wf+; arc=none smtp.remote-ip=195.10.208.52 X-Received: from mout-b-203.mailbox.org (mout-b-203.mailbox.org [195.10.208.52]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E531E606A320; Wed, 11 Nov 2026 08:44:24 +0000 (UTC) X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E531E606A320 X-Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=mandelbit.com X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mandelbit.com X-Received: from smtp1.mailbox.org (smtp1.mailbox.org [IPv6:2001:67c:2050:b231:465::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519MLKEM768 server-signature RSA-PSS (4096 bits) server-digest SHA512) (No client certificate requested) by mout-b-203.mailbox.org (Postfix) with ESMTPS id 4jPcWg4fPpzLmG0; Wed, 11 Nov 2026 09:44:15 +0100 (CET) X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mandelbit.com; s=MBO0001; t=1794386655; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=0TaZ5JCUftcvgABnCcPfKOwKyYVdTvJYp612kagBhak=; b=ATF2+Wf+xgoumpPzKb+IWK0RLW4Fsetsy6H391CtVmhidqOaqAe2SH6R4INqUSWp8WHgYB F+kqPWHMA2XMr7rwDOr2MNOIY4pl92TSPS39JykUSPypBuDEdhxjljdOtGJwqo1k74A4k6 8a38WCXNlXeNb8YpnoLWfYjTvWae4ZtFN4KX+7IKfcLWKglOILtDy7fZLOfuN3X9N2fkpu qHkqMPvB459suDuYyvp2XWT0KpKF6qsz6CkwzoENEclNxuVFoCwQ1U+5UehpNB6uokQ6Qo v3NWaoMpD0U+OUiJAXQtfRDy2rADFS+86+we3kW9TRjlWjOk8vlWPFR9CTeJAA== X-Authentication-Results: outgoing_mbo_mout; dkim=none; spf=pass (outgoing_mbo_mout: domain of antonio@mandelbit.com designates 2001:67c:2050:b231:465::1 as permitted sender) smtp.mailfrom=antonio@mandelbit.com X-Cc: Antonio Quartulli <antonio@mandelbit.com>, phil@nwl.cc, steffen.klassert@secunet.com, herbert@gondor.apana.org.au, davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org, pabeni@redhat.com, horms@kernel.org, netfilter-devel@vger.kernel.org, coreteam@netfilter.org, netdev@vger.kernel.org X-Mailer: git-send-email 2.56.0 X-Mailing-List: netdev@vger.kernel.org X-List-Post: <mailto:netdev@vger.kernel.org> X-List-Help: <mailto:netdev+help@vger.kernel.org> X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org> X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org> X-List-Owner: <mailto:netdev+owner@vger.kernel.org> X-Precedence: list X-Rspamd-Queue-Id: 4jPcWg4fPpzLmG0