Home | History | Annotate | Line # | Download | only in net
bpf.c revision 1.25
      1 /*	$NetBSD: bpf.c,v 1.25 1996/03/30 21:57:30 christos 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, s, error;
    902 
    903 	/*
    904 	 * Separate string into name part and unit number.  Put a null
    905 	 * byte at the end of the name part, and compute the number.
    906 	 * If the a unit number is unspecified, the default is 0,
    907 	 * as initialized above.  XXX This should be common code.
    908 	 */
    909 	unit = 0;
    910 	cp = ifr->ifr_name;
    911 	cp[sizeof(ifr->ifr_name) - 1] = '\0';
    912 	while (*cp++) {
    913 		if (*cp >= '0' && *cp <= '9') {
    914 			unit = *cp - '0';
    915 			*cp++ = '\0';
    916 			while (*cp)
    917 				unit = 10 * unit + *cp++ - '0';
    918 			break;
    919 		}
    920 	}
    921 	/*
    922 	 * Look through attached interfaces for the named one.
    923 	 */
    924 	for (bp = bpf_iflist; bp != 0; bp = bp->bif_next) {
    925 		struct ifnet *ifp = bp->bif_ifp;
    926 
    927 		if (ifp == 0 || unit != ifp->if_unit
    928 		    || strcmp(ifp->if_name, ifr->ifr_name) != 0)
    929 			continue;
    930 		/*
    931 		 * We found the requested interface.
    932 		 * If it's not up, return an error.
    933 		 * Allocate the packet buffers if we need to.
    934 		 * If we're already attached to requested interface,
    935 		 * just flush the buffer.
    936 		 */
    937 		if ((ifp->if_flags & IFF_UP) == 0)
    938 			return (ENETDOWN);
    939 
    940 		if (d->bd_sbuf == 0) {
    941 			error = bpf_allocbufs(d);
    942 			if (error != 0)
    943 				return (error);
    944 		}
    945 		s = splimp();
    946 		if (bp != d->bd_bif) {
    947 			if (d->bd_bif)
    948 				/*
    949 				 * Detach if attached to something else.
    950 				 */
    951 				bpf_detachd(d);
    952 
    953 			bpf_attachd(d, bp);
    954 		}
    955 		reset_d(d);
    956 		splx(s);
    957 		return (0);
    958 	}
    959 	/* Not found. */
    960 	return (ENXIO);
    961 }
    962 
    963 /*
    964  * Convert an interface name plus unit number of an ifp to a single
    965  * name which is returned in the ifr.
    966  */
    967 static void
    968 bpf_ifname(ifp, ifr)
    969 	struct ifnet *ifp;
    970 	struct ifreq *ifr;
    971 {
    972 	char *s = ifp->if_name;
    973 	char *d = ifr->ifr_name;
    974 
    975 	while ((*d++ = *s++) != '\0')
    976 		continue;
    977 	sprintf(d, "%d", ifp->if_unit);
    978 }
    979 
    980 /*
    981  * The new select interface passes down the proc pointer; the old select
    982  * stubs had to grab it out of the user struct.  This glue allows either case.
    983  */
    984 #if BSD >= 199103
    985 #define bpf_select bpfselect
    986 #else
    987 int
    988 bpfselect(dev, rw)
    989 	register dev_t dev;
    990 	int rw;
    991 {
    992 	return (bpf_select(dev, rw, u.u_procp));
    993 }
    994 #endif
    995 
    996 /*
    997  * Support for select() system call
    998  *
    999  * Return true iff the specific operation will not block indefinitely.
   1000  * Otherwise, return false but make a note that a selwakeup() must be done.
   1001  */
   1002 int
   1003 bpf_select(dev, rw, p)
   1004 	register dev_t dev;
   1005 	int rw;
   1006 	struct proc *p;
   1007 {
   1008 	register struct bpf_d *d;
   1009 	register int s;
   1010 
   1011 	if (rw != FREAD)
   1012 		return (0);
   1013 	/*
   1014 	 * An imitation of the FIONREAD ioctl code.
   1015 	 */
   1016 	d = &bpf_dtab[minor(dev)];
   1017 
   1018 	s = splimp();
   1019 	if (d->bd_hlen != 0 || (d->bd_immediate && d->bd_slen != 0)) {
   1020 		/*
   1021 		 * There is data waiting.
   1022 		 */
   1023 		splx(s);
   1024 		return (1);
   1025 	}
   1026 #if BSD >= 199103
   1027 	selrecord(p, &d->bd_sel);
   1028 #else
   1029 	/*
   1030 	 * No data ready.  If there's already a select() waiting on this
   1031 	 * minor device then this is a collision.  This shouldn't happen
   1032 	 * because minors really should not be shared, but if a process
   1033 	 * forks while one of these is open, it is possible that both
   1034 	 * processes could select on the same descriptor.
   1035 	 */
   1036 	if (d->bd_selproc && d->bd_selproc->p_wchan == (caddr_t)&selwait)
   1037 		d->bd_selcoll = 1;
   1038 	else
   1039 		d->bd_selproc = p;
   1040 #endif
   1041 	splx(s);
   1042 	return (0);
   1043 }
   1044 
   1045 /*
   1046  * Incoming linkage from device drivers.  Process the packet pkt, of length
   1047  * pktlen, which is stored in a contiguous buffer.  The packet is parsed
   1048  * by each process' filter, and if accepted, stashed into the corresponding
   1049  * buffer.
   1050  */
   1051 void
   1052 bpf_tap(arg, pkt, pktlen)
   1053 	caddr_t arg;
   1054 	register u_char *pkt;
   1055 	register u_int pktlen;
   1056 {
   1057 	struct bpf_if *bp;
   1058 	register struct bpf_d *d;
   1059 	register size_t slen;
   1060 	/*
   1061 	 * Note that the ipl does not have to be raised at this point.
   1062 	 * The only problem that could arise here is that if two different
   1063 	 * interfaces shared any data.  This is not the case.
   1064 	 */
   1065 	bp = (struct bpf_if *)arg;
   1066 	for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
   1067 		++d->bd_rcount;
   1068 		slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen);
   1069 		if (slen != 0)
   1070 			catchpacket(d, pkt, pktlen, slen, bcopy);
   1071 	}
   1072 }
   1073 
   1074 /*
   1075  * Copy data from an mbuf chain into a buffer.  This code is derived
   1076  * from m_copydata in sys/uipc_mbuf.c.
   1077  */
   1078 static void
   1079 bpf_mcopy(src_arg, dst_arg, len)
   1080 	const void *src_arg;
   1081 	void *dst_arg;
   1082 	register size_t len;
   1083 {
   1084 	register const struct mbuf *m;
   1085 	register u_int count;
   1086 	u_char *dst;
   1087 
   1088 	m = src_arg;
   1089 	dst = dst_arg;
   1090 	while (len > 0) {
   1091 		if (m == 0)
   1092 			panic("bpf_mcopy");
   1093 		count = min(m->m_len, len);
   1094 		bcopy(mtod(m, caddr_t), (caddr_t)dst, count);
   1095 		m = m->m_next;
   1096 		dst += count;
   1097 		len -= count;
   1098 	}
   1099 }
   1100 
   1101 /*
   1102  * Incoming linkage from device drivers, when packet is in an mbuf chain.
   1103  */
   1104 void
   1105 bpf_mtap(arg, m)
   1106 	caddr_t arg;
   1107 	struct mbuf *m;
   1108 {
   1109 	struct bpf_if *bp = (struct bpf_if *)arg;
   1110 	struct bpf_d *d;
   1111 	size_t pktlen, slen;
   1112 	struct mbuf *m0;
   1113 
   1114 	pktlen = 0;
   1115 	for (m0 = m; m0 != 0; m0 = m0->m_next)
   1116 		pktlen += m0->m_len;
   1117 
   1118 	for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
   1119 		++d->bd_rcount;
   1120 		slen = bpf_filter(d->bd_filter, (u_char *)m, pktlen, 0);
   1121 		if (slen != 0)
   1122 			catchpacket(d, (u_char *)m, pktlen, slen, bpf_mcopy);
   1123 	}
   1124 }
   1125 
   1126 /*
   1127  * Move the packet data from interface memory (pkt) into the
   1128  * store buffer.  Return 1 if it's time to wakeup a listener (buffer full),
   1129  * otherwise 0.  "copy" is the routine called to do the actual data
   1130  * transfer.  bcopy is passed in to copy contiguous chunks, while
   1131  * bpf_mcopy is passed in to copy mbuf chains.  In the latter case,
   1132  * pkt is really an mbuf.
   1133  */
   1134 static void
   1135 catchpacket(d, pkt, pktlen, snaplen, cpfn)
   1136 	register struct bpf_d *d;
   1137 	register u_char *pkt;
   1138 	register size_t pktlen, snaplen;
   1139 	register void (*cpfn) __P((const void *, void *, size_t));
   1140 {
   1141 	register struct bpf_hdr *hp;
   1142 	register int totlen, curlen;
   1143 	register int hdrlen = d->bd_bif->bif_hdrlen;
   1144 	/*
   1145 	 * Figure out how many bytes to move.  If the packet is
   1146 	 * greater or equal to the snapshot length, transfer that
   1147 	 * much.  Otherwise, transfer the whole packet (unless
   1148 	 * we hit the buffer size limit).
   1149 	 */
   1150 	totlen = hdrlen + min(snaplen, pktlen);
   1151 	if (totlen > d->bd_bufsize)
   1152 		totlen = d->bd_bufsize;
   1153 
   1154 	/*
   1155 	 * Round up the end of the previous packet to the next longword.
   1156 	 */
   1157 	curlen = BPF_WORDALIGN(d->bd_slen);
   1158 	if (curlen + totlen > d->bd_bufsize) {
   1159 		/*
   1160 		 * This packet will overflow the storage buffer.
   1161 		 * Rotate the buffers if we can, then wakeup any
   1162 		 * pending reads.
   1163 		 */
   1164 		if (d->bd_fbuf == 0) {
   1165 			/*
   1166 			 * We haven't completed the previous read yet,
   1167 			 * so drop the packet.
   1168 			 */
   1169 			++d->bd_dcount;
   1170 			return;
   1171 		}
   1172 		ROTATE_BUFFERS(d);
   1173 		bpf_wakeup(d);
   1174 		curlen = 0;
   1175 	}
   1176 	else if (d->bd_immediate)
   1177 		/*
   1178 		 * Immediate mode is set.  A packet arrived so any
   1179 		 * reads should be woken up.
   1180 		 */
   1181 		bpf_wakeup(d);
   1182 
   1183 	/*
   1184 	 * Append the bpf header.
   1185 	 */
   1186 	hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
   1187 #if BSD >= 199103
   1188 	microtime(&hp->bh_tstamp);
   1189 #elif defined(sun)
   1190 	uniqtime(&hp->bh_tstamp);
   1191 #else
   1192 	hp->bh_tstamp = time;
   1193 #endif
   1194 	hp->bh_datalen = pktlen;
   1195 	hp->bh_hdrlen = hdrlen;
   1196 	/*
   1197 	 * Copy the packet data into the store buffer and update its length.
   1198 	 */
   1199 	(*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen));
   1200 	d->bd_slen = curlen + totlen;
   1201 }
   1202 
   1203 /*
   1204  * Initialize all nonzero fields of a descriptor.
   1205  */
   1206 static int
   1207 bpf_allocbufs(d)
   1208 	register struct bpf_d *d;
   1209 {
   1210 	d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_DEVBUF, M_WAITOK);
   1211 	if (d->bd_fbuf == 0)
   1212 		return (ENOBUFS);
   1213 
   1214 	d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_DEVBUF, M_WAITOK);
   1215 	if (d->bd_sbuf == 0) {
   1216 		free(d->bd_fbuf, M_DEVBUF);
   1217 		return (ENOBUFS);
   1218 	}
   1219 	d->bd_slen = 0;
   1220 	d->bd_hlen = 0;
   1221 	return (0);
   1222 }
   1223 
   1224 /*
   1225  * Free buffers currently in use by a descriptor.
   1226  * Called on close.
   1227  */
   1228 static void
   1229 bpf_freed(d)
   1230 	register struct bpf_d *d;
   1231 {
   1232 	/*
   1233 	 * We don't need to lock out interrupts since this descriptor has
   1234 	 * been detached from its interface and it yet hasn't been marked
   1235 	 * free.
   1236 	 */
   1237 	if (d->bd_sbuf != 0) {
   1238 		free(d->bd_sbuf, M_DEVBUF);
   1239 		if (d->bd_hbuf != 0)
   1240 			free(d->bd_hbuf, M_DEVBUF);
   1241 		if (d->bd_fbuf != 0)
   1242 			free(d->bd_fbuf, M_DEVBUF);
   1243 	}
   1244 	if (d->bd_filter)
   1245 		free((caddr_t)d->bd_filter, M_DEVBUF);
   1246 
   1247 	D_MARKFREE(d);
   1248 }
   1249 
   1250 /*
   1251  * Attach an interface to bpf.  driverp is a pointer to a (struct bpf_if *)
   1252  * in the driver's softc; dlt is the link layer type; hdrlen is the fixed
   1253  * size of the link header (variable length headers not yet supported).
   1254  */
   1255 void
   1256 bpfattach(driverp, ifp, dlt, hdrlen)
   1257 	caddr_t *driverp;
   1258 	struct ifnet *ifp;
   1259 	u_int dlt, hdrlen;
   1260 {
   1261 	struct bpf_if *bp;
   1262 	int i;
   1263 #if BSD < 199103
   1264 	static struct bpf_if bpf_ifs[NBPFILTER];
   1265 	static int bpfifno;
   1266 
   1267 	bp = (bpfifno < NBPFILTER) ? &bpf_ifs[bpfifno++] : 0;
   1268 #else
   1269 	bp = (struct bpf_if *)malloc(sizeof(*bp), M_DEVBUF, M_DONTWAIT);
   1270 #endif
   1271 	if (bp == 0)
   1272 		panic("bpfattach");
   1273 
   1274 	bp->bif_dlist = 0;
   1275 	bp->bif_driverp = (struct bpf_if **)driverp;
   1276 	bp->bif_ifp = ifp;
   1277 	bp->bif_dlt = dlt;
   1278 
   1279 	bp->bif_next = bpf_iflist;
   1280 	bpf_iflist = bp;
   1281 
   1282 	*bp->bif_driverp = 0;
   1283 
   1284 	/*
   1285 	 * Compute the length of the bpf header.  This is not necessarily
   1286 	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
   1287 	 * that the network layer header begins on a longword boundary (for
   1288 	 * performance reasons and to alleviate alignment restrictions).
   1289 	 */
   1290 	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
   1291 
   1292 	/*
   1293 	 * Mark all the descriptors free if this hasn't been done.
   1294 	 */
   1295 	if (!D_ISFREE(&bpf_dtab[0]))
   1296 		for (i = 0; i < NBPFILTER; ++i)
   1297 			D_MARKFREE(&bpf_dtab[i]);
   1298 
   1299 #if 0
   1300 	printf("bpf: %s%d attached\n", ifp->if_name, ifp->if_unit);
   1301 #endif
   1302 }
   1303 
   1304 #if BSD >= 199103
   1305 /* XXX This routine belongs in net/if.c. */
   1306 /*
   1307  * Set/clear promiscuous mode on interface ifp based on the truth value
   1308  * of pswitch.  The calls are reference counted so that only the first
   1309  * "on" request actually has an effect, as does the final "off" request.
   1310  * Results are undefined if the "off" and "on" requests are not matched.
   1311  */
   1312 int
   1313 ifpromisc(ifp, pswitch)
   1314 	struct ifnet *ifp;
   1315 	int pswitch;
   1316 {
   1317 	struct ifreq ifr;
   1318 
   1319 	if (pswitch) {
   1320 		/*
   1321 		 * If the device is not configured up, we cannot put it in
   1322 		 * promiscuous mode.
   1323 		 */
   1324 		if ((ifp->if_flags & IFF_UP) == 0)
   1325 			return (ENETDOWN);
   1326 		if (ifp->if_pcount++ != 0)
   1327 			return (0);
   1328 		ifp->if_flags |= IFF_PROMISC;
   1329 	} else {
   1330 		if (--ifp->if_pcount > 0)
   1331 			return (0);
   1332 		ifp->if_flags &= ~IFF_PROMISC;
   1333 		/*
   1334 		 * If the device is not configured up, we should not need to
   1335 		 * turn off promiscuous mode (device should have turned it
   1336 		 * off when interface went down; and will look at IFF_PROMISC
   1337 		 * again next time interface comes up).
   1338 		 */
   1339 		if ((ifp->if_flags & IFF_UP) == 0)
   1340 			return (0);
   1341 	}
   1342 	ifr.ifr_flags = ifp->if_flags;
   1343 	return ((*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr));
   1344 }
   1345 #endif
   1346 
   1347 #if BSD < 199103
   1348 /*
   1349  * Allocate some memory for bpf.  This is temporary SunOS support, and
   1350  * is admittedly a hack.
   1351  * If resources unavaiable, return 0.
   1352  */
   1353 static caddr_t
   1354 bpf_alloc(size, canwait)
   1355 	register int size;
   1356 	register int canwait;
   1357 {
   1358 	register struct mbuf *m;
   1359 
   1360 	if ((unsigned)size > (MCLBYTES-8))
   1361 		return 0;
   1362 
   1363 	MGET(m, canwait, MT_DATA);
   1364 	if (m == 0)
   1365 		return 0;
   1366 	if ((unsigned)size > (MLEN-8)) {
   1367 		MCLGET(m);
   1368 		if (m->m_len != MCLBYTES) {
   1369 			m_freem(m);
   1370 			return 0;
   1371 		}
   1372 	}
   1373 	*mtod(m, struct mbuf **) = m;
   1374 	return mtod(m, caddr_t) + 8;
   1375 }
   1376 #endif
   1377