bpf.h revision 37619
1210311Sjmallett/*
2210311Sjmallett * Copyright (c) 1990, 1991, 1993
3210311Sjmallett *	The Regents of the University of California.  All rights reserved.
4210311Sjmallett *
5210311Sjmallett * This code is derived from the Stanford/CMU enet packet filter,
6210311Sjmallett * (net/enet.c) distributed as part of 4.3BSD, and code contributed
7210311Sjmallett * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
8210311Sjmallett * Berkeley Laboratory.
9210311Sjmallett *
10210311Sjmallett * Redistribution and use in source and binary forms, with or without
11210311Sjmallett * modification, are permitted provided that the following conditions
12210311Sjmallett * are met:
13210311Sjmallett * 1. Redistributions of source code must retain the above copyright
14210311Sjmallett *    notice, this list of conditions and the following disclaimer.
15210311Sjmallett * 2. Redistributions in binary form must reproduce the above copyright
16210311Sjmallett *    notice, this list of conditions and the following disclaimer in the
17210311Sjmallett *    documentation and/or other materials provided with the distribution.
18210311Sjmallett * 3. All advertising materials mentioning features or use of this software
19210311Sjmallett *    must display the following acknowledgement:
20210311Sjmallett *	This product includes software developed by the University of
21210311Sjmallett *	California, Berkeley and its contributors.
22210311Sjmallett * 4. Neither the name of the University nor the names of its contributors
23210311Sjmallett *    may be used to endorse or promote products derived from this software
24210311Sjmallett *    without specific prior written permission.
25210311Sjmallett *
26210311Sjmallett * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27210311Sjmallett * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28210311Sjmallett * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29210311Sjmallett * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30210311Sjmallett * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31210311Sjmallett * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32210311Sjmallett * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33210311Sjmallett * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34210311Sjmallett * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35210311Sjmallett * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36210311Sjmallett * SUCH DAMAGE.
37210311Sjmallett *
38210311Sjmallett *      @(#)bpf.h	8.1 (Berkeley) 6/10/93
39210311Sjmallett *	@(#)bpf.h	1.34 (LBL)     6/16/96
40210311Sjmallett *
41210311Sjmallett * $Id: bpf.h,v 1.12 1997/02/22 09:40:55 peter Exp $
42210311Sjmallett */
43210311Sjmallett
44210311Sjmallett#ifndef _NET_BPF_H_
45210311Sjmallett#define _NET_BPF_H_
46210311Sjmallett
47232289Sgonzo/* BSD style release date */
48232289Sgonzo#define	BPF_RELEASE 199606
49210311Sjmallett
50210311Sjmalletttypedef	int32_t	  bpf_int32;
51210311Sjmalletttypedef	u_int32_t bpf_u_int32;
52210311Sjmallett
53210311Sjmallett/*
54210311Sjmallett * Alignment macros.  BPF_WORDALIGN rounds up to the next
55210311Sjmallett * even multiple of BPF_ALIGNMENT.
56210311Sjmallett */
57210311Sjmallett#define BPF_ALIGNMENT sizeof(bpf_int32)
58210311Sjmallett#define BPF_WORDALIGN(x) (((x)+(BPF_ALIGNMENT-1))&~(BPF_ALIGNMENT-1))
59210311Sjmallett
60210311Sjmallett#define BPF_MAXINSNS 512
61210311Sjmallett#define BPF_MAXBUFSIZE 0x8000
62210311Sjmallett#define BPF_MINBUFSIZE 32
63210311Sjmallett
64210311Sjmallett/*
65210311Sjmallett *  Structure for BIOCSETF.
66210311Sjmallett */
67210311Sjmallettstruct bpf_program {
68210311Sjmallett	u_int bf_len;
69210311Sjmallett	struct bpf_insn *bf_insns;
70210311Sjmallett};
71210311Sjmallett
72210311Sjmallett/*
73210311Sjmallett * Struct returned by BIOCGSTATS.
74210311Sjmallett */
75210311Sjmallettstruct bpf_stat {
76210311Sjmallett	u_int bs_recv;		/* number of packets received */
77210311Sjmallett	u_int bs_drop;		/* number of packets dropped */
78210311Sjmallett};
79210311Sjmallett
80210311Sjmallett/*
81210311Sjmallett * Struct return by BIOCVERSION.  This represents the version number of
82210311Sjmallett * the filter language described by the instruction encodings below.
83210311Sjmallett * bpf understands a program iff kernel_major == filter_major &&
84210311Sjmallett * kernel_minor >= filter_minor, that is, if the value returned by the
85210311Sjmallett * running kernel has the same major number and a minor number equal
86210311Sjmallett * equal to or less than the filter being downloaded.  Otherwise, the
87210311Sjmallett * results are undefined, meaning an error may be returned or packets
88210311Sjmallett * may be accepted haphazardly.
89210311Sjmallett * It has nothing to do with the source code version.
90210311Sjmallett */
91210311Sjmallettstruct bpf_version {
92210311Sjmallett	u_short bv_major;
93231987Sgonzo	u_short bv_minor;
94231987Sgonzo};
95231987Sgonzo/* Current version number of filter architecture. */
96231987Sgonzo#define BPF_MAJOR_VERSION 1
97231987Sgonzo#define BPF_MINOR_VERSION 1
98231987Sgonzo
99231987Sgonzo#define	BIOCGBLEN	_IOR('B',102, u_int)
100231987Sgonzo#define	BIOCSBLEN	_IOWR('B',102, u_int)
101231987Sgonzo#define	BIOCSETF	_IOW('B',103, struct bpf_program)
102231987Sgonzo#define	BIOCFLUSH	_IO('B',104)
103232289Sgonzo#define BIOCPROMISC	_IO('B',105)
104232289Sgonzo#define	BIOCGDLT	_IOR('B',106, u_int)
105232289Sgonzo#define BIOCGETIF	_IOR('B',107, struct ifreq)
106232289Sgonzo#define BIOCSETIF	_IOW('B',108, struct ifreq)
107232289Sgonzo#define BIOCSRTIMEOUT	_IOW('B',109, struct timeval)
108231987Sgonzo#define BIOCGRTIMEOUT	_IOR('B',110, struct timeval)
109232289Sgonzo#define BIOCGSTATS	_IOR('B',111, struct bpf_stat)
110250192Simp#define BIOCIMMEDIATE	_IOW('B',112, u_int)
111232289Sgonzo#define BIOCVERSION	_IOR('B',113, struct bpf_version)
112231987Sgonzo#define BIOCGRSIG	_IOR('B',114, u_int)
113231987Sgonzo#define BIOCSRSIG	_IOW('B',115, u_int)
114232289Sgonzo
115231987Sgonzo/*
116231987Sgonzo * Structure prepended to each packet.
117231987Sgonzo */
118232289Sgonzostruct bpf_hdr {
119231987Sgonzo	struct timeval	bh_tstamp;	/* time stamp */
120232289Sgonzo	bpf_u_int32	bh_caplen;	/* length of captured portion */
121231987Sgonzo	bpf_u_int32	bh_datalen;	/* original length of packet */
122232289Sgonzo	u_short		bh_hdrlen;	/* length of bpf header (this struct
123232289Sgonzo					   plus alignment padding) */
124231987Sgonzo};
125231987Sgonzo/*
126231987Sgonzo * Because the structure above is not a multiple of 4 bytes, some compilers
127231987Sgonzo * will insist on inserting padding; hence, sizeof(struct bpf_hdr) won't work.
128231987Sgonzo * Only the kernel needs to know about it; applications use bh_hdrlen.
129210311Sjmallett */
130210311Sjmallett#ifdef KERNEL
131210311Sjmallett#define	SIZEOF_BPF_HDR	(sizeof(struct bpf_hdr) <= 20 ? 18 : \
132210311Sjmallett    sizeof(struct bpf_hdr))
133210311Sjmallett#endif
134210311Sjmallett
135210311Sjmallett/*
136210311Sjmallett * Data-link level type codes.
137210311Sjmallett */
138210311Sjmallett#define DLT_NULL	0	/* no link-layer encapsulation */
139210311Sjmallett#define DLT_EN10MB	1	/* Ethernet (10Mb) */
140210311Sjmallett#define DLT_EN3MB	2	/* Experimental Ethernet (3Mb) */
141210311Sjmallett#define DLT_AX25	3	/* Amateur Radio AX.25 */
142210311Sjmallett#define DLT_PRONET	4	/* Proteon ProNET Token Ring */
143210311Sjmallett#define DLT_CHAOS	5	/* Chaos */
144210311Sjmallett#define DLT_IEEE802	6	/* IEEE 802 Networks */
145210311Sjmallett#define DLT_ARCNET	7	/* ARCNET */
146210311Sjmallett#define DLT_SLIP	8	/* Serial Line IP */
147210311Sjmallett#define DLT_PPP		9	/* Point-to-point Protocol */
148210311Sjmallett#define DLT_FDDI	10	/* FDDI */
149210311Sjmallett#define DLT_ATM_RFC1483	11	/* LLC/SNAP encapsulated atm */
150210311Sjmallett
151210311Sjmallett/*
152210311Sjmallett * The instruction encodings.
153210311Sjmallett */
154210311Sjmallett/* instruction classes */
155210311Sjmallett#define BPF_CLASS(code) ((code) & 0x07)
156210311Sjmallett#define		BPF_LD		0x00
157210311Sjmallett#define		BPF_LDX		0x01
158210311Sjmallett#define		BPF_ST		0x02
159210311Sjmallett#define		BPF_STX		0x03
160210311Sjmallett#define		BPF_ALU		0x04
161210311Sjmallett#define		BPF_JMP		0x05
162210311Sjmallett#define		BPF_RET		0x06
163210311Sjmallett#define		BPF_MISC	0x07
164210311Sjmallett
165210311Sjmallett/* ld/ldx fields */
166210311Sjmallett#define BPF_SIZE(code)	((code) & 0x18)
167210311Sjmallett#define		BPF_W		0x00
168210311Sjmallett#define		BPF_H		0x08
169210311Sjmallett#define		BPF_B		0x10
170210311Sjmallett#define BPF_MODE(code)	((code) & 0xe0)
171210311Sjmallett#define		BPF_IMM 	0x00
172210311Sjmallett#define		BPF_ABS		0x20
173210311Sjmallett#define		BPF_IND		0x40
174210311Sjmallett#define		BPF_MEM		0x60
175210311Sjmallett#define		BPF_LEN		0x80
176210311Sjmallett#define		BPF_MSH		0xa0
177210311Sjmallett
178210311Sjmallett/* alu/jmp fields */
179210311Sjmallett#define BPF_OP(code)	((code) & 0xf0)
180210311Sjmallett#define		BPF_ADD		0x00
181210311Sjmallett#define		BPF_SUB		0x10
182210311Sjmallett#define		BPF_MUL		0x20
183210311Sjmallett#define		BPF_DIV		0x30
184210311Sjmallett#define		BPF_OR		0x40
185210311Sjmallett#define		BPF_AND		0x50
186210311Sjmallett#define		BPF_LSH		0x60
187210311Sjmallett#define		BPF_RSH		0x70
188210311Sjmallett#define		BPF_NEG		0x80
189210311Sjmallett#define		BPF_JA		0x00
190210311Sjmallett#define		BPF_JEQ		0x10
191210311Sjmallett#define		BPF_JGT		0x20
192210311Sjmallett#define		BPF_JGE		0x30
193210311Sjmallett#define		BPF_JSET	0x40
194210311Sjmallett#define BPF_SRC(code)	((code) & 0x08)
195210311Sjmallett#define		BPF_K		0x00
196210311Sjmallett#define		BPF_X		0x08
197210311Sjmallett
198210311Sjmallett/* ret - BPF_K and BPF_X also apply */
199210311Sjmallett#define BPF_RVAL(code)	((code) & 0x18)
200210311Sjmallett#define		BPF_A		0x10
201210311Sjmallett
202210311Sjmallett/* misc */
203210311Sjmallett#define BPF_MISCOP(code) ((code) & 0xf8)
204210311Sjmallett#define		BPF_TAX		0x00
205210311Sjmallett#define		BPF_TXA		0x80
206210311Sjmallett
207210311Sjmallett/*
208210311Sjmallett * The instruction data structure.
209210311Sjmallett */
210210311Sjmallettstruct bpf_insn {
211210311Sjmallett	u_short		code;
212215974Sjmallett	u_char		jt;
213215974Sjmallett	u_char		jf;
214215974Sjmallett	bpf_u_int32	k;
215215974Sjmallett};
216215974Sjmallett
217215974Sjmallett/*
218215974Sjmallett * Macros for insn array initializers.
219215974Sjmallett */
220215974Sjmallett#define BPF_STMT(code, k) { (u_short)(code), 0, 0, k }
221215974Sjmallett#define BPF_JUMP(code, k, jt, jf) { (u_short)(code), jt, jf, k }
222215974Sjmallett
223215974Sjmallett#ifdef KERNEL
224215974Sjmallettint	 bpf_validate __P((struct bpf_insn *, int));
225215974Sjmallettvoid	 bpf_tap __P((struct ifnet *, u_char *, u_int));
226216071Sjmallettvoid	 bpf_mtap __P((struct ifnet *, struct mbuf *));
227216071Sjmallettvoid	 bpfattach __P((struct ifnet *, u_int, u_int));
228216071Sjmallettvoid	 bpfilterattach __P((int));
229242346Sjmallettu_int	 bpf_filter __P((struct bpf_insn *, u_char *, u_int, u_int));
230215974Sjmallett#endif
231215974Sjmallett
232215974Sjmallett/*
233215974Sjmallett * Number of scratch memory words (for BPF_LD|BPF_MEM and BPF_ST).
234215974Sjmallett */
235215974Sjmallett#define BPF_MEMWORDS 16
236215974Sjmallett
237215974Sjmallett#endif
238215974Sjmallett