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