home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / os / bsdss4.tz / bsdss4 / bsdss / server / net / bpf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-22  |  25.0 KB  |  1,140 lines

  1. /* 
  2.  * Mach Operating System
  3.  * Copyright (c) 1992 Carnegie Mellon University
  4.  * All Rights Reserved.
  5.  * 
  6.  * Permission to use, copy, modify and distribute this software and its
  7.  * documentation is hereby granted, provided that both the copyright
  8.  * notice and this permission notice appear in all copies of the
  9.  * software, derivative works or modified versions, and any portions
  10.  * thereof, and that both notices appear in supporting documentation.
  11.  * 
  12.  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  13.  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
  14.  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  15.  * 
  16.  * Carnegie Mellon requests users of this software to return to
  17.  * 
  18.  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
  19.  *  School of Computer Science
  20.  *  Carnegie Mellon University
  21.  *  Pittsburgh PA 15213-3890
  22.  * 
  23.  * any improvements or extensions that they make and grant Carnegie Mellon 
  24.  * the rights to redistribute these changes.
  25.  */
  26. /*
  27.  * HISTORY
  28.  * $Log:    bpf.c,v $
  29.  * Revision 2.1  92/04/21  17:14:08  rwd
  30.  * BSDSS
  31.  * 
  32.  *
  33.  */
  34.  
  35. /*-
  36.  * Copyright (c) 1991 The Regents of the University of California.
  37.  * All rights reserved.
  38.  *
  39.  * This code is derived from the Stanford/CMU enet packet filter,
  40.  * (net/enet.c) distributed as part of 4.3BSD, and code contributed
  41.  * to Berkeley by Steven McCanne of Lawrence Berkeley Laboratory.
  42.  *
  43.  * Redistribution and use in source and binary forms, with or without
  44.  * modification, are permitted provided that the following conditions
  45.  * are met:
  46.  * 1. Redistributions of source code must retain the above copyright
  47.  *    notice, this list of conditions and the following disclaimer.
  48.  * 2. Redistributions in binary form must reproduce the above copyright
  49.  *    notice, this list of conditions and the following disclaimer in the
  50.  *    documentation and/or other materials provided with the distribution.
  51.  * 3. All advertising materials mentioning features or use of this software
  52.  *    must display the following acknowledgement:
  53.  *    This product includes software developed by the University of
  54.  *    California, Berkeley and its contributors.
  55.  * 4. Neither the name of the University nor the names of its contributors
  56.  *    may be used to endorse or promote products derived from this software
  57.  *    without specific prior written permission.
  58.  *
  59.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  60.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  61.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  62.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  63.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  64.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  65.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  66.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  67.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  68.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  69.  * SUCH DAMAGE.
  70.  *
  71.  *    @(#)bpf.c    7.4 (Berkeley) 6/17/91
  72.  *
  73.  * static char rcsid[] =
  74.  * "$Header: /afs/cs.cmu.edu/project/mach/mach3/rcs/bsdss/server/net/bpf.c,v 2.1 92/04/21 17:14:08 rwd Exp $";
  75.  */
  76.  
  77. #include <sys/bpfilter.h>
  78.  
  79. #if (NBPFILTER > 0)
  80.  
  81. #include <sys/param.h>
  82. #include <sys/systm.h>
  83. #include <sys/mbuf.h>
  84. #include <sys/buf.h>
  85. #include <sys/dir.h>
  86. #include <sys/proc.h>
  87. #include <sys/user.h>
  88. #include <sys/ioctl.h>
  89. #include <sys/map.h>
  90.  
  91. #include <sys/file.h>
  92. #ifdef sparc
  93. #include <sys/stream.h>
  94. #endif
  95. #include <sys/tty.h>
  96. #include <sys/uio.h>
  97.  
  98. #include <sys/protosw.h>
  99. #include <sys/socket.h>
  100. #include <net/if.h>
  101.  
  102. #include <net/bpf.h>
  103. #include <net/bpfdesc.h>
  104.  
  105. #include <sys/errno.h>
  106.  
  107. #include <netinet/in.h>
  108. #include <netinet/if_ether.h>
  109. #include <sys/kernel.h>
  110.  
  111. #define PRINET  26            /* interruptible */
  112.  
  113. /*
  114.  * The default read buffer size is patchable.
  115.  */
  116. int bpf_bufsize = MCLBYTES;
  117.  
  118. /*
  119.  *  bpf_iflist is the list of interfaces; each corresponds to an ifnet
  120.  *  bpf_dtab holds the descriptors, indexed by minor device #
  121.  *
  122.  * We really don't need NBPFILTER bpf_if entries, but this eliminates
  123.  * the need to account for all possible drivers here.
  124.  * This problem will go away when these structures are allocated dynamically.
  125.  */
  126. static struct bpf_if     *bpf_iflist;
  127. static struct bpf_d    bpf_dtab[NBPFILTER];
  128.  
  129. static void    bpf_ifname();
  130. static void    catchpacket();
  131. static int    bpf_setif();
  132. static int    bpf_initd();
  133.  
  134. static int
  135. bpf_movein(uio, linktype, mp, sockp)
  136.     register struct uio *uio;
  137.     int linktype;
  138.     register struct mbuf **mp;
  139.     register struct sockaddr *sockp;
  140. {
  141.     struct mbuf *m;
  142.     int error;
  143.     int len;
  144.     int hlen;
  145.  
  146.     /*
  147.      * Build a sockaddr based on the data link layer type.
  148.      * We do this at this level because the ethernet header
  149.      * is copied directly into the data field of the sockaddr.
  150.      * In the case of SLIP, there is no header and the packet
  151.      * is forwarded as is.
  152.      * Also, we are careful to leave room at the front of the mbuf
  153.      * for the link level header.
  154.      */
  155.     switch (linktype) {
  156.     case DLT_SLIP:
  157.         sockp->sa_family = AF_INET;
  158.         hlen = 0;
  159.         break;
  160.  
  161.     case DLT_EN10MB:
  162.         sockp->sa_family = AF_UNSPEC;
  163.         /* XXX Would MAXLINKHDR be better? */
  164.         hlen = sizeof(struct ether_header);
  165.         break;
  166.  
  167.        case DLT_FDDI:
  168.         sockp->sa_family = AF_UNSPEC;
  169.         /* XXX 4(FORMAC)+6(dst)+6(src)+3(LLC)+5(SNAP) */
  170.         hlen = 24;
  171.         break;
  172.  
  173.     default:
  174.         return (EIO);
  175.     }
  176.  
  177.     len = uio->uio_resid;
  178.     if ((unsigned)len > MCLBYTES)
  179.         return (EIO);
  180.  
  181.     MGET(m, M_WAIT, MT_DATA);
  182.     if (m == 0)
  183.         return (ENOBUFS);
  184.     if (len > MLEN) {
  185.         MCLGET(m, M_WAIT);
  186.         if ((m->m_flags & M_EXT) == 0) {
  187.             error = ENOBUFS;
  188.             goto bad;
  189.         }
  190.     }
  191.     m->m_len = len;
  192.     *mp = m;
  193.     /*
  194.      * Make room for link header.
  195.      */
  196.     if (hlen) {
  197.         m->m_len -= hlen;
  198.         m->m_data += hlen; /* XXX */
  199.  
  200.         error = uiomove((caddr_t)sockp->sa_data, hlen, uio);
  201.         if (error)
  202.             goto bad;
  203.     }
  204.     error = uiomove(mtod(m, caddr_t), len - hlen, uio);
  205.     if (!error) 
  206.         return (0);
  207.  bad:
  208.     m_freem(m);
  209.     return (error);
  210. }
  211.  
  212. /*
  213.  * Attach 'd' to the bpf interface 'bp', i.e. make 'd' listen on 'bp'.
  214.  * Must be called at splimp.
  215.  */
  216. static void
  217. bpf_attachd(d, bp)
  218.     struct bpf_d *d;
  219.     struct bpf_if *bp;
  220. {
  221.     /* Point d at bp. */
  222.     d->bd_bif = bp;
  223.  
  224.     /* Add d to bp's list of listeners. */
  225.     d->bd_next = bp->bif_dlist;
  226.     bp->bif_dlist = d;
  227.  
  228.     /*
  229.      * Let the driver know we're here (if it doesn't already).
  230.      */
  231.     *bp->bif_driverp = bp;
  232. }
  233.  
  234. static void
  235. bpf_detachd(d)
  236.     struct bpf_d *d;
  237. {
  238.     struct bpf_d **p;
  239.     struct bpf_if *bp;
  240.  
  241.     bp = d->bd_bif;
  242.     /*
  243.      * Check if this descriptor had requested promiscuous mode.
  244.      * If so, turn it off.
  245.      */
  246.     if (d->bd_promisc) {
  247.         d->bd_promisc = 0;
  248.         if (ifpromisc(bp->bif_ifp, 0))
  249.             /*
  250.              * Something is really wrong if we were able to put
  251.              * the driver into promiscuous mode, but can't
  252.              * take it out.
  253.              */
  254.             panic("bpf_detachd: ifpromisc failed");
  255.     }
  256.     /* Remove 'd' from the interface's descriptor list. */
  257.     p = &bp->bif_dlist;
  258.     while (*p != d) {
  259.         p = &(*p)->bd_next;
  260.         if (*p == 0)
  261.             panic("bpf_detachd: descriptor not in list");
  262.     }
  263.     *p = (*p)->bd_next;
  264.     if (bp->bif_dlist == 0)
  265.         /*
  266.          * Let the driver know that there are no more listeners.
  267.          */
  268.         *d->bd_bif->bif_driverp = 0;
  269.     d->bd_bif = 0;
  270. }
  271.  
  272.  
  273. /*
  274.  * Mark a descriptor free by making it point to itself. 
  275.  * This is probably cheaper than marking with a constant since
  276.  * the address should be in a register anyway.
  277.  */
  278. #define D_ISFREE(d) ((d) == (d)->bd_next)
  279. #define D_MARKFREE(d) ((d)->bd_next = (d))
  280. #define D_MARKUSED(d) ((d)->bd_next = 0)
  281.  
  282. /*
  283.  *  bpfopen - open ethernet device
  284.  *
  285.  *  Errors:    ENXIO    - illegal minor device number
  286.  *        EBUSY    - too many files open
  287.  */
  288. /* ARGSUSED */
  289. int
  290. bpfopen(dev, flag)
  291.     dev_t dev;
  292.     int flag;
  293. {
  294.     int error, s;
  295.     register struct bpf_d *d;
  296.     
  297.     if (minor(dev) >= NBPFILTER)
  298.         return (ENXIO);
  299.  
  300.     /*
  301.      * Each minor can be opened by only one process.  If the requested
  302.      * minor is in use, return EBUSY.
  303.      */
  304.     s = splimp();
  305.     d = &bpf_dtab[minor(dev)];
  306.     if (!D_ISFREE(d)) {
  307.         splx(s);
  308.         return (EBUSY);
  309.     } else
  310.         /* Mark "free" and do most initialization. */
  311.         bzero((char *)d, sizeof(*d));
  312.     splx(s);
  313.  
  314.     error = bpf_initd(d);
  315.     if (error) {
  316.         D_MARKFREE(d);
  317.         return (error);
  318.     }
  319.     return (0);
  320. }
  321.  
  322. /*
  323.  * Close the descriptor by detaching it from its interface,
  324.  * deallocating its buffers, and marking it free.
  325.  */
  326. /* ARGSUSED */
  327. bpfclose(dev, flag)
  328.     dev_t dev;
  329.     int flag;
  330. {
  331.     register struct bpf_d *d = &bpf_dtab[minor(dev)];
  332.     int s;
  333.  
  334.     s = splimp();
  335.     if (d->bd_bif)
  336.         bpf_detachd(d);
  337.     splx(s);
  338.  
  339.     /* Free the buffer space. */
  340.     if (d->bd_hbuf)
  341.         bsd_free(d->bd_hbuf, M_DEVBUF);
  342.     if (d->bd_fbuf)
  343.         bsd_free(d->bd_fbuf, M_DEVBUF);
  344.     bsd_free(d->bd_sbuf, M_DEVBUF);
  345.     if (d->bd_filter)
  346.         bsd_free((caddr_t)d->bd_filter, M_DEVBUF);
  347.     
  348.     D_MARKFREE(d);
  349. }
  350.  
  351. /*
  352.  * Rotate the packet buffers in descriptor d.  Move the store buffer
  353.  * into the hold slot, and the free buffer into the store slot.  
  354.  * Zero the length of the new store buffer.
  355.  */
  356. #define ROTATE_BUFFERS(d) \
  357.     (d)->bd_hbuf = (d)->bd_sbuf; \
  358.     (d)->bd_hlen = (d)->bd_slen; \
  359.     (d)->bd_sbuf = (d)->bd_fbuf; \
  360.     (d)->bd_slen = 0; \
  361.     (d)->bd_fbuf = 0; 
  362. /*
  363.  *  bpfread - read next chunk of packets from buffers
  364.  */
  365. int
  366. bpfread(dev, uio)
  367.     dev_t dev;
  368.     register struct uio *uio;
  369. {
  370.     register struct bpf_d *d = &bpf_dtab[minor(dev)];
  371.     int error;
  372.     int s;
  373.  
  374.     /*
  375.      * Restrict application to use a buffer the same size as 
  376.      * as kernel buffers.
  377.      */
  378.     if (uio->uio_resid != d->bd_bufsize)
  379.         return (EINVAL);
  380.  
  381.     s = splimp();
  382.     /*
  383.      * If the hold buffer is empty, then set a timer and sleep
  384.      * until either the timeout has occurred or enough packets have
  385.      * arrived to fill the store buffer.
  386.      */
  387.     while (d->bd_hbuf == 0) {
  388.         if (d->bd_immediate && d->bd_slen != 0) {
  389.             /*
  390.              * A packet(s) either arrived since the previous
  391.              * read or arrived while we were asleep.
  392.              * Rotate the buffers and return what's here.
  393.              */
  394.             ROTATE_BUFFERS(d);
  395.             break;
  396.         }
  397.         error = tsleep((caddr_t)d, PRINET|PCATCH, "bpf", d->bd_rtout);
  398.         if (error == EINTR || error == ERESTART) {
  399.             splx(s);
  400.             return (error);
  401.         }
  402.         if (error == EWOULDBLOCK) {
  403.             /*
  404.              * On a timeout, return what's in the buffer,
  405.              * which may be nothing.  If there is something
  406.              * in the store buffer, we can rotate the buffers.
  407.              */
  408.             if (d->bd_hbuf)
  409.                 /*
  410.                  * We filled up the buffer in between 
  411.                  * getting the timeout and arriving
  412.                  * here, so we don't need to rotate.
  413.                  */
  414.                 break;
  415.  
  416.             if (d->bd_slen == 0) {
  417.                 splx(s);
  418.                 return (0);
  419.             }
  420.             ROTATE_BUFFERS(d);
  421.             break;
  422.         }
  423.     }
  424.     /*
  425.      * At this point, we know we have something in the hold slot.
  426.      */
  427.     splx(s);
  428.     
  429.     /*  
  430.      * Move data from hold buffer into user space.
  431.      * We know the entire buffer is transferred since
  432.      * we checked above that the read buffer is bpf_bufsize bytes.
  433.      */
  434.     error = uiomove(d->bd_hbuf, d->bd_hlen, uio);
  435.  
  436.     s = splimp();
  437.     d->bd_fbuf = d->bd_hbuf;
  438.     d->bd_hbuf = 0;
  439.     splx(s);
  440.     
  441.     return (error);
  442. }
  443.  
  444.  
  445. /*
  446.  * If there are processes sleeping on this descriptor, wake them up.  
  447.  */
  448. static inline void
  449. bpf_wakeup(d)
  450.     register struct bpf_d *d;
  451. {
  452.     wakeup((caddr_t)d);
  453.     if (d->bd_selproc) {
  454.         selwakeup(d->bd_selproc, (int)d->bd_selcoll);
  455.         d->bd_selcoll = 0;
  456.         d->bd_selproc = 0;
  457.     }
  458. }
  459.  
  460. int
  461. bpfwrite(dev, uio)
  462.     dev_t dev;
  463.     struct uio *uio;
  464. {
  465.     register struct bpf_d *d = &bpf_dtab[minor(dev)];
  466.     struct ifnet *ifp;
  467.     struct mbuf *m;
  468.     int error, s;
  469.     static struct sockaddr dst;
  470.  
  471.     if (d->bd_bif == 0)
  472.         return (ENXIO);
  473.  
  474.     ifp = d->bd_bif->bif_ifp;
  475.  
  476.     if (uio->uio_resid == 0)
  477.         return (0);
  478.     if (uio->uio_resid > ifp->if_mtu)
  479.         return (EMSGSIZE);
  480.  
  481.     error = bpf_movein(uio, (int)d->bd_bif->bif_dlt, &m, &dst);
  482.     if (error)
  483.         return (error);
  484.  
  485.     s = splnet();
  486.     error = (*ifp->if_output)(ifp, m, &dst);
  487.     splx(s);
  488.     /*
  489.      * The driver frees the mbuf. 
  490.      */
  491.     return (error);
  492. }
  493.  
  494. /*
  495.  * Reset a descriptor by flushing its packet bufferand clearing the receive
  496.  * and drop counts.  Should be called at splimp.
  497.  */
  498. static void
  499. reset_d(d)
  500.     struct bpf_d *d;
  501. {
  502.     if (d->bd_hbuf) {
  503.         /* Free the hold buffer. */
  504.         d->bd_fbuf = d->bd_hbuf;
  505.         d->bd_hbuf = 0;
  506.     }
  507.     d->bd_slen = 0;
  508.     d->bd_rcount = 0;
  509.     d->bd_dcount = 0;
  510. }
  511.  
  512. /*
  513.  *  FIONREAD        Check for read packet available.
  514.  *  SIOCGIFADDR        Get interface address - convenient hook to driver.
  515.  *  BIOCGFLEN        Get max filter len.
  516.  *  BIOCGBLEN        Get buffer len [for read()].
  517.  *  BIOCSETF        Set ethernet read filter.
  518.  *  BIOCFLUSH        Flush read packet buffer.
  519.  *  BIOCPROMISC        Put interface into promiscuous mode.
  520.  *  BIOCGDLT        Get link layer type.
  521.  *  BIOCGETIF        Get interface name.
  522.  *  BIOCSETIF        Set interface.
  523.  *  BIOCSRTIMEOUT    Set read timeout.
  524.  *  BIOCGRTIMEOUT    Get read timeout.
  525.  *  BIOCGSTATS        Get packet stats.
  526.  *  BIOCIMMEDIATE    Set immediate mode.
  527.  */
  528. /* ARGSUSED */
  529. int
  530. bpfioctl(dev, cmd, addr, flag)
  531.     dev_t dev;
  532.     int cmd;
  533.     caddr_t addr;
  534.     int flag;
  535. {
  536.     register struct bpf_d *d = &bpf_dtab[minor(dev)];
  537.     int s, error = 0;
  538.  
  539.     switch (cmd) {
  540.  
  541.     default:
  542.         error = EINVAL;
  543.         break;
  544.  
  545.     /*
  546.      * Check for read packet available.
  547.      */
  548.     case FIONREAD:
  549.         {
  550.             int n;
  551.         
  552.             s = splimp();
  553.             n = d->bd_slen;
  554.             if (d->bd_hbuf) 
  555.                 n += d->bd_hlen;
  556.             splx(s);
  557.  
  558.             *(int *)addr = n;
  559.             break;
  560.         }
  561.  
  562.     case SIOCGIFADDR:
  563.         {
  564.             struct ifnet *ifp;
  565.  
  566.             if (d->bd_bif == 0)
  567.                 error = EINVAL;
  568.             else {
  569.                 ifp = d->bd_bif->bif_ifp;
  570.                 error =  (*ifp->if_ioctl)(ifp, cmd, addr);
  571.             }
  572.             break;
  573.         }
  574.  
  575.     /*
  576.      * Get max filter len.
  577.      */
  578.     case BIOCGFLEN:
  579.         *(u_int *)addr = BPF_MAXINSNS;
  580.         break;
  581.     /*
  582.      * Get buffer len [for read()].
  583.      */
  584.     case BIOCGBLEN:
  585.         *(u_int *)addr = d->bd_bufsize;
  586.         break;
  587.  
  588.     /*
  589.      * Set ethernet read filter.
  590.      */
  591.         case BIOCSETF:
  592.         error = bpf_setf(d, (struct bpf_program *)addr);
  593.         break;
  594.  
  595.     /*
  596.      * Flush read packet buffer.
  597.      */
  598.     case BIOCFLUSH:
  599.         s = splimp();
  600.         reset_d(d);
  601.         splx(s);
  602.         break;
  603.  
  604.     /*
  605.      * Put interface into promiscuous mode.
  606.      */
  607.     case BIOCPROMISC:
  608.         if (d->bd_bif == 0) {
  609.             /*
  610.              * No interface attached yet.
  611.              */
  612.             error = EINVAL;
  613.             break;
  614.         }
  615.         s = splimp();
  616.         if (d->bd_promisc == 0) {
  617.             d->bd_promisc = 1;
  618.             error = ifpromisc(d->bd_bif->bif_ifp, 1);
  619.         }
  620.         splx(s);
  621.         break;
  622.  
  623.     /*
  624.      * Get device parameters.
  625.      */
  626.     case BIOCGDLT:
  627.         if (d->bd_bif == 0)
  628.             error = EINVAL;
  629.         else
  630.             *(u_int *)addr = d->bd_bif->bif_dlt;
  631.         break;
  632.  
  633.     /*
  634.      * Set interface name.
  635.      */
  636.     case BIOCGETIF:
  637.         if (d->bd_bif == 0)
  638.             error = EINVAL;
  639.         else
  640.             bpf_ifname(d->bd_bif->bif_ifp, (struct ifreq *)addr);
  641.         break;
  642.  
  643.     /*
  644.      * Set interface.
  645.      */
  646.     case BIOCSETIF:
  647.         error = bpf_setif(d, (struct ifreq *)addr);
  648.         break;
  649.  
  650.     /*
  651.      * Set read timeout.
  652.      */
  653.      case BIOCSRTIMEOUT:
  654.         {
  655.             struct timeval *tv = (struct timeval *)addr;
  656.             u_long msec;
  657.  
  658.             /* Compute number of milliseconds. */
  659.             msec = tv->tv_sec * 1000 + tv->tv_usec / 1000;
  660.             /* Scale milliseconds to ticks.  Assume hard
  661.                clock has millisecond or greater resolution
  662.                (i.e. tick >= 1000).  For 10ms hardclock,
  663.                tick/1000 = 10, so rtout<-msec/10. */
  664.             d->bd_rtout = msec / (tick / 1000);
  665.             break;
  666.         }
  667.  
  668.     /*
  669.      * Get read timeout.
  670.      */
  671.      case BIOCGRTIMEOUT:
  672.         {
  673.             struct timeval *tv = (struct timeval *)addr;
  674.             u_long msec = d->bd_rtout;
  675.  
  676.             msec *= tick / 1000;
  677.             tv->tv_sec = msec / 1000;
  678.             tv->tv_usec = msec % 1000;
  679.             break;
  680.         }
  681.  
  682.     /*
  683.      * Get packet stats.
  684.      */
  685.     case BIOCGSTATS:
  686.         {
  687.             struct bpf_stat *bs = (struct bpf_stat *)addr;
  688.  
  689.             bs->bs_recv = d->bd_rcount;
  690.             bs->bs_drop = d->bd_dcount;
  691.             break;
  692.         }
  693.  
  694.     /*
  695.      * Set immediate mode.
  696.      */
  697.     case BIOCIMMEDIATE:
  698.         d->bd_immediate = *(u_int *)addr;
  699.         break;
  700.     }
  701.     return (error);
  702. }
  703.  
  704. /* 
  705.  * Set d's packet filter program to 'fp'.  If 'd' already has a filter,
  706.  * free it and replace it.  Returns EINVAL for bogus requests.
  707.  */
  708. int
  709. bpf_setf(d, fp)
  710.     struct bpf_d *d;
  711.     struct bpf_program *fp;
  712. {
  713.     struct bpf_insn *fcode, *old;
  714.     u_int flen, size;
  715.     int s;
  716.  
  717.     old = d->bd_filter;
  718.     if (fp->bf_insns == 0) {
  719.         if (fp->bf_len != 0)
  720.             return (EINVAL);
  721.         s = splimp();
  722.         d->bd_filter = 0;
  723.         reset_d(d);
  724.         splx(s);
  725.         if (old != 0)
  726.             bsd_free((caddr_t)old, M_DEVBUF);
  727.         return (0);
  728.     }
  729.     flen = fp->bf_len;
  730.     if (flen > BPF_MAXINSNS)
  731.         return (EINVAL);
  732.  
  733.     size = flen * sizeof(*fp->bf_insns);
  734.     fcode = (struct bpf_insn *)bsd_malloc(size, M_DEVBUF, M_WAITOK);
  735.     if (copyin((caddr_t)fp->bf_insns, (caddr_t)fcode, size))
  736.         return (EINVAL);
  737.     
  738.     if (bpf_validate(fcode, (int)flen)) {
  739.         s = splimp();
  740.         d->bd_filter = fcode;
  741.         reset_d(d);
  742.         splx(s);
  743.         if (old != 0)
  744.             bsd_free((caddr_t)old, M_DEVBUF);
  745.  
  746.         return (0);
  747.     }
  748.     bsd_free((caddr_t)fcode, M_DEVBUF);
  749.     return (EINVAL);
  750. }
  751.  
  752. /*
  753.  * Detach 'd' from its current interface (if attached at all) and attach to 
  754.  * the interface named 'name'.  Return ioctl error code or 0.
  755.  */
  756. static int
  757. bpf_setif(d, ifr)
  758.     struct bpf_d *d;
  759.     struct ifreq *ifr;
  760. {
  761.     struct bpf_if *bp;
  762.     char *cp;
  763.     int unit, s;
  764.  
  765.     /*
  766.      * Separate string into name part and unit number.  Put a null
  767.      * byte at the end of the name part, and compute the number. 
  768.      * If the a unit number is unspecified, the default is 0,
  769.      * as initialized above.  XXX This should be common code.
  770.      */
  771.     unit = 0;
  772.     cp = ifr->ifr_name;
  773.     cp[sizeof(ifr->ifr_name) - 1] = '\0';
  774.     while (*cp++) {
  775.         if (*cp >= '0' && *cp <= '9') {
  776.             unit = *cp - '0';
  777.             *cp++ = '\0';
  778.             while (*cp)
  779.                 unit = 10 * unit + *cp++ - '0';
  780.             break;
  781.         }
  782.     }
  783.     /*
  784.      * Look through attached interfaces for the named one.
  785.      */
  786.     for (bp = bpf_iflist; bp != 0; bp = bp->bif_next) {
  787.         struct ifnet *ifp = bp->bif_ifp;
  788.  
  789.         if (ifp == 0 || unit != ifp->if_unit 
  790.             || strcmp(ifp->if_name, ifr->ifr_name) != 0)
  791.             continue;
  792.         /*
  793.          * We found the requested interface.  If we're
  794.          * already attached to it, just flush the buffer.
  795.          * If it's not up, return an error.
  796.          */
  797.         if ((ifp->if_flags & IFF_UP) == 0)
  798.             return (ENETDOWN);
  799.         s = splimp();
  800.         if (bp != d->bd_bif) {
  801.             if (d->bd_bif)
  802.                 /* 
  803.                  * Detach if attached to something else.
  804.                  */
  805.                 bpf_detachd(d);
  806.  
  807.             bpf_attachd(d, bp);
  808.         }
  809.         reset_d(d);
  810.         splx(s);
  811.         return (0);
  812.     }
  813.     /* Not found. */
  814.     return (ENXIO);
  815. }
  816.  
  817. /*
  818.  * Lookup the name of the 'ifp' interface and return it in 'ifr->ifr_name'.
  819.  * We augment the ifp's base name with its unit number.
  820.  */
  821. static void
  822. bpf_ifname(ifp, ifr)
  823.     struct ifnet *ifp;
  824.     struct ifreq *ifr;
  825. {
  826.     char *s = ifp->if_name;
  827.     char *d = ifr->ifr_name;
  828.  
  829.     while (*d++ = *s++)
  830.         ;
  831.     /* XXX Assume that unit number is less than 10. */
  832.     *d++ = ifp->if_unit + '0';
  833.     *d = '\0';
  834. }
  835.  
  836. /*
  837.  * Support for select() system call
  838.  * Inspired by the code in tty.c for the same purpose.
  839.  *
  840.  * bpfselect - returns true iff the specific operation
  841.  *    will not block indefinitely.  Otherwise, return
  842.  *    false but make a note that a selwakeup() must be done.
  843.  */
  844. int
  845. bpfselect(dev, rw, p)
  846.     register dev_t dev;
  847.     int rw;
  848.     struct proc *p;
  849. {
  850.     register struct bpf_d *d;
  851.     register int s;
  852.     
  853.     if (rw != FREAD)
  854.         return (0);
  855.     /*
  856.      * An imitation of the FIONREAD ioctl code.
  857.      */
  858.     d = &bpf_dtab[minor(dev)];
  859.     
  860.     s = splimp();
  861.     if (d->bd_hlen != 0 || (d->bd_immediate && d->bd_slen != 0)) {
  862.         /*
  863.          * There is data waiting.
  864.          */
  865.         splx(s);
  866.         return (1);
  867.     }
  868.     /*
  869.      * No data ready.  If there's already a select() waiting on this
  870.      * minor device then this is a collision.  This shouldn't happen 
  871.      * because minors really should not be shared, but if a process
  872.      * forks while one of these is open, it is possible that both
  873.      * processes could select on the same descriptor.
  874.      */
  875.     if (d->bd_selproc && d->bd_selproc->p_wchan == (caddr_t)&selwait)
  876.         d->bd_selcoll = 1;
  877.     else
  878.         d->bd_selproc = p;
  879.  
  880.     splx(s);    
  881.     return (0);
  882. }
  883.  
  884. /*
  885.  * bpf_tap - incoming linkage from device drivers
  886.  */
  887. void
  888. bpf_tap(arg, pkt, pktlen)
  889.     caddr_t arg;
  890.     register u_char *pkt;
  891.     register u_int pktlen;
  892. {
  893.     struct bpf_if *bp;
  894.     register struct bpf_d *d;
  895.     register u_int slen;
  896.     /*
  897.      * Note that the ipl does not have to be raised at this point.
  898.      * The only problem that could arise here is that if two different
  899.      * interfaces shared any data.  This is not the case.
  900.      */
  901.     bp = (struct bpf_if *)arg;
  902.     for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
  903.         ++d->bd_rcount;
  904.         slen = bpf_filter(d->bd_filter, pkt, pktlen, pktlen);
  905.         if (slen != 0)
  906.             catchpacket(d, pkt, pktlen, slen, bcopy);
  907.     }
  908. }
  909.  
  910. /*
  911.  * Copy data from an mbuf chain into a buffer.  This code is derived
  912.  * from m_copydata in sys/uipc_mbuf.c.
  913.  */
  914. static void
  915. bpf_mcopy(src, dst, len)
  916.     u_char *src;
  917.     u_char *dst;
  918.     register int len;
  919. {
  920.     register struct mbuf *m = (struct mbuf *)src;
  921.     register unsigned count;
  922.  
  923.     while (len > 0) {
  924.         if (m == 0)
  925.             panic("bpf_mcopy");
  926.         count = MIN(m->m_len, len);
  927.         bcopy(mtod(m, caddr_t), (caddr_t)dst, count);
  928.         m = m->m_next;
  929.         dst += count;
  930.         len -= count;
  931.     }
  932. }
  933.  
  934. /*
  935.  * bpf_mtap - incoming linkage from device drivers, when packet
  936.  *   is in an mbuf chain
  937.  */
  938. void
  939. bpf_mtap(arg, m)
  940.     caddr_t arg;
  941.     struct mbuf *m;
  942. {
  943.     struct bpf_if *bp = (struct bpf_if *)arg;
  944.     struct bpf_d *d;
  945.     u_int pktlen, slen;
  946.     struct mbuf *m0;
  947.  
  948.     pktlen = 0;
  949.     for (m0 = m; m0 != m; m0 = m0->m_next)
  950.         pktlen += m0->m_len;
  951.  
  952.     for (d = bp->bif_dlist; d != 0; d = d->bd_next) {
  953.         ++d->bd_rcount;
  954.         slen = bpf_filter(d->bd_filter, (u_char *)m, pktlen, 0);
  955.         if (slen != 0)
  956.             catchpacket(d, (u_char *)m, pktlen, slen, bpf_mcopy);
  957.     }
  958. }
  959.  
  960. /*
  961.  * Move the packet data from interface memory (pkt) into the
  962.  * store buffer.  Return 1 if it's time to wakeup a listener (buffer full),
  963.  * otherwise 0.  'copy' is the routine called to do the actual data 
  964.  * transfer.  'bcopy' is passed in to copy contiguous chunks, while
  965.  * 'bpf_mcopy' is passed in to copy mbuf chains.  In the latter
  966.  * case, 'pkt' is really an mbuf.
  967.  */
  968. static void
  969. catchpacket(d, pkt, pktlen, snaplen, cpfn)
  970.     register struct bpf_d *d;
  971.     register u_char *pkt;
  972.     register u_int pktlen, snaplen;
  973.     register void (*cpfn)();
  974. {
  975.     register struct bpf_hdr *hp;
  976.     register int totlen, curlen;
  977.     register int hdrlen = d->bd_bif->bif_hdrlen;
  978.     /*
  979.      * Figure out how many bytes to move.  If the packet is
  980.      * greater or equal to the snapshot length, transfer that
  981.      * much.  Otherwise, transfer the whole packet (unless
  982.      * we hit the buffer size limit).
  983.      */
  984.     totlen = hdrlen + MIN(snaplen, pktlen);
  985.     if (totlen > d->bd_bufsize)
  986.         totlen = d->bd_bufsize;
  987.  
  988.     /*
  989.      * Round up the end of the previous packet to the next longword.
  990.      */
  991.     curlen = BPF_WORDALIGN(d->bd_slen);
  992.     if (curlen + totlen > d->bd_bufsize) {
  993.         /*
  994.          * This packet will overflow the storage buffer.
  995.          * Rotate the buffers if we can, then wakeup any
  996.          * pending reads.
  997.          */
  998.         if (d->bd_fbuf == 0) {
  999.             /* 
  1000.              * We haven't completed the previous read yet, 
  1001.              * so drop the packet.
  1002.              */
  1003.             ++d->bd_dcount;
  1004.             return;
  1005.         }
  1006.         ROTATE_BUFFERS(d);
  1007.         bpf_wakeup(d);
  1008.         curlen = 0;
  1009.     }
  1010.     else if (d->bd_immediate) 
  1011.         /*
  1012.          * Immediate mode is set.  A packet arrived so any
  1013.          * reads should be woken up.
  1014.          */
  1015.         bpf_wakeup(d);
  1016.  
  1017.     /*
  1018.      * Append the bpf header.
  1019.      */
  1020.     hp = (struct bpf_hdr *)(d->bd_sbuf + curlen);
  1021. #ifdef sun
  1022.     uniqtime(&hp->bh_tstamp);
  1023. #else
  1024. #ifdef hp300
  1025.     microtime(&hp->bh_tstamp);
  1026. #else
  1027.     hp->bh_tstamp = time;
  1028. #endif
  1029. #endif
  1030.     hp->bh_datalen = pktlen;
  1031.     hp->bh_hdrlen = hdrlen;
  1032.     /*
  1033.      * Copy the packet data into the store buffer and update its length.
  1034.      */
  1035.     (*cpfn)(pkt, (u_char *)hp + hdrlen, (hp->bh_caplen = totlen - hdrlen));
  1036.     d->bd_slen = curlen + totlen;
  1037. }
  1038.  
  1039. /* 
  1040.  * Initialize all nonzero fields of a descriptor.
  1041.  */
  1042. static int
  1043. bpf_initd(d)
  1044.     register struct bpf_d *d;
  1045. {
  1046.     d->bd_bufsize = bpf_bufsize;
  1047.     d->bd_fbuf = (caddr_t)bsd_malloc(d->bd_bufsize, M_DEVBUF, M_WAITOK);
  1048.     if (d->bd_fbuf == 0)
  1049.         return (ENOBUFS);
  1050.  
  1051.     d->bd_sbuf = (caddr_t)bsd_malloc(d->bd_bufsize, M_DEVBUF, M_WAITOK);
  1052.     if (d->bd_sbuf == 0) {
  1053.         bsd_free(d->bd_fbuf, M_DEVBUF);
  1054.         return (ENOBUFS);
  1055.     }
  1056.     d->bd_slen = 0;
  1057.     d->bd_hlen = 0;
  1058.     return (0);
  1059. }
  1060.  
  1061. /*
  1062.  * Register 'ifp' with bpf.  XXX
  1063.  * and 'driverp' is a pointer to the 'struct bpf_if *' in the driver's softc.
  1064.  */
  1065. void
  1066. bpfattach(driverp, ifp, dlt, hdrlen)
  1067.     caddr_t *driverp;
  1068.     struct ifnet *ifp;
  1069.     u_int dlt, hdrlen;
  1070. {
  1071.     struct bpf_if *bp;
  1072.     int i;
  1073.  
  1074.     bp = (struct bpf_if *)bsd_malloc(sizeof(*bp), M_DEVBUF, M_DONTWAIT);
  1075.     if (bp == 0)
  1076.         panic("bpfattach");
  1077.  
  1078.     bp->bif_dlist = 0;
  1079.     bp->bif_driverp = (struct bpf_if **)driverp;
  1080.     bp->bif_ifp = ifp;
  1081.     bp->bif_dlt = dlt;
  1082.  
  1083.     bp->bif_next = bpf_iflist;
  1084.     bpf_iflist = bp;
  1085.  
  1086.     *bp->bif_driverp = 0;
  1087.  
  1088.     /*
  1089.      * Compute the length of the bpf header.  This is not necessarily
  1090.      * equal to SIZEOF_BPF_HDR because we want to insert spacing such 
  1091.      * that the network layer header begins on a longword boundary (for 
  1092.      * performance reasons and to alleviate alignment restrictions).
  1093.      */
  1094.     bp->bif_hdrlen = BPF_WORDALIGN(hdrlen + SIZEOF_BPF_HDR) - hdrlen;
  1095.  
  1096.     /*
  1097.      * Mark all the descriptors free if this hasn't been done.
  1098.      */
  1099.     if (!D_ISFREE(&bpf_dtab[0]))
  1100.         for (i = 0; i < NBPFILTER; ++i)
  1101.             D_MARKFREE(&bpf_dtab[i]);
  1102.  
  1103.     printf("bpf: %s%d attached\n", ifp->if_name, ifp->if_unit);
  1104. }
  1105.  
  1106. /* XXX This routine belongs in net/if.c. */
  1107. /*
  1108.  * Set/clear promiscuous mode on interface ifp based on the truth value`
  1109.  * of pswitch.  The calls are reference counted so that only the first
  1110.  * on request actually has an effect, as does the final off request.
  1111.  * Results are undefined if the off and on requests are not matched.
  1112.  */
  1113. int
  1114. ifpromisc(ifp, pswitch)
  1115.     struct ifnet *ifp;
  1116.     int pswitch;
  1117. {
  1118.     struct ifreq ifr;
  1119.     /* 
  1120.      * If the device is not configured up, we cannot put it in
  1121.      * promiscuous mode.
  1122.      */
  1123.     if ((ifp->if_flags & IFF_UP) == 0)
  1124.         return (ENETDOWN);
  1125.  
  1126.     if (pswitch) {
  1127.         if (ifp->if_pcount++ != 0)
  1128.             return (0);
  1129.         ifp->if_flags |= IFF_PROMISC;
  1130.     } else {
  1131.         if (--ifp->if_pcount > 0)
  1132.             return (0);
  1133.         ifp->if_flags &= ~IFF_PROMISC;
  1134.     }
  1135.     ifr.ifr_flags = ifp->if_flags;
  1136.     return ((*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr));
  1137. }
  1138.  
  1139. #endif (NBPFILTER > 0)
  1140.