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"
47 #include "portability.h"
50 #include <sys/param.h>
51 #include <sys/types.h>
60 * If we have versions of GCC or Clang that support an __attribute__
61 * to say "if we're building with unsigned behavior sanitization,
62 * don't complain about undefined behavior in this function", we
63 * label these functions with that attribute - we *know* it's undefined
64 * in the C standard, but we *also* know it does what we want with
65 * the ISA we're targeting and the compiler we're using.
67 * For GCC 4.9.0 and later, we use __attribute__((no_sanitize_undefined));
68 * pre-5.0 GCC doesn't have __has_attribute, and I'm not sure whether
69 * GCC or Clang first had __attribute__((no_sanitize(XXX)).
71 * For Clang, we check for __attribute__((no_sanitize(XXX)) with
72 * __has_attribute, as there are versions of Clang that support
73 * __attribute__((no_sanitize("undefined")) but don't support
74 * __attribute__((no_sanitize_undefined)).
76 * We define this here, rather than in funcattrs.h, because we
77 * only want it used here, we don't want it to be broadly used.
78 * (Any printer will get this defined, but this should at least
79 * make it harder for people to find.)
81 #if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 409)
82 #define UNALIGNED_OK __attribute__((no_sanitize_undefined))
83 #elif __has_attribute(no_sanitize)
84 #define UNALIGNED_OK __attribute__((no_sanitize("undefined")))
89 #if (defined(__i386__) || defined(_M_IX86) || defined(__X86__) || defined(__x86_64__) || defined(_M_X64)) || \
90 (defined(__arm__) || defined(_M_ARM) || defined(__aarch64__)) || \
91 (defined(__m68k__) && (!defined(__mc68000__) && !defined(__mc68010__))) || \
92 (defined(__ppc__) || defined(__ppc64__) || defined(_M_PPC) || defined(_ARCH_PPC) || defined(_ARCH_PPC64)) || \
93 (defined(__s390__) || defined(__s390x__) || defined(__zarch__))
95 * The processor natively handles unaligned loads, so we can just
96 * cast the pointer and fetch through it.
98 * XXX - are those all the x86 tests we need?
99 * XXX - do we need to worry about ARMv1 through ARMv5, which didn't
100 * support unaligned loads, and, if so, do we need to worry about all
101 * of them, or just some of them, e.g. ARMv5?
102 * XXX - are those the only 68k tests we need not to generated
103 * unaligned accesses if the target is the 68000 or 68010?
104 * XXX - are there any tests we don't need, because some definitions are for
105 * compilers that also predefine the GCC symbols?
106 * XXX - do we need to test for both 32-bit and 64-bit versions of those
107 * architectures in all cases?
109 UNALIGNED_OK
static inline uint16_t
110 EXTRACT_SHORT(const void *p
)
112 return ((uint16_t)ntohs(*(const uint16_t *)(p
)));
115 UNALIGNED_OK
static inline uint32_t
116 EXTRACT_LONG(const void *p
)
118 return ((uint32_t)ntohl(*(const uint32_t *)(p
)));
121 #elif PCAP_IS_AT_LEAST_GNUC_VERSION(2,0) && \
122 (defined(__alpha) || defined(__alpha__) || \
123 defined(__mips) || defined(__mips__))
125 * This is MIPS or Alpha, which don't natively handle unaligned loads,
126 * but which have instructions that can help when doing unaligned
127 * loads, and this is GCC 2.0 or later or a compiler that claims to
128 * be GCC 2.0 or later, which we assume that mean we have
129 * __attribute__((packed)), which we can use to convince the compiler
130 * to generate those instructions.
132 * Declare packed structures containing a uint16_t and a uint32_t,
133 * cast the pointer to point to one of those, and fetch through it;
134 * the GCC manual doesn't appear to explicitly say that
135 * __attribute__((packed)) causes the compiler to generate unaligned-safe
136 * code, but it apppears to do so.
138 * We do this in case the compiler can generate code using those
139 * instructions to do an unaligned load and pass stuff to "ntohs()" or
140 * "ntohl()", which might be better than than the code to fetch the
141 * bytes one at a time and assemble them. (That might not be the
142 * case on a little-endian platform, such as DEC's MIPS machines and
143 * Alpha machines, where "ntohs()" and "ntohl()" might not be done
146 * We do this only for specific architectures because, for example,
147 * at least some versions of GCC, when compiling for 64-bit SPARC,
148 * generate code that assumes alignment if we do this.
150 * XXX - add other architectures and compilers as possible and
153 * HP's C compiler, indicated by __HP_cc being defined, supports
154 * "#pragma unaligned N" in version A.05.50 and later, where "N"
155 * specifies a number of bytes at which the typedef on the next
156 * line is aligned, e.g.
159 * typedef uint16_t unaligned_uint16_t;
161 * to define unaligned_uint16_t as a 16-bit unaligned data type.
162 * This could be presumably used, in sufficiently recent versions of
163 * the compiler, with macros similar to those below. This would be
164 * useful only if that compiler could generate better code for PA-RISC
165 * or Itanium than would be generated by a bunch of shifts-and-ORs.
167 * DEC C, indicated by __DECC being defined, has, at least on Alpha,
168 * an __unaligned qualifier that can be applied to pointers to get the
169 * compiler to generate code that does unaligned loads and stores when
170 * dereferencing the pointer in question.
172 * XXX - what if the native C compiler doesn't support
173 * __attribute__((packed))? How can we get it to generate unaligned
174 * accesses for *specific* items?
178 } __attribute__((packed
)) unaligned_uint16_t
;
182 } __attribute__((packed
)) unaligned_uint32_t
;
184 UNALIGNED_OK
static inline uint16_t
185 EXTRACT_SHORT(const void *p
)
187 return ((uint16_t)ntohs(((const unaligned_uint16_t
*)(p
))->val
));
190 UNALIGNED_OK
static inline uint32_t
191 EXTRACT_LONG(const void *p
)
193 return ((uint32_t)ntohl(((const unaligned_uint32_t
*)(p
))->val
));
197 * This architecture doesn't natively support unaligned loads, and either
198 * this isn't a GCC-compatible compiler, we don't have __attribute__,
199 * or we do but we don't know of any better way with this instruction
200 * set to do unaligned loads, so do unaligned loads of big-endian
201 * quantities the hard way - fetch the bytes one at a time and
204 #define EXTRACT_SHORT(p) \
205 ((uint16_t)(((uint16_t)(*((const uint8_t *)(p) + 0)) << 8) | \
206 ((uint16_t)(*((const uint8_t *)(p) + 1)) << 0)))
207 #define EXTRACT_LONG(p) \
208 ((uint32_t)(((uint32_t)(*((const uint8_t *)(p) + 0)) << 24) | \
209 ((uint32_t)(*((const uint8_t *)(p) + 1)) << 16) | \
210 ((uint32_t)(*((const uint8_t *)(p) + 2)) << 8) | \
211 ((uint32_t)(*((const uint8_t *)(p) + 3)) << 0)))
212 #endif /* unaligned access checks */
215 #include <linux/types.h>
216 #include <linux/if_packet.h>
217 #include <linux/filter.h>
223 BPF_S_ANC_VLAN_TAG_PRESENT
,
227 * Execute the filter program starting at pc on the packet p
228 * wirelen is the length of the original packet
229 * buflen is the amount of data present
230 * aux_data is auxiliary data, currently used only when interpreting
231 * filters intended for the Linux kernel in cases where the kernel
232 * rejects the filter; it contains VLAN tag information
233 * For the kernel, p is assumed to be a pointer to an mbuf if buflen is 0,
234 * in all other cases, p is a pointer to a buffer and buflen is its size.
236 * Thanks to Ani Sinha <ani@arista.com> for providing initial implementation
239 bpf_filter_with_aux_data(const struct bpf_insn
*pc
, const u_char
*p
,
240 u_int wirelen
, u_int buflen
, const struct bpf_aux_data
*aux_data
)
242 register u_int32_t A
, X
;
243 register bpf_u_int32 k
;
244 u_int32_t mem
[BPF_MEMWORDS
];
248 * No filter means accept all.
266 case BPF_LD
|BPF_W
|BPF_ABS
:
268 if (k
> buflen
|| sizeof(int32_t) > buflen
- k
) {
271 A
= EXTRACT_LONG(&p
[k
]);
274 case BPF_LD
|BPF_H
|BPF_ABS
:
276 if (k
> buflen
|| sizeof(int16_t) > buflen
- k
) {
279 A
= EXTRACT_SHORT(&p
[k
]);
282 case BPF_LD
|BPF_B
|BPF_ABS
:
285 #if defined(SKF_AD_VLAN_TAG_PRESENT)
286 case SKF_AD_OFF
+ SKF_AD_VLAN_TAG
:
289 A
= aux_data
->vlan_tag
;
292 case SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
:
295 A
= aux_data
->vlan_tag_present
;
308 case BPF_LD
|BPF_W
|BPF_LEN
:
312 case BPF_LDX
|BPF_W
|BPF_LEN
:
316 case BPF_LD
|BPF_W
|BPF_IND
:
318 if (pc
->k
> buflen
|| X
> buflen
- pc
->k
||
319 sizeof(int32_t) > buflen
- k
) {
322 A
= EXTRACT_LONG(&p
[k
]);
325 case BPF_LD
|BPF_H
|BPF_IND
:
327 if (X
> buflen
|| pc
->k
> buflen
- X
||
328 sizeof(int16_t) > buflen
- k
) {
331 A
= EXTRACT_SHORT(&p
[k
]);
334 case BPF_LD
|BPF_B
|BPF_IND
:
336 if (pc
->k
>= buflen
|| X
>= buflen
- pc
->k
) {
342 case BPF_LDX
|BPF_MSH
|BPF_B
:
347 X
= (p
[pc
->k
] & 0xf) << 2;
354 case BPF_LDX
|BPF_IMM
:
362 case BPF_LDX
|BPF_MEM
:
376 * XXX - we currently implement "ip6 protochain"
377 * with backward jumps, so sign-extend pc->k.
379 pc
+= (bpf_int32
)pc
->k
;
382 case BPF_JMP
|BPF_JGT
|BPF_K
:
383 pc
+= (A
> pc
->k
) ? pc
->jt
: pc
->jf
;
386 case BPF_JMP
|BPF_JGE
|BPF_K
:
387 pc
+= (A
>= pc
->k
) ? pc
->jt
: pc
->jf
;
390 case BPF_JMP
|BPF_JEQ
|BPF_K
:
391 pc
+= (A
== pc
->k
) ? pc
->jt
: pc
->jf
;
394 case BPF_JMP
|BPF_JSET
|BPF_K
:
395 pc
+= (A
& pc
->k
) ? pc
->jt
: pc
->jf
;
398 case BPF_JMP
|BPF_JGT
|BPF_X
:
399 pc
+= (A
> X
) ? pc
->jt
: pc
->jf
;
402 case BPF_JMP
|BPF_JGE
|BPF_X
:
403 pc
+= (A
>= X
) ? pc
->jt
: pc
->jf
;
406 case BPF_JMP
|BPF_JEQ
|BPF_X
:
407 pc
+= (A
== X
) ? pc
->jt
: pc
->jf
;
410 case BPF_JMP
|BPF_JSET
|BPF_X
:
411 pc
+= (A
& X
) ? pc
->jt
: pc
->jf
;
414 case BPF_ALU
|BPF_ADD
|BPF_X
:
418 case BPF_ALU
|BPF_SUB
|BPF_X
:
422 case BPF_ALU
|BPF_MUL
|BPF_X
:
426 case BPF_ALU
|BPF_DIV
|BPF_X
:
432 case BPF_ALU
|BPF_MOD
|BPF_X
:
438 case BPF_ALU
|BPF_AND
|BPF_X
:
442 case BPF_ALU
|BPF_OR
|BPF_X
:
446 case BPF_ALU
|BPF_XOR
|BPF_X
:
450 case BPF_ALU
|BPF_LSH
|BPF_X
:
454 case BPF_ALU
|BPF_RSH
|BPF_X
:
458 case BPF_ALU
|BPF_ADD
|BPF_K
:
462 case BPF_ALU
|BPF_SUB
|BPF_K
:
466 case BPF_ALU
|BPF_MUL
|BPF_K
:
470 case BPF_ALU
|BPF_DIV
|BPF_K
:
474 case BPF_ALU
|BPF_MOD
|BPF_K
:
478 case BPF_ALU
|BPF_AND
|BPF_K
:
482 case BPF_ALU
|BPF_OR
|BPF_K
:
486 case BPF_ALU
|BPF_XOR
|BPF_K
:
490 case BPF_ALU
|BPF_LSH
|BPF_K
:
494 case BPF_ALU
|BPF_RSH
|BPF_K
:
498 case BPF_ALU
|BPF_NEG
:
500 * Most BPF arithmetic is unsigned, but negation
501 * can't be unsigned; throw some casts to
502 * specify what we're trying to do.
504 A
= (u_int32_t
)(-(int32_t)A
);
507 case BPF_MISC
|BPF_TAX
:
511 case BPF_MISC
|BPF_TXA
:
519 bpf_filter(const struct bpf_insn
*pc
, const u_char
*p
, u_int wirelen
,
522 return bpf_filter_with_aux_data(pc
, p
, wirelen
, buflen
, NULL
);
527 * Return true if the 'fcode' is a valid filter program.
528 * The constraints are that each jump be forward and to a valid
529 * code, that memory accesses are within valid ranges (to the
530 * extent that this can be checked statically; loads of packet
531 * data have to be, and are, also checked at run time), and that
532 * the code terminates with either an accept or reject.
534 * The kernel needs to be able to verify an application's filter code.
535 * Otherwise, a bogus program could easily crash the system.
538 bpf_validate(const struct bpf_insn
*f
, int len
)
541 const struct bpf_insn
*p
;
546 for (i
= 0; i
< (u_int
)len
; ++i
) {
548 switch (BPF_CLASS(p
->code
)) {
550 * Check that memory operations use valid addresses.
554 switch (BPF_MODE(p
->code
)) {
561 * There's no maximum packet data size
562 * in userland. The runtime packet length
567 if (p
->k
>= BPF_MEMWORDS
)
578 if (p
->k
>= BPF_MEMWORDS
)
582 switch (BPF_OP(p
->code
)) {
596 * Check for constant division or modulus
599 if (BPF_SRC(p
->code
) == BPF_K
&& p
->k
== 0)
608 * Check that jumps are within the code block,
609 * and that unconditional branches don't go
610 * backwards as a result of an overflow.
611 * Unconditional branches have a 32-bit offset,
612 * so they could overflow; we check to make
613 * sure they don't. Conditional branches have
614 * an 8-bit offset, and the from address is <=
615 * BPF_MAXINSNS, and we assume that BPF_MAXINSNS
616 * is sufficiently small that adding 255 to it
619 * We know that len is <= BPF_MAXINSNS, and we
620 * assume that BPF_MAXINSNS is < the maximum size
621 * of a u_int, so that i + 1 doesn't overflow.
623 * For userland, we don't know that the from
624 * or len are <= BPF_MAXINSNS, but we know that
625 * from <= len, and, except on a 64-bit system,
626 * it's unlikely that len, if it truly reflects
627 * the size of the program we've been handed,
628 * will be anywhere near the maximum size of
629 * a u_int. We also don't check for backward
630 * branches, as we currently support them in
631 * userland for the protochain operation.
634 switch (BPF_OP(p
->code
)) {
636 if (from
+ p
->k
>= (u_int
)len
)
643 if (from
+ p
->jt
>= (u_int
)len
|| from
+ p
->jf
>= (u_int
)len
)
658 return BPF_CLASS(f
[len
- 1].code
) == BPF_RET
;