Skip to contentSkip to conversation

wintun: upgrade error message in case of ring registration failure

PATCH In review Simon Rozman 3 participants 5 messages Feb 5, 2020 – Mar 10, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 wintun: upgrade error message in case of ring registration failure

Rather than have the Interactive Service return a custom 0x20000004 (ERROR_REGISTER_RING_BUFFERS) error, return the true GetLastError() code that the TUN_IOCTL_REGISTER_RINGS provides.

---
 src/openvpn/ring_buffer.h     |  2 +-
 src/openvpn/tun.c             | 18 +++++++++++++++---
 src/openvpnserv/interactive.c |  3 +--
 3 files changed, 17 insertions(+), 6 deletions(-)
src/openvpn/ring_buffer.h +1 -1
index 3522c984..af46f106 100644
@@ -92,7 +92,7 @@ struct TUN_PACKET
  *                            that data is available for reading in send ring
  * @param receive_tail_moved  event set by openvpn to signal wintun
  *                            that data has been written to receive ring
- * @return                    true if registration is successful, false otherwise
+ * @return                    true if registration is successful, false otherwise - use GetLastError()
  */
 bool register_ring_buffers(HANDLE device,
                            struct tun_ring *send_ring,
src/openvpn/tun.c +15 -3
index af09e676..27fcb785 100644
@@ -5943,7 +5943,7 @@ tuntap_set_ip_addr(struct tuntap *tt,
 }
 
 static bool
-wintun_register_ring_buffer(struct tuntap *tt)
+wintun_register_ring_buffer(struct tuntap *tt, const char *device_guid)
 {
     bool ret = true;
 
@@ -5975,7 +5975,19 @@ wintun_register_ring_buffer(struct tuntap *tt)
             tt->rw_handle.read,
             tt->rw_handle.write))
         {
-            msg(M_NONFATAL, "Failed to register ring buffers: %lu", GetLastError());
+            switch (GetLastError())
+            {
+                case ERROR_ACCESS_DENIED:
+                    msg(M_FATAL, "Access denied registering ring buffers. Is this process run as SYSTEM?");
+                    break;
+
+                case ERROR_ALREADY_INITIALIZED:
+                    msg(M_NONFATAL, "Adapter %s is already in use", device_guid);
+                    break;
+
+                default:
+                    msg(M_NONFATAL | M_ERRNO, "Failed to register ring buffers");
+            }
             ret = false;
         }
         if (!RevertToSelf())
@@ -6197,7 +6209,7 @@ tun_try_open_device(struct tuntap *tt, const char *device_guid, const struct dev
     if (tt->windows_driver == WINDOWS_DRIVER_WINTUN)
     {
         /* Wintun adapter may be considered "open" after ring buffers are successfuly registered. */
-        if (!wintun_register_ring_buffer(tt))
+        if (!wintun_register_ring_buffer(tt, device_guid))
         {
             msg(D_TUNTAP_INFO, "Failed to register %s adapter ring buffers", device_guid);
             CloseHandle(tt->hand);
src/openvpnserv/interactive.c +1 -2
index 6e72a141..5b250225 100644
@@ -51,7 +51,6 @@
 #define ERROR_STARTUP_DATA           0x20000001
 #define ERROR_MESSAGE_DATA           0x20000002
 #define ERROR_MESSAGE_TYPE           0x20000003
-#define ERROR_REGISTER_RING_BUFFERS  0x20000004
 
 static SERVICE_STATUS_HANDLE service;
 static SERVICE_STATUS status = { .dwServiceType = SERVICE_WIN32_SHARE_PROCESS };
@@ -1308,8 +1307,8 @@ HandleRegisterRingBuffers(const register_ring_buffers_message_t *rrb, HANDLE ovp
     if (!register_ring_buffers(ring_buffer_handles->device, send_ring, receive_ring,
                                ring_buffer_handles->send_tail_moved, ring_buffer_handles->receive_tail_moved))
     {
+        err = GetLastError();
         MsgToEventLog(M_SYSERR, TEXT("Could not register ring buffers"));
-        err = ERROR_REGISTER_RING_BUFFERS;
     }
 
     return err;
Signature · 1 line
2.24.1.windows.2
#1 lore raw

Hi,

- msg(M_NONFATAL, "Failed to register ring buffers: %lu",

Quoted 2 lines

GetLastError());

<skip>

Quoted 2 lines

+ switch (GetLastError())

<skip>

Quoted 4 lines
(quoted diff) +2 -0
+                default:
+                    msg(M_NONFATAL | M_ERRNO, "Failed to register ring

buffers");

I think it would make sense to print the error code / error message (with strerror_win32) in the "default" case.

#2 lore raw

Hi,

I believe M_ERRNO should handle this…

After the register_ring_buffers() exits, the Win32 error code is left in GetLastError().

msg(M_ERRNO) picks it from there and formats a human-readable message.

Regards, Simon

From: Lev Stipakov <[email protected]...>
Sent: Wednesday, February 5, 2020 8:38 PM
To: Simon Rozman <[email protected]...>
Cc: openvpn-devel <[email protected]>
Subject: Re: [Openvpn-devel] [PATCH] wintun: upgrade error message in case of ring registration failure

Hi,

- msg(M_NONFATAL, "Failed to register ring buffers: %lu", GetLastError());

<skip>

+ switch (GetLastError())

<skip>

+ default:
+ msg(M_NONFATAL | M_ERRNO, "Failed to register ring buffers");

I think it would make sense to print the error code / error message (with strerror_win32) in the "default" case.

#4 lore raw
In reply to Simon Rozman

Your patch has been applied to the master branch.

"If Lev says so" :-) - code looks reasonable, test compiles fine (MinGW).

commit 089fbe653c3a8efd6dac8cc84448ccbe1d3aa887

Author: Simon Rozman
Date:   Wed Feb 5 19:38:41 2020 +0100
     wintun: upgrade error message in case of ring registration failure

     Signed-off-by: Simon Rozman <[email protected]...>
     Acked-by: Lev Stipakov <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg19367.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering