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$
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 */
51177548Scsjpstruct zbuf;
521541Srgrimesstruct bpf_d {
53134967Srwatson	LIST_ENTRY(bpf_d) bd_next;	/* Linked list of descriptors */
541541Srgrimes	/*
55177548Scsjp	 * Buffer slots: two memory buffers store the incoming packets.
561541Srgrimes	 *   The model has three slots.  Sbuf is always occupied.
571541Srgrimes	 *   sbuf (store) - Receive interrupt puts packets here.
58166329Srwatson	 *   hbuf (hold) - When sbuf is full, put buffer here and
591541Srgrimes	 *                 wakeup read (replace sbuf with fbuf).
60166329Srwatson	 *   fbuf (free) - When read is done, put buffer 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 */
66244090Sghelmer	int		bd_hbuf_in_use;	/* don't rotate buffers */
671541Srgrimes	int 		bd_slen;	/* current length of store buffer */
681541Srgrimes	int 		bd_hlen;	/* current length of hold buffer */
691541Srgrimes
701541Srgrimes	int		bd_bufsize;	/* absolute length of buffers */
711541Srgrimes
721541Srgrimes	struct bpf_if *	bd_bif;		/* interface descriptor */
731541Srgrimes	u_long		bd_rtout;	/* Read timeout in 'ticks' */
74149376Scsjp	struct bpf_insn *bd_rfilter; 	/* read filter code */
75149376Scsjp	struct bpf_insn *bd_wfilter;	/* write filter code */
76196150Sjkim	void		*bd_bfilter;	/* binary filter code */
77177548Scsjp	u_int64_t	bd_rcount;	/* number of packets received */
78177548Scsjp	u_int64_t	bd_dcount;	/* number of packets dropped */
791541Srgrimes
801541Srgrimes	u_char		bd_promisc;	/* true if listening promiscuously */
811541Srgrimes	u_char		bd_state;	/* idle, waiting, or timed out */
821541Srgrimes	u_char		bd_immediate;	/* true to return on packet arrival */
83233938Smelifaro	u_char		bd_writer;	/* non-zero if d is writer-only */
8452248Smsmith	int		bd_hdrcmplt;	/* false to fill in src lladdr automatically */
85167035Sjkim	int		bd_direction;	/* select packet direction */
86209216Sjkim	int		bd_tstamp;	/* select time stamping function */
87167035Sjkim	int		bd_feedback;	/* true to feed back sent packets */
889235Spst	int		bd_async;	/* non-zero if packet reception should generate signal */
899235Spst	int		bd_sig;		/* signal to send upon packet reception */
9041087Struckman	struct sigio *	bd_sigio;	/* information for async I/O */
911541Srgrimes	struct selinfo	bd_sel;		/* bsd select info */
92235745Smelifaro	struct mtx	bd_lock;	/* per-descriptor lock */
9387955Sjdp	struct callout	bd_callout;	/* for BPF timeouts with select */
94122524Srwatson	struct label	*bd_label;	/* MAC label for descriptor */
95177548Scsjp	u_int64_t	bd_fcount;	/* number of packets which matched filter */
96148366Scsjp	pid_t		bd_pid;		/* PID which created descriptor */
97149376Scsjp	int		bd_locked;	/* true if descriptor is locked */
98177548Scsjp	u_int		bd_bufmode;	/* Current buffer mode. */
99177548Scsjp	u_int64_t	bd_wcount;	/* number of packets written */
100177548Scsjp	u_int64_t	bd_wfcount;	/* number of packets that matched write filter */
101177548Scsjp	u_int64_t	bd_wdcount;	/* number of packets dropped during a write */
102177548Scsjp	u_int64_t	bd_zcopy;	/* number of zero copy operations */
103207195Skib	u_char		bd_compat32;	/* 32-bit stream on LP64 system */
1041541Srgrimes};
1051541Srgrimes
10687955Sjdp/* Values for bd_state */
10787955Sjdp#define BPF_IDLE	0		/* no select in progress */
10887955Sjdp#define BPF_WAITING	1		/* waiting for read timeout in select */
10987955Sjdp#define BPF_TIMED_OUT	2		/* read timeout has expired in select */
11087955Sjdp
111235745Smelifaro#define BPFD_LOCK(bd)		mtx_lock(&(bd)->bd_lock)
112235745Smelifaro#define BPFD_UNLOCK(bd)		mtx_unlock(&(bd)->bd_lock)
113235745Smelifaro#define BPFD_LOCK_ASSERT(bd)	mtx_assert(&(bd)->bd_lock, MA_OWNED)
11472544Sjlemon
115233937Smelifaro#define BPF_PID_REFRESH(bd, td)	(bd)->bd_pid = (td)->td_proc->p_pid
116233937Smelifaro#define BPF_PID_REFRESH_CUR(bd)	(bd)->bd_pid = curthread->td_proc->p_pid
117233937Smelifaro
118233937Smelifaro#define BPF_LOCK()		mtx_lock(&bpf_mtx)
119233937Smelifaro#define BPF_UNLOCK()		mtx_unlock(&bpf_mtx)
120233937Smelifaro#define BPF_LOCK_ASSERT()	mtx_assert(&bpf_mtx, MA_OWNED)
1211541Srgrimes/*
122148366Scsjp * External representation of the bpf descriptor
123148366Scsjp */
124148366Scsjpstruct xbpf_d {
125177548Scsjp	u_int		bd_structsize;	/* Size of this structure. */
126148366Scsjp	u_char		bd_promisc;
127148366Scsjp	u_char		bd_immediate;
128177548Scsjp	u_char		__bd_pad[6];
129148366Scsjp	int		bd_hdrcmplt;
130167035Sjkim	int		bd_direction;
131167035Sjkim	int		bd_feedback;
132148366Scsjp	int		bd_async;
133177548Scsjp	u_int64_t	bd_rcount;
134177548Scsjp	u_int64_t	bd_dcount;
135177548Scsjp	u_int64_t	bd_fcount;
136148366Scsjp	int		bd_sig;
137148366Scsjp	int		bd_slen;
138148366Scsjp	int		bd_hlen;
139148366Scsjp	int		bd_bufsize;
140148366Scsjp	pid_t		bd_pid;
141148366Scsjp	char		bd_ifname[IFNAMSIZ];
142149376Scsjp	int		bd_locked;
143177548Scsjp	u_int64_t	bd_wcount;
144177548Scsjp	u_int64_t	bd_wfcount;
145177548Scsjp	u_int64_t	bd_wdcount;
146177548Scsjp	u_int64_t	bd_zcopy;
147177548Scsjp	int		bd_bufmode;
148177548Scsjp	/*
149177548Scsjp	 * Allocate 4 64 bit unsigned integers for future expansion so we do
150177548Scsjp	 * not have to worry about breaking the ABI.
151177548Scsjp	 */
152177548Scsjp	u_int64_t	bd_spare[4];
153148366Scsjp};
154148366Scsjp
155233937Smelifaro#define BPFIF_RLOCK(bif)	rw_rlock(&(bif)->bif_lock)
156233937Smelifaro#define BPFIF_RUNLOCK(bif)	rw_runlock(&(bif)->bif_lock)
157233937Smelifaro#define BPFIF_WLOCK(bif)	rw_wlock(&(bif)->bif_lock)
158233937Smelifaro#define BPFIF_WUNLOCK(bif)	rw_wunlock(&(bif)->bif_lock)
15972544Sjlemon
160235745Smelifaro#define BPFIF_FLAG_DYING	1	/* Reject new bpf consumers */
161235745Smelifaro
1621541Srgrimes#endif
163