Home | History | Annotate | Line # | Download | only in net
bpf.c revision 1.44
      1 /*	$NetBSD: bpf.c,v 1.44 1998/08/18 06:32:13 thorpej 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 
     58 #include <sys/file.h>
     59 #if defined(sparc) && BSD < 199103
     60 #include <sys/stream.h>
     61 #endif
     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 /*
     83  * Older BSDs don't have kernel malloc.
     84  */
     85 #if BSD < 199103
     86 extern bcopy();
     87 static caddr_t bpf_alloc();
     88 #include <net/bpf_compat.h>
     89 #define BPF_BUFSIZE (MCLBYTES-8)
     90 #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, code, uio)
     91 #else
     92 #define BPF_BUFSIZE 4096
     93 #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, uio)
     94 #endif
     95 
     96 #define PRINET  26			/* interruptible */
     97 
     98 /*
     99  * The default read buffer size is patchable.
    100  */
    101 int bpf_bufsize = BPF_BUFSIZE;
    102 
    103 /*
    104  *  bpf_iflist is the list of interfaces; each corresponds to an ifnet
    105  *  bpf_dtab holds the descriptors, indexed by minor device #
    106  */
    107 struct bpf_if	*bpf_iflist;
    108 struct bpf_d	bpf_dtab[NBPFILTER];
    109 
    110 #if BSD >= 199207 || NetBSD0_9 >= 2
    111 /*
    112  * bpfilterattach() is called at boot time in new systems.  We do
    113  * nothing here since old systems will not call this.
    114  */
    115 /* ARGSUSED */
    116 void
    117 bpfilterattach(n)
    118 	int n;
    119 {
    120 }
    121 #endif
    122 
    123 static int	bpf_allocbufs __P((struct bpf_d *));
    124 static int	bpf_allocbufs __P((struct bpf_d *));
    125 static void	bpf_freed __P((struct bpf_d *));
    126 static void	bpf_freed __P((struct bpf_d *));
    127 static void	bpf_ifname __P((struct ifnet *, struct ifreq *));
    128 static void	bpf_ifname __P((struct ifnet *, struct ifreq *));
    129 static void	*bpf_mcpy __P((void *, const void *, size_t));
    130 static int	bpf_movein __P((struct uio *, int, int,
    131 			        struct mbuf **, struct sockaddr *));
    132 static void	bpf_attachd __P((struct bpf_d *, struct bpf_if *));
    133 static void	bpf_detachd __P((struct bpf_d *));
    134 static int	bpf_setif __P((struct bpf_d *, struct ifreq *));
    135 int		bpfpoll __P((dev_t, int, struct proc *));
    136 static __inline void
    137 		bpf_wakeup __P((struct bpf_d *));
    138 static void	catchpacket __P((struct bpf_d *, u_char *, u_int, u_int,
    139 				 void *(*)(void *, const void *, size_t)));
    140 static void	reset_d __P((struct bpf_d *));
    141 
    142 static int
    143 bpf_movein(uio, linktype, mtu, mp, sockp)
    144 	register struct uio *uio;
    145 	int linktype;
    146 	int mtu;
    147 	register struct mbuf **mp;
    148 	register struct sockaddr *sockp;
    149 {
    150 	struct mbuf *m;
    151 	int error;
    152 	int len;
    153 	int hlen;
    154 	int align;
    155 
    156 	/*
    157 	 * Build a sockaddr based on the data link layer type.
    158 	 * We do this at this level because the ethernet header
    159 	 * is copied directly into the data field of the sockaddr.
    160 	 * In the case of SLIP, there is no header and the packet
    161 	 * is forwarded as is.
    162 	 * Also, we are careful to leave room at the front of the mbuf
    163 	 * for the link level header.
    164 	 */
    165 	switch (linktype) {
    166 
    167 	case DLT_SLIP:
    168 		sockp->sa_family = AF_INET;
    169 		hlen = 0;
    170 		align = 0;
    171 		break;
    172 
    173 	case DLT_PPP:
    174 		sockp->sa_family = AF_UNSPEC;
    175 		hlen = 0;
    176 		align = 0;
    177 		break;
    178 
    179 	case DLT_EN10MB:
    180 		sockp->sa_family = AF_UNSPEC;
    181 		/* XXX Would MAXLINKHDR be better? */
    182  		/* 6(dst)+6(src)+2(type) */
    183 		hlen = sizeof(struct ether_header);
    184 		align = 2;
    185 		break;
    186 
    187 	case DLT_ARCNET:
    188 		sockp->sa_family = AF_UNSPEC;
    189 		hlen = ARC_HDRLEN;
    190 		align = 5;
    191 		break;
    192 
    193 	case DLT_FDDI:
    194 		sockp->sa_family = AF_UNSPEC;
    195 		/* XXX 4(FORMAC)+6(dst)+6(src)+3(LLC)+5(SNAP) */
    196 		hlen = 24;
    197 		align = 0;
    198 		break;
    199 
    200 	case DLT_NULL:
    201 		sockp->sa_family = AF_UNSPEC;
    202 		hlen = 0;
    203 		align = 0;
    204 		break;
    205 
    206 	default:
    207 		return (EIO);
    208 	}
    209 
    210 	len = uio->uio_resid;
    211 	/*
    212 	 * If there aren't enough bytes for a link level header or the
    213 	 * packet length exceeds the interface mtu, return an error.
    214 	 */
    215 	if (len < hlen || len - hlen > mtu)
    216 		return (EMSGSIZE);
    217 
    218 	/*
    219 	 * XXX Avoid complicated buffer chaining ---
    220 	 * bail if it won't fit in a single mbuf.
    221 	 * (Take into account possible alignment bytes)
    222 	 */
    223 	if ((unsigned)len > MCLBYTES - align)
    224 		return (EIO);
    225 
    226 	MGETHDR(m, M_WAIT, MT_DATA);
    227 	if (m == 0)
    228 		return (ENOBUFS);
    229 	m->m_pkthdr.rcvif = 0;
    230 	m->m_pkthdr.len = len - hlen;
    231 	if (len > MHLEN - align) {
    232 #if BSD >= 199103
    233 		MCLGET(m, M_WAIT);
    234 		if ((m->m_flags & M_EXT) == 0) {
    235 #else
    236 		MCLGET(m);
    237 		if (m->m_len != MCLBYTES) {
    238 #endif
    239 			error = ENOBUFS;
    240 			goto bad;
    241 		}
    242 	}
    243 
    244 	/* Insure the data is properly aligned */
    245 	if (align > 0) {
    246 #if BSD >= 199103
    247 		m->m_data += align;
    248 #else
    249 		m->m_off += align;
    250 #endif
    251 		m->m_len -= align;
    252 	}
    253 
    254 	error = UIOMOVE(mtod(m, caddr_t), len, UIO_WRITE, uio);
    255 	if (error)
    256 		goto bad;
    257 	if (hlen != 0) {
    258 		memcpy(sockp->sa_data, mtod(m, caddr_t), hlen);
    259 #if BSD >= 199103
    260 		m->m_data += hlen; /* XXX */
    261 #else
    262 		m->m_off += hlen;
    263 #endif
    264 		len -= hlen;
    265 	}
    266 	m->m_len = len;
    267 	*mp = m;
    268 	return (0);
    269 
    270 bad:
    271 	m_freem(m);
    272 	return (error);
    273 }
    274 
    275 /*
    276  * Attach file to the bpf interface, i.e. make d listen on bp.
    277  * Must be called at splimp.
    278  */
    279 static void
    280 bpf_attachd(d, bp)
    281 	struct bpf_d *d;
    282 	struct bpf_if *bp;
    283 {
    284 	/*
    285 	 * Point d at bp, and add d to the interface's list of listeners.
    286 	 * Finally, point the driver's bpf cookie at the interface so
    287 	 * it will divert packets to bpf.
    288 	 */
    289 	d->bd_bif = bp;
    290 	d->bd_next = bp->bif_dlist;
    291 	bp->bif_dlist = d;
    292 
    293 	*bp->bif_driverp = bp;
    294 }
    295 
    296 /*
    297  * Detach a file from its interface.
    298  */
    299 static void
    300 bpf_detachd(d)
    301 	struct bpf_d *d;
    302 {
    303 	struct bpf_d **p;
    304 	struct bpf_if *bp;
    305 
    306 	bp = d->bd_bif;
    307 	/*
    308 	 * Check if this descriptor had requested promiscuous mode.
    309 	 * If so, turn it off.
    310 	 */
    311 	if (d->bd_promisc) {
    312 		int error;
    313 
    314 		d->bd_promisc = 0;
    315 		/*
    316 		 * Take device out of promiscuous mode.  Since we were
    317 		 * able to enter promiscuous mode, we should be able
    318 		 * to turn it off.  But we can get an error if
    319 		 * the interface was configured down, so only panic
    320 		 * if we don't get an unexpected error.
    321 		 */
    322   		error = ifpromisc(bp->bif_ifp, 0);
    323 		if (error && error != EINVAL)
    324 			panic("bpf: ifpromisc failed");
    325 	}
    326 	/* Remove d from the interface's descriptor list. */
    327 	p = &bp->bif_dlist;
    328 	while (*p != d) {
    329 		p = &(*p)->bd_next;
    330 		if (*p == 0)
    331 			panic("bpf_detachd: descriptor not in list");
    332 	}
    333 	*p = (*p)->bd_next;
    334 	if (bp->bif_dlist == 0)
    335 		/*
    336 		 * Let the driver know that there are no more listeners.
    337 		 */
    338 		*d->bd_bif->bif_driverp = 0;
    339 	d->bd_bif = 0;
    340 }
    341 
    342 
    343 /*
    344  * Mark a descriptor free by making it point to itself.
    345  * This is probably cheaper than marking with a constant since
    346  * the address should be in a register anyway.
    347  */
    348 #define D_ISFREE(d) ((d) == (d)->bd_next)
    349 #define D_MARKFREE(d) ((d)->bd_next = (d))
    350 #define D_MARKUSED(d) ((d)->bd_next = 0)
    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 	register 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 	register struct bpf_d *d = &bpf_dtab[minor(dev)];
    396 	register int s;
    397 
    398 	s = splimp();
    399 	if (d->bd_bif)
    400 		bpf_detachd(d);
    401 	splx(s);
    402 	bpf_freed(d);
    403 
    404 	return (0);
    405 }
    406 
    407 /*
    408  * Support for SunOS, which does not have tsleep.
    409  */
    410 #if BSD < 199103
    411 static
    412 bpf_timeout(arg)
    413 	caddr_t arg;
    414 {
    415 	struct bpf_d *d = (struct bpf_d *)arg;
    416 	d->bd_timedout = 1;
    417 	wakeup(arg);
    418 }
    419 
    420 #define BPF_SLEEP(chan, pri, s, t) bpf_sleep((struct bpf_d *)chan)
    421 
    422 int
    423 bpf_sleep(d)
    424 	register struct bpf_d *d;
    425 {
    426 	register int rto = d->bd_rtout;
    427 	register int st;
    428 
    429 	if (rto != 0) {
    430 		d->bd_timedout = 0;
    431 		timeout(bpf_timeout, (caddr_t)d, rto);
    432 	}
    433 	st = sleep((caddr_t)d, PRINET|PCATCH);
    434 	if (rto != 0) {
    435 		if (d->bd_timedout == 0)
    436 			untimeout(bpf_timeout, (caddr_t)d);
    437 		else if (st == 0)
    438 			return EWOULDBLOCK;
    439 	}
    440 	return (st != 0) ? EINTR : 0;
    441 }
    442 #else
    443 #define BPF_SLEEP tsleep
    444 #endif
    445 
    446 /*
    447  * Rotate the packet buffers in descriptor d.  Move the store buffer
    448  * into the hold slot, and the free buffer into the store slot.
    449  * Zero the length of the new store buffer.
    450  */
    451 #define ROTATE_BUFFERS(d) \
    452 	(d)->bd_hbuf = (d)->bd_sbuf; \
    453 	(d)->bd_hlen = (d)->bd_slen; \
    454 	(d)->bd_sbuf = (d)->bd_fbuf; \
    455 	(d)->bd_slen = 0; \
    456 	(d)->bd_fbuf = 0;
    457 /*
    458  *  bpfread - read next chunk of packets from buffers
    459  */
    460 int
    461 bpfread(dev, uio, ioflag)
    462 	dev_t dev;
    463 	register struct uio *uio;
    464 	int ioflag;
    465 {
    466 	register struct bpf_d *d = &bpf_dtab[minor(dev)];
    467 	int error;
    468 	int s;
    469 
    470 	/*
    471 	 * Restrict application to use a buffer the same size as
    472 	 * as kernel buffers.
    473 	 */
    474 	if (uio->uio_resid != d->bd_bufsize)
    475 		return (EINVAL);
    476 
    477 	s = splimp();
    478 	/*
    479 	 * If the hold buffer is empty, then do a timed sleep, which
    480 	 * ends when the timeout expires or when enough packets
    481 	 * have arrived to fill the store buffer.
    482 	 */
    483 	while (d->bd_hbuf == 0) {
    484 		if (d->bd_immediate) {
    485 			if (d->bd_slen == 0) {
    486 				splx(s);
    487 				return (EWOULDBLOCK);
    488 			}
    489 			/*
    490 			 * A packet(s) either arrived since the previous
    491 			 * read or arrived while we were asleep.
    492 			 * Rotate the buffers and return what's here.
    493 			 */
    494 			ROTATE_BUFFERS(d);
    495 			break;
    496 		}
    497 		if (d->bd_rtout != -1)
    498 			error = BPF_SLEEP((caddr_t)d, PRINET|PCATCH, "bpf",
    499 					  d->bd_rtout);
    500 		else
    501 			error = EWOULDBLOCK; /* User requested non-blocking I/O */
    502 		if (error == EINTR || error == ERESTART) {
    503 			splx(s);
    504 			return (error);
    505 		}
    506 		if (error == EWOULDBLOCK) {
    507 			/*
    508 			 * On a timeout, return what's in the buffer,
    509 			 * which may be nothing.  If there is something
    510 			 * in the store buffer, we can rotate the buffers.
    511 			 */
    512 			if (d->bd_hbuf)
    513 				/*
    514 				 * We filled up the buffer in between
    515 				 * getting the timeout and arriving
    516 				 * here, so we don't need to rotate.
    517 				 */
    518 				break;
    519 
    520 			if (d->bd_slen == 0) {
    521 				splx(s);
    522 				return (0);
    523 			}
    524 			ROTATE_BUFFERS(d);
    525 			break;
    526 		}
    527 		if (error != 0)
    528 			goto done;
    529 	}
    530 	/*
    531 	 * At this point, we know we have something in the hold slot.
    532 	 */
    533 	splx(s);
    534 
    535 	/*
    536 	 * Move data from hold buffer into user space.
    537 	 * We know the entire buffer is transferred since
    538 	 * we checked above that the read buffer is bpf_bufsize bytes.
    539 	 */
    540 	error = UIOMOVE(d->bd_hbuf, d->bd_hlen, UIO_READ, uio);
    541 
    542 	s = splimp();
    543 	d->bd_fbuf = d->bd_hbuf;
    544 	d->bd_hbuf = 0;
    545 	d->bd_hlen = 0;
    546 done:
    547 	splx(s);
    548 	return (error);
    549 }
    550 
    551 
    552 /*
    553  * If there are processes sleeping on this descriptor, wake them up.
    554  */
    555 static __inline void
    556 bpf_wakeup(d)
    557 	register struct bpf_d *d;
    558 {
    559 	struct proc *p;
    560 
    561 	wakeup((caddr_t)d);
    562 	if (d->bd_async) {
    563 		if (d->bd_pgid > 0)
    564 			gsignal (d->bd_pgid, SIGIO);
    565 		else if ((p = pfind (-d->bd_pgid)) != NULL)
    566 			psignal (p, SIGIO);
    567 	}
    568 
    569 #if BSD >= 199103
    570 	selwakeup(&d->bd_sel);
    571 	/* XXX */
    572 	d->bd_sel.si_pid = 0;
    573 #else
    574 	if (d->bd_selproc) {
    575 		selwakeup(d->bd_selproc, (int)d->bd_selcoll);
    576 		d->bd_selcoll = 0;
    577 		d->bd_selproc = 0;
    578 	}
    579 #endif
    580 }
    581 
    582 int
    583 bpfwrite(dev, uio, ioflag)
    584 	dev_t dev;
    585 	struct uio *uio;
    586 	int ioflag;
    587 {
    588 	register struct bpf_d *d = &bpf_dtab[minor(dev)];
    589 	struct ifnet *ifp;
    590 	struct mbuf *m;
    591 	int error, s;
    592 	static struct sockaddr dst;
    593 
    594 	if (d->bd_bif == 0)
    595 		return (ENXIO);
    596 
    597 	ifp = d->bd_bif->bif_ifp;
    598 
    599 	if (uio->uio_resid == 0)
    600 		return (0);
    601 
    602 	error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, ifp->if_mtu, &m, &dst);
    603 	if (error)
    604 		return (error);
    605 
    606 	if (m->m_pkthdr.len > ifp->if_mtu)
    607 		return (EMSGSIZE);
    608 
    609 	if (d->bd_hdrcmplt)
    610 		dst.sa_family = pseudo_AF_HDRCMPLT;
    611 
    612 	s = splsoftnet();
    613 #if BSD >= 199103
    614 	error = (*ifp->if_output)(ifp, m, &dst, (struct rtentry *)0);
    615 #else
    616 	error = (*ifp->if_output)(ifp, m, &dst);
    617 #endif
    618 	splx(s);
    619 	/*
    620 	 * The driver frees the mbuf.
    621 	 */
    622 	return (error);
    623 }
    624 
    625 /*
    626  * Reset a descriptor by flushing its packet buffer and clearing the
    627  * receive and drop counts.  Should be called at splimp.
    628  */
    629 static void
    630 reset_d(d)
    631 	struct bpf_d *d;
    632 {
    633 	if (d->bd_hbuf) {
    634 		/* Free the hold buffer. */
    635 		d->bd_fbuf = d->bd_hbuf;
    636 		d->bd_hbuf = 0;
    637 	}
    638 	d->bd_slen = 0;
    639 	d->bd_hlen = 0;
    640 	d->bd_rcount = 0;
    641 	d->bd_dcount = 0;
    642 }
    643 
    644 #ifdef BPF_KERN_FILTER
    645 extern struct bpf_insn *bpf_tcp_filter;
    646 extern struct bpf_insn *bpf_udp_filter;
    647 #endif
    648 
    649 /*
    650  *  FIONREAD		Check for read packet available.
    651  *  BIOCGBLEN		Get buffer len [for read()].
    652  *  BIOCSETF		Set ethernet read filter.
    653  *  BIOCFLUSH		Flush read packet buffer.
    654  *  BIOCPROMISC		Put interface into promiscuous mode.
    655  *  BIOCGDLT		Get link layer type.
    656  *  BIOCGETIF		Get interface name.
    657  *  BIOCSETIF		Set interface.
    658  *  BIOCSRTIMEOUT	Set read timeout.
    659  *  BIOCGRTIMEOUT	Get read timeout.
    660  *  BIOCGSTATS		Get packet stats.
    661  *  BIOCIMMEDIATE	Set immediate mode.
    662  *  BIOCVERSION		Get filter language version.
    663  *  BIOGHDRCMPLT	Get "header already complete" flag.
    664  *  BIOSHDRCMPLT	Set "header already complete" flag.
    665  */
    666 /* ARGSUSED */
    667 int
    668 bpfioctl(dev, cmd, addr, flag, p)
    669 	dev_t dev;
    670 	u_long cmd;
    671 	caddr_t addr;
    672 	int flag;
    673 	struct proc *p;
    674 {
    675 	register struct bpf_d *d = &bpf_dtab[minor(dev)];
    676 	int s, error = 0;
    677 #ifdef BPF_KERN_FILTER
    678 	register struct bpf_insn **p;
    679 #endif
    680 
    681 	switch (cmd) {
    682 
    683 	default:
    684 		error = EINVAL;
    685 		break;
    686 
    687 	/*
    688 	 * Check for read packet available.
    689 	 */
    690 	case FIONREAD:
    691 		{
    692 			int n;
    693 
    694 			s = splimp();
    695 			n = d->bd_slen;
    696 			if (d->bd_hbuf)
    697 				n += d->bd_hlen;
    698 			splx(s);
    699 
    700 			*(int *)addr = n;
    701 			break;
    702 		}
    703 
    704 	/*
    705 	 * Get buffer len [for read()].
    706 	 */
    707 	case BIOCGBLEN:
    708 		*(u_int *)addr = d->bd_bufsize;
    709 		break;
    710 
    711 	/*
    712 	 * Set buffer length.
    713 	 */
    714 	case BIOCSBLEN:
    715 #if BSD < 199103
    716 		error = EINVAL;
    717 #else
    718 		if (d->bd_bif != 0)
    719 			error = EINVAL;
    720 		else {
    721 			register u_int size = *(u_int *)addr;
    722 
    723 			if (size > BPF_MAXBUFSIZE)
    724 				*(u_int *)addr = size = BPF_MAXBUFSIZE;
    725 			else if (size < BPF_MINBUFSIZE)
    726 				*(u_int *)addr = size = BPF_MINBUFSIZE;
    727 			d->bd_bufsize = size;
    728 		}
    729 #endif
    730 		break;
    731 
    732 	/*
    733 	 * Set link layer read filter.
    734 	 */
    735 	case BIOCSETF:
    736 		error = bpf_setf(d, (struct bpf_program *)addr);
    737 		break;
    738 
    739 #ifdef BPF_KERN_FILTER
    740 	/*
    741 	 * Set TCP or UDP reject filter.
    742 	 */
    743 	case BIOCSTCPF:
    744 	case BIOCSUDPF:
    745 		if (!suser()) {
    746 			error = EPERM;
    747 			break;
    748 		}
    749 
    750 		/* Validate and store filter */
    751 		error = bpf_setf(d, (struct bpf_program *)addr);
    752 
    753 		/* Free possible old filter */
    754 		if (cmd == BIOCSTCPF)
    755 			p = &bpf_tcp_filter;
    756 		else
    757 			p = &bpf_udp_filter;
    758 		if (*p != NULL)
    759 			free((caddr_t)*p, M_DEVBUF);
    760 
    761 		/* Steal new filter (noop if error) */
    762 		s = splimp();
    763 		*p = d->bd_filter;
    764 		d->bd_filter = NULL;
    765 		splx(s);
    766 		break;
    767 #endif
    768 
    769 	/*
    770 	 * Flush read packet buffer.
    771 	 */
    772 	case BIOCFLUSH:
    773 		s = splimp();
    774 		reset_d(d);
    775 		splx(s);
    776 		break;
    777 
    778 	/*
    779 	 * Put interface into promiscuous mode.
    780 	 */
    781 	case BIOCPROMISC:
    782 		if (d->bd_bif == 0) {
    783 			/*
    784 			 * No interface attached yet.
    785 			 */
    786 			error = EINVAL;
    787 			break;
    788 		}
    789 		s = splimp();
    790 		if (d->bd_promisc == 0) {
    791 			error = ifpromisc(d->bd_bif->bif_ifp, 1);
    792 			if (error == 0)
    793 				d->bd_promisc = 1;
    794 		}
    795 		splx(s);
    796 		break;
    797 
    798 	/*
    799 	 * Get device parameters.
    800 	 */
    801 	case BIOCGDLT:
    802 		if (d->bd_bif == 0)
    803 			error = EINVAL;
    804 		else
    805 			*(u_int *)addr = d->bd_bif->bif_dlt;
    806 		break;
    807 
    808 	/*
    809 	 * Set interface name.
    810 	 */
    811 	case BIOCGETIF:
    812 		if (d->bd_bif == 0)
    813 			error = EINVAL;
    814 		else
    815 			bpf_ifname(d->bd_bif->bif_ifp, (struct ifreq *)addr);
    816 		break;
    817 
    818 	/*
    819 	 * Set interface.
    820 	 */
    821 	case BIOCSETIF:
    822 		error = bpf_setif(d, (struct ifreq *)addr);
    823 		break;
    824 
    825 	/*
    826 	 * Set read timeout.
    827 	 */
    828 	case BIOCSRTIMEOUT:
    829 		{
    830 			struct timeval *tv = (struct timeval *)addr;
    831 
    832 			/* Compute number of ticks. */
    833 			d->bd_rtout = tv->tv_sec * hz + tv->tv_usec / tick;
    834 			if ((d->bd_rtout == 0) && (tv->tv_usec != 0))
    835 				d->bd_rtout = 1;
    836 			break;
    837 		}
    838 
    839 	/*
    840 	 * Get read timeout.
    841 	 */
    842 	case BIOCGRTIMEOUT:
    843 		{
    844 			struct timeval *tv = (struct timeval *)addr;
    845 
    846 			tv->tv_sec = d->bd_rtout / hz;
    847 			tv->tv_usec = (d->bd_rtout % hz) * tick;
    848 			break;
    849 		}
    850 
    851 	/*
    852 	 * Get packet stats.
    853 	 */
    854 	case BIOCGSTATS:
    855 		{
    856 			struct bpf_stat *bs = (struct bpf_stat *)addr;
    857 
    858 			bs->bs_recv = d->bd_rcount;
    859 			bs->bs_drop = d->bd_dcount;
    860 			break;
    861 		}
    862 
    863 	/*
    864 	 * Set immediate mode.
    865 	 */
    866 	case BIOCIMMEDIATE:
    867 		d->bd_immediate = *(u_int *)addr;
    868 		break;
    869 
    870 	case BIOCVERSION:
    871 		{
    872 			struct bpf_version *bv = (struct bpf_version *)addr;
    873 
    874 			bv->bv_major = BPF_MAJOR_VERSION;
    875 			bv->bv_minor = BPF_MINOR_VERSION;
    876 			break;
    877 		}
    878 
    879 	case BIOCGHDRCMPLT:	/* get "header already complete" flag */
    880 		*(u_int *)addr = d->bd_hdrcmplt;
    881 		break;
    882 
    883 	case BIOCSHDRCMPLT:	/* set "header already complete" flag */
    884 		d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
    885 		break;
    886 
    887 	case FIONBIO:		/* Non-blocking I/O */
    888 		if (*(int *)addr)
    889 			d->bd_rtout = -1;
    890 		else
    891 			d->bd_rtout = 0;
    892 		break;
    893 
    894 	case FIOASYNC:		/* Send signal on receive packets */
    895 		d->bd_async = *(int *)addr;
    896 		break;
    897 
    898 	/*
    899 	 * N.B.  ioctl (FIOSETOWN) and fcntl (F_SETOWN) both end up doing
    900 	 * the equivalent of a TIOCSPGRP and hence end up here.  *However*
    901 	 * TIOCSPGRP's arg is a process group if it's positive and a process
    902 	 * id if it's negative.  This is exactly the opposite of what the
    903 	 * other two functions want!  Therefore there is code in ioctl and
    904 	 * fcntl to negate the arg before calling here.
    905 	 */
    906 	case TIOCSPGRP:		/* Process or group to send signals to */
    907 		d->bd_pgid = *(int *)addr;
    908 		break;
    909 
    910 	case TIOCGPGRP:
    911 		*(int *)addr = d->bd_pgid;
    912 		break;
    913 	}
    914 	return (error);
    915 }
    916 
    917 /*
    918  * Set d's packet filter program to fp.  If this file already has a filter,
    919  * free it and replace it.  Returns EINVAL for bogus requests.
    920  */
    921 int
    922 bpf_setf(d, fp)
    923 	struct bpf_d *d;
    924 	struct bpf_program *fp;
    925 {
    926 	struct bpf_insn *fcode, *old;
    927 	u_int flen, size;
    928 	int s;
    929 
    930 	old = d->bd_filter;
    931 	if (fp->bf_insns == 0) {
    932 		if (fp->bf_len != 0)
    933 			return (EINVAL);
    934 		s = splimp();
    935 		d->bd_filter = 0;
    936 		reset_d(d);
    937 		splx(s);
    938 		if (old != 0)
    939 			free((caddr_t)old, M_DEVBUF);
    940 		return (0);
    941 	}
    942 	flen = fp->bf_len;
    943 	if (flen > BPF_MAXINSNS)
    944 		return (EINVAL);
    945 
    946 	size = flen * sizeof(*fp->bf_insns);
    947 	fcode = (struct bpf_insn *)malloc(size, M_DEVBUF, M_WAITOK);
    948 	if (fcode == 0)
    949 		return (ENOMEM);
    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 	register dev_t dev;
   1065 	int events;
   1066 	struct proc *p;
   1067 {
   1068 	register struct bpf_d *d = &bpf_dtab[minor(dev)];
   1069 	int revents = 0;
   1070 	register 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 	register u_char *pkt;
   1096 	register u_int pktlen;
   1097 {
   1098 	struct bpf_if *bp;
   1099 	register struct bpf_d *d;
   1100 	register 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 	register size_t len;
   1124 {
   1125 	register const struct mbuf *m;
   1126 	register 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 	register struct bpf_d *d;
   1179 	register u_char *pkt;
   1180 	register u_int pktlen, snaplen;
   1181 	register void *(*cpfn) __P((void *, const void *, size_t));
   1182 {
   1183 	register struct bpf_hdr *hp;
   1184 	register int totlen, curlen;
   1185 	register 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 	register struct bpf_d *d;
   1251 {
   1252 	d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_DEVBUF, M_WAITOK);
   1253 	if (d->bd_fbuf == 0)
   1254 		return (ENOBUFS);
   1255 
   1256 	d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_DEVBUF, M_WAITOK);
   1257 	if (d->bd_sbuf == 0) {
   1258 		free(d->bd_fbuf, M_DEVBUF);
   1259 		return (ENOBUFS);
   1260 	}
   1261 	d->bd_slen = 0;
   1262 	d->bd_hlen = 0;
   1263 	return (0);
   1264 }
   1265 
   1266 /*
   1267  * Free buffers currently in use by a descriptor.
   1268  * Called on close.
   1269  */
   1270 static void
   1271 bpf_freed(d)
   1272 	register struct bpf_d *d;
   1273 {
   1274 	/*
   1275 	 * We don't need to lock out interrupts since this descriptor has
   1276 	 * been detached from its interface and it yet hasn't been marked
   1277 	 * free.
   1278 	 */
   1279 	if (d->bd_sbuf != 0) {
   1280 		free(d->bd_sbuf, M_DEVBUF);
   1281 		if (d->bd_hbuf != 0)
   1282 			free(d->bd_hbuf, M_DEVBUF);
   1283 		if (d->bd_fbuf != 0)
   1284 			free(d->bd_fbuf, M_DEVBUF);
   1285 	}
   1286 	if (d->bd_filter)
   1287 		free((caddr_t)d->bd_filter, M_DEVBUF);
   1288 
   1289 	D_MARKFREE(d);
   1290 }
   1291 
   1292 /*
   1293  * Attach an interface to bpf.  driverp is a pointer to a (struct bpf_if *)
   1294  * in the driver's softc; dlt is the link layer type; hdrlen is the fixed
   1295  * size of the link header (variable length headers not yet supported).
   1296  */
   1297 void
   1298 bpfattach(driverp, ifp, dlt, hdrlen)
   1299 	caddr_t *driverp;
   1300 	struct ifnet *ifp;
   1301 	u_int dlt, hdrlen;
   1302 {
   1303 	struct bpf_if *bp;
   1304 	int i;
   1305 #if BSD < 199103
   1306 	static struct bpf_if bpf_ifs[NBPFILTER];
   1307 	static int bpfifno;
   1308 
   1309 	bp = (bpfifno < NBPFILTER) ? &bpf_ifs[bpfifno++] : 0;
   1310 #else
   1311 	bp = (struct bpf_if *)malloc(sizeof(*bp), M_DEVBUF, M_DONTWAIT);
   1312 #endif
   1313 	if (bp == 0)
   1314 		panic("bpfattach");
   1315 
   1316 	bp->bif_dlist = 0;
   1317 	bp->bif_driverp = (struct bpf_if **)driverp;
   1318 	bp->bif_ifp = ifp;
   1319 	bp->bif_dlt = dlt;
   1320 
   1321 	bp->bif_next = bpf_iflist;
   1322 	bpf_iflist = bp;
   1323 
   1324 	*bp->bif_driverp = 0;
   1325 
   1326 	/*
   1327 	 * Compute the length of the bpf header.  This is not necessarily
   1328 	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
   1329 	 * that the network layer header begins on a longword boundary (for
   1330 	 * performance reasons and to alleviate alignment restrictions).
   1331 	 */
   1332 	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
   1333 
   1334 	/*
   1335 	 * Mark all the descriptors free if this hasn't been done.
   1336 	 */
   1337 	if (!D_ISFREE(&bpf_dtab[0]))
   1338 		for (i = 0; i < NBPFILTER; ++i)
   1339 			D_MARKFREE(&bpf_dtab[i]);
   1340 
   1341 #if 0
   1342 	printf("bpf: %s attached\n", ifp->if_xname);
   1343 #endif
   1344 }
   1345 
   1346 #if BSD >= 199103
   1347 /* XXX This routine belongs in net/if.c. */
   1348 /*
   1349  * Set/clear promiscuous mode on interface ifp based on the truth value
   1350  * of pswitch.  The calls are reference counted so that only the first
   1351  * "on" request actually has an effect, as does the final "off" request.
   1352  * Results are undefined if the "off" and "on" requests are not matched.
   1353  */
   1354 int
   1355 ifpromisc(ifp, pswitch)
   1356 	register struct ifnet *ifp;
   1357 	register int pswitch;
   1358 {
   1359 	register int pcount, ret;
   1360 	register short flags;
   1361 	struct ifreq ifr;
   1362 
   1363 	pcount = ifp->if_pcount;
   1364 	flags = ifp->if_flags;
   1365 	if (pswitch) {
   1366 		/*
   1367 		 * If the device is not configured up, we cannot put it in
   1368 		 * promiscuous mode.
   1369 		 */
   1370 		if ((ifp->if_flags & IFF_UP) == 0)
   1371 			return (ENETDOWN);
   1372 		if (ifp->if_pcount++ != 0)
   1373 			return (0);
   1374 		ifp->if_flags |= IFF_PROMISC;
   1375 	} else {
   1376 		if (--ifp->if_pcount > 0)
   1377 			return (0);
   1378 		ifp->if_flags &= ~IFF_PROMISC;
   1379 		/*
   1380 		 * If the device is not configured up, we should not need to
   1381 		 * turn off promiscuous mode (device should have turned it
   1382 		 * off when interface went down; and will look at IFF_PROMISC
   1383 		 * again next time interface comes up).
   1384 		 */
   1385 		if ((ifp->if_flags & IFF_UP) == 0)
   1386 			return (0);
   1387 	}
   1388 	memset((caddr_t)&ifr, 0, sizeof(ifr));
   1389 	ifr.ifr_flags = ifp->if_flags;
   1390 	ret = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
   1391 	/* Restore interface state if not successful */
   1392 	if (ret != 0) {
   1393 		ifp->if_pcount = pcount;
   1394 		ifp->if_flags = flags;
   1395 	}
   1396 	return (ret);
   1397 }
   1398 #endif
   1399 
   1400 #if BSD < 199103
   1401 /*
   1402  * Allocate some memory for bpf.  This is temporary SunOS support, and
   1403  * is admittedly a hack.
   1404  * If resources unavailable, return 0.
   1405  */
   1406 static caddr_t
   1407 bpf_alloc(size, canwait)
   1408 	register int size;
   1409 	register int canwait;
   1410 {
   1411 	register struct mbuf *m;
   1412 
   1413 	if ((unsigned)size > (MCLBYTES-8))
   1414 		return 0;
   1415 
   1416 	MGET(m, canwait, MT_DATA);
   1417 	if (m == 0)
   1418 		return 0;
   1419 	if ((unsigned)size > (MLEN-8)) {
   1420 		MCLGET(m);
   1421 		if (m->m_len != MCLBYTES) {
   1422 			m_freem(m);
   1423 			return 0;
   1424 		}
   1425 	}
   1426 	*mtod(m, struct mbuf **) = m;
   1427 	return mtod(m, caddr_t) + 8;
   1428 }
   1429 #endif
   1430