twfw.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2021 Facebook */
  3. #include <linux/types.h>
  4. #include <bpf/bpf_helpers.h>
  5. #include <linux/bpf.h>
  6. #include <stdint.h>
  7. #define TWFW_MAX_TIERS (64)
  8. /*
  9. * load is successful
  10. * #define TWFW_MAX_TIERS (64u)$
  11. */
  12. struct twfw_tier_value {
  13. unsigned long mask[1];
  14. };
  15. struct rule {
  16. uint8_t seqnum;
  17. };
  18. struct rules_map {
  19. __uint(type, BPF_MAP_TYPE_ARRAY);
  20. __type(key, __u32);
  21. __type(value, struct rule);
  22. __uint(max_entries, 1);
  23. };
  24. struct tiers_map {
  25. __uint(type, BPF_MAP_TYPE_ARRAY);
  26. __type(key, __u32);
  27. __type(value, struct twfw_tier_value);
  28. __uint(max_entries, 1);
  29. };
  30. struct rules_map rules SEC(".maps");
  31. struct tiers_map tiers SEC(".maps");
  32. SEC("cgroup_skb/ingress")
  33. int twfw_verifier(struct __sk_buff* skb)
  34. {
  35. const uint32_t key = 0;
  36. const struct twfw_tier_value* tier = bpf_map_lookup_elem(&tiers, &key);
  37. if (!tier)
  38. return 1;
  39. struct rule* rule = bpf_map_lookup_elem(&rules, &key);
  40. if (!rule)
  41. return 1;
  42. if (rule && rule->seqnum < TWFW_MAX_TIERS) {
  43. /* rule->seqnum / 64 should always be 0 */
  44. unsigned long mask = tier->mask[rule->seqnum / 64];
  45. if (mask)
  46. return 0;
  47. }
  48. return 1;
  49. }