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
9void *local_storage_ptr = NULL;
10void *sk_ptr = NULL;
11int cookie_found = 0;
12__u64 cookie = 0;
13__u32 omem = 0;
14
15struct {
16	__uint(type, BPF_MAP_TYPE_SK_STORAGE);
17	__uint(map_flags, BPF_F_NO_PREALLOC);
18	__type(key, int);
19	__type(value, int);
20} sk_storage SEC(".maps");
21
22SEC("fexit/bpf_local_storage_destroy")
23int BPF_PROG(bpf_local_storage_destroy, struct bpf_local_storage *local_storage)
24{
25	struct sock *sk;
26
27	if (local_storage_ptr != local_storage)
28		return 0;
29
30	sk = bpf_core_cast(sk_ptr, struct sock);
31	if (sk->sk_cookie.counter != cookie)
32		return 0;
33
34	cookie_found++;
35	omem = sk->sk_omem_alloc.counter;
36	local_storage_ptr = NULL;
37
38	return 0;
39}
40
41SEC("fentry/inet6_sock_destruct")
42int BPF_PROG(inet6_sock_destruct, struct sock *sk)
43{
44	int *value;
45
46	if (!cookie || sk->sk_cookie.counter != cookie)
47		return 0;
48
49	value = bpf_sk_storage_get(&sk_storage, sk, 0, 0);
50	if (value && *value == 0xdeadbeef) {
51		cookie_found++;
52		sk_ptr = sk;
53		local_storage_ptr = sk->sk_bpf_storage;
54	}
55
56	return 0;
57}
58
59char _license[] SEC("license") = "GPL";
60