1 /* $NetBSD: bpf.c,v 1.149 2009/12/09 21:32:59 dsl Exp $ */
4 * Copyright (c) 1990, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from the Stanford/CMU enet packet filter,
8 * (net/enet.c) distributed as part of 4.3BSD, and code contributed
9 * to Berkeley by Steven McCanne and Van Jacobson both of Lawrence
10 * Berkeley Laboratory.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)bpf.c 8.4 (Berkeley) 1/9/95
37 * static char rcsid[] =
38 * "Header: bpf.c,v 1.67 96/09/26 22:00:52 leres Exp ";
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: bpf.c,v 1.149 2009/12/09 21:32:59 dsl Exp $");
44 #if defined(_KERNEL_OPT)
50 #include <sys/param.h>
51 #include <sys/systm.h>
56 #include <sys/ioctl.h>
58 #include <sys/vnode.h>
59 #include <sys/queue.h>
63 #include <sys/filedesc.h>
67 #include <sys/protosw.h>
68 #include <sys/socket.h>
69 #include <sys/errno.h>
70 #include <sys/kernel.h>
72 #include <sys/sysctl.h>
73 #include <sys/kauth.h>
79 #include <net/bpfdesc.h>
81 #include <net/if_arc.h>
82 #include <net/if_ether.h>
84 #include <netinet/in.h>
85 #include <netinet/if_inarp.h>
88 #include <compat/sys/sockio.h>
92 * 4096 is too small for FDDI frames. 8192 is too small for gigabit Ethernet
93 * jumbos (circa 9k), ATM, or Intel gig/10gig ethernet jumbos (16k).
95 # define BPF_BUFSIZE 32768
98 #define PRINET 26 /* interruptible */
101 * The default read buffer size, and limit for BIOCSBLEN, is sysctl'able.
102 * XXX the default values should be computed dynamically based
103 * on available memory size and available mbuf clusters.
105 int bpf_bufsize
= BPF_BUFSIZE
;
106 int bpf_maxbufsize
= BPF_DFLTBUFSIZE
; /* XXX set dynamically, see above */
110 * Global BPF statistics returned by net.bpf.stats sysctl.
112 struct bpf_stat bpf_gstats
;
115 * Use a mutex to avoid a race condition between gathering the stats/peers
116 * and opening/closing the device.
118 static kmutex_t bpf_mtx
;
121 * bpf_iflist is the list of interfaces; each corresponds to an ifnet
122 * bpf_dtab holds the descriptors, indexed by minor device #
124 struct bpf_if
*bpf_iflist
;
125 LIST_HEAD(, bpf_d
) bpf_list
;
127 static int bpf_allocbufs(struct bpf_d
*);
128 static void bpf_deliver(struct bpf_if
*,
129 void *(*cpfn
)(void *, const void *, size_t),
130 void *, u_int
, u_int
, struct ifnet
*);
131 static void bpf_freed(struct bpf_d
*);
132 static void bpf_ifname(struct ifnet
*, struct ifreq
*);
133 static void *bpf_mcpy(void *, const void *, size_t);
134 static int bpf_movein(struct uio
*, int, int,
135 struct mbuf
**, struct sockaddr
*);
136 static void bpf_attachd(struct bpf_d
*, struct bpf_if
*);
137 static void bpf_detachd(struct bpf_d
*);
138 static int bpf_setif(struct bpf_d
*, struct ifreq
*);
139 static void bpf_timed_out(void *);
141 bpf_wakeup(struct bpf_d
*);
142 static void catchpacket(struct bpf_d
*, u_char
*, u_int
, u_int
,
143 void *(*)(void *, const void *, size_t), struct timespec
*);
144 static void reset_d(struct bpf_d
*);
145 static int bpf_getdltlist(struct bpf_d
*, struct bpf_dltlist
*);
146 static int bpf_setdlt(struct bpf_d
*, u_int
);
148 static int bpf_read(struct file
*, off_t
*, struct uio
*, kauth_cred_t
,
150 static int bpf_write(struct file
*, off_t
*, struct uio
*, kauth_cred_t
,
152 static int bpf_ioctl(struct file
*, u_long
, void *);
153 static int bpf_poll(struct file
*, int);
154 static int bpf_stat(struct file
*, struct stat
*);
155 static int bpf_close(struct file
*);
156 static int bpf_kqfilter(struct file
*, struct knote
*);
157 static void bpf_softintr(void *);
159 static const struct fileops bpf_fileops
= {
161 .fo_write
= bpf_write
,
162 .fo_ioctl
= bpf_ioctl
,
163 .fo_fcntl
= fnullop_fcntl
,
166 .fo_close
= bpf_close
,
167 .fo_kqfilter
= bpf_kqfilter
,
168 .fo_restart
= fnullop_restart
,
171 dev_type_open(bpfopen
);
173 const struct cdevsw bpf_cdevsw
= {
174 bpfopen
, noclose
, noread
, nowrite
, noioctl
,
175 nostop
, notty
, nopoll
, nommap
, nokqfilter
, D_OTHER
179 bpf_movein(struct uio
*uio
, int linktype
, int mtu
, struct mbuf
**mp
,
180 struct sockaddr
*sockp
)
189 * Build a sockaddr based on the data link layer type.
190 * We do this at this level because the ethernet header
191 * is copied directly into the data field of the sockaddr.
192 * In the case of SLIP, there is no header and the packet
193 * is forwarded as is.
194 * Also, we are careful to leave room at the front of the mbuf
195 * for the link level header.
200 sockp
->sa_family
= AF_INET
;
206 sockp
->sa_family
= AF_UNSPEC
;
212 sockp
->sa_family
= AF_UNSPEC
;
213 /* XXX Would MAXLINKHDR be better? */
214 /* 6(dst)+6(src)+2(type) */
215 hlen
= sizeof(struct ether_header
);
220 sockp
->sa_family
= AF_UNSPEC
;
226 sockp
->sa_family
= AF_LINK
;
227 /* XXX 4(FORMAC)+6(dst)+6(src) */
233 sockp
->sa_family
= AF_UNSPEC
;
239 sockp
->sa_family
= AF_UNSPEC
;
248 len
= uio
->uio_resid
;
250 * If there aren't enough bytes for a link level header or the
251 * packet length exceeds the interface mtu, return an error.
253 if (len
< hlen
|| len
- hlen
> mtu
)
257 * XXX Avoid complicated buffer chaining ---
258 * bail if it won't fit in a single mbuf.
259 * (Take into account possible alignment bytes)
261 if ((unsigned)len
> MCLBYTES
- align
)
264 m
= m_gethdr(M_WAIT
, MT_DATA
);
265 m
->m_pkthdr
.rcvif
= 0;
266 m
->m_pkthdr
.len
= len
- hlen
;
267 if (len
> MHLEN
- align
) {
269 if ((m
->m_flags
& M_EXT
) == 0) {
275 /* Insure the data is properly aligned */
281 error
= uiomove(mtod(m
, void *), len
, uio
);
285 memcpy(sockp
->sa_data
, mtod(m
, void *), hlen
);
286 m
->m_data
+= hlen
; /* XXX */
299 * Attach file to the bpf interface, i.e. make d listen on bp.
300 * Must be called at splnet.
303 bpf_attachd(struct bpf_d
*d
, struct bpf_if
*bp
)
306 * Point d at bp, and add d to the interface's list of listeners.
307 * Finally, point the driver's bpf cookie at the interface so
308 * it will divert packets to bpf.
311 d
->bd_next
= bp
->bif_dlist
;
314 *bp
->bif_driverp
= bp
;
318 * Detach a file from its interface.
321 bpf_detachd(struct bpf_d
*d
)
328 * Check if this descriptor had requested promiscuous mode.
329 * If so, turn it off.
336 * Take device out of promiscuous mode. Since we were
337 * able to enter promiscuous mode, we should be able
338 * to turn it off. But we can get an error if
339 * the interface was configured down, so only panic
340 * if we don't get an unexpected error.
342 error
= ifpromisc(bp
->bif_ifp
, 0);
343 if (error
&& error
!= EINVAL
)
344 panic("%s: ifpromisc failed: %d", __func__
, error
);
346 /* Remove d from the interface's descriptor list. */
351 panic("%s: descriptor not in list", __func__
);
354 if (bp
->bif_dlist
== 0)
356 * Let the driver know that there are no more listeners.
358 *d
->bd_bif
->bif_driverp
= 0;
364 * Mark a descriptor free by making it point to itself.
365 * This is probably cheaper than marking with a constant since
366 * the address should be in a register anyway.
370 * bpfilterattach() is called at boot time.
374 bpfilterattach(int n
)
376 mutex_init(&bpf_mtx
, MUTEX_DEFAULT
, IPL_NONE
);
378 mutex_enter(&bpf_mtx
);
379 LIST_INIT(&bpf_list
);
380 mutex_exit(&bpf_mtx
);
382 bpf_gstats
.bs_recv
= 0;
383 bpf_gstats
.bs_drop
= 0;
384 bpf_gstats
.bs_capt
= 0;
388 * Open ethernet device. Clones.
392 bpfopen(dev_t dev
, int flag
, int mode
, struct lwp
*l
)
398 /* falloc() will use the descriptor for us. */
399 if ((error
= fd_allocfile(&fp
, &fd
)) != 0)
402 d
= malloc(sizeof(*d
), M_DEVBUF
, M_WAITOK
|M_ZERO
);
403 d
->bd_bufsize
= bpf_bufsize
;
405 d
->bd_pid
= l
->l_proc
->p_pid
;
406 getnanotime(&d
->bd_btime
);
407 d
->bd_atime
= d
->bd_mtime
= d
->bd_btime
;
408 callout_init(&d
->bd_callout
, 0);
410 d
->bd_sih
= softint_establish(SOFTINT_CLOCK
, bpf_softintr
, d
);
412 mutex_enter(&bpf_mtx
);
413 LIST_INSERT_HEAD(&bpf_list
, d
, bd_list
);
414 mutex_exit(&bpf_mtx
);
416 return fd_clone(fp
, fd
, flag
, &bpf_fileops
, d
);
420 * Close the descriptor by detaching it from its interface,
421 * deallocating its buffers, and marking it free.
425 bpf_close(struct file
*fp
)
427 struct bpf_d
*d
= fp
->f_data
;
430 KERNEL_LOCK(1, NULL
);
433 * Refresh the PID associated with this bpf file.
435 d
->bd_pid
= curproc
->p_pid
;
438 if (d
->bd_state
== BPF_WAITING
)
439 callout_stop(&d
->bd_callout
);
440 d
->bd_state
= BPF_IDLE
;
445 mutex_enter(&bpf_mtx
);
446 LIST_REMOVE(d
, bd_list
);
447 mutex_exit(&bpf_mtx
);
448 callout_destroy(&d
->bd_callout
);
449 seldestroy(&d
->bd_sel
);
450 softint_disestablish(d
->bd_sih
);
454 KERNEL_UNLOCK_ONE(NULL
);
460 * Rotate the packet buffers in descriptor d. Move the store buffer
461 * into the hold slot, and the free buffer into the store slot.
462 * Zero the length of the new store buffer.
464 #define ROTATE_BUFFERS(d) \
465 (d)->bd_hbuf = (d)->bd_sbuf; \
466 (d)->bd_hlen = (d)->bd_slen; \
467 (d)->bd_sbuf = (d)->bd_fbuf; \
471 * bpfread - read next chunk of packets from buffers
474 bpf_read(struct file
*fp
, off_t
*offp
, struct uio
*uio
,
475 kauth_cred_t cred
, int flags
)
477 struct bpf_d
*d
= fp
->f_data
;
482 getnanotime(&d
->bd_atime
);
484 * Restrict application to use a buffer the same size as
485 * the kernel buffers.
487 if (uio
->uio_resid
!= d
->bd_bufsize
)
490 KERNEL_LOCK(1, NULL
);
492 if (d
->bd_state
== BPF_WAITING
)
493 callout_stop(&d
->bd_callout
);
494 timed_out
= (d
->bd_state
== BPF_TIMED_OUT
);
495 d
->bd_state
= BPF_IDLE
;
497 * If the hold buffer is empty, then do a timed sleep, which
498 * ends when the timeout expires or when enough packets
499 * have arrived to fill the store buffer.
501 while (d
->bd_hbuf
== 0) {
502 if (fp
->f_flag
& FNONBLOCK
) {
503 if (d
->bd_slen
== 0) {
505 KERNEL_UNLOCK_ONE(NULL
);
506 return (EWOULDBLOCK
);
512 if ((d
->bd_immediate
|| timed_out
) && d
->bd_slen
!= 0) {
514 * A packet(s) either arrived since the previous
515 * read or arrived while we were asleep.
516 * Rotate the buffers and return what's here.
521 error
= tsleep(d
, PRINET
|PCATCH
, "bpf",
523 if (error
== EINTR
|| error
== ERESTART
) {
525 KERNEL_UNLOCK_ONE(NULL
);
528 if (error
== EWOULDBLOCK
) {
530 * On a timeout, return what's in the buffer,
531 * which may be nothing. If there is something
532 * in the store buffer, we can rotate the buffers.
536 * We filled up the buffer in between
537 * getting the timeout and arriving
538 * here, so we don't need to rotate.
542 if (d
->bd_slen
== 0) {
544 KERNEL_UNLOCK_ONE(NULL
);
554 * At this point, we know we have something in the hold slot.
559 * Move data from hold buffer into user space.
560 * We know the entire buffer is transferred since
561 * we checked above that the read buffer is bpf_bufsize bytes.
563 error
= uiomove(d
->bd_hbuf
, d
->bd_hlen
, uio
);
566 d
->bd_fbuf
= d
->bd_hbuf
;
571 KERNEL_UNLOCK_ONE(NULL
);
577 * If there are processes sleeping on this descriptor, wake them up.
580 bpf_wakeup(struct bpf_d
*d
)
584 softint_schedule(d
->bd_sih
);
585 selnotify(&d
->bd_sel
, 0, 0);
589 bpf_softintr(void *cookie
)
595 fownsignal(d
->bd_pgid
, SIGIO
, 0, 0, NULL
);
599 bpf_timed_out(void *arg
)
601 struct bpf_d
*d
= arg
;
605 if (d
->bd_state
== BPF_WAITING
) {
606 d
->bd_state
= BPF_TIMED_OUT
;
615 bpf_write(struct file
*fp
, off_t
*offp
, struct uio
*uio
,
616 kauth_cred_t cred
, int flags
)
618 struct bpf_d
*d
= fp
->f_data
;
622 static struct sockaddr_storage dst
;
624 m
= NULL
; /* XXX gcc */
626 KERNEL_LOCK(1, NULL
);
628 if (d
->bd_bif
== 0) {
629 KERNEL_UNLOCK_ONE(NULL
);
632 getnanotime(&d
->bd_mtime
);
634 ifp
= d
->bd_bif
->bif_ifp
;
636 if (uio
->uio_resid
== 0) {
637 KERNEL_UNLOCK_ONE(NULL
);
641 error
= bpf_movein(uio
, (int)d
->bd_bif
->bif_dlt
, ifp
->if_mtu
, &m
,
642 (struct sockaddr
*) &dst
);
644 KERNEL_UNLOCK_ONE(NULL
);
648 if (m
->m_pkthdr
.len
> ifp
->if_mtu
) {
649 KERNEL_UNLOCK_ONE(NULL
);
655 dst
.ss_family
= pseudo_AF_HDRCMPLT
;
658 error
= (*ifp
->if_output
)(ifp
, m
, (struct sockaddr
*) &dst
, NULL
);
660 KERNEL_UNLOCK_ONE(NULL
);
662 * The driver frees the mbuf.
668 * Reset a descriptor by flushing its packet buffer and clearing the
669 * receive and drop counts. Should be called at splnet.
672 reset_d(struct bpf_d
*d
)
675 /* Free the hold buffer. */
676 d
->bd_fbuf
= d
->bd_hbuf
;
687 * FIONREAD Check for read packet available.
688 * BIOCGBLEN Get buffer len [for read()].
689 * BIOCSETF Set ethernet read filter.
690 * BIOCFLUSH Flush read packet buffer.
691 * BIOCPROMISC Put interface into promiscuous mode.
692 * BIOCGDLT Get link layer type.
693 * BIOCGETIF Get interface name.
694 * BIOCSETIF Set interface.
695 * BIOCSRTIMEOUT Set read timeout.
696 * BIOCGRTIMEOUT Get read timeout.
697 * BIOCGSTATS Get packet stats.
698 * BIOCIMMEDIATE Set immediate mode.
699 * BIOCVERSION Get filter language version.
700 * BIOCGHDRCMPLT Get "header already complete" flag.
701 * BIOCSHDRCMPLT Set "header already complete" flag.
705 bpf_ioctl(struct file
*fp
, u_long cmd
, void *addr
)
707 struct bpf_d
*d
= fp
->f_data
;
711 * Refresh the PID associated with this bpf file.
713 KERNEL_LOCK(1, NULL
);
714 d
->bd_pid
= curproc
->p_pid
;
717 if (d
->bd_state
== BPF_WAITING
)
718 callout_stop(&d
->bd_callout
);
719 d
->bd_state
= BPF_IDLE
;
729 * Check for read packet available.
746 * Get buffer len [for read()].
749 *(u_int
*)addr
= d
->bd_bufsize
;
759 u_int size
= *(u_int
*)addr
;
761 if (size
> bpf_maxbufsize
)
762 *(u_int
*)addr
= size
= bpf_maxbufsize
;
763 else if (size
< BPF_MINBUFSIZE
)
764 *(u_int
*)addr
= size
= BPF_MINBUFSIZE
;
765 d
->bd_bufsize
= size
;
770 * Set link layer read filter.
773 error
= bpf_setf(d
, addr
);
777 * Flush read packet buffer.
786 * Put interface into promiscuous mode.
789 if (d
->bd_bif
== 0) {
791 * No interface attached yet.
797 if (d
->bd_promisc
== 0) {
798 error
= ifpromisc(d
->bd_bif
->bif_ifp
, 1);
806 * Get device parameters.
812 *(u_int
*)addr
= d
->bd_bif
->bif_dlt
;
816 * Get a list of supported device parameters.
822 error
= bpf_getdltlist(d
, addr
);
826 * Set device parameters.
832 error
= bpf_setdlt(d
, *(u_int
*)addr
);
836 * Set interface name.
845 bpf_ifname(d
->bd_bif
->bif_ifp
, addr
);
855 error
= bpf_setif(d
, addr
);
863 struct timeval
*tv
= addr
;
865 /* Compute number of ticks. */
866 d
->bd_rtout
= tv
->tv_sec
* hz
+ tv
->tv_usec
/ tick
;
867 if ((d
->bd_rtout
== 0) && (tv
->tv_usec
!= 0))
872 #ifdef BIOCGORTIMEOUT
878 struct timeval50
*tv
= addr
;
880 tv
->tv_sec
= d
->bd_rtout
/ hz
;
881 tv
->tv_usec
= (d
->bd_rtout
% hz
) * tick
;
886 #ifdef BIOCSORTIMEOUT
892 struct timeval50
*tv
= addr
;
894 /* Compute number of ticks. */
895 d
->bd_rtout
= tv
->tv_sec
* hz
+ tv
->tv_usec
/ tick
;
896 if ((d
->bd_rtout
== 0) && (tv
->tv_usec
!= 0))
907 struct timeval
*tv
= addr
;
909 tv
->tv_sec
= d
->bd_rtout
/ hz
;
910 tv
->tv_usec
= (d
->bd_rtout
% hz
) * tick
;
918 struct bpf_stat
*bs
= addr
;
920 bs
->bs_recv
= d
->bd_rcount
;
921 bs
->bs_drop
= d
->bd_dcount
;
922 bs
->bs_capt
= d
->bd_ccount
;
928 struct bpf_stat_old
*bs
= addr
;
930 bs
->bs_recv
= d
->bd_rcount
;
931 bs
->bs_drop
= d
->bd_dcount
;
936 * Set immediate mode.
939 d
->bd_immediate
= *(u_int
*)addr
;
944 struct bpf_version
*bv
= addr
;
946 bv
->bv_major
= BPF_MAJOR_VERSION
;
947 bv
->bv_minor
= BPF_MINOR_VERSION
;
951 case BIOCGHDRCMPLT
: /* get "header already complete" flag */
952 *(u_int
*)addr
= d
->bd_hdrcmplt
;
955 case BIOCSHDRCMPLT
: /* set "header already complete" flag */
956 d
->bd_hdrcmplt
= *(u_int
*)addr
? 1 : 0;
960 * Get "see sent packets" flag
963 *(u_int
*)addr
= d
->bd_seesent
;
967 * Set "see sent" packets flag
970 d
->bd_seesent
= *(u_int
*)addr
;
973 case FIONBIO
: /* Non-blocking I/O */
975 * No need to do anything special as we use IO_NDELAY in
976 * bpfread() as an indication of whether or not to block
981 case FIOASYNC
: /* Send signal on receive packets */
982 d
->bd_async
= *(int *)addr
;
985 case TIOCSPGRP
: /* Process or group to send signals to */
987 error
= fsetown(&d
->bd_pgid
, cmd
, addr
);
992 error
= fgetown(d
->bd_pgid
, cmd
, addr
);
995 KERNEL_UNLOCK_ONE(NULL
);
1000 * Set d's packet filter program to fp. If this file already has a filter,
1001 * free it and replace it. Returns EINVAL for bogus requests.
1004 bpf_setf(struct bpf_d
*d
, struct bpf_program
*fp
)
1006 struct bpf_insn
*fcode
, *old
;
1011 if (fp
->bf_insns
== 0) {
1012 if (fp
->bf_len
!= 0)
1019 free(old
, M_DEVBUF
);
1023 if (flen
> BPF_MAXINSNS
)
1026 size
= flen
* sizeof(*fp
->bf_insns
);
1027 fcode
= malloc(size
, M_DEVBUF
, M_WAITOK
);
1028 if (copyin(fp
->bf_insns
, fcode
, size
) == 0 &&
1029 bpf_validate(fcode
, (int)flen
)) {
1031 d
->bd_filter
= fcode
;
1035 free(old
, M_DEVBUF
);
1039 free(fcode
, M_DEVBUF
);
1044 * Detach a file from its current interface (if attached at all) and attach
1045 * to the interface indicated by the name stored in ifr.
1046 * Return an errno or 0.
1049 bpf_setif(struct bpf_d
*d
, struct ifreq
*ifr
)
1053 int unit_seen
, i
, s
, error
;
1056 * Make sure the provided name has a unit number, and default
1057 * it to '0' if not specified.
1058 * XXX This is ugly ... do this differently?
1062 cp
[sizeof(ifr
->ifr_name
) - 1] = '\0'; /* sanity */
1064 if (*cp
>= '0' && *cp
<= '9')
1067 /* Make sure to leave room for the '\0'. */
1068 for (i
= 0; i
< (IFNAMSIZ
- 1); ++i
) {
1069 if ((ifr
->ifr_name
[i
] >= 'a' &&
1070 ifr
->ifr_name
[i
] <= 'z') ||
1071 (ifr
->ifr_name
[i
] >= 'A' &&
1072 ifr
->ifr_name
[i
] <= 'Z'))
1074 ifr
->ifr_name
[i
] = '0';
1079 * Look through attached interfaces for the named one.
1081 for (bp
= bpf_iflist
; bp
!= 0; bp
= bp
->bif_next
) {
1082 struct ifnet
*ifp
= bp
->bif_ifp
;
1085 strcmp(ifp
->if_xname
, ifr
->ifr_name
) != 0)
1087 /* skip additional entry */
1088 if ((void **)bp
->bif_driverp
!= &ifp
->if_bpf
)
1091 * We found the requested interface.
1092 * Allocate the packet buffers if we need to.
1093 * If we're already attached to requested interface,
1094 * just flush the buffer.
1096 if (d
->bd_sbuf
== 0) {
1097 error
= bpf_allocbufs(d
);
1102 if (bp
!= d
->bd_bif
) {
1105 * Detach if attached to something else.
1120 * Copy the interface name to the ifreq.
1123 bpf_ifname(struct ifnet
*ifp
, struct ifreq
*ifr
)
1125 memcpy(ifr
->ifr_name
, ifp
->if_xname
, IFNAMSIZ
);
1129 bpf_stat(struct file
*fp
, struct stat
*st
)
1131 struct bpf_d
*d
= fp
->f_data
;
1133 (void)memset(st
, 0, sizeof(*st
));
1134 KERNEL_LOCK(1, NULL
);
1135 st
->st_dev
= makedev(cdevsw_lookup_major(&bpf_cdevsw
), d
->bd_pid
);
1136 st
->st_atimespec
= d
->bd_atime
;
1137 st
->st_mtimespec
= d
->bd_mtime
;
1138 st
->st_ctimespec
= st
->st_birthtimespec
= d
->bd_btime
;
1139 st
->st_uid
= kauth_cred_geteuid(fp
->f_cred
);
1140 st
->st_gid
= kauth_cred_getegid(fp
->f_cred
);
1141 KERNEL_UNLOCK_ONE(NULL
);
1146 * Support for poll() system call
1148 * Return true iff the specific operation will not block indefinitely - with
1149 * the assumption that it is safe to positively acknowledge a request for the
1150 * ability to write to the BPF device.
1151 * Otherwise, return false but make a note that a selnotify() must be done.
1154 bpf_poll(struct file
*fp
, int events
)
1156 struct bpf_d
*d
= fp
->f_data
;
1161 * Refresh the PID associated with this bpf file.
1163 KERNEL_LOCK(1, NULL
);
1164 d
->bd_pid
= curproc
->p_pid
;
1166 revents
= events
& (POLLOUT
| POLLWRNORM
);
1167 if (events
& (POLLIN
| POLLRDNORM
)) {
1169 * An imitation of the FIONREAD ioctl code.
1171 if (d
->bd_hlen
!= 0 ||
1172 ((d
->bd_immediate
|| d
->bd_state
== BPF_TIMED_OUT
) &&
1174 revents
|= events
& (POLLIN
| POLLRDNORM
);
1176 selrecord(curlwp
, &d
->bd_sel
);
1177 /* Start the read timeout if necessary */
1178 if (d
->bd_rtout
> 0 && d
->bd_state
== BPF_IDLE
) {
1179 callout_reset(&d
->bd_callout
, d
->bd_rtout
,
1181 d
->bd_state
= BPF_WAITING
;
1186 KERNEL_UNLOCK_ONE(NULL
);
1192 filt_bpfrdetach(struct knote
*kn
)
1194 struct bpf_d
*d
= kn
->kn_hook
;
1197 KERNEL_LOCK(1, NULL
);
1199 SLIST_REMOVE(&d
->bd_sel
.sel_klist
, kn
, knote
, kn_selnext
);
1201 KERNEL_UNLOCK_ONE(NULL
);
1205 filt_bpfread(struct knote
*kn
, long hint
)
1207 struct bpf_d
*d
= kn
->kn_hook
;
1210 KERNEL_LOCK(1, NULL
);
1211 kn
->kn_data
= d
->bd_hlen
;
1212 if (d
->bd_immediate
)
1213 kn
->kn_data
+= d
->bd_slen
;
1214 rv
= (kn
->kn_data
> 0);
1215 KERNEL_UNLOCK_ONE(NULL
);
1219 static const struct filterops bpfread_filtops
=
1220 { 1, NULL
, filt_bpfrdetach
, filt_bpfread
};
1223 bpf_kqfilter(struct file
*fp
, struct knote
*kn
)
1225 struct bpf_d
*d
= fp
->f_data
;
1226 struct klist
*klist
;
1229 KERNEL_LOCK(1, NULL
);
1231 switch (kn
->kn_filter
) {
1233 klist
= &d
->bd_sel
.sel_klist
;
1234 kn
->kn_fop
= &bpfread_filtops
;
1238 KERNEL_UNLOCK_ONE(NULL
);
1245 SLIST_INSERT_HEAD(klist
, kn
, kn_selnext
);
1247 KERNEL_UNLOCK_ONE(NULL
);
1253 * Incoming linkage from device drivers. Process the packet pkt, of length
1254 * pktlen, which is stored in a contiguous buffer. The packet is parsed
1255 * by each process' filter, and if accepted, stashed into the corresponding
1259 bpf_tap(void *arg
, u_char
*pkt
, u_int pktlen
)
1268 * Note that the ipl does not have to be raised at this point.
1269 * The only problem that could arise here is that if two different
1270 * interfaces shared any data. This is not the case.
1273 for (d
= bp
->bif_dlist
; d
!= 0; d
= d
->bd_next
) {
1275 ++bpf_gstats
.bs_recv
;
1276 slen
= bpf_filter(d
->bd_filter
, pkt
, pktlen
, pktlen
);
1282 catchpacket(d
, pkt
, pktlen
, slen
, memcpy
, &ts
);
1288 * Copy data from an mbuf chain into a buffer. This code is derived
1289 * from m_copydata in sys/uipc_mbuf.c.
1292 bpf_mcpy(void *dst_arg
, const void *src_arg
, size_t len
)
1294 const struct mbuf
*m
;
1303 count
= min(m
->m_len
, len
);
1304 memcpy(dst
, mtod(m
, const void *), count
);
1313 * Dispatch a packet to all the listeners on interface bp.
1315 * marg pointer to the packet, either a data buffer or an mbuf chain
1316 * buflen buffer length, if marg is a data buffer
1317 * cpfn a function that can copy marg into the listener's buffer
1318 * pktlen length of the packet
1319 * rcvif either NULL or the interface the packet came in on.
1322 bpf_deliver(struct bpf_if
*bp
, void *(*cpfn
)(void *, const void *, size_t),
1323 void *marg
, u_int pktlen
, u_int buflen
, struct ifnet
*rcvif
)
1330 for (d
= bp
->bif_dlist
; d
!= 0; d
= d
->bd_next
) {
1331 if (!d
->bd_seesent
&& (rcvif
== NULL
))
1334 ++bpf_gstats
.bs_recv
;
1335 slen
= bpf_filter(d
->bd_filter
, marg
, pktlen
, buflen
);
1341 catchpacket(d
, marg
, pktlen
, slen
, cpfn
, &ts
);
1347 * Incoming linkage from device drivers, when the head of the packet is in
1348 * a buffer, and the tail is in an mbuf chain.
1351 bpf_mtap2(void *arg
, void *data
, u_int dlen
, struct mbuf
*m
)
1353 struct bpf_if
*bp
= arg
;
1357 pktlen
= m_length(m
) + dlen
;
1360 * Craft on-stack mbuf suitable for passing to bpf_filter.
1361 * Note that we cut corners here; we only setup what's
1362 * absolutely needed--this mbuf should never go anywhere else.
1364 (void)memset(&mb
, 0, sizeof(mb
));
1369 bpf_deliver(bp
, bpf_mcpy
, &mb
, pktlen
, 0, m
->m_pkthdr
.rcvif
);
1373 * Incoming linkage from device drivers, when packet is in an mbuf chain.
1376 bpf_mtap(void *arg
, struct mbuf
*m
)
1378 void *(*cpfn
)(void *, const void *, size_t);
1379 struct bpf_if
*bp
= arg
;
1380 u_int pktlen
, buflen
;
1383 pktlen
= m_length(m
);
1385 if (pktlen
== m
->m_len
) {
1386 cpfn
= (void *)memcpy
;
1387 marg
= mtod(m
, void *);
1390 /*###1299 [cc] warning: assignment from incompatible pointer type%%%*/
1396 bpf_deliver(bp
, cpfn
, marg
, pktlen
, buflen
, m
->m_pkthdr
.rcvif
);
1400 * We need to prepend the address family as
1401 * a four byte field. Cons up a dummy header
1402 * to pacify bpf. This is safe because bpf
1403 * will only read from the mbuf (i.e., it won't
1404 * try to free it or keep a pointer a to it).
1407 bpf_mtap_af(void *arg
, uint32_t af
, struct mbuf
*m
)
1414 m0
.m_data
= (char *)&af
;
1420 bpf_mtap_et(void *arg
, uint16_t et
, struct mbuf
*m
)
1427 m0
.m_data
= m0
.m_dat
;
1429 ((uint32_t *)m0
.m_data
)[0] = 0;
1430 ((uint32_t *)m0
.m_data
)[1] = 0;
1431 ((uint32_t *)m0
.m_data
)[2] = 0;
1432 ((uint16_t *)m0
.m_data
)[6] = et
;
1437 #if NSL > 0 || NSTRIP > 0
1439 * Put the SLIP pseudo-"link header" in place.
1440 * Note this M_PREPEND() should never fail,
1441 * swince we know we always have enough space
1442 * in the input buffer.
1445 bpf_mtap_sl_in(void *arg
, u_char
*chdr
, struct mbuf
**m
)
1450 M_PREPEND(*m
, SLIP_HDRLEN
, M_DONTWAIT
);
1454 hp
= mtod(*m
, u_char
*);
1455 hp
[SLX_DIR
] = SLIPDIR_IN
;
1456 (void)memcpy(&hp
[SLX_CHDR
], chdr
, CHDR_LEN
);
1462 m_adj(*m
, SLIP_HDRLEN
);
1466 * Put the SLIP pseudo-"link header" in
1467 * place. The compressed header is now
1468 * at the beginning of the mbuf.
1471 bpf_mtap_sl_out(void *arg
, u_char
*chdr
, struct mbuf
*m
)
1479 m0
.m_data
= m0
.m_dat
;
1480 m0
.m_len
= SLIP_HDRLEN
;
1482 hp
= mtod(&m0
, u_char
*);
1484 hp
[SLX_DIR
] = SLIPDIR_OUT
;
1485 (void)memcpy(&hp
[SLX_CHDR
], chdr
, CHDR_LEN
);
1495 * Move the packet data from interface memory (pkt) into the
1496 * store buffer. Return 1 if it's time to wakeup a listener (buffer full),
1497 * otherwise 0. "copy" is the routine called to do the actual data
1498 * transfer. memcpy is passed in to copy contiguous chunks, while
1499 * bpf_mcpy is passed in to copy mbuf chains. In the latter case,
1500 * pkt is really an mbuf.
1503 catchpacket(struct bpf_d
*d
, u_char
*pkt
, u_int pktlen
, u_int snaplen
,
1504 void *(*cpfn
)(void *, const void *, size_t), struct timespec
*ts
)
1508 int hdrlen
= d
->bd_bif
->bif_hdrlen
;
1512 ++bpf_gstats
.bs_capt
;
1514 * Figure out how many bytes to move. If the packet is
1515 * greater or equal to the snapshot length, transfer that
1516 * much. Otherwise, transfer the whole packet (unless
1517 * we hit the buffer size limit).
1519 totlen
= hdrlen
+ min(snaplen
, pktlen
);
1520 if (totlen
> d
->bd_bufsize
)
1521 totlen
= d
->bd_bufsize
;
1524 * Round up the end of the previous packet to the next longword.
1526 curlen
= BPF_WORDALIGN(d
->bd_slen
);
1527 if (curlen
+ totlen
> d
->bd_bufsize
) {
1529 * This packet will overflow the storage buffer.
1530 * Rotate the buffers if we can, then wakeup any
1533 if (d
->bd_fbuf
== 0) {
1535 * We haven't completed the previous read yet,
1536 * so drop the packet.
1539 ++bpf_gstats
.bs_drop
;
1545 } else if (d
->bd_immediate
|| d
->bd_state
== BPF_TIMED_OUT
) {
1547 * Immediate mode is set, or the read timeout has
1548 * already expired during a select call. A packet
1549 * arrived, so the reader should be woken up.
1555 * Append the bpf header.
1557 hp
= (struct bpf_hdr
*)((char *)d
->bd_sbuf
+ curlen
);
1558 hp
->bh_tstamp
.tv_sec
= ts
->tv_sec
;
1559 hp
->bh_tstamp
.tv_usec
= ts
->tv_nsec
/ 1000;
1560 hp
->bh_datalen
= pktlen
;
1561 hp
->bh_hdrlen
= hdrlen
;
1563 * Copy the packet data into the store buffer and update its length.
1565 (*cpfn
)((u_char
*)hp
+ hdrlen
, pkt
, (hp
->bh_caplen
= totlen
- hdrlen
));
1566 d
->bd_slen
= curlen
+ totlen
;
1569 * Call bpf_wakeup after bd_slen has been updated so that kevent(2)
1570 * will cause filt_bpfread() to be called with it adjusted.
1577 * Initialize all nonzero fields of a descriptor.
1580 bpf_allocbufs(struct bpf_d
*d
)
1583 d
->bd_fbuf
= malloc(d
->bd_bufsize
, M_DEVBUF
, M_NOWAIT
);
1586 d
->bd_sbuf
= malloc(d
->bd_bufsize
, M_DEVBUF
, M_NOWAIT
);
1588 free(d
->bd_fbuf
, M_DEVBUF
);
1597 * Free buffers currently in use by a descriptor.
1601 bpf_freed(struct bpf_d
*d
)
1604 * We don't need to lock out interrupts since this descriptor has
1605 * been detached from its interface and it yet hasn't been marked
1608 if (d
->bd_sbuf
!= 0) {
1609 free(d
->bd_sbuf
, M_DEVBUF
);
1610 if (d
->bd_hbuf
!= 0)
1611 free(d
->bd_hbuf
, M_DEVBUF
);
1612 if (d
->bd_fbuf
!= 0)
1613 free(d
->bd_fbuf
, M_DEVBUF
);
1616 free(d
->bd_filter
, M_DEVBUF
);
1620 * Attach an interface to bpf. dlt is the link layer type; hdrlen is the
1621 * fixed size of the link header (variable length headers not yet supported).
1624 bpfattach(struct ifnet
*ifp
, u_int dlt
, u_int hdrlen
)
1627 bpfattach2(ifp
, dlt
, hdrlen
, &ifp
->if_bpf
);
1631 * Attach additional dlt for a interface to bpf. dlt is the link layer type;
1632 * hdrlen is the fixed size of the link header for the specified dlt
1633 * (variable length headers not yet supported).
1636 bpfattach2(struct ifnet
*ifp
, u_int dlt
, u_int hdrlen
, void *driverp
)
1639 bp
= malloc(sizeof(*bp
), M_DEVBUF
, M_DONTWAIT
);
1644 bp
->bif_driverp
= driverp
;
1648 bp
->bif_next
= bpf_iflist
;
1651 *bp
->bif_driverp
= 0;
1654 * Compute the length of the bpf header. This is not necessarily
1655 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1656 * that the network layer header begins on a longword boundary (for
1657 * performance reasons and to alleviate alignment restrictions).
1659 bp
->bif_hdrlen
= BPF_WORDALIGN(hdrlen
+ SIZEOF_BPF_HDR
) - hdrlen
;
1662 printf("bpf: %s attached\n", ifp
->if_xname
);
1667 * Remove an interface from bpf.
1670 bpfdetach(struct ifnet
*ifp
)
1672 struct bpf_if
*bp
, **pbp
;
1676 /* Nuke the vnodes for any open instances */
1677 LIST_FOREACH(d
, &bpf_list
, bd_list
) {
1678 if (d
->bd_bif
!= NULL
&& d
->bd_bif
->bif_ifp
== ifp
) {
1680 * Detach the descriptor from an interface now.
1681 * It will be free'ed later by close routine.
1684 d
->bd_promisc
= 0; /* we can't touch device. */
1691 for (bp
= bpf_iflist
, pbp
= &bpf_iflist
;
1692 bp
!= NULL
; pbp
= &bp
->bif_next
, bp
= bp
->bif_next
) {
1693 if (bp
->bif_ifp
== ifp
) {
1694 *pbp
= bp
->bif_next
;
1702 * Change the data link type of a interface.
1705 bpf_change_type(struct ifnet
*ifp
, u_int dlt
, u_int hdrlen
)
1709 for (bp
= bpf_iflist
; bp
!= NULL
; bp
= bp
->bif_next
) {
1710 if ((void **)bp
->bif_driverp
== &ifp
->if_bpf
)
1714 panic("bpf_change_type");
1719 * Compute the length of the bpf header. This is not necessarily
1720 * equal to SIZEOF_BPF_HDR because we want to insert spacing such
1721 * that the network layer header begins on a longword boundary (for
1722 * performance reasons and to alleviate alignment restrictions).
1724 bp
->bif_hdrlen
= BPF_WORDALIGN(hdrlen
+ SIZEOF_BPF_HDR
) - hdrlen
;
1728 * Get a list of available data link type of the interface.
1731 bpf_getdltlist(struct bpf_d
*d
, struct bpf_dltlist
*bfl
)
1737 ifp
= d
->bd_bif
->bif_ifp
;
1740 for (bp
= bpf_iflist
; bp
!= NULL
; bp
= bp
->bif_next
) {
1741 if (bp
->bif_ifp
!= ifp
)
1743 if (bfl
->bfl_list
!= NULL
) {
1744 if (n
>= bfl
->bfl_len
)
1746 error
= copyout(&bp
->bif_dlt
,
1747 bfl
->bfl_list
+ n
, sizeof(u_int
));
1756 * Set the data link type of a BPF instance.
1759 bpf_setdlt(struct bpf_d
*d
, u_int dlt
)
1761 int s
, error
, opromisc
;
1765 if (d
->bd_bif
->bif_dlt
== dlt
)
1767 ifp
= d
->bd_bif
->bif_ifp
;
1768 for (bp
= bpf_iflist
; bp
!= NULL
; bp
= bp
->bif_next
) {
1769 if (bp
->bif_ifp
== ifp
&& bp
->bif_dlt
== dlt
)
1775 opromisc
= d
->bd_promisc
;
1780 error
= ifpromisc(bp
->bif_ifp
, 1);
1782 printf("%s: bpf_setdlt: ifpromisc failed (%d)\n",
1783 bp
->bif_ifp
->if_xname
, error
);
1792 sysctl_net_bpf_maxbufsize(SYSCTLFN_ARGS
)
1795 struct sysctlnode node
;
1798 node
.sysctl_data
= &newsize
;
1799 newsize
= bpf_maxbufsize
;
1800 error
= sysctl_lookup(SYSCTLFN_CALL(&node
));
1801 if (error
|| newp
== NULL
)
1804 if (newsize
< BPF_MINBUFSIZE
|| newsize
> BPF_MAXBUFSIZE
)
1807 bpf_maxbufsize
= newsize
;
1813 sysctl_net_bpf_peers(SYSCTLFN_ARGS
)
1815 int error
, elem_count
;
1817 struct bpf_d_ext dpe
;
1818 size_t len
, needed
, elem_size
, out_size
;
1821 if (namelen
== 1 && name
[0] == CTL_QUERY
)
1822 return (sysctl_query(SYSCTLFN_CALL(rnode
)));
1827 /* BPF peers is privileged information. */
1828 error
= kauth_authorize_network(l
->l_cred
, KAUTH_NETWORK_INTERFACE
,
1829 KAUTH_REQ_NETWORK_INTERFACE_GETPRIV
, NULL
, NULL
, NULL
);
1833 len
= (oldp
!= NULL
) ? *oldlenp
: 0;
1835 elem_size
= name
[0];
1836 elem_count
= name
[1];
1837 out_size
= MIN(sizeof(dpe
), elem_size
);
1840 if (elem_size
< 1 || elem_count
< 0)
1843 mutex_enter(&bpf_mtx
);
1844 LIST_FOREACH(dp
, &bpf_list
, bd_list
) {
1845 if (len
>= elem_size
&& elem_count
> 0) {
1846 #define BPF_EXT(field) dpe.bde_ ## field = dp->bd_ ## field
1860 (void)strlcpy(dpe
.bde_ifname
,
1861 dp
->bd_bif
->bif_ifp
->if_xname
,
1864 dpe
.bde_ifname
[0] = '\0';
1866 error
= copyout(&dpe
, sp
, out_size
);
1872 needed
+= elem_size
;
1873 if (elem_count
> 0 && elem_count
!= INT_MAX
)
1876 mutex_exit(&bpf_mtx
);
1883 SYSCTL_SETUP(sysctl_net_bpf_setup
, "sysctl net.bpf subtree setup")
1885 const struct sysctlnode
*node
;
1887 sysctl_createv(clog
, 0, NULL
, NULL
,
1889 CTLTYPE_NODE
, "net", NULL
,
1894 sysctl_createv(clog
, 0, NULL
, &node
,
1896 CTLTYPE_NODE
, "bpf",
1897 SYSCTL_DESCR("BPF options"),
1899 CTL_NET
, CTL_CREATE
, CTL_EOL
);
1901 sysctl_createv(clog
, 0, NULL
, NULL
,
1902 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
,
1903 CTLTYPE_INT
, "maxbufsize",
1904 SYSCTL_DESCR("Maximum size for data capture buffer"),
1905 sysctl_net_bpf_maxbufsize
, 0, &bpf_maxbufsize
, 0,
1906 CTL_NET
, node
->sysctl_num
, CTL_CREATE
, CTL_EOL
);
1907 sysctl_createv(clog
, 0, NULL
, NULL
,
1909 CTLTYPE_STRUCT
, "stats",
1910 SYSCTL_DESCR("BPF stats"),
1911 NULL
, 0, &bpf_gstats
, sizeof(bpf_gstats
),
1912 CTL_NET
, node
->sysctl_num
, CTL_CREATE
, CTL_EOL
);
1913 sysctl_createv(clog
, 0, NULL
, NULL
,
1915 CTLTYPE_STRUCT
, "peers",
1916 SYSCTL_DESCR("BPF peers"),
1917 sysctl_net_bpf_peers
, 0, NULL
, 0,
1918 CTL_NET
, node
->sysctl_num
, CTL_CREATE
, CTL_EOL
);