sock_flags_kern.c 986 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include <uapi/linux/bpf.h>
  2. #include <linux/socket.h>
  3. #include <linux/net.h>
  4. #include <uapi/linux/in.h>
  5. #include <uapi/linux/in6.h>
  6. #include "bpf_helpers.h"
  7. SEC("cgroup/sock1")
  8. int bpf_prog1(struct bpf_sock *sk)
  9. {
  10. char fmt[] = "socket: family %d type %d protocol %d\n";
  11. bpf_trace_printk(fmt, sizeof(fmt), sk->family, sk->type, sk->protocol);
  12. /* block PF_INET6, SOCK_RAW, IPPROTO_ICMPV6 sockets
  13. * ie., make ping6 fail
  14. */
  15. if (sk->family == PF_INET6 &&
  16. sk->type == SOCK_RAW &&
  17. sk->protocol == IPPROTO_ICMPV6)
  18. return 0;
  19. return 1;
  20. }
  21. SEC("cgroup/sock2")
  22. int bpf_prog2(struct bpf_sock *sk)
  23. {
  24. char fmt[] = "socket: family %d type %d protocol %d\n";
  25. bpf_trace_printk(fmt, sizeof(fmt), sk->family, sk->type, sk->protocol);
  26. /* block PF_INET, SOCK_RAW, IPPROTO_ICMP sockets
  27. * ie., make ping fail
  28. */
  29. if (sk->family == PF_INET &&
  30. sk->type == SOCK_RAW &&
  31. sk->protocol == IPPROTO_ICMP)
  32. return 0;
  33. return 1;
  34. }
  35. char _license[] SEC("license") = "GPL";