Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 // Copyright (c) 2020 Isovalent, Inc.
0003 #include "vmlinux.h"
0004 #include <bpf/bpf_helpers.h>
0005 
0006 struct {
0007     __uint(type, BPF_MAP_TYPE_SOCKMAP);
0008     __uint(max_entries, 2);
0009     __type(key, __u32);
0010     __type(value, __u64);
0011 } sock_map SEC(".maps");
0012 
0013 struct {
0014     __uint(type, BPF_MAP_TYPE_SOCKHASH);
0015     __uint(max_entries, 2);
0016     __type(key, __u32);
0017     __type(value, __u64);
0018 } sock_hash SEC(".maps");
0019 
0020 struct {
0021     __uint(type, BPF_MAP_TYPE_SK_STORAGE);
0022     __uint(map_flags, BPF_F_NO_PREALLOC);
0023     __type(key, __u32);
0024     __type(value, __u64);
0025 } socket_storage SEC(".maps");
0026 
0027 SEC("sk_msg")
0028 int prog_msg_verdict(struct sk_msg_md *msg)
0029 {
0030     struct task_struct *task = (struct task_struct *)bpf_get_current_task();
0031     int verdict = SK_PASS;
0032     __u32 pid, tpid;
0033     __u64 *sk_stg;
0034 
0035     pid = bpf_get_current_pid_tgid() >> 32;
0036     sk_stg = bpf_sk_storage_get(&socket_storage, msg->sk, 0, BPF_SK_STORAGE_GET_F_CREATE);
0037     if (!sk_stg)
0038         return SK_DROP;
0039     *sk_stg = pid;
0040     bpf_probe_read_kernel(&tpid , sizeof(tpid), &task->tgid);
0041     if (pid != tpid)
0042         verdict = SK_DROP;
0043     bpf_sk_storage_delete(&socket_storage, (void *)msg->sk);
0044     return verdict;
0045 }
0046 
0047 char _license[] SEC("license") = "GPL";