2 * Copyright (c) 2000-2008 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>
124 #include <security/mac_framework.h>
127 extern int tvtohz(struct timeval
*);
130 * Older BSDs don't have kernel malloc.
134 static caddr_t
bpf_alloc();
135 #include <net/bpf_compat.h>
136 #define BPF_BUFSIZE (MCLBYTES-8)
137 #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, code, uio)
139 #define BPF_BUFSIZE 4096
140 #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, uio)
144 #define PRINET 26 /* interruptible */
147 * The default read buffer size is patchable.
149 static unsigned int bpf_bufsize
= BPF_BUFSIZE
;
150 SYSCTL_INT(_debug
, OID_AUTO
, bpf_bufsize
, CTLFLAG_RW
,
151 &bpf_bufsize
, 0, "");
152 static unsigned int bpf_maxbufsize
= BPF_MAXBUFSIZE
;
153 SYSCTL_INT(_debug
, OID_AUTO
, bpf_maxbufsize
, CTLFLAG_RW
,
154 &bpf_maxbufsize
, 0, "");
155 static unsigned int bpf_maxdevices
= 256;
156 SYSCTL_UINT(_debug
, OID_AUTO
, bpf_maxdevices
, CTLFLAG_RW
,
157 &bpf_maxdevices
, 0, "");
160 * bpf_iflist is the list of interfaces; each corresponds to an ifnet
161 * bpf_dtab holds pointer to the descriptors, indexed by minor device #
163 static struct bpf_if
*bpf_iflist
;
166 * BSD now stores the bpf_d in the dev_t which is a struct
167 * on their system. Our dev_t is an int, so we still store
168 * the bpf_d in a separate table indexed by minor device #.
170 * The value stored in bpf_dtab[n] represent three states:
171 * 0: device not opened
172 * 1: device opening or closing
173 * other: device <n> opened with pointer to storage
175 static struct bpf_d
**bpf_dtab
= NULL
;
176 static unsigned int bpf_dtab_size
= 0;
177 static unsigned int nbpfilter
= 0;
179 static lck_mtx_t
*bpf_mlock
;
180 static lck_grp_t
*bpf_mlock_grp
;
181 static lck_grp_attr_t
*bpf_mlock_grp_attr
;
182 static lck_attr_t
*bpf_mlock_attr
;
185 * Mark a descriptor free by making it point to itself.
186 * This is probably cheaper than marking with a constant since
187 * the address should be in a register anyway.
189 #endif /* __APPLE__ */
191 static int bpf_allocbufs(struct bpf_d
*);
192 static errno_t
bpf_attachd(struct bpf_d
*d
, struct bpf_if
*bp
);
193 static void bpf_detachd(struct bpf_d
*d
);
194 static void bpf_freed(struct bpf_d
*);
195 static void bpf_mcopy(const void *, void *, size_t);
196 static int bpf_movein(struct uio
*, int,
197 struct mbuf
**, struct sockaddr
*, int *);
198 static int bpf_setif(struct bpf_d
*, ifnet_t ifp
, u_int32_t dlt
);
199 static void bpf_wakeup(struct bpf_d
*);
200 static void catchpacket(struct bpf_d
*, u_char
*, u_int
,
201 u_int
, void (*)(const void *, void *, size_t));
202 static void reset_d(struct bpf_d
*);
203 static int bpf_setf(struct bpf_d
*, u_int bf_len
, user_addr_t bf_insns
);
204 static int bpf_getdltlist(struct bpf_d
*, struct bpf_dltlist
*,
206 static int bpf_setdlt(struct bpf_d
*, u_int
);
208 /*static void *bpf_devfs_token[MAXBPFILTER];*/
210 static int bpf_devsw_installed
;
212 void bpf_init(void *unused
);
213 static int bpf_tap_callback(struct ifnet
*ifp
, struct mbuf
*m
);
216 * Darwin differs from BSD here, the following are static
217 * on BSD and not static on Darwin.
223 ioctl_fcn_t bpfioctl
;
224 select_fcn_t bpfpoll
;
227 /* Darwin's cdevsw struct differs slightly from BSDs */
228 #define CDEV_MAJOR 23
229 static struct cdevsw bpf_cdevsw
= {
231 /* close */ bpfclose
,
233 /* write */ bpfwrite
,
234 /* ioctl */ bpfioctl
,
236 /* reset */ eno_reset
,
238 /* select */ bpfpoll
,
240 /* strategy*/ eno_strat
,
246 #define SOCKADDR_HDR_LEN offsetof(struct sockaddr, sa_data)
249 bpf_movein(struct uio
*uio
, int linktype
, struct mbuf
**mp
, struct sockaddr
*sockp
, int *datlen
)
267 sa_family
= AF_UNSPEC
;
268 /* XXX Would MAXLINKHDR be better? */
269 hlen
= sizeof(struct ether_header
);
274 #if defined(__FreeBSD__) || defined(__bsdi__)
275 sa_family
= AF_IMPLINK
;
278 sa_family
= AF_UNSPEC
;
279 /* XXX 4(FORMAC)+6(dst)+6(src)+3(LLC)+5(SNAP) */
287 sa_family
= AF_UNSPEC
;
292 case DLT_ATM_RFC1483
:
294 * en atm driver requires 4-byte atm pseudo header.
295 * though it isn't standard, vpi:vci needs to be
298 sa_family
= AF_UNSPEC
;
299 hlen
= 12; /* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
304 sa_family
= AF_UNSPEC
;
305 hlen
= 4; /* This should match PPP_HDRLEN */
308 case DLT_APPLE_IP_OVER_IEEE1394
:
309 sa_family
= AF_UNSPEC
;
310 hlen
= sizeof(struct firewire_header
);
313 case DLT_IEEE802_11
: /* IEEE 802.11 wireless */
314 sa_family
= AF_IEEE80211
;
322 // LP64todo - fix this!
323 len
= uio_resid(uio
);
324 *datlen
= len
- hlen
;
325 if ((unsigned)len
> MCLBYTES
)
330 * Build a sockaddr based on the data link layer type.
331 * We do this at this level because the ethernet header
332 * is copied directly into the data field of the sockaddr.
333 * In the case of SLIP, there is no header and the packet
334 * is forwarded as is.
335 * Also, we are careful to leave room at the front of the mbuf
336 * for the link level header.
338 if ((hlen
+ SOCKADDR_HDR_LEN
) > sockp
->sa_len
) {
341 sockp
->sa_family
= sa_family
;
344 * We're directly sending the packet data supplied by
345 * the user; we don't need to make room for the link
346 * header, and don't need the header length value any
347 * more, so set it to 0.
352 MGETHDR(m
, M_WAIT
, MT_DATA
);
355 if ((unsigned)len
> MHLEN
) {
358 if ((m
->m_flags
& M_EXT
) == 0) {
361 if (m
->m_len
!= MCLBYTES
) {
367 m
->m_pkthdr
.len
= m
->m_len
= len
;
368 m
->m_pkthdr
.rcvif
= NULL
;
371 * Make room for link header.
374 m
->m_pkthdr
.len
-= hlen
;
377 m
->m_data
+= hlen
; /* XXX */
381 error
= UIOMOVE((caddr_t
)sockp
->sa_data
, hlen
, UIO_WRITE
, uio
);
385 error
= UIOMOVE(mtod(m
, caddr_t
), len
- hlen
, UIO_WRITE
, uio
);
396 * The dynamic addition of a new device node must block all processes that are opening
397 * the last device so that no process will get an unexpected ENOENT
400 bpf_make_dev_t(int maj
)
402 static int bpf_growing
= 0;
403 unsigned int cur_size
= nbpfilter
, i
;
405 if (nbpfilter
>= bpf_maxdevices
)
408 while (bpf_growing
) {
409 /* Wait until new device has been created */
410 (void)tsleep((caddr_t
)&bpf_growing
, PZERO
, "bpf_growing", 0);
412 if (nbpfilter
> cur_size
) {
413 /* other thread grew it already */
418 /* need to grow bpf_dtab first */
419 if (nbpfilter
== bpf_dtab_size
) {
421 struct bpf_d
**new_dtab
= NULL
;
422 struct bpf_d
**old_dtab
= NULL
;
424 new_dtab_size
= bpf_dtab_size
+ NBPFILTER
;
425 new_dtab
= (struct bpf_d
**)_MALLOC(sizeof(struct bpf_d
*) * new_dtab_size
, M_DEVBUF
, M_WAIT
);
427 printf("bpf_make_dev_t: malloc bpf_dtab failed\n");
431 bcopy(bpf_dtab
, new_dtab
,
432 sizeof(struct bpf_d
*) * bpf_dtab_size
);
434 bzero(new_dtab
+ bpf_dtab_size
,
435 sizeof(struct bpf_d
*) * NBPFILTER
);
438 bpf_dtab_size
= new_dtab_size
;
439 if (old_dtab
!= NULL
)
440 _FREE(old_dtab
, M_DEVBUF
);
443 (void) devfs_make_node(makedev(maj
, i
),
444 DEVFS_CHAR
, UID_ROOT
, GID_WHEEL
, 0600,
448 wakeup((caddr_t
)&bpf_growing
);
454 * Attach file to the bpf interface, i.e. make d listen on bp.
457 bpf_attachd(struct bpf_d
*d
, struct bpf_if
*bp
)
459 int first
= bp
->bif_dlist
== NULL
;
463 * Point d at bp, and add d to the interface's list of listeners.
464 * Finally, point the driver's bpf cookie at the interface so
465 * it will divert packets to bpf.
468 d
->bd_next
= bp
->bif_dlist
;
472 /* Find the default bpf entry for this ifp */
473 if (bp
->bif_ifp
->if_bpf
== NULL
) {
474 struct bpf_if
*primary
;
476 for (primary
= bpf_iflist
; primary
&& primary
->bif_ifp
!= bp
->bif_ifp
;
477 primary
= primary
->bif_next
)
480 bp
->bif_ifp
->if_bpf
= primary
;
483 /* Only call dlil_set_bpf_tap for primary dlt */
484 if (bp
->bif_ifp
->if_bpf
== bp
)
485 dlil_set_bpf_tap(bp
->bif_ifp
, BPF_TAP_INPUT_OUTPUT
, bpf_tap_callback
);
488 error
= bp
->bif_tap(bp
->bif_ifp
, bp
->bif_dlt
, BPF_TAP_INPUT_OUTPUT
);
495 * Detach a file from its interface.
498 bpf_detachd(struct bpf_d
*d
)
504 ifp
= d
->bd_bif
->bif_ifp
;
507 /* Remove d from the interface's descriptor list. */
512 panic("bpf_detachd: descriptor not in list");
515 if (bp
->bif_dlist
== 0) {
517 * Let the driver know that there are no more listeners.
519 /* Only call dlil_set_bpf_tap for primary dlt */
520 if (bp
->bif_ifp
->if_bpf
== bp
)
521 dlil_set_bpf_tap(ifp
, BPF_TAP_DISABLE
, NULL
);
523 bp
->bif_tap(ifp
, bp
->bif_dlt
, BPF_TAP_DISABLE
);
525 for (bp
= bpf_iflist
; bp
; bp
= bp
->bif_next
)
526 if (bp
->bif_ifp
== ifp
&& bp
->bif_dlist
!= 0)
533 * Check if this descriptor had requested promiscuous mode.
534 * If so, turn it off.
538 lck_mtx_unlock(bpf_mlock
);
539 if (ifnet_set_promiscuous(ifp
, 0)) {
541 * Something is really wrong if we were able to put
542 * the driver into promiscuous mode, but can't
544 * Most likely the network interface is gone.
546 printf("bpf: ifnet_set_promiscuous failed");
548 lck_mtx_lock(bpf_mlock
);
554 * Open ethernet device. Returns ENXIO for illegal minor device number,
555 * EBUSY if file is open by another process.
559 bpfopen(dev_t dev
, int flags
, __unused
int fmt
,
560 __unused
struct proc
*p
)
564 lck_mtx_lock(bpf_mlock
);
565 if ((unsigned int) minor(dev
) >= nbpfilter
) {
566 lck_mtx_unlock(bpf_mlock
);
570 * New device nodes are created on demand when opening the last one.
571 * The programming model is for processes to loop on the minor starting at 0
572 * as long as EBUSY is returned. The loop stops when either the open succeeds or
573 * an error other that EBUSY is returned. That means that bpf_make_dev_t() must
574 * block all processes that are opening the last node. If not all
575 * processes are blocked, they could unexpectedly get ENOENT and abort their
578 if ((unsigned int) minor(dev
) == (nbpfilter
- 1))
579 bpf_make_dev_t(major(dev
));
582 * Each minor can be opened by only one process. If the requested
583 * minor is in use, return EBUSY.
585 * Important: bpfopen() and bpfclose() have to check and set the status of a device
586 * in the same lockin context otherwise the device may be leaked because the vnode use count
587 * will be unpextectly greater than 1 when close() is called.
589 if (bpf_dtab
[minor(dev
)] == 0) {
590 bpf_dtab
[minor(dev
)] = (void *)1; /* Mark opening */
592 lck_mtx_unlock(bpf_mlock
);
595 d
= (struct bpf_d
*)_MALLOC(sizeof(struct bpf_d
), M_DEVBUF
, M_WAIT
);
597 /* this really is a catastrophic failure */
598 printf("bpfopen: malloc bpf_d failed\n");
599 bpf_dtab
[minor(dev
)] = NULL
;
600 lck_mtx_unlock(bpf_mlock
);
603 bzero(d
, sizeof(struct bpf_d
));
606 * It is not necessary to take the BPF lock here because no other
607 * thread can access the device until it is marked opened...
610 /* Mark "in use" and do most initialization. */
611 d
->bd_bufsize
= bpf_bufsize
;
614 d
->bd_oflags
= flags
;
616 mac_bpfdesc_label_init(d
);
617 mac_bpfdesc_label_associate(kauth_cred_get(), d
);
619 bpf_dtab
[minor(dev
)] = d
; /* Mark opened */
620 lck_mtx_unlock(bpf_mlock
);
626 * Close the descriptor by detaching it from its interface,
627 * deallocating its buffers, and marking it free.
631 bpfclose(dev_t dev
, __unused
int flags
, __unused
int fmt
,
632 __unused
struct proc
*p
)
636 /* Take BPF lock to ensure no other thread is using the device */
637 lck_mtx_lock(bpf_mlock
);
639 d
= bpf_dtab
[minor(dev
)];
640 if (d
== 0 || d
== (void *)1) {
641 lck_mtx_unlock(bpf_mlock
);
644 bpf_dtab
[minor(dev
)] = (void *)1; /* Mark closing */
648 selthreadclear(&d
->bd_sel
);
650 mac_bpfdesc_label_destroy(d
);
654 /* Mark free in same context as bpfopen comes to check */
655 bpf_dtab
[minor(dev
)] = NULL
; /* Mark closed */
656 lck_mtx_unlock(bpf_mlock
);
664 #define BPF_SLEEP bpf_sleep
667 bpf_sleep(struct bpf_d
*d
, int pri
, const char *wmesg
, int timo
)
671 lck_mtx_unlock(bpf_mlock
);
673 st
= tsleep((caddr_t
)d
, pri
, wmesg
, timo
);
675 lck_mtx_lock(bpf_mlock
);
681 * Rotate the packet buffers in descriptor d. Move the store buffer
682 * into the hold slot, and the free buffer into the store slot.
683 * Zero the length of the new store buffer.
685 #define ROTATE_BUFFERS(d) \
686 (d)->bd_hbuf = (d)->bd_sbuf; \
687 (d)->bd_hlen = (d)->bd_slen; \
688 (d)->bd_sbuf = (d)->bd_fbuf; \
692 * bpfread - read next chunk of packets from buffers
695 bpfread(dev_t dev
, struct uio
*uio
, int ioflag
)
700 lck_mtx_lock(bpf_mlock
);
702 d
= bpf_dtab
[minor(dev
)];
703 if (d
== 0 || d
== (void *)1) {
704 lck_mtx_unlock(bpf_mlock
);
710 * Restrict application to use a buffer the same size as
713 if (uio_resid(uio
) != d
->bd_bufsize
) {
714 lck_mtx_unlock(bpf_mlock
);
719 * If the hold buffer is empty, then do a timed sleep, which
720 * ends when the timeout expires or when enough packets
721 * have arrived to fill the store buffer.
723 while (d
->bd_hbuf
== 0) {
724 if (d
->bd_immediate
&& d
->bd_slen
!= 0) {
726 * A packet(s) either arrived since the previous
727 * read or arrived while we were asleep.
728 * Rotate the buffers and return what's here.
735 * No data is available, check to see if the bpf device
736 * is still pointed at a real interface. If not, return
737 * ENXIO so that the userland process knows to rebind
738 * it before using it again.
740 if (d
->bd_bif
== NULL
) {
741 lck_mtx_unlock(bpf_mlock
);
744 if (ioflag
& IO_NDELAY
) {
745 lck_mtx_unlock(bpf_mlock
);
746 return (EWOULDBLOCK
);
748 error
= BPF_SLEEP(d
, PRINET
|PCATCH
, "bpf",
751 * Make sure device is still opened
753 d
= bpf_dtab
[minor(dev
)];
754 if (d
== 0 || d
== (void *)1) {
755 lck_mtx_unlock(bpf_mlock
);
758 if (error
== EINTR
|| error
== ERESTART
) {
759 lck_mtx_unlock(bpf_mlock
);
762 if (error
== EWOULDBLOCK
) {
764 * On a timeout, return what's in the buffer,
765 * which may be nothing. If there is something
766 * in the store buffer, we can rotate the buffers.
770 * We filled up the buffer in between
771 * getting the timeout and arriving
772 * here, so we don't need to rotate.
776 if (d
->bd_slen
== 0) {
777 lck_mtx_unlock(bpf_mlock
);
785 * At this point, we know we have something in the hold slot.
789 * Move data from hold buffer into user space.
790 * We know the entire buffer is transferred since
791 * we checked above that the read buffer is bpf_bufsize bytes.
793 error
= UIOMOVE(d
->bd_hbuf
, d
->bd_hlen
, UIO_READ
, uio
);
795 d
->bd_fbuf
= d
->bd_hbuf
;
798 lck_mtx_unlock(bpf_mlock
);
804 * If there are processes sleeping on this descriptor, wake them up.
807 bpf_wakeup(struct bpf_d
*d
)
810 if (d
->bd_async
&& d
->bd_sig
&& d
->bd_sigio
)
811 pgsigio(d
->bd_sigio
, d
->bd_sig
);
814 selwakeup(&d
->bd_sel
);
815 KNOTE(&d
->bd_sel
.si_note
, 1);
818 d
->bd_sel
.si_pid
= 0;
822 selwakeup(d
->bd_selproc
, (int)d
->bd_selcoll
);
829 /* keep in sync with bpf_movein above: */
830 #define MAX_DATALINK_HDR_LEN (sizeof(struct firewire_header))
833 bpfwrite(dev_t dev
, struct uio
*uio
, __unused
int ioflag
)
837 struct mbuf
*m
= NULL
;
839 char dst_buf
[SOCKADDR_HDR_LEN
+ MAX_DATALINK_HDR_LEN
];
842 lck_mtx_lock(bpf_mlock
);
844 d
= bpf_dtab
[minor(dev
)];
845 if (d
== 0 || d
== (void *)1) {
846 lck_mtx_unlock(bpf_mlock
);
849 if (d
->bd_bif
== 0) {
850 lck_mtx_unlock(bpf_mlock
);
854 ifp
= d
->bd_bif
->bif_ifp
;
856 if (uio_resid(uio
) == 0) {
857 lck_mtx_unlock(bpf_mlock
);
860 ((struct sockaddr
*)dst_buf
)->sa_len
= sizeof(dst_buf
);
861 error
= bpf_movein(uio
, (int)d
->bd_bif
->bif_dlt
, &m
,
862 d
->bd_hdrcmplt
? NULL
: (struct sockaddr
*)dst_buf
,
865 lck_mtx_unlock(bpf_mlock
);
869 if ((unsigned)datlen
> ifp
->if_mtu
) {
870 lck_mtx_unlock(bpf_mlock
);
875 if ((error
= ifp_use(ifp
, kIfNetUseCount_MustNotBeZero
)) != 0) {
876 lck_mtx_unlock(bpf_mlock
);
882 mac_mbuf_label_associate_bpfdesc(d
, m
);
884 lck_mtx_unlock(bpf_mlock
);
886 if (d
->bd_hdrcmplt
) {
887 if (d
->bd_bif
->bif_send
)
888 error
= d
->bd_bif
->bif_send(ifp
, d
->bd_bif
->bif_dlt
, m
);
890 error
= dlil_output(ifp
, 0, m
, NULL
, NULL
, 1);
893 error
= dlil_output(ifp
, PF_INET
, m
, NULL
, (struct sockaddr
*)dst_buf
, 0);
896 if (ifp_unuse(ifp
) != 0)
897 ifp_use_reached_zero(ifp
);
900 * The driver frees the mbuf.
906 * Reset a descriptor by flushing its packet buffer and clearing the
907 * receive and drop counts.
910 reset_d(struct bpf_d
*d
)
913 /* Free the hold buffer. */
914 d
->bd_fbuf
= d
->bd_hbuf
;
924 * FIONREAD Check for read packet available.
925 * SIOCGIFADDR Get interface address - convenient hook to driver.
926 * BIOCGBLEN Get buffer len [for read()].
927 * BIOCSETF Set ethernet read filter.
928 * BIOCFLUSH Flush read packet buffer.
929 * BIOCPROMISC Put interface into promiscuous mode.
930 * BIOCGDLT Get link layer type.
931 * BIOCGETIF Get interface name.
932 * BIOCSETIF Set interface.
933 * BIOCSRTIMEOUT Set read timeout.
934 * BIOCGRTIMEOUT Get read timeout.
935 * BIOCGSTATS Get packet stats.
936 * BIOCIMMEDIATE Set immediate mode.
937 * BIOCVERSION Get filter language version.
938 * BIOCGHDRCMPLT Get "header already complete" flag
939 * BIOCSHDRCMPLT Set "header already complete" flag
940 * BIOCGSEESENT Get "see packets sent" flag
941 * BIOCSSEESENT Set "see packets sent" flag
945 bpfioctl(dev_t dev
, u_long cmd
, caddr_t addr
, __unused
int flags
,
951 lck_mtx_lock(bpf_mlock
);
953 d
= bpf_dtab
[minor(dev
)];
954 if (d
== 0 || d
== (void *)1) {
955 lck_mtx_unlock(bpf_mlock
);
966 * Check for read packet available.
987 ifp
= d
->bd_bif
->bif_ifp
;
988 error
= ifnet_ioctl(ifp
, 0, cmd
, addr
);
994 * Get buffer len [for read()].
997 *(u_int
*)addr
= d
->bd_bufsize
;
1001 * Set buffer length.
1010 u_int size
= *(u_int
*)addr
;
1012 if (size
> bpf_maxbufsize
)
1013 *(u_int
*)addr
= size
= bpf_maxbufsize
;
1014 else if (size
< BPF_MINBUFSIZE
)
1015 *(u_int
*)addr
= size
= BPF_MINBUFSIZE
;
1016 d
->bd_bufsize
= size
;
1022 * Set link layer read filter.
1025 struct bpf_program32
*prg32
= (struct bpf_program32
*)addr
;
1026 error
= bpf_setf(d
, prg32
->bf_len
,
1027 CAST_USER_ADDR_T(prg32
->bf_insns
));
1032 struct bpf_program64
*prg64
= (struct bpf_program64
*)addr
;
1033 error
= bpf_setf(d
, prg64
->bf_len
, prg64
->bf_insns
);
1038 * Flush read packet buffer.
1045 * Put interface into promiscuous mode.
1048 if (d
->bd_bif
== 0) {
1050 * No interface attached yet.
1055 if (d
->bd_promisc
== 0) {
1056 lck_mtx_unlock(bpf_mlock
);
1057 error
= ifnet_set_promiscuous(d
->bd_bif
->bif_ifp
, 1);
1058 lck_mtx_lock(bpf_mlock
);
1065 * Get device parameters.
1071 *(u_int
*)addr
= d
->bd_bif
->bif_dlt
;
1075 * Get a list of supported data link types.
1078 if (d
->bd_bif
== NULL
) {
1081 error
= bpf_getdltlist(d
,
1082 (struct bpf_dltlist
*)addr
, p
);
1087 * Set data link type.
1090 if (d
->bd_bif
== NULL
)
1093 error
= bpf_setdlt(d
, *(u_int
*)addr
);
1097 * Get interface name.
1103 struct ifnet
*const ifp
= d
->bd_bif
->bif_ifp
;
1104 struct ifreq
*const ifr
= (struct ifreq
*)addr
;
1106 snprintf(ifr
->ifr_name
, sizeof(ifr
->ifr_name
),
1107 "%s%d", ifp
->if_name
, ifp
->if_unit
);
1116 ifp
= ifunit(((struct ifreq
*)addr
)->ifr_name
);
1120 error
= bpf_setif(d
, ifp
, 0);
1129 struct BPF_TIMEVAL
*_tv
= (struct BPF_TIMEVAL
*)addr
;
1132 tv
.tv_sec
= _tv
->tv_sec
;
1133 tv
.tv_usec
= _tv
->tv_usec
;
1136 * Subtract 1 tick from tvtohz() since this isn't
1139 if ((error
= itimerfix(&tv
)) == 0)
1140 d
->bd_rtout
= tvtohz(&tv
) - 1;
1149 struct BPF_TIMEVAL
*tv
= (struct BPF_TIMEVAL
*)addr
;
1151 tv
->tv_sec
= d
->bd_rtout
/ hz
;
1152 tv
->tv_usec
= (d
->bd_rtout
% hz
) * tick
;
1161 struct bpf_stat
*bs
= (struct bpf_stat
*)addr
;
1163 bs
->bs_recv
= d
->bd_rcount
;
1164 bs
->bs_drop
= d
->bd_dcount
;
1169 * Set immediate mode.
1172 d
->bd_immediate
= *(u_int
*)addr
;
1177 struct bpf_version
*bv
= (struct bpf_version
*)addr
;
1179 bv
->bv_major
= BPF_MAJOR_VERSION
;
1180 bv
->bv_minor
= BPF_MINOR_VERSION
;
1185 * Get "header already complete" flag
1188 *(u_int
*)addr
= d
->bd_hdrcmplt
;
1192 * Set "header already complete" flag
1195 d
->bd_hdrcmplt
= *(u_int
*)addr
? 1 : 0;
1199 * Get "see sent packets" flag
1202 *(u_int
*)addr
= d
->bd_seesent
;
1206 * Set "see sent packets" flag
1209 d
->bd_seesent
= *(u_int
*)addr
;
1212 case FIONBIO
: /* Non-blocking I/O */
1215 case FIOASYNC
: /* Send signal on receive packets */
1216 d
->bd_async
= *(int *)addr
;
1220 error
= fsetown(*(int *)addr
, &d
->bd_sigio
);
1224 *(int *)addr
= fgetown(d
->bd_sigio
);
1227 /* This is deprecated, FIOSETOWN should be used instead. */
1229 error
= fsetown(-(*(int *)addr
), &d
->bd_sigio
);
1232 /* This is deprecated, FIOGETOWN should be used instead. */
1234 *(int *)addr
= -fgetown(d
->bd_sigio
);
1237 case BIOCSRSIG
: /* Set receive signal */
1241 sig
= *(u_int
*)addr
;
1250 *(u_int
*)addr
= d
->bd_sig
;
1254 lck_mtx_unlock(bpf_mlock
);
1260 * Set d's packet filter program to fp. If this file already has a filter,
1261 * free it and replace it. Returns EINVAL for bogus requests.
1264 bpf_setf(struct bpf_d
*d
, u_int bf_len
, user_addr_t bf_insns
)
1266 struct bpf_insn
*fcode
, *old
;
1270 if (bf_insns
== USER_ADDR_NULL
) {
1273 d
->bd_filter
= NULL
;
1276 FREE((caddr_t
)old
, M_DEVBUF
);
1280 if (flen
> BPF_MAXINSNS
)
1283 size
= flen
* sizeof(struct bpf_insn
);
1284 fcode
= (struct bpf_insn
*) _MALLOC(size
, M_DEVBUF
, M_WAIT
);
1289 if (copyin(bf_insns
, (caddr_t
)fcode
, size
) == 0 &&
1290 bpf_validate(fcode
, (int)flen
)) {
1291 d
->bd_filter
= fcode
;
1294 FREE((caddr_t
)old
, M_DEVBUF
);
1298 FREE((caddr_t
)fcode
, M_DEVBUF
);
1303 * Detach a file from its current interface (if attached at all) and attach
1304 * to the interface indicated by the name stored in ifr.
1305 * Return an errno or 0.
1308 bpf_setif(struct bpf_d
*d
, ifnet_t theywant
, u_int32_t dlt
)
1314 * Look through attached interfaces for the named one.
1316 for (bp
= bpf_iflist
; bp
!= 0; bp
= bp
->bif_next
) {
1317 struct ifnet
*ifp
= bp
->bif_ifp
;
1319 if (ifp
== 0 || ifp
!= theywant
|| (dlt
!= 0 && dlt
!= bp
->bif_dlt
))
1322 * We found the requested interface.
1323 * If it's not up, return an error.
1324 * Allocate the packet buffers if we need to.
1325 * If we're already attached to requested interface,
1326 * just flush the buffer.
1328 if ((ifp
->if_flags
& IFF_UP
) == 0)
1331 if (d
->bd_sbuf
== 0) {
1332 error
= bpf_allocbufs(d
);
1336 if (bp
!= d
->bd_bif
) {
1339 * Detach if attached to something else.
1343 if (bpf_attachd(d
, bp
) != 0) {
1357 * Get a list of available data link type of the interface.
1360 bpf_getdltlist(struct bpf_d
*d
, struct bpf_dltlist
*bfl
, struct proc
*p
)
1368 if (proc_is64bit(p
)) {
1369 dlist
= (user_addr_t
)bfl
->bfl_u
.bflu_pad
;
1371 dlist
= CAST_USER_ADDR_T(bfl
->bfl_u
.bflu_list
);
1374 ifp
= d
->bd_bif
->bif_ifp
;
1377 for (bp
= bpf_iflist
; bp
; bp
= bp
->bif_next
) {
1378 if (bp
->bif_ifp
!= ifp
)
1380 if (dlist
!= USER_ADDR_NULL
) {
1381 if (n
>= bfl
->bfl_len
) {
1384 error
= copyout(&bp
->bif_dlt
, dlist
,
1385 sizeof (bp
->bif_dlt
));
1386 dlist
+= sizeof (bp
->bif_dlt
);
1395 * Set the data link type of a BPF instance.
1398 bpf_setdlt(struct bpf_d
*d
, uint32_t dlt
)
1402 int error
, opromisc
;
1406 if (d
->bd_bif
->bif_dlt
== dlt
)
1408 ifp
= d
->bd_bif
->bif_ifp
;
1409 for (bp
= bpf_iflist
; bp
; bp
= bp
->bif_next
) {
1410 if (bp
->bif_ifp
== ifp
&& bp
->bif_dlt
== dlt
)
1414 opromisc
= d
->bd_promisc
;
1416 error
= bpf_attachd(d
, bp
);
1418 printf("bpf_setdlt: bpf_attachd %s%d failed (%d)\n",
1419 ifnet_name(bp
->bif_ifp
), ifnet_unit(bp
->bif_ifp
), error
);
1424 lck_mtx_unlock(bpf_mlock
);
1425 error
= ifnet_set_promiscuous(bp
->bif_ifp
, 1);
1426 lck_mtx_lock(bpf_mlock
);
1428 printf("bpf_setdlt: ifpromisc %s%d failed (%d)\n",
1429 ifnet_name(bp
->bif_ifp
), ifnet_unit(bp
->bif_ifp
), error
);
1434 return (bp
== NULL
? EINVAL
: 0);
1438 * Support for select()
1440 * Return true iff the specific operation will not block indefinitely.
1441 * Otherwise, return false but make a note that a selwakeup() must be done.
1444 bpfpoll(dev_t dev
, int events
, void * wql
, struct proc
*p
)
1449 lck_mtx_lock(bpf_mlock
);
1451 d
= bpf_dtab
[minor(dev
)];
1452 if (d
== 0 || d
== (void *)1) {
1453 lck_mtx_unlock(bpf_mlock
);
1458 * An imitation of the FIONREAD ioctl code.
1460 if (d
->bd_bif
== NULL
) {
1461 lck_mtx_unlock(bpf_mlock
);
1465 if (events
& (POLLIN
| POLLRDNORM
)) {
1466 if (d
->bd_hlen
!= 0 || (d
->bd_immediate
&& d
->bd_slen
!= 0))
1467 revents
|= events
& (POLLIN
| POLLRDNORM
);
1469 selrecord(p
, &d
->bd_sel
, wql
);
1472 lck_mtx_unlock(bpf_mlock
);
1477 * Support for kevent() system call. Register EVFILT_READ filters and
1478 * reject all others.
1480 int bpfkqfilter(dev_t dev
, struct knote
*kn
);
1481 static void filt_bpfdetach(struct knote
*);
1482 static int filt_bpfread(struct knote
*, long);
1484 static struct filterops bpfread_filtops
= {
1486 .f_detach
= filt_bpfdetach
,
1487 .f_event
= filt_bpfread
,
1491 bpfkqfilter(dev_t dev
, struct knote
*kn
)
1496 * Is this device a bpf?
1498 if (major(dev
) != CDEV_MAJOR
) {
1502 if (kn
->kn_filter
!= EVFILT_READ
) {
1506 lck_mtx_lock(bpf_mlock
);
1508 d
= bpf_dtab
[minor(dev
)];
1509 if (d
== 0 || d
== (void *)1) {
1510 lck_mtx_unlock(bpf_mlock
);
1515 * An imitation of the FIONREAD ioctl code.
1517 if (d
->bd_bif
== NULL
) {
1518 lck_mtx_unlock(bpf_mlock
);
1523 kn
->kn_fop
= &bpfread_filtops
;
1524 KNOTE_ATTACH(&d
->bd_sel
.si_note
, kn
);
1525 lck_mtx_unlock(bpf_mlock
);
1530 filt_bpfdetach(struct knote
*kn
)
1532 struct bpf_d
*d
= (struct bpf_d
*)kn
->kn_hook
;
1534 lck_mtx_lock(bpf_mlock
);
1535 KNOTE_DETACH(&d
->bd_sel
.si_note
, kn
);
1536 lck_mtx_unlock(bpf_mlock
);
1540 filt_bpfread(struct knote
*kn
, long hint
)
1542 struct bpf_d
*d
= (struct bpf_d
*)kn
->kn_hook
;
1546 lck_mtx_lock(bpf_mlock
);
1548 if (d
->bd_immediate
) {
1549 kn
->kn_data
= (d
->bd_hlen
== 0 ? d
->bd_slen
: d
->bd_hlen
);
1550 ready
= (kn
->kn_data
>= ((kn
->kn_sfflags
& NOTE_LOWAT
) ?
1553 kn
->kn_data
= d
->bd_hlen
;
1554 ready
= (kn
->kn_data
> 0);
1558 lck_mtx_unlock(bpf_mlock
);
1563 _cast_non_const(const void * ptr
) {
1574 * Copy data from an mbuf chain into a buffer. This code is derived
1575 * from m_copydata in sys/uipc_mbuf.c.
1578 bpf_mcopy(const void *src_arg
, void *dst_arg
, size_t len
)
1580 struct mbuf
*m
= _cast_non_const(src_arg
);
1588 count
= min(m
->m_len
, len
);
1589 bcopy(mbuf_data(m
), dst
, count
);
1608 * It's possible that we get here after the bpf descriptor has been
1609 * detached from the interface; in such a case we simply return.
1610 * Lock ordering is important since we can be called asynchronously
1611 * (from the IOKit) to process an inbound packet; when that happens
1612 * we would have been holding its "gateLock" and will be acquiring
1613 * "bpf_mlock" upon entering this routine. Due to that, we release
1614 * "bpf_mlock" prior to calling ifnet_set_promiscuous (which will
1615 * acquire "gateLock" in the IOKit), in order to avoid a deadlock
1616 * when a ifnet_set_promiscuous request simultaneously collides with
1617 * an inbound packet being passed into the tap callback.
1619 lck_mtx_lock(bpf_mlock
);
1620 if (ifp
->if_bpf
== NULL
) {
1621 lck_mtx_unlock(bpf_mlock
);
1625 for (bp
= ifp
->if_bpf
; bp
&& bp
->bif_ifp
== ifp
&&
1626 (dlt
!= 0 && bp
->bif_dlt
!= dlt
); bp
= bp
->bif_next
)
1628 if (bp
&& bp
->bif_ifp
== ifp
&& bp
->bif_dlist
!= NULL
) {
1630 struct m_hdr hack_hdr
;
1637 * This is gross. We mock up an mbuf that points to the
1638 * header buffer. This means we don't have to copy the
1639 * header. A number of interfaces prepended headers just
1640 * for bpf by allocating an mbuf on the stack. We want to
1641 * give developers an easy way to prepend a header for bpf.
1642 * Since a developer allocating an mbuf on the stack is bad,
1643 * we do even worse here, allocating only a header to point
1644 * to a buffer the developer supplied. This makes assumptions
1645 * that bpf_filter and catchpacket will not look at anything
1646 * in the mbuf other than the header. This was true at the
1647 * time this code was written.
1649 hack_hdr
.mh_next
= m
;
1650 hack_hdr
.mh_nextpkt
= NULL
;
1651 hack_hdr
.mh_len
= hlen
;
1652 hack_hdr
.mh_data
= hdr
;
1653 hack_hdr
.mh_type
= m
->m_type
;
1654 hack_hdr
.mh_flags
= 0;
1656 m
= (mbuf_t
)&hack_hdr
;
1659 for (m0
= m
; m0
!= 0; m0
= m0
->m_next
)
1660 pktlen
+= m0
->m_len
;
1662 for (d
= bp
->bif_dlist
; d
; d
= d
->bd_next
) {
1663 if (outbound
&& !d
->bd_seesent
)
1666 slen
= bpf_filter(d
->bd_filter
, (u_char
*)m
, pktlen
, 0);
1669 if (mac_bpfdesc_check_receive(d
, bp
->bif_ifp
) != 0)
1672 catchpacket(d
, (u_char
*)m
, pktlen
, slen
, bpf_mcopy
);
1676 lck_mtx_unlock(bpf_mlock
);
1687 bpf_tap_imp(ifp
, dlt
, m
, hdr
, hlen
, 1);
1698 bpf_tap_imp(ifp
, dlt
, m
, hdr
, hlen
, 0);
1701 /* Callback registered with Ethernet driver. */
1702 static int bpf_tap_callback(struct ifnet
*ifp
, struct mbuf
*m
)
1704 bpf_tap_imp(ifp
, 0, m
, NULL
, 0, mbuf_pkthdr_rcvif(m
) == NULL
);
1710 * Move the packet data from interface memory (pkt) into the
1711 * store buffer. Return 1 if it's time to wakeup a listener (buffer full),
1712 * otherwise 0. "copy" is the routine called to do the actual data
1713 * transfer. bcopy is passed in to copy contiguous chunks, while
1714 * bpf_mcopy is passed in to copy mbuf chains. In the latter case,
1715 * pkt is really an mbuf.
1718 catchpacket(struct bpf_d
*d
, u_char
*pkt
, u_int pktlen
, u_int snaplen
,
1719 void (*cpfn
)(const void *, void *, size_t))
1723 int hdrlen
= d
->bd_bif
->bif_hdrlen
;
1725 * Figure out how many bytes to move. If the packet is
1726 * greater or equal to the snapshot length, transfer that
1727 * much. Otherwise, transfer the whole packet (unless
1728 * we hit the buffer size limit).
1730 totlen
= hdrlen
+ min(snaplen
, pktlen
);
1731 if (totlen
> d
->bd_bufsize
)
1732 totlen
= d
->bd_bufsize
;
1735 * Round up the end of the previous packet to the next longword.
1737 curlen
= BPF_WORDALIGN(d
->bd_slen
);
1738 if (curlen
+ totlen
> d
->bd_bufsize
) {
1740 * This packet will overflow the storage buffer.
1741 * Rotate the buffers if we can, then wakeup any
1744 if (d
->bd_fbuf
== 0) {
1746 * We haven't completed the previous read yet,
1747 * so drop the packet.
1756 else if (d
->bd_immediate
)
1758 * Immediate mode is set. A packet arrived so any
1759 * reads should be woken up.
1764 * Append the bpf header.
1766 hp
= (struct bpf_hdr
*)(d
->bd_sbuf
+ curlen
);
1769 hp
->bh_tstamp
.tv_sec
= tv
.tv_sec
;
1770 hp
->bh_tstamp
.tv_usec
= tv
.tv_usec
;
1771 hp
->bh_datalen
= pktlen
;
1772 hp
->bh_hdrlen
= hdrlen
;
1774 * Copy the packet data into the store buffer and update its length.
1776 (*cpfn
)(pkt
, (u_char
*)hp
+ hdrlen
, (hp
->bh_caplen
= totlen
- hdrlen
));
1777 d
->bd_slen
= curlen
+ totlen
;
1781 * Initialize all nonzero fields of a descriptor.
1784 bpf_allocbufs(struct bpf_d
*d
)
1786 d
->bd_fbuf
= (caddr_t
) _MALLOC(d
->bd_bufsize
, M_DEVBUF
, M_WAIT
);
1787 if (d
->bd_fbuf
== 0)
1790 d
->bd_sbuf
= (caddr_t
) _MALLOC(d
->bd_bufsize
, M_DEVBUF
, M_WAIT
);
1791 if (d
->bd_sbuf
== 0) {
1792 FREE(d
->bd_fbuf
, M_DEVBUF
);
1801 * Free buffers currently in use by a descriptor.
1805 bpf_freed(struct bpf_d
*d
)
1808 * We don't need to lock out interrupts since this descriptor has
1809 * been detached from its interface and it yet hasn't been marked
1812 if (d
->bd_sbuf
!= 0) {
1813 FREE(d
->bd_sbuf
, M_DEVBUF
);
1814 if (d
->bd_hbuf
!= 0)
1815 FREE(d
->bd_hbuf
, M_DEVBUF
);
1816 if (d
->bd_fbuf
!= 0)
1817 FREE(d
->bd_fbuf
, M_DEVBUF
);
1820 FREE((caddr_t
)d
->bd_filter
, M_DEVBUF
);
1824 * Attach an interface to bpf. driverp is a pointer to a (struct bpf_if *)
1825 * in the driver's softc; dlt is the link layer type; hdrlen is the fixed
1826 * size of the link header (variable length headers not yet supported).
1829 bpfattach(struct ifnet
*ifp
, u_int dlt
, u_int hdrlen
)
1831 bpf_attach(ifp
, dlt
, hdrlen
, NULL
, NULL
);
1842 struct bpf_if
*bp_new
;
1843 struct bpf_if
*bp_temp
;
1844 struct bpf_if
*bp_first
= NULL
;
1846 bp_new
= (struct bpf_if
*) _MALLOC(sizeof(*bp_new
), M_DEVBUF
, M_WAIT
);
1850 lck_mtx_lock(bpf_mlock
);
1853 * Check if this interface/dlt is already attached, record first
1854 * attachment for this interface.
1856 for (bp_temp
= bpf_iflist
; bp_temp
&& (bp_temp
->bif_ifp
!= ifp
||
1857 bp_temp
->bif_dlt
!= dlt
); bp_temp
= bp_temp
->bif_next
) {
1858 if (bp_temp
->bif_ifp
== ifp
&& bp_first
== NULL
)
1862 if (bp_temp
!= NULL
) {
1863 printf("bpfattach - %s%d with dlt %d is already attached\n",
1864 ifp
->if_name
, ifp
->if_unit
, dlt
);
1865 FREE(bp_new
, M_DEVBUF
);
1866 lck_mtx_unlock(bpf_mlock
);
1870 bzero(bp_new
, sizeof(*bp_new
));
1871 bp_new
->bif_ifp
= ifp
;
1872 bp_new
->bif_dlt
= dlt
;
1873 bp_new
->bif_send
= send
;
1874 bp_new
->bif_tap
= tap
;
1876 if (bp_first
== NULL
) {
1877 /* No other entries for this ifp */
1878 bp_new
->bif_next
= bpf_iflist
;
1879 bpf_iflist
= bp_new
;
1882 /* Add this after the first entry for this interface */
1883 bp_new
->bif_next
= bp_first
->bif_next
;
1884 bp_first
->bif_next
= bp_new
;
1888 * Compute the length of the bpf header. This is not necessarily
1889 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1890 * that the network layer header begins on a longword boundary (for
1891 * performance reasons and to alleviate alignment restrictions).
1893 bp_new
->bif_hdrlen
= BPF_WORDALIGN(hdrlen
+ SIZEOF_BPF_HDR
) - hdrlen
;
1895 /* Take a reference on the interface */
1896 ifnet_reference(ifp
);
1898 lck_mtx_unlock(bpf_mlock
);
1902 printf("bpf: %s%d attached\n", ifp
->if_name
, ifp
->if_unit
);
1909 * Detach bpf from an interface. This involves detaching each descriptor
1910 * associated with the interface, and leaving bd_bif NULL. Notify each
1911 * descriptor as it's detached so that any sleepers wake up and get
1915 bpfdetach(struct ifnet
*ifp
)
1917 struct bpf_if
*bp
, *bp_prev
, *bp_next
;
1918 struct bpf_if
*bp_free
= NULL
;
1922 lck_mtx_lock(bpf_mlock
);
1924 /* Locate BPF interface information */
1926 for (bp
= bpf_iflist
; bp
!= NULL
; bp
= bp_next
) {
1927 bp_next
= bp
->bif_next
;
1928 if (ifp
!= bp
->bif_ifp
) {
1933 while ((d
= bp
->bif_dlist
) != NULL
) {
1939 bp_prev
->bif_next
= bp
->bif_next
;
1941 bpf_iflist
= bp
->bif_next
;
1944 bp
->bif_next
= bp_free
;
1950 lck_mtx_unlock(bpf_mlock
);
1957 bpf_init(__unused
void *unused
)
1963 if (bpf_devsw_installed
== 0) {
1964 bpf_devsw_installed
= 1;
1966 bpf_mlock_grp_attr
= lck_grp_attr_alloc_init();
1968 bpf_mlock_grp
= lck_grp_alloc_init("bpf", bpf_mlock_grp_attr
);
1970 bpf_mlock_attr
= lck_attr_alloc_init();
1972 bpf_mlock
= lck_mtx_alloc_init(bpf_mlock_grp
, bpf_mlock_attr
);
1974 if (bpf_mlock
== 0) {
1975 printf("bpf_init: failed to allocate bpf_mlock\n");
1976 bpf_devsw_installed
= 0;
1980 maj
= cdevsw_add(CDEV_MAJOR
, &bpf_cdevsw
);
1983 lck_mtx_free(bpf_mlock
, bpf_mlock_grp
);
1985 lck_attr_free(bpf_mlock_attr
);
1987 lck_grp_free(bpf_mlock_grp
);
1988 if (bpf_mlock_grp_attr
)
1989 lck_grp_attr_free(bpf_mlock_grp_attr
);
1992 bpf_mlock_attr
= NULL
;
1993 bpf_mlock_grp
= NULL
;
1994 bpf_mlock_grp_attr
= NULL
;
1995 bpf_devsw_installed
= 0;
1996 printf("bpf_init: failed to allocate a major number!\n");
2000 for (i
= 0 ; i
< NBPFILTER
; i
++)
2001 bpf_make_dev_t(maj
);
2004 cdevsw_add(&bpf_cdevsw
);
2009 SYSINIT(bpfdev
,SI_SUB_DRIVERS
,SI_ORDER_MIDDLE
+CDEV_MAJOR
,bpf_drvinit
,NULL
)
2014 mac_bpfdesc_label_get(struct bpf_d
*d
)
2017 return (d
->bd_label
);
2021 mac_bpfdesc_label_set(struct bpf_d
*d
, struct label
*label
)
2024 d
->bd_label
= label
;