xt_bpf.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Xtables module to match packets using a BPF filter.
  3. * Copyright 2013 Google Inc.
  4. * Written by Willem de Bruijn <willemb@google.com>
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/module.h>
  8. #include <linux/syscalls.h>
  9. #include <linux/skbuff.h>
  10. #include <linux/filter.h>
  11. #include <linux/bpf.h>
  12. #include <linux/netfilter/xt_bpf.h>
  13. #include <linux/netfilter/x_tables.h>
  14. MODULE_AUTHOR("Willem de Bruijn <willemb@google.com>");
  15. MODULE_DESCRIPTION("Xtables: BPF filter match");
  16. MODULE_LICENSE("GPL");
  17. MODULE_ALIAS("ipt_bpf");
  18. MODULE_ALIAS("ip6t_bpf");
  19. static int __bpf_mt_check_bytecode(struct sock_filter *insns, __u16 len,
  20. struct bpf_prog **ret)
  21. {
  22. struct sock_fprog_kern program;
  23. if (len > XT_BPF_MAX_NUM_INSTR)
  24. return -EINVAL;
  25. program.len = len;
  26. program.filter = insns;
  27. if (bpf_prog_create(ret, &program)) {
  28. pr_info_ratelimited("check failed: parse error\n");
  29. return -EINVAL;
  30. }
  31. return 0;
  32. }
  33. static int __bpf_mt_check_fd(int fd, struct bpf_prog **ret)
  34. {
  35. struct bpf_prog *prog;
  36. prog = bpf_prog_get_type(fd, BPF_PROG_TYPE_SOCKET_FILTER);
  37. if (IS_ERR(prog))
  38. return PTR_ERR(prog);
  39. *ret = prog;
  40. return 0;
  41. }
  42. static int __bpf_mt_check_path(const char *path, struct bpf_prog **ret)
  43. {
  44. if (strnlen(path, XT_BPF_PATH_MAX) == XT_BPF_PATH_MAX)
  45. return -EINVAL;
  46. *ret = bpf_prog_get_type_path(path, BPF_PROG_TYPE_SOCKET_FILTER);
  47. return PTR_ERR_OR_ZERO(*ret);
  48. }
  49. static int bpf_mt_check(const struct xt_mtchk_param *par)
  50. {
  51. struct xt_bpf_info *info = par->matchinfo;
  52. return __bpf_mt_check_bytecode(info->bpf_program,
  53. info->bpf_program_num_elem,
  54. &info->filter);
  55. }
  56. static int bpf_mt_check_v1(const struct xt_mtchk_param *par)
  57. {
  58. struct xt_bpf_info_v1 *info = par->matchinfo;
  59. if (info->mode == XT_BPF_MODE_BYTECODE)
  60. return __bpf_mt_check_bytecode(info->bpf_program,
  61. info->bpf_program_num_elem,
  62. &info->filter);
  63. else if (info->mode == XT_BPF_MODE_FD_ELF)
  64. return __bpf_mt_check_fd(info->fd, &info->filter);
  65. else if (info->mode == XT_BPF_MODE_PATH_PINNED)
  66. return __bpf_mt_check_path(info->path, &info->filter);
  67. else
  68. return -EINVAL;
  69. }
  70. static bool bpf_mt(const struct sk_buff *skb, struct xt_action_param *par)
  71. {
  72. const struct xt_bpf_info *info = par->matchinfo;
  73. return BPF_PROG_RUN(info->filter, skb);
  74. }
  75. static bool bpf_mt_v1(const struct sk_buff *skb, struct xt_action_param *par)
  76. {
  77. const struct xt_bpf_info_v1 *info = par->matchinfo;
  78. return !!bpf_prog_run_save_cb(info->filter, (struct sk_buff *) skb);
  79. }
  80. static void bpf_mt_destroy(const struct xt_mtdtor_param *par)
  81. {
  82. const struct xt_bpf_info *info = par->matchinfo;
  83. bpf_prog_destroy(info->filter);
  84. }
  85. static void bpf_mt_destroy_v1(const struct xt_mtdtor_param *par)
  86. {
  87. const struct xt_bpf_info_v1 *info = par->matchinfo;
  88. bpf_prog_destroy(info->filter);
  89. }
  90. static struct xt_match bpf_mt_reg[] __read_mostly = {
  91. {
  92. .name = "bpf",
  93. .revision = 0,
  94. .family = NFPROTO_UNSPEC,
  95. .checkentry = bpf_mt_check,
  96. .match = bpf_mt,
  97. .destroy = bpf_mt_destroy,
  98. .matchsize = sizeof(struct xt_bpf_info),
  99. .usersize = offsetof(struct xt_bpf_info, filter),
  100. .me = THIS_MODULE,
  101. },
  102. {
  103. .name = "bpf",
  104. .revision = 1,
  105. .family = NFPROTO_UNSPEC,
  106. .checkentry = bpf_mt_check_v1,
  107. .match = bpf_mt_v1,
  108. .destroy = bpf_mt_destroy_v1,
  109. .matchsize = sizeof(struct xt_bpf_info_v1),
  110. .usersize = offsetof(struct xt_bpf_info_v1, filter),
  111. .me = THIS_MODULE,
  112. },
  113. };
  114. static int __init bpf_mt_init(void)
  115. {
  116. return xt_register_matches(bpf_mt_reg, ARRAY_SIZE(bpf_mt_reg));
  117. }
  118. static void __exit bpf_mt_exit(void)
  119. {
  120. xt_unregister_matches(bpf_mt_reg, ARRAY_SIZE(bpf_mt_reg));
  121. }
  122. module_init(bpf_mt_init);
  123. module_exit(bpf_mt_exit);