1 /* $Id: bpf_filter.c,v 1.1 1996/04/04 04:22:23 paulus Exp $ */
2 /* From: NetBSD: bpf_filter.c,v 1.11 1995/04/22 13:26:39 cgd Exp $ */
5 * Copyright (c) 1990, 1991, 1992, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from the Stanford/CMU enet packet filter,
9 * (net/enet.c) distributed as part of 4.3BSD, and code contributed
10 * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
11 * Berkeley Laboratory.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * @(#)bpf_filter.c 8.1 (Berkeley) 6/10/93
44 #include <sys/param.h>
45 #include <sys/types.h>
47 #include <net/ppp_defs.h>
49 #if !defined(i386) && !defined(m68k)
53 /* XXX we assume ints are 32 bits, shorts are 16 bits. */
56 #define EXTRACT_SHORT(p) ((unsigned short)ntohs(*(unsigned short *)p))
57 #define EXTRACT_LONG(p) (ntohl(*(u_int32_t *)p))
59 #define EXTRACT_SHORT(p)\
61 ((unsigned short)*((u_char *)p+0)<<8|\
62 (unsigned short)*((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)
73 * Execute the filter program starting at pc on the packet p
74 * wirelen is the length of the original packet
75 * buflen is the amount of data present
78 bpf_filter(pc, p, wirelen, buflen)
79 register struct bpf_insn *pc;
82 register u_int buflen;
84 register u_int32_t A, X;
86 int mem[BPF_MEMWORDS];
90 * No filter means accept all.
111 case BPF_LD|BPF_W|BPF_ABS:
113 if (k + sizeof(int) > buflen) {
116 A = EXTRACT_LONG(&p[k]);
119 case BPF_LD|BPF_H|BPF_ABS:
121 if (k + sizeof(short int) > buflen) {
124 A = EXTRACT_SHORT(&p[k]);
127 case BPF_LD|BPF_B|BPF_ABS:
135 case BPF_LD|BPF_W|BPF_LEN:
139 case BPF_LDX|BPF_W|BPF_LEN:
143 case BPF_LD|BPF_W|BPF_IND:
145 if (k + sizeof(int) > buflen) {
148 A = EXTRACT_LONG(&p[k]);
151 case BPF_LD|BPF_H|BPF_IND:
153 if (k + sizeof(short int) > buflen) {
156 A = EXTRACT_SHORT(&p[k]);
159 case BPF_LD|BPF_B|BPF_IND:
167 case BPF_LDX|BPF_MSH|BPF_B:
172 X = (p[pc->k] & 0xf) << 2;
179 case BPF_LDX|BPF_IMM:
187 case BPF_LDX|BPF_MEM:
203 case BPF_JMP|BPF_JGT|BPF_K:
204 pc += (A > pc->k) ? pc->jt : pc->jf;
207 case BPF_JMP|BPF_JGE|BPF_K:
208 pc += (A >= pc->k) ? pc->jt : pc->jf;
211 case BPF_JMP|BPF_JEQ|BPF_K:
212 pc += (A == pc->k) ? pc->jt : pc->jf;
215 case BPF_JMP|BPF_JSET|BPF_K:
216 pc += (A & pc->k) ? pc->jt : pc->jf;
219 case BPF_JMP|BPF_JGT|BPF_X:
220 pc += (A > X) ? pc->jt : pc->jf;
223 case BPF_JMP|BPF_JGE|BPF_X:
224 pc += (A >= X) ? pc->jt : pc->jf;
227 case BPF_JMP|BPF_JEQ|BPF_X:
228 pc += (A == X) ? pc->jt : pc->jf;
231 case BPF_JMP|BPF_JSET|BPF_X:
232 pc += (A & X) ? pc->jt : pc->jf;
235 case BPF_ALU|BPF_ADD|BPF_X:
239 case BPF_ALU|BPF_SUB|BPF_X:
243 case BPF_ALU|BPF_MUL|BPF_X:
247 case BPF_ALU|BPF_DIV|BPF_X:
253 case BPF_ALU|BPF_AND|BPF_X:
257 case BPF_ALU|BPF_OR|BPF_X:
261 case BPF_ALU|BPF_LSH|BPF_X:
265 case BPF_ALU|BPF_RSH|BPF_X:
269 case BPF_ALU|BPF_ADD|BPF_K:
273 case BPF_ALU|BPF_SUB|BPF_K:
277 case BPF_ALU|BPF_MUL|BPF_K:
281 case BPF_ALU|BPF_DIV|BPF_K:
285 case BPF_ALU|BPF_AND|BPF_K:
289 case BPF_ALU|BPF_OR|BPF_K:
293 case BPF_ALU|BPF_LSH|BPF_K:
297 case BPF_ALU|BPF_RSH|BPF_K:
301 case BPF_ALU|BPF_NEG:
305 case BPF_MISC|BPF_TAX:
309 case BPF_MISC|BPF_TXA: