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
);
127 # include <net/if_media.h>
130 #include "pcap-int.h"
132 #ifdef HAVE_OS_PROTO_H
133 #include "os-proto.h"
137 * Later versions of NetBSD stick padding in front of FDDI frames
138 * to align the IP header on a 4-byte boundary.
140 #if defined(__NetBSD__) && __NetBSD_Version__ > 106000000
141 #define PCAP_FDDIPAD 3
145 * Private data for capturing on BPF devices.
148 #ifdef HAVE_ZEROCOPY_BPF
150 * Zero-copy read buffer -- for zero-copy BPF. 'buffer' above will
151 * alternative between these two actual mmap'd buffers as required.
152 * As there is a header on the front size of the mmap'd buffer, only
153 * some of the buffer is exposed to libpcap as a whole via bufsize;
154 * zbufsize is the true size. zbuffer tracks the current zbuf
155 * associated with buffer so that it can be used to decide which the
156 * next buffer to read will be.
158 u_char
*zbuf1
, *zbuf2
, *zbuffer
;
162 struct timespec firstsel
;
164 * If there's currently a buffer being actively processed, then it is
165 * referenced here; 'buffer' is also pointed at it, but offset by the
166 * size of the header.
168 struct bpf_zbuf_header
*bzh
;
169 int nonblock
; /* true if in nonblocking mode */
170 #endif /* HAVE_ZEROCOPY_BPF */
172 char *device
; /* device name */
173 int filtering_in_kernel
; /* using kernel filter */
174 int must_do_on_close
; /* stuff we must do when we close */
178 * Stuff to do when we close.
180 #define MUST_CLEAR_RFMON 0x00000001 /* clear rfmon (monitor) mode */
181 #define MUST_DESTROY_USBUS 0x00000002 /* destroy usbusN interface */
184 # if (defined(HAVE_NET_IF_MEDIA_H) && defined(IFM_IEEE80211)) && !defined(__APPLE__)
185 #define HAVE_BSD_IEEE80211
188 * The ifm_ulist member of a struct ifmediareq is an int * on most systems,
189 * but it's a uint64_t on newer versions of OpenBSD.
191 * We check this by checking whether IFM_GMASK is defined and > 2^32-1.
193 # if defined(IFM_GMASK) && IFM_GMASK > 0xFFFFFFFF
194 # define IFM_ULIST_TYPE uint64_t
196 # define IFM_ULIST_TYPE int
200 # if defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)
201 static int find_802_11(struct bpf_dltlist
*);
203 # ifdef HAVE_BSD_IEEE80211
204 static int monitor_mode(pcap_t
*, int);
207 # if defined(__APPLE__)
208 static void remove_non_802_11(pcap_t
*);
209 static void remove_802_11(pcap_t
*);
212 # endif /* defined(__APPLE__) || defined(HAVE_BSD_IEEE80211) */
214 #endif /* BIOCGDLTLIST */
216 #if defined(sun) && defined(LIFNAMSIZ) && defined(lifr_zoneid)
221 * We include the OS's <net/bpf.h>, not our "pcap/bpf.h", so we probably
222 * don't get DLT_DOCSIS defined.
225 #define DLT_DOCSIS 143
229 * In some versions of macOS, we might not even get any of the
230 * 802.11-plus-radio-header DLT_'s defined, even though some
231 * of them are used by various Airport drivers in those versions.
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 pcap_fmt_errmsg_for_errno(p
->errbuf
,
372 PCAP_ERRBUF_SIZE
, errno
, "BIOCROTZBUF");
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 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
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 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
419 errno
, "BIOCROTZBUF");
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
, 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_list
= malloc(2 * sizeof(u_int
));
459 if (p
->tstamp_precision_list
== NULL
) {
460 pcap_fmt_errmsg_for_errno(ebuf
, PCAP_ERRBUF_SIZE
, errno
,
465 p
->tstamp_precision_list
[0] = PCAP_TSTAMP_PRECISION_MICRO
;
466 p
->tstamp_precision_list
[1] = PCAP_TSTAMP_PRECISION_NANO
;
467 p
->tstamp_precision_count
= 2;
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 static const char cloning_device
[] = "/dev/bpf";
482 char device
[sizeof "/dev/bpf0000000000"];
483 static int no_cloning_bpf
= 0;
487 * Load the bpf driver, if it isn't already loaded,
488 * and create the BPF device entries, if they don't
491 if (bpf_load(errbuf
) == PCAP_ERROR
)
496 * First, unless we've already tried opening /dev/bpf and
497 * gotten ENOENT, try opening /dev/bpf.
498 * If it fails with ENOENT, remember that, so we don't try
499 * again, and try /dev/bpfN.
501 if (!no_cloning_bpf
&&
502 (fd
= open(cloning_device
, O_RDWR
)) == -1 &&
503 ((errno
!= EACCES
&& errno
!= ENOENT
) ||
504 (fd
= open(cloning_device
, O_RDONLY
)) == -1)) {
505 if (errno
!= ENOENT
) {
507 fd
= PCAP_ERROR_PERM_DENIED
;
510 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
511 errno
, "(cannot open device) %s", cloning_device
);
517 if (no_cloning_bpf
) {
519 * We don't have /dev/bpf.
520 * Go through all the /dev/bpfN minors and find one
524 (void)snprintf(device
, sizeof(device
), "/dev/bpf%d", n
++);
526 * Initially try a read/write open (to allow the inject
527 * method to work). If that fails due to permission
528 * issues, fall back to read-only. This allows a
529 * non-root user to be granted specific access to pcap
530 * capabilities via file permissions.
532 * XXX - we should have an API that has a flag that
533 * controls whether to open read-only or read-write,
534 * so that denial of permission to send (or inability
535 * to send, if sending packets isn't supported on
536 * the device in question) can be indicated at open
539 fd
= open(device
, O_RDWR
);
540 if (fd
== -1 && errno
== EACCES
)
541 fd
= open(device
, O_RDONLY
);
542 } while (fd
< 0 && errno
== EBUSY
);
546 * XXX better message for all minors used
555 * /dev/bpf0 doesn't exist, which
556 * means we probably have no BPF
559 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
560 "(there are no BPF devices)");
563 * We got EBUSY on at least one
564 * BPF device, so we have BPF
565 * devices, but all the ones
566 * that exist are busy.
568 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
569 "(all BPF devices are busy)");
575 * Got EACCES on the last device we tried,
576 * and EBUSY on all devices before that,
579 fd
= PCAP_ERROR_PERM_DENIED
;
580 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
581 errno
, "(cannot open BPF device) %s", device
);
586 * Some other problem.
589 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
590 errno
, "(cannot open BPF device) %s", device
);
599 * Bind a network adapter to a BPF device, given a descriptor for the
600 * BPF device and the name of the network adapter.
602 * Use BIOCSETLIF if available (meaning "on Solaris"), as it supports
603 * longer device names.
605 * If the name is longer than will fit, return PCAP_ERROR_NO_SUCH_DEVICE
606 * before trying to bind the interface, as there cannot be such a device.
608 * If the attempt succeeds, return BPF_BIND_SUCCEEDED.
610 * If the attempt fails:
612 * if it fails with ENXIO, return PCAP_ERROR_NO_SUCH_DEVICE, as
613 * the device doesn't exist;
615 * if it fails with ENETDOWN, return PCAP_ERROR_IFACE_NOT_UP, as
616 * the interface exists but isn't up and the OS doesn't allow
617 * binding to an interface that isn't up;
619 * if it fails with ENOBUFS, return BPF_BIND_BUFFER_TOO_BIG, and
620 * fill in an error message, as the buffer being requested is too
623 * otherwise, return PCAP_ERROR and fill in an error message.
625 #define BPF_BIND_SUCCEEDED 0
626 #define BPF_BIND_BUFFER_TOO_BIG 1
629 bpf_bind(int fd
, const char *name
, char *errbuf
)
635 if (strlen(name
) >= sizeof(ifr
.lifr_name
)) {
636 /* The name is too long, so it can't possibly exist. */
637 return (PCAP_ERROR_NO_SUCH_DEVICE
);
639 (void)pcap_strlcpy(ifr
.lifr_name
, name
, sizeof(ifr
.lifr_name
));
640 status
= ioctl(fd
, BIOCSETLIF
, (caddr_t
)&ifr
);
644 if (strlen(name
) >= sizeof(ifr
.ifr_name
)) {
645 /* The name is too long, so it can't possibly exist. */
646 return (PCAP_ERROR_NO_SUCH_DEVICE
);
648 (void)pcap_strlcpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
649 status
= ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
);
657 * There's no such device.
659 return (PCAP_ERROR_NO_SUCH_DEVICE
);
663 * Return a "network down" indication, so that
664 * the application can report that rather than
665 * saying we had a mysterious failure and
666 * suggest that they report a problem to the
667 * libpcap developers.
669 return (PCAP_ERROR_IFACE_NOT_UP
);
673 * The buffer size is too big.
674 * Return a special indication so that, if we're
675 * trying to crank the buffer size down, we know
676 * we have to continue; add an error message that
677 * tells the user what needs to be fixed.
679 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
680 errno
, "The requested buffer size for %s is too large",
682 return (BPF_BIND_BUFFER_TOO_BIG
);
685 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
686 errno
, "Binding interface %s to BPF device failed",
691 return (BPF_BIND_SUCCEEDED
);
695 * Open and bind to a device; used if we're not actually going to use
696 * the device, but are just testing whether it can be opened, or opening
697 * it to get information about it.
699 * Returns an error code on failure (always negative), and an FD for
700 * the now-bound BPF device on success (always non-negative).
703 bpf_open_and_bind(const char *name
, char *errbuf
)
709 * First, open a BPF device.
711 fd
= bpf_open(errbuf
);
713 return (fd
); /* fd is the appropriate error code */
716 * Now bind to the device.
718 status
= bpf_bind(fd
, name
, errbuf
);
719 if (status
!= BPF_BIND_SUCCEEDED
) {
721 if (status
== BPF_BIND_BUFFER_TOO_BIG
) {
723 * We didn't specify a buffer size, so
724 * this *really* shouldn't fail because
725 * there's no buffer space. Fail.
740 device_exists(int fd
, const char *name
, char *errbuf
)
745 if (strlen(name
) >= sizeof(ifr
.ifr_name
)) {
746 /* The name is too long, so it can't possibly exist. */
747 return (PCAP_ERROR_NO_SUCH_DEVICE
);
749 (void)pcap_strlcpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
750 status
= ioctl(fd
, SIOCGIFFLAGS
, (caddr_t
)&ifr
);
753 if (errno
== ENXIO
|| errno
== EINVAL
) {
755 * macOS and *BSD return one of those two
756 * errors if the device doesn't exist.
757 * Don't fill in an error, as this is
758 * an "expected" condition.
760 return (PCAP_ERROR_NO_SUCH_DEVICE
);
764 * Some other error - provide a message for it, as
767 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
, errno
,
768 "Can't get interface flags on %s", name
);
781 get_dlt_list(int fd
, int v
, struct bpf_dltlist
*bdlp
, char *ebuf
)
783 memset(bdlp
, 0, sizeof(*bdlp
));
784 if (ioctl(fd
, BIOCGDLTLIST
, (caddr_t
)bdlp
) == 0) {
788 bdlp
->bfl_list
= (u_int
*) malloc(sizeof(u_int
) * (bdlp
->bfl_len
+ 1));
789 if (bdlp
->bfl_list
== NULL
) {
790 pcap_fmt_errmsg_for_errno(ebuf
, PCAP_ERRBUF_SIZE
,
795 if (ioctl(fd
, BIOCGDLTLIST
, (caddr_t
)bdlp
) < 0) {
796 pcap_fmt_errmsg_for_errno(ebuf
, PCAP_ERRBUF_SIZE
,
797 errno
, "BIOCGDLTLIST");
798 free(bdlp
->bfl_list
);
803 * OK, for real Ethernet devices, add DLT_DOCSIS to the
804 * list, so that an application can let you choose it,
805 * in case you're capturing DOCSIS traffic that a Cisco
806 * Cable Modem Termination System is putting out onto
807 * an Ethernet (it doesn't put an Ethernet header onto
808 * the wire, it puts raw DOCSIS frames out on the wire
809 * inside the low-level Ethernet framing).
811 * A "real Ethernet device" is defined here as a device
812 * that has a link-layer type of DLT_EN10MB and that has
813 * no alternate link-layer types; that's done to exclude
814 * 802.11 interfaces (which might or might not be the
815 * right thing to do, but I suspect it is - Ethernet <->
816 * 802.11 bridges would probably badly mishandle frames
817 * that don't have Ethernet headers).
819 * On Solaris with BPF, Ethernet devices also offer
820 * DLT_IPNET, so we, if DLT_IPNET is defined, we don't
821 * treat it as an indication that the device isn't an
824 if (v
== DLT_EN10MB
) {
826 for (i
= 0; i
< bdlp
->bfl_len
; i
++) {
827 if (bdlp
->bfl_list
[i
] != DLT_EN10MB
829 && bdlp
->bfl_list
[i
] != DLT_IPNET
838 * We reserved one more slot at the end of
841 bdlp
->bfl_list
[bdlp
->bfl_len
] = DLT_DOCSIS
;
847 * EINVAL just means "we don't support this ioctl on
848 * this device"; don't treat it as an error.
850 if (errno
!= EINVAL
) {
851 pcap_fmt_errmsg_for_errno(ebuf
, PCAP_ERRBUF_SIZE
,
852 errno
, "BIOCGDLTLIST");
860 #if defined(__APPLE__)
862 pcap_can_set_rfmon_bpf(pcap_t
*p
)
864 struct utsname osinfo
;
867 struct bpf_dltlist bdl
;
872 * The joys of monitor mode on Mac OS X/OS X/macOS.
874 * Prior to 10.4, it's not supported at all.
876 * In 10.4, if adapter enN supports monitor mode, there's a
877 * wltN adapter corresponding to it; you open it, instead of
878 * enN, to get monitor mode. You get whatever link-layer
879 * headers it supplies.
881 * In 10.5, and, we assume, later releases, if adapter enN
882 * supports monitor mode, it offers, among its selectable
883 * DLT_ values, values that let you get the 802.11 header;
884 * selecting one of those values puts the adapter into monitor
885 * mode (i.e., you can't get 802.11 headers except in monitor
886 * mode, and you can't get Ethernet headers in monitor mode).
888 if (uname(&osinfo
) == -1) {
890 * Can't get the OS version; just say "no".
895 * We assume osinfo.sysname is "Darwin", because
896 * __APPLE__ is defined. We just check the version.
898 if (osinfo
.release
[0] < '8' && osinfo
.release
[1] == '.') {
900 * 10.3 (Darwin 7.x) or earlier.
901 * Monitor mode not supported.
905 if (osinfo
.release
[0] == '8' && osinfo
.release
[1] == '.') {
910 * 10.4 (Darwin 8.x). s/en/wlt/, and check
911 * whether the device exists.
913 if (strncmp(p
->opt
.device
, "en", 2) != 0) {
915 * Not an enN device; no monitor mode.
919 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
921 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
925 if (pcap_asprintf(&wlt_name
, "wlt%s", p
->opt
.device
+ 2) == -1) {
926 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
931 status
= device_exists(fd
, wlt_name
, p
->errbuf
);
935 if (status
== PCAP_ERROR_NO_SUCH_DEVICE
)
948 * Everything else is 10.5 or later; for those,
949 * we just open the enN device, and check whether
950 * we have any 802.11 devices.
952 * First, open a BPF device.
954 fd
= bpf_open(p
->errbuf
);
956 return (fd
); /* fd is the appropriate error code */
959 * Now bind to the device.
961 err
= bpf_bind(fd
, p
->opt
.device
, p
->errbuf
);
962 if (err
!= BPF_BIND_SUCCEEDED
) {
964 if (err
== BPF_BIND_BUFFER_TOO_BIG
) {
966 * We didn't specify a buffer size, so
967 * this *really* shouldn't fail because
968 * there's no buffer space. Fail.
976 * We know the default link type -- now determine all the DLTs
977 * this interface supports. If this fails with EINVAL, it's
978 * not fatal; we just don't get to use the feature later.
979 * (We don't care about DLT_DOCSIS, so we pass DLT_NULL
980 * as the default DLT for this adapter.)
982 if (get_dlt_list(fd
, DLT_NULL
, &bdl
, p
->errbuf
) == PCAP_ERROR
) {
986 if (find_802_11(&bdl
) != -1) {
988 * We have an 802.11 DLT, so we can set monitor mode.
996 #endif /* BIOCGDLTLIST */
999 #elif defined(HAVE_BSD_IEEE80211)
1001 pcap_can_set_rfmon_bpf(pcap_t
*p
)
1005 ret
= monitor_mode(p
, 0);
1006 if (ret
== PCAP_ERROR_RFMON_NOTSUP
)
1007 return (0); /* not an error, just a "can't do" */
1009 return (1); /* success */
1014 pcap_can_set_rfmon_bpf(pcap_t
*p _U_
)
1021 pcap_stats_bpf(pcap_t
*p
, struct pcap_stat
*ps
)
1026 * "ps_recv" counts packets handed to the filter, not packets
1027 * that passed the filter. This includes packets later dropped
1028 * because we ran out of buffer space.
1030 * "ps_drop" counts packets dropped inside the BPF device
1031 * because we ran out of buffer space. It doesn't count
1032 * packets dropped by the interface driver. It counts
1033 * only packets that passed the filter.
1035 * Both statistics include packets not yet read from the kernel
1036 * by libpcap, and thus not yet seen by the application.
1038 if (ioctl(p
->fd
, BIOCGSTATS
, (caddr_t
)&s
) < 0) {
1039 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1040 errno
, "BIOCGSTATS");
1041 return (PCAP_ERROR
);
1044 ps
->ps_recv
= s
.bs_recv
;
1045 ps
->ps_drop
= s
.bs_drop
;
1051 pcap_read_bpf(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
1053 struct pcap_bpf
*pb
= p
->priv
;
1056 register u_char
*bp
, *ep
;
1061 #ifdef HAVE_ZEROCOPY_BPF
1067 * Has "pcap_breakloop()" been called?
1069 if (p
->break_loop
) {
1071 * Yes - clear the flag that indicates that it
1072 * has, and return PCAP_ERROR_BREAK to indicate
1073 * that we were told to break out of the loop.
1076 return (PCAP_ERROR_BREAK
);
1081 * When reading without zero-copy from a file descriptor, we
1082 * use a single buffer and return a length of data in the
1083 * buffer. With zero-copy, we update the p->buffer pointer
1084 * to point at whatever underlying buffer contains the next
1085 * data and update cc to reflect the data found in the
1088 #ifdef HAVE_ZEROCOPY_BPF
1090 if (p
->buffer
!= NULL
)
1092 i
= pcap_next_zbuf(p
, &cc
);
1096 return (PCAP_ERROR
);
1100 cc
= (int)read(p
->fd
, p
->buffer
, p
->bufsize
);
1103 /* Don't choke when we get ptraced */
1112 * Sigh. More AIX wonderfulness.
1114 * For some unknown reason the uiomove()
1115 * operation in the bpf kernel extension
1116 * used to copy the buffer into user
1117 * space sometimes returns EFAULT. I have
1118 * no idea why this is the case given that
1119 * a kernel debugger shows the user buffer
1120 * is correct. This problem appears to
1121 * be mostly mitigated by the memset of
1122 * the buffer before it is first used.
1123 * Very strange.... Shaun Clowes
1125 * In any case this means that we shouldn't
1126 * treat EFAULT as a fatal error; as we
1127 * don't have an API for returning
1128 * a "some packets were dropped since
1129 * the last packet you saw" indication,
1130 * we just ignore EFAULT and keep reading.
1138 case ENXIO
: /* FreeBSD, DragonFly BSD, and Darwin */
1139 case EIO
: /* OpenBSD */
1140 /* NetBSD appears not to return an error in this case */
1142 * The device on which we're capturing
1145 * XXX - we should really return
1146 * an appropriate error for that,
1147 * but pcap_dispatch() etc. aren't
1148 * documented as having error returns
1149 * other than PCAP_ERROR or PCAP_ERROR_BREAK.
1151 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1152 "The interface disappeared");
1153 return (PCAP_ERROR
);
1155 #if defined(sun) && !defined(BSD) && !defined(__svr4__) && !defined(__SVR4)
1157 * Due to a SunOS bug, after 2^31 bytes, the kernel
1158 * file offset overflows and read fails with EINVAL.
1159 * The lseek() to 0 will fix things.
1162 if (lseek(p
->fd
, 0L, SEEK_CUR
) +
1164 (void)lseek(p
->fd
, 0L, SEEK_SET
);
1170 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1172 return (PCAP_ERROR
);
1174 bp
= (u_char
*)p
->buffer
;
1179 * Loop through each packet.
1181 * This assumes that a single buffer of packets will have
1182 * <= INT_MAX packets, so the packet count doesn't overflow.
1185 #define bhp ((struct bpf_xhdr *)bp)
1187 #define bhp ((struct bpf_hdr *)bp)
1194 register u_int caplen
, hdrlen
;
1197 * Has "pcap_breakloop()" been called?
1198 * If so, return immediately - if we haven't read any
1199 * packets, clear the flag and return PCAP_ERROR_BREAK
1200 * to indicate that we were told to break out of the loop,
1201 * otherwise leave the flag set, so that the *next* call
1202 * will break out of the loop without having read any
1203 * packets, and return the number of packets we've
1206 if (p
->break_loop
) {
1208 p
->cc
= (int)(ep
- bp
);
1210 * ep is set based on the return value of read(),
1211 * but read() from a BPF device doesn't necessarily
1212 * return a value that's a multiple of the alignment
1213 * value for BPF_WORDALIGN(). However, whenever we
1214 * increment bp, we round up the increment value by
1215 * a value rounded up by BPF_WORDALIGN(), so we
1216 * could increment bp past ep after processing the
1217 * last packet in the buffer.
1219 * We treat ep < bp as an indication that this
1220 * happened, and just set p->cc to 0.
1226 return (PCAP_ERROR_BREAK
);
1231 caplen
= bhp
->bh_caplen
;
1232 hdrlen
= bhp
->bh_hdrlen
;
1233 datap
= bp
+ hdrlen
;
1235 * Short-circuit evaluation: if using BPF filter
1236 * in kernel, no need to do it now - we already know
1237 * the packet passed the filter.
1240 * Note: the filter code was generated assuming
1241 * that p->fddipad was the amount of padding
1242 * before the header, as that's what's required
1243 * in the kernel, so we run the filter before
1244 * skipping that padding.
1247 if (pb
->filtering_in_kernel
||
1248 pcap_filter(p
->fcode
.bf_insns
, datap
, bhp
->bh_datalen
, caplen
)) {
1249 struct pcap_pkthdr pkthdr
;
1253 bt
.sec
= bhp
->bh_tstamp
.bt_sec
;
1254 bt
.frac
= bhp
->bh_tstamp
.bt_frac
;
1255 if (p
->opt
.tstamp_precision
== PCAP_TSTAMP_PRECISION_NANO
) {
1258 bintime2timespec(&bt
, &ts
);
1259 pkthdr
.ts
.tv_sec
= ts
.tv_sec
;
1260 pkthdr
.ts
.tv_usec
= ts
.tv_nsec
;
1264 bintime2timeval(&bt
, &tv
);
1265 pkthdr
.ts
.tv_sec
= tv
.tv_sec
;
1266 pkthdr
.ts
.tv_usec
= tv
.tv_usec
;
1269 pkthdr
.ts
.tv_sec
= bhp
->bh_tstamp
.tv_sec
;
1272 * AIX's BPF returns seconds/nanoseconds time
1273 * stamps, not seconds/microseconds time stamps.
1275 pkthdr
.ts
.tv_usec
= bhp
->bh_tstamp
.tv_usec
/1000;
1277 pkthdr
.ts
.tv_usec
= bhp
->bh_tstamp
.tv_usec
;
1279 #endif /* BIOCSTSTAMP */
1282 pkthdr
.caplen
= caplen
- pad
;
1285 if (bhp
->bh_datalen
> pad
)
1286 pkthdr
.len
= bhp
->bh_datalen
- pad
;
1291 pkthdr
.caplen
= caplen
;
1292 pkthdr
.len
= bhp
->bh_datalen
;
1294 (*callback
)(user
, &pkthdr
, datap
);
1295 bp
+= BPF_WORDALIGN(caplen
+ hdrlen
);
1296 if (++n
>= cnt
&& !PACKET_COUNT_IS_UNLIMITED(cnt
)) {
1298 p
->cc
= (int)(ep
- bp
);
1300 * See comment above about p->cc < 0.
1310 bp
+= BPF_WORDALIGN(caplen
+ hdrlen
);
1319 pcap_inject_bpf(pcap_t
*p
, const void *buf
, int size
)
1323 ret
= (int)write(p
->fd
, buf
, size
);
1325 if (ret
== -1 && errno
== EAFNOSUPPORT
) {
1327 * In some versions of macOS, there's a bug wherein setting
1328 * the BIOCSHDRCMPLT flag causes writes to fail; see, for
1331 * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/BIOCSHDRCMPLT-10.3.3.patch
1333 * So, if, on macOS, we get EAFNOSUPPORT from the write, we
1334 * assume it's due to that bug, and turn off that flag
1335 * and try again. If we succeed, it either means that
1336 * somebody applied the fix from that URL, or other patches
1339 * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/
1341 * and are running a Darwin kernel with those fixes, or
1342 * that Apple fixed the problem in some macOS release.
1344 u_int spoof_eth_src
= 0;
1346 if (ioctl(p
->fd
, BIOCSHDRCMPLT
, &spoof_eth_src
) == -1) {
1347 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1348 errno
, "send: can't turn off BIOCSHDRCMPLT");
1349 return (PCAP_ERROR
);
1353 * Now try the write again.
1355 ret
= (int)write(p
->fd
, buf
, size
);
1357 #endif /* __APPLE__ */
1359 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1361 return (PCAP_ERROR
);
1368 bpf_odminit(char *errbuf
)
1372 if (odm_initialize() == -1) {
1373 if (odm_err_msg(odmerrno
, &errstr
) == -1)
1374 errstr
= "Unknown error";
1375 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1376 "bpf_load: odm_initialize failed: %s",
1378 return (PCAP_ERROR
);
1381 if ((odmlockid
= odm_lock("/etc/objrepos/config_lock", ODM_WAIT
)) == -1) {
1382 if (odm_err_msg(odmerrno
, &errstr
) == -1)
1383 errstr
= "Unknown error";
1384 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1385 "bpf_load: odm_lock of /etc/objrepos/config_lock failed: %s",
1387 (void)odm_terminate();
1388 return (PCAP_ERROR
);
1395 bpf_odmcleanup(char *errbuf
)
1399 if (odm_unlock(odmlockid
) == -1) {
1400 if (errbuf
!= NULL
) {
1401 if (odm_err_msg(odmerrno
, &errstr
) == -1)
1402 errstr
= "Unknown error";
1403 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1404 "bpf_load: odm_unlock failed: %s",
1407 return (PCAP_ERROR
);
1410 if (odm_terminate() == -1) {
1411 if (errbuf
!= NULL
) {
1412 if (odm_err_msg(odmerrno
, &errstr
) == -1)
1413 errstr
= "Unknown error";
1414 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1415 "bpf_load: odm_terminate failed: %s",
1418 return (PCAP_ERROR
);
1425 bpf_load(char *errbuf
)
1429 int numminors
, i
, rc
;
1432 struct bpf_config cfg_bpf
;
1433 struct cfg_load cfg_ld
;
1434 struct cfg_kmod cfg_km
;
1437 * This is very very close to what happens in the real implementation
1438 * but I've fixed some (unlikely) bug situations.
1443 if (bpf_odminit(errbuf
) == PCAP_ERROR
)
1444 return (PCAP_ERROR
);
1446 major
= genmajor(BPF_NAME
);
1448 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
1449 errno
, "bpf_load: genmajor failed");
1450 (void)bpf_odmcleanup(NULL
);
1451 return (PCAP_ERROR
);
1454 minors
= getminor(major
, &numminors
, BPF_NAME
);
1456 minors
= genminor("bpf", major
, 0, BPF_MINORS
, 1, 1);
1458 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
1459 errno
, "bpf_load: genminor failed");
1460 (void)bpf_odmcleanup(NULL
);
1461 return (PCAP_ERROR
);
1465 if (bpf_odmcleanup(errbuf
) == PCAP_ERROR
)
1466 return (PCAP_ERROR
);
1468 rc
= stat(BPF_NODE
"0", &sbuf
);
1469 if (rc
== -1 && errno
!= ENOENT
) {
1470 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
1471 errno
, "bpf_load: can't stat %s", BPF_NODE
"0");
1472 return (PCAP_ERROR
);
1475 if (rc
== -1 || getmajor(sbuf
.st_rdev
) != major
) {
1476 for (i
= 0; i
< BPF_MINORS
; i
++) {
1477 snprintf(buf
, sizeof(buf
), "%s%d", BPF_NODE
, i
);
1479 if (mknod(buf
, S_IRUSR
| S_IFCHR
, domakedev(major
, i
)) == -1) {
1480 pcap_fmt_errmsg_for_errno(errbuf
,
1481 PCAP_ERRBUF_SIZE
, errno
,
1482 "bpf_load: can't mknod %s", buf
);
1483 return (PCAP_ERROR
);
1488 /* Check if the driver is loaded */
1489 memset(&cfg_ld
, 0x0, sizeof(cfg_ld
));
1490 snprintf(buf
, sizeof(buf
), "%s/%s", DRIVER_PATH
, BPF_NAME
);
1492 if ((sysconfig(SYS_QUERYLOAD
, (void *)&cfg_ld
, sizeof(cfg_ld
)) == -1) ||
1493 (cfg_ld
.kmid
== 0)) {
1494 /* Driver isn't loaded, load it now */
1495 if (sysconfig(SYS_SINGLELOAD
, (void *)&cfg_ld
, sizeof(cfg_ld
)) == -1) {
1496 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
1497 errno
, "bpf_load: could not load driver");
1498 return (PCAP_ERROR
);
1502 /* Configure the driver */
1503 cfg_km
.cmd
= CFG_INIT
;
1504 cfg_km
.kmid
= cfg_ld
.kmid
;
1505 cfg_km
.mdilen
= sizeof(cfg_bpf
);
1506 cfg_km
.mdiptr
= (void *)&cfg_bpf
;
1507 for (i
= 0; i
< BPF_MINORS
; i
++) {
1508 cfg_bpf
.devno
= domakedev(major
, i
);
1509 if (sysconfig(SYS_CFGKMOD
, (void *)&cfg_km
, sizeof(cfg_km
)) == -1) {
1510 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
1511 errno
, "bpf_load: could not configure driver");
1512 return (PCAP_ERROR
);
1523 * Undo any operations done when opening the device when necessary.
1526 pcap_cleanup_bpf(pcap_t
*p
)
1528 struct pcap_bpf
*pb
= p
->priv
;
1529 #ifdef HAVE_BSD_IEEE80211
1531 struct ifmediareq req
;
1535 if (pb
->must_do_on_close
!= 0) {
1537 * There's something we have to do when closing this
1540 #ifdef HAVE_BSD_IEEE80211
1541 if (pb
->must_do_on_close
& MUST_CLEAR_RFMON
) {
1543 * We put the interface into rfmon mode;
1544 * take it out of rfmon mode.
1546 * XXX - if somebody else wants it in rfmon
1547 * mode, this code cannot know that, so it'll take
1548 * it out of rfmon mode.
1550 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
1553 "Can't restore interface flags (socket() failed: %s).\n"
1554 "Please adjust manually.\n",
1557 memset(&req
, 0, sizeof(req
));
1558 pcap_strlcpy(req
.ifm_name
, pb
->device
,
1559 sizeof(req
.ifm_name
));
1560 if (ioctl(sock
, SIOCGIFMEDIA
, &req
) < 0) {
1562 "Can't restore interface flags (SIOCGIFMEDIA failed: %s).\n"
1563 "Please adjust manually.\n",
1566 if (req
.ifm_current
& IFM_IEEE80211_MONITOR
) {
1568 * Rfmon mode is currently on;
1571 memset(&ifr
, 0, sizeof(ifr
));
1572 (void)pcap_strlcpy(ifr
.ifr_name
,
1574 sizeof(ifr
.ifr_name
));
1576 req
.ifm_current
& ~IFM_IEEE80211_MONITOR
;
1577 if (ioctl(sock
, SIOCSIFMEDIA
,
1580 "Can't restore interface flags (SIOCSIFMEDIA failed: %s).\n"
1581 "Please adjust manually.\n",
1589 #endif /* HAVE_BSD_IEEE80211 */
1591 #if defined(__FreeBSD__) && defined(SIOCIFCREATE2)
1593 * Attempt to destroy the usbusN interface that we created.
1595 if (pb
->must_do_on_close
& MUST_DESTROY_USBUS
) {
1596 if (if_nametoindex(pb
->device
) > 0) {
1599 s
= socket(AF_LOCAL
, SOCK_DGRAM
, 0);
1601 pcap_strlcpy(ifr
.ifr_name
, pb
->device
,
1602 sizeof(ifr
.ifr_name
));
1603 ioctl(s
, SIOCIFDESTROY
, &ifr
);
1608 #endif /* defined(__FreeBSD__) && defined(SIOCIFCREATE2) */
1610 * Take this pcap out of the list of pcaps for which we
1611 * have to take the interface out of some mode.
1613 pcap_remove_from_pcaps_to_close(p
);
1614 pb
->must_do_on_close
= 0;
1617 #ifdef HAVE_ZEROCOPY_BPF
1620 * Delete the mappings. Note that p->buffer gets
1621 * initialized to one of the mmapped regions in
1622 * this case, so do not try and free it directly;
1623 * null it out so that pcap_cleanup_live_common()
1624 * doesn't try to free it.
1626 if (pb
->zbuf1
!= MAP_FAILED
&& pb
->zbuf1
!= NULL
)
1627 (void) munmap(pb
->zbuf1
, pb
->zbufsize
);
1628 if (pb
->zbuf2
!= MAP_FAILED
&& pb
->zbuf2
!= NULL
)
1629 (void) munmap(pb
->zbuf2
, pb
->zbufsize
);
1633 if (pb
->device
!= NULL
) {
1637 pcap_cleanup_live_common(p
);
1642 check_setif_failure(pcap_t
*p
, int error
)
1647 if (error
== PCAP_ERROR_NO_SUCH_DEVICE
) {
1649 * No such device exists.
1651 if (p
->opt
.rfmon
&& strncmp(p
->opt
.device
, "wlt", 3) == 0) {
1653 * Monitor mode was requested, and we're trying
1654 * to open a "wltN" device. Assume that this
1655 * is 10.4 and that we were asked to open an
1656 * "enN" device; if that device exists, return
1657 * "monitor mode not supported on the device".
1659 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
1663 if (pcap_asprintf(&en_name
, "en%s",
1664 p
->opt
.device
+ 3) == -1) {
1666 * We can't find out whether there's
1667 * an underlying "enN" device, so
1668 * just report "no such device".
1670 pcap_fmt_errmsg_for_errno(p
->errbuf
,
1671 PCAP_ERRBUF_SIZE
, errno
,
1674 return (PCAP_ERROR_NO_SUCH_DEVICE
);
1676 err
= device_exists(fd
, en_name
, p
->errbuf
);
1679 if (err
== PCAP_ERROR_NO_SUCH_DEVICE
) {
1681 * The underlying "enN" device
1682 * exists, but there's no
1683 * corresponding "wltN" device;
1684 * that means that the "enN"
1685 * device doesn't support
1686 * monitor mode, probably
1687 * because it's an Ethernet
1688 * device rather than a
1691 err
= PCAP_ERROR_RFMON_NOTSUP
;
1697 * We can't find out whether there's
1698 * an underlying "enN" device, so
1699 * just report "no such device".
1701 err
= PCAP_ERROR_NO_SUCH_DEVICE
;
1702 pcap_fmt_errmsg_for_errno(p
->errbuf
,
1703 errno
, PCAP_ERRBUF_SIZE
,
1712 return (PCAP_ERROR_NO_SUCH_DEVICE
);
1716 * Just return the error status; it's what we want, and, if it's
1717 * PCAP_ERROR, the error string has been filled in.
1723 check_setif_failure(pcap_t
*p _U_
, int error
)
1726 * Just return the error status; it's what we want, and, if it's
1727 * PCAP_ERROR, the error string has been filled in.
1734 * Default capture buffer size.
1735 * 32K isn't very much for modern machines with fast networks; we
1736 * pick .5M, as that's the maximum on at least some systems with BPF.
1738 * However, on AIX 3.5, the larger buffer sized caused unrecoverable
1739 * read failures under stress, so we leave it as 32K; yet another
1740 * place where AIX's BPF is broken.
1743 #define DEFAULT_BUFSIZE 32768
1745 #define DEFAULT_BUFSIZE 524288
1749 pcap_activate_bpf(pcap_t
*p
)
1751 struct pcap_bpf
*pb
= p
->priv
;
1753 #ifdef HAVE_BSD_IEEE80211
1757 #if defined(LIFNAMSIZ) && defined(ZONENAME_MAX) && defined(lifr_zoneid)
1761 struct bpf_version bv
;
1764 char *wltdev
= NULL
;
1767 struct bpf_dltlist bdl
;
1768 #if defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)
1771 #endif /* BIOCGDLTLIST */
1772 #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
1773 u_int spoof_eth_src
= 1;
1776 struct bpf_insn total_insn
;
1777 struct bpf_program total_prog
;
1778 struct utsname osinfo
;
1779 int have_osinfo
= 0;
1780 #ifdef HAVE_ZEROCOPY_BPF
1782 u_int bufmode
, zbufmax
;
1785 fd
= bpf_open(p
->errbuf
);
1793 if (ioctl(fd
, BIOCVERSION
, (caddr_t
)&bv
) < 0) {
1794 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1795 errno
, "BIOCVERSION");
1796 status
= PCAP_ERROR
;
1799 if (bv
.bv_major
!= BPF_MAJOR_VERSION
||
1800 bv
.bv_minor
< BPF_MINOR_VERSION
) {
1801 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1802 "kernel bpf filter out of date");
1803 status
= PCAP_ERROR
;
1808 * Turn a negative snapshot value (invalid), a snapshot value of
1809 * 0 (unspecified), or a value bigger than the normal maximum
1810 * value, into the maximum allowed value.
1812 * If some application really *needs* a bigger snapshot
1813 * length, we should just increase MAXIMUM_SNAPLEN.
1815 if (p
->snapshot
<= 0 || p
->snapshot
> MAXIMUM_SNAPLEN
)
1816 p
->snapshot
= MAXIMUM_SNAPLEN
;
1818 #if defined(LIFNAMSIZ) && defined(ZONENAME_MAX) && defined(lifr_zoneid)
1820 * Retrieve the zoneid of the zone we are currently executing in.
1822 if ((ifr
.lifr_zoneid
= getzoneid()) == -1) {
1823 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1824 errno
, "getzoneid()");
1825 status
= PCAP_ERROR
;
1829 * Check if the given source datalink name has a '/' separated
1830 * zonename prefix string. The zonename prefixed source datalink can
1831 * be used by pcap consumers in the Solaris global zone to capture
1832 * traffic on datalinks in non-global zones. Non-global zones
1833 * do not have access to datalinks outside of their own namespace.
1835 if ((zonesep
= strchr(p
->opt
.device
, '/')) != NULL
) {
1836 char path_zname
[ZONENAME_MAX
];
1840 if (ifr
.lifr_zoneid
!= GLOBAL_ZONEID
) {
1841 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1842 "zonename/linkname only valid in global zone.");
1843 status
= PCAP_ERROR
;
1846 znamelen
= zonesep
- p
->opt
.device
;
1847 (void) pcap_strlcpy(path_zname
, p
->opt
.device
, znamelen
+ 1);
1848 ifr
.lifr_zoneid
= getzoneidbyname(path_zname
);
1849 if (ifr
.lifr_zoneid
== -1) {
1850 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1851 errno
, "getzoneidbyname(%s)", path_zname
);
1852 status
= PCAP_ERROR
;
1855 lnamep
= strdup(zonesep
+ 1);
1856 if (lnamep
== NULL
) {
1857 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1859 status
= PCAP_ERROR
;
1862 free(p
->opt
.device
);
1863 p
->opt
.device
= lnamep
;
1867 pb
->device
= strdup(p
->opt
.device
);
1868 if (pb
->device
== NULL
) {
1869 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1871 status
= PCAP_ERROR
;
1876 * Attempt to find out the version of the OS on which we're running.
1878 if (uname(&osinfo
) == 0)
1883 * See comment in pcap_can_set_rfmon_bpf() for an explanation
1884 * of why we check the version number.
1889 * We assume osinfo.sysname is "Darwin", because
1890 * __APPLE__ is defined. We just check the version.
1892 if (osinfo
.release
[0] < '8' &&
1893 osinfo
.release
[1] == '.') {
1895 * 10.3 (Darwin 7.x) or earlier.
1897 status
= PCAP_ERROR_RFMON_NOTSUP
;
1900 if (osinfo
.release
[0] == '8' &&
1901 osinfo
.release
[1] == '.') {
1903 * 10.4 (Darwin 8.x). s/en/wlt/
1905 if (strncmp(p
->opt
.device
, "en", 2) != 0) {
1907 * Not an enN device; check
1908 * whether the device even exists.
1910 sockfd
= socket(AF_INET
, SOCK_DGRAM
, 0);
1912 status
= device_exists(sockfd
,
1913 p
->opt
.device
, p
->errbuf
);
1916 * The device exists,
1923 status
= PCAP_ERROR_RFMON_NOTSUP
;
1928 * We can't find out whether
1929 * the device exists, so just
1930 * report "no such device".
1932 status
= PCAP_ERROR_NO_SUCH_DEVICE
;
1933 pcap_fmt_errmsg_for_errno(p
->errbuf
,
1934 PCAP_ERRBUF_SIZE
, errno
,
1939 wltdev
= malloc(strlen(p
->opt
.device
) + 2);
1940 if (wltdev
== NULL
) {
1941 pcap_fmt_errmsg_for_errno(p
->errbuf
,
1942 PCAP_ERRBUF_SIZE
, errno
,
1944 status
= PCAP_ERROR
;
1947 strcpy(wltdev
, "wlt");
1948 strcat(wltdev
, p
->opt
.device
+ 2);
1949 free(p
->opt
.device
);
1950 p
->opt
.device
= wltdev
;
1953 * Everything else is 10.5 or later; for those,
1954 * we just open the enN device, and set the DLT.
1958 #endif /* __APPLE__ */
1961 * If this is FreeBSD, and the device name begins with "usbus",
1962 * try to create the interface if it's not available.
1964 #if defined(__FreeBSD__) && defined(SIOCIFCREATE2)
1965 if (strncmp(p
->opt
.device
, usbus_prefix
, USBUS_PREFIX_LEN
) == 0) {
1967 * Do we already have an interface with that name?
1969 if (if_nametoindex(p
->opt
.device
) == 0) {
1971 * No. We need to create it, and, if we
1972 * succeed, remember that we should destroy
1973 * it when the pcap_t is closed.
1979 * Open a socket to use for ioctls to
1980 * create the interface.
1982 s
= socket(AF_LOCAL
, SOCK_DGRAM
, 0);
1984 pcap_fmt_errmsg_for_errno(p
->errbuf
,
1985 PCAP_ERRBUF_SIZE
, errno
,
1986 "Can't open socket");
1987 status
= PCAP_ERROR
;
1992 * If we haven't already done so, arrange to have
1993 * "pcap_close_all()" called when we exit.
1995 if (!pcap_do_addexit(p
)) {
1997 * "atexit()" failed; don't create the
1998 * interface, just give up.
2000 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2003 status
= PCAP_ERROR
;
2008 * Create the interface.
2010 pcap_strlcpy(ifr
.ifr_name
, p
->opt
.device
, sizeof(ifr
.ifr_name
));
2011 if (ioctl(s
, SIOCIFCREATE2
, &ifr
) < 0) {
2012 if (errno
== EINVAL
) {
2013 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2014 "Invalid USB bus interface %s",
2017 pcap_fmt_errmsg_for_errno(p
->errbuf
,
2018 PCAP_ERRBUF_SIZE
, errno
,
2019 "Can't create interface for %s",
2023 status
= PCAP_ERROR
;
2028 * Make sure we clean this up when we close.
2030 pb
->must_do_on_close
|= MUST_DESTROY_USBUS
;
2033 * Add this to the list of pcaps to close when we exit.
2035 pcap_add_to_pcaps_to_close(p
);
2038 #endif /* defined(__FreeBSD__) && defined(SIOCIFCREATE2) */
2040 #ifdef HAVE_ZEROCOPY_BPF
2042 * If the BPF extension to set buffer mode is present, try setting
2043 * the mode to zero-copy. If that fails, use regular buffering. If
2044 * it succeeds but other setup fails, return an error to the user.
2046 bufmode
= BPF_BUFMODE_ZBUF
;
2047 if (ioctl(fd
, BIOCSETBUFMODE
, (caddr_t
)&bufmode
) == 0) {
2049 * We have zerocopy BPF; use it.
2054 * How to pick a buffer size: first, query the maximum buffer
2055 * size supported by zero-copy. This also lets us quickly
2056 * determine whether the kernel generally supports zero-copy.
2057 * Then, if a buffer size was specified, use that, otherwise
2058 * query the default buffer size, which reflects kernel
2059 * policy for a desired default. Round to the nearest page
2062 if (ioctl(fd
, BIOCGETZMAX
, (caddr_t
)&zbufmax
) < 0) {
2063 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2064 errno
, "BIOCGETZMAX");
2065 status
= PCAP_ERROR
;
2069 if (p
->opt
.buffer_size
!= 0) {
2071 * A buffer size was explicitly specified; use it.
2073 v
= p
->opt
.buffer_size
;
2075 if ((ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) ||
2076 v
< DEFAULT_BUFSIZE
)
2077 v
= DEFAULT_BUFSIZE
;
2080 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) /* to any y */
2082 pb
->zbufsize
= roundup(v
, getpagesize());
2083 if (pb
->zbufsize
> zbufmax
)
2084 pb
->zbufsize
= zbufmax
;
2085 pb
->zbuf1
= mmap(NULL
, pb
->zbufsize
, PROT_READ
| PROT_WRITE
,
2087 pb
->zbuf2
= mmap(NULL
, pb
->zbufsize
, PROT_READ
| PROT_WRITE
,
2089 if (pb
->zbuf1
== MAP_FAILED
|| pb
->zbuf2
== MAP_FAILED
) {
2090 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2092 status
= PCAP_ERROR
;
2095 memset(&bz
, 0, sizeof(bz
)); /* bzero() deprecated, replaced with memset() */
2096 bz
.bz_bufa
= pb
->zbuf1
;
2097 bz
.bz_bufb
= pb
->zbuf2
;
2098 bz
.bz_buflen
= pb
->zbufsize
;
2099 if (ioctl(fd
, BIOCSETZBUF
, (caddr_t
)&bz
) < 0) {
2100 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2101 errno
, "BIOCSETZBUF");
2102 status
= PCAP_ERROR
;
2105 status
= bpf_bind(fd
, p
->opt
.device
, ifnamsiz
, p
->errbuf
);
2106 if (status
!= BPF_BIND_SUCCEEDED
) {
2107 if (status
== BPF_BIND_BUFFER_TOO_BIG
) {
2109 * The requested buffer size
2112 * XXX - should we do the "keep cutting
2113 * the buffer size in half" loop here if
2114 * we're using the default buffer size?
2116 status
= PCAP_ERROR
;
2120 v
= pb
->zbufsize
- sizeof(struct bpf_zbuf_header
);
2125 * We don't have zerocopy BPF.
2126 * Set the buffer size.
2128 if (p
->opt
.buffer_size
!= 0) {
2130 * A buffer size was explicitly specified; use it.
2132 if (ioctl(fd
, BIOCSBLEN
,
2133 (caddr_t
)&p
->opt
.buffer_size
) < 0) {
2134 pcap_fmt_errmsg_for_errno(p
->errbuf
,
2135 PCAP_ERRBUF_SIZE
, errno
,
2136 "BIOCSBLEN: %s", p
->opt
.device
);
2137 status
= PCAP_ERROR
;
2142 * Now bind to the device.
2144 status
= bpf_bind(fd
, p
->opt
.device
, p
->errbuf
);
2145 if (status
!= BPF_BIND_SUCCEEDED
) {
2146 if (status
== BPF_BIND_BUFFER_TOO_BIG
) {
2148 * The requested buffer size
2151 status
= PCAP_ERROR
;
2156 * Special checks on macOS to deal with
2157 * the way monitor mode was done on
2160 status
= check_setif_failure(p
, status
);
2165 * No buffer size was explicitly specified.
2167 * Try finding a good size for the buffer;
2168 * DEFAULT_BUFSIZE may be too big, so keep
2169 * cutting it in half until we find a size
2170 * that works, or run out of sizes to try.
2171 * If the default is larger, don't make it smaller.
2173 if ((ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) ||
2174 v
< DEFAULT_BUFSIZE
)
2175 v
= DEFAULT_BUFSIZE
;
2176 for ( ; v
!= 0; v
>>= 1) {
2178 * Ignore the return value - this is because the
2179 * call fails on BPF systems that don't have
2180 * kernel malloc. And if the call fails, it's
2181 * no big deal, we just continue to use the
2182 * standard buffer size.
2184 (void) ioctl(fd
, BIOCSBLEN
, (caddr_t
)&v
);
2186 status
= bpf_bind(fd
, p
->opt
.device
, p
->errbuf
);
2187 if (status
== BPF_BIND_SUCCEEDED
)
2188 break; /* that size worked; we're done */
2191 * If the attempt failed because the
2192 * buffer was too big, cut the buffer
2193 * size in half and try again.
2197 if (errno
!= BPF_BIND_BUFFER_TOO_BIG
) {
2199 * Special checks on macOS to deal
2200 * with the way monitor mode was
2201 * done on 10.4 Tiger.
2203 status
= check_setif_failure(p
, status
);
2209 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2210 "BIOCSBLEN: %s: No buffer size worked",
2212 status
= PCAP_ERROR
;
2218 /* Get the data link layer type. */
2219 if (ioctl(fd
, BIOCGDLT
, (caddr_t
)&v
) < 0) {
2220 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2222 status
= PCAP_ERROR
;
2228 * AIX's BPF returns IFF_ types, not DLT_ types, in BIOCGDLT.
2251 * We don't know what to map this to yet.
2253 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "unknown interface type %u",
2255 status
= PCAP_ERROR
;
2259 #if _BSDI_VERSION - 0 >= 199510
2260 /* The SLIP and PPP link layer header changed in BSD/OS 2.1 */
2275 case 12: /*DLT_C_HDLC*/
2283 * We know the default link type -- now determine all the DLTs
2284 * this interface supports. If this fails with EINVAL, it's
2285 * not fatal; we just don't get to use the feature later.
2287 if (get_dlt_list(fd
, v
, &bdl
, p
->errbuf
) == -1) {
2288 status
= PCAP_ERROR
;
2291 p
->dlt_count
= bdl
.bfl_len
;
2292 p
->dlt_list
= bdl
.bfl_list
;
2296 * Monitor mode fun, continued.
2298 * For 10.5 and, we're assuming, later releases, as noted above,
2299 * 802.1 adapters that support monitor mode offer both DLT_EN10MB,
2300 * DLT_IEEE802_11, and possibly some 802.11-plus-radio-information
2301 * DLT_ value. Choosing one of the 802.11 DLT_ values will turn
2304 * Therefore, if the user asked for monitor mode, we filter out
2305 * the DLT_EN10MB value, as you can't get that in monitor mode,
2306 * and, if the user didn't ask for monitor mode, we filter out
2307 * the 802.11 DLT_ values, because selecting those will turn
2308 * monitor mode on. Then, for monitor mode, if an 802.11-plus-
2309 * radio DLT_ value is offered, we try to select that, otherwise
2310 * we try to select DLT_IEEE802_11.
2313 if (PCAP_ISDIGIT((unsigned)osinfo
.release
[0]) &&
2314 (osinfo
.release
[0] == '9' ||
2315 PCAP_ISDIGIT((unsigned)osinfo
.release
[1]))) {
2317 * 10.5 (Darwin 9.x), or later.
2319 new_dlt
= find_802_11(&bdl
);
2320 if (new_dlt
!= -1) {
2322 * We have at least one 802.11 DLT_ value,
2323 * so this is an 802.11 interface.
2324 * new_dlt is the best of the 802.11
2325 * DLT_ values in the list.
2329 * Our caller wants monitor mode.
2330 * Purge DLT_EN10MB from the list
2331 * of link-layer types, as selecting
2332 * it will keep monitor mode off.
2334 remove_non_802_11(p
);
2337 * If the new mode we want isn't
2338 * the default mode, attempt to
2339 * select the new mode.
2341 if ((u_int
)new_dlt
!= v
) {
2342 if (ioctl(p
->fd
, BIOCSDLT
,
2354 * Our caller doesn't want
2355 * monitor mode. Unless this
2356 * is being done by pcap_open_live(),
2357 * purge the 802.11 link-layer types
2358 * from the list, as selecting
2359 * one of them will turn monitor
2368 * The caller requested monitor
2369 * mode, but we have no 802.11
2370 * link-layer types, so they
2373 status
= PCAP_ERROR_RFMON_NOTSUP
;
2379 #elif defined(HAVE_BSD_IEEE80211)
2381 * *BSD with the new 802.11 ioctls.
2382 * Do we want monitor mode?
2386 * Try to put the interface into monitor mode.
2388 retv
= monitor_mode(p
, 1);
2398 * We're in monitor mode.
2399 * Try to find the best 802.11 DLT_ value and, if we
2400 * succeed, try to switch to that mode if we're not
2401 * already in that mode.
2403 new_dlt
= find_802_11(&bdl
);
2404 if (new_dlt
!= -1) {
2406 * We have at least one 802.11 DLT_ value.
2407 * new_dlt is the best of the 802.11
2408 * DLT_ values in the list.
2410 * If the new mode we want isn't the default mode,
2411 * attempt to select the new mode.
2413 if ((u_int
)new_dlt
!= v
) {
2414 if (ioctl(p
->fd
, BIOCSDLT
, &new_dlt
) != -1) {
2416 * We succeeded; make this the
2424 #endif /* various platforms */
2425 #endif /* BIOCGDLTLIST */
2428 * If this is an Ethernet device, and we don't have a DLT_ list,
2429 * give it a list with DLT_EN10MB and DLT_DOCSIS. (That'd give
2430 * 802.11 interfaces DLT_DOCSIS, which isn't the right thing to
2431 * do, but there's not much we can do about that without finding
2432 * some other way of determining whether it's an Ethernet or 802.11
2435 if (v
== DLT_EN10MB
&& p
->dlt_count
== 0) {
2436 p
->dlt_list
= (u_int
*) malloc(sizeof(u_int
) * 2);
2438 * If that fails, just leave the list empty.
2440 if (p
->dlt_list
!= NULL
) {
2441 p
->dlt_list
[0] = DLT_EN10MB
;
2442 p
->dlt_list
[1] = DLT_DOCSIS
;
2448 p
->fddipad
= PCAP_FDDIPAD
;
2454 #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
2456 * Do a BIOCSHDRCMPLT, if defined, to turn that flag on, so
2457 * the link-layer source address isn't forcibly overwritten.
2458 * (Should we ignore errors? Should we do this only if
2459 * we're open for writing?)
2461 * XXX - I seem to remember some packet-sending bug in some
2462 * BSDs - check CVS log for "bpf.c"?
2464 if (ioctl(fd
, BIOCSHDRCMPLT
, &spoof_eth_src
) == -1) {
2465 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2466 errno
, "BIOCSHDRCMPLT");
2467 status
= PCAP_ERROR
;
2472 #ifdef HAVE_ZEROCOPY_BPF
2474 * In zero-copy mode, we just use the timeout in select().
2475 * XXX - what if we're in non-blocking mode and the *application*
2476 * is using select() or poll() or kqueues or....?
2478 if (p
->opt
.timeout
&& !pb
->zerocopy
) {
2480 if (p
->opt
.timeout
) {
2483 * XXX - is this seconds/nanoseconds in AIX?
2484 * (Treating it as such doesn't fix the timeout
2485 * problem described below.)
2487 * XXX - Mac OS X 10.6 mishandles BIOCSRTIMEOUT in
2488 * 64-bit userland - it takes, as an argument, a
2489 * "struct BPF_TIMEVAL", which has 32-bit tv_sec
2490 * and tv_usec, rather than a "struct timeval".
2492 * If this platform defines "struct BPF_TIMEVAL",
2493 * we check whether the structure size in BIOCSRTIMEOUT
2494 * is that of a "struct timeval" and, if not, we use
2495 * a "struct BPF_TIMEVAL" rather than a "struct timeval".
2496 * (That way, if the bug is fixed in a future release,
2497 * we will still do the right thing.)
2500 #ifdef HAVE_STRUCT_BPF_TIMEVAL
2501 struct BPF_TIMEVAL bpf_to
;
2503 if (IOCPARM_LEN(BIOCSRTIMEOUT
) != sizeof(struct timeval
)) {
2504 bpf_to
.tv_sec
= p
->opt
.timeout
/ 1000;
2505 bpf_to
.tv_usec
= (p
->opt
.timeout
* 1000) % 1000000;
2506 if (ioctl(p
->fd
, BIOCSRTIMEOUT
, (caddr_t
)&bpf_to
) < 0) {
2507 pcap_fmt_errmsg_for_errno(p
->errbuf
,
2508 errno
, PCAP_ERRBUF_SIZE
, "BIOCSRTIMEOUT");
2509 status
= PCAP_ERROR
;
2514 to
.tv_sec
= p
->opt
.timeout
/ 1000;
2515 to
.tv_usec
= (p
->opt
.timeout
* 1000) % 1000000;
2516 if (ioctl(p
->fd
, BIOCSRTIMEOUT
, (caddr_t
)&to
) < 0) {
2517 pcap_fmt_errmsg_for_errno(p
->errbuf
,
2518 errno
, PCAP_ERRBUF_SIZE
, "BIOCSRTIMEOUT");
2519 status
= PCAP_ERROR
;
2522 #ifdef HAVE_STRUCT_BPF_TIMEVAL
2527 #ifdef BIOCIMMEDIATE
2529 * Darren Reed notes that
2531 * On AIX (4.2 at least), if BIOCIMMEDIATE is not set, the
2532 * timeout appears to be ignored and it waits until the buffer
2533 * is filled before returning. The result of not having it
2534 * set is almost worse than useless if your BPF filter
2535 * is reducing things to only a few packets (i.e. one every
2538 * so we always turn BIOCIMMEDIATE mode on if this is AIX.
2540 * For other platforms, we don't turn immediate mode on by default,
2541 * as that would mean we get woken up for every packet, which
2542 * probably isn't what you want for a packet sniffer.
2544 * We set immediate mode if the caller requested it by calling
2545 * pcap_set_immediate() before calling pcap_activate().
2548 if (p
->opt
.immediate
) {
2551 if (ioctl(p
->fd
, BIOCIMMEDIATE
, &v
) < 0) {
2552 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2553 errno
, "BIOCIMMEDIATE");
2554 status
= PCAP_ERROR
;
2560 #else /* BIOCIMMEDIATE */
2561 if (p
->opt
.immediate
) {
2563 * We don't support immediate mode. Fail.
2565 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "Immediate mode not supported");
2566 status
= PCAP_ERROR
;
2569 #endif /* BIOCIMMEDIATE */
2571 if (p
->opt
.promisc
) {
2572 /* set promiscuous mode, just warn if it fails */
2573 if (ioctl(p
->fd
, BIOCPROMISC
, NULL
) < 0) {
2574 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2575 errno
, "BIOCPROMISC");
2576 status
= PCAP_WARNING_PROMISC_NOTSUP
;
2582 if (ioctl(p
->fd
, BIOCSTSTAMP
, &v
) < 0) {
2583 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2584 errno
, "BIOCSTSTAMP");
2585 status
= PCAP_ERROR
;
2588 #endif /* BIOCSTSTAMP */
2590 if (ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) {
2591 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2592 errno
, "BIOCGBLEN");
2593 status
= PCAP_ERROR
;
2597 #ifdef HAVE_ZEROCOPY_BPF
2598 if (!pb
->zerocopy
) {
2600 p
->buffer
= malloc(p
->bufsize
);
2601 if (p
->buffer
== NULL
) {
2602 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2604 status
= PCAP_ERROR
;
2608 /* For some strange reason this seems to prevent the EFAULT
2609 * problems we have experienced from AIX BPF. */
2610 memset(p
->buffer
, 0x0, p
->bufsize
);
2612 #ifdef HAVE_ZEROCOPY_BPF
2617 * If there's no filter program installed, there's
2618 * no indication to the kernel of what the snapshot
2619 * length should be, so no snapshotting is done.
2621 * Therefore, when we open the device, we install
2622 * an "accept everything" filter with the specified
2625 total_insn
.code
= (u_short
)(BPF_RET
| BPF_K
);
2628 total_insn
.k
= p
->snapshot
;
2630 total_prog
.bf_len
= 1;
2631 total_prog
.bf_insns
= &total_insn
;
2632 if (ioctl(p
->fd
, BIOCSETF
, (caddr_t
)&total_prog
) < 0) {
2633 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2635 status
= PCAP_ERROR
;
2640 * On most BPF platforms, either you can do a "select()" or
2641 * "poll()" on a BPF file descriptor and it works correctly,
2642 * or you can do it and it will return "readable" if the
2643 * hold buffer is full but not if the timeout expires *and*
2644 * a non-blocking read will, if the hold buffer is empty
2645 * but the store buffer isn't empty, rotate the buffers
2646 * and return what packets are available.
2648 * In the latter case, the fact that a non-blocking read
2649 * will give you the available packets means you can work
2650 * around the failure of "select()" and "poll()" to wake up
2651 * and return "readable" when the timeout expires by using
2652 * the timeout as the "select()" or "poll()" timeout, putting
2653 * the BPF descriptor into non-blocking mode, and read from
2654 * it regardless of whether "select()" reports it as readable
2657 * However, in FreeBSD 4.3 and 4.4, "select()" and "poll()"
2658 * won't wake up and return "readable" if the timer expires
2659 * and non-blocking reads return EWOULDBLOCK if the hold
2660 * buffer is empty, even if the store buffer is non-empty.
2662 * This means the workaround in question won't work.
2664 * Therefore, on FreeBSD 4.3 and 4.4, we set "p->selectable_fd"
2665 * to -1, which means "sorry, you can't use 'select()' or 'poll()'
2666 * here". On all other BPF platforms, we set it to the FD for
2667 * the BPF device; in NetBSD, OpenBSD, and Darwin, a non-blocking
2668 * read will, if the hold buffer is empty and the store buffer
2669 * isn't empty, rotate the buffers and return what packets are
2670 * there (and in sufficiently recent versions of OpenBSD
2671 * "select()" and "poll()" should work correctly).
2673 * XXX - what about AIX?
2675 p
->selectable_fd
= p
->fd
; /* assume select() works until we know otherwise */
2678 * We can check what OS this is.
2680 if (strcmp(osinfo
.sysname
, "FreeBSD") == 0) {
2681 if (strncmp(osinfo
.release
, "4.3-", 4) == 0 ||
2682 strncmp(osinfo
.release
, "4.4-", 4) == 0)
2683 p
->selectable_fd
= -1;
2687 p
->read_op
= pcap_read_bpf
;
2688 p
->inject_op
= pcap_inject_bpf
;
2689 p
->setfilter_op
= pcap_setfilter_bpf
;
2690 p
->setdirection_op
= pcap_setdirection_bpf
;
2691 p
->set_datalink_op
= pcap_set_datalink_bpf
;
2692 p
->getnonblock_op
= pcap_getnonblock_bpf
;
2693 p
->setnonblock_op
= pcap_setnonblock_bpf
;
2694 p
->stats_op
= pcap_stats_bpf
;
2695 p
->cleanup_op
= pcap_cleanup_bpf
;
2699 pcap_cleanup_bpf(p
);
2704 * Not all interfaces can be bound to by BPF, so try to bind to
2705 * the specified interface; return 0 if we fail with
2706 * PCAP_ERROR_NO_SUCH_DEVICE (which means we got an ENXIO when we tried
2707 * to bind, which means this interface isn't in the list of interfaces
2708 * attached to BPF) and 1 otherwise.
2711 check_bpf_bindable(const char *name
)
2714 char errbuf
[PCAP_ERRBUF_SIZE
];
2717 * On macOS, we don't do this check if the device name begins
2718 * with "wlt"; at least some versions of macOS (actually, it
2719 * was called "Mac OS X" then...) offer monitor mode capturing
2720 * by having a separate "monitor mode" device for each wireless
2721 * adapter, rather than by implementing the ioctls that
2722 * {Free,Net,Open,DragonFly}BSD provide. Opening that device
2723 * puts the adapter into monitor mode, which, at least for
2724 * some adapters, causes them to deassociate from the network
2725 * with which they're associated.
2727 * Instead, we try to open the corresponding "en" device (so
2728 * that we don't end up with, for users without sufficient
2729 * privilege to open capture devices, a list of adapters that
2730 * only includes the wlt devices).
2733 if (strncmp(name
, "wlt", 3) == 0) {
2738 * Try to allocate a buffer for the "en"
2741 en_name_len
= strlen(name
) - 1;
2742 en_name
= malloc(en_name_len
+ 1);
2743 if (en_name
== NULL
) {
2744 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
2748 strcpy(en_name
, "en");
2749 strcat(en_name
, name
+ 3);
2750 fd
= bpf_open_and_bind(en_name
, errbuf
);
2753 #endif /* __APPLE */
2754 fd
= bpf_open_and_bind(name
, errbuf
);
2757 * Error - was it PCAP_ERROR_NO_SUCH_DEVICE?
2759 if (fd
== PCAP_ERROR_NO_SUCH_DEVICE
) {
2761 * Yes, so we can't bind to this because it's
2762 * not something supported by BPF.
2767 * No, so we don't know whether it's supported or not;
2768 * say it is, so that the user can at least try to
2769 * open it and report the error (which is probably
2770 * "you don't have permission to open BPF devices";
2771 * reporting those interfaces means users will ask
2772 * "why am I getting a permissions error when I try
2773 * to capture" rather than "why am I not seeing any
2774 * interfaces", making the underlying problem clearer).
2786 #if defined(__FreeBSD__) && defined(SIOCIFCREATE2)
2788 get_usb_if_flags(const char *name _U_
, bpf_u_int32
*flags _U_
, char *errbuf _U_
)
2791 * XXX - if there's a way to determine whether there's something
2792 * plugged into a given USB bus, use that to determine whether
2793 * this device is "connected" or not.
2799 finddevs_usb(pcap_if_list_t
*devlistp
, char *errbuf
)
2802 struct dirent
*usbitem
;
2807 * We might have USB sniffing support, so try looking for USB
2810 * We want to report a usbusN device for each USB bus, but
2811 * usbusN interfaces might, or might not, exist for them -
2812 * we create one if there isn't already one.
2814 * So, instead, we look in /dev/usb for all buses and create
2815 * a "usbusN" device for each one.
2817 usbdir
= opendir("/dev/usb");
2818 if (usbdir
== NULL
) {
2826 * Leave enough room for a 32-bit (10-digit) bus number.
2827 * Yes, that's overkill, but we won't be using
2828 * the buffer very long.
2830 name_max
= USBUS_PREFIX_LEN
+ 10 + 1;
2831 name
= malloc(name_max
);
2836 while ((usbitem
= readdir(usbdir
)) != NULL
) {
2840 if (strcmp(usbitem
->d_name
, ".") == 0 ||
2841 strcmp(usbitem
->d_name
, "..") == 0) {
2847 p
= strchr(usbitem
->d_name
, '.');
2850 busnumlen
= p
- usbitem
->d_name
;
2851 memcpy(name
, usbus_prefix
, USBUS_PREFIX_LEN
);
2852 memcpy(name
+ USBUS_PREFIX_LEN
, usbitem
->d_name
, busnumlen
);
2853 *(name
+ USBUS_PREFIX_LEN
+ busnumlen
) = '\0';
2855 * There's an entry in this directory for every USB device,
2856 * not for every bus; if there's more than one device on
2857 * the bus, there'll be more than one entry for that bus,
2858 * so we need to avoid adding multiple capture devices
2861 if (find_or_add_dev(devlistp
, name
, PCAP_IF_UP
,
2862 get_usb_if_flags
, NULL
, errbuf
) == NULL
) {
2865 return (PCAP_ERROR
);
2875 * Get additional flags for a device, using SIOCGIFMEDIA.
2879 get_if_flags(const char *name
, bpf_u_int32
*flags
, char *errbuf
)
2882 struct ifmediareq req
;
2884 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
2886 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
, errno
,
2887 "Can't create socket to get media information for %s",
2891 memset(&req
, 0, sizeof(req
));
2892 pcap_strlcpy(req
.ifm_name
, name
, sizeof(req
.ifm_name
));
2893 if (ioctl(sock
, SIOCGIFMEDIA
, &req
) < 0) {
2894 if (errno
== EOPNOTSUPP
|| errno
== EINVAL
|| errno
== ENOTTY
||
2895 errno
== ENODEV
|| errno
== EPERM
2901 * Not supported, so we can't provide any
2902 * additional information. Assume that
2903 * this means that "connected" vs.
2904 * "disconnected" doesn't apply.
2906 * The ioctl routine for Apple's pktap devices,
2907 * annoyingly, checks for "are you root?" before
2908 * checking whether the ioctl is valid, so it
2909 * returns EPERM, rather than ENOTSUP, for the
2910 * invalid SIOCGIFMEDIA, unless you're root.
2911 * So, just as we do for some ethtool ioctls
2912 * on Linux, which makes the same mistake, we
2913 * also treat EPERM as meaning "not supported".
2915 * And it appears that Apple's llw0 device, which
2916 * appears to be part of the Skywalk subsystem:
2918 * http://newosxbook.com/bonus/vol1ch16.html
2920 * can sometimes return EPWROFF ("Device power
2921 * is off") for that ioctl, so we treat *that*
2922 * as another indication that we can't get a
2923 * connection status. (If it *isn't* "powered
2924 * off", it's reported as a wireless device,
2925 * complete with an active/inactive state.)
2927 *flags
|= PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE
;
2931 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
, errno
,
2932 "SIOCGIFMEDIA on %s failed", name
);
2939 * OK, what type of network is this?
2941 switch (IFM_TYPE(req
.ifm_active
)) {
2947 *flags
|= PCAP_IF_WIRELESS
;
2952 * Do we know whether it's connected?
2954 if (req
.ifm_status
& IFM_AVALID
) {
2958 if (req
.ifm_status
& IFM_ACTIVE
) {
2962 *flags
|= PCAP_IF_CONNECTION_STATUS_CONNECTED
;
2965 * It's disconnected.
2967 *flags
|= PCAP_IF_CONNECTION_STATUS_DISCONNECTED
;
2974 get_if_flags(const char *name _U_
, bpf_u_int32
*flags
, char *errbuf _U_
)
2977 * Nothing we can do other than mark loopback devices as "the
2978 * connected/disconnected status doesn't apply".
2980 * XXX - on Solaris, can we do what the dladm command does,
2981 * i.e. get a connected/disconnected indication from a kstat?
2982 * (Note that you can also get the link speed, and possibly
2983 * other information, from a kstat as well.)
2985 if (*flags
& PCAP_IF_LOOPBACK
) {
2987 * Loopback devices aren't wireless, and "connected"/
2988 * "disconnected" doesn't apply to them.
2990 *flags
|= PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE
;
2998 pcap_platform_finddevs(pcap_if_list_t
*devlistp
, char *errbuf
)
3001 * Get the list of regular interfaces first.
3003 if (pcap_findalldevs_interfaces(devlistp
, errbuf
, check_bpf_bindable
,
3004 get_if_flags
) == -1)
3005 return (-1); /* failure */
3007 #if defined(__FreeBSD__) && defined(SIOCIFCREATE2)
3008 if (finddevs_usb(devlistp
, errbuf
) == -1)
3015 #ifdef HAVE_BSD_IEEE80211
3017 monitor_mode(pcap_t
*p
, int set
)
3019 struct pcap_bpf
*pb
= p
->priv
;
3021 struct ifmediareq req
;
3022 IFM_ULIST_TYPE
*media_list
;
3027 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
3029 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
3030 errno
, "can't open socket");
3031 return (PCAP_ERROR
);
3034 memset(&req
, 0, sizeof req
);
3035 pcap_strlcpy(req
.ifm_name
, p
->opt
.device
, sizeof req
.ifm_name
);
3038 * Find out how many media types we have.
3040 if (ioctl(sock
, SIOCGIFMEDIA
, &req
) < 0) {
3042 * Can't get the media types.
3048 * There's no such device.
3051 return (PCAP_ERROR_NO_SUCH_DEVICE
);
3055 * Interface doesn't support SIOC{G,S}IFMEDIA.
3058 return (PCAP_ERROR_RFMON_NOTSUP
);
3061 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
3062 errno
, "SIOCGIFMEDIA");
3064 return (PCAP_ERROR
);
3067 if (req
.ifm_count
== 0) {
3072 return (PCAP_ERROR_RFMON_NOTSUP
);
3076 * Allocate a buffer to hold all the media types, and
3077 * get the media types.
3079 media_list
= malloc(req
.ifm_count
* sizeof(*media_list
));
3080 if (media_list
== NULL
) {
3081 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
3084 return (PCAP_ERROR
);
3086 req
.ifm_ulist
= media_list
;
3087 if (ioctl(sock
, SIOCGIFMEDIA
, &req
) < 0) {
3088 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
3089 errno
, "SIOCGIFMEDIA");
3092 return (PCAP_ERROR
);
3096 * Look for an 802.11 "automatic" media type.
3097 * We assume that all 802.11 adapters have that media type,
3098 * and that it will carry the monitor mode supported flag.
3101 for (i
= 0; i
< req
.ifm_count
; i
++) {
3102 if (IFM_TYPE(media_list
[i
]) == IFM_IEEE80211
3103 && IFM_SUBTYPE(media_list
[i
]) == IFM_AUTO
) {
3104 /* OK, does it do monitor mode? */
3105 if (media_list
[i
] & IFM_IEEE80211_MONITOR
) {
3114 * This adapter doesn't support monitor mode.
3117 return (PCAP_ERROR_RFMON_NOTSUP
);
3122 * Don't just check whether we can enable monitor mode,
3123 * do so, if it's not already enabled.
3125 if ((req
.ifm_current
& IFM_IEEE80211_MONITOR
) == 0) {
3127 * Monitor mode isn't currently on, so turn it on,
3128 * and remember that we should turn it off when the
3133 * If we haven't already done so, arrange to have
3134 * "pcap_close_all()" called when we exit.
3136 if (!pcap_do_addexit(p
)) {
3138 * "atexit()" failed; don't put the interface
3139 * in monitor mode, just give up.
3142 return (PCAP_ERROR
);
3144 memset(&ifr
, 0, sizeof(ifr
));
3145 (void)pcap_strlcpy(ifr
.ifr_name
, p
->opt
.device
,
3146 sizeof(ifr
.ifr_name
));
3147 ifr
.ifr_media
= req
.ifm_current
| IFM_IEEE80211_MONITOR
;
3148 if (ioctl(sock
, SIOCSIFMEDIA
, &ifr
) == -1) {
3149 pcap_fmt_errmsg_for_errno(p
->errbuf
,
3150 PCAP_ERRBUF_SIZE
, errno
, "SIOCSIFMEDIA");
3152 return (PCAP_ERROR
);
3155 pb
->must_do_on_close
|= MUST_CLEAR_RFMON
;
3158 * Add this to the list of pcaps to close when we exit.
3160 pcap_add_to_pcaps_to_close(p
);
3165 #endif /* HAVE_BSD_IEEE80211 */
3167 #if defined(BIOCGDLTLIST) && (defined(__APPLE__) || defined(HAVE_BSD_IEEE80211))
3169 * Check whether we have any 802.11 link-layer types; return the best
3170 * of the 802.11 link-layer types if we find one, and return -1
3173 * DLT_IEEE802_11_RADIO, with the radiotap header, is considered the
3174 * best 802.11 link-layer type; any of the other 802.11-plus-radio
3175 * headers are second-best; 802.11 with no radio information is
3179 find_802_11(struct bpf_dltlist
*bdlp
)
3185 * Scan the list of DLT_ values, looking for 802.11 values,
3186 * and, if we find any, choose the best of them.
3189 for (i
= 0; i
< bdlp
->bfl_len
; i
++) {
3190 switch (bdlp
->bfl_list
[i
]) {
3192 case DLT_IEEE802_11
:
3194 * 802.11, but no radio.
3196 * Offer this, and select it as the new mode
3197 * unless we've already found an 802.11
3198 * header with radio information.
3201 new_dlt
= bdlp
->bfl_list
[i
];
3204 #ifdef DLT_PRISM_HEADER
3205 case DLT_PRISM_HEADER
:
3207 #ifdef DLT_AIRONET_HEADER
3208 case DLT_AIRONET_HEADER
:
3210 case DLT_IEEE802_11_RADIO_AVS
:
3212 * 802.11 with radio, but not radiotap.
3214 * Offer this, and select it as the new mode
3215 * unless we've already found the radiotap DLT_.
3217 if (new_dlt
!= DLT_IEEE802_11_RADIO
)
3218 new_dlt
= bdlp
->bfl_list
[i
];
3221 case DLT_IEEE802_11_RADIO
:
3223 * 802.11 with radiotap.
3225 * Offer this, and select it as the new mode.
3227 new_dlt
= bdlp
->bfl_list
[i
];
3240 #endif /* defined(BIOCGDLTLIST) && (defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)) */
3242 #if defined(__APPLE__) && defined(BIOCGDLTLIST)
3244 * Remove non-802.11 header types from the list of DLT_ values, as we're in
3245 * monitor mode, and those header types aren't supported in monitor mode.
3248 remove_non_802_11(pcap_t
*p
)
3253 * Scan the list of DLT_ values and discard non-802.11 ones.
3256 for (i
= 0; i
< p
->dlt_count
; i
++) {
3257 switch (p
->dlt_list
[i
]) {
3262 * Not 802.11. Don't offer this one.
3268 * Just copy this mode over.
3274 * Copy this DLT_ value to its new position.
3276 p
->dlt_list
[j
] = p
->dlt_list
[i
];
3281 * Set the DLT_ count to the number of entries we copied.
3287 * Remove 802.11 link-layer types from the list of DLT_ values, as
3288 * we're not in monitor mode, and those DLT_ values will switch us
3292 remove_802_11(pcap_t
*p
)
3297 * Scan the list of DLT_ values and discard 802.11 values.
3300 for (i
= 0; i
< p
->dlt_count
; i
++) {
3301 switch (p
->dlt_list
[i
]) {
3303 case DLT_IEEE802_11
:
3304 #ifdef DLT_PRISM_HEADER
3305 case DLT_PRISM_HEADER
:
3307 #ifdef DLT_AIRONET_HEADER
3308 case DLT_AIRONET_HEADER
:
3310 case DLT_IEEE802_11_RADIO
:
3311 case DLT_IEEE802_11_RADIO_AVS
:
3316 * 802.11. Don't offer this one.
3322 * Just copy this mode over.
3328 * Copy this DLT_ value to its new position.
3330 p
->dlt_list
[j
] = p
->dlt_list
[i
];
3335 * Set the DLT_ count to the number of entries we copied.
3339 #endif /* defined(__APPLE__) && defined(BIOCGDLTLIST) */
3342 pcap_setfilter_bpf(pcap_t
*p
, struct bpf_program
*fp
)
3344 struct pcap_bpf
*pb
= p
->priv
;
3347 * Free any user-mode filter we might happen to have installed.
3349 pcap_freecode(&p
->fcode
);
3352 * Try to install the kernel filter.
3354 if (ioctl(p
->fd
, BIOCSETF
, (caddr_t
)fp
) == 0) {
3358 pb
->filtering_in_kernel
= 1; /* filtering in the kernel */
3361 * Discard any previously-received packets, as they might
3362 * have passed whatever filter was formerly in effect, but
3363 * might not pass this filter (BIOCSETF discards packets
3364 * buffered in the kernel, so you can lose packets in any
3374 * If it failed with EINVAL, that's probably because the program
3375 * is invalid or too big. Validate it ourselves; if we like it
3376 * (we currently allow backward branches, to support protochain),
3377 * run it in userland. (There's no notion of "too big" for
3380 * Otherwise, just give up.
3381 * XXX - if the copy of the program into the kernel failed,
3382 * we will get EINVAL rather than, say, EFAULT on at least
3385 if (errno
!= EINVAL
) {
3386 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
3392 * install_bpf_program() validates the program.
3394 * XXX - what if we already have a filter in the kernel?
3396 if (install_bpf_program(p
, fp
) < 0)
3398 pb
->filtering_in_kernel
= 0; /* filtering in userland */
3403 * Set direction flag: Which packets do we accept on a forwarding
3404 * single device? IN, OUT or both?
3406 #if defined(BIOCSDIRECTION)
3408 pcap_setdirection_bpf(pcap_t
*p
, pcap_direction_t d
)
3411 const char *direction_name
;
3414 * FreeBSD and NetBSD.
3420 * Incoming, but not outgoing, so accept only
3423 direction
= BPF_D_IN
;
3424 direction_name
= "\"incoming only\"";
3429 * Outgoing, but not incoming, so accept only
3432 direction
= BPF_D_OUT
;
3433 direction_name
= "\"outgoing only\"";
3438 * Incoming and outgoing, so accept both
3439 * incoming and outgoing packets.
3441 * It's guaranteed, at this point, that d is a valid
3442 * direction value, so we know that this is PCAP_D_INOUT
3443 * if it's not PCAP_D_IN or PCAP_D_OUT.
3445 direction
= BPF_D_INOUT
;
3446 direction_name
= "\"incoming and outgoing\"";
3450 if (ioctl(p
->fd
, BIOCSDIRECTION
, &direction
) == -1) {
3451 pcap_fmt_errmsg_for_errno(p
->errbuf
, sizeof(p
->errbuf
),
3452 errno
, "Cannot set direction to %s", direction_name
);
3457 #elif defined(BIOCSDIRFILT)
3459 pcap_setdirection_bpf(pcap_t
*p
, pcap_direction_t d
)
3462 const char *direction_name
;
3465 * OpenBSD; same functionality, different names, different
3466 * semantics (the flags mean "*don't* capture packets in
3467 * that direction", not "*capture only* packets in that
3474 * Incoming, but not outgoing, so filter out
3477 dirfilt
= BPF_DIRECTION_OUT
;
3478 direction_name
= "\"incoming only\"";
3483 * Outgoing, but not incoming, so filter out
3486 dirfilt
= BPF_DIRECTION_IN
;
3487 direction_name
= "\"outgoing only\"";
3492 * Incoming and outgoing, so don't filter out
3493 * any packets based on direction.
3495 * It's guaranteed, at this point, that d is a valid
3496 * direction value, so we know that this is PCAP_D_INOUT
3497 * if it's not PCAP_D_IN or PCAP_D_OUT.
3500 direction_name
= "\"incoming and outgoing\"";
3503 if (ioctl(p
->fd
, BIOCSDIRFILT
, &dirfilt
) == -1) {
3504 pcap_fmt_errmsg_for_errno(p
->errbuf
, sizeof(p
->errbuf
),
3505 errno
, "Cannot set direction to %s", direction_name
);
3510 #elif defined(BIOCSSEESENT)
3512 pcap_setdirection_bpf(pcap_t
*p
, pcap_direction_t d
)
3515 const char *direction_name
;
3518 * OS with just BIOCSSEESENT.
3524 * Incoming, but not outgoing, so we don't want to
3525 * see transmitted packets.
3528 direction_name
= "\"incoming only\"";
3533 * Outgoing, but not incoming; we can't specify that.
3535 snprintf(p
->errbuf
, sizeof(p
->errbuf
),
3536 "Setting direction to \"outgoing only\" is not supported on this device");
3541 * Incoming and outgoing, so we want to see transmitted
3544 * It's guaranteed, at this point, that d is a valid
3545 * direction value, so we know that this is PCAP_D_INOUT
3546 * if it's not PCAP_D_IN or PCAP_D_OUT.
3549 direction_name
= "\"incoming and outgoing\"";
3553 if (ioctl(p
->fd
, BIOCSSEESENT
, &seesent
) == -1) {
3554 pcap_fmt_errmsg_for_errno(p
->errbuf
, sizeof(p
->errbuf
),
3555 errno
, "Cannot set direction to %s", direction_name
);
3562 pcap_setdirection_bpf(pcap_t
*p
, pcap_direction_t d _U_
)
3564 (void) snprintf(p
->errbuf
, sizeof(p
->errbuf
),
3565 "Setting direction is not supported on this device");
3572 pcap_set_datalink_bpf(pcap_t
*p
, int dlt
)
3574 if (ioctl(p
->fd
, BIOCSDLT
, &dlt
) == -1) {
3575 pcap_fmt_errmsg_for_errno(p
->errbuf
, sizeof(p
->errbuf
),
3576 errno
, "Cannot set DLT %d", dlt
);
3583 pcap_set_datalink_bpf(pcap_t
*p _U_
, int dlt _U_
)
3590 * Platform-specific information.
3593 pcap_lib_version(void)
3595 #ifdef HAVE_ZEROCOPY_BPF
3596 return (PCAP_VERSION_STRING
" (with zerocopy support)");
3598 return (PCAP_VERSION_STRING
);