2 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
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.c 7.5 (Berkeley) 7/15/91
45 #include <pcap/pcap-inttypes.h>
46 #include "pcap-types.h"
49 #include <sys/param.h>
50 #include <sys/types.h>
58 #define int32 bpf_int32
59 #define u_int32 bpf_u_int32
63 * XXX - IA-64? If not, this probably won't work on Win64 IA-64
64 * systems, unless LBL_ALIGN is defined elsewhere for them.
65 * XXX - SuperH? If not, this probably won't work on WinCE SuperH
66 * systems, unless LBL_ALIGN is defined elsewhere for them.
68 #if defined(sparc) || defined(__sparc__) || defined(mips) || \
69 defined(ibm032) || defined(__alpha) || defined(__hpux) || \
77 #include <netinet/in.h>
80 #define EXTRACT_SHORT(p) ((u_short)ntohs(*(u_short *)p))
81 #define EXTRACT_LONG(p) (ntohl(*(u_int32 *)p))
83 #define EXTRACT_SHORT(p)\
85 ((u_short)*((u_char *)p+0)<<8|\
86 (u_short)*((u_char *)p+1)<<0))
87 #define EXTRACT_LONG(p)\
88 ((u_int32)*((u_char *)p+0)<<24|\
89 (u_int32)*((u_char *)p+1)<<16|\
90 (u_int32)*((u_char *)p+2)<<8|\
91 (u_int32)*((u_char *)p+3)<<0)
95 #include <linux/types.h>
96 #include <linux/if_packet.h>
97 #include <linux/filter.h>
103 BPF_S_ANC_VLAN_TAG_PRESENT
,
107 * Execute the filter program starting at pc on the packet p
108 * wirelen is the length of the original packet
109 * buflen is the amount of data present
110 * aux_data is auxiliary data, currently used only when interpreting
111 * filters intended for the Linux kernel in cases where the kernel
112 * rejects the filter; it contains VLAN tag information
113 * For the kernel, p is assumed to be a pointer to an mbuf if buflen is 0,
114 * in all other cases, p is a pointer to a buffer and buflen is its size.
116 * Thanks to Ani Sinha <ani@arista.com> for providing initial implementation
119 bpf_filter_with_aux_data(const struct bpf_insn
*pc
, const u_char
*p
,
120 u_int wirelen
, u_int buflen
, const struct bpf_aux_data
*aux_data
)
122 register u_int32 A
, X
;
123 register bpf_u_int32 k
;
124 u_int32 mem
[BPF_MEMWORDS
];
128 * No filter means accept all.
146 case BPF_LD
|BPF_W
|BPF_ABS
:
148 if (k
> buflen
|| sizeof(int32_t) > buflen
- k
) {
151 A
= EXTRACT_LONG(&p
[k
]);
154 case BPF_LD
|BPF_H
|BPF_ABS
:
156 if (k
> buflen
|| sizeof(int16_t) > buflen
- k
) {
159 A
= EXTRACT_SHORT(&p
[k
]);
162 case BPF_LD
|BPF_B
|BPF_ABS
:
165 #if defined(SKF_AD_VLAN_TAG_PRESENT)
166 case SKF_AD_OFF
+ SKF_AD_VLAN_TAG
:
169 A
= aux_data
->vlan_tag
;
172 case SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
:
175 A
= aux_data
->vlan_tag_present
;
188 case BPF_LD
|BPF_W
|BPF_LEN
:
192 case BPF_LDX
|BPF_W
|BPF_LEN
:
196 case BPF_LD
|BPF_W
|BPF_IND
:
198 if (pc
->k
> buflen
|| X
> buflen
- pc
->k
||
199 sizeof(int32_t) > buflen
- k
) {
202 A
= EXTRACT_LONG(&p
[k
]);
205 case BPF_LD
|BPF_H
|BPF_IND
:
207 if (X
> buflen
|| pc
->k
> buflen
- X
||
208 sizeof(int16_t) > buflen
- k
) {
211 A
= EXTRACT_SHORT(&p
[k
]);
214 case BPF_LD
|BPF_B
|BPF_IND
:
216 if (pc
->k
>= buflen
|| X
>= buflen
- pc
->k
) {
222 case BPF_LDX
|BPF_MSH
|BPF_B
:
227 X
= (p
[pc
->k
] & 0xf) << 2;
234 case BPF_LDX
|BPF_IMM
:
242 case BPF_LDX
|BPF_MEM
:
256 * XXX - we currently implement "ip6 protochain"
257 * with backward jumps, so sign-extend pc->k.
259 pc
+= (bpf_int32
)pc
->k
;
262 case BPF_JMP
|BPF_JGT
|BPF_K
:
263 pc
+= (A
> pc
->k
) ? pc
->jt
: pc
->jf
;
266 case BPF_JMP
|BPF_JGE
|BPF_K
:
267 pc
+= (A
>= pc
->k
) ? pc
->jt
: pc
->jf
;
270 case BPF_JMP
|BPF_JEQ
|BPF_K
:
271 pc
+= (A
== pc
->k
) ? pc
->jt
: pc
->jf
;
274 case BPF_JMP
|BPF_JSET
|BPF_K
:
275 pc
+= (A
& pc
->k
) ? pc
->jt
: pc
->jf
;
278 case BPF_JMP
|BPF_JGT
|BPF_X
:
279 pc
+= (A
> X
) ? pc
->jt
: pc
->jf
;
282 case BPF_JMP
|BPF_JGE
|BPF_X
:
283 pc
+= (A
>= X
) ? pc
->jt
: pc
->jf
;
286 case BPF_JMP
|BPF_JEQ
|BPF_X
:
287 pc
+= (A
== X
) ? pc
->jt
: pc
->jf
;
290 case BPF_JMP
|BPF_JSET
|BPF_X
:
291 pc
+= (A
& X
) ? pc
->jt
: pc
->jf
;
294 case BPF_ALU
|BPF_ADD
|BPF_X
:
298 case BPF_ALU
|BPF_SUB
|BPF_X
:
302 case BPF_ALU
|BPF_MUL
|BPF_X
:
306 case BPF_ALU
|BPF_DIV
|BPF_X
:
312 case BPF_ALU
|BPF_MOD
|BPF_X
:
318 case BPF_ALU
|BPF_AND
|BPF_X
:
322 case BPF_ALU
|BPF_OR
|BPF_X
:
326 case BPF_ALU
|BPF_XOR
|BPF_X
:
330 case BPF_ALU
|BPF_LSH
|BPF_X
:
334 case BPF_ALU
|BPF_RSH
|BPF_X
:
338 case BPF_ALU
|BPF_ADD
|BPF_K
:
342 case BPF_ALU
|BPF_SUB
|BPF_K
:
346 case BPF_ALU
|BPF_MUL
|BPF_K
:
350 case BPF_ALU
|BPF_DIV
|BPF_K
:
354 case BPF_ALU
|BPF_MOD
|BPF_K
:
358 case BPF_ALU
|BPF_AND
|BPF_K
:
362 case BPF_ALU
|BPF_OR
|BPF_K
:
366 case BPF_ALU
|BPF_XOR
|BPF_K
:
370 case BPF_ALU
|BPF_LSH
|BPF_K
:
374 case BPF_ALU
|BPF_RSH
|BPF_K
:
378 case BPF_ALU
|BPF_NEG
:
380 * Most BPF arithmetic is unsigned, but negation
381 * can't be unsigned; throw some casts to
382 * specify what we're trying to do.
384 A
= (u_int32
)(-(int32
)A
);
387 case BPF_MISC
|BPF_TAX
:
391 case BPF_MISC
|BPF_TXA
:
399 bpf_filter(const struct bpf_insn
*pc
, const u_char
*p
, u_int wirelen
,
402 return bpf_filter_with_aux_data(pc
, p
, wirelen
, buflen
, NULL
);
407 * Return true if the 'fcode' is a valid filter program.
408 * The constraints are that each jump be forward and to a valid
409 * code, that memory accesses are within valid ranges (to the
410 * extent that this can be checked statically; loads of packet
411 * data have to be, and are, also checked at run time), and that
412 * the code terminates with either an accept or reject.
414 * The kernel needs to be able to verify an application's filter code.
415 * Otherwise, a bogus program could easily crash the system.
418 bpf_validate(const struct bpf_insn
*f
, int len
)
421 const struct bpf_insn
*p
;
426 for (i
= 0; i
< (u_int
)len
; ++i
) {
428 switch (BPF_CLASS(p
->code
)) {
430 * Check that memory operations use valid addresses.
434 switch (BPF_MODE(p
->code
)) {
441 * There's no maximum packet data size
442 * in userland. The runtime packet length
447 if (p
->k
>= BPF_MEMWORDS
)
458 if (p
->k
>= BPF_MEMWORDS
)
462 switch (BPF_OP(p
->code
)) {
476 * Check for constant division or modulus
479 if (BPF_SRC(p
->code
) == BPF_K
&& p
->k
== 0)
488 * Check that jumps are within the code block,
489 * and that unconditional branches don't go
490 * backwards as a result of an overflow.
491 * Unconditional branches have a 32-bit offset,
492 * so they could overflow; we check to make
493 * sure they don't. Conditional branches have
494 * an 8-bit offset, and the from address is <=
495 * BPF_MAXINSNS, and we assume that BPF_MAXINSNS
496 * is sufficiently small that adding 255 to it
499 * We know that len is <= BPF_MAXINSNS, and we
500 * assume that BPF_MAXINSNS is < the maximum size
501 * of a u_int, so that i + 1 doesn't overflow.
503 * For userland, we don't know that the from
504 * or len are <= BPF_MAXINSNS, but we know that
505 * from <= len, and, except on a 64-bit system,
506 * it's unlikely that len, if it truly reflects
507 * the size of the program we've been handed,
508 * will be anywhere near the maximum size of
509 * a u_int. We also don't check for backward
510 * branches, as we currently support them in
511 * userland for the protochain operation.
514 switch (BPF_OP(p
->code
)) {
516 if (from
+ p
->k
>= (u_int
)len
)
523 if (from
+ p
->jt
>= (u_int
)len
|| from
+ p
->jf
>= (u_int
)len
)
538 return BPF_CLASS(f
[len
- 1].code
) == BPF_RET
;