xref: /freebsd/sys/net/bpf.c (revision 293c06a186615c5476c1605078d0e7d25a83c1d0)
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>
68ae275efcSJung-uk Kim #ifdef BPF_JITTER
69ae275efcSJung-uk Kim #include <net/bpf_jitter.h>
70ae275efcSJung-uk Kim #endif
71df8bae1dSRodney W. Grimes #include <net/bpfdesc.h>
72df8bae1dSRodney W. Grimes 
73df8bae1dSRodney W. Grimes #include <netinet/in.h>
74df8bae1dSRodney W. Grimes #include <netinet/if_ether.h>
75df8bae1dSRodney W. Grimes #include <sys/kernel.h>
76f708ef1bSPoul-Henning Kamp #include <sys/sysctl.h>
777b778b5eSEivind Eklund 
78959b7375SPoul-Henning Kamp static MALLOC_DEFINE(M_BPF, "BPF", "BPF data");
7987f6c662SJulian Elischer 
805bb5f2c9SPeter Wemm #if defined(DEV_BPF) || defined(NETGRAPH_BPF)
8153ac6efbSJulian Elischer 
82df8bae1dSRodney W. Grimes #define PRINET  26			/* interruptible */
83df8bae1dSRodney W. Grimes 
84df8bae1dSRodney W. Grimes /*
85d1a67300SRobert Watson  * bpf_iflist is a list of BPF interface structures, each corresponding to a
86d1a67300SRobert Watson  * specific DLT.  The same network interface might have several BPF interface
87d1a67300SRobert Watson  * structures registered by different layers in the stack (i.e., 802.11
88d1a67300SRobert Watson  * frames, ethernet frames, etc).
89df8bae1dSRodney W. Grimes  */
904a3feeaaSRobert Watson static LIST_HEAD(, bpf_if)	bpf_iflist;
91e7bb21b3SJonathan Lemon static struct mtx	bpf_mtx;		/* bpf global lock */
9269f7644bSChristian S.J. Peron static int		bpf_bpfd_cnt;
93df8bae1dSRodney W. Grimes 
94929ddbbbSAlfred Perlstein static int	bpf_allocbufs(struct bpf_d *);
95929ddbbbSAlfred Perlstein static void	bpf_attachd(struct bpf_d *d, struct bpf_if *bp);
96929ddbbbSAlfred Perlstein static void	bpf_detachd(struct bpf_d *d);
97929ddbbbSAlfred Perlstein static void	bpf_freed(struct bpf_d *);
98929ddbbbSAlfred Perlstein static void	bpf_mcopy(const void *, void *, size_t);
9901399f34SDavid Malone static int	bpf_movein(struct uio *, int, int,
10093e39f0bSChristian S.J. Peron 		    struct mbuf **, struct sockaddr *, struct bpf_insn *);
101929ddbbbSAlfred Perlstein static int	bpf_setif(struct bpf_d *, struct ifreq *);
102929ddbbbSAlfred Perlstein static void	bpf_timed_out(void *);
103e7bb21b3SJonathan Lemon static __inline void
104929ddbbbSAlfred Perlstein 		bpf_wakeup(struct bpf_d *);
105929ddbbbSAlfred Perlstein static void	catchpacket(struct bpf_d *, u_char *, u_int,
106929ddbbbSAlfred Perlstein 		    u_int, void (*)(const void *, void *, size_t));
107929ddbbbSAlfred Perlstein static void	reset_d(struct bpf_d *);
10893e39f0bSChristian S.J. Peron static int	 bpf_setf(struct bpf_d *, struct bpf_program *, u_long cmd);
1098eab61f3SSam Leffler static int	bpf_getdltlist(struct bpf_d *, struct bpf_dltlist *);
1108eab61f3SSam Leffler static int	bpf_setdlt(struct bpf_d *, u_int);
11195aab9ccSJohn-Mark Gurney static void	filt_bpfdetach(struct knote *);
11295aab9ccSJohn-Mark Gurney static int	filt_bpfread(struct knote *, long);
113a3272e3cSChristian S.J. Peron static void	bpf_drvinit(void *);
1146a113b3dSRobert Watson static void	bpf_clone(void *, struct ucred *, char *, int, struct cdev **);
11569f7644bSChristian S.J. Peron static int	bpf_stats_sysctl(SYSCTL_HANDLER_ARGS);
11669f7644bSChristian S.J. Peron 
11769f7644bSChristian S.J. Peron /*
11869f7644bSChristian S.J. Peron  * The default read buffer size is patchable.
11969f7644bSChristian S.J. Peron  */
12069f7644bSChristian S.J. Peron SYSCTL_NODE(_net, OID_AUTO, bpf, CTLFLAG_RW, 0, "bpf sysctl");
12169f7644bSChristian S.J. Peron static int bpf_bufsize = 4096;
12269f7644bSChristian S.J. Peron SYSCTL_INT(_net_bpf, OID_AUTO, bufsize, CTLFLAG_RW,
12369f7644bSChristian S.J. Peron     &bpf_bufsize, 0, "");
12469f7644bSChristian S.J. Peron static int bpf_maxbufsize = BPF_MAXBUFSIZE;
12569f7644bSChristian S.J. Peron SYSCTL_INT(_net_bpf, OID_AUTO, maxbufsize, CTLFLAG_RW,
12669f7644bSChristian S.J. Peron     &bpf_maxbufsize, 0, "");
12769f7644bSChristian S.J. Peron static int bpf_maxinsns = BPF_MAXINSNS;
12869f7644bSChristian S.J. Peron SYSCTL_INT(_net_bpf, OID_AUTO, maxinsns, CTLFLAG_RW,
12969f7644bSChristian S.J. Peron     &bpf_maxinsns, 0, "Maximum bpf program instructions");
13069f7644bSChristian S.J. Peron SYSCTL_NODE(_net_bpf, OID_AUTO, stats, CTLFLAG_RW,
13169f7644bSChristian S.J. Peron     bpf_stats_sysctl, "bpf statistics portal");
132df8bae1dSRodney W. Grimes 
13387f6c662SJulian Elischer static	d_open_t	bpfopen;
13487f6c662SJulian Elischer static	d_close_t	bpfclose;
13587f6c662SJulian Elischer static	d_read_t	bpfread;
13687f6c662SJulian Elischer static	d_write_t	bpfwrite;
13787f6c662SJulian Elischer static	d_ioctl_t	bpfioctl;
138243ac7d8SPeter Wemm static	d_poll_t	bpfpoll;
13995aab9ccSJohn-Mark Gurney static	d_kqfilter_t	bpfkqfilter;
14087f6c662SJulian Elischer 
1414e2f199eSPoul-Henning Kamp static struct cdevsw bpf_cdevsw = {
142dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
143dc08ffecSPoul-Henning Kamp 	.d_flags =	D_NEEDGIANT,
1447ac40f5fSPoul-Henning Kamp 	.d_open =	bpfopen,
1457ac40f5fSPoul-Henning Kamp 	.d_close =	bpfclose,
1467ac40f5fSPoul-Henning Kamp 	.d_read =	bpfread,
1477ac40f5fSPoul-Henning Kamp 	.d_write =	bpfwrite,
1487ac40f5fSPoul-Henning Kamp 	.d_ioctl =	bpfioctl,
1497ac40f5fSPoul-Henning Kamp 	.d_poll =	bpfpoll,
1507ac40f5fSPoul-Henning Kamp 	.d_name =	"bpf",
15195aab9ccSJohn-Mark Gurney 	.d_kqfilter =	bpfkqfilter,
1524e2f199eSPoul-Henning Kamp };
15387f6c662SJulian Elischer 
15495aab9ccSJohn-Mark Gurney static struct filterops bpfread_filtops =
15595aab9ccSJohn-Mark Gurney 	{ 1, NULL, filt_bpfdetach, filt_bpfread };
15687f6c662SJulian Elischer 
157df8bae1dSRodney W. Grimes static int
15893e39f0bSChristian S.J. Peron bpf_movein(uio, linktype, mtu, mp, sockp, wfilter)
1598994a245SDag-Erling Smørgrav 	struct uio *uio;
16001399f34SDavid Malone 	int linktype;
16101399f34SDavid Malone 	int mtu;
1628994a245SDag-Erling Smørgrav 	struct mbuf **mp;
1638994a245SDag-Erling Smørgrav 	struct sockaddr *sockp;
16493e39f0bSChristian S.J. Peron 	struct bpf_insn *wfilter;
165df8bae1dSRodney W. Grimes {
166df8bae1dSRodney W. Grimes 	struct mbuf *m;
167df8bae1dSRodney W. Grimes 	int error;
168df8bae1dSRodney W. Grimes 	int len;
169df8bae1dSRodney W. Grimes 	int hlen;
17093e39f0bSChristian S.J. Peron 	int slen;
171df8bae1dSRodney W. Grimes 
172df8bae1dSRodney W. Grimes 	/*
173df8bae1dSRodney W. Grimes 	 * Build a sockaddr based on the data link layer type.
174df8bae1dSRodney W. Grimes 	 * We do this at this level because the ethernet header
175df8bae1dSRodney W. Grimes 	 * is copied directly into the data field of the sockaddr.
176df8bae1dSRodney W. Grimes 	 * In the case of SLIP, there is no header and the packet
177df8bae1dSRodney W. Grimes 	 * is forwarded as is.
178df8bae1dSRodney W. Grimes 	 * Also, we are careful to leave room at the front of the mbuf
179df8bae1dSRodney W. Grimes 	 * for the link level header.
180df8bae1dSRodney W. Grimes 	 */
181df8bae1dSRodney W. Grimes 	switch (linktype) {
182df8bae1dSRodney W. Grimes 
183df8bae1dSRodney W. Grimes 	case DLT_SLIP:
184df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_INET;
185df8bae1dSRodney W. Grimes 		hlen = 0;
186df8bae1dSRodney W. Grimes 		break;
187df8bae1dSRodney W. Grimes 
188df8bae1dSRodney W. Grimes 	case DLT_EN10MB:
189df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_UNSPEC;
190df8bae1dSRodney W. Grimes 		/* XXX Would MAXLINKHDR be better? */
191797f247bSMatthew N. Dodd 		hlen = ETHER_HDR_LEN;
192df8bae1dSRodney W. Grimes 		break;
193df8bae1dSRodney W. Grimes 
194df8bae1dSRodney W. Grimes 	case DLT_FDDI:
195d41f24e7SDavid Greenman 		sockp->sa_family = AF_IMPLINK;
196d41f24e7SDavid Greenman 		hlen = 0;
197df8bae1dSRodney W. Grimes 		break;
198df8bae1dSRodney W. Grimes 
19922f05c43SAndrey A. Chernov 	case DLT_RAW:
200df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_UNSPEC;
201df8bae1dSRodney W. Grimes 		hlen = 0;
202df8bae1dSRodney W. Grimes 		break;
203df8bae1dSRodney W. Grimes 
20401399f34SDavid Malone 	case DLT_NULL:
20501399f34SDavid Malone 		/*
20601399f34SDavid Malone 		 * null interface types require a 4 byte pseudo header which
20701399f34SDavid Malone 		 * corresponds to the address family of the packet.
20801399f34SDavid Malone 		 */
20901399f34SDavid Malone 		sockp->sa_family = AF_UNSPEC;
21001399f34SDavid Malone 		hlen = 4;
21101399f34SDavid Malone 		break;
21201399f34SDavid Malone 
2134f53e3ccSKenjiro Cho 	case DLT_ATM_RFC1483:
2144f53e3ccSKenjiro Cho 		/*
2154f53e3ccSKenjiro Cho 		 * en atm driver requires 4-byte atm pseudo header.
2164f53e3ccSKenjiro Cho 		 * though it isn't standard, vpi:vci needs to be
2174f53e3ccSKenjiro Cho 		 * specified anyway.
2184f53e3ccSKenjiro Cho 		 */
2194f53e3ccSKenjiro Cho 		sockp->sa_family = AF_UNSPEC;
2204f53e3ccSKenjiro Cho 		hlen = 12;	/* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
2214f53e3ccSKenjiro Cho 		break;
2224f53e3ccSKenjiro Cho 
22330fa52a6SBrian Somers 	case DLT_PPP:
22430fa52a6SBrian Somers 		sockp->sa_family = AF_UNSPEC;
22530fa52a6SBrian Somers 		hlen = 4;	/* This should match PPP_HDRLEN */
22630fa52a6SBrian Somers 		break;
22730fa52a6SBrian Somers 
228df8bae1dSRodney W. Grimes 	default:
229df8bae1dSRodney W. Grimes 		return (EIO);
230df8bae1dSRodney W. Grimes 	}
231df8bae1dSRodney W. Grimes 
232df8bae1dSRodney W. Grimes 	len = uio->uio_resid;
23301399f34SDavid Malone 
23401399f34SDavid Malone 	if (len - hlen > mtu)
23501399f34SDavid Malone 		return (EMSGSIZE);
23601399f34SDavid Malone 
237df8bae1dSRodney W. Grimes 	if ((unsigned)len > MCLBYTES)
238df8bae1dSRodney W. Grimes 		return (EIO);
239df8bae1dSRodney W. Grimes 
240963e4c2aSGarrett Wollman 	if (len > MHLEN) {
241a163d034SWarner Losh 		m = m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
24224a229f4SSam Leffler 	} else {
243a163d034SWarner Losh 		MGETHDR(m, M_TRYWAIT, MT_DATA);
244df8bae1dSRodney W. Grimes 	}
24524a229f4SSam Leffler 	if (m == NULL)
24624a229f4SSam Leffler 		return (ENOBUFS);
247963e4c2aSGarrett Wollman 	m->m_pkthdr.len = m->m_len = len;
248963e4c2aSGarrett Wollman 	m->m_pkthdr.rcvif = NULL;
249df8bae1dSRodney W. Grimes 	*mp = m;
25024a229f4SSam Leffler 
25193e39f0bSChristian S.J. Peron 	if (m->m_len < hlen) {
25293e39f0bSChristian S.J. Peron 		error = EPERM;
25393e39f0bSChristian S.J. Peron 		goto bad;
25493e39f0bSChristian S.J. Peron 	}
25593e39f0bSChristian S.J. Peron 
25693e39f0bSChristian S.J. Peron 	error = uiomove(mtod(m, u_char *), len, uio);
25793e39f0bSChristian S.J. Peron 	if (error)
25893e39f0bSChristian S.J. Peron 		goto bad;
25993e39f0bSChristian S.J. Peron 
26093e39f0bSChristian S.J. Peron 	slen = bpf_filter(wfilter, mtod(m, u_char *), len, len);
26193e39f0bSChristian S.J. Peron 	if (slen == 0) {
26293e39f0bSChristian S.J. Peron 		error = EPERM;
26393e39f0bSChristian S.J. Peron 		goto bad;
26493e39f0bSChristian S.J. Peron 	}
26593e39f0bSChristian S.J. Peron 
266df8bae1dSRodney W. Grimes 	/*
26793e39f0bSChristian S.J. Peron 	 * Make room for link header, and copy it to sockaddr
268df8bae1dSRodney W. Grimes 	 */
269df8bae1dSRodney W. Grimes 	if (hlen != 0) {
27093e39f0bSChristian S.J. Peron 		bcopy(m->m_data, sockp->sa_data, hlen);
2714f079e2fSGarrett Wollman 		m->m_pkthdr.len -= hlen;
272df8bae1dSRodney W. Grimes 		m->m_len -= hlen;
273df8bae1dSRodney W. Grimes #if BSD >= 199103
274df8bae1dSRodney W. Grimes 		m->m_data += hlen; /* XXX */
275df8bae1dSRodney W. Grimes #else
276df8bae1dSRodney W. Grimes 		m->m_off += hlen;
277df8bae1dSRodney W. Grimes #endif
278df8bae1dSRodney W. Grimes 	}
27993e39f0bSChristian S.J. Peron 
280df8bae1dSRodney W. Grimes 	return (0);
281df8bae1dSRodney W. Grimes bad:
282df8bae1dSRodney W. Grimes 	m_freem(m);
283df8bae1dSRodney W. Grimes 	return (error);
284df8bae1dSRodney W. Grimes }
285df8bae1dSRodney W. Grimes 
286df8bae1dSRodney W. Grimes /*
287df8bae1dSRodney W. Grimes  * Attach file to the bpf interface, i.e. make d listen on bp.
288df8bae1dSRodney W. Grimes  */
289df8bae1dSRodney W. Grimes static void
290df8bae1dSRodney W. Grimes bpf_attachd(d, bp)
291df8bae1dSRodney W. Grimes 	struct bpf_d *d;
292df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
293df8bae1dSRodney W. Grimes {
294df8bae1dSRodney W. Grimes 	/*
295df8bae1dSRodney W. Grimes 	 * Point d at bp, and add d to the interface's list of listeners.
296df8bae1dSRodney W. Grimes 	 * Finally, point the driver's bpf cookie at the interface so
297df8bae1dSRodney W. Grimes 	 * it will divert packets to bpf.
298df8bae1dSRodney W. Grimes 	 */
299e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
300df8bae1dSRodney W. Grimes 	d->bd_bif = bp;
3014a3feeaaSRobert Watson 	LIST_INSERT_HEAD(&bp->bif_dlist, d, bd_next);
302df8bae1dSRodney W. Grimes 
30369f7644bSChristian S.J. Peron 	bpf_bpfd_cnt++;
30424a229f4SSam Leffler 	*bp->bif_driverp = bp;
305e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
306df8bae1dSRodney W. Grimes }
307df8bae1dSRodney W. Grimes 
308df8bae1dSRodney W. Grimes /*
309df8bae1dSRodney W. Grimes  * Detach a file from its interface.
310df8bae1dSRodney W. Grimes  */
311df8bae1dSRodney W. Grimes static void
312df8bae1dSRodney W. Grimes bpf_detachd(d)
313df8bae1dSRodney W. Grimes 	struct bpf_d *d;
314df8bae1dSRodney W. Grimes {
3156e891d64SPoul-Henning Kamp 	int error;
316df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
31746448b5aSRobert Watson 	struct ifnet *ifp;
318df8bae1dSRodney W. Grimes 
319df8bae1dSRodney W. Grimes 	bp = d->bd_bif;
32046448b5aSRobert Watson 	BPFIF_LOCK(bp);
32146448b5aSRobert Watson 	BPFD_LOCK(d);
32246448b5aSRobert Watson 	ifp = d->bd_bif->bif_ifp;
32346448b5aSRobert Watson 
32446448b5aSRobert Watson 	/*
32546448b5aSRobert Watson 	 * Remove d from the interface's descriptor list.
32646448b5aSRobert Watson 	 */
32746448b5aSRobert Watson 	LIST_REMOVE(d, bd_next);
32846448b5aSRobert Watson 
32969f7644bSChristian S.J. Peron 	bpf_bpfd_cnt--;
33046448b5aSRobert Watson 	/*
33146448b5aSRobert Watson 	 * Let the driver know that there are no more listeners.
33246448b5aSRobert Watson 	 */
33346448b5aSRobert Watson 	if (LIST_EMPTY(&bp->bif_dlist))
33446448b5aSRobert Watson 		*bp->bif_driverp = NULL;
33546448b5aSRobert Watson 
336572bde2aSRobert Watson 	d->bd_bif = NULL;
33746448b5aSRobert Watson 	BPFD_UNLOCK(d);
33846448b5aSRobert Watson 	BPFIF_UNLOCK(bp);
33946448b5aSRobert Watson 
340df8bae1dSRodney W. Grimes 	/*
341df8bae1dSRodney W. Grimes 	 * Check if this descriptor had requested promiscuous mode.
342df8bae1dSRodney W. Grimes 	 * If so, turn it off.
343df8bae1dSRodney W. Grimes 	 */
344df8bae1dSRodney W. Grimes 	if (d->bd_promisc) {
345df8bae1dSRodney W. Grimes 		d->bd_promisc = 0;
34646448b5aSRobert Watson 		error = ifpromisc(ifp, 0);
3476e891d64SPoul-Henning Kamp 		if (error != 0 && error != ENXIO) {
348df8bae1dSRodney W. Grimes 			/*
3496e891d64SPoul-Henning Kamp 			 * ENXIO can happen if a pccard is unplugged
350df8bae1dSRodney W. Grimes 			 * Something is really wrong if we were able to put
351df8bae1dSRodney W. Grimes 			 * the driver into promiscuous mode, but can't
352df8bae1dSRodney W. Grimes 			 * take it out.
353df8bae1dSRodney W. Grimes 			 */
3548eab61f3SSam Leffler 			if_printf(bp->bif_ifp,
3558eab61f3SSam Leffler 				"bpf_detach: ifpromisc failed (%d)\n", error);
3566e891d64SPoul-Henning Kamp 		}
357df8bae1dSRodney W. Grimes 	}
358df8bae1dSRodney W. Grimes }
359df8bae1dSRodney W. Grimes 
360df8bae1dSRodney W. Grimes /*
361df8bae1dSRodney W. Grimes  * Open ethernet device.  Returns ENXIO for illegal minor device number,
362df8bae1dSRodney W. Grimes  * EBUSY if file is open by another process.
363df8bae1dSRodney W. Grimes  */
364df8bae1dSRodney W. Grimes /* ARGSUSED */
36587f6c662SJulian Elischer static	int
366b40ce416SJulian Elischer bpfopen(dev, flags, fmt, td)
36789c9c53dSPoul-Henning Kamp 	struct cdev *dev;
36860039670SBruce Evans 	int flags;
36960039670SBruce Evans 	int fmt;
370b40ce416SJulian Elischer 	struct thread *td;
371df8bae1dSRodney W. Grimes {
372e7bb21b3SJonathan Lemon 	struct bpf_d *d;
373df8bae1dSRodney W. Grimes 
374e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
375bd3a5320SPoul-Henning Kamp 	d = dev->si_drv1;
376df8bae1dSRodney W. Grimes 	/*
377df8bae1dSRodney W. Grimes 	 * Each minor can be opened by only one process.  If the requested
378df8bae1dSRodney W. Grimes 	 * minor is in use, return EBUSY.
379df8bae1dSRodney W. Grimes 	 */
380d17d8184SRobert Watson 	if (d != NULL) {
381e7bb21b3SJonathan Lemon 		mtx_unlock(&bpf_mtx);
382df8bae1dSRodney W. Grimes 		return (EBUSY);
383e7bb21b3SJonathan Lemon 	}
384e7bb21b3SJonathan Lemon 	dev->si_drv1 = (struct bpf_d *)~0;	/* mark device in use */
385e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
386e7bb21b3SJonathan Lemon 
387d1d74c28SJohn Baldwin 	if ((dev->si_flags & SI_NAMED) == 0)
388b0d17ba6SPoul-Henning Kamp 		make_dev(&bpf_cdevsw, minor(dev), UID_ROOT, GID_WHEEL, 0600,
389b0d17ba6SPoul-Henning Kamp 		    "bpf%d", dev2unit(dev));
390a163d034SWarner Losh 	MALLOC(d, struct bpf_d *, sizeof(*d), M_BPF, M_WAITOK | M_ZERO);
391bd3a5320SPoul-Henning Kamp 	dev->si_drv1 = d;
392df8bae1dSRodney W. Grimes 	d->bd_bufsize = bpf_bufsize;
39300a83887SPaul Traina 	d->bd_sig = SIGIO;
3948ed3828cSRobert Watson 	d->bd_seesent = 1;
39569f7644bSChristian S.J. Peron 	d->bd_pid = td->td_proc->p_pid;
39682f4445dSRobert Watson #ifdef MAC
39782f4445dSRobert Watson 	mac_init_bpfdesc(d);
39882f4445dSRobert Watson 	mac_create_bpfdesc(td->td_ucred, d);
39982f4445dSRobert Watson #endif
4006008862bSJohn Baldwin 	mtx_init(&d->bd_mtx, devtoname(dev), "bpf cdev lock", MTX_DEF);
40131199c84SGleb Smirnoff 	callout_init(&d->bd_callout, NET_CALLOUT_MPSAFE);
402571dcd15SSuleiman Souhlal 	knlist_init(&d->bd_sel.si_note, &d->bd_mtx, NULL, NULL, NULL);
403df8bae1dSRodney W. Grimes 
404df8bae1dSRodney W. Grimes 	return (0);
405df8bae1dSRodney W. Grimes }
406df8bae1dSRodney W. Grimes 
407df8bae1dSRodney W. Grimes /*
408df8bae1dSRodney W. Grimes  * Close the descriptor by detaching it from its interface,
409df8bae1dSRodney W. Grimes  * deallocating its buffers, and marking it free.
410df8bae1dSRodney W. Grimes  */
411df8bae1dSRodney W. Grimes /* ARGSUSED */
41287f6c662SJulian Elischer static	int
413b40ce416SJulian Elischer bpfclose(dev, flags, fmt, td)
41489c9c53dSPoul-Henning Kamp 	struct cdev *dev;
41560039670SBruce Evans 	int flags;
41660039670SBruce Evans 	int fmt;
417b40ce416SJulian Elischer 	struct thread *td;
418df8bae1dSRodney W. Grimes {
419e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
420df8bae1dSRodney W. Grimes 
42181bda851SJohn Polstra 	BPFD_LOCK(d);
42281bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
42381bda851SJohn Polstra 		callout_stop(&d->bd_callout);
42481bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
42581bda851SJohn Polstra 	BPFD_UNLOCK(d);
426e649887bSAlfred Perlstein 	funsetown(&d->bd_sigio);
427e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
428df8bae1dSRodney W. Grimes 	if (d->bd_bif)
429df8bae1dSRodney W. Grimes 		bpf_detachd(d);
430e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
4314549709fSBrian Feldman 	selwakeuppri(&d->bd_sel, PRINET);
43282f4445dSRobert Watson #ifdef MAC
43382f4445dSRobert Watson 	mac_destroy_bpfdesc(d);
43482f4445dSRobert Watson #endif /* MAC */
435ad3b9257SJohn-Mark Gurney 	knlist_destroy(&d->bd_sel.si_note);
436df8bae1dSRodney W. Grimes 	bpf_freed(d);
437d17d8184SRobert Watson 	dev->si_drv1 = NULL;
438d722be54SLuigi Rizzo 	free(d, M_BPF);
439df8bae1dSRodney W. Grimes 
440df8bae1dSRodney W. Grimes 	return (0);
441df8bae1dSRodney W. Grimes }
442df8bae1dSRodney W. Grimes 
443df8bae1dSRodney W. Grimes 
444df8bae1dSRodney W. Grimes /*
445df8bae1dSRodney W. Grimes  * Rotate the packet buffers in descriptor d.  Move the store buffer
446df8bae1dSRodney W. Grimes  * into the hold slot, and the free buffer into the store slot.
447df8bae1dSRodney W. Grimes  * Zero the length of the new store buffer.
448df8bae1dSRodney W. Grimes  */
449df8bae1dSRodney W. Grimes #define ROTATE_BUFFERS(d) \
450df8bae1dSRodney W. Grimes 	(d)->bd_hbuf = (d)->bd_sbuf; \
451df8bae1dSRodney W. Grimes 	(d)->bd_hlen = (d)->bd_slen; \
452df8bae1dSRodney W. Grimes 	(d)->bd_sbuf = (d)->bd_fbuf; \
453df8bae1dSRodney W. Grimes 	(d)->bd_slen = 0; \
454d17d8184SRobert Watson 	(d)->bd_fbuf = NULL;
455df8bae1dSRodney W. Grimes /*
456df8bae1dSRodney W. Grimes  *  bpfread - read next chunk of packets from buffers
457df8bae1dSRodney W. Grimes  */
45887f6c662SJulian Elischer static	int
45960039670SBruce Evans bpfread(dev, uio, ioflag)
46089c9c53dSPoul-Henning Kamp 	struct cdev *dev;
4618994a245SDag-Erling Smørgrav 	struct uio *uio;
46260039670SBruce Evans 	int ioflag;
463df8bae1dSRodney W. Grimes {
464e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
46581bda851SJohn Polstra 	int timed_out;
466df8bae1dSRodney W. Grimes 	int error;
467df8bae1dSRodney W. Grimes 
468df8bae1dSRodney W. Grimes 	/*
469df8bae1dSRodney W. Grimes 	 * Restrict application to use a buffer the same size as
470df8bae1dSRodney W. Grimes 	 * as kernel buffers.
471df8bae1dSRodney W. Grimes 	 */
472df8bae1dSRodney W. Grimes 	if (uio->uio_resid != d->bd_bufsize)
473df8bae1dSRodney W. Grimes 		return (EINVAL);
474df8bae1dSRodney W. Grimes 
475e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
47681bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
47781bda851SJohn Polstra 		callout_stop(&d->bd_callout);
47881bda851SJohn Polstra 	timed_out = (d->bd_state == BPF_TIMED_OUT);
47981bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
480df8bae1dSRodney W. Grimes 	/*
481df8bae1dSRodney W. Grimes 	 * If the hold buffer is empty, then do a timed sleep, which
482df8bae1dSRodney W. Grimes 	 * ends when the timeout expires or when enough packets
483df8bae1dSRodney W. Grimes 	 * have arrived to fill the store buffer.
484df8bae1dSRodney W. Grimes 	 */
485572bde2aSRobert Watson 	while (d->bd_hbuf == NULL) {
48681bda851SJohn Polstra 		if ((d->bd_immediate || timed_out) && d->bd_slen != 0) {
487df8bae1dSRodney W. Grimes 			/*
488df8bae1dSRodney W. Grimes 			 * A packet(s) either arrived since the previous
489df8bae1dSRodney W. Grimes 			 * read or arrived while we were asleep.
490df8bae1dSRodney W. Grimes 			 * Rotate the buffers and return what's here.
491df8bae1dSRodney W. Grimes 			 */
492df8bae1dSRodney W. Grimes 			ROTATE_BUFFERS(d);
493df8bae1dSRodney W. Grimes 			break;
494df8bae1dSRodney W. Grimes 		}
495de5d9935SRobert Watson 
496de5d9935SRobert Watson 		/*
497de5d9935SRobert Watson 		 * No data is available, check to see if the bpf device
498de5d9935SRobert Watson 		 * is still pointed at a real interface.  If not, return
499de5d9935SRobert Watson 		 * ENXIO so that the userland process knows to rebind
500de5d9935SRobert Watson 		 * it before using it again.
501de5d9935SRobert Watson 		 */
502de5d9935SRobert Watson 		if (d->bd_bif == NULL) {
503e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
504de5d9935SRobert Watson 			return (ENXIO);
505de5d9935SRobert Watson 		}
506de5d9935SRobert Watson 
507e76eee55SPoul-Henning Kamp 		if (ioflag & O_NONBLOCK) {
508e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
509fba3cfdeSJohn Polstra 			return (EWOULDBLOCK);
510fba3cfdeSJohn Polstra 		}
511521f364bSDag-Erling Smørgrav 		error = msleep(d, &d->bd_mtx, PRINET|PCATCH,
512e7bb21b3SJonathan Lemon 		     "bpf", d->bd_rtout);
513df8bae1dSRodney W. Grimes 		if (error == EINTR || error == ERESTART) {
514e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
515df8bae1dSRodney W. Grimes 			return (error);
516df8bae1dSRodney W. Grimes 		}
517df8bae1dSRodney W. Grimes 		if (error == EWOULDBLOCK) {
518df8bae1dSRodney W. Grimes 			/*
519df8bae1dSRodney W. Grimes 			 * On a timeout, return what's in the buffer,
520df8bae1dSRodney W. Grimes 			 * which may be nothing.  If there is something
521df8bae1dSRodney W. Grimes 			 * in the store buffer, we can rotate the buffers.
522df8bae1dSRodney W. Grimes 			 */
523df8bae1dSRodney W. Grimes 			if (d->bd_hbuf)
524df8bae1dSRodney W. Grimes 				/*
525df8bae1dSRodney W. Grimes 				 * We filled up the buffer in between
526df8bae1dSRodney W. Grimes 				 * getting the timeout and arriving
527df8bae1dSRodney W. Grimes 				 * here, so we don't need to rotate.
528df8bae1dSRodney W. Grimes 				 */
529df8bae1dSRodney W. Grimes 				break;
530df8bae1dSRodney W. Grimes 
531df8bae1dSRodney W. Grimes 			if (d->bd_slen == 0) {
532e7bb21b3SJonathan Lemon 				BPFD_UNLOCK(d);
533df8bae1dSRodney W. Grimes 				return (0);
534df8bae1dSRodney W. Grimes 			}
535df8bae1dSRodney W. Grimes 			ROTATE_BUFFERS(d);
536df8bae1dSRodney W. Grimes 			break;
537df8bae1dSRodney W. Grimes 		}
538df8bae1dSRodney W. Grimes 	}
539df8bae1dSRodney W. Grimes 	/*
540df8bae1dSRodney W. Grimes 	 * At this point, we know we have something in the hold slot.
541df8bae1dSRodney W. Grimes 	 */
542e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
543df8bae1dSRodney W. Grimes 
544df8bae1dSRodney W. Grimes 	/*
545df8bae1dSRodney W. Grimes 	 * Move data from hold buffer into user space.
546df8bae1dSRodney W. Grimes 	 * We know the entire buffer is transferred since
547df8bae1dSRodney W. Grimes 	 * we checked above that the read buffer is bpf_bufsize bytes.
548df8bae1dSRodney W. Grimes 	 */
549e7bb21b3SJonathan Lemon 	error = uiomove(d->bd_hbuf, d->bd_hlen, uio);
550df8bae1dSRodney W. Grimes 
551e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
552df8bae1dSRodney W. Grimes 	d->bd_fbuf = d->bd_hbuf;
553572bde2aSRobert Watson 	d->bd_hbuf = NULL;
554df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
555e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
556df8bae1dSRodney W. Grimes 
557df8bae1dSRodney W. Grimes 	return (error);
558df8bae1dSRodney W. Grimes }
559df8bae1dSRodney W. Grimes 
560df8bae1dSRodney W. Grimes 
561df8bae1dSRodney W. Grimes /*
562df8bae1dSRodney W. Grimes  * If there are processes sleeping on this descriptor, wake them up.
563df8bae1dSRodney W. Grimes  */
564e7bb21b3SJonathan Lemon static __inline void
565df8bae1dSRodney W. Grimes bpf_wakeup(d)
5668994a245SDag-Erling Smørgrav 	struct bpf_d *d;
567df8bae1dSRodney W. Grimes {
568a3272e3cSChristian S.J. Peron 
569a3272e3cSChristian S.J. Peron 	BPFD_LOCK_ASSERT(d);
57081bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING) {
57181bda851SJohn Polstra 		callout_stop(&d->bd_callout);
57281bda851SJohn Polstra 		d->bd_state = BPF_IDLE;
57381bda851SJohn Polstra 	}
574521f364bSDag-Erling Smørgrav 	wakeup(d);
575831d27a9SDon Lewis 	if (d->bd_async && d->bd_sig && d->bd_sigio)
576f1320723SAlfred Perlstein 		pgsigio(&d->bd_sigio, d->bd_sig, 0);
57700a83887SPaul Traina 
578512824f8SSeigo Tanimura 	selwakeuppri(&d->bd_sel, PRINET);
579ad3b9257SJohn-Mark Gurney 	KNOTE_LOCKED(&d->bd_sel.si_note, 0);
580df8bae1dSRodney W. Grimes }
581df8bae1dSRodney W. Grimes 
58281bda851SJohn Polstra static void
58381bda851SJohn Polstra bpf_timed_out(arg)
58481bda851SJohn Polstra 	void *arg;
58581bda851SJohn Polstra {
58681bda851SJohn Polstra 	struct bpf_d *d = (struct bpf_d *)arg;
58781bda851SJohn Polstra 
58881bda851SJohn Polstra 	BPFD_LOCK(d);
58981bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING) {
59081bda851SJohn Polstra 		d->bd_state = BPF_TIMED_OUT;
59181bda851SJohn Polstra 		if (d->bd_slen != 0)
59281bda851SJohn Polstra 			bpf_wakeup(d);
59381bda851SJohn Polstra 	}
59481bda851SJohn Polstra 	BPFD_UNLOCK(d);
59581bda851SJohn Polstra }
59681bda851SJohn Polstra 
59787f6c662SJulian Elischer static	int
59860039670SBruce Evans bpfwrite(dev, uio, ioflag)
59989c9c53dSPoul-Henning Kamp 	struct cdev *dev;
600df8bae1dSRodney W. Grimes 	struct uio *uio;
60160039670SBruce Evans 	int ioflag;
602df8bae1dSRodney W. Grimes {
603e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
604df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
605df8bae1dSRodney W. Grimes 	struct mbuf *m;
606e7bb21b3SJonathan Lemon 	int error;
6078240bf1eSRobert Watson 	struct sockaddr dst;
608df8bae1dSRodney W. Grimes 
609572bde2aSRobert Watson 	if (d->bd_bif == NULL)
610df8bae1dSRodney W. Grimes 		return (ENXIO);
611df8bae1dSRodney W. Grimes 
612df8bae1dSRodney W. Grimes 	ifp = d->bd_bif->bif_ifp;
613df8bae1dSRodney W. Grimes 
6143518d220SSam Leffler 	if ((ifp->if_flags & IFF_UP) == 0)
6153518d220SSam Leffler 		return (ENETDOWN);
6163518d220SSam Leffler 
617df8bae1dSRodney W. Grimes 	if (uio->uio_resid == 0)
618df8bae1dSRodney W. Grimes 		return (0);
619df8bae1dSRodney W. Grimes 
6208240bf1eSRobert Watson 	bzero(&dst, sizeof(dst));
62193e39f0bSChristian S.J. Peron 	error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, ifp->if_mtu,
62293e39f0bSChristian S.J. Peron 	    &m, &dst, d->bd_wfilter);
623df8bae1dSRodney W. Grimes 	if (error)
624df8bae1dSRodney W. Grimes 		return (error);
625df8bae1dSRodney W. Grimes 
626114ae644SMike Smith 	if (d->bd_hdrcmplt)
627114ae644SMike Smith 		dst.sa_family = pseudo_AF_HDRCMPLT;
628114ae644SMike Smith 
62982f4445dSRobert Watson #ifdef MAC
630f747d2ddSRobert Watson 	BPFD_LOCK(d);
63182f4445dSRobert Watson 	mac_create_mbuf_from_bpfdesc(d, m);
632f747d2ddSRobert Watson 	BPFD_UNLOCK(d);
63382f4445dSRobert Watson #endif
634b8f9429dSRobert Watson 	NET_LOCK_GIANT();
635572bde2aSRobert Watson 	error = (*ifp->if_output)(ifp, m, &dst, NULL);
636b8f9429dSRobert Watson 	NET_UNLOCK_GIANT();
637df8bae1dSRodney W. Grimes 	/*
638df8bae1dSRodney W. Grimes 	 * The driver frees the mbuf.
639df8bae1dSRodney W. Grimes 	 */
640df8bae1dSRodney W. Grimes 	return (error);
641df8bae1dSRodney W. Grimes }
642df8bae1dSRodney W. Grimes 
643df8bae1dSRodney W. Grimes /*
644df8bae1dSRodney W. Grimes  * Reset a descriptor by flushing its packet buffer and clearing the
645e7bb21b3SJonathan Lemon  * receive and drop counts.
646df8bae1dSRodney W. Grimes  */
647df8bae1dSRodney W. Grimes static void
648df8bae1dSRodney W. Grimes reset_d(d)
649df8bae1dSRodney W. Grimes 	struct bpf_d *d;
650df8bae1dSRodney W. Grimes {
651e7bb21b3SJonathan Lemon 
652e7bb21b3SJonathan Lemon 	mtx_assert(&d->bd_mtx, MA_OWNED);
653df8bae1dSRodney W. Grimes 	if (d->bd_hbuf) {
654df8bae1dSRodney W. Grimes 		/* Free the hold buffer. */
655df8bae1dSRodney W. Grimes 		d->bd_fbuf = d->bd_hbuf;
656572bde2aSRobert Watson 		d->bd_hbuf = NULL;
657df8bae1dSRodney W. Grimes 	}
658df8bae1dSRodney W. Grimes 	d->bd_slen = 0;
659df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
660df8bae1dSRodney W. Grimes 	d->bd_rcount = 0;
661df8bae1dSRodney W. Grimes 	d->bd_dcount = 0;
66269f7644bSChristian S.J. Peron 	d->bd_fcount = 0;
663df8bae1dSRodney W. Grimes }
664df8bae1dSRodney W. Grimes 
665df8bae1dSRodney W. Grimes /*
666df8bae1dSRodney W. Grimes  *  FIONREAD		Check for read packet available.
667df8bae1dSRodney W. Grimes  *  SIOCGIFADDR		Get interface address - convenient hook to driver.
668df8bae1dSRodney W. Grimes  *  BIOCGBLEN		Get buffer len [for read()].
669df8bae1dSRodney W. Grimes  *  BIOCSETF		Set ethernet read filter.
67093e39f0bSChristian S.J. Peron  *  BIOCSETWF		Set ethernet write filter.
671df8bae1dSRodney W. Grimes  *  BIOCFLUSH		Flush read packet buffer.
672df8bae1dSRodney W. Grimes  *  BIOCPROMISC		Put interface into promiscuous mode.
673df8bae1dSRodney W. Grimes  *  BIOCGDLT		Get link layer type.
674df8bae1dSRodney W. Grimes  *  BIOCGETIF		Get interface name.
675df8bae1dSRodney W. Grimes  *  BIOCSETIF		Set interface.
676df8bae1dSRodney W. Grimes  *  BIOCSRTIMEOUT	Set read timeout.
677df8bae1dSRodney W. Grimes  *  BIOCGRTIMEOUT	Get read timeout.
678df8bae1dSRodney W. Grimes  *  BIOCGSTATS		Get packet stats.
679df8bae1dSRodney W. Grimes  *  BIOCIMMEDIATE	Set immediate mode.
680df8bae1dSRodney W. Grimes  *  BIOCVERSION		Get filter language version.
681114ae644SMike Smith  *  BIOCGHDRCMPLT	Get "header already complete" flag
682114ae644SMike Smith  *  BIOCSHDRCMPLT	Set "header already complete" flag
6838ed3828cSRobert Watson  *  BIOCGSEESENT	Get "see packets sent" flag
6848ed3828cSRobert Watson  *  BIOCSSEESENT	Set "see packets sent" flag
68593e39f0bSChristian S.J. Peron  *  BIOCLOCK		Set "locked" flag
686df8bae1dSRodney W. Grimes  */
687df8bae1dSRodney W. Grimes /* ARGSUSED */
68887f6c662SJulian Elischer static	int
689b40ce416SJulian Elischer bpfioctl(dev, cmd, addr, flags, td)
69089c9c53dSPoul-Henning Kamp 	struct cdev *dev;
691ecbb00a2SDoug Rabson 	u_long cmd;
692df8bae1dSRodney W. Grimes 	caddr_t addr;
69360039670SBruce Evans 	int flags;
694b40ce416SJulian Elischer 	struct thread *td;
695df8bae1dSRodney W. Grimes {
696e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
697e7bb21b3SJonathan Lemon 	int error = 0;
698df8bae1dSRodney W. Grimes 
699b75a24a0SChristian S.J. Peron 	/*
700b75a24a0SChristian S.J. Peron 	 * Refresh PID associated with this descriptor.
701b75a24a0SChristian S.J. Peron 	 */
70281bda851SJohn Polstra 	BPFD_LOCK(d);
703cb1d4f92SChristian S.J. Peron 	d->bd_pid = td->td_proc->p_pid;
70481bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
70581bda851SJohn Polstra 		callout_stop(&d->bd_callout);
70681bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
70781bda851SJohn Polstra 	BPFD_UNLOCK(d);
70881bda851SJohn Polstra 
70993e39f0bSChristian S.J. Peron 	if (d->bd_locked == 1) {
71093e39f0bSChristian S.J. Peron 		switch (cmd) {
71193e39f0bSChristian S.J. Peron 		case BIOCGBLEN:
71293e39f0bSChristian S.J. Peron 		case BIOCFLUSH:
71393e39f0bSChristian S.J. Peron 		case BIOCGDLT:
71493e39f0bSChristian S.J. Peron 		case BIOCGDLTLIST:
71593e39f0bSChristian S.J. Peron 		case BIOCGETIF:
71693e39f0bSChristian S.J. Peron 		case BIOCGRTIMEOUT:
71793e39f0bSChristian S.J. Peron 		case BIOCGSTATS:
71893e39f0bSChristian S.J. Peron 		case BIOCVERSION:
71993e39f0bSChristian S.J. Peron 		case BIOCGRSIG:
72093e39f0bSChristian S.J. Peron 		case BIOCGHDRCMPLT:
72193e39f0bSChristian S.J. Peron 		case FIONREAD:
72293e39f0bSChristian S.J. Peron 		case BIOCLOCK:
72393e39f0bSChristian S.J. Peron 		case BIOCSRTIMEOUT:
72493e39f0bSChristian S.J. Peron 		case BIOCIMMEDIATE:
72593e39f0bSChristian S.J. Peron 		case TIOCGPGRP:
72693e39f0bSChristian S.J. Peron 			break;
72793e39f0bSChristian S.J. Peron 		default:
72893e39f0bSChristian S.J. Peron 			return (EPERM);
72993e39f0bSChristian S.J. Peron 		}
73093e39f0bSChristian S.J. Peron 	}
731df8bae1dSRodney W. Grimes 	switch (cmd) {
732df8bae1dSRodney W. Grimes 
733df8bae1dSRodney W. Grimes 	default:
734df8bae1dSRodney W. Grimes 		error = EINVAL;
735df8bae1dSRodney W. Grimes 		break;
736df8bae1dSRodney W. Grimes 
737df8bae1dSRodney W. Grimes 	/*
738df8bae1dSRodney W. Grimes 	 * Check for read packet available.
739df8bae1dSRodney W. Grimes 	 */
740df8bae1dSRodney W. Grimes 	case FIONREAD:
741df8bae1dSRodney W. Grimes 		{
742df8bae1dSRodney W. Grimes 			int n;
743df8bae1dSRodney W. Grimes 
744e7bb21b3SJonathan Lemon 			BPFD_LOCK(d);
745df8bae1dSRodney W. Grimes 			n = d->bd_slen;
746df8bae1dSRodney W. Grimes 			if (d->bd_hbuf)
747df8bae1dSRodney W. Grimes 				n += d->bd_hlen;
748e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
749df8bae1dSRodney W. Grimes 
750df8bae1dSRodney W. Grimes 			*(int *)addr = n;
751df8bae1dSRodney W. Grimes 			break;
752df8bae1dSRodney W. Grimes 		}
753df8bae1dSRodney W. Grimes 
754df8bae1dSRodney W. Grimes 	case SIOCGIFADDR:
755df8bae1dSRodney W. Grimes 		{
756df8bae1dSRodney W. Grimes 			struct ifnet *ifp;
757df8bae1dSRodney W. Grimes 
758572bde2aSRobert Watson 			if (d->bd_bif == NULL)
759df8bae1dSRodney W. Grimes 				error = EINVAL;
760df8bae1dSRodney W. Grimes 			else {
761df8bae1dSRodney W. Grimes 				ifp = d->bd_bif->bif_ifp;
762df8bae1dSRodney W. Grimes 				error = (*ifp->if_ioctl)(ifp, cmd, addr);
763df8bae1dSRodney W. Grimes 			}
764df8bae1dSRodney W. Grimes 			break;
765df8bae1dSRodney W. Grimes 		}
766df8bae1dSRodney W. Grimes 
767df8bae1dSRodney W. Grimes 	/*
768df8bae1dSRodney W. Grimes 	 * Get buffer len [for read()].
769df8bae1dSRodney W. Grimes 	 */
770df8bae1dSRodney W. Grimes 	case BIOCGBLEN:
771df8bae1dSRodney W. Grimes 		*(u_int *)addr = d->bd_bufsize;
772df8bae1dSRodney W. Grimes 		break;
773df8bae1dSRodney W. Grimes 
774df8bae1dSRodney W. Grimes 	/*
775df8bae1dSRodney W. Grimes 	 * Set buffer length.
776df8bae1dSRodney W. Grimes 	 */
777df8bae1dSRodney W. Grimes 	case BIOCSBLEN:
778572bde2aSRobert Watson 		if (d->bd_bif != NULL)
779df8bae1dSRodney W. Grimes 			error = EINVAL;
780df8bae1dSRodney W. Grimes 		else {
7818994a245SDag-Erling Smørgrav 			u_int size = *(u_int *)addr;
782df8bae1dSRodney W. Grimes 
783eba2a1aeSPoul-Henning Kamp 			if (size > bpf_maxbufsize)
784eba2a1aeSPoul-Henning Kamp 				*(u_int *)addr = size = bpf_maxbufsize;
785df8bae1dSRodney W. Grimes 			else if (size < BPF_MINBUFSIZE)
786df8bae1dSRodney W. Grimes 				*(u_int *)addr = size = BPF_MINBUFSIZE;
787df8bae1dSRodney W. Grimes 			d->bd_bufsize = size;
788df8bae1dSRodney W. Grimes 		}
789df8bae1dSRodney W. Grimes 		break;
790df8bae1dSRodney W. Grimes 
791df8bae1dSRodney W. Grimes 	/*
792df8bae1dSRodney W. Grimes 	 * Set link layer read filter.
793df8bae1dSRodney W. Grimes 	 */
794df8bae1dSRodney W. Grimes 	case BIOCSETF:
79593e39f0bSChristian S.J. Peron 	case BIOCSETWF:
79693e39f0bSChristian S.J. Peron 		error = bpf_setf(d, (struct bpf_program *)addr, cmd);
797df8bae1dSRodney W. Grimes 		break;
798df8bae1dSRodney W. Grimes 
799df8bae1dSRodney W. Grimes 	/*
800df8bae1dSRodney W. Grimes 	 * Flush read packet buffer.
801df8bae1dSRodney W. Grimes 	 */
802df8bae1dSRodney W. Grimes 	case BIOCFLUSH:
803e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
804df8bae1dSRodney W. Grimes 		reset_d(d);
805e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
806df8bae1dSRodney W. Grimes 		break;
807df8bae1dSRodney W. Grimes 
808df8bae1dSRodney W. Grimes 	/*
809df8bae1dSRodney W. Grimes 	 * Put interface into promiscuous mode.
810df8bae1dSRodney W. Grimes 	 */
811df8bae1dSRodney W. Grimes 	case BIOCPROMISC:
812572bde2aSRobert Watson 		if (d->bd_bif == NULL) {
813df8bae1dSRodney W. Grimes 			/*
814df8bae1dSRodney W. Grimes 			 * No interface attached yet.
815df8bae1dSRodney W. Grimes 			 */
816df8bae1dSRodney W. Grimes 			error = EINVAL;
817df8bae1dSRodney W. Grimes 			break;
818df8bae1dSRodney W. Grimes 		}
819df8bae1dSRodney W. Grimes 		if (d->bd_promisc == 0) {
820e7bb21b3SJonathan Lemon 			mtx_lock(&Giant);
821df8bae1dSRodney W. Grimes 			error = ifpromisc(d->bd_bif->bif_ifp, 1);
822e7bb21b3SJonathan Lemon 			mtx_unlock(&Giant);
823df8bae1dSRodney W. Grimes 			if (error == 0)
824df8bae1dSRodney W. Grimes 				d->bd_promisc = 1;
825df8bae1dSRodney W. Grimes 		}
826df8bae1dSRodney W. Grimes 		break;
827df8bae1dSRodney W. Grimes 
828df8bae1dSRodney W. Grimes 	/*
8298eab61f3SSam Leffler 	 * Get current data link type.
830df8bae1dSRodney W. Grimes 	 */
831df8bae1dSRodney W. Grimes 	case BIOCGDLT:
832572bde2aSRobert Watson 		if (d->bd_bif == NULL)
833df8bae1dSRodney W. Grimes 			error = EINVAL;
834df8bae1dSRodney W. Grimes 		else
835df8bae1dSRodney W. Grimes 			*(u_int *)addr = d->bd_bif->bif_dlt;
836df8bae1dSRodney W. Grimes 		break;
837df8bae1dSRodney W. Grimes 
838df8bae1dSRodney W. Grimes 	/*
8398eab61f3SSam Leffler 	 * Get a list of supported data link types.
8408eab61f3SSam Leffler 	 */
8418eab61f3SSam Leffler 	case BIOCGDLTLIST:
842572bde2aSRobert Watson 		if (d->bd_bif == NULL)
8438eab61f3SSam Leffler 			error = EINVAL;
8448eab61f3SSam Leffler 		else
8458eab61f3SSam Leffler 			error = bpf_getdltlist(d, (struct bpf_dltlist *)addr);
8468eab61f3SSam Leffler 		break;
8478eab61f3SSam Leffler 
8488eab61f3SSam Leffler 	/*
8498eab61f3SSam Leffler 	 * Set data link type.
8508eab61f3SSam Leffler 	 */
8518eab61f3SSam Leffler 	case BIOCSDLT:
852572bde2aSRobert Watson 		if (d->bd_bif == NULL)
8538eab61f3SSam Leffler 			error = EINVAL;
8548eab61f3SSam Leffler 		else
8558eab61f3SSam Leffler 			error = bpf_setdlt(d, *(u_int *)addr);
8568eab61f3SSam Leffler 		break;
8578eab61f3SSam Leffler 
8588eab61f3SSam Leffler 	/*
8591dd0feaaSArchie Cobbs 	 * Get interface name.
860df8bae1dSRodney W. Grimes 	 */
861df8bae1dSRodney W. Grimes 	case BIOCGETIF:
862572bde2aSRobert Watson 		if (d->bd_bif == NULL)
863df8bae1dSRodney W. Grimes 			error = EINVAL;
8641dd0feaaSArchie Cobbs 		else {
8651dd0feaaSArchie Cobbs 			struct ifnet *const ifp = d->bd_bif->bif_ifp;
8661dd0feaaSArchie Cobbs 			struct ifreq *const ifr = (struct ifreq *)addr;
8671dd0feaaSArchie Cobbs 
8689bf40edeSBrooks Davis 			strlcpy(ifr->ifr_name, ifp->if_xname,
8699bf40edeSBrooks Davis 			    sizeof(ifr->ifr_name));
8701dd0feaaSArchie Cobbs 		}
871df8bae1dSRodney W. Grimes 		break;
872df8bae1dSRodney W. Grimes 
873df8bae1dSRodney W. Grimes 	/*
874df8bae1dSRodney W. Grimes 	 * Set interface.
875df8bae1dSRodney W. Grimes 	 */
876df8bae1dSRodney W. Grimes 	case BIOCSETIF:
877df8bae1dSRodney W. Grimes 		error = bpf_setif(d, (struct ifreq *)addr);
878df8bae1dSRodney W. Grimes 		break;
879df8bae1dSRodney W. Grimes 
880df8bae1dSRodney W. Grimes 	/*
881df8bae1dSRodney W. Grimes 	 * Set read timeout.
882df8bae1dSRodney W. Grimes 	 */
883df8bae1dSRodney W. Grimes 	case BIOCSRTIMEOUT:
884df8bae1dSRodney W. Grimes 		{
885df8bae1dSRodney W. Grimes 			struct timeval *tv = (struct timeval *)addr;
886df8bae1dSRodney W. Grimes 
887bdc2cdc5SAlexander Langer 			/*
888bdc2cdc5SAlexander Langer 			 * Subtract 1 tick from tvtohz() since this isn't
889bdc2cdc5SAlexander Langer 			 * a one-shot timer.
890bdc2cdc5SAlexander Langer 			 */
891bdc2cdc5SAlexander Langer 			if ((error = itimerfix(tv)) == 0)
892bdc2cdc5SAlexander Langer 				d->bd_rtout = tvtohz(tv) - 1;
893df8bae1dSRodney W. Grimes 			break;
894df8bae1dSRodney W. Grimes 		}
895df8bae1dSRodney W. Grimes 
896df8bae1dSRodney W. Grimes 	/*
897df8bae1dSRodney W. Grimes 	 * Get read timeout.
898df8bae1dSRodney W. Grimes 	 */
899df8bae1dSRodney W. Grimes 	case BIOCGRTIMEOUT:
900df8bae1dSRodney W. Grimes 		{
901df8bae1dSRodney W. Grimes 			struct timeval *tv = (struct timeval *)addr;
902df8bae1dSRodney W. Grimes 
903bdc2cdc5SAlexander Langer 			tv->tv_sec = d->bd_rtout / hz;
904bdc2cdc5SAlexander Langer 			tv->tv_usec = (d->bd_rtout % hz) * tick;
905df8bae1dSRodney W. Grimes 			break;
906df8bae1dSRodney W. Grimes 		}
907df8bae1dSRodney W. Grimes 
908df8bae1dSRodney W. Grimes 	/*
909df8bae1dSRodney W. Grimes 	 * Get packet stats.
910df8bae1dSRodney W. Grimes 	 */
911df8bae1dSRodney W. Grimes 	case BIOCGSTATS:
912df8bae1dSRodney W. Grimes 		{
913df8bae1dSRodney W. Grimes 			struct bpf_stat *bs = (struct bpf_stat *)addr;
914df8bae1dSRodney W. Grimes 
915df8bae1dSRodney W. Grimes 			bs->bs_recv = d->bd_rcount;
916df8bae1dSRodney W. Grimes 			bs->bs_drop = d->bd_dcount;
917df8bae1dSRodney W. Grimes 			break;
918df8bae1dSRodney W. Grimes 		}
919df8bae1dSRodney W. Grimes 
920df8bae1dSRodney W. Grimes 	/*
921df8bae1dSRodney W. Grimes 	 * Set immediate mode.
922df8bae1dSRodney W. Grimes 	 */
923df8bae1dSRodney W. Grimes 	case BIOCIMMEDIATE:
924df8bae1dSRodney W. Grimes 		d->bd_immediate = *(u_int *)addr;
925df8bae1dSRodney W. Grimes 		break;
926df8bae1dSRodney W. Grimes 
927df8bae1dSRodney W. Grimes 	case BIOCVERSION:
928df8bae1dSRodney W. Grimes 		{
929df8bae1dSRodney W. Grimes 			struct bpf_version *bv = (struct bpf_version *)addr;
930df8bae1dSRodney W. Grimes 
931df8bae1dSRodney W. Grimes 			bv->bv_major = BPF_MAJOR_VERSION;
932df8bae1dSRodney W. Grimes 			bv->bv_minor = BPF_MINOR_VERSION;
933df8bae1dSRodney W. Grimes 			break;
934df8bae1dSRodney W. Grimes 		}
93500a83887SPaul Traina 
936114ae644SMike Smith 	/*
937114ae644SMike Smith 	 * Get "header already complete" flag
938114ae644SMike Smith 	 */
939114ae644SMike Smith 	case BIOCGHDRCMPLT:
940114ae644SMike Smith 		*(u_int *)addr = d->bd_hdrcmplt;
941114ae644SMike Smith 		break;
942114ae644SMike Smith 
94393e39f0bSChristian S.J. Peron 	case BIOCLOCK:
94493e39f0bSChristian S.J. Peron 		d->bd_locked = 1;
94593e39f0bSChristian S.J. Peron 		break;
946114ae644SMike Smith 	/*
947114ae644SMike Smith 	 * Set "header already complete" flag
948114ae644SMike Smith 	 */
949114ae644SMike Smith 	case BIOCSHDRCMPLT:
950114ae644SMike Smith 		d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
951114ae644SMike Smith 		break;
952114ae644SMike Smith 
9538ed3828cSRobert Watson 	/*
9548ed3828cSRobert Watson 	 * Get "see sent packets" flag
9558ed3828cSRobert Watson 	 */
9568ed3828cSRobert Watson 	case BIOCGSEESENT:
9578ed3828cSRobert Watson 		*(u_int *)addr = d->bd_seesent;
9588ed3828cSRobert Watson 		break;
9598ed3828cSRobert Watson 
9608ed3828cSRobert Watson 	/*
9618ed3828cSRobert Watson 	 * Set "see sent packets" flag
9628ed3828cSRobert Watson 	 */
9638ed3828cSRobert Watson 	case BIOCSSEESENT:
9648ed3828cSRobert Watson 		d->bd_seesent = *(u_int *)addr;
9658ed3828cSRobert Watson 		break;
9668ed3828cSRobert Watson 
96700a83887SPaul Traina 	case FIONBIO:		/* Non-blocking I/O */
96800a83887SPaul Traina 		break;
96900a83887SPaul Traina 
97000a83887SPaul Traina 	case FIOASYNC:		/* Send signal on receive packets */
97100a83887SPaul Traina 		d->bd_async = *(int *)addr;
97200a83887SPaul Traina 		break;
97300a83887SPaul Traina 
974831d27a9SDon Lewis 	case FIOSETOWN:
975831d27a9SDon Lewis 		error = fsetown(*(int *)addr, &d->bd_sigio);
97600a83887SPaul Traina 		break;
97700a83887SPaul Traina 
978831d27a9SDon Lewis 	case FIOGETOWN:
97991e97a82SDon Lewis 		*(int *)addr = fgetown(&d->bd_sigio);
980831d27a9SDon Lewis 		break;
981831d27a9SDon Lewis 
982831d27a9SDon Lewis 	/* This is deprecated, FIOSETOWN should be used instead. */
983831d27a9SDon Lewis 	case TIOCSPGRP:
984831d27a9SDon Lewis 		error = fsetown(-(*(int *)addr), &d->bd_sigio);
985831d27a9SDon Lewis 		break;
986831d27a9SDon Lewis 
987831d27a9SDon Lewis 	/* This is deprecated, FIOGETOWN should be used instead. */
98800a83887SPaul Traina 	case TIOCGPGRP:
98991e97a82SDon Lewis 		*(int *)addr = -fgetown(&d->bd_sigio);
99000a83887SPaul Traina 		break;
99100a83887SPaul Traina 
99200a83887SPaul Traina 	case BIOCSRSIG:		/* Set receive signal */
99300a83887SPaul Traina 		{
99400a83887SPaul Traina 			u_int sig;
99500a83887SPaul Traina 
99600a83887SPaul Traina 			sig = *(u_int *)addr;
99700a83887SPaul Traina 
99800a83887SPaul Traina 			if (sig >= NSIG)
99900a83887SPaul Traina 				error = EINVAL;
100000a83887SPaul Traina 			else
100100a83887SPaul Traina 				d->bd_sig = sig;
100200a83887SPaul Traina 			break;
100300a83887SPaul Traina 		}
100400a83887SPaul Traina 	case BIOCGRSIG:
100500a83887SPaul Traina 		*(u_int *)addr = d->bd_sig;
100600a83887SPaul Traina 		break;
1007df8bae1dSRodney W. Grimes 	}
1008df8bae1dSRodney W. Grimes 	return (error);
1009df8bae1dSRodney W. Grimes }
1010df8bae1dSRodney W. Grimes 
1011df8bae1dSRodney W. Grimes /*
1012df8bae1dSRodney W. Grimes  * Set d's packet filter program to fp.  If this file already has a filter,
1013df8bae1dSRodney W. Grimes  * free it and replace it.  Returns EINVAL for bogus requests.
1014df8bae1dSRodney W. Grimes  */
1015f708ef1bSPoul-Henning Kamp static int
101693e39f0bSChristian S.J. Peron bpf_setf(d, fp, cmd)
1017df8bae1dSRodney W. Grimes 	struct bpf_d *d;
1018df8bae1dSRodney W. Grimes 	struct bpf_program *fp;
101993e39f0bSChristian S.J. Peron 	u_long cmd;
1020df8bae1dSRodney W. Grimes {
1021df8bae1dSRodney W. Grimes 	struct bpf_insn *fcode, *old;
102293e39f0bSChristian S.J. Peron 	u_int wfilter, flen, size;
1023293c06a1SRuslan Ermilov #ifdef BPF_JITTER
1024ae275efcSJung-uk Kim 	bpf_jit_filter *ofunc;
1025ae275efcSJung-uk Kim #endif
1026df8bae1dSRodney W. Grimes 
102793e39f0bSChristian S.J. Peron 	if (cmd == BIOCSETWF) {
102893e39f0bSChristian S.J. Peron 		old = d->bd_wfilter;
102993e39f0bSChristian S.J. Peron 		wfilter = 1;
1030293c06a1SRuslan Ermilov #ifdef BPF_JITTER
1031ae275efcSJung-uk Kim 		ofunc = NULL;
1032ae275efcSJung-uk Kim #endif
103393e39f0bSChristian S.J. Peron 	} else {
103493e39f0bSChristian S.J. Peron 		wfilter = 0;
103593e39f0bSChristian S.J. Peron 		old = d->bd_rfilter;
1036293c06a1SRuslan Ermilov #ifdef BPF_JITTER
1037ae275efcSJung-uk Kim 		ofunc = d->bd_bfilter;
1038ae275efcSJung-uk Kim #endif
103993e39f0bSChristian S.J. Peron 	}
1040572bde2aSRobert Watson 	if (fp->bf_insns == NULL) {
1041df8bae1dSRodney W. Grimes 		if (fp->bf_len != 0)
1042df8bae1dSRodney W. Grimes 			return (EINVAL);
1043e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
104493e39f0bSChristian S.J. Peron 		if (wfilter)
104593e39f0bSChristian S.J. Peron 			d->bd_wfilter = NULL;
1046ae275efcSJung-uk Kim 		else {
104793e39f0bSChristian S.J. Peron 			d->bd_rfilter = NULL;
1048293c06a1SRuslan Ermilov #ifdef BPF_JITTER
1049ae275efcSJung-uk Kim 			d->bd_bfilter = NULL;
1050ae275efcSJung-uk Kim #endif
1051ae275efcSJung-uk Kim 		}
1052df8bae1dSRodney W. Grimes 		reset_d(d);
1053e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1054572bde2aSRobert Watson 		if (old != NULL)
1055bd3a5320SPoul-Henning Kamp 			free((caddr_t)old, M_BPF);
1056293c06a1SRuslan Ermilov #ifdef BPF_JITTER
1057ae275efcSJung-uk Kim 		if (ofunc != NULL)
1058ae275efcSJung-uk Kim 			bpf_destroy_jit_filter(ofunc);
1059ae275efcSJung-uk Kim #endif
1060df8bae1dSRodney W. Grimes 		return (0);
1061df8bae1dSRodney W. Grimes 	}
1062df8bae1dSRodney W. Grimes 	flen = fp->bf_len;
10630eb20604SChristian S.J. Peron 	if (flen > bpf_maxinsns)
1064df8bae1dSRodney W. Grimes 		return (EINVAL);
1065df8bae1dSRodney W. Grimes 
1066df8bae1dSRodney W. Grimes 	size = flen * sizeof(*fp->bf_insns);
1067a163d034SWarner Losh 	fcode = (struct bpf_insn *)malloc(size, M_BPF, M_WAITOK);
1068df8bae1dSRodney W. Grimes 	if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
1069df8bae1dSRodney W. Grimes 	    bpf_validate(fcode, (int)flen)) {
1070e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
107193e39f0bSChristian S.J. Peron 		if (wfilter)
107293e39f0bSChristian S.J. Peron 			d->bd_wfilter = fcode;
1073ae275efcSJung-uk Kim 		else {
107493e39f0bSChristian S.J. Peron 			d->bd_rfilter = fcode;
1075293c06a1SRuslan Ermilov #ifdef BPF_JITTER
1076ae275efcSJung-uk Kim 			d->bd_bfilter = bpf_jitter(fcode, flen);
1077ae275efcSJung-uk Kim #endif
1078ae275efcSJung-uk Kim 		}
1079df8bae1dSRodney W. Grimes 		reset_d(d);
1080e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1081572bde2aSRobert Watson 		if (old != NULL)
1082bd3a5320SPoul-Henning Kamp 			free((caddr_t)old, M_BPF);
1083293c06a1SRuslan Ermilov #ifdef BPF_JITTER
1084ae275efcSJung-uk Kim 		if (ofunc != NULL)
1085ae275efcSJung-uk Kim 			bpf_destroy_jit_filter(ofunc);
1086ae275efcSJung-uk Kim #endif
1087df8bae1dSRodney W. Grimes 
1088df8bae1dSRodney W. Grimes 		return (0);
1089df8bae1dSRodney W. Grimes 	}
1090bd3a5320SPoul-Henning Kamp 	free((caddr_t)fcode, M_BPF);
1091df8bae1dSRodney W. Grimes 	return (EINVAL);
1092df8bae1dSRodney W. Grimes }
1093df8bae1dSRodney W. Grimes 
1094df8bae1dSRodney W. Grimes /*
1095df8bae1dSRodney W. Grimes  * Detach a file from its current interface (if attached at all) and attach
1096df8bae1dSRodney W. Grimes  * to the interface indicated by the name stored in ifr.
1097df8bae1dSRodney W. Grimes  * Return an errno or 0.
1098df8bae1dSRodney W. Grimes  */
1099df8bae1dSRodney W. Grimes static int
1100df8bae1dSRodney W. Grimes bpf_setif(d, ifr)
1101df8bae1dSRodney W. Grimes 	struct bpf_d *d;
1102df8bae1dSRodney W. Grimes 	struct ifreq *ifr;
1103df8bae1dSRodney W. Grimes {
1104df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
1105e7bb21b3SJonathan Lemon 	int error;
11069b44ff22SGarrett Wollman 	struct ifnet *theywant;
1107df8bae1dSRodney W. Grimes 
11089b44ff22SGarrett Wollman 	theywant = ifunit(ifr->ifr_name);
1109572bde2aSRobert Watson 	if (theywant == NULL)
11109b44ff22SGarrett Wollman 		return ENXIO;
11119b44ff22SGarrett Wollman 
1112df8bae1dSRodney W. Grimes 	/*
1113df8bae1dSRodney W. Grimes 	 * Look through attached interfaces for the named one.
1114df8bae1dSRodney W. Grimes 	 */
1115e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
11164a3feeaaSRobert Watson 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1117df8bae1dSRodney W. Grimes 		struct ifnet *ifp = bp->bif_ifp;
1118df8bae1dSRodney W. Grimes 
1119572bde2aSRobert Watson 		if (ifp == NULL || ifp != theywant)
1120df8bae1dSRodney W. Grimes 			continue;
112124a229f4SSam Leffler 		/* skip additional entry */
1122fc74a9f9SBrooks Davis 		if (bp->bif_driverp != &ifp->if_bpf)
112324a229f4SSam Leffler 			continue;
1124e7bb21b3SJonathan Lemon 
1125e7bb21b3SJonathan Lemon 		mtx_unlock(&bpf_mtx);
1126df8bae1dSRodney W. Grimes 		/*
1127df8bae1dSRodney W. Grimes 		 * We found the requested interface.
1128df8bae1dSRodney W. Grimes 		 * Allocate the packet buffers if we need to.
1129df8bae1dSRodney W. Grimes 		 * If we're already attached to requested interface,
1130df8bae1dSRodney W. Grimes 		 * just flush the buffer.
1131df8bae1dSRodney W. Grimes 		 */
1132572bde2aSRobert Watson 		if (d->bd_sbuf == NULL) {
1133df8bae1dSRodney W. Grimes 			error = bpf_allocbufs(d);
1134df8bae1dSRodney W. Grimes 			if (error != 0)
1135df8bae1dSRodney W. Grimes 				return (error);
1136df8bae1dSRodney W. Grimes 		}
1137df8bae1dSRodney W. Grimes 		if (bp != d->bd_bif) {
1138df8bae1dSRodney W. Grimes 			if (d->bd_bif)
1139df8bae1dSRodney W. Grimes 				/*
1140df8bae1dSRodney W. Grimes 				 * Detach if attached to something else.
1141df8bae1dSRodney W. Grimes 				 */
1142df8bae1dSRodney W. Grimes 				bpf_detachd(d);
1143df8bae1dSRodney W. Grimes 
1144df8bae1dSRodney W. Grimes 			bpf_attachd(d, bp);
1145df8bae1dSRodney W. Grimes 		}
1146e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1147df8bae1dSRodney W. Grimes 		reset_d(d);
1148e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1149df8bae1dSRodney W. Grimes 		return (0);
1150df8bae1dSRodney W. Grimes 	}
1151e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
1152df8bae1dSRodney W. Grimes 	/* Not found. */
1153df8bae1dSRodney W. Grimes 	return (ENXIO);
1154df8bae1dSRodney W. Grimes }
1155df8bae1dSRodney W. Grimes 
1156df8bae1dSRodney W. Grimes /*
1157243ac7d8SPeter Wemm  * Support for select() and poll() system calls
1158df8bae1dSRodney W. Grimes  *
1159df8bae1dSRodney W. Grimes  * Return true iff the specific operation will not block indefinitely.
1160df8bae1dSRodney W. Grimes  * Otherwise, return false but make a note that a selwakeup() must be done.
1161df8bae1dSRodney W. Grimes  */
116237c84183SPoul-Henning Kamp static int
1163b40ce416SJulian Elischer bpfpoll(dev, events, td)
116489c9c53dSPoul-Henning Kamp 	struct cdev *dev;
1165243ac7d8SPeter Wemm 	int events;
1166b40ce416SJulian Elischer 	struct thread *td;
1167df8bae1dSRodney W. Grimes {
1168e7bb21b3SJonathan Lemon 	struct bpf_d *d;
11690832fc64SGarance A Drosehn 	int revents;
1170df8bae1dSRodney W. Grimes 
1171bd3a5320SPoul-Henning Kamp 	d = dev->si_drv1;
1172de5d9935SRobert Watson 	if (d->bd_bif == NULL)
1173de5d9935SRobert Watson 		return (ENXIO);
1174de5d9935SRobert Watson 
1175b75a24a0SChristian S.J. Peron 	/*
1176b75a24a0SChristian S.J. Peron 	 * Refresh PID associated with this descriptor.
1177b75a24a0SChristian S.J. Peron 	 */
11780832fc64SGarance A Drosehn 	revents = events & (POLLOUT | POLLWRNORM);
1179e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
1180cb1d4f92SChristian S.J. Peron 	d->bd_pid = td->td_proc->p_pid;
118175c13541SPoul-Henning Kamp 	if (events & (POLLIN | POLLRDNORM)) {
118295aab9ccSJohn-Mark Gurney 		if (bpf_ready(d))
1183243ac7d8SPeter Wemm 			revents |= events & (POLLIN | POLLRDNORM);
118481bda851SJohn Polstra 		else {
1185ed01445dSJohn Baldwin 			selrecord(td, &d->bd_sel);
118681bda851SJohn Polstra 			/* Start the read timeout if necessary. */
118781bda851SJohn Polstra 			if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
118881bda851SJohn Polstra 				callout_reset(&d->bd_callout, d->bd_rtout,
118981bda851SJohn Polstra 				    bpf_timed_out, d);
119081bda851SJohn Polstra 				d->bd_state = BPF_WAITING;
119181bda851SJohn Polstra 			}
119281bda851SJohn Polstra 		}
119375c13541SPoul-Henning Kamp 	}
1194e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
1195243ac7d8SPeter Wemm 	return (revents);
1196df8bae1dSRodney W. Grimes }
1197df8bae1dSRodney W. Grimes 
1198df8bae1dSRodney W. Grimes /*
119995aab9ccSJohn-Mark Gurney  * Support for kevent() system call.  Register EVFILT_READ filters and
120095aab9ccSJohn-Mark Gurney  * reject all others.
120195aab9ccSJohn-Mark Gurney  */
120295aab9ccSJohn-Mark Gurney int
120395aab9ccSJohn-Mark Gurney bpfkqfilter(dev, kn)
120489c9c53dSPoul-Henning Kamp 	struct cdev *dev;
120595aab9ccSJohn-Mark Gurney 	struct knote *kn;
120695aab9ccSJohn-Mark Gurney {
120795aab9ccSJohn-Mark Gurney 	struct bpf_d *d = (struct bpf_d *)dev->si_drv1;
120895aab9ccSJohn-Mark Gurney 
120995aab9ccSJohn-Mark Gurney 	if (kn->kn_filter != EVFILT_READ)
121095aab9ccSJohn-Mark Gurney 		return (1);
121195aab9ccSJohn-Mark Gurney 
1212b75a24a0SChristian S.J. Peron 	/*
1213b75a24a0SChristian S.J. Peron 	 * Refresh PID associated with this descriptor.
1214b75a24a0SChristian S.J. Peron 	 */
1215cb1d4f92SChristian S.J. Peron 	BPFD_LOCK(d);
1216b75a24a0SChristian S.J. Peron 	d->bd_pid = curthread->td_proc->p_pid;
121795aab9ccSJohn-Mark Gurney 	kn->kn_fop = &bpfread_filtops;
121895aab9ccSJohn-Mark Gurney 	kn->kn_hook = d;
1219ad3b9257SJohn-Mark Gurney 	knlist_add(&d->bd_sel.si_note, kn, 0);
1220cb1d4f92SChristian S.J. Peron 	BPFD_UNLOCK(d);
122195aab9ccSJohn-Mark Gurney 
122295aab9ccSJohn-Mark Gurney 	return (0);
122395aab9ccSJohn-Mark Gurney }
122495aab9ccSJohn-Mark Gurney 
122595aab9ccSJohn-Mark Gurney static void
122695aab9ccSJohn-Mark Gurney filt_bpfdetach(kn)
122795aab9ccSJohn-Mark Gurney 	struct knote *kn;
122895aab9ccSJohn-Mark Gurney {
122995aab9ccSJohn-Mark Gurney 	struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
123095aab9ccSJohn-Mark Gurney 
1231cb1d4f92SChristian S.J. Peron 	BPFD_LOCK(d);
1232ad3b9257SJohn-Mark Gurney 	knlist_remove(&d->bd_sel.si_note, kn, 0);
1233cb1d4f92SChristian S.J. Peron 	BPFD_UNLOCK(d);
123495aab9ccSJohn-Mark Gurney }
123595aab9ccSJohn-Mark Gurney 
123695aab9ccSJohn-Mark Gurney static int
123795aab9ccSJohn-Mark Gurney filt_bpfread(kn, hint)
123895aab9ccSJohn-Mark Gurney 	struct knote *kn;
123995aab9ccSJohn-Mark Gurney 	long hint;
124095aab9ccSJohn-Mark Gurney {
124195aab9ccSJohn-Mark Gurney 	struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
124295aab9ccSJohn-Mark Gurney 	int ready;
124395aab9ccSJohn-Mark Gurney 
124486c9a453SJohn-Mark Gurney 	BPFD_LOCK_ASSERT(d);
124595aab9ccSJohn-Mark Gurney 	ready = bpf_ready(d);
124695aab9ccSJohn-Mark Gurney 	if (ready) {
124795aab9ccSJohn-Mark Gurney 		kn->kn_data = d->bd_slen;
124895aab9ccSJohn-Mark Gurney 		if (d->bd_hbuf)
124995aab9ccSJohn-Mark Gurney 			kn->kn_data += d->bd_hlen;
125095aab9ccSJohn-Mark Gurney 	}
125195aab9ccSJohn-Mark Gurney 	else if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
125295aab9ccSJohn-Mark Gurney 		callout_reset(&d->bd_callout, d->bd_rtout,
125395aab9ccSJohn-Mark Gurney 		    bpf_timed_out, d);
125495aab9ccSJohn-Mark Gurney 		d->bd_state = BPF_WAITING;
125595aab9ccSJohn-Mark Gurney 	}
125695aab9ccSJohn-Mark Gurney 
125795aab9ccSJohn-Mark Gurney 	return (ready);
125895aab9ccSJohn-Mark Gurney }
125995aab9ccSJohn-Mark Gurney 
126095aab9ccSJohn-Mark Gurney /*
1261df8bae1dSRodney W. Grimes  * Incoming linkage from device drivers.  Process the packet pkt, of length
1262df8bae1dSRodney W. Grimes  * pktlen, which is stored in a contiguous buffer.  The packet is parsed
1263df8bae1dSRodney W. Grimes  * by each process' filter, and if accepted, stashed into the corresponding
1264df8bae1dSRodney W. Grimes  * buffer.
1265df8bae1dSRodney W. Grimes  */
1266df8bae1dSRodney W. Grimes void
126724a229f4SSam Leffler bpf_tap(bp, pkt, pktlen)
126824a229f4SSam Leffler 	struct bpf_if *bp;
12698994a245SDag-Erling Smørgrav 	u_char *pkt;
12708994a245SDag-Erling Smørgrav 	u_int pktlen;
1271df8bae1dSRodney W. Grimes {
12728994a245SDag-Erling Smørgrav 	struct bpf_d *d;
12738994a245SDag-Erling Smørgrav 	u_int slen;
1274e7bb21b3SJonathan Lemon 
127546691dd8SRobert Watson 	/*
127646691dd8SRobert Watson 	 * Lockless read to avoid cost of locking the interface if there are
127746691dd8SRobert Watson 	 * no descriptors attached.
127846691dd8SRobert Watson 	 */
12794a3feeaaSRobert Watson 	if (LIST_EMPTY(&bp->bif_dlist))
128046691dd8SRobert Watson 		return;
128146691dd8SRobert Watson 
1282e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
12834a3feeaaSRobert Watson 	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1284e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1285df8bae1dSRodney W. Grimes 		++d->bd_rcount;
1286ae275efcSJung-uk Kim #ifdef BPF_JITTER
1287ae275efcSJung-uk Kim 		if (bpf_jitter_enable != 0 && d->bd_bfilter != NULL)
1288ae275efcSJung-uk Kim 			slen = (*(d->bd_bfilter->func))(pkt, pktlen, pktlen);
1289ae275efcSJung-uk Kim 		else
1290ae275efcSJung-uk Kim #endif
129193e39f0bSChristian S.J. Peron 		slen = bpf_filter(d->bd_rfilter, pkt, pktlen, pktlen);
1292ec272d87SRobert Watson 		if (slen != 0) {
129369f7644bSChristian S.J. Peron 			d->bd_fcount++;
1294ec272d87SRobert Watson #ifdef MAC
129524a229f4SSam Leffler 			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1296ec272d87SRobert Watson #endif
1297df8bae1dSRodney W. Grimes 				catchpacket(d, pkt, pktlen, slen, bcopy);
1298ec272d87SRobert Watson 		}
1299e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1300df8bae1dSRodney W. Grimes 	}
1301e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
1302df8bae1dSRodney W. Grimes }
1303df8bae1dSRodney W. Grimes 
1304df8bae1dSRodney W. Grimes /*
1305df8bae1dSRodney W. Grimes  * Copy data from an mbuf chain into a buffer.  This code is derived
1306df8bae1dSRodney W. Grimes  * from m_copydata in sys/uipc_mbuf.c.
1307df8bae1dSRodney W. Grimes  */
1308df8bae1dSRodney W. Grimes static void
1309df8bae1dSRodney W. Grimes bpf_mcopy(src_arg, dst_arg, len)
1310df8bae1dSRodney W. Grimes 	const void *src_arg;
1311df8bae1dSRodney W. Grimes 	void *dst_arg;
13128994a245SDag-Erling Smørgrav 	size_t len;
1313df8bae1dSRodney W. Grimes {
13148994a245SDag-Erling Smørgrav 	const struct mbuf *m;
13158994a245SDag-Erling Smørgrav 	u_int count;
1316df8bae1dSRodney W. Grimes 	u_char *dst;
1317df8bae1dSRodney W. Grimes 
1318df8bae1dSRodney W. Grimes 	m = src_arg;
1319df8bae1dSRodney W. Grimes 	dst = dst_arg;
1320df8bae1dSRodney W. Grimes 	while (len > 0) {
1321572bde2aSRobert Watson 		if (m == NULL)
1322df8bae1dSRodney W. Grimes 			panic("bpf_mcopy");
1323df8bae1dSRodney W. Grimes 		count = min(m->m_len, len);
13240453d3cbSBruce Evans 		bcopy(mtod(m, void *), dst, count);
1325df8bae1dSRodney W. Grimes 		m = m->m_next;
1326df8bae1dSRodney W. Grimes 		dst += count;
1327df8bae1dSRodney W. Grimes 		len -= count;
1328df8bae1dSRodney W. Grimes 	}
1329df8bae1dSRodney W. Grimes }
1330df8bae1dSRodney W. Grimes 
1331df8bae1dSRodney W. Grimes /*
1332df8bae1dSRodney W. Grimes  * Incoming linkage from device drivers, when packet is in an mbuf chain.
1333df8bae1dSRodney W. Grimes  */
1334df8bae1dSRodney W. Grimes void
133524a229f4SSam Leffler bpf_mtap(bp, m)
133624a229f4SSam Leffler 	struct bpf_if *bp;
1337df8bae1dSRodney W. Grimes 	struct mbuf *m;
1338df8bae1dSRodney W. Grimes {
1339df8bae1dSRodney W. Grimes 	struct bpf_d *d;
1340df8bae1dSRodney W. Grimes 	u_int pktlen, slen;
1341df8bae1dSRodney W. Grimes 
134246691dd8SRobert Watson 	/*
134346691dd8SRobert Watson 	 * Lockless read to avoid cost of locking the interface if there are
134446691dd8SRobert Watson 	 * no descriptors attached.
134546691dd8SRobert Watson 	 */
13464a3feeaaSRobert Watson 	if (LIST_EMPTY(&bp->bif_dlist))
134746691dd8SRobert Watson 		return;
134846691dd8SRobert Watson 
1349f0e2422bSPoul-Henning Kamp 	pktlen = m_length(m, NULL);
1350df8bae1dSRodney W. Grimes 
1351e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
13524a3feeaaSRobert Watson 	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
13538ed3828cSRobert Watson 		if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL))
13548ed3828cSRobert Watson 			continue;
1355e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1356df8bae1dSRodney W. Grimes 		++d->bd_rcount;
1357ae275efcSJung-uk Kim #ifdef BPF_JITTER
1358ae275efcSJung-uk Kim 		/* XXX We cannot handle multiple mbufs. */
1359ae275efcSJung-uk Kim 		if (bpf_jitter_enable != 0 && d->bd_bfilter != NULL &&
1360ae275efcSJung-uk Kim 		    m->m_next == NULL)
1361ae275efcSJung-uk Kim 			slen = (*(d->bd_bfilter->func))(mtod(m, u_char *),
1362ae275efcSJung-uk Kim 			    pktlen, pktlen);
1363ae275efcSJung-uk Kim 		else
1364ae275efcSJung-uk Kim #endif
136593e39f0bSChristian S.J. Peron 		slen = bpf_filter(d->bd_rfilter, (u_char *)m, pktlen, 0);
13664ddfb531SChristian S.J. Peron 		if (slen != 0) {
136769f7644bSChristian S.J. Peron 			d->bd_fcount++;
13680c7fb534SRobert Watson #ifdef MAC
136924a229f4SSam Leffler 			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
13700c7fb534SRobert Watson #endif
13710c7fb534SRobert Watson 				catchpacket(d, (u_char *)m, pktlen, slen,
13720c7fb534SRobert Watson 				    bpf_mcopy);
13734ddfb531SChristian S.J. Peron 		}
1374e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1375df8bae1dSRodney W. Grimes 	}
1376e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
1377df8bae1dSRodney W. Grimes }
1378df8bae1dSRodney W. Grimes 
1379df8bae1dSRodney W. Grimes /*
1380437ffe18SSam Leffler  * Incoming linkage from device drivers, when packet is in
1381437ffe18SSam Leffler  * an mbuf chain and to be prepended by a contiguous header.
1382437ffe18SSam Leffler  */
1383437ffe18SSam Leffler void
1384437ffe18SSam Leffler bpf_mtap2(bp, data, dlen, m)
1385437ffe18SSam Leffler 	struct bpf_if *bp;
1386437ffe18SSam Leffler 	void *data;
1387437ffe18SSam Leffler 	u_int dlen;
1388437ffe18SSam Leffler 	struct mbuf *m;
1389437ffe18SSam Leffler {
1390437ffe18SSam Leffler 	struct mbuf mb;
1391437ffe18SSam Leffler 	struct bpf_d *d;
1392437ffe18SSam Leffler 	u_int pktlen, slen;
1393437ffe18SSam Leffler 
139446691dd8SRobert Watson 	/*
139546691dd8SRobert Watson 	 * Lockless read to avoid cost of locking the interface if there are
139646691dd8SRobert Watson 	 * no descriptors attached.
139746691dd8SRobert Watson 	 */
13984a3feeaaSRobert Watson 	if (LIST_EMPTY(&bp->bif_dlist))
139946691dd8SRobert Watson 		return;
140046691dd8SRobert Watson 
1401437ffe18SSam Leffler 	pktlen = m_length(m, NULL);
1402437ffe18SSam Leffler 	/*
1403437ffe18SSam Leffler 	 * Craft on-stack mbuf suitable for passing to bpf_filter.
1404437ffe18SSam Leffler 	 * Note that we cut corners here; we only setup what's
1405437ffe18SSam Leffler 	 * absolutely needed--this mbuf should never go anywhere else.
1406437ffe18SSam Leffler 	 */
1407437ffe18SSam Leffler 	mb.m_next = m;
1408437ffe18SSam Leffler 	mb.m_data = data;
1409437ffe18SSam Leffler 	mb.m_len = dlen;
1410437ffe18SSam Leffler 	pktlen += dlen;
1411437ffe18SSam Leffler 
1412437ffe18SSam Leffler 	BPFIF_LOCK(bp);
14134a3feeaaSRobert Watson 	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1414437ffe18SSam Leffler 		if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL))
1415437ffe18SSam Leffler 			continue;
1416437ffe18SSam Leffler 		BPFD_LOCK(d);
1417437ffe18SSam Leffler 		++d->bd_rcount;
141893e39f0bSChristian S.J. Peron 		slen = bpf_filter(d->bd_rfilter, (u_char *)&mb, pktlen, 0);
14194ddfb531SChristian S.J. Peron 		if (slen != 0) {
142069f7644bSChristian S.J. Peron 			d->bd_fcount++;
1421437ffe18SSam Leffler #ifdef MAC
1422437ffe18SSam Leffler 			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1423437ffe18SSam Leffler #endif
1424437ffe18SSam Leffler 				catchpacket(d, (u_char *)&mb, pktlen, slen,
1425437ffe18SSam Leffler 				    bpf_mcopy);
14264ddfb531SChristian S.J. Peron 		}
1427437ffe18SSam Leffler 		BPFD_UNLOCK(d);
1428437ffe18SSam Leffler 	}
1429437ffe18SSam Leffler 	BPFIF_UNLOCK(bp);
1430437ffe18SSam Leffler }
1431437ffe18SSam Leffler 
1432437ffe18SSam Leffler /*
1433df8bae1dSRodney W. Grimes  * Move the packet data from interface memory (pkt) into the
14349e610888SDag-Erling Smørgrav  * store buffer.  "cpfn" is the routine called to do the actual data
1435df8bae1dSRodney W. Grimes  * transfer.  bcopy is passed in to copy contiguous chunks, while
1436df8bae1dSRodney W. Grimes  * bpf_mcopy is passed in to copy mbuf chains.  In the latter case,
1437df8bae1dSRodney W. Grimes  * pkt is really an mbuf.
1438df8bae1dSRodney W. Grimes  */
1439df8bae1dSRodney W. Grimes static void
1440df8bae1dSRodney W. Grimes catchpacket(d, pkt, pktlen, snaplen, cpfn)
14418994a245SDag-Erling Smørgrav 	struct bpf_d *d;
14428994a245SDag-Erling Smørgrav 	u_char *pkt;
14438994a245SDag-Erling Smørgrav 	u_int pktlen, snaplen;
14448994a245SDag-Erling Smørgrav 	void (*cpfn)(const void *, void *, size_t);
1445df8bae1dSRodney W. Grimes {
14468994a245SDag-Erling Smørgrav 	struct bpf_hdr *hp;
14478994a245SDag-Erling Smørgrav 	int totlen, curlen;
14488994a245SDag-Erling Smørgrav 	int hdrlen = d->bd_bif->bif_hdrlen;
14497819da79SJohn-Mark Gurney 	int do_wakeup = 0;
14509e610888SDag-Erling Smørgrav 
1451a3272e3cSChristian S.J. Peron 	BPFD_LOCK_ASSERT(d);
1452df8bae1dSRodney W. Grimes 	/*
1453df8bae1dSRodney W. Grimes 	 * Figure out how many bytes to move.  If the packet is
1454df8bae1dSRodney W. Grimes 	 * greater or equal to the snapshot length, transfer that
1455df8bae1dSRodney W. Grimes 	 * much.  Otherwise, transfer the whole packet (unless
1456df8bae1dSRodney W. Grimes 	 * we hit the buffer size limit).
1457df8bae1dSRodney W. Grimes 	 */
1458df8bae1dSRodney W. Grimes 	totlen = hdrlen + min(snaplen, pktlen);
1459df8bae1dSRodney W. Grimes 	if (totlen > d->bd_bufsize)
1460df8bae1dSRodney W. Grimes 		totlen = d->bd_bufsize;
1461df8bae1dSRodney W. Grimes 
1462df8bae1dSRodney W. Grimes 	/*
1463df8bae1dSRodney W. Grimes 	 * Round up the end of the previous packet to the next longword.
1464df8bae1dSRodney W. Grimes 	 */
1465df8bae1dSRodney W. Grimes 	curlen = BPF_WORDALIGN(d->bd_slen);
1466df8bae1dSRodney W. Grimes 	if (curlen + totlen > d->bd_bufsize) {
1467df8bae1dSRodney W. Grimes 		/*
1468df8bae1dSRodney W. Grimes 		 * This packet will overflow the storage buffer.
1469df8bae1dSRodney W. Grimes 		 * Rotate the buffers if we can, then wakeup any
1470df8bae1dSRodney W. Grimes 		 * pending reads.
1471df8bae1dSRodney W. Grimes 		 */
1472572bde2aSRobert Watson 		if (d->bd_fbuf == NULL) {
1473df8bae1dSRodney W. Grimes 			/*
1474df8bae1dSRodney W. Grimes 			 * We haven't completed the previous read yet,
1475df8bae1dSRodney W. Grimes 			 * so drop the packet.
1476df8bae1dSRodney W. Grimes 			 */
1477df8bae1dSRodney W. Grimes 			++d->bd_dcount;
1478df8bae1dSRodney W. Grimes 			return;
1479df8bae1dSRodney W. Grimes 		}
1480df8bae1dSRodney W. Grimes 		ROTATE_BUFFERS(d);
14817819da79SJohn-Mark Gurney 		do_wakeup = 1;
1482df8bae1dSRodney W. Grimes 		curlen = 0;
1483df8bae1dSRodney W. Grimes 	}
148481bda851SJohn Polstra 	else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT)
1485df8bae1dSRodney W. Grimes 		/*
148681bda851SJohn Polstra 		 * Immediate mode is set, or the read timeout has
148781bda851SJohn Polstra 		 * already expired during a select call.  A packet
148881bda851SJohn Polstra 		 * arrived, so the reader should be woken up.
1489df8bae1dSRodney W. Grimes 		 */
14907819da79SJohn-Mark Gurney 		do_wakeup = 1;
1491df8bae1dSRodney W. Grimes 
1492df8bae1dSRodney W. Grimes 	/*
1493df8bae1dSRodney W. Grimes 	 * Append the bpf header.
1494df8bae1dSRodney W. Grimes 	 */
1495df8bae1dSRodney W. Grimes 	hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
1496df8bae1dSRodney W. Grimes 	microtime(&hp->bh_tstamp);
1497df8bae1dSRodney W. Grimes 	hp->bh_datalen = pktlen;
1498df8bae1dSRodney W. Grimes 	hp->bh_hdrlen = hdrlen;
1499df8bae1dSRodney W. Grimes 	/*
1500df8bae1dSRodney W. Grimes 	 * Copy the packet data into the store buffer and update its length.
1501df8bae1dSRodney W. Grimes 	 */
1502df8bae1dSRodney W. Grimes 	(*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen));
1503df8bae1dSRodney W. Grimes 	d->bd_slen = curlen + totlen;
15047819da79SJohn-Mark Gurney 
15057819da79SJohn-Mark Gurney 	if (do_wakeup)
15067819da79SJohn-Mark Gurney 		bpf_wakeup(d);
1507df8bae1dSRodney W. Grimes }
1508df8bae1dSRodney W. Grimes 
1509df8bae1dSRodney W. Grimes /*
1510df8bae1dSRodney W. Grimes  * Initialize all nonzero fields of a descriptor.
1511df8bae1dSRodney W. Grimes  */
1512df8bae1dSRodney W. Grimes static int
1513df8bae1dSRodney W. Grimes bpf_allocbufs(d)
15148994a245SDag-Erling Smørgrav 	struct bpf_d *d;
1515df8bae1dSRodney W. Grimes {
1516a163d034SWarner Losh 	d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1517572bde2aSRobert Watson 	if (d->bd_fbuf == NULL)
1518df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1519df8bae1dSRodney W. Grimes 
1520a163d034SWarner Losh 	d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1521572bde2aSRobert Watson 	if (d->bd_sbuf == NULL) {
1522bd3a5320SPoul-Henning Kamp 		free(d->bd_fbuf, M_BPF);
1523df8bae1dSRodney W. Grimes 		return (ENOBUFS);
1524df8bae1dSRodney W. Grimes 	}
1525df8bae1dSRodney W. Grimes 	d->bd_slen = 0;
1526df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
1527df8bae1dSRodney W. Grimes 	return (0);
1528df8bae1dSRodney W. Grimes }
1529df8bae1dSRodney W. Grimes 
1530df8bae1dSRodney W. Grimes /*
1531df8bae1dSRodney W. Grimes  * Free buffers currently in use by a descriptor.
1532df8bae1dSRodney W. Grimes  * Called on close.
1533df8bae1dSRodney W. Grimes  */
1534df8bae1dSRodney W. Grimes static void
1535df8bae1dSRodney W. Grimes bpf_freed(d)
15368994a245SDag-Erling Smørgrav 	struct bpf_d *d;
1537df8bae1dSRodney W. Grimes {
1538df8bae1dSRodney W. Grimes 	/*
1539df8bae1dSRodney W. Grimes 	 * We don't need to lock out interrupts since this descriptor has
1540df8bae1dSRodney W. Grimes 	 * been detached from its interface and it yet hasn't been marked
1541df8bae1dSRodney W. Grimes 	 * free.
1542df8bae1dSRodney W. Grimes 	 */
1543572bde2aSRobert Watson 	if (d->bd_sbuf != NULL) {
1544bd3a5320SPoul-Henning Kamp 		free(d->bd_sbuf, M_BPF);
1545572bde2aSRobert Watson 		if (d->bd_hbuf != NULL)
1546bd3a5320SPoul-Henning Kamp 			free(d->bd_hbuf, M_BPF);
1547572bde2aSRobert Watson 		if (d->bd_fbuf != NULL)
1548bd3a5320SPoul-Henning Kamp 			free(d->bd_fbuf, M_BPF);
1549df8bae1dSRodney W. Grimes 	}
1550ae275efcSJung-uk Kim 	if (d->bd_rfilter) {
155193e39f0bSChristian S.J. Peron 		free((caddr_t)d->bd_rfilter, M_BPF);
1552ae275efcSJung-uk Kim #ifdef BPF_JITTER
1553ae275efcSJung-uk Kim 		bpf_destroy_jit_filter(d->bd_bfilter);
1554ae275efcSJung-uk Kim #endif
1555ae275efcSJung-uk Kim 	}
155693e39f0bSChristian S.J. Peron 	if (d->bd_wfilter)
155793e39f0bSChristian S.J. Peron 		free((caddr_t)d->bd_wfilter, M_BPF);
1558e7bb21b3SJonathan Lemon 	mtx_destroy(&d->bd_mtx);
1559df8bae1dSRodney W. Grimes }
1560df8bae1dSRodney W. Grimes 
1561df8bae1dSRodney W. Grimes /*
156224a229f4SSam Leffler  * Attach an interface to bpf.  dlt is the link layer type; hdrlen is the
156324a229f4SSam Leffler  * fixed size of the link header (variable length headers not yet supported).
1564df8bae1dSRodney W. Grimes  */
1565df8bae1dSRodney W. Grimes void
15669b44ff22SGarrett Wollman bpfattach(ifp, dlt, hdrlen)
1567df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
1568df8bae1dSRodney W. Grimes 	u_int dlt, hdrlen;
1569df8bae1dSRodney W. Grimes {
157024a229f4SSam Leffler 
157124a229f4SSam Leffler 	bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
157224a229f4SSam Leffler }
157324a229f4SSam Leffler 
157424a229f4SSam Leffler /*
157524a229f4SSam Leffler  * Attach an interface to bpf.  ifp is a pointer to the structure
157624a229f4SSam Leffler  * defining the interface to be attached, dlt is the link layer type,
157724a229f4SSam Leffler  * and hdrlen is the fixed size of the link header (variable length
157824a229f4SSam Leffler  * headers are not yet supporrted).
157924a229f4SSam Leffler  */
158024a229f4SSam Leffler void
158124a229f4SSam Leffler bpfattach2(ifp, dlt, hdrlen, driverp)
158224a229f4SSam Leffler 	struct ifnet *ifp;
158324a229f4SSam Leffler 	u_int dlt, hdrlen;
158424a229f4SSam Leffler 	struct bpf_if **driverp;
158524a229f4SSam Leffler {
1586df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
15876a40ecceSJohn Baldwin 	bp = (struct bpf_if *)malloc(sizeof(*bp), M_BPF, M_NOWAIT | M_ZERO);
1588572bde2aSRobert Watson 	if (bp == NULL)
1589df8bae1dSRodney W. Grimes 		panic("bpfattach");
1590df8bae1dSRodney W. Grimes 
15914a3feeaaSRobert Watson 	LIST_INIT(&bp->bif_dlist);
159224a229f4SSam Leffler 	bp->bif_driverp = driverp;
1593df8bae1dSRodney W. Grimes 	bp->bif_ifp = ifp;
1594df8bae1dSRodney W. Grimes 	bp->bif_dlt = dlt;
15956008862bSJohn Baldwin 	mtx_init(&bp->bif_mtx, "bpf interface lock", NULL, MTX_DEF);
1596df8bae1dSRodney W. Grimes 
1597e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
15984a3feeaaSRobert Watson 	LIST_INSERT_HEAD(&bpf_iflist, bp, bif_next);
1599e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
1600df8bae1dSRodney W. Grimes 
1601572bde2aSRobert Watson 	*bp->bif_driverp = NULL;
1602df8bae1dSRodney W. Grimes 
1603df8bae1dSRodney W. Grimes 	/*
1604df8bae1dSRodney W. Grimes 	 * Compute the length of the bpf header.  This is not necessarily
1605df8bae1dSRodney W. Grimes 	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1606df8bae1dSRodney W. Grimes 	 * that the network layer header begins on a longword boundary (for
1607df8bae1dSRodney W. Grimes 	 * performance reasons and to alleviate alignment restrictions).
1608df8bae1dSRodney W. Grimes 	 */
1609df8bae1dSRodney W. Grimes 	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1610df8bae1dSRodney W. Grimes 
16112eeab939SGarrett Wollman 	if (bootverbose)
161224a229f4SSam Leffler 		if_printf(ifp, "bpf attached\n");
1613df8bae1dSRodney W. Grimes }
161453ac6efbSJulian Elischer 
1615de5d9935SRobert Watson /*
1616de5d9935SRobert Watson  * Detach bpf from an interface.  This involves detaching each descriptor
1617de5d9935SRobert Watson  * associated with the interface, and leaving bd_bif NULL.  Notify each
1618de5d9935SRobert Watson  * descriptor as it's detached so that any sleepers wake up and get
1619de5d9935SRobert Watson  * ENXIO.
1620de5d9935SRobert Watson  */
1621de5d9935SRobert Watson void
1622de5d9935SRobert Watson bpfdetach(ifp)
1623de5d9935SRobert Watson 	struct ifnet *ifp;
1624de5d9935SRobert Watson {
16254a3feeaaSRobert Watson 	struct bpf_if	*bp;
1626de5d9935SRobert Watson 	struct bpf_d	*d;
1627de5d9935SRobert Watson 
1628de5d9935SRobert Watson 	/* Locate BPF interface information */
16298eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
16304a3feeaaSRobert Watson 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1631de5d9935SRobert Watson 		if (ifp == bp->bif_ifp)
1632de5d9935SRobert Watson 			break;
1633de5d9935SRobert Watson 	}
1634de5d9935SRobert Watson 
1635de5d9935SRobert Watson 	/* Interface wasn't attached */
1636d79bf337SMatthew N. Dodd 	if ((bp == NULL) || (bp->bif_ifp == NULL)) {
1637e7bb21b3SJonathan Lemon 		mtx_unlock(&bpf_mtx);
16389bf40edeSBrooks Davis 		printf("bpfdetach: %s was not attached\n", ifp->if_xname);
1639de5d9935SRobert Watson 		return;
1640de5d9935SRobert Watson 	}
1641de5d9935SRobert Watson 
16424a3feeaaSRobert Watson 	LIST_REMOVE(bp, bif_next);
16438eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
1644de5d9935SRobert Watson 
16454a3feeaaSRobert Watson 	while ((d = LIST_FIRST(&bp->bif_dlist)) != NULL) {
1646e7bb21b3SJonathan Lemon 		bpf_detachd(d);
1647e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1648e7bb21b3SJonathan Lemon 		bpf_wakeup(d);
1649e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1650e7bb21b3SJonathan Lemon 	}
1651e7bb21b3SJonathan Lemon 
1652e7bb21b3SJonathan Lemon 	mtx_destroy(&bp->bif_mtx);
1653de5d9935SRobert Watson 	free(bp, M_BPF);
16548eab61f3SSam Leffler }
1655de5d9935SRobert Watson 
16568eab61f3SSam Leffler /*
16578eab61f3SSam Leffler  * Get a list of available data link type of the interface.
16588eab61f3SSam Leffler  */
16598eab61f3SSam Leffler static int
16608eab61f3SSam Leffler bpf_getdltlist(d, bfl)
16618eab61f3SSam Leffler 	struct bpf_d *d;
16628eab61f3SSam Leffler 	struct bpf_dltlist *bfl;
16638eab61f3SSam Leffler {
16648eab61f3SSam Leffler 	int n, error;
16658eab61f3SSam Leffler 	struct ifnet *ifp;
16668eab61f3SSam Leffler 	struct bpf_if *bp;
16678eab61f3SSam Leffler 
16688eab61f3SSam Leffler 	ifp = d->bd_bif->bif_ifp;
16698eab61f3SSam Leffler 	n = 0;
16708eab61f3SSam Leffler 	error = 0;
16718eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
16724a3feeaaSRobert Watson 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
16738eab61f3SSam Leffler 		if (bp->bif_ifp != ifp)
16748eab61f3SSam Leffler 			continue;
16758eab61f3SSam Leffler 		if (bfl->bfl_list != NULL) {
16768eab61f3SSam Leffler 			if (n >= bfl->bfl_len) {
1677e7bb21b3SJonathan Lemon 				mtx_unlock(&bpf_mtx);
16788eab61f3SSam Leffler 				return (ENOMEM);
16798eab61f3SSam Leffler 			}
16808eab61f3SSam Leffler 			error = copyout(&bp->bif_dlt,
16818eab61f3SSam Leffler 			    bfl->bfl_list + n, sizeof(u_int));
16828eab61f3SSam Leffler 		}
16838eab61f3SSam Leffler 		n++;
16848eab61f3SSam Leffler 	}
16858eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
16868eab61f3SSam Leffler 	bfl->bfl_len = n;
16878eab61f3SSam Leffler 	return (error);
16888eab61f3SSam Leffler }
16898eab61f3SSam Leffler 
16908eab61f3SSam Leffler /*
16918eab61f3SSam Leffler  * Set the data link type of a BPF instance.
16928eab61f3SSam Leffler  */
16938eab61f3SSam Leffler static int
16948eab61f3SSam Leffler bpf_setdlt(d, dlt)
16958eab61f3SSam Leffler 	struct bpf_d *d;
16968eab61f3SSam Leffler 	u_int dlt;
16978eab61f3SSam Leffler {
16988eab61f3SSam Leffler 	int error, opromisc;
16998eab61f3SSam Leffler 	struct ifnet *ifp;
17008eab61f3SSam Leffler 	struct bpf_if *bp;
17018eab61f3SSam Leffler 
17028eab61f3SSam Leffler 	if (d->bd_bif->bif_dlt == dlt)
17038eab61f3SSam Leffler 		return (0);
17048eab61f3SSam Leffler 	ifp = d->bd_bif->bif_ifp;
17058eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
17064a3feeaaSRobert Watson 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
17078eab61f3SSam Leffler 		if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
17088eab61f3SSam Leffler 			break;
17098eab61f3SSam Leffler 	}
17108eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
17118eab61f3SSam Leffler 	if (bp != NULL) {
17128eab61f3SSam Leffler 		opromisc = d->bd_promisc;
17138eab61f3SSam Leffler 		bpf_detachd(d);
17148eab61f3SSam Leffler 		bpf_attachd(d, bp);
171593daabddSBrian Feldman 		BPFD_LOCK(d);
17168eab61f3SSam Leffler 		reset_d(d);
17178eab61f3SSam Leffler 		BPFD_UNLOCK(d);
17188eab61f3SSam Leffler 		if (opromisc) {
17198eab61f3SSam Leffler 			error = ifpromisc(bp->bif_ifp, 1);
17208eab61f3SSam Leffler 			if (error)
17218eab61f3SSam Leffler 				if_printf(bp->bif_ifp,
17228eab61f3SSam Leffler 					"bpf_setdlt: ifpromisc failed (%d)\n",
17238eab61f3SSam Leffler 					error);
17248eab61f3SSam Leffler 			else
17258eab61f3SSam Leffler 				d->bd_promisc = 1;
17268eab61f3SSam Leffler 		}
17278eab61f3SSam Leffler 	}
17288eab61f3SSam Leffler 	return (bp == NULL ? EINVAL : 0);
1729de5d9935SRobert Watson }
1730de5d9935SRobert Watson 
17313f54a085SPoul-Henning Kamp static void
17326a113b3dSRobert Watson bpf_clone(arg, cred, name, namelen, dev)
17333f54a085SPoul-Henning Kamp 	void *arg;
17346a113b3dSRobert Watson 	struct ucred *cred;
17353f54a085SPoul-Henning Kamp 	char *name;
17363f54a085SPoul-Henning Kamp 	int namelen;
173789c9c53dSPoul-Henning Kamp 	struct cdev **dev;
17383f54a085SPoul-Henning Kamp {
17393f54a085SPoul-Henning Kamp 	int u;
17403f54a085SPoul-Henning Kamp 
1741f3732fd1SPoul-Henning Kamp 	if (*dev != NULL)
17423f54a085SPoul-Henning Kamp 		return;
1743db901281SPoul-Henning Kamp 	if (dev_stdclone(name, NULL, "bpf", &u) != 1)
17443f54a085SPoul-Henning Kamp 		return;
1745b0d17ba6SPoul-Henning Kamp 	*dev = make_dev(&bpf_cdevsw, unit2minor(u), UID_ROOT, GID_WHEEL, 0600,
1746b0d17ba6SPoul-Henning Kamp 	    "bpf%d", u);
1747f4f6abcbSPoul-Henning Kamp 	dev_ref(*dev);
1748b0d17ba6SPoul-Henning Kamp 	(*dev)->si_flags |= SI_CHEAPCLONE;
17493f54a085SPoul-Henning Kamp 	return;
17503f54a085SPoul-Henning Kamp }
17513f54a085SPoul-Henning Kamp 
1752514ede09SBruce Evans static void
1753514ede09SBruce Evans bpf_drvinit(unused)
1754514ede09SBruce Evans 	void *unused;
175553ac6efbSJulian Elischer {
175653ac6efbSJulian Elischer 
17576008862bSJohn Baldwin 	mtx_init(&bpf_mtx, "bpf global lock", NULL, MTX_DEF);
17584a3feeaaSRobert Watson 	LIST_INIT(&bpf_iflist);
1759db901281SPoul-Henning Kamp 	EVENTHANDLER_REGISTER(dev_clone, bpf_clone, 0, 1000);
17607198bf47SJulian Elischer }
176153ac6efbSJulian Elischer 
176269f7644bSChristian S.J. Peron static void
176369f7644bSChristian S.J. Peron bpfstats_fill_xbpf(struct xbpf_d *d, struct bpf_d *bd)
176469f7644bSChristian S.J. Peron {
176569f7644bSChristian S.J. Peron 
176669f7644bSChristian S.J. Peron 	bzero(d, sizeof(*d));
176769f7644bSChristian S.J. Peron 	BPFD_LOCK_ASSERT(bd);
176869f7644bSChristian S.J. Peron 	d->bd_immediate = bd->bd_immediate;
176969f7644bSChristian S.J. Peron 	d->bd_promisc = bd->bd_promisc;
177069f7644bSChristian S.J. Peron 	d->bd_hdrcmplt = bd->bd_hdrcmplt;
177169f7644bSChristian S.J. Peron 	d->bd_seesent = bd->bd_seesent;
177269f7644bSChristian S.J. Peron 	d->bd_async = bd->bd_async;
177369f7644bSChristian S.J. Peron 	d->bd_rcount = bd->bd_rcount;
177469f7644bSChristian S.J. Peron 	d->bd_dcount = bd->bd_dcount;
177569f7644bSChristian S.J. Peron 	d->bd_fcount = bd->bd_fcount;
177669f7644bSChristian S.J. Peron 	d->bd_sig = bd->bd_sig;
177769f7644bSChristian S.J. Peron 	d->bd_slen = bd->bd_slen;
177869f7644bSChristian S.J. Peron 	d->bd_hlen = bd->bd_hlen;
177969f7644bSChristian S.J. Peron 	d->bd_bufsize = bd->bd_bufsize;
178069f7644bSChristian S.J. Peron 	d->bd_pid = bd->bd_pid;
178169f7644bSChristian S.J. Peron 	strlcpy(d->bd_ifname,
178269f7644bSChristian S.J. Peron 	    bd->bd_bif->bif_ifp->if_xname, IFNAMSIZ);
178393e39f0bSChristian S.J. Peron 	d->bd_locked = bd->bd_locked;
178469f7644bSChristian S.J. Peron }
178569f7644bSChristian S.J. Peron 
178669f7644bSChristian S.J. Peron static int
178769f7644bSChristian S.J. Peron bpf_stats_sysctl(SYSCTL_HANDLER_ARGS)
178869f7644bSChristian S.J. Peron {
1789422a63daSChristian S.J. Peron 	struct xbpf_d *xbdbuf, *xbd;
1790422a63daSChristian S.J. Peron 	int index, error;
179169f7644bSChristian S.J. Peron 	struct bpf_if *bp;
179269f7644bSChristian S.J. Peron 	struct bpf_d *bd;
179369f7644bSChristian S.J. Peron 
179469f7644bSChristian S.J. Peron 	/*
179569f7644bSChristian S.J. Peron 	 * XXX This is not technically correct. It is possible for non
179669f7644bSChristian S.J. Peron 	 * privileged users to open bpf devices. It would make sense
179769f7644bSChristian S.J. Peron 	 * if the users who opened the devices were able to retrieve
179869f7644bSChristian S.J. Peron 	 * the statistics for them, too.
179969f7644bSChristian S.J. Peron 	 */
180069f7644bSChristian S.J. Peron 	error = suser(req->td);
180169f7644bSChristian S.J. Peron 	if (error)
180269f7644bSChristian S.J. Peron 		return (error);
180369f7644bSChristian S.J. Peron 	if (req->oldptr == NULL)
1804422a63daSChristian S.J. Peron 		return (SYSCTL_OUT(req, 0, bpf_bpfd_cnt * sizeof(*xbd)));
180569f7644bSChristian S.J. Peron 	if (bpf_bpfd_cnt == 0)
180669f7644bSChristian S.J. Peron 		return (SYSCTL_OUT(req, 0, 0));
1807422a63daSChristian S.J. Peron 	xbdbuf = malloc(req->oldlen, M_BPF, M_WAITOK);
180869f7644bSChristian S.J. Peron 	mtx_lock(&bpf_mtx);
1809422a63daSChristian S.J. Peron 	if (req->oldlen < (bpf_bpfd_cnt * sizeof(*xbd))) {
1810422a63daSChristian S.J. Peron 		mtx_unlock(&bpf_mtx);
1811422a63daSChristian S.J. Peron 		free(xbdbuf, M_BPF);
1812422a63daSChristian S.J. Peron 		return (ENOMEM);
1813422a63daSChristian S.J. Peron 	}
1814422a63daSChristian S.J. Peron 	index = 0;
181569f7644bSChristian S.J. Peron 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
18161fc9e387SChristian S.J. Peron 		BPFIF_LOCK(bp);
181769f7644bSChristian S.J. Peron 		LIST_FOREACH(bd, &bp->bif_dlist, bd_next) {
1818422a63daSChristian S.J. Peron 			xbd = &xbdbuf[index++];
181969f7644bSChristian S.J. Peron 			BPFD_LOCK(bd);
1820422a63daSChristian S.J. Peron 			bpfstats_fill_xbpf(xbd, bd);
182169f7644bSChristian S.J. Peron 			BPFD_UNLOCK(bd);
182269f7644bSChristian S.J. Peron 		}
18231fc9e387SChristian S.J. Peron 		BPFIF_UNLOCK(bp);
182469f7644bSChristian S.J. Peron 	}
182569f7644bSChristian S.J. Peron 	mtx_unlock(&bpf_mtx);
1826422a63daSChristian S.J. Peron 	error = SYSCTL_OUT(req, xbdbuf, index * sizeof(*xbd));
1827422a63daSChristian S.J. Peron 	free(xbdbuf, M_BPF);
182869f7644bSChristian S.J. Peron 	return (error);
182969f7644bSChristian S.J. Peron }
183069f7644bSChristian S.J. Peron 
1831c9c7976fSPoul-Henning Kamp SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,bpf_drvinit,NULL)
183253ac6efbSJulian Elischer 
18335bb5f2c9SPeter Wemm #else /* !DEV_BPF && !NETGRAPH_BPF */
1834f8dc4716SMike Smith /*
1835f8dc4716SMike Smith  * NOP stubs to allow bpf-using drivers to load and function.
1836f8dc4716SMike Smith  *
1837f8dc4716SMike Smith  * A 'better' implementation would allow the core bpf functionality
1838f8dc4716SMike Smith  * to be loaded at runtime.
1839f8dc4716SMike Smith  */
1840f8dc4716SMike Smith 
1841f8dc4716SMike Smith void
1842e5562beeSSam Leffler bpf_tap(bp, pkt, pktlen)
1843e5562beeSSam Leffler 	struct bpf_if *bp;
18448994a245SDag-Erling Smørgrav 	u_char *pkt;
18458994a245SDag-Erling Smørgrav 	u_int pktlen;
1846f8dc4716SMike Smith {
1847f8dc4716SMike Smith }
1848f8dc4716SMike Smith 
1849f8dc4716SMike Smith void
1850e5562beeSSam Leffler bpf_mtap(bp, m)
1851e5562beeSSam Leffler 	struct bpf_if *bp;
1852f8dc4716SMike Smith 	struct mbuf *m;
1853f8dc4716SMike Smith {
1854f8dc4716SMike Smith }
1855f8dc4716SMike Smith 
1856f8dc4716SMike Smith void
1857437ffe18SSam Leffler bpf_mtap2(bp, d, l, m)
1858437ffe18SSam Leffler 	struct bpf_if *bp;
1859c1dae2f0STim J. Robbins 	void *d;
1860437ffe18SSam Leffler 	u_int l;
1861437ffe18SSam Leffler 	struct mbuf *m;
1862437ffe18SSam Leffler {
1863437ffe18SSam Leffler }
1864437ffe18SSam Leffler 
1865437ffe18SSam Leffler void
1866f8dc4716SMike Smith bpfattach(ifp, dlt, hdrlen)
1867f8dc4716SMike Smith 	struct ifnet *ifp;
1868f8dc4716SMike Smith 	u_int dlt, hdrlen;
1869f8dc4716SMike Smith {
1870f8dc4716SMike Smith }
1871f8dc4716SMike Smith 
1872da626c17SBill Paul void
18735f7a7923SSam Leffler bpfattach2(ifp, dlt, hdrlen, driverp)
18745f7a7923SSam Leffler 	struct ifnet *ifp;
18755f7a7923SSam Leffler 	u_int dlt, hdrlen;
18765f7a7923SSam Leffler 	struct bpf_if **driverp;
18775f7a7923SSam Leffler {
18785f7a7923SSam Leffler }
18795f7a7923SSam Leffler 
18805f7a7923SSam Leffler void
1881da626c17SBill Paul bpfdetach(ifp)
1882da626c17SBill Paul 	struct ifnet *ifp;
1883da626c17SBill Paul {
1884da626c17SBill Paul }
1885da626c17SBill Paul 
1886f8dc4716SMike Smith u_int
1887f8dc4716SMike Smith bpf_filter(pc, p, wirelen, buflen)
18888994a245SDag-Erling Smørgrav 	const struct bpf_insn *pc;
18898994a245SDag-Erling Smørgrav 	u_char *p;
1890f8dc4716SMike Smith 	u_int wirelen;
18918994a245SDag-Erling Smørgrav 	u_int buflen;
1892f8dc4716SMike Smith {
1893f8dc4716SMike Smith 	return -1;	/* "no filter" behaviour */
1894f8dc4716SMike Smith }
1895f8dc4716SMike Smith 
18965bb5f2c9SPeter Wemm int
18975bb5f2c9SPeter Wemm bpf_validate(f, len)
18985bb5f2c9SPeter Wemm 	const struct bpf_insn *f;
18995bb5f2c9SPeter Wemm 	int len;
19005bb5f2c9SPeter Wemm {
19015bb5f2c9SPeter Wemm 	return 0;		/* false */
19025bb5f2c9SPeter Wemm }
19035bb5f2c9SPeter Wemm 
19045bb5f2c9SPeter Wemm #endif /* !DEV_BPF && !NETGRAPH_BPF */
1905