2 * Copyright (c) 2000-2013 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>
126 #include <kern/locks.h>
127 #include <kern/thread_call.h>
130 #include <security/mac_framework.h>
133 extern int tvtohz(struct timeval
*);
135 #define BPF_BUFSIZE 4096
136 #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, uio)
139 #define PRINET 26 /* interruptible */
142 * The default read buffer size is patchable.
144 static unsigned int bpf_bufsize
= BPF_BUFSIZE
;
145 SYSCTL_INT(_debug
, OID_AUTO
, bpf_bufsize
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
146 &bpf_bufsize
, 0, "");
147 __private_extern__
unsigned int bpf_maxbufsize
= BPF_MAXBUFSIZE
;
148 SYSCTL_INT(_debug
, OID_AUTO
, bpf_maxbufsize
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
149 &bpf_maxbufsize
, 0, "");
150 static unsigned int bpf_maxdevices
= 256;
151 SYSCTL_UINT(_debug
, OID_AUTO
, bpf_maxdevices
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
152 &bpf_maxdevices
, 0, "");
155 * bpf_iflist is the list of interfaces; each corresponds to an ifnet
156 * bpf_dtab holds pointer to the descriptors, indexed by minor device #
158 static struct bpf_if
*bpf_iflist
;
161 * BSD now stores the bpf_d in the dev_t which is a struct
162 * on their system. Our dev_t is an int, so we still store
163 * the bpf_d in a separate table indexed by minor device #.
165 * The value stored in bpf_dtab[n] represent three states:
166 * 0: device not opened
167 * 1: device opening or closing
168 * other: device <n> opened with pointer to storage
170 static struct bpf_d
**bpf_dtab
= NULL
;
171 static unsigned int bpf_dtab_size
= 0;
172 static unsigned int nbpfilter
= 0;
174 decl_lck_mtx_data(static, bpf_mlock_data
);
175 static lck_mtx_t
*bpf_mlock
= &bpf_mlock_data
;
176 static lck_grp_t
*bpf_mlock_grp
;
177 static lck_grp_attr_t
*bpf_mlock_grp_attr
;
178 static lck_attr_t
*bpf_mlock_attr
;
180 static mbuf_tag_id_t bpf_mtag_id
;
181 #endif /* __APPLE__ */
183 static int bpf_allocbufs(struct bpf_d
*);
184 static errno_t
bpf_attachd(struct bpf_d
*d
, struct bpf_if
*bp
);
185 static void bpf_detachd(struct bpf_d
*d
);
186 static void bpf_freed(struct bpf_d
*);
187 static void bpf_mcopy(const void *, void *, size_t);
188 static int bpf_movein(struct uio
*, int,
189 struct mbuf
**, struct sockaddr
*, int *);
190 static int bpf_setif(struct bpf_d
*, ifnet_t ifp
, u_int32_t dlt
, dev_t
);
191 static void bpf_timed_out(void *, void *);
192 static void bpf_wakeup(struct bpf_d
*);
193 static void catchpacket(struct bpf_d
*, u_char
*, struct mbuf
*, u_int
,
194 u_int
, int, void (*)(const void *, void *, size_t));
195 static void reset_d(struct bpf_d
*);
196 static int bpf_setf(struct bpf_d
*, u_int
, user_addr_t
, dev_t
, u_long
);
197 static int bpf_getdltlist(struct bpf_d
*, caddr_t
, struct proc
*);
198 static int bpf_setdlt(struct bpf_d
*, u_int
, dev_t
);
199 static int bpf_set_traffic_class(struct bpf_d
*, int);
200 static void bpf_set_packet_service_class(struct mbuf
*, int);
202 /*static void *bpf_devfs_token[MAXBPFILTER];*/
204 static int bpf_devsw_installed
;
206 void bpf_init(void *unused
);
207 static int bpf_tap_callback(struct ifnet
*ifp
, struct mbuf
*m
);
210 * Darwin differs from BSD here, the following are static
211 * on BSD and not static on Darwin.
217 ioctl_fcn_t bpfioctl
;
218 select_fcn_t bpfselect
;
221 /* Darwin's cdevsw struct differs slightly from BSDs */
222 #define CDEV_MAJOR 23
223 static struct cdevsw bpf_cdevsw
= {
225 /* close */ bpfclose
,
227 /* write */ bpfwrite
,
228 /* ioctl */ bpfioctl
,
230 /* reset */ eno_reset
,
232 /* select */ bpfselect
,
234 /* strategy*/ eno_strat
,
240 #define SOCKADDR_HDR_LEN offsetof(struct sockaddr, sa_data)
243 bpf_movein(struct uio
*uio
, int linktype
, struct mbuf
**mp
, struct sockaddr
*sockp
, int *datlen
)
261 sa_family
= AF_UNSPEC
;
262 /* XXX Would MAXLINKHDR be better? */
263 hlen
= sizeof(struct ether_header
);
268 #if defined(__FreeBSD__) || defined(__bsdi__)
269 sa_family
= AF_IMPLINK
;
272 sa_family
= AF_UNSPEC
;
273 /* XXX 4(FORMAC)+6(dst)+6(src)+3(LLC)+5(SNAP) */
281 sa_family
= AF_UNSPEC
;
286 case DLT_ATM_RFC1483
:
288 * en atm driver requires 4-byte atm pseudo header.
289 * though it isn't standard, vpi:vci needs to be
292 sa_family
= AF_UNSPEC
;
293 hlen
= 12; /* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
298 sa_family
= AF_UNSPEC
;
299 hlen
= 4; /* This should match PPP_HDRLEN */
302 case DLT_APPLE_IP_OVER_IEEE1394
:
303 sa_family
= AF_UNSPEC
;
304 hlen
= sizeof(struct firewire_header
);
307 case DLT_IEEE802_11
: /* IEEE 802.11 wireless */
308 sa_family
= AF_IEEE80211
;
312 case DLT_IEEE802_11_RADIO
:
313 sa_family
= AF_IEEE80211
;
321 // LP64todo - fix this!
322 len
= uio_resid(uio
);
323 *datlen
= len
- hlen
;
324 if ((unsigned)len
> MCLBYTES
)
329 * Build a sockaddr based on the data link layer type.
330 * We do this at this level because the ethernet header
331 * is copied directly into the data field of the sockaddr.
332 * In the case of SLIP, there is no header and the packet
333 * is forwarded as is.
334 * Also, we are careful to leave room at the front of the mbuf
335 * for the link level header.
337 if ((hlen
+ SOCKADDR_HDR_LEN
) > sockp
->sa_len
) {
340 sockp
->sa_family
= sa_family
;
343 * We're directly sending the packet data supplied by
344 * the user; we don't need to make room for the link
345 * header, and don't need the header length value any
346 * more, so set it to 0.
351 MGETHDR(m
, M_WAIT
, MT_DATA
);
354 if ((unsigned)len
> MHLEN
) {
356 if ((m
->m_flags
& M_EXT
) == 0) {
361 m
->m_pkthdr
.len
= m
->m_len
= len
;
362 m
->m_pkthdr
.rcvif
= NULL
;
366 * Make room for link header.
369 m
->m_pkthdr
.len
-= hlen
;
371 m
->m_data
+= hlen
; /* XXX */
372 error
= UIOMOVE((caddr_t
)sockp
->sa_data
, hlen
, UIO_WRITE
, uio
);
376 error
= UIOMOVE(mtod(m
, caddr_t
), len
- hlen
, UIO_WRITE
, uio
);
380 /* Check for multicast destination */
383 struct ether_header
*eh
= mtod(m
, struct ether_header
*);
385 if (ETHER_IS_MULTICAST(eh
->ether_dhost
)) {
386 if (_ether_cmp(etherbroadcastaddr
, eh
->ether_dhost
) == 0)
387 m
->m_flags
|= M_BCAST
;
389 m
->m_flags
|= M_MCAST
;
404 * The dynamic addition of a new device node must block all processes that
405 * are opening the last device so that no process will get an unexpected
409 bpf_make_dev_t(int maj
)
411 static int bpf_growing
= 0;
412 unsigned int cur_size
= nbpfilter
, i
;
414 if (nbpfilter
>= bpf_maxdevices
)
417 while (bpf_growing
) {
418 /* Wait until new device has been created */
419 (void)tsleep((caddr_t
)&bpf_growing
, PZERO
, "bpf_growing", 0);
421 if (nbpfilter
> cur_size
) {
422 /* other thread grew it already */
427 /* need to grow bpf_dtab first */
428 if (nbpfilter
== bpf_dtab_size
) {
430 struct bpf_d
**new_dtab
= NULL
;
431 struct bpf_d
**old_dtab
= NULL
;
433 new_dtab_size
= bpf_dtab_size
+ NBPFILTER
;
434 new_dtab
= (struct bpf_d
**)_MALLOC(sizeof(struct bpf_d
*) * new_dtab_size
, M_DEVBUF
, M_WAIT
);
436 printf("bpf_make_dev_t: malloc bpf_dtab failed\n");
440 bcopy(bpf_dtab
, new_dtab
,
441 sizeof(struct bpf_d
*) * bpf_dtab_size
);
443 bzero(new_dtab
+ bpf_dtab_size
,
444 sizeof(struct bpf_d
*) * NBPFILTER
);
447 bpf_dtab_size
= new_dtab_size
;
448 if (old_dtab
!= NULL
)
449 _FREE(old_dtab
, M_DEVBUF
);
452 (void) devfs_make_node(makedev(maj
, i
),
453 DEVFS_CHAR
, UID_ROOT
, GID_WHEEL
, 0600,
457 wakeup((caddr_t
)&bpf_growing
);
463 * Attach file to the bpf interface, i.e. make d listen on bp.
466 bpf_attachd(struct bpf_d
*d
, struct bpf_if
*bp
)
468 int first
= bp
->bif_dlist
== NULL
;
472 * Point d at bp, and add d to the interface's list of listeners.
473 * Finally, point the driver's bpf cookie at the interface so
474 * it will divert packets to bpf.
477 d
->bd_next
= bp
->bif_dlist
;
481 /* Find the default bpf entry for this ifp */
482 if (bp
->bif_ifp
->if_bpf
== NULL
) {
483 struct bpf_if
*primary
;
485 for (primary
= bpf_iflist
; primary
&& primary
->bif_ifp
!= bp
->bif_ifp
;
486 primary
= primary
->bif_next
)
489 bp
->bif_ifp
->if_bpf
= primary
;
492 /* Only call dlil_set_bpf_tap for primary dlt */
493 if (bp
->bif_ifp
->if_bpf
== bp
)
494 dlil_set_bpf_tap(bp
->bif_ifp
, BPF_TAP_INPUT_OUTPUT
, bpf_tap_callback
);
497 error
= bp
->bif_tap(bp
->bif_ifp
, bp
->bif_dlt
, BPF_TAP_INPUT_OUTPUT
);
504 * Detach a file from its interface.
507 bpf_detachd(struct bpf_d
*d
)
513 ifp
= d
->bd_bif
->bif_ifp
;
516 /* Remove d from the interface's descriptor list. */
521 panic("bpf_detachd: descriptor not in list");
524 if (bp
->bif_dlist
== 0) {
526 * Let the driver know that there are no more listeners.
528 /* Only call dlil_set_bpf_tap for primary dlt */
529 if (bp
->bif_ifp
->if_bpf
== bp
)
530 dlil_set_bpf_tap(ifp
, BPF_TAP_DISABLE
, NULL
);
532 bp
->bif_tap(ifp
, bp
->bif_dlt
, BPF_TAP_DISABLE
);
534 for (bp
= bpf_iflist
; bp
; bp
= bp
->bif_next
)
535 if (bp
->bif_ifp
== ifp
&& bp
->bif_dlist
!= 0)
542 * Check if this descriptor had requested promiscuous mode.
543 * If so, turn it off.
547 lck_mtx_unlock(bpf_mlock
);
548 if (ifnet_set_promiscuous(ifp
, 0)) {
550 * Something is really wrong if we were able to put
551 * the driver into promiscuous mode, but can't
553 * Most likely the network interface is gone.
555 printf("bpf: ifnet_set_promiscuous failed");
557 lck_mtx_lock(bpf_mlock
);
563 * Start asynchronous timer, if necessary.
564 * Must be called with bpf_mlock held.
567 bpf_start_timer(struct bpf_d
*d
)
572 if (d
->bd_rtout
> 0 && d
->bd_state
== BPF_IDLE
) {
573 tv
.tv_sec
= d
->bd_rtout
/ hz
;
574 tv
.tv_usec
= (d
->bd_rtout
% hz
) * tick
;
576 clock_interval_to_deadline(
577 (uint64_t)tv
.tv_sec
* USEC_PER_SEC
+ tv
.tv_usec
,
578 NSEC_PER_USEC
, &deadline
);
580 * The state is BPF_IDLE, so the timer hasn't
581 * been started yet, and hasn't gone off yet;
582 * there is no thread call scheduled, so this
583 * won't change the schedule.
585 * XXX - what if, by the time it gets entered,
586 * the deadline has already passed?
588 thread_call_enter_delayed(d
->bd_thread_call
, deadline
);
589 d
->bd_state
= BPF_WAITING
;
594 * Cancel asynchronous timer.
595 * Must be called with bpf_mlock held.
598 bpf_stop_timer(struct bpf_d
*d
)
601 * If the timer has already gone off, this does nothing.
602 * Our caller is expected to set d->bd_state to BPF_IDLE,
603 * with the bpf_mlock, after we are called. bpf_timed_out()
604 * also grabs bpf_mlock, so, if the timer has gone off and
605 * bpf_timed_out() hasn't finished, it's waiting for the
606 * lock; when this thread releases the lock, it will
607 * find the state is BPF_IDLE, and just release the
610 return (thread_call_cancel(d
->bd_thread_call
));
616 * Open ethernet device. Returns ENXIO for illegal minor device number,
617 * EBUSY if file is open by another process.
621 bpfopen(dev_t dev
, int flags
, __unused
int fmt
,
622 __unused
struct proc
*p
)
626 lck_mtx_lock(bpf_mlock
);
627 if ((unsigned int) minor(dev
) >= nbpfilter
) {
628 lck_mtx_unlock(bpf_mlock
);
632 * New device nodes are created on demand when opening the last one.
633 * The programming model is for processes to loop on the minor starting at 0
634 * as long as EBUSY is returned. The loop stops when either the open succeeds or
635 * an error other that EBUSY is returned. That means that bpf_make_dev_t() must
636 * block all processes that are opening the last node. If not all
637 * processes are blocked, they could unexpectedly get ENOENT and abort their
640 if ((unsigned int) minor(dev
) == (nbpfilter
- 1))
641 bpf_make_dev_t(major(dev
));
644 * Each minor can be opened by only one process. If the requested
645 * minor is in use, return EBUSY.
647 * Important: bpfopen() and bpfclose() have to check and set the status of a device
648 * in the same lockin context otherwise the device may be leaked because the vnode use count
649 * will be unpextectly greater than 1 when close() is called.
651 if (bpf_dtab
[minor(dev
)] == 0) {
652 bpf_dtab
[minor(dev
)] = (void *)1; /* Mark opening */
654 lck_mtx_unlock(bpf_mlock
);
657 d
= (struct bpf_d
*)_MALLOC(sizeof(struct bpf_d
), M_DEVBUF
, M_WAIT
);
659 /* this really is a catastrophic failure */
660 printf("bpfopen: malloc bpf_d failed\n");
661 bpf_dtab
[minor(dev
)] = NULL
;
662 lck_mtx_unlock(bpf_mlock
);
665 bzero(d
, sizeof(struct bpf_d
));
668 * It is not necessary to take the BPF lock here because no other
669 * thread can access the device until it is marked opened...
672 /* Mark "in use" and do most initialization. */
673 d
->bd_bufsize
= bpf_bufsize
;
676 d
->bd_oflags
= flags
;
677 d
->bd_state
= BPF_IDLE
;
678 d
->bd_thread_call
= thread_call_allocate(bpf_timed_out
, d
);
679 d
->bd_traffic_class
= SO_TC_BE
;
681 if (d
->bd_thread_call
== NULL
) {
682 printf("bpfopen: malloc thread call failed\n");
683 bpf_dtab
[minor(dev
)] = NULL
;
684 lck_mtx_unlock(bpf_mlock
);
689 mac_bpfdesc_label_init(d
);
690 mac_bpfdesc_label_associate(kauth_cred_get(), d
);
692 bpf_dtab
[minor(dev
)] = d
; /* Mark opened */
693 lck_mtx_unlock(bpf_mlock
);
699 * Close the descriptor by detaching it from its interface,
700 * deallocating its buffers, and marking it free.
704 bpfclose(dev_t dev
, __unused
int flags
, __unused
int fmt
,
705 __unused
struct proc
*p
)
709 /* Take BPF lock to ensure no other thread is using the device */
710 lck_mtx_lock(bpf_mlock
);
712 d
= bpf_dtab
[minor(dev
)];
713 if (d
== 0 || d
== (void *)1) {
714 lck_mtx_unlock(bpf_mlock
);
717 bpf_dtab
[minor(dev
)] = (void *)1; /* Mark closing */
720 * Deal with any in-progress timeouts.
722 switch (d
->bd_state
) {
725 * Not waiting for a timeout, and no timeout happened.
731 * Waiting for a timeout.
732 * Cancel any timer that has yet to go off,
733 * and mark the state as "closing".
734 * Then drop the lock to allow any timers that
735 * *have* gone off to run to completion, and wait
736 * for them to finish.
738 if (!bpf_stop_timer(d
)) {
740 * There was no pending call, so the call must
741 * have been in progress. Wait for the call to
742 * complete; we have to drop the lock while
743 * waiting. to let the in-progrss call complete
745 d
->bd_state
= BPF_DRAINING
;
746 while (d
->bd_state
== BPF_DRAINING
)
747 msleep((caddr_t
)d
, bpf_mlock
, PRINET
,
748 "bpfdraining", NULL
);
750 d
->bd_state
= BPF_IDLE
;
755 * Timer went off, and the timeout routine finished.
757 d
->bd_state
= BPF_IDLE
;
762 * Another thread is blocked on a close waiting for
763 * a timeout to finish.
764 * This "shouldn't happen", as the first thread to enter
765 * bpfclose() will set bpf_dtab[minor(dev)] to 1, and
766 * all subsequent threads should see that and fail with
769 panic("Two threads blocked in a BPF close");
775 selthreadclear(&d
->bd_sel
);
777 mac_bpfdesc_label_destroy(d
);
779 thread_call_free(d
->bd_thread_call
);
781 while (d
->bd_hbuf_read
)
782 msleep((caddr_t
)d
, bpf_mlock
, PRINET
, "bpf_reading", NULL
);
786 /* Mark free in same context as bpfopen comes to check */
787 bpf_dtab
[minor(dev
)] = NULL
; /* Mark closed */
788 lck_mtx_unlock(bpf_mlock
);
796 #define BPF_SLEEP bpf_sleep
799 bpf_sleep(struct bpf_d
*d
, int pri
, const char *wmesg
, int timo
)
801 u_int64_t abstime
= 0;
804 clock_interval_to_deadline(timo
, NSEC_PER_SEC
/ hz
, &abstime
);
806 return msleep1((caddr_t
)d
, bpf_mlock
, pri
, wmesg
, abstime
);
810 * Rotate the packet buffers in descriptor d. Move the store buffer
811 * into the hold slot, and the free buffer into the store slot.
812 * Zero the length of the new store buffer.
814 #define ROTATE_BUFFERS(d) \
815 if (d->bd_hbuf_read) \
816 panic("rotating bpf buffers during read"); \
817 (d)->bd_hbuf = (d)->bd_sbuf; \
818 (d)->bd_hlen = (d)->bd_slen; \
819 (d)->bd_sbuf = (d)->bd_fbuf; \
823 * bpfread - read next chunk of packets from buffers
826 bpfread(dev_t dev
, struct uio
*uio
, int ioflag
)
830 int timed_out
, hbuf_len
;
833 lck_mtx_lock(bpf_mlock
);
835 d
= bpf_dtab
[minor(dev
)];
836 if (d
== 0 || d
== (void *)1) {
837 lck_mtx_unlock(bpf_mlock
);
842 * Restrict application to use a buffer the same size as
845 if (uio_resid(uio
) != d
->bd_bufsize
) {
846 lck_mtx_unlock(bpf_mlock
);
850 if (d
->bd_state
== BPF_WAITING
)
853 timed_out
= (d
->bd_state
== BPF_TIMED_OUT
);
854 d
->bd_state
= BPF_IDLE
;
856 while (d
->bd_hbuf_read
)
857 msleep((caddr_t
)d
, bpf_mlock
, PRINET
, "bpf_reading", NULL
);
859 d
= bpf_dtab
[minor(dev
)];
860 if (d
== 0 || d
== (void *)1) {
861 lck_mtx_unlock(bpf_mlock
);
865 * If the hold buffer is empty, then do a timed sleep, which
866 * ends when the timeout expires or when enough packets
867 * have arrived to fill the store buffer.
869 while (d
->bd_hbuf
== 0) {
870 if ((d
->bd_immediate
|| timed_out
|| (ioflag
& IO_NDELAY
))
871 && d
->bd_slen
!= 0) {
873 * We're in immediate mode, or are reading
874 * in non-blocking mode, or a timer was
875 * started before the read (e.g., by select()
876 * or poll()) and has expired and a packet(s)
877 * either arrived since the previous
878 * read or arrived while we were asleep.
879 * Rotate the buffers and return what's here.
886 * No data is available, check to see if the bpf device
887 * is still pointed at a real interface. If not, return
888 * ENXIO so that the userland process knows to rebind
889 * it before using it again.
891 if (d
->bd_bif
== NULL
) {
892 lck_mtx_unlock(bpf_mlock
);
895 if (ioflag
& IO_NDELAY
) {
896 lck_mtx_unlock(bpf_mlock
);
897 return (EWOULDBLOCK
);
899 error
= BPF_SLEEP(d
, PRINET
|PCATCH
, "bpf",
902 * Make sure device is still opened
904 d
= bpf_dtab
[minor(dev
)];
905 if (d
== 0 || d
== (void *)1) {
906 lck_mtx_unlock(bpf_mlock
);
910 while (d
->bd_hbuf_read
)
911 msleep((caddr_t
)d
, bpf_mlock
, PRINET
, "bpf_reading", NULL
);
913 d
= bpf_dtab
[minor(dev
)];
914 if (d
== 0 || d
== (void *)1) {
915 lck_mtx_unlock(bpf_mlock
);
919 if (error
== EINTR
|| error
== ERESTART
) {
922 * Sometimes we may be interrupted often and
923 * the sleep above will not timeout.
924 * Regardless, we should rotate the buffers
925 * if there's any new data pending and
931 lck_mtx_unlock(bpf_mlock
);
934 if (error
== EWOULDBLOCK
) {
936 * On a timeout, return what's in the buffer,
937 * which may be nothing. If there is something
938 * in the store buffer, we can rotate the buffers.
942 * We filled up the buffer in between
943 * getting the timeout and arriving
944 * here, so we don't need to rotate.
948 if (d
->bd_slen
== 0) {
949 lck_mtx_unlock(bpf_mlock
);
957 * At this point, we know we have something in the hold slot.
962 * Before we move data to userland, we fill out the extended
965 if (d
->bd_extendedhdr
) {
969 while (p
< d
->bd_hbuf
+ d
->bd_hlen
) {
970 struct bpf_hdr_ext
*ehp
;
972 struct so_procinfo soprocinfo
;
975 ehp
= (struct bpf_hdr_ext
*)(void *)p
;
976 if ((flowid
= ehp
->bh_flowid
)) {
977 if (ehp
->bh_proto
== IPPROTO_TCP
)
978 found
= inp_findinpcb_procinfo(&tcbinfo
,
979 flowid
, &soprocinfo
);
980 else if (ehp
->bh_proto
== IPPROTO_UDP
)
981 found
= inp_findinpcb_procinfo(&udbinfo
,
982 flowid
, &soprocinfo
);
984 ehp
->bh_pid
= soprocinfo
.spi_pid
;
985 proc_name(ehp
->bh_pid
, ehp
->bh_comm
, MAXCOMLEN
);
989 p
+= BPF_WORDALIGN(ehp
->bh_hdrlen
+ ehp
->bh_caplen
);
994 * Set the hold buffer read. So we do not
995 * rotate the buffers until the hold buffer
996 * read is complete. Also to avoid issues resulting
997 * from page faults during disk sleep (<rdar://problem/13436396>).
1001 hbuf_len
= d
->bd_hlen
;
1002 lck_mtx_unlock(bpf_mlock
);
1005 * Move data from hold buffer into user space.
1006 * We know the entire buffer is transferred since
1007 * we checked above that the read buffer is bpf_bufsize bytes.
1009 error
= UIOMOVE(hbuf
, hbuf_len
, UIO_READ
, uio
);
1011 lck_mtx_lock(bpf_mlock
);
1013 * Make sure device is still opened
1015 d
= bpf_dtab
[minor(dev
)];
1016 if (d
== 0 || d
== (void *)1) {
1017 lck_mtx_unlock(bpf_mlock
);
1021 d
->bd_hbuf_read
= 0;
1022 d
->bd_fbuf
= d
->bd_hbuf
;
1026 lck_mtx_unlock(bpf_mlock
);
1033 * If there are processes sleeping on this descriptor, wake them up.
1036 bpf_wakeup(struct bpf_d
*d
)
1038 if (d
->bd_state
== BPF_WAITING
) {
1040 d
->bd_state
= BPF_IDLE
;
1043 if (d
->bd_async
&& d
->bd_sig
&& d
->bd_sigio
)
1044 pgsigio(d
->bd_sigio
, d
->bd_sig
);
1046 selwakeup(&d
->bd_sel
);
1047 KNOTE(&d
->bd_sel
.si_note
, 1);
1050 d
->bd_sel
.si_pid
= 0;
1056 bpf_timed_out(void *arg
, __unused
void *dummy
)
1058 struct bpf_d
*d
= (struct bpf_d
*)arg
;
1060 lck_mtx_lock(bpf_mlock
);
1061 if (d
->bd_state
== BPF_WAITING
) {
1063 * There's a select or kqueue waiting for this; if there's
1064 * now stuff to read, wake it up.
1066 d
->bd_state
= BPF_TIMED_OUT
;
1067 if (d
->bd_slen
!= 0)
1069 } else if (d
->bd_state
== BPF_DRAINING
) {
1071 * A close is waiting for this to finish.
1072 * Mark it as finished, and wake the close up.
1074 d
->bd_state
= BPF_IDLE
;
1077 lck_mtx_unlock(bpf_mlock
);
1084 /* keep in sync with bpf_movein above: */
1085 #define MAX_DATALINK_HDR_LEN (sizeof(struct firewire_header))
1088 bpfwrite(dev_t dev
, struct uio
*uio
, __unused
int ioflag
)
1092 struct mbuf
*m
= NULL
;
1094 char dst_buf
[SOCKADDR_HDR_LEN
+ MAX_DATALINK_HDR_LEN
];
1099 lck_mtx_lock(bpf_mlock
);
1101 d
= bpf_dtab
[minor(dev
)];
1102 if (d
== 0 || d
== (void *)1) {
1103 lck_mtx_unlock(bpf_mlock
);
1106 if (d
->bd_bif
== 0) {
1107 lck_mtx_unlock(bpf_mlock
);
1111 ifp
= d
->bd_bif
->bif_ifp
;
1113 if ((ifp
->if_flags
& IFF_UP
) == 0) {
1114 lck_mtx_unlock(bpf_mlock
);
1117 if (uio_resid(uio
) == 0) {
1118 lck_mtx_unlock(bpf_mlock
);
1121 ((struct sockaddr
*)dst_buf
)->sa_len
= sizeof(dst_buf
);
1124 * fix for PR-6849527
1125 * geting variables onto stack before dropping lock for bpf_movein()
1127 bif_dlt
= (int)d
->bd_bif
->bif_dlt
;
1128 bd_hdrcmplt
= d
->bd_hdrcmplt
;
1130 /* bpf_movein allocating mbufs; drop lock */
1131 lck_mtx_unlock(bpf_mlock
);
1133 error
= bpf_movein(uio
, bif_dlt
, &m
,
1134 bd_hdrcmplt
? NULL
: (struct sockaddr
*)dst_buf
,
1141 /* taking the lock again and verifying whether device is open */
1142 lck_mtx_lock(bpf_mlock
);
1143 d
= bpf_dtab
[minor(dev
)];
1144 if (d
== 0 || d
== (void *)1) {
1145 lck_mtx_unlock(bpf_mlock
);
1150 if (d
->bd_bif
== NULL
) {
1151 lck_mtx_unlock(bpf_mlock
);
1156 if ((unsigned)datlen
> ifp
->if_mtu
) {
1157 lck_mtx_unlock(bpf_mlock
);
1164 mac_mbuf_label_associate_bpfdesc(d
, m
);
1167 bpf_set_packet_service_class(m
, d
->bd_traffic_class
);
1169 lck_mtx_unlock(bpf_mlock
);
1171 if (d
->bd_hdrcmplt
) {
1172 if (d
->bd_bif
->bif_send
)
1173 error
= d
->bd_bif
->bif_send(ifp
, d
->bd_bif
->bif_dlt
, m
);
1175 error
= dlil_output(ifp
, 0, m
, NULL
, NULL
, 1, NULL
);
1177 error
= dlil_output(ifp
, PF_INET
, m
, NULL
,
1178 (struct sockaddr
*)dst_buf
, 0, NULL
);
1182 * The driver frees the mbuf.
1188 * Reset a descriptor by flushing its packet buffer and clearing the
1189 * receive and drop counts.
1192 reset_d(struct bpf_d
*d
)
1194 if (d
->bd_hbuf_read
)
1195 panic("resetting buffers during read");
1198 /* Free the hold buffer. */
1199 d
->bd_fbuf
= d
->bd_hbuf
;
1209 * FIONREAD Check for read packet available.
1210 * SIOCGIFADDR Get interface address - convenient hook to driver.
1211 * BIOCGBLEN Get buffer len [for read()].
1212 * BIOCSETF Set ethernet read filter.
1213 * BIOCFLUSH Flush read packet buffer.
1214 * BIOCPROMISC Put interface into promiscuous mode.
1215 * BIOCGDLT Get link layer type.
1216 * BIOCGETIF Get interface name.
1217 * BIOCSETIF Set interface.
1218 * BIOCSRTIMEOUT Set read timeout.
1219 * BIOCGRTIMEOUT Get read timeout.
1220 * BIOCGSTATS Get packet stats.
1221 * BIOCIMMEDIATE Set immediate mode.
1222 * BIOCVERSION Get filter language version.
1223 * BIOCGHDRCMPLT Get "header already complete" flag
1224 * BIOCSHDRCMPLT Set "header already complete" flag
1225 * BIOCGSEESENT Get "see packets sent" flag
1226 * BIOCSSEESENT Set "see packets sent" flag
1227 * BIOCSETTC Set traffic class.
1228 * BIOCGETTC Get traffic class.
1229 * BIOCSEXTHDR Set "extended header" flag
1233 bpfioctl(dev_t dev
, u_long cmd
, caddr_t addr
, __unused
int flags
,
1237 int error
= 0, int_arg
;
1240 lck_mtx_lock(bpf_mlock
);
1242 d
= bpf_dtab
[minor(dev
)];
1243 if (d
== 0 || d
== (void *)1) {
1244 lck_mtx_unlock(bpf_mlock
);
1248 if (d
->bd_state
== BPF_WAITING
)
1250 d
->bd_state
= BPF_IDLE
;
1259 * Check for read packet available.
1261 case FIONREAD
: /* int */
1266 if (d
->bd_hbuf
&& d
->bd_hbuf_read
== 0)
1269 bcopy(&n
, addr
, sizeof (n
));
1273 case SIOCGIFADDR
: /* struct ifreq */
1280 ifp
= d
->bd_bif
->bif_ifp
;
1281 error
= ifnet_ioctl(ifp
, 0, cmd
, addr
);
1287 * Get buffer len [for read()].
1289 case BIOCGBLEN
: /* u_int */
1290 bcopy(&d
->bd_bufsize
, addr
, sizeof (u_int
));
1294 * Set buffer length.
1296 case BIOCSBLEN
: /* u_int */
1302 bcopy(addr
, &size
, sizeof (size
));
1304 if (size
> bpf_maxbufsize
)
1305 size
= bpf_maxbufsize
;
1306 else if (size
< BPF_MINBUFSIZE
)
1307 size
= BPF_MINBUFSIZE
;
1308 bcopy(&size
, addr
, sizeof (size
));
1309 d
->bd_bufsize
= size
;
1314 * Set link layer read filter.
1317 case BIOCSETFNR32
: { /* struct bpf_program32 */
1318 struct bpf_program32 prg32
;
1320 bcopy(addr
, &prg32
, sizeof (prg32
));
1321 error
= bpf_setf(d
, prg32
.bf_len
,
1322 CAST_USER_ADDR_T(prg32
.bf_insns
), dev
, cmd
);
1327 case BIOCSETFNR64
: { /* struct bpf_program64 */
1328 struct bpf_program64 prg64
;
1330 bcopy(addr
, &prg64
, sizeof (prg64
));
1331 error
= bpf_setf(d
, prg64
.bf_len
, prg64
.bf_insns
, dev
, cmd
);
1336 * Flush read packet buffer.
1339 while (d
->bd_hbuf_read
) {
1340 msleep((caddr_t
)d
, bpf_mlock
, PRINET
, "bpf_reading", NULL
);
1343 d
= bpf_dtab
[minor(dev
)];
1344 if (d
== 0 || d
== (void *)1)
1351 * Put interface into promiscuous mode.
1354 if (d
->bd_bif
== 0) {
1356 * No interface attached yet.
1361 if (d
->bd_promisc
== 0) {
1362 lck_mtx_unlock(bpf_mlock
);
1363 error
= ifnet_set_promiscuous(d
->bd_bif
->bif_ifp
, 1);
1364 lck_mtx_lock(bpf_mlock
);
1371 * Get device parameters.
1373 case BIOCGDLT
: /* u_int */
1377 bcopy(&d
->bd_bif
->bif_dlt
, addr
, sizeof (u_int
));
1381 * Get a list of supported data link types.
1383 case BIOCGDLTLIST
: /* struct bpf_dltlist */
1384 if (d
->bd_bif
== NULL
) {
1387 error
= bpf_getdltlist(d
, addr
, p
);
1392 * Set data link type.
1394 case BIOCSDLT
: /* u_int */
1395 if (d
->bd_bif
== NULL
) {
1400 bcopy(addr
, &dlt
, sizeof (dlt
));
1401 error
= bpf_setdlt(d
, dlt
, dev
);
1406 * Get interface name.
1408 case BIOCGETIF
: /* struct ifreq */
1412 struct ifnet
*const ifp
= d
->bd_bif
->bif_ifp
;
1414 snprintf(((struct ifreq
*)(void *)addr
)->ifr_name
,
1415 sizeof (ifr
.ifr_name
), "%s", if_name(ifp
));
1422 case BIOCSETIF
: { /* struct ifreq */
1425 bcopy(addr
, &ifr
, sizeof (ifr
));
1426 ifr
.ifr_name
[IFNAMSIZ
- 1] = '\0';
1427 ifp
= ifunit(ifr
.ifr_name
);
1431 error
= bpf_setif(d
, ifp
, 0, dev
);
1438 case BIOCSRTIMEOUT32
: { /* struct user32_timeval */
1439 struct user32_timeval _tv
;
1442 bcopy(addr
, &_tv
, sizeof (_tv
));
1443 tv
.tv_sec
= _tv
.tv_sec
;
1444 tv
.tv_usec
= _tv
.tv_usec
;
1447 * Subtract 1 tick from tvtohz() since this isn't
1450 if ((error
= itimerfix(&tv
)) == 0)
1451 d
->bd_rtout
= tvtohz(&tv
) - 1;
1455 case BIOCSRTIMEOUT64
: { /* struct user64_timeval */
1456 struct user64_timeval _tv
;
1459 bcopy(addr
, &_tv
, sizeof (_tv
));
1460 tv
.tv_sec
= _tv
.tv_sec
;
1461 tv
.tv_usec
= _tv
.tv_usec
;
1464 * Subtract 1 tick from tvtohz() since this isn't
1467 if ((error
= itimerfix(&tv
)) == 0)
1468 d
->bd_rtout
= tvtohz(&tv
) - 1;
1475 case BIOCGRTIMEOUT32
: { /* struct user32_timeval */
1476 struct user32_timeval tv
;
1478 bzero(&tv
, sizeof (tv
));
1479 tv
.tv_sec
= d
->bd_rtout
/ hz
;
1480 tv
.tv_usec
= (d
->bd_rtout
% hz
) * tick
;
1481 bcopy(&tv
, addr
, sizeof (tv
));
1485 case BIOCGRTIMEOUT64
: { /* struct user64_timeval */
1486 struct user64_timeval tv
;
1488 bzero(&tv
, sizeof (tv
));
1489 tv
.tv_sec
= d
->bd_rtout
/ hz
;
1490 tv
.tv_usec
= (d
->bd_rtout
% hz
) * tick
;
1491 bcopy(&tv
, addr
, sizeof (tv
));
1498 case BIOCGSTATS
: { /* struct bpf_stat */
1501 bzero(&bs
, sizeof (bs
));
1502 bs
.bs_recv
= d
->bd_rcount
;
1503 bs
.bs_drop
= d
->bd_dcount
;
1504 bcopy(&bs
, addr
, sizeof (bs
));
1509 * Set immediate mode.
1511 case BIOCIMMEDIATE
: /* u_int */
1512 bcopy(addr
, &d
->bd_immediate
, sizeof (u_int
));
1515 case BIOCVERSION
: { /* struct bpf_version */
1516 struct bpf_version bv
;
1518 bzero(&bv
, sizeof (bv
));
1519 bv
.bv_major
= BPF_MAJOR_VERSION
;
1520 bv
.bv_minor
= BPF_MINOR_VERSION
;
1521 bcopy(&bv
, addr
, sizeof (bv
));
1526 * Get "header already complete" flag
1528 case BIOCGHDRCMPLT
: /* u_int */
1529 bcopy(&d
->bd_hdrcmplt
, addr
, sizeof (u_int
));
1533 * Set "header already complete" flag
1535 case BIOCSHDRCMPLT
: /* u_int */
1536 bcopy(addr
, &int_arg
, sizeof (int_arg
));
1537 d
->bd_hdrcmplt
= int_arg
? 1 : 0;
1541 * Get "see sent packets" flag
1543 case BIOCGSEESENT
: /* u_int */
1544 bcopy(&d
->bd_seesent
, addr
, sizeof (u_int
));
1548 * Set "see sent packets" flag
1550 case BIOCSSEESENT
: /* u_int */
1551 bcopy(addr
, &d
->bd_seesent
, sizeof (u_int
));
1555 * Set traffic service class
1557 case BIOCSETTC
: { /* int */
1560 bcopy(addr
, &tc
, sizeof (int));
1561 error
= bpf_set_traffic_class(d
, tc
);
1566 * Get traffic service class
1568 case BIOCGETTC
: /* int */
1569 bcopy(&d
->bd_traffic_class
, addr
, sizeof (int));
1572 case FIONBIO
: /* Non-blocking I/O; int */
1575 case FIOASYNC
: /* Send signal on receive packets; int */
1576 bcopy(addr
, &d
->bd_async
, sizeof (int));
1580 error
= fsetown(*(int *)addr
, &d
->bd_sigio
);
1584 *(int *)addr
= fgetown(d
->bd_sigio
);
1587 /* This is deprecated, FIOSETOWN should be used instead. */
1589 error
= fsetown(-(*(int *)addr
), &d
->bd_sigio
);
1592 /* This is deprecated, FIOGETOWN should be used instead. */
1594 *(int *)addr
= -fgetown(d
->bd_sigio
);
1597 case BIOCSRSIG
: { /* Set receive signal; u_int */
1600 bcopy(addr
, &sig
, sizeof (u_int
));
1608 case BIOCGRSIG
: /* u_int */
1609 bcopy(&d
->bd_sig
, addr
, sizeof (u_int
));
1613 bcopy(addr
, &d
->bd_extendedhdr
, sizeof (u_int
));
1616 case BIOCGIFATTACHCOUNT
: { /* struct ifreq */
1620 bcopy(addr
, &ifr
, sizeof (ifr
));
1621 ifr
.ifr_name
[IFNAMSIZ
- 1] = '\0';
1622 ifp
= ifunit(ifr
.ifr_name
);
1628 for (bp
= bpf_iflist
; bp
!= 0; bp
= bp
->bif_next
) {
1629 struct bpf_d
*bpf_d
;
1631 if (bp
->bif_ifp
== NULL
|| bp
->bif_ifp
!= ifp
)
1633 for (bpf_d
= bp
->bif_dlist
; bpf_d
; bpf_d
= bpf_d
->bd_next
) {
1634 ifr
.ifr_intval
+= 1;
1637 bcopy(&ifr
, addr
, sizeof (ifr
));
1643 lck_mtx_unlock(bpf_mlock
);
1649 * Set d's packet filter program to fp. If this file already has a filter,
1650 * free it and replace it. Returns EINVAL for bogus requests.
1653 bpf_setf(struct bpf_d
*d
, u_int bf_len
, user_addr_t bf_insns
, dev_t dev
, u_long cmd
)
1655 struct bpf_insn
*fcode
, *old
;
1658 while (d
->bd_hbuf_read
)
1659 msleep((caddr_t
)d
, bpf_mlock
, PRINET
, "bpf_reading", NULL
);
1661 d
= bpf_dtab
[minor(dev
)];
1662 if (d
== 0 || d
== (void *)1)
1666 if (bf_insns
== USER_ADDR_NULL
) {
1669 d
->bd_filter
= NULL
;
1672 FREE((caddr_t
)old
, M_DEVBUF
);
1676 if (flen
> BPF_MAXINSNS
)
1679 size
= flen
* sizeof(struct bpf_insn
);
1680 fcode
= (struct bpf_insn
*) _MALLOC(size
, M_DEVBUF
, M_WAIT
);
1685 if (copyin(bf_insns
, (caddr_t
)fcode
, size
) == 0 &&
1686 bpf_validate(fcode
, (int)flen
)) {
1687 d
->bd_filter
= fcode
;
1689 if (cmd
== BIOCSETF32
|| cmd
== BIOCSETF64
)
1693 FREE((caddr_t
)old
, M_DEVBUF
);
1697 FREE((caddr_t
)fcode
, M_DEVBUF
);
1702 * Detach a file from its current interface (if attached at all) and attach
1703 * to the interface indicated by the name stored in ifr.
1704 * Return an errno or 0.
1707 bpf_setif(struct bpf_d
*d
, ifnet_t theywant
, u_int32_t dlt
, dev_t dev
)
1712 while (d
->bd_hbuf_read
)
1713 msleep((caddr_t
)d
, bpf_mlock
, PRINET
, "bpf_reading", NULL
);
1715 d
= bpf_dtab
[minor(dev
)];
1716 if (d
== 0 || d
== (void *)1)
1720 * Look through attached interfaces for the named one.
1722 for (bp
= bpf_iflist
; bp
!= 0; bp
= bp
->bif_next
) {
1723 struct ifnet
*ifp
= bp
->bif_ifp
;
1725 if (ifp
== 0 || ifp
!= theywant
|| (dlt
!= 0 && dlt
!= bp
->bif_dlt
))
1728 * We found the requested interface.
1729 * Allocate the packet buffers if we need to.
1730 * If we're already attached to requested interface,
1731 * just flush the buffer.
1733 if (d
->bd_sbuf
== 0) {
1734 error
= bpf_allocbufs(d
);
1738 if (bp
!= d
->bd_bif
) {
1741 * Detach if attached to something else.
1745 if (bpf_attachd(d
, bp
) != 0) {
1759 * Get a list of available data link type of the interface.
1762 bpf_getdltlist(struct bpf_d
*d
, caddr_t addr
, struct proc
*p
)
1769 struct bpf_dltlist bfl
;
1771 bcopy(addr
, &bfl
, sizeof (bfl
));
1772 if (proc_is64bit(p
)) {
1773 dlist
= (user_addr_t
)bfl
.bfl_u
.bflu_pad
;
1775 dlist
= CAST_USER_ADDR_T(bfl
.bfl_u
.bflu_list
);
1778 ifp
= d
->bd_bif
->bif_ifp
;
1781 for (bp
= bpf_iflist
; bp
; bp
= bp
->bif_next
) {
1782 if (bp
->bif_ifp
!= ifp
)
1784 if (dlist
!= USER_ADDR_NULL
) {
1785 if (n
>= bfl
.bfl_len
) {
1788 error
= copyout(&bp
->bif_dlt
, dlist
,
1789 sizeof (bp
->bif_dlt
));
1792 dlist
+= sizeof (bp
->bif_dlt
);
1797 bcopy(&bfl
, addr
, sizeof (bfl
));
1803 * Set the data link type of a BPF instance.
1806 bpf_setdlt(struct bpf_d
*d
, uint32_t dlt
, dev_t dev
)
1808 int error
, opromisc
;
1812 if (d
->bd_bif
->bif_dlt
== dlt
)
1815 while (d
->bd_hbuf_read
)
1816 msleep((caddr_t
)d
, bpf_mlock
, PRINET
, "bpf_reading", NULL
);
1818 d
= bpf_dtab
[minor(dev
)];
1819 if (d
== 0 || d
== (void *)1)
1822 ifp
= d
->bd_bif
->bif_ifp
;
1823 for (bp
= bpf_iflist
; bp
; bp
= bp
->bif_next
) {
1824 if (bp
->bif_ifp
== ifp
&& bp
->bif_dlt
== dlt
)
1828 opromisc
= d
->bd_promisc
;
1830 error
= bpf_attachd(d
, bp
);
1832 printf("bpf_setdlt: bpf_attachd %s%d failed (%d)\n",
1833 ifnet_name(bp
->bif_ifp
), ifnet_unit(bp
->bif_ifp
), error
);
1838 lck_mtx_unlock(bpf_mlock
);
1839 error
= ifnet_set_promiscuous(bp
->bif_ifp
, 1);
1840 lck_mtx_lock(bpf_mlock
);
1842 printf("bpf_setdlt: ifpromisc %s%d failed (%d)\n",
1843 ifnet_name(bp
->bif_ifp
), ifnet_unit(bp
->bif_ifp
), error
);
1848 return (bp
== NULL
? EINVAL
: 0);
1852 bpf_set_traffic_class(struct bpf_d
*d
, int tc
)
1856 if (!SO_VALID_TC(tc
))
1859 d
->bd_traffic_class
= tc
;
1865 bpf_set_packet_service_class(struct mbuf
*m
, int tc
)
1867 if (!(m
->m_flags
& M_PKTHDR
))
1870 VERIFY(SO_VALID_TC(tc
));
1871 (void) m_set_service_class(m
, so_tc2msc(tc
));
1875 * Support for select()
1877 * Return true iff the specific operation will not block indefinitely.
1878 * Otherwise, return false but make a note that a selwakeup() must be done.
1881 bpfselect(dev_t dev
, int which
, void * wql
, struct proc
*p
)
1886 lck_mtx_lock(bpf_mlock
);
1888 d
= bpf_dtab
[minor(dev
)];
1889 if (d
== 0 || d
== (void *)1) {
1890 lck_mtx_unlock(bpf_mlock
);
1894 if (d
->bd_bif
== NULL
) {
1895 lck_mtx_unlock(bpf_mlock
);
1899 while (d
->bd_hbuf_read
)
1900 msleep((caddr_t
)d
, bpf_mlock
, PRINET
, "bpf_reading", NULL
);
1902 d
= bpf_dtab
[minor(dev
)];
1903 if (d
== 0 || d
== (void *)1) {
1904 lck_mtx_unlock(bpf_mlock
);
1910 if (d
->bd_hlen
!= 0 ||
1911 ((d
->bd_immediate
|| d
->bd_state
== BPF_TIMED_OUT
) &&
1913 ret
= 1; /* read has data to return */
1916 * Read has no data to return.
1917 * Make the select wait, and start a timer if
1920 selrecord(p
, &d
->bd_sel
, wql
);
1926 ret
= 1; /* can't determine whether a write would block */
1930 lck_mtx_unlock(bpf_mlock
);
1936 * Support for kevent() system call. Register EVFILT_READ filters and
1937 * reject all others.
1939 int bpfkqfilter(dev_t dev
, struct knote
*kn
);
1940 static void filt_bpfdetach(struct knote
*);
1941 static int filt_bpfread(struct knote
*, long);
1943 static struct filterops bpfread_filtops
= {
1945 .f_detach
= filt_bpfdetach
,
1946 .f_event
= filt_bpfread
,
1950 bpfkqfilter(dev_t dev
, struct knote
*kn
)
1955 * Is this device a bpf?
1957 if (major(dev
) != CDEV_MAJOR
) {
1961 if (kn
->kn_filter
!= EVFILT_READ
) {
1965 lck_mtx_lock(bpf_mlock
);
1967 d
= bpf_dtab
[minor(dev
)];
1968 if (d
== 0 || d
== (void *)1) {
1969 lck_mtx_unlock(bpf_mlock
);
1973 if (d
->bd_bif
== NULL
) {
1974 lck_mtx_unlock(bpf_mlock
);
1979 kn
->kn_fop
= &bpfread_filtops
;
1980 KNOTE_ATTACH(&d
->bd_sel
.si_note
, kn
);
1981 lck_mtx_unlock(bpf_mlock
);
1986 filt_bpfdetach(struct knote
*kn
)
1988 struct bpf_d
*d
= (struct bpf_d
*)kn
->kn_hook
;
1990 lck_mtx_lock(bpf_mlock
);
1991 KNOTE_DETACH(&d
->bd_sel
.si_note
, kn
);
1992 lck_mtx_unlock(bpf_mlock
);
1996 filt_bpfread(struct knote
*kn
, long hint
)
1998 struct bpf_d
*d
= (struct bpf_d
*)kn
->kn_hook
;
2002 lck_mtx_lock(bpf_mlock
);
2004 if (d
->bd_immediate
) {
2006 * If there's data in the hold buffer, it's the
2007 * amount of data a read will return.
2009 * If there's no data in the hold buffer, but
2010 * there's data in the store buffer, a read will
2011 * immediately rotate the store buffer to the
2012 * hold buffer, the amount of data in the store
2013 * buffer is the amount of data a read will
2016 * If there's no data in either buffer, we're not
2019 kn
->kn_data
= ((d
->bd_hlen
== 0 || d
->bd_hbuf_read
)
2020 ? d
->bd_slen
: d
->bd_hlen
);
2022 if (kn
->kn_sfflags
& NOTE_LOWAT
)
2024 if (kn
->kn_sdata
> d
->bd_bufsize
)
2025 lowwat
= d
->bd_bufsize
;
2026 else if (kn
->kn_sdata
> lowwat
)
2027 lowwat
= kn
->kn_sdata
;
2029 ready
= (kn
->kn_data
>= lowwat
);
2032 * If there's data in the hold buffer, it's the
2033 * amount of data a read will return.
2035 * If there's no data in the hold buffer, but
2036 * there's data in the store buffer, if the
2037 * timer has expired a read will immediately
2038 * rotate the store buffer to the hold buffer,
2039 * so the amount of data in the store buffer is
2040 * the amount of data a read will return.
2042 * If there's no data in either buffer, or there's
2043 * no data in the hold buffer and the timer hasn't
2044 * expired, we're not ready to read.
2046 kn
->kn_data
= ((d
->bd_hlen
== 0 || d
->bd_hbuf_read
) && d
->bd_state
== BPF_TIMED_OUT
?
2047 d
->bd_slen
: d
->bd_hlen
);
2048 ready
= (kn
->kn_data
> 0);
2054 lck_mtx_unlock(bpf_mlock
);
2059 * Copy data from an mbuf chain into a buffer. This code is derived
2060 * from m_copydata in sys/uipc_mbuf.c.
2063 bpf_mcopy(const void *src_arg
, void *dst_arg
, size_t len
)
2065 struct mbuf
*m
= (struct mbuf
*)(uintptr_t)(src_arg
);
2073 count
= min(m
->m_len
, len
);
2074 bcopy(mbuf_data(m
), dst
, count
);
2091 struct mbuf
*savedm
= m
;
2094 * It's possible that we get here after the bpf descriptor has been
2095 * detached from the interface; in such a case we simply return.
2096 * Lock ordering is important since we can be called asynchronously
2097 * (from the IOKit) to process an inbound packet; when that happens
2098 * we would have been holding its "gateLock" and will be acquiring
2099 * "bpf_mlock" upon entering this routine. Due to that, we release
2100 * "bpf_mlock" prior to calling ifnet_set_promiscuous (which will
2101 * acquire "gateLock" in the IOKit), in order to avoid a deadlock
2102 * when a ifnet_set_promiscuous request simultaneously collides with
2103 * an inbound packet being passed into the tap callback.
2105 lck_mtx_lock(bpf_mlock
);
2106 if (ifp
->if_bpf
== NULL
) {
2107 lck_mtx_unlock(bpf_mlock
);
2111 for (bp
= ifp
->if_bpf
; bp
&& bp
->bif_ifp
== ifp
&&
2112 (dlt
!= 0 && bp
->bif_dlt
!= dlt
); bp
= bp
->bif_next
)
2114 if (bp
&& bp
->bif_ifp
== ifp
&& bp
->bif_dlist
!= NULL
) {
2116 struct m_hdr hack_hdr
;
2123 * This is gross. We mock up an mbuf that points to the
2124 * header buffer. This means we don't have to copy the
2125 * header. A number of interfaces prepended headers just
2126 * for bpf by allocating an mbuf on the stack. We want to
2127 * give developers an easy way to prepend a header for bpf.
2128 * Since a developer allocating an mbuf on the stack is bad,
2129 * we do even worse here, allocating only a header to point
2130 * to a buffer the developer supplied. This makes assumptions
2131 * that bpf_filter and catchpacket will not look at anything
2132 * in the mbuf other than the header. This was true at the
2133 * time this code was written.
2135 hack_hdr
.mh_next
= m
;
2136 hack_hdr
.mh_nextpkt
= NULL
;
2137 hack_hdr
.mh_len
= hlen
;
2138 hack_hdr
.mh_data
= hdr
;
2139 hack_hdr
.mh_type
= m
->m_type
;
2140 hack_hdr
.mh_flags
= 0;
2142 m
= (mbuf_t
)&hack_hdr
;
2145 for (m0
= m
; m0
!= 0; m0
= m0
->m_next
)
2146 pktlen
+= m0
->m_len
;
2148 for (d
= bp
->bif_dlist
; d
; d
= d
->bd_next
) {
2149 if (outbound
&& !d
->bd_seesent
)
2152 slen
= bpf_filter(d
->bd_filter
, (u_char
*)m
, pktlen
, 0);
2155 if (mac_bpfdesc_check_receive(d
, bp
->bif_ifp
) != 0)
2158 catchpacket(d
, (u_char
*)m
, savedm
, pktlen
,
2159 slen
, outbound
, bpf_mcopy
);
2163 lck_mtx_unlock(bpf_mlock
);
2174 bpf_tap_imp(ifp
, dlt
, m
, hdr
, hlen
, 1);
2185 bpf_tap_imp(ifp
, dlt
, m
, hdr
, hlen
, 0);
2188 /* Callback registered with Ethernet driver. */
2189 static int bpf_tap_callback(struct ifnet
*ifp
, struct mbuf
*m
)
2191 bpf_tap_imp(ifp
, 0, m
, NULL
, 0, mbuf_pkthdr_rcvif(m
) == NULL
);
2197 * Move the packet data from interface memory (pkt) into the
2198 * store buffer. Return 1 if it's time to wakeup a listener (buffer full),
2199 * otherwise 0. "copy" is the routine called to do the actual data
2200 * transfer. bcopy is passed in to copy contiguous chunks, while
2201 * bpf_mcopy is passed in to copy mbuf chains. In the latter case,
2202 * pkt is really an mbuf.
2205 catchpacket(struct bpf_d
*d
, u_char
*pkt
, struct mbuf
*m
, u_int pktlen
,
2206 u_int snaplen
, int outbound
,
2207 void (*cpfn
)(const void *, void *, size_t))
2210 struct bpf_hdr_ext
*ehp
;
2216 struct m_tag
*mt
= NULL
;
2217 struct bpf_mtag
*bt
= NULL
;
2219 hdrlen
= d
->bd_extendedhdr
? d
->bd_bif
->bif_exthdrlen
:
2220 d
->bd_bif
->bif_hdrlen
;
2222 * Figure out how many bytes to move. If the packet is
2223 * greater or equal to the snapshot length, transfer that
2224 * much. Otherwise, transfer the whole packet (unless
2225 * we hit the buffer size limit).
2227 totlen
= hdrlen
+ min(snaplen
, pktlen
);
2228 if (totlen
> d
->bd_bufsize
)
2229 totlen
= d
->bd_bufsize
;
2232 * Round up the end of the previous packet to the next longword.
2234 curlen
= BPF_WORDALIGN(d
->bd_slen
);
2235 if (curlen
+ totlen
> d
->bd_bufsize
) {
2237 * This packet will overflow the storage buffer.
2238 * Rotate the buffers if we can, then wakeup any
2241 if (d
->bd_fbuf
== NULL
) {
2243 * We haven't completed the previous read yet,
2244 * so drop the packet.
2253 else if (d
->bd_immediate
|| d
->bd_state
== BPF_TIMED_OUT
)
2255 * Immediate mode is set, or the read timeout has
2256 * already expired during a select call. A packet
2257 * arrived, so the reader should be woken up.
2262 * Append the bpf header.
2265 if (d
->bd_extendedhdr
) {
2266 ehp
= (struct bpf_hdr_ext
*)(void *)(d
->bd_sbuf
+ curlen
);
2267 memset(ehp
, 0, sizeof(*ehp
));
2268 ehp
->bh_tstamp
.tv_sec
= tv
.tv_sec
;
2269 ehp
->bh_tstamp
.tv_usec
= tv
.tv_usec
;
2270 ehp
->bh_datalen
= pktlen
;
2271 ehp
->bh_hdrlen
= hdrlen
;
2272 ehp
->bh_caplen
= totlen
- hdrlen
;
2273 mt
= m_tag_locate(m
, bpf_mtag_id
, 0, NULL
);
2274 if (mt
&& mt
->m_tag_len
>= sizeof(*bt
)) {
2275 bt
= (struct bpf_mtag
*)(mt
+ 1);
2276 ehp
->bh_pid
= bt
->bt_pid
;
2277 strlcpy(ehp
->bh_comm
, bt
->bt_comm
,
2278 sizeof(ehp
->bh_comm
));
2279 ehp
->bh_svc
= so_svc2tc(bt
->bt_svc
);
2280 if (bt
->bt_direction
== BPF_MTAG_DIR_OUT
)
2281 ehp
->bh_flags
|= BPF_HDR_EXT_FLAGS_DIR_OUT
;
2283 ehp
->bh_flags
|= BPF_HDR_EXT_FLAGS_DIR_IN
;
2284 m_tag_delete(m
, mt
);
2285 } else if (outbound
) {
2286 /* only do lookups on non-raw INPCB */
2287 if ((m
->m_pkthdr
.pkt_flags
& (PKTF_FLOW_ID
|
2288 PKTF_FLOW_LOCALSRC
|PKTF_FLOW_RAWSOCK
)) ==
2289 (PKTF_FLOW_ID
|PKTF_FLOW_LOCALSRC
) &&
2290 m
->m_pkthdr
.pkt_flowsrc
== FLOWSRC_INPCB
) {
2291 ehp
->bh_flowid
= m
->m_pkthdr
.pkt_flowid
;
2292 ehp
->bh_proto
= m
->m_pkthdr
.pkt_proto
;
2294 ehp
->bh_svc
= so_svc2tc(m
->m_pkthdr
.pkt_svc
);
2295 ehp
->bh_flags
|= BPF_HDR_EXT_FLAGS_DIR_OUT
;
2297 ehp
->bh_flags
|= BPF_HDR_EXT_FLAGS_DIR_IN
;
2298 payload
= (u_char
*)ehp
+ hdrlen
;
2299 caplen
= ehp
->bh_caplen
;
2301 hp
= (struct bpf_hdr
*)(void *)(d
->bd_sbuf
+ curlen
);
2302 hp
->bh_tstamp
.tv_sec
= tv
.tv_sec
;
2303 hp
->bh_tstamp
.tv_usec
= tv
.tv_usec
;
2304 hp
->bh_datalen
= pktlen
;
2305 hp
->bh_hdrlen
= hdrlen
;
2306 hp
->bh_caplen
= totlen
- hdrlen
;
2307 payload
= (u_char
*)hp
+ hdrlen
;
2308 caplen
= hp
->bh_caplen
;
2311 * Copy the packet data into the store buffer and update its length.
2313 (*cpfn
)(pkt
, payload
, caplen
);
2314 d
->bd_slen
= curlen
+ totlen
;
2321 * Initialize all nonzero fields of a descriptor.
2324 bpf_allocbufs(struct bpf_d
*d
)
2326 d
->bd_fbuf
= (caddr_t
) _MALLOC(d
->bd_bufsize
, M_DEVBUF
, M_WAIT
);
2327 if (d
->bd_fbuf
== 0)
2330 d
->bd_sbuf
= (caddr_t
) _MALLOC(d
->bd_bufsize
, M_DEVBUF
, M_WAIT
);
2331 if (d
->bd_sbuf
== 0) {
2332 FREE(d
->bd_fbuf
, M_DEVBUF
);
2341 * Free buffers currently in use by a descriptor.
2345 bpf_freed(struct bpf_d
*d
)
2348 * We don't need to lock out interrupts since this descriptor has
2349 * been detached from its interface and it yet hasn't been marked
2352 if (d
->bd_hbuf_read
)
2353 panic("bpf buffer freed during read");
2355 if (d
->bd_sbuf
!= 0) {
2356 FREE(d
->bd_sbuf
, M_DEVBUF
);
2357 if (d
->bd_hbuf
!= 0)
2358 FREE(d
->bd_hbuf
, M_DEVBUF
);
2359 if (d
->bd_fbuf
!= 0)
2360 FREE(d
->bd_fbuf
, M_DEVBUF
);
2363 FREE((caddr_t
)d
->bd_filter
, M_DEVBUF
);
2367 * Attach an interface to bpf. driverp is a pointer to a (struct bpf_if *)
2368 * in the driver's softc; dlt is the link layer type; hdrlen is the fixed
2369 * size of the link header (variable length headers not yet supported).
2372 bpfattach(struct ifnet
*ifp
, u_int dlt
, u_int hdrlen
)
2374 bpf_attach(ifp
, dlt
, hdrlen
, NULL
, NULL
);
2385 struct bpf_if
*bp_new
;
2386 struct bpf_if
*bp_temp
;
2387 struct bpf_if
*bp_first
= NULL
;
2389 bp_new
= (struct bpf_if
*) _MALLOC(sizeof(*bp_new
), M_DEVBUF
, M_WAIT
);
2393 lck_mtx_lock(bpf_mlock
);
2396 * Check if this interface/dlt is already attached, record first
2397 * attachment for this interface.
2399 for (bp_temp
= bpf_iflist
; bp_temp
&& (bp_temp
->bif_ifp
!= ifp
||
2400 bp_temp
->bif_dlt
!= dlt
); bp_temp
= bp_temp
->bif_next
) {
2401 if (bp_temp
->bif_ifp
== ifp
&& bp_first
== NULL
)
2405 if (bp_temp
!= NULL
) {
2406 printf("bpfattach - %s with dlt %d is already attached\n",
2408 FREE(bp_new
, M_DEVBUF
);
2409 lck_mtx_unlock(bpf_mlock
);
2413 bzero(bp_new
, sizeof(*bp_new
));
2414 bp_new
->bif_ifp
= ifp
;
2415 bp_new
->bif_dlt
= dlt
;
2416 bp_new
->bif_send
= send
;
2417 bp_new
->bif_tap
= tap
;
2419 if (bp_first
== NULL
) {
2420 /* No other entries for this ifp */
2421 bp_new
->bif_next
= bpf_iflist
;
2422 bpf_iflist
= bp_new
;
2425 /* Add this after the first entry for this interface */
2426 bp_new
->bif_next
= bp_first
->bif_next
;
2427 bp_first
->bif_next
= bp_new
;
2431 * Compute the length of the bpf header. This is not necessarily
2432 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
2433 * that the network layer header begins on a longword boundary (for
2434 * performance reasons and to alleviate alignment restrictions).
2436 bp_new
->bif_hdrlen
= BPF_WORDALIGN(hdrlen
+ SIZEOF_BPF_HDR
) - hdrlen
;
2437 bp_new
->bif_exthdrlen
= BPF_WORDALIGN(hdrlen
+
2438 sizeof(struct bpf_hdr_ext
)) - hdrlen
;
2440 /* Take a reference on the interface */
2441 ifnet_reference(ifp
);
2443 lck_mtx_unlock(bpf_mlock
);
2447 printf("bpf: %s attached\n", if_name(ifp
));
2454 * Detach bpf from an interface. This involves detaching each descriptor
2455 * associated with the interface, and leaving bd_bif NULL. Notify each
2456 * descriptor as it's detached so that any sleepers wake up and get
2460 bpfdetach(struct ifnet
*ifp
)
2462 struct bpf_if
*bp
, *bp_prev
, *bp_next
;
2463 struct bpf_if
*bp_free
= NULL
;
2467 lck_mtx_lock(bpf_mlock
);
2469 /* Locate BPF interface information */
2471 for (bp
= bpf_iflist
; bp
!= NULL
; bp
= bp_next
) {
2472 bp_next
= bp
->bif_next
;
2473 if (ifp
!= bp
->bif_ifp
) {
2478 while ((d
= bp
->bif_dlist
) != NULL
) {
2484 bp_prev
->bif_next
= bp
->bif_next
;
2486 bpf_iflist
= bp
->bif_next
;
2489 bp
->bif_next
= bp_free
;
2495 lck_mtx_unlock(bpf_mlock
);
2502 bpf_init(__unused
void *unused
)
2508 if (bpf_devsw_installed
== 0) {
2509 bpf_devsw_installed
= 1;
2510 bpf_mlock_grp_attr
= lck_grp_attr_alloc_init();
2511 bpf_mlock_grp
= lck_grp_alloc_init("bpf", bpf_mlock_grp_attr
);
2512 bpf_mlock_attr
= lck_attr_alloc_init();
2513 lck_mtx_init(bpf_mlock
, bpf_mlock_grp
, bpf_mlock_attr
);
2514 maj
= cdevsw_add(CDEV_MAJOR
, &bpf_cdevsw
);
2517 lck_attr_free(bpf_mlock_attr
);
2519 lck_grp_free(bpf_mlock_grp
);
2520 if (bpf_mlock_grp_attr
)
2521 lck_grp_attr_free(bpf_mlock_grp_attr
);
2524 bpf_mlock_attr
= NULL
;
2525 bpf_mlock_grp
= NULL
;
2526 bpf_mlock_grp_attr
= NULL
;
2527 bpf_devsw_installed
= 0;
2528 printf("bpf_init: failed to allocate a major number!\n");
2532 for (i
= 0 ; i
< NBPFILTER
; i
++)
2533 bpf_make_dev_t(maj
);
2535 VERIFY(mbuf_tag_id_find(BPF_CONTROL_NAME
, &bpf_mtag_id
) == 0);
2538 cdevsw_add(&bpf_cdevsw
);
2543 SYSINIT(bpfdev
,SI_SUB_DRIVERS
,SI_ORDER_MIDDLE
+CDEV_MAJOR
,bpf_drvinit
,NULL
)
2548 mac_bpfdesc_label_get(struct bpf_d
*d
)
2551 return (d
->bd_label
);
2555 mac_bpfdesc_label_set(struct bpf_d
*d
, struct label
*label
)
2558 d
->bd_label
= label
;