2 * Copyright (c) 1993, 1994, 1995, 1996, 1998
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
26 #include <sys/param.h> /* optionally get BSD define */
27 #include <sys/socket.h>
30 * <net/bpf.h> defines ioctls, but doesn't include <sys/ioccom.h>.
32 * We include <sys/ioctl.h> as it might be necessary to declare ioctl();
33 * at least on *BSD and Mac OS X, it also defines various SIOC ioctls -
34 * we could include <sys/sockio.h>, but if we're already including
35 * <sys/ioctl.h>, which includes <sys/sockio.h> on those platforms,
36 * there's not much point in doing so.
38 * If we have <sys/ioccom.h>, we include it as well, to handle systems
39 * such as Solaris which don't arrange to include <sys/ioccom.h> if you
40 * include <sys/ioctl.h>
42 #include <sys/ioctl.h>
43 #ifdef HAVE_SYS_IOCCOM_H
44 #include <sys/ioccom.h>
46 #include <sys/utsname.h>
48 #if defined(__FreeBSD__) && defined(SIOCIFCREATE2)
50 * Add support for capturing on FreeBSD usbusN interfaces.
52 static const char usbus_prefix
[] = "usbus";
53 #define USBUS_PREFIX_LEN (sizeof(usbus_prefix) - 1)
62 * Make "pcap.h" not include "pcap/bpf.h"; we are going to include the
63 * native OS version, as we need "struct bpf_config" from it.
65 #define PCAP_DONT_INCLUDE_PCAP_BPF_H
67 #include <sys/types.h>
70 * Prevent bpf.h from redefining the DLT_ values to their
71 * IFT_ values, as we're going to return the standard libpcap
72 * values, not IBM's non-standard IFT_ values.
79 * If both BIOCROTZBUF and BPF_BUFMODE_ZBUF are defined, we have
82 #if defined(BIOCROTZBUF) && defined(BPF_BUFMODE_ZBUF)
83 #define HAVE_ZEROCOPY_BPF
85 #include <machine/atomic.h>
88 #include <net/if_types.h> /* for IFT_ values */
89 #include <sys/sysconfig.h>
90 #include <sys/device.h>
91 #include <sys/cfgodm.h>
95 #define domakedev makedev64
96 #define getmajor major64
97 #define bpf_hdr bpf_hdr32
99 #define domakedev makedev
100 #define getmajor major
101 #endif /* __64BIT__ */
103 #define BPF_NAME "bpf"
105 #define DRIVER_PATH "/usr/lib/drivers"
106 #define BPF_NODE "/dev/bpf"
107 static int bpfloadedflag
= 0;
108 static int odmlockid
= 0;
110 static int bpf_load(char *errbuf
);
127 #ifdef HAVE_NET_IF_MEDIA_H
128 # include <net/if_media.h>
131 #include "pcap-int.h"
133 #ifdef HAVE_OS_PROTO_H
134 #include "os-proto.h"
138 * Later versions of NetBSD stick padding in front of FDDI frames
139 * to align the IP header on a 4-byte boundary.
141 #if defined(__NetBSD__) && __NetBSD_Version__ > 106000000
142 #define PCAP_FDDIPAD 3
146 * Private data for capturing on BPF devices.
149 #ifdef HAVE_ZEROCOPY_BPF
151 * Zero-copy read buffer -- for zero-copy BPF. 'buffer' above will
152 * alternative between these two actual mmap'd buffers as required.
153 * As there is a header on the front size of the mmap'd buffer, only
154 * some of the buffer is exposed to libpcap as a whole via bufsize;
155 * zbufsize is the true size. zbuffer tracks the current zbuf
156 * assocated with buffer so that it can be used to decide which the
157 * next buffer to read will be.
159 u_char
*zbuf1
, *zbuf2
, *zbuffer
;
163 struct timespec firstsel
;
165 * If there's currently a buffer being actively processed, then it is
166 * referenced here; 'buffer' is also pointed at it, but offset by the
167 * size of the header.
169 struct bpf_zbuf_header
*bzh
;
170 int nonblock
; /* true if in nonblocking mode */
171 #endif /* HAVE_ZEROCOPY_BPF */
173 char *device
; /* device name */
174 int filtering_in_kernel
; /* using kernel filter */
175 int must_do_on_close
; /* stuff we must do when we close */
179 * Stuff to do when we close.
181 #define MUST_CLEAR_RFMON 0x00000001 /* clear rfmon (monitor) mode */
182 #define MUST_DESTROY_USBUS 0x00000002 /* destroy usbusN interface */
185 # if (defined(HAVE_NET_IF_MEDIA_H) && defined(IFM_IEEE80211)) && !defined(__APPLE__)
186 #define HAVE_BSD_IEEE80211
189 * The ifm_ulist member of a struct ifmediareq is an int * on most systems,
190 * but it's a uint64_t on newer versions of OpenBSD.
192 * We check this by checking whether IFM_GMASK is defined and > 2^32-1.
194 # if defined(IFM_GMASK) && IFM_GMASK > 0xFFFFFFFF
195 # define IFM_ULIST_TYPE uint64_t
197 # define IFM_ULIST_TYPE int
201 # if defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)
202 static int find_802_11(struct bpf_dltlist
*);
204 # ifdef HAVE_BSD_IEEE80211
205 static int monitor_mode(pcap_t
*, int);
208 # if defined(__APPLE__)
209 static void remove_en(pcap_t
*);
210 static void remove_802_11(pcap_t
*);
213 # endif /* defined(__APPLE__) || defined(HAVE_BSD_IEEE80211) */
215 #endif /* BIOCGDLTLIST */
217 #if defined(sun) && defined(LIFNAMSIZ) && defined(lifr_zoneid)
222 * We include the OS's <net/bpf.h>, not our "pcap/bpf.h", so we probably
223 * don't get DLT_DOCSIS defined.
226 #define DLT_DOCSIS 143
230 * On OS X, we don't even get any of the 802.11-plus-radio-header DLT_'s
231 * defined, even though some of them are used by various Airport drivers.
233 #ifndef DLT_PRISM_HEADER
234 #define DLT_PRISM_HEADER 119
236 #ifndef DLT_AIRONET_HEADER
237 #define DLT_AIRONET_HEADER 120
239 #ifndef DLT_IEEE802_11_RADIO
240 #define DLT_IEEE802_11_RADIO 127
242 #ifndef DLT_IEEE802_11_RADIO_AVS
243 #define DLT_IEEE802_11_RADIO_AVS 163
246 static int pcap_can_set_rfmon_bpf(pcap_t
*p
);
247 static int pcap_activate_bpf(pcap_t
*p
);
248 static int pcap_setfilter_bpf(pcap_t
*p
, struct bpf_program
*fp
);
249 static int pcap_setdirection_bpf(pcap_t
*, pcap_direction_t
);
250 static int pcap_set_datalink_bpf(pcap_t
*p
, int dlt
);
253 * For zerocopy bpf, the setnonblock/getnonblock routines need to modify
254 * pb->nonblock so we don't call select(2) if the pcap handle is in non-
258 pcap_getnonblock_bpf(pcap_t
*p
)
260 #ifdef HAVE_ZEROCOPY_BPF
261 struct pcap_bpf
*pb
= p
->priv
;
264 return (pb
->nonblock
);
266 return (pcap_getnonblock_fd(p
));
270 pcap_setnonblock_bpf(pcap_t
*p
, int nonblock
)
272 #ifdef HAVE_ZEROCOPY_BPF
273 struct pcap_bpf
*pb
= p
->priv
;
276 pb
->nonblock
= nonblock
;
280 return (pcap_setnonblock_fd(p
, nonblock
));
283 #ifdef HAVE_ZEROCOPY_BPF
285 * Zero-copy BPF buffer routines to check for and acknowledge BPF data in
286 * shared memory buffers.
288 * pcap_next_zbuf_shm(): Check for a newly available shared memory buffer,
289 * and set up p->buffer and cc to reflect one if available. Notice that if
290 * there was no prior buffer, we select zbuf1 as this will be the first
291 * buffer filled for a fresh BPF session.
294 pcap_next_zbuf_shm(pcap_t
*p
, int *cc
)
296 struct pcap_bpf
*pb
= p
->priv
;
297 struct bpf_zbuf_header
*bzh
;
299 if (pb
->zbuffer
== pb
->zbuf2
|| pb
->zbuffer
== NULL
) {
300 bzh
= (struct bpf_zbuf_header
*)pb
->zbuf1
;
301 if (bzh
->bzh_user_gen
!=
302 atomic_load_acq_int(&bzh
->bzh_kernel_gen
)) {
304 pb
->zbuffer
= (u_char
*)pb
->zbuf1
;
305 p
->buffer
= pb
->zbuffer
+ sizeof(*bzh
);
306 *cc
= bzh
->bzh_kernel_len
;
309 } else if (pb
->zbuffer
== pb
->zbuf1
) {
310 bzh
= (struct bpf_zbuf_header
*)pb
->zbuf2
;
311 if (bzh
->bzh_user_gen
!=
312 atomic_load_acq_int(&bzh
->bzh_kernel_gen
)) {
314 pb
->zbuffer
= (u_char
*)pb
->zbuf2
;
315 p
->buffer
= pb
->zbuffer
+ sizeof(*bzh
);
316 *cc
= bzh
->bzh_kernel_len
;
325 * pcap_next_zbuf() -- Similar to pcap_next_zbuf_shm(), except wait using
326 * select() for data or a timeout, and possibly force rotation of the buffer
327 * in the event we time out or are in immediate mode. Invoke the shared
328 * memory check before doing system calls in order to avoid doing avoidable
332 pcap_next_zbuf(pcap_t
*p
, int *cc
)
334 struct pcap_bpf
*pb
= p
->priv
;
342 #define TSTOMILLI(ts) (((ts)->tv_sec * 1000) + ((ts)->tv_nsec / 1000000))
344 * Start out by seeing whether anything is waiting by checking the
345 * next shared memory buffer for data.
347 data
= pcap_next_zbuf_shm(p
, cc
);
351 * If a previous sleep was interrupted due to signal delivery, make
352 * sure that the timeout gets adjusted accordingly. This requires
353 * that we analyze when the timeout should be been expired, and
354 * subtract the current time from that. If after this operation,
355 * our timeout is less then or equal to zero, handle it like a
358 tmout
= p
->opt
.timeout
;
360 (void) clock_gettime(CLOCK_MONOTONIC
, &cur
);
361 if (pb
->interrupted
&& p
->opt
.timeout
) {
362 expire
= TSTOMILLI(&pb
->firstsel
) + p
->opt
.timeout
;
363 tmout
= expire
- TSTOMILLI(&cur
);
367 data
= pcap_next_zbuf_shm(p
, cc
);
370 if (ioctl(p
->fd
, BIOCROTZBUF
, &bz
) < 0) {
371 (void) pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
372 "BIOCROTZBUF: %s", strerror(errno
));
375 return (pcap_next_zbuf_shm(p
, cc
));
379 * No data in the buffer, so must use select() to wait for data or
380 * the next timeout. Note that we only call select if the handle
381 * is in blocking mode.
385 FD_SET(p
->fd
, &r_set
);
387 tv
.tv_sec
= tmout
/ 1000;
388 tv
.tv_usec
= (tmout
* 1000) % 1000000;
390 r
= select(p
->fd
+ 1, &r_set
, NULL
, NULL
,
391 p
->opt
.timeout
!= 0 ? &tv
: NULL
);
392 if (r
< 0 && errno
== EINTR
) {
393 if (!pb
->interrupted
&& p
->opt
.timeout
) {
399 (void) pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
400 "select: %s", strerror(errno
));
406 * Check again for data, which may exist now that we've either been
407 * woken up as a result of data or timed out. Try the "there's data"
408 * case first since it doesn't require a system call.
410 data
= pcap_next_zbuf_shm(p
, cc
);
414 * Try forcing a buffer rotation to dislodge timed out or immediate
417 if (ioctl(p
->fd
, BIOCROTZBUF
, &bz
) < 0) {
418 (void) pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
419 "BIOCROTZBUF: %s", strerror(errno
));
422 return (pcap_next_zbuf_shm(p
, cc
));
426 * Notify kernel that we are done with the buffer. We don't reset zbuffer so
427 * that we know which buffer to use next time around.
430 pcap_ack_zbuf(pcap_t
*p
)
432 struct pcap_bpf
*pb
= p
->priv
;
434 atomic_store_rel_int(&pb
->bzh
->bzh_user_gen
,
435 pb
->bzh
->bzh_kernel_gen
);
440 #endif /* HAVE_ZEROCOPY_BPF */
443 pcap_create_interface(const char *device _U_
, char *ebuf
)
447 p
= pcap_create_common(ebuf
, sizeof (struct pcap_bpf
));
451 p
->activate_op
= pcap_activate_bpf
;
452 p
->can_set_rfmon_op
= pcap_can_set_rfmon_bpf
;
455 * We claim that we support microsecond and nanosecond time
458 p
->tstamp_precision_count
= 2;
459 p
->tstamp_precision_list
= malloc(2 * sizeof(u_int
));
460 if (p
->tstamp_precision_list
== NULL
) {
461 pcap_snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
462 pcap_strerror(errno
));
466 p
->tstamp_precision_list
[0] = PCAP_TSTAMP_PRECISION_MICRO
;
467 p
->tstamp_precision_list
[1] = PCAP_TSTAMP_PRECISION_NANO
;
468 #endif /* BIOCSTSTAMP */
473 * On success, returns a file descriptor for a BPF device.
474 * On failure, returns a PCAP_ERROR_ value, and sets p->errbuf.
477 bpf_open(char *errbuf
)
480 #ifdef HAVE_CLONING_BPF
481 static const char device
[] = "/dev/bpf";
484 char device
[sizeof "/dev/bpf0000000000"];
489 * Load the bpf driver, if it isn't already loaded,
490 * and create the BPF device entries, if they don't
493 if (bpf_load(errbuf
) == PCAP_ERROR
)
497 #ifdef HAVE_CLONING_BPF
498 if ((fd
= open(device
, O_RDWR
)) == -1 &&
499 (errno
!= EACCES
|| (fd
= open(device
, O_RDONLY
)) == -1)) {
501 fd
= PCAP_ERROR_PERM_DENIED
;
504 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
505 "(cannot open device) %s: %s", device
, pcap_strerror(errno
));
509 * Go through all the minors and find one that isn't in use.
512 (void)pcap_snprintf(device
, sizeof(device
), "/dev/bpf%d", n
++);
514 * Initially try a read/write open (to allow the inject
515 * method to work). If that fails due to permission
516 * issues, fall back to read-only. This allows a
517 * non-root user to be granted specific access to pcap
518 * capabilities via file permissions.
520 * XXX - we should have an API that has a flag that
521 * controls whether to open read-only or read-write,
522 * so that denial of permission to send (or inability
523 * to send, if sending packets isn't supported on
524 * the device in question) can be indicated at open
527 fd
= open(device
, O_RDWR
);
528 if (fd
== -1 && errno
== EACCES
)
529 fd
= open(device
, O_RDONLY
);
530 } while (fd
< 0 && errno
== EBUSY
);
533 * XXX better message for all minors used
542 * /dev/bpf0 doesn't exist, which
543 * means we probably have no BPF
546 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
547 "(there are no BPF devices)");
550 * We got EBUSY on at least one
551 * BPF device, so we have BPF
552 * devices, but all the ones
553 * that exist are busy.
555 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
556 "(all BPF devices are busy)");
562 * Got EACCES on the last device we tried,
563 * and EBUSY on all devices before that,
566 fd
= PCAP_ERROR_PERM_DENIED
;
567 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
568 "(cannot open BPF device) %s: %s", device
,
569 pcap_strerror(errno
));
574 * Some other problem.
577 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
578 "(cannot open BPF device) %s: %s", device
,
579 pcap_strerror(errno
));
589 * Open and bind to a device; used if we're not actually going to use
590 * the device, but are just testing whether it can be opened, or opening
591 * it to get information about it.
593 * Returns an error code on failure (always negative), and an FD for
594 * the now-bound BPF device on success (always non-negative).
597 bpf_open_and_bind(const char *name
, char *errbuf
)
603 * First, open a BPF device.
605 fd
= bpf_open(errbuf
);
607 return (fd
); /* fd is the appropriate error code */
610 * Now bind to the device.
612 (void)strncpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
613 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) < 0) {
618 * There's no such device.
621 return (PCAP_ERROR_NO_SUCH_DEVICE
);
625 * Return a "network down" indication, so that
626 * the application can report that rather than
627 * saying we had a mysterious failure and
628 * suggest that they report a problem to the
629 * libpcap developers.
632 return (PCAP_ERROR_IFACE_NOT_UP
);
635 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
636 "BIOCSETIF: %s: %s", name
, pcap_strerror(errno
));
650 get_dlt_list(int fd
, int v
, struct bpf_dltlist
*bdlp
, char *ebuf
)
652 memset(bdlp
, 0, sizeof(*bdlp
));
653 if (ioctl(fd
, BIOCGDLTLIST
, (caddr_t
)bdlp
) == 0) {
657 bdlp
->bfl_list
= (u_int
*) malloc(sizeof(u_int
) * (bdlp
->bfl_len
+ 1));
658 if (bdlp
->bfl_list
== NULL
) {
659 (void)pcap_snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
660 pcap_strerror(errno
));
664 if (ioctl(fd
, BIOCGDLTLIST
, (caddr_t
)bdlp
) < 0) {
665 (void)pcap_snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
666 "BIOCGDLTLIST: %s", pcap_strerror(errno
));
667 free(bdlp
->bfl_list
);
672 * OK, for real Ethernet devices, add DLT_DOCSIS to the
673 * list, so that an application can let you choose it,
674 * in case you're capturing DOCSIS traffic that a Cisco
675 * Cable Modem Termination System is putting out onto
676 * an Ethernet (it doesn't put an Ethernet header onto
677 * the wire, it puts raw DOCSIS frames out on the wire
678 * inside the low-level Ethernet framing).
680 * A "real Ethernet device" is defined here as a device
681 * that has a link-layer type of DLT_EN10MB and that has
682 * no alternate link-layer types; that's done to exclude
683 * 802.11 interfaces (which might or might not be the
684 * right thing to do, but I suspect it is - Ethernet <->
685 * 802.11 bridges would probably badly mishandle frames
686 * that don't have Ethernet headers).
688 * On Solaris with BPF, Ethernet devices also offer
689 * DLT_IPNET, so we, if DLT_IPNET is defined, we don't
690 * treat it as an indication that the device isn't an
693 if (v
== DLT_EN10MB
) {
695 for (i
= 0; i
< bdlp
->bfl_len
; i
++) {
696 if (bdlp
->bfl_list
[i
] != DLT_EN10MB
698 && bdlp
->bfl_list
[i
] != DLT_IPNET
707 * We reserved one more slot at the end of
710 bdlp
->bfl_list
[bdlp
->bfl_len
] = DLT_DOCSIS
;
716 * EINVAL just means "we don't support this ioctl on
717 * this device"; don't treat it as an error.
719 if (errno
!= EINVAL
) {
720 (void)pcap_snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
721 "BIOCGDLTLIST: %s", pcap_strerror(errno
));
730 pcap_can_set_rfmon_bpf(pcap_t
*p
)
732 #if defined(__APPLE__)
733 struct utsname osinfo
;
737 struct bpf_dltlist bdl
;
741 * The joys of monitor mode on OS X.
743 * Prior to 10.4, it's not supported at all.
745 * In 10.4, if adapter enN supports monitor mode, there's a
746 * wltN adapter corresponding to it; you open it, instead of
747 * enN, to get monitor mode. You get whatever link-layer
748 * headers it supplies.
750 * In 10.5, and, we assume, later releases, if adapter enN
751 * supports monitor mode, it offers, among its selectable
752 * DLT_ values, values that let you get the 802.11 header;
753 * selecting one of those values puts the adapter into monitor
754 * mode (i.e., you can't get 802.11 headers except in monitor
755 * mode, and you can't get Ethernet headers in monitor mode).
757 if (uname(&osinfo
) == -1) {
759 * Can't get the OS version; just say "no".
764 * We assume osinfo.sysname is "Darwin", because
765 * __APPLE__ is defined. We just check the version.
767 if (osinfo
.release
[0] < '8' && osinfo
.release
[1] == '.') {
769 * 10.3 (Darwin 7.x) or earlier.
770 * Monitor mode not supported.
774 if (osinfo
.release
[0] == '8' && osinfo
.release
[1] == '.') {
776 * 10.4 (Darwin 8.x). s/en/wlt/, and check
777 * whether the device exists.
779 if (strncmp(p
->opt
.device
, "en", 2) != 0) {
781 * Not an enN device; no monitor mode.
785 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
787 (void)pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
788 "socket: %s", pcap_strerror(errno
));
791 strlcpy(ifr
.ifr_name
, "wlt", sizeof(ifr
.ifr_name
));
792 strlcat(ifr
.ifr_name
, p
->opt
.device
+ 2, sizeof(ifr
.ifr_name
));
793 if (ioctl(fd
, SIOCGIFFLAGS
, (char *)&ifr
) < 0) {
806 * Everything else is 10.5 or later; for those,
807 * we just open the enN device, and check whether
808 * we have any 802.11 devices.
810 * First, open a BPF device.
812 fd
= bpf_open(p
->errbuf
);
814 return (fd
); /* fd is the appropriate error code */
817 * Now bind to the device.
819 (void)strncpy(ifr
.ifr_name
, p
->opt
.device
, sizeof(ifr
.ifr_name
));
820 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) < 0) {
825 * There's no such device.
828 return (PCAP_ERROR_NO_SUCH_DEVICE
);
832 * Return a "network down" indication, so that
833 * the application can report that rather than
834 * saying we had a mysterious failure and
835 * suggest that they report a problem to the
836 * libpcap developers.
839 return (PCAP_ERROR_IFACE_NOT_UP
);
842 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
844 p
->opt
.device
, pcap_strerror(errno
));
851 * We know the default link type -- now determine all the DLTs
852 * this interface supports. If this fails with EINVAL, it's
853 * not fatal; we just don't get to use the feature later.
854 * (We don't care about DLT_DOCSIS, so we pass DLT_NULL
855 * as the default DLT for this adapter.)
857 if (get_dlt_list(fd
, DLT_NULL
, &bdl
, p
->errbuf
) == PCAP_ERROR
) {
861 if (find_802_11(&bdl
) != -1) {
863 * We have an 802.11 DLT, so we can set monitor mode.
871 #endif /* BIOCGDLTLIST */
873 #elif defined(HAVE_BSD_IEEE80211)
876 ret
= monitor_mode(p
, 0);
877 if (ret
== PCAP_ERROR_RFMON_NOTSUP
)
878 return (0); /* not an error, just a "can't do" */
880 return (1); /* success */
888 pcap_stats_bpf(pcap_t
*p
, struct pcap_stat
*ps
)
893 * "ps_recv" counts packets handed to the filter, not packets
894 * that passed the filter. This includes packets later dropped
895 * because we ran out of buffer space.
897 * "ps_drop" counts packets dropped inside the BPF device
898 * because we ran out of buffer space. It doesn't count
899 * packets dropped by the interface driver. It counts
900 * only packets that passed the filter.
902 * Both statistics include packets not yet read from the kernel
903 * by libpcap, and thus not yet seen by the application.
905 if (ioctl(p
->fd
, BIOCGSTATS
, (caddr_t
)&s
) < 0) {
906 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCGSTATS: %s",
907 pcap_strerror(errno
));
911 ps
->ps_recv
= s
.bs_recv
;
912 ps
->ps_drop
= s
.bs_drop
;
918 pcap_read_bpf(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
920 struct pcap_bpf
*pb
= p
->priv
;
923 register u_char
*bp
, *ep
;
928 #ifdef HAVE_ZEROCOPY_BPF
934 * Has "pcap_breakloop()" been called?
938 * Yes - clear the flag that indicates that it
939 * has, and return PCAP_ERROR_BREAK to indicate
940 * that we were told to break out of the loop.
943 return (PCAP_ERROR_BREAK
);
948 * When reading without zero-copy from a file descriptor, we
949 * use a single buffer and return a length of data in the
950 * buffer. With zero-copy, we update the p->buffer pointer
951 * to point at whatever underlying buffer contains the next
952 * data and update cc to reflect the data found in the
955 #ifdef HAVE_ZEROCOPY_BPF
957 if (p
->buffer
!= NULL
)
959 i
= pcap_next_zbuf(p
, &cc
);
967 cc
= read(p
->fd
, p
->buffer
, p
->bufsize
);
970 /* Don't choke when we get ptraced */
979 * Sigh. More AIX wonderfulness.
981 * For some unknown reason the uiomove()
982 * operation in the bpf kernel extension
983 * used to copy the buffer into user
984 * space sometimes returns EFAULT. I have
985 * no idea why this is the case given that
986 * a kernel debugger shows the user buffer
987 * is correct. This problem appears to
988 * be mostly mitigated by the memset of
989 * the buffer before it is first used.
990 * Very strange.... Shaun Clowes
992 * In any case this means that we shouldn't
993 * treat EFAULT as a fatal error; as we
994 * don't have an API for returning
995 * a "some packets were dropped since
996 * the last packet you saw" indication,
997 * we just ignore EFAULT and keep reading.
1007 * The device on which we're capturing
1010 * XXX - we should really return
1011 * PCAP_ERROR_IFACE_NOT_UP, but
1012 * pcap_dispatch() etc. aren't
1013 * defined to retur that.
1015 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1016 "The interface went down");
1017 return (PCAP_ERROR
);
1019 #if defined(sun) && !defined(BSD) && !defined(__svr4__) && !defined(__SVR4)
1021 * Due to a SunOS bug, after 2^31 bytes, the kernel
1022 * file offset overflows and read fails with EINVAL.
1023 * The lseek() to 0 will fix things.
1026 if (lseek(p
->fd
, 0L, SEEK_CUR
) +
1028 (void)lseek(p
->fd
, 0L, SEEK_SET
);
1034 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "read: %s",
1035 pcap_strerror(errno
));
1036 return (PCAP_ERROR
);
1038 bp
= (u_char
*)p
->buffer
;
1043 * Loop through each packet.
1046 #define bhp ((struct bpf_xhdr *)bp)
1048 #define bhp ((struct bpf_hdr *)bp)
1055 register u_int caplen
, hdrlen
;
1058 * Has "pcap_breakloop()" been called?
1059 * If so, return immediately - if we haven't read any
1060 * packets, clear the flag and return PCAP_ERROR_BREAK
1061 * to indicate that we were told to break out of the loop,
1062 * otherwise leave the flag set, so that the *next* call
1063 * will break out of the loop without having read any
1064 * packets, and return the number of packets we've
1067 if (p
->break_loop
) {
1071 * ep is set based on the return value of read(),
1072 * but read() from a BPF device doesn't necessarily
1073 * return a value that's a multiple of the alignment
1074 * value for BPF_WORDALIGN(). However, whenever we
1075 * increment bp, we round up the increment value by
1076 * a value rounded up by BPF_WORDALIGN(), so we
1077 * could increment bp past ep after processing the
1078 * last packet in the buffer.
1080 * We treat ep < bp as an indication that this
1081 * happened, and just set p->cc to 0.
1087 return (PCAP_ERROR_BREAK
);
1092 caplen
= bhp
->bh_caplen
;
1093 hdrlen
= bhp
->bh_hdrlen
;
1094 datap
= bp
+ hdrlen
;
1096 * Short-circuit evaluation: if using BPF filter
1097 * in kernel, no need to do it now - we already know
1098 * the packet passed the filter.
1101 * Note: the filter code was generated assuming
1102 * that p->fddipad was the amount of padding
1103 * before the header, as that's what's required
1104 * in the kernel, so we run the filter before
1105 * skipping that padding.
1108 if (pb
->filtering_in_kernel
||
1109 bpf_filter(p
->fcode
.bf_insns
, datap
, bhp
->bh_datalen
, caplen
)) {
1110 struct pcap_pkthdr pkthdr
;
1114 bt
.sec
= bhp
->bh_tstamp
.bt_sec
;
1115 bt
.frac
= bhp
->bh_tstamp
.bt_frac
;
1116 if (p
->opt
.tstamp_precision
== PCAP_TSTAMP_PRECISION_NANO
) {
1119 bintime2timespec(&bt
, &ts
);
1120 pkthdr
.ts
.tv_sec
= ts
.tv_sec
;
1121 pkthdr
.ts
.tv_usec
= ts
.tv_nsec
;
1125 bintime2timeval(&bt
, &tv
);
1126 pkthdr
.ts
.tv_sec
= tv
.tv_sec
;
1127 pkthdr
.ts
.tv_usec
= tv
.tv_usec
;
1130 pkthdr
.ts
.tv_sec
= bhp
->bh_tstamp
.tv_sec
;
1133 * AIX's BPF returns seconds/nanoseconds time
1134 * stamps, not seconds/microseconds time stamps.
1136 pkthdr
.ts
.tv_usec
= bhp
->bh_tstamp
.tv_usec
/1000;
1138 pkthdr
.ts
.tv_usec
= bhp
->bh_tstamp
.tv_usec
;
1140 #endif /* BIOCSTSTAMP */
1143 pkthdr
.caplen
= caplen
- pad
;
1146 if (bhp
->bh_datalen
> pad
)
1147 pkthdr
.len
= bhp
->bh_datalen
- pad
;
1152 pkthdr
.caplen
= caplen
;
1153 pkthdr
.len
= bhp
->bh_datalen
;
1155 (*callback
)(user
, &pkthdr
, datap
);
1156 bp
+= BPF_WORDALIGN(caplen
+ hdrlen
);
1157 if (++n
>= cnt
&& !PACKET_COUNT_IS_UNLIMITED(cnt
)) {
1161 * See comment above about p->cc < 0.
1171 bp
+= BPF_WORDALIGN(caplen
+ hdrlen
);
1180 pcap_inject_bpf(pcap_t
*p
, const void *buf
, size_t size
)
1184 ret
= write(p
->fd
, buf
, size
);
1186 if (ret
== -1 && errno
== EAFNOSUPPORT
) {
1188 * In Mac OS X, there's a bug wherein setting the
1189 * BIOCSHDRCMPLT flag causes writes to fail; see,
1192 * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/BIOCSHDRCMPLT-10.3.3.patch
1194 * So, if, on OS X, we get EAFNOSUPPORT from the write, we
1195 * assume it's due to that bug, and turn off that flag
1196 * and try again. If we succeed, it either means that
1197 * somebody applied the fix from that URL, or other patches
1200 * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/
1202 * and are running a Darwin kernel with those fixes, or
1203 * that Apple fixed the problem in some OS X release.
1205 u_int spoof_eth_src
= 0;
1207 if (ioctl(p
->fd
, BIOCSHDRCMPLT
, &spoof_eth_src
) == -1) {
1208 (void)pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1209 "send: can't turn off BIOCSHDRCMPLT: %s",
1210 pcap_strerror(errno
));
1211 return (PCAP_ERROR
);
1215 * Now try the write again.
1217 ret
= write(p
->fd
, buf
, size
);
1219 #endif /* __APPLE__ */
1221 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "send: %s",
1222 pcap_strerror(errno
));
1223 return (PCAP_ERROR
);
1230 bpf_odminit(char *errbuf
)
1234 if (odm_initialize() == -1) {
1235 if (odm_err_msg(odmerrno
, &errstr
) == -1)
1236 errstr
= "Unknown error";
1237 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1238 "bpf_load: odm_initialize failed: %s",
1240 return (PCAP_ERROR
);
1243 if ((odmlockid
= odm_lock("/etc/objrepos/config_lock", ODM_WAIT
)) == -1) {
1244 if (odm_err_msg(odmerrno
, &errstr
) == -1)
1245 errstr
= "Unknown error";
1246 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1247 "bpf_load: odm_lock of /etc/objrepos/config_lock failed: %s",
1249 (void)odm_terminate();
1250 return (PCAP_ERROR
);
1257 bpf_odmcleanup(char *errbuf
)
1261 if (odm_unlock(odmlockid
) == -1) {
1262 if (errbuf
!= NULL
) {
1263 if (odm_err_msg(odmerrno
, &errstr
) == -1)
1264 errstr
= "Unknown error";
1265 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1266 "bpf_load: odm_unlock failed: %s",
1269 return (PCAP_ERROR
);
1272 if (odm_terminate() == -1) {
1273 if (errbuf
!= NULL
) {
1274 if (odm_err_msg(odmerrno
, &errstr
) == -1)
1275 errstr
= "Unknown error";
1276 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1277 "bpf_load: odm_terminate failed: %s",
1280 return (PCAP_ERROR
);
1287 bpf_load(char *errbuf
)
1291 int numminors
, i
, rc
;
1294 struct bpf_config cfg_bpf
;
1295 struct cfg_load cfg_ld
;
1296 struct cfg_kmod cfg_km
;
1299 * This is very very close to what happens in the real implementation
1300 * but I've fixed some (unlikely) bug situations.
1305 if (bpf_odminit(errbuf
) == PCAP_ERROR
)
1306 return (PCAP_ERROR
);
1308 major
= genmajor(BPF_NAME
);
1310 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1311 "bpf_load: genmajor failed: %s", pcap_strerror(errno
));
1312 (void)bpf_odmcleanup(NULL
);
1313 return (PCAP_ERROR
);
1316 minors
= getminor(major
, &numminors
, BPF_NAME
);
1318 minors
= genminor("bpf", major
, 0, BPF_MINORS
, 1, 1);
1320 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1321 "bpf_load: genminor failed: %s",
1322 pcap_strerror(errno
));
1323 (void)bpf_odmcleanup(NULL
);
1324 return (PCAP_ERROR
);
1328 if (bpf_odmcleanup(errbuf
) == PCAP_ERROR
)
1329 return (PCAP_ERROR
);
1331 rc
= stat(BPF_NODE
"0", &sbuf
);
1332 if (rc
== -1 && errno
!= ENOENT
) {
1333 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1334 "bpf_load: can't stat %s: %s",
1335 BPF_NODE
"0", pcap_strerror(errno
));
1336 return (PCAP_ERROR
);
1339 if (rc
== -1 || getmajor(sbuf
.st_rdev
) != major
) {
1340 for (i
= 0; i
< BPF_MINORS
; i
++) {
1341 sprintf(buf
, "%s%d", BPF_NODE
, i
);
1343 if (mknod(buf
, S_IRUSR
| S_IFCHR
, domakedev(major
, i
)) == -1) {
1344 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1345 "bpf_load: can't mknod %s: %s",
1346 buf
, pcap_strerror(errno
));
1347 return (PCAP_ERROR
);
1352 /* Check if the driver is loaded */
1353 memset(&cfg_ld
, 0x0, sizeof(cfg_ld
));
1355 sprintf(cfg_ld
.path
, "%s/%s", DRIVER_PATH
, BPF_NAME
);
1356 if ((sysconfig(SYS_QUERYLOAD
, (void *)&cfg_ld
, sizeof(cfg_ld
)) == -1) ||
1357 (cfg_ld
.kmid
== 0)) {
1358 /* Driver isn't loaded, load it now */
1359 if (sysconfig(SYS_SINGLELOAD
, (void *)&cfg_ld
, sizeof(cfg_ld
)) == -1) {
1360 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1361 "bpf_load: could not load driver: %s",
1363 return (PCAP_ERROR
);
1367 /* Configure the driver */
1368 cfg_km
.cmd
= CFG_INIT
;
1369 cfg_km
.kmid
= cfg_ld
.kmid
;
1370 cfg_km
.mdilen
= sizeof(cfg_bpf
);
1371 cfg_km
.mdiptr
= (void *)&cfg_bpf
;
1372 for (i
= 0; i
< BPF_MINORS
; i
++) {
1373 cfg_bpf
.devno
= domakedev(major
, i
);
1374 if (sysconfig(SYS_CFGKMOD
, (void *)&cfg_km
, sizeof(cfg_km
)) == -1) {
1375 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1376 "bpf_load: could not configure driver: %s",
1378 return (PCAP_ERROR
);
1389 * Undo any operations done when opening the device when necessary.
1392 pcap_cleanup_bpf(pcap_t
*p
)
1394 struct pcap_bpf
*pb
= p
->priv
;
1395 #ifdef HAVE_BSD_IEEE80211
1397 struct ifmediareq req
;
1401 if (pb
->must_do_on_close
!= 0) {
1403 * There's something we have to do when closing this
1406 #ifdef HAVE_BSD_IEEE80211
1407 if (pb
->must_do_on_close
& MUST_CLEAR_RFMON
) {
1409 * We put the interface into rfmon mode;
1410 * take it out of rfmon mode.
1412 * XXX - if somebody else wants it in rfmon
1413 * mode, this code cannot know that, so it'll take
1414 * it out of rfmon mode.
1416 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
1419 "Can't restore interface flags (socket() failed: %s).\n"
1420 "Please adjust manually.\n",
1423 memset(&req
, 0, sizeof(req
));
1424 strncpy(req
.ifm_name
, pb
->device
,
1425 sizeof(req
.ifm_name
));
1426 if (ioctl(sock
, SIOCGIFMEDIA
, &req
) < 0) {
1428 "Can't restore interface flags (SIOCGIFMEDIA failed: %s).\n"
1429 "Please adjust manually.\n",
1432 if (req
.ifm_current
& IFM_IEEE80211_MONITOR
) {
1434 * Rfmon mode is currently on;
1437 memset(&ifr
, 0, sizeof(ifr
));
1438 (void)strncpy(ifr
.ifr_name
,
1440 sizeof(ifr
.ifr_name
));
1442 req
.ifm_current
& ~IFM_IEEE80211_MONITOR
;
1443 if (ioctl(sock
, SIOCSIFMEDIA
,
1446 "Can't restore interface flags (SIOCSIFMEDIA failed: %s).\n"
1447 "Please adjust manually.\n",
1455 #endif /* HAVE_BSD_IEEE80211 */
1457 #if defined(__FreeBSD__) && defined(SIOCIFCREATE2)
1459 * Attempt to destroy the usbusN interface that we created.
1461 if (pb
->must_do_on_close
& MUST_DESTROY_USBUS
) {
1462 if (if_nametoindex(pb
->device
) > 0) {
1465 s
= socket(AF_LOCAL
, SOCK_DGRAM
, 0);
1467 strlcpy(ifr
.ifr_name
, pb
->device
,
1468 sizeof(ifr
.ifr_name
));
1469 ioctl(s
, SIOCIFDESTROY
, &ifr
);
1474 #endif /* defined(__FreeBSD__) && defined(SIOCIFCREATE2) */
1476 * Take this pcap out of the list of pcaps for which we
1477 * have to take the interface out of some mode.
1479 pcap_remove_from_pcaps_to_close(p
);
1480 pb
->must_do_on_close
= 0;
1483 #ifdef HAVE_ZEROCOPY_BPF
1486 * Delete the mappings. Note that p->buffer gets
1487 * initialized to one of the mmapped regions in
1488 * this case, so do not try and free it directly;
1489 * null it out so that pcap_cleanup_live_common()
1490 * doesn't try to free it.
1492 if (pb
->zbuf1
!= MAP_FAILED
&& pb
->zbuf1
!= NULL
)
1493 (void) munmap(pb
->zbuf1
, pb
->zbufsize
);
1494 if (pb
->zbuf2
!= MAP_FAILED
&& pb
->zbuf2
!= NULL
)
1495 (void) munmap(pb
->zbuf2
, pb
->zbufsize
);
1499 if (pb
->device
!= NULL
) {
1503 pcap_cleanup_live_common(p
);
1507 check_setif_failure(pcap_t
*p
, int error
)
1515 if (error
== ENXIO
) {
1517 * No such device exists.
1520 if (p
->opt
.rfmon
&& strncmp(p
->opt
.device
, "wlt", 3) == 0) {
1522 * Monitor mode was requested, and we're trying
1523 * to open a "wltN" device. Assume that this
1524 * is 10.4 and that we were asked to open an
1525 * "enN" device; if that device exists, return
1526 * "monitor mode not supported on the device".
1528 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
1530 strlcpy(ifr
.ifr_name
, "en",
1531 sizeof(ifr
.ifr_name
));
1532 strlcat(ifr
.ifr_name
, p
->opt
.device
+ 3,
1533 sizeof(ifr
.ifr_name
));
1534 if (ioctl(fd
, SIOCGIFFLAGS
, (char *)&ifr
) < 0) {
1536 * We assume this failed because
1537 * the underlying device doesn't
1540 err
= PCAP_ERROR_NO_SUCH_DEVICE
;
1541 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1542 "SIOCGIFFLAGS on %s failed: %s",
1543 ifr
.ifr_name
, pcap_strerror(errno
));
1546 * The underlying "enN" device
1547 * exists, but there's no
1548 * corresponding "wltN" device;
1549 * that means that the "enN"
1550 * device doesn't support
1551 * monitor mode, probably because
1552 * it's an Ethernet device rather
1553 * than a wireless device.
1555 err
= PCAP_ERROR_RFMON_NOTSUP
;
1560 * We can't find out whether there's
1561 * an underlying "enN" device, so
1562 * just report "no such device".
1564 err
= PCAP_ERROR_NO_SUCH_DEVICE
;
1565 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1566 "socket() failed: %s",
1567 pcap_strerror(errno
));
1575 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETIF failed: %s",
1576 pcap_strerror(errno
));
1577 return (PCAP_ERROR_NO_SUCH_DEVICE
);
1578 } else if (errno
== ENETDOWN
) {
1580 * Return a "network down" indication, so that
1581 * the application can report that rather than
1582 * saying we had a mysterious failure and
1583 * suggest that they report a problem to the
1584 * libpcap developers.
1586 return (PCAP_ERROR_IFACE_NOT_UP
);
1589 * Some other error; fill in the error string, and
1590 * return PCAP_ERROR.
1592 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETIF: %s: %s",
1593 p
->opt
.device
, pcap_strerror(errno
));
1594 return (PCAP_ERROR
);
1599 * Default capture buffer size.
1600 * 32K isn't very much for modern machines with fast networks; we
1601 * pick .5M, as that's the maximum on at least some systems with BPF.
1603 * However, on AIX 3.5, the larger buffer sized caused unrecoverable
1604 * read failures under stress, so we leave it as 32K; yet another
1605 * place where AIX's BPF is broken.
1608 #define DEFAULT_BUFSIZE 32768
1610 #define DEFAULT_BUFSIZE 524288
1614 pcap_activate_bpf(pcap_t
*p
)
1616 struct pcap_bpf
*pb
= p
->priv
;
1618 #ifdef HAVE_BSD_IEEE80211
1625 char *ifrname
= ifr
.lifr_name
;
1626 const size_t ifnamsiz
= sizeof(ifr
.lifr_name
);
1629 char *ifrname
= ifr
.ifr_name
;
1630 const size_t ifnamsiz
= sizeof(ifr
.ifr_name
);
1632 struct bpf_version bv
;
1635 char *wltdev
= NULL
;
1638 struct bpf_dltlist bdl
;
1639 #if defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)
1642 #endif /* BIOCGDLTLIST */
1643 #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
1644 u_int spoof_eth_src
= 1;
1647 struct bpf_insn total_insn
;
1648 struct bpf_program total_prog
;
1649 struct utsname osinfo
;
1650 int have_osinfo
= 0;
1651 #ifdef HAVE_ZEROCOPY_BPF
1653 u_int bufmode
, zbufmax
;
1656 fd
= bpf_open(p
->errbuf
);
1664 if (ioctl(fd
, BIOCVERSION
, (caddr_t
)&bv
) < 0) {
1665 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCVERSION: %s",
1666 pcap_strerror(errno
));
1667 status
= PCAP_ERROR
;
1670 if (bv
.bv_major
!= BPF_MAJOR_VERSION
||
1671 bv
.bv_minor
< BPF_MINOR_VERSION
) {
1672 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1673 "kernel bpf filter out of date");
1674 status
= PCAP_ERROR
;
1679 * Turn a negative snapshot value (invalid), a snapshot value of
1680 * 0 (unspecified), or a value bigger than the normal maximum
1681 * value, into the maximum allowed value.
1683 * If some application really *needs* a bigger snapshot
1684 * length, we should just increase MAXIMUM_SNAPLEN.
1686 if (p
->snapshot
<= 0 || p
->snapshot
> MAXIMUM_SNAPLEN
)
1687 p
->snapshot
= MAXIMUM_SNAPLEN
;
1689 #if defined(LIFNAMSIZ) && defined(ZONENAME_MAX) && defined(lifr_zoneid)
1691 * Retrieve the zoneid of the zone we are currently executing in.
1693 if ((ifr
.lifr_zoneid
= getzoneid()) == -1) {
1694 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "getzoneid(): %s",
1695 pcap_strerror(errno
));
1696 status
= PCAP_ERROR
;
1700 * Check if the given source datalink name has a '/' separated
1701 * zonename prefix string. The zonename prefixed source datalink can
1702 * be used by pcap consumers in the Solaris global zone to capture
1703 * traffic on datalinks in non-global zones. Non-global zones
1704 * do not have access to datalinks outside of their own namespace.
1706 if ((zonesep
= strchr(p
->opt
.device
, '/')) != NULL
) {
1707 char path_zname
[ZONENAME_MAX
];
1711 if (ifr
.lifr_zoneid
!= GLOBAL_ZONEID
) {
1712 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1713 "zonename/linkname only valid in global zone.");
1714 status
= PCAP_ERROR
;
1717 znamelen
= zonesep
- p
->opt
.device
;
1718 (void) strlcpy(path_zname
, p
->opt
.device
, znamelen
+ 1);
1719 ifr
.lifr_zoneid
= getzoneidbyname(path_zname
);
1720 if (ifr
.lifr_zoneid
== -1) {
1721 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1722 "getzoneidbyname(%s): %s", path_zname
,
1723 pcap_strerror(errno
));
1724 status
= PCAP_ERROR
;
1727 lnamep
= strdup(zonesep
+ 1);
1728 if (lnamep
== NULL
) {
1729 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "strdup: %s",
1730 pcap_strerror(errno
));
1731 status
= PCAP_ERROR
;
1734 free(p
->opt
.device
);
1735 p
->opt
.device
= lnamep
;
1739 pb
->device
= strdup(p
->opt
.device
);
1740 if (pb
->device
== NULL
) {
1741 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "strdup: %s",
1742 pcap_strerror(errno
));
1743 status
= PCAP_ERROR
;
1748 * Attempt to find out the version of the OS on which we're running.
1750 if (uname(&osinfo
) == 0)
1755 * See comment in pcap_can_set_rfmon_bpf() for an explanation
1756 * of why we check the version number.
1761 * We assume osinfo.sysname is "Darwin", because
1762 * __APPLE__ is defined. We just check the version.
1764 if (osinfo
.release
[0] < '8' &&
1765 osinfo
.release
[1] == '.') {
1767 * 10.3 (Darwin 7.x) or earlier.
1769 status
= PCAP_ERROR_RFMON_NOTSUP
;
1772 if (osinfo
.release
[0] == '8' &&
1773 osinfo
.release
[1] == '.') {
1775 * 10.4 (Darwin 8.x). s/en/wlt/
1777 if (strncmp(p
->opt
.device
, "en", 2) != 0) {
1779 * Not an enN device; check
1780 * whether the device even exists.
1782 sockfd
= socket(AF_INET
, SOCK_DGRAM
, 0);
1785 p
->opt
.device
, ifnamsiz
);
1786 if (ioctl(sockfd
, SIOCGIFFLAGS
,
1787 (char *)&ifr
) < 0) {
1795 status
= PCAP_ERROR_NO_SUCH_DEVICE
;
1796 pcap_snprintf(p
->errbuf
,
1798 "SIOCGIFFLAGS failed: %s",
1799 pcap_strerror(errno
));
1801 status
= PCAP_ERROR_RFMON_NOTSUP
;
1805 * We can't find out whether
1806 * the device exists, so just
1807 * report "no such device".
1809 status
= PCAP_ERROR_NO_SUCH_DEVICE
;
1810 pcap_snprintf(p
->errbuf
,
1812 "socket() failed: %s",
1813 pcap_strerror(errno
));
1817 wltdev
= malloc(strlen(p
->opt
.device
) + 2);
1818 if (wltdev
== NULL
) {
1819 (void)pcap_snprintf(p
->errbuf
,
1820 PCAP_ERRBUF_SIZE
, "malloc: %s",
1821 pcap_strerror(errno
));
1822 status
= PCAP_ERROR
;
1825 strcpy(wltdev
, "wlt");
1826 strcat(wltdev
, p
->opt
.device
+ 2);
1827 free(p
->opt
.device
);
1828 p
->opt
.device
= wltdev
;
1831 * Everything else is 10.5 or later; for those,
1832 * we just open the enN device, and set the DLT.
1836 #endif /* __APPLE__ */
1839 * If this is FreeBSD, and the device name begins with "usbus",
1840 * try to create the interface if it's not available.
1842 #if defined(__FreeBSD__) && defined(SIOCIFCREATE2)
1843 if (strncmp(p
->opt
.device
, usbus_prefix
, USBUS_PREFIX_LEN
) == 0) {
1845 * Do we already have an interface with that name?
1847 if (if_nametoindex(p
->opt
.device
) == 0) {
1849 * No. We need to create it, and, if we
1850 * succeed, remember that we should destroy
1851 * it when the pcap_t is closed.
1856 * Open a socket to use for ioctls to
1857 * create the interface.
1859 s
= socket(AF_LOCAL
, SOCK_DGRAM
, 0);
1861 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1862 "Can't open socket: %s",
1863 pcap_strerror(errno
));
1864 status
= PCAP_ERROR
;
1869 * If we haven't already done so, arrange to have
1870 * "pcap_close_all()" called when we exit.
1872 if (!pcap_do_addexit(p
)) {
1874 * "atexit()" failed; don't create the
1875 * interface, just give up.
1877 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1880 status
= PCAP_ERROR
;
1885 * Create the interface.
1887 strlcpy(ifr
.ifr_name
, p
->opt
.device
, sizeof(ifr
.ifr_name
));
1888 if (ioctl(s
, SIOCIFCREATE2
, &ifr
) < 0) {
1889 if (errno
== EINVAL
) {
1890 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1891 "Invalid USB bus interface %s",
1894 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1895 "Can't create interface for %s: %s",
1896 p
->opt
.device
, pcap_strerror(errno
));
1899 status
= PCAP_ERROR
;
1904 * Make sure we clean this up when we close.
1906 pb
->must_do_on_close
|= MUST_DESTROY_USBUS
;
1909 * Add this to the list of pcaps to close when we exit.
1911 pcap_add_to_pcaps_to_close(p
);
1914 #endif /* defined(__FreeBSD__) && defined(SIOCIFCREATE2) */
1916 #ifdef HAVE_ZEROCOPY_BPF
1918 * If the BPF extension to set buffer mode is present, try setting
1919 * the mode to zero-copy. If that fails, use regular buffering. If
1920 * it succeeds but other setup fails, return an error to the user.
1922 bufmode
= BPF_BUFMODE_ZBUF
;
1923 if (ioctl(fd
, BIOCSETBUFMODE
, (caddr_t
)&bufmode
) == 0) {
1925 * We have zerocopy BPF; use it.
1930 * How to pick a buffer size: first, query the maximum buffer
1931 * size supported by zero-copy. This also lets us quickly
1932 * determine whether the kernel generally supports zero-copy.
1933 * Then, if a buffer size was specified, use that, otherwise
1934 * query the default buffer size, which reflects kernel
1935 * policy for a desired default. Round to the nearest page
1938 if (ioctl(fd
, BIOCGETZMAX
, (caddr_t
)&zbufmax
) < 0) {
1939 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCGETZMAX: %s",
1940 pcap_strerror(errno
));
1941 status
= PCAP_ERROR
;
1945 if (p
->opt
.buffer_size
!= 0) {
1947 * A buffer size was explicitly specified; use it.
1949 v
= p
->opt
.buffer_size
;
1951 if ((ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) ||
1952 v
< DEFAULT_BUFSIZE
)
1953 v
= DEFAULT_BUFSIZE
;
1956 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) /* to any y */
1958 pb
->zbufsize
= roundup(v
, getpagesize());
1959 if (pb
->zbufsize
> zbufmax
)
1960 pb
->zbufsize
= zbufmax
;
1961 pb
->zbuf1
= mmap(NULL
, pb
->zbufsize
, PROT_READ
| PROT_WRITE
,
1963 pb
->zbuf2
= mmap(NULL
, pb
->zbufsize
, PROT_READ
| PROT_WRITE
,
1965 if (pb
->zbuf1
== MAP_FAILED
|| pb
->zbuf2
== MAP_FAILED
) {
1966 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "mmap: %s",
1967 pcap_strerror(errno
));
1968 status
= PCAP_ERROR
;
1971 memset(&bz
, 0, sizeof(bz
)); /* bzero() deprecated, replaced with memset() */
1972 bz
.bz_bufa
= pb
->zbuf1
;
1973 bz
.bz_bufb
= pb
->zbuf2
;
1974 bz
.bz_buflen
= pb
->zbufsize
;
1975 if (ioctl(fd
, BIOCSETZBUF
, (caddr_t
)&bz
) < 0) {
1976 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETZBUF: %s",
1977 pcap_strerror(errno
));
1978 status
= PCAP_ERROR
;
1981 (void)strncpy(ifrname
, p
->opt
.device
, ifnamsiz
);
1982 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) < 0) {
1983 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETIF: %s: %s",
1984 p
->opt
.device
, pcap_strerror(errno
));
1985 status
= PCAP_ERROR
;
1988 v
= pb
->zbufsize
- sizeof(struct bpf_zbuf_header
);
1993 * We don't have zerocopy BPF.
1994 * Set the buffer size.
1996 if (p
->opt
.buffer_size
!= 0) {
1998 * A buffer size was explicitly specified; use it.
2000 if (ioctl(fd
, BIOCSBLEN
,
2001 (caddr_t
)&p
->opt
.buffer_size
) < 0) {
2002 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2003 "BIOCSBLEN: %s: %s", p
->opt
.device
,
2004 pcap_strerror(errno
));
2005 status
= PCAP_ERROR
;
2010 * Now bind to the device.
2012 (void)strncpy(ifrname
, p
->opt
.device
, ifnamsiz
);
2014 if (ioctl(fd
, BIOCSETLIF
, (caddr_t
)&ifr
) < 0)
2016 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) < 0)
2019 status
= check_setif_failure(p
, errno
);
2024 * No buffer size was explicitly specified.
2026 * Try finding a good size for the buffer;
2027 * DEFAULT_BUFSIZE may be too big, so keep
2028 * cutting it in half until we find a size
2029 * that works, or run out of sizes to try.
2030 * If the default is larger, don't make it smaller.
2032 if ((ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) ||
2033 v
< DEFAULT_BUFSIZE
)
2034 v
= DEFAULT_BUFSIZE
;
2035 for ( ; v
!= 0; v
>>= 1) {
2037 * Ignore the return value - this is because the
2038 * call fails on BPF systems that don't have
2039 * kernel malloc. And if the call fails, it's
2040 * no big deal, we just continue to use the
2041 * standard buffer size.
2043 (void) ioctl(fd
, BIOCSBLEN
, (caddr_t
)&v
);
2045 (void)strncpy(ifrname
, p
->opt
.device
, ifnamsiz
);
2047 if (ioctl(fd
, BIOCSETLIF
, (caddr_t
)&ifr
) >= 0)
2049 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) >= 0)
2051 break; /* that size worked; we're done */
2053 if (errno
!= ENOBUFS
) {
2054 status
= check_setif_failure(p
, errno
);
2060 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2061 "BIOCSBLEN: %s: No buffer size worked",
2063 status
= PCAP_ERROR
;
2069 /* Get the data link layer type. */
2070 if (ioctl(fd
, BIOCGDLT
, (caddr_t
)&v
) < 0) {
2071 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCGDLT: %s",
2072 pcap_strerror(errno
));
2073 status
= PCAP_ERROR
;
2079 * AIX's BPF returns IFF_ types, not DLT_ types, in BIOCGDLT.
2102 * We don't know what to map this to yet.
2104 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "unknown interface type %u",
2106 status
= PCAP_ERROR
;
2110 #if _BSDI_VERSION - 0 >= 199510
2111 /* The SLIP and PPP link layer header changed in BSD/OS 2.1 */
2126 case 12: /*DLT_C_HDLC*/
2134 * We know the default link type -- now determine all the DLTs
2135 * this interface supports. If this fails with EINVAL, it's
2136 * not fatal; we just don't get to use the feature later.
2138 if (get_dlt_list(fd
, v
, &bdl
, p
->errbuf
) == -1) {
2139 status
= PCAP_ERROR
;
2142 p
->dlt_count
= bdl
.bfl_len
;
2143 p
->dlt_list
= bdl
.bfl_list
;
2147 * Monitor mode fun, continued.
2149 * For 10.5 and, we're assuming, later releases, as noted above,
2150 * 802.1 adapters that support monitor mode offer both DLT_EN10MB,
2151 * DLT_IEEE802_11, and possibly some 802.11-plus-radio-information
2152 * DLT_ value. Choosing one of the 802.11 DLT_ values will turn
2155 * Therefore, if the user asked for monitor mode, we filter out
2156 * the DLT_EN10MB value, as you can't get that in monitor mode,
2157 * and, if the user didn't ask for monitor mode, we filter out
2158 * the 802.11 DLT_ values, because selecting those will turn
2159 * monitor mode on. Then, for monitor mode, if an 802.11-plus-
2160 * radio DLT_ value is offered, we try to select that, otherwise
2161 * we try to select DLT_IEEE802_11.
2164 if (isdigit((unsigned)osinfo
.release
[0]) &&
2165 (osinfo
.release
[0] == '9' ||
2166 isdigit((unsigned)osinfo
.release
[1]))) {
2168 * 10.5 (Darwin 9.x), or later.
2170 new_dlt
= find_802_11(&bdl
);
2171 if (new_dlt
!= -1) {
2173 * We have at least one 802.11 DLT_ value,
2174 * so this is an 802.11 interface.
2175 * new_dlt is the best of the 802.11
2176 * DLT_ values in the list.
2180 * Our caller wants monitor mode.
2181 * Purge DLT_EN10MB from the list
2182 * of link-layer types, as selecting
2183 * it will keep monitor mode off.
2188 * If the new mode we want isn't
2189 * the default mode, attempt to
2190 * select the new mode.
2192 if ((u_int
)new_dlt
!= v
) {
2193 if (ioctl(p
->fd
, BIOCSDLT
,
2205 * Our caller doesn't want
2206 * monitor mode. Unless this
2207 * is being done by pcap_open_live(),
2208 * purge the 802.11 link-layer types
2209 * from the list, as selecting
2210 * one of them will turn monitor
2219 * The caller requested monitor
2220 * mode, but we have no 802.11
2221 * link-layer types, so they
2224 status
= PCAP_ERROR_RFMON_NOTSUP
;
2230 #elif defined(HAVE_BSD_IEEE80211)
2232 * *BSD with the new 802.11 ioctls.
2233 * Do we want monitor mode?
2237 * Try to put the interface into monitor mode.
2239 retv
= monitor_mode(p
, 1);
2249 * We're in monitor mode.
2250 * Try to find the best 802.11 DLT_ value and, if we
2251 * succeed, try to switch to that mode if we're not
2252 * already in that mode.
2254 new_dlt
= find_802_11(&bdl
);
2255 if (new_dlt
!= -1) {
2257 * We have at least one 802.11 DLT_ value.
2258 * new_dlt is the best of the 802.11
2259 * DLT_ values in the list.
2261 * If the new mode we want isn't the default mode,
2262 * attempt to select the new mode.
2264 if ((u_int
)new_dlt
!= v
) {
2265 if (ioctl(p
->fd
, BIOCSDLT
, &new_dlt
) != -1) {
2267 * We succeeded; make this the
2275 #endif /* various platforms */
2276 #endif /* BIOCGDLTLIST */
2279 * If this is an Ethernet device, and we don't have a DLT_ list,
2280 * give it a list with DLT_EN10MB and DLT_DOCSIS. (That'd give
2281 * 802.11 interfaces DLT_DOCSIS, which isn't the right thing to
2282 * do, but there's not much we can do about that without finding
2283 * some other way of determining whether it's an Ethernet or 802.11
2286 if (v
== DLT_EN10MB
&& p
->dlt_count
== 0) {
2287 p
->dlt_list
= (u_int
*) malloc(sizeof(u_int
) * 2);
2289 * If that fails, just leave the list empty.
2291 if (p
->dlt_list
!= NULL
) {
2292 p
->dlt_list
[0] = DLT_EN10MB
;
2293 p
->dlt_list
[1] = DLT_DOCSIS
;
2299 p
->fddipad
= PCAP_FDDIPAD
;
2305 #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
2307 * Do a BIOCSHDRCMPLT, if defined, to turn that flag on, so
2308 * the link-layer source address isn't forcibly overwritten.
2309 * (Should we ignore errors? Should we do this only if
2310 * we're open for writing?)
2312 * XXX - I seem to remember some packet-sending bug in some
2313 * BSDs - check CVS log for "bpf.c"?
2315 if (ioctl(fd
, BIOCSHDRCMPLT
, &spoof_eth_src
) == -1) {
2316 (void)pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2317 "BIOCSHDRCMPLT: %s", pcap_strerror(errno
));
2318 status
= PCAP_ERROR
;
2323 #ifdef HAVE_ZEROCOPY_BPF
2325 * In zero-copy mode, we just use the timeout in select().
2326 * XXX - what if we're in non-blocking mode and the *application*
2327 * is using select() or poll() or kqueues or....?
2329 if (p
->opt
.timeout
&& !pb
->zerocopy
) {
2331 if (p
->opt
.timeout
) {
2334 * XXX - is this seconds/nanoseconds in AIX?
2335 * (Treating it as such doesn't fix the timeout
2336 * problem described below.)
2338 * XXX - Mac OS X 10.6 mishandles BIOCSRTIMEOUT in
2339 * 64-bit userland - it takes, as an argument, a
2340 * "struct BPF_TIMEVAL", which has 32-bit tv_sec
2341 * and tv_usec, rather than a "struct timeval".
2343 * If this platform defines "struct BPF_TIMEVAL",
2344 * we check whether the structure size in BIOCSRTIMEOUT
2345 * is that of a "struct timeval" and, if not, we use
2346 * a "struct BPF_TIMEVAL" rather than a "struct timeval".
2347 * (That way, if the bug is fixed in a future release,
2348 * we will still do the right thing.)
2351 #ifdef HAVE_STRUCT_BPF_TIMEVAL
2352 struct BPF_TIMEVAL bpf_to
;
2354 if (IOCPARM_LEN(BIOCSRTIMEOUT
) != sizeof(struct timeval
)) {
2355 bpf_to
.tv_sec
= p
->opt
.timeout
/ 1000;
2356 bpf_to
.tv_usec
= (p
->opt
.timeout
* 1000) % 1000000;
2357 if (ioctl(p
->fd
, BIOCSRTIMEOUT
, (caddr_t
)&bpf_to
) < 0) {
2358 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2359 "BIOCSRTIMEOUT: %s", pcap_strerror(errno
));
2360 status
= PCAP_ERROR
;
2365 to
.tv_sec
= p
->opt
.timeout
/ 1000;
2366 to
.tv_usec
= (p
->opt
.timeout
* 1000) % 1000000;
2367 if (ioctl(p
->fd
, BIOCSRTIMEOUT
, (caddr_t
)&to
) < 0) {
2368 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2369 "BIOCSRTIMEOUT: %s", pcap_strerror(errno
));
2370 status
= PCAP_ERROR
;
2373 #ifdef HAVE_STRUCT_BPF_TIMEVAL
2378 #ifdef BIOCIMMEDIATE
2380 * Darren Reed notes that
2382 * On AIX (4.2 at least), if BIOCIMMEDIATE is not set, the
2383 * timeout appears to be ignored and it waits until the buffer
2384 * is filled before returning. The result of not having it
2385 * set is almost worse than useless if your BPF filter
2386 * is reducing things to only a few packets (i.e. one every
2389 * so we always turn BIOCIMMEDIATE mode on if this is AIX.
2391 * For other platforms, we don't turn immediate mode on by default,
2392 * as that would mean we get woken up for every packet, which
2393 * probably isn't what you want for a packet sniffer.
2395 * We set immediate mode if the caller requested it by calling
2396 * pcap_set_immediate() before calling pcap_activate().
2399 if (p
->opt
.immediate
) {
2402 if (ioctl(p
->fd
, BIOCIMMEDIATE
, &v
) < 0) {
2403 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2404 "BIOCIMMEDIATE: %s", pcap_strerror(errno
));
2405 status
= PCAP_ERROR
;
2411 #else /* BIOCIMMEDIATE */
2412 if (p
->opt
.immediate
) {
2414 * We don't support immediate mode. Fail.
2416 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "Immediate mode not supported");
2417 status
= PCAP_ERROR
;
2420 #endif /* BIOCIMMEDIATE */
2422 if (p
->opt
.promisc
) {
2423 /* set promiscuous mode, just warn if it fails */
2424 if (ioctl(p
->fd
, BIOCPROMISC
, NULL
) < 0) {
2425 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCPROMISC: %s",
2426 pcap_strerror(errno
));
2427 status
= PCAP_WARNING_PROMISC_NOTSUP
;
2433 if (ioctl(p
->fd
, BIOCSTSTAMP
, &v
) < 0) {
2434 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSTSTAMP: %s",
2435 pcap_strerror(errno
));
2436 status
= PCAP_ERROR
;
2439 #endif /* BIOCSTSTAMP */
2441 if (ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) {
2442 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCGBLEN: %s",
2443 pcap_strerror(errno
));
2444 status
= PCAP_ERROR
;
2448 #ifdef HAVE_ZEROCOPY_BPF
2449 if (!pb
->zerocopy
) {
2451 p
->buffer
= malloc(p
->bufsize
);
2452 if (p
->buffer
== NULL
) {
2453 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
2454 pcap_strerror(errno
));
2455 status
= PCAP_ERROR
;
2459 /* For some strange reason this seems to prevent the EFAULT
2460 * problems we have experienced from AIX BPF. */
2461 memset(p
->buffer
, 0x0, p
->bufsize
);
2463 #ifdef HAVE_ZEROCOPY_BPF
2468 * If there's no filter program installed, there's
2469 * no indication to the kernel of what the snapshot
2470 * length should be, so no snapshotting is done.
2472 * Therefore, when we open the device, we install
2473 * an "accept everything" filter with the specified
2476 total_insn
.code
= (u_short
)(BPF_RET
| BPF_K
);
2479 total_insn
.k
= p
->snapshot
;
2481 total_prog
.bf_len
= 1;
2482 total_prog
.bf_insns
= &total_insn
;
2483 if (ioctl(p
->fd
, BIOCSETF
, (caddr_t
)&total_prog
) < 0) {
2484 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETF: %s",
2485 pcap_strerror(errno
));
2486 status
= PCAP_ERROR
;
2491 * On most BPF platforms, either you can do a "select()" or
2492 * "poll()" on a BPF file descriptor and it works correctly,
2493 * or you can do it and it will return "readable" if the
2494 * hold buffer is full but not if the timeout expires *and*
2495 * a non-blocking read will, if the hold buffer is empty
2496 * but the store buffer isn't empty, rotate the buffers
2497 * and return what packets are available.
2499 * In the latter case, the fact that a non-blocking read
2500 * will give you the available packets means you can work
2501 * around the failure of "select()" and "poll()" to wake up
2502 * and return "readable" when the timeout expires by using
2503 * the timeout as the "select()" or "poll()" timeout, putting
2504 * the BPF descriptor into non-blocking mode, and read from
2505 * it regardless of whether "select()" reports it as readable
2508 * However, in FreeBSD 4.3 and 4.4, "select()" and "poll()"
2509 * won't wake up and return "readable" if the timer expires
2510 * and non-blocking reads return EWOULDBLOCK if the hold
2511 * buffer is empty, even if the store buffer is non-empty.
2513 * This means the workaround in question won't work.
2515 * Therefore, on FreeBSD 4.3 and 4.4, we set "p->selectable_fd"
2516 * to -1, which means "sorry, you can't use 'select()' or 'poll()'
2517 * here". On all other BPF platforms, we set it to the FD for
2518 * the BPF device; in NetBSD, OpenBSD, and Darwin, a non-blocking
2519 * read will, if the hold buffer is empty and the store buffer
2520 * isn't empty, rotate the buffers and return what packets are
2521 * there (and in sufficiently recent versions of OpenBSD
2522 * "select()" and "poll()" should work correctly).
2524 * XXX - what about AIX?
2526 p
->selectable_fd
= p
->fd
; /* assume select() works until we know otherwise */
2529 * We can check what OS this is.
2531 if (strcmp(osinfo
.sysname
, "FreeBSD") == 0) {
2532 if (strncmp(osinfo
.release
, "4.3-", 4) == 0 ||
2533 strncmp(osinfo
.release
, "4.4-", 4) == 0)
2534 p
->selectable_fd
= -1;
2538 p
->read_op
= pcap_read_bpf
;
2539 p
->inject_op
= pcap_inject_bpf
;
2540 p
->setfilter_op
= pcap_setfilter_bpf
;
2541 p
->setdirection_op
= pcap_setdirection_bpf
;
2542 p
->set_datalink_op
= pcap_set_datalink_bpf
;
2543 p
->getnonblock_op
= pcap_getnonblock_bpf
;
2544 p
->setnonblock_op
= pcap_setnonblock_bpf
;
2545 p
->stats_op
= pcap_stats_bpf
;
2546 p
->cleanup_op
= pcap_cleanup_bpf
;
2550 pcap_cleanup_bpf(p
);
2555 * Not all interfaces can be bound to by BPF, so try to bind to
2556 * the specified interface; return 0 if we fail with
2557 * PCAP_ERROR_NO_SUCH_DEVICE (which means we got an ENXIO when we tried
2558 * to bind, which means this interface isn't in the list of interfaces
2559 * attached to BPF) and 1 otherwise.
2562 check_bpf_bindable(const char *name
)
2565 char errbuf
[PCAP_ERRBUF_SIZE
];
2568 * On macOS, we don't do this check if the device name begins
2569 * with "wlt"; at least some versions of macOS (actually, it
2570 * was called "Mac OS X" then...) offer monitor mode capturing
2571 * by having a separate "monitor mode" device for each wireless
2572 * adapter, rather than by implementing the ioctls that
2573 * {Free,Net,Open,DragonFly}BSD provide. Opening that device
2574 * puts the adapter into monitor mode, which, at least for
2575 * some adapters, causes them to deassociate from the network
2576 * with which they're associated.
2578 * Instead, we try to open the corresponding "en" device (so
2579 * that we don't end up with, for users without sufficient
2580 * privilege to open capture devices, a list of adapters that
2581 * only includes the wlt devices).
2584 if (strncmp(name
, "wlt", 3) == 0) {
2589 * Try to allocate a buffer for the "en"
2592 en_name_len
= strlen(name
) - 1;
2593 en_name
= malloc(en_name_len
+ 1);
2594 if (en_name
== NULL
) {
2595 (void)pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
2596 "malloc: %s", pcap_strerror(errno
));
2599 strcpy(en_name
, "en");
2600 strcat(en_name
, name
+ 3);
2601 fd
= bpf_open_and_bind(en_name
, errbuf
);
2604 #endif /* __APPLE */
2605 fd
= bpf_open_and_bind(name
, errbuf
);
2608 * Error - was it PCAP_ERROR_NO_SUCH_DEVICE?
2610 if (fd
== PCAP_ERROR_NO_SUCH_DEVICE
) {
2612 * Yes, so we can't bind to this because it's
2613 * not something supported by BPF.
2618 * No, so we don't know whether it's supported or not;
2619 * say it is, so that the user can at least try to
2620 * open it and report the error (which is probably
2621 * "you don't have permission to open BPF devices";
2622 * reporting those interfaces means users will ask
2623 * "why am I getting a permissions error when I try
2624 * to capture" rather than "why am I not seeing any
2625 * interfaces", making the underlying problem clearer).
2637 #if defined(__FreeBSD__) && defined(SIOCIFCREATE2)
2639 finddevs_usb(pcap_if_list_t
*devlistp
, char *errbuf
)
2642 struct dirent
*usbitem
;
2647 * We might have USB sniffing support, so try looking for USB
2650 * We want to report a usbusN device for each USB bus, but
2651 * usbusN interfaces might, or might not, exist for them -
2652 * we create one if there isn't already one.
2654 * So, instead, we look in /dev/usb for all buses and create
2655 * a "usbusN" device for each one.
2657 usbdir
= opendir("/dev/usb");
2658 if (usbdir
== NULL
) {
2666 * Leave enough room for a 32-bit (10-digit) bus number.
2667 * Yes, that's overkill, but we won't be using
2668 * the buffer very long.
2670 name_max
= USBUS_PREFIX_LEN
+ 10 + 1;
2671 name
= malloc(name_max
);
2676 while ((usbitem
= readdir(usbdir
)) != NULL
) {
2680 if (strcmp(usbitem
->d_name
, ".") == 0 ||
2681 strcmp(usbitem
->d_name
, "..") == 0) {
2687 p
= strchr(usbitem
->d_name
, '.');
2690 busnumlen
= p
- usbitem
->d_name
;
2691 memcpy(name
, usbus_prefix
, USBUS_PREFIX_LEN
);
2692 memcpy(name
+ USBUS_PREFIX_LEN
, usbitem
->d_name
, busnumlen
);
2693 *(name
+ USBUS_PREFIX_LEN
+ busnumlen
) = '\0';
2695 * There's an entry in this directory for every USB device,
2696 * not for every bus; if there's more than one device on
2697 * the bus, there'll be more than one entry for that bus,
2698 * so we need to avoid adding multiple capture devices
2701 if (find_or_add_dev(devlistp
, name
, PCAP_IF_UP
, NULL
, errbuf
) == NULL
) {
2704 return (PCAP_ERROR
);
2714 pcap_platform_finddevs(pcap_if_list_t
*devlistp
, char *errbuf
)
2717 * Get the list of regular interfaces first.
2719 if (pcap_findalldevs_interfaces(devlistp
, errbuf
, check_bpf_bindable
) == -1)
2720 return (-1); /* failure */
2722 #if defined(__FreeBSD__) && defined(SIOCIFCREATE2)
2723 if (finddevs_usb(devlistp
, errbuf
) == -1)
2730 #ifdef HAVE_BSD_IEEE80211
2732 monitor_mode(pcap_t
*p
, int set
)
2734 struct pcap_bpf
*pb
= p
->priv
;
2736 struct ifmediareq req
;
2737 IFM_ULIST_TYPE
*media_list
;
2742 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
2744 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "can't open socket: %s",
2745 pcap_strerror(errno
));
2746 return (PCAP_ERROR
);
2749 memset(&req
, 0, sizeof req
);
2750 strncpy(req
.ifm_name
, p
->opt
.device
, sizeof req
.ifm_name
);
2753 * Find out how many media types we have.
2755 if (ioctl(sock
, SIOCGIFMEDIA
, &req
) < 0) {
2757 * Can't get the media types.
2763 * There's no such device.
2766 return (PCAP_ERROR_NO_SUCH_DEVICE
);
2770 * Interface doesn't support SIOC{G,S}IFMEDIA.
2773 return (PCAP_ERROR_RFMON_NOTSUP
);
2776 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2777 "SIOCGIFMEDIA 1: %s", pcap_strerror(errno
));
2779 return (PCAP_ERROR
);
2782 if (req
.ifm_count
== 0) {
2787 return (PCAP_ERROR_RFMON_NOTSUP
);
2791 * Allocate a buffer to hold all the media types, and
2792 * get the media types.
2794 media_list
= malloc(req
.ifm_count
* sizeof(*media_list
));
2795 if (media_list
== NULL
) {
2796 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
2797 pcap_strerror(errno
));
2799 return (PCAP_ERROR
);
2801 req
.ifm_ulist
= media_list
;
2802 if (ioctl(sock
, SIOCGIFMEDIA
, &req
) < 0) {
2803 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "SIOCGIFMEDIA: %s",
2804 pcap_strerror(errno
));
2807 return (PCAP_ERROR
);
2811 * Look for an 802.11 "automatic" media type.
2812 * We assume that all 802.11 adapters have that media type,
2813 * and that it will carry the monitor mode supported flag.
2816 for (i
= 0; i
< req
.ifm_count
; i
++) {
2817 if (IFM_TYPE(media_list
[i
]) == IFM_IEEE80211
2818 && IFM_SUBTYPE(media_list
[i
]) == IFM_AUTO
) {
2819 /* OK, does it do monitor mode? */
2820 if (media_list
[i
] & IFM_IEEE80211_MONITOR
) {
2829 * This adapter doesn't support monitor mode.
2832 return (PCAP_ERROR_RFMON_NOTSUP
);
2837 * Don't just check whether we can enable monitor mode,
2838 * do so, if it's not already enabled.
2840 if ((req
.ifm_current
& IFM_IEEE80211_MONITOR
) == 0) {
2842 * Monitor mode isn't currently on, so turn it on,
2843 * and remember that we should turn it off when the
2848 * If we haven't already done so, arrange to have
2849 * "pcap_close_all()" called when we exit.
2851 if (!pcap_do_addexit(p
)) {
2853 * "atexit()" failed; don't put the interface
2854 * in monitor mode, just give up.
2857 return (PCAP_ERROR
);
2859 memset(&ifr
, 0, sizeof(ifr
));
2860 (void)strncpy(ifr
.ifr_name
, p
->opt
.device
,
2861 sizeof(ifr
.ifr_name
));
2862 ifr
.ifr_media
= req
.ifm_current
| IFM_IEEE80211_MONITOR
;
2863 if (ioctl(sock
, SIOCSIFMEDIA
, &ifr
) == -1) {
2864 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2865 "SIOCSIFMEDIA: %s", pcap_strerror(errno
));
2867 return (PCAP_ERROR
);
2870 pb
->must_do_on_close
|= MUST_CLEAR_RFMON
;
2873 * Add this to the list of pcaps to close when we exit.
2875 pcap_add_to_pcaps_to_close(p
);
2880 #endif /* HAVE_BSD_IEEE80211 */
2882 #if defined(BIOCGDLTLIST) && (defined(__APPLE__) || defined(HAVE_BSD_IEEE80211))
2884 * Check whether we have any 802.11 link-layer types; return the best
2885 * of the 802.11 link-layer types if we find one, and return -1
2888 * DLT_IEEE802_11_RADIO, with the radiotap header, is considered the
2889 * best 802.11 link-layer type; any of the other 802.11-plus-radio
2890 * headers are second-best; 802.11 with no radio information is
2894 find_802_11(struct bpf_dltlist
*bdlp
)
2900 * Scan the list of DLT_ values, looking for 802.11 values,
2901 * and, if we find any, choose the best of them.
2904 for (i
= 0; i
< bdlp
->bfl_len
; i
++) {
2905 switch (bdlp
->bfl_list
[i
]) {
2907 case DLT_IEEE802_11
:
2909 * 802.11, but no radio.
2911 * Offer this, and select it as the new mode
2912 * unless we've already found an 802.11
2913 * header with radio information.
2916 new_dlt
= bdlp
->bfl_list
[i
];
2919 case DLT_PRISM_HEADER
:
2920 case DLT_AIRONET_HEADER
:
2921 case DLT_IEEE802_11_RADIO_AVS
:
2923 * 802.11 with radio, but not radiotap.
2925 * Offer this, and select it as the new mode
2926 * unless we've already found the radiotap DLT_.
2928 if (new_dlt
!= DLT_IEEE802_11_RADIO
)
2929 new_dlt
= bdlp
->bfl_list
[i
];
2932 case DLT_IEEE802_11_RADIO
:
2934 * 802.11 with radiotap.
2936 * Offer this, and select it as the new mode.
2938 new_dlt
= bdlp
->bfl_list
[i
];
2951 #endif /* defined(BIOCGDLTLIST) && (defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)) */
2953 #if defined(__APPLE__) && defined(BIOCGDLTLIST)
2955 * Remove DLT_EN10MB from the list of DLT_ values, as we're in monitor mode,
2956 * and DLT_EN10MB isn't supported in monitor mode.
2959 remove_en(pcap_t
*p
)
2964 * Scan the list of DLT_ values and discard DLT_EN10MB.
2967 for (i
= 0; i
< p
->dlt_count
; i
++) {
2968 switch (p
->dlt_list
[i
]) {
2972 * Don't offer this one.
2978 * Just copy this mode over.
2984 * Copy this DLT_ value to its new position.
2986 p
->dlt_list
[j
] = p
->dlt_list
[i
];
2991 * Set the DLT_ count to the number of entries we copied.
2997 * Remove 802.11 link-layer types from the list of DLT_ values, as
2998 * we're not in monitor mode, and those DLT_ values will switch us
3002 remove_802_11(pcap_t
*p
)
3007 * Scan the list of DLT_ values and discard 802.11 values.
3010 for (i
= 0; i
< p
->dlt_count
; i
++) {
3011 switch (p
->dlt_list
[i
]) {
3013 case DLT_IEEE802_11
:
3014 case DLT_PRISM_HEADER
:
3015 case DLT_AIRONET_HEADER
:
3016 case DLT_IEEE802_11_RADIO
:
3017 case DLT_IEEE802_11_RADIO_AVS
:
3019 * 802.11. Don't offer this one.
3025 * Just copy this mode over.
3031 * Copy this DLT_ value to its new position.
3033 p
->dlt_list
[j
] = p
->dlt_list
[i
];
3038 * Set the DLT_ count to the number of entries we copied.
3042 #endif /* defined(__APPLE__) && defined(BIOCGDLTLIST) */
3045 pcap_setfilter_bpf(pcap_t
*p
, struct bpf_program
*fp
)
3047 struct pcap_bpf
*pb
= p
->priv
;
3050 * Free any user-mode filter we might happen to have installed.
3052 pcap_freecode(&p
->fcode
);
3055 * Try to install the kernel filter.
3057 if (ioctl(p
->fd
, BIOCSETF
, (caddr_t
)fp
) == 0) {
3061 pb
->filtering_in_kernel
= 1; /* filtering in the kernel */
3064 * Discard any previously-received packets, as they might
3065 * have passed whatever filter was formerly in effect, but
3066 * might not pass this filter (BIOCSETF discards packets
3067 * buffered in the kernel, so you can lose packets in any
3077 * If it failed with EINVAL, that's probably because the program
3078 * is invalid or too big. Validate it ourselves; if we like it
3079 * (we currently allow backward branches, to support protochain),
3080 * run it in userland. (There's no notion of "too big" for
3083 * Otherwise, just give up.
3084 * XXX - if the copy of the program into the kernel failed,
3085 * we will get EINVAL rather than, say, EFAULT on at least
3088 if (errno
!= EINVAL
) {
3089 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETF: %s",
3090 pcap_strerror(errno
));
3095 * install_bpf_program() validates the program.
3097 * XXX - what if we already have a filter in the kernel?
3099 if (install_bpf_program(p
, fp
) < 0)
3101 pb
->filtering_in_kernel
= 0; /* filtering in userland */
3106 * Set direction flag: Which packets do we accept on a forwarding
3107 * single device? IN, OUT or both?
3110 pcap_setdirection_bpf(pcap_t
*p
, pcap_direction_t d
)
3112 #if defined(BIOCSDIRECTION)
3115 direction
= (d
== PCAP_D_IN
) ? BPF_D_IN
:
3116 ((d
== PCAP_D_OUT
) ? BPF_D_OUT
: BPF_D_INOUT
);
3117 if (ioctl(p
->fd
, BIOCSDIRECTION
, &direction
) == -1) {
3118 (void) pcap_snprintf(p
->errbuf
, sizeof(p
->errbuf
),
3119 "Cannot set direction to %s: %s",
3120 (d
== PCAP_D_IN
) ? "PCAP_D_IN" :
3121 ((d
== PCAP_D_OUT
) ? "PCAP_D_OUT" : "PCAP_D_INOUT"),
3126 #elif defined(BIOCSSEESENT)
3130 * We don't support PCAP_D_OUT.
3132 if (d
== PCAP_D_OUT
) {
3133 pcap_snprintf(p
->errbuf
, sizeof(p
->errbuf
),
3134 "Setting direction to PCAP_D_OUT is not supported on BPF");
3138 seesent
= (d
== PCAP_D_INOUT
);
3139 if (ioctl(p
->fd
, BIOCSSEESENT
, &seesent
) == -1) {
3140 (void) pcap_snprintf(p
->errbuf
, sizeof(p
->errbuf
),
3141 "Cannot set direction to %s: %s",
3142 (d
== PCAP_D_INOUT
) ? "PCAP_D_INOUT" : "PCAP_D_IN",
3148 (void) pcap_snprintf(p
->errbuf
, sizeof(p
->errbuf
),
3149 "This system doesn't support BIOCSSEESENT, so the direction can't be set");
3155 pcap_set_datalink_bpf(pcap_t
*p
, int dlt
)
3158 if (ioctl(p
->fd
, BIOCSDLT
, &dlt
) == -1) {
3159 (void) pcap_snprintf(p
->errbuf
, sizeof(p
->errbuf
),
3160 "Cannot set DLT %d: %s", dlt
, strerror(errno
));
3167 #include "pcap_version.h"
3170 * Platform-specific information.
3173 pcap_lib_version(void)
3175 #ifdef HAVE_ZEROCOPY_BPF
3176 return (PCAP_VERSION_STRING
" (with zerocopy support)");
3178 return (PCAP_VERSION_STRING
);