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
644d621040SChristian S.J. Peron #include <sys/cdefs.h>
654d621040SChristian S.J. Peron #include "opt_bpf.h"
664d621040SChristian S.J. Peron
674d621040SChristian S.J. Peron #include <sys/param.h>
68e2e050c8SConrad Meyer #include <sys/lock.h>
694d621040SChristian S.J. Peron #include <sys/malloc.h>
704d621040SChristian S.J. Peron #include <sys/mbuf.h>
71e2e050c8SConrad Meyer #include <sys/mutex.h>
724d621040SChristian S.J. Peron #include <sys/socket.h>
734d621040SChristian S.J. Peron #include <sys/uio.h>
744d621040SChristian S.J. Peron #include <sys/kernel.h>
754d621040SChristian S.J. Peron #include <sys/sysctl.h>
764d621040SChristian S.J. Peron
774d621040SChristian S.J. Peron #include <net/if.h>
784d621040SChristian S.J. Peron #include <net/bpf.h>
794d621040SChristian S.J. Peron #include <net/bpf_buffer.h>
804d621040SChristian S.J. Peron #include <net/bpfdesc.h>
814d621040SChristian S.J. Peron
824d621040SChristian S.J. Peron /*
834d621040SChristian S.J. Peron * Implement historical kernel memory buffering model for BPF: two malloc(9)
844d621040SChristian S.J. Peron * kernel buffers are hung off of the descriptor. The size is fixed prior to
854d621040SChristian S.J. Peron * attaching to an ifnet, ad cannot be changed after that. read(2) simply
864d621040SChristian S.J. Peron * copies the data to user space using uiomove(9).
874d621040SChristian S.J. Peron */
884d621040SChristian S.J. Peron
894d621040SChristian S.J. Peron static int bpf_bufsize = 4096;
904d621040SChristian S.J. Peron SYSCTL_INT(_net_bpf, OID_AUTO, bufsize, CTLFLAG_RW,
9157542d04SMaxim Konovalov &bpf_bufsize, 0, "Default capture buffer size in bytes");
924d621040SChristian S.J. Peron static int bpf_maxbufsize = BPF_MAXBUFSIZE;
934d621040SChristian S.J. Peron SYSCTL_INT(_net_bpf, OID_AUTO, maxbufsize, CTLFLAG_RW,
9457542d04SMaxim Konovalov &bpf_maxbufsize, 0, "Maximum capture buffer in bytes");
954d621040SChristian S.J. Peron
964d621040SChristian S.J. Peron /*
974d621040SChristian S.J. Peron * Simple data copy to the current kernel buffer.
984d621040SChristian S.J. Peron */
994d621040SChristian S.J. Peron void
bpf_buffer_append_bytes(struct bpf_d * d,caddr_t buf,u_int offset,void * src,u_int len)1004d621040SChristian S.J. Peron bpf_buffer_append_bytes(struct bpf_d *d, caddr_t buf, u_int offset,
1014d621040SChristian S.J. Peron void *src, u_int len)
1024d621040SChristian S.J. Peron {
1034d621040SChristian S.J. Peron u_char *src_bytes;
1044d621040SChristian S.J. Peron
1054d621040SChristian S.J. Peron src_bytes = (u_char *)src;
1064d621040SChristian S.J. Peron bcopy(src_bytes, buf + offset, len);
1074d621040SChristian S.J. Peron }
1084d621040SChristian S.J. Peron
1094d621040SChristian S.J. Peron /*
1104d621040SChristian S.J. Peron * Scatter-gather data copy from an mbuf chain to the current kernel buffer.
1114d621040SChristian S.J. Peron */
1124d621040SChristian S.J. Peron void
bpf_buffer_append_mbuf(struct bpf_d * d,caddr_t buf,u_int offset,void * src,u_int len)1134d621040SChristian S.J. Peron bpf_buffer_append_mbuf(struct bpf_d *d, caddr_t buf, u_int offset, void *src,
1144d621040SChristian S.J. Peron u_int len)
1154d621040SChristian S.J. Peron {
1164d621040SChristian S.J. Peron const struct mbuf *m;
1174d621040SChristian S.J. Peron u_char *dst;
1184d621040SChristian S.J. Peron
1194d621040SChristian S.J. Peron m = (struct mbuf *)src;
1204d621040SChristian S.J. Peron dst = (u_char *)buf + offset;
121*82334850SJohn Baldwin m_copydata(m, 0, len, dst);
1224d621040SChristian S.J. Peron }
1234d621040SChristian S.J. Peron
1244d621040SChristian S.J. Peron /*
1254d621040SChristian S.J. Peron * Free BPF kernel buffers on device close.
1264d621040SChristian S.J. Peron */
1274d621040SChristian S.J. Peron void
bpf_buffer_free(struct bpf_d * d)1284d621040SChristian S.J. Peron bpf_buffer_free(struct bpf_d *d)
1294d621040SChristian S.J. Peron {
1304d621040SChristian S.J. Peron
1314d621040SChristian S.J. Peron if (d->bd_sbuf != NULL)
1324d621040SChristian S.J. Peron free(d->bd_sbuf, M_BPF);
1334d621040SChristian S.J. Peron if (d->bd_hbuf != NULL)
1344d621040SChristian S.J. Peron free(d->bd_hbuf, M_BPF);
1354d621040SChristian S.J. Peron if (d->bd_fbuf != NULL)
1364d621040SChristian S.J. Peron free(d->bd_fbuf, M_BPF);
1374d621040SChristian S.J. Peron
1384d621040SChristian S.J. Peron #ifdef INVARIANTS
1394d621040SChristian S.J. Peron d->bd_sbuf = d->bd_hbuf = d->bd_fbuf = (caddr_t)~0;
1404d621040SChristian S.J. Peron #endif
1414d621040SChristian S.J. Peron }
1424d621040SChristian S.J. Peron
1434d621040SChristian S.J. Peron /*
1444d621040SChristian S.J. Peron * This is a historical initialization that occurs when the BPF descriptor is
1454d621040SChristian S.J. Peron * first opened. It does not imply selection of a buffer mode, so we don't
1464d621040SChristian S.J. Peron * allocate buffers here.
1474d621040SChristian S.J. Peron */
1484d621040SChristian S.J. Peron void
bpf_buffer_init(struct bpf_d * d)1494d621040SChristian S.J. Peron bpf_buffer_init(struct bpf_d *d)
1504d621040SChristian S.J. Peron {
1514d621040SChristian S.J. Peron
1524d621040SChristian S.J. Peron d->bd_bufsize = bpf_bufsize;
1534d621040SChristian S.J. Peron }
1544d621040SChristian S.J. Peron
1554d621040SChristian S.J. Peron /*
1564d621040SChristian S.J. Peron * Allocate or resize buffers.
1574d621040SChristian S.J. Peron */
1584d621040SChristian S.J. Peron int
bpf_buffer_ioctl_sblen(struct bpf_d * d,u_int * i)1594d621040SChristian S.J. Peron bpf_buffer_ioctl_sblen(struct bpf_d *d, u_int *i)
1604d621040SChristian S.J. Peron {
1614d621040SChristian S.J. Peron u_int size;
162c7b0200eSAlexander V. Chernikov caddr_t fbuf, sbuf;
1634d621040SChristian S.J. Peron
1644d621040SChristian S.J. Peron size = *i;
1654d621040SChristian S.J. Peron if (size > bpf_maxbufsize)
1664d621040SChristian S.J. Peron *i = size = bpf_maxbufsize;
1674d621040SChristian S.J. Peron else if (size < BPF_MINBUFSIZE)
1684d621040SChristian S.J. Peron *i = size = BPF_MINBUFSIZE;
169c7b0200eSAlexander V. Chernikov
170c7b0200eSAlexander V. Chernikov /* Allocate buffers immediately */
171c7b0200eSAlexander V. Chernikov fbuf = (caddr_t)malloc(size, M_BPF, M_WAITOK);
172c7b0200eSAlexander V. Chernikov sbuf = (caddr_t)malloc(size, M_BPF, M_WAITOK);
173c7b0200eSAlexander V. Chernikov
174c7b0200eSAlexander V. Chernikov BPFD_LOCK(d);
175c7b0200eSAlexander V. Chernikov if (d->bd_bif != NULL) {
176c7b0200eSAlexander V. Chernikov /* Interface already attached, unable to change buffers */
177c7b0200eSAlexander V. Chernikov BPFD_UNLOCK(d);
178c7b0200eSAlexander V. Chernikov free(fbuf, M_BPF);
179c7b0200eSAlexander V. Chernikov free(sbuf, M_BPF);
180c7b0200eSAlexander V. Chernikov return (EINVAL);
181c7b0200eSAlexander V. Chernikov }
182c7b0200eSAlexander V. Chernikov
183c7b0200eSAlexander V. Chernikov /* Free old buffers if set */
184c7b0200eSAlexander V. Chernikov if (d->bd_fbuf != NULL)
185c7b0200eSAlexander V. Chernikov free(d->bd_fbuf, M_BPF);
186c7b0200eSAlexander V. Chernikov if (d->bd_sbuf != NULL)
187c7b0200eSAlexander V. Chernikov free(d->bd_sbuf, M_BPF);
188c7b0200eSAlexander V. Chernikov
189c7b0200eSAlexander V. Chernikov /* Fill in new data */
1904d621040SChristian S.J. Peron d->bd_bufsize = size;
191c7b0200eSAlexander V. Chernikov d->bd_fbuf = fbuf;
192c7b0200eSAlexander V. Chernikov d->bd_sbuf = sbuf;
193c7b0200eSAlexander V. Chernikov
194c7b0200eSAlexander V. Chernikov d->bd_hbuf = NULL;
195c7b0200eSAlexander V. Chernikov d->bd_slen = 0;
196c7b0200eSAlexander V. Chernikov d->bd_hlen = 0;
197c7b0200eSAlexander V. Chernikov
198afa85850SAlexander V. Chernikov BPFD_UNLOCK(d);
1994d621040SChristian S.J. Peron return (0);
2004d621040SChristian S.J. Peron }
2014d621040SChristian S.J. Peron
2024d621040SChristian S.J. Peron /*
2034d621040SChristian S.J. Peron * Copy buffer storage to user space in read().
2044d621040SChristian S.J. Peron */
2054d621040SChristian S.J. Peron int
bpf_buffer_uiomove(struct bpf_d * d,caddr_t buf,u_int len,struct uio * uio)2064d621040SChristian S.J. Peron bpf_buffer_uiomove(struct bpf_d *d, caddr_t buf, u_int len, struct uio *uio)
2074d621040SChristian S.J. Peron {
2084d621040SChristian S.J. Peron
2094d621040SChristian S.J. Peron return (uiomove(buf, len, uio));
2104d621040SChristian S.J. Peron }
211