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