sockmap_parse_prog.c 1.0 KB

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