bpf.h revision 37619
1303980Sngie/*
2303980Sngie * Copyright (c) 1990, 1991, 1993
3303980Sngie *	The Regents of the University of California.  All rights reserved.
4303980Sngie *
5303980Sngie * This code is derived from the Stanford/CMU enet packet filter,
6303980Sngie * (net/enet.c) distributed as part of 4.3BSD, and code contributed
7303980Sngie * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
8303980Sngie * Berkeley Laboratory.
9303980Sngie *
10303980Sngie * Redistribution and use in source and binary forms, with or without
11303980Sngie * modification, are permitted provided that the following conditions
12303980Sngie * are met:
13303980Sngie * 1. Redistributions of source code must retain the above copyright
14303980Sngie *    notice, this list of conditions and the following disclaimer.
15303980Sngie * 2. Redistributions in binary form must reproduce the above copyright
16303980Sngie *    notice, this list of conditions and the following disclaimer in the
17303980Sngie *    documentation and/or other materials provided with the distribution.
18303980Sngie * 3. All advertising materials mentioning features or use of this software
19303980Sngie *    must display the following acknowledgement:
20303980Sngie *	This product includes software developed by the University of
21303980Sngie *	California, Berkeley and its contributors.
22303980Sngie * 4. Neither the name of the University nor the names of its contributors
23303980Sngie *    may be used to endorse or promote products derived from this software
24303980Sngie *    without specific prior written permission.
25303980Sngie *
26303980Sngie * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27303980Sngie * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28303980Sngie * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29303980Sngie * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30303980Sngie * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31303980Sngie * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32303980Sngie * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33303980Sngie * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34303980Sngie * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35303980Sngie * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36303980Sngie * SUCH DAMAGE.
37303980Sngie *
38303980Sngie *      @(#)bpf.h	8.1 (Berkeley) 6/10/93
39303980Sngie *	@(#)bpf.h	1.34 (LBL)     6/16/96
40303980Sngie *
41303980Sngie * $Id: bpf.h,v 1.12 1997/02/22 09:40:55 peter Exp $
42303980Sngie */
43303980Sngie
44303980Sngie#ifndef _NET_BPF_H_
45303980Sngie#define _NET_BPF_H_
46303980Sngie
47303980Sngie/* BSD style release date */
48303980Sngie#define	BPF_RELEASE 199606
49303980Sngie
50303980Sngietypedef	int32_t	  bpf_int32;
51303980Sngietypedef	u_int32_t bpf_u_int32;
52303980Sngie
53303980Sngie/*
54303980Sngie * Alignment macros.  BPF_WORDALIGN rounds up to the next
55303980Sngie * even multiple of BPF_ALIGNMENT.
56303980Sngie */
57303980Sngie#define BPF_ALIGNMENT sizeof(bpf_int32)
58303980Sngie#define BPF_WORDALIGN(x) (((x)+(BPF_ALIGNMENT-1))&~(BPF_ALIGNMENT-1))
59303980Sngie
60303980Sngie#define BPF_MAXINSNS 512
61303980Sngie#define BPF_MAXBUFSIZE 0x8000
62303980Sngie#define BPF_MINBUFSIZE 32
63303980Sngie
64303980Sngie/*
65303980Sngie *  Structure for BIOCSETF.
66303980Sngie */
67303980Sngiestruct bpf_program {
68303980Sngie	u_int bf_len;
69303980Sngie	struct bpf_insn *bf_insns;
70303980Sngie};
71303980Sngie
72303980Sngie/*
73303980Sngie * Struct returned by BIOCGSTATS.
74303980Sngie */
75303980Sngiestruct bpf_stat {
76303980Sngie	u_int bs_recv;		/* number of packets received */
77303980Sngie	u_int bs_drop;		/* number of packets dropped */
78303980Sngie};
79303980Sngie
80303980Sngie/*
81303980Sngie * Struct return by BIOCVERSION.  This represents the version number of
82303980Sngie * the filter language described by the instruction encodings below.
83303980Sngie * bpf understands a program iff kernel_major == filter_major &&
84303980Sngie * kernel_minor >= filter_minor, that is, if the value returned by the
85303980Sngie * running kernel has the same major number and a minor number equal
86303980Sngie * equal to or less than the filter being downloaded.  Otherwise, the
87303980Sngie * results are undefined, meaning an error may be returned or packets
88303980Sngie * may be accepted haphazardly.
89303980Sngie * It has nothing to do with the source code version.
90303980Sngie */
91303980Sngiestruct bpf_version {
92303980Sngie	u_short bv_major;
93303980Sngie	u_short bv_minor;
94303980Sngie};
95303980Sngie/* Current version number of filter architecture. */
96303980Sngie#define BPF_MAJOR_VERSION 1
97303980Sngie#define BPF_MINOR_VERSION 1
98303980Sngie
99303980Sngie#define	BIOCGBLEN	_IOR('B',102, u_int)
100303980Sngie#define	BIOCSBLEN	_IOWR('B',102, u_int)
101303980Sngie#define	BIOCSETF	_IOW('B',103, struct bpf_program)
102303980Sngie#define	BIOCFLUSH	_IO('B',104)
103303980Sngie#define BIOCPROMISC	_IO('B',105)
104303980Sngie#define	BIOCGDLT	_IOR('B',106, u_int)
105303980Sngie#define BIOCGETIF	_IOR('B',107, struct ifreq)
106303980Sngie#define BIOCSETIF	_IOW('B',108, struct ifreq)
107303980Sngie#define BIOCSRTIMEOUT	_IOW('B',109, struct timeval)
108303980Sngie#define BIOCGRTIMEOUT	_IOR('B',110, struct timeval)
109303980Sngie#define BIOCGSTATS	_IOR('B',111, struct bpf_stat)
110303980Sngie#define BIOCIMMEDIATE	_IOW('B',112, u_int)
111303980Sngie#define BIOCVERSION	_IOR('B',113, struct bpf_version)
112303980Sngie#define BIOCGRSIG	_IOR('B',114, u_int)
113303980Sngie#define BIOCSRSIG	_IOW('B',115, u_int)
114303980Sngie
115303980Sngie/*
116303980Sngie * Structure prepended to each packet.
117303980Sngie */
118303980Sngiestruct bpf_hdr {
119303980Sngie	struct timeval	bh_tstamp;	/* time stamp */
120303980Sngie	bpf_u_int32	bh_caplen;	/* length of captured portion */
121303980Sngie	bpf_u_int32	bh_datalen;	/* original length of packet */
122303980Sngie	u_short		bh_hdrlen;	/* length of bpf header (this struct
123303980Sngie					   plus alignment padding) */
124303980Sngie};
125303980Sngie/*
126303980Sngie * Because the structure above is not a multiple of 4 bytes, some compilers
127303980Sngie * will insist on inserting padding; hence, sizeof(struct bpf_hdr) won't work.
128303980Sngie * Only the kernel needs to know about it; applications use bh_hdrlen.
129303980Sngie */
130303980Sngie#ifdef KERNEL
131303980Sngie#define	SIZEOF_BPF_HDR	(sizeof(struct bpf_hdr) <= 20 ? 18 : \
132303980Sngie    sizeof(struct bpf_hdr))
133303980Sngie#endif
134303980Sngie
135303980Sngie/*
136303980Sngie * Data-link level type codes.
137303980Sngie */
138303980Sngie#define DLT_NULL	0	/* no link-layer encapsulation */
139303980Sngie#define DLT_EN10MB	1	/* Ethernet (10Mb) */
140303980Sngie#define DLT_EN3MB	2	/* Experimental Ethernet (3Mb) */
141303980Sngie#define DLT_AX25	3	/* Amateur Radio AX.25 */
142303980Sngie#define DLT_PRONET	4	/* Proteon ProNET Token Ring */
143303980Sngie#define DLT_CHAOS	5	/* Chaos */
144303980Sngie#define DLT_IEEE802	6	/* IEEE 802 Networks */
145303980Sngie#define DLT_ARCNET	7	/* ARCNET */
146303980Sngie#define DLT_SLIP	8	/* Serial Line IP */
147303980Sngie#define DLT_PPP		9	/* Point-to-point Protocol */
148303980Sngie#define DLT_FDDI	10	/* FDDI */
149303980Sngie#define DLT_ATM_RFC1483	11	/* LLC/SNAP encapsulated atm */
150303980Sngie
151303980Sngie/*
152303980Sngie * The instruction encodings.
153303980Sngie */
154303980Sngie/* instruction classes */
155303980Sngie#define BPF_CLASS(code) ((code) & 0x07)
156303980Sngie#define		BPF_LD		0x00
157303980Sngie#define		BPF_LDX		0x01
158303980Sngie#define		BPF_ST		0x02
159303980Sngie#define		BPF_STX		0x03
160303980Sngie#define		BPF_ALU		0x04
161303980Sngie#define		BPF_JMP		0x05
162303980Sngie#define		BPF_RET		0x06
163303980Sngie#define		BPF_MISC	0x07
164303980Sngie
165303980Sngie/* ld/ldx fields */
166303980Sngie#define BPF_SIZE(code)	((code) & 0x18)
167303980Sngie#define		BPF_W		0x00
168303980Sngie#define		BPF_H		0x08
169303980Sngie#define		BPF_B		0x10
170303980Sngie#define BPF_MODE(code)	((code) & 0xe0)
171303980Sngie#define		BPF_IMM 	0x00
172303980Sngie#define		BPF_ABS		0x20
173303980Sngie#define		BPF_IND		0x40
174303980Sngie#define		BPF_MEM		0x60
175303980Sngie#define		BPF_LEN		0x80
176303980Sngie#define		BPF_MSH		0xa0
177303980Sngie
178303980Sngie/* alu/jmp fields */
179303980Sngie#define BPF_OP(code)	((code) & 0xf0)
180303980Sngie#define		BPF_ADD		0x00
181303980Sngie#define		BPF_SUB		0x10
182#define		BPF_MUL		0x20
183#define		BPF_DIV		0x30
184#define		BPF_OR		0x40
185#define		BPF_AND		0x50
186#define		BPF_LSH		0x60
187#define		BPF_RSH		0x70
188#define		BPF_NEG		0x80
189#define		BPF_JA		0x00
190#define		BPF_JEQ		0x10
191#define		BPF_JGT		0x20
192#define		BPF_JGE		0x30
193#define		BPF_JSET	0x40
194#define BPF_SRC(code)	((code) & 0x08)
195#define		BPF_K		0x00
196#define		BPF_X		0x08
197
198/* ret - BPF_K and BPF_X also apply */
199#define BPF_RVAL(code)	((code) & 0x18)
200#define		BPF_A		0x10
201
202/* misc */
203#define BPF_MISCOP(code) ((code) & 0xf8)
204#define		BPF_TAX		0x00
205#define		BPF_TXA		0x80
206
207/*
208 * The instruction data structure.
209 */
210struct bpf_insn {
211	u_short		code;
212	u_char		jt;
213	u_char		jf;
214	bpf_u_int32	k;
215};
216
217/*
218 * Macros for insn array initializers.
219 */
220#define BPF_STMT(code, k) { (u_short)(code), 0, 0, k }
221#define BPF_JUMP(code, k, jt, jf) { (u_short)(code), jt, jf, k }
222
223#ifdef KERNEL
224int	 bpf_validate __P((struct bpf_insn *, int));
225void	 bpf_tap __P((struct ifnet *, u_char *, u_int));
226void	 bpf_mtap __P((struct ifnet *, struct mbuf *));
227void	 bpfattach __P((struct ifnet *, u_int, u_int));
228void	 bpfilterattach __P((int));
229u_int	 bpf_filter __P((struct bpf_insn *, u_char *, u_int, u_int));
230#endif
231
232/*
233 * Number of scratch memory words (for BPF_LD|BPF_MEM and BPF_ST).
234 */
235#define BPF_MEMWORDS 16
236
237#endif
238