The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/net/bpf.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 6527b8250d99f2a3b1e64503e3eca404


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.