2 * Copyright (C) 2008-2009 Jung-uk Kim <jkim@FreeBSD.org>. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
33 #include <sys/types.h>
48 #define BPF_NRUNS 10000000
53 static void sig_handler(int);
55 static int nins = sizeof(pc) / sizeof(pc[0]);
56 static int verbose = LOG_LEVEL;
58 #ifdef BPF_JIT_COMPILER
62 #include <net/bpf_jitter.h>
65 bpf_compile_and_filter(void)
67 bpf_jit_filter *filter;
70 /* Compile the BPF filter program and generate native code. */
71 if ((filter = bpf_jitter(pc, nins)) == NULL) {
73 printf("Failed to allocate memory:\t");
80 hexdump(filter->func, filter->size, NULL, HD_OMIT_CHARS);
83 for (i = 0; i < BPF_NRUNS; i++)
84 ret = (*(filter->func))(pkt, wirelen, buflen);
86 bpf_destroy_jit_filter(filter);
93 u_int bpf_filter(const struct bpf_insn *, u_char *, u_int, u_int);
98 static const u_short bpf_code_map[] = {
99 0x10ff, /* 0x00-0x0f: 1111111100001000 */
100 0x3070, /* 0x10-0x1f: 0000111000001100 */
101 0x3131, /* 0x20-0x2f: 1000110010001100 */
102 0x3031, /* 0x30-0x3f: 1000110000001100 */
103 0x3131, /* 0x40-0x4f: 1000110010001100 */
104 0x1011, /* 0x50-0x5f: 1000100000001000 */
105 0x1013, /* 0x60-0x6f: 1100100000001000 */
106 0x1010, /* 0x70-0x7f: 0000100000001000 */
107 0x0093, /* 0x80-0x8f: 1100100100000000 */
108 0x1010, /* 0x90-0x9f: 0000100000001000 */
109 0x1010, /* 0xa0-0xaf: 0000100000001000 */
110 0x0002, /* 0xb0-0xbf: 0100000000000000 */
111 0x0000, /* 0xc0-0xcf: 0000000000000000 */
112 0x0000, /* 0xd0-0xdf: 0000000000000000 */
113 0x0000, /* 0xe0-0xef: 0000000000000000 */
114 0x0000 /* 0xf0-0xff: 0000000000000000 */
117 #define BPF_VALIDATE_CODE(c) \
118 ((c) <= 0xff && (bpf_code_map[(c) >> 4] & (1 << ((c) & 0xf))) != 0)
121 * XXX Copied from sys/net/bpf_filter.c and modified.
123 * Return true if the 'fcode' is a valid filter program.
124 * The constraints are that each jump be forward and to a valid
125 * code. The code must terminate with either an accept or reject.
127 * The kernel needs to be able to verify an application's filter code.
128 * Otherwise, a bogus program could easily crash the system.
131 bpf_validate(const struct bpf_insn *f, int len)
134 register const struct bpf_insn *p;
136 /* Do not accept negative length filter. */
140 /* An empty filter means accept all. */
144 for (i = 0; i < len; ++i) {
147 * Check that the code is valid.
149 if (!BPF_VALIDATE_CODE(p->code))
152 * Check that jumps are forward, and within
155 if (BPF_CLASS(p->code) == BPF_JMP) {
156 register u_int offset;
158 if (p->code == (BPF_JMP|BPF_JA))
161 offset = p->jt > p->jf ? p->jt : p->jf;
162 if (offset >= (u_int)(len - i) - 1)
167 * Check that memory operations use valid addresses.
169 if (p->code == BPF_ST || p->code == BPF_STX ||
170 p->code == (BPF_LD|BPF_MEM) ||
171 p->code == (BPF_LDX|BPF_MEM)) {
172 if (p->k >= BPF_MEMWORDS)
177 * Check for constant division by 0.
179 if ((p->code == (BPF_ALU|BPF_DIV|BPF_K) ||
180 p->code == (BPF_ALU|BPF_MOD|BPF_K)) && p->k == 0)
183 return (BPF_CLASS(f[len - 1].code) == BPF_RET);
190 #ifndef BPF_JIT_COMPILER
199 /* Try to catch all signals */
200 for (sig = SIGHUP; sig <= SIGUSR2; sig++)
201 signal(sig, sig_handler);
204 valid = bpf_validate(pc, nins);
205 if (valid != 0 && invalid != 0) {
207 printf("Validated invalid instruction(s):\t");
211 } else if (valid == 0 && invalid == 0) {
213 printf("Invalidated valid instruction(s):\t");
217 } else if (invalid != 0) {
219 printf("Expected and invalidated:\t");
226 #ifdef BPF_JIT_COMPILER
227 ret = bpf_compile_and_filter();
229 for (i = 0; i < BPF_NRUNS; i++)
230 ret = bpf_filter(nins != 0 ? pc : NULL, pkt, wirelen, buflen);
232 if (expect_signal != 0) {
234 printf("Expected signal %d but got none:\t",
242 printf("Expected 0x%x but got 0x%x:\t", expect, ret);
248 printf("Expected and got 0x%x:\t", ret);
259 if (expect_signal == 0) {
261 printf("Received unexpected signal %d:\t", sig);
266 if (expect_signal != sig) {
268 printf("Expected signal %d but got %d:\t",
276 printf("Expected and got signal %d:\t", sig);