2 * Copyright (c) 1990, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
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
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)bpf_filter.c 8.1 (Berkeley) 6/10/93
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
42 #if !defined(_KERNEL) || defined(sun)
43 #include <netinet/in.h>
51 #define EXTRACT_SHORT(p) ((u_int16_t)ntohs(*(u_int16_t *)p))
52 #define EXTRACT_LONG(p) (ntohl(*(u_int32_t *)p))
54 #define EXTRACT_SHORT(p)\
56 ((u_int16_t)*((u_char *)p+0)<<8|\
57 (u_int16_t)*((u_char *)p+1)<<0))
58 #define EXTRACT_LONG(p)\
59 ((u_int32_t)*((u_char *)p+0)<<24|\
60 (u_int32_t)*((u_char *)p+1)<<16|\
61 (u_int32_t)*((u_char *)p+2)<<8|\
62 (u_int32_t)*((u_char *)p+3)<<0)
72 #define MINDEX(m, k) \
74 register int len = m->m_len; \
85 static u_int16_t m_xhalf(struct mbuf *m, bpf_u_int32 k, int *err);
86 static u_int32_t m_xword(struct mbuf *m, bpf_u_int32 k, int *err);
89 m_xword(struct mbuf *m, bpf_u_int32 k, int *err)
103 cp = mtod(m, u_char *) + k;
106 return (EXTRACT_LONG(cp));
109 if (m0 == 0 || m0->m_len + len - k < 4)
112 np = mtod(m0, u_char *);
115 return (((u_int32_t)cp[0] << 24) |
116 ((u_int32_t)np[0] << 16) |
117 ((u_int32_t)np[1] << 8) |
121 return (((u_int32_t)cp[0] << 24) |
122 ((u_int32_t)cp[1] << 16) |
123 ((u_int32_t)np[0] << 8) |
127 return (((u_int32_t)cp[0] << 24) |
128 ((u_int32_t)cp[1] << 16) |
129 ((u_int32_t)cp[2] << 8) |
138 m_xhalf(struct mbuf *m, bpf_u_int32 k, int *err)
152 cp = mtod(m, u_char *) + k;
155 return (EXTRACT_SHORT(cp));
161 return ((cp[0] << 8) | mtod(m0, u_char *)[0]);
169 * Execute the filter program starting at pc on the packet p
170 * wirelen is the length of the original packet
171 * buflen is the amount of data present
174 bpf_filter(const struct bpf_insn *pc, u_char *p, u_int wirelen, u_int buflen)
176 u_int32_t A = 0, X = 0;
178 u_int32_t mem[BPF_MEMWORDS];
182 * No filter means accept all.
198 return ((u_int)pc->k);
203 case BPF_LD|BPF_W|BPF_ABS:
205 if (k > buflen || sizeof(int32_t) > buflen - k) {
211 A = m_xword((struct mbuf *)p, k, &merr);
220 if (((intptr_t)(p + k) & 3) != 0)
221 A = EXTRACT_LONG(&p[k]);
224 A = ntohl(*(int32_t *)(p + k));
227 case BPF_LD|BPF_H|BPF_ABS:
229 if (k > buflen || sizeof(int16_t) > buflen - k) {
235 A = m_xhalf((struct mbuf *)p, k, &merr);
241 A = EXTRACT_SHORT(&p[k]);
244 case BPF_LD|BPF_B|BPF_ABS:
252 m = (struct mbuf *)p;
254 A = mtod(m, u_char *)[k];
263 case BPF_LD|BPF_W|BPF_LEN:
267 case BPF_LDX|BPF_W|BPF_LEN:
271 case BPF_LD|BPF_W|BPF_IND:
273 if (pc->k > buflen || X > buflen - pc->k ||
274 sizeof(int32_t) > buflen - k) {
280 A = m_xword((struct mbuf *)p, k, &merr);
289 if (((intptr_t)(p + k) & 3) != 0)
290 A = EXTRACT_LONG(&p[k]);
293 A = ntohl(*(int32_t *)(p + k));
296 case BPF_LD|BPF_H|BPF_IND:
298 if (X > buflen || pc->k > buflen - X ||
299 sizeof(int16_t) > buflen - k) {
305 A = m_xhalf((struct mbuf *)p, k, &merr);
313 A = EXTRACT_SHORT(&p[k]);
316 case BPF_LD|BPF_B|BPF_IND:
318 if (pc->k >= buflen || X >= buflen - pc->k) {
324 m = (struct mbuf *)p;
326 A = mtod(m, u_char *)[k];
335 case BPF_LDX|BPF_MSH|BPF_B:
339 register struct mbuf *m;
343 m = (struct mbuf *)p;
345 X = (mtod(m, u_char *)[k] & 0xf) << 2;
351 X = (p[pc->k] & 0xf) << 2;
358 case BPF_LDX|BPF_IMM:
366 case BPF_LDX|BPF_MEM:
382 case BPF_JMP|BPF_JGT|BPF_K:
383 pc += (A > pc->k) ? pc->jt : pc->jf;
386 case BPF_JMP|BPF_JGE|BPF_K:
387 pc += (A >= pc->k) ? pc->jt : pc->jf;
390 case BPF_JMP|BPF_JEQ|BPF_K:
391 pc += (A == pc->k) ? pc->jt : pc->jf;
394 case BPF_JMP|BPF_JSET|BPF_K:
395 pc += (A & pc->k) ? pc->jt : pc->jf;
398 case BPF_JMP|BPF_JGT|BPF_X:
399 pc += (A > X) ? pc->jt : pc->jf;
402 case BPF_JMP|BPF_JGE|BPF_X:
403 pc += (A >= X) ? pc->jt : pc->jf;
406 case BPF_JMP|BPF_JEQ|BPF_X:
407 pc += (A == X) ? pc->jt : pc->jf;
410 case BPF_JMP|BPF_JSET|BPF_X:
411 pc += (A & X) ? pc->jt : pc->jf;
414 case BPF_ALU|BPF_ADD|BPF_X:
418 case BPF_ALU|BPF_SUB|BPF_X:
422 case BPF_ALU|BPF_MUL|BPF_X:
426 case BPF_ALU|BPF_DIV|BPF_X:
432 case BPF_ALU|BPF_AND|BPF_X:
436 case BPF_ALU|BPF_OR|BPF_X:
440 case BPF_ALU|BPF_LSH|BPF_X:
444 case BPF_ALU|BPF_RSH|BPF_X:
448 case BPF_ALU|BPF_ADD|BPF_K:
452 case BPF_ALU|BPF_SUB|BPF_K:
456 case BPF_ALU|BPF_MUL|BPF_K:
460 case BPF_ALU|BPF_DIV|BPF_K:
464 case BPF_ALU|BPF_AND|BPF_K:
468 case BPF_ALU|BPF_OR|BPF_K:
472 case BPF_ALU|BPF_LSH|BPF_K:
476 case BPF_ALU|BPF_RSH|BPF_K:
480 case BPF_ALU|BPF_NEG:
484 case BPF_MISC|BPF_TAX:
488 case BPF_MISC|BPF_TXA:
496 static const u_short bpf_code_map[] = {
497 0x10ff, /* 0x00-0x0f: 1111111100001000 */
498 0x3070, /* 0x10-0x1f: 0000111000001100 */
499 0x3131, /* 0x20-0x2f: 1000110010001100 */
500 0x3031, /* 0x30-0x3f: 1000110000001100 */
501 0x3131, /* 0x40-0x4f: 1000110010001100 */
502 0x1011, /* 0x50-0x5f: 1000100000001000 */
503 0x1013, /* 0x60-0x6f: 1100100000001000 */
504 0x1010, /* 0x70-0x7f: 0000100000001000 */
505 0x0093, /* 0x80-0x8f: 1100100100000000 */
506 0x0000, /* 0x90-0x9f: 0000000000000000 */
507 0x0000, /* 0xa0-0xaf: 0000000000000000 */
508 0x0002, /* 0xb0-0xbf: 0100000000000000 */
509 0x0000, /* 0xc0-0xcf: 0000000000000000 */
510 0x0000, /* 0xd0-0xdf: 0000000000000000 */
511 0x0000, /* 0xe0-0xef: 0000000000000000 */
512 0x0000 /* 0xf0-0xff: 0000000000000000 */
515 #define BPF_VALIDATE_CODE(c) \
516 ((c) <= 0xff && (bpf_code_map[(c) >> 4] & (1 << ((c) & 0xf))) != 0)
519 * Return true if the 'fcode' is a valid filter program.
520 * The constraints are that each jump be forward and to a valid
521 * code. The code must terminate with either an accept or reject.
523 * The kernel needs to be able to verify an application's filter code.
524 * Otherwise, a bogus program could easily crash the system.
527 bpf_validate(const struct bpf_insn *f, int len)
530 register const struct bpf_insn *p;
532 /* Do not accept negative length filter. */
536 /* An empty filter means accept all. */
540 for (i = 0; i < len; ++i) {
543 * Check that the code is valid.
545 if (!BPF_VALIDATE_CODE(p->code))
548 * Check that that jumps are forward, and within
551 if (BPF_CLASS(p->code) == BPF_JMP) {
552 register u_int offset;
554 if (p->code == (BPF_JMP|BPF_JA))
557 offset = p->jt > p->jf ? p->jt : p->jf;
558 if (offset >= (u_int)(len - i) - 1)
563 * Check that memory operations use valid addresses.
565 if (p->code == BPF_ST || p->code == BPF_STX ||
566 p->code == (BPF_LD|BPF_MEM) ||
567 p->code == (BPF_LDX|BPF_MEM)) {
568 if (p->k >= BPF_MEMWORDS)
573 * Check for constant division by 0.
575 if (p->code == (BPF_ALU|BPF_DIV|BPF_K) && p->k == 0)
578 return (BPF_CLASS(f[len - 1].code) == BPF_RET);