xref: /csrg-svn/sys/net/bpf.h (revision 51427)
149284Sbostic /*-
2*51427Smccanne  * Copyright (c) 1990-1991 The Regents of the University of California.
347638Smccanne  * All rights reserved.
447638Smccanne  *
549284Sbostic  * This code is derived from the Stanford/CMU enet packet filter,
649284Sbostic  * (net/enet.c) distributed as part of 4.3BSD, and code contributed
7*51427Smccanne  * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
8*51427Smccanne  * Berkeley Laboratory.
947638Smccanne  *
10*51427Smccanne  * Redistribution and use in source and binary forms, with or without
11*51427Smccanne  * modification, are permitted provided that the following conditions
12*51427Smccanne  * are met:
13*51427Smccanne  * 1. Redistributions of source code must retain the above copyright
14*51427Smccanne  *    notice, this list of conditions and the following disclaimer.
15*51427Smccanne  * 2. Redistributions in binary form must reproduce the above copyright
16*51427Smccanne  *    notice, this list of conditions and the following disclaimer in the
17*51427Smccanne  *    documentation and/or other materials provided with the distribution.
18*51427Smccanne  * 3. All advertising materials mentioning features or use of this software
19*51427Smccanne  *    must display the following acknowledgement:
20*51427Smccanne  *      This product includes software developed by the University of
21*51427Smccanne  *      California, Berkeley and its contributors.
22*51427Smccanne  * 4. Neither the name of the University nor the names of its contributors
23*51427Smccanne  *    may be used to endorse or promote products derived from this software
24*51427Smccanne  *    without specific prior written permission.
2549284Sbostic  *
26*51427Smccanne  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27*51427Smccanne  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28*51427Smccanne  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29*51427Smccanne  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30*51427Smccanne  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31*51427Smccanne  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32*51427Smccanne  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33*51427Smccanne  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34*51427Smccanne  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35*51427Smccanne  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36*51427Smccanne  * SUCH DAMAGE.
3749284Sbostic  *
38*51427Smccanne  *      @(#)bpf.h       7.1 (Berkeley) 5/7/91
39*51427Smccanne  *
40*51427Smccanne  * @(#) $Header: bpf.h,v 1.24 91/10/27 21:22:32 mccanne Exp $ (LBL)
4147638Smccanne  */
4247638Smccanne 
4347638Smccanne /*
4447638Smccanne  * Alignment macros.  BPF_WORDALIGN rounds up to the next
4547638Smccanne  * even multiple of BPF_ALIGNMENT.
4647638Smccanne  */
4747638Smccanne #define BPF_ALIGNMENT sizeof(long)
4847638Smccanne #define BPF_WORDALIGN(x) (((x)+(BPF_ALIGNMENT-1))&~(BPF_ALIGNMENT-1))
4947638Smccanne 
5048930Smccanne #define BPF_MAXINSNS 512
5148930Smccanne #define BPF_MAXBUFSIZE 0x8000
5248930Smccanne 
5347638Smccanne /*
5447638Smccanne  *  Structure for BIOCSETF.
5547638Smccanne  */
5647638Smccanne struct bpf_program {
5747638Smccanne 	u_int bf_len;
5847638Smccanne 	struct bpf_insn *bf_insns;
5947638Smccanne };
6047638Smccanne 
6147638Smccanne /*
6247638Smccanne  * Struct returned by BIOCGSTATS.
6347638Smccanne  */
6447638Smccanne struct bpf_stat {
6547638Smccanne 	u_int bs_recv;		/* number of packets received */
6647638Smccanne 	u_int bs_drop;		/* number of packets dropped */
6747638Smccanne };
6847638Smccanne 
6947638Smccanne /*
7047638Smccanne  * BPF ioctls
7147638Smccanne  *
7247638Smccanne  * The first set is for compatibility with Sun's pcc style
7347638Smccanne  * header files.  If your using gcc, we assume that you
7447638Smccanne  * have run fixincludes so the latter set should work.
7547638Smccanne  */
76*51427Smccanne #if (defined(sun) || defined(ibm032)) && !defined(__GNUC__)
7747638Smccanne #define	BIOCGBLEN	_IOR(B,102, u_int)
78*51427Smccanne #define	BIOCSBLEN	_IOWR(B,102, u_int)
7947638Smccanne #define	BIOCSETF	_IOW(B,103, struct bpf_program)
8047638Smccanne #define	BIOCFLUSH	_IO(B,104)
8147638Smccanne #define BIOCPROMISC	_IO(B,105)
8249201Smccanne #define	BIOCGDLT	_IOR(B,106, u_int)
8347638Smccanne #define BIOCGETIF	_IOR(B,107, struct ifreq)
8447638Smccanne #define BIOCSETIF	_IOW(B,108, struct ifreq)
8547638Smccanne #define BIOCSRTIMEOUT	_IOW(B,109, struct timeval)
8647638Smccanne #define BIOCGRTIMEOUT	_IOR(B,110, struct timeval)
8747638Smccanne #define BIOCGSTATS	_IOR(B,111, struct bpf_stat)
8847638Smccanne #define BIOCIMMEDIATE	_IOW(B,112, u_int)
8947638Smccanne #else
9047638Smccanne #define	BIOCGBLEN	_IOR('B',102, u_int)
91*51427Smccanne #define	BIOCSBLEN	_IOWR('B',102, u_int)
9247638Smccanne #define	BIOCSETF	_IOW('B',103, struct bpf_program)
9347638Smccanne #define	BIOCFLUSH	_IO('B',104)
9447638Smccanne #define BIOCPROMISC	_IO('B',105)
9549201Smccanne #define	BIOCGDLT	_IOR('B',106, u_int)
9647638Smccanne #define BIOCGETIF	_IOR('B',107, struct ifreq)
9747638Smccanne #define BIOCSETIF	_IOW('B',108, struct ifreq)
9847638Smccanne #define BIOCSRTIMEOUT	_IOW('B',109, struct timeval)
9947638Smccanne #define BIOCGRTIMEOUT	_IOR('B',110, struct timeval)
10047638Smccanne #define BIOCGSTATS	_IOR('B',111, struct bpf_stat)
10147638Smccanne #define BIOCIMMEDIATE	_IOW('B',112, u_int)
10247638Smccanne #endif
10347638Smccanne 
10447638Smccanne /*
10547638Smccanne  * Structure prepended to each packet.
10647638Smccanne  */
10747638Smccanne struct bpf_hdr {
10847638Smccanne 	struct timeval	bh_tstamp;	/* time stamp */
10947638Smccanne 	u_long		bh_caplen;	/* length of captured portion */
11047638Smccanne 	u_long		bh_datalen;	/* original length of packet */
11147638Smccanne 	u_short		bh_hdrlen;	/* length of bpf header (this struct
11247638Smccanne 					   plus alignment padding) */
11347638Smccanne };
11447638Smccanne /*
11547638Smccanne  * Because the structure above is not a multiple of 4 bytes, some compilers
11647638Smccanne  * will insist on inserting padding; hence, sizeof(struct bpf_hdr) won't work.
11747638Smccanne  * Only the kernel needs to know about it; applications use bh_hdrlen.
11847638Smccanne  */
11947638Smccanne #ifdef KERNEL
12047638Smccanne #define SIZEOF_BPF_HDR 18
12147638Smccanne #endif
12247638Smccanne 
12347638Smccanne /*
12447638Smccanne  * Data-link level type codes.
12549201Smccanne  * Currently, only DLT_EN10MB and DLT_SLIP are supported.
12647638Smccanne  */
127*51427Smccanne #define DLT_NULL	0	/* no link-layer encapsulation */
12847638Smccanne #define DLT_EN10MB	1	/* Ethernet (10Mb) */
12947638Smccanne #define DLT_EN3MB	2	/* Experimental Ethernet (3Mb) */
13047638Smccanne #define DLT_AX25	3	/* Amateur Radio AX.25 */
13147638Smccanne #define DLT_PRONET	4	/* Proteon ProNET Token Ring */
13247638Smccanne #define DLT_CHAOS	5	/* Chaos */
13347638Smccanne #define DLT_IEEE802	6	/* IEEE 802 Networks */
13447638Smccanne #define DLT_ARCNET	7	/* ARCNET */
13547638Smccanne #define DLT_SLIP	8	/* Serial Line IP */
13647638Smccanne #define DLT_PPP		9	/* Point-to-point Protocol */
13747638Smccanne #define DLT_FDDI	10	/* FDDI */
13847638Smccanne 
13947638Smccanne /*
14048968Smccanne  * The instruction encondings.
14147638Smccanne  */
14248968Smccanne /* classes <2:0> */
14348968Smccanne #define BPF_CLASS(code) ((code) & 0x07)
14448968Smccanne #define		BPF_LD		0x00
14548968Smccanne #define		BPF_LDX		0x01
14648968Smccanne #define		BPF_ST		0x02
14748968Smccanne #define		BPF_STX		0x03
14848968Smccanne #define		BPF_ALU		0x04
14948968Smccanne #define		BPF_JMP		0x05
15048968Smccanne #define		BPF_RET		0x06
15148968Smccanne #define		BPF_MISC	0x07
15247638Smccanne 
15348968Smccanne /* ld/ldx fields */
15448968Smccanne #define BPF_SIZE(code)	((code) & 0x18)
15548968Smccanne #define		BPF_W		0x00
15648968Smccanne #define		BPF_H		0x08
15748968Smccanne #define		BPF_B		0x10
15848968Smccanne #define BPF_MODE(code)	((code) & 0xe0)
15948968Smccanne #define		BPF_IMM 	0x00
16048968Smccanne #define		BPF_ABS		0x20
16148968Smccanne #define		BPF_IND		0x40
16248968Smccanne #define		BPF_MEM		0x60
16348968Smccanne #define		BPF_LEN		0x80
16448968Smccanne #define		BPF_MSH		0xa0
16547638Smccanne 
16648968Smccanne /* alu/jmp fields */
16748968Smccanne #define BPF_OP(code)	((code) & 0xf0)
16848968Smccanne #define		BPF_ADD		0x00
16948968Smccanne #define		BPF_SUB		0x10
17048968Smccanne #define		BPF_MUL		0x20
17148968Smccanne #define		BPF_DIV		0x30
17248968Smccanne #define		BPF_OR		0x40
17348968Smccanne #define		BPF_AND		0x50
17448968Smccanne #define		BPF_LSH		0x60
17548968Smccanne #define		BPF_RSH		0x70
17648968Smccanne #define		BPF_NEG		0x80
17748968Smccanne #define		BPF_JA		0x00
17848968Smccanne #define		BPF_JEQ		0x10
17948968Smccanne #define		BPF_JGT		0x20
18048968Smccanne #define		BPF_JGE		0x30
18148968Smccanne #define		BPF_JSET	0x40
18248968Smccanne #define BPF_SRC(code)	((code) & 0x08)
18348968Smccanne #define		BPF_K		0x00
18448968Smccanne #define		BPF_X		0x08
18548968Smccanne 
18648968Smccanne /* ret - BPF_K and BPF_X also apply */
18748968Smccanne #define BPF_RVAL(code)	((code) & 0x18)
18848968Smccanne #define		BPF_A		0x10
18948968Smccanne 
19048968Smccanne /* misc */
19148968Smccanne #define BPF_MISCOP(code) ((code) & 0xf8)
19248968Smccanne #define		BPF_TAX		0x00
19348968Smccanne #define		BPF_TXA		0x80
19448968Smccanne 
19547638Smccanne /*
19647638Smccanne  * The instruction data structure.
19747638Smccanne  */
19847638Smccanne struct bpf_insn {
19947638Smccanne 	u_short	code;
20047638Smccanne 	u_char 	jt;
20147638Smccanne 	u_char 	jf;
20247638Smccanne 	long	k;
20347638Smccanne };
20447638Smccanne 
20547638Smccanne /*
20648968Smccanne  * Macros for insn array initializers.
20747638Smccanne  */
20848968Smccanne #define BPF_STMT(code, k) { (u_short)(code), 0, 0, k }
20948968Smccanne #define BPF_JUMP(code, k, jt, jf) { (u_short)(code), jt, jf, k }
21047638Smccanne 
21147638Smccanne #ifdef KERNEL
21247638Smccanne extern u_int bpf_filter();
21347638Smccanne extern void bpfattach();
21447638Smccanne extern void bpf_tap();
21547638Smccanne extern void bpf_mtap();
21647638Smccanne #endif
21747638Smccanne 
21847638Smccanne /*
21948968Smccanne  * Number of scratch memory words (for BPF_LD|BPF_MEM and BPF_ST).
22047638Smccanne  */
22147638Smccanne #define BPF_MEMWORDS 16
222*51427Smccanne 
223