1 /* From NetBSD: bpf_filter.c,v 1.12 1996/02/13 22:00:00 christos Exp */
4 * Copyright (c) 1990, 1991, 1992, 1993
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 * From: @(#)bpf_filter.c 8.1 (Berkeley) 6/10/93
41 * $Id: bpf_filter.c,v 1.1 1996/04/04 02:45:45 paulus Exp $
44 #include <sys/param.h>
45 #include <sys/types.h>
46 #include <sys/stream.h>
47 #include <net/ppp_defs.h>
52 #include <sys/byteorder.h> /* for ntohl, etc. */
54 /* make sure we don't get the gnu "fixed" one! */
55 #include "/usr/include/sys/byteorder.h"
60 #include <net/net_globals.h>
62 #include <netinet/in.h>
65 #define _NETINET_IN_SYSTM_H_
66 typedef u_long n_long;
68 #include <netinet/in_systm.h>
71 #if !(defined(__i386__) || defined(__m68k__)) /* any others? */
76 #define EXTRACT_SHORT(p) ((ushort)ntohs(*(ushort *)p))
77 #define EXTRACT_LONG(p) (ntohl(*(uint *)p))
79 #define EXTRACT_SHORT(p)\
81 ((ushort)*((u_char *)p+0)<<8|\
82 (ushort)*((u_char *)p+1)<<0))
83 #define EXTRACT_LONG(p)\
84 ((uint)*((u_char *)p+0)<<24|\
85 (uint)*((u_char *)p+1)<<16|\
86 (uint)*((u_char *)p+2)<<8|\
87 (uint)*((u_char *)p+3)<<0)
91 #define MINDEX(len, m, k) \
93 len = m->b_wptr - m->b_rptr; \
99 len = m->b_wptr - m->b_rptr; \
103 static int m_xword __P((mblk_t *, int, int *));
104 static int m_xhalf __P((mblk_t *, int, int *));
109 register int k, *err;
112 register uchar_t *cp, *np;
119 return EXTRACT_LONG(cp);
122 if (m0 == 0 || m0->b_wptr - m0->b_rptr + len - k < 4)
129 return (cp[0] << 24) | (np[0] << 16) | (np[1] << 8) | np[2];
132 return (cp[0] << 24) | (cp[1] << 16) | (np[0] << 8) | np[1];
135 return (cp[0] << 24) | (cp[1] << 16) | (cp[2] << 8) | np[0];
145 register int k, *err;
148 register uchar_t *cp;
155 return EXTRACT_SHORT(cp);
161 return (cp[0] << 8) | m0->b_rptr[0];
171 * Execute the filter program starting at pc on the packet p
172 * wirelen is the length of the original packet
173 * buflen is the amount of data present
176 bpf_filter(pc, p, wirelen, buflen)
177 register struct bpf_insn *pc;
180 register uint buflen;
182 register uint A = 0, X = 0;
184 int mem[BPF_MEMWORDS];
188 * No filter means accept all.
208 case BPF_LD|BPF_W|BPF_ABS:
210 if (k + sizeof(int) > buflen) {
216 A = m_xword((mblk_t *)p, k, &merr);
224 A = EXTRACT_LONG(&p[k]);
227 case BPF_LD|BPF_H|BPF_ABS:
229 if (k + sizeof(short int) > buflen) {
235 A = m_xhalf((mblk_t *)p, k, &merr);
241 A = EXTRACT_SHORT(&p[k]);
244 case BPF_LD|BPF_B|BPF_ABS:
264 case BPF_LD|BPF_W|BPF_LEN:
268 case BPF_LDX|BPF_W|BPF_LEN:
272 case BPF_LD|BPF_W|BPF_IND:
274 if (k + sizeof(int) > buflen) {
280 A = m_xword((mblk_t *)p, k, &merr);
288 A = EXTRACT_LONG(&p[k]);
291 case BPF_LD|BPF_H|BPF_IND:
293 if (k + sizeof(short int) > buflen) {
299 A = m_xhalf((mblk_t *)p, k, &merr);
307 A = EXTRACT_SHORT(&p[k]);
310 case BPF_LD|BPF_B|BPF_IND:
330 case BPF_LDX|BPF_MSH|BPF_B:
341 X = (m->b_rptr[k] & 0xf) << 2;
347 X = (p[pc->k] & 0xf) << 2;
354 case BPF_LDX|BPF_IMM:
362 case BPF_LDX|BPF_MEM:
378 case BPF_JMP|BPF_JGT|BPF_K:
379 pc += (A > pc->k) ? pc->jt : pc->jf;
382 case BPF_JMP|BPF_JGE|BPF_K:
383 pc += (A >= pc->k) ? pc->jt : pc->jf;
386 case BPF_JMP|BPF_JEQ|BPF_K:
387 pc += (A == pc->k) ? pc->jt : pc->jf;
390 case BPF_JMP|BPF_JSET|BPF_K:
391 pc += (A & pc->k) ? pc->jt : pc->jf;
394 case BPF_JMP|BPF_JGT|BPF_X:
395 pc += (A > X) ? pc->jt : pc->jf;
398 case BPF_JMP|BPF_JGE|BPF_X:
399 pc += (A >= X) ? pc->jt : pc->jf;
402 case BPF_JMP|BPF_JEQ|BPF_X:
403 pc += (A == X) ? pc->jt : pc->jf;
406 case BPF_JMP|BPF_JSET|BPF_X:
407 pc += (A & X) ? pc->jt : pc->jf;
410 case BPF_ALU|BPF_ADD|BPF_X:
414 case BPF_ALU|BPF_SUB|BPF_X:
418 case BPF_ALU|BPF_MUL|BPF_X:
422 case BPF_ALU|BPF_DIV|BPF_X:
428 case BPF_ALU|BPF_AND|BPF_X:
432 case BPF_ALU|BPF_OR|BPF_X:
436 case BPF_ALU|BPF_LSH|BPF_X:
440 case BPF_ALU|BPF_RSH|BPF_X:
444 case BPF_ALU|BPF_ADD|BPF_K:
448 case BPF_ALU|BPF_SUB|BPF_K:
452 case BPF_ALU|BPF_MUL|BPF_K:
456 case BPF_ALU|BPF_DIV|BPF_K:
460 case BPF_ALU|BPF_AND|BPF_K:
464 case BPF_ALU|BPF_OR|BPF_K:
468 case BPF_ALU|BPF_LSH|BPF_K:
472 case BPF_ALU|BPF_RSH|BPF_K:
476 case BPF_ALU|BPF_NEG:
480 case BPF_MISC|BPF_TAX:
484 case BPF_MISC|BPF_TXA:
493 * Return true if the 'fcode' is a valid filter program.
494 * The constraints are that each jump be forward and to a valid
495 * code. The code must terminate with either an accept or reject.
496 * 'valid' is an array for use by the routine (it must be at least
499 * The kernel needs to be able to verify an application's filter code.
500 * Otherwise, a bogus program could easily crash the system.
508 register struct bpf_insn *p;
510 for (i = 0; i < len; ++i) {
512 * Check that that jumps are forward, and within
516 if (BPF_CLASS(p->code) == BPF_JMP) {
517 register int from = i + 1;
519 if (BPF_OP(p->code) == BPF_JA) {
520 if (from + p->k >= len)
523 else if (from + p->jt >= len || from + p->jf >= len)
527 * Check that memory operations use valid addresses.
529 if ((BPF_CLASS(p->code) == BPF_ST ||
530 (BPF_CLASS(p->code) == BPF_LD &&
531 (p->code & 0xe0) == BPF_MEM)) &&
532 (p->k >= BPF_MEMWORDS || p->k < 0))
535 * Check for constant division by 0.
537 if (p->code == (BPF_ALU|BPF_DIV|BPF_K) && p->k == 0)
540 return BPF_CLASS(f[len - 1].code) == BPF_RET;