bpf.c revision 142787
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 * $FreeBSD: head/sys/net/bpf.c 142787 2005-02-28 12:35:52Z rwatson $
37 */
38
39#include "opt_bpf.h"
40#include "opt_mac.h"
41#include "opt_netgraph.h"
42
43#include <sys/types.h>
44#include <sys/param.h>
45#include <sys/systm.h>
46#include <sys/conf.h>
47#include <sys/fcntl.h>
48#include <sys/mac.h>
49#include <sys/malloc.h>
50#include <sys/mbuf.h>
51#include <sys/time.h>
52#include <sys/proc.h>
53#include <sys/signalvar.h>
54#include <sys/filio.h>
55#include <sys/sockio.h>
56#include <sys/ttycom.h>
57#include <sys/uio.h>
58
59#include <sys/event.h>
60#include <sys/file.h>
61#include <sys/poll.h>
62#include <sys/proc.h>
63
64#include <sys/socket.h>
65
66#include <net/if.h>
67#include <net/bpf.h>
68#include <net/bpfdesc.h>
69
70#include <netinet/in.h>
71#include <netinet/if_ether.h>
72#include <sys/kernel.h>
73#include <sys/sysctl.h>
74
75static MALLOC_DEFINE(M_BPF, "BPF", "BPF data");
76
77#if defined(DEV_BPF) || defined(NETGRAPH_BPF)
78
79#define PRINET  26			/* interruptible */
80
81/*
82 * The default read buffer size is patchable.
83 */
84static int bpf_bufsize = 4096;
85SYSCTL_INT(_debug, OID_AUTO, bpf_bufsize, CTLFLAG_RW,
86	&bpf_bufsize, 0, "");
87static int bpf_maxbufsize = BPF_MAXBUFSIZE;
88SYSCTL_INT(_debug, OID_AUTO, bpf_maxbufsize, CTLFLAG_RW,
89	&bpf_maxbufsize, 0, "");
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 */
99
100static int	bpf_allocbufs(struct bpf_d *);
101static void	bpf_attachd(struct bpf_d *d, struct bpf_if *bp);
102static void	bpf_detachd(struct bpf_d *d);
103static void	bpf_freed(struct bpf_d *);
104static void	bpf_mcopy(const void *, void *, size_t);
105static int	bpf_movein(struct uio *, int,
106		    struct mbuf **, struct sockaddr *, int *);
107static int	bpf_setif(struct bpf_d *, struct ifreq *);
108static void	bpf_timed_out(void *);
109static __inline void
110		bpf_wakeup(struct bpf_d *);
111static void	catchpacket(struct bpf_d *, u_char *, u_int,
112		    u_int, void (*)(const void *, void *, size_t));
113static void	reset_d(struct bpf_d *);
114static int	 bpf_setf(struct bpf_d *, struct bpf_program *);
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);
119
120static	d_open_t	bpfopen;
121static	d_close_t	bpfclose;
122static	d_read_t	bpfread;
123static	d_write_t	bpfwrite;
124static	d_ioctl_t	bpfioctl;
125static	d_poll_t	bpfpoll;
126static	d_kqfilter_t	bpfkqfilter;
127
128static struct cdevsw bpf_cdevsw = {
129	.d_version =	D_VERSION,
130	.d_flags =	D_NEEDGIANT,
131	.d_open =	bpfopen,
132	.d_close =	bpfclose,
133	.d_read =	bpfread,
134	.d_write =	bpfwrite,
135	.d_ioctl =	bpfioctl,
136	.d_poll =	bpfpoll,
137	.d_name =	"bpf",
138	.d_kqfilter =	bpfkqfilter,
139};
140
141static struct filterops bpfread_filtops =
142	{ 1, NULL, filt_bpfdetach, filt_bpfread };
143
144static int
145bpf_movein(uio, linktype, mp, sockp, datlen)
146	struct uio *uio;
147	int linktype, *datlen;
148	struct mbuf **mp;
149	struct sockaddr *sockp;
150{
151	struct mbuf *m;
152	int error;
153	int len;
154	int hlen;
155
156	/*
157	 * Build a sockaddr based on the data link layer type.
158	 * We do this at this level because the ethernet header
159	 * is copied directly into the data field of the sockaddr.
160	 * In the case of SLIP, there is no header and the packet
161	 * is forwarded as is.
162	 * Also, we are careful to leave room at the front of the mbuf
163	 * for the link level header.
164	 */
165	switch (linktype) {
166
167	case DLT_SLIP:
168		sockp->sa_family = AF_INET;
169		hlen = 0;
170		break;
171
172	case DLT_EN10MB:
173		sockp->sa_family = AF_UNSPEC;
174		/* XXX Would MAXLINKHDR be better? */
175		hlen = ETHER_HDR_LEN;
176		break;
177
178	case DLT_FDDI:
179		sockp->sa_family = AF_IMPLINK;
180		hlen = 0;
181		break;
182
183	case DLT_RAW:
184	case DLT_NULL:
185		sockp->sa_family = AF_UNSPEC;
186		hlen = 0;
187		break;
188
189	case DLT_ATM_RFC1483:
190		/*
191		 * en atm driver requires 4-byte atm pseudo header.
192		 * though it isn't standard, vpi:vci needs to be
193		 * specified anyway.
194		 */
195		sockp->sa_family = AF_UNSPEC;
196		hlen = 12;	/* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
197		break;
198
199	case DLT_PPP:
200		sockp->sa_family = AF_UNSPEC;
201		hlen = 4;	/* This should match PPP_HDRLEN */
202		break;
203
204	default:
205		return (EIO);
206	}
207
208	len = uio->uio_resid;
209	*datlen = len - hlen;
210	if ((unsigned)len > MCLBYTES)
211		return (EIO);
212
213	if (len > MHLEN) {
214		m = m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
215	} else {
216		MGETHDR(m, M_TRYWAIT, MT_DATA);
217	}
218	if (m == NULL)
219		return (ENOBUFS);
220	m->m_pkthdr.len = m->m_len = len;
221	m->m_pkthdr.rcvif = NULL;
222	*mp = m;
223
224	/*
225	 * Make room for link header.
226	 */
227	if (hlen != 0) {
228		m->m_pkthdr.len -= hlen;
229		m->m_len -= hlen;
230#if BSD >= 199103
231		m->m_data += hlen; /* XXX */
232#else
233		m->m_off += hlen;
234#endif
235		error = uiomove(sockp->sa_data, hlen, uio);
236		if (error)
237			goto bad;
238	}
239	error = uiomove(mtod(m, void *), len - hlen, uio);
240	if (!error)
241		return (0);
242bad:
243	m_freem(m);
244	return (error);
245}
246
247/*
248 * Attach file to the bpf interface, i.e. make d listen on bp.
249 */
250static void
251bpf_attachd(d, bp)
252	struct bpf_d *d;
253	struct bpf_if *bp;
254{
255	/*
256	 * Point d at bp, and add d to the interface's list of listeners.
257	 * Finally, point the driver's bpf cookie at the interface so
258	 * it will divert packets to bpf.
259	 */
260	BPFIF_LOCK(bp);
261	d->bd_bif = bp;
262	LIST_INSERT_HEAD(&bp->bif_dlist, d, bd_next);
263
264	*bp->bif_driverp = bp;
265	BPFIF_UNLOCK(bp);
266}
267
268/*
269 * Detach a file from its interface.
270 */
271static void
272bpf_detachd(d)
273	struct bpf_d *d;
274{
275	int error;
276	struct bpf_if *bp;
277	struct ifnet *ifp;
278
279	bp = d->bd_bif;
280	BPFIF_LOCK(bp);
281	BPFD_LOCK(d);
282	ifp = d->bd_bif->bif_ifp;
283
284	/*
285	 * Remove d from the interface's descriptor list.
286	 */
287	LIST_REMOVE(d, bd_next);
288
289	/*
290	 * Let the driver know that there are no more listeners.
291	 */
292	if (LIST_EMPTY(&bp->bif_dlist))
293		*bp->bif_driverp = NULL;
294
295	d->bd_bif = NULL;
296	BPFD_UNLOCK(d);
297	BPFIF_UNLOCK(bp);
298
299	/*
300	 * Check if this descriptor had requested promiscuous mode.
301	 * If so, turn it off.
302	 */
303	if (d->bd_promisc) {
304		d->bd_promisc = 0;
305		error = ifpromisc(ifp, 0);
306		if (error != 0 && error != ENXIO) {
307			/*
308			 * ENXIO can happen if a pccard is unplugged
309			 * Something is really wrong if we were able to put
310			 * the driver into promiscuous mode, but can't
311			 * take it out.
312			 */
313			if_printf(bp->bif_ifp,
314				"bpf_detach: ifpromisc failed (%d)\n", error);
315		}
316	}
317}
318
319/*
320 * Open ethernet device.  Returns ENXIO for illegal minor device number,
321 * EBUSY if file is open by another process.
322 */
323/* ARGSUSED */
324static	int
325bpfopen(dev, flags, fmt, td)
326	struct cdev *dev;
327	int flags;
328	int fmt;
329	struct thread *td;
330{
331	struct bpf_d *d;
332
333	mtx_lock(&bpf_mtx);
334	d = dev->si_drv1;
335	/*
336	 * Each minor can be opened by only one process.  If the requested
337	 * minor is in use, return EBUSY.
338	 */
339	if (d != NULL) {
340		mtx_unlock(&bpf_mtx);
341		return (EBUSY);
342	}
343	dev->si_drv1 = (struct bpf_d *)~0;	/* mark device in use */
344	mtx_unlock(&bpf_mtx);
345
346	if ((dev->si_flags & SI_NAMED) == 0)
347		make_dev(&bpf_cdevsw, minor(dev), UID_ROOT, GID_WHEEL, 0600,
348		    "bpf%d", dev2unit(dev));
349	MALLOC(d, struct bpf_d *, sizeof(*d), M_BPF, M_WAITOK | M_ZERO);
350	dev->si_drv1 = d;
351	d->bd_bufsize = bpf_bufsize;
352	d->bd_sig = SIGIO;
353	d->bd_seesent = 1;
354#ifdef MAC
355	mac_init_bpfdesc(d);
356	mac_create_bpfdesc(td->td_ucred, d);
357#endif
358	mtx_init(&d->bd_mtx, devtoname(dev), "bpf cdev lock", MTX_DEF);
359	callout_init(&d->bd_callout, debug_mpsafenet ? CALLOUT_MPSAFE : 0);
360	knlist_init(&d->bd_sel.si_note, &d->bd_mtx);
361
362	return (0);
363}
364
365/*
366 * Close the descriptor by detaching it from its interface,
367 * deallocating its buffers, and marking it free.
368 */
369/* ARGSUSED */
370static	int
371bpfclose(dev, flags, fmt, td)
372	struct cdev *dev;
373	int flags;
374	int fmt;
375	struct thread *td;
376{
377	struct bpf_d *d = dev->si_drv1;
378
379	BPFD_LOCK(d);
380	if (d->bd_state == BPF_WAITING)
381		callout_stop(&d->bd_callout);
382	d->bd_state = BPF_IDLE;
383	BPFD_UNLOCK(d);
384	funsetown(&d->bd_sigio);
385	mtx_lock(&bpf_mtx);
386	if (d->bd_bif)
387		bpf_detachd(d);
388	mtx_unlock(&bpf_mtx);
389#ifdef MAC
390	mac_destroy_bpfdesc(d);
391#endif /* MAC */
392	knlist_destroy(&d->bd_sel.si_note);
393	bpf_freed(d);
394	dev->si_drv1 = NULL;
395	free(d, M_BPF);
396
397	return (0);
398}
399
400
401/*
402 * Rotate the packet buffers in descriptor d.  Move the store buffer
403 * into the hold slot, and the free buffer into the store slot.
404 * Zero the length of the new store buffer.
405 */
406#define ROTATE_BUFFERS(d) \
407	(d)->bd_hbuf = (d)->bd_sbuf; \
408	(d)->bd_hlen = (d)->bd_slen; \
409	(d)->bd_sbuf = (d)->bd_fbuf; \
410	(d)->bd_slen = 0; \
411	(d)->bd_fbuf = NULL;
412/*
413 *  bpfread - read next chunk of packets from buffers
414 */
415static	int
416bpfread(dev, uio, ioflag)
417	struct cdev *dev;
418	struct uio *uio;
419	int ioflag;
420{
421	struct bpf_d *d = dev->si_drv1;
422	int timed_out;
423	int error;
424
425	/*
426	 * Restrict application to use a buffer the same size as
427	 * as kernel buffers.
428	 */
429	if (uio->uio_resid != d->bd_bufsize)
430		return (EINVAL);
431
432	BPFD_LOCK(d);
433	if (d->bd_state == BPF_WAITING)
434		callout_stop(&d->bd_callout);
435	timed_out = (d->bd_state == BPF_TIMED_OUT);
436	d->bd_state = BPF_IDLE;
437	/*
438	 * If the hold buffer is empty, then do a timed sleep, which
439	 * ends when the timeout expires or when enough packets
440	 * have arrived to fill the store buffer.
441	 */
442	while (d->bd_hbuf == NULL) {
443		if ((d->bd_immediate || timed_out) && d->bd_slen != 0) {
444			/*
445			 * A packet(s) either arrived since the previous
446			 * read or arrived while we were asleep.
447			 * Rotate the buffers and return what's here.
448			 */
449			ROTATE_BUFFERS(d);
450			break;
451		}
452
453		/*
454		 * No data is available, check to see if the bpf device
455		 * is still pointed at a real interface.  If not, return
456		 * ENXIO so that the userland process knows to rebind
457		 * it before using it again.
458		 */
459		if (d->bd_bif == NULL) {
460			BPFD_UNLOCK(d);
461			return (ENXIO);
462		}
463
464		if (ioflag & O_NONBLOCK) {
465			BPFD_UNLOCK(d);
466			return (EWOULDBLOCK);
467		}
468		error = msleep(d, &d->bd_mtx, PRINET|PCATCH,
469		     "bpf", d->bd_rtout);
470		if (error == EINTR || error == ERESTART) {
471			BPFD_UNLOCK(d);
472			return (error);
473		}
474		if (error == EWOULDBLOCK) {
475			/*
476			 * On a timeout, return what's in the buffer,
477			 * which may be nothing.  If there is something
478			 * in the store buffer, we can rotate the buffers.
479			 */
480			if (d->bd_hbuf)
481				/*
482				 * We filled up the buffer in between
483				 * getting the timeout and arriving
484				 * here, so we don't need to rotate.
485				 */
486				break;
487
488			if (d->bd_slen == 0) {
489				BPFD_UNLOCK(d);
490				return (0);
491			}
492			ROTATE_BUFFERS(d);
493			break;
494		}
495	}
496	/*
497	 * At this point, we know we have something in the hold slot.
498	 */
499	BPFD_UNLOCK(d);
500
501	/*
502	 * Move data from hold buffer into user space.
503	 * We know the entire buffer is transferred since
504	 * we checked above that the read buffer is bpf_bufsize bytes.
505	 */
506	error = uiomove(d->bd_hbuf, d->bd_hlen, uio);
507
508	BPFD_LOCK(d);
509	d->bd_fbuf = d->bd_hbuf;
510	d->bd_hbuf = NULL;
511	d->bd_hlen = 0;
512	BPFD_UNLOCK(d);
513
514	return (error);
515}
516
517
518/*
519 * If there are processes sleeping on this descriptor, wake them up.
520 */
521static __inline void
522bpf_wakeup(d)
523	struct bpf_d *d;
524{
525	if (d->bd_state == BPF_WAITING) {
526		callout_stop(&d->bd_callout);
527		d->bd_state = BPF_IDLE;
528	}
529	wakeup(d);
530	if (d->bd_async && d->bd_sig && d->bd_sigio)
531		pgsigio(&d->bd_sigio, d->bd_sig, 0);
532
533	selwakeuppri(&d->bd_sel, PRINET);
534	KNOTE_LOCKED(&d->bd_sel.si_note, 0);
535}
536
537static void
538bpf_timed_out(arg)
539	void *arg;
540{
541	struct bpf_d *d = (struct bpf_d *)arg;
542
543	BPFD_LOCK(d);
544	if (d->bd_state == BPF_WAITING) {
545		d->bd_state = BPF_TIMED_OUT;
546		if (d->bd_slen != 0)
547			bpf_wakeup(d);
548	}
549	BPFD_UNLOCK(d);
550}
551
552static	int
553bpfwrite(dev, uio, ioflag)
554	struct cdev *dev;
555	struct uio *uio;
556	int ioflag;
557{
558	struct bpf_d *d = dev->si_drv1;
559	struct ifnet *ifp;
560	struct mbuf *m;
561	int error;
562	struct sockaddr dst;
563	int datlen;
564
565	if (d->bd_bif == NULL)
566		return (ENXIO);
567
568	ifp = d->bd_bif->bif_ifp;
569
570	if ((ifp->if_flags & IFF_UP) == 0)
571		return (ENETDOWN);
572
573	if (uio->uio_resid == 0)
574		return (0);
575
576	bzero(&dst, sizeof(dst));
577	error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, &m, &dst, &datlen);
578	if (error)
579		return (error);
580
581	if (datlen > ifp->if_mtu) {
582		m_freem(m);
583		return (EMSGSIZE);
584	}
585
586	if (d->bd_hdrcmplt)
587		dst.sa_family = pseudo_AF_HDRCMPLT;
588
589#ifdef MAC
590	BPFD_LOCK(d);
591	mac_create_mbuf_from_bpfdesc(d, m);
592	BPFD_UNLOCK(d);
593#endif
594	NET_LOCK_GIANT();
595	error = (*ifp->if_output)(ifp, m, &dst, NULL);
596	NET_UNLOCK_GIANT();
597	/*
598	 * The driver frees the mbuf.
599	 */
600	return (error);
601}
602
603/*
604 * Reset a descriptor by flushing its packet buffer and clearing the
605 * receive and drop counts.
606 */
607static void
608reset_d(d)
609	struct bpf_d *d;
610{
611
612	mtx_assert(&d->bd_mtx, MA_OWNED);
613	if (d->bd_hbuf) {
614		/* Free the hold buffer. */
615		d->bd_fbuf = d->bd_hbuf;
616		d->bd_hbuf = NULL;
617	}
618	d->bd_slen = 0;
619	d->bd_hlen = 0;
620	d->bd_rcount = 0;
621	d->bd_dcount = 0;
622}
623
624/*
625 *  FIONREAD		Check for read packet available.
626 *  SIOCGIFADDR		Get interface address - convenient hook to driver.
627 *  BIOCGBLEN		Get buffer len [for read()].
628 *  BIOCSETF		Set ethernet read filter.
629 *  BIOCFLUSH		Flush read packet buffer.
630 *  BIOCPROMISC		Put interface into promiscuous mode.
631 *  BIOCGDLT		Get link layer type.
632 *  BIOCGETIF		Get interface name.
633 *  BIOCSETIF		Set interface.
634 *  BIOCSRTIMEOUT	Set read timeout.
635 *  BIOCGRTIMEOUT	Get read timeout.
636 *  BIOCGSTATS		Get packet stats.
637 *  BIOCIMMEDIATE	Set immediate mode.
638 *  BIOCVERSION		Get filter language version.
639 *  BIOCGHDRCMPLT	Get "header already complete" flag
640 *  BIOCSHDRCMPLT	Set "header already complete" flag
641 *  BIOCGSEESENT	Get "see packets sent" flag
642 *  BIOCSSEESENT	Set "see packets sent" flag
643 */
644/* ARGSUSED */
645static	int
646bpfioctl(dev, cmd, addr, flags, td)
647	struct cdev *dev;
648	u_long cmd;
649	caddr_t addr;
650	int flags;
651	struct thread *td;
652{
653	struct bpf_d *d = dev->si_drv1;
654	int error = 0;
655
656	BPFD_LOCK(d);
657	if (d->bd_state == BPF_WAITING)
658		callout_stop(&d->bd_callout);
659	d->bd_state = BPF_IDLE;
660	BPFD_UNLOCK(d);
661
662	switch (cmd) {
663
664	default:
665		error = EINVAL;
666		break;
667
668	/*
669	 * Check for read packet available.
670	 */
671	case FIONREAD:
672		{
673			int n;
674
675			BPFD_LOCK(d);
676			n = d->bd_slen;
677			if (d->bd_hbuf)
678				n += d->bd_hlen;
679			BPFD_UNLOCK(d);
680
681			*(int *)addr = n;
682			break;
683		}
684
685	case SIOCGIFADDR:
686		{
687			struct ifnet *ifp;
688
689			if (d->bd_bif == NULL)
690				error = EINVAL;
691			else {
692				ifp = d->bd_bif->bif_ifp;
693				error = (*ifp->if_ioctl)(ifp, cmd, addr);
694			}
695			break;
696		}
697
698	/*
699	 * Get buffer len [for read()].
700	 */
701	case BIOCGBLEN:
702		*(u_int *)addr = d->bd_bufsize;
703		break;
704
705	/*
706	 * Set buffer length.
707	 */
708	case BIOCSBLEN:
709		if (d->bd_bif != NULL)
710			error = EINVAL;
711		else {
712			u_int size = *(u_int *)addr;
713
714			if (size > bpf_maxbufsize)
715				*(u_int *)addr = size = bpf_maxbufsize;
716			else if (size < BPF_MINBUFSIZE)
717				*(u_int *)addr = size = BPF_MINBUFSIZE;
718			d->bd_bufsize = size;
719		}
720		break;
721
722	/*
723	 * Set link layer read filter.
724	 */
725	case BIOCSETF:
726		error = bpf_setf(d, (struct bpf_program *)addr);
727		break;
728
729	/*
730	 * Flush read packet buffer.
731	 */
732	case BIOCFLUSH:
733		BPFD_LOCK(d);
734		reset_d(d);
735		BPFD_UNLOCK(d);
736		break;
737
738	/*
739	 * Put interface into promiscuous mode.
740	 */
741	case BIOCPROMISC:
742		if (d->bd_bif == NULL) {
743			/*
744			 * No interface attached yet.
745			 */
746			error = EINVAL;
747			break;
748		}
749		if (d->bd_promisc == 0) {
750			mtx_lock(&Giant);
751			error = ifpromisc(d->bd_bif->bif_ifp, 1);
752			mtx_unlock(&Giant);
753			if (error == 0)
754				d->bd_promisc = 1;
755		}
756		break;
757
758	/*
759	 * Get current data link type.
760	 */
761	case BIOCGDLT:
762		if (d->bd_bif == NULL)
763			error = EINVAL;
764		else
765			*(u_int *)addr = d->bd_bif->bif_dlt;
766		break;
767
768	/*
769	 * Get a list of supported data link types.
770	 */
771	case BIOCGDLTLIST:
772		if (d->bd_bif == NULL)
773			error = EINVAL;
774		else
775			error = bpf_getdltlist(d, (struct bpf_dltlist *)addr);
776		break;
777
778	/*
779	 * Set data link type.
780	 */
781	case BIOCSDLT:
782		if (d->bd_bif == NULL)
783			error = EINVAL;
784		else
785			error = bpf_setdlt(d, *(u_int *)addr);
786		break;
787
788	/*
789	 * Get interface name.
790	 */
791	case BIOCGETIF:
792		if (d->bd_bif == NULL)
793			error = EINVAL;
794		else {
795			struct ifnet *const ifp = d->bd_bif->bif_ifp;
796			struct ifreq *const ifr = (struct ifreq *)addr;
797
798			strlcpy(ifr->ifr_name, ifp->if_xname,
799			    sizeof(ifr->ifr_name));
800		}
801		break;
802
803	/*
804	 * Set interface.
805	 */
806	case BIOCSETIF:
807		error = bpf_setif(d, (struct ifreq *)addr);
808		break;
809
810	/*
811	 * Set read timeout.
812	 */
813	case BIOCSRTIMEOUT:
814		{
815			struct timeval *tv = (struct timeval *)addr;
816
817			/*
818			 * Subtract 1 tick from tvtohz() since this isn't
819			 * a one-shot timer.
820			 */
821			if ((error = itimerfix(tv)) == 0)
822				d->bd_rtout = tvtohz(tv) - 1;
823			break;
824		}
825
826	/*
827	 * Get read timeout.
828	 */
829	case BIOCGRTIMEOUT:
830		{
831			struct timeval *tv = (struct timeval *)addr;
832
833			tv->tv_sec = d->bd_rtout / hz;
834			tv->tv_usec = (d->bd_rtout % hz) * tick;
835			break;
836		}
837
838	/*
839	 * Get packet stats.
840	 */
841	case BIOCGSTATS:
842		{
843			struct bpf_stat *bs = (struct bpf_stat *)addr;
844
845			bs->bs_recv = d->bd_rcount;
846			bs->bs_drop = d->bd_dcount;
847			break;
848		}
849
850	/*
851	 * Set immediate mode.
852	 */
853	case BIOCIMMEDIATE:
854		d->bd_immediate = *(u_int *)addr;
855		break;
856
857	case BIOCVERSION:
858		{
859			struct bpf_version *bv = (struct bpf_version *)addr;
860
861			bv->bv_major = BPF_MAJOR_VERSION;
862			bv->bv_minor = BPF_MINOR_VERSION;
863			break;
864		}
865
866	/*
867	 * Get "header already complete" flag
868	 */
869	case BIOCGHDRCMPLT:
870		*(u_int *)addr = d->bd_hdrcmplt;
871		break;
872
873	/*
874	 * Set "header already complete" flag
875	 */
876	case BIOCSHDRCMPLT:
877		d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
878		break;
879
880	/*
881	 * Get "see sent packets" flag
882	 */
883	case BIOCGSEESENT:
884		*(u_int *)addr = d->bd_seesent;
885		break;
886
887	/*
888	 * Set "see sent packets" flag
889	 */
890	case BIOCSSEESENT:
891		d->bd_seesent = *(u_int *)addr;
892		break;
893
894	case FIONBIO:		/* Non-blocking I/O */
895		break;
896
897	case FIOASYNC:		/* Send signal on receive packets */
898		d->bd_async = *(int *)addr;
899		break;
900
901	case FIOSETOWN:
902		error = fsetown(*(int *)addr, &d->bd_sigio);
903		break;
904
905	case FIOGETOWN:
906		*(int *)addr = fgetown(&d->bd_sigio);
907		break;
908
909	/* This is deprecated, FIOSETOWN should be used instead. */
910	case TIOCSPGRP:
911		error = fsetown(-(*(int *)addr), &d->bd_sigio);
912		break;
913
914	/* This is deprecated, FIOGETOWN should be used instead. */
915	case TIOCGPGRP:
916		*(int *)addr = -fgetown(&d->bd_sigio);
917		break;
918
919	case BIOCSRSIG:		/* Set receive signal */
920		{
921			u_int sig;
922
923			sig = *(u_int *)addr;
924
925			if (sig >= NSIG)
926				error = EINVAL;
927			else
928				d->bd_sig = sig;
929			break;
930		}
931	case BIOCGRSIG:
932		*(u_int *)addr = d->bd_sig;
933		break;
934	}
935	return (error);
936}
937
938/*
939 * Set d's packet filter program to fp.  If this file already has a filter,
940 * free it and replace it.  Returns EINVAL for bogus requests.
941 */
942static int
943bpf_setf(d, fp)
944	struct bpf_d *d;
945	struct bpf_program *fp;
946{
947	struct bpf_insn *fcode, *old;
948	u_int flen, size;
949
950	old = d->bd_filter;
951	if (fp->bf_insns == NULL) {
952		if (fp->bf_len != 0)
953			return (EINVAL);
954		BPFD_LOCK(d);
955		d->bd_filter = NULL;
956		reset_d(d);
957		BPFD_UNLOCK(d);
958		if (old != NULL)
959			free((caddr_t)old, M_BPF);
960		return (0);
961	}
962	flen = fp->bf_len;
963	if (flen > BPF_MAXINSNS)
964		return (EINVAL);
965
966	size = flen * sizeof(*fp->bf_insns);
967	fcode = (struct bpf_insn *)malloc(size, M_BPF, M_WAITOK);
968	if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
969	    bpf_validate(fcode, (int)flen)) {
970		BPFD_LOCK(d);
971		d->bd_filter = fcode;
972		reset_d(d);
973		BPFD_UNLOCK(d);
974		if (old != NULL)
975			free((caddr_t)old, M_BPF);
976
977		return (0);
978	}
979	free((caddr_t)fcode, M_BPF);
980	return (EINVAL);
981}
982
983/*
984 * Detach a file from its current interface (if attached at all) and attach
985 * to the interface indicated by the name stored in ifr.
986 * Return an errno or 0.
987 */
988static int
989bpf_setif(d, ifr)
990	struct bpf_d *d;
991	struct ifreq *ifr;
992{
993	struct bpf_if *bp;
994	int error;
995	struct ifnet *theywant;
996
997	theywant = ifunit(ifr->ifr_name);
998	if (theywant == NULL)
999		return ENXIO;
1000
1001	/*
1002	 * Look through attached interfaces for the named one.
1003	 */
1004	mtx_lock(&bpf_mtx);
1005	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1006		struct ifnet *ifp = bp->bif_ifp;
1007
1008		if (ifp == NULL || ifp != theywant)
1009			continue;
1010		/* skip additional entry */
1011		if (bp->bif_driverp != (struct bpf_if **)&ifp->if_bpf)
1012			continue;
1013
1014		mtx_unlock(&bpf_mtx);
1015		/*
1016		 * We found the requested interface.
1017		 * Allocate the packet buffers if we need to.
1018		 * If we're already attached to requested interface,
1019		 * just flush the buffer.
1020		 */
1021		if (d->bd_sbuf == NULL) {
1022			error = bpf_allocbufs(d);
1023			if (error != 0)
1024				return (error);
1025		}
1026		if (bp != d->bd_bif) {
1027			if (d->bd_bif)
1028				/*
1029				 * Detach if attached to something else.
1030				 */
1031				bpf_detachd(d);
1032
1033			bpf_attachd(d, bp);
1034		}
1035		BPFD_LOCK(d);
1036		reset_d(d);
1037		BPFD_UNLOCK(d);
1038		return (0);
1039	}
1040	mtx_unlock(&bpf_mtx);
1041	/* Not found. */
1042	return (ENXIO);
1043}
1044
1045/*
1046 * Support for select() and poll() system calls
1047 *
1048 * Return true iff the specific operation will not block indefinitely.
1049 * Otherwise, return false but make a note that a selwakeup() must be done.
1050 */
1051static int
1052bpfpoll(dev, events, td)
1053	struct cdev *dev;
1054	int events;
1055	struct thread *td;
1056{
1057	struct bpf_d *d;
1058	int revents;
1059
1060	d = dev->si_drv1;
1061	if (d->bd_bif == NULL)
1062		return (ENXIO);
1063
1064	revents = events & (POLLOUT | POLLWRNORM);
1065	BPFD_LOCK(d);
1066	if (events & (POLLIN | POLLRDNORM)) {
1067		if (bpf_ready(d))
1068			revents |= events & (POLLIN | POLLRDNORM);
1069		else {
1070			selrecord(td, &d->bd_sel);
1071			/* Start the read timeout if necessary. */
1072			if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
1073				callout_reset(&d->bd_callout, d->bd_rtout,
1074				    bpf_timed_out, d);
1075				d->bd_state = BPF_WAITING;
1076			}
1077		}
1078	}
1079	BPFD_UNLOCK(d);
1080	return (revents);
1081}
1082
1083/*
1084 * Support for kevent() system call.  Register EVFILT_READ filters and
1085 * reject all others.
1086 */
1087int
1088bpfkqfilter(dev, kn)
1089	struct cdev *dev;
1090	struct knote *kn;
1091{
1092	struct bpf_d *d = (struct bpf_d *)dev->si_drv1;
1093
1094	if (kn->kn_filter != EVFILT_READ)
1095		return (1);
1096
1097	kn->kn_fop = &bpfread_filtops;
1098	kn->kn_hook = d;
1099	knlist_add(&d->bd_sel.si_note, kn, 0);
1100
1101	return (0);
1102}
1103
1104static void
1105filt_bpfdetach(kn)
1106	struct knote *kn;
1107{
1108	struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
1109
1110	knlist_remove(&d->bd_sel.si_note, kn, 0);
1111}
1112
1113static int
1114filt_bpfread(kn, hint)
1115	struct knote *kn;
1116	long hint;
1117{
1118	struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
1119	int ready;
1120
1121	BPFD_LOCK_ASSERT(d);
1122	ready = bpf_ready(d);
1123	if (ready) {
1124		kn->kn_data = d->bd_slen;
1125		if (d->bd_hbuf)
1126			kn->kn_data += d->bd_hlen;
1127	}
1128	else if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
1129		callout_reset(&d->bd_callout, d->bd_rtout,
1130		    bpf_timed_out, d);
1131		d->bd_state = BPF_WAITING;
1132	}
1133
1134	return (ready);
1135}
1136
1137/*
1138 * Incoming linkage from device drivers.  Process the packet pkt, of length
1139 * pktlen, which is stored in a contiguous buffer.  The packet is parsed
1140 * by each process' filter, and if accepted, stashed into the corresponding
1141 * buffer.
1142 */
1143void
1144bpf_tap(bp, pkt, pktlen)
1145	struct bpf_if *bp;
1146	u_char *pkt;
1147	u_int pktlen;
1148{
1149	struct bpf_d *d;
1150	u_int slen;
1151
1152	/*
1153	 * Lockless read to avoid cost of locking the interface if there are
1154	 * no descriptors attached.
1155	 */
1156	if (LIST_EMPTY(&bp->bif_dlist))
1157		return;
1158
1159	BPFIF_LOCK(bp);
1160	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1161		BPFD_LOCK(d);
1162		++d->bd_rcount;
1163		slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen);
1164		if (slen != 0) {
1165#ifdef MAC
1166			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1167#endif
1168				catchpacket(d, pkt, pktlen, slen, bcopy);
1169		}
1170		BPFD_UNLOCK(d);
1171	}
1172	BPFIF_UNLOCK(bp);
1173}
1174
1175/*
1176 * Copy data from an mbuf chain into a buffer.  This code is derived
1177 * from m_copydata in sys/uipc_mbuf.c.
1178 */
1179static void
1180bpf_mcopy(src_arg, dst_arg, len)
1181	const void *src_arg;
1182	void *dst_arg;
1183	size_t len;
1184{
1185	const struct mbuf *m;
1186	u_int count;
1187	u_char *dst;
1188
1189	m = src_arg;
1190	dst = dst_arg;
1191	while (len > 0) {
1192		if (m == NULL)
1193			panic("bpf_mcopy");
1194		count = min(m->m_len, len);
1195		bcopy(mtod(m, void *), dst, count);
1196		m = m->m_next;
1197		dst += count;
1198		len -= count;
1199	}
1200}
1201
1202/*
1203 * Incoming linkage from device drivers, when packet is in an mbuf chain.
1204 */
1205void
1206bpf_mtap(bp, m)
1207	struct bpf_if *bp;
1208	struct mbuf *m;
1209{
1210	struct bpf_d *d;
1211	u_int pktlen, slen;
1212
1213	/*
1214	 * Lockless read to avoid cost of locking the interface if there are
1215	 * no descriptors attached.
1216	 */
1217	if (LIST_EMPTY(&bp->bif_dlist))
1218		return;
1219
1220	pktlen = m_length(m, NULL);
1221	if (pktlen == m->m_len) {
1222		bpf_tap(bp, mtod(m, u_char *), pktlen);
1223		return;
1224	}
1225
1226	BPFIF_LOCK(bp);
1227	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1228		if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL))
1229			continue;
1230		BPFD_LOCK(d);
1231		++d->bd_rcount;
1232		slen = bpf_filter(d->bd_filter, (u_char *)m, pktlen, 0);
1233		if (slen != 0)
1234#ifdef MAC
1235			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1236#endif
1237				catchpacket(d, (u_char *)m, pktlen, slen,
1238				    bpf_mcopy);
1239		BPFD_UNLOCK(d);
1240	}
1241	BPFIF_UNLOCK(bp);
1242}
1243
1244/*
1245 * Incoming linkage from device drivers, when packet is in
1246 * an mbuf chain and to be prepended by a contiguous header.
1247 */
1248void
1249bpf_mtap2(bp, data, dlen, m)
1250	struct bpf_if *bp;
1251	void *data;
1252	u_int dlen;
1253	struct mbuf *m;
1254{
1255	struct mbuf mb;
1256	struct bpf_d *d;
1257	u_int pktlen, slen;
1258
1259	/*
1260	 * Lockless read to avoid cost of locking the interface if there are
1261	 * no descriptors attached.
1262	 */
1263	if (LIST_EMPTY(&bp->bif_dlist))
1264		return;
1265
1266	pktlen = m_length(m, NULL);
1267	/*
1268	 * Craft on-stack mbuf suitable for passing to bpf_filter.
1269	 * Note that we cut corners here; we only setup what's
1270	 * absolutely needed--this mbuf should never go anywhere else.
1271	 */
1272	mb.m_next = m;
1273	mb.m_data = data;
1274	mb.m_len = dlen;
1275	pktlen += dlen;
1276
1277	BPFIF_LOCK(bp);
1278	LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
1279		if (!d->bd_seesent && (m->m_pkthdr.rcvif == NULL))
1280			continue;
1281		BPFD_LOCK(d);
1282		++d->bd_rcount;
1283		slen = bpf_filter(d->bd_filter, (u_char *)&mb, pktlen, 0);
1284		if (slen != 0)
1285#ifdef MAC
1286			if (mac_check_bpfdesc_receive(d, bp->bif_ifp) == 0)
1287#endif
1288				catchpacket(d, (u_char *)&mb, pktlen, slen,
1289				    bpf_mcopy);
1290		BPFD_UNLOCK(d);
1291	}
1292	BPFIF_UNLOCK(bp);
1293}
1294
1295/*
1296 * Move the packet data from interface memory (pkt) into the
1297 * store buffer.  "cpfn" is the routine called to do the actual data
1298 * transfer.  bcopy is passed in to copy contiguous chunks, while
1299 * bpf_mcopy is passed in to copy mbuf chains.  In the latter case,
1300 * pkt is really an mbuf.
1301 */
1302static void
1303catchpacket(d, pkt, pktlen, snaplen, cpfn)
1304	struct bpf_d *d;
1305	u_char *pkt;
1306	u_int pktlen, snaplen;
1307	void (*cpfn)(const void *, void *, size_t);
1308{
1309	struct bpf_hdr *hp;
1310	int totlen, curlen;
1311	int hdrlen = d->bd_bif->bif_hdrlen;
1312
1313	/*
1314	 * Figure out how many bytes to move.  If the packet is
1315	 * greater or equal to the snapshot length, transfer that
1316	 * much.  Otherwise, transfer the whole packet (unless
1317	 * we hit the buffer size limit).
1318	 */
1319	totlen = hdrlen + min(snaplen, pktlen);
1320	if (totlen > d->bd_bufsize)
1321		totlen = d->bd_bufsize;
1322
1323	/*
1324	 * Round up the end of the previous packet to the next longword.
1325	 */
1326	curlen = BPF_WORDALIGN(d->bd_slen);
1327	if (curlen + totlen > d->bd_bufsize) {
1328		/*
1329		 * This packet will overflow the storage buffer.
1330		 * Rotate the buffers if we can, then wakeup any
1331		 * pending reads.
1332		 */
1333		if (d->bd_fbuf == NULL) {
1334			/*
1335			 * We haven't completed the previous read yet,
1336			 * so drop the packet.
1337			 */
1338			++d->bd_dcount;
1339			return;
1340		}
1341		ROTATE_BUFFERS(d);
1342		bpf_wakeup(d);
1343		curlen = 0;
1344	}
1345	else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT)
1346		/*
1347		 * Immediate mode is set, or the read timeout has
1348		 * already expired during a select call.  A packet
1349		 * arrived, so the reader should be woken up.
1350		 */
1351		bpf_wakeup(d);
1352
1353	/*
1354	 * Append the bpf header.
1355	 */
1356	hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
1357	microtime(&hp->bh_tstamp);
1358	hp->bh_datalen = pktlen;
1359	hp->bh_hdrlen = hdrlen;
1360	/*
1361	 * Copy the packet data into the store buffer and update its length.
1362	 */
1363	(*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen));
1364	d->bd_slen = curlen + totlen;
1365}
1366
1367/*
1368 * Initialize all nonzero fields of a descriptor.
1369 */
1370static int
1371bpf_allocbufs(d)
1372	struct bpf_d *d;
1373{
1374	d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1375	if (d->bd_fbuf == NULL)
1376		return (ENOBUFS);
1377
1378	d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
1379	if (d->bd_sbuf == NULL) {
1380		free(d->bd_fbuf, M_BPF);
1381		return (ENOBUFS);
1382	}
1383	d->bd_slen = 0;
1384	d->bd_hlen = 0;
1385	return (0);
1386}
1387
1388/*
1389 * Free buffers currently in use by a descriptor.
1390 * Called on close.
1391 */
1392static void
1393bpf_freed(d)
1394	struct bpf_d *d;
1395{
1396	/*
1397	 * We don't need to lock out interrupts since this descriptor has
1398	 * been detached from its interface and it yet hasn't been marked
1399	 * free.
1400	 */
1401	if (d->bd_sbuf != NULL) {
1402		free(d->bd_sbuf, M_BPF);
1403		if (d->bd_hbuf != NULL)
1404			free(d->bd_hbuf, M_BPF);
1405		if (d->bd_fbuf != NULL)
1406			free(d->bd_fbuf, M_BPF);
1407	}
1408	if (d->bd_filter)
1409		free((caddr_t)d->bd_filter, M_BPF);
1410	mtx_destroy(&d->bd_mtx);
1411}
1412
1413/*
1414 * Attach an interface to bpf.  dlt is the link layer type; hdrlen is the
1415 * fixed size of the link header (variable length headers not yet supported).
1416 */
1417void
1418bpfattach(ifp, dlt, hdrlen)
1419	struct ifnet *ifp;
1420	u_int dlt, hdrlen;
1421{
1422
1423	bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
1424}
1425
1426/*
1427 * Attach an interface to bpf.  ifp is a pointer to the structure
1428 * defining the interface to be attached, dlt is the link layer type,
1429 * and hdrlen is the fixed size of the link header (variable length
1430 * headers are not yet supporrted).
1431 */
1432void
1433bpfattach2(ifp, dlt, hdrlen, driverp)
1434	struct ifnet *ifp;
1435	u_int dlt, hdrlen;
1436	struct bpf_if **driverp;
1437{
1438	struct bpf_if *bp;
1439	bp = (struct bpf_if *)malloc(sizeof(*bp), M_BPF, M_NOWAIT | M_ZERO);
1440	if (bp == NULL)
1441		panic("bpfattach");
1442
1443	LIST_INIT(&bp->bif_dlist);
1444	bp->bif_driverp = driverp;
1445	bp->bif_ifp = ifp;
1446	bp->bif_dlt = dlt;
1447	mtx_init(&bp->bif_mtx, "bpf interface lock", NULL, MTX_DEF);
1448
1449	mtx_lock(&bpf_mtx);
1450	LIST_INSERT_HEAD(&bpf_iflist, bp, bif_next);
1451	mtx_unlock(&bpf_mtx);
1452
1453	*bp->bif_driverp = NULL;
1454
1455	/*
1456	 * Compute the length of the bpf header.  This is not necessarily
1457	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1458	 * that the network layer header begins on a longword boundary (for
1459	 * performance reasons and to alleviate alignment restrictions).
1460	 */
1461	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
1462
1463	if (bootverbose)
1464		if_printf(ifp, "bpf attached\n");
1465}
1466
1467/*
1468 * Detach bpf from an interface.  This involves detaching each descriptor
1469 * associated with the interface, and leaving bd_bif NULL.  Notify each
1470 * descriptor as it's detached so that any sleepers wake up and get
1471 * ENXIO.
1472 */
1473void
1474bpfdetach(ifp)
1475	struct ifnet *ifp;
1476{
1477	struct bpf_if	*bp;
1478	struct bpf_d	*d;
1479
1480	/* Locate BPF interface information */
1481	mtx_lock(&bpf_mtx);
1482	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1483		if (ifp == bp->bif_ifp)
1484			break;
1485	}
1486
1487	/* Interface wasn't attached */
1488	if ((bp == NULL) || (bp->bif_ifp == NULL)) {
1489		mtx_unlock(&bpf_mtx);
1490		printf("bpfdetach: %s was not attached\n", ifp->if_xname);
1491		return;
1492	}
1493
1494	LIST_REMOVE(bp, bif_next);
1495	mtx_unlock(&bpf_mtx);
1496
1497	while ((d = LIST_FIRST(&bp->bif_dlist)) != NULL) {
1498		bpf_detachd(d);
1499		BPFD_LOCK(d);
1500		bpf_wakeup(d);
1501		BPFD_UNLOCK(d);
1502	}
1503
1504	mtx_destroy(&bp->bif_mtx);
1505	free(bp, M_BPF);
1506}
1507
1508/*
1509 * Get a list of available data link type of the interface.
1510 */
1511static int
1512bpf_getdltlist(d, bfl)
1513	struct bpf_d *d;
1514	struct bpf_dltlist *bfl;
1515{
1516	int n, error;
1517	struct ifnet *ifp;
1518	struct bpf_if *bp;
1519
1520	ifp = d->bd_bif->bif_ifp;
1521	n = 0;
1522	error = 0;
1523	mtx_lock(&bpf_mtx);
1524	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1525		if (bp->bif_ifp != ifp)
1526			continue;
1527		if (bfl->bfl_list != NULL) {
1528			if (n >= bfl->bfl_len) {
1529				mtx_unlock(&bpf_mtx);
1530				return (ENOMEM);
1531			}
1532			error = copyout(&bp->bif_dlt,
1533			    bfl->bfl_list + n, sizeof(u_int));
1534		}
1535		n++;
1536	}
1537	mtx_unlock(&bpf_mtx);
1538	bfl->bfl_len = n;
1539	return (error);
1540}
1541
1542/*
1543 * Set the data link type of a BPF instance.
1544 */
1545static int
1546bpf_setdlt(d, dlt)
1547	struct bpf_d *d;
1548	u_int dlt;
1549{
1550	int error, opromisc;
1551	struct ifnet *ifp;
1552	struct bpf_if *bp;
1553
1554	if (d->bd_bif->bif_dlt == dlt)
1555		return (0);
1556	ifp = d->bd_bif->bif_ifp;
1557	mtx_lock(&bpf_mtx);
1558	LIST_FOREACH(bp, &bpf_iflist, bif_next) {
1559		if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
1560			break;
1561	}
1562	mtx_unlock(&bpf_mtx);
1563	if (bp != NULL) {
1564		opromisc = d->bd_promisc;
1565		bpf_detachd(d);
1566		bpf_attachd(d, bp);
1567		BPFD_LOCK(d);
1568		reset_d(d);
1569		BPFD_UNLOCK(d);
1570		if (opromisc) {
1571			error = ifpromisc(bp->bif_ifp, 1);
1572			if (error)
1573				if_printf(bp->bif_ifp,
1574					"bpf_setdlt: ifpromisc failed (%d)\n",
1575					error);
1576			else
1577				d->bd_promisc = 1;
1578		}
1579	}
1580	return (bp == NULL ? EINVAL : 0);
1581}
1582
1583static void bpf_drvinit(void *unused);
1584
1585static void bpf_clone(void *arg, char *name, int namelen, struct cdev **dev);
1586
1587static void
1588bpf_clone(arg, name, namelen, dev)
1589	void *arg;
1590	char *name;
1591	int namelen;
1592	struct cdev **dev;
1593{
1594	int u;
1595
1596	if (*dev != NULL)
1597		return;
1598	if (dev_stdclone(name, NULL, "bpf", &u) != 1)
1599		return;
1600	*dev = make_dev(&bpf_cdevsw, unit2minor(u), UID_ROOT, GID_WHEEL, 0600,
1601	    "bpf%d", u);
1602	(*dev)->si_flags |= SI_CHEAPCLONE;
1603	return;
1604}
1605
1606static void
1607bpf_drvinit(unused)
1608	void *unused;
1609{
1610
1611	mtx_init(&bpf_mtx, "bpf global lock", NULL, MTX_DEF);
1612	LIST_INIT(&bpf_iflist);
1613	EVENTHANDLER_REGISTER(dev_clone, bpf_clone, 0, 1000);
1614}
1615
1616SYSINIT(bpfdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,bpf_drvinit,NULL)
1617
1618#else /* !DEV_BPF && !NETGRAPH_BPF */
1619/*
1620 * NOP stubs to allow bpf-using drivers to load and function.
1621 *
1622 * A 'better' implementation would allow the core bpf functionality
1623 * to be loaded at runtime.
1624 */
1625
1626void
1627bpf_tap(bp, pkt, pktlen)
1628	struct bpf_if *bp;
1629	u_char *pkt;
1630	u_int pktlen;
1631{
1632}
1633
1634void
1635bpf_mtap(bp, m)
1636	struct bpf_if *bp;
1637	struct mbuf *m;
1638{
1639}
1640
1641void
1642bpf_mtap2(bp, d, l, m)
1643	struct bpf_if *bp;
1644	void *d;
1645	u_int l;
1646	struct mbuf *m;
1647{
1648}
1649
1650void
1651bpfattach(ifp, dlt, hdrlen)
1652	struct ifnet *ifp;
1653	u_int dlt, hdrlen;
1654{
1655}
1656
1657void
1658bpfattach2(ifp, dlt, hdrlen, driverp)
1659	struct ifnet *ifp;
1660	u_int dlt, hdrlen;
1661	struct bpf_if **driverp;
1662{
1663}
1664
1665void
1666bpfdetach(ifp)
1667	struct ifnet *ifp;
1668{
1669}
1670
1671u_int
1672bpf_filter(pc, p, wirelen, buflen)
1673	const struct bpf_insn *pc;
1674	u_char *p;
1675	u_int wirelen;
1676	u_int buflen;
1677{
1678	return -1;	/* "no filter" behaviour */
1679}
1680
1681int
1682bpf_validate(f, len)
1683	const struct bpf_insn *f;
1684	int len;
1685{
1686	return 0;		/* false */
1687}
1688
1689#endif /* !DEV_BPF && !NETGRAPH_BPF */
1690