bpf_jitter.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*-
  2. * SPDX-License-Identifier: BSD-3-Clause
  3. *
  4. * Copyright (C) 2002-2003 NetGroup, Politecnico di Torino (Italy)
  5. * Copyright (C) 2005-2017 Jung-uk Kim <jkim@FreeBSD.org>
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the Politecnico di Torino nor the names of its
  18. * contributors may be used to endorse or promote products derived from
  19. * this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #include <sys/cdefs.h>
  34. __FBSDID("$FreeBSD$");
  35. #ifdef _KERNEL
  36. #include "opt_bpf.h"
  37. #include <sys/param.h>
  38. #include <sys/kernel.h>
  39. #include <sys/malloc.h>
  40. #include <sys/mbuf.h>
  41. #include <sys/sysctl.h>
  42. #else
  43. #include <stdlib.h>
  44. #include <sys/mman.h>
  45. #include <sys/param.h>
  46. #include <sys/types.h>
  47. #endif
  48. #include <net/bpf.h>
  49. #include <net/bpf_jitter.h>
  50. static u_int bpf_jit_accept_all(u_char *, u_int, u_int);
  51. #ifdef _KERNEL
  52. MALLOC_DEFINE(M_BPFJIT, "BPF_JIT", "BPF JIT compiler");
  53. SYSCTL_NODE(_net, OID_AUTO, bpf_jitter, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
  54. "BPF JIT compiler");
  55. int bpf_jitter_enable = 1;
  56. SYSCTL_INT(_net_bpf_jitter, OID_AUTO, enable, CTLFLAG_RW,
  57. &bpf_jitter_enable, 0, "enable BPF JIT compiler");
  58. #endif
  59. bpf_jit_filter *
  60. bpf_jitter(struct bpf_insn *fp, int nins)
  61. {
  62. bpf_jit_filter *filter;
  63. /* Allocate the filter structure. */
  64. #ifdef _KERNEL
  65. filter = (struct bpf_jit_filter *)malloc(sizeof(*filter),
  66. M_BPFJIT, M_NOWAIT);
  67. #else
  68. filter = (struct bpf_jit_filter *)malloc(sizeof(*filter));
  69. #endif
  70. if (filter == NULL)
  71. return (NULL);
  72. /* No filter means accept all. */
  73. if (fp == NULL || nins == 0) {
  74. filter->func = bpf_jit_accept_all;
  75. return (filter);
  76. }
  77. /* Create the binary. */
  78. if ((filter->func = bpf_jit_compile(fp, nins, &filter->size)) == NULL) {
  79. #ifdef _KERNEL
  80. free(filter, M_BPFJIT);
  81. #else
  82. free(filter);
  83. #endif
  84. return (NULL);
  85. }
  86. return (filter);
  87. }
  88. void
  89. bpf_destroy_jit_filter(bpf_jit_filter *filter)
  90. {
  91. #ifdef _KERNEL
  92. if (filter->func != bpf_jit_accept_all)
  93. free(filter->func, M_BPFJIT);
  94. free(filter, M_BPFJIT);
  95. #else
  96. if (filter->func != bpf_jit_accept_all)
  97. munmap(filter->func, filter->size);
  98. free(filter);
  99. #endif
  100. }
  101. static u_int
  102. bpf_jit_accept_all(__unused u_char *p, __unused u_int wirelen,
  103. __unused u_int buflen)
  104. {
  105. return ((u_int)-1);
  106. }