2 * Utility routines for packet capture
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
36 #ifdef HAVE_SYS_TYPES_H
37 # include <sys/types.h>
40 #ifdef HAVE_SYS_SOCKET_H
41 #include <sys/socket.h>
47 #include "capture_ifinfo.h"
48 #include "capture-pcap-util.h"
49 #include "capture-pcap-util-int.h"
51 #include "wsutil/file_util.h"
54 #include <netinet/in.h>
58 #include "capture_win_ifnames.h" /* windows friendly interface names */
62 * Given an interface name, find the "friendly name" and interface
63 * type for the interface.
66 #if defined(__APPLE__)
68 #include <CoreFoundation/CoreFoundation.h>
69 #include <SystemConfiguration/SystemConfiguration.h>
74 * On OS X, we get the "friendly name" and interface type for the interface
75 * from the System Configuration framework.
77 * To find the System Configuration framework information for the
78 * interface, we get all the interfaces that the System Configuration
79 * framework knows about and look for the one with a "BSD name" matching
82 * If we find it, we use its "localized display name", if it has one, as
83 * the "friendly name".
85 * As for the interface type:
87 * Yes, fetching all the network addresses for an interface gets you an
88 * AF_LINK address, of type "struct sockaddr_dl", and, yes, that includes
89 * an SNMP MIB-II ifType value.
91 * However, it's IFT_ETHER, i.e. Ethernet, for AirPort interfaces,
92 * not IFT_IEEE80211 (which isn't defined in OS X in any case).
94 * Perhaps some other BSD-flavored OSes won't make this mistake;
95 * however, FreeBSD 7.0 and OpenBSD 4.2, at least, appear to have
96 * made the same mistake, at least for my Belkin ZyDAS stick.
98 * SCNetworkInterfaceGetInterfaceType() will get the interface
99 * type. The interface type is a CFString, and:
101 * kSCNetworkInterfaceTypeIEEE80211 means IF_WIRELESS;
102 * kSCNetworkInterfaceTypeBluetooth means IF_BLUETOOTH;
103 * kSCNetworkInterfaceTypeModem or
104 * kSCNetworkInterfaceTypePPP or
105 * maybe kSCNetworkInterfaceTypeWWAN means IF_DIALUP
108 add_unix_interface_ifinfo(if_info_t *if_info, const char *name,
109 const char *description _U_)
111 CFStringRef name_CFString;
112 CFArrayRef interfaces;
113 CFIndex num_interfaces;
115 SCNetworkInterfaceRef interface;
116 CFStringRef bsdname_CFString;
117 CFStringRef friendly_name_CFString;
118 CFStringRef interface_type_CFString;
120 interfaces = SCNetworkInterfaceCopyAll();
121 if (interfaces == NULL) {
123 * Couldn't get a list of interfaces.
128 name_CFString = CFStringCreateWithCString(kCFAllocatorDefault,
129 name, kCFStringEncodingUTF8);
130 if (name_CFString == NULL) {
132 * Couldn't convert the interface name to a CFString.
134 CFRelease(interfaces);
138 num_interfaces = CFArrayGetCount(interfaces);
139 for (i = 0; i < num_interfaces; i++) {
140 interface = (SCNetworkInterfaceRef)CFArrayGetValueAtIndex(interfaces, i);
141 bsdname_CFString = SCNetworkInterfaceGetBSDName(interface);
142 if (bsdname_CFString == NULL) {
144 * This interface has no BSD name, so it's not
145 * a regular network interface.
149 if (CFStringCompare(name_CFString, bsdname_CFString, 0) == 0) {
151 * This is the interface.
152 * First, get the friendly name.
154 friendly_name_CFString = SCNetworkInterfaceGetLocalizedDisplayName(interface);
155 if (friendly_name_CFString != NULL)
156 if_info->friendly_name = CFString_to_C_string(friendly_name_CFString);
159 * Now get the interface type.
161 interface_type_CFString = SCNetworkInterfaceGetInterfaceType(interface);
162 if (CFStringCompare(interface_type_CFString,
163 kSCNetworkInterfaceTypeIEEE80211, 0) == kCFCompareEqualTo)
164 if_info->type = IF_WIRELESS;
165 else if (CFStringCompare(interface_type_CFString,
166 kSCNetworkInterfaceTypeBluetooth, 0) == kCFCompareEqualTo)
167 if_info->type = IF_BLUETOOTH;
168 else if (CFStringCompare(interface_type_CFString,
169 kSCNetworkInterfaceTypeModem, 0) == kCFCompareEqualTo)
170 if_info->type = IF_DIALUP;
171 else if (CFStringCompare(interface_type_CFString,
172 kSCNetworkInterfaceTypePPP, 0) == kCFCompareEqualTo)
173 if_info->type = IF_DIALUP;
174 else if (CFStringCompare(interface_type_CFString,
175 kSCNetworkInterfaceTypeWWAN, 0) == kCFCompareEqualTo)
176 if_info->type = IF_DIALUP;
178 if_info->type = IF_WIRED;
183 CFRelease(interfaces);
185 #elif defined(__linux__)
187 * Linux doesn't offer any form of "friendly name", but you can
188 * determine an interface type to some degree.
191 add_unix_interface_ifinfo(if_info_t *if_info, const char *name,
192 const char *description _U_)
198 * Look for /sys/class/net/{device}/wireless. If it exists,
199 * it's a wireless interface.
201 wireless_path = g_strdup_printf("/sys/class/net/%s/wireless", name);
202 if (wireless_path != NULL) {
203 if (ws_stat64(wireless_path, &statb) == 0)
204 if_info->type = IF_WIRELESS;
205 g_free(wireless_path);
207 if (if_info->type == IF_WIRED) {
209 * We still don't know what it is. Check for
210 * Bluetooth and USB devices.
212 if (strstr(name, "bluetooth") != NULL) {
214 * XXX - this is for raw Bluetooth capture; what
215 * about IP-over-Bluetooth devices?
217 if_info->type = IF_BLUETOOTH;
218 } else if (strstr(name, "usbmon") != NULL)
219 if_info->type = IF_USB;
224 * On other UN*Xes, if there is a description, it's a friendly
225 * name, and there is no vendor description. ("Other UN*Xes"
226 * currently means "FreeBSD and OpenBSD".)
229 add_unix_interface_ifinfo(if_info_t *if_info, const char *name _U_,
230 const char *description)
232 if_info->friendly_name = g_strdup(description);
237 if_info_new(const char *name, const char *description, gboolean loopback)
241 const char *guid_text;
245 if_info = (if_info_t *)g_malloc(sizeof (if_info_t));
246 if_info->name = g_strdup(name);
247 if_info->friendly_name = NULL; /* default - unknown */
248 if_info->vendor_description = NULL;
249 if_info->type = IF_WIRED; /* default */
252 * Get the interface type.
254 * Much digging failed to reveal any obvious way to get something
255 * such as the SNMP MIB-II ifType value for an interface:
257 * http://www.iana.org/assignments/ianaiftype-mib
259 * by making some NDIS request. And even if there were such
260 * a way, there's no guarantee that the ifType reflects an
261 * interface type that a user would view as correct (for
262 * example, some systems report Wi-Fi interfaces as
263 * Ethernet interfaces).
265 * So we look for keywords in the vendor's interface
268 if (description && (strstr(description, "generic dialup") != NULL ||
269 strstr(description, "PPP/SLIP") != NULL)) {
270 if_info->type = IF_DIALUP;
271 } else if (description && (strstr(description, "Wireless") != NULL ||
272 strstr(description,"802.11") != NULL)) {
273 if_info->type = IF_WIRELESS;
274 } else if (description && strstr(description, "AirPcap") != NULL ||
275 strstr(name, "airpcap") != NULL) {
276 if_info->type = IF_AIRPCAP;
277 } else if (description && strstr(description, "Bluetooth") != NULL ) {
278 if_info->type = IF_BLUETOOTH;
279 } else if (description && strstr(description, "VMware") != NULL) {
281 * Bridge, NAT, or host-only interface on a VMware host.
283 * XXX - what about guest interfaces?
285 if_info->type = IF_VIRTUAL;
289 * On Windows, the "description" is a vendor description,
290 * and the friendly name isn't returned by WinPcap.
291 * Fetch it ourselves.
295 * Skip over the "\Device\NPF_" prefix in the device name,
298 if (strncmp("\\Device\\NPF_", name, 12) == 0)
299 guid_text = name + 12;
303 /* Now try to parse what remains as a GUID. */
304 if (parse_as_guid(guid_text, &guid)) {
306 * Success. Try to get a friendly name using the GUID.
307 * As this is a regular interface, the description is a
308 * vendor description.
310 if_info->friendly_name = get_interface_friendly_name_from_device_guid(&guid);
311 if_info->vendor_description = g_strdup(description);
314 * This is probably not a regular interface; we only
315 * support NT 5 (W2K) and later, so all regular interfaces
316 * should have GUIDs at the end of the name. Therefore,
317 * the description, if supplied, is a friendly name
318 * provided by WinPcap, and there is no vendor
321 if_info->friendly_name = g_strdup(description);
322 if_info->vendor_description = NULL;
326 * On UN*X, if there is a description, it's a friendly
327 * name, and there is no vendor description.
329 * Try the platform's way of getting a friendly name and
330 * interface type first.
332 * If that fails, then, for a loopback interface, give it the
333 * friendly name "Loopback" and, for VMware interfaces,
334 * give them the type IF_VIRTUAL.
336 add_unix_interface_ifinfo(if_info, name, description);
337 if (if_info->type == IF_WIRED) {
339 * This is the default interface type.
341 * Bridge, NAT, or host-only interfaces on VMWare hosts
342 * have the name vmnet[0-9]+. Guests might use a native
343 * (LANCE or E1000) driver or the vmxnet driver. Check
346 if (g_ascii_strncasecmp(name, "vmnet", 5) == 0)
347 if_info->type = IF_VIRTUAL;
348 else if (g_ascii_strncasecmp(name, "vmxnet", 6) == 0)
349 if_info->type = IF_VIRTUAL;
351 if (if_info->friendly_name == NULL) {
353 * We couldn't get interface information using platform-
356 * If this is a loopback interface, give it a
357 * "friendly name" of "Loopback".
360 if_info->friendly_name = g_strdup("Loopback");
362 if_info->vendor_description = NULL;
364 if_info->loopback = loopback;
365 if_info->addrs = NULL;
370 if_info_add_address(if_info_t *if_info, struct sockaddr *addr)
373 struct sockaddr_in *ai;
375 struct sockaddr_in6 *ai6;
378 switch (addr->sa_family) {
381 ai = (struct sockaddr_in *)(void *)addr;
382 if_addr = (if_addr_t *)g_malloc(sizeof(*if_addr));
383 if_addr->ifat_type = IF_AT_IPv4;
384 if_addr->addr.ip4_addr =
385 *((guint32 *)&(ai->sin_addr.s_addr));
386 if_info->addrs = g_slist_append(if_info->addrs, if_addr);
391 ai6 = (struct sockaddr_in6 *)(void *)addr;
392 if_addr = (if_addr_t *)g_malloc(sizeof(*if_addr));
393 if_addr->ifat_type = IF_AT_IPv6;
394 memcpy((void *)&if_addr->addr.ip6_addr,
395 (void *)&ai6->sin6_addr.s6_addr,
396 sizeof if_addr->addr.ip6_addr);
397 if_info->addrs = g_slist_append(if_info->addrs, if_addr);
403 #ifdef HAVE_PCAP_FINDALLDEVS
405 * Get all IP address information for the given interface.
408 if_info_ip(if_info_t *if_info, pcap_if_t *d)
413 for (a = d->addresses; a != NULL; a = a->next) {
415 if_info_add_address(if_info, a->addr);
419 #ifdef HAVE_PCAP_REMOTE
421 get_interface_list_findalldevs_ex(const char *source,
422 struct pcap_rmtauth *auth,
423 int *err, char **err_str)
426 pcap_if_t *alldevs, *dev;
428 char errbuf[PCAP_ERRBUF_SIZE];
430 if (pcap_findalldevs_ex((char *)source, auth, &alldevs, errbuf) == -1) {
431 *err = CANT_GET_INTERFACE_LIST;
433 *err_str = cant_get_if_list_error_message(errbuf);
437 if (alldevs == NULL) {
439 * No interfaces found.
441 *err = NO_INTERFACES_FOUND;
447 for (dev = alldevs; dev != NULL; dev = dev->next) {
448 if_info = if_info_new(dev->name, dev->description,
449 (dev->flags & PCAP_IF_LOOPBACK) ? TRUE : FALSE);
450 il = g_list_append(il, if_info);
451 if_info_ip(if_info, dev);
453 pcap_freealldevs(alldevs);
460 get_interface_list_findalldevs(int *err, char **err_str)
463 pcap_if_t *alldevs, *dev;
465 char errbuf[PCAP_ERRBUF_SIZE];
467 if (pcap_findalldevs(&alldevs, errbuf) == -1) {
468 *err = CANT_GET_INTERFACE_LIST;
470 *err_str = cant_get_if_list_error_message(errbuf);
474 if (alldevs == NULL) {
476 * No interfaces found.
478 *err = NO_INTERFACES_FOUND;
484 for (dev = alldevs; dev != NULL; dev = dev->next) {
485 if_info = if_info_new(dev->name, dev->description,
486 (dev->flags & PCAP_IF_LOOPBACK) ? TRUE : FALSE);
487 il = g_list_append(il, if_info);
488 if_info_ip(if_info, dev);
490 pcap_freealldevs(alldevs);
494 #endif /* HAVE_PCAP_FINDALLDEVS */
497 free_if_info_addr_cb(gpointer addr, gpointer user_data _U_)
503 free_if_cb(gpointer data, gpointer user_data _U_)
505 if_info_t *if_info = (if_info_t *)data;
507 g_free(if_info->name);
508 g_free(if_info->friendly_name);
509 g_free(if_info->vendor_description);
511 g_slist_foreach(if_info->addrs, free_if_info_addr_cb, NULL);
512 g_slist_free(if_info->addrs);
517 free_interface_list(GList *if_list)
519 g_list_foreach(if_list, free_if_cb, NULL);
520 g_list_free(if_list);
523 #if !defined(HAVE_PCAP_DATALINK_NAME_TO_VAL) || !defined(HAVE_PCAP_DATALINK_VAL_TO_NAME) || !defined(HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION)
526 const char *description;
530 #define DLT_CHOICE(code, description) { #code, description, code }
531 #define DLT_CHOICE_SENTINEL { NULL, NULL, 0 }
533 static struct dlt_choice dlt_choices[] = {
534 DLT_CHOICE(DLT_NULL, "BSD loopback"),
535 DLT_CHOICE(DLT_EN10MB, "Ethernet"),
536 DLT_CHOICE(DLT_IEEE802, "Token ring"),
537 DLT_CHOICE(DLT_ARCNET, "ARCNET"),
538 DLT_CHOICE(DLT_SLIP, "SLIP"),
539 DLT_CHOICE(DLT_PPP, "PPP"),
540 DLT_CHOICE(DLT_FDDI, "FDDI"),
541 DLT_CHOICE(DLT_ATM_RFC1483, "RFC 1483 IP-over-ATM"),
542 DLT_CHOICE(DLT_RAW, "Raw IP"),
543 DLT_CHOICE(DLT_SLIP_BSDOS, "BSD/OS SLIP"),
544 DLT_CHOICE(DLT_PPP_BSDOS, "BSD/OS PPP"),
545 DLT_CHOICE(DLT_ATM_CLIP, "Linux Classical IP-over-ATM"),
546 DLT_CHOICE(DLT_PPP_SERIAL, "PPP over serial"),
547 DLT_CHOICE(DLT_PPP_ETHER, "PPPoE"),
548 DLT_CHOICE(DLT_C_HDLC, "Cisco HDLC"),
549 DLT_CHOICE(DLT_IEEE802_11, "802.11"),
550 DLT_CHOICE(DLT_FRELAY, "Frame Relay"),
551 DLT_CHOICE(DLT_LOOP, "OpenBSD loopback"),
552 DLT_CHOICE(DLT_ENC, "OpenBSD encapsulated IP"),
553 DLT_CHOICE(DLT_LINUX_SLL, "Linux cooked"),
554 DLT_CHOICE(DLT_LTALK, "Localtalk"),
555 DLT_CHOICE(DLT_PFLOG, "OpenBSD pflog file"),
556 DLT_CHOICE(DLT_PRISM_HEADER, "802.11 plus Prism header"),
557 DLT_CHOICE(DLT_IP_OVER_FC, "RFC 2625 IP-over-Fibre Channel"),
558 DLT_CHOICE(DLT_SUNATM, "Sun raw ATM"),
559 DLT_CHOICE(DLT_IEEE802_11_RADIO, "802.11 plus BSD radio information header"),
560 DLT_CHOICE(DLT_APPLE_IP_OVER_IEEE1394, "Apple IP-over-IEEE 1394"),
561 DLT_CHOICE(DLT_ARCNET_LINUX, "Linux ARCNET"),
562 DLT_CHOICE(DLT_LINUX_IRDA, "Linux IrDA"),
563 DLT_CHOICE(DLT_IEEE802_11_RADIO_AVS, "802.11 plus AVS radio information header"),
567 #if !defined(HAVE_PCAP_DATALINK_NAME_TO_VAL)
569 pcap_datalink_name_to_val(const char *name)
573 for (i = 0; dlt_choices[i].name != NULL; i++) {
574 if (g_ascii_strcasecmp(dlt_choices[i].name + sizeof("DLT_") - 1,
576 return (dlt_choices[i].dlt);
580 #endif /* defined(HAVE_PCAP_DATALINK_NAME_TO_VAL) */
582 #if !defined(HAVE_PCAP_DATALINK_VAL_TO_NAME)
584 pcap_datalink_val_to_name(int dlt)
588 for (i = 0; dlt_choices[i].name != NULL; i++) {
589 if (dlt_choices[i].dlt == dlt)
590 return (dlt_choices[i].name + sizeof("DLT_") - 1);
594 #endif /* defined(HAVE_PCAP_DATALINK_VAL_TO_NAME) */
596 #if !defined(HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION)
598 pcap_datalink_val_to_description(int dlt)
602 for (i = 0; dlt_choices[i].name != NULL; i++) {
603 if (dlt_choices[i].dlt == dlt)
604 return (dlt_choices[i].description);
608 #endif /* defined(HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION) */
610 #endif /* !defined(HAVE_PCAP_DATALINK_VAL_TO_NAME) || !defined(HAVE_PCAP_DATALINK_VAL_TO_DESCRIPTION) */
613 free_linktype_cb(gpointer data, gpointer user_data _U_)
615 data_link_info_t *linktype_info = (data_link_info_t *)data;
617 g_free(linktype_info->name);
618 g_free(linktype_info->description);
622 free_if_capabilities(if_capabilities_t *caps)
624 g_list_foreach(caps->data_link_types, free_linktype_cb, NULL);
625 g_list_free(caps->data_link_types);
630 linktype_val_to_name(int dlt)
632 return pcap_datalink_val_to_name(dlt);
635 int linktype_name_to_val(const char *linktype)
637 return pcap_datalink_name_to_val(linktype);
640 #endif /* HAVE_LIBPCAP */