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