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