/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (C) 2002-2003 NetGroup, Politecnico di Torino (Italy)4* Copyright (C) 2005-2017 Jung-uk Kim <[email protected]>5* All rights reserved.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10*11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13* 2. Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in the15* documentation and/or other materials provided with the distribution.16* 3. Neither the name of the Politecnico di Torino nor the names of its17* contributors may be used to endorse or promote products derived from18* this software without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS21* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT22* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR23* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT24* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,25* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT26* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,27* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY28* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT29* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE30* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.31*/3233#include <sys/cdefs.h>34#ifdef _KERNEL35#include "opt_bpf.h"3637#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#else43#include <stdlib.h>44#include <sys/mman.h>45#include <sys/param.h>46#include <sys/types.h>47#endif4849#include <net/bpf.h>50#include <net/bpf_jitter.h>5152static u_int bpf_jit_accept_all(u_char *, u_int, u_int);5354#ifdef _KERNEL55MALLOC_DEFINE(M_BPFJIT, "BPF_JIT", "BPF JIT compiler");5657SYSCTL_NODE(_net, OID_AUTO, bpf_jitter, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,58"BPF JIT compiler");59int bpf_jitter_enable = 1;60SYSCTL_INT(_net_bpf_jitter, OID_AUTO, enable, CTLFLAG_RW,61&bpf_jitter_enable, 0, "enable BPF JIT compiler");62#endif6364bpf_jit_filter *65bpf_jitter(struct bpf_insn *fp, int nins)66{67bpf_jit_filter *filter;6869/* Allocate the filter structure. */70#ifdef _KERNEL71filter = (struct bpf_jit_filter *)malloc(sizeof(*filter),72M_BPFJIT, M_NOWAIT);73#else74filter = (struct bpf_jit_filter *)malloc(sizeof(*filter));75#endif76if (filter == NULL)77return (NULL);7879/* No filter means accept all. */80if (fp == NULL || nins == 0) {81filter->func = bpf_jit_accept_all;82return (filter);83}8485/* Create the binary. */86if ((filter->func = bpf_jit_compile(fp, nins, &filter->size)) == NULL) {87#ifdef _KERNEL88free(filter, M_BPFJIT);89#else90free(filter);91#endif92return (NULL);93}9495return (filter);96}9798void99bpf_destroy_jit_filter(bpf_jit_filter *filter)100{101102#ifdef _KERNEL103if (filter->func != bpf_jit_accept_all)104free(filter->func, M_BPFJIT);105free(filter, M_BPFJIT);106#else107if (filter->func != bpf_jit_accept_all)108munmap(filter->func, filter->size);109free(filter);110#endif111}112113static u_int114bpf_jit_accept_all(__unused u_char *p, __unused u_int wirelen,115__unused u_int buflen)116{117118return ((u_int)-1);119}120121122