2 * Copyright (c) 2000-2020 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1990, 1991, 1993
30 * The Regents of the University of California. All rights reserved.
32 * This code is derived from the Stanford/CMU enet packet filter,
33 * (net/enet.c) distributed as part of 4.3BSD, and code contributed
34 * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
35 * Berkeley Laboratory.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * @(#)bpf.c 8.2 (Berkeley) 3/28/94
67 * $FreeBSD: src/sys/net/bpf.c,v 1.59.2.5 2001/01/05 04:49:09 jdp Exp $
70 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
71 * support for mandatory and extensible security protections. This notice
72 * is included in support of clause 2.2 (b) of the Apple Public License,
81 #define inline __inline
84 #include <sys/param.h>
85 #include <sys/systm.h>
87 #include <sys/malloc.h>
91 #include <sys/signalvar.h>
92 #include <sys/filio.h>
93 #include <sys/sockio.h>
94 #include <sys/ttycom.h>
95 #include <sys/filedesc.h>
96 #include <sys/uio_internal.h>
97 #include <sys/file_internal.h>
98 #include <sys/event.h>
100 #include <sys/poll.h>
102 #include <sys/socket.h>
103 #include <sys/socketvar.h>
104 #include <sys/vnode.h>
108 #include <net/bpfdesc.h>
110 #include <netinet/in.h>
111 #include <netinet/ip.h>
112 #include <netinet/ip6.h>
113 #include <netinet/in_pcb.h>
114 #include <netinet/in_var.h>
115 #include <netinet/ip_var.h>
116 #include <netinet/tcp.h>
117 #include <netinet/tcp_var.h>
118 #include <netinet/udp.h>
119 #include <netinet/udp_var.h>
120 #include <netinet/if_ether.h>
121 #include <netinet/isakmp.h>
122 #include <netinet6/esp.h>
123 #include <sys/kernel.h>
124 #include <sys/sysctl.h>
125 #include <net/firewire.h>
127 #include <miscfs/devfs/devfs.h>
128 #include <net/dlil.h>
129 #include <net/pktap.h>
131 #include <kern/locks.h>
132 #include <kern/thread_call.h>
133 #include <libkern/section_keywords.h>
137 extern int tvtohz(struct timeval
*);
139 #define BPF_BUFSIZE 4096
140 #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, uio)
142 #define PRINET 26 /* interruptible */
144 #define ISAKMP_HDR_SIZE (sizeof(struct isakmp) + sizeof(struct isakmp_gen))
145 #define ESP_HDR_SIZE sizeof(struct newesp)
147 typedef void (*pktcopyfunc_t
)(const void *, void *, size_t);
150 * The default read buffer size is patchable.
152 static unsigned int bpf_bufsize
= BPF_BUFSIZE
;
153 SYSCTL_INT(_debug
, OID_AUTO
, bpf_bufsize
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
154 &bpf_bufsize
, 0, "");
156 static int sysctl_bpf_maxbufsize SYSCTL_HANDLER_ARGS
;
157 extern const int copysize_limit_panic
;
158 #define BPF_MAXSIZE_CAP (copysize_limit_panic >> 1)
159 __private_extern__
unsigned int bpf_maxbufsize
= BPF_MAXBUFSIZE
;
160 SYSCTL_PROC(_debug
, OID_AUTO
, bpf_maxbufsize
, CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
162 sysctl_bpf_maxbufsize
, "I", "Default BPF max buffer size");
164 static unsigned int bpf_maxdevices
= 256;
165 SYSCTL_UINT(_debug
, OID_AUTO
, bpf_maxdevices
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
166 &bpf_maxdevices
, 0, "");
168 * bpf_wantpktap controls the defaul visibility of DLT_PKTAP
169 * For OS X is off by default so process need to use the ioctl BPF_WANT_PKTAP
170 * explicitly to be able to use DLT_PKTAP.
172 #if !XNU_TARGET_OS_OSX
173 static unsigned int bpf_wantpktap
= 1;
174 #else /* XNU_TARGET_OS_OSX */
175 static unsigned int bpf_wantpktap
= 0;
176 #endif /* XNU_TARGET_OS_OSX */
177 SYSCTL_UINT(_debug
, OID_AUTO
, bpf_wantpktap
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
178 &bpf_wantpktap
, 0, "");
180 static int bpf_debug
= 0;
181 SYSCTL_INT(_debug
, OID_AUTO
, bpf_debug
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
185 * bpf_iflist is the list of interfaces; each corresponds to an ifnet
186 * bpf_dtab holds pointer to the descriptors, indexed by minor device #
188 static struct bpf_if
*bpf_iflist
;
191 * BSD now stores the bpf_d in the dev_t which is a struct
192 * on their system. Our dev_t is an int, so we still store
193 * the bpf_d in a separate table indexed by minor device #.
195 * The value stored in bpf_dtab[n] represent three states:
196 * NULL: device not opened
197 * BPF_DEV_RESERVED: device opening or closing
198 * other: device <n> opened with pointer to storage
200 #define BPF_DEV_RESERVED ((struct bpf_d *)(uintptr_t)1)
201 static struct bpf_d
**bpf_dtab
= NULL
;
202 static unsigned int bpf_dtab_size
= 0;
203 static unsigned int nbpfilter
= 0;
205 decl_lck_mtx_data(static, bpf_mlock_data
);
206 static lck_mtx_t
*bpf_mlock
= &bpf_mlock_data
;
207 static lck_grp_t
*bpf_mlock_grp
;
208 static lck_grp_attr_t
*bpf_mlock_grp_attr
;
209 static lck_attr_t
*bpf_mlock_attr
;
211 #endif /* __APPLE__ */
213 static int bpf_allocbufs(struct bpf_d
*);
214 static errno_t
bpf_attachd(struct bpf_d
*d
, struct bpf_if
*bp
);
215 static int bpf_detachd(struct bpf_d
*d
, int);
216 static void bpf_freed(struct bpf_d
*);
217 static int bpf_movein(struct uio
*, int,
218 struct mbuf
**, struct sockaddr
*, int *);
219 static int bpf_setif(struct bpf_d
*, ifnet_t ifp
, bool, bool);
220 static void bpf_timed_out(void *, void *);
221 static void bpf_wakeup(struct bpf_d
*);
222 static u_int
get_pkt_trunc_len(u_char
*, u_int
);
223 static void catchpacket(struct bpf_d
*, struct bpf_packet
*, u_int
, int);
224 static void reset_d(struct bpf_d
*);
225 static int bpf_setf(struct bpf_d
*, u_int
, user_addr_t
, u_long
);
226 static int bpf_getdltlist(struct bpf_d
*, caddr_t
, struct proc
*);
227 static int bpf_setdlt(struct bpf_d
*, u_int
);
228 static int bpf_set_traffic_class(struct bpf_d
*, int);
229 static void bpf_set_packet_service_class(struct mbuf
*, int);
231 static void bpf_acquire_d(struct bpf_d
*);
232 static void bpf_release_d(struct bpf_d
*);
234 static int bpf_devsw_installed
;
236 void bpf_init(void *unused
);
237 static int bpf_tap_callback(struct ifnet
*ifp
, struct mbuf
*m
);
240 * Darwin differs from BSD here, the following are static
241 * on BSD and not static on Darwin.
247 ioctl_fcn_t bpfioctl
;
248 select_fcn_t bpfselect
;
250 /* Darwin's cdevsw struct differs slightly from BSDs */
251 #define CDEV_MAJOR 23
252 static const struct cdevsw bpf_cdevsw
= {
259 .d_reset
= eno_reset
,
261 .d_select
= bpfselect
,
263 .d_strategy
= eno_strat
,
264 .d_reserved_1
= eno_getc
,
265 .d_reserved_2
= eno_putc
,
269 #define SOCKADDR_HDR_LEN offsetof(struct sockaddr, sa_data)
272 bpf_movein(struct uio
*uio
, int linktype
, struct mbuf
**mp
,
273 struct sockaddr
*sockp
, int *datlen
)
290 sa_family
= AF_UNSPEC
;
291 /* XXX Would MAXLINKHDR be better? */
292 hlen
= sizeof(struct ether_header
);
297 #if defined(__FreeBSD__) || defined(__bsdi__)
298 sa_family
= AF_IMPLINK
;
301 sa_family
= AF_UNSPEC
;
302 /* XXX 4(FORMAC)+6(dst)+6(src)+3(LLC)+5(SNAP) */
310 sa_family
= AF_UNSPEC
;
315 case DLT_ATM_RFC1483
:
317 * en atm driver requires 4-byte atm pseudo header.
318 * though it isn't standard, vpi:vci needs to be
321 sa_family
= AF_UNSPEC
;
322 hlen
= 12; /* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
327 sa_family
= AF_UNSPEC
;
328 hlen
= 4; /* This should match PPP_HDRLEN */
331 case DLT_APPLE_IP_OVER_IEEE1394
:
332 sa_family
= AF_UNSPEC
;
333 hlen
= sizeof(struct firewire_header
);
336 case DLT_IEEE802_11
: /* IEEE 802.11 wireless */
337 sa_family
= AF_IEEE80211
;
341 case DLT_IEEE802_11_RADIO
:
342 sa_family
= AF_IEEE80211
;
350 // LP64todo - fix this!
351 len
= uio_resid(uio
);
352 *datlen
= len
- hlen
;
353 if ((unsigned)len
> MCLBYTES
) {
359 * Build a sockaddr based on the data link layer type.
360 * We do this at this level because the ethernet header
361 * is copied directly into the data field of the sockaddr.
362 * In the case of SLIP, there is no header and the packet
363 * is forwarded as is.
364 * Also, we are careful to leave room at the front of the mbuf
365 * for the link level header.
367 if ((hlen
+ SOCKADDR_HDR_LEN
) > sockp
->sa_len
) {
370 sockp
->sa_family
= sa_family
;
373 * We're directly sending the packet data supplied by
374 * the user; we don't need to make room for the link
375 * header, and don't need the header length value any
376 * more, so set it to 0.
381 MGETHDR(m
, M_WAIT
, MT_DATA
);
385 if ((unsigned)len
> MHLEN
) {
387 if ((m
->m_flags
& M_EXT
) == 0) {
392 m
->m_pkthdr
.len
= m
->m_len
= len
;
393 m
->m_pkthdr
.rcvif
= NULL
;
397 * Make room for link header.
400 m
->m_pkthdr
.len
-= hlen
;
402 m
->m_data
+= hlen
; /* XXX */
403 error
= UIOMOVE((caddr_t
)sockp
->sa_data
, hlen
, UIO_WRITE
, uio
);
408 error
= UIOMOVE(mtod(m
, caddr_t
), len
- hlen
, UIO_WRITE
, uio
);
413 /* Check for multicast destination */
416 struct ether_header
*eh
;
418 eh
= mtod(m
, struct ether_header
*);
419 if (ETHER_IS_MULTICAST(eh
->ether_dhost
)) {
420 if (_ether_cmp(etherbroadcastaddr
,
421 eh
->ether_dhost
) == 0) {
422 m
->m_flags
|= M_BCAST
;
424 m
->m_flags
|= M_MCAST
;
440 * The dynamic addition of a new device node must block all processes that
441 * are opening the last device so that no process will get an unexpected
445 bpf_make_dev_t(int maj
)
447 static int bpf_growing
= 0;
448 unsigned int cur_size
= nbpfilter
, i
;
450 if (nbpfilter
>= bpf_maxdevices
) {
454 while (bpf_growing
) {
455 /* Wait until new device has been created */
456 (void) tsleep((caddr_t
)&bpf_growing
, PZERO
, "bpf_growing", 0);
458 if (nbpfilter
> cur_size
) {
459 /* other thread grew it already */
464 /* need to grow bpf_dtab first */
465 if (nbpfilter
== bpf_dtab_size
) {
467 struct bpf_d
**new_dtab
= NULL
;
468 struct bpf_d
**old_dtab
= NULL
;
470 new_dtab_size
= bpf_dtab_size
+ NBPFILTER
;
471 new_dtab
= (struct bpf_d
**)_MALLOC(
472 sizeof(struct bpf_d
*) * new_dtab_size
, M_DEVBUF
, M_WAIT
);
474 printf("bpf_make_dev_t: malloc bpf_dtab failed\n");
478 bcopy(bpf_dtab
, new_dtab
,
479 sizeof(struct bpf_d
*) * bpf_dtab_size
);
481 bzero(new_dtab
+ bpf_dtab_size
,
482 sizeof(struct bpf_d
*) * NBPFILTER
);
485 bpf_dtab_size
= new_dtab_size
;
486 if (old_dtab
!= NULL
) {
487 _FREE(old_dtab
, M_DEVBUF
);
491 (void) devfs_make_node(makedev(maj
, i
),
492 DEVFS_CHAR
, UID_ROOT
, GID_WHEEL
, 0600,
496 wakeup((caddr_t
)&bpf_growing
);
502 * Attach file to the bpf interface, i.e. make d listen on bp.
505 bpf_attachd(struct bpf_d
*d
, struct bpf_if
*bp
)
507 int first
= bp
->bif_dlist
== NULL
;
511 * Point d at bp, and add d to the interface's list of listeners.
512 * Finally, point the driver's bpf cookie at the interface so
513 * it will divert packets to bpf.
516 d
->bd_next
= bp
->bif_dlist
;
520 * Take a reference on the device even if an error is returned
521 * because we keep the device in the interface's list of listeners
526 /* Find the default bpf entry for this ifp */
527 if (bp
->bif_ifp
->if_bpf
== NULL
) {
528 struct bpf_if
*tmp
, *primary
= NULL
;
530 for (tmp
= bpf_iflist
; tmp
; tmp
= tmp
->bif_next
) {
531 if (tmp
->bif_ifp
== bp
->bif_ifp
) {
536 bp
->bif_ifp
->if_bpf
= primary
;
538 /* Only call dlil_set_bpf_tap for primary dlt */
539 if (bp
->bif_ifp
->if_bpf
== bp
) {
540 dlil_set_bpf_tap(bp
->bif_ifp
, BPF_TAP_INPUT_OUTPUT
,
544 if (bp
->bif_tap
!= NULL
) {
545 error
= bp
->bif_tap(bp
->bif_ifp
, bp
->bif_dlt
,
546 BPF_TAP_INPUT_OUTPUT
);
551 * Reset the detach flags in case we previously detached an interface
553 d
->bd_flags
&= ~(BPF_DETACHING
| BPF_DETACHED
);
555 if (bp
->bif_dlt
== DLT_PKTAP
) {
556 d
->bd_flags
|= BPF_FINALIZE_PKTAP
;
558 d
->bd_flags
&= ~BPF_FINALIZE_PKTAP
;
564 * Detach a file from its interface.
566 * Return 1 if was closed by some thread, 0 otherwise
569 bpf_detachd(struct bpf_d
*d
, int closing
)
575 int bpf_closed
= d
->bd_flags
& BPF_CLOSING
;
577 * Some other thread already detached
579 if ((d
->bd_flags
& (BPF_DETACHED
| BPF_DETACHING
)) != 0) {
583 * This thread is doing the detach
585 d
->bd_flags
|= BPF_DETACHING
;
587 ifp
= d
->bd_bif
->bif_ifp
;
590 if (bpf_debug
!= 0) {
591 printf("%s: %llx %s%s\n",
592 __func__
, (uint64_t)VM_KERNEL_ADDRPERM(d
),
593 if_name(ifp
), closing
? " closing" : "");
596 /* Remove d from the interface's descriptor list. */
601 panic("bpf_detachd: descriptor not in list");
605 if (bp
->bif_dlist
== 0) {
607 * Let the driver know that there are no more listeners.
609 /* Only call dlil_set_bpf_tap for primary dlt */
610 if (bp
->bif_ifp
->if_bpf
== bp
) {
611 dlil_set_bpf_tap(ifp
, BPF_TAP_DISABLE
, NULL
);
614 bp
->bif_tap(ifp
, bp
->bif_dlt
, BPF_TAP_DISABLE
);
617 for (bp
= bpf_iflist
; bp
; bp
= bp
->bif_next
) {
618 if (bp
->bif_ifp
== ifp
&& bp
->bif_dlist
!= 0) {
628 * Check if this descriptor had requested promiscuous mode.
629 * If so, turn it off.
633 lck_mtx_unlock(bpf_mlock
);
634 if (ifnet_set_promiscuous(ifp
, 0)) {
636 * Something is really wrong if we were able to put
637 * the driver into promiscuous mode, but can't
639 * Most likely the network interface is gone.
641 printf("%s: ifnet_set_promiscuous failed\n", __func__
);
643 lck_mtx_lock(bpf_mlock
);
647 * Wake up other thread that are waiting for this thread to finish
650 d
->bd_flags
&= ~BPF_DETACHING
;
651 d
->bd_flags
|= BPF_DETACHED
;
653 /* Refresh the local variable as d could have been modified */
654 bpf_closed
= d
->bd_flags
& BPF_CLOSING
;
656 * Note that We've kept the reference because we may have dropped
657 * the lock when turning off promiscuous mode
663 * When closing makes sure no other thread refer to the bpf_d
665 if (bpf_debug
!= 0) {
666 printf("%s: %llx done\n",
667 __func__
, (uint64_t)VM_KERNEL_ADDRPERM(d
));
670 * Let the caller know the bpf_d is closed
680 * Start asynchronous timer, if necessary.
681 * Must be called with bpf_mlock held.
684 bpf_start_timer(struct bpf_d
*d
)
689 if (d
->bd_rtout
> 0 && d
->bd_state
== BPF_IDLE
) {
690 tv
.tv_sec
= d
->bd_rtout
/ hz
;
691 tv
.tv_usec
= (d
->bd_rtout
% hz
) * tick
;
693 clock_interval_to_deadline(
694 (uint64_t)tv
.tv_sec
* USEC_PER_SEC
+ tv
.tv_usec
,
695 NSEC_PER_USEC
, &deadline
);
697 * The state is BPF_IDLE, so the timer hasn't
698 * been started yet, and hasn't gone off yet;
699 * there is no thread call scheduled, so this
700 * won't change the schedule.
702 * XXX - what if, by the time it gets entered,
703 * the deadline has already passed?
705 thread_call_enter_delayed(d
->bd_thread_call
, deadline
);
706 d
->bd_state
= BPF_WAITING
;
711 * Cancel asynchronous timer.
712 * Must be called with bpf_mlock held.
715 bpf_stop_timer(struct bpf_d
*d
)
718 * If the timer has already gone off, this does nothing.
719 * Our caller is expected to set d->bd_state to BPF_IDLE,
720 * with the bpf_mlock, after we are called. bpf_timed_out()
721 * also grabs bpf_mlock, so, if the timer has gone off and
722 * bpf_timed_out() hasn't finished, it's waiting for the
723 * lock; when this thread releases the lock, it will
724 * find the state is BPF_IDLE, and just release the
727 return thread_call_cancel(d
->bd_thread_call
);
731 bpf_acquire_d(struct bpf_d
*d
)
733 void *lr_saved
= __builtin_return_address(0);
735 LCK_MTX_ASSERT(bpf_mlock
, LCK_MTX_ASSERT_OWNED
);
739 d
->bd_ref_lr
[d
->bd_next_ref_lr
] = lr_saved
;
740 d
->bd_next_ref_lr
= (d
->bd_next_ref_lr
+ 1) % BPF_REF_HIST
;
744 bpf_release_d(struct bpf_d
*d
)
746 void *lr_saved
= __builtin_return_address(0);
748 LCK_MTX_ASSERT(bpf_mlock
, LCK_MTX_ASSERT_OWNED
);
750 if (d
->bd_refcnt
<= 0) {
751 panic("%s: %p refcnt <= 0", __func__
, d
);
756 d
->bd_unref_lr
[d
->bd_next_unref_lr
] = lr_saved
;
757 d
->bd_next_unref_lr
= (d
->bd_next_unref_lr
+ 1) % BPF_REF_HIST
;
759 if (d
->bd_refcnt
== 0) {
760 /* Assert the device is detached */
761 if ((d
->bd_flags
& BPF_DETACHED
) == 0) {
762 panic("%s: %p BPF_DETACHED not set", __func__
, d
);
770 * Open ethernet device. Returns ENXIO for illegal minor device number,
771 * EBUSY if file is open by another process.
775 bpfopen(dev_t dev
, int flags
, __unused
int fmt
,
780 lck_mtx_lock(bpf_mlock
);
781 if ((unsigned int) minor(dev
) >= nbpfilter
) {
782 lck_mtx_unlock(bpf_mlock
);
786 * New device nodes are created on demand when opening the last one.
787 * The programming model is for processes to loop on the minor starting
788 * at 0 as long as EBUSY is returned. The loop stops when either the
789 * open succeeds or an error other that EBUSY is returned. That means
790 * that bpf_make_dev_t() must block all processes that are opening the
791 * last node. If not all processes are blocked, they could unexpectedly
792 * get ENOENT and abort their opening loop.
794 if ((unsigned int) minor(dev
) == (nbpfilter
- 1)) {
795 bpf_make_dev_t(major(dev
));
799 * Each minor can be opened by only one process. If the requested
800 * minor is in use, return EBUSY.
802 * Important: bpfopen() and bpfclose() have to check and set the status
803 * of a device in the same lockin context otherwise the device may be
804 * leaked because the vnode use count will be unpextectly greater than 1
805 * when close() is called.
807 if (bpf_dtab
[minor(dev
)] == NULL
) {
808 /* Reserve while opening */
809 bpf_dtab
[minor(dev
)] = BPF_DEV_RESERVED
;
811 lck_mtx_unlock(bpf_mlock
);
814 d
= (struct bpf_d
*)_MALLOC(sizeof(struct bpf_d
), M_DEVBUF
,
817 /* this really is a catastrophic failure */
818 printf("bpfopen: malloc bpf_d failed\n");
819 bpf_dtab
[minor(dev
)] = NULL
;
820 lck_mtx_unlock(bpf_mlock
);
824 /* Mark "in use" and do most initialization. */
826 d
->bd_bufsize
= bpf_bufsize
;
829 d
->bd_oflags
= flags
;
830 d
->bd_state
= BPF_IDLE
;
831 d
->bd_traffic_class
= SO_TC_BE
;
832 d
->bd_flags
|= BPF_DETACHED
;
834 d
->bd_flags
|= BPF_WANT_PKTAP
;
836 d
->bd_flags
&= ~BPF_WANT_PKTAP
;
838 d
->bd_thread_call
= thread_call_allocate(bpf_timed_out
, d
);
839 if (d
->bd_thread_call
== NULL
) {
840 printf("bpfopen: malloc thread call failed\n");
841 bpf_dtab
[minor(dev
)] = NULL
;
843 lck_mtx_unlock(bpf_mlock
);
848 uuid_generate(d
->bd_uuid
);
850 bpf_dtab
[minor(dev
)] = d
; /* Mark opened */
851 lck_mtx_unlock(bpf_mlock
);
857 * Close the descriptor by detaching it from its interface,
858 * deallocating its buffers, and marking it free.
862 bpfclose(dev_t dev
, __unused
int flags
, __unused
int fmt
,
863 __unused
struct proc
*p
)
867 /* Take BPF lock to ensure no other thread is using the device */
868 lck_mtx_lock(bpf_mlock
);
870 d
= bpf_dtab
[minor(dev
)];
871 if (d
== NULL
|| d
== BPF_DEV_RESERVED
) {
872 lck_mtx_unlock(bpf_mlock
);
877 * Other threads may call bpd_detachd() if we drop the bpf_mlock
879 d
->bd_flags
|= BPF_CLOSING
;
881 if (bpf_debug
!= 0) {
883 __func__
, (uint64_t)VM_KERNEL_ADDRPERM(d
));
886 bpf_dtab
[minor(dev
)] = BPF_DEV_RESERVED
; /* Reserve while closing */
889 * Deal with any in-progress timeouts.
891 switch (d
->bd_state
) {
894 * Not waiting for a timeout, and no timeout happened.
900 * Waiting for a timeout.
901 * Cancel any timer that has yet to go off,
902 * and mark the state as "closing".
903 * Then drop the lock to allow any timers that
904 * *have* gone off to run to completion, and wait
905 * for them to finish.
907 if (!bpf_stop_timer(d
)) {
909 * There was no pending call, so the call must
910 * have been in progress. Wait for the call to
911 * complete; we have to drop the lock while
912 * waiting. to let the in-progrss call complete
914 d
->bd_state
= BPF_DRAINING
;
915 while (d
->bd_state
== BPF_DRAINING
) {
916 msleep((caddr_t
)d
, bpf_mlock
, PRINET
,
917 "bpfdraining", NULL
);
920 d
->bd_state
= BPF_IDLE
;
925 * Timer went off, and the timeout routine finished.
927 d
->bd_state
= BPF_IDLE
;
932 * Another thread is blocked on a close waiting for
933 * a timeout to finish.
934 * This "shouldn't happen", as the first thread to enter
935 * bpfclose() will set bpf_dtab[minor(dev)] to 1, and
936 * all subsequent threads should see that and fail with
939 panic("Two threads blocked in a BPF close");
946 selthreadclear(&d
->bd_sel
);
947 thread_call_free(d
->bd_thread_call
);
949 while (d
->bd_hbuf_read
!= 0) {
950 msleep((caddr_t
)d
, bpf_mlock
, PRINET
, "bpf_reading", NULL
);
955 /* Mark free in same context as bpfopen comes to check */
956 bpf_dtab
[minor(dev
)] = NULL
; /* Mark closed */
960 lck_mtx_unlock(bpf_mlock
);
965 #define BPF_SLEEP bpf_sleep
968 bpf_sleep(struct bpf_d
*d
, int pri
, const char *wmesg
, int timo
)
970 u_int64_t abstime
= 0;
973 clock_interval_to_deadline(timo
, NSEC_PER_SEC
/ hz
, &abstime
);
976 return msleep1((caddr_t
)d
, bpf_mlock
, pri
, wmesg
, abstime
);
980 bpf_finalize_pktap(struct bpf_hdr
*hp
, struct pktap_header
*pktaphdr
)
982 if (pktaphdr
->pth_flags
& PTH_FLAG_V2_HDR
) {
983 struct pktap_v2_hdr
*pktap_v2_hdr
;
985 pktap_v2_hdr
= (struct pktap_v2_hdr
*)pktaphdr
;
987 if (pktap_v2_hdr
->pth_flags
& PTH_FLAG_DELAY_PKTAP
) {
988 pktap_v2_finalize_proc_info(pktap_v2_hdr
);
991 if (pktaphdr
->pth_flags
& PTH_FLAG_DELAY_PKTAP
) {
992 pktap_finalize_proc_info(pktaphdr
);
995 if (pktaphdr
->pth_flags
& PTH_FLAG_TSTAMP
) {
996 hp
->bh_tstamp
.tv_sec
= pktaphdr
->pth_tstamp
.tv_sec
;
997 hp
->bh_tstamp
.tv_usec
= pktaphdr
->pth_tstamp
.tv_usec
;
1003 * Rotate the packet buffers in descriptor d. Move the store buffer
1004 * into the hold slot, and the free buffer into the store slot.
1005 * Zero the length of the new store buffer.
1007 #define ROTATE_BUFFERS(d) \
1008 if (d->bd_hbuf_read != 0) \
1009 panic("rotating bpf buffers during read"); \
1010 (d)->bd_hbuf = (d)->bd_sbuf; \
1011 (d)->bd_hlen = (d)->bd_slen; \
1012 (d)->bd_hcnt = (d)->bd_scnt; \
1013 (d)->bd_sbuf = (d)->bd_fbuf; \
1016 (d)->bd_fbuf = NULL;
1018 * bpfread - read next chunk of packets from buffers
1021 bpfread(dev_t dev
, struct uio
*uio
, int ioflag
)
1025 int timed_out
, hbuf_len
;
1029 lck_mtx_lock(bpf_mlock
);
1031 d
= bpf_dtab
[minor(dev
)];
1032 if (d
== NULL
|| d
== BPF_DEV_RESERVED
||
1033 (d
->bd_flags
& BPF_CLOSING
) != 0) {
1034 lck_mtx_unlock(bpf_mlock
);
1041 * Restrict application to use a buffer the same size as
1042 * as kernel buffers.
1044 if (uio_resid(uio
) != d
->bd_bufsize
) {
1046 lck_mtx_unlock(bpf_mlock
);
1050 if (d
->bd_state
== BPF_WAITING
) {
1054 timed_out
= (d
->bd_state
== BPF_TIMED_OUT
);
1055 d
->bd_state
= BPF_IDLE
;
1057 while (d
->bd_hbuf_read
!= 0) {
1058 msleep((caddr_t
)d
, bpf_mlock
, PRINET
, "bpf_reading", NULL
);
1061 if ((d
->bd_flags
& BPF_CLOSING
) != 0) {
1063 lck_mtx_unlock(bpf_mlock
);
1067 * If the hold buffer is empty, then do a timed sleep, which
1068 * ends when the timeout expires or when enough packets
1069 * have arrived to fill the store buffer.
1071 while (d
->bd_hbuf
== 0) {
1072 if ((d
->bd_immediate
|| timed_out
|| (ioflag
& IO_NDELAY
)) &&
1075 * We're in immediate mode, or are reading
1076 * in non-blocking mode, or a timer was
1077 * started before the read (e.g., by select()
1078 * or poll()) and has expired and a packet(s)
1079 * either arrived since the previous
1080 * read or arrived while we were asleep.
1081 * Rotate the buffers and return what's here.
1088 * No data is available, check to see if the bpf device
1089 * is still pointed at a real interface. If not, return
1090 * ENXIO so that the userland process knows to rebind
1091 * it before using it again.
1093 if (d
->bd_bif
== NULL
) {
1095 lck_mtx_unlock(bpf_mlock
);
1098 if (ioflag
& IO_NDELAY
) {
1100 lck_mtx_unlock(bpf_mlock
);
1103 error
= BPF_SLEEP(d
, PRINET
| PCATCH
, "bpf", d
->bd_rtout
);
1105 * Make sure device is still opened
1107 if ((d
->bd_flags
& BPF_CLOSING
) != 0) {
1109 lck_mtx_unlock(bpf_mlock
);
1113 while (d
->bd_hbuf_read
!= 0) {
1114 msleep((caddr_t
)d
, bpf_mlock
, PRINET
, "bpf_reading",
1118 if ((d
->bd_flags
& BPF_CLOSING
) != 0) {
1120 lck_mtx_unlock(bpf_mlock
);
1124 if (error
== EINTR
|| error
== ERESTART
) {
1125 if (d
->bd_hbuf
!= NULL
) {
1127 * Because we msleep, the hold buffer might
1128 * be filled when we wake up. Avoid rotating
1133 if (d
->bd_slen
!= 0) {
1135 * Sometimes we may be interrupted often and
1136 * the sleep above will not timeout.
1137 * Regardless, we should rotate the buffers
1138 * if there's any new data pending and
1145 lck_mtx_unlock(bpf_mlock
);
1146 if (error
== ERESTART
) {
1147 printf("%s: %llx ERESTART to EINTR\n",
1148 __func__
, (uint64_t)VM_KERNEL_ADDRPERM(d
));
1153 if (error
== EWOULDBLOCK
) {
1155 * On a timeout, return what's in the buffer,
1156 * which may be nothing. If there is something
1157 * in the store buffer, we can rotate the buffers.
1161 * We filled up the buffer in between
1162 * getting the timeout and arriving
1163 * here, so we don't need to rotate.
1168 if (d
->bd_slen
== 0) {
1170 lck_mtx_unlock(bpf_mlock
);
1178 * At this point, we know we have something in the hold slot.
1182 * Set the hold buffer read. So we do not
1183 * rotate the buffers until the hold buffer
1184 * read is complete. Also to avoid issues resulting
1185 * from page faults during disk sleep (<rdar://problem/13436396>).
1187 d
->bd_hbuf_read
= 1;
1189 hbuf_len
= d
->bd_hlen
;
1190 flags
= d
->bd_flags
;
1191 lck_mtx_unlock(bpf_mlock
);
1195 * Before we move data to userland, we fill out the extended
1198 if (flags
& BPF_EXTENDED_HDR
) {
1202 while (p
< hbuf
+ hbuf_len
) {
1203 struct bpf_hdr_ext
*ehp
;
1205 struct so_procinfo soprocinfo
;
1208 ehp
= (struct bpf_hdr_ext
*)(void *)p
;
1209 if ((flowid
= ehp
->bh_flowid
) != 0) {
1210 if (ehp
->bh_proto
== IPPROTO_TCP
) {
1211 found
= inp_findinpcb_procinfo(&tcbinfo
,
1212 flowid
, &soprocinfo
);
1213 } else if (ehp
->bh_proto
== IPPROTO_UDP
) {
1214 found
= inp_findinpcb_procinfo(&udbinfo
,
1215 flowid
, &soprocinfo
);
1218 ehp
->bh_pid
= soprocinfo
.spi_pid
;
1219 strlcpy(&ehp
->bh_comm
[0], &soprocinfo
.spi_proc_name
[0], sizeof(ehp
->bh_comm
));
1224 if (flags
& BPF_FINALIZE_PKTAP
) {
1225 struct pktap_header
*pktaphdr
;
1227 pktaphdr
= (struct pktap_header
*)(void *)
1228 (p
+ BPF_WORDALIGN(ehp
->bh_hdrlen
));
1230 bpf_finalize_pktap((struct bpf_hdr
*) ehp
,
1233 p
+= BPF_WORDALIGN(ehp
->bh_hdrlen
+ ehp
->bh_caplen
);
1235 } else if (flags
& BPF_FINALIZE_PKTAP
) {
1239 while (p
< hbuf
+ hbuf_len
) {
1241 struct pktap_header
*pktaphdr
;
1243 hp
= (struct bpf_hdr
*)(void *)p
;
1244 pktaphdr
= (struct pktap_header
*)(void *)
1245 (p
+ BPF_WORDALIGN(hp
->bh_hdrlen
));
1247 bpf_finalize_pktap(hp
, pktaphdr
);
1249 p
+= BPF_WORDALIGN(hp
->bh_hdrlen
+ hp
->bh_caplen
);
1255 * Move data from hold buffer into user space.
1256 * We know the entire buffer is transferred since
1257 * we checked above that the read buffer is bpf_bufsize bytes.
1259 error
= UIOMOVE(hbuf
, hbuf_len
, UIO_READ
, uio
);
1261 lck_mtx_lock(bpf_mlock
);
1263 * Make sure device is still opened
1265 if ((d
->bd_flags
& BPF_CLOSING
) != 0) {
1267 lck_mtx_unlock(bpf_mlock
);
1271 d
->bd_hbuf_read
= 0;
1272 d
->bd_fbuf
= d
->bd_hbuf
;
1279 lck_mtx_unlock(bpf_mlock
);
1284 * If there are processes sleeping on this descriptor, wake them up.
1287 bpf_wakeup(struct bpf_d
*d
)
1289 if (d
->bd_state
== BPF_WAITING
) {
1291 d
->bd_state
= BPF_IDLE
;
1294 if (d
->bd_async
&& d
->bd_sig
&& d
->bd_sigio
) {
1295 pgsigio(d
->bd_sigio
, d
->bd_sig
);
1298 selwakeup(&d
->bd_sel
);
1299 if ((d
->bd_flags
& BPF_KNOTE
)) {
1300 KNOTE(&d
->bd_sel
.si_note
, 1);
1305 bpf_timed_out(void *arg
, __unused
void *dummy
)
1307 struct bpf_d
*d
= (struct bpf_d
*)arg
;
1309 lck_mtx_lock(bpf_mlock
);
1310 if (d
->bd_state
== BPF_WAITING
) {
1312 * There's a select or kqueue waiting for this; if there's
1313 * now stuff to read, wake it up.
1315 d
->bd_state
= BPF_TIMED_OUT
;
1316 if (d
->bd_slen
!= 0) {
1319 } else if (d
->bd_state
== BPF_DRAINING
) {
1321 * A close is waiting for this to finish.
1322 * Mark it as finished, and wake the close up.
1324 d
->bd_state
= BPF_IDLE
;
1327 lck_mtx_unlock(bpf_mlock
);
1330 /* keep in sync with bpf_movein above: */
1331 #define MAX_DATALINK_HDR_LEN (sizeof(struct firewire_header))
1334 bpfwrite(dev_t dev
, struct uio
*uio
, __unused
int ioflag
)
1338 struct mbuf
*m
= NULL
;
1340 char dst_buf
[SOCKADDR_HDR_LEN
+ MAX_DATALINK_HDR_LEN
];
1345 lck_mtx_lock(bpf_mlock
);
1347 d
= bpf_dtab
[minor(dev
)];
1348 if (d
== NULL
|| d
== BPF_DEV_RESERVED
||
1349 (d
->bd_flags
& BPF_CLOSING
) != 0) {
1350 lck_mtx_unlock(bpf_mlock
);
1356 if (d
->bd_bif
== 0) {
1358 lck_mtx_unlock(bpf_mlock
);
1362 ifp
= d
->bd_bif
->bif_ifp
;
1364 if ((ifp
->if_flags
& IFF_UP
) == 0) {
1366 lck_mtx_unlock(bpf_mlock
);
1369 if (uio_resid(uio
) == 0) {
1371 lck_mtx_unlock(bpf_mlock
);
1374 ((struct sockaddr
*)dst_buf
)->sa_len
= sizeof(dst_buf
);
1377 * fix for PR-6849527
1378 * geting variables onto stack before dropping lock for bpf_movein()
1380 bif_dlt
= (int)d
->bd_bif
->bif_dlt
;
1381 bd_hdrcmplt
= d
->bd_hdrcmplt
;
1383 /* bpf_movein allocating mbufs; drop lock */
1384 lck_mtx_unlock(bpf_mlock
);
1386 error
= bpf_movein(uio
, bif_dlt
, &m
,
1387 bd_hdrcmplt
? NULL
: (struct sockaddr
*)dst_buf
,
1390 /* take the lock again */
1391 lck_mtx_lock(bpf_mlock
);
1394 lck_mtx_unlock(bpf_mlock
);
1398 /* verify the device is still open */
1399 if ((d
->bd_flags
& BPF_CLOSING
) != 0) {
1401 lck_mtx_unlock(bpf_mlock
);
1406 if (d
->bd_bif
== NULL
) {
1408 lck_mtx_unlock(bpf_mlock
);
1413 if ((unsigned)datlen
> ifp
->if_mtu
) {
1415 lck_mtx_unlock(bpf_mlock
);
1420 bpf_set_packet_service_class(m
, d
->bd_traffic_class
);
1422 lck_mtx_unlock(bpf_mlock
);
1425 * The driver frees the mbuf.
1427 if (d
->bd_hdrcmplt
) {
1428 if (d
->bd_bif
->bif_send
) {
1429 error
= d
->bd_bif
->bif_send(ifp
, d
->bd_bif
->bif_dlt
, m
);
1431 error
= dlil_output(ifp
, 0, m
, NULL
, NULL
, 1, NULL
);
1434 error
= dlil_output(ifp
, PF_INET
, m
, NULL
,
1435 (struct sockaddr
*)dst_buf
, 0, NULL
);
1438 lck_mtx_lock(bpf_mlock
);
1440 lck_mtx_unlock(bpf_mlock
);
1446 * Reset a descriptor by flushing its packet buffer and clearing the
1447 * receive and drop counts.
1450 reset_d(struct bpf_d
*d
)
1452 if (d
->bd_hbuf_read
!= 0) {
1453 panic("resetting buffers during read");
1457 /* Free the hold buffer. */
1458 d
->bd_fbuf
= d
->bd_hbuf
;
1469 static struct bpf_d
*
1470 bpf_get_device_from_uuid(uuid_t uuid
)
1474 for (i
= 0; i
< nbpfilter
; i
++) {
1475 struct bpf_d
*d
= bpf_dtab
[i
];
1477 if (d
== NULL
|| d
== BPF_DEV_RESERVED
||
1478 (d
->bd_flags
& BPF_CLOSING
) != 0) {
1481 if (uuid_compare(uuid
, d
->bd_uuid
) == 0) {
1490 * The BIOCSETUP command "atomically" attach to the interface and
1491 * copy the buffer from another interface. This minimizes the risk
1492 * of missing packet because this is done while holding
1493 * the BPF global lock
1496 bpf_setup(struct bpf_d
*d_to
, uuid_t uuid_from
, ifnet_t ifp
)
1498 struct bpf_d
*d_from
;
1501 LCK_MTX_ASSERT(bpf_mlock
, LCK_MTX_ASSERT_OWNED
);
1506 d_from
= bpf_get_device_from_uuid(uuid_from
);
1507 if (d_from
== NULL
) {
1509 os_log_info(OS_LOG_DEFAULT
,
1510 "%s: uuids not found error %d",
1514 if (d_from
->bd_opened_by
!= d_to
->bd_opened_by
) {
1516 os_log_info(OS_LOG_DEFAULT
,
1517 "%s: processes not matching error %d",
1523 * Prevent any read while copying
1525 while (d_to
->bd_hbuf_read
!= 0) {
1526 msleep((caddr_t
)d_to
, bpf_mlock
, PRINET
, __func__
, NULL
);
1528 d_to
->bd_hbuf_read
= 1;
1530 while (d_from
->bd_hbuf_read
!= 0) {
1531 msleep((caddr_t
)d_from
, bpf_mlock
, PRINET
, __func__
, NULL
);
1533 d_from
->bd_hbuf_read
= 1;
1536 * Verify the devices have not been closed
1538 if (d_to
->bd_flags
& BPF_CLOSING
) {
1540 os_log_info(OS_LOG_DEFAULT
,
1541 "%s: d_to is closing error %d",
1545 if (d_from
->bd_flags
& BPF_CLOSING
) {
1547 os_log_info(OS_LOG_DEFAULT
,
1548 "%s: d_from is closing error %d",
1554 * For now require the same buffer size
1556 if (d_from
->bd_bufsize
!= d_to
->bd_bufsize
) {
1558 os_log_info(OS_LOG_DEFAULT
,
1559 "%s: bufsizes not matching error %d",
1565 * Attach to the interface
1567 error
= bpf_setif(d_to
, ifp
, false, true);
1569 os_log_info(OS_LOG_DEFAULT
,
1570 "%s: bpf_setif() failed error %d",
1576 * Make sure the buffers are setup as expected by bpf_setif()
1578 ASSERT(d_to
->bd_hbuf
== NULL
);
1579 ASSERT(d_to
->bd_sbuf
!= NULL
);
1580 ASSERT(d_to
->bd_fbuf
!= NULL
);
1583 * Copy the buffers and update the pointers and counts
1585 memcpy(d_to
->bd_sbuf
, d_from
->bd_sbuf
, d_from
->bd_slen
);
1586 d_to
->bd_slen
= d_from
->bd_slen
;
1587 d_to
->bd_scnt
= d_from
->bd_scnt
;
1589 if (d_from
->bd_hbuf
!= NULL
) {
1590 d_to
->bd_hbuf
= d_to
->bd_fbuf
;
1591 d_to
->bd_fbuf
= NULL
;
1592 memcpy(d_to
->bd_hbuf
, d_from
->bd_hbuf
, d_from
->bd_hlen
);
1594 d_to
->bd_hlen
= d_from
->bd_hlen
;
1595 d_to
->bd_hcnt
= d_from
->bd_hcnt
;
1597 if (bpf_debug
> 0) {
1598 os_log_info(OS_LOG_DEFAULT
,
1599 "%s: done slen %u scnt %u hlen %u hcnt %u",
1600 __func__
, d_to
->bd_slen
, d_to
->bd_scnt
,
1601 d_to
->bd_hlen
, d_to
->bd_hcnt
);
1604 d_from
->bd_hbuf_read
= 0;
1605 wakeup((caddr_t
)d_from
);
1607 d_to
->bd_hbuf_read
= 0;
1608 wakeup((caddr_t
)d_to
);
1614 * FIONREAD Check for read packet available.
1615 * SIOCGIFADDR Get interface address - convenient hook to driver.
1616 * BIOCGBLEN Get buffer len [for read()].
1617 * BIOCSETF Set ethernet read filter.
1618 * BIOCFLUSH Flush read packet buffer.
1619 * BIOCPROMISC Put interface into promiscuous mode.
1620 * BIOCGDLT Get link layer type.
1621 * BIOCGETIF Get interface name.
1622 * BIOCSETIF Set interface.
1623 * BIOCSRTIMEOUT Set read timeout.
1624 * BIOCGRTIMEOUT Get read timeout.
1625 * BIOCGSTATS Get packet stats.
1626 * BIOCIMMEDIATE Set immediate mode.
1627 * BIOCVERSION Get filter language version.
1628 * BIOCGHDRCMPLT Get "header already complete" flag
1629 * BIOCSHDRCMPLT Set "header already complete" flag
1630 * BIOCGSEESENT Get "see packets sent" flag
1631 * BIOCSSEESENT Set "see packets sent" flag
1632 * BIOCSETTC Set traffic class.
1633 * BIOCGETTC Get traffic class.
1634 * BIOCSEXTHDR Set "extended header" flag
1635 * BIOCSHEADDROP Drop head of the buffer if user is not reading
1636 * BIOCGHEADDROP Get "head-drop" flag
1640 bpfioctl(dev_t dev
, u_long cmd
, caddr_t addr
, __unused
int flags
,
1648 lck_mtx_lock(bpf_mlock
);
1650 d
= bpf_dtab
[minor(dev
)];
1651 if (d
== NULL
|| d
== BPF_DEV_RESERVED
||
1652 (d
->bd_flags
& BPF_CLOSING
) != 0) {
1653 lck_mtx_unlock(bpf_mlock
);
1659 if (d
->bd_state
== BPF_WAITING
) {
1662 d
->bd_state
= BPF_IDLE
;
1670 * Check for read packet available.
1672 case FIONREAD
: /* int */
1677 if (d
->bd_hbuf
&& d
->bd_hbuf_read
== 0) {
1681 bcopy(&n
, addr
, sizeof(n
));
1685 case SIOCGIFADDR
: /* struct ifreq */
1689 if (d
->bd_bif
== 0) {
1692 ifp
= d
->bd_bif
->bif_ifp
;
1693 error
= ifnet_ioctl(ifp
, 0, cmd
, addr
);
1699 * Get buffer len [for read()].
1701 case BIOCGBLEN
: /* u_int */
1702 bcopy(&d
->bd_bufsize
, addr
, sizeof(u_int
));
1706 * Set buffer length.
1708 case BIOCSBLEN
: { /* u_int */
1710 unsigned int maxbufsize
= bpf_maxbufsize
;
1713 * Allow larger buffer in head drop mode to with the
1714 * assumption the reading process may be low priority but
1715 * is interested in the most recent traffic
1717 if (d
->bd_headdrop
!= 0) {
1718 maxbufsize
= 2 * bpf_maxbufsize
;
1721 if (d
->bd_bif
!= 0 || (d
->bd_flags
& BPF_DETACHING
)) {
1723 * Interface already attached, unable to change buffers
1728 bcopy(addr
, &size
, sizeof(size
));
1730 if (size
> maxbufsize
) {
1731 d
->bd_bufsize
= maxbufsize
;
1733 os_log_info(OS_LOG_DEFAULT
,
1734 "%s bufsize capped to %u from %u",
1735 __func__
, d
->bd_bufsize
, size
);
1736 } else if (size
< BPF_MINBUFSIZE
) {
1737 d
->bd_bufsize
= BPF_MINBUFSIZE
;
1739 os_log_info(OS_LOG_DEFAULT
,
1740 "%s bufsize bumped to %u from %u",
1741 __func__
, d
->bd_bufsize
, size
);
1743 d
->bd_bufsize
= size
;
1746 /* It's a read/write ioctl */
1747 bcopy(&d
->bd_bufsize
, addr
, sizeof(u_int
));
1751 * Set link layer read filter.
1754 case BIOCSETFNR32
: { /* struct bpf_program32 */
1755 struct bpf_program32 prg32
;
1757 bcopy(addr
, &prg32
, sizeof(prg32
));
1758 error
= bpf_setf(d
, prg32
.bf_len
,
1759 CAST_USER_ADDR_T(prg32
.bf_insns
), cmd
);
1764 case BIOCSETFNR64
: { /* struct bpf_program64 */
1765 struct bpf_program64 prg64
;
1767 bcopy(addr
, &prg64
, sizeof(prg64
));
1768 error
= bpf_setf(d
, prg64
.bf_len
, prg64
.bf_insns
, cmd
);
1773 * Flush read packet buffer.
1776 while (d
->bd_hbuf_read
!= 0) {
1777 msleep((caddr_t
)d
, bpf_mlock
, PRINET
, "bpf_reading",
1780 if ((d
->bd_flags
& BPF_CLOSING
) != 0) {
1788 * Put interface into promiscuous mode.
1791 if (d
->bd_bif
== 0) {
1793 * No interface attached yet.
1798 if (d
->bd_promisc
== 0) {
1799 lck_mtx_unlock(bpf_mlock
);
1800 error
= ifnet_set_promiscuous(d
->bd_bif
->bif_ifp
, 1);
1801 lck_mtx_lock(bpf_mlock
);
1809 * Get device parameters.
1811 case BIOCGDLT
: /* u_int */
1812 if (d
->bd_bif
== 0) {
1815 bcopy(&d
->bd_bif
->bif_dlt
, addr
, sizeof(u_int
));
1820 * Get a list of supported data link types.
1822 case BIOCGDLTLIST
: /* struct bpf_dltlist */
1823 if (d
->bd_bif
== NULL
) {
1826 error
= bpf_getdltlist(d
, addr
, p
);
1831 * Set data link type.
1833 case BIOCSDLT
: /* u_int */
1834 if (d
->bd_bif
== NULL
) {
1839 bcopy(addr
, &dlt
, sizeof(dlt
));
1841 if (dlt
== DLT_PKTAP
&&
1842 !(d
->bd_flags
& BPF_WANT_PKTAP
)) {
1845 error
= bpf_setdlt(d
, dlt
);
1850 * Get interface name.
1852 case BIOCGETIF
: /* struct ifreq */
1853 if (d
->bd_bif
== 0) {
1856 struct ifnet
*const ifp
= d
->bd_bif
->bif_ifp
;
1858 snprintf(((struct ifreq
*)(void *)addr
)->ifr_name
,
1859 sizeof(ifr
.ifr_name
), "%s", if_name(ifp
));
1866 case BIOCSETIF
: { /* struct ifreq */
1869 bcopy(addr
, &ifr
, sizeof(ifr
));
1870 ifr
.ifr_name
[IFNAMSIZ
- 1] = '\0';
1871 ifp
= ifunit(ifr
.ifr_name
);
1875 error
= bpf_setif(d
, ifp
, true, false);
1883 case BIOCSRTIMEOUT32
: { /* struct user32_timeval */
1884 struct user32_timeval _tv
;
1887 bcopy(addr
, &_tv
, sizeof(_tv
));
1888 tv
.tv_sec
= _tv
.tv_sec
;
1889 tv
.tv_usec
= _tv
.tv_usec
;
1892 * Subtract 1 tick from tvtohz() since this isn't
1895 if ((error
= itimerfix(&tv
)) == 0) {
1896 d
->bd_rtout
= tvtohz(&tv
) - 1;
1901 case BIOCSRTIMEOUT64
: { /* struct user64_timeval */
1902 struct user64_timeval _tv
;
1905 bcopy(addr
, &_tv
, sizeof(_tv
));
1906 tv
.tv_sec
= _tv
.tv_sec
;
1907 tv
.tv_usec
= _tv
.tv_usec
;
1910 * Subtract 1 tick from tvtohz() since this isn't
1913 if ((error
= itimerfix(&tv
)) == 0) {
1914 d
->bd_rtout
= tvtohz(&tv
) - 1;
1922 case BIOCGRTIMEOUT32
: { /* struct user32_timeval */
1923 struct user32_timeval tv
;
1925 bzero(&tv
, sizeof(tv
));
1926 tv
.tv_sec
= d
->bd_rtout
/ hz
;
1927 tv
.tv_usec
= (d
->bd_rtout
% hz
) * tick
;
1928 bcopy(&tv
, addr
, sizeof(tv
));
1932 case BIOCGRTIMEOUT64
: { /* struct user64_timeval */
1933 struct user64_timeval tv
;
1935 bzero(&tv
, sizeof(tv
));
1936 tv
.tv_sec
= d
->bd_rtout
/ hz
;
1937 tv
.tv_usec
= (d
->bd_rtout
% hz
) * tick
;
1938 bcopy(&tv
, addr
, sizeof(tv
));
1945 case BIOCGSTATS
: { /* struct bpf_stat */
1948 bzero(&bs
, sizeof(bs
));
1949 bs
.bs_recv
= d
->bd_rcount
;
1950 bs
.bs_drop
= d
->bd_dcount
;
1951 bcopy(&bs
, addr
, sizeof(bs
));
1956 * Set immediate mode.
1958 case BIOCIMMEDIATE
: /* u_int */
1959 d
->bd_immediate
= *(u_int
*)(void *)addr
;
1962 case BIOCVERSION
: { /* struct bpf_version */
1963 struct bpf_version bv
;
1965 bzero(&bv
, sizeof(bv
));
1966 bv
.bv_major
= BPF_MAJOR_VERSION
;
1967 bv
.bv_minor
= BPF_MINOR_VERSION
;
1968 bcopy(&bv
, addr
, sizeof(bv
));
1973 * Get "header already complete" flag
1975 case BIOCGHDRCMPLT
: /* u_int */
1976 bcopy(&d
->bd_hdrcmplt
, addr
, sizeof(u_int
));
1980 * Set "header already complete" flag
1982 case BIOCSHDRCMPLT
: /* u_int */
1983 bcopy(addr
, &int_arg
, sizeof(int_arg
));
1984 d
->bd_hdrcmplt
= int_arg
? 1 : 0;
1988 * Get "see sent packets" flag
1990 case BIOCGSEESENT
: /* u_int */
1991 bcopy(&d
->bd_seesent
, addr
, sizeof(u_int
));
1995 * Set "see sent packets" flag
1997 case BIOCSSEESENT
: /* u_int */
1998 bcopy(addr
, &d
->bd_seesent
, sizeof(u_int
));
2002 * Set traffic service class
2004 case BIOCSETTC
: { /* int */
2007 bcopy(addr
, &tc
, sizeof(int));
2008 error
= bpf_set_traffic_class(d
, tc
);
2013 * Get traffic service class
2015 case BIOCGETTC
: /* int */
2016 bcopy(&d
->bd_traffic_class
, addr
, sizeof(int));
2019 case FIONBIO
: /* Non-blocking I/O; int */
2022 case FIOASYNC
: /* Send signal on receive packets; int */
2023 bcopy(addr
, &d
->bd_async
, sizeof(int));
2027 error
= fsetown(*(int *)addr
, &d
->bd_sigio
);
2031 *(int *)addr
= fgetown(d
->bd_sigio
);
2034 /* This is deprecated, FIOSETOWN should be used instead. */
2036 error
= fsetown(-(*(int *)addr
), &d
->bd_sigio
);
2039 /* This is deprecated, FIOGETOWN should be used instead. */
2041 *(int *)addr
= -fgetown(d
->bd_sigio
);
2044 case BIOCSRSIG
: { /* Set receive signal; u_int */
2047 bcopy(addr
, &sig
, sizeof(u_int
));
2056 case BIOCGRSIG
: /* u_int */
2057 bcopy(&d
->bd_sig
, addr
, sizeof(u_int
));
2060 case BIOCSEXTHDR
: /* u_int */
2061 bcopy(addr
, &int_arg
, sizeof(int_arg
));
2063 d
->bd_flags
|= BPF_EXTENDED_HDR
;
2065 d
->bd_flags
&= ~BPF_EXTENDED_HDR
;
2069 case BIOCGIFATTACHCOUNT
: { /* struct ifreq */
2073 bcopy(addr
, &ifr
, sizeof(ifr
));
2074 ifr
.ifr_name
[IFNAMSIZ
- 1] = '\0';
2075 ifp
= ifunit(ifr
.ifr_name
);
2081 for (bp
= bpf_iflist
; bp
!= 0; bp
= bp
->bif_next
) {
2082 struct bpf_d
*bpf_d
;
2084 if (bp
->bif_ifp
== NULL
|| bp
->bif_ifp
!= ifp
) {
2087 for (bpf_d
= bp
->bif_dlist
; bpf_d
;
2088 bpf_d
= bpf_d
->bd_next
) {
2089 ifr
.ifr_intval
+= 1;
2092 bcopy(&ifr
, addr
, sizeof(ifr
));
2095 case BIOCGWANTPKTAP
: /* u_int */
2096 int_arg
= d
->bd_flags
& BPF_WANT_PKTAP
? 1 : 0;
2097 bcopy(&int_arg
, addr
, sizeof(int_arg
));
2100 case BIOCSWANTPKTAP
: /* u_int */
2101 bcopy(addr
, &int_arg
, sizeof(int_arg
));
2103 d
->bd_flags
|= BPF_WANT_PKTAP
;
2105 d
->bd_flags
&= ~BPF_WANT_PKTAP
;
2111 bcopy(addr
, &int_arg
, sizeof(int_arg
));
2112 d
->bd_headdrop
= int_arg
? 1 : 0;
2116 bcopy(&d
->bd_headdrop
, addr
, sizeof(int));
2120 bcopy(addr
, &int_arg
, sizeof(int_arg
));
2122 d
->bd_flags
|= BPF_TRUNCATE
;
2124 d
->bd_flags
&= ~BPF_TRUNCATE
;
2129 bcopy(&d
->bd_uuid
, addr
, sizeof(uuid_t
));
2133 struct bpf_setup_args bsa
;
2136 bcopy(addr
, &bsa
, sizeof(struct bpf_setup_args
));
2137 bsa
.bsa_ifname
[IFNAMSIZ
- 1] = 0;
2138 ifp
= ifunit(bsa
.bsa_ifname
);
2141 os_log_info(OS_LOG_DEFAULT
,
2142 "%s: ifnet not found for %s error %d",
2143 __func__
, bsa
.bsa_ifname
, error
);
2147 error
= bpf_setup(d
, bsa
.bsa_uuid
, ifp
);
2151 bcopy(addr
, &int_arg
, sizeof(int_arg
));
2153 d
->bd_flags
|= BPF_PKTHDRV2
;
2155 d
->bd_flags
&= ~BPF_PKTHDRV2
;
2160 int_arg
= d
->bd_flags
& BPF_PKTHDRV2
? 1 : 0;
2161 bcopy(&int_arg
, addr
, sizeof(int));
2166 lck_mtx_unlock(bpf_mlock
);
2172 * Set d's packet filter program to fp. If this file already has a filter,
2173 * free it and replace it. Returns EINVAL for bogus requests.
2176 bpf_setf(struct bpf_d
*d
, u_int bf_len
, user_addr_t bf_insns
,
2179 struct bpf_insn
*fcode
, *old
;
2182 while (d
->bd_hbuf_read
!= 0) {
2183 msleep((caddr_t
)d
, bpf_mlock
, PRINET
, "bpf_reading", NULL
);
2186 if ((d
->bd_flags
& BPF_CLOSING
) != 0) {
2191 if (bf_insns
== USER_ADDR_NULL
) {
2195 d
->bd_filter
= NULL
;
2198 FREE(old
, M_DEVBUF
);
2203 if (flen
> BPF_MAXINSNS
) {
2207 size
= flen
* sizeof(struct bpf_insn
);
2208 fcode
= (struct bpf_insn
*) _MALLOC(size
, M_DEVBUF
, M_WAIT
);
2210 if (fcode
== NULL
) {
2214 if (copyin(bf_insns
, (caddr_t
)fcode
, size
) == 0 &&
2215 bpf_validate(fcode
, (int)flen
)) {
2216 d
->bd_filter
= fcode
;
2218 if (cmd
== BIOCSETF32
|| cmd
== BIOCSETF64
) {
2223 FREE(old
, M_DEVBUF
);
2228 FREE(fcode
, M_DEVBUF
);
2233 * Detach a file from its current interface (if attached at all) and attach
2234 * to the interface indicated by the name stored in ifr.
2235 * Return an errno or 0.
2238 bpf_setif(struct bpf_d
*d
, ifnet_t theywant
, bool do_reset
, bool has_hbuf_read
)
2243 while (d
->bd_hbuf_read
!= 0 && !has_hbuf_read
) {
2244 msleep((caddr_t
)d
, bpf_mlock
, PRINET
, "bpf_reading", NULL
);
2247 if ((d
->bd_flags
& BPF_CLOSING
) != 0) {
2252 * Look through attached interfaces for the named one.
2254 for (bp
= bpf_iflist
; bp
!= 0; bp
= bp
->bif_next
) {
2255 struct ifnet
*ifp
= bp
->bif_ifp
;
2257 if (ifp
== 0 || ifp
!= theywant
) {
2261 * Do not use DLT_PKTAP, unless requested explicitly
2263 if (bp
->bif_dlt
== DLT_PKTAP
&& !(d
->bd_flags
& BPF_WANT_PKTAP
)) {
2267 * Skip the coprocessor interface
2269 if (!intcoproc_unrestricted
&& IFNET_IS_INTCOPROC(ifp
)) {
2273 * We found the requested interface.
2274 * Allocate the packet buffers.
2276 error
= bpf_allocbufs(d
);
2281 * Detach if attached to something else.
2283 if (bp
!= d
->bd_bif
) {
2284 if (d
->bd_bif
!= NULL
) {
2285 if (bpf_detachd(d
, 0) != 0) {
2289 if (bpf_attachd(d
, bp
) != 0) {
2303 * Get a list of available data link type of the interface.
2306 bpf_getdltlist(struct bpf_d
*d
, caddr_t addr
, struct proc
*p
)
2313 struct bpf_dltlist bfl
;
2315 bcopy(addr
, &bfl
, sizeof(bfl
));
2316 if (proc_is64bit(p
)) {
2317 dlist
= (user_addr_t
)bfl
.bfl_u
.bflu_pad
;
2319 dlist
= CAST_USER_ADDR_T(bfl
.bfl_u
.bflu_list
);
2322 ifp
= d
->bd_bif
->bif_ifp
;
2326 for (bp
= bpf_iflist
; bp
; bp
= bp
->bif_next
) {
2327 if (bp
->bif_ifp
!= ifp
) {
2331 * Do not use DLT_PKTAP, unless requested explicitly
2333 if (bp
->bif_dlt
== DLT_PKTAP
&& !(d
->bd_flags
& BPF_WANT_PKTAP
)) {
2336 if (dlist
!= USER_ADDR_NULL
) {
2337 if (n
>= bfl
.bfl_len
) {
2340 error
= copyout(&bp
->bif_dlt
, dlist
,
2341 sizeof(bp
->bif_dlt
));
2345 dlist
+= sizeof(bp
->bif_dlt
);
2350 bcopy(&bfl
, addr
, sizeof(bfl
));
2356 * Set the data link type of a BPF instance.
2359 bpf_setdlt(struct bpf_d
*d
, uint32_t dlt
)
2361 int error
, opromisc
;
2365 if (d
->bd_bif
->bif_dlt
== dlt
) {
2369 while (d
->bd_hbuf_read
!= 0) {
2370 msleep((caddr_t
)d
, bpf_mlock
, PRINET
, "bpf_reading", NULL
);
2373 if ((d
->bd_flags
& BPF_CLOSING
) != 0) {
2377 ifp
= d
->bd_bif
->bif_ifp
;
2378 for (bp
= bpf_iflist
; bp
; bp
= bp
->bif_next
) {
2379 if (bp
->bif_ifp
== ifp
&& bp
->bif_dlt
== dlt
) {
2381 * Do not use DLT_PKTAP, unless requested explicitly
2383 if (bp
->bif_dlt
== DLT_PKTAP
&&
2384 !(d
->bd_flags
& BPF_WANT_PKTAP
)) {
2391 opromisc
= d
->bd_promisc
;
2392 if (bpf_detachd(d
, 0) != 0) {
2395 error
= bpf_attachd(d
, bp
);
2397 printf("bpf_setdlt: bpf_attachd %s%d failed (%d)\n",
2398 ifnet_name(bp
->bif_ifp
), ifnet_unit(bp
->bif_ifp
),
2404 lck_mtx_unlock(bpf_mlock
);
2405 error
= ifnet_set_promiscuous(bp
->bif_ifp
, 1);
2406 lck_mtx_lock(bpf_mlock
);
2408 printf("%s: ifpromisc %s%d failed (%d)\n",
2409 __func__
, ifnet_name(bp
->bif_ifp
),
2410 ifnet_unit(bp
->bif_ifp
), error
);
2416 return bp
== NULL
? EINVAL
: 0;
2420 bpf_set_traffic_class(struct bpf_d
*d
, int tc
)
2424 if (!SO_VALID_TC(tc
)) {
2427 d
->bd_traffic_class
= tc
;
2434 bpf_set_packet_service_class(struct mbuf
*m
, int tc
)
2436 if (!(m
->m_flags
& M_PKTHDR
)) {
2440 VERIFY(SO_VALID_TC(tc
));
2441 (void) m_set_service_class(m
, so_tc2msc(tc
));
2445 * Support for select()
2447 * Return true iff the specific operation will not block indefinitely.
2448 * Otherwise, return false but make a note that a selwakeup() must be done.
2451 bpfselect(dev_t dev
, int which
, void * wql
, struct proc
*p
)
2456 lck_mtx_lock(bpf_mlock
);
2458 d
= bpf_dtab
[minor(dev
)];
2459 if (d
== NULL
|| d
== BPF_DEV_RESERVED
||
2460 (d
->bd_flags
& BPF_CLOSING
) != 0) {
2461 lck_mtx_unlock(bpf_mlock
);
2467 if (d
->bd_bif
== NULL
) {
2469 lck_mtx_unlock(bpf_mlock
);
2473 while (d
->bd_hbuf_read
!= 0) {
2474 msleep((caddr_t
)d
, bpf_mlock
, PRINET
, "bpf_reading", NULL
);
2477 if ((d
->bd_flags
& BPF_CLOSING
) != 0) {
2479 lck_mtx_unlock(bpf_mlock
);
2485 if (d
->bd_hlen
!= 0 ||
2486 ((d
->bd_immediate
||
2487 d
->bd_state
== BPF_TIMED_OUT
) && d
->bd_slen
!= 0)) {
2488 ret
= 1; /* read has data to return */
2491 * Read has no data to return.
2492 * Make the select wait, and start a timer if
2495 selrecord(p
, &d
->bd_sel
, wql
);
2501 /* can't determine whether a write would block */
2507 lck_mtx_unlock(bpf_mlock
);
2513 * Support for kevent() system call. Register EVFILT_READ filters and
2514 * reject all others.
2516 int bpfkqfilter(dev_t dev
, struct knote
*kn
);
2517 static void filt_bpfdetach(struct knote
*);
2518 static int filt_bpfread(struct knote
*, long);
2519 static int filt_bpftouch(struct knote
*kn
, struct kevent_qos_s
*kev
);
2520 static int filt_bpfprocess(struct knote
*kn
, struct kevent_qos_s
*kev
);
2522 SECURITY_READ_ONLY_EARLY(struct filterops
) bpfread_filtops
= {
2524 .f_detach
= filt_bpfdetach
,
2525 .f_event
= filt_bpfread
,
2526 .f_touch
= filt_bpftouch
,
2527 .f_process
= filt_bpfprocess
,
2531 filt_bpfread_common(struct knote
*kn
, struct kevent_qos_s
*kev
, struct bpf_d
*d
)
2536 if (d
->bd_immediate
) {
2538 * If there's data in the hold buffer, it's the
2539 * amount of data a read will return.
2541 * If there's no data in the hold buffer, but
2542 * there's data in the store buffer, a read will
2543 * immediately rotate the store buffer to the
2544 * hold buffer, the amount of data in the store
2545 * buffer is the amount of data a read will
2548 * If there's no data in either buffer, we're not
2551 data
= (d
->bd_hlen
== 0 || d
->bd_hbuf_read
!= 0 ?
2552 d
->bd_slen
: d
->bd_hlen
);
2553 int64_t lowwat
= knote_low_watermark(kn
);
2554 if (lowwat
> d
->bd_bufsize
) {
2555 lowwat
= d
->bd_bufsize
;
2557 ready
= (data
>= lowwat
);
2560 * If there's data in the hold buffer, it's the
2561 * amount of data a read will return.
2563 * If there's no data in the hold buffer, but
2564 * there's data in the store buffer, if the
2565 * timer has expired a read will immediately
2566 * rotate the store buffer to the hold buffer,
2567 * so the amount of data in the store buffer is
2568 * the amount of data a read will return.
2570 * If there's no data in either buffer, or there's
2571 * no data in the hold buffer and the timer hasn't
2572 * expired, we're not ready to read.
2574 data
= ((d
->bd_hlen
== 0 || d
->bd_hbuf_read
!= 0) &&
2575 d
->bd_state
== BPF_TIMED_OUT
? d
->bd_slen
: d
->bd_hlen
);
2581 knote_fill_kevent(kn
, kev
, data
);
2588 bpfkqfilter(dev_t dev
, struct knote
*kn
)
2594 * Is this device a bpf?
2596 if (major(dev
) != CDEV_MAJOR
|| kn
->kn_filter
!= EVFILT_READ
) {
2597 knote_set_error(kn
, EINVAL
);
2601 lck_mtx_lock(bpf_mlock
);
2603 d
= bpf_dtab
[minor(dev
)];
2605 if (d
== NULL
|| d
== BPF_DEV_RESERVED
||
2606 (d
->bd_flags
& BPF_CLOSING
) != 0 ||
2607 d
->bd_bif
== NULL
) {
2608 lck_mtx_unlock(bpf_mlock
);
2609 knote_set_error(kn
, ENXIO
);
2614 kn
->kn_filtid
= EVFILTID_BPFREAD
;
2615 KNOTE_ATTACH(&d
->bd_sel
.si_note
, kn
);
2616 d
->bd_flags
|= BPF_KNOTE
;
2618 /* capture the current state */
2619 res
= filt_bpfread_common(kn
, NULL
, d
);
2621 lck_mtx_unlock(bpf_mlock
);
2627 filt_bpfdetach(struct knote
*kn
)
2629 struct bpf_d
*d
= (struct bpf_d
*)kn
->kn_hook
;
2631 lck_mtx_lock(bpf_mlock
);
2632 if (d
->bd_flags
& BPF_KNOTE
) {
2633 KNOTE_DETACH(&d
->bd_sel
.si_note
, kn
);
2634 d
->bd_flags
&= ~BPF_KNOTE
;
2636 lck_mtx_unlock(bpf_mlock
);
2640 filt_bpfread(struct knote
*kn
, long hint
)
2642 #pragma unused(hint)
2643 struct bpf_d
*d
= (struct bpf_d
*)kn
->kn_hook
;
2645 return filt_bpfread_common(kn
, NULL
, d
);
2649 filt_bpftouch(struct knote
*kn
, struct kevent_qos_s
*kev
)
2651 struct bpf_d
*d
= (struct bpf_d
*)kn
->kn_hook
;
2654 lck_mtx_lock(bpf_mlock
);
2656 /* save off the lowat threshold and flag */
2657 kn
->kn_sdata
= kev
->data
;
2658 kn
->kn_sfflags
= kev
->fflags
;
2660 /* output data will be re-generated here */
2661 res
= filt_bpfread_common(kn
, NULL
, d
);
2663 lck_mtx_unlock(bpf_mlock
);
2669 filt_bpfprocess(struct knote
*kn
, struct kevent_qos_s
*kev
)
2671 struct bpf_d
*d
= (struct bpf_d
*)kn
->kn_hook
;
2674 lck_mtx_lock(bpf_mlock
);
2675 res
= filt_bpfread_common(kn
, kev
, d
);
2676 lck_mtx_unlock(bpf_mlock
);
2682 * Copy data from an mbuf chain into a buffer. This code is derived
2683 * from m_copydata in kern/uipc_mbuf.c.
2686 bpf_mcopy(struct mbuf
* m
, void *dst_arg
, size_t len
)
2696 count
= min(m
->m_len
, len
);
2697 bcopy(mbuf_data(m
), dst
, count
);
2708 struct bpf_packet
*bpf_pkt
,
2716 * It's possible that we get here after the bpf descriptor has been
2717 * detached from the interface; in such a case we simply return.
2718 * Lock ordering is important since we can be called asynchronously
2719 * (from IOKit) to process an inbound packet; when that happens
2720 * we would have been holding its "gateLock" and will be acquiring
2721 * "bpf_mlock" upon entering this routine. Due to that, we release
2722 * "bpf_mlock" prior to calling ifnet_set_promiscuous (which will
2723 * acquire "gateLock" in the IOKit), in order to avoid a deadlock
2724 * when a ifnet_set_promiscuous request simultaneously collides with
2725 * an inbound packet being passed into the tap callback.
2727 lck_mtx_lock(bpf_mlock
);
2728 if (ifp
->if_bpf
== NULL
) {
2729 lck_mtx_unlock(bpf_mlock
);
2732 for (bp
= ifp
->if_bpf
; bp
!= NULL
; bp
= bp
->bif_next
) {
2733 if (bp
->bif_ifp
!= ifp
) {
2734 /* wrong interface */
2738 if (dlt
== 0 || bp
->bif_dlt
== dlt
) {
2739 /* tapping default DLT or DLT matches */
2746 for (d
= bp
->bif_dlist
; d
; d
= d
->bd_next
) {
2747 struct bpf_packet
*bpf_pkt_saved
= bpf_pkt
;
2748 struct bpf_packet bpf_pkt_tmp
;
2749 struct pktap_header_buffer bpfp_header_tmp
;
2751 if (outbound
&& !d
->bd_seesent
) {
2756 slen
= bpf_filter(d
->bd_filter
, (u_char
*)bpf_pkt
,
2757 bpf_pkt
->bpfp_total_length
, 0);
2758 if (bp
->bif_ifp
->if_type
== IFT_PKTAP
&&
2759 bp
->bif_dlt
== DLT_PKTAP
) {
2761 * Need to copy the bpf_pkt because the conversion
2762 * to v2 pktap header modifies the content of the
2765 if ((d
->bd_flags
& BPF_PKTHDRV2
) &&
2766 bpf_pkt
->bpfp_header_length
<= sizeof(bpfp_header_tmp
)) {
2767 bpf_pkt_tmp
= *bpf_pkt
;
2769 bpf_pkt
= &bpf_pkt_tmp
;
2771 memcpy(&bpfp_header_tmp
, bpf_pkt
->bpfp_header
,
2772 bpf_pkt
->bpfp_header_length
);
2774 bpf_pkt
->bpfp_header
= &bpfp_header_tmp
;
2776 convert_to_pktap_header_to_v2(bpf_pkt
,
2777 !!(d
->bd_flags
& BPF_TRUNCATE
));
2780 if (d
->bd_flags
& BPF_TRUNCATE
) {
2782 get_pkt_trunc_len((u_char
*)bpf_pkt
,
2783 bpf_pkt
->bpfp_total_length
));
2787 catchpacket(d
, bpf_pkt
, slen
, outbound
);
2789 bpf_pkt
= bpf_pkt_saved
;
2793 lck_mtx_unlock(bpf_mlock
);
2805 struct bpf_packet bpf_pkt
;
2808 if (ifp
->if_bpf
== NULL
) {
2809 /* quickly check without taking lock */
2812 bpf_pkt
.bpfp_type
= BPF_PACKET_TYPE_MBUF
;
2813 bpf_pkt
.bpfp_mbuf
= m
;
2814 bpf_pkt
.bpfp_total_length
= 0;
2815 for (m0
= m
; m0
!= NULL
; m0
= m0
->m_next
) {
2816 bpf_pkt
.bpfp_total_length
+= m0
->m_len
;
2818 bpf_pkt
.bpfp_header
= hdr
;
2820 bpf_pkt
.bpfp_total_length
+= hlen
;
2821 bpf_pkt
.bpfp_header_length
= hlen
;
2823 bpf_pkt
.bpfp_header_length
= 0;
2825 bpf_tap_imp(ifp
, dlt
, &bpf_pkt
, outbound
);
2836 bpf_tap_mbuf(ifp
, dlt
, m
, hdr
, hlen
, 1);
2847 bpf_tap_mbuf(ifp
, dlt
, m
, hdr
, hlen
, 0);
2850 /* Callback registered with Ethernet driver. */
2852 bpf_tap_callback(struct ifnet
*ifp
, struct mbuf
*m
)
2854 bpf_tap_mbuf(ifp
, 0, m
, NULL
, 0, mbuf_pkthdr_rcvif(m
) == NULL
);
2861 bpf_copydata(struct bpf_packet
*pkt
, size_t off
, size_t len
, void* out_data
)
2864 if (pkt
->bpfp_type
== BPF_PACKET_TYPE_MBUF
) {
2865 err
= mbuf_copydata(pkt
->bpfp_mbuf
, off
, len
, out_data
);
2874 copy_bpf_packet(struct bpf_packet
* pkt
, void * dst
, size_t len
)
2876 /* copy the optional header */
2877 if (pkt
->bpfp_header_length
!= 0) {
2878 size_t count
= min(len
, pkt
->bpfp_header_length
);
2879 bcopy(pkt
->bpfp_header
, dst
, count
);
2884 /* nothing past the header */
2887 /* copy the packet */
2888 switch (pkt
->bpfp_type
) {
2889 case BPF_PACKET_TYPE_MBUF
:
2890 bpf_mcopy(pkt
->bpfp_mbuf
, dst
, len
);
2898 get_esp_trunc_len(__unused
struct bpf_packet
*pkt
, __unused
uint16_t off
,
2899 const uint16_t remaining_caplen
)
2902 * For some reason tcpdump expects to have one byte beyond the ESP header
2904 uint16_t trunc_len
= ESP_HDR_SIZE
+ 1;
2906 if (trunc_len
> remaining_caplen
) {
2907 return remaining_caplen
;
2914 get_isakmp_trunc_len(__unused
struct bpf_packet
*pkt
, __unused
uint16_t off
,
2915 const uint16_t remaining_caplen
)
2918 * Include the payload generic header
2920 uint16_t trunc_len
= ISAKMP_HDR_SIZE
;
2922 if (trunc_len
> remaining_caplen
) {
2923 return remaining_caplen
;
2930 get_isakmp_natt_trunc_len(struct bpf_packet
*pkt
, uint16_t off
,
2931 const uint16_t remaining_caplen
)
2934 uint16_t trunc_len
= 0;
2935 char payload
[remaining_caplen
];
2937 err
= bpf_copydata(pkt
, off
, remaining_caplen
, payload
);
2939 return remaining_caplen
;
2942 * They are three cases:
2943 * - IKE: payload start with 4 bytes header set to zero before ISAKMP header
2944 * - keep alive: 1 byte payload
2945 * - otherwise it's ESP
2947 if (remaining_caplen
>= 4 &&
2948 payload
[0] == 0 && payload
[1] == 0 &&
2949 payload
[2] == 0 && payload
[3] == 0) {
2950 trunc_len
= 4 + get_isakmp_trunc_len(pkt
, off
+ 4, remaining_caplen
- 4);
2951 } else if (remaining_caplen
== 1) {
2954 trunc_len
= get_esp_trunc_len(pkt
, off
, remaining_caplen
);
2957 if (trunc_len
> remaining_caplen
) {
2958 return remaining_caplen
;
2965 get_udp_trunc_len(struct bpf_packet
*pkt
, uint16_t off
, const uint16_t remaining_caplen
)
2968 uint16_t trunc_len
= sizeof(struct udphdr
); /* By default no UDP payload */
2970 if (trunc_len
>= remaining_caplen
) {
2971 return remaining_caplen
;
2974 struct udphdr udphdr
;
2975 err
= bpf_copydata(pkt
, off
, sizeof(struct udphdr
), &udphdr
);
2977 return remaining_caplen
;
2980 u_short sport
, dport
;
2982 sport
= EXTRACT_SHORT(&udphdr
.uh_sport
);
2983 dport
= EXTRACT_SHORT(&udphdr
.uh_dport
);
2985 if (dport
== PORT_DNS
|| sport
== PORT_DNS
) {
2987 * Full UDP payload for DNS
2989 trunc_len
= remaining_caplen
;
2990 } else if ((sport
== PORT_BOOTPS
&& dport
== PORT_BOOTPC
) ||
2991 (sport
== PORT_BOOTPC
&& dport
== PORT_BOOTPS
)) {
2993 * Full UDP payload for BOOTP and DHCP
2995 trunc_len
= remaining_caplen
;
2996 } else if (dport
== PORT_ISAKMP
&& sport
== PORT_ISAKMP
) {
2998 * Return the ISAKMP header
3000 trunc_len
+= get_isakmp_trunc_len(pkt
, off
+ sizeof(struct udphdr
),
3001 remaining_caplen
- sizeof(struct udphdr
));
3002 } else if (dport
== PORT_ISAKMP_NATT
&& sport
== PORT_ISAKMP_NATT
) {
3003 trunc_len
+= get_isakmp_natt_trunc_len(pkt
, off
+ sizeof(struct udphdr
),
3004 remaining_caplen
- sizeof(struct udphdr
));
3006 if (trunc_len
>= remaining_caplen
) {
3007 return remaining_caplen
;
3014 get_tcp_trunc_len(struct bpf_packet
*pkt
, uint16_t off
, const uint16_t remaining_caplen
)
3017 uint16_t trunc_len
= sizeof(struct tcphdr
); /* By default no TCP payload */
3018 if (trunc_len
>= remaining_caplen
) {
3019 return remaining_caplen
;
3022 struct tcphdr tcphdr
;
3023 err
= bpf_copydata(pkt
, off
, sizeof(struct tcphdr
), &tcphdr
);
3025 return remaining_caplen
;
3028 u_short sport
, dport
;
3029 sport
= EXTRACT_SHORT(&tcphdr
.th_sport
);
3030 dport
= EXTRACT_SHORT(&tcphdr
.th_dport
);
3032 if (dport
== PORT_DNS
|| sport
== PORT_DNS
) {
3034 * Full TCP payload for DNS
3036 trunc_len
= remaining_caplen
;
3038 trunc_len
= tcphdr
.th_off
<< 2;
3040 if (trunc_len
>= remaining_caplen
) {
3041 return remaining_caplen
;
3048 get_proto_trunc_len(uint8_t proto
, struct bpf_packet
*pkt
, uint16_t off
, const uint16_t remaining_caplen
)
3053 case IPPROTO_ICMP
: {
3057 trunc_len
= remaining_caplen
;
3060 case IPPROTO_ICMPV6
: {
3062 * Full IMCPV6 payload
3064 trunc_len
= remaining_caplen
;
3067 case IPPROTO_IGMP
: {
3071 trunc_len
= remaining_caplen
;
3075 trunc_len
= get_udp_trunc_len(pkt
, off
, remaining_caplen
);
3079 trunc_len
= get_tcp_trunc_len(pkt
, off
, remaining_caplen
);
3083 trunc_len
= get_esp_trunc_len(pkt
, off
, remaining_caplen
);
3088 * By default we only include the IP header
3094 if (trunc_len
>= remaining_caplen
) {
3095 return remaining_caplen
;
3102 get_ip_trunc_len(struct bpf_packet
*pkt
, uint16_t off
, const uint16_t remaining_caplen
)
3105 uint16_t iplen
= sizeof(struct ip
);
3106 if (iplen
>= remaining_caplen
) {
3107 return remaining_caplen
;
3111 err
= bpf_copydata(pkt
, off
, sizeof(struct ip
), &iphdr
);
3113 return remaining_caplen
;
3118 iplen
= iphdr
.ip_hl
<< 2;
3119 if (iplen
>= remaining_caplen
) {
3120 return remaining_caplen
;
3124 iplen
+= get_proto_trunc_len(proto
, pkt
, off
+ iplen
, remaining_caplen
- iplen
);
3126 if (iplen
>= remaining_caplen
) {
3127 return remaining_caplen
;
3134 get_ip6_trunc_len(struct bpf_packet
*pkt
, uint16_t off
, const uint16_t remaining_caplen
)
3137 uint16_t iplen
= sizeof(struct ip6_hdr
);
3138 if (iplen
>= remaining_caplen
) {
3139 return remaining_caplen
;
3142 struct ip6_hdr ip6hdr
;
3143 err
= bpf_copydata(pkt
, off
, sizeof(struct ip6_hdr
), &ip6hdr
);
3145 return remaining_caplen
;
3151 * TBD: process the extension headers
3153 proto
= ip6hdr
.ip6_nxt
;
3154 iplen
+= get_proto_trunc_len(proto
, pkt
, off
+ iplen
, remaining_caplen
- iplen
);
3156 if (iplen
>= remaining_caplen
) {
3157 return remaining_caplen
;
3164 get_ether_trunc_len(struct bpf_packet
*pkt
, int off
, const uint16_t remaining_caplen
)
3167 uint16_t ethlen
= sizeof(struct ether_header
);
3168 if (ethlen
>= remaining_caplen
) {
3169 return remaining_caplen
;
3172 struct ether_header eh
;
3174 err
= bpf_copydata(pkt
, off
, sizeof(struct ether_header
), &eh
);
3176 return remaining_caplen
;
3179 type
= EXTRACT_SHORT(&eh
.ether_type
);
3180 /* Include full ARP */
3181 if (type
== ETHERTYPE_ARP
) {
3182 ethlen
= remaining_caplen
;
3183 } else if (type
!= ETHERTYPE_IP
&& type
!= ETHERTYPE_IPV6
) {
3184 ethlen
= min(BPF_MIN_PKT_SIZE
, remaining_caplen
);
3186 if (type
== ETHERTYPE_IP
) {
3187 ethlen
+= get_ip_trunc_len(pkt
, sizeof(struct ether_header
),
3189 } else if (type
== ETHERTYPE_IPV6
) {
3190 ethlen
+= get_ip6_trunc_len(pkt
, sizeof(struct ether_header
),
3198 get_pkt_trunc_len(u_char
*p
, u_int len
)
3200 struct bpf_packet
*pkt
= (struct bpf_packet
*)(void *) p
;
3201 struct pktap_header
*pktap
= (struct pktap_header
*) (pkt
->bpfp_header
);
3202 uint32_t out_pkt_len
= 0, tlen
= 0;
3204 * pktap->pth_frame_pre_length is L2 header length and accounts
3205 * for both pre and pre_adjust.
3206 * pktap->pth_length is sizeof(pktap_header) (excl the pre/pre_adjust)
3207 * pkt->bpfp_header_length is (pktap->pth_length + pre_adjust)
3208 * pre is the offset to the L3 header after the bpfp_header, or length
3209 * of L2 header after bpfp_header, if present.
3211 int32_t pre
= pktap
->pth_frame_pre_length
-
3212 (pkt
->bpfp_header_length
- pktap
->pth_length
);
3214 /* Length of the input packet starting from L3 header */
3215 uint32_t in_pkt_len
= len
- pkt
->bpfp_header_length
- pre
;
3216 if (pktap
->pth_protocol_family
== AF_INET
||
3217 pktap
->pth_protocol_family
== AF_INET6
) {
3218 /* Contains L2 header */
3220 if (pre
< (int32_t)sizeof(struct ether_header
)) {
3224 out_pkt_len
= get_ether_trunc_len(pkt
, 0, in_pkt_len
);
3225 } else if (pre
== 0) {
3226 if (pktap
->pth_protocol_family
== AF_INET
) {
3227 out_pkt_len
= get_ip_trunc_len(pkt
, pre
, in_pkt_len
);
3228 } else if (pktap
->pth_protocol_family
== AF_INET6
) {
3229 out_pkt_len
= get_ip6_trunc_len(pkt
, pre
, in_pkt_len
);
3232 /* Ideally pre should be >= 0. This is an exception */
3233 out_pkt_len
= min(BPF_MIN_PKT_SIZE
, in_pkt_len
);
3236 if (pktap
->pth_iftype
== IFT_ETHER
) {
3237 if (in_pkt_len
< sizeof(struct ether_header
)) {
3240 /* At most include the Ethernet header and 16 bytes */
3241 out_pkt_len
= MIN(sizeof(struct ether_header
) + 16,
3245 * For unknown protocols include at most 16 bytes
3247 out_pkt_len
= MIN(16, in_pkt_len
);
3251 tlen
= pkt
->bpfp_header_length
+ out_pkt_len
+ pre
;
3254 out_pkt_len
= in_pkt_len
;
3259 * Move the packet data from interface memory (pkt) into the
3260 * store buffer. Return 1 if it's time to wakeup a listener (buffer full),
3264 catchpacket(struct bpf_d
*d
, struct bpf_packet
* pkt
,
3265 u_int snaplen
, int outbound
)
3268 struct bpf_hdr_ext
*ehp
;
3275 hdrlen
= (d
->bd_flags
& BPF_EXTENDED_HDR
) ? d
->bd_bif
->bif_exthdrlen
:
3276 d
->bd_bif
->bif_hdrlen
;
3278 * Figure out how many bytes to move. If the packet is
3279 * greater or equal to the snapshot length, transfer that
3280 * much. Otherwise, transfer the whole packet (unless
3281 * we hit the buffer size limit).
3283 totlen
= hdrlen
+ min(snaplen
, pkt
->bpfp_total_length
);
3284 if (totlen
> d
->bd_bufsize
) {
3285 totlen
= d
->bd_bufsize
;
3288 if (hdrlen
> totlen
) {
3293 * Round up the end of the previous packet to the next longword.
3295 curlen
= BPF_WORDALIGN(d
->bd_slen
);
3296 if (curlen
+ totlen
> d
->bd_bufsize
) {
3298 * This packet will overflow the storage buffer.
3299 * Rotate the buffers if we can, then wakeup any
3302 * We cannot rotate buffers if a read is in progress
3303 * so drop the packet
3305 if (d
->bd_hbuf_read
!= 0) {
3310 if (d
->bd_fbuf
== NULL
) {
3311 if (d
->bd_headdrop
== 0) {
3313 * We haven't completed the previous read yet,
3314 * so drop the packet.
3320 * Drop the hold buffer as it contains older packets
3322 d
->bd_dcount
+= d
->bd_hcnt
;
3323 d
->bd_fbuf
= d
->bd_hbuf
;
3330 } else if (d
->bd_immediate
|| d
->bd_state
== BPF_TIMED_OUT
) {
3332 * Immediate mode is set, or the read timeout has
3333 * already expired during a select call. A packet
3334 * arrived, so the reader should be woken up.
3340 * Append the bpf header.
3343 if (d
->bd_flags
& BPF_EXTENDED_HDR
) {
3346 m
= (pkt
->bpfp_type
== BPF_PACKET_TYPE_MBUF
)
3347 ? pkt
->bpfp_mbuf
: NULL
;
3348 ehp
= (struct bpf_hdr_ext
*)(void *)(d
->bd_sbuf
+ curlen
);
3349 memset(ehp
, 0, sizeof(*ehp
));
3350 ehp
->bh_tstamp
.tv_sec
= tv
.tv_sec
;
3351 ehp
->bh_tstamp
.tv_usec
= tv
.tv_usec
;
3353 ehp
->bh_datalen
= pkt
->bpfp_total_length
;
3354 ehp
->bh_hdrlen
= hdrlen
;
3355 caplen
= ehp
->bh_caplen
= totlen
- hdrlen
;
3358 ehp
->bh_flags
|= BPF_HDR_EXT_FLAGS_DIR_OUT
;
3360 ehp
->bh_flags
|= BPF_HDR_EXT_FLAGS_DIR_IN
;
3362 } else if (outbound
) {
3363 ehp
->bh_flags
|= BPF_HDR_EXT_FLAGS_DIR_OUT
;
3365 /* only do lookups on non-raw INPCB */
3366 if ((m
->m_pkthdr
.pkt_flags
& (PKTF_FLOW_ID
|
3367 PKTF_FLOW_LOCALSRC
| PKTF_FLOW_RAWSOCK
)) ==
3368 (PKTF_FLOW_ID
| PKTF_FLOW_LOCALSRC
) &&
3369 m
->m_pkthdr
.pkt_flowsrc
== FLOWSRC_INPCB
) {
3370 ehp
->bh_flowid
= m
->m_pkthdr
.pkt_flowid
;
3371 ehp
->bh_proto
= m
->m_pkthdr
.pkt_proto
;
3373 ehp
->bh_svc
= so_svc2tc(m
->m_pkthdr
.pkt_svc
);
3374 if (m
->m_pkthdr
.pkt_flags
& PKTF_TCP_REXMT
) {
3375 ehp
->bh_pktflags
|= BPF_PKTFLAGS_TCP_REXMT
;
3377 if (m
->m_pkthdr
.pkt_flags
& PKTF_START_SEQ
) {
3378 ehp
->bh_pktflags
|= BPF_PKTFLAGS_START_SEQ
;
3380 if (m
->m_pkthdr
.pkt_flags
& PKTF_LAST_PKT
) {
3381 ehp
->bh_pktflags
|= BPF_PKTFLAGS_LAST_PKT
;
3383 if (m
->m_pkthdr
.pkt_flags
& PKTF_VALID_UNSENT_DATA
) {
3384 ehp
->bh_unsent_bytes
=
3385 m
->m_pkthdr
.bufstatus_if
;
3386 ehp
->bh_unsent_snd
=
3387 m
->m_pkthdr
.bufstatus_sndbuf
;
3390 ehp
->bh_flags
|= BPF_HDR_EXT_FLAGS_DIR_IN
;
3392 payload
= (u_char
*)ehp
+ hdrlen
;
3394 hp
= (struct bpf_hdr
*)(void *)(d
->bd_sbuf
+ curlen
);
3395 hp
->bh_tstamp
.tv_sec
= tv
.tv_sec
;
3396 hp
->bh_tstamp
.tv_usec
= tv
.tv_usec
;
3397 hp
->bh_datalen
= pkt
->bpfp_total_length
;
3398 hp
->bh_hdrlen
= hdrlen
;
3399 caplen
= hp
->bh_caplen
= totlen
- hdrlen
;
3400 payload
= (u_char
*)hp
+ hdrlen
;
3403 * Copy the packet data into the store buffer and update its length.
3405 copy_bpf_packet(pkt
, payload
, caplen
);
3406 d
->bd_slen
= curlen
+ totlen
;
3415 * Initialize all nonzero fields of a descriptor.
3418 bpf_allocbufs(struct bpf_d
*d
)
3420 if (d
->bd_sbuf
!= NULL
) {
3421 FREE(d
->bd_sbuf
, M_DEVBUF
);
3424 if (d
->bd_hbuf
!= NULL
) {
3425 FREE(d
->bd_hbuf
, M_DEVBUF
);
3428 if (d
->bd_fbuf
!= NULL
) {
3429 FREE(d
->bd_fbuf
, M_DEVBUF
);
3433 d
->bd_fbuf
= (caddr_t
) _MALLOC(d
->bd_bufsize
, M_DEVBUF
, M_WAIT
);
3434 if (d
->bd_fbuf
== NULL
) {
3438 d
->bd_sbuf
= (caddr_t
) _MALLOC(d
->bd_bufsize
, M_DEVBUF
, M_WAIT
);
3439 if (d
->bd_sbuf
== NULL
) {
3440 FREE(d
->bd_fbuf
, M_DEVBUF
);
3452 * Free buffers currently in use by a descriptor.
3456 bpf_freed(struct bpf_d
*d
)
3459 * We don't need to lock out interrupts since this descriptor has
3460 * been detached from its interface and it yet hasn't been marked
3463 if (d
->bd_hbuf_read
!= 0) {
3464 panic("bpf buffer freed during read");
3467 if (d
->bd_sbuf
!= 0) {
3468 FREE(d
->bd_sbuf
, M_DEVBUF
);
3469 if (d
->bd_hbuf
!= 0) {
3470 FREE(d
->bd_hbuf
, M_DEVBUF
);
3472 if (d
->bd_fbuf
!= 0) {
3473 FREE(d
->bd_fbuf
, M_DEVBUF
);
3477 FREE(d
->bd_filter
, M_DEVBUF
);
3482 * Attach an interface to bpf. driverp is a pointer to a (struct bpf_if *)
3483 * in the driver's softc; dlt is the link layer type; hdrlen is the fixed
3484 * size of the link header (variable length headers not yet supported).
3487 bpfattach(struct ifnet
*ifp
, u_int dlt
, u_int hdrlen
)
3489 bpf_attach(ifp
, dlt
, hdrlen
, NULL
, NULL
);
3501 struct bpf_if
*bp_new
;
3502 struct bpf_if
*bp_before_first
= NULL
;
3503 struct bpf_if
*bp_first
= NULL
;
3504 struct bpf_if
*bp_last
= NULL
;
3507 bp_new
= (struct bpf_if
*) _MALLOC(sizeof(*bp_new
), M_DEVBUF
,
3513 lck_mtx_lock(bpf_mlock
);
3516 * Check if this interface/dlt is already attached. Remember the
3517 * first and last attachment for this interface, as well as the
3518 * element before the first attachment.
3521 for (bp
= bpf_iflist
; bp
!= NULL
; bp
= bp
->bif_next
) {
3522 if (bp
->bif_ifp
!= ifp
) {
3523 if (bp_first
!= NULL
) {
3524 /* no more elements for this interface */
3527 bp_before_first
= bp
;
3529 if (bp
->bif_dlt
== dlt
) {
3533 if (bp_first
== NULL
) {
3540 lck_mtx_unlock(bpf_mlock
);
3541 printf("bpfattach - %s with dlt %d is already attached\n",
3543 FREE(bp_new
, M_DEVBUF
);
3547 bp_new
->bif_ifp
= ifp
;
3548 bp_new
->bif_dlt
= dlt
;
3549 bp_new
->bif_send
= send
;
3550 bp_new
->bif_tap
= tap
;
3552 if (bp_first
== NULL
) {
3553 /* No other entries for this ifp */
3554 bp_new
->bif_next
= bpf_iflist
;
3555 bpf_iflist
= bp_new
;
3557 if (ifnet_type(ifp
) == IFT_ETHER
&& dlt
== DLT_EN10MB
) {
3558 /* Make this the first entry for this interface */
3559 if (bp_before_first
!= NULL
) {
3560 /* point the previous to us */
3561 bp_before_first
->bif_next
= bp_new
;
3563 /* we're the new head */
3564 bpf_iflist
= bp_new
;
3566 bp_new
->bif_next
= bp_first
;
3568 /* Add this after the last entry for this interface */
3569 bp_new
->bif_next
= bp_last
->bif_next
;
3570 bp_last
->bif_next
= bp_new
;
3575 * Compute the length of the bpf header. This is not necessarily
3576 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
3577 * that the network layer header begins on a longword boundary (for
3578 * performance reasons and to alleviate alignment restrictions).
3580 bp_new
->bif_hdrlen
= BPF_WORDALIGN(hdrlen
+ SIZEOF_BPF_HDR
) - hdrlen
;
3581 bp_new
->bif_exthdrlen
= BPF_WORDALIGN(hdrlen
+
3582 sizeof(struct bpf_hdr_ext
)) - hdrlen
;
3584 /* Take a reference on the interface */
3585 ifnet_reference(ifp
);
3587 lck_mtx_unlock(bpf_mlock
);
3591 printf("bpf: %s attached\n", if_name(ifp
));
3599 * Detach bpf from an interface. This involves detaching each descriptor
3600 * associated with the interface, and leaving bd_bif NULL. Notify each
3601 * descriptor as it's detached so that any sleepers wake up and get
3605 bpfdetach(struct ifnet
*ifp
)
3607 struct bpf_if
*bp
, *bp_prev
, *bp_next
;
3610 if (bpf_debug
!= 0) {
3611 printf("%s: %s\n", __func__
, if_name(ifp
));
3614 lck_mtx_lock(bpf_mlock
);
3617 * Build the list of devices attached to that interface
3618 * that we need to free while keeping the lock to maintain
3619 * the integrity of the interface list
3622 for (bp
= bpf_iflist
; bp
!= NULL
; bp
= bp_next
) {
3623 bp_next
= bp
->bif_next
;
3625 if (ifp
!= bp
->bif_ifp
) {
3629 /* Unlink from the interface list */
3631 bp_prev
->bif_next
= bp
->bif_next
;
3633 bpf_iflist
= bp
->bif_next
;
3636 /* Detach the devices attached to the interface */
3637 while ((d
= bp
->bif_dlist
) != NULL
) {
3639 * Take an extra reference to prevent the device
3640 * from being freed when bpf_detachd() releases
3641 * the reference for the interface list
3651 lck_mtx_unlock(bpf_mlock
);
3655 bpf_init(__unused
void *unused
)
3661 if (bpf_devsw_installed
== 0) {
3662 bpf_devsw_installed
= 1;
3663 bpf_mlock_grp_attr
= lck_grp_attr_alloc_init();
3664 bpf_mlock_grp
= lck_grp_alloc_init("bpf", bpf_mlock_grp_attr
);
3665 bpf_mlock_attr
= lck_attr_alloc_init();
3666 lck_mtx_init(bpf_mlock
, bpf_mlock_grp
, bpf_mlock_attr
);
3667 maj
= cdevsw_add(CDEV_MAJOR
, &bpf_cdevsw
);
3669 if (bpf_mlock_attr
) {
3670 lck_attr_free(bpf_mlock_attr
);
3672 if (bpf_mlock_grp
) {
3673 lck_grp_free(bpf_mlock_grp
);
3675 if (bpf_mlock_grp_attr
) {
3676 lck_grp_attr_free(bpf_mlock_grp_attr
);
3680 bpf_mlock_attr
= NULL
;
3681 bpf_mlock_grp
= NULL
;
3682 bpf_mlock_grp_attr
= NULL
;
3683 bpf_devsw_installed
= 0;
3684 printf("bpf_init: failed to allocate a major number\n");
3688 for (i
= 0; i
< NBPFILTER
; i
++) {
3689 bpf_make_dev_t(maj
);
3693 cdevsw_add(&bpf_cdevsw
);
3698 SYSINIT(bpfdev
, SI_SUB_DRIVERS
, SI_ORDER_MIDDLE
+ CDEV_MAJOR
, bpf_drvinit
, NULL
);
3702 sysctl_bpf_maxbufsize SYSCTL_HANDLER_ARGS
3704 #pragma unused(arg1, arg2)
3709 err
= sysctl_handle_int(oidp
, &i
, 0, req
);
3710 if (err
!= 0 || req
->newptr
== USER_ADDR_NULL
) {
3714 if (i
< 0 || i
> BPF_MAXSIZE_CAP
) {
3715 i
= BPF_MAXSIZE_CAP
;