]>
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 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1990, 1991, 1993
27 * The Regents of the University of California. All rights reserved.
29 * This code is derived from the Stanford/CMU enet packet filter,
30 * (net/enet.c) distributed as part of 4.3BSD, and code contributed
31 * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
32 * Berkeley Laboratory.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * @(#)bpf.c 8.2 (Berkeley) 3/28/94
64 * $FreeBSD: src/sys/net/bpf.c,v 1.59.2.5 2001/01/05 04:49:09 jdp Exp $
72 #define inline __inline
75 #include <sys/param.h>
76 #include <sys/systm.h>
78 #include <sys/malloc.h>
82 #include <sys/signalvar.h>
83 #include <sys/filio.h>
84 #include <sys/sockio.h>
85 #include <sys/ttycom.h>
86 #include <sys/filedesc.h>
88 #if defined(sparc) && BSD < 199103
89 #include <sys/stream.h>
93 #include <sys/socket.h>
94 #include <sys/vnode.h>
98 #include <net/bpfdesc.h>
100 #include <netinet/in.h>
101 #include <netinet/if_ether.h>
102 #include <sys/kernel.h>
103 #include <sys/sysctl.h>
106 #include <miscfs/devfs/devfs.h>
107 #include <net/dlil.h>
112 * Older BSDs don't have kernel malloc.
116 static caddr_t
bpf_alloc();
117 #include <net/bpf_compat.h>
118 #define BPF_BUFSIZE (MCLBYTES-8)
119 #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, code, uio)
121 #define BPF_BUFSIZE 4096
122 #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, uio)
125 #define PRINET 26 /* interruptible */
128 * The default read buffer size is patchable.
130 static int bpf_bufsize
= BPF_BUFSIZE
;
131 SYSCTL_INT(_debug
, OID_AUTO
, bpf_bufsize
, CTLFLAG_RW
,
132 &bpf_bufsize
, 0, "");
133 static int bpf_maxbufsize
= BPF_MAXBUFSIZE
;
134 SYSCTL_INT(_debug
, OID_AUTO
, bpf_maxbufsize
, CTLFLAG_RW
,
135 &bpf_maxbufsize
, 0, "");
138 * bpf_iflist is the list of interfaces; each corresponds to an ifnet
139 * bpf_dtab holds the descriptors, indexed by minor device #
141 static struct bpf_if
*bpf_iflist
;
144 * BSD now stores the bpf_d in the dev_t which is a struct
145 * on their system. Our dev_t is an int, so we still store
146 * the bpf_d in a separate table indexed by minor device #.
148 static struct bpf_d bpf_dtab
[NBPFILTER
];
149 static int bpf_dtab_init
;
150 static int nbpfilter
= NBPFILTER
;
153 static int bpf_allocbufs
__P((struct bpf_d
*));
154 static void bpf_attachd
__P((struct bpf_d
*d
, struct bpf_if
*bp
));
155 static void bpf_detachd
__P((struct bpf_d
*d
));
156 static void bpf_freed
__P((struct bpf_d
*));
157 static void bpf_mcopy
__P((const void *, void *, size_t));
158 static int bpf_movein
__P((struct uio
*, int,
159 struct mbuf
**, struct sockaddr
*, int *));
160 static int bpf_setif
__P((struct bpf_d
*, struct ifreq
*));
162 bpf_wakeup
__P((struct bpf_d
*));
163 static void catchpacket
__P((struct bpf_d
*, u_char
*, u_int
,
164 u_int
, void (*)(const void *, void *, size_t)));
165 static void reset_d
__P((struct bpf_d
*));
166 static int bpf_setf
__P((struct bpf_d
*, struct bpf_program
*));
169 * Darwin differs from BSD here, the following are static
170 * on BSD and not static on Darwin.
177 select_fcn_t bpfpoll
;
180 void bpf_mtap(struct ifnet
*, struct mbuf
*);
182 int bpfopen(), bpfclose(), bpfread(), bpfwrite(), bpfioctl(),
186 /* Darwin's cdevsw struct differs slightly from BSDs */
187 #define CDEV_MAJOR 23
188 static struct cdevsw bpf_cdevsw
= {
190 /* close */ bpfclose
,
192 /* write */ bpfwrite
,
193 /* ioctl */ bpfioctl
,
197 /* select */ bpfpoll
,
199 /* strategy*/ eno_strat
,
207 bpf_movein(uio
, linktype
, mp
, sockp
, datlen
)
208 register struct uio
*uio
;
209 int linktype
, *datlen
;
210 register struct mbuf
**mp
;
211 register struct sockaddr
*sockp
;
219 * Build a sockaddr based on the data link layer type.
220 * We do this at this level because the ethernet header
221 * is copied directly into the data field of the sockaddr.
222 * In the case of SLIP, there is no header and the packet
223 * is forwarded as is.
224 * Also, we are careful to leave room at the front of the mbuf
225 * for the link level header.
230 sockp
->sa_family
= AF_INET
;
235 sockp
->sa_family
= AF_UNSPEC
;
236 /* XXX Would MAXLINKHDR be better? */
237 hlen
= sizeof(struct ether_header
);
241 #if defined(__FreeBSD__) || defined(__bsdi__)
242 sockp
->sa_family
= AF_IMPLINK
;
245 sockp
->sa_family
= AF_UNSPEC
;
246 /* XXX 4(FORMAC)+6(dst)+6(src)+3(LLC)+5(SNAP) */
253 sockp
->sa_family
= AF_UNSPEC
;
258 case DLT_ATM_RFC1483
:
260 * en atm driver requires 4-byte atm pseudo header.
261 * though it isn't standard, vpi:vci needs to be
264 sockp
->sa_family
= AF_UNSPEC
;
265 hlen
= 12; /* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
269 sockp
->sa_family
= AF_UNSPEC
;
270 hlen
= 4; /* This should match PPP_HDRLEN */
277 len
= uio
->uio_resid
;
278 *datlen
= len
- hlen
;
279 if ((unsigned)len
> MCLBYTES
)
282 MGETHDR(m
, M_WAIT
, MT_DATA
);
288 if ((m
->m_flags
& M_EXT
) == 0) {
291 if (m
->m_len
!= MCLBYTES
) {
297 m
->m_pkthdr
.len
= m
->m_len
= len
;
298 m
->m_pkthdr
.rcvif
= NULL
;
301 * Make room for link header.
304 m
->m_pkthdr
.len
-= hlen
;
307 m
->m_data
+= hlen
; /* XXX */
311 error
= UIOMOVE((caddr_t
)sockp
->sa_data
, hlen
, UIO_WRITE
, uio
);
315 error
= UIOMOVE(mtod(m
, caddr_t
), len
- hlen
, UIO_WRITE
, uio
);
324 /* Callback registered with Ethernet driver. */
325 int bpf_tap_callback(struct ifnet
*ifp
, struct mbuf
*m
)
327 boolean_t funnel_state
;
329 funnel_state
= thread_funnel_set(network_flock
, TRUE
);
332 * Do nothing if the BPF tap has been turned off.
333 * This is to protect from a potential race where this
334 * call blocks on the funnel lock. And in the meantime
335 * BPF is turned off, which will clear if_bpf.
340 thread_funnel_set(network_flock
, funnel_state
);
346 * Attach file to the bpf interface, i.e. make d listen on bp.
347 * Must be called at splimp.
355 * Point d at bp, and add d to the interface's list of listeners.
356 * Finally, point the driver's bpf cookie at the interface so
357 * it will divert packets to bpf.
360 d
->bd_next
= bp
->bif_dlist
;
363 bp
->bif_ifp
->if_bpf
= bp
;
366 if (bp
->bif_ifp
->if_set_bpf_tap
)
367 (*bp
->bif_ifp
->if_set_bpf_tap
)(bp
->bif_ifp
, BPF_TAP_INPUT_OUTPUT
, bpf_tap_callback
);
372 * Detach a file from its interface.
383 ifp
= d
->bd_bif
->bif_ifp
;
389 * Check if this descriptor had requested promiscuous mode.
390 * If so, turn it off.
394 if (ifpromisc(bp
->bif_ifp
, 0))
396 * Something is really wrong if we were able to put
397 * the driver into promiscuous mode, but can't
399 * Most likely the network interface is gone.
401 printf("bpf: ifpromisc failed");
403 /* Remove d from the interface's descriptor list. */
408 panic("bpf_detachd: descriptor not in list");
411 if (bp
->bif_dlist
== 0) {
413 * Let the driver know that there are no more listeners.
415 if (ifp
->if_set_bpf_tap
)
416 (*ifp
->if_set_bpf_tap
)(ifp
, BPF_TAP_DISABLE
, 0);
417 d
->bd_bif
->bif_ifp
->if_bpf
= 0;
425 * Mark a descriptor free by making it point to itself.
426 * This is probably cheaper than marking with a constant since
427 * the address should be in a register anyway.
429 #define D_ISFREE(d) ((d) == (d)->bd_next)
430 #define D_MARKFREE(d) ((d)->bd_next = (d))
431 #define D_MARKUSED(d) ((d)->bd_next = 0)
434 * Open ethernet device. Returns ENXIO for illegal minor device number,
435 * EBUSY if file is open by another process.
439 bpfopen(dev
, flags
, fmt
, p
)
445 register struct bpf_d
*d
;
448 if (minor(dev
) >= nbpfilter
)
451 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
453 d
= &bpf_dtab
[minor(dev
)];
461 * Each minor can be opened by only one process. If the requested
462 * minor is in use, return EBUSY.
466 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
470 /* Mark "free" and do most initialization. */
471 bzero((char *)d
, sizeof(*d
));
475 make_dev(&bpf_cdevsw
, minor(dev
), 0, 0, 0600, "bpf%d", lminor(dev
));
476 MALLOC(d
, struct bpf_d
*, sizeof(*d
), M_BPF
, M_WAITOK
);
477 bzero(d
, sizeof(*d
));
480 d
->bd_bufsize
= bpf_bufsize
;
483 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
488 * Close the descriptor by detaching it from its interface,
489 * deallocating its buffers, and marking it free.
493 bpfclose(dev
, flags
, fmt
, p
)
499 register struct bpf_d
*d
;
502 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
505 funsetown(d
->bd_sigio
);
509 d
= &bpf_dtab
[minor(dev
)];
515 selthreadclear(&d
->bd_sel
);
518 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
523 * Support for SunOS, which does not have tsleep.
530 boolean_t funnel_state
;
531 struct bpf_d
*d
= (struct bpf_d
*)arg
;
532 funnel_state
= thread_funnel_set(network_flock
, TRUE
);
535 (void) thread_funnel_set(network_flock
, FALSE
);
538 #define BPF_SLEEP(chan, pri, s, t) bpf_sleep((struct bpf_d *)chan)
542 register struct bpf_d
*d
;
544 register int rto
= d
->bd_rtout
;
549 timeout(bpf_timeout
, (caddr_t
)d
, rto
);
551 st
= sleep((caddr_t
)d
, PRINET
|PCATCH
);
553 if (d
->bd_timedout
== 0)
554 untimeout(bpf_timeout
, (caddr_t
)d
);
558 return (st
!= 0) ? EINTR
: 0;
561 #define BPF_SLEEP tsleep
565 * Rotate the packet buffers in descriptor d. Move the store buffer
566 * into the hold slot, and the free buffer into the store slot.
567 * Zero the length of the new store buffer.
569 #define ROTATE_BUFFERS(d) \
570 (d)->bd_hbuf = (d)->bd_sbuf; \
571 (d)->bd_hlen = (d)->bd_slen; \
572 (d)->bd_sbuf = (d)->bd_fbuf; \
576 * bpfread - read next chunk of packets from buffers
579 bpfread(dev
, uio
, ioflag
)
584 register struct bpf_d
*d
;
588 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
589 d
= &bpf_dtab
[minor(dev
)];
592 * Restrict application to use a buffer the same size as
595 if (uio
->uio_resid
!= d
->bd_bufsize
) {
596 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
602 * If the hold buffer is empty, then do a timed sleep, which
603 * ends when the timeout expires or when enough packets
604 * have arrived to fill the store buffer.
606 while (d
->bd_hbuf
== 0) {
607 if (d
->bd_immediate
&& d
->bd_slen
!= 0) {
609 * A packet(s) either arrived since the previous
610 * read or arrived while we were asleep.
611 * Rotate the buffers and return what's here.
618 * No data is available, check to see if the bpf device
619 * is still pointed at a real interface. If not, return
620 * ENXIO so that the userland process knows to rebind
621 * it before using it again.
623 if (d
->bd_bif
== NULL
) {
625 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
629 if (ioflag
& IO_NDELAY
)
632 error
= BPF_SLEEP((caddr_t
)d
, PRINET
|PCATCH
, "bpf",
634 if (error
== EINTR
|| error
== ERESTART
) {
636 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
639 if (error
== EWOULDBLOCK
) {
641 * On a timeout, return what's in the buffer,
642 * which may be nothing. If there is something
643 * in the store buffer, we can rotate the buffers.
647 * We filled up the buffer in between
648 * getting the timeout and arriving
649 * here, so we don't need to rotate.
653 if (d
->bd_slen
== 0) {
655 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
663 * At this point, we know we have something in the hold slot.
668 * Move data from hold buffer into user space.
669 * We know the entire buffer is transferred since
670 * we checked above that the read buffer is bpf_bufsize bytes.
672 error
= UIOMOVE(d
->bd_hbuf
, d
->bd_hlen
, UIO_READ
, uio
);
675 d
->bd_fbuf
= d
->bd_hbuf
;
679 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
685 * If there are processes sleeping on this descriptor, wake them up.
689 register struct bpf_d
*d
;
692 if (d
->bd_async
&& d
->bd_sig
&& d
->bd_sigio
)
693 pgsigio(d
->bd_sigio
, d
->bd_sig
, 0);
696 selwakeup(&d
->bd_sel
);
699 d
->bd_sel
.si_pid
= 0;
703 selwakeup(d
->bd_selproc
, (int)d
->bd_selcoll
);
711 bpfwrite(dev
, uio
, ioflag
)
716 register struct bpf_d
*d
;
720 static struct sockaddr dst
;
723 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
724 d
= &bpf_dtab
[minor(dev
)];
726 if (d
->bd_bif
== 0) {
727 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
731 ifp
= d
->bd_bif
->bif_ifp
;
733 if (uio
->uio_resid
== 0) {
734 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
738 error
= bpf_movein(uio
, (int)d
->bd_bif
->bif_dlt
, &m
, &dst
, &datlen
);
740 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
744 if (datlen
> ifp
->if_mtu
) {
745 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
750 dst
.sa_family
= pseudo_AF_HDRCMPLT
;
754 error
= dlil_output(ifp
->if_data
.default_proto
, m
,
755 (caddr_t
) 0, &dst
, 0);
758 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
760 * The driver frees the mbuf.
766 * Reset a descriptor by flushing its packet buffer and clearing the
767 * receive and drop counts. Should be called at splimp.
774 /* Free the hold buffer. */
775 d
->bd_fbuf
= d
->bd_hbuf
;
785 * FIONREAD Check for read packet available.
786 * SIOCGIFADDR Get interface address - convenient hook to driver.
787 * BIOCGBLEN Get buffer len [for read()].
788 * BIOCSETF Set ethernet read filter.
789 * BIOCFLUSH Flush read packet buffer.
790 * BIOCPROMISC Put interface into promiscuous mode.
791 * BIOCGDLT Get link layer type.
792 * BIOCGETIF Get interface name.
793 * BIOCSETIF Set interface.
794 * BIOCSRTIMEOUT Set read timeout.
795 * BIOCGRTIMEOUT Get read timeout.
796 * BIOCGSTATS Get packet stats.
797 * BIOCIMMEDIATE Set immediate mode.
798 * BIOCVERSION Get filter language version.
799 * BIOCGHDRCMPLT Get "header already complete" flag
800 * BIOCSHDRCMPLT Set "header already complete" flag
801 * BIOCGSEESENT Get "see packets sent" flag
802 * BIOCSSEESENT Set "see packets sent" flag
806 bpfioctl(dev
, cmd
, addr
, flags
, p
)
813 register struct bpf_d
*d
;
817 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
818 d
= &bpf_dtab
[minor(dev
)];
827 * Check for read packet available.
850 ifp
= d
->bd_bif
->bif_ifp
;
851 error
= (*ifp
->if_ioctl
)(ifp
, cmd
, addr
);
857 * Get buffer len [for read()].
860 *(u_int
*)addr
= d
->bd_bufsize
;
873 register u_int size
= *(u_int
*)addr
;
875 if (size
> bpf_maxbufsize
)
876 *(u_int
*)addr
= size
= bpf_maxbufsize
;
877 else if (size
< BPF_MINBUFSIZE
)
878 *(u_int
*)addr
= size
= BPF_MINBUFSIZE
;
879 d
->bd_bufsize
= size
;
885 * Set link layer read filter.
888 error
= bpf_setf(d
, (struct bpf_program
*)addr
);
892 * Flush read packet buffer.
901 * Put interface into promiscuous mode.
904 if (d
->bd_bif
== 0) {
906 * No interface attached yet.
912 if (d
->bd_promisc
== 0) {
913 error
= ifpromisc(d
->bd_bif
->bif_ifp
, 1);
921 * Get device parameters.
927 *(u_int
*)addr
= d
->bd_bif
->bif_dlt
;
931 * Get interface name.
937 struct ifnet
*const ifp
= d
->bd_bif
->bif_ifp
;
938 struct ifreq
*const ifr
= (struct ifreq
*)addr
;
940 snprintf(ifr
->ifr_name
, sizeof(ifr
->ifr_name
),
941 "%s%d", ifp
->if_name
, ifp
->if_unit
);
949 error
= bpf_setif(d
, (struct ifreq
*)addr
);
957 struct timeval
*tv
= (struct timeval
*)addr
;
960 * Subtract 1 tick from tvtohz() since this isn't
963 if ((error
= itimerfix(tv
)) == 0)
964 d
->bd_rtout
= tvtohz(tv
) - 1;
973 struct timeval
*tv
= (struct timeval
*)addr
;
975 tv
->tv_sec
= d
->bd_rtout
/ hz
;
976 tv
->tv_usec
= (d
->bd_rtout
% hz
) * tick
;
985 struct bpf_stat
*bs
= (struct bpf_stat
*)addr
;
987 bs
->bs_recv
= d
->bd_rcount
;
988 bs
->bs_drop
= d
->bd_dcount
;
993 * Set immediate mode.
996 d
->bd_immediate
= *(u_int
*)addr
;
1001 struct bpf_version
*bv
= (struct bpf_version
*)addr
;
1003 bv
->bv_major
= BPF_MAJOR_VERSION
;
1004 bv
->bv_minor
= BPF_MINOR_VERSION
;
1009 * Get "header already complete" flag
1012 *(u_int
*)addr
= d
->bd_hdrcmplt
;
1016 * Set "header already complete" flag
1019 d
->bd_hdrcmplt
= *(u_int
*)addr
? 1 : 0;
1023 * Get "see sent packets" flag
1026 *(u_int
*)addr
= d
->bd_seesent
;
1030 * Set "see sent packets" flag
1033 d
->bd_seesent
= *(u_int
*)addr
;
1036 case FIONBIO
: /* Non-blocking I/O */
1039 case FIOASYNC
: /* Send signal on receive packets */
1040 d
->bd_async
= *(int *)addr
;
1044 error
= fsetown(*(int *)addr
, &d
->bd_sigio
);
1048 *(int *)addr
= fgetown(d
->bd_sigio
);
1051 /* This is deprecated, FIOSETOWN should be used instead. */
1053 error
= fsetown(-(*(int *)addr
), &d
->bd_sigio
);
1056 /* This is deprecated, FIOGETOWN should be used instead. */
1058 *(int *)addr
= -fgetown(d
->bd_sigio
);
1061 case BIOCSRSIG
: /* Set receive signal */
1065 sig
= *(u_int
*)addr
;
1074 *(u_int
*)addr
= d
->bd_sig
;
1077 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1082 * Set d's packet filter program to fp. If this file already has a filter,
1083 * free it and replace it. Returns EINVAL for bogus requests.
1088 struct bpf_program
*fp
;
1090 struct bpf_insn
*fcode
, *old
;
1095 if (fp
->bf_insns
== 0) {
1096 if (fp
->bf_len
!= 0)
1103 FREE((caddr_t
)old
, M_DEVBUF
);
1107 if (flen
> BPF_MAXINSNS
)
1110 size
= flen
* sizeof(*fp
->bf_insns
);
1111 fcode
= (struct bpf_insn
*) _MALLOC(size
, M_DEVBUF
, M_WAIT
);
1116 if (copyin((caddr_t
)fp
->bf_insns
, (caddr_t
)fcode
, size
) == 0 &&
1117 bpf_validate(fcode
, (int)flen
)) {
1119 d
->bd_filter
= fcode
;
1123 FREE((caddr_t
)old
, M_DEVBUF
);
1127 FREE((caddr_t
)fcode
, M_DEVBUF
);
1132 * Detach a file from its current interface (if attached at all) and attach
1133 * to the interface indicated by the name stored in ifr.
1134 * Return an errno or 0.
1143 struct ifnet
*theywant
;
1145 theywant
= ifunit(ifr
->ifr_name
);
1150 * Look through attached interfaces for the named one.
1152 for (bp
= bpf_iflist
; bp
!= 0; bp
= bp
->bif_next
) {
1153 struct ifnet
*ifp
= bp
->bif_ifp
;
1155 if (ifp
== 0 || ifp
!= theywant
)
1158 * We found the requested interface.
1159 * If it's not up, return an error.
1160 * Allocate the packet buffers if we need to.
1161 * If we're already attached to requested interface,
1162 * just flush the buffer.
1164 if ((ifp
->if_flags
& IFF_UP
) == 0)
1167 if (d
->bd_sbuf
== 0) {
1168 error
= bpf_allocbufs(d
);
1173 if (bp
!= d
->bd_bif
) {
1176 * Detach if attached to something else.
1191 * Support for select() and poll() system calls
1193 * Return true iff the specific operation will not block indefinitely.
1194 * Otherwise, return false but make a note that a selwakeup() must be done.
1197 bpfpoll(dev
, events
, wql
, p
)
1203 register struct bpf_d
*d
;
1207 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1209 * An imitation of the FIONREAD ioctl code.
1211 d
= &bpf_dtab
[minor(dev
)];
1213 if (d
->bd_bif
== NULL
) {
1214 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1219 if (events
& (POLLIN
| POLLRDNORM
)) {
1220 if (d
->bd_hlen
!= 0 || (d
->bd_immediate
&& d
->bd_slen
!= 0))
1221 revents
|= events
& (POLLIN
| POLLRDNORM
);
1223 selrecord(p
, &d
->bd_sel
, wql
);
1226 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1231 * Incoming linkage from device drivers. Process the packet pkt, of length
1232 * pktlen, which is stored in a contiguous buffer. The packet is parsed
1233 * by each process' filter, and if accepted, stashed into the corresponding
1237 bpf_tap(ifp
, pkt
, pktlen
)
1239 register u_char
*pkt
;
1240 register u_int pktlen
;
1243 register struct bpf_d
*d
;
1244 register u_int slen
;
1246 * Note that the ipl does not have to be raised at this point.
1247 * The only problem that could arise here is that if two different
1248 * interfaces shared any data. This is not the case.
1250 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1255 for (d
= bp
->bif_dlist
; d
!= 0; d
= d
->bd_next
) {
1257 slen
= bpf_filter(d
->bd_filter
, pkt
, pktlen
, pktlen
);
1259 catchpacket(d
, pkt
, pktlen
, slen
, bcopy
);
1263 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1268 * Copy data from an mbuf chain into a buffer. This code is derived
1269 * from m_copydata in sys/uipc_mbuf.c.
1272 bpf_mcopy(src_arg
, dst_arg
, len
)
1273 const void *src_arg
;
1275 register size_t len
;
1277 register const struct mbuf
*m
;
1278 register u_int count
;
1286 count
= min(m
->m_len
, len
);
1287 bcopy(mtod(m
, void *), dst
, count
);
1295 * Incoming linkage from device drivers, when packet is in an mbuf chain.
1302 struct bpf_if
*bp
= ifp
->if_bpf
;
1308 for (m0
= m
; m0
!= 0; m0
= m0
->m_next
)
1309 pktlen
+= m0
->m_len
;
1311 for (d
= bp
->bif_dlist
; d
!= 0; d
= d
->bd_next
) {
1312 if (!d
->bd_seesent
&& (m
->m_pkthdr
.rcvif
== NULL
))
1315 slen
= bpf_filter(d
->bd_filter
, (u_char
*)m
, pktlen
, 0);
1317 catchpacket(d
, (u_char
*)m
, pktlen
, slen
, bpf_mcopy
);
1322 * Move the packet data from interface memory (pkt) into the
1323 * store buffer. Return 1 if it's time to wakeup a listener (buffer full),
1324 * otherwise 0. "copy" is the routine called to do the actual data
1325 * transfer. bcopy is passed in to copy contiguous chunks, while
1326 * bpf_mcopy is passed in to copy mbuf chains. In the latter case,
1327 * pkt is really an mbuf.
1330 catchpacket(d
, pkt
, pktlen
, snaplen
, cpfn
)
1331 register struct bpf_d
*d
;
1332 register u_char
*pkt
;
1333 register u_int pktlen
, snaplen
;
1334 register void (*cpfn
) __P((const void *, void *, size_t));
1336 register struct bpf_hdr
*hp
;
1337 register int totlen
, curlen
;
1338 register int hdrlen
= d
->bd_bif
->bif_hdrlen
;
1340 * Figure out how many bytes to move. If the packet is
1341 * greater or equal to the snapshot length, transfer that
1342 * much. Otherwise, transfer the whole packet (unless
1343 * we hit the buffer size limit).
1345 totlen
= hdrlen
+ min(snaplen
, pktlen
);
1346 if (totlen
> d
->bd_bufsize
)
1347 totlen
= d
->bd_bufsize
;
1350 * Round up the end of the previous packet to the next longword.
1352 curlen
= BPF_WORDALIGN(d
->bd_slen
);
1353 if (curlen
+ totlen
> d
->bd_bufsize
) {
1355 * This packet will overflow the storage buffer.
1356 * Rotate the buffers if we can, then wakeup any
1359 if (d
->bd_fbuf
== 0) {
1361 * We haven't completed the previous read yet,
1362 * so drop the packet.
1371 else if (d
->bd_immediate
)
1373 * Immediate mode is set. A packet arrived so any
1374 * reads should be woken up.
1379 * Append the bpf header.
1381 hp
= (struct bpf_hdr
*)(d
->bd_sbuf
+ curlen
);
1383 microtime(&hp
->bh_tstamp
);
1385 uniqtime(&hp
->bh_tstamp
);
1387 hp
->bh_tstamp
= time
;
1389 hp
->bh_datalen
= pktlen
;
1390 hp
->bh_hdrlen
= hdrlen
;
1392 * Copy the packet data into the store buffer and update its length.
1394 (*cpfn
)(pkt
, (u_char
*)hp
+ hdrlen
, (hp
->bh_caplen
= totlen
- hdrlen
));
1395 d
->bd_slen
= curlen
+ totlen
;
1399 * Initialize all nonzero fields of a descriptor.
1403 register struct bpf_d
*d
;
1405 d
->bd_fbuf
= (caddr_t
) _MALLOC(d
->bd_bufsize
, M_DEVBUF
, M_WAIT
);
1406 if (d
->bd_fbuf
== 0)
1409 d
->bd_sbuf
= (caddr_t
) _MALLOC(d
->bd_bufsize
, M_DEVBUF
, M_WAIT
);
1410 if (d
->bd_sbuf
== 0) {
1411 FREE(d
->bd_fbuf
, M_DEVBUF
);
1420 * Free buffers currently in use by a descriptor.
1425 register struct bpf_d
*d
;
1428 * We don't need to lock out interrupts since this descriptor has
1429 * been detached from its interface and it yet hasn't been marked
1432 if (d
->bd_sbuf
!= 0) {
1433 FREE(d
->bd_sbuf
, M_DEVBUF
);
1434 if (d
->bd_hbuf
!= 0)
1435 FREE(d
->bd_hbuf
, M_DEVBUF
);
1436 if (d
->bd_fbuf
!= 0)
1437 FREE(d
->bd_fbuf
, M_DEVBUF
);
1440 FREE((caddr_t
)d
->bd_filter
, M_DEVBUF
);
1446 * Attach an interface to bpf. driverp is a pointer to a (struct bpf_if *)
1447 * in the driver's softc; dlt is the link layer type; hdrlen is the fixed
1448 * size of the link header (variable length headers not yet supported).
1451 bpfattach(ifp
, dlt
, hdrlen
)
1457 bp
= (struct bpf_if
*) _MALLOC(sizeof(*bp
), M_DEVBUF
, M_WAIT
);
1465 bp
->bif_next
= bpf_iflist
;
1468 bp
->bif_ifp
->if_bpf
= 0;
1471 * Compute the length of the bpf header. This is not necessarily
1472 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1473 * that the network layer header begins on a longword boundary (for
1474 * performance reasons and to alleviate alignment restrictions).
1476 bp
->bif_hdrlen
= BPF_WORDALIGN(hdrlen
+ SIZEOF_BPF_HDR
) - hdrlen
;
1480 * Mark all the descriptors free if this hasn't been done.
1482 if (!bpf_dtab_init
) {
1483 for (i
= 0; i
< nbpfilter
; ++i
)
1484 D_MARKFREE(&bpf_dtab
[i
]);
1489 printf("bpf: %s%d attached\n", ifp
->if_name
, ifp
->if_unit
);
1494 * Detach bpf from an interface. This involves detaching each descriptor
1495 * associated with the interface, and leaving bd_bif NULL. Notify each
1496 * descriptor as it's detached so that any sleepers wake up and get
1503 struct bpf_if
*bp
, *bp_prev
;
1509 /* Locate BPF interface information */
1511 for (bp
= bpf_iflist
; bp
!= NULL
; bp
= bp
->bif_next
) {
1512 if (ifp
== bp
->bif_ifp
)
1518 /* Check for no BPF interface information */
1524 /* Interface wasn't attached */
1525 if (bp
->bif_ifp
== NULL
) {
1528 printf("bpfdetach: %s%d was not attached\n", ifp
->if_name
,
1534 while ((d
= bp
->bif_dlist
) != NULL
) {
1540 bp_prev
->bif_next
= bp
->bif_next
;
1542 bpf_iflist
= bp
->bif_next
;
1550 static void *bpf_devfs_token
[NBPFILTER
];
1552 static int bpf_devsw_installed
;
1554 void bpf_init
__P((void *unused
));
1564 if (!bpf_devsw_installed
) {
1565 bpf_devsw_installed
= 1;
1566 maj
= cdevsw_add(CDEV_MAJOR
, &bpf_cdevsw
);
1568 printf("bpf_init: failed to allocate a major number!\n");
1572 for (i
= 0 ; i
< nbpfilter
; i
++) {
1573 bpf_devfs_token
[i
] = devfs_make_node(makedev(maj
, i
),
1574 DEVFS_CHAR
, UID_ROOT
, GID_WHEEL
, 0600,
1579 cdevsw_add(&bpf_cdevsw
);
1584 SYSINIT(bpfdev
,SI_SUB_DRIVERS
,SI_ORDER_MIDDLE
+CDEV_MAJOR
,bpf_drvinit
,NULL
)
1590 * NOP stubs to allow bpf-using drivers to load and function.
1592 * A 'better' implementation would allow the core bpf functionality
1593 * to be loaded at runtime.
1597 bpf_tap(ifp
, pkt
, pktlen
)
1599 register u_char
*pkt
;
1600 register u_int pktlen
;
1612 bpfattach(ifp
, dlt
, hdrlen
)
1625 bpf_filter(pc
, p
, wirelen
, buflen
)
1626 register const struct bpf_insn
*pc
;
1629 register u_int buflen
;
1631 return -1; /* "no filter" behaviour */
1633 #endif /* !defined(__APPLE__) */
1634 #endif /* NBPFILTER > 0 */