FreeBSD/Linux Kernel Cross Reference
sys/device/bpf.h
1 /*
2 * Mach Operating System
3 * Copyright (c) 1993 Carnegie Mellon University
4 * All Rights Reserved.
5 *
6 * Permission to use, copy, modify and distribute this software and its
7 * documentation is hereby granted, provided that both the copyright
8 * notice and this permission notice appear in all copies of the
9 * software, derivative works or modified versions, and any portions
10 * thereof, and that both notices appear in supporting documentation.
11 *
12 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
13 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
15 *
16 * Carnegie Mellon requests users of this software to return to
17 *
18 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
19 * School of Computer Science
20 * Carnegie Mellon University
21 * Pittsburgh PA 15213-3890
22 *
23 * any improvements or extensions that they make and grant Carnegie Mellon
24 * the rights to redistribute these changes.
25 */
26 /*
27 * HISTORY
28 * $Log: bpf.h,v $
29 * Revision 2.2 93/08/10 15:11:16 mrt
30 * 93/02/08 11:28 Masanobu Yuhara at Fujitsu Labs.
31 * Added BPF support. Derived from tcpdump-2.2.1/bpf/net/bpf.h.
32 * Add: New BPF instructions and macros:
33 * BPF_MATCH_IMM, BPF_CKMATCH_IMM, BPF_MATCH_DATA, BPF_REG_DATA,
34 * BPF_POSTPONE, NETF_BPF, BPF_BEGIN, BPF_INSN_STMT, BPF_INSN_JUMP.
35 * Add: BPF_DLBASE for accessing a data link level header.
36 * Changed: BPF_RVAL mask value (0x18 -> 0x38)
37 * [93/02/08 11:28:23 yuhara]
38 *
39 */
40
41 /*
42 * Berkeley Packet Filter Definitions from Berkeley
43 */
44
45 /*-
46 * Copyright (c) 1990-1991 The Regents of the University of California.
47 * All rights reserved.
48 *
49 * This code is derived from the Stanford/CMU enet packet filter,
50 * (net/enet.c) distributed as part of 4.3BSD, and code contributed
51 * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
52 * Berkeley Laboratory.
53 *
54 * Redistribution and use in source and binary forms, with or without
55 * modification, are permitted provided that the following conditions
56 * are met:
57 * 1. Redistributions of source code must retain the above copyright
58 * notice, this list of conditions and the following disclaimer.
59 * 2. Redistributions in binary form must reproduce the above copyright
60 * notice, this list of conditions and the following disclaimer in the
61 * documentation and/or other materials provided with the distribution.
62 * 3. All advertising materials mentioning features or use of this software
63 * must display the following acknowledgement:
64 * This product includes software developed by the University of
65 * California, Berkeley and its contributors.
66 * 4. Neither the name of the University nor the names of its contributors
67 * may be used to endorse or promote products derived from this software
68 * without specific prior written permission.
69 *
70 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
71 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
72 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
73 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
74 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
75 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
76 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
77 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
78 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
79 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
80 * SUCH DAMAGE.
81 *
82 * @(#)bpf.h 7.1 (Berkeley) 5/7/91
83 *
84 * @(#) $Header: bpf.h,v 2.2 93/08/10 15:11:16 mrt Exp $ (LBL)
85 */
86
87 #ifndef _DEVICE_BPF_H_
88 #define _DEVICE_BPF_H_
89
90 #if 0 /* not used in MK now */
91 /*
92 * Alignment macros. BPF_WORDALIGN rounds up to the next
93 * even multiple of BPF_ALIGNMENT.
94 */
95 #define BPF_ALIGNMENT sizeof(long)
96 #define BPF_WORDALIGN(x) (((x)+(BPF_ALIGNMENT-1))&~(BPF_ALIGNMENT-1))
97
98 /*
99 * Struct return by BIOCVERSION. This represents the version number of
100 * the filter language described by the instruction encodings below.
101 * bpf understands a program iff kernel_major == filter_major &&
102 * kernel_minor >= filter_minor, that is, if the value returned by the
103 * running kernel has the same major number and a minor number equal
104 * equal to or less than the filter being downloaded. Otherwise, the
105 * results are undefined, meaning an error may be returned or packets
106 * may be accepted haphazardly.
107 * It has nothing to do with the source code version.
108 */
109 struct bpf_version {
110 u_short bv_major;
111 u_short bv_minor;
112 };
113 /* Current version number. */
114 #define BPF_MAJOR_VERSION 1
115 #define BPF_MINOR_VERSION 1
116
117 /*
118 * Data-link level type codes.
119 * Currently, only DLT_EN10MB and DLT_SLIP are supported.
120 */
121 #define DLT_NULL 0 /* no link-layer encapsulation */
122 #define DLT_EN10MB 1 /* Ethernet (10Mb) */
123 #define DLT_EN3MB 2 /* Experimental Ethernet (3Mb) */
124 #define DLT_AX25 3 /* Amateur Radio AX.25 */
125 #define DLT_PRONET 4 /* Proteon ProNET Token Ring */
126 #define DLT_CHAOS 5 /* Chaos */
127 #define DLT_IEEE802 6 /* IEEE 802 Networks */
128 #define DLT_ARCNET 7 /* ARCNET */
129 #define DLT_SLIP 8 /* Serial Line IP */
130 #define DLT_PPP 9 /* Point-to-point Protocol */
131 #define DLT_FDDI 10 /* FDDI */
132
133 #endif /* 0 */
134
135 /*
136 * The instruction encondings.
137 */
138
139 /* Magic number for the first instruction */
140 #define BPF_BEGIN NETF_BPF
141
142 /* instruction classes */
143 #define BPF_CLASS(code) ((code) & 0x07)
144 #define BPF_LD 0x00
145 #define BPF_LDX 0x01
146 #define BPF_ST 0x02
147 #define BPF_STX 0x03
148 #define BPF_ALU 0x04
149 #define BPF_JMP 0x05
150 #define BPF_RET 0x06
151 #define BPF_MISC 0x07
152
153 /* ld/ldx fields */
154 #define BPF_SIZE(code) ((code) & 0x18)
155 #define BPF_W 0x00
156 #define BPF_H 0x08
157 #define BPF_B 0x10
158 #define BPF_MODE(code) ((code) & 0xe0)
159 #define BPF_IMM 0x00
160 #define BPF_ABS 0x20
161 #define BPF_IND 0x40
162 #define BPF_MEM 0x60
163 #define BPF_LEN 0x80
164 #define BPF_MSH 0xa0
165
166 /* alu/jmp fields */
167 #define BPF_OP(code) ((code) & 0xf0)
168 #define BPF_ADD 0x00
169 #define BPF_SUB 0x10
170 #define BPF_MUL 0x20
171 #define BPF_DIV 0x30
172 #define BPF_OR 0x40
173 #define BPF_AND 0x50
174 #define BPF_LSH 0x60
175 #define BPF_RSH 0x70
176 #define BPF_NEG 0x80
177 #define BPF_JA 0x00
178 #define BPF_JEQ 0x10
179 #define BPF_JGT 0x20
180 #define BPF_JGE 0x30
181 #define BPF_JSET 0x40
182 #define BPF_CKMATCH_IMM 0x50
183 #define BPF_SRC(code) ((code) & 0x08)
184 #define BPF_K 0x00
185 #define BPF_X 0x08
186
187 /* ret - BPF_K and BPF_X also apply */
188 #define BPF_RVAL(code) ((code) & 0x38)
189 #define BPF_A 0x10
190 #define BPF_MATCH_IMM 0x18
191 #define BPF_MATCH_DATA 0x20
192
193 /* misc */
194 #define BPF_MISCOP(code) ((code) & 0xf8)
195 #define BPF_TAX 0x00
196 #define BPF_TXA 0x80
197 #define BPF_KEY 0x10
198 #define BPF_REG_DATA 0x18
199 #define BPF_POSTPONE 0x20
200
201 /*
202 * The instruction data structure.
203 */
204 struct bpf_insn {
205 unsigned short code;
206 unsigned char jt;
207 unsigned char jf;
208 long k;
209 };
210 typedef struct bpf_insn *bpf_insn_t;
211
212 /*
213 * largest bpf program size
214 */
215 #define NET_MAX_BPF ((NET_MAX_FILTER*sizeof(filter_t))/sizeof(struct bpf_insn))
216
217 /*
218 * Macros for insn array initializers.
219 */
220 #define BPF_STMT(code, k) { (unsigned short)(code), 0, 0, k }
221 #define BPF_JUMP(code, k, jt, jf) { (unsigned short)(code), jt, jf, k }
222 #define BPF_RETMATCH(code, k, nkey) { (unsigned short)(code), nkey, 0, k }
223
224 #define BPF_INSN_STMT(pc, c, n) {\
225 (pc)->code = (c); \
226 (pc)->jt = (pc)->jf = 0; \
227 (pc)->k = (n); \
228 (pc)++; \
229 }
230
231 #define BPF_INSN_JUMP(pc, c, n, jtrue, jfalse) {\
232 (pc)->code = (c); \
233 (pc)->jt = (jtrue); \
234 (pc)->jf = (jfalse); \
235 (pc)->k = (n); \
236 (pc)++; \
237 }
238
239 #define BPF_INSN_RETMATCH(pc, c, n, nkey) {\
240 (pc)->code = (c); \
241 (pc)->jt = (nkey); \
242 (pc)->jf = 0; \
243 (pc)->k = (n); \
244 (pc)++; \
245 }
246
247 /*
248 * Number of scratch memory words (for BPF_LD|BPF_MEM and BPF_ST).
249 */
250 #define BPF_MEMWORDS 16
251
252 /*
253 * Link level header can be accessed by adding BPF_DLBASE to an offset.
254 */
255 #define BPF_DLBASE (1<<30)
256
257 #define BPF_BYTES(n) ((n) * sizeof (struct bpf_insn))
258 #define BPF_BYTES2LEN(n) ((n) / sizeof (struct bpf_insn))
259 #define BPF_INSN_EQ(p,q) ((p)->code == (q)->code && \
260 (p)->jt == (q)->jt && \
261 (p)->jf == (q)->jf && \
262 (p)->k == (q)->k)
263
264 #endif /* _DEVICE_BPF_H_ */
Cache object: 2cf888d52d318a9815c2b7bf400c68ee
|