2 * Copyright (c) 2000-2017 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/in_pcb.h>
112 #include <netinet/in_var.h>
113 #include <netinet/ip_var.h>
114 #include <netinet/tcp.h>
115 #include <netinet/tcp_var.h>
116 #include <netinet/udp.h>
117 #include <netinet/udp_var.h>
118 #include <netinet/if_ether.h>
119 #include <sys/kernel.h>
120 #include <sys/sysctl.h>
121 #include <net/firewire.h>
123 #include <miscfs/devfs/devfs.h>
124 #include <net/dlil.h>
125 #include <net/pktap.h>
127 #include <kern/locks.h>
128 #include <kern/thread_call.h>
129 #include <libkern/section_keywords.h>
132 #include <security/mac_framework.h>
135 extern int tvtohz(struct timeval
*);
137 #define BPF_BUFSIZE 4096
138 #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, uio)
141 #define PRINET 26 /* interruptible */
143 typedef void (*pktcopyfunc_t
)(const void *, void *, size_t);
146 * The default read buffer size is patchable.
148 static unsigned int bpf_bufsize
= BPF_BUFSIZE
;
149 SYSCTL_INT(_debug
, OID_AUTO
, bpf_bufsize
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
150 &bpf_bufsize
, 0, "");
151 __private_extern__
unsigned int bpf_maxbufsize
= BPF_MAXBUFSIZE
;
152 SYSCTL_INT(_debug
, OID_AUTO
, bpf_maxbufsize
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
153 &bpf_maxbufsize
, 0, "");
154 static unsigned int bpf_maxdevices
= 256;
155 SYSCTL_UINT(_debug
, OID_AUTO
, bpf_maxdevices
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
156 &bpf_maxdevices
, 0, "");
158 * bpf_wantpktap controls the defaul visibility of DLT_PKTAP
159 * For OS X is off by default so process need to use the ioctl BPF_WANT_PKTAP
160 * explicitly to be able to use DLT_PKTAP.
163 static unsigned int bpf_wantpktap
= 1;
165 static unsigned int bpf_wantpktap
= 0;
167 SYSCTL_UINT(_debug
, OID_AUTO
, bpf_wantpktap
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
168 &bpf_wantpktap
, 0, "");
170 static int bpf_debug
= 0;
171 SYSCTL_INT(_debug
, OID_AUTO
, bpf_debug
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
175 * bpf_iflist is the list of interfaces; each corresponds to an ifnet
176 * bpf_dtab holds pointer to the descriptors, indexed by minor device #
178 static struct bpf_if
*bpf_iflist
;
181 * BSD now stores the bpf_d in the dev_t which is a struct
182 * on their system. Our dev_t is an int, so we still store
183 * the bpf_d in a separate table indexed by minor device #.
185 * The value stored in bpf_dtab[n] represent three states:
186 * 0: device not opened
187 * 1: device opening or closing
188 * other: device <n> opened with pointer to storage
190 static struct bpf_d
**bpf_dtab
= NULL
;
191 static unsigned int bpf_dtab_size
= 0;
192 static unsigned int nbpfilter
= 0;
194 decl_lck_mtx_data(static, bpf_mlock_data
);
195 static lck_mtx_t
*bpf_mlock
= &bpf_mlock_data
;
196 static lck_grp_t
*bpf_mlock_grp
;
197 static lck_grp_attr_t
*bpf_mlock_grp_attr
;
198 static lck_attr_t
*bpf_mlock_attr
;
200 #endif /* __APPLE__ */
202 static int bpf_allocbufs(struct bpf_d
*);
203 static errno_t
bpf_attachd(struct bpf_d
*d
, struct bpf_if
*bp
);
204 static int bpf_detachd(struct bpf_d
*d
, int);
205 static void bpf_freed(struct bpf_d
*);
206 static int bpf_movein(struct uio
*, int,
207 struct mbuf
**, struct sockaddr
*, int *);
208 static int bpf_setif(struct bpf_d
*, ifnet_t ifp
);
209 static void bpf_timed_out(void *, void *);
210 static void bpf_wakeup(struct bpf_d
*);
211 static void catchpacket(struct bpf_d
*, struct bpf_packet
*, u_int
, int);
212 static void reset_d(struct bpf_d
*);
213 static int bpf_setf(struct bpf_d
*, u_int
, user_addr_t
, u_long
);
214 static int bpf_getdltlist(struct bpf_d
*, caddr_t
, struct proc
*);
215 static int bpf_setdlt(struct bpf_d
*, u_int
);
216 static int bpf_set_traffic_class(struct bpf_d
*, int);
217 static void bpf_set_packet_service_class(struct mbuf
*, int);
219 static void bpf_acquire_d(struct bpf_d
*);
220 static void bpf_release_d(struct bpf_d
*);
222 static int bpf_devsw_installed
;
224 void bpf_init(void *unused
);
225 static int bpf_tap_callback(struct ifnet
*ifp
, struct mbuf
*m
);
228 * Darwin differs from BSD here, the following are static
229 * on BSD and not static on Darwin.
235 ioctl_fcn_t bpfioctl
;
236 select_fcn_t bpfselect
;
239 /* Darwin's cdevsw struct differs slightly from BSDs */
240 #define CDEV_MAJOR 23
241 static struct cdevsw bpf_cdevsw
= {
243 /* close */ bpfclose
,
245 /* write */ bpfwrite
,
246 /* ioctl */ bpfioctl
,
248 /* reset */ eno_reset
,
250 /* select */ bpfselect
,
252 /* strategy*/ eno_strat
,
258 #define SOCKADDR_HDR_LEN offsetof(struct sockaddr, sa_data)
261 bpf_movein(struct uio
*uio
, int linktype
, struct mbuf
**mp
, struct sockaddr
*sockp
, int *datlen
)
279 sa_family
= AF_UNSPEC
;
280 /* XXX Would MAXLINKHDR be better? */
281 hlen
= sizeof(struct ether_header
);
286 #if defined(__FreeBSD__) || defined(__bsdi__)
287 sa_family
= AF_IMPLINK
;
290 sa_family
= AF_UNSPEC
;
291 /* XXX 4(FORMAC)+6(dst)+6(src)+3(LLC)+5(SNAP) */
299 sa_family
= AF_UNSPEC
;
304 case DLT_ATM_RFC1483
:
306 * en atm driver requires 4-byte atm pseudo header.
307 * though it isn't standard, vpi:vci needs to be
310 sa_family
= AF_UNSPEC
;
311 hlen
= 12; /* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
316 sa_family
= AF_UNSPEC
;
317 hlen
= 4; /* This should match PPP_HDRLEN */
320 case DLT_APPLE_IP_OVER_IEEE1394
:
321 sa_family
= AF_UNSPEC
;
322 hlen
= sizeof(struct firewire_header
);
325 case DLT_IEEE802_11
: /* IEEE 802.11 wireless */
326 sa_family
= AF_IEEE80211
;
330 case DLT_IEEE802_11_RADIO
:
331 sa_family
= AF_IEEE80211
;
339 // LP64todo - fix this!
340 len
= uio_resid(uio
);
341 *datlen
= len
- hlen
;
342 if ((unsigned)len
> MCLBYTES
)
347 * Build a sockaddr based on the data link layer type.
348 * We do this at this level because the ethernet header
349 * is copied directly into the data field of the sockaddr.
350 * In the case of SLIP, there is no header and the packet
351 * is forwarded as is.
352 * Also, we are careful to leave room at the front of the mbuf
353 * for the link level header.
355 if ((hlen
+ SOCKADDR_HDR_LEN
) > sockp
->sa_len
) {
358 sockp
->sa_family
= sa_family
;
361 * We're directly sending the packet data supplied by
362 * the user; we don't need to make room for the link
363 * header, and don't need the header length value any
364 * more, so set it to 0.
369 MGETHDR(m
, M_WAIT
, MT_DATA
);
372 if ((unsigned)len
> MHLEN
) {
374 if ((m
->m_flags
& M_EXT
) == 0) {
379 m
->m_pkthdr
.len
= m
->m_len
= len
;
380 m
->m_pkthdr
.rcvif
= NULL
;
384 * Make room for link header.
387 m
->m_pkthdr
.len
-= hlen
;
389 m
->m_data
+= hlen
; /* XXX */
390 error
= UIOMOVE((caddr_t
)sockp
->sa_data
, hlen
, UIO_WRITE
, uio
);
394 error
= UIOMOVE(mtod(m
, caddr_t
), len
- hlen
, UIO_WRITE
, uio
);
398 /* Check for multicast destination */
401 struct ether_header
*eh
= mtod(m
, struct ether_header
*);
403 if (ETHER_IS_MULTICAST(eh
->ether_dhost
)) {
404 if (_ether_cmp(etherbroadcastaddr
, eh
->ether_dhost
) == 0)
405 m
->m_flags
|= M_BCAST
;
407 m
->m_flags
|= M_MCAST
;
422 * The dynamic addition of a new device node must block all processes that
423 * are opening the last device so that no process will get an unexpected
427 bpf_make_dev_t(int maj
)
429 static int bpf_growing
= 0;
430 unsigned int cur_size
= nbpfilter
, i
;
432 if (nbpfilter
>= bpf_maxdevices
)
435 while (bpf_growing
) {
436 /* Wait until new device has been created */
437 (void)tsleep((caddr_t
)&bpf_growing
, PZERO
, "bpf_growing", 0);
439 if (nbpfilter
> cur_size
) {
440 /* other thread grew it already */
445 /* need to grow bpf_dtab first */
446 if (nbpfilter
== bpf_dtab_size
) {
448 struct bpf_d
**new_dtab
= NULL
;
449 struct bpf_d
**old_dtab
= NULL
;
451 new_dtab_size
= bpf_dtab_size
+ NBPFILTER
;
452 new_dtab
= (struct bpf_d
**)_MALLOC(sizeof(struct bpf_d
*) * new_dtab_size
, M_DEVBUF
, M_WAIT
);
454 printf("bpf_make_dev_t: malloc bpf_dtab failed\n");
458 bcopy(bpf_dtab
, new_dtab
,
459 sizeof(struct bpf_d
*) * bpf_dtab_size
);
461 bzero(new_dtab
+ bpf_dtab_size
,
462 sizeof(struct bpf_d
*) * NBPFILTER
);
465 bpf_dtab_size
= new_dtab_size
;
466 if (old_dtab
!= NULL
)
467 _FREE(old_dtab
, M_DEVBUF
);
470 (void) devfs_make_node(makedev(maj
, i
),
471 DEVFS_CHAR
, UID_ROOT
, GID_WHEEL
, 0600,
475 wakeup((caddr_t
)&bpf_growing
);
481 * Attach file to the bpf interface, i.e. make d listen on bp.
484 bpf_attachd(struct bpf_d
*d
, struct bpf_if
*bp
)
486 int first
= bp
->bif_dlist
== NULL
;
490 * Point d at bp, and add d to the interface's list of listeners.
491 * Finally, point the driver's bpf cookie at the interface so
492 * it will divert packets to bpf.
495 d
->bd_next
= bp
->bif_dlist
;
499 * Take a reference on the device even if an error is returned
500 * because we keep the device in the interface's list of listeners
505 /* Find the default bpf entry for this ifp */
506 if (bp
->bif_ifp
->if_bpf
== NULL
) {
507 struct bpf_if
*tmp
, *primary
= NULL
;
509 for (tmp
= bpf_iflist
; tmp
; tmp
= tmp
->bif_next
) {
510 if (tmp
->bif_ifp
== bp
->bif_ifp
) {
515 bp
->bif_ifp
->if_bpf
= primary
;
517 /* Only call dlil_set_bpf_tap for primary dlt */
518 if (bp
->bif_ifp
->if_bpf
== bp
)
519 dlil_set_bpf_tap(bp
->bif_ifp
, BPF_TAP_INPUT_OUTPUT
, bpf_tap_callback
);
521 if (bp
->bif_tap
!= NULL
)
522 error
= bp
->bif_tap(bp
->bif_ifp
, bp
->bif_dlt
, BPF_TAP_INPUT_OUTPUT
);
526 * Reset the detach flags in case we previously detached an interface
528 d
->bd_flags
&= ~(BPF_DETACHING
| BPF_DETACHED
);
530 if (bp
->bif_dlt
== DLT_PKTAP
) {
531 d
->bd_flags
|= BPF_FINALIZE_PKTAP
;
533 d
->bd_flags
&= ~BPF_FINALIZE_PKTAP
;
539 * Detach a file from its interface.
541 * Return 1 if was closed by some thread, 0 otherwise
544 bpf_detachd(struct bpf_d
*d
, int closing
)
551 * Some other thread already detached
553 if ((d
->bd_flags
& (BPF_DETACHED
| BPF_DETACHING
)) != 0)
556 * This thread is doing the detach
558 d
->bd_flags
|= BPF_DETACHING
;
560 ifp
= d
->bd_bif
->bif_ifp
;
564 printf("%s: %llx %s%s\n",
565 __func__
, (uint64_t)VM_KERNEL_ADDRPERM(d
),
566 if_name(ifp
), closing
? " closing" : "");
568 /* Remove d from the interface's descriptor list. */
573 panic("bpf_detachd: descriptor not in list");
576 if (bp
->bif_dlist
== 0) {
578 * Let the driver know that there are no more listeners.
580 /* Only call dlil_set_bpf_tap for primary dlt */
581 if (bp
->bif_ifp
->if_bpf
== bp
)
582 dlil_set_bpf_tap(ifp
, BPF_TAP_DISABLE
, NULL
);
584 bp
->bif_tap(ifp
, bp
->bif_dlt
, BPF_TAP_DISABLE
);
586 for (bp
= bpf_iflist
; bp
; bp
= bp
->bif_next
)
587 if (bp
->bif_ifp
== ifp
&& bp
->bif_dlist
!= 0)
594 * Check if this descriptor had requested promiscuous mode.
595 * If so, turn it off.
599 lck_mtx_unlock(bpf_mlock
);
600 if (ifnet_set_promiscuous(ifp
, 0)) {
602 * Something is really wrong if we were able to put
603 * the driver into promiscuous mode, but can't
605 * Most likely the network interface is gone.
607 printf("%s: ifnet_set_promiscuous failed\n", __func__
);
609 lck_mtx_lock(bpf_mlock
);
613 * Wake up other thread that are waiting for this thread to finish
616 d
->bd_flags
&= ~BPF_DETACHING
;
617 d
->bd_flags
|= BPF_DETACHED
;
619 * Note that We've kept the reference because we may have dropped
620 * the lock when turning off promiscuous mode
626 * When closing makes sure no other thread refer to the bpf_d
629 printf("%s: %llx done\n",
630 __func__
, (uint64_t)VM_KERNEL_ADDRPERM(d
));
632 * Let the caller know the bpf_d is closed
634 if ((d
->bd_flags
& BPF_CLOSING
))
642 * Start asynchronous timer, if necessary.
643 * Must be called with bpf_mlock held.
646 bpf_start_timer(struct bpf_d
*d
)
651 if (d
->bd_rtout
> 0 && d
->bd_state
== BPF_IDLE
) {
652 tv
.tv_sec
= d
->bd_rtout
/ hz
;
653 tv
.tv_usec
= (d
->bd_rtout
% hz
) * tick
;
655 clock_interval_to_deadline(
656 (uint64_t)tv
.tv_sec
* USEC_PER_SEC
+ tv
.tv_usec
,
657 NSEC_PER_USEC
, &deadline
);
659 * The state is BPF_IDLE, so the timer hasn't
660 * been started yet, and hasn't gone off yet;
661 * there is no thread call scheduled, so this
662 * won't change the schedule.
664 * XXX - what if, by the time it gets entered,
665 * the deadline has already passed?
667 thread_call_enter_delayed(d
->bd_thread_call
, deadline
);
668 d
->bd_state
= BPF_WAITING
;
673 * Cancel asynchronous timer.
674 * Must be called with bpf_mlock held.
677 bpf_stop_timer(struct bpf_d
*d
)
680 * If the timer has already gone off, this does nothing.
681 * Our caller is expected to set d->bd_state to BPF_IDLE,
682 * with the bpf_mlock, after we are called. bpf_timed_out()
683 * also grabs bpf_mlock, so, if the timer has gone off and
684 * bpf_timed_out() hasn't finished, it's waiting for the
685 * lock; when this thread releases the lock, it will
686 * find the state is BPF_IDLE, and just release the
689 return (thread_call_cancel(d
->bd_thread_call
));
693 bpf_acquire_d(struct bpf_d
*d
)
695 void *lr_saved
= __builtin_return_address(0);
697 LCK_MTX_ASSERT(bpf_mlock
, LCK_MTX_ASSERT_OWNED
);
701 d
->bd_ref_lr
[d
->bd_next_ref_lr
] = lr_saved
;
702 d
->bd_next_ref_lr
= (d
->bd_next_ref_lr
+ 1) % BPF_REF_HIST
;
706 bpf_release_d(struct bpf_d
*d
)
708 void *lr_saved
= __builtin_return_address(0);
710 LCK_MTX_ASSERT(bpf_mlock
, LCK_MTX_ASSERT_OWNED
);
712 if (d
->bd_refcnt
<= 0)
713 panic("%s: %p refcnt <= 0", __func__
, d
);
717 d
->bd_unref_lr
[d
->bd_next_unref_lr
] = lr_saved
;
718 d
->bd_next_unref_lr
= (d
->bd_next_unref_lr
+ 1) % BPF_REF_HIST
;
720 if (d
->bd_refcnt
== 0) {
721 /* Assert the device is detached */
722 if ((d
->bd_flags
& BPF_DETACHED
) == 0)
723 panic("%s: %p BPF_DETACHED not set", __func__
, d
);
730 * Open ethernet device. Returns ENXIO for illegal minor device number,
731 * EBUSY if file is open by another process.
735 bpfopen(dev_t dev
, int flags
, __unused
int fmt
,
736 __unused
struct proc
*p
)
740 lck_mtx_lock(bpf_mlock
);
741 if ((unsigned int) minor(dev
) >= nbpfilter
) {
742 lck_mtx_unlock(bpf_mlock
);
746 * New device nodes are created on demand when opening the last one.
747 * The programming model is for processes to loop on the minor starting at 0
748 * as long as EBUSY is returned. The loop stops when either the open succeeds or
749 * an error other that EBUSY is returned. That means that bpf_make_dev_t() must
750 * block all processes that are opening the last node. If not all
751 * processes are blocked, they could unexpectedly get ENOENT and abort their
754 if ((unsigned int) minor(dev
) == (nbpfilter
- 1))
755 bpf_make_dev_t(major(dev
));
758 * Each minor can be opened by only one process. If the requested
759 * minor is in use, return EBUSY.
761 * Important: bpfopen() and bpfclose() have to check and set the status of a device
762 * in the same lockin context otherwise the device may be leaked because the vnode use count
763 * will be unpextectly greater than 1 when close() is called.
765 if (bpf_dtab
[minor(dev
)] == 0) {
766 bpf_dtab
[minor(dev
)] = (void *)1; /* Mark opening */
768 lck_mtx_unlock(bpf_mlock
);
771 d
= (struct bpf_d
*)_MALLOC(sizeof(struct bpf_d
), M_DEVBUF
,
774 /* this really is a catastrophic failure */
775 printf("bpfopen: malloc bpf_d failed\n");
776 bpf_dtab
[minor(dev
)] = NULL
;
777 lck_mtx_unlock(bpf_mlock
);
781 /* Mark "in use" and do most initialization. */
783 d
->bd_bufsize
= bpf_bufsize
;
786 d
->bd_oflags
= flags
;
787 d
->bd_state
= BPF_IDLE
;
788 d
->bd_traffic_class
= SO_TC_BE
;
789 d
->bd_flags
|= BPF_DETACHED
;
791 d
->bd_flags
|= BPF_WANT_PKTAP
;
793 d
->bd_flags
&= ~BPF_WANT_PKTAP
;
794 d
->bd_thread_call
= thread_call_allocate(bpf_timed_out
, d
);
795 if (d
->bd_thread_call
== NULL
) {
796 printf("bpfopen: malloc thread call failed\n");
797 bpf_dtab
[minor(dev
)] = NULL
;
799 lck_mtx_unlock(bpf_mlock
);
804 mac_bpfdesc_label_init(d
);
805 mac_bpfdesc_label_associate(kauth_cred_get(), d
);
807 bpf_dtab
[minor(dev
)] = d
; /* Mark opened */
808 lck_mtx_unlock(bpf_mlock
);
814 * Close the descriptor by detaching it from its interface,
815 * deallocating its buffers, and marking it free.
819 bpfclose(dev_t dev
, __unused
int flags
, __unused
int fmt
,
820 __unused
struct proc
*p
)
824 /* Take BPF lock to ensure no other thread is using the device */
825 lck_mtx_lock(bpf_mlock
);
827 d
= bpf_dtab
[minor(dev
)];
828 if (d
== 0 || d
== (void *)1) {
829 lck_mtx_unlock(bpf_mlock
);
834 * Other threads may call bpd_detachd() if we drop the bpf_mlock
836 d
->bd_flags
|= BPF_CLOSING
;
840 __func__
, (uint64_t)VM_KERNEL_ADDRPERM(d
));
842 bpf_dtab
[minor(dev
)] = (void *)1; /* Mark closing */
845 * Deal with any in-progress timeouts.
847 switch (d
->bd_state
) {
850 * Not waiting for a timeout, and no timeout happened.
856 * Waiting for a timeout.
857 * Cancel any timer that has yet to go off,
858 * and mark the state as "closing".
859 * Then drop the lock to allow any timers that
860 * *have* gone off to run to completion, and wait
861 * for them to finish.
863 if (!bpf_stop_timer(d
)) {
865 * There was no pending call, so the call must
866 * have been in progress. Wait for the call to
867 * complete; we have to drop the lock while
868 * waiting. to let the in-progrss call complete
870 d
->bd_state
= BPF_DRAINING
;
871 while (d
->bd_state
== BPF_DRAINING
)
872 msleep((caddr_t
)d
, bpf_mlock
, PRINET
,
873 "bpfdraining", NULL
);
875 d
->bd_state
= BPF_IDLE
;
880 * Timer went off, and the timeout routine finished.
882 d
->bd_state
= BPF_IDLE
;
887 * Another thread is blocked on a close waiting for
888 * a timeout to finish.
889 * This "shouldn't happen", as the first thread to enter
890 * bpfclose() will set bpf_dtab[minor(dev)] to 1, and
891 * all subsequent threads should see that and fail with
894 panic("Two threads blocked in a BPF close");
900 selthreadclear(&d
->bd_sel
);
902 mac_bpfdesc_label_destroy(d
);
904 thread_call_free(d
->bd_thread_call
);
906 while (d
->bd_hbuf_read
)
907 msleep((caddr_t
)d
, bpf_mlock
, PRINET
, "bpf_reading", NULL
);
911 /* Mark free in same context as bpfopen comes to check */
912 bpf_dtab
[minor(dev
)] = NULL
; /* Mark closed */
916 lck_mtx_unlock(bpf_mlock
);
922 #define BPF_SLEEP bpf_sleep
925 bpf_sleep(struct bpf_d
*d
, int pri
, const char *wmesg
, int timo
)
927 u_int64_t abstime
= 0;
930 clock_interval_to_deadline(timo
, NSEC_PER_SEC
/ hz
, &abstime
);
932 return msleep1((caddr_t
)d
, bpf_mlock
, pri
, wmesg
, abstime
);
936 * Rotate the packet buffers in descriptor d. Move the store buffer
937 * into the hold slot, and the free buffer into the store slot.
938 * Zero the length of the new store buffer.
940 #define ROTATE_BUFFERS(d) \
941 if (d->bd_hbuf_read) \
942 panic("rotating bpf buffers during read"); \
943 (d)->bd_hbuf = (d)->bd_sbuf; \
944 (d)->bd_hlen = (d)->bd_slen; \
945 (d)->bd_hcnt = (d)->bd_scnt; \
946 (d)->bd_sbuf = (d)->bd_fbuf; \
951 * bpfread - read next chunk of packets from buffers
954 bpfread(dev_t dev
, struct uio
*uio
, int ioflag
)
958 int timed_out
, hbuf_len
;
962 lck_mtx_lock(bpf_mlock
);
964 d
= bpf_dtab
[minor(dev
)];
965 if (d
== 0 || d
== (void *)1 || (d
->bd_flags
& BPF_CLOSING
) != 0) {
966 lck_mtx_unlock(bpf_mlock
);
973 * Restrict application to use a buffer the same size as
976 if (uio_resid(uio
) != d
->bd_bufsize
) {
978 lck_mtx_unlock(bpf_mlock
);
982 if (d
->bd_state
== BPF_WAITING
)
985 timed_out
= (d
->bd_state
== BPF_TIMED_OUT
);
986 d
->bd_state
= BPF_IDLE
;
988 while (d
->bd_hbuf_read
)
989 msleep((caddr_t
)d
, bpf_mlock
, PRINET
, "bpf_reading", NULL
);
991 if ((d
->bd_flags
& BPF_CLOSING
) != 0) {
993 lck_mtx_unlock(bpf_mlock
);
997 * If the hold buffer is empty, then do a timed sleep, which
998 * ends when the timeout expires or when enough packets
999 * have arrived to fill the store buffer.
1001 while (d
->bd_hbuf
== 0) {
1002 if ((d
->bd_immediate
|| timed_out
|| (ioflag
& IO_NDELAY
))
1003 && d
->bd_slen
!= 0) {
1005 * We're in immediate mode, or are reading
1006 * in non-blocking mode, or a timer was
1007 * started before the read (e.g., by select()
1008 * or poll()) and has expired and a packet(s)
1009 * either arrived since the previous
1010 * read or arrived while we were asleep.
1011 * Rotate the buffers and return what's here.
1018 * No data is available, check to see if the bpf device
1019 * is still pointed at a real interface. If not, return
1020 * ENXIO so that the userland process knows to rebind
1021 * it before using it again.
1023 if (d
->bd_bif
== NULL
) {
1025 lck_mtx_unlock(bpf_mlock
);
1028 if (ioflag
& IO_NDELAY
) {
1030 lck_mtx_unlock(bpf_mlock
);
1031 return (EWOULDBLOCK
);
1033 error
= BPF_SLEEP(d
, PRINET
|PCATCH
, "bpf",
1036 * Make sure device is still opened
1038 if ((d
->bd_flags
& BPF_CLOSING
) != 0) {
1040 lck_mtx_unlock(bpf_mlock
);
1044 while (d
->bd_hbuf_read
)
1045 msleep((caddr_t
)d
, bpf_mlock
, PRINET
, "bpf_reading", NULL
);
1047 if ((d
->bd_flags
& BPF_CLOSING
) != 0) {
1049 lck_mtx_unlock(bpf_mlock
);
1053 if (error
== EINTR
|| error
== ERESTART
) {
1054 if (d
->bd_hbuf
!= NULL
) {
1056 * Because we msleep, the hold buffer might
1057 * be filled when we wake up. Avoid rotating
1062 if (d
->bd_slen
!= 0) {
1064 * Sometimes we may be interrupted often and
1065 * the sleep above will not timeout.
1066 * Regardless, we should rotate the buffers
1067 * if there's any new data pending and
1074 lck_mtx_unlock(bpf_mlock
);
1075 if (error
== ERESTART
) {
1076 printf("%s: %llx ERESTART to EINTR\n",
1077 __func__
, (uint64_t)VM_KERNEL_ADDRPERM(d
));
1082 if (error
== EWOULDBLOCK
) {
1084 * On a timeout, return what's in the buffer,
1085 * which may be nothing. If there is something
1086 * in the store buffer, we can rotate the buffers.
1090 * We filled up the buffer in between
1091 * getting the timeout and arriving
1092 * here, so we don't need to rotate.
1096 if (d
->bd_slen
== 0) {
1098 lck_mtx_unlock(bpf_mlock
);
1106 * At this point, we know we have something in the hold slot.
1110 * Set the hold buffer read. So we do not
1111 * rotate the buffers until the hold buffer
1112 * read is complete. Also to avoid issues resulting
1113 * from page faults during disk sleep (<rdar://problem/13436396>).
1115 d
->bd_hbuf_read
= 1;
1117 hbuf_len
= d
->bd_hlen
;
1118 flags
= d
->bd_flags
;
1119 lck_mtx_unlock(bpf_mlock
);
1123 * Before we move data to userland, we fill out the extended
1126 if (flags
& BPF_EXTENDED_HDR
) {
1130 while (p
< hbuf
+ hbuf_len
) {
1131 struct bpf_hdr_ext
*ehp
;
1133 struct so_procinfo soprocinfo
;
1136 ehp
= (struct bpf_hdr_ext
*)(void *)p
;
1137 if ((flowid
= ehp
->bh_flowid
)) {
1138 if (ehp
->bh_proto
== IPPROTO_TCP
)
1139 found
= inp_findinpcb_procinfo(&tcbinfo
,
1140 flowid
, &soprocinfo
);
1141 else if (ehp
->bh_proto
== IPPROTO_UDP
)
1142 found
= inp_findinpcb_procinfo(&udbinfo
,
1143 flowid
, &soprocinfo
);
1145 ehp
->bh_pid
= soprocinfo
.spi_pid
;
1146 proc_name(ehp
->bh_pid
, ehp
->bh_comm
, MAXCOMLEN
);
1151 if (flags
& BPF_FINALIZE_PKTAP
) {
1152 struct pktap_header
*pktaphdr
;
1154 pktaphdr
= (struct pktap_header
*)(void *)
1155 (p
+ BPF_WORDALIGN(ehp
->bh_hdrlen
));
1157 if (pktaphdr
->pth_flags
& PTH_FLAG_DELAY_PKTAP
)
1158 pktap_finalize_proc_info(pktaphdr
);
1160 if (pktaphdr
->pth_flags
& PTH_FLAG_TSTAMP
) {
1161 ehp
->bh_tstamp
.tv_sec
=
1162 pktaphdr
->pth_tstamp
.tv_sec
;
1163 ehp
->bh_tstamp
.tv_usec
=
1164 pktaphdr
->pth_tstamp
.tv_usec
;
1167 p
+= BPF_WORDALIGN(ehp
->bh_hdrlen
+ ehp
->bh_caplen
);
1169 } else if (flags
& BPF_FINALIZE_PKTAP
) {
1173 while (p
< hbuf
+ hbuf_len
) {
1175 struct pktap_header
*pktaphdr
;
1177 hp
= (struct bpf_hdr
*)(void *)p
;
1178 pktaphdr
= (struct pktap_header
*)(void *)
1179 (p
+ BPF_WORDALIGN(hp
->bh_hdrlen
));
1181 if (pktaphdr
->pth_flags
& PTH_FLAG_DELAY_PKTAP
)
1182 pktap_finalize_proc_info(pktaphdr
);
1184 if (pktaphdr
->pth_flags
& PTH_FLAG_TSTAMP
) {
1185 hp
->bh_tstamp
.tv_sec
=
1186 pktaphdr
->pth_tstamp
.tv_sec
;
1187 hp
->bh_tstamp
.tv_usec
=
1188 pktaphdr
->pth_tstamp
.tv_usec
;
1191 p
+= BPF_WORDALIGN(hp
->bh_hdrlen
+ hp
->bh_caplen
);
1197 * Move data from hold buffer into user space.
1198 * We know the entire buffer is transferred since
1199 * we checked above that the read buffer is bpf_bufsize bytes.
1201 error
= UIOMOVE(hbuf
, hbuf_len
, UIO_READ
, uio
);
1203 lck_mtx_lock(bpf_mlock
);
1205 * Make sure device is still opened
1207 if ((d
->bd_flags
& BPF_CLOSING
) != 0) {
1209 lck_mtx_unlock(bpf_mlock
);
1213 d
->bd_hbuf_read
= 0;
1214 d
->bd_fbuf
= d
->bd_hbuf
;
1221 lck_mtx_unlock(bpf_mlock
);
1228 * If there are processes sleeping on this descriptor, wake them up.
1231 bpf_wakeup(struct bpf_d
*d
)
1233 if (d
->bd_state
== BPF_WAITING
) {
1235 d
->bd_state
= BPF_IDLE
;
1238 if (d
->bd_async
&& d
->bd_sig
&& d
->bd_sigio
)
1239 pgsigio(d
->bd_sigio
, d
->bd_sig
);
1241 selwakeup(&d
->bd_sel
);
1242 if ((d
->bd_flags
& BPF_KNOTE
))
1243 KNOTE(&d
->bd_sel
.si_note
, 1);
1248 bpf_timed_out(void *arg
, __unused
void *dummy
)
1250 struct bpf_d
*d
= (struct bpf_d
*)arg
;
1252 lck_mtx_lock(bpf_mlock
);
1253 if (d
->bd_state
== BPF_WAITING
) {
1255 * There's a select or kqueue waiting for this; if there's
1256 * now stuff to read, wake it up.
1258 d
->bd_state
= BPF_TIMED_OUT
;
1259 if (d
->bd_slen
!= 0)
1261 } else if (d
->bd_state
== BPF_DRAINING
) {
1263 * A close is waiting for this to finish.
1264 * Mark it as finished, and wake the close up.
1266 d
->bd_state
= BPF_IDLE
;
1269 lck_mtx_unlock(bpf_mlock
);
1276 /* keep in sync with bpf_movein above: */
1277 #define MAX_DATALINK_HDR_LEN (sizeof(struct firewire_header))
1280 bpfwrite(dev_t dev
, struct uio
*uio
, __unused
int ioflag
)
1284 struct mbuf
*m
= NULL
;
1286 char dst_buf
[SOCKADDR_HDR_LEN
+ MAX_DATALINK_HDR_LEN
];
1291 lck_mtx_lock(bpf_mlock
);
1293 d
= bpf_dtab
[minor(dev
)];
1294 if (d
== 0 || d
== (void *)1 || (d
->bd_flags
& BPF_CLOSING
) != 0) {
1295 lck_mtx_unlock(bpf_mlock
);
1301 if (d
->bd_bif
== 0) {
1303 lck_mtx_unlock(bpf_mlock
);
1307 ifp
= d
->bd_bif
->bif_ifp
;
1309 if ((ifp
->if_flags
& IFF_UP
) == 0) {
1311 lck_mtx_unlock(bpf_mlock
);
1314 if (uio_resid(uio
) == 0) {
1316 lck_mtx_unlock(bpf_mlock
);
1319 ((struct sockaddr
*)dst_buf
)->sa_len
= sizeof(dst_buf
);
1322 * fix for PR-6849527
1323 * geting variables onto stack before dropping lock for bpf_movein()
1325 bif_dlt
= (int)d
->bd_bif
->bif_dlt
;
1326 bd_hdrcmplt
= d
->bd_hdrcmplt
;
1328 /* bpf_movein allocating mbufs; drop lock */
1329 lck_mtx_unlock(bpf_mlock
);
1331 error
= bpf_movein(uio
, bif_dlt
, &m
,
1332 bd_hdrcmplt
? NULL
: (struct sockaddr
*)dst_buf
,
1335 /* take the lock again */
1336 lck_mtx_lock(bpf_mlock
);
1339 lck_mtx_unlock(bpf_mlock
);
1343 /* verify the device is still open */
1344 if ((d
->bd_flags
& BPF_CLOSING
) != 0) {
1346 lck_mtx_unlock(bpf_mlock
);
1351 if (d
->bd_bif
== NULL
) {
1353 lck_mtx_unlock(bpf_mlock
);
1358 if ((unsigned)datlen
> ifp
->if_mtu
) {
1360 lck_mtx_unlock(bpf_mlock
);
1367 mac_mbuf_label_associate_bpfdesc(d
, m
);
1370 bpf_set_packet_service_class(m
, d
->bd_traffic_class
);
1372 lck_mtx_unlock(bpf_mlock
);
1375 * The driver frees the mbuf.
1377 if (d
->bd_hdrcmplt
) {
1378 if (d
->bd_bif
->bif_send
)
1379 error
= d
->bd_bif
->bif_send(ifp
, d
->bd_bif
->bif_dlt
, m
);
1381 error
= dlil_output(ifp
, 0, m
, NULL
, NULL
, 1, NULL
);
1383 error
= dlil_output(ifp
, PF_INET
, m
, NULL
,
1384 (struct sockaddr
*)dst_buf
, 0, NULL
);
1387 lck_mtx_lock(bpf_mlock
);
1389 lck_mtx_unlock(bpf_mlock
);
1395 * Reset a descriptor by flushing its packet buffer and clearing the
1396 * receive and drop counts.
1399 reset_d(struct bpf_d
*d
)
1401 if (d
->bd_hbuf_read
)
1402 panic("resetting buffers during read");
1405 /* Free the hold buffer. */
1406 d
->bd_fbuf
= d
->bd_hbuf
;
1418 * FIONREAD Check for read packet available.
1419 * SIOCGIFADDR Get interface address - convenient hook to driver.
1420 * BIOCGBLEN Get buffer len [for read()].
1421 * BIOCSETF Set ethernet read filter.
1422 * BIOCFLUSH Flush read packet buffer.
1423 * BIOCPROMISC Put interface into promiscuous mode.
1424 * BIOCGDLT Get link layer type.
1425 * BIOCGETIF Get interface name.
1426 * BIOCSETIF Set interface.
1427 * BIOCSRTIMEOUT Set read timeout.
1428 * BIOCGRTIMEOUT Get read timeout.
1429 * BIOCGSTATS Get packet stats.
1430 * BIOCIMMEDIATE Set immediate mode.
1431 * BIOCVERSION Get filter language version.
1432 * BIOCGHDRCMPLT Get "header already complete" flag
1433 * BIOCSHDRCMPLT Set "header already complete" flag
1434 * BIOCGSEESENT Get "see packets sent" flag
1435 * BIOCSSEESENT Set "see packets sent" flag
1436 * BIOCSETTC Set traffic class.
1437 * BIOCGETTC Get traffic class.
1438 * BIOCSEXTHDR Set "extended header" flag
1439 * BIOCSHEADDROP Drop head of the buffer if user is not reading
1440 * BIOCGHEADDROP Get "head-drop" flag
1444 bpfioctl(dev_t dev
, u_long cmd
, caddr_t addr
, __unused
int flags
,
1452 lck_mtx_lock(bpf_mlock
);
1454 d
= bpf_dtab
[minor(dev
)];
1455 if (d
== 0 || d
== (void *)1 || (d
->bd_flags
& BPF_CLOSING
) != 0) {
1456 lck_mtx_unlock(bpf_mlock
);
1462 if (d
->bd_state
== BPF_WAITING
)
1464 d
->bd_state
= BPF_IDLE
;
1473 * Check for read packet available.
1475 case FIONREAD
: /* int */
1480 if (d
->bd_hbuf
&& d
->bd_hbuf_read
== 0)
1483 bcopy(&n
, addr
, sizeof (n
));
1487 case SIOCGIFADDR
: /* struct ifreq */
1494 ifp
= d
->bd_bif
->bif_ifp
;
1495 error
= ifnet_ioctl(ifp
, 0, cmd
, addr
);
1501 * Get buffer len [for read()].
1503 case BIOCGBLEN
: /* u_int */
1504 bcopy(&d
->bd_bufsize
, addr
, sizeof (u_int
));
1508 * Set buffer length.
1510 case BIOCSBLEN
: /* u_int */
1516 bcopy(addr
, &size
, sizeof (size
));
1519 * Allow larger buffer in head drop mode with the
1520 * assumption the capture is in standby mode to
1521 * keep a cache of recent traffic
1523 if (d
->bd_headdrop
!= 0 && size
> 2 * bpf_maxbufsize
)
1524 size
= 2 * bpf_maxbufsize
;
1525 else if (size
> bpf_maxbufsize
)
1526 size
= bpf_maxbufsize
;
1527 else if (size
< BPF_MINBUFSIZE
)
1528 size
= BPF_MINBUFSIZE
;
1529 bcopy(&size
, addr
, sizeof (size
));
1530 d
->bd_bufsize
= size
;
1535 * Set link layer read filter.
1538 case BIOCSETFNR32
: { /* struct bpf_program32 */
1539 struct bpf_program32 prg32
;
1541 bcopy(addr
, &prg32
, sizeof (prg32
));
1542 error
= bpf_setf(d
, prg32
.bf_len
,
1543 CAST_USER_ADDR_T(prg32
.bf_insns
), cmd
);
1548 case BIOCSETFNR64
: { /* struct bpf_program64 */
1549 struct bpf_program64 prg64
;
1551 bcopy(addr
, &prg64
, sizeof (prg64
));
1552 error
= bpf_setf(d
, prg64
.bf_len
, prg64
.bf_insns
, cmd
);
1557 * Flush read packet buffer.
1560 while (d
->bd_hbuf_read
) {
1561 msleep((caddr_t
)d
, bpf_mlock
, PRINET
, "bpf_reading", NULL
);
1563 if ((d
->bd_flags
& BPF_CLOSING
) != 0) {
1571 * Put interface into promiscuous mode.
1574 if (d
->bd_bif
== 0) {
1576 * No interface attached yet.
1581 if (d
->bd_promisc
== 0) {
1582 lck_mtx_unlock(bpf_mlock
);
1583 error
= ifnet_set_promiscuous(d
->bd_bif
->bif_ifp
, 1);
1584 lck_mtx_lock(bpf_mlock
);
1591 * Get device parameters.
1593 case BIOCGDLT
: /* u_int */
1597 bcopy(&d
->bd_bif
->bif_dlt
, addr
, sizeof (u_int
));
1601 * Get a list of supported data link types.
1603 case BIOCGDLTLIST
: /* struct bpf_dltlist */
1604 if (d
->bd_bif
== NULL
) {
1607 error
= bpf_getdltlist(d
, addr
, p
);
1612 * Set data link type.
1614 case BIOCSDLT
: /* u_int */
1615 if (d
->bd_bif
== NULL
) {
1620 bcopy(addr
, &dlt
, sizeof (dlt
));
1622 if (dlt
== DLT_PKTAP
&& !(d
->bd_flags
& BPF_WANT_PKTAP
)) {
1623 printf("BIOCSDLT downgrade DLT_PKTAP to DLT_RAW\n");
1626 error
= bpf_setdlt(d
, dlt
);
1631 * Get interface name.
1633 case BIOCGETIF
: /* struct ifreq */
1637 struct ifnet
*const ifp
= d
->bd_bif
->bif_ifp
;
1639 snprintf(((struct ifreq
*)(void *)addr
)->ifr_name
,
1640 sizeof (ifr
.ifr_name
), "%s", if_name(ifp
));
1647 case BIOCSETIF
: { /* struct ifreq */
1650 bcopy(addr
, &ifr
, sizeof (ifr
));
1651 ifr
.ifr_name
[IFNAMSIZ
- 1] = '\0';
1652 ifp
= ifunit(ifr
.ifr_name
);
1656 error
= bpf_setif(d
, ifp
);
1663 case BIOCSRTIMEOUT32
: { /* struct user32_timeval */
1664 struct user32_timeval _tv
;
1667 bcopy(addr
, &_tv
, sizeof (_tv
));
1668 tv
.tv_sec
= _tv
.tv_sec
;
1669 tv
.tv_usec
= _tv
.tv_usec
;
1672 * Subtract 1 tick from tvtohz() since this isn't
1675 if ((error
= itimerfix(&tv
)) == 0)
1676 d
->bd_rtout
= tvtohz(&tv
) - 1;
1680 case BIOCSRTIMEOUT64
: { /* struct user64_timeval */
1681 struct user64_timeval _tv
;
1684 bcopy(addr
, &_tv
, sizeof (_tv
));
1685 tv
.tv_sec
= _tv
.tv_sec
;
1686 tv
.tv_usec
= _tv
.tv_usec
;
1689 * Subtract 1 tick from tvtohz() since this isn't
1692 if ((error
= itimerfix(&tv
)) == 0)
1693 d
->bd_rtout
= tvtohz(&tv
) - 1;
1700 case BIOCGRTIMEOUT32
: { /* struct user32_timeval */
1701 struct user32_timeval tv
;
1703 bzero(&tv
, sizeof (tv
));
1704 tv
.tv_sec
= d
->bd_rtout
/ hz
;
1705 tv
.tv_usec
= (d
->bd_rtout
% hz
) * tick
;
1706 bcopy(&tv
, addr
, sizeof (tv
));
1710 case BIOCGRTIMEOUT64
: { /* struct user64_timeval */
1711 struct user64_timeval tv
;
1713 bzero(&tv
, sizeof (tv
));
1714 tv
.tv_sec
= d
->bd_rtout
/ hz
;
1715 tv
.tv_usec
= (d
->bd_rtout
% hz
) * tick
;
1716 bcopy(&tv
, addr
, sizeof (tv
));
1723 case BIOCGSTATS
: { /* struct bpf_stat */
1726 bzero(&bs
, sizeof (bs
));
1727 bs
.bs_recv
= d
->bd_rcount
;
1728 bs
.bs_drop
= d
->bd_dcount
;
1729 bcopy(&bs
, addr
, sizeof (bs
));
1734 * Set immediate mode.
1736 case BIOCIMMEDIATE
: /* u_int */
1737 d
->bd_immediate
= *(u_int
*)(void *)addr
;
1740 case BIOCVERSION
: { /* struct bpf_version */
1741 struct bpf_version bv
;
1743 bzero(&bv
, sizeof (bv
));
1744 bv
.bv_major
= BPF_MAJOR_VERSION
;
1745 bv
.bv_minor
= BPF_MINOR_VERSION
;
1746 bcopy(&bv
, addr
, sizeof (bv
));
1751 * Get "header already complete" flag
1753 case BIOCGHDRCMPLT
: /* u_int */
1754 bcopy(&d
->bd_hdrcmplt
, addr
, sizeof (u_int
));
1758 * Set "header already complete" flag
1760 case BIOCSHDRCMPLT
: /* u_int */
1761 bcopy(addr
, &int_arg
, sizeof (int_arg
));
1762 d
->bd_hdrcmplt
= int_arg
? 1 : 0;
1766 * Get "see sent packets" flag
1768 case BIOCGSEESENT
: /* u_int */
1769 bcopy(&d
->bd_seesent
, addr
, sizeof (u_int
));
1773 * Set "see sent packets" flag
1775 case BIOCSSEESENT
: /* u_int */
1776 bcopy(addr
, &d
->bd_seesent
, sizeof (u_int
));
1780 * Set traffic service class
1782 case BIOCSETTC
: { /* int */
1785 bcopy(addr
, &tc
, sizeof (int));
1786 error
= bpf_set_traffic_class(d
, tc
);
1791 * Get traffic service class
1793 case BIOCGETTC
: /* int */
1794 bcopy(&d
->bd_traffic_class
, addr
, sizeof (int));
1797 case FIONBIO
: /* Non-blocking I/O; int */
1800 case FIOASYNC
: /* Send signal on receive packets; int */
1801 bcopy(addr
, &d
->bd_async
, sizeof (int));
1805 error
= fsetown(*(int *)addr
, &d
->bd_sigio
);
1809 *(int *)addr
= fgetown(d
->bd_sigio
);
1812 /* This is deprecated, FIOSETOWN should be used instead. */
1814 error
= fsetown(-(*(int *)addr
), &d
->bd_sigio
);
1817 /* This is deprecated, FIOGETOWN should be used instead. */
1819 *(int *)addr
= -fgetown(d
->bd_sigio
);
1822 case BIOCSRSIG
: { /* Set receive signal; u_int */
1825 bcopy(addr
, &sig
, sizeof (u_int
));
1833 case BIOCGRSIG
: /* u_int */
1834 bcopy(&d
->bd_sig
, addr
, sizeof (u_int
));
1837 case BIOCSEXTHDR
: /* u_int */
1838 bcopy(addr
, &int_arg
, sizeof (int_arg
));
1840 d
->bd_flags
|= BPF_EXTENDED_HDR
;
1842 d
->bd_flags
&= ~BPF_EXTENDED_HDR
;
1845 case BIOCGIFATTACHCOUNT
: { /* struct ifreq */
1849 bcopy(addr
, &ifr
, sizeof (ifr
));
1850 ifr
.ifr_name
[IFNAMSIZ
- 1] = '\0';
1851 ifp
= ifunit(ifr
.ifr_name
);
1857 for (bp
= bpf_iflist
; bp
!= 0; bp
= bp
->bif_next
) {
1858 struct bpf_d
*bpf_d
;
1860 if (bp
->bif_ifp
== NULL
|| bp
->bif_ifp
!= ifp
)
1862 for (bpf_d
= bp
->bif_dlist
; bpf_d
; bpf_d
= bpf_d
->bd_next
) {
1863 ifr
.ifr_intval
+= 1;
1866 bcopy(&ifr
, addr
, sizeof (ifr
));
1869 case BIOCGWANTPKTAP
: /* u_int */
1870 int_arg
= d
->bd_flags
& BPF_WANT_PKTAP
? 1 : 0;
1871 bcopy(&int_arg
, addr
, sizeof (int_arg
));
1874 case BIOCSWANTPKTAP
: /* u_int */
1875 bcopy(addr
, &int_arg
, sizeof (int_arg
));
1877 d
->bd_flags
|= BPF_WANT_PKTAP
;
1879 d
->bd_flags
&= ~BPF_WANT_PKTAP
;
1884 bcopy(addr
, &int_arg
, sizeof (int_arg
));
1885 d
->bd_headdrop
= int_arg
? 1 : 0;
1889 bcopy(&d
->bd_headdrop
, addr
, sizeof (int));
1894 lck_mtx_unlock(bpf_mlock
);
1900 * Set d's packet filter program to fp. If this file already has a filter,
1901 * free it and replace it. Returns EINVAL for bogus requests.
1904 bpf_setf(struct bpf_d
*d
, u_int bf_len
, user_addr_t bf_insns
,
1907 struct bpf_insn
*fcode
, *old
;
1910 while (d
->bd_hbuf_read
)
1911 msleep((caddr_t
)d
, bpf_mlock
, PRINET
, "bpf_reading", NULL
);
1913 if ((d
->bd_flags
& BPF_CLOSING
) != 0)
1917 if (bf_insns
== USER_ADDR_NULL
) {
1920 d
->bd_filter
= NULL
;
1923 FREE((caddr_t
)old
, M_DEVBUF
);
1927 if (flen
> BPF_MAXINSNS
)
1930 size
= flen
* sizeof(struct bpf_insn
);
1931 fcode
= (struct bpf_insn
*) _MALLOC(size
, M_DEVBUF
, M_WAIT
);
1936 if (copyin(bf_insns
, (caddr_t
)fcode
, size
) == 0 &&
1937 bpf_validate(fcode
, (int)flen
)) {
1938 d
->bd_filter
= fcode
;
1940 if (cmd
== BIOCSETF32
|| cmd
== BIOCSETF64
)
1944 FREE((caddr_t
)old
, M_DEVBUF
);
1948 FREE((caddr_t
)fcode
, M_DEVBUF
);
1953 * Detach a file from its current interface (if attached at all) and attach
1954 * to the interface indicated by the name stored in ifr.
1955 * Return an errno or 0.
1958 bpf_setif(struct bpf_d
*d
, ifnet_t theywant
)
1963 while (d
->bd_hbuf_read
)
1964 msleep((caddr_t
)d
, bpf_mlock
, PRINET
, "bpf_reading", NULL
);
1966 if ((d
->bd_flags
& BPF_CLOSING
) != 0)
1970 * Look through attached interfaces for the named one.
1972 for (bp
= bpf_iflist
; bp
!= 0; bp
= bp
->bif_next
) {
1973 struct ifnet
*ifp
= bp
->bif_ifp
;
1975 if (ifp
== 0 || ifp
!= theywant
)
1978 * Do not use DLT_PKTAP, unless requested explicitly
1980 if (bp
->bif_dlt
== DLT_PKTAP
&& !(d
->bd_flags
& BPF_WANT_PKTAP
))
1983 * Skip the coprocessor interface
1985 if (!intcoproc_unrestricted
&& IFNET_IS_INTCOPROC(ifp
))
1988 * We found the requested interface.
1989 * Allocate the packet buffers.
1991 error
= bpf_allocbufs(d
);
1995 * Detach if attached to something else.
1997 if (bp
!= d
->bd_bif
) {
1998 if (d
->bd_bif
!= NULL
) {
1999 if (bpf_detachd(d
, 0) != 0)
2002 if (bpf_attachd(d
, bp
) != 0)
2015 * Get a list of available data link type of the interface.
2018 bpf_getdltlist(struct bpf_d
*d
, caddr_t addr
, struct proc
*p
)
2025 struct bpf_dltlist bfl
;
2027 bcopy(addr
, &bfl
, sizeof (bfl
));
2028 if (proc_is64bit(p
)) {
2029 dlist
= (user_addr_t
)bfl
.bfl_u
.bflu_pad
;
2031 dlist
= CAST_USER_ADDR_T(bfl
.bfl_u
.bflu_list
);
2034 ifp
= d
->bd_bif
->bif_ifp
;
2038 for (bp
= bpf_iflist
; bp
; bp
= bp
->bif_next
) {
2039 if (bp
->bif_ifp
!= ifp
)
2042 * Do not use DLT_PKTAP, unless requested explicitly
2044 if (bp
->bif_dlt
== DLT_PKTAP
&& !(d
->bd_flags
& BPF_WANT_PKTAP
))
2046 if (dlist
!= USER_ADDR_NULL
) {
2047 if (n
>= bfl
.bfl_len
) {
2050 error
= copyout(&bp
->bif_dlt
, dlist
,
2051 sizeof (bp
->bif_dlt
));
2054 dlist
+= sizeof (bp
->bif_dlt
);
2059 bcopy(&bfl
, addr
, sizeof (bfl
));
2065 * Set the data link type of a BPF instance.
2068 bpf_setdlt(struct bpf_d
*d
, uint32_t dlt
)
2070 int error
, opromisc
;
2074 if (d
->bd_bif
->bif_dlt
== dlt
)
2077 while (d
->bd_hbuf_read
)
2078 msleep((caddr_t
)d
, bpf_mlock
, PRINET
, "bpf_reading", NULL
);
2080 if ((d
->bd_flags
& BPF_CLOSING
) != 0)
2083 ifp
= d
->bd_bif
->bif_ifp
;
2084 for (bp
= bpf_iflist
; bp
; bp
= bp
->bif_next
) {
2085 if (bp
->bif_ifp
== ifp
&& bp
->bif_dlt
== dlt
) {
2087 * Do not use DLT_PKTAP, unless requested explicitly
2089 if (bp
->bif_dlt
== DLT_PKTAP
&& !(d
->bd_flags
& BPF_WANT_PKTAP
)) {
2096 opromisc
= d
->bd_promisc
;
2097 if (bpf_detachd(d
, 0) != 0)
2099 error
= bpf_attachd(d
, bp
);
2101 printf("bpf_setdlt: bpf_attachd %s%d failed (%d)\n",
2102 ifnet_name(bp
->bif_ifp
), ifnet_unit(bp
->bif_ifp
), error
);
2107 lck_mtx_unlock(bpf_mlock
);
2108 error
= ifnet_set_promiscuous(bp
->bif_ifp
, 1);
2109 lck_mtx_lock(bpf_mlock
);
2111 printf("%s: ifpromisc %s%d failed (%d)\n",
2112 __func__
, ifnet_name(bp
->bif_ifp
),
2113 ifnet_unit(bp
->bif_ifp
), error
);
2119 return (bp
== NULL
? EINVAL
: 0);
2123 bpf_set_traffic_class(struct bpf_d
*d
, int tc
)
2127 if (!SO_VALID_TC(tc
))
2130 d
->bd_traffic_class
= tc
;
2136 bpf_set_packet_service_class(struct mbuf
*m
, int tc
)
2138 if (!(m
->m_flags
& M_PKTHDR
))
2141 VERIFY(SO_VALID_TC(tc
));
2142 (void) m_set_service_class(m
, so_tc2msc(tc
));
2146 * Support for select()
2148 * Return true iff the specific operation will not block indefinitely.
2149 * Otherwise, return false but make a note that a selwakeup() must be done.
2152 bpfselect(dev_t dev
, int which
, void * wql
, struct proc
*p
)
2157 lck_mtx_lock(bpf_mlock
);
2159 d
= bpf_dtab
[minor(dev
)];
2160 if (d
== 0 || d
== (void *)1 || (d
->bd_flags
& BPF_CLOSING
) != 0) {
2161 lck_mtx_unlock(bpf_mlock
);
2167 if (d
->bd_bif
== NULL
) {
2169 lck_mtx_unlock(bpf_mlock
);
2173 while (d
->bd_hbuf_read
)
2174 msleep((caddr_t
)d
, bpf_mlock
, PRINET
, "bpf_reading", NULL
);
2176 if ((d
->bd_flags
& BPF_CLOSING
) != 0) {
2178 lck_mtx_unlock(bpf_mlock
);
2184 if (d
->bd_hlen
!= 0 ||
2185 ((d
->bd_immediate
|| d
->bd_state
== BPF_TIMED_OUT
) &&
2187 ret
= 1; /* read has data to return */
2190 * Read has no data to return.
2191 * Make the select wait, and start a timer if
2194 selrecord(p
, &d
->bd_sel
, wql
);
2200 ret
= 1; /* can't determine whether a write would block */
2205 lck_mtx_unlock(bpf_mlock
);
2212 * Support for kevent() system call. Register EVFILT_READ filters and
2213 * reject all others.
2215 int bpfkqfilter(dev_t dev
, struct knote
*kn
);
2216 static void filt_bpfdetach(struct knote
*);
2217 static int filt_bpfread(struct knote
*, long);
2218 static int filt_bpftouch(struct knote
*kn
, struct kevent_internal_s
*kev
);
2219 static int filt_bpfprocess(struct knote
*kn
, struct filt_process_s
*data
, struct kevent_internal_s
*kev
);
2221 SECURITY_READ_ONLY_EARLY(struct filterops
) bpfread_filtops
= {
2223 .f_detach
= filt_bpfdetach
,
2224 .f_event
= filt_bpfread
,
2225 .f_touch
= filt_bpftouch
,
2226 .f_process
= filt_bpfprocess
,
2230 filt_bpfread_common(struct knote
*kn
, struct bpf_d
*d
)
2234 if (d
->bd_immediate
) {
2236 * If there's data in the hold buffer, it's the
2237 * amount of data a read will return.
2239 * If there's no data in the hold buffer, but
2240 * there's data in the store buffer, a read will
2241 * immediately rotate the store buffer to the
2242 * hold buffer, the amount of data in the store
2243 * buffer is the amount of data a read will
2246 * If there's no data in either buffer, we're not
2249 kn
->kn_data
= ((d
->bd_hlen
== 0 || d
->bd_hbuf_read
)
2250 ? d
->bd_slen
: d
->bd_hlen
);
2252 if (kn
->kn_sfflags
& NOTE_LOWAT
)
2254 if (kn
->kn_sdata
> d
->bd_bufsize
)
2255 lowwat
= d
->bd_bufsize
;
2256 else if (kn
->kn_sdata
> lowwat
)
2257 lowwat
= kn
->kn_sdata
;
2259 ready
= (kn
->kn_data
>= lowwat
);
2262 * If there's data in the hold buffer, it's the
2263 * amount of data a read will return.
2265 * If there's no data in the hold buffer, but
2266 * there's data in the store buffer, if the
2267 * timer has expired a read will immediately
2268 * rotate the store buffer to the hold buffer,
2269 * so the amount of data in the store buffer is
2270 * the amount of data a read will return.
2272 * If there's no data in either buffer, or there's
2273 * no data in the hold buffer and the timer hasn't
2274 * expired, we're not ready to read.
2276 kn
->kn_data
= ((d
->bd_hlen
== 0 || d
->bd_hbuf_read
) && d
->bd_state
== BPF_TIMED_OUT
?
2277 d
->bd_slen
: d
->bd_hlen
);
2278 ready
= (kn
->kn_data
> 0);
2287 bpfkqfilter(dev_t dev
, struct knote
*kn
)
2293 * Is this device a bpf?
2295 if (major(dev
) != CDEV_MAJOR
||
2296 kn
->kn_filter
!= EVFILT_READ
) {
2297 kn
->kn_flags
= EV_ERROR
;
2298 kn
->kn_data
= EINVAL
;
2302 lck_mtx_lock(bpf_mlock
);
2304 d
= bpf_dtab
[minor(dev
)];
2308 d
->bd_bif
== NULL
||
2309 (d
->bd_flags
& BPF_CLOSING
) != 0) {
2310 lck_mtx_unlock(bpf_mlock
);
2311 kn
->kn_flags
= EV_ERROR
;
2312 kn
->kn_data
= ENXIO
;
2317 kn
->kn_filtid
= EVFILTID_BPFREAD
;
2318 KNOTE_ATTACH(&d
->bd_sel
.si_note
, kn
);
2319 d
->bd_flags
|= BPF_KNOTE
;
2321 /* capture the current state */
2322 res
= filt_bpfread_common(kn
, d
);
2324 lck_mtx_unlock(bpf_mlock
);
2330 filt_bpfdetach(struct knote
*kn
)
2332 struct bpf_d
*d
= (struct bpf_d
*)kn
->kn_hook
;
2334 lck_mtx_lock(bpf_mlock
);
2335 if (d
->bd_flags
& BPF_KNOTE
) {
2336 KNOTE_DETACH(&d
->bd_sel
.si_note
, kn
);
2337 d
->bd_flags
&= ~BPF_KNOTE
;
2339 lck_mtx_unlock(bpf_mlock
);
2343 filt_bpfread(struct knote
*kn
, long hint
)
2345 #pragma unused(hint)
2346 struct bpf_d
*d
= (struct bpf_d
*)kn
->kn_hook
;
2348 return filt_bpfread_common(kn
, d
);
2352 filt_bpftouch(struct knote
*kn
, struct kevent_internal_s
*kev
)
2354 struct bpf_d
*d
= (struct bpf_d
*)kn
->kn_hook
;
2357 lck_mtx_lock(bpf_mlock
);
2359 /* save off the lowat threshold and flag */
2360 kn
->kn_sdata
= kev
->data
;
2361 kn
->kn_sfflags
= kev
->fflags
;
2362 if ((kn
->kn_status
& KN_UDATA_SPECIFIC
) == 0)
2363 kn
->kn_udata
= kev
->udata
;
2365 /* output data will be re-generated here */
2366 res
= filt_bpfread_common(kn
, d
);
2368 lck_mtx_unlock(bpf_mlock
);
2374 filt_bpfprocess(struct knote
*kn
, struct filt_process_s
*data
, struct kevent_internal_s
*kev
)
2376 #pragma unused(data)
2377 struct bpf_d
*d
= (struct bpf_d
*)kn
->kn_hook
;
2380 lck_mtx_lock(bpf_mlock
);
2381 res
= filt_bpfread_common(kn
, d
);
2383 *kev
= kn
->kn_kevent
;
2385 lck_mtx_unlock(bpf_mlock
);
2391 * Copy data from an mbuf chain into a buffer. This code is derived
2392 * from m_copydata in kern/uipc_mbuf.c.
2395 bpf_mcopy(struct mbuf
* m
, void *dst_arg
, size_t len
)
2404 count
= min(m
->m_len
, len
);
2405 bcopy(mbuf_data(m
), dst
, count
);
2416 struct bpf_packet
*bpf_pkt
,
2424 * It's possible that we get here after the bpf descriptor has been
2425 * detached from the interface; in such a case we simply return.
2426 * Lock ordering is important since we can be called asynchronously
2427 * (from IOKit) to process an inbound packet; when that happens
2428 * we would have been holding its "gateLock" and will be acquiring
2429 * "bpf_mlock" upon entering this routine. Due to that, we release
2430 * "bpf_mlock" prior to calling ifnet_set_promiscuous (which will
2431 * acquire "gateLock" in the IOKit), in order to avoid a deadlock
2432 * when a ifnet_set_promiscuous request simultaneously collides with
2433 * an inbound packet being passed into the tap callback.
2435 lck_mtx_lock(bpf_mlock
);
2436 if (ifp
->if_bpf
== NULL
) {
2437 lck_mtx_unlock(bpf_mlock
);
2440 for (bp
= ifp
->if_bpf
; bp
!= NULL
; bp
= bp
->bif_next
) {
2441 if (bp
->bif_ifp
!= ifp
) {
2442 /* wrong interface */
2446 if (dlt
== 0 || bp
->bif_dlt
== dlt
) {
2447 /* tapping default DLT or DLT matches */
2454 for (d
= bp
->bif_dlist
; d
; d
= d
->bd_next
) {
2455 if (outbound
&& !d
->bd_seesent
)
2458 slen
= bpf_filter(d
->bd_filter
, (u_char
*)bpf_pkt
,
2459 bpf_pkt
->bpfp_total_length
, 0);
2462 if (mac_bpfdesc_check_receive(d
, bp
->bif_ifp
) != 0)
2465 catchpacket(d
, bpf_pkt
, slen
, outbound
);
2470 lck_mtx_unlock(bpf_mlock
);
2482 struct bpf_packet bpf_pkt
;
2485 if (ifp
->if_bpf
== NULL
) {
2486 /* quickly check without taking lock */
2489 bpf_pkt
.bpfp_type
= BPF_PACKET_TYPE_MBUF
;
2490 bpf_pkt
.bpfp_mbuf
= m
;
2491 bpf_pkt
.bpfp_total_length
= 0;
2492 for (m0
= m
; m0
!= NULL
; m0
= m0
->m_next
)
2493 bpf_pkt
.bpfp_total_length
+= m0
->m_len
;
2494 bpf_pkt
.bpfp_header
= hdr
;
2496 bpf_pkt
.bpfp_total_length
+= hlen
;
2497 bpf_pkt
.bpfp_header_length
= hlen
;
2499 bpf_pkt
.bpfp_header_length
= 0;
2501 bpf_tap_imp(ifp
, dlt
, &bpf_pkt
, outbound
);
2512 bpf_tap_mbuf(ifp
, dlt
, m
, hdr
, hlen
, 1);
2523 bpf_tap_mbuf(ifp
, dlt
, m
, hdr
, hlen
, 0);
2526 /* Callback registered with Ethernet driver. */
2527 static int bpf_tap_callback(struct ifnet
*ifp
, struct mbuf
*m
)
2529 bpf_tap_mbuf(ifp
, 0, m
, NULL
, 0, mbuf_pkthdr_rcvif(m
) == NULL
);
2536 copy_bpf_packet(struct bpf_packet
* pkt
, void * dst
, size_t len
)
2538 /* copy the optional header */
2539 if (pkt
->bpfp_header_length
!= 0) {
2540 size_t count
= min(len
, pkt
->bpfp_header_length
);
2541 bcopy(pkt
->bpfp_header
, dst
, count
);
2546 /* nothing past the header */
2549 /* copy the packet */
2550 switch (pkt
->bpfp_type
) {
2551 case BPF_PACKET_TYPE_MBUF
:
2552 bpf_mcopy(pkt
->bpfp_mbuf
, dst
, len
);
2560 * Move the packet data from interface memory (pkt) into the
2561 * store buffer. Return 1 if it's time to wakeup a listener (buffer full),
2565 catchpacket(struct bpf_d
*d
, struct bpf_packet
* pkt
,
2566 u_int snaplen
, int outbound
)
2569 struct bpf_hdr_ext
*ehp
;
2576 hdrlen
= (d
->bd_flags
& BPF_EXTENDED_HDR
) ? d
->bd_bif
->bif_exthdrlen
:
2577 d
->bd_bif
->bif_hdrlen
;
2579 * Figure out how many bytes to move. If the packet is
2580 * greater or equal to the snapshot length, transfer that
2581 * much. Otherwise, transfer the whole packet (unless
2582 * we hit the buffer size limit).
2584 totlen
= hdrlen
+ min(snaplen
, pkt
->bpfp_total_length
);
2585 if (totlen
> d
->bd_bufsize
)
2586 totlen
= d
->bd_bufsize
;
2589 * Round up the end of the previous packet to the next longword.
2591 curlen
= BPF_WORDALIGN(d
->bd_slen
);
2592 if (curlen
+ totlen
> d
->bd_bufsize
) {
2594 * This packet will overflow the storage buffer.
2595 * Rotate the buffers if we can, then wakeup any
2598 * We cannot rotate buffers if a read is in progress
2599 * so drop the packet
2601 if (d
->bd_hbuf_read
) {
2606 if (d
->bd_fbuf
== NULL
) {
2607 if (d
->bd_headdrop
== 0) {
2609 * We haven't completed the previous read yet,
2610 * so drop the packet.
2616 * Drop the hold buffer as it contains older packets
2618 d
->bd_dcount
+= d
->bd_hcnt
;
2619 d
->bd_fbuf
= d
->bd_hbuf
;
2627 else if (d
->bd_immediate
|| d
->bd_state
== BPF_TIMED_OUT
)
2629 * Immediate mode is set, or the read timeout has
2630 * already expired during a select call. A packet
2631 * arrived, so the reader should be woken up.
2636 * Append the bpf header.
2639 if (d
->bd_flags
& BPF_EXTENDED_HDR
) {
2642 m
= (pkt
->bpfp_type
== BPF_PACKET_TYPE_MBUF
)
2643 ? pkt
->bpfp_mbuf
: NULL
;
2644 ehp
= (struct bpf_hdr_ext
*)(void *)(d
->bd_sbuf
+ curlen
);
2645 memset(ehp
, 0, sizeof(*ehp
));
2646 ehp
->bh_tstamp
.tv_sec
= tv
.tv_sec
;
2647 ehp
->bh_tstamp
.tv_usec
= tv
.tv_usec
;
2649 ehp
->bh_datalen
= pkt
->bpfp_total_length
;
2650 ehp
->bh_hdrlen
= hdrlen
;
2651 caplen
= ehp
->bh_caplen
= totlen
- hdrlen
;
2654 ehp
->bh_flags
|= BPF_HDR_EXT_FLAGS_DIR_OUT
;
2656 ehp
->bh_flags
|= BPF_HDR_EXT_FLAGS_DIR_IN
;
2658 } else if (outbound
) {
2659 ehp
->bh_flags
|= BPF_HDR_EXT_FLAGS_DIR_OUT
;
2661 /* only do lookups on non-raw INPCB */
2662 if ((m
->m_pkthdr
.pkt_flags
& (PKTF_FLOW_ID
|
2663 PKTF_FLOW_LOCALSRC
|PKTF_FLOW_RAWSOCK
)) ==
2664 (PKTF_FLOW_ID
|PKTF_FLOW_LOCALSRC
) &&
2665 m
->m_pkthdr
.pkt_flowsrc
== FLOWSRC_INPCB
) {
2666 ehp
->bh_flowid
= m
->m_pkthdr
.pkt_flowid
;
2667 ehp
->bh_proto
= m
->m_pkthdr
.pkt_proto
;
2669 ehp
->bh_svc
= so_svc2tc(m
->m_pkthdr
.pkt_svc
);
2670 if (m
->m_pkthdr
.pkt_flags
& PKTF_TCP_REXMT
)
2671 ehp
->bh_pktflags
|= BPF_PKTFLAGS_TCP_REXMT
;
2672 if (m
->m_pkthdr
.pkt_flags
& PKTF_START_SEQ
)
2673 ehp
->bh_pktflags
|= BPF_PKTFLAGS_START_SEQ
;
2674 if (m
->m_pkthdr
.pkt_flags
& PKTF_LAST_PKT
)
2675 ehp
->bh_pktflags
|= BPF_PKTFLAGS_LAST_PKT
;
2676 if (m
->m_pkthdr
.pkt_flags
& PKTF_VALID_UNSENT_DATA
) {
2677 ehp
->bh_unsent_bytes
=
2678 m
->m_pkthdr
.bufstatus_if
;
2679 ehp
->bh_unsent_snd
=
2680 m
->m_pkthdr
.bufstatus_sndbuf
;
2683 ehp
->bh_flags
|= BPF_HDR_EXT_FLAGS_DIR_IN
;
2684 payload
= (u_char
*)ehp
+ hdrlen
;
2686 hp
= (struct bpf_hdr
*)(void *)(d
->bd_sbuf
+ curlen
);
2687 hp
->bh_tstamp
.tv_sec
= tv
.tv_sec
;
2688 hp
->bh_tstamp
.tv_usec
= tv
.tv_usec
;
2689 hp
->bh_datalen
= pkt
->bpfp_total_length
;
2690 hp
->bh_hdrlen
= hdrlen
;
2691 caplen
= hp
->bh_caplen
= totlen
- hdrlen
;
2692 payload
= (u_char
*)hp
+ hdrlen
;
2695 * Copy the packet data into the store buffer and update its length.
2697 copy_bpf_packet(pkt
, payload
, caplen
);
2698 d
->bd_slen
= curlen
+ totlen
;
2706 * Initialize all nonzero fields of a descriptor.
2709 bpf_allocbufs(struct bpf_d
*d
)
2711 if (d
->bd_sbuf
!= NULL
) {
2712 FREE(d
->bd_sbuf
, M_DEVBUF
);
2715 if (d
->bd_hbuf
!= NULL
) {
2716 FREE(d
->bd_hbuf
, M_DEVBUF
);
2719 if (d
->bd_fbuf
!= NULL
) {
2720 FREE(d
->bd_fbuf
, M_DEVBUF
);
2724 d
->bd_fbuf
= (caddr_t
) _MALLOC(d
->bd_bufsize
, M_DEVBUF
, M_WAIT
);
2725 if (d
->bd_fbuf
== NULL
)
2728 d
->bd_sbuf
= (caddr_t
) _MALLOC(d
->bd_bufsize
, M_DEVBUF
, M_WAIT
);
2729 if (d
->bd_sbuf
== NULL
) {
2730 FREE(d
->bd_fbuf
, M_DEVBUF
);
2742 * Free buffers currently in use by a descriptor.
2746 bpf_freed(struct bpf_d
*d
)
2749 * We don't need to lock out interrupts since this descriptor has
2750 * been detached from its interface and it yet hasn't been marked
2753 if (d
->bd_hbuf_read
)
2754 panic("bpf buffer freed during read");
2756 if (d
->bd_sbuf
!= 0) {
2757 FREE(d
->bd_sbuf
, M_DEVBUF
);
2758 if (d
->bd_hbuf
!= 0)
2759 FREE(d
->bd_hbuf
, M_DEVBUF
);
2760 if (d
->bd_fbuf
!= 0)
2761 FREE(d
->bd_fbuf
, M_DEVBUF
);
2764 FREE((caddr_t
)d
->bd_filter
, M_DEVBUF
);
2768 * Attach an interface to bpf. driverp is a pointer to a (struct bpf_if *)
2769 * in the driver's softc; dlt is the link layer type; hdrlen is the fixed
2770 * size of the link header (variable length headers not yet supported).
2773 bpfattach(struct ifnet
*ifp
, u_int dlt
, u_int hdrlen
)
2775 bpf_attach(ifp
, dlt
, hdrlen
, NULL
, NULL
);
2787 struct bpf_if
*bp_new
;
2788 struct bpf_if
*bp_before_first
= NULL
;
2789 struct bpf_if
*bp_first
= NULL
;
2790 struct bpf_if
*bp_last
= NULL
;
2793 bp_new
= (struct bpf_if
*) _MALLOC(sizeof(*bp_new
), M_DEVBUF
,
2798 lck_mtx_lock(bpf_mlock
);
2801 * Check if this interface/dlt is already attached. Remember the
2802 * first and last attachment for this interface, as well as the
2803 * element before the first attachment.
2806 for (bp
= bpf_iflist
; bp
!= NULL
; bp
= bp
->bif_next
) {
2807 if (bp
->bif_ifp
!= ifp
) {
2808 if (bp_first
!= NULL
) {
2809 /* no more elements for this interface */
2812 bp_before_first
= bp
;
2814 if (bp
->bif_dlt
== dlt
) {
2818 if (bp_first
== NULL
) {
2825 lck_mtx_unlock(bpf_mlock
);
2826 printf("bpfattach - %s with dlt %d is already attached\n",
2828 FREE(bp_new
, M_DEVBUF
);
2832 bp_new
->bif_ifp
= ifp
;
2833 bp_new
->bif_dlt
= dlt
;
2834 bp_new
->bif_send
= send
;
2835 bp_new
->bif_tap
= tap
;
2837 if (bp_first
== NULL
) {
2838 /* No other entries for this ifp */
2839 bp_new
->bif_next
= bpf_iflist
;
2840 bpf_iflist
= bp_new
;
2843 if (ifnet_type(ifp
) == IFT_ETHER
&& dlt
== DLT_EN10MB
) {
2844 /* Make this the first entry for this interface */
2845 if (bp_before_first
!= NULL
) {
2846 /* point the previous to us */
2847 bp_before_first
->bif_next
= bp_new
;
2849 /* we're the new head */
2850 bpf_iflist
= bp_new
;
2852 bp_new
->bif_next
= bp_first
;
2854 /* Add this after the last entry for this interface */
2855 bp_new
->bif_next
= bp_last
->bif_next
;
2856 bp_last
->bif_next
= bp_new
;
2861 * Compute the length of the bpf header. This is not necessarily
2862 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
2863 * that the network layer header begins on a longword boundary (for
2864 * performance reasons and to alleviate alignment restrictions).
2866 bp_new
->bif_hdrlen
= BPF_WORDALIGN(hdrlen
+ SIZEOF_BPF_HDR
) - hdrlen
;
2867 bp_new
->bif_exthdrlen
= BPF_WORDALIGN(hdrlen
+
2868 sizeof(struct bpf_hdr_ext
)) - hdrlen
;
2870 /* Take a reference on the interface */
2871 ifnet_reference(ifp
);
2873 lck_mtx_unlock(bpf_mlock
);
2877 printf("bpf: %s attached\n", if_name(ifp
));
2884 * Detach bpf from an interface. This involves detaching each descriptor
2885 * associated with the interface, and leaving bd_bif NULL. Notify each
2886 * descriptor as it's detached so that any sleepers wake up and get
2890 bpfdetach(struct ifnet
*ifp
)
2892 struct bpf_if
*bp
, *bp_prev
, *bp_next
;
2896 printf("%s: %s\n", __func__
, if_name(ifp
));
2898 lck_mtx_lock(bpf_mlock
);
2901 * Build the list of devices attached to that interface
2902 * that we need to free while keeping the lock to maintain
2903 * the integrity of the interface list
2906 for (bp
= bpf_iflist
; bp
!= NULL
; bp
= bp_next
) {
2907 bp_next
= bp
->bif_next
;
2909 if (ifp
!= bp
->bif_ifp
) {
2913 /* Unlink from the interface list */
2915 bp_prev
->bif_next
= bp
->bif_next
;
2917 bpf_iflist
= bp
->bif_next
;
2919 /* Detach the devices attached to the interface */
2920 while ((d
= bp
->bif_dlist
) != NULL
) {
2922 * Take an extra reference to prevent the device
2923 * from being freed when bpf_detachd() releases
2924 * the reference for the interface list
2934 lck_mtx_unlock(bpf_mlock
);
2938 bpf_init(__unused
void *unused
)
2944 if (bpf_devsw_installed
== 0) {
2945 bpf_devsw_installed
= 1;
2946 bpf_mlock_grp_attr
= lck_grp_attr_alloc_init();
2947 bpf_mlock_grp
= lck_grp_alloc_init("bpf", bpf_mlock_grp_attr
);
2948 bpf_mlock_attr
= lck_attr_alloc_init();
2949 lck_mtx_init(bpf_mlock
, bpf_mlock_grp
, bpf_mlock_attr
);
2950 maj
= cdevsw_add(CDEV_MAJOR
, &bpf_cdevsw
);
2953 lck_attr_free(bpf_mlock_attr
);
2955 lck_grp_free(bpf_mlock_grp
);
2956 if (bpf_mlock_grp_attr
)
2957 lck_grp_attr_free(bpf_mlock_grp_attr
);
2960 bpf_mlock_attr
= NULL
;
2961 bpf_mlock_grp
= NULL
;
2962 bpf_mlock_grp_attr
= NULL
;
2963 bpf_devsw_installed
= 0;
2964 printf("bpf_init: failed to allocate a major number!\n");
2968 for (i
= 0 ; i
< NBPFILTER
; i
++)
2969 bpf_make_dev_t(maj
);
2972 cdevsw_add(&bpf_cdevsw
);
2977 SYSINIT(bpfdev
,SI_SUB_DRIVERS
,SI_ORDER_MIDDLE
+CDEV_MAJOR
,bpf_drvinit
,NULL
)
2982 mac_bpfdesc_label_get(struct bpf_d
*d
)
2985 return (d
->bd_label
);
2989 mac_bpfdesc_label_set(struct bpf_d
*d
, struct label
*label
)
2992 d
->bd_label
= label
;