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