2 * Copyright (c) 2000-2018 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>
136 #include <security/mac_framework.h>
141 extern int tvtohz(struct timeval
*);
143 #define BPF_BUFSIZE 4096
144 #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, uio)
146 #define PRINET 26 /* interruptible */
148 #define ISAKMP_HDR_SIZE (sizeof(struct isakmp) + sizeof(struct isakmp_gen))
149 #define ESP_HDR_SIZE sizeof(struct newesp)
151 typedef void (*pktcopyfunc_t
)(const void *, void *, size_t);
154 * The default read buffer size is patchable.
156 static unsigned int bpf_bufsize
= BPF_BUFSIZE
;
157 SYSCTL_INT(_debug
, OID_AUTO
, bpf_bufsize
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
158 &bpf_bufsize
, 0, "");
159 __private_extern__
unsigned int bpf_maxbufsize
= BPF_MAXBUFSIZE
;
160 SYSCTL_INT(_debug
, OID_AUTO
, bpf_maxbufsize
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
161 &bpf_maxbufsize
, 0, "");
162 static unsigned int bpf_maxdevices
= 256;
163 SYSCTL_UINT(_debug
, OID_AUTO
, bpf_maxdevices
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
164 &bpf_maxdevices
, 0, "");
166 * bpf_wantpktap controls the defaul visibility of DLT_PKTAP
167 * For OS X is off by default so process need to use the ioctl BPF_WANT_PKTAP
168 * explicitly to be able to use DLT_PKTAP.
171 static unsigned int bpf_wantpktap
= 1;
173 static unsigned int bpf_wantpktap
= 0;
175 SYSCTL_UINT(_debug
, OID_AUTO
, bpf_wantpktap
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
176 &bpf_wantpktap
, 0, "");
178 static int bpf_debug
= 0;
179 SYSCTL_INT(_debug
, OID_AUTO
, bpf_debug
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
183 * bpf_iflist is the list of interfaces; each corresponds to an ifnet
184 * bpf_dtab holds pointer to the descriptors, indexed by minor device #
186 static struct bpf_if
*bpf_iflist
;
189 * BSD now stores the bpf_d in the dev_t which is a struct
190 * on their system. Our dev_t is an int, so we still store
191 * the bpf_d in a separate table indexed by minor device #.
193 * The value stored in bpf_dtab[n] represent three states:
194 * NULL: device not opened
195 * BPF_DEV_RESERVED: device opening or closing
196 * other: device <n> opened with pointer to storage
198 #define BPF_DEV_RESERVED ((struct bpf_d *)(uintptr_t)1)
199 static struct bpf_d
**bpf_dtab
= NULL
;
200 static unsigned int bpf_dtab_size
= 0;
201 static unsigned int nbpfilter
= 0;
203 decl_lck_mtx_data(static, bpf_mlock_data
);
204 static lck_mtx_t
*bpf_mlock
= &bpf_mlock_data
;
205 static lck_grp_t
*bpf_mlock_grp
;
206 static lck_grp_attr_t
*bpf_mlock_grp_attr
;
207 static lck_attr_t
*bpf_mlock_attr
;
209 #endif /* __APPLE__ */
211 static int bpf_allocbufs(struct bpf_d
*);
212 static errno_t
bpf_attachd(struct bpf_d
*d
, struct bpf_if
*bp
);
213 static int bpf_detachd(struct bpf_d
*d
, int);
214 static void bpf_freed(struct bpf_d
*);
215 static int bpf_movein(struct uio
*, int,
216 struct mbuf
**, struct sockaddr
*, int *);
217 static int bpf_setif(struct bpf_d
*, ifnet_t ifp
, bool, bool);
218 static void bpf_timed_out(void *, void *);
219 static void bpf_wakeup(struct bpf_d
*);
220 static u_int
get_pkt_trunc_len(u_char
*, u_int
);
221 static void catchpacket(struct bpf_d
*, struct bpf_packet
*, u_int
, int);
222 static void reset_d(struct bpf_d
*);
223 static int bpf_setf(struct bpf_d
*, u_int
, user_addr_t
, u_long
);
224 static int bpf_getdltlist(struct bpf_d
*, caddr_t
, struct proc
*);
225 static int bpf_setdlt(struct bpf_d
*, u_int
);
226 static int bpf_set_traffic_class(struct bpf_d
*, int);
227 static void bpf_set_packet_service_class(struct mbuf
*, int);
229 static void bpf_acquire_d(struct bpf_d
*);
230 static void bpf_release_d(struct bpf_d
*);
232 static int bpf_devsw_installed
;
234 void bpf_init(void *unused
);
235 static int bpf_tap_callback(struct ifnet
*ifp
, struct mbuf
*m
);
238 * Darwin differs from BSD here, the following are static
239 * on BSD and not static on Darwin.
245 ioctl_fcn_t bpfioctl
;
246 select_fcn_t bpfselect
;
248 /* Darwin's cdevsw struct differs slightly from BSDs */
249 #define CDEV_MAJOR 23
250 static struct cdevsw bpf_cdevsw
= {
252 /* close */ bpfclose
,
254 /* write */ bpfwrite
,
255 /* ioctl */ bpfioctl
,
257 /* reset */ eno_reset
,
259 /* select */ bpfselect
,
261 /* strategy */ eno_strat
,
267 #define SOCKADDR_HDR_LEN offsetof(struct sockaddr, sa_data)
270 bpf_movein(struct uio
*uio
, int linktype
, struct mbuf
**mp
,
271 struct sockaddr
*sockp
, int *datlen
)
288 sa_family
= AF_UNSPEC
;
289 /* XXX Would MAXLINKHDR be better? */
290 hlen
= sizeof(struct ether_header
);
295 #if defined(__FreeBSD__) || defined(__bsdi__)
296 sa_family
= AF_IMPLINK
;
299 sa_family
= AF_UNSPEC
;
300 /* XXX 4(FORMAC)+6(dst)+6(src)+3(LLC)+5(SNAP) */
308 sa_family
= AF_UNSPEC
;
313 case DLT_ATM_RFC1483
:
315 * en atm driver requires 4-byte atm pseudo header.
316 * though it isn't standard, vpi:vci needs to be
319 sa_family
= AF_UNSPEC
;
320 hlen
= 12; /* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
325 sa_family
= AF_UNSPEC
;
326 hlen
= 4; /* This should match PPP_HDRLEN */
329 case DLT_APPLE_IP_OVER_IEEE1394
:
330 sa_family
= AF_UNSPEC
;
331 hlen
= sizeof(struct firewire_header
);
334 case DLT_IEEE802_11
: /* IEEE 802.11 wireless */
335 sa_family
= AF_IEEE80211
;
339 case DLT_IEEE802_11_RADIO
:
340 sa_family
= AF_IEEE80211
;
348 // LP64todo - fix this!
349 len
= uio_resid(uio
);
350 *datlen
= len
- hlen
;
351 if ((unsigned)len
> MCLBYTES
) {
357 * Build a sockaddr based on the data link layer type.
358 * We do this at this level because the ethernet header
359 * is copied directly into the data field of the sockaddr.
360 * In the case of SLIP, there is no header and the packet
361 * is forwarded as is.
362 * Also, we are careful to leave room at the front of the mbuf
363 * for the link level header.
365 if ((hlen
+ SOCKADDR_HDR_LEN
) > sockp
->sa_len
) {
368 sockp
->sa_family
= sa_family
;
371 * We're directly sending the packet data supplied by
372 * the user; we don't need to make room for the link
373 * header, and don't need the header length value any
374 * more, so set it to 0.
379 MGETHDR(m
, M_WAIT
, MT_DATA
);
383 if ((unsigned)len
> MHLEN
) {
385 if ((m
->m_flags
& M_EXT
) == 0) {
390 m
->m_pkthdr
.len
= m
->m_len
= len
;
391 m
->m_pkthdr
.rcvif
= NULL
;
395 * Make room for link header.
398 m
->m_pkthdr
.len
-= hlen
;
400 m
->m_data
+= hlen
; /* XXX */
401 error
= UIOMOVE((caddr_t
)sockp
->sa_data
, hlen
, UIO_WRITE
, uio
);
406 error
= UIOMOVE(mtod(m
, caddr_t
), len
- hlen
, UIO_WRITE
, uio
);
411 /* Check for multicast destination */
414 struct ether_header
*eh
;
416 eh
= mtod(m
, struct ether_header
*);
417 if (ETHER_IS_MULTICAST(eh
->ether_dhost
)) {
418 if (_ether_cmp(etherbroadcastaddr
,
419 eh
->ether_dhost
) == 0) {
420 m
->m_flags
|= M_BCAST
;
422 m
->m_flags
|= M_MCAST
;
438 * The dynamic addition of a new device node must block all processes that
439 * are opening the last device so that no process will get an unexpected
443 bpf_make_dev_t(int maj
)
445 static int bpf_growing
= 0;
446 unsigned int cur_size
= nbpfilter
, i
;
448 if (nbpfilter
>= bpf_maxdevices
) {
452 while (bpf_growing
) {
453 /* Wait until new device has been created */
454 (void) tsleep((caddr_t
)&bpf_growing
, PZERO
, "bpf_growing", 0);
456 if (nbpfilter
> cur_size
) {
457 /* other thread grew it already */
462 /* need to grow bpf_dtab first */
463 if (nbpfilter
== bpf_dtab_size
) {
465 struct bpf_d
**new_dtab
= NULL
;
466 struct bpf_d
**old_dtab
= NULL
;
468 new_dtab_size
= bpf_dtab_size
+ NBPFILTER
;
469 new_dtab
= (struct bpf_d
**)_MALLOC(
470 sizeof(struct bpf_d
*) * new_dtab_size
, M_DEVBUF
, M_WAIT
);
472 printf("bpf_make_dev_t: malloc bpf_dtab failed\n");
476 bcopy(bpf_dtab
, new_dtab
,
477 sizeof(struct bpf_d
*) * bpf_dtab_size
);
479 bzero(new_dtab
+ bpf_dtab_size
,
480 sizeof(struct bpf_d
*) * NBPFILTER
);
483 bpf_dtab_size
= new_dtab_size
;
484 if (old_dtab
!= NULL
) {
485 _FREE(old_dtab
, M_DEVBUF
);
489 (void) devfs_make_node(makedev(maj
, i
),
490 DEVFS_CHAR
, UID_ROOT
, GID_WHEEL
, 0600,
494 wakeup((caddr_t
)&bpf_growing
);
500 * Attach file to the bpf interface, i.e. make d listen on bp.
503 bpf_attachd(struct bpf_d
*d
, struct bpf_if
*bp
)
505 int first
= bp
->bif_dlist
== NULL
;
509 * Point d at bp, and add d to the interface's list of listeners.
510 * Finally, point the driver's bpf cookie at the interface so
511 * it will divert packets to bpf.
514 d
->bd_next
= bp
->bif_dlist
;
518 * Take a reference on the device even if an error is returned
519 * because we keep the device in the interface's list of listeners
524 /* Find the default bpf entry for this ifp */
525 if (bp
->bif_ifp
->if_bpf
== NULL
) {
526 struct bpf_if
*tmp
, *primary
= NULL
;
528 for (tmp
= bpf_iflist
; tmp
; tmp
= tmp
->bif_next
) {
529 if (tmp
->bif_ifp
== bp
->bif_ifp
) {
534 bp
->bif_ifp
->if_bpf
= primary
;
536 /* Only call dlil_set_bpf_tap for primary dlt */
537 if (bp
->bif_ifp
->if_bpf
== bp
) {
538 dlil_set_bpf_tap(bp
->bif_ifp
, BPF_TAP_INPUT_OUTPUT
,
542 if (bp
->bif_tap
!= NULL
) {
543 error
= bp
->bif_tap(bp
->bif_ifp
, bp
->bif_dlt
,
544 BPF_TAP_INPUT_OUTPUT
);
549 * Reset the detach flags in case we previously detached an interface
551 d
->bd_flags
&= ~(BPF_DETACHING
| BPF_DETACHED
);
553 if (bp
->bif_dlt
== DLT_PKTAP
) {
554 d
->bd_flags
|= BPF_FINALIZE_PKTAP
;
556 d
->bd_flags
&= ~BPF_FINALIZE_PKTAP
;
562 * Detach a file from its interface.
564 * Return 1 if was closed by some thread, 0 otherwise
567 bpf_detachd(struct bpf_d
*d
, int closing
)
573 int bpf_closed
= d
->bd_flags
& BPF_CLOSING
;
575 * Some other thread already detached
577 if ((d
->bd_flags
& (BPF_DETACHED
| BPF_DETACHING
)) != 0) {
581 * This thread is doing the detach
583 d
->bd_flags
|= BPF_DETACHING
;
585 ifp
= d
->bd_bif
->bif_ifp
;
588 if (bpf_debug
!= 0) {
589 printf("%s: %llx %s%s\n",
590 __func__
, (uint64_t)VM_KERNEL_ADDRPERM(d
),
591 if_name(ifp
), closing
? " closing" : "");
594 /* Remove d from the interface's descriptor list. */
599 panic("bpf_detachd: descriptor not in list");
603 if (bp
->bif_dlist
== 0) {
605 * Let the driver know that there are no more listeners.
607 /* Only call dlil_set_bpf_tap for primary dlt */
608 if (bp
->bif_ifp
->if_bpf
== bp
) {
609 dlil_set_bpf_tap(ifp
, BPF_TAP_DISABLE
, NULL
);
612 bp
->bif_tap(ifp
, bp
->bif_dlt
, BPF_TAP_DISABLE
);
615 for (bp
= bpf_iflist
; bp
; bp
= bp
->bif_next
) {
616 if (bp
->bif_ifp
== ifp
&& bp
->bif_dlist
!= 0) {
626 * Check if this descriptor had requested promiscuous mode.
627 * If so, turn it off.
631 lck_mtx_unlock(bpf_mlock
);
632 if (ifnet_set_promiscuous(ifp
, 0)) {
634 * Something is really wrong if we were able to put
635 * the driver into promiscuous mode, but can't
637 * Most likely the network interface is gone.
639 printf("%s: ifnet_set_promiscuous failed\n", __func__
);
641 lck_mtx_lock(bpf_mlock
);
645 * Wake up other thread that are waiting for this thread to finish
648 d
->bd_flags
&= ~BPF_DETACHING
;
649 d
->bd_flags
|= BPF_DETACHED
;
651 /* Refresh the local variable as d could have been modified */
652 bpf_closed
= d
->bd_flags
& BPF_CLOSING
;
654 * Note that We've kept the reference because we may have dropped
655 * the lock when turning off promiscuous mode
661 * When closing makes sure no other thread refer to the bpf_d
663 if (bpf_debug
!= 0) {
664 printf("%s: %llx done\n",
665 __func__
, (uint64_t)VM_KERNEL_ADDRPERM(d
));
668 * Let the caller know the bpf_d is closed
678 * Start asynchronous timer, if necessary.
679 * Must be called with bpf_mlock held.
682 bpf_start_timer(struct bpf_d
*d
)
687 if (d
->bd_rtout
> 0 && d
->bd_state
== BPF_IDLE
) {
688 tv
.tv_sec
= d
->bd_rtout
/ hz
;
689 tv
.tv_usec
= (d
->bd_rtout
% hz
) * tick
;
691 clock_interval_to_deadline(
692 (uint64_t)tv
.tv_sec
* USEC_PER_SEC
+ tv
.tv_usec
,
693 NSEC_PER_USEC
, &deadline
);
695 * The state is BPF_IDLE, so the timer hasn't
696 * been started yet, and hasn't gone off yet;
697 * there is no thread call scheduled, so this
698 * won't change the schedule.
700 * XXX - what if, by the time it gets entered,
701 * the deadline has already passed?
703 thread_call_enter_delayed(d
->bd_thread_call
, deadline
);
704 d
->bd_state
= BPF_WAITING
;
709 * Cancel asynchronous timer.
710 * Must be called with bpf_mlock held.
713 bpf_stop_timer(struct bpf_d
*d
)
716 * If the timer has already gone off, this does nothing.
717 * Our caller is expected to set d->bd_state to BPF_IDLE,
718 * with the bpf_mlock, after we are called. bpf_timed_out()
719 * also grabs bpf_mlock, so, if the timer has gone off and
720 * bpf_timed_out() hasn't finished, it's waiting for the
721 * lock; when this thread releases the lock, it will
722 * find the state is BPF_IDLE, and just release the
725 return thread_call_cancel(d
->bd_thread_call
);
729 bpf_acquire_d(struct bpf_d
*d
)
731 void *lr_saved
= __builtin_return_address(0);
733 LCK_MTX_ASSERT(bpf_mlock
, LCK_MTX_ASSERT_OWNED
);
737 d
->bd_ref_lr
[d
->bd_next_ref_lr
] = lr_saved
;
738 d
->bd_next_ref_lr
= (d
->bd_next_ref_lr
+ 1) % BPF_REF_HIST
;
742 bpf_release_d(struct bpf_d
*d
)
744 void *lr_saved
= __builtin_return_address(0);
746 LCK_MTX_ASSERT(bpf_mlock
, LCK_MTX_ASSERT_OWNED
);
748 if (d
->bd_refcnt
<= 0) {
749 panic("%s: %p refcnt <= 0", __func__
, d
);
754 d
->bd_unref_lr
[d
->bd_next_unref_lr
] = lr_saved
;
755 d
->bd_next_unref_lr
= (d
->bd_next_unref_lr
+ 1) % BPF_REF_HIST
;
757 if (d
->bd_refcnt
== 0) {
758 /* Assert the device is detached */
759 if ((d
->bd_flags
& BPF_DETACHED
) == 0) {
760 panic("%s: %p BPF_DETACHED not set", __func__
, d
);
768 * Open ethernet device. Returns ENXIO for illegal minor device number,
769 * EBUSY if file is open by another process.
773 bpfopen(dev_t dev
, int flags
, __unused
int fmt
,
778 lck_mtx_lock(bpf_mlock
);
779 if ((unsigned int) minor(dev
) >= nbpfilter
) {
780 lck_mtx_unlock(bpf_mlock
);
784 * New device nodes are created on demand when opening the last one.
785 * The programming model is for processes to loop on the minor starting
786 * at 0 as long as EBUSY is returned. The loop stops when either the
787 * open succeeds or an error other that EBUSY is returned. That means
788 * that bpf_make_dev_t() must block all processes that are opening the
789 * last node. If not all processes are blocked, they could unexpectedly
790 * get ENOENT and abort their opening loop.
792 if ((unsigned int) minor(dev
) == (nbpfilter
- 1)) {
793 bpf_make_dev_t(major(dev
));
797 * Each minor can be opened by only one process. If the requested
798 * minor is in use, return EBUSY.
800 * Important: bpfopen() and bpfclose() have to check and set the status
801 * of a device in the same lockin context otherwise the device may be
802 * leaked because the vnode use count will be unpextectly greater than 1
803 * when close() is called.
805 if (bpf_dtab
[minor(dev
)] == NULL
) {
806 /* Reserve while opening */
807 bpf_dtab
[minor(dev
)] = BPF_DEV_RESERVED
;
809 lck_mtx_unlock(bpf_mlock
);
812 d
= (struct bpf_d
*)_MALLOC(sizeof(struct bpf_d
), M_DEVBUF
,
815 /* this really is a catastrophic failure */
816 printf("bpfopen: malloc bpf_d failed\n");
817 bpf_dtab
[minor(dev
)] = NULL
;
818 lck_mtx_unlock(bpf_mlock
);
822 /* Mark "in use" and do most initialization. */
824 d
->bd_bufsize
= bpf_bufsize
;
827 d
->bd_oflags
= flags
;
828 d
->bd_state
= BPF_IDLE
;
829 d
->bd_traffic_class
= SO_TC_BE
;
830 d
->bd_flags
|= BPF_DETACHED
;
832 d
->bd_flags
|= BPF_WANT_PKTAP
;
834 d
->bd_flags
&= ~BPF_WANT_PKTAP
;
836 d
->bd_thread_call
= thread_call_allocate(bpf_timed_out
, d
);
837 if (d
->bd_thread_call
== NULL
) {
838 printf("bpfopen: malloc thread call failed\n");
839 bpf_dtab
[minor(dev
)] = NULL
;
841 lck_mtx_unlock(bpf_mlock
);
846 uuid_generate(d
->bd_uuid
);
849 mac_bpfdesc_label_init(d
);
850 mac_bpfdesc_label_associate(kauth_cred_get(), d
);
852 bpf_dtab
[minor(dev
)] = d
; /* Mark opened */
853 lck_mtx_unlock(bpf_mlock
);
859 * Close the descriptor by detaching it from its interface,
860 * deallocating its buffers, and marking it free.
864 bpfclose(dev_t dev
, __unused
int flags
, __unused
int fmt
,
865 __unused
struct proc
*p
)
869 /* Take BPF lock to ensure no other thread is using the device */
870 lck_mtx_lock(bpf_mlock
);
872 d
= bpf_dtab
[minor(dev
)];
873 if (d
== NULL
|| d
== BPF_DEV_RESERVED
) {
874 lck_mtx_unlock(bpf_mlock
);
879 * Other threads may call bpd_detachd() if we drop the bpf_mlock
881 d
->bd_flags
|= BPF_CLOSING
;
883 if (bpf_debug
!= 0) {
885 __func__
, (uint64_t)VM_KERNEL_ADDRPERM(d
));
888 bpf_dtab
[minor(dev
)] = BPF_DEV_RESERVED
; /* Reserve while closing */
891 * Deal with any in-progress timeouts.
893 switch (d
->bd_state
) {
896 * Not waiting for a timeout, and no timeout happened.
902 * Waiting for a timeout.
903 * Cancel any timer that has yet to go off,
904 * and mark the state as "closing".
905 * Then drop the lock to allow any timers that
906 * *have* gone off to run to completion, and wait
907 * for them to finish.
909 if (!bpf_stop_timer(d
)) {
911 * There was no pending call, so the call must
912 * have been in progress. Wait for the call to
913 * complete; we have to drop the lock while
914 * waiting. to let the in-progrss call complete
916 d
->bd_state
= BPF_DRAINING
;
917 while (d
->bd_state
== BPF_DRAINING
) {
918 msleep((caddr_t
)d
, bpf_mlock
, PRINET
,
919 "bpfdraining", NULL
);
922 d
->bd_state
= BPF_IDLE
;
927 * Timer went off, and the timeout routine finished.
929 d
->bd_state
= BPF_IDLE
;
934 * Another thread is blocked on a close waiting for
935 * a timeout to finish.
936 * This "shouldn't happen", as the first thread to enter
937 * bpfclose() will set bpf_dtab[minor(dev)] to 1, and
938 * all subsequent threads should see that and fail with
941 panic("Two threads blocked in a BPF close");
948 selthreadclear(&d
->bd_sel
);
950 mac_bpfdesc_label_destroy(d
);
952 thread_call_free(d
->bd_thread_call
);
954 while (d
->bd_hbuf_read
!= 0) {
955 msleep((caddr_t
)d
, bpf_mlock
, PRINET
, "bpf_reading", NULL
);
960 /* Mark free in same context as bpfopen comes to check */
961 bpf_dtab
[minor(dev
)] = NULL
; /* Mark closed */
965 lck_mtx_unlock(bpf_mlock
);
970 #define BPF_SLEEP bpf_sleep
973 bpf_sleep(struct bpf_d
*d
, int pri
, const char *wmesg
, int timo
)
975 u_int64_t abstime
= 0;
978 clock_interval_to_deadline(timo
, NSEC_PER_SEC
/ hz
, &abstime
);
981 return msleep1((caddr_t
)d
, bpf_mlock
, pri
, wmesg
, abstime
);
985 bpf_finalize_pktap(struct bpf_hdr
*hp
, struct pktap_header
*pktaphdr
)
987 if (pktaphdr
->pth_flags
& PTH_FLAG_V2_HDR
) {
988 struct pktap_v2_hdr
*pktap_v2_hdr
;
990 pktap_v2_hdr
= (struct pktap_v2_hdr
*)pktaphdr
;
992 if (pktap_v2_hdr
->pth_flags
& PTH_FLAG_DELAY_PKTAP
) {
993 pktap_v2_finalize_proc_info(pktap_v2_hdr
);
996 if (pktaphdr
->pth_flags
& PTH_FLAG_DELAY_PKTAP
) {
997 pktap_finalize_proc_info(pktaphdr
);
1000 if (pktaphdr
->pth_flags
& PTH_FLAG_TSTAMP
) {
1001 hp
->bh_tstamp
.tv_sec
= pktaphdr
->pth_tstamp
.tv_sec
;
1002 hp
->bh_tstamp
.tv_usec
= pktaphdr
->pth_tstamp
.tv_usec
;
1008 * Rotate the packet buffers in descriptor d. Move the store buffer
1009 * into the hold slot, and the free buffer into the store slot.
1010 * Zero the length of the new store buffer.
1012 #define ROTATE_BUFFERS(d) \
1013 if (d->bd_hbuf_read != 0) \
1014 panic("rotating bpf buffers during read"); \
1015 (d)->bd_hbuf = (d)->bd_sbuf; \
1016 (d)->bd_hlen = (d)->bd_slen; \
1017 (d)->bd_hcnt = (d)->bd_scnt; \
1018 (d)->bd_sbuf = (d)->bd_fbuf; \
1021 (d)->bd_fbuf = NULL;
1023 * bpfread - read next chunk of packets from buffers
1026 bpfread(dev_t dev
, struct uio
*uio
, int ioflag
)
1030 int timed_out
, hbuf_len
;
1034 lck_mtx_lock(bpf_mlock
);
1036 d
= bpf_dtab
[minor(dev
)];
1037 if (d
== NULL
|| d
== BPF_DEV_RESERVED
||
1038 (d
->bd_flags
& BPF_CLOSING
) != 0) {
1039 lck_mtx_unlock(bpf_mlock
);
1046 * Restrict application to use a buffer the same size as
1047 * as kernel buffers.
1049 if (uio_resid(uio
) != d
->bd_bufsize
) {
1051 lck_mtx_unlock(bpf_mlock
);
1055 if (d
->bd_state
== BPF_WAITING
) {
1059 timed_out
= (d
->bd_state
== BPF_TIMED_OUT
);
1060 d
->bd_state
= BPF_IDLE
;
1062 while (d
->bd_hbuf_read
!= 0) {
1063 msleep((caddr_t
)d
, bpf_mlock
, PRINET
, "bpf_reading", NULL
);
1066 if ((d
->bd_flags
& BPF_CLOSING
) != 0) {
1068 lck_mtx_unlock(bpf_mlock
);
1072 * If the hold buffer is empty, then do a timed sleep, which
1073 * ends when the timeout expires or when enough packets
1074 * have arrived to fill the store buffer.
1076 while (d
->bd_hbuf
== 0) {
1077 if ((d
->bd_immediate
|| timed_out
|| (ioflag
& IO_NDELAY
)) &&
1080 * We're in immediate mode, or are reading
1081 * in non-blocking mode, or a timer was
1082 * started before the read (e.g., by select()
1083 * or poll()) and has expired and a packet(s)
1084 * either arrived since the previous
1085 * read or arrived while we were asleep.
1086 * Rotate the buffers and return what's here.
1093 * No data is available, check to see if the bpf device
1094 * is still pointed at a real interface. If not, return
1095 * ENXIO so that the userland process knows to rebind
1096 * it before using it again.
1098 if (d
->bd_bif
== NULL
) {
1100 lck_mtx_unlock(bpf_mlock
);
1103 if (ioflag
& IO_NDELAY
) {
1105 lck_mtx_unlock(bpf_mlock
);
1108 error
= BPF_SLEEP(d
, PRINET
| PCATCH
, "bpf", d
->bd_rtout
);
1110 * Make sure device is still opened
1112 if ((d
->bd_flags
& BPF_CLOSING
) != 0) {
1114 lck_mtx_unlock(bpf_mlock
);
1118 while (d
->bd_hbuf_read
!= 0) {
1119 msleep((caddr_t
)d
, bpf_mlock
, PRINET
, "bpf_reading",
1123 if ((d
->bd_flags
& BPF_CLOSING
) != 0) {
1125 lck_mtx_unlock(bpf_mlock
);
1129 if (error
== EINTR
|| error
== ERESTART
) {
1130 if (d
->bd_hbuf
!= NULL
) {
1132 * Because we msleep, the hold buffer might
1133 * be filled when we wake up. Avoid rotating
1138 if (d
->bd_slen
!= 0) {
1140 * Sometimes we may be interrupted often and
1141 * the sleep above will not timeout.
1142 * Regardless, we should rotate the buffers
1143 * if there's any new data pending and
1150 lck_mtx_unlock(bpf_mlock
);
1151 if (error
== ERESTART
) {
1152 printf("%s: %llx ERESTART to EINTR\n",
1153 __func__
, (uint64_t)VM_KERNEL_ADDRPERM(d
));
1158 if (error
== EWOULDBLOCK
) {
1160 * On a timeout, return what's in the buffer,
1161 * which may be nothing. If there is something
1162 * in the store buffer, we can rotate the buffers.
1166 * We filled up the buffer in between
1167 * getting the timeout and arriving
1168 * here, so we don't need to rotate.
1173 if (d
->bd_slen
== 0) {
1175 lck_mtx_unlock(bpf_mlock
);
1183 * At this point, we know we have something in the hold slot.
1187 * Set the hold buffer read. So we do not
1188 * rotate the buffers until the hold buffer
1189 * read is complete. Also to avoid issues resulting
1190 * from page faults during disk sleep (<rdar://problem/13436396>).
1192 d
->bd_hbuf_read
= 1;
1194 hbuf_len
= d
->bd_hlen
;
1195 flags
= d
->bd_flags
;
1196 lck_mtx_unlock(bpf_mlock
);
1200 * Before we move data to userland, we fill out the extended
1203 if (flags
& BPF_EXTENDED_HDR
) {
1207 while (p
< hbuf
+ hbuf_len
) {
1208 struct bpf_hdr_ext
*ehp
;
1210 struct so_procinfo soprocinfo
;
1213 ehp
= (struct bpf_hdr_ext
*)(void *)p
;
1214 if ((flowid
= ehp
->bh_flowid
) != 0) {
1215 if (ehp
->bh_proto
== IPPROTO_TCP
) {
1216 found
= inp_findinpcb_procinfo(&tcbinfo
,
1217 flowid
, &soprocinfo
);
1218 } else if (ehp
->bh_proto
== IPPROTO_UDP
) {
1219 found
= inp_findinpcb_procinfo(&udbinfo
,
1220 flowid
, &soprocinfo
);
1223 ehp
->bh_pid
= soprocinfo
.spi_pid
;
1224 proc_name(ehp
->bh_pid
, ehp
->bh_comm
,
1230 if (flags
& BPF_FINALIZE_PKTAP
) {
1231 struct pktap_header
*pktaphdr
;
1233 pktaphdr
= (struct pktap_header
*)(void *)
1234 (p
+ BPF_WORDALIGN(ehp
->bh_hdrlen
));
1236 bpf_finalize_pktap((struct bpf_hdr
*) ehp
,
1239 p
+= BPF_WORDALIGN(ehp
->bh_hdrlen
+ ehp
->bh_caplen
);
1241 } else if (flags
& BPF_FINALIZE_PKTAP
) {
1245 while (p
< hbuf
+ hbuf_len
) {
1247 struct pktap_header
*pktaphdr
;
1249 hp
= (struct bpf_hdr
*)(void *)p
;
1250 pktaphdr
= (struct pktap_header
*)(void *)
1251 (p
+ BPF_WORDALIGN(hp
->bh_hdrlen
));
1253 bpf_finalize_pktap(hp
, pktaphdr
);
1255 p
+= BPF_WORDALIGN(hp
->bh_hdrlen
+ hp
->bh_caplen
);
1261 * Move data from hold buffer into user space.
1262 * We know the entire buffer is transferred since
1263 * we checked above that the read buffer is bpf_bufsize bytes.
1265 error
= UIOMOVE(hbuf
, hbuf_len
, UIO_READ
, uio
);
1267 lck_mtx_lock(bpf_mlock
);
1269 * Make sure device is still opened
1271 if ((d
->bd_flags
& BPF_CLOSING
) != 0) {
1273 lck_mtx_unlock(bpf_mlock
);
1277 d
->bd_hbuf_read
= 0;
1278 d
->bd_fbuf
= d
->bd_hbuf
;
1285 lck_mtx_unlock(bpf_mlock
);
1290 * If there are processes sleeping on this descriptor, wake them up.
1293 bpf_wakeup(struct bpf_d
*d
)
1295 if (d
->bd_state
== BPF_WAITING
) {
1297 d
->bd_state
= BPF_IDLE
;
1300 if (d
->bd_async
&& d
->bd_sig
&& d
->bd_sigio
) {
1301 pgsigio(d
->bd_sigio
, d
->bd_sig
);
1304 selwakeup(&d
->bd_sel
);
1305 if ((d
->bd_flags
& BPF_KNOTE
)) {
1306 KNOTE(&d
->bd_sel
.si_note
, 1);
1311 bpf_timed_out(void *arg
, __unused
void *dummy
)
1313 struct bpf_d
*d
= (struct bpf_d
*)arg
;
1315 lck_mtx_lock(bpf_mlock
);
1316 if (d
->bd_state
== BPF_WAITING
) {
1318 * There's a select or kqueue waiting for this; if there's
1319 * now stuff to read, wake it up.
1321 d
->bd_state
= BPF_TIMED_OUT
;
1322 if (d
->bd_slen
!= 0) {
1325 } else if (d
->bd_state
== BPF_DRAINING
) {
1327 * A close is waiting for this to finish.
1328 * Mark it as finished, and wake the close up.
1330 d
->bd_state
= BPF_IDLE
;
1333 lck_mtx_unlock(bpf_mlock
);
1336 /* keep in sync with bpf_movein above: */
1337 #define MAX_DATALINK_HDR_LEN (sizeof(struct firewire_header))
1340 bpfwrite(dev_t dev
, struct uio
*uio
, __unused
int ioflag
)
1344 struct mbuf
*m
= NULL
;
1346 char dst_buf
[SOCKADDR_HDR_LEN
+ MAX_DATALINK_HDR_LEN
];
1351 lck_mtx_lock(bpf_mlock
);
1353 d
= bpf_dtab
[minor(dev
)];
1354 if (d
== NULL
|| d
== BPF_DEV_RESERVED
||
1355 (d
->bd_flags
& BPF_CLOSING
) != 0) {
1356 lck_mtx_unlock(bpf_mlock
);
1362 if (d
->bd_bif
== 0) {
1364 lck_mtx_unlock(bpf_mlock
);
1368 ifp
= d
->bd_bif
->bif_ifp
;
1370 if ((ifp
->if_flags
& IFF_UP
) == 0) {
1372 lck_mtx_unlock(bpf_mlock
);
1375 if (uio_resid(uio
) == 0) {
1377 lck_mtx_unlock(bpf_mlock
);
1380 ((struct sockaddr
*)dst_buf
)->sa_len
= sizeof(dst_buf
);
1383 * fix for PR-6849527
1384 * geting variables onto stack before dropping lock for bpf_movein()
1386 bif_dlt
= (int)d
->bd_bif
->bif_dlt
;
1387 bd_hdrcmplt
= d
->bd_hdrcmplt
;
1389 /* bpf_movein allocating mbufs; drop lock */
1390 lck_mtx_unlock(bpf_mlock
);
1392 error
= bpf_movein(uio
, bif_dlt
, &m
,
1393 bd_hdrcmplt
? NULL
: (struct sockaddr
*)dst_buf
,
1396 /* take the lock again */
1397 lck_mtx_lock(bpf_mlock
);
1400 lck_mtx_unlock(bpf_mlock
);
1404 /* verify the device is still open */
1405 if ((d
->bd_flags
& BPF_CLOSING
) != 0) {
1407 lck_mtx_unlock(bpf_mlock
);
1412 if (d
->bd_bif
== NULL
) {
1414 lck_mtx_unlock(bpf_mlock
);
1419 if ((unsigned)datlen
> ifp
->if_mtu
) {
1421 lck_mtx_unlock(bpf_mlock
);
1427 mac_mbuf_label_associate_bpfdesc(d
, m
);
1430 bpf_set_packet_service_class(m
, d
->bd_traffic_class
);
1432 lck_mtx_unlock(bpf_mlock
);
1435 * The driver frees the mbuf.
1437 if (d
->bd_hdrcmplt
) {
1438 if (d
->bd_bif
->bif_send
) {
1439 error
= d
->bd_bif
->bif_send(ifp
, d
->bd_bif
->bif_dlt
, m
);
1441 error
= dlil_output(ifp
, 0, m
, NULL
, NULL
, 1, NULL
);
1444 error
= dlil_output(ifp
, PF_INET
, m
, NULL
,
1445 (struct sockaddr
*)dst_buf
, 0, NULL
);
1448 lck_mtx_lock(bpf_mlock
);
1450 lck_mtx_unlock(bpf_mlock
);
1456 * Reset a descriptor by flushing its packet buffer and clearing the
1457 * receive and drop counts.
1460 reset_d(struct bpf_d
*d
)
1462 if (d
->bd_hbuf_read
!= 0) {
1463 panic("resetting buffers during read");
1467 /* Free the hold buffer. */
1468 d
->bd_fbuf
= d
->bd_hbuf
;
1479 static struct bpf_d
*
1480 bpf_get_device_from_uuid(uuid_t uuid
)
1484 for (i
= 0; i
< nbpfilter
; i
++) {
1485 struct bpf_d
*d
= bpf_dtab
[i
];
1487 if (d
== NULL
|| d
== BPF_DEV_RESERVED
||
1488 (d
->bd_flags
& BPF_CLOSING
) != 0) {
1491 if (uuid_compare(uuid
, d
->bd_uuid
) == 0) {
1500 * The BIOCSETUP command "atomically" attach to the interface and
1501 * copy the buffer from another interface. This minimizes the risk
1502 * of missing packet because this is done while holding
1503 * the BPF global lock
1506 bpf_setup(struct bpf_d
*d_to
, uuid_t uuid_from
, ifnet_t ifp
)
1508 struct bpf_d
*d_from
;
1511 LCK_MTX_ASSERT(bpf_mlock
, LCK_MTX_ASSERT_OWNED
);
1516 d_from
= bpf_get_device_from_uuid(uuid_from
);
1517 if (d_from
== NULL
) {
1519 os_log_info(OS_LOG_DEFAULT
,
1520 "%s: uuids not found error %d",
1524 if (d_from
->bd_opened_by
!= d_to
->bd_opened_by
) {
1526 os_log_info(OS_LOG_DEFAULT
,
1527 "%s: processes not matching error %d",
1533 * Prevent any read while copying
1535 while (d_to
->bd_hbuf_read
!= 0) {
1536 msleep((caddr_t
)d_to
, bpf_mlock
, PRINET
, __func__
, NULL
);
1538 d_to
->bd_hbuf_read
= 1;
1540 while (d_from
->bd_hbuf_read
!= 0) {
1541 msleep((caddr_t
)d_from
, bpf_mlock
, PRINET
, __func__
, NULL
);
1543 d_from
->bd_hbuf_read
= 1;
1546 * Verify the devices have not been closed
1548 if (d_to
->bd_flags
& BPF_CLOSING
) {
1550 os_log_info(OS_LOG_DEFAULT
,
1551 "%s: d_to is closing error %d",
1555 if (d_from
->bd_flags
& BPF_CLOSING
) {
1557 os_log_info(OS_LOG_DEFAULT
,
1558 "%s: d_from is closing error %d",
1564 * For now require the same buffer size
1566 if (d_from
->bd_bufsize
!= d_to
->bd_bufsize
) {
1568 os_log_info(OS_LOG_DEFAULT
,
1569 "%s: bufsizes not matching error %d",
1575 * Attach to the interface
1577 error
= bpf_setif(d_to
, ifp
, false, true);
1579 os_log_info(OS_LOG_DEFAULT
,
1580 "%s: bpf_setif() failed error %d",
1586 * Make sure the buffers are setup as expected by bpf_setif()
1588 ASSERT(d_to
->bd_hbuf
== NULL
);
1589 ASSERT(d_to
->bd_sbuf
!= NULL
);
1590 ASSERT(d_to
->bd_fbuf
!= NULL
);
1593 * Copy the buffers and update the pointers and counts
1595 memcpy(d_to
->bd_sbuf
, d_from
->bd_sbuf
, d_from
->bd_slen
);
1596 d_to
->bd_slen
= d_from
->bd_slen
;
1597 d_to
->bd_scnt
= d_from
->bd_scnt
;
1599 if (d_from
->bd_hbuf
!= NULL
) {
1600 d_to
->bd_hbuf
= d_to
->bd_fbuf
;
1601 d_to
->bd_fbuf
= NULL
;
1602 memcpy(d_to
->bd_hbuf
, d_from
->bd_hbuf
, d_from
->bd_hlen
);
1604 d_to
->bd_hlen
= d_from
->bd_hlen
;
1605 d_to
->bd_hcnt
= d_from
->bd_hcnt
;
1607 if (bpf_debug
> 0) {
1608 os_log_info(OS_LOG_DEFAULT
,
1609 "%s: done slen %u scnt %u hlen %u hcnt %u",
1610 __func__
, d_to
->bd_slen
, d_to
->bd_scnt
,
1611 d_to
->bd_hlen
, d_to
->bd_hcnt
);
1614 d_from
->bd_hbuf_read
= 0;
1615 wakeup((caddr_t
)d_from
);
1617 d_to
->bd_hbuf_read
= 0;
1618 wakeup((caddr_t
)d_to
);
1624 * FIONREAD Check for read packet available.
1625 * SIOCGIFADDR Get interface address - convenient hook to driver.
1626 * BIOCGBLEN Get buffer len [for read()].
1627 * BIOCSETF Set ethernet read filter.
1628 * BIOCFLUSH Flush read packet buffer.
1629 * BIOCPROMISC Put interface into promiscuous mode.
1630 * BIOCGDLT Get link layer type.
1631 * BIOCGETIF Get interface name.
1632 * BIOCSETIF Set interface.
1633 * BIOCSRTIMEOUT Set read timeout.
1634 * BIOCGRTIMEOUT Get read timeout.
1635 * BIOCGSTATS Get packet stats.
1636 * BIOCIMMEDIATE Set immediate mode.
1637 * BIOCVERSION Get filter language version.
1638 * BIOCGHDRCMPLT Get "header already complete" flag
1639 * BIOCSHDRCMPLT Set "header already complete" flag
1640 * BIOCGSEESENT Get "see packets sent" flag
1641 * BIOCSSEESENT Set "see packets sent" flag
1642 * BIOCSETTC Set traffic class.
1643 * BIOCGETTC Get traffic class.
1644 * BIOCSEXTHDR Set "extended header" flag
1645 * BIOCSHEADDROP Drop head of the buffer if user is not reading
1646 * BIOCGHEADDROP Get "head-drop" flag
1650 bpfioctl(dev_t dev
, u_long cmd
, caddr_t addr
, __unused
int flags
,
1658 lck_mtx_lock(bpf_mlock
);
1660 d
= bpf_dtab
[minor(dev
)];
1661 if (d
== NULL
|| d
== BPF_DEV_RESERVED
||
1662 (d
->bd_flags
& BPF_CLOSING
) != 0) {
1663 lck_mtx_unlock(bpf_mlock
);
1669 if (d
->bd_state
== BPF_WAITING
) {
1672 d
->bd_state
= BPF_IDLE
;
1680 * Check for read packet available.
1682 case FIONREAD
: /* int */
1687 if (d
->bd_hbuf
&& d
->bd_hbuf_read
== 0) {
1691 bcopy(&n
, addr
, sizeof(n
));
1695 case SIOCGIFADDR
: /* struct ifreq */
1699 if (d
->bd_bif
== 0) {
1702 ifp
= d
->bd_bif
->bif_ifp
;
1703 error
= ifnet_ioctl(ifp
, 0, cmd
, addr
);
1709 * Get buffer len [for read()].
1711 case BIOCGBLEN
: /* u_int */
1712 bcopy(&d
->bd_bufsize
, addr
, sizeof(u_int
));
1716 * Set buffer length.
1718 case BIOCSBLEN
: { /* u_int */
1720 unsigned int maxbufsize
= bpf_maxbufsize
;
1723 * Allow larger buffer in head drop mode to with the
1724 * assumption the reading process may be low priority but
1725 * is interested in the most recent traffic
1727 if (d
->bd_headdrop
!= 0) {
1728 maxbufsize
= 2 * bpf_maxbufsize
;
1731 if (d
->bd_bif
!= 0 || (d
->bd_flags
& BPF_DETACHING
)) {
1733 * Interface already attached, unable to change buffers
1738 bcopy(addr
, &size
, sizeof(size
));
1740 if (size
> maxbufsize
) {
1741 d
->bd_bufsize
= maxbufsize
;
1743 os_log_info(OS_LOG_DEFAULT
,
1744 "%s bufsize capped to %u from %u",
1745 __func__
, d
->bd_bufsize
, size
);
1746 } else if (size
< BPF_MINBUFSIZE
) {
1747 d
->bd_bufsize
= BPF_MINBUFSIZE
;
1749 os_log_info(OS_LOG_DEFAULT
,
1750 "%s bufsize bumped to %u from %u",
1751 __func__
, d
->bd_bufsize
, size
);
1753 d
->bd_bufsize
= size
;
1756 /* It's a read/write ioctl */
1757 bcopy(&d
->bd_bufsize
, addr
, sizeof(u_int
));
1761 * Set link layer read filter.
1764 case BIOCSETFNR32
: { /* struct bpf_program32 */
1765 struct bpf_program32 prg32
;
1767 bcopy(addr
, &prg32
, sizeof(prg32
));
1768 error
= bpf_setf(d
, prg32
.bf_len
,
1769 CAST_USER_ADDR_T(prg32
.bf_insns
), cmd
);
1774 case BIOCSETFNR64
: { /* struct bpf_program64 */
1775 struct bpf_program64 prg64
;
1777 bcopy(addr
, &prg64
, sizeof(prg64
));
1778 error
= bpf_setf(d
, prg64
.bf_len
, prg64
.bf_insns
, cmd
);
1783 * Flush read packet buffer.
1786 while (d
->bd_hbuf_read
!= 0) {
1787 msleep((caddr_t
)d
, bpf_mlock
, PRINET
, "bpf_reading",
1790 if ((d
->bd_flags
& BPF_CLOSING
) != 0) {
1798 * Put interface into promiscuous mode.
1801 if (d
->bd_bif
== 0) {
1803 * No interface attached yet.
1808 if (d
->bd_promisc
== 0) {
1809 lck_mtx_unlock(bpf_mlock
);
1810 error
= ifnet_set_promiscuous(d
->bd_bif
->bif_ifp
, 1);
1811 lck_mtx_lock(bpf_mlock
);
1819 * Get device parameters.
1821 case BIOCGDLT
: /* u_int */
1822 if (d
->bd_bif
== 0) {
1825 bcopy(&d
->bd_bif
->bif_dlt
, addr
, sizeof(u_int
));
1830 * Get a list of supported data link types.
1832 case BIOCGDLTLIST
: /* struct bpf_dltlist */
1833 if (d
->bd_bif
== NULL
) {
1836 error
= bpf_getdltlist(d
, addr
, p
);
1841 * Set data link type.
1843 case BIOCSDLT
: /* u_int */
1844 if (d
->bd_bif
== NULL
) {
1849 bcopy(addr
, &dlt
, sizeof(dlt
));
1851 if (dlt
== DLT_PKTAP
&&
1852 !(d
->bd_flags
& BPF_WANT_PKTAP
)) {
1855 error
= bpf_setdlt(d
, dlt
);
1860 * Get interface name.
1862 case BIOCGETIF
: /* struct ifreq */
1863 if (d
->bd_bif
== 0) {
1866 struct ifnet
*const ifp
= d
->bd_bif
->bif_ifp
;
1868 snprintf(((struct ifreq
*)(void *)addr
)->ifr_name
,
1869 sizeof(ifr
.ifr_name
), "%s", if_name(ifp
));
1876 case BIOCSETIF
: { /* struct ifreq */
1879 bcopy(addr
, &ifr
, sizeof(ifr
));
1880 ifr
.ifr_name
[IFNAMSIZ
- 1] = '\0';
1881 ifp
= ifunit(ifr
.ifr_name
);
1885 error
= bpf_setif(d
, ifp
, true, false);
1893 case BIOCSRTIMEOUT32
: { /* struct user32_timeval */
1894 struct user32_timeval _tv
;
1897 bcopy(addr
, &_tv
, sizeof(_tv
));
1898 tv
.tv_sec
= _tv
.tv_sec
;
1899 tv
.tv_usec
= _tv
.tv_usec
;
1902 * Subtract 1 tick from tvtohz() since this isn't
1905 if ((error
= itimerfix(&tv
)) == 0) {
1906 d
->bd_rtout
= tvtohz(&tv
) - 1;
1911 case BIOCSRTIMEOUT64
: { /* struct user64_timeval */
1912 struct user64_timeval _tv
;
1915 bcopy(addr
, &_tv
, sizeof(_tv
));
1916 tv
.tv_sec
= _tv
.tv_sec
;
1917 tv
.tv_usec
= _tv
.tv_usec
;
1920 * Subtract 1 tick from tvtohz() since this isn't
1923 if ((error
= itimerfix(&tv
)) == 0) {
1924 d
->bd_rtout
= tvtohz(&tv
) - 1;
1932 case BIOCGRTIMEOUT32
: { /* struct user32_timeval */
1933 struct user32_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
));
1942 case BIOCGRTIMEOUT64
: { /* struct user64_timeval */
1943 struct user64_timeval tv
;
1945 bzero(&tv
, sizeof(tv
));
1946 tv
.tv_sec
= d
->bd_rtout
/ hz
;
1947 tv
.tv_usec
= (d
->bd_rtout
% hz
) * tick
;
1948 bcopy(&tv
, addr
, sizeof(tv
));
1955 case BIOCGSTATS
: { /* struct bpf_stat */
1958 bzero(&bs
, sizeof(bs
));
1959 bs
.bs_recv
= d
->bd_rcount
;
1960 bs
.bs_drop
= d
->bd_dcount
;
1961 bcopy(&bs
, addr
, sizeof(bs
));
1966 * Set immediate mode.
1968 case BIOCIMMEDIATE
: /* u_int */
1969 d
->bd_immediate
= *(u_int
*)(void *)addr
;
1972 case BIOCVERSION
: { /* struct bpf_version */
1973 struct bpf_version bv
;
1975 bzero(&bv
, sizeof(bv
));
1976 bv
.bv_major
= BPF_MAJOR_VERSION
;
1977 bv
.bv_minor
= BPF_MINOR_VERSION
;
1978 bcopy(&bv
, addr
, sizeof(bv
));
1983 * Get "header already complete" flag
1985 case BIOCGHDRCMPLT
: /* u_int */
1986 bcopy(&d
->bd_hdrcmplt
, addr
, sizeof(u_int
));
1990 * Set "header already complete" flag
1992 case BIOCSHDRCMPLT
: /* u_int */
1993 bcopy(addr
, &int_arg
, sizeof(int_arg
));
1994 d
->bd_hdrcmplt
= int_arg
? 1 : 0;
1998 * Get "see sent packets" flag
2000 case BIOCGSEESENT
: /* u_int */
2001 bcopy(&d
->bd_seesent
, addr
, sizeof(u_int
));
2005 * Set "see sent packets" flag
2007 case BIOCSSEESENT
: /* u_int */
2008 bcopy(addr
, &d
->bd_seesent
, sizeof(u_int
));
2012 * Set traffic service class
2014 case BIOCSETTC
: { /* int */
2017 bcopy(addr
, &tc
, sizeof(int));
2018 error
= bpf_set_traffic_class(d
, tc
);
2023 * Get traffic service class
2025 case BIOCGETTC
: /* int */
2026 bcopy(&d
->bd_traffic_class
, addr
, sizeof(int));
2029 case FIONBIO
: /* Non-blocking I/O; int */
2032 case FIOASYNC
: /* Send signal on receive packets; int */
2033 bcopy(addr
, &d
->bd_async
, sizeof(int));
2037 error
= fsetown(*(int *)addr
, &d
->bd_sigio
);
2041 *(int *)addr
= fgetown(d
->bd_sigio
);
2044 /* This is deprecated, FIOSETOWN should be used instead. */
2046 error
= fsetown(-(*(int *)addr
), &d
->bd_sigio
);
2049 /* This is deprecated, FIOGETOWN should be used instead. */
2051 *(int *)addr
= -fgetown(d
->bd_sigio
);
2054 case BIOCSRSIG
: { /* Set receive signal; u_int */
2057 bcopy(addr
, &sig
, sizeof(u_int
));
2066 case BIOCGRSIG
: /* u_int */
2067 bcopy(&d
->bd_sig
, addr
, sizeof(u_int
));
2070 case BIOCSEXTHDR
: /* u_int */
2071 bcopy(addr
, &int_arg
, sizeof(int_arg
));
2073 d
->bd_flags
|= BPF_EXTENDED_HDR
;
2075 d
->bd_flags
&= ~BPF_EXTENDED_HDR
;
2079 case BIOCGIFATTACHCOUNT
: { /* struct ifreq */
2083 bcopy(addr
, &ifr
, sizeof(ifr
));
2084 ifr
.ifr_name
[IFNAMSIZ
- 1] = '\0';
2085 ifp
= ifunit(ifr
.ifr_name
);
2091 for (bp
= bpf_iflist
; bp
!= 0; bp
= bp
->bif_next
) {
2092 struct bpf_d
*bpf_d
;
2094 if (bp
->bif_ifp
== NULL
|| bp
->bif_ifp
!= ifp
) {
2097 for (bpf_d
= bp
->bif_dlist
; bpf_d
;
2098 bpf_d
= bpf_d
->bd_next
) {
2099 ifr
.ifr_intval
+= 1;
2102 bcopy(&ifr
, addr
, sizeof(ifr
));
2105 case BIOCGWANTPKTAP
: /* u_int */
2106 int_arg
= d
->bd_flags
& BPF_WANT_PKTAP
? 1 : 0;
2107 bcopy(&int_arg
, addr
, sizeof(int_arg
));
2110 case BIOCSWANTPKTAP
: /* u_int */
2111 bcopy(addr
, &int_arg
, sizeof(int_arg
));
2113 d
->bd_flags
|= BPF_WANT_PKTAP
;
2115 d
->bd_flags
&= ~BPF_WANT_PKTAP
;
2121 bcopy(addr
, &int_arg
, sizeof(int_arg
));
2122 d
->bd_headdrop
= int_arg
? 1 : 0;
2126 bcopy(&d
->bd_headdrop
, addr
, sizeof(int));
2130 bcopy(addr
, &int_arg
, sizeof(int_arg
));
2132 d
->bd_flags
|= BPF_TRUNCATE
;
2134 d
->bd_flags
&= ~BPF_TRUNCATE
;
2139 bcopy(&d
->bd_uuid
, addr
, sizeof(uuid_t
));
2143 struct bpf_setup_args bsa
;
2146 bcopy(addr
, &bsa
, sizeof(struct bpf_setup_args
));
2147 bsa
.bsa_ifname
[IFNAMSIZ
- 1] = 0;
2148 ifp
= ifunit(bsa
.bsa_ifname
);
2151 os_log_info(OS_LOG_DEFAULT
,
2152 "%s: ifnet not found for %s error %d",
2153 __func__
, bsa
.bsa_ifname
, error
);
2157 error
= bpf_setup(d
, bsa
.bsa_uuid
, ifp
);
2161 bcopy(addr
, &int_arg
, sizeof(int_arg
));
2163 d
->bd_flags
|= BPF_PKTHDRV2
;
2165 d
->bd_flags
&= ~BPF_PKTHDRV2
;
2170 int_arg
= d
->bd_flags
& BPF_PKTHDRV2
? 1 : 0;
2171 bcopy(&int_arg
, addr
, sizeof(int));
2176 lck_mtx_unlock(bpf_mlock
);
2182 * Set d's packet filter program to fp. If this file already has a filter,
2183 * free it and replace it. Returns EINVAL for bogus requests.
2186 bpf_setf(struct bpf_d
*d
, u_int bf_len
, user_addr_t bf_insns
,
2189 struct bpf_insn
*fcode
, *old
;
2192 while (d
->bd_hbuf_read
!= 0) {
2193 msleep((caddr_t
)d
, bpf_mlock
, PRINET
, "bpf_reading", NULL
);
2196 if ((d
->bd_flags
& BPF_CLOSING
) != 0) {
2201 if (bf_insns
== USER_ADDR_NULL
) {
2205 d
->bd_filter
= NULL
;
2208 FREE(old
, M_DEVBUF
);
2213 if (flen
> BPF_MAXINSNS
) {
2217 size
= flen
* sizeof(struct bpf_insn
);
2218 fcode
= (struct bpf_insn
*) _MALLOC(size
, M_DEVBUF
, M_WAIT
);
2220 if (fcode
== NULL
) {
2224 if (copyin(bf_insns
, (caddr_t
)fcode
, size
) == 0 &&
2225 bpf_validate(fcode
, (int)flen
)) {
2226 d
->bd_filter
= fcode
;
2228 if (cmd
== BIOCSETF32
|| cmd
== BIOCSETF64
) {
2233 FREE(old
, M_DEVBUF
);
2238 FREE(fcode
, M_DEVBUF
);
2243 * Detach a file from its current interface (if attached at all) and attach
2244 * to the interface indicated by the name stored in ifr.
2245 * Return an errno or 0.
2248 bpf_setif(struct bpf_d
*d
, ifnet_t theywant
, bool do_reset
, bool has_hbuf_read
)
2253 while (d
->bd_hbuf_read
!= 0 && !has_hbuf_read
) {
2254 msleep((caddr_t
)d
, bpf_mlock
, PRINET
, "bpf_reading", NULL
);
2257 if ((d
->bd_flags
& BPF_CLOSING
) != 0) {
2262 * Look through attached interfaces for the named one.
2264 for (bp
= bpf_iflist
; bp
!= 0; bp
= bp
->bif_next
) {
2265 struct ifnet
*ifp
= bp
->bif_ifp
;
2267 if (ifp
== 0 || ifp
!= theywant
) {
2271 * Do not use DLT_PKTAP, unless requested explicitly
2273 if (bp
->bif_dlt
== DLT_PKTAP
&& !(d
->bd_flags
& BPF_WANT_PKTAP
)) {
2277 * Skip the coprocessor interface
2279 if (!intcoproc_unrestricted
&& IFNET_IS_INTCOPROC(ifp
)) {
2283 * We found the requested interface.
2284 * Allocate the packet buffers.
2286 error
= bpf_allocbufs(d
);
2291 * Detach if attached to something else.
2293 if (bp
!= d
->bd_bif
) {
2294 if (d
->bd_bif
!= NULL
) {
2295 if (bpf_detachd(d
, 0) != 0) {
2299 if (bpf_attachd(d
, bp
) != 0) {
2313 * Get a list of available data link type of the interface.
2316 bpf_getdltlist(struct bpf_d
*d
, caddr_t addr
, struct proc
*p
)
2323 struct bpf_dltlist bfl
;
2325 bcopy(addr
, &bfl
, sizeof(bfl
));
2326 if (proc_is64bit(p
)) {
2327 dlist
= (user_addr_t
)bfl
.bfl_u
.bflu_pad
;
2329 dlist
= CAST_USER_ADDR_T(bfl
.bfl_u
.bflu_list
);
2332 ifp
= d
->bd_bif
->bif_ifp
;
2336 for (bp
= bpf_iflist
; bp
; bp
= bp
->bif_next
) {
2337 if (bp
->bif_ifp
!= ifp
) {
2341 * Do not use DLT_PKTAP, unless requested explicitly
2343 if (bp
->bif_dlt
== DLT_PKTAP
&& !(d
->bd_flags
& BPF_WANT_PKTAP
)) {
2346 if (dlist
!= USER_ADDR_NULL
) {
2347 if (n
>= bfl
.bfl_len
) {
2350 error
= copyout(&bp
->bif_dlt
, dlist
,
2351 sizeof(bp
->bif_dlt
));
2355 dlist
+= sizeof(bp
->bif_dlt
);
2360 bcopy(&bfl
, addr
, sizeof(bfl
));
2366 * Set the data link type of a BPF instance.
2369 bpf_setdlt(struct bpf_d
*d
, uint32_t dlt
)
2371 int error
, opromisc
;
2375 if (d
->bd_bif
->bif_dlt
== dlt
) {
2379 while (d
->bd_hbuf_read
!= 0) {
2380 msleep((caddr_t
)d
, bpf_mlock
, PRINET
, "bpf_reading", NULL
);
2383 if ((d
->bd_flags
& BPF_CLOSING
) != 0) {
2387 ifp
= d
->bd_bif
->bif_ifp
;
2388 for (bp
= bpf_iflist
; bp
; bp
= bp
->bif_next
) {
2389 if (bp
->bif_ifp
== ifp
&& bp
->bif_dlt
== dlt
) {
2391 * Do not use DLT_PKTAP, unless requested explicitly
2393 if (bp
->bif_dlt
== DLT_PKTAP
&&
2394 !(d
->bd_flags
& BPF_WANT_PKTAP
)) {
2401 opromisc
= d
->bd_promisc
;
2402 if (bpf_detachd(d
, 0) != 0) {
2405 error
= bpf_attachd(d
, bp
);
2407 printf("bpf_setdlt: bpf_attachd %s%d failed (%d)\n",
2408 ifnet_name(bp
->bif_ifp
), ifnet_unit(bp
->bif_ifp
),
2414 lck_mtx_unlock(bpf_mlock
);
2415 error
= ifnet_set_promiscuous(bp
->bif_ifp
, 1);
2416 lck_mtx_lock(bpf_mlock
);
2418 printf("%s: ifpromisc %s%d failed (%d)\n",
2419 __func__
, ifnet_name(bp
->bif_ifp
),
2420 ifnet_unit(bp
->bif_ifp
), error
);
2426 return bp
== NULL
? EINVAL
: 0;
2430 bpf_set_traffic_class(struct bpf_d
*d
, int tc
)
2434 if (!SO_VALID_TC(tc
)) {
2437 d
->bd_traffic_class
= tc
;
2444 bpf_set_packet_service_class(struct mbuf
*m
, int tc
)
2446 if (!(m
->m_flags
& M_PKTHDR
)) {
2450 VERIFY(SO_VALID_TC(tc
));
2451 (void) m_set_service_class(m
, so_tc2msc(tc
));
2455 * Support for select()
2457 * Return true iff the specific operation will not block indefinitely.
2458 * Otherwise, return false but make a note that a selwakeup() must be done.
2461 bpfselect(dev_t dev
, int which
, void * wql
, struct proc
*p
)
2466 lck_mtx_lock(bpf_mlock
);
2468 d
= bpf_dtab
[minor(dev
)];
2469 if (d
== NULL
|| d
== BPF_DEV_RESERVED
||
2470 (d
->bd_flags
& BPF_CLOSING
) != 0) {
2471 lck_mtx_unlock(bpf_mlock
);
2477 if (d
->bd_bif
== NULL
) {
2479 lck_mtx_unlock(bpf_mlock
);
2483 while (d
->bd_hbuf_read
!= 0) {
2484 msleep((caddr_t
)d
, bpf_mlock
, PRINET
, "bpf_reading", NULL
);
2487 if ((d
->bd_flags
& BPF_CLOSING
) != 0) {
2489 lck_mtx_unlock(bpf_mlock
);
2495 if (d
->bd_hlen
!= 0 ||
2496 ((d
->bd_immediate
||
2497 d
->bd_state
== BPF_TIMED_OUT
) && d
->bd_slen
!= 0)) {
2498 ret
= 1; /* read has data to return */
2501 * Read has no data to return.
2502 * Make the select wait, and start a timer if
2505 selrecord(p
, &d
->bd_sel
, wql
);
2511 /* can't determine whether a write would block */
2517 lck_mtx_unlock(bpf_mlock
);
2523 * Support for kevent() system call. Register EVFILT_READ filters and
2524 * reject all others.
2526 int bpfkqfilter(dev_t dev
, struct knote
*kn
);
2527 static void filt_bpfdetach(struct knote
*);
2528 static int filt_bpfread(struct knote
*, long);
2529 static int filt_bpftouch(struct knote
*kn
, struct kevent_internal_s
*kev
);
2530 static int filt_bpfprocess(struct knote
*kn
, struct filt_process_s
*data
,
2531 struct kevent_internal_s
*kev
);
2533 SECURITY_READ_ONLY_EARLY(struct filterops
) bpfread_filtops
= {
2535 .f_detach
= filt_bpfdetach
,
2536 .f_event
= filt_bpfread
,
2537 .f_touch
= filt_bpftouch
,
2538 .f_process
= filt_bpfprocess
,
2542 filt_bpfread_common(struct knote
*kn
, struct bpf_d
*d
)
2546 if (d
->bd_immediate
) {
2548 * If there's data in the hold buffer, it's the
2549 * amount of data a read will return.
2551 * If there's no data in the hold buffer, but
2552 * there's data in the store buffer, a read will
2553 * immediately rotate the store buffer to the
2554 * hold buffer, the amount of data in the store
2555 * buffer is the amount of data a read will
2558 * If there's no data in either buffer, we're not
2561 kn
->kn_data
= (d
->bd_hlen
== 0 || d
->bd_hbuf_read
!= 0 ?
2562 d
->bd_slen
: d
->bd_hlen
);
2564 if (kn
->kn_sfflags
& NOTE_LOWAT
) {
2565 if (kn
->kn_sdata
> d
->bd_bufsize
) {
2566 lowwat
= d
->bd_bufsize
;
2567 } else if (kn
->kn_sdata
> lowwat
) {
2568 lowwat
= kn
->kn_sdata
;
2571 ready
= (kn
->kn_data
>= lowwat
);
2574 * If there's data in the hold buffer, it's the
2575 * amount of data a read will return.
2577 * If there's no data in the hold buffer, but
2578 * there's data in the store buffer, if the
2579 * timer has expired a read will immediately
2580 * rotate the store buffer to the hold buffer,
2581 * so the amount of data in the store buffer is
2582 * the amount of data a read will return.
2584 * If there's no data in either buffer, or there's
2585 * no data in the hold buffer and the timer hasn't
2586 * expired, we're not ready to read.
2588 kn
->kn_data
= ((d
->bd_hlen
== 0 || d
->bd_hbuf_read
!= 0) &&
2589 d
->bd_state
== BPF_TIMED_OUT
? d
->bd_slen
: d
->bd_hlen
);
2590 ready
= (kn
->kn_data
> 0);
2600 bpfkqfilter(dev_t dev
, struct knote
*kn
)
2606 * Is this device a bpf?
2608 if (major(dev
) != CDEV_MAJOR
||
2609 kn
->kn_filter
!= EVFILT_READ
) {
2610 kn
->kn_flags
= EV_ERROR
;
2611 kn
->kn_data
= EINVAL
;
2615 lck_mtx_lock(bpf_mlock
);
2617 d
= bpf_dtab
[minor(dev
)];
2619 if (d
== NULL
|| d
== BPF_DEV_RESERVED
||
2620 (d
->bd_flags
& BPF_CLOSING
) != 0 ||
2621 d
->bd_bif
== NULL
) {
2622 lck_mtx_unlock(bpf_mlock
);
2623 kn
->kn_flags
= EV_ERROR
;
2624 kn
->kn_data
= ENXIO
;
2629 kn
->kn_filtid
= EVFILTID_BPFREAD
;
2630 KNOTE_ATTACH(&d
->bd_sel
.si_note
, kn
);
2631 d
->bd_flags
|= BPF_KNOTE
;
2633 /* capture the current state */
2634 res
= filt_bpfread_common(kn
, d
);
2636 lck_mtx_unlock(bpf_mlock
);
2642 filt_bpfdetach(struct knote
*kn
)
2644 struct bpf_d
*d
= (struct bpf_d
*)kn
->kn_hook
;
2646 lck_mtx_lock(bpf_mlock
);
2647 if (d
->bd_flags
& BPF_KNOTE
) {
2648 KNOTE_DETACH(&d
->bd_sel
.si_note
, kn
);
2649 d
->bd_flags
&= ~BPF_KNOTE
;
2651 lck_mtx_unlock(bpf_mlock
);
2655 filt_bpfread(struct knote
*kn
, long hint
)
2657 #pragma unused(hint)
2658 struct bpf_d
*d
= (struct bpf_d
*)kn
->kn_hook
;
2660 return filt_bpfread_common(kn
, d
);
2664 filt_bpftouch(struct knote
*kn
, struct kevent_internal_s
*kev
)
2666 struct bpf_d
*d
= (struct bpf_d
*)kn
->kn_hook
;
2669 lck_mtx_lock(bpf_mlock
);
2671 /* save off the lowat threshold and flag */
2672 kn
->kn_sdata
= kev
->data
;
2673 kn
->kn_sfflags
= kev
->fflags
;
2675 /* output data will be re-generated here */
2676 res
= filt_bpfread_common(kn
, d
);
2678 lck_mtx_unlock(bpf_mlock
);
2684 filt_bpfprocess(struct knote
*kn
, struct filt_process_s
*data
,
2685 struct kevent_internal_s
*kev
)
2687 #pragma unused(data)
2688 struct bpf_d
*d
= (struct bpf_d
*)kn
->kn_hook
;
2691 lck_mtx_lock(bpf_mlock
);
2692 res
= filt_bpfread_common(kn
, d
);
2694 *kev
= kn
->kn_kevent
;
2696 lck_mtx_unlock(bpf_mlock
);
2702 * Copy data from an mbuf chain into a buffer. This code is derived
2703 * from m_copydata in kern/uipc_mbuf.c.
2706 bpf_mcopy(struct mbuf
* m
, void *dst_arg
, size_t len
)
2716 count
= min(m
->m_len
, len
);
2717 bcopy(mbuf_data(m
), dst
, count
);
2728 struct bpf_packet
*bpf_pkt
,
2736 * It's possible that we get here after the bpf descriptor has been
2737 * detached from the interface; in such a case we simply return.
2738 * Lock ordering is important since we can be called asynchronously
2739 * (from IOKit) to process an inbound packet; when that happens
2740 * we would have been holding its "gateLock" and will be acquiring
2741 * "bpf_mlock" upon entering this routine. Due to that, we release
2742 * "bpf_mlock" prior to calling ifnet_set_promiscuous (which will
2743 * acquire "gateLock" in the IOKit), in order to avoid a deadlock
2744 * when a ifnet_set_promiscuous request simultaneously collides with
2745 * an inbound packet being passed into the tap callback.
2747 lck_mtx_lock(bpf_mlock
);
2748 if (ifp
->if_bpf
== NULL
) {
2749 lck_mtx_unlock(bpf_mlock
);
2752 for (bp
= ifp
->if_bpf
; bp
!= NULL
; bp
= bp
->bif_next
) {
2753 if (bp
->bif_ifp
!= ifp
) {
2754 /* wrong interface */
2758 if (dlt
== 0 || bp
->bif_dlt
== dlt
) {
2759 /* tapping default DLT or DLT matches */
2766 for (d
= bp
->bif_dlist
; d
; d
= d
->bd_next
) {
2767 struct bpf_packet
*bpf_pkt_saved
= bpf_pkt
;
2768 struct bpf_packet bpf_pkt_tmp
;
2769 struct pktap_header_buffer bpfp_header_tmp
;
2771 if (outbound
&& !d
->bd_seesent
) {
2776 slen
= bpf_filter(d
->bd_filter
, (u_char
*)bpf_pkt
,
2777 bpf_pkt
->bpfp_total_length
, 0);
2778 if (bp
->bif_ifp
->if_type
== IFT_PKTAP
&&
2779 bp
->bif_dlt
== DLT_PKTAP
) {
2781 * Need to copy the bpf_pkt because the conversion
2782 * to v2 pktap header modifies the content of the
2785 if ((d
->bd_flags
& BPF_PKTHDRV2
) &&
2786 bpf_pkt
->bpfp_header_length
<= sizeof(bpfp_header_tmp
)) {
2787 bpf_pkt_tmp
= *bpf_pkt
;
2789 bpf_pkt
= &bpf_pkt_tmp
;
2791 memcpy(&bpfp_header_tmp
, bpf_pkt
->bpfp_header
,
2792 bpf_pkt
->bpfp_header_length
);
2794 bpf_pkt
->bpfp_header
= &bpfp_header_tmp
;
2796 convert_to_pktap_header_to_v2(bpf_pkt
,
2797 !!(d
->bd_flags
& BPF_TRUNCATE
));
2800 if (d
->bd_flags
& BPF_TRUNCATE
) {
2802 get_pkt_trunc_len((u_char
*)bpf_pkt
,
2803 bpf_pkt
->bpfp_total_length
));
2808 if (mac_bpfdesc_check_receive(d
, bp
->bif_ifp
) != 0) {
2812 catchpacket(d
, bpf_pkt
, slen
, outbound
);
2814 bpf_pkt
= bpf_pkt_saved
;
2818 lck_mtx_unlock(bpf_mlock
);
2830 struct bpf_packet bpf_pkt
;
2833 if (ifp
->if_bpf
== NULL
) {
2834 /* quickly check without taking lock */
2837 bpf_pkt
.bpfp_type
= BPF_PACKET_TYPE_MBUF
;
2838 bpf_pkt
.bpfp_mbuf
= m
;
2839 bpf_pkt
.bpfp_total_length
= 0;
2840 for (m0
= m
; m0
!= NULL
; m0
= m0
->m_next
) {
2841 bpf_pkt
.bpfp_total_length
+= m0
->m_len
;
2843 bpf_pkt
.bpfp_header
= hdr
;
2845 bpf_pkt
.bpfp_total_length
+= hlen
;
2846 bpf_pkt
.bpfp_header_length
= hlen
;
2848 bpf_pkt
.bpfp_header_length
= 0;
2850 bpf_tap_imp(ifp
, dlt
, &bpf_pkt
, outbound
);
2861 bpf_tap_mbuf(ifp
, dlt
, m
, hdr
, hlen
, 1);
2872 bpf_tap_mbuf(ifp
, dlt
, m
, hdr
, hlen
, 0);
2875 /* Callback registered with Ethernet driver. */
2877 bpf_tap_callback(struct ifnet
*ifp
, struct mbuf
*m
)
2879 bpf_tap_mbuf(ifp
, 0, m
, NULL
, 0, mbuf_pkthdr_rcvif(m
) == NULL
);
2886 bpf_copydata(struct bpf_packet
*pkt
, size_t off
, size_t len
, void* out_data
)
2889 if (pkt
->bpfp_type
== BPF_PACKET_TYPE_MBUF
) {
2890 err
= mbuf_copydata(pkt
->bpfp_mbuf
, off
, len
, out_data
);
2899 copy_bpf_packet(struct bpf_packet
* pkt
, void * dst
, size_t len
)
2901 /* copy the optional header */
2902 if (pkt
->bpfp_header_length
!= 0) {
2903 size_t count
= min(len
, pkt
->bpfp_header_length
);
2904 bcopy(pkt
->bpfp_header
, dst
, count
);
2909 /* nothing past the header */
2912 /* copy the packet */
2913 switch (pkt
->bpfp_type
) {
2914 case BPF_PACKET_TYPE_MBUF
:
2915 bpf_mcopy(pkt
->bpfp_mbuf
, dst
, len
);
2923 get_esp_trunc_len(__unused
struct bpf_packet
*pkt
, __unused
uint16_t off
,
2924 const uint16_t remaining_caplen
)
2927 * For some reason tcpdump expects to have one byte beyond the ESP header
2929 uint16_t trunc_len
= ESP_HDR_SIZE
+ 1;
2931 if (trunc_len
> remaining_caplen
) {
2932 return remaining_caplen
;
2939 get_isakmp_trunc_len(__unused
struct bpf_packet
*pkt
, __unused
uint16_t off
,
2940 const uint16_t remaining_caplen
)
2943 * Include the payload generic header
2945 uint16_t trunc_len
= ISAKMP_HDR_SIZE
;
2947 if (trunc_len
> remaining_caplen
) {
2948 return remaining_caplen
;
2955 get_isakmp_natt_trunc_len(struct bpf_packet
*pkt
, uint16_t off
,
2956 const uint16_t remaining_caplen
)
2959 uint16_t trunc_len
= 0;
2960 char payload
[remaining_caplen
];
2962 err
= bpf_copydata(pkt
, off
, remaining_caplen
, payload
);
2964 return remaining_caplen
;
2967 * They are three cases:
2968 * - IKE: payload start with 4 bytes header set to zero before ISAKMP header
2969 * - keep alive: 1 byte payload
2970 * - otherwise it's ESP
2972 if (remaining_caplen
>= 4 &&
2973 payload
[0] == 0 && payload
[1] == 0 &&
2974 payload
[2] == 0 && payload
[3] == 0) {
2975 trunc_len
= 4 + get_isakmp_trunc_len(pkt
, off
+ 4, remaining_caplen
- 4);
2976 } else if (remaining_caplen
== 1) {
2979 trunc_len
= get_esp_trunc_len(pkt
, off
, remaining_caplen
);
2982 if (trunc_len
> remaining_caplen
) {
2983 return remaining_caplen
;
2990 get_udp_trunc_len(struct bpf_packet
*pkt
, uint16_t off
, const uint16_t remaining_caplen
)
2993 uint16_t trunc_len
= sizeof(struct udphdr
); /* By default no UDP payload */
2995 if (trunc_len
>= remaining_caplen
) {
2996 return remaining_caplen
;
2999 struct udphdr udphdr
;
3000 err
= bpf_copydata(pkt
, off
, sizeof(struct udphdr
), &udphdr
);
3002 return remaining_caplen
;
3005 u_short sport
, dport
;
3007 sport
= EXTRACT_SHORT(&udphdr
.uh_sport
);
3008 dport
= EXTRACT_SHORT(&udphdr
.uh_dport
);
3010 if (dport
== PORT_DNS
|| sport
== PORT_DNS
) {
3012 * Full UDP payload for DNS
3014 trunc_len
= remaining_caplen
;
3015 } else if ((sport
== PORT_BOOTPS
&& dport
== PORT_BOOTPC
) ||
3016 (sport
== PORT_BOOTPC
&& dport
== PORT_BOOTPS
)) {
3018 * Full UDP payload for BOOTP and DHCP
3020 trunc_len
= remaining_caplen
;
3021 } else if (dport
== PORT_ISAKMP
&& sport
== PORT_ISAKMP
) {
3023 * Return the ISAKMP header
3025 trunc_len
+= get_isakmp_trunc_len(pkt
, off
+ sizeof(struct udphdr
),
3026 remaining_caplen
- sizeof(struct udphdr
));
3027 } else if (dport
== PORT_ISAKMP_NATT
&& sport
== PORT_ISAKMP_NATT
) {
3028 trunc_len
+= get_isakmp_natt_trunc_len(pkt
, off
+ sizeof(struct udphdr
),
3029 remaining_caplen
- sizeof(struct udphdr
));
3031 if (trunc_len
>= remaining_caplen
) {
3032 return remaining_caplen
;
3039 get_tcp_trunc_len(struct bpf_packet
*pkt
, uint16_t off
, const uint16_t remaining_caplen
)
3042 uint16_t trunc_len
= sizeof(struct tcphdr
); /* By default no TCP payload */
3043 if (trunc_len
>= remaining_caplen
) {
3044 return remaining_caplen
;
3047 struct tcphdr tcphdr
;
3048 err
= bpf_copydata(pkt
, off
, sizeof(struct tcphdr
), &tcphdr
);
3050 return remaining_caplen
;
3053 u_short sport
, dport
;
3054 sport
= EXTRACT_SHORT(&tcphdr
.th_sport
);
3055 dport
= EXTRACT_SHORT(&tcphdr
.th_dport
);
3057 if (dport
== PORT_DNS
|| sport
== PORT_DNS
) {
3059 * Full TCP payload for DNS
3061 trunc_len
= remaining_caplen
;
3063 trunc_len
= tcphdr
.th_off
<< 2;
3065 if (trunc_len
>= remaining_caplen
) {
3066 return remaining_caplen
;
3073 get_proto_trunc_len(uint8_t proto
, struct bpf_packet
*pkt
, uint16_t off
, const uint16_t remaining_caplen
)
3078 case IPPROTO_ICMP
: {
3082 trunc_len
= remaining_caplen
;
3085 case IPPROTO_ICMPV6
: {
3087 * Full IMCPV6 payload
3089 trunc_len
= remaining_caplen
;
3092 case IPPROTO_IGMP
: {
3096 trunc_len
= remaining_caplen
;
3100 trunc_len
= get_udp_trunc_len(pkt
, off
, remaining_caplen
);
3104 trunc_len
= get_tcp_trunc_len(pkt
, off
, remaining_caplen
);
3108 trunc_len
= get_esp_trunc_len(pkt
, off
, remaining_caplen
);
3113 * By default we only include the IP header
3119 if (trunc_len
>= remaining_caplen
) {
3120 return remaining_caplen
;
3127 get_ip_trunc_len(struct bpf_packet
*pkt
, uint16_t off
, const uint16_t remaining_caplen
)
3130 uint16_t iplen
= sizeof(struct ip
);
3131 if (iplen
>= remaining_caplen
) {
3132 return remaining_caplen
;
3136 err
= bpf_copydata(pkt
, off
, sizeof(struct ip
), &iphdr
);
3138 return remaining_caplen
;
3143 iplen
= iphdr
.ip_hl
<< 2;
3144 if (iplen
>= remaining_caplen
) {
3145 return remaining_caplen
;
3149 iplen
+= get_proto_trunc_len(proto
, pkt
, off
+ iplen
, remaining_caplen
- iplen
);
3151 if (iplen
>= remaining_caplen
) {
3152 return remaining_caplen
;
3159 get_ip6_trunc_len(struct bpf_packet
*pkt
, uint16_t off
, const uint16_t remaining_caplen
)
3162 uint16_t iplen
= sizeof(struct ip6_hdr
);
3163 if (iplen
>= remaining_caplen
) {
3164 return remaining_caplen
;
3167 struct ip6_hdr ip6hdr
;
3168 err
= bpf_copydata(pkt
, off
, sizeof(struct ip6_hdr
), &ip6hdr
);
3170 return remaining_caplen
;
3176 * TBD: process the extension headers
3178 proto
= ip6hdr
.ip6_nxt
;
3179 iplen
+= get_proto_trunc_len(proto
, pkt
, off
+ iplen
, remaining_caplen
- iplen
);
3181 if (iplen
>= remaining_caplen
) {
3182 return remaining_caplen
;
3189 get_ether_trunc_len(struct bpf_packet
*pkt
, int off
, const uint16_t remaining_caplen
)
3192 uint16_t ethlen
= sizeof(struct ether_header
);
3193 if (ethlen
>= remaining_caplen
) {
3194 return remaining_caplen
;
3197 struct ether_header eh
;
3199 err
= bpf_copydata(pkt
, off
, sizeof(struct ether_header
), &eh
);
3201 return remaining_caplen
;
3204 type
= EXTRACT_SHORT(&eh
.ether_type
);
3205 /* Include full ARP */
3206 if (type
== ETHERTYPE_ARP
) {
3207 ethlen
= remaining_caplen
;
3208 } else if (type
!= ETHERTYPE_IP
&& type
!= ETHERTYPE_IPV6
) {
3209 ethlen
= min(BPF_MIN_PKT_SIZE
, remaining_caplen
);
3211 if (type
== ETHERTYPE_IP
) {
3212 ethlen
+= get_ip_trunc_len(pkt
, sizeof(struct ether_header
),
3214 } else if (type
== ETHERTYPE_IPV6
) {
3215 ethlen
+= get_ip6_trunc_len(pkt
, sizeof(struct ether_header
),
3223 get_pkt_trunc_len(u_char
*p
, u_int len
)
3225 struct bpf_packet
*pkt
= (struct bpf_packet
*)(void *) p
;
3226 struct pktap_header
*pktap
= (struct pktap_header
*) (pkt
->bpfp_header
);
3227 uint32_t out_pkt_len
= 0, tlen
= 0;
3229 * pktap->pth_frame_pre_length is L2 header length and accounts
3230 * for both pre and pre_adjust.
3231 * pktap->pth_length is sizeof(pktap_header) (excl the pre/pre_adjust)
3232 * pkt->bpfp_header_length is (pktap->pth_length + pre_adjust)
3233 * pre is the offset to the L3 header after the bpfp_header, or length
3234 * of L2 header after bpfp_header, if present.
3236 uint32_t pre
= pktap
->pth_frame_pre_length
-
3237 (pkt
->bpfp_header_length
- pktap
->pth_length
);
3239 /* Length of the input packet starting from L3 header */
3240 uint32_t in_pkt_len
= len
- pkt
->bpfp_header_length
- pre
;
3241 if (pktap
->pth_protocol_family
== AF_INET
||
3242 pktap
->pth_protocol_family
== AF_INET6
) {
3243 /* Contains L2 header */
3245 if (pre
< sizeof(struct ether_header
)) {
3249 out_pkt_len
= get_ether_trunc_len(pkt
, 0, in_pkt_len
);
3250 } else if (pre
== 0) {
3251 if (pktap
->pth_protocol_family
== AF_INET
) {
3252 out_pkt_len
= get_ip_trunc_len(pkt
, pre
, in_pkt_len
);
3253 } else if (pktap
->pth_protocol_family
== AF_INET6
) {
3254 out_pkt_len
= get_ip6_trunc_len(pkt
, pre
, in_pkt_len
);
3257 /* Ideally pre should be >= 0. This is an exception */
3258 out_pkt_len
= min(BPF_MIN_PKT_SIZE
, in_pkt_len
);
3261 if (pktap
->pth_iftype
== IFT_ETHER
) {
3262 if (in_pkt_len
< sizeof(struct ether_header
)) {
3265 /* At most include the Ethernet header and 16 bytes */
3266 out_pkt_len
= MIN(sizeof(struct ether_header
) + 16,
3270 * For unknown protocols include at most 16 bytes
3272 out_pkt_len
= MIN(16, in_pkt_len
);
3276 tlen
= pkt
->bpfp_header_length
+ out_pkt_len
+ pre
;
3279 out_pkt_len
= in_pkt_len
;
3284 * Move the packet data from interface memory (pkt) into the
3285 * store buffer. Return 1 if it's time to wakeup a listener (buffer full),
3289 catchpacket(struct bpf_d
*d
, struct bpf_packet
* pkt
,
3290 u_int snaplen
, int outbound
)
3293 struct bpf_hdr_ext
*ehp
;
3300 hdrlen
= (d
->bd_flags
& BPF_EXTENDED_HDR
) ? d
->bd_bif
->bif_exthdrlen
:
3301 d
->bd_bif
->bif_hdrlen
;
3303 * Figure out how many bytes to move. If the packet is
3304 * greater or equal to the snapshot length, transfer that
3305 * much. Otherwise, transfer the whole packet (unless
3306 * we hit the buffer size limit).
3308 totlen
= hdrlen
+ min(snaplen
, pkt
->bpfp_total_length
);
3309 if (totlen
> d
->bd_bufsize
) {
3310 totlen
= d
->bd_bufsize
;
3313 if (hdrlen
> totlen
) {
3318 * Round up the end of the previous packet to the next longword.
3320 curlen
= BPF_WORDALIGN(d
->bd_slen
);
3321 if (curlen
+ totlen
> d
->bd_bufsize
) {
3323 * This packet will overflow the storage buffer.
3324 * Rotate the buffers if we can, then wakeup any
3327 * We cannot rotate buffers if a read is in progress
3328 * so drop the packet
3330 if (d
->bd_hbuf_read
!= 0) {
3335 if (d
->bd_fbuf
== NULL
) {
3336 if (d
->bd_headdrop
== 0) {
3338 * We haven't completed the previous read yet,
3339 * so drop the packet.
3345 * Drop the hold buffer as it contains older packets
3347 d
->bd_dcount
+= d
->bd_hcnt
;
3348 d
->bd_fbuf
= d
->bd_hbuf
;
3355 } else if (d
->bd_immediate
|| d
->bd_state
== BPF_TIMED_OUT
) {
3357 * Immediate mode is set, or the read timeout has
3358 * already expired during a select call. A packet
3359 * arrived, so the reader should be woken up.
3365 * Append the bpf header.
3368 if (d
->bd_flags
& BPF_EXTENDED_HDR
) {
3371 m
= (pkt
->bpfp_type
== BPF_PACKET_TYPE_MBUF
)
3372 ? pkt
->bpfp_mbuf
: NULL
;
3373 ehp
= (struct bpf_hdr_ext
*)(void *)(d
->bd_sbuf
+ curlen
);
3374 memset(ehp
, 0, sizeof(*ehp
));
3375 ehp
->bh_tstamp
.tv_sec
= tv
.tv_sec
;
3376 ehp
->bh_tstamp
.tv_usec
= tv
.tv_usec
;
3378 ehp
->bh_datalen
= pkt
->bpfp_total_length
;
3379 ehp
->bh_hdrlen
= hdrlen
;
3380 caplen
= ehp
->bh_caplen
= totlen
- hdrlen
;
3383 ehp
->bh_flags
|= BPF_HDR_EXT_FLAGS_DIR_OUT
;
3385 ehp
->bh_flags
|= BPF_HDR_EXT_FLAGS_DIR_IN
;
3387 } else if (outbound
) {
3388 ehp
->bh_flags
|= BPF_HDR_EXT_FLAGS_DIR_OUT
;
3390 /* only do lookups on non-raw INPCB */
3391 if ((m
->m_pkthdr
.pkt_flags
& (PKTF_FLOW_ID
|
3392 PKTF_FLOW_LOCALSRC
| PKTF_FLOW_RAWSOCK
)) ==
3393 (PKTF_FLOW_ID
| PKTF_FLOW_LOCALSRC
) &&
3394 m
->m_pkthdr
.pkt_flowsrc
== FLOWSRC_INPCB
) {
3395 ehp
->bh_flowid
= m
->m_pkthdr
.pkt_flowid
;
3396 ehp
->bh_proto
= m
->m_pkthdr
.pkt_proto
;
3398 ehp
->bh_svc
= so_svc2tc(m
->m_pkthdr
.pkt_svc
);
3399 if (m
->m_pkthdr
.pkt_flags
& PKTF_TCP_REXMT
) {
3400 ehp
->bh_pktflags
|= BPF_PKTFLAGS_TCP_REXMT
;
3402 if (m
->m_pkthdr
.pkt_flags
& PKTF_START_SEQ
) {
3403 ehp
->bh_pktflags
|= BPF_PKTFLAGS_START_SEQ
;
3405 if (m
->m_pkthdr
.pkt_flags
& PKTF_LAST_PKT
) {
3406 ehp
->bh_pktflags
|= BPF_PKTFLAGS_LAST_PKT
;
3408 if (m
->m_pkthdr
.pkt_flags
& PKTF_VALID_UNSENT_DATA
) {
3409 ehp
->bh_unsent_bytes
=
3410 m
->m_pkthdr
.bufstatus_if
;
3411 ehp
->bh_unsent_snd
=
3412 m
->m_pkthdr
.bufstatus_sndbuf
;
3415 ehp
->bh_flags
|= BPF_HDR_EXT_FLAGS_DIR_IN
;
3417 payload
= (u_char
*)ehp
+ hdrlen
;
3419 hp
= (struct bpf_hdr
*)(void *)(d
->bd_sbuf
+ curlen
);
3420 hp
->bh_tstamp
.tv_sec
= tv
.tv_sec
;
3421 hp
->bh_tstamp
.tv_usec
= tv
.tv_usec
;
3422 hp
->bh_datalen
= pkt
->bpfp_total_length
;
3423 hp
->bh_hdrlen
= hdrlen
;
3424 caplen
= hp
->bh_caplen
= totlen
- hdrlen
;
3425 payload
= (u_char
*)hp
+ hdrlen
;
3428 * Copy the packet data into the store buffer and update its length.
3430 copy_bpf_packet(pkt
, payload
, caplen
);
3431 d
->bd_slen
= curlen
+ totlen
;
3440 * Initialize all nonzero fields of a descriptor.
3443 bpf_allocbufs(struct bpf_d
*d
)
3445 if (d
->bd_sbuf
!= NULL
) {
3446 FREE(d
->bd_sbuf
, M_DEVBUF
);
3449 if (d
->bd_hbuf
!= NULL
) {
3450 FREE(d
->bd_hbuf
, M_DEVBUF
);
3453 if (d
->bd_fbuf
!= NULL
) {
3454 FREE(d
->bd_fbuf
, M_DEVBUF
);
3458 d
->bd_fbuf
= (caddr_t
) _MALLOC(d
->bd_bufsize
, M_DEVBUF
, M_WAIT
);
3459 if (d
->bd_fbuf
== NULL
) {
3463 d
->bd_sbuf
= (caddr_t
) _MALLOC(d
->bd_bufsize
, M_DEVBUF
, M_WAIT
);
3464 if (d
->bd_sbuf
== NULL
) {
3465 FREE(d
->bd_fbuf
, M_DEVBUF
);
3477 * Free buffers currently in use by a descriptor.
3481 bpf_freed(struct bpf_d
*d
)
3484 * We don't need to lock out interrupts since this descriptor has
3485 * been detached from its interface and it yet hasn't been marked
3488 if (d
->bd_hbuf_read
!= 0) {
3489 panic("bpf buffer freed during read");
3492 if (d
->bd_sbuf
!= 0) {
3493 FREE(d
->bd_sbuf
, M_DEVBUF
);
3494 if (d
->bd_hbuf
!= 0) {
3495 FREE(d
->bd_hbuf
, M_DEVBUF
);
3497 if (d
->bd_fbuf
!= 0) {
3498 FREE(d
->bd_fbuf
, M_DEVBUF
);
3502 FREE(d
->bd_filter
, M_DEVBUF
);
3507 * Attach an interface to bpf. driverp is a pointer to a (struct bpf_if *)
3508 * in the driver's softc; dlt is the link layer type; hdrlen is the fixed
3509 * size of the link header (variable length headers not yet supported).
3512 bpfattach(struct ifnet
*ifp
, u_int dlt
, u_int hdrlen
)
3514 bpf_attach(ifp
, dlt
, hdrlen
, NULL
, NULL
);
3526 struct bpf_if
*bp_new
;
3527 struct bpf_if
*bp_before_first
= NULL
;
3528 struct bpf_if
*bp_first
= NULL
;
3529 struct bpf_if
*bp_last
= NULL
;
3532 bp_new
= (struct bpf_if
*) _MALLOC(sizeof(*bp_new
), M_DEVBUF
,
3538 lck_mtx_lock(bpf_mlock
);
3541 * Check if this interface/dlt is already attached. Remember the
3542 * first and last attachment for this interface, as well as the
3543 * element before the first attachment.
3546 for (bp
= bpf_iflist
; bp
!= NULL
; bp
= bp
->bif_next
) {
3547 if (bp
->bif_ifp
!= ifp
) {
3548 if (bp_first
!= NULL
) {
3549 /* no more elements for this interface */
3552 bp_before_first
= bp
;
3554 if (bp
->bif_dlt
== dlt
) {
3558 if (bp_first
== NULL
) {
3565 lck_mtx_unlock(bpf_mlock
);
3566 printf("bpfattach - %s with dlt %d is already attached\n",
3568 FREE(bp_new
, M_DEVBUF
);
3572 bp_new
->bif_ifp
= ifp
;
3573 bp_new
->bif_dlt
= dlt
;
3574 bp_new
->bif_send
= send
;
3575 bp_new
->bif_tap
= tap
;
3577 if (bp_first
== NULL
) {
3578 /* No other entries for this ifp */
3579 bp_new
->bif_next
= bpf_iflist
;
3580 bpf_iflist
= bp_new
;
3582 if (ifnet_type(ifp
) == IFT_ETHER
&& dlt
== DLT_EN10MB
) {
3583 /* Make this the first entry for this interface */
3584 if (bp_before_first
!= NULL
) {
3585 /* point the previous to us */
3586 bp_before_first
->bif_next
= bp_new
;
3588 /* we're the new head */
3589 bpf_iflist
= bp_new
;
3591 bp_new
->bif_next
= bp_first
;
3593 /* Add this after the last entry for this interface */
3594 bp_new
->bif_next
= bp_last
->bif_next
;
3595 bp_last
->bif_next
= bp_new
;
3600 * Compute the length of the bpf header. This is not necessarily
3601 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
3602 * that the network layer header begins on a longword boundary (for
3603 * performance reasons and to alleviate alignment restrictions).
3605 bp_new
->bif_hdrlen
= BPF_WORDALIGN(hdrlen
+ SIZEOF_BPF_HDR
) - hdrlen
;
3606 bp_new
->bif_exthdrlen
= BPF_WORDALIGN(hdrlen
+
3607 sizeof(struct bpf_hdr_ext
)) - hdrlen
;
3609 /* Take a reference on the interface */
3610 ifnet_reference(ifp
);
3612 lck_mtx_unlock(bpf_mlock
);
3616 printf("bpf: %s attached\n", if_name(ifp
));
3624 * Detach bpf from an interface. This involves detaching each descriptor
3625 * associated with the interface, and leaving bd_bif NULL. Notify each
3626 * descriptor as it's detached so that any sleepers wake up and get
3630 bpfdetach(struct ifnet
*ifp
)
3632 struct bpf_if
*bp
, *bp_prev
, *bp_next
;
3635 if (bpf_debug
!= 0) {
3636 printf("%s: %s\n", __func__
, if_name(ifp
));
3639 lck_mtx_lock(bpf_mlock
);
3642 * Build the list of devices attached to that interface
3643 * that we need to free while keeping the lock to maintain
3644 * the integrity of the interface list
3647 for (bp
= bpf_iflist
; bp
!= NULL
; bp
= bp_next
) {
3648 bp_next
= bp
->bif_next
;
3650 if (ifp
!= bp
->bif_ifp
) {
3654 /* Unlink from the interface list */
3656 bp_prev
->bif_next
= bp
->bif_next
;
3658 bpf_iflist
= bp
->bif_next
;
3661 /* Detach the devices attached to the interface */
3662 while ((d
= bp
->bif_dlist
) != NULL
) {
3664 * Take an extra reference to prevent the device
3665 * from being freed when bpf_detachd() releases
3666 * the reference for the interface list
3676 lck_mtx_unlock(bpf_mlock
);
3680 bpf_init(__unused
void *unused
)
3686 if (bpf_devsw_installed
== 0) {
3687 bpf_devsw_installed
= 1;
3688 bpf_mlock_grp_attr
= lck_grp_attr_alloc_init();
3689 bpf_mlock_grp
= lck_grp_alloc_init("bpf", bpf_mlock_grp_attr
);
3690 bpf_mlock_attr
= lck_attr_alloc_init();
3691 lck_mtx_init(bpf_mlock
, bpf_mlock_grp
, bpf_mlock_attr
);
3692 maj
= cdevsw_add(CDEV_MAJOR
, &bpf_cdevsw
);
3694 if (bpf_mlock_attr
) {
3695 lck_attr_free(bpf_mlock_attr
);
3697 if (bpf_mlock_grp
) {
3698 lck_grp_free(bpf_mlock_grp
);
3700 if (bpf_mlock_grp_attr
) {
3701 lck_grp_attr_free(bpf_mlock_grp_attr
);
3705 bpf_mlock_attr
= NULL
;
3706 bpf_mlock_grp
= NULL
;
3707 bpf_mlock_grp_attr
= NULL
;
3708 bpf_devsw_installed
= 0;
3709 printf("bpf_init: failed to allocate a major number\n");
3713 for (i
= 0; i
< NBPFILTER
; i
++) {
3714 bpf_make_dev_t(maj
);
3718 cdevsw_add(&bpf_cdevsw
);
3723 SYSINIT(bpfdev
, SI_SUB_DRIVERS
, SI_ORDER_MIDDLE
+ CDEV_MAJOR
, bpf_drvinit
, NULL
)
3728 mac_bpfdesc_label_get(struct bpf_d
*d
)
3734 mac_bpfdesc_label_set(struct bpf_d
*d
, struct label
*label
)
3736 d
->bd_label
= label
;