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