]>
The Tcpdump Group git mirrors - libpcap/blob - pcap/bpf.h
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. 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.h 7.1 (Berkeley) 5/7/91
38 * This is libpcap's cut-down version of bpf.h; it includes only
39 * the stuff needed for the code generator and the userland BPF
40 * interpreter, and the libpcap APIs for setting filters, etc..
42 * "pcap-bpf.c" will include the native OS version, as it deals with
43 * the OS's BPF implementation.
45 * At least two programs found by Google Code Search explicitly includes
46 * <pcap/bpf.h> (even though <pcap.h>/<pcap/pcap.h> includes it for you),
47 * so moving that stuff to <pcap/pcap.h> would break the build for some
52 * If we've already included <net/bpf.h>, don't re-define this stuff.
53 * We assume BSD-style multiple-include protection in <net/bpf.h>,
54 * which is true of all but the oldest versions of FreeBSD and NetBSD,
55 * or Tru64 UNIX-style multiple-include protection (or, at least,
56 * Tru64 UNIX 5.x-style; I don't have earlier versions available to check),
57 * or AIX-style multiple-include protection (or, at least, AIX 5.x-style;
58 * I don't have earlier versions available to check), or QNX-style
59 * multiple-include protection (as per GitHub pull request #394).
61 * We trust that they will define structures and macros and types in
62 * a fashion that's source-compatible and binary-compatible with our
65 * We do not check for BPF_MAJOR_VERSION, as that's defined by
66 * <linux/filter.h>, which is directly or indirectly included in some
67 * programs that also include pcap.h, and <linux/filter.h> doesn't
68 * define stuff we need. We *do* protect against <linux/filter.h>
69 * defining various macros for BPF code itself; <linux/filter.h> says
71 * Try and keep these values and structures similar to BSD, especially
72 * the BPF code definitions which need to match so you can share filters
74 * so we trust that it will define them in a fashion that's source-compatible
75 * and binary-compatible with our definitions.
77 * This also provides our own multiple-include protection.
79 #if !defined(_NET_BPF_H_) && !defined(_NET_BPF_H_INCLUDED) && !defined(_BPF_H_) && !defined(_H_BPF) && !defined(lib_pcap_bpf_h)
80 #define lib_pcap_bpf_h
82 #include <pcap/funcattrs.h>
89 /* BSD style release date */
90 #define BPF_RELEASE 199606
92 typedef int bpf_int32
;
93 typedef u_int bpf_u_int32
;
96 * Alignment macros. BPF_WORDALIGN rounds up to the next
97 * even multiple of BPF_ALIGNMENT.
99 * Tcpdump's print-pflog.c uses this, so we define it here.
102 #define BPF_ALIGNMENT sizeof(bpf_int32)
104 #define BPF_ALIGNMENT sizeof(long)
106 #define BPF_WORDALIGN(x) (((x)+(BPF_ALIGNMENT-1))&~(BPF_ALIGNMENT-1))
109 * Structure for "pcap_compile()", "pcap_setfilter()", etc..
113 struct bpf_insn
*bf_insns
;
117 * The instruction encodings.
119 * Please inform tcpdump-workers@lists.tcpdump.org if you use any
120 * of the reserved values, so that we can note that they're used
121 * (and perhaps implement it in the reference BPF implementation
122 * and encourage its implementation elsewhere).
126 * The upper 8 bits of the opcode aren't used. BSD/OS used 0x8000.
129 /* instruction classes */
130 #define BPF_CLASS(code) ((code) & 0x07)
138 #define BPF_MISC 0x07
141 #define BPF_SIZE(code) ((code) & 0x18)
145 /* 0x18 reserved; used by BSD/OS */
146 #define BPF_MODE(code) ((code) & 0xe0)
153 /* 0xc0 reserved; used by BSD/OS; also by OpenBSD for BPF_RND */
154 /* 0xe0 reserved; used by BSD/OS */
157 #define BPF_OP(code) ((code) & 0xf0)
179 #define BPF_JSET 0x40
180 /* 0x50 reserved; used on BSD/OS */
191 #define BPF_SRC(code) ((code) & 0x08)
195 /* ret - BPF_K and BPF_X also apply */
196 #define BPF_RVAL(code) ((code) & 0x18)
201 #define BPF_MISCOP(code) ((code) & 0xf8)
206 /* #define BPF_COP 0x20 NetBSD "coprocessor" extensions */
210 /* #define BPF_COPX 0x40 NetBSD "coprocessor" extensions */
211 /* also used on BSD/OS */
227 /* 0xc0 reserved; used on BSD/OS */
237 * The instruction data structure.
247 * Macros for insn array initializers.
249 * In case somebody's included <linux/filter.h>, or something else that
250 * gives the kernel's definitions of BPF statements, get rid of its
251 * definitions, so we can supply ours instead. If some kernel's
252 * definitions aren't *binary-compatible* with what BPF has had
253 * since it first sprung from the brows of Van Jacobson and Steve
254 * McCanne, that kernel should be fixed.
259 #define BPF_STMT(code, k) { (u_short)(code), 0, 0, k }
263 #define BPF_JUMP(code, k, jt, jf) { (u_short)(code), jt, jf, k }
266 PCAP_DEPRECATED("use pcap_offline_filter()")
267 PCAP_API u_int
bpf_filter(const struct bpf_insn
*, const u_char
*, u_int
, u_int
);
270 PCAP_API
int bpf_validate(const struct bpf_insn
*f
, int len
);
273 PCAP_API
char *bpf_image(const struct bpf_insn
*, int);
276 PCAP_API
void bpf_dump(const struct bpf_program
*, int);
279 * Number of scratch memory words (for BPF_LD|BPF_MEM and BPF_ST).
281 #define BPF_MEMWORDS 16
287 #endif /* !defined(_NET_BPF_H_) && !defined(_BPF_H_) && !defined(_H_BPF) && !defined(lib_pcap_bpf_h) */