xref: /freebsd/sys/net/bpf_jitter.h (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
1ae275efcSJung-uk Kim /*-
2*51369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*51369649SPedro F. Giffuni  *
43bfea868SJung-uk Kim  * Copyright (C) 2002-2003 NetGroup, Politecnico di Torino (Italy)
5366652f9SJung-uk Kim  * Copyright (C) 2005-2009 Jung-uk Kim <jkim@FreeBSD.org>
6ae275efcSJung-uk Kim  * All rights reserved.
7ae275efcSJung-uk Kim  *
8ae275efcSJung-uk Kim  * Redistribution and use in source and binary forms, with or without
9ae275efcSJung-uk Kim  * modification, are permitted provided that the following conditions
10ae275efcSJung-uk Kim  * are met:
11ae275efcSJung-uk Kim  *
12ae275efcSJung-uk Kim  * 1. Redistributions of source code must retain the above copyright
13ae275efcSJung-uk Kim  * notice, this list of conditions and the following disclaimer.
14ae275efcSJung-uk Kim  * 2. Redistributions in binary form must reproduce the above copyright
15ae275efcSJung-uk Kim  * notice, this list of conditions and the following disclaimer in the
16ae275efcSJung-uk Kim  * documentation and/or other materials provided with the distribution.
17ae275efcSJung-uk Kim  * 3. Neither the name of the Politecnico di Torino nor the names of its
18ae275efcSJung-uk Kim  * contributors may be used to endorse or promote products derived from
19ae275efcSJung-uk Kim  * this software without specific prior written permission.
20ae275efcSJung-uk Kim  *
21ae275efcSJung-uk Kim  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22ae275efcSJung-uk Kim  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23ae275efcSJung-uk Kim  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24ae275efcSJung-uk Kim  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25ae275efcSJung-uk Kim  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26ae275efcSJung-uk Kim  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27ae275efcSJung-uk Kim  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28f471e569SJung-uk Kim  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29ae275efcSJung-uk Kim  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30ae275efcSJung-uk Kim  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31ae275efcSJung-uk Kim  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32ae275efcSJung-uk Kim  */
33ae275efcSJung-uk Kim 
34ae275efcSJung-uk Kim #ifndef _NET_BPF_JITTER_H_
35ae275efcSJung-uk Kim #define _NET_BPF_JITTER_H_
36ae275efcSJung-uk Kim 
373bfea868SJung-uk Kim #ifdef _KERNEL
38ae275efcSJung-uk Kim MALLOC_DECLARE(M_BPFJIT);
393bfea868SJung-uk Kim #endif
40ae275efcSJung-uk Kim 
41848c454cSJung-uk Kim extern int bpf_jitter_enable;
42848c454cSJung-uk Kim 
43ae275efcSJung-uk Kim /*
44ae275efcSJung-uk Kim  * Prototype of a filtering function created by the jitter.
45ae275efcSJung-uk Kim  *
46ae275efcSJung-uk Kim  * The syntax and the meaning of the parameters is analogous to the one of
47ae275efcSJung-uk Kim  * bpf_filter(). Notice that the filter is not among the parameters because
48ae275efcSJung-uk Kim  * it is hardwired in the function.
49ae275efcSJung-uk Kim  */
50ae275efcSJung-uk Kim typedef u_int (*bpf_filter_func)(u_char *, u_int, u_int);
51ae275efcSJung-uk Kim 
52ae275efcSJung-uk Kim /* Structure describing a native filtering program created by the jitter. */
53ae275efcSJung-uk Kim typedef struct bpf_jit_filter {
54ae275efcSJung-uk Kim 	/* The native filtering binary, in the form of a bpf_filter_func. */
55ae275efcSJung-uk Kim 	bpf_filter_func	func;
56ae4fdab8SJung-uk Kim 	size_t		size;
57ae275efcSJung-uk Kim } bpf_jit_filter;
58ae275efcSJung-uk Kim 
59ae275efcSJung-uk Kim /*
60ae275efcSJung-uk Kim  * BPF jitter, builds a machine function from a BPF program.
61ae275efcSJung-uk Kim  *
62ae275efcSJung-uk Kim  * param fp	The BPF pseudo-assembly filter that will be translated
63ae275efcSJung-uk Kim  *		into native code.
64ae275efcSJung-uk Kim  * param nins	Number of instructions of the input filter.
65ae275efcSJung-uk Kim  * return	The bpf_jit_filter structure containing the native filtering
66ae275efcSJung-uk Kim  *		binary.
67ae275efcSJung-uk Kim  *
68ae275efcSJung-uk Kim  * bpf_jitter allocates the buffers for the new native filter and
69ae275efcSJung-uk Kim  * then translates the program pointed by fp calling bpf_jit_compile().
70ae275efcSJung-uk Kim  */
71ae275efcSJung-uk Kim bpf_jit_filter	*bpf_jitter(struct bpf_insn *fp, int nins);
72ae275efcSJung-uk Kim 
73ae275efcSJung-uk Kim /*
74ae275efcSJung-uk Kim  * Deletes a filtering function that was previously created by bpf_jitter().
75ae275efcSJung-uk Kim  *
76ae275efcSJung-uk Kim  * param filter	The filter to destroy.
77ae275efcSJung-uk Kim  *
78ae275efcSJung-uk Kim  * This function frees the variuos buffers (code, memory, etc.) associated
79ae275efcSJung-uk Kim  * with a filtering function.
80ae275efcSJung-uk Kim  */
81ae275efcSJung-uk Kim void		bpf_destroy_jit_filter(bpf_jit_filter *filter);
82ae275efcSJung-uk Kim 
83e329e330SJung-uk Kim /*
84e329e330SJung-uk Kim  * Declarations for machine-dependent functions.
85e329e330SJung-uk Kim  */
86e329e330SJung-uk Kim struct bpf_insn;
87e329e330SJung-uk Kim 
88e329e330SJung-uk Kim bpf_filter_func	bpf_jit_compile(struct bpf_insn *, u_int, size_t *);
89e329e330SJung-uk Kim 
90ae275efcSJung-uk Kim #endif	/* _NET_BPF_JITTER_H_ */
91