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