summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/bpf/progs/test_dst_clear.c
blob: c22a6eeb479823adaf09cc70645d69b6e0b9138f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */

#include "vmlinux.h"
#include "bpf_tracing_net.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_endian.h>

#define UDP_TEST_PORT 7777

void *bpf_cast_to_kern_ctx(void *) __ksym;

bool had_dst = false;
bool dst_cleared = false;

SEC("tc/egress")
int dst_clear(struct __sk_buff *skb)
{
	struct sk_buff *kskb;
	struct iphdr iph;
	struct udphdr udph;
	int err;

	if (skb->protocol != __bpf_constant_htons(ETH_P_IP))
		return TC_ACT_OK;

	if (bpf_skb_load_bytes(skb, ETH_HLEN, &iph, sizeof(iph)))
		return TC_ACT_OK;

	if (iph.protocol != IPPROTO_UDP)
		return TC_ACT_OK;

	if (bpf_skb_load_bytes(skb, ETH_HLEN + sizeof(iph), &udph, sizeof(udph)))
		return TC_ACT_OK;

	if (udph.dest != __bpf_constant_htons(UDP_TEST_PORT))
		return TC_ACT_OK;

	kskb = bpf_cast_to_kern_ctx(skb);
	had_dst = (kskb->_skb_refdst != 0);

	/* Same-protocol encap (IPIP): protocol stays IPv4, but the dst
	 * from the original routing is no longer valid for the outer hdr.
	 */
	err = bpf_skb_adjust_room(skb, (s32)sizeof(struct iphdr),
				  BPF_ADJ_ROOM_MAC,
				  BPF_F_ADJ_ROOM_FIXED_GSO |
				  BPF_F_ADJ_ROOM_ENCAP_L3_IPV4);
	if (err)
		return TC_ACT_SHOT;

	dst_cleared = (kskb->_skb_refdst == 0);

	return TC_ACT_SHOT;
}

char __license[] SEC("license") = "GPL";