2 * Copyright (c) 2000-2004 Apple Computer, 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 $
75 #define inline __inline
78 #include <sys/param.h>
79 #include <sys/systm.h>
81 #include <sys/malloc.h>
85 #include <sys/signalvar.h>
86 #include <sys/filio.h>
87 #include <sys/sockio.h>
88 #include <sys/ttycom.h>
89 #include <sys/filedesc.h>
90 #include <sys/uio_internal.h>
92 #if defined(sparc) && BSD < 199103
93 #include <sys/stream.h>
97 #include <sys/socket.h>
98 #include <sys/vnode.h>
102 #include <net/bpfdesc.h>
104 #include <netinet/in.h>
105 #include <netinet/if_ether.h>
106 #include <sys/kernel.h>
107 #include <sys/sysctl.h>
108 #include <net/firewire.h>
110 #include <machine/spl.h>
111 #include <miscfs/devfs/devfs.h>
112 #include <net/dlil.h>
114 #include <kern/locks.h>
116 extern int tvtohz(struct timeval
*);
121 * Older BSDs don't have kernel malloc.
125 static caddr_t
bpf_alloc();
126 #include <net/bpf_compat.h>
127 #define BPF_BUFSIZE (MCLBYTES-8)
128 #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, code, uio)
130 #define BPF_BUFSIZE 4096
131 #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, uio)
135 #define PRINET 26 /* interruptible */
138 * The default read buffer size is patchable.
140 static unsigned int bpf_bufsize
= BPF_BUFSIZE
;
141 SYSCTL_INT(_debug
, OID_AUTO
, bpf_bufsize
, CTLFLAG_RW
,
142 &bpf_bufsize
, 0, "");
143 static unsigned int bpf_maxbufsize
= BPF_MAXBUFSIZE
;
144 SYSCTL_INT(_debug
, OID_AUTO
, bpf_maxbufsize
, CTLFLAG_RW
,
145 &bpf_maxbufsize
, 0, "");
146 static unsigned int bpf_maxdevices
= 256;
147 SYSCTL_UINT(_debug
, OID_AUTO
, bpf_maxdevices
, CTLFLAG_RW
,
148 &bpf_maxdevices
, 0, "");
151 * bpf_iflist is the list of interfaces; each corresponds to an ifnet
152 * bpf_dtab holds pointer to the descriptors, indexed by minor device #
154 static struct bpf_if
*bpf_iflist
;
157 * BSD now stores the bpf_d in the dev_t which is a struct
158 * on their system. Our dev_t is an int, so we still store
159 * the bpf_d in a separate table indexed by minor device #.
161 * The value stored in bpf_dtab[n] represent three states:
162 * 0: device not opened
163 * 1: device opening or closing
164 * other: device <n> opened with pointer to storage
166 static struct bpf_d
**bpf_dtab
= NULL
;
167 static unsigned int bpf_dtab_size
= 0;
168 static unsigned int nbpfilter
= 0;
170 static lck_mtx_t
*bpf_mlock
;
171 static lck_grp_t
*bpf_mlock_grp
;
172 static lck_grp_attr_t
*bpf_mlock_grp_attr
;
173 static lck_attr_t
*bpf_mlock_attr
;
176 * Mark a descriptor free by making it point to itself.
177 * This is probably cheaper than marking with a constant since
178 * the address should be in a register anyway.
180 #endif /* __APPLE__ */
182 static int bpf_allocbufs(struct bpf_d
*);
183 static void bpf_attachd(struct bpf_d
*d
, struct bpf_if
*bp
);
184 static void bpf_detachd(struct bpf_d
*d
);
185 static void bpf_freed(struct bpf_d
*);
186 static void bpf_mcopy(const void *, void *, size_t);
187 static int bpf_movein(struct uio
*, int,
188 struct mbuf
**, struct sockaddr
*, int *);
189 static int bpf_setif(struct bpf_d
*, struct ifreq
*);
190 static void bpf_wakeup(struct bpf_d
*);
191 static void catchpacket(struct bpf_d
*, u_char
*, u_int
,
192 u_int
, void (*)(const void *, void *, size_t));
193 static void reset_d(struct bpf_d
*);
194 static int bpf_setf(struct bpf_d
*, struct user_bpf_program
*);
196 /*static void *bpf_devfs_token[MAXBPFILTER];*/
198 static int bpf_devsw_installed
;
200 void bpf_init(void *unused
);
201 int bpf_tap_callback(struct ifnet
*ifp
, struct mbuf
*m
);
204 * Darwin differs from BSD here, the following are static
205 * on BSD and not static on Darwin.
211 ioctl_fcn_t bpfioctl
;
212 select_fcn_t bpfpoll
;
215 /* Darwin's cdevsw struct differs slightly from BSDs */
216 #define CDEV_MAJOR 23
217 static struct cdevsw bpf_cdevsw
= {
219 /* close */ bpfclose
,
221 /* write */ bpfwrite
,
222 /* ioctl */ bpfioctl
,
224 /* reset */ eno_reset
,
226 /* select */ bpfpoll
,
228 /* strategy*/ eno_strat
,
234 #define SOCKADDR_HDR_LEN offsetof(struct sockaddr, sa_data)
237 bpf_movein(struct uio
*uio
, int linktype
, struct mbuf
**mp
, struct sockaddr
*sockp
, int *datlen
)
246 * Build a sockaddr based on the data link layer type.
247 * We do this at this level because the ethernet header
248 * is copied directly into the data field of the sockaddr.
249 * In the case of SLIP, there is no header and the packet
250 * is forwarded as is.
251 * Also, we are careful to leave room at the front of the mbuf
252 * for the link level header.
257 sockp
->sa_family
= AF_INET
;
262 sockp
->sa_family
= AF_UNSPEC
;
263 /* XXX Would MAXLINKHDR be better? */
264 hlen
= sizeof(struct ether_header
);
268 #if defined(__FreeBSD__) || defined(__bsdi__)
269 sockp
->sa_family
= AF_IMPLINK
;
272 sockp
->sa_family
= AF_UNSPEC
;
273 /* XXX 4(FORMAC)+6(dst)+6(src)+3(LLC)+5(SNAP) */
280 sockp
->sa_family
= AF_UNSPEC
;
285 case DLT_ATM_RFC1483
:
287 * en atm driver requires 4-byte atm pseudo header.
288 * though it isn't standard, vpi:vci needs to be
291 sockp
->sa_family
= AF_UNSPEC
;
292 hlen
= 12; /* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
296 sockp
->sa_family
= AF_UNSPEC
;
297 hlen
= 4; /* This should match PPP_HDRLEN */
300 case DLT_APPLE_IP_OVER_IEEE1394
:
301 sockp
->sa_family
= AF_UNSPEC
;
302 hlen
= sizeof(struct firewire_header
);
308 if ((hlen
+ SOCKADDR_HDR_LEN
) > sockp
->sa_len
) {
316 // LP64todo - fix this!
317 len
= uio_resid(uio
);
318 *datlen
= len
- hlen
;
319 if ((unsigned)len
> MCLBYTES
)
322 MGETHDR(m
, M_WAIT
, MT_DATA
);
325 if ((unsigned)len
> MHLEN
) {
328 if ((m
->m_flags
& M_EXT
) == 0) {
331 if (m
->m_len
!= MCLBYTES
) {
337 m
->m_pkthdr
.len
= m
->m_len
= len
;
338 m
->m_pkthdr
.rcvif
= NULL
;
341 * Make room for link header.
344 m
->m_pkthdr
.len
-= hlen
;
347 m
->m_data
+= hlen
; /* XXX */
351 error
= UIOMOVE((caddr_t
)sockp
->sa_data
, hlen
, UIO_WRITE
, uio
);
355 error
= UIOMOVE(mtod(m
, caddr_t
), len
- hlen
, UIO_WRITE
, uio
);
364 /* Callback registered with Ethernet driver. */
365 int bpf_tap_callback(struct ifnet
*ifp
, struct mbuf
*m
)
368 * Do nothing if the BPF tap has been turned off.
369 * This is to protect from a potential race where this
370 * call blocks on the lock. And in the meantime
371 * BPF is turned off, which will clear if_bpf.
379 * The dynamic addition of a new device node must block all processes that are opening
380 * the last device so that no process will get an unexpected ENOENT
383 bpf_make_dev_t(int maj
)
385 static int bpf_growing
= 0;
386 unsigned int cur_size
= nbpfilter
, i
;
388 if (nbpfilter
>= bpf_maxdevices
)
391 while (bpf_growing
) {
392 /* Wait until new device has been created */
393 (void)tsleep((caddr_t
)&bpf_growing
, PZERO
, "bpf_growing", 0);
395 if (nbpfilter
> cur_size
) {
396 /* other thread grew it already */
401 /* need to grow bpf_dtab first */
402 if (nbpfilter
== bpf_dtab_size
) {
404 struct bpf_d
**new_dtab
= NULL
;
405 struct bpf_d
**old_dtab
= NULL
;
407 new_dtab_size
= bpf_dtab_size
+ NBPFILTER
;
408 new_dtab
= (struct bpf_d
**)_MALLOC(sizeof(struct bpf_d
*) * new_dtab_size
, M_DEVBUF
, M_WAIT
);
410 printf("bpf_make_dev_t: malloc bpf_dtab failed\n");
414 bcopy(bpf_dtab
, new_dtab
,
415 sizeof(struct bpf_d
*) * bpf_dtab_size
);
417 bzero(new_dtab
+ bpf_dtab_size
,
418 sizeof(struct bpf_d
*) * NBPFILTER
);
421 bpf_dtab_size
= new_dtab_size
;
422 if (old_dtab
!= NULL
)
423 _FREE(old_dtab
, M_DEVBUF
);
426 (void) devfs_make_node(makedev(maj
, i
),
427 DEVFS_CHAR
, UID_ROOT
, GID_WHEEL
, 0600,
431 wakeup((caddr_t
)&bpf_growing
);
437 * Attach file to the bpf interface, i.e. make d listen on bp.
438 * Must be called at splimp.
441 bpf_attachd(struct bpf_d
*d
, struct bpf_if
*bp
)
444 * Point d at bp, and add d to the interface's list of listeners.
445 * Finally, point the driver's bpf cookie at the interface so
446 * it will divert packets to bpf.
449 d
->bd_next
= bp
->bif_dlist
;
452 bp
->bif_ifp
->if_bpf
= bp
;
455 dlil_set_bpf_tap(bp
->bif_ifp
, BPF_TAP_INPUT_OUTPUT
, bpf_tap_callback
);
460 * Detach a file from its interface.
463 bpf_detachd(struct bpf_d
*d
)
470 ifp
= d
->bd_bif
->bif_ifp
;
476 * Check if this descriptor had requested promiscuous mode.
477 * If so, turn it off.
481 if (ifnet_set_promiscuous(bp
->bif_ifp
, 0))
483 * Something is really wrong if we were able to put
484 * the driver into promiscuous mode, but can't
486 * Most likely the network interface is gone.
488 printf("bpf: ifnet_set_promiscuous failed");
490 /* Remove d from the interface's descriptor list. */
495 panic("bpf_detachd: descriptor not in list");
498 if (bp
->bif_dlist
== 0) {
500 * Let the driver know that there are no more listeners.
502 if (ifp
->if_set_bpf_tap
)
503 (*ifp
->if_set_bpf_tap
)(ifp
, BPF_TAP_DISABLE
, 0);
504 d
->bd_bif
->bif_ifp
->if_bpf
= 0;
511 * Open ethernet device. Returns ENXIO for illegal minor device number,
512 * EBUSY if file is open by another process.
516 bpfopen(dev_t dev
, __unused
int flags
, __unused
int fmt
, __unused
struct proc
*p
)
518 register struct bpf_d
*d
;
520 if ((unsigned int) minor(dev
) >= nbpfilter
)
524 * New device nodes are created on demand when opening the last one.
525 * The programming model is for processes to loop on the minor starting at 0
526 * as long as EBUSY is returned. The loop stops when either the open succeeds or
527 * an error other that EBUSY is returned. That means that bpf_make_dev_t() must
528 * block all processes that are opening the last node. If not all
529 * processes are blocked, they could unexpectedly get ENOENT and abort their
532 if ((unsigned int) minor(dev
) == (nbpfilter
- 1))
533 bpf_make_dev_t(major(dev
));
536 * Each minor can be opened by only one process. If the requested
537 * minor is in use, return EBUSY.
539 * Important: bpfopen() and bpfclose() have to check and set the status of a device
540 * in the same lockin context otherwise the device may be leaked because the vnode use count
541 * will be unpextectly greater than 1 when close() is called.
543 if (bpf_dtab
[minor(dev
)] == 0)
544 bpf_dtab
[minor(dev
)] = (void *)1; /* Mark opening */
548 d
= (struct bpf_d
*)_MALLOC(sizeof(struct bpf_d
), M_DEVBUF
, M_WAIT
);
550 /* this really is a catastrophic failure */
551 printf("bpfopen: malloc bpf_d failed\n");
552 bpf_dtab
[minor(dev
)] = 0;
555 bzero(d
, sizeof(struct bpf_d
));
558 * It is not necessary to take the BPF lock here because no other
559 * thread can access the device until it is marked opened...
562 /* Mark "in use" and do most initialization. */
563 d
->bd_bufsize
= bpf_bufsize
;
566 bpf_dtab
[minor(dev
)] = d
; /* Mark opened */
572 * Close the descriptor by detaching it from its interface,
573 * deallocating its buffers, and marking it free.
577 bpfclose(dev_t dev
, __unused
int flags
, __unused
int fmt
, __unused
struct proc
*p
)
579 register struct bpf_d
*d
;
581 d
= bpf_dtab
[minor(dev
)];
582 if (d
== 0 || d
== (void *)1)
585 bpf_dtab
[minor(dev
)] = (void *)1; /* Mark closing */
587 /* Take BPF lock to ensure no other thread is using the device */
588 lck_mtx_lock(bpf_mlock
);
592 selthreadclear(&d
->bd_sel
);
595 lck_mtx_unlock(bpf_mlock
);
597 /* Mark free in same context as bpfopen comes to check */
598 bpf_dtab
[minor(dev
)] = 0; /* Mark closed */
605 #define BPF_SLEEP bpf_sleep
608 bpf_sleep(struct bpf_d
*d
, int pri
, const char *wmesg
, int timo
)
612 lck_mtx_unlock(bpf_mlock
);
614 st
= tsleep((caddr_t
)d
, pri
, wmesg
, timo
);
616 lck_mtx_lock(bpf_mlock
);
622 * Rotate the packet buffers in descriptor d. Move the store buffer
623 * into the hold slot, and the free buffer into the store slot.
624 * Zero the length of the new store buffer.
626 #define ROTATE_BUFFERS(d) \
627 (d)->bd_hbuf = (d)->bd_sbuf; \
628 (d)->bd_hlen = (d)->bd_slen; \
629 (d)->bd_sbuf = (d)->bd_fbuf; \
633 * bpfread - read next chunk of packets from buffers
636 bpfread(dev_t dev
, struct uio
*uio
, int ioflag
)
638 register struct bpf_d
*d
;
642 d
= bpf_dtab
[minor(dev
)];
643 if (d
== 0 || d
== (void *)1)
646 lck_mtx_lock(bpf_mlock
);
650 * Restrict application to use a buffer the same size as
653 // LP64todo - fix this
654 if (uio
->uio_resid
!= d
->bd_bufsize
) {
655 lck_mtx_unlock(bpf_mlock
);
661 * If the hold buffer is empty, then do a timed sleep, which
662 * ends when the timeout expires or when enough packets
663 * have arrived to fill the store buffer.
665 while (d
->bd_hbuf
== 0) {
666 if (d
->bd_immediate
&& d
->bd_slen
!= 0) {
668 * A packet(s) either arrived since the previous
669 * read or arrived while we were asleep.
670 * Rotate the buffers and return what's here.
677 * No data is available, check to see if the bpf device
678 * is still pointed at a real interface. If not, return
679 * ENXIO so that the userland process knows to rebind
680 * it before using it again.
682 if (d
->bd_bif
== NULL
) {
684 lck_mtx_unlock(bpf_mlock
);
688 if (ioflag
& IO_NDELAY
)
691 error
= BPF_SLEEP(d
, PRINET
|PCATCH
, "bpf",
693 if (error
== EINTR
|| error
== ERESTART
) {
695 lck_mtx_unlock(bpf_mlock
);
698 if (error
== EWOULDBLOCK
) {
700 * On a timeout, return what's in the buffer,
701 * which may be nothing. If there is something
702 * in the store buffer, we can rotate the buffers.
706 * We filled up the buffer in between
707 * getting the timeout and arriving
708 * here, so we don't need to rotate.
712 if (d
->bd_slen
== 0) {
714 lck_mtx_unlock(bpf_mlock
);
722 * At this point, we know we have something in the hold slot.
727 * Move data from hold buffer into user space.
728 * We know the entire buffer is transferred since
729 * we checked above that the read buffer is bpf_bufsize bytes.
731 error
= UIOMOVE(d
->bd_hbuf
, d
->bd_hlen
, UIO_READ
, uio
);
734 d
->bd_fbuf
= d
->bd_hbuf
;
738 lck_mtx_unlock(bpf_mlock
);
744 * If there are processes sleeping on this descriptor, wake them up.
747 bpf_wakeup(struct bpf_d
*d
)
750 if (d
->bd_async
&& d
->bd_sig
&& d
->bd_sigio
)
751 pgsigio(d
->bd_sigio
, d
->bd_sig
, 0);
754 selwakeup(&d
->bd_sel
);
757 d
->bd_sel
.si_pid
= 0;
761 selwakeup(d
->bd_selproc
, (int)d
->bd_selcoll
);
768 /* keep in sync with bpf_movein above: */
769 #define MAX_DATALINK_HDR_LEN (sizeof(struct firewire_header))
772 bpfwrite(dev_t dev
, struct uio
*uio
, __unused
int ioflag
)
774 register struct bpf_d
*d
;
778 char dst_buf
[SOCKADDR_HDR_LEN
+ MAX_DATALINK_HDR_LEN
];
781 d
= bpf_dtab
[minor(dev
)];
782 if (d
== 0 || d
== (void *)1)
785 lck_mtx_lock(bpf_mlock
);
787 if (d
->bd_bif
== 0) {
788 lck_mtx_unlock(bpf_mlock
);
792 ifp
= d
->bd_bif
->bif_ifp
;
794 if (uio
->uio_resid
== 0) {
795 lck_mtx_unlock(bpf_mlock
);
798 ((struct sockaddr
*)dst_buf
)->sa_len
= sizeof(dst_buf
);
799 error
= bpf_movein(uio
, (int)d
->bd_bif
->bif_dlt
, &m
,
800 d
->bd_hdrcmplt
? 0 : (struct sockaddr
*)dst_buf
, &datlen
);
802 lck_mtx_unlock(bpf_mlock
);
806 if ((unsigned)datlen
> ifp
->if_mtu
) {
807 lck_mtx_unlock(bpf_mlock
);
811 lck_mtx_unlock(bpf_mlock
);
813 if (d
->bd_hdrcmplt
) {
814 error
= dlil_output(ifp
, 0, m
, NULL
, NULL
, 1);
817 error
= dlil_output(ifp
, PF_INET
, m
, NULL
, (struct sockaddr
*)dst_buf
, 0);
821 * The driver frees the mbuf.
827 * Reset a descriptor by flushing its packet buffer and clearing the
828 * receive and drop counts. Should be called at splimp.
831 reset_d(struct bpf_d
*d
)
834 /* Free the hold buffer. */
835 d
->bd_fbuf
= d
->bd_hbuf
;
845 * FIONREAD Check for read packet available.
846 * SIOCGIFADDR Get interface address - convenient hook to driver.
847 * BIOCGBLEN Get buffer len [for read()].
848 * BIOCSETF Set ethernet read filter.
849 * BIOCFLUSH Flush read packet buffer.
850 * BIOCPROMISC Put interface into promiscuous mode.
851 * BIOCGDLT Get link layer type.
852 * BIOCGETIF Get interface name.
853 * BIOCSETIF Set interface.
854 * BIOCSRTIMEOUT Set read timeout.
855 * BIOCGRTIMEOUT Get read timeout.
856 * BIOCGSTATS Get packet stats.
857 * BIOCIMMEDIATE Set immediate mode.
858 * BIOCVERSION Get filter language version.
859 * BIOCGHDRCMPLT Get "header already complete" flag
860 * BIOCSHDRCMPLT Set "header already complete" flag
861 * BIOCGSEESENT Get "see packets sent" flag
862 * BIOCSSEESENT Set "see packets sent" flag
866 bpfioctl(dev_t dev
, u_long cmd
, caddr_t addr
, __unused
int flags
, struct proc
*p
)
868 register struct bpf_d
*d
;
871 d
= bpf_dtab
[minor(dev
)];
872 if (d
== 0 || d
== (void *)1)
875 lck_mtx_lock(bpf_mlock
);
884 * Check for read packet available.
907 ifp
= d
->bd_bif
->bif_ifp
;
908 error
= dlil_ioctl(0, ifp
, cmd
, addr
);
914 * Get buffer len [for read()].
917 *(u_int
*)addr
= d
->bd_bufsize
;
930 register u_int size
= *(u_int
*)addr
;
932 if (size
> bpf_maxbufsize
)
933 *(u_int
*)addr
= size
= bpf_maxbufsize
;
934 else if (size
< BPF_MINBUFSIZE
)
935 *(u_int
*)addr
= size
= BPF_MINBUFSIZE
;
936 d
->bd_bufsize
= size
;
942 * Set link layer read filter.
945 if (proc_is64bit(p
)) {
946 error
= bpf_setf(d
, (struct user_bpf_program
*)addr
);
949 struct bpf_program
* tmpp
;
950 struct user_bpf_program tmp
;
952 tmpp
= (struct bpf_program
*)addr
;
953 tmp
.bf_len
= tmpp
->bf_len
;
954 tmp
.bf_insns
= CAST_USER_ADDR_T(tmpp
->bf_insns
);
955 error
= bpf_setf(d
, &tmp
);
960 * Flush read packet buffer.
969 * Put interface into promiscuous mode.
972 if (d
->bd_bif
== 0) {
974 * No interface attached yet.
980 if (d
->bd_promisc
== 0) {
981 error
= ifnet_set_promiscuous(d
->bd_bif
->bif_ifp
, 1);
989 * Get device parameters.
995 *(u_int
*)addr
= d
->bd_bif
->bif_dlt
;
999 * Get interface name.
1005 struct ifnet
*const ifp
= d
->bd_bif
->bif_ifp
;
1006 struct ifreq
*const ifr
= (struct ifreq
*)addr
;
1008 snprintf(ifr
->ifr_name
, sizeof(ifr
->ifr_name
),
1009 "%s%d", ifp
->if_name
, ifp
->if_unit
);
1017 error
= bpf_setif(d
, (struct ifreq
*)addr
);
1025 struct timeval
*tv
= (struct timeval
*)addr
;
1028 * Subtract 1 tick from tvtohz() since this isn't
1031 if ((error
= itimerfix(tv
)) == 0)
1032 d
->bd_rtout
= tvtohz(tv
) - 1;
1041 struct timeval
*tv
= (struct timeval
*)addr
;
1043 tv
->tv_sec
= d
->bd_rtout
/ hz
;
1044 tv
->tv_usec
= (d
->bd_rtout
% hz
) * tick
;
1053 struct bpf_stat
*bs
= (struct bpf_stat
*)addr
;
1055 bs
->bs_recv
= d
->bd_rcount
;
1056 bs
->bs_drop
= d
->bd_dcount
;
1061 * Set immediate mode.
1064 d
->bd_immediate
= *(u_int
*)addr
;
1069 struct bpf_version
*bv
= (struct bpf_version
*)addr
;
1071 bv
->bv_major
= BPF_MAJOR_VERSION
;
1072 bv
->bv_minor
= BPF_MINOR_VERSION
;
1077 * Get "header already complete" flag
1080 *(u_int
*)addr
= d
->bd_hdrcmplt
;
1084 * Set "header already complete" flag
1087 d
->bd_hdrcmplt
= *(u_int
*)addr
? 1 : 0;
1091 * Get "see sent packets" flag
1094 *(u_int
*)addr
= d
->bd_seesent
;
1098 * Set "see sent packets" flag
1101 d
->bd_seesent
= *(u_int
*)addr
;
1104 case FIONBIO
: /* Non-blocking I/O */
1107 case FIOASYNC
: /* Send signal on receive packets */
1108 d
->bd_async
= *(int *)addr
;
1112 error
= fsetown(*(int *)addr
, &d
->bd_sigio
);
1116 *(int *)addr
= fgetown(d
->bd_sigio
);
1119 /* This is deprecated, FIOSETOWN should be used instead. */
1121 error
= fsetown(-(*(int *)addr
), &d
->bd_sigio
);
1124 /* This is deprecated, FIOGETOWN should be used instead. */
1126 *(int *)addr
= -fgetown(d
->bd_sigio
);
1129 case BIOCSRSIG
: /* Set receive signal */
1133 sig
= *(u_int
*)addr
;
1142 *(u_int
*)addr
= d
->bd_sig
;
1146 lck_mtx_unlock(bpf_mlock
);
1152 * Set d's packet filter program to fp. If this file already has a filter,
1153 * free it and replace it. Returns EINVAL for bogus requests.
1156 bpf_setf(struct bpf_d
*d
, struct user_bpf_program
*fp
)
1158 struct bpf_insn
*fcode
, *old
;
1163 if (fp
->bf_insns
== USER_ADDR_NULL
) {
1164 if (fp
->bf_len
!= 0)
1171 FREE((caddr_t
)old
, M_DEVBUF
);
1175 if (flen
> BPF_MAXINSNS
)
1178 size
= flen
* sizeof(struct bpf_insn
);
1179 fcode
= (struct bpf_insn
*) _MALLOC(size
, M_DEVBUF
, M_WAIT
);
1184 if (copyin(fp
->bf_insns
, (caddr_t
)fcode
, size
) == 0 &&
1185 bpf_validate(fcode
, (int)flen
)) {
1187 d
->bd_filter
= fcode
;
1191 FREE((caddr_t
)old
, M_DEVBUF
);
1195 FREE((caddr_t
)fcode
, M_DEVBUF
);
1200 * Detach a file from its current interface (if attached at all) and attach
1201 * to the interface indicated by the name stored in ifr.
1202 * Return an errno or 0.
1205 bpf_setif(struct bpf_d
*d
, struct ifreq
*ifr
)
1209 struct ifnet
*theywant
;
1211 theywant
= ifunit(ifr
->ifr_name
);
1216 * Look through attached interfaces for the named one.
1218 for (bp
= bpf_iflist
; bp
!= 0; bp
= bp
->bif_next
) {
1219 struct ifnet
*ifp
= bp
->bif_ifp
;
1221 if (ifp
== 0 || ifp
!= theywant
)
1224 * We found the requested interface.
1225 * If it's not up, return an error.
1226 * Allocate the packet buffers if we need to.
1227 * If we're already attached to requested interface,
1228 * just flush the buffer.
1230 if ((ifp
->if_flags
& IFF_UP
) == 0)
1233 if (d
->bd_sbuf
== 0) {
1234 error
= bpf_allocbufs(d
);
1239 if (bp
!= d
->bd_bif
) {
1242 * Detach if attached to something else.
1257 * Support for select() and poll() system calls
1259 * Return true iff the specific operation will not block indefinitely.
1260 * Otherwise, return false but make a note that a selwakeup() must be done.
1263 bpfpoll(dev_t dev
, int events
, void * wql
, struct proc
*p
)
1265 register struct bpf_d
*d
;
1269 d
= bpf_dtab
[minor(dev
)];
1270 if (d
== 0 || d
== (void *)1)
1273 lck_mtx_lock(bpf_mlock
);
1276 * An imitation of the FIONREAD ioctl code.
1278 if (d
->bd_bif
== NULL
) {
1279 lck_mtx_unlock(bpf_mlock
);
1284 if (events
& (POLLIN
| POLLRDNORM
)) {
1285 if (d
->bd_hlen
!= 0 || (d
->bd_immediate
&& d
->bd_slen
!= 0))
1286 revents
|= events
& (POLLIN
| POLLRDNORM
);
1288 selrecord(p
, &d
->bd_sel
, wql
);
1292 lck_mtx_unlock(bpf_mlock
);
1297 * Incoming linkage from device drivers. Process the packet pkt, of length
1298 * pktlen, which is stored in a contiguous buffer. The packet is parsed
1299 * by each process' filter, and if accepted, stashed into the corresponding
1303 bpf_tap(struct ifnet
*ifp
, u_char
*pkt
, u_int pktlen
)
1306 register struct bpf_d
*d
;
1307 register u_int slen
;
1309 * Note that the ipl does not have to be raised at this point.
1310 * The only problem that could arise here is that if two different
1311 * interfaces shared any data. This is not the case.
1313 lck_mtx_lock(bpf_mlock
);
1319 for (d
= bp
->bif_dlist
; d
!= 0; d
= d
->bd_next
) {
1321 slen
= bpf_filter(d
->bd_filter
, pkt
, pktlen
, pktlen
);
1323 catchpacket(d
, pkt
, pktlen
, slen
, bcopy
);
1327 lck_mtx_unlock(bpf_mlock
);
1332 * Copy data from an mbuf chain into a buffer. This code is derived
1333 * from m_copydata in sys/uipc_mbuf.c.
1336 bpf_mcopy(const void *src_arg
, void *dst_arg
, size_t len
)
1338 const struct mbuf
*m
;
1347 count
= min(m
->m_len
, len
);
1348 bcopy(mtod(m
, const void *), dst
, count
);
1356 * Incoming linkage from device drivers, when packet is in an mbuf chain.
1359 bpf_mtap(struct ifnet
*ifp
, struct mbuf
*m
)
1366 lck_mtx_lock(bpf_mlock
);
1371 for (m0
= m
; m0
!= 0; m0
= m0
->m_next
)
1372 pktlen
+= m0
->m_len
;
1374 for (d
= bp
->bif_dlist
; d
!= 0; d
= d
->bd_next
) {
1375 if (!d
->bd_seesent
&& (m
->m_pkthdr
.rcvif
== NULL
))
1378 slen
= bpf_filter(d
->bd_filter
, (u_char
*)m
, pktlen
, 0);
1380 catchpacket(d
, (u_char
*)m
, pktlen
, slen
, bpf_mcopy
);
1384 lck_mtx_unlock(bpf_mlock
);
1388 * Move the packet data from interface memory (pkt) into the
1389 * store buffer. Return 1 if it's time to wakeup a listener (buffer full),
1390 * otherwise 0. "copy" is the routine called to do the actual data
1391 * transfer. bcopy is passed in to copy contiguous chunks, while
1392 * bpf_mcopy is passed in to copy mbuf chains. In the latter case,
1393 * pkt is really an mbuf.
1396 catchpacket(struct bpf_d
*d
, u_char
*pkt
, u_int pktlen
, u_int snaplen
,
1397 void (*cpfn
)(const void *, void *, size_t))
1399 register struct bpf_hdr
*hp
;
1400 register int totlen
, curlen
;
1401 register int hdrlen
= d
->bd_bif
->bif_hdrlen
;
1403 * Figure out how many bytes to move. If the packet is
1404 * greater or equal to the snapshot length, transfer that
1405 * much. Otherwise, transfer the whole packet (unless
1406 * we hit the buffer size limit).
1408 totlen
= hdrlen
+ min(snaplen
, pktlen
);
1409 if (totlen
> d
->bd_bufsize
)
1410 totlen
= d
->bd_bufsize
;
1413 * Round up the end of the previous packet to the next longword.
1415 curlen
= BPF_WORDALIGN(d
->bd_slen
);
1416 if (curlen
+ totlen
> d
->bd_bufsize
) {
1418 * This packet will overflow the storage buffer.
1419 * Rotate the buffers if we can, then wakeup any
1422 if (d
->bd_fbuf
== 0) {
1424 * We haven't completed the previous read yet,
1425 * so drop the packet.
1434 else if (d
->bd_immediate
)
1436 * Immediate mode is set. A packet arrived so any
1437 * reads should be woken up.
1442 * Append the bpf header.
1444 hp
= (struct bpf_hdr
*)(d
->bd_sbuf
+ curlen
);
1446 microtime(&hp
->bh_tstamp
);
1448 uniqtime(&hp
->bh_tstamp
);
1450 hp
->bh_tstamp
= time
;
1452 hp
->bh_datalen
= pktlen
;
1453 hp
->bh_hdrlen
= hdrlen
;
1455 * Copy the packet data into the store buffer and update its length.
1457 (*cpfn
)(pkt
, (u_char
*)hp
+ hdrlen
, (hp
->bh_caplen
= totlen
- hdrlen
));
1458 d
->bd_slen
= curlen
+ totlen
;
1462 * Initialize all nonzero fields of a descriptor.
1465 bpf_allocbufs(struct bpf_d
*d
)
1467 d
->bd_fbuf
= (caddr_t
) _MALLOC(d
->bd_bufsize
, M_DEVBUF
, M_WAIT
);
1468 if (d
->bd_fbuf
== 0)
1471 d
->bd_sbuf
= (caddr_t
) _MALLOC(d
->bd_bufsize
, M_DEVBUF
, M_WAIT
);
1472 if (d
->bd_sbuf
== 0) {
1473 FREE(d
->bd_fbuf
, M_DEVBUF
);
1482 * Free buffers currently in use by a descriptor.
1486 bpf_freed(struct bpf_d
*d
)
1489 * We don't need to lock out interrupts since this descriptor has
1490 * been detached from its interface and it yet hasn't been marked
1493 if (d
->bd_sbuf
!= 0) {
1494 FREE(d
->bd_sbuf
, M_DEVBUF
);
1495 if (d
->bd_hbuf
!= 0)
1496 FREE(d
->bd_hbuf
, M_DEVBUF
);
1497 if (d
->bd_fbuf
!= 0)
1498 FREE(d
->bd_fbuf
, M_DEVBUF
);
1501 FREE((caddr_t
)d
->bd_filter
, M_DEVBUF
);
1505 * Attach an interface to bpf. driverp is a pointer to a (struct bpf_if *)
1506 * in the driver's softc; dlt is the link layer type; hdrlen is the fixed
1507 * size of the link header (variable length headers not yet supported).
1510 bpfattach(struct ifnet
*ifp
, u_int dlt
, u_int hdrlen
)
1513 bp
= (struct bpf_if
*) _MALLOC(sizeof(*bp
), M_DEVBUF
, M_WAIT
);
1517 lck_mtx_lock(bpf_mlock
);
1523 bp
->bif_next
= bpf_iflist
;
1526 bp
->bif_ifp
->if_bpf
= 0;
1529 * Compute the length of the bpf header. This is not necessarily
1530 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1531 * that the network layer header begins on a longword boundary (for
1532 * performance reasons and to alleviate alignment restrictions).
1534 bp
->bif_hdrlen
= BPF_WORDALIGN(hdrlen
+ SIZEOF_BPF_HDR
) - hdrlen
;
1536 /* Take a reference on the interface */
1539 lck_mtx_unlock(bpf_mlock
);
1543 printf("bpf: %s%d attached\n", ifp
->if_name
, ifp
->if_unit
);
1548 * Detach bpf from an interface. This involves detaching each descriptor
1549 * associated with the interface, and leaving bd_bif NULL. Notify each
1550 * descriptor as it's detached so that any sleepers wake up and get
1554 bpfdetach(struct ifnet
*ifp
)
1556 struct bpf_if
*bp
, *bp_prev
;
1562 lck_mtx_lock(bpf_mlock
);
1564 /* Locate BPF interface information */
1566 for (bp
= bpf_iflist
; bp
!= NULL
; bp
= bp
->bif_next
) {
1567 if (ifp
== bp
->bif_ifp
)
1573 /* Check for no BPF interface information */
1579 /* Interface wasn't attached */
1580 if (bp
->bif_ifp
== NULL
) {
1583 printf("bpfdetach: %s%d was not attached\n", ifp
->if_name
,
1589 while ((d
= bp
->bif_dlist
) != NULL
) {
1595 bp_prev
->bif_next
= bp
->bif_next
;
1597 bpf_iflist
= bp
->bif_next
;
1602 lck_mtx_unlock(bpf_mlock
);
1610 bpf_init(__unused
void *unused
)
1616 if (bpf_devsw_installed
== 0) {
1617 bpf_devsw_installed
= 1;
1619 bpf_mlock_grp_attr
= lck_grp_attr_alloc_init();
1620 lck_grp_attr_setdefault(bpf_mlock_grp_attr
);
1622 bpf_mlock_grp
= lck_grp_alloc_init("bpf", bpf_mlock_grp_attr
);
1624 bpf_mlock_attr
= lck_attr_alloc_init();
1625 lck_attr_setdefault(bpf_mlock_attr
);
1627 bpf_mlock
= lck_mtx_alloc_init(bpf_mlock_grp
, bpf_mlock_attr
);
1629 if (bpf_mlock
== 0) {
1630 printf("bpf_init: failed to allocate bpf_mlock\n");
1631 bpf_devsw_installed
= 0;
1635 maj
= cdevsw_add(CDEV_MAJOR
, &bpf_cdevsw
);
1638 lck_mtx_free(bpf_mlock
, bpf_mlock_grp
);
1640 lck_attr_free(bpf_mlock_attr
);
1642 lck_grp_free(bpf_mlock_grp
);
1643 if (bpf_mlock_grp_attr
)
1644 lck_grp_attr_free(bpf_mlock_grp_attr
);
1649 bpf_mlock_grp_attr
= 0;
1650 bpf_devsw_installed
= 0;
1651 printf("bpf_init: failed to allocate a major number!\n");
1655 for (i
= 0 ; i
< NBPFILTER
; i
++)
1656 bpf_make_dev_t(maj
);
1659 cdevsw_add(&bpf_cdevsw
);
1664 SYSINIT(bpfdev
,SI_SUB_DRIVERS
,SI_ORDER_MIDDLE
+CDEV_MAJOR
,bpf_drvinit
,NULL
)
1670 * NOP stubs to allow bpf-using drivers to load and function.
1672 * A 'better' implementation would allow the core bpf functionality
1673 * to be loaded at runtime.
1677 bpf_tap(ifp
, pkt
, pktlen
)
1679 register u_char
*pkt
;
1680 register u_int pktlen
;
1692 bpfattach(ifp
, dlt
, hdrlen
)
1705 bpf_filter(pc
, p
, wirelen
, buflen
)
1706 register const struct bpf_insn
*pc
;
1709 register u_int buflen
;
1711 return -1; /* "no filter" behaviour */
1713 #endif /* !defined(__APPLE__) */
1714 #endif /* NBPFILTER > 0 */