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 macOS, 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
);
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_non_802_11(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 * In some versions of macOS, we might not even get any of the
231 * 802.11-plus-radio-header DLT_'s defined, even though some
232 * of them are used by various Airport drivers in those versions.
234 #ifndef DLT_PRISM_HEADER
235 #define DLT_PRISM_HEADER 119
237 #ifndef DLT_AIRONET_HEADER
238 #define DLT_AIRONET_HEADER 120
240 #ifndef DLT_IEEE802_11_RADIO
241 #define DLT_IEEE802_11_RADIO 127
243 #ifndef DLT_IEEE802_11_RADIO_AVS
244 #define DLT_IEEE802_11_RADIO_AVS 163
247 static int pcap_can_set_rfmon_bpf(pcap_t
*p
);
248 static int pcap_activate_bpf(pcap_t
*p
);
249 static int pcap_setfilter_bpf(pcap_t
*p
, struct bpf_program
*fp
);
250 static int pcap_setdirection_bpf(pcap_t
*, pcap_direction_t
);
251 static int pcap_set_datalink_bpf(pcap_t
*p
, int dlt
);
254 * For zerocopy bpf, the setnonblock/getnonblock routines need to modify
255 * pb->nonblock so we don't call select(2) if the pcap handle is in non-
259 pcap_getnonblock_bpf(pcap_t
*p
)
261 #ifdef HAVE_ZEROCOPY_BPF
262 struct pcap_bpf
*pb
= p
->priv
;
265 return (pb
->nonblock
);
267 return (pcap_getnonblock_fd(p
));
271 pcap_setnonblock_bpf(pcap_t
*p
, int nonblock
)
273 #ifdef HAVE_ZEROCOPY_BPF
274 struct pcap_bpf
*pb
= p
->priv
;
277 pb
->nonblock
= nonblock
;
281 return (pcap_setnonblock_fd(p
, nonblock
));
284 #ifdef HAVE_ZEROCOPY_BPF
286 * Zero-copy BPF buffer routines to check for and acknowledge BPF data in
287 * shared memory buffers.
289 * pcap_next_zbuf_shm(): Check for a newly available shared memory buffer,
290 * and set up p->buffer and cc to reflect one if available. Notice that if
291 * there was no prior buffer, we select zbuf1 as this will be the first
292 * buffer filled for a fresh BPF session.
295 pcap_next_zbuf_shm(pcap_t
*p
, int *cc
)
297 struct pcap_bpf
*pb
= p
->priv
;
298 struct bpf_zbuf_header
*bzh
;
300 if (pb
->zbuffer
== pb
->zbuf2
|| pb
->zbuffer
== NULL
) {
301 bzh
= (struct bpf_zbuf_header
*)pb
->zbuf1
;
302 if (bzh
->bzh_user_gen
!=
303 atomic_load_acq_int(&bzh
->bzh_kernel_gen
)) {
305 pb
->zbuffer
= (u_char
*)pb
->zbuf1
;
306 p
->buffer
= pb
->zbuffer
+ sizeof(*bzh
);
307 *cc
= bzh
->bzh_kernel_len
;
310 } else if (pb
->zbuffer
== pb
->zbuf1
) {
311 bzh
= (struct bpf_zbuf_header
*)pb
->zbuf2
;
312 if (bzh
->bzh_user_gen
!=
313 atomic_load_acq_int(&bzh
->bzh_kernel_gen
)) {
315 pb
->zbuffer
= (u_char
*)pb
->zbuf2
;
316 p
->buffer
= pb
->zbuffer
+ sizeof(*bzh
);
317 *cc
= bzh
->bzh_kernel_len
;
326 * pcap_next_zbuf() -- Similar to pcap_next_zbuf_shm(), except wait using
327 * select() for data or a timeout, and possibly force rotation of the buffer
328 * in the event we time out or are in immediate mode. Invoke the shared
329 * memory check before doing system calls in order to avoid doing avoidable
333 pcap_next_zbuf(pcap_t
*p
, int *cc
)
335 struct pcap_bpf
*pb
= p
->priv
;
343 #define TSTOMILLI(ts) (((ts)->tv_sec * 1000) + ((ts)->tv_nsec / 1000000))
345 * Start out by seeing whether anything is waiting by checking the
346 * next shared memory buffer for data.
348 data
= pcap_next_zbuf_shm(p
, cc
);
352 * If a previous sleep was interrupted due to signal delivery, make
353 * sure that the timeout gets adjusted accordingly. This requires
354 * that we analyze when the timeout should be been expired, and
355 * subtract the current time from that. If after this operation,
356 * our timeout is less then or equal to zero, handle it like a
359 tmout
= p
->opt
.timeout
;
361 (void) clock_gettime(CLOCK_MONOTONIC
, &cur
);
362 if (pb
->interrupted
&& p
->opt
.timeout
) {
363 expire
= TSTOMILLI(&pb
->firstsel
) + p
->opt
.timeout
;
364 tmout
= expire
- TSTOMILLI(&cur
);
368 data
= pcap_next_zbuf_shm(p
, cc
);
371 if (ioctl(p
->fd
, BIOCROTZBUF
, &bz
) < 0) {
372 pcap_fmt_errmsg_for_errno(p
->errbuf
,
373 PCAP_ERRBUF_SIZE
, errno
, "BIOCROTZBUF");
376 return (pcap_next_zbuf_shm(p
, cc
));
380 * No data in the buffer, so must use select() to wait for data or
381 * the next timeout. Note that we only call select if the handle
382 * is in blocking mode.
386 FD_SET(p
->fd
, &r_set
);
388 tv
.tv_sec
= tmout
/ 1000;
389 tv
.tv_usec
= (tmout
* 1000) % 1000000;
391 r
= select(p
->fd
+ 1, &r_set
, NULL
, NULL
,
392 p
->opt
.timeout
!= 0 ? &tv
: NULL
);
393 if (r
< 0 && errno
== EINTR
) {
394 if (!pb
->interrupted
&& p
->opt
.timeout
) {
400 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
407 * Check again for data, which may exist now that we've either been
408 * woken up as a result of data or timed out. Try the "there's data"
409 * case first since it doesn't require a system call.
411 data
= pcap_next_zbuf_shm(p
, cc
);
415 * Try forcing a buffer rotation to dislodge timed out or immediate
418 if (ioctl(p
->fd
, BIOCROTZBUF
, &bz
) < 0) {
419 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
420 errno
, "BIOCROTZBUF");
423 return (pcap_next_zbuf_shm(p
, cc
));
427 * Notify kernel that we are done with the buffer. We don't reset zbuffer so
428 * that we know which buffer to use next time around.
431 pcap_ack_zbuf(pcap_t
*p
)
433 struct pcap_bpf
*pb
= p
->priv
;
435 atomic_store_rel_int(&pb
->bzh
->bzh_user_gen
,
436 pb
->bzh
->bzh_kernel_gen
);
441 #endif /* HAVE_ZEROCOPY_BPF */
444 pcap_create_interface(const char *device _U_
, char *ebuf
)
448 p
= pcap_create_common(ebuf
, sizeof (struct pcap_bpf
));
452 p
->activate_op
= pcap_activate_bpf
;
453 p
->can_set_rfmon_op
= pcap_can_set_rfmon_bpf
;
456 * We claim that we support microsecond and nanosecond time
459 p
->tstamp_precision_count
= 2;
460 p
->tstamp_precision_list
= malloc(2 * sizeof(u_int
));
461 if (p
->tstamp_precision_list
== NULL
) {
462 pcap_fmt_errmsg_for_errno(ebuf
, PCAP_ERRBUF_SIZE
, errno
,
467 p
->tstamp_precision_list
[0] = PCAP_TSTAMP_PRECISION_MICRO
;
468 p
->tstamp_precision_list
[1] = PCAP_TSTAMP_PRECISION_NANO
;
469 #endif /* BIOCSTSTAMP */
474 * On success, returns a file descriptor for a BPF device.
475 * On failure, returns a PCAP_ERROR_ value, and sets p->errbuf.
478 bpf_open(char *errbuf
)
481 static const char cloning_device
[] = "/dev/bpf";
483 char device
[sizeof "/dev/bpf0000000000"];
484 static int no_cloning_bpf
= 0;
488 * Load the bpf driver, if it isn't already loaded,
489 * and create the BPF device entries, if they don't
492 if (bpf_load(errbuf
) == PCAP_ERROR
)
497 * First, unless we've already tried opening /dev/bpf and
498 * gotten ENOENT, try opening /dev/bpf.
499 * If it fails with ENOENT, remember that, so we don't try
500 * again, and try /dev/bpfN.
502 if (!no_cloning_bpf
&&
503 (fd
= open(cloning_device
, O_RDWR
)) == -1 &&
504 ((errno
!= EACCES
&& errno
!= ENOENT
) ||
505 (fd
= open(cloning_device
, O_RDONLY
)) == -1)) {
506 if (errno
!= ENOENT
) {
508 fd
= PCAP_ERROR_PERM_DENIED
;
511 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
512 errno
, "(cannot open device) %s", cloning_device
);
518 if (no_cloning_bpf
) {
520 * We don't have /dev/bpf.
521 * Go through all the /dev/bpfN minors and find one
525 (void)pcap_snprintf(device
, sizeof(device
), "/dev/bpf%d", n
++);
527 * Initially try a read/write open (to allow the inject
528 * method to work). If that fails due to permission
529 * issues, fall back to read-only. This allows a
530 * non-root user to be granted specific access to pcap
531 * capabilities via file permissions.
533 * XXX - we should have an API that has a flag that
534 * controls whether to open read-only or read-write,
535 * so that denial of permission to send (or inability
536 * to send, if sending packets isn't supported on
537 * the device in question) can be indicated at open
540 fd
= open(device
, O_RDWR
);
541 if (fd
== -1 && errno
== EACCES
)
542 fd
= open(device
, O_RDONLY
);
543 } while (fd
< 0 && errno
== EBUSY
);
547 * XXX better message for all minors used
556 * /dev/bpf0 doesn't exist, which
557 * means we probably have no BPF
560 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
561 "(there are no BPF devices)");
564 * We got EBUSY on at least one
565 * BPF device, so we have BPF
566 * devices, but all the ones
567 * that exist are busy.
569 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
570 "(all BPF devices are busy)");
576 * Got EACCES on the last device we tried,
577 * and EBUSY on all devices before that,
580 fd
= PCAP_ERROR_PERM_DENIED
;
581 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
582 errno
, "(cannot open BPF device) %s", device
);
587 * Some other problem.
590 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
591 errno
, "(cannot open BPF device) %s", device
);
600 * Open and bind to a device; used if we're not actually going to use
601 * the device, but are just testing whether it can be opened, or opening
602 * it to get information about it.
604 * Returns an error code on failure (always negative), and an FD for
605 * the now-bound BPF device on success (always non-negative).
608 bpf_open_and_bind(const char *name
, char *errbuf
)
614 * First, open a BPF device.
616 fd
= bpf_open(errbuf
);
618 return (fd
); /* fd is the appropriate error code */
621 * Now bind to the device.
623 (void)strncpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
624 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) < 0) {
629 * There's no such device.
632 return (PCAP_ERROR_NO_SUCH_DEVICE
);
636 * Return a "network down" indication, so that
637 * the application can report that rather than
638 * saying we had a mysterious failure and
639 * suggest that they report a problem to the
640 * libpcap developers.
643 return (PCAP_ERROR_IFACE_NOT_UP
);
646 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
647 errno
, "BIOCSETIF: %s", name
);
661 get_dlt_list(int fd
, int v
, struct bpf_dltlist
*bdlp
, char *ebuf
)
663 memset(bdlp
, 0, sizeof(*bdlp
));
664 if (ioctl(fd
, BIOCGDLTLIST
, (caddr_t
)bdlp
) == 0) {
668 bdlp
->bfl_list
= (u_int
*) malloc(sizeof(u_int
) * (bdlp
->bfl_len
+ 1));
669 if (bdlp
->bfl_list
== NULL
) {
670 pcap_fmt_errmsg_for_errno(ebuf
, PCAP_ERRBUF_SIZE
,
675 if (ioctl(fd
, BIOCGDLTLIST
, (caddr_t
)bdlp
) < 0) {
676 pcap_fmt_errmsg_for_errno(ebuf
, PCAP_ERRBUF_SIZE
,
677 errno
, "BIOCGDLTLIST");
678 free(bdlp
->bfl_list
);
683 * OK, for real Ethernet devices, add DLT_DOCSIS to the
684 * list, so that an application can let you choose it,
685 * in case you're capturing DOCSIS traffic that a Cisco
686 * Cable Modem Termination System is putting out onto
687 * an Ethernet (it doesn't put an Ethernet header onto
688 * the wire, it puts raw DOCSIS frames out on the wire
689 * inside the low-level Ethernet framing).
691 * A "real Ethernet device" is defined here as a device
692 * that has a link-layer type of DLT_EN10MB and that has
693 * no alternate link-layer types; that's done to exclude
694 * 802.11 interfaces (which might or might not be the
695 * right thing to do, but I suspect it is - Ethernet <->
696 * 802.11 bridges would probably badly mishandle frames
697 * that don't have Ethernet headers).
699 * On Solaris with BPF, Ethernet devices also offer
700 * DLT_IPNET, so we, if DLT_IPNET is defined, we don't
701 * treat it as an indication that the device isn't an
704 if (v
== DLT_EN10MB
) {
706 for (i
= 0; i
< bdlp
->bfl_len
; i
++) {
707 if (bdlp
->bfl_list
[i
] != DLT_EN10MB
709 && bdlp
->bfl_list
[i
] != DLT_IPNET
718 * We reserved one more slot at the end of
721 bdlp
->bfl_list
[bdlp
->bfl_len
] = DLT_DOCSIS
;
727 * EINVAL just means "we don't support this ioctl on
728 * this device"; don't treat it as an error.
730 if (errno
!= EINVAL
) {
731 pcap_fmt_errmsg_for_errno(ebuf
, PCAP_ERRBUF_SIZE
,
732 errno
, "BIOCGDLTLIST");
740 #if defined(__APPLE__)
742 pcap_can_set_rfmon_bpf(pcap_t
*p
)
744 struct utsname osinfo
;
748 struct bpf_dltlist bdl
;
752 * The joys of monitor mode on Mac OS X/OS X/macOS.
754 * Prior to 10.4, it's not supported at all.
756 * In 10.4, if adapter enN supports monitor mode, there's a
757 * wltN adapter corresponding to it; you open it, instead of
758 * enN, to get monitor mode. You get whatever link-layer
759 * headers it supplies.
761 * In 10.5, and, we assume, later releases, if adapter enN
762 * supports monitor mode, it offers, among its selectable
763 * DLT_ values, values that let you get the 802.11 header;
764 * selecting one of those values puts the adapter into monitor
765 * mode (i.e., you can't get 802.11 headers except in monitor
766 * mode, and you can't get Ethernet headers in monitor mode).
768 if (uname(&osinfo
) == -1) {
770 * Can't get the OS version; just say "no".
775 * We assume osinfo.sysname is "Darwin", because
776 * __APPLE__ is defined. We just check the version.
778 if (osinfo
.release
[0] < '8' && osinfo
.release
[1] == '.') {
780 * 10.3 (Darwin 7.x) or earlier.
781 * Monitor mode not supported.
785 if (osinfo
.release
[0] == '8' && osinfo
.release
[1] == '.') {
787 * 10.4 (Darwin 8.x). s/en/wlt/, and check
788 * whether the device exists.
790 if (strncmp(p
->opt
.device
, "en", 2) != 0) {
792 * Not an enN device; no monitor mode.
796 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
798 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
802 pcap_strlcpy(ifr
.ifr_name
, "wlt", sizeof(ifr
.ifr_name
));
803 pcap_strlcat(ifr
.ifr_name
, p
->opt
.device
+ 2, sizeof(ifr
.ifr_name
));
804 if (ioctl(fd
, SIOCGIFFLAGS
, (char *)&ifr
) < 0) {
817 * Everything else is 10.5 or later; for those,
818 * we just open the enN device, and check whether
819 * we have any 802.11 devices.
821 * First, open a BPF device.
823 fd
= bpf_open(p
->errbuf
);
825 return (fd
); /* fd is the appropriate error code */
828 * Now bind to the device.
830 (void)strncpy(ifr
.ifr_name
, p
->opt
.device
, sizeof(ifr
.ifr_name
));
831 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) < 0) {
836 * There's no such device.
839 return (PCAP_ERROR_NO_SUCH_DEVICE
);
843 * Return a "network down" indication, so that
844 * the application can report that rather than
845 * saying we had a mysterious failure and
846 * suggest that they report a problem to the
847 * libpcap developers.
850 return (PCAP_ERROR_IFACE_NOT_UP
);
853 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
854 errno
, "BIOCSETIF: %s", p
->opt
.device
);
861 * We know the default link type -- now determine all the DLTs
862 * this interface supports. If this fails with EINVAL, it's
863 * not fatal; we just don't get to use the feature later.
864 * (We don't care about DLT_DOCSIS, so we pass DLT_NULL
865 * as the default DLT for this adapter.)
867 if (get_dlt_list(fd
, DLT_NULL
, &bdl
, p
->errbuf
) == PCAP_ERROR
) {
871 if (find_802_11(&bdl
) != -1) {
873 * We have an 802.11 DLT, so we can set monitor mode.
881 #endif /* BIOCGDLTLIST */
884 #elif defined(HAVE_BSD_IEEE80211)
886 pcap_can_set_rfmon_bpf(pcap_t
*p
)
890 ret
= monitor_mode(p
, 0);
891 if (ret
== PCAP_ERROR_RFMON_NOTSUP
)
892 return (0); /* not an error, just a "can't do" */
894 return (1); /* success */
899 pcap_can_set_rfmon_bpf(pcap_t
*p _U_
)
906 pcap_stats_bpf(pcap_t
*p
, struct pcap_stat
*ps
)
911 * "ps_recv" counts packets handed to the filter, not packets
912 * that passed the filter. This includes packets later dropped
913 * because we ran out of buffer space.
915 * "ps_drop" counts packets dropped inside the BPF device
916 * because we ran out of buffer space. It doesn't count
917 * packets dropped by the interface driver. It counts
918 * only packets that passed the filter.
920 * Both statistics include packets not yet read from the kernel
921 * by libpcap, and thus not yet seen by the application.
923 if (ioctl(p
->fd
, BIOCGSTATS
, (caddr_t
)&s
) < 0) {
924 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
925 errno
, "BIOCGSTATS");
929 ps
->ps_recv
= s
.bs_recv
;
930 ps
->ps_drop
= s
.bs_drop
;
936 pcap_read_bpf(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
938 struct pcap_bpf
*pb
= p
->priv
;
941 register u_char
*bp
, *ep
;
946 #ifdef HAVE_ZEROCOPY_BPF
952 * Has "pcap_breakloop()" been called?
956 * Yes - clear the flag that indicates that it
957 * has, and return PCAP_ERROR_BREAK to indicate
958 * that we were told to break out of the loop.
961 return (PCAP_ERROR_BREAK
);
966 * When reading without zero-copy from a file descriptor, we
967 * use a single buffer and return a length of data in the
968 * buffer. With zero-copy, we update the p->buffer pointer
969 * to point at whatever underlying buffer contains the next
970 * data and update cc to reflect the data found in the
973 #ifdef HAVE_ZEROCOPY_BPF
975 if (p
->buffer
!= NULL
)
977 i
= pcap_next_zbuf(p
, &cc
);
985 cc
= (int)read(p
->fd
, p
->buffer
, p
->bufsize
);
988 /* Don't choke when we get ptraced */
997 * Sigh. More AIX wonderfulness.
999 * For some unknown reason the uiomove()
1000 * operation in the bpf kernel extension
1001 * used to copy the buffer into user
1002 * space sometimes returns EFAULT. I have
1003 * no idea why this is the case given that
1004 * a kernel debugger shows the user buffer
1005 * is correct. This problem appears to
1006 * be mostly mitigated by the memset of
1007 * the buffer before it is first used.
1008 * Very strange.... Shaun Clowes
1010 * In any case this means that we shouldn't
1011 * treat EFAULT as a fatal error; as we
1012 * don't have an API for returning
1013 * a "some packets were dropped since
1014 * the last packet you saw" indication,
1015 * we just ignore EFAULT and keep reading.
1025 * The device on which we're capturing
1028 * XXX - we should really return
1029 * PCAP_ERROR_IFACE_NOT_UP, but
1030 * pcap_dispatch() etc. aren't
1031 * defined to retur that.
1033 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1034 "The interface went down");
1035 return (PCAP_ERROR
);
1037 #if defined(sun) && !defined(BSD) && !defined(__svr4__) && !defined(__SVR4)
1039 * Due to a SunOS bug, after 2^31 bytes, the kernel
1040 * file offset overflows and read fails with EINVAL.
1041 * The lseek() to 0 will fix things.
1044 if (lseek(p
->fd
, 0L, SEEK_CUR
) +
1046 (void)lseek(p
->fd
, 0L, SEEK_SET
);
1052 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1054 return (PCAP_ERROR
);
1056 bp
= (u_char
*)p
->buffer
;
1061 * Loop through each packet.
1064 #define bhp ((struct bpf_xhdr *)bp)
1066 #define bhp ((struct bpf_hdr *)bp)
1073 register u_int caplen
, hdrlen
;
1076 * Has "pcap_breakloop()" been called?
1077 * If so, return immediately - if we haven't read any
1078 * packets, clear the flag and return PCAP_ERROR_BREAK
1079 * to indicate that we were told to break out of the loop,
1080 * otherwise leave the flag set, so that the *next* call
1081 * will break out of the loop without having read any
1082 * packets, and return the number of packets we've
1085 if (p
->break_loop
) {
1087 p
->cc
= (int)(ep
- bp
);
1089 * ep is set based on the return value of read(),
1090 * but read() from a BPF device doesn't necessarily
1091 * return a value that's a multiple of the alignment
1092 * value for BPF_WORDALIGN(). However, whenever we
1093 * increment bp, we round up the increment value by
1094 * a value rounded up by BPF_WORDALIGN(), so we
1095 * could increment bp past ep after processing the
1096 * last packet in the buffer.
1098 * We treat ep < bp as an indication that this
1099 * happened, and just set p->cc to 0.
1105 return (PCAP_ERROR_BREAK
);
1110 caplen
= bhp
->bh_caplen
;
1111 hdrlen
= bhp
->bh_hdrlen
;
1112 datap
= bp
+ hdrlen
;
1114 * Short-circuit evaluation: if using BPF filter
1115 * in kernel, no need to do it now - we already know
1116 * the packet passed the filter.
1119 * Note: the filter code was generated assuming
1120 * that p->fddipad was the amount of padding
1121 * before the header, as that's what's required
1122 * in the kernel, so we run the filter before
1123 * skipping that padding.
1126 if (pb
->filtering_in_kernel
||
1127 pcap_filter(p
->fcode
.bf_insns
, datap
, bhp
->bh_datalen
, caplen
)) {
1128 struct pcap_pkthdr pkthdr
;
1132 bt
.sec
= bhp
->bh_tstamp
.bt_sec
;
1133 bt
.frac
= bhp
->bh_tstamp
.bt_frac
;
1134 if (p
->opt
.tstamp_precision
== PCAP_TSTAMP_PRECISION_NANO
) {
1137 bintime2timespec(&bt
, &ts
);
1138 pkthdr
.ts
.tv_sec
= ts
.tv_sec
;
1139 pkthdr
.ts
.tv_usec
= ts
.tv_nsec
;
1143 bintime2timeval(&bt
, &tv
);
1144 pkthdr
.ts
.tv_sec
= tv
.tv_sec
;
1145 pkthdr
.ts
.tv_usec
= tv
.tv_usec
;
1148 pkthdr
.ts
.tv_sec
= bhp
->bh_tstamp
.tv_sec
;
1151 * AIX's BPF returns seconds/nanoseconds time
1152 * stamps, not seconds/microseconds time stamps.
1154 pkthdr
.ts
.tv_usec
= bhp
->bh_tstamp
.tv_usec
/1000;
1156 pkthdr
.ts
.tv_usec
= bhp
->bh_tstamp
.tv_usec
;
1158 #endif /* BIOCSTSTAMP */
1161 pkthdr
.caplen
= caplen
- pad
;
1164 if (bhp
->bh_datalen
> pad
)
1165 pkthdr
.len
= bhp
->bh_datalen
- pad
;
1170 pkthdr
.caplen
= caplen
;
1171 pkthdr
.len
= bhp
->bh_datalen
;
1173 (*callback
)(user
, &pkthdr
, datap
);
1174 bp
+= BPF_WORDALIGN(caplen
+ hdrlen
);
1175 if (++n
>= cnt
&& !PACKET_COUNT_IS_UNLIMITED(cnt
)) {
1177 p
->cc
= (int)(ep
- bp
);
1179 * See comment above about p->cc < 0.
1189 bp
+= BPF_WORDALIGN(caplen
+ hdrlen
);
1198 pcap_inject_bpf(pcap_t
*p
, const void *buf
, int size
)
1202 ret
= (int)write(p
->fd
, buf
, size
);
1204 if (ret
== -1 && errno
== EAFNOSUPPORT
) {
1206 * In some versions of macOS, there's a bug wherein setting
1207 * the BIOCSHDRCMPLT flag causes writes to fail; see, for
1210 * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/BIOCSHDRCMPLT-10.3.3.patch
1212 * So, if, on macOS, we get EAFNOSUPPORT from the write, we
1213 * assume it's due to that bug, and turn off that flag
1214 * and try again. If we succeed, it either means that
1215 * somebody applied the fix from that URL, or other patches
1218 * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/
1220 * and are running a Darwin kernel with those fixes, or
1221 * that Apple fixed the problem in some macOS release.
1223 u_int spoof_eth_src
= 0;
1225 if (ioctl(p
->fd
, BIOCSHDRCMPLT
, &spoof_eth_src
) == -1) {
1226 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1227 errno
, "send: can't turn off BIOCSHDRCMPLT");
1228 return (PCAP_ERROR
);
1232 * Now try the write again.
1234 ret
= (int)write(p
->fd
, buf
, size
);
1236 #endif /* __APPLE__ */
1238 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1240 return (PCAP_ERROR
);
1247 bpf_odminit(char *errbuf
)
1251 if (odm_initialize() == -1) {
1252 if (odm_err_msg(odmerrno
, &errstr
) == -1)
1253 errstr
= "Unknown error";
1254 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1255 "bpf_load: odm_initialize failed: %s",
1257 return (PCAP_ERROR
);
1260 if ((odmlockid
= odm_lock("/etc/objrepos/config_lock", ODM_WAIT
)) == -1) {
1261 if (odm_err_msg(odmerrno
, &errstr
) == -1)
1262 errstr
= "Unknown error";
1263 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1264 "bpf_load: odm_lock of /etc/objrepos/config_lock failed: %s",
1266 (void)odm_terminate();
1267 return (PCAP_ERROR
);
1274 bpf_odmcleanup(char *errbuf
)
1278 if (odm_unlock(odmlockid
) == -1) {
1279 if (errbuf
!= NULL
) {
1280 if (odm_err_msg(odmerrno
, &errstr
) == -1)
1281 errstr
= "Unknown error";
1282 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1283 "bpf_load: odm_unlock failed: %s",
1286 return (PCAP_ERROR
);
1289 if (odm_terminate() == -1) {
1290 if (errbuf
!= NULL
) {
1291 if (odm_err_msg(odmerrno
, &errstr
) == -1)
1292 errstr
= "Unknown error";
1293 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1294 "bpf_load: odm_terminate failed: %s",
1297 return (PCAP_ERROR
);
1304 bpf_load(char *errbuf
)
1308 int numminors
, i
, rc
;
1311 struct bpf_config cfg_bpf
;
1312 struct cfg_load cfg_ld
;
1313 struct cfg_kmod cfg_km
;
1316 * This is very very close to what happens in the real implementation
1317 * but I've fixed some (unlikely) bug situations.
1322 if (bpf_odminit(errbuf
) == PCAP_ERROR
)
1323 return (PCAP_ERROR
);
1325 major
= genmajor(BPF_NAME
);
1327 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
1328 errno
, "bpf_load: genmajor failed");
1329 (void)bpf_odmcleanup(NULL
);
1330 return (PCAP_ERROR
);
1333 minors
= getminor(major
, &numminors
, BPF_NAME
);
1335 minors
= genminor("bpf", major
, 0, BPF_MINORS
, 1, 1);
1337 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
1338 errno
, "bpf_load: genminor failed");
1339 (void)bpf_odmcleanup(NULL
);
1340 return (PCAP_ERROR
);
1344 if (bpf_odmcleanup(errbuf
) == PCAP_ERROR
)
1345 return (PCAP_ERROR
);
1347 rc
= stat(BPF_NODE
"0", &sbuf
);
1348 if (rc
== -1 && errno
!= ENOENT
) {
1349 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
1350 errno
, "bpf_load: can't stat %s", BPF_NODE
"0");
1351 return (PCAP_ERROR
);
1354 if (rc
== -1 || getmajor(sbuf
.st_rdev
) != major
) {
1355 for (i
= 0; i
< BPF_MINORS
; i
++) {
1356 pcap_snprintf(buf
, sizeof(buf
), "%s%d", BPF_NODE
, i
);
1358 if (mknod(buf
, S_IRUSR
| S_IFCHR
, domakedev(major
, i
)) == -1) {
1359 pcap_fmt_errmsg_for_errno(errbuf
,
1360 PCAP_ERRBUF_SIZE
, errno
,
1361 "bpf_load: can't mknod %s", buf
);
1362 return (PCAP_ERROR
);
1367 /* Check if the driver is loaded */
1368 memset(&cfg_ld
, 0x0, sizeof(cfg_ld
));
1369 pcap_snprintf(buf
, sizeof(buf
), "%s/%s", DRIVER_PATH
, BPF_NAME
);
1371 if ((sysconfig(SYS_QUERYLOAD
, (void *)&cfg_ld
, sizeof(cfg_ld
)) == -1) ||
1372 (cfg_ld
.kmid
== 0)) {
1373 /* Driver isn't loaded, load it now */
1374 if (sysconfig(SYS_SINGLELOAD
, (void *)&cfg_ld
, sizeof(cfg_ld
)) == -1) {
1375 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
1376 errno
, "bpf_load: could not load driver");
1377 return (PCAP_ERROR
);
1381 /* Configure the driver */
1382 cfg_km
.cmd
= CFG_INIT
;
1383 cfg_km
.kmid
= cfg_ld
.kmid
;
1384 cfg_km
.mdilen
= sizeof(cfg_bpf
);
1385 cfg_km
.mdiptr
= (void *)&cfg_bpf
;
1386 for (i
= 0; i
< BPF_MINORS
; i
++) {
1387 cfg_bpf
.devno
= domakedev(major
, i
);
1388 if (sysconfig(SYS_CFGKMOD
, (void *)&cfg_km
, sizeof(cfg_km
)) == -1) {
1389 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
1390 errno
, "bpf_load: could not configure driver");
1391 return (PCAP_ERROR
);
1402 * Undo any operations done when opening the device when necessary.
1405 pcap_cleanup_bpf(pcap_t
*p
)
1407 struct pcap_bpf
*pb
= p
->priv
;
1408 #ifdef HAVE_BSD_IEEE80211
1410 struct ifmediareq req
;
1414 if (pb
->must_do_on_close
!= 0) {
1416 * There's something we have to do when closing this
1419 #ifdef HAVE_BSD_IEEE80211
1420 if (pb
->must_do_on_close
& MUST_CLEAR_RFMON
) {
1422 * We put the interface into rfmon mode;
1423 * take it out of rfmon mode.
1425 * XXX - if somebody else wants it in rfmon
1426 * mode, this code cannot know that, so it'll take
1427 * it out of rfmon mode.
1429 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
1432 "Can't restore interface flags (socket() failed: %s).\n"
1433 "Please adjust manually.\n",
1436 memset(&req
, 0, sizeof(req
));
1437 strncpy(req
.ifm_name
, pb
->device
,
1438 sizeof(req
.ifm_name
));
1439 if (ioctl(sock
, SIOCGIFMEDIA
, &req
) < 0) {
1441 "Can't restore interface flags (SIOCGIFMEDIA failed: %s).\n"
1442 "Please adjust manually.\n",
1445 if (req
.ifm_current
& IFM_IEEE80211_MONITOR
) {
1447 * Rfmon mode is currently on;
1450 memset(&ifr
, 0, sizeof(ifr
));
1451 (void)strncpy(ifr
.ifr_name
,
1453 sizeof(ifr
.ifr_name
));
1455 req
.ifm_current
& ~IFM_IEEE80211_MONITOR
;
1456 if (ioctl(sock
, SIOCSIFMEDIA
,
1459 "Can't restore interface flags (SIOCSIFMEDIA failed: %s).\n"
1460 "Please adjust manually.\n",
1468 #endif /* HAVE_BSD_IEEE80211 */
1470 #if defined(__FreeBSD__) && defined(SIOCIFCREATE2)
1472 * Attempt to destroy the usbusN interface that we created.
1474 if (pb
->must_do_on_close
& MUST_DESTROY_USBUS
) {
1475 if (if_nametoindex(pb
->device
) > 0) {
1478 s
= socket(AF_LOCAL
, SOCK_DGRAM
, 0);
1480 pcap_strlcpy(ifr
.ifr_name
, pb
->device
,
1481 sizeof(ifr
.ifr_name
));
1482 ioctl(s
, SIOCIFDESTROY
, &ifr
);
1487 #endif /* defined(__FreeBSD__) && defined(SIOCIFCREATE2) */
1489 * Take this pcap out of the list of pcaps for which we
1490 * have to take the interface out of some mode.
1492 pcap_remove_from_pcaps_to_close(p
);
1493 pb
->must_do_on_close
= 0;
1496 #ifdef HAVE_ZEROCOPY_BPF
1499 * Delete the mappings. Note that p->buffer gets
1500 * initialized to one of the mmapped regions in
1501 * this case, so do not try and free it directly;
1502 * null it out so that pcap_cleanup_live_common()
1503 * doesn't try to free it.
1505 if (pb
->zbuf1
!= MAP_FAILED
&& pb
->zbuf1
!= NULL
)
1506 (void) munmap(pb
->zbuf1
, pb
->zbufsize
);
1507 if (pb
->zbuf2
!= MAP_FAILED
&& pb
->zbuf2
!= NULL
)
1508 (void) munmap(pb
->zbuf2
, pb
->zbufsize
);
1512 if (pb
->device
!= NULL
) {
1516 pcap_cleanup_live_common(p
);
1520 check_setif_failure(pcap_t
*p
, int error
)
1528 if (error
== ENXIO
) {
1530 * No such device exists.
1533 if (p
->opt
.rfmon
&& strncmp(p
->opt
.device
, "wlt", 3) == 0) {
1535 * Monitor mode was requested, and we're trying
1536 * to open a "wltN" device. Assume that this
1537 * is 10.4 and that we were asked to open an
1538 * "enN" device; if that device exists, return
1539 * "monitor mode not supported on the device".
1541 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
1543 pcap_strlcpy(ifr
.ifr_name
, "en",
1544 sizeof(ifr
.ifr_name
));
1545 pcap_strlcat(ifr
.ifr_name
, p
->opt
.device
+ 3,
1546 sizeof(ifr
.ifr_name
));
1547 if (ioctl(fd
, SIOCGIFFLAGS
, (char *)&ifr
) < 0) {
1549 * We assume this failed because
1550 * the underlying device doesn't
1553 err
= PCAP_ERROR_NO_SUCH_DEVICE
;
1554 pcap_fmt_errmsg_for_errno(p
->errbuf
,
1555 PCAP_ERRBUF_SIZE
, errno
,
1556 "SIOCGIFFLAGS on %s failed",
1560 * The underlying "enN" device
1561 * exists, but there's no
1562 * corresponding "wltN" device;
1563 * that means that the "enN"
1564 * device doesn't support
1565 * monitor mode, probably because
1566 * it's an Ethernet device rather
1567 * than a wireless device.
1569 err
= PCAP_ERROR_RFMON_NOTSUP
;
1574 * We can't find out whether there's
1575 * an underlying "enN" device, so
1576 * just report "no such device".
1578 err
= PCAP_ERROR_NO_SUCH_DEVICE
;
1579 pcap_fmt_errmsg_for_errno(p
->errbuf
,
1580 errno
, PCAP_ERRBUF_SIZE
,
1589 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1590 errno
, "BIOCSETIF failed");
1591 return (PCAP_ERROR_NO_SUCH_DEVICE
);
1592 } else if (errno
== ENETDOWN
) {
1594 * Return a "network down" indication, so that
1595 * the application can report that rather than
1596 * saying we had a mysterious failure and
1597 * suggest that they report a problem to the
1598 * libpcap developers.
1600 return (PCAP_ERROR_IFACE_NOT_UP
);
1603 * Some other error; fill in the error string, and
1604 * return PCAP_ERROR.
1606 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1607 errno
, "BIOCSETIF: %s", p
->opt
.device
);
1608 return (PCAP_ERROR
);
1613 * Default capture buffer size.
1614 * 32K isn't very much for modern machines with fast networks; we
1615 * pick .5M, as that's the maximum on at least some systems with BPF.
1617 * However, on AIX 3.5, the larger buffer sized caused unrecoverable
1618 * read failures under stress, so we leave it as 32K; yet another
1619 * place where AIX's BPF is broken.
1622 #define DEFAULT_BUFSIZE 32768
1624 #define DEFAULT_BUFSIZE 524288
1628 pcap_activate_bpf(pcap_t
*p
)
1630 struct pcap_bpf
*pb
= p
->priv
;
1632 #ifdef HAVE_BSD_IEEE80211
1639 char *ifrname
= ifr
.lifr_name
;
1640 const size_t ifnamsiz
= sizeof(ifr
.lifr_name
);
1643 char *ifrname
= ifr
.ifr_name
;
1644 const size_t ifnamsiz
= sizeof(ifr
.ifr_name
);
1646 struct bpf_version bv
;
1649 char *wltdev
= NULL
;
1652 struct bpf_dltlist bdl
;
1653 #if defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)
1656 #endif /* BIOCGDLTLIST */
1657 #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
1658 u_int spoof_eth_src
= 1;
1661 struct bpf_insn total_insn
;
1662 struct bpf_program total_prog
;
1663 struct utsname osinfo
;
1664 int have_osinfo
= 0;
1665 #ifdef HAVE_ZEROCOPY_BPF
1667 u_int bufmode
, zbufmax
;
1670 fd
= bpf_open(p
->errbuf
);
1678 if (ioctl(fd
, BIOCVERSION
, (caddr_t
)&bv
) < 0) {
1679 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1680 errno
, "BIOCVERSION");
1681 status
= PCAP_ERROR
;
1684 if (bv
.bv_major
!= BPF_MAJOR_VERSION
||
1685 bv
.bv_minor
< BPF_MINOR_VERSION
) {
1686 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1687 "kernel bpf filter out of date");
1688 status
= PCAP_ERROR
;
1693 * Turn a negative snapshot value (invalid), a snapshot value of
1694 * 0 (unspecified), or a value bigger than the normal maximum
1695 * value, into the maximum allowed value.
1697 * If some application really *needs* a bigger snapshot
1698 * length, we should just increase MAXIMUM_SNAPLEN.
1700 if (p
->snapshot
<= 0 || p
->snapshot
> MAXIMUM_SNAPLEN
)
1701 p
->snapshot
= MAXIMUM_SNAPLEN
;
1703 #if defined(LIFNAMSIZ) && defined(ZONENAME_MAX) && defined(lifr_zoneid)
1705 * Retrieve the zoneid of the zone we are currently executing in.
1707 if ((ifr
.lifr_zoneid
= getzoneid()) == -1) {
1708 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1709 errno
, "getzoneid()");
1710 status
= PCAP_ERROR
;
1714 * Check if the given source datalink name has a '/' separated
1715 * zonename prefix string. The zonename prefixed source datalink can
1716 * be used by pcap consumers in the Solaris global zone to capture
1717 * traffic on datalinks in non-global zones. Non-global zones
1718 * do not have access to datalinks outside of their own namespace.
1720 if ((zonesep
= strchr(p
->opt
.device
, '/')) != NULL
) {
1721 char path_zname
[ZONENAME_MAX
];
1725 if (ifr
.lifr_zoneid
!= GLOBAL_ZONEID
) {
1726 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1727 "zonename/linkname only valid in global zone.");
1728 status
= PCAP_ERROR
;
1731 znamelen
= zonesep
- p
->opt
.device
;
1732 (void) pcap_strlcpy(path_zname
, p
->opt
.device
, znamelen
+ 1);
1733 ifr
.lifr_zoneid
= getzoneidbyname(path_zname
);
1734 if (ifr
.lifr_zoneid
== -1) {
1735 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1736 errno
, "getzoneidbyname(%s)", path_zname
);
1737 status
= PCAP_ERROR
;
1740 lnamep
= strdup(zonesep
+ 1);
1741 if (lnamep
== NULL
) {
1742 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1744 status
= PCAP_ERROR
;
1747 free(p
->opt
.device
);
1748 p
->opt
.device
= lnamep
;
1752 pb
->device
= strdup(p
->opt
.device
);
1753 if (pb
->device
== NULL
) {
1754 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1756 status
= PCAP_ERROR
;
1761 * Attempt to find out the version of the OS on which we're running.
1763 if (uname(&osinfo
) == 0)
1768 * See comment in pcap_can_set_rfmon_bpf() for an explanation
1769 * of why we check the version number.
1774 * We assume osinfo.sysname is "Darwin", because
1775 * __APPLE__ is defined. We just check the version.
1777 if (osinfo
.release
[0] < '8' &&
1778 osinfo
.release
[1] == '.') {
1780 * 10.3 (Darwin 7.x) or earlier.
1782 status
= PCAP_ERROR_RFMON_NOTSUP
;
1785 if (osinfo
.release
[0] == '8' &&
1786 osinfo
.release
[1] == '.') {
1788 * 10.4 (Darwin 8.x). s/en/wlt/
1790 if (strncmp(p
->opt
.device
, "en", 2) != 0) {
1792 * Not an enN device; check
1793 * whether the device even exists.
1795 sockfd
= socket(AF_INET
, SOCK_DGRAM
, 0);
1797 pcap_strlcpy(ifrname
,
1798 p
->opt
.device
, ifnamsiz
);
1799 if (ioctl(sockfd
, SIOCGIFFLAGS
,
1800 (char *)&ifr
) < 0) {
1808 status
= PCAP_ERROR_NO_SUCH_DEVICE
;
1809 pcap_fmt_errmsg_for_errno(p
->errbuf
,
1812 "SIOCGIFFLAGS failed");
1814 status
= PCAP_ERROR_RFMON_NOTSUP
;
1818 * We can't find out whether
1819 * the device exists, so just
1820 * report "no such device".
1822 status
= PCAP_ERROR_NO_SUCH_DEVICE
;
1823 pcap_fmt_errmsg_for_errno(p
->errbuf
,
1824 PCAP_ERRBUF_SIZE
, errno
,
1829 wltdev
= malloc(strlen(p
->opt
.device
) + 2);
1830 if (wltdev
== NULL
) {
1831 pcap_fmt_errmsg_for_errno(p
->errbuf
,
1832 PCAP_ERRBUF_SIZE
, errno
,
1834 status
= PCAP_ERROR
;
1837 strcpy(wltdev
, "wlt");
1838 strcat(wltdev
, p
->opt
.device
+ 2);
1839 free(p
->opt
.device
);
1840 p
->opt
.device
= wltdev
;
1843 * Everything else is 10.5 or later; for those,
1844 * we just open the enN device, and set the DLT.
1848 #endif /* __APPLE__ */
1851 * If this is FreeBSD, and the device name begins with "usbus",
1852 * try to create the interface if it's not available.
1854 #if defined(__FreeBSD__) && defined(SIOCIFCREATE2)
1855 if (strncmp(p
->opt
.device
, usbus_prefix
, USBUS_PREFIX_LEN
) == 0) {
1857 * Do we already have an interface with that name?
1859 if (if_nametoindex(p
->opt
.device
) == 0) {
1861 * No. We need to create it, and, if we
1862 * succeed, remember that we should destroy
1863 * it when the pcap_t is closed.
1868 * Open a socket to use for ioctls to
1869 * create the interface.
1871 s
= socket(AF_LOCAL
, SOCK_DGRAM
, 0);
1873 pcap_fmt_errmsg_for_errno(p
->errbuf
,
1874 PCAP_ERRBUF_SIZE
, errno
,
1875 "Can't open socket");
1876 status
= PCAP_ERROR
;
1881 * If we haven't already done so, arrange to have
1882 * "pcap_close_all()" called when we exit.
1884 if (!pcap_do_addexit(p
)) {
1886 * "atexit()" failed; don't create the
1887 * interface, just give up.
1889 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1892 status
= PCAP_ERROR
;
1897 * Create the interface.
1899 pcap_strlcpy(ifr
.ifr_name
, p
->opt
.device
, sizeof(ifr
.ifr_name
));
1900 if (ioctl(s
, SIOCIFCREATE2
, &ifr
) < 0) {
1901 if (errno
== EINVAL
) {
1902 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1903 "Invalid USB bus interface %s",
1906 pcap_fmt_errmsg_for_errno(p
->errbuf
,
1907 PCAP_ERRBUF_SIZE
, errno
,
1908 "Can't create interface for %s",
1912 status
= PCAP_ERROR
;
1917 * Make sure we clean this up when we close.
1919 pb
->must_do_on_close
|= MUST_DESTROY_USBUS
;
1922 * Add this to the list of pcaps to close when we exit.
1924 pcap_add_to_pcaps_to_close(p
);
1927 #endif /* defined(__FreeBSD__) && defined(SIOCIFCREATE2) */
1929 #ifdef HAVE_ZEROCOPY_BPF
1931 * If the BPF extension to set buffer mode is present, try setting
1932 * the mode to zero-copy. If that fails, use regular buffering. If
1933 * it succeeds but other setup fails, return an error to the user.
1935 bufmode
= BPF_BUFMODE_ZBUF
;
1936 if (ioctl(fd
, BIOCSETBUFMODE
, (caddr_t
)&bufmode
) == 0) {
1938 * We have zerocopy BPF; use it.
1943 * How to pick a buffer size: first, query the maximum buffer
1944 * size supported by zero-copy. This also lets us quickly
1945 * determine whether the kernel generally supports zero-copy.
1946 * Then, if a buffer size was specified, use that, otherwise
1947 * query the default buffer size, which reflects kernel
1948 * policy for a desired default. Round to the nearest page
1951 if (ioctl(fd
, BIOCGETZMAX
, (caddr_t
)&zbufmax
) < 0) {
1952 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1953 errno
, "BIOCGETZMAX");
1954 status
= PCAP_ERROR
;
1958 if (p
->opt
.buffer_size
!= 0) {
1960 * A buffer size was explicitly specified; use it.
1962 v
= p
->opt
.buffer_size
;
1964 if ((ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) ||
1965 v
< DEFAULT_BUFSIZE
)
1966 v
= DEFAULT_BUFSIZE
;
1969 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) /* to any y */
1971 pb
->zbufsize
= roundup(v
, getpagesize());
1972 if (pb
->zbufsize
> zbufmax
)
1973 pb
->zbufsize
= zbufmax
;
1974 pb
->zbuf1
= mmap(NULL
, pb
->zbufsize
, PROT_READ
| PROT_WRITE
,
1976 pb
->zbuf2
= mmap(NULL
, pb
->zbufsize
, PROT_READ
| PROT_WRITE
,
1978 if (pb
->zbuf1
== MAP_FAILED
|| pb
->zbuf2
== MAP_FAILED
) {
1979 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1981 status
= PCAP_ERROR
;
1984 memset(&bz
, 0, sizeof(bz
)); /* bzero() deprecated, replaced with memset() */
1985 bz
.bz_bufa
= pb
->zbuf1
;
1986 bz
.bz_bufb
= pb
->zbuf2
;
1987 bz
.bz_buflen
= pb
->zbufsize
;
1988 if (ioctl(fd
, BIOCSETZBUF
, (caddr_t
)&bz
) < 0) {
1989 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1990 errno
, "BIOCSETZBUF");
1991 status
= PCAP_ERROR
;
1994 (void)strncpy(ifrname
, p
->opt
.device
, ifnamsiz
);
1995 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) < 0) {
1996 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1997 errno
, "BIOCSETIF: %s", p
->opt
.device
);
1998 status
= PCAP_ERROR
;
2001 v
= pb
->zbufsize
- sizeof(struct bpf_zbuf_header
);
2006 * We don't have zerocopy BPF.
2007 * Set the buffer size.
2009 if (p
->opt
.buffer_size
!= 0) {
2011 * A buffer size was explicitly specified; use it.
2013 if (ioctl(fd
, BIOCSBLEN
,
2014 (caddr_t
)&p
->opt
.buffer_size
) < 0) {
2015 pcap_fmt_errmsg_for_errno(p
->errbuf
,
2016 PCAP_ERRBUF_SIZE
, errno
,
2017 "BIOCSBLEN: %s", p
->opt
.device
);
2018 status
= PCAP_ERROR
;
2023 * Now bind to the device.
2025 (void)strncpy(ifrname
, p
->opt
.device
, ifnamsiz
);
2027 if (ioctl(fd
, BIOCSETLIF
, (caddr_t
)&ifr
) < 0)
2029 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) < 0)
2032 status
= check_setif_failure(p
, errno
);
2037 * No buffer size was explicitly specified.
2039 * Try finding a good size for the buffer;
2040 * DEFAULT_BUFSIZE may be too big, so keep
2041 * cutting it in half until we find a size
2042 * that works, or run out of sizes to try.
2043 * If the default is larger, don't make it smaller.
2045 if ((ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) ||
2046 v
< DEFAULT_BUFSIZE
)
2047 v
= DEFAULT_BUFSIZE
;
2048 for ( ; v
!= 0; v
>>= 1) {
2050 * Ignore the return value - this is because the
2051 * call fails on BPF systems that don't have
2052 * kernel malloc. And if the call fails, it's
2053 * no big deal, we just continue to use the
2054 * standard buffer size.
2056 (void) ioctl(fd
, BIOCSBLEN
, (caddr_t
)&v
);
2058 (void)strncpy(ifrname
, p
->opt
.device
, ifnamsiz
);
2060 if (ioctl(fd
, BIOCSETLIF
, (caddr_t
)&ifr
) >= 0)
2062 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) >= 0)
2064 break; /* that size worked; we're done */
2066 if (errno
!= ENOBUFS
) {
2067 status
= check_setif_failure(p
, errno
);
2073 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2074 "BIOCSBLEN: %s: No buffer size worked",
2076 status
= PCAP_ERROR
;
2082 /* Get the data link layer type. */
2083 if (ioctl(fd
, BIOCGDLT
, (caddr_t
)&v
) < 0) {
2084 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2086 status
= PCAP_ERROR
;
2092 * AIX's BPF returns IFF_ types, not DLT_ types, in BIOCGDLT.
2115 * We don't know what to map this to yet.
2117 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "unknown interface type %u",
2119 status
= PCAP_ERROR
;
2123 #if _BSDI_VERSION - 0 >= 199510
2124 /* The SLIP and PPP link layer header changed in BSD/OS 2.1 */
2139 case 12: /*DLT_C_HDLC*/
2147 * We know the default link type -- now determine all the DLTs
2148 * this interface supports. If this fails with EINVAL, it's
2149 * not fatal; we just don't get to use the feature later.
2151 if (get_dlt_list(fd
, v
, &bdl
, p
->errbuf
) == -1) {
2152 status
= PCAP_ERROR
;
2155 p
->dlt_count
= bdl
.bfl_len
;
2156 p
->dlt_list
= bdl
.bfl_list
;
2160 * Monitor mode fun, continued.
2162 * For 10.5 and, we're assuming, later releases, as noted above,
2163 * 802.1 adapters that support monitor mode offer both DLT_EN10MB,
2164 * DLT_IEEE802_11, and possibly some 802.11-plus-radio-information
2165 * DLT_ value. Choosing one of the 802.11 DLT_ values will turn
2168 * Therefore, if the user asked for monitor mode, we filter out
2169 * the DLT_EN10MB value, as you can't get that in monitor mode,
2170 * and, if the user didn't ask for monitor mode, we filter out
2171 * the 802.11 DLT_ values, because selecting those will turn
2172 * monitor mode on. Then, for monitor mode, if an 802.11-plus-
2173 * radio DLT_ value is offered, we try to select that, otherwise
2174 * we try to select DLT_IEEE802_11.
2177 if (isdigit((unsigned)osinfo
.release
[0]) &&
2178 (osinfo
.release
[0] == '9' ||
2179 isdigit((unsigned)osinfo
.release
[1]))) {
2181 * 10.5 (Darwin 9.x), or later.
2183 new_dlt
= find_802_11(&bdl
);
2184 if (new_dlt
!= -1) {
2186 * We have at least one 802.11 DLT_ value,
2187 * so this is an 802.11 interface.
2188 * new_dlt is the best of the 802.11
2189 * DLT_ values in the list.
2193 * Our caller wants monitor mode.
2194 * Purge DLT_EN10MB from the list
2195 * of link-layer types, as selecting
2196 * it will keep monitor mode off.
2198 remove_non_802_11(p
);
2201 * If the new mode we want isn't
2202 * the default mode, attempt to
2203 * select the new mode.
2205 if ((u_int
)new_dlt
!= v
) {
2206 if (ioctl(p
->fd
, BIOCSDLT
,
2218 * Our caller doesn't want
2219 * monitor mode. Unless this
2220 * is being done by pcap_open_live(),
2221 * purge the 802.11 link-layer types
2222 * from the list, as selecting
2223 * one of them will turn monitor
2232 * The caller requested monitor
2233 * mode, but we have no 802.11
2234 * link-layer types, so they
2237 status
= PCAP_ERROR_RFMON_NOTSUP
;
2243 #elif defined(HAVE_BSD_IEEE80211)
2245 * *BSD with the new 802.11 ioctls.
2246 * Do we want monitor mode?
2250 * Try to put the interface into monitor mode.
2252 retv
= monitor_mode(p
, 1);
2262 * We're in monitor mode.
2263 * Try to find the best 802.11 DLT_ value and, if we
2264 * succeed, try to switch to that mode if we're not
2265 * already in that mode.
2267 new_dlt
= find_802_11(&bdl
);
2268 if (new_dlt
!= -1) {
2270 * We have at least one 802.11 DLT_ value.
2271 * new_dlt is the best of the 802.11
2272 * DLT_ values in the list.
2274 * If the new mode we want isn't the default mode,
2275 * attempt to select the new mode.
2277 if ((u_int
)new_dlt
!= v
) {
2278 if (ioctl(p
->fd
, BIOCSDLT
, &new_dlt
) != -1) {
2280 * We succeeded; make this the
2288 #endif /* various platforms */
2289 #endif /* BIOCGDLTLIST */
2292 * If this is an Ethernet device, and we don't have a DLT_ list,
2293 * give it a list with DLT_EN10MB and DLT_DOCSIS. (That'd give
2294 * 802.11 interfaces DLT_DOCSIS, which isn't the right thing to
2295 * do, but there's not much we can do about that without finding
2296 * some other way of determining whether it's an Ethernet or 802.11
2299 if (v
== DLT_EN10MB
&& p
->dlt_count
== 0) {
2300 p
->dlt_list
= (u_int
*) malloc(sizeof(u_int
) * 2);
2302 * If that fails, just leave the list empty.
2304 if (p
->dlt_list
!= NULL
) {
2305 p
->dlt_list
[0] = DLT_EN10MB
;
2306 p
->dlt_list
[1] = DLT_DOCSIS
;
2312 p
->fddipad
= PCAP_FDDIPAD
;
2318 #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
2320 * Do a BIOCSHDRCMPLT, if defined, to turn that flag on, so
2321 * the link-layer source address isn't forcibly overwritten.
2322 * (Should we ignore errors? Should we do this only if
2323 * we're open for writing?)
2325 * XXX - I seem to remember some packet-sending bug in some
2326 * BSDs - check CVS log for "bpf.c"?
2328 if (ioctl(fd
, BIOCSHDRCMPLT
, &spoof_eth_src
) == -1) {
2329 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2330 errno
, "BIOCSHDRCMPLT");
2331 status
= PCAP_ERROR
;
2336 #ifdef HAVE_ZEROCOPY_BPF
2338 * In zero-copy mode, we just use the timeout in select().
2339 * XXX - what if we're in non-blocking mode and the *application*
2340 * is using select() or poll() or kqueues or....?
2342 if (p
->opt
.timeout
&& !pb
->zerocopy
) {
2344 if (p
->opt
.timeout
) {
2347 * XXX - is this seconds/nanoseconds in AIX?
2348 * (Treating it as such doesn't fix the timeout
2349 * problem described below.)
2351 * XXX - Mac OS X 10.6 mishandles BIOCSRTIMEOUT in
2352 * 64-bit userland - it takes, as an argument, a
2353 * "struct BPF_TIMEVAL", which has 32-bit tv_sec
2354 * and tv_usec, rather than a "struct timeval".
2356 * If this platform defines "struct BPF_TIMEVAL",
2357 * we check whether the structure size in BIOCSRTIMEOUT
2358 * is that of a "struct timeval" and, if not, we use
2359 * a "struct BPF_TIMEVAL" rather than a "struct timeval".
2360 * (That way, if the bug is fixed in a future release,
2361 * we will still do the right thing.)
2364 #ifdef HAVE_STRUCT_BPF_TIMEVAL
2365 struct BPF_TIMEVAL bpf_to
;
2367 if (IOCPARM_LEN(BIOCSRTIMEOUT
) != sizeof(struct timeval
)) {
2368 bpf_to
.tv_sec
= p
->opt
.timeout
/ 1000;
2369 bpf_to
.tv_usec
= (p
->opt
.timeout
* 1000) % 1000000;
2370 if (ioctl(p
->fd
, BIOCSRTIMEOUT
, (caddr_t
)&bpf_to
) < 0) {
2371 pcap_fmt_errmsg_for_errno(p
->errbuf
,
2372 errno
, PCAP_ERRBUF_SIZE
, "BIOCSRTIMEOUT");
2373 status
= PCAP_ERROR
;
2378 to
.tv_sec
= p
->opt
.timeout
/ 1000;
2379 to
.tv_usec
= (p
->opt
.timeout
* 1000) % 1000000;
2380 if (ioctl(p
->fd
, BIOCSRTIMEOUT
, (caddr_t
)&to
) < 0) {
2381 pcap_fmt_errmsg_for_errno(p
->errbuf
,
2382 errno
, PCAP_ERRBUF_SIZE
, "BIOCSRTIMEOUT");
2383 status
= PCAP_ERROR
;
2386 #ifdef HAVE_STRUCT_BPF_TIMEVAL
2391 #ifdef BIOCIMMEDIATE
2393 * Darren Reed notes that
2395 * On AIX (4.2 at least), if BIOCIMMEDIATE is not set, the
2396 * timeout appears to be ignored and it waits until the buffer
2397 * is filled before returning. The result of not having it
2398 * set is almost worse than useless if your BPF filter
2399 * is reducing things to only a few packets (i.e. one every
2402 * so we always turn BIOCIMMEDIATE mode on if this is AIX.
2404 * For other platforms, we don't turn immediate mode on by default,
2405 * as that would mean we get woken up for every packet, which
2406 * probably isn't what you want for a packet sniffer.
2408 * We set immediate mode if the caller requested it by calling
2409 * pcap_set_immediate() before calling pcap_activate().
2412 if (p
->opt
.immediate
) {
2415 if (ioctl(p
->fd
, BIOCIMMEDIATE
, &v
) < 0) {
2416 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2417 errno
, "BIOCIMMEDIATE");
2418 status
= PCAP_ERROR
;
2424 #else /* BIOCIMMEDIATE */
2425 if (p
->opt
.immediate
) {
2427 * We don't support immediate mode. Fail.
2429 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "Immediate mode not supported");
2430 status
= PCAP_ERROR
;
2433 #endif /* BIOCIMMEDIATE */
2435 if (p
->opt
.promisc
) {
2436 /* set promiscuous mode, just warn if it fails */
2437 if (ioctl(p
->fd
, BIOCPROMISC
, NULL
) < 0) {
2438 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2439 errno
, "BIOCPROMISC");
2440 status
= PCAP_WARNING_PROMISC_NOTSUP
;
2446 if (ioctl(p
->fd
, BIOCSTSTAMP
, &v
) < 0) {
2447 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2448 errno
, "BIOCSTSTAMP");
2449 status
= PCAP_ERROR
;
2452 #endif /* BIOCSTSTAMP */
2454 if (ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) {
2455 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2456 errno
, "BIOCGBLEN");
2457 status
= PCAP_ERROR
;
2461 #ifdef HAVE_ZEROCOPY_BPF
2462 if (!pb
->zerocopy
) {
2464 p
->buffer
= malloc(p
->bufsize
);
2465 if (p
->buffer
== NULL
) {
2466 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2468 status
= PCAP_ERROR
;
2472 /* For some strange reason this seems to prevent the EFAULT
2473 * problems we have experienced from AIX BPF. */
2474 memset(p
->buffer
, 0x0, p
->bufsize
);
2476 #ifdef HAVE_ZEROCOPY_BPF
2481 * If there's no filter program installed, there's
2482 * no indication to the kernel of what the snapshot
2483 * length should be, so no snapshotting is done.
2485 * Therefore, when we open the device, we install
2486 * an "accept everything" filter with the specified
2489 total_insn
.code
= (u_short
)(BPF_RET
| BPF_K
);
2492 total_insn
.k
= p
->snapshot
;
2494 total_prog
.bf_len
= 1;
2495 total_prog
.bf_insns
= &total_insn
;
2496 if (ioctl(p
->fd
, BIOCSETF
, (caddr_t
)&total_prog
) < 0) {
2497 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2499 status
= PCAP_ERROR
;
2504 * On most BPF platforms, either you can do a "select()" or
2505 * "poll()" on a BPF file descriptor and it works correctly,
2506 * or you can do it and it will return "readable" if the
2507 * hold buffer is full but not if the timeout expires *and*
2508 * a non-blocking read will, if the hold buffer is empty
2509 * but the store buffer isn't empty, rotate the buffers
2510 * and return what packets are available.
2512 * In the latter case, the fact that a non-blocking read
2513 * will give you the available packets means you can work
2514 * around the failure of "select()" and "poll()" to wake up
2515 * and return "readable" when the timeout expires by using
2516 * the timeout as the "select()" or "poll()" timeout, putting
2517 * the BPF descriptor into non-blocking mode, and read from
2518 * it regardless of whether "select()" reports it as readable
2521 * However, in FreeBSD 4.3 and 4.4, "select()" and "poll()"
2522 * won't wake up and return "readable" if the timer expires
2523 * and non-blocking reads return EWOULDBLOCK if the hold
2524 * buffer is empty, even if the store buffer is non-empty.
2526 * This means the workaround in question won't work.
2528 * Therefore, on FreeBSD 4.3 and 4.4, we set "p->selectable_fd"
2529 * to -1, which means "sorry, you can't use 'select()' or 'poll()'
2530 * here". On all other BPF platforms, we set it to the FD for
2531 * the BPF device; in NetBSD, OpenBSD, and Darwin, a non-blocking
2532 * read will, if the hold buffer is empty and the store buffer
2533 * isn't empty, rotate the buffers and return what packets are
2534 * there (and in sufficiently recent versions of OpenBSD
2535 * "select()" and "poll()" should work correctly).
2537 * XXX - what about AIX?
2539 p
->selectable_fd
= p
->fd
; /* assume select() works until we know otherwise */
2542 * We can check what OS this is.
2544 if (strcmp(osinfo
.sysname
, "FreeBSD") == 0) {
2545 if (strncmp(osinfo
.release
, "4.3-", 4) == 0 ||
2546 strncmp(osinfo
.release
, "4.4-", 4) == 0)
2547 p
->selectable_fd
= -1;
2551 p
->read_op
= pcap_read_bpf
;
2552 p
->inject_op
= pcap_inject_bpf
;
2553 p
->setfilter_op
= pcap_setfilter_bpf
;
2554 p
->setdirection_op
= pcap_setdirection_bpf
;
2555 p
->set_datalink_op
= pcap_set_datalink_bpf
;
2556 p
->getnonblock_op
= pcap_getnonblock_bpf
;
2557 p
->setnonblock_op
= pcap_setnonblock_bpf
;
2558 p
->stats_op
= pcap_stats_bpf
;
2559 p
->cleanup_op
= pcap_cleanup_bpf
;
2563 pcap_cleanup_bpf(p
);
2568 * Not all interfaces can be bound to by BPF, so try to bind to
2569 * the specified interface; return 0 if we fail with
2570 * PCAP_ERROR_NO_SUCH_DEVICE (which means we got an ENXIO when we tried
2571 * to bind, which means this interface isn't in the list of interfaces
2572 * attached to BPF) and 1 otherwise.
2575 check_bpf_bindable(const char *name
)
2578 char errbuf
[PCAP_ERRBUF_SIZE
];
2581 * On macOS, we don't do this check if the device name begins
2582 * with "wlt"; at least some versions of macOS (actually, it
2583 * was called "Mac OS X" then...) offer monitor mode capturing
2584 * by having a separate "monitor mode" device for each wireless
2585 * adapter, rather than by implementing the ioctls that
2586 * {Free,Net,Open,DragonFly}BSD provide. Opening that device
2587 * puts the adapter into monitor mode, which, at least for
2588 * some adapters, causes them to deassociate from the network
2589 * with which they're associated.
2591 * Instead, we try to open the corresponding "en" device (so
2592 * that we don't end up with, for users without sufficient
2593 * privilege to open capture devices, a list of adapters that
2594 * only includes the wlt devices).
2597 if (strncmp(name
, "wlt", 3) == 0) {
2602 * Try to allocate a buffer for the "en"
2605 en_name_len
= strlen(name
) - 1;
2606 en_name
= malloc(en_name_len
+ 1);
2607 if (en_name
== NULL
) {
2608 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
2612 strcpy(en_name
, "en");
2613 strcat(en_name
, name
+ 3);
2614 fd
= bpf_open_and_bind(en_name
, errbuf
);
2617 #endif /* __APPLE */
2618 fd
= bpf_open_and_bind(name
, errbuf
);
2621 * Error - was it PCAP_ERROR_NO_SUCH_DEVICE?
2623 if (fd
== PCAP_ERROR_NO_SUCH_DEVICE
) {
2625 * Yes, so we can't bind to this because it's
2626 * not something supported by BPF.
2631 * No, so we don't know whether it's supported or not;
2632 * say it is, so that the user can at least try to
2633 * open it and report the error (which is probably
2634 * "you don't have permission to open BPF devices";
2635 * reporting those interfaces means users will ask
2636 * "why am I getting a permissions error when I try
2637 * to capture" rather than "why am I not seeing any
2638 * interfaces", making the underlying problem clearer).
2650 #if defined(__FreeBSD__) && defined(SIOCIFCREATE2)
2652 get_usb_if_flags(const char *name _U_
, bpf_u_int32
*flags _U_
, char *errbuf _U_
)
2655 * XXX - if there's a way to determine whether there's something
2656 * plugged into a given USB bus, use that to determine whether
2657 * this device is "connected" or not.
2663 finddevs_usb(pcap_if_list_t
*devlistp
, char *errbuf
)
2666 struct dirent
*usbitem
;
2671 * We might have USB sniffing support, so try looking for USB
2674 * We want to report a usbusN device for each USB bus, but
2675 * usbusN interfaces might, or might not, exist for them -
2676 * we create one if there isn't already one.
2678 * So, instead, we look in /dev/usb for all buses and create
2679 * a "usbusN" device for each one.
2681 usbdir
= opendir("/dev/usb");
2682 if (usbdir
== NULL
) {
2690 * Leave enough room for a 32-bit (10-digit) bus number.
2691 * Yes, that's overkill, but we won't be using
2692 * the buffer very long.
2694 name_max
= USBUS_PREFIX_LEN
+ 10 + 1;
2695 name
= malloc(name_max
);
2700 while ((usbitem
= readdir(usbdir
)) != NULL
) {
2704 if (strcmp(usbitem
->d_name
, ".") == 0 ||
2705 strcmp(usbitem
->d_name
, "..") == 0) {
2711 p
= strchr(usbitem
->d_name
, '.');
2714 busnumlen
= p
- usbitem
->d_name
;
2715 memcpy(name
, usbus_prefix
, USBUS_PREFIX_LEN
);
2716 memcpy(name
+ USBUS_PREFIX_LEN
, usbitem
->d_name
, busnumlen
);
2717 *(name
+ USBUS_PREFIX_LEN
+ busnumlen
) = '\0';
2719 * There's an entry in this directory for every USB device,
2720 * not for every bus; if there's more than one device on
2721 * the bus, there'll be more than one entry for that bus,
2722 * so we need to avoid adding multiple capture devices
2725 if (find_or_add_dev(devlistp
, name
, PCAP_IF_UP
,
2726 get_usb_if_flags
, NULL
, errbuf
) == NULL
) {
2729 return (PCAP_ERROR
);
2739 * Get additional flags for a device, using SIOCGIFMEDIA.
2743 get_if_flags(const char *name
, bpf_u_int32
*flags
, char *errbuf
)
2746 struct ifmediareq req
;
2748 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
2750 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
, errno
,
2751 "Can't create socket to get media information for %s",
2755 memset(&req
, 0, sizeof(req
));
2756 strncpy(req
.ifm_name
, name
, sizeof(req
.ifm_name
));
2757 if (ioctl(sock
, SIOCGIFMEDIA
, &req
) < 0) {
2758 if (errno
== EOPNOTSUPP
|| errno
== EINVAL
|| errno
== ENOTTY
||
2759 errno
== ENODEV
|| errno
== EPERM
) {
2761 * Not supported, so we can't provide any
2762 * additional information. Assume that
2763 * this means that "connected" vs.
2764 * "disconnected" doesn't apply.
2766 * The ioctl routine for Apple's pktap devices,
2767 * annoyingly, checks for "are you root?" before
2768 * checking whether the ioctl is valid, so it
2769 * returns EPERM, rather than ENOTSUP, for the
2770 * invalid SIOCGIFMEDIA, unless you're root.
2771 * So, just as we do for some ethtool ioctls
2772 * on Linux, which makes the same mistake, we
2773 * also treat EPERM as meaning "not supported".
2775 *flags
|= PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE
;
2779 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
, errno
,
2780 "SIOCGIFMEDIA on %s failed", name
);
2787 * OK, what type of network is this?
2789 switch (IFM_TYPE(req
.ifm_active
)) {
2795 *flags
|= PCAP_IF_WIRELESS
;
2800 * Do we know whether it's connected?
2802 if (req
.ifm_status
& IFM_AVALID
) {
2806 if (req
.ifm_status
& IFM_ACTIVE
) {
2810 *flags
|= PCAP_IF_CONNECTION_STATUS_CONNECTED
;
2813 * It's disconnected.
2815 *flags
|= PCAP_IF_CONNECTION_STATUS_DISCONNECTED
;
2822 get_if_flags(const char *name _U_
, bpf_u_int32
*flags _U_
, char *errbuf _U_
)
2825 * Nothing we can do other than mark loopback devices as "the
2826 * connected/disconnected status doesn't apply".
2828 * XXX - on Solaris, can we do what the dladm command does,
2829 * i.e. get a connected/disconnected indication from a kstat?
2830 * (Note that you can also get the link speed, and possibly
2831 * other information, from a kstat as well.)
2833 if (*flags
& PCAP_IF_LOOPBACK
) {
2835 * Loopback devices aren't wireless, and "connected"/
2836 * "disconnected" doesn't apply to them.
2838 *flags
|= PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE
;
2846 pcap_platform_finddevs(pcap_if_list_t
*devlistp
, char *errbuf
)
2849 * Get the list of regular interfaces first.
2851 if (pcap_findalldevs_interfaces(devlistp
, errbuf
, check_bpf_bindable
,
2852 get_if_flags
) == -1)
2853 return (-1); /* failure */
2855 #if defined(__FreeBSD__) && defined(SIOCIFCREATE2)
2856 if (finddevs_usb(devlistp
, errbuf
) == -1)
2863 #ifdef HAVE_BSD_IEEE80211
2865 monitor_mode(pcap_t
*p
, int set
)
2867 struct pcap_bpf
*pb
= p
->priv
;
2869 struct ifmediareq req
;
2870 IFM_ULIST_TYPE
*media_list
;
2875 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
2877 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2878 errno
, "can't open socket");
2879 return (PCAP_ERROR
);
2882 memset(&req
, 0, sizeof req
);
2883 strncpy(req
.ifm_name
, p
->opt
.device
, sizeof req
.ifm_name
);
2886 * Find out how many media types we have.
2888 if (ioctl(sock
, SIOCGIFMEDIA
, &req
) < 0) {
2890 * Can't get the media types.
2896 * There's no such device.
2899 return (PCAP_ERROR_NO_SUCH_DEVICE
);
2903 * Interface doesn't support SIOC{G,S}IFMEDIA.
2906 return (PCAP_ERROR_RFMON_NOTSUP
);
2909 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2910 errno
, "SIOCGIFMEDIA");
2912 return (PCAP_ERROR
);
2915 if (req
.ifm_count
== 0) {
2920 return (PCAP_ERROR_RFMON_NOTSUP
);
2924 * Allocate a buffer to hold all the media types, and
2925 * get the media types.
2927 media_list
= malloc(req
.ifm_count
* sizeof(*media_list
));
2928 if (media_list
== NULL
) {
2929 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2932 return (PCAP_ERROR
);
2934 req
.ifm_ulist
= media_list
;
2935 if (ioctl(sock
, SIOCGIFMEDIA
, &req
) < 0) {
2936 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2937 errno
, "SIOCGIFMEDIA");
2940 return (PCAP_ERROR
);
2944 * Look for an 802.11 "automatic" media type.
2945 * We assume that all 802.11 adapters have that media type,
2946 * and that it will carry the monitor mode supported flag.
2949 for (i
= 0; i
< req
.ifm_count
; i
++) {
2950 if (IFM_TYPE(media_list
[i
]) == IFM_IEEE80211
2951 && IFM_SUBTYPE(media_list
[i
]) == IFM_AUTO
) {
2952 /* OK, does it do monitor mode? */
2953 if (media_list
[i
] & IFM_IEEE80211_MONITOR
) {
2962 * This adapter doesn't support monitor mode.
2965 return (PCAP_ERROR_RFMON_NOTSUP
);
2970 * Don't just check whether we can enable monitor mode,
2971 * do so, if it's not already enabled.
2973 if ((req
.ifm_current
& IFM_IEEE80211_MONITOR
) == 0) {
2975 * Monitor mode isn't currently on, so turn it on,
2976 * and remember that we should turn it off when the
2981 * If we haven't already done so, arrange to have
2982 * "pcap_close_all()" called when we exit.
2984 if (!pcap_do_addexit(p
)) {
2986 * "atexit()" failed; don't put the interface
2987 * in monitor mode, just give up.
2990 return (PCAP_ERROR
);
2992 memset(&ifr
, 0, sizeof(ifr
));
2993 (void)strncpy(ifr
.ifr_name
, p
->opt
.device
,
2994 sizeof(ifr
.ifr_name
));
2995 ifr
.ifr_media
= req
.ifm_current
| IFM_IEEE80211_MONITOR
;
2996 if (ioctl(sock
, SIOCSIFMEDIA
, &ifr
) == -1) {
2997 pcap_fmt_errmsg_for_errno(p
->errbuf
,
2998 PCAP_ERRBUF_SIZE
, errno
, "SIOCSIFMEDIA");
3000 return (PCAP_ERROR
);
3003 pb
->must_do_on_close
|= MUST_CLEAR_RFMON
;
3006 * Add this to the list of pcaps to close when we exit.
3008 pcap_add_to_pcaps_to_close(p
);
3013 #endif /* HAVE_BSD_IEEE80211 */
3015 #if defined(BIOCGDLTLIST) && (defined(__APPLE__) || defined(HAVE_BSD_IEEE80211))
3017 * Check whether we have any 802.11 link-layer types; return the best
3018 * of the 802.11 link-layer types if we find one, and return -1
3021 * DLT_IEEE802_11_RADIO, with the radiotap header, is considered the
3022 * best 802.11 link-layer type; any of the other 802.11-plus-radio
3023 * headers are second-best; 802.11 with no radio information is
3027 find_802_11(struct bpf_dltlist
*bdlp
)
3033 * Scan the list of DLT_ values, looking for 802.11 values,
3034 * and, if we find any, choose the best of them.
3037 for (i
= 0; i
< bdlp
->bfl_len
; i
++) {
3038 switch (bdlp
->bfl_list
[i
]) {
3040 case DLT_IEEE802_11
:
3042 * 802.11, but no radio.
3044 * Offer this, and select it as the new mode
3045 * unless we've already found an 802.11
3046 * header with radio information.
3049 new_dlt
= bdlp
->bfl_list
[i
];
3052 #ifdef DLT_PRISM_HEADER
3053 case DLT_PRISM_HEADER
:
3055 #ifdef DLT_AIRONET_HEADER
3056 case DLT_AIRONET_HEADER
:
3058 case DLT_IEEE802_11_RADIO_AVS
:
3060 * 802.11 with radio, but not radiotap.
3062 * Offer this, and select it as the new mode
3063 * unless we've already found the radiotap DLT_.
3065 if (new_dlt
!= DLT_IEEE802_11_RADIO
)
3066 new_dlt
= bdlp
->bfl_list
[i
];
3069 case DLT_IEEE802_11_RADIO
:
3071 * 802.11 with radiotap.
3073 * Offer this, and select it as the new mode.
3075 new_dlt
= bdlp
->bfl_list
[i
];
3088 #endif /* defined(BIOCGDLTLIST) && (defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)) */
3090 #if defined(__APPLE__) && defined(BIOCGDLTLIST)
3092 * Remove non-802.11 header types from the list of DLT_ values, as we're in
3093 * monitor mode, and those header types aren't supported in monitor mode.
3096 remove_non_802_11(pcap_t
*p
)
3101 * Scan the list of DLT_ values and discard non-802.11 ones.
3104 for (i
= 0; i
< p
->dlt_count
; i
++) {
3105 switch (p
->dlt_list
[i
]) {
3110 * Not 802.11. Don't offer this one.
3116 * Just copy this mode over.
3122 * Copy this DLT_ value to its new position.
3124 p
->dlt_list
[j
] = p
->dlt_list
[i
];
3129 * Set the DLT_ count to the number of entries we copied.
3135 * Remove 802.11 link-layer types from the list of DLT_ values, as
3136 * we're not in monitor mode, and those DLT_ values will switch us
3140 remove_802_11(pcap_t
*p
)
3145 * Scan the list of DLT_ values and discard 802.11 values.
3148 for (i
= 0; i
< p
->dlt_count
; i
++) {
3149 switch (p
->dlt_list
[i
]) {
3151 case DLT_IEEE802_11
:
3152 #ifdef DLT_PRISM_HEADER
3153 case DLT_PRISM_HEADER
:
3155 #ifdef DLT_AIRONET_HEADER
3156 case DLT_AIRONET_HEADER
:
3158 case DLT_IEEE802_11_RADIO
:
3159 case DLT_IEEE802_11_RADIO_AVS
:
3164 * 802.11. Don't offer this one.
3170 * Just copy this mode over.
3176 * Copy this DLT_ value to its new position.
3178 p
->dlt_list
[j
] = p
->dlt_list
[i
];
3183 * Set the DLT_ count to the number of entries we copied.
3187 #endif /* defined(__APPLE__) && defined(BIOCGDLTLIST) */
3190 pcap_setfilter_bpf(pcap_t
*p
, struct bpf_program
*fp
)
3192 struct pcap_bpf
*pb
= p
->priv
;
3195 * Free any user-mode filter we might happen to have installed.
3197 pcap_freecode(&p
->fcode
);
3200 * Try to install the kernel filter.
3202 if (ioctl(p
->fd
, BIOCSETF
, (caddr_t
)fp
) == 0) {
3206 pb
->filtering_in_kernel
= 1; /* filtering in the kernel */
3209 * Discard any previously-received packets, as they might
3210 * have passed whatever filter was formerly in effect, but
3211 * might not pass this filter (BIOCSETF discards packets
3212 * buffered in the kernel, so you can lose packets in any
3222 * If it failed with EINVAL, that's probably because the program
3223 * is invalid or too big. Validate it ourselves; if we like it
3224 * (we currently allow backward branches, to support protochain),
3225 * run it in userland. (There's no notion of "too big" for
3228 * Otherwise, just give up.
3229 * XXX - if the copy of the program into the kernel failed,
3230 * we will get EINVAL rather than, say, EFAULT on at least
3233 if (errno
!= EINVAL
) {
3234 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
3240 * install_bpf_program() validates the program.
3242 * XXX - what if we already have a filter in the kernel?
3244 if (install_bpf_program(p
, fp
) < 0)
3246 pb
->filtering_in_kernel
= 0; /* filtering in userland */
3251 * Set direction flag: Which packets do we accept on a forwarding
3252 * single device? IN, OUT or both?
3254 #if defined(BIOCSDIRECTION)
3256 pcap_setdirection_bpf(pcap_t
*p
, pcap_direction_t d
)
3260 direction
= (d
== PCAP_D_IN
) ? BPF_D_IN
:
3261 ((d
== PCAP_D_OUT
) ? BPF_D_OUT
: BPF_D_INOUT
);
3262 if (ioctl(p
->fd
, BIOCSDIRECTION
, &direction
) == -1) {
3263 pcap_fmt_errmsg_for_errno(p
->errbuf
, sizeof(p
->errbuf
),
3264 errno
, "Cannot set direction to %s",
3265 (d
== PCAP_D_IN
) ? "PCAP_D_IN" :
3266 ((d
== PCAP_D_OUT
) ? "PCAP_D_OUT" : "PCAP_D_INOUT"));
3271 #elif defined(BIOCSSEESENT)
3273 pcap_setdirection_bpf(pcap_t
*p
, pcap_direction_t d
)
3278 * We don't support PCAP_D_OUT.
3280 if (d
== PCAP_D_OUT
) {
3281 pcap_snprintf(p
->errbuf
, sizeof(p
->errbuf
),
3282 "Setting direction to PCAP_D_OUT is not supported on BPF");
3286 seesent
= (d
== PCAP_D_INOUT
);
3287 if (ioctl(p
->fd
, BIOCSSEESENT
, &seesent
) == -1) {
3288 pcap_fmt_errmsg_for_errno(p
->errbuf
, sizeof(p
->errbuf
),
3289 errno
, "Cannot set direction to %s",
3290 (d
== PCAP_D_INOUT
) ? "PCAP_D_INOUT" : "PCAP_D_IN");
3297 pcap_setdirection_bpf(pcap_t
*p
, pcap_direction_t d _U_
)
3299 (void) pcap_snprintf(p
->errbuf
, sizeof(p
->errbuf
),
3300 "This system doesn't support BIOCSSEESENT, so the direction can't be set");
3307 pcap_set_datalink_bpf(pcap_t
*p
, int dlt
)
3309 if (ioctl(p
->fd
, BIOCSDLT
, &dlt
) == -1) {
3310 pcap_fmt_errmsg_for_errno(p
->errbuf
, sizeof(p
->errbuf
),
3311 errno
, "Cannot set DLT %d", dlt
);
3318 pcap_set_datalink_bpf(pcap_t
*p _U_
, int dlt _U_
)
3325 * Platform-specific information.
3328 pcap_lib_version(void)
3330 #ifdef HAVE_ZEROCOPY_BPF
3331 return (PCAP_VERSION_STRING
" (with zerocopy support)");
3333 return (PCAP_VERSION_STRING
);