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 * 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.
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
38 * @(#)bpf_filter.c 8.1 (Berkeley) 6/10/93
40 * $FreeBSD: src/sys/net/bpf_filter.c,v 1.17 1999/12/29 04:38:31 peter Exp $
41 * $DragonFly: src/sys/net/bpf_filter.c,v 1.4 2003/08/26 20:49:47 rob Exp $
44 #include <sys/param.h>
47 #include <netinet/in.h>
50 #if defined(sparc) || defined(mips) || defined(ibm032) || defined(__alpha__)
55 #define EXTRACT_SHORT(p) ((u_int16_t)ntohs(*(u_int16_t *)p))
56 #define EXTRACT_LONG(p) (ntohl(*(u_int32_t *)p))
58 #define EXTRACT_SHORT(p)\
60 ((u_int16_t)*((u_char *)p+0)<<8|\
61 (u_int16_t)*((u_char *)p+1)<<0))
62 #define EXTRACT_LONG(p)\
63 ((u_int32_t)*((u_char *)p+0)<<24|\
64 (u_int32_t)*((u_char *)p+1)<<16|\
65 (u_int32_t)*((u_char *)p+2)<<8|\
66 (u_int32_t)*((u_char *)p+3)<<0)
74 #define MINDEX(m, k) \
87 static u_int16_t m_xhalf (struct mbuf *m, bpf_u_int32 k, int *err);
88 static u_int32_t 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 == 0 || m0->m_len + len - k < 4)
117 np = mtod(m0, u_char *);
122 ((u_int32_t)cp[0] << 24) |
123 ((u_int32_t)np[0] << 16) |
124 ((u_int32_t)np[1] << 8) |
129 ((u_int32_t)cp[0] << 24) |
130 ((u_int32_t)cp[1] << 16) |
131 ((u_int32_t)np[0] << 8) |
136 ((u_int32_t)cp[0] << 24) |
137 ((u_int32_t)cp[1] << 16) |
138 ((u_int32_t)cp[2] << 8) |
164 cp = mtod(m, u_char *) + k;
167 return EXTRACT_SHORT(cp);
173 return (cp[0] << 8) | mtod(m0, u_char *)[0];
181 * Execute the filter program starting at pc on the packet p
182 * wirelen is the length of the original packet
183 * buflen is the amount of data present
186 bpf_filter(pc, p, wirelen, buflen)
187 const struct bpf_insn *pc;
192 u_int32_t A = 0, X = 0;
194 int32_t mem[BPF_MEMWORDS];
198 * No filter means accept all.
219 case BPF_LD|BPF_W|BPF_ABS:
221 if (k > buflen || sizeof(int32_t) > buflen - k) {
227 A = m_xword((struct mbuf *)p, k, &merr);
236 if (((intptr_t)(p + k) & 3) != 0)
237 A = EXTRACT_LONG(&p[k]);
240 A = ntohl(*(int32_t *)(p + k));
243 case BPF_LD|BPF_H|BPF_ABS:
245 if (k > buflen || sizeof(int16_t) > buflen - k) {
251 A = m_xhalf((struct mbuf *)p, k, &merr);
257 A = EXTRACT_SHORT(&p[k]);
260 case BPF_LD|BPF_B|BPF_ABS:
268 m = (struct mbuf *)p;
270 A = mtod(m, u_char *)[k];
279 case BPF_LD|BPF_W|BPF_LEN:
283 case BPF_LDX|BPF_W|BPF_LEN:
287 case BPF_LD|BPF_W|BPF_IND:
289 if (pc->k > buflen || X > buflen - pc->k ||
290 sizeof(int32_t) > buflen - k) {
296 A = m_xword((struct mbuf *)p, k, &merr);
305 if (((intptr_t)(p + k) & 3) != 0)
306 A = EXTRACT_LONG(&p[k]);
309 A = ntohl(*(int32_t *)(p + k));
312 case BPF_LD|BPF_H|BPF_IND:
314 if (X > buflen || pc->k > buflen - X ||
315 sizeof(int16_t) > buflen - k) {
321 A = m_xhalf((struct mbuf *)p, k, &merr);
329 A = EXTRACT_SHORT(&p[k]);
332 case BPF_LD|BPF_B|BPF_IND:
334 if (pc->k >= buflen || X >= buflen - pc->k) {
340 m = (struct mbuf *)p;
342 A = mtod(m, char *)[k];
351 case BPF_LDX|BPF_MSH|BPF_B:
359 m = (struct mbuf *)p;
361 X = (mtod(m, char *)[k] & 0xf) << 2;
367 X = (p[pc->k] & 0xf) << 2;
374 case BPF_LDX|BPF_IMM:
382 case BPF_LDX|BPF_MEM:
398 case BPF_JMP|BPF_JGT|BPF_K:
399 pc += (A > pc->k) ? pc->jt : pc->jf;
402 case BPF_JMP|BPF_JGE|BPF_K:
403 pc += (A >= pc->k) ? pc->jt : pc->jf;
406 case BPF_JMP|BPF_JEQ|BPF_K:
407 pc += (A == pc->k) ? pc->jt : pc->jf;
410 case BPF_JMP|BPF_JSET|BPF_K:
411 pc += (A & pc->k) ? pc->jt : pc->jf;
414 case BPF_JMP|BPF_JGT|BPF_X:
415 pc += (A > X) ? pc->jt : pc->jf;
418 case BPF_JMP|BPF_JGE|BPF_X:
419 pc += (A >= X) ? pc->jt : pc->jf;
422 case BPF_JMP|BPF_JEQ|BPF_X:
423 pc += (A == X) ? pc->jt : pc->jf;
426 case BPF_JMP|BPF_JSET|BPF_X:
427 pc += (A & X) ? pc->jt : pc->jf;
430 case BPF_ALU|BPF_ADD|BPF_X:
434 case BPF_ALU|BPF_SUB|BPF_X:
438 case BPF_ALU|BPF_MUL|BPF_X:
442 case BPF_ALU|BPF_DIV|BPF_X:
448 case BPF_ALU|BPF_AND|BPF_X:
452 case BPF_ALU|BPF_OR|BPF_X:
456 case BPF_ALU|BPF_LSH|BPF_X:
460 case BPF_ALU|BPF_RSH|BPF_X:
464 case BPF_ALU|BPF_ADD|BPF_K:
468 case BPF_ALU|BPF_SUB|BPF_K:
472 case BPF_ALU|BPF_MUL|BPF_K:
476 case BPF_ALU|BPF_DIV|BPF_K:
480 case BPF_ALU|BPF_AND|BPF_K:
484 case BPF_ALU|BPF_OR|BPF_K:
488 case BPF_ALU|BPF_LSH|BPF_K:
492 case BPF_ALU|BPF_RSH|BPF_K:
496 case BPF_ALU|BPF_NEG:
500 case BPF_MISC|BPF_TAX:
504 case BPF_MISC|BPF_TXA:
513 * Return true if the 'fcode' is a valid filter program.
514 * The constraints are that each jump be forward and to a valid
515 * code. The code must terminate with either an accept or reject.
517 * The kernel needs to be able to verify an application's filter code.
518 * Otherwise, a bogus program could easily crash the system.
522 const struct bpf_insn *f;
526 const struct bpf_insn *p;
528 for (i = 0; i < len; ++i) {
530 * Check that that jumps are forward, and within
534 if (BPF_CLASS(p->code) == BPF_JMP) {
537 if (BPF_OP(p->code) == BPF_JA) {
538 if (from >= len || p->k >= len - from)
541 else if (from >= len || p->jt >= len - from ||
546 * Check that memory operations use valid addresses.
548 if ((BPF_CLASS(p->code) == BPF_ST ||
549 (BPF_CLASS(p->code) == BPF_LD &&
550 (p->code & 0xe0) == BPF_MEM)) &&
551 p->k >= BPF_MEMWORDS)
554 * Check for constant division by 0.
556 if (p->code == (BPF_ALU|BPF_DIV|BPF_K) && p->k == 0)
559 return BPF_CLASS(f[len - 1].code) == BPF_RET;