bpf.c revision 180337
1/*-
2 * Copyright (c) 1990, 1991, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from the Stanford/CMU enet packet filter,
6 * (net/enet.c) distributed as part of 4.3BSD, and code contributed
7 * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
8 * Berkeley Laboratory.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 *      @(#)bpf.c	8.4 (Berkeley) 1/9/95
35 */
36
37#include <sys/cdefs.h>
38__FBSDID("$FreeBSD: head/sys/net/bpf.c 180337 2008-07-07 09:25:49Z dwmalone $");
39
40#include "opt_bpf.h"
41#include "opt_mac.h"
42#include "opt_netgraph.h"
43
44#include <sys/types.h>
45#include <sys/param.h>
46#include <sys/systm.h>
47#include <sys/conf.h>
48#include <sys/fcntl.h>
49#include <sys/malloc.h>
50#include <sys/mbuf.h>
51#include <sys/time.h>
52#include <sys/priv.h>
53#include <sys/proc.h>
54#include <sys/signalvar.h>
55#include <sys/filio.h>
56#include <sys/sockio.h>
57#include <sys/ttycom.h>
58#include <sys/uio.h>
59
60#include <sys/event.h>
61#include <sys/file.h>
62#include <sys/poll.h>
63#include <sys/proc.h>
64
65#include <sys/socket.h>
66
67#include <net/if.h>
68#include <net/bpf.h>
69#include <net/bpf_buffer.h>
70#ifdef BPF_JITTER
71#include <net/bpf_jitter.h>
72#endif
73#include <net/bpf_zerocopy.h>
74#include <net/bpfdesc.h>
75
76#include <netinet/in.h>
77#include <netinet/if_ether.h>
78#include <sys/kernel.h>
79#include <sys/sysctl.h>
80
81#include <net80211/ieee80211_freebsd.h>
82
83#include <security/mac/mac_framework.h>
84
85MALLOC_DEFINE(M_BPF, "BPF", "BPF data");
86
87#if defined(DEV_BPF) || defined(NETGRAPH_BPF)
88
89#define PRINET  26			/* interruptible */
90
91/*
92 * bpf_iflist is a list of BPF interface structures, each corresponding to a
93 * specific DLT.  The same network interface might have several BPF interface
94 * structures registered by different layers in the stack (i.e., 802.11
95 * frames, ethernet frames, etc).
96 */
97static LIST_HEAD(, bpf_if)	bpf_iflist;
98static struct mtx	bpf_mtx;		/* bpf global lock */
99static int		bpf_bpfd_cnt;
100
101static void	bpf_attachd(struct bpf_d *, struct bpf_if *);
102static void	bpf_detachd(struct bpf_d *);
103static void	bpf_freed(struct bpf_d *);
104static int	bpf_movein(struct uio *, int, struct ifnet *, struct mbuf **,
105		    struct sockaddr *, int *, struct bpf_insn *);
106static int	bpf_setif(struct bpf_d *, struct ifreq *);
107static void	bpf_timed_out(void *);
108static __inline void
109		bpf_wakeup(struct bpf_d *);
110static void	catchpacket(struct bpf_d *, u_char *, u_int, u_int,
111		    void (*)(struct bpf_d *, caddr_t, u_int, void *, u_int),
112		    struct timeval *);
113static void	reset_d(struct bpf_d *);
114static int	 bpf_setf(struct bpf_d *, struct bpf_program *, u_long cmd);
115static int	bpf_getdltlist(struct bpf_d *, struct bpf_dltlist *);
116static int	bpf_setdlt(struct bpf_d *, u_int);
117static void	filt_bpfdetach(struct knote *);
118static int	filt_bpfread(struct knote *, long);
119static void	bpf_drvinit(void *);
120static void	bpf_clone(void *, struct ucred *, char *, int, struct cdev **);
121static int	bpf_stats_sysctl(SYSCTL_HANDLER_ARGS);
122
123SYSCTL_NODE(_net, OID_AUTO, bpf, CTLFLAG_RW, 0, "bpf sysctl");
124static int bpf_maxinsns = BPF_MAXINSNS;
125SYSCTL_INT(_net_bpf, OID_AUTO, maxinsns, CTLFLAG_RW,
126    &bpf_maxinsns, 0, "Maximum bpf program instructions");
127static int bpf_zerocopy_enable = 0;
128SYSCTL_INT(_net_bpf, OID_AUTO, zerocopy_enable, CTLFLAG_RW,
129    &bpf_zerocopy_enable, 0, "Enable new zero-copy BPF buffer sessions");
130SYSCTL_NODE(_net_bpf, OID_AUTO, stats, CTLFLAG_RW,
131    bpf_stats_sysctl, "bpf statistics portal");
132
133static	d_open_t	bpfopen;
134static	d_close_t	bpfclose;
135static	d_read_t	bpfread;
136static	d_write_t	bpfwrite;
137static	d_ioctl_t	bpfioctl;
138static	d_poll_t	bpfpoll;
139static	d_kqfilter_t	bpfkqfilter;
140
141static struct cdevsw bpf_cdevsw = {
142	.d_version =	D_VERSION,
143	.d_flags =	D_TRACKCLOSE,
144	.d_open =	bpfopen,
145	.d_close =	bpfclose,
146	.d_read =	bpfread,
147	.d_write =	bpfwrite,
148	.d_ioctl =	bpfioctl,
149	.d_poll =	bpfpoll,
150	.d_name =	"bpf",
151	.d_kqfilter =	bpfkqfilter,
152};
153
154static struct filterops bpfread_filtops =
155	{ 1, NULL, filt_bpfdetach, filt_bpfread };
156
157/*
158 * Wrapper functions for various buffering methods.  If the set of buffer
159 * modes expands, we will probably want to introduce a switch data structure
160 * similar to protosw, et.
161 */
162static void
163bpf_append_bytes(struct bpf_d *d, caddr_t buf, u_int offset, void *src,
164    u_int len)
165{
166
167	BPFD_LOCK_ASSERT(d);
168
169	switch (d->bd_bufmode) {
170	case BPF_BUFMODE_BUFFER:
171		return (bpf_buffer_append_bytes(d, buf, offset, src, len));
172
173	case BPF_BUFMODE_ZBUF:
174		d->bd_zcopy++;
175		return (bpf_zerocopy_append_bytes(d, buf, offset, src, len));
176
177	default:
178		panic("bpf_buf_append_bytes");
179	}
180}
181
182static void
183bpf_append_mbuf(struct bpf_d *d, caddr_t buf, u_int offset, void *src,
184    u_int len)
185{
186
187	BPFD_LOCK_ASSERT(d);
188
189	switch (d->bd_bufmode) {
190	case BPF_BUFMODE_BUFFER:
191		return (bpf_buffer_append_mbuf(d, buf, offset, src, len));
192
193	case BPF_BUFMODE_ZBUF:
194		d->bd_zcopy++;
195		return (bpf_zerocopy_append_mbuf(d, buf, offset, src, len));
196
197	default:
198		panic("bpf_buf_append_mbuf");
199	}
200}
201
202/*
203 * This function gets called when the free buffer is re-assigned.
204 */
205static void
206bpf_buf_reclaimed(struct bpf_d *d)
207{
208
209	BPFD_LOCK_ASSERT(d);
210
211	switch (d->bd_bufmode) {
212	case BPF_BUFMODE_BUFFER:
213		return;
214
215	case BPF_BUFMODE_ZBUF:
216		bpf_zerocopy_buf_reclaimed(d);
217		return;
218
219	default:
220		panic("bpf_buf_reclaimed");
221	}
222}
223
224/*
225 * If the buffer mechanism has a way to decide that a held buffer can be made
226 * free, then it is exposed via the bpf_canfreebuf() interface.  (1) is
227 * returned if the buffer can be discarded, (0) is returned if it cannot.
228 */
229static int
230bpf_canfreebuf(struct bpf_d *d)
231{
232
233	BPFD_LOCK_ASSERT(d);
234
235	switch (d->bd_bufmode) {
236	case BPF_BUFMODE_ZBUF:
237		return (bpf_zerocopy_canfreebuf(d));
238	}
239	return (0);
240}
241
242/*
243 * Allow the buffer model to indicate that the current store buffer is
244 * immutable, regardless of the appearance of space.  Return (1) if the
245 * buffer is writable, and (0) if not.
246 */
247static int
248bpf_canwritebuf(struct bpf_d *d)
249{
250
251	BPFD_LOCK_ASSERT(d);
252
253	switch (d->bd_bufmode) {
254	case BPF_BUFMODE_ZBUF:
255		return (bpf_zerocopy_canwritebuf(d));
256	}
257	return (1);
258}
259
260/*
261 * Notify buffer model that an attempt to write to the store buffer has
262 * resulted in a dropped packet, in which case the buffer may be considered
263 * full.
264 */
265static void
266bpf_buffull(struct bpf_d *d)
267{
268
269	BPFD_LOCK_ASSERT(d);
270
271	switch (d->bd_bufmode) {
272	case BPF_BUFMODE_ZBUF:
273		bpf_zerocopy_buffull(d);
274		break;
275	}
276}
277
278/*
279 * Notify the buffer model that a buffer has moved into the hold position.
280 */
281void
282bpf_bufheld(struct bpf_d *d)
283{
284
285	BPFD_LOCK_ASSERT(d);
286
287	switch (d->bd_bufmode) {
288	case BPF_BUFMODE_ZBUF:
289		bpf_zerocopy_bufheld(d);
290		break;
291	}
292}
293
294static void
295bpf_free(struct bpf_d *d)
296{
297
298	switch (d->bd_bufmode) {
299	case BPF_BUFMODE_BUFFER:
300		return (bpf_buffer_free(d));
301
302	case BPF_BUFMODE_ZBUF:
303		return (bpf_zerocopy_free(d));
304
305	default:
306		panic("bpf_buf_free");
307	}
308}
309
310static int
311bpf_uiomove(struct bpf_d *d, caddr_t buf, u_int len, struct uio *uio)
312{
313
314	if (d->bd_bufmode != BPF_BUFMODE_BUFFER)
315		return (EOPNOTSUPP);
316	return (bpf_buffer_uiomove(d, buf, len, uio));
317}
318
319static int
320bpf_ioctl_sblen(struct bpf_d *d, u_int *i)
321{
322
323	if (d->bd_bufmode != BPF_BUFMODE_BUFFER)
324		return (EOPNOTSUPP);
325	return (bpf_buffer_ioctl_sblen(d, i));
326}
327
328static int
329bpf_ioctl_getzmax(struct thread *td, struct bpf_d *d, size_t *i)
330{
331
332	if (d->bd_bufmode != BPF_BUFMODE_ZBUF)
333		return (EOPNOTSUPP);
334	return (bpf_zerocopy_ioctl_getzmax(td, d, i));
335}
336
337static int
338bpf_ioctl_rotzbuf(struct thread *td, struct bpf_d *d, struct bpf_zbuf *bz)
339{
340
341	if (d->bd_bufmode != BPF_BUFMODE_ZBUF)
342		return (EOPNOTSUPP);
343	return (bpf_zerocopy_ioctl_rotzbuf(td, d, bz));
344}
345
346static int
347bpf_ioctl_setzbuf(struct thread *td, struct bpf_d *d, struct bpf_zbuf *bz)
348{
349
350	if (d->bd_bufmode != BPF_BUFMODE_ZBUF)
351		return (EOPNOTSUPP);
352	return (bpf_zerocopy_ioctl_setzbuf(td, d, bz));
353}
354
355/*
356 * General BPF functions.
357 */
358static int
359bpf_movein(struct uio *uio, int linktype, struct ifnet *ifp, struct mbuf **mp,
360    struct sockaddr *sockp, int *hdrlen, struct bpf_insn *wfilter)
361{
362	const struct ieee80211_bpf_params *p;
363	struct ether_header *eh;
364	struct mbuf *m;
365	int error;
366	int len;
367	int hlen;
368	int slen;
369
370	/*
371	 * Build a sockaddr based on the data link layer type.
372	 * We do this at this level because the ethernet header
373	 * is copied directly into the data field of the sockaddr.
374	 * In the case of SLIP, there is no header and the packet
375	 * is forwarded as is.
376	 * Also, we are careful to leave room at the front of the mbuf
377	 * for the link level header.
378	 */
379	switch (linktype) {
380
381	case DLT_SLIP:
382		sockp->sa_family = AF_INET;
383		hlen = 0;
384		break;
385
386	case DLT_EN10MB:
387		sockp->sa_family = AF_UNSPEC;
388		/* XXX Would MAXLINKHDR be better? */
389		hlen = ETHER_HDR_LEN;
390		break;
391
392	case DLT_FDDI:
393		sockp->sa_family = AF_IMPLINK;
394		hlen = 0;
395		break;
396
397	case DLT_RAW:
398		sockp->sa_family = AF_UNSPEC;
399		hlen = 0;
400		break;
401
402	case DLT_NULL:
403		/*
404		 * null interface types require a 4 byte pseudo header which
405		 * corresponds to the address family of the packet.
406		 */
407		sockp->sa_family = AF_UNSPEC;
408		hlen = 4;
409		break;
410
411	case DLT_ATM_RFC1483:
412		/*
413		 * en atm driver requires 4-byte atm pseudo header.
414		 * though it isn't standard, vpi:vci needs to be
415		 * specified anyway.
416		 */
417		sockp->sa_family = AF_UNSPEC;
418		hlen = 12;	/* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
419		break;
420
421	case DLT_PPP:
422		sockp->sa_family = AF_UNSPEC;
423		hlen = 4;	/* This should match PPP_HDRLEN */
424		break;
425
426	case DLT_IEEE802_11:		/* IEEE 802.11 wireless */
427		sockp->sa_family = AF_IEEE80211;
428		hlen = 0;
429		break;
430
431	case DLT_IEEE802_11_RADIO:	/* IEEE 802.11 wireless w/ phy params */
432		sockp->sa_family = AF_IEEE80211;
433		sockp->sa_len = 12;	/* XXX != 0 */
434		hlen = sizeof(struct ieee80211_bpf_params);
435		break;
436
437	default:
438		return (EIO);
439	}
440
441	len = uio->uio_resid;
442
443	if (len - hlen > ifp->if_mtu)
444		return (EMSGSIZE);
445
446	if ((unsigned)len > MCLBYTES)
447		return (EIO);
448
449	if (len > MHLEN)
450		m = m_getcl(M_WAIT, MT_DATA, M_PKTHDR);
451	else
452		MGETHDR(m, M_WAIT, MT_DATA);
453	m->m_pkthdr.len = m->m_len = len;
454	m->m_pkthdr.rcvif = NULL;
455	*mp = m;
456
457	if (m->m_len < hlen) {
458		error = EPERM;
459		goto bad;
460	}
461
462	error = uiomove(mtod(m, u_char *), len, uio);
463	if (error)
464		goto bad;
465
466	slen = bpf_filter(wfilter, mtod(m, u_char *), len, len);
467	if (slen == 0) {
468		error = EPERM;
469		goto bad;
470	}
471
472	/* Check for multicast destination */
473	switch (linktype) {
474	case DLT_EN10MB:
475		eh = mtod(m, struct ether_header *);
476		if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
477			if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
478			    ETHER_ADDR_LEN) == 0)
479				m->m_flags |= M_BCAST;
480			else
481				m->m_flags |= M_MCAST;
482		}
483		break;
484	}
485
486	/*
487	 * Make room for link header, and copy it to sockaddr
488	 */
489	if (hlen != 0) {
490		if (sockp->sa_family == AF_IEEE80211) {
491			/*
492			 * Collect true length from the parameter header
493			 * NB: sockp is known to be zero'd so if we do a
494			 *     short copy unspecified parameters will be
495			 *     zero.
496			 * NB: packet may not be aligned after stripping
497			 *     bpf params
498			 * XXX check ibp_vers
499			 */
500			p = mtod(m, const struct ieee80211_bpf_params *);
501			hlen = p->ibp_len;
502			if (hlen > sizeof(sockp->sa_data)) {
503				error = EINVAL;
504				goto bad;
505			}
506		}
507		bcopy(m->m_data, sockp->sa_data, hlen);
508	}
509	*hdrlen = hlen;
510
511	return (0);
512bad:
513	m_freem(m);
514	return (error);
515}
516
517/*
518 * Attach file to the bpf interface, i.e. make d listen on bp.
519 */
520static void
521bpf_attachd(struct bpf_d *d, struct bpf_if *bp)
522{
523	/*
524	 * Point d at bp, and add d to the interface's list of listeners.
525	 * Finally, point the driver's bpf cookie at the interface so
526	 * it will divert packets to bpf.
527	 */
528	BPFIF_LOCK(bp);
529	d->bd_bif = bp;
530	LIST_INSERT_HEAD(&bp->bif_dlist, d, bd_next);
531
532	bpf_bpfd_cnt++;
533	BPFIF_UNLOCK(bp);
534}
535
536/*
537 * Detach a file from its interface.
538 */
539static void
540bpf_detachd(struct bpf_d *d)
541{
542	int error;
543	struct bpf_if *bp;
544	struct ifnet *ifp;
545
546	bp = d->bd_bif;
547	BPFIF_LOCK(bp);
548	BPFD_LOCK(d);
549	ifp = d->bd_bif->bif_ifp;
550
551	/*
552	 * Remove d from the interface's descriptor list.
553	 */
554	LIST_REMOVE(d, bd_next);
555
556	bpf_bpfd_cnt--;
557	d->bd_bif = NULL;
558	BPFD_UNLOCK(d);
559	BPFIF_UNLOCK(bp);
560
561	/*
562	 * Check if this descriptor had requested promiscuous mode.
563	 * If so, turn it off.
564	 */
565	if (d->bd_promisc) {
566		d->bd_promisc = 0;
567		error = ifpromisc(ifp, 0);
568		if (error != 0 && error != ENXIO) {
569			/*
570			 * ENXIO can happen if a pccard is unplugged
571			 * Something is really wrong if we were able to put
572			 * the driver into promiscuous mode, but can't
573			 * take it out.
574			 */
575			if_printf(bp->bif_ifp,
576				"bpf_detach: ifpromisc failed (%d)\n", error);
577		}
578	}
579}
580
581/*
582 * Open ethernet device.  Returns ENXIO for illegal minor device number,
583 * EBUSY if file is open by another process.
584 */
585/* ARGSUSED */
586static	int
587bpfopen(struct cdev *dev, int flags, int fmt, struct thread *td)
588{
589	struct bpf_d *d;
590
591	mtx_lock(&bpf_mtx);
592	d = dev->si_drv1;
593	/*
594	 * Each minor can be opened by only one process.  If the requested
595	 * minor is in use, return EBUSY.
596	 */
597	if (d != NULL) {
598		mtx_unlock(&bpf_mtx);
599		return (EBUSY);
600	}
601	dev->si_drv1 = (struct bpf_d *)~0;	/* mark device in use */
602	mtx_unlock(&bpf_mtx);
603
604	if ((dev->si_flags & SI_NAMED) == 0)
605		make_dev(&bpf_cdevsw, minor(dev), UID_ROOT, GID_WHEEL, 0600,
606		    "bpf%d", dev2unit(dev));
607	MALLOC(d, struct bpf_d *, sizeof(*d), M_BPF, M_WAITOK | M_ZERO);
608	dev->si_drv1 = d;
609
610	/*
611	 * For historical reasons, perform a one-time initialization call to
612	 * the buffer routines, even though we're not yet committed to a
613	 * particular buffer method.
614	 */
615	bpf_buffer_init(d);
616	d->bd_bufmode = BPF_BUFMODE_BUFFER;
617	d->bd_sig = SIGIO;
618	d->bd_direction = BPF_D_INOUT;
619	d->bd_pid = td->td_proc->p_pid;
620#ifdef MAC
621	mac_bpfdesc_init(d);
622	mac_bpfdesc_create(td->td_ucred, d);
623#endif
624	mtx_init(&d->bd_mtx, devtoname(dev), "bpf cdev lock", MTX_DEF);
625	callout_init(&d->bd_callout, CALLOUT_MPSAFE);
626	knlist_init(&d->bd_sel.si_note, &d->bd_mtx, NULL, NULL, NULL);
627
628	return (0);
629}
630
631/*
632 * Close the descriptor by detaching it from its interface,
633 * deallocating its buffers, and marking it free.
634 */
635/* ARGSUSED */
636static	int
637bpfclose(struct cdev *dev, int flags, int fmt, struct thread *td)
638{
639	struct bpf_d *d = dev->si_drv1;
640
641	BPFD_LOCK(d);
642	if (d->bd_state == BPF_WAITING)
643		callout_stop(&d->bd_callout);
644	d->bd_state = BPF_IDLE;
645	BPFD_UNLOCK(d);
646	funsetown(&d->bd_sigio);
647	mtx_lock(&bpf_mtx);
648	if (d->bd_bif)
649		bpf_detachd(d);
650	mtx_unlock(&bpf_mtx);
651	selwakeuppri(&d->bd_sel, PRINET);
652#ifdef MAC
653	mac_bpfdesc_destroy(d);
654#endif /* MAC */
655	knlist_destroy(&d->bd_sel.si_note);
656	bpf_freed(d);
657	dev->si_drv1 = NULL;
658	free(d, M_BPF);
659
660	return (0);
661}
662
663/*
664 *  bpfread - read next chunk of packets from buffers
665 */
666static	int
667bpfread(struct cdev *dev, struct uio *uio, int ioflag)
668{
669	struct bpf_d *d = dev->si_drv1;
670	int timed_out;
671	int error;
672
673	/*
674	 * Restrict application to use a buffer the same size as
675	 * as kernel buffers.
676	 */
677	if (uio->uio_resid != d->bd_bufsize)
678		return (EINVAL);
679
680	BPFD_LOCK(d);
681	d->bd_pid = curthread->td_proc->p_pid;
682	if (d->bd_bufmode != BPF_BUFMODE_BUFFER) {
683		BPFD_UNLOCK(d);
684		return (EOPNOTSUPP);
685	}
686	if (d->bd_state == BPF_WAITING)
687		callout_stop(&d->bd_callout);
688	timed_out = (d->bd_state == BPF_TIMED_OUT);
689	d->bd_state = BPF_IDLE;
690	/*
691	 * If the hold buffer is empty, then do a timed sleep, which
692	 * ends when the timeout expires or when enough packets
693	 * have arrived to fill the store buffer.
694	 */
695	while (d->bd_hbuf == NULL) {
696		if ((d->bd_immediate || timed_out) && d->bd_slen != 0) {
697			/*
698			 * A packet(s) either arrived since the previous
699			 * read or arrived while we were asleep.
700			 * Rotate the buffers and return what's here.
701			 */
702			ROTATE_BUFFERS(d);
703			break;
704		}
705
706		/*
707		 * No data is available, check to see if the bpf device
708		 * is still pointed at a real interface.  If not, return
709		 * ENXIO so that the userland process knows to rebind
710		 * it before using it again.
711		 */
712		if (d->bd_bif == NULL) {
713			BPFD_UNLOCK(d);
714			return (ENXIO);
715		}
716
717		if (ioflag & O_NONBLOCK) {
718			BPFD_UNLOCK(d);
719			return (EWOULDBLOCK);
720		}
721		error = msleep(d, &d->bd_mtx, PRINET|PCATCH,
722		     "bpf", d->bd_rtout);
723		if (error == EINTR || error == ERESTART) {
724			BPFD_UNLOCK(d);
725			return (error);
726		}
727		if (error == EWOULDBLOCK) {
728			/*
729			 * On a timeout, return what's in the buffer,
730			 * which may be nothing.  If there is something
731			 * in the store buffer, we can rotate the buffers.
732			 */
733			if (d->bd_hbuf)
734				/*
735				 * We filled up the buffer in between
736				 * getting the timeout and arriving
737				 * here, so we don't need to rotate.
738				 */
739				break;
740
741			if (d->bd_slen == 0) {
742				BPFD_UNLOCK(d);
743				return (0);
744			}
745			ROTATE_BUFFERS(d);
746			break;
747		}
748	}
749	/*
750	 * At this point, we know we have something in the hold slot.
751	 */
752	BPFD_UNLOCK(d);
753
754	/*
755	 * Move data from hold buffer into user space.
756	 * We know the entire buffer is transferred since
757	 * we checked above that the read buffer is bpf_bufsize bytes.
758	 *
759	 * XXXRW: More synchronization needed here: what if a second thread
760	 * issues a read on the same fd at the same time?  Don't want this
761	 * getting invalidated.
762	 */
763	error = bpf_uiomove(d, d->bd_hbuf, d->bd_hlen, uio);
764
765	BPFD_LOCK(d);
766	d->bd_fbuf = d->bd_hbuf;
767	d->bd_hbuf = NULL;
768	d->bd_hlen = 0;
769	bpf_buf_reclaimed(d);
770	BPFD_UNLOCK(d);
771
772	return (error);
773}
774
775/*
776 * If there are processes sleeping on this descriptor, wake them up.
777 */
778static __inline void
779bpf_wakeup(struct bpf_d *d)
780{
781
782	BPFD_LOCK_ASSERT(d);
783	if (d->bd_state == BPF_WAITING) {
784		callout_stop(&d->bd_callout);
785		d->bd_state = BPF_IDLE;
786	}
787	wakeup(d);
788	if (d->bd_async && d->bd_sig && d->bd_sigio)
789		pgsigio(&d->bd_sigio, d->bd_sig, 0);
790
791	selwakeuppri(&d->bd_sel, PRINET);
792	KNOTE_LOCKED(&d->bd_sel.si_note, 0);
793}
794
795static void
796bpf_timed_out(void *arg)
797{
798	struct bpf_d *d = (struct bpf_d *)arg;
799
800	BPFD_LOCK(d);
801	if (d->bd_state == BPF_WAITING) {
802		d->bd_state = BPF_TIMED_OUT;
803		if (d->bd_slen != 0)
804			bpf_wakeup(d);
805	}
806	BPFD_UNLOCK(d);
807}
808
809static int
810bpf_ready(struct bpf_d *d)
811{
812
813	BPFD_LOCK_ASSERT(d);
814
815	if (!bpf_canfreebuf(d) && d->bd_hlen != 0)
816		return (1);
817	if ((d->bd_immediate || d->bd_state == BPF_TIMED_OUT) &&
818	    d->bd_slen != 0)
819		return (1);
820	return (0);
821}
822
823static int
824bpfwrite(struct cdev *dev, struct uio *uio, int ioflag)
825{
826	struct bpf_d *d = dev->si_drv1;
827	struct ifnet *ifp;
828	struct mbuf *m, *mc;
829	struct sockaddr dst;
830	int error, hlen;
831
832	d->bd_pid = curthread->td_proc->p_pid;
833	d->bd_wcount++;
834	if (d->bd_bif == NULL) {
835		d->bd_wdcount++;
836		return (ENXIO);
837	}
838
839	ifp = d->bd_bif->bif_ifp;
840
841	if ((ifp->if_flags & IFF_UP) == 0) {
842		d->bd_wdcount++;
843		return (ENETDOWN);
844	}
845
846	if (uio->uio_resid == 0) {
847		d->bd_wdcount++;
848		return (0);
849	}
850
851	bzero(&dst, sizeof(dst));
852	m = NULL;
853	hlen = 0;
854	error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, ifp,
855	    &m, &dst, &hlen, d->bd_wfilter);
856	if (error) {
857		d->bd_wdcount++;
858		return (error);
859	}
860	d->bd_wfcount++;
861	if (d->bd_hdrcmplt)
862		dst.sa_family = pseudo_AF_HDRCMPLT;
863
864	if (d->bd_feedback) {
865		mc = m_dup(m, M_DONTWAIT);
866		if (mc != NULL)
867			mc->m_pkthdr.rcvif = ifp;
868		/* Set M_PROMISC for outgoing packets to be discarded. */
869		if (d->bd_direction == BPF_D_INOUT)
870			m->m_flags |= M_PROMISC;
871	} else
872		mc = NULL;
873
874	m->m_pkthdr.len -= hlen;
875	m->m_len -= hlen;
876	m->m_data += hlen;	/* XXX */
877
878#ifdef MAC
879	BPFD_LOCK(d);
880	mac_bpfdesc_create_mbuf(d, m);
881	if (mc != NULL)
882		mac_bpfdesc_create_mbuf(d, mc);
883	BPFD_UNLOCK(d);
884#endif
885
886	error = (*ifp->if_output)(ifp, m, &dst, NULL);
887	if (error)
888		d->bd_wdcount++;
889
890	if (mc != NULL) {
891		if (error == 0)
892			(*ifp->if_input)(ifp, mc);
893		else
894			m_freem(mc);
895	}
896
897	return (error);
898}
899
900/*
901 * Reset a descriptor by flushing its packet buffer and clearing the
902 * receive and drop counts.
903 */
904static void
905reset_d(struct bpf_d *d)
906{
907
908	mtx_assert(&d->bd_mtx, MA_OWNED);
909	if (d->bd_hbuf) {
910		/* Free the hold buffer. */
911		d->bd_fbuf = d->bd_hbuf;
912		d->bd_hbuf = NULL;
913		bpf_buf_reclaimed(d);
914	}
915	d->bd_slen = 0;
916	d->bd_hlen = 0;
917	d->bd_rcount = 0;
918	d->bd_dcount = 0;
919	d->bd_fcount = 0;
920	d->bd_wcount = 0;
921	d->bd_wfcount = 0;
922	d->bd_wdcount = 0;
923	d->bd_zcopy = 0;
924}
925
926/*
927 *  FIONREAD		Check for read packet available.
928 *  SIOCGIFADDR		Get interface address - convenient hook to driver.
929 *  BIOCGBLEN		Get buffer len [for read()].
930 *  BIOCSETF		Set read filter.
931 *  BIOCSETFNR		Set read filter without resetting descriptor.
932 *  BIOCSETWF		Set write filter.
933 *  BIOCFLUSH		Flush read packet buffer.
934 *  BIOCPROMISC		Put interface into promiscuous mode.
935 *  BIOCGDLT		Get link layer type.
936 *  BIOCGETIF		Get interface name.
937 *  BIOCSETIF		Set interface.
938 *  BIOCSRTIMEOUT	Set read timeout.
939 *  BIOCGRTIMEOUT	Get read timeout.
940 *  BIOCGSTATS		Get packet stats.
941 *  BIOCIMMEDIATE	Set immediate mode.
942 *  BIOCVERSION		Get filter language version.
943 *  BIOCGHDRCMPLT	Get "header already complete" flag
944 *  BIOCSHDRCMPLT	Set "header already complete" flag
945 *  BIOCGDIRECTION	Get packet direction flag
946 *  BIOCSDIRECTION	Set packet direction flag
947 *  BIOCLOCK		Set "locked" flag
948 *  BIOCFEEDBACK	Set packet feedback mode.
949 *  BIOCSETZBUF		Set current zero-copy buffer locations.
950 *  BIOCGETZMAX		Get maximum zero-copy buffer size.
951 *  BIOCROTZBUF		Force rotation of zero-copy buffer
952 *  BIOCSETBUFMODE	Set buffer mode.
953 *  BIOCGETBUFMODE	Get current buffer mode.
954 */
955/* ARGSUSED */
956static	int
957bpfioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags,
958    struct thread *td)
959{
960	struct bpf_d *d = dev->si_drv1;
961	int error = 0;
962
963	/*
964	 * Refresh PID associated with this descriptor.
965	 */
966	BPFD_LOCK(d);
967	d->bd_pid = td->td_proc->p_pid;
968	if (d->bd_state == BPF_WAITING)
969		callout_stop(&d->bd_callout);
970	d->bd_state = BPF_IDLE;
971	BPFD_UNLOCK(d);
972
973	if (d->bd_locked == 1) {
974		switch (cmd) {
975		case BIOCGBLEN:
976		case BIOCFLUSH:
977		case BIOCGDLT:
978		case BIOCGDLTLIST:
979		case BIOCGETIF:
980		case BIOCGRTIMEOUT:
981		case BIOCGSTATS:
982		case BIOCVERSION:
983		case BIOCGRSIG:
984		case BIOCGHDRCMPLT:
985		case BIOCFEEDBACK:
986		case FIONREAD:
987		case BIOCLOCK:
988		case BIOCSRTIMEOUT:
989		case BIOCIMMEDIATE:
990		case TIOCGPGRP:
991		case BIOCROTZBUF:
992			break;
993		default:
994			return (EPERM);
995		}
996	}
997	switch (cmd) {
998
999	default:
1000		error = EINVAL;
1001		break;
1002
1003	/*
1004	 * Check for read packet available.
1005	 */
1006	case FIONREAD:
1007		{
1008			int n;
1009
1010			BPFD_LOCK(d);
1011			n = d->bd_slen;
1012			if (d->bd_hbuf)
1013				n += d->bd_hlen;
1014			BPFD_UNLOCK(d);
1015
1016			*(int *)addr = n;
1017			break;
1018		}
1019
1020	case SIOCGIFADDR:
1021		{
1022			struct ifnet *ifp;
1023
1024			if (d->bd_bif == NULL)
1025				error = EINVAL;
1026			else {
1027				ifp = d->bd_bif->bif_ifp;
1028				error = (*ifp->if_ioctl)(ifp, cmd, addr);
1029			}
1030			break;
1031		}
1032
1033	/*
1034	 * Get buffer len [for read()].
1035	 */
1036	case BIOCGBLEN:
1037		*(u_int *)addr = d->bd_bufsize;
1038		break;
1039
1040	/*
1041	 * Set buffer length.
1042	 */
1043	case BIOCSBLEN:
1044		error = bpf_ioctl_sblen(d, (u_int *)addr);
1045		break;
1046
1047	/*
1048	 * Set link layer read filter.
1049	 */
1050	case BIOCSETF:
1051	case BIOCSETFNR:
1052	case BIOCSETWF:
1053		error = bpf_setf(d, (struct bpf_program *)addr, cmd);
1054		break;
1055
1056	/*
1057	 * Flush read packet buffer.
1058	 */
1059	case BIOCFLUSH:
1060		BPFD_LOCK(d);
1061		reset_d(d);
1062		BPFD_UNLOCK(d);
1063		break;
1064
1065	/*
1066	 * Put interface into promiscuous mode.
1067	 */
1068	case BIOCPROMISC:
1069		if (d->bd_bif == NULL) {
1070			/*
1071			 * No interface attached yet.
1072			 */
1073			error = EINVAL;
1074			break;
1075		}
1076		if (d->bd_promisc == 0) {
1077			error = ifpromisc(d->bd_bif->bif_ifp, 1);
1078			if (error == 0)
1079				d->bd_promisc = 1;
1080		}
1081		break;
1082
1083	/*
1084	 * Get current data link type.
1085	 */
1086	case BIOCGDLT:
1087		if (d->bd_bif == NULL)
1088			error = EINVAL;
1089		else
1090			*(u_int *)addr = d->bd_bif->bif_dlt;
1091		break;
1092
1093	/*
1094	 * Get a list of supported data link types.
1095	 */
1096	case BIOCGDLTLIST:
1097		if (d->bd_bif == NULL)
1098			error = EINVAL;
1099		else
1100			error = bpf_getdltlist(d, (struct bpf_dltlist *)addr);
1101		break;
1102
1103	/*
1104	 * Set data link type.
1105	 */
1106	case BIOCSDLT:
1107		if (d->bd_bif == NULL)
1108			error = EINVAL;
1109		else
1110			error = bpf_setdlt(d, *(u_int *)addr);
1111		break;
1112
1113	/*
1114	 * Get interface name.
1115	 */
1116	case BIOCGETIF:
1117		if (d->bd_bif == NULL)
1118			error = EINVAL;
1119		else {
1120			struct ifnet *const ifp = d->bd_bif->bif_ifp;
1121			struct ifreq *const ifr = (struct ifreq *)addr;
1122
1123			strlcpy(ifr->ifr_name, ifp->if_xname,
1124			    sizeof(ifr->ifr_name));
1125		}
1126		break;
1127
1128	/*
1129	 * Set interface.
1130	 */
1131	case BIOCSETIF:
1132		error = bpf_setif(d, (struct ifreq *)addr);
1133		break;
1134
1135	/*
1136	 * Set read timeout.
1137	 */
1138	case BIOCSRTIMEOUT:
1139		{
1140			struct timeval *tv = (struct timeval *)addr;
1141
1142			/*
1143			 * Subtract 1 tick from tvtohz() since this isn't
1144			 * a one-shot timer.
1145			 */
1146			if ((error = itimerfix(tv)) == 0)
1147				d->bd_rtout = tvtohz(tv) - 1;
1148			break;
1149		}
1150
1151	/*
1152	 * Get read timeout.
1153	 */
1154	case BIOCGRTIMEOUT:
1155		{
1156			struct timeval *tv = (struct timeval *)addr;
1157
1158			tv->tv_sec = d->bd_rtout / hz;
1159			tv->tv_usec = (d->bd_rtout % hz) * tick;
1160			break;
1161		}
1162
1163	/*
1164	 * Get packet stats.
1165	 */
1166	case BIOCGSTATS:
1167		{
1168			struct bpf_stat *bs = (struct bpf_stat *)addr;
1169
1170			/* XXXCSJP overflow */
1171			bs->bs_recv = d->bd_rcount;
1172			bs->bs_drop = d->bd_dcount;
1173			break;
1174		}
1175
1176	/*
1177	 * Set immediate mode.
1178	 */
1179	case BIOCIMMEDIATE:
1180		d->bd_immediate = *(u_int *)addr;
1181		break;
1182
1183	case BIOCVERSION:
1184		{
1185			struct bpf_version *bv = (struct bpf_version *)addr;
1186
1187			bv->bv_major = BPF_MAJOR_VERSION;
1188			bv->bv_minor = BPF_MINOR_VERSION;
1189			break;
1190		}
1191
1192	/*
1193	 * Get "header already complete" flag
1194	 */
1195	case BIOCGHDRCMPLT:
1196		*(u_int *)addr = d->bd_hdrcmplt;
1197		break;
1198
1199	/*
1200	 * Set "header already complete" flag
1201	 */
1202	case BIOCSHDRCMPLT:
1203		d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
1204		break;
1205
1206	/*
1207	 * Get packet direction flag
1208	 */
1209	case BIOCGDIRECTION:
1210		*(u_int *)addr = d->bd_direction;
1211		break;
1212
1213	/*
1214	 * Set packet direction flag
1215	 */
1216	case BIOCSDIRECTION:
1217		{
1218			u_int	direction;
1219
1220			direction = *(u_int *)addr;
1221			switch (direction) {
1222			case BPF_D_IN:
1223			case BPF_D_INOUT:
1224			case BPF_D_OUT:
1225				d->bd_direction = direction;
1226				break;
1227			default:
1228				error = EINVAL;
1229			}
1230		}
1231		break;
1232
1233	case BIOCFEEDBACK:
1234		d->bd_feedback = *(u_int *)addr;
1235		break;
1236
1237	case BIOCLOCK:
1238		d->bd_locked = 1;
1239		break;
1240
1241	case FIONBIO:		/* Non-blocking I/O */
1242		break;
1243
1244	case FIOASYNC:		/* Send signal on receive packets */
1245		d->bd_async = *(int *)addr;
1246		break;
1247
1248	case FIOSETOWN:
1249		error = fsetown(*(int *)addr, &d->bd_sigio);
1250		break;
1251
1252	case FIOGETOWN:
1253		*(int *)addr = fgetown(&d->bd_sigio);
1254		break;
1255
1256	/* This is deprecated, FIOSETOWN should be used instead. */
1257	case TIOCSPGRP:
1258		error = fsetown(-(*(int *)addr), &d->bd_sigio);
1259		break;
1260
1261	/* This is deprecated, FIOGETOWN should be used instead. */
1262	case TIOCGPGRP:
1263		*(int *)addr = -fgetown(&d->bd_sigio);
1264		break;
1265
1266	case BIOCSRSIG:		/* Set receive signal */
1267		{
1268			u_int sig;
1269
1270			sig = *(u_int *)addr;
1271
1272			if (sig >= NSIG)
1273				error = EINVAL;
1274			else
1275				d->bd_sig = sig;
1276			break;
1277		}
1278	case BIOCGRSIG:
1279		*(u_int *)addr = d->bd_sig;
1280		break;
1281
1282	case BIOCGETBUFMODE:
1283		*(u_int *)addr = d->bd_bufmode;
1284		break;
1285
1286	case BIOCSETBUFMODE:
1287		/*
1288		 * Allow the buffering mode to be changed as long as we
1289		 * haven't yet committed to a particular mode.  Our
1290		 * definition of commitment, for now, is whether or not a
1291		 * buffer has been allocated or an interface attached, since
1292		 * that's the point where things get tricky.
1293		 */
1294		switch (*(u_int *)addr) {
1295		case BPF_BUFMODE_BUFFER:
1296			break;
1297
1298		case BPF_BUFMODE_ZBUF:
1299			if (bpf_zerocopy_enable)
1300				break;
1301			/* FALLSTHROUGH */
1302
1303		default:
1304			return (EINVAL);
1305		}
1306
1307		BPFD_LOCK(d);
1308		if (d->bd_sbuf != NULL || d->bd_hbuf != NULL ||
1309		    d->bd_fbuf != NULL || d->bd_bif != NULL) {
1310			BPFD_UNLOCK(d);
1311			return (EBUSY);
1312		}
1313		d->bd_bufmode = *(u_int *)addr;
1314		BPFD_UNLOCK(d);
1315		break;
1316
1317	case BIOCGETZMAX:
1318		return (bpf_ioctl_getzmax(td, d, (size_t *)addr));
1319
1320	case BIOCSETZBUF:
1321		return (bpf_ioctl_setzbuf(td, d, (struct bpf_zbuf *)addr));
1322
1323	case BIOCROTZBUF:
1324		return (bpf_ioctl_rotzbuf(td, d, (struct bpf_zbuf *)addr));
1325	}
1326	return (error);
1327}
1328
1329/*
1330 * Set d's packet filter program to fp.  If this file already has a filter,
1331 * free it and replace it.  Returns EINVAL for bogus requests.
1332 */
1333static int
1334bpf_setf(struct bpf_d *d, struct bpf_program *fp, u_long cmd)
1335{
1336	struct bpf_insn *fcode, *old;
1337	u_int wfilter, flen, size;
1338#ifdef BPF_JITTER
1339	bpf_jit_filter *ofunc;
1340#endif
1341
1342	if (cmd == BIOCSETWF) {
1343		old = d->bd_wfilter;
1344		wfilter = 1;
1345#ifdef BPF_JITTER
1346		ofunc = NULL;
1347#endif
1348	} else {
1349		wfilter = 0;
1350		old = d->bd_rfilter;
1351#ifdef BPF_JITTER
1352		ofunc = d->bd_bfilter;
1353#endif
1354	}
1355	if (fp->bf_insns == NULL) {
1356		if (fp->bf_len != 0)
1357			return (EINVAL);
1358		BPFD_LOCK(d);
1359		if (wfilter)
1360			d->bd_wfilter = NULL;
1361		else {
1362			d->bd_rfilter = NULL;
1363#ifdef BPF_JITTER
1364			d->bd_bfilter = NULL;
1365#endif
1366			if (cmd == BIOCSETF)
1367				reset_d(d);
1368		}
1369		BPFD_UNLOCK(d);
1370		if (old != NULL)
1371			free((caddr_t)old, M_BPF);
1372#ifdef BPF_JITTER
1373		if (ofunc != NULL)
1374			bpf_destroy_jit_filter(ofunc);
1375#endif
1376		return (0);
1377	}
1378	flen = fp->bf_len;
1379	if (flen > bpf_maxinsns)
1380		return (EINVAL);
1381
1382	size = flen * sizeof(*fp->bf_insns);
1383	fcode = (struct bpf_insn *)malloc(size, M_BPF, M_WAITOK);
1384	if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
1385	    bpf_validate(fcode, (int)flen)) {
1386		BPFD_LOCK(d);
1387		if (wfilter)
1388			d->bd_wfilter = fcode;
1389		else {
1390			d->bd_rfilter = fcode;
1391#ifdef BPF_JITTER
1392			d->bd_bfilter = bpf_jitter(fcode, flen);
1393#endif
1394			if (cmd == BIOCSETF)
1395				reset_d(d);
1396		}
1397		BPFD_UNLOCK(d);
1398		if (old != NULL)
1399			free((caddr_t)old, M_BPF);
1400#ifdef BPF_JITTER
1401		if (ofunc != NULL)
1402			bpf_destroy_jit_filter(ofunc);
1403#endif
1404
1405		return (0);
1406	}
1407	free((caddr_t)fcode, M_BPF);
1408	return (EINVAL);
1409}
1410
1411/*
1412 * Detach a file from its current interface (if attached at all) and attach
1413 * to the interface indicated by the name stored in ifr.
1414 * Return an errno or 0.
1415 */
1416static int
1417bpf_setif(struct bpf_d *d, struct ifreq *ifr)
1418{
1419	struct bpf_if *bp;
1420	struct ifnet *theywant;
1421
1422	theywant = ifunit(ifr->ifr_name);
1423	if (theywant == NULL || theywant->if_bpf == NULL)
1424		return (ENXIO);
1425
1426	bp = theywant->if_bpf;
1427
1428	/*
1429	 * Behavior here depends on the buffering model.  If we're using
1430	 * kernel memory buffers, then we can allocate them here.  If we're
1431	 * using zero-copy, then the user process must have registered
1432	 * buffers by the time we get here.  If not, return an error.
1433	 *
1434	 * XXXRW: There are locking issues here with multi-threaded use: what
1435	 * if two threads try to set the interface at once?
1436	 */
1437	switch (d->bd_bufmode) {
1438	case BPF_BUFMODE_BUFFER:
1439		if (d->bd_sbuf == NULL)
1440			bpf_buffer_alloc(d);
1441		KASSERT(d->bd_sbuf != NULL, ("bpf_setif: bd_sbuf NULL"));
1442		break;
1443
1444	case BPF_BUFMODE_ZBUF:
1445		if (d->bd_sbuf == NULL)
1446			return (EINVAL);
1447		break;
1448
1449	default:
1450		panic("bpf_setif: bufmode %d", d->bd_bufmode);
1451	}
1452	if (bp != d->bd_bif) {
1453		if (d->bd_bif)
1454			/*
1455			 * Detach if attached to something else.
1456			 */
1457			bpf_detachd(d);
1458
1459		bpf_attachd(d, bp);
1460	}
1461	BPFD_LOCK(d);
1462	reset_d(d);
1463	BPFD_UNLOCK(d);
1464	return (0);
1465}
1466
1467/*
1468 * Support for select() and poll() system calls
1469 *
1470 * Return true iff the specific operation will not block indefinitely.
1471 * Otherwise, return false but make a note that a selwakeup() must be done.
1472 */
1473static int
1474bpfpoll(struct cdev *dev, int events, struct thread *td)
1475{
1476	struct bpf_d *d;
1477	int revents;
1478
1479	d = dev->si_drv1;
1480	if (d->bd_bif == NULL)
1481		return (ENXIO);
1482
1483	/*
1484	 * Refresh PID associated with this descriptor.
1485	 */
1486	revents = events & (POLLOUT | POLLWRNORM);
1487	BPFD_LOCK(d);
1488	d->bd_pid = td->td_proc->p_pid;
1489	if (events & (POLLIN | POLLRDNORM)) {
1490		if (bpf_ready(d))
1491			revents |= events & (POLLIN | POLLRDNORM);
1492		else {
1493			selrecord(td, &d->bd_sel);
1494			/* Start the read timeout if necessary. */
1495			if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
1496				callout_reset(&d->bd_callout, d->bd_rtout,
1497				    bpf_timed_out, d);
1498				d->bd_state = BPF_WAITING;
1499			}
1500		}
1501	}
1502	BPFD_UNLOCK(d);
1503	return (revents);
1504}
1505
1506/*
1507 * Support for kevent() system call.  Register EVFILT_READ filters and
1508 * reject all others.
1509 */
1510int
1511bpfkqfilter(struct cdev *dev, struct knote *kn)
1512{
1513	struct bpf_d *d = (struct bpf_d *)dev->si_drv1;
1514
1515	if (kn->kn_filter != EVFILT_READ)
1516		return (1);
1517
1518	/*
1519	 * Refresh PID associated with this descriptor.
1520	 */
1521	BPFD_LOCK(d);
1522	d->bd_pid = curthread->td_proc->p_pid;
1523	kn->kn_fop = &bpfread_filtops;
1524	kn->kn_hook = d;
1525	knlist_add(&d->bd_sel.si_note, kn, 1);
1526	BPFD_UNLOCK(d);
1527
1528	return (0);
1529}
1530
1531static void
1532filt_bpfdetach(struct knote *kn)
1533{
1534	struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
1535
1536	knlist_remove(&d->bd_sel.si_note, kn, 0);
1537}
1538
1539static int
1540filt_bpfread(struct knote *kn, long hint)
1541{
1542	struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
1543	int ready;
1544
1545	BPFD_LOCK_ASSERT(d);
1546	ready = bpf_ready(d);
1547	if (ready) {
1548		kn->kn_data = d->bd_slen;
1549		if (d->bd_hbuf)
1550			kn->kn_data += d->bd_hlen;
1551	}
1552	else if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
1553		callout_reset(&d->bd_callout, d->bd_rtout,
1554		    bpf_timed_out, d);
1555		d->bd_state = BPF_WAITING;
1556	}
1557
1558	return (ready);
1559}
1560
1561/*
1562 * Incoming linkage from device drivers.  Process the packet pkt, of length
1563 * pktlen, which is stored in a contiguous buffer.  The packet is parsed
1564 * by each process' filter, and if accepted, stashed into the corresponding
1565 * buffer.
1566 */
1567void
1568bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen)
1569{
1570	struct bpf_d *d;
1571	u_int slen;
1572	int gottime;
1573	struct timeval tv;
1574
1575	gottime = 0;
1576	BPFIF_LOCK(bp);
1577	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1578		BPFD_LOCK(d);
1579		++d->bd_rcount;
1580#ifdef BPF_JITTER
1581		if (bpf_jitter_enable != 0 && d->bd_bfilter != NULL)
1582			slen = (*(d->bd_bfilter->func))(pkt, pktlen, pktlen);
1583		else
1584#endif
1585		slen = bpf_filter(d->bd_rfilter, pkt, pktlen, pktlen);
1586		if (slen != 0) {
1587			d->bd_fcount++;
1588			if (!gottime) {
1589				microtime(&tv);
1590				gottime = 1;
1591			}
1592#ifdef MAC
1593			if (mac_bpfdesc_check_receive(d, bp->bif_ifp) == 0)
1594#endif
1595				catchpacket(d, pkt, pktlen, slen,
1596				    bpf_append_bytes, &tv);
1597		}
1598		BPFD_UNLOCK(d);
1599	}
1600	BPFIF_UNLOCK(bp);
1601}
1602
1603#define	BPF_CHECK_DIRECTION(d, r, i)				\
1604	    (((d)->bd_direction == BPF_D_IN && (r) != (i)) ||	\
1605	    ((d)->bd_direction == BPF_D_OUT && (r) == (i)))
1606
1607/*
1608 * Incoming linkage from device drivers, when packet is in an mbuf chain.
1609 */
1610void
1611bpf_mtap(struct bpf_if *bp, struct mbuf *m)
1612{
1613	struct bpf_d *d;
1614	u_int pktlen, slen;
1615	int gottime;
1616	struct timeval tv;
1617
1618	/* Skip outgoing duplicate packets. */
1619	if ((m->m_flags & M_PROMISC) != 0 && m->m_pkthdr.rcvif == NULL) {
1620		m->m_flags &= ~M_PROMISC;
1621		return;
1622	}
1623
1624	gottime = 0;
1625
1626	pktlen = m_length(m, NULL);
1627
1628	BPFIF_LOCK(bp);
1629	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1630		if (BPF_CHECK_DIRECTION(d, m->m_pkthdr.rcvif, bp->bif_ifp))
1631			continue;
1632		BPFD_LOCK(d);
1633		++d->bd_rcount;
1634#ifdef BPF_JITTER
1635		/* XXX We cannot handle multiple mbufs. */
1636		if (bpf_jitter_enable != 0 && d->bd_bfilter != NULL &&
1637		    m->m_next == NULL)
1638			slen = (*(d->bd_bfilter->func))(mtod(m, u_char *),
1639			    pktlen, pktlen);
1640		else
1641#endif
1642		slen = bpf_filter(d->bd_rfilter, (u_char *)m, pktlen, 0);
1643		if (slen != 0) {
1644			d->bd_fcount++;
1645			if (!gottime) {
1646				microtime(&tv);
1647				gottime = 1;
1648			}
1649#ifdef MAC
1650			if (mac_bpfdesc_check_receive(d, bp->bif_ifp) == 0)
1651#endif
1652				catchpacket(d, (u_char *)m, pktlen, slen,
1653				    bpf_append_mbuf, &tv);
1654		}
1655		BPFD_UNLOCK(d);
1656	}
1657	BPFIF_UNLOCK(bp);
1658}
1659
1660/*
1661 * Incoming linkage from device drivers, when packet is in
1662 * an mbuf chain and to be prepended by a contiguous header.
1663 */
1664void
1665bpf_mtap2(struct bpf_if *bp, void *data, u_int dlen, struct mbuf *m)
1666{
1667	struct mbuf mb;
1668	struct bpf_d *d;
1669	u_int pktlen, slen;
1670	int gottime;
1671	struct timeval tv;
1672
1673	/* Skip outgoing duplicate packets. */
1674	if ((m->m_flags & M_PROMISC) != 0 && m->m_pkthdr.rcvif == NULL) {
1675		m->m_flags &= ~M_PROMISC;
1676		return;
1677	}
1678
1679	gottime = 0;
1680
1681	pktlen = m_length(m, NULL);
1682	/*
1683	 * Craft on-stack mbuf suitable for passing to bpf_filter.
1684	 * Note that we cut corners here; we only setup what's
1685	 * absolutely needed--this mbuf should never go anywhere else.
1686	 */
1687	mb.m_next = m;
1688	mb.m_data = data;
1689	mb.m_len = dlen;
1690	pktlen += dlen;
1691
1692	BPFIF_LOCK(bp);
1693	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1694		if (BPF_CHECK_DIRECTION(d, m->m_pkthdr.rcvif, bp->bif_ifp))
1695			continue;
1696		BPFD_LOCK(d);
1697		++d->bd_rcount;
1698		slen = bpf_filter(d->bd_rfilter, (u_char *)&mb, pktlen, 0);
1699		if (slen != 0) {
1700			d->bd_fcount++;
1701			if (!gottime) {
1702				microtime(&tv);
1703				gottime = 1;
1704			}
1705#ifdef MAC
1706			if (mac_bpfdesc_check_receive(d, bp->bif_ifp) == 0)
1707#endif
1708				catchpacket(d, (u_char *)&mb, pktlen, slen,
1709				    bpf_append_mbuf, &tv);
1710		}
1711		BPFD_UNLOCK(d);
1712	}
1713	BPFIF_UNLOCK(bp);
1714}
1715
1716#undef	BPF_CHECK_DIRECTION
1717
1718/*
1719 * Move the packet data from interface memory (pkt) into the
1720 * store buffer.  "cpfn" is the routine called to do the actual data
1721 * transfer.  bcopy is passed in to copy contiguous chunks, while
1722 * bpf_append_mbuf is passed in to copy mbuf chains.  In the latter case,
1723 * pkt is really an mbuf.
1724 */
1725static void
1726catchpacket(struct bpf_d *d, u_char *pkt, u_int pktlen, u_int snaplen,
1727    void (*cpfn)(struct bpf_d *, caddr_t, u_int, void *, u_int),
1728    struct timeval *tv)
1729{
1730	struct bpf_hdr hdr;
1731	int totlen, curlen;
1732	int hdrlen = d->bd_bif->bif_hdrlen;
1733	int do_wakeup = 0;
1734
1735	BPFD_LOCK_ASSERT(d);
1736
1737	/*
1738	 * Detect whether user space has released a buffer back to us, and if
1739	 * so, move it from being a hold buffer to a free buffer.  This may
1740	 * not be the best place to do it (for example, we might only want to
1741	 * run this check if we need the space), but for now it's a reliable
1742	 * spot to do it.
1743	 */
1744	if (d->bd_fbuf == NULL && bpf_canfreebuf(d)) {
1745		d->bd_fbuf = d->bd_hbuf;
1746		d->bd_hbuf = NULL;
1747		d->bd_hlen = 0;
1748		bpf_buf_reclaimed(d);
1749	}
1750
1751	/*
1752	 * Figure out how many bytes to move.  If the packet is
1753	 * greater or equal to the snapshot length, transfer that
1754	 * much.  Otherwise, transfer the whole packet (unless
1755	 * we hit the buffer size limit).
1756	 */
1757	totlen = hdrlen + min(snaplen, pktlen);
1758	if (totlen > d->bd_bufsize)
1759		totlen = d->bd_bufsize;
1760
1761	/*
1762	 * Round up the end of the previous packet to the next longword.
1763	 *
1764	 * Drop the packet if there's no room and no hope of room
1765	 * If the packet would overflow the storage buffer or the storage
1766	 * buffer is considered immutable by the buffer model, try to rotate
1767	 * the buffer and wakeup pending processes.
1768	 */
1769	curlen = BPF_WORDALIGN(d->bd_slen);
1770	if (curlen + totlen > d->bd_bufsize || !bpf_canwritebuf(d)) {
1771		if (d->bd_fbuf == NULL) {
1772			/*
1773			 * There's no room in the store buffer, and no
1774			 * prospect of room, so drop the packet.  Notify the
1775			 * buffer model.
1776			 */
1777			bpf_buffull(d);
1778			++d->bd_dcount;
1779			return;
1780		}
1781		ROTATE_BUFFERS(d);
1782		do_wakeup = 1;
1783		curlen = 0;
1784	} else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT)
1785		/*
1786		 * Immediate mode is set, or the read timeout has already
1787		 * expired during a select call.  A packet arrived, so the
1788		 * reader should be woken up.
1789		 */
1790		do_wakeup = 1;
1791
1792	/*
1793	 * Append the bpf header.  Note we append the actual header size, but
1794	 * move forward the length of the header plus padding.
1795	 */
1796	bzero(&hdr, sizeof(hdr));
1797	hdr.bh_tstamp = *tv;
1798	hdr.bh_datalen = pktlen;
1799	hdr.bh_hdrlen = hdrlen;
1800	hdr.bh_caplen = totlen - hdrlen;
1801	bpf_append_bytes(d, d->bd_sbuf, curlen, &hdr, sizeof(hdr));
1802
1803	/*
1804	 * Copy the packet data into the store buffer and update its length.
1805	 */
1806	(*cpfn)(d, d->bd_sbuf, curlen + hdrlen, pkt, hdr.bh_caplen);
1807	d->bd_slen = curlen + totlen;
1808
1809	if (do_wakeup)
1810		bpf_wakeup(d);
1811}
1812
1813/*
1814 * Free buffers currently in use by a descriptor.
1815 * Called on close.
1816 */
1817static void
1818bpf_freed(struct bpf_d *d)
1819{
1820
1821	/*
1822	 * We don't need to lock out interrupts since this descriptor has
1823	 * been detached from its interface and it yet hasn't been marked
1824	 * free.
1825	 */
1826	bpf_free(d);
1827	if (d->bd_rfilter) {
1828		free((caddr_t)d->bd_rfilter, M_BPF);
1829#ifdef BPF_JITTER
1830		bpf_destroy_jit_filter(d->bd_bfilter);
1831#endif
1832	}
1833	if (d->bd_wfilter)
1834		free((caddr_t)d->bd_wfilter, M_BPF);
1835	mtx_destroy(&d->bd_mtx);
1836}
1837
1838/*
1839 * Attach an interface to bpf.  dlt is the link layer type; hdrlen is the
1840 * fixed size of the link header (variable length headers not yet supported).
1841 */
1842void
1843bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen)
1844{
1845
1846	bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
1847}
1848
1849/*
1850 * Attach an interface to bpf.  ifp is a pointer to the structure
1851 * defining the interface to be attached, dlt is the link layer type,
1852 * and hdrlen is the fixed size of the link header (variable length
1853 * headers are not yet supporrted).
1854 */
1855void
1856bpfattach2(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp)
1857{
1858	struct bpf_if *bp;
1859
1860	bp = malloc(sizeof(*bp), M_BPF, M_NOWAIT | M_ZERO);
1861	if (bp == NULL)
1862		panic("bpfattach");
1863
1864	LIST_INIT(&bp->bif_dlist);
1865	bp->bif_ifp = ifp;
1866	bp->bif_dlt = dlt;
1867	mtx_init(&bp->bif_mtx, "bpf interface lock", NULL, MTX_DEF);
1868	KASSERT(*driverp == NULL, ("bpfattach2: driverp already initialized"));
1869	*driverp = bp;
1870
1871	mtx_lock(&bpf_mtx);
1872	LIST_INSERT_HEAD(&bpf_iflist, bp, bif_next);
1873	mtx_unlock(&bpf_mtx);
1874
1875	/*
1876	 * Compute the length of the bpf header.  This is not necessarily
1877	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1878	 * that the network layer header begins on a longword boundary (for
1879	 * performance reasons and to alleviate alignment restrictions).
1880	 */
1881	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1882
1883	if (bootverbose)
1884		if_printf(ifp, "bpf attached\n");
1885}
1886
1887/*
1888 * Detach bpf from an interface.  This involves detaching each descriptor
1889 * associated with the interface, and leaving bd_bif NULL.  Notify each
1890 * descriptor as it's detached so that any sleepers wake up and get
1891 * ENXIO.
1892 */
1893void
1894bpfdetach(struct ifnet *ifp)
1895{
1896	struct bpf_if	*bp;
1897	struct bpf_d	*d;
1898
1899	/* Locate BPF interface information */
1900	mtx_lock(&bpf_mtx);
1901	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1902		if (ifp == bp->bif_ifp)
1903			break;
1904	}
1905
1906	/* Interface wasn't attached */
1907	if ((bp == NULL) || (bp->bif_ifp == NULL)) {
1908		mtx_unlock(&bpf_mtx);
1909		printf("bpfdetach: %s was not attached\n", ifp->if_xname);
1910		return;
1911	}
1912
1913	LIST_REMOVE(bp, bif_next);
1914	mtx_unlock(&bpf_mtx);
1915
1916	while ((d = LIST_FIRST(&bp->bif_dlist)) != NULL) {
1917		bpf_detachd(d);
1918		BPFD_LOCK(d);
1919		bpf_wakeup(d);
1920		BPFD_UNLOCK(d);
1921	}
1922
1923	mtx_destroy(&bp->bif_mtx);
1924	free(bp, M_BPF);
1925}
1926
1927/*
1928 * Get a list of available data link type of the interface.
1929 */
1930static int
1931bpf_getdltlist(struct bpf_d *d, struct bpf_dltlist *bfl)
1932{
1933	int n, error;
1934	struct ifnet *ifp;
1935	struct bpf_if *bp;
1936
1937	ifp = d->bd_bif->bif_ifp;
1938	n = 0;
1939	error = 0;
1940	mtx_lock(&bpf_mtx);
1941	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1942		if (bp->bif_ifp != ifp)
1943			continue;
1944		if (bfl->bfl_list != NULL) {
1945			if (n >= bfl->bfl_len) {
1946				mtx_unlock(&bpf_mtx);
1947				return (ENOMEM);
1948			}
1949			error = copyout(&bp->bif_dlt,
1950			    bfl->bfl_list + n, sizeof(u_int));
1951		}
1952		n++;
1953	}
1954	mtx_unlock(&bpf_mtx);
1955	bfl->bfl_len = n;
1956	return (error);
1957}
1958
1959/*
1960 * Set the data link type of a BPF instance.
1961 */
1962static int
1963bpf_setdlt(struct bpf_d *d, u_int dlt)
1964{
1965	int error, opromisc;
1966	struct ifnet *ifp;
1967	struct bpf_if *bp;
1968
1969	if (d->bd_bif->bif_dlt == dlt)
1970		return (0);
1971	ifp = d->bd_bif->bif_ifp;
1972	mtx_lock(&bpf_mtx);
1973	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1974		if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
1975			break;
1976	}
1977	mtx_unlock(&bpf_mtx);
1978	if (bp != NULL) {
1979		opromisc = d->bd_promisc;
1980		bpf_detachd(d);
1981		bpf_attachd(d, bp);
1982		BPFD_LOCK(d);
1983		reset_d(d);
1984		BPFD_UNLOCK(d);
1985		if (opromisc) {
1986			error = ifpromisc(bp->bif_ifp, 1);
1987			if (error)
1988				if_printf(bp->bif_ifp,
1989					"bpf_setdlt: ifpromisc failed (%d)\n",
1990					error);
1991			else
1992				d->bd_promisc = 1;
1993		}
1994	}
1995	return (bp == NULL ? EINVAL : 0);
1996}
1997
1998static void
1999bpf_clone(void *arg, struct ucred *cred, char *name, int namelen,
2000    struct cdev **dev)
2001{
2002	int u;
2003
2004	if (*dev != NULL)
2005		return;
2006	if (dev_stdclone(name, NULL, "bpf", &u) != 1)
2007		return;
2008	*dev = make_dev(&bpf_cdevsw, unit2minor(u), UID_ROOT, GID_WHEEL, 0600,
2009	    "bpf%d", u);
2010	dev_ref(*dev);
2011	(*dev)->si_flags |= SI_CHEAPCLONE;
2012	return;
2013}
2014
2015static void
2016bpf_drvinit(void *unused)
2017{
2018
2019	mtx_init(&bpf_mtx, "bpf global lock", NULL, MTX_DEF);
2020	LIST_INIT(&bpf_iflist);
2021	EVENTHANDLER_REGISTER(dev_clone, bpf_clone, 0, 1000);
2022}
2023
2024static void
2025bpfstats_fill_xbpf(struct xbpf_d *d, struct bpf_d *bd)
2026{
2027
2028	bzero(d, sizeof(*d));
2029	BPFD_LOCK_ASSERT(bd);
2030	d->bd_structsize = sizeof(*d);
2031	d->bd_immediate = bd->bd_immediate;
2032	d->bd_promisc = bd->bd_promisc;
2033	d->bd_hdrcmplt = bd->bd_hdrcmplt;
2034	d->bd_direction = bd->bd_direction;
2035	d->bd_feedback = bd->bd_feedback;
2036	d->bd_async = bd->bd_async;
2037	d->bd_rcount = bd->bd_rcount;
2038	d->bd_dcount = bd->bd_dcount;
2039	d->bd_fcount = bd->bd_fcount;
2040	d->bd_sig = bd->bd_sig;
2041	d->bd_slen = bd->bd_slen;
2042	d->bd_hlen = bd->bd_hlen;
2043	d->bd_bufsize = bd->bd_bufsize;
2044	d->bd_pid = bd->bd_pid;
2045	strlcpy(d->bd_ifname,
2046	    bd->bd_bif->bif_ifp->if_xname, IFNAMSIZ);
2047	d->bd_locked = bd->bd_locked;
2048	d->bd_wcount = bd->bd_wcount;
2049	d->bd_wdcount = bd->bd_wdcount;
2050	d->bd_wfcount = bd->bd_wfcount;
2051	d->bd_zcopy = bd->bd_zcopy;
2052	d->bd_bufmode = bd->bd_bufmode;
2053}
2054
2055static int
2056bpf_stats_sysctl(SYSCTL_HANDLER_ARGS)
2057{
2058	struct xbpf_d *xbdbuf, *xbd;
2059	int index, error;
2060	struct bpf_if *bp;
2061	struct bpf_d *bd;
2062
2063	/*
2064	 * XXX This is not technically correct. It is possible for non
2065	 * privileged users to open bpf devices. It would make sense
2066	 * if the users who opened the devices were able to retrieve
2067	 * the statistics for them, too.
2068	 */
2069	error = priv_check(req->td, PRIV_NET_BPF);
2070	if (error)
2071		return (error);
2072	if (req->oldptr == NULL)
2073		return (SYSCTL_OUT(req, 0, bpf_bpfd_cnt * sizeof(*xbd)));
2074	if (bpf_bpfd_cnt == 0)
2075		return (SYSCTL_OUT(req, 0, 0));
2076	xbdbuf = malloc(req->oldlen, M_BPF, M_WAITOK);
2077	mtx_lock(&bpf_mtx);
2078	if (req->oldlen < (bpf_bpfd_cnt * sizeof(*xbd))) {
2079		mtx_unlock(&bpf_mtx);
2080		free(xbdbuf, M_BPF);
2081		return (ENOMEM);
2082	}
2083	index = 0;
2084	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
2085		BPFIF_LOCK(bp);
2086		LIST_FOREACH(bd, &bp->bif_dlist, bd_next) {
2087			xbd = &xbdbuf[index++];
2088			BPFD_LOCK(bd);
2089			bpfstats_fill_xbpf(xbd, bd);
2090			BPFD_UNLOCK(bd);
2091		}
2092		BPFIF_UNLOCK(bp);
2093	}
2094	mtx_unlock(&bpf_mtx);
2095	error = SYSCTL_OUT(req, xbdbuf, index * sizeof(*xbd));
2096	free(xbdbuf, M_BPF);
2097	return (error);
2098}
2099
2100SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,bpf_drvinit,NULL);
2101
2102#else /* !DEV_BPF && !NETGRAPH_BPF */
2103/*
2104 * NOP stubs to allow bpf-using drivers to load and function.
2105 *
2106 * A 'better' implementation would allow the core bpf functionality
2107 * to be loaded at runtime.
2108 */
2109static struct bpf_if bp_null;
2110
2111void
2112bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen)
2113{
2114}
2115
2116void
2117bpf_mtap(struct bpf_if *bp, struct mbuf *m)
2118{
2119}
2120
2121void
2122bpf_mtap2(struct bpf_if *bp, void *d, u_int l, struct mbuf *m)
2123{
2124}
2125
2126void
2127bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen)
2128{
2129
2130	bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
2131}
2132
2133void
2134bpfattach2(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp)
2135{
2136
2137	*driverp = &bp_null;
2138}
2139
2140void
2141bpfdetach(struct ifnet *ifp)
2142{
2143}
2144
2145u_int
2146bpf_filter(const struct bpf_insn *pc, u_char *p, u_int wirelen, u_int buflen)
2147{
2148	return -1;	/* "no filter" behaviour */
2149}
2150
2151int
2152bpf_validate(const struct bpf_insn *f, int len)
2153{
2154	return 0;		/* false */
2155}
2156
2157#endif /* !DEV_BPF && !NETGRAPH_BPF */
2158