bpfdesc.h revision 167035
1238106Sdes/*-
2238106Sdes * Copyright (c) 1990, 1991, 1993
3238106Sdes *	The Regents of the University of California.  All rights reserved.
4238106Sdes *
5238106Sdes * This code is derived from the Stanford/CMU enet packet filter,
6238106Sdes * (net/enet.c) distributed as part of 4.3BSD, and code contributed
7238106Sdes * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
8238106Sdes * Berkeley Laboratory.
9238106Sdes *
10238106Sdes * Redistribution and use in source and binary forms, with or without
11238106Sdes * modification, are permitted provided that the following conditions
12238106Sdes * are met:
13238106Sdes * 1. Redistributions of source code must retain the above copyright
14238106Sdes *    notice, this list of conditions and the following disclaimer.
15238106Sdes * 2. Redistributions in binary form must reproduce the above copyright
16238106Sdes *    notice, this list of conditions and the following disclaimer in the
17238106Sdes *    documentation and/or other materials provided with the distribution.
18238106Sdes * 4. Neither the name of the University nor the names of its contributors
19238106Sdes *    may be used to endorse or promote products derived from this software
20238106Sdes *    without specific prior written permission.
21238106Sdes *
22238106Sdes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23238106Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24238106Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25238106Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26238106Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27238106Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28238106Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29238106Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30238106Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31238106Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32238106Sdes * SUCH DAMAGE.
33238106Sdes *
34238106Sdes *      @(#)bpfdesc.h	8.1 (Berkeley) 6/10/93
35238106Sdes *
36238106Sdes * $FreeBSD: head/sys/net/bpfdesc.h 167035 2007-02-26 22:24:14Z jkim $
37238106Sdes */
38238106Sdes
39238106Sdes#ifndef _NET_BPFDESC_H_
40238106Sdes#define _NET_BPFDESC_H_
41238106Sdes
42238106Sdes#include <sys/callout.h>
43238106Sdes#include <sys/selinfo.h>
44238106Sdes#include <sys/queue.h>
45238106Sdes#include <sys/conf.h>
46238106Sdes#include <net/if.h>
47238106Sdes
48238106Sdes/*
49238106Sdes * Descriptor associated with each open bpf file.
50238106Sdes */
51238106Sdesstruct bpf_d {
52238106Sdes	LIST_ENTRY(bpf_d) bd_next;	/* Linked list of descriptors */
53238106Sdes	/*
54238106Sdes	 * Buffer slots: two malloc buffers store the incoming packets.
55238106Sdes	 *   The model has three slots.  Sbuf is always occupied.
56238106Sdes	 *   sbuf (store) - Receive interrupt puts packets here.
57238106Sdes	 *   hbuf (hold) - When sbuf is full, put buffer here and
58238106Sdes	 *                 wakeup read (replace sbuf with fbuf).
59238106Sdes	 *   fbuf (free) - When read is done, put buffer here.
60238106Sdes	 * On receiving, if sbuf is full and fbuf is 0, packet is dropped.
61238106Sdes	 */
62238106Sdes	caddr_t		bd_sbuf;	/* store slot */
63238106Sdes	caddr_t		bd_hbuf;	/* hold slot */
64238106Sdes	caddr_t		bd_fbuf;	/* free slot */
65238106Sdes	int 		bd_slen;	/* current length of store buffer */
66238106Sdes	int 		bd_hlen;	/* current length of hold buffer */
67238106Sdes
68238106Sdes	int		bd_bufsize;	/* absolute length of buffers */
69238106Sdes
70238106Sdes	struct bpf_if *	bd_bif;		/* interface descriptor */
71238106Sdes	u_long		bd_rtout;	/* Read timeout in 'ticks' */
72238106Sdes	struct bpf_insn *bd_rfilter; 	/* read filter code */
73238106Sdes	struct bpf_insn *bd_wfilter;	/* write filter code */
74238106Sdes#ifdef BPF_JITTER
75238106Sdes	bpf_jit_filter	*bd_bfilter;	/* binary filter code */
76238106Sdes#endif
77238106Sdes	u_long		bd_rcount;	/* number of packets received */
78238106Sdes	u_long		bd_dcount;	/* number of packets dropped */
79238106Sdes
80238106Sdes	u_char		bd_promisc;	/* true if listening promiscuously */
81238106Sdes	u_char		bd_state;	/* idle, waiting, or timed out */
82238106Sdes	u_char		bd_immediate;	/* true to return on packet arrival */
83238106Sdes	int		bd_hdrcmplt;	/* false to fill in src lladdr automatically */
84238106Sdes	int		bd_direction;	/* select packet direction */
85238106Sdes	int		bd_feedback;	/* true to feed back sent packets */
86238106Sdes	int		bd_async;	/* non-zero if packet reception should generate signal */
87238106Sdes	int		bd_sig;		/* signal to send upon packet reception */
88238106Sdes	struct sigio *	bd_sigio;	/* information for async I/O */
89238106Sdes	struct selinfo	bd_sel;		/* bsd select info */
90238106Sdes	struct mtx	bd_mtx;		/* mutex for this descriptor */
91238106Sdes	struct callout	bd_callout;	/* for BPF timeouts with select */
92238106Sdes	struct label	*bd_label;	/* MAC label for descriptor */
93238106Sdes	u_long		bd_fcount;	/* number of packets which matched filter */
94238106Sdes	pid_t		bd_pid;		/* PID which created descriptor */
95238106Sdes	int		bd_locked;	/* true if descriptor is locked */
96238106Sdes};
97238106Sdes
98238106Sdes/* Values for bd_state */
99238106Sdes#define BPF_IDLE	0		/* no select in progress */
100238106Sdes#define BPF_WAITING	1		/* waiting for read timeout in select */
101238106Sdes#define BPF_TIMED_OUT	2		/* read timeout has expired in select */
102238106Sdes
103238106Sdes#define BPFD_LOCK(bd)		mtx_lock(&(bd)->bd_mtx)
104238106Sdes#define BPFD_UNLOCK(bd)		mtx_unlock(&(bd)->bd_mtx)
105238106Sdes#define BPFD_LOCK_ASSERT(bd)	do {				\
106238106Sdes	mtx_assert(&(bd)->bd_mtx, MA_OWNED);			\
107238106Sdes	NET_ASSERT_GIANT();					\
108238106Sdes} while (0)
109238106Sdes
110238106Sdes/* Test whether a BPF is ready for read(). */
111238106Sdes#define	bpf_ready(bd)						 \
112238106Sdes	((bd)->bd_hlen != 0 ||					 \
113238106Sdes	 (((bd)->bd_immediate || (bd)->bd_state == BPF_TIMED_OUT) && \
114238106Sdes	  (bd)->bd_slen != 0))
115238106Sdes
116238106Sdes/*
117238106Sdes * External representation of the bpf descriptor
118238106Sdes */
119238106Sdesstruct xbpf_d {
120238106Sdes	u_char		bd_promisc;
121238106Sdes	u_char		bd_immediate;
122238106Sdes	int		bd_hdrcmplt;
123238106Sdes	int		bd_direction;
124238106Sdes	int		bd_feedback;
125238106Sdes	int		bd_async;
126238106Sdes	u_long		bd_rcount;
127238106Sdes	u_long		bd_dcount;
128238106Sdes	u_long		bd_fcount;
129238106Sdes	int		bd_sig;
130238106Sdes	int		bd_slen;
131238106Sdes	int		bd_hlen;
132238106Sdes	int		bd_bufsize;
133238106Sdes	pid_t		bd_pid;
134238106Sdes	char		bd_ifname[IFNAMSIZ];
135238106Sdes	int		bd_locked;
136238106Sdes};
137238106Sdes
138238106Sdes#define BPFIF_LOCK(bif)		mtx_lock(&(bif)->bif_mtx)
139238106Sdes#define BPFIF_UNLOCK(bif)	mtx_unlock(&(bif)->bif_mtx)
140238106Sdes
141238106Sdes#endif
142238106Sdes