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 bpf_map_def SEC("maps") sock_map = { 11 .type = BPF_MAP_TYPE_SOCKMAP, 12 .key_size = sizeof(int), 13 .value_size = sizeof(int), 14 .max_entries = 2, 15 }; 16 17 SEC("freplace/cls_redirect") 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