xref: /linux/tools/testing/selftests/bpf/progs/sockmap_parse_prog.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1bd4aed0eSJiong Wang #include <linux/bpf.h>
2*3e689141SToke Høiland-Jørgensen #include <bpf/bpf_helpers.h>
3*3e689141SToke Høiland-Jørgensen #include <bpf/bpf_endian.h>
4bd4aed0eSJiong Wang 
5bd4aed0eSJiong Wang SEC("sk_skb1")
bpf_prog1(struct __sk_buff * skb)6bd4aed0eSJiong Wang int bpf_prog1(struct __sk_buff *skb)
7bd4aed0eSJiong Wang {
8bd4aed0eSJiong Wang 	void *data_end = (void *)(long) skb->data_end;
9bd4aed0eSJiong Wang 	void *data = (void *)(long) skb->data;
10bd4aed0eSJiong Wang 	__u8 *d = data;
11bd4aed0eSJiong Wang 	int err;
12bd4aed0eSJiong Wang 
13bd4aed0eSJiong Wang 	if (data + 10 > data_end) {
14bd4aed0eSJiong Wang 		err = bpf_skb_pull_data(skb, 10);
15bd4aed0eSJiong Wang 		if (err)
16bd4aed0eSJiong Wang 			return SK_DROP;
17bd4aed0eSJiong Wang 
18bd4aed0eSJiong Wang 		data_end = (void *)(long)skb->data_end;
19bd4aed0eSJiong Wang 		data = (void *)(long)skb->data;
20bd4aed0eSJiong Wang 		if (data + 10 > data_end)
21bd4aed0eSJiong Wang 			return SK_DROP;
22bd4aed0eSJiong Wang 	}
23bd4aed0eSJiong Wang 
24bd4aed0eSJiong Wang 	/* This write/read is a bit pointless but tests the verifier and
25bd4aed0eSJiong Wang 	 * strparser handler for read/write pkt data and access into sk
26bd4aed0eSJiong Wang 	 * fields.
27bd4aed0eSJiong Wang 	 */
28bd4aed0eSJiong Wang 	d = data;
29bd4aed0eSJiong Wang 	d[7] = 1;
30bd4aed0eSJiong Wang 	return skb->len;
31bd4aed0eSJiong Wang }
32bd4aed0eSJiong Wang 
33bd4aed0eSJiong Wang char _license[] SEC("license") = "GPL";
34