bpfdesc.h revision 148366
1139823Simp/*-
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 * 4. Neither the name of the University nor the names of its contributors
191541Srgrimes *    may be used to endorse or promote products derived from this software
201541Srgrimes *    without specific prior written permission.
211541Srgrimes *
221541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
231541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
241541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
251541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
261541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
271541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
281541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
291541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
301541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
311541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
321541Srgrimes * SUCH DAMAGE.
331541Srgrimes *
341541Srgrimes *      @(#)bpfdesc.h	8.1 (Berkeley) 6/10/93
351541Srgrimes *
3650477Speter * $FreeBSD: head/sys/net/bpfdesc.h 148366 2005-07-24 17:21:17Z csjp $
371541Srgrimes */
381541Srgrimes
392168Spaul#ifndef _NET_BPFDESC_H_
402168Spaul#define _NET_BPFDESC_H_
412168Spaul
4287955Sjdp#include <sys/callout.h>
4370834Swollman#include <sys/selinfo.h>
44134967Srwatson#include <sys/queue.h>
45148366Scsjp#include <sys/conf.h>
46148366Scsjp#include <net/if.h>
471549Srgrimes
481541Srgrimes/*
491541Srgrimes * Descriptor associated with each open bpf file.
501541Srgrimes */
511541Srgrimesstruct bpf_d {
52134967Srwatson	LIST_ENTRY(bpf_d) bd_next;	/* Linked list of descriptors */
531541Srgrimes	/*
541541Srgrimes	 * Buffer slots: two mbuf clusters buffer the incoming packets.
551541Srgrimes	 *   The model has three slots.  Sbuf is always occupied.
561541Srgrimes	 *   sbuf (store) - Receive interrupt puts packets here.
571541Srgrimes	 *   hbuf (hold) - When sbuf is full, put cluster here and
581541Srgrimes	 *                 wakeup read (replace sbuf with fbuf).
591541Srgrimes	 *   fbuf (free) - When read is done, put cluster here.
601541Srgrimes	 * On receiving, if sbuf is full and fbuf is 0, packet is dropped.
611541Srgrimes	 */
621541Srgrimes	caddr_t		bd_sbuf;	/* store slot */
631541Srgrimes	caddr_t		bd_hbuf;	/* hold slot */
641541Srgrimes	caddr_t		bd_fbuf;	/* free slot */
651541Srgrimes	int 		bd_slen;	/* current length of store buffer */
661541Srgrimes	int 		bd_hlen;	/* current length of hold buffer */
671541Srgrimes
681541Srgrimes	int		bd_bufsize;	/* absolute length of buffers */
691541Srgrimes
701541Srgrimes	struct bpf_if *	bd_bif;		/* interface descriptor */
711541Srgrimes	u_long		bd_rtout;	/* Read timeout in 'ticks' */
721541Srgrimes	struct bpf_insn *bd_filter; 	/* filter code */
731541Srgrimes	u_long		bd_rcount;	/* number of packets received */
741541Srgrimes	u_long		bd_dcount;	/* number of packets dropped */
751541Srgrimes
761541Srgrimes	u_char		bd_promisc;	/* true if listening promiscuously */
771541Srgrimes	u_char		bd_state;	/* idle, waiting, or timed out */
781541Srgrimes	u_char		bd_immediate;	/* true to return on packet arrival */
7952248Smsmith	int		bd_hdrcmplt;	/* false to fill in src lladdr automatically */
8058192Srwatson	int		bd_seesent;	/* true if bpf should see sent packets */
819235Spst	int		bd_async;	/* non-zero if packet reception should generate signal */
829235Spst	int		bd_sig;		/* signal to send upon packet reception */
8341087Struckman	struct sigio *	bd_sigio;	/* information for async I/O */
841541Srgrimes#if BSD < 199103
851541Srgrimes	u_char		bd_selcoll;	/* true if selects collide */
861541Srgrimes	int		bd_timedout;
8783366Sjulian	struct thread *	bd_selthread;	/* process that last selected us */
881541Srgrimes#else
891541Srgrimes	u_char		bd_pad;		/* explicit alignment */
901541Srgrimes	struct selinfo	bd_sel;		/* bsd select info */
911541Srgrimes#endif
9272544Sjlemon	struct mtx	bd_mtx;		/* mutex for this descriptor */
9387955Sjdp	struct callout	bd_callout;	/* for BPF timeouts with select */
94122524Srwatson	struct label	*bd_label;	/* MAC label for descriptor */
95148366Scsjp	u_long		bd_fcount;	/* number of packets which matched filter */
96148366Scsjp	pid_t		bd_pid;		/* PID which created descriptor */
97148366Scsjp	char		bd_pcomm[MAXCOMLEN + 1];
981541Srgrimes};
991541Srgrimes
10087955Sjdp/* Values for bd_state */
10187955Sjdp#define BPF_IDLE	0		/* no select in progress */
10287955Sjdp#define BPF_WAITING	1		/* waiting for read timeout in select */
10387955Sjdp#define BPF_TIMED_OUT	2		/* read timeout has expired in select */
10487955Sjdp
10572544Sjlemon#define BPFD_LOCK(bd)		mtx_lock(&(bd)->bd_mtx)
10672544Sjlemon#define BPFD_UNLOCK(bd)		mtx_unlock(&(bd)->bd_mtx)
107127541Srwatson#define BPFD_LOCK_ASSERT(bd)	do {				\
108127541Srwatson	mtx_assert(&(bd)->bd_mtx, MA_OWNED);			\
109127541Srwatson	NET_ASSERT_GIANT();					\
110127541Srwatson} while (0)
11172544Sjlemon
112118471Sjmg/* Test whether a BPF is ready for read(). */
113118471Sjmg#define	bpf_ready(bd)						 \
114118471Sjmg	((bd)->bd_hlen != 0 ||					 \
115118471Sjmg	 (((bd)->bd_immediate || (bd)->bd_state == BPF_TIMED_OUT) && \
116118471Sjmg	  (bd)->bd_slen != 0))
117118471Sjmg
1181541Srgrimes/*
1191541Srgrimes * Descriptor associated with each attached hardware interface.
1201541Srgrimes */
1211541Srgrimesstruct bpf_if {
122134967Srwatson	LIST_ENTRY(bpf_if)	bif_next;	/* list of all interfaces */
123134967Srwatson	LIST_HEAD(, bpf_d)	bif_dlist;	/* descriptor list */
124106927Ssam	struct bpf_if **bif_driverp;	/* pointer into softc */
1251541Srgrimes	u_int bif_dlt;			/* link layer type */
1261541Srgrimes	u_int bif_hdrlen;		/* length of header (with padding) */
12713765Smpp	struct ifnet *bif_ifp;		/* corresponding interface */
12872544Sjlemon	struct mtx	bif_mtx;	/* mutex for interface */
1291541Srgrimes};
1301541Srgrimes
131148366Scsjp/*
132148366Scsjp * External representation of the bpf descriptor
133148366Scsjp */
134148366Scsjpstruct xbpf_d {
135148366Scsjp	u_char		bd_promisc;
136148366Scsjp	u_char		bd_immediate;
137148366Scsjp	int		bd_hdrcmplt;
138148366Scsjp	int		bd_seesent;
139148366Scsjp	int		bd_async;
140148366Scsjp	u_long		bd_rcount;
141148366Scsjp	u_long		bd_dcount;
142148366Scsjp	u_long		bd_fcount;
143148366Scsjp	int		bd_sig;
144148366Scsjp	int		bd_slen;
145148366Scsjp	int		bd_hlen;
146148366Scsjp	int		bd_bufsize;
147148366Scsjp	pid_t		bd_pid;
148148366Scsjp	char		bd_ifname[IFNAMSIZ];
149148366Scsjp	char		bd_pcomm[MAXCOMLEN + 1];
150148366Scsjp};
151148366Scsjp
15272544Sjlemon#define BPFIF_LOCK(bif)		mtx_lock(&(bif)->bif_mtx)
15372544Sjlemon#define BPFIF_UNLOCK(bif)	mtx_unlock(&(bif)->bif_mtx)
15472544Sjlemon
1551541Srgrimes#endif
156