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
) {
506 if (errno
== EACCES
) {
507 fd
= PCAP_ERROR_PERM_DENIED
;
508 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
509 "Attempt to open %s failed - root privileges may be required",
513 pcap_fmt_errmsg_for_errno(errbuf
,
514 PCAP_ERRBUF_SIZE
, errno
,
515 "(cannot open device) %s", cloning_device
);
522 if (no_cloning_bpf
) {
524 * We don't have /dev/bpf.
525 * Go through all the /dev/bpfN minors and find one
529 (void)snprintf(device
, sizeof(device
), "/dev/bpf%d", n
++);
531 * Initially try a read/write open (to allow the inject
532 * method to work). If that fails due to permission
533 * issues, fall back to read-only. This allows a
534 * non-root user to be granted specific access to pcap
535 * capabilities via file permissions.
537 * XXX - we should have an API that has a flag that
538 * controls whether to open read-only or read-write,
539 * so that denial of permission to send (or inability
540 * to send, if sending packets isn't supported on
541 * the device in question) can be indicated at open
544 fd
= open(device
, O_RDWR
);
545 if (fd
== -1 && errno
== EACCES
)
546 fd
= open(device
, O_RDONLY
);
547 } while (fd
< 0 && errno
== EBUSY
);
551 * XXX better message for all minors used
560 * /dev/bpf0 doesn't exist, which
561 * means we probably have no BPF
564 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
565 "(there are no BPF devices)");
568 * We got EBUSY on at least one
569 * BPF device, so we have BPF
570 * devices, but all the ones
571 * that exist are busy.
573 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
574 "(all BPF devices are busy)");
580 * Got EACCES on the last device we tried,
581 * and EBUSY on all devices before that,
584 fd
= PCAP_ERROR_PERM_DENIED
;
585 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
586 "Attempt to open %s failed - root privileges may be required",
592 * Some other problem.
595 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
596 errno
, "(cannot open BPF device) %s", device
);
605 * Bind a network adapter to a BPF device, given a descriptor for the
606 * BPF device and the name of the network adapter.
608 * Use BIOCSETLIF if available (meaning "on Solaris"), as it supports
609 * longer device names.
611 * If the name is longer than will fit, return PCAP_ERROR_NO_SUCH_DEVICE
612 * before trying to bind the interface, as there cannot be such a device.
614 * If the attempt succeeds, return BPF_BIND_SUCCEEDED.
616 * If the attempt fails:
618 * if it fails with ENXIO, return PCAP_ERROR_NO_SUCH_DEVICE, as
619 * the device doesn't exist;
621 * if it fails with ENETDOWN, return PCAP_ERROR_IFACE_NOT_UP, as
622 * the interface exists but isn't up and the OS doesn't allow
623 * binding to an interface that isn't up;
625 * if it fails with ENOBUFS, return BPF_BIND_BUFFER_TOO_BIG, and
626 * fill in an error message, as the buffer being requested is too
629 * otherwise, return PCAP_ERROR and fill in an error message.
631 #define BPF_BIND_SUCCEEDED 0
632 #define BPF_BIND_BUFFER_TOO_BIG 1
635 bpf_bind(int fd
, const char *name
, char *errbuf
)
641 if (strlen(name
) >= sizeof(ifr
.lifr_name
)) {
642 /* The name is too long, so it can't possibly exist. */
643 return (PCAP_ERROR_NO_SUCH_DEVICE
);
645 (void)pcap_strlcpy(ifr
.lifr_name
, name
, sizeof(ifr
.lifr_name
));
646 status
= ioctl(fd
, BIOCSETLIF
, (caddr_t
)&ifr
);
650 if (strlen(name
) >= sizeof(ifr
.ifr_name
)) {
651 /* The name is too long, so it can't possibly exist. */
652 return (PCAP_ERROR_NO_SUCH_DEVICE
);
654 (void)pcap_strlcpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
655 status
= ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
);
663 * There's no such device.
665 return (PCAP_ERROR_NO_SUCH_DEVICE
);
669 * Return a "network down" indication, so that
670 * the application can report that rather than
671 * saying we had a mysterious failure and
672 * suggest that they report a problem to the
673 * libpcap developers.
675 return (PCAP_ERROR_IFACE_NOT_UP
);
679 * The buffer size is too big.
680 * Return a special indication so that, if we're
681 * trying to crank the buffer size down, we know
682 * we have to continue; add an error message that
683 * tells the user what needs to be fixed.
685 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
686 errno
, "The requested buffer size for %s is too large",
688 return (BPF_BIND_BUFFER_TOO_BIG
);
691 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
692 errno
, "Binding interface %s to BPF device failed",
697 return (BPF_BIND_SUCCEEDED
);
701 * Open and bind to a device; used if we're not actually going to use
702 * the device, but are just testing whether it can be opened, or opening
703 * it to get information about it.
705 * Returns an error code on failure (always negative), and an FD for
706 * the now-bound BPF device on success (always non-negative).
709 bpf_open_and_bind(const char *name
, char *errbuf
)
715 * First, open a BPF device.
717 fd
= bpf_open(errbuf
);
719 return (fd
); /* fd is the appropriate error code */
722 * Now bind to the device.
724 status
= bpf_bind(fd
, name
, errbuf
);
725 if (status
!= BPF_BIND_SUCCEEDED
) {
727 if (status
== BPF_BIND_BUFFER_TOO_BIG
) {
729 * We didn't specify a buffer size, so
730 * this *really* shouldn't fail because
731 * there's no buffer space. Fail.
746 device_exists(int fd
, const char *name
, char *errbuf
)
751 if (strlen(name
) >= sizeof(ifr
.ifr_name
)) {
752 /* The name is too long, so it can't possibly exist. */
753 return (PCAP_ERROR_NO_SUCH_DEVICE
);
755 (void)pcap_strlcpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
756 status
= ioctl(fd
, SIOCGIFFLAGS
, (caddr_t
)&ifr
);
759 if (errno
== ENXIO
|| errno
== EINVAL
) {
761 * macOS and *BSD return one of those two
762 * errors if the device doesn't exist.
763 * Don't fill in an error, as this is
764 * an "expected" condition.
766 return (PCAP_ERROR_NO_SUCH_DEVICE
);
770 * Some other error - provide a message for it, as
773 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
, errno
,
774 "Can't get interface flags on %s", name
);
787 get_dlt_list(int fd
, int v
, struct bpf_dltlist
*bdlp
, char *ebuf
)
789 memset(bdlp
, 0, sizeof(*bdlp
));
790 if (ioctl(fd
, BIOCGDLTLIST
, (caddr_t
)bdlp
) == 0) {
794 bdlp
->bfl_list
= (u_int
*) malloc(sizeof(u_int
) * (bdlp
->bfl_len
+ 1));
795 if (bdlp
->bfl_list
== NULL
) {
796 pcap_fmt_errmsg_for_errno(ebuf
, PCAP_ERRBUF_SIZE
,
801 if (ioctl(fd
, BIOCGDLTLIST
, (caddr_t
)bdlp
) < 0) {
802 pcap_fmt_errmsg_for_errno(ebuf
, PCAP_ERRBUF_SIZE
,
803 errno
, "BIOCGDLTLIST");
804 free(bdlp
->bfl_list
);
809 * OK, for real Ethernet devices, add DLT_DOCSIS to the
810 * list, so that an application can let you choose it,
811 * in case you're capturing DOCSIS traffic that a Cisco
812 * Cable Modem Termination System is putting out onto
813 * an Ethernet (it doesn't put an Ethernet header onto
814 * the wire, it puts raw DOCSIS frames out on the wire
815 * inside the low-level Ethernet framing).
817 * A "real Ethernet device" is defined here as a device
818 * that has a link-layer type of DLT_EN10MB and that has
819 * no alternate link-layer types; that's done to exclude
820 * 802.11 interfaces (which might or might not be the
821 * right thing to do, but I suspect it is - Ethernet <->
822 * 802.11 bridges would probably badly mishandle frames
823 * that don't have Ethernet headers).
825 * On Solaris with BPF, Ethernet devices also offer
826 * DLT_IPNET, so we, if DLT_IPNET is defined, we don't
827 * treat it as an indication that the device isn't an
830 if (v
== DLT_EN10MB
) {
832 for (i
= 0; i
< bdlp
->bfl_len
; i
++) {
833 if (bdlp
->bfl_list
[i
] != DLT_EN10MB
835 && bdlp
->bfl_list
[i
] != DLT_IPNET
844 * We reserved one more slot at the end of
847 bdlp
->bfl_list
[bdlp
->bfl_len
] = DLT_DOCSIS
;
853 * EINVAL just means "we don't support this ioctl on
854 * this device"; don't treat it as an error.
856 if (errno
!= EINVAL
) {
857 pcap_fmt_errmsg_for_errno(ebuf
, PCAP_ERRBUF_SIZE
,
858 errno
, "BIOCGDLTLIST");
866 #if defined(__APPLE__)
868 pcap_can_set_rfmon_bpf(pcap_t
*p
)
870 struct utsname osinfo
;
873 struct bpf_dltlist bdl
;
878 * The joys of monitor mode on Mac OS X/OS X/macOS.
880 * Prior to 10.4, it's not supported at all.
882 * In 10.4, if adapter enN supports monitor mode, there's a
883 * wltN adapter corresponding to it; you open it, instead of
884 * enN, to get monitor mode. You get whatever link-layer
885 * headers it supplies.
887 * In 10.5, and, we assume, later releases, if adapter enN
888 * supports monitor mode, it offers, among its selectable
889 * DLT_ values, values that let you get the 802.11 header;
890 * selecting one of those values puts the adapter into monitor
891 * mode (i.e., you can't get 802.11 headers except in monitor
892 * mode, and you can't get Ethernet headers in monitor mode).
894 if (uname(&osinfo
) == -1) {
896 * Can't get the OS version; just say "no".
901 * We assume osinfo.sysname is "Darwin", because
902 * __APPLE__ is defined. We just check the version.
904 if (osinfo
.release
[0] < '8' && osinfo
.release
[1] == '.') {
906 * 10.3 (Darwin 7.x) or earlier.
907 * Monitor mode not supported.
911 if (osinfo
.release
[0] == '8' && osinfo
.release
[1] == '.') {
916 * 10.4 (Darwin 8.x). s/en/wlt/, and check
917 * whether the device exists.
919 if (strncmp(p
->opt
.device
, "en", 2) != 0) {
921 * Not an enN device; no monitor mode.
925 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
927 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
931 if (pcap_asprintf(&wlt_name
, "wlt%s", p
->opt
.device
+ 2) == -1) {
932 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
937 status
= device_exists(fd
, wlt_name
, p
->errbuf
);
941 if (status
== PCAP_ERROR_NO_SUCH_DEVICE
)
954 * Everything else is 10.5 or later; for those,
955 * we just open the enN device, and check whether
956 * we have any 802.11 devices.
958 * First, open a BPF device.
960 fd
= bpf_open(p
->errbuf
);
962 return (fd
); /* fd is the appropriate error code */
965 * Now bind to the device.
967 err
= bpf_bind(fd
, p
->opt
.device
, p
->errbuf
);
968 if (err
!= BPF_BIND_SUCCEEDED
) {
970 if (err
== BPF_BIND_BUFFER_TOO_BIG
) {
972 * We didn't specify a buffer size, so
973 * this *really* shouldn't fail because
974 * there's no buffer space. Fail.
982 * We know the default link type -- now determine all the DLTs
983 * this interface supports. If this fails with EINVAL, it's
984 * not fatal; we just don't get to use the feature later.
985 * (We don't care about DLT_DOCSIS, so we pass DLT_NULL
986 * as the default DLT for this adapter.)
988 if (get_dlt_list(fd
, DLT_NULL
, &bdl
, p
->errbuf
) == PCAP_ERROR
) {
992 if (find_802_11(&bdl
) != -1) {
994 * We have an 802.11 DLT, so we can set monitor mode.
1002 #endif /* BIOCGDLTLIST */
1005 #elif defined(HAVE_BSD_IEEE80211)
1007 pcap_can_set_rfmon_bpf(pcap_t
*p
)
1011 ret
= monitor_mode(p
, 0);
1012 if (ret
== PCAP_ERROR_RFMON_NOTSUP
)
1013 return (0); /* not an error, just a "can't do" */
1015 return (1); /* success */
1020 pcap_can_set_rfmon_bpf(pcap_t
*p _U_
)
1027 pcap_stats_bpf(pcap_t
*p
, struct pcap_stat
*ps
)
1032 * "ps_recv" counts packets handed to the filter, not packets
1033 * that passed the filter. This includes packets later dropped
1034 * because we ran out of buffer space.
1036 * "ps_drop" counts packets dropped inside the BPF device
1037 * because we ran out of buffer space. It doesn't count
1038 * packets dropped by the interface driver. It counts
1039 * only packets that passed the filter.
1041 * Both statistics include packets not yet read from the kernel
1042 * by libpcap, and thus not yet seen by the application.
1044 if (ioctl(p
->fd
, BIOCGSTATS
, (caddr_t
)&s
) < 0) {
1045 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1046 errno
, "BIOCGSTATS");
1047 return (PCAP_ERROR
);
1050 ps
->ps_recv
= s
.bs_recv
;
1051 ps
->ps_drop
= s
.bs_drop
;
1057 pcap_read_bpf(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
1059 struct pcap_bpf
*pb
= p
->priv
;
1062 register u_char
*bp
, *ep
;
1067 #ifdef HAVE_ZEROCOPY_BPF
1073 * Has "pcap_breakloop()" been called?
1075 if (p
->break_loop
) {
1077 * Yes - clear the flag that indicates that it
1078 * has, and return PCAP_ERROR_BREAK to indicate
1079 * that we were told to break out of the loop.
1082 return (PCAP_ERROR_BREAK
);
1087 * When reading without zero-copy from a file descriptor, we
1088 * use a single buffer and return a length of data in the
1089 * buffer. With zero-copy, we update the p->buffer pointer
1090 * to point at whatever underlying buffer contains the next
1091 * data and update cc to reflect the data found in the
1094 #ifdef HAVE_ZEROCOPY_BPF
1096 if (p
->buffer
!= NULL
)
1098 i
= pcap_next_zbuf(p
, &cc
);
1102 return (PCAP_ERROR
);
1106 cc
= (int)read(p
->fd
, p
->buffer
, p
->bufsize
);
1109 /* Don't choke when we get ptraced */
1118 * Sigh. More AIX wonderfulness.
1120 * For some unknown reason the uiomove()
1121 * operation in the bpf kernel extension
1122 * used to copy the buffer into user
1123 * space sometimes returns EFAULT. I have
1124 * no idea why this is the case given that
1125 * a kernel debugger shows the user buffer
1126 * is correct. This problem appears to
1127 * be mostly mitigated by the memset of
1128 * the buffer before it is first used.
1129 * Very strange.... Shaun Clowes
1131 * In any case this means that we shouldn't
1132 * treat EFAULT as a fatal error; as we
1133 * don't have an API for returning
1134 * a "some packets were dropped since
1135 * the last packet you saw" indication,
1136 * we just ignore EFAULT and keep reading.
1144 case ENXIO
: /* FreeBSD, DragonFly BSD, and Darwin */
1145 case EIO
: /* OpenBSD */
1146 /* NetBSD appears not to return an error in this case */
1148 * The device on which we're capturing
1151 * XXX - we should really return
1152 * an appropriate error for that,
1153 * but pcap_dispatch() etc. aren't
1154 * documented as having error returns
1155 * other than PCAP_ERROR or PCAP_ERROR_BREAK.
1157 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1158 "The interface disappeared");
1159 return (PCAP_ERROR
);
1161 #if defined(sun) && !defined(BSD) && !defined(__svr4__) && !defined(__SVR4)
1163 * Due to a SunOS bug, after 2^31 bytes, the kernel
1164 * file offset overflows and read fails with EINVAL.
1165 * The lseek() to 0 will fix things.
1168 if (lseek(p
->fd
, 0L, SEEK_CUR
) +
1170 (void)lseek(p
->fd
, 0L, SEEK_SET
);
1176 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1178 return (PCAP_ERROR
);
1180 bp
= (u_char
*)p
->buffer
;
1185 * Loop through each packet.
1187 * This assumes that a single buffer of packets will have
1188 * <= INT_MAX packets, so the packet count doesn't overflow.
1191 #define bhp ((struct bpf_xhdr *)bp)
1193 #define bhp ((struct bpf_hdr *)bp)
1200 register u_int caplen
, hdrlen
;
1203 * Has "pcap_breakloop()" been called?
1204 * If so, return immediately - if we haven't read any
1205 * packets, clear the flag and return PCAP_ERROR_BREAK
1206 * to indicate that we were told to break out of the loop,
1207 * otherwise leave the flag set, so that the *next* call
1208 * will break out of the loop without having read any
1209 * packets, and return the number of packets we've
1212 if (p
->break_loop
) {
1214 p
->cc
= (int)(ep
- bp
);
1216 * ep is set based on the return value of read(),
1217 * but read() from a BPF device doesn't necessarily
1218 * return a value that's a multiple of the alignment
1219 * value for BPF_WORDALIGN(). However, whenever we
1220 * increment bp, we round up the increment value by
1221 * a value rounded up by BPF_WORDALIGN(), so we
1222 * could increment bp past ep after processing the
1223 * last packet in the buffer.
1225 * We treat ep < bp as an indication that this
1226 * happened, and just set p->cc to 0.
1232 return (PCAP_ERROR_BREAK
);
1237 caplen
= bhp
->bh_caplen
;
1238 hdrlen
= bhp
->bh_hdrlen
;
1239 datap
= bp
+ hdrlen
;
1241 * Short-circuit evaluation: if using BPF filter
1242 * in kernel, no need to do it now - we already know
1243 * the packet passed the filter.
1246 * Note: the filter code was generated assuming
1247 * that p->fddipad was the amount of padding
1248 * before the header, as that's what's required
1249 * in the kernel, so we run the filter before
1250 * skipping that padding.
1253 if (pb
->filtering_in_kernel
||
1254 pcap_filter(p
->fcode
.bf_insns
, datap
, bhp
->bh_datalen
, caplen
)) {
1255 struct pcap_pkthdr pkthdr
;
1259 bt
.sec
= bhp
->bh_tstamp
.bt_sec
;
1260 bt
.frac
= bhp
->bh_tstamp
.bt_frac
;
1261 if (p
->opt
.tstamp_precision
== PCAP_TSTAMP_PRECISION_NANO
) {
1264 bintime2timespec(&bt
, &ts
);
1265 pkthdr
.ts
.tv_sec
= ts
.tv_sec
;
1266 pkthdr
.ts
.tv_usec
= ts
.tv_nsec
;
1270 bintime2timeval(&bt
, &tv
);
1271 pkthdr
.ts
.tv_sec
= tv
.tv_sec
;
1272 pkthdr
.ts
.tv_usec
= tv
.tv_usec
;
1275 pkthdr
.ts
.tv_sec
= bhp
->bh_tstamp
.tv_sec
;
1278 * AIX's BPF returns seconds/nanoseconds time
1279 * stamps, not seconds/microseconds time stamps.
1281 pkthdr
.ts
.tv_usec
= bhp
->bh_tstamp
.tv_usec
/1000;
1283 pkthdr
.ts
.tv_usec
= bhp
->bh_tstamp
.tv_usec
;
1285 #endif /* BIOCSTSTAMP */
1288 pkthdr
.caplen
= caplen
- pad
;
1291 if (bhp
->bh_datalen
> pad
)
1292 pkthdr
.len
= bhp
->bh_datalen
- pad
;
1297 pkthdr
.caplen
= caplen
;
1298 pkthdr
.len
= bhp
->bh_datalen
;
1300 (*callback
)(user
, &pkthdr
, datap
);
1301 bp
+= BPF_WORDALIGN(caplen
+ hdrlen
);
1302 if (++n
>= cnt
&& !PACKET_COUNT_IS_UNLIMITED(cnt
)) {
1304 p
->cc
= (int)(ep
- bp
);
1306 * See comment above about p->cc < 0.
1316 bp
+= BPF_WORDALIGN(caplen
+ hdrlen
);
1325 pcap_inject_bpf(pcap_t
*p
, const void *buf
, int size
)
1329 ret
= (int)write(p
->fd
, buf
, size
);
1331 if (ret
== -1 && errno
== EAFNOSUPPORT
) {
1333 * In some versions of macOS, there's a bug wherein setting
1334 * the BIOCSHDRCMPLT flag causes writes to fail; see, for
1337 * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/BIOCSHDRCMPLT-10.3.3.patch
1339 * So, if, on macOS, we get EAFNOSUPPORT from the write, we
1340 * assume it's due to that bug, and turn off that flag
1341 * and try again. If we succeed, it either means that
1342 * somebody applied the fix from that URL, or other patches
1345 * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/
1347 * and are running a Darwin kernel with those fixes, or
1348 * that Apple fixed the problem in some macOS release.
1350 u_int spoof_eth_src
= 0;
1352 if (ioctl(p
->fd
, BIOCSHDRCMPLT
, &spoof_eth_src
) == -1) {
1353 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1354 errno
, "send: can't turn off BIOCSHDRCMPLT");
1355 return (PCAP_ERROR
);
1359 * Now try the write again.
1361 ret
= (int)write(p
->fd
, buf
, size
);
1363 #endif /* __APPLE__ */
1365 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1367 return (PCAP_ERROR
);
1374 bpf_odminit(char *errbuf
)
1378 if (odm_initialize() == -1) {
1379 if (odm_err_msg(odmerrno
, &errstr
) == -1)
1380 errstr
= "Unknown error";
1381 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1382 "bpf_load: odm_initialize failed: %s",
1384 return (PCAP_ERROR
);
1387 if ((odmlockid
= odm_lock("/etc/objrepos/config_lock", ODM_WAIT
)) == -1) {
1388 if (odm_err_msg(odmerrno
, &errstr
) == -1)
1389 errstr
= "Unknown error";
1390 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1391 "bpf_load: odm_lock of /etc/objrepos/config_lock failed: %s",
1393 (void)odm_terminate();
1394 return (PCAP_ERROR
);
1401 bpf_odmcleanup(char *errbuf
)
1405 if (odm_unlock(odmlockid
) == -1) {
1406 if (errbuf
!= NULL
) {
1407 if (odm_err_msg(odmerrno
, &errstr
) == -1)
1408 errstr
= "Unknown error";
1409 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1410 "bpf_load: odm_unlock failed: %s",
1413 return (PCAP_ERROR
);
1416 if (odm_terminate() == -1) {
1417 if (errbuf
!= NULL
) {
1418 if (odm_err_msg(odmerrno
, &errstr
) == -1)
1419 errstr
= "Unknown error";
1420 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1421 "bpf_load: odm_terminate failed: %s",
1424 return (PCAP_ERROR
);
1431 bpf_load(char *errbuf
)
1435 int numminors
, i
, rc
;
1438 struct bpf_config cfg_bpf
;
1439 struct cfg_load cfg_ld
;
1440 struct cfg_kmod cfg_km
;
1443 * This is very very close to what happens in the real implementation
1444 * but I've fixed some (unlikely) bug situations.
1449 if (bpf_odminit(errbuf
) == PCAP_ERROR
)
1450 return (PCAP_ERROR
);
1452 major
= genmajor(BPF_NAME
);
1454 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
1455 errno
, "bpf_load: genmajor failed");
1456 (void)bpf_odmcleanup(NULL
);
1457 return (PCAP_ERROR
);
1460 minors
= getminor(major
, &numminors
, BPF_NAME
);
1462 minors
= genminor("bpf", major
, 0, BPF_MINORS
, 1, 1);
1464 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
1465 errno
, "bpf_load: genminor failed");
1466 (void)bpf_odmcleanup(NULL
);
1467 return (PCAP_ERROR
);
1471 if (bpf_odmcleanup(errbuf
) == PCAP_ERROR
)
1472 return (PCAP_ERROR
);
1474 rc
= stat(BPF_NODE
"0", &sbuf
);
1475 if (rc
== -1 && errno
!= ENOENT
) {
1476 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
1477 errno
, "bpf_load: can't stat %s", BPF_NODE
"0");
1478 return (PCAP_ERROR
);
1481 if (rc
== -1 || getmajor(sbuf
.st_rdev
) != major
) {
1482 for (i
= 0; i
< BPF_MINORS
; i
++) {
1483 snprintf(buf
, sizeof(buf
), "%s%d", BPF_NODE
, i
);
1485 if (mknod(buf
, S_IRUSR
| S_IFCHR
, domakedev(major
, i
)) == -1) {
1486 pcap_fmt_errmsg_for_errno(errbuf
,
1487 PCAP_ERRBUF_SIZE
, errno
,
1488 "bpf_load: can't mknod %s", buf
);
1489 return (PCAP_ERROR
);
1494 /* Check if the driver is loaded */
1495 memset(&cfg_ld
, 0x0, sizeof(cfg_ld
));
1496 snprintf(buf
, sizeof(buf
), "%s/%s", DRIVER_PATH
, BPF_NAME
);
1498 if ((sysconfig(SYS_QUERYLOAD
, (void *)&cfg_ld
, sizeof(cfg_ld
)) == -1) ||
1499 (cfg_ld
.kmid
== 0)) {
1500 /* Driver isn't loaded, load it now */
1501 if (sysconfig(SYS_SINGLELOAD
, (void *)&cfg_ld
, sizeof(cfg_ld
)) == -1) {
1502 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
1503 errno
, "bpf_load: could not load driver");
1504 return (PCAP_ERROR
);
1508 /* Configure the driver */
1509 cfg_km
.cmd
= CFG_INIT
;
1510 cfg_km
.kmid
= cfg_ld
.kmid
;
1511 cfg_km
.mdilen
= sizeof(cfg_bpf
);
1512 cfg_km
.mdiptr
= (void *)&cfg_bpf
;
1513 for (i
= 0; i
< BPF_MINORS
; i
++) {
1514 cfg_bpf
.devno
= domakedev(major
, i
);
1515 if (sysconfig(SYS_CFGKMOD
, (void *)&cfg_km
, sizeof(cfg_km
)) == -1) {
1516 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
1517 errno
, "bpf_load: could not configure driver");
1518 return (PCAP_ERROR
);
1529 * Undo any operations done when opening the device when necessary.
1532 pcap_cleanup_bpf(pcap_t
*p
)
1534 struct pcap_bpf
*pb
= p
->priv
;
1535 #ifdef HAVE_BSD_IEEE80211
1537 struct ifmediareq req
;
1541 if (pb
->must_do_on_close
!= 0) {
1543 * There's something we have to do when closing this
1546 #ifdef HAVE_BSD_IEEE80211
1547 if (pb
->must_do_on_close
& MUST_CLEAR_RFMON
) {
1549 * We put the interface into rfmon mode;
1550 * take it out of rfmon mode.
1552 * XXX - if somebody else wants it in rfmon
1553 * mode, this code cannot know that, so it'll take
1554 * it out of rfmon mode.
1556 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
1559 "Can't restore interface flags (socket() failed: %s).\n"
1560 "Please adjust manually.\n",
1563 memset(&req
, 0, sizeof(req
));
1564 pcap_strlcpy(req
.ifm_name
, pb
->device
,
1565 sizeof(req
.ifm_name
));
1566 if (ioctl(sock
, SIOCGIFMEDIA
, &req
) < 0) {
1568 "Can't restore interface flags (SIOCGIFMEDIA failed: %s).\n"
1569 "Please adjust manually.\n",
1572 if (req
.ifm_current
& IFM_IEEE80211_MONITOR
) {
1574 * Rfmon mode is currently on;
1577 memset(&ifr
, 0, sizeof(ifr
));
1578 (void)pcap_strlcpy(ifr
.ifr_name
,
1580 sizeof(ifr
.ifr_name
));
1582 req
.ifm_current
& ~IFM_IEEE80211_MONITOR
;
1583 if (ioctl(sock
, SIOCSIFMEDIA
,
1586 "Can't restore interface flags (SIOCSIFMEDIA failed: %s).\n"
1587 "Please adjust manually.\n",
1595 #endif /* HAVE_BSD_IEEE80211 */
1597 #if defined(__FreeBSD__) && defined(SIOCIFCREATE2)
1599 * Attempt to destroy the usbusN interface that we created.
1601 if (pb
->must_do_on_close
& MUST_DESTROY_USBUS
) {
1602 if (if_nametoindex(pb
->device
) > 0) {
1605 s
= socket(AF_LOCAL
, SOCK_DGRAM
, 0);
1607 pcap_strlcpy(ifr
.ifr_name
, pb
->device
,
1608 sizeof(ifr
.ifr_name
));
1609 ioctl(s
, SIOCIFDESTROY
, &ifr
);
1614 #endif /* defined(__FreeBSD__) && defined(SIOCIFCREATE2) */
1616 * Take this pcap out of the list of pcaps for which we
1617 * have to take the interface out of some mode.
1619 pcap_remove_from_pcaps_to_close(p
);
1620 pb
->must_do_on_close
= 0;
1623 #ifdef HAVE_ZEROCOPY_BPF
1626 * Delete the mappings. Note that p->buffer gets
1627 * initialized to one of the mmapped regions in
1628 * this case, so do not try and free it directly;
1629 * null it out so that pcap_cleanup_live_common()
1630 * doesn't try to free it.
1632 if (pb
->zbuf1
!= MAP_FAILED
&& pb
->zbuf1
!= NULL
)
1633 (void) munmap(pb
->zbuf1
, pb
->zbufsize
);
1634 if (pb
->zbuf2
!= MAP_FAILED
&& pb
->zbuf2
!= NULL
)
1635 (void) munmap(pb
->zbuf2
, pb
->zbufsize
);
1639 if (pb
->device
!= NULL
) {
1643 pcap_cleanup_live_common(p
);
1648 check_setif_failure(pcap_t
*p
, int error
)
1653 if (error
== PCAP_ERROR_NO_SUCH_DEVICE
) {
1655 * No such device exists.
1657 if (p
->opt
.rfmon
&& strncmp(p
->opt
.device
, "wlt", 3) == 0) {
1659 * Monitor mode was requested, and we're trying
1660 * to open a "wltN" device. Assume that this
1661 * is 10.4 and that we were asked to open an
1662 * "enN" device; if that device exists, return
1663 * "monitor mode not supported on the device".
1665 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
1669 if (pcap_asprintf(&en_name
, "en%s",
1670 p
->opt
.device
+ 3) == -1) {
1672 * We can't find out whether there's
1673 * an underlying "enN" device, so
1674 * just report "no such device".
1676 pcap_fmt_errmsg_for_errno(p
->errbuf
,
1677 PCAP_ERRBUF_SIZE
, errno
,
1680 return (PCAP_ERROR_NO_SUCH_DEVICE
);
1682 err
= device_exists(fd
, en_name
, p
->errbuf
);
1685 if (err
== PCAP_ERROR_NO_SUCH_DEVICE
) {
1687 * The underlying "enN" device
1688 * exists, but there's no
1689 * corresponding "wltN" device;
1690 * that means that the "enN"
1691 * device doesn't support
1692 * monitor mode, probably
1693 * because it's an Ethernet
1694 * device rather than a
1697 err
= PCAP_ERROR_RFMON_NOTSUP
;
1703 * We can't find out whether there's
1704 * an underlying "enN" device, so
1705 * just report "no such device".
1707 err
= PCAP_ERROR_NO_SUCH_DEVICE
;
1708 pcap_fmt_errmsg_for_errno(p
->errbuf
,
1709 errno
, PCAP_ERRBUF_SIZE
,
1718 return (PCAP_ERROR_NO_SUCH_DEVICE
);
1722 * Just return the error status; it's what we want, and, if it's
1723 * PCAP_ERROR, the error string has been filled in.
1729 check_setif_failure(pcap_t
*p _U_
, int error
)
1732 * Just return the error status; it's what we want, and, if it's
1733 * PCAP_ERROR, the error string has been filled in.
1740 * Default capture buffer size.
1741 * 32K isn't very much for modern machines with fast networks; we
1742 * pick .5M, as that's the maximum on at least some systems with BPF.
1744 * However, on AIX 3.5, the larger buffer sized caused unrecoverable
1745 * read failures under stress, so we leave it as 32K; yet another
1746 * place where AIX's BPF is broken.
1749 #define DEFAULT_BUFSIZE 32768
1751 #define DEFAULT_BUFSIZE 524288
1755 pcap_activate_bpf(pcap_t
*p
)
1757 struct pcap_bpf
*pb
= p
->priv
;
1759 #ifdef HAVE_BSD_IEEE80211
1763 #if defined(LIFNAMSIZ) && defined(ZONENAME_MAX) && defined(lifr_zoneid)
1767 struct bpf_version bv
;
1770 char *wltdev
= NULL
;
1773 struct bpf_dltlist bdl
;
1774 #if defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)
1777 #endif /* BIOCGDLTLIST */
1778 #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
1779 u_int spoof_eth_src
= 1;
1782 struct bpf_insn total_insn
;
1783 struct bpf_program total_prog
;
1784 struct utsname osinfo
;
1785 int have_osinfo
= 0;
1786 #ifdef HAVE_ZEROCOPY_BPF
1788 u_int bufmode
, zbufmax
;
1791 fd
= bpf_open(p
->errbuf
);
1799 if (ioctl(fd
, BIOCVERSION
, (caddr_t
)&bv
) < 0) {
1800 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1801 errno
, "BIOCVERSION");
1802 status
= PCAP_ERROR
;
1805 if (bv
.bv_major
!= BPF_MAJOR_VERSION
||
1806 bv
.bv_minor
< BPF_MINOR_VERSION
) {
1807 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1808 "kernel bpf filter out of date");
1809 status
= PCAP_ERROR
;
1814 * Turn a negative snapshot value (invalid), a snapshot value of
1815 * 0 (unspecified), or a value bigger than the normal maximum
1816 * value, into the maximum allowed value.
1818 * If some application really *needs* a bigger snapshot
1819 * length, we should just increase MAXIMUM_SNAPLEN.
1821 if (p
->snapshot
<= 0 || p
->snapshot
> MAXIMUM_SNAPLEN
)
1822 p
->snapshot
= MAXIMUM_SNAPLEN
;
1824 #if defined(LIFNAMSIZ) && defined(ZONENAME_MAX) && defined(lifr_zoneid)
1826 * Retrieve the zoneid of the zone we are currently executing in.
1828 if ((ifr
.lifr_zoneid
= getzoneid()) == -1) {
1829 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1830 errno
, "getzoneid()");
1831 status
= PCAP_ERROR
;
1835 * Check if the given source datalink name has a '/' separated
1836 * zonename prefix string. The zonename prefixed source datalink can
1837 * be used by pcap consumers in the Solaris global zone to capture
1838 * traffic on datalinks in non-global zones. Non-global zones
1839 * do not have access to datalinks outside of their own namespace.
1841 if ((zonesep
= strchr(p
->opt
.device
, '/')) != NULL
) {
1842 char path_zname
[ZONENAME_MAX
];
1846 if (ifr
.lifr_zoneid
!= GLOBAL_ZONEID
) {
1847 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1848 "zonename/linkname only valid in global zone.");
1849 status
= PCAP_ERROR
;
1852 znamelen
= zonesep
- p
->opt
.device
;
1853 (void) pcap_strlcpy(path_zname
, p
->opt
.device
, znamelen
+ 1);
1854 ifr
.lifr_zoneid
= getzoneidbyname(path_zname
);
1855 if (ifr
.lifr_zoneid
== -1) {
1856 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1857 errno
, "getzoneidbyname(%s)", path_zname
);
1858 status
= PCAP_ERROR
;
1861 lnamep
= strdup(zonesep
+ 1);
1862 if (lnamep
== NULL
) {
1863 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1865 status
= PCAP_ERROR
;
1868 free(p
->opt
.device
);
1869 p
->opt
.device
= lnamep
;
1873 pb
->device
= strdup(p
->opt
.device
);
1874 if (pb
->device
== NULL
) {
1875 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
1877 status
= PCAP_ERROR
;
1882 * Attempt to find out the version of the OS on which we're running.
1884 if (uname(&osinfo
) == 0)
1889 * See comment in pcap_can_set_rfmon_bpf() for an explanation
1890 * of why we check the version number.
1895 * We assume osinfo.sysname is "Darwin", because
1896 * __APPLE__ is defined. We just check the version.
1898 if (osinfo
.release
[0] < '8' &&
1899 osinfo
.release
[1] == '.') {
1901 * 10.3 (Darwin 7.x) or earlier.
1903 status
= PCAP_ERROR_RFMON_NOTSUP
;
1906 if (osinfo
.release
[0] == '8' &&
1907 osinfo
.release
[1] == '.') {
1909 * 10.4 (Darwin 8.x). s/en/wlt/
1911 if (strncmp(p
->opt
.device
, "en", 2) != 0) {
1913 * Not an enN device; check
1914 * whether the device even exists.
1916 sockfd
= socket(AF_INET
, SOCK_DGRAM
, 0);
1918 status
= device_exists(sockfd
,
1919 p
->opt
.device
, p
->errbuf
);
1922 * The device exists,
1929 status
= PCAP_ERROR_RFMON_NOTSUP
;
1934 * We can't find out whether
1935 * the device exists, so just
1936 * report "no such device".
1938 status
= PCAP_ERROR_NO_SUCH_DEVICE
;
1939 pcap_fmt_errmsg_for_errno(p
->errbuf
,
1940 PCAP_ERRBUF_SIZE
, errno
,
1945 wltdev
= malloc(strlen(p
->opt
.device
) + 2);
1946 if (wltdev
== NULL
) {
1947 pcap_fmt_errmsg_for_errno(p
->errbuf
,
1948 PCAP_ERRBUF_SIZE
, errno
,
1950 status
= PCAP_ERROR
;
1953 strcpy(wltdev
, "wlt");
1954 strcat(wltdev
, p
->opt
.device
+ 2);
1955 free(p
->opt
.device
);
1956 p
->opt
.device
= wltdev
;
1959 * Everything else is 10.5 or later; for those,
1960 * we just open the enN device, and set the DLT.
1964 #endif /* __APPLE__ */
1967 * If this is FreeBSD, and the device name begins with "usbus",
1968 * try to create the interface if it's not available.
1970 #if defined(__FreeBSD__) && defined(SIOCIFCREATE2)
1971 if (strncmp(p
->opt
.device
, usbus_prefix
, USBUS_PREFIX_LEN
) == 0) {
1973 * Do we already have an interface with that name?
1975 if (if_nametoindex(p
->opt
.device
) == 0) {
1977 * No. We need to create it, and, if we
1978 * succeed, remember that we should destroy
1979 * it when the pcap_t is closed.
1985 * Open a socket to use for ioctls to
1986 * create the interface.
1988 s
= socket(AF_LOCAL
, SOCK_DGRAM
, 0);
1990 pcap_fmt_errmsg_for_errno(p
->errbuf
,
1991 PCAP_ERRBUF_SIZE
, errno
,
1992 "Can't open socket");
1993 status
= PCAP_ERROR
;
1998 * If we haven't already done so, arrange to have
1999 * "pcap_close_all()" called when we exit.
2001 if (!pcap_do_addexit(p
)) {
2003 * "atexit()" failed; don't create the
2004 * interface, just give up.
2006 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2009 status
= PCAP_ERROR
;
2014 * Create the interface.
2016 pcap_strlcpy(ifr
.ifr_name
, p
->opt
.device
, sizeof(ifr
.ifr_name
));
2017 if (ioctl(s
, SIOCIFCREATE2
, &ifr
) < 0) {
2018 if (errno
== EINVAL
) {
2019 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2020 "Invalid USB bus interface %s",
2023 pcap_fmt_errmsg_for_errno(p
->errbuf
,
2024 PCAP_ERRBUF_SIZE
, errno
,
2025 "Can't create interface for %s",
2029 status
= PCAP_ERROR
;
2034 * Make sure we clean this up when we close.
2036 pb
->must_do_on_close
|= MUST_DESTROY_USBUS
;
2039 * Add this to the list of pcaps to close when we exit.
2041 pcap_add_to_pcaps_to_close(p
);
2044 #endif /* defined(__FreeBSD__) && defined(SIOCIFCREATE2) */
2046 #ifdef HAVE_ZEROCOPY_BPF
2048 * If the BPF extension to set buffer mode is present, try setting
2049 * the mode to zero-copy. If that fails, use regular buffering. If
2050 * it succeeds but other setup fails, return an error to the user.
2052 bufmode
= BPF_BUFMODE_ZBUF
;
2053 if (ioctl(fd
, BIOCSETBUFMODE
, (caddr_t
)&bufmode
) == 0) {
2055 * We have zerocopy BPF; use it.
2060 * How to pick a buffer size: first, query the maximum buffer
2061 * size supported by zero-copy. This also lets us quickly
2062 * determine whether the kernel generally supports zero-copy.
2063 * Then, if a buffer size was specified, use that, otherwise
2064 * query the default buffer size, which reflects kernel
2065 * policy for a desired default. Round to the nearest page
2068 if (ioctl(fd
, BIOCGETZMAX
, (caddr_t
)&zbufmax
) < 0) {
2069 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2070 errno
, "BIOCGETZMAX");
2071 status
= PCAP_ERROR
;
2075 if (p
->opt
.buffer_size
!= 0) {
2077 * A buffer size was explicitly specified; use it.
2079 v
= p
->opt
.buffer_size
;
2081 if ((ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) ||
2082 v
< DEFAULT_BUFSIZE
)
2083 v
= DEFAULT_BUFSIZE
;
2086 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) /* to any y */
2088 pb
->zbufsize
= roundup(v
, getpagesize());
2089 if (pb
->zbufsize
> zbufmax
)
2090 pb
->zbufsize
= zbufmax
;
2091 pb
->zbuf1
= mmap(NULL
, pb
->zbufsize
, PROT_READ
| PROT_WRITE
,
2093 pb
->zbuf2
= mmap(NULL
, pb
->zbufsize
, PROT_READ
| PROT_WRITE
,
2095 if (pb
->zbuf1
== MAP_FAILED
|| pb
->zbuf2
== MAP_FAILED
) {
2096 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2098 status
= PCAP_ERROR
;
2101 memset(&bz
, 0, sizeof(bz
)); /* bzero() deprecated, replaced with memset() */
2102 bz
.bz_bufa
= pb
->zbuf1
;
2103 bz
.bz_bufb
= pb
->zbuf2
;
2104 bz
.bz_buflen
= pb
->zbufsize
;
2105 if (ioctl(fd
, BIOCSETZBUF
, (caddr_t
)&bz
) < 0) {
2106 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2107 errno
, "BIOCSETZBUF");
2108 status
= PCAP_ERROR
;
2111 status
= bpf_bind(fd
, p
->opt
.device
, ifnamsiz
, p
->errbuf
);
2112 if (status
!= BPF_BIND_SUCCEEDED
) {
2113 if (status
== BPF_BIND_BUFFER_TOO_BIG
) {
2115 * The requested buffer size
2118 * XXX - should we do the "keep cutting
2119 * the buffer size in half" loop here if
2120 * we're using the default buffer size?
2122 status
= PCAP_ERROR
;
2126 v
= pb
->zbufsize
- sizeof(struct bpf_zbuf_header
);
2131 * We don't have zerocopy BPF.
2132 * Set the buffer size.
2134 if (p
->opt
.buffer_size
!= 0) {
2136 * A buffer size was explicitly specified; use it.
2138 if (ioctl(fd
, BIOCSBLEN
,
2139 (caddr_t
)&p
->opt
.buffer_size
) < 0) {
2140 pcap_fmt_errmsg_for_errno(p
->errbuf
,
2141 PCAP_ERRBUF_SIZE
, errno
,
2142 "BIOCSBLEN: %s", p
->opt
.device
);
2143 status
= PCAP_ERROR
;
2148 * Now bind to the device.
2150 status
= bpf_bind(fd
, p
->opt
.device
, p
->errbuf
);
2151 if (status
!= BPF_BIND_SUCCEEDED
) {
2152 if (status
== BPF_BIND_BUFFER_TOO_BIG
) {
2154 * The requested buffer size
2157 status
= PCAP_ERROR
;
2162 * Special checks on macOS to deal with
2163 * the way monitor mode was done on
2166 status
= check_setif_failure(p
, status
);
2171 * No buffer size was explicitly specified.
2173 * Try finding a good size for the buffer;
2174 * DEFAULT_BUFSIZE may be too big, so keep
2175 * cutting it in half until we find a size
2176 * that works, or run out of sizes to try.
2177 * If the default is larger, don't make it smaller.
2179 if ((ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) ||
2180 v
< DEFAULT_BUFSIZE
)
2181 v
= DEFAULT_BUFSIZE
;
2182 for ( ; v
!= 0; v
>>= 1) {
2184 * Ignore the return value - this is because the
2185 * call fails on BPF systems that don't have
2186 * kernel malloc. And if the call fails, it's
2187 * no big deal, we just continue to use the
2188 * standard buffer size.
2190 (void) ioctl(fd
, BIOCSBLEN
, (caddr_t
)&v
);
2192 status
= bpf_bind(fd
, p
->opt
.device
, p
->errbuf
);
2193 if (status
== BPF_BIND_SUCCEEDED
)
2194 break; /* that size worked; we're done */
2197 * If the attempt failed because the
2198 * buffer was too big, cut the buffer
2199 * size in half and try again.
2203 if (status
!= BPF_BIND_BUFFER_TOO_BIG
) {
2205 * Special checks on macOS to deal
2206 * with the way monitor mode was
2207 * done on 10.4 Tiger.
2209 status
= check_setif_failure(p
, status
);
2215 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2216 "BIOCSBLEN: %s: No buffer size worked",
2218 status
= PCAP_ERROR
;
2224 /* Get the data link layer type. */
2225 if (ioctl(fd
, BIOCGDLT
, (caddr_t
)&v
) < 0) {
2226 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2228 status
= PCAP_ERROR
;
2234 * AIX's BPF returns IFF_ types, not DLT_ types, in BIOCGDLT.
2257 * We don't know what to map this to yet.
2259 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "unknown interface type %u",
2261 status
= PCAP_ERROR
;
2265 #if _BSDI_VERSION - 0 >= 199510
2266 /* The SLIP and PPP link layer header changed in BSD/OS 2.1 */
2281 case 12: /*DLT_C_HDLC*/
2289 * We know the default link type -- now determine all the DLTs
2290 * this interface supports. If this fails with EINVAL, it's
2291 * not fatal; we just don't get to use the feature later.
2293 if (get_dlt_list(fd
, v
, &bdl
, p
->errbuf
) == -1) {
2294 status
= PCAP_ERROR
;
2297 p
->dlt_count
= bdl
.bfl_len
;
2298 p
->dlt_list
= bdl
.bfl_list
;
2302 * Monitor mode fun, continued.
2304 * For 10.5 and, we're assuming, later releases, as noted above,
2305 * 802.1 adapters that support monitor mode offer both DLT_EN10MB,
2306 * DLT_IEEE802_11, and possibly some 802.11-plus-radio-information
2307 * DLT_ value. Choosing one of the 802.11 DLT_ values will turn
2310 * Therefore, if the user asked for monitor mode, we filter out
2311 * the DLT_EN10MB value, as you can't get that in monitor mode,
2312 * and, if the user didn't ask for monitor mode, we filter out
2313 * the 802.11 DLT_ values, because selecting those will turn
2314 * monitor mode on. Then, for monitor mode, if an 802.11-plus-
2315 * radio DLT_ value is offered, we try to select that, otherwise
2316 * we try to select DLT_IEEE802_11.
2319 if (PCAP_ISDIGIT((unsigned)osinfo
.release
[0]) &&
2320 (osinfo
.release
[0] == '9' ||
2321 PCAP_ISDIGIT((unsigned)osinfo
.release
[1]))) {
2323 * 10.5 (Darwin 9.x), or later.
2325 new_dlt
= find_802_11(&bdl
);
2326 if (new_dlt
!= -1) {
2328 * We have at least one 802.11 DLT_ value,
2329 * so this is an 802.11 interface.
2330 * new_dlt is the best of the 802.11
2331 * DLT_ values in the list.
2335 * Our caller wants monitor mode.
2336 * Purge DLT_EN10MB from the list
2337 * of link-layer types, as selecting
2338 * it will keep monitor mode off.
2340 remove_non_802_11(p
);
2343 * If the new mode we want isn't
2344 * the default mode, attempt to
2345 * select the new mode.
2347 if ((u_int
)new_dlt
!= v
) {
2348 if (ioctl(p
->fd
, BIOCSDLT
,
2360 * Our caller doesn't want
2361 * monitor mode. Unless this
2362 * is being done by pcap_open_live(),
2363 * purge the 802.11 link-layer types
2364 * from the list, as selecting
2365 * one of them will turn monitor
2374 * The caller requested monitor
2375 * mode, but we have no 802.11
2376 * link-layer types, so they
2379 status
= PCAP_ERROR_RFMON_NOTSUP
;
2385 #elif defined(HAVE_BSD_IEEE80211)
2387 * *BSD with the new 802.11 ioctls.
2388 * Do we want monitor mode?
2392 * Try to put the interface into monitor mode.
2394 retv
= monitor_mode(p
, 1);
2404 * We're in monitor mode.
2405 * Try to find the best 802.11 DLT_ value and, if we
2406 * succeed, try to switch to that mode if we're not
2407 * already in that mode.
2409 new_dlt
= find_802_11(&bdl
);
2410 if (new_dlt
!= -1) {
2412 * We have at least one 802.11 DLT_ value.
2413 * new_dlt is the best of the 802.11
2414 * DLT_ values in the list.
2416 * If the new mode we want isn't the default mode,
2417 * attempt to select the new mode.
2419 if ((u_int
)new_dlt
!= v
) {
2420 if (ioctl(p
->fd
, BIOCSDLT
, &new_dlt
) != -1) {
2422 * We succeeded; make this the
2430 #endif /* various platforms */
2431 #endif /* BIOCGDLTLIST */
2434 * If this is an Ethernet device, and we don't have a DLT_ list,
2435 * give it a list with DLT_EN10MB and DLT_DOCSIS. (That'd give
2436 * 802.11 interfaces DLT_DOCSIS, which isn't the right thing to
2437 * do, but there's not much we can do about that without finding
2438 * some other way of determining whether it's an Ethernet or 802.11
2441 if (v
== DLT_EN10MB
&& p
->dlt_count
== 0) {
2442 p
->dlt_list
= (u_int
*) malloc(sizeof(u_int
) * 2);
2444 * If that fails, just leave the list empty.
2446 if (p
->dlt_list
!= NULL
) {
2447 p
->dlt_list
[0] = DLT_EN10MB
;
2448 p
->dlt_list
[1] = DLT_DOCSIS
;
2454 p
->fddipad
= PCAP_FDDIPAD
;
2460 #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
2462 * Do a BIOCSHDRCMPLT, if defined, to turn that flag on, so
2463 * the link-layer source address isn't forcibly overwritten.
2464 * (Should we ignore errors? Should we do this only if
2465 * we're open for writing?)
2467 * XXX - I seem to remember some packet-sending bug in some
2468 * BSDs - check CVS log for "bpf.c"?
2470 if (ioctl(fd
, BIOCSHDRCMPLT
, &spoof_eth_src
) == -1) {
2471 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2472 errno
, "BIOCSHDRCMPLT");
2473 status
= PCAP_ERROR
;
2478 #ifdef HAVE_ZEROCOPY_BPF
2480 * In zero-copy mode, we just use the timeout in select().
2481 * XXX - what if we're in non-blocking mode and the *application*
2482 * is using select() or poll() or kqueues or....?
2484 if (p
->opt
.timeout
&& !pb
->zerocopy
) {
2486 if (p
->opt
.timeout
) {
2489 * XXX - is this seconds/nanoseconds in AIX?
2490 * (Treating it as such doesn't fix the timeout
2491 * problem described below.)
2493 * XXX - Mac OS X 10.6 mishandles BIOCSRTIMEOUT in
2494 * 64-bit userland - it takes, as an argument, a
2495 * "struct BPF_TIMEVAL", which has 32-bit tv_sec
2496 * and tv_usec, rather than a "struct timeval".
2498 * If this platform defines "struct BPF_TIMEVAL",
2499 * we check whether the structure size in BIOCSRTIMEOUT
2500 * is that of a "struct timeval" and, if not, we use
2501 * a "struct BPF_TIMEVAL" rather than a "struct timeval".
2502 * (That way, if the bug is fixed in a future release,
2503 * we will still do the right thing.)
2506 #ifdef HAVE_STRUCT_BPF_TIMEVAL
2507 struct BPF_TIMEVAL bpf_to
;
2509 if (IOCPARM_LEN(BIOCSRTIMEOUT
) != sizeof(struct timeval
)) {
2510 bpf_to
.tv_sec
= p
->opt
.timeout
/ 1000;
2511 bpf_to
.tv_usec
= (p
->opt
.timeout
* 1000) % 1000000;
2512 if (ioctl(p
->fd
, BIOCSRTIMEOUT
, (caddr_t
)&bpf_to
) < 0) {
2513 pcap_fmt_errmsg_for_errno(p
->errbuf
,
2514 errno
, PCAP_ERRBUF_SIZE
, "BIOCSRTIMEOUT");
2515 status
= PCAP_ERROR
;
2520 to
.tv_sec
= p
->opt
.timeout
/ 1000;
2521 to
.tv_usec
= (p
->opt
.timeout
* 1000) % 1000000;
2522 if (ioctl(p
->fd
, BIOCSRTIMEOUT
, (caddr_t
)&to
) < 0) {
2523 pcap_fmt_errmsg_for_errno(p
->errbuf
,
2524 errno
, PCAP_ERRBUF_SIZE
, "BIOCSRTIMEOUT");
2525 status
= PCAP_ERROR
;
2528 #ifdef HAVE_STRUCT_BPF_TIMEVAL
2533 #ifdef BIOCIMMEDIATE
2535 * Darren Reed notes that
2537 * On AIX (4.2 at least), if BIOCIMMEDIATE is not set, the
2538 * timeout appears to be ignored and it waits until the buffer
2539 * is filled before returning. The result of not having it
2540 * set is almost worse than useless if your BPF filter
2541 * is reducing things to only a few packets (i.e. one every
2544 * so we always turn BIOCIMMEDIATE mode on if this is AIX.
2546 * For other platforms, we don't turn immediate mode on by default,
2547 * as that would mean we get woken up for every packet, which
2548 * probably isn't what you want for a packet sniffer.
2550 * We set immediate mode if the caller requested it by calling
2551 * pcap_set_immediate() before calling pcap_activate().
2554 if (p
->opt
.immediate
) {
2557 if (ioctl(p
->fd
, BIOCIMMEDIATE
, &v
) < 0) {
2558 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2559 errno
, "BIOCIMMEDIATE");
2560 status
= PCAP_ERROR
;
2566 #else /* BIOCIMMEDIATE */
2567 if (p
->opt
.immediate
) {
2569 * We don't support immediate mode. Fail.
2571 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "Immediate mode not supported");
2572 status
= PCAP_ERROR
;
2575 #endif /* BIOCIMMEDIATE */
2577 if (p
->opt
.promisc
) {
2578 /* set promiscuous mode, just warn if it fails */
2579 if (ioctl(p
->fd
, BIOCPROMISC
, NULL
) < 0) {
2580 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2581 errno
, "BIOCPROMISC");
2582 status
= PCAP_WARNING_PROMISC_NOTSUP
;
2588 if (ioctl(p
->fd
, BIOCSTSTAMP
, &v
) < 0) {
2589 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2590 errno
, "BIOCSTSTAMP");
2591 status
= PCAP_ERROR
;
2594 #endif /* BIOCSTSTAMP */
2596 if (ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) {
2597 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2598 errno
, "BIOCGBLEN");
2599 status
= PCAP_ERROR
;
2603 #ifdef HAVE_ZEROCOPY_BPF
2604 if (!pb
->zerocopy
) {
2606 p
->buffer
= malloc(p
->bufsize
);
2607 if (p
->buffer
== NULL
) {
2608 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2610 status
= PCAP_ERROR
;
2614 /* For some strange reason this seems to prevent the EFAULT
2615 * problems we have experienced from AIX BPF. */
2616 memset(p
->buffer
, 0x0, p
->bufsize
);
2618 #ifdef HAVE_ZEROCOPY_BPF
2623 * If there's no filter program installed, there's
2624 * no indication to the kernel of what the snapshot
2625 * length should be, so no snapshotting is done.
2627 * Therefore, when we open the device, we install
2628 * an "accept everything" filter with the specified
2631 total_insn
.code
= (u_short
)(BPF_RET
| BPF_K
);
2634 total_insn
.k
= p
->snapshot
;
2636 total_prog
.bf_len
= 1;
2637 total_prog
.bf_insns
= &total_insn
;
2638 if (ioctl(p
->fd
, BIOCSETF
, (caddr_t
)&total_prog
) < 0) {
2639 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
2641 status
= PCAP_ERROR
;
2646 * On most BPF platforms, either you can do a "select()" or
2647 * "poll()" on a BPF file descriptor and it works correctly,
2648 * or you can do it and it will return "readable" if the
2649 * hold buffer is full but not if the timeout expires *and*
2650 * a non-blocking read will, if the hold buffer is empty
2651 * but the store buffer isn't empty, rotate the buffers
2652 * and return what packets are available.
2654 * In the latter case, the fact that a non-blocking read
2655 * will give you the available packets means you can work
2656 * around the failure of "select()" and "poll()" to wake up
2657 * and return "readable" when the timeout expires by using
2658 * the timeout as the "select()" or "poll()" timeout, putting
2659 * the BPF descriptor into non-blocking mode, and read from
2660 * it regardless of whether "select()" reports it as readable
2663 * However, in FreeBSD 4.3 and 4.4, "select()" and "poll()"
2664 * won't wake up and return "readable" if the timer expires
2665 * and non-blocking reads return EWOULDBLOCK if the hold
2666 * buffer is empty, even if the store buffer is non-empty.
2668 * This means the workaround in question won't work.
2670 * Therefore, on FreeBSD 4.3 and 4.4, we set "p->selectable_fd"
2671 * to -1, which means "sorry, you can't use 'select()' or 'poll()'
2672 * here". On all other BPF platforms, we set it to the FD for
2673 * the BPF device; in NetBSD, OpenBSD, and Darwin, a non-blocking
2674 * read will, if the hold buffer is empty and the store buffer
2675 * isn't empty, rotate the buffers and return what packets are
2676 * there (and in sufficiently recent versions of OpenBSD
2677 * "select()" and "poll()" should work correctly).
2679 * XXX - what about AIX?
2681 p
->selectable_fd
= p
->fd
; /* assume select() works until we know otherwise */
2684 * We can check what OS this is.
2686 if (strcmp(osinfo
.sysname
, "FreeBSD") == 0) {
2687 if (strncmp(osinfo
.release
, "4.3-", 4) == 0 ||
2688 strncmp(osinfo
.release
, "4.4-", 4) == 0)
2689 p
->selectable_fd
= -1;
2693 p
->read_op
= pcap_read_bpf
;
2694 p
->inject_op
= pcap_inject_bpf
;
2695 p
->setfilter_op
= pcap_setfilter_bpf
;
2696 p
->setdirection_op
= pcap_setdirection_bpf
;
2697 p
->set_datalink_op
= pcap_set_datalink_bpf
;
2698 p
->getnonblock_op
= pcap_getnonblock_bpf
;
2699 p
->setnonblock_op
= pcap_setnonblock_bpf
;
2700 p
->stats_op
= pcap_stats_bpf
;
2701 p
->cleanup_op
= pcap_cleanup_bpf
;
2705 pcap_cleanup_bpf(p
);
2710 * Not all interfaces can be bound to by BPF, so try to bind to
2711 * the specified interface; return 0 if we fail with
2712 * PCAP_ERROR_NO_SUCH_DEVICE (which means we got an ENXIO when we tried
2713 * to bind, which means this interface isn't in the list of interfaces
2714 * attached to BPF) and 1 otherwise.
2717 check_bpf_bindable(const char *name
)
2720 char errbuf
[PCAP_ERRBUF_SIZE
];
2723 * On macOS, we don't do this check if the device name begins
2724 * with "wlt"; at least some versions of macOS (actually, it
2725 * was called "Mac OS X" then...) offer monitor mode capturing
2726 * by having a separate "monitor mode" device for each wireless
2727 * adapter, rather than by implementing the ioctls that
2728 * {Free,Net,Open,DragonFly}BSD provide. Opening that device
2729 * puts the adapter into monitor mode, which, at least for
2730 * some adapters, causes them to deassociate from the network
2731 * with which they're associated.
2733 * Instead, we try to open the corresponding "en" device (so
2734 * that we don't end up with, for users without sufficient
2735 * privilege to open capture devices, a list of adapters that
2736 * only includes the wlt devices).
2739 if (strncmp(name
, "wlt", 3) == 0) {
2744 * Try to allocate a buffer for the "en"
2747 en_name_len
= strlen(name
) - 1;
2748 en_name
= malloc(en_name_len
+ 1);
2749 if (en_name
== NULL
) {
2750 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
,
2754 strcpy(en_name
, "en");
2755 strcat(en_name
, name
+ 3);
2756 fd
= bpf_open_and_bind(en_name
, errbuf
);
2759 #endif /* __APPLE */
2760 fd
= bpf_open_and_bind(name
, errbuf
);
2763 * Error - was it PCAP_ERROR_NO_SUCH_DEVICE?
2765 if (fd
== PCAP_ERROR_NO_SUCH_DEVICE
) {
2767 * Yes, so we can't bind to this because it's
2768 * not something supported by BPF.
2773 * No, so we don't know whether it's supported or not;
2774 * say it is, so that the user can at least try to
2775 * open it and report the error (which is probably
2776 * "you don't have permission to open BPF devices";
2777 * reporting those interfaces means users will ask
2778 * "why am I getting a permissions error when I try
2779 * to capture" rather than "why am I not seeing any
2780 * interfaces", making the underlying problem clearer).
2792 #if defined(__FreeBSD__) && defined(SIOCIFCREATE2)
2794 get_usb_if_flags(const char *name _U_
, bpf_u_int32
*flags _U_
, char *errbuf _U_
)
2797 * XXX - if there's a way to determine whether there's something
2798 * plugged into a given USB bus, use that to determine whether
2799 * this device is "connected" or not.
2805 finddevs_usb(pcap_if_list_t
*devlistp
, char *errbuf
)
2808 struct dirent
*usbitem
;
2813 * We might have USB sniffing support, so try looking for USB
2816 * We want to report a usbusN device for each USB bus, but
2817 * usbusN interfaces might, or might not, exist for them -
2818 * we create one if there isn't already one.
2820 * So, instead, we look in /dev/usb for all buses and create
2821 * a "usbusN" device for each one.
2823 usbdir
= opendir("/dev/usb");
2824 if (usbdir
== NULL
) {
2832 * Leave enough room for a 32-bit (10-digit) bus number.
2833 * Yes, that's overkill, but we won't be using
2834 * the buffer very long.
2836 name_max
= USBUS_PREFIX_LEN
+ 10 + 1;
2837 name
= malloc(name_max
);
2842 while ((usbitem
= readdir(usbdir
)) != NULL
) {
2846 if (strcmp(usbitem
->d_name
, ".") == 0 ||
2847 strcmp(usbitem
->d_name
, "..") == 0) {
2853 p
= strchr(usbitem
->d_name
, '.');
2856 busnumlen
= p
- usbitem
->d_name
;
2857 memcpy(name
, usbus_prefix
, USBUS_PREFIX_LEN
);
2858 memcpy(name
+ USBUS_PREFIX_LEN
, usbitem
->d_name
, busnumlen
);
2859 *(name
+ USBUS_PREFIX_LEN
+ busnumlen
) = '\0';
2861 * There's an entry in this directory for every USB device,
2862 * not for every bus; if there's more than one device on
2863 * the bus, there'll be more than one entry for that bus,
2864 * so we need to avoid adding multiple capture devices
2867 if (find_or_add_dev(devlistp
, name
, PCAP_IF_UP
,
2868 get_usb_if_flags
, NULL
, errbuf
) == NULL
) {
2871 return (PCAP_ERROR
);
2881 * Get additional flags for a device, using SIOCGIFMEDIA.
2885 get_if_flags(const char *name
, bpf_u_int32
*flags
, char *errbuf
)
2888 struct ifmediareq req
;
2890 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
2892 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
, errno
,
2893 "Can't create socket to get media information for %s",
2897 memset(&req
, 0, sizeof(req
));
2898 pcap_strlcpy(req
.ifm_name
, name
, sizeof(req
.ifm_name
));
2899 if (ioctl(sock
, SIOCGIFMEDIA
, &req
) < 0) {
2900 if (errno
== EOPNOTSUPP
|| errno
== EINVAL
|| errno
== ENOTTY
||
2901 errno
== ENODEV
|| errno
== EPERM
2907 * Not supported, so we can't provide any
2908 * additional information. Assume that
2909 * this means that "connected" vs.
2910 * "disconnected" doesn't apply.
2912 * The ioctl routine for Apple's pktap devices,
2913 * annoyingly, checks for "are you root?" before
2914 * checking whether the ioctl is valid, so it
2915 * returns EPERM, rather than ENOTSUP, for the
2916 * invalid SIOCGIFMEDIA, unless you're root.
2917 * So, just as we do for some ethtool ioctls
2918 * on Linux, which makes the same mistake, we
2919 * also treat EPERM as meaning "not supported".
2921 * And it appears that Apple's llw0 device, which
2922 * appears to be part of the Skywalk subsystem:
2924 * http://newosxbook.com/bonus/vol1ch16.html
2926 * can sometimes return EPWROFF ("Device power
2927 * is off") for that ioctl, so we treat *that*
2928 * as another indication that we can't get a
2929 * connection status. (If it *isn't* "powered
2930 * off", it's reported as a wireless device,
2931 * complete with an active/inactive state.)
2933 *flags
|= PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE
;
2937 pcap_fmt_errmsg_for_errno(errbuf
, PCAP_ERRBUF_SIZE
, errno
,
2938 "SIOCGIFMEDIA on %s failed", name
);
2945 * OK, what type of network is this?
2947 switch (IFM_TYPE(req
.ifm_active
)) {
2953 *flags
|= PCAP_IF_WIRELESS
;
2958 * Do we know whether it's connected?
2960 if (req
.ifm_status
& IFM_AVALID
) {
2964 if (req
.ifm_status
& IFM_ACTIVE
) {
2968 *flags
|= PCAP_IF_CONNECTION_STATUS_CONNECTED
;
2971 * It's disconnected.
2973 *flags
|= PCAP_IF_CONNECTION_STATUS_DISCONNECTED
;
2980 get_if_flags(const char *name _U_
, bpf_u_int32
*flags
, char *errbuf _U_
)
2983 * Nothing we can do other than mark loopback devices as "the
2984 * connected/disconnected status doesn't apply".
2986 * XXX - on Solaris, can we do what the dladm command does,
2987 * i.e. get a connected/disconnected indication from a kstat?
2988 * (Note that you can also get the link speed, and possibly
2989 * other information, from a kstat as well.)
2991 if (*flags
& PCAP_IF_LOOPBACK
) {
2993 * Loopback devices aren't wireless, and "connected"/
2994 * "disconnected" doesn't apply to them.
2996 *flags
|= PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE
;
3004 pcap_platform_finddevs(pcap_if_list_t
*devlistp
, char *errbuf
)
3007 * Get the list of regular interfaces first.
3009 if (pcap_findalldevs_interfaces(devlistp
, errbuf
, check_bpf_bindable
,
3010 get_if_flags
) == -1)
3011 return (-1); /* failure */
3013 #if defined(__FreeBSD__) && defined(SIOCIFCREATE2)
3014 if (finddevs_usb(devlistp
, errbuf
) == -1)
3021 #ifdef HAVE_BSD_IEEE80211
3023 monitor_mode(pcap_t
*p
, int set
)
3025 struct pcap_bpf
*pb
= p
->priv
;
3027 struct ifmediareq req
;
3028 IFM_ULIST_TYPE
*media_list
;
3033 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
3035 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
3036 errno
, "can't open socket");
3037 return (PCAP_ERROR
);
3040 memset(&req
, 0, sizeof req
);
3041 pcap_strlcpy(req
.ifm_name
, p
->opt
.device
, sizeof req
.ifm_name
);
3044 * Find out how many media types we have.
3046 if (ioctl(sock
, SIOCGIFMEDIA
, &req
) < 0) {
3048 * Can't get the media types.
3054 * There's no such device.
3057 return (PCAP_ERROR_NO_SUCH_DEVICE
);
3061 * Interface doesn't support SIOC{G,S}IFMEDIA.
3064 return (PCAP_ERROR_RFMON_NOTSUP
);
3067 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
3068 errno
, "SIOCGIFMEDIA");
3070 return (PCAP_ERROR
);
3073 if (req
.ifm_count
== 0) {
3078 return (PCAP_ERROR_RFMON_NOTSUP
);
3082 * Allocate a buffer to hold all the media types, and
3083 * get the media types.
3085 media_list
= malloc(req
.ifm_count
* sizeof(*media_list
));
3086 if (media_list
== NULL
) {
3087 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
3090 return (PCAP_ERROR
);
3092 req
.ifm_ulist
= media_list
;
3093 if (ioctl(sock
, SIOCGIFMEDIA
, &req
) < 0) {
3094 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
3095 errno
, "SIOCGIFMEDIA");
3098 return (PCAP_ERROR
);
3102 * Look for an 802.11 "automatic" media type.
3103 * We assume that all 802.11 adapters have that media type,
3104 * and that it will carry the monitor mode supported flag.
3107 for (i
= 0; i
< req
.ifm_count
; i
++) {
3108 if (IFM_TYPE(media_list
[i
]) == IFM_IEEE80211
3109 && IFM_SUBTYPE(media_list
[i
]) == IFM_AUTO
) {
3110 /* OK, does it do monitor mode? */
3111 if (media_list
[i
] & IFM_IEEE80211_MONITOR
) {
3120 * This adapter doesn't support monitor mode.
3123 return (PCAP_ERROR_RFMON_NOTSUP
);
3128 * Don't just check whether we can enable monitor mode,
3129 * do so, if it's not already enabled.
3131 if ((req
.ifm_current
& IFM_IEEE80211_MONITOR
) == 0) {
3133 * Monitor mode isn't currently on, so turn it on,
3134 * and remember that we should turn it off when the
3139 * If we haven't already done so, arrange to have
3140 * "pcap_close_all()" called when we exit.
3142 if (!pcap_do_addexit(p
)) {
3144 * "atexit()" failed; don't put the interface
3145 * in monitor mode, just give up.
3148 return (PCAP_ERROR
);
3150 memset(&ifr
, 0, sizeof(ifr
));
3151 (void)pcap_strlcpy(ifr
.ifr_name
, p
->opt
.device
,
3152 sizeof(ifr
.ifr_name
));
3153 ifr
.ifr_media
= req
.ifm_current
| IFM_IEEE80211_MONITOR
;
3154 if (ioctl(sock
, SIOCSIFMEDIA
, &ifr
) == -1) {
3155 pcap_fmt_errmsg_for_errno(p
->errbuf
,
3156 PCAP_ERRBUF_SIZE
, errno
, "SIOCSIFMEDIA");
3158 return (PCAP_ERROR
);
3161 pb
->must_do_on_close
|= MUST_CLEAR_RFMON
;
3164 * Add this to the list of pcaps to close when we exit.
3166 pcap_add_to_pcaps_to_close(p
);
3171 #endif /* HAVE_BSD_IEEE80211 */
3173 #if defined(BIOCGDLTLIST) && (defined(__APPLE__) || defined(HAVE_BSD_IEEE80211))
3175 * Check whether we have any 802.11 link-layer types; return the best
3176 * of the 802.11 link-layer types if we find one, and return -1
3179 * DLT_IEEE802_11_RADIO, with the radiotap header, is considered the
3180 * best 802.11 link-layer type; any of the other 802.11-plus-radio
3181 * headers are second-best; 802.11 with no radio information is
3185 find_802_11(struct bpf_dltlist
*bdlp
)
3191 * Scan the list of DLT_ values, looking for 802.11 values,
3192 * and, if we find any, choose the best of them.
3195 for (i
= 0; i
< bdlp
->bfl_len
; i
++) {
3196 switch (bdlp
->bfl_list
[i
]) {
3198 case DLT_IEEE802_11
:
3200 * 802.11, but no radio.
3202 * Offer this, and select it as the new mode
3203 * unless we've already found an 802.11
3204 * header with radio information.
3207 new_dlt
= bdlp
->bfl_list
[i
];
3210 #ifdef DLT_PRISM_HEADER
3211 case DLT_PRISM_HEADER
:
3213 #ifdef DLT_AIRONET_HEADER
3214 case DLT_AIRONET_HEADER
:
3216 case DLT_IEEE802_11_RADIO_AVS
:
3218 * 802.11 with radio, but not radiotap.
3220 * Offer this, and select it as the new mode
3221 * unless we've already found the radiotap DLT_.
3223 if (new_dlt
!= DLT_IEEE802_11_RADIO
)
3224 new_dlt
= bdlp
->bfl_list
[i
];
3227 case DLT_IEEE802_11_RADIO
:
3229 * 802.11 with radiotap.
3231 * Offer this, and select it as the new mode.
3233 new_dlt
= bdlp
->bfl_list
[i
];
3246 #endif /* defined(BIOCGDLTLIST) && (defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)) */
3248 #if defined(__APPLE__) && defined(BIOCGDLTLIST)
3250 * Remove non-802.11 header types from the list of DLT_ values, as we're in
3251 * monitor mode, and those header types aren't supported in monitor mode.
3254 remove_non_802_11(pcap_t
*p
)
3259 * Scan the list of DLT_ values and discard non-802.11 ones.
3262 for (i
= 0; i
< p
->dlt_count
; i
++) {
3263 switch (p
->dlt_list
[i
]) {
3268 * Not 802.11. Don't offer this one.
3274 * Just copy this mode over.
3280 * Copy this DLT_ value to its new position.
3282 p
->dlt_list
[j
] = p
->dlt_list
[i
];
3287 * Set the DLT_ count to the number of entries we copied.
3293 * Remove 802.11 link-layer types from the list of DLT_ values, as
3294 * we're not in monitor mode, and those DLT_ values will switch us
3298 remove_802_11(pcap_t
*p
)
3303 * Scan the list of DLT_ values and discard 802.11 values.
3306 for (i
= 0; i
< p
->dlt_count
; i
++) {
3307 switch (p
->dlt_list
[i
]) {
3309 case DLT_IEEE802_11
:
3310 #ifdef DLT_PRISM_HEADER
3311 case DLT_PRISM_HEADER
:
3313 #ifdef DLT_AIRONET_HEADER
3314 case DLT_AIRONET_HEADER
:
3316 case DLT_IEEE802_11_RADIO
:
3317 case DLT_IEEE802_11_RADIO_AVS
:
3322 * 802.11. Don't offer this one.
3328 * Just copy this mode over.
3334 * Copy this DLT_ value to its new position.
3336 p
->dlt_list
[j
] = p
->dlt_list
[i
];
3341 * Set the DLT_ count to the number of entries we copied.
3345 #endif /* defined(__APPLE__) && defined(BIOCGDLTLIST) */
3348 pcap_setfilter_bpf(pcap_t
*p
, struct bpf_program
*fp
)
3350 struct pcap_bpf
*pb
= p
->priv
;
3353 * Free any user-mode filter we might happen to have installed.
3355 pcap_freecode(&p
->fcode
);
3358 * Try to install the kernel filter.
3360 if (ioctl(p
->fd
, BIOCSETF
, (caddr_t
)fp
) == 0) {
3364 pb
->filtering_in_kernel
= 1; /* filtering in the kernel */
3367 * Discard any previously-received packets, as they might
3368 * have passed whatever filter was formerly in effect, but
3369 * might not pass this filter (BIOCSETF discards packets
3370 * buffered in the kernel, so you can lose packets in any
3380 * If it failed with EINVAL, that's probably because the program
3381 * is invalid or too big. Validate it ourselves; if we like it
3382 * (we currently allow backward branches, to support protochain),
3383 * run it in userland. (There's no notion of "too big" for
3386 * Otherwise, just give up.
3387 * XXX - if the copy of the program into the kernel failed,
3388 * we will get EINVAL rather than, say, EFAULT on at least
3391 if (errno
!= EINVAL
) {
3392 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
3398 * install_bpf_program() validates the program.
3400 * XXX - what if we already have a filter in the kernel?
3402 if (install_bpf_program(p
, fp
) < 0)
3404 pb
->filtering_in_kernel
= 0; /* filtering in userland */
3409 * Set direction flag: Which packets do we accept on a forwarding
3410 * single device? IN, OUT or both?
3412 #if defined(BIOCSDIRECTION)
3414 pcap_setdirection_bpf(pcap_t
*p
, pcap_direction_t d
)
3417 const char *direction_name
;
3420 * FreeBSD and NetBSD.
3426 * Incoming, but not outgoing, so accept only
3429 direction
= BPF_D_IN
;
3430 direction_name
= "\"incoming only\"";
3435 * Outgoing, but not incoming, so accept only
3438 direction
= BPF_D_OUT
;
3439 direction_name
= "\"outgoing only\"";
3444 * Incoming and outgoing, so accept both
3445 * incoming and outgoing packets.
3447 * It's guaranteed, at this point, that d is a valid
3448 * direction value, so we know that this is PCAP_D_INOUT
3449 * if it's not PCAP_D_IN or PCAP_D_OUT.
3451 direction
= BPF_D_INOUT
;
3452 direction_name
= "\"incoming and outgoing\"";
3456 if (ioctl(p
->fd
, BIOCSDIRECTION
, &direction
) == -1) {
3457 pcap_fmt_errmsg_for_errno(p
->errbuf
, sizeof(p
->errbuf
),
3458 errno
, "Cannot set direction to %s", direction_name
);
3463 #elif defined(BIOCSDIRFILT)
3465 pcap_setdirection_bpf(pcap_t
*p
, pcap_direction_t d
)
3468 const char *direction_name
;
3471 * OpenBSD; same functionality, different names, different
3472 * semantics (the flags mean "*don't* capture packets in
3473 * that direction", not "*capture only* packets in that
3480 * Incoming, but not outgoing, so filter out
3483 dirfilt
= BPF_DIRECTION_OUT
;
3484 direction_name
= "\"incoming only\"";
3489 * Outgoing, but not incoming, so filter out
3492 dirfilt
= BPF_DIRECTION_IN
;
3493 direction_name
= "\"outgoing only\"";
3498 * Incoming and outgoing, so don't filter out
3499 * any packets based on direction.
3501 * It's guaranteed, at this point, that d is a valid
3502 * direction value, so we know that this is PCAP_D_INOUT
3503 * if it's not PCAP_D_IN or PCAP_D_OUT.
3506 direction_name
= "\"incoming and outgoing\"";
3509 if (ioctl(p
->fd
, BIOCSDIRFILT
, &dirfilt
) == -1) {
3510 pcap_fmt_errmsg_for_errno(p
->errbuf
, sizeof(p
->errbuf
),
3511 errno
, "Cannot set direction to %s", direction_name
);
3516 #elif defined(BIOCSSEESENT)
3518 pcap_setdirection_bpf(pcap_t
*p
, pcap_direction_t d
)
3521 const char *direction_name
;
3524 * OS with just BIOCSSEESENT.
3530 * Incoming, but not outgoing, so we don't want to
3531 * see transmitted packets.
3534 direction_name
= "\"incoming only\"";
3539 * Outgoing, but not incoming; we can't specify that.
3541 snprintf(p
->errbuf
, sizeof(p
->errbuf
),
3542 "Setting direction to \"outgoing only\" is not supported on this device");
3547 * Incoming and outgoing, so we want to see transmitted
3550 * It's guaranteed, at this point, that d is a valid
3551 * direction value, so we know that this is PCAP_D_INOUT
3552 * if it's not PCAP_D_IN or PCAP_D_OUT.
3555 direction_name
= "\"incoming and outgoing\"";
3559 if (ioctl(p
->fd
, BIOCSSEESENT
, &seesent
) == -1) {
3560 pcap_fmt_errmsg_for_errno(p
->errbuf
, sizeof(p
->errbuf
),
3561 errno
, "Cannot set direction to %s", direction_name
);
3568 pcap_setdirection_bpf(pcap_t
*p
, pcap_direction_t d _U_
)
3570 (void) snprintf(p
->errbuf
, sizeof(p
->errbuf
),
3571 "Setting direction is not supported on this device");
3578 pcap_set_datalink_bpf(pcap_t
*p
, int dlt
)
3580 if (ioctl(p
->fd
, BIOCSDLT
, &dlt
) == -1) {
3581 pcap_fmt_errmsg_for_errno(p
->errbuf
, sizeof(p
->errbuf
),
3582 errno
, "Cannot set DLT %d", dlt
);
3589 pcap_set_datalink_bpf(pcap_t
*p _U_
, int dlt _U_
)
3596 * Platform-specific information.
3599 pcap_lib_version(void)
3601 #ifdef HAVE_ZEROCOPY_BPF
3602 return (PCAP_VERSION_STRING
" (with zerocopy support)");
3604 return (PCAP_VERSION_STRING
);