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