Thread View: linux.kernel.netdev
3 messages
3 total messages
Started by lvjunyu
Thu, 08 Oct 2026 05:42
[PATCH v3] netfilter: nf_nat: Fix stale outer UDP checksum on VXLAN encapsulated packets
Author: lvjunyu
Date: Thu, 08 Oct 2026 05:42
Date: Thu, 08 Oct 2026 05:42
95 lines
9621 bytes
9621 bytes
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
Re: [PATCH v3] netfilter: nf_nat: Fix stale outer UDP checksum on VXLAN encapsulated packets
Author: netdev-bot+sashiko
Date: Mon, 12 Oct 2026 06:13
Date: Mon, 12 Oct 2026 06:13
128 lines
10189 bytes
10189 bytes
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
Re: [PATCH v3] netfilter: nf_nat: Fix stale outer UDP checksum on VXLAN encapsulated packets
Author: lvjunyu
Date: Tue, 13 Oct 2026 09:00
Date: Tue, 13 Oct 2026 09:00
50 lines
8206 bytes
8206 bytes
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
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads