2 * Utility routines for packet capture
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * SPDX-License-Identifier: GPL-2.0-or-later
12 #define WS_LOG_DOMAIN LOG_DOMAIN_CAPCHILD
16 #include <wireshark.h>
23 #include <sys/types.h>
25 #ifdef HAVE_SYS_SOCKET_H
26 #include <sys/socket.h>
33 #include "ws_attributes.h"
36 * Linux bonding devices mishandle unknown ioctls; they fail
37 * with ENODEV rather than ENOTSUP, EOPNOTSUPP, or ENOTTY,
38 * so pcap_can_set_rfmon() returns a "no such device" indication
39 * if we try to do SIOCGIWMODE on them.
41 * So, on Linux, we check for bonding devices, if we can, before
42 * trying pcap_can_set_rfmon(), as pcap_can_set_rfmon() will
43 * end up trying SIOCGIWMODE on the device if that ioctl exists.
45 #if defined(HAVE_PCAP_CREATE) && defined(__linux__)
47 #include <sys/ioctl.h>
50 * If we're building for a Linux version that supports bonding,
51 * HAVE_BONDING will be defined.
54 #ifdef HAVE_LINUX_SOCKIOS_H
55 #include <linux/sockios.h>
58 #ifdef HAVE_LINUX_IF_BONDING_H
59 #include <linux/if_bonding.h>
62 #if defined(BOND_INFO_QUERY_OLD) || defined(SIOCBONDINFOQUERY)
66 #endif /* defined(HAVE_PCAP_CREATE) && defined(__linux__) */
68 #include "capture/capture_ifinfo.h"
69 #include "capture/capture-pcap-util.h"
70 #include "capture/capture-pcap-util-int.h"
72 #include "capture/capture-wpcap.h"
74 #define ws_pcap_findalldevs_ex pcap_findalldevs_ex
77 #include <wsutil/file_util.h>
78 #include <wsutil/please_report_bug.h>
79 #include <wsutil/wslog.h>
82 #include <netinet/in.h>
88 #include "capture/capture_win_ifnames.h" /* windows friendly interface names */
91 #if defined(__FreeBSD__) || defined(__OpenBSD__)
93 * Needed for the code to get a device description.
97 #include <sys/sockio.h>
98 #include <sys/ioctl.h>
102 * Given an interface name, find the "friendly name" and interface
103 * type for the interface.
106 #if defined(HAVE_MACOS_FRAMEWORKS)
108 #include <CoreFoundation/CoreFoundation.h>
109 #include <SystemConfiguration/SystemConfiguration.h>
111 #include <wsutil/cfutils.h>
114 * On macOS, we get the "friendly name" and interface type for the interface
115 * from the System Configuration framework.
117 * To find the System Configuration framework information for the
118 * interface, we get all the interfaces that the System Configuration
119 * framework knows about and look for the one with a "BSD name" matching
120 * the interface name.
122 * If we find it, we use its "localized display name", if it has one, as
123 * the "friendly name".
125 * As for the interface type:
127 * Yes, fetching all the network addresses for an interface gets you an
128 * AF_LINK address, of type "struct sockaddr_dl", and, yes, that includes
129 * an SNMP MIB-II ifType value.
131 * However, it's IFT_ETHER, i.e. Ethernet, for AirPort interfaces,
132 * not IFT_IEEE80211 (which isn't defined in macOS in any case).
134 * Perhaps some other BSD-flavored OSes won't make this mistake;
135 * however, FreeBSD 7.0 and OpenBSD 4.2, at least, appear to have
136 * made the same mistake, at least for my Belkin ZyDAS stick.
138 * SCNetworkInterfaceGetInterfaceType() will get the interface
139 * type. The interface type is a CFString, and:
141 * kSCNetworkInterfaceTypeIEEE80211 means IF_WIRELESS;
142 * kSCNetworkInterfaceTypeBluetooth means IF_BLUETOOTH;
143 * kSCNetworkInterfaceTypeModem or
144 * kSCNetworkInterfaceTypePPP or
145 * maybe kSCNetworkInterfaceTypeWWAN means IF_DIALUP
148 add_unix_interface_ifinfo(if_info_t
*if_info
, const char *name
,
149 const char *description _U_
)
151 CFStringRef name_CFString
;
152 CFArrayRef interfaces
;
153 CFIndex num_interfaces
;
155 SCNetworkInterfaceRef interface
;
156 CFStringRef bsdname_CFString
;
157 CFStringRef friendly_name_CFString
;
158 CFStringRef interface_type_CFString
;
160 interfaces
= SCNetworkInterfaceCopyAll();
161 if (interfaces
== NULL
) {
163 * Couldn't get a list of interfaces.
168 name_CFString
= CFStringCreateWithCString(kCFAllocatorDefault
,
169 name
, kCFStringEncodingUTF8
);
170 if (name_CFString
== NULL
) {
172 * Couldn't convert the interface name to a CFString.
174 CFRelease(interfaces
);
178 num_interfaces
= CFArrayGetCount(interfaces
);
179 for (i
= 0; i
< num_interfaces
; i
++) {
180 interface
= (SCNetworkInterfaceRef
)CFArrayGetValueAtIndex(interfaces
, i
);
181 bsdname_CFString
= SCNetworkInterfaceGetBSDName(interface
);
182 if (bsdname_CFString
== NULL
) {
184 * This interface has no BSD name, so it's not
185 * a regular network interface.
189 if (CFStringCompare(name_CFString
, bsdname_CFString
, 0) == 0) {
191 * This is the interface.
192 * First, get the friendly name.
194 friendly_name_CFString
= SCNetworkInterfaceGetLocalizedDisplayName(interface
);
195 if (friendly_name_CFString
!= NULL
)
196 if_info
->friendly_name
= CFString_to_C_string(friendly_name_CFString
);
199 * Now get the interface type.
201 interface_type_CFString
= SCNetworkInterfaceGetInterfaceType(interface
);
202 if (CFStringCompare(interface_type_CFString
,
203 kSCNetworkInterfaceTypeIEEE80211
, 0) == kCFCompareEqualTo
)
204 if_info
->type
= IF_WIRELESS
;
205 else if (CFStringCompare(interface_type_CFString
,
206 kSCNetworkInterfaceTypeBluetooth
, 0) == kCFCompareEqualTo
)
207 if_info
->type
= IF_BLUETOOTH
;
208 else if (CFStringCompare(interface_type_CFString
,
209 kSCNetworkInterfaceTypeModem
, 0) == kCFCompareEqualTo
)
210 if_info
->type
= IF_DIALUP
;
211 else if (CFStringCompare(interface_type_CFString
,
212 kSCNetworkInterfaceTypePPP
, 0) == kCFCompareEqualTo
)
213 if_info
->type
= IF_DIALUP
;
214 else if (CFStringCompare(interface_type_CFString
,
215 kSCNetworkInterfaceTypeWWAN
, 0) == kCFCompareEqualTo
)
216 if_info
->type
= IF_DIALUP
;
218 if_info
->type
= IF_WIRED
;
223 CFRelease(interfaces
);
224 CFRelease(name_CFString
);
226 #elif defined(__linux__)
228 * Linux doesn't offer any form of "friendly name", but you can
229 * determine an interface type to some degree.
232 add_unix_interface_ifinfo(if_info_t
*if_info
, const char *name
,
233 const char *description _U_
)
239 * Look for /sys/class/net/{device}/wireless. If it exists,
240 * it's a wireless interface.
242 wireless_path
= ws_strdup_printf("/sys/class/net/%s/wireless", name
);
243 if (wireless_path
!= NULL
) {
244 if (ws_stat64(wireless_path
, &statb
) == 0)
245 if_info
->type
= IF_WIRELESS
;
246 g_free(wireless_path
);
248 if (if_info
->type
== IF_WIRED
) {
250 * We still don't know what it is. Check for
251 * Bluetooth and USB devices.
253 if (strstr(name
, "bluetooth") != NULL
) {
255 * XXX - this is for raw Bluetooth capture; what
256 * about IP-over-Bluetooth devices?
258 if_info
->type
= IF_BLUETOOTH
;
259 } else if (strstr(name
, "usbmon") != NULL
)
260 if_info
->type
= IF_USB
;
263 #elif !defined(_WIN32)
265 * On other UN*Xes, if there is a description, it's a friendly
266 * name, and there is no vendor description. ("Other UN*Xes"
267 * currently means "FreeBSD and OpenBSD".)
270 add_unix_interface_ifinfo(if_info_t
*if_info
, const char *name _U_
,
271 const char *description
)
273 if_info
->friendly_name
= g_strdup(description
);
278 if_info_get(const char *name
)
280 char *description
= NULL
;
284 * Try to fetch the description of this interface.
285 * XXX - this is only here because libpcap has no API to
286 * get the description of a *single* interface; it really
287 * needs both an API to get pcapng-IDB-style attributes
288 * for a single interface and to get a list of interfaces
289 * with pcapng-IDB-style attributes for each interface.
292 struct ifreq ifrdesc
;
294 size_t descrlen
= 64;
296 size_t descrlen
= IFDESCRSIZE
;
297 #endif /* IFDESCRSIZE */
300 * Get the description for the interface.
302 memset(&ifrdesc
, 0, sizeof ifrdesc
);
303 (void) g_strlcpy(ifrdesc
.ifr_name
, name
, sizeof ifrdesc
.ifr_name
);
304 s
= socket(AF_INET
, SOCK_DGRAM
, 0);
308 * On FreeBSD, if the buffer isn't big enough for the
309 * description, the ioctl succeeds, but the description
310 * isn't copied, ifr_buffer.length is set to the description
311 * length, and ifr_buffer.buffer is set to NULL.
315 if ((description
= (char*)g_malloc(descrlen
)) != NULL
) {
316 ifrdesc
.ifr_buffer
.buffer
= description
;
317 ifrdesc
.ifr_buffer
.length
= descrlen
;
318 if (ioctl(s
, SIOCGIFDESCR
, &ifrdesc
) == 0) {
319 if (ifrdesc
.ifr_buffer
.buffer
==
323 descrlen
= ifrdesc
.ifr_buffer
.length
;
326 * Failed to get interface description.
335 #else /* __FreeBSD__ */
337 * The only other OS that currently supports
338 * SIOCGIFDESCR is OpenBSD, and it has no way
339 * to get the description length - it's clamped
340 * to a maximum of IFDESCRSIZE.
342 if ((description
= (char*)g_malloc(descrlen
)) != NULL
) {
343 ifrdesc
.ifr_data
= (caddr_t
)description
;
344 if (ioctl(s
, SIOCGIFDESCR
, &ifrdesc
) != 0) {
346 * Failed to get interface description.
352 #endif /* __FreeBSD__ */
354 if (description
!= NULL
&& strlen(description
) == 0) {
356 * Description is empty, so discard it.
365 * For FreeBSD, if we didn't get a description, and this is
366 * a device with a name of the form usbusN, label it as a USB
369 if (description
== NULL
) {
370 if (strncmp(name
, "usbus", 5) == 0) {
372 * OK, it begins with "usbus".
378 busnum
= strtol(name
+ 5, &p
, 10);
379 if (errno
== 0 && p
!= name
+ 5 && *p
== '\0' &&
380 busnum
>= 0 && busnum
<= INT_MAX
) {
382 * OK, it's a valid number that's not
383 * bigger than INT_MAX. Construct
384 * a description from it.
386 static const char descr_prefix
[] = "USB bus number ";
390 * Allow enough room for a 32-bit bus number.
391 * sizeof (descr_prefix) includes the
394 descr_size
= sizeof (descr_prefix
) + 10;
395 description
= g_malloc(descr_size
);
396 if (description
!= NULL
) {
397 snprintf(description
, descr_size
,
398 "%s%ld", descr_prefix
, busnum
);
403 #endif /* __FreeBSD__ */
404 #endif /* SIOCGIFDESCR */
405 if_info
= if_info_new(name
, description
, false);
411 if_addr_copy(const if_addr_t
*addr
)
413 if_addr_t
*new_addr
= g_new(if_addr_t
, 1);
414 new_addr
->ifat_type
= addr
->ifat_type
;
415 switch (addr
->ifat_type
) {
417 new_addr
->addr
.ip4_addr
= addr
->addr
.ip4_addr
;
420 memcpy(new_addr
->addr
.ip6_addr
, addr
->addr
.ip6_addr
, sizeof(addr
->addr
));
423 /* In case we add non-IP addresses */
430 if_addr_copy_cb(const void *data
, void *user_data _U_
)
432 return if_addr_copy((if_addr_t
*)data
);
436 if_info_free(if_info_t
*if_info
)
438 if (if_info
== NULL
) {
441 g_free(if_info
->name
);
442 g_free(if_info
->friendly_name
);
443 g_free(if_info
->vendor_description
);
444 g_free(if_info
->extcap
);
445 g_slist_free_full(if_info
->addrs
, g_free
);
447 free_if_capabilities(if_info
->caps
);
453 copy_linktype_cb(const void *data
, void *user_data _U_
)
455 data_link_info_t
*linktype_info
= (data_link_info_t
*)data
;
457 data_link_info_t
*ret
= g_new(data_link_info_t
, 1);
458 ret
->dlt
= linktype_info
->dlt
;
459 ret
->name
= g_strdup(linktype_info
->name
);
460 ret
->description
= g_strdup(linktype_info
->description
);
465 copy_timestamp_cb(const void *data
, void *user_data _U_
)
467 timestamp_info_t
*timestamp_info
= (timestamp_info_t
*)data
;
469 timestamp_info_t
*ret
= g_new(timestamp_info_t
, 1);
470 ret
->name
= g_strdup(timestamp_info
->name
);
471 ret
->description
= g_strdup(timestamp_info
->description
);
475 static if_capabilities_t
*
476 if_capabilities_copy(const if_capabilities_t
*caps
)
478 if (caps
== NULL
) return NULL
;
480 if_capabilities_t
*ret
= g_new(if_capabilities_t
, 1);
481 ret
->can_set_rfmon
= caps
->can_set_rfmon
;
482 ret
->data_link_types
= g_list_copy_deep(caps
->data_link_types
, copy_linktype_cb
, NULL
);
483 ret
->timestamp_types
= g_list_copy_deep(caps
->timestamp_types
, copy_timestamp_cb
, NULL
);
484 ret
->data_link_types_rfmon
= g_list_copy_deep(caps
->data_link_types_rfmon
, copy_linktype_cb
, NULL
);
485 ret
->primary_msg
= g_strdup(caps
->primary_msg
);
486 ret
->secondary_msg
= caps
->secondary_msg
;
492 if_info_copy(const if_info_t
*if_info
)
494 if_info_t
*new_if_info
;
495 new_if_info
= g_new(if_info_t
, 1);
496 new_if_info
->name
= g_strdup(if_info
->name
);
497 /* g_strdup accepts NULL as input and returns NULL. */
498 new_if_info
->friendly_name
= g_strdup(if_info
->friendly_name
);
499 new_if_info
->vendor_description
= g_strdup(if_info
->vendor_description
);
500 new_if_info
->addrs
= g_slist_copy_deep(if_info
->addrs
, if_addr_copy_cb
, NULL
);
501 new_if_info
->type
= if_info
->type
;
502 new_if_info
->loopback
= if_info
->loopback
;
503 new_if_info
->extcap
= g_strdup(if_info
->extcap
);
504 new_if_info
->caps
= if_capabilities_copy(if_info
->caps
);
510 if_info_copy_cb(const void* data
, void *user_data _U_
)
512 return if_info_copy((const if_info_t
*)data
);
516 if_info_new(const char *name
, const char *description
, bool loopback
)
520 const char *guid_text
;
524 if_info
= g_new(if_info_t
, 1);
525 if_info
->name
= g_strdup(name
);
526 if_info
->friendly_name
= NULL
; /* default - unknown */
527 if_info
->vendor_description
= NULL
;
528 if_info
->type
= IF_WIRED
; /* default */
529 if_info
->extcap
= g_strdup("");
532 * Get the interface type.
534 * Much digging failed to reveal any obvious way to get something
535 * such as the SNMP MIB-II ifType value for an interface:
537 * https://www.iana.org/assignments/ianaiftype-mib/ianaiftype-mib
539 * by making some NDIS request. And even if there were such
540 * a way, there's no guarantee that the ifType reflects an
541 * interface type that a user would view as correct (for
542 * example, some systems report Wi-Fi interfaces as
543 * Ethernet interfaces).
545 * So we look for keywords in the vendor's interface
548 if (description
&& (strstr(description
, "generic dialup") != NULL
||
549 strstr(description
, "PPP/SLIP") != NULL
)) {
550 if_info
->type
= IF_DIALUP
;
551 } else if (description
&& (strstr(description
, "Wireless") != NULL
||
552 strstr(description
,"802.11") != NULL
)) {
553 if_info
->type
= IF_WIRELESS
;
554 } else if (description
&& (strstr(description
, "AirPcap") != NULL
||
555 strstr(name
, "airpcap") != NULL
)) {
556 if_info
->type
= IF_AIRPCAP
;
557 } else if (description
&& strstr(description
, "Bluetooth") != NULL
) {
558 if_info
->type
= IF_BLUETOOTH
;
559 } else if (description
&& strstr(description
, "VMware") != NULL
) {
561 * Bridge, NAT, or host-only interface on a VMware host.
563 * XXX - what about guest interfaces?
565 if_info
->type
= IF_VIRTUAL
;
569 * On Windows, the "description" is a vendor description,
570 * and the friendly name isn't returned by Npcap/WinPcap.
571 * Fetch it ourselves.
575 * Skip over the "\Device\NPF_" prefix in the device name,
578 if (strncmp("\\Device\\NPF_", name
, 12) == 0)
579 guid_text
= name
+ 12;
583 /* Now try to parse what remains as a GUID. */
584 if (parse_as_guid(guid_text
, &guid
)) {
586 * Success. Try to get a friendly name using the GUID.
587 * As this is a regular interface, the description is a
588 * vendor description.
590 if_info
->friendly_name
= get_interface_friendly_name_from_device_guid(&guid
);
591 if_info
->vendor_description
= g_strdup(description
);
594 * This is probably not a regular interface; we only
595 * support NT 5 (W2K) and later, so all regular interfaces
596 * should have GUIDs at the end of the name. Therefore,
597 * the description, if supplied, is a friendly name
598 * provided by WinPcap, and there is no vendor
601 if_info
->friendly_name
= g_strdup(description
);
602 if_info
->vendor_description
= NULL
;
606 * On UN*X, if there is a description, it's a friendly
607 * name, and there is no vendor description.
609 * Try the platform's way of getting a friendly name and
610 * interface type first.
612 * If that fails, then, for a loopback interface, give it the
613 * friendly name "Loopback" and, for VMware interfaces,
614 * give them the type IF_VIRTUAL.
616 add_unix_interface_ifinfo(if_info
, name
, description
);
617 if (if_info
->type
== IF_WIRED
) {
619 * This is the default interface type.
621 * Bridge, NAT, or host-only interfaces on VMWare hosts
622 * have the name vmnet[0-9]+. Guests might use a native
623 * (LANCE or E1000) driver or the vmxnet driver. Check
626 if (g_ascii_strncasecmp(name
, "vmnet", 5) == 0)
627 if_info
->type
= IF_VIRTUAL
;
628 else if (g_ascii_strncasecmp(name
, "vmxnet", 6) == 0)
629 if_info
->type
= IF_VIRTUAL
;
631 if (if_info
->friendly_name
== NULL
) {
633 * We couldn't get interface information using platform-
636 * If this is a loopback interface, give it a
637 * "friendly name" of "Loopback".
640 if_info
->friendly_name
= g_strdup("Loopback");
642 if_info
->vendor_description
= NULL
;
644 if_info
->loopback
= loopback
;
645 if_info
->addrs
= NULL
;
646 if_info
->caps
= NULL
;
651 if_info_add_address(if_info_t
*if_info
, struct sockaddr
*addr
)
654 struct sockaddr_in
*ai
;
655 struct sockaddr_in6
*ai6
;
657 switch (addr
->sa_family
) {
660 ai
= (struct sockaddr_in
*)(void *)addr
;
661 if_addr
= (if_addr_t
*)g_malloc(sizeof(*if_addr
));
662 if_addr
->ifat_type
= IF_AT_IPv4
;
663 if_addr
->addr
.ip4_addr
= ai
->sin_addr
.s_addr
;
664 if_info
->addrs
= g_slist_prepend(if_info
->addrs
, if_addr
);
668 ai6
= (struct sockaddr_in6
*)(void *)addr
;
669 if_addr
= (if_addr_t
*)g_malloc(sizeof(*if_addr
));
670 if_addr
->ifat_type
= IF_AT_IPv6
;
671 memcpy((void *)&if_addr
->addr
.ip6_addr
,
672 (void *)&ai6
->sin6_addr
.s6_addr
,
673 sizeof if_addr
->addr
.ip6_addr
);
674 if_info
->addrs
= g_slist_prepend(if_info
->addrs
, if_addr
);
680 * Get all IP address information for the given interface.
683 if_info_ip(if_info_t
*if_info
, pcap_if_t
*d
)
688 for (a
= d
->addresses
; a
!= NULL
; a
= a
->next
) {
690 if_info_add_address(if_info
, a
->addr
);
694 if_info
->addrs
= g_slist_reverse(if_info
->addrs
);
698 #ifdef HAVE_PCAP_REMOTE
700 get_interface_list_findalldevs_ex(const char *hostname
, const char *port
,
701 int auth_type
, const char *username
,
702 const char *passwd
, int *err
, char **err_str
)
704 char source
[PCAP_BUF_SIZE
];
705 struct pcap_rmtauth auth
;
707 pcap_if_t
*alldevs
, *dev
;
710 * WinPcap can overflow PCAP_ERRBUF_SIZE if the host is unreachable.
711 * Fudge a larger size.
713 char errbuf
[PCAP_ERRBUF_SIZE
*4];
715 if (pcap_createsrcstr(source
, PCAP_SRC_IFREMOTE
, hostname
, port
,
716 NULL
, errbuf
) == -1) {
717 *err
= CANT_GET_INTERFACE_LIST
;
718 if (strcmp(errbuf
, "not supported") == 0) {
720 * macOS 14's pcap_createsrcstr(), which is a
721 * stub that always returns -1 with an error
722 * message of "not supported".
724 * In this case, as we passed it an rpcap://
725 * URL, treat that as meaning "remote capture
728 g_strlcpy(errbuf
, "Remote capture not supported",
732 *err_str
= cant_get_if_list_error_message(errbuf
);
736 auth
.type
= auth_type
;
737 auth
.username
= g_strdup(username
);
738 auth
.password
= g_strdup(passwd
);
740 if (ws_pcap_findalldevs_ex(source
, &auth
, &alldevs
, errbuf
) == -1) {
741 *err
= CANT_GET_INTERFACE_LIST
;
742 if (strcmp(errbuf
, "not supported") == 0) {
744 * macOS 14's pcap_findalldevs_ex(), which is a
745 * stub that always returns -1 with an error
746 * message of "not supported".
748 * In this case, as we passed it an rpcap://
749 * URL, treat that as meaning "remote capture
752 g_strlcpy(errbuf
, "Remote capture not supported",
756 *err_str
= cant_get_if_list_error_message(errbuf
);
757 g_free(auth
.username
);
758 g_free(auth
.password
);
762 if (alldevs
== NULL
) {
764 * No interfaces found.
769 g_free(auth
.username
);
770 g_free(auth
.password
);
774 for (dev
= alldevs
; dev
!= NULL
; dev
= dev
->next
) {
775 if_info
= if_info_new(dev
->name
, dev
->description
,
776 (dev
->flags
& PCAP_IF_LOOPBACK
) ? true : false);
777 il
= g_list_append(il
, if_info
);
778 if_info_ip(if_info
, dev
);
780 pcap_freealldevs(alldevs
);
781 g_free(auth
.username
);
782 g_free(auth
.password
);
789 get_interface_list_findalldevs(int *err
, char **err_str
)
792 pcap_if_t
*alldevs
, *dev
;
794 char errbuf
[PCAP_ERRBUF_SIZE
];
796 if (pcap_findalldevs(&alldevs
, errbuf
) == -1) {
797 *err
= CANT_GET_INTERFACE_LIST
;
799 *err_str
= cant_get_if_list_error_message(errbuf
);
803 if (alldevs
== NULL
) {
805 * No interfaces found.
813 for (dev
= alldevs
; dev
!= NULL
; dev
= dev
->next
) {
814 if_info
= if_info_new(dev
->name
, dev
->description
,
815 (dev
->flags
& PCAP_IF_LOOPBACK
) ? true : false);
816 il
= g_list_append(il
, if_info
);
817 if_info_ip(if_info
, dev
);
819 pcap_freealldevs(alldevs
);
825 free_if_cb(void * data
, void * user_data _U_
)
827 if_info_free((if_info_t
*)data
);
831 free_interface_list(GList
*if_list
)
833 g_list_foreach(if_list
, free_if_cb
, NULL
);
834 g_list_free(if_list
);
838 interface_list_copy(GList
*if_list
)
840 return g_list_copy_deep(if_list
, if_info_copy_cb
, NULL
);
844 free_linktype_cb(void * data
)
846 data_link_info_t
*linktype_info
= (data_link_info_t
*)data
;
848 g_free(linktype_info
->name
);
849 g_free(linktype_info
->description
);
850 g_free(linktype_info
);
854 free_timestamp_cb(void * data
)
856 timestamp_info_t
*timestamp_info
= (timestamp_info_t
*)data
;
858 g_free(timestamp_info
->name
);
859 g_free(timestamp_info
->description
);
864 free_if_capabilities(if_capabilities_t
*caps
)
866 g_list_free_full(caps
->data_link_types
, free_linktype_cb
);
867 g_list_free_full(caps
->data_link_types_rfmon
, free_linktype_cb
);
869 g_list_free_full(caps
->timestamp_types
, free_timestamp_cb
);
871 g_free(caps
->primary_msg
);
877 linktype_val_to_name(int dlt
)
879 return pcap_datalink_val_to_name(dlt
);
883 linktype_name_to_val(const char *linktype
)
885 return pcap_datalink_name_to_val(linktype
);
889 * Get the data-link type for a libpcap device.
890 * This works around AIX 5.x's non-standard and incompatible-with-the-
891 * rest-of-the-universe libpcap.
894 get_pcap_datalink(pcap_t
*pch
,
896 const char* devicename
898 const char* devicename _U_
904 const char *ifacename
;
907 datalink
= pcap_datalink(pch
);
911 * The libpcap that comes with AIX 5.x uses RFC 1573 ifType values
912 * rather than DLT_ values for link-layer types; the ifType values
913 * for LAN devices are:
920 * and the ifType value for a loopback device is 24.
922 * The AIX names for LAN devices begin with:
929 * and the AIX names for loopback devices begin with "lo".
931 * (The difference between "Ethernet" and "802.3" is presumably
932 * whether packets have an Ethernet header, with a packet type,
933 * or an 802.3 header, with a packet length, followed by an 802.2
934 * header and possibly a SNAP header.)
936 * If the device name matches "datalink" interpreted as an ifType
937 * value, rather than as a DLT_ value, we will assume this is AIX's
938 * non-standard, incompatible libpcap, rather than a standard libpcap,
939 * and will map the link-layer type to the standard DLT_ value for
940 * that link-layer type, as that's what the rest of Wireshark expects.
942 * (This means the capture files won't be readable by a tcpdump
943 * linked with AIX's non-standard libpcap, but so it goes. They
944 * *will* be readable by standard versions of tcpdump, Wireshark,
947 * XXX - if we conclude we're using AIX libpcap, should we also
948 * set a flag to cause us to assume the time stamps are in
949 * seconds-and-nanoseconds form, and to convert them to
950 * seconds-and-microseconds form before processing them and
955 * Find the last component of the device name, which is the
958 ifacename
= strchr(devicename
, '/');
959 if (ifacename
== NULL
)
960 ifacename
= devicename
;
962 /* See if it matches any of the LAN device names. */
963 if (strncmp(ifacename
, "en", 2) == 0) {
966 * That's the RFC 1573 value for Ethernet;
967 * map it to DLT_EN10MB.
971 } else if (strncmp(ifacename
, "et", 2) == 0) {
974 * That's the RFC 1573 value for 802.3;
975 * map it to DLT_EN10MB.
977 * (libpcap, tcpdump, Wireshark, etc. don't
978 * care if it's Ethernet or 802.3.)
982 } else if (strncmp(ifacename
, "tr", 2) == 0) {
985 * That's the RFC 1573 value for 802.5 (Token Ring);
986 * map it to DLT_IEEE802, which is what's used for
991 } else if (strncmp(ifacename
, "fi", 2) == 0) {
992 if (datalink
== 15) {
994 * That's the RFC 1573 value for FDDI;
995 * map it to DLT_FDDI.
999 } else if (strncmp(ifacename
, "lo", 2) == 0) {
1000 if (datalink
== 24) {
1002 * That's the RFC 1573 value for "software loopback"
1003 * devices; map it to DLT_NULL, which is what's used
1004 * for loopback devices on BSD.
1014 /* Set the data link type on a pcap. */
1016 set_pcap_datalink(pcap_t
*pcap_h
, int datalink
, char *name
,
1017 char *errmsg
, size_t errmsg_len
,
1018 char *secondary_errmsg
, size_t secondary_errmsg_len
)
1020 char *set_datalink_err_str
;
1023 return true; /* just use the default */
1024 if (pcap_set_datalink(pcap_h
, datalink
) == 0)
1025 return true; /* no error */
1026 set_datalink_err_str
= pcap_geterr(pcap_h
);
1027 snprintf(errmsg
, errmsg_len
, "Unable to set data link type on interface '%s' (%s).",
1028 name
, set_datalink_err_str
);
1030 * If the error isn't "XXX is not one of the DLTs supported by this device",
1031 * tell the user to tell the Wireshark developers about it.
1033 if (strstr(set_datalink_err_str
, "is not one of the DLTs supported by this device") == NULL
)
1034 snprintf(secondary_errmsg
, secondary_errmsg_len
,
1035 "%s", please_report_bug());
1037 secondary_errmsg
[0] = '\0';
1041 static data_link_info_t
*
1042 create_data_link_info(int dlt
)
1044 data_link_info_t
*data_link_info
;
1047 data_link_info
= g_new(data_link_info_t
, 1);
1048 data_link_info
->dlt
= dlt
;
1049 text
= pcap_datalink_val_to_name(dlt
);
1051 data_link_info
->name
= g_strdup(text
);
1053 data_link_info
->name
= ws_strdup_printf("DLT %d", dlt
);
1054 text
= pcap_datalink_val_to_description(dlt
);
1055 data_link_info
->description
= g_strdup(text
);
1056 return data_link_info
;
1060 get_data_link_types(pcap_t
*pch
, interface_options
*interface_opts
,
1061 cap_device_open_status
*status
, char **status_str
)
1063 GList
*data_link_types
;
1067 data_link_info_t
*data_link_info
;
1069 deflt
= get_pcap_datalink(pch
, interface_opts
->name
);
1070 nlt
= pcap_list_datalinks(pch
, &linktypes
);
1073 * A negative return is an error.
1075 #ifdef HAVE_PCAP_CREATE
1077 * If we have pcap_create(), we have
1078 * pcap_statustostr(), and we can get back errors
1079 * other than PCAP_ERROR (-1), such as
1080 * PCAP_ERROR_NOT_ACTIVATED. and we should report
1086 *status
= CAP_DEVICE_OPEN_ERROR_OTHER
;
1087 *status_str
= ws_strdup_printf("pcap_list_datalinks() failed: %s",
1093 * This "shouldn't happen".
1095 *status
= CAP_DEVICE_OPEN_ERROR_OTHER
;
1096 *status_str
= ws_strdup_printf("pcap_list_datalinks() failed: %s - %s",
1097 pcap_statustostr(nlt
), pcap_geterr(pch
));
1100 #else /* HAVE_PCAP_CREATE */
1101 *status
= CAP_DEVICE_OPEN_ERROR_OTHER
;
1102 *status_str
= ws_strdup_printf("pcap_list_datalinks() failed: %s",
1104 #endif /* HAVE_PCAP_CREATE */
1107 data_link_types
= NULL
;
1108 for (i
= 0; i
< nlt
; i
++) {
1109 data_link_info
= create_data_link_info(linktypes
[i
]);
1112 * XXX - for 802.11, make the most detailed 802.11
1113 * version the default, rather than the one the
1114 * device has as the default?
1116 if (linktypes
[i
] == deflt
)
1117 data_link_types
= g_list_prepend(data_link_types
,
1120 data_link_types
= g_list_append(data_link_types
,
1123 #ifdef HAVE_PCAP_FREE_DATALINKS
1124 pcap_free_datalinks(linktypes
);
1127 * In Windows, there's no guarantee that if you have a library
1128 * built with one version of the MSVC++ run-time library, and
1129 * it returns a pointer to allocated data, you can free that
1130 * data from a program linked with another version of the
1131 * MSVC++ run-time library.
1133 * This is not an issue on UN*X.
1135 * See the mail threads starting at
1137 * https://www.winpcap.org/pipermail/winpcap-users/2006-September/001421.html
1141 * https://www.winpcap.org/pipermail/winpcap-users/2008-May/002498.html
1144 #define xx_free free /* hack so checkAPIs doesn't complain */
1147 #endif /* HAVE_PCAP_FREE_DATALINKS */
1150 return data_link_types
;
1153 /* Get supported timestamp types for a libpcap device. */
1155 get_pcap_timestamp_types(pcap_t
*pch _U_
, char **err_str _U_
)
1158 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1160 int ntypes
= pcap_list_tstamp_types(pch
, &types
);
1163 *err_str
= ntypes
< 0 ? pcap_geterr(pch
) : NULL
;
1169 timestamp_info_t
*info
= (timestamp_info_t
*)g_malloc(sizeof *info
);
1170 info
->name
= g_strdup(pcap_tstamp_type_val_to_name(types
[ntypes
]));
1171 info
->description
= g_strdup(pcap_tstamp_type_val_to_description(types
[ntypes
]));
1172 list
= g_list_prepend(list
, info
);
1175 pcap_free_tstamp_types(types
);
1180 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1182 * Request high-resolution time stamps.
1184 * If this fails with PCAP_ERROR_TSTAMP_PRECISION_NOTSUP, that means
1185 * that boring old microsecond-resolution time stamps are all that
1186 * are supported, so we just live with that.
1189 request_high_resolution_timestamp(pcap_t
*pcap_h
)
1195 * On macOS, if you build with a newer SDK, pcap_set_tstamp_precision()
1196 * is available, so the code will be built with it.
1198 * However, if you then try to run on an older release that
1199 * doesn't have pcap_set_tstamp_precision(), the dynamic linker
1200 * will fail, as it won't find pcap_set_tstamp_precision().
1202 * libpcap doesn't use macOS "weak linking" for new routines,
1203 * so we can't just check whether a pointer to
1204 * pcap_set_tstamp_precision() is null and, if it is, not
1205 * call it. We have to, instead, use dlopen() to load
1206 * libpcap, and dlsym() to find a pointer to pcap_set_tstamp_precision(),
1207 * and if we find the pointer, call it.
1209 static bool initialized
= false;
1210 static int (*p_pcap_set_tstamp_precision
)(pcap_t
*, int);
1213 p_pcap_set_tstamp_precision
=
1214 (int (*)(pcap_t
*, int))
1215 dlsym(RTLD_NEXT
, "pcap_set_tstamp_precision");
1218 if (p_pcap_set_tstamp_precision
!= NULL
) {
1219 status
= (*p_pcap_set_tstamp_precision
)(pcap_h
,
1220 PCAP_TSTAMP_PRECISION_NANO
);
1223 * Older libpcap, which doesn't have support
1224 * for setting the time stamp resolution.
1226 status
= PCAP_ERROR_TSTAMP_PRECISION_NOTSUP
;
1228 #else /* __APPLE__ */
1230 * On other UN*Xes we require that we be run on an OS version
1231 * with a libpcap equal to or later than the version with which
1234 status
= pcap_set_tstamp_precision(pcap_h
, PCAP_TSTAMP_PRECISION_NANO
);
1235 #endif /* __APPLE__ */
1236 if (status
== PCAP_ERROR_TSTAMP_PRECISION_NOTSUP
) {
1237 /* This isn't a fatal error. */
1244 * Return true if the pcap_t in question is set up for high-precision
1245 * time stamps, false otherwise.
1248 have_high_resolution_timestamp(pcap_t
*pcap_h
)
1254 static bool initialized
= false;
1255 static int (*p_pcap_get_tstamp_precision
)(pcap_t
*);
1258 p_pcap_get_tstamp_precision
=
1260 dlsym(RTLD_NEXT
, "pcap_get_tstamp_precision");
1263 if (p_pcap_get_tstamp_precision
!= NULL
)
1264 return (*p_pcap_get_tstamp_precision
)(pcap_h
) == PCAP_TSTAMP_PRECISION_NANO
;
1266 return false; /* Can't get implies couldn't set */
1267 #else /* __APPLE__ */
1269 * On other UN*Xes we require that we be run on an OS version
1270 * with a libpcap equal to or later than the version with which
1273 return pcap_get_tstamp_precision(pcap_h
) == PCAP_TSTAMP_PRECISION_NANO
;
1274 #endif /* __APPLE__ */
1277 #endif /* HAVE_PCAP_SET_TSTAMP_PRECISION */
1279 #ifdef HAVE_PCAP_CREATE
1282 is_linux_bonding_device(const char *ifname
)
1288 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
1292 memset(&ifr
, 0, sizeof ifr
);
1293 (void) g_strlcpy(ifr
.ifr_name
, ifname
, sizeof ifr
.ifr_name
);
1294 memset(&ifb
, 0, sizeof ifb
);
1295 ifr
.ifr_data
= (caddr_t
)&ifb
;
1296 #if defined(SIOCBONDINFOQUERY)
1297 if (ioctl(fd
, SIOCBONDINFOQUERY
, &ifr
) == 0) {
1302 if (ioctl(fd
, BOND_INFO_QUERY_OLD
, &ifr
) == 0) {
1313 is_linux_bonding_device(const char *ifname _U_
)
1320 get_if_capabilities_pcap_create(interface_options
*interface_opts
,
1321 cap_device_open_status
*open_status
, char **open_status_str
)
1323 if_capabilities_t
*caps
;
1324 char errbuf
[PCAP_ERRBUF_SIZE
];
1328 pch
= pcap_create(interface_opts
->name
, errbuf
);
1330 *open_status
= CAP_DEVICE_OPEN_ERROR_OTHER
;
1331 *open_status_str
= g_strdup(errbuf
);
1335 if (is_linux_bonding_device(interface_opts
->name
)) {
1337 * Linux bonding device; not Wi-Fi, so no monitor mode, and
1338 * calling pcap_can_set_rfmon() might get a "no such device"
1344 * Not a Linux bonding device, so go ahead.
1346 status
= pcap_can_set_rfmon(pch
);
1352 case PCAP_ERROR_NO_SUCH_DEVICE
:
1353 *open_status
= CAP_DEVICE_OPEN_ERROR_NO_SUCH_DEVICE
;
1354 *open_status_str
= ws_strdup_printf("pcap_can_set_rfmon() failed: %s",
1358 case PCAP_ERROR_PERM_DENIED
:
1359 *open_status
= CAP_DEVICE_OPEN_ERROR_PERM_DENIED
;
1360 *open_status_str
= ws_strdup_printf("pcap_can_set_rfmon() failed: %s",
1365 *open_status
= CAP_DEVICE_OPEN_ERROR_OTHER
;
1366 *open_status_str
= ws_strdup_printf("pcap_can_set_rfmon() failed: %s",
1371 *open_status
= CAP_DEVICE_OPEN_ERROR_OTHER
;
1372 *open_status_str
= ws_strdup_printf("pcap_can_set_rfmon() failed: %s - %s",
1373 pcap_statustostr(status
), pcap_geterr(pch
));
1379 caps
= (if_capabilities_t
*)g_malloc0(sizeof *caps
);
1381 caps
->can_set_rfmon
= false;
1382 else if (status
== 1) {
1383 caps
->can_set_rfmon
= true;
1384 if (interface_opts
->monitor_mode
) {
1385 status
= pcap_set_rfmon(pch
, 1);
1388 * This "should not happen".
1390 *open_status
= CAP_DEVICE_OPEN_ERROR_OTHER
;
1391 *open_status_str
= ws_strdup_printf("pcap_set_rfmon() returned %d",
1400 * This "should not happen".
1402 *open_status
= CAP_DEVICE_OPEN_ERROR_OTHER
;
1403 *open_status_str
= ws_strdup_printf("pcap_can_set_rfmon() returned %d",
1410 status
= pcap_activate(pch
);
1415 case PCAP_ERROR_NO_SUCH_DEVICE
:
1416 *open_status
= CAP_DEVICE_OPEN_ERROR_NO_SUCH_DEVICE
;
1417 *open_status_str
= ws_strdup_printf("pcap_activate() failed: %s",
1421 case PCAP_ERROR_PERM_DENIED
:
1422 *open_status
= CAP_DEVICE_OPEN_ERROR_PERM_DENIED
;
1423 *open_status_str
= ws_strdup_printf("pcap_activate() failed: %s",
1427 case PCAP_ERROR_IFACE_NOT_UP
:
1428 *open_status
= CAP_DEVICE_OPEN_ERROR_IFACE_NOT_UP
;
1429 *open_status_str
= ws_strdup_printf("pcap_activate() failed: %s",
1434 *open_status
= CAP_DEVICE_OPEN_ERROR_OTHER
;
1435 *open_status_str
= ws_strdup_printf("pcap_activate() failed: %s",
1440 *open_status
= CAP_DEVICE_OPEN_ERROR_OTHER
;
1441 *open_status_str
= ws_strdup_printf("pcap_activate() failed: %s - %s",
1442 pcap_statustostr(status
), pcap_geterr(pch
));
1450 caps
->data_link_types
= get_data_link_types(pch
, interface_opts
,
1451 open_status
, open_status_str
);
1452 if (caps
->data_link_types
== NULL
) {
1457 if (interface_opts
->monitor_mode
) {
1458 caps
->data_link_types_rfmon
= caps
->data_link_types
;
1459 caps
->data_link_types
= NULL
;
1462 caps
->timestamp_types
= get_pcap_timestamp_types(pch
, NULL
);
1466 *open_status
= CAP_DEVICE_OPEN_NO_ERR
;
1467 if (open_status_str
!= NULL
)
1468 *open_status_str
= NULL
;
1473 set_open_status_str(int status
, pcap_t
*pcap_h
,
1474 char (*open_status_str
)[PCAP_ERRBUF_SIZE
])
1479 (void) g_strlcpy(*open_status_str
, pcap_geterr(pcap_h
),
1480 sizeof *open_status_str
);
1484 (void) g_strlcpy(*open_status_str
, pcap_statustostr(status
),
1485 sizeof *open_status_str
);
1491 open_capture_device_pcap_create(
1492 capture_options
* capture_opts _U_
,
1493 interface_options
*interface_opts
, int timeout
,
1494 cap_device_open_status
*open_status
,
1495 char (*open_status_str
)[PCAP_ERRBUF_SIZE
])
1500 ws_debug("Calling pcap_create() using %s.", interface_opts
->name
);
1501 pcap_h
= pcap_create(interface_opts
->name
, *open_status_str
);
1502 ws_debug("pcap_create() returned %p.", (void *)pcap_h
);
1503 if (pcap_h
== NULL
) {
1504 *open_status
= CAP_DEVICE_OPEN_ERROR_OTHER
;
1507 if (interface_opts
->has_snaplen
) {
1508 ws_debug("Calling pcap_set_snaplen() with snaplen %d.",
1509 interface_opts
->snaplen
);
1510 status
= pcap_set_snaplen(pcap_h
, interface_opts
->snaplen
);
1513 set_open_status_str(status
, pcap_h
, open_status_str
);
1514 *open_status
= CAP_DEVICE_OPEN_ERROR_OTHER
;
1519 ws_debug("Calling pcap_set_promisc() with promisc_mode %d.",
1520 interface_opts
->promisc_mode
);
1521 status
= pcap_set_promisc(pcap_h
, interface_opts
->promisc_mode
);
1524 set_open_status_str(status
, pcap_h
, open_status_str
);
1525 *open_status
= CAP_DEVICE_OPEN_ERROR_OTHER
;
1529 status
= pcap_set_timeout(pcap_h
, timeout
);
1532 set_open_status_str(status
, pcap_h
, open_status_str
);
1533 *open_status
= CAP_DEVICE_OPEN_ERROR_OTHER
;
1538 #ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
1540 * Try to enable nanosecond-resolution capture; any code
1541 * that can read pcapng files must be able to handle
1542 * nanosecond-resolution time stamps. We think at this
1543 * point that code that reads pcap files should recognize
1544 * the nanosecond-resolution pcap file magic number. If
1545 * it doesn't, we can downconvert via a program that
1548 * We don't care whether this succeeds or fails; if it
1549 * fails (because we don't have pcap_set_tstamp_precision(),
1550 * or because we do but the OS or device doesn't support
1551 * nanosecond resolution timing), we just use the microsecond-
1552 * resolution time stamps we get.
1554 status
= request_high_resolution_timestamp(pcap_h
);
1557 set_open_status_str(status
, pcap_h
, open_status_str
);
1558 *open_status
= CAP_DEVICE_OPEN_ERROR_OTHER
;
1562 #endif /* HAVE_PCAP_SET_TSTAMP_PRECISION */
1564 #ifdef HAVE_PCAP_SET_TSTAMP_TYPE
1565 if (interface_opts
->timestamp_type
) {
1566 status
= pcap_set_tstamp_type(pcap_h
, interface_opts
->timestamp_type_id
);
1568 * XXX - what if it fails because that time stamp type
1572 set_open_status_str(status
, pcap_h
, open_status_str
);
1573 *open_status
= CAP_DEVICE_OPEN_ERROR_OTHER
;
1578 #endif /* HAVE_PCAP_SET_TSTAMP_PRECISION */
1580 ws_debug("buffersize %d.", interface_opts
->buffer_size
);
1581 if (interface_opts
->buffer_size
!= 0) {
1582 status
= pcap_set_buffer_size(pcap_h
,
1583 interface_opts
->buffer_size
* 1024 * 1024);
1585 set_open_status_str(status
, pcap_h
, open_status_str
);
1586 *open_status
= CAP_DEVICE_OPEN_ERROR_OTHER
;
1591 ws_debug("monitor_mode %d.", interface_opts
->monitor_mode
);
1592 if (interface_opts
->monitor_mode
) {
1593 status
= pcap_set_rfmon(pcap_h
, 1);
1595 set_open_status_str(status
, pcap_h
, open_status_str
);
1596 *open_status
= CAP_DEVICE_OPEN_ERROR_OTHER
;
1601 status
= pcap_activate(pcap_h
);
1602 ws_debug("pcap_activate() returned %d.", status
);
1604 /* Failed to activate, set to NULL */
1607 case PCAP_ERROR_NO_SUCH_DEVICE
:
1608 *open_status
= CAP_DEVICE_OPEN_ERROR_NO_SUCH_DEVICE
;
1609 (void) g_strlcpy(*open_status_str
, pcap_geterr(pcap_h
),
1610 sizeof *open_status_str
);
1613 case PCAP_ERROR_PERM_DENIED
:
1614 *open_status
= CAP_DEVICE_OPEN_ERROR_PERM_DENIED
;
1615 (void) g_strlcpy(*open_status_str
, pcap_geterr(pcap_h
),
1616 sizeof *open_status_str
);
1619 #ifdef HAVE_PCAP_ERROR_PROMISC_PERM_DENIED
1620 case PCAP_ERROR_PROMISC_PERM_DENIED
:
1621 *open_status
= CAP_DEVICE_OPEN_ERROR_PROMISC_PERM_DENIED
;
1622 (void) g_strlcpy(*open_status_str
, pcap_geterr(pcap_h
),
1623 sizeof *open_status_str
);
1627 case PCAP_ERROR_RFMON_NOTSUP
:
1628 *open_status
= CAP_DEVICE_OPEN_ERROR_RFMON_NOTSUP
;
1629 (void) g_strlcpy(*open_status_str
, pcap_geterr(pcap_h
),
1630 sizeof *open_status_str
);
1633 case PCAP_ERROR_IFACE_NOT_UP
:
1634 *open_status
= CAP_DEVICE_OPEN_ERROR_IFACE_NOT_UP
;
1635 (void) g_strlcpy(*open_status_str
, pcap_geterr(pcap_h
),
1636 sizeof *open_status_str
);
1640 *open_status
= CAP_DEVICE_OPEN_ERROR_OTHER
;
1641 (void) g_strlcpy(*open_status_str
, pcap_geterr(pcap_h
),
1642 sizeof *open_status_str
);
1646 *open_status
= CAP_DEVICE_OPEN_ERROR_OTHER
;
1647 snprintf(*open_status_str
, sizeof *open_status_str
,
1648 "%s - %s", pcap_statustostr(status
), pcap_geterr(pcap_h
));
1656 * Warning. The call succeeded, but something happened
1657 * that the user might want to know.
1661 case PCAP_WARNING_PROMISC_NOTSUP
:
1662 *open_status
= CAP_DEVICE_OPEN_WARNING_PROMISC_NOTSUP
;
1663 (void) g_strlcpy(*open_status_str
, pcap_geterr(pcap_h
),
1664 sizeof *open_status_str
);
1667 #ifdef HAVE_PCAP_WARNING_TSTAMP_TYPE_NOTSUP
1668 case PCAP_WARNING_TSTAMP_TYPE_NOTSUP
:
1669 *open_status
= CAP_DEVICE_OPEN_WARNING_TSTAMP_TYPE_NOTSUP
;
1670 (void) g_strlcpy(*open_status_str
, pcap_geterr(pcap_h
),
1671 sizeof *open_status_str
);
1676 *open_status
= CAP_DEVICE_OPEN_WARNING_OTHER
;
1677 (void) g_strlcpy(*open_status_str
, pcap_geterr(pcap_h
),
1678 sizeof *open_status_str
);
1682 *open_status
= CAP_DEVICE_OPEN_WARNING_OTHER
;
1683 snprintf(*open_status_str
, sizeof *open_status_str
,
1684 "%s - %s", pcap_statustostr(status
), pcap_geterr(pcap_h
));
1689 * No warning issued.
1691 *open_status
= CAP_DEVICE_OPEN_NO_ERR
;
1695 #endif /* HAVE_PCAP_CREATE */
1698 get_if_capabilities_pcap_open_live(interface_options
*interface_opts
,
1699 cap_device_open_status
*open_status
, char **open_status_str
)
1701 if_capabilities_t
*caps
;
1702 char errbuf
[PCAP_ERRBUF_SIZE
];
1705 pch
= pcap_open_live(interface_opts
->name
, MIN_PACKET_SIZE
, 0, 0,
1708 *open_status
= CAP_DEVICE_OPEN_ERROR_OTHER
;
1709 *open_status_str
= g_strdup(errbuf
[0] == '\0' ? "Unknown error (pcap bug; actual error cause not reported)" : errbuf
);
1713 caps
= (if_capabilities_t
*)g_malloc0(sizeof *caps
);
1714 caps
->can_set_rfmon
= false;
1715 caps
->data_link_types
= get_data_link_types(pch
, interface_opts
,
1716 open_status
, open_status_str
);
1717 if (caps
->data_link_types
== NULL
) {
1723 caps
->timestamp_types
= get_pcap_timestamp_types(pch
, NULL
);
1727 *open_status
= CAP_DEVICE_OPEN_NO_ERR
;
1728 *open_status_str
= NULL
;
1733 open_capture_device_pcap_open_live(interface_options
*interface_opts
,
1734 int timeout
, cap_device_open_status
*open_status
,
1735 char (*open_status_str
)[PCAP_ERRBUF_SIZE
])
1740 if (interface_opts
->has_snaplen
)
1741 snaplen
= interface_opts
->snaplen
;
1744 * Default - use the non-D-Bus maximum snapshot length of
1745 * 256KB, which should be big enough (libpcap didn't get
1746 * D-Bus support until after it goet pcap_create() and
1747 * pcap_activate(), so we don't have D-Bus support and
1748 * don't have to worry about really huge packets).
1752 ws_debug("pcap_open_live() calling using name %s, snaplen %d, promisc_mode %d.",
1753 interface_opts
->name
, snaplen
, interface_opts
->promisc_mode
);
1755 * This might succeed but put a messsage in *open_status_str;
1756 * that means that a warning was issued.
1758 * Clear the error message buffer, so that if it's not an empty
1759 * string after the call, we know a warning was issued.
1761 (*open_status_str
)[0] = '\0';
1762 pcap_h
= pcap_open_live(interface_opts
->name
, snaplen
,
1763 interface_opts
->promisc_mode
, timeout
, *open_status_str
);
1764 ws_debug("pcap_open_live() returned %p.", (void *)pcap_h
);
1765 if (pcap_h
== NULL
) {
1766 *open_status
= CAP_DEVICE_OPEN_ERROR_OTHER
;
1769 if ((*open_status_str
)[0] != '\0') {
1771 * Warning. The call succeeded, but something happened
1772 * that the user might want to know.
1774 *open_status
= CAP_DEVICE_OPEN_WARNING_OTHER
;
1777 * No warning issued.
1779 *open_status
= CAP_DEVICE_OPEN_NO_ERR
;
1783 /* Try to set the capture buffer size. */
1784 if (interface_opts
->buffer_size
> 1) {
1786 * We have no mechanism to report a warning if this
1787 * fails; we just keep capturing with the smaller buffer,
1788 * as is the case on systems with BPF and pcap_create()
1789 * and pcap_set_buffer_size(), where pcap_activate() just
1790 * silently clamps the buffer size to the maximum.
1792 pcap_setbuff(pcap_h
, interface_opts
->buffer_size
* 1024 * 1024);
1800 * Get the capabilities of a network device.
1803 get_if_capabilities(interface_options
*interface_opts
,
1804 cap_device_open_status
*status
, char **status_str
)
1806 #if defined(HAVE_PCAP_OPEN) && defined(HAVE_PCAP_REMOTE)
1807 if_capabilities_t
*caps
;
1808 char errbuf
[PCAP_ERRBUF_SIZE
];
1811 data_link_info_t
*data_link_info
;
1813 if (strncmp (interface_opts
->name
, "rpcap://", 8) == 0) {
1814 struct pcap_rmtauth auth
;
1816 auth
.type
= interface_opts
->auth_type
== CAPTURE_AUTH_PWD
?
1817 RPCAP_RMTAUTH_PWD
: RPCAP_RMTAUTH_NULL
;
1818 auth
.username
= interface_opts
->auth_username
;
1819 auth
.password
= interface_opts
->auth_password
;
1822 * WinPcap 4.1.2, and possibly earlier versions, have a bug
1823 * wherein, when an open with an rpcap: URL fails, the error
1824 * message for the error is not copied to errbuf and whatever
1825 * on-the-stack junk is in errbuf is treated as the error
1828 * To work around that (and any other bugs of that sort), we
1829 * initialize errbuf to an empty string. If we get an error
1830 * and the string is empty, we report it as an unknown error.
1831 * (If we *don't* get an error, and the string is *non*-empty,
1832 * that could be a warning returned, such as "can't turn
1833 * promiscuous mode on"; we currently don't do so.)
1836 pch
= pcap_open(interface_opts
->name
, MIN_PACKET_SIZE
, 0, 0, &auth
,
1840 * We don't know whether it's a permission error or not.
1841 * And, if it is, the user will either have to ask for
1842 * permission for their own remote account or will have
1843 * to use an account that *does* have permissions.
1845 *status
= CAP_DEVICE_OPEN_ERROR_GENERIC
;
1846 if (strcmp(errbuf
, "not supported") == 0) {
1848 * macOS 14's pcap_open(), which is a stub that
1849 * always returns NULL with an error message of
1852 * In this case, as we passed it an rpcap://
1853 * URL, treat that as meaning "remote capture
1856 g_strlcpy(errbuf
, "Remote capture not supported",
1859 *status_str
= g_strdup(errbuf
[0] == '\0' ? "Unknown error (pcap bug; actual error cause not reported)" : errbuf
);
1863 caps
= (if_capabilities_t
*)g_malloc0(sizeof *caps
);
1864 caps
->can_set_rfmon
= false;
1865 caps
->data_link_types
= NULL
;
1866 deflt
= get_pcap_datalink(pch
, interface_opts
->name
);
1867 data_link_info
= create_data_link_info(deflt
);
1868 caps
->data_link_types
= g_list_append(caps
->data_link_types
, data_link_info
);
1869 caps
->timestamp_types
= get_pcap_timestamp_types(pch
, NULL
);
1873 * This doesn't return warnings for remote devices, and
1874 * we don't use it for local devices.
1876 *status
= CAP_DEVICE_OPEN_NO_ERR
;
1880 #endif /* defined(HAVE_PCAP_OPEN) && defined(HAVE_PCAP_REMOTE) */
1885 return get_if_capabilities_local(interface_opts
, status
, status_str
);
1889 open_capture_device(capture_options
*capture_opts
,
1890 interface_options
*interface_opts
, int timeout
,
1891 cap_device_open_status
*open_status
,
1892 char (*open_status_str
)[PCAP_ERRBUF_SIZE
])
1895 #if defined(HAVE_PCAP_OPEN) && defined(HAVE_PCAP_REMOTE)
1896 struct pcap_rmtauth auth
;
1899 /* Open the network interface to capture from it.
1900 Some versions of libpcap may put warnings into the error buffer
1901 if they succeed; to tell if that's happened, we have to clear
1902 the error buffer, and check if it's still a null string. */
1903 ws_debug("Entering open_capture_device().");
1904 *open_status
= CAP_DEVICE_OPEN_NO_ERR
;
1905 (*open_status_str
)[0] = '\0';
1906 #if defined(HAVE_PCAP_OPEN) && defined(HAVE_PCAP_REMOTE)
1908 * If we're opening a remote device, use pcap_open(); that's currently
1909 * the only open routine that supports remote devices.
1911 if (strncmp (interface_opts
->name
, "rpcap://", 8) == 0) {
1914 auth
.type
= interface_opts
->auth_type
== CAPTURE_AUTH_PWD
?
1915 RPCAP_RMTAUTH_PWD
: RPCAP_RMTAUTH_NULL
;
1916 auth
.username
= interface_opts
->auth_username
;
1917 auth
.password
= interface_opts
->auth_password
;
1919 if (interface_opts
->has_snaplen
)
1920 snaplen
= interface_opts
->snaplen
;
1923 * Default - use the non-D-Bus maximum snapshot length,
1924 * which should be big enough, except for D-Bus.
1928 ws_debug("Calling pcap_open() using name %s, snaplen %d, promisc_mode %d, datatx_udp %d, nocap_rpcap %d.",
1929 interface_opts
->name
, snaplen
,
1930 interface_opts
->promisc_mode
, interface_opts
->datatx_udp
,
1931 interface_opts
->nocap_rpcap
);
1932 pcap_h
= pcap_open(interface_opts
->name
, snaplen
,
1934 (interface_opts
->promisc_mode
? PCAP_OPENFLAG_PROMISCUOUS
: 0) |
1935 (interface_opts
->datatx_udp
? PCAP_OPENFLAG_DATATX_UDP
: 0) |
1936 (interface_opts
->nocap_rpcap
? PCAP_OPENFLAG_NOCAPTURE_RPCAP
: 0),
1937 timeout
, &auth
, *open_status_str
);
1938 if (pcap_h
== NULL
) {
1942 * We don't know whether it's a permission error
1944 * (If it is, maybe we can give ourselves permission
1945 * or maybe we just have to ask politely for
1948 *open_status
= CAP_DEVICE_OPEN_ERROR_GENERIC
;
1949 if (strcmp(*open_status_str
, "not supported") == 0) {
1951 * macOS 14's pcap_open(), which is a stub
1952 * that always returns NULL with an error
1953 * message of "not supported".
1955 * In this case, as we passed it an rpcap://
1956 * URL, treat that as meaning "remote capture
1959 g_strlcpy(*open_status_str
,
1960 "Remote capture not supported",
1964 /* Did pcap actually supply an error message? */
1965 if ((*open_status_str
)[0] == '\0') {
1967 * Work around known WinPcap bug wherein
1968 * no error message is filled in on a
1969 * failure to open an rpcap: URL.
1971 (void) g_strlcpy(*open_status_str
,
1972 "Unknown error (pcap bug; actual error cause not reported)",
1973 sizeof *open_status_str
);
1976 ws_debug("pcap_open() returned %p.", (void *)pcap_h
);
1977 ws_debug("open_capture_device %s : %s", pcap_h
? "SUCCESS" : "FAILURE", interface_opts
->name
);
1979 * This doesn't return warnings for remote devices, and
1980 * we don't use it for local devices.
1982 *open_status
= CAP_DEVICE_OPEN_NO_ERR
;
1987 pcap_h
= open_capture_device_local(capture_opts
, interface_opts
,
1988 timeout
, open_status
, open_status_str
);
1989 ws_debug("open_capture_device %s : %s", pcap_h
? "SUCCESS" : "FAILURE", interface_opts
->name
);
1994 * Platform-dependent suggestions for fixing permissions.
1998 #define LIBCAP_PERMISSIONS_SUGGESTION \
2000 "If you did not install Wireshark from a package, ensure that Dumpcap " \
2001 "has the needed CAP_NET_RAW and CAP_NET_ADMIN capabilities by running " \
2003 " sudo setcap cap_net_raw,cap_net_admin=ep {path/to/}dumpcap" \
2005 "and then restarting Wireshark."
2007 #define LIBCAP_PERMISSIONS_SUGGESTION
2010 #if defined(__linux__)
2011 #define PLATFORM_PERMISSIONS_SUGGESTION \
2013 "On Debian and Debian derivatives such as Ubuntu, if you have " \
2014 "installed Wireshark from a package, try running" \
2016 " sudo dpkg-reconfigure wireshark-common" \
2018 "selecting \"<Yes>\" in response to the question" \
2020 " Should non-superusers be able to capture packets?" \
2022 "adding yourself to the \"wireshark\" group by running" \
2024 " sudo usermod -a -G wireshark {your username}" \
2026 "and then logging out and logging back in again." \
2027 LIBCAP_PERMISSIONS_SUGGESTION
2028 #elif defined(__APPLE__)
2029 #define PLATFORM_PERMISSIONS_SUGGESTION \
2031 "If you installed Wireshark using the package from wireshark.org, " \
2032 "close this dialog and click on the \"installing ChmodBPF\" link in " \
2033 "\"You can fix this by installing ChmodBPF.\" on the main screen, " \
2034 "and then complete the installation procedure."
2036 #define PLATFORM_PERMISSIONS_SUGGESTION
2041 get_platform_pcap_failure_secondary_error_message(const char *open_status_str
)
2044 * The error string begins with the error produced by WinPcap
2045 * and Npcap if attempting to set promiscuous mode fails.
2046 * (Note that this string could have a specific error message
2047 * from an NDIS error after the initial part, so we do a prefix
2048 * check rather than an exact match check.)
2050 * If this is with Npcap 1.71 through 1.73, which have bugs that
2051 * cause this error on Windows 11 with some drivers, suggest that
2052 * the user upgrade to the current version of Npcap;
2053 * otherwise, suggest that they turn off promiscuous mode
2056 static const char promisc_failed
[] =
2057 "failed to set hardware filter to promiscuous mode";
2059 if (strncmp(open_status_str
, promisc_failed
, sizeof promisc_failed
- 1) == 0) {
2060 unsigned int npcap_major
, npcap_minor
;
2062 if (caplibs_get_npcap_version(&npcap_major
, &npcap_minor
)) {
2063 if (npcap_major
== 1 &&
2064 (npcap_minor
>= 71 && npcap_minor
<= 73)) {
2066 "This is a bug in your version of Npcap.\n"
2068 "If you need to use promiscuous mode, you must upgrade to the current "
2069 "version of Npcap, which is available from https://npcap.com/\n"
2071 "Otherwise, turn off promiscuous mode for this device.";
2075 "Please turn off promiscuous mode for this device.";
2079 #elif defined(__linux__)
2081 get_platform_pcap_failure_secondary_error_message(const char *open_status_str
)
2084 * The error string is the message provided by libpcap on
2085 * Linux if an attempt to open a PF_PACKET socket failed
2086 * with EAFNOSUPPORT. This probably means that either 1)
2087 * the kernel doesn't have PF_PACKET support configured in
2088 * or 2) this is a Flatpak version of Wireshark that's been
2089 * sandboxed in a way that disallows opening PF_PACKET
2092 * Suggest that the user find some other package of
2093 * Wireshark if they want to capture traffic and are
2094 * running a Flatpak of Wireshark or that they configure
2095 * PF_PACKET support back in if it's configured out.
2097 static const char af_notsup
[] =
2098 "socket: Address family not supported by protocol";
2100 if (strcmp(open_status_str
, af_notsup
) == 0) {
2102 "If you are running Wireshark from a Flatpak package, "
2103 "it does not support packet capture; you will need "
2104 "to run a different version of Wireshark in order "
2105 "to capture traffic.\n"
2107 "Otherwise, if your machine is running a kernel that "
2108 "was not configured with CONFIG_PACKET, that kernel "
2109 "does not support packet capture; you will need to "
2110 "use a kernel configured with CONFIG_PACKET.";
2116 get_platform_pcap_failure_secondary_error_message(const char *open_status_str _U_
)
2118 /* No such message for platforms not handled above. */
2124 get_pcap_failure_secondary_error_message(cap_device_open_status open_status
,
2125 const char *open_status_str
)
2127 const char *platform_secondary_error_message
;
2131 * On Windows, first make sure they *have* Npcap installed.
2135 "In order to capture packets, Npcap or WinPcap must be installed. See\n"
2137 " https://npcap.com/\n"
2139 "for a downloadable version of Npcap and for instructions on how to\n"
2145 * OK, now just return a largely platform-independent error that might
2146 * have platform-specific suggestions at the end (for example, suggestions
2147 * for how to get permission to capture).
2149 switch (open_status
) {
2151 case CAP_DEVICE_OPEN_NO_ERR
:
2152 case CAP_DEVICE_OPEN_WARNING_PROMISC_NOTSUP
:
2153 case CAP_DEVICE_OPEN_WARNING_TSTAMP_TYPE_NOTSUP
:
2154 case CAP_DEVICE_OPEN_WARNING_OTHER
:
2155 /* This should not happen, as those aren't errors. */
2158 case CAP_DEVICE_OPEN_ERROR_NO_SUCH_DEVICE
:
2159 case CAP_DEVICE_OPEN_ERROR_RFMON_NOTSUP
:
2160 case CAP_DEVICE_OPEN_ERROR_IFACE_NOT_UP
:
2162 * Not clear what suggestions to make for these cases.
2166 case CAP_DEVICE_OPEN_ERROR_PERM_DENIED
:
2167 case CAP_DEVICE_OPEN_ERROR_PROMISC_PERM_DENIED
:
2169 * This is a permissions error, so no need to specify any other
2173 "Please check to make sure you have sufficient permissions."
2174 PLATFORM_PERMISSIONS_SUGGESTION
;
2177 case CAP_DEVICE_OPEN_ERROR_OTHER
:
2178 case CAP_DEVICE_OPEN_ERROR_GENERIC
:
2180 * We don't know what kind of error it is. See if there's a hint
2181 * in the error string; if not, throw all generic suggestions at
2184 * First, check for some text that pops up in some errors.
2185 * Do platform-specific checks first.
2187 platform_secondary_error_message
=
2188 get_platform_pcap_failure_secondary_error_message(open_status_str
);
2189 if (platform_secondary_error_message
!= NULL
) {
2190 /* We got one, so return it. */
2191 return platform_secondary_error_message
;
2195 * Not one of those particular problems. Was this a "generic"
2196 * error from pcap_open_live() or pcap_open(), in which case
2197 * it might be a permissions error?
2199 if (open_status
== CAP_DEVICE_OPEN_ERROR_GENERIC
) {
2202 "Please check to make sure you have sufficient permissions, and that you have "
2203 "the proper interface or pipe specified."
2204 PLATFORM_PERMISSIONS_SUGGESTION
;
2207 * This is not a permissions error, so no need to suggest
2208 * checking permissions.
2211 "Please check that you have the proper interface or pipe specified.";
2217 * This is not a permissions error, so no need to suggest
2218 * checking permissions.
2221 "Please check that you have the proper interface or pipe specified.";
2226 #endif /* HAVE_LIBPCAP */
2229 * Editor modelines - https://www.wireshark.org/tools/modelines.html
2234 * indent-tabs-mode: t
2237 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
2238 * :indentSize=8:tabSize=8:noTabs=false: