xref: /netbsd-src/external/bsd/libpcap/dist/pcap/bpf.h (revision 449458b6eb1216099c07f8de50eafef7f9d9d6f0)
1 /*	$NetBSD: bpf.h,v 1.11 2023/08/17 15:18:13 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
5  *	The Regents of the University of California.  All rights reserved.
6  *
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.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
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.
23  *
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
34  * SUCH DAMAGE.
35  *
36  *      @(#)bpf.h       7.1 (Berkeley) 5/7/91
37  */
38 #error "This is not used in NetBSD, we use <net/bpf.h>"
39 /*
40  * This is libpcap's cut-down version of bpf.h; it includes only
41  * the stuff needed for the code generator and the userland BPF
42  * interpreter, and the libpcap APIs for setting filters, etc..
43  *
44  * "pcap-bpf.c" will include the native OS version, as it deals with
45  * the OS's BPF implementation.
46  *
47  * At least two programs found by Google Code Search explicitly includes
48  * <pcap/bpf.h> (even though <pcap.h>/<pcap/pcap.h> includes it for you),
49  * so moving that stuff to <pcap/pcap.h> would break the build for some
50  * programs.
51  */
52 
53 /*
54  * If we've already included <net/bpf.h>, don't re-define this stuff.
55  * We assume BSD-style multiple-include protection in <net/bpf.h>,
56  * which is true of all but the oldest versions of FreeBSD and NetBSD,
57  * or Tru64 UNIX-style multiple-include protection (or, at least,
58  * Tru64 UNIX 5.x-style; I don't have earlier versions available to check),
59  * or AIX-style multiple-include protection (or, at least, AIX 5.x-style;
60  * I don't have earlier versions available to check), or QNX-style
61  * multiple-include protection (as per GitHub pull request #394).
62  *
63  * We trust that they will define structures and macros and types in
64  * a fashion that's source-compatible and binary-compatible with our
65  * definitions.
66  *
67  * We do not check for BPF_MAJOR_VERSION, as that's defined by
68  * <linux/filter.h>, which is directly or indirectly included in some
69  * programs that also include pcap.h, and <linux/filter.h> doesn't
70  * define stuff we need.  We *do* protect against <linux/filter.h>
71  * defining various macros for BPF code itself; <linux/filter.h> says
72  *
73  *	Try and keep these values and structures similar to BSD, especially
74  *	the BPF code definitions which need to match so you can share filters
75  *
76  * so we trust that it will define them in a fashion that's source-compatible
77  * and binary-compatible with our definitions.
78  *
79  * This also provides our own multiple-include protection.
80  */
81 #if !defined(_NET_BPF_H_) && !defined(_NET_BPF_H_INCLUDED) && !defined(_BPF_H_) && !defined(_H_BPF) && !defined(lib_pcap_bpf_h)
82 #define lib_pcap_bpf_h
83 
84 #include <pcap/funcattrs.h>
85 #include <pcap/dlt.h>
86 
87 #ifdef __cplusplus
88 extern "C" {
89 #endif
90 
91 /* BSD style release date */
92 #define BPF_RELEASE 199606
93 
94 #ifdef MSDOS /* must be 32-bit */
95 typedef long          bpf_int32;
96 typedef unsigned long bpf_u_int32;
97 #else
98 typedef	int bpf_int32;
99 typedef	u_int bpf_u_int32;
100 #endif
101 
102 /*
103  * Alignment macros.  BPF_WORDALIGN rounds up to the next
104  * even multiple of BPF_ALIGNMENT.
105  *
106  * Tcpdump's print-pflog.c uses this, so we define it here.
107  */
108 #ifndef __NetBSD__
109 #define BPF_ALIGNMENT sizeof(bpf_int32)
110 #else
111 #define BPF_ALIGNMENT sizeof(long)
112 #endif
113 #define BPF_WORDALIGN(x) (((x)+(BPF_ALIGNMENT-1))&~(BPF_ALIGNMENT-1))
114 
115 /*
116  * Structure for "pcap_compile()", "pcap_setfilter()", etc..
117  */
118 struct bpf_program {
119 	u_int bf_len;
120 	struct bpf_insn *bf_insns;
121 };
122 
123 /*
124  * The instruction encodings.
125  *
126  * Please inform tcpdump-workers@lists.tcpdump.org if you use any
127  * of the reserved values, so that we can note that they're used
128  * (and perhaps implement it in the reference BPF implementation
129  * and encourage its implementation elsewhere).
130  */
131 
132 /*
133  * The upper 8 bits of the opcode aren't used. BSD/OS used 0x8000.
134  */
135 
136 /* instruction classes */
137 #define BPF_CLASS(code) ((code) & 0x07)
138 #define		BPF_LD		0x00
139 #define		BPF_LDX		0x01
140 #define		BPF_ST		0x02
141 #define		BPF_STX		0x03
142 #define		BPF_ALU		0x04
143 #define		BPF_JMP		0x05
144 #define		BPF_RET		0x06
145 #define		BPF_MISC	0x07
146 
147 /* ld/ldx fields */
148 #define BPF_SIZE(code)	((code) & 0x18)
149 #define		BPF_W		0x00
150 #define		BPF_H		0x08
151 #define		BPF_B		0x10
152 /*				0x18	reserved; used by BSD/OS */
153 #define BPF_MODE(code)	((code) & 0xe0)
154 #define		BPF_IMM	0x00
155 #define		BPF_ABS		0x20
156 #define		BPF_IND		0x40
157 #define		BPF_MEM		0x60
158 #define		BPF_LEN		0x80
159 #define		BPF_MSH		0xa0
160 /*				0xc0	reserved; used by BSD/OS */
161 /*				0xe0	reserved; used by BSD/OS */
162 
163 /* alu/jmp fields */
164 #define BPF_OP(code)	((code) & 0xf0)
165 #define		BPF_ADD		0x00
166 #define		BPF_SUB		0x10
167 #define		BPF_MUL		0x20
168 #define		BPF_DIV		0x30
169 #define		BPF_OR		0x40
170 #define		BPF_AND		0x50
171 #define		BPF_LSH		0x60
172 #define		BPF_RSH		0x70
173 #define		BPF_NEG		0x80
174 #define		BPF_MOD		0x90
175 #define		BPF_XOR		0xa0
176 /*				0xb0	reserved */
177 /*				0xc0	reserved */
178 /*				0xd0	reserved */
179 /*				0xe0	reserved */
180 /*				0xf0	reserved */
181 
182 #define		BPF_JA		0x00
183 #define		BPF_JEQ		0x10
184 #define		BPF_JGT		0x20
185 #define		BPF_JGE		0x30
186 #define		BPF_JSET	0x40
187 /*				0x50	reserved; used on BSD/OS */
188 /*				0x60	reserved */
189 /*				0x70	reserved */
190 /*				0x80	reserved */
191 /*				0x90	reserved */
192 /*				0xa0	reserved */
193 /*				0xb0	reserved */
194 /*				0xc0	reserved */
195 /*				0xd0	reserved */
196 /*				0xe0	reserved */
197 /*				0xf0	reserved */
198 #define BPF_SRC(code)	((code) & 0x08)
199 #define		BPF_K		0x00
200 #define		BPF_X		0x08
201 
202 /* ret - BPF_K and BPF_X also apply */
203 #define BPF_RVAL(code)	((code) & 0x18)
204 #define		BPF_A		0x10
205 /*				0x18	reserved */
206 
207 /* misc */
208 #define BPF_MISCOP(code) ((code) & 0xf8)
209 #define		BPF_TAX		0x00
210 /*				0x08	reserved */
211 /*				0x10	reserved */
212 /*				0x18	reserved */
213 /* #define	BPF_COP		0x20	NetBSD "coprocessor" extensions */
214 /*				0x28	reserved */
215 /*				0x30	reserved */
216 /*				0x38	reserved */
217 /* #define	BPF_COPX	0x40	NetBSD "coprocessor" extensions */
218 /*					also used on BSD/OS */
219 /*				0x48	reserved */
220 /*				0x50	reserved */
221 /*				0x58	reserved */
222 /*				0x60	reserved */
223 /*				0x68	reserved */
224 /*				0x70	reserved */
225 /*				0x78	reserved */
226 #define		BPF_TXA		0x80
227 /*				0x88	reserved */
228 /*				0x90	reserved */
229 /*				0x98	reserved */
230 /*				0xa0	reserved */
231 /*				0xa8	reserved */
232 /*				0xb0	reserved */
233 /*				0xb8	reserved */
234 /*				0xc0	reserved; used on BSD/OS */
235 /*				0xc8	reserved */
236 /*				0xd0	reserved */
237 /*				0xd8	reserved */
238 /*				0xe0	reserved */
239 /*				0xe8	reserved */
240 /*				0xf0	reserved */
241 /*				0xf8	reserved */
242 
243 /*
244  * The instruction data structure.
245  */
246 struct bpf_insn {
247 	u_short	code;
248 	u_char	jt;
249 	u_char	jf;
250 	bpf_u_int32 k;
251 };
252 
253 /*
254  * Macros for insn array initializers.
255  *
256  * In case somebody's included <linux/filter.h>, or something else that
257  * gives the kernel's definitions of BPF statements, get rid of its
258  * definitions, so we can supply ours instead.  If some kernel's
259  * definitions aren't *binary-compatible* with what BPF has had
260  * since it first sprung from the brows of Van Jacobson and Steve
261  * McCanne, that kernel should be fixed.
262  */
263 #ifdef BPF_STMT
264 #undef BPF_STMT
265 #endif
266 #define BPF_STMT(code, k) { (u_short)(code), 0, 0, k }
267 #ifdef BPF_JUMP
268 #undef BPF_JUMP
269 #endif
270 #define BPF_JUMP(code, k, jt, jf) { (u_short)(code), jt, jf, k }
271 
272 PCAP_AVAILABLE_0_4
273 PCAP_API u_int	bpf_filter(const struct bpf_insn *, const u_char *, u_int, u_int);
274 
275 PCAP_AVAILABLE_0_6
276 PCAP_API int	bpf_validate(const struct bpf_insn *f, int len);
277 
278 PCAP_AVAILABLE_0_4
279 PCAP_API char	*bpf_image(const struct bpf_insn *, int);
280 
281 PCAP_AVAILABLE_0_6
282 PCAP_API void	bpf_dump(const struct bpf_program *, int);
283 
284 /*
285  * Number of scratch memory words (for BPF_LD|BPF_MEM and BPF_ST).
286  */
287 #define BPF_MEMWORDS 16
288 
289 #ifdef __cplusplus
290 }
291 #endif
292 
293 #endif /* !defined(_NET_BPF_H_) && !defined(_BPF_H_) && !defined(_H_BPF) && !defined(lib_pcap_bpf_h) */
294