/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1990, 1991, 19934* The Regents of the University of California. All rights reserved.5*6* This code is derived from the Stanford/CMU enet packet filter,7* (net/enet.c) distributed as part of 4.3BSD, and code contributed8* to Berkeley by Steven McCanne and Van Jacobson both of Lawrence9* Berkeley Laboratory.10*11* Redistribution and use in source and binary forms, with or without12* modification, are permitted provided that the following conditions13* are met:14* 1. Redistributions of source code must retain the above copyright15* notice, this list of conditions and the following disclaimer.16* 2. Redistributions in binary form must reproduce the above copyright17* notice, this list of conditions and the following disclaimer in the18* documentation and/or other materials provided with the distribution.19* 3. Neither the name of the University nor the names of its contributors20* may be used to endorse or promote products derived from this software21* without specific prior written permission.22*23* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND24* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE25* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE26* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE27* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL28* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS29* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)30* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT31* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY32* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF33* SUCH DAMAGE.34*/3536#ifndef _NET_BPF_H_37#define _NET_BPF_H_3839#include <sys/types.h>40#include <sys/_eventhandler.h>41#include <sys/ck.h>42#include <net/dlt.h>4344/* BSD style release date */45#define BPF_RELEASE 1996064647typedef int32_t bpf_int32;48typedef u_int32_t bpf_u_int32;49typedef int64_t bpf_int64;50typedef u_int64_t bpf_u_int64;51struct ifnet;5253/*54* Alignment macros. BPF_WORDALIGN rounds up to the next multiple of55* BPF_ALIGNMENT.56*/57#define BPF_ALIGNMENT sizeof(long)58#define BPF_WORDALIGN(x) (((x) + (BPF_ALIGNMENT - 1)) & ~(BPF_ALIGNMENT - 1))5960#define BPF_MAXINSNS 51261#define BPF_MAXBUFSIZE 0x8000062#define BPF_MINBUFSIZE 326364/*65* Structure for BIOCSETF.66*/67struct bpf_program {68u_int bf_len;69struct bpf_insn *bf_insns;70};7172/*73* Struct returned by BIOCGSTATS.74*/75struct bpf_stat {76u_int bs_recv; /* number of packets received */77u_int bs_drop; /* number of packets dropped */78};7980/*81* Struct return by BIOCVERSION. This represents the version number of82* 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 the85* running kernel has the same major number and a minor number equal86* equal to or less than the filter being downloaded. Otherwise, the87* results are undefined, meaning an error may be returned or packets88* may be accepted haphazardly.89* It has nothing to do with the source code version.90*/91struct bpf_version {92u_short bv_major;93u_short bv_minor;94};95/* Current version number of filter architecture. */96#define BPF_MAJOR_VERSION 197#define BPF_MINOR_VERSION 19899/*100* Historically, BPF has supported a single buffering model, first using mbuf101* clusters in kernel, and later using malloc(9) buffers in kernel. We now102* support multiple buffering modes, which may be queried and set using103* BIOCGETBUFMODE and BIOCSETBUFMODE. So as to avoid handling the complexity104* of changing modes while sniffing packets, the mode becomes fixed once an105* interface has been attached to the BPF descriptor.106*/107#define BPF_BUFMODE_BUFFER 1 /* Kernel buffers with read(). */108#define BPF_BUFMODE_ZBUF 2 /* Zero-copy buffers. */109110/*-111* Struct used by BIOCSETZBUF, BIOCROTZBUF: describes up to two zero-copy112* buffer as used by BPF.113*/114struct bpf_zbuf {115void *bz_bufa; /* Location of 'a' zero-copy buffer. */116void *bz_bufb; /* Location of 'b' zero-copy buffer. */117size_t bz_buflen; /* Size of zero-copy buffers. */118};119120/*121* Struct used by BIOCGETIFLIST.122*/123struct bpf_iflist {124u_int bi_size;125u_int bi_count;126void *bi_ubuf;127};128129#define BIOCGBLEN _IOR('B', 102, u_int)130#define BIOCSBLEN _IOWR('B', 102, u_int)131#define BIOCSETF _IOW('B', 103, struct bpf_program)132#define BIOCFLUSH _IO('B', 104)133#define BIOCPROMISC _IO('B', 105)134#define BIOCGDLT _IOR('B', 106, u_int)135#define BIOCGETIF _IOR('B', 107, struct ifreq)136#define BIOCSETIF _IOW('B', 108, struct ifreq)137#define BIOCSRTIMEOUT _IOW('B', 109, struct timeval)138#define BIOCGRTIMEOUT _IOR('B', 110, struct timeval)139#define BIOCGSTATS _IOR('B', 111, struct bpf_stat)140#define BIOCIMMEDIATE _IOW('B', 112, u_int)141#define BIOCVERSION _IOR('B', 113, struct bpf_version)142#define BIOCGRSIG _IOR('B', 114, u_int)143#define BIOCSRSIG _IOW('B', 115, u_int)144#define BIOCGHDRCMPLT _IOR('B', 116, u_int)145#define BIOCSHDRCMPLT _IOW('B', 117, u_int)146#define BIOCGDIRECTION _IOR('B', 118, u_int)147#define BIOCSDIRECTION _IOW('B', 119, u_int)148#define BIOCSDLT _IOW('B', 120, u_int)149#define BIOCGDLTLIST _IOWR('B', 121, struct bpf_dltlist)150#define BIOCLOCK _IO('B', 122)151#define BIOCSETWF _IOW('B', 123, struct bpf_program)152#define BIOCFEEDBACK _IOW('B', 124, u_int)153#define BIOCGETBUFMODE _IOR('B', 125, u_int)154#define BIOCSETBUFMODE _IOW('B', 126, u_int)155#define BIOCGETZMAX _IOR('B', 127, size_t)156#define BIOCROTZBUF _IOR('B', 128, struct bpf_zbuf)157#define BIOCSETZBUF _IOW('B', 129, struct bpf_zbuf)158#define BIOCSETFNR _IOW('B', 130, struct bpf_program)159#define BIOCGTSTAMP _IOR('B', 131, u_int)160#define BIOCSTSTAMP _IOW('B', 132, u_int)161#define BIOCSETVLANPCP _IOW('B', 133, u_int)162#define BIOCGETIFLIST _IOWR('B', 134, struct bpf_iflist)163164/* Obsolete */165#define BIOCGSEESENT BIOCGDIRECTION166#define BIOCSSEESENT BIOCSDIRECTION167168/* Packet directions */169enum bpf_direction {170BPF_D_IN, /* See incoming packets */171BPF_D_INOUT, /* See incoming and outgoing packets */172BPF_D_OUT /* See outgoing packets */173};174175/* Time stamping functions */176#define BPF_T_MICROTIME 0x0000177#define BPF_T_NANOTIME 0x0001178#define BPF_T_BINTIME 0x0002179#define BPF_T_NONE 0x0003180#define BPF_T_FORMAT_MASK 0x0003181#define BPF_T_NORMAL 0x0000182#define BPF_T_FAST 0x0100183#define BPF_T_MONOTONIC 0x0200184#define BPF_T_MONOTONIC_FAST (BPF_T_FAST | BPF_T_MONOTONIC)185#define BPF_T_FLAG_MASK 0x0300186#define BPF_T_FORMAT(t) ((t) & BPF_T_FORMAT_MASK)187#define BPF_T_FLAG(t) ((t) & BPF_T_FLAG_MASK)188#define BPF_T_VALID(t) \189((t) == BPF_T_NONE || (BPF_T_FORMAT(t) != BPF_T_NONE && \190((t) & ~(BPF_T_FORMAT_MASK | BPF_T_FLAG_MASK)) == 0))191192#define BPF_T_MICROTIME_FAST (BPF_T_MICROTIME | BPF_T_FAST)193#define BPF_T_NANOTIME_FAST (BPF_T_NANOTIME | BPF_T_FAST)194#define BPF_T_BINTIME_FAST (BPF_T_BINTIME | BPF_T_FAST)195#define BPF_T_MICROTIME_MONOTONIC (BPF_T_MICROTIME | BPF_T_MONOTONIC)196#define BPF_T_NANOTIME_MONOTONIC (BPF_T_NANOTIME | BPF_T_MONOTONIC)197#define BPF_T_BINTIME_MONOTONIC (BPF_T_BINTIME | BPF_T_MONOTONIC)198#define BPF_T_MICROTIME_MONOTONIC_FAST (BPF_T_MICROTIME | BPF_T_MONOTONIC_FAST)199#define BPF_T_NANOTIME_MONOTONIC_FAST (BPF_T_NANOTIME | BPF_T_MONOTONIC_FAST)200#define BPF_T_BINTIME_MONOTONIC_FAST (BPF_T_BINTIME | BPF_T_MONOTONIC_FAST)201202/*203* Structure prepended to each packet.204*/205struct bpf_ts {206bpf_int64 bt_sec; /* seconds */207bpf_u_int64 bt_frac; /* fraction */208};209struct bpf_xhdr {210struct bpf_ts bh_tstamp; /* time stamp */211bpf_u_int32 bh_caplen; /* length of captured portion */212bpf_u_int32 bh_datalen; /* original length of packet */213u_short bh_hdrlen; /* length of bpf header (this struct214plus alignment padding) */215};216/* Obsolete */217struct bpf_hdr {218struct timeval bh_tstamp; /* time stamp */219bpf_u_int32 bh_caplen; /* length of captured portion */220bpf_u_int32 bh_datalen; /* original length of packet */221u_short bh_hdrlen; /* length of bpf header (this struct222plus alignment padding) */223};224#ifdef _KERNEL225#define MTAG_BPF 0x627066226#define MTAG_BPF_TIMESTAMP 0227#endif228229/*230* When using zero-copy BPF buffers, a shared memory header is present231* allowing the kernel BPF implementation and user process to synchronize232* without using system calls. This structure defines that header. When233* accessing these fields, appropriate atomic operation and memory barriers234* are required in order not to see stale or out-of-order data; see bpf(4)235* for reference code to access these fields from userspace.236*237* The layout of this structure is critical, and must not be changed; if must238* fit in a single page on all architectures.239*/240struct bpf_zbuf_header {241volatile u_int bzh_kernel_gen; /* Kernel generation number. */242volatile u_int bzh_kernel_len; /* Length of data in the buffer. */243volatile u_int bzh_user_gen; /* User generation number. */244u_int _bzh_pad[5];245};246247/*248* The instruction encodings.249*250* Please inform [email protected] if you use any251* of the reserved values, so that we can note that they're used252* (and perhaps implement it in the reference BPF implementation253* and encourage its implementation elsewhere).254*/255256/*257* The upper 8 bits of the opcode aren't used. BSD/OS used 0x8000.258*/259260/* instruction classes */261#define BPF_CLASS(code) ((code) & 0x07)262#define BPF_LD 0x00263#define BPF_LDX 0x01264#define BPF_ST 0x02265#define BPF_STX 0x03266#define BPF_ALU 0x04267#define BPF_JMP 0x05268#define BPF_RET 0x06269#define BPF_MISC 0x07270271/* ld/ldx fields */272#define BPF_SIZE(code) ((code) & 0x18)273#define BPF_W 0x00274#define BPF_H 0x08275#define BPF_B 0x10276/* 0x18 reserved; used by BSD/OS */277#define BPF_MODE(code) ((code) & 0xe0)278#define BPF_IMM 0x00279#define BPF_ABS 0x20280#define BPF_IND 0x40281#define BPF_MEM 0x60282#define BPF_LEN 0x80283#define BPF_MSH 0xa0284/* 0xc0 reserved; used by BSD/OS */285/* 0xe0 reserved; used by BSD/OS */286287/* alu/jmp fields */288#define BPF_OP(code) ((code) & 0xf0)289#define BPF_ADD 0x00290#define BPF_SUB 0x10291#define BPF_MUL 0x20292#define BPF_DIV 0x30293#define BPF_OR 0x40294#define BPF_AND 0x50295#define BPF_LSH 0x60296#define BPF_RSH 0x70297#define BPF_NEG 0x80298#define BPF_MOD 0x90299#define BPF_XOR 0xa0300/* 0xb0 reserved */301/* 0xc0 reserved */302/* 0xd0 reserved */303/* 0xe0 reserved */304/* 0xf0 reserved */305306#define BPF_JA 0x00307#define BPF_JEQ 0x10308#define BPF_JGT 0x20309#define BPF_JGE 0x30310#define BPF_JSET 0x40311/* 0x50 reserved; used on BSD/OS */312/* 0x60 reserved */313/* 0x70 reserved */314/* 0x80 reserved */315/* 0x90 reserved */316/* 0xa0 reserved */317/* 0xb0 reserved */318/* 0xc0 reserved */319/* 0xd0 reserved */320/* 0xe0 reserved */321/* 0xf0 reserved */322#define BPF_SRC(code) ((code) & 0x08)323#define BPF_K 0x00324#define BPF_X 0x08325326/* ret - BPF_K and BPF_X also apply */327#define BPF_RVAL(code) ((code) & 0x18)328#define BPF_A 0x10329/* 0x18 reserved */330331/* misc */332#define BPF_MISCOP(code) ((code) & 0xf8)333#define BPF_TAX 0x00334/* 0x08 reserved */335/* 0x10 reserved */336/* 0x18 reserved */337/* #define BPF_COP 0x20 NetBSD "coprocessor" extensions */338/* 0x28 reserved */339/* 0x30 reserved */340/* 0x38 reserved */341/* #define BPF_COPX 0x40 NetBSD "coprocessor" extensions */342/* also used on BSD/OS */343/* 0x48 reserved */344/* 0x50 reserved */345/* 0x58 reserved */346/* 0x60 reserved */347/* 0x68 reserved */348/* 0x70 reserved */349/* 0x78 reserved */350#define BPF_TXA 0x80351/* 0x88 reserved */352/* 0x90 reserved */353/* 0x98 reserved */354/* 0xa0 reserved */355/* 0xa8 reserved */356/* 0xb0 reserved */357/* 0xb8 reserved */358/* 0xc0 reserved; used on BSD/OS */359/* 0xc8 reserved */360/* 0xd0 reserved */361/* 0xd8 reserved */362/* 0xe0 reserved */363/* 0xe8 reserved */364/* 0xf0 reserved */365/* 0xf8 reserved */366367/*368* The instruction data structure.369*/370struct bpf_insn {371u_short code;372u_char jt;373u_char jf;374bpf_u_int32 k;375};376377/*378* Macros for insn array initializers.379*/380#define BPF_STMT(code, k) { (u_short)(code), 0, 0, k }381#define BPF_JUMP(code, k, jt, jf) { (u_short)(code), jt, jf, k }382383/*384* Structure to retrieve available DLTs for the interface.385*/386struct bpf_dltlist {387u_int bfl_len; /* number of bfd_list array */388u_int *bfl_list; /* array of DLTs */389};390391#ifdef _KERNEL392#ifdef MALLOC_DECLARE393MALLOC_DECLARE(M_BPF);394#endif395#ifdef SYSCTL_DECL396SYSCTL_DECL(_net_bpf);397#endif398399/*400* Rotate the packet buffers in descriptor d. Move the store buffer into the401* hold slot, and the free buffer into the store slot. Zero the length of the402* new store buffer. Descriptor lock should be held. One must be careful to403* not rotate the buffers twice, i.e. if fbuf != NULL.404*/405#define ROTATE_BUFFERS(d) do { \406(d)->bd_hbuf = (d)->bd_sbuf; \407(d)->bd_hlen = (d)->bd_slen; \408(d)->bd_sbuf = (d)->bd_fbuf; \409(d)->bd_slen = 0; \410(d)->bd_fbuf = NULL; \411bpf_bufheld(d); \412} while (0)413414/*415* Descriptor associated with each attached hardware interface.416* Part of this structure is exposed to external callers to speed up417* bpf_peers_present() calls.418*/419struct mbuf;420struct bpf_if;421struct bif_methods;422CK_LIST_HEAD(bpfd_list, bpf_d);423424struct bpf_if * bpf_attach(const char *, u_int, u_int,425const struct bif_methods *, void *);426void bpf_detach(struct bpf_if *);427void bpf_vmove(struct bpf_if *bp);428void bpf_bufheld(struct bpf_d *d);429int bpf_validate(const struct bpf_insn *, int);430void bpf_tap(struct bpf_if *, u_char *, u_int);431void bpf_tap_if(struct ifnet *, u_char *, u_int);432void bpf_mtap(struct bpf_if *, struct mbuf *);433void bpf_mtap_if(struct ifnet *, struct mbuf *);434void bpf_mtap2(struct bpf_if *, void *, u_int, struct mbuf *);435void bpf_mtap2_if(struct ifnet *, void *, u_int, struct mbuf *);436void bpfattach(struct ifnet *, u_int, u_int);437void bpfdetach(struct ifnet *);438bool bpf_peers_present_if(struct ifnet *);439#ifdef VIMAGE440void bpf_ifdetach(struct ifnet *);441#endif442443void bpfilterattach(int);444u_int bpf_filter(const struct bpf_insn *, u_char *, u_int, u_int);445446static __inline bool447bpf_peers_present(const struct bpf_if *bpf)448{449const struct bpfd_list *dlist;450451dlist = (const struct bpfd_list *)bpf;452return (!CK_LIST_EMPTY(dlist));453}454455#define BPF_TAP(_ifp, _pkt, _pktlen) \456bpf_tap_if((_ifp), (_pkt), (_pktlen))457#define BPF_MTAP(_ifp, _m) \458bpf_mtap_if((_ifp), (_m))459#define BPF_MTAP2(_ifp, _data, _dlen, _m) \460bpf_mtap2_if((_ifp), (_data), (_dlen), (_m))461462typedef void bif_attachd_t(void *);463typedef void bif_detachd_t(void *);464typedef bool bif_chkdir_t(void *, const struct mbuf *, int);465typedef int bif_write_t(void *, struct mbuf *, struct mbuf *, int);466typedef uint32_t bif_wrsize_t(void *);467typedef int bif_promisc_t(void *, bool);468typedef int bif_mac_check_receive_t(void *, struct bpf_d *);469struct bif_methods {470bif_attachd_t *bif_attachd;471bif_detachd_t *bif_detachd;472bif_chkdir_t *bif_chkdir;473bif_promisc_t *bif_promisc;474/* Writable taps shall implement the below methods. */475bif_write_t *bif_write;476bif_wrsize_t *bif_wrsize;477bif_mac_check_receive_t *bif_mac_check_receive;478};479480/* Ifnet methods implemented in bpf_ifnet.c are shared with net80211. */481extern bif_wrsize_t bpf_ifnet_wrsize;482extern bif_promisc_t bpf_ifnet_promisc;483#endif /* _KERNEL */484485/*486* Number of scratch memory words (for BPF_LD|BPF_MEM and BPF_ST).487*/488#define BPF_MEMWORDS 16489490/* BPF attach/detach events */491typedef void (*bpf_track_fn)(void *, struct ifnet *, int /* dlt */,492int /* 1 =>'s attach */);493EVENTHANDLER_DECLARE(bpf_track, bpf_track_fn);494495#endif /* _NET_BPF_H_ */496497498