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

Cache object: 8f38d50361b2d97bcccd5be71d731e23


[ 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.