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