Skip to contentSkip to conversation

bpf: clear stale IPv4 options after LWT encapsulation

PATCH In review Weiming Shi 4 participants 6 messages Sep 21 – Sep 22

Subsystems (7)
lore
Share & tools
lore
#0 lore raw

v4 cover bpf: clear stale IPv4 options after LWT encapsulation

bpf_lwt_push_ip_encap() rebases the network header after prepending an outer IP header, but can leave IPCB(skb)->opt describing the inner IPv4 header. An ingress LWT route can then make an ICMP error consume stale option offsets.

Patch 1 marks completed LWT IP encapsulation and clears the stale metadata after bpf_prog_run_save_cb() has restored the protocol control block. For same-family IPv4 encapsulation it clears only IPCB(skb)->opt. For a family change it resets the new family's control block following seg6_do_srh_encap(). Patch 2 adds compact coverage to the existing LWT IP encapsulation selftest.

Changes:

v4:
- Follow Alexei Starovoitov's suggestion to drop the independent freplace patch and keep cb_access as one bit.
- Clear only IPv4 options when the family is unchanged; use skb->protocol and the SEG6 precedent when it changes.
- Rebase on bpf-next and replace the standalone selftest with compact coverage in the existing LWT IP encapsulation test.

v3: https://lore.kernel.org/bpf/[email protected]/
- Follow Daniel Borkmann's suggestion to reset the CB after bpf_prog_run_save_cb() restores it, avoiding an extra CB copy.
- Split out freplace handling and selftests, and cover CB access, VRF, nested runs, and already-encapsulated packets.

v2: https://lore.kernel.org/bpf/[email protected]/
- Preserve the protocol CB for programs without ctx->cb[] access and restore the proper layout for the final packet consumer.

v1: https://lore.kernel.org/bpf/[email protected]/

Weiming Shi (2):
bpf: clear stale IPv4 options after LWT encapsulation selftests/bpf: cover stale CB after LWT IP encapsulation

 include/linux/filter.h                        |  1 +
 net/core/lwt_bpf.c                            | 30 ++++++++++++
 .../selftests/bpf/prog_tests/lwt_ip_encap.c   | 45 ++++++++++++++++++
 .../selftests/bpf/progs/test_lwt_ip_encap.c   | 47 +++++++++++++++++++
 4 files changed, 123 insertions(+)
Signature · 1 line
2.55.0
#1 lore raw

PATCH v4 1/2 bpf: clear stale IPv4 options after LWT encapsulation

bpf_lwt_push_ip_encap() rebases the network header after prepending an IP header, but leaves IPCB(skb)->opt describing the inner IPv4 header. An ingress LWT route can consequently make an ICMP error use stale option offsets when constructing its reply.

Mark completed LWT IP encapsulation in the run's BPF network context and clear stale metadata after bpf_prog_run_save_cb() restores the protocol control block. For same-family IPv4 encapsulation, clear only the cached options, as the IP tunnel transmit paths do. When the family changes, reset the destination family's control block and initialize its ingress interface, following seg6_do_srh_encap(). Save and restore the marker around nested LWT runs.

Fixes 52f278774e79 ("bpf: implement BPF_LWT_ENCAP_IP mode in bpf_lwt_push_encap")
Assisted-by LLM
---
 include/linux/filter.h |  1 +
 net/core/lwt_bpf.c     | 30 ++++++++++++++++++++++++++++++
 2 files changed, 31 insertions(+)
include/linux/filter.h +1 -0
index 39decde7fc73..195898af9528 100644
@@ -848,6 +848,7 @@ struct bpf_nh_params {
 #define BPF_RI_F_CPU_MAP_INIT	BIT(2)
 #define BPF_RI_F_DEV_MAP_INIT	BIT(3)
 #define BPF_RI_F_XSK_MAP_INIT	BIT(4)
+#define BPF_RI_F_LWT_IP_ENCAP	BIT(5)
 
 struct bpf_redirect_info {
 	u64 tgt_index;
net/core/lwt_bpf.c +30 -0
index da49364ec63d..88664382ca44 100644
@@ -36,10 +36,30 @@ static inline struct bpf_lwt *bpf_lwt_lwtunnel(struct lwtunnel_state *lwt)
 #define NO_REDIRECT false
 #define CAN_REDIRECT true
 
+static void bpf_lwt_reset_cb(struct sk_buff *skb, __be16 orig_proto)
+{
+	if (skb->protocol == orig_proto) {
+		if (skb->protocol == htons(ETH_P_IP))
+			memset(&IPCB(skb)->opt, 0, sizeof(IPCB(skb)->opt));
+		return;
+	}
+
+	if (skb->protocol == htons(ETH_P_IP)) {
+		memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
+		IPCB(skb)->iif = skb->skb_iif;
+	} else if (skb->protocol == htons(ETH_P_IPV6)) {
+		memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
+		IP6CB(skb)->iif = skb->skb_iif;
+	}
+}
+
 static int run_lwt_bpf(struct sk_buff *skb, struct bpf_lwt_prog *lwt,
 		       struct dst_entry *dst, bool can_redirect)
 {
 	struct bpf_net_context __bpf_net_ctx, *bpf_net_ctx;
+	struct bpf_redirect_info *ri;
+	bool lwt_ip_encap, saved_lwt_ip_encap;
+	__be16 orig_proto = skb->protocol;
 	int ret;
 
 	/* Disabling BH is needed to protect per-CPU bpf_redirect_info between
@@ -47,8 +67,17 @@ static int run_lwt_bpf(struct sk_buff *skb, struct bpf_lwt_prog *lwt,
 	 */
 	local_bh_disable();
 	bpf_net_ctx = bpf_net_ctx_set(&__bpf_net_ctx);
+	ri = bpf_net_ctx_get_ri();
+	saved_lwt_ip_encap = ri->kern_flags & BPF_RI_F_LWT_IP_ENCAP;
+	ri->kern_flags &= ~BPF_RI_F_LWT_IP_ENCAP;
 	bpf_compute_data_pointers(skb);
 	ret = bpf_prog_run_save_cb(lwt->prog, skb);
+	lwt_ip_encap = ri->kern_flags & BPF_RI_F_LWT_IP_ENCAP;
+	ri->kern_flags &= ~BPF_RI_F_LWT_IP_ENCAP;
+	if (saved_lwt_ip_encap)
+		ri->kern_flags |= BPF_RI_F_LWT_IP_ENCAP;
+	if (lwt_ip_encap)
+		bpf_lwt_reset_cb(skb, orig_proto);
 
 	switch (ret) {
 	case BPF_OK:
@@ -668,6 +697,7 @@ int bpf_lwt_push_ip_encap(struct sk_buff *skb, void *hdr, u32 len, bool ingress)
 	} else {
 		skb->protocol = htons(ETH_P_IPV6);
 	}
+	bpf_net_ctx_get_ri()->kern_flags |= BPF_RI_F_LWT_IP_ENCAP;
 
 	if (skb_is_gso(skb))
 		return handle_gso_encap(skb, ipv4, len);
Signature · 1 line
2.55.0
#2 lore raw

PATCH v4 2/2 selftests/bpf: cover stale CB after LWT IP encapsulation

In reply to Weiming Shi

Add compact regression coverage to the existing LWT IP encapsulation test. Send an IPv4 ping with a Record Route option through an ingress LWT program which prepends a TTL-one IPv4 header, then observe __icmp_send() with fentry and verify that the restored protocol control block no longer contains the inner header's cached option offsets.

Read ctx->cb[0] in the LWT program so the test exercises bpf_prog_run_save_cb()'s save/restore path before the post-run check.

---
 .../selftests/bpf/prog_tests/lwt_ip_encap.c   | 45 ++++++++++++++++++
 .../selftests/bpf/progs/test_lwt_ip_encap.c   | 47 +++++++++++++++++++
 2 files changed, 92 insertions(+)
tools/testing/selftests/bpf/prog_tests/lwt_ip_encap.c +45 -0
index 39e8a3b8b6af..14ba33eaeb96 100644
@@ -686,3 +686,48 @@ void test_lwt_ip_encap_vxlan_ipv6(void)
 {
 	lwt_ip_encap_vxlan(IPV6_ENCAP);
 }
+
+void test_lwt_ip_encap_stale_cb(void)
+{
+	char ns1[NETNS_NAME_SIZE] = NETNS_BASE "-1-";
+	char ns2[NETNS_NAME_SIZE] = NETNS_BASE "-2-";
+	char ns3[NETNS_NAME_SIZE] = NETNS_BASE "-3-";
+	struct test_lwt_ip_encap *skel = NULL;
+
+	if (!ASSERT_OK(create_ns(ns1, NETNS_NAME_SIZE), "create ns1"))
+		goto out;
+	if (!ASSERT_OK(create_ns(ns2, NETNS_NAME_SIZE), "create ns2"))
+		goto out;
+	if (!ASSERT_OK(create_ns(ns3, NETNS_NAME_SIZE), "create ns3"))
+		goto out;
+	if (!ASSERT_OK(setup_network(ns1, ns2, ns3, ""), "setup network"))
+		goto out;
+
+	skel = test_lwt_ip_encap__open();
+	if (!ASSERT_OK_PTR(skel, "open"))
+		goto out;
+	bpf_program__set_autoload(skel->progs.bpf_lwt_encap_gre, false);
+	bpf_program__set_autoload(skel->progs.bpf_lwt_encap_gre6, false);
+	bpf_program__set_autoload(skel->progs.bpf_lwt_encap_vxlan, false);
+	bpf_program__set_autoload(skel->progs.bpf_lwt_encap_vxlan6, false);
+	bpf_program__set_autoload(skel->progs.bpf_lwt_encap_stale, false);
+	if (!ASSERT_OK(test_lwt_ip_encap__load(skel), "load"))
+		goto out;
+	if (!ASSERT_OK(test_lwt_ip_encap__attach(skel), "attach"))
+		goto out;
+
+	SYS(out, "ip -n %s route replace %s/32 encap bpf in obj %s sec encap_stale dev veth3",
+	    ns2, IP4_ADDR_DST, BPF_FILE);
+	skel->bss->stale_cb_seen = false;
+	skel->bss->stale_cb_cleared = false;
+	SYS_NOFAIL("ip netns exec %s ping -q -R -c 1 -W 1 -I veth1 %s >/dev/null 2>&1",
+		   ns1, IP4_ADDR_DST);
+	ASSERT_TRUE(skel->bss->stale_cb_seen, "stale_cb_seen");
+	ASSERT_TRUE(skel->bss->stale_cb_cleared, "stale_cb_cleared");
+
+out:
+	test_lwt_ip_encap__destroy(skel);
+	SYS_NOFAIL("ip netns del %s", ns1);
+	SYS_NOFAIL("ip netns del %s", ns2);
+	SYS_NOFAIL("ip netns del %s", ns3);
+}
tools/testing/selftests/bpf/progs/test_lwt_ip_encap.c +47 -0

71 lines not on this page · raw message

Signature · 1 line
2.55.0
#3 lore raw

Re: [PATCH v4 2/2] selftests/bpf: cover stale CB after LWT IP encapsulation

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] bpf: bpf_lwt_encap_stale breaks lwt_ip_encap_vxlan test due to auto-loading --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] bpf: bpf_lwt_encap_stale breaks lwt_ip_encap_vxlan test due to auto-loading --

commit 62bf6da3e832e187b8a03919cec2518d27ebceed
Author: Weiming Shi <[email protected]>

selftests/bpf: cover stale CB after LWT IP encapsulation

This commit adds regression coverage to the LWT IP encapsulation test. It verifies that the restored protocol control block no longer contains the inner header's cached option offsets after LWT program execution.

Quoted 4 lines
tools/testing/selftests/bpf/progs/test_lwt_ip_encap.c +0 -0
index 4a934fccf8f5b..8b57c7fb7fa8a 100644

[ ... ]

tools/testing/selftests/bpf/progs/test_lwt_ip_encap.c int bpf_lwt_encap_gre6(struct __sk_buff *skb) · line 88

Quoted 7 lines
tools/testing/selftests/bpf/progs/test_lwt_ip_encap.c +3 -0
@@ -80,6 +83,29 @@ int bpf_lwt_encap_gre6(struct __sk_buff *skb)
 	return BPF_LWT_REROUTE;
 }
 
+SEC("encap_stale")
+int bpf_lwt_encap_stale(struct __sk_buff *skb)
+{

[Severity: Medium]
Will this new program cause the existing lwt_ip_encap_vxlan() test to fail unconditionally?

Since "encap_" is an unknown section name prefix for libbpf, it defaults to the BPF_PROG_TYPE_UNSPEC program type.

The existing lwt_ip_encap_vxlan() test function relies on test_lwt_ip_encap__load(skel) to load the skeleton. It manually disables auto-loading for preexisting programs (like encap_gre and encap_vxlan) but was not updated to disable auto-loading for this newly added encap_stale program.

Consequently, libbpf attempts to load it, sees BPF_PROG_TYPE_UNSPEC, and returns -EINVAL, which immediately fails the lwt_ip_encap_vxlan() test. Should this program's auto-loading be explicitly disabled in the existing tests?

Quoted 5 lines
tools/testing/selftests/bpf/progs/test_lwt_ip_encap.c +5 -0
+	struct iphdr iph = {};
+
+	/* Exercise CB restore before post-run invalidation. */
+	if (skb->cb[0])
+		return BPF_DROP;

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#4 lore raw

Re: [PATCH v4 2/2] selftests/bpf: cover stale CB after LWT IP encapsulation

In reply to Weiming Shi
Could the test confirm that the probe packet actually carries an IP option?
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/lwt_ip_encap.c +0 -0
index 5c5560d45c5b4..ef90dd8d4b87c 100644

[ ... ]

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/lwt_ip_encap.c +3 -0
@@ -739,3 +739,48 @@ void test_lwt_ip_encap_vxlan_ipv6(void)
 {
 	lwt_ip_encap_vxlan(IPV6_ENCAP);
 }
+
+void test_lwt_ip_encap_stale_cb(void)
+{

[ ... ]

tools/testing/selftests/bpf/prog_tests/lwt_ip_encap.c void test_lwt_ip_encap_vxlan_ipv6(void) · line 726

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/lwt_ip_encap.c +4 -0
+	SYS_NOFAIL("ip netns exec %s ping -q -R -c 1 -W 1 -I veth1 %s >/dev/null 2>&1",
+		   ns1, IP4_ADDR_DST);
+	ASSERT_TRUE(skel->bss->stale_cb_seen, "stale_cb_seen");
+	ASSERT_TRUE(skel->bss->stale_cb_cleared, "stale_cb_cleared");

Could the test confirm that the probe packet actually carries an IP option?

Looking at ip_rcv_core() in net/ipv4/ip_input.c, IPCB(skb) is zeroed unconditionally on receive:

  memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));

The opt.optlen and opt.rr fields are only written by ip_rcv_options(), which ip_rcv_finish_core() calls only when the IP header has options:

  if (iph->ihl > 5) {
      drop_reason = ip_rcv_options(skb, dev);

If ping -R does not place a Record Route option on the wire (a ping implementation that accepts but ignores -R, an option stripped en route, or a non-iputils ping), the inner header has ihl == 5, IPCB(skb)->opt stays all-zero through the LWT run, and the fentry hook records stale_cb_cleared = true whether or not bpf_lwt_reset_cb() exists.

The stale_cb_seen flag still becomes true, because the ICMP_TIME_EXCEEDED is produced by the TTL-one outer header the BPF program prepends and does not depend on options at all, so neither assertion catches the missing precondition and the test silently stops guarding the fix.

Could a pre-encapsulation check record whether opt.optlen was non-zero, so the test fails loudly when the probe packet is not what it expects? For example, an fentry or fexit on bpf_lwt_push_ip_encap that reads IPCB(skb)->opt.optlen before the reset, then asserts that value was non-zero.

tools/testing/selftests/bpf/prog_tests/lwt_ip_encap.c void test_lwt_ip_encap_vxlan_ipv6(void) · line 733

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/lwt_ip_encap.c +7 -0
+
+out:
+	test_lwt_ip_encap__destroy(skel);
+	SYS_NOFAIL("ip netns del %s", ns1);
+	SYS_NOFAIL("ip netns del %s", ns2);
+	SYS_NOFAIL("ip netns del %s", ns3);
+}

[ ... ]

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/35634260486

maintainer
#5 lore raw

Re: [PATCH v4 2/2] selftests/bpf: cover stale CB after LWT IP encapsulation

In reply to Weiming Shi
Quoted 11 lines · Weiming Shi

Add compact regression coverage to the existing LWT IP encapsulation test. Send an IPv4 ping with a Record Route option through an ingress LWT program which prepends a TTL-one IPv4 header, then observe __icmp_send() with fentry and verify that the restored protocol control block no longer contains the inner header's cached option offsets.

Read ctx->cb[0] in the LWT program so the test exercises bpf_prog_run_save_cb()'s save/restore path before the post-run check.

Did you /actually/ try to run the test you submitted?

BPF CI :

[...]
lwt_ip_encap_vxlan:PASS:setup network 0 nsec
setup_vxlan_routes:PASS:open ns3 for vxlan 0 nsec
setup_vxlan_routes:PASS:ip a add 172.16.17.100/32 dev veth8 0 nsec
setup_vxlan_routes:PASS:ip -6 a add fb11::1/128 dev veth8 0 nsec
setup_vxlan_routes:PASS:ip link add vxlan4 type vxlan id 1 dstport 4789 local 172.16.17.100 dev veth8 nolearning noudpcsum 0 nsec setup_vxlan_routes:PASS:ip link set vxlan4 up 0 nsec
setup_vxlan_routes:PASS:ip link add vxlan6 type vxlan id 1 dstport 4789 local fb11::1 dev veth8 nolearning udp6zerocsumrx 0 nsec setup_vxlan_routes:PASS:ip link set vxlan6 up 0 nsec
setup_vxlan_routes:PASS:ip -n ns-lwt-ip-encap-1-0000128 route add 172.16.17.100/32 dev veth5 via 172.16.6.100 0 nsec
setup_vxlan_routes:PASS:ip -n ns-lwt-ip-encap-2-0000128 route add 172.16.17.100/32 dev veth7 via 172.16.8.100 0 nsec
setup_vxlan_routes:PASS:ip -n ns-lwt-ip-encap-1-0000128 -6 route add fb11::1/128 dev veth5 via fb06::1 0 nsec setup_vxlan_routes:PASS:ip -n ns-lwt-ip-encap-2-0000128 -6 route add fb11::1/128 dev veth7 via fb08::1 0 nsec lwt_ip_encap_vxlan:PASS:setup vxlan routes 0 nsec
lwt_ip_encap_vxlan:PASS:test_lwt_ip_encap__open 0 nsec
libbpf: prog 'bpf_lwt_encap_stale': missing BPF prog type, check ELF section name 'encap_stale'
libbpf: prog 'bpf_lwt_encap_stale': failed to load: -EINVAL
libbpf: failed to load object 'test_lwt_ip_encap'
libbpf: failed to load BPF skeleton 'test_lwt_ip_encap': -EINVAL lwt_ip_encap_vxlan:FAIL:test_lwt_ip_encap__load unexpected error: -22 (errno 22)