1 /* $NetBSD: bpf_filter.c,v 1.34 2008/01/02 15:58:01 christos Exp $ */
4 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
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. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)bpf_filter.c 8.1 (Berkeley) 6/10/93
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: bpf_filter.c,v 1.34 2008/01/02 15:58:01 christos Exp $");
43 #if !(defined(lint) || defined(KERNEL))
44 static const char rcsid
[] =
45 "@(#) Header: bpf_filter.c,v 1.33 97/04/26 13:37:18 leres Exp (LBL)";
49 #include <sys/param.h>
51 #include <sys/endian.h>
53 #define EXTRACT_SHORT(p) be16dec(p)
54 #define EXTRACT_LONG(p) be32dec(p)
58 #define MINDEX(len, m, k) \
70 static int m_xword (struct mbuf
*, uint32_t, int *);
71 static int m_xhalf (struct mbuf
*, uint32_t, int *);
74 m_xword(struct mbuf
*m
, uint32_t k
, int *err
)
82 cp
= mtod(m
, u_char
*) + k
;
85 return EXTRACT_LONG(cp
);
88 if (m0
== 0 || m0
->m_len
+ len
- k
< 4)
91 np
= mtod(m0
, u_char
*);
95 return (cp
[0] << 24) | (np
[0] << 16) | (np
[1] << 8) | np
[2];
98 return (cp
[0] << 24) | (cp
[1] << 16) | (np
[0] << 8) | np
[1];
101 return (cp
[0] << 24) | (cp
[1] << 16) | (cp
[2] << 8) | np
[0];
106 m_xhalf(struct mbuf
*m
, uint32_t k
, int *err
)
114 cp
= mtod(m
, u_char
*) + k
;
117 return EXTRACT_SHORT(cp
);
123 return (cp
[0] << 8) | mtod(m0
, u_char
*)[0];
127 #endif /* !_KERNEL */
132 * Execute the filter program starting at pc on the packet p
133 * wirelen is the length of the original packet
134 * buflen is the amount of data present
137 bpf_filter(struct bpf_insn
*pc
, u_char
*p
, u_int wirelen
, u_int buflen
)
140 uint32_t mem
[BPF_MEMWORDS
];
144 * No filter means accept all.
167 case BPF_LD
|BPF_W
|BPF_ABS
:
169 if (k
+ sizeof(int32_t) > buflen
) {
171 int merr
= 0; /* XXX: GCC */
175 A
= m_xword((struct mbuf
*)p
, k
, &merr
);
183 A
= EXTRACT_LONG(&p
[k
]);
186 case BPF_LD
|BPF_H
|BPF_ABS
:
188 if (k
+ sizeof(int16_t) > buflen
) {
194 A
= m_xhalf((struct mbuf
*)p
, k
, &merr
);
202 A
= EXTRACT_SHORT(&p
[k
]);
205 case BPF_LD
|BPF_B
|BPF_ABS
:
214 m
= (struct mbuf
*)p
;
216 A
= mtod(m
, u_char
*)[k
];
225 case BPF_LD
|BPF_W
|BPF_LEN
:
229 case BPF_LDX
|BPF_W
|BPF_LEN
:
233 case BPF_LD
|BPF_W
|BPF_IND
:
235 if (k
+ sizeof(int32_t) > buflen
) {
237 int merr
= 0; /* XXX: GCC */
241 A
= m_xword((struct mbuf
*)p
, k
, &merr
);
249 A
= EXTRACT_LONG(&p
[k
]);
252 case BPF_LD
|BPF_H
|BPF_IND
:
254 if (k
+ sizeof(int16_t) > buflen
) {
256 int merr
= 0; /* XXX: GCC */
260 A
= m_xhalf((struct mbuf
*)p
, k
, &merr
);
268 A
= EXTRACT_SHORT(&p
[k
]);
271 case BPF_LD
|BPF_B
|BPF_IND
:
280 m
= (struct mbuf
*)p
;
282 A
= mtod(m
, u_char
*)[k
];
291 case BPF_LDX
|BPF_MSH
|BPF_B
:
300 m
= (struct mbuf
*)p
;
302 X
= (mtod(m
, char *)[k
] & 0xf) << 2;
308 X
= (p
[pc
->k
] & 0xf) << 2;
315 case BPF_LDX
|BPF_IMM
:
323 case BPF_LDX
|BPF_MEM
:
339 case BPF_JMP
|BPF_JGT
|BPF_K
:
340 pc
+= (A
> pc
->k
) ? pc
->jt
: pc
->jf
;
343 case BPF_JMP
|BPF_JGE
|BPF_K
:
344 pc
+= (A
>= pc
->k
) ? pc
->jt
: pc
->jf
;
347 case BPF_JMP
|BPF_JEQ
|BPF_K
:
348 pc
+= (A
== pc
->k
) ? pc
->jt
: pc
->jf
;
351 case BPF_JMP
|BPF_JSET
|BPF_K
:
352 pc
+= (A
& pc
->k
) ? pc
->jt
: pc
->jf
;
355 case BPF_JMP
|BPF_JGT
|BPF_X
:
356 pc
+= (A
> X
) ? pc
->jt
: pc
->jf
;
359 case BPF_JMP
|BPF_JGE
|BPF_X
:
360 pc
+= (A
>= X
) ? pc
->jt
: pc
->jf
;
363 case BPF_JMP
|BPF_JEQ
|BPF_X
:
364 pc
+= (A
== X
) ? pc
->jt
: pc
->jf
;
367 case BPF_JMP
|BPF_JSET
|BPF_X
:
368 pc
+= (A
& X
) ? pc
->jt
: pc
->jf
;
371 case BPF_ALU
|BPF_ADD
|BPF_X
:
375 case BPF_ALU
|BPF_SUB
|BPF_X
:
379 case BPF_ALU
|BPF_MUL
|BPF_X
:
383 case BPF_ALU
|BPF_DIV
|BPF_X
:
389 case BPF_ALU
|BPF_AND
|BPF_X
:
393 case BPF_ALU
|BPF_OR
|BPF_X
:
397 case BPF_ALU
|BPF_LSH
|BPF_X
:
401 case BPF_ALU
|BPF_RSH
|BPF_X
:
405 case BPF_ALU
|BPF_ADD
|BPF_K
:
409 case BPF_ALU
|BPF_SUB
|BPF_K
:
413 case BPF_ALU
|BPF_MUL
|BPF_K
:
417 case BPF_ALU
|BPF_DIV
|BPF_K
:
421 case BPF_ALU
|BPF_AND
|BPF_K
:
425 case BPF_ALU
|BPF_OR
|BPF_K
:
429 case BPF_ALU
|BPF_LSH
|BPF_K
:
433 case BPF_ALU
|BPF_RSH
|BPF_K
:
437 case BPF_ALU
|BPF_NEG
:
441 case BPF_MISC
|BPF_TAX
:
445 case BPF_MISC
|BPF_TXA
:
454 * Return true if the 'fcode' is a valid filter program.
455 * The constraints are that each jump be forward and to a valid
456 * code, that memory accesses are within valid ranges (to the
457 * extent that this can be checked statically; loads of packet
458 * data have to be, and are, also checked at run time), and that
459 * the code terminates with either an accept or reject.
461 * The kernel needs to be able to verify an application's filter code.
462 * Otherwise, a bogus program could easily crash the system.
465 bpf_validate(struct bpf_insn
*f
, int len
)
470 if (len
< 1 || len
> BPF_MAXINSNS
)
473 for (i
= 0; i
< len
; ++i
) {
475 switch (BPF_CLASS(p
->code
)) {
477 * Check that memory operations use valid addresses.
481 switch (BPF_MODE(p
->code
)) {
483 if (p
->k
>= BPF_MEMWORDS
)
498 if (p
->k
>= BPF_MEMWORDS
)
502 switch (BPF_OP(p
->code
)) {
514 * Check for constant division by 0.
516 if (BPF_RVAL(p
->code
) == BPF_K
&& p
->k
== 0)
525 * Check that jumps are within the code block,
526 * and that unconditional branches don't go
527 * backwards as a result of an overflow.
528 * Unconditional branches have a 32-bit offset,
529 * so they could overflow; we check to make
530 * sure they don't. Conditional branches have
531 * an 8-bit offset, and the from address is <=
532 * BPF_MAXINSNS, and we assume that BPF_MAXINSNS
533 * is sufficiently small that adding 255 to it
536 * We know that len is <= BPF_MAXINSNS, and we
537 * assume that BPF_MAXINSNS is < the maximum size
538 * of a u_int, so that i + 1 doesn't overflow.
541 switch (BPF_OP(p
->code
)) {
543 if (from
+ p
->k
< from
|| from
+ p
->k
>= len
)
550 if (from
+ p
->jt
>= len
|| from
+ p
->jf
>= len
)
566 return BPF_CLASS(f
[len
- 1].code
) == BPF_RET
;