xref: /freebsd/sys/net/bpf.c (revision fa0c2b3474b7a5cc8c6b1b649402fe77515f9eaf)
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  */
36df8bae1dSRodney W. Grimes 
37c7866007SRobert Watson #include <sys/cdefs.h>
38c7866007SRobert Watson __FBSDID("$FreeBSD$");
39c7866007SRobert Watson 
405bb5f2c9SPeter Wemm #include "opt_bpf.h"
4182f4445dSRobert Watson #include "opt_mac.h"
425bb5f2c9SPeter Wemm #include "opt_netgraph.h"
43df8bae1dSRodney W. Grimes 
4495aab9ccSJohn-Mark Gurney #include <sys/types.h>
45df8bae1dSRodney W. Grimes #include <sys/param.h>
46df8bae1dSRodney W. Grimes #include <sys/systm.h>
47ce7609a4SBruce Evans #include <sys/conf.h>
48e76eee55SPoul-Henning Kamp #include <sys/fcntl.h>
494d1d4912SBruce Evans #include <sys/malloc.h>
50df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
51df8bae1dSRodney W. Grimes #include <sys/time.h>
52acd3428bSRobert Watson #include <sys/priv.h>
53df8bae1dSRodney W. Grimes #include <sys/proc.h>
540310c19fSBruce Evans #include <sys/signalvar.h>
55528f627fSBruce Evans #include <sys/filio.h>
56528f627fSBruce Evans #include <sys/sockio.h>
57528f627fSBruce Evans #include <sys/ttycom.h>
58e76eee55SPoul-Henning Kamp #include <sys/uio.h>
59df8bae1dSRodney W. Grimes 
6095aab9ccSJohn-Mark Gurney #include <sys/event.h>
6195aab9ccSJohn-Mark Gurney #include <sys/file.h>
62243ac7d8SPeter Wemm #include <sys/poll.h>
6395aab9ccSJohn-Mark Gurney #include <sys/proc.h>
64df8bae1dSRodney W. Grimes 
65df8bae1dSRodney W. Grimes #include <sys/socket.h>
66df8bae1dSRodney W. Grimes 
67fba9235dSBruce Evans #include <net/if.h>
68df8bae1dSRodney W. Grimes #include <net/bpf.h>
694d621040SChristian S.J. Peron #include <net/bpf_buffer.h>
70ae275efcSJung-uk Kim #ifdef BPF_JITTER
71ae275efcSJung-uk Kim #include <net/bpf_jitter.h>
72ae275efcSJung-uk Kim #endif
734d621040SChristian S.J. Peron #include <net/bpf_zerocopy.h>
74df8bae1dSRodney W. Grimes #include <net/bpfdesc.h>
75df8bae1dSRodney W. Grimes 
76df8bae1dSRodney W. Grimes #include <netinet/in.h>
77df8bae1dSRodney W. Grimes #include <netinet/if_ether.h>
78df8bae1dSRodney W. Grimes #include <sys/kernel.h>
79f708ef1bSPoul-Henning Kamp #include <sys/sysctl.h>
807b778b5eSEivind Eklund 
81246b5467SSam Leffler #include <net80211/ieee80211_freebsd.h>
82246b5467SSam Leffler 
83aed55708SRobert Watson #include <security/mac/mac_framework.h>
84aed55708SRobert Watson 
854d621040SChristian S.J. Peron MALLOC_DEFINE(M_BPF, "BPF", "BPF data");
8687f6c662SJulian Elischer 
875bb5f2c9SPeter Wemm #if defined(DEV_BPF) || defined(NETGRAPH_BPF)
8853ac6efbSJulian Elischer 
89df8bae1dSRodney W. Grimes #define PRINET  26			/* interruptible */
90df8bae1dSRodney W. Grimes 
91560a54e1SJung-uk Kim #define	M_SKIP_BPF	M_SKIP_FIREWALL
92560a54e1SJung-uk Kim 
93df8bae1dSRodney W. Grimes /*
94d1a67300SRobert Watson  * bpf_iflist is a list of BPF interface structures, each corresponding to a
95d1a67300SRobert Watson  * specific DLT.  The same network interface might have several BPF interface
96d1a67300SRobert Watson  * structures registered by different layers in the stack (i.e., 802.11
97d1a67300SRobert Watson  * frames, ethernet frames, etc).
98df8bae1dSRodney W. Grimes  */
994a3feeaaSRobert Watson static LIST_HEAD(, bpf_if)	bpf_iflist;
100e7bb21b3SJonathan Lemon static struct mtx	bpf_mtx;		/* bpf global lock */
10169f7644bSChristian S.J. Peron static int		bpf_bpfd_cnt;
102df8bae1dSRodney W. Grimes 
10319ba8395SChristian S.J. Peron static void	bpf_attachd(struct bpf_d *, struct bpf_if *);
10419ba8395SChristian S.J. Peron static void	bpf_detachd(struct bpf_d *);
105929ddbbbSAlfred Perlstein static void	bpf_freed(struct bpf_d *);
106cb44b6dfSAndrew Thompson static int	bpf_movein(struct uio *, int, struct ifnet *, struct mbuf **,
107560a54e1SJung-uk Kim 		    struct sockaddr *, int *, struct bpf_insn *);
108929ddbbbSAlfred Perlstein static int	bpf_setif(struct bpf_d *, struct ifreq *);
109929ddbbbSAlfred Perlstein static void	bpf_timed_out(void *);
110e7bb21b3SJonathan Lemon static __inline void
111929ddbbbSAlfred Perlstein 		bpf_wakeup(struct bpf_d *);
1124d621040SChristian S.J. Peron static void	catchpacket(struct bpf_d *, u_char *, u_int, u_int,
1134d621040SChristian S.J. Peron 		    void (*)(struct bpf_d *, caddr_t, u_int, void *, u_int),
11491433904SDavid Malone 		    struct timeval *);
115929ddbbbSAlfred Perlstein static void	reset_d(struct bpf_d *);
11693e39f0bSChristian S.J. Peron static int	 bpf_setf(struct bpf_d *, struct bpf_program *, u_long cmd);
1178eab61f3SSam Leffler static int	bpf_getdltlist(struct bpf_d *, struct bpf_dltlist *);
1188eab61f3SSam Leffler static int	bpf_setdlt(struct bpf_d *, u_int);
11995aab9ccSJohn-Mark Gurney static void	filt_bpfdetach(struct knote *);
12095aab9ccSJohn-Mark Gurney static int	filt_bpfread(struct knote *, long);
121a3272e3cSChristian S.J. Peron static void	bpf_drvinit(void *);
1226a113b3dSRobert Watson static void	bpf_clone(void *, struct ucred *, char *, int, struct cdev **);
12369f7644bSChristian S.J. Peron static int	bpf_stats_sysctl(SYSCTL_HANDLER_ARGS);
12469f7644bSChristian S.J. Peron 
12569f7644bSChristian S.J. Peron SYSCTL_NODE(_net, OID_AUTO, bpf, CTLFLAG_RW, 0, "bpf sysctl");
12669f7644bSChristian S.J. Peron static int bpf_maxinsns = BPF_MAXINSNS;
12769f7644bSChristian S.J. Peron SYSCTL_INT(_net_bpf, OID_AUTO, maxinsns, CTLFLAG_RW,
12869f7644bSChristian S.J. Peron     &bpf_maxinsns, 0, "Maximum bpf program instructions");
1294d621040SChristian S.J. Peron static int bpf_zerocopy_enable = 0;
1304d621040SChristian S.J. Peron SYSCTL_INT(_net_bpf, OID_AUTO, zerocopy_enable, CTLFLAG_RW,
1314d621040SChristian S.J. Peron     &bpf_zerocopy_enable, 0, "Enable new zero-copy BPF buffer sessions");
13269f7644bSChristian S.J. Peron SYSCTL_NODE(_net_bpf, OID_AUTO, stats, CTLFLAG_RW,
13369f7644bSChristian S.J. Peron     bpf_stats_sysctl, "bpf statistics portal");
134df8bae1dSRodney W. Grimes 
13587f6c662SJulian Elischer static	d_open_t	bpfopen;
13687f6c662SJulian Elischer static	d_close_t	bpfclose;
13787f6c662SJulian Elischer static	d_read_t	bpfread;
13887f6c662SJulian Elischer static	d_write_t	bpfwrite;
13987f6c662SJulian Elischer static	d_ioctl_t	bpfioctl;
140243ac7d8SPeter Wemm static	d_poll_t	bpfpoll;
14195aab9ccSJohn-Mark Gurney static	d_kqfilter_t	bpfkqfilter;
14287f6c662SJulian Elischer 
1434e2f199eSPoul-Henning Kamp static struct cdevsw bpf_cdevsw = {
144dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
1457ac40f5fSPoul-Henning Kamp 	.d_open =	bpfopen,
1467ac40f5fSPoul-Henning Kamp 	.d_close =	bpfclose,
1477ac40f5fSPoul-Henning Kamp 	.d_read =	bpfread,
1487ac40f5fSPoul-Henning Kamp 	.d_write =	bpfwrite,
1497ac40f5fSPoul-Henning Kamp 	.d_ioctl =	bpfioctl,
1507ac40f5fSPoul-Henning Kamp 	.d_poll =	bpfpoll,
1517ac40f5fSPoul-Henning Kamp 	.d_name =	"bpf",
15295aab9ccSJohn-Mark Gurney 	.d_kqfilter =	bpfkqfilter,
1534e2f199eSPoul-Henning Kamp };
15487f6c662SJulian Elischer 
15595aab9ccSJohn-Mark Gurney static struct filterops bpfread_filtops =
15695aab9ccSJohn-Mark Gurney 	{ 1, NULL, filt_bpfdetach, filt_bpfread };
15787f6c662SJulian Elischer 
1584d621040SChristian S.J. Peron /*
1594d621040SChristian S.J. Peron  * Wrapper functions for various buffering methods.  If the set of buffer
1604d621040SChristian S.J. Peron  * modes expands, we will probably want to introduce a switch data structure
1614d621040SChristian S.J. Peron  * similar to protosw, et.
1624d621040SChristian S.J. Peron  */
1634d621040SChristian S.J. Peron static void
1644d621040SChristian S.J. Peron bpf_append_bytes(struct bpf_d *d, caddr_t buf, u_int offset, void *src,
1654d621040SChristian S.J. Peron     u_int len)
1664d621040SChristian S.J. Peron {
1674d621040SChristian S.J. Peron 
1684d621040SChristian S.J. Peron 	BPFD_LOCK_ASSERT(d);
1694d621040SChristian S.J. Peron 
1704d621040SChristian S.J. Peron 	switch (d->bd_bufmode) {
1714d621040SChristian S.J. Peron 	case BPF_BUFMODE_BUFFER:
1724d621040SChristian S.J. Peron 		return (bpf_buffer_append_bytes(d, buf, offset, src, len));
1734d621040SChristian S.J. Peron 
1744d621040SChristian S.J. Peron 	case BPF_BUFMODE_ZBUF:
1754d621040SChristian S.J. Peron 		d->bd_zcopy++;
1764d621040SChristian S.J. Peron 		return (bpf_zerocopy_append_bytes(d, buf, offset, src, len));
1774d621040SChristian S.J. Peron 
1784d621040SChristian S.J. Peron 	default:
1794d621040SChristian S.J. Peron 		panic("bpf_buf_append_bytes");
1804d621040SChristian S.J. Peron 	}
1814d621040SChristian S.J. Peron }
1824d621040SChristian S.J. Peron 
1834d621040SChristian S.J. Peron static void
1844d621040SChristian S.J. Peron bpf_append_mbuf(struct bpf_d *d, caddr_t buf, u_int offset, void *src,
1854d621040SChristian S.J. Peron     u_int len)
1864d621040SChristian S.J. Peron {
1874d621040SChristian S.J. Peron 
1884d621040SChristian S.J. Peron 	BPFD_LOCK_ASSERT(d);
1894d621040SChristian S.J. Peron 
1904d621040SChristian S.J. Peron 	switch (d->bd_bufmode) {
1914d621040SChristian S.J. Peron 	case BPF_BUFMODE_BUFFER:
1924d621040SChristian S.J. Peron 		return (bpf_buffer_append_mbuf(d, buf, offset, src, len));
1934d621040SChristian S.J. Peron 
1944d621040SChristian S.J. Peron 	case BPF_BUFMODE_ZBUF:
1954d621040SChristian S.J. Peron 		d->bd_zcopy++;
1964d621040SChristian S.J. Peron 		return (bpf_zerocopy_append_mbuf(d, buf, offset, src, len));
1974d621040SChristian S.J. Peron 
1984d621040SChristian S.J. Peron 	default:
1994d621040SChristian S.J. Peron 		panic("bpf_buf_append_mbuf");
2004d621040SChristian S.J. Peron 	}
2014d621040SChristian S.J. Peron }
2024d621040SChristian S.J. Peron 
2034d621040SChristian S.J. Peron /*
2044d621040SChristian S.J. Peron  * If the buffer mechanism has a way to decide that a held buffer can be made
2054d621040SChristian S.J. Peron  * free, then it is exposed via the bpf_canfreebuf() interface.  (1) is
2064d621040SChristian S.J. Peron  * returned if the buffer can be discarded, (0) is returned if it cannot.
2074d621040SChristian S.J. Peron  */
2084d621040SChristian S.J. Peron static int
2094d621040SChristian S.J. Peron bpf_canfreebuf(struct bpf_d *d)
2104d621040SChristian S.J. Peron {
2114d621040SChristian S.J. Peron 
2124d621040SChristian S.J. Peron 	BPFD_LOCK_ASSERT(d);
2134d621040SChristian S.J. Peron 
2144d621040SChristian S.J. Peron 	switch (d->bd_bufmode) {
2154d621040SChristian S.J. Peron 	case BPF_BUFMODE_ZBUF:
2164d621040SChristian S.J. Peron 		return (bpf_zerocopy_canfreebuf(d));
2174d621040SChristian S.J. Peron 	}
2184d621040SChristian S.J. Peron 	return (0);
2194d621040SChristian S.J. Peron }
2204d621040SChristian S.J. Peron 
2214d621040SChristian S.J. Peron void
2224d621040SChristian S.J. Peron bpf_bufheld(struct bpf_d *d)
2234d621040SChristian S.J. Peron {
2244d621040SChristian S.J. Peron 
2254d621040SChristian S.J. Peron 	BPFD_LOCK_ASSERT(d);
2264d621040SChristian S.J. Peron 
2274d621040SChristian S.J. Peron 	switch (d->bd_bufmode) {
2284d621040SChristian S.J. Peron 	case BPF_BUFMODE_ZBUF:
2294d621040SChristian S.J. Peron 		bpf_zerocopy_bufheld(d);
2304d621040SChristian S.J. Peron 		break;
2314d621040SChristian S.J. Peron 	}
2324d621040SChristian S.J. Peron }
2334d621040SChristian S.J. Peron 
2344d621040SChristian S.J. Peron static void
2354d621040SChristian S.J. Peron bpf_free(struct bpf_d *d)
2364d621040SChristian S.J. Peron {
2374d621040SChristian S.J. Peron 
2384d621040SChristian S.J. Peron 	switch (d->bd_bufmode) {
2394d621040SChristian S.J. Peron 	case BPF_BUFMODE_BUFFER:
2404d621040SChristian S.J. Peron 		return (bpf_buffer_free(d));
2414d621040SChristian S.J. Peron 
2424d621040SChristian S.J. Peron 	case BPF_BUFMODE_ZBUF:
2434d621040SChristian S.J. Peron 		return (bpf_zerocopy_free(d));
2444d621040SChristian S.J. Peron 
2454d621040SChristian S.J. Peron 	default:
2464d621040SChristian S.J. Peron 		panic("bpf_buf_free");
2474d621040SChristian S.J. Peron 	}
2484d621040SChristian S.J. Peron }
2494d621040SChristian S.J. Peron 
2504d621040SChristian S.J. Peron static int
2514d621040SChristian S.J. Peron bpf_uiomove(struct bpf_d *d, caddr_t buf, u_int len, struct uio *uio)
2524d621040SChristian S.J. Peron {
2534d621040SChristian S.J. Peron 
2544d621040SChristian S.J. Peron 	if (d->bd_bufmode != BPF_BUFMODE_BUFFER)
2554d621040SChristian S.J. Peron 		return (EOPNOTSUPP);
2564d621040SChristian S.J. Peron 	return (bpf_buffer_uiomove(d, buf, len, uio));
2574d621040SChristian S.J. Peron }
2584d621040SChristian S.J. Peron 
2594d621040SChristian S.J. Peron static int
2604d621040SChristian S.J. Peron bpf_ioctl_sblen(struct bpf_d *d, u_int *i)
2614d621040SChristian S.J. Peron {
2624d621040SChristian S.J. Peron 
2634d621040SChristian S.J. Peron 	if (d->bd_bufmode != BPF_BUFMODE_BUFFER)
2644d621040SChristian S.J. Peron 		return (EOPNOTSUPP);
2654d621040SChristian S.J. Peron 	return (bpf_buffer_ioctl_sblen(d, i));
2664d621040SChristian S.J. Peron }
2674d621040SChristian S.J. Peron 
2684d621040SChristian S.J. Peron static int
2694d621040SChristian S.J. Peron bpf_ioctl_getzmax(struct thread *td, struct bpf_d *d, size_t *i)
2704d621040SChristian S.J. Peron {
2714d621040SChristian S.J. Peron 
2724d621040SChristian S.J. Peron 	if (d->bd_bufmode != BPF_BUFMODE_ZBUF)
2734d621040SChristian S.J. Peron 		return (EOPNOTSUPP);
2744d621040SChristian S.J. Peron 	return (bpf_zerocopy_ioctl_getzmax(td, d, i));
2754d621040SChristian S.J. Peron }
2764d621040SChristian S.J. Peron 
2774d621040SChristian S.J. Peron static int
2784d621040SChristian S.J. Peron bpf_ioctl_rotzbuf(struct thread *td, struct bpf_d *d, struct bpf_zbuf *bz)
2794d621040SChristian S.J. Peron {
2804d621040SChristian S.J. Peron 
2814d621040SChristian S.J. Peron 	if (d->bd_bufmode != BPF_BUFMODE_ZBUF)
2824d621040SChristian S.J. Peron 		return (EOPNOTSUPP);
2834d621040SChristian S.J. Peron 	return (bpf_zerocopy_ioctl_rotzbuf(td, d, bz));
2844d621040SChristian S.J. Peron }
2854d621040SChristian S.J. Peron 
2864d621040SChristian S.J. Peron static int
2874d621040SChristian S.J. Peron bpf_ioctl_setzbuf(struct thread *td, struct bpf_d *d, struct bpf_zbuf *bz)
2884d621040SChristian S.J. Peron {
2894d621040SChristian S.J. Peron 
2904d621040SChristian S.J. Peron 	if (d->bd_bufmode != BPF_BUFMODE_ZBUF)
2914d621040SChristian S.J. Peron 		return (EOPNOTSUPP);
2924d621040SChristian S.J. Peron 	return (bpf_zerocopy_ioctl_setzbuf(td, d, bz));
2934d621040SChristian S.J. Peron }
2944d621040SChristian S.J. Peron 
2954d621040SChristian S.J. Peron /*
2964d621040SChristian S.J. Peron  * General BPF functions.
2974d621040SChristian S.J. Peron  */
298df8bae1dSRodney W. Grimes static int
299cb44b6dfSAndrew Thompson bpf_movein(struct uio *uio, int linktype, struct ifnet *ifp, struct mbuf **mp,
300560a54e1SJung-uk Kim     struct sockaddr *sockp, int *hdrlen, struct bpf_insn *wfilter)
301df8bae1dSRodney W. Grimes {
302246b5467SSam Leffler 	const struct ieee80211_bpf_params *p;
303cb44b6dfSAndrew Thompson 	struct ether_header *eh;
304df8bae1dSRodney W. Grimes 	struct mbuf *m;
305df8bae1dSRodney W. Grimes 	int error;
306df8bae1dSRodney W. Grimes 	int len;
307df8bae1dSRodney W. Grimes 	int hlen;
30893e39f0bSChristian S.J. Peron 	int slen;
309df8bae1dSRodney W. Grimes 
310df8bae1dSRodney W. Grimes 	/*
311df8bae1dSRodney W. Grimes 	 * Build a sockaddr based on the data link layer type.
312df8bae1dSRodney W. Grimes 	 * We do this at this level because the ethernet header
313df8bae1dSRodney W. Grimes 	 * is copied directly into the data field of the sockaddr.
314df8bae1dSRodney W. Grimes 	 * In the case of SLIP, there is no header and the packet
315df8bae1dSRodney W. Grimes 	 * is forwarded as is.
316df8bae1dSRodney W. Grimes 	 * Also, we are careful to leave room at the front of the mbuf
317df8bae1dSRodney W. Grimes 	 * for the link level header.
318df8bae1dSRodney W. Grimes 	 */
319df8bae1dSRodney W. Grimes 	switch (linktype) {
320df8bae1dSRodney W. Grimes 
321df8bae1dSRodney W. Grimes 	case DLT_SLIP:
322df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_INET;
323df8bae1dSRodney W. Grimes 		hlen = 0;
324df8bae1dSRodney W. Grimes 		break;
325df8bae1dSRodney W. Grimes 
326df8bae1dSRodney W. Grimes 	case DLT_EN10MB:
327df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_UNSPEC;
328df8bae1dSRodney W. Grimes 		/* XXX Would MAXLINKHDR be better? */
329797f247bSMatthew N. Dodd 		hlen = ETHER_HDR_LEN;
330df8bae1dSRodney W. Grimes 		break;
331df8bae1dSRodney W. Grimes 
332df8bae1dSRodney W. Grimes 	case DLT_FDDI:
333d41f24e7SDavid Greenman 		sockp->sa_family = AF_IMPLINK;
334d41f24e7SDavid Greenman 		hlen = 0;
335df8bae1dSRodney W. Grimes 		break;
336df8bae1dSRodney W. Grimes 
33722f05c43SAndrey A. Chernov 	case DLT_RAW:
338df8bae1dSRodney W. Grimes 		sockp->sa_family = AF_UNSPEC;
339df8bae1dSRodney W. Grimes 		hlen = 0;
340df8bae1dSRodney W. Grimes 		break;
341df8bae1dSRodney W. Grimes 
34201399f34SDavid Malone 	case DLT_NULL:
34301399f34SDavid Malone 		/*
34401399f34SDavid Malone 		 * null interface types require a 4 byte pseudo header which
34501399f34SDavid Malone 		 * corresponds to the address family of the packet.
34601399f34SDavid Malone 		 */
34701399f34SDavid Malone 		sockp->sa_family = AF_UNSPEC;
34801399f34SDavid Malone 		hlen = 4;
34901399f34SDavid Malone 		break;
35001399f34SDavid Malone 
3514f53e3ccSKenjiro Cho 	case DLT_ATM_RFC1483:
3524f53e3ccSKenjiro Cho 		/*
3534f53e3ccSKenjiro Cho 		 * en atm driver requires 4-byte atm pseudo header.
3544f53e3ccSKenjiro Cho 		 * though it isn't standard, vpi:vci needs to be
3554f53e3ccSKenjiro Cho 		 * specified anyway.
3564f53e3ccSKenjiro Cho 		 */
3574f53e3ccSKenjiro Cho 		sockp->sa_family = AF_UNSPEC;
3584f53e3ccSKenjiro Cho 		hlen = 12;	/* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
3594f53e3ccSKenjiro Cho 		break;
3604f53e3ccSKenjiro Cho 
36130fa52a6SBrian Somers 	case DLT_PPP:
36230fa52a6SBrian Somers 		sockp->sa_family = AF_UNSPEC;
36330fa52a6SBrian Somers 		hlen = 4;	/* This should match PPP_HDRLEN */
36430fa52a6SBrian Somers 		break;
36530fa52a6SBrian Somers 
366246b5467SSam Leffler 	case DLT_IEEE802_11:		/* IEEE 802.11 wireless */
367246b5467SSam Leffler 		sockp->sa_family = AF_IEEE80211;
368246b5467SSam Leffler 		hlen = 0;
369246b5467SSam Leffler 		break;
370246b5467SSam Leffler 
371246b5467SSam Leffler 	case DLT_IEEE802_11_RADIO:	/* IEEE 802.11 wireless w/ phy params */
372246b5467SSam Leffler 		sockp->sa_family = AF_IEEE80211;
373246b5467SSam Leffler 		sockp->sa_len = 12;	/* XXX != 0 */
374246b5467SSam Leffler 		hlen = sizeof(struct ieee80211_bpf_params);
375246b5467SSam Leffler 		break;
376246b5467SSam Leffler 
377df8bae1dSRodney W. Grimes 	default:
378df8bae1dSRodney W. Grimes 		return (EIO);
379df8bae1dSRodney W. Grimes 	}
380df8bae1dSRodney W. Grimes 
381df8bae1dSRodney W. Grimes 	len = uio->uio_resid;
38201399f34SDavid Malone 
383cb44b6dfSAndrew Thompson 	if (len - hlen > ifp->if_mtu)
38401399f34SDavid Malone 		return (EMSGSIZE);
38501399f34SDavid Malone 
386df8bae1dSRodney W. Grimes 	if ((unsigned)len > MCLBYTES)
387df8bae1dSRodney W. Grimes 		return (EIO);
388df8bae1dSRodney W. Grimes 
389963e4c2aSGarrett Wollman 	if (len > MHLEN) {
390a163d034SWarner Losh 		m = m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
39124a229f4SSam Leffler 	} else {
392a163d034SWarner Losh 		MGETHDR(m, M_TRYWAIT, MT_DATA);
393df8bae1dSRodney W. Grimes 	}
39424a229f4SSam Leffler 	if (m == NULL)
39524a229f4SSam Leffler 		return (ENOBUFS);
396963e4c2aSGarrett Wollman 	m->m_pkthdr.len = m->m_len = len;
397963e4c2aSGarrett Wollman 	m->m_pkthdr.rcvif = NULL;
398df8bae1dSRodney W. Grimes 	*mp = m;
39924a229f4SSam Leffler 
40093e39f0bSChristian S.J. Peron 	if (m->m_len < hlen) {
40193e39f0bSChristian S.J. Peron 		error = EPERM;
40293e39f0bSChristian S.J. Peron 		goto bad;
40393e39f0bSChristian S.J. Peron 	}
40493e39f0bSChristian S.J. Peron 
40593e39f0bSChristian S.J. Peron 	error = uiomove(mtod(m, u_char *), len, uio);
40693e39f0bSChristian S.J. Peron 	if (error)
40793e39f0bSChristian S.J. Peron 		goto bad;
40893e39f0bSChristian S.J. Peron 
40993e39f0bSChristian S.J. Peron 	slen = bpf_filter(wfilter, mtod(m, u_char *), len, len);
41093e39f0bSChristian S.J. Peron 	if (slen == 0) {
41193e39f0bSChristian S.J. Peron 		error = EPERM;
41293e39f0bSChristian S.J. Peron 		goto bad;
41393e39f0bSChristian S.J. Peron 	}
41493e39f0bSChristian S.J. Peron 
415cb44b6dfSAndrew Thompson 	/* Check for multicast destination */
416cb44b6dfSAndrew Thompson 	switch (linktype) {
417cb44b6dfSAndrew Thompson 	case DLT_EN10MB:
418cb44b6dfSAndrew Thompson 		eh = mtod(m, struct ether_header *);
419cb44b6dfSAndrew Thompson 		if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
420cb44b6dfSAndrew Thompson 			if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
421cb44b6dfSAndrew Thompson 			    ETHER_ADDR_LEN) == 0)
422cb44b6dfSAndrew Thompson 				m->m_flags |= M_BCAST;
423cb44b6dfSAndrew Thompson 			else
424cb44b6dfSAndrew Thompson 				m->m_flags |= M_MCAST;
425cb44b6dfSAndrew Thompson 		}
426cb44b6dfSAndrew Thompson 		break;
427cb44b6dfSAndrew Thompson 	}
428cb44b6dfSAndrew Thompson 
429df8bae1dSRodney W. Grimes 	/*
43093e39f0bSChristian S.J. Peron 	 * Make room for link header, and copy it to sockaddr
431df8bae1dSRodney W. Grimes 	 */
432df8bae1dSRodney W. Grimes 	if (hlen != 0) {
433246b5467SSam Leffler 		if (sockp->sa_family == AF_IEEE80211) {
434246b5467SSam Leffler 			/*
435246b5467SSam Leffler 			 * Collect true length from the parameter header
436246b5467SSam Leffler 			 * NB: sockp is known to be zero'd so if we do a
437246b5467SSam Leffler 			 *     short copy unspecified parameters will be
438246b5467SSam Leffler 			 *     zero.
439246b5467SSam Leffler 			 * NB: packet may not be aligned after stripping
440246b5467SSam Leffler 			 *     bpf params
441246b5467SSam Leffler 			 * XXX check ibp_vers
442246b5467SSam Leffler 			 */
443246b5467SSam Leffler 			p = mtod(m, const struct ieee80211_bpf_params *);
444246b5467SSam Leffler 			hlen = p->ibp_len;
445246b5467SSam Leffler 			if (hlen > sizeof(sockp->sa_data)) {
446246b5467SSam Leffler 				error = EINVAL;
447246b5467SSam Leffler 				goto bad;
448246b5467SSam Leffler 			}
449246b5467SSam Leffler 		}
45093e39f0bSChristian S.J. Peron 		bcopy(m->m_data, sockp->sa_data, hlen);
451df8bae1dSRodney W. Grimes 	}
452560a54e1SJung-uk Kim 	*hdrlen = hlen;
45393e39f0bSChristian S.J. Peron 
454df8bae1dSRodney W. Grimes 	return (0);
455df8bae1dSRodney W. Grimes bad:
456df8bae1dSRodney W. Grimes 	m_freem(m);
457df8bae1dSRodney W. Grimes 	return (error);
458df8bae1dSRodney W. Grimes }
459df8bae1dSRodney W. Grimes 
460df8bae1dSRodney W. Grimes /*
461df8bae1dSRodney W. Grimes  * Attach file to the bpf interface, i.e. make d listen on bp.
462df8bae1dSRodney W. Grimes  */
463df8bae1dSRodney W. Grimes static void
46419ba8395SChristian S.J. Peron bpf_attachd(struct bpf_d *d, struct bpf_if *bp)
465df8bae1dSRodney W. Grimes {
466df8bae1dSRodney W. Grimes 	/*
467df8bae1dSRodney W. Grimes 	 * Point d at bp, and add d to the interface's list of listeners.
468df8bae1dSRodney W. Grimes 	 * Finally, point the driver's bpf cookie at the interface so
469df8bae1dSRodney W. Grimes 	 * it will divert packets to bpf.
470df8bae1dSRodney W. Grimes 	 */
471e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
472df8bae1dSRodney W. Grimes 	d->bd_bif = bp;
4734a3feeaaSRobert Watson 	LIST_INSERT_HEAD(&bp->bif_dlist, d, bd_next);
474df8bae1dSRodney W. Grimes 
47569f7644bSChristian S.J. Peron 	bpf_bpfd_cnt++;
476e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
477df8bae1dSRodney W. Grimes }
478df8bae1dSRodney W. Grimes 
479df8bae1dSRodney W. Grimes /*
480df8bae1dSRodney W. Grimes  * Detach a file from its interface.
481df8bae1dSRodney W. Grimes  */
482df8bae1dSRodney W. Grimes static void
48319ba8395SChristian S.J. Peron bpf_detachd(struct bpf_d *d)
484df8bae1dSRodney W. Grimes {
4856e891d64SPoul-Henning Kamp 	int error;
486df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
48746448b5aSRobert Watson 	struct ifnet *ifp;
488df8bae1dSRodney W. Grimes 
489df8bae1dSRodney W. Grimes 	bp = d->bd_bif;
49046448b5aSRobert Watson 	BPFIF_LOCK(bp);
49146448b5aSRobert Watson 	BPFD_LOCK(d);
49246448b5aSRobert Watson 	ifp = d->bd_bif->bif_ifp;
49346448b5aSRobert Watson 
49446448b5aSRobert Watson 	/*
49546448b5aSRobert Watson 	 * Remove d from the interface's descriptor list.
49646448b5aSRobert Watson 	 */
49746448b5aSRobert Watson 	LIST_REMOVE(d, bd_next);
49846448b5aSRobert Watson 
49969f7644bSChristian S.J. Peron 	bpf_bpfd_cnt--;
500572bde2aSRobert Watson 	d->bd_bif = NULL;
50146448b5aSRobert Watson 	BPFD_UNLOCK(d);
50246448b5aSRobert Watson 	BPFIF_UNLOCK(bp);
50346448b5aSRobert Watson 
504df8bae1dSRodney W. Grimes 	/*
505df8bae1dSRodney W. Grimes 	 * Check if this descriptor had requested promiscuous mode.
506df8bae1dSRodney W. Grimes 	 * If so, turn it off.
507df8bae1dSRodney W. Grimes 	 */
508df8bae1dSRodney W. Grimes 	if (d->bd_promisc) {
509df8bae1dSRodney W. Grimes 		d->bd_promisc = 0;
51046448b5aSRobert Watson 		error = ifpromisc(ifp, 0);
5116e891d64SPoul-Henning Kamp 		if (error != 0 && error != ENXIO) {
512df8bae1dSRodney W. Grimes 			/*
5136e891d64SPoul-Henning Kamp 			 * ENXIO can happen if a pccard is unplugged
514df8bae1dSRodney W. Grimes 			 * Something is really wrong if we were able to put
515df8bae1dSRodney W. Grimes 			 * the driver into promiscuous mode, but can't
516df8bae1dSRodney W. Grimes 			 * take it out.
517df8bae1dSRodney W. Grimes 			 */
5188eab61f3SSam Leffler 			if_printf(bp->bif_ifp,
5198eab61f3SSam Leffler 				"bpf_detach: ifpromisc failed (%d)\n", error);
5206e891d64SPoul-Henning Kamp 		}
521df8bae1dSRodney W. Grimes 	}
522df8bae1dSRodney W. Grimes }
523df8bae1dSRodney W. Grimes 
524df8bae1dSRodney W. Grimes /*
525df8bae1dSRodney W. Grimes  * Open ethernet device.  Returns ENXIO for illegal minor device number,
526df8bae1dSRodney W. Grimes  * EBUSY if file is open by another process.
527df8bae1dSRodney W. Grimes  */
528df8bae1dSRodney W. Grimes /* ARGSUSED */
52987f6c662SJulian Elischer static	int
53019ba8395SChristian S.J. Peron bpfopen(struct cdev *dev, int flags, int fmt, struct thread *td)
531df8bae1dSRodney W. Grimes {
532e7bb21b3SJonathan Lemon 	struct bpf_d *d;
533df8bae1dSRodney W. Grimes 
534e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
535bd3a5320SPoul-Henning Kamp 	d = dev->si_drv1;
536df8bae1dSRodney W. Grimes 	/*
537df8bae1dSRodney W. Grimes 	 * Each minor can be opened by only one process.  If the requested
538df8bae1dSRodney W. Grimes 	 * minor is in use, return EBUSY.
539df8bae1dSRodney W. Grimes 	 */
540d17d8184SRobert Watson 	if (d != NULL) {
541e7bb21b3SJonathan Lemon 		mtx_unlock(&bpf_mtx);
542df8bae1dSRodney W. Grimes 		return (EBUSY);
543e7bb21b3SJonathan Lemon 	}
544e7bb21b3SJonathan Lemon 	dev->si_drv1 = (struct bpf_d *)~0;	/* mark device in use */
545e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
546e7bb21b3SJonathan Lemon 
547d1d74c28SJohn Baldwin 	if ((dev->si_flags & SI_NAMED) == 0)
548b0d17ba6SPoul-Henning Kamp 		make_dev(&bpf_cdevsw, minor(dev), UID_ROOT, GID_WHEEL, 0600,
549b0d17ba6SPoul-Henning Kamp 		    "bpf%d", dev2unit(dev));
550a163d034SWarner Losh 	MALLOC(d, struct bpf_d *, sizeof(*d), M_BPF, M_WAITOK | M_ZERO);
551bd3a5320SPoul-Henning Kamp 	dev->si_drv1 = d;
5524d621040SChristian S.J. Peron 
5534d621040SChristian S.J. Peron 	/*
5544d621040SChristian S.J. Peron 	 * For historical reasons, perform a one-time initialization call to
5554d621040SChristian S.J. Peron 	 * the buffer routines, even though we're not yet committed to a
5564d621040SChristian S.J. Peron 	 * particular buffer method.
5574d621040SChristian S.J. Peron 	 */
5584d621040SChristian S.J. Peron 	bpf_buffer_init(d);
5594d621040SChristian S.J. Peron 	d->bd_bufmode = BPF_BUFMODE_BUFFER;
56000a83887SPaul Traina 	d->bd_sig = SIGIO;
561560a54e1SJung-uk Kim 	d->bd_direction = BPF_D_INOUT;
56269f7644bSChristian S.J. Peron 	d->bd_pid = td->td_proc->p_pid;
56382f4445dSRobert Watson #ifdef MAC
56430d239bcSRobert Watson 	mac_bpfdesc_init(d);
56530d239bcSRobert Watson 	mac_bpfdesc_create(td->td_ucred, d);
56682f4445dSRobert Watson #endif
5676008862bSJohn Baldwin 	mtx_init(&d->bd_mtx, devtoname(dev), "bpf cdev lock", MTX_DEF);
568c6b28997SRobert Watson 	callout_init(&d->bd_callout, CALLOUT_MPSAFE);
569571dcd15SSuleiman Souhlal 	knlist_init(&d->bd_sel.si_note, &d->bd_mtx, NULL, NULL, NULL);
570df8bae1dSRodney W. Grimes 
571df8bae1dSRodney W. Grimes 	return (0);
572df8bae1dSRodney W. Grimes }
573df8bae1dSRodney W. Grimes 
574df8bae1dSRodney W. Grimes /*
575df8bae1dSRodney W. Grimes  * Close the descriptor by detaching it from its interface,
576df8bae1dSRodney W. Grimes  * deallocating its buffers, and marking it free.
577df8bae1dSRodney W. Grimes  */
578df8bae1dSRodney W. Grimes /* ARGSUSED */
57987f6c662SJulian Elischer static	int
58019ba8395SChristian S.J. Peron bpfclose(struct cdev *dev, int flags, int fmt, struct thread *td)
581df8bae1dSRodney W. Grimes {
582e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
583df8bae1dSRodney W. Grimes 
58481bda851SJohn Polstra 	BPFD_LOCK(d);
58581bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
58681bda851SJohn Polstra 		callout_stop(&d->bd_callout);
58781bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
58881bda851SJohn Polstra 	BPFD_UNLOCK(d);
589e649887bSAlfred Perlstein 	funsetown(&d->bd_sigio);
590e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
591df8bae1dSRodney W. Grimes 	if (d->bd_bif)
592df8bae1dSRodney W. Grimes 		bpf_detachd(d);
593e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
5944549709fSBrian Feldman 	selwakeuppri(&d->bd_sel, PRINET);
59582f4445dSRobert Watson #ifdef MAC
59630d239bcSRobert Watson 	mac_bpfdesc_destroy(d);
59782f4445dSRobert Watson #endif /* MAC */
598ad3b9257SJohn-Mark Gurney 	knlist_destroy(&d->bd_sel.si_note);
599df8bae1dSRodney W. Grimes 	bpf_freed(d);
600d17d8184SRobert Watson 	dev->si_drv1 = NULL;
601d722be54SLuigi Rizzo 	free(d, M_BPF);
602df8bae1dSRodney W. Grimes 
603df8bae1dSRodney W. Grimes 	return (0);
604df8bae1dSRodney W. Grimes }
605df8bae1dSRodney W. Grimes 
606df8bae1dSRodney W. Grimes /*
607df8bae1dSRodney W. Grimes  *  bpfread - read next chunk of packets from buffers
608df8bae1dSRodney W. Grimes  */
60987f6c662SJulian Elischer static	int
61019ba8395SChristian S.J. Peron bpfread(struct cdev *dev, struct uio *uio, int ioflag)
611df8bae1dSRodney W. Grimes {
612e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
61381bda851SJohn Polstra 	int timed_out;
614df8bae1dSRodney W. Grimes 	int error;
615df8bae1dSRodney W. Grimes 
616df8bae1dSRodney W. Grimes 	/*
617df8bae1dSRodney W. Grimes 	 * Restrict application to use a buffer the same size as
618df8bae1dSRodney W. Grimes 	 * as kernel buffers.
619df8bae1dSRodney W. Grimes 	 */
620df8bae1dSRodney W. Grimes 	if (uio->uio_resid != d->bd_bufsize)
621df8bae1dSRodney W. Grimes 		return (EINVAL);
622df8bae1dSRodney W. Grimes 
623e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
62450ed6e07SChristian S.J. Peron 	d->bd_pid = curthread->td_proc->p_pid;
6254d621040SChristian S.J. Peron 	if (d->bd_bufmode != BPF_BUFMODE_BUFFER) {
6264d621040SChristian S.J. Peron 		BPFD_UNLOCK(d);
6274d621040SChristian S.J. Peron 		return (EOPNOTSUPP);
6284d621040SChristian S.J. Peron 	}
62981bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
63081bda851SJohn Polstra 		callout_stop(&d->bd_callout);
63181bda851SJohn Polstra 	timed_out = (d->bd_state == BPF_TIMED_OUT);
63281bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
633df8bae1dSRodney W. Grimes 	/*
634df8bae1dSRodney W. Grimes 	 * If the hold buffer is empty, then do a timed sleep, which
635df8bae1dSRodney W. Grimes 	 * ends when the timeout expires or when enough packets
636df8bae1dSRodney W. Grimes 	 * have arrived to fill the store buffer.
637df8bae1dSRodney W. Grimes 	 */
638572bde2aSRobert Watson 	while (d->bd_hbuf == NULL) {
63981bda851SJohn Polstra 		if ((d->bd_immediate || timed_out) && d->bd_slen != 0) {
640df8bae1dSRodney W. Grimes 			/*
641df8bae1dSRodney W. Grimes 			 * A packet(s) either arrived since the previous
642df8bae1dSRodney W. Grimes 			 * read or arrived while we were asleep.
643df8bae1dSRodney W. Grimes 			 * Rotate the buffers and return what's here.
644df8bae1dSRodney W. Grimes 			 */
645df8bae1dSRodney W. Grimes 			ROTATE_BUFFERS(d);
646df8bae1dSRodney W. Grimes 			break;
647df8bae1dSRodney W. Grimes 		}
648de5d9935SRobert Watson 
649de5d9935SRobert Watson 		/*
650de5d9935SRobert Watson 		 * No data is available, check to see if the bpf device
651de5d9935SRobert Watson 		 * is still pointed at a real interface.  If not, return
652de5d9935SRobert Watson 		 * ENXIO so that the userland process knows to rebind
653de5d9935SRobert Watson 		 * it before using it again.
654de5d9935SRobert Watson 		 */
655de5d9935SRobert Watson 		if (d->bd_bif == NULL) {
656e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
657de5d9935SRobert Watson 			return (ENXIO);
658de5d9935SRobert Watson 		}
659de5d9935SRobert Watson 
660e76eee55SPoul-Henning Kamp 		if (ioflag & O_NONBLOCK) {
661e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
662fba3cfdeSJohn Polstra 			return (EWOULDBLOCK);
663fba3cfdeSJohn Polstra 		}
664521f364bSDag-Erling Smørgrav 		error = msleep(d, &d->bd_mtx, PRINET|PCATCH,
665e7bb21b3SJonathan Lemon 		     "bpf", d->bd_rtout);
666df8bae1dSRodney W. Grimes 		if (error == EINTR || error == ERESTART) {
667e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
668df8bae1dSRodney W. Grimes 			return (error);
669df8bae1dSRodney W. Grimes 		}
670df8bae1dSRodney W. Grimes 		if (error == EWOULDBLOCK) {
671df8bae1dSRodney W. Grimes 			/*
672df8bae1dSRodney W. Grimes 			 * On a timeout, return what's in the buffer,
673df8bae1dSRodney W. Grimes 			 * which may be nothing.  If there is something
674df8bae1dSRodney W. Grimes 			 * in the store buffer, we can rotate the buffers.
675df8bae1dSRodney W. Grimes 			 */
676df8bae1dSRodney W. Grimes 			if (d->bd_hbuf)
677df8bae1dSRodney W. Grimes 				/*
678df8bae1dSRodney W. Grimes 				 * We filled up the buffer in between
679df8bae1dSRodney W. Grimes 				 * getting the timeout and arriving
680df8bae1dSRodney W. Grimes 				 * here, so we don't need to rotate.
681df8bae1dSRodney W. Grimes 				 */
682df8bae1dSRodney W. Grimes 				break;
683df8bae1dSRodney W. Grimes 
684df8bae1dSRodney W. Grimes 			if (d->bd_slen == 0) {
685e7bb21b3SJonathan Lemon 				BPFD_UNLOCK(d);
686df8bae1dSRodney W. Grimes 				return (0);
687df8bae1dSRodney W. Grimes 			}
688df8bae1dSRodney W. Grimes 			ROTATE_BUFFERS(d);
689df8bae1dSRodney W. Grimes 			break;
690df8bae1dSRodney W. Grimes 		}
691df8bae1dSRodney W. Grimes 	}
692df8bae1dSRodney W. Grimes 	/*
693df8bae1dSRodney W. Grimes 	 * At this point, we know we have something in the hold slot.
694df8bae1dSRodney W. Grimes 	 */
695e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
696df8bae1dSRodney W. Grimes 
697df8bae1dSRodney W. Grimes 	/*
698df8bae1dSRodney W. Grimes 	 * Move data from hold buffer into user space.
699df8bae1dSRodney W. Grimes 	 * We know the entire buffer is transferred since
700df8bae1dSRodney W. Grimes 	 * we checked above that the read buffer is bpf_bufsize bytes.
70131b32e6dSRobert Watson 	 *
70231b32e6dSRobert Watson 	 * XXXRW: More synchronization needed here: what if a second thread
70331b32e6dSRobert Watson 	 * issues a read on the same fd at the same time?  Don't want this
70431b32e6dSRobert Watson 	 * getting invalidated.
705df8bae1dSRodney W. Grimes 	 */
7064d621040SChristian S.J. Peron 	error = bpf_uiomove(d, d->bd_hbuf, d->bd_hlen, uio);
707df8bae1dSRodney W. Grimes 
708e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
709df8bae1dSRodney W. Grimes 	d->bd_fbuf = d->bd_hbuf;
710572bde2aSRobert Watson 	d->bd_hbuf = NULL;
711df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
712e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
713df8bae1dSRodney W. Grimes 
714df8bae1dSRodney W. Grimes 	return (error);
715df8bae1dSRodney W. Grimes }
716df8bae1dSRodney W. Grimes 
717df8bae1dSRodney W. Grimes /*
718df8bae1dSRodney W. Grimes  * If there are processes sleeping on this descriptor, wake them up.
719df8bae1dSRodney W. Grimes  */
720e7bb21b3SJonathan Lemon static __inline void
72119ba8395SChristian S.J. Peron bpf_wakeup(struct bpf_d *d)
722df8bae1dSRodney W. Grimes {
723a3272e3cSChristian S.J. Peron 
724a3272e3cSChristian S.J. Peron 	BPFD_LOCK_ASSERT(d);
72581bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING) {
72681bda851SJohn Polstra 		callout_stop(&d->bd_callout);
72781bda851SJohn Polstra 		d->bd_state = BPF_IDLE;
72881bda851SJohn Polstra 	}
729521f364bSDag-Erling Smørgrav 	wakeup(d);
730831d27a9SDon Lewis 	if (d->bd_async && d->bd_sig && d->bd_sigio)
731f1320723SAlfred Perlstein 		pgsigio(&d->bd_sigio, d->bd_sig, 0);
73200a83887SPaul Traina 
733512824f8SSeigo Tanimura 	selwakeuppri(&d->bd_sel, PRINET);
734ad3b9257SJohn-Mark Gurney 	KNOTE_LOCKED(&d->bd_sel.si_note, 0);
735df8bae1dSRodney W. Grimes }
736df8bae1dSRodney W. Grimes 
73781bda851SJohn Polstra static void
73819ba8395SChristian S.J. Peron bpf_timed_out(void *arg)
73981bda851SJohn Polstra {
74081bda851SJohn Polstra 	struct bpf_d *d = (struct bpf_d *)arg;
74181bda851SJohn Polstra 
74281bda851SJohn Polstra 	BPFD_LOCK(d);
74381bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING) {
74481bda851SJohn Polstra 		d->bd_state = BPF_TIMED_OUT;
74581bda851SJohn Polstra 		if (d->bd_slen != 0)
74681bda851SJohn Polstra 			bpf_wakeup(d);
74781bda851SJohn Polstra 	}
74881bda851SJohn Polstra 	BPFD_UNLOCK(d);
74981bda851SJohn Polstra }
75081bda851SJohn Polstra 
75187f6c662SJulian Elischer static int
7524d621040SChristian S.J. Peron bpf_ready(struct bpf_d *d)
7534d621040SChristian S.J. Peron {
7544d621040SChristian S.J. Peron 
7554d621040SChristian S.J. Peron 	BPFD_LOCK_ASSERT(d);
7564d621040SChristian S.J. Peron 
7574d621040SChristian S.J. Peron 	if (!bpf_canfreebuf(d) && d->bd_hlen != 0)
7584d621040SChristian S.J. Peron 		return (1);
7594d621040SChristian S.J. Peron 	if ((d->bd_immediate || d->bd_state == BPF_TIMED_OUT) &&
7604d621040SChristian S.J. Peron 	    d->bd_slen != 0)
7614d621040SChristian S.J. Peron 		return (1);
7624d621040SChristian S.J. Peron 	return (0);
7634d621040SChristian S.J. Peron }
7644d621040SChristian S.J. Peron 
7654d621040SChristian S.J. Peron static int
76619ba8395SChristian S.J. Peron bpfwrite(struct cdev *dev, struct uio *uio, int ioflag)
767df8bae1dSRodney W. Grimes {
768e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
769df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
770560a54e1SJung-uk Kim 	struct mbuf *m, *mc;
7718240bf1eSRobert Watson 	struct sockaddr dst;
772560a54e1SJung-uk Kim 	int error, hlen;
773df8bae1dSRodney W. Grimes 
77450ed6e07SChristian S.J. Peron 	d->bd_pid = curthread->td_proc->p_pid;
7754d621040SChristian S.J. Peron 	d->bd_wcount++;
7764d621040SChristian S.J. Peron 	if (d->bd_bif == NULL) {
7774d621040SChristian S.J. Peron 		d->bd_wdcount++;
778df8bae1dSRodney W. Grimes 		return (ENXIO);
7794d621040SChristian S.J. Peron 	}
780df8bae1dSRodney W. Grimes 
781df8bae1dSRodney W. Grimes 	ifp = d->bd_bif->bif_ifp;
782df8bae1dSRodney W. Grimes 
7834d621040SChristian S.J. Peron 	if ((ifp->if_flags & IFF_UP) == 0) {
7844d621040SChristian S.J. Peron 		d->bd_wdcount++;
7853518d220SSam Leffler 		return (ENETDOWN);
7864d621040SChristian S.J. Peron 	}
7873518d220SSam Leffler 
7884d621040SChristian S.J. Peron 	if (uio->uio_resid == 0) {
7894d621040SChristian S.J. Peron 		d->bd_wdcount++;
790df8bae1dSRodney W. Grimes 		return (0);
7914d621040SChristian S.J. Peron 	}
792df8bae1dSRodney W. Grimes 
7938240bf1eSRobert Watson 	bzero(&dst, sizeof(dst));
794d83e603aSChristian S.J. Peron 	m = NULL;
795d83e603aSChristian S.J. Peron 	hlen = 0;
796cb44b6dfSAndrew Thompson 	error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, ifp,
797560a54e1SJung-uk Kim 	    &m, &dst, &hlen, d->bd_wfilter);
7984d621040SChristian S.J. Peron 	if (error) {
7994d621040SChristian S.J. Peron 		d->bd_wdcount++;
800df8bae1dSRodney W. Grimes 		return (error);
8014d621040SChristian S.J. Peron 	}
8024d621040SChristian S.J. Peron 	d->bd_wfcount++;
803114ae644SMike Smith 	if (d->bd_hdrcmplt)
804114ae644SMike Smith 		dst.sa_family = pseudo_AF_HDRCMPLT;
805114ae644SMike Smith 
806560a54e1SJung-uk Kim 	if (d->bd_feedback) {
807560a54e1SJung-uk Kim 		mc = m_dup(m, M_DONTWAIT);
808560a54e1SJung-uk Kim 		if (mc != NULL)
809560a54e1SJung-uk Kim 			mc->m_pkthdr.rcvif = ifp;
810560a54e1SJung-uk Kim 		/* XXX Do not return the same packet twice. */
811560a54e1SJung-uk Kim 		if (d->bd_direction == BPF_D_INOUT)
812560a54e1SJung-uk Kim 			m->m_flags |= M_SKIP_BPF;
813560a54e1SJung-uk Kim 	} else
814560a54e1SJung-uk Kim 		mc = NULL;
815560a54e1SJung-uk Kim 
816560a54e1SJung-uk Kim 	m->m_pkthdr.len -= hlen;
817560a54e1SJung-uk Kim 	m->m_len -= hlen;
818560a54e1SJung-uk Kim 	m->m_data += hlen;	/* XXX */
819560a54e1SJung-uk Kim 
82082f4445dSRobert Watson #ifdef MAC
821f747d2ddSRobert Watson 	BPFD_LOCK(d);
82230d239bcSRobert Watson 	mac_bpfdesc_create_mbuf(d, m);
823560a54e1SJung-uk Kim 	if (mc != NULL)
82430d239bcSRobert Watson 		mac_bpfdesc_create_mbuf(d, mc);
825f747d2ddSRobert Watson 	BPFD_UNLOCK(d);
82682f4445dSRobert Watson #endif
827560a54e1SJung-uk Kim 
828572bde2aSRobert Watson 	error = (*ifp->if_output)(ifp, m, &dst, NULL);
8294d621040SChristian S.J. Peron 	if (error)
8304d621040SChristian S.J. Peron 		d->bd_wdcount++;
831560a54e1SJung-uk Kim 
832560a54e1SJung-uk Kim 	if (mc != NULL) {
8330bf686c1SRobert Watson 		if (error == 0)
834560a54e1SJung-uk Kim 			(*ifp->if_input)(ifp, mc);
8350bf686c1SRobert Watson 		else
836560a54e1SJung-uk Kim 			m_freem(mc);
837560a54e1SJung-uk Kim 	}
838560a54e1SJung-uk Kim 
839df8bae1dSRodney W. Grimes 	return (error);
840df8bae1dSRodney W. Grimes }
841df8bae1dSRodney W. Grimes 
842df8bae1dSRodney W. Grimes /*
843df8bae1dSRodney W. Grimes  * Reset a descriptor by flushing its packet buffer and clearing the
844e7bb21b3SJonathan Lemon  * receive and drop counts.
845df8bae1dSRodney W. Grimes  */
846df8bae1dSRodney W. Grimes static void
84719ba8395SChristian S.J. Peron reset_d(struct bpf_d *d)
848df8bae1dSRodney W. Grimes {
849e7bb21b3SJonathan Lemon 
850e7bb21b3SJonathan Lemon 	mtx_assert(&d->bd_mtx, MA_OWNED);
851df8bae1dSRodney W. Grimes 	if (d->bd_hbuf) {
852df8bae1dSRodney W. Grimes 		/* Free the hold buffer. */
853df8bae1dSRodney W. Grimes 		d->bd_fbuf = d->bd_hbuf;
854572bde2aSRobert Watson 		d->bd_hbuf = NULL;
855df8bae1dSRodney W. Grimes 	}
856df8bae1dSRodney W. Grimes 	d->bd_slen = 0;
857df8bae1dSRodney W. Grimes 	d->bd_hlen = 0;
858df8bae1dSRodney W. Grimes 	d->bd_rcount = 0;
859df8bae1dSRodney W. Grimes 	d->bd_dcount = 0;
86069f7644bSChristian S.J. Peron 	d->bd_fcount = 0;
8614d621040SChristian S.J. Peron 	d->bd_wcount = 0;
8624d621040SChristian S.J. Peron 	d->bd_wfcount = 0;
8634d621040SChristian S.J. Peron 	d->bd_wdcount = 0;
8644d621040SChristian S.J. Peron 	d->bd_zcopy = 0;
865df8bae1dSRodney W. Grimes }
866df8bae1dSRodney W. Grimes 
867df8bae1dSRodney W. Grimes /*
868df8bae1dSRodney W. Grimes  *  FIONREAD		Check for read packet available.
869df8bae1dSRodney W. Grimes  *  SIOCGIFADDR		Get interface address - convenient hook to driver.
870df8bae1dSRodney W. Grimes  *  BIOCGBLEN		Get buffer len [for read()].
871df8bae1dSRodney W. Grimes  *  BIOCSETF		Set ethernet read filter.
87293e39f0bSChristian S.J. Peron  *  BIOCSETWF		Set ethernet write filter.
873df8bae1dSRodney W. Grimes  *  BIOCFLUSH		Flush read packet buffer.
874df8bae1dSRodney W. Grimes  *  BIOCPROMISC		Put interface into promiscuous mode.
875df8bae1dSRodney W. Grimes  *  BIOCGDLT		Get link layer type.
876df8bae1dSRodney W. Grimes  *  BIOCGETIF		Get interface name.
877df8bae1dSRodney W. Grimes  *  BIOCSETIF		Set interface.
878df8bae1dSRodney W. Grimes  *  BIOCSRTIMEOUT	Set read timeout.
879df8bae1dSRodney W. Grimes  *  BIOCGRTIMEOUT	Get read timeout.
880df8bae1dSRodney W. Grimes  *  BIOCGSTATS		Get packet stats.
881df8bae1dSRodney W. Grimes  *  BIOCIMMEDIATE	Set immediate mode.
882df8bae1dSRodney W. Grimes  *  BIOCVERSION		Get filter language version.
883114ae644SMike Smith  *  BIOCGHDRCMPLT	Get "header already complete" flag
884114ae644SMike Smith  *  BIOCSHDRCMPLT	Set "header already complete" flag
885560a54e1SJung-uk Kim  *  BIOCGDIRECTION	Get packet direction flag
886560a54e1SJung-uk Kim  *  BIOCSDIRECTION	Set packet direction flag
88793e39f0bSChristian S.J. Peron  *  BIOCLOCK		Set "locked" flag
888560a54e1SJung-uk Kim  *  BIOCFEEDBACK	Set packet feedback mode.
8894d621040SChristian S.J. Peron  *  BIOCSETZBUF		Set current zero-copy buffer locations.
8904d621040SChristian S.J. Peron  *  BIOCGETZMAX		Get maximum zero-copy buffer size.
8914d621040SChristian S.J. Peron  *  BIOCROTZBUF		Force rotation of zero-copy buffer
8924d621040SChristian S.J. Peron  *  BIOCSETBUFMODE	Set buffer mode.
8934d621040SChristian S.J. Peron  *  BIOCGETBUFMODE	Get current buffer mode.
894df8bae1dSRodney W. Grimes  */
895df8bae1dSRodney W. Grimes /* ARGSUSED */
89687f6c662SJulian Elischer static	int
89719ba8395SChristian S.J. Peron bpfioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags,
89819ba8395SChristian S.J. Peron     struct thread *td)
899df8bae1dSRodney W. Grimes {
900e7bb21b3SJonathan Lemon 	struct bpf_d *d = dev->si_drv1;
901e7bb21b3SJonathan Lemon 	int error = 0;
902df8bae1dSRodney W. Grimes 
903b75a24a0SChristian S.J. Peron 	/*
904b75a24a0SChristian S.J. Peron 	 * Refresh PID associated with this descriptor.
905b75a24a0SChristian S.J. Peron 	 */
90681bda851SJohn Polstra 	BPFD_LOCK(d);
907cb1d4f92SChristian S.J. Peron 	d->bd_pid = td->td_proc->p_pid;
90881bda851SJohn Polstra 	if (d->bd_state == BPF_WAITING)
90981bda851SJohn Polstra 		callout_stop(&d->bd_callout);
91081bda851SJohn Polstra 	d->bd_state = BPF_IDLE;
91181bda851SJohn Polstra 	BPFD_UNLOCK(d);
91281bda851SJohn Polstra 
91393e39f0bSChristian S.J. Peron 	if (d->bd_locked == 1) {
91493e39f0bSChristian S.J. Peron 		switch (cmd) {
91593e39f0bSChristian S.J. Peron 		case BIOCGBLEN:
91693e39f0bSChristian S.J. Peron 		case BIOCFLUSH:
91793e39f0bSChristian S.J. Peron 		case BIOCGDLT:
91893e39f0bSChristian S.J. Peron 		case BIOCGDLTLIST:
91993e39f0bSChristian S.J. Peron 		case BIOCGETIF:
92093e39f0bSChristian S.J. Peron 		case BIOCGRTIMEOUT:
92193e39f0bSChristian S.J. Peron 		case BIOCGSTATS:
92293e39f0bSChristian S.J. Peron 		case BIOCVERSION:
92393e39f0bSChristian S.J. Peron 		case BIOCGRSIG:
92493e39f0bSChristian S.J. Peron 		case BIOCGHDRCMPLT:
925560a54e1SJung-uk Kim 		case BIOCFEEDBACK:
92693e39f0bSChristian S.J. Peron 		case FIONREAD:
92793e39f0bSChristian S.J. Peron 		case BIOCLOCK:
92893e39f0bSChristian S.J. Peron 		case BIOCSRTIMEOUT:
92993e39f0bSChristian S.J. Peron 		case BIOCIMMEDIATE:
93093e39f0bSChristian S.J. Peron 		case TIOCGPGRP:
9314d621040SChristian S.J. Peron 		case BIOCROTZBUF:
93293e39f0bSChristian S.J. Peron 			break;
93393e39f0bSChristian S.J. Peron 		default:
93493e39f0bSChristian S.J. Peron 			return (EPERM);
93593e39f0bSChristian S.J. Peron 		}
93693e39f0bSChristian S.J. Peron 	}
937df8bae1dSRodney W. Grimes 	switch (cmd) {
938df8bae1dSRodney W. Grimes 
939df8bae1dSRodney W. Grimes 	default:
940df8bae1dSRodney W. Grimes 		error = EINVAL;
941df8bae1dSRodney W. Grimes 		break;
942df8bae1dSRodney W. Grimes 
943df8bae1dSRodney W. Grimes 	/*
944df8bae1dSRodney W. Grimes 	 * Check for read packet available.
945df8bae1dSRodney W. Grimes 	 */
946df8bae1dSRodney W. Grimes 	case FIONREAD:
947df8bae1dSRodney W. Grimes 		{
948df8bae1dSRodney W. Grimes 			int n;
949df8bae1dSRodney W. Grimes 
950e7bb21b3SJonathan Lemon 			BPFD_LOCK(d);
951df8bae1dSRodney W. Grimes 			n = d->bd_slen;
952df8bae1dSRodney W. Grimes 			if (d->bd_hbuf)
953df8bae1dSRodney W. Grimes 				n += d->bd_hlen;
954e7bb21b3SJonathan Lemon 			BPFD_UNLOCK(d);
955df8bae1dSRodney W. Grimes 
956df8bae1dSRodney W. Grimes 			*(int *)addr = n;
957df8bae1dSRodney W. Grimes 			break;
958df8bae1dSRodney W. Grimes 		}
959df8bae1dSRodney W. Grimes 
960df8bae1dSRodney W. Grimes 	case SIOCGIFADDR:
961df8bae1dSRodney W. Grimes 		{
962df8bae1dSRodney W. Grimes 			struct ifnet *ifp;
963df8bae1dSRodney W. Grimes 
964572bde2aSRobert Watson 			if (d->bd_bif == NULL)
965df8bae1dSRodney W. Grimes 				error = EINVAL;
966df8bae1dSRodney W. Grimes 			else {
967df8bae1dSRodney W. Grimes 				ifp = d->bd_bif->bif_ifp;
968df8bae1dSRodney W. Grimes 				error = (*ifp->if_ioctl)(ifp, cmd, addr);
969df8bae1dSRodney W. Grimes 			}
970df8bae1dSRodney W. Grimes 			break;
971df8bae1dSRodney W. Grimes 		}
972df8bae1dSRodney W. Grimes 
973df8bae1dSRodney W. Grimes 	/*
974df8bae1dSRodney W. Grimes 	 * Get buffer len [for read()].
975df8bae1dSRodney W. Grimes 	 */
976df8bae1dSRodney W. Grimes 	case BIOCGBLEN:
977df8bae1dSRodney W. Grimes 		*(u_int *)addr = d->bd_bufsize;
978df8bae1dSRodney W. Grimes 		break;
979df8bae1dSRodney W. Grimes 
980df8bae1dSRodney W. Grimes 	/*
981df8bae1dSRodney W. Grimes 	 * Set buffer length.
982df8bae1dSRodney W. Grimes 	 */
983df8bae1dSRodney W. Grimes 	case BIOCSBLEN:
9844d621040SChristian S.J. Peron 		error = bpf_ioctl_sblen(d, (u_int *)addr);
985df8bae1dSRodney W. Grimes 		break;
986df8bae1dSRodney W. Grimes 
987df8bae1dSRodney W. Grimes 	/*
988df8bae1dSRodney W. Grimes 	 * Set link layer read filter.
989df8bae1dSRodney W. Grimes 	 */
990df8bae1dSRodney W. Grimes 	case BIOCSETF:
99193e39f0bSChristian S.J. Peron 	case BIOCSETWF:
99293e39f0bSChristian S.J. Peron 		error = bpf_setf(d, (struct bpf_program *)addr, cmd);
993df8bae1dSRodney W. Grimes 		break;
994df8bae1dSRodney W. Grimes 
995df8bae1dSRodney W. Grimes 	/*
996df8bae1dSRodney W. Grimes 	 * Flush read packet buffer.
997df8bae1dSRodney W. Grimes 	 */
998df8bae1dSRodney W. Grimes 	case BIOCFLUSH:
999e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1000df8bae1dSRodney W. Grimes 		reset_d(d);
1001e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1002df8bae1dSRodney W. Grimes 		break;
1003df8bae1dSRodney W. Grimes 
1004df8bae1dSRodney W. Grimes 	/*
1005df8bae1dSRodney W. Grimes 	 * Put interface into promiscuous mode.
1006df8bae1dSRodney W. Grimes 	 */
1007df8bae1dSRodney W. Grimes 	case BIOCPROMISC:
1008572bde2aSRobert Watson 		if (d->bd_bif == NULL) {
1009df8bae1dSRodney W. Grimes 			/*
1010df8bae1dSRodney W. Grimes 			 * No interface attached yet.
1011df8bae1dSRodney W. Grimes 			 */
1012df8bae1dSRodney W. Grimes 			error = EINVAL;
1013df8bae1dSRodney W. Grimes 			break;
1014df8bae1dSRodney W. Grimes 		}
1015df8bae1dSRodney W. Grimes 		if (d->bd_promisc == 0) {
1016df8bae1dSRodney W. Grimes 			error = ifpromisc(d->bd_bif->bif_ifp, 1);
1017df8bae1dSRodney W. Grimes 			if (error == 0)
1018df8bae1dSRodney W. Grimes 				d->bd_promisc = 1;
1019df8bae1dSRodney W. Grimes 		}
1020df8bae1dSRodney W. Grimes 		break;
1021df8bae1dSRodney W. Grimes 
1022df8bae1dSRodney W. Grimes 	/*
10238eab61f3SSam Leffler 	 * Get current data link type.
1024df8bae1dSRodney W. Grimes 	 */
1025df8bae1dSRodney W. Grimes 	case BIOCGDLT:
1026572bde2aSRobert Watson 		if (d->bd_bif == NULL)
1027df8bae1dSRodney W. Grimes 			error = EINVAL;
1028df8bae1dSRodney W. Grimes 		else
1029df8bae1dSRodney W. Grimes 			*(u_int *)addr = d->bd_bif->bif_dlt;
1030df8bae1dSRodney W. Grimes 		break;
1031df8bae1dSRodney W. Grimes 
1032df8bae1dSRodney W. Grimes 	/*
10338eab61f3SSam Leffler 	 * Get a list of supported data link types.
10348eab61f3SSam Leffler 	 */
10358eab61f3SSam Leffler 	case BIOCGDLTLIST:
1036572bde2aSRobert Watson 		if (d->bd_bif == NULL)
10378eab61f3SSam Leffler 			error = EINVAL;
10388eab61f3SSam Leffler 		else
10398eab61f3SSam Leffler 			error = bpf_getdltlist(d, (struct bpf_dltlist *)addr);
10408eab61f3SSam Leffler 		break;
10418eab61f3SSam Leffler 
10428eab61f3SSam Leffler 	/*
10438eab61f3SSam Leffler 	 * Set data link type.
10448eab61f3SSam Leffler 	 */
10458eab61f3SSam Leffler 	case BIOCSDLT:
1046572bde2aSRobert Watson 		if (d->bd_bif == NULL)
10478eab61f3SSam Leffler 			error = EINVAL;
10488eab61f3SSam Leffler 		else
10498eab61f3SSam Leffler 			error = bpf_setdlt(d, *(u_int *)addr);
10508eab61f3SSam Leffler 		break;
10518eab61f3SSam Leffler 
10528eab61f3SSam Leffler 	/*
10531dd0feaaSArchie Cobbs 	 * Get interface name.
1054df8bae1dSRodney W. Grimes 	 */
1055df8bae1dSRodney W. Grimes 	case BIOCGETIF:
1056572bde2aSRobert Watson 		if (d->bd_bif == NULL)
1057df8bae1dSRodney W. Grimes 			error = EINVAL;
10581dd0feaaSArchie Cobbs 		else {
10591dd0feaaSArchie Cobbs 			struct ifnet *const ifp = d->bd_bif->bif_ifp;
10601dd0feaaSArchie Cobbs 			struct ifreq *const ifr = (struct ifreq *)addr;
10611dd0feaaSArchie Cobbs 
10629bf40edeSBrooks Davis 			strlcpy(ifr->ifr_name, ifp->if_xname,
10639bf40edeSBrooks Davis 			    sizeof(ifr->ifr_name));
10641dd0feaaSArchie Cobbs 		}
1065df8bae1dSRodney W. Grimes 		break;
1066df8bae1dSRodney W. Grimes 
1067df8bae1dSRodney W. Grimes 	/*
1068df8bae1dSRodney W. Grimes 	 * Set interface.
1069df8bae1dSRodney W. Grimes 	 */
1070df8bae1dSRodney W. Grimes 	case BIOCSETIF:
1071df8bae1dSRodney W. Grimes 		error = bpf_setif(d, (struct ifreq *)addr);
1072df8bae1dSRodney W. Grimes 		break;
1073df8bae1dSRodney W. Grimes 
1074df8bae1dSRodney W. Grimes 	/*
1075df8bae1dSRodney W. Grimes 	 * Set read timeout.
1076df8bae1dSRodney W. Grimes 	 */
1077df8bae1dSRodney W. Grimes 	case BIOCSRTIMEOUT:
1078df8bae1dSRodney W. Grimes 		{
1079df8bae1dSRodney W. Grimes 			struct timeval *tv = (struct timeval *)addr;
1080df8bae1dSRodney W. Grimes 
1081bdc2cdc5SAlexander Langer 			/*
1082bdc2cdc5SAlexander Langer 			 * Subtract 1 tick from tvtohz() since this isn't
1083bdc2cdc5SAlexander Langer 			 * a one-shot timer.
1084bdc2cdc5SAlexander Langer 			 */
1085bdc2cdc5SAlexander Langer 			if ((error = itimerfix(tv)) == 0)
1086bdc2cdc5SAlexander Langer 				d->bd_rtout = tvtohz(tv) - 1;
1087df8bae1dSRodney W. Grimes 			break;
1088df8bae1dSRodney W. Grimes 		}
1089df8bae1dSRodney W. Grimes 
1090df8bae1dSRodney W. Grimes 	/*
1091df8bae1dSRodney W. Grimes 	 * Get read timeout.
1092df8bae1dSRodney W. Grimes 	 */
1093df8bae1dSRodney W. Grimes 	case BIOCGRTIMEOUT:
1094df8bae1dSRodney W. Grimes 		{
1095df8bae1dSRodney W. Grimes 			struct timeval *tv = (struct timeval *)addr;
1096df8bae1dSRodney W. Grimes 
1097bdc2cdc5SAlexander Langer 			tv->tv_sec = d->bd_rtout / hz;
1098bdc2cdc5SAlexander Langer 			tv->tv_usec = (d->bd_rtout % hz) * tick;
1099df8bae1dSRodney W. Grimes 			break;
1100df8bae1dSRodney W. Grimes 		}
1101df8bae1dSRodney W. Grimes 
1102df8bae1dSRodney W. Grimes 	/*
1103df8bae1dSRodney W. Grimes 	 * Get packet stats.
1104df8bae1dSRodney W. Grimes 	 */
1105df8bae1dSRodney W. Grimes 	case BIOCGSTATS:
1106df8bae1dSRodney W. Grimes 		{
1107df8bae1dSRodney W. Grimes 			struct bpf_stat *bs = (struct bpf_stat *)addr;
1108df8bae1dSRodney W. Grimes 
11094d621040SChristian S.J. Peron 			/* XXXCSJP overflow */
1110df8bae1dSRodney W. Grimes 			bs->bs_recv = d->bd_rcount;
1111df8bae1dSRodney W. Grimes 			bs->bs_drop = d->bd_dcount;
1112df8bae1dSRodney W. Grimes 			break;
1113df8bae1dSRodney W. Grimes 		}
1114df8bae1dSRodney W. Grimes 
1115df8bae1dSRodney W. Grimes 	/*
1116df8bae1dSRodney W. Grimes 	 * Set immediate mode.
1117df8bae1dSRodney W. Grimes 	 */
1118df8bae1dSRodney W. Grimes 	case BIOCIMMEDIATE:
1119df8bae1dSRodney W. Grimes 		d->bd_immediate = *(u_int *)addr;
1120df8bae1dSRodney W. Grimes 		break;
1121df8bae1dSRodney W. Grimes 
1122df8bae1dSRodney W. Grimes 	case BIOCVERSION:
1123df8bae1dSRodney W. Grimes 		{
1124df8bae1dSRodney W. Grimes 			struct bpf_version *bv = (struct bpf_version *)addr;
1125df8bae1dSRodney W. Grimes 
1126df8bae1dSRodney W. Grimes 			bv->bv_major = BPF_MAJOR_VERSION;
1127df8bae1dSRodney W. Grimes 			bv->bv_minor = BPF_MINOR_VERSION;
1128df8bae1dSRodney W. Grimes 			break;
1129df8bae1dSRodney W. Grimes 		}
113000a83887SPaul Traina 
1131114ae644SMike Smith 	/*
1132114ae644SMike Smith 	 * Get "header already complete" flag
1133114ae644SMike Smith 	 */
1134114ae644SMike Smith 	case BIOCGHDRCMPLT:
1135114ae644SMike Smith 		*(u_int *)addr = d->bd_hdrcmplt;
1136114ae644SMike Smith 		break;
1137114ae644SMike Smith 
1138114ae644SMike Smith 	/*
1139114ae644SMike Smith 	 * Set "header already complete" flag
1140114ae644SMike Smith 	 */
1141114ae644SMike Smith 	case BIOCSHDRCMPLT:
1142114ae644SMike Smith 		d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
1143114ae644SMike Smith 		break;
1144114ae644SMike Smith 
11458ed3828cSRobert Watson 	/*
1146560a54e1SJung-uk Kim 	 * Get packet direction flag
11478ed3828cSRobert Watson 	 */
1148560a54e1SJung-uk Kim 	case BIOCGDIRECTION:
1149560a54e1SJung-uk Kim 		*(u_int *)addr = d->bd_direction;
11508ed3828cSRobert Watson 		break;
11518ed3828cSRobert Watson 
11528ed3828cSRobert Watson 	/*
1153560a54e1SJung-uk Kim 	 * Set packet direction flag
11548ed3828cSRobert Watson 	 */
1155560a54e1SJung-uk Kim 	case BIOCSDIRECTION:
1156560a54e1SJung-uk Kim 		{
1157560a54e1SJung-uk Kim 			u_int	direction;
1158560a54e1SJung-uk Kim 
1159560a54e1SJung-uk Kim 			direction = *(u_int *)addr;
1160560a54e1SJung-uk Kim 			switch (direction) {
1161560a54e1SJung-uk Kim 			case BPF_D_IN:
1162560a54e1SJung-uk Kim 			case BPF_D_INOUT:
1163560a54e1SJung-uk Kim 			case BPF_D_OUT:
1164560a54e1SJung-uk Kim 				d->bd_direction = direction;
1165560a54e1SJung-uk Kim 				break;
1166560a54e1SJung-uk Kim 			default:
1167560a54e1SJung-uk Kim 				error = EINVAL;
1168560a54e1SJung-uk Kim 			}
1169560a54e1SJung-uk Kim 		}
1170560a54e1SJung-uk Kim 		break;
1171560a54e1SJung-uk Kim 
1172560a54e1SJung-uk Kim 	case BIOCFEEDBACK:
1173560a54e1SJung-uk Kim 		d->bd_feedback = *(u_int *)addr;
1174560a54e1SJung-uk Kim 		break;
1175560a54e1SJung-uk Kim 
1176560a54e1SJung-uk Kim 	case BIOCLOCK:
1177560a54e1SJung-uk Kim 		d->bd_locked = 1;
11788ed3828cSRobert Watson 		break;
11798ed3828cSRobert Watson 
118000a83887SPaul Traina 	case FIONBIO:		/* Non-blocking I/O */
118100a83887SPaul Traina 		break;
118200a83887SPaul Traina 
118300a83887SPaul Traina 	case FIOASYNC:		/* Send signal on receive packets */
118400a83887SPaul Traina 		d->bd_async = *(int *)addr;
118500a83887SPaul Traina 		break;
118600a83887SPaul Traina 
1187831d27a9SDon Lewis 	case FIOSETOWN:
1188831d27a9SDon Lewis 		error = fsetown(*(int *)addr, &d->bd_sigio);
118900a83887SPaul Traina 		break;
119000a83887SPaul Traina 
1191831d27a9SDon Lewis 	case FIOGETOWN:
119291e97a82SDon Lewis 		*(int *)addr = fgetown(&d->bd_sigio);
1193831d27a9SDon Lewis 		break;
1194831d27a9SDon Lewis 
1195831d27a9SDon Lewis 	/* This is deprecated, FIOSETOWN should be used instead. */
1196831d27a9SDon Lewis 	case TIOCSPGRP:
1197831d27a9SDon Lewis 		error = fsetown(-(*(int *)addr), &d->bd_sigio);
1198831d27a9SDon Lewis 		break;
1199831d27a9SDon Lewis 
1200831d27a9SDon Lewis 	/* This is deprecated, FIOGETOWN should be used instead. */
120100a83887SPaul Traina 	case TIOCGPGRP:
120291e97a82SDon Lewis 		*(int *)addr = -fgetown(&d->bd_sigio);
120300a83887SPaul Traina 		break;
120400a83887SPaul Traina 
120500a83887SPaul Traina 	case BIOCSRSIG:		/* Set receive signal */
120600a83887SPaul Traina 		{
120700a83887SPaul Traina 			u_int sig;
120800a83887SPaul Traina 
120900a83887SPaul Traina 			sig = *(u_int *)addr;
121000a83887SPaul Traina 
121100a83887SPaul Traina 			if (sig >= NSIG)
121200a83887SPaul Traina 				error = EINVAL;
121300a83887SPaul Traina 			else
121400a83887SPaul Traina 				d->bd_sig = sig;
121500a83887SPaul Traina 			break;
121600a83887SPaul Traina 		}
121700a83887SPaul Traina 	case BIOCGRSIG:
121800a83887SPaul Traina 		*(u_int *)addr = d->bd_sig;
121900a83887SPaul Traina 		break;
12204d621040SChristian S.J. Peron 
12214d621040SChristian S.J. Peron 	case BIOCGETBUFMODE:
12224d621040SChristian S.J. Peron 		*(u_int *)addr = d->bd_bufmode;
12234d621040SChristian S.J. Peron 		break;
12244d621040SChristian S.J. Peron 
12254d621040SChristian S.J. Peron 	case BIOCSETBUFMODE:
12264d621040SChristian S.J. Peron 		/*
12274d621040SChristian S.J. Peron 		 * Allow the buffering mode to be changed as long as we
12284d621040SChristian S.J. Peron 		 * haven't yet committed to a particular mode.  Our
12294d621040SChristian S.J. Peron 		 * definition of commitment, for now, is whether or not a
12304d621040SChristian S.J. Peron 		 * buffer has been allocated or an interface attached, since
12314d621040SChristian S.J. Peron 		 * that's the point where things get tricky.
12324d621040SChristian S.J. Peron 		 */
12334d621040SChristian S.J. Peron 		switch (*(u_int *)addr) {
12344d621040SChristian S.J. Peron 		case BPF_BUFMODE_BUFFER:
12354d621040SChristian S.J. Peron 			break;
12364d621040SChristian S.J. Peron 
12374d621040SChristian S.J. Peron 		case BPF_BUFMODE_ZBUF:
12384d621040SChristian S.J. Peron 			if (bpf_zerocopy_enable)
12394d621040SChristian S.J. Peron 				break;
12404d621040SChristian S.J. Peron 			/* FALLSTHROUGH */
12414d621040SChristian S.J. Peron 
12424d621040SChristian S.J. Peron 		default:
12434d621040SChristian S.J. Peron 			return (EINVAL);
12444d621040SChristian S.J. Peron 		}
12454d621040SChristian S.J. Peron 
12464d621040SChristian S.J. Peron 		BPFD_LOCK(d);
12474d621040SChristian S.J. Peron 		if (d->bd_sbuf != NULL || d->bd_hbuf != NULL ||
12484d621040SChristian S.J. Peron 		    d->bd_fbuf != NULL || d->bd_bif != NULL) {
12494d621040SChristian S.J. Peron 			BPFD_UNLOCK(d);
12504d621040SChristian S.J. Peron 			return (EBUSY);
12514d621040SChristian S.J. Peron 		}
12524d621040SChristian S.J. Peron 		d->bd_bufmode = *(u_int *)addr;
12534d621040SChristian S.J. Peron 		BPFD_UNLOCK(d);
12544d621040SChristian S.J. Peron 		break;
12554d621040SChristian S.J. Peron 
12564d621040SChristian S.J. Peron 	case BIOCGETZMAX:
12574d621040SChristian S.J. Peron 		return (bpf_ioctl_getzmax(td, d, (size_t *)addr));
12584d621040SChristian S.J. Peron 
12594d621040SChristian S.J. Peron 	case BIOCSETZBUF:
12604d621040SChristian S.J. Peron 		return (bpf_ioctl_setzbuf(td, d, (struct bpf_zbuf *)addr));
12614d621040SChristian S.J. Peron 
12624d621040SChristian S.J. Peron 	case BIOCROTZBUF:
12634d621040SChristian S.J. Peron 		return (bpf_ioctl_rotzbuf(td, d, (struct bpf_zbuf *)addr));
1264df8bae1dSRodney W. Grimes 	}
1265df8bae1dSRodney W. Grimes 	return (error);
1266df8bae1dSRodney W. Grimes }
1267df8bae1dSRodney W. Grimes 
1268df8bae1dSRodney W. Grimes /*
1269df8bae1dSRodney W. Grimes  * Set d's packet filter program to fp.  If this file already has a filter,
1270df8bae1dSRodney W. Grimes  * free it and replace it.  Returns EINVAL for bogus requests.
1271df8bae1dSRodney W. Grimes  */
1272f708ef1bSPoul-Henning Kamp static int
127319ba8395SChristian S.J. Peron bpf_setf(struct bpf_d *d, struct bpf_program *fp, u_long cmd)
1274df8bae1dSRodney W. Grimes {
1275df8bae1dSRodney W. Grimes 	struct bpf_insn *fcode, *old;
127693e39f0bSChristian S.J. Peron 	u_int wfilter, flen, size;
1277293c06a1SRuslan Ermilov #ifdef BPF_JITTER
1278ae275efcSJung-uk Kim 	bpf_jit_filter *ofunc;
1279ae275efcSJung-uk Kim #endif
1280df8bae1dSRodney W. Grimes 
128193e39f0bSChristian S.J. Peron 	if (cmd == BIOCSETWF) {
128293e39f0bSChristian S.J. Peron 		old = d->bd_wfilter;
128393e39f0bSChristian S.J. Peron 		wfilter = 1;
1284293c06a1SRuslan Ermilov #ifdef BPF_JITTER
1285ae275efcSJung-uk Kim 		ofunc = NULL;
1286ae275efcSJung-uk Kim #endif
128793e39f0bSChristian S.J. Peron 	} else {
128893e39f0bSChristian S.J. Peron 		wfilter = 0;
128993e39f0bSChristian S.J. Peron 		old = d->bd_rfilter;
1290293c06a1SRuslan Ermilov #ifdef BPF_JITTER
1291ae275efcSJung-uk Kim 		ofunc = d->bd_bfilter;
1292ae275efcSJung-uk Kim #endif
129393e39f0bSChristian S.J. Peron 	}
1294572bde2aSRobert Watson 	if (fp->bf_insns == NULL) {
1295df8bae1dSRodney W. Grimes 		if (fp->bf_len != 0)
1296df8bae1dSRodney W. Grimes 			return (EINVAL);
1297e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
129893e39f0bSChristian S.J. Peron 		if (wfilter)
129993e39f0bSChristian S.J. Peron 			d->bd_wfilter = NULL;
1300ae275efcSJung-uk Kim 		else {
130193e39f0bSChristian S.J. Peron 			d->bd_rfilter = NULL;
1302293c06a1SRuslan Ermilov #ifdef BPF_JITTER
1303ae275efcSJung-uk Kim 			d->bd_bfilter = NULL;
1304ae275efcSJung-uk Kim #endif
1305ae275efcSJung-uk Kim 		}
1306df8bae1dSRodney W. Grimes 		reset_d(d);
1307e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1308572bde2aSRobert Watson 		if (old != NULL)
1309bd3a5320SPoul-Henning Kamp 			free((caddr_t)old, M_BPF);
1310293c06a1SRuslan Ermilov #ifdef BPF_JITTER
1311ae275efcSJung-uk Kim 		if (ofunc != NULL)
1312ae275efcSJung-uk Kim 			bpf_destroy_jit_filter(ofunc);
1313ae275efcSJung-uk Kim #endif
1314df8bae1dSRodney W. Grimes 		return (0);
1315df8bae1dSRodney W. Grimes 	}
1316df8bae1dSRodney W. Grimes 	flen = fp->bf_len;
13170eb20604SChristian S.J. Peron 	if (flen > bpf_maxinsns)
1318df8bae1dSRodney W. Grimes 		return (EINVAL);
1319df8bae1dSRodney W. Grimes 
1320df8bae1dSRodney W. Grimes 	size = flen * sizeof(*fp->bf_insns);
1321a163d034SWarner Losh 	fcode = (struct bpf_insn *)malloc(size, M_BPF, M_WAITOK);
1322df8bae1dSRodney W. Grimes 	if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
1323df8bae1dSRodney W. Grimes 	    bpf_validate(fcode, (int)flen)) {
1324e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
132593e39f0bSChristian S.J. Peron 		if (wfilter)
132693e39f0bSChristian S.J. Peron 			d->bd_wfilter = fcode;
1327ae275efcSJung-uk Kim 		else {
132893e39f0bSChristian S.J. Peron 			d->bd_rfilter = fcode;
1329293c06a1SRuslan Ermilov #ifdef BPF_JITTER
1330ae275efcSJung-uk Kim 			d->bd_bfilter = bpf_jitter(fcode, flen);
1331ae275efcSJung-uk Kim #endif
1332ae275efcSJung-uk Kim 		}
1333df8bae1dSRodney W. Grimes 		reset_d(d);
1334e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1335572bde2aSRobert Watson 		if (old != NULL)
1336bd3a5320SPoul-Henning Kamp 			free((caddr_t)old, M_BPF);
1337293c06a1SRuslan Ermilov #ifdef BPF_JITTER
1338ae275efcSJung-uk Kim 		if (ofunc != NULL)
1339ae275efcSJung-uk Kim 			bpf_destroy_jit_filter(ofunc);
1340ae275efcSJung-uk Kim #endif
1341df8bae1dSRodney W. Grimes 
1342df8bae1dSRodney W. Grimes 		return (0);
1343df8bae1dSRodney W. Grimes 	}
1344bd3a5320SPoul-Henning Kamp 	free((caddr_t)fcode, M_BPF);
1345df8bae1dSRodney W. Grimes 	return (EINVAL);
1346df8bae1dSRodney W. Grimes }
1347df8bae1dSRodney W. Grimes 
1348df8bae1dSRodney W. Grimes /*
1349df8bae1dSRodney W. Grimes  * Detach a file from its current interface (if attached at all) and attach
1350df8bae1dSRodney W. Grimes  * to the interface indicated by the name stored in ifr.
1351df8bae1dSRodney W. Grimes  * Return an errno or 0.
1352df8bae1dSRodney W. Grimes  */
1353df8bae1dSRodney W. Grimes static int
135419ba8395SChristian S.J. Peron bpf_setif(struct bpf_d *d, struct ifreq *ifr)
1355df8bae1dSRodney W. Grimes {
1356df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
13579b44ff22SGarrett Wollman 	struct ifnet *theywant;
1358df8bae1dSRodney W. Grimes 
13599b44ff22SGarrett Wollman 	theywant = ifunit(ifr->ifr_name);
136016d878ccSChristian S.J. Peron 	if (theywant == NULL || theywant->if_bpf == NULL)
136116d878ccSChristian S.J. Peron 		return (ENXIO);
13629b44ff22SGarrett Wollman 
136316d878ccSChristian S.J. Peron 	bp = theywant->if_bpf;
13644d621040SChristian S.J. Peron 
1365df8bae1dSRodney W. Grimes 	/*
13664d621040SChristian S.J. Peron 	 * Behavior here depends on the buffering model.  If we're using
13674d621040SChristian S.J. Peron 	 * kernel memory buffers, then we can allocate them here.  If we're
13684d621040SChristian S.J. Peron 	 * using zero-copy, then the user process must have registered
13694d621040SChristian S.J. Peron 	 * buffers by the time we get here.  If not, return an error.
13704d621040SChristian S.J. Peron 	 *
13714d621040SChristian S.J. Peron 	 * XXXRW: There are locking issues here with multi-threaded use: what
13724d621040SChristian S.J. Peron 	 * if two threads try to set the interface at once?
1373df8bae1dSRodney W. Grimes 	 */
13744d621040SChristian S.J. Peron 	switch (d->bd_bufmode) {
13754d621040SChristian S.J. Peron 	case BPF_BUFMODE_BUFFER:
1376a3594432SRobert Watson 		if (d->bd_sbuf == NULL)
13774d621040SChristian S.J. Peron 			bpf_buffer_alloc(d);
13784d621040SChristian S.J. Peron 		KASSERT(d->bd_sbuf != NULL, ("bpf_setif: bd_sbuf NULL"));
13794d621040SChristian S.J. Peron 		break;
13804d621040SChristian S.J. Peron 
13814d621040SChristian S.J. Peron 	case BPF_BUFMODE_ZBUF:
13824d621040SChristian S.J. Peron 		if (d->bd_sbuf == NULL)
13834d621040SChristian S.J. Peron 			return (EINVAL);
13844d621040SChristian S.J. Peron 		break;
13854d621040SChristian S.J. Peron 
13864d621040SChristian S.J. Peron 	default:
13874d621040SChristian S.J. Peron 		panic("bpf_setif: bufmode %d", d->bd_bufmode);
13884d621040SChristian S.J. Peron 	}
1389df8bae1dSRodney W. Grimes 	if (bp != d->bd_bif) {
1390df8bae1dSRodney W. Grimes 		if (d->bd_bif)
1391df8bae1dSRodney W. Grimes 			/*
1392df8bae1dSRodney W. Grimes 			 * Detach if attached to something else.
1393df8bae1dSRodney W. Grimes 			 */
1394df8bae1dSRodney W. Grimes 			bpf_detachd(d);
1395df8bae1dSRodney W. Grimes 
1396df8bae1dSRodney W. Grimes 		bpf_attachd(d, bp);
1397df8bae1dSRodney W. Grimes 	}
1398e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
1399df8bae1dSRodney W. Grimes 	reset_d(d);
1400e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
1401df8bae1dSRodney W. Grimes 	return (0);
1402df8bae1dSRodney W. Grimes }
1403df8bae1dSRodney W. Grimes 
1404df8bae1dSRodney W. Grimes /*
1405243ac7d8SPeter Wemm  * Support for select() and poll() system calls
1406df8bae1dSRodney W. Grimes  *
1407df8bae1dSRodney W. Grimes  * Return true iff the specific operation will not block indefinitely.
1408df8bae1dSRodney W. Grimes  * Otherwise, return false but make a note that a selwakeup() must be done.
1409df8bae1dSRodney W. Grimes  */
141037c84183SPoul-Henning Kamp static int
141119ba8395SChristian S.J. Peron bpfpoll(struct cdev *dev, int events, struct thread *td)
1412df8bae1dSRodney W. Grimes {
1413e7bb21b3SJonathan Lemon 	struct bpf_d *d;
14140832fc64SGarance A Drosehn 	int revents;
1415df8bae1dSRodney W. Grimes 
1416bd3a5320SPoul-Henning Kamp 	d = dev->si_drv1;
1417de5d9935SRobert Watson 	if (d->bd_bif == NULL)
1418de5d9935SRobert Watson 		return (ENXIO);
1419de5d9935SRobert Watson 
1420b75a24a0SChristian S.J. Peron 	/*
1421b75a24a0SChristian S.J. Peron 	 * Refresh PID associated with this descriptor.
1422b75a24a0SChristian S.J. Peron 	 */
14230832fc64SGarance A Drosehn 	revents = events & (POLLOUT | POLLWRNORM);
1424e7bb21b3SJonathan Lemon 	BPFD_LOCK(d);
1425cb1d4f92SChristian S.J. Peron 	d->bd_pid = td->td_proc->p_pid;
142675c13541SPoul-Henning Kamp 	if (events & (POLLIN | POLLRDNORM)) {
142795aab9ccSJohn-Mark Gurney 		if (bpf_ready(d))
1428243ac7d8SPeter Wemm 			revents |= events & (POLLIN | POLLRDNORM);
142981bda851SJohn Polstra 		else {
1430ed01445dSJohn Baldwin 			selrecord(td, &d->bd_sel);
143181bda851SJohn Polstra 			/* Start the read timeout if necessary. */
143281bda851SJohn Polstra 			if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
143381bda851SJohn Polstra 				callout_reset(&d->bd_callout, d->bd_rtout,
143481bda851SJohn Polstra 				    bpf_timed_out, d);
143581bda851SJohn Polstra 				d->bd_state = BPF_WAITING;
143681bda851SJohn Polstra 			}
143781bda851SJohn Polstra 		}
143875c13541SPoul-Henning Kamp 	}
1439e7bb21b3SJonathan Lemon 	BPFD_UNLOCK(d);
1440243ac7d8SPeter Wemm 	return (revents);
1441df8bae1dSRodney W. Grimes }
1442df8bae1dSRodney W. Grimes 
1443df8bae1dSRodney W. Grimes /*
144495aab9ccSJohn-Mark Gurney  * Support for kevent() system call.  Register EVFILT_READ filters and
144595aab9ccSJohn-Mark Gurney  * reject all others.
144695aab9ccSJohn-Mark Gurney  */
144795aab9ccSJohn-Mark Gurney int
144819ba8395SChristian S.J. Peron bpfkqfilter(struct cdev *dev, struct knote *kn)
144995aab9ccSJohn-Mark Gurney {
145095aab9ccSJohn-Mark Gurney 	struct bpf_d *d = (struct bpf_d *)dev->si_drv1;
145195aab9ccSJohn-Mark Gurney 
145295aab9ccSJohn-Mark Gurney 	if (kn->kn_filter != EVFILT_READ)
145395aab9ccSJohn-Mark Gurney 		return (1);
145495aab9ccSJohn-Mark Gurney 
1455b75a24a0SChristian S.J. Peron 	/*
1456b75a24a0SChristian S.J. Peron 	 * Refresh PID associated with this descriptor.
1457b75a24a0SChristian S.J. Peron 	 */
1458cb1d4f92SChristian S.J. Peron 	BPFD_LOCK(d);
1459b75a24a0SChristian S.J. Peron 	d->bd_pid = curthread->td_proc->p_pid;
146095aab9ccSJohn-Mark Gurney 	kn->kn_fop = &bpfread_filtops;
146195aab9ccSJohn-Mark Gurney 	kn->kn_hook = d;
14624b19419eSChristian S.J. Peron 	knlist_add(&d->bd_sel.si_note, kn, 1);
1463cb1d4f92SChristian S.J. Peron 	BPFD_UNLOCK(d);
146495aab9ccSJohn-Mark Gurney 
146595aab9ccSJohn-Mark Gurney 	return (0);
146695aab9ccSJohn-Mark Gurney }
146795aab9ccSJohn-Mark Gurney 
146895aab9ccSJohn-Mark Gurney static void
146919ba8395SChristian S.J. Peron filt_bpfdetach(struct knote *kn)
147095aab9ccSJohn-Mark Gurney {
147195aab9ccSJohn-Mark Gurney 	struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
147295aab9ccSJohn-Mark Gurney 
1473ad3b9257SJohn-Mark Gurney 	knlist_remove(&d->bd_sel.si_note, kn, 0);
147495aab9ccSJohn-Mark Gurney }
147595aab9ccSJohn-Mark Gurney 
147695aab9ccSJohn-Mark Gurney static int
147719ba8395SChristian S.J. Peron filt_bpfread(struct knote *kn, long hint)
147895aab9ccSJohn-Mark Gurney {
147995aab9ccSJohn-Mark Gurney 	struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
148095aab9ccSJohn-Mark Gurney 	int ready;
148195aab9ccSJohn-Mark Gurney 
148286c9a453SJohn-Mark Gurney 	BPFD_LOCK_ASSERT(d);
148395aab9ccSJohn-Mark Gurney 	ready = bpf_ready(d);
148495aab9ccSJohn-Mark Gurney 	if (ready) {
148595aab9ccSJohn-Mark Gurney 		kn->kn_data = d->bd_slen;
148695aab9ccSJohn-Mark Gurney 		if (d->bd_hbuf)
148795aab9ccSJohn-Mark Gurney 			kn->kn_data += d->bd_hlen;
148895aab9ccSJohn-Mark Gurney 	}
148995aab9ccSJohn-Mark Gurney 	else if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
149095aab9ccSJohn-Mark Gurney 		callout_reset(&d->bd_callout, d->bd_rtout,
149195aab9ccSJohn-Mark Gurney 		    bpf_timed_out, d);
149295aab9ccSJohn-Mark Gurney 		d->bd_state = BPF_WAITING;
149395aab9ccSJohn-Mark Gurney 	}
149495aab9ccSJohn-Mark Gurney 
149595aab9ccSJohn-Mark Gurney 	return (ready);
149695aab9ccSJohn-Mark Gurney }
149795aab9ccSJohn-Mark Gurney 
149895aab9ccSJohn-Mark Gurney /*
1499df8bae1dSRodney W. Grimes  * Incoming linkage from device drivers.  Process the packet pkt, of length
1500df8bae1dSRodney W. Grimes  * pktlen, which is stored in a contiguous buffer.  The packet is parsed
1501df8bae1dSRodney W. Grimes  * by each process' filter, and if accepted, stashed into the corresponding
1502df8bae1dSRodney W. Grimes  * buffer.
1503df8bae1dSRodney W. Grimes  */
1504df8bae1dSRodney W. Grimes void
150519ba8395SChristian S.J. Peron bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen)
1506df8bae1dSRodney W. Grimes {
15078994a245SDag-Erling Smørgrav 	struct bpf_d *d;
15088994a245SDag-Erling Smørgrav 	u_int slen;
150991433904SDavid Malone 	int gottime;
151091433904SDavid Malone 	struct timeval tv;
1511e7bb21b3SJonathan Lemon 
151291433904SDavid Malone 	gottime = 0;
1513e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
15144a3feeaaSRobert Watson 	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1515e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1516df8bae1dSRodney W. Grimes 		++d->bd_rcount;
1517ae275efcSJung-uk Kim #ifdef BPF_JITTER
1518ae275efcSJung-uk Kim 		if (bpf_jitter_enable != 0 && d->bd_bfilter != NULL)
1519ae275efcSJung-uk Kim 			slen = (*(d->bd_bfilter->func))(pkt, pktlen, pktlen);
1520ae275efcSJung-uk Kim 		else
1521ae275efcSJung-uk Kim #endif
152293e39f0bSChristian S.J. Peron 		slen = bpf_filter(d->bd_rfilter, pkt, pktlen, pktlen);
1523ec272d87SRobert Watson 		if (slen != 0) {
152469f7644bSChristian S.J. Peron 			d->bd_fcount++;
152591433904SDavid Malone 			if (!gottime) {
152691433904SDavid Malone 				microtime(&tv);
152791433904SDavid Malone 				gottime = 1;
152891433904SDavid Malone 			}
1529ec272d87SRobert Watson #ifdef MAC
153030d239bcSRobert Watson 			if (mac_bpfdesc_check_receive(d, bp->bif_ifp) == 0)
1531ec272d87SRobert Watson #endif
15324d621040SChristian S.J. Peron 				catchpacket(d, pkt, pktlen, slen,
15334d621040SChristian S.J. Peron 				    bpf_append_bytes, &tv);
1534ec272d87SRobert Watson 		}
1535e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1536df8bae1dSRodney W. Grimes 	}
1537e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
1538df8bae1dSRodney W. Grimes }
1539df8bae1dSRodney W. Grimes 
1540560a54e1SJung-uk Kim #define	BPF_CHECK_DIRECTION(d, m) \
1541560a54e1SJung-uk Kim 	if (((d)->bd_direction == BPF_D_IN && (m)->m_pkthdr.rcvif == NULL) || \
1542560a54e1SJung-uk Kim 	    ((d)->bd_direction == BPF_D_OUT && (m)->m_pkthdr.rcvif != NULL))
1543560a54e1SJung-uk Kim 
1544df8bae1dSRodney W. Grimes /*
1545df8bae1dSRodney W. Grimes  * Incoming linkage from device drivers, when packet is in an mbuf chain.
1546df8bae1dSRodney W. Grimes  */
1547df8bae1dSRodney W. Grimes void
154819ba8395SChristian S.J. Peron bpf_mtap(struct bpf_if *bp, struct mbuf *m)
1549df8bae1dSRodney W. Grimes {
1550df8bae1dSRodney W. Grimes 	struct bpf_d *d;
1551df8bae1dSRodney W. Grimes 	u_int pktlen, slen;
155291433904SDavid Malone 	int gottime;
155391433904SDavid Malone 	struct timeval tv;
155491433904SDavid Malone 
1555560a54e1SJung-uk Kim 	if (m->m_flags & M_SKIP_BPF) {
1556560a54e1SJung-uk Kim 		m->m_flags &= ~M_SKIP_BPF;
1557560a54e1SJung-uk Kim 		return;
1558560a54e1SJung-uk Kim 	}
1559560a54e1SJung-uk Kim 
156091433904SDavid Malone 	gottime = 0;
1561df8bae1dSRodney W. Grimes 
1562f0e2422bSPoul-Henning Kamp 	pktlen = m_length(m, NULL);
1563df8bae1dSRodney W. Grimes 
1564e7bb21b3SJonathan Lemon 	BPFIF_LOCK(bp);
15654a3feeaaSRobert Watson 	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1566560a54e1SJung-uk Kim 		BPF_CHECK_DIRECTION(d, m)
15678ed3828cSRobert Watson 			continue;
1568e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1569df8bae1dSRodney W. Grimes 		++d->bd_rcount;
1570ae275efcSJung-uk Kim #ifdef BPF_JITTER
1571ae275efcSJung-uk Kim 		/* XXX We cannot handle multiple mbufs. */
1572ae275efcSJung-uk Kim 		if (bpf_jitter_enable != 0 && d->bd_bfilter != NULL &&
1573ae275efcSJung-uk Kim 		    m->m_next == NULL)
1574ae275efcSJung-uk Kim 			slen = (*(d->bd_bfilter->func))(mtod(m, u_char *),
1575ae275efcSJung-uk Kim 			    pktlen, pktlen);
1576ae275efcSJung-uk Kim 		else
1577ae275efcSJung-uk Kim #endif
157893e39f0bSChristian S.J. Peron 		slen = bpf_filter(d->bd_rfilter, (u_char *)m, pktlen, 0);
15794ddfb531SChristian S.J. Peron 		if (slen != 0) {
158069f7644bSChristian S.J. Peron 			d->bd_fcount++;
158191433904SDavid Malone 			if (!gottime) {
158291433904SDavid Malone 				microtime(&tv);
158391433904SDavid Malone 				gottime = 1;
158491433904SDavid Malone 			}
15850c7fb534SRobert Watson #ifdef MAC
158630d239bcSRobert Watson 			if (mac_bpfdesc_check_receive(d, bp->bif_ifp) == 0)
15870c7fb534SRobert Watson #endif
15880c7fb534SRobert Watson 				catchpacket(d, (u_char *)m, pktlen, slen,
15894d621040SChristian S.J. Peron 				    bpf_append_mbuf, &tv);
15904ddfb531SChristian S.J. Peron 		}
1591e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1592df8bae1dSRodney W. Grimes 	}
1593e7bb21b3SJonathan Lemon 	BPFIF_UNLOCK(bp);
1594df8bae1dSRodney W. Grimes }
1595df8bae1dSRodney W. Grimes 
1596df8bae1dSRodney W. Grimes /*
1597437ffe18SSam Leffler  * Incoming linkage from device drivers, when packet is in
1598437ffe18SSam Leffler  * an mbuf chain and to be prepended by a contiguous header.
1599437ffe18SSam Leffler  */
1600437ffe18SSam Leffler void
160119ba8395SChristian S.J. Peron bpf_mtap2(struct bpf_if *bp, void *data, u_int dlen, struct mbuf *m)
1602437ffe18SSam Leffler {
1603437ffe18SSam Leffler 	struct mbuf mb;
1604437ffe18SSam Leffler 	struct bpf_d *d;
1605437ffe18SSam Leffler 	u_int pktlen, slen;
160691433904SDavid Malone 	int gottime;
160791433904SDavid Malone 	struct timeval tv;
160891433904SDavid Malone 
1609560a54e1SJung-uk Kim 	if (m->m_flags & M_SKIP_BPF) {
1610560a54e1SJung-uk Kim 		m->m_flags &= ~M_SKIP_BPF;
1611560a54e1SJung-uk Kim 		return;
1612560a54e1SJung-uk Kim 	}
1613560a54e1SJung-uk Kim 
161491433904SDavid Malone 	gottime = 0;
1615437ffe18SSam Leffler 
1616437ffe18SSam Leffler 	pktlen = m_length(m, NULL);
1617437ffe18SSam Leffler 	/*
1618437ffe18SSam Leffler 	 * Craft on-stack mbuf suitable for passing to bpf_filter.
1619437ffe18SSam Leffler 	 * Note that we cut corners here; we only setup what's
1620437ffe18SSam Leffler 	 * absolutely needed--this mbuf should never go anywhere else.
1621437ffe18SSam Leffler 	 */
1622437ffe18SSam Leffler 	mb.m_next = m;
1623437ffe18SSam Leffler 	mb.m_data = data;
1624437ffe18SSam Leffler 	mb.m_len = dlen;
1625437ffe18SSam Leffler 	pktlen += dlen;
1626437ffe18SSam Leffler 
1627437ffe18SSam Leffler 	BPFIF_LOCK(bp);
16284a3feeaaSRobert Watson 	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1629560a54e1SJung-uk Kim 		BPF_CHECK_DIRECTION(d, m)
1630437ffe18SSam Leffler 			continue;
1631437ffe18SSam Leffler 		BPFD_LOCK(d);
1632437ffe18SSam Leffler 		++d->bd_rcount;
163393e39f0bSChristian S.J. Peron 		slen = bpf_filter(d->bd_rfilter, (u_char *)&mb, pktlen, 0);
16344ddfb531SChristian S.J. Peron 		if (slen != 0) {
163569f7644bSChristian S.J. Peron 			d->bd_fcount++;
163691433904SDavid Malone 			if (!gottime) {
163791433904SDavid Malone 				microtime(&tv);
163891433904SDavid Malone 				gottime = 1;
163991433904SDavid Malone 			}
1640437ffe18SSam Leffler #ifdef MAC
164130d239bcSRobert Watson 			if (mac_bpfdesc_check_receive(d, bp->bif_ifp) == 0)
1642437ffe18SSam Leffler #endif
1643437ffe18SSam Leffler 				catchpacket(d, (u_char *)&mb, pktlen, slen,
16444d621040SChristian S.J. Peron 				    bpf_append_mbuf, &tv);
16454ddfb531SChristian S.J. Peron 		}
1646437ffe18SSam Leffler 		BPFD_UNLOCK(d);
1647437ffe18SSam Leffler 	}
1648437ffe18SSam Leffler 	BPFIF_UNLOCK(bp);
1649437ffe18SSam Leffler }
1650437ffe18SSam Leffler 
1651560a54e1SJung-uk Kim #undef	BPF_CHECK_DIRECTION
1652560a54e1SJung-uk Kim 
1653437ffe18SSam Leffler /*
1654df8bae1dSRodney W. Grimes  * Move the packet data from interface memory (pkt) into the
16559e610888SDag-Erling Smørgrav  * store buffer.  "cpfn" is the routine called to do the actual data
1656df8bae1dSRodney W. Grimes  * transfer.  bcopy is passed in to copy contiguous chunks, while
16574d621040SChristian S.J. Peron  * bpf_append_mbuf is passed in to copy mbuf chains.  In the latter case,
1658df8bae1dSRodney W. Grimes  * pkt is really an mbuf.
1659df8bae1dSRodney W. Grimes  */
1660df8bae1dSRodney W. Grimes static void
166119ba8395SChristian S.J. Peron catchpacket(struct bpf_d *d, u_char *pkt, u_int pktlen, u_int snaplen,
16624d621040SChristian S.J. Peron     void (*cpfn)(struct bpf_d *, caddr_t, u_int, void *, u_int),
16634d621040SChristian S.J. Peron     struct timeval *tv)
1664df8bae1dSRodney W. Grimes {
16654d621040SChristian S.J. Peron 	struct bpf_hdr hdr;
16668994a245SDag-Erling Smørgrav 	int totlen, curlen;
16678994a245SDag-Erling Smørgrav 	int hdrlen = d->bd_bif->bif_hdrlen;
16687819da79SJohn-Mark Gurney 	int do_wakeup = 0;
16699e610888SDag-Erling Smørgrav 
1670a3272e3cSChristian S.J. Peron 	BPFD_LOCK_ASSERT(d);
16714d621040SChristian S.J. Peron 
16724d621040SChristian S.J. Peron 	/*
16734d621040SChristian S.J. Peron 	 * Detect whether user space has released a buffer back to us, and if
16744d621040SChristian S.J. Peron 	 * so, move it from being a hold buffer to a free buffer.  This may
16754d621040SChristian S.J. Peron 	 * not be the best place to do it (for example, we might only want to
16764d621040SChristian S.J. Peron 	 * run this check if we need the space), but for now it's a reliable
16774d621040SChristian S.J. Peron 	 * spot to do it.
16784d621040SChristian S.J. Peron 	 */
1679fa0c2b34SRobert Watson 	if (d->bd_fbuf == NULL && bpf_canfreebuf(d)) {
16804d621040SChristian S.J. Peron 		d->bd_fbuf = d->bd_hbuf;
16814d621040SChristian S.J. Peron 		d->bd_hbuf = NULL;
16824d621040SChristian S.J. Peron 		d->bd_hlen = 0;
16834d621040SChristian S.J. Peron 	}
16844d621040SChristian S.J. Peron 
1685df8bae1dSRodney W. Grimes 	/*
1686df8bae1dSRodney W. Grimes 	 * Figure out how many bytes to move.  If the packet is
1687df8bae1dSRodney W. Grimes 	 * greater or equal to the snapshot length, transfer that
1688df8bae1dSRodney W. Grimes 	 * much.  Otherwise, transfer the whole packet (unless
1689df8bae1dSRodney W. Grimes 	 * we hit the buffer size limit).
1690df8bae1dSRodney W. Grimes 	 */
1691df8bae1dSRodney W. Grimes 	totlen = hdrlen + min(snaplen, pktlen);
1692df8bae1dSRodney W. Grimes 	if (totlen > d->bd_bufsize)
1693df8bae1dSRodney W. Grimes 		totlen = d->bd_bufsize;
1694df8bae1dSRodney W. Grimes 
1695df8bae1dSRodney W. Grimes 	/*
1696df8bae1dSRodney W. Grimes 	 * Round up the end of the previous packet to the next longword.
1697df8bae1dSRodney W. Grimes 	 */
1698df8bae1dSRodney W. Grimes 	curlen = BPF_WORDALIGN(d->bd_slen);
1699df8bae1dSRodney W. Grimes 	if (curlen + totlen > d->bd_bufsize) {
1700df8bae1dSRodney W. Grimes 		/*
1701df8bae1dSRodney W. Grimes 		 * This packet will overflow the storage buffer.
1702df8bae1dSRodney W. Grimes 		 * Rotate the buffers if we can, then wakeup any
1703df8bae1dSRodney W. Grimes 		 * pending reads.
1704df8bae1dSRodney W. Grimes 		 */
1705572bde2aSRobert Watson 		if (d->bd_fbuf == NULL) {
1706df8bae1dSRodney W. Grimes 			/*
1707df8bae1dSRodney W. Grimes 			 * We haven't completed the previous read yet,
1708df8bae1dSRodney W. Grimes 			 * so drop the packet.
1709df8bae1dSRodney W. Grimes 			 */
1710df8bae1dSRodney W. Grimes 			++d->bd_dcount;
1711df8bae1dSRodney W. Grimes 			return;
1712df8bae1dSRodney W. Grimes 		}
1713df8bae1dSRodney W. Grimes 		ROTATE_BUFFERS(d);
17147819da79SJohn-Mark Gurney 		do_wakeup = 1;
1715df8bae1dSRodney W. Grimes 		curlen = 0;
1716df8bae1dSRodney W. Grimes 	}
171781bda851SJohn Polstra 	else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT)
1718df8bae1dSRodney W. Grimes 		/*
17194d621040SChristian S.J. Peron 		 * Immediate mode is set, or the read timeout has already
17204d621040SChristian S.J. Peron 		 * expired during a select call.  A packet arrived, so the
17214d621040SChristian S.J. Peron 		 * reader should be woken up.
1722df8bae1dSRodney W. Grimes 		 */
17237819da79SJohn-Mark Gurney 		do_wakeup = 1;
1724df8bae1dSRodney W. Grimes 
1725df8bae1dSRodney W. Grimes 	/*
17264d621040SChristian S.J. Peron 	 * Append the bpf header.  Note we append the actual header size, but
17274d621040SChristian S.J. Peron 	 * move forward the length of the header plus padding.
1728df8bae1dSRodney W. Grimes 	 */
17294d621040SChristian S.J. Peron 	bzero(&hdr, sizeof(hdr));
17304d621040SChristian S.J. Peron 	hdr.bh_tstamp = *tv;
17314d621040SChristian S.J. Peron 	hdr.bh_datalen = pktlen;
17324d621040SChristian S.J. Peron 	hdr.bh_hdrlen = hdrlen;
17334d621040SChristian S.J. Peron 	hdr.bh_caplen = totlen - hdrlen;
17344d621040SChristian S.J. Peron 	bpf_append_bytes(d, d->bd_sbuf, curlen, &hdr, sizeof(hdr));
17354d621040SChristian S.J. Peron 
1736df8bae1dSRodney W. Grimes 	/*
1737df8bae1dSRodney W. Grimes 	 * Copy the packet data into the store buffer and update its length.
1738df8bae1dSRodney W. Grimes 	 */
17394d621040SChristian S.J. Peron 	(*cpfn)(d, d->bd_sbuf, curlen + hdrlen, pkt, hdr.bh_caplen);
1740df8bae1dSRodney W. Grimes 	d->bd_slen = curlen + totlen;
17417819da79SJohn-Mark Gurney 
17427819da79SJohn-Mark Gurney 	if (do_wakeup)
17437819da79SJohn-Mark Gurney 		bpf_wakeup(d);
1744df8bae1dSRodney W. Grimes }
1745df8bae1dSRodney W. Grimes 
1746df8bae1dSRodney W. Grimes /*
1747df8bae1dSRodney W. Grimes  * Free buffers currently in use by a descriptor.
1748df8bae1dSRodney W. Grimes  * Called on close.
1749df8bae1dSRodney W. Grimes  */
1750df8bae1dSRodney W. Grimes static void
175119ba8395SChristian S.J. Peron bpf_freed(struct bpf_d *d)
1752df8bae1dSRodney W. Grimes {
17534d621040SChristian S.J. Peron 
1754df8bae1dSRodney W. Grimes 	/*
1755df8bae1dSRodney W. Grimes 	 * We don't need to lock out interrupts since this descriptor has
1756df8bae1dSRodney W. Grimes 	 * been detached from its interface and it yet hasn't been marked
1757df8bae1dSRodney W. Grimes 	 * free.
1758df8bae1dSRodney W. Grimes 	 */
17594d621040SChristian S.J. Peron 	bpf_free(d);
1760ae275efcSJung-uk Kim 	if (d->bd_rfilter) {
176193e39f0bSChristian S.J. Peron 		free((caddr_t)d->bd_rfilter, M_BPF);
1762ae275efcSJung-uk Kim #ifdef BPF_JITTER
1763ae275efcSJung-uk Kim 		bpf_destroy_jit_filter(d->bd_bfilter);
1764ae275efcSJung-uk Kim #endif
1765ae275efcSJung-uk Kim 	}
176693e39f0bSChristian S.J. Peron 	if (d->bd_wfilter)
176793e39f0bSChristian S.J. Peron 		free((caddr_t)d->bd_wfilter, M_BPF);
1768e7bb21b3SJonathan Lemon 	mtx_destroy(&d->bd_mtx);
1769df8bae1dSRodney W. Grimes }
1770df8bae1dSRodney W. Grimes 
1771df8bae1dSRodney W. Grimes /*
177224a229f4SSam Leffler  * Attach an interface to bpf.  dlt is the link layer type; hdrlen is the
177324a229f4SSam Leffler  * fixed size of the link header (variable length headers not yet supported).
1774df8bae1dSRodney W. Grimes  */
1775df8bae1dSRodney W. Grimes void
177619ba8395SChristian S.J. Peron bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen)
1777df8bae1dSRodney W. Grimes {
177824a229f4SSam Leffler 
177924a229f4SSam Leffler 	bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
178024a229f4SSam Leffler }
178124a229f4SSam Leffler 
178224a229f4SSam Leffler /*
178324a229f4SSam Leffler  * Attach an interface to bpf.  ifp is a pointer to the structure
178424a229f4SSam Leffler  * defining the interface to be attached, dlt is the link layer type,
178524a229f4SSam Leffler  * and hdrlen is the fixed size of the link header (variable length
178624a229f4SSam Leffler  * headers are not yet supporrted).
178724a229f4SSam Leffler  */
178824a229f4SSam Leffler void
178919ba8395SChristian S.J. Peron bpfattach2(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp)
179024a229f4SSam Leffler {
1791df8bae1dSRodney W. Grimes 	struct bpf_if *bp;
179219ba8395SChristian S.J. Peron 
179319ba8395SChristian S.J. Peron 	bp = malloc(sizeof(*bp), M_BPF, M_NOWAIT | M_ZERO);
1794572bde2aSRobert Watson 	if (bp == NULL)
1795df8bae1dSRodney W. Grimes 		panic("bpfattach");
1796df8bae1dSRodney W. Grimes 
17974a3feeaaSRobert Watson 	LIST_INIT(&bp->bif_dlist);
1798df8bae1dSRodney W. Grimes 	bp->bif_ifp = ifp;
1799df8bae1dSRodney W. Grimes 	bp->bif_dlt = dlt;
18006008862bSJohn Baldwin 	mtx_init(&bp->bif_mtx, "bpf interface lock", NULL, MTX_DEF);
180116d878ccSChristian S.J. Peron 	KASSERT(*driverp == NULL, ("bpfattach2: driverp already initialized"));
180216d878ccSChristian S.J. Peron 	*driverp = bp;
1803df8bae1dSRodney W. Grimes 
1804e7bb21b3SJonathan Lemon 	mtx_lock(&bpf_mtx);
18054a3feeaaSRobert Watson 	LIST_INSERT_HEAD(&bpf_iflist, bp, bif_next);
1806e7bb21b3SJonathan Lemon 	mtx_unlock(&bpf_mtx);
1807df8bae1dSRodney W. Grimes 
1808df8bae1dSRodney W. Grimes 	/*
1809df8bae1dSRodney W. Grimes 	 * Compute the length of the bpf header.  This is not necessarily
1810df8bae1dSRodney W. Grimes 	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1811df8bae1dSRodney W. Grimes 	 * that the network layer header begins on a longword boundary (for
1812df8bae1dSRodney W. Grimes 	 * performance reasons and to alleviate alignment restrictions).
1813df8bae1dSRodney W. Grimes 	 */
1814df8bae1dSRodney W. Grimes 	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1815df8bae1dSRodney W. Grimes 
18162eeab939SGarrett Wollman 	if (bootverbose)
181724a229f4SSam Leffler 		if_printf(ifp, "bpf attached\n");
1818df8bae1dSRodney W. Grimes }
181953ac6efbSJulian Elischer 
1820de5d9935SRobert Watson /*
1821de5d9935SRobert Watson  * Detach bpf from an interface.  This involves detaching each descriptor
1822de5d9935SRobert Watson  * associated with the interface, and leaving bd_bif NULL.  Notify each
1823de5d9935SRobert Watson  * descriptor as it's detached so that any sleepers wake up and get
1824de5d9935SRobert Watson  * ENXIO.
1825de5d9935SRobert Watson  */
1826de5d9935SRobert Watson void
182719ba8395SChristian S.J. Peron bpfdetach(struct ifnet *ifp)
1828de5d9935SRobert Watson {
18294a3feeaaSRobert Watson 	struct bpf_if	*bp;
1830de5d9935SRobert Watson 	struct bpf_d	*d;
1831de5d9935SRobert Watson 
1832de5d9935SRobert Watson 	/* Locate BPF interface information */
18338eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
18344a3feeaaSRobert Watson 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1835de5d9935SRobert Watson 		if (ifp == bp->bif_ifp)
1836de5d9935SRobert Watson 			break;
1837de5d9935SRobert Watson 	}
1838de5d9935SRobert Watson 
1839de5d9935SRobert Watson 	/* Interface wasn't attached */
1840d79bf337SMatthew N. Dodd 	if ((bp == NULL) || (bp->bif_ifp == NULL)) {
1841e7bb21b3SJonathan Lemon 		mtx_unlock(&bpf_mtx);
18429bf40edeSBrooks Davis 		printf("bpfdetach: %s was not attached\n", ifp->if_xname);
1843de5d9935SRobert Watson 		return;
1844de5d9935SRobert Watson 	}
1845de5d9935SRobert Watson 
18464a3feeaaSRobert Watson 	LIST_REMOVE(bp, bif_next);
18478eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
1848de5d9935SRobert Watson 
18494a3feeaaSRobert Watson 	while ((d = LIST_FIRST(&bp->bif_dlist)) != NULL) {
1850e7bb21b3SJonathan Lemon 		bpf_detachd(d);
1851e7bb21b3SJonathan Lemon 		BPFD_LOCK(d);
1852e7bb21b3SJonathan Lemon 		bpf_wakeup(d);
1853e7bb21b3SJonathan Lemon 		BPFD_UNLOCK(d);
1854e7bb21b3SJonathan Lemon 	}
1855e7bb21b3SJonathan Lemon 
1856e7bb21b3SJonathan Lemon 	mtx_destroy(&bp->bif_mtx);
1857de5d9935SRobert Watson 	free(bp, M_BPF);
18588eab61f3SSam Leffler }
1859de5d9935SRobert Watson 
18608eab61f3SSam Leffler /*
18618eab61f3SSam Leffler  * Get a list of available data link type of the interface.
18628eab61f3SSam Leffler  */
18638eab61f3SSam Leffler static int
186419ba8395SChristian S.J. Peron bpf_getdltlist(struct bpf_d *d, struct bpf_dltlist *bfl)
18658eab61f3SSam Leffler {
18668eab61f3SSam Leffler 	int n, error;
18678eab61f3SSam Leffler 	struct ifnet *ifp;
18688eab61f3SSam Leffler 	struct bpf_if *bp;
18698eab61f3SSam Leffler 
18708eab61f3SSam Leffler 	ifp = d->bd_bif->bif_ifp;
18718eab61f3SSam Leffler 	n = 0;
18728eab61f3SSam Leffler 	error = 0;
18738eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
18744a3feeaaSRobert Watson 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
18758eab61f3SSam Leffler 		if (bp->bif_ifp != ifp)
18768eab61f3SSam Leffler 			continue;
18778eab61f3SSam Leffler 		if (bfl->bfl_list != NULL) {
18788eab61f3SSam Leffler 			if (n >= bfl->bfl_len) {
1879e7bb21b3SJonathan Lemon 				mtx_unlock(&bpf_mtx);
18808eab61f3SSam Leffler 				return (ENOMEM);
18818eab61f3SSam Leffler 			}
18828eab61f3SSam Leffler 			error = copyout(&bp->bif_dlt,
18838eab61f3SSam Leffler 			    bfl->bfl_list + n, sizeof(u_int));
18848eab61f3SSam Leffler 		}
18858eab61f3SSam Leffler 		n++;
18868eab61f3SSam Leffler 	}
18878eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
18888eab61f3SSam Leffler 	bfl->bfl_len = n;
18898eab61f3SSam Leffler 	return (error);
18908eab61f3SSam Leffler }
18918eab61f3SSam Leffler 
18928eab61f3SSam Leffler /*
18938eab61f3SSam Leffler  * Set the data link type of a BPF instance.
18948eab61f3SSam Leffler  */
18958eab61f3SSam Leffler static int
189619ba8395SChristian S.J. Peron bpf_setdlt(struct bpf_d *d, u_int dlt)
18978eab61f3SSam Leffler {
18988eab61f3SSam Leffler 	int error, opromisc;
18998eab61f3SSam Leffler 	struct ifnet *ifp;
19008eab61f3SSam Leffler 	struct bpf_if *bp;
19018eab61f3SSam Leffler 
19028eab61f3SSam Leffler 	if (d->bd_bif->bif_dlt == dlt)
19038eab61f3SSam Leffler 		return (0);
19048eab61f3SSam Leffler 	ifp = d->bd_bif->bif_ifp;
19058eab61f3SSam Leffler 	mtx_lock(&bpf_mtx);
19064a3feeaaSRobert Watson 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
19078eab61f3SSam Leffler 		if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
19088eab61f3SSam Leffler 			break;
19098eab61f3SSam Leffler 	}
19108eab61f3SSam Leffler 	mtx_unlock(&bpf_mtx);
19118eab61f3SSam Leffler 	if (bp != NULL) {
19128eab61f3SSam Leffler 		opromisc = d->bd_promisc;
19138eab61f3SSam Leffler 		bpf_detachd(d);
19148eab61f3SSam Leffler 		bpf_attachd(d, bp);
191593daabddSBrian Feldman 		BPFD_LOCK(d);
19168eab61f3SSam Leffler 		reset_d(d);
19178eab61f3SSam Leffler 		BPFD_UNLOCK(d);
19188eab61f3SSam Leffler 		if (opromisc) {
19198eab61f3SSam Leffler 			error = ifpromisc(bp->bif_ifp, 1);
19208eab61f3SSam Leffler 			if (error)
19218eab61f3SSam Leffler 				if_printf(bp->bif_ifp,
19228eab61f3SSam Leffler 					"bpf_setdlt: ifpromisc failed (%d)\n",
19238eab61f3SSam Leffler 					error);
19248eab61f3SSam Leffler 			else
19258eab61f3SSam Leffler 				d->bd_promisc = 1;
19268eab61f3SSam Leffler 		}
19278eab61f3SSam Leffler 	}
19288eab61f3SSam Leffler 	return (bp == NULL ? EINVAL : 0);
1929de5d9935SRobert Watson }
1930de5d9935SRobert Watson 
19313f54a085SPoul-Henning Kamp static void
193219ba8395SChristian S.J. Peron bpf_clone(void *arg, struct ucred *cred, char *name, int namelen,
193319ba8395SChristian S.J. Peron     struct cdev **dev)
19343f54a085SPoul-Henning Kamp {
19353f54a085SPoul-Henning Kamp 	int u;
19363f54a085SPoul-Henning Kamp 
1937f3732fd1SPoul-Henning Kamp 	if (*dev != NULL)
19383f54a085SPoul-Henning Kamp 		return;
1939db901281SPoul-Henning Kamp 	if (dev_stdclone(name, NULL, "bpf", &u) != 1)
19403f54a085SPoul-Henning Kamp 		return;
1941b0d17ba6SPoul-Henning Kamp 	*dev = make_dev(&bpf_cdevsw, unit2minor(u), UID_ROOT, GID_WHEEL, 0600,
1942b0d17ba6SPoul-Henning Kamp 	    "bpf%d", u);
1943f4f6abcbSPoul-Henning Kamp 	dev_ref(*dev);
1944b0d17ba6SPoul-Henning Kamp 	(*dev)->si_flags |= SI_CHEAPCLONE;
19453f54a085SPoul-Henning Kamp 	return;
19463f54a085SPoul-Henning Kamp }
19473f54a085SPoul-Henning Kamp 
1948514ede09SBruce Evans static void
194919ba8395SChristian S.J. Peron bpf_drvinit(void *unused)
195053ac6efbSJulian Elischer {
195153ac6efbSJulian Elischer 
19526008862bSJohn Baldwin 	mtx_init(&bpf_mtx, "bpf global lock", NULL, MTX_DEF);
19534a3feeaaSRobert Watson 	LIST_INIT(&bpf_iflist);
1954db901281SPoul-Henning Kamp 	EVENTHANDLER_REGISTER(dev_clone, bpf_clone, 0, 1000);
19557198bf47SJulian Elischer }
195653ac6efbSJulian Elischer 
195769f7644bSChristian S.J. Peron static void
195869f7644bSChristian S.J. Peron bpfstats_fill_xbpf(struct xbpf_d *d, struct bpf_d *bd)
195969f7644bSChristian S.J. Peron {
196069f7644bSChristian S.J. Peron 
196169f7644bSChristian S.J. Peron 	bzero(d, sizeof(*d));
196269f7644bSChristian S.J. Peron 	BPFD_LOCK_ASSERT(bd);
19634d621040SChristian S.J. Peron 	d->bd_structsize = sizeof(*d);
196469f7644bSChristian S.J. Peron 	d->bd_immediate = bd->bd_immediate;
196569f7644bSChristian S.J. Peron 	d->bd_promisc = bd->bd_promisc;
196669f7644bSChristian S.J. Peron 	d->bd_hdrcmplt = bd->bd_hdrcmplt;
1967560a54e1SJung-uk Kim 	d->bd_direction = bd->bd_direction;
1968560a54e1SJung-uk Kim 	d->bd_feedback = bd->bd_feedback;
196969f7644bSChristian S.J. Peron 	d->bd_async = bd->bd_async;
197069f7644bSChristian S.J. Peron 	d->bd_rcount = bd->bd_rcount;
197169f7644bSChristian S.J. Peron 	d->bd_dcount = bd->bd_dcount;
197269f7644bSChristian S.J. Peron 	d->bd_fcount = bd->bd_fcount;
197369f7644bSChristian S.J. Peron 	d->bd_sig = bd->bd_sig;
197469f7644bSChristian S.J. Peron 	d->bd_slen = bd->bd_slen;
197569f7644bSChristian S.J. Peron 	d->bd_hlen = bd->bd_hlen;
197669f7644bSChristian S.J. Peron 	d->bd_bufsize = bd->bd_bufsize;
197769f7644bSChristian S.J. Peron 	d->bd_pid = bd->bd_pid;
197869f7644bSChristian S.J. Peron 	strlcpy(d->bd_ifname,
197969f7644bSChristian S.J. Peron 	    bd->bd_bif->bif_ifp->if_xname, IFNAMSIZ);
198093e39f0bSChristian S.J. Peron 	d->bd_locked = bd->bd_locked;
19814d621040SChristian S.J. Peron 	d->bd_wcount = bd->bd_wcount;
19824d621040SChristian S.J. Peron 	d->bd_wdcount = bd->bd_wdcount;
19834d621040SChristian S.J. Peron 	d->bd_wfcount = bd->bd_wfcount;
19844d621040SChristian S.J. Peron 	d->bd_zcopy = bd->bd_zcopy;
19854d621040SChristian S.J. Peron 	d->bd_bufmode = bd->bd_bufmode;
198669f7644bSChristian S.J. Peron }
198769f7644bSChristian S.J. Peron 
198869f7644bSChristian S.J. Peron static int
198969f7644bSChristian S.J. Peron bpf_stats_sysctl(SYSCTL_HANDLER_ARGS)
199069f7644bSChristian S.J. Peron {
1991422a63daSChristian S.J. Peron 	struct xbpf_d *xbdbuf, *xbd;
1992422a63daSChristian S.J. Peron 	int index, error;
199369f7644bSChristian S.J. Peron 	struct bpf_if *bp;
199469f7644bSChristian S.J. Peron 	struct bpf_d *bd;
199569f7644bSChristian S.J. Peron 
199669f7644bSChristian S.J. Peron 	/*
199769f7644bSChristian S.J. Peron 	 * XXX This is not technically correct. It is possible for non
199869f7644bSChristian S.J. Peron 	 * privileged users to open bpf devices. It would make sense
199969f7644bSChristian S.J. Peron 	 * if the users who opened the devices were able to retrieve
200069f7644bSChristian S.J. Peron 	 * the statistics for them, too.
200169f7644bSChristian S.J. Peron 	 */
2002acd3428bSRobert Watson 	error = priv_check(req->td, PRIV_NET_BPF);
200369f7644bSChristian S.J. Peron 	if (error)
200469f7644bSChristian S.J. Peron 		return (error);
200569f7644bSChristian S.J. Peron 	if (req->oldptr == NULL)
2006422a63daSChristian S.J. Peron 		return (SYSCTL_OUT(req, 0, bpf_bpfd_cnt * sizeof(*xbd)));
200769f7644bSChristian S.J. Peron 	if (bpf_bpfd_cnt == 0)
200869f7644bSChristian S.J. Peron 		return (SYSCTL_OUT(req, 0, 0));
2009422a63daSChristian S.J. Peron 	xbdbuf = malloc(req->oldlen, M_BPF, M_WAITOK);
201069f7644bSChristian S.J. Peron 	mtx_lock(&bpf_mtx);
2011422a63daSChristian S.J. Peron 	if (req->oldlen < (bpf_bpfd_cnt * sizeof(*xbd))) {
2012422a63daSChristian S.J. Peron 		mtx_unlock(&bpf_mtx);
2013422a63daSChristian S.J. Peron 		free(xbdbuf, M_BPF);
2014422a63daSChristian S.J. Peron 		return (ENOMEM);
2015422a63daSChristian S.J. Peron 	}
2016422a63daSChristian S.J. Peron 	index = 0;
201769f7644bSChristian S.J. Peron 	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
20181fc9e387SChristian S.J. Peron 		BPFIF_LOCK(bp);
201969f7644bSChristian S.J. Peron 		LIST_FOREACH(bd, &bp->bif_dlist, bd_next) {
2020422a63daSChristian S.J. Peron 			xbd = &xbdbuf[index++];
202169f7644bSChristian S.J. Peron 			BPFD_LOCK(bd);
2022422a63daSChristian S.J. Peron 			bpfstats_fill_xbpf(xbd, bd);
202369f7644bSChristian S.J. Peron 			BPFD_UNLOCK(bd);
202469f7644bSChristian S.J. Peron 		}
20251fc9e387SChristian S.J. Peron 		BPFIF_UNLOCK(bp);
202669f7644bSChristian S.J. Peron 	}
202769f7644bSChristian S.J. Peron 	mtx_unlock(&bpf_mtx);
2028422a63daSChristian S.J. Peron 	error = SYSCTL_OUT(req, xbdbuf, index * sizeof(*xbd));
2029422a63daSChristian S.J. Peron 	free(xbdbuf, M_BPF);
203069f7644bSChristian S.J. Peron 	return (error);
203169f7644bSChristian S.J. Peron }
203269f7644bSChristian S.J. Peron 
2033237fdd78SRobert Watson SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,bpf_drvinit,NULL);
203453ac6efbSJulian Elischer 
20355bb5f2c9SPeter Wemm #else /* !DEV_BPF && !NETGRAPH_BPF */
2036f8dc4716SMike Smith /*
2037f8dc4716SMike Smith  * NOP stubs to allow bpf-using drivers to load and function.
2038f8dc4716SMike Smith  *
2039f8dc4716SMike Smith  * A 'better' implementation would allow the core bpf functionality
2040f8dc4716SMike Smith  * to be loaded at runtime.
2041f8dc4716SMike Smith  */
20427eae78a4SChristian S.J. Peron static struct bpf_if bp_null;
2043f8dc4716SMike Smith 
2044f8dc4716SMike Smith void
204519ba8395SChristian S.J. Peron bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen)
2046f8dc4716SMike Smith {
2047f8dc4716SMike Smith }
2048f8dc4716SMike Smith 
2049f8dc4716SMike Smith void
205019ba8395SChristian S.J. Peron bpf_mtap(struct bpf_if *bp, struct mbuf *m)
2051f8dc4716SMike Smith {
2052f8dc4716SMike Smith }
2053f8dc4716SMike Smith 
2054f8dc4716SMike Smith void
205519ba8395SChristian S.J. Peron bpf_mtap2(struct bpf_if *bp, void *d, u_int l, struct mbuf *m)
2056437ffe18SSam Leffler {
2057437ffe18SSam Leffler }
2058437ffe18SSam Leffler 
2059437ffe18SSam Leffler void
206019ba8395SChristian S.J. Peron bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen)
2061f8dc4716SMike Smith {
20627eae78a4SChristian S.J. Peron 
20637eae78a4SChristian S.J. Peron 	bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
2064f8dc4716SMike Smith }
2065f8dc4716SMike Smith 
2066da626c17SBill Paul void
206719ba8395SChristian S.J. Peron bpfattach2(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp)
20685f7a7923SSam Leffler {
20697eae78a4SChristian S.J. Peron 
20707eae78a4SChristian S.J. Peron 	*driverp = &bp_null;
20715f7a7923SSam Leffler }
20725f7a7923SSam Leffler 
20735f7a7923SSam Leffler void
207419ba8395SChristian S.J. Peron bpfdetach(struct ifnet *ifp)
2075da626c17SBill Paul {
2076da626c17SBill Paul }
2077da626c17SBill Paul 
2078f8dc4716SMike Smith u_int
207919ba8395SChristian S.J. Peron bpf_filter(const struct bpf_insn *pc, u_char *p, u_int wirelen, u_int buflen)
2080f8dc4716SMike Smith {
2081f8dc4716SMike Smith 	return -1;	/* "no filter" behaviour */
2082f8dc4716SMike Smith }
2083f8dc4716SMike Smith 
20845bb5f2c9SPeter Wemm int
208519ba8395SChristian S.J. Peron bpf_validate(const struct bpf_insn *f, int len)
20865bb5f2c9SPeter Wemm {
20875bb5f2c9SPeter Wemm 	return 0;		/* false */
20885bb5f2c9SPeter Wemm }
20895bb5f2c9SPeter Wemm 
20905bb5f2c9SPeter Wemm #endif /* !DEV_BPF && !NETGRAPH_BPF */
2091