1// SPDX-License-Identifier: GPL-2.0 2/* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */ 3#include "vmlinux.h" 4#include <bpf/bpf_helpers.h> 5#include <bpf/bpf_tracing.h> 6#include <bpf/bpf_core_read.h> 7#include "bpf_kfuncs.h" 8 9struct { 10 __uint(type, BPF_MAP_TYPE_TASK_STORAGE); 11 __uint(map_flags, BPF_F_NO_PREALLOC); 12 __type(key, int); 13 __type(value, long); 14} enter_id SEC(".maps"); 15 16#define IFNAMSIZ 16 17 18int ifindex, ingress_ifindex; 19char name[IFNAMSIZ]; 20unsigned int inum; 21unsigned int meta_len, frag0_len, kskb_len, kskb2_len; 22 23SEC("?xdp") 24int md_xdp(struct xdp_md *ctx) 25{ 26 struct xdp_buff *kctx = bpf_cast_to_kern_ctx(ctx); 27 struct net_device *dev; 28 29 dev = kctx->rxq->dev; 30 ifindex = dev->ifindex; 31 inum = dev->nd_net.net->ns.inum; 32 __builtin_memcpy(name, dev->name, IFNAMSIZ); 33 ingress_ifindex = ctx->ingress_ifindex; 34 return XDP_PASS; 35} 36 37SEC("?tc") 38int md_skb(struct __sk_buff *skb) 39{ 40 struct sk_buff *kskb = bpf_cast_to_kern_ctx(skb); 41 struct skb_shared_info *shared_info; 42 struct sk_buff *kskb2; 43 44 kskb_len = kskb->len; 45 46 /* Simulate the following kernel macro: 47 * #define skb_shinfo(SKB) ((struct skb_shared_info *)(skb_end_pointer(SKB))) 48 */ 49 shared_info = bpf_core_cast(kskb->head + kskb->end, struct skb_shared_info); 50 meta_len = shared_info->meta_len; 51 frag0_len = shared_info->frag_list->len; 52 53 /* kskb2 should be equal to kskb */ 54 kskb2 = bpf_core_cast(kskb, typeof(*kskb2)); 55 kskb2_len = kskb2->len; 56 return 0; 57} 58 59SEC("?tp_btf/sys_enter") 60int BPF_PROG(untrusted_ptr, struct pt_regs *regs, long id) 61{ 62 struct task_struct *task, *task_dup; 63 64 task = bpf_get_current_task_btf(); 65 task_dup = bpf_core_cast(task, struct task_struct); 66 (void)bpf_task_storage_get(&enter_id, task_dup, 0, 0); 67 return 0; 68} 69 70SEC("?tracepoint/syscalls/sys_enter_nanosleep") 71int kctx_u64(void *ctx) 72{ 73 u64 *kctx = bpf_core_cast(ctx, u64); 74 75 (void)kctx; 76 return 0; 77} 78 79char _license[] SEC("license") = "GPL"; 80