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