xref: /original-bsd/sys/net/bpf.h (revision 8fbb78b3)
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * 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 of Lawrence Berkeley Laboratory.
8  *
9  * %sccs.include.redist.c%
10  *
11  *	@(#)bpf.h	7.1 (Berkeley) 05/07/91
12  *
13  * @(#) $Header: bpf.h,v 1.20 91/04/24 22:06:24 mccanne Locked $ (LBL)
14  */
15 
16 /*
17  * Alignment macros.  BPF_WORDALIGN rounds up to the next
18  * even multiple of BPF_ALIGNMENT.
19  */
20 #define BPF_ALIGNMENT sizeof(long)
21 #define BPF_WORDALIGN(x) (((x)+(BPF_ALIGNMENT-1))&~(BPF_ALIGNMENT-1))
22 
23 #define BPF_MAXINSNS 512
24 #define BPF_MAXBUFSIZE 0x8000
25 
26 /*
27  *  Structure for BIOCSETF.
28  */
29 struct bpf_program {
30 	u_int bf_len;
31 	struct bpf_insn *bf_insns;
32 };
33 
34 /*
35  * Struct returned by BIOCGSTATS.
36  */
37 struct bpf_stat {
38 	u_int bs_recv;		/* number of packets received */
39 	u_int bs_drop;		/* number of packets dropped */
40 };
41 
42 /*
43  * BPF ioctls
44  *
45  * The first set is for compatibility with Sun's pcc style
46  * header files.  If your using gcc, we assume that you
47  * have run fixincludes so the latter set should work.
48  */
49 #if defined(sun) && !defined(__GNUC__)
50 #define	BIOCGFLEN	_IOR(B,101, u_int)
51 #define	BIOCGBLEN	_IOR(B,102, u_int)
52 #define	BIOCSETF	_IOW(B,103, struct bpf_program)
53 #define	BIOCFLUSH	_IO(B,104)
54 #define BIOCPROMISC	_IO(B,105)
55 #define	BIOCGDLT	_IOR(B,106, u_int)
56 #define BIOCGETIF	_IOR(B,107, struct ifreq)
57 #define BIOCSETIF	_IOW(B,108, struct ifreq)
58 #define BIOCSRTIMEOUT	_IOW(B,109, struct timeval)
59 #define BIOCGRTIMEOUT	_IOR(B,110, struct timeval)
60 #define BIOCGSTATS	_IOR(B,111, struct bpf_stat)
61 #define BIOCIMMEDIATE	_IOW(B,112, u_int)
62 #else
63 #define	BIOCGFLEN	_IOR('B',101, u_int)
64 #define	BIOCGBLEN	_IOR('B',102, u_int)
65 #define	BIOCSETF	_IOW('B',103, struct bpf_program)
66 #define	BIOCFLUSH	_IO('B',104)
67 #define BIOCPROMISC	_IO('B',105)
68 #define	BIOCGDLT	_IOR('B',106, u_int)
69 #define BIOCGETIF	_IOR('B',107, struct ifreq)
70 #define BIOCSETIF	_IOW('B',108, struct ifreq)
71 #define BIOCSRTIMEOUT	_IOW('B',109, struct timeval)
72 #define BIOCGRTIMEOUT	_IOR('B',110, struct timeval)
73 #define BIOCGSTATS	_IOR('B',111, struct bpf_stat)
74 #define BIOCIMMEDIATE	_IOW('B',112, u_int)
75 #endif
76 
77 /*
78  * Structure prepended to each packet.
79  */
80 struct bpf_hdr {
81 	struct timeval	bh_tstamp;	/* time stamp */
82 	u_long		bh_caplen;	/* length of captured portion */
83 	u_long		bh_datalen;	/* original length of packet */
84 	u_short		bh_hdrlen;	/* length of bpf header (this struct
85 					   plus alignment padding) */
86 };
87 /*
88  * Because the structure above is not a multiple of 4 bytes, some compilers
89  * will insist on inserting padding; hence, sizeof(struct bpf_hdr) won't work.
90  * Only the kernel needs to know about it; applications use bh_hdrlen.
91  */
92 #ifdef KERNEL
93 #define SIZEOF_BPF_HDR 18
94 #endif
95 
96 /*
97  * Data-link level type codes.
98  * Currently, only DLT_EN10MB and DLT_SLIP are supported.
99  */
100 #define DLT_EN10MB	1	/* Ethernet (10Mb) */
101 #define DLT_EN3MB	2	/* Experimental Ethernet (3Mb) */
102 #define DLT_AX25	3	/* Amateur Radio AX.25 */
103 #define DLT_PRONET	4	/* Proteon ProNET Token Ring */
104 #define DLT_CHAOS	5	/* Chaos */
105 #define DLT_IEEE802	6	/* IEEE 802 Networks */
106 #define DLT_ARCNET	7	/* ARCNET */
107 #define DLT_SLIP	8	/* Serial Line IP */
108 #define DLT_PPP		9	/* Point-to-point Protocol */
109 #define DLT_FDDI	10	/* FDDI */
110 
111 /*
112  * The instruction encondings.
113  */
114 /* classes <2:0> */
115 #define BPF_CLASS(code) ((code) & 0x07)
116 #define		BPF_LD		0x00
117 #define		BPF_LDX		0x01
118 #define		BPF_ST		0x02
119 #define		BPF_STX		0x03
120 #define		BPF_ALU		0x04
121 #define		BPF_JMP		0x05
122 #define		BPF_RET		0x06
123 #define		BPF_MISC	0x07
124 
125 /* ld/ldx fields */
126 #define BPF_SIZE(code)	((code) & 0x18)
127 #define		BPF_W		0x00
128 #define		BPF_H		0x08
129 #define		BPF_B		0x10
130 #define BPF_MODE(code)	((code) & 0xe0)
131 #define		BPF_IMM 	0x00
132 #define		BPF_ABS		0x20
133 #define		BPF_IND		0x40
134 #define		BPF_MEM		0x60
135 #define		BPF_LEN		0x80
136 #define		BPF_MSH		0xa0
137 
138 /* alu/jmp fields */
139 #define BPF_OP(code)	((code) & 0xf0)
140 #define		BPF_ADD		0x00
141 #define		BPF_SUB		0x10
142 #define		BPF_MUL		0x20
143 #define		BPF_DIV		0x30
144 #define		BPF_OR		0x40
145 #define		BPF_AND		0x50
146 #define		BPF_LSH		0x60
147 #define		BPF_RSH		0x70
148 #define		BPF_NEG		0x80
149 #define		BPF_JA		0x00
150 #define		BPF_JEQ		0x10
151 #define		BPF_JGT		0x20
152 #define		BPF_JGE		0x30
153 #define		BPF_JSET	0x40
154 #define BPF_SRC(code)	((code) & 0x08)
155 #define		BPF_K		0x00
156 #define		BPF_X		0x08
157 
158 /* ret - BPF_K and BPF_X also apply */
159 #define BPF_RVAL(code)	((code) & 0x18)
160 #define		BPF_A		0x10
161 
162 /* misc */
163 #define BPF_MISCOP(code) ((code) & 0xf8)
164 #define		BPF_TAX		0x00
165 #define		BPF_TXA		0x80
166 
167 /*
168  * The instruction data structure.
169  */
170 struct bpf_insn {
171 	u_short	code;
172 	u_char 	jt;
173 	u_char 	jf;
174 	long	k;
175 };
176 
177 /*
178  * Macros for insn array initializers.
179  */
180 #define BPF_STMT(code, k) { (u_short)(code), 0, 0, k }
181 #define BPF_JUMP(code, k, jt, jf) { (u_short)(code), jt, jf, k }
182 
183 #ifdef KERNEL
184 extern u_int bpf_filter();
185 extern void bpfattach();
186 extern void bpf_tap();
187 extern void bpf_mtap();
188 #endif
189 
190 /*
191  * Number of scratch memory words (for BPF_LD|BPF_MEM and BPF_ST).
192  */
193 #define BPF_MEMWORDS 16
194