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) || defined(_KERNEL))
42 static const char rcsid[] _U_ =
43 "@(#) $Header: /tcpdump/master/libpcap/bpf/net/bpf_filter.c,v 1.46 2008-01-02 04:16:46 guy Exp $ (LBL)";
52 #include <pcap-stdinc.h>
61 #ifdef HAVE_SYS_BITYPES_H
62 #include <sys/bitypes.h>
65 #include <sys/param.h>
66 #include <sys/types.h>
69 #define SOLARIS (defined(sun) && (defined(__SVR4) || defined(__svr4__)))
70 #if defined(__hpux) || SOLARIS
71 # include <sys/sysmacros.h>
72 # include <sys/stream.h>
74 # define m_next b_cont
75 # define MLEN(m) ((m)->b_wptr - (m)->b_rptr)
76 # define mtod(m,t) ((t)(m)->b_rptr)
77 #else /* defined(__hpux) || SOLARIS */
78 # define MLEN(m) ((m)->m_len)
79 #endif /* defined(__hpux) || SOLARIS */
85 #if !defined(KERNEL) && !defined(_KERNEL)
89 #define int32 bpf_int32
90 #define u_int32 bpf_u_int32
94 * XXX - IA-64? If not, this probably won't work on Win64 IA-64
95 * systems, unless LBL_ALIGN is defined elsewhere for them.
96 * XXX - SuperH? If not, this probably won't work on WinCE SuperH
97 * systems, unless LBL_ALIGN is defined elsewhere for them.
99 #if defined(sparc) || defined(__sparc__) || defined(mips) || \
100 defined(ibm032) || defined(__alpha) || defined(__hpux) || \
108 #include <netinet/in.h>
111 #define EXTRACT_SHORT(p) ((u_short)ntohs(*(u_short *)p))
112 #define EXTRACT_LONG(p) (ntohl(*(u_int32 *)p))
114 #define EXTRACT_SHORT(p)\
116 ((u_short)*((u_char *)p+0)<<8|\
117 (u_short)*((u_char *)p+1)<<0))
118 #define EXTRACT_LONG(p)\
119 ((u_int32)*((u_char *)p+0)<<24|\
120 (u_int32)*((u_char *)p+1)<<16|\
121 (u_int32)*((u_char *)p+2)<<8|\
122 (u_int32)*((u_char *)p+3)<<0)
125 #if defined(KERNEL) || defined(_KERNEL)
126 # if !defined(__hpux) && !SOLARIS
127 #include <sys/mbuf.h>
129 #define MINDEX(len, _m, _k) \
132 while ((_k) >= len) { \
134 (_m) = (_m)->m_next; \
143 register struct mbuf *m;
144 register int k, *err;
147 register u_char *cp, *np;
148 register struct mbuf *m0;
151 cp = mtod(m, u_char *) + k;
154 return EXTRACT_LONG(cp);
157 if (m0 == 0 || MLEN(m0) + len - k < 4)
160 np = mtod(m0, u_char *);
164 return (cp[0] << 24) | (np[0] << 16) | (np[1] << 8) | np[2];
167 return (cp[0] << 24) | (cp[1] << 16) | (np[0] << 8) | np[1];
170 return (cp[0] << 24) | (cp[1] << 16) | (cp[2] << 8) | np[0];
179 register struct mbuf *m;
180 register int k, *err;
184 register struct mbuf *m0;
187 cp = mtod(m, u_char *) + k;
190 return EXTRACT_SHORT(cp);
196 return (cp[0] << 8) | mtod(m0, u_char *)[0];
204 * Execute the filter program starting at pc on the packet p
205 * wirelen is the length of the original packet
206 * buflen is the amount of data present
207 * For the kernel, p is assumed to be a pointer to an mbuf if buflen is 0,
208 * in all other cases, p is a pointer to a buffer and buflen is its size.
211 bpf_filter(pc, p, wirelen, buflen)
212 register struct bpf_insn *pc;
215 register u_int buflen;
217 register u_int32 A, X;
219 int32 mem[BPF_MEMWORDS];
220 #if defined(KERNEL) || defined(_KERNEL)
225 m = (struct mbuf *)p;
226 p = mtod(m, u_char *);
234 * No filter means accept all.
245 #if defined(KERNEL) || defined(_KERNEL)
256 case BPF_LD|BPF_W|BPF_ABS:
258 if (k + sizeof(int32) > buflen) {
259 #if defined(KERNEL) || defined(_KERNEL)
262 A = m_xword(m, k, &merr);
270 A = EXTRACT_LONG(&p[k]);
273 case BPF_LD|BPF_H|BPF_ABS:
275 if (k + sizeof(short) > buflen) {
276 #if defined(KERNEL) || defined(_KERNEL)
279 A = m_xhalf(m, k, &merr);
287 A = EXTRACT_SHORT(&p[k]);
290 case BPF_LD|BPF_B|BPF_ABS:
293 #if defined(KERNEL) || defined(_KERNEL)
298 A = mtod(n, u_char *)[k];
307 case BPF_LD|BPF_W|BPF_LEN:
311 case BPF_LDX|BPF_W|BPF_LEN:
315 case BPF_LD|BPF_W|BPF_IND:
317 if (k + sizeof(int32) > buflen) {
318 #if defined(KERNEL) || defined(_KERNEL)
321 A = m_xword(m, k, &merr);
329 A = EXTRACT_LONG(&p[k]);
332 case BPF_LD|BPF_H|BPF_IND:
334 if (k + sizeof(short) > buflen) {
335 #if defined(KERNEL) || defined(_KERNEL)
338 A = m_xhalf(m, k, &merr);
346 A = EXTRACT_SHORT(&p[k]);
349 case BPF_LD|BPF_B|BPF_IND:
352 #if defined(KERNEL) || defined(_KERNEL)
357 A = mtod(n, u_char *)[k];
366 case BPF_LDX|BPF_MSH|BPF_B:
369 #if defined(KERNEL) || defined(_KERNEL)
374 X = (mtod(n, char *)[k] & 0xf) << 2;
380 X = (p[pc->k] & 0xf) << 2;
387 case BPF_LDX|BPF_IMM:
395 case BPF_LDX|BPF_MEM:
408 #if defined(KERNEL) || defined(_KERNEL)
410 * No backward jumps allowed.
415 * XXX - we currently implement "ip6 protochain"
416 * with backward jumps, so sign-extend pc->k.
418 pc += (bpf_int32)pc->k;
422 case BPF_JMP|BPF_JGT|BPF_K:
423 pc += (A > pc->k) ? pc->jt : pc->jf;
426 case BPF_JMP|BPF_JGE|BPF_K:
427 pc += (A >= pc->k) ? pc->jt : pc->jf;
430 case BPF_JMP|BPF_JEQ|BPF_K:
431 pc += (A == pc->k) ? pc->jt : pc->jf;
434 case BPF_JMP|BPF_JSET|BPF_K:
435 pc += (A & pc->k) ? pc->jt : pc->jf;
438 case BPF_JMP|BPF_JGT|BPF_X:
439 pc += (A > X) ? pc->jt : pc->jf;
442 case BPF_JMP|BPF_JGE|BPF_X:
443 pc += (A >= X) ? pc->jt : pc->jf;
446 case BPF_JMP|BPF_JEQ|BPF_X:
447 pc += (A == X) ? pc->jt : pc->jf;
450 case BPF_JMP|BPF_JSET|BPF_X:
451 pc += (A & X) ? pc->jt : pc->jf;
454 case BPF_ALU|BPF_ADD|BPF_X:
458 case BPF_ALU|BPF_SUB|BPF_X:
462 case BPF_ALU|BPF_MUL|BPF_X:
466 case BPF_ALU|BPF_DIV|BPF_X:
472 case BPF_ALU|BPF_AND|BPF_X:
476 case BPF_ALU|BPF_OR|BPF_X:
480 case BPF_ALU|BPF_LSH|BPF_X:
484 case BPF_ALU|BPF_RSH|BPF_X:
488 case BPF_ALU|BPF_ADD|BPF_K:
492 case BPF_ALU|BPF_SUB|BPF_K:
496 case BPF_ALU|BPF_MUL|BPF_K:
500 case BPF_ALU|BPF_DIV|BPF_K:
504 case BPF_ALU|BPF_AND|BPF_K:
508 case BPF_ALU|BPF_OR|BPF_K:
512 case BPF_ALU|BPF_LSH|BPF_K:
516 case BPF_ALU|BPF_RSH|BPF_K:
520 case BPF_ALU|BPF_NEG:
524 case BPF_MISC|BPF_TAX:
528 case BPF_MISC|BPF_TXA:
536 * Return true if the 'fcode' is a valid filter program.
537 * The constraints are that each jump be forward and to a valid
538 * code, that memory accesses are within valid ranges (to the
539 * extent that this can be checked statically; loads of packet
540 * data have to be, and are, also checked at run time), and that
541 * the code terminates with either an accept or reject.
543 * The kernel needs to be able to verify an application's filter code.
544 * Otherwise, a bogus program could easily crash the system.
548 const struct bpf_insn *f;
552 const struct bpf_insn *p;
557 * There's no maximum program length in userland.
559 #if defined(KERNEL) || defined(_KERNEL)
560 if (len > BPF_MAXINSNS)
564 for (i = 0; i < len; ++i) {
566 switch (BPF_CLASS(p->code)) {
568 * Check that memory operations use valid addresses.
572 switch (BPF_MODE(p->code)) {
579 * There's no maximum packet data size
580 * in userland. The runtime packet length
583 #if defined(KERNEL) || defined(_KERNEL)
585 * More strict check with actual packet length
588 if (p->k >= bpf_maxbufsize)
593 if (p->k >= BPF_MEMWORDS)
604 if (p->k >= BPF_MEMWORDS)
608 switch (BPF_OP(p->code)) {
620 * Check for constant division by 0.
622 if (BPF_RVAL(p->code) == BPF_K && p->k == 0)
631 * Check that jumps are within the code block,
632 * and that unconditional branches don't go
633 * backwards as a result of an overflow.
634 * Unconditional branches have a 32-bit offset,
635 * so they could overflow; we check to make
636 * sure they don't. Conditional branches have
637 * an 8-bit offset, and the from address is <=
638 * BPF_MAXINSNS, and we assume that BPF_MAXINSNS
639 * is sufficiently small that adding 255 to it
642 * We know that len is <= BPF_MAXINSNS, and we
643 * assume that BPF_MAXINSNS is < the maximum size
644 * of a u_int, so that i + 1 doesn't overflow.
646 * For userland, we don't know that the from
647 * or len are <= BPF_MAXINSNS, but we know that
648 * from <= len, and, except on a 64-bit system,
649 * it's unlikely that len, if it truly reflects
650 * the size of the program we've been handed,
651 * will be anywhere near the maximum size of
652 * a u_int. We also don't check for backward
653 * branches, as we currently support them in
654 * userland for the protochain operation.
657 switch (BPF_OP(p->code)) {
659 #if defined(KERNEL) || defined(_KERNEL)
660 if (from + p->k < from || from + p->k >= len)
662 if (from + p->k >= len)
670 if (from + p->jt >= len || from + p->jf >= len)
685 return BPF_CLASS(f[len - 1].code) == BPF_RET;