]> www.infradead.org Git - users/dwmw2/openconnect.git/commitdiff
Use hostname as Wintun ifname (if ifname not specified)
authorDaniel Lenski <dlenski@gmail.com>
Wed, 31 Mar 2021 01:08:00 +0000 (18:08 -0700)
committerDaniel Lenski <dlenski@gmail.com>
Fri, 10 Sep 2021 18:26:56 +0000 (11:26 -0700)
Without this change, OpenConnect will *only* attempt to use the Wintun
driver if `-i InterfaceName`, and will require TAP-Windows driver otherwise.

That seems like a surprising and hard-to-discover behavior.

Instead, we should use the VPN server's hostname as a sane default interface
name with Wintun, and only attempt to use TAP-Windows as a fallback in the
case where Wintun can't be initialized.

Signed-off-by: Daniel Lenski <dlenski@gmail.com>
tun-win32.c

index b3113178dd28fa0f35228211877221c3df3acf23..b265df8d8748c2fd1624f31826e9e75a050523eb 100644 (file)
@@ -343,28 +343,43 @@ static intptr_t open_tun(struct openconnect_info *vpninfo, int adapter_type, cha
        return ret;
 }
 
+static intptr_t create_ifname_w(struct openconnect_info *vpninfo, const char *ifname) {
+       struct oc_text_buf *ifname_buf = buf_alloc();
+       buf_append_utf16le(ifname_buf, vpninfo->ifname);
+
+       if (buf_error(ifname_buf)) {
+               vpn_progress(vpninfo, PRG_ERR, _("Could not construct interface name\n"));
+               return buf_free(ifname_buf);
+       }
+
+       free(vpninfo->ifname_w);
+       vpninfo->ifname_w = (wchar_t *)ifname_buf->data;
+       ifname_buf->data = NULL;
+       buf_free(ifname_buf);
+       return 0;
+}
+
 intptr_t os_setup_tun(struct openconnect_info *vpninfo)
 {
-       if (vpninfo->ifname) {
-               struct oc_text_buf *ifname_buf = buf_alloc();
-               buf_append_utf16le(ifname_buf, vpninfo->ifname);
+       intptr_t ret;
 
-               if (buf_error(ifname_buf)) {
-                       vpn_progress(vpninfo, PRG_ERR, _("Could not construct interface name\n"));
-                       return buf_free(ifname_buf);
-               }
-
-               free(vpninfo->ifname_w);
-               vpninfo->ifname_w = (wchar_t *)ifname_buf->data;
-               ifname_buf->data = NULL;
-               buf_free(ifname_buf);
+       if (vpninfo->ifname) {
+               ret = create_ifname_w(vpninfo, vpninfo->ifname);
+               if (ret)
+                       return ret;
        }
 
-       intptr_t ret = search_taps(vpninfo, open_tun);
+       ret = search_taps(vpninfo, open_tun);
 
        if (ret == OPEN_TUN_SOFTFAIL) {
-               /* If an interface name was given, try creating a Wintun */
-               if (vpninfo->ifname && !create_wintun(vpninfo)) {
+               if (!vpninfo->ifname_w) {
+                       ret = create_ifname_w(vpninfo, vpninfo->hostname);
+                       if (ret)
+                               return ret;
+               }
+
+               /* Try creating a Wintun instead of TAP */
+               if (!create_wintun(vpninfo)) {
                        ret = search_taps(vpninfo, open_tun);
 
                        if (ret == OPEN_TUN_SOFTFAIL)
@@ -376,10 +391,7 @@ intptr_t os_setup_tun(struct openconnect_info *vpninfo)
 
        if (ret == OPEN_TUN_SOFTFAIL) {
                vpn_progress(vpninfo, PRG_ERR,
-                            _("No Windows-TAP adapters found. Is the driver installed?\n"));
-               if (!vpninfo->ifname)
-                       vpn_progress(vpninfo, PRG_ERR,
-                                    _("Alternatively, specify an interface name to try creating it with Wintun\n"));
+                            _("Neither Windows-TAP nor Wintun adapters were found. Is the driver installed?\n"));
                ret = OPEN_TUN_HARDFAIL;
        }