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_buffer.c

Version: -  FREEBSD  -  FREEBSD11  -  FREEBSD10  -  FREEBSD9  -  FREEBSD92  -  FREEBSD91  -  FREEBSD90  -  FREEBSD8  -  FREEBSD82  -  FREEBSD81  -  FREEBSD80  -  FREEBSD7  -  FREEBSD74  -  FREEBSD73  -  FREEBSD72  -  FREEBSD71  -  FREEBSD70  -  FREEBSD6  -  FREEBSD64  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2007 Seccuris Inc.
    3  * All rights reserved.
    4  *
    5  * This sofware was developed by Robert N. M. Watson under contract to
    6  * Seccuris Inc.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions
   10  * are met:
   11  * 1. Redistributions of source code must retain the above copyright
   12  *    notice, this list of conditions and the following disclaimer.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *    notice, this list of conditions and the following disclaimer in the
   15  *    documentation and/or other materials provided with the distribution.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27  * SUCH DAMAGE.
   28  *
   29  * Copyright (c) 1990, 1991, 1993
   30  *      The Regents of the University of California.  All rights reserved.
   31  *
   32  * This code is derived from the Stanford/CMU enet packet filter,
   33  * (net/enet.c) distributed as part of 4.3BSD, and code contributed
   34  * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
   35  * Berkeley Laboratory.
   36  *
   37  * Redistribution and use in source and binary forms, with or without
   38  * modification, are permitted provided that the following conditions
   39  * are met:
   40  * 1. Redistributions of source code must retain the above copyright
   41  *    notice, this list of conditions and the following disclaimer.
   42  * 2. Redistributions in binary form must reproduce the above copyright
   43  *    notice, this list of conditions and the following disclaimer in the
   44  *    documentation and/or other materials provided with the distribution.
   45  * 4. Neither the name of the University nor the names of its contributors
   46  *    may be used to endorse or promote products derived from this software
   47  *    without specific prior written permission.
   48  *
   49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   59  * SUCH DAMAGE.
   60  *
   61  *      @(#)bpf.c       8.4 (Berkeley) 1/9/95
   62  */
   63 
   64 #include <sys/cdefs.h>
   65 __FBSDID("$FreeBSD: releng/8.1/sys/net/bpf_buffer.c 196151 2009-08-12 17:45:55Z jkim $");
   66 
   67 #include "opt_bpf.h"
   68 
   69 #include <sys/param.h>
   70 #include <sys/malloc.h>
   71 #include <sys/mbuf.h>
   72 #include <sys/socket.h>
   73 #include <sys/uio.h>
   74 #include <sys/kernel.h>
   75 #include <sys/sysctl.h>
   76 
   77 #include <net/if.h>
   78 #include <net/bpf.h>
   79 #include <net/bpf_buffer.h>
   80 #include <net/bpfdesc.h>
   81 
   82 /*
   83  * Implement historical kernel memory buffering model for BPF: two malloc(9)
   84  * kernel buffers are hung off of the descriptor.  The size is fixed prior to
   85  * attaching to an ifnet, ad cannot be changed after that.  read(2) simply
   86  * copies the data to user space using uiomove(9).
   87  */
   88 
   89 static int bpf_bufsize = 4096;
   90 SYSCTL_INT(_net_bpf, OID_AUTO, bufsize, CTLFLAG_RW,
   91     &bpf_bufsize, 0, "Maximum capture buffer size in bytes");
   92 static int bpf_maxbufsize = BPF_MAXBUFSIZE;
   93 SYSCTL_INT(_net_bpf, OID_AUTO, maxbufsize, CTLFLAG_RW,
   94     &bpf_maxbufsize, 0, "Default capture buffer in bytes");
   95 
   96 void
   97 bpf_buffer_alloc(struct bpf_d *d)
   98 {
   99 
  100         KASSERT(d->bd_fbuf == NULL, ("bpf_buffer_alloc: bd_fbuf != NULL"));
  101         KASSERT(d->bd_sbuf == NULL, ("bpf_buffer_alloc: bd_sbuf != NULL"));
  102         KASSERT(d->bd_hbuf == NULL, ("bpf_buffer_alloc: bd_hbuf != NULL"));
  103 
  104         d->bd_fbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
  105         d->bd_sbuf = (caddr_t)malloc(d->bd_bufsize, M_BPF, M_WAITOK);
  106         d->bd_hbuf = NULL;
  107         d->bd_slen = 0;
  108         d->bd_hlen = 0;
  109 }
  110 
  111 /*
  112  * Simple data copy to the current kernel buffer.
  113  */
  114 void
  115 bpf_buffer_append_bytes(struct bpf_d *d, caddr_t buf, u_int offset,
  116     void *src, u_int len)
  117 {
  118         u_char *src_bytes;
  119 
  120         src_bytes = (u_char *)src;
  121         bcopy(src_bytes, buf + offset, len);
  122 }
  123 
  124 /*
  125  * Scatter-gather data copy from an mbuf chain to the current kernel buffer.
  126  */
  127 void
  128 bpf_buffer_append_mbuf(struct bpf_d *d, caddr_t buf, u_int offset, void *src,
  129     u_int len)
  130 {
  131         const struct mbuf *m;
  132         u_char *dst;
  133         u_int count;
  134 
  135         m = (struct mbuf *)src;
  136         dst = (u_char *)buf + offset;
  137         while (len > 0) {
  138                 if (m == NULL)
  139                         panic("bpf_mcopy");
  140                 count = min(m->m_len, len);
  141                 bcopy(mtod(m, void *), dst, count);
  142                 m = m->m_next;
  143                 dst += count;
  144                 len -= count;
  145         }
  146 }
  147 
  148 /*
  149  * Free BPF kernel buffers on device close.
  150  */
  151 void
  152 bpf_buffer_free(struct bpf_d *d)
  153 {
  154 
  155         if (d->bd_sbuf != NULL)
  156                 free(d->bd_sbuf, M_BPF);
  157         if (d->bd_hbuf != NULL)
  158                 free(d->bd_hbuf, M_BPF);
  159         if (d->bd_fbuf != NULL)
  160                 free(d->bd_fbuf, M_BPF);
  161 
  162 #ifdef INVARIANTS
  163         d->bd_sbuf = d->bd_hbuf = d->bd_fbuf = (caddr_t)~0;
  164 #endif
  165 }
  166 
  167 /*
  168  * This is a historical initialization that occurs when the BPF descriptor is
  169  * first opened.  It does not imply selection of a buffer mode, so we don't
  170  * allocate buffers here.
  171  */
  172 void
  173 bpf_buffer_init(struct bpf_d *d)
  174 {
  175 
  176         d->bd_bufsize = bpf_bufsize;
  177 }
  178 
  179 /*
  180  * Allocate or resize buffers.
  181  */
  182 int
  183 bpf_buffer_ioctl_sblen(struct bpf_d *d, u_int *i)
  184 {
  185         u_int size;
  186 
  187         BPFD_LOCK(d);
  188         if (d->bd_bif != NULL) {
  189                 BPFD_UNLOCK(d);
  190                 return (EINVAL);
  191         }
  192         size = *i;
  193         if (size > bpf_maxbufsize)
  194                 *i = size = bpf_maxbufsize;
  195         else if (size < BPF_MINBUFSIZE)
  196                 *i = size = BPF_MINBUFSIZE;
  197         d->bd_bufsize = size;
  198         BPFD_UNLOCK(d);
  199         return (0);
  200 }
  201 
  202 /*
  203  * Copy buffer storage to user space in read().
  204  */
  205 int
  206 bpf_buffer_uiomove(struct bpf_d *d, caddr_t buf, u_int len, struct uio *uio)
  207 {
  208 
  209         return (uiomove(buf, len, uio));
  210 }

Cache object: cdf9a769f096a5897302befdd072f590


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