]> The Tcpdump Group git mirrors - libpcap/blob - bpf_filter.c
added DLT/LINKTYPE for openvizsla.org
[libpcap] / bpf_filter.c
1 /*-
2 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from the Stanford/CMU enet packet filter,
6 * (net/enet.c) distributed as part of 4.3BSD, and code contributed
7 * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
8 * Berkeley Laboratory.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)bpf.c 7.5 (Berkeley) 7/15/91
39 */
40
41 #ifdef HAVE_CONFIG_H
42 #include <config.h>
43 #endif
44
45 #include <pcap/pcap-inttypes.h>
46 #include "pcap-types.h"
47 #include "varattrs.h"
48 #include "extract.h"
49
50 #define EXTRACT_SHORT EXTRACT_BE_U_2
51 #define EXTRACT_LONG EXTRACT_BE_U_4
52
53 #ifndef _WIN32
54 #include <sys/param.h>
55 #include <sys/types.h>
56 #include <sys/time.h>
57 #endif /* _WIN32 */
58
59 #include <pcap/bpf.h>
60
61 #include <stdlib.h>
62
63 #ifdef __linux__
64 #include <linux/types.h>
65 #include <linux/if_packet.h>
66 #include <linux/filter.h>
67 #endif
68
69 enum {
70 BPF_S_ANC_NONE,
71 BPF_S_ANC_VLAN_TAG,
72 BPF_S_ANC_VLAN_TAG_PRESENT,
73 };
74
75 /*
76 * Execute the filter program starting at pc on the packet p
77 * wirelen is the length of the original packet
78 * buflen is the amount of data present
79 * aux_data is auxiliary data, currently used only when interpreting
80 * filters intended for the Linux kernel in cases where the kernel
81 * rejects the filter; it contains VLAN tag information
82 * For the kernel, p is assumed to be a pointer to an mbuf if buflen is 0,
83 * in all other cases, p is a pointer to a buffer and buflen is its size.
84 *
85 * Thanks to Ani Sinha <ani@arista.com> for providing initial implementation
86 */
87 #if defined(SKF_AD_VLAN_TAG_PRESENT)
88 u_int
89 bpf_filter_with_aux_data(const struct bpf_insn *pc, const u_char *p,
90 u_int wirelen, u_int buflen, const struct bpf_aux_data *aux_data)
91 #else
92 u_int
93 bpf_filter_with_aux_data(const struct bpf_insn *pc, const u_char *p,
94 u_int wirelen, u_int buflen, const struct bpf_aux_data *aux_data _U_)
95 #endif
96 {
97 register uint32_t A, X;
98 register bpf_u_int32 k;
99 uint32_t mem[BPF_MEMWORDS];
100
101 if (pc == 0)
102 /*
103 * No filter means accept all.
104 */
105 return (u_int)-1;
106 A = 0;
107 X = 0;
108 --pc;
109 for (;;) {
110 ++pc;
111 switch (pc->code) {
112
113 default:
114 abort();
115 case BPF_RET|BPF_K:
116 return (u_int)pc->k;
117
118 case BPF_RET|BPF_A:
119 return (u_int)A;
120
121 case BPF_LD|BPF_W|BPF_ABS:
122 k = pc->k;
123 if (k > buflen || sizeof(int32_t) > buflen - k) {
124 return 0;
125 }
126 A = EXTRACT_LONG(&p[k]);
127 continue;
128
129 case BPF_LD|BPF_H|BPF_ABS:
130 k = pc->k;
131 if (k > buflen || sizeof(int16_t) > buflen - k) {
132 return 0;
133 }
134 A = EXTRACT_SHORT(&p[k]);
135 continue;
136
137 case BPF_LD|BPF_B|BPF_ABS:
138 switch (pc->k) {
139
140 #if defined(SKF_AD_VLAN_TAG_PRESENT)
141 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
142 if (!aux_data)
143 return 0;
144 A = aux_data->vlan_tag;
145 break;
146
147 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
148 if (!aux_data)
149 return 0;
150 A = aux_data->vlan_tag_present;
151 break;
152 #endif
153 default:
154 k = pc->k;
155 if (k >= buflen) {
156 return 0;
157 }
158 A = p[k];
159 break;
160 }
161 continue;
162
163 case BPF_LD|BPF_W|BPF_LEN:
164 A = wirelen;
165 continue;
166
167 case BPF_LDX|BPF_W|BPF_LEN:
168 X = wirelen;
169 continue;
170
171 case BPF_LD|BPF_W|BPF_IND:
172 k = X + pc->k;
173 if (pc->k > buflen || X > buflen - pc->k ||
174 sizeof(int32_t) > buflen - k) {
175 return 0;
176 }
177 A = EXTRACT_LONG(&p[k]);
178 continue;
179
180 case BPF_LD|BPF_H|BPF_IND:
181 k = X + pc->k;
182 if (X > buflen || pc->k > buflen - X ||
183 sizeof(int16_t) > buflen - k) {
184 return 0;
185 }
186 A = EXTRACT_SHORT(&p[k]);
187 continue;
188
189 case BPF_LD|BPF_B|BPF_IND:
190 k = X + pc->k;
191 if (pc->k >= buflen || X >= buflen - pc->k) {
192 return 0;
193 }
194 A = p[k];
195 continue;
196
197 case BPF_LDX|BPF_MSH|BPF_B:
198 k = pc->k;
199 if (k >= buflen) {
200 return 0;
201 }
202 X = (p[pc->k] & 0xf) << 2;
203 continue;
204
205 case BPF_LD|BPF_IMM:
206 A = pc->k;
207 continue;
208
209 case BPF_LDX|BPF_IMM:
210 X = pc->k;
211 continue;
212
213 case BPF_LD|BPF_MEM:
214 A = mem[pc->k];
215 continue;
216
217 case BPF_LDX|BPF_MEM:
218 X = mem[pc->k];
219 continue;
220
221 case BPF_ST:
222 mem[pc->k] = A;
223 continue;
224
225 case BPF_STX:
226 mem[pc->k] = X;
227 continue;
228
229 case BPF_JMP|BPF_JA:
230 /*
231 * XXX - we currently implement "ip6 protochain"
232 * with backward jumps, so sign-extend pc->k.
233 */
234 pc += (bpf_int32)pc->k;
235 continue;
236
237 case BPF_JMP|BPF_JGT|BPF_K:
238 pc += (A > pc->k) ? pc->jt : pc->jf;
239 continue;
240
241 case BPF_JMP|BPF_JGE|BPF_K:
242 pc += (A >= pc->k) ? pc->jt : pc->jf;
243 continue;
244
245 case BPF_JMP|BPF_JEQ|BPF_K:
246 pc += (A == pc->k) ? pc->jt : pc->jf;
247 continue;
248
249 case BPF_JMP|BPF_JSET|BPF_K:
250 pc += (A & pc->k) ? pc->jt : pc->jf;
251 continue;
252
253 case BPF_JMP|BPF_JGT|BPF_X:
254 pc += (A > X) ? pc->jt : pc->jf;
255 continue;
256
257 case BPF_JMP|BPF_JGE|BPF_X:
258 pc += (A >= X) ? pc->jt : pc->jf;
259 continue;
260
261 case BPF_JMP|BPF_JEQ|BPF_X:
262 pc += (A == X) ? pc->jt : pc->jf;
263 continue;
264
265 case BPF_JMP|BPF_JSET|BPF_X:
266 pc += (A & X) ? pc->jt : pc->jf;
267 continue;
268
269 case BPF_ALU|BPF_ADD|BPF_X:
270 A += X;
271 continue;
272
273 case BPF_ALU|BPF_SUB|BPF_X:
274 A -= X;
275 continue;
276
277 case BPF_ALU|BPF_MUL|BPF_X:
278 A *= X;
279 continue;
280
281 case BPF_ALU|BPF_DIV|BPF_X:
282 if (X == 0)
283 return 0;
284 A /= X;
285 continue;
286
287 case BPF_ALU|BPF_MOD|BPF_X:
288 if (X == 0)
289 return 0;
290 A %= X;
291 continue;
292
293 case BPF_ALU|BPF_AND|BPF_X:
294 A &= X;
295 continue;
296
297 case BPF_ALU|BPF_OR|BPF_X:
298 A |= X;
299 continue;
300
301 case BPF_ALU|BPF_XOR|BPF_X:
302 A ^= X;
303 continue;
304
305 case BPF_ALU|BPF_LSH|BPF_X:
306 A <<= X;
307 continue;
308
309 case BPF_ALU|BPF_RSH|BPF_X:
310 A >>= X;
311 continue;
312
313 case BPF_ALU|BPF_ADD|BPF_K:
314 A += pc->k;
315 continue;
316
317 case BPF_ALU|BPF_SUB|BPF_K:
318 A -= pc->k;
319 continue;
320
321 case BPF_ALU|BPF_MUL|BPF_K:
322 A *= pc->k;
323 continue;
324
325 case BPF_ALU|BPF_DIV|BPF_K:
326 A /= pc->k;
327 continue;
328
329 case BPF_ALU|BPF_MOD|BPF_K:
330 A %= pc->k;
331 continue;
332
333 case BPF_ALU|BPF_AND|BPF_K:
334 A &= pc->k;
335 continue;
336
337 case BPF_ALU|BPF_OR|BPF_K:
338 A |= pc->k;
339 continue;
340
341 case BPF_ALU|BPF_XOR|BPF_K:
342 A ^= pc->k;
343 continue;
344
345 case BPF_ALU|BPF_LSH|BPF_K:
346 A <<= pc->k;
347 continue;
348
349 case BPF_ALU|BPF_RSH|BPF_K:
350 A >>= pc->k;
351 continue;
352
353 case BPF_ALU|BPF_NEG:
354 /*
355 * Most BPF arithmetic is unsigned, but negation
356 * can't be unsigned; throw some casts to
357 * specify what we're trying to do.
358 */
359 A = (uint32_t)(-(int32_t)A);
360 continue;
361
362 case BPF_MISC|BPF_TAX:
363 X = A;
364 continue;
365
366 case BPF_MISC|BPF_TXA:
367 A = X;
368 continue;
369 }
370 }
371 }
372
373 u_int
374 bpf_filter(const struct bpf_insn *pc, const u_char *p, u_int wirelen,
375 u_int buflen)
376 {
377 return bpf_filter_with_aux_data(pc, p, wirelen, buflen, NULL);
378 }
379
380 /*
381 * Return true if the 'fcode' is a valid filter program.
382 * The constraints are that each jump be forward and to a valid
383 * code, that memory accesses are within valid ranges (to the
384 * extent that this can be checked statically; loads of packet
385 * data have to be, and are, also checked at run time), and that
386 * the code terminates with either an accept or reject.
387 *
388 * The kernel needs to be able to verify an application's filter code.
389 * Otherwise, a bogus program could easily crash the system.
390 */
391 int
392 bpf_validate(const struct bpf_insn *f, int len)
393 {
394 u_int i, from;
395 const struct bpf_insn *p;
396
397 if (len < 1)
398 return 0;
399
400 for (i = 0; i < (u_int)len; ++i) {
401 p = &f[i];
402 switch (BPF_CLASS(p->code)) {
403 /*
404 * Check that memory operations use valid addresses.
405 */
406 case BPF_LD:
407 case BPF_LDX:
408 switch (BPF_MODE(p->code)) {
409 case BPF_IMM:
410 break;
411 case BPF_ABS:
412 case BPF_IND:
413 case BPF_MSH:
414 /*
415 * There's no maximum packet data size
416 * in userland. The runtime packet length
417 * check suffices.
418 */
419 break;
420 case BPF_MEM:
421 if (p->k >= BPF_MEMWORDS)
422 return 0;
423 break;
424 case BPF_LEN:
425 break;
426 default:
427 return 0;
428 }
429 break;
430 case BPF_ST:
431 case BPF_STX:
432 if (p->k >= BPF_MEMWORDS)
433 return 0;
434 break;
435 case BPF_ALU:
436 switch (BPF_OP(p->code)) {
437 case BPF_ADD:
438 case BPF_SUB:
439 case BPF_MUL:
440 case BPF_OR:
441 case BPF_AND:
442 case BPF_XOR:
443 case BPF_LSH:
444 case BPF_RSH:
445 case BPF_NEG:
446 break;
447 case BPF_DIV:
448 case BPF_MOD:
449 /*
450 * Check for constant division or modulus
451 * by 0.
452 */
453 if (BPF_SRC(p->code) == BPF_K && p->k == 0)
454 return 0;
455 break;
456 default:
457 return 0;
458 }
459 break;
460 case BPF_JMP:
461 /*
462 * Check that jumps are within the code block,
463 * and that unconditional branches don't go
464 * backwards as a result of an overflow.
465 * Unconditional branches have a 32-bit offset,
466 * so they could overflow; we check to make
467 * sure they don't. Conditional branches have
468 * an 8-bit offset, and the from address is <=
469 * BPF_MAXINSNS, and we assume that BPF_MAXINSNS
470 * is sufficiently small that adding 255 to it
471 * won't overflow.
472 *
473 * We know that len is <= BPF_MAXINSNS, and we
474 * assume that BPF_MAXINSNS is < the maximum size
475 * of a u_int, so that i + 1 doesn't overflow.
476 *
477 * For userland, we don't know that the from
478 * or len are <= BPF_MAXINSNS, but we know that
479 * from <= len, and, except on a 64-bit system,
480 * it's unlikely that len, if it truly reflects
481 * the size of the program we've been handed,
482 * will be anywhere near the maximum size of
483 * a u_int. We also don't check for backward
484 * branches, as we currently support them in
485 * userland for the protochain operation.
486 */
487 from = i + 1;
488 switch (BPF_OP(p->code)) {
489 case BPF_JA:
490 if (from + p->k >= (u_int)len)
491 return 0;
492 break;
493 case BPF_JEQ:
494 case BPF_JGT:
495 case BPF_JGE:
496 case BPF_JSET:
497 if (from + p->jt >= (u_int)len || from + p->jf >= (u_int)len)
498 return 0;
499 break;
500 default:
501 return 0;
502 }
503 break;
504 case BPF_RET:
505 break;
506 case BPF_MISC:
507 break;
508 default:
509 return 0;
510 }
511 }
512 return BPF_CLASS(f[len - 1].code) == BPF_RET;
513 }