2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1990, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from the Stanford/CMU enet packet filter,
8 * (net/enet.c) distributed as part of 4.3BSD, and code contributed
9 * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
10 * Berkeley Laboratory.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)bpf_filter.c 8.1 (Berkeley) 6/10/93
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
47 #if !defined(_KERNEL) || defined(sun)
48 #include <netinet/in.h>
56 #define EXTRACT_SHORT(p) ((u_int16_t)ntohs(*(u_int16_t *)p))
57 #define EXTRACT_LONG(p) (ntohl(*(u_int32_t *)p))
59 #define EXTRACT_SHORT(p)\
61 ((u_int16_t)*((u_char *)p+0)<<8|\
62 (u_int16_t)*((u_char *)p+1)<<0))
63 #define EXTRACT_LONG(p)\
64 ((u_int32_t)*((u_char *)p+0)<<24|\
65 (u_int32_t)*((u_char *)p+1)<<16|\
66 (u_int32_t)*((u_char *)p+2)<<8|\
67 (u_int32_t)*((u_char *)p+3)<<0)
77 #define MINDEX(m, k) \
90 static u_int16_t m_xhalf(struct mbuf *m, bpf_u_int32 k, int *err);
91 static u_int32_t m_xword(struct mbuf *m, bpf_u_int32 k, int *err);
94 m_xword(struct mbuf *m, bpf_u_int32 k, int *err)
108 cp = mtod(m, u_char *) + k;
111 return (EXTRACT_LONG(cp));
114 if (m0 == NULL || m0->m_len + len - k < 4)
117 np = mtod(m0, u_char *);
120 return (((u_int32_t)cp[0] << 24) |
121 ((u_int32_t)np[0] << 16) |
122 ((u_int32_t)np[1] << 8) |
126 return (((u_int32_t)cp[0] << 24) |
127 ((u_int32_t)cp[1] << 16) |
128 ((u_int32_t)np[0] << 8) |
132 return (((u_int32_t)cp[0] << 24) |
133 ((u_int32_t)cp[1] << 16) |
134 ((u_int32_t)cp[2] << 8) |
143 m_xhalf(struct mbuf *m, bpf_u_int32 k, int *err)
157 cp = mtod(m, u_char *) + k;
160 return (EXTRACT_SHORT(cp));
166 return ((cp[0] << 8) | mtod(m0, u_char *)[0]);
174 * Execute the filter program starting at pc on the packet p
175 * wirelen is the length of the original packet
176 * buflen is the amount of data present
179 bpf_filter(const struct bpf_insn *pc, u_char *p, u_int wirelen, u_int buflen)
181 u_int32_t A = 0, X = 0;
183 u_int32_t mem[BPF_MEMWORDS];
185 bzero(mem, sizeof(mem));
189 * No filter means accept all.
205 return ((u_int)pc->k);
210 case BPF_LD|BPF_W|BPF_ABS:
212 if (k > buflen || sizeof(int32_t) > buflen - k) {
218 A = m_xword((struct mbuf *)p, k, &merr);
227 if (((intptr_t)(p + k) & 3) != 0)
228 A = EXTRACT_LONG(&p[k]);
231 A = ntohl(*(int32_t *)(p + k));
234 case BPF_LD|BPF_H|BPF_ABS:
236 if (k > buflen || sizeof(int16_t) > buflen - k) {
242 A = m_xhalf((struct mbuf *)p, k, &merr);
248 A = EXTRACT_SHORT(&p[k]);
251 case BPF_LD|BPF_B|BPF_ABS:
259 m = (struct mbuf *)p;
261 A = mtod(m, u_char *)[k];
270 case BPF_LD|BPF_W|BPF_LEN:
274 case BPF_LDX|BPF_W|BPF_LEN:
278 case BPF_LD|BPF_W|BPF_IND:
280 if (pc->k > buflen || X > buflen - pc->k ||
281 sizeof(int32_t) > buflen - k) {
287 A = m_xword((struct mbuf *)p, k, &merr);
296 if (((intptr_t)(p + k) & 3) != 0)
297 A = EXTRACT_LONG(&p[k]);
300 A = ntohl(*(int32_t *)(p + k));
303 case BPF_LD|BPF_H|BPF_IND:
305 if (X > buflen || pc->k > buflen - X ||
306 sizeof(int16_t) > buflen - k) {
312 A = m_xhalf((struct mbuf *)p, k, &merr);
320 A = EXTRACT_SHORT(&p[k]);
323 case BPF_LD|BPF_B|BPF_IND:
325 if (pc->k >= buflen || X >= buflen - pc->k) {
331 m = (struct mbuf *)p;
333 A = mtod(m, u_char *)[k];
342 case BPF_LDX|BPF_MSH|BPF_B:
350 m = (struct mbuf *)p;
352 X = (mtod(m, u_char *)[k] & 0xf) << 2;
358 X = (p[pc->k] & 0xf) << 2;
365 case BPF_LDX|BPF_IMM:
373 case BPF_LDX|BPF_MEM:
389 case BPF_JMP|BPF_JGT|BPF_K:
390 pc += (A > pc->k) ? pc->jt : pc->jf;
393 case BPF_JMP|BPF_JGE|BPF_K:
394 pc += (A >= pc->k) ? pc->jt : pc->jf;
397 case BPF_JMP|BPF_JEQ|BPF_K:
398 pc += (A == pc->k) ? pc->jt : pc->jf;
401 case BPF_JMP|BPF_JSET|BPF_K:
402 pc += (A & pc->k) ? pc->jt : pc->jf;
405 case BPF_JMP|BPF_JGT|BPF_X:
406 pc += (A > X) ? pc->jt : pc->jf;
409 case BPF_JMP|BPF_JGE|BPF_X:
410 pc += (A >= X) ? pc->jt : pc->jf;
413 case BPF_JMP|BPF_JEQ|BPF_X:
414 pc += (A == X) ? pc->jt : pc->jf;
417 case BPF_JMP|BPF_JSET|BPF_X:
418 pc += (A & X) ? pc->jt : pc->jf;
421 case BPF_ALU|BPF_ADD|BPF_X:
425 case BPF_ALU|BPF_SUB|BPF_X:
429 case BPF_ALU|BPF_MUL|BPF_X:
433 case BPF_ALU|BPF_DIV|BPF_X:
439 case BPF_ALU|BPF_MOD|BPF_X:
445 case BPF_ALU|BPF_AND|BPF_X:
449 case BPF_ALU|BPF_OR|BPF_X:
453 case BPF_ALU|BPF_XOR|BPF_X:
457 case BPF_ALU|BPF_LSH|BPF_X:
461 case BPF_ALU|BPF_RSH|BPF_X:
465 case BPF_ALU|BPF_ADD|BPF_K:
469 case BPF_ALU|BPF_SUB|BPF_K:
473 case BPF_ALU|BPF_MUL|BPF_K:
477 case BPF_ALU|BPF_DIV|BPF_K:
481 case BPF_ALU|BPF_MOD|BPF_K:
485 case BPF_ALU|BPF_AND|BPF_K:
489 case BPF_ALU|BPF_OR|BPF_K:
493 case BPF_ALU|BPF_XOR|BPF_K:
497 case BPF_ALU|BPF_LSH|BPF_K:
501 case BPF_ALU|BPF_RSH|BPF_K:
505 case BPF_ALU|BPF_NEG:
509 case BPF_MISC|BPF_TAX:
513 case BPF_MISC|BPF_TXA:
521 static const u_short bpf_code_map[] = {
522 0x10ff, /* 0x00-0x0f: 1111111100001000 */
523 0x3070, /* 0x10-0x1f: 0000111000001100 */
524 0x3131, /* 0x20-0x2f: 1000110010001100 */
525 0x3031, /* 0x30-0x3f: 1000110000001100 */
526 0x3131, /* 0x40-0x4f: 1000110010001100 */
527 0x1011, /* 0x50-0x5f: 1000100000001000 */
528 0x1013, /* 0x60-0x6f: 1100100000001000 */
529 0x1010, /* 0x70-0x7f: 0000100000001000 */
530 0x0093, /* 0x80-0x8f: 1100100100000000 */
531 0x1010, /* 0x90-0x9f: 0000100000001000 */
532 0x1010, /* 0xa0-0xaf: 0000100000001000 */
533 0x0002, /* 0xb0-0xbf: 0100000000000000 */
534 0x0000, /* 0xc0-0xcf: 0000000000000000 */
535 0x0000, /* 0xd0-0xdf: 0000000000000000 */
536 0x0000, /* 0xe0-0xef: 0000000000000000 */
537 0x0000 /* 0xf0-0xff: 0000000000000000 */
540 #define BPF_VALIDATE_CODE(c) \
541 ((c) <= 0xff && (bpf_code_map[(c) >> 4] & (1 << ((c) & 0xf))) != 0)
544 * Return true if the 'fcode' is a valid filter program.
545 * The constraints are that each jump be forward and to a valid
546 * code. The code must terminate with either an accept or reject.
548 * The kernel needs to be able to verify an application's filter code.
549 * Otherwise, a bogus program could easily crash the system.
552 bpf_validate(const struct bpf_insn *f, int len)
555 const struct bpf_insn *p;
557 /* Do not accept negative length filter. */
561 /* An empty filter means accept all. */
565 for (i = 0; i < len; ++i) {
568 * Check that the code is valid.
570 if (!BPF_VALIDATE_CODE(p->code))
573 * Check that that jumps are forward, and within
576 if (BPF_CLASS(p->code) == BPF_JMP) {
579 if (p->code == (BPF_JMP|BPF_JA))
582 offset = p->jt > p->jf ? p->jt : p->jf;
583 if (offset >= (u_int)(len - i) - 1)
588 * Check that memory operations use valid addresses.
590 if (p->code == BPF_ST || p->code == BPF_STX ||
591 p->code == (BPF_LD|BPF_MEM) ||
592 p->code == (BPF_LDX|BPF_MEM)) {
593 if (p->k >= BPF_MEMWORDS)
598 * Check for constant division by 0.
600 if ((p->code == (BPF_ALU|BPF_DIV|BPF_K) ||
601 p->code == (BPF_ALU|BPF_MOD|BPF_K)) && p->k == 0)
604 return (BPF_CLASS(f[len - 1].code) == BPF_RET);