sock_flags_kern.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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/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. char fmt2[] = "socket: uid %u gid %u\n";
  12. __u64 gid_uid = bpf_get_current_uid_gid();
  13. __u32 uid = gid_uid & 0xffffffff;
  14. __u32 gid = gid_uid >> 32;
  15. bpf_trace_printk(fmt, sizeof(fmt), sk->family, sk->type, sk->protocol);
  16. bpf_trace_printk(fmt2, sizeof(fmt2), uid, gid);
  17. /* block PF_INET6, SOCK_RAW, IPPROTO_ICMPV6 sockets
  18. * ie., make ping6 fail
  19. */
  20. if (sk->family == PF_INET6 &&
  21. sk->type == SOCK_RAW &&
  22. sk->protocol == IPPROTO_ICMPV6)
  23. return 0;
  24. return 1;
  25. }
  26. SEC("cgroup/sock2")
  27. int bpf_prog2(struct bpf_sock *sk)
  28. {
  29. char fmt[] = "socket: family %d type %d protocol %d\n";
  30. bpf_trace_printk(fmt, sizeof(fmt), sk->family, sk->type, sk->protocol);
  31. /* block PF_INET, SOCK_RAW, IPPROTO_ICMP sockets
  32. * ie., make ping fail
  33. */
  34. if (sk->family == PF_INET &&
  35. sk->type == SOCK_RAW &&
  36. sk->protocol == IPPROTO_ICMP)
  37. return 0;
  38. return 1;
  39. }
  40. char _license[] SEC("license") = "GPL";