1*387f9dfdSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0
2*387f9dfdSAndroid Build Coastguard Worker // Copyright (c) 2021 Yaqi Chen
3*387f9dfdSAndroid Build Coastguard Worker #include <vmlinux.h>
4*387f9dfdSAndroid Build Coastguard Worker #include <bpf/bpf_helpers.h>
5*387f9dfdSAndroid Build Coastguard Worker #include <bpf/bpf_core_read.h>
6*387f9dfdSAndroid Build Coastguard Worker #include <bpf/bpf_tracing.h>
7*387f9dfdSAndroid Build Coastguard Worker #include <bpf/bpf_endian.h>
8*387f9dfdSAndroid Build Coastguard Worker #include "tcpsynbl.h"
9*387f9dfdSAndroid Build Coastguard Worker #include "bits.bpf.h"
10*387f9dfdSAndroid Build Coastguard Worker #include "maps.bpf.h"
11*387f9dfdSAndroid Build Coastguard Worker
12*387f9dfdSAndroid Build Coastguard Worker #define MAX_ENTRIES 65536
13*387f9dfdSAndroid Build Coastguard Worker
14*387f9dfdSAndroid Build Coastguard Worker struct {
15*387f9dfdSAndroid Build Coastguard Worker __uint(type, BPF_MAP_TYPE_HASH);
16*387f9dfdSAndroid Build Coastguard Worker __uint(max_entries, MAX_ENTRIES);
17*387f9dfdSAndroid Build Coastguard Worker __type(key, u64);
18*387f9dfdSAndroid Build Coastguard Worker __type(value, struct hist);
19*387f9dfdSAndroid Build Coastguard Worker } hists SEC(".maps");
20*387f9dfdSAndroid Build Coastguard Worker
21*387f9dfdSAndroid Build Coastguard Worker static struct hist zero;
22*387f9dfdSAndroid Build Coastguard Worker
do_entry(struct sock * sk)23*387f9dfdSAndroid Build Coastguard Worker static int do_entry(struct sock *sk)
24*387f9dfdSAndroid Build Coastguard Worker {
25*387f9dfdSAndroid Build Coastguard Worker u64 max_backlog, backlog, slot;
26*387f9dfdSAndroid Build Coastguard Worker struct hist *histp;
27*387f9dfdSAndroid Build Coastguard Worker
28*387f9dfdSAndroid Build Coastguard Worker max_backlog = BPF_CORE_READ(sk, sk_max_ack_backlog);
29*387f9dfdSAndroid Build Coastguard Worker backlog = BPF_CORE_READ(sk, sk_ack_backlog);
30*387f9dfdSAndroid Build Coastguard Worker histp = bpf_map_lookup_or_try_init(&hists, &max_backlog, &zero);
31*387f9dfdSAndroid Build Coastguard Worker if (!histp)
32*387f9dfdSAndroid Build Coastguard Worker return 0;
33*387f9dfdSAndroid Build Coastguard Worker
34*387f9dfdSAndroid Build Coastguard Worker slot = log2l(backlog);
35*387f9dfdSAndroid Build Coastguard Worker if (slot >= MAX_SLOTS)
36*387f9dfdSAndroid Build Coastguard Worker slot = MAX_SLOTS - 1;
37*387f9dfdSAndroid Build Coastguard Worker __sync_fetch_and_add(&histp->slots[slot], 1);
38*387f9dfdSAndroid Build Coastguard Worker return 0;
39*387f9dfdSAndroid Build Coastguard Worker }
40*387f9dfdSAndroid Build Coastguard Worker
41*387f9dfdSAndroid Build Coastguard Worker
42*387f9dfdSAndroid Build Coastguard Worker SEC("kprobe/tcp_v4_syn_recv_sock")
BPF_KPROBE(tcp_v4_syn_recv_kprobe,struct sock * sk)43*387f9dfdSAndroid Build Coastguard Worker int BPF_KPROBE(tcp_v4_syn_recv_kprobe, struct sock *sk)
44*387f9dfdSAndroid Build Coastguard Worker {
45*387f9dfdSAndroid Build Coastguard Worker return do_entry(sk);
46*387f9dfdSAndroid Build Coastguard Worker }
47*387f9dfdSAndroid Build Coastguard Worker
48*387f9dfdSAndroid Build Coastguard Worker SEC("kprobe/tcp_v6_syn_recv_sock")
BPF_KPROBE(tcp_v6_syn_recv_kprobe,struct sock * sk)49*387f9dfdSAndroid Build Coastguard Worker int BPF_KPROBE(tcp_v6_syn_recv_kprobe, struct sock *sk)
50*387f9dfdSAndroid Build Coastguard Worker {
51*387f9dfdSAndroid Build Coastguard Worker return do_entry(sk);
52*387f9dfdSAndroid Build Coastguard Worker }
53*387f9dfdSAndroid Build Coastguard Worker
54*387f9dfdSAndroid Build Coastguard Worker SEC("fentry/tcp_v4_syn_recv_sock")
BPF_PROG(tcp_v4_syn_recv,struct sock * sk)55*387f9dfdSAndroid Build Coastguard Worker int BPF_PROG(tcp_v4_syn_recv, struct sock *sk)
56*387f9dfdSAndroid Build Coastguard Worker {
57*387f9dfdSAndroid Build Coastguard Worker return do_entry(sk);
58*387f9dfdSAndroid Build Coastguard Worker }
59*387f9dfdSAndroid Build Coastguard Worker
60*387f9dfdSAndroid Build Coastguard Worker SEC("fentry/tcp_v6_syn_recv_sock")
BPF_PROG(tcp_v6_syn_recv,struct sock * sk)61*387f9dfdSAndroid Build Coastguard Worker int BPF_PROG(tcp_v6_syn_recv, struct sock *sk)
62*387f9dfdSAndroid Build Coastguard Worker {
63*387f9dfdSAndroid Build Coastguard Worker return do_entry(sk);
64*387f9dfdSAndroid Build Coastguard Worker }
65*387f9dfdSAndroid Build Coastguard Worker
66*387f9dfdSAndroid Build Coastguard Worker char LICENSE[] SEC("license") = "Dual BSD/GPL";
67