xref: /freebsd/sys/net/bpf.c (revision 69f7644bc9ec1f0caf98e5a10af2ac7ae4ff4380)
1c398230bSWarner Losh /*-
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  * 4. Neither the name of the University nor the names of its contributors
19df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
20df8bae1dSRodney W. Grimes  *    without specific prior written permission.
21df8bae1dSRodney W. Grimes  *
22df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
33df8bae1dSRodney W. Grimes  *
344f252c4dSRuslan Ermilov  *      @(#)bpf.c	8.4 (Berkeley) 1/9/95
35df8bae1dSRodney W. Grimes  *
36c3aac50fSPeter Wemm  * $FreeBSD$
37df8bae1dSRodney W. Grimes  */
38df8bae1dSRodney W. Grimes 
395bb5f2c9SPeter Wemm #include "opt_bpf.h"
4082f4445dSRobert Watson #include "opt_mac.h"
415bb5f2c9SPeter Wemm #include "opt_netgraph.h"
42df8bae1dSRodney W. Grimes 
4395aab9ccSJohn-Mark Gurney #include <sys/types.h>
44df8bae1dSRodney W. Grimes #include <sys/param.h>
45df8bae1dSRodney W. Grimes #include <sys/systm.h>
46ce7609a4SBruce Evans #include <sys/conf.h>
47e76eee55SPoul-Henning Kamp #include <sys/fcntl.h>
4882f4445dSRobert Watson #include <sys/mac.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>
57e76eee55SPoul-Henning Kamp #include <sys/uio.h>
58df8bae1dSRodney W. Grimes 
5995aab9ccSJohn-Mark Gurney #include <sys/event.h>
6095aab9ccSJohn-Mark Gurney #include <sys/file.h>
61243ac7d8SPeter Wemm #include <sys/poll.h>
6295aab9ccSJohn-Mark Gurney #include <sys/proc.h>
63df8bae1dSRodney W. Grimes 
64df8bae1dSRodney W. Grimes #include <sys/socket.h>
65df8bae1dSRodney W. Grimes 
66fba9235dSBruce Evans #include <net/if.h>
67df8bae1dSRodney W. Grimes #include <net/bpf.h>
68df8bae1dSRodney W. Grimes #include <net/bpfdesc.h>
69df8bae1dSRodney W. Grimes 
70df8bae1dSRodney W. Grimes #include <netinet/in.h>
71df8bae1dSRodney W. Grimes #include <netinet/if_ether.h>
72df8bae1dSRodney W. Grimes #include <sys/kernel.h>
73f708ef1bSPoul-Henning Kamp #include <sys/sysctl.h>
747b778b5eSEivind Eklund 
75959b7375SPoul-Henning Kamp static MALLOC_DEFINE(M_BPF, "BPF", "BPF data");
7687f6c662SJulian Elischer 
775bb5f2c9SPeter Wemm #if defined(DEV_BPF) || defined(NETGRAPH_BPF)
7853ac6efbSJulian Elischer 
79df8bae1dSRodney W. Grimes #define PRINET  26			/* interruptible */
80df8bae1dSRodney W. Grimes 
81df8bae1dSRodney W. Grimes /*
82d1a67300SRobert Watson  * bpf_iflist is a list of BPF interface structures, each corresponding to a
83d1a67300SRobert Watson  * specific DLT.  The same network interface might have several BPF interface
84d1a67300SRobert Watson  * structures registered by different layers in the stack (i.e., 802.11
85d1a67300SRobert Watson  * frames, ethernet frames, etc).
86df8bae1dSRodney W. Grimes  */
874a3feeaaSRobert Watson static LIST_HEAD(, bpf_if)	bpf_iflist;
88e7bb21b3SJonathan Lemon static struct mtx	bpf_mtx;		/* bpf global lock */
8969f7644bSChristian S.J. Peron static int		bpf_bpfd_cnt;
90df8bae1dSRodney W. Grimes 
91929ddbbbSAlfred Perlstein static int	bpf_allocbufs(struct bpf_d *);
92929ddbbbSAlfred Perlstein static void	bpf_attachd(struct bpf_d *d, struct bpf_if *bp);
93929ddbbbSAlfred Perlstein static void	bpf_detachd(struct bpf_d *d);
94929ddbbbSAlfred Perlstein static void	bpf_freed(struct bpf_d *);
95929ddbbbSAlfred Perlstein static void	bpf_mcopy(const void *, void *, size_t);
9601399f34SDavid Malone static int	bpf_movein(struct uio *, int, int,
9701399f34SDavid Malone 		    struct mbuf **, struct sockaddr *);
98929ddbbbSAlfred Perlstein static int	bpf_setif(struct bpf_d *, struct ifreq *);
99929ddbbbSAlfred Perlstein static void	bpf_timed_out(void *);
100e7bb21b3SJonathan Lemon static __inline void
101929ddbbbSAlfred Perlstein 		bpf_wakeup(struct bpf_d *);
102929ddbbbSAlfred Perlstein static void	catchpacket(struct bpf_d *, u_char *, u_int,
103929ddbbbSAlfred Perlstein 		    u_int, void (*)(const void *, void *, size_t));
104929ddbbbSAlfred Perlstein static void	reset_d(struct bpf_d *);
105929ddbbbSAlfred Perlstein static int	 bpf_setf(struct bpf_d *, struct bpf_program *);
1068eab61f3SSam Leffler static int	bpf_getdltlist(struct bpf_d *, struct bpf_dltlist *);
1078eab61f3SSam Leffler static int	bpf_setdlt(struct bpf_d *, u_int);
10895aab9ccSJohn-Mark Gurney static void	filt_bpfdetach(struct knote *);
10995aab9ccSJohn-Mark Gurney static int	filt_bpfread(struct knote *, long);
110a3272e3cSChristian S.J. Peron static void	bpf_drvinit(void *);
111a3272e3cSChristian S.J. Peron static void	bpf_clone(void *, char *, int, struct cdev **);
11269f7644bSChristian S.J. Peron static int	bpf_stats_sysctl(SYSCTL_HANDLER_ARGS);
11369f7644bSChristian S.J. Peron 
11469f7644bSChristian S.J. Peron /*
11569f7644bSChristian S.J. Peron  * The default read buffer size is patchable.
11669f7644bSChristian S.J. Peron  */
11769f7644bSChristian S.J. Peron SYSCTL_NODE(_net, OID_AUTO, bpf, CTLFLAG_RW, 0, "bpf sysctl");
11869f7644bSChristian S.J. Peron static int bpf_bufsize = 4096;
11969f7644bSChristian S.J. Peron SYSCTL_INT(_net_bpf, OID_AUTO, bufsize, CTLFLAG_RW,
12069f7644bSChristian S.J. Peron     &bpf_bufsize, 0, "");
12169f7644bSChristian S.J. Peron static int bpf_maxbufsize = BPF_MAXBUFSIZE;
12269f7644bSChristian S.J. Peron SYSCTL_INT(_net_bpf, OID_AUTO, maxbufsize, CTLFLAG_RW,
12369f7644bSChristian S.J. Peron     &bpf_maxbufsize, 0, "");
12469f7644bSChristian S.J. Peron static int bpf_maxinsns = BPF_MAXINSNS;
12569f7644bSChristian S.J. Peron SYSCTL_INT(_net_bpf, OID_AUTO, maxinsns, CTLFLAG_RW,
12669f7644bSChristian S.J. Peron     &bpf_maxinsns, 0, "Maximum bpf program instructions");
12769f7644bSChristian S.J. Peron SYSCTL_NODE(_net_bpf, OID_AUTO, stats, CTLFLAG_RW,
12869f7644bSChristian S.J. Peron     bpf_stats_sysctl, "bpf statistics portal");
129df8bae1dSRodney W. Grimes 
13087f6c662SJulian Elischer static	d_open_t	bpfopen;
13187f6c662SJulian Elischer static	d_close_t	bpfclose;
13287f6c662SJulian Elischer static	d_read_t	bpfread;
13387f6c662SJulian Elischer static	d_write_t	bpfwrite;
13487f6c662SJulian Elischer static	d_ioctl_t	bpfioctl;
135243ac7d8SPeter Wemm static	d_poll_t	bpfpoll;
13695aab9ccSJohn-Mark Gurney static	d_kqfilter_t	bpfkqfilter;
13787f6c662SJulian Elischer 
1384e2f199eSPoul-Henning Kamp static struct cdevsw bpf_cdevsw = {
139dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
140dc08ffecSPoul-Henning Kamp 	.d_flags =	D_NEEDGIANT,
1417ac40f5fSPoul-Henning Kamp 	.d_open =	bpfopen,
1427ac40f5fSPoul-Henning Kamp 	.d_close =	bpfclose,
1437ac40f5fSPoul-Henning Kamp 	.d_read =	bpfread,
1447ac40f5fSPoul-Henning Kamp 	.d_write =	bpfwrite,
1457ac40f5fSPoul-Henning Kamp 	.d_ioctl =	bpfioctl,
1467ac40f5fSPoul-Henning Kamp 	.d_poll =	bpfpoll,
1477ac40f5fSPoul-Henning Kamp 	.d_name =	"bpf",
14895aab9ccSJohn-Mark Gurney 	.d_kqfilter =	bpfkqfilter,
1494e2f199eSPoul-Henning Kamp };
15087f6c662SJulian Elischer 
15195aab9ccSJohn-Mark Gurney static struct filterops bpfread_filtops =
15295aab9ccSJohn-Mark Gurney 	{ 1, NULL, filt_bpfdetach, filt_bpfread };
15387f6c662SJulian Elischer 
154df8bae1dSRodney W. Grimes static int
15501399f34SDavid Malone bpf_movein(uio, linktype, mtu, mp, sockp)
1568994a245SDag-Erling Smørgrav 	struct uio *uio;
15701399f34SDavid Malone 	int linktype;
15801399f34SDavid Malone 	int mtu;
1598994a245SDag-Erling Smørgrav 	struct mbuf **mp;
1608994a245SDag-Erling Smørgrav 	struct sockaddr *sockp;
161df8bae1dSRodney W. Grimes {
162df8bae1dSRodney W. Grimes 	struct mbuf *m;
163df8bae1dSRodney W. Grimes 	int error;
164df8bae1dSRodney W. Grimes 	int len;
165df8bae1dSRodney W. Grimes 	int hlen;
166df8bae1dSRodney W. Grimes 
167df8bae1dSRodney W. Grimes 	/*
168df8bae1dSRodney W. Grimes 	 * Build a sockaddr based on the data link layer type.
169df8bae1dSRodney W. Grimes 	 * We do this at this level because the ethernet header
170df8bae1dSRodney W. Grimes 	 * is copied directly into the data field of the sockaddr.
171df8bae1dSRodney W. Grimes 	 * In the case of SLIP, there is no header and the packet
172df8bae1dSRodney W. Grimes 	 * is forwarded as is.
173df8bae1dSRodney W. Grimes 	 * Also, we are careful to leave room at the front of the mbuf
174df8bae1dSRodney W. Grimes 	 * for the link level header.
175df8bae1dSRodney W. Grimes 	 */
176df8bae1dSRodney W. Grimes 	switch (linktype) {
177df8bae1dSRodney W. Grimes 
178df8bae1dSRodney W. Grimes 	case DLT_SLIP:
179df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_INET;
180df8bae1dSRodney W. Grimes 		hlen = 0;
181df8bae1dSRodney W. Grimes 		break;
182df8bae1dSRodney W. Grimes 
183df8bae1dSRodney W. Grimes 	case DLT_EN10MB:
184df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_UNSPEC;
185df8bae1dSRodney W. Grimes 		/* XXX Would MAXLINKHDR be better? */
186797f247bSMatthew N. Dodd 		hlen = ETHER_HDR_LEN;
187df8bae1dSRodney W. Grimes 		break;
188df8bae1dSRodney W. Grimes 
189df8bae1dSRodney W. Grimes 	case DLT_FDDI:
190d41f24e7SDavid Greenman 		sockp->sa_family = AF_IMPLINK;
191d41f24e7SDavid Greenman 		hlen = 0;
192df8bae1dSRodney W. Grimes 		break;
193df8bae1dSRodney W. Grimes 
19422f05c43SAndrey A. Chernov 	case DLT_RAW:
195df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_UNSPEC;
196df8bae1dSRodney W. Grimes 		hlen = 0;
197df8bae1dSRodney W. Grimes 		break;
198df8bae1dSRodney W. Grimes 
19901399f34SDavid Malone 	case DLT_NULL:
20001399f34SDavid Malone 		/*
20101399f34SDavid Malone 		 * null interface types require a 4 byte pseudo header which
20201399f34SDavid Malone 		 * corresponds to the address family of the packet.
20301399f34SDavid Malone 		 */
20401399f34SDavid Malone 		sockp->sa_family = AF_UNSPEC;
20501399f34SDavid Malone 		hlen = 4;
20601399f34SDavid Malone 		break;
20701399f34SDavid Malone 
2084f53e3ccSKenjiro Cho 	case DLT_ATM_RFC1483:
2094f53e3ccSKenjiro Cho 		/*
2104f53e3ccSKenjiro Cho 		 * en atm driver requires 4-byte atm pseudo header.
2114f53e3ccSKenjiro Cho 		 * though it isn't standard, vpi:vci needs to be
2124f53e3ccSKenjiro Cho 		 * specified anyway.
2134f53e3ccSKenjiro Cho 		 */
2144f53e3ccSKenjiro Cho 		sockp->sa_family = AF_UNSPEC;
2154f53e3ccSKenjiro Cho 		hlen = 12;	/* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
2164f53e3ccSKenjiro Cho 		break;
2174f53e3ccSKenjiro Cho 
21830fa52a6SBrian Somers 	case DLT_PPP:
21930fa52a6SBrian Somers 		sockp->sa_family = AF_UNSPEC;
22030fa52a6SBrian Somers 		hlen = 4;	/* This should match PPP_HDRLEN */
22130fa52a6SBrian Somers 		break;
22230fa52a6SBrian Somers 
223df8bae1dSRodney W. Grimes 	default:
224df8bae1dSRodney W. Grimes 		return (EIO);
225df8bae1dSRodney W. Grimes 	}
226df8bae1dSRodney W. Grimes 
227df8bae1dSRodney W. Grimes 	len = uio->uio_resid;
22801399f34SDavid Malone 
22901399f34SDavid Malone 	if (len - hlen > mtu)
23001399f34SDavid Malone 		return (EMSGSIZE);
23101399f34SDavid Malone 
232df8bae1dSRodney W. Grimes 	if ((unsigned)len > MCLBYTES)
233df8bae1dSRodney W. Grimes 		return (EIO);
234df8bae1dSRodney W. Grimes 
235963e4c2aSGarrett Wollman 	if (len > MHLEN) {
236a163d034SWarner Losh 		m = m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
23724a229f4SSam Leffler 	} else {
238a163d034SWarner Losh 		MGETHDR(m, M_TRYWAIT, MT_DATA);
239df8bae1dSRodney W. Grimes 	}
24024a229f4SSam Leffler 	if (m == NULL)
24124a229f4SSam Leffler 		return (ENOBUFS);
242963e4c2aSGarrett Wollman 	m->m_pkthdr.len = m->m_len = len;
243963e4c2aSGarrett Wollman 	m->m_pkthdr.rcvif = NULL;
244df8bae1dSRodney W. Grimes 	*mp = m;
24524a229f4SSam Leffler 
246df8bae1dSRodney W. Grimes 	/*
247df8bae1dSRodney W. Grimes 	 * Make room for link header.
248df8bae1dSRodney W. Grimes 	 */
249df8bae1dSRodney W. Grimes 	if (hlen != 0) {
2504f079e2fSGarrett Wollman 		m->m_pkthdr.len -= hlen;
251df8bae1dSRodney W. Grimes 		m->m_len -= hlen;
252df8bae1dSRodney W. Grimes #if BSD >= 199103
253df8bae1dSRodney W. Grimes 		m->m_data += hlen; /* XXX */
254df8bae1dSRodney W. Grimes #else
255df8bae1dSRodney W. Grimes 		m->m_off += hlen;
256df8bae1dSRodney W. Grimes #endif
257c9524588SDag-Erling Smørgrav 		error = uiomove(sockp->sa_data, hlen, uio);
258df8bae1dSRodney W. Grimes 		if (error)
259df8bae1dSRodney W. Grimes 			goto bad;
260df8bae1dSRodney W. Grimes 	}
261c9524588SDag-Erling Smørgrav 	error = uiomove(mtod(m, void *), len - hlen, uio);
262df8bae1dSRodney W. Grimes 	if (!error)
263df8bae1dSRodney W. Grimes 		return (0);
264df8bae1dSRodney W. Grimes bad:
265df8bae1dSRodney W. Grimes 	m_freem(m);
266df8bae1dSRodney W. Grimes 	return (error);
267df8bae1dSRodney W. Grimes }
268df8bae1dSRodney W. Grimes 
269df8bae1dSRodney W. Grimes /*
270df8bae1dSRodney W. Grimes  * Attach file to the bpf interface, i.e. make d listen on bp.
271df8bae1dSRodney W. Grimes  */
272df8bae1dSRodney W. Grimes static void
273df8bae1dSRodney W. Grimes bpf_attachd(d, bp)
274df8bae1dSRodney W. Grimes 	struct bpf_d *d;
275df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
276df8bae1dSRodney W. Grimes {
277df8bae1dSRodney W. Grimes 	/*
278df8bae1dSRodney W. Grimes 	 * Point d at bp, and add d to the interface's list of listeners.
279df8bae1dSRodney W. Grimes 	 * Finally, point the driver's bpf cookie at the interface so
280df8bae1dSRodney W. Grimes 	 * it will divert packets to bpf.
281df8bae1dSRodney W. Grimes 	 */
282e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
283df8bae1dSRodney W. Grimes 	d->bd_bif = bp;
2844a3feeaaSRobert Watson 	LIST_INSERT_HEAD(&bp->bif_dlist, d, bd_next);
285df8bae1dSRodney W. Grimes 
28669f7644bSChristian S.J. Peron 	bpf_bpfd_cnt++;
28724a229f4SSam Leffler 	*bp->bif_driverp = bp;
288e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
289df8bae1dSRodney W. Grimes }
290df8bae1dSRodney W. Grimes 
291df8bae1dSRodney W. Grimes /*
292df8bae1dSRodney W. Grimes  * Detach a file from its interface.
293df8bae1dSRodney W. Grimes  */
294df8bae1dSRodney W. Grimes static void
295df8bae1dSRodney W. Grimes bpf_detachd(d)
296df8bae1dSRodney W. Grimes 	struct bpf_d *d;
297df8bae1dSRodney W. Grimes {
2986e891d64SPoul-Henning Kamp 	int error;
299df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
30046448b5aSRobert Watson 	struct ifnet *ifp;
301df8bae1dSRodney W. Grimes 
302df8bae1dSRodney W. Grimes 	bp = d->bd_bif;
30346448b5aSRobert Watson 	BPFIF_LOCK(bp);
30446448b5aSRobert Watson 	BPFD_LOCK(d);
30546448b5aSRobert Watson 	ifp = d->bd_bif->bif_ifp;
30646448b5aSRobert Watson 
30746448b5aSRobert Watson 	/*
30846448b5aSRobert Watson 	 * Remove d from the interface's descriptor list.
30946448b5aSRobert Watson 	 */
31046448b5aSRobert Watson 	LIST_REMOVE(d, bd_next);
31146448b5aSRobert Watson 
31269f7644bSChristian S.J. Peron 	bpf_bpfd_cnt--;
31346448b5aSRobert Watson 	/*
31446448b5aSRobert Watson 	 * Let the driver know that there are no more listeners.
31546448b5aSRobert Watson 	 */
31646448b5aSRobert Watson 	if (LIST_EMPTY(&bp->bif_dlist))
31746448b5aSRobert Watson 		*bp->bif_driverp = NULL;
31846448b5aSRobert Watson 
319572bde2aSRobert Watson 	d->bd_bif = NULL;
32046448b5aSRobert Watson 	BPFD_UNLOCK(d);
32146448b5aSRobert Watson 	BPFIF_UNLOCK(bp);
32246448b5aSRobert Watson 
323df8bae1dSRodney W. Grimes 	/*
324df8bae1dSRodney W. Grimes 	 * Check if this descriptor had requested promiscuous mode.
325df8bae1dSRodney W. Grimes 	 * If so, turn it off.
326df8bae1dSRodney W. Grimes 	 */
327df8bae1dSRodney W. Grimes 	if (d->bd_promisc) {
328df8bae1dSRodney W. Grimes 		d->bd_promisc = 0;
32946448b5aSRobert Watson 		error = ifpromisc(ifp, 0);
3306e891d64SPoul-Henning Kamp 		if (error != 0 && error != ENXIO) {
331df8bae1dSRodney W. Grimes 			/*
3326e891d64SPoul-Henning Kamp 			 * ENXIO can happen if a pccard is unplugged
333df8bae1dSRodney W. Grimes 			 * Something is really wrong if we were able to put
334df8bae1dSRodney W. Grimes 			 * the driver into promiscuous mode, but can't
335df8bae1dSRodney W. Grimes 			 * take it out.
336df8bae1dSRodney W. Grimes 			 */
3378eab61f3SSam Leffler 			if_printf(bp->bif_ifp,
3388eab61f3SSam Leffler 				"bpf_detach: ifpromisc failed (%d)\n", error);
3396e891d64SPoul-Henning Kamp 		}
340df8bae1dSRodney W. Grimes 	}
341df8bae1dSRodney W. Grimes }
342df8bae1dSRodney W. Grimes 
343df8bae1dSRodney W. Grimes /*
344df8bae1dSRodney W. Grimes  * Open ethernet device.  Returns ENXIO for illegal minor device number,
345df8bae1dSRodney W. Grimes  * EBUSY if file is open by another process.
346df8bae1dSRodney W. Grimes  */
347df8bae1dSRodney W. Grimes /* ARGSUSED */
34887f6c662SJulian Elischer static	int
349b40ce416SJulian Elischer bpfopen(dev, flags, fmt, td)
35089c9c53dSPoul-Henning Kamp 	struct cdev *dev;
35160039670SBruce Evans 	int flags;
35260039670SBruce Evans 	int fmt;
353b40ce416SJulian Elischer 	struct thread *td;
354df8bae1dSRodney W. Grimes {
355e7bb21b3SJonathan Lemon 	struct bpf_d *d;
356df8bae1dSRodney W. Grimes 
357e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
358bd3a5320SPoul-Henning Kamp 	d = dev->si_drv1;
359df8bae1dSRodney W. Grimes 	/*
360df8bae1dSRodney W. Grimes 	 * Each minor can be opened by only one process.  If the requested
361df8bae1dSRodney W. Grimes 	 * minor is in use, return EBUSY.
362df8bae1dSRodney W. Grimes 	 */
363d17d8184SRobert Watson 	if (d != NULL) {
364e7bb21b3SJonathan Lemon 		mtx_unlock(&bpf_mtx);
365df8bae1dSRodney W. Grimes 		return (EBUSY);
366e7bb21b3SJonathan Lemon 	}
367e7bb21b3SJonathan Lemon 	dev->si_drv1 = (struct bpf_d *)~0;	/* mark device in use */
368e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
369e7bb21b3SJonathan Lemon 
370d1d74c28SJohn Baldwin 	if ((dev->si_flags & SI_NAMED) == 0)
371b0d17ba6SPoul-Henning Kamp 		make_dev(&bpf_cdevsw, minor(dev), UID_ROOT, GID_WHEEL, 0600,
372b0d17ba6SPoul-Henning Kamp 		    "bpf%d", dev2unit(dev));
373a163d034SWarner Losh 	MALLOC(d, struct bpf_d *, sizeof(*d), M_BPF, M_WAITOK | M_ZERO);
374bd3a5320SPoul-Henning Kamp 	dev->si_drv1 = d;
375df8bae1dSRodney W. Grimes 	d->bd_bufsize = bpf_bufsize;
37600a83887SPaul Traina 	d->bd_sig = SIGIO;
3778ed3828cSRobert Watson 	d->bd_seesent = 1;
37869f7644bSChristian S.J. Peron 	d->bd_pid = td->td_proc->p_pid;
37969f7644bSChristian S.J. Peron 	strlcpy(d->bd_pcomm, td->td_proc->p_comm, MAXCOMLEN);
38082f4445dSRobert Watson #ifdef MAC
38182f4445dSRobert Watson 	mac_init_bpfdesc(d);
38282f4445dSRobert Watson 	mac_create_bpfdesc(td->td_ucred, d);
38382f4445dSRobert Watson #endif
3846008862bSJohn Baldwin 	mtx_init(&d->bd_mtx, devtoname(dev), "bpf cdev lock", MTX_DEF);
38531199c84SGleb Smirnoff 	callout_init(&d->bd_callout, NET_CALLOUT_MPSAFE);
386571dcd15SSuleiman Souhlal 	knlist_init(&d->bd_sel.si_note, &d->bd_mtx, NULL, NULL, NULL);
387df8bae1dSRodney W. Grimes 
388df8bae1dSRodney W. Grimes 	return (0);
389df8bae1dSRodney W. Grimes }
390df8bae1dSRodney W. Grimes 
391df8bae1dSRodney W. Grimes /*
392df8bae1dSRodney W. Grimes  * Close the descriptor by detaching it from its interface,
393df8bae1dSRodney W. Grimes  * deallocating its buffers, and marking it free.
394df8bae1dSRodney W. Grimes  */
395df8bae1dSRodney W. Grimes /* ARGSUSED */
39687f6c662SJulian Elischer static	int
397b40ce416SJulian Elischer bpfclose(dev, flags, fmt, td)
39889c9c53dSPoul-Henning Kamp 	struct cdev *dev;
39960039670SBruce Evans 	int flags;
40060039670SBruce Evans 	int fmt;
401b40ce416SJulian Elischer 	struct thread *td;
402df8bae1dSRodney W. Grimes {
403e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
404df8bae1dSRodney W. Grimes 
40581bda851SJohn Polstra 	BPFD_LOCK(d);
40681bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
40781bda851SJohn Polstra 		callout_stop(&d->bd_callout);
40881bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
40981bda851SJohn Polstra 	BPFD_UNLOCK(d);
410e649887bSAlfred Perlstein 	funsetown(&d->bd_sigio);
411e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
412df8bae1dSRodney W. Grimes 	if (d->bd_bif)
413df8bae1dSRodney W. Grimes 		bpf_detachd(d);
414e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
4154549709fSBrian Feldman 	selwakeuppri(&d->bd_sel, PRINET);
41682f4445dSRobert Watson #ifdef MAC
41782f4445dSRobert Watson 	mac_destroy_bpfdesc(d);
41882f4445dSRobert Watson #endif /* MAC */
419ad3b9257SJohn-Mark Gurney 	knlist_destroy(&d->bd_sel.si_note);
420df8bae1dSRodney W. Grimes 	bpf_freed(d);
421d17d8184SRobert Watson 	dev->si_drv1 = NULL;
422d722be54SLuigi Rizzo 	free(d, M_BPF);
423df8bae1dSRodney W. Grimes 
424df8bae1dSRodney W. Grimes 	return (0);
425df8bae1dSRodney W. Grimes }
426df8bae1dSRodney W. Grimes 
427df8bae1dSRodney W. Grimes 
428df8bae1dSRodney W. Grimes /*
429df8bae1dSRodney W. Grimes  * Rotate the packet buffers in descriptor d.  Move the store buffer
430df8bae1dSRodney W. Grimes  * into the hold slot, and the free buffer into the store slot.
431df8bae1dSRodney W. Grimes  * Zero the length of the new store buffer.
432df8bae1dSRodney W. Grimes  */
433df8bae1dSRodney W. Grimes #define ROTATE_BUFFERS(d) \
434df8bae1dSRodney W. Grimes 	(d)->bd_hbuf = (d)->bd_sbuf; \
435df8bae1dSRodney W. Grimes 	(d)->bd_hlen = (d)->bd_slen; \
436df8bae1dSRodney W. Grimes 	(d)->bd_sbuf = (d)->bd_fbuf; \
437df8bae1dSRodney W. Grimes 	(d)->bd_slen = 0; \
438d17d8184SRobert Watson 	(d)->bd_fbuf = NULL;
439df8bae1dSRodney W. Grimes /*
440df8bae1dSRodney W. Grimes  *  bpfread - read next chunk of packets from buffers
441df8bae1dSRodney W. Grimes  */
44287f6c662SJulian Elischer static	int
44360039670SBruce Evans bpfread(dev, uio, ioflag)
44489c9c53dSPoul-Henning Kamp 	struct cdev *dev;
4458994a245SDag-Erling Smørgrav 	struct uio *uio;
44660039670SBruce Evans 	int ioflag;
447df8bae1dSRodney W. Grimes {
448e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
44981bda851SJohn Polstra 	int timed_out;
450df8bae1dSRodney W. Grimes 	int error;
451df8bae1dSRodney W. Grimes 
452df8bae1dSRodney W. Grimes 	/*
453df8bae1dSRodney W. Grimes 	 * Restrict application to use a buffer the same size as
454df8bae1dSRodney W. Grimes 	 * as kernel buffers.
455df8bae1dSRodney W. Grimes 	 */
456df8bae1dSRodney W. Grimes 	if (uio->uio_resid != d->bd_bufsize)
457df8bae1dSRodney W. Grimes 		return (EINVAL);
458df8bae1dSRodney W. Grimes 
459e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
46081bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
46181bda851SJohn Polstra 		callout_stop(&d->bd_callout);
46281bda851SJohn Polstra 	timed_out = (d->bd_state == BPF_TIMED_OUT);
46381bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
464df8bae1dSRodney W. Grimes 	/*
465df8bae1dSRodney W. Grimes 	 * If the hold buffer is empty, then do a timed sleep, which
466df8bae1dSRodney W. Grimes 	 * ends when the timeout expires or when enough packets
467df8bae1dSRodney W. Grimes 	 * have arrived to fill the store buffer.
468df8bae1dSRodney W. Grimes 	 */
469572bde2aSRobert Watson 	while (d->bd_hbuf == NULL) {
47081bda851SJohn Polstra 		if ((d->bd_immediate || timed_out) && d->bd_slen != 0) {
471df8bae1dSRodney W. Grimes 			/*
472df8bae1dSRodney W. Grimes 			 * A packet(s) either arrived since the previous
473df8bae1dSRodney W. Grimes 			 * read or arrived while we were asleep.
474df8bae1dSRodney W. Grimes 			 * Rotate the buffers and return what's here.
475df8bae1dSRodney W. Grimes 			 */
476df8bae1dSRodney W. Grimes 			ROTATE_BUFFERS(d);
477df8bae1dSRodney W. Grimes 			break;
478df8bae1dSRodney W. Grimes 		}
479de5d9935SRobert Watson 
480de5d9935SRobert Watson 		/*
481de5d9935SRobert Watson 		 * No data is available, check to see if the bpf device
482de5d9935SRobert Watson 		 * is still pointed at a real interface.  If not, return
483de5d9935SRobert Watson 		 * ENXIO so that the userland process knows to rebind
484de5d9935SRobert Watson 		 * it before using it again.
485de5d9935SRobert Watson 		 */
486de5d9935SRobert Watson 		if (d->bd_bif == NULL) {
487e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
488de5d9935SRobert Watson 			return (ENXIO);
489de5d9935SRobert Watson 		}
490de5d9935SRobert Watson 
491e76eee55SPoul-Henning Kamp 		if (ioflag & O_NONBLOCK) {
492e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
493fba3cfdeSJohn Polstra 			return (EWOULDBLOCK);
494fba3cfdeSJohn Polstra 		}
495521f364bSDag-Erling Smørgrav 		error = msleep(d, &d->bd_mtx, PRINET|PCATCH,
496e7bb21b3SJonathan Lemon 		     "bpf", d->bd_rtout);
497df8bae1dSRodney W. Grimes 		if (error == EINTR || error == ERESTART) {
498e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
499df8bae1dSRodney W. Grimes 			return (error);
500df8bae1dSRodney W. Grimes 		}
501df8bae1dSRodney W. Grimes 		if (error == EWOULDBLOCK) {
502df8bae1dSRodney W. Grimes 			/*
503df8bae1dSRodney W. Grimes 			 * On a timeout, return what's in the buffer,
504df8bae1dSRodney W. Grimes 			 * which may be nothing.  If there is something
505df8bae1dSRodney W. Grimes 			 * in the store buffer, we can rotate the buffers.
506df8bae1dSRodney W. Grimes 			 */
507df8bae1dSRodney W. Grimes 			if (d->bd_hbuf)
508df8bae1dSRodney W. Grimes 				/*
509df8bae1dSRodney W. Grimes 				 * We filled up the buffer in between
510df8bae1dSRodney W. Grimes 				 * getting the timeout and arriving
511df8bae1dSRodney W. Grimes 				 * here, so we don't need to rotate.
512df8bae1dSRodney W. Grimes 				 */
513df8bae1dSRodney W. Grimes 				break;
514df8bae1dSRodney W. Grimes 
515df8bae1dSRodney W. Grimes 			if (d->bd_slen == 0) {
516e7bb21b3SJonathan Lemon 				BPFD_UNLOCK(d);
517df8bae1dSRodney W. Grimes 				return (0);
518df8bae1dSRodney W. Grimes 			}
519df8bae1dSRodney W. Grimes 			ROTATE_BUFFERS(d);
520df8bae1dSRodney W. Grimes 			break;
521df8bae1dSRodney W. Grimes 		}
522df8bae1dSRodney W. Grimes 	}
523df8bae1dSRodney W. Grimes 	/*
524df8bae1dSRodney W. Grimes 	 * At this point, we know we have something in the hold slot.
525df8bae1dSRodney W. Grimes 	 */
526e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
527df8bae1dSRodney W. Grimes 
528df8bae1dSRodney W. Grimes 	/*
529df8bae1dSRodney W. Grimes 	 * Move data from hold buffer into user space.
530df8bae1dSRodney W. Grimes 	 * We know the entire buffer is transferred since
531df8bae1dSRodney W. Grimes 	 * we checked above that the read buffer is bpf_bufsize bytes.
532df8bae1dSRodney W. Grimes 	 */
533e7bb21b3SJonathan Lemon 	error = uiomove(d->bd_hbuf, d->bd_hlen, uio);
534df8bae1dSRodney W. Grimes 
535e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
536df8bae1dSRodney W. Grimes 	d->bd_fbuf = d->bd_hbuf;
537572bde2aSRobert Watson 	d->bd_hbuf = NULL;
538df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
539e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
540df8bae1dSRodney W. Grimes 
541df8bae1dSRodney W. Grimes 	return (error);
542df8bae1dSRodney W. Grimes }
543df8bae1dSRodney W. Grimes 
544df8bae1dSRodney W. Grimes 
545df8bae1dSRodney W. Grimes /*
546df8bae1dSRodney W. Grimes  * If there are processes sleeping on this descriptor, wake them up.
547df8bae1dSRodney W. Grimes  */
548e7bb21b3SJonathan Lemon static __inline void
549df8bae1dSRodney W. Grimes bpf_wakeup(d)
5508994a245SDag-Erling Smørgrav 	struct bpf_d *d;
551df8bae1dSRodney W. Grimes {
552a3272e3cSChristian S.J. Peron 
553a3272e3cSChristian S.J. Peron 	BPFD_LOCK_ASSERT(d);
55481bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING) {
55581bda851SJohn Polstra 		callout_stop(&d->bd_callout);
55681bda851SJohn Polstra 		d->bd_state = BPF_IDLE;
55781bda851SJohn Polstra 	}
558521f364bSDag-Erling Smørgrav 	wakeup(d);
559831d27a9SDon Lewis 	if (d->bd_async && d->bd_sig && d->bd_sigio)
560f1320723SAlfred Perlstein 		pgsigio(&d->bd_sigio, d->bd_sig, 0);
56100a83887SPaul Traina 
562512824f8SSeigo Tanimura 	selwakeuppri(&d->bd_sel, PRINET);
563ad3b9257SJohn-Mark Gurney 	KNOTE_LOCKED(&d->bd_sel.si_note, 0);
564df8bae1dSRodney W. Grimes }
565df8bae1dSRodney W. Grimes 
56681bda851SJohn Polstra static void
56781bda851SJohn Polstra bpf_timed_out(arg)
56881bda851SJohn Polstra 	void *arg;
56981bda851SJohn Polstra {
57081bda851SJohn Polstra 	struct bpf_d *d = (struct bpf_d *)arg;
57181bda851SJohn Polstra 
57281bda851SJohn Polstra 	BPFD_LOCK(d);
57381bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING) {
57481bda851SJohn Polstra 		d->bd_state = BPF_TIMED_OUT;
57581bda851SJohn Polstra 		if (d->bd_slen != 0)
57681bda851SJohn Polstra 			bpf_wakeup(d);
57781bda851SJohn Polstra 	}
57881bda851SJohn Polstra 	BPFD_UNLOCK(d);
57981bda851SJohn Polstra }
58081bda851SJohn Polstra 
58187f6c662SJulian Elischer static	int
58260039670SBruce Evans bpfwrite(dev, uio, ioflag)
58389c9c53dSPoul-Henning Kamp 	struct cdev *dev;
584df8bae1dSRodney W. Grimes 	struct uio *uio;
58560039670SBruce Evans 	int ioflag;
586df8bae1dSRodney W. Grimes {
587e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
588df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
589df8bae1dSRodney W. Grimes 	struct mbuf *m;
590e7bb21b3SJonathan Lemon 	int error;
5918240bf1eSRobert Watson 	struct sockaddr dst;
592df8bae1dSRodney W. Grimes 
593572bde2aSRobert Watson 	if (d->bd_bif == NULL)
594df8bae1dSRodney W. Grimes 		return (ENXIO);
595df8bae1dSRodney W. Grimes 
596df8bae1dSRodney W. Grimes 	ifp = d->bd_bif->bif_ifp;
597df8bae1dSRodney W. Grimes 
5983518d220SSam Leffler 	if ((ifp->if_flags & IFF_UP) == 0)
5993518d220SSam Leffler 		return (ENETDOWN);
6003518d220SSam Leffler 
601df8bae1dSRodney W. Grimes 	if (uio->uio_resid == 0)
602df8bae1dSRodney W. Grimes 		return (0);
603df8bae1dSRodney W. Grimes 
6048240bf1eSRobert Watson 	bzero(&dst, sizeof(dst));
60501399f34SDavid Malone 	error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, ifp->if_mtu, &m, &dst);
606df8bae1dSRodney W. Grimes 	if (error)
607df8bae1dSRodney W. Grimes 		return (error);
608df8bae1dSRodney W. Grimes 
609114ae644SMike Smith 	if (d->bd_hdrcmplt)
610114ae644SMike Smith 		dst.sa_family = pseudo_AF_HDRCMPLT;
611114ae644SMike Smith 
61282f4445dSRobert Watson #ifdef MAC
613f747d2ddSRobert Watson 	BPFD_LOCK(d);
61482f4445dSRobert Watson 	mac_create_mbuf_from_bpfdesc(d, m);
615f747d2ddSRobert Watson 	BPFD_UNLOCK(d);
61682f4445dSRobert Watson #endif
617b8f9429dSRobert Watson 	NET_LOCK_GIANT();
618572bde2aSRobert Watson 	error = (*ifp->if_output)(ifp, m, &dst, NULL);
619b8f9429dSRobert Watson 	NET_UNLOCK_GIANT();
620df8bae1dSRodney W. Grimes 	/*
621df8bae1dSRodney W. Grimes 	 * The driver frees the mbuf.
622df8bae1dSRodney W. Grimes 	 */
623df8bae1dSRodney W. Grimes 	return (error);
624df8bae1dSRodney W. Grimes }
625df8bae1dSRodney W. Grimes 
626df8bae1dSRodney W. Grimes /*
627df8bae1dSRodney W. Grimes  * Reset a descriptor by flushing its packet buffer and clearing the
628e7bb21b3SJonathan Lemon  * receive and drop counts.
629df8bae1dSRodney W. Grimes  */
630df8bae1dSRodney W. Grimes static void
631df8bae1dSRodney W. Grimes reset_d(d)
632df8bae1dSRodney W. Grimes 	struct bpf_d *d;
633df8bae1dSRodney W. Grimes {
634e7bb21b3SJonathan Lemon 
635e7bb21b3SJonathan Lemon 	mtx_assert(&d->bd_mtx, MA_OWNED);
636df8bae1dSRodney W. Grimes 	if (d->bd_hbuf) {
637df8bae1dSRodney W. Grimes 		/* Free the hold buffer. */
638df8bae1dSRodney W. Grimes 		d->bd_fbuf = d->bd_hbuf;
639572bde2aSRobert Watson 		d->bd_hbuf = NULL;
640df8bae1dSRodney W. Grimes 	}
641df8bae1dSRodney W. Grimes 	d->bd_slen = 0;
642df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
643df8bae1dSRodney W. Grimes 	d->bd_rcount = 0;
644df8bae1dSRodney W. Grimes 	d->bd_dcount = 0;
64569f7644bSChristian S.J. Peron 	d->bd_fcount = 0;
646df8bae1dSRodney W. Grimes }
647df8bae1dSRodney W. Grimes 
648df8bae1dSRodney W. Grimes /*
649df8bae1dSRodney W. Grimes  *  FIONREAD		Check for read packet available.
650df8bae1dSRodney W. Grimes  *  SIOCGIFADDR		Get interface address - convenient hook to driver.
651df8bae1dSRodney W. Grimes  *  BIOCGBLEN		Get buffer len [for read()].
652df8bae1dSRodney W. Grimes  *  BIOCSETF		Set ethernet read filter.
653df8bae1dSRodney W. Grimes  *  BIOCFLUSH		Flush read packet buffer.
654df8bae1dSRodney W. Grimes  *  BIOCPROMISC		Put interface into promiscuous mode.
655df8bae1dSRodney W. Grimes  *  BIOCGDLT		Get link layer type.
656df8bae1dSRodney W. Grimes  *  BIOCGETIF		Get interface name.
657df8bae1dSRodney W. Grimes  *  BIOCSETIF		Set interface.
658df8bae1dSRodney W. Grimes  *  BIOCSRTIMEOUT	Set read timeout.
659df8bae1dSRodney W. Grimes  *  BIOCGRTIMEOUT	Get read timeout.
660df8bae1dSRodney W. Grimes  *  BIOCGSTATS		Get packet stats.
661df8bae1dSRodney W. Grimes  *  BIOCIMMEDIATE	Set immediate mode.
662df8bae1dSRodney W. Grimes  *  BIOCVERSION		Get filter language version.
663114ae644SMike Smith  *  BIOCGHDRCMPLT	Get "header already complete" flag
664114ae644SMike Smith  *  BIOCSHDRCMPLT	Set "header already complete" flag
6658ed3828cSRobert Watson  *  BIOCGSEESENT	Get "see packets sent" flag
6668ed3828cSRobert Watson  *  BIOCSSEESENT	Set "see packets sent" flag
667df8bae1dSRodney W. Grimes  */
668df8bae1dSRodney W. Grimes /* ARGSUSED */
66987f6c662SJulian Elischer static	int
670b40ce416SJulian Elischer bpfioctl(dev, cmd, addr, flags, td)
67189c9c53dSPoul-Henning Kamp 	struct cdev *dev;
672ecbb00a2SDoug Rabson 	u_long cmd;
673df8bae1dSRodney W. Grimes 	caddr_t addr;
67460039670SBruce Evans 	int flags;
675b40ce416SJulian Elischer 	struct thread *td;
676df8bae1dSRodney W. Grimes {
677e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
678e7bb21b3SJonathan Lemon 	int error = 0;
679df8bae1dSRodney W. Grimes 
68081bda851SJohn Polstra 	BPFD_LOCK(d);
68181bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
68281bda851SJohn Polstra 		callout_stop(&d->bd_callout);
68381bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
68481bda851SJohn Polstra 	BPFD_UNLOCK(d);
68581bda851SJohn Polstra 
686df8bae1dSRodney W. Grimes 	switch (cmd) {
687df8bae1dSRodney W. Grimes 
688df8bae1dSRodney W. Grimes 	default:
689df8bae1dSRodney W. Grimes 		error = EINVAL;
690df8bae1dSRodney W. Grimes 		break;
691df8bae1dSRodney W. Grimes 
692df8bae1dSRodney W. Grimes 	/*
693df8bae1dSRodney W. Grimes 	 * Check for read packet available.
694df8bae1dSRodney W. Grimes 	 */
695df8bae1dSRodney W. Grimes 	case FIONREAD:
696df8bae1dSRodney W. Grimes 		{
697df8bae1dSRodney W. Grimes 			int n;
698df8bae1dSRodney W. Grimes 
699e7bb21b3SJonathan Lemon 			BPFD_LOCK(d);
700df8bae1dSRodney W. Grimes 			n = d->bd_slen;
701df8bae1dSRodney W. Grimes 			if (d->bd_hbuf)
702df8bae1dSRodney W. Grimes 				n += d->bd_hlen;
703e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
704df8bae1dSRodney W. Grimes 
705df8bae1dSRodney W. Grimes 			*(int *)addr = n;
706df8bae1dSRodney W. Grimes 			break;
707df8bae1dSRodney W. Grimes 		}
708df8bae1dSRodney W. Grimes 
709df8bae1dSRodney W. Grimes 	case SIOCGIFADDR:
710df8bae1dSRodney W. Grimes 		{
711df8bae1dSRodney W. Grimes 			struct ifnet *ifp;
712df8bae1dSRodney W. Grimes 
713572bde2aSRobert Watson 			if (d->bd_bif == NULL)
714df8bae1dSRodney W. Grimes 				error = EINVAL;
715df8bae1dSRodney W. Grimes 			else {
716df8bae1dSRodney W. Grimes 				ifp = d->bd_bif->bif_ifp;
717df8bae1dSRodney W. Grimes 				error = (*ifp->if_ioctl)(ifp, cmd, addr);
718df8bae1dSRodney W. Grimes 			}
719df8bae1dSRodney W. Grimes 			break;
720df8bae1dSRodney W. Grimes 		}
721df8bae1dSRodney W. Grimes 
722df8bae1dSRodney W. Grimes 	/*
723df8bae1dSRodney W. Grimes 	 * Get buffer len [for read()].
724df8bae1dSRodney W. Grimes 	 */
725df8bae1dSRodney W. Grimes 	case BIOCGBLEN:
726df8bae1dSRodney W. Grimes 		*(u_int *)addr = d->bd_bufsize;
727df8bae1dSRodney W. Grimes 		break;
728df8bae1dSRodney W. Grimes 
729df8bae1dSRodney W. Grimes 	/*
730df8bae1dSRodney W. Grimes 	 * Set buffer length.
731df8bae1dSRodney W. Grimes 	 */
732df8bae1dSRodney W. Grimes 	case BIOCSBLEN:
733572bde2aSRobert Watson 		if (d->bd_bif != NULL)
734df8bae1dSRodney W. Grimes 			error = EINVAL;
735df8bae1dSRodney W. Grimes 		else {
7368994a245SDag-Erling Smørgrav 			u_int size = *(u_int *)addr;
737df8bae1dSRodney W. Grimes 
738eba2a1aeSPoul-Henning Kamp 			if (size > bpf_maxbufsize)
739eba2a1aeSPoul-Henning Kamp 				*(u_int *)addr = size = bpf_maxbufsize;
740df8bae1dSRodney W. Grimes 			else if (size < BPF_MINBUFSIZE)
741df8bae1dSRodney W. Grimes 				*(u_int *)addr = size = BPF_MINBUFSIZE;
742df8bae1dSRodney W. Grimes 			d->bd_bufsize = size;
743df8bae1dSRodney W. Grimes 		}
744df8bae1dSRodney W. Grimes 		break;
745df8bae1dSRodney W. Grimes 
746df8bae1dSRodney W. Grimes 	/*
747df8bae1dSRodney W. Grimes 	 * Set link layer read filter.
748df8bae1dSRodney W. Grimes 	 */
749df8bae1dSRodney W. Grimes 	case BIOCSETF:
750df8bae1dSRodney W. Grimes 		error = bpf_setf(d, (struct bpf_program *)addr);
751df8bae1dSRodney W. Grimes 		break;
752df8bae1dSRodney W. Grimes 
753df8bae1dSRodney W. Grimes 	/*
754df8bae1dSRodney W. Grimes 	 * Flush read packet buffer.
755df8bae1dSRodney W. Grimes 	 */
756df8bae1dSRodney W. Grimes 	case BIOCFLUSH:
757e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
758df8bae1dSRodney W. Grimes 		reset_d(d);
759e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
760df8bae1dSRodney W. Grimes 		break;
761df8bae1dSRodney W. Grimes 
762df8bae1dSRodney W. Grimes 	/*
763df8bae1dSRodney W. Grimes 	 * Put interface into promiscuous mode.
764df8bae1dSRodney W. Grimes 	 */
765df8bae1dSRodney W. Grimes 	case BIOCPROMISC:
766572bde2aSRobert Watson 		if (d->bd_bif == NULL) {
767df8bae1dSRodney W. Grimes 			/*
768df8bae1dSRodney W. Grimes 			 * No interface attached yet.
769df8bae1dSRodney W. Grimes 			 */
770df8bae1dSRodney W. Grimes 			error = EINVAL;
771df8bae1dSRodney W. Grimes 			break;
772df8bae1dSRodney W. Grimes 		}
773df8bae1dSRodney W. Grimes 		if (d->bd_promisc == 0) {
774e7bb21b3SJonathan Lemon 			mtx_lock(&Giant);
775df8bae1dSRodney W. Grimes 			error = ifpromisc(d->bd_bif->bif_ifp, 1);
776e7bb21b3SJonathan Lemon 			mtx_unlock(&Giant);
777df8bae1dSRodney W. Grimes 			if (error == 0)
778df8bae1dSRodney W. Grimes 				d->bd_promisc = 1;
779df8bae1dSRodney W. Grimes 		}
780df8bae1dSRodney W. Grimes 		break;
781df8bae1dSRodney W. Grimes 
782df8bae1dSRodney W. Grimes 	/*
7838eab61f3SSam Leffler 	 * Get current data link type.
784df8bae1dSRodney W. Grimes 	 */
785df8bae1dSRodney W. Grimes 	case BIOCGDLT:
786572bde2aSRobert Watson 		if (d->bd_bif == NULL)
787df8bae1dSRodney W. Grimes 			error = EINVAL;
788df8bae1dSRodney W. Grimes 		else
789df8bae1dSRodney W. Grimes 			*(u_int *)addr = d->bd_bif->bif_dlt;
790df8bae1dSRodney W. Grimes 		break;
791df8bae1dSRodney W. Grimes 
792df8bae1dSRodney W. Grimes 	/*
7938eab61f3SSam Leffler 	 * Get a list of supported data link types.
7948eab61f3SSam Leffler 	 */
7958eab61f3SSam Leffler 	case BIOCGDLTLIST:
796572bde2aSRobert Watson 		if (d->bd_bif == NULL)
7978eab61f3SSam Leffler 			error = EINVAL;
7988eab61f3SSam Leffler 		else
7998eab61f3SSam Leffler 			error = bpf_getdltlist(d, (struct bpf_dltlist *)addr);
8008eab61f3SSam Leffler 		break;
8018eab61f3SSam Leffler 
8028eab61f3SSam Leffler 	/*
8038eab61f3SSam Leffler 	 * Set data link type.
8048eab61f3SSam Leffler 	 */
8058eab61f3SSam Leffler 	case BIOCSDLT:
806572bde2aSRobert Watson 		if (d->bd_bif == NULL)
8078eab61f3SSam Leffler 			error = EINVAL;
8088eab61f3SSam Leffler 		else
8098eab61f3SSam Leffler 			error = bpf_setdlt(d, *(u_int *)addr);
8108eab61f3SSam Leffler 		break;
8118eab61f3SSam Leffler 
8128eab61f3SSam Leffler 	/*
8131dd0feaaSArchie Cobbs 	 * Get interface name.
814df8bae1dSRodney W. Grimes 	 */
815df8bae1dSRodney W. Grimes 	case BIOCGETIF:
816572bde2aSRobert Watson 		if (d->bd_bif == NULL)
817df8bae1dSRodney W. Grimes 			error = EINVAL;
8181dd0feaaSArchie Cobbs 		else {
8191dd0feaaSArchie Cobbs 			struct ifnet *const ifp = d->bd_bif->bif_ifp;
8201dd0feaaSArchie Cobbs 			struct ifreq *const ifr = (struct ifreq *)addr;
8211dd0feaaSArchie Cobbs 
8229bf40edeSBrooks Davis 			strlcpy(ifr->ifr_name, ifp->if_xname,
8239bf40edeSBrooks Davis 			    sizeof(ifr->ifr_name));
8241dd0feaaSArchie Cobbs 		}
825df8bae1dSRodney W. Grimes 		break;
826df8bae1dSRodney W. Grimes 
827df8bae1dSRodney W. Grimes 	/*
828df8bae1dSRodney W. Grimes 	 * Set interface.
829df8bae1dSRodney W. Grimes 	 */
830df8bae1dSRodney W. Grimes 	case BIOCSETIF:
831df8bae1dSRodney W. Grimes 		error = bpf_setif(d, (struct ifreq *)addr);
832df8bae1dSRodney W. Grimes 		break;
833df8bae1dSRodney W. Grimes 
834df8bae1dSRodney W. Grimes 	/*
835df8bae1dSRodney W. Grimes 	 * Set read timeout.
836df8bae1dSRodney W. Grimes 	 */
837df8bae1dSRodney W. Grimes 	case BIOCSRTIMEOUT:
838df8bae1dSRodney W. Grimes 		{
839df8bae1dSRodney W. Grimes 			struct timeval *tv = (struct timeval *)addr;
840df8bae1dSRodney W. Grimes 
841bdc2cdc5SAlexander Langer 			/*
842bdc2cdc5SAlexander Langer 			 * Subtract 1 tick from tvtohz() since this isn't
843bdc2cdc5SAlexander Langer 			 * a one-shot timer.
844bdc2cdc5SAlexander Langer 			 */
845bdc2cdc5SAlexander Langer 			if ((error = itimerfix(tv)) == 0)
846bdc2cdc5SAlexander Langer 				d->bd_rtout = tvtohz(tv) - 1;
847df8bae1dSRodney W. Grimes 			break;
848df8bae1dSRodney W. Grimes 		}
849df8bae1dSRodney W. Grimes 
850df8bae1dSRodney W. Grimes 	/*
851df8bae1dSRodney W. Grimes 	 * Get read timeout.
852df8bae1dSRodney W. Grimes 	 */
853df8bae1dSRodney W. Grimes 	case BIOCGRTIMEOUT:
854df8bae1dSRodney W. Grimes 		{
855df8bae1dSRodney W. Grimes 			struct timeval *tv = (struct timeval *)addr;
856df8bae1dSRodney W. Grimes 
857bdc2cdc5SAlexander Langer 			tv->tv_sec = d->bd_rtout / hz;
858bdc2cdc5SAlexander Langer 			tv->tv_usec = (d->bd_rtout % hz) * tick;
859df8bae1dSRodney W. Grimes 			break;
860df8bae1dSRodney W. Grimes 		}
861df8bae1dSRodney W. Grimes 
862df8bae1dSRodney W. Grimes 	/*
863df8bae1dSRodney W. Grimes 	 * Get packet stats.
864df8bae1dSRodney W. Grimes 	 */
865df8bae1dSRodney W. Grimes 	case BIOCGSTATS:
866df8bae1dSRodney W. Grimes 		{
867df8bae1dSRodney W. Grimes 			struct bpf_stat *bs = (struct bpf_stat *)addr;
868df8bae1dSRodney W. Grimes 
869df8bae1dSRodney W. Grimes 			bs->bs_recv = d->bd_rcount;
870df8bae1dSRodney W. Grimes 			bs->bs_drop = d->bd_dcount;
871df8bae1dSRodney W. Grimes 			break;
872df8bae1dSRodney W. Grimes 		}
873df8bae1dSRodney W. Grimes 
874df8bae1dSRodney W. Grimes 	/*
875df8bae1dSRodney W. Grimes 	 * Set immediate mode.
876df8bae1dSRodney W. Grimes 	 */
877df8bae1dSRodney W. Grimes 	case BIOCIMMEDIATE:
878df8bae1dSRodney W. Grimes 		d->bd_immediate = *(u_int *)addr;
879df8bae1dSRodney W. Grimes 		break;
880df8bae1dSRodney W. Grimes 
881df8bae1dSRodney W. Grimes 	case BIOCVERSION:
882df8bae1dSRodney W. Grimes 		{
883df8bae1dSRodney W. Grimes 			struct bpf_version *bv = (struct bpf_version *)addr;
884df8bae1dSRodney W. Grimes 
885df8bae1dSRodney W. Grimes 			bv->bv_major = BPF_MAJOR_VERSION;
886df8bae1dSRodney W. Grimes 			bv->bv_minor = BPF_MINOR_VERSION;
887df8bae1dSRodney W. Grimes 			break;
888df8bae1dSRodney W. Grimes 		}
88900a83887SPaul Traina 
890114ae644SMike Smith 	/*
891114ae644SMike Smith 	 * Get "header already complete" flag
892114ae644SMike Smith 	 */
893114ae644SMike Smith 	case BIOCGHDRCMPLT:
894114ae644SMike Smith 		*(u_int *)addr = d->bd_hdrcmplt;
895114ae644SMike Smith 		break;
896114ae644SMike Smith 
897114ae644SMike Smith 	/*
898114ae644SMike Smith 	 * Set "header already complete" flag
899114ae644SMike Smith 	 */
900114ae644SMike Smith 	case BIOCSHDRCMPLT:
901114ae644SMike Smith 		d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
902114ae644SMike Smith 		break;
903114ae644SMike Smith 
9048ed3828cSRobert Watson 	/*
9058ed3828cSRobert Watson 	 * Get "see sent packets" flag
9068ed3828cSRobert Watson 	 */
9078ed3828cSRobert Watson 	case BIOCGSEESENT:
9088ed3828cSRobert Watson 		*(u_int *)addr = d->bd_seesent;
9098ed3828cSRobert Watson 		break;
9108ed3828cSRobert Watson 
9118ed3828cSRobert Watson 	/*
9128ed3828cSRobert Watson 	 * Set "see sent packets" flag
9138ed3828cSRobert Watson 	 */
9148ed3828cSRobert Watson 	case BIOCSSEESENT:
9158ed3828cSRobert Watson 		d->bd_seesent = *(u_int *)addr;
9168ed3828cSRobert Watson 		break;
9178ed3828cSRobert Watson 
91800a83887SPaul Traina 	case FIONBIO:		/* Non-blocking I/O */
91900a83887SPaul Traina 		break;
92000a83887SPaul Traina 
92100a83887SPaul Traina 	case FIOASYNC:		/* Send signal on receive packets */
92200a83887SPaul Traina 		d->bd_async = *(int *)addr;
92300a83887SPaul Traina 		break;
92400a83887SPaul Traina 
925831d27a9SDon Lewis 	case FIOSETOWN:
926831d27a9SDon Lewis 		error = fsetown(*(int *)addr, &d->bd_sigio);
92700a83887SPaul Traina 		break;
92800a83887SPaul Traina 
929831d27a9SDon Lewis 	case FIOGETOWN:
93091e97a82SDon Lewis 		*(int *)addr = fgetown(&d->bd_sigio);
931831d27a9SDon Lewis 		break;
932831d27a9SDon Lewis 
933831d27a9SDon Lewis 	/* This is deprecated, FIOSETOWN should be used instead. */
934831d27a9SDon Lewis 	case TIOCSPGRP:
935831d27a9SDon Lewis 		error = fsetown(-(*(int *)addr), &d->bd_sigio);
936831d27a9SDon Lewis 		break;
937831d27a9SDon Lewis 
938831d27a9SDon Lewis 	/* This is deprecated, FIOGETOWN should be used instead. */
93900a83887SPaul Traina 	case TIOCGPGRP:
94091e97a82SDon Lewis 		*(int *)addr = -fgetown(&d->bd_sigio);
94100a83887SPaul Traina 		break;
94200a83887SPaul Traina 
94300a83887SPaul Traina 	case BIOCSRSIG:		/* Set receive signal */
94400a83887SPaul Traina 		{
94500a83887SPaul Traina 			u_int sig;
94600a83887SPaul Traina 
94700a83887SPaul Traina 			sig = *(u_int *)addr;
94800a83887SPaul Traina 
94900a83887SPaul Traina 			if (sig >= NSIG)
95000a83887SPaul Traina 				error = EINVAL;
95100a83887SPaul Traina 			else
95200a83887SPaul Traina 				d->bd_sig = sig;
95300a83887SPaul Traina 			break;
95400a83887SPaul Traina 		}
95500a83887SPaul Traina 	case BIOCGRSIG:
95600a83887SPaul Traina 		*(u_int *)addr = d->bd_sig;
95700a83887SPaul Traina 		break;
958df8bae1dSRodney W. Grimes 	}
959df8bae1dSRodney W. Grimes 	return (error);
960df8bae1dSRodney W. Grimes }
961df8bae1dSRodney W. Grimes 
962df8bae1dSRodney W. Grimes /*
963df8bae1dSRodney W. Grimes  * Set d's packet filter program to fp.  If this file already has a filter,
964df8bae1dSRodney W. Grimes  * free it and replace it.  Returns EINVAL for bogus requests.
965df8bae1dSRodney W. Grimes  */
966f708ef1bSPoul-Henning Kamp static int
967df8bae1dSRodney W. Grimes bpf_setf(d, fp)
968df8bae1dSRodney W. Grimes 	struct bpf_d *d;
969df8bae1dSRodney W. Grimes 	struct bpf_program *fp;
970df8bae1dSRodney W. Grimes {
971df8bae1dSRodney W. Grimes 	struct bpf_insn *fcode, *old;
972df8bae1dSRodney W. Grimes 	u_int flen, size;
973df8bae1dSRodney W. Grimes 
974572bde2aSRobert Watson 	if (fp->bf_insns == NULL) {
975df8bae1dSRodney W. Grimes 		if (fp->bf_len != 0)
976df8bae1dSRodney W. Grimes 			return (EINVAL);
977e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
978a8e93fb7SRobert Watson 		old = d->bd_filter;
979572bde2aSRobert Watson 		d->bd_filter = NULL;
980df8bae1dSRodney W. Grimes 		reset_d(d);
981e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
982572bde2aSRobert Watson 		if (old != NULL)
983bd3a5320SPoul-Henning Kamp 			free((caddr_t)old, M_BPF);
984df8bae1dSRodney W. Grimes 		return (0);
985df8bae1dSRodney W. Grimes 	}
986df8bae1dSRodney W. Grimes 	flen = fp->bf_len;
9870eb20604SChristian S.J. Peron 	if (flen > bpf_maxinsns)
988df8bae1dSRodney W. Grimes 		return (EINVAL);
989df8bae1dSRodney W. Grimes 
990df8bae1dSRodney W. Grimes 	size = flen * sizeof(*fp->bf_insns);
991a163d034SWarner Losh 	fcode = (struct bpf_insn *)malloc(size, M_BPF, M_WAITOK);
992df8bae1dSRodney W. Grimes 	if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
993df8bae1dSRodney W. Grimes 	    bpf_validate(fcode, (int)flen)) {
994e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
995a8e93fb7SRobert Watson 		old = d->bd_filter;
996df8bae1dSRodney W. Grimes 		d->bd_filter = fcode;
997df8bae1dSRodney W. Grimes 		reset_d(d);
998e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
999572bde2aSRobert Watson 		if (old != NULL)
1000bd3a5320SPoul-Henning Kamp 			free((caddr_t)old, M_BPF);
1001df8bae1dSRodney W. Grimes 
1002df8bae1dSRodney W. Grimes 		return (0);
1003df8bae1dSRodney W. Grimes 	}
1004bd3a5320SPoul-Henning Kamp 	free((caddr_t)fcode, M_BPF);
1005df8bae1dSRodney W. Grimes 	return (EINVAL);
1006df8bae1dSRodney W. Grimes }
1007df8bae1dSRodney W. Grimes 
1008df8bae1dSRodney W. Grimes /*
1009df8bae1dSRodney W. Grimes  * Detach a file from its current interface (if attached at all) and attach
1010df8bae1dSRodney W. Grimes  * to the interface indicated by the name stored in ifr.
1011df8bae1dSRodney W. Grimes  * Return an errno or 0.
1012df8bae1dSRodney W. Grimes  */
1013df8bae1dSRodney W. Grimes static int
1014df8bae1dSRodney W. Grimes bpf_setif(d, ifr)
1015df8bae1dSRodney W. Grimes 	struct bpf_d *d;
1016df8bae1dSRodney W. Grimes 	struct ifreq *ifr;
1017df8bae1dSRodney W. Grimes {
1018df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
1019e7bb21b3SJonathan Lemon 	int error;
10209b44ff22SGarrett Wollman 	struct ifnet *theywant;
1021df8bae1dSRodney W. Grimes 
10229b44ff22SGarrett Wollman 	theywant = ifunit(ifr->ifr_name);
1023572bde2aSRobert Watson 	if (theywant == NULL)
10249b44ff22SGarrett Wollman 		return ENXIO;
10259b44ff22SGarrett Wollman 
1026df8bae1dSRodney W. Grimes 	/*
1027df8bae1dSRodney W. Grimes 	 * Look through attached interfaces for the named one.
1028df8bae1dSRodney W. Grimes 	 */
1029e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
10304a3feeaaSRobert Watson 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1031df8bae1dSRodney W. Grimes 		struct ifnet *ifp = bp->bif_ifp;
1032df8bae1dSRodney W. Grimes 
1033572bde2aSRobert Watson 		if (ifp == NULL || ifp != theywant)
1034df8bae1dSRodney W. Grimes 			continue;
103524a229f4SSam Leffler 		/* skip additional entry */
1036fc74a9f9SBrooks Davis 		if (bp->bif_driverp != &ifp->if_bpf)
103724a229f4SSam Leffler 			continue;
1038e7bb21b3SJonathan Lemon 
1039e7bb21b3SJonathan Lemon 		mtx_unlock(&bpf_mtx);
1040df8bae1dSRodney W. Grimes 		/*
1041df8bae1dSRodney W. Grimes 		 * We found the requested interface.
1042df8bae1dSRodney W. Grimes 		 * Allocate the packet buffers if we need to.
1043df8bae1dSRodney W. Grimes 		 * If we're already attached to requested interface,
1044df8bae1dSRodney W. Grimes 		 * just flush the buffer.
1045df8bae1dSRodney W. Grimes 		 */
1046572bde2aSRobert Watson 		if (d->bd_sbuf == NULL) {
1047df8bae1dSRodney W. Grimes 			error = bpf_allocbufs(d);
1048df8bae1dSRodney W. Grimes 			if (error != 0)
1049df8bae1dSRodney W. Grimes 				return (error);
1050df8bae1dSRodney W. Grimes 		}
1051df8bae1dSRodney W. Grimes 		if (bp != d->bd_bif) {
1052df8bae1dSRodney W. Grimes 			if (d->bd_bif)
1053df8bae1dSRodney W. Grimes 				/*
1054df8bae1dSRodney W. Grimes 				 * Detach if attached to something else.
1055df8bae1dSRodney W. Grimes 				 */
1056df8bae1dSRodney W. Grimes 				bpf_detachd(d);
1057df8bae1dSRodney W. Grimes 
1058df8bae1dSRodney W. Grimes 			bpf_attachd(d, bp);
1059df8bae1dSRodney W. Grimes 		}
1060e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1061df8bae1dSRodney W. Grimes 		reset_d(d);
1062e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1063df8bae1dSRodney W. Grimes 		return (0);
1064df8bae1dSRodney W. Grimes 	}
1065e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
1066df8bae1dSRodney W. Grimes 	/* Not found. */
1067df8bae1dSRodney W. Grimes 	return (ENXIO);
1068df8bae1dSRodney W. Grimes }
1069df8bae1dSRodney W. Grimes 
1070df8bae1dSRodney W. Grimes /*
1071243ac7d8SPeter Wemm  * Support for select() and poll() system calls
1072df8bae1dSRodney W. Grimes  *
1073df8bae1dSRodney W. Grimes  * Return true iff the specific operation will not block indefinitely.
1074df8bae1dSRodney W. Grimes  * Otherwise, return false but make a note that a selwakeup() must be done.
1075df8bae1dSRodney W. Grimes  */
107637c84183SPoul-Henning Kamp static int
1077b40ce416SJulian Elischer bpfpoll(dev, events, td)
107889c9c53dSPoul-Henning Kamp 	struct cdev *dev;
1079243ac7d8SPeter Wemm 	int events;
1080b40ce416SJulian Elischer 	struct thread *td;
1081df8bae1dSRodney W. Grimes {
1082e7bb21b3SJonathan Lemon 	struct bpf_d *d;
10830832fc64SGarance A Drosehn 	int revents;
1084df8bae1dSRodney W. Grimes 
1085bd3a5320SPoul-Henning Kamp 	d = dev->si_drv1;
1086de5d9935SRobert Watson 	if (d->bd_bif == NULL)
1087de5d9935SRobert Watson 		return (ENXIO);
1088de5d9935SRobert Watson 
10890832fc64SGarance A Drosehn 	revents = events & (POLLOUT | POLLWRNORM);
1090e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
109175c13541SPoul-Henning Kamp 	if (events & (POLLIN | POLLRDNORM)) {
109295aab9ccSJohn-Mark Gurney 		if (bpf_ready(d))
1093243ac7d8SPeter Wemm 			revents |= events & (POLLIN | POLLRDNORM);
109481bda851SJohn Polstra 		else {
1095ed01445dSJohn Baldwin 			selrecord(td, &d->bd_sel);
109681bda851SJohn Polstra 			/* Start the read timeout if necessary. */
109781bda851SJohn Polstra 			if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
109881bda851SJohn Polstra 				callout_reset(&d->bd_callout, d->bd_rtout,
109981bda851SJohn Polstra 				    bpf_timed_out, d);
110081bda851SJohn Polstra 				d->bd_state = BPF_WAITING;
110181bda851SJohn Polstra 			}
110281bda851SJohn Polstra 		}
110375c13541SPoul-Henning Kamp 	}
1104e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
1105243ac7d8SPeter Wemm 	return (revents);
1106df8bae1dSRodney W. Grimes }
1107df8bae1dSRodney W. Grimes 
1108df8bae1dSRodney W. Grimes /*
110995aab9ccSJohn-Mark Gurney  * Support for kevent() system call.  Register EVFILT_READ filters and
111095aab9ccSJohn-Mark Gurney  * reject all others.
111195aab9ccSJohn-Mark Gurney  */
111295aab9ccSJohn-Mark Gurney int
111395aab9ccSJohn-Mark Gurney bpfkqfilter(dev, kn)
111489c9c53dSPoul-Henning Kamp 	struct cdev *dev;
111595aab9ccSJohn-Mark Gurney 	struct knote *kn;
111695aab9ccSJohn-Mark Gurney {
111795aab9ccSJohn-Mark Gurney 	struct bpf_d *d = (struct bpf_d *)dev->si_drv1;
111895aab9ccSJohn-Mark Gurney 
111995aab9ccSJohn-Mark Gurney 	if (kn->kn_filter != EVFILT_READ)
112095aab9ccSJohn-Mark Gurney 		return (1);
112195aab9ccSJohn-Mark Gurney 
112295aab9ccSJohn-Mark Gurney 	kn->kn_fop = &bpfread_filtops;
112395aab9ccSJohn-Mark Gurney 	kn->kn_hook = d;
1124ad3b9257SJohn-Mark Gurney 	knlist_add(&d->bd_sel.si_note, kn, 0);
112595aab9ccSJohn-Mark Gurney 
112695aab9ccSJohn-Mark Gurney 	return (0);
112795aab9ccSJohn-Mark Gurney }
112895aab9ccSJohn-Mark Gurney 
112995aab9ccSJohn-Mark Gurney static void
113095aab9ccSJohn-Mark Gurney filt_bpfdetach(kn)
113195aab9ccSJohn-Mark Gurney 	struct knote *kn;
113295aab9ccSJohn-Mark Gurney {
113395aab9ccSJohn-Mark Gurney 	struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
113495aab9ccSJohn-Mark Gurney 
1135ad3b9257SJohn-Mark Gurney 	knlist_remove(&d->bd_sel.si_note, kn, 0);
113695aab9ccSJohn-Mark Gurney }
113795aab9ccSJohn-Mark Gurney 
113895aab9ccSJohn-Mark Gurney static int
113995aab9ccSJohn-Mark Gurney filt_bpfread(kn, hint)
114095aab9ccSJohn-Mark Gurney 	struct knote *kn;
114195aab9ccSJohn-Mark Gurney 	long hint;
114295aab9ccSJohn-Mark Gurney {
114395aab9ccSJohn-Mark Gurney 	struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
114495aab9ccSJohn-Mark Gurney 	int ready;
114595aab9ccSJohn-Mark Gurney 
114686c9a453SJohn-Mark Gurney 	BPFD_LOCK_ASSERT(d);
114795aab9ccSJohn-Mark Gurney 	ready = bpf_ready(d);
114895aab9ccSJohn-Mark Gurney 	if (ready) {
114995aab9ccSJohn-Mark Gurney 		kn->kn_data = d->bd_slen;
115095aab9ccSJohn-Mark Gurney 		if (d->bd_hbuf)
115195aab9ccSJohn-Mark Gurney 			kn->kn_data += d->bd_hlen;
115295aab9ccSJohn-Mark Gurney 	}
115395aab9ccSJohn-Mark Gurney 	else if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
115495aab9ccSJohn-Mark Gurney 		callout_reset(&d->bd_callout, d->bd_rtout,
115595aab9ccSJohn-Mark Gurney 		    bpf_timed_out, d);
115695aab9ccSJohn-Mark Gurney 		d->bd_state = BPF_WAITING;
115795aab9ccSJohn-Mark Gurney 	}
115895aab9ccSJohn-Mark Gurney 
115995aab9ccSJohn-Mark Gurney 	return (ready);
116095aab9ccSJohn-Mark Gurney }
116195aab9ccSJohn-Mark Gurney 
116295aab9ccSJohn-Mark Gurney /*
1163df8bae1dSRodney W. Grimes  * Incoming linkage from device drivers.  Process the packet pkt, of length
1164df8bae1dSRodney W. Grimes  * pktlen, which is stored in a contiguous buffer.  The packet is parsed
1165df8bae1dSRodney W. Grimes  * by each process' filter, and if accepted, stashed into the corresponding
1166df8bae1dSRodney W. Grimes  * buffer.
1167df8bae1dSRodney W. Grimes  */
1168df8bae1dSRodney W. Grimes void
116924a229f4SSam Leffler bpf_tap(bp, pkt, pktlen)
117024a229f4SSam Leffler 	struct bpf_if *bp;
11718994a245SDag-Erling Smørgrav 	u_char *pkt;
11728994a245SDag-Erling Smørgrav 	u_int pktlen;
1173df8bae1dSRodney W. Grimes {
11748994a245SDag-Erling Smørgrav 	struct bpf_d *d;
11758994a245SDag-Erling Smørgrav 	u_int slen;
1176e7bb21b3SJonathan Lemon 
117746691dd8SRobert Watson 	/*
117846691dd8SRobert Watson 	 * Lockless read to avoid cost of locking the interface if there are
117946691dd8SRobert Watson 	 * no descriptors attached.
118046691dd8SRobert Watson 	 */
11814a3feeaaSRobert Watson 	if (LIST_EMPTY(&bp->bif_dlist))
118246691dd8SRobert Watson 		return;
118346691dd8SRobert Watson 
1184e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
11854a3feeaaSRobert Watson 	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1186e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1187df8bae1dSRodney W. Grimes 		++d->bd_rcount;
1188df8bae1dSRodney W. Grimes 		slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen);
1189ec272d87SRobert Watson 		if (slen != 0) {
119069f7644bSChristian S.J. Peron 			d->bd_fcount++;
1191ec272d87SRobert Watson #ifdef MAC
119224a229f4SSam Leffler 			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1193ec272d87SRobert Watson #endif
1194df8bae1dSRodney W. Grimes 				catchpacket(d, pkt, pktlen, slen, bcopy);
1195ec272d87SRobert Watson 		}
1196e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1197df8bae1dSRodney W. Grimes 	}
1198e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
1199df8bae1dSRodney W. Grimes }
1200df8bae1dSRodney W. Grimes 
1201df8bae1dSRodney W. Grimes /*
1202df8bae1dSRodney W. Grimes  * Copy data from an mbuf chain into a buffer.  This code is derived
1203df8bae1dSRodney W. Grimes  * from m_copydata in sys/uipc_mbuf.c.
1204df8bae1dSRodney W. Grimes  */
1205df8bae1dSRodney W. Grimes static void
1206df8bae1dSRodney W. Grimes bpf_mcopy(src_arg, dst_arg, len)
1207df8bae1dSRodney W. Grimes 	const void *src_arg;
1208df8bae1dSRodney W. Grimes 	void *dst_arg;
12098994a245SDag-Erling Smørgrav 	size_t len;
1210df8bae1dSRodney W. Grimes {
12118994a245SDag-Erling Smørgrav 	const struct mbuf *m;
12128994a245SDag-Erling Smørgrav 	u_int count;
1213df8bae1dSRodney W. Grimes 	u_char *dst;
1214df8bae1dSRodney W. Grimes 
1215df8bae1dSRodney W. Grimes 	m = src_arg;
1216df8bae1dSRodney W. Grimes 	dst = dst_arg;
1217df8bae1dSRodney W. Grimes 	while (len > 0) {
1218572bde2aSRobert Watson 		if (m == NULL)
1219df8bae1dSRodney W. Grimes 			panic("bpf_mcopy");
1220df8bae1dSRodney W. Grimes 		count = min(m->m_len, len);
12210453d3cbSBruce Evans 		bcopy(mtod(m, void *), dst, count);
1222df8bae1dSRodney W. Grimes 		m = m->m_next;
1223df8bae1dSRodney W. Grimes 		dst += count;
1224df8bae1dSRodney W. Grimes 		len -= count;
1225df8bae1dSRodney W. Grimes 	}
1226df8bae1dSRodney W. Grimes }
1227df8bae1dSRodney W. Grimes 
1228df8bae1dSRodney W. Grimes /*
1229df8bae1dSRodney W. Grimes  * Incoming linkage from device drivers, when packet is in an mbuf chain.
1230df8bae1dSRodney W. Grimes  */
1231df8bae1dSRodney W. Grimes void
123224a229f4SSam Leffler bpf_mtap(bp, m)
123324a229f4SSam Leffler 	struct bpf_if *bp;
1234df8bae1dSRodney W. Grimes 	struct mbuf *m;
1235df8bae1dSRodney W. Grimes {
1236df8bae1dSRodney W. Grimes 	struct bpf_d *d;
1237df8bae1dSRodney W. Grimes 	u_int pktlen, slen;
1238df8bae1dSRodney W. Grimes 
123946691dd8SRobert Watson 	/*
124046691dd8SRobert Watson 	 * Lockless read to avoid cost of locking the interface if there are
124146691dd8SRobert Watson 	 * no descriptors attached.
124246691dd8SRobert Watson 	 */
12434a3feeaaSRobert Watson 	if (LIST_EMPTY(&bp->bif_dlist))
124446691dd8SRobert Watson 		return;
124546691dd8SRobert Watson 
1246f0e2422bSPoul-Henning Kamp 	pktlen = m_length(m, NULL);
12477b831242SPoul-Henning Kamp 	if (pktlen == m->m_len) {
124824a229f4SSam Leffler 		bpf_tap(bp, mtod(m, u_char *), pktlen);
12497b831242SPoul-Henning Kamp 		return;
12507b831242SPoul-Henning Kamp 	}
1251df8bae1dSRodney W. Grimes 
1252e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
12534a3feeaaSRobert Watson 	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
12548ed3828cSRobert Watson 		if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL))
12558ed3828cSRobert Watson 			continue;
1256e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1257df8bae1dSRodney W. Grimes 		++d->bd_rcount;
1258df8bae1dSRodney W. Grimes 		slen = bpf_filter(d->bd_filter, (u_char *)m, pktlen, 0);
1259df8bae1dSRodney W. Grimes 		if (slen != 0)
126069f7644bSChristian S.J. Peron 			d->bd_fcount++;
12610c7fb534SRobert Watson #ifdef MAC
126224a229f4SSam Leffler 			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
12630c7fb534SRobert Watson #endif
12640c7fb534SRobert Watson 				catchpacket(d, (u_char *)m, pktlen, slen,
12650c7fb534SRobert Watson 				    bpf_mcopy);
1266e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1267df8bae1dSRodney W. Grimes 	}
1268e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
1269df8bae1dSRodney W. Grimes }
1270df8bae1dSRodney W. Grimes 
1271df8bae1dSRodney W. Grimes /*
1272437ffe18SSam Leffler  * Incoming linkage from device drivers, when packet is in
1273437ffe18SSam Leffler  * an mbuf chain and to be prepended by a contiguous header.
1274437ffe18SSam Leffler  */
1275437ffe18SSam Leffler void
1276437ffe18SSam Leffler bpf_mtap2(bp, data, dlen, m)
1277437ffe18SSam Leffler 	struct bpf_if *bp;
1278437ffe18SSam Leffler 	void *data;
1279437ffe18SSam Leffler 	u_int dlen;
1280437ffe18SSam Leffler 	struct mbuf *m;
1281437ffe18SSam Leffler {
1282437ffe18SSam Leffler 	struct mbuf mb;
1283437ffe18SSam Leffler 	struct bpf_d *d;
1284437ffe18SSam Leffler 	u_int pktlen, slen;
1285437ffe18SSam Leffler 
128646691dd8SRobert Watson 	/*
128746691dd8SRobert Watson 	 * Lockless read to avoid cost of locking the interface if there are
128846691dd8SRobert Watson 	 * no descriptors attached.
128946691dd8SRobert Watson 	 */
12904a3feeaaSRobert Watson 	if (LIST_EMPTY(&bp->bif_dlist))
129146691dd8SRobert Watson 		return;
129246691dd8SRobert Watson 
1293437ffe18SSam Leffler 	pktlen = m_length(m, NULL);
1294437ffe18SSam Leffler 	/*
1295437ffe18SSam Leffler 	 * Craft on-stack mbuf suitable for passing to bpf_filter.
1296437ffe18SSam Leffler 	 * Note that we cut corners here; we only setup what's
1297437ffe18SSam Leffler 	 * absolutely needed--this mbuf should never go anywhere else.
1298437ffe18SSam Leffler 	 */
1299437ffe18SSam Leffler 	mb.m_next = m;
1300437ffe18SSam Leffler 	mb.m_data = data;
1301437ffe18SSam Leffler 	mb.m_len = dlen;
1302437ffe18SSam Leffler 	pktlen += dlen;
1303437ffe18SSam Leffler 
1304437ffe18SSam Leffler 	BPFIF_LOCK(bp);
13054a3feeaaSRobert Watson 	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1306437ffe18SSam Leffler 		if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL))
1307437ffe18SSam Leffler 			continue;
1308437ffe18SSam Leffler 		BPFD_LOCK(d);
1309437ffe18SSam Leffler 		++d->bd_rcount;
1310437ffe18SSam Leffler 		slen = bpf_filter(d->bd_filter, (u_char *)&mb, pktlen, 0);
1311437ffe18SSam Leffler 		if (slen != 0)
131269f7644bSChristian S.J. Peron 			d->bd_fcount++;
1313437ffe18SSam Leffler #ifdef MAC
1314437ffe18SSam Leffler 			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1315437ffe18SSam Leffler #endif
1316437ffe18SSam Leffler 				catchpacket(d, (u_char *)&mb, pktlen, slen,
1317437ffe18SSam Leffler 				    bpf_mcopy);
1318437ffe18SSam Leffler 		BPFD_UNLOCK(d);
1319437ffe18SSam Leffler 	}
1320437ffe18SSam Leffler 	BPFIF_UNLOCK(bp);
1321437ffe18SSam Leffler }
1322437ffe18SSam Leffler 
1323437ffe18SSam Leffler /*
1324df8bae1dSRodney W. Grimes  * Move the packet data from interface memory (pkt) into the
13259e610888SDag-Erling Smørgrav  * store buffer.  "cpfn" is the routine called to do the actual data
1326df8bae1dSRodney W. Grimes  * transfer.  bcopy is passed in to copy contiguous chunks, while
1327df8bae1dSRodney W. Grimes  * bpf_mcopy is passed in to copy mbuf chains.  In the latter case,
1328df8bae1dSRodney W. Grimes  * pkt is really an mbuf.
1329df8bae1dSRodney W. Grimes  */
1330df8bae1dSRodney W. Grimes static void
1331df8bae1dSRodney W. Grimes catchpacket(d, pkt, pktlen, snaplen, cpfn)
13328994a245SDag-Erling Smørgrav 	struct bpf_d *d;
13338994a245SDag-Erling Smørgrav 	u_char *pkt;
13348994a245SDag-Erling Smørgrav 	u_int pktlen, snaplen;
13358994a245SDag-Erling Smørgrav 	void (*cpfn)(const void *, void *, size_t);
1336df8bae1dSRodney W. Grimes {
13378994a245SDag-Erling Smørgrav 	struct bpf_hdr *hp;
13388994a245SDag-Erling Smørgrav 	int totlen, curlen;
13398994a245SDag-Erling Smørgrav 	int hdrlen = d->bd_bif->bif_hdrlen;
13407819da79SJohn-Mark Gurney 	int do_wakeup = 0;
13419e610888SDag-Erling Smørgrav 
1342a3272e3cSChristian S.J. Peron 	BPFD_LOCK_ASSERT(d);
1343df8bae1dSRodney W. Grimes 	/*
1344df8bae1dSRodney W. Grimes 	 * Figure out how many bytes to move.  If the packet is
1345df8bae1dSRodney W. Grimes 	 * greater or equal to the snapshot length, transfer that
1346df8bae1dSRodney W. Grimes 	 * much.  Otherwise, transfer the whole packet (unless
1347df8bae1dSRodney W. Grimes 	 * we hit the buffer size limit).
1348df8bae1dSRodney W. Grimes 	 */
1349df8bae1dSRodney W. Grimes 	totlen = hdrlen + min(snaplen, pktlen);
1350df8bae1dSRodney W. Grimes 	if (totlen > d->bd_bufsize)
1351df8bae1dSRodney W. Grimes 		totlen = d->bd_bufsize;
1352df8bae1dSRodney W. Grimes 
1353df8bae1dSRodney W. Grimes 	/*
1354df8bae1dSRodney W. Grimes 	 * Round up the end of the previous packet to the next longword.
1355df8bae1dSRodney W. Grimes 	 */
1356df8bae1dSRodney W. Grimes 	curlen = BPF_WORDALIGN(d->bd_slen);
1357df8bae1dSRodney W. Grimes 	if (curlen + totlen > d->bd_bufsize) {
1358df8bae1dSRodney W. Grimes 		/*
1359df8bae1dSRodney W. Grimes 		 * This packet will overflow the storage buffer.
1360df8bae1dSRodney W. Grimes 		 * Rotate the buffers if we can, then wakeup any
1361df8bae1dSRodney W. Grimes 		 * pending reads.
1362df8bae1dSRodney W. Grimes 		 */
1363572bde2aSRobert Watson 		if (d->bd_fbuf == NULL) {
1364df8bae1dSRodney W. Grimes 			/*
1365df8bae1dSRodney W. Grimes 			 * We haven't completed the previous read yet,
1366df8bae1dSRodney W. Grimes 			 * so drop the packet.
1367df8bae1dSRodney W. Grimes 			 */
1368df8bae1dSRodney W. Grimes 			++d->bd_dcount;
1369df8bae1dSRodney W. Grimes 			return;
1370df8bae1dSRodney W. Grimes 		}
1371df8bae1dSRodney W. Grimes 		ROTATE_BUFFERS(d);
13727819da79SJohn-Mark Gurney 		do_wakeup = 1;
1373df8bae1dSRodney W. Grimes 		curlen = 0;
1374df8bae1dSRodney W. Grimes 	}
137581bda851SJohn Polstra 	else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT)
1376df8bae1dSRodney W. Grimes 		/*
137781bda851SJohn Polstra 		 * Immediate mode is set, or the read timeout has
137881bda851SJohn Polstra 		 * already expired during a select call.  A packet
137981bda851SJohn Polstra 		 * arrived, so the reader should be woken up.
1380df8bae1dSRodney W. Grimes 		 */
13817819da79SJohn-Mark Gurney 		do_wakeup = 1;
1382df8bae1dSRodney W. Grimes 
1383df8bae1dSRodney W. Grimes 	/*
1384df8bae1dSRodney W. Grimes 	 * Append the bpf header.
1385df8bae1dSRodney W. Grimes 	 */
1386df8bae1dSRodney W. Grimes 	hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
1387df8bae1dSRodney W. Grimes 	microtime(&hp->bh_tstamp);
1388df8bae1dSRodney W. Grimes 	hp->bh_datalen = pktlen;
1389df8bae1dSRodney W. Grimes 	hp->bh_hdrlen = hdrlen;
1390df8bae1dSRodney W. Grimes 	/*
1391df8bae1dSRodney W. Grimes 	 * Copy the packet data into the store buffer and update its length.
1392df8bae1dSRodney W. Grimes 	 */
1393df8bae1dSRodney W. Grimes 	(*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen));
1394df8bae1dSRodney W. Grimes 	d->bd_slen = curlen + totlen;
13957819da79SJohn-Mark Gurney 
13967819da79SJohn-Mark Gurney 	if (do_wakeup)
13977819da79SJohn-Mark Gurney 		bpf_wakeup(d);
1398df8bae1dSRodney W. Grimes }
1399df8bae1dSRodney W. Grimes 
1400df8bae1dSRodney W. Grimes /*
1401df8bae1dSRodney W. Grimes  * Initialize all nonzero fields of a descriptor.
1402df8bae1dSRodney W. Grimes  */
1403df8bae1dSRodney W. Grimes static int
1404df8bae1dSRodney W. Grimes bpf_allocbufs(d)
14058994a245SDag-Erling Smørgrav 	struct bpf_d *d;
1406df8bae1dSRodney W. Grimes {
1407a163d034SWarner Losh 	d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1408572bde2aSRobert Watson 	if (d->bd_fbuf == NULL)
1409df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1410df8bae1dSRodney W. Grimes 
1411a163d034SWarner Losh 	d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1412572bde2aSRobert Watson 	if (d->bd_sbuf == NULL) {
1413bd3a5320SPoul-Henning Kamp 		free(d->bd_fbuf, M_BPF);
1414df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1415df8bae1dSRodney W. Grimes 	}
1416df8bae1dSRodney W. Grimes 	d->bd_slen = 0;
1417df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
1418df8bae1dSRodney W. Grimes 	return (0);
1419df8bae1dSRodney W. Grimes }
1420df8bae1dSRodney W. Grimes 
1421df8bae1dSRodney W. Grimes /*
1422df8bae1dSRodney W. Grimes  * Free buffers currently in use by a descriptor.
1423df8bae1dSRodney W. Grimes  * Called on close.
1424df8bae1dSRodney W. Grimes  */
1425df8bae1dSRodney W. Grimes static void
1426df8bae1dSRodney W. Grimes bpf_freed(d)
14278994a245SDag-Erling Smørgrav 	struct bpf_d *d;
1428df8bae1dSRodney W. Grimes {
1429df8bae1dSRodney W. Grimes 	/*
1430df8bae1dSRodney W. Grimes 	 * We don't need to lock out interrupts since this descriptor has
1431df8bae1dSRodney W. Grimes 	 * been detached from its interface and it yet hasn't been marked
1432df8bae1dSRodney W. Grimes 	 * free.
1433df8bae1dSRodney W. Grimes 	 */
1434572bde2aSRobert Watson 	if (d->bd_sbuf != NULL) {
1435bd3a5320SPoul-Henning Kamp 		free(d->bd_sbuf, M_BPF);
1436572bde2aSRobert Watson 		if (d->bd_hbuf != NULL)
1437bd3a5320SPoul-Henning Kamp 			free(d->bd_hbuf, M_BPF);
1438572bde2aSRobert Watson 		if (d->bd_fbuf != NULL)
1439bd3a5320SPoul-Henning Kamp 			free(d->bd_fbuf, M_BPF);
1440df8bae1dSRodney W. Grimes 	}
1441df8bae1dSRodney W. Grimes 	if (d->bd_filter)
1442bd3a5320SPoul-Henning Kamp 		free((caddr_t)d->bd_filter, M_BPF);
1443e7bb21b3SJonathan Lemon 	mtx_destroy(&d->bd_mtx);
1444df8bae1dSRodney W. Grimes }
1445df8bae1dSRodney W. Grimes 
1446df8bae1dSRodney W. Grimes /*
144724a229f4SSam Leffler  * Attach an interface to bpf.  dlt is the link layer type; hdrlen is the
144824a229f4SSam Leffler  * fixed size of the link header (variable length headers not yet supported).
1449df8bae1dSRodney W. Grimes  */
1450df8bae1dSRodney W. Grimes void
14519b44ff22SGarrett Wollman bpfattach(ifp, dlt, hdrlen)
1452df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
1453df8bae1dSRodney W. Grimes 	u_int dlt, hdrlen;
1454df8bae1dSRodney W. Grimes {
145524a229f4SSam Leffler 
145624a229f4SSam Leffler 	bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
145724a229f4SSam Leffler }
145824a229f4SSam Leffler 
145924a229f4SSam Leffler /*
146024a229f4SSam Leffler  * Attach an interface to bpf.  ifp is a pointer to the structure
146124a229f4SSam Leffler  * defining the interface to be attached, dlt is the link layer type,
146224a229f4SSam Leffler  * and hdrlen is the fixed size of the link header (variable length
146324a229f4SSam Leffler  * headers are not yet supporrted).
146424a229f4SSam Leffler  */
146524a229f4SSam Leffler void
146624a229f4SSam Leffler bpfattach2(ifp, dlt, hdrlen, driverp)
146724a229f4SSam Leffler 	struct ifnet *ifp;
146824a229f4SSam Leffler 	u_int dlt, hdrlen;
146924a229f4SSam Leffler 	struct bpf_if **driverp;
147024a229f4SSam Leffler {
1471df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
14726a40ecceSJohn Baldwin 	bp = (struct bpf_if *)malloc(sizeof(*bp), M_BPF, M_NOWAIT | M_ZERO);
1473572bde2aSRobert Watson 	if (bp == NULL)
1474df8bae1dSRodney W. Grimes 		panic("bpfattach");
1475df8bae1dSRodney W. Grimes 
14764a3feeaaSRobert Watson 	LIST_INIT(&bp->bif_dlist);
147724a229f4SSam Leffler 	bp->bif_driverp = driverp;
1478df8bae1dSRodney W. Grimes 	bp->bif_ifp = ifp;
1479df8bae1dSRodney W. Grimes 	bp->bif_dlt = dlt;
14806008862bSJohn Baldwin 	mtx_init(&bp->bif_mtx, "bpf interface lock", NULL, MTX_DEF);
1481df8bae1dSRodney W. Grimes 
1482e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
14834a3feeaaSRobert Watson 	LIST_INSERT_HEAD(&bpf_iflist, bp, bif_next);
1484e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
1485df8bae1dSRodney W. Grimes 
1486572bde2aSRobert Watson 	*bp->bif_driverp = NULL;
1487df8bae1dSRodney W. Grimes 
1488df8bae1dSRodney W. Grimes 	/*
1489df8bae1dSRodney W. Grimes 	 * Compute the length of the bpf header.  This is not necessarily
1490df8bae1dSRodney W. Grimes 	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1491df8bae1dSRodney W. Grimes 	 * that the network layer header begins on a longword boundary (for
1492df8bae1dSRodney W. Grimes 	 * performance reasons and to alleviate alignment restrictions).
1493df8bae1dSRodney W. Grimes 	 */
1494df8bae1dSRodney W. Grimes 	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1495df8bae1dSRodney W. Grimes 
14962eeab939SGarrett Wollman 	if (bootverbose)
149724a229f4SSam Leffler 		if_printf(ifp, "bpf attached\n");
1498df8bae1dSRodney W. Grimes }
149953ac6efbSJulian Elischer 
1500de5d9935SRobert Watson /*
1501de5d9935SRobert Watson  * Detach bpf from an interface.  This involves detaching each descriptor
1502de5d9935SRobert Watson  * associated with the interface, and leaving bd_bif NULL.  Notify each
1503de5d9935SRobert Watson  * descriptor as it's detached so that any sleepers wake up and get
1504de5d9935SRobert Watson  * ENXIO.
1505de5d9935SRobert Watson  */
1506de5d9935SRobert Watson void
1507de5d9935SRobert Watson bpfdetach(ifp)
1508de5d9935SRobert Watson 	struct ifnet *ifp;
1509de5d9935SRobert Watson {
15104a3feeaaSRobert Watson 	struct bpf_if	*bp;
1511de5d9935SRobert Watson 	struct bpf_d	*d;
1512de5d9935SRobert Watson 
1513de5d9935SRobert Watson 	/* Locate BPF interface information */
15148eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
15154a3feeaaSRobert Watson 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1516de5d9935SRobert Watson 		if (ifp == bp->bif_ifp)
1517de5d9935SRobert Watson 			break;
1518de5d9935SRobert Watson 	}
1519de5d9935SRobert Watson 
1520de5d9935SRobert Watson 	/* Interface wasn't attached */
1521d79bf337SMatthew N. Dodd 	if ((bp == NULL) || (bp->bif_ifp == NULL)) {
1522e7bb21b3SJonathan Lemon 		mtx_unlock(&bpf_mtx);
15239bf40edeSBrooks Davis 		printf("bpfdetach: %s was not attached\n", ifp->if_xname);
1524de5d9935SRobert Watson 		return;
1525de5d9935SRobert Watson 	}
1526de5d9935SRobert Watson 
15274a3feeaaSRobert Watson 	LIST_REMOVE(bp, bif_next);
15288eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
1529de5d9935SRobert Watson 
15304a3feeaaSRobert Watson 	while ((d = LIST_FIRST(&bp->bif_dlist)) != NULL) {
1531e7bb21b3SJonathan Lemon 		bpf_detachd(d);
1532e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1533e7bb21b3SJonathan Lemon 		bpf_wakeup(d);
1534e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1535e7bb21b3SJonathan Lemon 	}
1536e7bb21b3SJonathan Lemon 
1537e7bb21b3SJonathan Lemon 	mtx_destroy(&bp->bif_mtx);
1538de5d9935SRobert Watson 	free(bp, M_BPF);
15398eab61f3SSam Leffler }
1540de5d9935SRobert Watson 
15418eab61f3SSam Leffler /*
15428eab61f3SSam Leffler  * Get a list of available data link type of the interface.
15438eab61f3SSam Leffler  */
15448eab61f3SSam Leffler static int
15458eab61f3SSam Leffler bpf_getdltlist(d, bfl)
15468eab61f3SSam Leffler 	struct bpf_d *d;
15478eab61f3SSam Leffler 	struct bpf_dltlist *bfl;
15488eab61f3SSam Leffler {
15498eab61f3SSam Leffler 	int n, error;
15508eab61f3SSam Leffler 	struct ifnet *ifp;
15518eab61f3SSam Leffler 	struct bpf_if *bp;
15528eab61f3SSam Leffler 
15538eab61f3SSam Leffler 	ifp = d->bd_bif->bif_ifp;
15548eab61f3SSam Leffler 	n = 0;
15558eab61f3SSam Leffler 	error = 0;
15568eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
15574a3feeaaSRobert Watson 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
15588eab61f3SSam Leffler 		if (bp->bif_ifp != ifp)
15598eab61f3SSam Leffler 			continue;
15608eab61f3SSam Leffler 		if (bfl->bfl_list != NULL) {
15618eab61f3SSam Leffler 			if (n >= bfl->bfl_len) {
1562e7bb21b3SJonathan Lemon 				mtx_unlock(&bpf_mtx);
15638eab61f3SSam Leffler 				return (ENOMEM);
15648eab61f3SSam Leffler 			}
15658eab61f3SSam Leffler 			error = copyout(&bp->bif_dlt,
15668eab61f3SSam Leffler 			    bfl->bfl_list + n, sizeof(u_int));
15678eab61f3SSam Leffler 		}
15688eab61f3SSam Leffler 		n++;
15698eab61f3SSam Leffler 	}
15708eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
15718eab61f3SSam Leffler 	bfl->bfl_len = n;
15728eab61f3SSam Leffler 	return (error);
15738eab61f3SSam Leffler }
15748eab61f3SSam Leffler 
15758eab61f3SSam Leffler /*
15768eab61f3SSam Leffler  * Set the data link type of a BPF instance.
15778eab61f3SSam Leffler  */
15788eab61f3SSam Leffler static int
15798eab61f3SSam Leffler bpf_setdlt(d, dlt)
15808eab61f3SSam Leffler 	struct bpf_d *d;
15818eab61f3SSam Leffler 	u_int dlt;
15828eab61f3SSam Leffler {
15838eab61f3SSam Leffler 	int error, opromisc;
15848eab61f3SSam Leffler 	struct ifnet *ifp;
15858eab61f3SSam Leffler 	struct bpf_if *bp;
15868eab61f3SSam Leffler 
15878eab61f3SSam Leffler 	if (d->bd_bif->bif_dlt == dlt)
15888eab61f3SSam Leffler 		return (0);
15898eab61f3SSam Leffler 	ifp = d->bd_bif->bif_ifp;
15908eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
15914a3feeaaSRobert Watson 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
15928eab61f3SSam Leffler 		if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
15938eab61f3SSam Leffler 			break;
15948eab61f3SSam Leffler 	}
15958eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
15968eab61f3SSam Leffler 	if (bp != NULL) {
15978eab61f3SSam Leffler 		opromisc = d->bd_promisc;
15988eab61f3SSam Leffler 		bpf_detachd(d);
15998eab61f3SSam Leffler 		bpf_attachd(d, bp);
160093daabddSBrian Feldman 		BPFD_LOCK(d);
16018eab61f3SSam Leffler 		reset_d(d);
16028eab61f3SSam Leffler 		BPFD_UNLOCK(d);
16038eab61f3SSam Leffler 		if (opromisc) {
16048eab61f3SSam Leffler 			error = ifpromisc(bp->bif_ifp, 1);
16058eab61f3SSam Leffler 			if (error)
16068eab61f3SSam Leffler 				if_printf(bp->bif_ifp,
16078eab61f3SSam Leffler 					"bpf_setdlt: ifpromisc failed (%d)\n",
16088eab61f3SSam Leffler 					error);
16098eab61f3SSam Leffler 			else
16108eab61f3SSam Leffler 				d->bd_promisc = 1;
16118eab61f3SSam Leffler 		}
16128eab61f3SSam Leffler 	}
16138eab61f3SSam Leffler 	return (bp == NULL ? EINVAL : 0);
1614de5d9935SRobert Watson }
1615de5d9935SRobert Watson 
16163f54a085SPoul-Henning Kamp static void
16173f54a085SPoul-Henning Kamp bpf_clone(arg, name, namelen, dev)
16183f54a085SPoul-Henning Kamp 	void *arg;
16193f54a085SPoul-Henning Kamp 	char *name;
16203f54a085SPoul-Henning Kamp 	int namelen;
162189c9c53dSPoul-Henning Kamp 	struct cdev **dev;
16223f54a085SPoul-Henning Kamp {
16233f54a085SPoul-Henning Kamp 	int u;
16243f54a085SPoul-Henning Kamp 
1625f3732fd1SPoul-Henning Kamp 	if (*dev != NULL)
16263f54a085SPoul-Henning Kamp 		return;
1627db901281SPoul-Henning Kamp 	if (dev_stdclone(name, NULL, "bpf", &u) != 1)
16283f54a085SPoul-Henning Kamp 		return;
1629b0d17ba6SPoul-Henning Kamp 	*dev = make_dev(&bpf_cdevsw, unit2minor(u), UID_ROOT, GID_WHEEL, 0600,
1630b0d17ba6SPoul-Henning Kamp 	    "bpf%d", u);
1631f4f6abcbSPoul-Henning Kamp 	dev_ref(*dev);
1632b0d17ba6SPoul-Henning Kamp 	(*dev)->si_flags |= SI_CHEAPCLONE;
16333f54a085SPoul-Henning Kamp 	return;
16343f54a085SPoul-Henning Kamp }
16353f54a085SPoul-Henning Kamp 
1636514ede09SBruce Evans static void
1637514ede09SBruce Evans bpf_drvinit(unused)
1638514ede09SBruce Evans 	void *unused;
163953ac6efbSJulian Elischer {
164053ac6efbSJulian Elischer 
16416008862bSJohn Baldwin 	mtx_init(&bpf_mtx, "bpf global lock", NULL, MTX_DEF);
16424a3feeaaSRobert Watson 	LIST_INIT(&bpf_iflist);
1643db901281SPoul-Henning Kamp 	EVENTHANDLER_REGISTER(dev_clone, bpf_clone, 0, 1000);
16447198bf47SJulian Elischer }
164553ac6efbSJulian Elischer 
164669f7644bSChristian S.J. Peron static void
164769f7644bSChristian S.J. Peron bpfstats_fill_xbpf(struct xbpf_d *d, struct bpf_d *bd)
164869f7644bSChristian S.J. Peron {
164969f7644bSChristian S.J. Peron 
165069f7644bSChristian S.J. Peron 	bzero(d, sizeof(*d));
165169f7644bSChristian S.J. Peron 	BPFD_LOCK_ASSERT(bd);
165269f7644bSChristian S.J. Peron 	d->bd_immediate = bd->bd_immediate;
165369f7644bSChristian S.J. Peron 	d->bd_promisc = bd->bd_promisc;
165469f7644bSChristian S.J. Peron 	d->bd_hdrcmplt = bd->bd_hdrcmplt;
165569f7644bSChristian S.J. Peron 	d->bd_seesent = bd->bd_seesent;
165669f7644bSChristian S.J. Peron 	d->bd_async = bd->bd_async;
165769f7644bSChristian S.J. Peron 	d->bd_rcount = bd->bd_rcount;
165869f7644bSChristian S.J. Peron 	d->bd_dcount = bd->bd_dcount;
165969f7644bSChristian S.J. Peron 	d->bd_fcount = bd->bd_fcount;
166069f7644bSChristian S.J. Peron 	d->bd_sig = bd->bd_sig;
166169f7644bSChristian S.J. Peron 	d->bd_slen = bd->bd_slen;
166269f7644bSChristian S.J. Peron 	d->bd_hlen = bd->bd_hlen;
166369f7644bSChristian S.J. Peron 	d->bd_bufsize = bd->bd_bufsize;
166469f7644bSChristian S.J. Peron 	d->bd_pid = bd->bd_pid;
166569f7644bSChristian S.J. Peron 	strlcpy(d->bd_ifname,
166669f7644bSChristian S.J. Peron 	    bd->bd_bif->bif_ifp->if_xname, IFNAMSIZ);
166769f7644bSChristian S.J. Peron 	strlcpy(d->bd_pcomm, bd->bd_pcomm, MAXCOMLEN);
166869f7644bSChristian S.J. Peron }
166969f7644bSChristian S.J. Peron 
167069f7644bSChristian S.J. Peron static int
167169f7644bSChristian S.J. Peron bpf_stats_sysctl(SYSCTL_HANDLER_ARGS)
167269f7644bSChristian S.J. Peron {
167369f7644bSChristian S.J. Peron 	struct xbpf_d xbd;
167469f7644bSChristian S.J. Peron 	int error;
167569f7644bSChristian S.J. Peron 	struct bpf_if *bp;
167669f7644bSChristian S.J. Peron 	struct bpf_d *bd;
167769f7644bSChristian S.J. Peron 
167869f7644bSChristian S.J. Peron 	/*
167969f7644bSChristian S.J. Peron 	 * XXX This is not technically correct. It is possible for non
168069f7644bSChristian S.J. Peron 	 * privileged users to open bpf devices. It would make sense
168169f7644bSChristian S.J. Peron 	 * if the users who opened the devices were able to retrieve
168269f7644bSChristian S.J. Peron 	 * the statistics for them, too.
168369f7644bSChristian S.J. Peron 	 */
168469f7644bSChristian S.J. Peron 	error = suser(req->td);
168569f7644bSChristian S.J. Peron 	if (error)
168669f7644bSChristian S.J. Peron 		return (error);
168769f7644bSChristian S.J. Peron 	if (req->oldptr == NULL)
168869f7644bSChristian S.J. Peron 		return (SYSCTL_OUT(req, 0, bpf_bpfd_cnt * sizeof(xbd)));
168969f7644bSChristian S.J. Peron 	if (req->oldlen < bpf_bpfd_cnt * sizeof(xbd))
169069f7644bSChristian S.J. Peron 		return (ENOMEM);
169169f7644bSChristian S.J. Peron 	if (bpf_bpfd_cnt == 0)
169269f7644bSChristian S.J. Peron 		return (SYSCTL_OUT(req, 0, 0));
169369f7644bSChristian S.J. Peron 	mtx_lock(&bpf_mtx);
169469f7644bSChristian S.J. Peron 	KASSERT(bpf_bpfd_cnt != 0, ("zero bpf descriptors present"));
169569f7644bSChristian S.J. Peron 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
169669f7644bSChristian S.J. Peron 		LIST_FOREACH(bd, &bp->bif_dlist, bd_next) {
169769f7644bSChristian S.J. Peron 			BPFD_LOCK(bd);
169869f7644bSChristian S.J. Peron 			bpfstats_fill_xbpf(&xbd, bd);
169969f7644bSChristian S.J. Peron 			BPFD_UNLOCK(bd);
170069f7644bSChristian S.J. Peron 			error = SYSCTL_OUT(req, &xbd, sizeof(xbd));
170169f7644bSChristian S.J. Peron 			if (error != 0) {
170269f7644bSChristian S.J. Peron 				mtx_unlock(&bpf_mtx);
170369f7644bSChristian S.J. Peron 				return (error);
170469f7644bSChristian S.J. Peron 			}
170569f7644bSChristian S.J. Peron 		}
170669f7644bSChristian S.J. Peron 	}
170769f7644bSChristian S.J. Peron 	mtx_unlock(&bpf_mtx);
170869f7644bSChristian S.J. Peron 	return (error);
170969f7644bSChristian S.J. Peron }
171069f7644bSChristian S.J. Peron 
1711c9c7976fSPoul-Henning Kamp SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,bpf_drvinit,NULL)
171253ac6efbSJulian Elischer 
17135bb5f2c9SPeter Wemm #else /* !DEV_BPF && !NETGRAPH_BPF */
1714f8dc4716SMike Smith /*
1715f8dc4716SMike Smith  * NOP stubs to allow bpf-using drivers to load and function.
1716f8dc4716SMike Smith  *
1717f8dc4716SMike Smith  * A 'better' implementation would allow the core bpf functionality
1718f8dc4716SMike Smith  * to be loaded at runtime.
1719f8dc4716SMike Smith  */
1720f8dc4716SMike Smith 
1721f8dc4716SMike Smith void
1722e5562beeSSam Leffler bpf_tap(bp, pkt, pktlen)
1723e5562beeSSam Leffler 	struct bpf_if *bp;
17248994a245SDag-Erling Smørgrav 	u_char *pkt;
17258994a245SDag-Erling Smørgrav 	u_int pktlen;
1726f8dc4716SMike Smith {
1727f8dc4716SMike Smith }
1728f8dc4716SMike Smith 
1729f8dc4716SMike Smith void
1730e5562beeSSam Leffler bpf_mtap(bp, m)
1731e5562beeSSam Leffler 	struct bpf_if *bp;
1732f8dc4716SMike Smith 	struct mbuf *m;
1733f8dc4716SMike Smith {
1734f8dc4716SMike Smith }
1735f8dc4716SMike Smith 
1736f8dc4716SMike Smith void
1737437ffe18SSam Leffler bpf_mtap2(bp, d, l, m)
1738437ffe18SSam Leffler 	struct bpf_if *bp;
1739c1dae2f0STim J. Robbins 	void *d;
1740437ffe18SSam Leffler 	u_int l;
1741437ffe18SSam Leffler 	struct mbuf *m;
1742437ffe18SSam Leffler {
1743437ffe18SSam Leffler }
1744437ffe18SSam Leffler 
1745437ffe18SSam Leffler void
1746f8dc4716SMike Smith bpfattach(ifp, dlt, hdrlen)
1747f8dc4716SMike Smith 	struct ifnet *ifp;
1748f8dc4716SMike Smith 	u_int dlt, hdrlen;
1749f8dc4716SMike Smith {
1750f8dc4716SMike Smith }
1751f8dc4716SMike Smith 
1752da626c17SBill Paul void
17535f7a7923SSam Leffler bpfattach2(ifp, dlt, hdrlen, driverp)
17545f7a7923SSam Leffler 	struct ifnet *ifp;
17555f7a7923SSam Leffler 	u_int dlt, hdrlen;
17565f7a7923SSam Leffler 	struct bpf_if **driverp;
17575f7a7923SSam Leffler {
17585f7a7923SSam Leffler }
17595f7a7923SSam Leffler 
17605f7a7923SSam Leffler void
1761da626c17SBill Paul bpfdetach(ifp)
1762da626c17SBill Paul 	struct ifnet *ifp;
1763da626c17SBill Paul {
1764da626c17SBill Paul }
1765da626c17SBill Paul 
1766f8dc4716SMike Smith u_int
1767f8dc4716SMike Smith bpf_filter(pc, p, wirelen, buflen)
17688994a245SDag-Erling Smørgrav 	const struct bpf_insn *pc;
17698994a245SDag-Erling Smørgrav 	u_char *p;
1770f8dc4716SMike Smith 	u_int wirelen;
17718994a245SDag-Erling Smørgrav 	u_int buflen;
1772f8dc4716SMike Smith {
1773f8dc4716SMike Smith 	return -1;	/* "no filter" behaviour */
1774f8dc4716SMike Smith }
1775f8dc4716SMike Smith 
17765bb5f2c9SPeter Wemm int
17775bb5f2c9SPeter Wemm bpf_validate(f, len)
17785bb5f2c9SPeter Wemm 	const struct bpf_insn *f;
17795bb5f2c9SPeter Wemm 	int len;
17805bb5f2c9SPeter Wemm {
17815bb5f2c9SPeter Wemm 	return 0;		/* false */
17825bb5f2c9SPeter Wemm }
17835bb5f2c9SPeter Wemm 
17845bb5f2c9SPeter Wemm #endif /* !DEV_BPF && !NETGRAPH_BPF */
1785