]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - tools/regression/bpf/bpf_filter/bpf_test.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / tools / regression / bpf / bpf_filter / bpf_test.c
1 /*-
2  * Copyright (C) 2008 Jung-uk Kim <jkim@FreeBSD.org>. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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
23  * SUCH DAMAGE.
24  */
25
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28
29 #include <signal.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32
33 #include <sys/types.h>
34
35 #include <net/bpf.h>
36
37 #include BPF_TEST_H
38
39 #define PASSED          0
40 #define FAILED          1
41 #define FATAL           -1
42
43 #ifndef LOG_LEVEL
44 #define LOG_LEVEL       1
45 #endif
46
47 #ifdef BPF_BENCHMARK
48 #define BPF_NRUNS       10000000
49 #else
50 #define BPF_NRUNS       1
51 #endif
52
53 static void     sig_handler(int);
54
55 static int      nins = sizeof(pc) / sizeof(pc[0]);
56 static int      verbose = LOG_LEVEL;
57
58 #ifdef BPF_JIT_COMPILER
59
60 #include <net/bpf_jitter.h>
61
62 static u_int
63 bpf_compile_and_filter(void)
64 {
65         bpf_jit_filter  *filter;
66         u_int           i, ret;
67
68         /* Compile the BPF filter program and generate native code. */
69         if ((filter = bpf_jitter(pc, nins)) == NULL) {
70                 if (verbose > 1)
71                         printf("Failed to allocate memory:\t");
72                 if (verbose > 0)
73                         printf("FATAL\n");
74                 exit(FATAL);
75         }
76
77         for (i = 0; i < BPF_NRUNS; i++)
78                 ret = (*(filter->func))(pkt, wirelen, buflen);
79
80         bpf_destroy_jit_filter(filter);
81
82         return (ret);
83 }
84
85 #else
86
87 u_int   bpf_filter(const struct bpf_insn *, u_char *, u_int, u_int);
88
89 #endif
90
91 #ifdef BPF_VALIDATE
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 */
109 };
110
111 #define BPF_VALIDATE_CODE(c)    \
112     ((c) <= 0xff && (bpf_code_map[(c) >> 4] & (1 << ((c) & 0xf))) != 0)
113
114 /*
115  * XXX Copied from sys/net/bpf_filter.c and modified.
116  *
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.
120  *
121  * The kernel needs to be able to verify an application's filter code.
122  * Otherwise, a bogus program could easily crash the system.
123  */
124 static int
125 bpf_validate(const struct bpf_insn *f, int len)
126 {
127         register int i;
128         register const struct bpf_insn *p;
129
130         /* Do not accept negative length filter. */
131         if (len < 0)
132                 return (0);
133
134         /* An empty filter means accept all. */
135         if (len == 0)
136                 return (1);
137
138         for (i = 0; i < len; ++i) {
139                 p = &f[i];
140                 /*
141                  * Check that the code is valid.
142                  */
143                 if (!BPF_VALIDATE_CODE(p->code))
144                         return (0);
145                 /*
146                  * Check that that jumps are forward, and within
147                  * the code block.
148                  */
149                 if (BPF_CLASS(p->code) == BPF_JMP) {
150                         register u_int offset;
151
152                         if (p->code == (BPF_JMP|BPF_JA))
153                                 offset = p->k;
154                         else
155                                 offset = p->jt > p->jf ? p->jt : p->jf;
156                         if (offset >= (u_int)(len - i) - 1)
157                                 return (0);
158                         continue;
159                 }
160                 /*
161                  * Check that memory operations use valid addresses.
162                  */
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)
167                                 return (0);
168                         continue;
169                 }
170                 /*
171                  * Check for constant division by 0.
172                  */
173                 if (p->code == (BPF_ALU|BPF_DIV|BPF_K) && p->k == 0)
174                         return (0);
175         }
176         return (BPF_CLASS(f[len - 1].code) == BPF_RET);
177 }
178 #endif
179
180 int
181 main(void)
182 {
183 #ifndef BPF_JIT_COMPILER
184         u_int   i;
185 #endif
186         u_int   ret;
187         int     sig;
188 #ifdef BPF_VALIDATE
189         int     valid;
190 #endif
191
192         /* Try to catch all signals */
193         for (sig = SIGHUP; sig <= SIGUSR2; sig++)
194                 signal(sig, sig_handler);
195
196 #ifdef BPF_VALIDATE
197         valid = bpf_validate(pc, nins);
198         if (valid != 0 && invalid != 0) {
199                 if (verbose > 1)
200                         printf("Validated invalid instruction(s):\t");
201                 if (verbose > 0)
202                         printf("FAILED\n");
203                 return (FAILED);
204         } else if (valid == 0 && invalid == 0) {
205                 if (verbose > 1)
206                         printf("Invalidated valid instruction(s):\t");
207                 if (verbose > 0)
208                         printf("FAILED\n");
209                 return (FAILED);
210         } else if (invalid != 0) {
211                 if (verbose > 1)
212                         printf("Expected and invalidated:\t");
213                 if (verbose > 0)
214                         printf("PASSED\n");
215                 return (PASSED);
216         }
217 #endif
218
219 #ifdef BPF_JIT_COMPILER
220         ret = bpf_compile_and_filter();
221 #else
222         for (i = 0; i < BPF_NRUNS; i++)
223                 ret = bpf_filter(nins != 0 ? pc : NULL, pkt, wirelen, buflen);
224 #endif
225         if (ret != expect) {
226                 if (verbose > 1)
227                         printf("Expected 0x%x but got 0x%x:\t", expect, ret);
228                 if (verbose > 0)
229                         printf("FAILED\n");
230                 return (FAILED);
231         }
232         if (verbose > 1)
233                 printf("Expected and got 0x%x:\t", ret);
234         if (verbose > 0)
235                 printf("PASSED\n");
236
237         return (PASSED);
238 }
239
240 static void
241 sig_handler(int sig)
242 {
243
244         if (expect_signal == 0) {
245                 if (verbose > 1)
246                         printf("Received unexpected signal %d:\t", sig);
247                 if (verbose > 0)
248                         printf("FATAL\n");
249                 exit(FATAL);
250         }
251         if (expect_signal != sig) {
252                 if (verbose > 1)
253                         printf("Expected signal %d but got %d:\t",
254                             expect_signal, sig);
255                 if (verbose > 0)
256                         printf("FAILED\n");
257                 exit(FAILED);
258         }
259
260         if (verbose > 1)
261                 printf("Expected and got signal %d:\t", sig);
262         if (verbose > 0)
263                 printf("PASSED\n");
264
265         exit(PASSED);
266 }