[Openvpn-devel,v2,5/7] wintun: add support for --dev-node

Message ID 20191220161117.1434-5-simon@rozman.si
State Accepted
Headers show
Series
  • [Openvpn-devel,v2,1/7] tun.c: make Windows device lookup functions more general
Related show

Commit Message

Simon Rozman Dec. 20, 2019, 4:11 p.m.
Signed-off-by: Simon Rozman <simon@rozman.si>
---
 src/openvpn/tun.c | 38 ++++++++++++++++++++------------------
 1 file changed, 20 insertions(+), 18 deletions(-)

Comments

Lev Stipakov Dec. 21, 2019, 11:05 a.m. | #1
Compared with v1 - the difference is more helpful error message.

Compiled, tested.

Acked-by: Lev Stipakov <lstipakov@gmail.com>

pe 20. jouluk. 2019 klo 18.14 Simon Rozman (simon@rozman.si) kirjoitti:

> Signed-off-by: Simon Rozman <simon@rozman.si>
> ---
>  src/openvpn/tun.c | 38 ++++++++++++++++++++------------------
>  1 file changed, 20 insertions(+), 18 deletions(-)
>
> diff --git a/src/openvpn/tun.c b/src/openvpn/tun.c
> index f56682ef..18f06bb6 100644
> --- a/src/openvpn/tun.c
> +++ b/src/openvpn/tun.c
> @@ -6196,7 +6196,6 @@ static void
>  tun_open_device(struct tuntap *tt, const char *dev_node, const char
> **device_guid)
>  {
>      struct gc_arena gc = gc_new();
> -    char tuntap_device_path[256];
>      const struct tap_reg* tap_reg = get_tap_reg(&gc);
>      const struct panel_reg* panel_reg = get_panel_reg(&gc);
>      const struct device_instance_id_interface*
> device_instance_id_interface = get_device_instance_id_interface(&gc);
> @@ -6209,31 +6208,34 @@ tun_open_device(struct tuntap *tt, const char
> *dev_node, const char **device_gui
>       */
>      if (dev_node)
>      {
> +        bool is_picked_device_wintun = false;
> +
>          /* Get the device GUID for the device specified with --dev-node.
> */
> -        *device_guid = get_device_guid(dev_node, actual_buffer,
> sizeof(actual_buffer), NULL, tap_reg, panel_reg, &gc);
> +        *device_guid = get_device_guid(dev_node, actual_buffer,
> sizeof(actual_buffer), &is_picked_device_wintun, tap_reg, panel_reg, &gc);
>
>          if (!*device_guid)
>          {
> -            msg(M_FATAL, "TAP-Windows adapter '%s' not found", dev_node);
> +            msg(M_FATAL, "Adapter '%s' not found", dev_node);
>          }
>
> -        /* Open Windows TAP-Windows adapter */
> -        openvpn_snprintf(tuntap_device_path, sizeof(tuntap_device_path),
> "%s%s%s",
> -                         USERMODEDEVICEDIR,
> -                         *device_guid,
> -                         TAP_WIN_SUFFIX);
> -
> -        tt->hand = CreateFile(tuntap_device_path,
> -                              GENERIC_READ | GENERIC_WRITE,
> -                              0,                /* was: FILE_SHARE_READ */
> -                              0,
> -                              OPEN_EXISTING,
> -                              FILE_ATTRIBUTE_SYSTEM |
> FILE_FLAG_OVERLAPPED,
> -                              0);
> +        if (tt->wintun)
> +        {
> +            if (!is_picked_device_wintun)
> +            {
> +                msg(M_FATAL, "Adapter '%s' is TAP-Windows, Wintun
> expected. If you want to use this device, remove --windows-driver wintun.",
> dev_node);
> +            }
> +        }
> +        else
> +        {
> +            if (is_picked_device_wintun)
> +            {
> +                msg(M_FATAL, "Adapter '%s' is Wintun, TAP-Windows
> expected. If you want to use this device, add --windows-driver wintun.",
> dev_node);
> +            }
> +        }
>
> -        if (tt->hand == INVALID_HANDLE_VALUE)
> +        if (!tun_try_open_device(tt, *device_guid,
> device_instance_id_interface))
>          {
> -            msg(M_ERR, "CreateFile failed on TAP device: %s",
> tuntap_device_path);
> +            msg(M_FATAL, "Failed to open %s adapter: %s", tt->wintun ?
> "Wintun" : "TAP-Windows", dev_node);
>          }
>      }
>      else
> --
> 2.24.1.windows.2
>
>
>
> _______________________________________________
> Openvpn-devel mailing list
> Openvpn-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/openvpn-devel
>
Gert Doering Dec. 22, 2019, 7:14 p.m. | #2
Your patch has been applied to the master branch.

Compile-tested on Ubuntu 1604/mingw, stared a bit at the code, seems
to make sense :-)

(I do wonder if it makes sense to just use the driver we found if
--dev-mode was specified, and auto-sense whether this should be handled
as tap6 or wintun - after all, if one option is specifying exactly what
the user wants, why require a second option to be set to something we
can infer?  But that's fine tuning for special cases)

commit d7142c7d883602c6083f65037a52fa1a9df10861
Author: Simon Rozman
Date:   Fri Dec 20 17:11:15 2019 +0100

     wintun: add support for --dev-node

     Signed-off-by: Simon Rozman <simon@rozman.si>
     Acked-by: Lev Stipakov <lstipakov@gmail.com>
     Message-Id: <20191220161117.1434-5-simon@rozman.si>
     URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg19282.html
     Signed-off-by: Gert Doering <gert@greenie.muc.de>


--
kind regards,

Gert Doering

Patch

diff --git a/src/openvpn/tun.c b/src/openvpn/tun.c
index f56682ef..18f06bb6 100644
--- a/src/openvpn/tun.c
+++ b/src/openvpn/tun.c
@@ -6196,7 +6196,6 @@  static void
 tun_open_device(struct tuntap *tt, const char *dev_node, const char **device_guid)
 {
     struct gc_arena gc = gc_new();
-    char tuntap_device_path[256];
     const struct tap_reg* tap_reg = get_tap_reg(&gc);
     const struct panel_reg* panel_reg = get_panel_reg(&gc);
     const struct device_instance_id_interface* device_instance_id_interface = get_device_instance_id_interface(&gc);
@@ -6209,31 +6208,34 @@  tun_open_device(struct tuntap *tt, const char *dev_node, const char **device_gui
      */
     if (dev_node)
     {
+        bool is_picked_device_wintun = false;
+
         /* Get the device GUID for the device specified with --dev-node. */
-        *device_guid = get_device_guid(dev_node, actual_buffer, sizeof(actual_buffer), NULL, tap_reg, panel_reg, &gc);
+        *device_guid = get_device_guid(dev_node, actual_buffer, sizeof(actual_buffer), &is_picked_device_wintun, tap_reg, panel_reg, &gc);
 
         if (!*device_guid)
         {
-            msg(M_FATAL, "TAP-Windows adapter '%s' not found", dev_node);
+            msg(M_FATAL, "Adapter '%s' not found", dev_node);
         }
 
-        /* Open Windows TAP-Windows adapter */
-        openvpn_snprintf(tuntap_device_path, sizeof(tuntap_device_path), "%s%s%s",
-                         USERMODEDEVICEDIR,
-                         *device_guid,
-                         TAP_WIN_SUFFIX);
-
-        tt->hand = CreateFile(tuntap_device_path,
-                              GENERIC_READ | GENERIC_WRITE,
-                              0,                /* was: FILE_SHARE_READ */
-                              0,
-                              OPEN_EXISTING,
-                              FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED,
-                              0);
+        if (tt->wintun)
+        {
+            if (!is_picked_device_wintun)
+            {
+                msg(M_FATAL, "Adapter '%s' is TAP-Windows, Wintun expected. If you want to use this device, remove --windows-driver wintun.", dev_node);
+            }
+        }
+        else
+        {
+            if (is_picked_device_wintun)
+            {
+                msg(M_FATAL, "Adapter '%s' is Wintun, TAP-Windows expected. If you want to use this device, add --windows-driver wintun.", dev_node);
+            }
+        }
 
-        if (tt->hand == INVALID_HANDLE_VALUE)
+        if (!tun_try_open_device(tt, *device_guid, device_instance_id_interface))
         {
-            msg(M_ERR, "CreateFile failed on TAP device: %s", tuntap_device_path);
+            msg(M_FATAL, "Failed to open %s adapter: %s", tt->wintun ? "Wintun" : "TAP-Windows", dev_node);
         }
     }
     else