From: Guy Harris Date: Fri, 11 Mar 2022 07:53:17 +0000 (-0800) Subject: npf: don't have pcap_create() fail if the device doesn't exist. X-Git-Tag: libpcap-1.10.2~208 X-Git-Url: https://git.tcpdump.org/libpcap/commitdiff_plain/ba84dae08b45470f64a4c9cecaad227a848526dc npf: don't have pcap_create() fail if the device doesn't exist. Also, don't have it fail if the user doesn't have permission to open it. To quote the comment (which opens with a paragraph taken directly from the equivalent code in pcap-linux.c): If we can't open the device now, we won't be able to later, either. If the error is something that indicates that the device doesn't exist, or that they don't have permission to open the device - or perhaps that they don't have permission to get a list of devices, if PacketOpenAdapter() does that - the user will find that out when they try to activate the device; just return an empty list of time stamp types. Treating either of those as errors will, for example, cause "tcpdump -i " to fail, because it first tries to pass the interface name to pcap_create() and pcap_activate(), in order to handle OSes where interfaces can have names that are just numbers (stand up and say hello, Linux!), and, if pcap_activate() fails with a "no such device" error, checks whether the interface name is a valid number and, if so, tries to use it as an index in the list of interfaces. That means pcap_create() must succeed even for interfaces that don't exist, with the failure occurring at pcap_activate() time. (cherry picked from commit e8c0f491649c5067299b976b6b49b82814fb4c9a) --- diff --git a/pcap-npf.c b/pcap-npf.c index b6cd8861..25ed4acd 100644 --- a/pcap-npf.c +++ b/pcap-npf.c @@ -1591,10 +1591,46 @@ get_ts_types(const char *device, pcap_t *p, char *ebuf) if (adapter == NULL) { error = GetLastError(); - /* If we can't open the device now, we won't be able to later, either. */ - pcap_fmt_errmsg_for_win32_err(ebuf, PCAP_ERRBUF_SIZE, - error, "Error opening adapter"); - status = -1; + /* + * If we can't open the device now, we won't be + * able to later, either. + * + * If the error is something that indicates + * that the device doesn't exist, or that they + * don't have permission to open the device - or + * perhaps that they don't have permission to get + * a list of devices, if PacketOpenAdapter() does + * that - the user will find that out when they try + * to activate the device; just return an empty + * list of time stamp types. + * + * Treating either of those as errors will, for + * example, cause "tcpdump -i " to fail, + * because it first tries to pass the interface + * name to pcap_create() and pcap_activate(), + * in order to handle OSes where interfaces can + * have names that are just numbers (stand up + * and say hello, Linux!), and, if pcap_activate() + * fails with a "no such device" error, checks + * whether the interface name is a valid number + * and, if so, tries to use it as an index in + * the list of interfaces. + * + * That means pcap_create() must succeed even + * for interfaces that don't exist, with the + * failure occurring at pcap_activate() time. + */ + if (error == ERROR_BAD_UNIT || + error == ERROR_ACCESS_DENIED) { + p->tstamp_type_count = 0; + p->tstamp_type_list = NULL; + status = 0; + } else { + pcap_fmt_errmsg_for_win32_err(ebuf, + PCAP_ERRBUF_SIZE, error, + "Error opening adapter"); + status = -1; + } break; }