bpf_jitter.c revision 153157
1153151Sjkim/*- 2153151Sjkim * Copyright (c) 2002 - 2003 NetGroup, Politecnico di Torino (Italy) 3153151Sjkim * Copyright (c) 2005 Jung-uk Kim <jkim@FreeBSD.org> 4153151Sjkim * All rights reserved. 5153151Sjkim * 6153151Sjkim * Redistribution and use in source and binary forms, with or without 7153151Sjkim * modification, are permitted provided that the following conditions 8153151Sjkim * are met: 9153151Sjkim * 10153151Sjkim * 1. Redistributions of source code must retain the above copyright 11153151Sjkim * notice, this list of conditions and the following disclaimer. 12153151Sjkim * 2. Redistributions in binary form must reproduce the above copyright 13153151Sjkim * notice, this list of conditions and the following disclaimer in the 14153151Sjkim * documentation and/or other materials provided with the distribution. 15153151Sjkim * 3. Neither the name of the Politecnico di Torino nor the names of its 16153151Sjkim * contributors may be used to endorse or promote products derived from 17153151Sjkim * this software without specific prior written permission. 18153151Sjkim * 19153151Sjkim * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20153151Sjkim * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21153151Sjkim * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22153151Sjkim * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23153151Sjkim * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24153151Sjkim * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25153151Sjkim * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26153151Sjkim * DATA, OR PROFITS; OR BUSINESS intERRUPTION) HOWEVER CAUSED AND ON ANY 27153151Sjkim * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28153151Sjkim * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29153151Sjkim * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30153151Sjkim */ 31153151Sjkim 32153151Sjkim#include <sys/cdefs.h> 33153151Sjkim__FBSDID("$FreeBSD: head/sys/net/bpf_jitter.c 153157 2005-12-06 07:22:01Z jkim $"); 34153151Sjkim 35153151Sjkim#include "opt_bpf.h" 36153151Sjkim 37153151Sjkim#include <sys/param.h> 38153151Sjkim#include <sys/kernel.h> 39153151Sjkim#include <sys/malloc.h> 40153151Sjkim#include <sys/mbuf.h> 41153151Sjkim 42153151Sjkim#include <net/bpf.h> 43153151Sjkim#include <net/bpf_jitter.h> 44153151Sjkim 45153151SjkimMALLOC_DEFINE(M_BPFJIT, "BPF_JIT", "BPF JIT compiler"); 46153151Sjkim 47153151Sjkimbpf_filter_func bpf_jit_compile(struct bpf_insn *, u_int, int *); 48153151Sjkim 49153151Sjkimbpf_jit_filter * 50153151Sjkimbpf_jitter(struct bpf_insn *fp, int nins) 51153151Sjkim{ 52153151Sjkim bpf_jit_filter *filter; 53153151Sjkim 54153151Sjkim /* Allocate the filter structure */ 55153151Sjkim filter = (struct bpf_jit_filter *)malloc(sizeof(struct bpf_jit_filter), 56153157Sjkim M_BPFJIT, M_NOWAIT); 57153151Sjkim if (filter == NULL) 58153151Sjkim return NULL; 59153151Sjkim 60153151Sjkim /* Allocate the filter's memory */ 61153151Sjkim filter->mem = (int *)malloc(BPF_MEMWORDS * sizeof(int), 62153157Sjkim M_BPFJIT, M_NOWAIT); 63153151Sjkim if (filter->mem == NULL) { 64153151Sjkim free(filter, M_BPFJIT); 65153151Sjkim return NULL; 66153151Sjkim } 67153151Sjkim 68153151Sjkim /* Create the binary */ 69153151Sjkim if ((filter->func = bpf_jit_compile(fp, nins, filter->mem)) == NULL) { 70153151Sjkim free(filter->mem, M_BPFJIT); 71153151Sjkim free(filter, M_BPFJIT); 72153151Sjkim return NULL; 73153151Sjkim } 74153151Sjkim 75153151Sjkim return filter; 76153151Sjkim} 77153151Sjkim 78153151Sjkimvoid 79153151Sjkimbpf_destroy_jit_filter(bpf_jit_filter *filter) 80153151Sjkim{ 81153151Sjkim 82153151Sjkim free(filter->mem, M_BPFJIT); 83153151Sjkim free(filter->func, M_BPFJIT); 84153151Sjkim free(filter, M_BPFJIT); 85153151Sjkim} 86