]> The Tcpdump Group git mirrors - libpcap/commitdiff
npf: don't have pcap_create() fail if the device doesn't exist.
authorGuy Harris <[email protected]>
Fri, 11 Mar 2022 07:53:17 +0000 (23:53 -0800)
committerGuy Harris <[email protected]>
Wed, 16 Mar 2022 04:56:53 +0000 (21:56 -0700)
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
<number>" 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)

pcap-npf.c

index b6cd8861f83b4159e4a69b11d542a26f16794e7e..25ed4acd241afd83db39552962ce9cf2ca4d1d67 100644 (file)
@@ -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 <number>" 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;
                }