1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2020 Facebook
3 
4 #include <linux/stddef.h>
5 #include <linux/bpf.h>
6 #include <linux/pkt_cls.h>
7 #include <bpf/bpf_endian.h>
8 #include <bpf/bpf_helpers.h>
9 
10 struct {
11 	__uint(type, BPF_MAP_TYPE_SOCKMAP);
12 	__type(key, int);
13 	__type(value, int);
14 	__uint(max_entries, 2);
15 } sock_map SEC(".maps");
16 
17 SEC("freplace/cls_redirect")
freplace_cls_redirect_test(struct __sk_buff * skb)18 int freplace_cls_redirect_test(struct __sk_buff *skb)
19 {
20 	int ret = 0;
21 	const int zero = 0;
22 	struct bpf_sock *sk;
23 
24 	sk = bpf_map_lookup_elem(&sock_map, &zero);
25 	if (!sk)
26 		return TC_ACT_SHOT;
27 
28 	ret = bpf_map_update_elem(&sock_map, &zero, sk, 0);
29 	bpf_sk_release(sk);
30 
31 	return ret == 0 ? TC_ACT_OK : TC_ACT_SHOT;
32 }
33 
34 char _license[] SEC("license") = "GPL";
35