1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2023 Facebook */
3 #include "vmlinux.h"
4 #include "bpf_tracing_net.h"
5 #include <bpf/bpf_helpers.h>
6 #include <bpf/bpf_tracing.h>
7 #include <bpf/bpf_core_read.h>
8
9 void *local_storage_ptr = NULL;
10 void *sk_ptr = NULL;
11 int cookie_found = 0;
12 __u64 cookie = 0;
13 __u32 omem = 0;
14
15 void *bpf_rdonly_cast(void *, __u32) __ksym;
16
17 struct {
18 __uint(type, BPF_MAP_TYPE_SK_STORAGE);
19 __uint(map_flags, BPF_F_NO_PREALLOC);
20 __type(key, int);
21 __type(value, int);
22 } sk_storage SEC(".maps");
23
24 SEC("fexit/bpf_local_storage_destroy")
BPF_PROG(bpf_local_storage_destroy,struct bpf_local_storage * local_storage)25 int BPF_PROG(bpf_local_storage_destroy, struct bpf_local_storage *local_storage)
26 {
27 struct sock *sk;
28
29 if (local_storage_ptr != local_storage)
30 return 0;
31
32 sk = bpf_rdonly_cast(sk_ptr, bpf_core_type_id_kernel(struct sock));
33 if (sk->sk_cookie.counter != cookie)
34 return 0;
35
36 cookie_found++;
37 omem = sk->sk_omem_alloc.counter;
38 local_storage_ptr = NULL;
39
40 return 0;
41 }
42
43 SEC("fentry/inet6_sock_destruct")
BPF_PROG(inet6_sock_destruct,struct sock * sk)44 int BPF_PROG(inet6_sock_destruct, struct sock *sk)
45 {
46 int *value;
47
48 if (!cookie || sk->sk_cookie.counter != cookie)
49 return 0;
50
51 value = bpf_sk_storage_get(&sk_storage, sk, 0, 0);
52 if (value && *value == 0xdeadbeef) {
53 cookie_found++;
54 sk_ptr = sk;
55 local_storage_ptr = sk->sk_bpf_storage;
56 }
57
58 return 0;
59 }
60
61 char _license[] SEC("license") = "GPL";
62