2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * Copyright (c) 1990, 1991, 1993
32 * The Regents of the University of California. All rights reserved.
34 * This code is derived from the Stanford/CMU enet packet filter,
35 * (net/enet.c) distributed as part of 4.3BSD, and code contributed
36 * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
37 * Berkeley Laboratory.
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. All advertising materials mentioning features or use of this software
48 * must display the following acknowledgement:
49 * This product includes software developed by the University of
50 * California, Berkeley and its contributors.
51 * 4. Neither the name of the University nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 * @(#)bpf.c 8.2 (Berkeley) 3/28/94
69 * $FreeBSD: src/sys/net/bpf.c,v 1.59.2.5 2001/01/05 04:49:09 jdp Exp $
77 #define inline __inline
80 #include <sys/param.h>
81 #include <sys/systm.h>
83 #include <sys/malloc.h>
87 #include <sys/signalvar.h>
88 #include <sys/filio.h>
89 #include <sys/sockio.h>
90 #include <sys/ttycom.h>
91 #include <sys/filedesc.h>
92 #include <sys/uio_internal.h>
94 #if defined(sparc) && BSD < 199103
95 #include <sys/stream.h>
99 #include <sys/socket.h>
100 #include <sys/vnode.h>
104 #include <net/bpfdesc.h>
106 #include <netinet/in.h>
107 #include <netinet/if_ether.h>
108 #include <sys/kernel.h>
109 #include <sys/sysctl.h>
110 #include <net/firewire.h>
112 #include <machine/spl.h>
113 #include <miscfs/devfs/devfs.h>
114 #include <net/dlil.h>
116 #include <kern/locks.h>
118 extern int tvtohz(struct timeval
*);
123 * Older BSDs don't have kernel malloc.
127 static caddr_t
bpf_alloc();
128 #include <net/bpf_compat.h>
129 #define BPF_BUFSIZE (MCLBYTES-8)
130 #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, code, uio)
132 #define BPF_BUFSIZE 4096
133 #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, uio)
137 #define PRINET 26 /* interruptible */
140 * The default read buffer size is patchable.
142 static unsigned int bpf_bufsize
= BPF_BUFSIZE
;
143 SYSCTL_INT(_debug
, OID_AUTO
, bpf_bufsize
, CTLFLAG_RW
,
144 &bpf_bufsize
, 0, "");
145 static unsigned int bpf_maxbufsize
= BPF_MAXBUFSIZE
;
146 SYSCTL_INT(_debug
, OID_AUTO
, bpf_maxbufsize
, CTLFLAG_RW
,
147 &bpf_maxbufsize
, 0, "");
148 static unsigned int bpf_maxdevices
= 256;
149 SYSCTL_UINT(_debug
, OID_AUTO
, bpf_maxdevices
, CTLFLAG_RW
,
150 &bpf_maxdevices
, 0, "");
153 * bpf_iflist is the list of interfaces; each corresponds to an ifnet
154 * bpf_dtab holds pointer to the descriptors, indexed by minor device #
156 static struct bpf_if
*bpf_iflist
;
159 * BSD now stores the bpf_d in the dev_t which is a struct
160 * on their system. Our dev_t is an int, so we still store
161 * the bpf_d in a separate table indexed by minor device #.
163 * The value stored in bpf_dtab[n] represent three states:
164 * 0: device not opened
165 * 1: device opening or closing
166 * other: device <n> opened with pointer to storage
168 static struct bpf_d
**bpf_dtab
= NULL
;
169 static unsigned int bpf_dtab_size
= 0;
170 static unsigned int nbpfilter
= 0;
172 static lck_mtx_t
*bpf_mlock
;
173 static lck_grp_t
*bpf_mlock_grp
;
174 static lck_grp_attr_t
*bpf_mlock_grp_attr
;
175 static lck_attr_t
*bpf_mlock_attr
;
178 * Mark a descriptor free by making it point to itself.
179 * This is probably cheaper than marking with a constant since
180 * the address should be in a register anyway.
182 #endif /* __APPLE__ */
184 static int bpf_allocbufs(struct bpf_d
*);
185 static void bpf_attachd(struct bpf_d
*d
, struct bpf_if
*bp
);
186 static void bpf_detachd(struct bpf_d
*d
);
187 static void bpf_freed(struct bpf_d
*);
188 static void bpf_mcopy(const void *, void *, size_t);
189 static int bpf_movein(struct uio
*, int,
190 struct mbuf
**, struct sockaddr
*, int *);
191 static int bpf_setif(struct bpf_d
*, struct ifreq
*);
192 static void bpf_wakeup(struct bpf_d
*);
193 static void catchpacket(struct bpf_d
*, u_char
*, u_int
,
194 u_int
, void (*)(const void *, void *, size_t));
195 static void reset_d(struct bpf_d
*);
196 static int bpf_setf(struct bpf_d
*, struct user_bpf_program
*);
198 /*static void *bpf_devfs_token[MAXBPFILTER];*/
200 static int bpf_devsw_installed
;
202 void bpf_init(void *unused
);
203 int bpf_tap_callback(struct ifnet
*ifp
, struct mbuf
*m
);
206 * Darwin differs from BSD here, the following are static
207 * on BSD and not static on Darwin.
213 ioctl_fcn_t bpfioctl
;
214 select_fcn_t bpfpoll
;
217 /* Darwin's cdevsw struct differs slightly from BSDs */
218 #define CDEV_MAJOR 23
219 static struct cdevsw bpf_cdevsw
= {
221 /* close */ bpfclose
,
223 /* write */ bpfwrite
,
224 /* ioctl */ bpfioctl
,
226 /* reset */ eno_reset
,
228 /* select */ bpfpoll
,
230 /* strategy*/ eno_strat
,
236 #define SOCKADDR_HDR_LEN offsetof(struct sockaddr, sa_data)
239 bpf_movein(struct uio
*uio
, int linktype
, struct mbuf
**mp
, struct sockaddr
*sockp
, int *datlen
)
248 * Build a sockaddr based on the data link layer type.
249 * We do this at this level because the ethernet header
250 * is copied directly into the data field of the sockaddr.
251 * In the case of SLIP, there is no header and the packet
252 * is forwarded as is.
253 * Also, we are careful to leave room at the front of the mbuf
254 * for the link level header.
259 sockp
->sa_family
= AF_INET
;
264 sockp
->sa_family
= AF_UNSPEC
;
265 /* XXX Would MAXLINKHDR be better? */
266 hlen
= sizeof(struct ether_header
);
270 #if defined(__FreeBSD__) || defined(__bsdi__)
271 sockp
->sa_family
= AF_IMPLINK
;
274 sockp
->sa_family
= AF_UNSPEC
;
275 /* XXX 4(FORMAC)+6(dst)+6(src)+3(LLC)+5(SNAP) */
282 sockp
->sa_family
= AF_UNSPEC
;
287 case DLT_ATM_RFC1483
:
289 * en atm driver requires 4-byte atm pseudo header.
290 * though it isn't standard, vpi:vci needs to be
293 sockp
->sa_family
= AF_UNSPEC
;
294 hlen
= 12; /* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
298 sockp
->sa_family
= AF_UNSPEC
;
299 hlen
= 4; /* This should match PPP_HDRLEN */
302 case DLT_APPLE_IP_OVER_IEEE1394
:
303 sockp
->sa_family
= AF_UNSPEC
;
304 hlen
= sizeof(struct firewire_header
);
310 if ((hlen
+ SOCKADDR_HDR_LEN
) > sockp
->sa_len
) {
318 // LP64todo - fix this!
319 len
= uio_resid(uio
);
320 *datlen
= len
- hlen
;
321 if ((unsigned)len
> MCLBYTES
)
324 MGETHDR(m
, M_WAIT
, MT_DATA
);
327 if ((unsigned)len
> MHLEN
) {
330 if ((m
->m_flags
& M_EXT
) == 0) {
333 if (m
->m_len
!= MCLBYTES
) {
339 m
->m_pkthdr
.len
= m
->m_len
= len
;
340 m
->m_pkthdr
.rcvif
= NULL
;
343 * Make room for link header.
346 m
->m_pkthdr
.len
-= hlen
;
349 m
->m_data
+= hlen
; /* XXX */
353 error
= UIOMOVE((caddr_t
)sockp
->sa_data
, hlen
, UIO_WRITE
, uio
);
357 error
= UIOMOVE(mtod(m
, caddr_t
), len
- hlen
, UIO_WRITE
, uio
);
366 /* Callback registered with Ethernet driver. */
367 int bpf_tap_callback(struct ifnet
*ifp
, struct mbuf
*m
)
370 * Do nothing if the BPF tap has been turned off.
371 * This is to protect from a potential race where this
372 * call blocks on the lock. And in the meantime
373 * BPF is turned off, which will clear if_bpf.
381 * The dynamic addition of a new device node must block all processes that are opening
382 * the last device so that no process will get an unexpected ENOENT
385 bpf_make_dev_t(int maj
)
387 static int bpf_growing
= 0;
388 unsigned int cur_size
= nbpfilter
, i
;
390 if (nbpfilter
>= bpf_maxdevices
)
393 while (bpf_growing
) {
394 /* Wait until new device has been created */
395 (void)tsleep((caddr_t
)&bpf_growing
, PZERO
, "bpf_growing", 0);
397 if (nbpfilter
> cur_size
) {
398 /* other thread grew it already */
403 /* need to grow bpf_dtab first */
404 if (nbpfilter
== bpf_dtab_size
) {
406 struct bpf_d
**new_dtab
= NULL
;
407 struct bpf_d
**old_dtab
= NULL
;
409 new_dtab_size
= bpf_dtab_size
+ NBPFILTER
;
410 new_dtab
= (struct bpf_d
**)_MALLOC(sizeof(struct bpf_d
*) * new_dtab_size
, M_DEVBUF
, M_WAIT
);
412 printf("bpf_make_dev_t: malloc bpf_dtab failed\n");
416 bcopy(bpf_dtab
, new_dtab
,
417 sizeof(struct bpf_d
*) * bpf_dtab_size
);
419 bzero(new_dtab
+ bpf_dtab_size
,
420 sizeof(struct bpf_d
*) * NBPFILTER
);
423 bpf_dtab_size
= new_dtab_size
;
424 if (old_dtab
!= NULL
)
425 _FREE(old_dtab
, M_DEVBUF
);
428 (void) devfs_make_node(makedev(maj
, i
),
429 DEVFS_CHAR
, UID_ROOT
, GID_WHEEL
, 0600,
433 wakeup((caddr_t
)&bpf_growing
);
439 * Attach file to the bpf interface, i.e. make d listen on bp.
440 * Must be called at splimp.
443 bpf_attachd(struct bpf_d
*d
, struct bpf_if
*bp
)
446 * Point d at bp, and add d to the interface's list of listeners.
447 * Finally, point the driver's bpf cookie at the interface so
448 * it will divert packets to bpf.
451 d
->bd_next
= bp
->bif_dlist
;
454 bp
->bif_ifp
->if_bpf
= bp
;
457 dlil_set_bpf_tap(bp
->bif_ifp
, BPF_TAP_INPUT_OUTPUT
, bpf_tap_callback
);
462 * Detach a file from its interface.
465 bpf_detachd(struct bpf_d
*d
)
472 ifp
= d
->bd_bif
->bif_ifp
;
478 * Check if this descriptor had requested promiscuous mode.
479 * If so, turn it off.
483 if (ifnet_set_promiscuous(bp
->bif_ifp
, 0))
485 * Something is really wrong if we were able to put
486 * the driver into promiscuous mode, but can't
488 * Most likely the network interface is gone.
490 printf("bpf: ifnet_set_promiscuous failed");
492 /* Remove d from the interface's descriptor list. */
497 panic("bpf_detachd: descriptor not in list");
500 if (bp
->bif_dlist
== 0) {
502 * Let the driver know that there are no more listeners.
504 if (ifp
->if_set_bpf_tap
)
505 (*ifp
->if_set_bpf_tap
)(ifp
, BPF_TAP_DISABLE
, 0);
506 d
->bd_bif
->bif_ifp
->if_bpf
= 0;
513 * Open ethernet device. Returns ENXIO for illegal minor device number,
514 * EBUSY if file is open by another process.
518 bpfopen(dev_t dev
, __unused
int flags
, __unused
int fmt
, __unused
struct proc
*p
)
520 register struct bpf_d
*d
;
522 if ((unsigned int) minor(dev
) >= nbpfilter
)
526 * New device nodes are created on demand when opening the last one.
527 * The programming model is for processes to loop on the minor starting at 0
528 * as long as EBUSY is returned. The loop stops when either the open succeeds or
529 * an error other that EBUSY is returned. That means that bpf_make_dev_t() must
530 * block all processes that are opening the last node. If not all
531 * processes are blocked, they could unexpectedly get ENOENT and abort their
534 if ((unsigned int) minor(dev
) == (nbpfilter
- 1))
535 bpf_make_dev_t(major(dev
));
538 * Each minor can be opened by only one process. If the requested
539 * minor is in use, return EBUSY.
541 * Important: bpfopen() and bpfclose() have to check and set the status of a device
542 * in the same lockin context otherwise the device may be leaked because the vnode use count
543 * will be unpextectly greater than 1 when close() is called.
545 if (bpf_dtab
[minor(dev
)] == 0)
546 bpf_dtab
[minor(dev
)] = (void *)1; /* Mark opening */
550 d
= (struct bpf_d
*)_MALLOC(sizeof(struct bpf_d
), M_DEVBUF
, M_WAIT
);
552 /* this really is a catastrophic failure */
553 printf("bpfopen: malloc bpf_d failed\n");
554 bpf_dtab
[minor(dev
)] = 0;
557 bzero(d
, sizeof(struct bpf_d
));
560 * It is not necessary to take the BPF lock here because no other
561 * thread can access the device until it is marked opened...
564 /* Mark "in use" and do most initialization. */
565 d
->bd_bufsize
= bpf_bufsize
;
568 bpf_dtab
[minor(dev
)] = d
; /* Mark opened */
574 * Close the descriptor by detaching it from its interface,
575 * deallocating its buffers, and marking it free.
579 bpfclose(dev_t dev
, __unused
int flags
, __unused
int fmt
, __unused
struct proc
*p
)
581 register struct bpf_d
*d
;
583 d
= bpf_dtab
[minor(dev
)];
584 if (d
== 0 || d
== (void *)1)
587 bpf_dtab
[minor(dev
)] = (void *)1; /* Mark closing */
589 /* Take BPF lock to ensure no other thread is using the device */
590 lck_mtx_lock(bpf_mlock
);
594 selthreadclear(&d
->bd_sel
);
597 lck_mtx_unlock(bpf_mlock
);
599 /* Mark free in same context as bpfopen comes to check */
600 bpf_dtab
[minor(dev
)] = 0; /* Mark closed */
607 #define BPF_SLEEP bpf_sleep
610 bpf_sleep(struct bpf_d
*d
, int pri
, const char *wmesg
, int timo
)
614 lck_mtx_unlock(bpf_mlock
);
616 st
= tsleep((caddr_t
)d
, pri
, wmesg
, timo
);
618 lck_mtx_lock(bpf_mlock
);
624 * Rotate the packet buffers in descriptor d. Move the store buffer
625 * into the hold slot, and the free buffer into the store slot.
626 * Zero the length of the new store buffer.
628 #define ROTATE_BUFFERS(d) \
629 (d)->bd_hbuf = (d)->bd_sbuf; \
630 (d)->bd_hlen = (d)->bd_slen; \
631 (d)->bd_sbuf = (d)->bd_fbuf; \
635 * bpfread - read next chunk of packets from buffers
638 bpfread(dev_t dev
, struct uio
*uio
, int ioflag
)
640 register struct bpf_d
*d
;
644 d
= bpf_dtab
[minor(dev
)];
645 if (d
== 0 || d
== (void *)1)
648 lck_mtx_lock(bpf_mlock
);
652 * Restrict application to use a buffer the same size as
655 // LP64todo - fix this
656 if (uio
->uio_resid
!= d
->bd_bufsize
) {
657 lck_mtx_unlock(bpf_mlock
);
663 * If the hold buffer is empty, then do a timed sleep, which
664 * ends when the timeout expires or when enough packets
665 * have arrived to fill the store buffer.
667 while (d
->bd_hbuf
== 0) {
668 if (d
->bd_immediate
&& d
->bd_slen
!= 0) {
670 * A packet(s) either arrived since the previous
671 * read or arrived while we were asleep.
672 * Rotate the buffers and return what's here.
679 * No data is available, check to see if the bpf device
680 * is still pointed at a real interface. If not, return
681 * ENXIO so that the userland process knows to rebind
682 * it before using it again.
684 if (d
->bd_bif
== NULL
) {
686 lck_mtx_unlock(bpf_mlock
);
690 if (ioflag
& IO_NDELAY
)
693 error
= BPF_SLEEP(d
, PRINET
|PCATCH
, "bpf",
695 if (error
== EINTR
|| error
== ERESTART
) {
697 lck_mtx_unlock(bpf_mlock
);
700 if (error
== EWOULDBLOCK
) {
702 * On a timeout, return what's in the buffer,
703 * which may be nothing. If there is something
704 * in the store buffer, we can rotate the buffers.
708 * We filled up the buffer in between
709 * getting the timeout and arriving
710 * here, so we don't need to rotate.
714 if (d
->bd_slen
== 0) {
716 lck_mtx_unlock(bpf_mlock
);
724 * At this point, we know we have something in the hold slot.
729 * Move data from hold buffer into user space.
730 * We know the entire buffer is transferred since
731 * we checked above that the read buffer is bpf_bufsize bytes.
733 error
= UIOMOVE(d
->bd_hbuf
, d
->bd_hlen
, UIO_READ
, uio
);
736 d
->bd_fbuf
= d
->bd_hbuf
;
740 lck_mtx_unlock(bpf_mlock
);
746 * If there are processes sleeping on this descriptor, wake them up.
749 bpf_wakeup(struct bpf_d
*d
)
752 if (d
->bd_async
&& d
->bd_sig
&& d
->bd_sigio
)
753 pgsigio(d
->bd_sigio
, d
->bd_sig
, 0);
756 selwakeup(&d
->bd_sel
);
759 d
->bd_sel
.si_pid
= 0;
763 selwakeup(d
->bd_selproc
, (int)d
->bd_selcoll
);
770 /* keep in sync with bpf_movein above: */
771 #define MAX_DATALINK_HDR_LEN (sizeof(struct firewire_header))
774 bpfwrite(dev_t dev
, struct uio
*uio
, __unused
int ioflag
)
776 register struct bpf_d
*d
;
780 char dst_buf
[SOCKADDR_HDR_LEN
+ MAX_DATALINK_HDR_LEN
];
783 d
= bpf_dtab
[minor(dev
)];
784 if (d
== 0 || d
== (void *)1)
787 lck_mtx_lock(bpf_mlock
);
789 if (d
->bd_bif
== 0) {
790 lck_mtx_unlock(bpf_mlock
);
794 ifp
= d
->bd_bif
->bif_ifp
;
796 if (uio
->uio_resid
== 0) {
797 lck_mtx_unlock(bpf_mlock
);
800 ((struct sockaddr
*)dst_buf
)->sa_len
= sizeof(dst_buf
);
801 error
= bpf_movein(uio
, (int)d
->bd_bif
->bif_dlt
, &m
,
802 d
->bd_hdrcmplt
? 0 : (struct sockaddr
*)dst_buf
, &datlen
);
804 lck_mtx_unlock(bpf_mlock
);
808 if ((unsigned)datlen
> ifp
->if_mtu
) {
809 lck_mtx_unlock(bpf_mlock
);
813 lck_mtx_unlock(bpf_mlock
);
815 if (d
->bd_hdrcmplt
) {
816 error
= dlil_output(ifp
, 0, m
, NULL
, NULL
, 1);
819 error
= dlil_output(ifp
, PF_INET
, m
, NULL
, (struct sockaddr
*)dst_buf
, 0);
823 * The driver frees the mbuf.
829 * Reset a descriptor by flushing its packet buffer and clearing the
830 * receive and drop counts. Should be called at splimp.
833 reset_d(struct bpf_d
*d
)
836 /* Free the hold buffer. */
837 d
->bd_fbuf
= d
->bd_hbuf
;
847 * FIONREAD Check for read packet available.
848 * SIOCGIFADDR Get interface address - convenient hook to driver.
849 * BIOCGBLEN Get buffer len [for read()].
850 * BIOCSETF Set ethernet read filter.
851 * BIOCFLUSH Flush read packet buffer.
852 * BIOCPROMISC Put interface into promiscuous mode.
853 * BIOCGDLT Get link layer type.
854 * BIOCGETIF Get interface name.
855 * BIOCSETIF Set interface.
856 * BIOCSRTIMEOUT Set read timeout.
857 * BIOCGRTIMEOUT Get read timeout.
858 * BIOCGSTATS Get packet stats.
859 * BIOCIMMEDIATE Set immediate mode.
860 * BIOCVERSION Get filter language version.
861 * BIOCGHDRCMPLT Get "header already complete" flag
862 * BIOCSHDRCMPLT Set "header already complete" flag
863 * BIOCGSEESENT Get "see packets sent" flag
864 * BIOCSSEESENT Set "see packets sent" flag
868 bpfioctl(dev_t dev
, u_long cmd
, caddr_t addr
, __unused
int flags
, struct proc
*p
)
870 register struct bpf_d
*d
;
873 d
= bpf_dtab
[minor(dev
)];
874 if (d
== 0 || d
== (void *)1)
877 lck_mtx_lock(bpf_mlock
);
886 * Check for read packet available.
909 ifp
= d
->bd_bif
->bif_ifp
;
910 error
= dlil_ioctl(0, ifp
, cmd
, addr
);
916 * Get buffer len [for read()].
919 *(u_int
*)addr
= d
->bd_bufsize
;
932 register u_int size
= *(u_int
*)addr
;
934 if (size
> bpf_maxbufsize
)
935 *(u_int
*)addr
= size
= bpf_maxbufsize
;
936 else if (size
< BPF_MINBUFSIZE
)
937 *(u_int
*)addr
= size
= BPF_MINBUFSIZE
;
938 d
->bd_bufsize
= size
;
944 * Set link layer read filter.
947 if (proc_is64bit(p
)) {
948 error
= bpf_setf(d
, (struct user_bpf_program
*)addr
);
951 struct bpf_program
* tmpp
;
952 struct user_bpf_program tmp
;
954 tmpp
= (struct bpf_program
*)addr
;
955 tmp
.bf_len
= tmpp
->bf_len
;
956 tmp
.bf_insns
= CAST_USER_ADDR_T(tmpp
->bf_insns
);
957 error
= bpf_setf(d
, &tmp
);
962 * Flush read packet buffer.
971 * Put interface into promiscuous mode.
974 if (d
->bd_bif
== 0) {
976 * No interface attached yet.
982 if (d
->bd_promisc
== 0) {
983 error
= ifnet_set_promiscuous(d
->bd_bif
->bif_ifp
, 1);
991 * Get device parameters.
997 *(u_int
*)addr
= d
->bd_bif
->bif_dlt
;
1001 * Get interface name.
1007 struct ifnet
*const ifp
= d
->bd_bif
->bif_ifp
;
1008 struct ifreq
*const ifr
= (struct ifreq
*)addr
;
1010 snprintf(ifr
->ifr_name
, sizeof(ifr
->ifr_name
),
1011 "%s%d", ifp
->if_name
, ifp
->if_unit
);
1019 error
= bpf_setif(d
, (struct ifreq
*)addr
);
1027 struct timeval
*tv
= (struct timeval
*)addr
;
1030 * Subtract 1 tick from tvtohz() since this isn't
1033 if ((error
= itimerfix(tv
)) == 0)
1034 d
->bd_rtout
= tvtohz(tv
) - 1;
1043 struct timeval
*tv
= (struct timeval
*)addr
;
1045 tv
->tv_sec
= d
->bd_rtout
/ hz
;
1046 tv
->tv_usec
= (d
->bd_rtout
% hz
) * tick
;
1055 struct bpf_stat
*bs
= (struct bpf_stat
*)addr
;
1057 bs
->bs_recv
= d
->bd_rcount
;
1058 bs
->bs_drop
= d
->bd_dcount
;
1063 * Set immediate mode.
1066 d
->bd_immediate
= *(u_int
*)addr
;
1071 struct bpf_version
*bv
= (struct bpf_version
*)addr
;
1073 bv
->bv_major
= BPF_MAJOR_VERSION
;
1074 bv
->bv_minor
= BPF_MINOR_VERSION
;
1079 * Get "header already complete" flag
1082 *(u_int
*)addr
= d
->bd_hdrcmplt
;
1086 * Set "header already complete" flag
1089 d
->bd_hdrcmplt
= *(u_int
*)addr
? 1 : 0;
1093 * Get "see sent packets" flag
1096 *(u_int
*)addr
= d
->bd_seesent
;
1100 * Set "see sent packets" flag
1103 d
->bd_seesent
= *(u_int
*)addr
;
1106 case FIONBIO
: /* Non-blocking I/O */
1109 case FIOASYNC
: /* Send signal on receive packets */
1110 d
->bd_async
= *(int *)addr
;
1114 error
= fsetown(*(int *)addr
, &d
->bd_sigio
);
1118 *(int *)addr
= fgetown(d
->bd_sigio
);
1121 /* This is deprecated, FIOSETOWN should be used instead. */
1123 error
= fsetown(-(*(int *)addr
), &d
->bd_sigio
);
1126 /* This is deprecated, FIOGETOWN should be used instead. */
1128 *(int *)addr
= -fgetown(d
->bd_sigio
);
1131 case BIOCSRSIG
: /* Set receive signal */
1135 sig
= *(u_int
*)addr
;
1144 *(u_int
*)addr
= d
->bd_sig
;
1148 lck_mtx_unlock(bpf_mlock
);
1154 * Set d's packet filter program to fp. If this file already has a filter,
1155 * free it and replace it. Returns EINVAL for bogus requests.
1158 bpf_setf(struct bpf_d
*d
, struct user_bpf_program
*fp
)
1160 struct bpf_insn
*fcode
, *old
;
1165 if (fp
->bf_insns
== USER_ADDR_NULL
) {
1166 if (fp
->bf_len
!= 0)
1173 FREE((caddr_t
)old
, M_DEVBUF
);
1177 if (flen
> BPF_MAXINSNS
)
1180 size
= flen
* sizeof(struct bpf_insn
);
1181 fcode
= (struct bpf_insn
*) _MALLOC(size
, M_DEVBUF
, M_WAIT
);
1186 if (copyin(fp
->bf_insns
, (caddr_t
)fcode
, size
) == 0 &&
1187 bpf_validate(fcode
, (int)flen
)) {
1189 d
->bd_filter
= fcode
;
1193 FREE((caddr_t
)old
, M_DEVBUF
);
1197 FREE((caddr_t
)fcode
, M_DEVBUF
);
1202 * Detach a file from its current interface (if attached at all) and attach
1203 * to the interface indicated by the name stored in ifr.
1204 * Return an errno or 0.
1207 bpf_setif(struct bpf_d
*d
, struct ifreq
*ifr
)
1211 struct ifnet
*theywant
;
1213 theywant
= ifunit(ifr
->ifr_name
);
1218 * Look through attached interfaces for the named one.
1220 for (bp
= bpf_iflist
; bp
!= 0; bp
= bp
->bif_next
) {
1221 struct ifnet
*ifp
= bp
->bif_ifp
;
1223 if (ifp
== 0 || ifp
!= theywant
)
1226 * We found the requested interface.
1227 * If it's not up, return an error.
1228 * Allocate the packet buffers if we need to.
1229 * If we're already attached to requested interface,
1230 * just flush the buffer.
1232 if ((ifp
->if_flags
& IFF_UP
) == 0)
1235 if (d
->bd_sbuf
== 0) {
1236 error
= bpf_allocbufs(d
);
1241 if (bp
!= d
->bd_bif
) {
1244 * Detach if attached to something else.
1259 * Support for select() and poll() system calls
1261 * Return true iff the specific operation will not block indefinitely.
1262 * Otherwise, return false but make a note that a selwakeup() must be done.
1265 bpfpoll(dev_t dev
, int events
, void * wql
, struct proc
*p
)
1267 register struct bpf_d
*d
;
1271 d
= bpf_dtab
[minor(dev
)];
1272 if (d
== 0 || d
== (void *)1)
1275 lck_mtx_lock(bpf_mlock
);
1278 * An imitation of the FIONREAD ioctl code.
1280 if (d
->bd_bif
== NULL
) {
1281 lck_mtx_unlock(bpf_mlock
);
1286 if (events
& (POLLIN
| POLLRDNORM
)) {
1287 if (d
->bd_hlen
!= 0 || (d
->bd_immediate
&& d
->bd_slen
!= 0))
1288 revents
|= events
& (POLLIN
| POLLRDNORM
);
1290 selrecord(p
, &d
->bd_sel
, wql
);
1294 lck_mtx_unlock(bpf_mlock
);
1299 * Incoming linkage from device drivers. Process the packet pkt, of length
1300 * pktlen, which is stored in a contiguous buffer. The packet is parsed
1301 * by each process' filter, and if accepted, stashed into the corresponding
1305 bpf_tap(struct ifnet
*ifp
, u_char
*pkt
, u_int pktlen
)
1308 register struct bpf_d
*d
;
1309 register u_int slen
;
1311 * Note that the ipl does not have to be raised at this point.
1312 * The only problem that could arise here is that if two different
1313 * interfaces shared any data. This is not the case.
1315 lck_mtx_lock(bpf_mlock
);
1321 for (d
= bp
->bif_dlist
; d
!= 0; d
= d
->bd_next
) {
1323 slen
= bpf_filter(d
->bd_filter
, pkt
, pktlen
, pktlen
);
1325 catchpacket(d
, pkt
, pktlen
, slen
, bcopy
);
1329 lck_mtx_unlock(bpf_mlock
);
1334 * Copy data from an mbuf chain into a buffer. This code is derived
1335 * from m_copydata in sys/uipc_mbuf.c.
1338 bpf_mcopy(const void *src_arg
, void *dst_arg
, size_t len
)
1340 const struct mbuf
*m
;
1349 count
= min(m
->m_len
, len
);
1350 bcopy(mtod(m
, const void *), dst
, count
);
1358 * Incoming linkage from device drivers, when packet is in an mbuf chain.
1361 bpf_mtap(struct ifnet
*ifp
, struct mbuf
*m
)
1368 lck_mtx_lock(bpf_mlock
);
1373 for (m0
= m
; m0
!= 0; m0
= m0
->m_next
)
1374 pktlen
+= m0
->m_len
;
1376 for (d
= bp
->bif_dlist
; d
!= 0; d
= d
->bd_next
) {
1377 if (!d
->bd_seesent
&& (m
->m_pkthdr
.rcvif
== NULL
))
1380 slen
= bpf_filter(d
->bd_filter
, (u_char
*)m
, pktlen
, 0);
1382 catchpacket(d
, (u_char
*)m
, pktlen
, slen
, bpf_mcopy
);
1386 lck_mtx_unlock(bpf_mlock
);
1390 * Move the packet data from interface memory (pkt) into the
1391 * store buffer. Return 1 if it's time to wakeup a listener (buffer full),
1392 * otherwise 0. "copy" is the routine called to do the actual data
1393 * transfer. bcopy is passed in to copy contiguous chunks, while
1394 * bpf_mcopy is passed in to copy mbuf chains. In the latter case,
1395 * pkt is really an mbuf.
1398 catchpacket(struct bpf_d
*d
, u_char
*pkt
, u_int pktlen
, u_int snaplen
,
1399 void (*cpfn
)(const void *, void *, size_t))
1401 register struct bpf_hdr
*hp
;
1402 register int totlen
, curlen
;
1403 register int hdrlen
= d
->bd_bif
->bif_hdrlen
;
1405 * Figure out how many bytes to move. If the packet is
1406 * greater or equal to the snapshot length, transfer that
1407 * much. Otherwise, transfer the whole packet (unless
1408 * we hit the buffer size limit).
1410 totlen
= hdrlen
+ min(snaplen
, pktlen
);
1411 if (totlen
> d
->bd_bufsize
)
1412 totlen
= d
->bd_bufsize
;
1415 * Round up the end of the previous packet to the next longword.
1417 curlen
= BPF_WORDALIGN(d
->bd_slen
);
1418 if (curlen
+ totlen
> d
->bd_bufsize
) {
1420 * This packet will overflow the storage buffer.
1421 * Rotate the buffers if we can, then wakeup any
1424 if (d
->bd_fbuf
== 0) {
1426 * We haven't completed the previous read yet,
1427 * so drop the packet.
1436 else if (d
->bd_immediate
)
1438 * Immediate mode is set. A packet arrived so any
1439 * reads should be woken up.
1444 * Append the bpf header.
1446 hp
= (struct bpf_hdr
*)(d
->bd_sbuf
+ curlen
);
1448 microtime(&hp
->bh_tstamp
);
1450 uniqtime(&hp
->bh_tstamp
);
1452 hp
->bh_tstamp
= time
;
1454 hp
->bh_datalen
= pktlen
;
1455 hp
->bh_hdrlen
= hdrlen
;
1457 * Copy the packet data into the store buffer and update its length.
1459 (*cpfn
)(pkt
, (u_char
*)hp
+ hdrlen
, (hp
->bh_caplen
= totlen
- hdrlen
));
1460 d
->bd_slen
= curlen
+ totlen
;
1464 * Initialize all nonzero fields of a descriptor.
1467 bpf_allocbufs(struct bpf_d
*d
)
1469 d
->bd_fbuf
= (caddr_t
) _MALLOC(d
->bd_bufsize
, M_DEVBUF
, M_WAIT
);
1470 if (d
->bd_fbuf
== 0)
1473 d
->bd_sbuf
= (caddr_t
) _MALLOC(d
->bd_bufsize
, M_DEVBUF
, M_WAIT
);
1474 if (d
->bd_sbuf
== 0) {
1475 FREE(d
->bd_fbuf
, M_DEVBUF
);
1484 * Free buffers currently in use by a descriptor.
1488 bpf_freed(struct bpf_d
*d
)
1491 * We don't need to lock out interrupts since this descriptor has
1492 * been detached from its interface and it yet hasn't been marked
1495 if (d
->bd_sbuf
!= 0) {
1496 FREE(d
->bd_sbuf
, M_DEVBUF
);
1497 if (d
->bd_hbuf
!= 0)
1498 FREE(d
->bd_hbuf
, M_DEVBUF
);
1499 if (d
->bd_fbuf
!= 0)
1500 FREE(d
->bd_fbuf
, M_DEVBUF
);
1503 FREE((caddr_t
)d
->bd_filter
, M_DEVBUF
);
1507 * Attach an interface to bpf. driverp is a pointer to a (struct bpf_if *)
1508 * in the driver's softc; dlt is the link layer type; hdrlen is the fixed
1509 * size of the link header (variable length headers not yet supported).
1512 bpfattach(struct ifnet
*ifp
, u_int dlt
, u_int hdrlen
)
1515 bp
= (struct bpf_if
*) _MALLOC(sizeof(*bp
), M_DEVBUF
, M_WAIT
);
1519 lck_mtx_lock(bpf_mlock
);
1525 bp
->bif_next
= bpf_iflist
;
1528 bp
->bif_ifp
->if_bpf
= 0;
1531 * Compute the length of the bpf header. This is not necessarily
1532 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1533 * that the network layer header begins on a longword boundary (for
1534 * performance reasons and to alleviate alignment restrictions).
1536 bp
->bif_hdrlen
= BPF_WORDALIGN(hdrlen
+ SIZEOF_BPF_HDR
) - hdrlen
;
1538 /* Take a reference on the interface */
1541 lck_mtx_unlock(bpf_mlock
);
1545 printf("bpf: %s%d attached\n", ifp
->if_name
, ifp
->if_unit
);
1550 * Detach bpf from an interface. This involves detaching each descriptor
1551 * associated with the interface, and leaving bd_bif NULL. Notify each
1552 * descriptor as it's detached so that any sleepers wake up and get
1556 bpfdetach(struct ifnet
*ifp
)
1558 struct bpf_if
*bp
, *bp_prev
;
1564 lck_mtx_lock(bpf_mlock
);
1566 /* Locate BPF interface information */
1568 for (bp
= bpf_iflist
; bp
!= NULL
; bp
= bp
->bif_next
) {
1569 if (ifp
== bp
->bif_ifp
)
1575 /* Check for no BPF interface information */
1581 /* Interface wasn't attached */
1582 if (bp
->bif_ifp
== NULL
) {
1585 printf("bpfdetach: %s%d was not attached\n", ifp
->if_name
,
1591 while ((d
= bp
->bif_dlist
) != NULL
) {
1597 bp_prev
->bif_next
= bp
->bif_next
;
1599 bpf_iflist
= bp
->bif_next
;
1604 lck_mtx_unlock(bpf_mlock
);
1612 bpf_init(__unused
void *unused
)
1618 if (bpf_devsw_installed
== 0) {
1619 bpf_devsw_installed
= 1;
1621 bpf_mlock_grp_attr
= lck_grp_attr_alloc_init();
1622 lck_grp_attr_setdefault(bpf_mlock_grp_attr
);
1624 bpf_mlock_grp
= lck_grp_alloc_init("bpf", bpf_mlock_grp_attr
);
1626 bpf_mlock_attr
= lck_attr_alloc_init();
1627 lck_attr_setdefault(bpf_mlock_attr
);
1629 bpf_mlock
= lck_mtx_alloc_init(bpf_mlock_grp
, bpf_mlock_attr
);
1631 if (bpf_mlock
== 0) {
1632 printf("bpf_init: failed to allocate bpf_mlock\n");
1633 bpf_devsw_installed
= 0;
1637 maj
= cdevsw_add(CDEV_MAJOR
, &bpf_cdevsw
);
1640 lck_mtx_free(bpf_mlock
, bpf_mlock_grp
);
1642 lck_attr_free(bpf_mlock_attr
);
1644 lck_grp_free(bpf_mlock_grp
);
1645 if (bpf_mlock_grp_attr
)
1646 lck_grp_attr_free(bpf_mlock_grp_attr
);
1651 bpf_mlock_grp_attr
= 0;
1652 bpf_devsw_installed
= 0;
1653 printf("bpf_init: failed to allocate a major number!\n");
1657 for (i
= 0 ; i
< NBPFILTER
; i
++)
1658 bpf_make_dev_t(maj
);
1661 cdevsw_add(&bpf_cdevsw
);
1666 SYSINIT(bpfdev
,SI_SUB_DRIVERS
,SI_ORDER_MIDDLE
+CDEV_MAJOR
,bpf_drvinit
,NULL
)
1672 * NOP stubs to allow bpf-using drivers to load and function.
1674 * A 'better' implementation would allow the core bpf functionality
1675 * to be loaded at runtime.
1679 bpf_tap(ifp
, pkt
, pktlen
)
1681 register u_char
*pkt
;
1682 register u_int pktlen
;
1694 bpfattach(ifp
, dlt
, hdrlen
)
1707 bpf_filter(pc
, p
, wirelen
, buflen
)
1708 register const struct bpf_insn
*pc
;
1711 register u_int buflen
;
1713 return -1; /* "no filter" behaviour */
1715 #endif /* !defined(__APPLE__) */
1716 #endif /* NBPFILTER > 0 */