xref: /freebsd/sys/net/bpf.h (revision 664a31e4967a61ec61870f45adc2f1400617993e)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1990, 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * This code is derived from the Stanford/CMU enet packet filter,
6df8bae1dSRodney W. Grimes  * (net/enet.c) distributed as part of 4.3BSD, and code contributed
7df8bae1dSRodney W. Grimes  * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
8df8bae1dSRodney W. Grimes  * Berkeley Laboratory.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
19df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
20df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
21df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
22df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
23df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
24df8bae1dSRodney W. Grimes  *    without specific prior written permission.
25df8bae1dSRodney W. Grimes  *
26df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  *      @(#)bpf.h	8.1 (Berkeley) 6/10/93
3911a53ef3SPaul Traina  *	@(#)bpf.h	1.34 (LBL)     6/16/96
40df8bae1dSRodney W. Grimes  *
41c3aac50fSPeter Wemm  * $FreeBSD$
42df8bae1dSRodney W. Grimes  */
43df8bae1dSRodney W. Grimes 
44cea1da3bSPaul Richards #ifndef _NET_BPF_H_
45cea1da3bSPaul Richards #define _NET_BPF_H_
46cea1da3bSPaul Richards 
4711a53ef3SPaul Traina /* BSD style release date */
4811a53ef3SPaul Traina #define	BPF_RELEASE 199606
4911a53ef3SPaul Traina 
5011a53ef3SPaul Traina typedef	int32_t	  bpf_int32;
5111a53ef3SPaul Traina typedef	u_int32_t bpf_u_int32;
5211a53ef3SPaul Traina 
53df8bae1dSRodney W. Grimes /*
54df8bae1dSRodney W. Grimes  * Alignment macros.  BPF_WORDALIGN rounds up to the next
55df8bae1dSRodney W. Grimes  * even multiple of BPF_ALIGNMENT.
56df8bae1dSRodney W. Grimes  */
57ba136d4fSAlexander Langer #define BPF_ALIGNMENT sizeof(long)
58df8bae1dSRodney W. Grimes #define BPF_WORDALIGN(x) (((x)+(BPF_ALIGNMENT-1))&~(BPF_ALIGNMENT-1))
59df8bae1dSRodney W. Grimes 
60df8bae1dSRodney W. Grimes #define BPF_MAXINSNS 512
61df8bae1dSRodney W. Grimes #define BPF_MAXBUFSIZE 0x8000
62df8bae1dSRodney W. Grimes #define BPF_MINBUFSIZE 32
63df8bae1dSRodney W. Grimes 
64df8bae1dSRodney W. Grimes /*
65df8bae1dSRodney W. Grimes  *  Structure for BIOCSETF.
66df8bae1dSRodney W. Grimes  */
67df8bae1dSRodney W. Grimes struct bpf_program {
68df8bae1dSRodney W. Grimes 	u_int bf_len;
69df8bae1dSRodney W. Grimes 	struct bpf_insn *bf_insns;
70df8bae1dSRodney W. Grimes };
71df8bae1dSRodney W. Grimes 
72df8bae1dSRodney W. Grimes /*
73df8bae1dSRodney W. Grimes  * Struct returned by BIOCGSTATS.
74df8bae1dSRodney W. Grimes  */
75df8bae1dSRodney W. Grimes struct bpf_stat {
76df8bae1dSRodney W. Grimes 	u_int bs_recv;		/* number of packets received */
77df8bae1dSRodney W. Grimes 	u_int bs_drop;		/* number of packets dropped */
78df8bae1dSRodney W. Grimes };
79df8bae1dSRodney W. Grimes 
80df8bae1dSRodney W. Grimes /*
81df8bae1dSRodney W. Grimes  * Struct return by BIOCVERSION.  This represents the version number of
82df8bae1dSRodney W. Grimes  * the filter language described by the instruction encodings below.
83df8bae1dSRodney W. Grimes  * bpf understands a program iff kernel_major == filter_major &&
84df8bae1dSRodney W. Grimes  * kernel_minor >= filter_minor, that is, if the value returned by the
85df8bae1dSRodney W. Grimes  * running kernel has the same major number and a minor number equal
86df8bae1dSRodney W. Grimes  * equal to or less than the filter being downloaded.  Otherwise, the
87df8bae1dSRodney W. Grimes  * results are undefined, meaning an error may be returned or packets
88df8bae1dSRodney W. Grimes  * may be accepted haphazardly.
89df8bae1dSRodney W. Grimes  * It has nothing to do with the source code version.
90df8bae1dSRodney W. Grimes  */
91df8bae1dSRodney W. Grimes struct bpf_version {
92df8bae1dSRodney W. Grimes 	u_short bv_major;
93df8bae1dSRodney W. Grimes 	u_short bv_minor;
94df8bae1dSRodney W. Grimes };
9511a53ef3SPaul Traina /* Current version number of filter architecture. */
96df8bae1dSRodney W. Grimes #define BPF_MAJOR_VERSION 1
97df8bae1dSRodney W. Grimes #define BPF_MINOR_VERSION 1
98df8bae1dSRodney W. Grimes 
99df8bae1dSRodney W. Grimes #define	BIOCGBLEN	_IOR('B',102, u_int)
100df8bae1dSRodney W. Grimes #define	BIOCSBLEN	_IOWR('B',102, u_int)
101df8bae1dSRodney W. Grimes #define	BIOCSETF	_IOW('B',103, struct bpf_program)
102df8bae1dSRodney W. Grimes #define	BIOCFLUSH	_IO('B',104)
103df8bae1dSRodney W. Grimes #define BIOCPROMISC	_IO('B',105)
104df8bae1dSRodney W. Grimes #define	BIOCGDLT	_IOR('B',106, u_int)
105df8bae1dSRodney W. Grimes #define BIOCGETIF	_IOR('B',107, struct ifreq)
106df8bae1dSRodney W. Grimes #define BIOCSETIF	_IOW('B',108, struct ifreq)
107df8bae1dSRodney W. Grimes #define BIOCSRTIMEOUT	_IOW('B',109, struct timeval)
108df8bae1dSRodney W. Grimes #define BIOCGRTIMEOUT	_IOR('B',110, struct timeval)
109df8bae1dSRodney W. Grimes #define BIOCGSTATS	_IOR('B',111, struct bpf_stat)
110df8bae1dSRodney W. Grimes #define BIOCIMMEDIATE	_IOW('B',112, u_int)
111df8bae1dSRodney W. Grimes #define BIOCVERSION	_IOR('B',113, struct bpf_version)
11200a83887SPaul Traina #define BIOCGRSIG	_IOR('B',114, u_int)
11300a83887SPaul Traina #define BIOCSRSIG	_IOW('B',115, u_int)
114114ae644SMike Smith #define BIOCGHDRCMPLT	_IOR('B',116, u_int)
115114ae644SMike Smith #define BIOCSHDRCMPLT	_IOW('B',117, u_int)
116df8bae1dSRodney W. Grimes 
117df8bae1dSRodney W. Grimes /*
118df8bae1dSRodney W. Grimes  * Structure prepended to each packet.
119df8bae1dSRodney W. Grimes  */
120df8bae1dSRodney W. Grimes struct bpf_hdr {
121df8bae1dSRodney W. Grimes 	struct timeval	bh_tstamp;	/* time stamp */
12211a53ef3SPaul Traina 	bpf_u_int32	bh_caplen;	/* length of captured portion */
12311a53ef3SPaul Traina 	bpf_u_int32	bh_datalen;	/* original length of packet */
124df8bae1dSRodney W. Grimes 	u_short		bh_hdrlen;	/* length of bpf header (this struct
125df8bae1dSRodney W. Grimes 					   plus alignment padding) */
126df8bae1dSRodney W. Grimes };
127df8bae1dSRodney W. Grimes /*
128df8bae1dSRodney W. Grimes  * Because the structure above is not a multiple of 4 bytes, some compilers
129df8bae1dSRodney W. Grimes  * will insist on inserting padding; hence, sizeof(struct bpf_hdr) won't work.
130df8bae1dSRodney W. Grimes  * Only the kernel needs to know about it; applications use bh_hdrlen.
131df8bae1dSRodney W. Grimes  */
132664a31e4SPeter Wemm #ifdef _KERNEL
133c086febeSBruce Evans #define	SIZEOF_BPF_HDR	(sizeof(struct bpf_hdr) <= 20 ? 18 : \
134c086febeSBruce Evans     sizeof(struct bpf_hdr))
135df8bae1dSRodney W. Grimes #endif
136df8bae1dSRodney W. Grimes 
137df8bae1dSRodney W. Grimes /*
138df8bae1dSRodney W. Grimes  * Data-link level type codes.
139df8bae1dSRodney W. Grimes  */
140df8bae1dSRodney W. Grimes #define DLT_NULL	0	/* no link-layer encapsulation */
141df8bae1dSRodney W. Grimes #define DLT_EN10MB	1	/* Ethernet (10Mb) */
142df8bae1dSRodney W. Grimes #define DLT_EN3MB	2	/* Experimental Ethernet (3Mb) */
143df8bae1dSRodney W. Grimes #define DLT_AX25	3	/* Amateur Radio AX.25 */
144df8bae1dSRodney W. Grimes #define DLT_PRONET	4	/* Proteon ProNET Token Ring */
145df8bae1dSRodney W. Grimes #define DLT_CHAOS	5	/* Chaos */
146df8bae1dSRodney W. Grimes #define DLT_IEEE802	6	/* IEEE 802 Networks */
147df8bae1dSRodney W. Grimes #define DLT_ARCNET	7	/* ARCNET */
148df8bae1dSRodney W. Grimes #define DLT_SLIP	8	/* Serial Line IP */
149df8bae1dSRodney W. Grimes #define DLT_PPP		9	/* Point-to-point Protocol */
150df8bae1dSRodney W. Grimes #define DLT_FDDI	10	/* FDDI */
15111a53ef3SPaul Traina #define DLT_ATM_RFC1483	11	/* LLC/SNAP encapsulated atm */
15222f05c43SAndrey A. Chernov #define DLT_RAW		12	/* raw IP */
153c2b0c424SBill Fenner #define DLT_SLIP_BSDOS	13	/* BSD/OS Serial Line IP */
154c2b0c424SBill Fenner #define DLT_PPP_BSDOS	14	/* BSD/OS Point-to-point Protocol */
155df8bae1dSRodney W. Grimes 
156df8bae1dSRodney W. Grimes /*
1576c5e9bbdSMike Pritchard  * The instruction encodings.
158df8bae1dSRodney W. Grimes  */
159df8bae1dSRodney W. Grimes /* instruction classes */
160df8bae1dSRodney W. Grimes #define BPF_CLASS(code) ((code) & 0x07)
161df8bae1dSRodney W. Grimes #define		BPF_LD		0x00
162df8bae1dSRodney W. Grimes #define		BPF_LDX		0x01
163df8bae1dSRodney W. Grimes #define		BPF_ST		0x02
164df8bae1dSRodney W. Grimes #define		BPF_STX		0x03
165df8bae1dSRodney W. Grimes #define		BPF_ALU		0x04
166df8bae1dSRodney W. Grimes #define		BPF_JMP		0x05
167df8bae1dSRodney W. Grimes #define		BPF_RET		0x06
168df8bae1dSRodney W. Grimes #define		BPF_MISC	0x07
169df8bae1dSRodney W. Grimes 
170df8bae1dSRodney W. Grimes /* ld/ldx fields */
171df8bae1dSRodney W. Grimes #define BPF_SIZE(code)	((code) & 0x18)
172df8bae1dSRodney W. Grimes #define		BPF_W		0x00
173df8bae1dSRodney W. Grimes #define		BPF_H		0x08
174df8bae1dSRodney W. Grimes #define		BPF_B		0x10
175df8bae1dSRodney W. Grimes #define BPF_MODE(code)	((code) & 0xe0)
176df8bae1dSRodney W. Grimes #define		BPF_IMM 	0x00
177df8bae1dSRodney W. Grimes #define		BPF_ABS		0x20
178df8bae1dSRodney W. Grimes #define		BPF_IND		0x40
179df8bae1dSRodney W. Grimes #define		BPF_MEM		0x60
180df8bae1dSRodney W. Grimes #define		BPF_LEN		0x80
181df8bae1dSRodney W. Grimes #define		BPF_MSH		0xa0
182df8bae1dSRodney W. Grimes 
183df8bae1dSRodney W. Grimes /* alu/jmp fields */
184df8bae1dSRodney W. Grimes #define BPF_OP(code)	((code) & 0xf0)
185df8bae1dSRodney W. Grimes #define		BPF_ADD		0x00
186df8bae1dSRodney W. Grimes #define		BPF_SUB		0x10
187df8bae1dSRodney W. Grimes #define		BPF_MUL		0x20
188df8bae1dSRodney W. Grimes #define		BPF_DIV		0x30
189df8bae1dSRodney W. Grimes #define		BPF_OR		0x40
190df8bae1dSRodney W. Grimes #define		BPF_AND		0x50
191df8bae1dSRodney W. Grimes #define		BPF_LSH		0x60
192df8bae1dSRodney W. Grimes #define		BPF_RSH		0x70
193df8bae1dSRodney W. Grimes #define		BPF_NEG		0x80
194df8bae1dSRodney W. Grimes #define		BPF_JA		0x00
195df8bae1dSRodney W. Grimes #define		BPF_JEQ		0x10
196df8bae1dSRodney W. Grimes #define		BPF_JGT		0x20
197df8bae1dSRodney W. Grimes #define		BPF_JGE		0x30
198df8bae1dSRodney W. Grimes #define		BPF_JSET	0x40
199df8bae1dSRodney W. Grimes #define BPF_SRC(code)	((code) & 0x08)
200df8bae1dSRodney W. Grimes #define		BPF_K		0x00
201df8bae1dSRodney W. Grimes #define		BPF_X		0x08
202df8bae1dSRodney W. Grimes 
203df8bae1dSRodney W. Grimes /* ret - BPF_K and BPF_X also apply */
204df8bae1dSRodney W. Grimes #define BPF_RVAL(code)	((code) & 0x18)
205df8bae1dSRodney W. Grimes #define		BPF_A		0x10
206df8bae1dSRodney W. Grimes 
207df8bae1dSRodney W. Grimes /* misc */
208df8bae1dSRodney W. Grimes #define BPF_MISCOP(code) ((code) & 0xf8)
209df8bae1dSRodney W. Grimes #define		BPF_TAX		0x00
210df8bae1dSRodney W. Grimes #define		BPF_TXA		0x80
211df8bae1dSRodney W. Grimes 
212df8bae1dSRodney W. Grimes /*
213df8bae1dSRodney W. Grimes  * The instruction data structure.
214df8bae1dSRodney W. Grimes  */
215df8bae1dSRodney W. Grimes struct bpf_insn {
216df8bae1dSRodney W. Grimes 	u_short		code;
217df8bae1dSRodney W. Grimes 	u_char		jt;
218df8bae1dSRodney W. Grimes 	u_char		jf;
21911a53ef3SPaul Traina 	bpf_u_int32	k;
220df8bae1dSRodney W. Grimes };
221df8bae1dSRodney W. Grimes 
222df8bae1dSRodney W. Grimes /*
223df8bae1dSRodney W. Grimes  * Macros for insn array initializers.
224df8bae1dSRodney W. Grimes  */
225df8bae1dSRodney W. Grimes #define BPF_STMT(code, k) { (u_short)(code), 0, 0, k }
226df8bae1dSRodney W. Grimes #define BPF_JUMP(code, k, jt, jf) { (u_short)(code), jt, jf, k }
227df8bae1dSRodney W. Grimes 
228664a31e4SPeter Wemm #ifdef _KERNEL
229dcb129d5SArchie Cobbs int	 bpf_validate __P((const struct bpf_insn *, int));
2309b44ff22SGarrett Wollman void	 bpf_tap __P((struct ifnet *, u_char *, u_int));
2319b44ff22SGarrett Wollman void	 bpf_mtap __P((struct ifnet *, struct mbuf *));
2329b44ff22SGarrett Wollman void	 bpfattach __P((struct ifnet *, u_int, u_int));
233df8bae1dSRodney W. Grimes void	 bpfilterattach __P((int));
234dcb129d5SArchie Cobbs u_int	 bpf_filter __P((const struct bpf_insn *, u_char *, u_int, u_int));
235df8bae1dSRodney W. Grimes #endif
236df8bae1dSRodney W. Grimes 
237df8bae1dSRodney W. Grimes /*
238df8bae1dSRodney W. Grimes  * Number of scratch memory words (for BPF_LD|BPF_MEM and BPF_ST).
239df8bae1dSRodney W. Grimes  */
240df8bae1dSRodney W. Grimes #define BPF_MEMWORDS 16
241df8bae1dSRodney W. Grimes 
242cea1da3bSPaul Richards #endif
243