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