xref: /freebsd/sys/net/bpf_jitter.c (revision af1973281e8bd2ffd85e0f35fec229f756e4ea77)
1ae275efcSJung-uk Kim /*-
23bfea868SJung-uk Kim  * Copyright (C) 2002-2003 NetGroup, Politecnico di Torino (Italy)
3*af197328SJung-uk Kim  * Copyright (C) 2005-2017 Jung-uk Kim <jkim@FreeBSD.org>
4ae275efcSJung-uk Kim  * All rights reserved.
5ae275efcSJung-uk Kim  *
6ae275efcSJung-uk Kim  * Redistribution and use in source and binary forms, with or without
7ae275efcSJung-uk Kim  * modification, are permitted provided that the following conditions
8ae275efcSJung-uk Kim  * are met:
9ae275efcSJung-uk Kim  *
10ae275efcSJung-uk Kim  * 1. Redistributions of source code must retain the above copyright
11ae275efcSJung-uk Kim  * notice, this list of conditions and the following disclaimer.
12ae275efcSJung-uk Kim  * 2. Redistributions in binary form must reproduce the above copyright
13ae275efcSJung-uk Kim  * notice, this list of conditions and the following disclaimer in the
14ae275efcSJung-uk Kim  * documentation and/or other materials provided with the distribution.
15ae275efcSJung-uk Kim  * 3. Neither the name of the Politecnico di Torino nor the names of its
16ae275efcSJung-uk Kim  * contributors may be used to endorse or promote products derived from
17ae275efcSJung-uk Kim  * this software without specific prior written permission.
18ae275efcSJung-uk Kim  *
19ae275efcSJung-uk Kim  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20ae275efcSJung-uk Kim  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21ae275efcSJung-uk Kim  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22ae275efcSJung-uk Kim  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23ae275efcSJung-uk Kim  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24ae275efcSJung-uk Kim  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25ae275efcSJung-uk Kim  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26f471e569SJung-uk Kim  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27ae275efcSJung-uk Kim  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28ae275efcSJung-uk Kim  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29ae275efcSJung-uk Kim  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30ae275efcSJung-uk Kim  */
31ae275efcSJung-uk Kim 
32ae275efcSJung-uk Kim #include <sys/cdefs.h>
33ae275efcSJung-uk Kim __FBSDID("$FreeBSD$");
34ae275efcSJung-uk Kim 
353bfea868SJung-uk Kim #ifdef _KERNEL
36ae275efcSJung-uk Kim #include "opt_bpf.h"
37ae275efcSJung-uk Kim 
38ae275efcSJung-uk Kim #include <sys/param.h>
39ae275efcSJung-uk Kim #include <sys/kernel.h>
40ae275efcSJung-uk Kim #include <sys/malloc.h>
41ae275efcSJung-uk Kim #include <sys/mbuf.h>
42848c454cSJung-uk Kim #include <sys/sysctl.h>
433bfea868SJung-uk Kim #else
443bfea868SJung-uk Kim #include <stdlib.h>
45366652f9SJung-uk Kim #include <sys/mman.h>
46366652f9SJung-uk Kim #include <sys/param.h>
473bfea868SJung-uk Kim #include <sys/types.h>
483bfea868SJung-uk Kim #endif
49ae275efcSJung-uk Kim 
50ae275efcSJung-uk Kim #include <net/bpf.h>
51ae275efcSJung-uk Kim #include <net/bpf_jitter.h>
52ae275efcSJung-uk Kim 
53a2b12e3bSJung-uk Kim static u_int	bpf_jit_accept_all(u_char *, u_int, u_int);
54a2b12e3bSJung-uk Kim 
553bfea868SJung-uk Kim #ifdef _KERNEL
563bfea868SJung-uk Kim MALLOC_DEFINE(M_BPFJIT, "BPF_JIT", "BPF JIT compiler");
573bfea868SJung-uk Kim 
58848c454cSJung-uk Kim SYSCTL_NODE(_net, OID_AUTO, bpf_jitter, CTLFLAG_RW, 0, "BPF JIT compiler");
59848c454cSJung-uk Kim int bpf_jitter_enable = 1;
60848c454cSJung-uk Kim SYSCTL_INT(_net_bpf_jitter, OID_AUTO, enable, CTLFLAG_RW,
61848c454cSJung-uk Kim     &bpf_jitter_enable, 0, "enable BPF JIT compiler");
62c12b965fSJung-uk Kim #endif
63848c454cSJung-uk Kim 
64ae275efcSJung-uk Kim bpf_jit_filter *
65ae275efcSJung-uk Kim bpf_jitter(struct bpf_insn *fp, int nins)
66ae275efcSJung-uk Kim {
67ae275efcSJung-uk Kim 	bpf_jit_filter *filter;
68ae275efcSJung-uk Kim 
69c12b965fSJung-uk Kim 	/* Allocate the filter structure. */
70c12b965fSJung-uk Kim #ifdef _KERNEL
713bfea868SJung-uk Kim 	filter = (struct bpf_jit_filter *)malloc(sizeof(*filter),
725ecf7736SJung-uk Kim 	    M_BPFJIT, M_NOWAIT);
73c12b965fSJung-uk Kim #else
74c12b965fSJung-uk Kim 	filter = (struct bpf_jit_filter *)malloc(sizeof(*filter));
75c12b965fSJung-uk Kim #endif
76ae275efcSJung-uk Kim 	if (filter == NULL)
773bfea868SJung-uk Kim 		return (NULL);
78ae275efcSJung-uk Kim 
79c12b965fSJung-uk Kim 	/* No filter means accept all. */
80a2b12e3bSJung-uk Kim 	if (fp == NULL || nins == 0) {
81a2b12e3bSJung-uk Kim 		filter->func = bpf_jit_accept_all;
82a2b12e3bSJung-uk Kim 		return (filter);
83a2b12e3bSJung-uk Kim 	}
84a2b12e3bSJung-uk Kim 
85c12b965fSJung-uk Kim 	/* Create the binary. */
865ecf7736SJung-uk Kim 	if ((filter->func = bpf_jit_compile(fp, nins, &filter->size)) == NULL) {
87c12b965fSJung-uk Kim #ifdef _KERNEL
88ae275efcSJung-uk Kim 		free(filter, M_BPFJIT);
89c12b965fSJung-uk Kim #else
90c12b965fSJung-uk Kim 		free(filter);
91c12b965fSJung-uk Kim #endif
923bfea868SJung-uk Kim 		return (NULL);
93ae275efcSJung-uk Kim 	}
94ae275efcSJung-uk Kim 
953bfea868SJung-uk Kim 	return (filter);
96ae275efcSJung-uk Kim }
97ae275efcSJung-uk Kim 
98ae275efcSJung-uk Kim void
99ae275efcSJung-uk Kim bpf_destroy_jit_filter(bpf_jit_filter *filter)
100ae275efcSJung-uk Kim {
101ae275efcSJung-uk Kim 
102a2b12e3bSJung-uk Kim 	if (filter->func != bpf_jit_accept_all)
103*af197328SJung-uk Kim 		bpf_jit_free(filter->func, filter->size);
104*af197328SJung-uk Kim #ifdef _KERNEL
105ae275efcSJung-uk Kim 	free(filter, M_BPFJIT);
1063bfea868SJung-uk Kim #else
1073bfea868SJung-uk Kim 	free(filter);
1083bfea868SJung-uk Kim #endif
109c12b965fSJung-uk Kim }
110a2b12e3bSJung-uk Kim 
111a2b12e3bSJung-uk Kim static u_int
112a2b12e3bSJung-uk Kim bpf_jit_accept_all(__unused u_char *p, __unused u_int wirelen,
113a2b12e3bSJung-uk Kim     __unused u_int buflen)
114a2b12e3bSJung-uk Kim {
115a2b12e3bSJung-uk Kim 
116a2b12e3bSJung-uk Kim 	return ((u_int)-1);
117a2b12e3bSJung-uk Kim }
118