]>
git.saurik.com Git - apple/xnu.git/blob - bsd/net/bpf.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1990, 1991, 1993
24 * The Regents of the University of California. All rights reserved.
26 * This code is derived from the Stanford/CMU enet packet filter,
27 * (net/enet.c) distributed as part of 4.3BSD, and code contributed
28 * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
29 * Berkeley Laboratory.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by the University of
42 * California, Berkeley and its contributors.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * @(#)bpf.c 8.2 (Berkeley) 3/28/94
61 * $FreeBSD: src/sys/net/bpf.c,v 1.59.2.5 2001/01/05 04:49:09 jdp Exp $
69 #define inline __inline
72 #include <sys/param.h>
73 #include <sys/systm.h>
75 #include <sys/malloc.h>
79 #include <sys/signalvar.h>
80 #include <sys/filio.h>
81 #include <sys/sockio.h>
82 #include <sys/ttycom.h>
83 #include <sys/filedesc.h>
85 #if defined(sparc) && BSD < 199103
86 #include <sys/stream.h>
90 #include <sys/socket.h>
91 #include <sys/vnode.h>
95 #include <net/bpfdesc.h>
97 #include <netinet/in.h>
98 #include <netinet/if_ether.h>
99 #include <sys/kernel.h>
100 #include <sys/sysctl.h>
103 #include <miscfs/devfs/devfs.h>
104 #include <net/dlil.h>
109 * Older BSDs don't have kernel malloc.
113 static caddr_t
bpf_alloc();
114 #include <net/bpf_compat.h>
115 #define BPF_BUFSIZE (MCLBYTES-8)
116 #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, code, uio)
118 #define BPF_BUFSIZE 4096
119 #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, uio)
122 #define PRINET 26 /* interruptible */
125 * The default read buffer size is patchable.
127 static int bpf_bufsize
= BPF_BUFSIZE
;
128 SYSCTL_INT(_debug
, OID_AUTO
, bpf_bufsize
, CTLFLAG_RW
,
129 &bpf_bufsize
, 0, "");
130 static int bpf_maxbufsize
= BPF_MAXBUFSIZE
;
131 SYSCTL_INT(_debug
, OID_AUTO
, bpf_maxbufsize
, CTLFLAG_RW
,
132 &bpf_maxbufsize
, 0, "");
135 * bpf_iflist is the list of interfaces; each corresponds to an ifnet
136 * bpf_dtab holds the descriptors, indexed by minor device #
138 static struct bpf_if
*bpf_iflist
;
141 * BSD now stores the bpf_d in the dev_t which is a struct
142 * on their system. Our dev_t is an int, so we still store
143 * the bpf_d in a separate table indexed by minor device #.
145 static struct bpf_d bpf_dtab
[NBPFILTER
];
146 static int bpf_dtab_init
;
147 static int nbpfilter
= NBPFILTER
;
150 static int bpf_allocbufs
__P((struct bpf_d
*));
151 static void bpf_attachd
__P((struct bpf_d
*d
, struct bpf_if
*bp
));
152 static void bpf_detachd
__P((struct bpf_d
*d
));
153 static void bpf_freed
__P((struct bpf_d
*));
154 static void bpf_mcopy
__P((const void *, void *, size_t));
155 static int bpf_movein
__P((struct uio
*, int,
156 struct mbuf
**, struct sockaddr
*, int *));
157 static int bpf_setif
__P((struct bpf_d
*, struct ifreq
*));
159 bpf_wakeup
__P((struct bpf_d
*));
160 static void catchpacket
__P((struct bpf_d
*, u_char
*, u_int
,
161 u_int
, void (*)(const void *, void *, size_t)));
162 static void reset_d
__P((struct bpf_d
*));
163 static int bpf_setf
__P((struct bpf_d
*, struct bpf_program
*));
166 * Darwin differs from BSD here, the following are static
167 * on BSD and not static on Darwin.
174 select_fcn_t bpfpoll
;
177 void bpf_mtap(struct ifnet
*, struct mbuf
*);
179 int bpfopen(), bpfclose(), bpfread(), bpfwrite(), bpfioctl(),
183 /* Darwin's cdevsw struct differs slightly from BSDs */
184 #define CDEV_MAJOR 23
185 static struct cdevsw bpf_cdevsw
= {
187 /* close */ bpfclose
,
189 /* write */ bpfwrite
,
190 /* ioctl */ bpfioctl
,
194 /* select */ bpfpoll
,
196 /* strategy*/ eno_strat
,
204 bpf_movein(uio
, linktype
, mp
, sockp
, datlen
)
205 register struct uio
*uio
;
206 int linktype
, *datlen
;
207 register struct mbuf
**mp
;
208 register struct sockaddr
*sockp
;
216 * Build a sockaddr based on the data link layer type.
217 * We do this at this level because the ethernet header
218 * is copied directly into the data field of the sockaddr.
219 * In the case of SLIP, there is no header and the packet
220 * is forwarded as is.
221 * Also, we are careful to leave room at the front of the mbuf
222 * for the link level header.
227 sockp
->sa_family
= AF_INET
;
232 sockp
->sa_family
= AF_UNSPEC
;
233 /* XXX Would MAXLINKHDR be better? */
234 hlen
= sizeof(struct ether_header
);
238 #if defined(__FreeBSD__) || defined(__bsdi__)
239 sockp
->sa_family
= AF_IMPLINK
;
242 sockp
->sa_family
= AF_UNSPEC
;
243 /* XXX 4(FORMAC)+6(dst)+6(src)+3(LLC)+5(SNAP) */
250 sockp
->sa_family
= AF_UNSPEC
;
255 case DLT_ATM_RFC1483
:
257 * en atm driver requires 4-byte atm pseudo header.
258 * though it isn't standard, vpi:vci needs to be
261 sockp
->sa_family
= AF_UNSPEC
;
262 hlen
= 12; /* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
266 sockp
->sa_family
= AF_UNSPEC
;
267 hlen
= 4; /* This should match PPP_HDRLEN */
274 len
= uio
->uio_resid
;
275 *datlen
= len
- hlen
;
276 if ((unsigned)len
> MCLBYTES
)
279 MGETHDR(m
, M_WAIT
, MT_DATA
);
285 if ((m
->m_flags
& M_EXT
) == 0) {
288 if (m
->m_len
!= MCLBYTES
) {
294 m
->m_pkthdr
.len
= m
->m_len
= len
;
295 m
->m_pkthdr
.rcvif
= NULL
;
298 * Make room for link header.
301 m
->m_pkthdr
.len
-= hlen
;
304 m
->m_data
+= hlen
; /* XXX */
308 error
= UIOMOVE((caddr_t
)sockp
->sa_data
, hlen
, UIO_WRITE
, uio
);
312 error
= UIOMOVE(mtod(m
, caddr_t
), len
- hlen
, UIO_WRITE
, uio
);
321 /* Callback registered with Ethernet driver. */
322 int bpf_tap_callback(struct ifnet
*ifp
, struct mbuf
*m
)
324 boolean_t funnel_state
;
326 funnel_state
= thread_funnel_set(network_flock
, TRUE
);
329 * Do nothing if the BPF tap has been turned off.
330 * This is to protect from a potential race where this
331 * call blocks on the funnel lock. And in the meantime
332 * BPF is turned off, which will clear if_bpf.
337 thread_funnel_set(network_flock
, funnel_state
);
343 * Attach file to the bpf interface, i.e. make d listen on bp.
344 * Must be called at splimp.
352 * Point d at bp, and add d to the interface's list of listeners.
353 * Finally, point the driver's bpf cookie at the interface so
354 * it will divert packets to bpf.
357 d
->bd_next
= bp
->bif_dlist
;
360 bp
->bif_ifp
->if_bpf
= bp
;
363 if (bp
->bif_ifp
->if_set_bpf_tap
)
364 (*bp
->bif_ifp
->if_set_bpf_tap
)(bp
->bif_ifp
, BPF_TAP_INPUT_OUTPUT
, bpf_tap_callback
);
369 * Detach a file from its interface.
380 ifp
= d
->bd_bif
->bif_ifp
;
386 * Check if this descriptor had requested promiscuous mode.
387 * If so, turn it off.
391 if (ifpromisc(bp
->bif_ifp
, 0))
393 * Something is really wrong if we were able to put
394 * the driver into promiscuous mode, but can't
396 * Most likely the network interface is gone.
398 printf("bpf: ifpromisc failed");
400 /* Remove d from the interface's descriptor list. */
405 panic("bpf_detachd: descriptor not in list");
408 if (bp
->bif_dlist
== 0) {
410 * Let the driver know that there are no more listeners.
412 if (ifp
->if_set_bpf_tap
)
413 (*ifp
->if_set_bpf_tap
)(ifp
, BPF_TAP_DISABLE
, 0);
414 d
->bd_bif
->bif_ifp
->if_bpf
= 0;
422 * Mark a descriptor free by making it point to itself.
423 * This is probably cheaper than marking with a constant since
424 * the address should be in a register anyway.
426 #define D_ISFREE(d) ((d) == (d)->bd_next)
427 #define D_MARKFREE(d) ((d)->bd_next = (d))
428 #define D_MARKUSED(d) ((d)->bd_next = 0)
431 * Open ethernet device. Returns ENXIO for illegal minor device number,
432 * EBUSY if file is open by another process.
436 bpfopen(dev
, flags
, fmt
, p
)
442 register struct bpf_d
*d
;
445 if (minor(dev
) >= nbpfilter
)
448 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
450 d
= &bpf_dtab
[minor(dev
)];
458 * Each minor can be opened by only one process. If the requested
459 * minor is in use, return EBUSY.
463 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
467 /* Mark "free" and do most initialization. */
468 bzero((char *)d
, sizeof(*d
));
472 make_dev(&bpf_cdevsw
, minor(dev
), 0, 0, 0600, "bpf%d", lminor(dev
));
473 MALLOC(d
, struct bpf_d
*, sizeof(*d
), M_BPF
, M_WAITOK
);
474 bzero(d
, sizeof(*d
));
477 d
->bd_bufsize
= bpf_bufsize
;
480 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
485 * Close the descriptor by detaching it from its interface,
486 * deallocating its buffers, and marking it free.
490 bpfclose(dev
, flags
, fmt
, p
)
496 register struct bpf_d
*d
;
499 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
502 funsetown(d
->bd_sigio
);
506 d
= &bpf_dtab
[minor(dev
)];
512 selthreadclear(&d
->bd_sel
);
515 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
520 * Support for SunOS, which does not have tsleep.
527 boolean_t funnel_state
;
528 struct bpf_d
*d
= (struct bpf_d
*)arg
;
529 funnel_state
= thread_funnel_set(network_flock
, TRUE
);
532 (void) thread_funnel_set(network_flock
, FALSE
);
535 #define BPF_SLEEP(chan, pri, s, t) bpf_sleep((struct bpf_d *)chan)
539 register struct bpf_d
*d
;
541 register int rto
= d
->bd_rtout
;
546 timeout(bpf_timeout
, (caddr_t
)d
, rto
);
548 st
= sleep((caddr_t
)d
, PRINET
|PCATCH
);
550 if (d
->bd_timedout
== 0)
551 untimeout(bpf_timeout
, (caddr_t
)d
);
555 return (st
!= 0) ? EINTR
: 0;
558 #define BPF_SLEEP tsleep
562 * Rotate the packet buffers in descriptor d. Move the store buffer
563 * into the hold slot, and the free buffer into the store slot.
564 * Zero the length of the new store buffer.
566 #define ROTATE_BUFFERS(d) \
567 (d)->bd_hbuf = (d)->bd_sbuf; \
568 (d)->bd_hlen = (d)->bd_slen; \
569 (d)->bd_sbuf = (d)->bd_fbuf; \
573 * bpfread - read next chunk of packets from buffers
576 bpfread(dev
, uio
, ioflag
)
581 register struct bpf_d
*d
;
585 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
586 d
= &bpf_dtab
[minor(dev
)];
589 * Restrict application to use a buffer the same size as
592 if (uio
->uio_resid
!= d
->bd_bufsize
) {
593 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
599 * If the hold buffer is empty, then do a timed sleep, which
600 * ends when the timeout expires or when enough packets
601 * have arrived to fill the store buffer.
603 while (d
->bd_hbuf
== 0) {
604 if (d
->bd_immediate
&& d
->bd_slen
!= 0) {
606 * A packet(s) either arrived since the previous
607 * read or arrived while we were asleep.
608 * Rotate the buffers and return what's here.
615 * No data is available, check to see if the bpf device
616 * is still pointed at a real interface. If not, return
617 * ENXIO so that the userland process knows to rebind
618 * it before using it again.
620 if (d
->bd_bif
== NULL
) {
622 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
626 if (ioflag
& IO_NDELAY
)
629 error
= BPF_SLEEP((caddr_t
)d
, PRINET
|PCATCH
, "bpf",
631 if (error
== EINTR
|| error
== ERESTART
) {
633 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
636 if (error
== EWOULDBLOCK
) {
638 * On a timeout, return what's in the buffer,
639 * which may be nothing. If there is something
640 * in the store buffer, we can rotate the buffers.
644 * We filled up the buffer in between
645 * getting the timeout and arriving
646 * here, so we don't need to rotate.
650 if (d
->bd_slen
== 0) {
652 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
660 * At this point, we know we have something in the hold slot.
665 * Move data from hold buffer into user space.
666 * We know the entire buffer is transferred since
667 * we checked above that the read buffer is bpf_bufsize bytes.
669 error
= UIOMOVE(d
->bd_hbuf
, d
->bd_hlen
, UIO_READ
, uio
);
672 d
->bd_fbuf
= d
->bd_hbuf
;
676 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
682 * If there are processes sleeping on this descriptor, wake them up.
686 register struct bpf_d
*d
;
689 if (d
->bd_async
&& d
->bd_sig
&& d
->bd_sigio
)
690 pgsigio(d
->bd_sigio
, d
->bd_sig
, 0);
693 selwakeup(&d
->bd_sel
);
696 d
->bd_sel
.si_pid
= 0;
700 selwakeup(d
->bd_selproc
, (int)d
->bd_selcoll
);
708 bpfwrite(dev
, uio
, ioflag
)
713 register struct bpf_d
*d
;
717 static struct sockaddr dst
;
720 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
721 d
= &bpf_dtab
[minor(dev
)];
723 if (d
->bd_bif
== 0) {
724 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
728 ifp
= d
->bd_bif
->bif_ifp
;
730 if (uio
->uio_resid
== 0) {
731 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
735 error
= bpf_movein(uio
, (int)d
->bd_bif
->bif_dlt
, &m
, &dst
, &datlen
);
737 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
741 if (datlen
> ifp
->if_mtu
) {
742 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
747 dst
.sa_family
= pseudo_AF_HDRCMPLT
;
751 error
= dlil_output(ifp
->if_data
.default_proto
, m
,
752 (caddr_t
) 0, &dst
, 0);
755 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
757 * The driver frees the mbuf.
763 * Reset a descriptor by flushing its packet buffer and clearing the
764 * receive and drop counts. Should be called at splimp.
771 /* Free the hold buffer. */
772 d
->bd_fbuf
= d
->bd_hbuf
;
782 * FIONREAD Check for read packet available.
783 * SIOCGIFADDR Get interface address - convenient hook to driver.
784 * BIOCGBLEN Get buffer len [for read()].
785 * BIOCSETF Set ethernet read filter.
786 * BIOCFLUSH Flush read packet buffer.
787 * BIOCPROMISC Put interface into promiscuous mode.
788 * BIOCGDLT Get link layer type.
789 * BIOCGETIF Get interface name.
790 * BIOCSETIF Set interface.
791 * BIOCSRTIMEOUT Set read timeout.
792 * BIOCGRTIMEOUT Get read timeout.
793 * BIOCGSTATS Get packet stats.
794 * BIOCIMMEDIATE Set immediate mode.
795 * BIOCVERSION Get filter language version.
796 * BIOCGHDRCMPLT Get "header already complete" flag
797 * BIOCSHDRCMPLT Set "header already complete" flag
798 * BIOCGSEESENT Get "see packets sent" flag
799 * BIOCSSEESENT Set "see packets sent" flag
803 bpfioctl(dev
, cmd
, addr
, flags
, p
)
810 register struct bpf_d
*d
;
814 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
815 d
= &bpf_dtab
[minor(dev
)];
824 * Check for read packet available.
847 ifp
= d
->bd_bif
->bif_ifp
;
848 error
= (*ifp
->if_ioctl
)(ifp
, cmd
, addr
);
854 * Get buffer len [for read()].
857 *(u_int
*)addr
= d
->bd_bufsize
;
870 register u_int size
= *(u_int
*)addr
;
872 if (size
> bpf_maxbufsize
)
873 *(u_int
*)addr
= size
= bpf_maxbufsize
;
874 else if (size
< BPF_MINBUFSIZE
)
875 *(u_int
*)addr
= size
= BPF_MINBUFSIZE
;
876 d
->bd_bufsize
= size
;
882 * Set link layer read filter.
885 error
= bpf_setf(d
, (struct bpf_program
*)addr
);
889 * Flush read packet buffer.
898 * Put interface into promiscuous mode.
901 if (d
->bd_bif
== 0) {
903 * No interface attached yet.
909 if (d
->bd_promisc
== 0) {
910 error
= ifpromisc(d
->bd_bif
->bif_ifp
, 1);
918 * Get device parameters.
924 *(u_int
*)addr
= d
->bd_bif
->bif_dlt
;
928 * Get interface name.
934 struct ifnet
*const ifp
= d
->bd_bif
->bif_ifp
;
935 struct ifreq
*const ifr
= (struct ifreq
*)addr
;
937 snprintf(ifr
->ifr_name
, sizeof(ifr
->ifr_name
),
938 "%s%d", ifp
->if_name
, ifp
->if_unit
);
946 error
= bpf_setif(d
, (struct ifreq
*)addr
);
954 struct timeval
*tv
= (struct timeval
*)addr
;
957 * Subtract 1 tick from tvtohz() since this isn't
960 if ((error
= itimerfix(tv
)) == 0)
961 d
->bd_rtout
= tvtohz(tv
) - 1;
970 struct timeval
*tv
= (struct timeval
*)addr
;
972 tv
->tv_sec
= d
->bd_rtout
/ hz
;
973 tv
->tv_usec
= (d
->bd_rtout
% hz
) * tick
;
982 struct bpf_stat
*bs
= (struct bpf_stat
*)addr
;
984 bs
->bs_recv
= d
->bd_rcount
;
985 bs
->bs_drop
= d
->bd_dcount
;
990 * Set immediate mode.
993 d
->bd_immediate
= *(u_int
*)addr
;
998 struct bpf_version
*bv
= (struct bpf_version
*)addr
;
1000 bv
->bv_major
= BPF_MAJOR_VERSION
;
1001 bv
->bv_minor
= BPF_MINOR_VERSION
;
1006 * Get "header already complete" flag
1009 *(u_int
*)addr
= d
->bd_hdrcmplt
;
1013 * Set "header already complete" flag
1016 d
->bd_hdrcmplt
= *(u_int
*)addr
? 1 : 0;
1020 * Get "see sent packets" flag
1023 *(u_int
*)addr
= d
->bd_seesent
;
1027 * Set "see sent packets" flag
1030 d
->bd_seesent
= *(u_int
*)addr
;
1033 case FIONBIO
: /* Non-blocking I/O */
1036 case FIOASYNC
: /* Send signal on receive packets */
1037 d
->bd_async
= *(int *)addr
;
1041 error
= fsetown(*(int *)addr
, &d
->bd_sigio
);
1045 *(int *)addr
= fgetown(d
->bd_sigio
);
1048 /* This is deprecated, FIOSETOWN should be used instead. */
1050 error
= fsetown(-(*(int *)addr
), &d
->bd_sigio
);
1053 /* This is deprecated, FIOGETOWN should be used instead. */
1055 *(int *)addr
= -fgetown(d
->bd_sigio
);
1058 case BIOCSRSIG
: /* Set receive signal */
1062 sig
= *(u_int
*)addr
;
1071 *(u_int
*)addr
= d
->bd_sig
;
1074 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1079 * Set d's packet filter program to fp. If this file already has a filter,
1080 * free it and replace it. Returns EINVAL for bogus requests.
1085 struct bpf_program
*fp
;
1087 struct bpf_insn
*fcode
, *old
;
1092 if (fp
->bf_insns
== 0) {
1093 if (fp
->bf_len
!= 0)
1100 FREE((caddr_t
)old
, M_DEVBUF
);
1104 if (flen
> BPF_MAXINSNS
)
1107 size
= flen
* sizeof(*fp
->bf_insns
);
1108 fcode
= (struct bpf_insn
*) _MALLOC(size
, M_DEVBUF
, M_WAIT
);
1113 if (copyin((caddr_t
)fp
->bf_insns
, (caddr_t
)fcode
, size
) == 0 &&
1114 bpf_validate(fcode
, (int)flen
)) {
1116 d
->bd_filter
= fcode
;
1120 FREE((caddr_t
)old
, M_DEVBUF
);
1124 FREE((caddr_t
)fcode
, M_DEVBUF
);
1129 * Detach a file from its current interface (if attached at all) and attach
1130 * to the interface indicated by the name stored in ifr.
1131 * Return an errno or 0.
1140 struct ifnet
*theywant
;
1142 theywant
= ifunit(ifr
->ifr_name
);
1147 * Look through attached interfaces for the named one.
1149 for (bp
= bpf_iflist
; bp
!= 0; bp
= bp
->bif_next
) {
1150 struct ifnet
*ifp
= bp
->bif_ifp
;
1152 if (ifp
== 0 || ifp
!= theywant
)
1155 * We found the requested interface.
1156 * If it's not up, return an error.
1157 * Allocate the packet buffers if we need to.
1158 * If we're already attached to requested interface,
1159 * just flush the buffer.
1161 if ((ifp
->if_flags
& IFF_UP
) == 0)
1164 if (d
->bd_sbuf
== 0) {
1165 error
= bpf_allocbufs(d
);
1170 if (bp
!= d
->bd_bif
) {
1173 * Detach if attached to something else.
1188 * Support for select() and poll() system calls
1190 * Return true iff the specific operation will not block indefinitely.
1191 * Otherwise, return false but make a note that a selwakeup() must be done.
1194 bpfpoll(dev
, events
, wql
, p
)
1200 register struct bpf_d
*d
;
1204 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1206 * An imitation of the FIONREAD ioctl code.
1208 d
= &bpf_dtab
[minor(dev
)];
1210 if (d
->bd_bif
== NULL
) {
1211 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1216 if (events
& (POLLIN
| POLLRDNORM
)) {
1217 if (d
->bd_hlen
!= 0 || (d
->bd_immediate
&& d
->bd_slen
!= 0))
1218 revents
|= events
& (POLLIN
| POLLRDNORM
);
1220 selrecord(p
, &d
->bd_sel
, wql
);
1223 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1228 * Incoming linkage from device drivers. Process the packet pkt, of length
1229 * pktlen, which is stored in a contiguous buffer. The packet is parsed
1230 * by each process' filter, and if accepted, stashed into the corresponding
1234 bpf_tap(ifp
, pkt
, pktlen
)
1236 register u_char
*pkt
;
1237 register u_int pktlen
;
1240 register struct bpf_d
*d
;
1241 register u_int slen
;
1243 * Note that the ipl does not have to be raised at this point.
1244 * The only problem that could arise here is that if two different
1245 * interfaces shared any data. This is not the case.
1247 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1252 for (d
= bp
->bif_dlist
; d
!= 0; d
= d
->bd_next
) {
1254 slen
= bpf_filter(d
->bd_filter
, pkt
, pktlen
, pktlen
);
1256 catchpacket(d
, pkt
, pktlen
, slen
, bcopy
);
1260 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1265 * Copy data from an mbuf chain into a buffer. This code is derived
1266 * from m_copydata in sys/uipc_mbuf.c.
1269 bpf_mcopy(src_arg
, dst_arg
, len
)
1270 const void *src_arg
;
1272 register size_t len
;
1274 register const struct mbuf
*m
;
1275 register u_int count
;
1283 count
= min(m
->m_len
, len
);
1284 bcopy(mtod(m
, void *), dst
, count
);
1292 * Incoming linkage from device drivers, when packet is in an mbuf chain.
1299 struct bpf_if
*bp
= ifp
->if_bpf
;
1305 for (m0
= m
; m0
!= 0; m0
= m0
->m_next
)
1306 pktlen
+= m0
->m_len
;
1308 for (d
= bp
->bif_dlist
; d
!= 0; d
= d
->bd_next
) {
1309 if (!d
->bd_seesent
&& (m
->m_pkthdr
.rcvif
== NULL
))
1312 slen
= bpf_filter(d
->bd_filter
, (u_char
*)m
, pktlen
, 0);
1314 catchpacket(d
, (u_char
*)m
, pktlen
, slen
, bpf_mcopy
);
1319 * Move the packet data from interface memory (pkt) into the
1320 * store buffer. Return 1 if it's time to wakeup a listener (buffer full),
1321 * otherwise 0. "copy" is the routine called to do the actual data
1322 * transfer. bcopy is passed in to copy contiguous chunks, while
1323 * bpf_mcopy is passed in to copy mbuf chains. In the latter case,
1324 * pkt is really an mbuf.
1327 catchpacket(d
, pkt
, pktlen
, snaplen
, cpfn
)
1328 register struct bpf_d
*d
;
1329 register u_char
*pkt
;
1330 register u_int pktlen
, snaplen
;
1331 register void (*cpfn
) __P((const void *, void *, size_t));
1333 register struct bpf_hdr
*hp
;
1334 register int totlen
, curlen
;
1335 register int hdrlen
= d
->bd_bif
->bif_hdrlen
;
1337 * Figure out how many bytes to move. If the packet is
1338 * greater or equal to the snapshot length, transfer that
1339 * much. Otherwise, transfer the whole packet (unless
1340 * we hit the buffer size limit).
1342 totlen
= hdrlen
+ min(snaplen
, pktlen
);
1343 if (totlen
> d
->bd_bufsize
)
1344 totlen
= d
->bd_bufsize
;
1347 * Round up the end of the previous packet to the next longword.
1349 curlen
= BPF_WORDALIGN(d
->bd_slen
);
1350 if (curlen
+ totlen
> d
->bd_bufsize
) {
1352 * This packet will overflow the storage buffer.
1353 * Rotate the buffers if we can, then wakeup any
1356 if (d
->bd_fbuf
== 0) {
1358 * We haven't completed the previous read yet,
1359 * so drop the packet.
1368 else if (d
->bd_immediate
)
1370 * Immediate mode is set. A packet arrived so any
1371 * reads should be woken up.
1376 * Append the bpf header.
1378 hp
= (struct bpf_hdr
*)(d
->bd_sbuf
+ curlen
);
1380 microtime(&hp
->bh_tstamp
);
1382 uniqtime(&hp
->bh_tstamp
);
1384 hp
->bh_tstamp
= time
;
1386 hp
->bh_datalen
= pktlen
;
1387 hp
->bh_hdrlen
= hdrlen
;
1389 * Copy the packet data into the store buffer and update its length.
1391 (*cpfn
)(pkt
, (u_char
*)hp
+ hdrlen
, (hp
->bh_caplen
= totlen
- hdrlen
));
1392 d
->bd_slen
= curlen
+ totlen
;
1396 * Initialize all nonzero fields of a descriptor.
1400 register struct bpf_d
*d
;
1402 d
->bd_fbuf
= (caddr_t
) _MALLOC(d
->bd_bufsize
, M_DEVBUF
, M_WAIT
);
1403 if (d
->bd_fbuf
== 0)
1406 d
->bd_sbuf
= (caddr_t
) _MALLOC(d
->bd_bufsize
, M_DEVBUF
, M_WAIT
);
1407 if (d
->bd_sbuf
== 0) {
1408 FREE(d
->bd_fbuf
, M_DEVBUF
);
1417 * Free buffers currently in use by a descriptor.
1422 register struct bpf_d
*d
;
1425 * We don't need to lock out interrupts since this descriptor has
1426 * been detached from its interface and it yet hasn't been marked
1429 if (d
->bd_sbuf
!= 0) {
1430 FREE(d
->bd_sbuf
, M_DEVBUF
);
1431 if (d
->bd_hbuf
!= 0)
1432 FREE(d
->bd_hbuf
, M_DEVBUF
);
1433 if (d
->bd_fbuf
!= 0)
1434 FREE(d
->bd_fbuf
, M_DEVBUF
);
1437 FREE((caddr_t
)d
->bd_filter
, M_DEVBUF
);
1443 * Attach an interface to bpf. driverp is a pointer to a (struct bpf_if *)
1444 * in the driver's softc; dlt is the link layer type; hdrlen is the fixed
1445 * size of the link header (variable length headers not yet supported).
1448 bpfattach(ifp
, dlt
, hdrlen
)
1454 bp
= (struct bpf_if
*) _MALLOC(sizeof(*bp
), M_DEVBUF
, M_WAIT
);
1462 bp
->bif_next
= bpf_iflist
;
1465 bp
->bif_ifp
->if_bpf
= 0;
1468 * Compute the length of the bpf header. This is not necessarily
1469 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1470 * that the network layer header begins on a longword boundary (for
1471 * performance reasons and to alleviate alignment restrictions).
1473 bp
->bif_hdrlen
= BPF_WORDALIGN(hdrlen
+ SIZEOF_BPF_HDR
) - hdrlen
;
1477 * Mark all the descriptors free if this hasn't been done.
1479 if (!bpf_dtab_init
) {
1480 for (i
= 0; i
< nbpfilter
; ++i
)
1481 D_MARKFREE(&bpf_dtab
[i
]);
1486 printf("bpf: %s%d attached\n", ifp
->if_name
, ifp
->if_unit
);
1491 * Detach bpf from an interface. This involves detaching each descriptor
1492 * associated with the interface, and leaving bd_bif NULL. Notify each
1493 * descriptor as it's detached so that any sleepers wake up and get
1500 struct bpf_if
*bp
, *bp_prev
;
1506 /* Locate BPF interface information */
1508 for (bp
= bpf_iflist
; bp
!= NULL
; bp
= bp
->bif_next
) {
1509 if (ifp
== bp
->bif_ifp
)
1515 /* Check for no BPF interface information */
1521 /* Interface wasn't attached */
1522 if (bp
->bif_ifp
== NULL
) {
1525 printf("bpfdetach: %s%d was not attached\n", ifp
->if_name
,
1531 while ((d
= bp
->bif_dlist
) != NULL
) {
1537 bp_prev
->bif_next
= bp
->bif_next
;
1539 bpf_iflist
= bp
->bif_next
;
1547 static void *bpf_devfs_token
[NBPFILTER
];
1549 static int bpf_devsw_installed
;
1551 void bpf_init
__P((void *unused
));
1561 if (!bpf_devsw_installed
) {
1562 bpf_devsw_installed
= 1;
1563 maj
= cdevsw_add(CDEV_MAJOR
, &bpf_cdevsw
);
1565 printf("bpf_init: failed to allocate a major number!\n");
1569 for (i
= 0 ; i
< nbpfilter
; i
++) {
1570 bpf_devfs_token
[i
] = devfs_make_node(makedev(maj
, i
),
1571 DEVFS_CHAR
, UID_ROOT
, GID_WHEEL
, 0600,
1576 cdevsw_add(&bpf_cdevsw
);
1581 SYSINIT(bpfdev
,SI_SUB_DRIVERS
,SI_ORDER_MIDDLE
+CDEV_MAJOR
,bpf_drvinit
,NULL
)
1587 * NOP stubs to allow bpf-using drivers to load and function.
1589 * A 'better' implementation would allow the core bpf functionality
1590 * to be loaded at runtime.
1594 bpf_tap(ifp
, pkt
, pktlen
)
1596 register u_char
*pkt
;
1597 register u_int pktlen
;
1609 bpfattach(ifp
, dlt
, hdrlen
)
1622 bpf_filter(pc
, p
, wirelen
, buflen
)
1623 register const struct bpf_insn
*pc
;
1626 register u_int buflen
;
1628 return -1; /* "no filter" behaviour */
1630 #endif /* !defined(__APPLE__) */
1631 #endif /* NBPFILTER > 0 */