Home | History | Annotate | Line # | Download | only in net
bpf.c revision 1.104
      1 /*	$NetBSD: bpf.c,v 1.104 2004/08/19 20:58:23 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. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *	@(#)bpf.c	8.4 (Berkeley) 1/9/95
     37  * static char rcsid[] =
     38  * "Header: bpf.c,v 1.67 96/09/26 22:00:52 leres Exp ";
     39  */
     40 
     41 #include <sys/cdefs.h>
     42 __KERNEL_RCSID(0, "$NetBSD: bpf.c,v 1.104 2004/08/19 20:58:23 christos Exp $");
     43 
     44 #include "bpfilter.h"
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/mbuf.h>
     49 #include <sys/buf.h>
     50 #include <sys/time.h>
     51 #include <sys/proc.h>
     52 #include <sys/user.h>
     53 #include <sys/ioctl.h>
     54 #include <sys/conf.h>
     55 #include <sys/vnode.h>
     56 
     57 #include <sys/file.h>
     58 #include <sys/tty.h>
     59 #include <sys/uio.h>
     60 
     61 #include <sys/protosw.h>
     62 #include <sys/socket.h>
     63 #include <sys/errno.h>
     64 #include <sys/kernel.h>
     65 #include <sys/poll.h>
     66 #include <sys/sysctl.h>
     67 
     68 #include <net/if.h>
     69 #include <net/slip.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 #if defined(_KERNEL_OPT)
     81 #include "opt_bpf.h"
     82 #include "sl.h"
     83 #include "strip.h"
     84 #endif
     85 
     86 #ifndef BPF_BUFSIZE
     87 /*
     88  * 4096 is too small for FDDI frames. 8192 is too small for gigabit Ethernet
     89  * jumbos (circa 9k), ATM, or Intel gig/10gig ethernet jumbos (16k).
     90  */
     91 # define BPF_BUFSIZE 32768
     92 #endif
     93 
     94 #define PRINET  26			/* interruptible */
     95 
     96 /*
     97  * The default read buffer size, and limit for BIOCSBLEN, is sysctl'able.
     98  * XXX the default values should be computed dynamically based
     99  * on available memory size and available mbuf clusters.
    100  */
    101 int bpf_bufsize = BPF_BUFSIZE;
    102 int bpf_maxbufsize = BPF_DFLTBUFSIZE;	/* XXX set dynamically, see above */
    103 
    104 /*
    105  *  bpf_iflist is the list of interfaces; each corresponds to an ifnet
    106  *  bpf_dtab holds the descriptors, indexed by minor device #
    107  */
    108 struct bpf_if	*bpf_iflist;
    109 struct bpf_d	bpf_dtab[NBPFILTER];
    110 
    111 static int	bpf_allocbufs(struct bpf_d *);
    112 static void	bpf_deliver(struct bpf_if *,
    113 		            void *(*cpfn)(void *, const void *, size_t),
    114 			    void *, u_int, u_int, struct ifnet *);
    115 static void	bpf_freed(struct bpf_d *);
    116 static void	bpf_ifname(struct ifnet *, struct ifreq *);
    117 static void	*bpf_mcpy(void *, const void *, size_t);
    118 static int	bpf_movein(struct uio *, int, int,
    119 			        struct mbuf **, struct sockaddr *);
    120 static void	bpf_attachd(struct bpf_d *, struct bpf_if *);
    121 static void	bpf_detachd(struct bpf_d *);
    122 static int	bpf_setif(struct bpf_d *, struct ifreq *);
    123 static void	bpf_timed_out(void *);
    124 static __inline void
    125 		bpf_wakeup(struct bpf_d *);
    126 static void	catchpacket(struct bpf_d *, u_char *, u_int, u_int,
    127 				 void *(*)(void *, const void *, size_t));
    128 static void	reset_d(struct bpf_d *);
    129 static int	bpf_getdltlist(struct bpf_d *, struct bpf_dltlist *);
    130 static int	bpf_setdlt(struct bpf_d *, u_int);
    131 
    132 dev_type_open(bpfopen);
    133 dev_type_close(bpfclose);
    134 dev_type_read(bpfread);
    135 dev_type_write(bpfwrite);
    136 dev_type_ioctl(bpfioctl);
    137 dev_type_poll(bpfpoll);
    138 dev_type_kqfilter(bpfkqfilter);
    139 
    140 const struct cdevsw bpf_cdevsw = {
    141 	bpfopen, bpfclose, bpfread, bpfwrite, bpfioctl,
    142 	nostop, notty, bpfpoll, nommap, bpfkqfilter,
    143 };
    144 
    145 static int
    146 bpf_movein(uio, linktype, mtu, mp, sockp)
    147 	struct uio *uio;
    148 	int linktype;
    149 	int mtu;
    150 	struct mbuf **mp;
    151 	struct sockaddr *sockp;
    152 {
    153 	struct mbuf *m;
    154 	int error;
    155 	int len;
    156 	int hlen;
    157 	int align;
    158 
    159 	/*
    160 	 * Build a sockaddr based on the data link layer type.
    161 	 * We do this at this level because the ethernet header
    162 	 * is copied directly into the data field of the sockaddr.
    163 	 * In the case of SLIP, there is no header and the packet
    164 	 * is forwarded as is.
    165 	 * Also, we are careful to leave room at the front of the mbuf
    166 	 * for the link level header.
    167 	 */
    168 	switch (linktype) {
    169 
    170 	case DLT_SLIP:
    171 		sockp->sa_family = AF_INET;
    172 		hlen = 0;
    173 		align = 0;
    174 		break;
    175 
    176 	case DLT_PPP:
    177 		sockp->sa_family = AF_UNSPEC;
    178 		hlen = 0;
    179 		align = 0;
    180 		break;
    181 
    182 	case DLT_EN10MB:
    183 		sockp->sa_family = AF_UNSPEC;
    184 		/* XXX Would MAXLINKHDR be better? */
    185  		/* 6(dst)+6(src)+2(type) */
    186 		hlen = sizeof(struct ether_header);
    187 		align = 2;
    188 		break;
    189 
    190 	case DLT_ARCNET:
    191 		sockp->sa_family = AF_UNSPEC;
    192 		hlen = ARC_HDRLEN;
    193 		align = 5;
    194 		break;
    195 
    196 	case DLT_FDDI:
    197 		sockp->sa_family = AF_LINK;
    198 		/* XXX 4(FORMAC)+6(dst)+6(src) */
    199 		hlen = 16;
    200 		align = 0;
    201 		break;
    202 
    203 	case DLT_ECONET:
    204 		sockp->sa_family = AF_UNSPEC;
    205 		hlen = 6;
    206 		align = 2;
    207 		break;
    208 
    209 	case DLT_NULL:
    210 		sockp->sa_family = AF_UNSPEC;
    211 		hlen = 0;
    212 		align = 0;
    213 		break;
    214 
    215 	default:
    216 		return (EIO);
    217 	}
    218 
    219 	len = uio->uio_resid;
    220 	/*
    221 	 * If there aren't enough bytes for a link level header or the
    222 	 * packet length exceeds the interface mtu, return an error.
    223 	 */
    224 	if (len < hlen || len - hlen > mtu)
    225 		return (EMSGSIZE);
    226 
    227 	/*
    228 	 * XXX Avoid complicated buffer chaining ---
    229 	 * bail if it won't fit in a single mbuf.
    230 	 * (Take into account possible alignment bytes)
    231 	 */
    232 	if ((unsigned)len > MCLBYTES - align)
    233 		return (EIO);
    234 
    235 	m = m_gethdr(M_WAIT, MT_DATA);
    236 	m->m_pkthdr.rcvif = 0;
    237 	m->m_pkthdr.len = len - hlen;
    238 	if (len > MHLEN - align) {
    239 		m_clget(m, M_WAIT);
    240 		if ((m->m_flags & M_EXT) == 0) {
    241 			error = ENOBUFS;
    242 			goto bad;
    243 		}
    244 	}
    245 
    246 	/* Insure the data is properly aligned */
    247 	if (align > 0) {
    248 		m->m_data += align;
    249 		m->m_len -= align;
    250 	}
    251 
    252 	error = uiomove(mtod(m, void *), len, uio);
    253 	if (error)
    254 		goto bad;
    255 	if (hlen != 0) {
    256 		memcpy(sockp->sa_data, mtod(m, void *), hlen);
    257 		m->m_data += hlen; /* XXX */
    258 		len -= hlen;
    259 	}
    260 	m->m_len = len;
    261 	*mp = m;
    262 	return (0);
    263 
    264 bad:
    265 	m_freem(m);
    266 	return (error);
    267 }
    268 
    269 /*
    270  * Attach file to the bpf interface, i.e. make d listen on bp.
    271  * Must be called at splnet.
    272  */
    273 static void
    274 bpf_attachd(d, bp)
    275 	struct bpf_d *d;
    276 	struct bpf_if *bp;
    277 {
    278 	/*
    279 	 * Point d at bp, and add d to the interface's list of listeners.
    280 	 * Finally, point the driver's bpf cookie at the interface so
    281 	 * it will divert packets to bpf.
    282 	 */
    283 	d->bd_bif = bp;
    284 	d->bd_next = bp->bif_dlist;
    285 	bp->bif_dlist = d;
    286 
    287 	*bp->bif_driverp = bp;
    288 }
    289 
    290 /*
    291  * Detach a file from its interface.
    292  */
    293 static void
    294 bpf_detachd(d)
    295 	struct bpf_d *d;
    296 {
    297 	struct bpf_d **p;
    298 	struct bpf_if *bp;
    299 
    300 	bp = d->bd_bif;
    301 	/*
    302 	 * Check if this descriptor had requested promiscuous mode.
    303 	 * If so, turn it off.
    304 	 */
    305 	if (d->bd_promisc) {
    306 		int error;
    307 
    308 		d->bd_promisc = 0;
    309 		/*
    310 		 * Take device out of promiscuous mode.  Since we were
    311 		 * able to enter promiscuous mode, we should be able
    312 		 * to turn it off.  But we can get an error if
    313 		 * the interface was configured down, so only panic
    314 		 * if we don't get an unexpected error.
    315 		 */
    316   		error = ifpromisc(bp->bif_ifp, 0);
    317 		if (error && error != EINVAL)
    318 			panic("bpf: ifpromisc failed");
    319 	}
    320 	/* Remove d from the interface's descriptor list. */
    321 	p = &bp->bif_dlist;
    322 	while (*p != d) {
    323 		p = &(*p)->bd_next;
    324 		if (*p == 0)
    325 			panic("bpf_detachd: descriptor not in list");
    326 	}
    327 	*p = (*p)->bd_next;
    328 	if (bp->bif_dlist == 0)
    329 		/*
    330 		 * Let the driver know that there are no more listeners.
    331 		 */
    332 		*d->bd_bif->bif_driverp = 0;
    333 	d->bd_bif = 0;
    334 }
    335 
    336 
    337 /*
    338  * Mark a descriptor free by making it point to itself.
    339  * This is probably cheaper than marking with a constant since
    340  * the address should be in a register anyway.
    341  */
    342 #define D_ISFREE(d) ((d) == (d)->bd_next)
    343 #define D_MARKFREE(d) ((d)->bd_next = (d))
    344 #define D_MARKUSED(d) ((d)->bd_next = 0)
    345 
    346 /*
    347  * bpfilterattach() is called at boot time.
    348  */
    349 /* ARGSUSED */
    350 void
    351 bpfilterattach(n)
    352 	int n;
    353 {
    354 	int i;
    355 	/*
    356 	 * Mark all the descriptors free.
    357 	 */
    358 	for (i = 0; i < NBPFILTER; ++i)
    359 		D_MARKFREE(&bpf_dtab[i]);
    360 
    361 }
    362 
    363 /*
    364  * Open ethernet device.  Returns ENXIO for illegal minor device number,
    365  * EBUSY if file is open by another process.
    366  */
    367 /* ARGSUSED */
    368 int
    369 bpfopen(dev, flag, mode, p)
    370 	dev_t dev;
    371 	int flag;
    372 	int mode;
    373 	struct proc *p;
    374 {
    375 	struct bpf_d *d;
    376 
    377 	if (minor(dev) >= NBPFILTER)
    378 		return (ENXIO);
    379 	/*
    380 	 * Each minor can be opened by only one process.  If the requested
    381 	 * minor is in use, return EBUSY.
    382 	 */
    383 	d = &bpf_dtab[minor(dev)];
    384 	if (!D_ISFREE(d))
    385 		return (EBUSY);
    386 
    387 	/* Mark "free" and do most initialization. */
    388 	memset((char *)d, 0, sizeof(*d));
    389 	d->bd_bufsize = bpf_bufsize;
    390 	d->bd_seesent = 1;
    391 	callout_init(&d->bd_callout);
    392 
    393 	return (0);
    394 }
    395 
    396 /*
    397  * Close the descriptor by detaching it from its interface,
    398  * deallocating its buffers, and marking it free.
    399  */
    400 /* ARGSUSED */
    401 int
    402 bpfclose(dev, flag, mode, p)
    403 	dev_t dev;
    404 	int flag;
    405 	int mode;
    406 	struct proc *p;
    407 {
    408 	struct bpf_d *d = &bpf_dtab[minor(dev)];
    409 	int s;
    410 
    411 	s = splnet();
    412 	if (d->bd_state == BPF_WAITING)
    413 		callout_stop(&d->bd_callout);
    414 	d->bd_state = BPF_IDLE;
    415 	if (d->bd_bif)
    416 		bpf_detachd(d);
    417 	splx(s);
    418 	bpf_freed(d);
    419 
    420 	return (0);
    421 }
    422 
    423 /*
    424  * Rotate the packet buffers in descriptor d.  Move the store buffer
    425  * into the hold slot, and the free buffer into the store slot.
    426  * Zero the length of the new store buffer.
    427  */
    428 #define ROTATE_BUFFERS(d) \
    429 	(d)->bd_hbuf = (d)->bd_sbuf; \
    430 	(d)->bd_hlen = (d)->bd_slen; \
    431 	(d)->bd_sbuf = (d)->bd_fbuf; \
    432 	(d)->bd_slen = 0; \
    433 	(d)->bd_fbuf = 0;
    434 /*
    435  *  bpfread - read next chunk of packets from buffers
    436  */
    437 int
    438 bpfread(dev, uio, ioflag)
    439 	dev_t dev;
    440 	struct uio *uio;
    441 	int ioflag;
    442 {
    443 	struct bpf_d *d = &bpf_dtab[minor(dev)];
    444 	int timed_out;
    445 	int error;
    446 	int s;
    447 
    448 	/*
    449 	 * Restrict application to use a buffer the same size as
    450 	 * as kernel buffers.
    451 	 */
    452 	if (uio->uio_resid != d->bd_bufsize)
    453 		return (EINVAL);
    454 
    455 	s = splnet();
    456 	if (d->bd_state == BPF_WAITING)
    457 		callout_stop(&d->bd_callout);
    458 	timed_out = (d->bd_state == BPF_TIMED_OUT);
    459 	d->bd_state = BPF_IDLE;
    460 	/*
    461 	 * If the hold buffer is empty, then do a timed sleep, which
    462 	 * ends when the timeout expires or when enough packets
    463 	 * have arrived to fill the store buffer.
    464 	 */
    465 	while (d->bd_hbuf == 0) {
    466 		if (ioflag & IO_NDELAY) {
    467 			if (d->bd_slen == 0) {
    468 				splx(s);
    469 				return (EWOULDBLOCK);
    470 			}
    471 			ROTATE_BUFFERS(d);
    472 			break;
    473 		}
    474 
    475 		if ((d->bd_immediate || timed_out) && d->bd_slen != 0) {
    476 			/*
    477 			 * A packet(s) either arrived since the previous
    478 			 * read or arrived while we were asleep.
    479 			 * Rotate the buffers and return what's here.
    480 			 */
    481 			ROTATE_BUFFERS(d);
    482 			break;
    483 		}
    484 		error = tsleep(d, PRINET|PCATCH, "bpf",
    485 				d->bd_rtout);
    486 		if (error == EINTR || error == ERESTART) {
    487 			splx(s);
    488 			return (error);
    489 		}
    490 		if (error == EWOULDBLOCK) {
    491 			/*
    492 			 * On a timeout, return what's in the buffer,
    493 			 * which may be nothing.  If there is something
    494 			 * in the store buffer, we can rotate the buffers.
    495 			 */
    496 			if (d->bd_hbuf)
    497 				/*
    498 				 * We filled up the buffer in between
    499 				 * getting the timeout and arriving
    500 				 * here, so we don't need to rotate.
    501 				 */
    502 				break;
    503 
    504 			if (d->bd_slen == 0) {
    505 				splx(s);
    506 				return (0);
    507 			}
    508 			ROTATE_BUFFERS(d);
    509 			break;
    510 		}
    511 		if (error != 0)
    512 			goto done;
    513 	}
    514 	/*
    515 	 * At this point, we know we have something in the hold slot.
    516 	 */
    517 	splx(s);
    518 
    519 	/*
    520 	 * Move data from hold buffer into user space.
    521 	 * We know the entire buffer is transferred since
    522 	 * we checked above that the read buffer is bpf_bufsize bytes.
    523 	 */
    524 	error = uiomove(d->bd_hbuf, d->bd_hlen, uio);
    525 
    526 	s = splnet();
    527 	d->bd_fbuf = d->bd_hbuf;
    528 	d->bd_hbuf = 0;
    529 	d->bd_hlen = 0;
    530 done:
    531 	splx(s);
    532 	return (error);
    533 }
    534 
    535 
    536 /*
    537  * If there are processes sleeping on this descriptor, wake them up.
    538  */
    539 static __inline void
    540 bpf_wakeup(d)
    541 	struct bpf_d *d;
    542 {
    543 	wakeup(d);
    544 	if (d->bd_async)
    545 		fownsignal(d->bd_pgid, SIGIO, 0, 0, NULL);
    546 
    547 	selnotify(&d->bd_sel, 0);
    548 	/* XXX */
    549 	d->bd_sel.sel_pid = 0;
    550 }
    551 
    552 
    553 static void
    554 bpf_timed_out(arg)
    555 	void *arg;
    556 {
    557 	struct bpf_d *d = arg;
    558 	int s;
    559 
    560 	s = splnet();
    561 	if (d->bd_state == BPF_WAITING) {
    562 		d->bd_state = BPF_TIMED_OUT;
    563 		if (d->bd_slen != 0)
    564 			bpf_wakeup(d);
    565 	}
    566 	splx(s);
    567 }
    568 
    569 
    570 int
    571 bpfwrite(dev, uio, ioflag)
    572 	dev_t dev;
    573 	struct uio *uio;
    574 	int ioflag;
    575 {
    576 	struct bpf_d *d = &bpf_dtab[minor(dev)];
    577 	struct ifnet *ifp;
    578 	struct mbuf *m;
    579 	int error, s;
    580 	static struct sockaddr_storage dst;
    581 
    582 	if (d->bd_bif == 0)
    583 		return (ENXIO);
    584 
    585 	ifp = d->bd_bif->bif_ifp;
    586 
    587 	if (uio->uio_resid == 0)
    588 		return (0);
    589 
    590 	error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, ifp->if_mtu, &m,
    591 		(struct sockaddr *) &dst);
    592 	if (error)
    593 		return (error);
    594 
    595 	if (m->m_pkthdr.len > ifp->if_mtu)
    596 		return (EMSGSIZE);
    597 
    598 	if (d->bd_hdrcmplt)
    599 		dst.ss_family = pseudo_AF_HDRCMPLT;
    600 
    601 	s = splsoftnet();
    602 	error = (*ifp->if_output)(ifp, m, (struct sockaddr *) &dst, NULL);
    603 	splx(s);
    604 	/*
    605 	 * The driver frees the mbuf.
    606 	 */
    607 	return (error);
    608 }
    609 
    610 /*
    611  * Reset a descriptor by flushing its packet buffer and clearing the
    612  * receive and drop counts.  Should be called at splnet.
    613  */
    614 static void
    615 reset_d(d)
    616 	struct bpf_d *d;
    617 {
    618 	if (d->bd_hbuf) {
    619 		/* Free the hold buffer. */
    620 		d->bd_fbuf = d->bd_hbuf;
    621 		d->bd_hbuf = 0;
    622 	}
    623 	d->bd_slen = 0;
    624 	d->bd_hlen = 0;
    625 	d->bd_rcount = 0;
    626 	d->bd_dcount = 0;
    627 	d->bd_ccount = 0;
    628 }
    629 
    630 #ifdef BPF_KERN_FILTER
    631 extern struct bpf_insn *bpf_tcp_filter;
    632 extern struct bpf_insn *bpf_udp_filter;
    633 #endif
    634 
    635 /*
    636  *  FIONREAD		Check for read packet available.
    637  *  BIOCGBLEN		Get buffer len [for read()].
    638  *  BIOCSETF		Set ethernet read filter.
    639  *  BIOCFLUSH		Flush read packet buffer.
    640  *  BIOCPROMISC		Put interface into promiscuous mode.
    641  *  BIOCGDLT		Get link layer type.
    642  *  BIOCGETIF		Get interface name.
    643  *  BIOCSETIF		Set interface.
    644  *  BIOCSRTIMEOUT	Set read timeout.
    645  *  BIOCGRTIMEOUT	Get read timeout.
    646  *  BIOCGSTATS		Get packet stats.
    647  *  BIOCIMMEDIATE	Set immediate mode.
    648  *  BIOCVERSION		Get filter language version.
    649  *  BIOGHDRCMPLT	Get "header already complete" flag.
    650  *  BIOSHDRCMPLT	Set "header already complete" flag.
    651  */
    652 /* ARGSUSED */
    653 int
    654 bpfioctl(dev_t dev, u_long cmd, caddr_t arg, int flag, struct proc *p)
    655 {
    656 	struct bpf_d *d = &bpf_dtab[minor(dev)];
    657 	int s, error = 0;
    658 #ifdef BPF_KERN_FILTER
    659 	struct bpf_insn **p;
    660 #endif
    661 	void *addr = arg;
    662 
    663 	s = splnet();
    664 	if (d->bd_state == BPF_WAITING)
    665 		callout_stop(&d->bd_callout);
    666 	d->bd_state = BPF_IDLE;
    667 	splx(s);
    668 
    669 	switch (cmd) {
    670 
    671 	default:
    672 		error = EINVAL;
    673 		break;
    674 
    675 	/*
    676 	 * Check for read packet available.
    677 	 */
    678 	case FIONREAD:
    679 		{
    680 			int n;
    681 
    682 			s = splnet();
    683 			n = d->bd_slen;
    684 			if (d->bd_hbuf)
    685 				n += d->bd_hlen;
    686 			splx(s);
    687 
    688 			*(int *)addr = n;
    689 			break;
    690 		}
    691 
    692 	/*
    693 	 * Get buffer len [for read()].
    694 	 */
    695 	case BIOCGBLEN:
    696 		*(u_int *)addr = d->bd_bufsize;
    697 		break;
    698 
    699 	/*
    700 	 * Set buffer length.
    701 	 */
    702 	case BIOCSBLEN:
    703 		if (d->bd_bif != 0)
    704 			error = EINVAL;
    705 		else {
    706 			u_int size = *(u_int *)addr;
    707 
    708 			if (size > bpf_maxbufsize)
    709 				*(u_int *)addr = size = bpf_maxbufsize;
    710 			else if (size < BPF_MINBUFSIZE)
    711 				*(u_int *)addr = size = BPF_MINBUFSIZE;
    712 			d->bd_bufsize = size;
    713 		}
    714 		break;
    715 
    716 	/*
    717 	 * Set link layer read filter.
    718 	 */
    719 	case BIOCSETF:
    720 		error = bpf_setf(d, addr);
    721 		break;
    722 
    723 #ifdef BPF_KERN_FILTER
    724 	/*
    725 	 * Set TCP or UDP reject filter.
    726 	 */
    727 	case BIOCSTCPF:
    728 	case BIOCSUDPF:
    729 		if (!suser()) {
    730 			error = EPERM;
    731 			break;
    732 		}
    733 
    734 		/* Validate and store filter */
    735 		error = bpf_setf(d, addr);
    736 
    737 		/* Free possible old filter */
    738 		if (cmd == BIOCSTCPF)
    739 			p = &bpf_tcp_filter;
    740 		else
    741 			p = &bpf_udp_filter;
    742 		if (*p != NULL)
    743 			free(*p, M_DEVBUF);
    744 
    745 		/* Steal new filter (noop if error) */
    746 		s = splnet();
    747 		*p = d->bd_filter;
    748 		d->bd_filter = NULL;
    749 		splx(s);
    750 		break;
    751 #endif
    752 
    753 	/*
    754 	 * Flush read packet buffer.
    755 	 */
    756 	case BIOCFLUSH:
    757 		s = splnet();
    758 		reset_d(d);
    759 		splx(s);
    760 		break;
    761 
    762 	/*
    763 	 * Put interface into promiscuous mode.
    764 	 */
    765 	case BIOCPROMISC:
    766 		if (d->bd_bif == 0) {
    767 			/*
    768 			 * No interface attached yet.
    769 			 */
    770 			error = EINVAL;
    771 			break;
    772 		}
    773 		s = splnet();
    774 		if (d->bd_promisc == 0) {
    775 			error = ifpromisc(d->bd_bif->bif_ifp, 1);
    776 			if (error == 0)
    777 				d->bd_promisc = 1;
    778 		}
    779 		splx(s);
    780 		break;
    781 
    782 	/*
    783 	 * Get device parameters.
    784 	 */
    785 	case BIOCGDLT:
    786 		if (d->bd_bif == 0)
    787 			error = EINVAL;
    788 		else
    789 			*(u_int *)addr = d->bd_bif->bif_dlt;
    790 		break;
    791 
    792 	/*
    793 	 * Get a list of supported device parameters.
    794 	 */
    795 	case BIOCGDLTLIST:
    796 		if (d->bd_bif == 0)
    797 			error = EINVAL;
    798 		else
    799 			error = bpf_getdltlist(d, addr);
    800 		break;
    801 
    802 	/*
    803 	 * Set device parameters.
    804 	 */
    805 	case BIOCSDLT:
    806 		if (d->bd_bif == 0)
    807 			error = EINVAL;
    808 		else
    809 			error = bpf_setdlt(d, *(u_int *)addr);
    810 		break;
    811 
    812 	/*
    813 	 * Set interface name.
    814 	 */
    815 	case BIOCGETIF:
    816 		if (d->bd_bif == 0)
    817 			error = EINVAL;
    818 		else
    819 			bpf_ifname(d->bd_bif->bif_ifp, addr);
    820 		break;
    821 
    822 	/*
    823 	 * Set interface.
    824 	 */
    825 	case BIOCSETIF:
    826 		error = bpf_setif(d, addr);
    827 		break;
    828 
    829 	/*
    830 	 * Set read timeout.
    831 	 */
    832 	case BIOCSRTIMEOUT:
    833 		{
    834 			struct timeval *tv = addr;
    835 
    836 			/* Compute number of ticks. */
    837 			d->bd_rtout = tv->tv_sec * hz + tv->tv_usec / tick;
    838 			if ((d->bd_rtout == 0) && (tv->tv_usec != 0))
    839 				d->bd_rtout = 1;
    840 			break;
    841 		}
    842 
    843 	/*
    844 	 * Get read timeout.
    845 	 */
    846 	case BIOCGRTIMEOUT:
    847 		{
    848 			struct timeval *tv = addr;
    849 
    850 			tv->tv_sec = d->bd_rtout / hz;
    851 			tv->tv_usec = (d->bd_rtout % hz) * tick;
    852 			break;
    853 		}
    854 
    855 	/*
    856 	 * Get packet stats.
    857 	 */
    858 	case BIOCGSTATS:
    859 		{
    860 			struct bpf_stat *bs = addr;
    861 
    862 			bs->bs_recv = d->bd_rcount;
    863 			bs->bs_drop = d->bd_dcount;
    864 			bs->bs_capt = d->bd_ccount;
    865 			break;
    866 		}
    867 
    868 	case BIOCGSTATSOLD:
    869 		{
    870 			struct bpf_stat_old *bs = addr;
    871 
    872 			bs->bs_recv = d->bd_rcount;
    873 			bs->bs_drop = d->bd_dcount;
    874 			break;
    875 		}
    876 
    877 	/*
    878 	 * Set immediate mode.
    879 	 */
    880 	case BIOCIMMEDIATE:
    881 		d->bd_immediate = *(u_int *)addr;
    882 		break;
    883 
    884 	case BIOCVERSION:
    885 		{
    886 			struct bpf_version *bv = addr;
    887 
    888 			bv->bv_major = BPF_MAJOR_VERSION;
    889 			bv->bv_minor = BPF_MINOR_VERSION;
    890 			break;
    891 		}
    892 
    893 	case BIOCGHDRCMPLT:	/* get "header already complete" flag */
    894 		*(u_int *)addr = d->bd_hdrcmplt;
    895 		break;
    896 
    897 	case BIOCSHDRCMPLT:	/* set "header already complete" flag */
    898 		d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
    899 		break;
    900 
    901 	/*
    902 	 * Get "see sent packets" flag
    903 	 */
    904 	case BIOCGSEESENT:
    905 		*(u_int *)addr = d->bd_seesent;
    906 		break;
    907 
    908 	/*
    909 	 * Set "see sent" packets flag
    910 	 */
    911 	case BIOCSSEESENT:
    912 		d->bd_seesent = *(u_int *)addr;
    913 		break;
    914 
    915 	case FIONBIO:		/* Non-blocking I/O */
    916 		/*
    917 		 * No need to do anything special as we use IO_NDELAY in
    918 		 * bpfread() as an indication of whether or not to block
    919 		 * the read.
    920 		 */
    921 		break;
    922 
    923 	case FIOASYNC:		/* Send signal on receive packets */
    924 		d->bd_async = *(int *)addr;
    925 		break;
    926 
    927 	case TIOCSPGRP:		/* Process or group to send signals to */
    928 	case FIOSETOWN:
    929 		error = fsetown(p, &d->bd_pgid, cmd, addr);
    930 		break;
    931 
    932 	case TIOCGPGRP:
    933 	case FIOGETOWN:
    934 		error = fgetown(p, d->bd_pgid, cmd, addr);
    935 		break;
    936 	}
    937 	return (error);
    938 }
    939 
    940 /*
    941  * Set d's packet filter program to fp.  If this file already has a filter,
    942  * free it and replace it.  Returns EINVAL for bogus requests.
    943  */
    944 int
    945 bpf_setf(struct bpf_d *d, struct bpf_program *fp)
    946 {
    947 	struct bpf_insn *fcode, *old;
    948 	u_int flen, size;
    949 	int s;
    950 
    951 	old = d->bd_filter;
    952 	if (fp->bf_insns == 0) {
    953 		if (fp->bf_len != 0)
    954 			return (EINVAL);
    955 		s = splnet();
    956 		d->bd_filter = 0;
    957 		reset_d(d);
    958 		splx(s);
    959 		if (old != 0)
    960 			free(old, M_DEVBUF);
    961 		return (0);
    962 	}
    963 	flen = fp->bf_len;
    964 	if (flen > BPF_MAXINSNS)
    965 		return (EINVAL);
    966 
    967 	size = flen * sizeof(*fp->bf_insns);
    968 	fcode = malloc(size, M_DEVBUF, M_WAITOK);
    969 	if (copyin(fp->bf_insns, fcode, size) == 0 &&
    970 	    bpf_validate(fcode, (int)flen)) {
    971 		s = splnet();
    972 		d->bd_filter = fcode;
    973 		reset_d(d);
    974 		splx(s);
    975 		if (old != 0)
    976 			free(old, M_DEVBUF);
    977 
    978 		return (0);
    979 	}
    980 	free(fcode, M_DEVBUF);
    981 	return (EINVAL);
    982 }
    983 
    984 /*
    985  * Detach a file from its current interface (if attached at all) and attach
    986  * to the interface indicated by the name stored in ifr.
    987  * Return an errno or 0.
    988  */
    989 static int
    990 bpf_setif(struct bpf_d *d, struct ifreq *ifr)
    991 {
    992 	struct bpf_if *bp;
    993 	char *cp;
    994 	int unit_seen, i, s, error;
    995 
    996 	/*
    997 	 * Make sure the provided name has a unit number, and default
    998 	 * it to '0' if not specified.
    999 	 * XXX This is ugly ... do this differently?
   1000 	 */
   1001 	unit_seen = 0;
   1002 	cp = ifr->ifr_name;
   1003 	cp[sizeof(ifr->ifr_name) - 1] = '\0';	/* sanity */
   1004 	while (*cp++)
   1005 		if (*cp >= '0' && *cp <= '9')
   1006 			unit_seen = 1;
   1007 	if (!unit_seen) {
   1008 		/* Make sure to leave room for the '\0'. */
   1009 		for (i = 0; i < (IFNAMSIZ - 1); ++i) {
   1010 			if ((ifr->ifr_name[i] >= 'a' &&
   1011 			     ifr->ifr_name[i] <= 'z') ||
   1012 			    (ifr->ifr_name[i] >= 'A' &&
   1013 			     ifr->ifr_name[i] <= 'Z'))
   1014 				continue;
   1015 			ifr->ifr_name[i] = '0';
   1016 		}
   1017 	}
   1018 
   1019 	/*
   1020 	 * Look through attached interfaces for the named one.
   1021 	 */
   1022 	for (bp = bpf_iflist; bp != 0; bp = bp->bif_next) {
   1023 		struct ifnet *ifp = bp->bif_ifp;
   1024 
   1025 		if (ifp == 0 ||
   1026 		    strcmp(ifp->if_xname, ifr->ifr_name) != 0)
   1027 			continue;
   1028 		/* skip additional entry */
   1029 		if (bp->bif_driverp != (struct bpf_if **)&ifp->if_bpf)
   1030 			continue;
   1031 		/*
   1032 		 * We found the requested interface.
   1033 		 * Allocate the packet buffers if we need to.
   1034 		 * If we're already attached to requested interface,
   1035 		 * just flush the buffer.
   1036 		 */
   1037 		if (d->bd_sbuf == 0) {
   1038 			error = bpf_allocbufs(d);
   1039 			if (error != 0)
   1040 				return (error);
   1041 		}
   1042 		s = splnet();
   1043 		if (bp != d->bd_bif) {
   1044 			if (d->bd_bif)
   1045 				/*
   1046 				 * Detach if attached to something else.
   1047 				 */
   1048 				bpf_detachd(d);
   1049 
   1050 			bpf_attachd(d, bp);
   1051 		}
   1052 		reset_d(d);
   1053 		splx(s);
   1054 		return (0);
   1055 	}
   1056 	/* Not found. */
   1057 	return (ENXIO);
   1058 }
   1059 
   1060 /*
   1061  * Copy the interface name to the ifreq.
   1062  */
   1063 static void
   1064 bpf_ifname(struct ifnet *ifp, struct ifreq *ifr)
   1065 {
   1066 	memcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
   1067 }
   1068 
   1069 /*
   1070  * Support for poll() system call
   1071  *
   1072  * Return true iff the specific operation will not block indefinitely - with
   1073  * the assumption that it is safe to positively acknowledge a request for the
   1074  * ability to write to the BPF device.
   1075  * Otherwise, return false but make a note that a selwakeup() must be done.
   1076  */
   1077 int
   1078 bpfpoll(dev_t dev, int events, struct proc *p)
   1079 {
   1080 	struct bpf_d *d = &bpf_dtab[minor(dev)];
   1081 	int s = splnet();
   1082 	int revents;
   1083 
   1084 	revents = events & (POLLOUT | POLLWRNORM);
   1085 	if (events & (POLLIN | POLLRDNORM)) {
   1086 		/*
   1087 		 * An imitation of the FIONREAD ioctl code.
   1088 		 */
   1089 		if ((d->bd_hlen != 0) ||
   1090 		    (d->bd_immediate && d->bd_slen != 0)) {
   1091 			revents |= events & (POLLIN | POLLRDNORM);
   1092 		} else if (d->bd_state == BPF_TIMED_OUT) {
   1093 			if (d->bd_slen != 0)
   1094 				revents |= events & (POLLIN | POLLRDNORM);
   1095 			else
   1096 				revents |= events & POLLIN;
   1097 		} else {
   1098 			selrecord(p, &d->bd_sel);
   1099 			/* Start the read timeout if necessary */
   1100 			if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
   1101 				callout_reset(&d->bd_callout, d->bd_rtout,
   1102 					      bpf_timed_out, d);
   1103 				d->bd_state = BPF_WAITING;
   1104 			}
   1105 		}
   1106 	}
   1107 
   1108 	splx(s);
   1109 	return (revents);
   1110 }
   1111 
   1112 static void
   1113 filt_bpfrdetach(struct knote *kn)
   1114 {
   1115 	struct bpf_d *d = kn->kn_hook;
   1116 	int s;
   1117 
   1118 	s = splnet();
   1119 	SLIST_REMOVE(&d->bd_sel.sel_klist, kn, knote, kn_selnext);
   1120 	splx(s);
   1121 }
   1122 
   1123 static int
   1124 filt_bpfread(struct knote *kn, long hint)
   1125 {
   1126 	struct bpf_d *d = kn->kn_hook;
   1127 
   1128 	kn->kn_data = d->bd_hlen;
   1129 	if (d->bd_immediate)
   1130 		kn->kn_data += d->bd_slen;
   1131 	return (kn->kn_data > 0);
   1132 }
   1133 
   1134 static const struct filterops bpfread_filtops =
   1135 	{ 1, NULL, filt_bpfrdetach, filt_bpfread };
   1136 
   1137 int
   1138 bpfkqfilter(dev_t dev, struct knote *kn)
   1139 {
   1140 	struct bpf_d *d = &bpf_dtab[minor(dev)];
   1141 	struct klist *klist;
   1142 	int s;
   1143 
   1144 	switch (kn->kn_filter) {
   1145 	case EVFILT_READ:
   1146 		klist = &d->bd_sel.sel_klist;
   1147 		kn->kn_fop = &bpfread_filtops;
   1148 		break;
   1149 
   1150 	default:
   1151 		return (1);
   1152 	}
   1153 
   1154 	kn->kn_hook = d;
   1155 
   1156 	s = splnet();
   1157 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
   1158 	splx(s);
   1159 
   1160 	return (0);
   1161 }
   1162 
   1163 /*
   1164  * Incoming linkage from device drivers.  Process the packet pkt, of length
   1165  * pktlen, which is stored in a contiguous buffer.  The packet is parsed
   1166  * by each process' filter, and if accepted, stashed into the corresponding
   1167  * buffer.
   1168  */
   1169 void
   1170 bpf_tap(void *arg, u_char *pkt, u_int pktlen)
   1171 {
   1172 	struct bpf_if *bp;
   1173 	struct bpf_d *d;
   1174 	u_int slen;
   1175 	/*
   1176 	 * Note that the ipl does not have to be raised at this point.
   1177 	 * The only problem that could arise here is that if two different
   1178 	 * interfaces shared any data.  This is not the case.
   1179 	 */
   1180 	bp = arg;
   1181 	for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
   1182 		++d->bd_rcount;
   1183 		slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen);
   1184 		if (slen != 0)
   1185 			catchpacket(d, pkt, pktlen, slen, memcpy);
   1186 	}
   1187 }
   1188 
   1189 /*
   1190  * Copy data from an mbuf chain into a buffer.  This code is derived
   1191  * from m_copydata in sys/uipc_mbuf.c.
   1192  */
   1193 static void *
   1194 bpf_mcpy(void *dst_arg, const void *src_arg, size_t len)
   1195 {
   1196 	const struct mbuf *m;
   1197 	u_int count;
   1198 	u_char *dst;
   1199 
   1200 	m = src_arg;
   1201 	dst = dst_arg;
   1202 	while (len > 0) {
   1203 		if (m == 0)
   1204 			panic("bpf_mcpy");
   1205 		count = min(m->m_len, len);
   1206 		memcpy(dst, mtod(m, void *), count);
   1207 		m = m->m_next;
   1208 		dst += count;
   1209 		len -= count;
   1210 	}
   1211 	return (dst_arg);
   1212 }
   1213 
   1214 /*
   1215  * Dispatch a packet to all the listeners on interface bp.
   1216  *
   1217  * marg    pointer to the packet, either a data buffer or an mbuf chain
   1218  * buflen  buffer length, if marg is a data buffer
   1219  * cpfn    a function that can copy marg into the listener's buffer
   1220  * pktlen  length of the packet
   1221  * rcvif   either NULL or the interface the packet came in on.
   1222  */
   1223 static __inline void
   1224 bpf_deliver(struct bpf_if *bp, void *(*cpfn)(void *, const void *, size_t),
   1225     void *marg, u_int pktlen, u_int buflen, struct ifnet *rcvif)
   1226 {
   1227 	u_int slen;
   1228 	struct bpf_d *d;
   1229 
   1230 	for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
   1231 		if (!d->bd_seesent && (rcvif == NULL))
   1232 			continue;
   1233 		++d->bd_rcount;
   1234 		slen = bpf_filter(d->bd_filter, marg, pktlen, buflen);
   1235 		if (slen != 0)
   1236 			catchpacket(d, marg, pktlen, slen, cpfn);
   1237 	}
   1238 }
   1239 
   1240 /*
   1241  * Incoming linkage from device drivers, when the head of the packet is in
   1242  * a buffer, and the tail is in an mbuf chain.
   1243  */
   1244 void
   1245 bpf_mtap2(void *arg, void *data, u_int dlen, struct mbuf *m)
   1246 {
   1247 	struct bpf_if *bp = arg;
   1248 	u_int pktlen;
   1249 	struct mbuf mb;
   1250 
   1251 	pktlen = m_length(m) + dlen;
   1252 
   1253 	/*
   1254 	 * Craft on-stack mbuf suitable for passing to bpf_filter.
   1255 	 * Note that we cut corners here; we only setup what's
   1256 	 * absolutely needed--this mbuf should never go anywhere else.
   1257 	 */
   1258 	(void)memset(&mb, 0, sizeof(mb));
   1259 	mb.m_next = m;
   1260 	mb.m_data = data;
   1261 	mb.m_len = dlen;
   1262 
   1263 	bpf_deliver(bp, bpf_mcpy, &mb, pktlen, 0, m->m_pkthdr.rcvif);
   1264 }
   1265 
   1266 /*
   1267  * Incoming linkage from device drivers, when packet is in an mbuf chain.
   1268  */
   1269 void
   1270 bpf_mtap(void *arg, struct mbuf *m)
   1271 {
   1272 	void *(*cpfn)(void *, const void *, size_t);
   1273 	struct bpf_if *bp = arg;
   1274 	u_int pktlen, buflen;
   1275 	void *marg;
   1276 
   1277 	pktlen = m_length(m);
   1278 
   1279 	if (pktlen == m->m_len) {
   1280 		cpfn = memcpy;
   1281 		marg = mtod(m, void *);
   1282 		buflen = pktlen;
   1283 	} else {
   1284 		cpfn = bpf_mcpy;
   1285 		marg = m;
   1286 		buflen = 0;
   1287 	}
   1288 
   1289 	bpf_deliver(bp, cpfn, marg, pktlen, buflen, m->m_pkthdr.rcvif);
   1290 }
   1291 
   1292 /*
   1293  * We need to prepend the address family as
   1294  * a four byte field.  Cons up a dummy header
   1295  * to pacify bpf.  This is safe because bpf
   1296  * will only read from the mbuf (i.e., it won't
   1297  * try to free it or keep a pointer a to it).
   1298  */
   1299 void
   1300 bpf_mtap_af(void *arg, u_int32_t af, struct mbuf *m)
   1301 {
   1302 	struct mbuf m0;
   1303 
   1304 	m0.m_flags = 0;
   1305 	m0.m_next = m;
   1306 	m0.m_len = 4;
   1307 	m0.m_data = (char *)&af;
   1308 
   1309 	bpf_mtap(arg, &m0);
   1310 }
   1311 
   1312 void
   1313 bpf_mtap_et(void *arg, u_int16_t et, struct mbuf *m)
   1314 {
   1315 	struct mbuf m0;
   1316 
   1317 	m0.m_flags = 0;
   1318 	m0.m_next = m;
   1319 	m0.m_len = 14;
   1320 	m0.m_data = m0.m_dat;
   1321 
   1322 	((u_int32_t *)m0.m_data)[0] = 0;
   1323 	((u_int32_t *)m0.m_data)[1] = 0;
   1324 	((u_int32_t *)m0.m_data)[2] = 0;
   1325 	((u_int16_t *)m0.m_data)[6] = et;
   1326 
   1327 	bpf_mtap(arg, &m0);
   1328 }
   1329 
   1330 #if NSL > 0 || NSTRIP > 0
   1331 /*
   1332  * Put the SLIP pseudo-"link header" in place.
   1333  * Note this M_PREPEND() should never fail,
   1334  * swince we know we always have enough space
   1335  * in the input buffer.
   1336  */
   1337 void
   1338 bpf_mtap_sl_in(void *arg, u_char *chdr, struct mbuf **m)
   1339 {
   1340 	int s;
   1341 	u_char *hp;
   1342 
   1343 	M_PREPEND(*m, SLIP_HDRLEN, M_DONTWAIT);
   1344 	if (*m == NULL)
   1345 		return;
   1346 
   1347 	hp = mtod(*m, u_char *);
   1348 	hp[SLX_DIR] = SLIPDIR_IN;
   1349 	(void)memcpy(&hp[SLX_CHDR], chdr, CHDR_LEN);
   1350 
   1351 	s = splnet();
   1352 	bpf_mtap(arg, *m);
   1353 	splx(s);
   1354 
   1355 	m_adj(*m, SLIP_HDRLEN);
   1356 }
   1357 
   1358 /*
   1359  * Put the SLIP pseudo-"link header" in
   1360  * place.  The compressed header is now
   1361  * at the beginning of the mbuf.
   1362  */
   1363 void
   1364 bpf_mtap_sl_out(void *arg, u_char *chdr, struct mbuf *m)
   1365 {
   1366 	struct mbuf m0;
   1367 	u_char *hp;
   1368 	int s;
   1369 
   1370 	m0.m_flags = 0;
   1371 	m0.m_next = m;
   1372 	m0.m_data = m0.m_dat;
   1373 	m0.m_len = SLIP_HDRLEN;
   1374 
   1375 	hp = mtod(&m0, u_char *);
   1376 
   1377 	hp[SLX_DIR] = SLIPDIR_OUT;
   1378 	(void)memcpy(&hp[SLX_CHDR], chdr, CHDR_LEN);
   1379 
   1380 	s = splnet();
   1381 	bpf_mtap(arg, &m0);
   1382 	splx(s);
   1383 	m_freem(m);
   1384 }
   1385 #endif
   1386 
   1387 /*
   1388  * Move the packet data from interface memory (pkt) into the
   1389  * store buffer.  Return 1 if it's time to wakeup a listener (buffer full),
   1390  * otherwise 0.  "copy" is the routine called to do the actual data
   1391  * transfer.  memcpy is passed in to copy contiguous chunks, while
   1392  * bpf_mcpy is passed in to copy mbuf chains.  In the latter case,
   1393  * pkt is really an mbuf.
   1394  */
   1395 static void
   1396 catchpacket(struct bpf_d *d, u_char *pkt, u_int pktlen, u_int snaplen,
   1397     void *(*cpfn)(void *, const void *, size_t))
   1398 {
   1399 	struct bpf_hdr *hp;
   1400 	int totlen, curlen;
   1401 	int hdrlen = d->bd_bif->bif_hdrlen;
   1402 
   1403 	++d->bd_ccount;
   1404 	/*
   1405 	 * Figure out how many bytes to move.  If the packet is
   1406 	 * greater or equal to the snapshot length, transfer that
   1407 	 * much.  Otherwise, transfer the whole packet (unless
   1408 	 * we hit the buffer size limit).
   1409 	 */
   1410 	totlen = hdrlen + min(snaplen, pktlen);
   1411 	if (totlen > d->bd_bufsize)
   1412 		totlen = d->bd_bufsize;
   1413 
   1414 	/*
   1415 	 * Round up the end of the previous packet to the next longword.
   1416 	 */
   1417 	curlen = BPF_WORDALIGN(d->bd_slen);
   1418 	if (curlen + totlen > d->bd_bufsize) {
   1419 		/*
   1420 		 * This packet will overflow the storage buffer.
   1421 		 * Rotate the buffers if we can, then wakeup any
   1422 		 * pending reads.
   1423 		 */
   1424 		if (d->bd_fbuf == 0) {
   1425 			/*
   1426 			 * We haven't completed the previous read yet,
   1427 			 * so drop the packet.
   1428 			 */
   1429 			++d->bd_dcount;
   1430 			return;
   1431 		}
   1432 		ROTATE_BUFFERS(d);
   1433 		bpf_wakeup(d);
   1434 		curlen = 0;
   1435 	}
   1436 
   1437 	/*
   1438 	 * Append the bpf header.
   1439 	 */
   1440 	hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
   1441 	microtime(&hp->bh_tstamp);
   1442 	hp->bh_datalen = pktlen;
   1443 	hp->bh_hdrlen = hdrlen;
   1444 	/*
   1445 	 * Copy the packet data into the store buffer and update its length.
   1446 	 */
   1447 	(*cpfn)((u_char *)hp + hdrlen, pkt, (hp->bh_caplen = totlen - hdrlen));
   1448 	d->bd_slen = curlen + totlen;
   1449 
   1450 	/*
   1451 	 * Call bpf_wakeup after bd_slen has been updated so that kevent(2)
   1452 	 * will cause filt_bpfread() to be called with it adjusted.
   1453 	 */
   1454 	if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT)
   1455 		/*
   1456 		 * Immediate mode is set, or the read timeout has
   1457 		 * already expired during a select call.  A packet
   1458 		 * arrived, so the reader should be woken up.
   1459 		 */
   1460 		bpf_wakeup(d);
   1461 }
   1462 
   1463 /*
   1464  * Initialize all nonzero fields of a descriptor.
   1465  */
   1466 static int
   1467 bpf_allocbufs(struct bpf_d *d)
   1468 {
   1469 
   1470 	d->bd_fbuf = malloc(d->bd_bufsize, M_DEVBUF, M_NOWAIT);
   1471 	if (!d->bd_fbuf)
   1472 		return (ENOBUFS);
   1473 	d->bd_sbuf = malloc(d->bd_bufsize, M_DEVBUF, M_NOWAIT);
   1474 	if (!d->bd_sbuf) {
   1475 		free(d->bd_fbuf, M_DEVBUF);
   1476 		return (ENOBUFS);
   1477 	}
   1478 	d->bd_slen = 0;
   1479 	d->bd_hlen = 0;
   1480 	return (0);
   1481 }
   1482 
   1483 /*
   1484  * Free buffers currently in use by a descriptor.
   1485  * Called on close.
   1486  */
   1487 static void
   1488 bpf_freed(struct bpf_d *d)
   1489 {
   1490 	/*
   1491 	 * We don't need to lock out interrupts since this descriptor has
   1492 	 * been detached from its interface and it yet hasn't been marked
   1493 	 * free.
   1494 	 */
   1495 	if (d->bd_sbuf != 0) {
   1496 		free(d->bd_sbuf, M_DEVBUF);
   1497 		if (d->bd_hbuf != 0)
   1498 			free(d->bd_hbuf, M_DEVBUF);
   1499 		if (d->bd_fbuf != 0)
   1500 			free(d->bd_fbuf, M_DEVBUF);
   1501 	}
   1502 	if (d->bd_filter)
   1503 		free(d->bd_filter, M_DEVBUF);
   1504 
   1505 	D_MARKFREE(d);
   1506 }
   1507 
   1508 /*
   1509  * Attach an interface to bpf.  dlt is the link layer type; hdrlen is the
   1510  * fixed size of the link header (variable length headers not yet supported).
   1511  */
   1512 void
   1513 bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen)
   1514 {
   1515 
   1516 	bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
   1517 }
   1518 
   1519 /*
   1520  * Attach additional dlt for a interface to bpf.  dlt is the link layer type;
   1521  * hdrlen is the fixed size of the link header for the specified dlt
   1522  * (variable length headers not yet supported).
   1523  */
   1524 void
   1525 bpfattach2(struct ifnet *ifp, u_int dlt, u_int hdrlen, void *driverp)
   1526 {
   1527 	struct bpf_if *bp;
   1528 	bp = malloc(sizeof(*bp), M_DEVBUF, M_DONTWAIT);
   1529 	if (bp == 0)
   1530 		panic("bpfattach");
   1531 
   1532 	bp->bif_dlist = 0;
   1533 	bp->bif_driverp = driverp;
   1534 	bp->bif_ifp = ifp;
   1535 	bp->bif_dlt = dlt;
   1536 
   1537 	bp->bif_next = bpf_iflist;
   1538 	bpf_iflist = bp;
   1539 
   1540 	*bp->bif_driverp = 0;
   1541 
   1542 	/*
   1543 	 * Compute the length of the bpf header.  This is not necessarily
   1544 	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
   1545 	 * that the network layer header begins on a longword boundary (for
   1546 	 * performance reasons and to alleviate alignment restrictions).
   1547 	 */
   1548 	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
   1549 
   1550 #if 0
   1551 	printf("bpf: %s attached\n", ifp->if_xname);
   1552 #endif
   1553 }
   1554 
   1555 /*
   1556  * Remove an interface from bpf.
   1557  */
   1558 void
   1559 bpfdetach(struct ifnet *ifp)
   1560 {
   1561 	struct bpf_if *bp, **pbp;
   1562 	struct bpf_d *d;
   1563 	int i, s, cmaj;
   1564 
   1565 	/* locate the major number */
   1566 	cmaj = cdevsw_lookup_major(&bpf_cdevsw);
   1567 
   1568 	/* Nuke the vnodes for any open instances */
   1569 	for (i = 0; i < NBPFILTER; ++i) {
   1570 		d = &bpf_dtab[i];
   1571 		if (!D_ISFREE(d) && d->bd_bif != NULL &&
   1572 		    d->bd_bif->bif_ifp == ifp) {
   1573 			/*
   1574 			 * Detach the descriptor from an interface now.
   1575 			 * It will be free'ed later by close routine.
   1576 			 */
   1577 			s = splnet();
   1578 			d->bd_promisc = 0;	/* we can't touch device. */
   1579 			bpf_detachd(d);
   1580 			splx(s);
   1581 			vdevgone(cmaj, i, i, VCHR);
   1582 		}
   1583 	}
   1584 
   1585   again:
   1586 	for (bp = bpf_iflist, pbp = &bpf_iflist;
   1587 	     bp != NULL; pbp = &bp->bif_next, bp = bp->bif_next) {
   1588 		if (bp->bif_ifp == ifp) {
   1589 			*pbp = bp->bif_next;
   1590 			free(bp, M_DEVBUF);
   1591 			goto again;
   1592 		}
   1593 	}
   1594 }
   1595 
   1596 /*
   1597  * Change the data link type of a interface.
   1598  */
   1599 void
   1600 bpf_change_type(struct ifnet *ifp, u_int dlt, u_int hdrlen)
   1601 {
   1602 	struct bpf_if *bp;
   1603 
   1604 	for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
   1605 		if (bp->bif_driverp == (struct bpf_if **)&ifp->if_bpf)
   1606 			break;
   1607 	}
   1608 	if (bp == NULL)
   1609 		panic("bpf_change_type");
   1610 
   1611 	bp->bif_dlt = dlt;
   1612 
   1613 	/*
   1614 	 * Compute the length of the bpf header.  This is not necessarily
   1615 	 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
   1616 	 * that the network layer header begins on a longword boundary (for
   1617 	 * performance reasons and to alleviate alignment restrictions).
   1618 	 */
   1619 	bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
   1620 }
   1621 
   1622 /*
   1623  * Get a list of available data link type of the interface.
   1624  */
   1625 static int
   1626 bpf_getdltlist(struct bpf_d *d, struct bpf_dltlist *bfl)
   1627 {
   1628 	int n, error;
   1629 	struct ifnet *ifp;
   1630 	struct bpf_if *bp;
   1631 
   1632 	ifp = d->bd_bif->bif_ifp;
   1633 	n = 0;
   1634 	error = 0;
   1635 	for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
   1636 		if (bp->bif_ifp != ifp)
   1637 			continue;
   1638 		if (bfl->bfl_list != NULL) {
   1639 			if (n >= bfl->bfl_len)
   1640 				return ENOMEM;
   1641 			error = copyout(&bp->bif_dlt,
   1642 			    bfl->bfl_list + n, sizeof(u_int));
   1643 		}
   1644 		n++;
   1645 	}
   1646 	bfl->bfl_len = n;
   1647 	return error;
   1648 }
   1649 
   1650 /*
   1651  * Set the data link type of a BPF instance.
   1652  */
   1653 static int
   1654 bpf_setdlt(struct bpf_d *d, u_int dlt)
   1655 {
   1656 	int s, error, opromisc;
   1657 	struct ifnet *ifp;
   1658 	struct bpf_if *bp;
   1659 
   1660 	if (d->bd_bif->bif_dlt == dlt)
   1661 		return 0;
   1662 	ifp = d->bd_bif->bif_ifp;
   1663 	for (bp = bpf_iflist; bp != NULL; bp = bp->bif_next) {
   1664 		if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
   1665 			break;
   1666 	}
   1667 	if (bp == NULL)
   1668 		return EINVAL;
   1669 	s = splnet();
   1670 	opromisc = d->bd_promisc;
   1671 	bpf_detachd(d);
   1672 	bpf_attachd(d, bp);
   1673 	reset_d(d);
   1674 	if (opromisc) {
   1675 		error = ifpromisc(bp->bif_ifp, 1);
   1676 		if (error)
   1677 			printf("%s: bpf_setdlt: ifpromisc failed (%d)\n",
   1678 			    bp->bif_ifp->if_xname, error);
   1679 		else
   1680 			d->bd_promisc = 1;
   1681 	}
   1682 	splx(s);
   1683 	return 0;
   1684 }
   1685 
   1686 static int
   1687 sysctl_net_bpf_maxbufsize(SYSCTLFN_ARGS)
   1688 {
   1689 	int newsize, error;
   1690 	struct sysctlnode node;
   1691 
   1692 	node = *rnode;
   1693 	node.sysctl_data = &newsize;
   1694 	newsize = bpf_maxbufsize;
   1695 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1696 	if (error || newp == NULL)
   1697 		return (error);
   1698 
   1699 	if (newsize < BPF_MINBUFSIZE || newsize > BPF_MAXBUFSIZE)
   1700 		return (EINVAL);
   1701 
   1702 	bpf_maxbufsize = newsize;
   1703 
   1704 	return (0);
   1705 }
   1706 
   1707 SYSCTL_SETUP(sysctl_net_bfp_setup, "sysctl net.bpf subtree setup")
   1708 {
   1709 	struct sysctlnode *node;
   1710 
   1711 	sysctl_createv(clog, 0, NULL, NULL,
   1712 		       CTLFLAG_PERMANENT,
   1713 		       CTLTYPE_NODE, "net", NULL,
   1714 		       NULL, 0, NULL, 0,
   1715 		       CTL_NET, CTL_EOL);
   1716 
   1717 	node = NULL;
   1718 	sysctl_createv(clog, 0, NULL, &node,
   1719 		       CTLFLAG_PERMANENT,
   1720 		       CTLTYPE_NODE, "bpf",
   1721 		       SYSCTL_DESCR("BPF options"),
   1722 		       NULL, 0, NULL, 0,
   1723 		       CTL_NET, CTL_CREATE, CTL_EOL);
   1724 	if (node != NULL)
   1725 		sysctl_createv(clog, 0, NULL, NULL,
   1726 			CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1727 			CTLTYPE_INT, "maxbufsize",
   1728 			SYSCTL_DESCR("Maximum size for data capture buffer"),
   1729 			sysctl_net_bpf_maxbufsize, 0, &bpf_maxbufsize, 0,
   1730 			CTL_NET, node->sysctl_num, CTL_CREATE, CTL_EOL);
   1731 }
   1732