xref: /original-bsd/sys/net/bpf.h (revision 27393bdf)
1 /*
2  * Copyright (c) 1990, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from the Stanford/CMU enet packet filter,
6  * (net/enet.c) distributed as part of 4.3BSD, and code contributed
7  * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
8  * Berkeley Laboratory.
9  *
10  * %sccs.include.redist.c%
11  *
12  *      @(#)bpf.h	8.2 (Berkeley) 01/09/95
13  *
14  * @(#) $Header: bpf.h,v 1.24 91/10/27 21:22:32 mccanne Exp $ (LBL)
15  */
16 
17 /*
18  * Alignment macros.  BPF_WORDALIGN rounds up to the next
19  * even multiple of BPF_ALIGNMENT.
20  */
21 #define BPF_ALIGNMENT sizeof(long)
22 #define BPF_WORDALIGN(x) (((x)+(BPF_ALIGNMENT-1))&~(BPF_ALIGNMENT-1))
23 
24 #define BPF_MAXINSNS 512
25 #define BPF_MAXBUFSIZE 0x8000
26 #define BPF_MINBUFSIZE 32
27 
28 /*
29  *  Structure for BIOCSETF.
30  */
31 struct bpf_program {
32 	u_int bf_len;
33 	struct bpf_insn *bf_insns;
34 };
35 
36 /*
37  * Struct returned by BIOCGSTATS.
38  */
39 struct bpf_stat {
40 	u_int bs_recv;		/* number of packets received */
41 	u_int bs_drop;		/* number of packets dropped */
42 };
43 
44 /*
45  * Struct return by BIOCVERSION.  This represents the version number of
46  * the filter language described by the instruction encodings below.
47  * bpf understands a program iff kernel_major == filter_major &&
48  * kernel_minor >= filter_minor, that is, if the value returned by the
49  * running kernel has the same major number and a minor number equal
50  * equal to or less than the filter being downloaded.  Otherwise, the
51  * results are undefined, meaning an error may be returned or packets
52  * may be accepted haphazardly.
53  * It has nothing to do with the source code version.
54  */
55 struct bpf_version {
56 	u_short bv_major;
57 	u_short bv_minor;
58 };
59 /* Current version number. */
60 #define BPF_MAJOR_VERSION 1
61 #define BPF_MINOR_VERSION 1
62 
63 /*
64  * BPF ioctls
65  *
66  * The first set is for compatibility with Sun's pcc style
67  * header files.  If your using gcc, we assume that you
68  * have run fixincludes so the latter set should work.
69  */
70 #if (defined(sun) || defined(ibm032)) && !defined(__GNUC__)
71 #define	BIOCGBLEN	_IOR(B,102, u_int)
72 #define	BIOCSBLEN	_IOWR(B,102, u_int)
73 #define	BIOCSETF	_IOW(B,103, struct bpf_program)
74 #define	BIOCFLUSH	_IO(B,104)
75 #define BIOCPROMISC	_IO(B,105)
76 #define	BIOCGDLT	_IOR(B,106, u_int)
77 #define BIOCGETIF	_IOR(B,107, struct ifreq)
78 #define BIOCSETIF	_IOW(B,108, struct ifreq)
79 #define BIOCSRTIMEOUT	_IOW(B,109, struct timeval)
80 #define BIOCGRTIMEOUT	_IOR(B,110, struct timeval)
81 #define BIOCGSTATS	_IOR(B,111, struct bpf_stat)
82 #define BIOCIMMEDIATE	_IOW(B,112, u_int)
83 #define BIOCVERSION	_IOR(B,113, struct bpf_version)
84 #else
85 #define	BIOCGBLEN	_IOR('B',102, u_int)
86 #define	BIOCSBLEN	_IOWR('B',102, u_int)
87 #define	BIOCSETF	_IOW('B',103, struct bpf_program)
88 #define	BIOCFLUSH	_IO('B',104)
89 #define BIOCPROMISC	_IO('B',105)
90 #define	BIOCGDLT	_IOR('B',106, u_int)
91 #define BIOCGETIF	_IOR('B',107, struct ifreq)
92 #define BIOCSETIF	_IOW('B',108, struct ifreq)
93 #define BIOCSRTIMEOUT	_IOW('B',109, struct timeval)
94 #define BIOCGRTIMEOUT	_IOR('B',110, struct timeval)
95 #define BIOCGSTATS	_IOR('B',111, struct bpf_stat)
96 #define BIOCIMMEDIATE	_IOW('B',112, u_int)
97 #define BIOCVERSION	_IOR('B',113, struct bpf_version)
98 #endif
99 
100 /*
101  * Structure prepended to each packet.
102  */
103 struct bpf_hdr {
104 	struct timeval	bh_tstamp;	/* time stamp */
105 	u_long		bh_caplen;	/* length of captured portion */
106 	u_long		bh_datalen;	/* original length of packet */
107 	u_short		bh_hdrlen;	/* length of bpf header (this struct
108 					   plus alignment padding) */
109 };
110 /*
111  * Because the structure above is not a multiple of 4 bytes, some compilers
112  * will insist on inserting padding; hence, sizeof(struct bpf_hdr) won't work.
113  * Only the kernel needs to know about it; applications use bh_hdrlen.
114  */
115 #ifdef KERNEL
116 #define SIZEOF_BPF_HDR 18
117 #endif
118 
119 /*
120  * Data-link level type codes.
121  * Currently, only DLT_EN10MB and DLT_SLIP are supported.
122  */
123 #define DLT_NULL	0	/* no link-layer encapsulation */
124 #define DLT_EN10MB	1	/* Ethernet (10Mb) */
125 #define DLT_EN3MB	2	/* Experimental Ethernet (3Mb) */
126 #define DLT_AX25	3	/* Amateur Radio AX.25 */
127 #define DLT_PRONET	4	/* Proteon ProNET Token Ring */
128 #define DLT_CHAOS	5	/* Chaos */
129 #define DLT_IEEE802	6	/* IEEE 802 Networks */
130 #define DLT_ARCNET	7	/* ARCNET */
131 #define DLT_SLIP	8	/* Serial Line IP */
132 #define DLT_PPP		9	/* Point-to-point Protocol */
133 #define DLT_FDDI	10	/* FDDI */
134 
135 /*
136  * The instruction encondings.
137  */
138 /* instruction classes */
139 #define BPF_CLASS(code) ((code) & 0x07)
140 #define		BPF_LD		0x00
141 #define		BPF_LDX		0x01
142 #define		BPF_ST		0x02
143 #define		BPF_STX		0x03
144 #define		BPF_ALU		0x04
145 #define		BPF_JMP		0x05
146 #define		BPF_RET		0x06
147 #define		BPF_MISC	0x07
148 
149 /* ld/ldx fields */
150 #define BPF_SIZE(code)	((code) & 0x18)
151 #define		BPF_W		0x00
152 #define		BPF_H		0x08
153 #define		BPF_B		0x10
154 #define BPF_MODE(code)	((code) & 0xe0)
155 #define		BPF_IMM 	0x00
156 #define		BPF_ABS		0x20
157 #define		BPF_IND		0x40
158 #define		BPF_MEM		0x60
159 #define		BPF_LEN		0x80
160 #define		BPF_MSH		0xa0
161 
162 /* alu/jmp fields */
163 #define BPF_OP(code)	((code) & 0xf0)
164 #define		BPF_ADD		0x00
165 #define		BPF_SUB		0x10
166 #define		BPF_MUL		0x20
167 #define		BPF_DIV		0x30
168 #define		BPF_OR		0x40
169 #define		BPF_AND		0x50
170 #define		BPF_LSH		0x60
171 #define		BPF_RSH		0x70
172 #define		BPF_NEG		0x80
173 #define		BPF_JA		0x00
174 #define		BPF_JEQ		0x10
175 #define		BPF_JGT		0x20
176 #define		BPF_JGE		0x30
177 #define		BPF_JSET	0x40
178 #define BPF_SRC(code)	((code) & 0x08)
179 #define		BPF_K		0x00
180 #define		BPF_X		0x08
181 
182 /* ret - BPF_K and BPF_X also apply */
183 #define BPF_RVAL(code)	((code) & 0x18)
184 #define		BPF_A		0x10
185 
186 /* misc */
187 #define BPF_MISCOP(code) ((code) & 0xf8)
188 #define		BPF_TAX		0x00
189 #define		BPF_TXA		0x80
190 
191 /*
192  * The instruction data structure.
193  */
194 struct bpf_insn {
195 	u_short	code;
196 	u_char 	jt;
197 	u_char 	jf;
198 	long	k;
199 };
200 
201 /*
202  * Macros for insn array initializers.
203  */
204 #define BPF_STMT(code, k) { (u_short)(code), 0, 0, k }
205 #define BPF_JUMP(code, k, jt, jf) { (u_short)(code), jt, jf, k }
206 
207 #ifdef KERNEL
208 int	 bpf_validate __P((struct bpf_insn *, int));
209 int	 bpfopen __P((dev_t, int));
210 int	 bpfclose __P((dev_t, int));
211 int	 bpfread __P((dev_t, struct uio *));
212 int	 bpfwrite __P((dev_t, struct uio *));
213 int	 bpfioctl __P((dev_t, u_long, caddr_t, int));
214 int	 bpf_select __P((dev_t, int, struct proc *));
215 void	 bpf_tap __P((caddr_t, u_char *, u_int));
216 void	 bpf_mtap __P((caddr_t, struct mbuf *));
217 void	 bpfattach __P((caddr_t *, struct ifnet *, u_int, u_int));
218 void	 bpfilterattach __P((int));
219 u_int	 bpf_filter __P((struct bpf_insn *, u_char *, u_int, u_int));
220 #endif
221 
222 /*
223  * Number of scratch memory words (for BPF_LD|BPF_MEM and BPF_ST).
224  */
225 #define BPF_MEMWORDS 16
226 
227