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