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 --- 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