xref: /openbmc/linux/tools/testing/selftests/bpf/progs/load_bytes_relative.c (revision 4b4193256c8d3bc3a5397b5cd9494c2ad386317d)
1*bd6fecb9SYiFei Zhu // SPDX-License-Identifier: GPL-2.0-only
2*bd6fecb9SYiFei Zhu 
3*bd6fecb9SYiFei Zhu /*
4*bd6fecb9SYiFei Zhu  * Copyright 2020 Google LLC.
5*bd6fecb9SYiFei Zhu  */
6*bd6fecb9SYiFei Zhu 
7*bd6fecb9SYiFei Zhu #include <errno.h>
8*bd6fecb9SYiFei Zhu #include <linux/bpf.h>
9*bd6fecb9SYiFei Zhu #include <linux/if_ether.h>
10*bd6fecb9SYiFei Zhu #include <linux/ip.h>
11*bd6fecb9SYiFei Zhu #include <bpf/bpf_helpers.h>
12*bd6fecb9SYiFei Zhu 
13*bd6fecb9SYiFei Zhu struct {
14*bd6fecb9SYiFei Zhu 	__uint(type, BPF_MAP_TYPE_ARRAY);
15*bd6fecb9SYiFei Zhu 	__uint(max_entries, 1);
16*bd6fecb9SYiFei Zhu 	__type(key, __u32);
17*bd6fecb9SYiFei Zhu 	__type(value, __u32);
18*bd6fecb9SYiFei Zhu } test_result SEC(".maps");
19*bd6fecb9SYiFei Zhu 
20*bd6fecb9SYiFei Zhu SEC("cgroup_skb/egress")
load_bytes_relative(struct __sk_buff * skb)21*bd6fecb9SYiFei Zhu int load_bytes_relative(struct __sk_buff *skb)
22*bd6fecb9SYiFei Zhu {
23*bd6fecb9SYiFei Zhu 	struct ethhdr eth;
24*bd6fecb9SYiFei Zhu 	struct iphdr iph;
25*bd6fecb9SYiFei Zhu 
26*bd6fecb9SYiFei Zhu 	__u32 map_key = 0;
27*bd6fecb9SYiFei Zhu 	__u32 test_passed = 0;
28*bd6fecb9SYiFei Zhu 
29*bd6fecb9SYiFei Zhu 	/* MAC header is not set by the time cgroup_skb/egress triggers */
30*bd6fecb9SYiFei Zhu 	if (bpf_skb_load_bytes_relative(skb, 0, &eth, sizeof(eth),
31*bd6fecb9SYiFei Zhu 					BPF_HDR_START_MAC) != -EFAULT)
32*bd6fecb9SYiFei Zhu 		goto fail;
33*bd6fecb9SYiFei Zhu 
34*bd6fecb9SYiFei Zhu 	if (bpf_skb_load_bytes_relative(skb, 0, &iph, sizeof(iph),
35*bd6fecb9SYiFei Zhu 					BPF_HDR_START_NET))
36*bd6fecb9SYiFei Zhu 		goto fail;
37*bd6fecb9SYiFei Zhu 
38*bd6fecb9SYiFei Zhu 	if (bpf_skb_load_bytes_relative(skb, 0xffff, &iph, sizeof(iph),
39*bd6fecb9SYiFei Zhu 					BPF_HDR_START_NET) != -EFAULT)
40*bd6fecb9SYiFei Zhu 		goto fail;
41*bd6fecb9SYiFei Zhu 
42*bd6fecb9SYiFei Zhu 	test_passed = 1;
43*bd6fecb9SYiFei Zhu 
44*bd6fecb9SYiFei Zhu fail:
45*bd6fecb9SYiFei Zhu 	bpf_map_update_elem(&test_result, &map_key, &test_passed, BPF_ANY);
46*bd6fecb9SYiFei Zhu 
47*bd6fecb9SYiFei Zhu 	return 1;
48*bd6fecb9SYiFei Zhu }
49