xref: /freebsd/sys/net/bpf_buffer.c (revision 82334850ea451f7f6903be20e4836118e6a77460)
14d621040SChristian S.J. Peron /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
44d621040SChristian S.J. Peron  * Copyright (c) 2007 Seccuris Inc.
54d621040SChristian S.J. Peron  * All rights reserved.
64d621040SChristian S.J. Peron  *
7dde153daSEitan Adler  * This software was developed by Robert N. M. Watson under contract to
84d621040SChristian S.J. Peron  * Seccuris Inc.
94d621040SChristian S.J. Peron  *
104d621040SChristian S.J. Peron  * Redistribution and use in source and binary forms, with or without
114d621040SChristian S.J. Peron  * modification, are permitted provided that the following conditions
124d621040SChristian S.J. Peron  * are met:
134d621040SChristian S.J. Peron  * 1. Redistributions of source code must retain the above copyright
144d621040SChristian S.J. Peron  *    notice, this list of conditions and the following disclaimer.
154d621040SChristian S.J. Peron  * 2. Redistributions in binary form must reproduce the above copyright
164d621040SChristian S.J. Peron  *    notice, this list of conditions and the following disclaimer in the
174d621040SChristian S.J. Peron  *    documentation and/or other materials provided with the distribution.
184d621040SChristian S.J. Peron  *
194d621040SChristian S.J. Peron  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
204d621040SChristian S.J. Peron  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
214d621040SChristian S.J. Peron  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
224d621040SChristian S.J. Peron  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
234d621040SChristian S.J. Peron  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
244d621040SChristian S.J. Peron  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
254d621040SChristian S.J. Peron  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
264d621040SChristian S.J. Peron  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
274d621040SChristian S.J. Peron  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
284d621040SChristian S.J. Peron  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
294d621040SChristian S.J. Peron  * SUCH DAMAGE.
304d621040SChristian S.J. Peron  *
314d621040SChristian S.J. Peron  * Copyright (c) 1990, 1991, 1993
324d621040SChristian S.J. Peron  *	The Regents of the University of California.  All rights reserved.
334d621040SChristian S.J. Peron  *
344d621040SChristian S.J. Peron  * This code is derived from the Stanford/CMU enet packet filter,
354d621040SChristian S.J. Peron  * (net/enet.c) distributed as part of 4.3BSD, and code contributed
364d621040SChristian S.J. Peron  * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
374d621040SChristian S.J. Peron  * Berkeley Laboratory.
384d621040SChristian S.J. Peron  *
394d621040SChristian S.J. Peron  * Redistribution and use in source and binary forms, with or without
404d621040SChristian S.J. Peron  * modification, are permitted provided that the following conditions
414d621040SChristian S.J. Peron  * are met:
424d621040SChristian S.J. Peron  * 1. Redistributions of source code must retain the above copyright
434d621040SChristian S.J. Peron  *    notice, this list of conditions and the following disclaimer.
444d621040SChristian S.J. Peron  * 2. Redistributions in binary form must reproduce the above copyright
454d621040SChristian S.J. Peron  *    notice, this list of conditions and the following disclaimer in the
464d621040SChristian S.J. Peron  *    documentation and/or other materials provided with the distribution.
47fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
484d621040SChristian S.J. Peron  *    may be used to endorse or promote products derived from this software
494d621040SChristian S.J. Peron  *    without specific prior written permission.
504d621040SChristian S.J. Peron  *
514d621040SChristian S.J. Peron  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
524d621040SChristian S.J. Peron  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
534d621040SChristian S.J. Peron  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
544d621040SChristian S.J. Peron  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
554d621040SChristian S.J. Peron  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
564d621040SChristian S.J. Peron  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
574d621040SChristian S.J. Peron  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
584d621040SChristian S.J. Peron  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
594d621040SChristian S.J. Peron  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
604d621040SChristian S.J. Peron  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
614d621040SChristian S.J. Peron  * SUCH DAMAGE.
624d621040SChristian S.J. Peron  *
634d621040SChristian S.J. Peron  *      @(#)bpf.c	8.4 (Berkeley) 1/9/95
644d621040SChristian S.J. Peron  */
654d621040SChristian S.J. Peron 
664d621040SChristian S.J. Peron #include <sys/cdefs.h>
674d621040SChristian S.J. Peron __FBSDID("$FreeBSD$");
684d621040SChristian S.J. Peron 
694d621040SChristian S.J. Peron #include "opt_bpf.h"
704d621040SChristian S.J. Peron 
714d621040SChristian S.J. Peron #include <sys/param.h>
72e2e050c8SConrad Meyer #include <sys/lock.h>
734d621040SChristian S.J. Peron #include <sys/malloc.h>
744d621040SChristian S.J. Peron #include <sys/mbuf.h>
75e2e050c8SConrad Meyer #include <sys/mutex.h>
764d621040SChristian S.J. Peron #include <sys/socket.h>
774d621040SChristian S.J. Peron #include <sys/uio.h>
784d621040SChristian S.J. Peron #include <sys/kernel.h>
794d621040SChristian S.J. Peron #include <sys/sysctl.h>
804d621040SChristian S.J. Peron 
814d621040SChristian S.J. Peron #include <net/if.h>
824d621040SChristian S.J. Peron #include <net/bpf.h>
834d621040SChristian S.J. Peron #include <net/bpf_buffer.h>
844d621040SChristian S.J. Peron #include <net/bpfdesc.h>
854d621040SChristian S.J. Peron 
864d621040SChristian S.J. Peron /*
874d621040SChristian S.J. Peron  * Implement historical kernel memory buffering model for BPF: two malloc(9)
884d621040SChristian S.J. Peron  * kernel buffers are hung off of the descriptor.  The size is fixed prior to
894d621040SChristian S.J. Peron  * attaching to an ifnet, ad cannot be changed after that.  read(2) simply
904d621040SChristian S.J. Peron  * copies the data to user space using uiomove(9).
914d621040SChristian S.J. Peron  */
924d621040SChristian S.J. Peron 
934d621040SChristian S.J. Peron static int bpf_bufsize = 4096;
944d621040SChristian S.J. Peron SYSCTL_INT(_net_bpf, OID_AUTO, bufsize, CTLFLAG_RW,
9557542d04SMaxim Konovalov     &bpf_bufsize, 0, "Default capture buffer size in bytes");
964d621040SChristian S.J. Peron static int bpf_maxbufsize = BPF_MAXBUFSIZE;
974d621040SChristian S.J. Peron SYSCTL_INT(_net_bpf, OID_AUTO, maxbufsize, CTLFLAG_RW,
9857542d04SMaxim Konovalov     &bpf_maxbufsize, 0, "Maximum capture buffer in bytes");
994d621040SChristian S.J. Peron 
1004d621040SChristian S.J. Peron /*
1014d621040SChristian S.J. Peron  * Simple data copy to the current kernel buffer.
1024d621040SChristian S.J. Peron  */
1034d621040SChristian S.J. Peron void
1044d621040SChristian S.J. Peron bpf_buffer_append_bytes(struct bpf_d *d, caddr_t buf, u_int offset,
1054d621040SChristian S.J. Peron     void *src, u_int len)
1064d621040SChristian S.J. Peron {
1074d621040SChristian S.J. Peron 	u_char *src_bytes;
1084d621040SChristian S.J. Peron 
1094d621040SChristian S.J. Peron 	src_bytes = (u_char *)src;
1104d621040SChristian S.J. Peron 	bcopy(src_bytes, buf + offset, len);
1114d621040SChristian S.J. Peron }
1124d621040SChristian S.J. Peron 
1134d621040SChristian S.J. Peron /*
1144d621040SChristian S.J. Peron  * Scatter-gather data copy from an mbuf chain to the current kernel buffer.
1154d621040SChristian S.J. Peron  */
1164d621040SChristian S.J. Peron void
1174d621040SChristian S.J. Peron bpf_buffer_append_mbuf(struct bpf_d *d, caddr_t buf, u_int offset, void *src,
1184d621040SChristian S.J. Peron     u_int len)
1194d621040SChristian S.J. Peron {
1204d621040SChristian S.J. Peron 	const struct mbuf *m;
1214d621040SChristian S.J. Peron 	u_char *dst;
1224d621040SChristian S.J. Peron 
1234d621040SChristian S.J. Peron 	m = (struct mbuf *)src;
1244d621040SChristian S.J. Peron 	dst = (u_char *)buf + offset;
125*82334850SJohn Baldwin 	m_copydata(m, 0, len, dst);
1264d621040SChristian S.J. Peron }
1274d621040SChristian S.J. Peron 
1284d621040SChristian S.J. Peron /*
1294d621040SChristian S.J. Peron  * Free BPF kernel buffers on device close.
1304d621040SChristian S.J. Peron  */
1314d621040SChristian S.J. Peron void
1324d621040SChristian S.J. Peron bpf_buffer_free(struct bpf_d *d)
1334d621040SChristian S.J. Peron {
1344d621040SChristian S.J. Peron 
1354d621040SChristian S.J. Peron 	if (d->bd_sbuf != NULL)
1364d621040SChristian S.J. Peron 		free(d->bd_sbuf, M_BPF);
1374d621040SChristian S.J. Peron 	if (d->bd_hbuf != NULL)
1384d621040SChristian S.J. Peron 		free(d->bd_hbuf, M_BPF);
1394d621040SChristian S.J. Peron 	if (d->bd_fbuf != NULL)
1404d621040SChristian S.J. Peron 		free(d->bd_fbuf, M_BPF);
1414d621040SChristian S.J. Peron 
1424d621040SChristian S.J. Peron #ifdef INVARIANTS
1434d621040SChristian S.J. Peron 	d->bd_sbuf = d->bd_hbuf = d->bd_fbuf = (caddr_t)~0;
1444d621040SChristian S.J. Peron #endif
1454d621040SChristian S.J. Peron }
1464d621040SChristian S.J. Peron 
1474d621040SChristian S.J. Peron /*
1484d621040SChristian S.J. Peron  * This is a historical initialization that occurs when the BPF descriptor is
1494d621040SChristian S.J. Peron  * first opened.  It does not imply selection of a buffer mode, so we don't
1504d621040SChristian S.J. Peron  * allocate buffers here.
1514d621040SChristian S.J. Peron  */
1524d621040SChristian S.J. Peron void
1534d621040SChristian S.J. Peron bpf_buffer_init(struct bpf_d *d)
1544d621040SChristian S.J. Peron {
1554d621040SChristian S.J. Peron 
1564d621040SChristian S.J. Peron 	d->bd_bufsize = bpf_bufsize;
1574d621040SChristian S.J. Peron }
1584d621040SChristian S.J. Peron 
1594d621040SChristian S.J. Peron /*
1604d621040SChristian S.J. Peron  * Allocate or resize buffers.
1614d621040SChristian S.J. Peron  */
1624d621040SChristian S.J. Peron int
1634d621040SChristian S.J. Peron bpf_buffer_ioctl_sblen(struct bpf_d *d, u_int *i)
1644d621040SChristian S.J. Peron {
1654d621040SChristian S.J. Peron 	u_int size;
166c7b0200eSAlexander V. Chernikov 	caddr_t fbuf, sbuf;
1674d621040SChristian S.J. Peron 
1684d621040SChristian S.J. Peron 	size = *i;
1694d621040SChristian S.J. Peron 	if (size > bpf_maxbufsize)
1704d621040SChristian S.J. Peron 		*i = size = bpf_maxbufsize;
1714d621040SChristian S.J. Peron 	else if (size < BPF_MINBUFSIZE)
1724d621040SChristian S.J. Peron 		*i = size = BPF_MINBUFSIZE;
173c7b0200eSAlexander V. Chernikov 
174c7b0200eSAlexander V. Chernikov 	/* Allocate buffers immediately */
175c7b0200eSAlexander V. Chernikov 	fbuf = (caddr_t)malloc(size, M_BPF, M_WAITOK);
176c7b0200eSAlexander V. Chernikov 	sbuf = (caddr_t)malloc(size, M_BPF, M_WAITOK);
177c7b0200eSAlexander V. Chernikov 
178c7b0200eSAlexander V. Chernikov 	BPFD_LOCK(d);
179c7b0200eSAlexander V. Chernikov 	if (d->bd_bif != NULL) {
180c7b0200eSAlexander V. Chernikov 		/* Interface already attached, unable to change buffers */
181c7b0200eSAlexander V. Chernikov 		BPFD_UNLOCK(d);
182c7b0200eSAlexander V. Chernikov 		free(fbuf, M_BPF);
183c7b0200eSAlexander V. Chernikov 		free(sbuf, M_BPF);
184c7b0200eSAlexander V. Chernikov 		return (EINVAL);
185c7b0200eSAlexander V. Chernikov 	}
186c7b0200eSAlexander V. Chernikov 
187c7b0200eSAlexander V. Chernikov 	/* Free old buffers if set */
188c7b0200eSAlexander V. Chernikov 	if (d->bd_fbuf != NULL)
189c7b0200eSAlexander V. Chernikov 		free(d->bd_fbuf, M_BPF);
190c7b0200eSAlexander V. Chernikov 	if (d->bd_sbuf != NULL)
191c7b0200eSAlexander V. Chernikov 		free(d->bd_sbuf, M_BPF);
192c7b0200eSAlexander V. Chernikov 
193c7b0200eSAlexander V. Chernikov 	/* Fill in new data */
1944d621040SChristian S.J. Peron 	d->bd_bufsize = size;
195c7b0200eSAlexander V. Chernikov 	d->bd_fbuf = fbuf;
196c7b0200eSAlexander V. Chernikov 	d->bd_sbuf = sbuf;
197c7b0200eSAlexander V. Chernikov 
198c7b0200eSAlexander V. Chernikov 	d->bd_hbuf = NULL;
199c7b0200eSAlexander V. Chernikov 	d->bd_slen = 0;
200c7b0200eSAlexander V. Chernikov 	d->bd_hlen = 0;
201c7b0200eSAlexander V. Chernikov 
202afa85850SAlexander V. Chernikov 	BPFD_UNLOCK(d);
2034d621040SChristian S.J. Peron 	return (0);
2044d621040SChristian S.J. Peron }
2054d621040SChristian S.J. Peron 
2064d621040SChristian S.J. Peron /*
2074d621040SChristian S.J. Peron  * Copy buffer storage to user space in read().
2084d621040SChristian S.J. Peron  */
2094d621040SChristian S.J. Peron int
2104d621040SChristian S.J. Peron bpf_buffer_uiomove(struct bpf_d *d, caddr_t buf, u_int len, struct uio *uio)
2114d621040SChristian S.J. Peron {
2124d621040SChristian S.J. Peron 
2134d621040SChristian S.J. Peron 	return (uiomove(buf, len, uio));
2144d621040SChristian S.J. Peron }
215