Netdev List
 help / color / mirror / Atom feed
* [PATCH v3] netfilter: nf_nat: Fix stale outer UDP checksum on VXLAN encapsulated packets
@ 2026-10-08  5:42 lvjunyu
  2026-10-12  6:13 ` netdev-bot+sashiko
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: lvjunyu @ 2026-10-08  5:42 UTC (permalink / raw)
  To: pablo; +Cc: fw, phil, netfilter-devel, netdev, linux-kernel, stable, lvjunyu

When MASQUERADE --random-fully rewrites the outer UDP source port and
address of a VXLAN-encapsulated packet whose inner header has
CHECKSUM_PARTIAL, the outer UDP checksum is not updated correctly.

udp_set_csum() takes the LCO (Local Checksum Offload) branch for
encapsulated CHECKSUM_PARTIAL non-GSO packets, writing a complete
(folded and complemented) checksum into uh->check while leaving
csum_start pointing at the inner transport header.  In this state:

  - inet_proto_csum_replace2() is a no-op (pseudohdr=false,
    CHECKSUM_PARTIAL)
  - nf_csum_update() -> inet_proto_csum_replace4(pseudohdr=true)
    takes the pseudo-header branch which treats uh->check as an
    uncomplemented seed, applying the address delta with the wrong
    sign

Fix this by using the offload target (csum_start + csum_offset) to
distinguish the LCO state from the seed/offload state, and temporarily
flipping ip_summed to CHECKSUM_NONE so both the address and port updates
use csum_replace*() (the complete-checksum path).

Test results (10 connections, single netns vxlan + veth environment):
  Before fix: ~1080ms avg first-connection latency, 10 SYN retransmits,
              UdpInCsumErrors incremented per connection
  After fix:  ~29ms avg first-connection latency, 0 SYN retransmits,
              UdpInCsumErrors unchanged

Fixes: faec18dbb0405 ("netfilter: nat: remove l4proto->manip_pkt")
Cc: stable@vger.kernel.org
Signed-off-by: lvjunyu <lvjunyu@cmss.chinamobile.com>

---

Changes in v3:
- Reword comment to distinguish port delta (no-op) from address
  delta (wrong sign) as suggested by Sashiko AI review

Changes in v2:
- Fix both address and port delta (v1 only fixed port delta)
- Use offload-target test instead of skb->encapsulation as the LCO
  discriminator (addresses Sashiko AI review feedback)
- Update comment to describe both CHECKSUM_PARTIAL states

v2: https://lore.kernel.org/netdev/20260921130312.686493-1-lvjunyu@cmss.chinamobile.com/
---
 net/netfilter/nf_nat_proto.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/net/netfilter/nf_nat_proto.c b/net/netfilter/nf_nat_proto.c
index 64b9bac228e..6779e9f2812 100644
--- a/net/netfilter/nf_nat_proto.c
+++ b/net/netfilter/nf_nat_proto.c
@@ -54,9 +54,39 @@ __udp_manip_pkt(struct sk_buff *skb,
 		portptr = &hdr->dest;
 	}
 	if (do_csum) {
+		/* When udp_set_csum() takes the LCO branch (encapsulated
+		 * CHECKSUM_PARTIAL, non-GSO), uh->check holds a complete
+		 * (folded and complemented) checksum while csum_start
+		 * points at the inner transport header, not at this outer
+		 * udphdr.  In that state:
+		 *   - nf_csum_update() -> inet_proto_csum_replace4(pseudohdr=true)
+		 *     takes the pseudo-header branch and applies the address
+		 *     delta with the wrong sign (to an uncomplemented seed).
+		 *   - inet_proto_csum_replace2(pseudohdr=false) is a no-op:
+		 *     neither branch is taken for CHECKSUM_PARTIAL when
+		 *     pseudohdr is false.
+		 * Temporarily flip ip_summed so both use the csum_replace*()
+		 * path that correctly updates the complete checksum.
+		 *
+		 * The seed/offload branch of udp_set_csum() sets csum_start
+		 * to the outer UDP header, so the offload-target test
+		 * below distinguishes the two states.
+		 */
+		bool lco = skb->ip_summed == CHECKSUM_PARTIAL &&
+			   !skb_is_gso(skb) &&
+			   (skb->head + skb->csum_start + skb->csum_offset) !=
+			   (unsigned char *)&hdr->check;
+
+		if (lco)
+			skb->ip_summed = CHECKSUM_NONE;
+
 		nf_csum_update(skb, iphdroff, &hdr->check, tuple, maniptype);
 		inet_proto_csum_replace2(&hdr->check, skb, *portptr, newport,
 					 false);
+
+		if (lco)
+			skb->ip_summed = CHECKSUM_PARTIAL;
+
 		if (!hdr->check)
 			hdr->check = CSUM_MANGLED_0;
 	}
--
2.43.0




^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH v3] netfilter: nf_nat: Fix stale outer UDP checksum on VXLAN encapsulated packets
  2026-10-08  5:42 [PATCH v3] netfilter: nf_nat: Fix stale outer UDP checksum on VXLAN encapsulated packets lvjunyu
@ 2026-10-12  6:13 ` netdev-bot+sashiko
  2026-10-13  9:00 ` lvjunyu
  2026-12-03  9:05 ` [PATCH v4 0/2] netfilter: " lvjunyu
  2 siblings, 0 replies; 8+ messages in thread
From: netdev-bot+sashiko @ 2026-10-12  6:13 UTC (permalink / raw)
  To: lvjunyu
  Cc: pablo, fw, phil, netfilter-devel, netdev, linux-kernel, stable,
	kuba

Thank you for your contribution! Sashiko AI review found 1 potential
issue(s) to consider.

Critical: 0 · High: 1 · Medium: 0 · Low: 0

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [High] The fix covers only one caller, __udp_manip_pkt() in
  net/netfilter/nf_nat_proto.c.

--

netfilter: nf_nat: Fix stale outer UDP checksum on VXLAN encapsulated packets

This commit changes __udp_manip_pkt() to detect the LCO state of a
CHECKSUM_PARTIAL non-GSO skb. That state is when csum_start + csum_offset
does not point at the outer udphdr check field. In that state the patch
switches ip_summed to CHECKSUM_NONE for a moment, so the address and port
deltas are applied to the complete checksum through csum_replace*().

> diff --git a/net/netfilter/nf_nat_proto.c b/net/netfilter/nf_nat_proto.c
> index 64b9bac228eae..6779e9f2812d9 100644
> --- a/net/netfilter/nf_nat_proto.c
> +++ b/net/netfilter/nf_nat_proto.c
> @@ -54,9 +54,39 @@ __udp_manip_pkt(struct sk_buff *skb,
>  		portptr = &hdr->dest;
>  	}
>  	if (do_csum) {

[ ... ]

> +		bool lco = skb->ip_summed == CHECKSUM_PARTIAL &&
> +			   !skb_is_gso(skb) &&
> +			   (skb->head + skb->csum_start + skb->csum_offset) !=
> +			   (unsigned char *)&hdr->check;
> +
> +		if (lco)
> +			skb->ip_summed = CHECKSUM_NONE;
> +
>  		nf_csum_update(skb, iphdroff, &hdr->check, tuple, maniptype);
>  		inet_proto_csum_replace2(&hdr->check, skb, *portptr, newport,
>  					 false);
> +
> +		if (lco)
> +			skb->ip_summed = CHECKSUM_PARTIAL;
> +

[Severity: High]
This change only covers the nf_nat path. Can the software flowtable fast
path still corrupt the outer UDP checksum for the same LCO packets?

Once the conntrack entry is offloaded (for example with "flow add @ft" plus
MASQUERADE), later packets of the outer VXLAN/Geneve flow take this path:

nf_flow_offload_ip_hook()
  nf_flow_nat_ip()
    nf_flow_snat_port() -> nf_flow_nat_port_udp()
    nf_flow_nat_ip_l4proto() -> nf_flow_nat_ip_udp()

They never reach __udp_manip_pkt().

In net/netfilter/nf_flow_table_ip.c:nf_flow_nat_ip_udp():

	if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
		inet_proto_csum_replace4(&udph->check, skb, addr,
					 new_addr, true);

There is no check here on whether csum_start + csum_offset points at this
outer udph->check. Because of that, inet_proto_csum_replace4() takes the
seed branch:

net/core/utils.c:inet_proto_csum_replace4() {
	...
	} else if (pseudohdr)
		*sum = ~csum_fold(csum_add(csum_sub(csum_unfold(*sum),
						    (__force __wsum)from),
					   (__force __wsum)to));
}

For an LCO complete checksum, this applies the address delta with the
wrong sign.

In net/netfilter/nf_flow_table_core.c:nf_flow_nat_port_udp():

	if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
		inet_proto_csum_replace2(&udph->check, skb, port,
					 new_port, false);

With pseudohdr=false under CHECKSUM_PARTIAL, this is a no-op. That is the
same lost port delta this patch fixes in __udp_manip_pkt().

nf_flow_nat_ipv6_udp() has the same pattern with
inet_proto_csum_replace16(..., true). IPv6 VXLAN computes the outer
checksum by default, so udp6_set_csum() takes the LCO branch there too.

If so, the first packets of the flow would be fixed by this patch. Would
packets after offload still be dropped by the receiver with
UdpInCsumErrors, which is the symptom the commit message describes?

Other callers make the same assumption. tcf_nat_act() in
net/sched/act_nat.c calls inet_proto_csum_replace4(&udph->check, skb,
addr, new_addr, true) under the same condition. In
net/netfilter/ipvs/ip_vs_proto_udp.c, udp_partial_csum_update() treats
every CHECKSUM_PARTIAL uh->check as a seed, though it does its own
arithmetic.

The shared helpers (inet_proto_csum_replace4/16/_by_diff) seem to be where
the assumption lives. Would a helper-level fix work better than a
per-caller ip_summed flip? For example, the helpers could take the seed
path only when:

	skb_checksum_start(skb) + skb->csum_offset == (u8 *)sum

and use csum_replace*() otherwise.

The commit message also doesn't mention these other call sites.

>  		if (!hdr->check)
>  			hdr->check = CSUM_MANGLED_0;
>  	}

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261008054228.752274-1-lvjunyu%40cmss.chinamobile.com

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v3] netfilter: nf_nat: Fix stale outer UDP checksum on VXLAN encapsulated packets
  2026-10-08  5:42 [PATCH v3] netfilter: nf_nat: Fix stale outer UDP checksum on VXLAN encapsulated packets lvjunyu
  2026-10-12  6:13 ` netdev-bot+sashiko
@ 2026-10-13  9:00 ` lvjunyu
  2026-12-03  9:05 ` [PATCH v4 0/2] netfilter: " lvjunyu
  2 siblings, 0 replies; 8+ messages in thread
From: lvjunyu @ 2026-10-13  9:00 UTC (permalink / raw)
  To: netdev, netdev-bot+sashiko
  Cc: pablo, fw, phil, netfilter-devel, linux-kernel, stable, kuba,
	lvjunyu

Thank you for the review.

> [High] The fix covers only one caller, __udp_manip_pkt() in
> net/netfilter/nf_nat_proto.c.

The scope is intentional.  The LCO state this patch fixes is specific
to the UDP encapsulation path: udp_set_csum() writes a complete
(folded, complemented) checksum into uh->check while leaving
csum_start pointing at the inner transport header.  This split state
arises only for locally generated VXLAN/Geneve outer headers that
traverse the conntrack NAT OUTPUT path.

The flowtable software fast path (nf_flow_nat_ip_udp and friends) is
not reached by these packets.  VXLAN outer headers are locally
generated via vxlan_xmit() -> udp_set_csum() and enter the OUTPUT
chain.  The flowtable hook (nf_flow_offload_ip_hook) is registered
at NF_INET_FORWARD, which locally generated packets do not traverse.
Even after the conntrack entry is established, subsequent outer
packets still follow OUTPUT -> POSTROUTING -> __udp_manip_pkt(),
because flowtable offload applies to forwarded flows, not locally
generated ones.

For the sibling call sites:

- tcp_manip_pkt() and icmpv6_manip_pkt(): the offload target is the
  TCP/ICMPv6 header itself (csum_start + csum_offset == &hdr->check),
  so the LCO condition does not arise.  The seed path is correct for
  these protocols.

- Headers embedded in ICMP errors
  (nf_nat_icmp_reply_translation): byte copies in the ICMP payload,
  not live checksum-offloaded data; ip_summed is typically NONE or
  COMPLETE.

- tcf_nat_act() and IPVS: these paths handle LCO-state encapsulated
  UDP packets in theory, but the practical deployment scenario
  (locally generated VXLAN + MASQUERADE --random-fully) is specific
  to the conntrack NAT OUTPUT path.

A helper-level fix in inet_proto_csum_replace4/16/_by_diff() would
cover all ~45 call sites at once and is architecturally preferable.
However, modifying core checksum helpers that are shared by TCP, UDP,
ICMPv6, eBPF, OVS, GSO offload, and ILA requires a separate, more
thorough analysis and test cycle.  This patch is scoped to the UDP
path where the bug is observed and verified; the helper-level
approach is planned as a follow-up.

--
lvjunyu



^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v4 0/2] netfilter: Fix stale outer UDP checksum on VXLAN encapsulated packets
  2026-10-08  5:42 [PATCH v3] netfilter: nf_nat: Fix stale outer UDP checksum on VXLAN encapsulated packets lvjunyu
  2026-10-12  6:13 ` netdev-bot+sashiko
  2026-10-13  9:00 ` lvjunyu
@ 2026-12-03  9:05 ` lvjunyu
  2026-12-03  9:05   ` [PATCH v4 1/2] netfilter: nf_nat: " lvjunyu
  2026-12-03  9:05   ` [PATCH v4 2/2] netfilter: nf_flow_table: Fix stale outer UDP checksum in the fastpath lvjunyu
  2 siblings, 2 replies; 8+ messages in thread
From: lvjunyu @ 2026-12-03  9:05 UTC (permalink / raw)
  To: pablo; +Cc: fw, phil, netfilter-devel, netdev, linux-kernel, stable, lvjunyu

VXLAN and other UDP tunnels can carry locally generated packets whose
outer UDP header holds a complete checksum in the LCO (Local Checksum
Offload) state: the skb is CHECKSUM_PARTIAL and non-GSO, but csum_start
points at the inner transport header rather than at the outer udphdr.
When NAT rewrites the outer header of such a packet, the incremental
checksum helpers either update a value the NIC later recomputes from a
stale seed, or skip the update entirely, so the peer receives packets
with a bad outer checksum and drops them.

This series fixes both NAT paths such a VXLAN flow can take:

Patch 1 fixes the classic conntrack NAT path (__udp_manip_pkt()).
Patch 2 fixes the flowtable software fastpath, which a flow enters
once its conntrack entry is offloaded, and which would otherwise keep
corrupting the outer checksum of every packet after offload even with
patch 1 applied.

Testing (netns + veth + vxlan + iptables MASQUERADE --random-fully):
without the fix, the first TCP connection through the tunnel always
paid a ~1s SYN retransmit because the SYN-ACK was dropped for a bad
outer UDP checksum; with this series applied the first SYN succeeds
and no bad outer checksum is observed.

---
Changes in v4:
- Patch 2 is new: it extends the same LCO fixup to the flowtable
  fastpath (nf_flow_nat_ip_udp(), nf_flow_nat_ipv6_udp() and
  nf_flow_nat_port_udp()).
- Patch 1 is unchanged compared to v3.

v3: https://lore.kernel.org/netfilter-devel/20261008054228.752274-1-lvjunyu@cmss.chinamobile.com/
v2: https://lore.kernel.org/netfilter-devel/20260921130312.686493-1-lvjunyu@cmss.chinamobile.com/
v1: https://lore.kernel.org/netfilter-devel/20260920074543.525572-1-lvjunyu@cmss.chinamobile.com/

lvjunyu (2):
  netfilter: nf_nat: Fix stale outer UDP checksum on VXLAN encapsulated
    packets
  netfilter: nf_flow_table: Fix stale outer UDP checksum in the fastpath

 net/netfilter/nf_flow_table_core.c | 16 ++++++++++++++
 net/netfilter/nf_flow_table_ip.c   | 34 ++++++++++++++++++++++++++++++
 net/netfilter/nf_nat_proto.c       | 30 ++++++++++++++++++++++++++
 3 files changed, 80 insertions(+)

-- 
2.43.0




^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v4 1/2] netfilter: nf_nat: Fix stale outer UDP checksum on VXLAN encapsulated packets
  2026-12-03  9:05 ` [PATCH v4 0/2] netfilter: " lvjunyu
@ 2026-12-03  9:05   ` lvjunyu
  2026-12-04  9:43     ` netdev-bot+sashiko
  2026-12-03  9:05   ` [PATCH v4 2/2] netfilter: nf_flow_table: Fix stale outer UDP checksum in the fastpath lvjunyu
  1 sibling, 1 reply; 8+ messages in thread
From: lvjunyu @ 2026-12-03  9:05 UTC (permalink / raw)
  To: pablo; +Cc: fw, phil, netfilter-devel, netdev, linux-kernel, stable, lvjunyu

When MASQUERADE --random-fully rewrites the outer UDP source port and
address of a VXLAN-encapsulated packet whose inner header has
CHECKSUM_PARTIAL, the outer UDP checksum is not updated correctly.

udp_set_csum() takes the LCO (Local Checksum Offload) branch for
encapsulated CHECKSUM_PARTIAL non-GSO packets, writing a complete
(folded and complemented) checksum into uh->check while leaving
csum_start pointing at the inner transport header.  In this state:

  - inet_proto_csum_replace2() is a no-op (pseudohdr=false,
    CHECKSUM_PARTIAL)
  - nf_csum_update() -> inet_proto_csum_replace4(pseudohdr=true)
    takes the pseudo-header branch which treats uh->check as an
    uncomplemented seed, applying the address delta with the wrong
    sign

Fix this by using the offload target (csum_start + csum_offset) to
distinguish the LCO state from the seed/offload state, and temporarily
flipping ip_summed to CHECKSUM_NONE so both the address and port updates
use csum_replace*() (the complete-checksum path).

Test results (10 connections, single netns vxlan + veth environment):
  Before fix: ~1080ms avg first-connection latency, 10 SYN retransmits,
              UdpInCsumErrors incremented per connection
  After fix:  ~29ms avg first-connection latency, 0 SYN retransmits,
              UdpInCsumErrors unchanged

Fixes: faec18dbb0405 ("netfilter: nat: remove l4proto->manip_pkt")
Cc: stable@vger.kernel.org
Signed-off-by: lvjunyu <lvjunyu@cmss.chinamobile.com>
---
 net/netfilter/nf_nat_proto.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/net/netfilter/nf_nat_proto.c b/net/netfilter/nf_nat_proto.c
index 64b9bac228ea..6779e9f2812d 100644
--- a/net/netfilter/nf_nat_proto.c
+++ b/net/netfilter/nf_nat_proto.c
@@ -54,9 +54,39 @@ __udp_manip_pkt(struct sk_buff *skb,
 		portptr = &hdr->dest;
 	}
 	if (do_csum) {
+		/* When udp_set_csum() takes the LCO branch (encapsulated
+		 * CHECKSUM_PARTIAL, non-GSO), uh->check holds a complete
+		 * (folded and complemented) checksum while csum_start
+		 * points at the inner transport header, not at this outer
+		 * udphdr.  In that state:
+		 *   - nf_csum_update() -> inet_proto_csum_replace4(pseudohdr=true)
+		 *     takes the pseudo-header branch and applies the address
+		 *     delta with the wrong sign (to an uncomplemented seed).
+		 *   - inet_proto_csum_replace2(pseudohdr=false) is a no-op:
+		 *     neither branch is taken for CHECKSUM_PARTIAL when
+		 *     pseudohdr is false.
+		 * Temporarily flip ip_summed so both use the csum_replace*()
+		 * path that correctly updates the complete checksum.
+		 *
+		 * The seed/offload branch of udp_set_csum() sets csum_start
+		 * to the outer UDP header, so the offload-target test
+		 * below distinguishes the two states.
+		 */
+		bool lco = skb->ip_summed == CHECKSUM_PARTIAL &&
+			   !skb_is_gso(skb) &&
+			   (skb->head + skb->csum_start + skb->csum_offset) !=
+			   (unsigned char *)&hdr->check;
+
+		if (lco)
+			skb->ip_summed = CHECKSUM_NONE;
+
 		nf_csum_update(skb, iphdroff, &hdr->check, tuple, maniptype);
 		inet_proto_csum_replace2(&hdr->check, skb, *portptr, newport,
 					 false);
+
+		if (lco)
+			skb->ip_summed = CHECKSUM_PARTIAL;
+
 		if (!hdr->check)
 			hdr->check = CSUM_MANGLED_0;
 	}
-- 
2.43.0




^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v4 2/2] netfilter: nf_flow_table: Fix stale outer UDP checksum in the fastpath
  2026-12-03  9:05 ` [PATCH v4 0/2] netfilter: " lvjunyu
  2026-12-03  9:05   ` [PATCH v4 1/2] netfilter: nf_nat: " lvjunyu
@ 2026-12-03  9:05   ` lvjunyu
  2026-12-04  9:43     ` netdev-bot+sashiko
  1 sibling, 1 reply; 8+ messages in thread
From: lvjunyu @ 2026-12-03  9:05 UTC (permalink / raw)
  To: pablo; +Cc: fw, phil, netfilter-devel, netdev, linux-kernel, stable, lvjunyu

The flowtable software fastpath has the same LCO (Local Checksum
Offload) defect that was fixed in __udp_manip_pkt() for the classic
conntrack NAT path.

Once the conntrack entry of a VXLAN/Geneve flow is offloaded,
subsequent packets of the outer flow bypass __udp_manip_pkt() and
are NATed by the flowtable helpers instead:

  nf_flow_nat_ip() -> nf_flow_nat_ip_udp()
  nf_flow_nat_ipv6() -> nf_flow_nat_ipv6_udp()
  nf_flow_snat_port()/nf_flow_dnat_port() -> nf_flow_nat_port_udp()

For locally generated encapsulated packets in the LCO state
(CHECKSUM_PARTIAL, non-GSO, a complete folded checksum in
uh->check while csum_start points at the inner transport header):

  - inet_proto_csum_replace4/16(pseudohdr=true) takes the seed
    branch and applies the address delta with the wrong sign.
  - inet_proto_csum_replace2(pseudohdr=false) is a no-op, so the
    port delta is lost.

Locally generated VXLAN packets keep CHECKSUM_PARTIAL across a veth
pair, so in a netns/container VXLAN deployment where the flowtable
hook sits at the ingress of the receiving netns these helpers are
reached with LCO packets.  Packets before the flow is offloaded are
fixed up by the __udp_manip_pkt() change, but every packet after
offload corrupts the outer UDP checksum again, the peer drops it
and the inner TCP flow stalls.

Mirror the __udp_manip_pkt() fix: detect the LCO state via the
offload target (csum_start + csum_offset does not point at this
udphdr->check) and temporarily flip ip_summed to CHECKSUM_NONE so
both the address and port updates use the csum_replace*() complete
checksum path.

Fixes: 97add9f0d66d ("netfilter: flow table support for IPv4")
Fixes: 0995210753a2 ("netfilter: flow table support for IPv6")
Fixes: ac2a66665e23 ("netfilter: add generic flow table infrastructure")
Cc: stable@vger.kernel.org
Signed-off-by: lvjunyu <lvjunyu@cmss.chinamobile.com>
---
 net/netfilter/nf_flow_table_core.c | 16 ++++++++++++++
 net/netfilter/nf_flow_table_ip.c   | 34 ++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+)

diff --git a/net/netfilter/nf_flow_table_core.c b/net/netfilter/nf_flow_table_core.c
index 2aa2647ed9df..1971ab36134d 100644
--- a/net/netfilter/nf_flow_table_core.c
+++ b/net/netfilter/nf_flow_table_core.c
@@ -642,8 +642,24 @@ static void nf_flow_nat_port_udp(struct sk_buff *skb, unsigned int thoff,
 
 	udph = (void *)(skb_network_header(skb) + thoff);
 	if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
+		/* With pseudohdr=false, inet_proto_csum_replace2() is
+		 * a no-op under CHECKSUM_PARTIAL, so the port delta
+		 * would be lost for the same LCO packets that
+		 * nf_flow_nat_ip_udp() fixes up.
+		 */
+		bool lco = skb->ip_summed == CHECKSUM_PARTIAL &&
+			   !skb_is_gso(skb) &&
+			   (skb->head + skb->csum_start + skb->csum_offset) !=
+			   (unsigned char *)&udph->check;
+
+		if (lco)
+			skb->ip_summed = CHECKSUM_NONE;
+
 		inet_proto_csum_replace2(&udph->check, skb, port,
 					 new_port, false);
+
+		if (lco)
+			skb->ip_summed = CHECKSUM_PARTIAL;
 		if (!udph->check)
 			udph->check = CSUM_MANGLED_0;
 	}
diff --git a/net/netfilter/nf_flow_table_ip.c b/net/netfilter/nf_flow_table_ip.c
index 19cafae748ab..4b69527942f6 100644
--- a/net/netfilter/nf_flow_table_ip.c
+++ b/net/netfilter/nf_flow_table_ip.c
@@ -59,8 +59,28 @@ static void nf_flow_nat_ip_udp(struct sk_buff *skb, unsigned int thoff,
 
 	udph = (void *)(skb_network_header(skb) + thoff);
 	if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
+		/* Same LCO state as __udp_manip_pkt() in the NAT
+		 * OUTPUT path: for encapsulated CHECKSUM_PARTIAL,
+		 * non-GSO packets, udp_set_csum() writes a complete
+		 * checksum into uh->check while csum_start points at
+		 * the inner transport header.  inet_proto_csum_replace4()
+		 * would take the pseudohdr seed branch and apply the
+		 * address delta with the wrong sign.  Temporarily flip
+		 * ip_summed so the complete-checksum path is used.
+		 */
+		bool lco = skb->ip_summed == CHECKSUM_PARTIAL &&
+			   !skb_is_gso(skb) &&
+			   (skb->head + skb->csum_start + skb->csum_offset) !=
+			   (unsigned char *)&udph->check;
+
+		if (lco)
+			skb->ip_summed = CHECKSUM_NONE;
+
 		inet_proto_csum_replace4(&udph->check, skb, addr,
 					 new_addr, true);
+
+		if (lco)
+			skb->ip_summed = CHECKSUM_PARTIAL;
 		if (!udph->check)
 			udph->check = CSUM_MANGLED_0;
 	}
@@ -951,8 +971,22 @@ static void nf_flow_nat_ipv6_udp(struct sk_buff *skb, unsigned int thoff,
 
 	udph = (void *)(skb_network_header(skb) + thoff);
 	if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
+		/* Same LCO fixup as nf_flow_nat_ip_udp(), with the
+		 * IPv6 address delta (inet_proto_csum_replace16()).
+		 */
+		bool lco = skb->ip_summed == CHECKSUM_PARTIAL &&
+			   !skb_is_gso(skb) &&
+			   (skb->head + skb->csum_start + skb->csum_offset) !=
+			   (unsigned char *)&udph->check;
+
+		if (lco)
+			skb->ip_summed = CHECKSUM_NONE;
+
 		inet_proto_csum_replace16(&udph->check, skb, addr->s6_addr32,
 					  new_addr->s6_addr32, true);
+
+		if (lco)
+			skb->ip_summed = CHECKSUM_PARTIAL;
 		if (!udph->check)
 			udph->check = CSUM_MANGLED_0;
 	}
-- 
2.43.0




^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH v4 1/2] netfilter: nf_nat: Fix stale outer UDP checksum on VXLAN encapsulated packets
  2026-12-03  9:05   ` [PATCH v4 1/2] netfilter: nf_nat: " lvjunyu
@ 2026-12-04  9:43     ` netdev-bot+sashiko
  0 siblings, 0 replies; 8+ messages in thread
From: netdev-bot+sashiko @ 2026-12-04  9:43 UTC (permalink / raw)
  To: lvjunyu
  Cc: pablo, fw, phil, netfilter-devel, netdev, linux-kernel, stable,
	kuba

Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.

Critical: 0 · High: 1 · Medium: 0 · Low: 2

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [High] The patch fixes one caller, nf_nat's __udp_manip_pkt()
  (net/netfilter/nf_nat_proto.c:75-88).
- [Low] The new comment in __udp_manip_pkt()
  (net/netfilter/nf_nat_proto.c:57-74) presents the offload-target test…
- [Low] netfilter: nf_nat: Fixes trailer cites refactoring commit
  faec18dbb0405

--

netfilter: nf_nat: Fix stale outer UDP checksum on VXLAN encapsulated packets

This patch changes __udp_manip_pkt() to detect when the outer UDP check
field is not the CHECKSUM_PARTIAL offload target. That is the state
udp_set_csum() leaves behind on its LCO branch. In that case ip_summed
is set to CHECKSUM_NONE for the duration of the address and port
checksum updates, so that the complete checksum is adjusted with
csum_replace*().

> diff --git a/net/netfilter/nf_nat_proto.c b/net/netfilter/nf_nat_proto.c
> index 64b9bac228eae..6779e9f2812d9 100644
> --- a/net/netfilter/nf_nat_proto.c
> +++ b/net/netfilter/nf_nat_proto.c
> @@ -54,9 +54,39 @@ __udp_manip_pkt(struct sk_buff *skb,
>  		portptr = &hdr->dest;
>  	}
>  	if (do_csum) {
> +		/* When udp_set_csum() takes the LCO branch (encapsulated
> +		 * CHECKSUM_PARTIAL, non-GSO), uh->check holds a complete
> +		 * (folded and complemented) checksum while csum_start
> +		 * points at the inner transport header, not at this outer
> +		 * udphdr.  In that state:
> +		 *   - nf_csum_update() -> inet_proto_csum_replace4(pseudohdr=true)
> +		 *     takes the pseudo-header branch and applies the address
> +		 *     delta with the wrong sign (to an uncomplemented seed).
> +		 *   - inet_proto_csum_replace2(pseudohdr=false) is a no-op:
> +		 *     neither branch is taken for CHECKSUM_PARTIAL when
> +		 *     pseudohdr is false.
> +		 * Temporarily flip ip_summed so both use the csum_replace*()
> +		 * path that correctly updates the complete checksum.
> +		 *
> +		 * The seed/offload branch of udp_set_csum() sets csum_start
> +		 * to the outer UDP header, so the offload-target test
> +		 * below distinguishes the two states.
> +		 */
> +		bool lco = skb->ip_summed == CHECKSUM_PARTIAL &&
> +			   !skb_is_gso(skb) &&
> +			   (skb->head + skb->csum_start + skb->csum_offset) !=
> +			   (unsigned char *)&hdr->check;

[Severity: Low]
The Fixes tag points at faec18dbb0405 ("netfilter: nat: remove
l4proto->manip_pkt"). As far as I can tell, that commit only moved
__udp_manip_pkt() from net/netfilter/nf_nat_proto_udp.c into
nf_nat_proto.c. The checksum handling came across unchanged: the l3
csum_update followed by inet_proto_csum_replace2(..., false). So it
didn't introduce the problem described here.

The state this patch has to handle is a complete outer UDP checksum in
uh->check, while ip_summed stays CHECKSUM_PARTIAL with csum_start on
the inner transport header. That state is created by the LCO branch of
udp_set_csum() and udp6_set_csum(). That branch, and lco_csum(), came
in with 179bc67f69b6 ("net: local checksum offload for
encapsulation").

As far as I can see, before that commit both helpers hit a BUG_ON() for
non-GSO CHECKSUM_PARTIAL skbs. So __udp_manip_pkt() could never see an
outer UDP header in this state.

Should the tag be the following instead?

  Fixes: 179bc67f69b6 ("net: local checksum offload for encapsulation")

Or did you have a specific reason to blame the refactoring commit?

This doesn't affect the code. However, stable tooling and backporters
use the Fixes tag to decide where the patch applies. Pointing at a file
rename suggests the bug started there, and hides that older trees would
need the fix adapted to nf_nat_proto_udp.c.

While at it, the hash is 13 characters. The usual form is the first 12.

[Severity: Low]
This isn't a bug, but the comment says the offload-target test tells
apart udp_set_csum()'s LCO branch and its seed/offload branch. The
condition is broader than that. It matches any UDP header whose check
field is not the CHECKSUM_PARTIAL target, including skbs that
udp_set_csum() never touched.

For example, nf_nat_icmp_reply_translation() reaches __udp_manip_pkt()
for the UDP header inside an ICMP error:

	if (!nf_nat_ipv4_manip_pkt(skb, hdrlen + sizeof(inside->icmp),
				   &ct->tuplehash[!dir].tuple, !manip))

Suppose that skb is CHECKSUM_PARTIAL with the target on the ICMP
checksum, for example one injected through virtio_net_hdr with
NEEDS_CSUM. Then lco is now true, and the embedded UDP checksum is
updated as a complete checksum.

That new behavior is correct, but neither the comment nor the commit
message mentions it.

Could the comment, and perhaps the lco name, describe the general rule
instead? Something like "if this check field is not the
CHECKSUM_PARTIAL offload target, it holds a complete checksum".

> +
> +		if (lco)
> +			skb->ip_summed = CHECKSUM_NONE;
> +
>  		nf_csum_update(skb, iphdroff, &hdr->check, tuple, maniptype);
>  		inet_proto_csum_replace2(&hdr->check, skb, *portptr, newport,
>  					 false);
> +
> +		if (lco)
> +			skb->ip_summed = CHECKSUM_PARTIAL;

[Severity: High]
Is the root cause in the shared helpers rather than in this caller?
Under CHECKSUM_PARTIAL, inet_proto_csum_replace4() and
inet_proto_csum_replace16() in net/core/utils.c assume *sum is the
uncomplemented pseudo-header seed at the offload target:

net/core/utils.c:inet_proto_csum_replace4() {
    ...
	} else if (pseudohdr)
		*sum = ~csum_fold(csum_add(csum_sub(csum_unfold(*sum),
						    (__force __wsum)from),
					   (__force __wsum)to));
}

With pseudohdr=false they do nothing at all.

That assumption doesn't hold for the state that udp_set_csum() and
udp6_set_csum() leave behind on the LCO path:

	uh->check = udp_v4_check(len, saddr, daddr, lco_csum(skb));

The follow-up patch "netfilter: nf_flow_table: Fix stale outer UDP
checksum in the fastpath" covers the flowtable. It copies the same
lco / ip_summed toggle into nf_flow_nat_ip_udp(),
nf_flow_nat_ipv6_udp() and nf_flow_nat_port_udp().

Even with both patches applied, other code that rewrites outer UDP
addresses or ports on the same skbs still uses the seed-only semantics:

  tcf_nat_act() in net/sched/act_nat.c, UDP case:

	if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
		inet_proto_csum_replace4(&udph->check, skb, addr,
					 new_addr, true);

  In net/openvswitch/actions.c, update_ip_l4_checksum() and
  update_ipv6_checksum() take the seed path. set_tp_port() calls
  inet_proto_csum_replace2(check, skb, *port, new_port, false), which
  does nothing.

  udp_snat_handler() and udp_dnat_handler() in
  net/netfilter/ipvs/ip_vs_proto_udp.c call udp_partial_csum_update()
  whenever ip_summed == CHECKSUM_PARTIAL.

  nft_payload_l4csum_update() in net/netfilter/nft_payload.c applies the
  seed formula under CHECKSUM_PARTIAL. nft_payload_set_eval() skips the
  transport header checksum update entirely under CHECKSUM_PARTIAL.

  nf_nat_ipv4_csum_recalc() and nf_nat_ipv6_csum_recalc() in this file
  use inet_proto_csum_replace2(..., true).

As one example path, a local inner TCP SYN on a vxlan device takes the
LCO branch here:

vxlan_xmit()
  udp_tunnel_xmit_skb()
    udp_set_csum()

The packet can then reach act_nat, an OVS set action, IPVS, or an nft
payload set with checksum flags. All of these run before
skb_csum_hwoffload_help() in validate_xmit_skb(), so ip_summed is still
CHECKSUM_PARTIAL at that point.

Wouldn't these paths still produce the bad outer UDP checksums and
UdpInCsumErrors described in the commit message?

Could the helpers take the CHECKSUM_PARTIAL seed branch only when
skb_is_gso() is true, or when sum equals skb->head + csum_start +
csum_offset, and use csum_replace*() otherwise? That would cover every
caller, and this function would no longer need to change skb->ip_summed
temporarily.

The patch is also tagged Cc: stable, but the commit message doesn't
mention these other callers. Backporters may assume this class of bug
is fully fixed.

>  		if (!hdr->check)
>  			hdr->check = CSUM_MANGLED_0;
>  	}

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261203090554.158077-1-lvjunyu%40cmss.chinamobile.com

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v4 2/2] netfilter: nf_flow_table: Fix stale outer UDP checksum in the fastpath
  2026-12-03  9:05   ` [PATCH v4 2/2] netfilter: nf_flow_table: Fix stale outer UDP checksum in the fastpath lvjunyu
@ 2026-12-04  9:43     ` netdev-bot+sashiko
  0 siblings, 0 replies; 8+ messages in thread
From: netdev-bot+sashiko @ 2026-12-04  9:43 UTC (permalink / raw)
  To: lvjunyu
  Cc: pablo, fw, phil, netfilter-devel, netdev, linux-kernel, stable,
	kuba

Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.

Critical: 0 · High: 1 · Medium: 0 · Low: 2

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [High] The fix is incomplete: tunnels with the outer UDP checksum
  turned off are still corrupted after flow offload.
- [Low] The new comment in nf_flow_nat_ip_udp()
  (net/netfilter/nf_flow_table_ip.c, lines 62-63) calls the reference…
- [Low] netfilter: nf_flow_table: duplicated LCO toggle open-codes csum
  helper

--

netfilter: nf_flow_table: Fix stale outer UDP checksum in the fastpath

This patch changes the flowtable UDP NAT helpers nf_flow_nat_ip_udp(),
nf_flow_nat_ipv6_udp() and nf_flow_nat_port_udp() to detect the LCO state.
In that state the skb is CHECKSUM_PARTIAL and not GSO, and the offload
target is not this udphdr->check.  When it is detected, ip_summed is
temporarily switched to CHECKSUM_NONE so the outer UDP checksum is updated
on the complete-checksum path, mirroring the __udp_manip_pkt() fix.

[ ... ]

> diff --git a/net/netfilter/nf_flow_table_ip.c b/net/netfilter/nf_flow_table_ip.c
> index 19cafae748abe..4b69527942f6d 100644
> --- a/net/netfilter/nf_flow_table_ip.c
> +++ b/net/netfilter/nf_flow_table_ip.c
> @@ -59,8 +59,28 @@ static void nf_flow_nat_ip_udp(struct sk_buff *skb, unsigned int thoff,
>  
>  	udph = (void *)(skb_network_header(skb) + thoff);
>  	if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
> +		/* Same LCO state as __udp_manip_pkt() in the NAT
> +		 * OUTPUT path: for encapsulated CHECKSUM_PARTIAL,

[Severity: Low]
This isn't a bug, but is "in the NAT OUTPUT path" accurate here?

__udp_manip_pkt() takes no hook argument.  udp_manip_pkt() calls it for
both SNAT and DNAT at every NAT hook, and the LCO case with MASQUERADE
would run in POSTROUTING rather than OUTPUT.

> +		 * non-GSO packets, udp_set_csum() writes a complete
> +		 * checksum into uh->check while csum_start points at
> +		 * the inner transport header.  inet_proto_csum_replace4()
> +		 * would take the pseudohdr seed branch and apply the
> +		 * address delta with the wrong sign.  Temporarily flip
> +		 * ip_summed so the complete-checksum path is used.
> +		 */
> +		bool lco = skb->ip_summed == CHECKSUM_PARTIAL &&
> +			   !skb_is_gso(skb) &&
> +			   (skb->head + skb->csum_start + skb->csum_offset) !=
> +			   (unsigned char *)&udph->check;
> +
> +		if (lco)
> +			skb->ip_summed = CHECKSUM_NONE;
> +
>  		inet_proto_csum_replace4(&udph->check, skb, addr,
>  					 new_addr, true);
> +
> +		if (lco)
> +			skb->ip_summed = CHECKSUM_PARTIAL;
>  		if (!udph->check)
>  			udph->check = CSUM_MANGLED_0;
>  	}

[Severity: Low]
This isn't a bug, but this patch open-codes the same lco predicate
three times, along with the ip_summed save/restore around
inet_proto_csum_replace*():

  - nf_flow_nat_port_udp()
  - nf_flow_nat_ip_udp()
  - nf_flow_nat_ipv6_udp()

Patch 1/2 adds a fourth copy in __udp_manip_pkt().  That is fairly
subtle checksum state logic, and every copy has to be kept in sync.
Any later fix to it would have to be applied in four places.

All four copies also open-code skb->head + skb->csum_start.
skb_checksum_start() in include/linux/skbuff.h already provides that.

Could the detection and the temporary ip_summed flip be factored into
a single shared helper built on skb_checksum_start()?

More broadly, inet_proto_csum_replace4(),
inet_proto_csum_replace16() and inet_proto_csum_replace_by_diff() in
net/core/utils.c already receive both sum and skb.  They could tell
whether sum is the offload target themselves.

Other in-tree users rewrite outer addresses or ports through these
helpers:

  - net/sched/act_nat.c
  - net/openvswitch/actions.c
  - the BPF l4 csum helpers in net/core/filter.c

They look like they would hit the same LCO state on UDP tunnel
traffic, and would still produce a stale outer checksum after this
series.

Was handling this in the core helpers considered?  Or is there a
reason to keep the workaround local to the netfilter callers?

[Severity: High]
Does the lco predicate also match tunnels that have the outer UDP checksum
disabled?

In that case udp_set_csum() takes the nocheck branch:

net/ipv4/udp.c:udp_set_csum() {
	if (nocheck) {
		uh->check = 0;
	...
}

ip_summed stays CHECKSUM_PARTIAL, csum_start still points at the inner
transport header, and uh->check is 0.

This gate still admits that skb:

	if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {

lco then evaluates true, and csum_replace4() runs on *sum == 0.  The result
is ~fold(~from + to), which is nonzero whenever the address actually
changes.  The CSUM_MANGLED_0 fallback therefore never fires, and the packet
goes out with a nonzero but wrong outer checksum.  Won't the receiving
VTEP drop it?

The same seems to apply in two other places:

  - nf_flow_nat_ipv6_udp() via inet_proto_csum_replace16().
    udp6_set_csum() has the same nocheck branch.
  - nf_flow_nat_port_udp() in nf_flow_table_core.c, where the port
    delta is now written into the zero checksum.

The classic path this is meant to mirror never rewrites a zero checksum:

net/netfilter/nf_nat_proto.c:udp_manip_pkt() {
	...
	__udp_manip_pkt(skb, iphdroff, hdr, tuple, maniptype, !!hdr->check);
	...
}

So these packets would pass before offload and still be dropped after
offload.

Several common setups are in this state:

  - Geneve over IPv4, by default.  geneve_nl2info() only sets
    IP_TUNNEL_CSUM_BIT when IFLA_GENEVE_UDP_CSUM=1.
  - VXLAN created with noudpcsum.
  - IPv6 tunnels using udp6zerocsumtx.

This state was already broken before the patch, so it isn't a new
regression.  Address updates went through the CHECKSUM_PARTIAL seed
branch, and the port update was a no-op followed by CSUM_MANGLED_0.

The commit message says this mirrors the __udp_manip_pkt() fix.  Should
these helpers skip the update when lco is set and udph->check is 0, to
match the !!hdr->check gate in udp_manip_pkt()?

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261203090554.158077-1-lvjunyu%40cmss.chinamobile.com

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2026-12-04  9:43 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-08  5:42 [PATCH v3] netfilter: nf_nat: Fix stale outer UDP checksum on VXLAN encapsulated packets lvjunyu
2026-10-12  6:13 ` netdev-bot+sashiko
2026-10-13  9:00 ` lvjunyu
2026-12-03  9:05 ` [PATCH v4 0/2] netfilter: " lvjunyu
2026-12-03  9:05   ` [PATCH v4 1/2] netfilter: nf_nat: " lvjunyu
2026-12-04  9:43     ` netdev-bot+sashiko
2026-12-03  9:05   ` [PATCH v4 2/2] netfilter: nf_flow_table: Fix stale outer UDP checksum in the fastpath lvjunyu
2026-12-04  9:43     ` netdev-bot+sashiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox