1c398230bSWarner Losh /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1990, 1991, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * This code is derived from the Stanford/CMU enet packet filter, 6df8bae1dSRodney W. Grimes * (net/enet.c) distributed as part of 4.3BSD, and code contributed 7df8bae1dSRodney W. Grimes * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence 8df8bae1dSRodney W. Grimes * Berkeley Laboratory. 9df8bae1dSRodney W. Grimes * 10df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 11df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 12df8bae1dSRodney W. Grimes * are met: 13df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 15df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 17df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 18df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 19df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 20df8bae1dSRodney W. Grimes * without specific prior written permission. 21df8bae1dSRodney W. Grimes * 22df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32df8bae1dSRodney W. Grimes * SUCH DAMAGE. 33df8bae1dSRodney W. Grimes * 34df8bae1dSRodney W. Grimes * @(#)bpfdesc.h 8.1 (Berkeley) 6/10/93 35df8bae1dSRodney W. Grimes * 36c3aac50fSPeter Wemm * $FreeBSD$ 37df8bae1dSRodney W. Grimes */ 38df8bae1dSRodney W. Grimes 39cea1da3bSPaul Richards #ifndef _NET_BPFDESC_H_ 40cea1da3bSPaul Richards #define _NET_BPFDESC_H_ 41cea1da3bSPaul Richards 4281bda851SJohn Polstra #include <sys/callout.h> 430a2c3d48SGarrett Wollman #include <sys/selinfo.h> 444a3feeaaSRobert Watson #include <sys/queue.h> 4569f7644bSChristian S.J. Peron #include <sys/conf.h> 4669f7644bSChristian S.J. Peron #include <net/if.h> 4726f9a767SRodney W. Grimes 48df8bae1dSRodney W. Grimes /* 49df8bae1dSRodney W. Grimes * Descriptor associated with each open bpf file. 50df8bae1dSRodney W. Grimes */ 51df8bae1dSRodney W. Grimes struct bpf_d { 524a3feeaaSRobert Watson LIST_ENTRY(bpf_d) bd_next; /* Linked list of descriptors */ 53df8bae1dSRodney W. Grimes /* 546d38c5adSRobert Watson * Buffer slots: two malloc buffers store the incoming packets. 55df8bae1dSRodney W. Grimes * The model has three slots. Sbuf is always occupied. 56df8bae1dSRodney W. Grimes * sbuf (store) - Receive interrupt puts packets here. 576d38c5adSRobert Watson * hbuf (hold) - When sbuf is full, put buffer here and 58df8bae1dSRodney W. Grimes * wakeup read (replace sbuf with fbuf). 596d38c5adSRobert Watson * fbuf (free) - When read is done, put buffer here. 60df8bae1dSRodney W. Grimes * On receiving, if sbuf is full and fbuf is 0, packet is dropped. 61df8bae1dSRodney W. Grimes */ 62df8bae1dSRodney W. Grimes caddr_t bd_sbuf; /* store slot */ 63df8bae1dSRodney W. Grimes caddr_t bd_hbuf; /* hold slot */ 64df8bae1dSRodney W. Grimes caddr_t bd_fbuf; /* free slot */ 65df8bae1dSRodney W. Grimes int bd_slen; /* current length of store buffer */ 66df8bae1dSRodney W. Grimes int bd_hlen; /* current length of hold buffer */ 67df8bae1dSRodney W. Grimes 68df8bae1dSRodney W. Grimes int bd_bufsize; /* absolute length of buffers */ 69df8bae1dSRodney W. Grimes 70df8bae1dSRodney W. Grimes struct bpf_if * bd_bif; /* interface descriptor */ 71df8bae1dSRodney W. Grimes u_long bd_rtout; /* Read timeout in 'ticks' */ 7293e39f0bSChristian S.J. Peron struct bpf_insn *bd_rfilter; /* read filter code */ 7393e39f0bSChristian S.J. Peron struct bpf_insn *bd_wfilter; /* write filter code */ 74ae275efcSJung-uk Kim #ifdef BPF_JITTER 75ae275efcSJung-uk Kim bpf_jit_filter *bd_bfilter; /* binary filter code */ 76ae275efcSJung-uk Kim #endif 77df8bae1dSRodney W. Grimes u_long bd_rcount; /* number of packets received */ 78df8bae1dSRodney W. Grimes u_long bd_dcount; /* number of packets dropped */ 79df8bae1dSRodney W. Grimes 80df8bae1dSRodney W. Grimes u_char bd_promisc; /* true if listening promiscuously */ 81df8bae1dSRodney W. Grimes u_char bd_state; /* idle, waiting, or timed out */ 82df8bae1dSRodney W. Grimes u_char bd_immediate; /* true to return on packet arrival */ 83114ae644SMike Smith int bd_hdrcmplt; /* false to fill in src lladdr automatically */ 848ed3828cSRobert Watson int bd_seesent; /* true if bpf should see sent packets */ 8500a83887SPaul Traina int bd_async; /* non-zero if packet reception should generate signal */ 8600a83887SPaul Traina int bd_sig; /* signal to send upon packet reception */ 8762d6ce3aSDon Lewis struct sigio * bd_sigio; /* information for async I/O */ 88df8bae1dSRodney W. Grimes struct selinfo bd_sel; /* bsd select info */ 89e7bb21b3SJonathan Lemon struct mtx bd_mtx; /* mutex for this descriptor */ 9081bda851SJohn Polstra struct callout bd_callout; /* for BPF timeouts with select */ 91eca8a663SRobert Watson struct label *bd_label; /* MAC label for descriptor */ 9269f7644bSChristian S.J. Peron u_long bd_fcount; /* number of packets which matched filter */ 9369f7644bSChristian S.J. Peron pid_t bd_pid; /* PID which created descriptor */ 9493e39f0bSChristian S.J. Peron int bd_locked; /* true if descriptor is locked */ 95df8bae1dSRodney W. Grimes }; 96df8bae1dSRodney W. Grimes 9781bda851SJohn Polstra /* Values for bd_state */ 9881bda851SJohn Polstra #define BPF_IDLE 0 /* no select in progress */ 9981bda851SJohn Polstra #define BPF_WAITING 1 /* waiting for read timeout in select */ 10081bda851SJohn Polstra #define BPF_TIMED_OUT 2 /* read timeout has expired in select */ 10181bda851SJohn Polstra 102e7bb21b3SJonathan Lemon #define BPFD_LOCK(bd) mtx_lock(&(bd)->bd_mtx) 103e7bb21b3SJonathan Lemon #define BPFD_UNLOCK(bd) mtx_unlock(&(bd)->bd_mtx) 1042418d3ccSRobert Watson #define BPFD_LOCK_ASSERT(bd) do { \ 1052418d3ccSRobert Watson mtx_assert(&(bd)->bd_mtx, MA_OWNED); \ 1062418d3ccSRobert Watson NET_ASSERT_GIANT(); \ 1072418d3ccSRobert Watson } while (0) 108e7bb21b3SJonathan Lemon 10995aab9ccSJohn-Mark Gurney /* Test whether a BPF is ready for read(). */ 11095aab9ccSJohn-Mark Gurney #define bpf_ready(bd) \ 11195aab9ccSJohn-Mark Gurney ((bd)->bd_hlen != 0 || \ 11295aab9ccSJohn-Mark Gurney (((bd)->bd_immediate || (bd)->bd_state == BPF_TIMED_OUT) && \ 11395aab9ccSJohn-Mark Gurney (bd)->bd_slen != 0)) 11495aab9ccSJohn-Mark Gurney 115df8bae1dSRodney W. Grimes /* 11669f7644bSChristian S.J. Peron * External representation of the bpf descriptor 11769f7644bSChristian S.J. Peron */ 11869f7644bSChristian S.J. Peron struct xbpf_d { 11969f7644bSChristian S.J. Peron u_char bd_promisc; 12069f7644bSChristian S.J. Peron u_char bd_immediate; 12169f7644bSChristian S.J. Peron int bd_hdrcmplt; 12269f7644bSChristian S.J. Peron int bd_seesent; 12369f7644bSChristian S.J. Peron int bd_async; 12469f7644bSChristian S.J. Peron u_long bd_rcount; 12569f7644bSChristian S.J. Peron u_long bd_dcount; 12669f7644bSChristian S.J. Peron u_long bd_fcount; 12769f7644bSChristian S.J. Peron int bd_sig; 12869f7644bSChristian S.J. Peron int bd_slen; 12969f7644bSChristian S.J. Peron int bd_hlen; 13069f7644bSChristian S.J. Peron int bd_bufsize; 13169f7644bSChristian S.J. Peron pid_t bd_pid; 13269f7644bSChristian S.J. Peron char bd_ifname[IFNAMSIZ]; 13393e39f0bSChristian S.J. Peron int bd_locked; 13469f7644bSChristian S.J. Peron }; 13569f7644bSChristian S.J. Peron 136e7bb21b3SJonathan Lemon #define BPFIF_LOCK(bif) mtx_lock(&(bif)->bif_mtx) 137e7bb21b3SJonathan Lemon #define BPFIF_UNLOCK(bif) mtx_unlock(&(bif)->bif_mtx) 138e7bb21b3SJonathan Lemon 139cea1da3bSPaul Richards #endif 140