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