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