4 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
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. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * @(#)bpf.c 7.5 (Berkeley) 7/15/91
43 #if !(defined(lint) || defined(KERNEL) || defined(_KERNEL))
44 static const char rcsid[] =
45 "@(#) $Header: /devel/CVS/IP-Filter/bpf_filter.c,v 2.2.2.1 2005/06/18 02:41:30 darrenr Exp $ (LBL)";
48 #include <sys/param.h>
49 #include <sys/types.h>
51 #include <sys/socket.h>
53 #include <netinet/in.h>
56 #include "netinet/ip_compat.h"
60 #if (defined(__hpux) || SOLARIS) && (defined(_KERNEL) || defined(KERNEL))
61 # include <sys/sysmacros.h>
62 # include <sys/stream.h>
67 #if !defined(KERNEL) && !defined(_KERNEL)
71 #define int32 bpf_int32
72 #define u_int32 bpf_u_int32
74 static int m_xword __P((mb_t *, int, int *));
75 static int m_xhalf __P((mb_t *, int, int *));
79 * XXX - IA-64? If not, this probably won't work on Win64 IA-64
80 * systems, unless LBL_ALIGN is defined elsewhere for them.
81 * XXX - SuperH? If not, this probably won't work on WinCE SuperH
82 * systems, unless LBL_ALIGN is defined elsewhere for them.
84 #if defined(sparc) || defined(__sparc__) || defined(mips) || \
85 defined(ibm032) || defined(__alpha) || defined(__hpux) || \
93 #define EXTRACT_SHORT(p) ((u_short)ntohs(*(u_short *)p))
94 #define EXTRACT_LONG(p) (ntohl(*(u_int32 *)p))
96 #define EXTRACT_SHORT(p)\
98 ((u_short)*((u_char *)p+0)<<8|\
99 (u_short)*((u_char *)p+1)<<0))
100 #define EXTRACT_LONG(p)\
101 ((u_int32)*((u_char *)p+0)<<24|\
102 (u_int32)*((u_char *)p+1)<<16|\
103 (u_int32)*((u_char *)p+2)<<8|\
104 (u_int32)*((u_char *)p+3)<<0)
107 #define MINDEX(len, _m, _k) \
110 while ((_k) >= len) { \
112 (_m) = (_m)->m_next; \
122 register int k, *err;
125 register u_char *cp, *np;
129 cp = MTOD(m, u_char *) + k;
132 return EXTRACT_LONG(cp);
135 if (m0 == 0 || M_LEN(m0) + len - k < 4)
138 np = MTOD(m0, u_char *);
142 return (cp[0] << 24) | (np[0] << 16) | (np[1] << 8) | np[2];
145 return (cp[0] << 24) | (cp[1] << 16) | (np[0] << 8) | np[1];
148 return (cp[0] << 24) | (cp[1] << 16) | (cp[2] << 8) | np[0];
158 register int k, *err;
165 cp = MTOD(m, u_char *) + k;
168 return EXTRACT_SHORT(cp);
174 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
184 * For the kernel, p is assumed to be a pointer to an mbuf if buflen is 0,
185 * in all other cases, p is a pointer to a buffer and buflen is its size.
188 bpf_filter(pc, p, wirelen, buflen)
189 register struct bpf_insn *pc;
192 register u_int buflen;
194 register u_int32 A, X;
196 int32 mem[BPF_MEMWORDS];
202 p = MTOD(m, u_char *);
209 * No filter means accept all.
227 case BPF_LD|BPF_W|BPF_ABS:
229 if (k + sizeof(int32) > buflen) {
232 A = m_xword(m, k, &merr);
237 A = EXTRACT_LONG(&p[k]);
240 case BPF_LD|BPF_H|BPF_ABS:
242 if (k + sizeof(short) > buflen) {
245 A = m_xhalf(m, k, &merr);
250 A = EXTRACT_SHORT(&p[k]);
253 case BPF_LD|BPF_B|BPF_ABS:
260 A = MTOD(n, u_char *)[k];
266 case BPF_LD|BPF_W|BPF_LEN:
270 case BPF_LDX|BPF_W|BPF_LEN:
274 case BPF_LD|BPF_W|BPF_IND:
276 if (k + sizeof(int32) > buflen) {
279 A = m_xword(m, k, &merr);
284 A = EXTRACT_LONG(&p[k]);
287 case BPF_LD|BPF_H|BPF_IND:
289 if (k + sizeof(short) > buflen) {
292 A = m_xhalf(m, k, &merr);
297 A = EXTRACT_SHORT(&p[k]);
300 case BPF_LD|BPF_B|BPF_IND:
307 A = MTOD(n, u_char *)[k];
313 case BPF_LDX|BPF_MSH|BPF_B:
320 X = (MTOD(n, char *)[k] & 0xf) << 2;
323 X = (p[pc->k] & 0xf) << 2;
330 case BPF_LDX|BPF_IMM:
338 case BPF_LDX|BPF_MEM:
354 case BPF_JMP|BPF_JGT|BPF_K:
355 pc += (A > pc->k) ? pc->jt : pc->jf;
358 case BPF_JMP|BPF_JGE|BPF_K:
359 pc += (A >= pc->k) ? pc->jt : pc->jf;
362 case BPF_JMP|BPF_JEQ|BPF_K:
363 pc += (A == pc->k) ? pc->jt : pc->jf;
366 case BPF_JMP|BPF_JSET|BPF_K:
367 pc += (A & pc->k) ? pc->jt : pc->jf;
370 case BPF_JMP|BPF_JGT|BPF_X:
371 pc += (A > X) ? pc->jt : pc->jf;
374 case BPF_JMP|BPF_JGE|BPF_X:
375 pc += (A >= X) ? pc->jt : pc->jf;
378 case BPF_JMP|BPF_JEQ|BPF_X:
379 pc += (A == X) ? pc->jt : pc->jf;
382 case BPF_JMP|BPF_JSET|BPF_X:
383 pc += (A & X) ? pc->jt : pc->jf;
386 case BPF_ALU|BPF_ADD|BPF_X:
390 case BPF_ALU|BPF_SUB|BPF_X:
394 case BPF_ALU|BPF_MUL|BPF_X:
398 case BPF_ALU|BPF_DIV|BPF_X:
404 case BPF_ALU|BPF_AND|BPF_X:
408 case BPF_ALU|BPF_OR|BPF_X:
412 case BPF_ALU|BPF_LSH|BPF_X:
416 case BPF_ALU|BPF_RSH|BPF_X:
420 case BPF_ALU|BPF_ADD|BPF_K:
424 case BPF_ALU|BPF_SUB|BPF_K:
428 case BPF_ALU|BPF_MUL|BPF_K:
432 case BPF_ALU|BPF_DIV|BPF_K:
436 case BPF_ALU|BPF_AND|BPF_K:
440 case BPF_ALU|BPF_OR|BPF_K:
444 case BPF_ALU|BPF_LSH|BPF_K:
448 case BPF_ALU|BPF_RSH|BPF_K:
452 case BPF_ALU|BPF_NEG:
456 case BPF_MISC|BPF_TAX:
460 case BPF_MISC|BPF_TXA:
469 * Return true if the 'fcode' is a valid filter program.
470 * The constraints are that each jump be forward and to a valid
471 * code. The code must terminate with either an accept or reject.
472 * 'valid' is an array for use by the routine (it must be at least
475 * The kernel needs to be able to verify an application's filter code.
476 * Otherwise, a bogus program could easily crash the system.
484 register struct bpf_insn *p;
486 for (i = 0; i < len; ++i) {
488 * Check that that jumps are forward, and within
492 if (BPF_CLASS(p->code) == BPF_JMP) {
493 register int from = i + 1;
495 if (BPF_OP(p->code) == BPF_JA) {
496 if (from + p->k >= (unsigned)len)
499 else if (from + p->jt >= len || from + p->jf >= len)
503 * Check that memory operations use valid addresses.
505 if ((BPF_CLASS(p->code) == BPF_ST ||
506 (BPF_CLASS(p->code) == BPF_LD &&
507 (p->code & 0xe0) == BPF_MEM)) &&
508 (p->k >= BPF_MEMWORDS || p->k < 0))
511 * Check for constant division by 0.
513 if (p->code == (BPF_ALU|BPF_DIV|BPF_K) && p->k == 0)
516 return BPF_CLASS(f[len - 1].code) == BPF_RET;