11eaf0ac3Slogwang /*- 2*d4a07e70Sfengbojiang * SPDX-License-Identifier: BSD-3-Clause 3*d4a07e70Sfengbojiang * 41eaf0ac3Slogwang * Copyright (c) 1990, 1991, 1993 51eaf0ac3Slogwang * The Regents of the University of California. All rights reserved. 61eaf0ac3Slogwang * 71eaf0ac3Slogwang * This code is derived from the Stanford/CMU enet packet filter, 81eaf0ac3Slogwang * (net/enet.c) distributed as part of 4.3BSD, and code contributed 91eaf0ac3Slogwang * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence 101eaf0ac3Slogwang * Berkeley Laboratory. 111eaf0ac3Slogwang * 121eaf0ac3Slogwang * Redistribution and use in source and binary forms, with or without 131eaf0ac3Slogwang * modification, are permitted provided that the following conditions 141eaf0ac3Slogwang * are met: 151eaf0ac3Slogwang * 1. Redistributions of source code must retain the above copyright 161eaf0ac3Slogwang * notice, this list of conditions and the following disclaimer. 171eaf0ac3Slogwang * 2. Redistributions in binary form must reproduce the above copyright 181eaf0ac3Slogwang * notice, this list of conditions and the following disclaimer in the 191eaf0ac3Slogwang * documentation and/or other materials provided with the distribution. 20*d4a07e70Sfengbojiang * 3. Neither the name of the University nor the names of its contributors 211eaf0ac3Slogwang * may be used to endorse or promote products derived from this software 221eaf0ac3Slogwang * without specific prior written permission. 231eaf0ac3Slogwang * 241eaf0ac3Slogwang * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 251eaf0ac3Slogwang * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 261eaf0ac3Slogwang * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 271eaf0ac3Slogwang * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 281eaf0ac3Slogwang * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 291eaf0ac3Slogwang * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 301eaf0ac3Slogwang * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 311eaf0ac3Slogwang * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 321eaf0ac3Slogwang * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 331eaf0ac3Slogwang * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 341eaf0ac3Slogwang * SUCH DAMAGE. 351eaf0ac3Slogwang * 361eaf0ac3Slogwang * @(#)bpfdesc.h 8.1 (Berkeley) 6/10/93 371eaf0ac3Slogwang * 381eaf0ac3Slogwang * $FreeBSD$ 391eaf0ac3Slogwang */ 401eaf0ac3Slogwang 411eaf0ac3Slogwang #ifndef _NET_BPFDESC_H_ 421eaf0ac3Slogwang #define _NET_BPFDESC_H_ 431eaf0ac3Slogwang 441eaf0ac3Slogwang #include <sys/callout.h> 451eaf0ac3Slogwang #include <sys/selinfo.h> 46*d4a07e70Sfengbojiang #include <sys/ck.h> 47*d4a07e70Sfengbojiang #include <sys/counter.h> 48*d4a07e70Sfengbojiang #include <sys/epoch.h> 491eaf0ac3Slogwang #include <net/if.h> 501eaf0ac3Slogwang 511eaf0ac3Slogwang /* 521eaf0ac3Slogwang * Descriptor associated with each open bpf file. 531eaf0ac3Slogwang */ 541eaf0ac3Slogwang struct zbuf; 551eaf0ac3Slogwang struct bpf_d { 56*d4a07e70Sfengbojiang CK_LIST_ENTRY(bpf_d) bd_next; /* Linked list of descriptors */ 571eaf0ac3Slogwang /* 581eaf0ac3Slogwang * Buffer slots: two memory buffers store the incoming packets. 591eaf0ac3Slogwang * The model has three slots. Sbuf is always occupied. 601eaf0ac3Slogwang * sbuf (store) - Receive interrupt puts packets here. 611eaf0ac3Slogwang * hbuf (hold) - When sbuf is full, put buffer here and 621eaf0ac3Slogwang * wakeup read (replace sbuf with fbuf). 631eaf0ac3Slogwang * fbuf (free) - When read is done, put buffer here. 641eaf0ac3Slogwang * On receiving, if sbuf is full and fbuf is 0, packet is dropped. 651eaf0ac3Slogwang */ 661eaf0ac3Slogwang caddr_t bd_sbuf; /* store slot */ 671eaf0ac3Slogwang caddr_t bd_hbuf; /* hold slot */ 681eaf0ac3Slogwang caddr_t bd_fbuf; /* free slot */ 691eaf0ac3Slogwang int bd_hbuf_in_use; /* don't rotate buffers */ 701eaf0ac3Slogwang int bd_slen; /* current length of store buffer */ 711eaf0ac3Slogwang int bd_hlen; /* current length of hold buffer */ 721eaf0ac3Slogwang 731eaf0ac3Slogwang int bd_bufsize; /* absolute length of buffers */ 741eaf0ac3Slogwang 751eaf0ac3Slogwang struct bpf_if * bd_bif; /* interface descriptor */ 761eaf0ac3Slogwang u_long bd_rtout; /* Read timeout in 'ticks' */ 771eaf0ac3Slogwang struct bpf_insn *bd_rfilter; /* read filter code */ 781eaf0ac3Slogwang struct bpf_insn *bd_wfilter; /* write filter code */ 791eaf0ac3Slogwang void *bd_bfilter; /* binary filter code */ 80*d4a07e70Sfengbojiang counter_u64_t bd_rcount; /* number of packets received */ 81*d4a07e70Sfengbojiang counter_u64_t bd_dcount; /* number of packets dropped */ 821eaf0ac3Slogwang 831eaf0ac3Slogwang u_char bd_promisc; /* true if listening promiscuously */ 841eaf0ac3Slogwang u_char bd_state; /* idle, waiting, or timed out */ 851eaf0ac3Slogwang u_char bd_immediate; /* true to return on packet arrival */ 861eaf0ac3Slogwang u_char bd_writer; /* non-zero if d is writer-only */ 871eaf0ac3Slogwang int bd_hdrcmplt; /* false to fill in src lladdr automatically */ 881eaf0ac3Slogwang int bd_direction; /* select packet direction */ 891eaf0ac3Slogwang int bd_tstamp; /* select time stamping function */ 901eaf0ac3Slogwang int bd_feedback; /* true to feed back sent packets */ 911eaf0ac3Slogwang int bd_async; /* non-zero if packet reception should generate signal */ 921eaf0ac3Slogwang int bd_sig; /* signal to send upon packet reception */ 931eaf0ac3Slogwang struct sigio * bd_sigio; /* information for async I/O */ 941eaf0ac3Slogwang struct selinfo bd_sel; /* bsd select info */ 951eaf0ac3Slogwang struct mtx bd_lock; /* per-descriptor lock */ 961eaf0ac3Slogwang struct callout bd_callout; /* for BPF timeouts with select */ 971eaf0ac3Slogwang struct label *bd_label; /* MAC label for descriptor */ 98*d4a07e70Sfengbojiang counter_u64_t bd_fcount; /* number of packets which matched filter */ 991eaf0ac3Slogwang pid_t bd_pid; /* PID which created descriptor */ 1001eaf0ac3Slogwang int bd_locked; /* true if descriptor is locked */ 1011eaf0ac3Slogwang u_int bd_bufmode; /* Current buffer mode. */ 102*d4a07e70Sfengbojiang counter_u64_t bd_wcount; /* number of packets written */ 103*d4a07e70Sfengbojiang counter_u64_t bd_wfcount; /* number of packets that matched write filter */ 104*d4a07e70Sfengbojiang counter_u64_t bd_wdcount; /* number of packets dropped during a write */ 105*d4a07e70Sfengbojiang counter_u64_t bd_zcopy; /* number of zero copy operations */ 1061eaf0ac3Slogwang u_char bd_compat32; /* 32-bit stream on LP64 system */ 107*d4a07e70Sfengbojiang 108*d4a07e70Sfengbojiang volatile u_int bd_refcnt; 109*d4a07e70Sfengbojiang struct epoch_context epoch_ctx; 1101eaf0ac3Slogwang }; 1111eaf0ac3Slogwang 1121eaf0ac3Slogwang /* Values for bd_state */ 1131eaf0ac3Slogwang #define BPF_IDLE 0 /* no select in progress */ 1141eaf0ac3Slogwang #define BPF_WAITING 1 /* waiting for read timeout in select */ 1151eaf0ac3Slogwang #define BPF_TIMED_OUT 2 /* read timeout has expired in select */ 1161eaf0ac3Slogwang 1171eaf0ac3Slogwang #define BPFD_LOCK(bd) mtx_lock(&(bd)->bd_lock) 1181eaf0ac3Slogwang #define BPFD_UNLOCK(bd) mtx_unlock(&(bd)->bd_lock) 1191eaf0ac3Slogwang #define BPFD_LOCK_ASSERT(bd) mtx_assert(&(bd)->bd_lock, MA_OWNED) 1201eaf0ac3Slogwang 1211eaf0ac3Slogwang #define BPF_PID_REFRESH(bd, td) (bd)->bd_pid = (td)->td_proc->p_pid 1221eaf0ac3Slogwang #define BPF_PID_REFRESH_CUR(bd) (bd)->bd_pid = curthread->td_proc->p_pid 1231eaf0ac3Slogwang 1241eaf0ac3Slogwang /* 1251eaf0ac3Slogwang * External representation of the bpf descriptor 1261eaf0ac3Slogwang */ 1271eaf0ac3Slogwang struct xbpf_d { 1281eaf0ac3Slogwang u_int bd_structsize; /* Size of this structure. */ 1291eaf0ac3Slogwang u_char bd_promisc; 1301eaf0ac3Slogwang u_char bd_immediate; 1311eaf0ac3Slogwang u_char __bd_pad[6]; 1321eaf0ac3Slogwang int bd_hdrcmplt; 1331eaf0ac3Slogwang int bd_direction; 1341eaf0ac3Slogwang int bd_feedback; 1351eaf0ac3Slogwang int bd_async; 1361eaf0ac3Slogwang u_int64_t bd_rcount; 1371eaf0ac3Slogwang u_int64_t bd_dcount; 1381eaf0ac3Slogwang u_int64_t bd_fcount; 1391eaf0ac3Slogwang int bd_sig; 1401eaf0ac3Slogwang int bd_slen; 1411eaf0ac3Slogwang int bd_hlen; 1421eaf0ac3Slogwang int bd_bufsize; 1431eaf0ac3Slogwang pid_t bd_pid; 1441eaf0ac3Slogwang char bd_ifname[IFNAMSIZ]; 1451eaf0ac3Slogwang int bd_locked; 1461eaf0ac3Slogwang u_int64_t bd_wcount; 1471eaf0ac3Slogwang u_int64_t bd_wfcount; 1481eaf0ac3Slogwang u_int64_t bd_wdcount; 1491eaf0ac3Slogwang u_int64_t bd_zcopy; 1501eaf0ac3Slogwang int bd_bufmode; 1511eaf0ac3Slogwang /* 1521eaf0ac3Slogwang * Allocate 4 64 bit unsigned integers for future expansion so we do 1531eaf0ac3Slogwang * not have to worry about breaking the ABI. 1541eaf0ac3Slogwang */ 1551eaf0ac3Slogwang u_int64_t bd_spare[4]; 1561eaf0ac3Slogwang }; 1571eaf0ac3Slogwang 1581eaf0ac3Slogwang #define BPFIF_FLAG_DYING 1 /* Reject new bpf consumers */ 1591eaf0ac3Slogwang 1601eaf0ac3Slogwang #endif 161