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>
131 #include <security/mac_framework.h>
134 extern int tvtohz(struct timeval
*);
136 #define BPF_BUFSIZE 4096
137 #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, uio)
140 #define PRINET 26 /* interruptible */
143 * The default read buffer size is patchable.
145 static unsigned int bpf_bufsize
= BPF_BUFSIZE
;
146 SYSCTL_INT(_debug
, OID_AUTO
, bpf_bufsize
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
147 &bpf_bufsize
, 0, "");
148 __private_extern__
unsigned int bpf_maxbufsize
= BPF_MAXBUFSIZE
;
149 SYSCTL_INT(_debug
, OID_AUTO
, bpf_maxbufsize
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
150 &bpf_maxbufsize
, 0, "");
151 static unsigned int bpf_maxdevices
= 256;
152 SYSCTL_UINT(_debug
, OID_AUTO
, bpf_maxdevices
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
153 &bpf_maxdevices
, 0, "");
155 * bpf_wantpktap controls the defaul visibility of DLT_PKTAP
156 * For OS X is off by default so process need to use the ioctl BPF_WANT_PKTAP
157 * explicitly to be able to use DLT_PKTAP.
159 static unsigned int bpf_wantpktap
= 0;
160 SYSCTL_UINT(_debug
, OID_AUTO
, bpf_wantpktap
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
161 &bpf_wantpktap
, 0, "");
163 static int bpf_debug
= 0;
164 SYSCTL_INT(_debug
, OID_AUTO
, bpf_debug
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
168 * bpf_iflist is the list of interfaces; each corresponds to an ifnet
169 * bpf_dtab holds pointer to the descriptors, indexed by minor device #
171 static struct bpf_if
*bpf_iflist
;
174 * BSD now stores the bpf_d in the dev_t which is a struct
175 * on their system. Our dev_t is an int, so we still store
176 * the bpf_d in a separate table indexed by minor device #.
178 * The value stored in bpf_dtab[n] represent three states:
179 * 0: device not opened
180 * 1: device opening or closing
181 * other: device <n> opened with pointer to storage
183 static struct bpf_d
**bpf_dtab
= NULL
;
184 static unsigned int bpf_dtab_size
= 0;
185 static unsigned int nbpfilter
= 0;
187 decl_lck_mtx_data(static, bpf_mlock_data
);
188 static lck_mtx_t
*bpf_mlock
= &bpf_mlock_data
;
189 static lck_grp_t
*bpf_mlock_grp
;
190 static lck_grp_attr_t
*bpf_mlock_grp_attr
;
191 static lck_attr_t
*bpf_mlock_attr
;
193 static mbuf_tag_id_t bpf_mtag_id
;
194 #endif /* __APPLE__ */
196 static int bpf_allocbufs(struct bpf_d
*);
197 static errno_t
bpf_attachd(struct bpf_d
*d
, struct bpf_if
*bp
);
198 static int bpf_detachd(struct bpf_d
*d
, int);
199 static void bpf_freed(struct bpf_d
*);
200 static void bpf_mcopy(const void *, void *, size_t);
201 static int bpf_movein(struct uio
*, int,
202 struct mbuf
**, struct sockaddr
*, int *);
203 static int bpf_setif(struct bpf_d
*, ifnet_t ifp
, u_int32_t dlt
);
204 static void bpf_timed_out(void *, void *);
205 static void bpf_wakeup(struct bpf_d
*);
206 static void catchpacket(struct bpf_d
*, u_char
*, struct mbuf
*, u_int
,
207 u_int
, int, void (*)(const void *, void *, size_t));
208 static void reset_d(struct bpf_d
*);
209 static int bpf_setf(struct bpf_d
*, u_int
, user_addr_t
, u_long
);
210 static int bpf_getdltlist(struct bpf_d
*, caddr_t
, struct proc
*);
211 static int bpf_setdlt(struct bpf_d
*, u_int
);
212 static int bpf_set_traffic_class(struct bpf_d
*, int);
213 static void bpf_set_packet_service_class(struct mbuf
*, int);
215 static void bpf_acquire_d(struct bpf_d
*);
216 static void bpf_release_d(struct bpf_d
*);
218 static int bpf_devsw_installed
;
220 void bpf_init(void *unused
);
221 static int bpf_tap_callback(struct ifnet
*ifp
, struct mbuf
*m
);
224 * Darwin differs from BSD here, the following are static
225 * on BSD and not static on Darwin.
231 ioctl_fcn_t bpfioctl
;
232 select_fcn_t bpfselect
;
235 /* Darwin's cdevsw struct differs slightly from BSDs */
236 #define CDEV_MAJOR 23
237 static struct cdevsw bpf_cdevsw
= {
239 /* close */ bpfclose
,
241 /* write */ bpfwrite
,
242 /* ioctl */ bpfioctl
,
244 /* reset */ eno_reset
,
246 /* select */ bpfselect
,
248 /* strategy*/ eno_strat
,
254 #define SOCKADDR_HDR_LEN offsetof(struct sockaddr, sa_data)
257 bpf_movein(struct uio
*uio
, int linktype
, struct mbuf
**mp
, struct sockaddr
*sockp
, int *datlen
)
275 sa_family
= AF_UNSPEC
;
276 /* XXX Would MAXLINKHDR be better? */
277 hlen
= sizeof(struct ether_header
);
282 #if defined(__FreeBSD__) || defined(__bsdi__)
283 sa_family
= AF_IMPLINK
;
286 sa_family
= AF_UNSPEC
;
287 /* XXX 4(FORMAC)+6(dst)+6(src)+3(LLC)+5(SNAP) */
295 sa_family
= AF_UNSPEC
;
300 case DLT_ATM_RFC1483
:
302 * en atm driver requires 4-byte atm pseudo header.
303 * though it isn't standard, vpi:vci needs to be
306 sa_family
= AF_UNSPEC
;
307 hlen
= 12; /* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
312 sa_family
= AF_UNSPEC
;
313 hlen
= 4; /* This should match PPP_HDRLEN */
316 case DLT_APPLE_IP_OVER_IEEE1394
:
317 sa_family
= AF_UNSPEC
;
318 hlen
= sizeof(struct firewire_header
);
321 case DLT_IEEE802_11
: /* IEEE 802.11 wireless */
322 sa_family
= AF_IEEE80211
;
326 case DLT_IEEE802_11_RADIO
:
327 sa_family
= AF_IEEE80211
;
335 // LP64todo - fix this!
336 len
= uio_resid(uio
);
337 *datlen
= len
- hlen
;
338 if ((unsigned)len
> MCLBYTES
)
343 * Build a sockaddr based on the data link layer type.
344 * We do this at this level because the ethernet header
345 * is copied directly into the data field of the sockaddr.
346 * In the case of SLIP, there is no header and the packet
347 * is forwarded as is.
348 * Also, we are careful to leave room at the front of the mbuf
349 * for the link level header.
351 if ((hlen
+ SOCKADDR_HDR_LEN
) > sockp
->sa_len
) {
354 sockp
->sa_family
= sa_family
;
357 * We're directly sending the packet data supplied by
358 * the user; we don't need to make room for the link
359 * header, and don't need the header length value any
360 * more, so set it to 0.
365 MGETHDR(m
, M_WAIT
, MT_DATA
);
368 if ((unsigned)len
> MHLEN
) {
370 if ((m
->m_flags
& M_EXT
) == 0) {
375 m
->m_pkthdr
.len
= m
->m_len
= len
;
376 m
->m_pkthdr
.rcvif
= NULL
;
380 * Make room for link header.
383 m
->m_pkthdr
.len
-= hlen
;
385 m
->m_data
+= hlen
; /* XXX */
386 error
= UIOMOVE((caddr_t
)sockp
->sa_data
, hlen
, UIO_WRITE
, uio
);
390 error
= UIOMOVE(mtod(m
, caddr_t
), len
- hlen
, UIO_WRITE
, uio
);
394 /* Check for multicast destination */
397 struct ether_header
*eh
= mtod(m
, struct ether_header
*);
399 if (ETHER_IS_MULTICAST(eh
->ether_dhost
)) {
400 if (_ether_cmp(etherbroadcastaddr
, eh
->ether_dhost
) == 0)
401 m
->m_flags
|= M_BCAST
;
403 m
->m_flags
|= M_MCAST
;
418 * The dynamic addition of a new device node must block all processes that
419 * are opening the last device so that no process will get an unexpected
423 bpf_make_dev_t(int maj
)
425 static int bpf_growing
= 0;
426 unsigned int cur_size
= nbpfilter
, i
;
428 if (nbpfilter
>= bpf_maxdevices
)
431 while (bpf_growing
) {
432 /* Wait until new device has been created */
433 (void)tsleep((caddr_t
)&bpf_growing
, PZERO
, "bpf_growing", 0);
435 if (nbpfilter
> cur_size
) {
436 /* other thread grew it already */
441 /* need to grow bpf_dtab first */
442 if (nbpfilter
== bpf_dtab_size
) {
444 struct bpf_d
**new_dtab
= NULL
;
445 struct bpf_d
**old_dtab
= NULL
;
447 new_dtab_size
= bpf_dtab_size
+ NBPFILTER
;
448 new_dtab
= (struct bpf_d
**)_MALLOC(sizeof(struct bpf_d
*) * new_dtab_size
, M_DEVBUF
, M_WAIT
);
450 printf("bpf_make_dev_t: malloc bpf_dtab failed\n");
454 bcopy(bpf_dtab
, new_dtab
,
455 sizeof(struct bpf_d
*) * bpf_dtab_size
);
457 bzero(new_dtab
+ bpf_dtab_size
,
458 sizeof(struct bpf_d
*) * NBPFILTER
);
461 bpf_dtab_size
= new_dtab_size
;
462 if (old_dtab
!= NULL
)
463 _FREE(old_dtab
, M_DEVBUF
);
466 (void) devfs_make_node(makedev(maj
, i
),
467 DEVFS_CHAR
, UID_ROOT
, GID_WHEEL
, 0600,
471 wakeup((caddr_t
)&bpf_growing
);
477 * Attach file to the bpf interface, i.e. make d listen on bp.
480 bpf_attachd(struct bpf_d
*d
, struct bpf_if
*bp
)
482 int first
= bp
->bif_dlist
== NULL
;
486 * Point d at bp, and add d to the interface's list of listeners.
487 * Finally, point the driver's bpf cookie at the interface so
488 * it will divert packets to bpf.
491 d
->bd_next
= bp
->bif_dlist
;
495 * Take a reference on the device even if an error is returned
496 * because we keep the device in the interface's list of listeners
501 /* Find the default bpf entry for this ifp */
502 if (bp
->bif_ifp
->if_bpf
== NULL
) {
503 struct bpf_if
*tmp
, *primary
= NULL
;
505 for (tmp
= bpf_iflist
; tmp
; tmp
= tmp
->bif_next
) {
506 if (tmp
->bif_ifp
!= bp
->bif_ifp
)
510 * Make DLT_PKTAP only if process knows how
511 * to deal with it, otherwise find another one
513 if (tmp
->bif_dlt
== DLT_PKTAP
&&
514 !(d
->bd_flags
& BPF_WANT_PKTAP
))
518 bp
->bif_ifp
->if_bpf
= primary
;
521 /* Only call dlil_set_bpf_tap for primary dlt */
522 if (bp
->bif_ifp
->if_bpf
== bp
)
523 dlil_set_bpf_tap(bp
->bif_ifp
, BPF_TAP_INPUT_OUTPUT
, bpf_tap_callback
);
526 error
= bp
->bif_tap(bp
->bif_ifp
, bp
->bif_dlt
, BPF_TAP_INPUT_OUTPUT
);
530 * Reset the detach flags in case we previously detached an interface
532 d
->bd_flags
&= ~(BPF_DETACHING
| BPF_DETACHED
);
534 if (bp
->bif_ifp
->if_bpf
!= NULL
&&
535 bp
->bif_ifp
->if_bpf
->bif_dlt
== DLT_PKTAP
)
536 d
->bd_flags
|= BPF_FINALIZE_PKTAP
;
538 d
->bd_flags
&= ~BPF_FINALIZE_PKTAP
;
544 * Detach a file from its interface.
546 * Return 1 if was closed by some thread, 0 otherwise
549 bpf_detachd(struct bpf_d
*d
, int closing
)
556 * Some other thread already detached
558 if ((d
->bd_flags
& (BPF_DETACHED
| BPF_DETACHING
)) != 0)
561 * This thread is doing the detach
563 d
->bd_flags
|= BPF_DETACHING
;
565 ifp
= d
->bd_bif
->bif_ifp
;
569 printf("%s: %llx %s%s\n",
570 __func__
, (uint64_t)VM_KERNEL_ADDRPERM(d
),
571 if_name(ifp
), closing
? " closing" : "");
573 /* Remove d from the interface's descriptor list. */
578 panic("bpf_detachd: descriptor not in list");
581 if (bp
->bif_dlist
== 0) {
583 * Let the driver know that there are no more listeners.
585 /* Only call dlil_set_bpf_tap for primary dlt */
586 if (bp
->bif_ifp
->if_bpf
== bp
)
587 dlil_set_bpf_tap(ifp
, BPF_TAP_DISABLE
, NULL
);
589 bp
->bif_tap(ifp
, bp
->bif_dlt
, BPF_TAP_DISABLE
);
591 for (bp
= bpf_iflist
; bp
; bp
= bp
->bif_next
)
592 if (bp
->bif_ifp
== ifp
&& bp
->bif_dlist
!= 0)
599 * Check if this descriptor had requested promiscuous mode.
600 * If so, turn it off.
604 lck_mtx_unlock(bpf_mlock
);
605 if (ifnet_set_promiscuous(ifp
, 0)) {
607 * Something is really wrong if we were able to put
608 * the driver into promiscuous mode, but can't
610 * Most likely the network interface is gone.
612 printf("%s: ifnet_set_promiscuous failed\n", __func__
);
614 lck_mtx_lock(bpf_mlock
);
618 * Wake up other thread that are waiting for this thread to finish
621 d
->bd_flags
&= ~BPF_DETACHING
;
622 d
->bd_flags
|= BPF_DETACHED
;
624 * Note that We've kept the reference because we may have dropped
625 * the lock when turning off promiscuous mode
631 * When closing makes sure no other thread refer to the bpf_d
634 printf("%s: %llx done\n",
635 __func__
, (uint64_t)VM_KERNEL_ADDRPERM(d
));
637 * Let the caller know the bpf_d is closed
639 if ((d
->bd_flags
& BPF_CLOSING
))
647 * Start asynchronous timer, if necessary.
648 * Must be called with bpf_mlock held.
651 bpf_start_timer(struct bpf_d
*d
)
656 if (d
->bd_rtout
> 0 && d
->bd_state
== BPF_IDLE
) {
657 tv
.tv_sec
= d
->bd_rtout
/ hz
;
658 tv
.tv_usec
= (d
->bd_rtout
% hz
) * tick
;
660 clock_interval_to_deadline(
661 (uint64_t)tv
.tv_sec
* USEC_PER_SEC
+ tv
.tv_usec
,
662 NSEC_PER_USEC
, &deadline
);
664 * The state is BPF_IDLE, so the timer hasn't
665 * been started yet, and hasn't gone off yet;
666 * there is no thread call scheduled, so this
667 * won't change the schedule.
669 * XXX - what if, by the time it gets entered,
670 * the deadline has already passed?
672 thread_call_enter_delayed(d
->bd_thread_call
, deadline
);
673 d
->bd_state
= BPF_WAITING
;
678 * Cancel asynchronous timer.
679 * Must be called with bpf_mlock held.
682 bpf_stop_timer(struct bpf_d
*d
)
685 * If the timer has already gone off, this does nothing.
686 * Our caller is expected to set d->bd_state to BPF_IDLE,
687 * with the bpf_mlock, after we are called. bpf_timed_out()
688 * also grabs bpf_mlock, so, if the timer has gone off and
689 * bpf_timed_out() hasn't finished, it's waiting for the
690 * lock; when this thread releases the lock, it will
691 * find the state is BPF_IDLE, and just release the
694 return (thread_call_cancel(d
->bd_thread_call
));
698 bpf_acquire_d(struct bpf_d
*d
)
700 void *lr_saved
= __builtin_return_address(0);
702 lck_mtx_assert(bpf_mlock
, LCK_MTX_ASSERT_OWNED
);
706 d
->bd_ref_lr
[d
->bd_next_ref_lr
] = lr_saved
;
707 d
->bd_next_ref_lr
= (d
->bd_next_ref_lr
+ 1) % BPF_REF_HIST
;
711 bpf_release_d(struct bpf_d
*d
)
713 void *lr_saved
= __builtin_return_address(0);
715 lck_mtx_assert(bpf_mlock
, LCK_MTX_ASSERT_OWNED
);
717 if (d
->bd_refcnt
<= 0)
718 panic("%s: %p refcnt <= 0", __func__
, d
);
722 d
->bd_unref_lr
[d
->bd_next_unref_lr
] = lr_saved
;
723 d
->bd_next_unref_lr
= (d
->bd_next_unref_lr
+ 1) % BPF_REF_HIST
;
725 if (d
->bd_refcnt
== 0) {
726 /* Assert the device is detached */
727 if ((d
->bd_flags
& BPF_DETACHED
) == 0)
728 panic("%s: %p BPF_DETACHED not set", __func__
, d
);
735 * Open ethernet device. Returns ENXIO for illegal minor device number,
736 * EBUSY if file is open by another process.
740 bpfopen(dev_t dev
, int flags
, __unused
int fmt
,
741 __unused
struct proc
*p
)
745 lck_mtx_lock(bpf_mlock
);
746 if ((unsigned int) minor(dev
) >= nbpfilter
) {
747 lck_mtx_unlock(bpf_mlock
);
751 * New device nodes are created on demand when opening the last one.
752 * The programming model is for processes to loop on the minor starting at 0
753 * as long as EBUSY is returned. The loop stops when either the open succeeds or
754 * an error other that EBUSY is returned. That means that bpf_make_dev_t() must
755 * block all processes that are opening the last node. If not all
756 * processes are blocked, they could unexpectedly get ENOENT and abort their
759 if ((unsigned int) minor(dev
) == (nbpfilter
- 1))
760 bpf_make_dev_t(major(dev
));
763 * Each minor can be opened by only one process. If the requested
764 * minor is in use, return EBUSY.
766 * Important: bpfopen() and bpfclose() have to check and set the status of a device
767 * in the same lockin context otherwise the device may be leaked because the vnode use count
768 * will be unpextectly greater than 1 when close() is called.
770 if (bpf_dtab
[minor(dev
)] == 0) {
771 bpf_dtab
[minor(dev
)] = (void *)1; /* Mark opening */
773 lck_mtx_unlock(bpf_mlock
);
776 d
= (struct bpf_d
*)_MALLOC(sizeof(struct bpf_d
), M_DEVBUF
,
779 /* this really is a catastrophic failure */
780 printf("bpfopen: malloc bpf_d failed\n");
781 bpf_dtab
[minor(dev
)] = NULL
;
782 lck_mtx_unlock(bpf_mlock
);
786 /* Mark "in use" and do most initialization. */
788 d
->bd_bufsize
= bpf_bufsize
;
791 d
->bd_oflags
= flags
;
792 d
->bd_state
= BPF_IDLE
;
793 d
->bd_traffic_class
= SO_TC_BE
;
794 d
->bd_flags
|= BPF_DETACHED
;
796 d
->bd_flags
|= BPF_WANT_PKTAP
;
798 d
->bd_flags
&= ~BPF_WANT_PKTAP
;
799 d
->bd_thread_call
= thread_call_allocate(bpf_timed_out
, d
);
800 if (d
->bd_thread_call
== NULL
) {
801 printf("bpfopen: malloc thread call failed\n");
802 bpf_dtab
[minor(dev
)] = NULL
;
804 lck_mtx_unlock(bpf_mlock
);
809 mac_bpfdesc_label_init(d
);
810 mac_bpfdesc_label_associate(kauth_cred_get(), d
);
812 bpf_dtab
[minor(dev
)] = d
; /* Mark opened */
813 lck_mtx_unlock(bpf_mlock
);
819 * Close the descriptor by detaching it from its interface,
820 * deallocating its buffers, and marking it free.
824 bpfclose(dev_t dev
, __unused
int flags
, __unused
int fmt
,
825 __unused
struct proc
*p
)
829 /* Take BPF lock to ensure no other thread is using the device */
830 lck_mtx_lock(bpf_mlock
);
832 d
= bpf_dtab
[minor(dev
)];
833 if (d
== 0 || d
== (void *)1) {
834 lck_mtx_unlock(bpf_mlock
);
839 * Other threads may call bpd_detachd() if we drop the bpf_mlock
841 d
->bd_flags
|= BPF_CLOSING
;
845 __func__
, (uint64_t)VM_KERNEL_ADDRPERM(d
));
847 bpf_dtab
[minor(dev
)] = (void *)1; /* Mark closing */
850 * Deal with any in-progress timeouts.
852 switch (d
->bd_state
) {
855 * Not waiting for a timeout, and no timeout happened.
861 * Waiting for a timeout.
862 * Cancel any timer that has yet to go off,
863 * and mark the state as "closing".
864 * Then drop the lock to allow any timers that
865 * *have* gone off to run to completion, and wait
866 * for them to finish.
868 if (!bpf_stop_timer(d
)) {
870 * There was no pending call, so the call must
871 * have been in progress. Wait for the call to
872 * complete; we have to drop the lock while
873 * waiting. to let the in-progrss call complete
875 d
->bd_state
= BPF_DRAINING
;
876 while (d
->bd_state
== BPF_DRAINING
)
877 msleep((caddr_t
)d
, bpf_mlock
, PRINET
,
878 "bpfdraining", NULL
);
880 d
->bd_state
= BPF_IDLE
;
885 * Timer went off, and the timeout routine finished.
887 d
->bd_state
= BPF_IDLE
;
892 * Another thread is blocked on a close waiting for
893 * a timeout to finish.
894 * This "shouldn't happen", as the first thread to enter
895 * bpfclose() will set bpf_dtab[minor(dev)] to 1, and
896 * all subsequent threads should see that and fail with
899 panic("Two threads blocked in a BPF close");
905 selthreadclear(&d
->bd_sel
);
907 mac_bpfdesc_label_destroy(d
);
909 thread_call_free(d
->bd_thread_call
);
911 while (d
->bd_hbuf_read
)
912 msleep((caddr_t
)d
, bpf_mlock
, PRINET
, "bpf_reading", NULL
);
916 /* Mark free in same context as bpfopen comes to check */
917 bpf_dtab
[minor(dev
)] = NULL
; /* Mark closed */
921 lck_mtx_unlock(bpf_mlock
);
927 #define BPF_SLEEP bpf_sleep
930 bpf_sleep(struct bpf_d
*d
, int pri
, const char *wmesg
, int timo
)
932 u_int64_t abstime
= 0;
935 clock_interval_to_deadline(timo
, NSEC_PER_SEC
/ hz
, &abstime
);
937 return msleep1((caddr_t
)d
, bpf_mlock
, pri
, wmesg
, abstime
);
941 * Rotate the packet buffers in descriptor d. Move the store buffer
942 * into the hold slot, and the free buffer into the store slot.
943 * Zero the length of the new store buffer.
945 #define ROTATE_BUFFERS(d) \
946 if (d->bd_hbuf_read) \
947 panic("rotating bpf buffers during read"); \
948 (d)->bd_hbuf = (d)->bd_sbuf; \
949 (d)->bd_hlen = (d)->bd_slen; \
950 (d)->bd_hcnt = (d)->bd_scnt; \
951 (d)->bd_sbuf = (d)->bd_fbuf; \
956 * bpfread - read next chunk of packets from buffers
959 bpfread(dev_t dev
, struct uio
*uio
, int ioflag
)
963 int timed_out
, hbuf_len
;
967 lck_mtx_lock(bpf_mlock
);
969 d
= bpf_dtab
[minor(dev
)];
970 if (d
== 0 || d
== (void *)1 || (d
->bd_flags
& BPF_CLOSING
) != 0) {
971 lck_mtx_unlock(bpf_mlock
);
978 * Restrict application to use a buffer the same size as
981 if (uio_resid(uio
) != d
->bd_bufsize
) {
983 lck_mtx_unlock(bpf_mlock
);
987 if (d
->bd_state
== BPF_WAITING
)
990 timed_out
= (d
->bd_state
== BPF_TIMED_OUT
);
991 d
->bd_state
= BPF_IDLE
;
993 while (d
->bd_hbuf_read
)
994 msleep((caddr_t
)d
, bpf_mlock
, PRINET
, "bpf_reading", NULL
);
996 if ((d
->bd_flags
& BPF_CLOSING
) != 0) {
998 lck_mtx_unlock(bpf_mlock
);
1002 * If the hold buffer is empty, then do a timed sleep, which
1003 * ends when the timeout expires or when enough packets
1004 * have arrived to fill the store buffer.
1006 while (d
->bd_hbuf
== 0) {
1007 if ((d
->bd_immediate
|| timed_out
|| (ioflag
& IO_NDELAY
))
1008 && d
->bd_slen
!= 0) {
1010 * We're in immediate mode, or are reading
1011 * in non-blocking mode, or a timer was
1012 * started before the read (e.g., by select()
1013 * or poll()) and has expired and a packet(s)
1014 * either arrived since the previous
1015 * read or arrived while we were asleep.
1016 * Rotate the buffers and return what's here.
1023 * No data is available, check to see if the bpf device
1024 * is still pointed at a real interface. If not, return
1025 * ENXIO so that the userland process knows to rebind
1026 * it before using it again.
1028 if (d
->bd_bif
== NULL
) {
1030 lck_mtx_unlock(bpf_mlock
);
1033 if (ioflag
& IO_NDELAY
) {
1035 lck_mtx_unlock(bpf_mlock
);
1036 return (EWOULDBLOCK
);
1038 error
= BPF_SLEEP(d
, PRINET
|PCATCH
, "bpf",
1041 * Make sure device is still opened
1043 if ((d
->bd_flags
& BPF_CLOSING
) != 0) {
1045 lck_mtx_unlock(bpf_mlock
);
1049 while (d
->bd_hbuf_read
)
1050 msleep((caddr_t
)d
, bpf_mlock
, PRINET
, "bpf_reading", NULL
);
1052 if ((d
->bd_flags
& BPF_CLOSING
) != 0) {
1054 lck_mtx_unlock(bpf_mlock
);
1058 if (error
== EINTR
|| error
== ERESTART
) {
1061 * Because we msleep, the hold buffer might
1062 * be filled when we wake up. Avoid rotating
1069 * Sometimes we may be interrupted often and
1070 * the sleep above will not timeout.
1071 * Regardless, we should rotate the buffers
1072 * if there's any new data pending and
1079 lck_mtx_unlock(bpf_mlock
);
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
);
1150 if (flags
& BPF_FINALIZE_PKTAP
) {
1151 struct pktap_header
*pktaphdr
;
1153 pktaphdr
= (struct pktap_header
*)(void *)
1154 (p
+ BPF_WORDALIGN(ehp
->bh_hdrlen
));
1156 if (pktaphdr
->pth_flags
& PTH_FLAG_DELAY_PKTAP
)
1157 pktap_finalize_proc_info(pktaphdr
);
1159 if (pktaphdr
->pth_flags
& PTH_FLAG_TSTAMP
) {
1160 ehp
->bh_tstamp
.tv_sec
=
1161 pktaphdr
->pth_tstamp
.tv_sec
;
1162 ehp
->bh_tstamp
.tv_usec
=
1163 pktaphdr
->pth_tstamp
.tv_usec
;
1166 p
+= BPF_WORDALIGN(ehp
->bh_hdrlen
+ ehp
->bh_caplen
);
1168 } else if (flags
& BPF_FINALIZE_PKTAP
) {
1172 while (p
< hbuf
+ hbuf_len
) {
1174 struct pktap_header
*pktaphdr
;
1176 hp
= (struct bpf_hdr
*)(void *)p
;
1177 pktaphdr
= (struct pktap_header
*)(void *)
1178 (p
+ BPF_WORDALIGN(hp
->bh_hdrlen
));
1180 if (pktaphdr
->pth_flags
& PTH_FLAG_DELAY_PKTAP
)
1181 pktap_finalize_proc_info(pktaphdr
);
1183 if (pktaphdr
->pth_flags
& PTH_FLAG_TSTAMP
) {
1184 hp
->bh_tstamp
.tv_sec
=
1185 pktaphdr
->pth_tstamp
.tv_sec
;
1186 hp
->bh_tstamp
.tv_usec
=
1187 pktaphdr
->pth_tstamp
.tv_usec
;
1190 p
+= BPF_WORDALIGN(hp
->bh_hdrlen
+ hp
->bh_caplen
);
1196 * Move data from hold buffer into user space.
1197 * We know the entire buffer is transferred since
1198 * we checked above that the read buffer is bpf_bufsize bytes.
1200 error
= UIOMOVE(hbuf
, hbuf_len
, UIO_READ
, uio
);
1202 lck_mtx_lock(bpf_mlock
);
1204 * Make sure device is still opened
1206 if ((d
->bd_flags
& BPF_CLOSING
) != 0) {
1208 lck_mtx_unlock(bpf_mlock
);
1212 d
->bd_hbuf_read
= 0;
1213 d
->bd_fbuf
= d
->bd_hbuf
;
1220 lck_mtx_unlock(bpf_mlock
);
1227 * If there are processes sleeping on this descriptor, wake them up.
1230 bpf_wakeup(struct bpf_d
*d
)
1232 if (d
->bd_state
== BPF_WAITING
) {
1234 d
->bd_state
= BPF_IDLE
;
1237 if (d
->bd_async
&& d
->bd_sig
&& d
->bd_sigio
)
1238 pgsigio(d
->bd_sigio
, d
->bd_sig
);
1240 selwakeup(&d
->bd_sel
);
1241 if ((d
->bd_flags
& BPF_KNOTE
))
1242 KNOTE(&d
->bd_sel
.si_note
, 1);
1247 bpf_timed_out(void *arg
, __unused
void *dummy
)
1249 struct bpf_d
*d
= (struct bpf_d
*)arg
;
1251 lck_mtx_lock(bpf_mlock
);
1252 if (d
->bd_state
== BPF_WAITING
) {
1254 * There's a select or kqueue waiting for this; if there's
1255 * now stuff to read, wake it up.
1257 d
->bd_state
= BPF_TIMED_OUT
;
1258 if (d
->bd_slen
!= 0)
1260 } else if (d
->bd_state
== BPF_DRAINING
) {
1262 * A close is waiting for this to finish.
1263 * Mark it as finished, and wake the close up.
1265 d
->bd_state
= BPF_IDLE
;
1268 lck_mtx_unlock(bpf_mlock
);
1275 /* keep in sync with bpf_movein above: */
1276 #define MAX_DATALINK_HDR_LEN (sizeof(struct firewire_header))
1279 bpfwrite(dev_t dev
, struct uio
*uio
, __unused
int ioflag
)
1283 struct mbuf
*m
= NULL
;
1285 char dst_buf
[SOCKADDR_HDR_LEN
+ MAX_DATALINK_HDR_LEN
];
1290 lck_mtx_lock(bpf_mlock
);
1292 d
= bpf_dtab
[minor(dev
)];
1293 if (d
== 0 || d
== (void *)1 || (d
->bd_flags
& BPF_CLOSING
) != 0) {
1294 lck_mtx_unlock(bpf_mlock
);
1300 if (d
->bd_bif
== 0) {
1302 lck_mtx_unlock(bpf_mlock
);
1306 ifp
= d
->bd_bif
->bif_ifp
;
1308 if ((ifp
->if_flags
& IFF_UP
) == 0) {
1310 lck_mtx_unlock(bpf_mlock
);
1313 if (uio_resid(uio
) == 0) {
1315 lck_mtx_unlock(bpf_mlock
);
1318 ((struct sockaddr
*)dst_buf
)->sa_len
= sizeof(dst_buf
);
1321 * fix for PR-6849527
1322 * geting variables onto stack before dropping lock for bpf_movein()
1324 bif_dlt
= (int)d
->bd_bif
->bif_dlt
;
1325 bd_hdrcmplt
= d
->bd_hdrcmplt
;
1327 /* bpf_movein allocating mbufs; drop lock */
1328 lck_mtx_unlock(bpf_mlock
);
1330 error
= bpf_movein(uio
, bif_dlt
, &m
,
1331 bd_hdrcmplt
? NULL
: (struct sockaddr
*)dst_buf
,
1334 /* take the lock again */
1335 lck_mtx_lock(bpf_mlock
);
1338 lck_mtx_unlock(bpf_mlock
);
1342 /* verify the device is still open */
1343 if ((d
->bd_flags
& BPF_CLOSING
) != 0) {
1345 lck_mtx_unlock(bpf_mlock
);
1350 if (d
->bd_bif
== NULL
) {
1352 lck_mtx_unlock(bpf_mlock
);
1357 if ((unsigned)datlen
> ifp
->if_mtu
) {
1359 lck_mtx_unlock(bpf_mlock
);
1366 mac_mbuf_label_associate_bpfdesc(d
, m
);
1369 bpf_set_packet_service_class(m
, d
->bd_traffic_class
);
1371 lck_mtx_unlock(bpf_mlock
);
1374 * The driver frees the mbuf.
1376 if (d
->bd_hdrcmplt
) {
1377 if (d
->bd_bif
->bif_send
)
1378 error
= d
->bd_bif
->bif_send(ifp
, d
->bd_bif
->bif_dlt
, m
);
1380 error
= dlil_output(ifp
, 0, m
, NULL
, NULL
, 1, NULL
);
1382 error
= dlil_output(ifp
, PF_INET
, m
, NULL
,
1383 (struct sockaddr
*)dst_buf
, 0, NULL
);
1386 lck_mtx_lock(bpf_mlock
);
1388 lck_mtx_unlock(bpf_mlock
);
1394 * Reset a descriptor by flushing its packet buffer and clearing the
1395 * receive and drop counts.
1398 reset_d(struct bpf_d
*d
)
1400 if (d
->bd_hbuf_read
)
1401 panic("resetting buffers during read");
1404 /* Free the hold buffer. */
1405 d
->bd_fbuf
= d
->bd_hbuf
;
1417 * FIONREAD Check for read packet available.
1418 * SIOCGIFADDR Get interface address - convenient hook to driver.
1419 * BIOCGBLEN Get buffer len [for read()].
1420 * BIOCSETF Set ethernet read filter.
1421 * BIOCFLUSH Flush read packet buffer.
1422 * BIOCPROMISC Put interface into promiscuous mode.
1423 * BIOCGDLT Get link layer type.
1424 * BIOCGETIF Get interface name.
1425 * BIOCSETIF Set interface.
1426 * BIOCSRTIMEOUT Set read timeout.
1427 * BIOCGRTIMEOUT Get read timeout.
1428 * BIOCGSTATS Get packet stats.
1429 * BIOCIMMEDIATE Set immediate mode.
1430 * BIOCVERSION Get filter language version.
1431 * BIOCGHDRCMPLT Get "header already complete" flag
1432 * BIOCSHDRCMPLT Set "header already complete" flag
1433 * BIOCGSEESENT Get "see packets sent" flag
1434 * BIOCSSEESENT Set "see packets sent" flag
1435 * BIOCSETTC Set traffic class.
1436 * BIOCGETTC Get traffic class.
1437 * BIOCSEXTHDR Set "extended header" flag
1438 * BIOCSHEADDROP Drop head of the buffer if user is not reading
1439 * BIOCGHEADDROP Get "head-drop" flag
1443 bpfioctl(dev_t dev
, u_long cmd
, caddr_t addr
, __unused
int flags
,
1451 lck_mtx_lock(bpf_mlock
);
1453 d
= bpf_dtab
[minor(dev
)];
1454 if (d
== 0 || d
== (void *)1 || (d
->bd_flags
& BPF_CLOSING
) != 0) {
1455 lck_mtx_unlock(bpf_mlock
);
1461 if (d
->bd_state
== BPF_WAITING
)
1463 d
->bd_state
= BPF_IDLE
;
1472 * Check for read packet available.
1474 case FIONREAD
: /* int */
1479 if (d
->bd_hbuf
&& d
->bd_hbuf_read
== 0)
1482 bcopy(&n
, addr
, sizeof (n
));
1486 case SIOCGIFADDR
: /* struct ifreq */
1493 ifp
= d
->bd_bif
->bif_ifp
;
1494 error
= ifnet_ioctl(ifp
, 0, cmd
, addr
);
1500 * Get buffer len [for read()].
1502 case BIOCGBLEN
: /* u_int */
1503 bcopy(&d
->bd_bufsize
, addr
, sizeof (u_int
));
1507 * Set buffer length.
1509 case BIOCSBLEN
: /* u_int */
1515 bcopy(addr
, &size
, sizeof (size
));
1518 * Allow larger buffer in head drop mode with the
1519 * assumption the capture is in standby mode to
1520 * keep a cache of recent traffic
1522 if (d
->bd_headdrop
!= 0 && size
> 2 * bpf_maxbufsize
)
1523 size
= 2 * bpf_maxbufsize
;
1524 else if (size
> bpf_maxbufsize
)
1525 size
= bpf_maxbufsize
;
1526 else if (size
< BPF_MINBUFSIZE
)
1527 size
= BPF_MINBUFSIZE
;
1528 bcopy(&size
, addr
, sizeof (size
));
1529 d
->bd_bufsize
= size
;
1534 * Set link layer read filter.
1537 case BIOCSETFNR32
: { /* struct bpf_program32 */
1538 struct bpf_program32 prg32
;
1540 bcopy(addr
, &prg32
, sizeof (prg32
));
1541 error
= bpf_setf(d
, prg32
.bf_len
,
1542 CAST_USER_ADDR_T(prg32
.bf_insns
), cmd
);
1547 case BIOCSETFNR64
: { /* struct bpf_program64 */
1548 struct bpf_program64 prg64
;
1550 bcopy(addr
, &prg64
, sizeof (prg64
));
1551 error
= bpf_setf(d
, prg64
.bf_len
, prg64
.bf_insns
, cmd
);
1556 * Flush read packet buffer.
1559 while (d
->bd_hbuf_read
) {
1560 msleep((caddr_t
)d
, bpf_mlock
, PRINET
, "bpf_reading", NULL
);
1562 if ((d
->bd_flags
& BPF_CLOSING
) != 0) {
1570 * Put interface into promiscuous mode.
1573 if (d
->bd_bif
== 0) {
1575 * No interface attached yet.
1580 if (d
->bd_promisc
== 0) {
1581 lck_mtx_unlock(bpf_mlock
);
1582 error
= ifnet_set_promiscuous(d
->bd_bif
->bif_ifp
, 1);
1583 lck_mtx_lock(bpf_mlock
);
1590 * Get device parameters.
1592 case BIOCGDLT
: /* u_int */
1596 bcopy(&d
->bd_bif
->bif_dlt
, addr
, sizeof (u_int
));
1600 * Get a list of supported data link types.
1602 case BIOCGDLTLIST
: /* struct bpf_dltlist */
1603 if (d
->bd_bif
== NULL
) {
1606 error
= bpf_getdltlist(d
, addr
, p
);
1611 * Set data link type.
1613 case BIOCSDLT
: /* u_int */
1614 if (d
->bd_bif
== NULL
) {
1619 bcopy(addr
, &dlt
, sizeof (dlt
));
1620 error
= bpf_setdlt(d
, dlt
);
1625 * Get interface name.
1627 case BIOCGETIF
: /* struct ifreq */
1631 struct ifnet
*const ifp
= d
->bd_bif
->bif_ifp
;
1633 snprintf(((struct ifreq
*)(void *)addr
)->ifr_name
,
1634 sizeof (ifr
.ifr_name
), "%s", if_name(ifp
));
1641 case BIOCSETIF
: { /* struct ifreq */
1644 bcopy(addr
, &ifr
, sizeof (ifr
));
1645 ifr
.ifr_name
[IFNAMSIZ
- 1] = '\0';
1646 ifp
= ifunit(ifr
.ifr_name
);
1650 error
= bpf_setif(d
, ifp
, 0);
1657 case BIOCSRTIMEOUT32
: { /* struct user32_timeval */
1658 struct user32_timeval _tv
;
1661 bcopy(addr
, &_tv
, sizeof (_tv
));
1662 tv
.tv_sec
= _tv
.tv_sec
;
1663 tv
.tv_usec
= _tv
.tv_usec
;
1666 * Subtract 1 tick from tvtohz() since this isn't
1669 if ((error
= itimerfix(&tv
)) == 0)
1670 d
->bd_rtout
= tvtohz(&tv
) - 1;
1674 case BIOCSRTIMEOUT64
: { /* struct user64_timeval */
1675 struct user64_timeval _tv
;
1678 bcopy(addr
, &_tv
, sizeof (_tv
));
1679 tv
.tv_sec
= _tv
.tv_sec
;
1680 tv
.tv_usec
= _tv
.tv_usec
;
1683 * Subtract 1 tick from tvtohz() since this isn't
1686 if ((error
= itimerfix(&tv
)) == 0)
1687 d
->bd_rtout
= tvtohz(&tv
) - 1;
1694 case BIOCGRTIMEOUT32
: { /* struct user32_timeval */
1695 struct user32_timeval tv
;
1697 bzero(&tv
, sizeof (tv
));
1698 tv
.tv_sec
= d
->bd_rtout
/ hz
;
1699 tv
.tv_usec
= (d
->bd_rtout
% hz
) * tick
;
1700 bcopy(&tv
, addr
, sizeof (tv
));
1704 case BIOCGRTIMEOUT64
: { /* struct user64_timeval */
1705 struct user64_timeval tv
;
1707 bzero(&tv
, sizeof (tv
));
1708 tv
.tv_sec
= d
->bd_rtout
/ hz
;
1709 tv
.tv_usec
= (d
->bd_rtout
% hz
) * tick
;
1710 bcopy(&tv
, addr
, sizeof (tv
));
1717 case BIOCGSTATS
: { /* struct bpf_stat */
1720 bzero(&bs
, sizeof (bs
));
1721 bs
.bs_recv
= d
->bd_rcount
;
1722 bs
.bs_drop
= d
->bd_dcount
;
1723 bcopy(&bs
, addr
, sizeof (bs
));
1728 * Set immediate mode.
1730 case BIOCIMMEDIATE
: /* u_int */
1731 d
->bd_immediate
= *(u_int
*)(void *)addr
;
1734 case BIOCVERSION
: { /* struct bpf_version */
1735 struct bpf_version bv
;
1737 bzero(&bv
, sizeof (bv
));
1738 bv
.bv_major
= BPF_MAJOR_VERSION
;
1739 bv
.bv_minor
= BPF_MINOR_VERSION
;
1740 bcopy(&bv
, addr
, sizeof (bv
));
1745 * Get "header already complete" flag
1747 case BIOCGHDRCMPLT
: /* u_int */
1748 bcopy(&d
->bd_hdrcmplt
, addr
, sizeof (u_int
));
1752 * Set "header already complete" flag
1754 case BIOCSHDRCMPLT
: /* u_int */
1755 bcopy(addr
, &int_arg
, sizeof (int_arg
));
1756 d
->bd_hdrcmplt
= int_arg
? 1 : 0;
1760 * Get "see sent packets" flag
1762 case BIOCGSEESENT
: /* u_int */
1763 bcopy(&d
->bd_seesent
, addr
, sizeof (u_int
));
1767 * Set "see sent packets" flag
1769 case BIOCSSEESENT
: /* u_int */
1770 bcopy(addr
, &d
->bd_seesent
, sizeof (u_int
));
1774 * Set traffic service class
1776 case BIOCSETTC
: { /* int */
1779 bcopy(addr
, &tc
, sizeof (int));
1780 error
= bpf_set_traffic_class(d
, tc
);
1785 * Get traffic service class
1787 case BIOCGETTC
: /* int */
1788 bcopy(&d
->bd_traffic_class
, addr
, sizeof (int));
1791 case FIONBIO
: /* Non-blocking I/O; int */
1794 case FIOASYNC
: /* Send signal on receive packets; int */
1795 bcopy(addr
, &d
->bd_async
, sizeof (int));
1799 error
= fsetown(*(int *)addr
, &d
->bd_sigio
);
1803 *(int *)addr
= fgetown(d
->bd_sigio
);
1806 /* This is deprecated, FIOSETOWN should be used instead. */
1808 error
= fsetown(-(*(int *)addr
), &d
->bd_sigio
);
1811 /* This is deprecated, FIOGETOWN should be used instead. */
1813 *(int *)addr
= -fgetown(d
->bd_sigio
);
1816 case BIOCSRSIG
: { /* Set receive signal; u_int */
1819 bcopy(addr
, &sig
, sizeof (u_int
));
1827 case BIOCGRSIG
: /* u_int */
1828 bcopy(&d
->bd_sig
, addr
, sizeof (u_int
));
1831 case BIOCSEXTHDR
: /* u_int */
1832 bcopy(addr
, &int_arg
, sizeof (int_arg
));
1834 d
->bd_flags
|= BPF_EXTENDED_HDR
;
1836 d
->bd_flags
&= ~BPF_EXTENDED_HDR
;
1839 case BIOCGIFATTACHCOUNT
: { /* struct ifreq */
1843 bcopy(addr
, &ifr
, sizeof (ifr
));
1844 ifr
.ifr_name
[IFNAMSIZ
- 1] = '\0';
1845 ifp
= ifunit(ifr
.ifr_name
);
1851 for (bp
= bpf_iflist
; bp
!= 0; bp
= bp
->bif_next
) {
1852 struct bpf_d
*bpf_d
;
1854 if (bp
->bif_ifp
== NULL
|| bp
->bif_ifp
!= ifp
)
1856 for (bpf_d
= bp
->bif_dlist
; bpf_d
; bpf_d
= bpf_d
->bd_next
) {
1857 ifr
.ifr_intval
+= 1;
1860 bcopy(&ifr
, addr
, sizeof (ifr
));
1863 case BIOCGWANTPKTAP
: /* u_int */
1864 int_arg
= d
->bd_flags
& BPF_WANT_PKTAP
? 1 : 0;
1865 bcopy(&int_arg
, addr
, sizeof (int_arg
));
1868 case BIOCSWANTPKTAP
: /* u_int */
1869 bcopy(addr
, &int_arg
, sizeof (int_arg
));
1871 d
->bd_flags
|= BPF_WANT_PKTAP
;
1873 d
->bd_flags
&= ~BPF_WANT_PKTAP
;
1878 bcopy(addr
, &int_arg
, sizeof (int_arg
));
1879 d
->bd_headdrop
= int_arg
? 1 : 0;
1883 bcopy(&d
->bd_headdrop
, addr
, sizeof (int));
1888 lck_mtx_unlock(bpf_mlock
);
1894 * Set d's packet filter program to fp. If this file already has a filter,
1895 * free it and replace it. Returns EINVAL for bogus requests.
1898 bpf_setf(struct bpf_d
*d
, u_int bf_len
, user_addr_t bf_insns
,
1901 struct bpf_insn
*fcode
, *old
;
1904 while (d
->bd_hbuf_read
)
1905 msleep((caddr_t
)d
, bpf_mlock
, PRINET
, "bpf_reading", NULL
);
1907 if ((d
->bd_flags
& BPF_CLOSING
) != 0)
1911 if (bf_insns
== USER_ADDR_NULL
) {
1914 d
->bd_filter
= NULL
;
1917 FREE((caddr_t
)old
, M_DEVBUF
);
1921 if (flen
> BPF_MAXINSNS
)
1924 size
= flen
* sizeof(struct bpf_insn
);
1925 fcode
= (struct bpf_insn
*) _MALLOC(size
, M_DEVBUF
, M_WAIT
);
1930 if (copyin(bf_insns
, (caddr_t
)fcode
, size
) == 0 &&
1931 bpf_validate(fcode
, (int)flen
)) {
1932 d
->bd_filter
= fcode
;
1934 if (cmd
== BIOCSETF32
|| cmd
== BIOCSETF64
)
1938 FREE((caddr_t
)old
, M_DEVBUF
);
1942 FREE((caddr_t
)fcode
, M_DEVBUF
);
1947 * Detach a file from its current interface (if attached at all) and attach
1948 * to the interface indicated by the name stored in ifr.
1949 * Return an errno or 0.
1952 bpf_setif(struct bpf_d
*d
, ifnet_t theywant
, u_int32_t dlt
)
1957 while (d
->bd_hbuf_read
)
1958 msleep((caddr_t
)d
, bpf_mlock
, PRINET
, "bpf_reading", NULL
);
1960 if ((d
->bd_flags
& BPF_CLOSING
) != 0)
1964 * Look through attached interfaces for the named one.
1966 for (bp
= bpf_iflist
; bp
!= 0; bp
= bp
->bif_next
) {
1967 struct ifnet
*ifp
= bp
->bif_ifp
;
1969 if (ifp
== 0 || ifp
!= theywant
|| (dlt
!= 0 && dlt
!= bp
->bif_dlt
))
1972 * If the process knows how to deal with DLT_PKTAP, use it
1975 if (dlt
== 0 && bp
->bif_dlt
== DLT_PKTAP
&&
1976 !(d
->bd_flags
& BPF_WANT_PKTAP
))
1979 * We found the requested interface.
1980 * Allocate the packet buffers.
1982 error
= bpf_allocbufs(d
);
1986 * Detach if attached to something else.
1988 if (bp
!= d
->bd_bif
) {
1989 if (d
->bd_bif
!= NULL
) {
1990 if (bpf_detachd(d
, 0) != 0)
1993 if (bpf_attachd(d
, bp
) != 0)
2006 * Get a list of available data link type of the interface.
2009 bpf_getdltlist(struct bpf_d
*d
, caddr_t addr
, struct proc
*p
)
2016 struct bpf_dltlist bfl
;
2018 bcopy(addr
, &bfl
, sizeof (bfl
));
2019 if (proc_is64bit(p
)) {
2020 dlist
= (user_addr_t
)bfl
.bfl_u
.bflu_pad
;
2022 dlist
= CAST_USER_ADDR_T(bfl
.bfl_u
.bflu_list
);
2025 ifp
= d
->bd_bif
->bif_ifp
;
2029 for (bp
= bpf_iflist
; bp
; bp
= bp
->bif_next
) {
2030 if (bp
->bif_ifp
!= ifp
)
2033 * Return DLT_PKTAP only to processes that know how to handle it
2035 if (bp
->bif_dlt
== DLT_PKTAP
&& !(d
->bd_flags
& BPF_WANT_PKTAP
))
2037 if (dlist
!= USER_ADDR_NULL
) {
2038 if (n
>= bfl
.bfl_len
) {
2041 error
= copyout(&bp
->bif_dlt
, dlist
,
2042 sizeof (bp
->bif_dlt
));
2045 dlist
+= sizeof (bp
->bif_dlt
);
2050 bcopy(&bfl
, addr
, sizeof (bfl
));
2056 * Set the data link type of a BPF instance.
2059 bpf_setdlt(struct bpf_d
*d
, uint32_t dlt
)
2061 int error
, opromisc
;
2065 if (d
->bd_bif
->bif_dlt
== dlt
)
2068 while (d
->bd_hbuf_read
)
2069 msleep((caddr_t
)d
, bpf_mlock
, PRINET
, "bpf_reading", NULL
);
2071 if ((d
->bd_flags
& BPF_CLOSING
) != 0)
2074 ifp
= d
->bd_bif
->bif_ifp
;
2075 for (bp
= bpf_iflist
; bp
; bp
= bp
->bif_next
) {
2076 if (bp
->bif_ifp
== ifp
&& bp
->bif_dlt
== dlt
)
2080 opromisc
= d
->bd_promisc
;
2081 if (bpf_detachd(d
, 0) != 0)
2083 error
= bpf_attachd(d
, bp
);
2085 printf("bpf_setdlt: bpf_attachd %s%d failed (%d)\n",
2086 ifnet_name(bp
->bif_ifp
), ifnet_unit(bp
->bif_ifp
), error
);
2091 lck_mtx_unlock(bpf_mlock
);
2092 error
= ifnet_set_promiscuous(bp
->bif_ifp
, 1);
2093 lck_mtx_lock(bpf_mlock
);
2095 printf("%s: ifpromisc %s%d failed (%d)\n",
2096 __func__
, ifnet_name(bp
->bif_ifp
),
2097 ifnet_unit(bp
->bif_ifp
), error
);
2103 return (bp
== NULL
? EINVAL
: 0);
2107 bpf_set_traffic_class(struct bpf_d
*d
, int tc
)
2111 if (!SO_VALID_TC(tc
))
2114 d
->bd_traffic_class
= tc
;
2120 bpf_set_packet_service_class(struct mbuf
*m
, int tc
)
2122 if (!(m
->m_flags
& M_PKTHDR
))
2125 VERIFY(SO_VALID_TC(tc
));
2126 (void) m_set_service_class(m
, so_tc2msc(tc
));
2130 * Support for select()
2132 * Return true iff the specific operation will not block indefinitely.
2133 * Otherwise, return false but make a note that a selwakeup() must be done.
2136 bpfselect(dev_t dev
, int which
, void * wql
, struct proc
*p
)
2141 lck_mtx_lock(bpf_mlock
);
2143 d
= bpf_dtab
[minor(dev
)];
2144 if (d
== 0 || d
== (void *)1 || (d
->bd_flags
& BPF_CLOSING
) != 0) {
2145 lck_mtx_unlock(bpf_mlock
);
2151 if (d
->bd_bif
== NULL
) {
2153 lck_mtx_unlock(bpf_mlock
);
2157 while (d
->bd_hbuf_read
)
2158 msleep((caddr_t
)d
, bpf_mlock
, PRINET
, "bpf_reading", NULL
);
2160 if ((d
->bd_flags
& BPF_CLOSING
) != 0) {
2162 lck_mtx_unlock(bpf_mlock
);
2168 if (d
->bd_hlen
!= 0 ||
2169 ((d
->bd_immediate
|| d
->bd_state
== BPF_TIMED_OUT
) &&
2171 ret
= 1; /* read has data to return */
2174 * Read has no data to return.
2175 * Make the select wait, and start a timer if
2178 selrecord(p
, &d
->bd_sel
, wql
);
2184 ret
= 1; /* can't determine whether a write would block */
2189 lck_mtx_unlock(bpf_mlock
);
2196 * Support for kevent() system call. Register EVFILT_READ filters and
2197 * reject all others.
2199 int bpfkqfilter(dev_t dev
, struct knote
*kn
);
2200 static void filt_bpfdetach(struct knote
*);
2201 static int filt_bpfread(struct knote
*, long);
2202 static int filt_bpftouch(struct knote
*kn
, struct kevent_internal_s
*kev
);
2203 static int filt_bpfprocess(struct knote
*kn
, struct filt_process_s
*data
, struct kevent_internal_s
*kev
);
2205 struct filterops bpfread_filtops
= {
2207 .f_detach
= filt_bpfdetach
,
2208 .f_event
= filt_bpfread
,
2209 .f_touch
= filt_bpftouch
,
2210 .f_process
= filt_bpfprocess
,
2214 filt_bpfread_common(struct knote
*kn
, struct bpf_d
*d
)
2218 if (d
->bd_immediate
) {
2220 * If there's data in the hold buffer, it's the
2221 * amount of data a read will return.
2223 * If there's no data in the hold buffer, but
2224 * there's data in the store buffer, a read will
2225 * immediately rotate the store buffer to the
2226 * hold buffer, the amount of data in the store
2227 * buffer is the amount of data a read will
2230 * If there's no data in either buffer, we're not
2233 kn
->kn_data
= ((d
->bd_hlen
== 0 || d
->bd_hbuf_read
)
2234 ? d
->bd_slen
: d
->bd_hlen
);
2236 if (kn
->kn_sfflags
& NOTE_LOWAT
)
2238 if (kn
->kn_sdata
> d
->bd_bufsize
)
2239 lowwat
= d
->bd_bufsize
;
2240 else if (kn
->kn_sdata
> lowwat
)
2241 lowwat
= kn
->kn_sdata
;
2243 ready
= (kn
->kn_data
>= lowwat
);
2246 * If there's data in the hold buffer, it's the
2247 * amount of data a read will return.
2249 * If there's no data in the hold buffer, but
2250 * there's data in the store buffer, if the
2251 * timer has expired a read will immediately
2252 * rotate the store buffer to the hold buffer,
2253 * so the amount of data in the store buffer is
2254 * the amount of data a read will return.
2256 * If there's no data in either buffer, or there's
2257 * no data in the hold buffer and the timer hasn't
2258 * expired, we're not ready to read.
2260 kn
->kn_data
= ((d
->bd_hlen
== 0 || d
->bd_hbuf_read
) && d
->bd_state
== BPF_TIMED_OUT
?
2261 d
->bd_slen
: d
->bd_hlen
);
2262 ready
= (kn
->kn_data
> 0);
2271 bpfkqfilter(dev_t dev
, struct knote
*kn
)
2277 * Is this device a bpf?
2279 if (major(dev
) != CDEV_MAJOR
||
2280 kn
->kn_filter
!= EVFILT_READ
) {
2281 kn
->kn_flags
= EV_ERROR
;
2282 kn
->kn_data
= EINVAL
;
2286 lck_mtx_lock(bpf_mlock
);
2288 d
= bpf_dtab
[minor(dev
)];
2292 d
->bd_bif
== NULL
||
2293 (d
->bd_flags
& BPF_CLOSING
) != 0) {
2294 lck_mtx_unlock(bpf_mlock
);
2295 kn
->kn_flags
= EV_ERROR
;
2296 kn
->kn_data
= ENXIO
;
2301 kn
->kn_filtid
= EVFILTID_BPFREAD
;
2302 KNOTE_ATTACH(&d
->bd_sel
.si_note
, kn
);
2303 d
->bd_flags
|= BPF_KNOTE
;
2305 /* capture the current state */
2306 res
= filt_bpfread_common(kn
, d
);
2308 lck_mtx_unlock(bpf_mlock
);
2314 filt_bpfdetach(struct knote
*kn
)
2316 struct bpf_d
*d
= (struct bpf_d
*)kn
->kn_hook
;
2318 lck_mtx_lock(bpf_mlock
);
2319 if (d
->bd_flags
& BPF_KNOTE
) {
2320 KNOTE_DETACH(&d
->bd_sel
.si_note
, kn
);
2321 d
->bd_flags
&= ~BPF_KNOTE
;
2323 lck_mtx_unlock(bpf_mlock
);
2327 filt_bpfread(struct knote
*kn
, long hint
)
2329 #pragma unused(hint)
2330 struct bpf_d
*d
= (struct bpf_d
*)kn
->kn_hook
;
2332 return filt_bpfread_common(kn
, d
);
2336 filt_bpftouch(struct knote
*kn
, struct kevent_internal_s
*kev
)
2338 struct bpf_d
*d
= (struct bpf_d
*)kn
->kn_hook
;
2341 lck_mtx_lock(bpf_mlock
);
2343 /* save off the lowat threshold and flag */
2344 kn
->kn_sdata
= kev
->data
;
2345 kn
->kn_sfflags
= kev
->fflags
;
2346 if ((kn
->kn_status
& KN_UDATA_SPECIFIC
) == 0)
2347 kn
->kn_udata
= kev
->udata
;
2349 /* output data will be re-generated here */
2350 res
= filt_bpfread_common(kn
, d
);
2352 lck_mtx_unlock(bpf_mlock
);
2358 filt_bpfprocess(struct knote
*kn
, struct filt_process_s
*data
, struct kevent_internal_s
*kev
)
2360 #pragma unused(data)
2361 struct bpf_d
*d
= (struct bpf_d
*)kn
->kn_hook
;
2364 lck_mtx_lock(bpf_mlock
);
2365 res
= filt_bpfread_common(kn
, d
);
2367 *kev
= kn
->kn_kevent
;
2369 lck_mtx_unlock(bpf_mlock
);
2375 * Copy data from an mbuf chain into a buffer. This code is derived
2376 * from m_copydata in sys/uipc_mbuf.c.
2379 bpf_mcopy(const void *src_arg
, void *dst_arg
, size_t len
)
2381 struct mbuf
*m
= (struct mbuf
*)(uintptr_t)(src_arg
);
2389 count
= min(m
->m_len
, len
);
2390 bcopy(mbuf_data(m
), dst
, count
);
2407 struct mbuf
*savedm
= m
;
2410 * It's possible that we get here after the bpf descriptor has been
2411 * detached from the interface; in such a case we simply return.
2412 * Lock ordering is important since we can be called asynchronously
2413 * (from the IOKit) to process an inbound packet; when that happens
2414 * we would have been holding its "gateLock" and will be acquiring
2415 * "bpf_mlock" upon entering this routine. Due to that, we release
2416 * "bpf_mlock" prior to calling ifnet_set_promiscuous (which will
2417 * acquire "gateLock" in the IOKit), in order to avoid a deadlock
2418 * when a ifnet_set_promiscuous request simultaneously collides with
2419 * an inbound packet being passed into the tap callback.
2421 lck_mtx_lock(bpf_mlock
);
2422 if (ifp
->if_bpf
== NULL
) {
2423 lck_mtx_unlock(bpf_mlock
);
2427 for (bp
= ifp
->if_bpf
; bp
&& bp
->bif_ifp
== ifp
&&
2428 (dlt
!= 0 && bp
->bif_dlt
!= dlt
); bp
= bp
->bif_next
)
2430 if (bp
&& bp
->bif_ifp
== ifp
&& bp
->bif_dlist
!= NULL
) {
2432 struct m_hdr hack_hdr
;
2439 * This is gross. We mock up an mbuf that points to the
2440 * header buffer. This means we don't have to copy the
2441 * header. A number of interfaces prepended headers just
2442 * for bpf by allocating an mbuf on the stack. We want to
2443 * give developers an easy way to prepend a header for bpf.
2444 * Since a developer allocating an mbuf on the stack is bad,
2445 * we do even worse here, allocating only a header to point
2446 * to a buffer the developer supplied. This makes assumptions
2447 * that bpf_filter and catchpacket will not look at anything
2448 * in the mbuf other than the header. This was true at the
2449 * time this code was written.
2451 hack_hdr
.mh_next
= m
;
2452 hack_hdr
.mh_nextpkt
= NULL
;
2453 hack_hdr
.mh_len
= hlen
;
2454 hack_hdr
.mh_data
= hdr
;
2455 hack_hdr
.mh_type
= m
->m_type
;
2456 hack_hdr
.mh_flags
= 0;
2458 __IGNORE_WCASTALIGN(m
= (mbuf_t
)&hack_hdr
);
2461 for (m0
= m
; m0
!= 0; m0
= m0
->m_next
)
2462 pktlen
+= m0
->m_len
;
2464 for (d
= bp
->bif_dlist
; d
; d
= d
->bd_next
) {
2465 if (outbound
&& !d
->bd_seesent
)
2468 slen
= bpf_filter(d
->bd_filter
, (u_char
*)m
, pktlen
, 0);
2471 if (mac_bpfdesc_check_receive(d
, bp
->bif_ifp
) != 0)
2474 catchpacket(d
, (u_char
*)m
, savedm
, pktlen
,
2475 slen
, outbound
, bpf_mcopy
);
2479 lck_mtx_unlock(bpf_mlock
);
2490 bpf_tap_imp(ifp
, dlt
, m
, hdr
, hlen
, 1);
2501 bpf_tap_imp(ifp
, dlt
, m
, hdr
, hlen
, 0);
2504 /* Callback registered with Ethernet driver. */
2505 static int bpf_tap_callback(struct ifnet
*ifp
, struct mbuf
*m
)
2507 bpf_tap_imp(ifp
, 0, m
, NULL
, 0, mbuf_pkthdr_rcvif(m
) == NULL
);
2513 * Move the packet data from interface memory (pkt) into the
2514 * store buffer. Return 1 if it's time to wakeup a listener (buffer full),
2515 * otherwise 0. "copy" is the routine called to do the actual data
2516 * transfer. bcopy is passed in to copy contiguous chunks, while
2517 * bpf_mcopy is passed in to copy mbuf chains. In the latter case,
2518 * pkt is really an mbuf.
2521 catchpacket(struct bpf_d
*d
, u_char
*pkt
, struct mbuf
*m
, u_int pktlen
,
2522 u_int snaplen
, int outbound
,
2523 void (*cpfn
)(const void *, void *, size_t))
2526 struct bpf_hdr_ext
*ehp
;
2532 struct m_tag
*mt
= NULL
;
2533 struct bpf_mtag
*bt
= NULL
;
2535 hdrlen
= (d
->bd_flags
& BPF_EXTENDED_HDR
) ? d
->bd_bif
->bif_exthdrlen
:
2536 d
->bd_bif
->bif_hdrlen
;
2538 * Figure out how many bytes to move. If the packet is
2539 * greater or equal to the snapshot length, transfer that
2540 * much. Otherwise, transfer the whole packet (unless
2541 * we hit the buffer size limit).
2543 totlen
= hdrlen
+ min(snaplen
, pktlen
);
2544 if (totlen
> d
->bd_bufsize
)
2545 totlen
= d
->bd_bufsize
;
2548 * Round up the end of the previous packet to the next longword.
2550 curlen
= BPF_WORDALIGN(d
->bd_slen
);
2551 if (curlen
+ totlen
> d
->bd_bufsize
) {
2553 * This packet will overflow the storage buffer.
2554 * Rotate the buffers if we can, then wakeup any
2557 * We cannot rotate buffers if a read is in progress
2558 * so drop the packet
2560 if (d
->bd_hbuf_read
) {
2565 if (d
->bd_fbuf
== NULL
) {
2566 if (d
->bd_headdrop
== 0) {
2568 * We haven't completed the previous read yet,
2569 * so drop the packet.
2575 * Drop the hold buffer as it contains older packets
2577 d
->bd_dcount
+= d
->bd_hcnt
;
2578 d
->bd_fbuf
= d
->bd_hbuf
;
2586 else if (d
->bd_immediate
|| d
->bd_state
== BPF_TIMED_OUT
)
2588 * Immediate mode is set, or the read timeout has
2589 * already expired during a select call. A packet
2590 * arrived, so the reader should be woken up.
2595 * Append the bpf header.
2598 if (d
->bd_flags
& BPF_EXTENDED_HDR
) {
2599 ehp
= (struct bpf_hdr_ext
*)(void *)(d
->bd_sbuf
+ curlen
);
2600 memset(ehp
, 0, sizeof(*ehp
));
2601 ehp
->bh_tstamp
.tv_sec
= tv
.tv_sec
;
2602 ehp
->bh_tstamp
.tv_usec
= tv
.tv_usec
;
2603 ehp
->bh_datalen
= pktlen
;
2604 ehp
->bh_hdrlen
= hdrlen
;
2605 ehp
->bh_caplen
= totlen
- hdrlen
;
2606 mt
= m_tag_locate(m
, bpf_mtag_id
, 0, NULL
);
2607 if (mt
&& mt
->m_tag_len
>= sizeof(*bt
)) {
2608 bt
= (struct bpf_mtag
*)(mt
+ 1);
2609 ehp
->bh_pid
= bt
->bt_pid
;
2610 strlcpy(ehp
->bh_comm
, bt
->bt_comm
,
2611 sizeof(ehp
->bh_comm
));
2612 ehp
->bh_svc
= so_svc2tc(bt
->bt_svc
);
2613 if (bt
->bt_direction
== BPF_MTAG_DIR_OUT
)
2614 ehp
->bh_flags
|= BPF_HDR_EXT_FLAGS_DIR_OUT
;
2616 ehp
->bh_flags
|= BPF_HDR_EXT_FLAGS_DIR_IN
;
2617 m_tag_delete(m
, mt
);
2618 } else if (outbound
) {
2619 /* only do lookups on non-raw INPCB */
2620 if ((m
->m_pkthdr
.pkt_flags
& (PKTF_FLOW_ID
|
2621 PKTF_FLOW_LOCALSRC
|PKTF_FLOW_RAWSOCK
)) ==
2622 (PKTF_FLOW_ID
|PKTF_FLOW_LOCALSRC
) &&
2623 m
->m_pkthdr
.pkt_flowsrc
== FLOWSRC_INPCB
) {
2624 ehp
->bh_flowid
= m
->m_pkthdr
.pkt_flowid
;
2625 ehp
->bh_proto
= m
->m_pkthdr
.pkt_proto
;
2627 ehp
->bh_svc
= so_svc2tc(m
->m_pkthdr
.pkt_svc
);
2628 ehp
->bh_flags
|= BPF_HDR_EXT_FLAGS_DIR_OUT
;
2629 if (m
->m_pkthdr
.pkt_flags
& PKTF_TCP_REXMT
)
2630 ehp
->bh_pktflags
|= BPF_PKTFLAGS_TCP_REXMT
;
2631 if (m
->m_pkthdr
.pkt_flags
& PKTF_START_SEQ
)
2632 ehp
->bh_pktflags
|= BPF_PKTFLAGS_START_SEQ
;
2633 if (m
->m_pkthdr
.pkt_flags
& PKTF_LAST_PKT
)
2634 ehp
->bh_pktflags
|= BPF_PKTFLAGS_LAST_PKT
;
2635 if (m
->m_pkthdr
.pkt_flags
& PKTF_VALID_UNSENT_DATA
) {
2636 ehp
->bh_unsent_bytes
=
2637 m
->m_pkthdr
.bufstatus_if
;
2638 ehp
->bh_unsent_snd
=
2639 m
->m_pkthdr
.bufstatus_sndbuf
;
2642 ehp
->bh_flags
|= BPF_HDR_EXT_FLAGS_DIR_IN
;
2643 payload
= (u_char
*)ehp
+ hdrlen
;
2644 caplen
= ehp
->bh_caplen
;
2646 hp
= (struct bpf_hdr
*)(void *)(d
->bd_sbuf
+ curlen
);
2647 hp
->bh_tstamp
.tv_sec
= tv
.tv_sec
;
2648 hp
->bh_tstamp
.tv_usec
= tv
.tv_usec
;
2649 hp
->bh_datalen
= pktlen
;
2650 hp
->bh_hdrlen
= hdrlen
;
2651 hp
->bh_caplen
= totlen
- hdrlen
;
2652 payload
= (u_char
*)hp
+ hdrlen
;
2653 caplen
= hp
->bh_caplen
;
2656 * Copy the packet data into the store buffer and update its length.
2658 (*cpfn
)(pkt
, payload
, caplen
);
2659 d
->bd_slen
= curlen
+ totlen
;
2667 * Initialize all nonzero fields of a descriptor.
2670 bpf_allocbufs(struct bpf_d
*d
)
2672 if (d
->bd_sbuf
!= NULL
) {
2673 FREE(d
->bd_sbuf
, M_DEVBUF
);
2676 if (d
->bd_hbuf
!= NULL
) {
2677 FREE(d
->bd_hbuf
, M_DEVBUF
);
2680 if (d
->bd_fbuf
!= NULL
) {
2681 FREE(d
->bd_fbuf
, M_DEVBUF
);
2685 d
->bd_fbuf
= (caddr_t
) _MALLOC(d
->bd_bufsize
, M_DEVBUF
, M_WAIT
);
2686 if (d
->bd_fbuf
== NULL
)
2689 d
->bd_sbuf
= (caddr_t
) _MALLOC(d
->bd_bufsize
, M_DEVBUF
, M_WAIT
);
2690 if (d
->bd_sbuf
== NULL
) {
2691 FREE(d
->bd_fbuf
, M_DEVBUF
);
2703 * Free buffers currently in use by a descriptor.
2707 bpf_freed(struct bpf_d
*d
)
2710 * We don't need to lock out interrupts since this descriptor has
2711 * been detached from its interface and it yet hasn't been marked
2714 if (d
->bd_hbuf_read
)
2715 panic("bpf buffer freed during read");
2717 if (d
->bd_sbuf
!= 0) {
2718 FREE(d
->bd_sbuf
, M_DEVBUF
);
2719 if (d
->bd_hbuf
!= 0)
2720 FREE(d
->bd_hbuf
, M_DEVBUF
);
2721 if (d
->bd_fbuf
!= 0)
2722 FREE(d
->bd_fbuf
, M_DEVBUF
);
2725 FREE((caddr_t
)d
->bd_filter
, M_DEVBUF
);
2729 * Attach an interface to bpf. driverp is a pointer to a (struct bpf_if *)
2730 * in the driver's softc; dlt is the link layer type; hdrlen is the fixed
2731 * size of the link header (variable length headers not yet supported).
2734 bpfattach(struct ifnet
*ifp
, u_int dlt
, u_int hdrlen
)
2736 bpf_attach(ifp
, dlt
, hdrlen
, NULL
, NULL
);
2747 struct bpf_if
*bp_new
;
2748 struct bpf_if
*bp_temp
;
2749 struct bpf_if
*bp_first
= NULL
;
2751 bp_new
= (struct bpf_if
*) _MALLOC(sizeof(*bp_new
), M_DEVBUF
,
2756 lck_mtx_lock(bpf_mlock
);
2759 * Check if this interface/dlt is already attached, record first
2760 * attachment for this interface.
2762 for (bp_temp
= bpf_iflist
; bp_temp
&& (bp_temp
->bif_ifp
!= ifp
||
2763 bp_temp
->bif_dlt
!= dlt
); bp_temp
= bp_temp
->bif_next
) {
2764 if (bp_temp
->bif_ifp
== ifp
&& bp_first
== NULL
)
2768 if (bp_temp
!= NULL
) {
2769 printf("bpfattach - %s with dlt %d is already attached\n",
2771 FREE(bp_new
, M_DEVBUF
);
2772 lck_mtx_unlock(bpf_mlock
);
2776 bp_new
->bif_ifp
= ifp
;
2777 bp_new
->bif_dlt
= dlt
;
2778 bp_new
->bif_send
= send
;
2779 bp_new
->bif_tap
= tap
;
2781 if (bp_first
== NULL
) {
2782 /* No other entries for this ifp */
2783 bp_new
->bif_next
= bpf_iflist
;
2784 bpf_iflist
= bp_new
;
2787 /* Add this after the first entry for this interface */
2788 bp_new
->bif_next
= bp_first
->bif_next
;
2789 bp_first
->bif_next
= bp_new
;
2793 * Compute the length of the bpf header. This is not necessarily
2794 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
2795 * that the network layer header begins on a longword boundary (for
2796 * performance reasons and to alleviate alignment restrictions).
2798 bp_new
->bif_hdrlen
= BPF_WORDALIGN(hdrlen
+ SIZEOF_BPF_HDR
) - hdrlen
;
2799 bp_new
->bif_exthdrlen
= BPF_WORDALIGN(hdrlen
+
2800 sizeof(struct bpf_hdr_ext
)) - hdrlen
;
2802 /* Take a reference on the interface */
2803 ifnet_reference(ifp
);
2805 lck_mtx_unlock(bpf_mlock
);
2809 printf("bpf: %s attached\n", if_name(ifp
));
2816 * Detach bpf from an interface. This involves detaching each descriptor
2817 * associated with the interface, and leaving bd_bif NULL. Notify each
2818 * descriptor as it's detached so that any sleepers wake up and get
2822 bpfdetach(struct ifnet
*ifp
)
2824 struct bpf_if
*bp
, *bp_prev
, *bp_next
;
2829 __func__
, if_name(ifp
));
2831 lck_mtx_lock(bpf_mlock
);
2834 * Build the list of devices attached to that interface
2835 * that we need to free while keeping the lock to maintain
2836 * the integrity of the interface list
2839 for (bp
= bpf_iflist
; bp
!= NULL
; bp
= bp_next
) {
2840 bp_next
= bp
->bif_next
;
2842 if (ifp
!= bp
->bif_ifp
) {
2846 /* Unlink from the interface list */
2848 bp_prev
->bif_next
= bp
->bif_next
;
2850 bpf_iflist
= bp
->bif_next
;
2852 /* Detach the devices attached to the interface */
2853 while ((d
= bp
->bif_dlist
) != NULL
) {
2855 * Take an extra reference to prevent the device
2856 * from being freed when bpf_detachd() releases
2857 * the reference for the interface list
2867 lck_mtx_unlock(bpf_mlock
);
2871 bpf_init(__unused
void *unused
)
2877 if (bpf_devsw_installed
== 0) {
2878 bpf_devsw_installed
= 1;
2879 bpf_mlock_grp_attr
= lck_grp_attr_alloc_init();
2880 bpf_mlock_grp
= lck_grp_alloc_init("bpf", bpf_mlock_grp_attr
);
2881 bpf_mlock_attr
= lck_attr_alloc_init();
2882 lck_mtx_init(bpf_mlock
, bpf_mlock_grp
, bpf_mlock_attr
);
2883 maj
= cdevsw_add(CDEV_MAJOR
, &bpf_cdevsw
);
2886 lck_attr_free(bpf_mlock_attr
);
2888 lck_grp_free(bpf_mlock_grp
);
2889 if (bpf_mlock_grp_attr
)
2890 lck_grp_attr_free(bpf_mlock_grp_attr
);
2893 bpf_mlock_attr
= NULL
;
2894 bpf_mlock_grp
= NULL
;
2895 bpf_mlock_grp_attr
= NULL
;
2896 bpf_devsw_installed
= 0;
2897 printf("bpf_init: failed to allocate a major number!\n");
2901 for (i
= 0 ; i
< NBPFILTER
; i
++)
2902 bpf_make_dev_t(maj
);
2904 VERIFY(mbuf_tag_id_find(BPF_CONTROL_NAME
, &bpf_mtag_id
) == 0);
2907 cdevsw_add(&bpf_cdevsw
);
2912 SYSINIT(bpfdev
,SI_SUB_DRIVERS
,SI_ORDER_MIDDLE
+CDEV_MAJOR
,bpf_drvinit
,NULL
)
2917 mac_bpfdesc_label_get(struct bpf_d
*d
)
2920 return (d
->bd_label
);
2924 mac_bpfdesc_label_set(struct bpf_d
*d
, struct label
*label
)
2927 d
->bd_label
= label
;