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