2 * Copyright (c) 2000-2010 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 #if defined(sparc) && BSD < 199103
101 #include <sys/stream.h>
103 #include <sys/poll.h>
105 #include <sys/socket.h>
106 #include <sys/vnode.h>
110 #include <net/bpfdesc.h>
112 #include <netinet/in.h>
113 #include <netinet/if_ether.h>
114 #include <sys/kernel.h>
115 #include <sys/sysctl.h>
116 #include <net/firewire.h>
118 #include <miscfs/devfs/devfs.h>
119 #include <net/dlil.h>
121 #include <kern/locks.h>
122 #include <kern/thread_call.h>
125 #include <security/mac_framework.h>
128 extern int tvtohz(struct timeval
*);
131 * Older BSDs don't have kernel malloc.
135 static caddr_t
bpf_alloc();
136 #include <net/bpf_compat.h>
137 #define BPF_BUFSIZE (MCLBYTES-8)
138 #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, code, uio)
140 #define BPF_BUFSIZE 4096
141 #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, uio)
145 #define PRINET 26 /* interruptible */
148 * The default read buffer size is patchable.
150 static unsigned int bpf_bufsize
= BPF_BUFSIZE
;
151 SYSCTL_INT(_debug
, OID_AUTO
, bpf_bufsize
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
152 &bpf_bufsize
, 0, "");
153 __private_extern__
unsigned int bpf_maxbufsize
= BPF_MAXBUFSIZE
;
154 SYSCTL_INT(_debug
, OID_AUTO
, bpf_maxbufsize
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
155 &bpf_maxbufsize
, 0, "");
156 static unsigned int bpf_maxdevices
= 256;
157 SYSCTL_UINT(_debug
, OID_AUTO
, bpf_maxdevices
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
158 &bpf_maxdevices
, 0, "");
161 * bpf_iflist is the list of interfaces; each corresponds to an ifnet
162 * bpf_dtab holds pointer to the descriptors, indexed by minor device #
164 static struct bpf_if
*bpf_iflist
;
167 * BSD now stores the bpf_d in the dev_t which is a struct
168 * on their system. Our dev_t is an int, so we still store
169 * the bpf_d in a separate table indexed by minor device #.
171 * The value stored in bpf_dtab[n] represent three states:
172 * 0: device not opened
173 * 1: device opening or closing
174 * other: device <n> opened with pointer to storage
176 static struct bpf_d
**bpf_dtab
= NULL
;
177 static unsigned int bpf_dtab_size
= 0;
178 static unsigned int nbpfilter
= 0;
180 static lck_mtx_t
*bpf_mlock
;
181 static lck_grp_t
*bpf_mlock_grp
;
182 static lck_grp_attr_t
*bpf_mlock_grp_attr
;
183 static lck_attr_t
*bpf_mlock_attr
;
186 * Mark a descriptor free by making it point to itself.
187 * This is probably cheaper than marking with a constant since
188 * the address should be in a register anyway.
190 #endif /* __APPLE__ */
192 static int bpf_allocbufs(struct bpf_d
*);
193 static errno_t
bpf_attachd(struct bpf_d
*d
, struct bpf_if
*bp
);
194 static void bpf_detachd(struct bpf_d
*d
);
195 static void bpf_freed(struct bpf_d
*);
196 static void bpf_mcopy(const void *, void *, size_t);
197 static int bpf_movein(struct uio
*, int,
198 struct mbuf
**, struct sockaddr
*, int *);
199 static int bpf_setif(struct bpf_d
*, ifnet_t ifp
, u_int32_t dlt
);
200 static void bpf_timed_out(void *, void *);
201 static void bpf_wakeup(struct bpf_d
*);
202 static void catchpacket(struct bpf_d
*, u_char
*, u_int
,
203 u_int
, void (*)(const void *, void *, size_t));
204 static void reset_d(struct bpf_d
*);
205 static int bpf_setf(struct bpf_d
*, u_int bf_len
, user_addr_t bf_insns
);
206 static int bpf_getdltlist(struct bpf_d
*, struct bpf_dltlist
*,
208 static int bpf_setdlt(struct bpf_d
*, u_int
);
210 /*static void *bpf_devfs_token[MAXBPFILTER];*/
212 static int bpf_devsw_installed
;
214 void bpf_init(void *unused
);
215 static int bpf_tap_callback(struct ifnet
*ifp
, struct mbuf
*m
);
218 * Darwin differs from BSD here, the following are static
219 * on BSD and not static on Darwin.
225 ioctl_fcn_t bpfioctl
;
226 select_fcn_t bpfselect
;
229 /* Darwin's cdevsw struct differs slightly from BSDs */
230 #define CDEV_MAJOR 23
231 static struct cdevsw bpf_cdevsw
= {
233 /* close */ bpfclose
,
235 /* write */ bpfwrite
,
236 /* ioctl */ bpfioctl
,
238 /* reset */ eno_reset
,
240 /* select */ bpfselect
,
242 /* strategy*/ eno_strat
,
248 #define SOCKADDR_HDR_LEN offsetof(struct sockaddr, sa_data)
251 bpf_movein(struct uio
*uio
, int linktype
, struct mbuf
**mp
, struct sockaddr
*sockp
, int *datlen
)
269 sa_family
= AF_UNSPEC
;
270 /* XXX Would MAXLINKHDR be better? */
271 hlen
= sizeof(struct ether_header
);
276 #if defined(__FreeBSD__) || defined(__bsdi__)
277 sa_family
= AF_IMPLINK
;
280 sa_family
= AF_UNSPEC
;
281 /* XXX 4(FORMAC)+6(dst)+6(src)+3(LLC)+5(SNAP) */
289 sa_family
= AF_UNSPEC
;
294 case DLT_ATM_RFC1483
:
296 * en atm driver requires 4-byte atm pseudo header.
297 * though it isn't standard, vpi:vci needs to be
300 sa_family
= AF_UNSPEC
;
301 hlen
= 12; /* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
306 sa_family
= AF_UNSPEC
;
307 hlen
= 4; /* This should match PPP_HDRLEN */
310 case DLT_APPLE_IP_OVER_IEEE1394
:
311 sa_family
= AF_UNSPEC
;
312 hlen
= sizeof(struct firewire_header
);
315 case DLT_IEEE802_11
: /* IEEE 802.11 wireless */
316 sa_family
= AF_IEEE80211
;
320 case DLT_IEEE802_11_RADIO
:
321 sa_family
= AF_IEEE80211
;
329 // LP64todo - fix this!
330 len
= uio_resid(uio
);
331 *datlen
= len
- hlen
;
332 if ((unsigned)len
> MCLBYTES
)
337 * Build a sockaddr based on the data link layer type.
338 * We do this at this level because the ethernet header
339 * is copied directly into the data field of the sockaddr.
340 * In the case of SLIP, there is no header and the packet
341 * is forwarded as is.
342 * Also, we are careful to leave room at the front of the mbuf
343 * for the link level header.
345 if ((hlen
+ SOCKADDR_HDR_LEN
) > sockp
->sa_len
) {
348 sockp
->sa_family
= sa_family
;
351 * We're directly sending the packet data supplied by
352 * the user; we don't need to make room for the link
353 * header, and don't need the header length value any
354 * more, so set it to 0.
359 MGETHDR(m
, M_WAIT
, MT_DATA
);
362 if ((unsigned)len
> MHLEN
) {
365 if ((m
->m_flags
& M_EXT
) == 0) {
368 if (m
->m_len
!= MCLBYTES
) {
374 m
->m_pkthdr
.len
= m
->m_len
= len
;
375 m
->m_pkthdr
.rcvif
= NULL
;
379 * Make room for link header.
382 m
->m_pkthdr
.len
-= hlen
;
385 m
->m_data
+= hlen
; /* XXX */
389 error
= UIOMOVE((caddr_t
)sockp
->sa_data
, hlen
, UIO_WRITE
, uio
);
393 error
= UIOMOVE(mtod(m
, caddr_t
), len
- hlen
, UIO_WRITE
, uio
);
397 /* Check for multicast destination */
400 struct ether_header
*eh
= mtod(m
, struct ether_header
*);
402 if (ETHER_IS_MULTICAST(eh
->ether_dhost
)) {
403 if (_ether_cmp(etherbroadcastaddr
, eh
->ether_dhost
) == 0)
404 m
->m_flags
|= M_BCAST
;
406 m
->m_flags
|= M_MCAST
;
421 * The dynamic addition of a new device node must block all processes that are opening
422 * the last device so that no process will get an unexpected ENOENT
425 bpf_make_dev_t(int maj
)
427 static int bpf_growing
= 0;
428 unsigned int cur_size
= nbpfilter
, i
;
430 if (nbpfilter
>= bpf_maxdevices
)
433 while (bpf_growing
) {
434 /* Wait until new device has been created */
435 (void)tsleep((caddr_t
)&bpf_growing
, PZERO
, "bpf_growing", 0);
437 if (nbpfilter
> cur_size
) {
438 /* other thread grew it already */
443 /* need to grow bpf_dtab first */
444 if (nbpfilter
== bpf_dtab_size
) {
446 struct bpf_d
**new_dtab
= NULL
;
447 struct bpf_d
**old_dtab
= NULL
;
449 new_dtab_size
= bpf_dtab_size
+ NBPFILTER
;
450 new_dtab
= (struct bpf_d
**)_MALLOC(sizeof(struct bpf_d
*) * new_dtab_size
, M_DEVBUF
, M_WAIT
);
452 printf("bpf_make_dev_t: malloc bpf_dtab failed\n");
456 bcopy(bpf_dtab
, new_dtab
,
457 sizeof(struct bpf_d
*) * bpf_dtab_size
);
459 bzero(new_dtab
+ bpf_dtab_size
,
460 sizeof(struct bpf_d
*) * NBPFILTER
);
463 bpf_dtab_size
= new_dtab_size
;
464 if (old_dtab
!= NULL
)
465 _FREE(old_dtab
, M_DEVBUF
);
468 (void) devfs_make_node(makedev(maj
, i
),
469 DEVFS_CHAR
, UID_ROOT
, GID_WHEEL
, 0600,
473 wakeup((caddr_t
)&bpf_growing
);
479 * Attach file to the bpf interface, i.e. make d listen on bp.
482 bpf_attachd(struct bpf_d
*d
, struct bpf_if
*bp
)
484 int first
= bp
->bif_dlist
== NULL
;
488 * Point d at bp, and add d to the interface's list of listeners.
489 * Finally, point the driver's bpf cookie at the interface so
490 * it will divert packets to bpf.
493 d
->bd_next
= bp
->bif_dlist
;
497 /* Find the default bpf entry for this ifp */
498 if (bp
->bif_ifp
->if_bpf
== NULL
) {
499 struct bpf_if
*primary
;
501 for (primary
= bpf_iflist
; primary
&& primary
->bif_ifp
!= bp
->bif_ifp
;
502 primary
= primary
->bif_next
)
505 bp
->bif_ifp
->if_bpf
= primary
;
508 /* Only call dlil_set_bpf_tap for primary dlt */
509 if (bp
->bif_ifp
->if_bpf
== bp
)
510 dlil_set_bpf_tap(bp
->bif_ifp
, BPF_TAP_INPUT_OUTPUT
, bpf_tap_callback
);
513 error
= bp
->bif_tap(bp
->bif_ifp
, bp
->bif_dlt
, BPF_TAP_INPUT_OUTPUT
);
520 * Detach a file from its interface.
523 bpf_detachd(struct bpf_d
*d
)
529 ifp
= d
->bd_bif
->bif_ifp
;
532 /* Remove d from the interface's descriptor list. */
537 panic("bpf_detachd: descriptor not in list");
540 if (bp
->bif_dlist
== 0) {
542 * Let the driver know that there are no more listeners.
544 /* Only call dlil_set_bpf_tap for primary dlt */
545 if (bp
->bif_ifp
->if_bpf
== bp
)
546 dlil_set_bpf_tap(ifp
, BPF_TAP_DISABLE
, NULL
);
548 bp
->bif_tap(ifp
, bp
->bif_dlt
, BPF_TAP_DISABLE
);
550 for (bp
= bpf_iflist
; bp
; bp
= bp
->bif_next
)
551 if (bp
->bif_ifp
== ifp
&& bp
->bif_dlist
!= 0)
558 * Check if this descriptor had requested promiscuous mode.
559 * If so, turn it off.
563 lck_mtx_unlock(bpf_mlock
);
564 if (ifnet_set_promiscuous(ifp
, 0)) {
566 * Something is really wrong if we were able to put
567 * the driver into promiscuous mode, but can't
569 * Most likely the network interface is gone.
571 printf("bpf: ifnet_set_promiscuous failed");
573 lck_mtx_lock(bpf_mlock
);
579 * Start asynchronous timer, if necessary.
580 * Must be called with bpf_mlock held.
583 bpf_start_timer(struct bpf_d
*d
)
588 if (d
->bd_rtout
> 0 && d
->bd_state
== BPF_IDLE
) {
589 tv
.tv_sec
= d
->bd_rtout
/ hz
;
590 tv
.tv_usec
= (d
->bd_rtout
% hz
) * tick
;
592 clock_interval_to_deadline((uint64_t)tv
.tv_sec
* USEC_PER_SEC
+ tv
.tv_usec
,
596 * The state is BPF_IDLE, so the timer hasn't
597 * been started yet, and hasn't gone off yet;
598 * there is no thread call scheduled, so this
599 * won't change the schedule.
601 * XXX - what if, by the time it gets entered,
602 * the deadline has already passed?
604 thread_call_enter_delayed(d
->bd_thread_call
, deadline
);
605 d
->bd_state
= BPF_WAITING
;
610 * Cancel asynchronous timer.
611 * Must be called with bpf_mlock held.
614 bpf_stop_timer(struct bpf_d
*d
)
617 * If the timer has already gone off, this does nothing.
618 * Our caller is expected to set d->bd_state to BPF_IDLE,
619 * with the bpf_mlock, after we are called. bpf_timed_out()
620 * also grabs bpf_mlock, so, if the timer has gone off and
621 * bpf_timed_out() hasn't finished, it's waiting for the
622 * lock; when this thread releases the lock, it will
623 * find the state is BPF_IDLE, and just release the
626 return (thread_call_cancel(d
->bd_thread_call
));
632 * Open ethernet device. Returns ENXIO for illegal minor device number,
633 * EBUSY if file is open by another process.
637 bpfopen(dev_t dev
, int flags
, __unused
int fmt
,
638 __unused
struct proc
*p
)
642 lck_mtx_lock(bpf_mlock
);
643 if ((unsigned int) minor(dev
) >= nbpfilter
) {
644 lck_mtx_unlock(bpf_mlock
);
648 * New device nodes are created on demand when opening the last one.
649 * The programming model is for processes to loop on the minor starting at 0
650 * as long as EBUSY is returned. The loop stops when either the open succeeds or
651 * an error other that EBUSY is returned. That means that bpf_make_dev_t() must
652 * block all processes that are opening the last node. If not all
653 * processes are blocked, they could unexpectedly get ENOENT and abort their
656 if ((unsigned int) minor(dev
) == (nbpfilter
- 1))
657 bpf_make_dev_t(major(dev
));
660 * Each minor can be opened by only one process. If the requested
661 * minor is in use, return EBUSY.
663 * Important: bpfopen() and bpfclose() have to check and set the status of a device
664 * in the same lockin context otherwise the device may be leaked because the vnode use count
665 * will be unpextectly greater than 1 when close() is called.
667 if (bpf_dtab
[minor(dev
)] == 0) {
668 bpf_dtab
[minor(dev
)] = (void *)1; /* Mark opening */
670 lck_mtx_unlock(bpf_mlock
);
673 d
= (struct bpf_d
*)_MALLOC(sizeof(struct bpf_d
), M_DEVBUF
, M_WAIT
);
675 /* this really is a catastrophic failure */
676 printf("bpfopen: malloc bpf_d failed\n");
677 bpf_dtab
[minor(dev
)] = NULL
;
678 lck_mtx_unlock(bpf_mlock
);
681 bzero(d
, sizeof(struct bpf_d
));
684 * It is not necessary to take the BPF lock here because no other
685 * thread can access the device until it is marked opened...
688 /* Mark "in use" and do most initialization. */
689 d
->bd_bufsize
= bpf_bufsize
;
692 d
->bd_oflags
= flags
;
693 d
->bd_state
= BPF_IDLE
;
694 d
->bd_thread_call
= thread_call_allocate(bpf_timed_out
, d
);
696 if (d
->bd_thread_call
== NULL
) {
697 printf("bpfopen: malloc thread call failed\n");
698 bpf_dtab
[minor(dev
)] = NULL
;
699 lck_mtx_unlock(bpf_mlock
);
704 mac_bpfdesc_label_init(d
);
705 mac_bpfdesc_label_associate(kauth_cred_get(), d
);
707 bpf_dtab
[minor(dev
)] = d
; /* Mark opened */
708 lck_mtx_unlock(bpf_mlock
);
714 * Close the descriptor by detaching it from its interface,
715 * deallocating its buffers, and marking it free.
719 bpfclose(dev_t dev
, __unused
int flags
, __unused
int fmt
,
720 __unused
struct proc
*p
)
724 /* Take BPF lock to ensure no other thread is using the device */
725 lck_mtx_lock(bpf_mlock
);
727 d
= bpf_dtab
[minor(dev
)];
728 if (d
== 0 || d
== (void *)1) {
729 lck_mtx_unlock(bpf_mlock
);
732 bpf_dtab
[minor(dev
)] = (void *)1; /* Mark closing */
735 * Deal with any in-progress timeouts.
737 switch (d
->bd_state
) {
740 * Not waiting for a timeout, and no timeout happened.
746 * Waiting for a timeout.
747 * Cancel any timer that has yet to go off,
748 * and mark the state as "closing".
749 * Then drop the lock to allow any timers that
750 * *have* gone off to run to completion, and wait
751 * for them to finish.
753 if (!bpf_stop_timer(d
)) {
755 * There was no pending call, so the call must
756 * have been in progress. Wait for the call to
757 * complete; we have to drop the lock while
758 * waiting. to let the in-progrss call complete
760 d
->bd_state
= BPF_DRAINING
;
761 while (d
->bd_state
== BPF_DRAINING
)
762 msleep((caddr_t
)d
, bpf_mlock
, PRINET
,
763 "bpfdraining", NULL
);
765 d
->bd_state
= BPF_IDLE
;
770 * Timer went off, and the timeout routine finished.
772 d
->bd_state
= BPF_IDLE
;
777 * Another thread is blocked on a close waiting for
778 * a timeout to finish.
779 * This "shouldn't happen", as the first thread to enter
780 * bpfclose() will set bpf_dtab[minor(dev)] to 1, and
781 * all subsequent threads should see that and fail with
784 panic("Two threads blocked in a BPF close");
790 selthreadclear(&d
->bd_sel
);
792 mac_bpfdesc_label_destroy(d
);
794 thread_call_free(d
->bd_thread_call
);
797 /* Mark free in same context as bpfopen comes to check */
798 bpf_dtab
[minor(dev
)] = NULL
; /* Mark closed */
799 lck_mtx_unlock(bpf_mlock
);
807 #define BPF_SLEEP bpf_sleep
810 bpf_sleep(struct bpf_d
*d
, int pri
, const char *wmesg
, int timo
)
812 u_int64_t abstime
= 0;
815 clock_interval_to_deadline(timo
, NSEC_PER_SEC
/ hz
, &abstime
);
817 return msleep1((caddr_t
)d
, bpf_mlock
, pri
, wmesg
, abstime
);
821 * Rotate the packet buffers in descriptor d. Move the store buffer
822 * into the hold slot, and the free buffer into the store slot.
823 * Zero the length of the new store buffer.
825 #define ROTATE_BUFFERS(d) \
826 (d)->bd_hbuf = (d)->bd_sbuf; \
827 (d)->bd_hlen = (d)->bd_slen; \
828 (d)->bd_sbuf = (d)->bd_fbuf; \
832 * bpfread - read next chunk of packets from buffers
835 bpfread(dev_t dev
, struct uio
*uio
, int ioflag
)
841 lck_mtx_lock(bpf_mlock
);
843 d
= bpf_dtab
[minor(dev
)];
844 if (d
== 0 || d
== (void *)1) {
845 lck_mtx_unlock(bpf_mlock
);
850 * Restrict application to use a buffer the same size as
853 if (uio_resid(uio
) != d
->bd_bufsize
) {
854 lck_mtx_unlock(bpf_mlock
);
858 if (d
->bd_state
== BPF_WAITING
)
861 timed_out
= (d
->bd_state
== BPF_TIMED_OUT
);
862 d
->bd_state
= BPF_IDLE
;
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
);
909 if (error
== EINTR
|| error
== ERESTART
) {
910 lck_mtx_unlock(bpf_mlock
);
913 if (error
== EWOULDBLOCK
) {
915 * On a timeout, return what's in the buffer,
916 * which may be nothing. If there is something
917 * in the store buffer, we can rotate the buffers.
921 * We filled up the buffer in between
922 * getting the timeout and arriving
923 * here, so we don't need to rotate.
927 if (d
->bd_slen
== 0) {
928 lck_mtx_unlock(bpf_mlock
);
936 * At this point, we know we have something in the hold slot.
940 * Move data from hold buffer into user space.
941 * We know the entire buffer is transferred since
942 * we checked above that the read buffer is bpf_bufsize bytes.
944 error
= UIOMOVE(d
->bd_hbuf
, d
->bd_hlen
, UIO_READ
, uio
);
946 d
->bd_fbuf
= d
->bd_hbuf
;
949 lck_mtx_unlock(bpf_mlock
);
955 * If there are processes sleeping on this descriptor, wake them up.
958 bpf_wakeup(struct bpf_d
*d
)
960 if (d
->bd_state
== BPF_WAITING
) {
962 d
->bd_state
= BPF_IDLE
;
965 if (d
->bd_async
&& d
->bd_sig
&& d
->bd_sigio
)
966 pgsigio(d
->bd_sigio
, d
->bd_sig
);
969 selwakeup(&d
->bd_sel
);
970 KNOTE(&d
->bd_sel
.si_note
, 1);
973 d
->bd_sel
.si_pid
= 0;
977 selwakeup(d
->bd_selproc
, (int)d
->bd_selcoll
);
986 bpf_timed_out(void *arg
, __unused
void *dummy
)
988 struct bpf_d
*d
= (struct bpf_d
*)arg
;
990 lck_mtx_lock(bpf_mlock
);
991 if (d
->bd_state
== BPF_WAITING
) {
993 * There's a select or kqueue waiting for this; if there's
994 * now stuff to read, wake it up.
996 d
->bd_state
= BPF_TIMED_OUT
;
999 } else if (d
->bd_state
== BPF_DRAINING
) {
1001 * A close is waiting for this to finish.
1002 * Mark it as finished, and wake the close up.
1004 d
->bd_state
= BPF_IDLE
;
1007 lck_mtx_unlock(bpf_mlock
);
1014 /* keep in sync with bpf_movein above: */
1015 #define MAX_DATALINK_HDR_LEN (sizeof(struct firewire_header))
1018 bpfwrite(dev_t dev
, struct uio
*uio
, __unused
int ioflag
)
1022 struct mbuf
*m
= NULL
;
1024 char dst_buf
[SOCKADDR_HDR_LEN
+ MAX_DATALINK_HDR_LEN
];
1029 lck_mtx_lock(bpf_mlock
);
1031 d
= bpf_dtab
[minor(dev
)];
1032 if (d
== 0 || d
== (void *)1) {
1033 lck_mtx_unlock(bpf_mlock
);
1036 if (d
->bd_bif
== 0) {
1037 lck_mtx_unlock(bpf_mlock
);
1041 ifp
= d
->bd_bif
->bif_ifp
;
1043 if ((ifp
->if_flags
& IFF_UP
) == 0) {
1044 lck_mtx_unlock(bpf_mlock
);
1047 if (uio_resid(uio
) == 0) {
1048 lck_mtx_unlock(bpf_mlock
);
1051 ((struct sockaddr
*)dst_buf
)->sa_len
= sizeof(dst_buf
);
1054 * fix for PR-6849527
1055 * geting variables onto stack before dropping lock for bpf_movein()
1057 bif_dlt
= (int)d
->bd_bif
->bif_dlt
;
1058 bd_hdrcmplt
= d
->bd_hdrcmplt
;
1060 /* bpf_movein allocating mbufs; drop lock */
1061 lck_mtx_unlock(bpf_mlock
);
1063 error
= bpf_movein(uio
, bif_dlt
, &m
,
1064 bd_hdrcmplt
? NULL
: (struct sockaddr
*)dst_buf
,
1071 /* taking the lock again and verifying whether device is open */
1072 lck_mtx_lock(bpf_mlock
);
1073 d
= bpf_dtab
[minor(dev
)];
1074 if (d
== 0 || d
== (void *)1) {
1075 lck_mtx_unlock(bpf_mlock
);
1080 if (d
->bd_bif
== NULL
) {
1081 lck_mtx_unlock(bpf_mlock
);
1086 if ((unsigned)datlen
> ifp
->if_mtu
) {
1087 lck_mtx_unlock(bpf_mlock
);
1094 mac_mbuf_label_associate_bpfdesc(d
, m
);
1096 lck_mtx_unlock(bpf_mlock
);
1098 if (d
->bd_hdrcmplt
) {
1099 if (d
->bd_bif
->bif_send
)
1100 error
= d
->bd_bif
->bif_send(ifp
, d
->bd_bif
->bif_dlt
, m
);
1102 error
= dlil_output(ifp
, 0, m
, NULL
, NULL
, 1);
1105 error
= dlil_output(ifp
, PF_INET
, m
, NULL
, (struct sockaddr
*)dst_buf
, 0);
1109 * The driver frees the mbuf.
1115 * Reset a descriptor by flushing its packet buffer and clearing the
1116 * receive and drop counts.
1119 reset_d(struct bpf_d
*d
)
1122 /* Free the hold buffer. */
1123 d
->bd_fbuf
= d
->bd_hbuf
;
1133 * FIONREAD Check for read packet available.
1134 * SIOCGIFADDR Get interface address - convenient hook to driver.
1135 * BIOCGBLEN Get buffer len [for read()].
1136 * BIOCSETF Set ethernet read filter.
1137 * BIOCFLUSH Flush read packet buffer.
1138 * BIOCPROMISC Put interface into promiscuous mode.
1139 * BIOCGDLT Get link layer type.
1140 * BIOCGETIF Get interface name.
1141 * BIOCSETIF Set interface.
1142 * BIOCSRTIMEOUT Set read timeout.
1143 * BIOCGRTIMEOUT Get read timeout.
1144 * BIOCGSTATS Get packet stats.
1145 * BIOCIMMEDIATE Set immediate mode.
1146 * BIOCVERSION Get filter language version.
1147 * BIOCGHDRCMPLT Get "header already complete" flag
1148 * BIOCSHDRCMPLT Set "header already complete" flag
1149 * BIOCGSEESENT Get "see packets sent" flag
1150 * BIOCSSEESENT Set "see packets sent" flag
1154 bpfioctl(dev_t dev
, u_long cmd
, caddr_t addr
, __unused
int flags
,
1160 lck_mtx_lock(bpf_mlock
);
1162 d
= bpf_dtab
[minor(dev
)];
1163 if (d
== 0 || d
== (void *)1) {
1164 lck_mtx_unlock(bpf_mlock
);
1168 if (d
->bd_state
== BPF_WAITING
)
1170 d
->bd_state
= BPF_IDLE
;
1179 * Check for read packet available.
1200 ifp
= d
->bd_bif
->bif_ifp
;
1201 error
= ifnet_ioctl(ifp
, 0, cmd
, addr
);
1207 * Get buffer len [for read()].
1210 *(u_int
*)addr
= d
->bd_bufsize
;
1214 * Set buffer length.
1223 u_int size
= *(u_int
*)addr
;
1225 if (size
> bpf_maxbufsize
)
1226 *(u_int
*)addr
= size
= bpf_maxbufsize
;
1227 else if (size
< BPF_MINBUFSIZE
)
1228 *(u_int
*)addr
= size
= BPF_MINBUFSIZE
;
1229 d
->bd_bufsize
= size
;
1235 * Set link layer read filter.
1238 struct bpf_program32
*prg32
= (struct bpf_program32
*)addr
;
1239 error
= bpf_setf(d
, prg32
->bf_len
,
1240 CAST_USER_ADDR_T(prg32
->bf_insns
));
1245 struct bpf_program64
*prg64
= (struct bpf_program64
*)addr
;
1246 error
= bpf_setf(d
, prg64
->bf_len
, prg64
->bf_insns
);
1251 * Flush read packet buffer.
1258 * Put interface into promiscuous mode.
1261 if (d
->bd_bif
== 0) {
1263 * No interface attached yet.
1268 if (d
->bd_promisc
== 0) {
1269 lck_mtx_unlock(bpf_mlock
);
1270 error
= ifnet_set_promiscuous(d
->bd_bif
->bif_ifp
, 1);
1271 lck_mtx_lock(bpf_mlock
);
1278 * Get device parameters.
1284 *(u_int
*)addr
= d
->bd_bif
->bif_dlt
;
1288 * Get a list of supported data link types.
1291 if (d
->bd_bif
== NULL
) {
1294 error
= bpf_getdltlist(d
,
1295 (struct bpf_dltlist
*)addr
, p
);
1300 * Set data link type.
1303 if (d
->bd_bif
== NULL
)
1306 error
= bpf_setdlt(d
, *(u_int
*)addr
);
1310 * Get interface name.
1316 struct ifnet
*const ifp
= d
->bd_bif
->bif_ifp
;
1317 struct ifreq
*const ifr
= (struct ifreq
*)addr
;
1319 snprintf(ifr
->ifr_name
, sizeof(ifr
->ifr_name
),
1320 "%s%d", ifp
->if_name
, ifp
->if_unit
);
1329 ifp
= ifunit(((struct ifreq
*)addr
)->ifr_name
);
1333 error
= bpf_setif(d
, ifp
, 0);
1340 case BIOCSRTIMEOUT32
:
1342 struct user32_timeval
*_tv
= (struct user32_timeval
*)addr
;
1345 tv
.tv_sec
= _tv
->tv_sec
;
1346 tv
.tv_usec
= _tv
->tv_usec
;
1349 * Subtract 1 tick from tvtohz() since this isn't
1352 if ((error
= itimerfix(&tv
)) == 0)
1353 d
->bd_rtout
= tvtohz(&tv
) - 1;
1357 case BIOCSRTIMEOUT64
:
1359 struct user64_timeval
*_tv
= (struct user64_timeval
*)addr
;
1362 tv
.tv_sec
= _tv
->tv_sec
;
1363 tv
.tv_usec
= _tv
->tv_usec
;
1366 * Subtract 1 tick from tvtohz() since this isn't
1369 if ((error
= itimerfix(&tv
)) == 0)
1370 d
->bd_rtout
= tvtohz(&tv
) - 1;
1377 case BIOCGRTIMEOUT32
:
1379 struct user32_timeval
*tv
= (struct user32_timeval
*)addr
;
1381 tv
->tv_sec
= d
->bd_rtout
/ hz
;
1382 tv
->tv_usec
= (d
->bd_rtout
% hz
) * tick
;
1386 case BIOCGRTIMEOUT64
:
1388 struct user64_timeval
*tv
= (struct user64_timeval
*)addr
;
1390 tv
->tv_sec
= d
->bd_rtout
/ hz
;
1391 tv
->tv_usec
= (d
->bd_rtout
% hz
) * tick
;
1400 struct bpf_stat
*bs
= (struct bpf_stat
*)addr
;
1402 bs
->bs_recv
= d
->bd_rcount
;
1403 bs
->bs_drop
= d
->bd_dcount
;
1408 * Set immediate mode.
1411 d
->bd_immediate
= *(u_int
*)addr
;
1416 struct bpf_version
*bv
= (struct bpf_version
*)addr
;
1418 bv
->bv_major
= BPF_MAJOR_VERSION
;
1419 bv
->bv_minor
= BPF_MINOR_VERSION
;
1424 * Get "header already complete" flag
1427 *(u_int
*)addr
= d
->bd_hdrcmplt
;
1431 * Set "header already complete" flag
1434 d
->bd_hdrcmplt
= *(u_int
*)addr
? 1 : 0;
1438 * Get "see sent packets" flag
1441 *(u_int
*)addr
= d
->bd_seesent
;
1445 * Set "see sent packets" flag
1448 d
->bd_seesent
= *(u_int
*)addr
;
1451 case FIONBIO
: /* Non-blocking I/O */
1454 case FIOASYNC
: /* Send signal on receive packets */
1455 d
->bd_async
= *(int *)addr
;
1459 error
= fsetown(*(int *)addr
, &d
->bd_sigio
);
1463 *(int *)addr
= fgetown(d
->bd_sigio
);
1466 /* This is deprecated, FIOSETOWN should be used instead. */
1468 error
= fsetown(-(*(int *)addr
), &d
->bd_sigio
);
1471 /* This is deprecated, FIOGETOWN should be used instead. */
1473 *(int *)addr
= -fgetown(d
->bd_sigio
);
1476 case BIOCSRSIG
: /* Set receive signal */
1480 sig
= *(u_int
*)addr
;
1489 *(u_int
*)addr
= d
->bd_sig
;
1493 lck_mtx_unlock(bpf_mlock
);
1499 * Set d's packet filter program to fp. If this file already has a filter,
1500 * free it and replace it. Returns EINVAL for bogus requests.
1503 bpf_setf(struct bpf_d
*d
, u_int bf_len
, user_addr_t bf_insns
)
1505 struct bpf_insn
*fcode
, *old
;
1509 if (bf_insns
== USER_ADDR_NULL
) {
1512 d
->bd_filter
= NULL
;
1515 FREE((caddr_t
)old
, M_DEVBUF
);
1519 if (flen
> BPF_MAXINSNS
)
1522 size
= flen
* sizeof(struct bpf_insn
);
1523 fcode
= (struct bpf_insn
*) _MALLOC(size
, M_DEVBUF
, M_WAIT
);
1528 if (copyin(bf_insns
, (caddr_t
)fcode
, size
) == 0 &&
1529 bpf_validate(fcode
, (int)flen
)) {
1530 d
->bd_filter
= fcode
;
1533 FREE((caddr_t
)old
, M_DEVBUF
);
1537 FREE((caddr_t
)fcode
, M_DEVBUF
);
1542 * Detach a file from its current interface (if attached at all) and attach
1543 * to the interface indicated by the name stored in ifr.
1544 * Return an errno or 0.
1547 bpf_setif(struct bpf_d
*d
, ifnet_t theywant
, u_int32_t dlt
)
1553 * Look through attached interfaces for the named one.
1555 for (bp
= bpf_iflist
; bp
!= 0; bp
= bp
->bif_next
) {
1556 struct ifnet
*ifp
= bp
->bif_ifp
;
1558 if (ifp
== 0 || ifp
!= theywant
|| (dlt
!= 0 && dlt
!= bp
->bif_dlt
))
1561 * We found the requested interface.
1562 * Allocate the packet buffers if we need to.
1563 * If we're already attached to requested interface,
1564 * just flush the buffer.
1566 if (d
->bd_sbuf
== 0) {
1567 error
= bpf_allocbufs(d
);
1571 if (bp
!= d
->bd_bif
) {
1574 * Detach if attached to something else.
1578 if (bpf_attachd(d
, bp
) != 0) {
1592 * Get a list of available data link type of the interface.
1595 bpf_getdltlist(struct bpf_d
*d
, struct bpf_dltlist
*bfl
, struct proc
*p
)
1603 if (proc_is64bit(p
)) {
1604 dlist
= (user_addr_t
)bfl
->bfl_u
.bflu_pad
;
1606 dlist
= CAST_USER_ADDR_T(bfl
->bfl_u
.bflu_list
);
1609 ifp
= d
->bd_bif
->bif_ifp
;
1612 for (bp
= bpf_iflist
; bp
; bp
= bp
->bif_next
) {
1613 if (bp
->bif_ifp
!= ifp
)
1615 if (dlist
!= USER_ADDR_NULL
) {
1616 if (n
>= bfl
->bfl_len
) {
1619 error
= copyout(&bp
->bif_dlt
, dlist
,
1620 sizeof (bp
->bif_dlt
));
1621 dlist
+= sizeof (bp
->bif_dlt
);
1630 * Set the data link type of a BPF instance.
1633 bpf_setdlt(struct bpf_d
*d
, uint32_t dlt
)
1637 int error
, opromisc
;
1641 if (d
->bd_bif
->bif_dlt
== dlt
)
1643 ifp
= d
->bd_bif
->bif_ifp
;
1644 for (bp
= bpf_iflist
; bp
; bp
= bp
->bif_next
) {
1645 if (bp
->bif_ifp
== ifp
&& bp
->bif_dlt
== dlt
)
1649 opromisc
= d
->bd_promisc
;
1651 error
= bpf_attachd(d
, bp
);
1653 printf("bpf_setdlt: bpf_attachd %s%d failed (%d)\n",
1654 ifnet_name(bp
->bif_ifp
), ifnet_unit(bp
->bif_ifp
), error
);
1659 lck_mtx_unlock(bpf_mlock
);
1660 error
= ifnet_set_promiscuous(bp
->bif_ifp
, 1);
1661 lck_mtx_lock(bpf_mlock
);
1663 printf("bpf_setdlt: ifpromisc %s%d failed (%d)\n",
1664 ifnet_name(bp
->bif_ifp
), ifnet_unit(bp
->bif_ifp
), error
);
1669 return (bp
== NULL
? EINVAL
: 0);
1673 * Support for select()
1675 * Return true iff the specific operation will not block indefinitely.
1676 * Otherwise, return false but make a note that a selwakeup() must be done.
1679 bpfselect(dev_t dev
, int which
, void * wql
, struct proc
*p
)
1684 lck_mtx_lock(bpf_mlock
);
1686 d
= bpf_dtab
[minor(dev
)];
1687 if (d
== 0 || d
== (void *)1) {
1688 lck_mtx_unlock(bpf_mlock
);
1692 if (d
->bd_bif
== NULL
) {
1693 lck_mtx_unlock(bpf_mlock
);
1699 if (d
->bd_hlen
!= 0 ||
1700 ((d
->bd_immediate
|| d
->bd_state
== BPF_TIMED_OUT
) &&
1702 ret
= 1; /* read has data to return */
1705 * Read has no data to return.
1706 * Make the select wait, and start a timer if
1709 selrecord(p
, &d
->bd_sel
, wql
);
1715 ret
= 1; /* can't determine whether a write would block */
1719 lck_mtx_unlock(bpf_mlock
);
1725 * Support for kevent() system call. Register EVFILT_READ filters and
1726 * reject all others.
1728 int bpfkqfilter(dev_t dev
, struct knote
*kn
);
1729 static void filt_bpfdetach(struct knote
*);
1730 static int filt_bpfread(struct knote
*, long);
1732 static struct filterops bpfread_filtops
= {
1734 .f_detach
= filt_bpfdetach
,
1735 .f_event
= filt_bpfread
,
1739 bpfkqfilter(dev_t dev
, struct knote
*kn
)
1744 * Is this device a bpf?
1746 if (major(dev
) != CDEV_MAJOR
) {
1750 if (kn
->kn_filter
!= EVFILT_READ
) {
1754 lck_mtx_lock(bpf_mlock
);
1756 d
= bpf_dtab
[minor(dev
)];
1757 if (d
== 0 || d
== (void *)1) {
1758 lck_mtx_unlock(bpf_mlock
);
1762 if (d
->bd_bif
== NULL
) {
1763 lck_mtx_unlock(bpf_mlock
);
1768 kn
->kn_fop
= &bpfread_filtops
;
1769 KNOTE_ATTACH(&d
->bd_sel
.si_note
, kn
);
1770 lck_mtx_unlock(bpf_mlock
);
1775 filt_bpfdetach(struct knote
*kn
)
1777 struct bpf_d
*d
= (struct bpf_d
*)kn
->kn_hook
;
1779 lck_mtx_lock(bpf_mlock
);
1780 KNOTE_DETACH(&d
->bd_sel
.si_note
, kn
);
1781 lck_mtx_unlock(bpf_mlock
);
1785 filt_bpfread(struct knote
*kn
, long hint
)
1787 struct bpf_d
*d
= (struct bpf_d
*)kn
->kn_hook
;
1791 lck_mtx_lock(bpf_mlock
);
1793 if (d
->bd_immediate
) {
1795 * If there's data in the hold buffer, it's the
1796 * amount of data a read will return.
1798 * If there's no data in the hold buffer, but
1799 * there's data in the store buffer, a read will
1800 * immediately rotate the store buffer to the
1801 * hold buffer, the amount of data in the store
1802 * buffer is the amount of data a read will
1805 * If there's no data in either buffer, we're not
1808 kn
->kn_data
= (d
->bd_hlen
== 0 ? d
->bd_slen
: d
->bd_hlen
);
1810 if (kn
->kn_sfflags
& NOTE_LOWAT
)
1812 if (kn
->kn_sdata
> d
->bd_bufsize
)
1813 lowwat
= d
->bd_bufsize
;
1814 else if (kn
->kn_sdata
> lowwat
)
1815 lowwat
= kn
->kn_sdata
;
1817 ready
= (kn
->kn_data
>= lowwat
);
1820 * If there's data in the hold buffer, it's the
1821 * amount of data a read will return.
1823 * If there's no data in the hold buffer, but
1824 * there's data in the store buffer, if the
1825 * timer has expired a read will immediately
1826 * rotate the store buffer to the hold buffer,
1827 * so the amount of data in the store buffer is
1828 * the amount of data a read will return.
1830 * If there's no data in either buffer, or there's
1831 * no data in the hold buffer and the timer hasn't
1832 * expired, we're not ready to read.
1834 kn
->kn_data
= (d
->bd_hlen
== 0 && d
->bd_state
== BPF_TIMED_OUT
?
1835 d
->bd_slen
: d
->bd_hlen
);
1836 ready
= (kn
->kn_data
> 0);
1842 lck_mtx_unlock(bpf_mlock
);
1847 _cast_non_const(const void * ptr
) {
1858 * Copy data from an mbuf chain into a buffer. This code is derived
1859 * from m_copydata in sys/uipc_mbuf.c.
1862 bpf_mcopy(const void *src_arg
, void *dst_arg
, size_t len
)
1864 struct mbuf
*m
= _cast_non_const(src_arg
);
1872 count
= min(m
->m_len
, len
);
1873 bcopy(mbuf_data(m
), dst
, count
);
1892 * It's possible that we get here after the bpf descriptor has been
1893 * detached from the interface; in such a case we simply return.
1894 * Lock ordering is important since we can be called asynchronously
1895 * (from the IOKit) to process an inbound packet; when that happens
1896 * we would have been holding its "gateLock" and will be acquiring
1897 * "bpf_mlock" upon entering this routine. Due to that, we release
1898 * "bpf_mlock" prior to calling ifnet_set_promiscuous (which will
1899 * acquire "gateLock" in the IOKit), in order to avoid a deadlock
1900 * when a ifnet_set_promiscuous request simultaneously collides with
1901 * an inbound packet being passed into the tap callback.
1903 lck_mtx_lock(bpf_mlock
);
1904 if (ifp
->if_bpf
== NULL
) {
1905 lck_mtx_unlock(bpf_mlock
);
1909 for (bp
= ifp
->if_bpf
; bp
&& bp
->bif_ifp
== ifp
&&
1910 (dlt
!= 0 && bp
->bif_dlt
!= dlt
); bp
= bp
->bif_next
)
1912 if (bp
&& bp
->bif_ifp
== ifp
&& bp
->bif_dlist
!= NULL
) {
1914 struct m_hdr hack_hdr
;
1921 * This is gross. We mock up an mbuf that points to the
1922 * header buffer. This means we don't have to copy the
1923 * header. A number of interfaces prepended headers just
1924 * for bpf by allocating an mbuf on the stack. We want to
1925 * give developers an easy way to prepend a header for bpf.
1926 * Since a developer allocating an mbuf on the stack is bad,
1927 * we do even worse here, allocating only a header to point
1928 * to a buffer the developer supplied. This makes assumptions
1929 * that bpf_filter and catchpacket will not look at anything
1930 * in the mbuf other than the header. This was true at the
1931 * time this code was written.
1933 hack_hdr
.mh_next
= m
;
1934 hack_hdr
.mh_nextpkt
= NULL
;
1935 hack_hdr
.mh_len
= hlen
;
1936 hack_hdr
.mh_data
= hdr
;
1937 hack_hdr
.mh_type
= m
->m_type
;
1938 hack_hdr
.mh_flags
= 0;
1940 m
= (mbuf_t
)&hack_hdr
;
1943 for (m0
= m
; m0
!= 0; m0
= m0
->m_next
)
1944 pktlen
+= m0
->m_len
;
1946 for (d
= bp
->bif_dlist
; d
; d
= d
->bd_next
) {
1947 if (outbound
&& !d
->bd_seesent
)
1950 slen
= bpf_filter(d
->bd_filter
, (u_char
*)m
, pktlen
, 0);
1953 if (mac_bpfdesc_check_receive(d
, bp
->bif_ifp
) != 0)
1956 catchpacket(d
, (u_char
*)m
, pktlen
, slen
, bpf_mcopy
);
1960 lck_mtx_unlock(bpf_mlock
);
1971 bpf_tap_imp(ifp
, dlt
, m
, hdr
, hlen
, 1);
1982 bpf_tap_imp(ifp
, dlt
, m
, hdr
, hlen
, 0);
1985 /* Callback registered with Ethernet driver. */
1986 static int bpf_tap_callback(struct ifnet
*ifp
, struct mbuf
*m
)
1988 bpf_tap_imp(ifp
, 0, m
, NULL
, 0, mbuf_pkthdr_rcvif(m
) == NULL
);
1994 * Move the packet data from interface memory (pkt) into the
1995 * store buffer. Return 1 if it's time to wakeup a listener (buffer full),
1996 * otherwise 0. "copy" is the routine called to do the actual data
1997 * transfer. bcopy is passed in to copy contiguous chunks, while
1998 * bpf_mcopy is passed in to copy mbuf chains. In the latter case,
1999 * pkt is really an mbuf.
2002 catchpacket(struct bpf_d
*d
, u_char
*pkt
, u_int pktlen
, u_int snaplen
,
2003 void (*cpfn
)(const void *, void *, size_t))
2007 int hdrlen
= d
->bd_bif
->bif_hdrlen
;
2010 * Figure out how many bytes to move. If the packet is
2011 * greater or equal to the snapshot length, transfer that
2012 * much. Otherwise, transfer the whole packet (unless
2013 * we hit the buffer size limit).
2015 totlen
= hdrlen
+ min(snaplen
, pktlen
);
2016 if (totlen
> d
->bd_bufsize
)
2017 totlen
= d
->bd_bufsize
;
2020 * Round up the end of the previous packet to the next longword.
2022 curlen
= BPF_WORDALIGN(d
->bd_slen
);
2023 if (curlen
+ totlen
> d
->bd_bufsize
) {
2025 * This packet will overflow the storage buffer.
2026 * Rotate the buffers if we can, then wakeup any
2029 if (d
->bd_fbuf
== NULL
) {
2031 * We haven't completed the previous read yet,
2032 * so drop the packet.
2041 else if (d
->bd_immediate
|| d
->bd_state
== BPF_TIMED_OUT
)
2043 * Immediate mode is set, or the read timeout has
2044 * already expired during a select call. A packet
2045 * arrived, so the reader should be woken up.
2050 * Append the bpf header.
2052 hp
= (struct bpf_hdr
*)(d
->bd_sbuf
+ curlen
);
2055 hp
->bh_tstamp
.tv_sec
= tv
.tv_sec
;
2056 hp
->bh_tstamp
.tv_usec
= tv
.tv_usec
;
2057 hp
->bh_datalen
= pktlen
;
2058 hp
->bh_hdrlen
= hdrlen
;
2060 * Copy the packet data into the store buffer and update its length.
2062 (*cpfn
)(pkt
, (u_char
*)hp
+ hdrlen
, (hp
->bh_caplen
= totlen
- hdrlen
));
2063 d
->bd_slen
= curlen
+ totlen
;
2070 * Initialize all nonzero fields of a descriptor.
2073 bpf_allocbufs(struct bpf_d
*d
)
2075 d
->bd_fbuf
= (caddr_t
) _MALLOC(d
->bd_bufsize
, M_DEVBUF
, M_WAIT
);
2076 if (d
->bd_fbuf
== 0)
2079 d
->bd_sbuf
= (caddr_t
) _MALLOC(d
->bd_bufsize
, M_DEVBUF
, M_WAIT
);
2080 if (d
->bd_sbuf
== 0) {
2081 FREE(d
->bd_fbuf
, M_DEVBUF
);
2090 * Free buffers currently in use by a descriptor.
2094 bpf_freed(struct bpf_d
*d
)
2097 * We don't need to lock out interrupts since this descriptor has
2098 * been detached from its interface and it yet hasn't been marked
2101 if (d
->bd_sbuf
!= 0) {
2102 FREE(d
->bd_sbuf
, M_DEVBUF
);
2103 if (d
->bd_hbuf
!= 0)
2104 FREE(d
->bd_hbuf
, M_DEVBUF
);
2105 if (d
->bd_fbuf
!= 0)
2106 FREE(d
->bd_fbuf
, M_DEVBUF
);
2109 FREE((caddr_t
)d
->bd_filter
, M_DEVBUF
);
2113 * Attach an interface to bpf. driverp is a pointer to a (struct bpf_if *)
2114 * in the driver's softc; dlt is the link layer type; hdrlen is the fixed
2115 * size of the link header (variable length headers not yet supported).
2118 bpfattach(struct ifnet
*ifp
, u_int dlt
, u_int hdrlen
)
2120 bpf_attach(ifp
, dlt
, hdrlen
, NULL
, NULL
);
2131 struct bpf_if
*bp_new
;
2132 struct bpf_if
*bp_temp
;
2133 struct bpf_if
*bp_first
= NULL
;
2135 bp_new
= (struct bpf_if
*) _MALLOC(sizeof(*bp_new
), M_DEVBUF
, M_WAIT
);
2139 lck_mtx_lock(bpf_mlock
);
2142 * Check if this interface/dlt is already attached, record first
2143 * attachment for this interface.
2145 for (bp_temp
= bpf_iflist
; bp_temp
&& (bp_temp
->bif_ifp
!= ifp
||
2146 bp_temp
->bif_dlt
!= dlt
); bp_temp
= bp_temp
->bif_next
) {
2147 if (bp_temp
->bif_ifp
== ifp
&& bp_first
== NULL
)
2151 if (bp_temp
!= NULL
) {
2152 printf("bpfattach - %s%d with dlt %d is already attached\n",
2153 ifp
->if_name
, ifp
->if_unit
, dlt
);
2154 FREE(bp_new
, M_DEVBUF
);
2155 lck_mtx_unlock(bpf_mlock
);
2159 bzero(bp_new
, sizeof(*bp_new
));
2160 bp_new
->bif_ifp
= ifp
;
2161 bp_new
->bif_dlt
= dlt
;
2162 bp_new
->bif_send
= send
;
2163 bp_new
->bif_tap
= tap
;
2165 if (bp_first
== NULL
) {
2166 /* No other entries for this ifp */
2167 bp_new
->bif_next
= bpf_iflist
;
2168 bpf_iflist
= bp_new
;
2171 /* Add this after the first entry for this interface */
2172 bp_new
->bif_next
= bp_first
->bif_next
;
2173 bp_first
->bif_next
= bp_new
;
2177 * Compute the length of the bpf header. This is not necessarily
2178 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
2179 * that the network layer header begins on a longword boundary (for
2180 * performance reasons and to alleviate alignment restrictions).
2182 bp_new
->bif_hdrlen
= BPF_WORDALIGN(hdrlen
+ SIZEOF_BPF_HDR
) - hdrlen
;
2184 /* Take a reference on the interface */
2185 ifnet_reference(ifp
);
2187 lck_mtx_unlock(bpf_mlock
);
2191 printf("bpf: %s%d attached\n", ifp
->if_name
, ifp
->if_unit
);
2198 * Detach bpf from an interface. This involves detaching each descriptor
2199 * associated with the interface, and leaving bd_bif NULL. Notify each
2200 * descriptor as it's detached so that any sleepers wake up and get
2204 bpfdetach(struct ifnet
*ifp
)
2206 struct bpf_if
*bp
, *bp_prev
, *bp_next
;
2207 struct bpf_if
*bp_free
= NULL
;
2211 lck_mtx_lock(bpf_mlock
);
2213 /* Locate BPF interface information */
2215 for (bp
= bpf_iflist
; bp
!= NULL
; bp
= bp_next
) {
2216 bp_next
= bp
->bif_next
;
2217 if (ifp
!= bp
->bif_ifp
) {
2222 while ((d
= bp
->bif_dlist
) != NULL
) {
2228 bp_prev
->bif_next
= bp
->bif_next
;
2230 bpf_iflist
= bp
->bif_next
;
2233 bp
->bif_next
= bp_free
;
2239 lck_mtx_unlock(bpf_mlock
);
2246 bpf_init(__unused
void *unused
)
2252 if (bpf_devsw_installed
== 0) {
2253 bpf_devsw_installed
= 1;
2255 bpf_mlock_grp_attr
= lck_grp_attr_alloc_init();
2257 bpf_mlock_grp
= lck_grp_alloc_init("bpf", bpf_mlock_grp_attr
);
2259 bpf_mlock_attr
= lck_attr_alloc_init();
2261 bpf_mlock
= lck_mtx_alloc_init(bpf_mlock_grp
, bpf_mlock_attr
);
2263 if (bpf_mlock
== 0) {
2264 printf("bpf_init: failed to allocate bpf_mlock\n");
2265 bpf_devsw_installed
= 0;
2269 maj
= cdevsw_add(CDEV_MAJOR
, &bpf_cdevsw
);
2272 lck_mtx_free(bpf_mlock
, bpf_mlock_grp
);
2274 lck_attr_free(bpf_mlock_attr
);
2276 lck_grp_free(bpf_mlock_grp
);
2277 if (bpf_mlock_grp_attr
)
2278 lck_grp_attr_free(bpf_mlock_grp_attr
);
2281 bpf_mlock_attr
= NULL
;
2282 bpf_mlock_grp
= NULL
;
2283 bpf_mlock_grp_attr
= NULL
;
2284 bpf_devsw_installed
= 0;
2285 printf("bpf_init: failed to allocate a major number!\n");
2289 for (i
= 0 ; i
< NBPFILTER
; i
++)
2290 bpf_make_dev_t(maj
);
2293 cdevsw_add(&bpf_cdevsw
);
2298 SYSINIT(bpfdev
,SI_SUB_DRIVERS
,SI_ORDER_MIDDLE
+CDEV_MAJOR
,bpf_drvinit
,NULL
)
2303 mac_bpfdesc_label_get(struct bpf_d
*d
)
2306 return (d
->bd_label
);
2310 mac_bpfdesc_label_set(struct bpf_d
*d
, struct label
*label
)
2313 d
->bd_label
= label
;