linux-yocto/tools
Paul Chaignon adbcb0b374 bpf: Fix L4 csum update on IPv6 in CHECKSUM_COMPLETE
commit ead7f9b8de upstream.

In Cilium, we use bpf_csum_diff + bpf_l4_csum_replace to, among other
things, update the L4 checksum after reverse SNATing IPv6 packets. That
use case is however not currently supported and leads to invalid
skb->csum values in some cases. This patch adds support for IPv6 address
changes in bpf_l4_csum_update via a new flag.

When calling bpf_l4_csum_replace in Cilium, it ends up calling
inet_proto_csum_replace_by_diff:

    1:  void inet_proto_csum_replace_by_diff(__sum16 *sum, struct sk_buff *skb,
    2:                                       __wsum diff, bool pseudohdr)
    3:  {
    4:      if (skb->ip_summed != CHECKSUM_PARTIAL) {
    5:          csum_replace_by_diff(sum, diff);
    6:          if (skb->ip_summed == CHECKSUM_COMPLETE && pseudohdr)
    7:              skb->csum = ~csum_sub(diff, skb->csum);
    8:      } else if (pseudohdr) {
    9:          *sum = ~csum_fold(csum_add(diff, csum_unfold(*sum)));
    10:     }
    11: }

The bug happens when we're in the CHECKSUM_COMPLETE state. We've just
updated one of the IPv6 addresses. The helper now updates the L4 header
checksum on line 5. Next, it updates skb->csum on line 7. It shouldn't.

For an IPv6 packet, the updates of the IPv6 address and of the L4
checksum will cancel each other. The checksums are set such that
computing a checksum over the packet including its checksum will result
in a sum of 0. So the same is true here when we update the L4 checksum
on line 5. We'll update it as to cancel the previous IPv6 address
update. Hence skb->csum should remain untouched in this case.

The same bug doesn't affect IPv4 packets because, in that case, three
fields are updated: the IPv4 address, the IP checksum, and the L4
checksum. The change to the IPv4 address and one of the checksums still
cancel each other in skb->csum, but we're left with one checksum update
and should therefore update skb->csum accordingly. That's exactly what
inet_proto_csum_replace_by_diff does.

This special case for IPv6 L4 checksums is also described atop
inet_proto_csum_replace16, the function we should be using in this case.

This patch introduces a new bpf_l4_csum_replace flag, BPF_F_IPV6,
to indicate that we're updating the L4 checksum of an IPv6 packet. When
the flag is set, inet_proto_csum_replace_by_diff will skip the
skb->csum update.

Fixes: 7d672345ed ("bpf: add generic bpf_csum_diff helper")
Signed-off-by: Paul Chaignon <paul.chaignon@gmail.com>
Acked-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://patch.msgid.link/96a6bc3a443e6f0b21ff7b7834000e17fb549e05.1748509484.git.paul.chaignon@gmail.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
Signed-off-by: Paul Chaignon <paul.chaignon@gmail.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2025-06-27 11:07:38 +01:00
..
accounting delayacct: improve the average delay precision of getdelay tool to microsecond 2024-10-17 15:22:06 +02:00
arch
bootconfig tools/bootconfig: Fix the wrong format specifier 2025-02-21 13:49:20 +01:00
bpf tools/resolve_btfids: Fix build when cross compiling kernel with clang. 2025-06-27 11:07:24 +01:00
build tools/build: Don't pass test log files to linker 2025-06-04 14:40:04 +02:00
certs
cgroup
counter
debugging
edid
firewire
firmware
gpio
hv
iio tools/iio: Add memory allocation failure check for trigger_name 2024-10-17 15:22:17 +02:00
include bpf: Fix L4 csum update on IPv6 in CHECKSUM_COMPLETE 2025-06-27 11:07:38 +01:00
io_uring
kvm/kvm_stat
laptop
leds
lib libbpf: Add identical pointer detection to btf_dedup_is_equiv() 2025-06-27 11:07:34 +01:00
memory-model
objtool objtool: Properly disable uaccess validation 2025-06-04 14:40:03 +02:00
pci
pcmcia
perf perf record: Fix incorrect --user-regs comments 2025-06-27 11:07:15 +01:00
power pm: cpupower: bench: Prevent NULL dereference on malloc failure 2025-04-25 10:43:27 +02:00
rcu
scripts tools: Override makefile ARCH variable if defined, but empty 2024-12-14 19:54:30 +01:00
spi
testing selftests/x86: Add a test to detect infinite SIGTRAP handler loop 2025-06-27 11:07:37 +01:00
thermal
time
tracing rtla/timerlat_top: Abort event processing on second signal 2025-02-21 13:49:59 +01:00
usb usbip: tools: Fix detach_port() invalid port error path 2024-11-08 16:26:44 +01:00
verification verification/dot2: Improve dot parser robustness 2024-12-14 19:54:47 +01:00
virtio
vm
wmi
Makefile