From patchwork Mon Mar 9 02:17:26 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Rozman X-Patchwork-Id: 1023 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director11.mail.ord1d.rsapps.net ([172.30.191.6]) by backend30.mail.ord1d.rsapps.net with LMTP id mG0eBE5CZl4qagAAIUCqbw for ; Mon, 09 Mar 2020 09:19:10 -0400 Received: from proxy19.mail.ord1d.rsapps.net ([172.30.191.6]) by director11.mail.ord1d.rsapps.net with LMTP id WPHuA05CZl63TAAAvGGmqA ; Mon, 09 Mar 2020 09:19:10 -0400 Received: from smtp19.gate.ord1d ([172.30.191.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy19.mail.ord1d.rsapps.net with LMTP id wFm/A05CZl78QwAAyH2SIw ; Mon, 09 Mar 2020 09:19:10 -0400 X-Spam-Threshold: 95 X-Spam-Score: 0 X-Spam-Flag: NO X-Virus-Scanned: OK X-Orig-To: openvpnslackdevel@openvpn.net X-Originating-Ip: [216.105.38.7] Authentication-Results: smtp19.gate.ord1d.rsapps.net; iprev=pass policy.iprev="216.105.38.7"; spf=pass smtp.mailfrom="openvpn-devel-bounces@lists.sourceforge.net" smtp.helo="lists.sourceforge.net"; dkim=fail (signature verification failed) header.d=sourceforge.net; dkim=fail (signature verification failed) header.d=sf.net; dkim=fail (signature verification failed) header.d=rozman.si; dmarc=fail (p=none; dis=none) header.from=rozman.si X-Suspicious-Flag: YES X-Classification-ID: 8f0d3082-6208-11ea-ac83-525400d67fa8-1-1 Received: from [216.105.38.7] ([216.105.38.7:35010] helo=lists.sourceforge.net) by smtp19.gate.ord1d.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id 79/1F-26733-D42466E5; Mon, 09 Mar 2020 09:19:09 -0400 Received: from [127.0.0.1] (helo=sfs-ml-2.v29.lw.sourceforge.com) by sfs-ml-2.v29.lw.sourceforge.com with esmtp (Exim 4.90_1) (envelope-from ) id 1jBIIb-0000Ab-0W; Mon, 09 Mar 2020 13:18:25 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-2.v29.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jBIIY-00009s-Da for openvpn-devel@lists.sourceforge.net; Mon, 09 Mar 2020 13:18:22 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=DSUftUY+u48z0j6rAhuxc+UMqnu9mv4OunO8lslEhoI=; b=lQ5laBIidDPTbDcBc4lNM5BZNz M7rfFvZ6kkTOX9uyY+h8Q+Ys7PEHLep4gOeK98jtS87JtBnZ5/9csUj2WWccnqtfWyERBOW/Q5wsN TRG6hp6lQDWwqtm/ii6o5omuTGrq/+zt6mepmmfotPFo4Twya9HctHpzeug/95Frf9tA=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-Id: Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=DSUftUY+u48z0j6rAhuxc+UMqnu9mv4OunO8lslEhoI=; b=TjwWwfLwqsezeABOcn1YnKe4Cd PQCXeuLybPajOhHdp6xJy+S/4u1JIlHv6o5c8vCwOn3XOz7QIrNkWHFmJKsgnmUTIeK4vjg3B/Ay0 KdHhQbugonHLBb1AQg+k4JNzoWhNvC7huOvq2zJb+WKqfUc+2uJztrdQ5LpDgCkktkHw=; Received: from pub5.amebis.si ([213.250.55.21]) by sfi-mx-1.v28.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.92.2) id 1jBIIW-00DVpc-4K for openvpn-devel@lists.sourceforge.net; Mon, 09 Mar 2020 13:18:22 +0000 Received: by pub5.amebis.si (Postfix, from userid 1000) id 33AFB1002FC0; Mon, 9 Mar 2020 14:18:03 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rozman.si; s=default; t=1583759883; bh=DSUftUY+u48z0j6rAhuxc+UMqnu9mv4OunO8lslEhoI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ea+H8crqYkdV2zXk9hx16lVh30i+wOyQ3MEiZ6F4tfRJdV2oidnNH3TSqteGt0ljo WvLVGORC96FtxMvXpvk5mmO+bgaYlZUwajNX+FFkylWKJlVKxuVDIkPwrWFJxOe9gM 0D0R85HsIsJBth6DP5Xyf/dQkR4cBUft9LIbVfbc= X-Spam-Checker-Version: SpamAssassin 3.4.3 (2019-12-06) on brana.amebis.doma X-Spam-Level: X-Spam-Status: No, score=-2.9 required=5.0 tests=ALL_TRUSTED,BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.3 Received: from SR6.amebis.doma (unknown [IPv6:2a00:ee2:209:164:a5ae:e83c:b7b:725]) by pub5.amebis.si (Postfix) with ESMTP id A59EE1002FCD; Mon, 9 Mar 2020 14:17:49 +0100 (CET) From: Simon Rozman To: openvpn-devel@lists.sourceforge.net Date: Mon, 9 Mar 2020 14:17:26 +0100 Message-Id: <20200309131728.380-10-simon@rozman.si> X-Mailer: git-send-email 2.24.1.windows.2 In-Reply-To: <20200309131728.380-1-simon@rozman.si> References: <20200309131728.380-1-simon@rozman.si> MIME-Version: 1.0 X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid 0.0 AWL AWL: Adjusted score from AWL reputation of From: address X-Headers-End: 1jBIIW-00DVpc-4K Subject: [Openvpn-devel] [PATCH 10/12] openvpnmsica, tapctl: Revise default hardware ID management X-BeenThere: openvpn-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox tap_create_adapter() and tap_list_adapter() no longer default to "root\tap0901". Defining a default hardware ID value is at the responsibility of upper layers that process user desires. Since the tap_list_adapter() no longer defaults the hardware ID to anything, its behavior was simplified to return all existing adapters when a NULL hardware ID is specified. Signed-off-by: Simon Rozman Acked-by: Lev Stipakov --- src/openvpnmsica/openvpnmsica.c | 16 ++-- src/tapctl/main.c | 14 ++-- src/tapctl/tap.c | 134 ++++++++++++++++---------------- src/tapctl/tap.h | 17 ++-- 4 files changed, 86 insertions(+), 95 deletions(-) diff --git a/src/openvpnmsica/openvpnmsica.c b/src/openvpnmsica/openvpnmsica.c index cfbda8da..ae9b007f 100644 --- a/src/openvpnmsica/openvpnmsica.c +++ b/src/openvpnmsica/openvpnmsica.c @@ -285,9 +285,9 @@ FindTUNTAPAdapters(_In_ MSIHANDLE hInstall) OPENVPNMSICA_SAVE_MSI_SESSION(hInstall); - /* Get all TUN/TAP network adapters. */ + /* Get existing network adapters. */ struct tap_adapter_node *pAdapterList = NULL; - uiResult = tap_list_adapters(NULL, NULL, &pAdapterList, FALSE); + uiResult = tap_list_adapters(NULL, NULL, &pAdapterList); if (uiResult != ERROR_SUCCESS) { goto cleanup_CoInitialize; @@ -573,9 +573,9 @@ schedule_adapter_create( _In_z_ LPCTSTR szHardwareId, _Inout_ int *iTicks) { - /* Get all available network adapters. */ + /* Get existing network adapters. */ struct tap_adapter_node *pAdapterList = NULL; - DWORD dwResult = tap_list_adapters(NULL, NULL, &pAdapterList, TRUE); + DWORD dwResult = tap_list_adapters(NULL, NULL, &pAdapterList); if (dwResult != ERROR_SUCCESS) { return dwResult; @@ -674,9 +674,9 @@ schedule_adapter_delete( _In_z_ LPCTSTR szHardwareId, _Inout_ int *iTicks) { - /* Get available adapters with given hardware ID. */ + /* Get adapters with given hardware ID. */ struct tap_adapter_node *pAdapterList = NULL; - DWORD dwResult = tap_list_adapters(NULL, szHardwareId, &pAdapterList, FALSE); + DWORD dwResult = tap_list_adapters(NULL, szHardwareId, &pAdapterList); if (dwResult != ERROR_SUCCESS) { return dwResult; @@ -1125,9 +1125,9 @@ ProcessDeferredAction(_In_ MSIHANDLE hInstall) } } - /* Get all available adapters. */ + /* Get existing adapters. */ struct tap_adapter_node *pAdapterList = NULL; - dwResult = tap_list_adapters(NULL, NULL, &pAdapterList, TRUE); + dwResult = tap_list_adapters(NULL, NULL, &pAdapterList); if (dwResult == ERROR_SUCCESS) { /* Does the adapter exist? */ diff --git a/src/tapctl/main.c b/src/tapctl/main.c index 1cc86424..fdeda7bf 100644 --- a/src/tapctl/main.c +++ b/src/tapctl/main.c @@ -177,7 +177,7 @@ _tmain(int argc, LPCTSTR argv[]) else if (_tcsicmp(argv[1], TEXT("create")) == 0) { LPCTSTR szName = NULL; - LPCTSTR szHwId = NULL; + LPCTSTR szHwId = TEXT("root\\") TEXT(TAP_WIN_COMPONENT_ID); /* Parse options. */ for (int i = 2; i < argc; i++) @@ -214,9 +214,9 @@ _tmain(int argc, LPCTSTR argv[]) if (szName) { - /* Get the list of all available adapters. */ + /* Get existing network adapters. */ struct tap_adapter_node *pAdapterList = NULL; - dwResult = tap_list_adapters(NULL, szHwId, &pAdapterList, TRUE); + dwResult = tap_list_adapters(NULL, NULL, &pAdapterList); if (dwResult != ERROR_SUCCESS) { _ftprintf(stderr, TEXT("Enumerating adapters failed (error 0x%x).\n"), dwResult); @@ -271,7 +271,7 @@ create_delete_adapter: } else if (_tcsicmp(argv[1], TEXT("list")) == 0) { - LPCTSTR szHwId = NULL; + LPCTSTR szHwId = TEXT("root\\") TEXT(TAP_WIN_COMPONENT_ID); /* Parse options. */ for (int i = 2; i < argc; i++) @@ -286,9 +286,9 @@ create_delete_adapter: } } - /* Output list of TUN/TAP adapters. */ + /* Output list of adapters with given hardware ID. */ struct tap_adapter_node *pAdapterList = NULL; - DWORD dwResult = tap_list_adapters(NULL, szHwId, &pAdapterList, FALSE); + DWORD dwResult = tap_list_adapters(NULL, szHwId, &pAdapterList); if (dwResult != ERROR_SUCCESS) { _ftprintf(stderr, TEXT("Enumerating TUN/TAP adapters failed (error 0x%x).\n"), dwResult); @@ -319,7 +319,7 @@ create_delete_adapter: { /* The argument failed to covert to GUID. Treat it as the adapter name. */ struct tap_adapter_node *pAdapterList = NULL; - DWORD dwResult = tap_list_adapters(NULL, NULL, &pAdapterList, FALSE); + DWORD dwResult = tap_list_adapters(NULL, NULL, &pAdapterList); if (dwResult != ERROR_SUCCESS) { _ftprintf(stderr, TEXT("Enumerating TUN/TAP adapters failed (error 0x%x).\n"), dwResult); diff --git a/src/tapctl/tap.c b/src/tapctl/tap.c index d718d43e..bdef3fc1 100644 --- a/src/tapctl/tap.c +++ b/src/tapctl/tap.c @@ -42,12 +42,53 @@ const static GUID GUID_DEVCLASS_NET = { 0x4d36e972L, 0xe325, 0x11ce, { 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 } }; -const static TCHAR szzDefaultHardwareIDs[] = TEXT("root\\") TEXT(TAP_WIN_COMPONENT_ID) TEXT("\0"); - const static TCHAR szAdapterRegKeyPathTemplate[] = TEXT("SYSTEM\\CurrentControlSet\\Control\\Network\\%") TEXT(PRIsLPOLESTR) TEXT("\\%") TEXT(PRIsLPOLESTR) TEXT("\\Connection"); #define ADAPTER_REGKEY_PATH_MAX (_countof(TEXT("SYSTEM\\CurrentControlSet\\Control\\Network\\")) - 1 + 38 + _countof(TEXT("\\")) - 1 + 38 + _countof(TEXT("\\Connection"))) +/** + * Returns length of string of strings + * + * @param szz Pointer to a string of strings (terminated by an empty string) + * + * @return Number of characters not counting the final zero terminator + **/ +static inline size_t +_tcszlen(_In_z_ LPCTSTR szz) +{ + LPCTSTR s; + for (s = szz; s[0]; s += _tcslen(s) + 1) + { + } + return s - szz; +} + + +/** + * Checks if string is contained in the string of strings. Comparison is made case-insensitive. + * + * @param szzHay Pointer to a string of strings (terminated by an empty string) we are + * looking in + * + * @param szNeedle The string we are searching for + * + * @return Pointer to the string in szzHay that matches szNeedle is found; NULL otherwise + */ +static LPCTSTR +_tcszistr(_In_z_ LPCTSTR szzHay, _In_z_ LPCTSTR szNeedle) +{ + for (LPCTSTR s = szzHay; s[0]; s += _tcslen(s) + 1) + { + if (_tcsicmp(s, szNeedle) == 0) + { + return s; + } + } + + return NULL; +} + + /** * Function that performs a specific task on a device * @@ -628,45 +669,23 @@ get_device_reg_property( } -/** - * Returns length of list of strings - * - * @param str Pointer to a list of strings terminated by an empty string. - * - * @return Number of characters not counting the final zero terminator - **/ -static inline size_t -_tcszlen(_In_ LPCTSTR str) -{ - LPCTSTR s; - for (s = str; s[0]; s += _tcslen(s) + 1) - { - } - return s - str; -} - - DWORD tap_create_adapter( _In_opt_ HWND hwndParent, _In_opt_ LPCTSTR szDeviceDescription, - _In_opt_ LPCTSTR szHwId, + _In_ LPCTSTR szHwId, _Inout_ LPBOOL pbRebootRequired, _Out_ LPGUID pguidAdapter) { DWORD dwResult; - if (pbRebootRequired == NULL + if (szHwId == NULL + || pbRebootRequired == NULL || pguidAdapter == NULL) { return ERROR_BAD_ARGUMENTS; } - if (szHwId == NULL) - { - szHwId = szzDefaultHardwareIDs; - } - /* Create an empty device info set for network adapter device class. */ HDEVINFO hDevInfoList = SetupDiCreateDeviceInfoList(&GUID_DEVCLASS_NET, hwndParent); if (hDevInfoList == INVALID_HANDLE_VALUE) @@ -818,29 +837,23 @@ tap_create_adapter( } } - /* Check the driver version first, since the check is trivial and will save us iterating over hardware IDs for any driver versioned prior our best match. */ - if (dwlDriverVersion < drvinfo_data.DriverVersion) + /* Check the driver version and hardware ID. */ + if (dwlDriverVersion < drvinfo_data.DriverVersion + && drvinfo_detail_data->HardwareID + && _tcszistr(drvinfo_detail_data->HardwareID, szHwId)) { - /* Search the list of hardware IDs. */ - for (LPTSTR szHwdID = drvinfo_detail_data->HardwareID; szHwdID && szHwdID[0]; szHwdID += _tcslen(szHwdID) + 1) + /* Newer version and matching hardware ID found. Select the driver. */ + if (!SetupDiSetSelectedDriver( + hDevInfoList, + &devinfo_data, + &drvinfo_data)) { - if (_tcsicmp(szHwdID, szHwId) == 0) - { - /* Matching hardware ID found. Select the driver. */ - if (!SetupDiSetSelectedDriver( - hDevInfoList, - &devinfo_data, - &drvinfo_data)) - { - /* Something is wrong with this driver. Skip it. */ - msg(M_WARN | M_ERRNO, "%s: SetupDiSetSelectedDriver(\"%hs\") failed", __FUNCTION__, drvinfo_data.Description); - break; - } - - dwlDriverVersion = drvinfo_data.DriverVersion; - break; - } + /* Something is wrong with this driver. Skip it. */ + msg(M_WARN | M_ERRNO, "%s: SetupDiSetSelectedDriver(\"%hs\") failed", __FUNCTION__, drvinfo_data.Description); + continue; } + + dwlDriverVersion = drvinfo_data.DriverVersion; } } if (drvinfo_detail_data) @@ -1167,8 +1180,7 @@ DWORD tap_list_adapters( _In_opt_ HWND hwndParent, _In_opt_ LPCTSTR szHwId, - _Out_ struct tap_adapter_node **ppAdapter, - _In_ BOOL bAll) + _Out_ struct tap_adapter_node **ppAdapter) { DWORD dwResult; @@ -1177,11 +1189,6 @@ tap_list_adapters( return ERROR_BAD_ARGUMENTS; } - if (szHwId == NULL) - { - szHwId = szzDefaultHardwareIDs; - } - /* Create a list of network devices. */ HDEVINFO hDevInfoList = SetupDiGetClassDevsEx( &GUID_DEVCLASS_NET, @@ -1253,7 +1260,7 @@ tap_list_adapters( /* Check that hardware ID is REG_SZ/REG_MULTI_SZ, and optionally if it matches ours. */ if (dwDataType == REG_SZ) { - if (!bAll && _tcsicmp(szzDeviceHardwareIDs, szHwId) != 0) + if (szHwId && _tcsicmp(szzDeviceHardwareIDs, szHwId) != 0) { /* This is not our device. Skip it. */ goto cleanup_szzDeviceHardwareIDs; @@ -1261,21 +1268,10 @@ tap_list_adapters( } else if (dwDataType == REG_MULTI_SZ) { - if (!bAll) + if (szHwId && _tcszistr(szzDeviceHardwareIDs, szHwId) == NULL) { - for (LPTSTR szHwdID = szzDeviceHardwareIDs;; szHwdID += _tcslen(szHwdID) + 1) - { - if (szHwdID[0] == 0) - { - /* This is not our device. Skip it. */ - goto cleanup_szzDeviceHardwareIDs; - } - else if (_tcsicmp(szHwdID, szHwId) == 0) - { - /* This is our device. */ - break; - } - } + /* This is not our device. Skip it. */ + goto cleanup_szzDeviceHardwareIDs; } } else diff --git a/src/tapctl/tap.h b/src/tapctl/tap.h index aec44ec8..68662c82 100644 --- a/src/tapctl/tap.h +++ b/src/tapctl/tap.h @@ -38,8 +38,7 @@ * description of the device. This pointer is optional and can be NULL. * * @param szHwId A pointer to a NULL-terminated string that supplies the hardware id - * of the device. This pointer is optional and can be NULL. Default value - * is root\tap0901. + * of the device (e.g. "root\\tap0901", "Wintun"). * * @param pbRebootRequired A pointer to a BOOL flag. If the device requires a system restart, * this flag is set to TRUE. Otherwise, the flag is left unmodified. This @@ -54,7 +53,7 @@ DWORD tap_create_adapter( _In_opt_ HWND hwndParent, _In_opt_ LPCTSTR szDeviceDescription, - _In_opt_ LPCTSTR szHwId, + _In_ LPCTSTR szHwId, _Inout_ LPBOOL pbRebootRequired, _Out_ LPGUID pguidAdapter); @@ -141,7 +140,7 @@ struct tap_adapter_node /** - * Creates a list of available network adapters. + * Creates a list of existing network adapters. * * @param hwndParent A handle to the top-level window to use for any user adapter that is * related to non-device-specific actions (such as a select-device dialog @@ -150,24 +149,20 @@ struct tap_adapter_node * hwndParent to NULL. * * @param szHwId A pointer to a NULL-terminated string that supplies the hardware id - * of the device. This pointer is optional and can be NULL. Default value - * is root\tap0901. + * of the device. This pointer is optional and can be NULL. When NULL, + * all network adapters found are added to the list. * * @param ppAdapterList A pointer to the list to receive pointer to the first adapter in * the list. After the list is no longer required, free it using * tap_free_adapter_list(). * - * @param bAll When TRUE, all network adapters found are added to the list. When - * FALSE, only TUN/TAP adapters found are added. - * * @return ERROR_SUCCESS on success; Win32 error code otherwise */ DWORD tap_list_adapters( _In_opt_ HWND hwndParent, _In_opt_ LPCTSTR szHwId, - _Out_ struct tap_adapter_node **ppAdapterList, - _In_ BOOL bAll); + _Out_ struct tap_adapter_node **ppAdapterList); /**