John Fastabend | 6f6d33f | 2017-08-15 22:34:22 -0700 | [diff] [blame] | 1 | #include <linux/bpf.h> |
| 2 | #include "bpf_helpers.h" |
| 3 | #include "bpf_util.h" |
| 4 | #include "bpf_endian.h" |
| 5 | |
| 6 | int _version SEC("version") = 1; |
| 7 | |
| 8 | #define bpf_printk(fmt, ...) \ |
| 9 | ({ \ |
| 10 | char ____fmt[] = fmt; \ |
| 11 | bpf_trace_printk(____fmt, sizeof(____fmt), \ |
| 12 | ##__VA_ARGS__); \ |
| 13 | }) |
| 14 | |
| 15 | SEC("sk_skb1") |
| 16 | int bpf_prog1(struct __sk_buff *skb) |
| 17 | { |
| 18 | void *data_end = (void *)(long) skb->data_end; |
| 19 | void *data = (void *)(long) skb->data; |
| 20 | __u32 lport = skb->local_port; |
| 21 | __u32 rport = skb->remote_port; |
John Fastabend | 6fd2886 | 2017-08-28 07:11:05 -0700 | [diff] [blame] | 22 | __u8 *d = data; |
John Fastabend | 82a8616 | 2018-03-18 12:57:31 -0700 | [diff] [blame^] | 23 | __u32 len = (__u32) data_end - (__u32) data; |
| 24 | int err; |
John Fastabend | 6f6d33f | 2017-08-15 22:34:22 -0700 | [diff] [blame] | 25 | |
John Fastabend | 82a8616 | 2018-03-18 12:57:31 -0700 | [diff] [blame^] | 26 | if (data + 10 > data_end) { |
| 27 | err = bpf_skb_pull_data(skb, 10); |
| 28 | if (err) |
| 29 | return SK_DROP; |
| 30 | |
| 31 | data_end = (void *)(long)skb->data_end; |
| 32 | data = (void *)(long)skb->data; |
| 33 | if (data + 10 > data_end) |
| 34 | return SK_DROP; |
| 35 | } |
John Fastabend | 6f6d33f | 2017-08-15 22:34:22 -0700 | [diff] [blame] | 36 | |
| 37 | /* This write/read is a bit pointless but tests the verifier and |
| 38 | * strparser handler for read/write pkt data and access into sk |
| 39 | * fields. |
| 40 | */ |
John Fastabend | 82a8616 | 2018-03-18 12:57:31 -0700 | [diff] [blame^] | 41 | d = data; |
John Fastabend | 6fd2886 | 2017-08-28 07:11:05 -0700 | [diff] [blame] | 42 | d[7] = 1; |
John Fastabend | 6f6d33f | 2017-08-15 22:34:22 -0700 | [diff] [blame] | 43 | return skb->len; |
| 44 | } |
| 45 | |
| 46 | char _license[] SEC("license") = "GPL"; |