1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1990, 1991, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 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. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 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. 23 * 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 34 * SUCH DAMAGE. 35 * 36 * @(#)bpf.h 8.1 (Berkeley) 6/10/93 37 * @(#)bpf.h 1.34 (LBL) 6/16/96 38 * 39 * $FreeBSD$ 40 */ 41 42 #ifndef _NET_BPF_H_ 43 #define _NET_BPF_H_ 44 45 #include <sys/ck.h> 46 #include <net/dlt.h> 47 48 /* BSD style release date */ 49 #define BPF_RELEASE 199606 50 51 typedef int32_t bpf_int32; 52 typedef u_int32_t bpf_u_int32; 53 typedef int64_t bpf_int64; 54 typedef u_int64_t bpf_u_int64; 55 56 /* 57 * Alignment macros. BPF_WORDALIGN rounds up to the next 58 * even multiple of BPF_ALIGNMENT. 59 */ 60 #define BPF_ALIGNMENT sizeof(long) 61 #define BPF_WORDALIGN(x) (((x)+(BPF_ALIGNMENT-1))&~(BPF_ALIGNMENT-1)) 62 63 #define BPF_MAXINSNS 512 64 #define BPF_MAXBUFSIZE 0x80000 65 #define BPF_MINBUFSIZE 32 66 67 /* 68 * Structure for BIOCSETF. 69 */ 70 struct bpf_program { 71 u_int bf_len; 72 struct bpf_insn *bf_insns; 73 }; 74 75 /* 76 * Struct returned by BIOCGSTATS. 77 */ 78 struct bpf_stat { 79 u_int bs_recv; /* number of packets received */ 80 u_int bs_drop; /* number of packets dropped */ 81 }; 82 83 /* 84 * Struct return by BIOCVERSION. This represents the version number of 85 * the filter language described by the instruction encodings below. 86 * bpf understands a program iff kernel_major == filter_major && 87 * kernel_minor >= filter_minor, that is, if the value returned by the 88 * running kernel has the same major number and a minor number equal 89 * equal to or less than the filter being downloaded. Otherwise, the 90 * results are undefined, meaning an error may be returned or packets 91 * may be accepted haphazardly. 92 * It has nothing to do with the source code version. 93 */ 94 struct bpf_version { 95 u_short bv_major; 96 u_short bv_minor; 97 }; 98 /* Current version number of filter architecture. */ 99 #define BPF_MAJOR_VERSION 1 100 #define BPF_MINOR_VERSION 1 101 102 /* 103 * Historically, BPF has supported a single buffering model, first using mbuf 104 * clusters in kernel, and later using malloc(9) buffers in kernel. We now 105 * support multiple buffering modes, which may be queried and set using 106 * BIOCGETBUFMODE and BIOCSETBUFMODE. So as to avoid handling the complexity 107 * of changing modes while sniffing packets, the mode becomes fixed once an 108 * interface has been attached to the BPF descriptor. 109 */ 110 #define BPF_BUFMODE_BUFFER 1 /* Kernel buffers with read(). */ 111 #define BPF_BUFMODE_ZBUF 2 /* Zero-copy buffers. */ 112 113 /*- 114 * Struct used by BIOCSETZBUF, BIOCROTZBUF: describes up to two zero-copy 115 * buffer as used by BPF. 116 */ 117 struct bpf_zbuf { 118 void *bz_bufa; /* Location of 'a' zero-copy buffer. */ 119 void *bz_bufb; /* Location of 'b' zero-copy buffer. */ 120 size_t bz_buflen; /* Size of zero-copy buffers. */ 121 }; 122 123 #define BIOCGBLEN _IOR('B', 102, u_int) 124 #define BIOCSBLEN _IOWR('B', 102, u_int) 125 #define BIOCSETF _IOW('B', 103, struct bpf_program) 126 #define BIOCFLUSH _IO('B', 104) 127 #define BIOCPROMISC _IO('B', 105) 128 #define BIOCGDLT _IOR('B', 106, u_int) 129 #define BIOCGETIF _IOR('B', 107, struct ifreq) 130 #define BIOCSETIF _IOW('B', 108, struct ifreq) 131 #define BIOCSRTIMEOUT _IOW('B', 109, struct timeval) 132 #define BIOCGRTIMEOUT _IOR('B', 110, struct timeval) 133 #define BIOCGSTATS _IOR('B', 111, struct bpf_stat) 134 #define BIOCIMMEDIATE _IOW('B', 112, u_int) 135 #define BIOCVERSION _IOR('B', 113, struct bpf_version) 136 #define BIOCGRSIG _IOR('B', 114, u_int) 137 #define BIOCSRSIG _IOW('B', 115, u_int) 138 #define BIOCGHDRCMPLT _IOR('B', 116, u_int) 139 #define BIOCSHDRCMPLT _IOW('B', 117, u_int) 140 #define BIOCGDIRECTION _IOR('B', 118, u_int) 141 #define BIOCSDIRECTION _IOW('B', 119, u_int) 142 #define BIOCSDLT _IOW('B', 120, u_int) 143 #define BIOCGDLTLIST _IOWR('B', 121, struct bpf_dltlist) 144 #define BIOCLOCK _IO('B', 122) 145 #define BIOCSETWF _IOW('B', 123, struct bpf_program) 146 #define BIOCFEEDBACK _IOW('B', 124, u_int) 147 #define BIOCGETBUFMODE _IOR('B', 125, u_int) 148 #define BIOCSETBUFMODE _IOW('B', 126, u_int) 149 #define BIOCGETZMAX _IOR('B', 127, size_t) 150 #define BIOCROTZBUF _IOR('B', 128, struct bpf_zbuf) 151 #define BIOCSETZBUF _IOW('B', 129, struct bpf_zbuf) 152 #define BIOCSETFNR _IOW('B', 130, struct bpf_program) 153 #define BIOCGTSTAMP _IOR('B', 131, u_int) 154 #define BIOCSTSTAMP _IOW('B', 132, u_int) 155 156 /* Obsolete */ 157 #define BIOCGSEESENT BIOCGDIRECTION 158 #define BIOCSSEESENT BIOCSDIRECTION 159 160 /* Packet directions */ 161 enum bpf_direction { 162 BPF_D_IN, /* See incoming packets */ 163 BPF_D_INOUT, /* See incoming and outgoing packets */ 164 BPF_D_OUT /* See outgoing packets */ 165 }; 166 167 /* Time stamping functions */ 168 #define BPF_T_MICROTIME 0x0000 169 #define BPF_T_NANOTIME 0x0001 170 #define BPF_T_BINTIME 0x0002 171 #define BPF_T_NONE 0x0003 172 #define BPF_T_FORMAT_MASK 0x0003 173 #define BPF_T_NORMAL 0x0000 174 #define BPF_T_FAST 0x0100 175 #define BPF_T_MONOTONIC 0x0200 176 #define BPF_T_MONOTONIC_FAST (BPF_T_FAST | BPF_T_MONOTONIC) 177 #define BPF_T_FLAG_MASK 0x0300 178 #define BPF_T_FORMAT(t) ((t) & BPF_T_FORMAT_MASK) 179 #define BPF_T_FLAG(t) ((t) & BPF_T_FLAG_MASK) 180 #define BPF_T_VALID(t) \ 181 ((t) == BPF_T_NONE || (BPF_T_FORMAT(t) != BPF_T_NONE && \ 182 ((t) & ~(BPF_T_FORMAT_MASK | BPF_T_FLAG_MASK)) == 0)) 183 184 #define BPF_T_MICROTIME_FAST (BPF_T_MICROTIME | BPF_T_FAST) 185 #define BPF_T_NANOTIME_FAST (BPF_T_NANOTIME | BPF_T_FAST) 186 #define BPF_T_BINTIME_FAST (BPF_T_BINTIME | BPF_T_FAST) 187 #define BPF_T_MICROTIME_MONOTONIC (BPF_T_MICROTIME | BPF_T_MONOTONIC) 188 #define BPF_T_NANOTIME_MONOTONIC (BPF_T_NANOTIME | BPF_T_MONOTONIC) 189 #define BPF_T_BINTIME_MONOTONIC (BPF_T_BINTIME | BPF_T_MONOTONIC) 190 #define BPF_T_MICROTIME_MONOTONIC_FAST (BPF_T_MICROTIME | BPF_T_MONOTONIC_FAST) 191 #define BPF_T_NANOTIME_MONOTONIC_FAST (BPF_T_NANOTIME | BPF_T_MONOTONIC_FAST) 192 #define BPF_T_BINTIME_MONOTONIC_FAST (BPF_T_BINTIME | BPF_T_MONOTONIC_FAST) 193 194 /* 195 * Structure prepended to each packet. 196 */ 197 struct bpf_ts { 198 bpf_int64 bt_sec; /* seconds */ 199 bpf_u_int64 bt_frac; /* fraction */ 200 }; 201 struct bpf_xhdr { 202 struct bpf_ts bh_tstamp; /* time stamp */ 203 bpf_u_int32 bh_caplen; /* length of captured portion */ 204 bpf_u_int32 bh_datalen; /* original length of packet */ 205 u_short bh_hdrlen; /* length of bpf header (this struct 206 plus alignment padding) */ 207 }; 208 /* Obsolete */ 209 struct bpf_hdr { 210 struct timeval bh_tstamp; /* time stamp */ 211 bpf_u_int32 bh_caplen; /* length of captured portion */ 212 bpf_u_int32 bh_datalen; /* original length of packet */ 213 u_short bh_hdrlen; /* length of bpf header (this struct 214 plus alignment padding) */ 215 }; 216 #ifdef _KERNEL 217 #define MTAG_BPF 0x627066 218 #define MTAG_BPF_TIMESTAMP 0 219 #endif 220 221 /* 222 * When using zero-copy BPF buffers, a shared memory header is present 223 * allowing the kernel BPF implementation and user process to synchronize 224 * without using system calls. This structure defines that header. When 225 * accessing these fields, appropriate atomic operation and memory barriers 226 * are required in order not to see stale or out-of-order data; see bpf(4) 227 * for reference code to access these fields from userspace. 228 * 229 * The layout of this structure is critical, and must not be changed; if must 230 * fit in a single page on all architectures. 231 */ 232 struct bpf_zbuf_header { 233 volatile u_int bzh_kernel_gen; /* Kernel generation number. */ 234 volatile u_int bzh_kernel_len; /* Length of data in the buffer. */ 235 volatile u_int bzh_user_gen; /* User generation number. */ 236 u_int _bzh_pad[5]; 237 }; 238 239 /* 240 * The instruction encodings. 241 * 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). 246 */ 247 248 /* 249 * The upper 8 bits of the opcode aren't used. BSD/OS used 0x8000. 250 */ 251 252 /* instruction classes */ 253 #define BPF_CLASS(code) ((code) & 0x07) 254 #define BPF_LD 0x00 255 #define BPF_LDX 0x01 256 #define BPF_ST 0x02 257 #define BPF_STX 0x03 258 #define BPF_ALU 0x04 259 #define BPF_JMP 0x05 260 #define BPF_RET 0x06 261 #define BPF_MISC 0x07 262 263 /* ld/ldx fields */ 264 #define BPF_SIZE(code) ((code) & 0x18) 265 #define BPF_W 0x00 266 #define BPF_H 0x08 267 #define BPF_B 0x10 268 /* 0x18 reserved; used by BSD/OS */ 269 #define BPF_MODE(code) ((code) & 0xe0) 270 #define BPF_IMM 0x00 271 #define BPF_ABS 0x20 272 #define BPF_IND 0x40 273 #define BPF_MEM 0x60 274 #define BPF_LEN 0x80 275 #define BPF_MSH 0xa0 276 /* 0xc0 reserved; used by BSD/OS */ 277 /* 0xe0 reserved; used by BSD/OS */ 278 279 /* alu/jmp fields */ 280 #define BPF_OP(code) ((code) & 0xf0) 281 #define BPF_ADD 0x00 282 #define BPF_SUB 0x10 283 #define BPF_MUL 0x20 284 #define BPF_DIV 0x30 285 #define BPF_OR 0x40 286 #define BPF_AND 0x50 287 #define BPF_LSH 0x60 288 #define BPF_RSH 0x70 289 #define BPF_NEG 0x80 290 #define BPF_MOD 0x90 291 #define BPF_XOR 0xa0 292 /* 0xb0 reserved */ 293 /* 0xc0 reserved */ 294 /* 0xd0 reserved */ 295 /* 0xe0 reserved */ 296 /* 0xf0 reserved */ 297 298 #define BPF_JA 0x00 299 #define BPF_JEQ 0x10 300 #define BPF_JGT 0x20 301 #define BPF_JGE 0x30 302 #define BPF_JSET 0x40 303 /* 0x50 reserved; used on BSD/OS */ 304 /* 0x60 reserved */ 305 /* 0x70 reserved */ 306 /* 0x80 reserved */ 307 /* 0x90 reserved */ 308 /* 0xa0 reserved */ 309 /* 0xb0 reserved */ 310 /* 0xc0 reserved */ 311 /* 0xd0 reserved */ 312 /* 0xe0 reserved */ 313 /* 0xf0 reserved */ 314 #define BPF_SRC(code) ((code) & 0x08) 315 #define BPF_K 0x00 316 #define BPF_X 0x08 317 318 /* ret - BPF_K and BPF_X also apply */ 319 #define BPF_RVAL(code) ((code) & 0x18) 320 #define BPF_A 0x10 321 /* 0x18 reserved */ 322 323 /* misc */ 324 #define BPF_MISCOP(code) ((code) & 0xf8) 325 #define BPF_TAX 0x00 326 /* 0x08 reserved */ 327 /* 0x10 reserved */ 328 /* 0x18 reserved */ 329 /* #define BPF_COP 0x20 NetBSD "coprocessor" extensions */ 330 /* 0x28 reserved */ 331 /* 0x30 reserved */ 332 /* 0x38 reserved */ 333 /* #define BPF_COPX 0x40 NetBSD "coprocessor" extensions */ 334 /* also used on BSD/OS */ 335 /* 0x48 reserved */ 336 /* 0x50 reserved */ 337 /* 0x58 reserved */ 338 /* 0x60 reserved */ 339 /* 0x68 reserved */ 340 /* 0x70 reserved */ 341 /* 0x78 reserved */ 342 #define BPF_TXA 0x80 343 /* 0x88 reserved */ 344 /* 0x90 reserved */ 345 /* 0x98 reserved */ 346 /* 0xa0 reserved */ 347 /* 0xa8 reserved */ 348 /* 0xb0 reserved */ 349 /* 0xb8 reserved */ 350 /* 0xc0 reserved; used on BSD/OS */ 351 /* 0xc8 reserved */ 352 /* 0xd0 reserved */ 353 /* 0xd8 reserved */ 354 /* 0xe0 reserved */ 355 /* 0xe8 reserved */ 356 /* 0xf0 reserved */ 357 /* 0xf8 reserved */ 358 359 /* 360 * The instruction data structure. 361 */ 362 struct bpf_insn { 363 u_short code; 364 u_char jt; 365 u_char jf; 366 bpf_u_int32 k; 367 }; 368 369 /* 370 * Macros for insn array initializers. 371 */ 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 } 374 375 /* 376 * Structure to retrieve available DLTs for the interface. 377 */ 378 struct bpf_dltlist { 379 u_int bfl_len; /* number of bfd_list array */ 380 u_int *bfl_list; /* array of DLTs */ 381 }; 382 383 #ifdef _KERNEL 384 #ifdef MALLOC_DECLARE 385 MALLOC_DECLARE(M_BPF); 386 #endif 387 #ifdef SYSCTL_DECL 388 SYSCTL_DECL(_net_bpf); 389 #endif 390 391 /* 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. 396 */ 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; \ 401 (d)->bd_slen = 0; \ 402 (d)->bd_fbuf = NULL; \ 403 bpf_bufheld(d); \ 404 } while (0) 405 406 /* 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. 410 */ 411 struct bpf_if; 412 CK_LIST_HEAD(bpfd_list, bpf_d); 413 414 struct bpf_if_ext { 415 CK_LIST_ENTRY(bpf_if) bif_next; /* list of all interfaces */ 416 struct bpfd_list bif_dlist; /* descriptor list */ 417 }; 418 419 void bpf_bufheld(struct bpf_d *d); 420 int bpf_validate(const struct bpf_insn *, int); 421 void bpf_tap(struct bpf_if *, u_char *, u_int); 422 void bpf_mtap(struct bpf_if *, struct mbuf *); 423 void bpf_mtap2(struct bpf_if *, void *, u_int, struct mbuf *); 424 void bpfattach(struct ifnet *, u_int, u_int); 425 void bpfattach2(struct ifnet *, u_int, u_int, struct bpf_if **); 426 void bpfdetach(struct ifnet *); 427 #ifdef VIMAGE 428 int bpf_get_bp_params(struct bpf_if *, u_int *, u_int *); 429 #endif 430 431 void bpfilterattach(int); 432 u_int bpf_filter(const struct bpf_insn *, u_char *, u_int, u_int); 433 434 static __inline int 435 bpf_peers_present(struct bpf_if *bpf) 436 { 437 struct bpf_if_ext *ext; 438 439 ext = (struct bpf_if_ext *)bpf; 440 if (!CK_LIST_EMPTY(&ext->bif_dlist)) 441 return (1); 442 return (0); 443 } 444 445 #define BPF_TAP(_ifp,_pkt,_pktlen) do { \ 446 if (bpf_peers_present((_ifp)->if_bpf)) \ 447 bpf_tap((_ifp)->if_bpf, (_pkt), (_pktlen)); \ 448 } while (0) 449 #define BPF_MTAP(_ifp,_m) do { \ 450 if (bpf_peers_present((_ifp)->if_bpf)) { \ 451 M_ASSERTVALID(_m); \ 452 bpf_mtap((_ifp)->if_bpf, (_m)); \ 453 } \ 454 } while (0) 455 #define BPF_MTAP2(_ifp,_data,_dlen,_m) do { \ 456 if (bpf_peers_present((_ifp)->if_bpf)) { \ 457 M_ASSERTVALID(_m); \ 458 bpf_mtap2((_ifp)->if_bpf,(_data),(_dlen),(_m)); \ 459 } \ 460 } while (0) 461 #endif 462 463 /* 464 * Number of scratch memory words (for BPF_LD|BPF_MEM and BPF_ST). 465 */ 466 #define BPF_MEMWORDS 16 467 468 #ifdef _SYS_EVENTHANDLER_H_ 469 /* BPF attach/detach events */ 470 struct ifnet; 471 typedef void (*bpf_track_fn)(void *, struct ifnet *, int /* dlt */, 472 int /* 1 =>'s attach */); 473 EVENTHANDLER_DECLARE(bpf_track, bpf_track_fn); 474 #endif /* _SYS_EVENTHANDLER_H_ */ 475 476 #endif /* _NET_BPF_H_ */ 477