2 * Copyright (c) 1990, 1991, 1993
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_filter.c 8.1 (Berkeley) 6/10/93
40 * $FreeBSD: src/sys/net/bpf_filter.c,v 1.17 1999/12/29 04:38:31 peter Exp $
43 #include <sys/param.h>
46 #include <netinet/in.h>
49 #if defined(sparc) || defined(mips) || defined(ibm032) || defined(__alpha__)
54 #define EXTRACT_SHORT(p) ((u_int16_t)ntohs(*(u_int16_t *)p))
55 #define EXTRACT_LONG(p) (ntohl(*(u_int32_t *)p))
57 #define EXTRACT_SHORT(p)\
59 ((u_int16_t)*((u_char *)p+0)<<8|\
60 (u_int16_t)*((u_char *)p+1)<<0))
61 #define EXTRACT_LONG(p)\
62 ((u_int32_t)*((u_char *)p+0)<<24|\
63 (u_int32_t)*((u_char *)p+1)<<16|\
64 (u_int32_t)*((u_char *)p+2)<<8|\
65 (u_int32_t)*((u_char *)p+3)<<0)
73 #define MINDEX(m, k) \
75 register int len = m->m_len; \
86 static u_int16_t m_xhalf __P((struct mbuf *m, bpf_u_int32 k, int *err));
87 static u_int32_t m_xword __P((struct mbuf *m, bpf_u_int32 k, int *err));
91 register struct mbuf *m;
92 register bpf_u_int32 k;
96 register u_char *cp, *np;
97 register struct mbuf *m0;
107 cp = mtod(m, u_char *) + k;
110 return EXTRACT_LONG(cp);
113 if (m0 == 0 || m0->m_len + len - k < 4)
116 np = mtod(m0, u_char *);
121 ((u_int32_t)cp[0] << 24) |
122 ((u_int32_t)np[0] << 16) |
123 ((u_int32_t)np[1] << 8) |
128 ((u_int32_t)cp[0] << 24) |
129 ((u_int32_t)cp[1] << 16) |
130 ((u_int32_t)np[0] << 8) |
135 ((u_int32_t)cp[0] << 24) |
136 ((u_int32_t)cp[1] << 16) |
137 ((u_int32_t)cp[2] << 8) |
147 register struct mbuf *m;
148 register bpf_u_int32 k;
153 register struct mbuf *m0;
163 cp = mtod(m, u_char *) + k;
166 return EXTRACT_SHORT(cp);
172 return (cp[0] << 8) | mtod(m0, u_char *)[0];
180 * Execute the filter program starting at pc on the packet p
181 * wirelen is the length of the original packet
182 * buflen is the amount of data present
185 bpf_filter(pc, p, wirelen, buflen)
186 register const struct bpf_insn *pc;
189 register u_int buflen;
191 register u_int32_t A = 0, X = 0;
192 register bpf_u_int32 k;
193 int32_t mem[BPF_MEMWORDS];
197 * No filter means accept all.
218 case BPF_LD|BPF_W|BPF_ABS:
220 if (k > buflen || sizeof(int32_t) > buflen - k) {
226 A = m_xword((struct mbuf *)p, k, &merr);
235 if (((intptr_t)(p + k) & 3) != 0)
236 A = EXTRACT_LONG(&p[k]);
239 A = ntohl(*(int32_t *)(p + k));
242 case BPF_LD|BPF_H|BPF_ABS:
244 if (k > buflen || sizeof(int16_t) > buflen - k) {
250 A = m_xhalf((struct mbuf *)p, k, &merr);
256 A = EXTRACT_SHORT(&p[k]);
259 case BPF_LD|BPF_B|BPF_ABS:
263 register struct mbuf *m;
267 m = (struct mbuf *)p;
269 A = mtod(m, u_char *)[k];
278 case BPF_LD|BPF_W|BPF_LEN:
282 case BPF_LDX|BPF_W|BPF_LEN:
286 case BPF_LD|BPF_W|BPF_IND:
288 if (pc->k > buflen || X > buflen - pc->k ||
289 sizeof(int32_t) > buflen - k) {
295 A = m_xword((struct mbuf *)p, k, &merr);
304 if (((intptr_t)(p + k) & 3) != 0)
305 A = EXTRACT_LONG(&p[k]);
308 A = ntohl(*(int32_t *)(p + k));
311 case BPF_LD|BPF_H|BPF_IND:
313 if (X > buflen || pc->k > buflen - X ||
314 sizeof(int16_t) > buflen - k) {
320 A = m_xhalf((struct mbuf *)p, k, &merr);
328 A = EXTRACT_SHORT(&p[k]);
331 case BPF_LD|BPF_B|BPF_IND:
333 if (pc->k >= buflen || X >= buflen - pc->k) {
335 register struct mbuf *m;
339 m = (struct mbuf *)p;
341 A = mtod(m, char *)[k];
350 case BPF_LDX|BPF_MSH|BPF_B:
354 register struct mbuf *m;
358 m = (struct mbuf *)p;
360 X = (mtod(m, char *)[k] & 0xf) << 2;
366 X = (p[pc->k] & 0xf) << 2;
373 case BPF_LDX|BPF_IMM:
381 case BPF_LDX|BPF_MEM:
397 case BPF_JMP|BPF_JGT|BPF_K:
398 pc += (A > pc->k) ? pc->jt : pc->jf;
401 case BPF_JMP|BPF_JGE|BPF_K:
402 pc += (A >= pc->k) ? pc->jt : pc->jf;
405 case BPF_JMP|BPF_JEQ|BPF_K:
406 pc += (A == pc->k) ? pc->jt : pc->jf;
409 case BPF_JMP|BPF_JSET|BPF_K:
410 pc += (A & pc->k) ? pc->jt : pc->jf;
413 case BPF_JMP|BPF_JGT|BPF_X:
414 pc += (A > X) ? pc->jt : pc->jf;
417 case BPF_JMP|BPF_JGE|BPF_X:
418 pc += (A >= X) ? pc->jt : pc->jf;
421 case BPF_JMP|BPF_JEQ|BPF_X:
422 pc += (A == X) ? pc->jt : pc->jf;
425 case BPF_JMP|BPF_JSET|BPF_X:
426 pc += (A & X) ? pc->jt : pc->jf;
429 case BPF_ALU|BPF_ADD|BPF_X:
433 case BPF_ALU|BPF_SUB|BPF_X:
437 case BPF_ALU|BPF_MUL|BPF_X:
441 case BPF_ALU|BPF_DIV|BPF_X:
447 case BPF_ALU|BPF_AND|BPF_X:
451 case BPF_ALU|BPF_OR|BPF_X:
455 case BPF_ALU|BPF_LSH|BPF_X:
459 case BPF_ALU|BPF_RSH|BPF_X:
463 case BPF_ALU|BPF_ADD|BPF_K:
467 case BPF_ALU|BPF_SUB|BPF_K:
471 case BPF_ALU|BPF_MUL|BPF_K:
475 case BPF_ALU|BPF_DIV|BPF_K:
479 case BPF_ALU|BPF_AND|BPF_K:
483 case BPF_ALU|BPF_OR|BPF_K:
487 case BPF_ALU|BPF_LSH|BPF_K:
491 case BPF_ALU|BPF_RSH|BPF_K:
495 case BPF_ALU|BPF_NEG:
499 case BPF_MISC|BPF_TAX:
503 case BPF_MISC|BPF_TXA:
512 * Return true if the 'fcode' is a valid filter program.
513 * The constraints are that each jump be forward and to a valid
514 * code. The code must terminate with either an accept or reject.
516 * The kernel needs to be able to verify an application's filter code.
517 * Otherwise, a bogus program could easily crash the system.
521 const struct bpf_insn *f;
525 register const struct bpf_insn *p;
527 for (i = 0; i < len; ++i) {
529 * Check that that jumps are forward, and within
533 if (BPF_CLASS(p->code) == BPF_JMP) {
534 register int from = i + 1;
536 if (BPF_OP(p->code) == BPF_JA) {
537 if (from >= len || p->k >= len - from)
540 else if (from >= len || p->jt >= len - from ||
545 * Check that memory operations use valid addresses.
547 if ((BPF_CLASS(p->code) == BPF_ST ||
548 (BPF_CLASS(p->code) == BPF_LD &&
549 (p->code & 0xe0) == BPF_MEM)) &&
550 p->k >= BPF_MEMWORDS)
553 * Check for constant division by 0.
555 if (p->code == (BPF_ALU|BPF_DIV|BPF_K) && p->k == 0)
558 return BPF_CLASS(f[len - 1].code) == BPF_RET;