xref: /csrg-svn/sys/net/bpf.h (revision 63207)
151441Smccanne /*
2*63207Sbostic  * Copyright (c) 1990, 1991, 1993
3*63207Sbostic  *	The Regents of the University of California.  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
751441Smccanne  * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
851427Smccanne  * Berkeley Laboratory.
947638Smccanne  *
1051441Smccanne  * %sccs.include.redist.c%
1149284Sbostic  *
12*63207Sbostic  *      @(#)bpf.h	8.1 (Berkeley) 06/10/93
1349284Sbostic  *
1451427Smccanne  * @(#) $Header: bpf.h,v 1.24 91/10/27 21:22:32 mccanne Exp $ (LBL)
1547638Smccanne  */
1647638Smccanne 
1747638Smccanne /*
1847638Smccanne  * Alignment macros.  BPF_WORDALIGN rounds up to the next
1947638Smccanne  * even multiple of BPF_ALIGNMENT.
2047638Smccanne  */
2147638Smccanne #define BPF_ALIGNMENT sizeof(long)
2247638Smccanne #define BPF_WORDALIGN(x) (((x)+(BPF_ALIGNMENT-1))&~(BPF_ALIGNMENT-1))
2347638Smccanne 
2448930Smccanne #define BPF_MAXINSNS 512
2548930Smccanne #define BPF_MAXBUFSIZE 0x8000
2653948Smccanne #define BPF_MINBUFSIZE 32
2748930Smccanne 
2847638Smccanne /*
2947638Smccanne  *  Structure for BIOCSETF.
3047638Smccanne  */
3147638Smccanne struct bpf_program {
3247638Smccanne 	u_int bf_len;
3347638Smccanne 	struct bpf_insn *bf_insns;
3447638Smccanne };
3547638Smccanne 
3647638Smccanne /*
3747638Smccanne  * Struct returned by BIOCGSTATS.
3847638Smccanne  */
3947638Smccanne struct bpf_stat {
4047638Smccanne 	u_int bs_recv;		/* number of packets received */
4147638Smccanne 	u_int bs_drop;		/* number of packets dropped */
4247638Smccanne };
4347638Smccanne 
4447638Smccanne /*
4553948Smccanne  * Struct return by BIOCVERSION.  This represents the version number of
4653948Smccanne  * the filter language described by the instruction encodings below.
4753948Smccanne  * bpf understands a program iff kernel_major == filter_major &&
4853948Smccanne  * kernel_minor >= filter_minor, that is, if the value returned by the
4953948Smccanne  * running kernel has the same major number and a minor number equal
5053948Smccanne  * equal to or less than the filter being downloaded.  Otherwise, the
5153948Smccanne  * results are undefined, meaning an error may be returned or packets
5253948Smccanne  * may be accepted haphazardly.
5353948Smccanne  * It has nothing to do with the source code version.
5453948Smccanne  */
5553948Smccanne struct bpf_version {
5653948Smccanne 	u_short bv_major;
5753948Smccanne 	u_short bv_minor;
5853948Smccanne };
5953948Smccanne /* Current version number. */
6053948Smccanne #define BPF_MAJOR_VERSION 1
6153948Smccanne #define BPF_MINOR_VERSION 1
6253948Smccanne 
6353948Smccanne /*
6447638Smccanne  * BPF ioctls
6547638Smccanne  *
6647638Smccanne  * The first set is for compatibility with Sun's pcc style
6747638Smccanne  * header files.  If your using gcc, we assume that you
6847638Smccanne  * have run fixincludes so the latter set should work.
6947638Smccanne  */
7051427Smccanne #if (defined(sun) || defined(ibm032)) && !defined(__GNUC__)
7147638Smccanne #define	BIOCGBLEN	_IOR(B,102, u_int)
7251427Smccanne #define	BIOCSBLEN	_IOWR(B,102, u_int)
7347638Smccanne #define	BIOCSETF	_IOW(B,103, struct bpf_program)
7447638Smccanne #define	BIOCFLUSH	_IO(B,104)
7547638Smccanne #define BIOCPROMISC	_IO(B,105)
7649201Smccanne #define	BIOCGDLT	_IOR(B,106, u_int)
7747638Smccanne #define BIOCGETIF	_IOR(B,107, struct ifreq)
7847638Smccanne #define BIOCSETIF	_IOW(B,108, struct ifreq)
7947638Smccanne #define BIOCSRTIMEOUT	_IOW(B,109, struct timeval)
8047638Smccanne #define BIOCGRTIMEOUT	_IOR(B,110, struct timeval)
8147638Smccanne #define BIOCGSTATS	_IOR(B,111, struct bpf_stat)
8247638Smccanne #define BIOCIMMEDIATE	_IOW(B,112, u_int)
8353948Smccanne #define BIOCVERSION	_IOR(B,113, struct bpf_version)
8447638Smccanne #else
8547638Smccanne #define	BIOCGBLEN	_IOR('B',102, u_int)
8651427Smccanne #define	BIOCSBLEN	_IOWR('B',102, u_int)
8747638Smccanne #define	BIOCSETF	_IOW('B',103, struct bpf_program)
8847638Smccanne #define	BIOCFLUSH	_IO('B',104)
8947638Smccanne #define BIOCPROMISC	_IO('B',105)
9049201Smccanne #define	BIOCGDLT	_IOR('B',106, u_int)
9147638Smccanne #define BIOCGETIF	_IOR('B',107, struct ifreq)
9247638Smccanne #define BIOCSETIF	_IOW('B',108, struct ifreq)
9347638Smccanne #define BIOCSRTIMEOUT	_IOW('B',109, struct timeval)
9447638Smccanne #define BIOCGRTIMEOUT	_IOR('B',110, struct timeval)
9547638Smccanne #define BIOCGSTATS	_IOR('B',111, struct bpf_stat)
9647638Smccanne #define BIOCIMMEDIATE	_IOW('B',112, u_int)
9753948Smccanne #define BIOCVERSION	_IOR('B',113, struct bpf_version)
9847638Smccanne #endif
9947638Smccanne 
10047638Smccanne /*
10147638Smccanne  * Structure prepended to each packet.
10247638Smccanne  */
10347638Smccanne struct bpf_hdr {
10447638Smccanne 	struct timeval	bh_tstamp;	/* time stamp */
10547638Smccanne 	u_long		bh_caplen;	/* length of captured portion */
10647638Smccanne 	u_long		bh_datalen;	/* original length of packet */
10747638Smccanne 	u_short		bh_hdrlen;	/* length of bpf header (this struct
10847638Smccanne 					   plus alignment padding) */
10947638Smccanne };
11047638Smccanne /*
11147638Smccanne  * Because the structure above is not a multiple of 4 bytes, some compilers
11247638Smccanne  * will insist on inserting padding; hence, sizeof(struct bpf_hdr) won't work.
11347638Smccanne  * Only the kernel needs to know about it; applications use bh_hdrlen.
11447638Smccanne  */
11547638Smccanne #ifdef KERNEL
11647638Smccanne #define SIZEOF_BPF_HDR 18
11747638Smccanne #endif
11847638Smccanne 
11947638Smccanne /*
12047638Smccanne  * Data-link level type codes.
12149201Smccanne  * Currently, only DLT_EN10MB and DLT_SLIP are supported.
12247638Smccanne  */
12351427Smccanne #define DLT_NULL	0	/* no link-layer encapsulation */
12447638Smccanne #define DLT_EN10MB	1	/* Ethernet (10Mb) */
12547638Smccanne #define DLT_EN3MB	2	/* Experimental Ethernet (3Mb) */
12647638Smccanne #define DLT_AX25	3	/* Amateur Radio AX.25 */
12747638Smccanne #define DLT_PRONET	4	/* Proteon ProNET Token Ring */
12847638Smccanne #define DLT_CHAOS	5	/* Chaos */
12947638Smccanne #define DLT_IEEE802	6	/* IEEE 802 Networks */
13047638Smccanne #define DLT_ARCNET	7	/* ARCNET */
13147638Smccanne #define DLT_SLIP	8	/* Serial Line IP */
13247638Smccanne #define DLT_PPP		9	/* Point-to-point Protocol */
13347638Smccanne #define DLT_FDDI	10	/* FDDI */
13447638Smccanne 
13547638Smccanne /*
13648968Smccanne  * The instruction encondings.
13747638Smccanne  */
13853948Smccanne /* instruction classes */
13948968Smccanne #define BPF_CLASS(code) ((code) & 0x07)
14048968Smccanne #define		BPF_LD		0x00
14148968Smccanne #define		BPF_LDX		0x01
14248968Smccanne #define		BPF_ST		0x02
14348968Smccanne #define		BPF_STX		0x03
14448968Smccanne #define		BPF_ALU		0x04
14548968Smccanne #define		BPF_JMP		0x05
14648968Smccanne #define		BPF_RET		0x06
14748968Smccanne #define		BPF_MISC	0x07
14847638Smccanne 
14948968Smccanne /* ld/ldx fields */
15048968Smccanne #define BPF_SIZE(code)	((code) & 0x18)
15148968Smccanne #define		BPF_W		0x00
15248968Smccanne #define		BPF_H		0x08
15348968Smccanne #define		BPF_B		0x10
15448968Smccanne #define BPF_MODE(code)	((code) & 0xe0)
15548968Smccanne #define		BPF_IMM 	0x00
15648968Smccanne #define		BPF_ABS		0x20
15748968Smccanne #define		BPF_IND		0x40
15848968Smccanne #define		BPF_MEM		0x60
15948968Smccanne #define		BPF_LEN		0x80
16048968Smccanne #define		BPF_MSH		0xa0
16147638Smccanne 
16248968Smccanne /* alu/jmp fields */
16348968Smccanne #define BPF_OP(code)	((code) & 0xf0)
16448968Smccanne #define		BPF_ADD		0x00
16548968Smccanne #define		BPF_SUB		0x10
16648968Smccanne #define		BPF_MUL		0x20
16748968Smccanne #define		BPF_DIV		0x30
16848968Smccanne #define		BPF_OR		0x40
16948968Smccanne #define		BPF_AND		0x50
17048968Smccanne #define		BPF_LSH		0x60
17148968Smccanne #define		BPF_RSH		0x70
17248968Smccanne #define		BPF_NEG		0x80
17348968Smccanne #define		BPF_JA		0x00
17448968Smccanne #define		BPF_JEQ		0x10
17548968Smccanne #define		BPF_JGT		0x20
17648968Smccanne #define		BPF_JGE		0x30
17748968Smccanne #define		BPF_JSET	0x40
17848968Smccanne #define BPF_SRC(code)	((code) & 0x08)
17948968Smccanne #define		BPF_K		0x00
18048968Smccanne #define		BPF_X		0x08
18148968Smccanne 
18248968Smccanne /* ret - BPF_K and BPF_X also apply */
18348968Smccanne #define BPF_RVAL(code)	((code) & 0x18)
18448968Smccanne #define		BPF_A		0x10
18548968Smccanne 
18648968Smccanne /* misc */
18748968Smccanne #define BPF_MISCOP(code) ((code) & 0xf8)
18848968Smccanne #define		BPF_TAX		0x00
18948968Smccanne #define		BPF_TXA		0x80
19048968Smccanne 
19147638Smccanne /*
19247638Smccanne  * The instruction data structure.
19347638Smccanne  */
19447638Smccanne struct bpf_insn {
19547638Smccanne 	u_short	code;
19647638Smccanne 	u_char 	jt;
19747638Smccanne 	u_char 	jf;
19847638Smccanne 	long	k;
19947638Smccanne };
20047638Smccanne 
20147638Smccanne /*
20248968Smccanne  * Macros for insn array initializers.
20347638Smccanne  */
20448968Smccanne #define BPF_STMT(code, k) { (u_short)(code), 0, 0, k }
20548968Smccanne #define BPF_JUMP(code, k, jt, jf) { (u_short)(code), jt, jf, k }
20647638Smccanne 
20747638Smccanne #ifdef KERNEL
20861337Sbostic int	 bpf_validate __P((struct bpf_insn *, int));
20961337Sbostic int	 bpfopen __P((dev_t, int));
21061337Sbostic int	 bpfclose __P((dev_t, int));
21161337Sbostic int	 bpfread __P((dev_t, struct uio *));
21261337Sbostic int	 bpfwrite __P((dev_t, struct uio *));
21361337Sbostic int	 bpfioctl __P((dev_t, int, caddr_t, int));
21461337Sbostic int	 bpf_select __P((dev_t, int, struct proc *));
21561337Sbostic void	 bpf_tap __P((caddr_t, u_char *, u_int));
21661337Sbostic void	 bpf_mtap __P((caddr_t, struct mbuf *));
21761337Sbostic void	 bpfattach __P((caddr_t *, struct ifnet *, u_int, u_int));
21861337Sbostic void	 bpfilterattach __P((int));
21961337Sbostic u_int	 bpf_filter __P((struct bpf_insn *, u_char *, u_int, u_int));
22047638Smccanne #endif
22147638Smccanne 
22247638Smccanne /*
22348968Smccanne  * Number of scratch memory words (for BPF_LD|BPF_MEM and BPF_ST).
22447638Smccanne  */
22547638Smccanne #define BPF_MEMWORDS 16
22651427Smccanne 
227