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>
101 #include <net/firewire.h>
103 #include <machine/ansi.h>
104 #include <miscfs/devfs/devfs.h>
105 #include <net/dlil.h>
110 * Older BSDs don't have kernel malloc.
114 static caddr_t
bpf_alloc();
115 #include <net/bpf_compat.h>
116 #define BPF_BUFSIZE (MCLBYTES-8)
117 #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, code, uio)
119 #define BPF_BUFSIZE 4096
120 #define UIOMOVE(cp, len, code, uio) uiomove(cp, len, uio)
124 #define PRINET 26 /* interruptible */
127 * The default read buffer size is patchable.
129 static int bpf_bufsize
= BPF_BUFSIZE
;
130 SYSCTL_INT(_debug
, OID_AUTO
, bpf_bufsize
, CTLFLAG_RW
,
131 &bpf_bufsize
, 0, "");
132 static int bpf_maxbufsize
= BPF_MAXBUFSIZE
;
133 SYSCTL_INT(_debug
, OID_AUTO
, bpf_maxbufsize
, CTLFLAG_RW
,
134 &bpf_maxbufsize
, 0, "");
137 * bpf_iflist is the list of interfaces; each corresponds to an ifnet
138 * bpf_dtab holds pointer to the descriptors, indexed by minor device #
140 static struct bpf_if
*bpf_iflist
;
143 * BSD now stores the bpf_d in the dev_t which is a struct
144 * on their system. Our dev_t is an int, so we still store
145 * the bpf_d in a separate table indexed by minor device #.
147 static struct bpf_d
**bpf_dtab
= NULL
;
148 static int bpf_dtab_size
= 0;
149 static int nbpfilter
= 0;
152 * Mark a descriptor free by making it point to itself.
153 * This is probably cheaper than marking with a constant since
154 * the address should be in a register anyway.
156 #define D_ISFREE(d) ((d) == (d)->bd_next)
157 #define D_MARKFREE(d) ((d)->bd_next = (d))
158 #define D_MARKUSED(d) ((d)->bd_next = 0)
159 #endif /* __APPLE__ */
161 static int bpf_allocbufs
__P((struct bpf_d
*));
162 static void bpf_attachd
__P((struct bpf_d
*d
, struct bpf_if
*bp
));
163 static void bpf_detachd
__P((struct bpf_d
*d
));
164 static void bpf_freed
__P((struct bpf_d
*));
165 static void bpf_mcopy
__P((const void *, void *, size_t));
166 static int bpf_movein
__P((struct uio
*, int,
167 struct mbuf
**, struct sockaddr
*, int *));
168 static int bpf_setif
__P((struct bpf_d
*, struct ifreq
*));
170 bpf_wakeup
__P((struct bpf_d
*));
171 static void catchpacket
__P((struct bpf_d
*, u_char
*, u_int
,
172 u_int
, void (*)(const void *, void *, size_t)));
173 static void reset_d
__P((struct bpf_d
*));
174 static int bpf_setf
__P((struct bpf_d
*, struct bpf_program
*));
176 /*static void *bpf_devfs_token[MAXBPFILTER];*/
178 static int bpf_devsw_installed
;
180 void bpf_init
__P((void *unused
));
184 * Darwin differs from BSD here, the following are static
185 * on BSD and not static on Darwin.
192 select_fcn_t bpfpoll
;
195 void bpf_mtap(struct ifnet
*, struct mbuf
*);
197 int bpfopen(), bpfclose(), bpfread(), bpfwrite(), bpfioctl(),
201 /* Darwin's cdevsw struct differs slightly from BSDs */
202 #define CDEV_MAJOR 23
203 static struct cdevsw bpf_cdevsw
= {
205 /* close */ bpfclose
,
207 /* write */ bpfwrite
,
208 /* ioctl */ bpfioctl
,
212 /* select */ bpfpoll
,
214 /* strategy*/ eno_strat
,
220 #define SOCKADDR_HDR_LEN offsetof(struct sockaddr, sa_data)
223 bpf_movein(uio
, linktype
, mp
, sockp
, datlen
)
224 register struct uio
*uio
;
225 int linktype
, *datlen
;
226 register struct mbuf
**mp
;
227 register struct sockaddr
*sockp
;
235 * Build a sockaddr based on the data link layer type.
236 * We do this at this level because the ethernet header
237 * is copied directly into the data field of the sockaddr.
238 * In the case of SLIP, there is no header and the packet
239 * is forwarded as is.
240 * Also, we are careful to leave room at the front of the mbuf
241 * for the link level header.
246 sockp
->sa_family
= AF_INET
;
251 sockp
->sa_family
= AF_UNSPEC
;
252 /* XXX Would MAXLINKHDR be better? */
253 hlen
= sizeof(struct ether_header
);
257 #if defined(__FreeBSD__) || defined(__bsdi__)
258 sockp
->sa_family
= AF_IMPLINK
;
261 sockp
->sa_family
= AF_UNSPEC
;
262 /* XXX 4(FORMAC)+6(dst)+6(src)+3(LLC)+5(SNAP) */
269 sockp
->sa_family
= AF_UNSPEC
;
274 case DLT_ATM_RFC1483
:
276 * en atm driver requires 4-byte atm pseudo header.
277 * though it isn't standard, vpi:vci needs to be
280 sockp
->sa_family
= AF_UNSPEC
;
281 hlen
= 12; /* XXX 4(ATM_PH) + 3(LLC) + 5(SNAP) */
285 sockp
->sa_family
= AF_UNSPEC
;
286 hlen
= 4; /* This should match PPP_HDRLEN */
289 case DLT_APPLE_IP_OVER_IEEE1394
:
290 sockp
->sa_family
= AF_UNSPEC
;
291 hlen
= sizeof(struct firewire_header
);
297 if ((hlen
+ SOCKADDR_HDR_LEN
) > sockp
->sa_len
) {
300 len
= uio
->uio_resid
;
301 *datlen
= len
- hlen
;
302 if ((unsigned)len
> MCLBYTES
)
305 MGETHDR(m
, M_WAIT
, MT_DATA
);
311 if ((m
->m_flags
& M_EXT
) == 0) {
314 if (m
->m_len
!= MCLBYTES
) {
320 m
->m_pkthdr
.len
= m
->m_len
= len
;
321 m
->m_pkthdr
.rcvif
= NULL
;
324 * Make room for link header.
327 m
->m_pkthdr
.len
-= hlen
;
330 m
->m_data
+= hlen
; /* XXX */
334 error
= UIOMOVE((caddr_t
)sockp
->sa_data
, hlen
, UIO_WRITE
, uio
);
338 error
= UIOMOVE(mtod(m
, caddr_t
), len
- hlen
, UIO_WRITE
, uio
);
347 /* Callback registered with Ethernet driver. */
348 int bpf_tap_callback(struct ifnet
*ifp
, struct mbuf
*m
)
350 boolean_t funnel_state
;
352 funnel_state
= thread_funnel_set(network_flock
, TRUE
);
355 * Do nothing if the BPF tap has been turned off.
356 * This is to protect from a potential race where this
357 * call blocks on the funnel lock. And in the meantime
358 * BPF is turned off, which will clear if_bpf.
363 thread_funnel_set(network_flock
, funnel_state
);
368 * Returns 1 on sucess, 0 on failure
371 bpf_dtab_grow(int increment
)
373 struct bpf_d
**new_dtab
= NULL
;
375 new_dtab
= (struct bpf_d
**)_MALLOC(sizeof(struct bpf_d
*) * (bpf_dtab_size
+ increment
), M_DEVBUF
, M_WAIT
);
376 if (new_dtab
== NULL
)
380 struct bpf_d
**old_dtab
;
382 bcopy(bpf_dtab
, new_dtab
, sizeof(struct bpf_d
*) * bpf_dtab_size
);
384 * replace must be atomic with respect to free do bpf_dtab
389 _FREE(old_dtab
, M_DEVBUF
);
391 else bpf_dtab
= new_dtab
;
393 bzero(bpf_dtab
+ bpf_dtab_size
, sizeof(struct bpf_d
*) * increment
);
395 bpf_dtab_size
+= increment
;
400 static struct bpf_d
*
401 bpf_make_dev_t(int maj
)
405 if (nbpfilter
>= bpf_dtab_size
&& bpf_dtab_grow(NBPFILTER
) == 0)
408 d
= (struct bpf_d
*)_MALLOC(sizeof(struct bpf_d
), M_DEVBUF
, M_WAIT
);
412 bzero(d
, sizeof(struct bpf_d
));
414 D_MARKFREE(bpf_dtab
[i
]);
415 /*bpf_devfs_token[i] = */devfs_make_node(makedev(maj
, i
),
416 DEVFS_CHAR
, UID_ROOT
, GID_WHEEL
, 0600,
425 * Attach file to the bpf interface, i.e. make d listen on bp.
426 * Must be called at splimp.
434 * Point d at bp, and add d to the interface's list of listeners.
435 * Finally, point the driver's bpf cookie at the interface so
436 * it will divert packets to bpf.
439 d
->bd_next
= bp
->bif_dlist
;
442 bp
->bif_ifp
->if_bpf
= bp
;
445 if (bp
->bif_ifp
->if_set_bpf_tap
)
446 (*bp
->bif_ifp
->if_set_bpf_tap
)(bp
->bif_ifp
, BPF_TAP_INPUT_OUTPUT
, bpf_tap_callback
);
451 * Detach a file from its interface.
462 ifp
= d
->bd_bif
->bif_ifp
;
468 * Check if this descriptor had requested promiscuous mode.
469 * If so, turn it off.
473 if (ifpromisc(bp
->bif_ifp
, 0))
475 * Something is really wrong if we were able to put
476 * the driver into promiscuous mode, but can't
478 * Most likely the network interface is gone.
480 printf("bpf: ifpromisc failed");
482 /* Remove d from the interface's descriptor list. */
487 panic("bpf_detachd: descriptor not in list");
490 if (bp
->bif_dlist
== 0) {
492 * Let the driver know that there are no more listeners.
494 if (ifp
->if_set_bpf_tap
)
495 (*ifp
->if_set_bpf_tap
)(ifp
, BPF_TAP_DISABLE
, 0);
496 d
->bd_bif
->bif_ifp
->if_bpf
= 0;
503 * Open ethernet device. Returns ENXIO for illegal minor device number,
504 * EBUSY if file is open by another process.
508 bpfopen(dev
, flags
, fmt
, p
)
514 register struct bpf_d
*d
;
517 /* new device nodes on demand when opening the last one */
518 if (minor(dev
) == nbpfilter
- 1)
519 bpf_make_dev_t(major(dev
));
521 if (minor(dev
) >= nbpfilter
)
524 d
= bpf_dtab
[minor(dev
)];
526 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
534 * Each minor can be opened by only one process. If the requested
535 * minor is in use, return EBUSY.
539 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
543 /* Mark "free" and do most initialization. */
544 bzero((char *)d
, sizeof(*d
));
548 make_dev(&bpf_cdevsw
, minor(dev
), 0, 0, 0600, "bpf%d", lminor(dev
));
549 MALLOC(d
, struct bpf_d
*, sizeof(*d
), M_BPF
, M_WAITOK
);
550 bzero(d
, sizeof(*d
));
553 d
->bd_bufsize
= bpf_bufsize
;
558 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
565 * Close the descriptor by detaching it from its interface,
566 * deallocating its buffers, and marking it free.
570 bpfclose(dev
, flags
, fmt
, p
)
576 register struct bpf_d
*d
;
579 struct bpf_d
**bpf_dtab_schk
;
583 funsetown(d
->bd_sigio
);
588 d
= bpf_dtab
[minor(dev
)];
589 bpf_dtab_schk
= bpf_dtab
;
591 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
595 * If someone grows bpf_dtab[] while we were waiting for the
596 * funnel, then we will be pointing off into freed memory;
597 * check to see if this is the case.
599 if (bpf_dtab_schk
!= bpf_dtab
) {
600 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
609 selthreadclear(&d
->bd_sel
);
612 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
617 * Support for SunOS, which does not have tsleep.
624 boolean_t funnel_state
;
625 struct bpf_d
*d
= (struct bpf_d
*)arg
;
626 funnel_state
= thread_funnel_set(network_flock
, TRUE
);
629 (void) thread_funnel_set(network_flock
, FALSE
);
632 #define BPF_SLEEP(chan, pri, s, t) bpf_sleep((struct bpf_d *)chan)
636 register struct bpf_d
*d
;
638 register int rto
= d
->bd_rtout
;
643 timeout(bpf_timeout
, (caddr_t
)d
, rto
);
645 st
= sleep((caddr_t
)d
, PRINET
|PCATCH
);
647 if (d
->bd_timedout
== 0)
648 untimeout(bpf_timeout
, (caddr_t
)d
);
652 return (st
!= 0) ? EINTR
: 0;
655 #define BPF_SLEEP tsleep
659 * Rotate the packet buffers in descriptor d. Move the store buffer
660 * into the hold slot, and the free buffer into the store slot.
661 * Zero the length of the new store buffer.
663 #define ROTATE_BUFFERS(d) \
664 (d)->bd_hbuf = (d)->bd_sbuf; \
665 (d)->bd_hlen = (d)->bd_slen; \
666 (d)->bd_sbuf = (d)->bd_fbuf; \
670 * bpfread - read next chunk of packets from buffers
673 bpfread(dev
, uio
, ioflag
)
678 register struct bpf_d
*d
;
682 d
= bpf_dtab
[minor(dev
)];
684 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
687 * Restrict application to use a buffer the same size as
690 if (uio
->uio_resid
!= d
->bd_bufsize
) {
691 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
697 * If the hold buffer is empty, then do a timed sleep, which
698 * ends when the timeout expires or when enough packets
699 * have arrived to fill the store buffer.
701 while (d
->bd_hbuf
== 0) {
702 if (d
->bd_immediate
&& d
->bd_slen
!= 0) {
704 * A packet(s) either arrived since the previous
705 * read or arrived while we were asleep.
706 * Rotate the buffers and return what's here.
713 * No data is available, check to see if the bpf device
714 * is still pointed at a real interface. If not, return
715 * ENXIO so that the userland process knows to rebind
716 * it before using it again.
718 if (d
->bd_bif
== NULL
) {
720 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
724 if (ioflag
& IO_NDELAY
)
727 error
= BPF_SLEEP((caddr_t
)d
, PRINET
|PCATCH
, "bpf",
729 if (error
== EINTR
|| error
== ERESTART
) {
731 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
734 if (error
== EWOULDBLOCK
) {
736 * On a timeout, return what's in the buffer,
737 * which may be nothing. If there is something
738 * in the store buffer, we can rotate the buffers.
742 * We filled up the buffer in between
743 * getting the timeout and arriving
744 * here, so we don't need to rotate.
748 if (d
->bd_slen
== 0) {
750 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
758 * At this point, we know we have something in the hold slot.
763 * Move data from hold buffer into user space.
764 * We know the entire buffer is transferred since
765 * we checked above that the read buffer is bpf_bufsize bytes.
767 error
= UIOMOVE(d
->bd_hbuf
, d
->bd_hlen
, UIO_READ
, uio
);
770 d
->bd_fbuf
= d
->bd_hbuf
;
774 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
780 * If there are processes sleeping on this descriptor, wake them up.
784 register struct bpf_d
*d
;
787 if (d
->bd_async
&& d
->bd_sig
&& d
->bd_sigio
)
788 pgsigio(d
->bd_sigio
, d
->bd_sig
, 0);
791 selwakeup(&d
->bd_sel
);
794 d
->bd_sel
.si_pid
= 0;
798 selwakeup(d
->bd_selproc
, (int)d
->bd_selcoll
);
805 /* keep in sync with bpf_movein above: */
806 #define MAX_DATALINK_HDR_LEN (sizeof(struct firewire_header))
809 bpfwrite(dev
, uio
, ioflag
)
814 register struct bpf_d
*d
;
818 char dst_buf
[SOCKADDR_HDR_LEN
+ MAX_DATALINK_HDR_LEN
];
821 d
= bpf_dtab
[minor(dev
)];
823 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
825 if (d
->bd_bif
== 0) {
826 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
830 ifp
= d
->bd_bif
->bif_ifp
;
832 if (uio
->uio_resid
== 0) {
833 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
836 ((struct sockaddr
*)dst_buf
)->sa_len
= sizeof(dst_buf
);
837 error
= bpf_movein(uio
, (int)d
->bd_bif
->bif_dlt
, &m
,
838 (struct sockaddr
*)dst_buf
, &datlen
);
840 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
844 if (datlen
> ifp
->if_mtu
) {
845 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
849 if (d
->bd_hdrcmplt
) {
850 ((struct sockaddr
*)dst_buf
)->sa_family
= pseudo_AF_HDRCMPLT
;
855 error
= dlil_output(ifptodlt(ifp
, PF_INET
), m
,
856 (caddr_t
) 0, (struct sockaddr
*)dst_buf
, 0);
859 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
861 * The driver frees the mbuf.
867 * Reset a descriptor by flushing its packet buffer and clearing the
868 * receive and drop counts. Should be called at splimp.
875 /* Free the hold buffer. */
876 d
->bd_fbuf
= d
->bd_hbuf
;
886 * FIONREAD Check for read packet available.
887 * SIOCGIFADDR Get interface address - convenient hook to driver.
888 * BIOCGBLEN Get buffer len [for read()].
889 * BIOCSETF Set ethernet read filter.
890 * BIOCFLUSH Flush read packet buffer.
891 * BIOCPROMISC Put interface into promiscuous mode.
892 * BIOCGDLT Get link layer type.
893 * BIOCGETIF Get interface name.
894 * BIOCSETIF Set interface.
895 * BIOCSRTIMEOUT Set read timeout.
896 * BIOCGRTIMEOUT Get read timeout.
897 * BIOCGSTATS Get packet stats.
898 * BIOCIMMEDIATE Set immediate mode.
899 * BIOCVERSION Get filter language version.
900 * BIOCGHDRCMPLT Get "header already complete" flag
901 * BIOCSHDRCMPLT Set "header already complete" flag
902 * BIOCGSEESENT Get "see packets sent" flag
903 * BIOCSSEESENT Set "see packets sent" flag
907 bpfioctl(dev
, cmd
, addr
, flags
, p
)
914 register struct bpf_d
*d
;
917 d
= bpf_dtab
[minor(dev
)];
919 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
928 * Check for read packet available.
951 ifp
= d
->bd_bif
->bif_ifp
;
952 error
= (*ifp
->if_ioctl
)(ifp
, cmd
, addr
);
958 * Get buffer len [for read()].
961 *(u_int
*)addr
= d
->bd_bufsize
;
974 register u_int size
= *(u_int
*)addr
;
976 if (size
> bpf_maxbufsize
)
977 *(u_int
*)addr
= size
= bpf_maxbufsize
;
978 else if (size
< BPF_MINBUFSIZE
)
979 *(u_int
*)addr
= size
= BPF_MINBUFSIZE
;
980 d
->bd_bufsize
= size
;
986 * Set link layer read filter.
989 error
= bpf_setf(d
, (struct bpf_program
*)addr
);
993 * Flush read packet buffer.
1002 * Put interface into promiscuous mode.
1005 if (d
->bd_bif
== 0) {
1007 * No interface attached yet.
1013 if (d
->bd_promisc
== 0) {
1014 error
= ifpromisc(d
->bd_bif
->bif_ifp
, 1);
1022 * Get device parameters.
1028 *(u_int
*)addr
= d
->bd_bif
->bif_dlt
;
1032 * Get interface name.
1038 struct ifnet
*const ifp
= d
->bd_bif
->bif_ifp
;
1039 struct ifreq
*const ifr
= (struct ifreq
*)addr
;
1041 snprintf(ifr
->ifr_name
, sizeof(ifr
->ifr_name
),
1042 "%s%d", ifp
->if_name
, ifp
->if_unit
);
1050 error
= bpf_setif(d
, (struct ifreq
*)addr
);
1058 struct timeval
*tv
= (struct timeval
*)addr
;
1061 * Subtract 1 tick from tvtohz() since this isn't
1064 if ((error
= itimerfix(tv
)) == 0)
1065 d
->bd_rtout
= tvtohz(tv
) - 1;
1074 struct timeval
*tv
= (struct timeval
*)addr
;
1076 tv
->tv_sec
= d
->bd_rtout
/ hz
;
1077 tv
->tv_usec
= (d
->bd_rtout
% hz
) * tick
;
1086 struct bpf_stat
*bs
= (struct bpf_stat
*)addr
;
1088 bs
->bs_recv
= d
->bd_rcount
;
1089 bs
->bs_drop
= d
->bd_dcount
;
1094 * Set immediate mode.
1097 d
->bd_immediate
= *(u_int
*)addr
;
1102 struct bpf_version
*bv
= (struct bpf_version
*)addr
;
1104 bv
->bv_major
= BPF_MAJOR_VERSION
;
1105 bv
->bv_minor
= BPF_MINOR_VERSION
;
1110 * Get "header already complete" flag
1113 *(u_int
*)addr
= d
->bd_hdrcmplt
;
1117 * Set "header already complete" flag
1120 d
->bd_hdrcmplt
= *(u_int
*)addr
? 1 : 0;
1124 * Get "see sent packets" flag
1127 *(u_int
*)addr
= d
->bd_seesent
;
1131 * Set "see sent packets" flag
1134 d
->bd_seesent
= *(u_int
*)addr
;
1137 case FIONBIO
: /* Non-blocking I/O */
1140 case FIOASYNC
: /* Send signal on receive packets */
1141 d
->bd_async
= *(int *)addr
;
1145 error
= fsetown(*(int *)addr
, &d
->bd_sigio
);
1149 *(int *)addr
= fgetown(d
->bd_sigio
);
1152 /* This is deprecated, FIOSETOWN should be used instead. */
1154 error
= fsetown(-(*(int *)addr
), &d
->bd_sigio
);
1157 /* This is deprecated, FIOGETOWN should be used instead. */
1159 *(int *)addr
= -fgetown(d
->bd_sigio
);
1162 case BIOCSRSIG
: /* Set receive signal */
1166 sig
= *(u_int
*)addr
;
1175 *(u_int
*)addr
= d
->bd_sig
;
1178 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1183 * Set d's packet filter program to fp. If this file already has a filter,
1184 * free it and replace it. Returns EINVAL for bogus requests.
1189 struct bpf_program
*fp
;
1191 struct bpf_insn
*fcode
, *old
;
1196 if (fp
->bf_insns
== 0) {
1197 if (fp
->bf_len
!= 0)
1204 FREE((caddr_t
)old
, M_DEVBUF
);
1208 if (flen
> BPF_MAXINSNS
)
1211 size
= flen
* sizeof(*fp
->bf_insns
);
1212 fcode
= (struct bpf_insn
*) _MALLOC(size
, M_DEVBUF
, M_WAIT
);
1217 if (copyin((caddr_t
)fp
->bf_insns
, (caddr_t
)fcode
, size
) == 0 &&
1218 bpf_validate(fcode
, (int)flen
)) {
1220 d
->bd_filter
= fcode
;
1224 FREE((caddr_t
)old
, M_DEVBUF
);
1228 FREE((caddr_t
)fcode
, M_DEVBUF
);
1233 * Detach a file from its current interface (if attached at all) and attach
1234 * to the interface indicated by the name stored in ifr.
1235 * Return an errno or 0.
1244 struct ifnet
*theywant
;
1246 theywant
= ifunit(ifr
->ifr_name
);
1251 * Look through attached interfaces for the named one.
1253 for (bp
= bpf_iflist
; bp
!= 0; bp
= bp
->bif_next
) {
1254 struct ifnet
*ifp
= bp
->bif_ifp
;
1256 if (ifp
== 0 || ifp
!= theywant
)
1259 * We found the requested interface.
1260 * If it's not up, return an error.
1261 * Allocate the packet buffers if we need to.
1262 * If we're already attached to requested interface,
1263 * just flush the buffer.
1265 if ((ifp
->if_flags
& IFF_UP
) == 0)
1268 if (d
->bd_sbuf
== 0) {
1269 error
= bpf_allocbufs(d
);
1274 if (bp
!= d
->bd_bif
) {
1277 * Detach if attached to something else.
1292 * Support for select() and poll() system calls
1294 * Return true iff the specific operation will not block indefinitely.
1295 * Otherwise, return false but make a note that a selwakeup() must be done.
1298 bpfpoll(dev
, events
, wql
, p
)
1304 register struct bpf_d
*d
;
1308 d
= bpf_dtab
[minor(dev
)];
1310 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1312 * An imitation of the FIONREAD ioctl code.
1314 if (d
->bd_bif
== NULL
) {
1315 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1320 if (events
& (POLLIN
| POLLRDNORM
)) {
1321 if (d
->bd_hlen
!= 0 || (d
->bd_immediate
&& d
->bd_slen
!= 0))
1322 revents
|= events
& (POLLIN
| POLLRDNORM
);
1324 selrecord(p
, &d
->bd_sel
, wql
);
1327 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1332 * Incoming linkage from device drivers. Process the packet pkt, of length
1333 * pktlen, which is stored in a contiguous buffer. The packet is parsed
1334 * by each process' filter, and if accepted, stashed into the corresponding
1338 bpf_tap(ifp
, pkt
, pktlen
)
1340 register u_char
*pkt
;
1341 register u_int pktlen
;
1344 register struct bpf_d
*d
;
1345 register u_int slen
;
1347 * Note that the ipl does not have to be raised at this point.
1348 * The only problem that could arise here is that if two different
1349 * interfaces shared any data. This is not the case.
1351 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1356 for (d
= bp
->bif_dlist
; d
!= 0; d
= d
->bd_next
) {
1358 slen
= bpf_filter(d
->bd_filter
, pkt
, pktlen
, pktlen
);
1360 catchpacket(d
, pkt
, pktlen
, slen
, bcopy
);
1364 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1369 * Copy data from an mbuf chain into a buffer. This code is derived
1370 * from m_copydata in sys/uipc_mbuf.c.
1373 bpf_mcopy(src_arg
, dst_arg
, len
)
1374 const void *src_arg
;
1376 register size_t len
;
1378 register const struct mbuf
*m
;
1379 register u_int count
;
1387 count
= min(m
->m_len
, len
);
1388 bcopy(mtod((struct mbuf
*)m
, void *), dst
, count
);
1396 * Incoming linkage from device drivers, when packet is in an mbuf chain.
1403 struct bpf_if
*bp
= ifp
->if_bpf
;
1409 for (m0
= m
; m0
!= 0; m0
= m0
->m_next
)
1410 pktlen
+= m0
->m_len
;
1412 for (d
= bp
->bif_dlist
; d
!= 0; d
= d
->bd_next
) {
1413 if (!d
->bd_seesent
&& (m
->m_pkthdr
.rcvif
== NULL
))
1416 slen
= bpf_filter(d
->bd_filter
, (u_char
*)m
, pktlen
, 0);
1418 catchpacket(d
, (u_char
*)m
, pktlen
, slen
, bpf_mcopy
);
1423 * Move the packet data from interface memory (pkt) into the
1424 * store buffer. Return 1 if it's time to wakeup a listener (buffer full),
1425 * otherwise 0. "copy" is the routine called to do the actual data
1426 * transfer. bcopy is passed in to copy contiguous chunks, while
1427 * bpf_mcopy is passed in to copy mbuf chains. In the latter case,
1428 * pkt is really an mbuf.
1431 catchpacket(d
, pkt
, pktlen
, snaplen
, cpfn
)
1432 register struct bpf_d
*d
;
1433 register u_char
*pkt
;
1434 register u_int pktlen
, snaplen
;
1435 register void (*cpfn
) __P((const void *, void *, size_t));
1437 register struct bpf_hdr
*hp
;
1438 register int totlen
, curlen
;
1439 register int hdrlen
= d
->bd_bif
->bif_hdrlen
;
1441 * Figure out how many bytes to move. If the packet is
1442 * greater or equal to the snapshot length, transfer that
1443 * much. Otherwise, transfer the whole packet (unless
1444 * we hit the buffer size limit).
1446 totlen
= hdrlen
+ min(snaplen
, pktlen
);
1447 if (totlen
> d
->bd_bufsize
)
1448 totlen
= d
->bd_bufsize
;
1451 * Round up the end of the previous packet to the next longword.
1453 curlen
= BPF_WORDALIGN(d
->bd_slen
);
1454 if (curlen
+ totlen
> d
->bd_bufsize
) {
1456 * This packet will overflow the storage buffer.
1457 * Rotate the buffers if we can, then wakeup any
1460 if (d
->bd_fbuf
== 0) {
1462 * We haven't completed the previous read yet,
1463 * so drop the packet.
1472 else if (d
->bd_immediate
)
1474 * Immediate mode is set. A packet arrived so any
1475 * reads should be woken up.
1480 * Append the bpf header.
1482 hp
= (struct bpf_hdr
*)(d
->bd_sbuf
+ curlen
);
1484 microtime(&hp
->bh_tstamp
);
1486 uniqtime(&hp
->bh_tstamp
);
1488 hp
->bh_tstamp
= time
;
1490 hp
->bh_datalen
= pktlen
;
1491 hp
->bh_hdrlen
= hdrlen
;
1493 * Copy the packet data into the store buffer and update its length.
1495 (*cpfn
)(pkt
, (u_char
*)hp
+ hdrlen
, (hp
->bh_caplen
= totlen
- hdrlen
));
1496 d
->bd_slen
= curlen
+ totlen
;
1500 * Initialize all nonzero fields of a descriptor.
1504 register struct bpf_d
*d
;
1506 d
->bd_fbuf
= (caddr_t
) _MALLOC(d
->bd_bufsize
, M_DEVBUF
, M_WAIT
);
1507 if (d
->bd_fbuf
== 0)
1510 d
->bd_sbuf
= (caddr_t
) _MALLOC(d
->bd_bufsize
, M_DEVBUF
, M_WAIT
);
1511 if (d
->bd_sbuf
== 0) {
1512 FREE(d
->bd_fbuf
, M_DEVBUF
);
1521 * Free buffers currently in use by a descriptor.
1526 register struct bpf_d
*d
;
1529 * We don't need to lock out interrupts since this descriptor has
1530 * been detached from its interface and it yet hasn't been marked
1533 if (d
->bd_sbuf
!= 0) {
1534 FREE(d
->bd_sbuf
, M_DEVBUF
);
1535 if (d
->bd_hbuf
!= 0)
1536 FREE(d
->bd_hbuf
, M_DEVBUF
);
1537 if (d
->bd_fbuf
!= 0)
1538 FREE(d
->bd_fbuf
, M_DEVBUF
);
1541 FREE((caddr_t
)d
->bd_filter
, M_DEVBUF
);
1547 * Attach an interface to bpf. driverp is a pointer to a (struct bpf_if *)
1548 * in the driver's softc; dlt is the link layer type; hdrlen is the fixed
1549 * size of the link header (variable length headers not yet supported).
1552 bpfattach(ifp
, dlt
, hdrlen
)
1558 bp
= (struct bpf_if
*) _MALLOC(sizeof(*bp
), M_DEVBUF
, M_WAIT
);
1566 bp
->bif_next
= bpf_iflist
;
1569 bp
->bif_ifp
->if_bpf
= 0;
1572 * Compute the length of the bpf header. This is not necessarily
1573 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1574 * that the network layer header begins on a longword boundary (for
1575 * performance reasons and to alleviate alignment restrictions).
1577 bp
->bif_hdrlen
= BPF_WORDALIGN(hdrlen
+ SIZEOF_BPF_HDR
) - hdrlen
;
1581 printf("bpf: %s%d attached\n", ifp
->if_name
, ifp
->if_unit
);
1586 * Detach bpf from an interface. This involves detaching each descriptor
1587 * associated with the interface, and leaving bd_bif NULL. Notify each
1588 * descriptor as it's detached so that any sleepers wake up and get
1595 struct bpf_if
*bp
, *bp_prev
;
1601 /* Locate BPF interface information */
1603 for (bp
= bpf_iflist
; bp
!= NULL
; bp
= bp
->bif_next
) {
1604 if (ifp
== bp
->bif_ifp
)
1610 /* Check for no BPF interface information */
1616 /* Interface wasn't attached */
1617 if (bp
->bif_ifp
== NULL
) {
1620 printf("bpfdetach: %s%d was not attached\n", ifp
->if_name
,
1626 while ((d
= bp
->bif_dlist
) != NULL
) {
1632 bp_prev
->bif_next
= bp
->bif_next
;
1634 bpf_iflist
= bp
->bif_next
;
1650 if (!bpf_devsw_installed
) {
1651 bpf_devsw_installed
= 1;
1652 maj
= cdevsw_add(CDEV_MAJOR
, &bpf_cdevsw
);
1654 printf("bpf_init: failed to allocate a major number!\n");
1658 if (bpf_dtab_grow(NBPFILTER
) == 0) {
1659 printf("bpf_init: failed to allocate bpf_dtab\n");
1662 for (i
= 0 ; i
< NBPFILTER
; i
++)
1663 bpf_make_dev_t(maj
);
1666 cdevsw_add(&bpf_cdevsw
);
1671 SYSINIT(bpfdev
,SI_SUB_DRIVERS
,SI_ORDER_MIDDLE
+CDEV_MAJOR
,bpf_drvinit
,NULL
)
1677 * NOP stubs to allow bpf-using drivers to load and function.
1679 * A 'better' implementation would allow the core bpf functionality
1680 * to be loaded at runtime.
1684 bpf_tap(ifp
, pkt
, pktlen
)
1686 register u_char
*pkt
;
1687 register u_int pktlen
;
1699 bpfattach(ifp
, dlt
, hdrlen
)
1712 bpf_filter(pc
, p
, wirelen
, buflen
)
1713 register const struct bpf_insn
*pc
;
1716 register u_int buflen
;
1718 return -1; /* "no filter" behaviour */
1720 #endif /* !defined(__APPLE__) */
1721 #endif /* NBPFILTER > 0 */