1*d5dfe396Schristos /* $NetBSD: bpf.h,v 1.10 2019/10/01 16:02:12 christos Exp $ */ 26a19824dSchristos 354a6ec8aSchristos /*- 454a6ec8aSchristos * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997 554a6ec8aSchristos * The Regents of the University of California. All rights reserved. 654a6ec8aSchristos * 754a6ec8aSchristos * This code is derived from the Stanford/CMU enet packet filter, 854a6ec8aSchristos * (net/enet.c) distributed as part of 4.3BSD, and code contributed 954a6ec8aSchristos * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence 1054a6ec8aSchristos * Berkeley Laboratory. 1154a6ec8aSchristos * 1254a6ec8aSchristos * Redistribution and use in source and binary forms, with or without 1354a6ec8aSchristos * modification, are permitted provided that the following conditions 1454a6ec8aSchristos * are met: 1554a6ec8aSchristos * 1. Redistributions of source code must retain the above copyright 1654a6ec8aSchristos * notice, this list of conditions and the following disclaimer. 1754a6ec8aSchristos * 2. Redistributions in binary form must reproduce the above copyright 1854a6ec8aSchristos * notice, this list of conditions and the following disclaimer in the 1954a6ec8aSchristos * documentation and/or other materials provided with the distribution. 20e1375535Schristos * 3. Neither the name of the University nor the names of its contributors 2154a6ec8aSchristos * may be used to endorse or promote products derived from this software 2254a6ec8aSchristos * without specific prior written permission. 2354a6ec8aSchristos * 2454a6ec8aSchristos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2554a6ec8aSchristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2654a6ec8aSchristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2754a6ec8aSchristos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2854a6ec8aSchristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2954a6ec8aSchristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3054a6ec8aSchristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3154a6ec8aSchristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3254a6ec8aSchristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3354a6ec8aSchristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3454a6ec8aSchristos * SUCH DAMAGE. 3554a6ec8aSchristos * 3654a6ec8aSchristos * @(#)bpf.h 7.1 (Berkeley) 5/7/91 3754a6ec8aSchristos */ 3805335472Schristos #error "This is not used in NetBSD, we use <net/bpf.h>" 3954a6ec8aSchristos /* 4054a6ec8aSchristos * This is libpcap's cut-down version of bpf.h; it includes only 4154a6ec8aSchristos * the stuff needed for the code generator and the userland BPF 4254a6ec8aSchristos * interpreter, and the libpcap APIs for setting filters, etc.. 4354a6ec8aSchristos * 4454a6ec8aSchristos * "pcap-bpf.c" will include the native OS version, as it deals with 4554a6ec8aSchristos * the OS's BPF implementation. 4654a6ec8aSchristos * 476a19824dSchristos * At least two programs found by Google Code Search explicitly includes 486a19824dSchristos * <pcap/bpf.h> (even though <pcap.h>/<pcap/pcap.h> includes it for you), 496a19824dSchristos * so moving that stuff to <pcap/pcap.h> would break the build for some 506a19824dSchristos * programs. 5154a6ec8aSchristos */ 5254a6ec8aSchristos 536a19824dSchristos /* 546a19824dSchristos * If we've already included <net/bpf.h>, don't re-define this stuff. 556a19824dSchristos * We assume BSD-style multiple-include protection in <net/bpf.h>, 566a19824dSchristos * which is true of all but the oldest versions of FreeBSD and NetBSD, 576a19824dSchristos * or Tru64 UNIX-style multiple-include protection (or, at least, 586a19824dSchristos * Tru64 UNIX 5.x-style; I don't have earlier versions available to check), 596a19824dSchristos * or AIX-style multiple-include protection (or, at least, AIX 5.x-style; 606c7e2519Schristos * I don't have earlier versions available to check), or QNX-style 616c7e2519Schristos * multiple-include protection (as per GitHub pull request #394). 626a19824dSchristos * 636a19824dSchristos * We do not check for BPF_MAJOR_VERSION, as that's defined by 646a19824dSchristos * <linux/filter.h>, which is directly or indirectly included in some 656a19824dSchristos * programs that also include pcap.h, and <linux/filter.h> doesn't 666a19824dSchristos * define stuff we need. 676a19824dSchristos * 686a19824dSchristos * This also provides our own multiple-include protection. 696a19824dSchristos */ 706c7e2519Schristos #if !defined(_NET_BPF_H_) && !defined(_NET_BPF_H_INCLUDED) && !defined(_BPF_H_) && !defined(_H_BPF) && !defined(lib_pcap_bpf_h) 716a19824dSchristos #define lib_pcap_bpf_h 7254a6ec8aSchristos 73e1375535Schristos #include <pcap/funcattrs.h> 7403e25b48Schristos 7554a6ec8aSchristos #ifdef __cplusplus 7654a6ec8aSchristos extern "C" { 7754a6ec8aSchristos #endif 7854a6ec8aSchristos 7954a6ec8aSchristos /* BSD style release date */ 8054a6ec8aSchristos #define BPF_RELEASE 199606 8154a6ec8aSchristos 8254a6ec8aSchristos #ifdef MSDOS /* must be 32-bit */ 8354a6ec8aSchristos typedef long bpf_int32; 8454a6ec8aSchristos typedef unsigned long bpf_u_int32; 8554a6ec8aSchristos #else 8654a6ec8aSchristos typedef int bpf_int32; 8754a6ec8aSchristos typedef u_int bpf_u_int32; 8854a6ec8aSchristos #endif 8954a6ec8aSchristos 9054a6ec8aSchristos /* 9154a6ec8aSchristos * Alignment macros. BPF_WORDALIGN rounds up to the next 9254a6ec8aSchristos * even multiple of BPF_ALIGNMENT. 936a19824dSchristos * 946a19824dSchristos * Tcpdump's print-pflog.c uses this, so we define it here. 9554a6ec8aSchristos */ 9654a6ec8aSchristos #ifndef __NetBSD__ 9754a6ec8aSchristos #define BPF_ALIGNMENT sizeof(bpf_int32) 9854a6ec8aSchristos #else 9954a6ec8aSchristos #define BPF_ALIGNMENT sizeof(long) 10054a6ec8aSchristos #endif 10154a6ec8aSchristos #define BPF_WORDALIGN(x) (((x)+(BPF_ALIGNMENT-1))&~(BPF_ALIGNMENT-1)) 10254a6ec8aSchristos 10354a6ec8aSchristos /* 10454a6ec8aSchristos * Structure for "pcap_compile()", "pcap_setfilter()", etc.. 10554a6ec8aSchristos */ 10654a6ec8aSchristos struct bpf_program { 10754a6ec8aSchristos u_int bf_len; 10854a6ec8aSchristos struct bpf_insn *bf_insns; 10954a6ec8aSchristos }; 11054a6ec8aSchristos 11103e25b48Schristos #include <pcap/dlt.h> 11254a6ec8aSchristos 11354a6ec8aSchristos /* 11454a6ec8aSchristos * The instruction encodings. 115e3899b6dSchristos * 116e3899b6dSchristos * Please inform tcpdump-workers@lists.tcpdump.org if you use any 117e3899b6dSchristos * of the reserved values, so that we can note that they're used 118e3899b6dSchristos * (and perhaps implement it in the reference BPF implementation 119e3899b6dSchristos * and encourage its implementation elsewhere). 12054a6ec8aSchristos */ 121e3899b6dSchristos 122e3899b6dSchristos /* 123e3899b6dSchristos * The upper 8 bits of the opcode aren't used. BSD/OS used 0x8000. 124e3899b6dSchristos */ 125e3899b6dSchristos 12654a6ec8aSchristos /* instruction classes */ 12754a6ec8aSchristos #define BPF_CLASS(code) ((code) & 0x07) 12854a6ec8aSchristos #define BPF_LD 0x00 12954a6ec8aSchristos #define BPF_LDX 0x01 13054a6ec8aSchristos #define BPF_ST 0x02 13154a6ec8aSchristos #define BPF_STX 0x03 13254a6ec8aSchristos #define BPF_ALU 0x04 13354a6ec8aSchristos #define BPF_JMP 0x05 13454a6ec8aSchristos #define BPF_RET 0x06 13554a6ec8aSchristos #define BPF_MISC 0x07 13654a6ec8aSchristos 13754a6ec8aSchristos /* ld/ldx fields */ 13854a6ec8aSchristos #define BPF_SIZE(code) ((code) & 0x18) 13954a6ec8aSchristos #define BPF_W 0x00 14054a6ec8aSchristos #define BPF_H 0x08 14154a6ec8aSchristos #define BPF_B 0x10 142e3899b6dSchristos /* 0x18 reserved; used by BSD/OS */ 14354a6ec8aSchristos #define BPF_MODE(code) ((code) & 0xe0) 14454a6ec8aSchristos #define BPF_IMM 0x00 14554a6ec8aSchristos #define BPF_ABS 0x20 14654a6ec8aSchristos #define BPF_IND 0x40 14754a6ec8aSchristos #define BPF_MEM 0x60 14854a6ec8aSchristos #define BPF_LEN 0x80 14954a6ec8aSchristos #define BPF_MSH 0xa0 150e3899b6dSchristos /* 0xc0 reserved; used by BSD/OS */ 151e3899b6dSchristos /* 0xe0 reserved; used by BSD/OS */ 15254a6ec8aSchristos 15354a6ec8aSchristos /* alu/jmp fields */ 15454a6ec8aSchristos #define BPF_OP(code) ((code) & 0xf0) 15554a6ec8aSchristos #define BPF_ADD 0x00 15654a6ec8aSchristos #define BPF_SUB 0x10 15754a6ec8aSchristos #define BPF_MUL 0x20 15854a6ec8aSchristos #define BPF_DIV 0x30 15954a6ec8aSchristos #define BPF_OR 0x40 16054a6ec8aSchristos #define BPF_AND 0x50 16154a6ec8aSchristos #define BPF_LSH 0x60 16254a6ec8aSchristos #define BPF_RSH 0x70 16354a6ec8aSchristos #define BPF_NEG 0x80 164e3899b6dSchristos #define BPF_MOD 0x90 165e3899b6dSchristos #define BPF_XOR 0xa0 166e3899b6dSchristos /* 0xb0 reserved */ 167e3899b6dSchristos /* 0xc0 reserved */ 168e3899b6dSchristos /* 0xd0 reserved */ 169e3899b6dSchristos /* 0xe0 reserved */ 170e3899b6dSchristos /* 0xf0 reserved */ 171e3899b6dSchristos 17254a6ec8aSchristos #define BPF_JA 0x00 17354a6ec8aSchristos #define BPF_JEQ 0x10 17454a6ec8aSchristos #define BPF_JGT 0x20 17554a6ec8aSchristos #define BPF_JGE 0x30 17654a6ec8aSchristos #define BPF_JSET 0x40 177e3899b6dSchristos /* 0x50 reserved; used on BSD/OS */ 178e3899b6dSchristos /* 0x60 reserved */ 179e3899b6dSchristos /* 0x70 reserved */ 180e3899b6dSchristos /* 0x80 reserved */ 181e3899b6dSchristos /* 0x90 reserved */ 182e3899b6dSchristos /* 0xa0 reserved */ 183e3899b6dSchristos /* 0xb0 reserved */ 184e3899b6dSchristos /* 0xc0 reserved */ 185e3899b6dSchristos /* 0xd0 reserved */ 186e3899b6dSchristos /* 0xe0 reserved */ 187e3899b6dSchristos /* 0xf0 reserved */ 18854a6ec8aSchristos #define BPF_SRC(code) ((code) & 0x08) 18954a6ec8aSchristos #define BPF_K 0x00 19054a6ec8aSchristos #define BPF_X 0x08 19154a6ec8aSchristos 19254a6ec8aSchristos /* ret - BPF_K and BPF_X also apply */ 19354a6ec8aSchristos #define BPF_RVAL(code) ((code) & 0x18) 19454a6ec8aSchristos #define BPF_A 0x10 195e3899b6dSchristos /* 0x18 reserved */ 19654a6ec8aSchristos 19754a6ec8aSchristos /* misc */ 19854a6ec8aSchristos #define BPF_MISCOP(code) ((code) & 0xf8) 19954a6ec8aSchristos #define BPF_TAX 0x00 200e3899b6dSchristos /* 0x08 reserved */ 201e3899b6dSchristos /* 0x10 reserved */ 202e3899b6dSchristos /* 0x18 reserved */ 203e3899b6dSchristos /* #define BPF_COP 0x20 NetBSD "coprocessor" extensions */ 204e3899b6dSchristos /* 0x28 reserved */ 205e3899b6dSchristos /* 0x30 reserved */ 206e3899b6dSchristos /* 0x38 reserved */ 207e3899b6dSchristos /* #define BPF_COPX 0x40 NetBSD "coprocessor" extensions */ 208e3899b6dSchristos /* also used on BSD/OS */ 209e3899b6dSchristos /* 0x48 reserved */ 210e3899b6dSchristos /* 0x50 reserved */ 211e3899b6dSchristos /* 0x58 reserved */ 212e3899b6dSchristos /* 0x60 reserved */ 213e3899b6dSchristos /* 0x68 reserved */ 214e3899b6dSchristos /* 0x70 reserved */ 215e3899b6dSchristos /* 0x78 reserved */ 21654a6ec8aSchristos #define BPF_TXA 0x80 217e3899b6dSchristos /* 0x88 reserved */ 218e3899b6dSchristos /* 0x90 reserved */ 219e3899b6dSchristos /* 0x98 reserved */ 220e3899b6dSchristos /* 0xa0 reserved */ 221e3899b6dSchristos /* 0xa8 reserved */ 222e3899b6dSchristos /* 0xb0 reserved */ 223e3899b6dSchristos /* 0xb8 reserved */ 224e3899b6dSchristos /* 0xc0 reserved; used on BSD/OS */ 225e3899b6dSchristos /* 0xc8 reserved */ 226e3899b6dSchristos /* 0xd0 reserved */ 227e3899b6dSchristos /* 0xd8 reserved */ 228e3899b6dSchristos /* 0xe0 reserved */ 229e3899b6dSchristos /* 0xe8 reserved */ 230e3899b6dSchristos /* 0xf0 reserved */ 231e3899b6dSchristos /* 0xf8 reserved */ 23254a6ec8aSchristos 23354a6ec8aSchristos /* 23454a6ec8aSchristos * The instruction data structure. 23554a6ec8aSchristos */ 23654a6ec8aSchristos struct bpf_insn { 23754a6ec8aSchristos u_short code; 23854a6ec8aSchristos u_char jt; 23954a6ec8aSchristos u_char jf; 24054a6ec8aSchristos bpf_u_int32 k; 24154a6ec8aSchristos }; 24254a6ec8aSchristos 24354a6ec8aSchristos /* 24454a6ec8aSchristos * Macros for insn array initializers. 24554a6ec8aSchristos */ 24654a6ec8aSchristos #define BPF_STMT(code, k) { (u_short)(code), 0, 0, k } 24754a6ec8aSchristos #define BPF_JUMP(code, k, jt, jf) { (u_short)(code), jt, jf, k } 24854a6ec8aSchristos 24903e25b48Schristos PCAP_API int bpf_validate(const struct bpf_insn *, int); 25003e25b48Schristos PCAP_API u_int bpf_filter(const struct bpf_insn *, const u_char *, u_int, u_int); 25154a6ec8aSchristos 25254a6ec8aSchristos /* 25354a6ec8aSchristos * Number of scratch memory words (for BPF_LD|BPF_MEM and BPF_ST). 25454a6ec8aSchristos */ 25554a6ec8aSchristos #define BPF_MEMWORDS 16 25654a6ec8aSchristos 25754a6ec8aSchristos #ifdef __cplusplus 25854a6ec8aSchristos } 25954a6ec8aSchristos #endif 26054a6ec8aSchristos 2616a19824dSchristos #endif /* !defined(_NET_BPF_H_) && !defined(_BPF_H_) && !defined(_H_BPF) && !defined(lib_pcap_bpf_h) */ 262