1// SPDX-License-Identifier: GPL-2.0 2/* Copyright (c) 2020 Google LLC. */ 3#include "bpf_iter.h" 4#include <bpf/bpf_helpers.h> 5#include <bpf/bpf_tracing.h> 6 7char _license[] SEC("license") = "GPL"; 8 9struct { 10 __uint(type, BPF_MAP_TYPE_SK_STORAGE); 11 __uint(map_flags, BPF_F_NO_PREALLOC); 12 __type(key, int); 13 __type(value, int); 14} sk_stg_map SEC(".maps"); 15 16SEC("iter/bpf_sk_storage_map") 17int delete_bpf_sk_storage_map(struct bpf_iter__bpf_sk_storage_map *ctx) 18{ 19 if (ctx->sk) 20 bpf_sk_storage_delete(&sk_stg_map, ctx->sk); 21 22 return 0; 23} 24 25SEC("iter/task_file") 26int fill_socket_owner(struct bpf_iter__task_file *ctx) 27{ 28 struct task_struct *task = ctx->task; 29 struct file *file = ctx->file; 30 struct socket *sock; 31 int *sock_tgid; 32 33 if (!task || !file) 34 return 0; 35 36 sock = bpf_sock_from_file(file); 37 if (!sock) 38 return 0; 39 40 sock_tgid = bpf_sk_storage_get(&sk_stg_map, sock->sk, 0, 0); 41 if (!sock_tgid) 42 return 0; 43 44 *sock_tgid = task->tgid; 45 46 return 0; 47} 48 49SEC("iter/tcp") 50int negate_socket_local_storage(struct bpf_iter__tcp *ctx) 51{ 52 struct sock_common *sk_common = ctx->sk_common; 53 int *sock_tgid; 54 55 if (!sk_common) 56 return 0; 57 58 sock_tgid = bpf_sk_storage_get(&sk_stg_map, sk_common, 0, 0); 59 if (!sock_tgid) 60 return 0; 61 62 *sock_tgid = -*sock_tgid; 63 64 return 0; 65} 66