bpf_jitter.c revision 153151
1276789Sdim/*-
2276789Sdim * Copyright (c) 2002 - 2003 NetGroup, Politecnico di Torino (Italy)
3353358Sdim * Copyright (c) 2005 Jung-uk Kim <jkim@FreeBSD.org>
4353358Sdim * All rights reserved.
5353358Sdim *
6276789Sdim * Redistribution and use in source and binary forms, with or without
7276789Sdim * modification, are permitted provided that the following conditions
8276789Sdim * are met:
9276789Sdim *
10276789Sdim * 1. Redistributions of source code must retain the above copyright
11276789Sdim * notice, this list of conditions and the following disclaimer.
12276789Sdim * 2. Redistributions in binary form must reproduce the above copyright
13276789Sdim * notice, this list of conditions and the following disclaimer in the
14276789Sdim * documentation and/or other materials provided with the distribution.
15276789Sdim * 3. Neither the name of the Politecnico di Torino nor the names of its
16276789Sdim * contributors may be used to endorse or promote products derived from
17276789Sdim * this software without specific prior written permission.
18276789Sdim *
19276789Sdim * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20276789Sdim * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21276789Sdim * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22276789Sdim * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23276789Sdim * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24276789Sdim * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25276789Sdim * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26276789Sdim * DATA, OR PROFITS; OR BUSINESS intERRUPTION) HOWEVER CAUSED AND ON ANY
27276789Sdim * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28276789Sdim * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29276789Sdim * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30276789Sdim */
31276789Sdim
32276789Sdim#include <sys/cdefs.h>
33276789Sdim__FBSDID("$FreeBSD: head/sys/net/bpf_jitter.c 153151 2005-12-06 02:58:12Z jkim $");
34276789Sdim
35276789Sdim#include "opt_bpf.h"
36276789Sdim
37288943Sdim#include <sys/param.h>
38276789Sdim#include <sys/kernel.h>
39276789Sdim#include <sys/malloc.h>
40276789Sdim#include <sys/mbuf.h>
41276789Sdim
42276789Sdim#include <net/bpf.h>
43276789Sdim#include <net/bpf_jitter.h>
44276789Sdim
45276789SdimMALLOC_DEFINE(M_BPFJIT, "BPF_JIT", "BPF JIT compiler");
46276789Sdim
47276789Sdimbpf_filter_func	bpf_jit_compile(struct bpf_insn *, u_int, int *);
48276789Sdim
49276789Sdimbpf_jit_filter *
50276789Sdimbpf_jitter(struct bpf_insn *fp, int nins)
51276789Sdim{
52276789Sdim	bpf_jit_filter *filter;
53276789Sdim
54276789Sdim	/* Allocate the filter structure */
55276789Sdim	filter = (struct bpf_jit_filter *)malloc(sizeof(struct bpf_jit_filter),
56276789Sdim	    M_BPFJIT, M_WAITOK);
57276789Sdim	if (filter == NULL)
58276789Sdim		return NULL;
59276789Sdim
60276789Sdim	/* Allocate the filter's memory */
61276789Sdim	filter->mem = (int *)malloc(BPF_MEMWORDS * sizeof(int),
62276789Sdim	    M_BPFJIT, M_WAITOK);
63276789Sdim	if (filter->mem == NULL) {
64276789Sdim		free(filter, M_BPFJIT);
65276789Sdim		return NULL;
66276789Sdim	}
67
68	/* Create the binary */
69	if ((filter->func = bpf_jit_compile(fp, nins, filter->mem)) == NULL) {
70		free(filter->mem, M_BPFJIT);
71		free(filter, M_BPFJIT);
72		return NULL;
73	}
74
75	return filter;
76}
77
78void
79bpf_destroy_jit_filter(bpf_jit_filter *filter)
80{
81
82	free(filter->mem, M_BPFJIT);
83	free(filter->func, M_BPFJIT);
84	free(filter, M_BPFJIT);
85}
86