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
43 #include <pcap/pcap-inttypes.h>
44 #include "pcap-types.h"
46 #include "diag-control.h"
48 #define EXTRACT_SHORT EXTRACT_BE_U_2
49 #define EXTRACT_LONG EXTRACT_BE_U_4
52 #include <sys/param.h>
53 #include <sys/types.h>
62 #include <linux/types.h>
63 #include <linux/if_packet.h>
64 #include <linux/filter.h>
70 BPF_S_ANC_VLAN_TAG_PRESENT
,
74 * Execute the filter program starting at pc on the packet p
75 * wirelen is the length of the original packet
76 * buflen is the amount of data present
77 * aux_data is auxiliary data, currently used only when interpreting
78 * filters intended for the Linux kernel in cases where the kernel
79 * rejects the filter; it contains VLAN tag information
80 * For the kernel, p is assumed to be a pointer to an mbuf if buflen is 0,
81 * in all other cases, p is a pointer to a buffer and buflen is its size.
83 * Thanks to Ani Sinha <ani@arista.com> for providing initial implementation
85 #if defined(SKF_AD_VLAN_TAG_PRESENT)
87 pcapint_filter_with_aux_data(const struct bpf_insn
*pc
, const u_char
*p
,
88 u_int wirelen
, u_int buflen
, const struct pcap_bpf_aux_data
*aux_data
)
91 pcapint_filter_with_aux_data(const struct bpf_insn
*pc
, const u_char
*p
,
92 u_int wirelen
, u_int buflen
, const struct pcap_bpf_aux_data
*aux_data _U_
)
95 register uint32_t A
, X
;
96 register bpf_u_int32 k
;
100 * No filter means accept all.
105 uint32_t mem
[BPF_MEMWORDS
] = {0};
119 case BPF_LD
|BPF_W
|BPF_ABS
:
121 if (k
> buflen
|| sizeof(int32_t) > buflen
- k
) {
124 A
= EXTRACT_LONG(&p
[k
]);
127 case BPF_LD
|BPF_H
|BPF_ABS
:
129 if (k
> buflen
|| sizeof(int16_t) > buflen
- k
) {
132 A
= EXTRACT_SHORT(&p
[k
]);
135 case BPF_LD
|BPF_B
|BPF_ABS
:
137 * Yes, we know, this switch doesn't do
138 * anything unless we're building for
139 * a Linux kernel with removed VLAN
140 * tags available as meta-data.
142 DIAG_OFF_DEFAULT_ONLY_SWITCH
145 #if defined(SKF_AD_VLAN_TAG_PRESENT)
146 case SKF_AD_OFF
+ SKF_AD_VLAN_TAG
:
149 A
= aux_data
->vlan_tag
;
152 case SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
:
155 A
= aux_data
->vlan_tag_present
;
166 DIAG_ON_DEFAULT_ONLY_SWITCH
169 case BPF_LD
|BPF_W
|BPF_LEN
:
173 case BPF_LDX
|BPF_W
|BPF_LEN
:
177 case BPF_LD
|BPF_W
|BPF_IND
:
179 if (pc
->k
> buflen
|| X
> buflen
- pc
->k
||
180 sizeof(int32_t) > buflen
- k
) {
183 A
= EXTRACT_LONG(&p
[k
]);
186 case BPF_LD
|BPF_H
|BPF_IND
:
188 if (X
> buflen
|| pc
->k
> buflen
- X
||
189 sizeof(int16_t) > buflen
- k
) {
192 A
= EXTRACT_SHORT(&p
[k
]);
195 case BPF_LD
|BPF_B
|BPF_IND
:
197 if (pc
->k
>= buflen
|| X
>= buflen
- pc
->k
) {
203 case BPF_LDX
|BPF_MSH
|BPF_B
:
208 X
= (p
[pc
->k
] & 0xf) << 2;
215 case BPF_LDX
|BPF_IMM
:
223 case BPF_LDX
|BPF_MEM
:
237 * XXX - we currently implement "ip6 protochain"
238 * with backward jumps, so sign-extend pc->k.
240 pc
+= (bpf_int32
)pc
->k
;
243 case BPF_JMP
|BPF_JGT
|BPF_K
:
244 pc
+= (A
> pc
->k
) ? pc
->jt
: pc
->jf
;
247 case BPF_JMP
|BPF_JGE
|BPF_K
:
248 pc
+= (A
>= pc
->k
) ? pc
->jt
: pc
->jf
;
251 case BPF_JMP
|BPF_JEQ
|BPF_K
:
252 pc
+= (A
== pc
->k
) ? pc
->jt
: pc
->jf
;
255 case BPF_JMP
|BPF_JSET
|BPF_K
:
256 pc
+= (A
& pc
->k
) ? pc
->jt
: pc
->jf
;
259 case BPF_JMP
|BPF_JGT
|BPF_X
:
260 pc
+= (A
> X
) ? pc
->jt
: pc
->jf
;
263 case BPF_JMP
|BPF_JGE
|BPF_X
:
264 pc
+= (A
>= X
) ? pc
->jt
: pc
->jf
;
267 case BPF_JMP
|BPF_JEQ
|BPF_X
:
268 pc
+= (A
== X
) ? pc
->jt
: pc
->jf
;
271 case BPF_JMP
|BPF_JSET
|BPF_X
:
272 pc
+= (A
& X
) ? pc
->jt
: pc
->jf
;
275 case BPF_ALU
|BPF_ADD
|BPF_X
:
279 case BPF_ALU
|BPF_SUB
|BPF_X
:
283 case BPF_ALU
|BPF_MUL
|BPF_X
:
287 case BPF_ALU
|BPF_DIV
|BPF_X
:
293 case BPF_ALU
|BPF_MOD
|BPF_X
:
299 case BPF_ALU
|BPF_AND
|BPF_X
:
303 case BPF_ALU
|BPF_OR
|BPF_X
:
307 case BPF_ALU
|BPF_XOR
|BPF_X
:
311 case BPF_ALU
|BPF_LSH
|BPF_X
:
318 case BPF_ALU
|BPF_RSH
|BPF_X
:
325 case BPF_ALU
|BPF_ADD
|BPF_K
:
329 case BPF_ALU
|BPF_SUB
|BPF_K
:
333 case BPF_ALU
|BPF_MUL
|BPF_K
:
337 case BPF_ALU
|BPF_DIV
|BPF_K
:
341 case BPF_ALU
|BPF_MOD
|BPF_K
:
345 case BPF_ALU
|BPF_AND
|BPF_K
:
349 case BPF_ALU
|BPF_OR
|BPF_K
:
353 case BPF_ALU
|BPF_XOR
|BPF_K
:
357 case BPF_ALU
|BPF_LSH
|BPF_K
:
361 case BPF_ALU
|BPF_RSH
|BPF_K
:
365 case BPF_ALU
|BPF_NEG
:
367 * Most BPF arithmetic is unsigned, but negation
368 * can't be unsigned; respecify it as subtracting
369 * the accumulator from 0U, so that 1) we don't
370 * get compiler warnings about negating an unsigned
371 * value and 2) don't get UBSan warnings about
372 * the result of negating 0x80000000 being undefined.
377 case BPF_MISC
|BPF_TAX
:
381 case BPF_MISC
|BPF_TXA
:
389 pcapint_filter(const struct bpf_insn
*pc
, const u_char
*p
, u_int wirelen
,
392 return pcapint_filter_with_aux_data(pc
, p
, wirelen
, buflen
, NULL
);
396 * Return true if the 'fcode' is a valid filter program.
397 * The constraints are that each jump be forward and to a valid
398 * code, that memory accesses are within valid ranges (to the
399 * extent that this can be checked statically; loads of packet
400 * data have to be, and are, also checked at run time), and that
401 * the code terminates with either an accept or reject.
403 * The kernel needs to be able to verify an application's filter code.
404 * Otherwise, a bogus program could easily crash the system.
407 pcapint_validate_filter(const struct bpf_insn
*f
, int len
)
410 const struct bpf_insn
*p
;
415 for (i
= 0; i
< (u_int
)len
; ++i
) {
417 switch (BPF_CLASS(p
->code
)) {
419 * Check that memory operations use valid addresses.
423 switch (BPF_MODE(p
->code
)) {
430 * There's no maximum packet data size
431 * in userland. The runtime packet length
436 if (p
->k
>= BPF_MEMWORDS
)
447 if (p
->k
>= BPF_MEMWORDS
)
451 switch (BPF_OP(p
->code
)) {
465 * Check for constant division or modulus
468 if (BPF_SRC(p
->code
) == BPF_K
&& p
->k
== 0)
477 * Check that jumps are within the code block,
478 * and that unconditional branches don't go
479 * backwards as a result of an overflow.
480 * Unconditional branches have a 32-bit offset,
481 * so they could overflow; we check to make
482 * sure they don't. Conditional branches have
483 * an 8-bit offset, and the from address is <=
484 * BPF_MAXINSNS, and we assume that BPF_MAXINSNS
485 * is sufficiently small that adding 255 to it
488 * We know that len is <= BPF_MAXINSNS, and we
489 * assume that BPF_MAXINSNS is < the maximum size
490 * of a u_int, so that i + 1 doesn't overflow.
492 * For userland, we don't know that the from
493 * or len are <= BPF_MAXINSNS, but we know that
494 * from <= len, and, except on a 64-bit system,
495 * it's unlikely that len, if it truly reflects
496 * the size of the program we've been handed,
497 * will be anywhere near the maximum size of
498 * a u_int. We also don't check for backward
499 * branches, as we currently support them in
500 * userland for the protochain operation.
503 switch (BPF_OP(p
->code
)) {
505 if (from
+ p
->k
>= (u_int
)len
)
512 if (from
+ p
->jt
>= (u_int
)len
|| from
+ p
->jf
>= (u_int
)len
)
527 return BPF_CLASS(f
[len
- 1].code
) == BPF_RET
;
531 * Exported because older versions of libpcap exported them.
534 bpf_filter(const struct bpf_insn
*pc
, const u_char
*p
, u_int wirelen
,
537 return pcapint_filter(pc
, p
, wirelen
, buflen
);
541 bpf_validate(const struct bpf_insn
*f
, int len
)
543 return pcapint_validate_filter(f
, len
);