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 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)bpf_filter.c 8.1 (Berkeley) 6/10/93
39 #include <sys/param.h>
42 #include <netinet/in.h>
50 #define EXTRACT_SHORT(p) ((u_int16_t)ntohs(*(u_int16_t *)p))
51 #define EXTRACT_LONG(p) (ntohl(*(u_int32_t *)p))
53 #define EXTRACT_SHORT(p)\
55 ((u_int16_t)*((u_char *)p+0)<<8|\
56 (u_int16_t)*((u_char *)p+1)<<0))
57 #define EXTRACT_LONG(p)\
58 ((u_int32_t)*((u_char *)p+0)<<24|\
59 (u_int32_t)*((u_char *)p+1)<<16|\
60 (u_int32_t)*((u_char *)p+2)<<8|\
61 (u_int32_t)*((u_char *)p+3)<<0)
69 #define MINDEX(m, k) \
71 register int len = m->m_len; \
82 static u_int16_t m_xhalf(struct mbuf *m, bpf_u_int32 k, int *err);
83 static u_int32_t m_xword(struct mbuf *m, bpf_u_int32 k, int *err);
87 register struct mbuf *m;
88 register bpf_u_int32 k;
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 *);
117 ((u_int32_t)cp[0] << 24) |
118 ((u_int32_t)np[0] << 16) |
119 ((u_int32_t)np[1] << 8) |
124 ((u_int32_t)cp[0] << 24) |
125 ((u_int32_t)cp[1] << 16) |
126 ((u_int32_t)np[0] << 8) |
131 ((u_int32_t)cp[0] << 24) |
132 ((u_int32_t)cp[1] << 16) |
133 ((u_int32_t)cp[2] << 8) |
143 register struct mbuf *m;
144 register bpf_u_int32 k;
149 register struct mbuf *m0;
159 cp = mtod(m, u_char *) + k;
162 return EXTRACT_SHORT(cp);
168 return (cp[0] << 8) | mtod(m0, u_char *)[0];
176 * Execute the filter program starting at pc on the packet p
177 * wirelen is the length of the original packet
178 * buflen is the amount of data present
181 bpf_filter(pc, p, wirelen, buflen)
182 register const struct bpf_insn *pc;
185 register u_int buflen;
187 register u_int32_t A = 0, X = 0;
188 register bpf_u_int32 k;
189 int32_t mem[BPF_MEMWORDS];
193 * No filter means accept all.
214 case BPF_LD|BPF_W|BPF_ABS:
216 if (k > buflen || sizeof(int32_t) > buflen - k) {
222 A = m_xword((struct mbuf *)p, k, &merr);
231 if (((intptr_t)(p + k) & 3) != 0)
232 A = EXTRACT_LONG(&p[k]);
235 A = ntohl(*(int32_t *)(p + k));
238 case BPF_LD|BPF_H|BPF_ABS:
240 if (k > buflen || sizeof(int16_t) > buflen - k) {
246 A = m_xhalf((struct mbuf *)p, k, &merr);
252 A = EXTRACT_SHORT(&p[k]);
255 case BPF_LD|BPF_B|BPF_ABS:
259 register struct mbuf *m;
263 m = (struct mbuf *)p;
265 A = mtod(m, u_char *)[k];
274 case BPF_LD|BPF_W|BPF_LEN:
278 case BPF_LDX|BPF_W|BPF_LEN:
282 case BPF_LD|BPF_W|BPF_IND:
284 if (pc->k > buflen || X > buflen - pc->k ||
285 sizeof(int32_t) > buflen - k) {
291 A = m_xword((struct mbuf *)p, k, &merr);
300 if (((intptr_t)(p + k) & 3) != 0)
301 A = EXTRACT_LONG(&p[k]);
304 A = ntohl(*(int32_t *)(p + k));
307 case BPF_LD|BPF_H|BPF_IND:
309 if (X > buflen || pc->k > buflen - X ||
310 sizeof(int16_t) > buflen - k) {
316 A = m_xhalf((struct mbuf *)p, k, &merr);
324 A = EXTRACT_SHORT(&p[k]);
327 case BPF_LD|BPF_B|BPF_IND:
329 if (pc->k >= buflen || X >= buflen - pc->k) {
331 register struct mbuf *m;
335 m = (struct mbuf *)p;
337 A = mtod(m, u_char *)[k];
346 case BPF_LDX|BPF_MSH|BPF_B:
350 register struct mbuf *m;
354 m = (struct mbuf *)p;
356 X = (mtod(m, u_char *)[k] & 0xf) << 2;
362 X = (p[pc->k] & 0xf) << 2;
369 case BPF_LDX|BPF_IMM:
377 case BPF_LDX|BPF_MEM:
393 case BPF_JMP|BPF_JGT|BPF_K:
394 pc += (A > pc->k) ? pc->jt : pc->jf;
397 case BPF_JMP|BPF_JGE|BPF_K:
398 pc += (A >= pc->k) ? pc->jt : pc->jf;
401 case BPF_JMP|BPF_JEQ|BPF_K:
402 pc += (A == pc->k) ? pc->jt : pc->jf;
405 case BPF_JMP|BPF_JSET|BPF_K:
406 pc += (A & pc->k) ? pc->jt : pc->jf;
409 case BPF_JMP|BPF_JGT|BPF_X:
410 pc += (A > X) ? pc->jt : pc->jf;
413 case BPF_JMP|BPF_JGE|BPF_X:
414 pc += (A >= X) ? pc->jt : pc->jf;
417 case BPF_JMP|BPF_JEQ|BPF_X:
418 pc += (A == X) ? pc->jt : pc->jf;
421 case BPF_JMP|BPF_JSET|BPF_X:
422 pc += (A & X) ? pc->jt : pc->jf;
425 case BPF_ALU|BPF_ADD|BPF_X:
429 case BPF_ALU|BPF_SUB|BPF_X:
433 case BPF_ALU|BPF_MUL|BPF_X:
437 case BPF_ALU|BPF_DIV|BPF_X:
443 case BPF_ALU|BPF_AND|BPF_X:
447 case BPF_ALU|BPF_OR|BPF_X:
451 case BPF_ALU|BPF_LSH|BPF_X:
455 case BPF_ALU|BPF_RSH|BPF_X:
459 case BPF_ALU|BPF_ADD|BPF_K:
463 case BPF_ALU|BPF_SUB|BPF_K:
467 case BPF_ALU|BPF_MUL|BPF_K:
471 case BPF_ALU|BPF_DIV|BPF_K:
475 case BPF_ALU|BPF_AND|BPF_K:
479 case BPF_ALU|BPF_OR|BPF_K:
483 case BPF_ALU|BPF_LSH|BPF_K:
487 case BPF_ALU|BPF_RSH|BPF_K:
491 case BPF_ALU|BPF_NEG:
495 case BPF_MISC|BPF_TAX:
499 case BPF_MISC|BPF_TXA:
508 * Return true if the 'fcode' is a valid filter program.
509 * The constraints are that each jump be forward and to a valid
510 * code. The code must terminate with either an accept or reject.
512 * The kernel needs to be able to verify an application's filter code.
513 * Otherwise, a bogus program could easily crash the system.
517 const struct bpf_insn *f;
521 register const struct bpf_insn *p;
523 /* Do not accept negative length filter. */
527 /* An empty filter means accept all. */
531 for (i = 0; i < len; ++i) {
533 * Check that that jumps are forward, and within
537 if (BPF_CLASS(p->code) == BPF_JMP) {
538 register int from = i + 1;
540 if (BPF_OP(p->code) == BPF_JA) {
541 if (from >= len || p->k >= len - from)
544 else if (from >= len || p->jt >= len - from ||
549 * Check that memory operations use valid addresses.
551 if ((BPF_CLASS(p->code) == BPF_ST ||
552 (BPF_CLASS(p->code) == BPF_LD &&
553 (p->code & 0xe0) == BPF_MEM)) &&
554 p->k >= BPF_MEMWORDS)
557 * Check for constant division by 0.
559 if (p->code == (BPF_ALU|BPF_DIV|BPF_K) && p->k == 0)
562 return BPF_CLASS(f[len - 1].code) == BPF_RET;