xref: /freebsd/sys/net/bpf.c (revision e20e5724e6145fd2cd922f11d745b10a048443af)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1990, 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  * Copyright (c) 2019 Andrey V. Elsukov <ae@FreeBSD.org>
7  *
8  * This code is derived from the Stanford/CMU enet packet filter,
9  * (net/enet.c) distributed as part of 4.3BSD, and code contributed
10  * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
11  * Berkeley Laboratory.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
38 #include <sys/cdefs.h>
39 #include "opt_bpf.h"
40 #include "opt_netgraph.h"
41 
42 #include <sys/param.h>
43 #include <sys/conf.h>
44 #include <sys/eventhandler.h>
45 #include <sys/fcntl.h>
46 #include <sys/jail.h>
47 #include <sys/ktr.h>
48 #include <sys/lock.h>
49 #include <sys/malloc.h>
50 #include <sys/mbuf.h>
51 #include <sys/mutex.h>
52 #include <sys/time.h>
53 #include <sys/priv.h>
54 #include <sys/proc.h>
55 #include <sys/signalvar.h>
56 #include <sys/filio.h>
57 #include <sys/sockio.h>
58 #include <sys/ttycom.h>
59 #include <sys/uio.h>
60 #include <sys/sysent.h>
61 #include <sys/systm.h>
62 
63 #include <sys/event.h>
64 #include <sys/file.h>
65 #include <sys/poll.h>
66 #include <sys/proc.h>
67 
68 #include <sys/socket.h>
69 
70 #include <net/if.h>
71 #include <net/if_var.h>
72 #include <net/if_private.h>
73 #include <net/if_vlan_var.h>
74 #include <net/if_dl.h>
75 #include <net/bpf.h>
76 #include <net/bpf_buffer.h>
77 #ifdef BPF_JITTER
78 #include <net/bpf_jitter.h>
79 #endif
80 #include <net/bpf_zerocopy.h>
81 #include <net/bpfdesc.h>
82 #include <net/route.h>
83 #include <net/vnet.h>
84 
85 #include <netinet/in.h>
86 #include <netinet/if_ether.h>
87 #include <sys/kernel.h>
88 #include <sys/sysctl.h>
89 
90 #include <net80211/ieee80211_freebsd.h>
91 
92 #include <security/mac/mac_framework.h>
93 
94 MALLOC_DEFINE(M_BPF, "BPF", "BPF data");
95 
96 static const struct bpf_if_ext dead_bpf_if = {
97 	.bif_dlist = CK_LIST_HEAD_INITIALIZER()
98 };
99 
100 struct bpf_if {
101 #define	bif_next	bif_ext.bif_next
102 #define	bif_dlist	bif_ext.bif_dlist
103 	struct bpf_if_ext bif_ext;	/* public members */
104 	u_int		bif_dlt;	/* link layer type */
105 	u_int		bif_hdrlen;	/* length of link header */
106 	struct bpfd_list bif_wlist;	/* writer-only list */
107 	struct ifnet	*bif_ifp;	/* corresponding interface */
108 	struct bpf_if	**bif_bpf;	/* Pointer to pointer to us */
109 	volatile u_int	bif_refcnt;
110 	struct epoch_context epoch_ctx;
111 };
112 
113 CTASSERT(offsetof(struct bpf_if, bif_ext) == 0);
114 
115 struct bpf_program_buffer {
116 	struct epoch_context	epoch_ctx;
117 #ifdef BPF_JITTER
118 	bpf_jit_filter		*func;
119 #endif
120 	void			*buffer[0];
121 };
122 
123 #if defined(DEV_BPF) || defined(NETGRAPH_BPF)
124 
125 #define PRINET  26			/* interruptible */
126 #define BPF_PRIO_MAX	7
127 
128 #define	SIZEOF_BPF_HDR(type)	\
129     (offsetof(type, bh_hdrlen) + sizeof(((type *)0)->bh_hdrlen))
130 
131 #ifdef COMPAT_FREEBSD32
132 #include <sys/mount.h>
133 #include <compat/freebsd32/freebsd32.h>
134 #define BPF_ALIGNMENT32 sizeof(int32_t)
135 #define	BPF_WORDALIGN32(x) roundup2(x, BPF_ALIGNMENT32)
136 
137 #ifndef BURN_BRIDGES
138 /*
139  * 32-bit version of structure prepended to each packet.  We use this header
140  * instead of the standard one for 32-bit streams.  We mark the a stream as
141  * 32-bit the first time we see a 32-bit compat ioctl request.
142  */
143 struct bpf_hdr32 {
144 	struct timeval32 bh_tstamp;	/* time stamp */
145 	uint32_t	bh_caplen;	/* length of captured portion */
146 	uint32_t	bh_datalen;	/* original length of packet */
147 	uint16_t	bh_hdrlen;	/* length of bpf header (this struct
148 					   plus alignment padding) */
149 };
150 #endif
151 
152 struct bpf_program32 {
153 	u_int bf_len;
154 	uint32_t bf_insns;
155 };
156 
157 struct bpf_dltlist32 {
158 	u_int	bfl_len;
159 	u_int	bfl_list;
160 };
161 
162 #define	BIOCSETF32	_IOW('B', 103, struct bpf_program32)
163 #define	BIOCSRTIMEOUT32	_IOW('B', 109, struct timeval32)
164 #define	BIOCGRTIMEOUT32	_IOR('B', 110, struct timeval32)
165 #define	BIOCGDLTLIST32	_IOWR('B', 121, struct bpf_dltlist32)
166 #define	BIOCSETWF32	_IOW('B', 123, struct bpf_program32)
167 #define	BIOCSETFNR32	_IOW('B', 130, struct bpf_program32)
168 #endif
169 
170 #define BPF_LOCK()		sx_xlock(&bpf_sx)
171 #define BPF_UNLOCK()		sx_xunlock(&bpf_sx)
172 #define BPF_LOCK_ASSERT()	sx_assert(&bpf_sx, SA_XLOCKED)
173 /*
174  * bpf_iflist is a list of BPF interface structures, each corresponding to a
175  * specific DLT. The same network interface might have several BPF interface
176  * structures registered by different layers in the stack (i.e., 802.11
177  * frames, ethernet frames, etc).
178  */
179 CK_LIST_HEAD(bpf_iflist, bpf_if);
180 static struct bpf_iflist bpf_iflist = CK_LIST_HEAD_INITIALIZER();
181 static struct sx	bpf_sx;		/* bpf global lock */
182 static int		bpf_bpfd_cnt;
183 
184 static void	bpfif_ref(struct bpf_if *);
185 static void	bpfif_rele(struct bpf_if *);
186 
187 static void	bpfd_ref(struct bpf_d *);
188 static void	bpfd_rele(struct bpf_d *);
189 static void	bpf_attachd(struct bpf_d *, struct bpf_if *);
190 static void	bpf_detachd(struct bpf_d *, bool);
191 static void	bpfd_free(epoch_context_t);
192 static int	bpf_movein(struct uio *, int, struct ifnet *, struct mbuf **,
193 		    struct sockaddr *, int *, struct bpf_d *);
194 static int	bpf_setif(struct bpf_d *, struct ifreq *);
195 static void	bpf_timed_out(void *);
196 static __inline void
197 		bpf_wakeup(struct bpf_d *);
198 static void	catchpacket(struct bpf_d *, u_char *, u_int, u_int,
199 		    void (*)(struct bpf_d *, caddr_t, u_int, void *, u_int),
200 		    struct bintime *);
201 static void	reset_d(struct bpf_d *);
202 static int	bpf_setf(struct bpf_d *, struct bpf_program *, u_long cmd);
203 static int	bpf_getdltlist(struct bpf_d *, struct bpf_dltlist *);
204 static int	bpf_setdlt(struct bpf_d *, u_int);
205 static void	filt_bpfdetach(struct knote *);
206 static int	filt_bpfread(struct knote *, long);
207 static int	filt_bpfwrite(struct knote *, long);
208 static void	bpf_drvinit(void *);
209 static int	bpf_stats_sysctl(SYSCTL_HANDLER_ARGS);
210 
211 SYSCTL_NODE(_net, OID_AUTO, bpf, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
212     "bpf sysctl");
213 int bpf_maxinsns = BPF_MAXINSNS;
214 SYSCTL_INT(_net_bpf, OID_AUTO, maxinsns, CTLFLAG_RW,
215     &bpf_maxinsns, 0, "Maximum bpf program instructions");
216 static int bpf_zerocopy_enable = 0;
217 SYSCTL_INT(_net_bpf, OID_AUTO, zerocopy_enable, CTLFLAG_RW,
218     &bpf_zerocopy_enable, 0, "Enable new zero-copy BPF buffer sessions");
219 static SYSCTL_NODE(_net_bpf, OID_AUTO, stats, CTLFLAG_MPSAFE | CTLFLAG_RW,
220     bpf_stats_sysctl, "bpf statistics portal");
221 
222 VNET_DEFINE_STATIC(int, bpf_optimize_writers) = 0;
223 #define	V_bpf_optimize_writers VNET(bpf_optimize_writers)
224 SYSCTL_INT(_net_bpf, OID_AUTO, optimize_writers, CTLFLAG_VNET | CTLFLAG_RWTUN,
225     &VNET_NAME(bpf_optimize_writers), 0,
226     "Do not send packets until BPF program is set");
227 
228 static	d_open_t	bpfopen;
229 static	d_read_t	bpfread;
230 static	d_write_t	bpfwrite;
231 static	d_ioctl_t	bpfioctl;
232 static	d_poll_t	bpfpoll;
233 static	d_kqfilter_t	bpfkqfilter;
234 
235 static struct cdevsw bpf_cdevsw = {
236 	.d_version =	D_VERSION,
237 	.d_open =	bpfopen,
238 	.d_read =	bpfread,
239 	.d_write =	bpfwrite,
240 	.d_ioctl =	bpfioctl,
241 	.d_poll =	bpfpoll,
242 	.d_name =	"bpf",
243 	.d_kqfilter =	bpfkqfilter,
244 };
245 
246 static const struct filterops bpfread_filtops = {
247 	.f_isfd = 1,
248 	.f_detach = filt_bpfdetach,
249 	.f_event = filt_bpfread,
250 	.f_copy = knote_triv_copy,
251 };
252 
253 static const struct filterops bpfwrite_filtops = {
254 	.f_isfd = 1,
255 	.f_detach = filt_bpfdetach,
256 	.f_event = filt_bpfwrite,
257 	.f_copy = knote_triv_copy,
258 };
259 
260 /*
261  * LOCKING MODEL USED BY BPF
262  *
263  * Locks:
264  * 1) global lock (BPF_LOCK). Sx, used to protect some global counters,
265  * every bpf_iflist changes, serializes ioctl access to bpf descriptors.
266  * 2) Descriptor lock. Mutex, used to protect BPF buffers and various
267  * structure fields used by bpf_*tap* code.
268  *
269  * Lock order: global lock, then descriptor lock.
270  *
271  * There are several possible consumers:
272  *
273  * 1. The kernel registers interface pointer with bpfattach().
274  * Each call allocates new bpf_if structure, references ifnet pointer
275  * and links bpf_if into bpf_iflist chain. This is protected with global
276  * lock.
277  *
278  * 2. An userland application uses ioctl() call to bpf_d descriptor.
279  * All such call are serialized with global lock. BPF filters can be
280  * changed, but pointer to old filter will be freed using NET_EPOCH_CALL().
281  * Thus it should be safe for bpf_tap/bpf_mtap* code to do access to
282  * filter pointers, even if change will happen during bpf_tap execution.
283  * Destroying of bpf_d descriptor also is doing using NET_EPOCH_CALL().
284  *
285  * 3. An userland application can write packets into bpf_d descriptor.
286  * There we need to be sure, that ifnet won't disappear during bpfwrite().
287  *
288  * 4. The kernel invokes bpf_tap/bpf_mtap* functions. The access to
289  * bif_dlist is protected with net_epoch_preempt section. So, it should
290  * be safe to make access to bpf_d descriptor inside the section.
291  *
292  * 5. The kernel invokes bpfdetach() on interface destroying. All lists
293  * are modified with global lock held and actual free() is done using
294  * NET_EPOCH_CALL().
295  */
296 
297 static void
bpfif_free(epoch_context_t ctx)298 bpfif_free(epoch_context_t ctx)
299 {
300 	struct bpf_if *bp;
301 
302 	bp = __containerof(ctx, struct bpf_if, epoch_ctx);
303 	if_rele(bp->bif_ifp);
304 	free(bp, M_BPF);
305 }
306 
307 static void
bpfif_ref(struct bpf_if * bp)308 bpfif_ref(struct bpf_if *bp)
309 {
310 
311 	refcount_acquire(&bp->bif_refcnt);
312 }
313 
314 static void
bpfif_rele(struct bpf_if * bp)315 bpfif_rele(struct bpf_if *bp)
316 {
317 
318 	if (!refcount_release(&bp->bif_refcnt))
319 		return;
320 	NET_EPOCH_CALL(bpfif_free, &bp->epoch_ctx);
321 }
322 
323 static void
bpfd_ref(struct bpf_d * d)324 bpfd_ref(struct bpf_d *d)
325 {
326 
327 	refcount_acquire(&d->bd_refcnt);
328 }
329 
330 static void
bpfd_rele(struct bpf_d * d)331 bpfd_rele(struct bpf_d *d)
332 {
333 
334 	if (!refcount_release(&d->bd_refcnt))
335 		return;
336 	NET_EPOCH_CALL(bpfd_free, &d->epoch_ctx);
337 }
338 
339 static struct bpf_program_buffer*
bpf_program_buffer_alloc(size_t size,int flags)340 bpf_program_buffer_alloc(size_t size, int flags)
341 {
342 
343 	return (malloc(sizeof(struct bpf_program_buffer) + size,
344 	    M_BPF, flags));
345 }
346 
347 static void
bpf_program_buffer_free(epoch_context_t ctx)348 bpf_program_buffer_free(epoch_context_t ctx)
349 {
350 	struct bpf_program_buffer *ptr;
351 
352 	ptr = __containerof(ctx, struct bpf_program_buffer, epoch_ctx);
353 #ifdef BPF_JITTER
354 	if (ptr->func != NULL)
355 		bpf_destroy_jit_filter(ptr->func);
356 #endif
357 	free(ptr, M_BPF);
358 }
359 
360 /*
361  * Wrapper functions for various buffering methods.  If the set of buffer
362  * modes expands, we will probably want to introduce a switch data structure
363  * similar to protosw, et.
364  */
365 static void
bpf_append_bytes(struct bpf_d * d,caddr_t buf,u_int offset,void * src,u_int len)366 bpf_append_bytes(struct bpf_d *d, caddr_t buf, u_int offset, void *src,
367     u_int len)
368 {
369 
370 	BPFD_LOCK_ASSERT(d);
371 
372 	switch (d->bd_bufmode) {
373 	case BPF_BUFMODE_BUFFER:
374 		return (bpf_buffer_append_bytes(d, buf, offset, src, len));
375 
376 	case BPF_BUFMODE_ZBUF:
377 		counter_u64_add(d->bd_zcopy, 1);
378 		return (bpf_zerocopy_append_bytes(d, buf, offset, src, len));
379 
380 	default:
381 		panic("bpf_buf_append_bytes");
382 	}
383 }
384 
385 static void
bpf_append_mbuf(struct bpf_d * d,caddr_t buf,u_int offset,void * src,u_int len)386 bpf_append_mbuf(struct bpf_d *d, caddr_t buf, u_int offset, void *src,
387     u_int len)
388 {
389 
390 	BPFD_LOCK_ASSERT(d);
391 
392 	switch (d->bd_bufmode) {
393 	case BPF_BUFMODE_BUFFER:
394 		return (bpf_buffer_append_mbuf(d, buf, offset, src, len));
395 
396 	case BPF_BUFMODE_ZBUF:
397 		counter_u64_add(d->bd_zcopy, 1);
398 		return (bpf_zerocopy_append_mbuf(d, buf, offset, src, len));
399 
400 	default:
401 		panic("bpf_buf_append_mbuf");
402 	}
403 }
404 
405 /*
406  * This function gets called when the free buffer is re-assigned.
407  */
408 static void
bpf_buf_reclaimed(struct bpf_d * d)409 bpf_buf_reclaimed(struct bpf_d *d)
410 {
411 
412 	BPFD_LOCK_ASSERT(d);
413 
414 	switch (d->bd_bufmode) {
415 	case BPF_BUFMODE_BUFFER:
416 		return;
417 
418 	case BPF_BUFMODE_ZBUF:
419 		bpf_zerocopy_buf_reclaimed(d);
420 		return;
421 
422 	default:
423 		panic("bpf_buf_reclaimed");
424 	}
425 }
426 
427 /*
428  * If the buffer mechanism has a way to decide that a held buffer can be made
429  * free, then it is exposed via the bpf_canfreebuf() interface.  (1) is
430  * returned if the buffer can be discarded, (0) is returned if it cannot.
431  */
432 static int
bpf_canfreebuf(struct bpf_d * d)433 bpf_canfreebuf(struct bpf_d *d)
434 {
435 
436 	BPFD_LOCK_ASSERT(d);
437 
438 	switch (d->bd_bufmode) {
439 	case BPF_BUFMODE_ZBUF:
440 		return (bpf_zerocopy_canfreebuf(d));
441 	}
442 	return (0);
443 }
444 
445 /*
446  * Allow the buffer model to indicate that the current store buffer is
447  * immutable, regardless of the appearance of space.  Return (1) if the
448  * buffer is writable, and (0) if not.
449  */
450 static int
bpf_canwritebuf(struct bpf_d * d)451 bpf_canwritebuf(struct bpf_d *d)
452 {
453 	BPFD_LOCK_ASSERT(d);
454 
455 	switch (d->bd_bufmode) {
456 	case BPF_BUFMODE_ZBUF:
457 		return (bpf_zerocopy_canwritebuf(d));
458 	}
459 	return (1);
460 }
461 
462 /*
463  * Notify buffer model that an attempt to write to the store buffer has
464  * resulted in a dropped packet, in which case the buffer may be considered
465  * full.
466  */
467 static void
bpf_buffull(struct bpf_d * d)468 bpf_buffull(struct bpf_d *d)
469 {
470 
471 	BPFD_LOCK_ASSERT(d);
472 
473 	switch (d->bd_bufmode) {
474 	case BPF_BUFMODE_ZBUF:
475 		bpf_zerocopy_buffull(d);
476 		break;
477 	}
478 }
479 
480 /*
481  * Notify the buffer model that a buffer has moved into the hold position.
482  */
483 void
bpf_bufheld(struct bpf_d * d)484 bpf_bufheld(struct bpf_d *d)
485 {
486 
487 	BPFD_LOCK_ASSERT(d);
488 
489 	switch (d->bd_bufmode) {
490 	case BPF_BUFMODE_ZBUF:
491 		bpf_zerocopy_bufheld(d);
492 		break;
493 	}
494 }
495 
496 static void
bpf_free(struct bpf_d * d)497 bpf_free(struct bpf_d *d)
498 {
499 
500 	switch (d->bd_bufmode) {
501 	case BPF_BUFMODE_BUFFER:
502 		return (bpf_buffer_free(d));
503 
504 	case BPF_BUFMODE_ZBUF:
505 		return (bpf_zerocopy_free(d));
506 
507 	default:
508 		panic("bpf_buf_free");
509 	}
510 }
511 
512 static int
bpf_uiomove(struct bpf_d * d,caddr_t buf,u_int len,struct uio * uio)513 bpf_uiomove(struct bpf_d *d, caddr_t buf, u_int len, struct uio *uio)
514 {
515 
516 	if (d->bd_bufmode != BPF_BUFMODE_BUFFER)
517 		return (EOPNOTSUPP);
518 	return (bpf_buffer_uiomove(d, buf, len, uio));
519 }
520 
521 static int
bpf_ioctl_sblen(struct bpf_d * d,u_int * i)522 bpf_ioctl_sblen(struct bpf_d *d, u_int *i)
523 {
524 
525 	if (d->bd_bufmode != BPF_BUFMODE_BUFFER)
526 		return (EOPNOTSUPP);
527 	return (bpf_buffer_ioctl_sblen(d, i));
528 }
529 
530 static int
bpf_ioctl_getzmax(struct thread * td,struct bpf_d * d,size_t * i)531 bpf_ioctl_getzmax(struct thread *td, struct bpf_d *d, size_t *i)
532 {
533 
534 	if (d->bd_bufmode != BPF_BUFMODE_ZBUF)
535 		return (EOPNOTSUPP);
536 	return (bpf_zerocopy_ioctl_getzmax(td, d, i));
537 }
538 
539 static int
bpf_ioctl_rotzbuf(struct thread * td,struct bpf_d * d,struct bpf_zbuf * bz)540 bpf_ioctl_rotzbuf(struct thread *td, struct bpf_d *d, struct bpf_zbuf *bz)
541 {
542 
543 	if (d->bd_bufmode != BPF_BUFMODE_ZBUF)
544 		return (EOPNOTSUPP);
545 	return (bpf_zerocopy_ioctl_rotzbuf(td, d, bz));
546 }
547 
548 static int
bpf_ioctl_setzbuf(struct thread * td,struct bpf_d * d,struct bpf_zbuf * bz)549 bpf_ioctl_setzbuf(struct thread *td, struct bpf_d *d, struct bpf_zbuf *bz)
550 {
551 
552 	if (d->bd_bufmode != BPF_BUFMODE_ZBUF)
553 		return (EOPNOTSUPP);
554 	return (bpf_zerocopy_ioctl_setzbuf(td, d, bz));
555 }
556 
557 /*
558  * General BPF functions.
559  */
560 static int
bpf_movein(struct uio * uio,int linktype,struct ifnet * ifp,struct mbuf ** mp,struct sockaddr * sockp,int * hdrlen,struct bpf_d * d)561 bpf_movein(struct uio *uio, int linktype, struct ifnet *ifp, struct mbuf **mp,
562     struct sockaddr *sockp, int *hdrlen, struct bpf_d *d)
563 {
564 	const struct ieee80211_bpf_params *p;
565 	struct ether_header *eh;
566 	struct mbuf *m;
567 	int error;
568 	int len;
569 	int hlen;
570 	int slen;
571 
572 	/*
573 	 * Build a sockaddr based on the data link layer type.
574 	 * We do this at this level because the ethernet header
575 	 * is copied directly into the data field of the sockaddr.
576 	 * In the case of SLIP, there is no header and the packet
577 	 * is forwarded as is.
578 	 * Also, we are careful to leave room at the front of the mbuf
579 	 * for the link level header.
580 	 */
581 	switch (linktype) {
582 	case DLT_SLIP:
583 		sockp->sa_family = AF_INET;
584 		hlen = 0;
585 		break;
586 
587 	case DLT_EN10MB:
588 		sockp->sa_family = AF_UNSPEC;
589 		/* XXX Would MAXLINKHDR be better? */
590 		hlen = ETHER_HDR_LEN;
591 		break;
592 
593 	case DLT_FDDI:
594 		sockp->sa_family = AF_IMPLINK;
595 		hlen = 0;
596 		break;
597 
598 	case DLT_RAW:
599 		sockp->sa_family = AF_UNSPEC;
600 		hlen = 0;
601 		break;
602 
603 	case DLT_NULL:
604 		/*
605 		 * null interface types require a 4 byte pseudo header which
606 		 * corresponds to the address family of the packet.
607 		 */
608 		sockp->sa_family = AF_UNSPEC;
609 		hlen = 4;
610 		break;
611 
612 	case DLT_ATM_RFC1483:
613 		/*
614 		 * en atm driver requires 4-byte atm pseudo header.
615 		 * though it isn't standard, vpi:vci needs to be
616 		 * specified anyway.
617 		 */
618 		sockp->sa_family = AF_UNSPEC;
619 		hlen = 12;	/* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
620 		break;
621 
622 	case DLT_PPP:
623 		sockp->sa_family = AF_UNSPEC;
624 		hlen = 4;	/* This should match PPP_HDRLEN */
625 		break;
626 
627 	case DLT_IEEE802_11:		/* IEEE 802.11 wireless */
628 		sockp->sa_family = AF_IEEE80211;
629 		hlen = 0;
630 		break;
631 
632 	case DLT_IEEE802_11_RADIO:	/* IEEE 802.11 wireless w/ phy params */
633 		sockp->sa_family = AF_IEEE80211;
634 		sockp->sa_len = 12;	/* XXX != 0 */
635 		hlen = sizeof(struct ieee80211_bpf_params);
636 		break;
637 
638 	default:
639 		return (EIO);
640 	}
641 
642 	len = uio->uio_resid;
643 	if (len < hlen || len - hlen > ifp->if_mtu)
644 		return (EMSGSIZE);
645 
646 	/* Allocate a mbuf, up to MJUM16BYTES bytes, for our write. */
647 	m = m_get3(len, M_WAITOK, MT_DATA, M_PKTHDR);
648 	if (m == NULL)
649 		return (EIO);
650 	m->m_pkthdr.len = m->m_len = len;
651 	*mp = m;
652 
653 	error = uiomove(mtod(m, u_char *), len, uio);
654 	if (error)
655 		goto bad;
656 
657 	slen = bpf_filter(d->bd_wfilter, mtod(m, u_char *), len, len);
658 	if (slen == 0) {
659 		error = EPERM;
660 		goto bad;
661 	}
662 
663 	/* Check for multicast destination */
664 	switch (linktype) {
665 	case DLT_EN10MB:
666 		eh = mtod(m, struct ether_header *);
667 		if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
668 			if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
669 			    ETHER_ADDR_LEN) == 0)
670 				m->m_flags |= M_BCAST;
671 			else
672 				m->m_flags |= M_MCAST;
673 		}
674 		if (d->bd_hdrcmplt == 0) {
675 			memcpy(eh->ether_shost, IF_LLADDR(ifp),
676 			    sizeof(eh->ether_shost));
677 		}
678 		break;
679 	}
680 
681 	/*
682 	 * Make room for link header, and copy it to sockaddr
683 	 */
684 	if (hlen != 0) {
685 		if (sockp->sa_family == AF_IEEE80211) {
686 			/*
687 			 * Collect true length from the parameter header
688 			 * NB: sockp is known to be zero'd so if we do a
689 			 *     short copy unspecified parameters will be
690 			 *     zero.
691 			 * NB: packet may not be aligned after stripping
692 			 *     bpf params
693 			 * XXX check ibp_vers
694 			 */
695 			p = mtod(m, const struct ieee80211_bpf_params *);
696 			hlen = p->ibp_len;
697 			if (hlen > sizeof(sockp->sa_data)) {
698 				error = EINVAL;
699 				goto bad;
700 			}
701 		}
702 		bcopy(mtod(m, const void *), sockp->sa_data, hlen);
703 	}
704 	*hdrlen = hlen;
705 
706 	return (0);
707 bad:
708 	m_freem(m);
709 	return (error);
710 }
711 
712 /*
713  * Attach descriptor to the bpf interface, i.e. make d listen on bp,
714  * then reset its buffers and counters with reset_d().
715  */
716 static void
bpf_attachd(struct bpf_d * d,struct bpf_if * bp)717 bpf_attachd(struct bpf_d *d, struct bpf_if *bp)
718 {
719 	int op_w;
720 
721 	BPF_LOCK_ASSERT();
722 
723 	/*
724 	 * Save sysctl value to protect from sysctl change
725 	 * between reads
726 	 */
727 	op_w = V_bpf_optimize_writers || d->bd_writer;
728 
729 	if (d->bd_bif != NULL)
730 		bpf_detachd(d, false);
731 	/*
732 	 * Point d at bp, and add d to the interface's list.
733 	 * Since there are many applications using BPF for
734 	 * sending raw packets only (dhcpd, cdpd are good examples)
735 	 * we can delay adding d to the list of active listeners until
736 	 * some filter is configured.
737 	 */
738 
739 	BPFD_LOCK(d);
740 	/*
741 	 * Hold reference to bpif while descriptor uses this interface.
742 	 */
743 	bpfif_ref(bp);
744 	d->bd_bif = bp;
745 	if (op_w != 0) {
746 		/* Add to writers-only list */
747 		CK_LIST_INSERT_HEAD(&bp->bif_wlist, d, bd_next);
748 		/*
749 		 * We decrement bd_writer on every filter set operation.
750 		 * First BIOCSETF is done by pcap_open_live() to set up
751 		 * snap length. After that appliation usually sets its own
752 		 * filter.
753 		 */
754 		d->bd_writer = 2;
755 	} else
756 		CK_LIST_INSERT_HEAD(&bp->bif_dlist, d, bd_next);
757 
758 	reset_d(d);
759 
760 	/* Trigger EVFILT_WRITE events. */
761 	bpf_wakeup(d);
762 
763 	BPFD_UNLOCK(d);
764 	bpf_bpfd_cnt++;
765 
766 	CTR3(KTR_NET, "%s: bpf_attach called by pid %d, adding to %s list",
767 	    __func__, d->bd_pid, d->bd_writer ? "writer" : "active");
768 
769 	if (op_w == 0)
770 		EVENTHANDLER_INVOKE(bpf_track, bp->bif_ifp, bp->bif_dlt, 1);
771 }
772 
773 /*
774  * Check if we need to upgrade our descriptor @d from write-only mode.
775  */
776 static int
bpf_check_upgrade(u_long cmd,struct bpf_d * d,struct bpf_insn * fcode,int flen)777 bpf_check_upgrade(u_long cmd, struct bpf_d *d, struct bpf_insn *fcode,
778     int flen)
779 {
780 	int is_snap, need_upgrade;
781 
782 	/*
783 	 * Check if we've already upgraded or new filter is empty.
784 	 */
785 	if (d->bd_writer == 0 || fcode == NULL)
786 		return (0);
787 
788 	need_upgrade = 0;
789 
790 	/*
791 	 * Check if cmd looks like snaplen setting from
792 	 * pcap_bpf.c:pcap_open_live().
793 	 * Note we're not checking .k value here:
794 	 * while pcap_open_live() definitely sets to non-zero value,
795 	 * we'd prefer to treat k=0 (deny ALL) case the same way: e.g.
796 	 * do not consider upgrading immediately
797 	 */
798 	if (cmd == BIOCSETF && flen == 1 &&
799 	    fcode[0].code == (BPF_RET | BPF_K))
800 		is_snap = 1;
801 	else
802 		is_snap = 0;
803 
804 	if (is_snap == 0) {
805 		/*
806 		 * We're setting first filter and it doesn't look like
807 		 * setting snaplen.  We're probably using bpf directly.
808 		 * Upgrade immediately.
809 		 */
810 		need_upgrade = 1;
811 	} else {
812 		/*
813 		 * Do not require upgrade by first BIOCSETF
814 		 * (used to set snaplen) by pcap_open_live().
815 		 */
816 
817 		if (--d->bd_writer == 0) {
818 			/*
819 			 * First snaplen filter has already
820 			 * been set. This is probably catch-all
821 			 * filter
822 			 */
823 			need_upgrade = 1;
824 		}
825 	}
826 
827 	CTR5(KTR_NET,
828 	    "%s: filter function set by pid %d, "
829 	    "bd_writer counter %d, snap %d upgrade %d",
830 	    __func__, d->bd_pid, d->bd_writer,
831 	    is_snap, need_upgrade);
832 
833 	return (need_upgrade);
834 }
835 
836 /*
837  * Detach a file from its interface.
838  */
839 static void
bpf_detachd(struct bpf_d * d,bool detached_ifp)840 bpf_detachd(struct bpf_d *d, bool detached_ifp)
841 {
842 	struct bpf_if *bp;
843 	struct ifnet *ifp;
844 	int error;
845 
846 	BPF_LOCK_ASSERT();
847 	CTR2(KTR_NET, "%s: detach required by pid %d", __func__, d->bd_pid);
848 
849 	/* Check if descriptor is attached */
850 	if ((bp = d->bd_bif) == NULL)
851 		return;
852 
853 	BPFD_LOCK(d);
854 	/* Remove d from the interface's descriptor list. */
855 	CK_LIST_REMOVE(d, bd_next);
856 	/* Save bd_writer value */
857 	error = d->bd_writer;
858 	ifp = bp->bif_ifp;
859 	d->bd_bif = NULL;
860 	if (detached_ifp) {
861 		/*
862 		 * Notify descriptor as it's detached, so that any
863 		 * sleepers wake up and get ENXIO.
864 		 */
865 		bpf_wakeup(d);
866 	}
867 	BPFD_UNLOCK(d);
868 	bpf_bpfd_cnt--;
869 
870 	/* Call event handler iff d is attached */
871 	if (error == 0)
872 		EVENTHANDLER_INVOKE(bpf_track, ifp, bp->bif_dlt, 0);
873 
874 	/*
875 	 * Check if this descriptor had requested promiscuous mode.
876 	 * If so and ifnet is not detached, turn it off.
877 	 */
878 	if (d->bd_promisc && !detached_ifp) {
879 		d->bd_promisc = 0;
880 		CURVNET_SET(ifp->if_vnet);
881 		error = ifpromisc(ifp, 0);
882 		CURVNET_RESTORE();
883 		if (error != 0 && error != ENXIO) {
884 			/*
885 			 * ENXIO can happen if a pccard is unplugged
886 			 * Something is really wrong if we were able to put
887 			 * the driver into promiscuous mode, but can't
888 			 * take it out.
889 			 */
890 			if_printf(bp->bif_ifp,
891 				"bpf_detach: ifpromisc failed (%d)\n", error);
892 		}
893 	}
894 	bpfif_rele(bp);
895 }
896 
897 /*
898  * Close the descriptor by detaching it from its interface,
899  * deallocating its buffers, and marking it free.
900  */
901 static void
bpf_dtor(void * data)902 bpf_dtor(void *data)
903 {
904 	struct bpf_d *d = data;
905 
906 	BPFD_LOCK(d);
907 	if (d->bd_state == BPF_WAITING)
908 		callout_stop(&d->bd_callout);
909 	d->bd_state = BPF_IDLE;
910 	BPFD_UNLOCK(d);
911 	funsetown(&d->bd_sigio);
912 	BPF_LOCK();
913 	bpf_detachd(d, false);
914 	BPF_UNLOCK();
915 #ifdef MAC
916 	mac_bpfdesc_destroy(d);
917 #endif /* MAC */
918 	seldrain(&d->bd_sel);
919 	knlist_destroy(&d->bd_sel.si_note);
920 	callout_drain(&d->bd_callout);
921 	bpfd_rele(d);
922 }
923 
924 /*
925  * Open ethernet device.  Returns ENXIO for illegal minor device number,
926  * EBUSY if file is open by another process.
927  */
928 /* ARGSUSED */
929 static	int
bpfopen(struct cdev * dev,int flags,int fmt,struct thread * td)930 bpfopen(struct cdev *dev, int flags, int fmt, struct thread *td)
931 {
932 	struct bpf_d *d;
933 	int error;
934 
935 	d = malloc(sizeof(*d), M_BPF, M_WAITOK | M_ZERO);
936 	error = devfs_set_cdevpriv(d, bpf_dtor);
937 	if (error != 0) {
938 		free(d, M_BPF);
939 		return (error);
940 	}
941 
942 	/* Setup counters */
943 	d->bd_rcount = counter_u64_alloc(M_WAITOK);
944 	d->bd_dcount = counter_u64_alloc(M_WAITOK);
945 	d->bd_fcount = counter_u64_alloc(M_WAITOK);
946 	d->bd_wcount = counter_u64_alloc(M_WAITOK);
947 	d->bd_wfcount = counter_u64_alloc(M_WAITOK);
948 	d->bd_wdcount = counter_u64_alloc(M_WAITOK);
949 	d->bd_zcopy = counter_u64_alloc(M_WAITOK);
950 
951 	/*
952 	 * For historical reasons, perform a one-time initialization call to
953 	 * the buffer routines, even though we're not yet committed to a
954 	 * particular buffer method.
955 	 */
956 	bpf_buffer_init(d);
957 	if ((flags & FREAD) == 0)
958 		d->bd_writer = 2;
959 	d->bd_hbuf_in_use = 0;
960 	d->bd_bufmode = BPF_BUFMODE_BUFFER;
961 	d->bd_sig = SIGIO;
962 	d->bd_direction = BPF_D_INOUT;
963 	refcount_init(&d->bd_refcnt, 1);
964 	BPF_PID_REFRESH(d, td);
965 #ifdef MAC
966 	mac_bpfdesc_init(d);
967 	mac_bpfdesc_create(td->td_ucred, d);
968 #endif
969 	mtx_init(&d->bd_lock, devtoname(dev), "bpf cdev lock", MTX_DEF);
970 	callout_init_mtx(&d->bd_callout, &d->bd_lock, 0);
971 	knlist_init_mtx(&d->bd_sel.si_note, &d->bd_lock);
972 
973 	/* Disable VLAN pcp tagging. */
974 	d->bd_pcp = 0;
975 
976 	return (0);
977 }
978 
979 /*
980  *  bpfread - read next chunk of packets from buffers
981  */
982 static	int
bpfread(struct cdev * dev,struct uio * uio,int ioflag)983 bpfread(struct cdev *dev, struct uio *uio, int ioflag)
984 {
985 	struct bpf_d *d;
986 	int error;
987 	int non_block;
988 	int timed_out;
989 
990 	error = devfs_get_cdevpriv((void **)&d);
991 	if (error != 0)
992 		return (error);
993 
994 	/*
995 	 * Restrict application to use a buffer the same size as
996 	 * as kernel buffers.
997 	 */
998 	if (uio->uio_resid != d->bd_bufsize)
999 		return (EINVAL);
1000 
1001 	non_block = ((ioflag & O_NONBLOCK) != 0);
1002 
1003 	BPFD_LOCK(d);
1004 	BPF_PID_REFRESH_CUR(d);
1005 	if (d->bd_bufmode != BPF_BUFMODE_BUFFER) {
1006 		BPFD_UNLOCK(d);
1007 		return (EOPNOTSUPP);
1008 	}
1009 	if (d->bd_state == BPF_WAITING)
1010 		callout_stop(&d->bd_callout);
1011 	timed_out = (d->bd_state == BPF_TIMED_OUT);
1012 	d->bd_state = BPF_IDLE;
1013 	while (d->bd_hbuf_in_use) {
1014 		error = mtx_sleep(&d->bd_hbuf_in_use, &d->bd_lock,
1015 		    PRINET | PCATCH, "bd_hbuf", 0);
1016 		if (error != 0) {
1017 			BPFD_UNLOCK(d);
1018 			return (error);
1019 		}
1020 	}
1021 	/*
1022 	 * If the hold buffer is empty, then do a timed sleep, which
1023 	 * ends when the timeout expires or when enough packets
1024 	 * have arrived to fill the store buffer.
1025 	 */
1026 	while (d->bd_hbuf == NULL) {
1027 		if (d->bd_slen != 0) {
1028 			/*
1029 			 * A packet(s) either arrived since the previous
1030 			 * read or arrived while we were asleep.
1031 			 */
1032 			if (d->bd_immediate || non_block || timed_out) {
1033 				/*
1034 				 * Rotate the buffers and return what's here
1035 				 * if we are in immediate mode, non-blocking
1036 				 * flag is set, or this descriptor timed out.
1037 				 */
1038 				ROTATE_BUFFERS(d);
1039 				break;
1040 			}
1041 		}
1042 
1043 		/*
1044 		 * No data is available, check to see if the bpf device
1045 		 * is still pointed at a real interface.  If not, return
1046 		 * ENXIO so that the userland process knows to rebind
1047 		 * it before using it again.
1048 		 */
1049 		if (d->bd_bif == NULL) {
1050 			BPFD_UNLOCK(d);
1051 			return (ENXIO);
1052 		}
1053 
1054 		if (non_block) {
1055 			BPFD_UNLOCK(d);
1056 			return (EWOULDBLOCK);
1057 		}
1058 		error = msleep(d, &d->bd_lock, PRINET | PCATCH,
1059 		     "bpf", d->bd_rtout);
1060 		if (error == EINTR || error == ERESTART) {
1061 			BPFD_UNLOCK(d);
1062 			return (error);
1063 		}
1064 		if (error == EWOULDBLOCK) {
1065 			/*
1066 			 * On a timeout, return what's in the buffer,
1067 			 * which may be nothing.  If there is something
1068 			 * in the store buffer, we can rotate the buffers.
1069 			 */
1070 			if (d->bd_hbuf)
1071 				/*
1072 				 * We filled up the buffer in between
1073 				 * getting the timeout and arriving
1074 				 * here, so we don't need to rotate.
1075 				 */
1076 				break;
1077 
1078 			if (d->bd_slen == 0) {
1079 				BPFD_UNLOCK(d);
1080 				return (0);
1081 			}
1082 			ROTATE_BUFFERS(d);
1083 			break;
1084 		}
1085 	}
1086 	/*
1087 	 * At this point, we know we have something in the hold slot.
1088 	 */
1089 	d->bd_hbuf_in_use = 1;
1090 	BPFD_UNLOCK(d);
1091 
1092 	/*
1093 	 * Move data from hold buffer into user space.
1094 	 * We know the entire buffer is transferred since
1095 	 * we checked above that the read buffer is bpf_bufsize bytes.
1096   	 *
1097 	 * We do not have to worry about simultaneous reads because
1098 	 * we waited for sole access to the hold buffer above.
1099 	 */
1100 	error = bpf_uiomove(d, d->bd_hbuf, d->bd_hlen, uio);
1101 
1102 	BPFD_LOCK(d);
1103 	if (d->bd_hbuf_in_use) {
1104 		KASSERT(d->bd_hbuf != NULL, ("bpfread: lost bd_hbuf"));
1105 		d->bd_fbuf = d->bd_hbuf;
1106 		d->bd_hbuf = NULL;
1107 		d->bd_hlen = 0;
1108 		bpf_buf_reclaimed(d);
1109 		d->bd_hbuf_in_use = 0;
1110 		wakeup(&d->bd_hbuf_in_use);
1111 	}
1112 	BPFD_UNLOCK(d);
1113 
1114 	return (error);
1115 }
1116 
1117 /*
1118  * If there are processes sleeping on this descriptor, wake them up.
1119  */
1120 static __inline void
bpf_wakeup(struct bpf_d * d)1121 bpf_wakeup(struct bpf_d *d)
1122 {
1123 
1124 	BPFD_LOCK_ASSERT(d);
1125 	if (d->bd_state == BPF_WAITING) {
1126 		callout_stop(&d->bd_callout);
1127 		d->bd_state = BPF_IDLE;
1128 	}
1129 	wakeup(d);
1130 	if (d->bd_async && d->bd_sig && d->bd_sigio)
1131 		pgsigio(&d->bd_sigio, d->bd_sig, 0);
1132 
1133 	selwakeuppri(&d->bd_sel, PRINET);
1134 	KNOTE_LOCKED(&d->bd_sel.si_note, 0);
1135 }
1136 
1137 static void
bpf_timed_out(void * arg)1138 bpf_timed_out(void *arg)
1139 {
1140 	struct bpf_d *d = (struct bpf_d *)arg;
1141 
1142 	BPFD_LOCK_ASSERT(d);
1143 
1144 	if (callout_pending(&d->bd_callout) ||
1145 	    !callout_active(&d->bd_callout))
1146 		return;
1147 	if (d->bd_state == BPF_WAITING) {
1148 		d->bd_state = BPF_TIMED_OUT;
1149 		if (d->bd_slen != 0)
1150 			bpf_wakeup(d);
1151 	}
1152 }
1153 
1154 static int
bpf_ready(struct bpf_d * d)1155 bpf_ready(struct bpf_d *d)
1156 {
1157 
1158 	BPFD_LOCK_ASSERT(d);
1159 
1160 	if (!bpf_canfreebuf(d) && d->bd_hlen != 0)
1161 		return (1);
1162 	if ((d->bd_immediate || d->bd_state == BPF_TIMED_OUT) &&
1163 	    d->bd_slen != 0)
1164 		return (1);
1165 	return (0);
1166 }
1167 
1168 static int
bpfwrite(struct cdev * dev,struct uio * uio,int ioflag)1169 bpfwrite(struct cdev *dev, struct uio *uio, int ioflag)
1170 {
1171 	struct route ro;
1172 	struct sockaddr dst;
1173 	struct epoch_tracker et;
1174 	struct bpf_if *bp;
1175 	struct bpf_d *d;
1176 	struct ifnet *ifp;
1177 	struct mbuf *m, *mc;
1178 	int error, hlen;
1179 
1180 	error = devfs_get_cdevpriv((void **)&d);
1181 	if (error != 0)
1182 		return (error);
1183 
1184 	NET_EPOCH_ENTER(et);
1185 	BPFD_LOCK(d);
1186 	BPF_PID_REFRESH_CUR(d);
1187 	counter_u64_add(d->bd_wcount, 1);
1188 	if ((bp = d->bd_bif) == NULL) {
1189 		error = ENXIO;
1190 		goto out_locked;
1191 	}
1192 
1193 	ifp = bp->bif_ifp;
1194 	if ((ifp->if_flags & IFF_UP) == 0) {
1195 		error = ENETDOWN;
1196 		goto out_locked;
1197 	}
1198 
1199 	if (uio->uio_resid == 0)
1200 		goto out_locked;
1201 
1202 	bzero(&dst, sizeof(dst));
1203 	m = NULL;
1204 	hlen = 0;
1205 
1206 	/*
1207 	 * Take extra reference, unlock d and exit from epoch section,
1208 	 * since bpf_movein() can sleep.
1209 	 */
1210 	bpfd_ref(d);
1211 	NET_EPOCH_EXIT(et);
1212 	BPFD_UNLOCK(d);
1213 
1214 	error = bpf_movein(uio, (int)bp->bif_dlt, ifp,
1215 	    &m, &dst, &hlen, d);
1216 
1217 	if (error != 0) {
1218 		counter_u64_add(d->bd_wdcount, 1);
1219 		bpfd_rele(d);
1220 		return (error);
1221 	}
1222 
1223 	BPFD_LOCK(d);
1224 	/*
1225 	 * Check that descriptor is still attached to the interface.
1226 	 * This can happen on bpfdetach(). To avoid access to detached
1227 	 * ifnet, free mbuf and return ENXIO.
1228 	 */
1229 	if (d->bd_bif == NULL) {
1230 		counter_u64_add(d->bd_wdcount, 1);
1231 		BPFD_UNLOCK(d);
1232 		bpfd_rele(d);
1233 		m_freem(m);
1234 		return (ENXIO);
1235 	}
1236 	counter_u64_add(d->bd_wfcount, 1);
1237 	if (d->bd_hdrcmplt)
1238 		dst.sa_family = pseudo_AF_HDRCMPLT;
1239 
1240 	if (d->bd_feedback) {
1241 		mc = m_dup(m, M_NOWAIT);
1242 		if (mc != NULL)
1243 			mc->m_pkthdr.rcvif = ifp;
1244 		/* Set M_PROMISC for outgoing packets to be discarded. */
1245 		if (d->bd_direction == BPF_D_INOUT)
1246 			m->m_flags |= M_PROMISC;
1247 	} else
1248 		mc = NULL;
1249 
1250 	m->m_pkthdr.len -= hlen;
1251 	m->m_len -= hlen;
1252 	m->m_data += hlen;	/* XXX */
1253 
1254 	CURVNET_SET(ifp->if_vnet);
1255 #ifdef MAC
1256 	mac_bpfdesc_create_mbuf(d, m);
1257 	if (mc != NULL)
1258 		mac_bpfdesc_create_mbuf(d, mc);
1259 #endif
1260 
1261 	bzero(&ro, sizeof(ro));
1262 	if (hlen != 0) {
1263 		ro.ro_prepend = (u_char *)&dst.sa_data;
1264 		ro.ro_plen = hlen;
1265 		ro.ro_flags = RT_HAS_HEADER;
1266 	}
1267 
1268 	if (d->bd_pcp != 0)
1269 		vlan_set_pcp(m, d->bd_pcp);
1270 
1271 	/* Avoid possible recursion on BPFD_LOCK(). */
1272 	NET_EPOCH_ENTER(et);
1273 	BPFD_UNLOCK(d);
1274 	error = (*ifp->if_output)(ifp, m, &dst, &ro);
1275 	if (error)
1276 		counter_u64_add(d->bd_wdcount, 1);
1277 
1278 	if (mc != NULL) {
1279 		if (error == 0)
1280 			(*ifp->if_input)(ifp, mc);
1281 		else
1282 			m_freem(mc);
1283 	}
1284 	NET_EPOCH_EXIT(et);
1285 	CURVNET_RESTORE();
1286 	bpfd_rele(d);
1287 	return (error);
1288 
1289 out_locked:
1290 	counter_u64_add(d->bd_wdcount, 1);
1291 	NET_EPOCH_EXIT(et);
1292 	BPFD_UNLOCK(d);
1293 	return (error);
1294 }
1295 
1296 /*
1297  * Reset a descriptor by flushing its packet buffer and clearing the receive
1298  * and drop counts.  This is doable for kernel-only buffers, but with
1299  * zero-copy buffers, we can't write to (or rotate) buffers that are
1300  * currently owned by userspace.  It would be nice if we could encapsulate
1301  * this logic in the buffer code rather than here.
1302  */
1303 static void
reset_d(struct bpf_d * d)1304 reset_d(struct bpf_d *d)
1305 {
1306 
1307 	BPFD_LOCK_ASSERT(d);
1308 
1309 	while (d->bd_hbuf_in_use)
1310 		mtx_sleep(&d->bd_hbuf_in_use, &d->bd_lock, PRINET,
1311 		    "bd_hbuf", 0);
1312 	if ((d->bd_hbuf != NULL) &&
1313 	    (d->bd_bufmode != BPF_BUFMODE_ZBUF || bpf_canfreebuf(d))) {
1314 		/* Free the hold buffer. */
1315 		d->bd_fbuf = d->bd_hbuf;
1316 		d->bd_hbuf = NULL;
1317 		d->bd_hlen = 0;
1318 		bpf_buf_reclaimed(d);
1319 	}
1320 	if (bpf_canwritebuf(d))
1321 		d->bd_slen = 0;
1322 	counter_u64_zero(d->bd_rcount);
1323 	counter_u64_zero(d->bd_dcount);
1324 	counter_u64_zero(d->bd_fcount);
1325 	counter_u64_zero(d->bd_wcount);
1326 	counter_u64_zero(d->bd_wfcount);
1327 	counter_u64_zero(d->bd_wdcount);
1328 	counter_u64_zero(d->bd_zcopy);
1329 }
1330 
1331 /*
1332  *  FIONREAD		Check for read packet available.
1333  *  BIOCGBLEN		Get buffer len [for read()].
1334  *  BIOCSETF		Set read filter.
1335  *  BIOCSETFNR		Set read filter without resetting descriptor.
1336  *  BIOCSETWF		Set write filter.
1337  *  BIOCFLUSH		Flush read packet buffer.
1338  *  BIOCPROMISC		Put interface into promiscuous mode.
1339  *  BIOCGDLT		Get link layer type.
1340  *  BIOCGETIF		Get interface name.
1341  *  BIOCSETIF		Set interface.
1342  *  BIOCSRTIMEOUT	Set read timeout.
1343  *  BIOCGRTIMEOUT	Get read timeout.
1344  *  BIOCGSTATS		Get packet stats.
1345  *  BIOCIMMEDIATE	Set immediate mode.
1346  *  BIOCVERSION		Get filter language version.
1347  *  BIOCGHDRCMPLT	Get "header already complete" flag
1348  *  BIOCSHDRCMPLT	Set "header already complete" flag
1349  *  BIOCGDIRECTION	Get packet direction flag
1350  *  BIOCSDIRECTION	Set packet direction flag
1351  *  BIOCGTSTAMP		Get time stamp format and resolution.
1352  *  BIOCSTSTAMP		Set time stamp format and resolution.
1353  *  BIOCLOCK		Set "locked" flag
1354  *  BIOCFEEDBACK	Set packet feedback mode.
1355  *  BIOCSETZBUF		Set current zero-copy buffer locations.
1356  *  BIOCGETZMAX		Get maximum zero-copy buffer size.
1357  *  BIOCROTZBUF		Force rotation of zero-copy buffer
1358  *  BIOCSETBUFMODE	Set buffer mode.
1359  *  BIOCGETBUFMODE	Get current buffer mode.
1360  *  BIOCSETVLANPCP	Set VLAN PCP tag.
1361  */
1362 /* ARGSUSED */
1363 static	int
bpfioctl(struct cdev * dev,u_long cmd,caddr_t addr,int flags,struct thread * td)1364 bpfioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags,
1365     struct thread *td)
1366 {
1367 	struct bpf_d *d;
1368 	int error;
1369 
1370 	error = devfs_get_cdevpriv((void **)&d);
1371 	if (error != 0)
1372 		return (error);
1373 
1374 	/*
1375 	 * Refresh PID associated with this descriptor.
1376 	 */
1377 	BPFD_LOCK(d);
1378 	BPF_PID_REFRESH(d, td);
1379 	if (d->bd_state == BPF_WAITING)
1380 		callout_stop(&d->bd_callout);
1381 	d->bd_state = BPF_IDLE;
1382 	BPFD_UNLOCK(d);
1383 
1384 	if (d->bd_locked == 1) {
1385 		switch (cmd) {
1386 		case BIOCGBLEN:
1387 		case BIOCFLUSH:
1388 		case BIOCGDLT:
1389 		case BIOCGDLTLIST:
1390 #ifdef COMPAT_FREEBSD32
1391 		case BIOCGDLTLIST32:
1392 #endif
1393 		case BIOCGETIF:
1394 		case BIOCGRTIMEOUT:
1395 #if defined(COMPAT_FREEBSD32) && defined(__amd64__)
1396 		case BIOCGRTIMEOUT32:
1397 #endif
1398 		case BIOCGSTATS:
1399 		case BIOCVERSION:
1400 		case BIOCGRSIG:
1401 		case BIOCGHDRCMPLT:
1402 		case BIOCSTSTAMP:
1403 		case BIOCFEEDBACK:
1404 		case FIONREAD:
1405 		case BIOCLOCK:
1406 		case BIOCSRTIMEOUT:
1407 #if defined(COMPAT_FREEBSD32) && defined(__amd64__)
1408 		case BIOCSRTIMEOUT32:
1409 #endif
1410 		case BIOCIMMEDIATE:
1411 		case TIOCGPGRP:
1412 		case BIOCROTZBUF:
1413 			break;
1414 		default:
1415 			return (EPERM);
1416 		}
1417 	}
1418 #ifdef COMPAT_FREEBSD32
1419 	/*
1420 	 * If we see a 32-bit compat ioctl, mark the stream as 32-bit so
1421 	 * that it will get 32-bit packet headers.
1422 	 */
1423 	switch (cmd) {
1424 	case BIOCSETF32:
1425 	case BIOCSETFNR32:
1426 	case BIOCSETWF32:
1427 	case BIOCGDLTLIST32:
1428 	case BIOCGRTIMEOUT32:
1429 	case BIOCSRTIMEOUT32:
1430 		if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
1431 			BPFD_LOCK(d);
1432 			d->bd_compat32 = 1;
1433 			BPFD_UNLOCK(d);
1434 		}
1435 	}
1436 #endif
1437 
1438 	CURVNET_SET(TD_TO_VNET(td));
1439 	switch (cmd) {
1440 	default:
1441 		error = EINVAL;
1442 		break;
1443 
1444 	/*
1445 	 * Check for read packet available.
1446 	 */
1447 	case FIONREAD:
1448 		{
1449 			int n;
1450 
1451 			BPFD_LOCK(d);
1452 			n = d->bd_slen;
1453 			while (d->bd_hbuf_in_use)
1454 				mtx_sleep(&d->bd_hbuf_in_use, &d->bd_lock,
1455 				    PRINET, "bd_hbuf", 0);
1456 			if (d->bd_hbuf)
1457 				n += d->bd_hlen;
1458 			BPFD_UNLOCK(d);
1459 
1460 			*(int *)addr = n;
1461 			break;
1462 		}
1463 
1464 	/*
1465 	 * Get buffer len [for read()].
1466 	 */
1467 	case BIOCGBLEN:
1468 		BPFD_LOCK(d);
1469 		*(u_int *)addr = d->bd_bufsize;
1470 		BPFD_UNLOCK(d);
1471 		break;
1472 
1473 	/*
1474 	 * Set buffer length.
1475 	 */
1476 	case BIOCSBLEN:
1477 		error = bpf_ioctl_sblen(d, (u_int *)addr);
1478 		break;
1479 
1480 	/*
1481 	 * Set link layer read filter.
1482 	 */
1483 	case BIOCSETF:
1484 	case BIOCSETFNR:
1485 	case BIOCSETWF:
1486 #ifdef COMPAT_FREEBSD32
1487 	case BIOCSETF32:
1488 	case BIOCSETFNR32:
1489 	case BIOCSETWF32:
1490 #endif
1491 		error = bpf_setf(d, (struct bpf_program *)addr, cmd);
1492 		break;
1493 
1494 	/*
1495 	 * Flush read packet buffer.
1496 	 */
1497 	case BIOCFLUSH:
1498 		BPFD_LOCK(d);
1499 		reset_d(d);
1500 		BPFD_UNLOCK(d);
1501 		break;
1502 
1503 	/*
1504 	 * Put interface into promiscuous mode.
1505 	 */
1506 	case BIOCPROMISC:
1507 		BPF_LOCK();
1508 		if (d->bd_bif == NULL) {
1509 			/*
1510 			 * No interface attached yet.
1511 			 */
1512 			error = EINVAL;
1513 		} else if (d->bd_promisc == 0) {
1514 			error = ifpromisc(d->bd_bif->bif_ifp, 1);
1515 			if (error == 0)
1516 				d->bd_promisc = 1;
1517 		}
1518 		BPF_UNLOCK();
1519 		break;
1520 
1521 	/*
1522 	 * Get current data link type.
1523 	 */
1524 	case BIOCGDLT:
1525 		BPF_LOCK();
1526 		if (d->bd_bif == NULL)
1527 			error = EINVAL;
1528 		else
1529 			*(u_int *)addr = d->bd_bif->bif_dlt;
1530 		BPF_UNLOCK();
1531 		break;
1532 
1533 	/*
1534 	 * Get a list of supported data link types.
1535 	 */
1536 #ifdef COMPAT_FREEBSD32
1537 	case BIOCGDLTLIST32:
1538 		{
1539 			struct bpf_dltlist32 *list32;
1540 			struct bpf_dltlist dltlist;
1541 
1542 			list32 = (struct bpf_dltlist32 *)addr;
1543 			dltlist.bfl_len = list32->bfl_len;
1544 			dltlist.bfl_list = PTRIN(list32->bfl_list);
1545 			BPF_LOCK();
1546 			if (d->bd_bif == NULL)
1547 				error = EINVAL;
1548 			else {
1549 				error = bpf_getdltlist(d, &dltlist);
1550 				if (error == 0)
1551 					list32->bfl_len = dltlist.bfl_len;
1552 			}
1553 			BPF_UNLOCK();
1554 			break;
1555 		}
1556 #endif
1557 
1558 	case BIOCGDLTLIST:
1559 		BPF_LOCK();
1560 		if (d->bd_bif == NULL)
1561 			error = EINVAL;
1562 		else
1563 			error = bpf_getdltlist(d, (struct bpf_dltlist *)addr);
1564 		BPF_UNLOCK();
1565 		break;
1566 
1567 	/*
1568 	 * Set data link type.
1569 	 */
1570 	case BIOCSDLT:
1571 		BPF_LOCK();
1572 		if (d->bd_bif == NULL)
1573 			error = EINVAL;
1574 		else
1575 			error = bpf_setdlt(d, *(u_int *)addr);
1576 		BPF_UNLOCK();
1577 		break;
1578 
1579 	/*
1580 	 * Get interface name.
1581 	 */
1582 	case BIOCGETIF:
1583 		BPF_LOCK();
1584 		if (d->bd_bif == NULL)
1585 			error = EINVAL;
1586 		else {
1587 			struct ifnet *const ifp = d->bd_bif->bif_ifp;
1588 			struct ifreq *const ifr = (struct ifreq *)addr;
1589 
1590 			strlcpy(ifr->ifr_name, ifp->if_xname,
1591 			    sizeof(ifr->ifr_name));
1592 		}
1593 		BPF_UNLOCK();
1594 		break;
1595 
1596 	/*
1597 	 * Set interface.
1598 	 */
1599 	case BIOCSETIF:
1600 		/*
1601 		 * Behavior here depends on the buffering model.  If we're
1602 		 * using kernel memory buffers, then we can allocate them here.
1603 		 * If we're using zero-copy, then the user process must have
1604 		 * registered buffers by the time we get here.
1605 		 */
1606 		BPFD_LOCK(d);
1607 		if (d->bd_bufmode == BPF_BUFMODE_BUFFER &&
1608 		    d->bd_sbuf == NULL) {
1609 			u_int size;
1610 
1611 			size = d->bd_bufsize;
1612 			BPFD_UNLOCK(d);
1613 			error = bpf_buffer_ioctl_sblen(d, &size);
1614 			if (error != 0)
1615 				break;
1616 		} else
1617 			BPFD_UNLOCK(d);
1618 		BPF_LOCK();
1619 		error = bpf_setif(d, (struct ifreq *)addr);
1620 		BPF_UNLOCK();
1621 		break;
1622 
1623 	/*
1624 	 * Set read timeout.
1625 	 */
1626 	case BIOCSRTIMEOUT:
1627 #if defined(COMPAT_FREEBSD32) && defined(__amd64__)
1628 	case BIOCSRTIMEOUT32:
1629 #endif
1630 		{
1631 			struct timeval *tv = (struct timeval *)addr;
1632 #if defined(COMPAT_FREEBSD32)
1633 			struct timeval32 *tv32;
1634 			struct timeval tv64;
1635 
1636 			if (cmd == BIOCSRTIMEOUT32) {
1637 				tv32 = (struct timeval32 *)addr;
1638 				tv = &tv64;
1639 				tv->tv_sec = tv32->tv_sec;
1640 				tv->tv_usec = tv32->tv_usec;
1641 			} else
1642 #endif
1643 				tv = (struct timeval *)addr;
1644 
1645 			/*
1646 			 * Subtract 1 tick from tvtohz() since this isn't
1647 			 * a one-shot timer.
1648 			 */
1649 			if ((error = itimerfix(tv)) == 0)
1650 				d->bd_rtout = tvtohz(tv) - 1;
1651 			break;
1652 		}
1653 
1654 	/*
1655 	 * Get read timeout.
1656 	 */
1657 	case BIOCGRTIMEOUT:
1658 #if defined(COMPAT_FREEBSD32) && defined(__amd64__)
1659 	case BIOCGRTIMEOUT32:
1660 #endif
1661 		{
1662 			struct timeval *tv;
1663 #if defined(COMPAT_FREEBSD32) && defined(__amd64__)
1664 			struct timeval32 *tv32;
1665 			struct timeval tv64;
1666 
1667 			if (cmd == BIOCGRTIMEOUT32)
1668 				tv = &tv64;
1669 			else
1670 #endif
1671 				tv = (struct timeval *)addr;
1672 
1673 			tv->tv_sec = d->bd_rtout / hz;
1674 			tv->tv_usec = (d->bd_rtout % hz) * tick;
1675 #if defined(COMPAT_FREEBSD32) && defined(__amd64__)
1676 			if (cmd == BIOCGRTIMEOUT32) {
1677 				tv32 = (struct timeval32 *)addr;
1678 				tv32->tv_sec = tv->tv_sec;
1679 				tv32->tv_usec = tv->tv_usec;
1680 			}
1681 #endif
1682 
1683 			break;
1684 		}
1685 
1686 	/*
1687 	 * Get packet stats.
1688 	 */
1689 	case BIOCGSTATS:
1690 		{
1691 			struct bpf_stat *bs = (struct bpf_stat *)addr;
1692 
1693 			/* XXXCSJP overflow */
1694 			bs->bs_recv = (u_int)counter_u64_fetch(d->bd_rcount);
1695 			bs->bs_drop = (u_int)counter_u64_fetch(d->bd_dcount);
1696 			break;
1697 		}
1698 
1699 	/*
1700 	 * Set immediate mode.
1701 	 */
1702 	case BIOCIMMEDIATE:
1703 		BPFD_LOCK(d);
1704 		d->bd_immediate = *(u_int *)addr;
1705 		BPFD_UNLOCK(d);
1706 		break;
1707 
1708 	case BIOCVERSION:
1709 		{
1710 			struct bpf_version *bv = (struct bpf_version *)addr;
1711 
1712 			bv->bv_major = BPF_MAJOR_VERSION;
1713 			bv->bv_minor = BPF_MINOR_VERSION;
1714 			break;
1715 		}
1716 
1717 	/*
1718 	 * Get "header already complete" flag
1719 	 */
1720 	case BIOCGHDRCMPLT:
1721 		BPFD_LOCK(d);
1722 		*(u_int *)addr = d->bd_hdrcmplt;
1723 		BPFD_UNLOCK(d);
1724 		break;
1725 
1726 	/*
1727 	 * Set "header already complete" flag
1728 	 */
1729 	case BIOCSHDRCMPLT:
1730 		BPFD_LOCK(d);
1731 		d->bd_hdrcmplt = *(u_int *)addr ? 1 : 0;
1732 		BPFD_UNLOCK(d);
1733 		break;
1734 
1735 	/*
1736 	 * Get packet direction flag
1737 	 */
1738 	case BIOCGDIRECTION:
1739 		BPFD_LOCK(d);
1740 		*(u_int *)addr = d->bd_direction;
1741 		BPFD_UNLOCK(d);
1742 		break;
1743 
1744 	/*
1745 	 * Set packet direction flag
1746 	 */
1747 	case BIOCSDIRECTION:
1748 		{
1749 			u_int	direction;
1750 
1751 			direction = *(u_int *)addr;
1752 			switch (direction) {
1753 			case BPF_D_IN:
1754 			case BPF_D_INOUT:
1755 			case BPF_D_OUT:
1756 				BPFD_LOCK(d);
1757 				d->bd_direction = direction;
1758 				BPFD_UNLOCK(d);
1759 				break;
1760 			default:
1761 				error = EINVAL;
1762 			}
1763 		}
1764 		break;
1765 
1766 	/*
1767 	 * Get packet timestamp format and resolution.
1768 	 */
1769 	case BIOCGTSTAMP:
1770 		BPFD_LOCK(d);
1771 		*(u_int *)addr = d->bd_tstamp;
1772 		BPFD_UNLOCK(d);
1773 		break;
1774 
1775 	/*
1776 	 * Set packet timestamp format and resolution.
1777 	 */
1778 	case BIOCSTSTAMP:
1779 		{
1780 			u_int	func;
1781 
1782 			func = *(u_int *)addr;
1783 			if (BPF_T_VALID(func))
1784 				d->bd_tstamp = func;
1785 			else
1786 				error = EINVAL;
1787 		}
1788 		break;
1789 
1790 	case BIOCFEEDBACK:
1791 		BPFD_LOCK(d);
1792 		d->bd_feedback = *(u_int *)addr;
1793 		BPFD_UNLOCK(d);
1794 		break;
1795 
1796 	case BIOCLOCK:
1797 		BPFD_LOCK(d);
1798 		d->bd_locked = 1;
1799 		BPFD_UNLOCK(d);
1800 		break;
1801 
1802 	case FIONBIO:		/* Non-blocking I/O */
1803 		break;
1804 
1805 	case FIOASYNC:		/* Send signal on receive packets */
1806 		BPFD_LOCK(d);
1807 		d->bd_async = *(int *)addr;
1808 		BPFD_UNLOCK(d);
1809 		break;
1810 
1811 	case FIOSETOWN:
1812 		/*
1813 		 * XXX: Add some sort of locking here?
1814 		 * fsetown() can sleep.
1815 		 */
1816 		error = fsetown(*(int *)addr, &d->bd_sigio);
1817 		break;
1818 
1819 	case FIOGETOWN:
1820 		BPFD_LOCK(d);
1821 		*(int *)addr = fgetown(&d->bd_sigio);
1822 		BPFD_UNLOCK(d);
1823 		break;
1824 
1825 	/* This is deprecated, FIOSETOWN should be used instead. */
1826 	case TIOCSPGRP:
1827 		error = fsetown(-(*(int *)addr), &d->bd_sigio);
1828 		break;
1829 
1830 	/* This is deprecated, FIOGETOWN should be used instead. */
1831 	case TIOCGPGRP:
1832 		*(int *)addr = -fgetown(&d->bd_sigio);
1833 		break;
1834 
1835 	case BIOCSRSIG:		/* Set receive signal */
1836 		{
1837 			u_int sig;
1838 
1839 			sig = *(u_int *)addr;
1840 
1841 			if (sig >= NSIG)
1842 				error = EINVAL;
1843 			else {
1844 				BPFD_LOCK(d);
1845 				d->bd_sig = sig;
1846 				BPFD_UNLOCK(d);
1847 			}
1848 			break;
1849 		}
1850 	case BIOCGRSIG:
1851 		BPFD_LOCK(d);
1852 		*(u_int *)addr = d->bd_sig;
1853 		BPFD_UNLOCK(d);
1854 		break;
1855 
1856 	case BIOCGETBUFMODE:
1857 		BPFD_LOCK(d);
1858 		*(u_int *)addr = d->bd_bufmode;
1859 		BPFD_UNLOCK(d);
1860 		break;
1861 
1862 	case BIOCSETBUFMODE:
1863 		/*
1864 		 * Allow the buffering mode to be changed as long as we
1865 		 * haven't yet committed to a particular mode.  Our
1866 		 * definition of commitment, for now, is whether or not a
1867 		 * buffer has been allocated or an interface attached, since
1868 		 * that's the point where things get tricky.
1869 		 */
1870 		switch (*(u_int *)addr) {
1871 		case BPF_BUFMODE_BUFFER:
1872 			break;
1873 
1874 		case BPF_BUFMODE_ZBUF:
1875 			if (bpf_zerocopy_enable)
1876 				break;
1877 			/* FALLSTHROUGH */
1878 
1879 		default:
1880 			CURVNET_RESTORE();
1881 			return (EINVAL);
1882 		}
1883 
1884 		BPFD_LOCK(d);
1885 		if (d->bd_sbuf != NULL || d->bd_hbuf != NULL ||
1886 		    d->bd_fbuf != NULL || d->bd_bif != NULL) {
1887 			BPFD_UNLOCK(d);
1888 			CURVNET_RESTORE();
1889 			return (EBUSY);
1890 		}
1891 		d->bd_bufmode = *(u_int *)addr;
1892 		BPFD_UNLOCK(d);
1893 		break;
1894 
1895 	case BIOCGETZMAX:
1896 		error = bpf_ioctl_getzmax(td, d, (size_t *)addr);
1897 		break;
1898 
1899 	case BIOCSETZBUF:
1900 		error = bpf_ioctl_setzbuf(td, d, (struct bpf_zbuf *)addr);
1901 		break;
1902 
1903 	case BIOCROTZBUF:
1904 		error = bpf_ioctl_rotzbuf(td, d, (struct bpf_zbuf *)addr);
1905 		break;
1906 
1907 	case BIOCSETVLANPCP:
1908 		{
1909 			u_int pcp;
1910 
1911 			pcp = *(u_int *)addr;
1912 			if (pcp > BPF_PRIO_MAX || pcp < 0) {
1913 				error = EINVAL;
1914 				break;
1915 			}
1916 			d->bd_pcp = pcp;
1917 			break;
1918 		}
1919 	}
1920 	CURVNET_RESTORE();
1921 	return (error);
1922 }
1923 
1924 /*
1925  * Set d's packet filter program to fp. If this file already has a filter,
1926  * free it and replace it. Returns EINVAL for bogus requests.
1927  *
1928  * Note we use global lock here to serialize bpf_setf() and bpf_setif()
1929  * calls.
1930  */
1931 static int
bpf_setf(struct bpf_d * d,struct bpf_program * fp,u_long cmd)1932 bpf_setf(struct bpf_d *d, struct bpf_program *fp, u_long cmd)
1933 {
1934 #ifdef COMPAT_FREEBSD32
1935 	struct bpf_program fp_swab;
1936 	struct bpf_program32 *fp32;
1937 #endif
1938 	struct bpf_program_buffer *fcode;
1939 	struct bpf_insn *filter;
1940 #ifdef BPF_JITTER
1941 	bpf_jit_filter *jfunc;
1942 #endif
1943 	size_t size;
1944 	u_int flen;
1945 	bool track_event;
1946 
1947 #ifdef COMPAT_FREEBSD32
1948 	switch (cmd) {
1949 	case BIOCSETF32:
1950 	case BIOCSETWF32:
1951 	case BIOCSETFNR32:
1952 		fp32 = (struct bpf_program32 *)fp;
1953 		fp_swab.bf_len = fp32->bf_len;
1954 		fp_swab.bf_insns =
1955 		    (struct bpf_insn *)(uintptr_t)fp32->bf_insns;
1956 		fp = &fp_swab;
1957 		switch (cmd) {
1958 		case BIOCSETF32:
1959 			cmd = BIOCSETF;
1960 			break;
1961 		case BIOCSETWF32:
1962 			cmd = BIOCSETWF;
1963 			break;
1964 		}
1965 		break;
1966 	}
1967 #endif
1968 
1969 	filter = NULL;
1970 #ifdef BPF_JITTER
1971 	jfunc = NULL;
1972 #endif
1973 	/*
1974 	 * Check new filter validness before acquiring any locks.
1975 	 * Allocate memory for new filter, if needed.
1976 	 */
1977 	flen = fp->bf_len;
1978 	if (flen > bpf_maxinsns || (fp->bf_insns == NULL && flen != 0))
1979 		return (EINVAL);
1980 	size = flen * sizeof(*fp->bf_insns);
1981 	if (size > 0) {
1982 		/* We're setting up new filter. Copy and check actual data. */
1983 		fcode = bpf_program_buffer_alloc(size, M_WAITOK);
1984 		filter = (struct bpf_insn *)fcode->buffer;
1985 		if (copyin(fp->bf_insns, filter, size) != 0 ||
1986 		    !bpf_validate(filter, flen)) {
1987 			free(fcode, M_BPF);
1988 			return (EINVAL);
1989 		}
1990 #ifdef BPF_JITTER
1991 		if (cmd != BIOCSETWF) {
1992 			/*
1993 			 * Filter is copied inside fcode and is
1994 			 * perfectly valid.
1995 			 */
1996 			jfunc = bpf_jitter(filter, flen);
1997 		}
1998 #endif
1999 	}
2000 
2001 	track_event = false;
2002 	fcode = NULL;
2003 
2004 	BPF_LOCK();
2005 	BPFD_LOCK(d);
2006 	/* Set up new filter. */
2007 	if (cmd == BIOCSETWF) {
2008 		if (d->bd_wfilter != NULL) {
2009 			fcode = __containerof((void *)d->bd_wfilter,
2010 			    struct bpf_program_buffer, buffer);
2011 #ifdef BPF_JITTER
2012 			fcode->func = NULL;
2013 #endif
2014 		}
2015 		d->bd_wfilter = filter;
2016 	} else {
2017 		if (d->bd_rfilter != NULL) {
2018 			fcode = __containerof((void *)d->bd_rfilter,
2019 			    struct bpf_program_buffer, buffer);
2020 #ifdef BPF_JITTER
2021 			fcode->func = d->bd_bfilter;
2022 #endif
2023 		}
2024 		d->bd_rfilter = filter;
2025 #ifdef BPF_JITTER
2026 		d->bd_bfilter = jfunc;
2027 #endif
2028 		if (cmd == BIOCSETF)
2029 			reset_d(d);
2030 
2031 		if (bpf_check_upgrade(cmd, d, filter, flen) != 0) {
2032 			/*
2033 			 * Filter can be set several times without
2034 			 * specifying interface. In this case just mark d
2035 			 * as reader.
2036 			 */
2037 			d->bd_writer = 0;
2038 			if (d->bd_bif != NULL) {
2039 				/*
2040 				 * Remove descriptor from writers-only list
2041 				 * and add it to active readers list.
2042 				 */
2043 				CK_LIST_REMOVE(d, bd_next);
2044 				CK_LIST_INSERT_HEAD(&d->bd_bif->bif_dlist,
2045 				    d, bd_next);
2046 				CTR2(KTR_NET,
2047 				    "%s: upgrade required by pid %d",
2048 				    __func__, d->bd_pid);
2049 				track_event = true;
2050 			}
2051 		}
2052 	}
2053 	BPFD_UNLOCK(d);
2054 
2055 	if (fcode != NULL)
2056 		NET_EPOCH_CALL(bpf_program_buffer_free, &fcode->epoch_ctx);
2057 
2058 	if (track_event)
2059 		EVENTHANDLER_INVOKE(bpf_track,
2060 		    d->bd_bif->bif_ifp, d->bd_bif->bif_dlt, 1);
2061 
2062 	BPF_UNLOCK();
2063 	return (0);
2064 }
2065 
2066 /*
2067  * Detach a file from its current interface (if attached at all) and attach
2068  * to the interface indicated by the name stored in ifr.
2069  * Return an errno or 0.
2070  */
2071 static int
bpf_setif(struct bpf_d * d,struct ifreq * ifr)2072 bpf_setif(struct bpf_d *d, struct ifreq *ifr)
2073 {
2074 	struct bpf_if *bp;
2075 	struct ifnet *theywant;
2076 
2077 	BPF_LOCK_ASSERT();
2078 
2079 	theywant = ifunit(ifr->ifr_name);
2080 	if (theywant == NULL)
2081 		return (ENXIO);
2082 	/*
2083 	 * Look through attached interfaces for the named one.
2084 	 */
2085 	CK_LIST_FOREACH(bp, &bpf_iflist, bif_next) {
2086 		if (bp->bif_ifp == theywant &&
2087 		    bp->bif_bpf == &theywant->if_bpf)
2088 			break;
2089 	}
2090 	if (bp == NULL)
2091 		return (ENXIO);
2092 
2093 	MPASS(bp == theywant->if_bpf);
2094 	/*
2095 	 * At this point, we expect the buffer is already allocated.  If not,
2096 	 * return an error.
2097 	 */
2098 	switch (d->bd_bufmode) {
2099 	case BPF_BUFMODE_BUFFER:
2100 	case BPF_BUFMODE_ZBUF:
2101 		if (d->bd_sbuf == NULL)
2102 			return (EINVAL);
2103 		break;
2104 
2105 	default:
2106 		panic("bpf_setif: bufmode %d", d->bd_bufmode);
2107 	}
2108 	if (bp != d->bd_bif)
2109 		bpf_attachd(d, bp);
2110 	else {
2111 		BPFD_LOCK(d);
2112 		reset_d(d);
2113 		BPFD_UNLOCK(d);
2114 	}
2115 	return (0);
2116 }
2117 
2118 /*
2119  * Support for select() and poll() system calls
2120  *
2121  * Return true iff the specific operation will not block indefinitely.
2122  * Otherwise, return false but make a note that a selwakeup() must be done.
2123  */
2124 static int
bpfpoll(struct cdev * dev,int events,struct thread * td)2125 bpfpoll(struct cdev *dev, int events, struct thread *td)
2126 {
2127 	struct bpf_d *d;
2128 	int revents;
2129 
2130 	if (devfs_get_cdevpriv((void **)&d) != 0 || d->bd_bif == NULL)
2131 		return (events &
2132 		    (POLLHUP | POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM));
2133 
2134 	/*
2135 	 * Refresh PID associated with this descriptor.
2136 	 */
2137 	revents = events & (POLLOUT | POLLWRNORM);
2138 	BPFD_LOCK(d);
2139 	BPF_PID_REFRESH(d, td);
2140 	if (events & (POLLIN | POLLRDNORM)) {
2141 		if (bpf_ready(d))
2142 			revents |= events & (POLLIN | POLLRDNORM);
2143 		else {
2144 			selrecord(td, &d->bd_sel);
2145 			/* Start the read timeout if necessary. */
2146 			if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
2147 				callout_reset(&d->bd_callout, d->bd_rtout,
2148 				    bpf_timed_out, d);
2149 				d->bd_state = BPF_WAITING;
2150 			}
2151 		}
2152 	}
2153 	BPFD_UNLOCK(d);
2154 	return (revents);
2155 }
2156 
2157 /*
2158  * Support for kevent() system call.  Register EVFILT_READ filters and
2159  * reject all others.
2160  */
2161 int
bpfkqfilter(struct cdev * dev,struct knote * kn)2162 bpfkqfilter(struct cdev *dev, struct knote *kn)
2163 {
2164 	struct bpf_d *d;
2165 
2166 	if (devfs_get_cdevpriv((void **)&d) != 0)
2167 		return (1);
2168 
2169 	switch (kn->kn_filter) {
2170 	case EVFILT_READ:
2171 		kn->kn_fop = &bpfread_filtops;
2172 		break;
2173 
2174 	case EVFILT_WRITE:
2175 		kn->kn_fop = &bpfwrite_filtops;
2176 		break;
2177 
2178 	default:
2179 		return (1);
2180 	}
2181 
2182 	/*
2183 	 * Refresh PID associated with this descriptor.
2184 	 */
2185 	BPFD_LOCK(d);
2186 	BPF_PID_REFRESH_CUR(d);
2187 	kn->kn_hook = d;
2188 	knlist_add(&d->bd_sel.si_note, kn, 1);
2189 	BPFD_UNLOCK(d);
2190 
2191 	return (0);
2192 }
2193 
2194 static void
filt_bpfdetach(struct knote * kn)2195 filt_bpfdetach(struct knote *kn)
2196 {
2197 	struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
2198 
2199 	knlist_remove(&d->bd_sel.si_note, kn, 0);
2200 }
2201 
2202 static int
filt_bpfread(struct knote * kn,long hint)2203 filt_bpfread(struct knote *kn, long hint)
2204 {
2205 	struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
2206 	int ready;
2207 
2208 	BPFD_LOCK_ASSERT(d);
2209 	ready = bpf_ready(d);
2210 	if (ready) {
2211 		kn->kn_data = d->bd_slen;
2212 		/*
2213 		 * Ignore the hold buffer if it is being copied to user space.
2214 		 */
2215 		if (!d->bd_hbuf_in_use && d->bd_hbuf)
2216 			kn->kn_data += d->bd_hlen;
2217 	} else if (d->bd_rtout > 0 && d->bd_state == BPF_IDLE) {
2218 		callout_reset(&d->bd_callout, d->bd_rtout,
2219 		    bpf_timed_out, d);
2220 		d->bd_state = BPF_WAITING;
2221 	}
2222 
2223 	return (ready);
2224 }
2225 
2226 static int
filt_bpfwrite(struct knote * kn,long hint)2227 filt_bpfwrite(struct knote *kn, long hint)
2228 {
2229 	struct bpf_d *d = (struct bpf_d *)kn->kn_hook;
2230 
2231 	BPFD_LOCK_ASSERT(d);
2232 
2233 	if (d->bd_bif == NULL) {
2234 		kn->kn_data = 0;
2235 		return (0);
2236 	} else {
2237 		kn->kn_data = d->bd_bif->bif_ifp->if_mtu;
2238 		return (1);
2239 	}
2240 }
2241 
2242 #define	BPF_TSTAMP_NONE		0
2243 #define	BPF_TSTAMP_FAST		1
2244 #define	BPF_TSTAMP_NORMAL	2
2245 #define	BPF_TSTAMP_EXTERN	3
2246 
2247 static int
bpf_ts_quality(int tstype)2248 bpf_ts_quality(int tstype)
2249 {
2250 
2251 	if (tstype == BPF_T_NONE)
2252 		return (BPF_TSTAMP_NONE);
2253 	if ((tstype & BPF_T_FAST) != 0)
2254 		return (BPF_TSTAMP_FAST);
2255 
2256 	return (BPF_TSTAMP_NORMAL);
2257 }
2258 
2259 static int
bpf_gettime(struct bintime * bt,int tstype,struct mbuf * m)2260 bpf_gettime(struct bintime *bt, int tstype, struct mbuf *m)
2261 {
2262 	struct timespec ts;
2263 	struct m_tag *tag;
2264 	int quality;
2265 
2266 	quality = bpf_ts_quality(tstype);
2267 	if (quality == BPF_TSTAMP_NONE)
2268 		return (quality);
2269 
2270 	if (m != NULL) {
2271 		if ((m->m_flags & (M_PKTHDR | M_TSTMP)) == (M_PKTHDR | M_TSTMP)) {
2272 			mbuf_tstmp2timespec(m, &ts);
2273 			timespec2bintime(&ts, bt);
2274 			return (BPF_TSTAMP_EXTERN);
2275 		}
2276 		tag = m_tag_locate(m, MTAG_BPF, MTAG_BPF_TIMESTAMP, NULL);
2277 		if (tag != NULL) {
2278 			*bt = *(struct bintime *)(tag + 1);
2279 			return (BPF_TSTAMP_EXTERN);
2280 		}
2281 	}
2282 	if (quality == BPF_TSTAMP_NORMAL)
2283 		binuptime(bt);
2284 	else
2285 		getbinuptime(bt);
2286 
2287 	return (quality);
2288 }
2289 
2290 /*
2291  * Incoming linkage from device drivers.  Process the packet pkt, of length
2292  * pktlen, which is stored in a contiguous buffer.  The packet is parsed
2293  * by each process' filter, and if accepted, stashed into the corresponding
2294  * buffer.
2295  */
2296 void
bpf_tap(struct bpf_if * bp,u_char * pkt,u_int pktlen)2297 bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen)
2298 {
2299 	struct epoch_tracker et;
2300 	struct bintime bt;
2301 	struct bpf_d *d;
2302 #ifdef BPF_JITTER
2303 	bpf_jit_filter *bf;
2304 #endif
2305 	u_int slen;
2306 	int gottime;
2307 
2308 	gottime = BPF_TSTAMP_NONE;
2309 	NET_EPOCH_ENTER(et);
2310 	CK_LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
2311 		counter_u64_add(d->bd_rcount, 1);
2312 		/*
2313 		 * NB: We dont call BPF_CHECK_DIRECTION() here since there
2314 		 * is no way for the caller to indiciate to us whether this
2315 		 * packet is inbound or outbound. In the bpf_mtap() routines,
2316 		 * we use the interface pointers on the mbuf to figure it out.
2317 		 */
2318 #ifdef BPF_JITTER
2319 		bf = bpf_jitter_enable != 0 ? d->bd_bfilter : NULL;
2320 		if (bf != NULL)
2321 			slen = (*(bf->func))(pkt, pktlen, pktlen);
2322 		else
2323 #endif
2324 		slen = bpf_filter(d->bd_rfilter, pkt, pktlen, pktlen);
2325 		if (slen != 0) {
2326 			/*
2327 			 * Filter matches. Let's to acquire write lock.
2328 			 */
2329 			BPFD_LOCK(d);
2330 			counter_u64_add(d->bd_fcount, 1);
2331 			if (gottime < bpf_ts_quality(d->bd_tstamp))
2332 				gottime = bpf_gettime(&bt, d->bd_tstamp,
2333 				    NULL);
2334 #ifdef MAC
2335 			if (mac_bpfdesc_check_receive(d, bp->bif_ifp) == 0)
2336 #endif
2337 				catchpacket(d, pkt, pktlen, slen,
2338 				    bpf_append_bytes, &bt);
2339 			BPFD_UNLOCK(d);
2340 		}
2341 	}
2342 	NET_EPOCH_EXIT(et);
2343 }
2344 
2345 void
bpf_tap_if(if_t ifp,u_char * pkt,u_int pktlen)2346 bpf_tap_if(if_t ifp, u_char *pkt, u_int pktlen)
2347 {
2348 	if (bpf_peers_present(ifp->if_bpf))
2349 		bpf_tap(ifp->if_bpf, pkt, pktlen);
2350 }
2351 
2352 #define	BPF_CHECK_DIRECTION(d, r, i)				\
2353 	    (((d)->bd_direction == BPF_D_IN && (r) != (i)) ||	\
2354 	    ((d)->bd_direction == BPF_D_OUT && (r) == (i)))
2355 
2356 /*
2357  * Incoming linkage from device drivers, when packet is in an mbuf chain.
2358  * Locking model is explained in bpf_tap().
2359  */
2360 void
bpf_mtap(struct bpf_if * bp,struct mbuf * m)2361 bpf_mtap(struct bpf_if *bp, struct mbuf *m)
2362 {
2363 	struct epoch_tracker et;
2364 	struct bintime bt;
2365 	struct bpf_d *d;
2366 #ifdef BPF_JITTER
2367 	bpf_jit_filter *bf;
2368 #endif
2369 	u_int pktlen, slen;
2370 	int gottime;
2371 
2372 	/* Skip outgoing duplicate packets. */
2373 	if ((m->m_flags & M_PROMISC) != 0 && m_rcvif(m) == NULL) {
2374 		m->m_flags &= ~M_PROMISC;
2375 		return;
2376 	}
2377 
2378 	pktlen = m_length(m, NULL);
2379 	gottime = BPF_TSTAMP_NONE;
2380 
2381 	NET_EPOCH_ENTER(et);
2382 	CK_LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
2383 		if (BPF_CHECK_DIRECTION(d, m_rcvif(m), bp->bif_ifp))
2384 			continue;
2385 		counter_u64_add(d->bd_rcount, 1);
2386 #ifdef BPF_JITTER
2387 		bf = bpf_jitter_enable != 0 ? d->bd_bfilter : NULL;
2388 		/* XXX We cannot handle multiple mbufs. */
2389 		if (bf != NULL && m->m_next == NULL)
2390 			slen = (*(bf->func))(mtod(m, u_char *), pktlen,
2391 			    pktlen);
2392 		else
2393 #endif
2394 		slen = bpf_filter(d->bd_rfilter, (u_char *)m, pktlen, 0);
2395 		if (slen != 0) {
2396 			BPFD_LOCK(d);
2397 
2398 			counter_u64_add(d->bd_fcount, 1);
2399 			if (gottime < bpf_ts_quality(d->bd_tstamp))
2400 				gottime = bpf_gettime(&bt, d->bd_tstamp, m);
2401 #ifdef MAC
2402 			if (mac_bpfdesc_check_receive(d, bp->bif_ifp) == 0)
2403 #endif
2404 				catchpacket(d, (u_char *)m, pktlen, slen,
2405 				    bpf_append_mbuf, &bt);
2406 			BPFD_UNLOCK(d);
2407 		}
2408 	}
2409 	NET_EPOCH_EXIT(et);
2410 }
2411 
2412 void
bpf_mtap_if(if_t ifp,struct mbuf * m)2413 bpf_mtap_if(if_t ifp, struct mbuf *m)
2414 {
2415 	if (bpf_peers_present(ifp->if_bpf)) {
2416 		M_ASSERTVALID(m);
2417 		bpf_mtap(ifp->if_bpf, m);
2418 	}
2419 }
2420 
2421 /*
2422  * Incoming linkage from device drivers, when packet is in
2423  * an mbuf chain and to be prepended by a contiguous header.
2424  */
2425 void
bpf_mtap2(struct bpf_if * bp,void * data,u_int dlen,struct mbuf * m)2426 bpf_mtap2(struct bpf_if *bp, void *data, u_int dlen, struct mbuf *m)
2427 {
2428 	struct epoch_tracker et;
2429 	struct bintime bt;
2430 	struct mbuf mb;
2431 	struct bpf_d *d;
2432 	u_int pktlen, slen;
2433 	int gottime;
2434 
2435 	/* Skip outgoing duplicate packets. */
2436 	if ((m->m_flags & M_PROMISC) != 0 && m->m_pkthdr.rcvif == NULL) {
2437 		m->m_flags &= ~M_PROMISC;
2438 		return;
2439 	}
2440 
2441 	pktlen = m_length(m, NULL);
2442 	/*
2443 	 * Craft on-stack mbuf suitable for passing to bpf_filter.
2444 	 * Note that we cut corners here; we only setup what's
2445 	 * absolutely needed--this mbuf should never go anywhere else.
2446 	 */
2447 	mb.m_flags = 0;
2448 	mb.m_next = m;
2449 	mb.m_data = data;
2450 	mb.m_len = dlen;
2451 	pktlen += dlen;
2452 
2453 	gottime = BPF_TSTAMP_NONE;
2454 
2455 	NET_EPOCH_ENTER(et);
2456 	CK_LIST_FOREACH(d, &bp->bif_dlist, bd_next) {
2457 		if (BPF_CHECK_DIRECTION(d, m->m_pkthdr.rcvif, bp->bif_ifp))
2458 			continue;
2459 		counter_u64_add(d->bd_rcount, 1);
2460 		slen = bpf_filter(d->bd_rfilter, (u_char *)&mb, pktlen, 0);
2461 		if (slen != 0) {
2462 			BPFD_LOCK(d);
2463 
2464 			counter_u64_add(d->bd_fcount, 1);
2465 			if (gottime < bpf_ts_quality(d->bd_tstamp))
2466 				gottime = bpf_gettime(&bt, d->bd_tstamp, m);
2467 #ifdef MAC
2468 			if (mac_bpfdesc_check_receive(d, bp->bif_ifp) == 0)
2469 #endif
2470 				catchpacket(d, (u_char *)&mb, pktlen, slen,
2471 				    bpf_append_mbuf, &bt);
2472 			BPFD_UNLOCK(d);
2473 		}
2474 	}
2475 	NET_EPOCH_EXIT(et);
2476 }
2477 
2478 void
bpf_mtap2_if(if_t ifp,void * data,u_int dlen,struct mbuf * m)2479 bpf_mtap2_if(if_t ifp, void *data, u_int dlen, struct mbuf *m)
2480 {
2481 	if (bpf_peers_present(ifp->if_bpf)) {
2482 		M_ASSERTVALID(m);
2483 		bpf_mtap2(ifp->if_bpf, data, dlen, m);
2484 	}
2485 }
2486 
2487 #undef	BPF_CHECK_DIRECTION
2488 #undef	BPF_TSTAMP_NONE
2489 #undef	BPF_TSTAMP_FAST
2490 #undef	BPF_TSTAMP_NORMAL
2491 #undef	BPF_TSTAMP_EXTERN
2492 
2493 static int
bpf_hdrlen(struct bpf_d * d)2494 bpf_hdrlen(struct bpf_d *d)
2495 {
2496 	int hdrlen;
2497 
2498 	hdrlen = d->bd_bif->bif_hdrlen;
2499 #ifndef BURN_BRIDGES
2500 	if (d->bd_tstamp == BPF_T_NONE ||
2501 	    BPF_T_FORMAT(d->bd_tstamp) == BPF_T_MICROTIME)
2502 #ifdef COMPAT_FREEBSD32
2503 		if (d->bd_compat32)
2504 			hdrlen += SIZEOF_BPF_HDR(struct bpf_hdr32);
2505 		else
2506 #endif
2507 			hdrlen += SIZEOF_BPF_HDR(struct bpf_hdr);
2508 	else
2509 #endif
2510 		hdrlen += SIZEOF_BPF_HDR(struct bpf_xhdr);
2511 #ifdef COMPAT_FREEBSD32
2512 	if (d->bd_compat32)
2513 		hdrlen = BPF_WORDALIGN32(hdrlen);
2514 	else
2515 #endif
2516 		hdrlen = BPF_WORDALIGN(hdrlen);
2517 
2518 	return (hdrlen - d->bd_bif->bif_hdrlen);
2519 }
2520 
2521 static void
bpf_bintime2ts(struct bintime * bt,struct bpf_ts * ts,int tstype)2522 bpf_bintime2ts(struct bintime *bt, struct bpf_ts *ts, int tstype)
2523 {
2524 	struct bintime bt2, boottimebin;
2525 	struct timeval tsm;
2526 	struct timespec tsn;
2527 
2528 	if ((tstype & BPF_T_MONOTONIC) == 0) {
2529 		bt2 = *bt;
2530 		getboottimebin(&boottimebin);
2531 		bintime_add(&bt2, &boottimebin);
2532 		bt = &bt2;
2533 	}
2534 	switch (BPF_T_FORMAT(tstype)) {
2535 	case BPF_T_MICROTIME:
2536 		bintime2timeval(bt, &tsm);
2537 		ts->bt_sec = tsm.tv_sec;
2538 		ts->bt_frac = tsm.tv_usec;
2539 		break;
2540 	case BPF_T_NANOTIME:
2541 		bintime2timespec(bt, &tsn);
2542 		ts->bt_sec = tsn.tv_sec;
2543 		ts->bt_frac = tsn.tv_nsec;
2544 		break;
2545 	case BPF_T_BINTIME:
2546 		ts->bt_sec = bt->sec;
2547 		ts->bt_frac = bt->frac;
2548 		break;
2549 	}
2550 }
2551 
2552 /*
2553  * Move the packet data from interface memory (pkt) into the
2554  * store buffer.  "cpfn" is the routine called to do the actual data
2555  * transfer.  bcopy is passed in to copy contiguous chunks, while
2556  * bpf_append_mbuf is passed in to copy mbuf chains.  In the latter case,
2557  * pkt is really an mbuf.
2558  */
2559 static void
catchpacket(struct bpf_d * d,u_char * pkt,u_int pktlen,u_int snaplen,void (* cpfn)(struct bpf_d *,caddr_t,u_int,void *,u_int),struct bintime * bt)2560 catchpacket(struct bpf_d *d, u_char *pkt, u_int pktlen, u_int snaplen,
2561     void (*cpfn)(struct bpf_d *, caddr_t, u_int, void *, u_int),
2562     struct bintime *bt)
2563 {
2564 	static char zeroes[BPF_ALIGNMENT];
2565 	struct bpf_xhdr hdr;
2566 #ifndef BURN_BRIDGES
2567 	struct bpf_hdr hdr_old;
2568 #ifdef COMPAT_FREEBSD32
2569 	struct bpf_hdr32 hdr32_old;
2570 #endif
2571 #endif
2572 	int caplen, curlen, hdrlen, pad, totlen;
2573 	int do_wakeup = 0;
2574 	int do_timestamp;
2575 	int tstype;
2576 
2577 	BPFD_LOCK_ASSERT(d);
2578 	if (d->bd_bif == NULL) {
2579 		/* Descriptor was detached in concurrent thread */
2580 		counter_u64_add(d->bd_dcount, 1);
2581 		return;
2582 	}
2583 
2584 	/*
2585 	 * Detect whether user space has released a buffer back to us, and if
2586 	 * so, move it from being a hold buffer to a free buffer.  This may
2587 	 * not be the best place to do it (for example, we might only want to
2588 	 * run this check if we need the space), but for now it's a reliable
2589 	 * spot to do it.
2590 	 */
2591 	if (d->bd_fbuf == NULL && bpf_canfreebuf(d)) {
2592 		d->bd_fbuf = d->bd_hbuf;
2593 		d->bd_hbuf = NULL;
2594 		d->bd_hlen = 0;
2595 		bpf_buf_reclaimed(d);
2596 	}
2597 
2598 	/*
2599 	 * Figure out how many bytes to move.  If the packet is
2600 	 * greater or equal to the snapshot length, transfer that
2601 	 * much.  Otherwise, transfer the whole packet (unless
2602 	 * we hit the buffer size limit).
2603 	 */
2604 	hdrlen = bpf_hdrlen(d);
2605 	totlen = hdrlen + min(snaplen, pktlen);
2606 	if (totlen > d->bd_bufsize)
2607 		totlen = d->bd_bufsize;
2608 
2609 	/*
2610 	 * Round up the end of the previous packet to the next longword.
2611 	 *
2612 	 * Drop the packet if there's no room and no hope of room
2613 	 * If the packet would overflow the storage buffer or the storage
2614 	 * buffer is considered immutable by the buffer model, try to rotate
2615 	 * the buffer and wakeup pending processes.
2616 	 */
2617 #ifdef COMPAT_FREEBSD32
2618 	if (d->bd_compat32)
2619 		curlen = BPF_WORDALIGN32(d->bd_slen);
2620 	else
2621 #endif
2622 		curlen = BPF_WORDALIGN(d->bd_slen);
2623 	if (curlen + totlen > d->bd_bufsize || !bpf_canwritebuf(d)) {
2624 		if (d->bd_fbuf == NULL) {
2625 			/*
2626 			 * There's no room in the store buffer, and no
2627 			 * prospect of room, so drop the packet.  Notify the
2628 			 * buffer model.
2629 			 */
2630 			bpf_buffull(d);
2631 			counter_u64_add(d->bd_dcount, 1);
2632 			return;
2633 		}
2634 		KASSERT(!d->bd_hbuf_in_use, ("hold buffer is in use"));
2635 		ROTATE_BUFFERS(d);
2636 		do_wakeup = 1;
2637 		curlen = 0;
2638 	} else {
2639 		if (d->bd_immediate || d->bd_state == BPF_TIMED_OUT) {
2640 			/*
2641 			 * Immediate mode is set, or the read timeout has
2642 			 * already expired during a select call.  A packet
2643 			 * arrived, so the reader should be woken up.
2644 			 */
2645 			do_wakeup = 1;
2646 		}
2647 		pad = curlen - d->bd_slen;
2648 		KASSERT(pad >= 0 && pad <= sizeof(zeroes),
2649 		    ("%s: invalid pad byte count %d", __func__, pad));
2650 		if (pad > 0) {
2651 			/* Zero pad bytes. */
2652 			bpf_append_bytes(d, d->bd_sbuf, d->bd_slen, zeroes,
2653 			    pad);
2654 		}
2655 	}
2656 
2657 	caplen = totlen - hdrlen;
2658 	tstype = d->bd_tstamp;
2659 	do_timestamp = tstype != BPF_T_NONE;
2660 #ifndef BURN_BRIDGES
2661 	if (tstype == BPF_T_NONE || BPF_T_FORMAT(tstype) == BPF_T_MICROTIME) {
2662 		struct bpf_ts ts;
2663 		if (do_timestamp)
2664 			bpf_bintime2ts(bt, &ts, tstype);
2665 #ifdef COMPAT_FREEBSD32
2666 		if (d->bd_compat32) {
2667 			bzero(&hdr32_old, sizeof(hdr32_old));
2668 			if (do_timestamp) {
2669 				hdr32_old.bh_tstamp.tv_sec = ts.bt_sec;
2670 				hdr32_old.bh_tstamp.tv_usec = ts.bt_frac;
2671 			}
2672 			hdr32_old.bh_datalen = pktlen;
2673 			hdr32_old.bh_hdrlen = hdrlen;
2674 			hdr32_old.bh_caplen = caplen;
2675 			bpf_append_bytes(d, d->bd_sbuf, curlen, &hdr32_old,
2676 			    sizeof(hdr32_old));
2677 			goto copy;
2678 		}
2679 #endif
2680 		bzero(&hdr_old, sizeof(hdr_old));
2681 		if (do_timestamp) {
2682 			hdr_old.bh_tstamp.tv_sec = ts.bt_sec;
2683 			hdr_old.bh_tstamp.tv_usec = ts.bt_frac;
2684 		}
2685 		hdr_old.bh_datalen = pktlen;
2686 		hdr_old.bh_hdrlen = hdrlen;
2687 		hdr_old.bh_caplen = caplen;
2688 		bpf_append_bytes(d, d->bd_sbuf, curlen, &hdr_old,
2689 		    sizeof(hdr_old));
2690 		goto copy;
2691 	}
2692 #endif
2693 
2694 	/*
2695 	 * Append the bpf header.  Note we append the actual header size, but
2696 	 * move forward the length of the header plus padding.
2697 	 */
2698 	bzero(&hdr, sizeof(hdr));
2699 	if (do_timestamp)
2700 		bpf_bintime2ts(bt, &hdr.bh_tstamp, tstype);
2701 	hdr.bh_datalen = pktlen;
2702 	hdr.bh_hdrlen = hdrlen;
2703 	hdr.bh_caplen = caplen;
2704 	bpf_append_bytes(d, d->bd_sbuf, curlen, &hdr, sizeof(hdr));
2705 
2706 	/*
2707 	 * Copy the packet data into the store buffer and update its length.
2708 	 */
2709 #ifndef BURN_BRIDGES
2710 copy:
2711 #endif
2712 	(*cpfn)(d, d->bd_sbuf, curlen + hdrlen, pkt, caplen);
2713 	d->bd_slen = curlen + totlen;
2714 
2715 	if (do_wakeup)
2716 		bpf_wakeup(d);
2717 }
2718 
2719 /*
2720  * Free buffers currently in use by a descriptor.
2721  * Called on close.
2722  */
2723 static void
bpfd_free(epoch_context_t ctx)2724 bpfd_free(epoch_context_t ctx)
2725 {
2726 	struct bpf_d *d;
2727 	struct bpf_program_buffer *p;
2728 
2729 	/*
2730 	 * We don't need to lock out interrupts since this descriptor has
2731 	 * been detached from its interface and it yet hasn't been marked
2732 	 * free.
2733 	 */
2734 	d = __containerof(ctx, struct bpf_d, epoch_ctx);
2735 	bpf_free(d);
2736 	if (d->bd_rfilter != NULL) {
2737 		p = __containerof((void *)d->bd_rfilter,
2738 		    struct bpf_program_buffer, buffer);
2739 #ifdef BPF_JITTER
2740 		p->func = d->bd_bfilter;
2741 #endif
2742 		bpf_program_buffer_free(&p->epoch_ctx);
2743 	}
2744 	if (d->bd_wfilter != NULL) {
2745 		p = __containerof((void *)d->bd_wfilter,
2746 		    struct bpf_program_buffer, buffer);
2747 #ifdef BPF_JITTER
2748 		p->func = NULL;
2749 #endif
2750 		bpf_program_buffer_free(&p->epoch_ctx);
2751 	}
2752 
2753 	mtx_destroy(&d->bd_lock);
2754 	counter_u64_free(d->bd_rcount);
2755 	counter_u64_free(d->bd_dcount);
2756 	counter_u64_free(d->bd_fcount);
2757 	counter_u64_free(d->bd_wcount);
2758 	counter_u64_free(d->bd_wfcount);
2759 	counter_u64_free(d->bd_wdcount);
2760 	counter_u64_free(d->bd_zcopy);
2761 	free(d, M_BPF);
2762 }
2763 
2764 /*
2765  * Attach an interface to bpf.  dlt is the link layer type; hdrlen is the
2766  * fixed size of the link header (variable length headers not yet supported).
2767  */
2768 void
bpfattach(struct ifnet * ifp,u_int dlt,u_int hdrlen)2769 bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen)
2770 {
2771 
2772 	bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
2773 }
2774 
2775 /*
2776  * Attach an interface to bpf.  ifp is a pointer to the structure
2777  * defining the interface to be attached, dlt is the link layer type,
2778  * and hdrlen is the fixed size of the link header (variable length
2779  * headers are not yet supporrted).
2780  */
2781 void
bpfattach2(struct ifnet * ifp,u_int dlt,u_int hdrlen,struct bpf_if ** driverp)2782 bpfattach2(struct ifnet *ifp, u_int dlt, u_int hdrlen,
2783     struct bpf_if **driverp)
2784 {
2785 	struct bpf_if *bp;
2786 
2787 	KASSERT(*driverp == NULL,
2788 	    ("bpfattach2: driverp already initialized"));
2789 
2790 	bp = malloc(sizeof(*bp), M_BPF, M_WAITOK | M_ZERO);
2791 
2792 	CK_LIST_INIT(&bp->bif_dlist);
2793 	CK_LIST_INIT(&bp->bif_wlist);
2794 	bp->bif_ifp = ifp;
2795 	bp->bif_dlt = dlt;
2796 	bp->bif_hdrlen = hdrlen;
2797 	bp->bif_bpf = driverp;
2798 	refcount_init(&bp->bif_refcnt, 1);
2799 	*driverp = bp;
2800 	/*
2801 	 * Reference ifnet pointer, so it won't freed until
2802 	 * we release it.
2803 	 */
2804 	if_ref(ifp);
2805 	BPF_LOCK();
2806 	CK_LIST_INSERT_HEAD(&bpf_iflist, bp, bif_next);
2807 	BPF_UNLOCK();
2808 
2809 	if (bootverbose && IS_DEFAULT_VNET(curvnet))
2810 		if_printf(ifp, "bpf attached\n");
2811 }
2812 
2813 #ifdef VIMAGE
2814 /*
2815  * Detach descriptors on interface's vmove event.
2816  */
2817 void
bpf_ifdetach(struct ifnet * ifp)2818 bpf_ifdetach(struct ifnet *ifp)
2819 {
2820 	struct bpf_if *bp;
2821 	struct bpf_d *d;
2822 
2823 	BPF_LOCK();
2824 	CK_LIST_FOREACH(bp, &bpf_iflist, bif_next) {
2825 		if (bp->bif_ifp != ifp)
2826 			continue;
2827 
2828 		/* Detach common descriptors */
2829 		while ((d = CK_LIST_FIRST(&bp->bif_dlist)) != NULL) {
2830 			bpf_detachd(d, true);
2831 		}
2832 
2833 		/* Detach writer-only descriptors */
2834 		while ((d = CK_LIST_FIRST(&bp->bif_wlist)) != NULL) {
2835 			bpf_detachd(d, true);
2836 		}
2837 	}
2838 	BPF_UNLOCK();
2839 }
2840 #endif
2841 
2842 /*
2843  * Detach bpf from an interface. This involves detaching each descriptor
2844  * associated with the interface. Notify each descriptor as it's detached
2845  * so that any sleepers wake up and get ENXIO.
2846  */
2847 void
bpfdetach(struct ifnet * ifp)2848 bpfdetach(struct ifnet *ifp)
2849 {
2850 	struct bpf_if *bp, *bp_temp;
2851 	struct bpf_d *d;
2852 
2853 	BPF_LOCK();
2854 	/* Find all bpf_if struct's which reference ifp and detach them. */
2855 	CK_LIST_FOREACH_SAFE(bp, &bpf_iflist, bif_next, bp_temp) {
2856 		if (ifp != bp->bif_ifp)
2857 			continue;
2858 
2859 		CK_LIST_REMOVE(bp, bif_next);
2860 		*bp->bif_bpf = __DECONST(struct bpf_if *, &dead_bpf_if);
2861 
2862 		CTR4(KTR_NET,
2863 		    "%s: sheduling free for encap %d (%p) for if %p",
2864 		    __func__, bp->bif_dlt, bp, ifp);
2865 
2866 		/* Detach common descriptors */
2867 		while ((d = CK_LIST_FIRST(&bp->bif_dlist)) != NULL) {
2868 			bpf_detachd(d, true);
2869 		}
2870 
2871 		/* Detach writer-only descriptors */
2872 		while ((d = CK_LIST_FIRST(&bp->bif_wlist)) != NULL) {
2873 			bpf_detachd(d, true);
2874 		}
2875 		bpfif_rele(bp);
2876 	}
2877 	BPF_UNLOCK();
2878 }
2879 
2880 bool
bpf_peers_present_if(struct ifnet * ifp)2881 bpf_peers_present_if(struct ifnet *ifp)
2882 {
2883 	return (bpf_peers_present(ifp->if_bpf));
2884 }
2885 
2886 /*
2887  * Get a list of available data link type of the interface.
2888  */
2889 static int
bpf_getdltlist(struct bpf_d * d,struct bpf_dltlist * bfl)2890 bpf_getdltlist(struct bpf_d *d, struct bpf_dltlist *bfl)
2891 {
2892 	struct ifnet *ifp;
2893 	struct bpf_if *bp;
2894 	u_int *lst;
2895 	int error, n, n1;
2896 
2897 	BPF_LOCK_ASSERT();
2898 
2899 	ifp = d->bd_bif->bif_ifp;
2900 	n1 = 0;
2901 	CK_LIST_FOREACH(bp, &bpf_iflist, bif_next) {
2902 		if (bp->bif_ifp == ifp)
2903 			n1++;
2904 	}
2905 	if (bfl->bfl_list == NULL) {
2906 		bfl->bfl_len = n1;
2907 		return (0);
2908 	}
2909 	if (n1 > bfl->bfl_len)
2910 		return (ENOMEM);
2911 
2912 	lst = malloc(n1 * sizeof(u_int), M_TEMP, M_WAITOK);
2913 	n = 0;
2914 	CK_LIST_FOREACH(bp, &bpf_iflist, bif_next) {
2915 		if (bp->bif_ifp != ifp)
2916 			continue;
2917 		lst[n++] = bp->bif_dlt;
2918 	}
2919 	error = copyout(lst, bfl->bfl_list, sizeof(u_int) * n);
2920 	free(lst, M_TEMP);
2921 	bfl->bfl_len = n;
2922 	return (error);
2923 }
2924 
2925 /*
2926  * Set the data link type of a BPF instance.
2927  */
2928 static int
bpf_setdlt(struct bpf_d * d,u_int dlt)2929 bpf_setdlt(struct bpf_d *d, u_int dlt)
2930 {
2931 	int error, opromisc;
2932 	struct ifnet *ifp;
2933 	struct bpf_if *bp;
2934 
2935 	BPF_LOCK_ASSERT();
2936 	MPASS(d->bd_bif != NULL);
2937 
2938 	/*
2939 	 * It is safe to check bd_bif without BPFD_LOCK, it can not be
2940 	 * changed while we hold global lock.
2941 	 */
2942 	if (d->bd_bif->bif_dlt == dlt)
2943 		return (0);
2944 
2945 	ifp = d->bd_bif->bif_ifp;
2946 	CK_LIST_FOREACH(bp, &bpf_iflist, bif_next) {
2947 		if (bp->bif_ifp == ifp && bp->bif_dlt == dlt)
2948 			break;
2949 	}
2950 	if (bp == NULL)
2951 		return (EINVAL);
2952 
2953 	opromisc = d->bd_promisc;
2954 	bpf_attachd(d, bp);
2955 	if (opromisc) {
2956 		error = ifpromisc(bp->bif_ifp, 1);
2957 		if (error)
2958 			if_printf(bp->bif_ifp, "%s: ifpromisc failed (%d)\n",
2959 			    __func__, error);
2960 		else
2961 			d->bd_promisc = 1;
2962 	}
2963 	return (0);
2964 }
2965 
2966 static void
bpf_drvinit(void * unused)2967 bpf_drvinit(void *unused)
2968 {
2969 	struct cdev *dev;
2970 
2971 	sx_init(&bpf_sx, "bpf global lock");
2972 	dev = make_dev(&bpf_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "bpf");
2973 	/* For compatibility */
2974 	make_dev_alias(dev, "bpf0");
2975 }
2976 
2977 /*
2978  * Zero out the various packet counters associated with all of the bpf
2979  * descriptors.  At some point, we will probably want to get a bit more
2980  * granular and allow the user to specify descriptors to be zeroed.
2981  */
2982 static void
bpf_zero_counters(void)2983 bpf_zero_counters(void)
2984 {
2985 	struct bpf_if *bp;
2986 	struct bpf_d *bd;
2987 
2988 	BPF_LOCK();
2989 	/*
2990 	 * We are protected by global lock here, interfaces and
2991 	 * descriptors can not be deleted while we hold it.
2992 	 */
2993 	CK_LIST_FOREACH(bp, &bpf_iflist, bif_next) {
2994 		CK_LIST_FOREACH(bd, &bp->bif_dlist, bd_next) {
2995 			counter_u64_zero(bd->bd_rcount);
2996 			counter_u64_zero(bd->bd_dcount);
2997 			counter_u64_zero(bd->bd_fcount);
2998 			counter_u64_zero(bd->bd_wcount);
2999 			counter_u64_zero(bd->bd_wfcount);
3000 			counter_u64_zero(bd->bd_zcopy);
3001 		}
3002 	}
3003 	BPF_UNLOCK();
3004 }
3005 
3006 /*
3007  * Fill filter statistics
3008  */
3009 static void
bpfstats_fill_xbpf(struct xbpf_d * d,struct bpf_d * bd)3010 bpfstats_fill_xbpf(struct xbpf_d *d, struct bpf_d *bd)
3011 {
3012 
3013 	BPF_LOCK_ASSERT();
3014 	bzero(d, sizeof(*d));
3015 	d->bd_structsize = sizeof(*d);
3016 	d->bd_immediate = bd->bd_immediate;
3017 	d->bd_promisc = bd->bd_promisc;
3018 	d->bd_hdrcmplt = bd->bd_hdrcmplt;
3019 	d->bd_direction = bd->bd_direction;
3020 	d->bd_feedback = bd->bd_feedback;
3021 	d->bd_async = bd->bd_async;
3022 	d->bd_rcount = counter_u64_fetch(bd->bd_rcount);
3023 	d->bd_dcount = counter_u64_fetch(bd->bd_dcount);
3024 	d->bd_fcount = counter_u64_fetch(bd->bd_fcount);
3025 	d->bd_sig = bd->bd_sig;
3026 	d->bd_slen = bd->bd_slen;
3027 	d->bd_hlen = bd->bd_hlen;
3028 	d->bd_bufsize = bd->bd_bufsize;
3029 	d->bd_pid = bd->bd_pid;
3030 	strlcpy(d->bd_ifname,
3031 	    bd->bd_bif->bif_ifp->if_xname, IFNAMSIZ);
3032 	d->bd_locked = bd->bd_locked;
3033 	d->bd_wcount = counter_u64_fetch(bd->bd_wcount);
3034 	d->bd_wdcount = counter_u64_fetch(bd->bd_wdcount);
3035 	d->bd_wfcount = counter_u64_fetch(bd->bd_wfcount);
3036 	d->bd_zcopy = counter_u64_fetch(bd->bd_zcopy);
3037 	d->bd_bufmode = bd->bd_bufmode;
3038 }
3039 
3040 /*
3041  * Handle `netstat -B' stats request
3042  */
3043 static int
bpf_stats_sysctl(SYSCTL_HANDLER_ARGS)3044 bpf_stats_sysctl(SYSCTL_HANDLER_ARGS)
3045 {
3046 	static const struct xbpf_d zerostats;
3047 	struct xbpf_d *xbdbuf, *xbd, tempstats;
3048 	int index, error;
3049 	struct bpf_if *bp;
3050 	struct bpf_d *bd;
3051 
3052 	/*
3053 	 * XXX This is not technically correct. It is possible for non
3054 	 * privileged users to open bpf devices. It would make sense
3055 	 * if the users who opened the devices were able to retrieve
3056 	 * the statistics for them, too.
3057 	 */
3058 	error = priv_check(req->td, PRIV_NET_BPF);
3059 	if (error)
3060 		return (error);
3061 	/*
3062 	 * Check to see if the user is requesting that the counters be
3063 	 * zeroed out.  Explicitly check that the supplied data is zeroed,
3064 	 * as we aren't allowing the user to set the counters currently.
3065 	 */
3066 	if (req->newptr != NULL) {
3067 		if (req->newlen != sizeof(tempstats))
3068 			return (EINVAL);
3069 		memset(&tempstats, 0, sizeof(tempstats));
3070 		error = SYSCTL_IN(req, &tempstats, sizeof(tempstats));
3071 		if (error)
3072 			return (error);
3073 		if (bcmp(&tempstats, &zerostats, sizeof(tempstats)) != 0)
3074 			return (EINVAL);
3075 		bpf_zero_counters();
3076 		return (0);
3077 	}
3078 	if (req->oldptr == NULL)
3079 		return (SYSCTL_OUT(req, 0, bpf_bpfd_cnt * sizeof(*xbd)));
3080 	if (bpf_bpfd_cnt == 0)
3081 		return (SYSCTL_OUT(req, 0, 0));
3082 	xbdbuf = malloc(req->oldlen, M_BPF, M_WAITOK);
3083 	BPF_LOCK();
3084 	if (req->oldlen < (bpf_bpfd_cnt * sizeof(*xbd))) {
3085 		BPF_UNLOCK();
3086 		free(xbdbuf, M_BPF);
3087 		return (ENOMEM);
3088 	}
3089 	index = 0;
3090 	CK_LIST_FOREACH(bp, &bpf_iflist, bif_next) {
3091 		/* Send writers-only first */
3092 		CK_LIST_FOREACH(bd, &bp->bif_wlist, bd_next) {
3093 			xbd = &xbdbuf[index++];
3094 			bpfstats_fill_xbpf(xbd, bd);
3095 		}
3096 		CK_LIST_FOREACH(bd, &bp->bif_dlist, bd_next) {
3097 			xbd = &xbdbuf[index++];
3098 			bpfstats_fill_xbpf(xbd, bd);
3099 		}
3100 	}
3101 	BPF_UNLOCK();
3102 	error = SYSCTL_OUT(req, xbdbuf, index * sizeof(*xbd));
3103 	free(xbdbuf, M_BPF);
3104 	return (error);
3105 }
3106 
3107 SYSINIT(bpfdev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, bpf_drvinit, NULL);
3108 
3109 #else /* !DEV_BPF && !NETGRAPH_BPF */
3110 
3111 /*
3112  * NOP stubs to allow bpf-using drivers to load and function.
3113  *
3114  * A 'better' implementation would allow the core bpf functionality
3115  * to be loaded at runtime.
3116  */
3117 
3118 void
bpf_tap(struct bpf_if * bp,u_char * pkt,u_int pktlen)3119 bpf_tap(struct bpf_if *bp, u_char *pkt, u_int pktlen)
3120 {
3121 }
3122 
3123 void
bpf_tap_if(if_t ifp,u_char * pkt,u_int pktlen)3124 bpf_tap_if(if_t ifp, u_char *pkt, u_int pktlen)
3125 {
3126 }
3127 
3128 void
bpf_mtap(struct bpf_if * bp,struct mbuf * m)3129 bpf_mtap(struct bpf_if *bp, struct mbuf *m)
3130 {
3131 }
3132 
3133 void
bpf_mtap_if(if_t ifp,struct mbuf * m)3134 bpf_mtap_if(if_t ifp, struct mbuf *m)
3135 {
3136 }
3137 
3138 void
bpf_mtap2(struct bpf_if * bp,void * d,u_int l,struct mbuf * m)3139 bpf_mtap2(struct bpf_if *bp, void *d, u_int l, struct mbuf *m)
3140 {
3141 }
3142 
3143 void
bpf_mtap2_if(if_t ifp,void * data,u_int dlen,struct mbuf * m)3144 bpf_mtap2_if(if_t ifp, void *data, u_int dlen, struct mbuf *m)
3145 {
3146 }
3147 
3148 void
bpfattach(struct ifnet * ifp,u_int dlt,u_int hdrlen)3149 bpfattach(struct ifnet *ifp, u_int dlt, u_int hdrlen)
3150 {
3151 
3152 	bpfattach2(ifp, dlt, hdrlen, &ifp->if_bpf);
3153 }
3154 
3155 void
bpfattach2(struct ifnet * ifp,u_int dlt,u_int hdrlen,struct bpf_if ** driverp)3156 bpfattach2(struct ifnet *ifp, u_int dlt, u_int hdrlen, struct bpf_if **driverp)
3157 {
3158 
3159 	*driverp = __DECONST(struct bpf_if *, &dead_bpf_if);
3160 }
3161 
3162 void
bpfdetach(struct ifnet * ifp)3163 bpfdetach(struct ifnet *ifp)
3164 {
3165 }
3166 
3167 bool
bpf_peers_present_if(struct ifnet * ifp)3168 bpf_peers_present_if(struct ifnet *ifp)
3169 {
3170 	return (false);
3171 }
3172 
3173 u_int
bpf_filter(const struct bpf_insn * pc,u_char * p,u_int wirelen,u_int buflen)3174 bpf_filter(const struct bpf_insn *pc, u_char *p, u_int wirelen, u_int buflen)
3175 {
3176 	return (-1);	/* "no filter" behaviour */
3177 }
3178 
3179 int
bpf_validate(const struct bpf_insn * f,int len)3180 bpf_validate(const struct bpf_insn *f, int len)
3181 {
3182 	return (0);	/* false */
3183 }
3184 
3185 #endif /* !DEV_BPF && !NETGRAPH_BPF */
3186