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 * $Id: bpf_filter.c,v 1.8 1997/02/22 09:40:57 peter Exp $
43 #include <sys/param.h>
46 #include <netinet/in.h>
49 #if defined(sparc) || defined(mips) || defined(ibm032)
54 #define EXTRACT_SHORT(p) ((u_short)ntohs(*(u_short *)p))
55 #define EXTRACT_LONG(p) (ntohl(*(u_long *)p))
57 #define EXTRACT_SHORT(p)\
59 ((u_short)*((u_char *)p+0)<<8|\
60 (u_short)*((u_char *)p+1)<<0))
61 #define EXTRACT_LONG(p)\
62 ((u_long)*((u_char *)p+0)<<24|\
63 (u_long)*((u_char *)p+1)<<16|\
64 (u_long)*((u_char *)p+2)<<8|\
65 (u_long)*((u_char *)p+3)<<0)
70 #define MINDEX(m, k) \
72 register int len = m->m_len; \
83 static int m_xhalf __P((struct mbuf *m, int k, int *err));
84 static int m_xword __P((struct mbuf *m, int k, int *err));
88 register struct mbuf *m;
92 register u_char *cp, *np;
93 register struct mbuf *m0;
103 cp = mtod(m, u_char *) + k;
106 return EXTRACT_LONG(cp);
109 if (m0 == 0 || m0->m_len + len - k < 4)
112 np = mtod(m0, u_char *);
116 return (cp[0] << 24) | (np[0] << 16) | (np[1] << 8) | np[2];
119 return (cp[0] << 24) | (cp[1] << 16) | (np[0] << 8) |
123 return (cp[0] << 24) | (cp[1] << 16) | (cp[2] << 8) |
133 register struct mbuf *m;
134 register int k, *err;
138 register struct mbuf *m0;
148 cp = mtod(m, u_char *) + k;
151 return EXTRACT_SHORT(cp);
157 return (cp[0] << 8) | mtod(m0, u_char *)[0];
166 * Execute the filter program starting at pc on the packet p
167 * wirelen is the length of the original packet
168 * buflen is the amount of data present
171 bpf_filter(pc, p, wirelen, buflen)
172 register struct bpf_insn *pc;
175 register u_int buflen;
177 register u_long A = 0, X = 0;
179 long mem[BPF_MEMWORDS];
183 * No filter means accept all.
204 case BPF_LD|BPF_W|BPF_ABS:
206 if (k + sizeof(long) > buflen) {
212 A = m_xword((struct mbuf *)p, k, &merr);
221 if (((int)(p + k) & 3) != 0)
222 A = EXTRACT_LONG(&p[k]);
225 A = ntohl(*(long *)(p + k));
228 case BPF_LD|BPF_H|BPF_ABS:
230 if (k + sizeof(short) > buflen) {
236 A = m_xhalf((struct mbuf *)p, k, &merr);
242 A = EXTRACT_SHORT(&p[k]);
245 case BPF_LD|BPF_B|BPF_ABS:
249 register struct mbuf *m;
253 m = (struct mbuf *)p;
255 A = mtod(m, u_char *)[k];
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(long) > buflen) {
280 A = m_xword((struct mbuf *)p, k, &merr);
289 if (((int)(p + k) & 3) != 0)
290 A = EXTRACT_LONG(&p[k]);
293 A = ntohl(*(long *)(p + k));
296 case BPF_LD|BPF_H|BPF_IND:
298 if (k + sizeof(short) > buflen) {
304 A = m_xhalf((struct mbuf *)p, k, &merr);
312 A = EXTRACT_SHORT(&p[k]);
315 case BPF_LD|BPF_B|BPF_IND:
319 register struct mbuf *m;
323 m = (struct mbuf *)p;
325 A = mtod(m, char *)[k];
334 case BPF_LDX|BPF_MSH|BPF_B:
338 register struct mbuf *m;
342 m = (struct mbuf *)p;
344 X = (mtod(m, char *)[k] & 0xf) << 2;
350 X = (p[pc->k] & 0xf) << 2;
357 case BPF_LDX|BPF_IMM:
365 case BPF_LDX|BPF_MEM:
381 case BPF_JMP|BPF_JGT|BPF_K:
382 pc += (A > pc->k) ? pc->jt : pc->jf;
385 case BPF_JMP|BPF_JGE|BPF_K:
386 pc += (A >= pc->k) ? pc->jt : pc->jf;
389 case BPF_JMP|BPF_JEQ|BPF_K:
390 pc += (A == pc->k) ? pc->jt : pc->jf;
393 case BPF_JMP|BPF_JSET|BPF_K:
394 pc += (A & pc->k) ? pc->jt : pc->jf;
397 case BPF_JMP|BPF_JGT|BPF_X:
398 pc += (A > X) ? pc->jt : pc->jf;
401 case BPF_JMP|BPF_JGE|BPF_X:
402 pc += (A >= X) ? pc->jt : pc->jf;
405 case BPF_JMP|BPF_JEQ|BPF_X:
406 pc += (A == X) ? pc->jt : pc->jf;
409 case BPF_JMP|BPF_JSET|BPF_X:
410 pc += (A & X) ? pc->jt : pc->jf;
413 case BPF_ALU|BPF_ADD|BPF_X:
417 case BPF_ALU|BPF_SUB|BPF_X:
421 case BPF_ALU|BPF_MUL|BPF_X:
425 case BPF_ALU|BPF_DIV|BPF_X:
431 case BPF_ALU|BPF_AND|BPF_X:
435 case BPF_ALU|BPF_OR|BPF_X:
439 case BPF_ALU|BPF_LSH|BPF_X:
443 case BPF_ALU|BPF_RSH|BPF_X:
447 case BPF_ALU|BPF_ADD|BPF_K:
451 case BPF_ALU|BPF_SUB|BPF_K:
455 case BPF_ALU|BPF_MUL|BPF_K:
459 case BPF_ALU|BPF_DIV|BPF_K:
463 case BPF_ALU|BPF_AND|BPF_K:
467 case BPF_ALU|BPF_OR|BPF_K:
471 case BPF_ALU|BPF_LSH|BPF_K:
475 case BPF_ALU|BPF_RSH|BPF_K:
479 case BPF_ALU|BPF_NEG:
483 case BPF_MISC|BPF_TAX:
487 case BPF_MISC|BPF_TXA:
496 * Return true if the 'fcode' is a valid filter program.
497 * The constraints are that each jump be forward and to a valid
498 * code. The code must terminate with either an accept or reject.
499 * 'valid' is an array for use by the routine (it must be at least
502 * The kernel needs to be able to verify an application's filter code.
503 * Otherwise, a bogus program could easily crash the system.
511 register struct bpf_insn *p;
513 for (i = 0; i < len; ++i) {
515 * Check that that jumps are forward, and within
519 if (BPF_CLASS(p->code) == BPF_JMP) {
520 register int from = i + 1;
522 if (BPF_OP(p->code) == BPF_JA) {
523 if (from + p->k >= len)
526 else if (from + p->jt >= len || from + p->jf >= len)
530 * Check that memory operations use valid addresses.
532 if ((BPF_CLASS(p->code) == BPF_ST ||
533 (BPF_CLASS(p->code) == BPF_LD &&
534 (p->code & 0xe0) == BPF_MEM)) &&
535 (p->k >= BPF_MEMWORDS || p->k < 0))
538 * Check for constant division by 0.
540 if (p->code == (BPF_ALU|BPF_DIV|BPF_K) && p->k == 0)
543 return BPF_CLASS(f[len - 1].code) == BPF_RET;