2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1990, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from the Stanford/CMU enet packet filter,
8 * (net/enet.c) distributed as part of 4.3BSD, and code contributed
9 * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
10 * Berkeley Laboratory.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)bpf.h 8.1 (Berkeley) 6/10/93
37 * @(#)bpf.h 1.34 (LBL) 6/16/96
45 /* BSD style release date */
46 #define BPF_RELEASE 199606
48 typedef int32_t bpf_int32;
49 typedef u_int32_t bpf_u_int32;
50 typedef int64_t bpf_int64;
51 typedef u_int64_t bpf_u_int64;
54 * Alignment macros. BPF_WORDALIGN rounds up to the next
55 * even multiple of BPF_ALIGNMENT.
57 #define BPF_ALIGNMENT sizeof(long)
58 #define BPF_WORDALIGN(x) (((x)+(BPF_ALIGNMENT-1))&~(BPF_ALIGNMENT-1))
60 #define BPF_MAXINSNS 512
61 #define BPF_MAXBUFSIZE 0x80000
62 #define BPF_MINBUFSIZE 32
65 * Structure for BIOCSETF.
69 struct bpf_insn *bf_insns;
73 * Struct returned by BIOCGSTATS.
76 u_int bs_recv; /* number of packets received */
77 u_int bs_drop; /* number of packets dropped */
81 * Struct return by BIOCVERSION. This represents the version number of
82 * the filter language described by the instruction encodings below.
83 * bpf understands a program iff kernel_major == filter_major &&
84 * kernel_minor >= filter_minor, that is, if the value returned by the
85 * running kernel has the same major number and a minor number equal
86 * equal to or less than the filter being downloaded. Otherwise, the
87 * results are undefined, meaning an error may be returned or packets
88 * may be accepted haphazardly.
89 * It has nothing to do with the source code version.
95 /* Current version number of filter architecture. */
96 #define BPF_MAJOR_VERSION 1
97 #define BPF_MINOR_VERSION 1
100 * Historically, BPF has supported a single buffering model, first using mbuf
101 * clusters in kernel, and later using malloc(9) buffers in kernel. We now
102 * support multiple buffering modes, which may be queried and set using
103 * BIOCGETBUFMODE and BIOCSETBUFMODE. So as to avoid handling the complexity
104 * of changing modes while sniffing packets, the mode becomes fixed once an
105 * interface has been attached to the BPF descriptor.
107 #define BPF_BUFMODE_BUFFER 1 /* Kernel buffers with read(). */
108 #define BPF_BUFMODE_ZBUF 2 /* Zero-copy buffers. */
111 * Struct used by BIOCSETZBUF, BIOCROTZBUF: describes up to two zero-copy
112 * buffer as used by BPF.
115 void *bz_bufa; /* Location of 'a' zero-copy buffer. */
116 void *bz_bufb; /* Location of 'b' zero-copy buffer. */
117 size_t bz_buflen; /* Size of zero-copy buffers. */
120 #define BIOCGBLEN _IOR('B', 102, u_int)
121 #define BIOCSBLEN _IOWR('B', 102, u_int)
122 #define BIOCSETF _IOW('B', 103, struct bpf_program)
123 #define BIOCFLUSH _IO('B', 104)
124 #define BIOCPROMISC _IO('B', 105)
125 #define BIOCGDLT _IOR('B', 106, u_int)
126 #define BIOCGETIF _IOR('B', 107, struct ifreq)
127 #define BIOCSETIF _IOW('B', 108, struct ifreq)
128 #define BIOCSRTIMEOUT _IOW('B', 109, struct timeval)
129 #define BIOCGRTIMEOUT _IOR('B', 110, struct timeval)
130 #define BIOCGSTATS _IOR('B', 111, struct bpf_stat)
131 #define BIOCIMMEDIATE _IOW('B', 112, u_int)
132 #define BIOCVERSION _IOR('B', 113, struct bpf_version)
133 #define BIOCGRSIG _IOR('B', 114, u_int)
134 #define BIOCSRSIG _IOW('B', 115, u_int)
135 #define BIOCGHDRCMPLT _IOR('B', 116, u_int)
136 #define BIOCSHDRCMPLT _IOW('B', 117, u_int)
137 #define BIOCGDIRECTION _IOR('B', 118, u_int)
138 #define BIOCSDIRECTION _IOW('B', 119, u_int)
139 #define BIOCSDLT _IOW('B', 120, u_int)
140 #define BIOCGDLTLIST _IOWR('B', 121, struct bpf_dltlist)
141 #define BIOCLOCK _IO('B', 122)
142 #define BIOCSETWF _IOW('B', 123, struct bpf_program)
143 #define BIOCFEEDBACK _IOW('B', 124, u_int)
144 #define BIOCGETBUFMODE _IOR('B', 125, u_int)
145 #define BIOCSETBUFMODE _IOW('B', 126, u_int)
146 #define BIOCGETZMAX _IOR('B', 127, size_t)
147 #define BIOCROTZBUF _IOR('B', 128, struct bpf_zbuf)
148 #define BIOCSETZBUF _IOW('B', 129, struct bpf_zbuf)
149 #define BIOCSETFNR _IOW('B', 130, struct bpf_program)
150 #define BIOCGTSTAMP _IOR('B', 131, u_int)
151 #define BIOCSTSTAMP _IOW('B', 132, u_int)
154 #define BIOCGSEESENT BIOCGDIRECTION
155 #define BIOCSSEESENT BIOCSDIRECTION
157 /* Packet directions */
159 BPF_D_IN, /* See incoming packets */
160 BPF_D_INOUT, /* See incoming and outgoing packets */
161 BPF_D_OUT /* See outgoing packets */
164 /* Time stamping functions */
165 #define BPF_T_MICROTIME 0x0000
166 #define BPF_T_NANOTIME 0x0001
167 #define BPF_T_BINTIME 0x0002
168 #define BPF_T_NONE 0x0003
169 #define BPF_T_FORMAT_MASK 0x0003
170 #define BPF_T_NORMAL 0x0000
171 #define BPF_T_FAST 0x0100
172 #define BPF_T_MONOTONIC 0x0200
173 #define BPF_T_MONOTONIC_FAST (BPF_T_FAST | BPF_T_MONOTONIC)
174 #define BPF_T_FLAG_MASK 0x0300
175 #define BPF_T_FORMAT(t) ((t) & BPF_T_FORMAT_MASK)
176 #define BPF_T_FLAG(t) ((t) & BPF_T_FLAG_MASK)
177 #define BPF_T_VALID(t) \
178 ((t) == BPF_T_NONE || (BPF_T_FORMAT(t) != BPF_T_NONE && \
179 ((t) & ~(BPF_T_FORMAT_MASK | BPF_T_FLAG_MASK)) == 0))
181 #define BPF_T_MICROTIME_FAST (BPF_T_MICROTIME | BPF_T_FAST)
182 #define BPF_T_NANOTIME_FAST (BPF_T_NANOTIME | BPF_T_FAST)
183 #define BPF_T_BINTIME_FAST (BPF_T_BINTIME | BPF_T_FAST)
184 #define BPF_T_MICROTIME_MONOTONIC (BPF_T_MICROTIME | BPF_T_MONOTONIC)
185 #define BPF_T_NANOTIME_MONOTONIC (BPF_T_NANOTIME | BPF_T_MONOTONIC)
186 #define BPF_T_BINTIME_MONOTONIC (BPF_T_BINTIME | BPF_T_MONOTONIC)
187 #define BPF_T_MICROTIME_MONOTONIC_FAST (BPF_T_MICROTIME | BPF_T_MONOTONIC_FAST)
188 #define BPF_T_NANOTIME_MONOTONIC_FAST (BPF_T_NANOTIME | BPF_T_MONOTONIC_FAST)
189 #define BPF_T_BINTIME_MONOTONIC_FAST (BPF_T_BINTIME | BPF_T_MONOTONIC_FAST)
192 * Structure prepended to each packet.
195 bpf_int64 bt_sec; /* seconds */
196 bpf_u_int64 bt_frac; /* fraction */
199 struct bpf_ts bh_tstamp; /* time stamp */
200 bpf_u_int32 bh_caplen; /* length of captured portion */
201 bpf_u_int32 bh_datalen; /* original length of packet */
202 u_short bh_hdrlen; /* length of bpf header (this struct
203 plus alignment padding) */
207 struct timeval bh_tstamp; /* time stamp */
208 bpf_u_int32 bh_caplen; /* length of captured portion */
209 bpf_u_int32 bh_datalen; /* original length of packet */
210 u_short bh_hdrlen; /* length of bpf header (this struct
211 plus alignment padding) */
214 #define MTAG_BPF 0x627066
215 #define MTAG_BPF_TIMESTAMP 0
219 * When using zero-copy BPF buffers, a shared memory header is present
220 * allowing the kernel BPF implementation and user process to synchronize
221 * without using system calls. This structure defines that header. When
222 * accessing these fields, appropriate atomic operation and memory barriers
223 * are required in order not to see stale or out-of-order data; see bpf(4)
224 * for reference code to access these fields from userspace.
226 * The layout of this structure is critical, and must not be changed; if must
227 * fit in a single page on all architectures.
229 struct bpf_zbuf_header {
230 volatile u_int bzh_kernel_gen; /* Kernel generation number. */
231 volatile u_int bzh_kernel_len; /* Length of data in the buffer. */
232 volatile u_int bzh_user_gen; /* User generation number. */
236 /* Pull in data-link level type codes. */
240 * The instruction encodings.
242 * Please inform tcpdump-workers@lists.tcpdump.org if you use any
243 * of the reserved values, so that we can note that they're used
244 * (and perhaps implement it in the reference BPF implementation
245 * and encourage its implementation elsewhere).
249 * The upper 8 bits of the opcode aren't used. BSD/OS used 0x8000.
252 /* instruction classes */
253 #define BPF_CLASS(code) ((code) & 0x07)
261 #define BPF_MISC 0x07
264 #define BPF_SIZE(code) ((code) & 0x18)
268 /* 0x18 reserved; used by BSD/OS */
269 #define BPF_MODE(code) ((code) & 0xe0)
276 /* 0xc0 reserved; used by BSD/OS */
277 /* 0xe0 reserved; used by BSD/OS */
280 #define BPF_OP(code) ((code) & 0xf0)
302 #define BPF_JSET 0x40
303 /* 0x50 reserved; used on BSD/OS */
314 #define BPF_SRC(code) ((code) & 0x08)
318 /* ret - BPF_K and BPF_X also apply */
319 #define BPF_RVAL(code) ((code) & 0x18)
324 #define BPF_MISCOP(code) ((code) & 0xf8)
329 /* #define BPF_COP 0x20 NetBSD "coprocessor" extensions */
333 /* #define BPF_COPX 0x40 NetBSD "coprocessor" extensions */
334 /* also used on BSD/OS */
350 /* 0xc0 reserved; used on BSD/OS */
360 * The instruction data structure.
370 * Macros for insn array initializers.
372 #define BPF_STMT(code, k) { (u_short)(code), 0, 0, k }
373 #define BPF_JUMP(code, k, jt, jf) { (u_short)(code), jt, jf, k }
376 * Structure to retrieve available DLTs for the interface.
379 u_int bfl_len; /* number of bfd_list array */
380 u_int *bfl_list; /* array of DLTs */
384 #ifdef MALLOC_DECLARE
385 MALLOC_DECLARE(M_BPF);
388 SYSCTL_DECL(_net_bpf);
392 * Rotate the packet buffers in descriptor d. Move the store buffer into the
393 * hold slot, and the free buffer into the store slot. Zero the length of the
394 * new store buffer. Descriptor lock should be held. One must be careful to
395 * not rotate the buffers twice, i.e. if fbuf != NULL.
397 #define ROTATE_BUFFERS(d) do { \
398 (d)->bd_hbuf = (d)->bd_sbuf; \
399 (d)->bd_hlen = (d)->bd_slen; \
400 (d)->bd_sbuf = (d)->bd_fbuf; \
402 (d)->bd_fbuf = NULL; \
407 * Descriptor associated with each attached hardware interface.
408 * Part of this structure is exposed to external callers to speed up
409 * bpf_peers_present() calls.
414 LIST_ENTRY(bpf_if) bif_next; /* list of all interfaces */
415 LIST_HEAD(, bpf_d) bif_dlist; /* descriptor list */
418 void bpf_bufheld(struct bpf_d *d);
419 int bpf_validate(const struct bpf_insn *, int);
420 void bpf_tap(struct bpf_if *, u_char *, u_int);
421 void bpf_mtap(struct bpf_if *, struct mbuf *);
422 void bpf_mtap2(struct bpf_if *, void *, u_int, struct mbuf *);
423 void bpfattach(struct ifnet *, u_int, u_int);
424 void bpfattach2(struct ifnet *, u_int, u_int, struct bpf_if **);
425 void bpfdetach(struct ifnet *);
427 int bpf_get_bp_params(struct bpf_if *, u_int *, u_int *);
430 void bpfilterattach(int);
431 u_int bpf_filter(const struct bpf_insn *, u_char *, u_int, u_int);
434 bpf_peers_present(struct bpf_if *bpf)
436 struct bpf_if_ext *ext;
438 ext = (struct bpf_if_ext *)bpf;
439 if (!LIST_EMPTY(&ext->bif_dlist))
444 #define BPF_TAP(_ifp,_pkt,_pktlen) do { \
445 if (bpf_peers_present((_ifp)->if_bpf)) \
446 bpf_tap((_ifp)->if_bpf, (_pkt), (_pktlen)); \
448 #define BPF_MTAP(_ifp,_m) do { \
449 if (bpf_peers_present((_ifp)->if_bpf)) { \
451 bpf_mtap((_ifp)->if_bpf, (_m)); \
454 #define BPF_MTAP2(_ifp,_data,_dlen,_m) do { \
455 if (bpf_peers_present((_ifp)->if_bpf)) { \
457 bpf_mtap2((_ifp)->if_bpf,(_data),(_dlen),(_m)); \
463 * Number of scratch memory words (for BPF_LD|BPF_MEM and BPF_ST).
465 #define BPF_MEMWORDS 16
467 #ifdef _SYS_EVENTHANDLER_H_
468 /* BPF attach/detach events */
470 typedef void (*bpf_track_fn)(void *, struct ifnet *, int /* dlt */,
471 int /* 1 =>'s attach */);
472 EVENTHANDLER_DECLARE(bpf_track, bpf_track_fn);
473 #endif /* _SYS_EVENTHANDLER_H_ */
475 #endif /* _NET_BPF_H_ */