xref: /freebsd/sys/net/bpf.h (revision c88f6908b40ce1bc0450db71f7fde141951d4c44)
1c398230bSWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1990, 1991, 1993
5253a3814SLawrence Stewart  *	The Regents of the University of California.  All rights reserved.
6df8bae1dSRodney W. Grimes  *
7df8bae1dSRodney W. Grimes  * This code is derived from the Stanford/CMU enet packet filter,
8df8bae1dSRodney W. Grimes  * (net/enet.c) distributed as part of 4.3BSD, and code contributed
9df8bae1dSRodney W. Grimes  * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
10df8bae1dSRodney W. Grimes  * Berkeley Laboratory.
11df8bae1dSRodney W. Grimes  *
12df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
13df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
14df8bae1dSRodney W. Grimes  * are met:
15df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
17df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
18df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
19df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
20fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
21df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
22df8bae1dSRodney W. Grimes  *    without specific prior written permission.
23df8bae1dSRodney W. Grimes  *
24df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
35df8bae1dSRodney W. Grimes  *
36df8bae1dSRodney W. Grimes  *      @(#)bpf.h	8.1 (Berkeley) 6/10/93
3711a53ef3SPaul Traina  *	@(#)bpf.h	1.34 (LBL)     6/16/96
38df8bae1dSRodney W. Grimes  *
39c3aac50fSPeter Wemm  * $FreeBSD$
40df8bae1dSRodney W. Grimes  */
41df8bae1dSRodney W. Grimes 
42cea1da3bSPaul Richards #ifndef _NET_BPF_H_
43cea1da3bSPaul Richards #define _NET_BPF_H_
44cea1da3bSPaul Richards 
45e2e050c8SConrad Meyer #include <sys/_eventhandler.h>
46699281b5SAndrey V. Elsukov #include <sys/ck.h>
47699281b5SAndrey V. Elsukov #include <net/dlt.h>
48699281b5SAndrey V. Elsukov 
4911a53ef3SPaul Traina /* BSD style release date */
5011a53ef3SPaul Traina #define	BPF_RELEASE 199606
5111a53ef3SPaul Traina 
5211a53ef3SPaul Traina typedef	int32_t	  bpf_int32;
5311a53ef3SPaul Traina typedef	u_int32_t bpf_u_int32;
54547d94bdSJung-uk Kim typedef	int64_t	  bpf_int64;
55547d94bdSJung-uk Kim typedef	u_int64_t bpf_u_int64;
5611a53ef3SPaul Traina 
57df8bae1dSRodney W. Grimes /*
58*c88f6908SMark Johnston  * Alignment macros.  BPF_WORDALIGN rounds up to the next multiple of
59*c88f6908SMark Johnston  * BPF_ALIGNMENT.
60df8bae1dSRodney W. Grimes  */
61ba136d4fSAlexander Langer #define BPF_ALIGNMENT sizeof(long)
62df8bae1dSRodney W. Grimes #define BPF_WORDALIGN(x) (((x)+(BPF_ALIGNMENT-1))&~(BPF_ALIGNMENT-1))
63df8bae1dSRodney W. Grimes 
64df8bae1dSRodney W. Grimes #define BPF_MAXINSNS 512
65eba2a1aeSPoul-Henning Kamp #define BPF_MAXBUFSIZE 0x80000
66df8bae1dSRodney W. Grimes #define BPF_MINBUFSIZE 32
67df8bae1dSRodney W. Grimes 
68df8bae1dSRodney W. Grimes /*
69df8bae1dSRodney W. Grimes  *  Structure for BIOCSETF.
70df8bae1dSRodney W. Grimes  */
71df8bae1dSRodney W. Grimes struct bpf_program {
72df8bae1dSRodney W. Grimes 	u_int bf_len;
73df8bae1dSRodney W. Grimes 	struct bpf_insn *bf_insns;
74df8bae1dSRodney W. Grimes };
75df8bae1dSRodney W. Grimes 
76df8bae1dSRodney W. Grimes /*
77df8bae1dSRodney W. Grimes  * Struct returned by BIOCGSTATS.
78df8bae1dSRodney W. Grimes  */
79df8bae1dSRodney W. Grimes struct bpf_stat {
80df8bae1dSRodney W. Grimes 	u_int bs_recv;		/* number of packets received */
81df8bae1dSRodney W. Grimes 	u_int bs_drop;		/* number of packets dropped */
82df8bae1dSRodney W. Grimes };
83df8bae1dSRodney W. Grimes 
84df8bae1dSRodney W. Grimes /*
85df8bae1dSRodney W. Grimes  * Struct return by BIOCVERSION.  This represents the version number of
86df8bae1dSRodney W. Grimes  * the filter language described by the instruction encodings below.
87df8bae1dSRodney W. Grimes  * bpf understands a program iff kernel_major == filter_major &&
88df8bae1dSRodney W. Grimes  * kernel_minor >= filter_minor, that is, if the value returned by the
89df8bae1dSRodney W. Grimes  * running kernel has the same major number and a minor number equal
90df8bae1dSRodney W. Grimes  * equal to or less than the filter being downloaded.  Otherwise, the
91df8bae1dSRodney W. Grimes  * results are undefined, meaning an error may be returned or packets
92df8bae1dSRodney W. Grimes  * may be accepted haphazardly.
93df8bae1dSRodney W. Grimes  * It has nothing to do with the source code version.
94df8bae1dSRodney W. Grimes  */
95df8bae1dSRodney W. Grimes struct bpf_version {
96df8bae1dSRodney W. Grimes 	u_short bv_major;
97df8bae1dSRodney W. Grimes 	u_short bv_minor;
98df8bae1dSRodney W. Grimes };
9911a53ef3SPaul Traina /* Current version number of filter architecture. */
100df8bae1dSRodney W. Grimes #define BPF_MAJOR_VERSION 1
101df8bae1dSRodney W. Grimes #define BPF_MINOR_VERSION 1
102df8bae1dSRodney W. Grimes 
1034d621040SChristian S.J. Peron /*
1044d621040SChristian S.J. Peron  * Historically, BPF has supported a single buffering model, first using mbuf
1054d621040SChristian S.J. Peron  * clusters in kernel, and later using malloc(9) buffers in kernel.  We now
1064d621040SChristian S.J. Peron  * support multiple buffering modes, which may be queried and set using
1074d621040SChristian S.J. Peron  * BIOCGETBUFMODE and BIOCSETBUFMODE.  So as to avoid handling the complexity
1084d621040SChristian S.J. Peron  * of changing modes while sniffing packets, the mode becomes fixed once an
1094d621040SChristian S.J. Peron  * interface has been attached to the BPF descriptor.
1104d621040SChristian S.J. Peron  */
1114d621040SChristian S.J. Peron #define	BPF_BUFMODE_BUFFER	1	/* Kernel buffers with read(). */
1124d621040SChristian S.J. Peron #define	BPF_BUFMODE_ZBUF	2	/* Zero-copy buffers. */
1134d621040SChristian S.J. Peron 
1144d621040SChristian S.J. Peron /*-
1154d621040SChristian S.J. Peron  * Struct used by BIOCSETZBUF, BIOCROTZBUF: describes up to two zero-copy
1164d621040SChristian S.J. Peron  * buffer as used by BPF.
1174d621040SChristian S.J. Peron  */
1184d621040SChristian S.J. Peron struct bpf_zbuf {
1194d621040SChristian S.J. Peron 	void	*bz_bufa;	/* Location of 'a' zero-copy buffer. */
1204d621040SChristian S.J. Peron 	void	*bz_bufb;	/* Location of 'b' zero-copy buffer. */
1214d621040SChristian S.J. Peron 	size_t	 bz_buflen;	/* Size of zero-copy buffers. */
1224d621040SChristian S.J. Peron };
1234d621040SChristian S.J. Peron 
124df8bae1dSRodney W. Grimes #define	BIOCGBLEN	_IOR('B', 102, u_int)
125df8bae1dSRodney W. Grimes #define	BIOCSBLEN	_IOWR('B', 102, u_int)
126df8bae1dSRodney W. Grimes #define	BIOCSETF	_IOW('B', 103, struct bpf_program)
127df8bae1dSRodney W. Grimes #define	BIOCFLUSH	_IO('B', 104)
128df8bae1dSRodney W. Grimes #define	BIOCPROMISC	_IO('B', 105)
129df8bae1dSRodney W. Grimes #define	BIOCGDLT	_IOR('B', 106, u_int)
130df8bae1dSRodney W. Grimes #define	BIOCGETIF	_IOR('B', 107, struct ifreq)
131df8bae1dSRodney W. Grimes #define	BIOCSETIF	_IOW('B', 108, struct ifreq)
132df8bae1dSRodney W. Grimes #define	BIOCSRTIMEOUT	_IOW('B', 109, struct timeval)
133df8bae1dSRodney W. Grimes #define	BIOCGRTIMEOUT	_IOR('B', 110, struct timeval)
134df8bae1dSRodney W. Grimes #define	BIOCGSTATS	_IOR('B', 111, struct bpf_stat)
135df8bae1dSRodney W. Grimes #define	BIOCIMMEDIATE	_IOW('B', 112, u_int)
136df8bae1dSRodney W. Grimes #define	BIOCVERSION	_IOR('B', 113, struct bpf_version)
13700a83887SPaul Traina #define	BIOCGRSIG	_IOR('B', 114, u_int)
13800a83887SPaul Traina #define	BIOCSRSIG	_IOW('B', 115, u_int)
139114ae644SMike Smith #define	BIOCGHDRCMPLT	_IOR('B', 116, u_int)
140114ae644SMike Smith #define	BIOCSHDRCMPLT	_IOW('B', 117, u_int)
141560a54e1SJung-uk Kim #define	BIOCGDIRECTION	_IOR('B', 118, u_int)
142560a54e1SJung-uk Kim #define	BIOCSDIRECTION	_IOW('B', 119, u_int)
1438eab61f3SSam Leffler #define	BIOCSDLT	_IOW('B', 120, u_int)
1448eab61f3SSam Leffler #define	BIOCGDLTLIST	_IOWR('B', 121, struct bpf_dltlist)
14593e39f0bSChristian S.J. Peron #define	BIOCLOCK	_IO('B', 122)
14693e39f0bSChristian S.J. Peron #define	BIOCSETWF	_IOW('B', 123, struct bpf_program)
147560a54e1SJung-uk Kim #define	BIOCFEEDBACK	_IOW('B', 124, u_int)
1484d621040SChristian S.J. Peron #define	BIOCGETBUFMODE	_IOR('B', 125, u_int)
1494d621040SChristian S.J. Peron #define	BIOCSETBUFMODE	_IOW('B', 126, u_int)
1504d621040SChristian S.J. Peron #define	BIOCGETZMAX	_IOR('B', 127, size_t)
1514d621040SChristian S.J. Peron #define	BIOCROTZBUF	_IOR('B', 128, struct bpf_zbuf)
1524d621040SChristian S.J. Peron #define	BIOCSETZBUF	_IOW('B', 129, struct bpf_zbuf)
153f11c3508SDavid Malone #define	BIOCSETFNR	_IOW('B', 130, struct bpf_program)
154547d94bdSJung-uk Kim #define	BIOCGTSTAMP	_IOR('B', 131, u_int)
155547d94bdSJung-uk Kim #define	BIOCSTSTAMP	_IOW('B', 132, u_int)
1561e7fe2fbSLuiz Otavio O Souza #define	BIOCSETVLANPCP	_IOW('B', 133, u_int)
157560a54e1SJung-uk Kim 
158560a54e1SJung-uk Kim /* Obsolete */
159560a54e1SJung-uk Kim #define	BIOCGSEESENT	BIOCGDIRECTION
160560a54e1SJung-uk Kim #define	BIOCSSEESENT	BIOCSDIRECTION
161560a54e1SJung-uk Kim 
162560a54e1SJung-uk Kim /* Packet directions */
163560a54e1SJung-uk Kim enum bpf_direction {
164560a54e1SJung-uk Kim 	BPF_D_IN,	/* See incoming packets */
165560a54e1SJung-uk Kim 	BPF_D_INOUT,	/* See incoming and outgoing packets */
166560a54e1SJung-uk Kim 	BPF_D_OUT	/* See outgoing packets */
167560a54e1SJung-uk Kim };
168df8bae1dSRodney W. Grimes 
169547d94bdSJung-uk Kim /* Time stamping functions */
170547d94bdSJung-uk Kim #define	BPF_T_MICROTIME		0x0000
171547d94bdSJung-uk Kim #define	BPF_T_NANOTIME		0x0001
172547d94bdSJung-uk Kim #define	BPF_T_BINTIME		0x0002
173547d94bdSJung-uk Kim #define	BPF_T_NONE		0x0003
174547d94bdSJung-uk Kim #define	BPF_T_FORMAT_MASK	0x0003
175547d94bdSJung-uk Kim #define	BPF_T_NORMAL		0x0000
176253a3814SLawrence Stewart #define	BPF_T_FAST		0x0100
177253a3814SLawrence Stewart #define	BPF_T_MONOTONIC		0x0200
178253a3814SLawrence Stewart #define	BPF_T_MONOTONIC_FAST	(BPF_T_FAST | BPF_T_MONOTONIC)
179253a3814SLawrence Stewart #define	BPF_T_FLAG_MASK		0x0300
180547d94bdSJung-uk Kim #define	BPF_T_FORMAT(t)		((t) & BPF_T_FORMAT_MASK)
181547d94bdSJung-uk Kim #define	BPF_T_FLAG(t)		((t) & BPF_T_FLAG_MASK)
182547d94bdSJung-uk Kim #define	BPF_T_VALID(t)						\
183547d94bdSJung-uk Kim     ((t) == BPF_T_NONE || (BPF_T_FORMAT(t) != BPF_T_NONE &&	\
184547d94bdSJung-uk Kim     ((t) & ~(BPF_T_FORMAT_MASK | BPF_T_FLAG_MASK)) == 0))
185547d94bdSJung-uk Kim 
186253a3814SLawrence Stewart #define	BPF_T_MICROTIME_FAST		(BPF_T_MICROTIME | BPF_T_FAST)
187253a3814SLawrence Stewart #define	BPF_T_NANOTIME_FAST		(BPF_T_NANOTIME | BPF_T_FAST)
188253a3814SLawrence Stewart #define	BPF_T_BINTIME_FAST		(BPF_T_BINTIME | BPF_T_FAST)
189547d94bdSJung-uk Kim #define	BPF_T_MICROTIME_MONOTONIC	(BPF_T_MICROTIME | BPF_T_MONOTONIC)
190547d94bdSJung-uk Kim #define	BPF_T_NANOTIME_MONOTONIC	(BPF_T_NANOTIME | BPF_T_MONOTONIC)
191547d94bdSJung-uk Kim #define	BPF_T_BINTIME_MONOTONIC		(BPF_T_BINTIME | BPF_T_MONOTONIC)
192253a3814SLawrence Stewart #define	BPF_T_MICROTIME_MONOTONIC_FAST	(BPF_T_MICROTIME | BPF_T_MONOTONIC_FAST)
193253a3814SLawrence Stewart #define	BPF_T_NANOTIME_MONOTONIC_FAST	(BPF_T_NANOTIME | BPF_T_MONOTONIC_FAST)
194253a3814SLawrence Stewart #define	BPF_T_BINTIME_MONOTONIC_FAST	(BPF_T_BINTIME | BPF_T_MONOTONIC_FAST)
195547d94bdSJung-uk Kim 
196df8bae1dSRodney W. Grimes /*
197df8bae1dSRodney W. Grimes  * Structure prepended to each packet.
198df8bae1dSRodney W. Grimes  */
199547d94bdSJung-uk Kim struct bpf_ts {
200547d94bdSJung-uk Kim 	bpf_int64	bt_sec;		/* seconds */
201547d94bdSJung-uk Kim 	bpf_u_int64	bt_frac;	/* fraction */
202547d94bdSJung-uk Kim };
203547d94bdSJung-uk Kim struct bpf_xhdr {
204547d94bdSJung-uk Kim 	struct bpf_ts	bh_tstamp;	/* time stamp */
205547d94bdSJung-uk Kim 	bpf_u_int32	bh_caplen;	/* length of captured portion */
206547d94bdSJung-uk Kim 	bpf_u_int32	bh_datalen;	/* original length of packet */
207547d94bdSJung-uk Kim 	u_short		bh_hdrlen;	/* length of bpf header (this struct
208547d94bdSJung-uk Kim 					   plus alignment padding) */
209547d94bdSJung-uk Kim };
210547d94bdSJung-uk Kim /* Obsolete */
211df8bae1dSRodney W. Grimes struct bpf_hdr {
212df8bae1dSRodney W. Grimes 	struct timeval	bh_tstamp;	/* time stamp */
21311a53ef3SPaul Traina 	bpf_u_int32	bh_caplen;	/* length of captured portion */
21411a53ef3SPaul Traina 	bpf_u_int32	bh_datalen;	/* original length of packet */
215df8bae1dSRodney W. Grimes 	u_short		bh_hdrlen;	/* length of bpf header (this struct
216df8bae1dSRodney W. Grimes 					   plus alignment padding) */
217df8bae1dSRodney W. Grimes };
218664a31e4SPeter Wemm #ifdef _KERNEL
219547d94bdSJung-uk Kim #define	MTAG_BPF		0x627066
220547d94bdSJung-uk Kim #define	MTAG_BPF_TIMESTAMP	0
221df8bae1dSRodney W. Grimes #endif
222df8bae1dSRodney W. Grimes 
223df8bae1dSRodney W. Grimes /*
2244d621040SChristian S.J. Peron  * When using zero-copy BPF buffers, a shared memory header is present
2254d621040SChristian S.J. Peron  * allowing the kernel BPF implementation and user process to synchronize
2264d621040SChristian S.J. Peron  * without using system calls.  This structure defines that header.  When
2274d621040SChristian S.J. Peron  * accessing these fields, appropriate atomic operation and memory barriers
2284d621040SChristian S.J. Peron  * are required in order not to see stale or out-of-order data; see bpf(4)
2294d621040SChristian S.J. Peron  * for reference code to access these fields from userspace.
2304d621040SChristian S.J. Peron  *
2314d621040SChristian S.J. Peron  * The layout of this structure is critical, and must not be changed; if must
2324d621040SChristian S.J. Peron  * fit in a single page on all architectures.
2334d621040SChristian S.J. Peron  */
2344d621040SChristian S.J. Peron struct bpf_zbuf_header {
2354d621040SChristian S.J. Peron 	volatile u_int	bzh_kernel_gen;	/* Kernel generation number. */
2364d621040SChristian S.J. Peron 	volatile u_int	bzh_kernel_len;	/* Length of data in the buffer. */
2374d621040SChristian S.J. Peron 	volatile u_int	bzh_user_gen;	/* User generation number. */
2384d621040SChristian S.J. Peron 	u_int _bzh_pad[5];
2394d621040SChristian S.J. Peron };
2404d621040SChristian S.J. Peron 
241dcf377edSRui Paulo /*
2426c5e9bbdSMike Pritchard  * The instruction encodings.
243681ed54cSXin LI  *
244681ed54cSXin LI  * Please inform tcpdump-workers@lists.tcpdump.org if you use any
245681ed54cSXin LI  * of the reserved values, so that we can note that they're used
246681ed54cSXin LI  * (and perhaps implement it in the reference BPF implementation
247681ed54cSXin LI  * and encourage its implementation elsewhere).
248df8bae1dSRodney W. Grimes  */
249681ed54cSXin LI 
250681ed54cSXin LI /*
251681ed54cSXin LI  * The upper 8 bits of the opcode aren't used. BSD/OS used 0x8000.
252681ed54cSXin LI  */
253681ed54cSXin LI 
254df8bae1dSRodney W. Grimes /* instruction classes */
255df8bae1dSRodney W. Grimes #define BPF_CLASS(code) ((code) & 0x07)
256df8bae1dSRodney W. Grimes #define		BPF_LD		0x00
257df8bae1dSRodney W. Grimes #define		BPF_LDX		0x01
258df8bae1dSRodney W. Grimes #define		BPF_ST		0x02
259df8bae1dSRodney W. Grimes #define		BPF_STX		0x03
260df8bae1dSRodney W. Grimes #define		BPF_ALU		0x04
261df8bae1dSRodney W. Grimes #define		BPF_JMP		0x05
262df8bae1dSRodney W. Grimes #define		BPF_RET		0x06
263df8bae1dSRodney W. Grimes #define		BPF_MISC	0x07
264df8bae1dSRodney W. Grimes 
265df8bae1dSRodney W. Grimes /* ld/ldx fields */
266df8bae1dSRodney W. Grimes #define BPF_SIZE(code)	((code) & 0x18)
267df8bae1dSRodney W. Grimes #define		BPF_W		0x00
268df8bae1dSRodney W. Grimes #define		BPF_H		0x08
269df8bae1dSRodney W. Grimes #define		BPF_B		0x10
270681ed54cSXin LI /*				0x18	reserved; used by BSD/OS */
271df8bae1dSRodney W. Grimes #define BPF_MODE(code)	((code) & 0xe0)
272df8bae1dSRodney W. Grimes #define		BPF_IMM 	0x00
273df8bae1dSRodney W. Grimes #define		BPF_ABS		0x20
274df8bae1dSRodney W. Grimes #define		BPF_IND		0x40
275df8bae1dSRodney W. Grimes #define		BPF_MEM		0x60
276df8bae1dSRodney W. Grimes #define		BPF_LEN		0x80
277df8bae1dSRodney W. Grimes #define		BPF_MSH		0xa0
278681ed54cSXin LI /*				0xc0	reserved; used by BSD/OS */
279681ed54cSXin LI /*				0xe0	reserved; used by BSD/OS */
280df8bae1dSRodney W. Grimes 
281df8bae1dSRodney W. Grimes /* alu/jmp fields */
282df8bae1dSRodney W. Grimes #define BPF_OP(code)	((code) & 0xf0)
283df8bae1dSRodney W. Grimes #define		BPF_ADD		0x00
284df8bae1dSRodney W. Grimes #define		BPF_SUB		0x10
285df8bae1dSRodney W. Grimes #define		BPF_MUL		0x20
286df8bae1dSRodney W. Grimes #define		BPF_DIV		0x30
287df8bae1dSRodney W. Grimes #define		BPF_OR		0x40
288df8bae1dSRodney W. Grimes #define		BPF_AND		0x50
289df8bae1dSRodney W. Grimes #define		BPF_LSH		0x60
290df8bae1dSRodney W. Grimes #define		BPF_RSH		0x70
291df8bae1dSRodney W. Grimes #define		BPF_NEG		0x80
292681ed54cSXin LI #define		BPF_MOD		0x90
293681ed54cSXin LI #define		BPF_XOR		0xa0
294681ed54cSXin LI /*				0xb0	reserved */
295681ed54cSXin LI /*				0xc0	reserved */
296681ed54cSXin LI /*				0xd0	reserved */
297681ed54cSXin LI /*				0xe0	reserved */
298681ed54cSXin LI /*				0xf0	reserved */
299681ed54cSXin LI 
300df8bae1dSRodney W. Grimes #define		BPF_JA		0x00
301df8bae1dSRodney W. Grimes #define		BPF_JEQ		0x10
302df8bae1dSRodney W. Grimes #define		BPF_JGT		0x20
303df8bae1dSRodney W. Grimes #define		BPF_JGE		0x30
304df8bae1dSRodney W. Grimes #define		BPF_JSET	0x40
305681ed54cSXin LI /*				0x50	reserved; used on BSD/OS */
306681ed54cSXin LI /*				0x60	reserved */
307681ed54cSXin LI /*				0x70	reserved */
308681ed54cSXin LI /*				0x80	reserved */
309681ed54cSXin LI /*				0x90	reserved */
310681ed54cSXin LI /*				0xa0	reserved */
311681ed54cSXin LI /*				0xb0	reserved */
312681ed54cSXin LI /*				0xc0	reserved */
313681ed54cSXin LI /*				0xd0	reserved */
314681ed54cSXin LI /*				0xe0	reserved */
315681ed54cSXin LI /*				0xf0	reserved */
316df8bae1dSRodney W. Grimes #define BPF_SRC(code)	((code) & 0x08)
317df8bae1dSRodney W. Grimes #define		BPF_K		0x00
318df8bae1dSRodney W. Grimes #define		BPF_X		0x08
319df8bae1dSRodney W. Grimes 
320df8bae1dSRodney W. Grimes /* ret - BPF_K and BPF_X also apply */
321df8bae1dSRodney W. Grimes #define BPF_RVAL(code)	((code) & 0x18)
322df8bae1dSRodney W. Grimes #define		BPF_A		0x10
323681ed54cSXin LI /*				0x18	reserved */
324df8bae1dSRodney W. Grimes 
325df8bae1dSRodney W. Grimes /* misc */
326df8bae1dSRodney W. Grimes #define BPF_MISCOP(code) ((code) & 0xf8)
327df8bae1dSRodney W. Grimes #define		BPF_TAX		0x00
328681ed54cSXin LI /*				0x08	reserved */
329681ed54cSXin LI /*				0x10	reserved */
330681ed54cSXin LI /*				0x18	reserved */
331681ed54cSXin LI /* #define	BPF_COP		0x20	NetBSD "coprocessor" extensions */
332681ed54cSXin LI /*				0x28	reserved */
333681ed54cSXin LI /*				0x30	reserved */
334681ed54cSXin LI /*				0x38	reserved */
335681ed54cSXin LI /* #define	BPF_COPX	0x40	NetBSD "coprocessor" extensions */
336681ed54cSXin LI /*					also used on BSD/OS */
337681ed54cSXin LI /*				0x48	reserved */
338681ed54cSXin LI /*				0x50	reserved */
339681ed54cSXin LI /*				0x58	reserved */
340681ed54cSXin LI /*				0x60	reserved */
341681ed54cSXin LI /*				0x68	reserved */
342681ed54cSXin LI /*				0x70	reserved */
343681ed54cSXin LI /*				0x78	reserved */
344df8bae1dSRodney W. Grimes #define		BPF_TXA		0x80
345681ed54cSXin LI /*				0x88	reserved */
346681ed54cSXin LI /*				0x90	reserved */
347681ed54cSXin LI /*				0x98	reserved */
348681ed54cSXin LI /*				0xa0	reserved */
349681ed54cSXin LI /*				0xa8	reserved */
350681ed54cSXin LI /*				0xb0	reserved */
351681ed54cSXin LI /*				0xb8	reserved */
352681ed54cSXin LI /*				0xc0	reserved; used on BSD/OS */
353681ed54cSXin LI /*				0xc8	reserved */
354681ed54cSXin LI /*				0xd0	reserved */
355681ed54cSXin LI /*				0xd8	reserved */
356681ed54cSXin LI /*				0xe0	reserved */
357681ed54cSXin LI /*				0xe8	reserved */
358681ed54cSXin LI /*				0xf0	reserved */
359681ed54cSXin LI /*				0xf8	reserved */
360df8bae1dSRodney W. Grimes 
361df8bae1dSRodney W. Grimes /*
362df8bae1dSRodney W. Grimes  * The instruction data structure.
363df8bae1dSRodney W. Grimes  */
364df8bae1dSRodney W. Grimes struct bpf_insn {
365df8bae1dSRodney W. Grimes 	u_short		code;
366df8bae1dSRodney W. Grimes 	u_char		jt;
367df8bae1dSRodney W. Grimes 	u_char		jf;
36811a53ef3SPaul Traina 	bpf_u_int32	k;
369df8bae1dSRodney W. Grimes };
370df8bae1dSRodney W. Grimes 
371df8bae1dSRodney W. Grimes /*
372df8bae1dSRodney W. Grimes  * Macros for insn array initializers.
373df8bae1dSRodney W. Grimes  */
374df8bae1dSRodney W. Grimes #define BPF_STMT(code, k) { (u_short)(code), 0, 0, k }
375df8bae1dSRodney W. Grimes #define BPF_JUMP(code, k, jt, jf) { (u_short)(code), jt, jf, k }
376df8bae1dSRodney W. Grimes 
3778eab61f3SSam Leffler /*
3788eab61f3SSam Leffler  * Structure to retrieve available DLTs for the interface.
3798eab61f3SSam Leffler  */
3808eab61f3SSam Leffler struct bpf_dltlist {
3818eab61f3SSam Leffler 	u_int	bfl_len;	/* number of bfd_list array */
3828eab61f3SSam Leffler 	u_int	*bfl_list;	/* array of DLTs */
3838eab61f3SSam Leffler };
3848eab61f3SSam Leffler 
385664a31e4SPeter Wemm #ifdef _KERNEL
3864d621040SChristian S.J. Peron #ifdef MALLOC_DECLARE
3874d621040SChristian S.J. Peron MALLOC_DECLARE(M_BPF);
3884d621040SChristian S.J. Peron #endif
3894d621040SChristian S.J. Peron #ifdef SYSCTL_DECL
3904d621040SChristian S.J. Peron SYSCTL_DECL(_net_bpf);
3914d621040SChristian S.J. Peron #endif
3924d621040SChristian S.J. Peron 
3934d621040SChristian S.J. Peron /*
3944d621040SChristian S.J. Peron  * Rotate the packet buffers in descriptor d.  Move the store buffer into the
395f87e372eSLuiz Otavio O Souza  * hold slot, and the free buffer into the store slot.  Zero the length of the
396f87e372eSLuiz Otavio O Souza  * new store buffer.  Descriptor lock should be held.  One must be careful to
397f87e372eSLuiz Otavio O Souza  * not rotate the buffers twice, i.e. if fbuf != NULL.
3984d621040SChristian S.J. Peron  */
3994d621040SChristian S.J. Peron #define	ROTATE_BUFFERS(d)	do {					\
4004d621040SChristian S.J. Peron 	(d)->bd_hbuf = (d)->bd_sbuf;					\
4014d621040SChristian S.J. Peron 	(d)->bd_hlen = (d)->bd_slen;					\
4024d621040SChristian S.J. Peron 	(d)->bd_sbuf = (d)->bd_fbuf;					\
4034d621040SChristian S.J. Peron 	(d)->bd_slen = 0;						\
4044d621040SChristian S.J. Peron 	(d)->bd_fbuf = NULL;						\
4054d621040SChristian S.J. Peron 	bpf_bufheld(d);							\
4064d621040SChristian S.J. Peron } while (0)
4074d621040SChristian S.J. Peron 
40816d878ccSChristian S.J. Peron /*
40916d878ccSChristian S.J. Peron  * Descriptor associated with each attached hardware interface.
410b23cbbe6SMark Johnston  * Part of this structure is exposed to external callers to speed up
411b23cbbe6SMark Johnston  * bpf_peers_present() calls.
41216d878ccSChristian S.J. Peron  */
413b23cbbe6SMark Johnston struct bpf_if;
414699281b5SAndrey V. Elsukov CK_LIST_HEAD(bpfd_list, bpf_d);
415b23cbbe6SMark Johnston 
416b23cbbe6SMark Johnston struct bpf_if_ext {
417699281b5SAndrey V. Elsukov 	CK_LIST_ENTRY(bpf_if)	bif_next;	/* list of all interfaces */
418699281b5SAndrey V. Elsukov 	struct bpfd_list	bif_dlist;	/* descriptor list */
41916d878ccSChristian S.J. Peron };
42016d878ccSChristian S.J. Peron 
4214d621040SChristian S.J. Peron void	 bpf_bufheld(struct bpf_d *d);
422929ddbbbSAlfred Perlstein int	 bpf_validate(const struct bpf_insn *, int);
42324a229f4SSam Leffler void	 bpf_tap(struct bpf_if *, u_char *, u_int);
42424a229f4SSam Leffler void	 bpf_mtap(struct bpf_if *, struct mbuf *);
425437ffe18SSam Leffler void	 bpf_mtap2(struct bpf_if *, void *, u_int, struct mbuf *);
426929ddbbbSAlfred Perlstein void	 bpfattach(struct ifnet *, u_int, u_int);
42724a229f4SSam Leffler void	 bpfattach2(struct ifnet *, u_int, u_int, struct bpf_if **);
428929ddbbbSAlfred Perlstein void	 bpfdetach(struct ifnet *);
42905fc4164SBjoern A. Zeeb #ifdef VIMAGE
43005fc4164SBjoern A. Zeeb int	 bpf_get_bp_params(struct bpf_if *, u_int *, u_int *);
43105fc4164SBjoern A. Zeeb #endif
432de5d9935SRobert Watson 
433929ddbbbSAlfred Perlstein void	 bpfilterattach(int);
43418b6f055SJung-uk Kim u_int	 bpf_filter(const struct bpf_insn *, u_char *, u_int, u_int);
43524a229f4SSam Leffler 
43616d878ccSChristian S.J. Peron static __inline int
43716d878ccSChristian S.J. Peron bpf_peers_present(struct bpf_if *bpf)
43816d878ccSChristian S.J. Peron {
439b23cbbe6SMark Johnston 	struct bpf_if_ext *ext;
44016d878ccSChristian S.J. Peron 
441b23cbbe6SMark Johnston 	ext = (struct bpf_if_ext *)bpf;
442699281b5SAndrey V. Elsukov 	if (!CK_LIST_EMPTY(&ext->bif_dlist))
443ffdc0471SChristian S.J. Peron 		return (1);
4445255290cSChristian S.J. Peron 	return (0);
44516d878ccSChristian S.J. Peron }
44616d878ccSChristian S.J. Peron 
44724a229f4SSam Leffler #define	BPF_TAP(_ifp,_pkt,_pktlen) do {				\
44816d878ccSChristian S.J. Peron 	if (bpf_peers_present((_ifp)->if_bpf))			\
44924a229f4SSam Leffler 		bpf_tap((_ifp)->if_bpf, (_pkt), (_pktlen));	\
45024a229f4SSam Leffler } while (0)
45124a229f4SSam Leffler #define	BPF_MTAP(_ifp,_m) do {					\
45216d878ccSChristian S.J. Peron 	if (bpf_peers_present((_ifp)->if_bpf)) {		\
453437ffe18SSam Leffler 		M_ASSERTVALID(_m);				\
45424a229f4SSam Leffler 		bpf_mtap((_ifp)->if_bpf, (_m));			\
455437ffe18SSam Leffler 	}							\
456437ffe18SSam Leffler } while (0)
457437ffe18SSam Leffler #define	BPF_MTAP2(_ifp,_data,_dlen,_m) do {			\
45816d878ccSChristian S.J. Peron 	if (bpf_peers_present((_ifp)->if_bpf)) {		\
459437ffe18SSam Leffler 		M_ASSERTVALID(_m);				\
460437ffe18SSam Leffler 		bpf_mtap2((_ifp)->if_bpf,(_data),(_dlen),(_m));	\
461437ffe18SSam Leffler 	}							\
46224a229f4SSam Leffler } while (0)
463df8bae1dSRodney W. Grimes #endif
464df8bae1dSRodney W. Grimes 
465df8bae1dSRodney W. Grimes /*
466df8bae1dSRodney W. Grimes  * Number of scratch memory words (for BPF_LD|BPF_MEM and BPF_ST).
467df8bae1dSRodney W. Grimes  */
468df8bae1dSRodney W. Grimes #define BPF_MEMWORDS 16
469df8bae1dSRodney W. Grimes 
4707ced9c2fSGleb Smirnoff /* BPF attach/detach events */
4717ced9c2fSGleb Smirnoff struct ifnet;
4727ced9c2fSGleb Smirnoff typedef void (*bpf_track_fn)(void *, struct ifnet *, int /* dlt */,
4737ced9c2fSGleb Smirnoff     int /* 1 =>'s attach */);
4747ced9c2fSGleb Smirnoff EVENTHANDLER_DECLARE(bpf_track, bpf_track_fn);
4757ced9c2fSGleb Smirnoff 
47624a229f4SSam Leffler #endif /* _NET_BPF_H_ */
477