2 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
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.c 7.5 (Berkeley) 7/15/91
41 #if !(defined(lint) || defined(KERNEL))
42 static const char rcsid[] =
43 "@(#) $Header: /usr/local/cvs/linux/tools/build/e100boot/libpcap-0.4/bpf/net/bpf_filter.c,v 1.1 1999/08/26 10:07:57 johana Exp $ (LBL)";
46 #include <sys/param.h>
47 #include <sys/types.h>
55 #define int32 bpf_int32
56 #define u_int32 bpf_u_int32
59 #if defined(sparc) || defined(mips) || defined(ibm032) || \
60 defined(__alpha) || defined(__hpux)
66 #include <netinet/in.h>
68 #define EXTRACT_SHORT(p) ((u_short)ntohs(*(u_short *)p))
69 #define EXTRACT_LONG(p) (ntohl(*(u_int32 *)p))
71 #define EXTRACT_SHORT(p)\
73 ((u_short)*((u_char *)p+0)<<8|\
74 (u_short)*((u_char *)p+1)<<0))
75 #define EXTRACT_LONG(p)\
76 ((u_int32)*((u_char *)p+0)<<24|\
77 (u_int32)*((u_char *)p+1)<<16|\
78 (u_int32)*((u_char *)p+2)<<8|\
79 (u_int32)*((u_char *)p+3)<<0)
84 #define MINDEX(len, m, k) \
98 register struct mbuf *m;
102 register u_char *cp, *np;
103 register struct mbuf *m0;
106 cp = mtod(m, u_char *) + k;
109 return EXTRACT_LONG(cp);
112 if (m0 == 0 || m0->m_len + len - k < 4)
115 np = mtod(m0, u_char *);
119 return (cp[0] << 24) | (np[0] << 16) | (np[1] << 8) | np[2];
122 return (cp[0] << 24) | (cp[1] << 16) | (np[0] << 8) | np[1];
125 return (cp[0] << 24) | (cp[1] << 16) | (cp[2] << 8) | np[0];
134 register struct mbuf *m;
135 register int k, *err;
139 register struct mbuf *m0;
142 cp = mtod(m, u_char *) + k;
145 return EXTRACT_SHORT(cp);
151 return (cp[0] << 8) | mtod(m0, u_char *)[0];
159 * Execute the filter program starting at pc on the packet p
160 * wirelen is the length of the original packet
161 * buflen is the amount of data present
164 bpf_filter(pc, p, wirelen, buflen)
165 register struct bpf_insn *pc;
168 register u_int buflen;
170 register u_int32 A, X;
172 int32 mem[BPF_MEMWORDS];
176 * No filter means accept all.
198 case BPF_LD|BPF_W|BPF_ABS:
200 if (k + sizeof(int32) > buflen) {
206 A = m_xword((struct mbuf *)p, k, &merr);
214 A = EXTRACT_LONG(&p[k]);
217 case BPF_LD|BPF_H|BPF_ABS:
219 if (k + sizeof(short) > buflen) {
225 A = m_xhalf((struct mbuf *)p, k, &merr);
231 A = EXTRACT_SHORT(&p[k]);
234 case BPF_LD|BPF_B|BPF_ABS:
238 register struct mbuf *m;
243 m = (struct mbuf *)p;
245 A = mtod(m, u_char *)[k];
254 case BPF_LD|BPF_W|BPF_LEN:
258 case BPF_LDX|BPF_W|BPF_LEN:
262 case BPF_LD|BPF_W|BPF_IND:
264 if (k + sizeof(int32) > buflen) {
270 A = m_xword((struct mbuf *)p, k, &merr);
278 A = EXTRACT_LONG(&p[k]);
281 case BPF_LD|BPF_H|BPF_IND:
283 if (k + sizeof(short) > buflen) {
289 A = m_xhalf((struct mbuf *)p, k, &merr);
297 A = EXTRACT_SHORT(&p[k]);
300 case BPF_LD|BPF_B|BPF_IND:
304 register struct mbuf *m;
309 m = (struct mbuf *)p;
311 A = mtod(m, u_char *)[k];
320 case BPF_LDX|BPF_MSH|BPF_B:
324 register struct mbuf *m;
329 m = (struct mbuf *)p;
331 X = (mtod(m, char *)[k] & 0xf) << 2;
337 X = (p[pc->k] & 0xf) << 2;
344 case BPF_LDX|BPF_IMM:
352 case BPF_LDX|BPF_MEM:
368 case BPF_JMP|BPF_JGT|BPF_K:
369 pc += (A > pc->k) ? pc->jt : pc->jf;
372 case BPF_JMP|BPF_JGE|BPF_K:
373 pc += (A >= pc->k) ? pc->jt : pc->jf;
376 case BPF_JMP|BPF_JEQ|BPF_K:
377 pc += (A == pc->k) ? pc->jt : pc->jf;
380 case BPF_JMP|BPF_JSET|BPF_K:
381 pc += (A & pc->k) ? pc->jt : pc->jf;
384 case BPF_JMP|BPF_JGT|BPF_X:
385 pc += (A > X) ? pc->jt : pc->jf;
388 case BPF_JMP|BPF_JGE|BPF_X:
389 pc += (A >= X) ? pc->jt : pc->jf;
392 case BPF_JMP|BPF_JEQ|BPF_X:
393 pc += (A == X) ? pc->jt : pc->jf;
396 case BPF_JMP|BPF_JSET|BPF_X:
397 pc += (A & X) ? pc->jt : pc->jf;
400 case BPF_ALU|BPF_ADD|BPF_X:
404 case BPF_ALU|BPF_SUB|BPF_X:
408 case BPF_ALU|BPF_MUL|BPF_X:
412 case BPF_ALU|BPF_DIV|BPF_X:
418 case BPF_ALU|BPF_AND|BPF_X:
422 case BPF_ALU|BPF_OR|BPF_X:
426 case BPF_ALU|BPF_LSH|BPF_X:
430 case BPF_ALU|BPF_RSH|BPF_X:
434 case BPF_ALU|BPF_ADD|BPF_K:
438 case BPF_ALU|BPF_SUB|BPF_K:
442 case BPF_ALU|BPF_MUL|BPF_K:
446 case BPF_ALU|BPF_DIV|BPF_K:
450 case BPF_ALU|BPF_AND|BPF_K:
454 case BPF_ALU|BPF_OR|BPF_K:
458 case BPF_ALU|BPF_LSH|BPF_K:
462 case BPF_ALU|BPF_RSH|BPF_K:
466 case BPF_ALU|BPF_NEG:
470 case BPF_MISC|BPF_TAX:
474 case BPF_MISC|BPF_TXA:
483 * Return true if the 'fcode' is a valid filter program.
484 * The constraints are that each jump be forward and to a valid
485 * code. The code must terminate with either an accept or reject.
486 * 'valid' is an array for use by the routine (it must be at least
489 * The kernel needs to be able to verify an application's filter code.
490 * Otherwise, a bogus program could easily crash the system.
498 register struct bpf_insn *p;
500 for (i = 0; i < len; ++i) {
502 * Check that that jumps are forward, and within
506 if (BPF_CLASS(p->code) == BPF_JMP) {
507 register int from = i + 1;
509 if (BPF_OP(p->code) == BPF_JA) {
510 if (from + p->k >= (unsigned)len)
513 else if (from + p->jt >= len || from + p->jf >= len)
517 * Check that memory operations use valid addresses.
519 if ((BPF_CLASS(p->code) == BPF_ST ||
520 (BPF_CLASS(p->code) == BPF_LD &&
521 (p->code & 0xe0) == BPF_MEM)) &&
522 (p->k >= BPF_MEMWORDS || p->k < 0))
525 * Check for constant division by 0.
527 if (p->code == (BPF_ALU|BPF_DIV|BPF_K) && p->k == 0)
530 return BPF_CLASS(f[len - 1].code) == BPF_RET;