Skip to contentSkip to conversation

Support of DNS domain for DHCP-less drivers

PATCH Superseded Lev Stipakov 3 participants 6 messages Apr 4, 2023 – Apr 11, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Support of DNS domain for DHCP-less drivers

From: Lev Stipakov <[email protected]...>

We set DNS domain either via interactve service or DHCP. When interactive service is not used, for example, when profiles are started by OpenVPNService, this option is not working for DCO and wintun.

This implements setting DNS domain via WMIC command, similar to implementation in interactive service. This is done when:

- interactive service is not used
- ip_win32_type is either METSH or IPAPI, which is
the case for DCO and wintun.

Fixes https://github.com/OpenVPN/openvpn/issues/306

Change-Id: I9ab51bf1c0774564204c75ecce9ebfb818db2f5b

---
 src/openvpn/options.c |  8 ++---
 src/openvpn/tun.c     | 72 ++++++++++++++++++++++++++++++++++---------
 src/openvpn/tun.h     |  6 ++++
 src/openvpn/win32.h   |  1 +
 4 files changed, 67 insertions(+), 20 deletions(-)
src/openvpn/options.c +3 -5
index 2680f268..36d54ceb 100644
@@ -3396,11 +3396,11 @@ options_postprocess_mutate_invariant(struct options *options)
 #ifdef _WIN32
     const int dev = dev_type_enum(options->dev, options->dev_type);
 
+    const bool dhcp = tuntap_maybe_dhcp(&options->tuntap_options);
+
     /* when using wintun/ovpn-dco, kernel doesn't send DHCP requests, so don't use it */
     if ((options->windows_driver == WINDOWS_DRIVER_WINTUN
-         || options->windows_driver == WINDOWS_DRIVER_DCO)
-        && (options->tuntap_options.ip_win32_type == IPW32_SET_DHCP_MASQ
-            || options->tuntap_options.ip_win32_type == IPW32_SET_ADAPTIVE))
+         || options->windows_driver == WINDOWS_DRIVER_DCO) && dhcp)
     {
         options->tuntap_options.ip_win32_type = IPW32_SET_NETSH;
     }
@@ -3408,8 +3408,6 @@ options_postprocess_mutate_invariant(struct options *options)
     if ((dev == DEV_TYPE_TUN || dev == DEV_TYPE_TAP) && !options->route_delay_defined)
     {
         /* delay may only be necessary when we perform DHCP handshake */
-        const bool dhcp = (options->tuntap_options.ip_win32_type == IPW32_SET_DHCP_MASQ)
-                          || (options->tuntap_options.ip_win32_type == IPW32_SET_ADAPTIVE);
         if ((options->mode == MODE_POINT_TO_POINT) && dhcp)
         {
             options->route_delay_defined = true;
src/openvpn/tun.c +57 -15

142 lines not on this page · raw message

src/openvpn/tun.h +6 -0
index e19e1a2e..0d8e2307 100644
@@ -668,6 +668,12 @@ tuntap_is_dco_win_timeout(struct tuntap *tt, int status)
 const char *
 print_windows_driver(enum windows_driver_type windows_driver);
 
+static inline bool
+tuntap_maybe_dhcp(struct tuntap_options *o)
+{
+    return o->ip_win32_type == IPW32_SET_DHCP_MASQ || o->ip_win32_type == IPW32_SET_ADAPTIVE;
+}
+
 #else  /* ifdef _WIN32 */
 
 static inline bool
src/openvpn/win32.h +1 -0
index 72ffb012..36059662 100644
@@ -38,6 +38,7 @@
 #define WIN_ROUTE_PATH_SUFFIX "\\system32\\route.exe"
 #define WIN_IPCONFIG_PATH_SUFFIX "\\system32\\ipconfig.exe"
 #define WIN_NET_PATH_SUFFIX "\\system32\\net.exe"
+#define WMIC_PATH_SUFFIX "\\system32\\wbem\\wmic.exe"
 
 /*
  * Win32-specific OpenVPN code, targeted at the mingw
Signature · 1 line
2.23.0.windows.1
#1 lore raw

Hi,

Quoted 102 lines · Lev Stipakov

From: Lev Stipakov <[email protected]...>

We set DNS domain either via interactve service or DHCP. When interactive service is not used, for example, when profiles are started by OpenVPNService, this option is not working for DCO and wintun.

This implements setting DNS domain via WMIC command, similar to implementation in interactive service. This is done when:

- interactive service is not used
- ip_win32_type is either METSH or IPAPI, which is
the case for DCO and wintun.

Fixes https://github.com/OpenVPN/openvpn/issues/306

Change-Id: I9ab51bf1c0774564204c75ecce9ebfb818db2f5b

---
 src/openvpn/options.c |  8 ++---
 src/openvpn/tun.c     | 72 ++++++++++++++++++++++++++++++++++---------
 src/openvpn/tun.h     |  6 ++++
 src/openvpn/win32.h   |  1 +
 4 files changed, 67 insertions(+), 20 deletions(-)
src/openvpn/options.c +2 -0
index 2680f268..36d54ceb 100644
@@ -3396,11 +3396,11 @@ options_postprocess_mutate_invariant(struct
 options *options)
 #ifdef _WIN32
     const int dev = dev_type_enum(options->dev, options->dev_type);
 
+    const bool dhcp = tuntap_maybe_dhcp(&options->tuntap_options);
+
     /* when using wintun/ovpn-dco, kernel doesn't send DHCP requests, so

don't use it */

     if ((options->windows_driver == WINDOWS_DRIVER_WINTUN
src/openvpn/options.c +0 -3
-         || options->windows_driver == WINDOWS_DRIVER_DCO)
-        && (options->tuntap_options.ip_win32_type == IPW32_SET_DHCP_MASQ
-            || options->tuntap_options.ip_win32_type ==

IPW32_SET_ADAPTIVE))

src/openvpn/options.c +1 -2
+         || options->windows_driver == WINDOWS_DRIVER_DCO) && dhcp)
     {
         options->tuntap_options.ip_win32_type = IPW32_SET_NETSH;
     }
@@ -3408,8 +3408,6 @@ options_postprocess_mutate_invariant(struct options
 *options)
     if ((dev == DEV_TYPE_TUN || dev == DEV_TYPE_TAP) &&
 !options->route_delay_defined)
     {
         /* delay may only be necessary when we perform DHCP handshake */
-        const bool dhcp = (options->tuntap_options.ip_win32_type ==
 IPW32_SET_DHCP_MASQ)
-                          || (options->tuntap_options.ip_win32_type ==
IPW32_SET_ADAPTIVE);
         if ((options->mode == MODE_POINT_TO_POINT) && dhcp)
         {
             options->route_delay_defined = true;
src/openvpn/tun.c +2 -0
index 2ebe4809..c2cc7e26 100644
@@ -84,6 +84,8 @@ static void netsh_set_dns6_servers(const struct in6_addr
 *addr_list,
 
 static void netsh_command(const struct argv *a, int n, int msglevel);
 
+static void exec_command(const char *prefix, const struct argv *a, int n,
 int msglevel);
+
 static const char *netsh_get_id(const char *dev_node, struct gc_arena

*gc);

static bool

src/openvpn/tun.c +16 -0
@@ -324,6 +326,22 @@ out:
     return ret;
 }
 
+static void
+wmic_do_dns_domain(bool add, const struct tuntap *tt)
+{
+    if (!tt->options.domain)
+    {
+        return;
+    }
+
+    struct argv argv = argv_new();
+    argv_printf(&argv, "%s%s nicconfig where (InterfaceIndex=%ld) call
 SetDNSDomain %s",
+                get_win_sys_path(), WMIC_PATH_SUFFIX, tt->adapter_index,
 add ? tt->options.domain : "");
+    exec_command("WMIC", &argv, 1, M_WARN);
+
+    argv_free(&argv);
+}
+
 #endif /* ifdef _WIN32 */

#ifdef TARGET_SOLARIS

src/openvpn/tun.c do_ifconfig_ipv6(struct tuntap *tt, const char · line 1215

Quoted 8 lines
src/openvpn/tun.c +2 -0
@@ -1190,6 +1208,11 @@ do_ifconfig_ipv6(struct tuntap *tt, const char
 *ifname, int tun_mtu,
         /* set ipv6 dns servers if any are specified */
         netsh_set_dns6_servers(tt->options.dns6, tt->options.dns6_len,
 tt->adapter_index);
         windows_set_mtu(tt->adapter_index, AF_INET6, tun_mtu);
+
+        if (!tt->did_ifconfig_setup && !tuntap_maybe_dhcp(&tt->options))
 

Adding "!tuntap_maybe_dhcp()" here looks wrong. We must set the domain if not set during IPv4 processing and that is indicated by did_ifconfig_setup. Just as done in the case handled by interactive-service above.

For example: in case there is no IPv4 address, the domain will not be set at this point but tuntap_may_dhcp() could still be true if the driver is tap-windows6. And the above will wrongly skip the wmic call. Even otherwise, adding a seemingly dhcp-related check in IPv6 case will only confuse us in future.

Same in close_tun() to match.

Once that check is removed there is no need to introduce tuntap_maybe_dhcp() in this patch at all. This can be kept simple by just adding wmic_do_dns_domain() and calling it in two places under the same conditions as done using service. Plus the netsh_command() to exec_command() generalization. That makes it easier to review as well.

As for refactoring/cleaning up, we can address it separately as the whole ip-win32 thing is a mess that could benefit from a thorough cleanup.

+ {

Quoted 88 lines
src/openvpn/tun.c +2 -0
+            wmic_do_dns_domain(true, tt);
+        }
     }
 #else /* platforms we have no IPv6 code for */
     msg(M_FATAL, "Sorry, but I don't know how to do IPv6 'ifconfig'

commands on this operating system. You should ifconfig your TUN/TAP device manually or use an --up script.");

src/openvpn/tun.c +1 -1
@@ -1525,7 +1548,7 @@ do_ifconfig_ipv4(struct tuntap *tt, const char
 *ifname, int tun_mtu,
             ifname, ifconfig_local,
             ifconfig_remote_netmask);
     }
-    else if (tt->options.ip_win32_type == IPW32_SET_DHCP_MASQ ||
 tt->options.ip_win32_type == IPW32_SET_ADAPTIVE)
+    else if (tuntap_maybe_dhcp(&tt->options))
     {
         /* Let the DHCP configure the interface. */
     }
src/openvpn/tun.c +10 -3
@@ -1535,11 +1558,18 @@ do_ifconfig_ipv4(struct tuntap *tt, const char
 *ifname, int tun_mtu,
         do_dns_service(true, AF_INET, tt);
         do_dns_domain_service(true, tt);
     }
-    else if (tt->options.ip_win32_type == IPW32_SET_NETSH)
+    else
     {
-        netsh_ifconfig(&tt->options, tt->adapter_index, tt->local,
-                       tt->adapter_netmask, NI_IP_NETMASK|NI_OPTIONS);
+        if (tt->options.ip_win32_type == IPW32_SET_NETSH)
+        {
+            netsh_ifconfig(&tt->options, tt->adapter_index, tt->local,
+                           tt->adapter_netmask, NI_IP_NETMASK |
 NI_OPTIONS);
+        }
+
+        wmic_do_dns_domain(true, tt);
     }
+
+
     if (tt->options.msg_channel)
     {
         do_set_mtu_service(tt, AF_INET, tun_mtu);
src/openvpn/tun.c +1 -5
@@ -5238,12 +5268,8 @@ dhcp_renew(const struct tuntap *tt)
     }
 }
 
-/*
- * netsh functions
- */
-
 static void
-netsh_command(const struct argv *a, int n, int msglevel)
+exec_command(const char *prefix, const struct argv *a, int n, int
 msglevel)
 {
     int i;
     for (i = 0; i < n; ++i)
src/openvpn/tun.c +2 -2
@@ -5251,8 +5277,8 @@ netsh_command(const struct argv *a, int n, int
 msglevel)
         bool status;
         management_sleep(0);
         netcmd_semaphore_lock();
-        argv_msg_prefix(M_INFO, a, "NETSH");
-        status = openvpn_execve_check(a, NULL, 0, "ERROR: netsh command
 failed");
+        argv_msg_prefix(M_INFO, a, prefix);
+        status = openvpn_execve_check(a, NULL, 0, "ERROR: command
 failed");
         netcmd_semaphore_release();
         if (status)
         {
src/openvpn/tun.c +7 -1
@@ -5260,7 +5286,13 @@ netsh_command(const struct argv *a, int n, int
 msglevel)
         }
         management_sleep(4);
     }
-    msg(msglevel, "NETSH: command failed");
+    msg(msglevel, "%s: command failed", prefix);
+}
+
+static void
+netsh_command(const struct argv *a, int n, int msglevel)
+{
+    exec_command("NETSH", a, n, msglevel);
 }
 

void

src/openvpn/tun.c close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx) · line 6964

Quoted 6 lines
src/openvpn/tun.c +1 -0
@@ -6927,6 +6959,11 @@ close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx)
         }
         else
         {
+            if (!tt->did_ifconfig_setup &&
 !tuntap_maybe_dhcp(&tt->options))
 

same here

Quoted 66 lines
src/openvpn/tun.c +4 -0
+            {
+                wmic_do_dns_domain(false, tt);
+            }
+
             netsh_delete_address_dns(tt, true, &gc);
         }
     }
src/openvpn/tun.c +1 -1
@@ -6937,7 +6974,7 @@ close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx)
         {
             /* We didn't do ifconfig. */
         }
-        else if (tt->options.ip_win32_type == IPW32_SET_DHCP_MASQ ||
 tt->options.ip_win32_type == IPW32_SET_ADAPTIVE)
+        else if (tuntap_maybe_dhcp(&tt->options))
         {
             /* We don't have to clean the configuration with DHCP. */
         }
src/openvpn/tun.c +7 -2
@@ -6947,9 +6984,14 @@ close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx)
             do_dns_service(false, AF_INET, tt);
             do_address_service(false, AF_INET, tt);
         }
-        else if (tt->options.ip_win32_type == IPW32_SET_NETSH)
+        else
         {
-            netsh_delete_address_dns(tt, false, &gc);
+            wmic_do_dns_domain(false, tt);
+
+            if (tt->options.ip_win32_type == IPW32_SET_NETSH)
+            {
+                netsh_delete_address_dns(tt, false, &gc);
+            }
         }
     }
 
src/openvpn/tun.h +6 -0
index e19e1a2e..0d8e2307 100644
@@ -668,6 +668,12 @@ tuntap_is_dco_win_timeout(struct tuntap *tt, int
 status)
 const char *
 print_windows_driver(enum windows_driver_type windows_driver);
 
+static inline bool
+tuntap_maybe_dhcp(struct tuntap_options *o)
+{
+    return o->ip_win32_type == IPW32_SET_DHCP_MASQ || o->ip_win32_type ==
 IPW32_SET_ADAPTIVE;
+}
+
 #else  /* ifdef _WIN32 */

static inline bool

src/openvpn/win32.h +1 -1
index 72ffb012..36059662 100644
@@ -38,6 +38,7 @@
 #define WIN_ROUTE_PATH_SUFFIX "\\system32\\route.exe"
 #define WIN_IPCONFIG_PATH_SUFFIX "\\system32\\ipconfig.exe"
 #define WIN_NET_PATH_SUFFIX "\\system32\\net.exe"
+#define WMIC_PATH_SUFFIX "\\system32\\wbem\\wmic.exe"
 
 /*
  * Win32-specific OpenVPN code, targeted at the mingw
--
Quoted 1 line

2.23.0.windows.1

Selva

#2 lore raw

Hi,

Quoted 1 line

Adding "!tuntap_maybe_dhcp()" here looks wrong. We must set the domain if not set during IPv4 processing and that is indicated by did_ifconfig_setup. Just as done in the case handled by interactive-service above.

Right, did_ifconfig_setup is not set if the IPv4 address is not pushed, in which case we don't do DHCP stuff (as seen in tuntap_post_open) and tap will be left without a DNS domain. Will remove this condition.

Quoted 1 line

Once that check is removed there is no need to introduce tuntap_maybe_dhcp() in this patch at all.

Idea behind tuntap_maybe_dhcp() was to make some parts of code easier to read by hiding long "if" statements behind this function. But I agree that it would be easier to review just wmic parts and do long overdue refactoring separately.

v2 is coming.

-Lev

#3 lore raw

PATCH v2 1/1 Support of DNS domain for DHCP-less drivers

From: Lev Stipakov <[email protected]...>

We set DNS domain either via interactve service or DHCP. When interactive service is not used, for example, when profiles are started by OpenVPNService, this option is not working for DCO and wintun.

This implements setting DNS domain via WMIC command, similar to implementation in interactive service. This is done when:

- interactive service is not used

- DHCP is not used (ip-win32 is either NETSH or IPAPI, or IPv4 address is not pushed)

Fixes https://github.com/OpenVPN/openvpn/issues/306

Change-Id Ic72a4ecd0414c0d7bf013415f52640fd122cb739

---
v2:
- remove tuntap_maybe_dhcp() check in ipv6 setup/teardown, because we still need wmic call in this case

- remove the whole tuntap_maybe_dhcp() function, because let's do refactoring separately

- rename wmic_do_dns_domain() to do_dns_domain_wmic() to be consistent with do_dns_domain_service()

 src/openvpn/tun.c   | 68 ++++++++++++++++++++++++++++++++++++---------
 src/openvpn/win32.h |  1 +
 2 files changed, 56 insertions(+), 13 deletions(-)
src/openvpn/tun.c +55 -13
index 2ebe4809..2320e8b1 100644
@@ -84,6 +84,8 @@ static void netsh_set_dns6_servers(const struct in6_addr *addr_list,
 
 static void netsh_command(const struct argv *a, int n, int msglevel);
 
+static void exec_command(const char *prefix, const struct argv *a, int n, int msglevel);
+
 static const char *netsh_get_id(const char *dev_node, struct gc_arena *gc);
 
 static bool
@@ -324,6 +326,22 @@ out:
     return ret;
 }
 
+static void
+do_dns_domain_wmic(bool add, const struct tuntap *tt)
+{
+    if (!tt->options.domain)
+    {
+        return;
+    }
+
+    struct argv argv = argv_new();
+    argv_printf(&argv, "%s%s nicconfig where (InterfaceIndex=%ld) call SetDNSDomain %s",
+                get_win_sys_path(), WMIC_PATH_SUFFIX, tt->adapter_index, add ? tt->options.domain : "");
+    exec_command("WMIC", &argv, 1, M_WARN);
+
+    argv_free(&argv);
+}
+
 #endif /* ifdef _WIN32 */
 
 #ifdef TARGET_SOLARIS
@@ -1190,6 +1208,11 @@ do_ifconfig_ipv6(struct tuntap *tt, const char *ifname, int tun_mtu,
         /* set ipv6 dns servers if any are specified */
         netsh_set_dns6_servers(tt->options.dns6, tt->options.dns6_len, tt->adapter_index);
         windows_set_mtu(tt->adapter_index, AF_INET6, tun_mtu);
+
+        if (!tt->did_ifconfig_setup)
+        {
+            do_dns_domain_wmic(true, tt);
+        }
     }
 #else /* platforms we have no IPv6 code for */
     msg(M_FATAL, "Sorry, but I don't know how to do IPv6 'ifconfig' commands on this operating system.  You should ifconfig your TUN/TAP device manually or use an --up script.");
@@ -1535,11 +1558,18 @@ do_ifconfig_ipv4(struct tuntap *tt, const char *ifname, int tun_mtu,
         do_dns_service(true, AF_INET, tt);
         do_dns_domain_service(true, tt);
     }
-    else if (tt->options.ip_win32_type == IPW32_SET_NETSH)
+    else
     {
-        netsh_ifconfig(&tt->options, tt->adapter_index, tt->local,
-                       tt->adapter_netmask, NI_IP_NETMASK|NI_OPTIONS);
+        if (tt->options.ip_win32_type == IPW32_SET_NETSH)
+        {
+            netsh_ifconfig(&tt->options, tt->adapter_index, tt->local,
+                           tt->adapter_netmask, NI_IP_NETMASK | NI_OPTIONS);
+        }
+
+        do_dns_domain_wmic(true, tt);
     }
+
+
     if (tt->options.msg_channel)
     {
         do_set_mtu_service(tt, AF_INET, tun_mtu);
@@ -5238,12 +5268,8 @@ dhcp_renew(const struct tuntap *tt)
     }
 }
 
-/*
- * netsh functions
- */
-
 static void
-netsh_command(const struct argv *a, int n, int msglevel)
+exec_command(const char *prefix, const struct argv *a, int n, int msglevel)
 {
     int i;
     for (i = 0; i < n; ++i)
@@ -5251,8 +5277,8 @@ netsh_command(const struct argv *a, int n, int msglevel)
         bool status;
         management_sleep(0);
         netcmd_semaphore_lock();
-        argv_msg_prefix(M_INFO, a, "NETSH");
-        status = openvpn_execve_check(a, NULL, 0, "ERROR: netsh command failed");
+        argv_msg_prefix(M_INFO, a, prefix);
+        status = openvpn_execve_check(a, NULL, 0, "ERROR: command failed");
         netcmd_semaphore_release();
         if (status)
         {
@@ -5260,7 +5286,13 @@ netsh_command(const struct argv *a, int n, int msglevel)
         }
         management_sleep(4);
     }
-    msg(msglevel, "NETSH: command failed");
+    msg(msglevel, "%s: command failed", prefix);
+}
+
+static void
+netsh_command(const struct argv *a, int n, int msglevel)
+{
+    exec_command("NETSH", a, n, msglevel);
 }
 
 void
@@ -6927,6 +6959,11 @@ close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx)
         }
         else
         {
+            if (!tt->did_ifconfig_setup)
+            {
+                do_dns_domain_wmic(false, tt);
+            }
+
             netsh_delete_address_dns(tt, true, &gc);
         }
     }
@@ -6947,9 +6984,14 @@ close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx)
             do_dns_service(false, AF_INET, tt);
             do_address_service(false, AF_INET, tt);
         }
-        else if (tt->options.ip_win32_type == IPW32_SET_NETSH)
+        else
         {
-            netsh_delete_address_dns(tt, false, &gc);
+            do_dns_domain_wmic(false, tt);
+
+            if (tt->options.ip_win32_type == IPW32_SET_NETSH)
+            {
+                netsh_delete_address_dns(tt, false, &gc);
+            }
         }
     }
 
src/openvpn/win32.h +1 -0
index 72ffb012..36059662 100644
@@ -38,6 +38,7 @@
 #define WIN_ROUTE_PATH_SUFFIX "\\system32\\route.exe"
 #define WIN_IPCONFIG_PATH_SUFFIX "\\system32\\ipconfig.exe"
 #define WIN_NET_PATH_SUFFIX "\\system32\\net.exe"
+#define WMIC_PATH_SUFFIX "\\system32\\wbem\\wmic.exe"
 
 /*
  * Win32-specific OpenVPN code, targeted at the mingw
Signature · 1 line
2.23.0.windows.1
Acked-by on v2 1/1
#4 lore raw

Hi

Quoted 196 lines · Lev Stipakov

From: Lev Stipakov <[email protected]...>

We set DNS domain either via interactve service or DHCP. When interactive service is not used, for example, when profiles are started by OpenVPNService, this option is not working for DCO and wintun.

This implements setting DNS domain via WMIC command, similar to implementation in interactive service. This is done when:

- interactive service is not used

- DHCP is not used (ip-win32 is either NETSH or IPAPI, or IPv4 address is not pushed)

Fixes https://github.com/OpenVPN/openvpn/issues/306

Change-Id Ic72a4ecd0414c0d7bf013415f52640fd122cb739

---
v2:
- remove tuntap_maybe_dhcp() check in ipv6 setup/teardown, because we still need wmic call in this case

- remove the whole tuntap_maybe_dhcp() function, because let's do refactoring separately

- rename wmic_do_dns_domain() to do_dns_domain_wmic() to be consistent with do_dns_domain_service()

 src/openvpn/tun.c   | 68 ++++++++++++++++++++++++++++++++++++---------
 src/openvpn/win32.h |  1 +
 2 files changed, 56 insertions(+), 13 deletions(-)
src/openvpn/tun.c +2 -0
index 2ebe4809..2320e8b1 100644
@@ -84,6 +84,8 @@ static void netsh_set_dns6_servers(const struct in6_addr
 *addr_list,
 
 static void netsh_command(const struct argv *a, int n, int msglevel);
 
+static void exec_command(const char *prefix, const struct argv *a, int n,
 int msglevel);
+
 static const char *netsh_get_id(const char *dev_node, struct gc_arena

*gc);

static bool

src/openvpn/tun.c +16 -0
@@ -324,6 +326,22 @@ out:
     return ret;
 }
 
+static void
+do_dns_domain_wmic(bool add, const struct tuntap *tt)
+{
+    if (!tt->options.domain)
+    {
+        return;
+    }
+
+    struct argv argv = argv_new();
+    argv_printf(&argv, "%s%s nicconfig where (InterfaceIndex=%ld) call
 SetDNSDomain %s",
+                get_win_sys_path(), WMIC_PATH_SUFFIX, tt->adapter_index,
 add ? tt->options.domain : "");
+    exec_command("WMIC", &argv, 1, M_WARN);
+
+    argv_free(&argv);
+}
+
 #endif /* ifdef _WIN32 */

#ifdef TARGET_SOLARIS

src/openvpn/tun.c +5 -0
@@ -1190,6 +1208,11 @@ do_ifconfig_ipv6(struct tuntap *tt, const char
 *ifname, int tun_mtu,
         /* set ipv6 dns servers if any are specified */
         netsh_set_dns6_servers(tt->options.dns6, tt->options.dns6_len,
 tt->adapter_index);
         windows_set_mtu(tt->adapter_index, AF_INET6, tun_mtu);
+
+        if (!tt->did_ifconfig_setup)
+        {
+            do_dns_domain_wmic(true, tt);
+        }
     }

#else /* platforms we have no IPv6 code for */
msg(M_FATAL, "Sorry, but I don't know how to do IPv6 'ifconfig'
commands on this operating system. You should ifconfig your TUN/TAP device manually or use an --up script.");

src/openvpn/tun.c +10 -3
@@ -1535,11 +1558,18 @@ do_ifconfig_ipv4(struct tuntap *tt, const char
 *ifname, int tun_mtu,
         do_dns_service(true, AF_INET, tt);
         do_dns_domain_service(true, tt);
     }
-    else if (tt->options.ip_win32_type == IPW32_SET_NETSH)
+    else
     {
-        netsh_ifconfig(&tt->options, tt->adapter_index, tt->local,
-                       tt->adapter_netmask, NI_IP_NETMASK|NI_OPTIONS);
+        if (tt->options.ip_win32_type == IPW32_SET_NETSH)
+        {
+            netsh_ifconfig(&tt->options, tt->adapter_index, tt->local,
+                           tt->adapter_netmask, NI_IP_NETMASK |
 NI_OPTIONS);
+        }
+
+        do_dns_domain_wmic(true, tt);
     }
+
+
     if (tt->options.msg_channel)
     {
         do_set_mtu_service(tt, AF_INET, tun_mtu);
src/openvpn/tun.c +1 -5
@@ -5238,12 +5268,8 @@ dhcp_renew(const struct tuntap *tt)
     }
 }
 
-/*
- * netsh functions
- */
-
 static void
-netsh_command(const struct argv *a, int n, int msglevel)
+exec_command(const char *prefix, const struct argv *a, int n, int
 msglevel)
 {
     int i;
     for (i = 0; i < n; ++i)
src/openvpn/tun.c +2 -2
@@ -5251,8 +5277,8 @@ netsh_command(const struct argv *a, int n, int
 msglevel)
         bool status;
         management_sleep(0);
         netcmd_semaphore_lock();
-        argv_msg_prefix(M_INFO, a, "NETSH");
-        status = openvpn_execve_check(a, NULL, 0, "ERROR: netsh command
 failed");
+        argv_msg_prefix(M_INFO, a, prefix);
+        status = openvpn_execve_check(a, NULL, 0, "ERROR: command
 failed");
         netcmd_semaphore_release();
         if (status)
         {
src/openvpn/tun.c +7 -1
@@ -5260,7 +5286,13 @@ netsh_command(const struct argv *a, int n, int
 msglevel)
         }
         management_sleep(4);
     }
-    msg(msglevel, "NETSH: command failed");
+    msg(msglevel, "%s: command failed", prefix);
+}
+
+static void
+netsh_command(const struct argv *a, int n, int msglevel)
+{
+    exec_command("NETSH", a, n, msglevel);
 }
 

void

src/openvpn/tun.c +12 -2
@@ -6927,6 +6959,11 @@ close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx)
         }
         else
         {
+            if (!tt->did_ifconfig_setup)
+            {
+                do_dns_domain_wmic(false, tt);
+            }
+
             netsh_delete_address_dns(tt, true, &gc);
         }
     }
@@ -6947,9 +6984,14 @@ close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx)
             do_dns_service(false, AF_INET, tt);
             do_address_service(false, AF_INET, tt);
         }
-        else if (tt->options.ip_win32_type == IPW32_SET_NETSH)
+        else
         {
-            netsh_delete_address_dns(tt, false, &gc);
+            do_dns_domain_wmic(false, tt);
+
+            if (tt->options.ip_win32_type == IPW32_SET_NETSH)
+            {
+                netsh_delete_address_dns(tt, false, &gc);
+            }
         }
     }
 
src/openvpn/win32.h +0 -0
index 72ffb012..36059662 100644

src/openvpn/win32.h line 44

Quoted 7 lines
src/openvpn/win32.h +1 -0
@@ -38,6 +38,7 @@
 #define WIN_ROUTE_PATH_SUFFIX "\\system32\\route.exe"
 #define WIN_IPCONFIG_PATH_SUFFIX "\\system32\\ipconfig.exe"
 #define WIN_NET_PATH_SUFFIX "\\system32\\net.exe"
+#define WMIC_PATH_SUFFIX "\\system32\\wbem\\wmic.exe"
 
 /*
  * Win32-specific OpenVPN code, targeted at the mingw

Useful to have in 2.6 as well, as dco is the default now. The changes are minimal and should not have any side effects.

Did some quick test runs on Win 10 using tap-windows6 and dco drivers with and without "dhcp-option DOMAIN".

Selva

#5 lore raw
In reply to Lev Stipakov

I haven't tested this beyond "compiles with MinGW" and "Github actions", but stared a bit at the code and it looks okay.

This can be considered a bugfix, so 2.6 as well.

Your patch has been applied to the master and release/2.6 branch.

commit 6cf7ce4eb33626b861031f965b35c3107d75e843 (master) commit 77a74357e3f9a5a18665d387e7c268f0c8cb975c (release/2.6)

Author: Lev Stipakov
Date:   Thu Apr 6 10:15:46 2023 +0300
     Support of DNS domain for DHCP-less drivers

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

--
kind regards,

Gert Doering