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