2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1990, 1991, 1993
25 * The Regents of the University of California. All rights reserved.
27 * This code is derived from the Stanford/CMU enet packet filter,
28 * (net/enet.c) distributed as part of 4.3BSD, and code contributed
29 * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
30 * Berkeley Laboratory.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)bpf.c 8.2 (Berkeley) 3/28/94
62 * $FreeBSD: src/sys/net/bpf.c,v 1.59.2.5 2001/01/05 04:49:09 jdp Exp $
70 #define inline __inline
73 #include <sys/param.h>
74 #include <sys/systm.h>
76 #include <sys/malloc.h>
80 #include <sys/signalvar.h>
81 #include <sys/filio.h>
82 #include <sys/sockio.h>
83 #include <sys/ttycom.h>
84 #include <sys/filedesc.h>
85 #include <sys/uio_internal.h>
87 #if defined(sparc) && BSD < 199103
88 #include <sys/stream.h>
92 #include <sys/socket.h>
93 #include <sys/vnode.h>
97 #include <net/bpfdesc.h>
99 #include <netinet/in.h>
100 #include <netinet/if_ether.h>
101 #include <sys/kernel.h>
102 #include <sys/sysctl.h>
103 #include <net/firewire.h>
105 #include <machine/spl.h>
106 #include <miscfs/devfs/devfs.h>
107 #include <net/dlil.h>
109 #include <kern/locks.h>
111 extern int tvtohz(struct timeval
*);
116 * Older BSDs don't have kernel malloc.
120 static caddr_t
bpf_alloc();
121 #include <net/bpf_compat.h>
122 #define BPF_BUFSIZE (MCLBYTES-8)
123 #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, code, uio)
125 #define BPF_BUFSIZE 4096
126 #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, uio)
130 #define PRINET 26 /* interruptible */
133 * The default read buffer size is patchable.
135 static unsigned int bpf_bufsize
= BPF_BUFSIZE
;
136 SYSCTL_INT(_debug
, OID_AUTO
, bpf_bufsize
, CTLFLAG_RW
,
137 &bpf_bufsize
, 0, "");
138 static unsigned int bpf_maxbufsize
= BPF_MAXBUFSIZE
;
139 SYSCTL_INT(_debug
, OID_AUTO
, bpf_maxbufsize
, CTLFLAG_RW
,
140 &bpf_maxbufsize
, 0, "");
141 static unsigned int bpf_maxdevices
= 256;
142 SYSCTL_UINT(_debug
, OID_AUTO
, bpf_maxdevices
, CTLFLAG_RW
,
143 &bpf_maxdevices
, 0, "");
146 * bpf_iflist is the list of interfaces; each corresponds to an ifnet
147 * bpf_dtab holds pointer to the descriptors, indexed by minor device #
149 static struct bpf_if
*bpf_iflist
;
152 * BSD now stores the bpf_d in the dev_t which is a struct
153 * on their system. Our dev_t is an int, so we still store
154 * the bpf_d in a separate table indexed by minor device #.
156 * The value stored in bpf_dtab[n] represent three states:
157 * 0: device not opened
158 * 1: device opening or closing
159 * other: device <n> opened with pointer to storage
161 static struct bpf_d
**bpf_dtab
= NULL
;
162 static unsigned int bpf_dtab_size
= 0;
163 static unsigned int nbpfilter
= 0;
165 static lck_mtx_t
*bpf_mlock
;
166 static lck_grp_t
*bpf_mlock_grp
;
167 static lck_grp_attr_t
*bpf_mlock_grp_attr
;
168 static lck_attr_t
*bpf_mlock_attr
;
171 * Mark a descriptor free by making it point to itself.
172 * This is probably cheaper than marking with a constant since
173 * the address should be in a register anyway.
175 #endif /* __APPLE__ */
177 static int bpf_allocbufs(struct bpf_d
*);
178 static void bpf_attachd(struct bpf_d
*d
, struct bpf_if
*bp
);
179 static void bpf_detachd(struct bpf_d
*d
);
180 static void bpf_freed(struct bpf_d
*);
181 static void bpf_mcopy(const void *, void *, size_t);
182 static int bpf_movein(struct uio
*, int,
183 struct mbuf
**, struct sockaddr
*, int *);
184 static int bpf_setif(struct bpf_d
*, struct ifreq
*);
185 static void bpf_wakeup(struct bpf_d
*);
186 static void catchpacket(struct bpf_d
*, u_char
*, u_int
,
187 u_int
, void (*)(const void *, void *, size_t));
188 static void reset_d(struct bpf_d
*);
189 static int bpf_setf(struct bpf_d
*, struct user_bpf_program
*);
191 /*static void *bpf_devfs_token[MAXBPFILTER];*/
193 static int bpf_devsw_installed
;
195 void bpf_init(void *unused
);
196 int bpf_tap_callback(struct ifnet
*ifp
, struct mbuf
*m
);
199 * Darwin differs from BSD here, the following are static
200 * on BSD and not static on Darwin.
206 ioctl_fcn_t bpfioctl
;
207 select_fcn_t bpfpoll
;
210 /* Darwin's cdevsw struct differs slightly from BSDs */
211 #define CDEV_MAJOR 23
212 static struct cdevsw bpf_cdevsw
= {
214 /* close */ bpfclose
,
216 /* write */ bpfwrite
,
217 /* ioctl */ bpfioctl
,
219 /* reset */ eno_reset
,
221 /* select */ bpfpoll
,
223 /* strategy*/ eno_strat
,
229 #define SOCKADDR_HDR_LEN offsetof(struct sockaddr, sa_data)
232 bpf_movein(struct uio
*uio
, int linktype
, struct mbuf
**mp
, struct sockaddr
*sockp
, int *datlen
)
241 * Build a sockaddr based on the data link layer type.
242 * We do this at this level because the ethernet header
243 * is copied directly into the data field of the sockaddr.
244 * In the case of SLIP, there is no header and the packet
245 * is forwarded as is.
246 * Also, we are careful to leave room at the front of the mbuf
247 * for the link level header.
252 sockp
->sa_family
= AF_INET
;
257 sockp
->sa_family
= AF_UNSPEC
;
258 /* XXX Would MAXLINKHDR be better? */
259 hlen
= sizeof(struct ether_header
);
263 #if defined(__FreeBSD__) || defined(__bsdi__)
264 sockp
->sa_family
= AF_IMPLINK
;
267 sockp
->sa_family
= AF_UNSPEC
;
268 /* XXX 4(FORMAC)+6(dst)+6(src)+3(LLC)+5(SNAP) */
275 sockp
->sa_family
= AF_UNSPEC
;
280 case DLT_ATM_RFC1483
:
282 * en atm driver requires 4-byte atm pseudo header.
283 * though it isn't standard, vpi:vci needs to be
286 sockp
->sa_family
= AF_UNSPEC
;
287 hlen
= 12; /* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
291 sockp
->sa_family
= AF_UNSPEC
;
292 hlen
= 4; /* This should match PPP_HDRLEN */
295 case DLT_APPLE_IP_OVER_IEEE1394
:
296 sockp
->sa_family
= AF_UNSPEC
;
297 hlen
= sizeof(struct firewire_header
);
303 if ((hlen
+ SOCKADDR_HDR_LEN
) > sockp
->sa_len
) {
311 // LP64todo - fix this!
312 len
= uio_resid(uio
);
313 *datlen
= len
- hlen
;
314 if ((unsigned)len
> MCLBYTES
)
317 MGETHDR(m
, M_WAIT
, MT_DATA
);
320 if ((unsigned)len
> MHLEN
) {
323 if ((m
->m_flags
& M_EXT
) == 0) {
326 if (m
->m_len
!= MCLBYTES
) {
332 m
->m_pkthdr
.len
= m
->m_len
= len
;
333 m
->m_pkthdr
.rcvif
= NULL
;
336 * Make room for link header.
339 m
->m_pkthdr
.len
-= hlen
;
342 m
->m_data
+= hlen
; /* XXX */
346 error
= UIOMOVE((caddr_t
)sockp
->sa_data
, hlen
, UIO_WRITE
, uio
);
350 error
= UIOMOVE(mtod(m
, caddr_t
), len
- hlen
, UIO_WRITE
, uio
);
359 /* Callback registered with Ethernet driver. */
360 int bpf_tap_callback(struct ifnet
*ifp
, struct mbuf
*m
)
363 * Do nothing if the BPF tap has been turned off.
364 * This is to protect from a potential race where this
365 * call blocks on the lock. And in the meantime
366 * BPF is turned off, which will clear if_bpf.
374 * The dynamic addition of a new device node must block all processes that are opening
375 * the last device so that no process will get an unexpected ENOENT
378 bpf_make_dev_t(int maj
)
380 static int bpf_growing
= 0;
381 unsigned int cur_size
= nbpfilter
, i
;
383 if (nbpfilter
>= bpf_maxdevices
)
386 while (bpf_growing
) {
387 /* Wait until new device has been created */
388 (void)tsleep((caddr_t
)&bpf_growing
, PZERO
, "bpf_growing", 0);
390 if (nbpfilter
> cur_size
) {
391 /* other thread grew it already */
396 /* need to grow bpf_dtab first */
397 if (nbpfilter
== bpf_dtab_size
) {
399 struct bpf_d
**new_dtab
= NULL
;
400 struct bpf_d
**old_dtab
= NULL
;
402 new_dtab_size
= bpf_dtab_size
+ NBPFILTER
;
403 new_dtab
= (struct bpf_d
**)_MALLOC(sizeof(struct bpf_d
*) * new_dtab_size
, M_DEVBUF
, M_WAIT
);
405 printf("bpf_make_dev_t: malloc bpf_dtab failed\n");
409 bcopy(bpf_dtab
, new_dtab
,
410 sizeof(struct bpf_d
*) * bpf_dtab_size
);
412 bzero(new_dtab
+ bpf_dtab_size
,
413 sizeof(struct bpf_d
*) * NBPFILTER
);
416 bpf_dtab_size
= new_dtab_size
;
417 if (old_dtab
!= NULL
)
418 _FREE(old_dtab
, M_DEVBUF
);
421 (void) devfs_make_node(makedev(maj
, i
),
422 DEVFS_CHAR
, UID_ROOT
, GID_WHEEL
, 0600,
426 wakeup((caddr_t
)&bpf_growing
);
432 * Attach file to the bpf interface, i.e. make d listen on bp.
433 * Must be called at splimp.
436 bpf_attachd(struct bpf_d
*d
, struct bpf_if
*bp
)
439 * Point d at bp, and add d to the interface's list of listeners.
440 * Finally, point the driver's bpf cookie at the interface so
441 * it will divert packets to bpf.
444 d
->bd_next
= bp
->bif_dlist
;
447 bp
->bif_ifp
->if_bpf
= bp
;
450 dlil_set_bpf_tap(bp
->bif_ifp
, BPF_TAP_INPUT_OUTPUT
, bpf_tap_callback
);
455 * Detach a file from its interface.
458 bpf_detachd(struct bpf_d
*d
)
465 ifp
= d
->bd_bif
->bif_ifp
;
471 * Check if this descriptor had requested promiscuous mode.
472 * If so, turn it off.
476 if (ifnet_set_promiscuous(bp
->bif_ifp
, 0))
478 * Something is really wrong if we were able to put
479 * the driver into promiscuous mode, but can't
481 * Most likely the network interface is gone.
483 printf("bpf: ifnet_set_promiscuous failed");
485 /* Remove d from the interface's descriptor list. */
490 panic("bpf_detachd: descriptor not in list");
493 if (bp
->bif_dlist
== 0) {
495 * Let the driver know that there are no more listeners.
497 if (ifp
->if_set_bpf_tap
)
498 (*ifp
->if_set_bpf_tap
)(ifp
, BPF_TAP_DISABLE
, 0);
499 d
->bd_bif
->bif_ifp
->if_bpf
= 0;
506 * Open ethernet device. Returns ENXIO for illegal minor device number,
507 * EBUSY if file is open by another process.
511 bpfopen(dev_t dev
, __unused
int flags
, __unused
int fmt
, __unused
struct proc
*p
)
513 register struct bpf_d
*d
;
515 if ((unsigned int) minor(dev
) >= nbpfilter
)
519 * New device nodes are created on demand when opening the last one.
520 * The programming model is for processes to loop on the minor starting at 0
521 * as long as EBUSY is returned. The loop stops when either the open succeeds or
522 * an error other that EBUSY is returned. That means that bpf_make_dev_t() must
523 * block all processes that are opening the last node. If not all
524 * processes are blocked, they could unexpectedly get ENOENT and abort their
527 if ((unsigned int) minor(dev
) == (nbpfilter
- 1))
528 bpf_make_dev_t(major(dev
));
531 * Each minor can be opened by only one process. If the requested
532 * minor is in use, return EBUSY.
534 * Important: bpfopen() and bpfclose() have to check and set the status of a device
535 * in the same lockin context otherwise the device may be leaked because the vnode use count
536 * will be unpextectly greater than 1 when close() is called.
538 if (bpf_dtab
[minor(dev
)] == 0)
539 bpf_dtab
[minor(dev
)] = (void *)1; /* Mark opening */
543 d
= (struct bpf_d
*)_MALLOC(sizeof(struct bpf_d
), M_DEVBUF
, M_WAIT
);
545 /* this really is a catastrophic failure */
546 printf("bpfopen: malloc bpf_d failed\n");
547 bpf_dtab
[minor(dev
)] = 0;
550 bzero(d
, sizeof(struct bpf_d
));
553 * It is not necessary to take the BPF lock here because no other
554 * thread can access the device until it is marked opened...
557 /* Mark "in use" and do most initialization. */
558 d
->bd_bufsize
= bpf_bufsize
;
561 bpf_dtab
[minor(dev
)] = d
; /* Mark opened */
567 * Close the descriptor by detaching it from its interface,
568 * deallocating its buffers, and marking it free.
572 bpfclose(dev_t dev
, __unused
int flags
, __unused
int fmt
, __unused
struct proc
*p
)
574 register struct bpf_d
*d
;
576 d
= bpf_dtab
[minor(dev
)];
577 if (d
== 0 || d
== (void *)1)
580 bpf_dtab
[minor(dev
)] = (void *)1; /* Mark closing */
582 /* Take BPF lock to ensure no other thread is using the device */
583 lck_mtx_lock(bpf_mlock
);
587 selthreadclear(&d
->bd_sel
);
590 lck_mtx_unlock(bpf_mlock
);
592 /* Mark free in same context as bpfopen comes to check */
593 bpf_dtab
[minor(dev
)] = 0; /* Mark closed */
600 #define BPF_SLEEP bpf_sleep
603 bpf_sleep(struct bpf_d
*d
, int pri
, const char *wmesg
, int timo
)
607 lck_mtx_unlock(bpf_mlock
);
609 st
= tsleep((caddr_t
)d
, pri
, wmesg
, timo
);
611 lck_mtx_lock(bpf_mlock
);
617 * Rotate the packet buffers in descriptor d. Move the store buffer
618 * into the hold slot, and the free buffer into the store slot.
619 * Zero the length of the new store buffer.
621 #define ROTATE_BUFFERS(d) \
622 (d)->bd_hbuf = (d)->bd_sbuf; \
623 (d)->bd_hlen = (d)->bd_slen; \
624 (d)->bd_sbuf = (d)->bd_fbuf; \
628 * bpfread - read next chunk of packets from buffers
631 bpfread(dev_t dev
, struct uio
*uio
, int ioflag
)
633 register struct bpf_d
*d
;
637 d
= bpf_dtab
[minor(dev
)];
638 if (d
== 0 || d
== (void *)1)
641 lck_mtx_lock(bpf_mlock
);
645 * Restrict application to use a buffer the same size as
648 // LP64todo - fix this
649 if (uio
->uio_resid
!= d
->bd_bufsize
) {
650 lck_mtx_unlock(bpf_mlock
);
656 * If the hold buffer is empty, then do a timed sleep, which
657 * ends when the timeout expires or when enough packets
658 * have arrived to fill the store buffer.
660 while (d
->bd_hbuf
== 0) {
661 if (d
->bd_immediate
&& d
->bd_slen
!= 0) {
663 * A packet(s) either arrived since the previous
664 * read or arrived while we were asleep.
665 * Rotate the buffers and return what's here.
672 * No data is available, check to see if the bpf device
673 * is still pointed at a real interface. If not, return
674 * ENXIO so that the userland process knows to rebind
675 * it before using it again.
677 if (d
->bd_bif
== NULL
) {
679 lck_mtx_unlock(bpf_mlock
);
683 if (ioflag
& IO_NDELAY
)
686 error
= BPF_SLEEP(d
, PRINET
|PCATCH
, "bpf",
688 if (error
== EINTR
|| error
== ERESTART
) {
690 lck_mtx_unlock(bpf_mlock
);
693 if (error
== EWOULDBLOCK
) {
695 * On a timeout, return what's in the buffer,
696 * which may be nothing. If there is something
697 * in the store buffer, we can rotate the buffers.
701 * We filled up the buffer in between
702 * getting the timeout and arriving
703 * here, so we don't need to rotate.
707 if (d
->bd_slen
== 0) {
709 lck_mtx_unlock(bpf_mlock
);
717 * At this point, we know we have something in the hold slot.
722 * Move data from hold buffer into user space.
723 * We know the entire buffer is transferred since
724 * we checked above that the read buffer is bpf_bufsize bytes.
726 error
= UIOMOVE(d
->bd_hbuf
, d
->bd_hlen
, UIO_READ
, uio
);
729 d
->bd_fbuf
= d
->bd_hbuf
;
733 lck_mtx_unlock(bpf_mlock
);
739 * If there are processes sleeping on this descriptor, wake them up.
742 bpf_wakeup(struct bpf_d
*d
)
745 if (d
->bd_async
&& d
->bd_sig
&& d
->bd_sigio
)
746 pgsigio(d
->bd_sigio
, d
->bd_sig
, 0);
749 selwakeup(&d
->bd_sel
);
752 d
->bd_sel
.si_pid
= 0;
756 selwakeup(d
->bd_selproc
, (int)d
->bd_selcoll
);
763 /* keep in sync with bpf_movein above: */
764 #define MAX_DATALINK_HDR_LEN (sizeof(struct firewire_header))
767 bpfwrite(dev_t dev
, struct uio
*uio
, __unused
int ioflag
)
769 register struct bpf_d
*d
;
773 char dst_buf
[SOCKADDR_HDR_LEN
+ MAX_DATALINK_HDR_LEN
];
776 d
= bpf_dtab
[minor(dev
)];
777 if (d
== 0 || d
== (void *)1)
780 lck_mtx_lock(bpf_mlock
);
782 if (d
->bd_bif
== 0) {
783 lck_mtx_unlock(bpf_mlock
);
787 ifp
= d
->bd_bif
->bif_ifp
;
789 if (uio
->uio_resid
== 0) {
790 lck_mtx_unlock(bpf_mlock
);
793 ((struct sockaddr
*)dst_buf
)->sa_len
= sizeof(dst_buf
);
794 error
= bpf_movein(uio
, (int)d
->bd_bif
->bif_dlt
, &m
,
795 d
->bd_hdrcmplt
? 0 : (struct sockaddr
*)dst_buf
, &datlen
);
797 lck_mtx_unlock(bpf_mlock
);
801 if ((unsigned)datlen
> ifp
->if_mtu
) {
802 lck_mtx_unlock(bpf_mlock
);
806 lck_mtx_unlock(bpf_mlock
);
808 if (d
->bd_hdrcmplt
) {
809 error
= dlil_output(ifp
, 0, m
, NULL
, NULL
, 1);
812 error
= dlil_output(ifp
, PF_INET
, m
, NULL
, (struct sockaddr
*)dst_buf
, 0);
816 * The driver frees the mbuf.
822 * Reset a descriptor by flushing its packet buffer and clearing the
823 * receive and drop counts. Should be called at splimp.
826 reset_d(struct bpf_d
*d
)
829 /* Free the hold buffer. */
830 d
->bd_fbuf
= d
->bd_hbuf
;
840 * FIONREAD Check for read packet available.
841 * SIOCGIFADDR Get interface address - convenient hook to driver.
842 * BIOCGBLEN Get buffer len [for read()].
843 * BIOCSETF Set ethernet read filter.
844 * BIOCFLUSH Flush read packet buffer.
845 * BIOCPROMISC Put interface into promiscuous mode.
846 * BIOCGDLT Get link layer type.
847 * BIOCGETIF Get interface name.
848 * BIOCSETIF Set interface.
849 * BIOCSRTIMEOUT Set read timeout.
850 * BIOCGRTIMEOUT Get read timeout.
851 * BIOCGSTATS Get packet stats.
852 * BIOCIMMEDIATE Set immediate mode.
853 * BIOCVERSION Get filter language version.
854 * BIOCGHDRCMPLT Get "header already complete" flag
855 * BIOCSHDRCMPLT Set "header already complete" flag
856 * BIOCGSEESENT Get "see packets sent" flag
857 * BIOCSSEESENT Set "see packets sent" flag
861 bpfioctl(dev_t dev
, u_long cmd
, caddr_t addr
, __unused
int flags
, struct proc
*p
)
863 register struct bpf_d
*d
;
866 d
= bpf_dtab
[minor(dev
)];
867 if (d
== 0 || d
== (void *)1)
870 lck_mtx_lock(bpf_mlock
);
879 * Check for read packet available.
902 ifp
= d
->bd_bif
->bif_ifp
;
903 error
= dlil_ioctl(0, ifp
, cmd
, addr
);
909 * Get buffer len [for read()].
912 *(u_int
*)addr
= d
->bd_bufsize
;
925 register u_int size
= *(u_int
*)addr
;
927 if (size
> bpf_maxbufsize
)
928 *(u_int
*)addr
= size
= bpf_maxbufsize
;
929 else if (size
< BPF_MINBUFSIZE
)
930 *(u_int
*)addr
= size
= BPF_MINBUFSIZE
;
931 d
->bd_bufsize
= size
;
937 * Set link layer read filter.
940 if (proc_is64bit(p
)) {
941 error
= bpf_setf(d
, (struct user_bpf_program
*)addr
);
944 struct bpf_program
* tmpp
;
945 struct user_bpf_program tmp
;
947 tmpp
= (struct bpf_program
*)addr
;
948 tmp
.bf_len
= tmpp
->bf_len
;
949 tmp
.bf_insns
= CAST_USER_ADDR_T(tmpp
->bf_insns
);
950 error
= bpf_setf(d
, &tmp
);
955 * Flush read packet buffer.
964 * Put interface into promiscuous mode.
967 if (d
->bd_bif
== 0) {
969 * No interface attached yet.
975 if (d
->bd_promisc
== 0) {
976 error
= ifnet_set_promiscuous(d
->bd_bif
->bif_ifp
, 1);
984 * Get device parameters.
990 *(u_int
*)addr
= d
->bd_bif
->bif_dlt
;
994 * Get interface name.
1000 struct ifnet
*const ifp
= d
->bd_bif
->bif_ifp
;
1001 struct ifreq
*const ifr
= (struct ifreq
*)addr
;
1003 snprintf(ifr
->ifr_name
, sizeof(ifr
->ifr_name
),
1004 "%s%d", ifp
->if_name
, ifp
->if_unit
);
1012 error
= bpf_setif(d
, (struct ifreq
*)addr
);
1020 struct timeval
*tv
= (struct timeval
*)addr
;
1023 * Subtract 1 tick from tvtohz() since this isn't
1026 if ((error
= itimerfix(tv
)) == 0)
1027 d
->bd_rtout
= tvtohz(tv
) - 1;
1036 struct timeval
*tv
= (struct timeval
*)addr
;
1038 tv
->tv_sec
= d
->bd_rtout
/ hz
;
1039 tv
->tv_usec
= (d
->bd_rtout
% hz
) * tick
;
1048 struct bpf_stat
*bs
= (struct bpf_stat
*)addr
;
1050 bs
->bs_recv
= d
->bd_rcount
;
1051 bs
->bs_drop
= d
->bd_dcount
;
1056 * Set immediate mode.
1059 d
->bd_immediate
= *(u_int
*)addr
;
1064 struct bpf_version
*bv
= (struct bpf_version
*)addr
;
1066 bv
->bv_major
= BPF_MAJOR_VERSION
;
1067 bv
->bv_minor
= BPF_MINOR_VERSION
;
1072 * Get "header already complete" flag
1075 *(u_int
*)addr
= d
->bd_hdrcmplt
;
1079 * Set "header already complete" flag
1082 d
->bd_hdrcmplt
= *(u_int
*)addr
? 1 : 0;
1086 * Get "see sent packets" flag
1089 *(u_int
*)addr
= d
->bd_seesent
;
1093 * Set "see sent packets" flag
1096 d
->bd_seesent
= *(u_int
*)addr
;
1099 case FIONBIO
: /* Non-blocking I/O */
1102 case FIOASYNC
: /* Send signal on receive packets */
1103 d
->bd_async
= *(int *)addr
;
1107 error
= fsetown(*(int *)addr
, &d
->bd_sigio
);
1111 *(int *)addr
= fgetown(d
->bd_sigio
);
1114 /* This is deprecated, FIOSETOWN should be used instead. */
1116 error
= fsetown(-(*(int *)addr
), &d
->bd_sigio
);
1119 /* This is deprecated, FIOGETOWN should be used instead. */
1121 *(int *)addr
= -fgetown(d
->bd_sigio
);
1124 case BIOCSRSIG
: /* Set receive signal */
1128 sig
= *(u_int
*)addr
;
1137 *(u_int
*)addr
= d
->bd_sig
;
1141 lck_mtx_unlock(bpf_mlock
);
1147 * Set d's packet filter program to fp. If this file already has a filter,
1148 * free it and replace it. Returns EINVAL for bogus requests.
1151 bpf_setf(struct bpf_d
*d
, struct user_bpf_program
*fp
)
1153 struct bpf_insn
*fcode
, *old
;
1158 if (fp
->bf_insns
== USER_ADDR_NULL
) {
1159 if (fp
->bf_len
!= 0)
1166 FREE((caddr_t
)old
, M_DEVBUF
);
1170 if (flen
> BPF_MAXINSNS
)
1173 size
= flen
* sizeof(struct bpf_insn
);
1174 fcode
= (struct bpf_insn
*) _MALLOC(size
, M_DEVBUF
, M_WAIT
);
1179 if (copyin(fp
->bf_insns
, (caddr_t
)fcode
, size
) == 0 &&
1180 bpf_validate(fcode
, (int)flen
)) {
1182 d
->bd_filter
= fcode
;
1186 FREE((caddr_t
)old
, M_DEVBUF
);
1190 FREE((caddr_t
)fcode
, M_DEVBUF
);
1195 * Detach a file from its current interface (if attached at all) and attach
1196 * to the interface indicated by the name stored in ifr.
1197 * Return an errno or 0.
1200 bpf_setif(struct bpf_d
*d
, struct ifreq
*ifr
)
1204 struct ifnet
*theywant
;
1206 theywant
= ifunit(ifr
->ifr_name
);
1211 * Look through attached interfaces for the named one.
1213 for (bp
= bpf_iflist
; bp
!= 0; bp
= bp
->bif_next
) {
1214 struct ifnet
*ifp
= bp
->bif_ifp
;
1216 if (ifp
== 0 || ifp
!= theywant
)
1219 * We found the requested interface.
1220 * If it's not up, return an error.
1221 * Allocate the packet buffers if we need to.
1222 * If we're already attached to requested interface,
1223 * just flush the buffer.
1225 if ((ifp
->if_flags
& IFF_UP
) == 0)
1228 if (d
->bd_sbuf
== 0) {
1229 error
= bpf_allocbufs(d
);
1234 if (bp
!= d
->bd_bif
) {
1237 * Detach if attached to something else.
1252 * Support for select() and poll() system calls
1254 * Return true iff the specific operation will not block indefinitely.
1255 * Otherwise, return false but make a note that a selwakeup() must be done.
1258 bpfpoll(dev_t dev
, int events
, void * wql
, struct proc
*p
)
1260 register struct bpf_d
*d
;
1264 d
= bpf_dtab
[minor(dev
)];
1265 if (d
== 0 || d
== (void *)1)
1268 lck_mtx_lock(bpf_mlock
);
1271 * An imitation of the FIONREAD ioctl code.
1273 if (d
->bd_bif
== NULL
) {
1274 lck_mtx_unlock(bpf_mlock
);
1279 if (events
& (POLLIN
| POLLRDNORM
)) {
1280 if (d
->bd_hlen
!= 0 || (d
->bd_immediate
&& d
->bd_slen
!= 0))
1281 revents
|= events
& (POLLIN
| POLLRDNORM
);
1283 selrecord(p
, &d
->bd_sel
, wql
);
1287 lck_mtx_unlock(bpf_mlock
);
1292 * Incoming linkage from device drivers. Process the packet pkt, of length
1293 * pktlen, which is stored in a contiguous buffer. The packet is parsed
1294 * by each process' filter, and if accepted, stashed into the corresponding
1298 bpf_tap(struct ifnet
*ifp
, u_char
*pkt
, u_int pktlen
)
1301 register struct bpf_d
*d
;
1302 register u_int slen
;
1304 * Note that the ipl does not have to be raised at this point.
1305 * The only problem that could arise here is that if two different
1306 * interfaces shared any data. This is not the case.
1308 lck_mtx_lock(bpf_mlock
);
1314 for (d
= bp
->bif_dlist
; d
!= 0; d
= d
->bd_next
) {
1316 slen
= bpf_filter(d
->bd_filter
, pkt
, pktlen
, pktlen
);
1318 catchpacket(d
, pkt
, pktlen
, slen
, bcopy
);
1322 lck_mtx_unlock(bpf_mlock
);
1327 * Copy data from an mbuf chain into a buffer. This code is derived
1328 * from m_copydata in sys/uipc_mbuf.c.
1331 bpf_mcopy(const void *src_arg
, void *dst_arg
, size_t len
)
1333 const struct mbuf
*m
;
1342 count
= min(m
->m_len
, len
);
1343 bcopy(mtod(m
, const void *), dst
, count
);
1351 * Incoming linkage from device drivers, when packet is in an mbuf chain.
1354 bpf_mtap(struct ifnet
*ifp
, struct mbuf
*m
)
1361 lck_mtx_lock(bpf_mlock
);
1366 for (m0
= m
; m0
!= 0; m0
= m0
->m_next
)
1367 pktlen
+= m0
->m_len
;
1369 for (d
= bp
->bif_dlist
; d
!= 0; d
= d
->bd_next
) {
1370 if (!d
->bd_seesent
&& (m
->m_pkthdr
.rcvif
== NULL
))
1373 slen
= bpf_filter(d
->bd_filter
, (u_char
*)m
, pktlen
, 0);
1375 catchpacket(d
, (u_char
*)m
, pktlen
, slen
, bpf_mcopy
);
1379 lck_mtx_unlock(bpf_mlock
);
1383 * Move the packet data from interface memory (pkt) into the
1384 * store buffer. Return 1 if it's time to wakeup a listener (buffer full),
1385 * otherwise 0. "copy" is the routine called to do the actual data
1386 * transfer. bcopy is passed in to copy contiguous chunks, while
1387 * bpf_mcopy is passed in to copy mbuf chains. In the latter case,
1388 * pkt is really an mbuf.
1391 catchpacket(struct bpf_d
*d
, u_char
*pkt
, u_int pktlen
, u_int snaplen
,
1392 void (*cpfn
)(const void *, void *, size_t))
1394 register struct bpf_hdr
*hp
;
1395 register int totlen
, curlen
;
1396 register int hdrlen
= d
->bd_bif
->bif_hdrlen
;
1398 * Figure out how many bytes to move. If the packet is
1399 * greater or equal to the snapshot length, transfer that
1400 * much. Otherwise, transfer the whole packet (unless
1401 * we hit the buffer size limit).
1403 totlen
= hdrlen
+ min(snaplen
, pktlen
);
1404 if (totlen
> d
->bd_bufsize
)
1405 totlen
= d
->bd_bufsize
;
1408 * Round up the end of the previous packet to the next longword.
1410 curlen
= BPF_WORDALIGN(d
->bd_slen
);
1411 if (curlen
+ totlen
> d
->bd_bufsize
) {
1413 * This packet will overflow the storage buffer.
1414 * Rotate the buffers if we can, then wakeup any
1417 if (d
->bd_fbuf
== 0) {
1419 * We haven't completed the previous read yet,
1420 * so drop the packet.
1429 else if (d
->bd_immediate
)
1431 * Immediate mode is set. A packet arrived so any
1432 * reads should be woken up.
1437 * Append the bpf header.
1439 hp
= (struct bpf_hdr
*)(d
->bd_sbuf
+ curlen
);
1441 microtime(&hp
->bh_tstamp
);
1443 uniqtime(&hp
->bh_tstamp
);
1445 hp
->bh_tstamp
= time
;
1447 hp
->bh_datalen
= pktlen
;
1448 hp
->bh_hdrlen
= hdrlen
;
1450 * Copy the packet data into the store buffer and update its length.
1452 (*cpfn
)(pkt
, (u_char
*)hp
+ hdrlen
, (hp
->bh_caplen
= totlen
- hdrlen
));
1453 d
->bd_slen
= curlen
+ totlen
;
1457 * Initialize all nonzero fields of a descriptor.
1460 bpf_allocbufs(struct bpf_d
*d
)
1462 d
->bd_fbuf
= (caddr_t
) _MALLOC(d
->bd_bufsize
, M_DEVBUF
, M_WAIT
);
1463 if (d
->bd_fbuf
== 0)
1466 d
->bd_sbuf
= (caddr_t
) _MALLOC(d
->bd_bufsize
, M_DEVBUF
, M_WAIT
);
1467 if (d
->bd_sbuf
== 0) {
1468 FREE(d
->bd_fbuf
, M_DEVBUF
);
1477 * Free buffers currently in use by a descriptor.
1481 bpf_freed(struct bpf_d
*d
)
1484 * We don't need to lock out interrupts since this descriptor has
1485 * been detached from its interface and it yet hasn't been marked
1488 if (d
->bd_sbuf
!= 0) {
1489 FREE(d
->bd_sbuf
, M_DEVBUF
);
1490 if (d
->bd_hbuf
!= 0)
1491 FREE(d
->bd_hbuf
, M_DEVBUF
);
1492 if (d
->bd_fbuf
!= 0)
1493 FREE(d
->bd_fbuf
, M_DEVBUF
);
1496 FREE((caddr_t
)d
->bd_filter
, M_DEVBUF
);
1500 * Attach an interface to bpf. driverp is a pointer to a (struct bpf_if *)
1501 * in the driver's softc; dlt is the link layer type; hdrlen is the fixed
1502 * size of the link header (variable length headers not yet supported).
1505 bpfattach(struct ifnet
*ifp
, u_int dlt
, u_int hdrlen
)
1508 bp
= (struct bpf_if
*) _MALLOC(sizeof(*bp
), M_DEVBUF
, M_WAIT
);
1512 lck_mtx_lock(bpf_mlock
);
1518 bp
->bif_next
= bpf_iflist
;
1521 bp
->bif_ifp
->if_bpf
= 0;
1524 * Compute the length of the bpf header. This is not necessarily
1525 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1526 * that the network layer header begins on a longword boundary (for
1527 * performance reasons and to alleviate alignment restrictions).
1529 bp
->bif_hdrlen
= BPF_WORDALIGN(hdrlen
+ SIZEOF_BPF_HDR
) - hdrlen
;
1531 /* Take a reference on the interface */
1534 lck_mtx_unlock(bpf_mlock
);
1538 printf("bpf: %s%d attached\n", ifp
->if_name
, ifp
->if_unit
);
1543 * Detach bpf from an interface. This involves detaching each descriptor
1544 * associated with the interface, and leaving bd_bif NULL. Notify each
1545 * descriptor as it's detached so that any sleepers wake up and get
1549 bpfdetach(struct ifnet
*ifp
)
1551 struct bpf_if
*bp
, *bp_prev
;
1557 lck_mtx_lock(bpf_mlock
);
1559 /* Locate BPF interface information */
1561 for (bp
= bpf_iflist
; bp
!= NULL
; bp
= bp
->bif_next
) {
1562 if (ifp
== bp
->bif_ifp
)
1568 /* Check for no BPF interface information */
1574 /* Interface wasn't attached */
1575 if (bp
->bif_ifp
== NULL
) {
1578 printf("bpfdetach: %s%d was not attached\n", ifp
->if_name
,
1584 while ((d
= bp
->bif_dlist
) != NULL
) {
1590 bp_prev
->bif_next
= bp
->bif_next
;
1592 bpf_iflist
= bp
->bif_next
;
1597 lck_mtx_unlock(bpf_mlock
);
1605 bpf_init(__unused
void *unused
)
1611 if (bpf_devsw_installed
== 0) {
1612 bpf_devsw_installed
= 1;
1614 bpf_mlock_grp_attr
= lck_grp_attr_alloc_init();
1615 lck_grp_attr_setdefault(bpf_mlock_grp_attr
);
1617 bpf_mlock_grp
= lck_grp_alloc_init("bpf", bpf_mlock_grp_attr
);
1619 bpf_mlock_attr
= lck_attr_alloc_init();
1620 lck_attr_setdefault(bpf_mlock_attr
);
1622 bpf_mlock
= lck_mtx_alloc_init(bpf_mlock_grp
, bpf_mlock_attr
);
1624 if (bpf_mlock
== 0) {
1625 printf("bpf_init: failed to allocate bpf_mlock\n");
1626 bpf_devsw_installed
= 0;
1630 maj
= cdevsw_add(CDEV_MAJOR
, &bpf_cdevsw
);
1633 lck_mtx_free(bpf_mlock
, bpf_mlock_grp
);
1635 lck_attr_free(bpf_mlock_attr
);
1637 lck_grp_free(bpf_mlock_grp
);
1638 if (bpf_mlock_grp_attr
)
1639 lck_grp_attr_free(bpf_mlock_grp_attr
);
1644 bpf_mlock_grp_attr
= 0;
1645 bpf_devsw_installed
= 0;
1646 printf("bpf_init: failed to allocate a major number!\n");
1650 for (i
= 0 ; i
< NBPFILTER
; i
++)
1651 bpf_make_dev_t(maj
);
1654 cdevsw_add(&bpf_cdevsw
);
1659 SYSINIT(bpfdev
,SI_SUB_DRIVERS
,SI_ORDER_MIDDLE
+CDEV_MAJOR
,bpf_drvinit
,NULL
)
1665 * NOP stubs to allow bpf-using drivers to load and function.
1667 * A 'better' implementation would allow the core bpf functionality
1668 * to be loaded at runtime.
1672 bpf_tap(ifp
, pkt
, pktlen
)
1674 register u_char
*pkt
;
1675 register u_int pktlen
;
1687 bpfattach(ifp
, dlt
, hdrlen
)
1700 bpf_filter(pc
, p
, wirelen
, buflen
)
1701 register const struct bpf_insn
*pc
;
1704 register u_int buflen
;
1706 return -1; /* "no filter" behaviour */
1708 #endif /* !defined(__APPLE__) */
1709 #endif /* NBPFILTER > 0 */