2 * Copyright (C) 2008 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
60 #include <net/bpf_jitter.h>
63 bpf_compile_and_filter(void)
65 bpf_jit_filter *filter;
68 /* Compile the BPF filter program and generate native code. */
69 if ((filter = bpf_jitter(pc, nins)) == NULL) {
71 printf("Failed to allocate memory:\t");
77 for (i = 0; i < BPF_NRUNS; i++)
78 ret = (*(filter->func))(pkt, wirelen, buflen);
80 bpf_destroy_jit_filter(filter);
87 u_int bpf_filter(const struct bpf_insn *, u_char *, u_int, u_int);
92 static const u_short bpf_code_map[] = {
93 0x10ff, /* 0x00-0x0f: 1111111100001000 */
94 0x3070, /* 0x10-0x1f: 0000111000001100 */
95 0x3131, /* 0x20-0x2f: 1000110010001100 */
96 0x3031, /* 0x30-0x3f: 1000110000001100 */
97 0x3131, /* 0x40-0x4f: 1000110010001100 */
98 0x1011, /* 0x50-0x5f: 1000100000001000 */
99 0x1013, /* 0x60-0x6f: 1100100000001000 */
100 0x1010, /* 0x70-0x7f: 0000100000001000 */
101 0x0093, /* 0x80-0x8f: 1100100100000000 */
102 0x0000, /* 0x90-0x9f: 0000000000000000 */
103 0x0000, /* 0xa0-0xaf: 0000000000000000 */
104 0x0002, /* 0xb0-0xbf: 0100000000000000 */
105 0x0000, /* 0xc0-0xcf: 0000000000000000 */
106 0x0000, /* 0xd0-0xdf: 0000000000000000 */
107 0x0000, /* 0xe0-0xef: 0000000000000000 */
108 0x0000 /* 0xf0-0xff: 0000000000000000 */
111 #define BPF_VALIDATE_CODE(c) \
112 ((c) <= 0xff && (bpf_code_map[(c) >> 4] & (1 << ((c) & 0xf))) != 0)
115 * XXX Copied from sys/net/bpf_filter.c and modified.
117 * Return true if the 'fcode' is a valid filter program.
118 * The constraints are that each jump be forward and to a valid
119 * code. The code must terminate with either an accept or reject.
121 * The kernel needs to be able to verify an application's filter code.
122 * Otherwise, a bogus program could easily crash the system.
125 bpf_validate(const struct bpf_insn *f, int len)
128 register const struct bpf_insn *p;
130 /* Do not accept negative length filter. */
134 /* An empty filter means accept all. */
138 for (i = 0; i < len; ++i) {
141 * Check that the code is valid.
143 if (!BPF_VALIDATE_CODE(p->code))
146 * Check that that jumps are forward, and within
149 if (BPF_CLASS(p->code) == BPF_JMP) {
150 register u_int offset;
152 if (p->code == (BPF_JMP|BPF_JA))
155 offset = p->jt > p->jf ? p->jt : p->jf;
156 if (offset >= (u_int)(len - i) - 1)
161 * Check that memory operations use valid addresses.
163 if (p->code == BPF_ST || p->code == BPF_STX ||
164 p->code == (BPF_LD|BPF_MEM) ||
165 p->code == (BPF_LDX|BPF_MEM)) {
166 if (p->k >= BPF_MEMWORDS)
171 * Check for constant division by 0.
173 if (p->code == (BPF_ALU|BPF_DIV|BPF_K) && p->k == 0)
176 return (BPF_CLASS(f[len - 1].code) == BPF_RET);
183 #ifndef BPF_JIT_COMPILER
192 /* Try to catch all signals */
193 for (sig = SIGHUP; sig <= SIGUSR2; sig++)
194 signal(sig, sig_handler);
197 valid = bpf_validate(pc, nins);
198 if (valid != 0 && invalid != 0) {
200 printf("Validated invalid instruction(s):\t");
204 } else if (valid == 0 && invalid == 0) {
206 printf("Invalidated valid instruction(s):\t");
210 } else if (invalid != 0) {
212 printf("Expected and invalidated:\t");
219 #ifdef BPF_JIT_COMPILER
220 ret = bpf_compile_and_filter();
222 for (i = 0; i < BPF_NRUNS; i++)
223 ret = bpf_filter(nins != 0 ? pc : NULL, pkt, wirelen, buflen);
227 printf("Expected 0x%x but got 0x%x:\t", expect, ret);
233 printf("Expected and got 0x%x:\t", ret);
244 if (expect_signal == 0) {
246 printf("Received unexpected signal %d:\t", sig);
251 if (expect_signal != sig) {
253 printf("Expected signal %d but got %d:\t",
261 printf("Expected and got signal %d:\t", sig);