xref: /csrg-svn/sys/net/bpf.h (revision 48968)
147638Smccanne /*
247638Smccanne  * Copyright (c) 1990 The Regents of the University of California.
347638Smccanne  * All rights reserved.
447638Smccanne  *
547638Smccanne  * Redistribution and use in source and binary forms, with or without
647638Smccanne  * modification, are permitted provided that: (1) source code distributions
747638Smccanne  * retain the above copyright notice and this paragraph in its entirety, (2)
847638Smccanne  * distributions including binary code include the above copyright notice and
947638Smccanne  * this paragraph in its entirety in the documentation or other materials
1047638Smccanne  * provided with the distribution, and (3) all advertising materials mentioning
1147638Smccanne  * features or use of this software display the following acknowledgement:
1247638Smccanne  * ``This product includes software developed by the University of California,
1347638Smccanne  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
1447638Smccanne  * the University nor the names of its contributors may be used to endorse
1547638Smccanne  * or promote products derived from this software without specific prior
1647638Smccanne  * written permission.
1747638Smccanne  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
1847638Smccanne  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
1947638Smccanne  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
2047638Smccanne  *
21*48968Smccanne  * @(#) $Header: bpf.h,v 1.20 91/04/24 22:06:24 mccanne Locked $ (LBL)
2247638Smccanne  *
2347638Smccanne  * This code is derived from the Stanford/CMU enet packet filter,
2447638Smccanne  * (net/enet.h) distributed with 4.3BSD Unix.
2547638Smccanne  */
2647638Smccanne 
2747638Smccanne /*
2847638Smccanne  * Alignment macros.  BPF_WORDALIGN rounds up to the next
2947638Smccanne  * even multiple of BPF_ALIGNMENT.
3047638Smccanne  */
3147638Smccanne #define BPF_ALIGNMENT sizeof(long)
3247638Smccanne #define BPF_WORDALIGN(x) (((x)+(BPF_ALIGNMENT-1))&~(BPF_ALIGNMENT-1))
3347638Smccanne 
3448930Smccanne #define BPF_MAXINSNS 512
3548930Smccanne #define BPF_MAXBUFSIZE 0x8000
3648930Smccanne 
3747638Smccanne /*
3847638Smccanne  *  Structure for BIOCSETF.
3947638Smccanne  */
4047638Smccanne struct bpf_program {
4147638Smccanne 	u_int bf_len;
4247638Smccanne 	struct bpf_insn *bf_insns;
4347638Smccanne };
4447638Smccanne 
4547638Smccanne /*
4647638Smccanne  * Struct returned by BIOCGSTATS.
4747638Smccanne  */
4847638Smccanne struct bpf_stat {
4947638Smccanne 	u_int bs_recv;		/* number of packets received */
5047638Smccanne 	u_int bs_drop;		/* number of packets dropped */
5147638Smccanne };
5247638Smccanne 
5347638Smccanne /*
5447638Smccanne  * BPF ioctls
5547638Smccanne  *
5647638Smccanne  * The first set is for compatibility with Sun's pcc style
5747638Smccanne  * header files.  If your using gcc, we assume that you
5847638Smccanne  * have run fixincludes so the latter set should work.
5947638Smccanne  */
6047638Smccanne #if defined(sun) && !defined(__GNUC__)
6147638Smccanne #define	BIOCGFLEN	_IOR(B,101, u_int)
6247638Smccanne #define	BIOCGBLEN	_IOR(B,102, u_int)
6347638Smccanne #define	BIOCSETF	_IOW(B,103, struct bpf_program)
6447638Smccanne #define	BIOCFLUSH	_IO(B,104)
6547638Smccanne #define BIOCPROMISC	_IO(B,105)
6647638Smccanne #define	BIOCDEVP	_IOR(B,106, struct bpf_devp)
6747638Smccanne #define BIOCGETIF	_IOR(B,107, struct ifreq)
6847638Smccanne #define BIOCSETIF	_IOW(B,108, struct ifreq)
6947638Smccanne #define BIOCSRTIMEOUT	_IOW(B,109, struct timeval)
7047638Smccanne #define BIOCGRTIMEOUT	_IOR(B,110, struct timeval)
7147638Smccanne #define BIOCGSTATS	_IOR(B,111, struct bpf_stat)
7247638Smccanne #define BIOCIMMEDIATE	_IOW(B,112, u_int)
7347638Smccanne #else
7447638Smccanne #define	BIOCGFLEN	_IOR('B',101, u_int)
7547638Smccanne #define	BIOCGBLEN	_IOR('B',102, u_int)
7647638Smccanne #define	BIOCSETF	_IOW('B',103, struct bpf_program)
7747638Smccanne #define	BIOCFLUSH	_IO('B',104)
7847638Smccanne #define BIOCPROMISC	_IO('B',105)
7947638Smccanne #define	BIOCDEVP	_IOR('B',106, struct bpf_devp)
8047638Smccanne #define BIOCGETIF	_IOR('B',107, struct ifreq)
8147638Smccanne #define BIOCSETIF	_IOW('B',108, struct ifreq)
8247638Smccanne #define BIOCSRTIMEOUT	_IOW('B',109, struct timeval)
8347638Smccanne #define BIOCGRTIMEOUT	_IOR('B',110, struct timeval)
8447638Smccanne #define BIOCGSTATS	_IOR('B',111, struct bpf_stat)
8547638Smccanne #define BIOCIMMEDIATE	_IOW('B',112, u_int)
8647638Smccanne #endif
8747638Smccanne 
8847638Smccanne /*
8947638Smccanne  * The device parameters of a network interface.
9047638Smccanne  */
9147638Smccanne struct bpf_devp {
9247638Smccanne 	u_short	bdev_type;	/* data link layer type, codes below */
9347638Smccanne 	u_short	bdev_hdrlen;	/* length of a hardware packet header */
9447638Smccanne };
9547638Smccanne 
9647638Smccanne /*
9747638Smccanne  * Structure prepended to each packet.
9847638Smccanne  */
9947638Smccanne struct bpf_hdr {
10047638Smccanne 	struct timeval	bh_tstamp;	/* time stamp */
10147638Smccanne 	u_long		bh_caplen;	/* length of captured portion */
10247638Smccanne 	u_long		bh_datalen;	/* original length of packet */
10347638Smccanne 	u_short		bh_hdrlen;	/* length of bpf header (this struct
10447638Smccanne 					   plus alignment padding) */
10547638Smccanne };
10647638Smccanne /*
10747638Smccanne  * Because the structure above is not a multiple of 4 bytes, some compilers
10847638Smccanne  * will insist on inserting padding; hence, sizeof(struct bpf_hdr) won't work.
10947638Smccanne  * Only the kernel needs to know about it; applications use bh_hdrlen.
11047638Smccanne  */
11147638Smccanne #ifdef KERNEL
11247638Smccanne #define SIZEOF_BPF_HDR 18
11347638Smccanne #endif
11447638Smccanne 
11547638Smccanne /*
11647638Smccanne  * Data-link level type codes.
11747638Smccanne  * Currently, only ENDT_10MB and DLT_SLIP are supported.
11847638Smccanne  */
11947638Smccanne #define DLT_EN10MB	1	/* Ethernet (10Mb) */
12047638Smccanne #define DLT_EN3MB	2	/* Experimental Ethernet (3Mb) */
12147638Smccanne #define DLT_AX25	3	/* Amateur Radio AX.25 */
12247638Smccanne #define DLT_PRONET	4	/* Proteon ProNET Token Ring */
12347638Smccanne #define DLT_CHAOS	5	/* Chaos */
12447638Smccanne #define DLT_IEEE802	6	/* IEEE 802 Networks */
12547638Smccanne #define DLT_ARCNET	7	/* ARCNET */
12647638Smccanne #define DLT_SLIP	8	/* Serial Line IP */
12747638Smccanne #define DLT_PPP		9	/* Point-to-point Protocol */
12847638Smccanne #define DLT_FDDI	10	/* FDDI */
12947638Smccanne 
13047638Smccanne /*
131*48968Smccanne  * The instruction encondings.
13247638Smccanne  */
133*48968Smccanne /* classes <2:0> */
134*48968Smccanne #define BPF_CLASS(code) ((code) & 0x07)
135*48968Smccanne #define		BPF_LD		0x00
136*48968Smccanne #define		BPF_LDX		0x01
137*48968Smccanne #define		BPF_ST		0x02
138*48968Smccanne #define		BPF_STX		0x03
139*48968Smccanne #define		BPF_ALU		0x04
140*48968Smccanne #define		BPF_JMP		0x05
141*48968Smccanne #define		BPF_RET		0x06
142*48968Smccanne #define		BPF_MISC	0x07
14347638Smccanne 
144*48968Smccanne /* ld/ldx fields */
145*48968Smccanne #define BPF_SIZE(code)	((code) & 0x18)
146*48968Smccanne #define		BPF_W		0x00
147*48968Smccanne #define		BPF_H		0x08
148*48968Smccanne #define		BPF_B		0x10
149*48968Smccanne #define BPF_MODE(code)	((code) & 0xe0)
150*48968Smccanne #define		BPF_IMM 	0x00
151*48968Smccanne #define		BPF_ABS		0x20
152*48968Smccanne #define		BPF_IND		0x40
153*48968Smccanne #define		BPF_MEM		0x60
154*48968Smccanne #define		BPF_LEN		0x80
155*48968Smccanne #define		BPF_MSH		0xa0
15647638Smccanne 
157*48968Smccanne /* alu/jmp fields */
158*48968Smccanne #define BPF_OP(code)	((code) & 0xf0)
159*48968Smccanne #define		BPF_ADD		0x00
160*48968Smccanne #define		BPF_SUB		0x10
161*48968Smccanne #define		BPF_MUL		0x20
162*48968Smccanne #define		BPF_DIV		0x30
163*48968Smccanne #define		BPF_OR		0x40
164*48968Smccanne #define		BPF_AND		0x50
165*48968Smccanne #define		BPF_LSH		0x60
166*48968Smccanne #define		BPF_RSH		0x70
167*48968Smccanne #define		BPF_NEG		0x80
168*48968Smccanne #define		BPF_JA		0x00
169*48968Smccanne #define		BPF_JEQ		0x10
170*48968Smccanne #define		BPF_JGT		0x20
171*48968Smccanne #define		BPF_JGE		0x30
172*48968Smccanne #define		BPF_JSET	0x40
173*48968Smccanne #define BPF_SRC(code)	((code) & 0x08)
174*48968Smccanne #define		BPF_K		0x00
175*48968Smccanne #define		BPF_X		0x08
176*48968Smccanne 
177*48968Smccanne /* ret - BPF_K and BPF_X also apply */
178*48968Smccanne #define BPF_RVAL(code)	((code) & 0x18)
179*48968Smccanne #define		BPF_A		0x10
180*48968Smccanne 
181*48968Smccanne /* misc */
182*48968Smccanne #define BPF_MISCOP(code) ((code) & 0xf8)
183*48968Smccanne #define		BPF_TAX		0x00
184*48968Smccanne #define		BPF_TXA		0x80
185*48968Smccanne 
18647638Smccanne /*
18747638Smccanne  * The instruction data structure.
18847638Smccanne  */
18947638Smccanne struct bpf_insn {
19047638Smccanne 	u_short	code;
19147638Smccanne 	u_char 	jt;
19247638Smccanne 	u_char 	jf;
19347638Smccanne 	long	k;
19447638Smccanne };
19547638Smccanne 
19647638Smccanne /*
197*48968Smccanne  * Macros for insn array initializers.
19847638Smccanne  */
199*48968Smccanne #define BPF_STMT(code, k) { (u_short)(code), 0, 0, k }
200*48968Smccanne #define BPF_JUMP(code, k, jt, jf) { (u_short)(code), jt, jf, k }
20147638Smccanne 
20247638Smccanne #ifdef KERNEL
20347638Smccanne extern u_int bpf_filter();
20447638Smccanne extern void bpfattach();
20547638Smccanne extern void bpf_tap();
20647638Smccanne extern void bpf_mtap();
20747638Smccanne #endif
20847638Smccanne 
20947638Smccanne /*
210*48968Smccanne  * Number of scratch memory words (for BPF_LD|BPF_MEM and BPF_ST).
21147638Smccanne  */
21247638Smccanne #define BPF_MEMWORDS 16
213