[PATCH v2] netfilter: nf_nat: Fix stale outer UDP checksum on VXLAN encapsulated packets

From: lvjunyu

Date: Tue Sep 22 2026 - 01:59:32 EST


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@xxxxxxxxxxxxxxx
Signed-off-by: lvjunyu <lvjunyu@xxxxxxxxxxxxxxxxxxxx>

---

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

v1: https://lore.kernel.org/netdev/20260920074543.525572-1-lvjunyu@xxxxxxxxxxxxxxxxxxxx/
---
net/netfilter/nf_nat_proto.c | 26 ++++++++++++++++++++++++++
1 file changed, 26 insertions(+)

diff --git a/net/netfilter/nf_nat_proto.c b/net/netfilter/nf_nat_proto.c
index 64b9bac228e..cb70d85716b 100644
--- a/net/netfilter/nf_nat_proto.c
+++ b/net/netfilter/nf_nat_proto.c
@@ -54,9 +54,35 @@ __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, inet_proto_csum_replace*() and
+ * nf_csum_update() take the pseudo-header branch which
+ * operates on an uncomplemented seed, applying the address
+ * and port deltas with the wrong sign. Temporarily flip
+ * ip_summed so they use csum_replace*() instead.
+ *
+ * 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