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