1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2020 Isovalent, Inc.
3 #include "vmlinux.h"
4 #include <bpf/bpf_helpers.h>
5
6 struct {
7 __uint(type, BPF_MAP_TYPE_SOCKMAP);
8 __uint(max_entries, 2);
9 __type(key, __u32);
10 __type(value, __u64);
11 } sock_map SEC(".maps");
12
13 struct {
14 __uint(type, BPF_MAP_TYPE_SOCKHASH);
15 __uint(max_entries, 2);
16 __type(key, __u32);
17 __type(value, __u64);
18 } sock_hash SEC(".maps");
19
20 struct {
21 __uint(type, BPF_MAP_TYPE_SK_STORAGE);
22 __uint(map_flags, BPF_F_NO_PREALLOC);
23 __type(key, __u32);
24 __type(value, __u64);
25 } socket_storage SEC(".maps");
26
27 SEC("sk_msg")
prog_msg_verdict(struct sk_msg_md * msg)28 int prog_msg_verdict(struct sk_msg_md *msg)
29 {
30 struct task_struct *task = (struct task_struct *)bpf_get_current_task();
31 int verdict = SK_PASS;
32 __u32 pid, tpid;
33 __u64 *sk_stg;
34
35 pid = bpf_get_current_pid_tgid() >> 32;
36 sk_stg = bpf_sk_storage_get(&socket_storage, msg->sk, 0, BPF_SK_STORAGE_GET_F_CREATE);
37 if (!sk_stg)
38 return SK_DROP;
39 *sk_stg = pid;
40 bpf_probe_read_kernel(&tpid , sizeof(tpid), &task->tgid);
41 if (pid != tpid)
42 verdict = SK_DROP;
43 bpf_sk_storage_delete(&socket_storage, (void *)msg->sk);
44 return verdict;
45 }
46
47 char _license[] SEC("license") = "GPL";
48