^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* Copyright (c) 2013-2015 PLUMgrid, http://plumgrid.com
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * This program is free software; you can redistribute it and/or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * modify it under the terms of version 2 of the GNU General Public
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * License as published by the Free Software Foundation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <uapi/linux/bpf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/version.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <bpf/bpf_helpers.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <bpf/bpf_tracing.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #define _(P) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) ({ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) typeof(P) val = 0; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) bpf_probe_read_kernel(&val, sizeof(val), &(P)); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) val; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) })
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) /* kprobe is NOT a stable ABI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * kernel functions can be removed, renamed or completely change semantics.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * Number of arguments and their positions can change, etc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * In such case this bpf+kprobe example will no longer be meaningful
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) SEC("kprobe/__netif_receive_skb_core")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) int bpf_prog1(struct pt_regs *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) /* attaches to kprobe __netif_receive_skb_core,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * looks for packets on loobpack device and prints them
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) char devname[IFNAMSIZ];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) /* non-portable! works for the given kernel only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) bpf_probe_read_kernel(&skb, sizeof(skb), (void *)PT_REGS_PARM1(ctx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) dev = _(skb->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) len = _(skb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) bpf_probe_read_kernel(devname, sizeof(devname), dev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) if (devname[0] == 'l' && devname[1] == 'o') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) char fmt[] = "skb %p len %d\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) /* using bpf_trace_printk() for DEBUG ONLY */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) bpf_trace_printk(fmt, sizeof(fmt), skb, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) char _license[] SEC("license") = "GPL";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) u32 _version SEC("version") = LINUX_VERSION_CODE;