1c398230bSWarner Losh /*- 2*51369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 3*51369649SPedro F. Giffuni * 4df8bae1dSRodney W. Grimes * Copyright (c) 1990, 1991, 1993 5df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 6df8bae1dSRodney W. Grimes * 7df8bae1dSRodney W. Grimes * This code is derived from the Stanford/CMU enet packet filter, 8df8bae1dSRodney W. Grimes * (net/enet.c) distributed as part of 4.3BSD, and code contributed 9df8bae1dSRodney W. Grimes * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence 10df8bae1dSRodney W. Grimes * Berkeley Laboratory. 11df8bae1dSRodney W. Grimes * 12df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 13df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 14df8bae1dSRodney W. Grimes * are met: 15df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 17df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 18df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 19df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 20fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 21df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 22df8bae1dSRodney W. Grimes * without specific prior written permission. 23df8bae1dSRodney W. Grimes * 24df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34df8bae1dSRodney W. Grimes * SUCH DAMAGE. 35df8bae1dSRodney W. Grimes * 36df8bae1dSRodney W. Grimes * @(#)bpfdesc.h 8.1 (Berkeley) 6/10/93 37df8bae1dSRodney W. Grimes * 38c3aac50fSPeter Wemm * $FreeBSD$ 39df8bae1dSRodney W. Grimes */ 40df8bae1dSRodney W. Grimes 41cea1da3bSPaul Richards #ifndef _NET_BPFDESC_H_ 42cea1da3bSPaul Richards #define _NET_BPFDESC_H_ 43cea1da3bSPaul Richards 4481bda851SJohn Polstra #include <sys/callout.h> 450a2c3d48SGarrett Wollman #include <sys/selinfo.h> 464a3feeaaSRobert Watson #include <sys/queue.h> 4769f7644bSChristian S.J. Peron #include <sys/conf.h> 4869f7644bSChristian S.J. Peron #include <net/if.h> 4926f9a767SRodney W. Grimes 50df8bae1dSRodney W. Grimes /* 51df8bae1dSRodney W. Grimes * Descriptor associated with each open bpf file. 52df8bae1dSRodney W. Grimes */ 534d621040SChristian S.J. Peron struct zbuf; 54df8bae1dSRodney W. Grimes struct bpf_d { 554a3feeaaSRobert Watson LIST_ENTRY(bpf_d) bd_next; /* Linked list of descriptors */ 56df8bae1dSRodney W. Grimes /* 574d621040SChristian S.J. Peron * Buffer slots: two memory buffers store the incoming packets. 58df8bae1dSRodney W. Grimes * The model has three slots. Sbuf is always occupied. 59df8bae1dSRodney W. Grimes * sbuf (store) - Receive interrupt puts packets here. 606d38c5adSRobert Watson * hbuf (hold) - When sbuf is full, put buffer here and 61df8bae1dSRodney W. Grimes * wakeup read (replace sbuf with fbuf). 626d38c5adSRobert Watson * fbuf (free) - When read is done, put buffer here. 63df8bae1dSRodney W. Grimes * On receiving, if sbuf is full and fbuf is 0, packet is dropped. 64df8bae1dSRodney W. Grimes */ 65df8bae1dSRodney W. Grimes caddr_t bd_sbuf; /* store slot */ 66df8bae1dSRodney W. Grimes caddr_t bd_hbuf; /* hold slot */ 67df8bae1dSRodney W. Grimes caddr_t bd_fbuf; /* free slot */ 683b3b91e7SGuy Helmer int bd_hbuf_in_use; /* don't rotate buffers */ 69df8bae1dSRodney W. Grimes int bd_slen; /* current length of store buffer */ 70df8bae1dSRodney W. Grimes int bd_hlen; /* current length of hold buffer */ 71df8bae1dSRodney W. Grimes 72df8bae1dSRodney W. Grimes int bd_bufsize; /* absolute length of buffers */ 73df8bae1dSRodney W. Grimes 74df8bae1dSRodney W. Grimes struct bpf_if * bd_bif; /* interface descriptor */ 75df8bae1dSRodney W. Grimes u_long bd_rtout; /* Read timeout in 'ticks' */ 7693e39f0bSChristian S.J. Peron struct bpf_insn *bd_rfilter; /* read filter code */ 7793e39f0bSChristian S.J. Peron struct bpf_insn *bd_wfilter; /* write filter code */ 78a36599ccSJung-uk Kim void *bd_bfilter; /* binary filter code */ 794d621040SChristian S.J. Peron u_int64_t bd_rcount; /* number of packets received */ 804d621040SChristian S.J. Peron u_int64_t bd_dcount; /* number of packets dropped */ 81df8bae1dSRodney W. Grimes 82df8bae1dSRodney W. Grimes u_char bd_promisc; /* true if listening promiscuously */ 83df8bae1dSRodney W. Grimes u_char bd_state; /* idle, waiting, or timed out */ 84df8bae1dSRodney W. Grimes u_char bd_immediate; /* true to return on packet arrival */ 8551ec1eb7SAlexander V. Chernikov u_char bd_writer; /* non-zero if d is writer-only */ 86114ae644SMike Smith int bd_hdrcmplt; /* false to fill in src lladdr automatically */ 87560a54e1SJung-uk Kim int bd_direction; /* select packet direction */ 88547d94bdSJung-uk Kim int bd_tstamp; /* select time stamping function */ 89560a54e1SJung-uk Kim int bd_feedback; /* true to feed back sent packets */ 9000a83887SPaul Traina int bd_async; /* non-zero if packet reception should generate signal */ 9100a83887SPaul Traina int bd_sig; /* signal to send upon packet reception */ 9262d6ce3aSDon Lewis struct sigio * bd_sigio; /* information for async I/O */ 93df8bae1dSRodney W. Grimes struct selinfo bd_sel; /* bsd select info */ 94afa85850SAlexander V. Chernikov struct mtx bd_lock; /* per-descriptor lock */ 9581bda851SJohn Polstra struct callout bd_callout; /* for BPF timeouts with select */ 96eca8a663SRobert Watson struct label *bd_label; /* MAC label for descriptor */ 974d621040SChristian S.J. Peron u_int64_t bd_fcount; /* number of packets which matched filter */ 9869f7644bSChristian S.J. Peron pid_t bd_pid; /* PID which created descriptor */ 9993e39f0bSChristian S.J. Peron int bd_locked; /* true if descriptor is locked */ 1004d621040SChristian S.J. Peron u_int bd_bufmode; /* Current buffer mode. */ 1014d621040SChristian S.J. Peron u_int64_t bd_wcount; /* number of packets written */ 1024d621040SChristian S.J. Peron u_int64_t bd_wfcount; /* number of packets that matched write filter */ 1034d621040SChristian S.J. Peron u_int64_t bd_wdcount; /* number of packets dropped during a write */ 1044d621040SChristian S.J. Peron u_int64_t bd_zcopy; /* number of zero copy operations */ 105fc0a61a4SKonstantin Belousov u_char bd_compat32; /* 32-bit stream on LP64 system */ 106df8bae1dSRodney W. Grimes }; 107df8bae1dSRodney W. Grimes 10881bda851SJohn Polstra /* Values for bd_state */ 10981bda851SJohn Polstra #define BPF_IDLE 0 /* no select in progress */ 11081bda851SJohn Polstra #define BPF_WAITING 1 /* waiting for read timeout in select */ 11181bda851SJohn Polstra #define BPF_TIMED_OUT 2 /* read timeout has expired in select */ 11281bda851SJohn Polstra 113afa85850SAlexander V. Chernikov #define BPFD_LOCK(bd) mtx_lock(&(bd)->bd_lock) 114afa85850SAlexander V. Chernikov #define BPFD_UNLOCK(bd) mtx_unlock(&(bd)->bd_lock) 115afa85850SAlexander V. Chernikov #define BPFD_LOCK_ASSERT(bd) mtx_assert(&(bd)->bd_lock, MA_OWNED) 116e7bb21b3SJonathan Lemon 117e4b3229aSAlexander V. Chernikov #define BPF_PID_REFRESH(bd, td) (bd)->bd_pid = (td)->td_proc->p_pid 118e4b3229aSAlexander V. Chernikov #define BPF_PID_REFRESH_CUR(bd) (bd)->bd_pid = curthread->td_proc->p_pid 119e4b3229aSAlexander V. Chernikov 120e4b3229aSAlexander V. Chernikov #define BPF_LOCK() mtx_lock(&bpf_mtx) 121e4b3229aSAlexander V. Chernikov #define BPF_UNLOCK() mtx_unlock(&bpf_mtx) 122e4b3229aSAlexander V. Chernikov #define BPF_LOCK_ASSERT() mtx_assert(&bpf_mtx, MA_OWNED) 123df8bae1dSRodney W. Grimes /* 12469f7644bSChristian S.J. Peron * External representation of the bpf descriptor 12569f7644bSChristian S.J. Peron */ 12669f7644bSChristian S.J. Peron struct xbpf_d { 1274d621040SChristian S.J. Peron u_int bd_structsize; /* Size of this structure. */ 12869f7644bSChristian S.J. Peron u_char bd_promisc; 12969f7644bSChristian S.J. Peron u_char bd_immediate; 1304d621040SChristian S.J. Peron u_char __bd_pad[6]; 13169f7644bSChristian S.J. Peron int bd_hdrcmplt; 132560a54e1SJung-uk Kim int bd_direction; 133560a54e1SJung-uk Kim int bd_feedback; 13469f7644bSChristian S.J. Peron int bd_async; 1354d621040SChristian S.J. Peron u_int64_t bd_rcount; 1364d621040SChristian S.J. Peron u_int64_t bd_dcount; 1374d621040SChristian S.J. Peron u_int64_t bd_fcount; 13869f7644bSChristian S.J. Peron int bd_sig; 13969f7644bSChristian S.J. Peron int bd_slen; 14069f7644bSChristian S.J. Peron int bd_hlen; 14169f7644bSChristian S.J. Peron int bd_bufsize; 14269f7644bSChristian S.J. Peron pid_t bd_pid; 14369f7644bSChristian S.J. Peron char bd_ifname[IFNAMSIZ]; 14493e39f0bSChristian S.J. Peron int bd_locked; 1454d621040SChristian S.J. Peron u_int64_t bd_wcount; 1464d621040SChristian S.J. Peron u_int64_t bd_wfcount; 1474d621040SChristian S.J. Peron u_int64_t bd_wdcount; 1484d621040SChristian S.J. Peron u_int64_t bd_zcopy; 1494d621040SChristian S.J. Peron int bd_bufmode; 1504d621040SChristian S.J. Peron /* 1514d621040SChristian S.J. Peron * Allocate 4 64 bit unsigned integers for future expansion so we do 1524d621040SChristian S.J. Peron * not have to worry about breaking the ABI. 1534d621040SChristian S.J. Peron */ 1544d621040SChristian S.J. Peron u_int64_t bd_spare[4]; 15569f7644bSChristian S.J. Peron }; 15669f7644bSChristian S.J. Peron 157e4b3229aSAlexander V. Chernikov #define BPFIF_RLOCK(bif) rw_rlock(&(bif)->bif_lock) 158e4b3229aSAlexander V. Chernikov #define BPFIF_RUNLOCK(bif) rw_runlock(&(bif)->bif_lock) 159e4b3229aSAlexander V. Chernikov #define BPFIF_WLOCK(bif) rw_wlock(&(bif)->bif_lock) 160e4b3229aSAlexander V. Chernikov #define BPFIF_WUNLOCK(bif) rw_wunlock(&(bif)->bif_lock) 161e7bb21b3SJonathan Lemon 162afa85850SAlexander V. Chernikov #define BPFIF_FLAG_DYING 1 /* Reject new bpf consumers */ 163afa85850SAlexander V. Chernikov 164cea1da3bSPaul Richards #endif 165