Home | History | Annotate | Line # | Download | only in net
bpf.c revision 1.96
      1 /*	$NetBSD: bpf.c,v 1.96 2004/04/30 22:07:21 dyoung Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1990, 1991, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from the Stanford/CMU enet packet filter,
      8  * (net/enet.c) distributed as part of 4.3BSD, and code contributed
      9  * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
     10  * Berkeley Laboratory.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *	@(#)bpf.c	8.4 (Berkeley) 1/9/95
     37  * static char rcsid[] =
     38  * "Header: bpf.c,v 1.67 96/09/26 22:00:52 leres Exp ";
     39  */
     40 
     41 #include <sys/cdefs.h>
     42 __KERNEL_RCSID(0, "$NetBSD: bpf.c,v 1.96 2004/04/30 22:07:21 dyoung Exp $");
     43 
     44 #include "bpfilter.h"
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/mbuf.h>
     49 #include <sys/buf.h>
     50 #include <sys/time.h>
     51 #include <sys/proc.h>
     52 #include <sys/user.h>
     53 #include <sys/ioctl.h>
     54 #include <sys/conf.h>
     55 #include <sys/vnode.h>
     56 
     57 #include <sys/file.h>
     58 #include <sys/tty.h>
     59 #include <sys/uio.h>
     60 
     61 #include <sys/protosw.h>
     62 #include <sys/socket.h>
     63 #include <sys/errno.h>
     64 #include <sys/kernel.h>
     65 #include <sys/poll.h>
     66 #include <sys/sysctl.h>
     67 
     68 #include <net/if.h>
     69 
     70 #include <net/bpf.h>
     71 #include <net/bpfdesc.h>
     72 
     73 #include <net/if_arc.h>
     74 #include <net/if_ether.h>
     75 
     76 #include <netinet/in.h>
     77 #include <netinet/if_inarp.h>
     78 
     79 #if defined(_KERNEL_OPT)
     80 #include "opt_bpf.h"
     81 #endif
     82 
     83 #ifndef BPF_BUFSIZE
     84 /*
     85  * 4096 is too small for FDDI frames. 8192 is too small for gigabit Ethernet
     86  * jumbos (circa 9k), ATM, or Intel gig/10gig ethernet jumbos (16k).
     87  */
     88 # define BPF_BUFSIZE 32768
     89 #endif
     90 
     91 #define PRINET  26			/* interruptible */
     92 
     93 /*
     94  * The default read buffer size, and limit for BIOCSBLEN, is sysctl'able.
     95  * XXX the default values should be computed dynamically based
     96  * on available memory size and available mbuf clusters.
     97  */
     98 int bpf_bufsize = BPF_BUFSIZE;
     99 int bpf_maxbufsize = BPF_DFLTBUFSIZE;	/* XXX set dynamically, see above */
    100 
    101 /*
    102  *  bpf_iflist is the list of interfaces; each corresponds to an ifnet
    103  *  bpf_dtab holds the descriptors, indexed by minor device #
    104  */
    105 struct bpf_if	*bpf_iflist;
    106 struct bpf_d	bpf_dtab[NBPFILTER];
    107 
    108 static int	bpf_allocbufs __P((struct bpf_d *));
    109 static void	bpf_freed __P((struct bpf_d *));
    110 static void	bpf_ifname __P((struct ifnet *, struct ifreq *));
    111 static void	*bpf_mcpy __P((void *, const void *, size_t));
    112 static int	bpf_movein __P((struct uio *, int, int,
    113 			        struct mbuf **, struct sockaddr *));
    114 static void	bpf_attachd __P((struct bpf_d *, struct bpf_if *));
    115 static void	bpf_detachd __P((struct bpf_d *));
    116 static int	bpf_setif __P((struct bpf_d *, struct ifreq *));
    117 static void	bpf_timed_out __P((void *));
    118 static __inline void
    119 		bpf_wakeup __P((struct bpf_d *));
    120 static void	catchpacket __P((struct bpf_d *, u_char *, u_int, u_int,
    121 				 void *(*)(void *, const void *, size_t)));
    122 static void	reset_d __P((struct bpf_d *));
    123 static int	bpf_getdltlist __P((struct bpf_d *, struct bpf_dltlist *));
    124 static int	bpf_setdlt __P((struct bpf_d *, u_int));
    125 
    126 dev_type_open(bpfopen);
    127 dev_type_close(bpfclose);
    128 dev_type_read(bpfread);
    129 dev_type_write(bpfwrite);
    130 dev_type_ioctl(bpfioctl);
    131 dev_type_poll(bpfpoll);
    132 dev_type_kqfilter(bpfkqfilter);
    133 
    134 const struct cdevsw bpf_cdevsw = {
    135 	bpfopen, bpfclose, bpfread, bpfwrite, bpfioctl,
    136 	nostop, notty, bpfpoll, nommap, bpfkqfilter,
    137 };
    138 
    139 static int
    140 bpf_movein(uio, linktype, mtu, mp, sockp)
    141 	struct uio *uio;
    142 	int linktype;
    143 	int mtu;
    144 	struct mbuf **mp;
    145 	struct sockaddr *sockp;
    146 {
    147 	struct mbuf *m;
    148 	int error;
    149 	int len;
    150 	int hlen;
    151 	int align;
    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 		align = 0;
    168 		break;
    169 
    170 	case DLT_PPP:
    171 		sockp->sa_family = AF_UNSPEC;
    172 		hlen = 0;
    173 		align = 0;
    174 		break;
    175 
    176 	case DLT_EN10MB:
    177 		sockp->sa_family = AF_UNSPEC;
    178 		/* XXX Would MAXLINKHDR be better? */
    179  		/* 6(dst)+6(src)+2(type) */
    180 		hlen = sizeof(struct ether_header);
    181 		align = 2;
    182 		break;
    183 
    184 	case DLT_ARCNET:
    185 		sockp->sa_family = AF_UNSPEC;
    186 		hlen = ARC_HDRLEN;
    187 		align = 5;
    188 		break;
    189 
    190 	case DLT_FDDI:
    191 		sockp->sa_family = AF_LINK;
    192 		/* XXX 4(FORMAC)+6(dst)+6(src) */
    193 		hlen = 16;
    194 		align = 0;
    195 		break;
    196 
    197 	case DLT_ECONET:
    198 		sockp->sa_family = AF_UNSPEC;
    199 		hlen = 6;
    200 		align = 2;
    201 		break;
    202 
    203 	case DLT_NULL:
    204 		sockp->sa_family = AF_UNSPEC;
    205 		hlen = 0;
    206 		align = 0;
    207 		break;
    208 
    209 	default:
    210 		return (EIO);
    211 	}
    212 
    213 	len = uio->uio_resid;
    214 	/*
    215 	 * If there aren't enough bytes for a link level header or the
    216 	 * packet length exceeds the interface mtu, return an error.
    217 	 */
    218 	if (len < hlen || len - hlen > mtu)
    219 		return (EMSGSIZE);
    220 
    221 	/*
    222 	 * XXX Avoid complicated buffer chaining ---
    223 	 * bail if it won't fit in a single mbuf.
    224 	 * (Take into account possible alignment bytes)
    225 	 */
    226 	if ((unsigned)len > MCLBYTES - align)
    227 		return (EIO);
    228 
    229 	m = m_gethdr(M_WAIT, MT_DATA);
    230 	m->m_pkthdr.rcvif = 0;
    231 	m->m_pkthdr.len = len - hlen;
    232 	if (len > MHLEN - align) {
    233 		m_clget(m, M_WAIT);
    234 		if ((m->m_flags & M_EXT) == 0) {
    235 			error = ENOBUFS;
    236 			goto bad;
    237 		}
    238 	}
    239 
    240 	/* Insure the data is properly aligned */
    241 	if (align > 0) {
    242 		m->m_data += align;
    243 		m->m_len -= align;
    244 	}
    245 
    246 	error = uiomove(mtod(m, caddr_t), len, uio);
    247 	if (error)
    248 		goto bad;
    249 	if (hlen != 0) {
    250 		memcpy(sockp->sa_data, mtod(m, caddr_t), hlen);
    251 		m->m_data += hlen; /* XXX */
    252 		len -= hlen;
    253 	}
    254 	m->m_len = len;
    255 	*mp = m;
    256 	return (0);
    257 
    258 bad:
    259 	m_freem(m);
    260 	return (error);
    261 }
    262 
    263 /*
    264  * Attach file to the bpf interface, i.e. make d listen on bp.
    265  * Must be called at splnet.
    266  */
    267 static void
    268 bpf_attachd(d, bp)
    269 	struct bpf_d *d;
    270 	struct bpf_if *bp;
    271 {
    272 	/*
    273 	 * Point d at bp, and add d to the interface's list of listeners.
    274 	 * Finally, point the driver's bpf cookie at the interface so
    275 	 * it will divert packets to bpf.
    276 	 */
    277 	d->bd_bif = bp;
    278 	d->bd_next = bp->bif_dlist;
    279 	bp->bif_dlist = d;
    280 
    281 	*bp->bif_driverp = bp;
    282 }
    283 
    284 /*
    285  * Detach a file from its interface.
    286  */
    287 static void
    288 bpf_detachd(d)
    289 	struct bpf_d *d;
    290 {
    291 	struct bpf_d **p;
    292 	struct bpf_if *bp;
    293 
    294 	bp = d->bd_bif;
    295 	/*
    296 	 * Check if this descriptor had requested promiscuous mode.
    297 	 * If so, turn it off.
    298 	 */
    299 	if (d->bd_promisc) {
    300 		int error;
    301 
    302 		d->bd_promisc = 0;
    303 		/*
    304 		 * Take device out of promiscuous mode.  Since we were
    305 		 * able to enter promiscuous mode, we should be able
    306 		 * to turn it off.  But we can get an error if
    307 		 * the interface was configured down, so only panic
    308 		 * if we don't get an unexpected error.
    309 		 */
    310   		error = ifpromisc(bp->bif_ifp, 0);
    311 		if (error && error != EINVAL)
    312 			panic("bpf: ifpromisc failed");
    313 	}
    314 	/* Remove d from the interface's descriptor list. */
    315 	p = &bp->bif_dlist;
    316 	while (*p != d) {
    317 		p = &(*p)->bd_next;
    318 		if (*p == 0)
    319 			panic("bpf_detachd: descriptor not in list");
    320 	}
    321 	*p = (*p)->bd_next;
    322 	if (bp->bif_dlist == 0)
    323 		/*
    324 		 * Let the driver know that there are no more listeners.
    325 		 */
    326 		*d->bd_bif->bif_driverp = 0;
    327 	d->bd_bif = 0;
    328 }
    329 
    330 
    331 /*
    332  * Mark a descriptor free by making it point to itself.
    333  * This is probably cheaper than marking with a constant since
    334  * the address should be in a register anyway.
    335  */
    336 #define D_ISFREE(d) ((d) == (d)->bd_next)
    337 #define D_MARKFREE(d) ((d)->bd_next = (d))
    338 #define D_MARKUSED(d) ((d)->bd_next = 0)
    339 
    340 /*
    341  * bpfilterattach() is called at boot time.
    342  */
    343 /* ARGSUSED */
    344 void
    345 bpfilterattach(n)
    346 	int n;
    347 {
    348 	int i;
    349 	/*
    350 	 * Mark all the descriptors free.
    351 	 */
    352 	for (i = 0; i < NBPFILTER; ++i)
    353 		D_MARKFREE(&bpf_dtab[i]);
    354 
    355 }
    356 
    357 /*
    358  * Open ethernet device.  Returns ENXIO for illegal minor device number,
    359  * EBUSY if file is open by another process.
    360  */
    361 /* ARGSUSED */
    362 int
    363 bpfopen(dev, flag, mode, p)
    364 	dev_t dev;
    365 	int flag;
    366 	int mode;
    367 	struct proc *p;
    368 {
    369 	struct bpf_d *d;
    370 
    371 	if (minor(dev) >= NBPFILTER)
    372 		return (ENXIO);
    373 	/*
    374 	 * Each minor can be opened by only one process.  If the requested
    375 	 * minor is in use, return EBUSY.
    376 	 */
    377 	d = &bpf_dtab[minor(dev)];
    378 	if (!D_ISFREE(d))
    379 		return (EBUSY);
    380 
    381 	/* Mark "free" and do most initialization. */
    382 	memset((char *)d, 0, sizeof(*d));
    383 	d->bd_bufsize = bpf_bufsize;
    384 	d->bd_seesent = 1;
    385 	callout_init(&d->bd_callout);
    386 
    387 	return (0);
    388 }
    389 
    390 /*
    391  * Close the descriptor by detaching it from its interface,
    392  * deallocating its buffers, and marking it free.
    393  */
    394 /* ARGSUSED */
    395 int
    396 bpfclose(dev, flag, mode, p)
    397 	dev_t dev;
    398 	int flag;
    399 	int mode;
    400 	struct proc *p;
    401 {
    402 	struct bpf_d *d = &bpf_dtab[minor(dev)];
    403 	int s;
    404 
    405 	s = splnet();
    406 	if (d->bd_state == BPF_WAITING)
    407 		callout_stop(&d->bd_callout);
    408 	d->bd_state = BPF_IDLE;
    409 	if (d->bd_bif)
    410 		bpf_detachd(d);
    411 	splx(s);
    412 	bpf_freed(d);
    413 
    414 	return (0);
    415 }
    416 
    417 /*
    418  * Rotate the packet buffers in descriptor d.  Move the store buffer
    419  * into the hold slot, and the free buffer into the store slot.
    420  * Zero the length of the new store buffer.
    421  */
    422 #define ROTATE_BUFFERS(d) \
    423 	(d)->bd_hbuf = (d)->bd_sbuf; \
    424 	(d)->bd_hlen = (d)->bd_slen; \
    425 	(d)->bd_sbuf = (d)->bd_fbuf; \
    426 	(d)->bd_slen = 0; \
    427 	(d)->bd_fbuf = 0;
    428 /*
    429  *  bpfread - read next chunk of packets from buffers
    430  */
    431 int
    432 bpfread(dev, uio, ioflag)
    433 	dev_t dev;
    434 	struct uio *uio;
    435 	int ioflag;
    436 {
    437 	struct bpf_d *d = &bpf_dtab[minor(dev)];
    438 	int timed_out;
    439 	int error;
    440 	int s;
    441 
    442 	/*
    443 	 * Restrict application to use a buffer the same size as
    444 	 * as kernel buffers.
    445 	 */
    446 	if (uio->uio_resid != d->bd_bufsize)
    447 		return (EINVAL);
    448 
    449 	s = splnet();
    450 	if (d->bd_state == BPF_WAITING)
    451 		callout_stop(&d->bd_callout);
    452 	timed_out = (d->bd_state == BPF_TIMED_OUT);
    453 	d->bd_state = BPF_IDLE;
    454 	/*
    455 	 * If the hold buffer is empty, then do a timed sleep, which
    456 	 * ends when the timeout expires or when enough packets
    457 	 * have arrived to fill the store buffer.
    458 	 */
    459 	while (d->bd_hbuf == 0) {
    460 		if (ioflag & IO_NDELAY) {
    461 			if (d->bd_slen == 0) {
    462 				splx(s);
    463 				return (EWOULDBLOCK);
    464 			}
    465 			ROTATE_BUFFERS(d);
    466 			break;
    467 		}
    468 
    469 		if ((d->bd_immediate || timed_out) && d->bd_slen != 0) {
    470 			/*
    471 			 * A packet(s) either arrived since the previous
    472 			 * read or arrived while we were asleep.
    473 			 * Rotate the buffers and return what's here.
    474 			 */
    475 			ROTATE_BUFFERS(d);
    476 			break;
    477 		}
    478 		error = tsleep((caddr_t)d, PRINET|PCATCH, "bpf",
    479 				d->bd_rtout);
    480 		if (error == EINTR || error == ERESTART) {
    481 			splx(s);
    482 			return (error);
    483 		}
    484 		if (error == EWOULDBLOCK) {
    485 			/*
    486 			 * On a timeout, return what's in the buffer,
    487 			 * which may be nothing.  If there is something
    488 			 * in the store buffer, we can rotate the buffers.
    489 			 */
    490 			if (d->bd_hbuf)
    491 				/*
    492 				 * We filled up the buffer in between
    493 				 * getting the timeout and arriving
    494 				 * here, so we don't need to rotate.
    495 				 */
    496 				break;
    497 
    498 			if (d->bd_slen == 0) {
    499 				splx(s);
    500 				return (0);
    501 			}
    502 			ROTATE_BUFFERS(d);
    503 			break;
    504 		}
    505 		if (error != 0)
    506 			goto done;
    507 	}
    508 	/*
    509 	 * At this point, we know we have something in the hold slot.
    510 	 */
    511 	splx(s);
    512 
    513 	/*
    514 	 * Move data from hold buffer into user space.
    515 	 * We know the entire buffer is transferred since
    516 	 * we checked above that the read buffer is bpf_bufsize bytes.
    517 	 */
    518 	error = uiomove(d->bd_hbuf, d->bd_hlen, uio);
    519 
    520 	s = splnet();
    521 	d->bd_fbuf = d->bd_hbuf;
    522 	d->bd_hbuf = 0;
    523 	d->bd_hlen = 0;
    524 done:
    525 	splx(s);
    526 	return (error);
    527 }
    528 
    529 
    530 /*
    531  * If there are processes sleeping on this descriptor, wake them up.
    532  */
    533 static __inline void
    534 bpf_wakeup(d)
    535 	struct bpf_d *d;
    536 {
    537 	wakeup((caddr_t)d);
    538 	if (d->bd_async)
    539 		fownsignal(d->bd_pgid, SIGIO, 0, 0, NULL);
    540 
    541 	selnotify(&d->bd_sel, 0);
    542 	/* XXX */
    543 	d->bd_sel.sel_pid = 0;
    544 }
    545 
    546 
    547 static void
    548 bpf_timed_out(arg)
    549 	void *arg;
    550 {
    551 	struct bpf_d *d = (struct bpf_d *)arg;
    552 	int s;
    553 
    554 	s = splnet();
    555 	if (d->bd_state == BPF_WAITING) {
    556 		d->bd_state = BPF_TIMED_OUT;
    557 		if (d->bd_slen != 0)
    558 			bpf_wakeup(d);
    559 	}
    560 	splx(s);
    561 }
    562 
    563 
    564 int
    565 bpfwrite(dev, uio, ioflag)
    566 	dev_t dev;
    567 	struct uio *uio;
    568 	int ioflag;
    569 {
    570 	struct bpf_d *d = &bpf_dtab[minor(dev)];
    571 	struct ifnet *ifp;
    572 	struct mbuf *m;
    573 	int error, s;
    574 	static struct sockaddr_storage dst;
    575 
    576 	if (d->bd_bif == 0)
    577 		return (ENXIO);
    578 
    579 	ifp = d->bd_bif->bif_ifp;
    580 
    581 	if (uio->uio_resid == 0)
    582 		return (0);
    583 
    584 	error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, ifp->if_mtu, &m,
    585 		(struct sockaddr *) &dst);
    586 	if (error)
    587 		return (error);
    588 
    589 	if (m->m_pkthdr.len > ifp->if_mtu)
    590 		return (EMSGSIZE);
    591 
    592 	if (d->bd_hdrcmplt)
    593 		dst.ss_family = pseudo_AF_HDRCMPLT;
    594 
    595 	s = splsoftnet();
    596 	error = (*ifp->if_output)(ifp, m, (struct sockaddr *) &dst, NULL);
    597 	splx(s);
    598 	/*
    599 	 * The driver frees the mbuf.
    600 	 */
    601 	return (error);
    602 }
    603 
    604 /*
    605  * Reset a descriptor by flushing its packet buffer and clearing the
    606  * receive and drop counts.  Should be called at splnet.
    607  */
    608 static void
    609 reset_d(d)
    610 	struct bpf_d *d;
    611 {
    612 	if (d->bd_hbuf) {
    613 		/* Free the hold buffer. */
    614 		d->bd_fbuf = d->bd_hbuf;
    615 		d->bd_hbuf = 0;
    616 	}
    617 	d->bd_slen = 0;
    618 	d->bd_hlen = 0;
    619 	d->bd_rcount = 0;
    620 	d->bd_dcount = 0;
    621 	d->bd_ccount = 0;
    622 }
    623 
    624 #ifdef BPF_KERN_FILTER
    625 extern struct bpf_insn *bpf_tcp_filter;
    626 extern struct bpf_insn *bpf_udp_filter;
    627 #endif
    628 
    629 /*
    630  *  FIONREAD		Check for read packet available.
    631  *  BIOCGBLEN		Get buffer len [for read()].
    632  *  BIOCSETF		Set ethernet read filter.
    633  *  BIOCFLUSH		Flush read packet buffer.
    634  *  BIOCPROMISC		Put interface into promiscuous mode.
    635  *  BIOCGDLT		Get link layer type.
    636  *  BIOCGETIF		Get interface name.
    637  *  BIOCSETIF		Set interface.
    638  *  BIOCSRTIMEOUT	Set read timeout.
    639  *  BIOCGRTIMEOUT	Get read timeout.
    640  *  BIOCGSTATS		Get packet stats.
    641  *  BIOCIMMEDIATE	Set immediate mode.
    642  *  BIOCVERSION		Get filter language version.
    643  *  BIOGHDRCMPLT	Get "header already complete" flag.
    644  *  BIOSHDRCMPLT	Set "header already complete" flag.
    645  */
    646 /* ARGSUSED */
    647 int
    648 bpfioctl(dev, cmd, addr, flag, p)
    649 	dev_t dev;
    650 	u_long cmd;
    651 	caddr_t addr;
    652 	int flag;
    653 	struct proc *p;
    654 {
    655 	struct bpf_d *d = &bpf_dtab[minor(dev)];
    656 	int s, error = 0;
    657 #ifdef BPF_KERN_FILTER
    658 	struct bpf_insn **p;
    659 #endif
    660 
    661 	s = splnet();
    662 	if (d->bd_state == BPF_WAITING)
    663 		callout_stop(&d->bd_callout);
    664 	d->bd_state = BPF_IDLE;
    665 	splx(s);
    666 
    667 	switch (cmd) {
    668 
    669 	default:
    670 		error = EINVAL;
    671 		break;
    672 
    673 	/*
    674 	 * Check for read packet available.
    675 	 */
    676 	case FIONREAD:
    677 		{
    678 			int n;
    679 
    680 			s = splnet();
    681 			n = d->bd_slen;
    682 			if (d->bd_hbuf)
    683 				n += d->bd_hlen;
    684 			splx(s);
    685 
    686 			*(int *)addr = n;
    687 			break;
    688 		}
    689 
    690 	/*
    691 	 * Get buffer len [for read()].
    692 	 */
    693 	case BIOCGBLEN:
    694 		*(u_int *)addr = d->bd_bufsize;
    695 		break;
    696 
    697 	/*
    698 	 * Set buffer length.
    699 	 */
    700 	case BIOCSBLEN:
    701 		if (d->bd_bif != 0)
    702 			error = EINVAL;
    703 		else {
    704 			u_int size = *(u_int *)addr;
    705 
    706 			if (size > bpf_maxbufsize)
    707 				*(u_int *)addr = size = bpf_maxbufsize;
    708 			else if (size < BPF_MINBUFSIZE)
    709 				*(u_int *)addr = size = BPF_MINBUFSIZE;
    710 			d->bd_bufsize = size;
    711 		}
    712 		break;
    713 
    714 	/*
    715 	 * Set link layer read filter.
    716 	 */
    717 	case BIOCSETF:
    718 		error = bpf_setf(d, (struct bpf_program *)addr);
    719 		break;
    720 
    721 #ifdef BPF_KERN_FILTER
    722 	/*
    723 	 * Set TCP or UDP reject filter.
    724 	 */
    725 	case BIOCSTCPF:
    726 	case BIOCSUDPF:
    727 		if (!suser()) {
    728 			error = EPERM;
    729 			break;
    730 		}
    731 
    732 		/* Validate and store filter */
    733 		error = bpf_setf(d, (struct bpf_program *)addr);
    734 
    735 		/* Free possible old filter */
    736 		if (cmd == BIOCSTCPF)
    737 			p = &bpf_tcp_filter;
    738 		else
    739 			p = &bpf_udp_filter;
    740 		if (*p != NULL)
    741 			free((caddr_t)*p, M_DEVBUF);
    742 
    743 		/* Steal new filter (noop if error) */
    744 		s = splnet();
    745 		*p = d->bd_filter;
    746 		d->bd_filter = NULL;
    747 		splx(s);
    748 		break;
    749 #endif
    750 
    751 	/*
    752 	 * Flush read packet buffer.
    753 	 */
    754 	case BIOCFLUSH:
    755 		s = splnet();
    756 		reset_d(d);
    757 		splx(s);
    758 		break;
    759 
    760 	/*
    761 	 * Put interface into promiscuous mode.
    762 	 */
    763 	case BIOCPROMISC:
    764 		if (d->bd_bif == 0) {
    765 			/*
    766 			 * No interface attached yet.
    767 			 */
    768 			error = EINVAL;
    769 			break;
    770 		}
    771 		s = splnet();
    772 		if (d->bd_promisc == 0) {
    773 			error = ifpromisc(d->bd_bif->bif_ifp, 1);
    774 			if (error == 0)
    775 				d->bd_promisc = 1;
    776 		}
    777 		splx(s);
    778 		break;
    779 
    780 	/*
    781 	 * Get device parameters.
    782 	 */
    783 	case BIOCGDLT:
    784 		if (d->bd_bif == 0)
    785 			error = EINVAL;
    786 		else
    787 			*(u_int *)addr = d->bd_bif->bif_dlt;
    788 		break;
    789 
    790 	/*
    791 	 * Get a list of supported device parameters.
    792 	 */
    793 	case BIOCGDLTLIST:
    794 		if (d->bd_bif == 0)
    795 			error = EINVAL;
    796 		else
    797 			error = bpf_getdltlist(d, (struct bpf_dltlist *)addr);
    798 		break;
    799 
    800 	/*
    801 	 * Set device parameters.
    802 	 */
    803 	case BIOCSDLT:
    804 		if (d->bd_bif == 0)
    805 			error = EINVAL;
    806 		else
    807 			error = bpf_setdlt(d, *(u_int *)addr);
    808 		break;
    809 
    810 	/*
    811 	 * Set interface name.
    812 	 */
    813 	case BIOCGETIF:
    814 		if (d->bd_bif == 0)
    815 			error = EINVAL;
    816 		else
    817 			bpf_ifname(d->bd_bif->bif_ifp, (struct ifreq *)addr);
    818 		break;
    819 
    820 	/*
    821 	 * Set interface.
    822 	 */
    823 	case BIOCSETIF:
    824 		error = bpf_setif(d, (struct ifreq *)addr);
    825 		break;
    826 
    827 	/*
    828 	 * Set read timeout.
    829 	 */
    830 	case BIOCSRTIMEOUT:
    831 		{
    832 			struct timeval *tv = (struct timeval *)addr;
    833 
    834 			/* Compute number of ticks. */
    835 			d->bd_rtout = tv->tv_sec * hz + tv->tv_usec / tick;
    836 			if ((d->bd_rtout == 0) && (tv->tv_usec != 0))
    837 				d->bd_rtout = 1;
    838 			break;
    839 		}
    840 
    841 	/*
    842 	 * Get read timeout.
    843 	 */
    844 	case BIOCGRTIMEOUT:
    845 		{
    846 			struct timeval *tv = (struct timeval *)addr;
    847 
    848 			tv->tv_sec = d->bd_rtout / hz;
    849 			tv->tv_usec = (d->bd_rtout % hz) * tick;
    850 			break;
    851 		}
    852 
    853 	/*
    854 	 * Get packet stats.
    855 	 */
    856 	case BIOCGSTATS:
    857 		{
    858 			struct bpf_stat *bs = (struct bpf_stat *)addr;
    859 
    860 			bs->bs_recv = d->bd_rcount;
    861 			bs->bs_drop = d->bd_dcount;
    862 			bs->bs_capt = d->bd_ccount;
    863 			break;
    864 		}
    865 
    866 	case BIOCGSTATSOLD:
    867 		{
    868 			struct bpf_stat_old *bs = (struct bpf_stat_old *)addr;
    869 
    870 			bs->bs_recv = d->bd_rcount;
    871 			bs->bs_drop = d->bd_dcount;
    872 			break;
    873 		}
    874 
    875 	/*
    876 	 * Set immediate mode.
    877 	 */
    878 	case BIOCIMMEDIATE:
    879 		d->bd_immediate = *(u_int *)addr;
    880 		break;
    881 
    882 	case BIOCVERSION:
    883 		{
    884 			struct bpf_version *bv = (struct bpf_version *)addr;
    885 
    886 			bv->bv_major = BPF_MAJOR_VERSION;
    887 			bv->bv_minor = BPF_MINOR_VERSION;
    888 			break;
    889 		}
    890 
    891 	case BIOCGHDRCMPLT:	/* get "header already complete" flag */
    892 		*(u_int *)addr = d->bd_hdrcmplt;
    893 		break;
    894 
    895 	case BIOCSHDRCMPLT:	/* set "header already complete" flag */
    896 		d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
    897 		break;
    898 
    899 	/*
    900 	 * Get "see sent packets" flag
    901 	 */
    902 	case BIOCGSEESENT:
    903 		*(u_int *)addr = d->bd_seesent;
    904 		break;
    905 
    906 	/*
    907 	 * Set "see sent" packets flag
    908 	 */
    909 	case BIOCSSEESENT:
    910 		d->bd_seesent = *(u_int *)addr;
    911 		break;
    912 
    913 	case FIONBIO:		/* Non-blocking I/O */
    914 		/*
    915 		 * No need to do anything special as we use IO_NDELAY in
    916 		 * bpfread() as an indication of whether or not to block
    917 		 * the read.
    918 		 */
    919 		break;
    920 
    921 	case FIOASYNC:		/* Send signal on receive packets */
    922 		d->bd_async = *(int *)addr;
    923 		break;
    924 
    925 	case TIOCSPGRP:		/* Process or group to send signals to */
    926 	case FIOSETOWN:
    927 		error = fsetown(p, &d->bd_pgid, cmd, addr);
    928 		break;
    929 
    930 	case TIOCGPGRP:
    931 	case FIOGETOWN:
    932 		error = fgetown(p, d->bd_pgid, cmd, addr);
    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  */
    942 int
    943 bpf_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 	int s;
    950 
    951 	old = d->bd_filter;
    952 	if (fp->bf_insns == 0) {
    953 		if (fp->bf_len != 0)
    954 			return (EINVAL);
    955 		s = splnet();
    956 		d->bd_filter = 0;
    957 		reset_d(d);
    958 		splx(s);
    959 		if (old != 0)
    960 			free((caddr_t)old, M_DEVBUF);
    961 		return (0);
    962 	}
    963 	flen = fp->bf_len;
    964 	if (flen > BPF_MAXINSNS)
    965 		return (EINVAL);
    966 
    967 	size = flen * sizeof(*fp->bf_insns);
    968 	fcode = (struct bpf_insn *)malloc(size, M_DEVBUF, M_WAITOK);
    969 	if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
    970 	    bpf_validate(fcode, (int)flen)) {
    971 		s = splnet();
    972 		d->bd_filter = fcode;
    973 		reset_d(d);
    974 		splx(s);
    975 		if (old != 0)
    976 			free((caddr_t)old, M_DEVBUF);
    977 
    978 		return (0);
    979 	}
    980 	free((caddr_t)fcode, M_DEVBUF);
    981 	return (EINVAL);
    982 }
    983 
    984 /*
    985  * Detach a file from its current interface (if attached at all) and attach
    986  * to the interface indicated by the name stored in ifr.
    987  * Return an errno or 0.
    988  */
    989 static int
    990 bpf_setif(d, ifr)
    991 	struct bpf_d *d;
    992 	struct ifreq *ifr;
    993 {
    994 	struct bpf_if *bp;
    995 	char *cp;
    996 	int unit_seen, i, s, error;
    997 
    998 	/*
    999 	 * Make sure the provided name has a unit number, and default
   1000 	 * it to '0' if not specified.
   1001 	 * XXX This is ugly ... do this differently?
   1002 	 */
   1003 	unit_seen = 0;
   1004 	cp = ifr->ifr_name;
   1005 	cp[sizeof(ifr->ifr_name) - 1] = '\0';	/* sanity */
   1006 	while (*cp++)
   1007 		if (*cp >= '0' && *cp <= '9')
   1008 			unit_seen = 1;
   1009 	if (!unit_seen) {
   1010 		/* Make sure to leave room for the '\0'. */
   1011 		for (i = 0; i < (IFNAMSIZ - 1); ++i) {
   1012 			if ((ifr->ifr_name[i] >= 'a' &&
   1013 			     ifr->ifr_name[i] <= 'z') ||
   1014 			    (ifr->ifr_name[i] >= 'A' &&
   1015 			     ifr->ifr_name[i] <= 'Z'))
   1016 				continue;
   1017 			ifr->ifr_name[i] = '0';
   1018 		}
   1019 	}
   1020 
   1021 	/*
   1022 	 * Look through attached interfaces for the named one.
   1023 	 */
   1024 	for (bp = bpf_iflist; bp != 0; bp = bp->bif_next) {
   1025 		struct ifnet *ifp = bp->bif_ifp;
   1026 
   1027 		if (ifp == 0 ||
   1028 		    strcmp(ifp->if_xname, ifr->ifr_name) != 0)
   1029 			continue;
   1030 		/* skip additional entry */
   1031 		if (bp->bif_driverp != (struct bpf_if **)&ifp->if_bpf)
   1032 			continue;
   1033 		/*
   1034 		 * We found the requested interface.
   1035 		 * If it's not up, return an error.
   1036 		 * Allocate the packet buffers if we need to.
   1037 		 * If we're already attached to requested interface,
   1038 		 * just flush the buffer.
   1039 		 */
   1040 		if ((ifp->if_flags & IFF_UP) == 0)
   1041 			return (ENETDOWN);
   1042 
   1043 		if (d->bd_sbuf == 0) {
   1044 			error = bpf_allocbufs(d);
   1045 			if (error != 0)
   1046 				return (error);
   1047 		}
   1048 		s = splnet();
   1049 		if (bp != d->bd_bif) {
   1050 			if (d->bd_bif)
   1051 				/*
   1052 				 * Detach if attached to something else.
   1053 				 */
   1054 				bpf_detachd(d);
   1055 
   1056 			bpf_attachd(d, bp);
   1057 		}
   1058 		reset_d(d);
   1059 		splx(s);
   1060 		return (0);
   1061 	}
   1062 	/* Not found. */
   1063 	return (ENXIO);
   1064 }
   1065 
   1066 /*
   1067  * Copy the interface name to the ifreq.
   1068  */
   1069 static void
   1070 bpf_ifname(ifp, ifr)
   1071 	struct ifnet *ifp;
   1072 	struct ifreq *ifr;
   1073 {
   1074 
   1075 	memcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
   1076 }
   1077 
   1078 /*
   1079  * Support for poll() system call
   1080  *
   1081  * Return true iff the specific operation will not block indefinitely - with
   1082  * the assumption that it is safe to positively acknowledge a request for the
   1083  * ability to write to the BPF device.
   1084  * Otherwise, return false but make a note that a selwakeup() must be done.
   1085  */
   1086 int
   1087 bpfpoll(dev, events, p)
   1088 	dev_t dev;
   1089 	int events;
   1090 	struct proc *p;
   1091 {
   1092 	struct bpf_d *d = &bpf_dtab[minor(dev)];
   1093 	int s = splnet();
   1094 	int revents;
   1095 
   1096 	revents = events & (POLLOUT | POLLWRNORM);
   1097 	if (events & (POLLIN | POLLRDNORM)) {
   1098 		/*
   1099 		 * An imitation of the FIONREAD ioctl code.
   1100 		 */
   1101 		if ((d->bd_hlen != 0) ||
   1102 		    (d->bd_immediate && d->bd_slen != 0)) {
   1103 			revents |= events & (POLLIN | POLLRDNORM);
   1104 		} else if (d->bd_state == BPF_TIMED_OUT) {
   1105 			if (d->bd_slen != 0)
   1106 				revents |= events & (POLLIN | POLLRDNORM);
   1107 			else
   1108 				revents |= events & POLLIN;
   1109 		} else {
   1110 			selrecord(p, &d->bd_sel);
   1111 			/* Start the read timeout if necessary */
   1112 			if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
   1113 				callout_reset(&d->bd_callout, d->bd_rtout,
   1114 					      bpf_timed_out, d);
   1115 				d->bd_state = BPF_WAITING;
   1116 			}
   1117 		}
   1118 	}
   1119 
   1120 	splx(s);
   1121 	return (revents);
   1122 }
   1123 
   1124 static void
   1125 filt_bpfrdetach(struct knote *kn)
   1126 {
   1127 	struct bpf_d *d = kn->kn_hook;
   1128 	int s;
   1129 
   1130 	s = splnet();
   1131 	SLIST_REMOVE(&d->bd_sel.sel_klist, kn, knote, kn_selnext);
   1132 	splx(s);
   1133 }
   1134 
   1135 static int
   1136 filt_bpfread(struct knote *kn, long hint)
   1137 {
   1138 	struct bpf_d *d = kn->kn_hook;
   1139 
   1140 	kn->kn_data = d->bd_hlen;
   1141 	if (d->bd_immediate)
   1142 		kn->kn_data += d->bd_slen;
   1143 	return (kn->kn_data > 0);
   1144 }
   1145 
   1146 static const struct filterops bpfread_filtops =
   1147 	{ 1, NULL, filt_bpfrdetach, filt_bpfread };
   1148 
   1149 int
   1150 bpfkqfilter(dev, kn)
   1151 	dev_t dev;
   1152 	struct knote *kn;
   1153 {
   1154 	struct bpf_d *d = &bpf_dtab[minor(dev)];
   1155 	struct klist *klist;
   1156 	int s;
   1157 
   1158 	switch (kn->kn_filter) {
   1159 	case EVFILT_READ:
   1160 		klist = &d->bd_sel.sel_klist;
   1161 		kn->kn_fop = &bpfread_filtops;
   1162 		break;
   1163 
   1164 	default:
   1165 		return (1);
   1166 	}
   1167 
   1168 	kn->kn_hook = d;
   1169 
   1170 	s = splnet();
   1171 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
   1172 	splx(s);
   1173 
   1174 	return (0);
   1175 }
   1176 
   1177 /*
   1178  * Incoming linkage from device drivers.  Process the packet pkt, of length
   1179  * pktlen, which is stored in a contiguous buffer.  The packet is parsed
   1180  * by each process' filter, and if accepted, stashed into the corresponding
   1181  * buffer.
   1182  */
   1183 void
   1184 bpf_tap(arg, pkt, pktlen)
   1185 	caddr_t arg;
   1186 	u_char *pkt;
   1187 	u_int pktlen;
   1188 {
   1189 	struct bpf_if *bp;
   1190 	struct bpf_d *d;
   1191 	u_int slen;
   1192 	/*
   1193 	 * Note that the ipl does not have to be raised at this point.
   1194 	 * The only problem that could arise here is that if two different
   1195 	 * interfaces shared any data.  This is not the case.
   1196 	 */
   1197 	bp = (struct bpf_if *)arg;
   1198 	for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
   1199 		++d->bd_rcount;
   1200 		slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen);
   1201 		if (slen != 0)
   1202 			catchpacket(d, pkt, pktlen, slen, memcpy);
   1203 	}
   1204 }
   1205 
   1206 /*
   1207  * Copy data from an mbuf chain into a buffer.  This code is derived
   1208  * from m_copydata in sys/uipc_mbuf.c.
   1209  */
   1210 static void *
   1211 bpf_mcpy(dst_arg, src_arg, len)
   1212 	void *dst_arg;
   1213 	const void *src_arg;
   1214 	size_t len;
   1215 {
   1216 	const struct mbuf *m;
   1217 	u_int count;
   1218 	u_char *dst;
   1219 
   1220 	m = src_arg;
   1221 	dst = dst_arg;
   1222 	while (len > 0) {
   1223 		if (m == 0)
   1224 			panic("bpf_mcpy");
   1225 		count = min(m->m_len, len);
   1226 		memcpy((caddr_t)dst, mtod(m, caddr_t), count);
   1227 		m = m->m_next;
   1228 		dst += count;
   1229 		len -= count;
   1230 	}
   1231 	return (dst_arg);
   1232 }
   1233 
   1234 /*
   1235  * Dispatch a packet to all the listeners on interface bp.
   1236  *
   1237  * marg    pointer to the packet, either a data buffer or an mbuf chain
   1238  * buflen  buffer length, if marg is a data buffer
   1239  * cpfn    a function that can copy marg into the listener's buffer
   1240  * pktlen  length of the packet
   1241  * rcvif   either NULL or the interface the packet came in on.
   1242  */
   1243 static __inline void
   1244 bpf_deliver(struct bpf_if *bp, void *(*cpfn)(void *, const void *, size_t),
   1245     void *marg, u_int pktlen, u_int buflen, struct ifnet *rcvif)
   1246 {
   1247 	u_int slen;
   1248 	struct bpf_d *d;
   1249 
   1250 	for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
   1251 		if (!d->bd_seesent && (rcvif == NULL))
   1252 			continue;
   1253 		++d->bd_rcount;
   1254 		slen = bpf_filter(d->bd_filter, marg, pktlen, buflen);
   1255 		if (slen != 0)
   1256 			catchpacket(d, marg, pktlen, slen, cpfn);
   1257 	}
   1258 }
   1259 
   1260 static __inline u_int
   1261 bpf_measure(struct mbuf *m)
   1262 {
   1263 	struct mbuf *m0;
   1264 	u_int pktlen;
   1265 
   1266 	pktlen = 0;
   1267 	for (m0 = m; m0 != 0; m0 = m0->m_next)
   1268 		pktlen += m0->m_len;
   1269 	return pktlen;
   1270 }
   1271 
   1272 /*
   1273  * Incoming linkage from device drivers, when the head of the packet is in
   1274  * a buffer, and the tail is in an mbuf chain.
   1275  */
   1276 void
   1277 bpf_mtap2(arg, data, dlen, m)
   1278 	caddr_t arg;
   1279 	void *data;
   1280 	u_int dlen;
   1281 	struct mbuf *m;
   1282 {
   1283 	struct bpf_if *bp = (struct bpf_if *)arg;
   1284 	u_int pktlen;
   1285 	struct mbuf mb;
   1286 
   1287 	pktlen = bpf_measure(m) + dlen;
   1288 
   1289 	/*
   1290 	 * Craft on-stack mbuf suitable for passing to bpf_filter.
   1291 	 * Note that we cut corners here; we only setup what's
   1292 	 * absolutely needed--this mbuf should never go anywhere else.
   1293 	 */
   1294 	mb.m_next = m;
   1295 	mb.m_data = data;
   1296 	mb.m_len = dlen;
   1297 
   1298 	bpf_deliver(bp, bpf_mcpy, &mb, pktlen, 0, m->m_pkthdr.rcvif);
   1299 }
   1300 
   1301 /*
   1302  * Incoming linkage from device drivers, when packet is in an mbuf chain.
   1303  */
   1304 void
   1305 bpf_mtap(arg, m)
   1306 	caddr_t arg;
   1307 	struct mbuf *m;
   1308 {
   1309 	void *(*cpfn) __P((void *, const void *, size_t));
   1310 	struct bpf_if *bp = (struct bpf_if *)arg;
   1311 	u_int pktlen, buflen;
   1312 	void *marg;
   1313 
   1314 	pktlen = bpf_measure(m);
   1315 
   1316 	if (pktlen == m->m_len) {
   1317 		cpfn = memcpy;
   1318 		marg = mtod(m, void *);
   1319 		buflen = pktlen;
   1320 	} else {
   1321 		cpfn = bpf_mcpy;
   1322 		marg = m;
   1323 		buflen = 0;
   1324 	}
   1325 
   1326 	bpf_deliver(bp, cpfn, marg, pktlen, buflen, m->m_pkthdr.rcvif);
   1327 }
   1328 
   1329 /*
   1330  * Move the packet data from interface memory (pkt) into the
   1331  * store buffer.  Return 1 if it's time to wakeup a listener (buffer full),
   1332  * otherwise 0.  "copy" is the routine called to do the actual data
   1333  * transfer.  memcpy is passed in to copy contiguous chunks, while
   1334  * bpf_mcpy is passed in to copy mbuf chains.  In the latter case,
   1335  * pkt is really an mbuf.
   1336  */
   1337 static void
   1338 catchpacket(d, pkt, pktlen, snaplen, cpfn)
   1339 	struct bpf_d *d;
   1340 	u_char *pkt;
   1341 	u_int pktlen, snaplen;
   1342 	void *(*cpfn) __P((void *, const void *, size_t));
   1343 {
   1344 	struct bpf_hdr *hp;
   1345 	int totlen, curlen;
   1346 	int hdrlen = d->bd_bif->bif_hdrlen;
   1347 
   1348 	++d->bd_ccount;
   1349 	/*
   1350 	 * Figure out how many bytes to move.  If the packet is
   1351 	 * greater or equal to the snapshot length, transfer that
   1352 	 * much.  Otherwise, transfer the whole packet (unless
   1353 	 * we hit the buffer size limit).
   1354 	 */
   1355 	totlen = hdrlen + min(snaplen, pktlen);
   1356 	if (totlen > d->bd_bufsize)
   1357 		totlen = d->bd_bufsize;
   1358 
   1359 	/*
   1360 	 * Round up the end of the previous packet to the next longword.
   1361 	 */
   1362 	curlen = BPF_WORDALIGN(d->bd_slen);
   1363 	if (curlen + totlen > d->bd_bufsize) {
   1364 		/*
   1365 		 * This packet will overflow the storage buffer.
   1366 		 * Rotate the buffers if we can, then wakeup any
   1367 		 * pending reads.
   1368 		 */
   1369 		if (d->bd_fbuf == 0) {
   1370 			/*
   1371 			 * We haven't completed the previous read yet,
   1372 			 * so drop the packet.
   1373 			 */
   1374 			++d->bd_dcount;
   1375 			return;
   1376 		}
   1377 		ROTATE_BUFFERS(d);
   1378 		bpf_wakeup(d);
   1379 		curlen = 0;
   1380 	} else if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT)
   1381 		/*
   1382 		 * Immediate mode is set, or the read timeout has
   1383 		 * already expired during a select call.  A packet
   1384 		 * arrived, so the reader should be woken up.
   1385 		 */
   1386 		bpf_wakeup(d);
   1387 
   1388 	/*
   1389 	 * Append the bpf header.
   1390 	 */
   1391 	hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
   1392 	microtime(&hp->bh_tstamp);
   1393 	hp->bh_datalen = pktlen;
   1394 	hp->bh_hdrlen = hdrlen;
   1395 	/*
   1396 	 * Copy the packet data into the store buffer and update its length.
   1397 	 */
   1398 	(*cpfn)((u_char *)hp + hdrlen, pkt, (hp->bh_caplen = totlen - hdrlen));
   1399 	d->bd_slen = curlen + totlen;
   1400 }
   1401 
   1402 /*
   1403  * Initialize all nonzero fields of a descriptor.
   1404  */
   1405 static int
   1406 bpf_allocbufs(d)
   1407 	struct bpf_d *d;
   1408 {
   1409 
   1410 	d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_DEVBUF, M_NOWAIT);
   1411 	if (!d->bd_fbuf)
   1412 		return (ENOBUFS);
   1413 	d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_DEVBUF, M_NOWAIT);
   1414 	if (!d->bd_sbuf) {
   1415 		free(d->bd_fbuf, M_DEVBUF);
   1416 		return (ENOBUFS);
   1417 	}
   1418 	d->bd_slen = 0;
   1419 	d->bd_hlen = 0;
   1420 	return (0);
   1421 }
   1422 
   1423 /*
   1424  * Free buffers currently in use by a descriptor.
   1425  * Called on close.
   1426  */
   1427 static void
   1428 bpf_freed(d)
   1429 	struct bpf_d *d;
   1430 {
   1431 	/*
   1432 	 * We don't need to lock out interrupts since this descriptor has
   1433 	 * been detached from its interface and it yet hasn't been marked
   1434 	 * free.
   1435 	 */
   1436 	if (d->bd_sbuf != 0) {
   1437 		free(d->bd_sbuf, M_DEVBUF);
   1438 		if (d->bd_hbuf != 0)
   1439 			free(d->bd_hbuf, M_DEVBUF);
   1440 		if (d->bd_fbuf != 0)
   1441 			free(d->bd_fbuf, M_DEVBUF);
   1442 	}
   1443 	if (d->bd_filter)
   1444 		free((caddr_t)d->bd_filter, M_DEVBUF);
   1445 
   1446 	D_MARKFREE(d);
   1447 }
   1448 
   1449 /*
   1450  * Attach an interface to bpf.  dlt is the link layer type; hdrlen is the
   1451  * fixed size of the link header (variable length headers not yet supported).
   1452  */
   1453 void
   1454 bpfattach(ifp, dlt, hdrlen)
   1455 	struct ifnet *ifp;
   1456 	u_int dlt, hdrlen;
   1457 {
   1458 
   1459 	bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
   1460 }
   1461 
   1462 /*
   1463  * Attach additional dlt for a interface to bpf.  dlt is the link layer type;
   1464  * hdrlen is the fixed size of the link header for the specified dlt
   1465  * (variable length headers not yet supported).
   1466  */
   1467 void
   1468 bpfattach2(ifp, dlt, hdrlen, driverp)
   1469 	struct ifnet *ifp;
   1470 	u_int dlt, hdrlen;
   1471 	caddr_t *driverp;
   1472 {
   1473 	struct bpf_if *bp;
   1474 	bp = (struct bpf_if *)malloc(sizeof(*bp), M_DEVBUF, M_DONTWAIT);
   1475 	if (bp == 0)
   1476 		panic("bpfattach");
   1477 
   1478 	bp->bif_dlist = 0;
   1479 	bp->bif_driverp = (struct bpf_if **)driverp;
   1480 	bp->bif_ifp = ifp;
   1481 	bp->bif_dlt = dlt;
   1482 
   1483 	bp->bif_next = bpf_iflist;
   1484 	bpf_iflist = bp;
   1485 
   1486 	*bp->bif_driverp = 0;
   1487 
   1488 	/*
   1489 	 * Compute the length of the bpf header.  This is not necessarily
   1490 	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
   1491 	 * that the network layer header begins on a longword boundary (for
   1492 	 * performance reasons and to alleviate alignment restrictions).
   1493 	 */
   1494 	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
   1495 
   1496 #if 0
   1497 	printf("bpf: %s attached\n", ifp->if_xname);
   1498 #endif
   1499 }
   1500 
   1501 /*
   1502  * Remove an interface from bpf.
   1503  */
   1504 void
   1505 bpfdetach(ifp)
   1506 	struct ifnet *ifp;
   1507 {
   1508 	struct bpf_if *bp, **pbp;
   1509 	struct bpf_d *d;
   1510 	int i, s, cmaj;
   1511 
   1512 	/* locate the major number */
   1513 	cmaj = cdevsw_lookup_major(&bpf_cdevsw);
   1514 
   1515 	/* Nuke the vnodes for any open instances */
   1516 	for (i = 0; i < NBPFILTER; ++i) {
   1517 		d = &bpf_dtab[i];
   1518 		if (!D_ISFREE(d) && d->bd_bif != NULL &&
   1519 		    d->bd_bif->bif_ifp == ifp) {
   1520 			/*
   1521 			 * Detach the descriptor from an interface now.
   1522 			 * It will be free'ed later by close routine.
   1523 			 */
   1524 			s = splnet();
   1525 			d->bd_promisc = 0;	/* we can't touch device. */
   1526 			bpf_detachd(d);
   1527 			splx(s);
   1528 			vdevgone(cmaj, i, i, VCHR);
   1529 		}
   1530 	}
   1531 
   1532   again:
   1533 	for (bp = bpf_iflist, pbp = &bpf_iflist;
   1534 	     bp != NULL; pbp = &bp->bif_next, bp = bp->bif_next) {
   1535 		if (bp->bif_ifp == ifp) {
   1536 			*pbp = bp->bif_next;
   1537 			free(bp, M_DEVBUF);
   1538 			goto again;
   1539 		}
   1540 	}
   1541 }
   1542 
   1543 /*
   1544  * Change the data link type of a interface.
   1545  */
   1546 void
   1547 bpf_change_type(ifp, dlt, hdrlen)
   1548 	struct ifnet *ifp;
   1549 	u_int dlt, hdrlen;
   1550 {
   1551 	struct bpf_if *bp;
   1552 
   1553 	for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
   1554 		if (bp->bif_driverp == (struct bpf_if **)&ifp->if_bpf)
   1555 			break;
   1556 	}
   1557 	if (bp == NULL)
   1558 		panic("bpf_change_type");
   1559 
   1560 	bp->bif_dlt = dlt;
   1561 
   1562 	/*
   1563 	 * Compute the length of the bpf header.  This is not necessarily
   1564 	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
   1565 	 * that the network layer header begins on a longword boundary (for
   1566 	 * performance reasons and to alleviate alignment restrictions).
   1567 	 */
   1568 	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
   1569 }
   1570 
   1571 /*
   1572  * Get a list of available data link type of the interface.
   1573  */
   1574 static int
   1575 bpf_getdltlist(d, bfl)
   1576 	struct bpf_d *d;
   1577 	struct bpf_dltlist *bfl;
   1578 {
   1579 	int n, error;
   1580 	struct ifnet *ifp;
   1581 	struct bpf_if *bp;
   1582 
   1583 	ifp = d->bd_bif->bif_ifp;
   1584 	n = 0;
   1585 	error = 0;
   1586 	for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
   1587 		if (bp->bif_ifp != ifp)
   1588 			continue;
   1589 		if (bfl->bfl_list != NULL) {
   1590 			if (n >= bfl->bfl_len)
   1591 				return ENOMEM;
   1592 			error = copyout(&bp->bif_dlt,
   1593 			    bfl->bfl_list + n, sizeof(u_int));
   1594 		}
   1595 		n++;
   1596 	}
   1597 	bfl->bfl_len = n;
   1598 	return error;
   1599 }
   1600 
   1601 /*
   1602  * Set the data link type of a BPF instance.
   1603  */
   1604 static int
   1605 bpf_setdlt(d, dlt)
   1606 	struct bpf_d *d;
   1607 	u_int dlt;
   1608 {
   1609 	int s, error, opromisc;
   1610 	struct ifnet *ifp;
   1611 	struct bpf_if *bp;
   1612 
   1613 	if (d->bd_bif->bif_dlt == dlt)
   1614 		return 0;
   1615 	ifp = d->bd_bif->bif_ifp;
   1616 	for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
   1617 		if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
   1618 			break;
   1619 	}
   1620 	if (bp == NULL)
   1621 		return EINVAL;
   1622 	s = splnet();
   1623 	opromisc = d->bd_promisc;
   1624 	bpf_detachd(d);
   1625 	bpf_attachd(d, bp);
   1626 	reset_d(d);
   1627 	if (opromisc) {
   1628 		error = ifpromisc(bp->bif_ifp, 1);
   1629 		if (error)
   1630 			printf("%s: bpf_setdlt: ifpromisc failed (%d)\n",
   1631 			    bp->bif_ifp->if_xname, error);
   1632 		else
   1633 			d->bd_promisc = 1;
   1634 	}
   1635 	splx(s);
   1636 	return 0;
   1637 }
   1638 
   1639 static int
   1640 sysctl_net_bpf_maxbufsize(SYSCTLFN_ARGS)
   1641 {
   1642 	int newsize, error;
   1643 	struct sysctlnode node;
   1644 
   1645 	node = *rnode;
   1646 	node.sysctl_data = &newsize;
   1647 	newsize = bpf_maxbufsize;
   1648 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1649 	if (error || newp == NULL)
   1650 		return (error);
   1651 
   1652 	if (newsize < BPF_MINBUFSIZE || newsize > BPF_MAXBUFSIZE)
   1653 		return (EINVAL);
   1654 
   1655 	bpf_maxbufsize = newsize;
   1656 
   1657 	return (0);
   1658 }
   1659 
   1660 SYSCTL_SETUP(sysctl_net_bfp_setup, "sysctl net.bpf subtree setup")
   1661 {
   1662 	struct sysctlnode *node;
   1663 
   1664 	sysctl_createv(clog, 0, NULL, NULL,
   1665 		       CTLFLAG_PERMANENT,
   1666 		       CTLTYPE_NODE, "net", NULL,
   1667 		       NULL, 0, NULL, 0,
   1668 		       CTL_NET, CTL_EOL);
   1669 
   1670 	node = NULL;
   1671 	sysctl_createv(clog, 0, NULL, &node,
   1672 		       CTLFLAG_PERMANENT,
   1673 		       CTLTYPE_NODE, "bpf", NULL,
   1674 		       NULL, 0, NULL, 0,
   1675 		       CTL_NET, CTL_CREATE, CTL_EOL);
   1676 	if (node != NULL)
   1677 		sysctl_createv(clog, 0, NULL, NULL,
   1678 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1679 			CTLTYPE_INT, "maxbufsize", NULL,
   1680 			sysctl_net_bpf_maxbufsize, 0, &bpf_maxbufsize, 0,
   1681 			CTL_NET, node->sysctl_num, CTL_CREATE, CTL_EOL);
   1682 }
   1683