2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2007 Seccuris Inc.
7 * This software was developed by Robert N. M. Watson under contract to
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Copyright (c) 1990, 1991, 1993
32 * The Regents of the University of California. All rights reserved.
34 * This code is derived from the Stanford/CMU enet packet filter,
35 * (net/enet.c) distributed as part of 4.3BSD, and code contributed
36 * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
37 * Berkeley Laboratory.
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * @(#)bpf.c 8.4 (Berkeley) 1/9/95
66 #include <sys/cdefs.h>
67 __FBSDID("$FreeBSD$");
71 #include <sys/param.h>
73 #include <sys/malloc.h>
75 #include <sys/mutex.h>
76 #include <sys/socket.h>
78 #include <sys/kernel.h>
79 #include <sys/sysctl.h>
83 #include <net/bpf_buffer.h>
84 #include <net/bpfdesc.h>
87 * Implement historical kernel memory buffering model for BPF: two malloc(9)
88 * kernel buffers are hung off of the descriptor. The size is fixed prior to
89 * attaching to an ifnet, ad cannot be changed after that. read(2) simply
90 * copies the data to user space using uiomove(9).
93 static int bpf_bufsize = 4096;
94 SYSCTL_INT(_net_bpf, OID_AUTO, bufsize, CTLFLAG_RW,
95 &bpf_bufsize, 0, "Default capture buffer size in bytes");
96 static int bpf_maxbufsize = BPF_MAXBUFSIZE;
97 SYSCTL_INT(_net_bpf, OID_AUTO, maxbufsize, CTLFLAG_RW,
98 &bpf_maxbufsize, 0, "Maximum capture buffer in bytes");
101 * Simple data copy to the current kernel buffer.
104 bpf_buffer_append_bytes(struct bpf_d *d, caddr_t buf, u_int offset,
105 void *src, u_int len)
109 src_bytes = (u_char *)src;
110 bcopy(src_bytes, buf + offset, len);
114 * Scatter-gather data copy from an mbuf chain to the current kernel buffer.
117 bpf_buffer_append_mbuf(struct bpf_d *d, caddr_t buf, u_int offset, void *src,
120 const struct mbuf *m;
123 m = (struct mbuf *)src;
124 dst = (u_char *)buf + offset;
125 m_copydata(m, 0, len, dst);
129 * Free BPF kernel buffers on device close.
132 bpf_buffer_free(struct bpf_d *d)
135 if (d->bd_sbuf != NULL)
136 free(d->bd_sbuf, M_BPF);
137 if (d->bd_hbuf != NULL)
138 free(d->bd_hbuf, M_BPF);
139 if (d->bd_fbuf != NULL)
140 free(d->bd_fbuf, M_BPF);
143 d->bd_sbuf = d->bd_hbuf = d->bd_fbuf = (caddr_t)~0;
148 * This is a historical initialization that occurs when the BPF descriptor is
149 * first opened. It does not imply selection of a buffer mode, so we don't
150 * allocate buffers here.
153 bpf_buffer_init(struct bpf_d *d)
156 d->bd_bufsize = bpf_bufsize;
160 * Allocate or resize buffers.
163 bpf_buffer_ioctl_sblen(struct bpf_d *d, u_int *i)
169 if (size > bpf_maxbufsize)
170 *i = size = bpf_maxbufsize;
171 else if (size < BPF_MINBUFSIZE)
172 *i = size = BPF_MINBUFSIZE;
174 /* Allocate buffers immediately */
175 fbuf = (caddr_t)malloc(size, M_BPF, M_WAITOK);
176 sbuf = (caddr_t)malloc(size, M_BPF, M_WAITOK);
179 if (d->bd_bif != NULL) {
180 /* Interface already attached, unable to change buffers */
187 /* Free old buffers if set */
188 if (d->bd_fbuf != NULL)
189 free(d->bd_fbuf, M_BPF);
190 if (d->bd_sbuf != NULL)
191 free(d->bd_sbuf, M_BPF);
193 /* Fill in new data */
194 d->bd_bufsize = size;
207 * Copy buffer storage to user space in read().
210 bpf_buffer_uiomove(struct bpf_d *d, caddr_t buf, u_int len, struct uio *uio)
213 return (uiomove(buf, len, uio));