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