xref: /freebsd/sys/net/bpf.c (revision 0832fc64942c39ee59c3b3890515cbb94c79c48b)
1df8bae1dSRodney W. Grimes /*
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  * 3. All advertising materials mentioning features or use of this software
19df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
20df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
21df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
22df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
23df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
24df8bae1dSRodney W. Grimes  *    without specific prior written permission.
25df8bae1dSRodney W. Grimes  *
26df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  *      @(#)bpf.c	8.2 (Berkeley) 3/28/94
39df8bae1dSRodney W. Grimes  *
40c3aac50fSPeter Wemm  * $FreeBSD$
41df8bae1dSRodney W. Grimes  */
42df8bae1dSRodney W. Grimes 
435bb5f2c9SPeter Wemm #include "opt_bpf.h"
445bb5f2c9SPeter Wemm #include "opt_netgraph.h"
45df8bae1dSRodney W. Grimes 
46df8bae1dSRodney W. Grimes #include <sys/param.h>
47df8bae1dSRodney W. Grimes #include <sys/systm.h>
48ce7609a4SBruce Evans #include <sys/conf.h>
494d1d4912SBruce Evans #include <sys/malloc.h>
50df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
51df8bae1dSRodney W. Grimes #include <sys/time.h>
52df8bae1dSRodney W. Grimes #include <sys/proc.h>
530310c19fSBruce Evans #include <sys/signalvar.h>
54528f627fSBruce Evans #include <sys/filio.h>
55528f627fSBruce Evans #include <sys/sockio.h>
56528f627fSBruce Evans #include <sys/ttycom.h>
57831d27a9SDon Lewis #include <sys/filedesc.h>
58df8bae1dSRodney W. Grimes 
59243ac7d8SPeter Wemm #include <sys/poll.h>
60df8bae1dSRodney W. Grimes 
61df8bae1dSRodney W. Grimes #include <sys/socket.h>
62fba9235dSBruce Evans #include <sys/vnode.h>
63df8bae1dSRodney W. Grimes 
64fba9235dSBruce Evans #include <net/if.h>
65df8bae1dSRodney W. Grimes #include <net/bpf.h>
66df8bae1dSRodney W. Grimes #include <net/bpfdesc.h>
67df8bae1dSRodney W. Grimes 
68df8bae1dSRodney W. Grimes #include <netinet/in.h>
69df8bae1dSRodney W. Grimes #include <netinet/if_ether.h>
70df8bae1dSRodney W. Grimes #include <sys/kernel.h>
71f708ef1bSPoul-Henning Kamp #include <sys/sysctl.h>
727b778b5eSEivind Eklund 
73959b7375SPoul-Henning Kamp static MALLOC_DEFINE(M_BPF, "BPF", "BPF data");
7487f6c662SJulian Elischer 
755bb5f2c9SPeter Wemm #if defined(DEV_BPF) || defined(NETGRAPH_BPF)
7653ac6efbSJulian Elischer 
77df8bae1dSRodney W. Grimes #define PRINET  26			/* interruptible */
78df8bae1dSRodney W. Grimes 
79df8bae1dSRodney W. Grimes /*
80df8bae1dSRodney W. Grimes  * The default read buffer size is patchable.
81df8bae1dSRodney W. Grimes  */
82e7bb21b3SJonathan Lemon static int bpf_bufsize = 4096;
83f708ef1bSPoul-Henning Kamp SYSCTL_INT(_debug, OID_AUTO, bpf_bufsize, CTLFLAG_RW,
84f708ef1bSPoul-Henning Kamp 	&bpf_bufsize, 0, "");
85eba2a1aeSPoul-Henning Kamp static int bpf_maxbufsize = BPF_MAXBUFSIZE;
86eba2a1aeSPoul-Henning Kamp SYSCTL_INT(_debug, OID_AUTO, bpf_maxbufsize, CTLFLAG_RW,
87eba2a1aeSPoul-Henning Kamp 	&bpf_maxbufsize, 0, "");
88df8bae1dSRodney W. Grimes 
89df8bae1dSRodney W. Grimes /*
90df8bae1dSRodney W. Grimes  *  bpf_iflist is the list of interfaces; each corresponds to an ifnet
91df8bae1dSRodney W. Grimes  */
92f708ef1bSPoul-Henning Kamp static struct bpf_if	*bpf_iflist;
93e7bb21b3SJonathan Lemon static struct mtx	bpf_mtx;		/* bpf global lock */
94df8bae1dSRodney W. Grimes 
95df8bae1dSRodney W. Grimes static int	bpf_allocbufs __P((struct bpf_d *));
96ce7609a4SBruce Evans static void	bpf_attachd __P((struct bpf_d *d, struct bpf_if *bp));
97ce7609a4SBruce Evans static void	bpf_detachd __P((struct bpf_d *d));
98df8bae1dSRodney W. Grimes static void	bpf_freed __P((struct bpf_d *));
998bcbc7dfSAlexander Langer static void	bpf_mcopy __P((const void *, void *, size_t));
100df8bae1dSRodney W. Grimes static int	bpf_movein __P((struct uio *, int,
101df8bae1dSRodney W. Grimes 		    struct mbuf **, struct sockaddr *, int *));
102df8bae1dSRodney W. Grimes static int	bpf_setif __P((struct bpf_d *, struct ifreq *));
103e7bb21b3SJonathan Lemon static __inline void
104df8bae1dSRodney W. Grimes 		bpf_wakeup __P((struct bpf_d *));
105df8bae1dSRodney W. Grimes static void	catchpacket __P((struct bpf_d *, u_char *, u_int,
1068bcbc7dfSAlexander Langer 		    u_int, void (*)(const void *, void *, size_t)));
107df8bae1dSRodney W. Grimes static void	reset_d __P((struct bpf_d *));
108f708ef1bSPoul-Henning Kamp static int	 bpf_setf __P((struct bpf_d *, struct bpf_program *));
109df8bae1dSRodney W. Grimes 
11087f6c662SJulian Elischer static	d_open_t	bpfopen;
11187f6c662SJulian Elischer static	d_close_t	bpfclose;
11287f6c662SJulian Elischer static	d_read_t	bpfread;
11387f6c662SJulian Elischer static	d_write_t	bpfwrite;
11487f6c662SJulian Elischer static	d_ioctl_t	bpfioctl;
115243ac7d8SPeter Wemm static	d_poll_t	bpfpoll;
11687f6c662SJulian Elischer 
11787f6c662SJulian Elischer #define CDEV_MAJOR 23
1184e2f199eSPoul-Henning Kamp static struct cdevsw bpf_cdevsw = {
1194e2f199eSPoul-Henning Kamp 	/* open */	bpfopen,
1204e2f199eSPoul-Henning Kamp 	/* close */	bpfclose,
1214e2f199eSPoul-Henning Kamp 	/* read */	bpfread,
1224e2f199eSPoul-Henning Kamp 	/* write */	bpfwrite,
1234e2f199eSPoul-Henning Kamp 	/* ioctl */	bpfioctl,
1244e2f199eSPoul-Henning Kamp 	/* poll */	bpfpoll,
1254e2f199eSPoul-Henning Kamp 	/* mmap */	nommap,
1264e2f199eSPoul-Henning Kamp 	/* strategy */	nostrategy,
1274e2f199eSPoul-Henning Kamp 	/* name */	"bpf",
1284e2f199eSPoul-Henning Kamp 	/* maj */	CDEV_MAJOR,
1294e2f199eSPoul-Henning Kamp 	/* dump */	nodump,
1304e2f199eSPoul-Henning Kamp 	/* psize */	nopsize,
1314e2f199eSPoul-Henning Kamp 	/* flags */	0,
1324e2f199eSPoul-Henning Kamp };
13387f6c662SJulian Elischer 
13487f6c662SJulian Elischer 
135df8bae1dSRodney W. Grimes static int
136df8bae1dSRodney W. Grimes bpf_movein(uio, linktype, mp, sockp, datlen)
137df8bae1dSRodney W. Grimes 	register struct uio *uio;
138df8bae1dSRodney W. Grimes 	int linktype, *datlen;
139df8bae1dSRodney W. Grimes 	register struct mbuf **mp;
140df8bae1dSRodney W. Grimes 	register struct sockaddr *sockp;
141df8bae1dSRodney W. Grimes {
142df8bae1dSRodney W. Grimes 	struct mbuf *m;
143df8bae1dSRodney W. Grimes 	int error;
144df8bae1dSRodney W. Grimes 	int len;
145df8bae1dSRodney W. Grimes 	int hlen;
146df8bae1dSRodney W. Grimes 
147df8bae1dSRodney W. Grimes 	/*
148df8bae1dSRodney W. Grimes 	 * Build a sockaddr based on the data link layer type.
149df8bae1dSRodney W. Grimes 	 * We do this at this level because the ethernet header
150df8bae1dSRodney W. Grimes 	 * is copied directly into the data field of the sockaddr.
151df8bae1dSRodney W. Grimes 	 * In the case of SLIP, there is no header and the packet
152df8bae1dSRodney W. Grimes 	 * is forwarded as is.
153df8bae1dSRodney W. Grimes 	 * Also, we are careful to leave room at the front of the mbuf
154df8bae1dSRodney W. Grimes 	 * for the link level header.
155df8bae1dSRodney W. Grimes 	 */
156df8bae1dSRodney W. Grimes 	switch (linktype) {
157df8bae1dSRodney W. Grimes 
158df8bae1dSRodney W. Grimes 	case DLT_SLIP:
159df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_INET;
160df8bae1dSRodney W. Grimes 		hlen = 0;
161df8bae1dSRodney W. Grimes 		break;
162df8bae1dSRodney W. Grimes 
163df8bae1dSRodney W. Grimes 	case DLT_EN10MB:
164df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_UNSPEC;
165df8bae1dSRodney W. Grimes 		/* XXX Would MAXLINKHDR be better? */
166df8bae1dSRodney W. Grimes 		hlen = sizeof(struct ether_header);
167df8bae1dSRodney W. Grimes 		break;
168df8bae1dSRodney W. Grimes 
169df8bae1dSRodney W. Grimes 	case DLT_FDDI:
170d41f24e7SDavid Greenman 		sockp->sa_family = AF_IMPLINK;
171d41f24e7SDavid Greenman 		hlen = 0;
172df8bae1dSRodney W. Grimes 		break;
173df8bae1dSRodney W. Grimes 
17422f05c43SAndrey A. Chernov 	case DLT_RAW:
175df8bae1dSRodney W. Grimes 	case DLT_NULL:
176df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_UNSPEC;
177df8bae1dSRodney W. Grimes 		hlen = 0;
178df8bae1dSRodney W. Grimes 		break;
179df8bae1dSRodney W. Grimes 
1804f53e3ccSKenjiro Cho 	case DLT_ATM_RFC1483:
1814f53e3ccSKenjiro Cho 		/*
1824f53e3ccSKenjiro Cho 		 * en atm driver requires 4-byte atm pseudo header.
1834f53e3ccSKenjiro Cho 		 * though it isn't standard, vpi:vci needs to be
1844f53e3ccSKenjiro Cho 		 * specified anyway.
1854f53e3ccSKenjiro Cho 		 */
1864f53e3ccSKenjiro Cho 		sockp->sa_family = AF_UNSPEC;
1874f53e3ccSKenjiro Cho 		hlen = 12; 	/* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
1884f53e3ccSKenjiro Cho 		break;
1894f53e3ccSKenjiro Cho 
19030fa52a6SBrian Somers 	case DLT_PPP:
19130fa52a6SBrian Somers 		sockp->sa_family = AF_UNSPEC;
19230fa52a6SBrian Somers 		hlen = 4;	/* This should match PPP_HDRLEN */
19330fa52a6SBrian Somers 		break;
19430fa52a6SBrian Somers 
195df8bae1dSRodney W. Grimes 	default:
196df8bae1dSRodney W. Grimes 		return (EIO);
197df8bae1dSRodney W. Grimes 	}
198df8bae1dSRodney W. Grimes 
199df8bae1dSRodney W. Grimes 	len = uio->uio_resid;
200df8bae1dSRodney W. Grimes 	*datlen = len - hlen;
201df8bae1dSRodney W. Grimes 	if ((unsigned)len > MCLBYTES)
202df8bae1dSRodney W. Grimes 		return (EIO);
203df8bae1dSRodney W. Grimes 
2042a0c503eSBosko Milekic 	MGETHDR(m, M_TRYWAIT, MT_DATA);
205df8bae1dSRodney W. Grimes 	if (m == 0)
206df8bae1dSRodney W. Grimes 		return (ENOBUFS);
207963e4c2aSGarrett Wollman 	if (len > MHLEN) {
2082a0c503eSBosko Milekic 		MCLGET(m, M_TRYWAIT);
209df8bae1dSRodney W. Grimes 		if ((m->m_flags & M_EXT) == 0) {
210df8bae1dSRodney W. Grimes 			error = ENOBUFS;
211df8bae1dSRodney W. Grimes 			goto bad;
212df8bae1dSRodney W. Grimes 		}
213df8bae1dSRodney W. Grimes 	}
214963e4c2aSGarrett Wollman 	m->m_pkthdr.len = m->m_len = len;
215963e4c2aSGarrett Wollman 	m->m_pkthdr.rcvif = NULL;
216df8bae1dSRodney W. Grimes 	*mp = m;
217df8bae1dSRodney W. Grimes 	/*
218df8bae1dSRodney W. Grimes 	 * Make room for link header.
219df8bae1dSRodney W. Grimes 	 */
220df8bae1dSRodney W. Grimes 	if (hlen != 0) {
2214f079e2fSGarrett Wollman 		m->m_pkthdr.len -= hlen;
222df8bae1dSRodney W. Grimes 		m->m_len -= hlen;
223df8bae1dSRodney W. Grimes #if BSD >= 199103
224df8bae1dSRodney W. Grimes 		m->m_data += hlen; /* XXX */
225df8bae1dSRodney W. Grimes #else
226df8bae1dSRodney W. Grimes 		m->m_off += hlen;
227df8bae1dSRodney W. Grimes #endif
228e7bb21b3SJonathan Lemon 		error = uiomove((caddr_t)sockp->sa_data, hlen, uio);
229df8bae1dSRodney W. Grimes 		if (error)
230df8bae1dSRodney W. Grimes 			goto bad;
231df8bae1dSRodney W. Grimes 	}
232e7bb21b3SJonathan Lemon 	error = uiomove(mtod(m, caddr_t), len - hlen, uio);
233df8bae1dSRodney W. Grimes 	if (!error)
234df8bae1dSRodney W. Grimes 		return (0);
235df8bae1dSRodney W. Grimes  bad:
236df8bae1dSRodney W. Grimes 	m_freem(m);
237df8bae1dSRodney W. Grimes 	return (error);
238df8bae1dSRodney W. Grimes }
239df8bae1dSRodney W. Grimes 
240df8bae1dSRodney W. Grimes /*
241df8bae1dSRodney W. Grimes  * Attach file to the bpf interface, i.e. make d listen on bp.
242df8bae1dSRodney W. Grimes  */
243df8bae1dSRodney W. Grimes static void
244df8bae1dSRodney W. Grimes bpf_attachd(d, bp)
245df8bae1dSRodney W. Grimes 	struct bpf_d *d;
246df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
247df8bae1dSRodney W. Grimes {
248df8bae1dSRodney W. Grimes 	/*
249df8bae1dSRodney W. Grimes 	 * Point d at bp, and add d to the interface's list of listeners.
250df8bae1dSRodney W. Grimes 	 * Finally, point the driver's bpf cookie at the interface so
251df8bae1dSRodney W. Grimes 	 * it will divert packets to bpf.
252df8bae1dSRodney W. Grimes 	 */
253e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
254df8bae1dSRodney W. Grimes 	d->bd_bif = bp;
255df8bae1dSRodney W. Grimes 	d->bd_next = bp->bif_dlist;
256df8bae1dSRodney W. Grimes 	bp->bif_dlist = d;
257df8bae1dSRodney W. Grimes 
2589b44ff22SGarrett Wollman 	bp->bif_ifp->if_bpf = bp;
259e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
260df8bae1dSRodney W. Grimes }
261df8bae1dSRodney W. Grimes 
262df8bae1dSRodney W. Grimes /*
263df8bae1dSRodney W. Grimes  * Detach a file from its interface.
264df8bae1dSRodney W. Grimes  */
265df8bae1dSRodney W. Grimes static void
266df8bae1dSRodney W. Grimes bpf_detachd(d)
267df8bae1dSRodney W. Grimes 	struct bpf_d *d;
268df8bae1dSRodney W. Grimes {
2696e891d64SPoul-Henning Kamp 	int error;
270df8bae1dSRodney W. Grimes 	struct bpf_d **p;
271df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
272df8bae1dSRodney W. Grimes 
273df8bae1dSRodney W. Grimes 	bp = d->bd_bif;
274df8bae1dSRodney W. Grimes 	/*
275df8bae1dSRodney W. Grimes 	 * Check if this descriptor had requested promiscuous mode.
276df8bae1dSRodney W. Grimes 	 * If so, turn it off.
277df8bae1dSRodney W. Grimes 	 */
278df8bae1dSRodney W. Grimes 	if (d->bd_promisc) {
279df8bae1dSRodney W. Grimes 		d->bd_promisc = 0;
2806e891d64SPoul-Henning Kamp 		error = ifpromisc(bp->bif_ifp, 0);
2816e891d64SPoul-Henning Kamp 		if (error != 0 && error != ENXIO) {
282df8bae1dSRodney W. Grimes 			/*
2836e891d64SPoul-Henning Kamp 			 * ENXIO can happen if a pccard is unplugged
284df8bae1dSRodney W. Grimes 			 * Something is really wrong if we were able to put
285df8bae1dSRodney W. Grimes 			 * the driver into promiscuous mode, but can't
286df8bae1dSRodney W. Grimes 			 * take it out.
287df8bae1dSRodney W. Grimes 			 */
2886e891d64SPoul-Henning Kamp 			printf("%s%d: ifpromisc failed %d\n",
2896e891d64SPoul-Henning Kamp 			    bp->bif_ifp->if_name, bp->bif_ifp->if_unit, error);
2906e891d64SPoul-Henning Kamp 		}
291df8bae1dSRodney W. Grimes 	}
292df8bae1dSRodney W. Grimes 	/* Remove d from the interface's descriptor list. */
293e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
294df8bae1dSRodney W. Grimes 	p = &bp->bif_dlist;
295df8bae1dSRodney W. Grimes 	while (*p != d) {
296df8bae1dSRodney W. Grimes 		p = &(*p)->bd_next;
297df8bae1dSRodney W. Grimes 		if (*p == 0)
298df8bae1dSRodney W. Grimes 			panic("bpf_detachd: descriptor not in list");
299df8bae1dSRodney W. Grimes 	}
300df8bae1dSRodney W. Grimes 	*p = (*p)->bd_next;
301df8bae1dSRodney W. Grimes 	if (bp->bif_dlist == 0)
302df8bae1dSRodney W. Grimes 		/*
303df8bae1dSRodney W. Grimes 		 * Let the driver know that there are no more listeners.
304df8bae1dSRodney W. Grimes 		 */
3059b44ff22SGarrett Wollman 		d->bd_bif->bif_ifp->if_bpf = 0;
306e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
307df8bae1dSRodney W. Grimes 	d->bd_bif = 0;
308df8bae1dSRodney W. Grimes }
309df8bae1dSRodney W. Grimes 
310df8bae1dSRodney W. Grimes /*
311df8bae1dSRodney W. Grimes  * Open ethernet device.  Returns ENXIO for illegal minor device number,
312df8bae1dSRodney W. Grimes  * EBUSY if file is open by another process.
313df8bae1dSRodney W. Grimes  */
314df8bae1dSRodney W. Grimes /* ARGSUSED */
31587f6c662SJulian Elischer static	int
31660039670SBruce Evans bpfopen(dev, flags, fmt, p)
317df8bae1dSRodney W. Grimes 	dev_t dev;
31860039670SBruce Evans 	int flags;
31960039670SBruce Evans 	int fmt;
32060039670SBruce Evans 	struct proc *p;
321df8bae1dSRodney W. Grimes {
322e7bb21b3SJonathan Lemon 	struct bpf_d *d;
323df8bae1dSRodney W. Grimes 
324e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
325bd3a5320SPoul-Henning Kamp 	d = dev->si_drv1;
326df8bae1dSRodney W. Grimes 	/*
327df8bae1dSRodney W. Grimes 	 * Each minor can be opened by only one process.  If the requested
328df8bae1dSRodney W. Grimes 	 * minor is in use, return EBUSY.
329df8bae1dSRodney W. Grimes 	 */
330e7bb21b3SJonathan Lemon 	if (d) {
331e7bb21b3SJonathan Lemon 		mtx_unlock(&bpf_mtx);
332df8bae1dSRodney W. Grimes 		return (EBUSY);
333e7bb21b3SJonathan Lemon 	}
334e7bb21b3SJonathan Lemon 	dev->si_drv1 = (struct bpf_d *)~0;	/* mark device in use */
335e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
336e7bb21b3SJonathan Lemon 
337d1d74c28SJohn Baldwin 	if ((dev->si_flags & SI_NAMED) == 0)
338b0d17ba6SPoul-Henning Kamp 		make_dev(&bpf_cdevsw, minor(dev), UID_ROOT, GID_WHEEL, 0600,
339b0d17ba6SPoul-Henning Kamp 		    "bpf%d", dev2unit(dev));
3407cc0979fSDavid Malone 	MALLOC(d, struct bpf_d *, sizeof(*d), M_BPF, M_WAITOK | M_ZERO);
341bd3a5320SPoul-Henning Kamp 	dev->si_drv1 = d;
342df8bae1dSRodney W. Grimes 	d->bd_bufsize = bpf_bufsize;
34300a83887SPaul Traina 	d->bd_sig = SIGIO;
3448ed3828cSRobert Watson 	d->bd_seesent = 1;
345e7bb21b3SJonathan Lemon 	mtx_init(&d->bd_mtx, devtoname(dev), MTX_DEF);
346df8bae1dSRodney W. Grimes 
347df8bae1dSRodney W. Grimes 	return (0);
348df8bae1dSRodney W. Grimes }
349df8bae1dSRodney W. Grimes 
350df8bae1dSRodney W. Grimes /*
351df8bae1dSRodney W. Grimes  * Close the descriptor by detaching it from its interface,
352df8bae1dSRodney W. Grimes  * deallocating its buffers, and marking it free.
353df8bae1dSRodney W. Grimes  */
354df8bae1dSRodney W. Grimes /* ARGSUSED */
35587f6c662SJulian Elischer static	int
35660039670SBruce Evans bpfclose(dev, flags, fmt, p)
357df8bae1dSRodney W. Grimes 	dev_t dev;
35860039670SBruce Evans 	int flags;
35960039670SBruce Evans 	int fmt;
36060039670SBruce Evans 	struct proc *p;
361df8bae1dSRodney W. Grimes {
362e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
363df8bae1dSRodney W. Grimes 
364831d27a9SDon Lewis 	funsetown(d->bd_sigio);
365e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
366df8bae1dSRodney W. Grimes 	if (d->bd_bif)
367df8bae1dSRodney W. Grimes 		bpf_detachd(d);
368e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
369df8bae1dSRodney W. Grimes 	bpf_freed(d);
370bd3a5320SPoul-Henning Kamp 	dev->si_drv1 = 0;
371bd3a5320SPoul-Henning Kamp 	FREE(d, M_BPF);
372df8bae1dSRodney W. Grimes 
373df8bae1dSRodney W. Grimes 	return (0);
374df8bae1dSRodney W. Grimes }
375df8bae1dSRodney W. Grimes 
376df8bae1dSRodney W. Grimes 
377df8bae1dSRodney W. Grimes /*
378df8bae1dSRodney W. Grimes  * Rotate the packet buffers in descriptor d.  Move the store buffer
379df8bae1dSRodney W. Grimes  * into the hold slot, and the free buffer into the store slot.
380df8bae1dSRodney W. Grimes  * Zero the length of the new store buffer.
381df8bae1dSRodney W. Grimes  */
382df8bae1dSRodney W. Grimes #define ROTATE_BUFFERS(d) \
383df8bae1dSRodney W. Grimes 	(d)->bd_hbuf = (d)->bd_sbuf; \
384df8bae1dSRodney W. Grimes 	(d)->bd_hlen = (d)->bd_slen; \
385df8bae1dSRodney W. Grimes 	(d)->bd_sbuf = (d)->bd_fbuf; \
386df8bae1dSRodney W. Grimes 	(d)->bd_slen = 0; \
387df8bae1dSRodney W. Grimes 	(d)->bd_fbuf = 0;
388df8bae1dSRodney W. Grimes /*
389df8bae1dSRodney W. Grimes  *  bpfread - read next chunk of packets from buffers
390df8bae1dSRodney W. Grimes  */
39187f6c662SJulian Elischer static	int
39260039670SBruce Evans bpfread(dev, uio, ioflag)
393df8bae1dSRodney W. Grimes 	dev_t dev;
394df8bae1dSRodney W. Grimes 	register struct uio *uio;
39560039670SBruce Evans 	int ioflag;
396df8bae1dSRodney W. Grimes {
397e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
398df8bae1dSRodney W. Grimes 	int error;
399df8bae1dSRodney W. Grimes 
400df8bae1dSRodney W. Grimes 	/*
401df8bae1dSRodney W. Grimes 	 * Restrict application to use a buffer the same size as
402df8bae1dSRodney W. Grimes 	 * as kernel buffers.
403df8bae1dSRodney W. Grimes 	 */
404df8bae1dSRodney W. Grimes 	if (uio->uio_resid != d->bd_bufsize)
405df8bae1dSRodney W. Grimes 		return (EINVAL);
406df8bae1dSRodney W. Grimes 
407e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
408df8bae1dSRodney W. Grimes 	/*
409df8bae1dSRodney W. Grimes 	 * If the hold buffer is empty, then do a timed sleep, which
410df8bae1dSRodney W. Grimes 	 * ends when the timeout expires or when enough packets
411df8bae1dSRodney W. Grimes 	 * have arrived to fill the store buffer.
412df8bae1dSRodney W. Grimes 	 */
413df8bae1dSRodney W. Grimes 	while (d->bd_hbuf == 0) {
414df8bae1dSRodney W. Grimes 		if (d->bd_immediate && d->bd_slen != 0) {
415df8bae1dSRodney W. Grimes 			/*
416df8bae1dSRodney W. Grimes 			 * A packet(s) either arrived since the previous
417df8bae1dSRodney W. Grimes 			 * read or arrived while we were asleep.
418df8bae1dSRodney W. Grimes 			 * Rotate the buffers and return what's here.
419df8bae1dSRodney W. Grimes 			 */
420df8bae1dSRodney W. Grimes 			ROTATE_BUFFERS(d);
421df8bae1dSRodney W. Grimes 			break;
422df8bae1dSRodney W. Grimes 		}
423de5d9935SRobert Watson 
424de5d9935SRobert Watson 		/*
425de5d9935SRobert Watson 		 * No data is available, check to see if the bpf device
426de5d9935SRobert Watson 		 * is still pointed at a real interface.  If not, return
427de5d9935SRobert Watson 		 * ENXIO so that the userland process knows to rebind
428de5d9935SRobert Watson 		 * it before using it again.
429de5d9935SRobert Watson 		 */
430de5d9935SRobert Watson 		if (d->bd_bif == NULL) {
431e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
432de5d9935SRobert Watson 			return (ENXIO);
433de5d9935SRobert Watson 		}
434de5d9935SRobert Watson 
435fba3cfdeSJohn Polstra 		if (ioflag & IO_NDELAY) {
436e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
437fba3cfdeSJohn Polstra 			return (EWOULDBLOCK);
438fba3cfdeSJohn Polstra 		}
439e7bb21b3SJonathan Lemon 		error = msleep((caddr_t)d, &d->bd_mtx, PRINET|PCATCH,
440e7bb21b3SJonathan Lemon 		     "bpf", d->bd_rtout);
441df8bae1dSRodney W. Grimes 		if (error == EINTR || error == ERESTART) {
442e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
443df8bae1dSRodney W. Grimes 			return (error);
444df8bae1dSRodney W. Grimes 		}
445df8bae1dSRodney W. Grimes 		if (error == EWOULDBLOCK) {
446df8bae1dSRodney W. Grimes 			/*
447df8bae1dSRodney W. Grimes 			 * On a timeout, return what's in the buffer,
448df8bae1dSRodney W. Grimes 			 * which may be nothing.  If there is something
449df8bae1dSRodney W. Grimes 			 * in the store buffer, we can rotate the buffers.
450df8bae1dSRodney W. Grimes 			 */
451df8bae1dSRodney W. Grimes 			if (d->bd_hbuf)
452df8bae1dSRodney W. Grimes 				/*
453df8bae1dSRodney W. Grimes 				 * We filled up the buffer in between
454df8bae1dSRodney W. Grimes 				 * getting the timeout and arriving
455df8bae1dSRodney W. Grimes 				 * here, so we don't need to rotate.
456df8bae1dSRodney W. Grimes 				 */
457df8bae1dSRodney W. Grimes 				break;
458df8bae1dSRodney W. Grimes 
459df8bae1dSRodney W. Grimes 			if (d->bd_slen == 0) {
460e7bb21b3SJonathan Lemon 				BPFD_UNLOCK(d);
461df8bae1dSRodney W. Grimes 				return (0);
462df8bae1dSRodney W. Grimes 			}
463df8bae1dSRodney W. Grimes 			ROTATE_BUFFERS(d);
464df8bae1dSRodney W. Grimes 			break;
465df8bae1dSRodney W. Grimes 		}
466df8bae1dSRodney W. Grimes 	}
467df8bae1dSRodney W. Grimes 	/*
468df8bae1dSRodney W. Grimes 	 * At this point, we know we have something in the hold slot.
469df8bae1dSRodney W. Grimes 	 */
470e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
471df8bae1dSRodney W. Grimes 
472df8bae1dSRodney W. Grimes 	/*
473df8bae1dSRodney W. Grimes 	 * Move data from hold buffer into user space.
474df8bae1dSRodney W. Grimes 	 * We know the entire buffer is transferred since
475df8bae1dSRodney W. Grimes 	 * we checked above that the read buffer is bpf_bufsize bytes.
476df8bae1dSRodney W. Grimes 	 */
477e7bb21b3SJonathan Lemon 	error = uiomove(d->bd_hbuf, d->bd_hlen, uio);
478df8bae1dSRodney W. Grimes 
479e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
480df8bae1dSRodney W. Grimes 	d->bd_fbuf = d->bd_hbuf;
481df8bae1dSRodney W. Grimes 	d->bd_hbuf = 0;
482df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
483e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
484df8bae1dSRodney W. Grimes 
485df8bae1dSRodney W. Grimes 	return (error);
486df8bae1dSRodney W. Grimes }
487df8bae1dSRodney W. Grimes 
488df8bae1dSRodney W. Grimes 
489df8bae1dSRodney W. Grimes /*
490df8bae1dSRodney W. Grimes  * If there are processes sleeping on this descriptor, wake them up.
491df8bae1dSRodney W. Grimes  */
492e7bb21b3SJonathan Lemon static __inline void
493df8bae1dSRodney W. Grimes bpf_wakeup(d)
494df8bae1dSRodney W. Grimes 	register struct bpf_d *d;
495df8bae1dSRodney W. Grimes {
496df8bae1dSRodney W. Grimes 	wakeup((caddr_t)d);
497831d27a9SDon Lewis 	if (d->bd_async && d->bd_sig && d->bd_sigio)
498831d27a9SDon Lewis 		pgsigio(d->bd_sigio, d->bd_sig, 0);
49900a83887SPaul Traina 
500df8bae1dSRodney W. Grimes 	selwakeup(&d->bd_sel);
501df8bae1dSRodney W. Grimes 	/* XXX */
502df8bae1dSRodney W. Grimes 	d->bd_sel.si_pid = 0;
503df8bae1dSRodney W. Grimes }
504df8bae1dSRodney W. Grimes 
50587f6c662SJulian Elischer static	int
50660039670SBruce Evans bpfwrite(dev, uio, ioflag)
507df8bae1dSRodney W. Grimes 	dev_t dev;
508df8bae1dSRodney W. Grimes 	struct uio *uio;
50960039670SBruce Evans 	int ioflag;
510df8bae1dSRodney W. Grimes {
511e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
512df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
513df8bae1dSRodney W. Grimes 	struct mbuf *m;
514e7bb21b3SJonathan Lemon 	int error;
515df8bae1dSRodney W. Grimes 	static struct sockaddr dst;
516df8bae1dSRodney W. Grimes 	int datlen;
517df8bae1dSRodney W. Grimes 
518df8bae1dSRodney W. Grimes 	if (d->bd_bif == 0)
519df8bae1dSRodney W. Grimes 		return (ENXIO);
520df8bae1dSRodney W. Grimes 
521df8bae1dSRodney W. Grimes 	ifp = d->bd_bif->bif_ifp;
522df8bae1dSRodney W. Grimes 
523df8bae1dSRodney W. Grimes 	if (uio->uio_resid == 0)
524df8bae1dSRodney W. Grimes 		return (0);
525df8bae1dSRodney W. Grimes 
526df8bae1dSRodney W. Grimes 	error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, &m, &dst, &datlen);
527df8bae1dSRodney W. Grimes 	if (error)
528df8bae1dSRodney W. Grimes 		return (error);
529df8bae1dSRodney W. Grimes 
530df8bae1dSRodney W. Grimes 	if (datlen > ifp->if_mtu)
531df8bae1dSRodney W. Grimes 		return (EMSGSIZE);
532df8bae1dSRodney W. Grimes 
533114ae644SMike Smith 	if (d->bd_hdrcmplt)
534114ae644SMike Smith 		dst.sa_family = pseudo_AF_HDRCMPLT;
535114ae644SMike Smith 
536e7bb21b3SJonathan Lemon 	mtx_lock(&Giant);
537df8bae1dSRodney W. Grimes 	error = (*ifp->if_output)(ifp, m, &dst, (struct rtentry *)0);
538e7bb21b3SJonathan Lemon 	mtx_unlock(&Giant);
539df8bae1dSRodney W. Grimes 	/*
540df8bae1dSRodney W. Grimes 	 * The driver frees the mbuf.
541df8bae1dSRodney W. Grimes 	 */
542df8bae1dSRodney W. Grimes 	return (error);
543df8bae1dSRodney W. Grimes }
544df8bae1dSRodney W. Grimes 
545df8bae1dSRodney W. Grimes /*
546df8bae1dSRodney W. Grimes  * Reset a descriptor by flushing its packet buffer and clearing the
547e7bb21b3SJonathan Lemon  * receive and drop counts.
548df8bae1dSRodney W. Grimes  */
549df8bae1dSRodney W. Grimes static void
550df8bae1dSRodney W. Grimes reset_d(d)
551df8bae1dSRodney W. Grimes 	struct bpf_d *d;
552df8bae1dSRodney W. Grimes {
553e7bb21b3SJonathan Lemon 
554e7bb21b3SJonathan Lemon 	mtx_assert(&d->bd_mtx, MA_OWNED);
555df8bae1dSRodney W. Grimes 	if (d->bd_hbuf) {
556df8bae1dSRodney W. Grimes 		/* Free the hold buffer. */
557df8bae1dSRodney W. Grimes 		d->bd_fbuf = d->bd_hbuf;
558df8bae1dSRodney W. Grimes 		d->bd_hbuf = 0;
559df8bae1dSRodney W. Grimes 	}
560df8bae1dSRodney W. Grimes 	d->bd_slen = 0;
561df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
562df8bae1dSRodney W. Grimes 	d->bd_rcount = 0;
563df8bae1dSRodney W. Grimes 	d->bd_dcount = 0;
564df8bae1dSRodney W. Grimes }
565df8bae1dSRodney W. Grimes 
566df8bae1dSRodney W. Grimes /*
567df8bae1dSRodney W. Grimes  *  FIONREAD		Check for read packet available.
568df8bae1dSRodney W. Grimes  *  SIOCGIFADDR		Get interface address - convenient hook to driver.
569df8bae1dSRodney W. Grimes  *  BIOCGBLEN		Get buffer len [for read()].
570df8bae1dSRodney W. Grimes  *  BIOCSETF		Set ethernet read filter.
571df8bae1dSRodney W. Grimes  *  BIOCFLUSH		Flush read packet buffer.
572df8bae1dSRodney W. Grimes  *  BIOCPROMISC		Put interface into promiscuous mode.
573df8bae1dSRodney W. Grimes  *  BIOCGDLT		Get link layer type.
574df8bae1dSRodney W. Grimes  *  BIOCGETIF		Get interface name.
575df8bae1dSRodney W. Grimes  *  BIOCSETIF		Set interface.
576df8bae1dSRodney W. Grimes  *  BIOCSRTIMEOUT	Set read timeout.
577df8bae1dSRodney W. Grimes  *  BIOCGRTIMEOUT	Get read timeout.
578df8bae1dSRodney W. Grimes  *  BIOCGSTATS		Get packet stats.
579df8bae1dSRodney W. Grimes  *  BIOCIMMEDIATE	Set immediate mode.
580df8bae1dSRodney W. Grimes  *  BIOCVERSION		Get filter language version.
581114ae644SMike Smith  *  BIOCGHDRCMPLT	Get "header already complete" flag
582114ae644SMike Smith  *  BIOCSHDRCMPLT	Set "header already complete" flag
5838ed3828cSRobert Watson  *  BIOCGSEESENT	Get "see packets sent" flag
5848ed3828cSRobert Watson  *  BIOCSSEESENT	Set "see packets sent" flag
585df8bae1dSRodney W. Grimes  */
586df8bae1dSRodney W. Grimes /* ARGSUSED */
58787f6c662SJulian Elischer static	int
58860039670SBruce Evans bpfioctl(dev, cmd, addr, flags, p)
589df8bae1dSRodney W. Grimes 	dev_t dev;
590ecbb00a2SDoug Rabson 	u_long cmd;
591df8bae1dSRodney W. Grimes 	caddr_t addr;
59260039670SBruce Evans 	int flags;
59360039670SBruce Evans 	struct proc *p;
594df8bae1dSRodney W. Grimes {
595e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
596e7bb21b3SJonathan Lemon 	int error = 0;
597df8bae1dSRodney W. Grimes 
598df8bae1dSRodney W. Grimes 	switch (cmd) {
599df8bae1dSRodney W. Grimes 
600df8bae1dSRodney W. Grimes 	default:
601df8bae1dSRodney W. Grimes 		error = EINVAL;
602df8bae1dSRodney W. Grimes 		break;
603df8bae1dSRodney W. Grimes 
604df8bae1dSRodney W. Grimes 	/*
605df8bae1dSRodney W. Grimes 	 * Check for read packet available.
606df8bae1dSRodney W. Grimes 	 */
607df8bae1dSRodney W. Grimes 	case FIONREAD:
608df8bae1dSRodney W. Grimes 		{
609df8bae1dSRodney W. Grimes 			int n;
610df8bae1dSRodney W. Grimes 
611e7bb21b3SJonathan Lemon 			BPFD_LOCK(d);
612df8bae1dSRodney W. Grimes 			n = d->bd_slen;
613df8bae1dSRodney W. Grimes 			if (d->bd_hbuf)
614df8bae1dSRodney W. Grimes 				n += d->bd_hlen;
615e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
616df8bae1dSRodney W. Grimes 
617df8bae1dSRodney W. Grimes 			*(int *)addr = n;
618df8bae1dSRodney W. Grimes 			break;
619df8bae1dSRodney W. Grimes 		}
620df8bae1dSRodney W. Grimes 
621df8bae1dSRodney W. Grimes 	case SIOCGIFADDR:
622df8bae1dSRodney W. Grimes 		{
623df8bae1dSRodney W. Grimes 			struct ifnet *ifp;
624df8bae1dSRodney W. Grimes 
625df8bae1dSRodney W. Grimes 			if (d->bd_bif == 0)
626df8bae1dSRodney W. Grimes 				error = EINVAL;
627df8bae1dSRodney W. Grimes 			else {
628df8bae1dSRodney W. Grimes 				ifp = d->bd_bif->bif_ifp;
629df8bae1dSRodney W. Grimes 				error = (*ifp->if_ioctl)(ifp, cmd, addr);
630df8bae1dSRodney W. Grimes 			}
631df8bae1dSRodney W. Grimes 			break;
632df8bae1dSRodney W. Grimes 		}
633df8bae1dSRodney W. Grimes 
634df8bae1dSRodney W. Grimes 	/*
635df8bae1dSRodney W. Grimes 	 * Get buffer len [for read()].
636df8bae1dSRodney W. Grimes 	 */
637df8bae1dSRodney W. Grimes 	case BIOCGBLEN:
638df8bae1dSRodney W. Grimes 		*(u_int *)addr = d->bd_bufsize;
639df8bae1dSRodney W. Grimes 		break;
640df8bae1dSRodney W. Grimes 
641df8bae1dSRodney W. Grimes 	/*
642df8bae1dSRodney W. Grimes 	 * Set buffer length.
643df8bae1dSRodney W. Grimes 	 */
644df8bae1dSRodney W. Grimes 	case BIOCSBLEN:
645df8bae1dSRodney W. Grimes 		if (d->bd_bif != 0)
646df8bae1dSRodney W. Grimes 			error = EINVAL;
647df8bae1dSRodney W. Grimes 		else {
648df8bae1dSRodney W. Grimes 			register u_int size = *(u_int *)addr;
649df8bae1dSRodney W. Grimes 
650eba2a1aeSPoul-Henning Kamp 			if (size > bpf_maxbufsize)
651eba2a1aeSPoul-Henning Kamp 				*(u_int *)addr = size = bpf_maxbufsize;
652df8bae1dSRodney W. Grimes 			else if (size < BPF_MINBUFSIZE)
653df8bae1dSRodney W. Grimes 				*(u_int *)addr = size = BPF_MINBUFSIZE;
654df8bae1dSRodney W. Grimes 			d->bd_bufsize = size;
655df8bae1dSRodney W. Grimes 		}
656df8bae1dSRodney W. Grimes 		break;
657df8bae1dSRodney W. Grimes 
658df8bae1dSRodney W. Grimes 	/*
659df8bae1dSRodney W. Grimes 	 * Set link layer read filter.
660df8bae1dSRodney W. Grimes 	 */
661df8bae1dSRodney W. Grimes 	case BIOCSETF:
662df8bae1dSRodney W. Grimes 		error = bpf_setf(d, (struct bpf_program *)addr);
663df8bae1dSRodney W. Grimes 		break;
664df8bae1dSRodney W. Grimes 
665df8bae1dSRodney W. Grimes 	/*
666df8bae1dSRodney W. Grimes 	 * Flush read packet buffer.
667df8bae1dSRodney W. Grimes 	 */
668df8bae1dSRodney W. Grimes 	case BIOCFLUSH:
669e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
670df8bae1dSRodney W. Grimes 		reset_d(d);
671e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
672df8bae1dSRodney W. Grimes 		break;
673df8bae1dSRodney W. Grimes 
674df8bae1dSRodney W. Grimes 	/*
675df8bae1dSRodney W. Grimes 	 * Put interface into promiscuous mode.
676df8bae1dSRodney W. Grimes 	 */
677df8bae1dSRodney W. Grimes 	case BIOCPROMISC:
678df8bae1dSRodney W. Grimes 		if (d->bd_bif == 0) {
679df8bae1dSRodney W. Grimes 			/*
680df8bae1dSRodney W. Grimes 			 * No interface attached yet.
681df8bae1dSRodney W. Grimes 			 */
682df8bae1dSRodney W. Grimes 			error = EINVAL;
683df8bae1dSRodney W. Grimes 			break;
684df8bae1dSRodney W. Grimes 		}
685df8bae1dSRodney W. Grimes 		if (d->bd_promisc == 0) {
686e7bb21b3SJonathan Lemon 			mtx_lock(&Giant);
687df8bae1dSRodney W. Grimes 			error = ifpromisc(d->bd_bif->bif_ifp, 1);
688e7bb21b3SJonathan Lemon 			mtx_unlock(&Giant);
689df8bae1dSRodney W. Grimes 			if (error == 0)
690df8bae1dSRodney W. Grimes 				d->bd_promisc = 1;
691df8bae1dSRodney W. Grimes 		}
692df8bae1dSRodney W. Grimes 		break;
693df8bae1dSRodney W. Grimes 
694df8bae1dSRodney W. Grimes 	/*
695df8bae1dSRodney W. Grimes 	 * Get device parameters.
696df8bae1dSRodney W. Grimes 	 */
697df8bae1dSRodney W. Grimes 	case BIOCGDLT:
698df8bae1dSRodney W. Grimes 		if (d->bd_bif == 0)
699df8bae1dSRodney W. Grimes 			error = EINVAL;
700df8bae1dSRodney W. Grimes 		else
701df8bae1dSRodney W. Grimes 			*(u_int *)addr = d->bd_bif->bif_dlt;
702df8bae1dSRodney W. Grimes 		break;
703df8bae1dSRodney W. Grimes 
704df8bae1dSRodney W. Grimes 	/*
7051dd0feaaSArchie Cobbs 	 * Get interface name.
706df8bae1dSRodney W. Grimes 	 */
707df8bae1dSRodney W. Grimes 	case BIOCGETIF:
708df8bae1dSRodney W. Grimes 		if (d->bd_bif == 0)
709df8bae1dSRodney W. Grimes 			error = EINVAL;
7101dd0feaaSArchie Cobbs 		else {
7111dd0feaaSArchie Cobbs 			struct ifnet *const ifp = d->bd_bif->bif_ifp;
7121dd0feaaSArchie Cobbs 			struct ifreq *const ifr = (struct ifreq *)addr;
7131dd0feaaSArchie Cobbs 
7141dd0feaaSArchie Cobbs 			snprintf(ifr->ifr_name, sizeof(ifr->ifr_name),
7151dd0feaaSArchie Cobbs 			    "%s%d", ifp->if_name, ifp->if_unit);
7161dd0feaaSArchie Cobbs 		}
717df8bae1dSRodney W. Grimes 		break;
718df8bae1dSRodney W. Grimes 
719df8bae1dSRodney W. Grimes 	/*
720df8bae1dSRodney W. Grimes 	 * Set interface.
721df8bae1dSRodney W. Grimes 	 */
722df8bae1dSRodney W. Grimes 	case BIOCSETIF:
723df8bae1dSRodney W. Grimes 		error = bpf_setif(d, (struct ifreq *)addr);
724df8bae1dSRodney W. Grimes 		break;
725df8bae1dSRodney W. Grimes 
726df8bae1dSRodney W. Grimes 	/*
727df8bae1dSRodney W. Grimes 	 * Set read timeout.
728df8bae1dSRodney W. Grimes 	 */
729df8bae1dSRodney W. Grimes 	case BIOCSRTIMEOUT:
730df8bae1dSRodney W. Grimes 		{
731df8bae1dSRodney W. Grimes 			struct timeval *tv = (struct timeval *)addr;
732df8bae1dSRodney W. Grimes 
733bdc2cdc5SAlexander Langer 			/*
734bdc2cdc5SAlexander Langer 			 * Subtract 1 tick from tvtohz() since this isn't
735bdc2cdc5SAlexander Langer 			 * a one-shot timer.
736bdc2cdc5SAlexander Langer 			 */
737bdc2cdc5SAlexander Langer 			if ((error = itimerfix(tv)) == 0)
738bdc2cdc5SAlexander Langer 				d->bd_rtout = tvtohz(tv) - 1;
739df8bae1dSRodney W. Grimes 			break;
740df8bae1dSRodney W. Grimes 		}
741df8bae1dSRodney W. Grimes 
742df8bae1dSRodney W. Grimes 	/*
743df8bae1dSRodney W. Grimes 	 * Get read timeout.
744df8bae1dSRodney W. Grimes 	 */
745df8bae1dSRodney W. Grimes 	case BIOCGRTIMEOUT:
746df8bae1dSRodney W. Grimes 		{
747df8bae1dSRodney W. Grimes 			struct timeval *tv = (struct timeval *)addr;
748df8bae1dSRodney W. Grimes 
749bdc2cdc5SAlexander Langer 			tv->tv_sec = d->bd_rtout / hz;
750bdc2cdc5SAlexander Langer 			tv->tv_usec = (d->bd_rtout % hz) * tick;
751df8bae1dSRodney W. Grimes 			break;
752df8bae1dSRodney W. Grimes 		}
753df8bae1dSRodney W. Grimes 
754df8bae1dSRodney W. Grimes 	/*
755df8bae1dSRodney W. Grimes 	 * Get packet stats.
756df8bae1dSRodney W. Grimes 	 */
757df8bae1dSRodney W. Grimes 	case BIOCGSTATS:
758df8bae1dSRodney W. Grimes 		{
759df8bae1dSRodney W. Grimes 			struct bpf_stat *bs = (struct bpf_stat *)addr;
760df8bae1dSRodney W. Grimes 
761df8bae1dSRodney W. Grimes 			bs->bs_recv = d->bd_rcount;
762df8bae1dSRodney W. Grimes 			bs->bs_drop = d->bd_dcount;
763df8bae1dSRodney W. Grimes 			break;
764df8bae1dSRodney W. Grimes 		}
765df8bae1dSRodney W. Grimes 
766df8bae1dSRodney W. Grimes 	/*
767df8bae1dSRodney W. Grimes 	 * Set immediate mode.
768df8bae1dSRodney W. Grimes 	 */
769df8bae1dSRodney W. Grimes 	case BIOCIMMEDIATE:
770df8bae1dSRodney W. Grimes 		d->bd_immediate = *(u_int *)addr;
771df8bae1dSRodney W. Grimes 		break;
772df8bae1dSRodney W. Grimes 
773df8bae1dSRodney W. Grimes 	case BIOCVERSION:
774df8bae1dSRodney W. Grimes 		{
775df8bae1dSRodney W. Grimes 			struct bpf_version *bv = (struct bpf_version *)addr;
776df8bae1dSRodney W. Grimes 
777df8bae1dSRodney W. Grimes 			bv->bv_major = BPF_MAJOR_VERSION;
778df8bae1dSRodney W. Grimes 			bv->bv_minor = BPF_MINOR_VERSION;
779df8bae1dSRodney W. Grimes 			break;
780df8bae1dSRodney W. Grimes 		}
78100a83887SPaul Traina 
782114ae644SMike Smith 	/*
783114ae644SMike Smith 	 * Get "header already complete" flag
784114ae644SMike Smith 	 */
785114ae644SMike Smith 	case BIOCGHDRCMPLT:
786114ae644SMike Smith 		*(u_int *)addr = d->bd_hdrcmplt;
787114ae644SMike Smith 		break;
788114ae644SMike Smith 
789114ae644SMike Smith 	/*
790114ae644SMike Smith 	 * Set "header already complete" flag
791114ae644SMike Smith 	 */
792114ae644SMike Smith 	case BIOCSHDRCMPLT:
793114ae644SMike Smith 		d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
794114ae644SMike Smith 		break;
795114ae644SMike Smith 
7968ed3828cSRobert Watson 	/*
7978ed3828cSRobert Watson 	 * Get "see sent packets" flag
7988ed3828cSRobert Watson 	 */
7998ed3828cSRobert Watson 	case BIOCGSEESENT:
8008ed3828cSRobert Watson 		*(u_int *)addr = d->bd_seesent;
8018ed3828cSRobert Watson 		break;
8028ed3828cSRobert Watson 
8038ed3828cSRobert Watson 	/*
8048ed3828cSRobert Watson 	 * Set "see sent packets" flag
8058ed3828cSRobert Watson 	 */
8068ed3828cSRobert Watson 	case BIOCSSEESENT:
8078ed3828cSRobert Watson 		d->bd_seesent = *(u_int *)addr;
8088ed3828cSRobert Watson 		break;
8098ed3828cSRobert Watson 
81000a83887SPaul Traina 	case FIONBIO:		/* Non-blocking I/O */
81100a83887SPaul Traina 		break;
81200a83887SPaul Traina 
81300a83887SPaul Traina 	case FIOASYNC:		/* Send signal on receive packets */
81400a83887SPaul Traina 		d->bd_async = *(int *)addr;
81500a83887SPaul Traina 		break;
81600a83887SPaul Traina 
817831d27a9SDon Lewis 	case FIOSETOWN:
818831d27a9SDon Lewis 		error = fsetown(*(int *)addr, &d->bd_sigio);
81900a83887SPaul Traina 		break;
82000a83887SPaul Traina 
821831d27a9SDon Lewis 	case FIOGETOWN:
822831d27a9SDon Lewis 		*(int *)addr = fgetown(d->bd_sigio);
823831d27a9SDon Lewis 		break;
824831d27a9SDon Lewis 
825831d27a9SDon Lewis 	/* This is deprecated, FIOSETOWN should be used instead. */
826831d27a9SDon Lewis 	case TIOCSPGRP:
827831d27a9SDon Lewis 		error = fsetown(-(*(int *)addr), &d->bd_sigio);
828831d27a9SDon Lewis 		break;
829831d27a9SDon Lewis 
830831d27a9SDon Lewis 	/* This is deprecated, FIOGETOWN should be used instead. */
83100a83887SPaul Traina 	case TIOCGPGRP:
832831d27a9SDon Lewis 		*(int *)addr = -fgetown(d->bd_sigio);
83300a83887SPaul Traina 		break;
83400a83887SPaul Traina 
83500a83887SPaul Traina 	case BIOCSRSIG:		/* Set receive signal */
83600a83887SPaul Traina 		{
83700a83887SPaul Traina 		 	u_int sig;
83800a83887SPaul Traina 
83900a83887SPaul Traina 			sig = *(u_int *)addr;
84000a83887SPaul Traina 
84100a83887SPaul Traina 			if (sig >= NSIG)
84200a83887SPaul Traina 				error = EINVAL;
84300a83887SPaul Traina 			else
84400a83887SPaul Traina 				d->bd_sig = sig;
84500a83887SPaul Traina 			break;
84600a83887SPaul Traina 		}
84700a83887SPaul Traina 	case BIOCGRSIG:
84800a83887SPaul Traina 		*(u_int *)addr = d->bd_sig;
84900a83887SPaul Traina 		break;
850df8bae1dSRodney W. Grimes 	}
851df8bae1dSRodney W. Grimes 	return (error);
852df8bae1dSRodney W. Grimes }
853df8bae1dSRodney W. Grimes 
854df8bae1dSRodney W. Grimes /*
855df8bae1dSRodney W. Grimes  * Set d's packet filter program to fp.  If this file already has a filter,
856df8bae1dSRodney W. Grimes  * free it and replace it.  Returns EINVAL for bogus requests.
857df8bae1dSRodney W. Grimes  */
858f708ef1bSPoul-Henning Kamp static int
859df8bae1dSRodney W. Grimes bpf_setf(d, fp)
860df8bae1dSRodney W. Grimes 	struct bpf_d *d;
861df8bae1dSRodney W. Grimes 	struct bpf_program *fp;
862df8bae1dSRodney W. Grimes {
863df8bae1dSRodney W. Grimes 	struct bpf_insn *fcode, *old;
864df8bae1dSRodney W. Grimes 	u_int flen, size;
865df8bae1dSRodney W. Grimes 
866df8bae1dSRodney W. Grimes 	old = d->bd_filter;
867df8bae1dSRodney W. Grimes 	if (fp->bf_insns == 0) {
868df8bae1dSRodney W. Grimes 		if (fp->bf_len != 0)
869df8bae1dSRodney W. Grimes 			return (EINVAL);
870e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
871df8bae1dSRodney W. Grimes 		d->bd_filter = 0;
872df8bae1dSRodney W. Grimes 		reset_d(d);
873e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
874df8bae1dSRodney W. Grimes 		if (old != 0)
875bd3a5320SPoul-Henning Kamp 			free((caddr_t)old, M_BPF);
876df8bae1dSRodney W. Grimes 		return (0);
877df8bae1dSRodney W. Grimes 	}
878df8bae1dSRodney W. Grimes 	flen = fp->bf_len;
879df8bae1dSRodney W. Grimes 	if (flen > BPF_MAXINSNS)
880df8bae1dSRodney W. Grimes 		return (EINVAL);
881df8bae1dSRodney W. Grimes 
882df8bae1dSRodney W. Grimes 	size = flen * sizeof(*fp->bf_insns);
883bd3a5320SPoul-Henning Kamp 	fcode = (struct bpf_insn *)malloc(size, M_BPF, M_WAITOK);
884df8bae1dSRodney W. Grimes 	if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
885df8bae1dSRodney W. Grimes 	    bpf_validate(fcode, (int)flen)) {
886e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
887df8bae1dSRodney W. Grimes 		d->bd_filter = fcode;
888df8bae1dSRodney W. Grimes 		reset_d(d);
889e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
890df8bae1dSRodney W. Grimes 		if (old != 0)
891bd3a5320SPoul-Henning Kamp 			free((caddr_t)old, M_BPF);
892df8bae1dSRodney W. Grimes 
893df8bae1dSRodney W. Grimes 		return (0);
894df8bae1dSRodney W. Grimes 	}
895bd3a5320SPoul-Henning Kamp 	free((caddr_t)fcode, M_BPF);
896df8bae1dSRodney W. Grimes 	return (EINVAL);
897df8bae1dSRodney W. Grimes }
898df8bae1dSRodney W. Grimes 
899df8bae1dSRodney W. Grimes /*
900df8bae1dSRodney W. Grimes  * Detach a file from its current interface (if attached at all) and attach
901df8bae1dSRodney W. Grimes  * to the interface indicated by the name stored in ifr.
902df8bae1dSRodney W. Grimes  * Return an errno or 0.
903df8bae1dSRodney W. Grimes  */
904df8bae1dSRodney W. Grimes static int
905df8bae1dSRodney W. Grimes bpf_setif(d, ifr)
906df8bae1dSRodney W. Grimes 	struct bpf_d *d;
907df8bae1dSRodney W. Grimes 	struct ifreq *ifr;
908df8bae1dSRodney W. Grimes {
909df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
910e7bb21b3SJonathan Lemon 	int error;
9119b44ff22SGarrett Wollman 	struct ifnet *theywant;
912df8bae1dSRodney W. Grimes 
9139b44ff22SGarrett Wollman 	theywant = ifunit(ifr->ifr_name);
9149b44ff22SGarrett Wollman 	if (theywant == 0)
9159b44ff22SGarrett Wollman 		return ENXIO;
9169b44ff22SGarrett Wollman 
917df8bae1dSRodney W. Grimes 	/*
918df8bae1dSRodney W. Grimes 	 * Look through attached interfaces for the named one.
919df8bae1dSRodney W. Grimes 	 */
920e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
921df8bae1dSRodney W. Grimes 	for (bp = bpf_iflist; bp != 0; bp = bp->bif_next) {
922df8bae1dSRodney W. Grimes 		struct ifnet *ifp = bp->bif_ifp;
923df8bae1dSRodney W. Grimes 
9249b44ff22SGarrett Wollman 		if (ifp == 0 || ifp != theywant)
925df8bae1dSRodney W. Grimes 			continue;
926e7bb21b3SJonathan Lemon 
927e7bb21b3SJonathan Lemon 		mtx_unlock(&bpf_mtx);
928df8bae1dSRodney W. Grimes 		/*
929df8bae1dSRodney W. Grimes 		 * We found the requested interface.
930df8bae1dSRodney W. Grimes 		 * If it's not up, return an error.
931df8bae1dSRodney W. Grimes 		 * Allocate the packet buffers if we need to.
932df8bae1dSRodney W. Grimes 		 * If we're already attached to requested interface,
933df8bae1dSRodney W. Grimes 		 * just flush the buffer.
934df8bae1dSRodney W. Grimes 		 */
935df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_UP) == 0)
936df8bae1dSRodney W. Grimes 			return (ENETDOWN);
937df8bae1dSRodney W. Grimes 
938df8bae1dSRodney W. Grimes 		if (d->bd_sbuf == 0) {
939df8bae1dSRodney W. Grimes 			error = bpf_allocbufs(d);
940df8bae1dSRodney W. Grimes 			if (error != 0)
941df8bae1dSRodney W. Grimes 				return (error);
942df8bae1dSRodney W. Grimes 		}
943df8bae1dSRodney W. Grimes 		if (bp != d->bd_bif) {
944df8bae1dSRodney W. Grimes 			if (d->bd_bif)
945df8bae1dSRodney W. Grimes 				/*
946df8bae1dSRodney W. Grimes 				 * Detach if attached to something else.
947df8bae1dSRodney W. Grimes 				 */
948df8bae1dSRodney W. Grimes 				bpf_detachd(d);
949df8bae1dSRodney W. Grimes 
950df8bae1dSRodney W. Grimes 			bpf_attachd(d, bp);
951df8bae1dSRodney W. Grimes 		}
952e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
953df8bae1dSRodney W. Grimes 		reset_d(d);
954e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
955df8bae1dSRodney W. Grimes 		return (0);
956df8bae1dSRodney W. Grimes 	}
957e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
958df8bae1dSRodney W. Grimes 	/* Not found. */
959df8bae1dSRodney W. Grimes 	return (ENXIO);
960df8bae1dSRodney W. Grimes }
961df8bae1dSRodney W. Grimes 
962df8bae1dSRodney W. Grimes /*
963243ac7d8SPeter Wemm  * Support for select() and poll() system calls
964df8bae1dSRodney W. Grimes  *
965df8bae1dSRodney W. Grimes  * Return true iff the specific operation will not block indefinitely.
966df8bae1dSRodney W. Grimes  * Otherwise, return false but make a note that a selwakeup() must be done.
967df8bae1dSRodney W. Grimes  */
968df8bae1dSRodney W. Grimes int
969243ac7d8SPeter Wemm bpfpoll(dev, events, p)
970df8bae1dSRodney W. Grimes 	register dev_t dev;
971243ac7d8SPeter Wemm 	int events;
972df8bae1dSRodney W. Grimes 	struct proc *p;
973df8bae1dSRodney W. Grimes {
974e7bb21b3SJonathan Lemon 	struct bpf_d *d;
9750832fc64SGarance A Drosehn 	int revents;
976df8bae1dSRodney W. Grimes 
977bd3a5320SPoul-Henning Kamp 	d = dev->si_drv1;
978de5d9935SRobert Watson 	if (d->bd_bif == NULL)
979de5d9935SRobert Watson 		return (ENXIO);
980de5d9935SRobert Watson 
9810832fc64SGarance A Drosehn 	revents = events & (POLLOUT | POLLWRNORM);
982e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
98375c13541SPoul-Henning Kamp 	if (events & (POLLIN | POLLRDNORM)) {
9840832fc64SGarance A Drosehn 		/*
9850832fc64SGarance A Drosehn 		 * An imitation of the FIONREAD ioctl code.
9860832fc64SGarance A Drosehn 		 * XXX not quite.  An exact imitation:
9870832fc64SGarance A Drosehn 		 *	if (d->b_slen != 0 ||
9880832fc64SGarance A Drosehn 		 *	    (d->bd_hbuf != NULL && d->bd_hlen != 0)
9890832fc64SGarance A Drosehn 		 */
990243ac7d8SPeter Wemm 		if (d->bd_hlen != 0 || (d->bd_immediate && d->bd_slen != 0))
991243ac7d8SPeter Wemm 			revents |= events & (POLLIN | POLLRDNORM);
992df8bae1dSRodney W. Grimes 		else
993243ac7d8SPeter Wemm 			selrecord(p, &d->bd_sel);
99475c13541SPoul-Henning Kamp 	}
995e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
996243ac7d8SPeter Wemm 	return (revents);
997df8bae1dSRodney W. Grimes }
998df8bae1dSRodney W. Grimes 
999df8bae1dSRodney W. Grimes /*
1000df8bae1dSRodney W. Grimes  * Incoming linkage from device drivers.  Process the packet pkt, of length
1001df8bae1dSRodney W. Grimes  * pktlen, which is stored in a contiguous buffer.  The packet is parsed
1002df8bae1dSRodney W. Grimes  * by each process' filter, and if accepted, stashed into the corresponding
1003df8bae1dSRodney W. Grimes  * buffer.
1004df8bae1dSRodney W. Grimes  */
1005df8bae1dSRodney W. Grimes void
10069b44ff22SGarrett Wollman bpf_tap(ifp, pkt, pktlen)
10079b44ff22SGarrett Wollman 	struct ifnet *ifp;
1008df8bae1dSRodney W. Grimes 	register u_char *pkt;
1009df8bae1dSRodney W. Grimes 	register u_int pktlen;
1010df8bae1dSRodney W. Grimes {
1011df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
1012df8bae1dSRodney W. Grimes 	register struct bpf_d *d;
1013df8bae1dSRodney W. Grimes 	register u_int slen;
1014e7bb21b3SJonathan Lemon 
10159b44ff22SGarrett Wollman 	bp = ifp->if_bpf;
1016e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
1017df8bae1dSRodney W. Grimes 	for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
1018e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1019df8bae1dSRodney W. Grimes 		++d->bd_rcount;
1020df8bae1dSRodney W. Grimes 		slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen);
1021df8bae1dSRodney W. Grimes 		if (slen != 0)
1022df8bae1dSRodney W. Grimes 			catchpacket(d, pkt, pktlen, slen, bcopy);
1023e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1024df8bae1dSRodney W. Grimes 	}
1025e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
1026df8bae1dSRodney W. Grimes }
1027df8bae1dSRodney W. Grimes 
1028df8bae1dSRodney W. Grimes /*
1029df8bae1dSRodney W. Grimes  * Copy data from an mbuf chain into a buffer.  This code is derived
1030df8bae1dSRodney W. Grimes  * from m_copydata in sys/uipc_mbuf.c.
1031df8bae1dSRodney W. Grimes  */
1032df8bae1dSRodney W. Grimes static void
1033df8bae1dSRodney W. Grimes bpf_mcopy(src_arg, dst_arg, len)
1034df8bae1dSRodney W. Grimes 	const void *src_arg;
1035df8bae1dSRodney W. Grimes 	void *dst_arg;
10368bcbc7dfSAlexander Langer 	register size_t len;
1037df8bae1dSRodney W. Grimes {
1038df8bae1dSRodney W. Grimes 	register const struct mbuf *m;
1039df8bae1dSRodney W. Grimes 	register u_int count;
1040df8bae1dSRodney W. Grimes 	u_char *dst;
1041df8bae1dSRodney W. Grimes 
1042df8bae1dSRodney W. Grimes 	m = src_arg;
1043df8bae1dSRodney W. Grimes 	dst = dst_arg;
1044df8bae1dSRodney W. Grimes 	while (len > 0) {
1045df8bae1dSRodney W. Grimes 		if (m == 0)
1046df8bae1dSRodney W. Grimes 			panic("bpf_mcopy");
1047df8bae1dSRodney W. Grimes 		count = min(m->m_len, len);
10480453d3cbSBruce Evans 		bcopy(mtod(m, void *), dst, count);
1049df8bae1dSRodney W. Grimes 		m = m->m_next;
1050df8bae1dSRodney W. Grimes 		dst += count;
1051df8bae1dSRodney W. Grimes 		len -= count;
1052df8bae1dSRodney W. Grimes 	}
1053df8bae1dSRodney W. Grimes }
1054df8bae1dSRodney W. Grimes 
1055df8bae1dSRodney W. Grimes /*
1056df8bae1dSRodney W. Grimes  * Incoming linkage from device drivers, when packet is in an mbuf chain.
1057df8bae1dSRodney W. Grimes  */
1058df8bae1dSRodney W. Grimes void
10599b44ff22SGarrett Wollman bpf_mtap(ifp, m)
10609b44ff22SGarrett Wollman 	struct ifnet *ifp;
1061df8bae1dSRodney W. Grimes 	struct mbuf *m;
1062df8bae1dSRodney W. Grimes {
10639b44ff22SGarrett Wollman 	struct bpf_if *bp = ifp->if_bpf;
1064df8bae1dSRodney W. Grimes 	struct bpf_d *d;
1065df8bae1dSRodney W. Grimes 	u_int pktlen, slen;
1066df8bae1dSRodney W. Grimes 	struct mbuf *m0;
1067df8bae1dSRodney W. Grimes 
1068df8bae1dSRodney W. Grimes 	pktlen = 0;
1069df8bae1dSRodney W. Grimes 	for (m0 = m; m0 != 0; m0 = m0->m_next)
1070df8bae1dSRodney W. Grimes 		pktlen += m0->m_len;
1071df8bae1dSRodney W. Grimes 
1072e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
1073df8bae1dSRodney W. Grimes 	for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
10748ed3828cSRobert Watson 		if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL))
10758ed3828cSRobert Watson 			continue;
1076e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1077df8bae1dSRodney W. Grimes 		++d->bd_rcount;
1078df8bae1dSRodney W. Grimes 		slen = bpf_filter(d->bd_filter, (u_char *)m, pktlen, 0);
1079df8bae1dSRodney W. Grimes 		if (slen != 0)
1080df8bae1dSRodney W. Grimes 			catchpacket(d, (u_char *)m, pktlen, slen, bpf_mcopy);
1081e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1082df8bae1dSRodney W. Grimes 	}
1083e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
1084df8bae1dSRodney W. Grimes }
1085df8bae1dSRodney W. Grimes 
1086df8bae1dSRodney W. Grimes /*
1087df8bae1dSRodney W. Grimes  * Move the packet data from interface memory (pkt) into the
1088df8bae1dSRodney W. Grimes  * store buffer.  Return 1 if it's time to wakeup a listener (buffer full),
1089df8bae1dSRodney W. Grimes  * otherwise 0.  "copy" is the routine called to do the actual data
1090df8bae1dSRodney W. Grimes  * transfer.  bcopy is passed in to copy contiguous chunks, while
1091df8bae1dSRodney W. Grimes  * bpf_mcopy is passed in to copy mbuf chains.  In the latter case,
1092df8bae1dSRodney W. Grimes  * pkt is really an mbuf.
1093df8bae1dSRodney W. Grimes  */
1094df8bae1dSRodney W. Grimes static void
1095df8bae1dSRodney W. Grimes catchpacket(d, pkt, pktlen, snaplen, cpfn)
1096df8bae1dSRodney W. Grimes 	register struct bpf_d *d;
1097df8bae1dSRodney W. Grimes 	register u_char *pkt;
1098df8bae1dSRodney W. Grimes 	register u_int pktlen, snaplen;
10998bcbc7dfSAlexander Langer 	register void (*cpfn) __P((const void *, void *, size_t));
1100df8bae1dSRodney W. Grimes {
1101df8bae1dSRodney W. Grimes 	register struct bpf_hdr *hp;
1102df8bae1dSRodney W. Grimes 	register int totlen, curlen;
1103df8bae1dSRodney W. Grimes 	register int hdrlen = d->bd_bif->bif_hdrlen;
1104df8bae1dSRodney W. Grimes 	/*
1105df8bae1dSRodney W. Grimes 	 * Figure out how many bytes to move.  If the packet is
1106df8bae1dSRodney W. Grimes 	 * greater or equal to the snapshot length, transfer that
1107df8bae1dSRodney W. Grimes 	 * much.  Otherwise, transfer the whole packet (unless
1108df8bae1dSRodney W. Grimes 	 * we hit the buffer size limit).
1109df8bae1dSRodney W. Grimes 	 */
1110df8bae1dSRodney W. Grimes 	totlen = hdrlen + min(snaplen, pktlen);
1111df8bae1dSRodney W. Grimes 	if (totlen > d->bd_bufsize)
1112df8bae1dSRodney W. Grimes 		totlen = d->bd_bufsize;
1113df8bae1dSRodney W. Grimes 
1114df8bae1dSRodney W. Grimes 	/*
1115df8bae1dSRodney W. Grimes 	 * Round up the end of the previous packet to the next longword.
1116df8bae1dSRodney W. Grimes 	 */
1117df8bae1dSRodney W. Grimes 	curlen = BPF_WORDALIGN(d->bd_slen);
1118df8bae1dSRodney W. Grimes 	if (curlen + totlen > d->bd_bufsize) {
1119df8bae1dSRodney W. Grimes 		/*
1120df8bae1dSRodney W. Grimes 		 * This packet will overflow the storage buffer.
1121df8bae1dSRodney W. Grimes 		 * Rotate the buffers if we can, then wakeup any
1122df8bae1dSRodney W. Grimes 		 * pending reads.
1123df8bae1dSRodney W. Grimes 		 */
1124df8bae1dSRodney W. Grimes 		if (d->bd_fbuf == 0) {
1125df8bae1dSRodney W. Grimes 			/*
1126df8bae1dSRodney W. Grimes 			 * We haven't completed the previous read yet,
1127df8bae1dSRodney W. Grimes 			 * so drop the packet.
1128df8bae1dSRodney W. Grimes 			 */
1129df8bae1dSRodney W. Grimes 			++d->bd_dcount;
1130df8bae1dSRodney W. Grimes 			return;
1131df8bae1dSRodney W. Grimes 		}
1132df8bae1dSRodney W. Grimes 		ROTATE_BUFFERS(d);
1133df8bae1dSRodney W. Grimes 		bpf_wakeup(d);
1134df8bae1dSRodney W. Grimes 		curlen = 0;
1135df8bae1dSRodney W. Grimes 	}
1136df8bae1dSRodney W. Grimes 	else if (d->bd_immediate)
1137df8bae1dSRodney W. Grimes 		/*
1138df8bae1dSRodney W. Grimes 		 * Immediate mode is set.  A packet arrived so any
1139df8bae1dSRodney W. Grimes 		 * reads should be woken up.
1140df8bae1dSRodney W. Grimes 		 */
1141df8bae1dSRodney W. Grimes 		bpf_wakeup(d);
1142df8bae1dSRodney W. Grimes 
1143df8bae1dSRodney W. Grimes 	/*
1144df8bae1dSRodney W. Grimes 	 * Append the bpf header.
1145df8bae1dSRodney W. Grimes 	 */
1146df8bae1dSRodney W. Grimes 	hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
1147df8bae1dSRodney W. Grimes 	microtime(&hp->bh_tstamp);
1148df8bae1dSRodney W. Grimes 	hp->bh_datalen = pktlen;
1149df8bae1dSRodney W. Grimes 	hp->bh_hdrlen = hdrlen;
1150df8bae1dSRodney W. Grimes 	/*
1151df8bae1dSRodney W. Grimes 	 * Copy the packet data into the store buffer and update its length.
1152df8bae1dSRodney W. Grimes 	 */
1153df8bae1dSRodney W. Grimes 	(*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen));
1154df8bae1dSRodney W. Grimes 	d->bd_slen = curlen + totlen;
1155df8bae1dSRodney W. Grimes }
1156df8bae1dSRodney W. Grimes 
1157df8bae1dSRodney W. Grimes /*
1158df8bae1dSRodney W. Grimes  * Initialize all nonzero fields of a descriptor.
1159df8bae1dSRodney W. Grimes  */
1160df8bae1dSRodney W. Grimes static int
1161df8bae1dSRodney W. Grimes bpf_allocbufs(d)
1162df8bae1dSRodney W. Grimes 	register struct bpf_d *d;
1163df8bae1dSRodney W. Grimes {
1164bd3a5320SPoul-Henning Kamp 	d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1165df8bae1dSRodney W. Grimes 	if (d->bd_fbuf == 0)
1166df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1167df8bae1dSRodney W. Grimes 
1168bd3a5320SPoul-Henning Kamp 	d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1169df8bae1dSRodney W. Grimes 	if (d->bd_sbuf == 0) {
1170bd3a5320SPoul-Henning Kamp 		free(d->bd_fbuf, M_BPF);
1171df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1172df8bae1dSRodney W. Grimes 	}
1173df8bae1dSRodney W. Grimes 	d->bd_slen = 0;
1174df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
1175df8bae1dSRodney W. Grimes 	return (0);
1176df8bae1dSRodney W. Grimes }
1177df8bae1dSRodney W. Grimes 
1178df8bae1dSRodney W. Grimes /*
1179df8bae1dSRodney W. Grimes  * Free buffers currently in use by a descriptor.
1180df8bae1dSRodney W. Grimes  * Called on close.
1181df8bae1dSRodney W. Grimes  */
1182df8bae1dSRodney W. Grimes static void
1183df8bae1dSRodney W. Grimes bpf_freed(d)
1184df8bae1dSRodney W. Grimes 	register struct bpf_d *d;
1185df8bae1dSRodney W. Grimes {
1186df8bae1dSRodney W. Grimes 	/*
1187df8bae1dSRodney W. Grimes 	 * We don't need to lock out interrupts since this descriptor has
1188df8bae1dSRodney W. Grimes 	 * been detached from its interface and it yet hasn't been marked
1189df8bae1dSRodney W. Grimes 	 * free.
1190df8bae1dSRodney W. Grimes 	 */
1191df8bae1dSRodney W. Grimes 	if (d->bd_sbuf != 0) {
1192bd3a5320SPoul-Henning Kamp 		free(d->bd_sbuf, M_BPF);
1193df8bae1dSRodney W. Grimes 		if (d->bd_hbuf != 0)
1194bd3a5320SPoul-Henning Kamp 			free(d->bd_hbuf, M_BPF);
1195df8bae1dSRodney W. Grimes 		if (d->bd_fbuf != 0)
1196bd3a5320SPoul-Henning Kamp 			free(d->bd_fbuf, M_BPF);
1197df8bae1dSRodney W. Grimes 	}
1198df8bae1dSRodney W. Grimes 	if (d->bd_filter)
1199bd3a5320SPoul-Henning Kamp 		free((caddr_t)d->bd_filter, M_BPF);
1200e7bb21b3SJonathan Lemon 	mtx_destroy(&d->bd_mtx);
1201df8bae1dSRodney W. Grimes }
1202df8bae1dSRodney W. Grimes 
1203df8bae1dSRodney W. Grimes /*
1204df8bae1dSRodney W. Grimes  * Attach an interface to bpf.  driverp is a pointer to a (struct bpf_if *)
1205df8bae1dSRodney W. Grimes  * in the driver's softc; dlt is the link layer type; hdrlen is the fixed
1206df8bae1dSRodney W. Grimes  * size of the link header (variable length headers not yet supported).
1207df8bae1dSRodney W. Grimes  */
1208df8bae1dSRodney W. Grimes void
12099b44ff22SGarrett Wollman bpfattach(ifp, dlt, hdrlen)
1210df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
1211df8bae1dSRodney W. Grimes 	u_int dlt, hdrlen;
1212df8bae1dSRodney W. Grimes {
1213df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
1214e3b4e866SBosko Milekic 	bp = (struct bpf_if *)malloc(sizeof(*bp), M_BPF, M_NOWAIT);
1215df8bae1dSRodney W. Grimes 	if (bp == 0)
1216df8bae1dSRodney W. Grimes 		panic("bpfattach");
1217df8bae1dSRodney W. Grimes 
1218df8bae1dSRodney W. Grimes 	bp->bif_dlist = 0;
1219df8bae1dSRodney W. Grimes 	bp->bif_ifp = ifp;
1220df8bae1dSRodney W. Grimes 	bp->bif_dlt = dlt;
1221e7bb21b3SJonathan Lemon 	mtx_init(&bp->bif_mtx, "bpf interface lock", MTX_DEF);
1222df8bae1dSRodney W. Grimes 
1223e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
1224df8bae1dSRodney W. Grimes 	bp->bif_next = bpf_iflist;
1225df8bae1dSRodney W. Grimes 	bpf_iflist = bp;
1226e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
1227df8bae1dSRodney W. Grimes 
12289b44ff22SGarrett Wollman 	bp->bif_ifp->if_bpf = 0;
1229df8bae1dSRodney W. Grimes 
1230df8bae1dSRodney W. Grimes 	/*
1231df8bae1dSRodney W. Grimes 	 * Compute the length of the bpf header.  This is not necessarily
1232df8bae1dSRodney W. Grimes 	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1233df8bae1dSRodney W. Grimes 	 * that the network layer header begins on a longword boundary (for
1234df8bae1dSRodney W. Grimes 	 * performance reasons and to alleviate alignment restrictions).
1235df8bae1dSRodney W. Grimes 	 */
1236df8bae1dSRodney W. Grimes 	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1237df8bae1dSRodney W. Grimes 
12382eeab939SGarrett Wollman 	if (bootverbose)
1239df8bae1dSRodney W. Grimes 		printf("bpf: %s%d attached\n", ifp->if_name, ifp->if_unit);
1240df8bae1dSRodney W. Grimes }
124153ac6efbSJulian Elischer 
1242de5d9935SRobert Watson /*
1243de5d9935SRobert Watson  * Detach bpf from an interface.  This involves detaching each descriptor
1244de5d9935SRobert Watson  * associated with the interface, and leaving bd_bif NULL.  Notify each
1245de5d9935SRobert Watson  * descriptor as it's detached so that any sleepers wake up and get
1246de5d9935SRobert Watson  * ENXIO.
1247de5d9935SRobert Watson  */
1248de5d9935SRobert Watson void
1249de5d9935SRobert Watson bpfdetach(ifp)
1250de5d9935SRobert Watson 	struct ifnet *ifp;
1251de5d9935SRobert Watson {
1252de5d9935SRobert Watson 	struct bpf_if	*bp, *bp_prev;
1253de5d9935SRobert Watson 	struct bpf_d	*d;
1254de5d9935SRobert Watson 
1255e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
1256de5d9935SRobert Watson 
1257de5d9935SRobert Watson 	/* Locate BPF interface information */
1258de5d9935SRobert Watson 	bp_prev = NULL;
1259de5d9935SRobert Watson 	for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
1260de5d9935SRobert Watson 		if (ifp == bp->bif_ifp)
1261de5d9935SRobert Watson 			break;
1262de5d9935SRobert Watson 		bp_prev = bp;
1263de5d9935SRobert Watson 	}
1264de5d9935SRobert Watson 
1265de5d9935SRobert Watson 	/* Interface wasn't attached */
1266de5d9935SRobert Watson 	if (bp->bif_ifp == NULL) {
1267e7bb21b3SJonathan Lemon 		mtx_unlock(&bpf_mtx);
1268de5d9935SRobert Watson 		printf("bpfdetach: %s%d was not attached\n", ifp->if_name,
1269de5d9935SRobert Watson 		    ifp->if_unit);
1270de5d9935SRobert Watson 		return;
1271de5d9935SRobert Watson 	}
1272de5d9935SRobert Watson 
1273de5d9935SRobert Watson 	if (bp_prev) {
1274de5d9935SRobert Watson 		bp_prev->bif_next = bp->bif_next;
1275de5d9935SRobert Watson 	} else {
1276de5d9935SRobert Watson 		bpf_iflist = bp->bif_next;
1277de5d9935SRobert Watson 	}
1278de5d9935SRobert Watson 
1279e7bb21b3SJonathan Lemon 	while ((d = bp->bif_dlist) != NULL) {
1280e7bb21b3SJonathan Lemon 		bpf_detachd(d);
1281e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1282e7bb21b3SJonathan Lemon 		bpf_wakeup(d);
1283e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1284e7bb21b3SJonathan Lemon 	}
1285e7bb21b3SJonathan Lemon 
1286e7bb21b3SJonathan Lemon 	mtx_destroy(&bp->bif_mtx);
1287de5d9935SRobert Watson 	free(bp, M_BPF);
1288de5d9935SRobert Watson 
1289e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
1290de5d9935SRobert Watson }
1291de5d9935SRobert Watson 
1292514ede09SBruce Evans static void bpf_drvinit __P((void *unused));
1293bd3a5320SPoul-Henning Kamp 
12943f54a085SPoul-Henning Kamp static void bpf_clone __P((void *arg, char *name, int namelen, dev_t *dev));
12953f54a085SPoul-Henning Kamp 
12963f54a085SPoul-Henning Kamp static void
12973f54a085SPoul-Henning Kamp bpf_clone(arg, name, namelen, dev)
12983f54a085SPoul-Henning Kamp 	void *arg;
12993f54a085SPoul-Henning Kamp 	char *name;
13003f54a085SPoul-Henning Kamp 	int namelen;
13013f54a085SPoul-Henning Kamp 	dev_t *dev;
13023f54a085SPoul-Henning Kamp {
13033f54a085SPoul-Henning Kamp 	int u;
13043f54a085SPoul-Henning Kamp 
13053f54a085SPoul-Henning Kamp 	if (*dev != NODEV)
13063f54a085SPoul-Henning Kamp 		return;
1307db901281SPoul-Henning Kamp 	if (dev_stdclone(name, NULL, "bpf", &u) != 1)
13083f54a085SPoul-Henning Kamp 		return;
1309b0d17ba6SPoul-Henning Kamp 	*dev = make_dev(&bpf_cdevsw, unit2minor(u), UID_ROOT, GID_WHEEL, 0600,
1310b0d17ba6SPoul-Henning Kamp 	    "bpf%d", u);
1311b0d17ba6SPoul-Henning Kamp 	(*dev)->si_flags |= SI_CHEAPCLONE;
13123f54a085SPoul-Henning Kamp 	return;
13133f54a085SPoul-Henning Kamp }
13143f54a085SPoul-Henning Kamp 
1315514ede09SBruce Evans static void
1316514ede09SBruce Evans bpf_drvinit(unused)
1317514ede09SBruce Evans 	void *unused;
131853ac6efbSJulian Elischer {
131953ac6efbSJulian Elischer 
1320e7bb21b3SJonathan Lemon 	mtx_init(&bpf_mtx, "bpf global lock", MTX_DEF);
1321db901281SPoul-Henning Kamp 	EVENTHANDLER_REGISTER(dev_clone, bpf_clone, 0, 1000);
13222447bec8SPoul-Henning Kamp 	cdevsw_add(&bpf_cdevsw);
13237198bf47SJulian Elischer }
132453ac6efbSJulian Elischer 
132553ac6efbSJulian Elischer SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,bpf_drvinit,NULL)
132653ac6efbSJulian Elischer 
13275bb5f2c9SPeter Wemm #else /* !DEV_BPF && !NETGRAPH_BPF */
1328f8dc4716SMike Smith /*
1329f8dc4716SMike Smith  * NOP stubs to allow bpf-using drivers to load and function.
1330f8dc4716SMike Smith  *
1331f8dc4716SMike Smith  * A 'better' implementation would allow the core bpf functionality
1332f8dc4716SMike Smith  * to be loaded at runtime.
1333f8dc4716SMike Smith  */
1334f8dc4716SMike Smith 
1335f8dc4716SMike Smith void
1336f8dc4716SMike Smith bpf_tap(ifp, pkt, pktlen)
1337f8dc4716SMike Smith 	struct ifnet *ifp;
1338f8dc4716SMike Smith 	register u_char *pkt;
1339f8dc4716SMike Smith 	register u_int pktlen;
1340f8dc4716SMike Smith {
1341f8dc4716SMike Smith }
1342f8dc4716SMike Smith 
1343f8dc4716SMike Smith void
1344f8dc4716SMike Smith bpf_mtap(ifp, m)
1345f8dc4716SMike Smith 	struct ifnet *ifp;
1346f8dc4716SMike Smith 	struct mbuf *m;
1347f8dc4716SMike Smith {
1348f8dc4716SMike Smith }
1349f8dc4716SMike Smith 
1350f8dc4716SMike Smith void
1351f8dc4716SMike Smith bpfattach(ifp, dlt, hdrlen)
1352f8dc4716SMike Smith 	struct ifnet *ifp;
1353f8dc4716SMike Smith 	u_int dlt, hdrlen;
1354f8dc4716SMike Smith {
1355f8dc4716SMike Smith }
1356f8dc4716SMike Smith 
1357da626c17SBill Paul void
1358da626c17SBill Paul bpfdetach(ifp)
1359da626c17SBill Paul 	struct ifnet *ifp;
1360da626c17SBill Paul {
1361da626c17SBill Paul }
1362da626c17SBill Paul 
1363f8dc4716SMike Smith u_int
1364f8dc4716SMike Smith bpf_filter(pc, p, wirelen, buflen)
13651f8ffa4bSJulian Elischer 	register const struct bpf_insn *pc;
1366f8dc4716SMike Smith 	register u_char *p;
1367f8dc4716SMike Smith 	u_int wirelen;
1368f8dc4716SMike Smith 	register u_int buflen;
1369f8dc4716SMike Smith {
1370f8dc4716SMike Smith 	return -1;	/* "no filter" behaviour */
1371f8dc4716SMike Smith }
1372f8dc4716SMike Smith 
13735bb5f2c9SPeter Wemm int
13745bb5f2c9SPeter Wemm bpf_validate(f, len)
13755bb5f2c9SPeter Wemm 	const struct bpf_insn *f;
13765bb5f2c9SPeter Wemm 	int len;
13775bb5f2c9SPeter Wemm {
13785bb5f2c9SPeter Wemm 	return 0;		/* false */
13795bb5f2c9SPeter Wemm }
13805bb5f2c9SPeter Wemm 
13815bb5f2c9SPeter Wemm #endif /* !DEV_BPF && !NETGRAPH_BPF */
1382