[Openvpn-devel] wintun: upgrade error message in case of ring registration failure

Message ID 20200205183841.1118-1-simon@rozman.si
State Accepted
Headers show
Series [Openvpn-devel] wintun: upgrade error message in case of ring registration failure | expand

Commit Message

Simon Rozman Feb. 5, 2020, 7:38 a.m. UTC
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.

Signed-off-by: Simon Rozman <simon@rozman.si>
---
 src/openvpn/ring_buffer.h     |  2 +-
 src/openvpn/tun.c             | 18 +++++++++++++++---
 src/openvpnserv/interactive.c |  3 +--
 3 files changed, 17 insertions(+), 6 deletions(-)

Comments

Lev Stipakov Feb. 5, 2020, 8:37 a.m. UTC | #1
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.
<div dir="ltr"><div dir="ltr"><div dir="ltr">Hi,<div><br></div></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">-            msg(M_NONFATAL, &quot;Failed to register ring buffers: %lu&quot;, GetLastError());<br>
</blockquote><div>&lt;skip&gt; </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">+            switch (GetLastError())<br></blockquote><div>&lt;skip&gt; </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">+                default:<br>
+                    msg(M_NONFATAL | M_ERRNO, &quot;Failed to register ring buffers&quot;);<br></blockquote><div><br></div><div>I think it would make sense to print the error code / error message (with strerror_win32) in the &quot;default&quot; case.</div></div></div></div>
Simon Rozman March 9, 2020, 2:42 a.m. UTC | #2
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 <lstipakov@gmail.com> 
Sent: Wednesday, February 5, 2020 8:38 PM
To: Simon Rozman <simon@rozman.si>
Cc: openvpn-devel <openvpn-devel@lists.sourceforge.net>
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.
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=utf-8"><meta name=Generator content="Microsoft Word 15 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
	{font-family:"Cambria Math";
	panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
	{font-family:Calibri;
	panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
	{font-family:Consolas;
	panose-1:2 11 6 9 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
	{margin:0cm;
	margin-bottom:.0001pt;
	font-size:11.0pt;
	font-family:"Calibri",sans-serif;}
span.EmailStyle19
	{mso-style-type:personal-reply;
	font-family:"Calibri",sans-serif;
	color:windowtext;}
.MsoChpDefault
	{mso-style-type:export-only;
	font-family:"Calibri",sans-serif;
	mso-fareast-language:EN-US;}
@page WordSection1
	{size:612.0pt 792.0pt;
	margin:70.85pt 70.85pt 70.85pt 70.85pt;}
div.WordSection1
	{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=SL link="#0563C1" vlink="#954F72"><div class=WordSection1><p class=MsoNormal><span lang=EN-GB style='mso-fareast-language:EN-US'>Hi,<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='mso-fareast-language:EN-US'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span lang=EN-GB style='mso-fareast-language:EN-US'>I believe M_ERRNO should handle this…<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='mso-fareast-language:EN-US'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span lang=EN-GB style='mso-fareast-language:EN-US'>After the register_ring_buffers() exits, the Win32 error code is left in GetLastError().<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='mso-fareast-language:EN-US'>msg(</span>M_ERRNO<span lang=EN-GB style='mso-fareast-language:EN-US'>) picks it from there and formats a human-readable message.<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-GB style='mso-fareast-language:EN-US'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span lang=EN-GB style='mso-fareast-language:EN-US'>Regards, Simon<o:p></o:p></span></p><p class=MsoNormal><span style='mso-fareast-language:EN-US'><o:p>&nbsp;</o:p></span></p><div style='border:none;border-left:solid blue 1.5pt;padding:0cm 0cm 0cm 4.0pt'><div><div style='border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0cm 0cm 0cm'><p class=MsoNormal><b><span lang=EN-US>From:</span></b><span lang=EN-US> Lev Stipakov &lt;lstipakov@gmail.com&gt; <br><b>Sent:</b> Wednesday, February 5, 2020 8:38 PM<br><b>To:</b> Simon Rozman &lt;simon@rozman.si&gt;<br><b>Cc:</b> openvpn-devel &lt;openvpn-devel@lists.sourceforge.net&gt;<br><b>Subject:</b> Re: [Openvpn-devel] [PATCH] wintun: upgrade error message in case of ring registration failure<o:p></o:p></span></p></div></div><p class=MsoNormal><o:p>&nbsp;</o:p></p><div><div><div><p class=MsoNormal>Hi,<o:p></o:p></p><div><p class=MsoNormal><o:p>&nbsp;</o:p></p></div></div><div><blockquote style='border:none;border-left:solid #CCCCCC 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-right:0cm'><p class=MsoNormal>-&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; msg(M_NONFATAL, &quot;Failed to register ring buffers: %lu&quot;, GetLastError());<o:p></o:p></p></blockquote><div><p class=MsoNormal>&lt;skip&gt;&nbsp;<o:p></o:p></p></div><blockquote style='border:none;border-left:solid #CCCCCC 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-right:0cm'><p class=MsoNormal>+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch (GetLastError())<o:p></o:p></p></blockquote><div><p class=MsoNormal>&lt;skip&gt;&nbsp;<o:p></o:p></p></div><blockquote style='border:none;border-left:solid #CCCCCC 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-right:0cm'><p class=MsoNormal>+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:<br>+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; msg(M_NONFATAL | M_ERRNO, &quot;Failed to register ring buffers&quot;);<o:p></o:p></p></blockquote><div><p class=MsoNormal><o:p>&nbsp;</o:p></p></div><div><p class=MsoNormal>I think it would make sense to print the error code / error message (with strerror_win32) in the &quot;default&quot; case.<o:p></o:p></p></div></div></div></div></div></div></body></html>
Lev Stipakov March 9, 2020, 8:47 p.m. UTC | #3
Sorry, my bad - this is correct.

Compiled, tested with MSVC,

Acked-by: Lev Stipakov <lstipakov@gmail.com>
Gert Doering March 9, 2020, 9:39 p.m. UTC | #4
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 <simon@rozman.si>
     Acked-by: Lev Stipakov <lstipakov@gmail.com>
     Message-Id: <20200205183841.1118-1-simon@rozman.si>
     URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg19367.html
     Signed-off-by: Gert Doering <gert@greenie.muc.de>


--
kind regards,

Gert Doering

Patch

diff --git a/src/openvpn/ring_buffer.h b/src/openvpn/ring_buffer.h
index 3522c984..af46f106 100644
--- a/src/openvpn/ring_buffer.h
+++ b/src/openvpn/ring_buffer.h
@@ -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,
diff --git a/src/openvpn/tun.c b/src/openvpn/tun.c
index af09e676..27fcb785 100644
--- a/src/openvpn/tun.c
+++ b/src/openvpn/tun.c
@@ -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);
diff --git a/src/openvpnserv/interactive.c b/src/openvpnserv/interactive.c
index 6e72a141..5b250225 100644
--- a/src/openvpnserv/interactive.c
+++ b/src/openvpnserv/interactive.c
@@ -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;