/*-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_BPFDESC_H_37#define _NET_BPFDESC_H_3839#include <sys/callout.h>40#include <sys/selinfo.h>41#include <sys/ck.h>42#include <sys/conf.h>43#include <sys/counter.h>44#include <sys/epoch.h>45#include <net/if.h>4647/*48* Descriptor associated with each open bpf file.49*/50struct zbuf;51struct bpf_d {52CK_LIST_ENTRY(bpf_d) bd_next; /* Linked list of descriptors */53/*54* Buffer slots: two memory buffers store the incoming packets.55* The model has three slots. Sbuf is always occupied.56* sbuf (store) - Receive interrupt puts packets here.57* hbuf (hold) - When sbuf is full, put buffer here and58* wakeup read (replace sbuf with fbuf).59* fbuf (free) - When read is done, put buffer here.60* On receiving, if sbuf is full and fbuf is 0, packet is dropped.61*/62caddr_t bd_sbuf; /* store slot */63caddr_t bd_hbuf; /* hold slot */64caddr_t bd_fbuf; /* free slot */65int bd_hbuf_in_use; /* don't rotate buffers */66int bd_slen; /* current length of store buffer */67int bd_hlen; /* current length of hold buffer */6869int bd_bufsize; /* absolute length of buffers */7071struct bpf_if * bd_bif; /* interface descriptor */72u_long bd_rtout; /* Read timeout in 'ticks' */73struct bpf_insn *bd_rfilter; /* read filter code */74struct bpf_insn *bd_wfilter; /* write filter code */75void *bd_bfilter; /* binary filter code */76counter_u64_t bd_rcount; /* number of packets received */77counter_u64_t bd_dcount; /* number of packets dropped */7879u_char bd_promisc; /* true if listening promiscuously */80u_char bd_state; /* idle, waiting, or timed out */81u_char bd_immediate; /* true to return on packet arrival */82u_char bd_writer; /* non-zero if d is writer-only */83int bd_hdrcmplt; /* false to fill in src lladdr automatically */84int bd_direction; /* select packet direction */85int bd_tstamp; /* select time stamping function */86int bd_feedback; /* true to feed back sent packets */87int bd_async; /* non-zero if packet reception should generate signal */88int bd_sig; /* signal to send upon packet reception */89int bd_pcp; /* VLAN pcp tag */90struct sigio * bd_sigio; /* information for async I/O */91struct selinfo bd_sel; /* bsd select info */92struct mtx bd_lock; /* per-descriptor lock */93struct callout bd_callout; /* for BPF timeouts with select */94struct label *bd_label; /* MAC label for descriptor */95counter_u64_t bd_fcount; /* number of packets which matched filter */96pid_t bd_pid; /* PID which created descriptor */97int bd_locked; /* true if descriptor is locked */98u_int bd_bufmode; /* Current buffer mode. */99counter_u64_t bd_wcount; /* number of packets written */100counter_u64_t bd_wfcount; /* number of packets that matched write filter */101counter_u64_t bd_wdcount; /* number of packets dropped during a write */102counter_u64_t bd_zcopy; /* number of zero copy operations */103u_char bd_compat32; /* 32-bit stream on LP64 system */104105volatile u_int bd_refcnt;106struct epoch_context epoch_ctx;107};108109/* Values for bd_state */110#define BPF_IDLE 0 /* no select in progress */111#define BPF_WAITING 1 /* waiting for read timeout in select */112#define BPF_TIMED_OUT 2 /* read timeout has expired in select */113114#define BPFD_LOCK(bd) mtx_lock(&(bd)->bd_lock)115#define BPFD_UNLOCK(bd) mtx_unlock(&(bd)->bd_lock)116#define BPFD_LOCK_ASSERT(bd) mtx_assert(&(bd)->bd_lock, MA_OWNED)117118#define BPF_PID_REFRESH(bd, td) (bd)->bd_pid = (td)->td_proc->p_pid119#define BPF_PID_REFRESH_CUR(bd) (bd)->bd_pid = curthread->td_proc->p_pid120121/*122* External representation of the bpf descriptor123*/124struct xbpf_d {125u_int bd_structsize; /* Size of this structure. */126u_char bd_promisc;127u_char bd_immediate;128u_char __bd_pad[6];129int bd_hdrcmplt;130int bd_direction;131int bd_feedback;132int bd_async;133u_int64_t bd_rcount;134u_int64_t bd_dcount;135u_int64_t bd_fcount;136int bd_sig;137int bd_slen;138int bd_hlen;139int bd_bufsize;140pid_t bd_pid;141char bd_ifname[IFNAMSIZ];142int bd_locked;143u_int64_t bd_wcount;144u_int64_t bd_wfcount;145u_int64_t bd_wdcount;146u_int64_t bd_zcopy;147int bd_bufmode;148/*149* Allocate 4 64 bit unsigned integers for future expansion so we do150* not have to worry about breaking the ABI.151*/152u_int64_t bd_spare[4];153};154155#define BPFIF_FLAG_DYING 1 /* Reject new bpf consumers */156157#endif158159160