xref: /linux/tools/testing/selftests/bpf/progs/sockmap_verdict_prog.c (revision 3d0fe49454652117522f60bfbefb978ba0e5300b)
1 #include <linux/bpf.h>
2 #include <bpf/bpf_helpers.h>
3 #include <bpf/bpf_endian.h>
4 #include "bpf_misc.h"
5 
6 struct {
7 	__uint(type, BPF_MAP_TYPE_SOCKMAP);
8 	__uint(max_entries, 20);
9 	__type(key, int);
10 	__type(value, int);
11 } sock_map_rx SEC(".maps");
12 
13 struct {
14 	__uint(type, BPF_MAP_TYPE_SOCKMAP);
15 	__uint(max_entries, 20);
16 	__type(key, int);
17 	__type(value, int);
18 } sock_map_tx SEC(".maps");
19 
20 struct {
21 	__uint(type, BPF_MAP_TYPE_SOCKMAP);
22 	__uint(max_entries, 20);
23 	__type(key, int);
24 	__type(value, int);
25 } sock_map_msg SEC(".maps");
26 
27 struct {
28 	__uint(type, BPF_MAP_TYPE_ARRAY);
29 	__uint(max_entries, 20);
30 	__type(key, int);
31 	__type(value, int);
32 } sock_map_break SEC(".maps");
33 
34 SEC("sk_skb2")
35 int bpf_prog2(struct __sk_buff *skb)
36 {
37 	void *data_end = (void *)(long) skb->data_end;
38 	void *data = (void *)(long) skb->data;
39 	__u32 lport = skb->local_port;
40 	__u32 rport = skb->remote_port;
41 	__u8 *d = data;
42 	__u8 sk, map;
43 
44 	__sink(lport);
45 	__sink(rport);
46 
47 	if (data + 8 > data_end)
48 		return SK_DROP;
49 
50 	map = d[0];
51 	sk = d[1];
52 
53 	d[0] = 0xd;
54 	d[1] = 0xe;
55 	d[2] = 0xa;
56 	d[3] = 0xd;
57 	d[4] = 0xb;
58 	d[5] = 0xe;
59 	d[6] = 0xe;
60 	d[7] = 0xf;
61 
62 	if (!map)
63 		return bpf_sk_redirect_map(skb, &sock_map_rx, sk, 0);
64 	return bpf_sk_redirect_map(skb, &sock_map_tx, sk, 0);
65 }
66 
67 char _license[] SEC("license") = "GPL";
68