bpfdesc.h revision 127541
11541Srgrimes/*
21541Srgrimes * Copyright (c) 1990, 1991, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * This code is derived from the Stanford/CMU enet packet filter,
61541Srgrimes * (net/enet.c) distributed as part of 4.3BSD, and code contributed
71541Srgrimes * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
81541Srgrimes * Berkeley Laboratory.
91541Srgrimes *
101541Srgrimes * Redistribution and use in source and binary forms, with or without
111541Srgrimes * modification, are permitted provided that the following conditions
121541Srgrimes * are met:
131541Srgrimes * 1. Redistributions of source code must retain the above copyright
141541Srgrimes *    notice, this list of conditions and the following disclaimer.
151541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
161541Srgrimes *    notice, this list of conditions and the following disclaimer in the
171541Srgrimes *    documentation and/or other materials provided with the distribution.
181541Srgrimes * 3. All advertising materials mentioning features or use of this software
191541Srgrimes *    must display the following acknowledgement:
201541Srgrimes *	This product includes software developed by the University of
211541Srgrimes *	California, Berkeley and its contributors.
221541Srgrimes * 4. Neither the name of the University nor the names of its contributors
231541Srgrimes *    may be used to endorse or promote products derived from this software
241541Srgrimes *    without specific prior written permission.
251541Srgrimes *
261541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
271541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
281541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
291541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
301541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
311541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
321541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
331541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
341541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
351541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
361541Srgrimes * SUCH DAMAGE.
371541Srgrimes *
381541Srgrimes *      @(#)bpfdesc.h	8.1 (Berkeley) 6/10/93
391541Srgrimes *
4050477Speter * $FreeBSD: head/sys/net/bpfdesc.h 127541 2004-03-29 00:33:39Z rwatson $
411541Srgrimes */
421541Srgrimes
432168Spaul#ifndef _NET_BPFDESC_H_
442168Spaul#define _NET_BPFDESC_H_
452168Spaul
4687955Sjdp#include <sys/callout.h>
4770834Swollman#include <sys/selinfo.h>
481549Srgrimes
491541Srgrimes/*
501541Srgrimes * Descriptor associated with each open bpf file.
511541Srgrimes */
521541Srgrimesstruct bpf_d {
531541Srgrimes	struct bpf_d	*bd_next;	/* Linked list of descriptors */
541541Srgrimes	/*
551541Srgrimes	 * Buffer slots: two mbuf clusters buffer the incoming packets.
561541Srgrimes	 *   The model has three slots.  Sbuf is always occupied.
571541Srgrimes	 *   sbuf (store) - Receive interrupt puts packets here.
581541Srgrimes	 *   hbuf (hold) - When sbuf is full, put cluster here and
591541Srgrimes	 *                 wakeup read (replace sbuf with fbuf).
601541Srgrimes	 *   fbuf (free) - When read is done, put cluster here.
611541Srgrimes	 * On receiving, if sbuf is full and fbuf is 0, packet is dropped.
621541Srgrimes	 */
631541Srgrimes	caddr_t		bd_sbuf;	/* store slot */
641541Srgrimes	caddr_t		bd_hbuf;	/* hold slot */
651541Srgrimes	caddr_t		bd_fbuf;	/* free slot */
661541Srgrimes	int 		bd_slen;	/* current length of store buffer */
671541Srgrimes	int 		bd_hlen;	/* current length of hold buffer */
681541Srgrimes
691541Srgrimes	int		bd_bufsize;	/* absolute length of buffers */
701541Srgrimes
711541Srgrimes	struct bpf_if *	bd_bif;		/* interface descriptor */
721541Srgrimes	u_long		bd_rtout;	/* Read timeout in 'ticks' */
731541Srgrimes	struct bpf_insn *bd_filter; 	/* filter code */
741541Srgrimes	u_long		bd_rcount;	/* number of packets received */
751541Srgrimes	u_long		bd_dcount;	/* number of packets dropped */
761541Srgrimes
771541Srgrimes	u_char		bd_promisc;	/* true if listening promiscuously */
781541Srgrimes	u_char		bd_state;	/* idle, waiting, or timed out */
791541Srgrimes	u_char		bd_immediate;	/* true to return on packet arrival */
8052248Smsmith	int		bd_hdrcmplt;	/* false to fill in src lladdr automatically */
8158192Srwatson	int		bd_seesent;	/* true if bpf should see sent packets */
829235Spst	int		bd_async;	/* non-zero if packet reception should generate signal */
839235Spst	int		bd_sig;		/* signal to send upon packet reception */
8441087Struckman	struct sigio *	bd_sigio;	/* information for async I/O */
851541Srgrimes#if BSD < 199103
861541Srgrimes	u_char		bd_selcoll;	/* true if selects collide */
871541Srgrimes	int		bd_timedout;
8883366Sjulian	struct thread *	bd_selthread;	/* process that last selected us */
891541Srgrimes#else
901541Srgrimes	u_char		bd_pad;		/* explicit alignment */
911541Srgrimes	struct selinfo	bd_sel;		/* bsd select info */
921541Srgrimes#endif
9372544Sjlemon	struct mtx	bd_mtx;		/* mutex for this descriptor */
9487955Sjdp	struct callout	bd_callout;	/* for BPF timeouts with select */
95122524Srwatson	struct label	*bd_label;	/* MAC label for descriptor */
961541Srgrimes};
971541Srgrimes
9887955Sjdp/* Values for bd_state */
9987955Sjdp#define BPF_IDLE	0		/* no select in progress */
10087955Sjdp#define BPF_WAITING	1		/* waiting for read timeout in select */
10187955Sjdp#define BPF_TIMED_OUT	2		/* read timeout has expired in select */
10287955Sjdp
10372544Sjlemon#define BPFD_LOCK(bd)		mtx_lock(&(bd)->bd_mtx)
10472544Sjlemon#define BPFD_UNLOCK(bd)		mtx_unlock(&(bd)->bd_mtx)
105127541Srwatson#define BPFD_LOCK_ASSERT(bd)	do {				\
106127541Srwatson	mtx_assert(&(bd)->bd_mtx, MA_OWNED);			\
107127541Srwatson	NET_ASSERT_GIANT();					\
108127541Srwatson} while (0)
10972544Sjlemon
110118471Sjmg/* Test whether a BPF is ready for read(). */
111118471Sjmg#define	bpf_ready(bd)						 \
112118471Sjmg	((bd)->bd_hlen != 0 ||					 \
113118471Sjmg	 (((bd)->bd_immediate || (bd)->bd_state == BPF_TIMED_OUT) && \
114118471Sjmg	  (bd)->bd_slen != 0))
115118471Sjmg
1161541Srgrimes/*
1171541Srgrimes * Descriptor associated with each attached hardware interface.
1181541Srgrimes */
1191541Srgrimesstruct bpf_if {
1201541Srgrimes	struct bpf_if *bif_next;	/* list of all interfaces */
1211541Srgrimes	struct bpf_d *bif_dlist;	/* descriptor list */
122106927Ssam	struct bpf_if **bif_driverp;	/* pointer into softc */
1231541Srgrimes	u_int bif_dlt;			/* link layer type */
1241541Srgrimes	u_int bif_hdrlen;		/* length of header (with padding) */
12513765Smpp	struct ifnet *bif_ifp;		/* corresponding interface */
12672544Sjlemon	struct mtx	bif_mtx;	/* mutex for interface */
1271541Srgrimes};
1281541Srgrimes
12972544Sjlemon#define BPFIF_LOCK(bif)		mtx_lock(&(bif)->bif_mtx)
13072544Sjlemon#define BPFIF_UNLOCK(bif)	mtx_unlock(&(bif)->bif_mtx)
13172544Sjlemon
1321541Srgrimes#endif
133