Home | History | Annotate | Line # | Download | only in net
bpf.c revision 1.41
      1 /*	$NetBSD: bpf.c,v 1.41 1998/08/06 04:24:25 perry 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((const void *, 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 (*)(const void *, 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 #if BSD >= 199103
    569 	selwakeup(&d->bd_sel);
    570 	/* XXX */
    571 	d->bd_sel.si_pid = 0;
    572 #else
    573 	if (d->bd_selproc) {
    574 		selwakeup(d->bd_selproc, (int)d->bd_selcoll);
    575 		d->bd_selcoll = 0;
    576 		d->bd_selproc = 0;
    577 	}
    578 #endif
    579 }
    580 
    581 int
    582 bpfwrite(dev, uio, ioflag)
    583 	dev_t dev;
    584 	struct uio *uio;
    585 	int ioflag;
    586 {
    587 	register struct bpf_d *d = &bpf_dtab[minor(dev)];
    588 	struct ifnet *ifp;
    589 	struct mbuf *m;
    590 	int error, s;
    591 	static struct sockaddr dst;
    592 
    593 	if (d->bd_bif == 0)
    594 		return (ENXIO);
    595 
    596 	ifp = d->bd_bif->bif_ifp;
    597 
    598 	if (uio->uio_resid == 0)
    599 		return (0);
    600 
    601 	error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, ifp->if_mtu, &m, &dst);
    602 	if (error)
    603 		return (error);
    604 
    605 	if (m->m_pkthdr.len > ifp->if_mtu)
    606 		return (EMSGSIZE);
    607 
    608 	if (d->bd_hdrcmplt)
    609 		dst.sa_family = pseudo_AF_HDRCMPLT;
    610 
    611 	s = splsoftnet();
    612 #if BSD >= 199103
    613 	error = (*ifp->if_output)(ifp, m, &dst, (struct rtentry *)0);
    614 #else
    615 	error = (*ifp->if_output)(ifp, m, &dst);
    616 #endif
    617 	splx(s);
    618 	/*
    619 	 * The driver frees the mbuf.
    620 	 */
    621 	return (error);
    622 }
    623 
    624 /*
    625  * Reset a descriptor by flushing its packet buffer and clearing the
    626  * receive and drop counts.  Should be called at splimp.
    627  */
    628 static void
    629 reset_d(d)
    630 	struct bpf_d *d;
    631 {
    632 	if (d->bd_hbuf) {
    633 		/* Free the hold buffer. */
    634 		d->bd_fbuf = d->bd_hbuf;
    635 		d->bd_hbuf = 0;
    636 	}
    637 	d->bd_slen = 0;
    638 	d->bd_hlen = 0;
    639 	d->bd_rcount = 0;
    640 	d->bd_dcount = 0;
    641 }
    642 
    643 #ifdef BPF_KERN_FILTER
    644 extern struct bpf_insn *bpf_tcp_filter;
    645 extern struct bpf_insn *bpf_udp_filter;
    646 #endif
    647 
    648 /*
    649  *  FIONREAD		Check for read packet available.
    650  *  BIOCGBLEN		Get buffer len [for read()].
    651  *  BIOCSETF		Set ethernet read filter.
    652  *  BIOCFLUSH		Flush read packet buffer.
    653  *  BIOCPROMISC		Put interface into promiscuous mode.
    654  *  BIOCGDLT		Get link layer type.
    655  *  BIOCGETIF		Get interface name.
    656  *  BIOCSETIF		Set interface.
    657  *  BIOCSRTIMEOUT	Set read timeout.
    658  *  BIOCGRTIMEOUT	Get read timeout.
    659  *  BIOCGSTATS		Get packet stats.
    660  *  BIOCIMMEDIATE	Set immediate mode.
    661  *  BIOCVERSION		Get filter language version.
    662  *  BIOGHDRCMPLT	Get "header already complete" flag.
    663  *  BIOSHDRCMPLT	Set "header already complete" flag.
    664  */
    665 /* ARGSUSED */
    666 int
    667 bpfioctl(dev, cmd, addr, flag, p)
    668 	dev_t dev;
    669 	u_long cmd;
    670 	caddr_t addr;
    671 	int flag;
    672 	struct proc *p;
    673 {
    674 	register struct bpf_d *d = &bpf_dtab[minor(dev)];
    675 	int s, error = 0;
    676 #ifdef BPF_KERN_FILTER
    677 	register struct bpf_insn **p;
    678 #endif
    679 
    680 	switch (cmd) {
    681 
    682 	default:
    683 		error = EINVAL;
    684 		break;
    685 
    686 	/*
    687 	 * Check for read packet available.
    688 	 */
    689 	case FIONREAD:
    690 		{
    691 			int n;
    692 
    693 			s = splimp();
    694 			n = d->bd_slen;
    695 			if (d->bd_hbuf)
    696 				n += d->bd_hlen;
    697 			splx(s);
    698 
    699 			*(int *)addr = n;
    700 			break;
    701 		}
    702 
    703 	/*
    704 	 * Get buffer len [for read()].
    705 	 */
    706 	case BIOCGBLEN:
    707 		*(u_int *)addr = d->bd_bufsize;
    708 		break;
    709 
    710 	/*
    711 	 * Set buffer length.
    712 	 */
    713 	case BIOCSBLEN:
    714 #if BSD < 199103
    715 		error = EINVAL;
    716 #else
    717 		if (d->bd_bif != 0)
    718 			error = EINVAL;
    719 		else {
    720 			register u_int size = *(u_int *)addr;
    721 
    722 			if (size > BPF_MAXBUFSIZE)
    723 				*(u_int *)addr = size = BPF_MAXBUFSIZE;
    724 			else if (size < BPF_MINBUFSIZE)
    725 				*(u_int *)addr = size = BPF_MINBUFSIZE;
    726 			d->bd_bufsize = size;
    727 		}
    728 #endif
    729 		break;
    730 
    731 	/*
    732 	 * Set link layer read filter.
    733 	 */
    734 	case BIOCSETF:
    735 		error = bpf_setf(d, (struct bpf_program *)addr);
    736 		break;
    737 
    738 #ifdef BPF_KERN_FILTER
    739 	/*
    740 	 * Set TCP or UDP reject filter.
    741 	 */
    742 	case BIOCSTCPF:
    743 	case BIOCSUDPF:
    744 		if (!suser()) {
    745 			error = EPERM;
    746 			break;
    747 		}
    748 
    749 		/* Validate and store filter */
    750 		error = bpf_setf(d, (struct bpf_program *)addr);
    751 
    752 		/* Free possible old filter */
    753 		if (cmd == BIOCSTCPF)
    754 			p = &bpf_tcp_filter;
    755 		else
    756 			p = &bpf_udp_filter;
    757 		if (*p != NULL)
    758 			free((caddr_t)*p, M_DEVBUF);
    759 
    760 		/* Steal new filter (noop if error) */
    761 		s = splimp();
    762 		*p = d->bd_filter;
    763 		d->bd_filter = NULL;
    764 		splx(s);
    765 		break;
    766 #endif
    767 
    768 	/*
    769 	 * Flush read packet buffer.
    770 	 */
    771 	case BIOCFLUSH:
    772 		s = splimp();
    773 		reset_d(d);
    774 		splx(s);
    775 		break;
    776 
    777 	/*
    778 	 * Put interface into promiscuous mode.
    779 	 */
    780 	case BIOCPROMISC:
    781 		if (d->bd_bif == 0) {
    782 			/*
    783 			 * No interface attached yet.
    784 			 */
    785 			error = EINVAL;
    786 			break;
    787 		}
    788 		s = splimp();
    789 		if (d->bd_promisc == 0) {
    790 			error = ifpromisc(d->bd_bif->bif_ifp, 1);
    791 			if (error == 0)
    792 				d->bd_promisc = 1;
    793 		}
    794 		splx(s);
    795 		break;
    796 
    797 	/*
    798 	 * Get device parameters.
    799 	 */
    800 	case BIOCGDLT:
    801 		if (d->bd_bif == 0)
    802 			error = EINVAL;
    803 		else
    804 			*(u_int *)addr = d->bd_bif->bif_dlt;
    805 		break;
    806 
    807 	/*
    808 	 * Set interface name.
    809 	 */
    810 	case BIOCGETIF:
    811 		if (d->bd_bif == 0)
    812 			error = EINVAL;
    813 		else
    814 			bpf_ifname(d->bd_bif->bif_ifp, (struct ifreq *)addr);
    815 		break;
    816 
    817 	/*
    818 	 * Set interface.
    819 	 */
    820 	case BIOCSETIF:
    821 		error = bpf_setif(d, (struct ifreq *)addr);
    822 		break;
    823 
    824 	/*
    825 	 * Set read timeout.
    826 	 */
    827 	case BIOCSRTIMEOUT:
    828 		{
    829 			struct timeval *tv = (struct timeval *)addr;
    830 
    831 			/* Compute number of ticks. */
    832 			d->bd_rtout = tv->tv_sec * hz + tv->tv_usec / tick;
    833 			if ((d->bd_rtout == 0) && (tv->tv_usec != 0))
    834 				d->bd_rtout = 1;
    835 			break;
    836 		}
    837 
    838 	/*
    839 	 * Get read timeout.
    840 	 */
    841 	case BIOCGRTIMEOUT:
    842 		{
    843 			struct timeval *tv = (struct timeval *)addr;
    844 
    845 			tv->tv_sec = d->bd_rtout / hz;
    846 			tv->tv_usec = (d->bd_rtout % hz) * tick;
    847 			break;
    848 		}
    849 
    850 	/*
    851 	 * Get packet stats.
    852 	 */
    853 	case BIOCGSTATS:
    854 		{
    855 			struct bpf_stat *bs = (struct bpf_stat *)addr;
    856 
    857 			bs->bs_recv = d->bd_rcount;
    858 			bs->bs_drop = d->bd_dcount;
    859 			break;
    860 		}
    861 
    862 	/*
    863 	 * Set immediate mode.
    864 	 */
    865 	case BIOCIMMEDIATE:
    866 		d->bd_immediate = *(u_int *)addr;
    867 		break;
    868 
    869 	case BIOCVERSION:
    870 		{
    871 			struct bpf_version *bv = (struct bpf_version *)addr;
    872 
    873 			bv->bv_major = BPF_MAJOR_VERSION;
    874 			bv->bv_minor = BPF_MINOR_VERSION;
    875 			break;
    876 		}
    877 
    878 	case BIOCGHDRCMPLT:	/* get "header already complete" flag */
    879 		*(u_int *)addr = d->bd_hdrcmplt;
    880 		break;
    881 
    882 	case BIOCSHDRCMPLT:	/* set "header already complete" flag */
    883 		d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
    884 		break;
    885 
    886 	case FIONBIO:		/* Non-blocking I/O */
    887 		if (*(int *)addr)
    888 			d->bd_rtout = -1;
    889 		else
    890 			d->bd_rtout = 0;
    891 		break;
    892 
    893 	case FIOASYNC:		/* Send signal on receive packets */
    894 		d->bd_async = *(int *)addr;
    895 		break;
    896 
    897 	/*
    898 	 * N.B.  ioctl (FIOSETOWN) and fcntl (F_SETOWN) both end up doing
    899 	 * the equivalent of a TIOCSPGRP and hence end up here.  *However*
    900 	 * TIOCSPGRP's arg is a process group if it's positive and a process
    901 	 * id if it's negative.  This is exactly the opposite of what the
    902 	 * other two functions want!  Therefore there is code in ioctl and
    903 	 * fcntl to negate the arg before calling here.
    904 	 */
    905 	case TIOCSPGRP:		/* Process or group to send signals to */
    906 		d->bd_pgid = *(int *)addr;
    907 		break;
    908 
    909 	case TIOCGPGRP:
    910 		*(int *)addr = d->bd_pgid;
    911 		break;
    912 	}
    913 	return (error);
    914 }
    915 
    916 /*
    917  * Set d's packet filter program to fp.  If this file already has a filter,
    918  * free it and replace it.  Returns EINVAL for bogus requests.
    919  */
    920 int
    921 bpf_setf(d, fp)
    922 	struct bpf_d *d;
    923 	struct bpf_program *fp;
    924 {
    925 	struct bpf_insn *fcode, *old;
    926 	u_int flen, size;
    927 	int s;
    928 
    929 	old = d->bd_filter;
    930 	if (fp->bf_insns == 0) {
    931 		if (fp->bf_len != 0)
    932 			return (EINVAL);
    933 		s = splimp();
    934 		d->bd_filter = 0;
    935 		reset_d(d);
    936 		splx(s);
    937 		if (old != 0)
    938 			free((caddr_t)old, M_DEVBUF);
    939 		return (0);
    940 	}
    941 	flen = fp->bf_len;
    942 	if (flen > BPF_MAXINSNS)
    943 		return (EINVAL);
    944 
    945 	size = flen * sizeof(*fp->bf_insns);
    946 	fcode = (struct bpf_insn *)malloc(size, M_DEVBUF, M_WAITOK);
    947 	if (fcode == 0)
    948 		return (ENOMEM);
    949 	if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size) == 0 &&
    950 	    bpf_validate(fcode, (int)flen)) {
    951 		s = splimp();
    952 		d->bd_filter = fcode;
    953 		reset_d(d);
    954 		splx(s);
    955 		if (old != 0)
    956 			free((caddr_t)old, M_DEVBUF);
    957 
    958 		return (0);
    959 	}
    960 	free((caddr_t)fcode, M_DEVBUF);
    961 	return (EINVAL);
    962 }
    963 
    964 /*
    965  * Detach a file from its current interface (if attached at all) and attach
    966  * to the interface indicated by the name stored in ifr.
    967  * Return an errno or 0.
    968  */
    969 static int
    970 bpf_setif(d, ifr)
    971 	struct bpf_d *d;
    972 	struct ifreq *ifr;
    973 {
    974 	struct bpf_if *bp;
    975 	char *cp;
    976 	int unit_seen, i, s, error;
    977 
    978 	/*
    979 	 * Make sure the provided name has a unit number, and default
    980 	 * it to '0' if not specified.
    981 	 * XXX This is ugly ... do this differently?
    982 	 */
    983 	unit_seen = 0;
    984 	cp = ifr->ifr_name;
    985 	cp[sizeof(ifr->ifr_name) - 1] = '\0';	/* sanity */
    986 	while (*cp++)
    987 		if (*cp >= '0' && *cp <= '9')
    988 			unit_seen = 1;
    989 	if (!unit_seen) {
    990 		/* Make sure to leave room for the '\0'. */
    991 		for (i = 0; i < (IFNAMSIZ - 1); ++i) {
    992 			if ((ifr->ifr_name[i] >= 'a' &&
    993 			     ifr->ifr_name[i] <= 'z') ||
    994 			    (ifr->ifr_name[i] >= 'A' &&
    995 			     ifr->ifr_name[i] <= 'Z'))
    996 				continue;
    997 			ifr->ifr_name[i] = '0';
    998 		}
    999 	}
   1000 
   1001 	/*
   1002 	 * Look through attached interfaces for the named one.
   1003 	 */
   1004 	for (bp = bpf_iflist; bp != 0; bp = bp->bif_next) {
   1005 		struct ifnet *ifp = bp->bif_ifp;
   1006 
   1007 		if (ifp == 0 ||
   1008 		    strcmp(ifp->if_xname, ifr->ifr_name) != 0)
   1009 			continue;
   1010 		/*
   1011 		 * We found the requested interface.
   1012 		 * If it's not up, return an error.
   1013 		 * Allocate the packet buffers if we need to.
   1014 		 * If we're already attached to requested interface,
   1015 		 * just flush the buffer.
   1016 		 */
   1017 		if ((ifp->if_flags & IFF_UP) == 0)
   1018 			return (ENETDOWN);
   1019 
   1020 		if (d->bd_sbuf == 0) {
   1021 			error = bpf_allocbufs(d);
   1022 			if (error != 0)
   1023 				return (error);
   1024 		}
   1025 		s = splimp();
   1026 		if (bp != d->bd_bif) {
   1027 			if (d->bd_bif)
   1028 				/*
   1029 				 * Detach if attached to something else.
   1030 				 */
   1031 				bpf_detachd(d);
   1032 
   1033 			bpf_attachd(d, bp);
   1034 		}
   1035 		reset_d(d);
   1036 		splx(s);
   1037 		return (0);
   1038 	}
   1039 	/* Not found. */
   1040 	return (ENXIO);
   1041 }
   1042 
   1043 /*
   1044  * Copy the interface name to the ifreq.
   1045  */
   1046 static void
   1047 bpf_ifname(ifp, ifr)
   1048 	struct ifnet *ifp;
   1049 	struct ifreq *ifr;
   1050 {
   1051 
   1052 	memcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
   1053 }
   1054 
   1055 /*
   1056  * Support for poll() system call
   1057  *
   1058  * Return true iff the specific operation will not block indefinitely.
   1059  * Otherwise, return false but make a note that a selwakeup() must be done.
   1060  */
   1061 int
   1062 bpfpoll(dev, events, p)
   1063 	register dev_t dev;
   1064 	int events;
   1065 	struct proc *p;
   1066 {
   1067 	register struct bpf_d *d = &bpf_dtab[minor(dev)];
   1068 	int revents = 0;
   1069 	register int s = splimp();
   1070 
   1071 	/*
   1072 	 * An imitation of the FIONREAD ioctl code.
   1073 	 */
   1074 	if (events & (POLLIN | POLLRDNORM))
   1075 		if (d->bd_hlen != 0 || (d->bd_immediate && d->bd_slen != 0))
   1076 			revents |= events & (POLLIN | POLLRDNORM);
   1077 		else
   1078 			selrecord(p, &d->bd_sel);
   1079 
   1080 	splx(s);
   1081 	return (revents);
   1082 }
   1083 
   1084 /*
   1085  * Incoming linkage from device drivers.  Process the packet pkt, of length
   1086  * pktlen, which is stored in a contiguous buffer.  The packet is parsed
   1087  * by each process' filter, and if accepted, stashed into the corresponding
   1088  * buffer.
   1089  */
   1090 void
   1091 bpf_tap(arg, pkt, pktlen)
   1092 	caddr_t arg;
   1093 	register u_char *pkt;
   1094 	register u_int pktlen;
   1095 {
   1096 	struct bpf_if *bp;
   1097 	register struct bpf_d *d;
   1098 	register u_int slen;
   1099 	/*
   1100 	 * Note that the ipl does not have to be raised at this point.
   1101 	 * The only problem that could arise here is that if two different
   1102 	 * interfaces shared any data.  This is not the case.
   1103 	 */
   1104 	bp = (struct bpf_if *)arg;
   1105 	for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
   1106 		++d->bd_rcount;
   1107 		slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen);
   1108 		if (slen != 0)
   1109 			catchpacket(d, pkt, pktlen, slen, memcpy);
   1110 	}
   1111 }
   1112 
   1113 /*
   1114  * Copy data from an mbuf chain into a buffer.  This code is derived
   1115  * from m_copydata in sys/uipc_mbuf.c.
   1116  */
   1117 static void
   1118 bpf_mcpy(dst_arg, src_arg, len)
   1119 	void *dst_arg;
   1120 	const void *src_arg;
   1121 	register size_t len;
   1122 {
   1123 	register const struct mbuf *m;
   1124 	register u_int count;
   1125 	u_char *dst;
   1126 
   1127 	m = src_arg;
   1128 	dst = dst_arg;
   1129 	while (len > 0) {
   1130 		if (m == 0)
   1131 			panic("bpf_mcpy");
   1132 		count = min(m->m_len, len);
   1133 		memcpy((caddr_t)dst, mtod(m, caddr_t), count);
   1134 		m = m->m_next;
   1135 		dst += count;
   1136 		len -= count;
   1137 	}
   1138 }
   1139 
   1140 /*
   1141  * Incoming linkage from device drivers, when packet is in an mbuf chain.
   1142  */
   1143 void
   1144 bpf_mtap(arg, m)
   1145 	caddr_t arg;
   1146 	struct mbuf *m;
   1147 {
   1148 	struct bpf_if *bp = (struct bpf_if *)arg;
   1149 	struct bpf_d *d;
   1150 	u_int pktlen, slen;
   1151 	struct mbuf *m0;
   1152 
   1153 	pktlen = 0;
   1154 	for (m0 = m; m0 != 0; m0 = m0->m_next)
   1155 		pktlen += m0->m_len;
   1156 
   1157 	for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
   1158 		++d->bd_rcount;
   1159 		slen = bpf_filter(d->bd_filter, (u_char *)m, pktlen, 0);
   1160 		if (slen != 0)
   1161 			catchpacket(d, (u_char *)m, pktlen, slen, bpf_mcpy);
   1162 	}
   1163 }
   1164 
   1165 /*
   1166  * Move the packet data from interface memory (pkt) into the
   1167  * store buffer.  Return 1 if it's time to wakeup a listener (buffer full),
   1168  * otherwise 0.  "copy" is the routine called to do the actual data
   1169  * transfer.  memcpy is passed in to copy contiguous chunks, while
   1170  * bpf_mcpy is passed in to copy mbuf chains.  In the latter case,
   1171  * pkt is really an mbuf.
   1172  */
   1173 static void
   1174 catchpacket(d, pkt, pktlen, snaplen, cpfn)
   1175 	register struct bpf_d *d;
   1176 	register u_char *pkt;
   1177 	register u_int pktlen, snaplen;
   1178 	register void (*cpfn) __P((const void *, void *, size_t));
   1179 {
   1180 	register struct bpf_hdr *hp;
   1181 	register int totlen, curlen;
   1182 	register int hdrlen = d->bd_bif->bif_hdrlen;
   1183 	/*
   1184 	 * Figure out how many bytes to move.  If the packet is
   1185 	 * greater or equal to the snapshot length, transfer that
   1186 	 * much.  Otherwise, transfer the whole packet (unless
   1187 	 * we hit the buffer size limit).
   1188 	 */
   1189 	totlen = hdrlen + min(snaplen, pktlen);
   1190 	if (totlen > d->bd_bufsize)
   1191 		totlen = d->bd_bufsize;
   1192 
   1193 	/*
   1194 	 * Round up the end of the previous packet to the next longword.
   1195 	 */
   1196 	curlen = BPF_WORDALIGN(d->bd_slen);
   1197 	if (curlen + totlen > d->bd_bufsize) {
   1198 		/*
   1199 		 * This packet will overflow the storage buffer.
   1200 		 * Rotate the buffers if we can, then wakeup any
   1201 		 * pending reads.
   1202 		 */
   1203 		if (d->bd_fbuf == 0) {
   1204 			/*
   1205 			 * We haven't completed the previous read yet,
   1206 			 * so drop the packet.
   1207 			 */
   1208 			++d->bd_dcount;
   1209 			return;
   1210 		}
   1211 		ROTATE_BUFFERS(d);
   1212 		bpf_wakeup(d);
   1213 		curlen = 0;
   1214 	}
   1215 	else if (d->bd_immediate)
   1216 		/*
   1217 		 * Immediate mode is set.  A packet arrived so any
   1218 		 * reads should be woken up.
   1219 		 */
   1220 		bpf_wakeup(d);
   1221 
   1222 	/*
   1223 	 * Append the bpf header.
   1224 	 */
   1225 	hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
   1226 #if BSD >= 199103
   1227 	microtime(&hp->bh_tstamp);
   1228 #elif defined(sun)
   1229 	uniqtime(&hp->bh_tstamp);
   1230 #else
   1231 	hp->bh_tstamp = time;
   1232 #endif
   1233 	hp->bh_datalen = pktlen;
   1234 	hp->bh_hdrlen = hdrlen;
   1235 	/*
   1236 	 * Copy the packet data into the store buffer and update its length.
   1237 	 */
   1238 	(*cpfn)((u_char *)hp + hdrlen, pkt, (hp->bh_caplen = totlen - hdrlen));
   1239 	d->bd_slen = curlen + totlen;
   1240 }
   1241 
   1242 /*
   1243  * Initialize all nonzero fields of a descriptor.
   1244  */
   1245 static int
   1246 bpf_allocbufs(d)
   1247 	register struct bpf_d *d;
   1248 {
   1249 	d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_DEVBUF, M_WAITOK);
   1250 	if (d->bd_fbuf == 0)
   1251 		return (ENOBUFS);
   1252 
   1253 	d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_DEVBUF, M_WAITOK);
   1254 	if (d->bd_sbuf == 0) {
   1255 		free(d->bd_fbuf, M_DEVBUF);
   1256 		return (ENOBUFS);
   1257 	}
   1258 	d->bd_slen = 0;
   1259 	d->bd_hlen = 0;
   1260 	return (0);
   1261 }
   1262 
   1263 /*
   1264  * Free buffers currently in use by a descriptor.
   1265  * Called on close.
   1266  */
   1267 static void
   1268 bpf_freed(d)
   1269 	register struct bpf_d *d;
   1270 {
   1271 	/*
   1272 	 * We don't need to lock out interrupts since this descriptor has
   1273 	 * been detached from its interface and it yet hasn't been marked
   1274 	 * free.
   1275 	 */
   1276 	if (d->bd_sbuf != 0) {
   1277 		free(d->bd_sbuf, M_DEVBUF);
   1278 		if (d->bd_hbuf != 0)
   1279 			free(d->bd_hbuf, M_DEVBUF);
   1280 		if (d->bd_fbuf != 0)
   1281 			free(d->bd_fbuf, M_DEVBUF);
   1282 	}
   1283 	if (d->bd_filter)
   1284 		free((caddr_t)d->bd_filter, M_DEVBUF);
   1285 
   1286 	D_MARKFREE(d);
   1287 }
   1288 
   1289 /*
   1290  * Attach an interface to bpf.  driverp is a pointer to a (struct bpf_if *)
   1291  * in the driver's softc; dlt is the link layer type; hdrlen is the fixed
   1292  * size of the link header (variable length headers not yet supported).
   1293  */
   1294 void
   1295 bpfattach(driverp, ifp, dlt, hdrlen)
   1296 	caddr_t *driverp;
   1297 	struct ifnet *ifp;
   1298 	u_int dlt, hdrlen;
   1299 {
   1300 	struct bpf_if *bp;
   1301 	int i;
   1302 #if BSD < 199103
   1303 	static struct bpf_if bpf_ifs[NBPFILTER];
   1304 	static int bpfifno;
   1305 
   1306 	bp = (bpfifno < NBPFILTER) ? &bpf_ifs[bpfifno++] : 0;
   1307 #else
   1308 	bp = (struct bpf_if *)malloc(sizeof(*bp), M_DEVBUF, M_DONTWAIT);
   1309 #endif
   1310 	if (bp == 0)
   1311 		panic("bpfattach");
   1312 
   1313 	bp->bif_dlist = 0;
   1314 	bp->bif_driverp = (struct bpf_if **)driverp;
   1315 	bp->bif_ifp = ifp;
   1316 	bp->bif_dlt = dlt;
   1317 
   1318 	bp->bif_next = bpf_iflist;
   1319 	bpf_iflist = bp;
   1320 
   1321 	*bp->bif_driverp = 0;
   1322 
   1323 	/*
   1324 	 * Compute the length of the bpf header.  This is not necessarily
   1325 	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
   1326 	 * that the network layer header begins on a longword boundary (for
   1327 	 * performance reasons and to alleviate alignment restrictions).
   1328 	 */
   1329 	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
   1330 
   1331 	/*
   1332 	 * Mark all the descriptors free if this hasn't been done.
   1333 	 */
   1334 	if (!D_ISFREE(&bpf_dtab[0]))
   1335 		for (i = 0; i < NBPFILTER; ++i)
   1336 			D_MARKFREE(&bpf_dtab[i]);
   1337 
   1338 #if 0
   1339 	printf("bpf: %s attached\n", ifp->if_xname);
   1340 #endif
   1341 }
   1342 
   1343 #if BSD >= 199103
   1344 /* XXX This routine belongs in net/if.c. */
   1345 /*
   1346  * Set/clear promiscuous mode on interface ifp based on the truth value
   1347  * of pswitch.  The calls are reference counted so that only the first
   1348  * "on" request actually has an effect, as does the final "off" request.
   1349  * Results are undefined if the "off" and "on" requests are not matched.
   1350  */
   1351 int
   1352 ifpromisc(ifp, pswitch)
   1353 	register struct ifnet *ifp;
   1354 	register int pswitch;
   1355 {
   1356 	register int pcount, ret;
   1357 	register short flags;
   1358 	struct ifreq ifr;
   1359 
   1360 	pcount = ifp->if_pcount;
   1361 	flags = ifp->if_flags;
   1362 	if (pswitch) {
   1363 		/*
   1364 		 * If the device is not configured up, we cannot put it in
   1365 		 * promiscuous mode.
   1366 		 */
   1367 		if ((ifp->if_flags & IFF_UP) == 0)
   1368 			return (ENETDOWN);
   1369 		if (ifp->if_pcount++ != 0)
   1370 			return (0);
   1371 		ifp->if_flags |= IFF_PROMISC;
   1372 	} else {
   1373 		if (--ifp->if_pcount > 0)
   1374 			return (0);
   1375 		ifp->if_flags &= ~IFF_PROMISC;
   1376 		/*
   1377 		 * If the device is not configured up, we should not need to
   1378 		 * turn off promiscuous mode (device should have turned it
   1379 		 * off when interface went down; and will look at IFF_PROMISC
   1380 		 * again next time interface comes up).
   1381 		 */
   1382 		if ((ifp->if_flags & IFF_UP) == 0)
   1383 			return (0);
   1384 	}
   1385 	memset((caddr_t)&ifr, 0, sizeof(ifr));
   1386 	ifr.ifr_flags = ifp->if_flags;
   1387 	ret = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
   1388 	/* Restore interface state if not successful */
   1389 	if (ret != 0) {
   1390 		ifp->if_pcount = pcount;
   1391 		ifp->if_flags = flags;
   1392 	}
   1393 	return (ret);
   1394 }
   1395 #endif
   1396 
   1397 #if BSD < 199103
   1398 /*
   1399  * Allocate some memory for bpf.  This is temporary SunOS support, and
   1400  * is admittedly a hack.
   1401  * If resources unavailable, return 0.
   1402  */
   1403 static caddr_t
   1404 bpf_alloc(size, canwait)
   1405 	register int size;
   1406 	register int canwait;
   1407 {
   1408 	register struct mbuf *m;
   1409 
   1410 	if ((unsigned)size > (MCLBYTES-8))
   1411 		return 0;
   1412 
   1413 	MGET(m, canwait, MT_DATA);
   1414 	if (m == 0)
   1415 		return 0;
   1416 	if ((unsigned)size > (MLEN-8)) {
   1417 		MCLGET(m);
   1418 		if (m->m_len != MCLBYTES) {
   1419 			m_freem(m);
   1420 			return 0;
   1421 		}
   1422 	}
   1423 	*mtod(m, struct mbuf **) = m;
   1424 	return mtod(m, caddr_t) + 8;
   1425 }
   1426 #endif
   1427