Skip to contentSkip to conversation

Wintun support

PATCH In review Lev Stipakov 7 participants 78 messages Nov 7, 2019 – Dec 18, 2019 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

v2 cover Wintun support

This set of patches adds support of wintun kernel driver
(https://www.wintun.net) to OpenVPN Windows client and server.

While wintun is in beta (currently used version is 0.7), it performs significantly faster comparison to tap-windows6.

Below are download bandwidth stats reported by iperf3 running on client:

Quoted 1 line

iperf3 -c <server-vpn-ip> -t 60

Server - community openvpn2 (2.4.4)

tap-windows6 - 396Mbit/s
wintun - 677Mbit/s

Server - propietary openvpn3 with kernel acceleration (in development)

tap-windows6 - 386Mbit/s
wintun - 840Mbit/s

Client version used for tap-windows6 tests is 2.4.8.

To use wintun driver instead of tap-window6, add "windows-driver wintun" to your VPN config file or openvpn.exe command line.

Ready-made Windows client installer (signed by OpenVPN Inc)
with wintun support could be found here: from http://staging.openvpn.net/openvpn2/.

To build installer yourself, you need a patched version of openvpn-build, see https://github.com/OpenVPN/openvpn-build/pull/154.

Changes from v1:

- rebased on top of latest master (2b11e57)

Lev Stipakov (7):
Visual Studio: upgrade project files to VS2019
wintun: add --windows-driver config option
wintun: implement opening wintun device
wintun: ring buffers based I/O
wintun: interactive service support
wintun: set adapter properties via interactive service
wintun: clear adapter settings on tun close

 include/openvpn-msg.h                       |  10 +
 src/compat/compat.vcxproj                   |  12 +-
 src/openvpn/Makefile.am                     |   4 +-
 src/openvpn/forward.c                       |  42 ++-
 src/openvpn/forward.h                       |  47 ++-
 src/openvpn/init.c                          |   7 +
 src/openvpn/mtcp.c                          |  28 +-
 src/openvpn/mudp.c                          |  14 +
 src/openvpn/openvpn.vcxproj                 |  16 +-
 src/openvpn/openvpn.vcxproj.filters         |   8 +-
 src/openvpn/options.c                       |  37 ++
 src/openvpn/options.h                       |   1 +
 src/openvpn/ring_buffer.c                   |  54 +++
 src/openvpn/ring_buffer.h                   |  79 +++++
 src/openvpn/route.c                         |   2 +-
 src/openvpn/route.h                         |   3 +-
 src/openvpn/syshead.h                       |   1 +
 src/openvpn/tun.c                           | 504 +++++++++++++++++++++++-----
 src/openvpn/tun.h                           | 139 +++++++-
 src/openvpn/win32.c                         |  95 ++++++
 src/openvpn/win32.h                         |   4 +
 src/openvpnmsica/openvpnmsica.vcxproj       |  14 +-
 src/openvpnserv/Makefile.am                 |   3 +-
 src/openvpnserv/interactive.c               | 141 +++++++-
 src/openvpnserv/openvpnserv.vcxproj         |  14 +-
 src/openvpnserv/openvpnserv.vcxproj.filters |   6 +
 src/tapctl/tapctl.vcxproj                   |  14 +-
 27 files changed, 1160 insertions(+), 139 deletions(-)
 create mode 100644 src/openvpn/ring_buffer.c
 create mode 100644 src/openvpn/ring_buffer.h
Signature · 1 line
2.7.4
#1 lore raw

PATCH v2 1/7 Visual Studio: upgrade project files to VS2019

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

---
 src/compat/compat.vcxproj             | 12 ++++++------
 src/openvpn/openvpn.vcxproj           | 12 ++++++------
 src/openvpnmsica/openvpnmsica.vcxproj | 14 +++++++-------
 src/openvpnserv/openvpnserv.vcxproj   | 12 ++++++------
 src/tapctl/tapctl.vcxproj             | 14 +++++++-------
 5 files changed, 32 insertions(+), 32 deletions(-)
src/compat/compat.vcxproj +6 -6
index 111dacd..e388008 100644
@@ -22,30 +22,30 @@
     <ProjectGuid>{4B2E2719-E661-45D7-9203-F6F456B22F19}</ProjectGuid>
     <RootNamespace>compat</RootNamespace>
     <Keyword>Win32Proj</Keyword>
-    <WindowsTargetPlatformVersion>10.0.17134.0</WindowsTargetPlatformVersion>
+    <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
   </PropertyGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
     <ConfigurationType>StaticLibrary</ConfigurationType>
     <CharacterSet>MultiByte</CharacterSet>
     <WholeProgramOptimization>true</WholeProgramOptimization>
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
     <ConfigurationType>StaticLibrary</ConfigurationType>
     <CharacterSet>MultiByte</CharacterSet>
     <WholeProgramOptimization>true</WholeProgramOptimization>
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
     <ConfigurationType>StaticLibrary</ConfigurationType>
     <CharacterSet>MultiByte</CharacterSet>
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
     <ConfigurationType>StaticLibrary</ConfigurationType>
     <CharacterSet>MultiByte</CharacterSet>
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
   </PropertyGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
   <ImportGroup Label="ExtensionSettings">
@@ -115,4 +115,4 @@
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <ImportGroup Label="ExtensionTargets">
   </ImportGroup>
-</Project>
+</Project>
\ No newline at end of file
src/openvpn/openvpn.vcxproj +6 -6
index 4ffff2b..e77f026 100644
@@ -22,30 +22,30 @@
     <ProjectGuid>{29DF226E-4D4E-440F-ADAF-5829CFD4CA94}</ProjectGuid>
     <RootNamespace>openvpn</RootNamespace>
     <Keyword>Win32Proj</Keyword>
-    <WindowsTargetPlatformVersion>10.0.17134.0</WindowsTargetPlatformVersion>
+    <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
   </PropertyGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
     <ConfigurationType>Application</ConfigurationType>
     <WholeProgramOptimization>true</WholeProgramOptimization>
     <CharacterSet>Unicode</CharacterSet>
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
     <ConfigurationType>Application</ConfigurationType>
     <WholeProgramOptimization>true</WholeProgramOptimization>
     <CharacterSet>Unicode</CharacterSet>
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
     <ConfigurationType>Application</ConfigurationType>
     <CharacterSet>Unicode</CharacterSet>
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
     <ConfigurationType>Application</ConfigurationType>
     <CharacterSet>Unicode</CharacterSet>
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
   </PropertyGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
   <ImportGroup Label="ExtensionSettings">
@@ -301,4 +301,4 @@
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <ImportGroup Label="ExtensionTargets">
   </ImportGroup>
-</Project>
+</Project>
\ No newline at end of file
src/openvpnmsica/openvpnmsica.vcxproj +7 -7
index 5f1d699..afa4fae 100644
@@ -31,32 +31,32 @@
     <ProjectGuid>{D41AA9D6-B818-476E-992E-0E16EB86BEE2}</ProjectGuid>
     <Keyword>Win32Proj</Keyword>
     <RootNamespace>openvpnmsica</RootNamespace>
-    <WindowsTargetPlatformVersion>10.0.17134.0</WindowsTargetPlatformVersion>
+    <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
   </PropertyGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'" Label="Configuration">
     <ConfigurationType>DynamicLibrary</ConfigurationType>
     <UseDebugLibraries>true</UseDebugLibraries>
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
     <CharacterSet>Unicode</CharacterSet>
     <WindowsSDKDesktopARM64Support>true</WindowsSDKDesktopARM64Support>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
     <ConfigurationType>DynamicLibrary</ConfigurationType>
     <UseDebugLibraries>true</UseDebugLibraries>
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
     <CharacterSet>Unicode</CharacterSet>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
     <ConfigurationType>DynamicLibrary</ConfigurationType>
     <UseDebugLibraries>true</UseDebugLibraries>
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
     <CharacterSet>Unicode</CharacterSet>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'" Label="Configuration">
     <ConfigurationType>DynamicLibrary</ConfigurationType>
     <UseDebugLibraries>false</UseDebugLibraries>
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
     <WholeProgramOptimization>true</WholeProgramOptimization>
     <CharacterSet>Unicode</CharacterSet>
     <WindowsSDKDesktopARM64Support>true</WindowsSDKDesktopARM64Support>
@@ -64,14 +64,14 @@
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
     <ConfigurationType>DynamicLibrary</ConfigurationType>
     <UseDebugLibraries>false</UseDebugLibraries>
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
     <WholeProgramOptimization>true</WholeProgramOptimization>
     <CharacterSet>Unicode</CharacterSet>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
     <ConfigurationType>DynamicLibrary</ConfigurationType>
     <UseDebugLibraries>false</UseDebugLibraries>
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
     <WholeProgramOptimization>true</WholeProgramOptimization>
     <CharacterSet>Unicode</CharacterSet>
   </PropertyGroup>
src/openvpnserv/openvpnserv.vcxproj +6 -6
index 7407757..7061b7b 100644
@@ -22,30 +22,30 @@
     <ProjectGuid>{9C91EE0B-817D-420A-A1E6-15A5A9D98BAD}</ProjectGuid>
     <RootNamespace>openvpnserv</RootNamespace>
     <Keyword>Win32Proj</Keyword>
-    <WindowsTargetPlatformVersion>10.0.17134.0</WindowsTargetPlatformVersion>
+    <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
   </PropertyGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
     <ConfigurationType>Application</ConfigurationType>
     <CharacterSet>Unicode</CharacterSet>
     <WholeProgramOptimization>true</WholeProgramOptimization>
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
     <ConfigurationType>Application</ConfigurationType>
     <CharacterSet>Unicode</CharacterSet>
     <WholeProgramOptimization>true</WholeProgramOptimization>
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
     <ConfigurationType>Application</ConfigurationType>
     <CharacterSet>Unicode</CharacterSet>
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
     <ConfigurationType>Application</ConfigurationType>
     <CharacterSet>Unicode</CharacterSet>
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
   </PropertyGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
   <ImportGroup Label="ExtensionSettings">
@@ -139,4 +139,4 @@
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <ImportGroup Label="ExtensionTargets">
   </ImportGroup>
-</Project>
+</Project>
\ No newline at end of file
src/tapctl/tapctl.vcxproj +7 -7
index 5c1983b..1d593fc 100644
@@ -31,32 +31,32 @@
     <ProjectGuid>{A06436E7-D576-490D-8BA0-0751D920334A}</ProjectGuid>
     <Keyword>Win32Proj</Keyword>
     <RootNamespace>tapctl</RootNamespace>
-    <WindowsTargetPlatformVersion>10.0.17134.0</WindowsTargetPlatformVersion>
+    <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
   </PropertyGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'" Label="Configuration">
     <ConfigurationType>Application</ConfigurationType>
     <UseDebugLibraries>true</UseDebugLibraries>
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
     <CharacterSet>Unicode</CharacterSet>
     <WindowsSDKDesktopARM64Support>true</WindowsSDKDesktopARM64Support>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
     <ConfigurationType>Application</ConfigurationType>
     <UseDebugLibraries>true</UseDebugLibraries>
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
     <CharacterSet>Unicode</CharacterSet>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
     <ConfigurationType>Application</ConfigurationType>
     <UseDebugLibraries>true</UseDebugLibraries>
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
     <CharacterSet>Unicode</CharacterSet>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'" Label="Configuration">
     <ConfigurationType>Application</ConfigurationType>
     <UseDebugLibraries>false</UseDebugLibraries>
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
     <WholeProgramOptimization>true</WholeProgramOptimization>
     <CharacterSet>Unicode</CharacterSet>
     <WindowsSDKDesktopARM64Support>true</WindowsSDKDesktopARM64Support>
@@ -64,14 +64,14 @@
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
     <ConfigurationType>Application</ConfigurationType>
     <UseDebugLibraries>false</UseDebugLibraries>
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
     <WholeProgramOptimization>true</WholeProgramOptimization>
     <CharacterSet>Unicode</CharacterSet>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
     <ConfigurationType>Application</ConfigurationType>
     <UseDebugLibraries>false</UseDebugLibraries>
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
     <WholeProgramOptimization>true</WholeProgramOptimization>
     <CharacterSet>Unicode</CharacterSet>
   </PropertyGroup>
Signature · 1 line
2.7.4
#2 lore raw

Re: [Openvpn-devel] [PATCH v2 1/7] Visual Studio: upgrade project files to VS2019

чт, 7 нояб. 2019 г. в 22:49, Lev Stipakov <[email protected]...>:

Quoted 40 lines

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

---
 src/compat/compat.vcxproj             | 12 ++++++------
 src/openvpn/openvpn.vcxproj           | 12 ++++++------
 src/openvpnmsica/openvpnmsica.vcxproj | 14 +++++++-------
 src/openvpnserv/openvpnserv.vcxproj   | 12 ++++++------
 src/tapctl/tapctl.vcxproj             | 14 +++++++-------
 5 files changed, 32 insertions(+), 32 deletions(-)
src/compat/compat.vcxproj +0 -1
index 111dacd..e388008 100644
@@ -22,30 +22,30 @@
     <ProjectGuid>{4B2E2719-E661-45D7-9203-F6F456B22F19}</ProjectGuid>
     <RootNamespace>compat</RootNamespace>
     <Keyword>Win32Proj</Keyword>
-

<WindowsTargetPlatformVersion>10.0.17134.0</WindowsTargetPlatformVersion>
+ <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> <PropertyGroup
Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType> <CharacterSet>MultiByte</CharacterSet> <WholeProgramOptimization>true</WholeProgramOptimization>

src/compat/compat.vcxproj +1 -1
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
   </PropertyGroup>
   <PropertyGroup

Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType> <CharacterSet>MultiByte</CharacterSet> <WholeProgramOptimization>true</WholeProgramOptimization>

src/compat/compat.vcxproj

Quoted 2 lines
src/compat/compat.vcxproj +1 -1
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>

does it limit target platform ?
can we build for Vista ? XP ? 7 ? does this setting affect that ?

Quoted 289 lines

</PropertyGroup>
<PropertyGroup
Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType> <CharacterSet>MultiByte</CharacterSet>

src/compat/compat.vcxproj +1 -1
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"

Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType> <CharacterSet>MultiByte</CharacterSet>

src/compat/compat.vcxproj +2 -2
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
   </PropertyGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
   <ImportGroup Label="ExtensionSettings">
@@ -115,4 +115,4 @@
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <ImportGroup Label="ExtensionTargets">
   </ImportGroup>
-</Project>
+</Project>
\ No newline at end of file
src/openvpn/openvpn.vcxproj +0 -1
index 4ffff2b..e77f026 100644
@@ -22,30 +22,30 @@
     <ProjectGuid>{29DF226E-4D4E-440F-ADAF-5829CFD4CA94}</ProjectGuid>
     <RootNamespace>openvpn</RootNamespace>
     <Keyword>Win32Proj</Keyword>
-

<WindowsTargetPlatformVersion>10.0.17134.0</WindowsTargetPlatformVersion>
+ <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> <PropertyGroup
Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <WholeProgramOptimization>true</WholeProgramOptimization> <CharacterSet>Unicode</CharacterSet>

src/openvpn/openvpn.vcxproj +1 -1
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
   </PropertyGroup>
   <PropertyGroup

Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <WholeProgramOptimization>true</WholeProgramOptimization> <CharacterSet>Unicode</CharacterSet>

src/openvpn/openvpn.vcxproj +1 -1
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
   </PropertyGroup>
   <PropertyGroup

Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <CharacterSet>Unicode</CharacterSet>

src/openvpn/openvpn.vcxproj +1 -1
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"

Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <CharacterSet>Unicode</CharacterSet>

src/openvpn/openvpn.vcxproj +2 -2
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
   </PropertyGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
   <ImportGroup Label="ExtensionSettings">
@@ -301,4 +301,4 @@
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <ImportGroup Label="ExtensionTargets">
   </ImportGroup>
-</Project>
+</Project>
\ No newline at end of file
src/openvpnmsica/openvpnmsica.vcxproj +0 -1
index 5f1d699..afa4fae 100644
@@ -31,32 +31,32 @@
     <ProjectGuid>{D41AA9D6-B818-476E-992E-0E16EB86BEE2}</ProjectGuid>
     <Keyword>Win32Proj</Keyword>
     <RootNamespace>openvpnmsica</RootNamespace>
-

<WindowsTargetPlatformVersion>10.0.17134.0</WindowsTargetPlatformVersion>
+ <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> <PropertyGroup
Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType> <UseDebugLibraries>true</UseDebugLibraries>

src/openvpnmsica/openvpnmsica.vcxproj +1 -1
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
     <CharacterSet>Unicode</CharacterSet>
     <WindowsSDKDesktopARM64Support>true</WindowsSDKDesktopARM64Support>
   </PropertyGroup>
   <PropertyGroup

Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType> <UseDebugLibraries>true</UseDebugLibraries>

src/openvpnmsica/openvpnmsica.vcxproj +1 -1
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
     <CharacterSet>Unicode</CharacterSet>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"

Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType> <UseDebugLibraries>true</UseDebugLibraries>

src/openvpnmsica/openvpnmsica.vcxproj +1 -1
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
     <CharacterSet>Unicode</CharacterSet>
   </PropertyGroup>
   <PropertyGroup

Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType> <UseDebugLibraries>false</UseDebugLibraries>

src/openvpnmsica/openvpnmsica.vcxproj +1 -1
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
     <WholeProgramOptimization>true</WholeProgramOptimization>
     <CharacterSet>Unicode</CharacterSet>
     <WindowsSDKDesktopARM64Support>true</WindowsSDKDesktopARM64Support>
@@ -64,14 +64,14 @@
   <PropertyGroup

Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType> <UseDebugLibraries>false</UseDebugLibraries>

src/openvpnmsica/openvpnmsica.vcxproj +1 -1
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
     <WholeProgramOptimization>true</WholeProgramOptimization>
     <CharacterSet>Unicode</CharacterSet>
   </PropertyGroup>
   <PropertyGroup

Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType> <UseDebugLibraries>false</UseDebugLibraries>

src/openvpnmsica/openvpnmsica.vcxproj +1 -1
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
     <WholeProgramOptimization>true</WholeProgramOptimization>
     <CharacterSet>Unicode</CharacterSet>
   </PropertyGroup>
src/openvpnserv/openvpnserv.vcxproj +0 -1
index 7407757..7061b7b 100644
@@ -22,30 +22,30 @@
     <ProjectGuid>{9C91EE0B-817D-420A-A1E6-15A5A9D98BAD}</ProjectGuid>
     <RootNamespace>openvpnserv</RootNamespace>
     <Keyword>Win32Proj</Keyword>
-

<WindowsTargetPlatformVersion>10.0.17134.0</WindowsTargetPlatformVersion>
+ <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> <PropertyGroup
Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <CharacterSet>Unicode</CharacterSet> <WholeProgramOptimization>true</WholeProgramOptimization>

src/openvpnserv/openvpnserv.vcxproj +1 -1
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
   </PropertyGroup>
   <PropertyGroup

Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <CharacterSet>Unicode</CharacterSet> <WholeProgramOptimization>true</WholeProgramOptimization>

src/openvpnserv/openvpnserv.vcxproj +1 -1
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
   </PropertyGroup>
   <PropertyGroup

Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <CharacterSet>Unicode</CharacterSet>

src/openvpnserv/openvpnserv.vcxproj +1 -1
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"

Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <CharacterSet>Unicode</CharacterSet>

src/openvpnserv/openvpnserv.vcxproj +2 -2
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
   </PropertyGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
   <ImportGroup Label="ExtensionSettings">
@@ -139,4 +139,4 @@
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <ImportGroup Label="ExtensionTargets">
   </ImportGroup>
-</Project>
+</Project>
\ No newline at end of file
src/tapctl/tapctl.vcxproj +0 -1
index 5c1983b..1d593fc 100644
@@ -31,32 +31,32 @@
     <ProjectGuid>{A06436E7-D576-490D-8BA0-0751D920334A}</ProjectGuid>
     <Keyword>Win32Proj</Keyword>
     <RootNamespace>tapctl</RootNamespace>
-

<WindowsTargetPlatformVersion>10.0.17134.0</WindowsTargetPlatformVersion>
+ <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> <PropertyGroup
Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <UseDebugLibraries>true</UseDebugLibraries>

src/tapctl/tapctl.vcxproj +1 -1
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
     <CharacterSet>Unicode</CharacterSet>
     <WindowsSDKDesktopARM64Support>true</WindowsSDKDesktopARM64Support>
   </PropertyGroup>
   <PropertyGroup

Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <UseDebugLibraries>true</UseDebugLibraries>

src/tapctl/tapctl.vcxproj +1 -1
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
     <CharacterSet>Unicode</CharacterSet>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"

Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <UseDebugLibraries>true</UseDebugLibraries>

src/tapctl/tapctl.vcxproj +1 -1
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
     <CharacterSet>Unicode</CharacterSet>
   </PropertyGroup>
   <PropertyGroup

Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <UseDebugLibraries>false</UseDebugLibraries>

src/tapctl/tapctl.vcxproj +1 -1
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
     <WholeProgramOptimization>true</WholeProgramOptimization>
     <CharacterSet>Unicode</CharacterSet>
     <WindowsSDKDesktopARM64Support>true</WindowsSDKDesktopARM64Support>
@@ -64,14 +64,14 @@
   <PropertyGroup

Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <UseDebugLibraries>false</UseDebugLibraries>

src/tapctl/tapctl.vcxproj +1 -1
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
     <WholeProgramOptimization>true</WholeProgramOptimization>
     <CharacterSet>Unicode</CharacterSet>
   </PropertyGroup>
   <PropertyGroup

Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <UseDebugLibraries>false</UseDebugLibraries>

src/tapctl/tapctl.vcxproj +1 -2
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
     <WholeProgramOptimization>true</WholeProgramOptimization>
     <CharacterSet>Unicode</CharacterSet>
   </PropertyGroup>
--

2.7.4

_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

#3 lore raw

Re: [Openvpn-devel] [PATCH v2 1/7] Visual Studio: upgrade project files to VS2019

With VS2019 you cannot build for XP, you would need to install build tools from VS2017 for that.

On the other hand, we do not build releases with VS, so it should not be an issue.

to 7. marrask. 2019 klo 18.52 Илья Шипицин <[email protected]...> kirjoitti:

Quoted 343 lines

чт, 7 нояб. 2019 г. в 22:49, Lev Stipakov <[email protected]...>:

Quoted 40 lines

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

---
 src/compat/compat.vcxproj             | 12 ++++++------
 src/openvpn/openvpn.vcxproj           | 12 ++++++------
 src/openvpnmsica/openvpnmsica.vcxproj | 14 +++++++-------
 src/openvpnserv/openvpnserv.vcxproj   | 12 ++++++------
 src/tapctl/tapctl.vcxproj             | 14 +++++++-------
 5 files changed, 32 insertions(+), 32 deletions(-)
src/compat/compat.vcxproj +0 -1
index 111dacd..e388008 100644
@@ -22,30 +22,30 @@
     <ProjectGuid>{4B2E2719-E661-45D7-9203-F6F456B22F19}</ProjectGuid>
     <RootNamespace>compat</RootNamespace>
     <Keyword>Win32Proj</Keyword>
-

<WindowsTargetPlatformVersion>10.0.17134.0</WindowsTargetPlatformVersion>
+ <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> <PropertyGroup
Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType> <CharacterSet>MultiByte</CharacterSet> <WholeProgramOptimization>true</WholeProgramOptimization>

src/compat/compat.vcxproj +1 -1
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
   </PropertyGroup>
   <PropertyGroup

Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType> <CharacterSet>MultiByte</CharacterSet> <WholeProgramOptimization>true</WholeProgramOptimization>

src/compat/compat.vcxproj

Quoted 2 lines
src/compat/compat.vcxproj +1 -1
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>

does it limit target platform ?
can we build for Vista ? XP ? 7 ? does this setting affect that ?

Quoted 289 lines

</PropertyGroup>
<PropertyGroup
Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType> <CharacterSet>MultiByte</CharacterSet>

src/compat/compat.vcxproj +1 -1
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"

Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType> <CharacterSet>MultiByte</CharacterSet>

src/compat/compat.vcxproj +2 -2
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
   </PropertyGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
   <ImportGroup Label="ExtensionSettings">
@@ -115,4 +115,4 @@
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <ImportGroup Label="ExtensionTargets">
   </ImportGroup>
-</Project>
+</Project>
\ No newline at end of file
src/openvpn/openvpn.vcxproj +0 -1
index 4ffff2b..e77f026 100644
@@ -22,30 +22,30 @@
     <ProjectGuid>{29DF226E-4D4E-440F-ADAF-5829CFD4CA94}</ProjectGuid>
     <RootNamespace>openvpn</RootNamespace>
     <Keyword>Win32Proj</Keyword>
-

<WindowsTargetPlatformVersion>10.0.17134.0</WindowsTargetPlatformVersion>
+ <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> <PropertyGroup
Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <WholeProgramOptimization>true</WholeProgramOptimization> <CharacterSet>Unicode</CharacterSet>

src/openvpn/openvpn.vcxproj +1 -1
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
   </PropertyGroup>
   <PropertyGroup

Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <WholeProgramOptimization>true</WholeProgramOptimization> <CharacterSet>Unicode</CharacterSet>

src/openvpn/openvpn.vcxproj +1 -1
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
   </PropertyGroup>
   <PropertyGroup

Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <CharacterSet>Unicode</CharacterSet>

src/openvpn/openvpn.vcxproj +1 -1
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"

Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <CharacterSet>Unicode</CharacterSet>

src/openvpn/openvpn.vcxproj +2 -2
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
   </PropertyGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
   <ImportGroup Label="ExtensionSettings">
@@ -301,4 +301,4 @@
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <ImportGroup Label="ExtensionTargets">
   </ImportGroup>
-</Project>
+</Project>
\ No newline at end of file
src/openvpnmsica/openvpnmsica.vcxproj +0 -1
index 5f1d699..afa4fae 100644
@@ -31,32 +31,32 @@
     <ProjectGuid>{D41AA9D6-B818-476E-992E-0E16EB86BEE2}</ProjectGuid>
     <Keyword>Win32Proj</Keyword>
     <RootNamespace>openvpnmsica</RootNamespace>
-

<WindowsTargetPlatformVersion>10.0.17134.0</WindowsTargetPlatformVersion>
+ <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> <PropertyGroup
Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType> <UseDebugLibraries>true</UseDebugLibraries>

src/openvpnmsica/openvpnmsica.vcxproj +1 -1
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
     <CharacterSet>Unicode</CharacterSet>
     <WindowsSDKDesktopARM64Support>true</WindowsSDKDesktopARM64Support>
   </PropertyGroup>
   <PropertyGroup

Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType> <UseDebugLibraries>true</UseDebugLibraries>

src/openvpnmsica/openvpnmsica.vcxproj +1 -1
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
     <CharacterSet>Unicode</CharacterSet>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"

Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType> <UseDebugLibraries>true</UseDebugLibraries>

src/openvpnmsica/openvpnmsica.vcxproj +1 -1
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
     <CharacterSet>Unicode</CharacterSet>
   </PropertyGroup>
   <PropertyGroup

Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType> <UseDebugLibraries>false</UseDebugLibraries>

src/openvpnmsica/openvpnmsica.vcxproj +1 -1
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
     <WholeProgramOptimization>true</WholeProgramOptimization>
     <CharacterSet>Unicode</CharacterSet>
     <WindowsSDKDesktopARM64Support>true</WindowsSDKDesktopARM64Support>
@@ -64,14 +64,14 @@
   <PropertyGroup

Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType> <UseDebugLibraries>false</UseDebugLibraries>

src/openvpnmsica/openvpnmsica.vcxproj +1 -1
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
     <WholeProgramOptimization>true</WholeProgramOptimization>
     <CharacterSet>Unicode</CharacterSet>
   </PropertyGroup>
   <PropertyGroup

Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType> <UseDebugLibraries>false</UseDebugLibraries>

src/openvpnmsica/openvpnmsica.vcxproj +1 -1
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
     <WholeProgramOptimization>true</WholeProgramOptimization>
     <CharacterSet>Unicode</CharacterSet>
   </PropertyGroup>
src/openvpnserv/openvpnserv.vcxproj +0 -1
index 7407757..7061b7b 100644
@@ -22,30 +22,30 @@
     <ProjectGuid>{9C91EE0B-817D-420A-A1E6-15A5A9D98BAD}</ProjectGuid>
     <RootNamespace>openvpnserv</RootNamespace>
     <Keyword>Win32Proj</Keyword>
-

<WindowsTargetPlatformVersion>10.0.17134.0</WindowsTargetPlatformVersion>
+ <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> <PropertyGroup
Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <CharacterSet>Unicode</CharacterSet> <WholeProgramOptimization>true</WholeProgramOptimization>

src/openvpnserv/openvpnserv.vcxproj +1 -1
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
   </PropertyGroup>
   <PropertyGroup

Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <CharacterSet>Unicode</CharacterSet> <WholeProgramOptimization>true</WholeProgramOptimization>

src/openvpnserv/openvpnserv.vcxproj +1 -1
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
   </PropertyGroup>
   <PropertyGroup

Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <CharacterSet>Unicode</CharacterSet>

src/openvpnserv/openvpnserv.vcxproj +1 -1
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"

Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <CharacterSet>Unicode</CharacterSet>

src/openvpnserv/openvpnserv.vcxproj +2 -2
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
   </PropertyGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
   <ImportGroup Label="ExtensionSettings">
@@ -139,4 +139,4 @@
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <ImportGroup Label="ExtensionTargets">
   </ImportGroup>
-</Project>
+</Project>
\ No newline at end of file
src/tapctl/tapctl.vcxproj +0 -1
index 5c1983b..1d593fc 100644
@@ -31,32 +31,32 @@
     <ProjectGuid>{A06436E7-D576-490D-8BA0-0751D920334A}</ProjectGuid>
     <Keyword>Win32Proj</Keyword>
     <RootNamespace>tapctl</RootNamespace>
-

<WindowsTargetPlatformVersion>10.0.17134.0</WindowsTargetPlatformVersion>
+ <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> <PropertyGroup
Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <UseDebugLibraries>true</UseDebugLibraries>

src/tapctl/tapctl.vcxproj +1 -1
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
     <CharacterSet>Unicode</CharacterSet>
     <WindowsSDKDesktopARM64Support>true</WindowsSDKDesktopARM64Support>
   </PropertyGroup>
   <PropertyGroup

Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <UseDebugLibraries>true</UseDebugLibraries>

src/tapctl/tapctl.vcxproj +1 -1
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
     <CharacterSet>Unicode</CharacterSet>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"

Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <UseDebugLibraries>true</UseDebugLibraries>

src/tapctl/tapctl.vcxproj +1 -1
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
     <CharacterSet>Unicode</CharacterSet>
   </PropertyGroup>
   <PropertyGroup

Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <UseDebugLibraries>false</UseDebugLibraries>

src/tapctl/tapctl.vcxproj +1 -1
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
     <WholeProgramOptimization>true</WholeProgramOptimization>
     <CharacterSet>Unicode</CharacterSet>
     <WindowsSDKDesktopARM64Support>true</WindowsSDKDesktopARM64Support>
@@ -64,14 +64,14 @@
   <PropertyGroup

Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <UseDebugLibraries>false</UseDebugLibraries>

src/tapctl/tapctl.vcxproj +1 -1
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
     <WholeProgramOptimization>true</WholeProgramOptimization>
     <CharacterSet>Unicode</CharacterSet>
   </PropertyGroup>
   <PropertyGroup

Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <UseDebugLibraries>false</UseDebugLibraries>

src/tapctl/tapctl.vcxproj +1 -2
-    <PlatformToolset>v141</PlatformToolset>
+    <PlatformToolset>v142</PlatformToolset>
     <WholeProgramOptimization>true</WholeProgramOptimization>
     <CharacterSet>Unicode</CharacterSet>
   </PropertyGroup>
--

2.7.4

_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

#4 lore raw

Re: [Openvpn-devel] [PATCH v2 1/7] Visual Studio: upgrade project files to VS2019

Hi,

Quoted 5 lines · Lev Stipakov

With VS2019 you cannot build for XP, you would need to install build tools from VS2017 for that.

On the other hand, we do not build releases with VS, so it should not be an issue.

Not sure right now about 2.4, but for master/2.5, we dropped support for XP/Vista long ago.

So "not being able to build for XP" would not be a problem.

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...
Acked-by on v2 1/7
#5 lore raw

Re: [Openvpn-devel] [PATCH v2 1/7] Visual Studio: upgrade project files to VS2019

In reply to Lev Stipakov

Note, the OpenSSL and other dependencies should also be built using the same version of MSVC.

Best regards,
Simon

-----Original Message-----
From: Lev Stipakov [mailto:[email protected]...]
Sent: Thursday, November 7, 2019 6:45 PM
To: [email protected]
Cc: Lev Stipakov <[email protected]...>
Subject: [Openvpn-devel] [PATCH v2 1/7] Visual Studio: upgrade project files to VS2019

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

---
 src/compat/compat.vcxproj             | 12 ++++++------
 src/openvpn/openvpn.vcxproj           | 12 ++++++------
 src/openvpnmsica/openvpnmsica.vcxproj | 14 +++++++-------
 src/openvpnserv/openvpnserv.vcxproj   | 12 ++++++------
 src/tapctl/tapctl.vcxproj             | 14 +++++++-------
 5 files changed, 32 insertions(+), 32 deletions(-)
src/compat/compat.vcxproj +0 -1
index 111dacd..e388008 100644
@@ -22,30 +22,30 @@
     <ProjectGuid>{4B2E2719-E661-45D7-9203-F6F456B22F19}</ProjectGuid>
     <RootNamespace>compat</RootNamespace>
     <Keyword>Win32Proj</Keyword>
-

<WindowsTargetPlatformVersion>10.0.17134.0</WindowsTargetPlatformVersion>
+ <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> <PropertyGroup
Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType> <CharacterSet>MultiByte</CharacterSet> <WholeProgramOptimization>true</WholeProgramOptimization>
- <PlatformToolset>v141</PlatformToolset>
+ <PlatformToolset>v142</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"
Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType> <CharacterSet>MultiByte</CharacterSet> <WholeProgramOptimization>true</WholeProgramOptimization>
- <PlatformToolset>v141</PlatformToolset>
+ <PlatformToolset>v142</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"
Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType> <CharacterSet>MultiByte</CharacterSet>
- <PlatformToolset>v141</PlatformToolset>
+ <PlatformToolset>v142</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"
Label="Configuration">
<ConfigurationType>StaticLibrary</ConfigurationType> <CharacterSet>MultiByte</CharacterSet>
- <PlatformToolset>v141</PlatformToolset>
+ <PlatformToolset>v142</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> <ImportGroup Label="ExtensionSettings">
@@ -115,4 +115,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <ImportGroup Label="ExtensionTargets">
</ImportGroup>
-</Project>
+</Project>
\ No newline at end of file

src/openvpn/openvpn.vcxproj +0 -1
index 4ffff2b..e77f026 100644
@@ -22,30 +22,30 @@
     <ProjectGuid>{29DF226E-4D4E-440F-ADAF-5829CFD4CA94}</ProjectGuid>
     <RootNamespace>openvpn</RootNamespace>
     <Keyword>Win32Proj</Keyword>
-

<WindowsTargetPlatformVersion>10.0.17134.0</WindowsTargetPlatformVersion>
+ <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> <PropertyGroup
Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <WholeProgramOptimization>true</WholeProgramOptimization> <CharacterSet>Unicode</CharacterSet>
- <PlatformToolset>v141</PlatformToolset>
+ <PlatformToolset>v142</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"
Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <WholeProgramOptimization>true</WholeProgramOptimization> <CharacterSet>Unicode</CharacterSet>
- <PlatformToolset>v141</PlatformToolset>
+ <PlatformToolset>v142</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"
Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <CharacterSet>Unicode</CharacterSet>
- <PlatformToolset>v141</PlatformToolset>
+ <PlatformToolset>v142</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"
Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <CharacterSet>Unicode</CharacterSet>
- <PlatformToolset>v141</PlatformToolset>
+ <PlatformToolset>v142</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> <ImportGroup Label="ExtensionSettings">
@@ -301,4 +301,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <ImportGroup Label="ExtensionTargets">
</ImportGroup>
-</Project>
+</Project>
\ No newline at end of file

(quoted diff) +0 -0

b/src/openvpnmsica/openvpnmsica.vcxproj
index 5f1d699..afa4fae 100644

src/openvpnmsica/openvpnmsica.vcxproj +0 -1
@@ -31,32 +31,32 @@
     <ProjectGuid>{D41AA9D6-B818-476E-992E-0E16EB86BEE2}</ProjectGuid>
     <Keyword>Win32Proj</Keyword>
     <RootNamespace>openvpnmsica</RootNamespace>
-

<WindowsTargetPlatformVersion>10.0.17134.0</WindowsTargetPlatformVersion>
+ <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'"
Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType> <UseDebugLibraries>true</UseDebugLibraries>
- <PlatformToolset>v141</PlatformToolset>
+ <PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet> <WindowsSDKDesktopARM64Support>true</WindowsSDKDesktopARM64Support>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"
Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType> <UseDebugLibraries>true</UseDebugLibraries>
- <PlatformToolset>v141</PlatformToolset>
+ <PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"
Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType> <UseDebugLibraries>true</UseDebugLibraries>
- <PlatformToolset>v141</PlatformToolset>
+ <PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup
Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType> <UseDebugLibraries>false</UseDebugLibraries>
- <PlatformToolset>v141</PlatformToolset>
+ <PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization> <CharacterSet>Unicode</CharacterSet> <WindowsSDKDesktopARM64Support>true</WindowsSDKDesktopARM64Support>
@@ -64,14 +64,14 @@
<PropertyGroup
Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType> <UseDebugLibraries>false</UseDebugLibraries>
- <PlatformToolset>v141</PlatformToolset>
+ <PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization> <CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"
Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType> <UseDebugLibraries>false</UseDebugLibraries>
- <PlatformToolset>v141</PlatformToolset>
+ <PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization> <CharacterSet>Unicode</CharacterSet>
</PropertyGroup>

(quoted diff) +0 -0

b/src/openvpnserv/openvpnserv.vcxproj
index 7407757..7061b7b 100644

src/openvpnserv/openvpnserv.vcxproj +0 -1
@@ -22,30 +22,30 @@
     <ProjectGuid>{9C91EE0B-817D-420A-A1E6-15A5A9D98BAD}</ProjectGuid>
     <RootNamespace>openvpnserv</RootNamespace>
     <Keyword>Win32Proj</Keyword>
-

<WindowsTargetPlatformVersion>10.0.17134.0</WindowsTargetPlatformVersion>
+ <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> <PropertyGroup
Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <CharacterSet>Unicode</CharacterSet> <WholeProgramOptimization>true</WholeProgramOptimization>
- <PlatformToolset>v141</PlatformToolset>
+ <PlatformToolset>v142</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"
Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <CharacterSet>Unicode</CharacterSet> <WholeProgramOptimization>true</WholeProgramOptimization>
- <PlatformToolset>v141</PlatformToolset>
+ <PlatformToolset>v142</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"
Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <CharacterSet>Unicode</CharacterSet>
- <PlatformToolset>v141</PlatformToolset>
+ <PlatformToolset>v142</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"
Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <CharacterSet>Unicode</CharacterSet>
- <PlatformToolset>v141</PlatformToolset>
+ <PlatformToolset>v142</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> <ImportGroup Label="ExtensionSettings">
@@ -139,4 +139,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <ImportGroup Label="ExtensionTargets">
</ImportGroup>
-</Project>
+</Project>
\ No newline at end of file

src/tapctl/tapctl.vcxproj +0 -1
index 5c1983b..1d593fc 100644
@@ -31,32 +31,32 @@
     <ProjectGuid>{A06436E7-D576-490D-8BA0-0751D920334A}</ProjectGuid>
     <Keyword>Win32Proj</Keyword>
     <RootNamespace>tapctl</RootNamespace>
-

<WindowsTargetPlatformVersion>10.0.17134.0</WindowsTargetPlatformVersion>
+ <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'"
Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <UseDebugLibraries>true</UseDebugLibraries>
- <PlatformToolset>v141</PlatformToolset>
+ <PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet> <WindowsSDKDesktopARM64Support>true</WindowsSDKDesktopARM64Support>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"
Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <UseDebugLibraries>true</UseDebugLibraries>
- <PlatformToolset>v141</PlatformToolset>
+ <PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"
Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <UseDebugLibraries>true</UseDebugLibraries>
- <PlatformToolset>v141</PlatformToolset>
+ <PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup
Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <UseDebugLibraries>false</UseDebugLibraries>
- <PlatformToolset>v141</PlatformToolset>
+ <PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization> <CharacterSet>Unicode</CharacterSet> <WindowsSDKDesktopARM64Support>true</WindowsSDKDesktopARM64Support>
@@ -64,14 +64,14 @@
<PropertyGroup
Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <UseDebugLibraries>false</UseDebugLibraries>
- <PlatformToolset>v141</PlatformToolset>
+ <PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization> <CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"
Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <UseDebugLibraries>false</UseDebugLibraries>
- <PlatformToolset>v141</PlatformToolset>
+ <PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization> <CharacterSet>Unicode</CharacterSet>
</PropertyGroup>

Signature · 8 lines
2.7.4



_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel
#6 lore raw

[Openvpn-devel] [PATCH applied] Re: Visual Studio: upgrade project files to VS2019

In reply to Lev Stipakov

Your patch has been applied to the master branch.

commit 472f9809430ec7311a98df134b4a91928ade5a6f

Author: Lev Stipakov
Date:   Thu Nov 7 19:45:23 2019 +0200
     Visual Studio: upgrade project files to VS2019

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

--
kind regards,

Gert Doering

#7 lore raw

PATCH v2 2/7 wintun: add --windows-driver config option

In reply to Lev Stipakov

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

This allows to specify which tun driver openvpn should use, tap-windows6 (default) or wintun.

Note than wintun support will be added in follow-up patches.

---
 src/openvpn/init.c    |  7 +++++++
 src/openvpn/options.c | 37 +++++++++++++++++++++++++++++++++++++
 src/openvpn/options.h |  1 +
 src/openvpn/tun.h     |  1 +
 4 files changed, 46 insertions(+)
src/openvpn/init.c +7 -0
index ae7bd63..c6d4953 100644
@@ -1733,6 +1733,10 @@ do_init_tun(struct context *c)
                             c->c2.es,
                             &c->net_ctx);
 
+#ifdef _WIN32
+    c->c1.tuntap->wintun = c->options.wintun;
+#endif
+
     init_tun_post(c->c1.tuntap,
                   &c->c2.frame,
                   &c->options.tuntap_options);
@@ -1775,6 +1779,9 @@ do_open_tun(struct context *c)
     /* store (hide) interactive service handle in tuntap_options */
     c->c1.tuntap->options.msg_channel = c->options.msg_channel;
     msg(D_ROUTE, "interactive service msg_channel=%u", (unsigned int) c->options.msg_channel);
+
+    c->c1.tuntap->wintun = c->options.wintun;
+
 #endif
 
     /* allocate route list structure */
src/openvpn/options.c +37 -0
index 1838a69..5c5033e 100644
@@ -747,6 +747,9 @@ static const char usage_message[] =
     "                       optional parameter controls the initial state of ex.\n"
     "--show-net-up   : Show " PACKAGE_NAME "'s view of routing table and net adapter list\n"
     "                  after TAP adapter is up and routes have been added.\n"
+    "--windows-driver   : Which tun driver to use?\n"
+    "                     tap-windows6 (default)\n"
+    "                     wintun\n"
 #ifdef _WIN32
     "--block-outside-dns   : Block DNS on other network adapters to prevent DNS leaks\n"
 #endif
@@ -851,6 +854,7 @@ init_options(struct options *o, const bool init_gc)
     o->tuntap_options.dhcp_masq_offset = 0;     /* use network address as internal DHCP server address */
     o->route_method = ROUTE_METHOD_ADAPTIVE;
     o->block_outside_dns = false;
+    o->wintun = false;
 #endif
     o->vlan_accept = VLAN_ONLY_UNTAGGED_OR_PRIORITY;
     o->vlan_pvid = 1;
@@ -2994,6 +2998,12 @@ options_postprocess_mutate_invariant(struct options *options)
         options->ifconfig_noexec = false;
     }
 
+    /* for wintun kernel doesn't send DHCP requests, so use ipapi to set IP address and netmask */
+    if (options->wintun)
+    {
+        options->tuntap_options.ip_win32_type = IPW32_SET_IPAPI;
+    }
+
     remap_redirect_gateway_flags(options);
 #endif
 
@@ -4039,6 +4049,26 @@ foreign_option(struct options *o, char *argv[], int len, struct env_set *es)
     }
 }
 
+#ifdef _WIN32
+bool
+parse_windows_driver(const char *str, const int msglevel)
+{
+    if (streq(str, "tap-windows6"))
+    {
+        return false;
+    }
+    else if (streq(str, "wintun"))
+    {
+        return true;
+    }
+    else
+    {
+        msg(msglevel, "--windows-driver must be tap-windows6 or wintun");
+        return false;
+    }
+}
+#endif
+
 /*
  * parse/print topology coding
  */
@@ -5281,6 +5311,13 @@ add_option(struct options *options,
         VERIFY_PERMISSION(OPT_P_GENERAL);
         options->dev_type = p[1];
     }
+#ifdef _WIN32
+    else if (streq(p[0], "windows-driver") && p[1] && !p[2])
+    {
+        VERIFY_PERMISSION(OPT_P_GENERAL);
+        options->wintun = parse_windows_driver(p[1], M_FATAL);
+    }
+#endif
     else if (streq(p[0], "dev-node") && p[1] && !p[2])
     {
         VERIFY_PERMISSION(OPT_P_GENERAL);
src/openvpn/options.h +1 -0
index ff7a5bb..0a24e5e 100644
@@ -632,6 +632,7 @@ struct options
     bool show_net_up;
     int route_method;
     bool block_outside_dns;
+    bool wintun;
 #endif
 
     bool use_peer_id;
src/openvpn/tun.h +1 -0
index 5a0a933..df935f6 100644
@@ -175,6 +175,7 @@ struct tuntap
      * ~0 if undefined */
     DWORD adapter_index;
 
+    bool wintun; /* true if wintun is used instead of tap-windows6 */
     int standby_iter;
 #else  /* ifdef _WIN32 */
     int fd; /* file descriptor for TUN/TAP dev */
Signature · 1 line
2.7.4
#8 lore raw

Re: [Openvpn-devel] [PATCH v2 2/7] wintun: add --windows-driver config option

Hi,

Thanks for looking into wintun support. Definitely feature-ack.

Quoted 7 lines · Lev Stipakov

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

This allows to specify which tun driver openvpn should use, tap-windows6 (default) or wintun.

Note than wintun support will be added in follow-up patches.

Quoted 7 lines
---
 src/openvpn/init.c    |  7 +++++++
 src/openvpn/options.c | 37 +++++++++++++++++++++++++++++++++++++
 src/openvpn/options.h |  1 +
 src/openvpn/tun.h     |  1 +
 4 files changed, 46 insertions(+)

Should there not be a manpage entry in this commit too?

Quoted 17 lines
src/openvpn/init.c +4 -0
index ae7bd63..c6d4953 100644
@@ -1733,6 +1733,10 @@ do_init_tun(struct context *c)
                             c->c2.es,
                             &c->net_ctx);
 
+#ifdef _WIN32
+    c->c1.tuntap->wintun = c->options.wintun;
+#endif
+
     init_tun_post(c->c1.tuntap,
                   &c->c2.frame,
                   &c->options.tuntap_options);

src/openvpn/init.c do_open_tun(struct context *c) · line 1784

Quoted 6 lines
src/openvpn/init.c +3 -0
@@ -1775,6 +1779,9 @@ do_open_tun(struct context *c)
     /* store (hide) interactive service handle in tuntap_options */
     c->c1.tuntap->options.msg_channel = c->options.msg_channel;
     msg(D_ROUTE, "interactive service msg_channel=%u", (unsigned int) c->options.msg_channel);
+
+    c->c1.tuntap->wintun = c->options.wintun;
+

Why do we have to set c->c2.tuntap->wintun in both do_init_tun and do_open_tun? Should one of them not suffice?

Quoted 8 lines
src/openvpn/init.c +0 -0
 #endif
 
     /* allocate route list structure */
src/openvpn/options.c +0 -0
index 1838a69..5c5033e 100644

src/openvpn/options.c static const char usage_message[] = · line 753

Quoted 7 lines
src/openvpn/options.c +3 -0
@@ -747,6 +747,9 @@ static const char usage_message[] =
     "                       optional parameter controls the initial state of ex.\n"
     "--show-net-up   : Show " PACKAGE_NAME "'s view of routing table and net adapter list\n"
     "                  after TAP adapter is up and routes have been added.\n"
+    "--windows-driver   : Which tun driver to use?\n"
+    "                     tap-windows6 (default)\n"
+    "                     wintun\n"
 #ifdef _WIN32

Should this --windows-driver option not be inside the #ifdef _WIN32 ?

Quoted 24 lines
src/openvpn/options.c +7 -0
     "--block-outside-dns   : Block DNS on other network adapters to prevent DNS leaks\n"
 #endif
@@ -851,6 +854,7 @@ init_options(struct options *o, const bool init_gc)
     o->tuntap_options.dhcp_masq_offset = 0;     /* use network address as internal DHCP server address */
     o->route_method = ROUTE_METHOD_ADAPTIVE;
     o->block_outside_dns = false;
+    o->wintun = false;
 #endif
     o->vlan_accept = VLAN_ONLY_UNTAGGED_OR_PRIORITY;
     o->vlan_pvid = 1;
@@ -2994,6 +2998,12 @@ options_postprocess_mutate_invariant(struct options *options)
         options->ifconfig_noexec = false;
     }
 
+    /* for wintun kernel doesn't send DHCP requests, so use ipapi to set IP address and netmask */
+    if (options->wintun)
+    {
+        options->tuntap_options.ip_win32_type = IPW32_SET_IPAPI;
+    }
+
     remap_redirect_gateway_flags(options);
 #endif
 

src/openvpn/options.c foreign_option(struct options *o, char *argv[], int len, struct env_set *es) · line 4054

Quoted 6 lines
src/openvpn/options.c +3 -0
@@ -4039,6 +4049,26 @@ foreign_option(struct options *o, char *argv[], int len, struct env_set *es)
     }
 }
 
+#ifdef _WIN32
+bool
+parse_windows_driver(const char *str, const int msglevel)

I think this should be a static function. Also a short (doxygen) comment explaining what the return value means would be nice.

Quoted 39 lines
src/openvpn/options.c +24 -0
+{
+    if (streq(str, "tap-windows6"))
+    {
+        return false;
+    }
+    else if (streq(str, "wintun"))
+    {
+        return true;
+    }
+    else
+    {
+        msg(msglevel, "--windows-driver must be tap-windows6 or wintun");
+        return false;
+    }
+}
+#endif
+
 /*
  * parse/print topology coding
  */
@@ -5281,6 +5311,13 @@ add_option(struct options *options,
         VERIFY_PERMISSION(OPT_P_GENERAL);
         options->dev_type = p[1];
     }
+#ifdef _WIN32
+    else if (streq(p[0], "windows-driver") && p[1] && !p[2])
+    {
+        VERIFY_PERMISSION(OPT_P_GENERAL);
+        options->wintun = parse_windows_driver(p[1], M_FATAL);
+    }
+#endif
     else if (streq(p[0], "dev-node") && p[1] && !p[2])
     {
         VERIFY_PERMISSION(OPT_P_GENERAL);
src/openvpn/options.h +0 -0
index ff7a5bb..0a24e5e 100644

src/openvpn/options.h struct options · line 635

Quoted 4 lines
src/openvpn/options.h +1 -0
@@ -632,6 +632,7 @@ struct options
     bool show_net_up;
     int route_method;
     bool block_outside_dns;
+    bool wintun;

Did you consider using an enum instead? I think it would make the code easier to read. E.g. with

    typedef enum { WINDRV_TAP6, WINDRV_WINTUN } windrv_t;

in tun.h or so, we'd get

    options->windrv = WINDRV_TAP6

instead of

    options->wintun = false.
Quoted 9 lines
src/openvpn/options.h +0 -0
 #endif
 
     bool use_peer_id;
src/openvpn/tun.h +0 -0
index 5a0a933..df935f6 100644

src/openvpn/tun.h struct tuntap · line 181

Quoted 7 lines
src/openvpn/tun.h +1 -0
@@ -175,6 +175,7 @@ struct tuntap
      * ~0 if undefined */
     DWORD adapter_index;
 
+    bool wintun; /* true if wintun is used instead of tap-windows6 */
     int standby_iter;
 #else  /* ifdef _WIN32 */
     int fd; /* file descriptor for TUN/TAP dev */

Thanks,
-Steffan

#9 lore raw

Re: [Openvpn-devel] [PATCH v2 2/7] wintun: add --windows-driver config option

Hi,

Should there not be a manpage entry in this commit too?

Quoted 1 line

Indeed. Added to manpage.

Why do we have to set c->c2.tuntap->wintun in both do_init_tun and

Quoted 2 lines

do_open_tun? Should one of them not suffice?

Just to be sure that it is assigned :)

Removed unneeded second assignment.

Should this --windows-driver option not be inside the #ifdef _WIN32 ?

Quoted 1 line

In fact it is already in _WIN32, but the next option has #ifdef _WIN32 again,
so I removed it.

Quoted 7 lines

src/openvpn/options.c foreign_option(struct options *o, char *argv[], int len, struct env_set *es) · line 4054

Quoted 3 lines
src/openvpn/options.c +3 -0
+#ifdef _WIN32
+bool
+parse_windows_driver(const char *str, const int msglevel)

I think this should be a static function. Also a short (doxygen) comment explaining what the return value means would be nice.

Done.

Quoted 5 lines
Quoted 1 line

+ bool wintun;

Did you consider using an enum instead? I think it would make the code easier to read.

Honestly I do not see to much value in converting bool to enum, it is unlikely that we'll have more tun drivers in the near future. Also changing it here would break follow-up patches.

As verbally agreed, I'll look into it afterwards.

v3 is on its way.

Signature · 1 line
-Lev
#10 lore raw

PATCH v3 1/1 wintun: add --windows-driver config option

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

This allows to specify which tun driver openvpn should use, tap-windows6 (default) or wintun.

Note than wintun support will be added in follow-up patches.

---

v3:
- added man page
- removed unneccesary assignment
- make method static
- removed unneeded _WIN32 define

v2:
- rebased on top of latest master

 doc/openvpn.8         |  8 ++++++++
 src/openvpn/init.c    |  4 ++++
 src/openvpn/options.c | 46 ++++++++++++++++++++++++++++++++++++++++++++--
 src/openvpn/options.h |  1 +
 src/openvpn/tun.h     |  1 +
 5 files changed, 58 insertions(+), 2 deletions(-)
doc/openvpn.8 +8 -0
index 11daa92..8a7e70f 100644
@@ -6147,6 +6147,14 @@ Note that pushing unknown options from server does not trigger
 fatal errors.
 .\"*********************************************************
 .TP
+.B \-\-windows\-driver
+Specifies which tun driver to use. Values are
+.B tap-windows6
+(default) and
+.B wintun.
+This is Windows-only option.
+.\"*********************************************************
+.TP
 .B \-\-dhcp\-renew
 Ask Windows to renew the TAP adapter lease on startup.
 This option is normally unnecessary, as Windows automatically
src/openvpn/init.c +4 -0
index ae7bd63..0bdb0a9 100644
@@ -1733,6 +1733,10 @@ do_init_tun(struct context *c)
                             c->c2.es,
                             &c->net_ctx);
 
+#ifdef _WIN32
+    c->c1.tuntap->wintun = c->options.wintun;
+#endif
+
     init_tun_post(c->c1.tuntap,
                   &c->c2.frame,
                   &c->options.tuntap_options);
src/openvpn/options.c +44 -2

77 lines not on this page · raw message

src/openvpn/options.h +1 -0
index ff7a5bb..0a24e5e 100644
@@ -632,6 +632,7 @@ struct options
     bool show_net_up;
     int route_method;
     bool block_outside_dns;
+    bool wintun;
 #endif
 
     bool use_peer_id;
src/openvpn/tun.h +1 -0
index 5a0a933..df935f6 100644
@@ -175,6 +175,7 @@ struct tuntap
      * ~0 if undefined */
     DWORD adapter_index;
 
+    bool wintun; /* true if wintun is used instead of tap-windows6 */
     int standby_iter;
 #else  /* ifdef _WIN32 */
     int fd; /* file descriptor for TUN/TAP dev */
Signature · 1 line
2.7.4
Acked-by on v3 1/1
#11 lore raw

Re: [Openvpn-devel] [PATCH v3] wintun: add --windows-driver config option

Hi,

Quoted 164 lines · Lev Stipakov

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

This allows to specify which tun driver openvpn should use, tap-windows6 (default) or wintun.

Note than wintun support will be added in follow-up patches.

---

v3:
- added man page
- removed unneccesary assignment
- make method static
- removed unneeded _WIN32 define

v2:
- rebased on top of latest master

 doc/openvpn.8         |  8 ++++++++
 src/openvpn/init.c    |  4 ++++
 src/openvpn/options.c | 46 ++++++++++++++++++++++++++++++++++++++++++++--
 src/openvpn/options.h |  1 +
 src/openvpn/tun.h     |  1 +
 5 files changed, 58 insertions(+), 2 deletions(-)
doc/openvpn.8 +8 -0
index 11daa92..8a7e70f 100644
@@ -6147,6 +6147,14 @@ Note that pushing unknown options from server does not trigger
 fatal errors.
 .\"*********************************************************
 .TP
+.B \-\-windows\-driver
+Specifies which tun driver to use. Values are
+.B tap-windows6
+(default) and
+.B wintun.
+This is Windows-only option.
+.\"*********************************************************
+.TP
 .B \-\-dhcp\-renew
 Ask Windows to renew the TAP adapter lease on startup.
 This option is normally unnecessary, as Windows automatically
src/openvpn/init.c +4 -0
index ae7bd63..0bdb0a9 100644
@@ -1733,6 +1733,10 @@ do_init_tun(struct context *c)
                             c->c2.es,
                             &c->net_ctx);
 
+#ifdef _WIN32
+    c->c1.tuntap->wintun = c->options.wintun;
+#endif
+
     init_tun_post(c->c1.tuntap,
                   &c->c2.frame,
                   &c->options.tuntap_options);
src/openvpn/options.c +44 -2
index 1838a69..a70a822 100644
@@ -747,9 +747,10 @@ static const char usage_message[] =
     "                       optional parameter controls the initial state of ex.\n"
     "--show-net-up   : Show " PACKAGE_NAME "'s view of routing table and net adapter list\n"
     "                  after TAP adapter is up and routes have been added.\n"
-#ifdef _WIN32
+    "--windows-driver   : Which tun driver to use?\n"
+    "                     tap-windows6 (default)\n"
+    "                     wintun\n"
     "--block-outside-dns   : Block DNS on other network adapters to prevent DNS leaks\n"
-#endif
     "Windows Standalone Options:\n"
     "\n"
     "--show-adapters : Show all TAP-Windows adapters.\n"
@@ -851,6 +852,7 @@ init_options(struct options *o, const bool init_gc)
     o->tuntap_options.dhcp_masq_offset = 0;     /* use network address as internal DHCP server address */
     o->route_method = ROUTE_METHOD_ADAPTIVE;
     o->block_outside_dns = false;
+    o->wintun = false;
 #endif
     o->vlan_accept = VLAN_ONLY_UNTAGGED_OR_PRIORITY;
     o->vlan_pvid = 1;
@@ -2994,6 +2996,12 @@ options_postprocess_mutate_invariant(struct options *options)
         options->ifconfig_noexec = false;
     }
 
+    /* for wintun kernel doesn't send DHCP requests, so use ipapi to set IP address and netmask */
+    if (options->wintun)
+    {
+        options->tuntap_options.ip_win32_type = IPW32_SET_IPAPI;
+    }
+
     remap_redirect_gateway_flags(options);
 #endif
 
@@ -4039,6 +4047,33 @@ foreign_option(struct options *o, char *argv[], int len, struct env_set *es)
     }
 }
 
+#ifdef _WIN32
+/**
+ * Parses --windows-driver config option
+ *
+ * @param str       value of --windows-driver option
+ * @param msglevel  msglevel to report parsing error
+ * @return bool     true if --windows-driver is wintun, false otherwise
+ */
+static bool
+parse_windows_driver(const char *str, const int msglevel)
+{
+    if (streq(str, "tap-windows6"))
+    {
+        return false;
+    }
+    else if (streq(str, "wintun"))
+    {
+        return true;
+    }
+    else
+    {
+        msg(msglevel, "--windows-driver must be tap-windows6 or wintun");
+        return false;
+    }
+}
+#endif
+
 /*
  * parse/print topology coding
  */
@@ -5281,6 +5316,13 @@ add_option(struct options *options,
         VERIFY_PERMISSION(OPT_P_GENERAL);
         options->dev_type = p[1];
     }
+#ifdef _WIN32
+    else if (streq(p[0], "windows-driver") && p[1] && !p[2])
+    {
+        VERIFY_PERMISSION(OPT_P_GENERAL);
+        options->wintun = parse_windows_driver(p[1], M_FATAL);
+    }
+#endif
     else if (streq(p[0], "dev-node") && p[1] && !p[2])
     {
         VERIFY_PERMISSION(OPT_P_GENERAL);
src/openvpn/options.h +1 -0
index ff7a5bb..0a24e5e 100644
@@ -632,6 +632,7 @@ struct options
     bool show_net_up;
     int route_method;
     bool block_outside_dns;
+    bool wintun;
 #endif
 
     bool use_peer_id;
src/openvpn/tun.h +0 -0
index 5a0a933..df935f6 100644

src/openvpn/tun.h struct tuntap · line 181

Quoted 7 lines
src/openvpn/tun.h +1 -0
@@ -175,6 +175,7 @@ struct tuntap
      * ~0 if undefined */
     DWORD adapter_index;
 
+    bool wintun; /* true if wintun is used instead of tap-windows6 */
     int standby_iter;
 #else  /* ifdef _WIN32 */
     int fd; /* file descriptor for TUN/TAP dev */

Thanks, this looks good to me now.

I don't have a working Windows test setup available, so only did stare-at-code and verified that building with mingw still works for me. So, as far as this goes without real testing:

-Steffan

#12 lore raw

[Openvpn-devel] [PATCH applied] Re: wintun: add --windows-driver config option

In reply to Lev Stipakov

Your patch has been applied to the master branch.

(It needed a bit of manual massaging as the context in options.c:851 changed due to the merge of the VLAN patches)

I'm not totally happy with introducing a new function (parse_windows_driver()) to effectively map two constant strings to a boolan variable... but since this got ACKed already, in it goes...

commit c0658271be5b45a201eb6aba1e4bdc5a729285be

Author: Lev Stipakov
Date:   Fri Nov 8 19:37:35 2019 +0200
     wintun: add --windows-driver config option

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

--
kind regards,

Gert Doering

#13 lore raw

Re: [Openvpn-devel] [PATCH applied] Re: wintun: add --windows-driver config option

Hi,

Quoted 4 lines · Gert Doering
     wintun: add --windows-driver config option

     Signed-off-by: Lev Stipakov <[email protected]...>
     Acked-by: Simon Rozman <[email protected]...>

Ooops. Misattributed the ACK, so this was really Steffan's ACK.

I am not amending & re-pusing the commit. This will call more confusion.

Sorry.

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...
#14 lore raw

Re: [Openvpn-devel] [PATCH applied] Re: wintun: add --windows-driver config option

Quoted 5 lines · Gert Doering

Hi,

Quoted 2 lines · Gert Doering
     wintun: add --windows-driver config option
Quoted 8 lines
Quoted 2 lines
     Signed-off-by: Lev Stipakov <[email protected]...>
     Acked-by: Simon Rozman <[email protected]...>

Ooops. Misattributed the ACK, so this was really Steffan's ACK.

I am not amending & re-pusing the commit. This will call more confusion.

Sorry.

Wise choice. However you can use another little git feature ... git notes
<https://web.archive.org/web/20180121193320/http://git-scm.com/blog/2010/08/25/notes.html>
<https://git-scm.com/docs/git-notes>

Git notes does not change the commitish, but are more weakly tied to commits and does not have the same "change traceability" as commits does. So I recommend to PGP sign notes (which might need to be handled outside git) if it important to ensure the note is unchanged. But a note can be

AFAIK, it doesn't push or fetch notes by default, so that might need to be handled separately, using:

    $ git push <remote> refs/notes/*

To fetch remote notes:

    $ git fetch <remote> refs/notes/*:refs/notes/*

We already have a few notes in our repo for similar situations or clarifications.

Signature · 4 lines
kind regards,

David Sommerseth
OpenVPN Inc
#15 lore raw

Re: [Openvpn-devel] [PATCH applied] Re: wintun: add --windows-driver config option

Quoted 15 lines · David Sommerseth
Quoted 5 lines · Gert Doering

Hi,

Quoted 2 lines · Gert Doering
     wintun: add --windows-driver config option
Quoted 8 lines
Quoted 2 lines
     Signed-off-by: Lev Stipakov <[email protected]...>
     Acked-by: Simon Rozman <[email protected]...>

Ooops. Misattributed the ACK, so this was really Steffan's ACK.

I am not amending & re-pusing the commit. This will call more confusion.

Sorry.

Quoted 8 lines

Wise choice. However you can use another little git feature ... git notes
<https://web.archive.org/web/20180121193320/http://git-scm.com/blog/2010/08/25/notes.html>
<https://git-scm.com/docs/git-notes>

Git notes does not change the commitish, but are more weakly tied to commits and does not have the same "change traceability" as commits does. So I recommend to PGP sign notes (which might need to be handled outside git) if it important to ensure the note is unchanged. But a note can be

Okay, lets complete that sentence ...

But a note can be removed without making it too obvious, so it's not a totally secure extension to commit messages. But it helps clarify things, especially if the integrity of the note isn't critical.

Signature · 4 lines
kind regards,

David Sommerseth
OpenVPN Inc
#16 lore raw

Re: [Openvpn-devel] [PATCH v3] wintun: add --windows-driver config option

In reply to Lev Stipakov

Hi,

I have been late to this wintun party (no time for anything fun, these days) and this has already been committed, it seems. But some concerns below..

src/openvpn/options.c options_postprocess_mutate_invariant(struct options *options) · line 3003

Quoted 7 lines

+ /* for wintun kernel doesn't send DHCP requests, so use ipapi to set
IP address and netmask */

src/openvpn/options.c +4 -0
+    if (options->wintun)
+    {
+        options->tuntap_options.ip_win32_type = IPW32_SET_IPAPI;
+    }

Looks like this will most likely break any dhcp-related options in the client config.. Say "dhcp-option DNS xxx". We currently require ip_win32_type to be adaptive or dynamic when such options are specified. I think we should set this to adaptive here, and then handle it in tun.c

Although there is no dhcp when wintun is used, we'll still support dhcp-options such as DNS etc using netsh or service, right?

Selva

#17 lore raw

Re: [Openvpn-devel] [PATCH v3] wintun: add --windows-driver config option

Hi,

Quoted 7 lines · Selva Nair

Looks like this will most likely break any dhcp-related options in the client config.. Say "dhcp-option DNS xxx". We currently require ip_win32_type to be adaptive or dynamic when such options are specified. I think we should set this to adaptive here, and then handle it in tun.c

Although there is no dhcp when wintun is used, we'll still support dhcp-options such as DNS etc using netsh or service, right?

I had the same concerns, and if I remember right (I haven't looked just now) one of the later patches adds the missing bits to the iservice.

So we'd accept the DHCP options, pass them on, and iservice would IPAPI them in.

I'll have another look...

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...
#18 lore raw

Re: [Openvpn-devel] [PATCH v3] wintun: add --windows-driver config option

Hello,

ti 19. marrask. 2019 klo 9.37 Gert Doering ([email protected]...) kirjoitti:

Quoted 2 lines

Looks like this will most likely break any dhcp-related options

Quoted 1 line

in the client config.. Say "dhcp-option DNS xxx".

Oops, indeed. When I add those to client config, I got:

Quoted 6 lines

Options error: --dhcp-options requires --ip-win32 dynamic or adaptive

We currently require ip_win32_type to be adaptive or dynamic when such options are specified.

That limitation has been presented since the very first commit in github:

    if (options->tuntap_options.dhcp_options
        && options->tuntap_options.ip_win32_type != IPW32_SET_DHCP_MASQ
        && options->tuntap_options.ip_win32_type != IPW32_SET_ADAPTIVE)
    {
        msg(M_USAGE, "--dhcp-options requires --ip-win32 dynamic or
adaptive");
    }

I commented it out and client-side --dhcp-options worked for me. Any ideas why we have this code?

Quoted 2 lines
Quoted 1 line

I think we should set this to adaptive here, and then handle it in tun.c

Actually, in follow-up commit I have changed IPAPI to NETSH - if I remember correctly
I could not set up routing using former, although it works when done by iserv.

If we don't know why we have that check and we could apply dhcp options from client config
without it, shouldn't we just get rid of it?

Quoted 4 lines
Quoted 3 lines

Although there is no dhcp when wintun is used, we'll still support dhcp-options such as DNS etc using netsh or service, right?

That's correct. At the moment we use:

- netsh when running openvpn without interactive service
- iserv, which itself uses both IPAPI (for setting up IP address and
routing) and netsh (for setting up DNS)

I had the same concerns, and if I remember right (I haven't looked just now)

Quoted 2 lines

one of the later patches adds the missing bits to the iservice.

Yep, patch 6/7 https://patchwork.openvpn.net/patch/883/

Signature · 1 line
-Lev
#19 lore raw

Re: [Openvpn-devel] [PATCH v3] wintun: add --windows-driver config option

Hi,

Quoted 50 lines · Lev Stipakov

Hello,

ti 19. marrask. 2019 klo 9.37 Gert Doering ([email protected]...) kirjoitti:

Quoted 2 lines

Looks like this will most likely break any dhcp-related options

Quoted 1 line

in the client config.. Say "dhcp-option DNS xxx".

Oops, indeed. When I add those to client config, I got:

Quoted 6 lines

Options error: --dhcp-options requires --ip-win32 dynamic or adaptive

We currently require ip_win32_type to be adaptive or dynamic when such options are specified.

That limitation has been presented since the very first commit in github:

    if (options->tuntap_options.dhcp_options
        && options->tuntap_options.ip_win32_type != IPW32_SET_DHCP_MASQ
        && options->tuntap_options.ip_win32_type != IPW32_SET_ADAPTIVE)
    {
        msg(M_USAGE, "--dhcp-options requires --ip-win32 dynamic or
adaptive");
    }

I commented it out and client-side --dhcp-options worked for me. Any ideas why we have this code?

Quoted 2 lines
Quoted 1 line

I think we should set this to adaptive here, and then handle it in tun.c

Actually, in follow-up commit I have changed IPAPI to NETSH - if I remember correctly
I could not set up routing using former, although it works when done by iserv.

If we don't know why we have that check and we could apply dhcp options from client config
without it, shouldn't we just get rid of it?

Quoted 4 lines
Quoted 3 lines

Although there is no dhcp when wintun is used, we'll still support dhcp-options such as DNS etc using netsh or service, right?

Quoted 6 lines

That's correct. At the moment we use:

- netsh when running openvpn without interactive service
- iserv, which itself uses both IPAPI (for setting up IP address and
routing) and netsh (for setting up DNS)

The ip-win32 mess does need clenup but at least it would be nice to use the same API/method in iservice and the core. Can we figure out why IP helper API is not working for setting IP? Unfortunately there is no API for setting DNS (not that I know of) so there the use of netsh is unavoidable.

Anyway, wintun + dhcp-option in client config is broken right now. Looks like it may work when dhcp-option is pushed (not tested) but there are cases where we do use it directly in the config.

Selva

#20 lore raw

PATCH v2 3/7 wintun: implement opening wintun device

In reply to Lev Stipakov

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

To open wintun device, we cannot use "\\.\Global\Wintun<luid>" path as before. To get device path which we supply to CreateFile, we have to use SetupAPI to:

- enumerate network adapters with "wintun" as component id
- for each adapter save its guid
- open device information set
- for each item in set
- open corresponding registry key to get net_cfg_instance_id
- get symbolic link name of device interface by instance id
- path will be symbolic link name of device instance matched with adapter's guid

See https://github.com/OpenVPN/openvpn3/blob/master/openvpn/tun/win/tunutil.hpp and
https://github.com/WireGuard/wireguard-go/blob/master/tun/wintun/wintun_windows.go for implementation examples.

---
 src/openvpn/Makefile.am     |   2 +-
 src/openvpn/openvpn.vcxproj |   6 +-
 src/openvpn/tun.c           | 244 +++++++++++++++++++++++++++++++++++++-------
 src/openvpn/tun.h           |  14 +++
 4 files changed, 223 insertions(+), 43 deletions(-)
src/openvpn/Makefile.am +1 -1
index fbb86ad..a091ffc 100644
@@ -139,5 +139,5 @@ openvpn_LDADD = \
 	$(OPTIONAL_DL_LIBS)
 if WIN32
 openvpn_SOURCES += openvpn_win32_resources.rc block_dns.c block_dns.h
-openvpn_LDADD += -lgdi32 -lws2_32 -lwininet -lcrypt32 -liphlpapi -lwinmm -lfwpuclnt -lrpcrt4 -lncrypt
+openvpn_LDADD += -lgdi32 -lws2_32 -lwininet -lcrypt32 -liphlpapi -lwinmm -lfwpuclnt -lrpcrt4 -lncrypt -lsetupapi
 endif
src/openvpn/openvpn.vcxproj +3 -3
index e77f026..9ffef9f 100644
@@ -91,7 +91,7 @@
     </ClCompile>
     <ResourceCompile />
     <Link>
-      <AdditionalDependencies>legacy_stdio_definitions.lib;Ncrypt.lib;libssl.lib;libcrypto.lib;lzo2.lib;pkcs11-helper.dll.lib;gdi32.lib;ws2_32.lib;wininet.lib;crypt32.lib;iphlpapi.lib;winmm.lib;Fwpuclnt.lib;Rpcrt4.lib;%(AdditionalDependencies)</AdditionalDependencies>
+      <AdditionalDependencies>legacy_stdio_definitions.lib;Ncrypt.lib;libssl.lib;libcrypto.lib;lzo2.lib;pkcs11-helper.dll.lib;gdi32.lib;ws2_32.lib;wininet.lib;crypt32.lib;iphlpapi.lib;winmm.lib;Fwpuclnt.lib;Rpcrt4.lib;setupapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
       <AdditionalLibraryDirectories>$(OPENSSL_HOME)/lib;$(LZO_HOME)/lib;$(PKCS11H_HOME)/lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
       <SubSystem>Console</SubSystem>
     </Link>
@@ -117,7 +117,7 @@
     </ClCompile>
     <ResourceCompile />
     <Link>
-      <AdditionalDependencies>legacy_stdio_definitions.lib;Ncrypt.lib;libssl.lib;libcrypto.lib;lzo2.lib;pkcs11-helper.dll.lib;gdi32.lib;ws2_32.lib;wininet.lib;crypt32.lib;iphlpapi.lib;winmm.lib;Fwpuclnt.lib;Rpcrt4.lib;%(AdditionalDependencies)</AdditionalDependencies>
+      <AdditionalDependencies>legacy_stdio_definitions.lib;Ncrypt.lib;libssl.lib;libcrypto.lib;lzo2.lib;pkcs11-helper.dll.lib;gdi32.lib;ws2_32.lib;wininet.lib;crypt32.lib;iphlpapi.lib;winmm.lib;Fwpuclnt.lib;Rpcrt4.lib;setupapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
       <AdditionalLibraryDirectories>$(OPENSSL_HOME)/lib;$(LZO_HOME)/lib;$(PKCS11H_HOME)/lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
       <SubSystem>Console</SubSystem>
     </Link>
@@ -301,4 +301,4 @@
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <ImportGroup Label="ExtensionTargets">
   </ImportGroup>
-</Project>
\ No newline at end of file
+</Project>
src/openvpn/tun.c +205 -39

393 lines not on this page · raw message

src/openvpn/tun.h +14 -0
index df935f6..19cab7e 100644
@@ -27,6 +27,8 @@
 #ifdef _WIN32
 #include <winioctl.h>
 #include <tap-windows.h>
+#include <setupapi.h>
+#include <cfgmgr32.h>
 #endif
 
 #include "buffer.h"
@@ -38,6 +40,10 @@
 #include "misc.h"
 #include "networking.h"
 
+#ifdef _WIN32
+#define WINTUN_COMPONENT_ID "wintun"
+#endif
+
 #if defined(_WIN32) || defined(TARGET_ANDROID)
 
 #define TUN_ADAPTER_INDEX_INVALID ((DWORD)-1)
@@ -340,6 +346,7 @@ route_order(void)
 struct tap_reg
 {
     const char *guid;
+    bool wintun;
     struct tap_reg *next;
 };
 
@@ -350,6 +357,13 @@ struct panel_reg
     struct panel_reg *next;
 };
 
+struct device_instance_id_interface
+{
+    const char *net_cfg_instance_id;
+    const char *device_interface_list;
+    struct device_instance_id_interface *next;
+};
+
 int ascii2ipset(const char *name);
 
 const char *ipset2ascii(int index);
Signature · 1 line
2.7.4
Acked-by on v2 3/7
#21 lore raw

Re: [Openvpn-devel] [PATCH v2 3/7] wintun: implement opening wintun device

Hi,

Review

Quoted 11 lines

-----Original Message-----
From: Lev Stipakov [mailto:[email protected]...]
Sent: Thursday, November 7, 2019 6:45 PM
To: [email protected]
Cc: Lev Stipakov <[email protected]...>
Subject: [Openvpn-devel] [PATCH v2 3/7] wintun: implement opening wintun device

(quoted diff) +3 -0
+const static GUID GUID_DEVCLASS_NET = { 0x4d36e972L, 0xe325, 0x11ce, {
+0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 } }; const static GUID
+GUID_DEVINTERFACE_NET = { 0xcac88484, 0x7

515, 0x4c03, { 0x82, 0xe6,

Quoted 2 lines

+0x71, 0xa8, 0x7a, 0xba, 0xc3, 0x61 } };
+

GUID_DEVCLASS_NET is declared in devguid.h, GUID_DEVINTERFACE_NET in ndisguid.h... No need to redefine them. However, while one could include those SDK files, one needs to add the appropriate .lib files too. It's not worth complicating for just a couple of GUIDs that will never ever change. So, ACK.

The rest LGTM.

Best regards,
Simon

#22 lore raw

[Openvpn-devel] [PATCH applied] Re: wintun: implement opening wintun device

In reply to Lev Stipakov

Your patch has been applied to the master branch.

Since this introduces a new library requirement, I gave it a test run on my Ubuntu 16 / MinGW linux build system. "Builds fine". Have not run the result.

I have not done anything resembling proper code review. Just basic "this is only changing _WIN32 code, and doesn't seem to do bad stuff with pointers".

What I do not really like is the inflation of the code with

	if (!tt->wintun)

statements now. I think this should be refactored out into an "open_tun_wintun()" and an "open_tun_tap_windows()" function, where all the bits that are now inside an "if (!tt->wintun)" get their own function with less if() and less nesting.

commit e64b4a9e68bde3cb7d878d277878fb2805040e3e

Author: Lev Stipakov
Date:   Thu Nov 7 19:45:25 2019 +0200
     wintun: implement opening wintun device

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

--
kind regards,

Gert Doering

#23 lore raw

Re: [Openvpn-devel] [PATCH applied] Re: wintun: implement opening wintun device

Hi,

Quoted 9 lines

What I do not really like is the inflation of the code with

        if (!tt->wintun)

statements now. I think this should be refactored out into an "open_tun_wintun()" and an "open_tun_tap_windows()" function, where all the bits that are now inside an "if (!tt->wintun)" get their own function with less if() and less nesting.

I agree.

I have sent a (yet another) patch (https://patchwork.openvpn.net/patch/918/
)
which refactors open_tun() method, which now looks like this:

    tun_open_device(tt, dev_node, &device_guid);

    if (tt->wintun)
    {
        wintun_register_ring_buffer(tt);
    }
    else
    {
        tuntap_post_open(tt, device_guid);
    }

Note that it requires 6 of 7 wintun patches to be merged.

Alternatively I can rebase wintun 4/5/6 patches on top of this one.

Signature · 1 line
-Lev
#24 lore raw

Re: [Openvpn-devel] [PATCH v2 3/7] wintun: implement opening wintun device

In reply to Lev Stipakov

Hi,

Quoted 19 lines · Lev Stipakov

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

To open wintun device, we cannot use "\\.\Global\Wintun<luid>" path as before. To get device path which we supply to CreateFile, we have to use SetupAPI to:

- enumerate network adapters with "wintun" as component id
- for each adapter save its guid
- open device information set
- for each item in set
- open corresponding registry key to get net_cfg_instance_id
- get symbolic link name of device interface by instance id
- path will be symbolic link name of device instance matched with
adapter's guid

See
https://github.com/OpenVPN/openvpn3/blob/master/openvpn/tun/win/tunutil.hpp
and

Quoted 6 lines

https://github.com/WireGuard/wireguard-go/blob/master/tun/wintun/wintun_windows.go
for
implementation examples.

This also has been ACKed and merged, but two questions that may need some attention:

@@ -5541,7 +5668,8 @@ void

Quoted 55 lines

open_tun(const char *dev, const char *dev_type, const char *dev_node,
struct tuntap *tt)

 {
     struct gc_arena gc = gc_new();
src/openvpn/tun.c +2 -1
-    char device_path[256];
+    char tuntap_device_path[256];
+    char *path = NULL;
     const char *device_guid = NULL;
     DWORD len;
     bool dhcp_masq = false;
@@ -5571,6 +5699,8 @@ open_tun(const char *dev, const char *dev_type,

const char *dev_node, struct tun

     {
         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);

src/openvpn/tun.c +1 -0
+
         char actual_buffer[256];
 
         at_least_one_tap_win(tap_reg);
@@ -5586,24 +5716,22 @@ open_tun(const char *dev, const char *dev_type,

const char *dev_node, struct tun

             }

             /* Open Windows TAP-Windows adapter */
(quoted diff) +1 -1
-            openvpn_snprintf(device_path, sizeof(device_path), "%s%s%s",
+            openvpn_snprintf(tuntap_device_path,

sizeof(tuntap_device_path), "%s%s%s",

                              USERMODEDEVICEDIR,
                              device_guid,
                              TAP_WIN_SUFFIX);
src/openvpn/tun.c +3 -9
-            tt->hand = CreateFile(
-                device_path,
-                GENERIC_READ | GENERIC_WRITE,
-                0,                /* was: FILE_SHARE_READ */
-                0,
-                OPEN_EXISTING,
-                FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED,
-                0
-                );
+            tt->hand = CreateFile(tuntap_device_path,
+                                  GENERIC_READ | GENERIC_WRITE,
+                                  0,                /* was:

FILE_SHARE_READ */

src/openvpn/tun.c +3 -0
+                                  0,
+                                  OPEN_EXISTING,
+                                  FILE_ATTRIBUTE_SYSTEM |

FILE_FLAG_OVERLAPPED,

src/openvpn/tun.c +1 -1
+                                  0);
 
             if (tt->hand == INVALID_HANDLE_VALUE)
             {
-                msg(M_ERR, "CreateFile failed on TAP device: %s",
Quoted 4 lines

device_path);
+ msg(M_ERR, "CreateFile failed on TAP device: %s",
tuntap_device_path);

Doesn't this mean that if --dev-node is specified, we'll open tapwindows adapter
with that name even if "--window-driver wintun" is specified? The open may succeed
but subsequent wintun-specific processing will fail.

             }
Quoted 16 lines
         }
         else
(quoted diff) +0 -0
@@ -5613,43 +5741,78 @@ open_tun(const char *dev, const char *dev_type,

const char *dev_node, struct tun

             /* Try opening all TAP devices until we find one available */
             while (true)
             {
src/openvpn/tun.c +2 -0
+                bool is_picked_device_wintun = false;
                 device_guid = get_unspecified_device_guid(device_number,
                                                           actual_buffer,
 
 sizeof(actual_buffer),
                                                           tap_reg,
                                                           panel_reg,
+

&is_picked_device_wintun,

Quoted 9 lines
                                                           &gc);

                 if (!device_guid)
                 {

- msg(M_FATAL, "All TAP-Windows adapters on this system
are currently in use.");
+ msg(M_FATAL, "All %s adapters on this system are
currently in use.", tt->wintun ? "wintun" : "TAP - Windows");

If I'm not mistaken wintun device can be opened multiple times, so we'll never get the
"All wintun adapters on this system...." error. Instead, open will succeed here and
something else may fail later. FILE_SHARE_READ = 0 will not save us when the driver
does not enforce it.

Selva

#25 lore raw

Re: [Openvpn-devel] [PATCH v2 3/7] wintun: implement opening wintun device

Hi,

Doesn't this mean that if --dev-node is specified, we'll open tapwindows

Quoted 5 lines

adapter
with that name even if "--window-driver wintun" is specified? The open may succeed
but subsequent wintun-specific processing will fail.

--dev-node is not (yet) supported and open will fail (just re-tested).

Quoted 4 lines

If I'm not mistaken wintun device can be opened multiple times, so we'll never get the
"All wintun adapters on this system...." error.

Well, we get it if wintun driver is not installed :) but yep, open will succeed.

Instead, open will succeed here and something else may fail later.

Quoted 1 line

Right after opening we register ring buffers, and that will fail (with a friendly message):

ERROR: Failed to register ring buffers: 1247

We should probably make it even more user friendly and write something like "make sure wintun adapter is not in use".

Wireguard creates wintun adapter on demand (on connect) and openvpn during installation and then
picks the first one found on connect. Maybe that logic could be more clever to not to pick adapter which
is currently used by wireguard.

I haven't looked yet into running wireguard and openvpn side by side (or multiple openvpn instances
with wintun), just tested that wg and openvpn could co-exist without problems on the same machine.

Signature · 1 line
-Lev
#26 lore raw

Re: [Openvpn-devel] [PATCH v2 3/7] wintun: implement opening wintun device

Hi,

Quoted 11 lines · Lev Stipakov

Hi,

Doesn't this mean that if --dev-node is specified, we'll open tapwindows

Quoted 5 lines

adapter
with that name even if "--window-driver wintun" is specified? The open may succeed
but subsequent wintun-specific processing will fail.

--dev-node is not (yet) supported and open will fail (just re-tested).

I did a quick test and found the open to succeed with a misleading message that Wintun device opened (though it opened the tap device) and then fails on register ring buffers. I haven't applied all patches as yet, so not sure whether this is changed in later patches.

If I'm not mistaken wintun device can be opened multiple times, so we'll

Quoted 4 lines
Quoted 3 lines

never get the
"All wintun adapters on this system...." error.

Well, we get it if wintun driver is not installed :) but yep, open will

Quoted 2 lines

succeed.

Instead, open will succeed here and something else may fail later.

Quoted 10 lines
Quoted 1 line

Right after opening we register ring buffers, and that will fail (with a friendly message):

ERROR: Failed to register ring buffers: 1247

We should probably make it even more user friendly and write something like "make sure wintun adapter is not in use".

Apart from the error message, there is a larger issue especially when we use iservice. In that case, we have to preserve privilege separation and allowing a user to open a device handle in use by another has to be avoided. The service could keep a lock on devices it manages, but I have no idea how we are going to lock out devices opened by other means (say instances started by automatic service, or wireguard or some other third party product).

Can we get the wintun folks to support exclusive open (say FILE_SHARE_READ = 0) or expose the internal count of number of open handles? That would also make it easier to fix this and provide better error messages.

Quoted 11 lines

Wireguard creates wintun adapter on demand (on connect) and openvpn during installation and then
picks the first one found on connect. Maybe that logic could be more clever to not to pick adapter which
is currently used by wireguard.

I haven't looked yet into running wireguard and openvpn side by side (or multiple openvpn instances
with wintun), just tested that wg and openvpn could co-exist without problems on the same machine.

Hmm.. if multiple openvpn instances are not tested this is not ready for review yet, is it?

Again, a quick test shows that, with multiple openvpn instances, it does open an already opened device and then fails while registering ring buffers.

Selva

#27 lore raw

Re: [Openvpn-devel] [PATCH v2 3/7] wintun: implement opening wintun device

Hi,

Apart from the error message, there is a larger issue especially when we

Quoted 3 lines

use iservice. In that case, we have to preserve privilege separation and allowing a user to open a device handle in use by another has to be avoided.

Do you see it as a security issue when handle can be opened by another process?

To read / write to tunnel one needs to register ring buffers, and this call will fail for any other process. Am I missing something here?

Quoted 4 lines

Can we get the wintun folks to support exclusive open (say FILE_SHARE_READ = 0) or expose the internal count of number of open handles? That would also make it easier to fix this and provide better error messages.

We could maybe submit a patch for that.

I agree it would be nice to have.

Quoted 3 lines

Hmm.. if multiple openvpn instances are not tested this is not ready for review yet, is it?

I don't think so. I would not expect new tun driver (or code around it) to support all features of old driver right from the beginning.

Wintun doesn't replace tap-windows6, so if someone requires functionality which wintun doesn't yet support, one could always switch to previous driver.

Again, a quick test shows that, with multiple openvpn instances, it does

Quoted 2 lines

open an already opened device and then fails while registering ring buffers.

Yeah, it would be nice to eventual support multiple tunnels, but I don't think that this is "must have" in the first version.

#28 lore raw

Re: [Openvpn-devel] [PATCH v2 3/7] wintun: implement opening wintun device

Hi Lev,

Quoted 10 lines · Lev Stipakov

Hi,

Apart from the error message, there is a larger issue especially when we

Quoted 3 lines

use iservice. In that case, we have to preserve privilege separation and allowing a user to open a device handle in use by another has to be avoided.

Do you see it as a security issue when handle can be opened by another process?

I don't know the internals of wintun to know that for sure.

Quoted 4 lines

To read / write to tunnel one needs to register ring buffers, and this call will fail for any other process. Am I missing something here?

Hopefully, Simon can confirm whether that provides a sufficient safety net.

Selva

#29 lore raw

Re: [Openvpn-devel] [PATCH v2 3/7] wintun: implement opening wintun device

Hi,

The Wintun doesn't create its own communication I/O device. Running a separate NdisRegisterDeviceEx() device came with a big can of worms, so we decided not to run our own, but rather piggy-back on the existing NDIS one from the NdisMRegisterMiniportDriver() that we tap on. Technically, we could limit it to a single client handle, but this device is primarily used by Windows to manage the adapter, therefore we must not tweak it in this direction.

As Lev said: Wintun adds an IOCTL call to register ring buffers and it is this IOCTL that has a refcounting implemented to deny secondary connection attempts. Unfortunately for OpenVPN, this IOCTL is restricted to privileged processes only. You cannot use it to detect which Wintun adapters are in use by an unprivileged openvpn.exe process. WireGuard is very strict about which TUN adapter it will use – it will never ever use a lottery to pick one.

We have more options here:

A. Wintun adapter can be explicitly chosen by configuration only (by name or GUID). With tapctl.exe utility users can make an adapter with predefined name and put this name into .ovpn. If the Wintun adapter is not specified explicitly => configuration error. Less lottery; predictable tunnel-adapter mapping when running multiple tunnels; disturbed users because there is an inconsistency between TAP-Windows6 and Wintun adapter selection logic.
B. The tapctl.exe code is in OpenVPN repo already and could be integrated in openvpn.exe and interactive service to create Wintun adapter on demand. But, you have to take responsibility to clean what you have created. Pretty much the same as A), but nicer for user. In the long term, I'd suggest this "create adapter if doesn't exist" approach even for TAP-Windows6 adapters.
C. Use adapter media state to see if particular Wintun adapter is already in use or not. Mind that registering buffer also sets MediaConnectStateConnected, and stays connected until its client closes handle or dies. This allows OpenVPN to extend its "use first available adapter" approach to Wintun adapters.

Best regards,

Simon

From: Selva Nair <[email protected]...>
Sent: Tuesday, November 19, 2019 7:03 PM
To: Lev Stipakov <[email protected]...>
Cc: Lev Stipakov <[email protected]...>; openvpn-devel <[email protected]>
Subject: Re: [Openvpn-devel] [PATCH v2 3/7] wintun: implement opening wintun device

Hi Lev,

On Tue, Nov 19, 2019 at 12:23 PM Lev Stipakov <[email protected]... <mailto:[email protected]...> > wrote:

Hi,

Apart from the error message, there is a larger issue especially when we use iservice. In that case, we have to preserve privilege separation and allowing a user to open a device handle in use by another has to be avoided.

Do you see it as a security issue when handle can be opened by another process?

I don't know the internals of wintun to know that for sure.

To read / write to tunnel one needs to register ring buffers, and this call will fail for any other process. Am I missing something here?

Hopefully, Simon can confirm whether that provides a sufficient safety net.

Selva

#30 lore raw

Re: [Openvpn-devel] [PATCH v2 3/7] wintun: implement opening wintun device

Hi,

Quoted 2 lines

We have more options here:

Can we combine B and C?

1) During installation, we create one adapter, as we do now.

2) On connection we enumerate adapters and pick the first one which is not connected.

3) If we could not find free adapter (another VPN connection is already running), we create one on demand.

Signature · 1 line
-Lev

A message that is not in the archive (sent off-list or not yet received): [email protected]...

#32 lore raw

Re: [Openvpn-devel] [PATCH v2 3/7] wintun: implement opening wintun device

Hi,

(cc:ed to -devel)

Quoted 7 lines

I would vote for B and not the combination.

With wintun there is no backwards compatibility requirements, so we could use a cleaner, consistent and simpler approach (i.e B). Do not create any adapter during installation and dynamically create a temporary adapter at connection time.

My main concern with creating tun adapter on demand is that it is far from instant:

$ time ./tapctl.exe create --hwid wintun
{D9F56B7A-3054-4ADC-9457-61030F0B469D}

real 0m2,090s

I don't think we want to add it to connection time.

Creating one persistent adapter per profile (as viscosity does for

Quoted 2 lines

tapwindows and wireguard seems to do for wintun)

If I remember right, wireguard doesn't create persistent adapter, instead it adds/removes it on demand.

If --dev-node is specified, we open the named adapter which the user is

Quoted 2 lines

supposed to have created as we do for tapwindows.

Yes, I plan to add support for --dev-node for wintun.

Signature · 1 line
-Lev
#33 lore raw

Re: [Openvpn-devel] [PATCH v2 3/7] wintun: implement opening wintun device

I know. The tap.c code needs an upgrade, not to evaluate all drivers, but just compatible drivers when creating a new adapter. This speeds things a lot. There's a flag that needs to be changed. Somewhere deep on my TODO lists.

I would suggest against temporary adapters on Windows. This is OK on Linux, but Windows is not Linux. Apart from the time penalty you already discovered, there are more annoying issues with temporary adapters: adapters always get a new GUID on creation, making NLA think it's a new network each time. This will keep resetting the firewall profile assigned to the adapter and piling up network IDs in user registry at each connection attempt. Also, changing bindings on adapters don't persist (vmWare, VirtualBox, Hyper-V bindings comes to my mind)…

I am testing an intermediate approach to adapter management on Windows, inspired by Microsoft's own way how Windows handle dial-up adapters:

1. if adapter "My VPN Connection" doesn't exist, create it.
2. else enable it
3. use it
4. disable it

In my observation, this is the most streamlined approach on Windows. It avoids adapter creation burden, while still (re)creates it on demand. Disabling adapter when it is not in use releases the resources, removes the adapter from the network stack, halts the adapter, unloads the driver when the last of the adapters (of the same driver) is disabled… Like adapter was deleted, only its settings persist in registry.

An annoyance here is, the adapters pile up over time. On multi-user computers, OpenVPN GUI don't have a complete overview which one are stale (no corresponding .ovpn file exist anymore) to clean them. Cleaning requires elevation… An admin user may use Device manager to clean them, uninstaller should clean them. Both are not ideal. On test computers with lots of configuration fuss that might be a problem. On production computers this shouldn't be a big issue. Mind that even Hyper-V doesn't clean up adapters if you install and uninstall it on Windows.

Maybe we could save the absolute path to .ovpn file in the adapter registry to assign it to a particular profile. Interactive service could periodically delete orphaned adapters. I'm not worried about .ovpn-less tunnels: users running openvpn.exe specifying all settings in the command line know what they're doing.

Best regards,

Simon

From: Lev Stipakov <[email protected]...>
Sent: Monday, November 25, 2019 10:04 AM
To: Selva Nair <[email protected]...>
Cc: openvpn-devel <[email protected]>
Subject: Re: [Openvpn-devel] [PATCH v2 3/7] wintun: implement opening wintun device

Hi,

(cc:ed to -devel)

I would vote for B and not the combination.

With wintun there is no backwards compatibility requirements, so we could use a cleaner, consistent and simpler approach (i.e B). Do not create any adapter during installation and dynamically create a temporary adapter at connection time.

My main concern with creating tun adapter on demand is that it is far from instant:

$ time ./tapctl.exe create --hwid wintun
{D9F56B7A-3054-4ADC-9457-61030F0B469D}

real 0m2,090s

I don't think we want to add it to connection time.

Creating one persistent adapter per profile (as viscosity does for tapwindows and wireguard seems to do for wintun)

If I remember right, wireguard doesn't create persistent adapter, instead it adds/removes it on demand.

If --dev-node is specified, we open the named adapter which the user is supposed to have created as we do for tapwindows.

Yes, I plan to add support for --dev-node for wintun.

Signature · 1 line
-Lev
#34 lore raw

Re: [Openvpn-devel] [PATCH v2 3/7] wintun: implement opening wintun device

Hi,

Quoted 8 lines · Simon Rozman

1. if adapter "My VPN Connection" doesn't exist, create it.
2. else enable it
3. use it
4. disable it

An annoyance here is, the adapters pile up over time. On multi-user computers, OpenVPN GUI don't have a complete overview which one are stale (no corresponding .ovpn file exist anymore) to clean them.

I like the initial approach, but the "pile-up" is annoying.

What about "do this for .ovpn configs that contain a --dev-node setting, and otherwise just try to enumerate 'OpenVPN Wintun 1', 'OpenVPN Wintun 2', ... and create those as-needed?

This should keep the numbers down...

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...
#35 lore raw

Re: [Openvpn-devel] [PATCH v2 3/7] wintun: implement opening wintun device

In reply to Lev Stipakov

Hi

Quoted 16 lines · Lev Stipakov

Hi,

(cc:ed to -devel)

Quoted 7 lines

I would vote for B and not the combination.

With wintun there is no backwards compatibility requirements, so we could use a cleaner, consistent and simpler approach (i.e B). Do not create any adapter during installation and dynamically create a temporary adapter at connection time.

My main concern with creating tun adapter on demand is that it is far from instant:

Quoted 7 lines
$ time ./tapctl.exe create --hwid wintun
{D9F56B7A-3054-4ADC-9457-61030F0B469D}

real 0m2,090s

I don't think we want to add it to connection time.

No, we don't want that.

I do realize what I wrote earlier was far from ideal. I had forgotten my travails two years back attempting to add dynamic tap adapter creation to OpnVPN....

As an easy way for supporting multiple tunnels, can we treat success of open-device + ring-buffer-registration calls together as a successful open of the adapter, and move on to the next one when that fails? That would be very similar to what we do for tapwindows right now.

To assist users, when we fail with no free adapters, we could add a feature to the GUI to add a new one using the RunAsAdmin shell-execute we already have support for. Will need an improved devcon/tapinstall that does not reset all adapters when a new one is added, except when unavoidable due to driver version change.

Simon's suggestion of persistent adapters as required and kept disabled when not in use sounds interesting. As some providers do supply 100's of configs, I think we should refrain from creating one adapter per ovpn, though. They should be using multiple --remote lines in a single config, but, for that to work well, we need to improve --management-remote option to provide a friendly UI for remote selection.

Selva

#36 lore raw

PATCH v2 4/7 wintun: ring buffers based I/O

In reply to Lev Stipakov

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

Implemented according to Wintun documentation and reference client code.

Wintun uses ring buffers to communicate between kernel driver and user process. Client allocates send and receive ring buffers, creates events and passes it to kernel driver under LocalSystem privileges.

When data is available for read, wintun modifies "tail" pointer of send ring and signals via event. User process reads data from "head" to "tail" and updates "head" pointer.

When user process is ready to write, it writes to receive ring, updates "tail" pointer and signals to kernel via event.

In openvpn code we add send ring's event to event loop. Before performing io wait, we compare "head" and "tail" pointers of send ring and if they're different, we skip io wait and perform read.

This also adds ring buffers support to tcp and udp server code.

---
 src/openvpn/forward.c |  42 +++++++++++++++---
 src/openvpn/forward.h |  47 +++++++++++++++++++-
 src/openvpn/mtcp.c    |  28 +++++++++++-
 src/openvpn/mudp.c    |  14 ++++++
 src/openvpn/options.c |   4 +-
 src/openvpn/syshead.h |   1 +
 src/openvpn/tun.c     |  45 +++++++++++++++++++
 src/openvpn/tun.h     | 121 +++++++++++++++++++++++++++++++++++++++++++++++++-
 src/openvpn/win32.c   | 120 +++++++++++++++++++++++++++++++++++++++++++++++++
 src/openvpn/win32.h   |  47 ++++++++++++++++++++
 10 files changed, 458 insertions(+), 11 deletions(-)
src/openvpn/forward.c +37 -5
index 8451706..0be8b6d 100644
@@ -1256,12 +1256,30 @@ read_incoming_tun(struct context *c)
     perf_push(PERF_READ_IN_TUN);
 
     c->c2.buf = c->c2.buffers->read_tun_buf;
+
 #ifdef _WIN32
-    read_tun_buffered(c->c1.tuntap, &c->c2.buf);
+    if (c->c1.tuntap->wintun)
+    {
+        read_wintun(c->c1.tuntap, &c->c2.buf);
+        if (c->c2.buf.len == -1)
+        {
+            register_signal(c, SIGHUP, "tun-abort");
+            c->persist.restart_sleep_seconds = 1;
+            msg(M_INFO, "Wintun read error, restarting");
+            perf_pop();
+            return;
+        }
+    }
+    else
+    {
+        read_tun_buffered(c->c1.tuntap, &c->c2.buf);
 #else
-    ASSERT(buf_init(&c->c2.buf, FRAME_HEADROOM(&c->c2.frame)));
-    ASSERT(buf_safe(&c->c2.buf, MAX_RW_SIZE_TUN(&c->c2.frame)));
-    c->c2.buf.len = read_tun(c->c1.tuntap, BPTR(&c->c2.buf), MAX_RW_SIZE_TUN(&c->c2.frame));
+        ASSERT(buf_init(&c->c2.buf, FRAME_HEADROOM(&c->c2.frame)));
+        ASSERT(buf_safe(&c->c2.buf, MAX_RW_SIZE_TUN(&c->c2.frame)));
+        c->c2.buf.len = read_tun(c->c1.tuntap, BPTR(&c->c2.buf), MAX_RW_SIZE_TUN(&c->c2.frame));
+#endif
+#ifdef _WIN32
+    }
 #endif
 
 #ifdef PACKET_TRUNCATION_CHECK
@@ -2103,7 +2121,21 @@ io_wait_dowork(struct context *c, const unsigned int flags)
      * Configure event wait based on socket, tuntap flags.
      */
     socket_set(c->c2.link_socket, c->c2.event_set, socket, (void *)&socket_shift, NULL);
-    tun_set(c->c1.tuntap, c->c2.event_set, tuntap, (void *)&tun_shift, NULL);
+
+#ifdef _WIN32
+    if (c->c1.tuntap && c->c1.tuntap->wintun)
+    {
+        /* add ring buffer event */
+        struct rw_handle rw = {.read = c->c1.tuntap->send_tail_moved };
+        event_ctl(c->c2.event_set, &rw, EVENT_READ, (void *)&tun_shift);
+    }
+    else
+    {
+#endif
+        tun_set(c->c1.tuntap, c->c2.event_set, tuntap, (void *)&tun_shift, NULL);
+#ifdef _WIN32
+    }
+#endif
 
 #ifdef ENABLE_MANAGEMENT
     if (management)
src/openvpn/forward.h +45 -2

59 lines not on this page · raw message

src/openvpn/mtcp.c +27 -1
index abe2059..9ac51c3 100644
@@ -270,7 +270,33 @@ multi_tcp_wait(const struct context *c,
 {
     int status;
     socket_set_listen_persistent(c->c2.link_socket, mtcp->es, MTCP_SOCKET);
-    tun_set(c->c1.tuntap, mtcp->es, EVENT_READ, MTCP_TUN, &mtcp->tun_rwflags);
+
+#ifdef _WIN32
+    if (c->c1.tuntap && c->c1.tuntap->wintun)
+    {
+        if (c->c1.tuntap->send_ring->head != c->c1.tuntap->send_ring->tail)
+        {
+            /* there is data in wintun ring buffer, read it immediately */
+            mtcp->esr[0].arg = MTCP_TUN;
+            mtcp->esr[0].rwflags = EVENT_READ;
+            mtcp->n_esr = 1;
+            return 1;
+        }
+        else
+        {
+            /* add ring buffer event */
+            struct rw_handle rw = { .read = c->c1.tuntap->send_tail_moved };
+            event_ctl(mtcp->es, &rw, EVENT_READ, MTCP_TUN);
+        }
+    }
+    else
+    {
+#endif
+        tun_set(c->c1.tuntap, mtcp->es, EVENT_READ, MTCP_TUN, &mtcp->tun_rwflags);
+#ifdef _WIN32
+    }
+#endif
+
 #ifdef ENABLE_MANAGEMENT
     if (management)
     {
src/openvpn/mudp.c +14 -0
index b7f061a..7715063 100644
@@ -279,6 +279,20 @@ p2mp_iow_flags(const struct multi_context *m)
         flags |= IOW_READ;
     }
 
+#ifdef _WIN32
+    {
+        struct tuntap* tt = m->top.c1.tuntap;
+        if (tt && tt->wintun)
+        {
+            if (tt->send_ring->head == tt->send_ring->tail)
+            {
+                /* nothing to read from tun -> remove tun read flag set by IOW_READ */
+                flags &= ~IOW_READ_TUN;
+            }
+        }
+    }
+#endif
+
     return flags;
 }
 
src/openvpn/options.c +2 -2
index 5c5033e..1891981 100644
@@ -2998,10 +2998,10 @@ options_postprocess_mutate_invariant(struct options *options)
         options->ifconfig_noexec = false;
     }
 
-    /* for wintun kernel doesn't send DHCP requests, so use ipapi to set IP address and netmask */
+    /* for wintun kernel doesn't send DHCP requests, so use netsh to set IP address and netmask */
     if (options->wintun)
     {
-        options->tuntap_options.ip_win32_type = IPW32_SET_IPAPI;
+        options->tuntap_options.ip_win32_type = IPW32_SET_NETSH;
     }
 
     remap_redirect_gateway_flags(options);
src/openvpn/syshead.h +1 -0
index 899aa59..e9accb5 100644
@@ -39,6 +39,7 @@
 #ifdef _WIN32
 #include <windows.h>
 #include <winsock2.h>
+#include <tlhelp32.h>
 #define sleep(x) Sleep((x)*1000)
 #define random rand
 #define srandom srand
src/openvpn/tun.c +45 -0

63 lines not on this page · raw message

src/openvpn/tun.h +120 -1

136 lines not on this page · raw message

src/openvpn/win32.c +120 -0

124 lines not on this page · raw message

src/openvpn/win32.h +47 -0

58 lines not on this page · raw message

Signature · 1 line
2.7.4
#37 lore raw

Re: [Openvpn-devel] [PATCH v2 4/7] wintun: ring buffers based I/O

Hi,

Quoted 34 lines

-----Original Message-----
From: Lev Stipakov [mailto:[email protected]...]
Sent: Thursday, November 7, 2019 6:45 PM
To: [email protected]
Cc: Lev Stipakov <[email protected]...>
Subject: [Openvpn-devel] [PATCH v2 4/7] wintun: ring buffers based I/O

src/openvpn/forward.c index renamed from src/openvpn/forward.c +0 -0

8451706..0be8b6d 100644

src/openvpn/forward.c +16 -1
@@ -1256,12 +1256,30 @@ read_incoming_tun(struct context *c)
     perf_push(PERF_READ_IN_TUN);
 
     c->c2.buf = c->c2.buffers->read_tun_buf;
+
 #ifdef _WIN32
-    read_tun_buffered(c->c1.tuntap, &c->c2.buf);
+    if (c->c1.tuntap->wintun)
+    {
+        read_wintun(c->c1.tuntap, &c->c2.buf);
+        if (c->c2.buf.len == -1)
+        {
+            register_signal(c, SIGHUP, "tun-abort");
+            c->persist.restart_sleep_seconds = 1;
+            msg(M_INFO, "Wintun read error, restarting");
+            perf_pop();
+            return;
+        }
+    }
+    else
+    {
+        read_tun_buffered(c->c1.tuntap, &c->c2.buf);
 #else

src/openvpn/forward.c read_incoming_tun(struct context *c) · line 1283

Quoted 10 lines
src/openvpn/forward.c +5 -3
-    ASSERT(buf_init(&c->c2.buf, FRAME_HEADROOM(&c->c2.frame)));
-    ASSERT(buf_safe(&c->c2.buf, MAX_RW_SIZE_TUN(&c->c2.frame)));
-    c->c2.buf.len = read_tun(c->c1.tuntap, BPTR(&c->c2.buf),
 MAX_RW_SIZE_TUN(&c->c2.frame));
+        ASSERT(buf_init(&c->c2.buf, FRAME_HEADROOM(&c->c2.frame)));
+        ASSERT(buf_safe(&c->c2.buf, MAX_RW_SIZE_TUN(&c->c2.frame)));
+        c->c2.buf.len = read_tun(c->c1.tuntap, BPTR(&c->c2.buf),
+MAX_RW_SIZE_TUN(&c->c2.frame)); #endif #ifdef _WIN32
+    }
 #endif

This #ifdef dance is a bit convoluted. I suggest simplification.

src/openvpn/forward.c io_wait_dowork(struct context *c, const unsigned · line 2124

Quoted 6 lines
src/openvpn/forward.c +0 -0
 #ifdef PACKET_TRUNCATION_CHECK
@@ -2103,7 +2121,21 @@ io_wait_dowork(struct context *c, const unsigned
 int flags)
      * Configure event wait based on socket, tuntap flags.
      */
     socket_set(c->c2.link_socket, c->c2.event_set, socket, (void

*)&socket_shift,

src/openvpn/forward.c io_wait_dowork(struct context *c, const unsigned · line 2108

Quoted 2 lines
src/openvpn/forward.c +0 -1
 NULL);
-    tun_set(c->c1.tuntap, c->c2.event_set, tuntap, (void *)&tun_shift,

NULL);

Quoted 5 lines
src/openvpn/forward.c +5 -0
+
+#ifdef _WIN32
+    if (c->c1.tuntap && c->c1.tuntap->wintun)
+    {
+        /* add ring buffer event */

src/openvpn/forward.c io_wait_dowork(struct context *c, const unsigned int flags) · line 2138

Quoted 10 lines
src/openvpn/forward.c +10 -0
+        struct rw_handle rw = {.read = c->c1.tuntap->send_tail_moved };
+        event_ctl(c->c2.event_set, &rw, EVENT_READ, (void *)&tun_shift);
+    }
+    else
+    {
+#endif
+        tun_set(c->c1.tuntap, c->c2.event_set, tuntap, (void
+*)&tun_shift, NULL); #ifdef _WIN32
+    }
+#endif

This #ifdef dance is a bit convoluted. I suggest simplification.

Quoted 11 lines
src/openvpn/forward.c +10 -0
+static inline int
+write_wintun(struct tuntap *tt, struct buffer *buf) {
+    struct tun_ring *ring = tt->receive_ring;
+    ULONG head = ring->head;
+    ULONG tail = ring->tail;
+    ULONG aligned_packet_size;
+    ULONG buf_space;
+    struct TUN_PACKET *packet;
+
+    if ((head > WINTUN_RING_CAPACITY) || (tail >=

WINTUN_RING_CAPACITY))

Should be ...head >= WINTUN_RING_CAPACITY...

Regards,
Simon

#38 lore raw

PATCH v3 4/7 wintun: ring buffers based I/O

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

Implemented according to Wintun documentation and reference client code.

Wintun uses ring buffers to communicate between kernel driver and user process. Client allocates send and receive ring buffers, creates events and passes it to kernel driver under LocalSystem privileges.

When data is available for read, wintun modifies "tail" pointer of send ring and signals via event. User process reads data from "head" to "tail" and updates "head" pointer.

When user process is ready to write, it writes to receive ring, updates "tail" pointer and signals to kernel via event.

In openvpn code we add send ring's event to event loop. Before performing io wait, we compare "head" and "tail" pointers of send ring and if they're different, we skip io wait and perform read.

This also adds ring buffers support to tcp and udp server code.

---

v3:
- simplified convoluted #ifdefs
- replaced "greater than" with "greater or equal than"

v2:
- rebased on top of master

 src/openvpn/forward.c |  32 ++++++++++++-
 src/openvpn/forward.h |  45 ++++++++++++++++++-
 src/openvpn/mtcp.c    |  26 ++++++++++-
 src/openvpn/mudp.c    |  14 ++++++
 src/openvpn/options.c |   4 +-
 src/openvpn/syshead.h |   1 +
 src/openvpn/tun.c     |  45 +++++++++++++++++++
 src/openvpn/tun.h     | 121 +++++++++++++++++++++++++++++++++++++++++++++++++-
 src/openvpn/win32.c   | 120 +++++++++++++++++++++++++++++++++++++++++++++++++
 src/openvpn/win32.h   |  47 ++++++++++++++++++++
 10 files changed, 447 insertions(+), 8 deletions(-)
src/openvpn/forward.c +30 -2
index 8451706..484975b 100644
@@ -1256,8 +1256,24 @@ read_incoming_tun(struct context *c)
     perf_push(PERF_READ_IN_TUN);
 
     c->c2.buf = c->c2.buffers->read_tun_buf;
+
 #ifdef _WIN32
-    read_tun_buffered(c->c1.tuntap, &c->c2.buf);
+    if (c->c1.tuntap->wintun)
+    {
+        read_wintun(c->c1.tuntap, &c->c2.buf);
+        if (c->c2.buf.len == -1)
+        {
+            register_signal(c, SIGHUP, "tun-abort");
+            c->persist.restart_sleep_seconds = 1;
+            msg(M_INFO, "Wintun read error, restarting");
+            perf_pop();
+            return;
+        }
+    }
+    else
+    {
+        read_tun_buffered(c->c1.tuntap, &c->c2.buf);
+    }
 #else
     ASSERT(buf_init(&c->c2.buf, FRAME_HEADROOM(&c->c2.frame)));
     ASSERT(buf_safe(&c->c2.buf, MAX_RW_SIZE_TUN(&c->c2.frame)));
@@ -2103,7 +2119,19 @@ io_wait_dowork(struct context *c, const unsigned int flags)
      * Configure event wait based on socket, tuntap flags.
      */
     socket_set(c->c2.link_socket, c->c2.event_set, socket, (void *)&socket_shift, NULL);
-    tun_set(c->c1.tuntap, c->c2.event_set, tuntap, (void *)&tun_shift, NULL);
+
+#ifdef _WIN32
+    if (c->c1.tuntap && c->c1.tuntap->wintun)
+    {
+        /* add ring buffer event */
+        struct rw_handle rw = {.read = c->c1.tuntap->send_tail_moved };
+        event_ctl(c->c2.event_set, &rw, EVENT_READ, (void *)&tun_shift);
+    }
+    else
+#endif
+    {
+        tun_set(c->c1.tuntap, c->c2.event_set, tuntap, (void *)&tun_shift, NULL);
+    }
 
 #ifdef ENABLE_MANAGEMENT
     if (management)
src/openvpn/forward.h +43 -2

57 lines not on this page · raw message

src/openvpn/mtcp.c +25 -1
index abe2059..02e9016 100644
@@ -270,7 +270,31 @@ multi_tcp_wait(const struct context *c,
 {
     int status;
     socket_set_listen_persistent(c->c2.link_socket, mtcp->es, MTCP_SOCKET);
-    tun_set(c->c1.tuntap, mtcp->es, EVENT_READ, MTCP_TUN, &mtcp->tun_rwflags);
+
+#ifdef _WIN32
+    if (c->c1.tuntap && c->c1.tuntap->wintun)
+    {
+        if (c->c1.tuntap->send_ring->head != c->c1.tuntap->send_ring->tail)
+        {
+            /* there is a data in wintun ring buffer, read it immediately */
+            mtcp->esr[0].arg = MTCP_TUN;
+            mtcp->esr[0].rwflags = EVENT_READ;
+            mtcp->n_esr = 1;
+            return 1;
+        }
+        else
+        {
+            /* add ring buffer event */
+            struct rw_handle rw = { .read = c->c1.tuntap->send_tail_moved };
+            event_ctl(mtcp->es, &rw, EVENT_READ, MTCP_TUN);
+        }
+    }
+    else
+#endif
+    {
+        tun_set(c->c1.tuntap, mtcp->es, EVENT_READ, MTCP_TUN, &mtcp->tun_rwflags);
+    }
+
 #ifdef ENABLE_MANAGEMENT
     if (management)
     {
src/openvpn/mudp.c +14 -0
index b7f061a..7715063 100644
@@ -279,6 +279,20 @@ p2mp_iow_flags(const struct multi_context *m)
         flags |= IOW_READ;
     }
 
+#ifdef _WIN32
+    {
+        struct tuntap* tt = m->top.c1.tuntap;
+        if (tt && tt->wintun)
+        {
+            if (tt->send_ring->head == tt->send_ring->tail)
+            {
+                /* nothing to read from tun -> remove tun read flag set by IOW_READ */
+                flags &= ~IOW_READ_TUN;
+            }
+        }
+    }
+#endif
+
     return flags;
 }
 
src/openvpn/options.c +2 -2
index a70a822..0a40d2d 100644
@@ -2996,10 +2996,10 @@ options_postprocess_mutate_invariant(struct options *options)
         options->ifconfig_noexec = false;
     }
 
-    /* for wintun kernel doesn't send DHCP requests, so use ipapi to set IP address and netmask */
+    /* for wintun kernel doesn't send DHCP requests, so use netsh to set IP address and netmask */
     if (options->wintun)
     {
-        options->tuntap_options.ip_win32_type = IPW32_SET_IPAPI;
+        options->tuntap_options.ip_win32_type = IPW32_SET_NETSH;
     }
 
     remap_redirect_gateway_flags(options);
src/openvpn/syshead.h +1 -0
index 899aa59..e9accb5 100644
@@ -39,6 +39,7 @@
 #ifdef _WIN32
 #include <windows.h>
 #include <winsock2.h>
+#include <tlhelp32.h>
 #define sleep(x) Sleep((x)*1000)
 #define random rand
 #define srandom srand
src/openvpn/tun.c +45 -0

63 lines not on this page · raw message

src/openvpn/tun.h +120 -1

136 lines not on this page · raw message

src/openvpn/win32.c +120 -0

124 lines not on this page · raw message

src/openvpn/win32.h +47 -0

58 lines not on this page · raw message

Signature · 1 line
2.7.4
#39 lore raw

Re: [Openvpn-devel] [PATCH v2 4/7] wintun: ring buffers based I/O

In reply to Lev Stipakov

Hi,

Some first-round review comments. I still need to fully grasp the event mechanism intricacies for a real in-depth review.

As a general remark: could you try to stick to the 80 char line length limit?

Quoted 69 lines · Lev Stipakov

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

Implemented according to Wintun documentation and reference client code.

Wintun uses ring buffers to communicate between kernel driver and user process. Client allocates send and receive ring buffers, creates events and passes it to kernel driver under LocalSystem privileges.

When data is available for read, wintun modifies "tail" pointer of send ring and signals via event. User process reads data from "head" to "tail" and updates "head" pointer.

When user process is ready to write, it writes to receive ring, updates "tail" pointer and signals to kernel via event.

In openvpn code we add send ring's event to event loop. Before performing io wait, we compare "head" and "tail" pointers of send ring and if they're different, we skip io wait and perform read.

This also adds ring buffers support to tcp and udp server code.

---
 src/openvpn/forward.c |  42 +++++++++++++++---
 src/openvpn/forward.h |  47 +++++++++++++++++++-
 src/openvpn/mtcp.c    |  28 +++++++++++-
 src/openvpn/mudp.c    |  14 ++++++
 src/openvpn/options.c |   4 +-
 src/openvpn/syshead.h |   1 +
 src/openvpn/tun.c     |  45 +++++++++++++++++++
 src/openvpn/tun.h     | 121 +++++++++++++++++++++++++++++++++++++++++++++++++-
 src/openvpn/win32.c   | 120 +++++++++++++++++++++++++++++++++++++++++++++++++
 src/openvpn/win32.h   |  47 ++++++++++++++++++++
 10 files changed, 458 insertions(+), 11 deletions(-)
src/openvpn/forward.c +16 -1
index 8451706..0be8b6d 100644
@@ -1256,12 +1256,30 @@ read_incoming_tun(struct context *c)
     perf_push(PERF_READ_IN_TUN);
 
     c->c2.buf = c->c2.buffers->read_tun_buf;
+
 #ifdef _WIN32
-    read_tun_buffered(c->c1.tuntap, &c->c2.buf);
+    if (c->c1.tuntap->wintun)
+    {
+        read_wintun(c->c1.tuntap, &c->c2.buf);
+        if (c->c2.buf.len == -1)
+        {
+            register_signal(c, SIGHUP, "tun-abort");
+            c->persist.restart_sleep_seconds = 1;
+            msg(M_INFO, "Wintun read error, restarting");
+            perf_pop();
+            return;
+        }
+    }
+    else
+    {
+        read_tun_buffered(c->c1.tuntap, &c->c2.buf);
 #else

src/openvpn/forward.c read_incoming_tun(struct context *c) · line 1283

Quoted 10 lines
src/openvpn/forward.c +6 -3
-    ASSERT(buf_init(&c->c2.buf, FRAME_HEADROOM(&c->c2.frame)));
-    ASSERT(buf_safe(&c->c2.buf, MAX_RW_SIZE_TUN(&c->c2.frame)));
-    c->c2.buf.len = read_tun(c->c1.tuntap, BPTR(&c->c2.buf), MAX_RW_SIZE_TUN(&c->c2.frame));
+        ASSERT(buf_init(&c->c2.buf, FRAME_HEADROOM(&c->c2.frame)));
+        ASSERT(buf_safe(&c->c2.buf, MAX_RW_SIZE_TUN(&c->c2.frame)));
+        c->c2.buf.len = read_tun(c->c1.tuntap, BPTR(&c->c2.buf), MAX_RW_SIZE_TUN(&c->c2.frame));
+#endif
+#ifdef _WIN32
+    }
 #endif

As Simon mentioned too, this code is getting more and more hard to read. Can we maybe do this in some cleaner way? Maybe add some helper function(s)?

Quoted 12 lines
src/openvpn/forward.c +5 -1
 #ifdef PACKET_TRUNCATION_CHECK
@@ -2103,7 +2121,21 @@ io_wait_dowork(struct context *c, const unsigned int flags)
      * Configure event wait based on socket, tuntap flags.
      */
     socket_set(c->c2.link_socket, c->c2.event_set, socket, (void *)&socket_shift, NULL);
-    tun_set(c->c1.tuntap, c->c2.event_set, tuntap, (void *)&tun_shift, NULL);
+
+#ifdef _WIN32
+    if (c->c1.tuntap && c->c1.tuntap->wintun)
+    {
+        /* add ring buffer event */

src/openvpn/forward.c io_wait_dowork(struct context *c, const unsigned int flags) · line 2138

Quoted 10 lines
src/openvpn/forward.c +10 -0
+        struct rw_handle rw = {.read = c->c1.tuntap->send_tail_moved };
+        event_ctl(c->c2.event_set, &rw, EVENT_READ, (void *)&tun_shift);
+    }
+    else
+    {
+#endif
+        tun_set(c->c1.tuntap, c->c2.event_set, tuntap, (void *)&tun_shift, NULL);
+#ifdef _WIN32
+    }
+#endif

As above :)

Quoted 14 lines
src/openvpn/forward.c +0 -0
 #ifdef ENABLE_MANAGEMENT
     if (management)
src/openvpn/forward.h +3 -0
index 48202c0..6096fa8 100644
@@ -375,6 +375,19 @@ p2p_iow_flags(const struct context *c)
     {
         flags |= IOW_TO_TUN;
     }
+#ifdef _WIN32
+    {
+        struct tuntap *tt = c->c1.tuntap;

src/openvpn/forward.h p2p_iow_flags(const struct context *c) · line 390

Quoted 10 lines
src/openvpn/forward.h +10 -0
+        if (tt && tt->wintun)
+        {
+            if (tt->send_ring->head == tt->send_ring->tail)
+            {
+                /* nothing to read from tun -> remove tun read flag set by IOW_READ */
+                flags &= ~IOW_READ_TUN;
+            }
+        }
+    }
+#endif

Looks like this should be a helper function (with a good name that helps understand what this does).

Quoted 22 lines
src/openvpn/forward.h +13 -2
     return flags;
 }
 
@@ -403,8 +416,38 @@ io_wait(struct context *c, const unsigned int flags)
     }
     else
     {
-        /* slow path */
-        io_wait_dowork(c, flags);
+#ifdef _WIN32
+        bool skip_iowait = flags & IOW_TO_TUN;
+        if (flags & IOW_READ_TUN)
+        {
+            /*
+             * don't read from tun if we have pending write to link,
+             * since every tun read overwrites to_link buffer filled
+             * by previous tun read
+             */
+            skip_iowait = !(flags & IOW_TO_LINK);
+        }
+        if (c->c1.tuntap && c->c1.tuntap->wintun && skip_iowait)
+        {

src/openvpn/forward.h io_wait(struct context *c, const unsigned int flags) · line 441

Quoted 10 lines
src/openvpn/forward.h +10 -0
+            unsigned int ret = 0;
+            if (flags & IOW_TO_TUN)
+            {
+                ret |= TUN_WRITE;
+            }
+            if (flags & IOW_READ_TUN)
+            {
+                ret |= TUN_READ;
+            }
+            c->c2.event_set_status = ret;

This chunk is the same as the "fast path" check above. Looks like you should re-arrange this code so you don't have to duplicate it.

Quoted 17 lines
src/openvpn/forward.h +9 -0
+        }
+        else
+        {
+#endif
+            /* slow path */
+            io_wait_dowork(c, flags);
+#ifdef _WIN32
+        }
+#endif
     }
 }
 
src/openvpn/mtcp.c +0 -0
index abe2059..9ac51c3 100644

src/openvpn/mtcp.c multi_tcp_wait(const struct context *c, · line 277

Quoted 9 lines
src/openvpn/mtcp.c +5 -1
@@ -270,7 +270,33 @@ multi_tcp_wait(const struct context *c,
 {
     int status;
     socket_set_listen_persistent(c->c2.link_socket, mtcp->es, MTCP_SOCKET);
-    tun_set(c->c1.tuntap, mtcp->es, EVENT_READ, MTCP_TUN, &mtcp->tun_rwflags);
+
+#ifdef _WIN32
+    if (c->c1.tuntap && c->c1.tuntap->wintun)
+    {
+        if (c->c1.tuntap->send_ring->head != c->c1.tuntap->send_ring->tail)

Looking this deep into the tuntap struct from another module looks like a layering violation. Consider adding a helper function like

bool tuntap_wintun_data_available(const struct tuntap *tt)

or so.

src/openvpn/mtcp.c multi_tcp_wait(const struct context *c, · line 283

Quoted 6 lines
src/openvpn/mtcp.c +6 -0
+        {
+            /* there is data in wintun ring buffer, read it immediately */
+            mtcp->esr[0].arg = MTCP_TUN;
+            mtcp->esr[0].rwflags = EVENT_READ;
+            mtcp->n_esr = 1;
+            return 1;

This feels ugly, but I can't find a better approach either. Meh.

src/openvpn/mtcp.c multi_tcp_wait(const struct context *c, · line 290

Quoted 7 lines
src/openvpn/mtcp.c +7 -0
+        }
+        else
+        {
+            /* add ring buffer event */
+            struct rw_handle rw = { .read = c->c1.tuntap->send_tail_moved };
+            event_ctl(mtcp->es, &rw, EVENT_READ, MTCP_TUN);
+        }

Can this not be done by tun_set() below? At first glance, if either tun_event_handle() returns tt->send_tail_moved for wintun (instaed of tt->rw_handle), of if tt->rw_handle would be set to send_tail_moved for wintun. The latter might even allow you to get rid of tt->send_tail_removed completely.

Quoted 17 lines
src/openvpn/mtcp.c +9 -0
+    }
+    else
+    {
+#endif
+        tun_set(c->c1.tuntap, mtcp->es, EVENT_READ, MTCP_TUN, &mtcp->tun_rwflags);
+#ifdef _WIN32
+    }
+#endif
+
 #ifdef ENABLE_MANAGEMENT
     if (management)
     {
src/openvpn/mudp.c +0 -0
index b7f061a..7715063 100644

src/openvpn/mudp.c p2mp_iow_flags(const struct multi_context *m) · line 287

Quoted 9 lines
src/openvpn/mudp.c +6 -0
@@ -279,6 +279,20 @@ p2mp_iow_flags(const struct multi_context *m)
         flags |= IOW_READ;
     }
 
+#ifdef _WIN32
+    {
+        struct tuntap* tt = m->top.c1.tuntap;
+        if (tt && tt->wintun)
+        {
+            if (tt->send_ring->head == tt->send_ring->tail)

This should use the same helper function as suggested above.

Quoted 115 lines
src/openvpn/mudp.c +8 -0
+            {
+                /* nothing to read from tun -> remove tun read flag set by IOW_READ */
+                flags &= ~IOW_READ_TUN;
+            }
+        }
+    }
+#endif
+
     return flags;
 }
 
src/openvpn/options.c +2 -2
index 5c5033e..1891981 100644
@@ -2998,10 +2998,10 @@ options_postprocess_mutate_invariant(struct options *options)
         options->ifconfig_noexec = false;
     }
 
-    /* for wintun kernel doesn't send DHCP requests, so use ipapi to set IP address and netmask */
+    /* for wintun kernel doesn't send DHCP requests, so use netsh to set IP address and netmask */
     if (options->wintun)
     {
-        options->tuntap_options.ip_win32_type = IPW32_SET_IPAPI;
+        options->tuntap_options.ip_win32_type = IPW32_SET_NETSH;
     }
 
     remap_redirect_gateway_flags(options);
src/openvpn/syshead.h +1 -0
index 899aa59..e9accb5 100644
@@ -39,6 +39,7 @@
 #ifdef _WIN32
 #include <windows.h>
 #include <winsock2.h>
+#include <tlhelp32.h>
 #define sleep(x) Sleep((x)*1000)
 #define random rand
 #define srandom srand
src/openvpn/tun.c +45 -0
index 37bf065..b909b89 100644
@@ -798,6 +798,18 @@ init_tun_post(struct tuntap *tt,
     tt->rw_handle.read = tt->reads.overlapped.hEvent;
     tt->rw_handle.write = tt->writes.overlapped.hEvent;
     tt->adapter_index = TUN_ADAPTER_INDEX_INVALID;
+
+    tt->send_ring = malloc(sizeof(struct tun_ring));
+    tt->receive_ring = malloc(sizeof(struct tun_ring));
+    if ((tt->send_ring == NULL) || (tt->receive_ring == NULL))
+    {
+        msg(M_FATAL, "Cannot allocate memory for receive ring");
+    }
+    ZeroMemory(tt->send_ring, sizeof(struct tun_ring));
+    ZeroMemory(tt->receive_ring, sizeof(struct tun_ring));
+
+    tt->send_tail_moved = CreateEvent(NULL, FALSE, FALSE, NULL);
+    tt->receive_tail_moved = CreateEvent(NULL, FALSE, FALSE, NULL);
 #endif
 }
 
@@ -6207,6 +6219,30 @@ open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tun
             tt->ipapi_context_defined = true;
         }
     }
+
+    if (tt->wintun)
+    {
+        if (tt->options.msg_channel)
+        {
+            /* TODO */
+        }
+        else
+        {
+            if (!impersonate_as_system())
+            {
+                msg(M_FATAL, "ERROR:  Failed to impersonate as SYSTEM, make sure process is running under privileged account");
+            }
+            if (!register_ring_buffers(tt->hand, tt->send_ring, tt->receive_ring, tt->send_tail_moved, tt->receive_tail_moved))
+            {
+                msg(M_FATAL, "ERROR:  Failed to register ring buffers: %lu", GetLastError());
+            }
+            if (!RevertToSelf())
+            {
+                msg(M_FATAL, "ERROR:  RevertToSelf error: %lu", GetLastError());
+            }
+        }
+    }
+
     /*netcmd_semaphore_release ();*/
     gc_free(&gc);
 }
@@ -6345,6 +6381,15 @@ close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx)
         free(tt->actual_name);
     }
 
+    CloseHandle(tt->receive_tail_moved);
+    CloseHandle(tt->send_tail_moved);
+
+    free(tt->receive_ring);
+    free(tt->send_ring);
+
+    tt->receive_ring = NULL;
+    tt->send_ring = NULL;
+
     clear_tuntap(tt);
     free(tt);
     gc_free(&gc);
src/openvpn/tun.h +0 -0
index 19cab7e..cbdcce4 100644

src/openvpn/tun.h struct tuntap · line 190

Quoted 8 lines
src/openvpn/tun.h +5 -0
@@ -183,6 +183,11 @@ struct tuntap
 
     bool wintun; /* true if wintun is used instead of tap-windows6 */
     int standby_iter;
+
+    struct tun_ring *send_ring;
+    struct tun_ring *receive_ring;
+    HANDLE send_tail_moved;
+    HANDLE receive_tail_moved;

These are wintun-specific, right? Should we maybe clarify this by prefixing them with wintun_ ?

Quoted 272 lines
src/openvpn/tun.h +115 -1
 #else  /* ifdef _WIN32 */
     int fd; /* file descriptor for TUN/TAP dev */
 #endif
@@ -479,10 +484,124 @@ read_tun_buffered(struct tuntap *tt, struct buffer *buf)
     return tun_finalize(tt->hand, &tt->reads, buf);
 }
 
+static inline ULONG
+wintun_ring_packet_align(ULONG size)
+{
+    return (size + (WINTUN_PACKET_ALIGN - 1)) & ~(WINTUN_PACKET_ALIGN - 1);
+}
+
+static inline ULONG
+wintun_ring_wrap(ULONG value)
+{
+    return value & (WINTUN_RING_CAPACITY - 1);
+}
+
+static inline void
+read_wintun(struct tuntap *tt, struct buffer* buf)
+{
+    struct tun_ring *ring = tt->send_ring;
+    ULONG head = ring->head;
+    ULONG tail = ring->tail;
+    ULONG content_len;
+    struct TUN_PACKET *packet;
+    ULONG aligned_packet_size;
+
+    *buf = tt->reads.buf_init;
+    buf->len = 0;
+
+    if ((head >= WINTUN_RING_CAPACITY) || (tail >= WINTUN_RING_CAPACITY))
+    {
+        msg(M_INFO, "Wintun: ring capacity exceeded");
+        buf->len = -1;
+        return;
+    }
+
+    if (head == tail)
+    {
+        /* nothing to read */
+        return;
+    }
+
+    content_len = wintun_ring_wrap(tail - head);
+    if (content_len < sizeof(struct TUN_PACKET_HEADER))
+    {
+        msg(M_INFO, "Wintun: incomplete packet header in send ring");
+        buf->len = -1;
+        return;
+    }
+
+    packet = (struct TUN_PACKET *) &ring->data[head];
+    if (packet->size > WINTUN_MAX_PACKET_SIZE)
+    {
+        msg(M_INFO, "Wintun: packet too big in send ring");
+        buf->len = -1;
+        return;
+    }
+
+    aligned_packet_size = wintun_ring_packet_align(sizeof(struct TUN_PACKET_HEADER) + packet->size);
+    if (aligned_packet_size > content_len)
+    {
+        msg(M_INFO, "Wintun: incomplete packet in send ring");
+        buf->len = -1;
+        return;
+    }
+
+    buf_write(buf, packet->data, packet->size);
+
+    head = wintun_ring_wrap(head + aligned_packet_size);
+    ring->head = head;
+}
+
+static inline int
+write_wintun(struct tuntap *tt, struct buffer *buf)
+{
+    struct tun_ring *ring = tt->receive_ring;
+    ULONG head = ring->head;
+    ULONG tail = ring->tail;
+    ULONG aligned_packet_size;
+    ULONG buf_space;
+    struct TUN_PACKET *packet;
+
+    if ((head > WINTUN_RING_CAPACITY) || (tail >= WINTUN_RING_CAPACITY))
+    {
+        msg(M_INFO, "Wintun: head/tail value is over capacity");
+        return -1;
+    }
+
+    aligned_packet_size = wintun_ring_packet_align(sizeof(struct TUN_PACKET_HEADER) + BLEN(buf));
+    buf_space = wintun_ring_wrap(head - tail - WINTUN_PACKET_ALIGN);
+    if (aligned_packet_size > buf_space)
+    {
+        msg(M_INFO, "Wintun: ring is full");
+        return 0;
+    }
+
+    /* copy packet size and data into ring */
+    packet = (struct TUN_PACKET* )&ring->data[tail];
+    packet->size = BLEN(buf);
+    memcpy(packet->data, BPTR(buf), BLEN(buf));
+
+    /* move ring tail */
+    ring->tail = wintun_ring_wrap(tail + aligned_packet_size);
+    if (ring->alertable != 0)
+    {
+        SetEvent(tt->receive_tail_moved);
+    }
+
+    return BLEN(buf);
+}
+
 static inline int
 write_tun_buffered(struct tuntap *tt, struct buffer *buf)
 {
-    return tun_write_win32(tt, buf);
+    if (tt->wintun)
+    {
+        return write_wintun(tt, buf);
+    }
+    else
+    {
+        return tun_write_win32(tt, buf);
+    }
 }
 
 #else  /* ifdef _WIN32 */
src/openvpn/win32.c +120 -0
index eb4c030..e9e0258 100644
@@ -1493,4 +1493,124 @@ send_msg_iservice(HANDLE pipe, const void *data, size_t size,
     return ret;
 }
 
+bool
+impersonate_as_system()
+{
+    HANDLE thread_token, process_snapshot, winlogon_process, winlogon_token, duplicated_token;
+    PROCESSENTRY32 entry;
+    BOOL ret;
+    DWORD pid = 0;
+    TOKEN_PRIVILEGES privileges;
+
+    CLEAR(entry);
+    CLEAR(privileges);
+
+    entry.dwSize = sizeof(PROCESSENTRY32);
+
+    privileges.PrivilegeCount = 1;
+    privileges.Privileges->Attributes = SE_PRIVILEGE_ENABLED;
+
+    if (!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &privileges.Privileges[0].Luid))
+    {
+        return false;
+    }
+
+    if (!ImpersonateSelf(SecurityImpersonation))
+    {
+        return false;
+    }
+
+    if (!OpenThreadToken(GetCurrentThread(), TOKEN_ADJUST_PRIVILEGES, FALSE, &thread_token))
+    {
+        RevertToSelf();
+        return false;
+    }
+    if (!AdjustTokenPrivileges(thread_token, FALSE, &privileges, sizeof(privileges), NULL, NULL))
+    {
+        CloseHandle(thread_token);
+        RevertToSelf();
+        return false;
+    }
+    CloseHandle(thread_token);
+
+    process_snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
+    if (process_snapshot == INVALID_HANDLE_VALUE)
+    {
+        RevertToSelf();
+        return false;
+    }
+    for (ret = Process32First(process_snapshot, &entry); ret; ret = Process32Next(process_snapshot, &entry))
+    {
+        if (!_stricmp(entry.szExeFile, "winlogon.exe"))
+        {
+            pid = entry.th32ProcessID;
+            break;
+        }
+    }
+    CloseHandle(process_snapshot);
+    if (!pid)
+    {
+        RevertToSelf();
+        return false;
+    }
+
+    winlogon_process = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
+    if (!winlogon_process)
+    {
+        RevertToSelf();
+        return false;
+    }
+
+    if (!OpenProcessToken(winlogon_process, TOKEN_IMPERSONATE | TOKEN_DUPLICATE, &winlogon_token))
+    {
+        CloseHandle(winlogon_process);
+        RevertToSelf();
+        return false;
+    }
+    CloseHandle(winlogon_process);
+
+    if (!DuplicateToken(winlogon_token, SecurityImpersonation, &duplicated_token))
+    {
+        CloseHandle(winlogon_token);
+        RevertToSelf();
+        return false;
+    }
+    CloseHandle(winlogon_token);
+
+    if (!SetThreadToken(NULL, duplicated_token))
+    {
+        CloseHandle(duplicated_token);
+        RevertToSelf();
+        return false;
+    }
+    CloseHandle(duplicated_token);
+
+    return true;
+}
+
+bool
+register_ring_buffers(HANDLE device,
+                      struct tun_ring* send_ring,
+                      struct tun_ring* receive_ring,
+                      HANDLE send_tail_moved,
+                      HANDLE receive_tail_moved)
+{
+    struct tun_register_rings rr;
+    BOOL res;
+
+    ZeroMemory(&rr, sizeof(rr));
+
+    rr.send.ring = send_ring;
+    rr.send.ring_size = sizeof(send_ring->data);
+    rr.send.tail_moved = send_tail_moved;
+
+    rr.receive.ring = receive_ring;
+    rr.receive.ring_size = sizeof(receive_ring->data);
+    rr.receive.tail_moved = receive_tail_moved;
+
+    res = DeviceIoControl(device, TUN_IOCTL_REGISTER_RINGS, &rr, sizeof(rr), NULL, 0, NULL, NULL);
+
+    return res == TRUE;
+}
+
 #endif /* ifdef _WIN32 */
src/openvpn/win32.h +2 -0
index 4814bbc..007c7d7 100644
@@ -25,6 +25,8 @@
 #ifndef OPENVPN_WIN32_H
 #define OPENVPN_WIN32_H
 
+#include <winioctl.h>
+
 #include "mtu.h"
 #include "openvpn-msg.h"
 #include "argv.h"

src/openvpn/win32.h bool send_msg_iservice(HANDLE pipe, const void *data, size_t size, · line 328

Quoted 4 lines
src/openvpn/win32.h +1 -0
@@ -323,5 +325,50 @@ bool send_msg_iservice(HANDLE pipe, const void *data, size_t size,
 int
 openvpn_execve(const struct argv *a, const struct env_set *es, const unsigned int flags);
 
+#define WINTUN_RING_CAPACITY 0x800000

Can you explain why this is the right size? Document either in a comment, or in the commit message.

Quoted 37 lines
src/openvpn/win32.h +36 -0
+#define WINTUN_RING_TRAILING_BYTES 0x10000
+#define WINTUN_RING_FRAMING_SIZE 12
+#define WINTUN_MAX_PACKET_SIZE 0xffff
+#define WINTUN_PACKET_ALIGN 4
+
+struct tun_ring
+{
+    volatile ULONG head;
+    volatile ULONG tail;
+    volatile LONG alertable;
+    UCHAR data[WINTUN_RING_CAPACITY + WINTUN_RING_TRAILING_BYTES + WINTUN_RING_FRAMING_SIZE];
+};
+
+struct tun_register_rings
+{
+    struct
+    {
+        ULONG ring_size;
+        struct tun_ring *ring;
+        HANDLE tail_moved;
+    } send, receive;
+};
+
+struct TUN_PACKET_HEADER
+{
+    uint32_t size;
+};
+
+struct TUN_PACKET
+{
+    uint32_t size;
+    UCHAR data[WINTUN_MAX_PACKET_SIZE];
+};
+
+#define TUN_IOCTL_REGISTER_RINGS CTL_CODE(51820U, 0x970U, METHOD_BUFFERED, FILE_READ_DATA | FILE_WRITE_DATA)
+

src/openvpn/win32.h bool send_msg_iservice(HANDLE pipe, const void *data, size_t size, · line 374

Quoted 10 lines
src/openvpn/win32.h +8 -0
+bool impersonate_as_system();
+
+bool register_ring_buffers(HANDLE device,
+                           struct tun_ring *send_ring,
+                           struct tun_ring *receive_ring,
+                           HANDLE send_tail_moved,
+                           HANDLE receive_tail_moved);
+
 #endif /* ifndef OPENVPN_WIN32_H */
 #endif /* ifdef _WIN32 */

-Steffan

#40 lore raw

Re: [Openvpn-devel] [PATCH v2 4/7] wintun: ring buffers based I/O

Hi,

As a general remark: could you try to stick to the 80 char line length

Quoted 2 lines

limit?

Ok.

Quoted 8 lines

src/openvpn/forward.c read_incoming_tun(struct context *c) · line 1283

Quoted 3 lines
src/openvpn/forward.c +2 -0
+#ifdef _WIN32
+    }
 #endif

As Simon mentioned too, this code is getting more and more hard to read. Can we maybe do this in some cleaner way? Maybe add some helper function(s)?

Already fixed in v3.

Quoted 6 lines

+#ifdef _WIN32

Review

Quoted 2 lines
(quoted diff) +2 -0
+    }
+#endif

As above :)

Already fixed in v3.

Quoted 4 lines

src/openvpn/forward.h p2p_iow_flags(const struct context *c)

Quoted 3 lines
src/openvpn/forward.h +3 -0
+            if (tt->send_ring->head == tt->send_ring->tail)
+            {
+                /* nothing to read from tun -> remove tun read flag set

by IOW_READ */

Quoted 9 lines

src/openvpn/forward.h p2p_iow_flags(const struct context *c) · line 390

Quoted 5 lines
src/openvpn/forward.h +5 -0
+                flags &= ~IOW_READ_TUN;
+            }
+        }
+    }
+#endif

Looks like this should be a helper function (with a good name that helps understand what this does).

Implemented helper function tuntap_ring_empty().

Quoted 5 lines

+ c->c2.event_set_status = ret;

This chunk is the same as the "fast path" check above. Looks like you should re-arrange this code so you don't have to duplicate it.

Not quite (already discussed verbally).

Quoted 9 lines

+#ifdef _WIN32

src/openvpn/forward.c io_wait_dowork(struct context *c, const unsigned int flags)

Quoted 3 lines
src/openvpn/forward.c +3 -0
+    if (c->c1.tuntap && c->c1.tuntap->wintun)
+    {
+        if (c->c1.tuntap->send_ring->head !=

c->c1.tuntap->send_ring->tail)

Looking this deep into the tuntap struct from another module looks like a layering violation. Consider adding a helper function like

Fixed with the helper from above (tuntap_ring_empty).

Quoted 4 lines

+ {

Review

Quoted 2 lines
(quoted diff) +2 -0
+            /* add ring buffer event */
+            struct rw_handle rw = { .read =

c->c1.tuntap->send_tail_moved };

Quoted 9 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+            event_ctl(mtcp->es, &rw, EVENT_READ, MTCP_TUN);
+        }

Can this not be done by tun_set() below? At first glance, if either tun_event_handle() returns tt->send_tail_moved for wintun (instaed of tt->rw_handle), of if tt->rw_handle would be set to send_tail_moved for wintun. The latter might even allow you to get rid of tt->send_tail_removed completely.

Thanks for the idea.

Indeed, I did some refactoring and reused rw_handle.read/write instead of send/receive_tail_moved. Also got rid of wintun-specific event_ctl calls - now it is handled in tun_set().

Quoted 4 lines
Quoted 1 line

+ if (tt->send_ring->head == tt->send_ring->tail)

This should use the same helper function as suggested above.

Done.

Quoted 8 lines

+ struct tun_ring *send_ring;

src/openvpn/tun.h struct tuntap · line 190

Quoted 3 lines
src/openvpn/tun.h +3 -0
+    struct tun_ring *receive_ring;
+    HANDLE send_tail_moved;
+    HANDLE receive_tail_moved;

These are wintun-specific, right? Should we maybe clarify this by prefixing them with wintun_ ?

*_tail_moved are gone, added "wintun_" prefix to *_ring.

Quoted 5 lines
Quoted 1 line

+#define WINTUN_RING_CAPACITY 0x800000

Can you explain why this is the right size? Document either in a comment, or in the commit message.

Those are from WG client, mentioned it in a comment.

-Steffan

V4 is coming!

Signature · 1 line
-Lev
#41 lore raw

PATCH v4 4/7 wintun: ring buffers based I/O

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

Implemented according to Wintun documentation and reference client code.

Wintun uses ring buffers to communicate between kernel driver and user process. Client allocates send and receive ring buffers, creates events and passes it to kernel driver under LocalSystem privileges.

When data is available for read, wintun modifies "tail" pointer of send ring and signals via event. User process reads data from "head" to "tail" and updates "head" pointer.

When user process is ready to write, it writes to receive ring, updates "tail" pointer and signals to kernel via event.

In openvpn code we add send ring's event to event loop. Before performing io wait, we compare "head" and "tail" pointers of send ring and if they're different, we skip io wait and perform read.

This also adds ring buffers support to tcp and udp server code.

---
v4:
- added helper function tuntap_ring_empty()
- refactored event handling, got rid of separate event_ctl() call for wintun and send/receive_tail_moved members
- added wintun_ prefix for ring buffer variables
- added a comment explaining the size of wintun-specific buffers

v3:
- simplified convoluted #ifdefs
- replaced "greater than" with "greater or equal than"

v2;
- rebased on top of master

 src/openvpn/forward.c |  25 +++++++++-
 src/openvpn/forward.h |  38 +++++++++++++-
 src/openvpn/mtcp.c    |  19 ++++++-
 src/openvpn/mudp.c    |   7 ++-
 src/openvpn/options.c |   4 +-
 src/openvpn/syshead.h |   1 +
 src/openvpn/tun.c     |  62 ++++++++++++++++++++++-
 src/openvpn/tun.h     | 135 +++++++++++++++++++++++++++++++++++++++++++++++++-
 src/openvpn/win32.c   | 120 ++++++++++++++++++++++++++++++++++++++++++++
 src/openvpn/win32.h   |  51 +++++++++++++++++++
 10 files changed, 451 insertions(+), 11 deletions(-)
src/openvpn/forward.c +24 -1
index 8451706..3dc04a4 100644
@@ -1256,8 +1256,24 @@ read_incoming_tun(struct context *c)
     perf_push(PERF_READ_IN_TUN);
 
     c->c2.buf = c->c2.buffers->read_tun_buf;
+
 #ifdef _WIN32
-    read_tun_buffered(c->c1.tuntap, &c->c2.buf);
+    if (c->c1.tuntap->wintun)
+    {
+        read_wintun(c->c1.tuntap, &c->c2.buf);
+        if (c->c2.buf.len == -1)
+        {
+            register_signal(c, SIGHUP, "tun-abort");
+            c->persist.restart_sleep_seconds = 1;
+            msg(M_INFO, "Wintun read error, restarting");
+            perf_pop();
+            return;
+        }
+    }
+    else
+    {
+        read_tun_buffered(c->c1.tuntap, &c->c2.buf);
+    }
 #else
     ASSERT(buf_init(&c->c2.buf, FRAME_HEADROOM(&c->c2.frame)));
     ASSERT(buf_safe(&c->c2.buf, MAX_RW_SIZE_TUN(&c->c2.frame)));
@@ -2099,6 +2115,13 @@ io_wait_dowork(struct context *c, const unsigned int flags)
         tuntap |= EVENT_READ;
     }
 
+#ifdef _WIN32
+    if (tuntap_is_wintun(c->c1.tuntap))
+    {
+        tuntap = EVENT_READ;
+    }
+#endif
+
     /*
      * Configure event wait based on socket, tuntap flags.
      */
src/openvpn/forward.h +36 -2
index 48202c0..b711ff0 100644
@@ -375,6 +375,12 @@ p2p_iow_flags(const struct context *c)
     {
         flags |= IOW_TO_TUN;
     }
+#ifdef _WIN32
+    if (tuntap_ring_empty(c->c1.tuntap))
+    {
+        flags &= ~IOW_READ_TUN;
+    }
+#endif
     return flags;
 }
 
@@ -403,8 +409,36 @@ io_wait(struct context *c, const unsigned int flags)
     }
     else
     {
-        /* slow path */
-        io_wait_dowork(c, flags);
+#ifdef _WIN32
+        bool skip_iowait = flags & IOW_TO_TUN;
+        if (flags & IOW_READ_TUN)
+        {
+            /*
+             * don't read from tun if we have pending write to link,
+             * since every tun read overwrites to_link buffer filled
+             * by previous tun read
+             */
+            skip_iowait = !(flags & IOW_TO_LINK);
+        }
+        if (tuntap_is_wintun(c->c1.tuntap) && skip_iowait)
+        {
+            unsigned int ret = 0;
+            if (flags & IOW_TO_TUN)
+            {
+                ret |= TUN_WRITE;
+            }
+            if (flags & IOW_READ_TUN)
+            {
+                ret |= TUN_READ;
+            }
+            c->c2.event_set_status = ret;
+        }
+        else
+#endif
+        {
+            /* slow path */
+            io_wait_dowork(c, flags);
+        }
     }
 }
 
src/openvpn/mtcp.c +18 -1
index abe2059..30a13f7 100644
@@ -269,8 +269,25 @@ multi_tcp_wait(const struct context *c,
                struct multi_tcp *mtcp)
 {
     int status;
+    unsigned int *persistent = &mtcp->tun_rwflags;
     socket_set_listen_persistent(c->c2.link_socket, mtcp->es, MTCP_SOCKET);
-    tun_set(c->c1.tuntap, mtcp->es, EVENT_READ, MTCP_TUN, &mtcp->tun_rwflags);
+
+#ifdef _WIN32
+    if (tuntap_is_wintun(c->c1.tuntap))
+    {
+        if (!tuntap_ring_empty(c->c1.tuntap))
+        {
+            /* there is a data in wintun ring buffer, read it immediately */
+            mtcp->esr[0].arg = MTCP_TUN;
+            mtcp->esr[0].rwflags = EVENT_READ;
+            mtcp->n_esr = 1;
+            return 1;
+        }
+        persistent = NULL;
+    }
+#endif
+    tun_set(c->c1.tuntap, mtcp->es, EVENT_READ, MTCP_TUN, persistent);
+
 #ifdef ENABLE_MANAGEMENT
     if (management)
     {
src/openvpn/mudp.c +6 -1
index b7f061a..6a29ccc 100644
@@ -278,7 +278,12 @@ p2mp_iow_flags(const struct multi_context *m)
     {
         flags |= IOW_READ;
     }
-
+#ifdef _WIN32
+    if (tuntap_ring_empty(m->top.c1.tuntap))
+    {
+        flags &= ~IOW_READ_TUN;
+    }
+#endif
     return flags;
 }
 
src/openvpn/options.c +2 -2
index c282b58..9887443 100644
@@ -3005,10 +3005,10 @@ options_postprocess_mutate_invariant(struct options *options)
         options->ifconfig_noexec = false;
     }
 
-    /* for wintun kernel doesn't send DHCP requests, so use ipapi to set IP address and netmask */
+    /* for wintun kernel doesn't send DHCP requests, so use netsh to set IP address and netmask */
     if (options->wintun)
     {
-        options->tuntap_options.ip_win32_type = IPW32_SET_IPAPI;
+        options->tuntap_options.ip_win32_type = IPW32_SET_NETSH;
     }
 
     remap_redirect_gateway_flags(options);
src/openvpn/syshead.h +1 -0
index 899aa59..e9accb5 100644
@@ -39,6 +39,7 @@
 #ifdef _WIN32
 #include <windows.h>
 #include <winsock2.h>
+#include <tlhelp32.h>
 #define sleep(x) Sleep((x)*1000)
 #define random rand
 #define srandom srand
src/openvpn/tun.c +60 -2

81 lines not on this page · raw message

src/openvpn/tun.h +133 -2

162 lines not on this page · raw message

src/openvpn/win32.c +120 -0

124 lines not on this page · raw message

src/openvpn/win32.h +51 -0

62 lines not on this page · raw message

Signature · 1 line
2.7.4
#42 lore raw

PATCH v5 4/7 wintun: ring buffers based I/O

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

Implemented according to Wintun documentation and reference client code.

Wintun uses ring buffers to communicate between kernel driver and user process. Client allocates send and receive ring buffers, creates events and passes it to kernel driver under LocalSystem privileges.

When data is available for read, wintun modifies "tail" pointer of send ring and signals via event. User process reads data from "head" to "tail" and updates "head" pointer.

When user process is ready to write, it writes to receive ring, updates "tail" pointer and signals to kernel via event.

In openvpn code we add send ring's event to event loop. Before performing io wait, we compare "head" and "tail" pointers of send ring and if they're different, we skip io wait and perform read.

This also adds ring buffers support to tcp and udp server code.

---
v5:
- fix crash at ring buffer registration on Win7 (passing NULL to DeviceIOControl, reported by kitsune1)

v4:
- added helper function tuntap_ring_empty()
- refactored event handling, got rid of separate event_ctl() call for wintun and send/receive_tail_moved members
- added wintun_ prefix for ring buffer variables
- added a comment explaining the size of wintun-specific buffers

v3:
- simplified convoluted #ifdefs
- replaced "greater than" with "greater or equal than"

v2;
- rebased on top of master

 src/openvpn/forward.c |  25 +++++++-
 src/openvpn/forward.h |  38 +++++++++++-
 src/openvpn/mtcp.c    |  19 +++++-
 src/openvpn/mudp.c    |   7 ++-
 src/openvpn/options.c |   4 +-
 src/openvpn/syshead.h |   1 +
 src/openvpn/tun.c     |  62 ++++++++++++++++++-
 src/openvpn/tun.h     | 135 +++++++++++++++++++++++++++++++++++++++++-
 src/openvpn/win32.c   | 122 ++++++++++++++++++++++++++++++++++++++
 src/openvpn/win32.h   |  51 ++++++++++++++++
 10 files changed, 453 insertions(+), 11 deletions(-)
src/openvpn/forward.c +24 -1
index 8451706b..3dc04a40 100644
@@ -1256,8 +1256,24 @@ read_incoming_tun(struct context *c)
     perf_push(PERF_READ_IN_TUN);
 
     c->c2.buf = c->c2.buffers->read_tun_buf;
+
 #ifdef _WIN32
-    read_tun_buffered(c->c1.tuntap, &c->c2.buf);
+    if (c->c1.tuntap->wintun)
+    {
+        read_wintun(c->c1.tuntap, &c->c2.buf);
+        if (c->c2.buf.len == -1)
+        {
+            register_signal(c, SIGHUP, "tun-abort");
+            c->persist.restart_sleep_seconds = 1;
+            msg(M_INFO, "Wintun read error, restarting");
+            perf_pop();
+            return;
+        }
+    }
+    else
+    {
+        read_tun_buffered(c->c1.tuntap, &c->c2.buf);
+    }
 #else
     ASSERT(buf_init(&c->c2.buf, FRAME_HEADROOM(&c->c2.frame)));
     ASSERT(buf_safe(&c->c2.buf, MAX_RW_SIZE_TUN(&c->c2.frame)));
@@ -2099,6 +2115,13 @@ io_wait_dowork(struct context *c, const unsigned int flags)
         tuntap |= EVENT_READ;
     }
 
+#ifdef _WIN32
+    if (tuntap_is_wintun(c->c1.tuntap))
+    {
+        tuntap = EVENT_READ;
+    }
+#endif
+
     /*
      * Configure event wait based on socket, tuntap flags.
      */
src/openvpn/forward.h +36 -2
index 48202c07..b711ff00 100644
@@ -375,6 +375,12 @@ p2p_iow_flags(const struct context *c)
     {
         flags |= IOW_TO_TUN;
     }
+#ifdef _WIN32
+    if (tuntap_ring_empty(c->c1.tuntap))
+    {
+        flags &= ~IOW_READ_TUN;
+    }
+#endif
     return flags;
 }
 
@@ -403,8 +409,36 @@ io_wait(struct context *c, const unsigned int flags)
     }
     else
     {
-        /* slow path */
-        io_wait_dowork(c, flags);
+#ifdef _WIN32
+        bool skip_iowait = flags & IOW_TO_TUN;
+        if (flags & IOW_READ_TUN)
+        {
+            /*
+             * don't read from tun if we have pending write to link,
+             * since every tun read overwrites to_link buffer filled
+             * by previous tun read
+             */
+            skip_iowait = !(flags & IOW_TO_LINK);
+        }
+        if (tuntap_is_wintun(c->c1.tuntap) && skip_iowait)
+        {
+            unsigned int ret = 0;
+            if (flags & IOW_TO_TUN)
+            {
+                ret |= TUN_WRITE;
+            }
+            if (flags & IOW_READ_TUN)
+            {
+                ret |= TUN_READ;
+            }
+            c->c2.event_set_status = ret;
+        }
+        else
+#endif
+        {
+            /* slow path */
+            io_wait_dowork(c, flags);
+        }
     }
 }
 
src/openvpn/mtcp.c +18 -1
index abe20593..30a13f73 100644
@@ -269,8 +269,25 @@ multi_tcp_wait(const struct context *c,
                struct multi_tcp *mtcp)
 {
     int status;
+    unsigned int *persistent = &mtcp->tun_rwflags;
     socket_set_listen_persistent(c->c2.link_socket, mtcp->es, MTCP_SOCKET);
-    tun_set(c->c1.tuntap, mtcp->es, EVENT_READ, MTCP_TUN, &mtcp->tun_rwflags);
+
+#ifdef _WIN32
+    if (tuntap_is_wintun(c->c1.tuntap))
+    {
+        if (!tuntap_ring_empty(c->c1.tuntap))
+        {
+            /* there is a data in wintun ring buffer, read it immediately */
+            mtcp->esr[0].arg = MTCP_TUN;
+            mtcp->esr[0].rwflags = EVENT_READ;
+            mtcp->n_esr = 1;
+            return 1;
+        }
+        persistent = NULL;
+    }
+#endif
+    tun_set(c->c1.tuntap, mtcp->es, EVENT_READ, MTCP_TUN, persistent);
+
 #ifdef ENABLE_MANAGEMENT
     if (management)
     {
src/openvpn/mudp.c +6 -1
index b7f061a2..6a29ccc8 100644
@@ -278,7 +278,12 @@ p2mp_iow_flags(const struct multi_context *m)
     {
         flags |= IOW_READ;
     }
-
+#ifdef _WIN32
+    if (tuntap_ring_empty(m->top.c1.tuntap))
+    {
+        flags &= ~IOW_READ_TUN;
+    }
+#endif
     return flags;
 }
 
src/openvpn/options.c +2 -2
index c282b582..98874431 100644
@@ -3005,10 +3005,10 @@ options_postprocess_mutate_invariant(struct options *options)
         options->ifconfig_noexec = false;
     }
 
-    /* for wintun kernel doesn't send DHCP requests, so use ipapi to set IP address and netmask */
+    /* for wintun kernel doesn't send DHCP requests, so use netsh to set IP address and netmask */
     if (options->wintun)
     {
-        options->tuntap_options.ip_win32_type = IPW32_SET_IPAPI;
+        options->tuntap_options.ip_win32_type = IPW32_SET_NETSH;
     }
 
     remap_redirect_gateway_flags(options);
src/openvpn/syshead.h +1 -0
index 899aa59e..e9accb52 100644
@@ -39,6 +39,7 @@
 #ifdef _WIN32
 #include <windows.h>
 #include <winsock2.h>
+#include <tlhelp32.h>
 #define sleep(x) Sleep((x)*1000)
 #define random rand
 #define srandom srand
src/openvpn/tun.c +60 -2

81 lines not on this page · raw message

src/openvpn/tun.h +133 -2

162 lines not on this page · raw message

src/openvpn/win32.c +122 -0

126 lines not on this page · raw message

src/openvpn/win32.h +51 -0

62 lines not on this page · raw message

Signature · 1 line
2.17.1
#43 lore raw

PATCH v6 1/1 wintun: ring buffers based I/O

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

Implemented according to Wintun documentation and reference client code.

Wintun uses ring buffers to communicate between kernel driver and user process. Client allocates send and receive ring buffers, creates events and passes it to kernel driver under LocalSystem privileges.

When data is available for read, wintun modifies "tail" pointer of send ring and signals via event. User process reads data from "head" to "tail" and updates "head" pointer.

When user process is ready to write, it writes to receive ring, updates "tail" pointer and signals to kernel via event.

In openvpn code we add send ring's event to event loop. Before performing io wait, we compare "head" and "tail" pointers of send ring and if they're different, we skip io wait and perform read.

This also adds ring buffers support to tcp and udp server code.

---
v6:
- added a sanity check to write_wintun() to avoid writing malformed IPv4/6 packet, which causes "ring buffer is out of capacity" error.

v5:
- fix crash at ring buffer registration on Win7 (passing NULL to DeviceIOControl, reported by kitsune1)

v4:
- added helper function tuntap_ring_empty()
- refactored event handling, got rid of separate event_ctl() call for wintun and send/receive_tail_moved members
- added wintun_ prefix for ring buffer variables
- added a comment explaining the size of wintun-specific buffers

v3:
- simplified convoluted #ifdefs
- replaced "greater than" with "greater or equal than"

v2;
- rebased on top of master

 src/openvpn/forward.c |  25 ++++++-
 src/openvpn/forward.h |  38 +++++++++-
 src/openvpn/mtcp.c    |  19 ++++-
 src/openvpn/mudp.c    |   7 +-
 src/openvpn/options.c |   4 +-
 src/openvpn/syshead.h |   1 +
 src/openvpn/tun.c     |  62 +++++++++++++++-
 src/openvpn/tun.h     | 169 +++++++++++++++++++++++++++++++++++++++++-
 src/openvpn/win32.c   | 122 ++++++++++++++++++++++++++++++
 src/openvpn/win32.h   |  51 +++++++++++++
 10 files changed, 487 insertions(+), 11 deletions(-)
src/openvpn/forward.c +24 -1
index 8451706b..3dc04a40 100644
@@ -1256,8 +1256,24 @@ read_incoming_tun(struct context *c)
     perf_push(PERF_READ_IN_TUN);
 
     c->c2.buf = c->c2.buffers->read_tun_buf;
+
 #ifdef _WIN32
-    read_tun_buffered(c->c1.tuntap, &c->c2.buf);
+    if (c->c1.tuntap->wintun)
+    {
+        read_wintun(c->c1.tuntap, &c->c2.buf);
+        if (c->c2.buf.len == -1)
+        {
+            register_signal(c, SIGHUP, "tun-abort");
+            c->persist.restart_sleep_seconds = 1;
+            msg(M_INFO, "Wintun read error, restarting");
+            perf_pop();
+            return;
+        }
+    }
+    else
+    {
+        read_tun_buffered(c->c1.tuntap, &c->c2.buf);
+    }
 #else
     ASSERT(buf_init(&c->c2.buf, FRAME_HEADROOM(&c->c2.frame)));
     ASSERT(buf_safe(&c->c2.buf, MAX_RW_SIZE_TUN(&c->c2.frame)));
@@ -2099,6 +2115,13 @@ io_wait_dowork(struct context *c, const unsigned int flags)
         tuntap |= EVENT_READ;
     }
 
+#ifdef _WIN32
+    if (tuntap_is_wintun(c->c1.tuntap))
+    {
+        tuntap = EVENT_READ;
+    }
+#endif
+
     /*
      * Configure event wait based on socket, tuntap flags.
      */
src/openvpn/forward.h +36 -2
index 48202c07..b711ff00 100644
@@ -375,6 +375,12 @@ p2p_iow_flags(const struct context *c)
     {
         flags |= IOW_TO_TUN;
     }
+#ifdef _WIN32
+    if (tuntap_ring_empty(c->c1.tuntap))
+    {
+        flags &= ~IOW_READ_TUN;
+    }
+#endif
     return flags;
 }
 
@@ -403,8 +409,36 @@ io_wait(struct context *c, const unsigned int flags)
     }
     else
     {
-        /* slow path */
-        io_wait_dowork(c, flags);
+#ifdef _WIN32
+        bool skip_iowait = flags & IOW_TO_TUN;
+        if (flags & IOW_READ_TUN)
+        {
+            /*
+             * don't read from tun if we have pending write to link,
+             * since every tun read overwrites to_link buffer filled
+             * by previous tun read
+             */
+            skip_iowait = !(flags & IOW_TO_LINK);
+        }
+        if (tuntap_is_wintun(c->c1.tuntap) && skip_iowait)
+        {
+            unsigned int ret = 0;
+            if (flags & IOW_TO_TUN)
+            {
+                ret |= TUN_WRITE;
+            }
+            if (flags & IOW_READ_TUN)
+            {
+                ret |= TUN_READ;
+            }
+            c->c2.event_set_status = ret;
+        }
+        else
+#endif
+        {
+            /* slow path */
+            io_wait_dowork(c, flags);
+        }
     }
 }
 
src/openvpn/mtcp.c +18 -1
index abe20593..30a13f73 100644
@@ -269,8 +269,25 @@ multi_tcp_wait(const struct context *c,
                struct multi_tcp *mtcp)
 {
     int status;
+    unsigned int *persistent = &mtcp->tun_rwflags;
     socket_set_listen_persistent(c->c2.link_socket, mtcp->es, MTCP_SOCKET);
-    tun_set(c->c1.tuntap, mtcp->es, EVENT_READ, MTCP_TUN, &mtcp->tun_rwflags);
+
+#ifdef _WIN32
+    if (tuntap_is_wintun(c->c1.tuntap))
+    {
+        if (!tuntap_ring_empty(c->c1.tuntap))
+        {
+            /* there is a data in wintun ring buffer, read it immediately */
+            mtcp->esr[0].arg = MTCP_TUN;
+            mtcp->esr[0].rwflags = EVENT_READ;
+            mtcp->n_esr = 1;
+            return 1;
+        }
+        persistent = NULL;
+    }
+#endif
+    tun_set(c->c1.tuntap, mtcp->es, EVENT_READ, MTCP_TUN, persistent);
+
 #ifdef ENABLE_MANAGEMENT
     if (management)
     {
src/openvpn/mudp.c +6 -1
index b7f061a2..6a29ccc8 100644
@@ -278,7 +278,12 @@ p2mp_iow_flags(const struct multi_context *m)
     {
         flags |= IOW_READ;
     }
-
+#ifdef _WIN32
+    if (tuntap_ring_empty(m->top.c1.tuntap))
+    {
+        flags &= ~IOW_READ_TUN;
+    }
+#endif
     return flags;
 }
 
src/openvpn/options.c +2 -2
index 49affc29..cebcbb07 100644
@@ -3016,10 +3016,10 @@ options_postprocess_mutate_invariant(struct options *options)
         options->ifconfig_noexec = false;
     }
 
-    /* for wintun kernel doesn't send DHCP requests, so use ipapi to set IP address and netmask */
+    /* for wintun kernel doesn't send DHCP requests, so use netsh to set IP address and netmask */
     if (options->wintun)
     {
-        options->tuntap_options.ip_win32_type = IPW32_SET_IPAPI;
+        options->tuntap_options.ip_win32_type = IPW32_SET_NETSH;
     }
 
     remap_redirect_gateway_flags(options);
src/openvpn/syshead.h +1 -0
index 899aa59e..e9accb52 100644
@@ -39,6 +39,7 @@
 #ifdef _WIN32
 #include <windows.h>
 #include <winsock2.h>
+#include <tlhelp32.h>
 #define sleep(x) Sleep((x)*1000)
 #define random rand
 #define srandom srand
src/openvpn/tun.c +60 -2

81 lines not on this page · raw message

src/openvpn/tun.h +167 -2

196 lines not on this page · raw message

src/openvpn/win32.c +122 -0

126 lines not on this page · raw message

src/openvpn/win32.h +51 -0

62 lines not on this page · raw message

Signature · 1 line
2.17.1
#44 lore raw

PATCH v6 4/7 wintun: ring buffers based I/O

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

Implemented according to Wintun documentation and reference client code.

Wintun uses ring buffers to communicate between kernel driver and user process. Client allocates send and receive ring buffers, creates events and passes it to kernel driver under LocalSystem privileges.

When data is available for read, wintun modifies "tail" pointer of send ring and signals via event. User process reads data from "head" to "tail" and updates "head" pointer.

When user process is ready to write, it writes to receive ring, updates "tail" pointer and signals to kernel via event.

In openvpn code we add send ring's event to event loop. Before performing io wait, we compare "head" and "tail" pointers of send ring and if they're different, we skip io wait and perform read.

This also adds ring buffers support to tcp and udp server code.

---
v6:
- added a sanity check to write_wintun() to avoid writing malformed IPv4/6 packet, which causes "ring buffer is out of capacity" error.

v5:
- fix crash at ring buffer registration on Win7 (passing NULL to DeviceIOControl, reported by kitsune1)

v4:
- added helper function tuntap_ring_empty()
- refactored event handling, got rid of separate event_ctl() call for wintun and send/receive_tail_moved members
- added wintun_ prefix for ring buffer variables
- added a comment explaining the size of wintun-specific buffers

v3:
- simplified convoluted #ifdefs
- replaced "greater than" with "greater or equal than"

v2;
- rebased on top of master

 src/openvpn/forward.c |  25 ++++++-
 src/openvpn/forward.h |  38 +++++++++-
 src/openvpn/mtcp.c    |  19 ++++-
 src/openvpn/mudp.c    |   7 +-
 src/openvpn/options.c |   4 +-
 src/openvpn/syshead.h |   1 +
 src/openvpn/tun.c     |  62 +++++++++++++++-
 src/openvpn/tun.h     | 169 +++++++++++++++++++++++++++++++++++++++++-
 src/openvpn/win32.c   | 122 ++++++++++++++++++++++++++++++
 src/openvpn/win32.h   |  51 +++++++++++++
 10 files changed, 487 insertions(+), 11 deletions(-)
src/openvpn/forward.c +24 -1
index 8451706b..3dc04a40 100644
@@ -1256,8 +1256,24 @@ read_incoming_tun(struct context *c)
     perf_push(PERF_READ_IN_TUN);
 
     c->c2.buf = c->c2.buffers->read_tun_buf;
+
 #ifdef _WIN32
-    read_tun_buffered(c->c1.tuntap, &c->c2.buf);
+    if (c->c1.tuntap->wintun)
+    {
+        read_wintun(c->c1.tuntap, &c->c2.buf);
+        if (c->c2.buf.len == -1)
+        {
+            register_signal(c, SIGHUP, "tun-abort");
+            c->persist.restart_sleep_seconds = 1;
+            msg(M_INFO, "Wintun read error, restarting");
+            perf_pop();
+            return;
+        }
+    }
+    else
+    {
+        read_tun_buffered(c->c1.tuntap, &c->c2.buf);
+    }
 #else
     ASSERT(buf_init(&c->c2.buf, FRAME_HEADROOM(&c->c2.frame)));
     ASSERT(buf_safe(&c->c2.buf, MAX_RW_SIZE_TUN(&c->c2.frame)));
@@ -2099,6 +2115,13 @@ io_wait_dowork(struct context *c, const unsigned int flags)
         tuntap |= EVENT_READ;
     }
 
+#ifdef _WIN32
+    if (tuntap_is_wintun(c->c1.tuntap))
+    {
+        tuntap = EVENT_READ;
+    }
+#endif
+
     /*
      * Configure event wait based on socket, tuntap flags.
      */
src/openvpn/forward.h +36 -2
index 48202c07..b711ff00 100644
@@ -375,6 +375,12 @@ p2p_iow_flags(const struct context *c)
     {
         flags |= IOW_TO_TUN;
     }
+#ifdef _WIN32
+    if (tuntap_ring_empty(c->c1.tuntap))
+    {
+        flags &= ~IOW_READ_TUN;
+    }
+#endif
     return flags;
 }
 
@@ -403,8 +409,36 @@ io_wait(struct context *c, const unsigned int flags)
     }
     else
     {
-        /* slow path */
-        io_wait_dowork(c, flags);
+#ifdef _WIN32
+        bool skip_iowait = flags & IOW_TO_TUN;
+        if (flags & IOW_READ_TUN)
+        {
+            /*
+             * don't read from tun if we have pending write to link,
+             * since every tun read overwrites to_link buffer filled
+             * by previous tun read
+             */
+            skip_iowait = !(flags & IOW_TO_LINK);
+        }
+        if (tuntap_is_wintun(c->c1.tuntap) && skip_iowait)
+        {
+            unsigned int ret = 0;
+            if (flags & IOW_TO_TUN)
+            {
+                ret |= TUN_WRITE;
+            }
+            if (flags & IOW_READ_TUN)
+            {
+                ret |= TUN_READ;
+            }
+            c->c2.event_set_status = ret;
+        }
+        else
+#endif
+        {
+            /* slow path */
+            io_wait_dowork(c, flags);
+        }
     }
 }
 
src/openvpn/mtcp.c +18 -1
index abe20593..30a13f73 100644
@@ -269,8 +269,25 @@ multi_tcp_wait(const struct context *c,
                struct multi_tcp *mtcp)
 {
     int status;
+    unsigned int *persistent = &mtcp->tun_rwflags;
     socket_set_listen_persistent(c->c2.link_socket, mtcp->es, MTCP_SOCKET);
-    tun_set(c->c1.tuntap, mtcp->es, EVENT_READ, MTCP_TUN, &mtcp->tun_rwflags);
+
+#ifdef _WIN32
+    if (tuntap_is_wintun(c->c1.tuntap))
+    {
+        if (!tuntap_ring_empty(c->c1.tuntap))
+        {
+            /* there is a data in wintun ring buffer, read it immediately */
+            mtcp->esr[0].arg = MTCP_TUN;
+            mtcp->esr[0].rwflags = EVENT_READ;
+            mtcp->n_esr = 1;
+            return 1;
+        }
+        persistent = NULL;
+    }
+#endif
+    tun_set(c->c1.tuntap, mtcp->es, EVENT_READ, MTCP_TUN, persistent);
+
 #ifdef ENABLE_MANAGEMENT
     if (management)
     {
src/openvpn/mudp.c +6 -1
index b7f061a2..6a29ccc8 100644
@@ -278,7 +278,12 @@ p2mp_iow_flags(const struct multi_context *m)
     {
         flags |= IOW_READ;
     }
-
+#ifdef _WIN32
+    if (tuntap_ring_empty(m->top.c1.tuntap))
+    {
+        flags &= ~IOW_READ_TUN;
+    }
+#endif
     return flags;
 }
 
src/openvpn/options.c +2 -2
index 49affc29..cebcbb07 100644
@@ -3016,10 +3016,10 @@ options_postprocess_mutate_invariant(struct options *options)
         options->ifconfig_noexec = false;
     }
 
-    /* for wintun kernel doesn't send DHCP requests, so use ipapi to set IP address and netmask */
+    /* for wintun kernel doesn't send DHCP requests, so use netsh to set IP address and netmask */
     if (options->wintun)
     {
-        options->tuntap_options.ip_win32_type = IPW32_SET_IPAPI;
+        options->tuntap_options.ip_win32_type = IPW32_SET_NETSH;
     }
 
     remap_redirect_gateway_flags(options);
src/openvpn/syshead.h +1 -0
index 899aa59e..e9accb52 100644
@@ -39,6 +39,7 @@
 #ifdef _WIN32
 #include <windows.h>
 #include <winsock2.h>
+#include <tlhelp32.h>
 #define sleep(x) Sleep((x)*1000)
 #define random rand
 #define srandom srand
src/openvpn/tun.c +60 -2

81 lines not on this page · raw message

src/openvpn/tun.h +167 -2

196 lines not on this page · raw message

src/openvpn/win32.c +122 -0

126 lines not on this page · raw message

src/openvpn/win32.h +51 -0

62 lines not on this page · raw message

Signature · 1 line
2.17.1
Acked-by on v6 4/7
#45 lore raw

Re: [Openvpn-devel] [PATCH v6 4/7] wintun: ring buffers based I/O

Hi Lev,

Thanks for the update. Even with the latest added fixes and sanity checks, the resulting code is now 30 lines shorter *and* more readable.

Some last questions / comments inline:

src/openvpn/forward.c io_wait_dowork(struct context *c, const unsigned int flags) · line 2123

Quoted 10 lines · Lev Stipakov
src/openvpn/forward.c +6 -0
@@ -2099,6 +2115,13 @@ io_wait_dowork(struct context *c, const unsigned int flags)
         tuntap |= EVENT_READ;
     }
 
+#ifdef _WIN32
+    if (tuntap_is_wintun(c->c1.tuntap))
+    {
+        tuntap = EVENT_READ;
+    }
+#endif

Now that the code is refactored, it is clear that for the tuntap case the code will ignore any of the earlier set flags. Can you maybe add a comment explaining why the other flags are being ignored?

src/openvpn/mtcp.c multi_tcp_wait(const struct context *c, · line 280

Quoted 1 line
src/openvpn/mtcp.c +1 -0
+            /* there is a data in wintun ring buffer, read it immediately */

Grammar nit: "there is data" (remove 'a').

Otherwise, as far as stare-at-code goes, I'm fine with this patch now. I haven't tested it, nor have I reviewed Windows-specific interaction. So this needs at least one other review from someone able to review those parts.

Regarding the integration into OpenVPN:

-Steffan

#46 lore raw

PATCH v7 4/7 wintun: ring buffers based I/O

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

Implemented according to Wintun documentation and reference client code.

Wintun uses ring buffers to communicate between kernel driver and user process. Client allocates send and receive ring buffers, creates events and passes it to kernel driver under LocalSystem privileges.

When data is available for read, wintun modifies "tail" pointer of send ring and signals via event. User process reads data from "head" to "tail" and updates "head" pointer.

When user process is ready to write, it writes to receive ring, updates "tail" pointer and signals to kernel via event.

In openvpn code we add send ring's event to event loop. Before performing io wait, we compare "head" and "tail" pointers of send ring and if they're different, we skip io wait and perform read.

This also adds ring buffers support to tcp and udp server code.

---

v7:
- fix comments (no code changes)

v6:
- added a sanity check to write_wintun() to avoid writing malformed IPv4/6 packet, which causes "ring buffer is out of capacity" error.

v5:
- fix crash at ring buffer registration on Win7 (passing NULL to DeviceIOControl, reported by kitsune1)

v4:
- added helper function tuntap_ring_empty()
- refactored event handling, got rid of separate event_ctl() call for wintun and send/receive_tail_moved members
- added wintun_ prefix for ring buffer variables
- added a comment explaining the size of wintun-specific buffers

v3:
- simplified convoluted #ifdefs
- replaced "greater than" with "greater or equal than"

v2:
- rebased on top of master

 src/openvpn/forward.c |  29 +++++++-
 src/openvpn/forward.h |  38 +++++++++-
 src/openvpn/mtcp.c    |  19 ++++-
 src/openvpn/mudp.c    |   7 +-
 src/openvpn/options.c |   4 +-
 src/openvpn/syshead.h |   1 +
 src/openvpn/tun.c     |  62 +++++++++++++++-
 src/openvpn/tun.h     | 169 +++++++++++++++++++++++++++++++++++++++++-
 src/openvpn/win32.c   | 122 ++++++++++++++++++++++++++++++
 src/openvpn/win32.h   |  51 +++++++++++++
 10 files changed, 491 insertions(+), 11 deletions(-)
src/openvpn/forward.c +28 -1
index 8451706b..6b823613 100644
@@ -1256,8 +1256,24 @@ read_incoming_tun(struct context *c)
     perf_push(PERF_READ_IN_TUN);
 
     c->c2.buf = c->c2.buffers->read_tun_buf;
+
 #ifdef _WIN32
-    read_tun_buffered(c->c1.tuntap, &c->c2.buf);
+    if (c->c1.tuntap->wintun)
+    {
+        read_wintun(c->c1.tuntap, &c->c2.buf);
+        if (c->c2.buf.len == -1)
+        {
+            register_signal(c, SIGHUP, "tun-abort");
+            c->persist.restart_sleep_seconds = 1;
+            msg(M_INFO, "Wintun read error, restarting");
+            perf_pop();
+            return;
+        }
+    }
+    else
+    {
+        read_tun_buffered(c->c1.tuntap, &c->c2.buf);
+    }
 #else
     ASSERT(buf_init(&c->c2.buf, FRAME_HEADROOM(&c->c2.frame)));
     ASSERT(buf_safe(&c->c2.buf, MAX_RW_SIZE_TUN(&c->c2.frame)));
@@ -2099,6 +2115,17 @@ io_wait_dowork(struct context *c, const unsigned int flags)
         tuntap |= EVENT_READ;
     }
 
+#ifdef _WIN32
+    if (tuntap_is_wintun(c->c1.tuntap))
+    {
+        /*
+         * With wintun we are only interested in read event. Ring buffer is
+         * always ready for write, so we don't do wait.
+         */
+        tuntap = EVENT_READ;
+    }
+#endif
+
     /*
      * Configure event wait based on socket, tuntap flags.
      */
src/openvpn/forward.h +36 -2
index 48202c07..b711ff00 100644
@@ -375,6 +375,12 @@ p2p_iow_flags(const struct context *c)
     {
         flags |= IOW_TO_TUN;
     }
+#ifdef _WIN32
+    if (tuntap_ring_empty(c->c1.tuntap))
+    {
+        flags &= ~IOW_READ_TUN;
+    }
+#endif
     return flags;
 }
 
@@ -403,8 +409,36 @@ io_wait(struct context *c, const unsigned int flags)
     }
     else
     {
-        /* slow path */
-        io_wait_dowork(c, flags);
+#ifdef _WIN32
+        bool skip_iowait = flags & IOW_TO_TUN;
+        if (flags & IOW_READ_TUN)
+        {
+            /*
+             * don't read from tun if we have pending write to link,
+             * since every tun read overwrites to_link buffer filled
+             * by previous tun read
+             */
+            skip_iowait = !(flags & IOW_TO_LINK);
+        }
+        if (tuntap_is_wintun(c->c1.tuntap) && skip_iowait)
+        {
+            unsigned int ret = 0;
+            if (flags & IOW_TO_TUN)
+            {
+                ret |= TUN_WRITE;
+            }
+            if (flags & IOW_READ_TUN)
+            {
+                ret |= TUN_READ;
+            }
+            c->c2.event_set_status = ret;
+        }
+        else
+#endif
+        {
+            /* slow path */
+            io_wait_dowork(c, flags);
+        }
     }
 }
 
src/openvpn/mtcp.c +18 -1
index abe20593..ee28a710 100644
@@ -269,8 +269,25 @@ multi_tcp_wait(const struct context *c,
                struct multi_tcp *mtcp)
 {
     int status;
+    unsigned int *persistent = &mtcp->tun_rwflags;
     socket_set_listen_persistent(c->c2.link_socket, mtcp->es, MTCP_SOCKET);
-    tun_set(c->c1.tuntap, mtcp->es, EVENT_READ, MTCP_TUN, &mtcp->tun_rwflags);
+
+#ifdef _WIN32
+    if (tuntap_is_wintun(c->c1.tuntap))
+    {
+        if (!tuntap_ring_empty(c->c1.tuntap))
+        {
+            /* there is data in wintun ring buffer, read it immediately */
+            mtcp->esr[0].arg = MTCP_TUN;
+            mtcp->esr[0].rwflags = EVENT_READ;
+            mtcp->n_esr = 1;
+            return 1;
+        }
+        persistent = NULL;
+    }
+#endif
+    tun_set(c->c1.tuntap, mtcp->es, EVENT_READ, MTCP_TUN, persistent);
+
 #ifdef ENABLE_MANAGEMENT
     if (management)
     {
src/openvpn/mudp.c +6 -1
index b7f061a2..6a29ccc8 100644
@@ -278,7 +278,12 @@ p2mp_iow_flags(const struct multi_context *m)
     {
         flags |= IOW_READ;
     }
-
+#ifdef _WIN32
+    if (tuntap_ring_empty(m->top.c1.tuntap))
+    {
+        flags &= ~IOW_READ_TUN;
+    }
+#endif
     return flags;
 }
 
src/openvpn/options.c +2 -2
index 49affc29..cebcbb07 100644
@@ -3016,10 +3016,10 @@ options_postprocess_mutate_invariant(struct options *options)
         options->ifconfig_noexec = false;
     }
 
-    /* for wintun kernel doesn't send DHCP requests, so use ipapi to set IP address and netmask */
+    /* for wintun kernel doesn't send DHCP requests, so use netsh to set IP address and netmask */
     if (options->wintun)
     {
-        options->tuntap_options.ip_win32_type = IPW32_SET_IPAPI;
+        options->tuntap_options.ip_win32_type = IPW32_SET_NETSH;
     }
 
     remap_redirect_gateway_flags(options);
src/openvpn/syshead.h +1 -0
index 899aa59e..e9accb52 100644
@@ -39,6 +39,7 @@
 #ifdef _WIN32
 #include <windows.h>
 #include <winsock2.h>
+#include <tlhelp32.h>
 #define sleep(x) Sleep((x)*1000)
 #define random rand
 #define srandom srand
src/openvpn/tun.c +60 -2

81 lines not on this page · raw message

src/openvpn/tun.h +167 -2

196 lines not on this page · raw message

src/openvpn/win32.c +122 -0

126 lines not on this page · raw message

src/openvpn/win32.h +51 -0

62 lines not on this page · raw message

Signature · 1 line
2.17.1
#47 lore raw

PATCH v8 4/7 wintun: ring buffers based I/O

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

Implemented according to Wintun documentation and reference client code.

Wintun uses ring buffers to communicate between kernel driver and user process. Client allocates send and receive ring buffers, creates events and passes it to kernel driver under LocalSystem privileges.

When data is available for read, wintun modifies "tail" pointer of send ring and signals via event. User process reads data from "head" to "tail" and updates "head" pointer.

When user process is ready to write, it writes to receive ring, updates "tail" pointer and signals to kernel via event.

In openvpn code we add send ring's event to event loop. Before performing io wait, we compare "head" and "tail" pointers of send ring and if they're different, we skip io wait and perform read.

This also adds ring buffers support to tcp and udp server code.

---

v8:
- make DeviceIoControl result check more robust
- fix struct tun_ring layout

v7:
- fix comments (no code changes)

v6:
- added a sanity check to write_wintun() to avoid writing malformed IPv4/6 packet, which causes "ring buffer is out of capacity" error.

v5:
- fix crash at ring buffer registration on Win7 (passing NULL to DeviceIOControl, reported by kitsune1)

v4:
- added helper function tuntap_ring_empty()
- refactored event handling, got rid of separate event_ctl() call for wintun and send/receive_tail_moved members
- added wintun_ prefix for ring buffer variables
- added a comment explaining the size of wintun-specific buffers

v3:
- simplified convoluted #ifdefs
- replaced "greater than" with "greater or equal than"

v2:
- rebased on top of master

 src/openvpn/forward.c |  29 +++++++-
 src/openvpn/forward.h |  38 +++++++++-
 src/openvpn/mtcp.c    |  19 ++++-
 src/openvpn/mudp.c    |   7 +-
 src/openvpn/options.c |   4 +-
 src/openvpn/syshead.h |   1 +
 src/openvpn/tun.c     |  62 +++++++++++++++-
 src/openvpn/tun.h     | 169 +++++++++++++++++++++++++++++++++++++++++-
 src/openvpn/win32.c   | 122 ++++++++++++++++++++++++++++++
 src/openvpn/win32.h   |  50 +++++++++++++
 10 files changed, 490 insertions(+), 11 deletions(-)
src/openvpn/forward.c +28 -1
index 8451706b..6b823613 100644
@@ -1256,8 +1256,24 @@ read_incoming_tun(struct context *c)
     perf_push(PERF_READ_IN_TUN);
 
     c->c2.buf = c->c2.buffers->read_tun_buf;
+
 #ifdef _WIN32
-    read_tun_buffered(c->c1.tuntap, &c->c2.buf);
+    if (c->c1.tuntap->wintun)
+    {
+        read_wintun(c->c1.tuntap, &c->c2.buf);
+        if (c->c2.buf.len == -1)
+        {
+            register_signal(c, SIGHUP, "tun-abort");
+            c->persist.restart_sleep_seconds = 1;
+            msg(M_INFO, "Wintun read error, restarting");
+            perf_pop();
+            return;
+        }
+    }
+    else
+    {
+        read_tun_buffered(c->c1.tuntap, &c->c2.buf);
+    }
 #else
     ASSERT(buf_init(&c->c2.buf, FRAME_HEADROOM(&c->c2.frame)));
     ASSERT(buf_safe(&c->c2.buf, MAX_RW_SIZE_TUN(&c->c2.frame)));
@@ -2099,6 +2115,17 @@ io_wait_dowork(struct context *c, const unsigned int flags)
         tuntap |= EVENT_READ;
     }
 
+#ifdef _WIN32
+    if (tuntap_is_wintun(c->c1.tuntap))
+    {
+        /*
+         * With wintun we are only interested in read event. Ring buffer is
+         * always ready for write, so we don't do wait.
+         */
+        tuntap = EVENT_READ;
+    }
+#endif
+
     /*
      * Configure event wait based on socket, tuntap flags.
      */
src/openvpn/forward.h +36 -2
index 48202c07..b711ff00 100644
@@ -375,6 +375,12 @@ p2p_iow_flags(const struct context *c)
     {
         flags |= IOW_TO_TUN;
     }
+#ifdef _WIN32
+    if (tuntap_ring_empty(c->c1.tuntap))
+    {
+        flags &= ~IOW_READ_TUN;
+    }
+#endif
     return flags;
 }
 
@@ -403,8 +409,36 @@ io_wait(struct context *c, const unsigned int flags)
     }
     else
     {
-        /* slow path */
-        io_wait_dowork(c, flags);
+#ifdef _WIN32
+        bool skip_iowait = flags & IOW_TO_TUN;
+        if (flags & IOW_READ_TUN)
+        {
+            /*
+             * don't read from tun if we have pending write to link,
+             * since every tun read overwrites to_link buffer filled
+             * by previous tun read
+             */
+            skip_iowait = !(flags & IOW_TO_LINK);
+        }
+        if (tuntap_is_wintun(c->c1.tuntap) && skip_iowait)
+        {
+            unsigned int ret = 0;
+            if (flags & IOW_TO_TUN)
+            {
+                ret |= TUN_WRITE;
+            }
+            if (flags & IOW_READ_TUN)
+            {
+                ret |= TUN_READ;
+            }
+            c->c2.event_set_status = ret;
+        }
+        else
+#endif
+        {
+            /* slow path */
+            io_wait_dowork(c, flags);
+        }
     }
 }
 
src/openvpn/mtcp.c +18 -1
index abe20593..ee28a710 100644
@@ -269,8 +269,25 @@ multi_tcp_wait(const struct context *c,
                struct multi_tcp *mtcp)
 {
     int status;
+    unsigned int *persistent = &mtcp->tun_rwflags;
     socket_set_listen_persistent(c->c2.link_socket, mtcp->es, MTCP_SOCKET);
-    tun_set(c->c1.tuntap, mtcp->es, EVENT_READ, MTCP_TUN, &mtcp->tun_rwflags);
+
+#ifdef _WIN32
+    if (tuntap_is_wintun(c->c1.tuntap))
+    {
+        if (!tuntap_ring_empty(c->c1.tuntap))
+        {
+            /* there is data in wintun ring buffer, read it immediately */
+            mtcp->esr[0].arg = MTCP_TUN;
+            mtcp->esr[0].rwflags = EVENT_READ;
+            mtcp->n_esr = 1;
+            return 1;
+        }
+        persistent = NULL;
+    }
+#endif
+    tun_set(c->c1.tuntap, mtcp->es, EVENT_READ, MTCP_TUN, persistent);
+
 #ifdef ENABLE_MANAGEMENT
     if (management)
     {
src/openvpn/mudp.c +6 -1
index b7f061a2..6a29ccc8 100644
@@ -278,7 +278,12 @@ p2mp_iow_flags(const struct multi_context *m)
     {
         flags |= IOW_READ;
     }
-
+#ifdef _WIN32
+    if (tuntap_ring_empty(m->top.c1.tuntap))
+    {
+        flags &= ~IOW_READ_TUN;
+    }
+#endif
     return flags;
 }
 
src/openvpn/options.c +2 -2
index 49affc29..cebcbb07 100644
@@ -3016,10 +3016,10 @@ options_postprocess_mutate_invariant(struct options *options)
         options->ifconfig_noexec = false;
     }
 
-    /* for wintun kernel doesn't send DHCP requests, so use ipapi to set IP address and netmask */
+    /* for wintun kernel doesn't send DHCP requests, so use netsh to set IP address and netmask */
     if (options->wintun)
     {
-        options->tuntap_options.ip_win32_type = IPW32_SET_IPAPI;
+        options->tuntap_options.ip_win32_type = IPW32_SET_NETSH;
     }
 
     remap_redirect_gateway_flags(options);
src/openvpn/syshead.h +1 -0
index 899aa59e..e9accb52 100644
@@ -39,6 +39,7 @@
 #ifdef _WIN32
 #include <windows.h>
 #include <winsock2.h>
+#include <tlhelp32.h>
 #define sleep(x) Sleep((x)*1000)
 #define random rand
 #define srandom srand
src/openvpn/tun.c +60 -2

81 lines not on this page · raw message

src/openvpn/tun.h +167 -2

196 lines not on this page · raw message

src/openvpn/win32.c +122 -0

126 lines not on this page · raw message

src/openvpn/win32.h +50 -0

61 lines not on this page · raw message

Signature · 1 line
2.17.1
Acked-by on v8 4/7
#48 lore raw

Re: [Openvpn-devel] [PATCH v8 4/7] wintun: ring buffers based I/O

I have stare-reviewed the code, then run it back and forth with different config files. Works as advertised.

Note that this patch contains controversial impersonate_as_system() which we will remove or #ifdef in the patches to follow.

Best regards,
Simon

Quoted 778 lines

-----Original Message-----
From: Lev Stipakov <[email protected]...>
Sent: Tuesday, December 17, 2019 1:44 PM
To: [email protected]
Cc: Lev Stipakov <[email protected]...>
Subject: [Openvpn-devel] [PATCH v8 4/7] wintun: ring buffers based I/O

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

Implemented according to Wintun documentation and reference client code.

Wintun uses ring buffers to communicate between kernel driver and user process. Client allocates send and receive ring buffers, creates events and passes it to kernel driver under LocalSystem privileges.

When data is available for read, wintun modifies "tail" pointer of send ring and signals via event.
User process reads data from "head" to "tail" and updates "head" pointer.

When user process is ready to write, it writes to receive ring, updates "tail" pointer and signals to kernel via event.

In openvpn code we add send ring's event to event loop. Before performing io wait, we compare "head" and "tail" pointers of send ring and if they're different, we skip io wait and perform read.

This also adds ring buffers support to tcp and udp server code.

---

v8:
- make DeviceIoControl result check more robust
- fix struct tun_ring layout

v7:
- fix comments (no code changes)

v6:
- added a sanity check to write_wintun() to avoid writing malformed IPv4/6 packet, which causes "ring buffer is out of capacity" error.

v5:
- fix crash at ring buffer registration on Win7 (passing NULL to DeviceIOControl, reported by kitsune1)

v4:
- added helper function tuntap_ring_empty()
- refactored event handling, got rid of separate event_ctl() call for wintun and send/receive_tail_moved members
- added wintun_ prefix for ring buffer variables
- added a comment explaining the size of wintun-specific buffers

v3:
- simplified convoluted #ifdefs
- replaced "greater than" with "greater or equal than"

v2:
- rebased on top of master

 src/openvpn/forward.c |  29 +++++++-
 src/openvpn/forward.h |  38 +++++++++-
 src/openvpn/mtcp.c    |  19 ++++-
 src/openvpn/mudp.c    |   7 +-
 src/openvpn/options.c |   4 +-
 src/openvpn/syshead.h |   1 +
 src/openvpn/tun.c     |  62 +++++++++++++++-
 src/openvpn/tun.h     | 169 +++++++++++++++++++++++++++++++++++++++++-
 src/openvpn/win32.c   | 122 ++++++++++++++++++++++++++++++
 src/openvpn/win32.h   |  50 +++++++++++++
 10 files changed, 490 insertions(+), 11 deletions(-)
src/openvpn/forward.c index renamed from src/openvpn/forward.c +0 -0

8451706b..6b823613 100644

src/openvpn/forward.c +17 -1
@@ -1256,8 +1256,24 @@ read_incoming_tun(struct context *c)
     perf_push(PERF_READ_IN_TUN);
 
     c->c2.buf = c->c2.buffers->read_tun_buf;
+
 #ifdef _WIN32
-    read_tun_buffered(c->c1.tuntap, &c->c2.buf);
+    if (c->c1.tuntap->wintun)
+    {
+        read_wintun(c->c1.tuntap, &c->c2.buf);
+        if (c->c2.buf.len == -1)
+        {
+            register_signal(c, SIGHUP, "tun-abort");
+            c->persist.restart_sleep_seconds = 1;
+            msg(M_INFO, "Wintun read error, restarting");
+            perf_pop();
+            return;
+        }
+    }
+    else
+    {
+        read_tun_buffered(c->c1.tuntap, &c->c2.buf);
+    }
 #else
     ASSERT(buf_init(&c->c2.buf, FRAME_HEADROOM(&c->c2.frame)));
     ASSERT(buf_safe(&c->c2.buf, MAX_RW_SIZE_TUN(&c->c2.frame))); @@ -

2099,6 +2115,17 @@ io_wait_dowork(struct context *c, const unsigned int flags)

         tuntap |= EVENT_READ;
     }
src/openvpn/forward.c +5 -0
+#ifdef _WIN32
+    if (tuntap_is_wintun(c->c1.tuntap))
+    {
+        /*
+         * With wintun we are only interested in read event. Ring

buffer is

src/openvpn/forward.c +6 -0
+         * always ready for write, so we don't do wait.
+         */
+        tuntap = EVENT_READ;
+    }
+#endif
+
     /*
      * Configure event wait based on socket, tuntap flags.
      */
src/openvpn/forward.h index renamed from src/openvpn/forward.h +0 -0

48202c07..b711ff00 100644

src/openvpn/forward.h +36 -2
@@ -375,6 +375,12 @@ p2p_iow_flags(const struct context *c)
     {
         flags |= IOW_TO_TUN;
     }
+#ifdef _WIN32
+    if (tuntap_ring_empty(c->c1.tuntap))
+    {
+        flags &= ~IOW_READ_TUN;
+    }
+#endif
     return flags;
 }
 
@@ -403,8 +409,36 @@ io_wait(struct context *c, const unsigned int
 flags)
     }
     else
     {
-        /* slow path */
-        io_wait_dowork(c, flags);
+#ifdef _WIN32
+        bool skip_iowait = flags & IOW_TO_TUN;
+        if (flags & IOW_READ_TUN)
+        {
+            /*
+             * don't read from tun if we have pending write to link,
+             * since every tun read overwrites to_link buffer filled
+             * by previous tun read
+             */
+            skip_iowait = !(flags & IOW_TO_LINK);
+        }
+        if (tuntap_is_wintun(c->c1.tuntap) && skip_iowait)
+        {
+            unsigned int ret = 0;
+            if (flags & IOW_TO_TUN)
+            {
+                ret |= TUN_WRITE;
+            }
+            if (flags & IOW_READ_TUN)
+            {
+                ret |= TUN_READ;
+            }
+            c->c2.event_set_status = ret;
+        }
+        else
+#endif
+        {
+            /* slow path */
+            io_wait_dowork(c, flags);
+        }
     }
 }
src/openvpn/mtcp.c index renamed from src/openvpn/mtcp.c +0 -0

abe20593..ee28a710 100644

src/openvpn/mtcp.c +18 -1
@@ -269,8 +269,25 @@ multi_tcp_wait(const struct context *c,
                struct multi_tcp *mtcp)
 {
     int status;
+    unsigned int *persistent = &mtcp->tun_rwflags;
     socket_set_listen_persistent(c->c2.link_socket, mtcp->es,
 MTCP_SOCKET);
-    tun_set(c->c1.tuntap, mtcp->es, EVENT_READ, MTCP_TUN, &mtcp-
 >tun_rwflags);
+
+#ifdef _WIN32
+    if (tuntap_is_wintun(c->c1.tuntap))
+    {
+        if (!tuntap_ring_empty(c->c1.tuntap))
+        {
+            /* there is data in wintun ring buffer, read it immediately
 */
+            mtcp->esr[0].arg = MTCP_TUN;
+            mtcp->esr[0].rwflags = EVENT_READ;
+            mtcp->n_esr = 1;
+            return 1;
+        }
+        persistent = NULL;
+    }
+#endif
+    tun_set(c->c1.tuntap, mtcp->es, EVENT_READ, MTCP_TUN, persistent);
+
 #ifdef ENABLE_MANAGEMENT
     if (management)
     {
src/openvpn/mudp.c index renamed from src/openvpn/mudp.c +0 -0

b7f061a2..6a29ccc8 100644

src/openvpn/mudp.c +6 -1
@@ -278,7 +278,12 @@ p2mp_iow_flags(const struct multi_context *m)
     {
         flags |= IOW_READ;
     }
-
+#ifdef _WIN32
+    if (tuntap_ring_empty(m->top.c1.tuntap))
+    {
+        flags &= ~IOW_READ_TUN;
+    }
+#endif
     return flags;
 }
 
src/openvpn/options.c index renamed from src/openvpn/options.c +0 -0

49affc29..cebcbb07 100644

src/openvpn/options.c +0 -1
@@ -3016,10 +3016,10 @@ options_postprocess_mutate_invariant(struct
 options *options)
         options->ifconfig_noexec = false;
     }
 
-    /* for wintun kernel doesn't send DHCP requests, so use ipapi to

set IP address and netmask */

src/openvpn/options.c +3 -1
+    /* for wintun kernel doesn't send DHCP requests, so use netsh to
+ set IP address and netmask */
     if (options->wintun)
     {
-        options->tuntap_options.ip_win32_type = IPW32_SET_IPAPI;
+        options->tuntap_options.ip_win32_type = IPW32_SET_NETSH;
     }
 
     remap_redirect_gateway_flags(options);
src/openvpn/syshead.h index renamed from src/openvpn/syshead.h +0 -0

899aa59e..e9accb52 100644

src/openvpn/syshead.h +1 -0
@@ -39,6 +39,7 @@
 #ifdef _WIN32
 #include <windows.h>
 #include <winsock2.h>
+#include <tlhelp32.h>
 #define sleep(x) Sleep((x)*1000)
 #define random rand
 #define srandom srand
src/openvpn/tun.c index renamed from src/openvpn/tun.c +0 -0

599fd817..3f66b216 100644

src/openvpn/tun.c +20 -2
@@ -775,9 +775,27 @@ init_tun_post(struct tuntap *tt,  #ifdef _WIN32
     overlapped_io_init(&tt->reads, frame, FALSE, true);
     overlapped_io_init(&tt->writes, frame, TRUE, true);
-    tt->rw_handle.read = tt->reads.overlapped.hEvent;
-    tt->rw_handle.write = tt->writes.overlapped.hEvent;
     tt->adapter_index = TUN_ADAPTER_INDEX_INVALID;
+
+    if (tt->wintun)
+    {
+        tt->wintun_send_ring = malloc(sizeof(struct tun_ring));
+        tt->wintun_receive_ring = malloc(sizeof(struct tun_ring));
+        if ((tt->wintun_send_ring == NULL) || (tt->wintun_receive_ring
 == NULL))
+        {
+            msg(M_FATAL, "Cannot allocate memory for ring buffer");
+        }
+        ZeroMemory(tt->wintun_send_ring, sizeof(struct tun_ring));
+        ZeroMemory(tt->wintun_receive_ring, sizeof(struct tun_ring));
+
+        tt->rw_handle.read = CreateEvent(NULL, FALSE, FALSE, NULL);
+        tt->rw_handle.write = CreateEvent(NULL, FALSE, FALSE, NULL);
+    }
+    else
+    {
+        tt->rw_handle.read = tt->reads.overlapped.hEvent;
+        tt->rw_handle.write = tt->writes.overlapped.hEvent;
+    }
 #endif
 }
 
@@ -6177,6 +6195,34 @@ open_tun(const char *dev, const char *dev_type,

const char *dev_node, struct tun

             tt->ipapi_context_defined = true;
         }
     }
src/openvpn/tun.c +12 -0
+
+    if (tt->wintun)
+    {
+        if (tt->options.msg_channel)
+        {
+            /* TODO */
+        }
+        else
+        {
+            if (!impersonate_as_system())
+            {
+                msg(M_FATAL, "ERROR:  Failed to impersonate as SYSTEM,

make sure process is running under privileged account");

src/openvpn/tun.c +8 -0
+            }
+            if (!register_ring_buffers(tt->hand,
+                                       tt->wintun_send_ring,
+                                       tt->wintun_receive_ring,
+                                       tt->rw_handle.read,
+                                       tt->rw_handle.write))
+            {
+                msg(M_FATAL, "ERROR:  Failed to register ring buffers:

%lu", GetLastError());

src/openvpn/tun.c +4 -0
+            }
+            if (!RevertToSelf())
+            {
+                msg(M_FATAL, "ERROR:  RevertToSelf error: %lu",

GetLastError());

src/openvpn/tun.c +16 -0
+            }
+        }
+    }
+
     /*netcmd_semaphore_release ();*/
     gc_free(&gc);
 }
@@ -6315,6 +6361,18 @@ close_tun(struct tuntap *tt, openvpn_net_ctx_t
 *ctx)
         free(tt->actual_name);
     }
 
+    if (tt->wintun)
+    {
+        CloseHandle(tt->rw_handle.read);
+        CloseHandle(tt->rw_handle.write);
+    }
+
+    free(tt->wintun_receive_ring);
+    free(tt->wintun_send_ring);
+
+    tt->wintun_receive_ring = NULL;
+    tt->wintun_send_ring = NULL;
+
     clear_tuntap(tt);
     free(tt);
     gc_free(&gc);
src/openvpn/tun.h index renamed from src/openvpn/tun.h +0 -0

66b75d93..10f687c5 100644

src/openvpn/tun.h +120 -0
@@ -182,6 +182,9 @@ struct tuntap
 
     bool wintun; /* true if wintun is used instead of tap-windows6 */
     int standby_iter;
+
+    struct tun_ring *wintun_send_ring;
+    struct tun_ring *wintun_receive_ring;
 #else  /* ifdef _WIN32 */
     int fd; /* file descriptor for TUN/TAP dev */  #endif @@ -211,6
+214,20 @@ tuntap_defined(const struct tuntap *tt)  #endif  }
 
+#ifdef _WIN32
+inline bool
+tuntap_is_wintun(struct tuntap *tt)
+{
+    return tt && tt->wintun;
+}
+
+inline bool
+tuntap_ring_empty(struct tuntap *tt)
+{
+    return tuntap_is_wintun(tt) && (tt->wintun_send_ring->head ==
+tt->wintun_send_ring->tail); } #endif
+
 /*
  * Function prototypes
  */
@@ -478,10 +495,158 @@ read_tun_buffered(struct tuntap *tt, struct
 buffer *buf)
     return tun_finalize(tt->hand, &tt->reads, buf);  }
 
+static inline ULONG
+wintun_ring_packet_align(ULONG size)
+{
+    return (size + (WINTUN_PACKET_ALIGN - 1)) & ~(WINTUN_PACKET_ALIGN -
+1); }
+
+static inline ULONG
+wintun_ring_wrap(ULONG value)
+{
+    return value & (WINTUN_RING_CAPACITY - 1); }
+
+static inline void
+read_wintun(struct tuntap *tt, struct buffer* buf) {
+    struct tun_ring *ring = tt->wintun_send_ring;
+    ULONG head = ring->head;
+    ULONG tail = ring->tail;
+    ULONG content_len;
+    struct TUN_PACKET *packet;
+    ULONG aligned_packet_size;
+
+    *buf = tt->reads.buf_init;
+    buf->len = 0;
+
+    if ((head >= WINTUN_RING_CAPACITY) || (tail >=
 WINTUN_RING_CAPACITY))
+    {
+        msg(M_INFO, "Wintun: ring capacity exceeded");
+        buf->len = -1;
+        return;
+    }
+
+    if (head == tail)
+    {
+        /* nothing to read */
+        return;
+    }
+
+    content_len = wintun_ring_wrap(tail - head);
+    if (content_len < sizeof(struct TUN_PACKET_HEADER))
+    {
+        msg(M_INFO, "Wintun: incomplete packet header in send ring");
+        buf->len = -1;
+        return;
+    }
+
+    packet = (struct TUN_PACKET *) &ring->data[head];
+    if (packet->size > WINTUN_MAX_PACKET_SIZE)
+    {
+        msg(M_INFO, "Wintun: packet too big in send ring");
+        buf->len = -1;
+        return;
+    }
+
+    aligned_packet_size = wintun_ring_packet_align(sizeof(struct
 TUN_PACKET_HEADER) + packet->size);
+    if (aligned_packet_size > content_len)
+    {
+        msg(M_INFO, "Wintun: incomplete packet in send ring");
+        buf->len = -1;
+        return;
+    }
+
+    buf_write(buf, packet->data, packet->size);
+
+    head = wintun_ring_wrap(head + aligned_packet_size);
+    ring->head = head;
+}
+
+static inline bool
+is_ip_packet_valid(const struct buffer *buf) {
+    const struct openvpn_iphdr* ih = (const struct openvpn_iphdr
+*)BPTR(buf);
+
+    if (OPENVPN_IPH_GET_VER(ih->version_len) == 4)
+    {
+        if (BLEN(buf) < sizeof(struct openvpn_iphdr))
+        {
+            return false;
+        }
+    }
+    else if (OPENVPN_IPH_GET_VER(ih->version_len) == 6)
+    {
+        if (BLEN(buf) < sizeof(struct openvpn_ipv6hdr))
+        {
+            return false;
+        }
+    }
+    else
+    {
+        return false;
+    }
+
+    return true;
+}
+
+static inline int
+write_wintun(struct tuntap *tt, struct buffer *buf) {
+    struct tun_ring *ring = tt->wintun_receive_ring;
+    ULONG head = ring->head;
+    ULONG tail = ring->tail;
+    ULONG aligned_packet_size;
+    ULONG buf_space;
+    struct TUN_PACKET *packet;
+
+    /* wintun marks ring as corrupted (overcapacity) if it receives

invalid IP packet */

src/openvpn/tun.h +7 -0
+    if (!is_ip_packet_valid(buf))
+    {
+        msg(D_LOW, "write_wintun(): drop invalid IP packet");
+        return 0;
+    }
+
+    if ((head >= WINTUN_RING_CAPACITY) || (tail >=

WINTUN_RING_CAPACITY))

src/openvpn/tun.h +2 -0
+    {
+        msg(M_INFO, "write_wintun(): head/tail value is over

capacity");

src/openvpn/tun.h +4 -0
+        return -1;
+    }
+
+    aligned_packet_size = wintun_ring_packet_align(sizeof(struct

TUN_PACKET_HEADER) + BLEN(buf));

src/openvpn/tun.h +31 -2
+    buf_space = wintun_ring_wrap(head - tail - WINTUN_PACKET_ALIGN);
+    if (aligned_packet_size > buf_space)
+    {
+        msg(M_INFO, "write_wintun(): ring is full");
+        return 0;
+    }
+
+    /* copy packet size and data into ring */
+    packet = (struct TUN_PACKET* )&ring->data[tail];
+    packet->size = BLEN(buf);
+    memcpy(packet->data, BPTR(buf), BLEN(buf));
+
+    /* move ring tail */
+    ring->tail = wintun_ring_wrap(tail + aligned_packet_size);
+    if (ring->alertable != 0)
+    {
+        SetEvent(tt->rw_handle.write);
+    }
+
+    return BLEN(buf);
+}
+
 static inline int
 write_tun_buffered(struct tuntap *tt, struct buffer *buf)  {
-    return tun_write_win32(tt, buf);
+    if (tt->wintun)
+    {
+        return write_wintun(tt, buf);
+    }
+    else
+    {
+        return tun_write_win32(tt, buf);
+    }
 }
 
 #else  /* ifdef _WIN32 */
@@ -544,7 +709,7 @@ tun_set(struct tuntap *tt,
             }
         }
 #ifdef _WIN32
-        if (rwflags & EVENT_READ)
+        if (!tt->wintun && (rwflags & EVENT_READ))
         {
             tun_read_queue(tt, 0);
         }
src/openvpn/win32.c index renamed from src/openvpn/win32.c +0 -0

eb4c0307..24dae7d6 100644

src/openvpn/win32.c +4 -0
@@ -1493,4 +1493,126 @@ send_msg_iservice(HANDLE pipe, const void *data,
 size_t size,
     return ret;
 }
 
+bool
+impersonate_as_system()
+{
+    HANDLE thread_token, process_snapshot, winlogon_process,

winlogon_token, duplicated_token;

src/openvpn/win32.c +14 -0
+    PROCESSENTRY32 entry;
+    BOOL ret;
+    DWORD pid = 0;
+    TOKEN_PRIVILEGES privileges;
+
+    CLEAR(entry);
+    CLEAR(privileges);
+
+    entry.dwSize = sizeof(PROCESSENTRY32);
+
+    privileges.PrivilegeCount = 1;
+    privileges.Privileges->Attributes = SE_PRIVILEGE_ENABLED;
+
+    if (!LookupPrivilegeValue(NULL, SE_DEBUG_NAME,

&privileges.Privileges[0].Luid))

src/openvpn/win32.c +10 -0
+    {
+        return false;
+    }
+
+    if (!ImpersonateSelf(SecurityImpersonation))
+    {
+        return false;
+    }
+
+    if (!OpenThreadToken(GetCurrentThread(), TOKEN_ADJUST_PRIVILEGES,

FALSE, &thread_token))

src/openvpn/win32.c +5 -0
+    {
+        RevertToSelf();
+        return false;
+    }
+    if (!AdjustTokenPrivileges(thread_token, FALSE, &privileges,

sizeof(privileges), NULL, NULL))

src/openvpn/win32.c +14 -0
+    {
+        CloseHandle(thread_token);
+        RevertToSelf();
+        return false;
+    }
+    CloseHandle(thread_token);
+
+    process_snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
+    if (process_snapshot == INVALID_HANDLE_VALUE)
+    {
+        RevertToSelf();
+        return false;
+    }
+    for (ret = Process32First(process_snapshot, &entry); ret; ret =

Process32Next(process_snapshot, &entry))

src/openvpn/win32.c +15 -0
+    {
+        if (!_stricmp(entry.szExeFile, "winlogon.exe"))
+        {
+            pid = entry.th32ProcessID;
+            break;
+        }
+    }
+    CloseHandle(process_snapshot);
+    if (!pid)
+    {
+        RevertToSelf();
+        return false;
+    }
+
+    winlogon_process = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE,

pid);

src/openvpn/win32.c +7 -0
+    if (!winlogon_process)
+    {
+        RevertToSelf();
+        return false;
+    }
+
+    if (!OpenProcessToken(winlogon_process, TOKEN_IMPERSONATE |

TOKEN_DUPLICATE, &winlogon_token))

src/openvpn/win32.c +8 -0
+    {
+        CloseHandle(winlogon_process);
+        RevertToSelf();
+        return false;
+    }
+    CloseHandle(winlogon_process);
+
+    if (!DuplicateToken(winlogon_token, SecurityImpersonation,

&duplicated_token))

src/openvpn/win32.c +39 -0
+    {
+        CloseHandle(winlogon_token);
+        RevertToSelf();
+        return false;
+    }
+    CloseHandle(winlogon_token);
+
+    if (!SetThreadToken(NULL, duplicated_token))
+    {
+        CloseHandle(duplicated_token);
+        RevertToSelf();
+        return false;
+    }
+    CloseHandle(duplicated_token);
+
+    return true;
+}
+
+bool
+register_ring_buffers(HANDLE device,
+                      struct tun_ring* send_ring,
+                      struct tun_ring* receive_ring,
+                      HANDLE send_tail_moved,
+                      HANDLE receive_tail_moved) {
+    struct tun_register_rings rr;
+    BOOL res;
+    DWORD bytes_returned;
+
+    ZeroMemory(&rr, sizeof(rr));
+
+    rr.send.ring = send_ring;
+    rr.send.ring_size = sizeof(struct tun_ring);
+    rr.send.tail_moved = send_tail_moved;
+
+    rr.receive.ring = receive_ring;
+    rr.receive.ring_size = sizeof(struct tun_ring);
+    rr.receive.tail_moved = receive_tail_moved;
+
+    res = DeviceIoControl(device, TUN_IOCTL_REGISTER_RINGS, &rr,

sizeof(rr),

src/openvpn/win32.c +5 -0
+                          NULL, 0, &bytes_returned, NULL);
+
+    return res != FALSE;
+}
+
 #endif /* ifdef _WIN32 */
src/openvpn/win32.h index renamed from src/openvpn/win32.h +0 -0

4814bbc5..5fe95f47 100644

src/openvpn/win32.h +50 -1
@@ -25,6 +25,8 @@
 #ifndef OPENVPN_WIN32_H
 #define OPENVPN_WIN32_H
 
+#include <winioctl.h>
+
 #include "mtu.h"
 #include "openvpn-msg.h"
 #include "argv.h"
@@ -323,5 +325,53 @@ bool send_msg_iservice(HANDLE pipe, const void
 *data, size_t size,  int  openvpn_execve(const struct argv *a, const
 struct env_set *es, const unsigned int flags);
 
+/*
+ * Values below are taken from Wireguard Windows client
+ *
+https://github.com/WireGuard/wireguard-go/blob/master/tun/wintun/ring_w
+indows.go#L14
+ */
+#define WINTUN_RING_CAPACITY 0x800000
+#define WINTUN_RING_TRAILING_BYTES 0x10000 #define
+WINTUN_MAX_PACKET_SIZE 0xffff #define WINTUN_PACKET_ALIGN 4
+
+struct tun_ring
+{
+    volatile ULONG head;
+    volatile ULONG tail;
+    volatile LONG alertable;
+    UCHAR data[WINTUN_RING_CAPACITY + WINTUN_RING_TRAILING_BYTES]; };
+
+struct tun_register_rings
+{
+    struct
+    {
+        ULONG ring_size;
+        struct tun_ring *ring;
+        HANDLE tail_moved;
+    } send, receive;
+};
+
+struct TUN_PACKET_HEADER
+{
+    uint32_t size;
+};
+
+struct TUN_PACKET
+{
+    uint32_t size;
+    UCHAR data[WINTUN_MAX_PACKET_SIZE]; };
+
+#define TUN_IOCTL_REGISTER_RINGS CTL_CODE(51820U, 0x970U,
+METHOD_BUFFERED, FILE_READ_DATA | FILE_WRITE_DATA)
+
+bool impersonate_as_system();
+
+bool register_ring_buffers(HANDLE device,
+                           struct tun_ring *send_ring,
+                           struct tun_ring *receive_ring,
+                           HANDLE send_tail_moved,
+                           HANDLE receive_tail_moved);
+
 #endif /* ifndef OPENVPN_WIN32_H */
 #endif /* ifdef _WIN32 */
--

2.17.1

_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

#49 lore raw

[Openvpn-devel] [PATCH applied] Re: wintun: ring buffers based I/O

In reply to Lev Stipakov

Your patch has been applied to the master branch.

I have "forward-logged" Steffan's ACK on the v6 patch as the v8 is close enough, except for the "struct win_tun" change (which look reasonable), plus Simon's ACK.

What I do not like is code that uses malloc() and then calls ZeroMemory() right after. calloc() exists. Standard-based, less lines of code.

No reason to NAK this, but maybe this can be fixed in a followup patch (and put on our styleguide somewhere?)...

Compile-tested on Ubuntu 16.04 / mingw (which worked fine). No own code review or any sort of testing done.

commit d5fc4bd41616ee01816df873cb6b6567872a29e2

Author: Lev Stipakov
Date:   Tue Dec 17 14:44:10 2019 +0200
     wintun: ring buffers based I/O

     Signed-off-by: Lev Stipakov <[email protected]...>
     Acked-by: Steffan Karger <[email protected]...>
     Acked-by: Simon Rozman <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg19243.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering

#50 lore raw

Re: [Openvpn-devel] [PATCH v6 4/7] wintun: ring buffers based I/O

In reply to Lev Stipakov

Hi,

I was reluctant to review this as I do not understand the event processing in OpenVPN well enough. Now that Stefann has reviewed those bits and given an Ack, here are some comments on the rest of the code.

TLDR:
(i) stealing SYSTEM access from winlogon.exe is not a good thing to do (ii) with a small change we can support multiple tunnels and provide better diagnostics when there are no free wintun adapters -- but this could also be a follow up patch.

Quoted 221 lines · Lev Stipakov

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

Implemented according to Wintun documentation and reference client code.

Wintun uses ring buffers to communicate between kernel driver and user process. Client allocates send and receive ring buffers, creates events and passes it to kernel driver under LocalSystem privileges.

When data is available for read, wintun modifies "tail" pointer of send ring and signals via event. User process reads data from "head" to "tail" and updates "head" pointer.

When user process is ready to write, it writes to receive ring, updates "tail" pointer and signals to kernel via event.

In openvpn code we add send ring's event to event loop. Before performing io wait, we compare "head" and "tail" pointers of send ring and if they're different, we skip io wait and perform read.

This also adds ring buffers support to tcp and udp server code.

---
v6:
- added a sanity check to write_wintun() to avoid writing malformed IPv4/6 packet, which causes "ring buffer is out of capacity" error.

v5:
- fix crash at ring buffer registration on Win7 (passing NULL to DeviceIOControl, reported by kitsune1)

v4:
- added helper function tuntap_ring_empty()
- refactored event handling, got rid of separate event_ctl() call for wintun and send/receive_tail_moved members
- added wintun_ prefix for ring buffer variables
- added a comment explaining the size of wintun-specific buffers

v3:
- simplified convoluted #ifdefs
- replaced "greater than" with "greater or equal than"

v2;
- rebased on top of master

 src/openvpn/forward.c |  25 ++++++-
 src/openvpn/forward.h |  38 +++++++++-
 src/openvpn/mtcp.c    |  19 ++++-
 src/openvpn/mudp.c    |   7 +-
 src/openvpn/options.c |   4 +-
 src/openvpn/syshead.h |   1 +
 src/openvpn/tun.c     |  62 +++++++++++++++-
 src/openvpn/tun.h     | 169 +++++++++++++++++++++++++++++++++++++++++-
 src/openvpn/win32.c   | 122 ++++++++++++++++++++++++++++++
 src/openvpn/win32.h   |  51 +++++++++++++
 10 files changed, 487 insertions(+), 11 deletions(-)
src/openvpn/forward.c +24 -1
index 8451706b..3dc04a40 100644
@@ -1256,8 +1256,24 @@ read_incoming_tun(struct context *c)
     perf_push(PERF_READ_IN_TUN);
 
     c->c2.buf = c->c2.buffers->read_tun_buf;
+
 #ifdef _WIN32
-    read_tun_buffered(c->c1.tuntap, &c->c2.buf);
+    if (c->c1.tuntap->wintun)
+    {
+        read_wintun(c->c1.tuntap, &c->c2.buf);
+        if (c->c2.buf.len == -1)
+        {
+            register_signal(c, SIGHUP, "tun-abort");
+            c->persist.restart_sleep_seconds = 1;
+            msg(M_INFO, "Wintun read error, restarting");
+            perf_pop();
+            return;
+        }
+    }
+    else
+    {
+        read_tun_buffered(c->c1.tuntap, &c->c2.buf);
+    }
 #else
     ASSERT(buf_init(&c->c2.buf, FRAME_HEADROOM(&c->c2.frame)));
     ASSERT(buf_safe(&c->c2.buf, MAX_RW_SIZE_TUN(&c->c2.frame)));
@@ -2099,6 +2115,13 @@ io_wait_dowork(struct context *c, const unsigned int flags)
         tuntap |= EVENT_READ;
     }
 
+#ifdef _WIN32
+    if (tuntap_is_wintun(c->c1.tuntap))
+    {
+        tuntap = EVENT_READ;
+    }
+#endif
+
     /*
      * Configure event wait based on socket, tuntap flags.
      */
src/openvpn/forward.h +36 -2
index 48202c07..b711ff00 100644
@@ -375,6 +375,12 @@ p2p_iow_flags(const struct context *c)
     {
         flags |= IOW_TO_TUN;
     }
+#ifdef _WIN32
+    if (tuntap_ring_empty(c->c1.tuntap))
+    {
+        flags &= ~IOW_READ_TUN;
+    }
+#endif
     return flags;
 }
 
@@ -403,8 +409,36 @@ io_wait(struct context *c, const unsigned int flags)
     }
     else
     {
-        /* slow path */
-        io_wait_dowork(c, flags);
+#ifdef _WIN32
+        bool skip_iowait = flags & IOW_TO_TUN;
+        if (flags & IOW_READ_TUN)
+        {
+            /*
+             * don't read from tun if we have pending write to link,
+             * since every tun read overwrites to_link buffer filled
+             * by previous tun read
+             */
+            skip_iowait = !(flags & IOW_TO_LINK);
+        }
+        if (tuntap_is_wintun(c->c1.tuntap) && skip_iowait)
+        {
+            unsigned int ret = 0;
+            if (flags & IOW_TO_TUN)
+            {
+                ret |= TUN_WRITE;
+            }
+            if (flags & IOW_READ_TUN)
+            {
+                ret |= TUN_READ;
+            }
+            c->c2.event_set_status = ret;
+        }
+        else
+#endif
+        {
+            /* slow path */
+            io_wait_dowork(c, flags);
+        }
     }
 }
 
src/openvpn/mtcp.c +18 -1
index abe20593..30a13f73 100644
@@ -269,8 +269,25 @@ multi_tcp_wait(const struct context *c,
                struct multi_tcp *mtcp)
 {
     int status;
+    unsigned int *persistent = &mtcp->tun_rwflags;
     socket_set_listen_persistent(c->c2.link_socket, mtcp->es, MTCP_SOCKET);
-    tun_set(c->c1.tuntap, mtcp->es, EVENT_READ, MTCP_TUN, &mtcp->tun_rwflags);
+
+#ifdef _WIN32
+    if (tuntap_is_wintun(c->c1.tuntap))
+    {
+        if (!tuntap_ring_empty(c->c1.tuntap))
+        {
+            /* there is a data in wintun ring buffer, read it immediately */
+            mtcp->esr[0].arg = MTCP_TUN;
+            mtcp->esr[0].rwflags = EVENT_READ;
+            mtcp->n_esr = 1;
+            return 1;
+        }
+        persistent = NULL;
+    }
+#endif
+    tun_set(c->c1.tuntap, mtcp->es, EVENT_READ, MTCP_TUN, persistent);
+
 #ifdef ENABLE_MANAGEMENT
     if (management)
     {
src/openvpn/mudp.c +6 -1
index b7f061a2..6a29ccc8 100644
@@ -278,7 +278,12 @@ p2mp_iow_flags(const struct multi_context *m)
     {
         flags |= IOW_READ;
     }
-
+#ifdef _WIN32
+    if (tuntap_ring_empty(m->top.c1.tuntap))
+    {
+        flags &= ~IOW_READ_TUN;
+    }
+#endif
     return flags;
 }
 
src/openvpn/options.c +0 -0
index 49affc29..cebcbb07 100644

src/openvpn/options.c options_postprocess_mutate_invariant(struct options *options) · line 3022

Quoted 9 lines
src/openvpn/options.c +2 -2
@@ -3016,10 +3016,10 @@ options_postprocess_mutate_invariant(struct options *options)
         options->ifconfig_noexec = false;
     }
 
-    /* for wintun kernel doesn't send DHCP requests, so use ipapi to set IP address and netmask */
+    /* for wintun kernel doesn't send DHCP requests, so use netsh to set IP address and netmask */
     if (options->wintun)
     {
-        options->tuntap_options.ip_win32_type = IPW32_SET_IPAPI;
+        options->tuntap_options.ip_win32_type = IPW32_SET_NETSH;

This shouldn't be required. I would prefer to not use netsh for tasks where API calls are available. We know IPAPI works as we use it in the service.

Quoted 63 lines
src/openvpn/options.c +0 -0
     }
 
     remap_redirect_gateway_flags(options);
src/openvpn/syshead.h +1 -0
index 899aa59e..e9accb52 100644
@@ -39,6 +39,7 @@
 #ifdef _WIN32
 #include <windows.h>
 #include <winsock2.h>
+#include <tlhelp32.h>
 #define sleep(x) Sleep((x)*1000)
 #define random rand
 #define srandom srand
src/openvpn/tun.c +30 -2
index 599fd817..3f66b216 100644
@@ -775,9 +775,27 @@ init_tun_post(struct tuntap *tt,
 #ifdef _WIN32
     overlapped_io_init(&tt->reads, frame, FALSE, true);
     overlapped_io_init(&tt->writes, frame, TRUE, true);
-    tt->rw_handle.read = tt->reads.overlapped.hEvent;
-    tt->rw_handle.write = tt->writes.overlapped.hEvent;
     tt->adapter_index = TUN_ADAPTER_INDEX_INVALID;
+
+    if (tt->wintun)
+    {
+        tt->wintun_send_ring = malloc(sizeof(struct tun_ring));
+        tt->wintun_receive_ring = malloc(sizeof(struct tun_ring));
+        if ((tt->wintun_send_ring == NULL) || (tt->wintun_receive_ring == NULL))
+        {
+            msg(M_FATAL, "Cannot allocate memory for ring buffer");
+        }
+        ZeroMemory(tt->wintun_send_ring, sizeof(struct tun_ring));
+        ZeroMemory(tt->wintun_receive_ring, sizeof(struct tun_ring));
+
+        tt->rw_handle.read = CreateEvent(NULL, FALSE, FALSE, NULL);
+        tt->rw_handle.write = CreateEvent(NULL, FALSE, FALSE, NULL);
+    }
+    else
+    {
+        tt->rw_handle.read = tt->reads.overlapped.hEvent;
+        tt->rw_handle.write = tt->writes.overlapped.hEvent;
+    }
 #endif
 }
 
@@ -6177,6 +6195,34 @@ open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tun
             tt->ipapi_context_defined = true;
         }
     }
+
+    if (tt->wintun)
+    {
+        if (tt->options.msg_channel)
+        {
+            /* TODO */
+        }
+        else
+        {
+            if (!impersonate_as_system())

src/openvpn/tun.c open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tun · line 6217

Quoted 10 lines
src/openvpn/tun.c +10 -0
+            {
+                msg(M_FATAL, "ERROR:  Failed to impersonate as SYSTEM, make sure process is running under privileged account");
+            }
+            if (!register_ring_buffers(tt->hand,
+                                       tt->wintun_send_ring,
+                                       tt->wintun_receive_ring,
+                                       tt->rw_handle.read,
+                                       tt->rw_handle.write))
+            {
+                msg(M_FATAL, "ERROR:  Failed to register ring buffers: %lu", GetLastError());

The correct error here is, very likely, the adapter is already in use. To trigger that, why not do register_ring_buffers soon after opening the device and on failure move on to the next device (if any).

That will also allow running multiple tunnels with no further changes provided the user manually creates wintum adapters.

Quoted 239 lines
src/openvpn/tun.c +20 -0
+            }
+            if (!RevertToSelf())
+            {
+                msg(M_FATAL, "ERROR:  RevertToSelf error: %lu", GetLastError());
+            }
+        }
+    }
+
     /*netcmd_semaphore_release ();*/
     gc_free(&gc);
 }
@@ -6315,6 +6361,18 @@ close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx)
         free(tt->actual_name);
     }
 
+    if (tt->wintun)
+    {
+        CloseHandle(tt->rw_handle.read);
+        CloseHandle(tt->rw_handle.write);
+    }
+
+    free(tt->wintun_receive_ring);
+    free(tt->wintun_send_ring);
+
+    tt->wintun_receive_ring = NULL;
+    tt->wintun_send_ring = NULL;
+
     clear_tuntap(tt);
     free(tt);
     gc_free(&gc);
src/openvpn/tun.h +167 -2
index 66b75d93..10f687c5 100644
@@ -182,6 +182,9 @@ struct tuntap
 
     bool wintun; /* true if wintun is used instead of tap-windows6 */
     int standby_iter;
+
+    struct tun_ring *wintun_send_ring;
+    struct tun_ring *wintun_receive_ring;
 #else  /* ifdef _WIN32 */
     int fd; /* file descriptor for TUN/TAP dev */
 #endif
@@ -211,6 +214,20 @@ tuntap_defined(const struct tuntap *tt)
 #endif
 }
 
+#ifdef _WIN32
+inline bool
+tuntap_is_wintun(struct tuntap *tt)
+{
+    return tt && tt->wintun;
+}
+
+inline bool
+tuntap_ring_empty(struct tuntap *tt)
+{
+    return tuntap_is_wintun(tt) && (tt->wintun_send_ring->head == tt->wintun_send_ring->tail);
+}
+#endif
+
 /*
  * Function prototypes
  */
@@ -478,10 +495,158 @@ read_tun_buffered(struct tuntap *tt, struct buffer *buf)
     return tun_finalize(tt->hand, &tt->reads, buf);
 }
 
+static inline ULONG
+wintun_ring_packet_align(ULONG size)
+{
+    return (size + (WINTUN_PACKET_ALIGN - 1)) & ~(WINTUN_PACKET_ALIGN - 1);
+}
+
+static inline ULONG
+wintun_ring_wrap(ULONG value)
+{
+    return value & (WINTUN_RING_CAPACITY - 1);
+}
+
+static inline void
+read_wintun(struct tuntap *tt, struct buffer* buf)
+{
+    struct tun_ring *ring = tt->wintun_send_ring;
+    ULONG head = ring->head;
+    ULONG tail = ring->tail;
+    ULONG content_len;
+    struct TUN_PACKET *packet;
+    ULONG aligned_packet_size;
+
+    *buf = tt->reads.buf_init;
+    buf->len = 0;
+
+    if ((head >= WINTUN_RING_CAPACITY) || (tail >= WINTUN_RING_CAPACITY))
+    {
+        msg(M_INFO, "Wintun: ring capacity exceeded");
+        buf->len = -1;
+        return;
+    }
+
+    if (head == tail)
+    {
+        /* nothing to read */
+        return;
+    }
+
+    content_len = wintun_ring_wrap(tail - head);
+    if (content_len < sizeof(struct TUN_PACKET_HEADER))
+    {
+        msg(M_INFO, "Wintun: incomplete packet header in send ring");
+        buf->len = -1;
+        return;
+    }
+
+    packet = (struct TUN_PACKET *) &ring->data[head];
+    if (packet->size > WINTUN_MAX_PACKET_SIZE)
+    {
+        msg(M_INFO, "Wintun: packet too big in send ring");
+        buf->len = -1;
+        return;
+    }
+
+    aligned_packet_size = wintun_ring_packet_align(sizeof(struct TUN_PACKET_HEADER) + packet->size);
+    if (aligned_packet_size > content_len)
+    {
+        msg(M_INFO, "Wintun: incomplete packet in send ring");
+        buf->len = -1;
+        return;
+    }
+
+    buf_write(buf, packet->data, packet->size);
+
+    head = wintun_ring_wrap(head + aligned_packet_size);
+    ring->head = head;
+}
+
+static inline bool
+is_ip_packet_valid(const struct buffer *buf)
+{
+    const struct openvpn_iphdr* ih = (const struct openvpn_iphdr *)BPTR(buf);
+
+    if (OPENVPN_IPH_GET_VER(ih->version_len) == 4)
+    {
+        if (BLEN(buf) < sizeof(struct openvpn_iphdr))
+        {
+            return false;
+        }
+    }
+    else if (OPENVPN_IPH_GET_VER(ih->version_len) == 6)
+    {
+        if (BLEN(buf) < sizeof(struct openvpn_ipv6hdr))
+        {
+            return false;
+        }
+    }
+    else
+    {
+        return false;
+    }
+
+    return true;
+}
+
+static inline int
+write_wintun(struct tuntap *tt, struct buffer *buf)
+{
+    struct tun_ring *ring = tt->wintun_receive_ring;
+    ULONG head = ring->head;
+    ULONG tail = ring->tail;
+    ULONG aligned_packet_size;
+    ULONG buf_space;
+    struct TUN_PACKET *packet;
+
+    /* wintun marks ring as corrupted (overcapacity) if it receives invalid IP packet */
+    if (!is_ip_packet_valid(buf))
+    {
+        msg(D_LOW, "write_wintun(): drop invalid IP packet");
+        return 0;
+    }
+
+    if ((head >= WINTUN_RING_CAPACITY) || (tail >= WINTUN_RING_CAPACITY))
+    {
+        msg(M_INFO, "write_wintun(): head/tail value is over capacity");
+        return -1;
+    }
+
+    aligned_packet_size = wintun_ring_packet_align(sizeof(struct TUN_PACKET_HEADER) + BLEN(buf));
+    buf_space = wintun_ring_wrap(head - tail - WINTUN_PACKET_ALIGN);
+    if (aligned_packet_size > buf_space)
+    {
+        msg(M_INFO, "write_wintun(): ring is full");
+        return 0;
+    }
+
+    /* copy packet size and data into ring */
+    packet = (struct TUN_PACKET* )&ring->data[tail];
+    packet->size = BLEN(buf);
+    memcpy(packet->data, BPTR(buf), BLEN(buf));
+
+    /* move ring tail */
+    ring->tail = wintun_ring_wrap(tail + aligned_packet_size);
+    if (ring->alertable != 0)
+    {
+        SetEvent(tt->rw_handle.write);
+    }
+
+    return BLEN(buf);
+}
+
 static inline int
 write_tun_buffered(struct tuntap *tt, struct buffer *buf)
 {
-    return tun_write_win32(tt, buf);
+    if (tt->wintun)
+    {
+        return write_wintun(tt, buf);
+    }
+    else
+    {
+        return tun_write_win32(tt, buf);
+    }
 }
 
 #else  /* ifdef _WIN32 */
@@ -544,7 +709,7 @@ tun_set(struct tuntap *tt,
             }
         }
 #ifdef _WIN32
-        if (rwflags & EVENT_READ)
+        if (!tt->wintun && (rwflags & EVENT_READ))
         {
             tun_read_queue(tt, 0);
         }
src/openvpn/win32.c +0 -0
index eb4c0307..5c168b3b 100644

src/openvpn/win32.c send_msg_iservice(HANDLE pipe, const void *data, size_t size, · line 1498

Quoted 6 lines
src/openvpn/win32.c +3 -0
@@ -1493,4 +1493,126 @@ send_msg_iservice(HANDLE pipe, const void *data, size_t size,
     return ret;
 }
 
+bool
+impersonate_as_system()
+{

This is implemented by stealing the access token from winlogon.exe. I don't think such tricks belong to OpenVPN. It may also trip some anti-virus software.

That said, probably there are no "legitimate" ways of getting LOCAL SYSTEM rights on Windows without running a service.

Why does wintun require SYSTEM for using it? If there is a good reason for that, we should not let every admin user bypass it.

I would suggest to not elevate to system as the code will still work in two important cases
(i) started by automatic service
(ii) started using interactive service (e.g., through the GUI)

Those who really need to test OpenVPN with wintun from command prompt can use diagnostic tools available to get a cmd prompt as system (e.g., psexec). That also makes it explicit that SYSTEM privilege is required.

In the longer run, we could provide a script to launch openvpn.exe using the interactive service. Modifying the automatic service to use interactive service for launching looks easy to do as well. Then, all privileged operations could be removed from openvpn core.

Quoted 7 lines
src/openvpn/win32.c +7 -0
+    HANDLE thread_token, process_snapshot, winlogon_process, winlogon_token, duplicated_token;
+    PROCESSENTRY32 entry;
+    BOOL ret;
+    DWORD pid = 0;
+    TOKEN_PRIVILEGES privileges;
+
+    CLEAR(entry);

src/openvpn/win32.c send_msg_iservice(HANDLE pipe, const void *data, size_t size, · line 1515

Quoted 10 lines
src/openvpn/win32.c +10 -0
+    CLEAR(privileges);
+
+    entry.dwSize = sizeof(PROCESSENTRY32);
+
+    privileges.PrivilegeCount = 1;
+    privileges.Privileges->Attributes = SE_PRIVILEGE_ENABLED;
+
+    if (!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &privileges.Privileges[0].Luid))
+    {
+        return false;

In case we keep this function, please log such
errors. Here and several such cases below.

Quoted 162 lines
src/openvpn/win32.c +102 -0
+    }
+
+    if (!ImpersonateSelf(SecurityImpersonation))
+    {
+        return false;
+    }
+
+    if (!OpenThreadToken(GetCurrentThread(), TOKEN_ADJUST_PRIVILEGES, FALSE, &thread_token))
+    {
+        RevertToSelf();
+        return false;
+    }
+    if (!AdjustTokenPrivileges(thread_token, FALSE, &privileges, sizeof(privileges), NULL, NULL))
+    {
+        CloseHandle(thread_token);
+        RevertToSelf();
+        return false;
+    }
+    CloseHandle(thread_token);
+
+    process_snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
+    if (process_snapshot == INVALID_HANDLE_VALUE)
+    {
+        RevertToSelf();
+        return false;
+    }
+    for (ret = Process32First(process_snapshot, &entry); ret; ret = Process32Next(process_snapshot, &entry))
+    {
+        if (!_stricmp(entry.szExeFile, "winlogon.exe"))
+        {
+            pid = entry.th32ProcessID;
+            break;
+        }
+    }
+    CloseHandle(process_snapshot);
+    if (!pid)
+    {
+        RevertToSelf();
+        return false;
+    }
+
+    winlogon_process = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
+    if (!winlogon_process)
+    {
+        RevertToSelf();
+        return false;
+    }
+
+    if (!OpenProcessToken(winlogon_process, TOKEN_IMPERSONATE | TOKEN_DUPLICATE, &winlogon_token))
+    {
+        CloseHandle(winlogon_process);
+        RevertToSelf();
+        return false;
+    }
+    CloseHandle(winlogon_process);
+
+    if (!DuplicateToken(winlogon_token, SecurityImpersonation, &duplicated_token))
+    {
+        CloseHandle(winlogon_token);
+        RevertToSelf();
+        return false;
+    }
+    CloseHandle(winlogon_token);
+
+    if (!SetThreadToken(NULL, duplicated_token))
+    {
+        CloseHandle(duplicated_token);
+        RevertToSelf();
+        return false;
+    }
+    CloseHandle(duplicated_token);
+
+    return true;
+}
+
+bool
+register_ring_buffers(HANDLE device,
+                      struct tun_ring* send_ring,
+                      struct tun_ring* receive_ring,
+                      HANDLE send_tail_moved,
+                      HANDLE receive_tail_moved)
+{
+    struct tun_register_rings rr;
+    BOOL res;
+    DWORD bytes_returned;
+
+    ZeroMemory(&rr, sizeof(rr));
+
+    rr.send.ring = send_ring;
+    rr.send.ring_size = sizeof(send_ring->data);
+    rr.send.tail_moved = send_tail_moved;
+
+    rr.receive.ring = receive_ring;
+    rr.receive.ring_size = sizeof(receive_ring->data);
+    rr.receive.tail_moved = receive_tail_moved;
+
+    res = DeviceIoControl(device, TUN_IOCTL_REGISTER_RINGS, &rr, sizeof(rr),
+                          NULL, 0, &bytes_returned, NULL);
+
+    return res == TRUE;
+}
+
 #endif /* ifdef _WIN32 */
src/openvpn/win32.h +44 -0
index 4814bbc5..f6d45bdc 100644
@@ -25,6 +25,8 @@
 #ifndef OPENVPN_WIN32_H
 #define OPENVPN_WIN32_H
 
+#include <winioctl.h>
+
 #include "mtu.h"
 #include "openvpn-msg.h"
 #include "argv.h"
@@ -323,5 +325,54 @@ bool send_msg_iservice(HANDLE pipe, const void *data, size_t size,
 int
 openvpn_execve(const struct argv *a, const struct env_set *es, const unsigned int flags);
 
+/*
+ * Values below are taken from Wireguard Windows client
+ * https://github.com/WireGuard/wireguard-go/blob/master/tun/wintun/ring_windows.go#L14
+ */
+#define WINTUN_RING_CAPACITY 0x800000
+#define WINTUN_RING_TRAILING_BYTES 0x10000
+#define WINTUN_RING_FRAMING_SIZE 12
+#define WINTUN_MAX_PACKET_SIZE 0xffff
+#define WINTUN_PACKET_ALIGN 4
+
+struct tun_ring
+{
+    volatile ULONG head;
+    volatile ULONG tail;
+    volatile LONG alertable;
+    UCHAR data[WINTUN_RING_CAPACITY + WINTUN_RING_TRAILING_BYTES + WINTUN_RING_FRAMING_SIZE];
+};
+
+struct tun_register_rings
+{
+    struct
+    {
+        ULONG ring_size;
+        struct tun_ring *ring;
+        HANDLE tail_moved;
+    } send, receive;
+};
+
+struct TUN_PACKET_HEADER
+{
+    uint32_t size;
+};
+
+struct TUN_PACKET
+{
+    uint32_t size;
+    UCHAR data[WINTUN_MAX_PACKET_SIZE];
+};
+
+#define TUN_IOCTL_REGISTER_RINGS CTL_CODE(51820U, 0x970U, METHOD_BUFFERED, FILE_READ_DATA | FILE_WRITE_DATA)
+
+bool impersonate_as_system();

src/openvpn/win32.h bool send_msg_iservice(HANDLE pipe, const void *data, size_t size,

Quoted 10 lines
src/openvpn/win32.h +7 -1
+
+bool register_ring_buffers(HANDLE device,
+                           struct tun_ring *send_ring,
+                           struct tun_ring *receive_ring,
+                           HANDLE send_tail_moved,
+                           HANDLE receive_tail_moved);
+
 #endif /* ifndef OPENVPN_WIN32_H */
 #endif /* ifdef _WIN32 */
--

Selva

#51 lore raw

Re: [Openvpn-devel] [PATCH v6 4/7] wintun: ring buffers based I/O

Hi,

Thanks for looking into this. See my comments below.

TLDR:

Quoted 2 lines

(i) stealing SYSTEM access from winlogon.exe is not a good thing to do

This doesn't happen for the majority of use cases - only when iservice is not used. We also
elevate only for the single DeviceIOControl call.

Below you mentioned psexec as a one of workarounds, but I think those will make user experience worse.
Also consider the case when OpenVPN GUI is running as Administrator.

(ii) with a small change we can support multiple tunnels and provide better

Quoted 3 lines

diagnostics when there are no free wintun adapters -- but this could also be a follow up patch.

This is already implemented by Simon on top of my patches - see
https://github.com/rozmansi/openvpn/commits/wintun

Quoted 5 lines

- /* for wintun kernel doesn't send DHCP requests, so use ipapi to set
IP address and netmask */

+ /* for wintun kernel doesn't send DHCP requests, so use netsh to set
IP address and netmask */

Quoted 9 lines

src/openvpn/options.c options_postprocess_mutate_invariant(struct options *options) · line 3022

Quoted 4 lines
     if (options->wintun)
     {
src/openvpn/options.c +1 -1
-        options->tuntap_options.ip_win32_type = IPW32_SET_IPAPI;
+        options->tuntap_options.ip_win32_type = IPW32_SET_NETSH;

This shouldn't be required. I would prefer to not use netsh for tasks where API calls are available. We know IPAPI works as we use it in the service.

I remember having some issues with IPAPI which got resolved when I switched to netsh.

However, I did some testing and I cannot reproduce IPAPI specific issues anymore. Too bad I didn't investigate
it further back then. The problem I see now can also be reproduced also with netsh:

- connect with tap adapter
- kill openvpn process
- connect with wintun adapter

IPAPI / netsh fails to set IP address, since it is already assigned to another adapter.

If this is OK, I would prefer to submit a separate patch which switches back to netsh. tun.c code
has changed in follow-up patches (mostly here
https://patchwork.openvpn.net/patch/918/), those patches
are based on an assumption that wintun uses netsh.

Quoted 11 lines
Quoted 1 line

+ msg(M_FATAL, "ERROR: Failed to register ring buffers:

%lu", GetLastError());

The correct error here is, very likely, the adapter is already in use. To trigger that, why not do register_ring_buffers soon after opening the device and on failure move on to the next device (if any).

That will also allow running multiple tunnels with no further changes provided the user manually creates wintum adapters.

As mentioned before, this is already implemented by Simon:
https://github.com/rozmansi/openvpn/commit/5bc09580e8771d8422feae4504fe2a74b7412fd1

Quoted 9 lines
Quoted 1 line

+bool

+impersonate_as_system()

Quoted 1 line

+{

This is implemented by stealing the access token from winlogon.exe. I don't think such tricks belong to OpenVPN. It may also trip some anti-virus software.

Quoted 7 lines

That said, probably there are no "legitimate" ways of getting LOCAL SYSTEM rights on Windows without running a service.

Why does wintun require SYSTEM for using it? If there is a good reason for that, we should not let every admin user bypass it.

I'll defer it to Simon.

Quoted 11 lines

I would suggest to not elevate to system as the code will still work in two important cases
(i) started by automatic service
(ii) started using interactive service (e.g., through the GUI)

Those who really need to test OpenVPN with wintun from command prompt can use diagnostic tools available to get a cmd prompt as system (e.g., psexec). That also makes it explicit that SYSTEM privilege is required.

Quoted 6 lines

In the longer run, we could provide a script to launch openvpn.exe using the interactive service. Modifying the automatic service to use interactive service for launching looks easy to do as well. Then, all privileged operations could be removed from openvpn core.

I think it is good not to break user experience and allow run openvpn as an administrator without iservice using wintun at the expense on elevation to system for single API call.

In case we keep this function, please log such

Quoted 2 lines

errors. Here and several such cases below.

Sure.

Signature · 1 line
-Lev
#52 lore raw

Re: [Openvpn-devel] [PATCH v6 4/7] wintun: ring buffers based I/O

Hi

Quoted 11 lines · Lev Stipakov

Hi,

Thanks for looking into this. See my comments below.

Quoted 2 lines

TLDR:
(i) stealing SYSTEM access from winlogon.exe is not a good thing to do

This doesn't happen for the majority of use cases - only when iservice is not used. We also elevate only for the single DeviceIOControl call.

I understand. But stealing access token from winlogon.exe is a hack and not something I would expect to see in a trustworthy executable. Diagnostic and forensic tools may be justified in doing such things.

Quoted 3 lines

Below you mentioned psexec as a one of workarounds, but I think those will make user experience worse.

I was not suggesting it as an approach for "users" -- only for testing or for those
who insist running OpenVPN from command line.

Quoted 1 line

Also consider the case when OpenVPN GUI is running as Administrator.

That would be a wrong way of using the GUI in 2.4 + releases. We need not and should not (IMO) support the GUI running as admin. That said, now that vista is no longer supported we can enable the use of iservice with GUI running as admin.

Quoted 89 lines
Quoted 4 lines

(ii) with a small change we can support multiple tunnels and provide better diagnostics when there are no free wintun adapters -- but this could also be a follow up patch.

This is already implemented by Simon on top of my patches - see https://github.com/rozmansi/openvpn/commits/wintun

Quoted 11 lines

src/openvpn/options.c options_postprocess_mutate_invariant(struct options *options) · line 3022

Quoted 7 lines
src/openvpn/options.c +2 -2
-    /* for wintun kernel doesn't send DHCP requests, so use ipapi to set IP address and netmask */
 
+    /* for wintun kernel doesn't send DHCP requests, so use netsh to set IP address and netmask */
     if (options->wintun)
     {
-        options->tuntap_options.ip_win32_type = IPW32_SET_IPAPI;
+        options->tuntap_options.ip_win32_type = IPW32_SET_NETSH;

This shouldn't be required. I would prefer to not use netsh for tasks where API calls are available. We know IPAPI works as we use it in the service.

I remember having some issues with IPAPI which got resolved when I switched to netsh.

However, I did some testing and I cannot reproduce IPAPI specific issues anymore. Too bad I didn't investigate it further back then. The problem I see now can also be reproduced also with netsh:

- connect with tap adapter
- kill openvpn process
- connect with wintun adapter

IPAPI / netsh fails to set IP address, since it is already assigned to another adapter.

If this is OK, I would prefer to submit a separate patch which switches back to netsh. tun.c code has changed in follow-up patches (mostly here https://patchwork.openvpn.net/patch/918/), those patches are based on an assumption that wintun uses netsh.

Quoted 10 lines
Quoted 2 lines

+ msg(M_FATAL, "ERROR: Failed to register ring buffers: %lu", GetLastError());

The correct error here is, very likely, the adapter is already in use. To trigger that, why not do register_ring_buffers soon after opening the device and on failure move on to the next device (if any).

That will also allow running multiple tunnels with no further changes provided the user manually creates wintum adapters.

As mentioned before, this is already implemented by Simon: https://github.com/rozmansi/openvpn/commit/5bc09580e8771d8422feae4504fe2a74b7412fd1

Quoted 10 lines

src/openvpn/win32.c send_msg_iservice(HANDLE pipe, const void *data, size_t size, · line 1498

Quoted 5 lines
src/openvpn/win32.c +3 -0
+bool
 
+impersonate_as_system()
+{

This is implemented by stealing the access token from winlogon.exe. I don't think such tricks belong to OpenVPN. It may also trip some anti-virus software.

Quoted 6 lines

That said, probably there are no "legitimate" ways of getting LOCAL SYSTEM rights on Windows without running a service.

Why does wintun require SYSTEM for using it? If there is a good reason for that, we should not let every admin user bypass it.

I'll defer it to Simon.

Quoted 12 lines

I would suggest to not elevate to system as the code will still work in two important cases
(i) started by automatic service
(ii) started using interactive service (e.g., through the GUI)

Those who really need to test OpenVPN with wintun from command prompt can use diagnostic tools available to get a cmd prompt as system (e.g., psexec). That also makes it explicit that SYSTEM privilege is required.

Quoted 5 lines

In the longer run, we could provide a script to launch openvpn.exe using the interactive service. Modifying the automatic service to use interactive service for launching looks easy to do as well. Then, all privileged operations could be removed from openvpn core.

Quoted 3 lines

I think it is good not to break user experience and allow run openvpn as an administrator without iservice using wintun at the expense on elevation to system for single API call.

I have already said what I think of it. As an admin I wouldn't like to see users running processes that elevate to SYSTEM like this.

Selva

#53 lore raw

Re: [Openvpn-devel] [PATCH v6 4/7] wintun: ring buffers based I/O

Quoted 4 lines

I have already said what I think of it. As an admin I wouldn't like to see users running processes that elevate to SYSTEM like this.

Would it be too much if

- openvpn.exe process detects that it is not started by interactive service
- interactive service process is running
- wintun is used as --windows-driver

then openvpn behaves as a command-line version of openvpn-gui - connects to service, which
runs openvpn.exe, and then prints output, received via management interface, to console?

Signature · 1 line
-Lev
#54 lore raw

Re: [Openvpn-devel] [PATCH v6 4/7] wintun: ring buffers based I/O

Hi

Quoted 11 lines · Lev Stipakov
Quoted 3 lines

I have already said what I think of it. As an admin I wouldn't like to see users running processes that elevate to SYSTEM like this.

Would it be too much if

- openvpn.exe process detects that it is not started by interactive service
- interactive service process is running
- wintun is used as --windows-driver

I think this is possible and in fact I had something like this in mind as well I wrote we could provide an openvpn launcher script.

And I would like to do this even if wintun is in use as ability to run openvpn.exe as a regular would be a plus for everyone.

Quoted 2 lines

then openvpn behaves as a command-line version of openvpn-gui - connects to service, which runs openvpn.exe, and then prints output, received via management interface, to console?

It may be better to redirect the output to the stdout or file as usual so that no
user visible changes and easier to implement too. Just keep the process id returned from the service and when the user kills the front-end, terminate the background process started by the service too.

This should also allow to run the automatic service as LocalService or a special service user as many services do.

Selva

Quoted 3 lines

--
-Lev

#55 lore raw

Re: [Openvpn-devel] [PATCH v6 4/7] wintun: ring buffers based I/O

In reply to Selva Nair

Hi,

Quoted 10 lines
Quoted 6 lines
Quoted 2 lines

TLDR:
(i) stealing SYSTEM access from winlogon.exe is not a good thing to do

This doesn't happen for the majority of use cases - only when iservice is not used. We also elevate only for the single DeviceIOControl call.

I understand. But stealing access token from winlogon.exe is a hack and not something I would expect to see in a trustworthy executable. Diagnostic and forensic tools may be justified in doing such things.

Wintun has a hardcoded check to allow ring registration from the SYSTEM user only.

To be honest: I am using a Windows 10 VPC in test mode with a modified Wintun driver installed that also allows ring registration for the members of Administrators group. This allows me to quickly test ideas while reviewing/upgrading Lev's work. I can run Visual Studio to compile openvpn.exe on my computer as an unprivileged user. Then have an elevated Remote Debugger running on the VPC and Visual Studio to remotely run openvpn.exe with debugger attached with a single F5 click.

Having to use OpenVPN GUI and iservice, or running openvpn.exe as service would require a lot more clicks to replace the binary, attach debugger etc.

As far as I am concerned, this elevation hack may be removed from the OpenVPN source code in the final release. However, mind that this would prevent people from running openvpn.exe+Wintun from the command line.

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

+bool

src/openvpn/win32.c send_msg_iservice(HANDLE pipe, const void *data, size_t size, · line 1498

Quoted 2 lines
src/openvpn/win32.c +2 -0
+impersonate_as_system()
+{

This is implemented by stealing the access token from winlogon.exe. I don't think such tricks belong to OpenVPN. It may also trip some anti-virus software.

That said, probably there are no "legitimate" ways of getting LOCAL SYSTEM rights on Windows without running a service.

Quoted 6 lines
Quoted 6 lines
Quoted 3 lines

Why does wintun require SYSTEM for using it? If there is a good reason for that, we should not let every admin user bypass it.

I'll defer it to Simon.

Unfortunately, I don't do security decisions in Wintun.

Wintun was originally designed for WireGuard. WireGuard is architectured to run all its tunnels as Windows services running as the SYSTEM user. Wintun's security is as tight as possible so the WireGuard can barely use it. I know a guy who is tempted to introduce a userspace binary code signature check to the Wintun. :)

Given the relative ease to get SYSTEM token just by being an elevated process — mind there's also a hack to get from non-elevated to elevated completely bypassing the UAC prompt as long as you are a member of Administrators — this SYSTEM restriction really doesn't provide considerable additional security compared to being a member of Administrators group.

Quoted 12 lines
Quoted 12 lines
Quoted 10 lines

Those who really need to test OpenVPN with wintun from command prompt can use diagnostic tools available to get a cmd prompt as system (e.g., psexec). That also makes it explicit that SYSTEM privilege is required.

In the longer run, we could provide a script to launch openvpn.exe using the interactive service. Modifying the automatic service to use interactive service for launching looks easy to do as well. Then, all privileged operations could be removed from openvpn core.

Quoted 6 lines
Quoted 3 lines

I think it is good not to break user experience and allow run openvpn as an administrator without iservice using wintun at the expense on elevation to system for single API call.

I have already said what I think of it. As an admin I wouldn't like to see users running processes that elevate to SYSTEM like this.

Selva, Windows is full of such hacks internally. :( This is no excuse for us doing the same of course. Just saying Windows is far from ideal world.

I am running OpenVPN on Windows using NSSM wrapper for years. I had a brief discussion on the Hackathon with Samuli about integrating SCM support directly into openvpn.exe (imagine --daemon for Windows):

sc create OpenVPN$MyTunnel binpath= "C:\Program Files\OpenVPN\bin\openvpn" --daemon --config "C:\Program Files\OpenVPN\config\MyTunnel.ovpn" --log "C:\Program Files\OpenVPN\log\MyTunnel.log" start= auto depend= dhcp sc start OpenVPN$MyTunnel

This would install openvpn.exe as a Windows service and run it as the SYSTEM user — no need for iservice, no need for SYSTEM token hack. Other than me, perhaps it could cover at least some of the users now running openvpn.exe directly.

Regards,
Simon

#56 lore raw

Re: [Openvpn-devel] [PATCH v6 4/7] wintun: ring buffers based I/O

Hi,

Quoted 44 lines · Simon Rozman

Hi,

TLDR:
(i) stealing SYSTEM access from winlogon.exe is not a good thing to do

This doesn't happen for the majority of use cases - only when iservice is not used. We also elevate only for the single DeviceIOControl call.

I understand. But stealing access token from winlogon.exe is a hack and not something I would expect to see in a trustworthy executable. Diagnostic and forensic tools may be justified in doing such things.

Wintun has a hardcoded check to allow ring registration from the SYSTEM user only.

To be honest: I am using a Windows 10 VPC in test mode with a modified Wintun driver installed that also allows ring registration for the members of Administrators group. This allows me to quickly test ideas while reviewing/upgrading Lev's work. I can run Visual Studio to compile openvpn.exe on my computer as an unprivileged user. Then have an elevated Remote Debugger running on the VPC and Visual Studio to remotely run openvpn.exe with debugger attached with a single F5 click.

Having to use OpenVPN GUI and iservice, or running openvpn.exe as service would require a lot more clicks to replace the binary, attach debugger etc.

As far as I am concerned, this elevation hack may be removed from the OpenVPN source code in the final release. However, mind that this would prevent people from running openvpn.exe+Wintun from the command line.

src/openvpn/win32.c +3 -0
+bool
 
+impersonate_as_system()
+{

This is implemented by stealing the access token from winlogon.exe. I don't think such tricks belong to OpenVPN. It may also trip some anti-virus software.

That said, probably there are no "legitimate" ways of getting LOCAL SYSTEM rights on Windows without running a service.

Why does wintun require SYSTEM for using it? If there is a good reason for that, we should not let every admin user bypass it.

Quoted 8 lines

I'll defer it to Simon.

Unfortunately, I don't do security decisions in Wintun.

Wintun was originally designed for WireGuard. WireGuard is architectured to run all its tunnels as Windows services running as the SYSTEM user. Wintun's security is as tight as possible so the WireGuard can barely use it. I know a guy who is tempted to introduce a userspace binary code signature check to the Wintun. :)

Given the relative ease to get SYSTEM token just by being an elevated process — mind there's also a hack to get from non-elevated to elevated completely bypassing the UAC prompt as long as you are a member of Administrators — this SYSTEM restriction really doesn't provide considerable additional security compared to being a member of Administrators group.

That's the point. It probably provides no extra security because we are forced to take the blame for using hacks to cross security boundaries. And, bypassing UAC is quite different from acquiring rights you were not supposed to have. Does openvpn wants to be doing that?

Quoted 19 lines

Those who really need to test OpenVPN with wintun from command prompt can use diagnostic tools available to get a cmd prompt as system (e.g., psexec). That also makes it explicit that SYSTEM privilege is required.

In the longer run, we could provide a script to launch openvpn.exe using the interactive service. Modifying the automatic service to use interactive service for launching looks easy to do as well. Then, all privileged operations could be removed from openvpn core.

I think it is good not to break user experience and allow run openvpn as an administrator without iservice using wintun at the expense on elevation to system for single API call.

Quoted 5 lines

I have already said what I think of it. As an admin I wouldn't like to see users running processes that elevate to SYSTEM like this.

Selva, Windows is full of such hacks internally. :( This is no excuse for us doing the same of course. Just saying Windows is far from ideal world.

In this particular case this has nothing to do with Windows -- its just a stupid decision by wintun that requires the use of undocumented hacks to elevate to SYSTEM.

Quoted 7 lines

I am running OpenVPN on Windows using NSSM wrapper for years. I had a brief discussion on the Hackathon with Samuli about integrating SCM support directly into openvpn.exe (imagine --daemon for Windows):

sc create OpenVPN$MyTunnel binpath= "C:\Program Files\OpenVPN\bin\openvpn" --daemon --config "C:\Program Files\OpenVPN\config\MyTunnel.ovpn" --log "C:\Program Files\OpenVPN\log\MyTunnel.log" start= auto depend= dhcp sc start OpenVPN$MyTunnel

This would install openvpn.exe as a Windows service and run it as the SYSTEM user — no need for iservice, no need for SYSTEM token hack. Other than me, perhaps it could cover at least some of the users now running openvpn.exe directly.

This is not the direction I want to see us moving towards. Instead I want to see us daemonizing OpenVPN.exe as LOCAL SERVICE or a custom service user and delegate privileged operations to a separate service running as SYSTEM. And we already have the latter: interactive service. So, not even admin rights is needed in openvpn.exe, let alone SYSTEM.

IMO, the right approach on Windows is to run a bare minimal code as a service to get SYSTEM rights and the rest with limited privileges.

Selva

#57 lore raw

Re: [Openvpn-devel] [PATCH v6 4/7] wintun: ring buffers based I/O

How about compromise - let's add "--enable-system-elevation" windows specific option.

- When it is set, we print warning and elevate to SYSTEM for the single
DeviceIOControl call

- When it is not set and wintun is used, we run openvpn from command line
via iservice

- If service is missing, we print error "either use iservice or
--enable-system-elevation (experts only)"

The ones who want to run openvpn from command line with wintun still could do it via psexec and it is
assumed that they know what they are doing. So why not make it simpler and add a option
"yes I know what I am doing and I am willing to take risks"? Also, it is safer to use system privilege
just for single call rather than run whole process with it.

This also will make debugging / development easier.

ti 17. jouluk. 2019 klo 0.38 Selva Nair ([email protected]...) kirjoitti:

Quoted 145 lines

Hi,

Quoted 9 lines · Simon Rozman

Hi,

TLDR:
(i) stealing SYSTEM access from winlogon.exe is not a good thing to do

This doesn't happen for the majority of use cases - only when iservice

is not used. We also

Quoted 4 lines

elevate only for the single DeviceIOControl call.

I understand. But stealing access token from winlogon.exe is a hack and

not

Quoted 5 lines

something I would expect to see in a trustworthy executable. Diagnostic and forensic tools may be justified in doing such things.

Wintun has a hardcoded check to allow ring registration from the SYSTEM

user only.

Quoted 2 lines

To be honest: I am using a Windows 10 VPC in test mode with a modified

Wintun driver installed that also allows ring registration for the members of Administrators group. This allows me to quickly test ideas while reviewing/upgrading Lev's work. I can run Visual Studio to compile openvpn.exe on my computer as an unprivileged user. Then have an elevated Remote Debugger running on the VPC and Visual Studio to remotely run openvpn.exe with debugger attached with a single F5 click.

Quoted 2 lines

Having to use OpenVPN GUI and iservice, or running openvpn.exe as

service would require a lot more clicks to replace the binary, attach debugger etc.

Quoted 2 lines

As far as I am concerned, this elevation hack may be removed from the

OpenVPN source code in the final release. However, mind that this would prevent people from running openvpn.exe+Wintun from the command line.

Quoted 20 lines
src/openvpn/win32.c +3 -0
+bool
 
+impersonate_as_system()
+{

This is implemented by stealing the access token from winlogon.exe. I don't think such tricks belong to OpenVPN. It may also trip some anti-virus software.

That said, probably there are no "legitimate" ways of getting LOCAL SYSTEM rights on Windows without running a service.

Why does wintun require SYSTEM for using it? If there is a good reason for that, we should not let every admin user bypass it.

Quoted 6 lines

I'll defer it to Simon.

Unfortunately, I don't do security decisions in Wintun.

Wintun was originally designed for WireGuard. WireGuard is architectured

to run all its tunnels as Windows services running as the SYSTEM user. Wintun's security is as tight as possible so the WireGuard can barely use it. I know a guy who is tempted to introduce a userspace binary code signature check to the Wintun. :)

Quoted 2 lines

Given the relative ease to get SYSTEM token just by being an elevated

process — mind there's also a hack to get from non-elevated to elevated completely bypassing the UAC prompt as long as you are a member of Administrators — this SYSTEM restriction really doesn't provide considerable additional security compared to being a member of Administrators group.

That's the point. It probably provides no extra security because we are forced to take the blame for using hacks to cross security boundaries. And, bypassing UAC is quite different from acquiring rights you were not supposed to have. Does openvpn wants to be doing that?

Quoted 14 lines

Those who really need to test OpenVPN with wintun from command prompt can use diagnostic tools available to get a cmd prompt as system (e.g., psexec). That also makes it explicit that SYSTEM privilege is required.

In the longer run, we could provide a script to launch openvpn.exe using the interactive service. Modifying the automatic service to use interactive service for launching looks easy to do as well. Then, all privileged operations could be removed from openvpn core.

Quoted 2 lines

I think it is good not to break user experience and allow run openvpn as an administrator without iservice using wintun at the expense on

elevation

Quoted 4 lines

to system for single API call.

I have already said what I think of it. As an admin I wouldn't like to

see

Quoted 4 lines

users running processes that elevate to SYSTEM like this.

Selva, Windows is full of such hacks internally. :( This is no excuse

for us doing the same of course. Just saying Windows is far from ideal world.

In this particular case this has nothing to do with Windows -- its just a stupid decision by wintun that requires the use of undocumented hacks to elevate to SYSTEM.

Quoted 2 lines

I am running OpenVPN on Windows using NSSM wrapper for years. I had a

brief discussion on the Hackathon with Samuli about integrating SCM support directly into openvpn.exe (imagine --daemon for Windows):

Quoted 2 lines

sc create OpenVPN$MyTunnel binpath= "C:\Program

Files\OpenVPN\bin\openvpn" --daemon --config "C:\Program Files\OpenVPN\config\MyTunnel.ovpn" --log "C:\Program Files\OpenVPN\log\MyTunnel.log" start= auto depend= dhcp

Quoted 3 lines

sc start OpenVPN$MyTunnel

This would install openvpn.exe as a Windows service and run it as the

SYSTEM user — no need for iservice, no need for SYSTEM token hack. Other than me, perhaps it could cover at least some of the users now running openvpn.exe directly.

This is not the direction I want to see us moving towards. Instead I want to see us daemonizing OpenVPN.exe as LOCAL SERVICE or a custom service user and delegate privileged operations to a separate service running as SYSTEM. And we already have the latter: interactive service. So, not even admin rights is needed in openvpn.exe, let alone SYSTEM.

IMO, the right approach on Windows is to run a bare minimal code as a service to get SYSTEM rights and the rest with limited privileges.

Selva

Signature · 1 line
-Lev
#58 lore raw

Re: [Openvpn-devel] [PATCH v6 4/7] wintun: ring buffers based I/O

Hi,

Lev, unfortunately, the openvpn.exe binary will still contain this hack's machine code and might really rise some eyebrows with anti-virus software.

Before the final release the entire SYSTEM token hack should be removed from the OpenVPN source.

I think it's okay to make a note in README that "--windows-driver wintun" works with iservice only. (Or, if user is responsible to run openvpn.exe as SYSTEM user himself somehow.)

Regards,

Simon

From: Lev Stipakov <[email protected]...>
Sent: Tuesday, December 17, 2019 10:35 AM
To: Selva Nair <[email protected]...>
Cc: Simon Rozman <[email protected]...>; Lev Stipakov <[email protected]...>; openvpn-devel <[email protected]>
Subject: Re: [Openvpn-devel] [PATCH v6 4/7] wintun: ring buffers based I/O

How about compromise - let's add "--enable-system-elevation" windows specific option.

- When it is set, we print warning and elevate to SYSTEM for the single
DeviceIOControl call

- When it is not set and wintun is used, we run openvpn from command line via
iservice

- If service is missing, we print error "either use iservice
or --enable-system-elevation (experts only)"

The ones who want to run openvpn from command line with wintun still could do it via psexec and it is

assumed that they know what they are doing. So why not make it simpler and add a option

"yes I know what I am doing and I am willing to take risks"? Also, it is safer to use system privilege

just for single call rather than run whole process with it.

This also will make debugging / development easier.

#59 lore raw

Re: [Openvpn-devel] [PATCH v6 4/7] wintun: ring buffers based I/O

I don't want to give up :)

Let's at least wrap that code inside #define ENABLE_SYSTEM_ELEVATION (because we don't have enough defines) to not to complicate development / debugging.

ti 17. jouluk. 2019 klo 13.18 Simon Rozman ([email protected]...) kirjoitti:

Quoted 71 lines

Hi,

Lev, unfortunately, the openvpn.exe binary will still contain this hack's machine code and might really rise some eyebrows with anti-virus software.

Before the final release the entire SYSTEM token hack should be removed from the OpenVPN source.

I think it's okay to make a note in README that "--windows-driver wintun" works with iservice only. (Or, if user is responsible to run openvpn.exe as SYSTEM user himself somehow.)

Regards,

Simon

*From:* Lev Stipakov <[email protected]...>
*Sent:* Tuesday, December 17, 2019 10:35 AM
*To:* Selva Nair <[email protected]...>
*Cc:* Simon Rozman <[email protected]...>; Lev Stipakov <[email protected]...>; openvpn-devel <[email protected]> *Subject:* Re: [Openvpn-devel] [PATCH v6 4/7] wintun: ring buffers based I/O

How about compromise - let's add "--enable-system-elevation" windows specific option.

- When it is set, we print warning and elevate to SYSTEM for the single
DeviceIOControl call

- When it is not set and wintun is used, we run openvpn from command line
via iservice

- If service is missing, we print error "either use iservice or
--enable-system-elevation (experts only)"

The ones who want to run openvpn from command line with wintun still could do it via psexec and it is

assumed that they know what they are doing. So why not make it simpler and add a option

"yes I know what I am doing and I am willing to take risks"? Also, it is safer to use system privilege

just for single call rather than run whole process with it.

This also will make debugging / development easier.

Signature · 1 line
-Lev
#60 lore raw

Re: [Openvpn-devel] [PATCH v6 4/7] wintun: ring buffers based I/O

In reply to Selva Nair

Hi,

Quoted 15 lines
Quoted 1 line

I am running OpenVPN on Windows using NSSM wrapper for years. I had a

brief discussion on the Hackathon with Samuli about integrating SCM support directly into openvpn.exe (imagine --daemon for Windows):

Quoted 8 lines

sc create OpenVPN$MyTunnel binpath= "C:\Program Files\OpenVPN\bin\openvpn" --daemon --config "C:\Program Files\OpenVPN\config\MyTunnel.ovpn" --log "C:\Program Files\OpenVPN\log\MyTunnel.log" start= auto depend= dhcp sc start OpenVPN$MyTunnel

This would install openvpn.exe as a Windows service and run it as the

SYSTEM user — no need for iservice, no need for SYSTEM token hack. Other than me, perhaps it could cover at least some of the users now running openvpn.exe directly.

Quoted 8 lines

This is not the direction I want to see us moving towards. Instead I want to see us daemonizing OpenVPN.exe as LOCAL SERVICE or a custom service user and delegate privileged operations to a separate service running as SYSTEM. And we already have the latter: interactive service. So, not even admin rights is needed in openvpn.exe, let alone SYSTEM.

IMO, the right approach on Windows is to run a bare minimal code as a service to get SYSTEM rights and the rest with limited privileges.

Selva, those are two different use-cases. And none is "right" or "wrong". OpenVPN can or should have both. :)

1. I need to run VPN tunnel as a persistent service - something that comes up with computer (Group Policy Client service waits for about 30 seconds on boot to get network access to AD server). And stays on all the time - any user signed in or not. I connect computers with VPN.

2. You need an openvpn.exe (or introduce some openvpn-tui.exe) to be a command line version of openvpn-gui.exe. Something to be run by a regular user or a batch script in an unprivileged context. You connect users with VPN.

If we implement both, we can obsolete SYSTEM token hack completely.

I have been playing with Lev's patches for the past few days. Tested them, debugged them, did some fixes. There are things to be desired like netsh=>ipcfg, remove or #ifdef the SYSTEM token hack... But those are design choices we should pursue in the future. I believe patches are mature enough to ack them. They should be merged into master to provide wider testing and easier development progress.

Regards,
Simon

#61 lore raw

Re: [Openvpn-devel] [PATCH v6 4/7] wintun: ring buffers based I/O

Hi Simon,

A quick reply:

Quoted 6 lines
Quoted 2 lines

IMO, the right approach on Windows is to run a bare minimal code as a service to get SYSTEM rights and the rest with limited privileges.

Selva, those are two different use-cases. And none is "right" or "wrong". OpenVPN can or should have both. :)

1. I need to run VPN tunnel as a persistent service - something that comes up with computer (Group Policy Client service waits for about 30 seconds on boot to get network access to AD server). And stays on all the time - any user signed in or not. I connect computers with VPN.

I too use OpenVPN like this, so I do understand the use case. And, the point was that the exe can be started through interactive service even in this case. That would allow running openvpn.exe at boot from a service with low privileges that delegates all privileged actions to iservice. Years ago when iservice was introduced we did briefly discuss this (with Heiko) but left it as a future enhancement which of course no one had time for.

Unless I'm missing some scenario where this wont work. Anyway, this is beyond the scope of the current patch.

Selva

#62 lore raw

Re: [Openvpn-devel] [PATCH v6 4/7] wintun: ring buffers based I/O

In reply to Simon Rozman

Hi

Quoted 2 lines · Simon Rozman

I have been playing with Lev's patches for the past few days. Tested them, debugged them, did some fixes. There are things to be desired like netsh=>ipcfg, remove or #ifdef the SYSTEM token hack... But those are design choices we should pursue in the future. I believe patches are mature enough to ack them. They should be merged into master to provide wider testing and easier development progress.

I agree. And, if we wont release official binaries with the system hack, the patch looks good to me too.

Selva

#63 lore raw

PATCH v2 5/7 wintun: interactive service support

In reply to Lev Stipakov

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

Wintun requires ring buffers registration to be
performed by privileged process. In order to use
openvpn with wintun by non-Administrator, we
need to use interactive service and shared memory
to register buffers.

Openvpn process creates memory mapping object and event for send and receive ring and passes handles to interactive service. There handles are duplicated and memory mapped object is mapped into the address space of service process. Then address of mapped view and event handle is passed to wintun kernel driver.

After interactive service preformed registration, openvpn process maps memory mapped object into own address space. Thus mapped views in openvpn and service process represent the same memory region.

---
 include/openvpn-msg.h                       |  10 ++
 src/openvpn/Makefile.am                     |   2 +-
 src/openvpn/openvpn.vcxproj                 |   2 +
 src/openvpn/openvpn.vcxproj.filters         |   8 +-
 src/openvpn/ring_buffer.c                   |  54 +++++++++++
 src/openvpn/ring_buffer.h                   |  79 ++++++++++++++++
 src/openvpn/tun.c                           |  89 +++++++++++++++---
 src/openvpn/tun.h                           |   3 +
 src/openvpn/win32.c                         |  25 -----
 src/openvpn/win32.h                         |  43 ---------
 src/openvpnserv/Makefile.am                 |   3 +-
 src/openvpnserv/interactive.c               | 141 ++++++++++++++++++++++++++--
 src/openvpnserv/openvpnserv.vcxproj         |   2 +
 src/openvpnserv/openvpnserv.vcxproj.filters |   6 ++
 14 files changed, 375 insertions(+), 92 deletions(-)
 create mode 100644 src/openvpn/ring_buffer.c
 create mode 100644 src/openvpn/ring_buffer.h
include/openvpn-msg.h +10 -0
index 66177a2..3ed6206 100644
@@ -39,6 +39,7 @@ typedef enum {
     msg_del_block_dns,
     msg_register_dns,
     msg_enable_dhcp,
+    msg_register_ring_buffers
 } message_type_t;
 
 typedef struct {
@@ -117,4 +118,13 @@ typedef struct {
     interface_t iface;
 } enable_dhcp_message_t;
 
+typedef struct {
+    message_header_t header;
+    HANDLE device;
+    HANDLE send_ring_handle;
+    HANDLE receive_ring_handle;
+    HANDLE send_tail_moved;
+    HANDLE receive_tail_moved;
+} register_ring_buffers_message_t;
+
 #endif /* ifndef OPENVPN_MSG_H_ */
src/openvpn/Makefile.am +1 -1
index a091ffc..d1bb99c 100644
@@ -138,6 +138,6 @@ openvpn_LDADD = \
 	$(OPTIONAL_SYSTEMD_LIBS) \
 	$(OPTIONAL_DL_LIBS)
 if WIN32
-openvpn_SOURCES += openvpn_win32_resources.rc block_dns.c block_dns.h
+openvpn_SOURCES += openvpn_win32_resources.rc block_dns.c block_dns.h ring_buffer.c ring_buffer.h
 openvpn_LDADD += -lgdi32 -lws2_32 -lwininet -lcrypt32 -liphlpapi -lwinmm -lfwpuclnt -lrpcrt4 -lncrypt -lsetupapi
 endif
src/openvpn/openvpn.vcxproj +2 -0
index 9ffef9f..61e634e 100644
@@ -181,6 +181,7 @@
     <ClCompile Include="ps.c" />
     <ClCompile Include="push.c" />
     <ClCompile Include="reliable.c" />
+    <ClCompile Include="ring_buffer.c" />
     <ClCompile Include="route.c" />
     <ClCompile Include="run_command.c" />
     <ClCompile Include="schedule.c" />
@@ -264,6 +265,7 @@
     <ClInclude Include="push.h" />
     <ClInclude Include="pushlist.h" />
     <ClInclude Include="reliable.h" />
+    <ClInclude Include="ring_buffer.h" />
     <ClInclude Include="route.h" />
     <ClInclude Include="run_command.h" />
     <ClInclude Include="schedule.h" />
src/openvpn/openvpn.vcxproj.filters +7 -1
index e6068af..8f1b9e0 100644
@@ -237,6 +237,9 @@
     <ClCompile Include="auth_token.c">
       <Filter>Source Files</Filter>
     </ClCompile>
+    <ClCompile Include="ring_buffer.c">
+      <Filter>Source Files</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="base64.h">
@@ -494,10 +497,13 @@
     <ClInclude Include="auth_token.h">
       <Filter>Header Files</Filter>
     </ClInclude>
+    <ClInclude Include="ring_buffer.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ResourceCompile Include="openvpn_win32_resources.rc">
       <Filter>Resource Files</Filter>
     </ResourceCompile>
   </ItemGroup>
-</Project>
\ No newline at end of file
+</Project>
src/openvpn/ring_buffer.c new file +54 -0

55 lines not on this page · raw message

src/openvpn/ring_buffer.h new file +79 -0

79 lines not on this page · raw message

src/openvpn/tun.c +74 -15

120 lines not on this page · raw message

src/openvpn/tun.h +3 -0
index cbdcce4..2799129 100644
@@ -39,6 +39,7 @@
 #include "proto.h"
 #include "misc.h"
 #include "networking.h"
+#include "ring_buffer.h"
 
 #ifdef _WIN32
 #define WINTUN_COMPONENT_ID "wintun"
@@ -184,6 +185,8 @@ struct tuntap
     bool wintun; /* true if wintun is used instead of tap-windows6 */
     int standby_iter;
 
+    HANDLE send_ring_handle;
+    HANDLE receive_ring_handle;
     struct tun_ring *send_ring;
     struct tun_ring *receive_ring;
     HANDLE send_tail_moved;
src/openvpn/win32.c +0 -25
index e9e0258..b2f2a19 100644
@@ -1588,29 +1588,4 @@ impersonate_as_system()
     return true;
 }
 
-bool
-register_ring_buffers(HANDLE device,
-                      struct tun_ring* send_ring,
-                      struct tun_ring* receive_ring,
-                      HANDLE send_tail_moved,
-                      HANDLE receive_tail_moved)
-{
-    struct tun_register_rings rr;
-    BOOL res;
-
-    ZeroMemory(&rr, sizeof(rr));
-
-    rr.send.ring = send_ring;
-    rr.send.ring_size = sizeof(send_ring->data);
-    rr.send.tail_moved = send_tail_moved;
-
-    rr.receive.ring = receive_ring;
-    rr.receive.ring_size = sizeof(receive_ring->data);
-    rr.receive.tail_moved = receive_tail_moved;
-
-    res = DeviceIoControl(device, TUN_IOCTL_REGISTER_RINGS, &rr, sizeof(rr), NULL, 0, NULL, NULL);
-
-    return res == TRUE;
-}
-
 #endif /* ifdef _WIN32 */
src/openvpn/win32.h +0 -43

50 lines not on this page · raw message

src/openvpnserv/Makefile.am +2 -1
index bc65070..f8d3319 100644
@@ -36,4 +36,5 @@ openvpnserv_SOURCES = \
 	service.c service.h \
 	validate.c validate.h \
 	$(top_srcdir)/src/openvpn/block_dns.c $(top_srcdir)/src/openvpn/block_dns.h \
-	openvpnserv_resources.rc
+	openvpnserv_resources.rc \
+	$(top_srcdir)/src/openvpn/ring_buffer.c $(top_srcdir)/src/openvpn/ring_buffer.h
src/openvpnserv/interactive.c +135 -6

211 lines not on this page · raw message

src/openvpnserv/openvpnserv.vcxproj +2 -0
index 7061b7b..c5a34b8 100644
@@ -115,6 +115,7 @@
     </Link>
   </ItemDefinitionGroup>
   <ItemGroup>
+    <ClCompile Include="..\openvpn\ring_buffer.c" />
     <ClCompile Include="automatic.c" />
     <ClCompile Include="common.c" />
     <ClCompile Include="interactive.c" />
@@ -123,6 +124,7 @@
     <ClCompile Include="..\openvpn\block_dns.c" />
   </ItemGroup>
   <ItemGroup>
+    <ClInclude Include="..\openvpn\ring_buffer.h" />
     <ClInclude Include="service.h" />
     <ClInclude Include="validate.h" />
     <ClInclude Include="..\openvpn\block_dns.h" />
src/openvpnserv/openvpnserv.vcxproj.filters +6 -0
index 3ce9bb2..3cb14ef 100644
@@ -33,6 +33,9 @@
     <ClCompile Include="..\openvpn\block_dns.c">
       <Filter>Source Files</Filter>
     </ClCompile>
+    <ClCompile Include="..\openvpn\ring_buffer.c">
+      <Filter>Source Files</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="service.h">
@@ -44,6 +47,9 @@
     <ClInclude Include="..\openvpn\block_dns.h">
       <Filter>Header Files</Filter>
     </ClInclude>
+    <ClInclude Include="..\openvpn\ring_buffer.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ResourceCompile Include="openvpnserv_resources.rc">
Signature · 1 line
2.7.4
#65 lore raw

PATCH v3 5/7 wintun: interactive service support

In reply to Lev Stipakov

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

Wintun requires ring buffers registration to be
performed by privileged process. In order to use
openvpn with wintun by non-Administrator, we
need to use interactive service and shared memory
to register buffers.

Openvpn process creates memory mapping object and event for send and receive ring and passes handles to interactive service. There handles are duplicated and memory mapped object is mapped into the address space of service process. Then address of mapped view and event handle is passed to wintun kernel driver.

After interactive service preformed registration, openvpn process maps memory mapped object into own address space. Thus mapped views in openvpn and service process represent the same memory region.

---

While v2 has been ACKed, it had to be rebased on top of previous patch in series, which required some manual work.

v3:
- rebased on top of PATCH v4 4/7 "wintun: ring buffers based I/O"
- added doxygen comments to ring_buffer.h

v2:
- rebased on top of master

 include/openvpn-msg.h                       |  10 ++
 src/openvpn/Makefile.am                     |   2 +-
 src/openvpn/openvpn.vcxproj                 |   2 +
 src/openvpn/openvpn.vcxproj.filters         |   8 +-
 src/openvpn/ring_buffer.c                   |  54 ++++++++
 src/openvpn/ring_buffer.h                   | 105 +++++++++++++++
 src/openvpn/tun.c                           |  82 ++++++++++--
 src/openvpn/tun.h                           |   3 +
 src/openvpn/win32.c                         |  25 ----
 src/openvpn/win32.h                         |  47 -------
 src/openvpnserv/Makefile.am                 |   3 +-
 src/openvpnserv/interactive.c               | 141 +++++++++++++++++++-
 src/openvpnserv/openvpnserv.vcxproj         |   2 +
 src/openvpnserv/openvpnserv.vcxproj.filters |   6 +
 14 files changed, 398 insertions(+), 92 deletions(-)
 create mode 100644 src/openvpn/ring_buffer.c
 create mode 100644 src/openvpn/ring_buffer.h
include/openvpn-msg.h +10 -0
index 66177a21..3ed62069 100644
@@ -39,6 +39,7 @@ typedef enum {
     msg_del_block_dns,
     msg_register_dns,
     msg_enable_dhcp,
+    msg_register_ring_buffers
 } message_type_t;
 
 typedef struct {
@@ -117,4 +118,13 @@ typedef struct {
     interface_t iface;
 } enable_dhcp_message_t;
 
+typedef struct {
+    message_header_t header;
+    HANDLE device;
+    HANDLE send_ring_handle;
+    HANDLE receive_ring_handle;
+    HANDLE send_tail_moved;
+    HANDLE receive_tail_moved;
+} register_ring_buffers_message_t;
+
 #endif /* ifndef OPENVPN_MSG_H_ */
src/openvpn/Makefile.am +1 -1
index a091ffc2..d1bb99c2 100644
@@ -138,6 +138,6 @@ openvpn_LDADD = \
 	$(OPTIONAL_SYSTEMD_LIBS) \
 	$(OPTIONAL_DL_LIBS)
 if WIN32
-openvpn_SOURCES += openvpn_win32_resources.rc block_dns.c block_dns.h
+openvpn_SOURCES += openvpn_win32_resources.rc block_dns.c block_dns.h ring_buffer.c ring_buffer.h
 openvpn_LDADD += -lgdi32 -lws2_32 -lwininet -lcrypt32 -liphlpapi -lwinmm -lfwpuclnt -lrpcrt4 -lncrypt -lsetupapi
 endif
src/openvpn/openvpn.vcxproj +2 -0
index 7446d97d..614d720a 100644
@@ -181,6 +181,7 @@
     <ClCompile Include="ps.c" />
     <ClCompile Include="push.c" />
     <ClCompile Include="reliable.c" />
+    <ClCompile Include="ring_buffer.c" />
     <ClCompile Include="route.c" />
     <ClCompile Include="run_command.c" />
     <ClCompile Include="schedule.c" />
@@ -265,6 +266,7 @@
     <ClInclude Include="push.h" />
     <ClInclude Include="pushlist.h" />
     <ClInclude Include="reliable.h" />
+    <ClInclude Include="ring_buffer.h" />
     <ClInclude Include="route.h" />
     <ClInclude Include="run_command.h" />
     <ClInclude Include="schedule.h" />
src/openvpn/openvpn.vcxproj.filters +7 -1
index 653e892c..41e62d14 100644
@@ -240,6 +240,9 @@
     <ClCompile Include="vlan.c">
       <Filter>Source Files</Filter>
     </ClCompile>
+    <ClCompile Include="ring_buffer.c">
+      <Filter>Source Files</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="base64.h">
@@ -500,10 +503,13 @@
     <ClInclude Include="vlan.h">
       <Filter>Header Files</Filter>
     </ClInclude>
+    <ClInclude Include="ring_buffer.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ResourceCompile Include="openvpn_win32_resources.rc">
       <Filter>Resource Files</Filter>
     </ResourceCompile>
   </ItemGroup>
-</Project>
\ No newline at end of file
+</Project>
src/openvpn/ring_buffer.c new file +54 -0

55 lines not on this page · raw message

src/openvpn/ring_buffer.h new file +105 -0

105 lines not on this page · raw message

src/openvpn/tun.c +71 -11

116 lines not on this page · raw message

src/openvpn/tun.h +3 -0
index 784bde1a..f9970900 100644
@@ -39,6 +39,7 @@
 #include "proto.h"
 #include "misc.h"
 #include "networking.h"
+#include "ring_buffer.h"
 
 #ifdef _WIN32
 #define WINTUN_COMPONENT_ID "wintun"
@@ -184,6 +185,8 @@ struct tuntap
     bool wintun; /* true if wintun is used instead of tap-windows6 */
     int standby_iter;
 
+    HANDLE wintun_send_ring_handle;
+    HANDLE wintun_receive_ring_handle;
     struct tun_ring *wintun_send_ring;
     struct tun_ring *wintun_receive_ring;
 #else  /* ifdef _WIN32 */
src/openvpn/win32.c +0 -25
index e9e0258f..b2f2a19f 100644
@@ -1588,29 +1588,4 @@ impersonate_as_system()
     return true;
 }
 
-bool
-register_ring_buffers(HANDLE device,
-                      struct tun_ring* send_ring,
-                      struct tun_ring* receive_ring,
-                      HANDLE send_tail_moved,
-                      HANDLE receive_tail_moved)
-{
-    struct tun_register_rings rr;
-    BOOL res;
-
-    ZeroMemory(&rr, sizeof(rr));
-
-    rr.send.ring = send_ring;
-    rr.send.ring_size = sizeof(send_ring->data);
-    rr.send.tail_moved = send_tail_moved;
-
-    rr.receive.ring = receive_ring;
-    rr.receive.ring_size = sizeof(receive_ring->data);
-    rr.receive.tail_moved = receive_tail_moved;
-
-    res = DeviceIoControl(device, TUN_IOCTL_REGISTER_RINGS, &rr, sizeof(rr), NULL, 0, NULL, NULL);
-
-    return res == TRUE;
-}
-
 #endif /* ifdef _WIN32 */
src/openvpn/win32.h +0 -47

54 lines not on this page · raw message

src/openvpnserv/Makefile.am +2 -1
index bc65070b..f8d3319c 100644
@@ -36,4 +36,5 @@ openvpnserv_SOURCES = \
 	service.c service.h \
 	validate.c validate.h \
 	$(top_srcdir)/src/openvpn/block_dns.c $(top_srcdir)/src/openvpn/block_dns.h \
-	openvpnserv_resources.rc
+	openvpnserv_resources.rc \
+	$(top_srcdir)/src/openvpn/ring_buffer.c $(top_srcdir)/src/openvpn/ring_buffer.h
src/openvpnserv/interactive.c +135 -6

211 lines not on this page · raw message

src/openvpnserv/openvpnserv.vcxproj +2 -0
index 7061b7b1..c5a34b87 100644
@@ -115,6 +115,7 @@
     </Link>
   </ItemDefinitionGroup>
   <ItemGroup>
+    <ClCompile Include="..\openvpn\ring_buffer.c" />
     <ClCompile Include="automatic.c" />
     <ClCompile Include="common.c" />
     <ClCompile Include="interactive.c" />
@@ -123,6 +124,7 @@
     <ClCompile Include="..\openvpn\block_dns.c" />
   </ItemGroup>
   <ItemGroup>
+    <ClInclude Include="..\openvpn\ring_buffer.h" />
     <ClInclude Include="service.h" />
     <ClInclude Include="validate.h" />
     <ClInclude Include="..\openvpn\block_dns.h" />
src/openvpnserv/openvpnserv.vcxproj.filters +6 -0
index 3ce9bb24..3cb14ef6 100644
@@ -33,6 +33,9 @@
     <ClCompile Include="..\openvpn\block_dns.c">
       <Filter>Source Files</Filter>
     </ClCompile>
+    <ClCompile Include="..\openvpn\ring_buffer.c">
+      <Filter>Source Files</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="service.h">
@@ -44,6 +47,9 @@
     <ClInclude Include="..\openvpn\block_dns.h">
       <Filter>Header Files</Filter>
     </ClInclude>
+    <ClInclude Include="..\openvpn\ring_buffer.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ResourceCompile Include="openvpnserv_resources.rc">
Signature · 1 line
2.17.1
#66 lore raw

PATCH v4 5/7 wintun: interactive service support

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

Wintun requires ring buffers registration to be
performed by privileged process. In order to use
openvpn with wintun by non-Administrator, we
need to use interactive service and shared memory
to register buffers.

Openvpn process creates memory mapping object and event for send and receive ring and passes handles to interactive service. There handles are duplicated and memory mapped object is mapped into the address space of service process. Then address of mapped view and event handle is passed to wintun kernel driver.

After interactive service preformed registration, openvpn process maps memory mapped object into own address space. Thus mapped views in openvpn and service process represent the same memory region.

---
While v2 has been ACKed, it had to be rebased on top of previous patch in series, which required some manual work.

v4:
- rebased on top of [PATCH v5 4/7] "wintun: ring buffers based I/O"

v3:
- rebased on top of [PATCH v4 4/7] "wintun: ring buffers based I/O"
- added doxygen comments to ring_buffer.h

v2:
- rebased on top of master

 include/openvpn-msg.h                       |  10 ++
 src/openvpn/Makefile.am                     |   2 +-
 src/openvpn/openvpn.vcxproj                 |   2 +
 src/openvpn/openvpn.vcxproj.filters         |   8 +-
 src/openvpn/ring_buffer.c                   |  56 ++++++++
 src/openvpn/ring_buffer.h                   | 105 +++++++++++++++
 src/openvpn/tun.c                           |  82 ++++++++++--
 src/openvpn/tun.h                           |   3 +
 src/openvpn/win32.c                         |  27 ----
 src/openvpn/win32.h                         |  47 -------
 src/openvpnserv/Makefile.am                 |   3 +-
 src/openvpnserv/interactive.c               | 141 +++++++++++++++++++-
 src/openvpnserv/openvpnserv.vcxproj         |   2 +
 src/openvpnserv/openvpnserv.vcxproj.filters |   6 +
 14 files changed, 400 insertions(+), 94 deletions(-)
 create mode 100644 src/openvpn/ring_buffer.c
 create mode 100644 src/openvpn/ring_buffer.h
include/openvpn-msg.h +10 -0
index 66177a21..3ed62069 100644
@@ -39,6 +39,7 @@ typedef enum {
     msg_del_block_dns,
     msg_register_dns,
     msg_enable_dhcp,
+    msg_register_ring_buffers
 } message_type_t;
 
 typedef struct {
@@ -117,4 +118,13 @@ typedef struct {
     interface_t iface;
 } enable_dhcp_message_t;
 
+typedef struct {
+    message_header_t header;
+    HANDLE device;
+    HANDLE send_ring_handle;
+    HANDLE receive_ring_handle;
+    HANDLE send_tail_moved;
+    HANDLE receive_tail_moved;
+} register_ring_buffers_message_t;
+
 #endif /* ifndef OPENVPN_MSG_H_ */
src/openvpn/Makefile.am +1 -1
index a091ffc2..d1bb99c2 100644
@@ -138,6 +138,6 @@ openvpn_LDADD = \
 	$(OPTIONAL_SYSTEMD_LIBS) \
 	$(OPTIONAL_DL_LIBS)
 if WIN32
-openvpn_SOURCES += openvpn_win32_resources.rc block_dns.c block_dns.h
+openvpn_SOURCES += openvpn_win32_resources.rc block_dns.c block_dns.h ring_buffer.c ring_buffer.h
 openvpn_LDADD += -lgdi32 -lws2_32 -lwininet -lcrypt32 -liphlpapi -lwinmm -lfwpuclnt -lrpcrt4 -lncrypt -lsetupapi
 endif
src/openvpn/openvpn.vcxproj +2 -0
index 7446d97d..614d720a 100644
@@ -181,6 +181,7 @@
     <ClCompile Include="ps.c" />
     <ClCompile Include="push.c" />
     <ClCompile Include="reliable.c" />
+    <ClCompile Include="ring_buffer.c" />
     <ClCompile Include="route.c" />
     <ClCompile Include="run_command.c" />
     <ClCompile Include="schedule.c" />
@@ -265,6 +266,7 @@
     <ClInclude Include="push.h" />
     <ClInclude Include="pushlist.h" />
     <ClInclude Include="reliable.h" />
+    <ClInclude Include="ring_buffer.h" />
     <ClInclude Include="route.h" />
     <ClInclude Include="run_command.h" />
     <ClInclude Include="schedule.h" />
src/openvpn/openvpn.vcxproj.filters +7 -1
index 653e892c..41e62d14 100644
@@ -240,6 +240,9 @@
     <ClCompile Include="vlan.c">
       <Filter>Source Files</Filter>
     </ClCompile>
+    <ClCompile Include="ring_buffer.c">
+      <Filter>Source Files</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="base64.h">
@@ -500,10 +503,13 @@
     <ClInclude Include="vlan.h">
       <Filter>Header Files</Filter>
     </ClInclude>
+    <ClInclude Include="ring_buffer.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ResourceCompile Include="openvpn_win32_resources.rc">
       <Filter>Resource Files</Filter>
     </ResourceCompile>
   </ItemGroup>
-</Project>
\ No newline at end of file
+</Project>
src/openvpn/ring_buffer.c new file +56 -0

57 lines not on this page · raw message

src/openvpn/ring_buffer.h new file +105 -0

105 lines not on this page · raw message

src/openvpn/tun.c +71 -11

116 lines not on this page · raw message

src/openvpn/tun.h +3 -0
index 784bde1a..f9970900 100644
@@ -39,6 +39,7 @@
 #include "proto.h"
 #include "misc.h"
 #include "networking.h"
+#include "ring_buffer.h"
 
 #ifdef _WIN32
 #define WINTUN_COMPONENT_ID "wintun"
@@ -184,6 +185,8 @@ struct tuntap
     bool wintun; /* true if wintun is used instead of tap-windows6 */
     int standby_iter;
 
+    HANDLE wintun_send_ring_handle;
+    HANDLE wintun_receive_ring_handle;
     struct tun_ring *wintun_send_ring;
     struct tun_ring *wintun_receive_ring;
 #else  /* ifdef _WIN32 */
src/openvpn/win32.c +0 -27
index 5c168b3b..b2f2a19f 100644
@@ -1588,31 +1588,4 @@ impersonate_as_system()
     return true;
 }
 
-bool
-register_ring_buffers(HANDLE device,
-                      struct tun_ring* send_ring,
-                      struct tun_ring* receive_ring,
-                      HANDLE send_tail_moved,
-                      HANDLE receive_tail_moved)
-{
-    struct tun_register_rings rr;
-    BOOL res;
-    DWORD bytes_returned;
-
-    ZeroMemory(&rr, sizeof(rr));
-
-    rr.send.ring = send_ring;
-    rr.send.ring_size = sizeof(send_ring->data);
-    rr.send.tail_moved = send_tail_moved;
-
-    rr.receive.ring = receive_ring;
-    rr.receive.ring_size = sizeof(receive_ring->data);
-    rr.receive.tail_moved = receive_tail_moved;
-
-    res = DeviceIoControl(device, TUN_IOCTL_REGISTER_RINGS, &rr, sizeof(rr),
-                          NULL, 0, &bytes_returned, NULL);
-
-    return res == TRUE;
-}
-
 #endif /* ifdef _WIN32 */
src/openvpn/win32.h +0 -47

54 lines not on this page · raw message

src/openvpnserv/Makefile.am +2 -1
index bc65070b..f8d3319c 100644
@@ -36,4 +36,5 @@ openvpnserv_SOURCES = \
 	service.c service.h \
 	validate.c validate.h \
 	$(top_srcdir)/src/openvpn/block_dns.c $(top_srcdir)/src/openvpn/block_dns.h \
-	openvpnserv_resources.rc
+	openvpnserv_resources.rc \
+	$(top_srcdir)/src/openvpn/ring_buffer.c $(top_srcdir)/src/openvpn/ring_buffer.h
src/openvpnserv/interactive.c +135 -6

211 lines not on this page · raw message

src/openvpnserv/openvpnserv.vcxproj +2 -0
index 7061b7b1..c5a34b87 100644
@@ -115,6 +115,7 @@
     </Link>
   </ItemDefinitionGroup>
   <ItemGroup>
+    <ClCompile Include="..\openvpn\ring_buffer.c" />
     <ClCompile Include="automatic.c" />
     <ClCompile Include="common.c" />
     <ClCompile Include="interactive.c" />
@@ -123,6 +124,7 @@
     <ClCompile Include="..\openvpn\block_dns.c" />
   </ItemGroup>
   <ItemGroup>
+    <ClInclude Include="..\openvpn\ring_buffer.h" />
     <ClInclude Include="service.h" />
     <ClInclude Include="validate.h" />
     <ClInclude Include="..\openvpn\block_dns.h" />
src/openvpnserv/openvpnserv.vcxproj.filters +6 -0
index 3ce9bb24..3cb14ef6 100644
@@ -33,6 +33,9 @@
     <ClCompile Include="..\openvpn\block_dns.c">
       <Filter>Source Files</Filter>
     </ClCompile>
+    <ClCompile Include="..\openvpn\ring_buffer.c">
+      <Filter>Source Files</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="service.h">
@@ -44,6 +47,9 @@
     <ClInclude Include="..\openvpn\block_dns.h">
       <Filter>Header Files</Filter>
     </ClInclude>
+    <ClInclude Include="..\openvpn\ring_buffer.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ResourceCompile Include="openvpnserv_resources.rc">
Signature · 1 line
2.17.1
#67 lore raw

PATCH v5 5/7 wintun: interactive service support

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

Wintun requires ring buffers registration to be
performed by privileged process. In order to use
openvpn with wintun by non-Administrator, we
need to use interactive service and shared memory
to register buffers.

Openvpn process creates memory mapping object and event for send and receive ring and passes handles to interactive service. There handles are duplicated and memory mapped object is mapped into the address space of service process. Then address of mapped view and event handle is passed to wintun kernel driver.

After interactive service preformed registration, openvpn process maps memory mapped object into own address space. Thus mapped views in openvpn and service process represent the same memory region.

---

While v2 has been ACKed, it had to be rebased on top of previous patch in series, which required some manual work.

v5:
- rebased on top of [PATCH v8 4/7] wintun: ring buffers based I/O (fixed struct tun_ring layout and made DeviceIoControl result check more robust)

v4:
- rebased on top of [PATCH v5 4/7] "wintun: ring buffers based I/O"

v3:
- rebased on top of [PATCH v4 4/7] "wintun: ring buffers based I/O"
- added doxygen comments to ring_buffer.h

v2:
- rebased on top of master

 include/openvpn-msg.h                       |  10 ++
 src/openvpn/Makefile.am                     |   2 +-
 src/openvpn/openvpn.vcxproj                 |   2 +
 src/openvpn/openvpn.vcxproj.filters         |   8 +-
 src/openvpn/ring_buffer.c                   |  56 ++++++++
 src/openvpn/ring_buffer.h                   | 104 +++++++++++++++
 src/openvpn/tun.c                           |  82 ++++++++++--
 src/openvpn/tun.h                           |   3 +
 src/openvpn/win32.c                         |  27 ----
 src/openvpn/win32.h                         |  46 -------
 src/openvpnserv/Makefile.am                 |   3 +-
 src/openvpnserv/interactive.c               | 141 +++++++++++++++++++-
 src/openvpnserv/openvpnserv.vcxproj         |   2 +
 src/openvpnserv/openvpnserv.vcxproj.filters |   6 +
 14 files changed, 399 insertions(+), 93 deletions(-)
 create mode 100644 src/openvpn/ring_buffer.c
 create mode 100644 src/openvpn/ring_buffer.h
include/openvpn-msg.h +10 -0
index 66177a21..3ed62069 100644
@@ -39,6 +39,7 @@ typedef enum {
     msg_del_block_dns,
     msg_register_dns,
     msg_enable_dhcp,
+    msg_register_ring_buffers
 } message_type_t;
 
 typedef struct {
@@ -117,4 +118,13 @@ typedef struct {
     interface_t iface;
 } enable_dhcp_message_t;
 
+typedef struct {
+    message_header_t header;
+    HANDLE device;
+    HANDLE send_ring_handle;
+    HANDLE receive_ring_handle;
+    HANDLE send_tail_moved;
+    HANDLE receive_tail_moved;
+} register_ring_buffers_message_t;
+
 #endif /* ifndef OPENVPN_MSG_H_ */
src/openvpn/Makefile.am +1 -1
index a091ffc2..d1bb99c2 100644
@@ -138,6 +138,6 @@ openvpn_LDADD = \
 	$(OPTIONAL_SYSTEMD_LIBS) \
 	$(OPTIONAL_DL_LIBS)
 if WIN32
-openvpn_SOURCES += openvpn_win32_resources.rc block_dns.c block_dns.h
+openvpn_SOURCES += openvpn_win32_resources.rc block_dns.c block_dns.h ring_buffer.c ring_buffer.h
 openvpn_LDADD += -lgdi32 -lws2_32 -lwininet -lcrypt32 -liphlpapi -lwinmm -lfwpuclnt -lrpcrt4 -lncrypt -lsetupapi
 endif
src/openvpn/openvpn.vcxproj +2 -0
index 7446d97d..614d720a 100644
@@ -181,6 +181,7 @@
     <ClCompile Include="ps.c" />
     <ClCompile Include="push.c" />
     <ClCompile Include="reliable.c" />
+    <ClCompile Include="ring_buffer.c" />
     <ClCompile Include="route.c" />
     <ClCompile Include="run_command.c" />
     <ClCompile Include="schedule.c" />
@@ -265,6 +266,7 @@
     <ClInclude Include="push.h" />
     <ClInclude Include="pushlist.h" />
     <ClInclude Include="reliable.h" />
+    <ClInclude Include="ring_buffer.h" />
     <ClInclude Include="route.h" />
     <ClInclude Include="run_command.h" />
     <ClInclude Include="schedule.h" />
src/openvpn/openvpn.vcxproj.filters +7 -1
index 653e892c..41e62d14 100644
@@ -240,6 +240,9 @@
     <ClCompile Include="vlan.c">
       <Filter>Source Files</Filter>
     </ClCompile>
+    <ClCompile Include="ring_buffer.c">
+      <Filter>Source Files</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="base64.h">
@@ -500,10 +503,13 @@
     <ClInclude Include="vlan.h">
       <Filter>Header Files</Filter>
     </ClInclude>
+    <ClInclude Include="ring_buffer.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ResourceCompile Include="openvpn_win32_resources.rc">
       <Filter>Resource Files</Filter>
     </ResourceCompile>
   </ItemGroup>
-</Project>
\ No newline at end of file
+</Project>
src/openvpn/ring_buffer.c new file +56 -0

57 lines not on this page · raw message

src/openvpn/ring_buffer.h new file +104 -0

104 lines not on this page · raw message

src/openvpn/tun.c +71 -11

116 lines not on this page · raw message

src/openvpn/tun.h +3 -0
index 10f687c5..b0b80d1a 100644
@@ -39,6 +39,7 @@
 #include "proto.h"
 #include "misc.h"
 #include "networking.h"
+#include "ring_buffer.h"
 
 #ifdef _WIN32
 #define WINTUN_COMPONENT_ID "wintun"
@@ -183,6 +184,8 @@ struct tuntap
     bool wintun; /* true if wintun is used instead of tap-windows6 */
     int standby_iter;
 
+    HANDLE wintun_send_ring_handle;
+    HANDLE wintun_receive_ring_handle;
     struct tun_ring *wintun_send_ring;
     struct tun_ring *wintun_receive_ring;
 #else  /* ifdef _WIN32 */
src/openvpn/win32.c +0 -27
index 24dae7d6..b2f2a19f 100644
@@ -1588,31 +1588,4 @@ impersonate_as_system()
     return true;
 }
 
-bool
-register_ring_buffers(HANDLE device,
-                      struct tun_ring* send_ring,
-                      struct tun_ring* receive_ring,
-                      HANDLE send_tail_moved,
-                      HANDLE receive_tail_moved)
-{
-    struct tun_register_rings rr;
-    BOOL res;
-    DWORD bytes_returned;
-
-    ZeroMemory(&rr, sizeof(rr));
-
-    rr.send.ring = send_ring;
-    rr.send.ring_size = sizeof(struct tun_ring);
-    rr.send.tail_moved = send_tail_moved;
-
-    rr.receive.ring = receive_ring;
-    rr.receive.ring_size = sizeof(struct tun_ring);
-    rr.receive.tail_moved = receive_tail_moved;
-
-    res = DeviceIoControl(device, TUN_IOCTL_REGISTER_RINGS, &rr, sizeof(rr),
-                          NULL, 0, &bytes_returned, NULL);
-
-    return res != FALSE;
-}
-
 #endif /* ifdef _WIN32 */
src/openvpn/win32.h +0 -46

53 lines not on this page · raw message

src/openvpnserv/Makefile.am +2 -1
index bc65070b..f8d3319c 100644
@@ -36,4 +36,5 @@ openvpnserv_SOURCES = \
 	service.c service.h \
 	validate.c validate.h \
 	$(top_srcdir)/src/openvpn/block_dns.c $(top_srcdir)/src/openvpn/block_dns.h \
-	openvpnserv_resources.rc
+	openvpnserv_resources.rc \
+	$(top_srcdir)/src/openvpn/ring_buffer.c $(top_srcdir)/src/openvpn/ring_buffer.h
src/openvpnserv/interactive.c +135 -6

211 lines not on this page · raw message

src/openvpnserv/openvpnserv.vcxproj +2 -0
index 7061b7b1..c5a34b87 100644
@@ -115,6 +115,7 @@
     </Link>
   </ItemDefinitionGroup>
   <ItemGroup>
+    <ClCompile Include="..\openvpn\ring_buffer.c" />
     <ClCompile Include="automatic.c" />
     <ClCompile Include="common.c" />
     <ClCompile Include="interactive.c" />
@@ -123,6 +124,7 @@
     <ClCompile Include="..\openvpn\block_dns.c" />
   </ItemGroup>
   <ItemGroup>
+    <ClInclude Include="..\openvpn\ring_buffer.h" />
     <ClInclude Include="service.h" />
     <ClInclude Include="validate.h" />
     <ClInclude Include="..\openvpn\block_dns.h" />
src/openvpnserv/openvpnserv.vcxproj.filters +6 -0
index 3ce9bb24..3cb14ef6 100644
@@ -33,6 +33,9 @@
     <ClCompile Include="..\openvpn\block_dns.c">
       <Filter>Source Files</Filter>
     </ClCompile>
+    <ClCompile Include="..\openvpn\ring_buffer.c">
+      <Filter>Source Files</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="service.h">
@@ -44,6 +47,9 @@
     <ClInclude Include="..\openvpn\block_dns.h">
       <Filter>Header Files</Filter>
     </ClInclude>
+    <ClInclude Include="..\openvpn\ring_buffer.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ResourceCompile Include="openvpnserv_resources.rc">
Signature · 1 line
2.17.1
Acked-by on v5 5/7
#68 lore raw

Re: [Openvpn-devel] [PATCH v5 5/7] wintun: interactive service support

Definitely an ack. We need this for Wintun and to drop that SYSTEM token hack.

Best regards,
Simon

Quoted 900 lines

-----Original Message-----
From: Lev Stipakov <[email protected]...>
Sent: Tuesday, December 17, 2019 1:51 PM
To: [email protected]
Cc: Lev Stipakov <[email protected]...>
Subject: [Openvpn-devel] [PATCH v5 5/7] wintun: interactive service support

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

Wintun requires ring buffers registration to be performed by privileged process. In order to use openvpn with wintun by non-Administrator, we need to use interactive service and shared memory to register buffers.

Openvpn process creates memory mapping object and event for send and receive ring and passes handles to interactive service. There handles are duplicated and memory mapped object is mapped into the address space of service process.
Then address of mapped view and event handle is passed to wintun kernel driver.

After interactive service preformed registration, openvpn process maps memory mapped object into own address space. Thus mapped views in openvpn and service process represent the same memory region.

---

While v2 has been ACKed, it had to be rebased on top of previous patch
in series, which required some manual work.

v5:
- rebased on top of [PATCH v8 4/7] wintun: ring buffers based I/O (fixed struct tun_ring layout and made DeviceIoControl result check
more robust)

v4:
- rebased on top of [PATCH v5 4/7] "wintun: ring buffers based I/O"

v3:
- rebased on top of [PATCH v4 4/7] "wintun: ring buffers based I/O"
- added doxygen comments to ring_buffer.h

v2:
- rebased on top of master

 include/openvpn-msg.h                       |  10 ++
 src/openvpn/Makefile.am                     |   2 +-
 src/openvpn/openvpn.vcxproj                 |   2 +
 src/openvpn/openvpn.vcxproj.filters         |   8 +-
 src/openvpn/ring_buffer.c                   |  56 ++++++++
 src/openvpn/ring_buffer.h                   | 104 +++++++++++++++
 src/openvpn/tun.c                           |  82 ++++++++++--
 src/openvpn/tun.h                           |   3 +
 src/openvpn/win32.c                         |  27 ----
 src/openvpn/win32.h                         |  46 -------
 src/openvpnserv/Makefile.am                 |   3 +-
 src/openvpnserv/interactive.c               | 141 +++++++++++++++++++-
 src/openvpnserv/openvpnserv.vcxproj         |   2 +
 src/openvpnserv/openvpnserv.vcxproj.filters |   6 +
 14 files changed, 399 insertions(+), 93 deletions(-)  create mode

100644 src/openvpn/ring_buffer.c create mode 100644 src/openvpn/ring_buffer.h

include/openvpn-msg.h index renamed from include/openvpn-msg.h +0 -0

66177a21..3ed62069 100644

include/openvpn-msg.h +10 -0
@@ -39,6 +39,7 @@ typedef enum {
     msg_del_block_dns,
     msg_register_dns,
     msg_enable_dhcp,
+    msg_register_ring_buffers
 } message_type_t;
 
 typedef struct {
@@ -117,4 +118,13 @@ typedef struct {
     interface_t iface;
 } enable_dhcp_message_t;
 
+typedef struct {
+    message_header_t header;
+    HANDLE device;
+    HANDLE send_ring_handle;
+    HANDLE receive_ring_handle;
+    HANDLE send_tail_moved;
+    HANDLE receive_tail_moved;
+} register_ring_buffers_message_t;
+
 #endif /* ifndef OPENVPN_MSG_H_ */
src/openvpn/Makefile.am index renamed from src/openvpn/Makefile.am +0 -0

a091ffc2..d1bb99c2 100644

src/openvpn/Makefile.am +2 -1
@@ -138,6 +138,6 @@ openvpn_LDADD = \
 	$(OPTIONAL_SYSTEMD_LIBS) \
 	$(OPTIONAL_DL_LIBS)
 if WIN32
-openvpn_SOURCES += openvpn_win32_resources.rc block_dns.c block_dns.h
+openvpn_SOURCES += openvpn_win32_resources.rc block_dns.c block_dns.h
+ring_buffer.c ring_buffer.h
 openvpn_LDADD += -lgdi32 -lws2_32 -lwininet -lcrypt32 -liphlpapi -

lwinmm -lfwpuclnt -lrpcrt4 -lncrypt -lsetupapi endif diff --git a/src/openvpn/openvpn.vcxproj b/src/openvpn/openvpn.vcxproj index 7446d97d..614d720a 100644

src/openvpn/openvpn.vcxproj +2 -0
@@ -181,6 +181,7 @@
     <ClCompile Include="ps.c" />
     <ClCompile Include="push.c" />
     <ClCompile Include="reliable.c" />
+    <ClCompile Include="ring_buffer.c" />
     <ClCompile Include="route.c" />
     <ClCompile Include="run_command.c" />
     <ClCompile Include="schedule.c" />
@@ -265,6 +266,7 @@
     <ClInclude Include="push.h" />
     <ClInclude Include="pushlist.h" />
     <ClInclude Include="reliable.h" />
+    <ClInclude Include="ring_buffer.h" />
     <ClInclude Include="route.h" />
     <ClInclude Include="run_command.h" />
     <ClInclude Include="schedule.h" />
src/openvpn/openvpn.vcxproj.filters +7 -1
index 653e892c..41e62d14 100644
@@ -240,6 +240,9 @@
     <ClCompile Include="vlan.c">
       <Filter>Source Files</Filter>
     </ClCompile>
+    <ClCompile Include="ring_buffer.c">
+      <Filter>Source Files</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="base64.h">
@@ -500,10 +503,13 @@
     <ClInclude Include="vlan.h">
       <Filter>Header Files</Filter>
     </ClInclude>
+    <ClInclude Include="ring_buffer.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ResourceCompile Include="openvpn_win32_resources.rc">
       <Filter>Resource Files</Filter>
     </ResourceCompile>
   </ItemGroup>
-</Project>
\ No newline at end of file
+</Project>
src/openvpn/ring_buffer.c new renamed from src/openvpn/ring_buffer.c +0 -0

file mode 100644 index 00000000..8c81dc46

src/openvpn/ring_buffer.c new file +52 -0
@@ -0,0 +1,56 @@
+/*
+ *  OpenVPN -- An application to securely tunnel IP networks
+ *             over a single UDP port, with support for SSL/TLS-based
+ *             session authentication and key exchange,
+ *             packet encryption, packet authentication, and
+ *             packet compression.
+ *
+ *  Copyright (C) 2002-2019 OpenVPN Inc <[email protected]...>
+ *                2019 Lev Stipakov <[email protected]...>
+ *
+ *  This program is free software; you can redistribute it and/or
+modify
+ *  it under the terms of the GNU General Public License version 2
+ *  as published by the Free Software Foundation.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+along
+ *  with this program; if not, write to the Free Software Foundation,
+Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "ring_buffer.h"
+
+#ifdef _WIN32
+
+bool
+register_ring_buffers(HANDLE device,
+                      struct tun_ring *send_ring,
+                      struct tun_ring *receive_ring,
+                      HANDLE send_tail_moved,
+                      HANDLE receive_tail_moved) {
+    struct tun_register_rings rr;
+    BOOL res;
+    DWORD bytes_returned;
+
+    ZeroMemory(&rr, sizeof(rr));
+
+    rr.send.ring = send_ring;
+    rr.send.ring_size = sizeof(struct tun_ring);
+    rr.send.tail_moved = send_tail_moved;
+
+    rr.receive.ring = receive_ring;
+    rr.receive.ring_size = sizeof(struct tun_ring);
+    rr.receive.tail_moved = receive_tail_moved;
+
+    res = DeviceIoControl(device, TUN_IOCTL_REGISTER_RINGS, &rr,

sizeof(rr),

src/openvpn/ring_buffer.c new file +6 -0
+                          NULL, 0, &bytes_returned, NULL);
+
+    return res != FALSE;
+}
+
+#endif /* ifdef _WIN32 */
\ No newline at end of file
src/openvpn/ring_buffer.h new renamed from src/openvpn/ring_buffer.h +0 -0

file mode 100644 index 00000000..3522c984

src/openvpn/ring_buffer.h new file +96 -0
@@ -0,0 +1,104 @@
+/*
+ *  OpenVPN -- An application to securely tunnel IP networks
+ *             over a single UDP port, with support for SSL/TLS-based
+ *             session authentication and key exchange,
+ *             packet encryption, packet authentication, and
+ *             packet compression.
+ *
+ *  Copyright (C) 2002-2019 OpenVPN Inc <[email protected]...>
+ *                2019 Lev Stipakov <[email protected]...>
+ *
+ *  This program is free software; you can redistribute it and/or
+modify
+ *  it under the terms of the GNU General Public License version 2
+ *  as published by the Free Software Foundation.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+along
+ *  with this program; if not, write to the Free Software Foundation,
+Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifdef _WIN32
+#ifndef OPENVPN_RING_BUFFER_H
+#define OPENVPN_RING_BUFFER_H
+
+#include <windows.h>
+#include <winioctl.h>
+
+#include <stdint.h>
+#include <stdbool.h>
+
+/*
+ * Values below are taken from Wireguard Windows client
+ *
+https://github.com/WireGuard/wireguard-go/blob/master/tun/wintun/ring_w
+indows.go#L14
+ */
+#define WINTUN_RING_CAPACITY        0x800000
+#define WINTUN_RING_TRAILING_BYTES  0x10000
+#define WINTUN_MAX_PACKET_SIZE      0xffff
+#define WINTUN_PACKET_ALIGN         4
+
+#define TUN_IOCTL_REGISTER_RINGS CTL_CODE(51820U, 0x970U,
+METHOD_BUFFERED, FILE_READ_DATA | FILE_WRITE_DATA)
+
+/**
+ * Wintun ring buffer
+ * See https://github.com/WireGuard/wintun#ring-layout
+ */
+struct tun_ring
+{
+    volatile ULONG head;
+    volatile ULONG tail;
+    volatile LONG alertable;
+    UCHAR data[WINTUN_RING_CAPACITY + WINTUN_RING_TRAILING_BYTES]; };
+
+/**
+ * Struct for ring buffers registration
+ * See https://github.com/WireGuard/wintun#registering-rings
+ */
+struct tun_register_rings
+{
+    struct
+    {
+        ULONG ring_size;
+        struct tun_ring *ring;
+        HANDLE tail_moved;
+    } send, receive;
+};
+
+struct TUN_PACKET_HEADER
+{
+    uint32_t size;
+};
+
+struct TUN_PACKET
+{
+    uint32_t size;
+    UCHAR data[WINTUN_MAX_PACKET_SIZE]; };
+
+/**
+ * Registers ring buffers used to exchange data between
+ * userspace openvpn process and wintun kernel driver,
+ * see https://github.com/WireGuard/wintun#registering-rings
+ *
+ * @param device              handle to opened wintun device
+ * @param send_ring           pointer to send ring
+ * @param receive_ring        pointer to receive ring
+ * @param send_tail_moved     event set by wintun to signal openvpn
+ *                            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
+ */

src/openvpn/ring_buffer.h new file +7 -0
+bool register_ring_buffers(HANDLE device,
+                           struct tun_ring *send_ring,
+                           struct tun_ring *receive_ring,
+                           HANDLE send_tail_moved,
+                           HANDLE receive_tail_moved);
+
+#endif /* ifndef OPENVPN_RING_BUFFER_H */ #endif /* ifdef _WIN32 */
src/openvpn/tun.c index renamed from src/openvpn/tun.c +0 -0

3f66b216..77d84fb2 100644

src/openvpn/tun.c +15 -5
@@ -779,17 +779,29 @@ init_tun_post(struct tuntap *tt,
 
     if (tt->wintun)
     {
-        tt->wintun_send_ring = malloc(sizeof(struct tun_ring));
-        tt->wintun_receive_ring = malloc(sizeof(struct tun_ring));
-        if ((tt->wintun_send_ring == NULL) || (tt->wintun_receive_ring
 == NULL))
+        tt->wintun_send_ring_handle =
 CreateFileMapping(INVALID_HANDLE_VALUE, NULL,
+                                                        PAGE_READWRITE,
+                                                        0,
+                                                        sizeof(struct
 tun_ring),
+                                                        NULL);
+        tt->wintun_receive_ring_handle =
 CreateFileMapping(INVALID_HANDLE_VALUE,
+                                                           NULL,
+
 PAGE_READWRITE,
+                                                           0,
+
 sizeof(struct tun_ring),
+                                                           NULL);
+        if ((tt->wintun_send_ring_handle == NULL) ||
+ (tt->wintun_receive_ring_handle == NULL))
         {
             msg(M_FATAL, "Cannot allocate memory for ring buffer");
         }
-        ZeroMemory(tt->wintun_send_ring, sizeof(struct tun_ring));
-        ZeroMemory(tt->wintun_receive_ring, sizeof(struct tun_ring));
 
         tt->rw_handle.read = CreateEvent(NULL, FALSE, FALSE, NULL);
         tt->rw_handle.write = CreateEvent(NULL, FALSE, FALSE, NULL);
+
+        if ((tt->rw_handle.read == NULL) || (tt->rw_handle.write ==

NULL))

src/openvpn/tun.c +40 -0
+        {
+            msg(M_FATAL, "Cannot create events for ring buffer");
+        }
     }
     else
     {
@@ -5604,6 +5616,44 @@ register_dns_service(const struct tuntap *tt)
     gc_free(&gc);
 }
 
+static void
+service_register_ring_buffers(const struct tuntap *tt) {
+    HANDLE msg_channel = tt->options.msg_channel;
+    ack_message_t ack;
+    struct gc_arena gc = gc_new();
+
+    register_ring_buffers_message_t msg = {
+        .header = {
+            msg_register_ring_buffers,
+            sizeof(register_ring_buffers_message_t),
+            0
+        },
+        .device = tt->hand,
+        .send_ring_handle = tt->wintun_send_ring_handle,
+        .receive_ring_handle = tt->wintun_receive_ring_handle,
+        .send_tail_moved = tt->rw_handle.read,
+        .receive_tail_moved = tt->rw_handle.write
+    };
+
+    if (!send_msg_iservice(msg_channel, &msg, sizeof(msg), &ack,
 "Register ring buffers"))
+    {
+        gc_free(&gc);
+        return;
+    }
+    else if (ack.error_number != NO_ERROR)
+    {
+        msg(M_FATAL, "Register ring buffers failed using service: %s
 [status=0x%x]",
+            strerror_win32(ack.error_number, &gc), ack.error_number);
+    }
+    else
+    {
+        msg(M_INFO, "Ring buffers registered via service");
+    }
+
+    gc_free(&gc);
+}
+
 void
 fork_register_dns_action(struct tuntap *tt)  { @@ -6198,9 +6248,20 @@

open_tun(const char *dev, const char *dev_type, const char *dev_node, struct tun

     if (tt->wintun)
     {

+ tt->wintun_send_ring = (struct tun_ring *)MapViewOfFile(tt-

Quoted 1 line

wintun_send_ring_handle,

+
FILE_MAP_ALL_ACCESS,

src/openvpn/tun.c +3 -0
+                                                                0,
+                                                                0,
+

sizeof(struct tun_ring));
+ tt->wintun_receive_ring = (struct tun_ring *)MapViewOfFile(tt-

Quoted 1 line

wintun_receive_ring_handle,

+
FILE_MAP_ALL_ACCESS,

src/openvpn/tun.c +10 -6
+                                                                   0,
+                                                                   0,
+
+ sizeof(struct tun_ring));
+
         if (tt->options.msg_channel)
         {
-            /* TODO */
+            service_register_ring_buffers(tt);
         }
         else
         {
@@ -6365,13 +6426,12 @@ close_tun(struct tuntap *tt, openvpn_net_ctx_t
 *ctx)
     {
         CloseHandle(tt->rw_handle.read);
         CloseHandle(tt->rw_handle.write);
+        UnmapViewOfFile(tt->wintun_send_ring);
+        UnmapViewOfFile(tt->wintun_receive_ring);
+        CloseHandle(tt->wintun_send_ring_handle);
+        CloseHandle(tt->wintun_receive_ring_handle);
     }
 
-    free(tt->wintun_receive_ring);
-    free(tt->wintun_send_ring);
-
-    tt->wintun_receive_ring = NULL;
-    tt->wintun_send_ring = NULL;
 
     clear_tuntap(tt);
     free(tt);
src/openvpn/tun.h index renamed from src/openvpn/tun.h +0 -0

10f687c5..b0b80d1a 100644

src/openvpn/tun.h +3 -0
@@ -39,6 +39,7 @@
 #include "proto.h"
 #include "misc.h"
 #include "networking.h"
+#include "ring_buffer.h"
 
 #ifdef _WIN32
 #define WINTUN_COMPONENT_ID "wintun"
@@ -183,6 +184,8 @@ struct tuntap
     bool wintun; /* true if wintun is used instead of tap-windows6 */
     int standby_iter;
 
+    HANDLE wintun_send_ring_handle;
+    HANDLE wintun_receive_ring_handle;
     struct tun_ring *wintun_send_ring;
     struct tun_ring *wintun_receive_ring;  #else  /* ifdef _WIN32 */
src/openvpn/win32.c index renamed from src/openvpn/win32.c +0 -0

24dae7d6..b2f2a19f 100644

src/openvpn/win32.c +0 -27
@@ -1588,31 +1588,4 @@ impersonate_as_system()
     return true;
 }
 
-bool
-register_ring_buffers(HANDLE device,
-                      struct tun_ring* send_ring,
-                      struct tun_ring* receive_ring,
-                      HANDLE send_tail_moved,
-                      HANDLE receive_tail_moved)
-{
-    struct tun_register_rings rr;
-    BOOL res;
-    DWORD bytes_returned;
-
-    ZeroMemory(&rr, sizeof(rr));
-
-    rr.send.ring = send_ring;
-    rr.send.ring_size = sizeof(struct tun_ring);
-    rr.send.tail_moved = send_tail_moved;
-
-    rr.receive.ring = receive_ring;
-    rr.receive.ring_size = sizeof(struct tun_ring);
-    rr.receive.tail_moved = receive_tail_moved;
-
-    res = DeviceIoControl(device, TUN_IOCTL_REGISTER_RINGS, &rr,
 sizeof(rr),
-                          NULL, 0, &bytes_returned, NULL);
-
-    return res != FALSE;
-}
-

#endif /* ifdef _WIN32 */

src/openvpn/win32.h index renamed from src/openvpn/win32.h +0 -0

5fe95f47..4b508c56 100644

src/openvpn/win32.h +0 -3
@@ -325,53 +325,7 @@ bool send_msg_iservice(HANDLE pipe, const void
 *data, size_t size,  int  openvpn_execve(const struct argv *a, const
 struct env_set *es, const unsigned int flags);
 
-/*
- * Values below are taken from Wireguard Windows client
- * https://github.com/WireGuard/wireguard-

go/blob/master/tun/wintun/ring_windows.go#L14
- */

src/openvpn/win32.h +0 -2
-#define WINTUN_RING_CAPACITY 0x800000
-#define WINTUN_RING_TRAILING_BYTES 0x10000 -#define

WINTUN_MAX_PACKET_SIZE 0xffff -#define WINTUN_PACKET_ALIGN 4

src/openvpn/win32.h +0 -31
-
-struct tun_ring
-{
-    volatile ULONG head;
-    volatile ULONG tail;
-    volatile LONG alertable;
-    UCHAR data[WINTUN_RING_CAPACITY + WINTUN_RING_TRAILING_BYTES];
-};
-
-struct tun_register_rings
-{
-    struct
-    {
-        ULONG ring_size;
-        struct tun_ring *ring;
-        HANDLE tail_moved;
-    } send, receive;
-};
-
-struct TUN_PACKET_HEADER
-{
-    uint32_t size;
-};
-
-struct TUN_PACKET
-{
-    uint32_t size;
-    UCHAR data[WINTUN_MAX_PACKET_SIZE];
-};
-
-#define TUN_IOCTL_REGISTER_RINGS CTL_CODE(51820U, 0x970U,

METHOD_BUFFERED, FILE_READ_DATA | FILE_WRITE_DATA)

src/openvpn/win32.h +0 -7
-
 bool impersonate_as_system();
 
-bool register_ring_buffers(HANDLE device,
-                           struct tun_ring *send_ring,
-                           struct tun_ring *receive_ring,
-                           HANDLE send_tail_moved,
-                           HANDLE receive_tail_moved);
-
 #endif /* ifndef OPENVPN_WIN32_H */
 #endif /* ifdef _WIN32 */
src/openvpnserv/Makefile.am +3 -1
index bc65070b..f8d3319c 100644
@@ -36,4 +36,5 @@ openvpnserv_SOURCES = \
 	service.c service.h \
 	validate.c validate.h \
 	$(top_srcdir)/src/openvpn/block_dns.c
 $(top_srcdir)/src/openvpn/block_dns.h \
-	openvpnserv_resources.rc
+	openvpnserv_resources.rc \
+	$(top_srcdir)/src/openvpn/ring_buffer.c
+$(top_srcdir)/src/openvpn/ring_buffer.h
(quoted diff) +0 -0

b/src/openvpnserv/interactive.c index 623c3ff7..6e72a141 100644

src/openvpnserv/interactive.c +6 -4
@@ -43,13 +43,15 @@
 #include "openvpn-msg.h"
 #include "validate.h"
 #include "block_dns.h"
+#include "ring_buffer.h"
 
 #define IO_TIMEOUT  2000 /*ms*/
 
-#define ERROR_OPENVPN_STARTUP  0x20000000
-#define ERROR_STARTUP_DATA     0x20000001
-#define ERROR_MESSAGE_DATA     0x20000002
-#define ERROR_MESSAGE_TYPE     0x20000003
+#define ERROR_OPENVPN_STARTUP        0x20000000
+#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 }; @@ -58,6 +60,7 @@ static settings_t settings; static HANDLE rdns_semaphore = NULL; #define RDNS_TIMEOUT 600 /* seconds to wait for the semaphore */

src/openvpnserv/interactive.c +10 -0
+#define TUN_IOCTL_REGISTER_RINGS CTL_CODE(51820U, 0x970U,
+METHOD_BUFFERED, FILE_READ_DATA | FILE_WRITE_DATA)
 
 openvpn_service_t interactive_service = {
     interactive,
@@ -100,6 +103,14 @@ typedef struct {
     int metric_v6;
 } block_dns_data_t;
 
+typedef struct {
+    HANDLE send_ring_handle;
+    HANDLE receive_ring_handle;
+    HANDLE send_tail_moved;
+    HANDLE receive_tail_moved;
+    HANDLE device;
+} ring_buffer_handles_t;
+
 
 static DWORD
 AddListItem(list_item_t **pfirst, LPVOID data) @@ -154,6 +165,26 @@

CloseHandleEx(LPHANDLE handle)

     return INVALID_HANDLE_VALUE;

}

src/openvpnserv/interactive.c +19 -0
+static HANDLE
+OvpnUnmapViewOfFile(LPHANDLE handle)
+{
+    if (handle && *handle && *handle != INVALID_HANDLE_VALUE)
+    {
+        UnmapViewOfFile(*handle);
+        *handle = INVALID_HANDLE_VALUE;
+    }
+    return INVALID_HANDLE_VALUE;
+}
+
+static void
+CloseRingBufferHandles(ring_buffer_handles_t *ring_buffer_handles) {
+    CloseHandleEx(&ring_buffer_handles->device);
+    CloseHandleEx(&ring_buffer_handles->receive_tail_moved);
+    CloseHandleEx(&ring_buffer_handles->send_tail_moved);
+    OvpnUnmapViewOfFile(&ring_buffer_handles->send_ring_handle);
+    OvpnUnmapViewOfFile(&ring_buffer_handles->receive_ring_handle);
+}
 
 static HANDLE
 InitOverlapped(LPOVERLAPPED overlapped) @@ -1198,8 +1229,95 @@

HandleEnableDHCPMessage(const enable_dhcp_message_t *dhcp)

     return err;

}

src/openvpnserv/interactive.c +6 -0
+static DWORD
+OvpnDuplicateHandle(HANDLE ovpn_proc, HANDLE orig_handle, HANDLE*
+new_handle) {
+    DWORD err = ERROR_SUCCESS;
+
+    if (!DuplicateHandle(ovpn_proc, orig_handle, GetCurrentProcess(),

new_handle, 0, FALSE, DUPLICATE_SAME_ACCESS))

src/openvpnserv/interactive.c +20 -0
+    {
+        err = GetLastError();
+        MsgToEventLog(M_SYSERR, TEXT("Could not duplicate handle"));
+        return err;
+    }
+
+    return err;
+}
+
+static DWORD
+DuplicateAndMapRing(HANDLE ovpn_proc, HANDLE orig_handle, HANDLE
+*new_handle, struct tun_ring **ring) {
+    DWORD err = ERROR_SUCCESS;
+
+    err = OvpnDuplicateHandle(ovpn_proc, orig_handle, new_handle);
+    if (err != ERROR_SUCCESS)
+    {
+        return err;
+    }
+    *ring = (struct tun_ring *)MapViewOfFile(*new_handle,

FILE_MAP_ALL_ACCESS, 0, 0, sizeof(struct tun_ring));

src/openvpnserv/interactive.c +12 -0
+    if (*ring == NULL)
+    {
+        err = GetLastError();
+        MsgToEventLog(M_SYSERR, TEXT("Could not map shared memory"));
+        return err;
+    }
+
+    return err;
+}
+
+static DWORD
+HandleRegisterRingBuffers(const register_ring_buffers_message_t *rrb,

HANDLE ovpn_proc,

src/openvpnserv/interactive.c +8 -0
+                          ring_buffer_handles_t *ring_buffer_handles) {
+    DWORD err = 0;
+    struct tun_ring *send_ring;
+    struct tun_ring *receive_ring;
+
+    CloseRingBufferHandles(ring_buffer_handles);
+
+    err = OvpnDuplicateHandle(ovpn_proc, rrb->device,

&ring_buffer_handles->device);

src/openvpnserv/interactive.c +6 -0
+    if (err != ERROR_SUCCESS)
+    {
+        return err;
+    }
+
+    err = DuplicateAndMapRing(ovpn_proc, rrb->send_ring_handle,

&ring_buffer_handles->send_ring_handle, &send_ring);

src/openvpnserv/interactive.c +6 -0
+    if (err != ERROR_SUCCESS)
+    {
+        return err;
+    }
+
+    err = DuplicateAndMapRing(ovpn_proc, rrb->receive_ring_handle,

&ring_buffer_handles->receive_ring_handle, &receive_ring);

src/openvpnserv/interactive.c +6 -0
+    if (err != ERROR_SUCCESS)
+    {
+        return err;
+    }
+
+    err = OvpnDuplicateHandle(ovpn_proc, rrb->send_tail_moved,

&ring_buffer_handles->send_tail_moved);

src/openvpnserv/interactive.c +6 -0
+    if (err != ERROR_SUCCESS)
+    {
+        return err;
+    }
+
+    err = OvpnDuplicateHandle(ovpn_proc, rrb->receive_tail_moved,

&ring_buffer_handles->receive_tail_moved);

src/openvpnserv/interactive.c +6 -0
+    if (err != ERROR_SUCCESS)
+    {
+        return err;
+    }
+
+    if (!register_ring_buffers(ring_buffer_handles->device, send_ring,

receive_ring,
+ ring_buffer_handles->send_tail_moved,
ring_buffer_handles->receive_tail_moved))

src/openvpnserv/interactive.c +2 -0
+    {
+        MsgToEventLog(M_SYSERR, TEXT("Could not register ring

buffers"));

src/openvpnserv/interactive.c +6 -1
+        err = ERROR_REGISTER_RING_BUFFERS;
+    }
+
+    return err;
+}
+
 static VOID
-HandleMessage(HANDLE pipe, DWORD bytes, DWORD count, LPHANDLE events,

undo_lists_t *lists)
+HandleMessage(HANDLE pipe, HANDLE ovpn_proc, ring_buffer_handles_t *ring_buffer_handles,

src/openvpnserv/interactive.c +2 -0
+              DWORD bytes, DWORD count, LPHANDLE events, undo_lists_t
+*lists)
 {
     DWORD read;
     union {
@@ -1210,6 +1328,7 @@ HandleMessage(HANDLE pipe, DWORD bytes, DWORD

count, LPHANDLE events, undo_lists

         block_dns_message_t block_dns;
         dns_cfg_message_t dns;
         enable_dhcp_message_t dhcp;
src/openvpnserv/interactive.c +1 -0
+        register_ring_buffers_message_t rrb;
     } msg;
     ack_message_t ack = {
         .header = {
@@ -1277,6 +1396,13 @@ HandleMessage(HANDLE pipe, DWORD bytes, DWORD

count, LPHANDLE events, undo_lists

             }
             break;
src/openvpnserv/interactive.c +4 -0
+        case msg_register_ring_buffers:
+            if (msg.header.size == sizeof(msg.rrb))
+            {
+                ack.error_number = HandleRegisterRingBuffers(&msg.rrb,

ovpn_proc, ring_buffer_handles);

src/openvpnserv/interactive.c +3 -0
+            }
+            break;
+
         default:
             ack.error_number = ERROR_MESSAGE_TYPE;
             MsgToEventLog(MSG_FLAGS_ERROR, TEXT("Unknown message type

%d"), msg.header.type); @@ -1360,6 +1486,7 @@ RunOpenvpn(LPVOID p)

     WCHAR *cmdline = NULL;
     size_t cmdline_size;
     undo_lists_t undo_lists;

+ ring_buffer_handles_t ring_buffer_handles;

     SECURITY_ATTRIBUTES inheritable = {
         .nLength = sizeof(inheritable), @@ -1380,6 +1507,7 @@
RunOpenvpn(LPVOID p)
     ZeroMemory(&startup_info, sizeof(startup_info));
     ZeroMemory(&undo_lists, sizeof(undo_lists));
     ZeroMemory(&proc_info, sizeof(proc_info));
src/openvpnserv/interactive.c +5 -1
+    ZeroMemory(&ring_buffer_handles, sizeof(ring_buffer_handles));
 
     if (!GetStartupData(pipe, &sud))
     {
@@ -1611,7 +1739,7 @@ RunOpenvpn(LPVOID p)
             break;
         }
 
-        HandleMessage(ovpn_pipe, bytes, 1, &exit_event, &undo_lists);
+        HandleMessage(ovpn_pipe, proc_info.hProcess,
+ &ring_buffer_handles, bytes, 1, &exit_event, &undo_lists);
     }
 
     WaitForSingleObject(proc_info.hProcess, IO_TIMEOUT); @@ -1638,6
+1766,7 @@ out:
     free(cmdline);
     DestroyEnvironmentBlock(user_env);
     FreeStartupData(&sud);
+    CloseRingBufferHandles(&ring_buffer_handles);
     CloseHandleEx(&proc_info.hProcess);
     CloseHandleEx(&proc_info.hThread);
     CloseHandleEx(&stdin_read);
src/openvpnserv/openvpnserv.vcxproj +1 -0
index 7061b7b1..c5a34b87 100644
@@ -115,6 +115,7 @@
     </Link>
   </ItemDefinitionGroup>
   <ItemGroup>
+    <ClCompile Include="..\openvpn\ring_buffer.c" />
     <ClCompile Include="automatic.c" />
     <ClCompile Include="common.c" />
     <ClCompile Include="interactive.c" /> @@ -123,6 +124,7 @@
     <ClCompile Include="..\openvpn\block_dns.c" />

</ItemGroup>
<ItemGroup>
+ <ClInclude Include="..\openvpn\ring_buffer.h" />
<ClInclude Include="service.h" />
<ClInclude Include="validate.h" />
<ClInclude Include="..\openvpn\block_dns.h" /> diff --git
a/src/openvpnserv/openvpnserv.vcxproj.filters b/src/openvpnserv/openvpnserv.vcxproj.filters
index 3ce9bb24..3cb14ef6 100644

src/openvpnserv/openvpnserv.vcxproj.filters +6 -1
@@ -33,6 +33,9 @@
     <ClCompile Include="..\openvpn\block_dns.c">
       <Filter>Source Files</Filter>
     </ClCompile>
+    <ClCompile Include="..\openvpn\ring_buffer.c">
+      <Filter>Source Files</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="service.h">
@@ -44,6 +47,9 @@
     <ClInclude Include="..\openvpn\block_dns.h">
       <Filter>Header Files</Filter>
     </ClInclude>
+    <ClInclude Include="..\openvpn\ring_buffer.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ResourceCompile Include="openvpnserv_resources.rc">
--

2.17.1

_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

#69 lore raw

[Openvpn-devel] [PATCH applied] Re: wintun: interactive service support

In reply to Lev Stipakov

Your patch has been applied to the master branch.

(An ACK on v2, an ACK on v5, this is v5 :-) ).

Test built on Ubuntu 1604 (works), not actually tested anything.

Did a little bit of stare-at-code, which did not turn up anything I need to complain about... introducing a new ring_buffer.c file for less lines of code than the ring_buffer.h header file has seems a bit excessive, but since this is shared code between openvpn and iservice, it makes sense.

commit da2e66ca74688094a3dcbf8e2ce0a3bc54478a53

Author: Lev Stipakov
Date:   Tue Dec 17 14:50:41 2019 +0200
     wintun: interactive service support

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

--
kind regards,

Gert Doering

#70 lore raw

PATCH v2 6/7 wintun: set adapter properties via interactive service

In reply to Lev Stipakov

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

Since Wintun doesn't do DHCP, use interactive service calls to set up adapter properties.

This also fixes bug in previously unused IPv4 code of do_address_service():

- ipv4 address must be in network byte order
- prefix length cannot be hardcoded /32 but
must be calculated from netmask

---
 src/openvpn/route.c |  2 +-
 src/openvpn/route.h |  3 ++-
 src/openvpn/tun.c   | 77 +++++++++++++++++++++++++++++++++++++++++------------
 3 files changed, 63 insertions(+), 19 deletions(-)
src/openvpn/route.c +1 -1
index 97e90e5..cc6d551 100644
@@ -3019,7 +3019,7 @@ out:
     return ret;
 }
 
-static bool
+bool
 do_route_ipv4_service(const bool add, const struct route_ipv4 *r, const struct tuntap *tt)
 {
     DWORD if_index = windows_route_find_if_index(r, tt);
src/openvpn/route.h +2 -1
index 2e68091..27b652c 100644
@@ -321,7 +321,8 @@ void setenv_routes(struct env_set *es, const struct route_list *rl);
 
 void setenv_routes_ipv6(struct env_set *es, const struct route_ipv6_list *rl6);
 
-
+bool do_route_ipv4_service(const bool add, const struct route_ipv4 *r,
+                           const struct tuntap *tt);
 
 bool is_special_addr(const char *addr_str);
 
src/openvpn/tun.c +60 -17

171 lines not on this page · raw message

Signature · 1 line
2.7.4
Acked-by on v2 6/7
#71 lore raw

Re: [Openvpn-devel] [PATCH v2 6/7] wintun: set adapter properties via interactive service

Hi,

Quoted 9 lines

-----Original Message-----
From: Lev Stipakov [mailto:[email protected]...]
Sent: Thursday, November 7, 2019 6:45 PM
To: [email protected]
Cc: Lev Stipakov <[email protected]...>
Subject: [Openvpn-devel] [PATCH v2 6/7] wintun: set adapter properties via interactive service

static bool

src/openvpn/tun.c out:

Quoted 9 lines
src/openvpn/tun.c +2 -2
-do_dns6_service(bool add, const struct tuntap *tt)
+do_dns_service(bool add, const short family, const struct tuntap *tt)
 {
     bool ret = false;
     ack_message_t ack;
     struct gc_arena gc = gc_new();
     HANDLE pipe = tt->options.msg_channel;
-    int addr_len = add ? tt->options.dns6_len : 0;
+    int len = family == AF_INET6 ? tt->options.dns6_len :

tt->options.dns_len;

src/openvpn/tun.c out: · line 152

Quoted 4 lines
src/openvpn/tun.c +4 -0
+    int addr_len = add ? len : 0;
+    char ip_proto_name[5];
+
+    strcpy(ip_proto_name, family == AF_INET6 ? "IPv6" : "IPv4");

const char *ip_proto_name = family == AF_INET6 ? "IPv6" : "IPv4";

... to avoid the strcpy().

The rest LGTM, so...

Best regards,
Simon

#72 lore raw

PATCH v2 1/1 wintun: set adapter properties via interactive service

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

Since Wintun doesn't do DHCP, use interactive service calls to set up adapter properties.

This also fixes bug in previously unused IPv4 code of do_address_service():

- ipv4 address must be in network byte order
- prefix length cannot be hardcoded /32 but
must be calculated from netmask

---
 src/openvpn/route.c |  2 +-
 src/openvpn/route.h |  3 +-
 src/openvpn/tun.c   | 75 +++++++++++++++++++++++++++++++++++----------
 3 files changed, 61 insertions(+), 19 deletions(-)
src/openvpn/route.c +1 -1
index 97e90e56..cc6d5519 100644
@@ -3019,7 +3019,7 @@ out:
     return ret;
 }
 
-static bool
+bool
 do_route_ipv4_service(const bool add, const struct route_ipv4 *r, const struct tuntap *tt)
 {
     DWORD if_index = windows_route_find_if_index(r, tt);
src/openvpn/route.h +2 -1
index 2e68091c..27b652cd 100644
@@ -321,7 +321,8 @@ void setenv_routes(struct env_set *es, const struct route_list *rl);
 
 void setenv_routes_ipv6(struct env_set *es, const struct route_ipv6_list *rl6);
 
-
+bool do_route_ipv4_service(const bool add, const struct route_ipv4 *r,
+                           const struct tuntap *tt);
 
 bool is_special_addr(const char *addr_str);
 
src/openvpn/tun.c +58 -17

169 lines not on this page · raw message

Signature · 6 lines
2.24.1.windows.2

I have made my amends to the original Lev's patch I suggested back then.

Regards,
Simon
#73 lore raw

[Openvpn-devel] [PATCH applied] Re: wintun: set adapter properties via interactive service

Your patch has been applied to the master branch.

(This is a bit of a weird one, as the "v3" of 6/7 was sent by Simon, fixing the strcpy() part, but having "From: Lev" in it... I cannot technically accept the acked-by: in such a patch because it would be from the same person that send the new version. *But* I verified that it's really the same patch minus the strcpy() part, and "Lev as author, Simon as ACKer" is fine under these circumstances)

Code-wise I'm not exactly happy with "do_route_ipv4_service_tun()"
- we do have a function called add_route_connected_v6_net(), so if we need the same functionality for IPv4, it should be called "add_route_connected_v4_net()" or whatever, not "something completely different"... also I don't think it should hook right into do_route_ipv4_service(), which is (was...) an *internal* function in route.c for a reason - the well-defined API between tun.c and route.c is "add_route()".

Finally the patch breaks indentation rules, so might have been coded in a nicer way *or* fix the indent right away - now we have another uncrustify patch coming which has to changes code without it being what the underlying change was.

Test built on Ubuntu 1604/mingw, not tested further.

commit 1c4a47f796639da45ec7c9bf5f38a9f0bb6aa9b2

Author: Lev Stipakov
Date:   Wed Dec 18 07:18:18 2019 +0100
     wintun: set adapter properties via interactive service

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

--
kind regards,

Gert Doering

#74 lore raw

PATCH v2 7/7 wintun: clear adapter settings on tun close

In reply to Lev Stipakov

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

With tap-windows6 we clear adapter settings with DHCP, but since wintun doesn't do DHCP we do it with netsh.

---
 src/openvpn/tun.c | 79 +++++++++++++++++++++++++++++++++++--------------------
 1 file changed, 50 insertions(+), 29 deletions(-)
src/openvpn/tun.c +50 -29
index c3ea4a8..9bdd707 100644
@@ -6369,6 +6369,50 @@ tun_show_debug(struct tuntap *tt)
     }
 }
 
+static void
+netsh_delete_address_dns(const struct tuntap *tt, bool ipv6, struct gc_arena *gc)
+{
+    const char* ifconfig_ip_local;
+    struct argv argv = argv_new();
+
+    /* "store=active" is needed in Windows 8(.1) to delete the
+     * address we added (pointed out by Cedric Tabary).
+     */
+
+     /* netsh interface ipvX delete address \"%s\" %s */
+    if (ipv6)
+    {
+        ifconfig_ip_local = print_in6_addr(tt->local_ipv6, 0, gc);
+    }
+    else
+    {
+        ifconfig_ip_local = print_in_addr_t(tt->local, 0, gc);
+    }
+    argv_printf(&argv,
+                "%s%sc interface %s delete address %s %s store=active",
+                get_win_sys_path(),
+                NETSH_PATH_SUFFIX,
+                ipv6 ? "ipv6" : "ipv4",
+                tt->actual_name,
+                ifconfig_ip_local);
+
+    netsh_command(&argv, 1, M_WARN);
+
+    /* delete ipvX dns servers if any were set */
+    int len = ipv6 ? tt->options.dns6_len : tt->options.dns_len;
+    if (len > 0)
+    {
+        argv_printf(&argv,
+                    "%s%sc interface %s delete dns %s all",
+                    get_win_sys_path(),
+                    NETSH_PATH_SUFFIX,
+                    ipv6 ? "ipv6" : "ipv4",
+                    tt->actual_name);
+        netsh_command(&argv, 1, M_WARN);
+    }
+    argv_reset(&argv);
+}
+
 void
 close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx)
 {
@@ -6391,35 +6435,7 @@ close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx)
         }
         else
         {
-            const char *ifconfig_ipv6_local;
-            struct argv argv = argv_new();
-
-            /* "store=active" is needed in Windows 8(.1) to delete the
-             * address we added (pointed out by Cedric Tabary).
-             */
-
-            /* netsh interface ipv6 delete address \"%s\" %s */
-            ifconfig_ipv6_local = print_in6_addr(tt->local_ipv6, 0,  &gc);
-            argv_printf(&argv,
-                        "%s%sc interface ipv6 delete address %s %s store=active",
-                        get_win_sys_path(),
-                        NETSH_PATH_SUFFIX,
-                        tt->actual_name,
-                        ifconfig_ipv6_local);
-
-            netsh_command(&argv, 1, M_WARN);
-
-            /* delete ipv6 dns servers if any were set */
-            if (tt->options.dns6_len > 0)
-            {
-                argv_printf(&argv,
-                            "%s%sc interface ipv6 delete dns %s all",
-                            get_win_sys_path(),
-                            NETSH_PATH_SUFFIX,
-                            tt->actual_name);
-                netsh_command(&argv, 1, M_WARN);
-            }
-            argv_reset(&argv);
+            netsh_delete_address_dns(tt, true, &gc);
         }
     }
 #if 1
@@ -6441,6 +6457,11 @@ close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx)
                 strerror_win32(status, &gc));
         }
     }
+    else
+    if (tt->wintun)
+    {
+        netsh_delete_address_dns(tt, false, &gc);
+    }
 #endif
 
     dhcp_release(tt);
Signature · 1 line
2.7.4
#75 lore raw

Re: [Openvpn-devel] [PATCH v2 7/7] wintun: clear adapter settings on tun close

Hi,

Quoted 6 lines

-----Original Message-----
From: Lev Stipakov [mailto:[email protected]...]
Sent: Thursday, November 7, 2019 6:45 PM
To: [email protected]
Cc: Lev Stipakov <[email protected]...>
Subject: [Openvpn-devel] [PATCH v2 7/7] wintun: clear adapter settings on

tun

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

Quoted 9 lines

close

     }
src/openvpn/tun.c +5 -0
+    else
+    if (tt->wintun)
+    {
+        netsh_delete_address_dns(tt, false, &gc);
+    }
 #endif
    if (tt->wintun && tt->options.msg_channel)
    {
	...
    }
    else
    if (tt->ipapi_context_defined)
    {
	...
    }
    else
    if (tt->wintun)
    {
	...
    }

This construct got a bit convoluted.

Otherwise, LGTM.

Best regards,
Simon

#76 lore raw

PATCH v3 7/7 wintun: clear adapter settings on tun close

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

With tap-windows6 we clear adapter settings with DHCP, but since wintun doesn't do DHCP we do it with netsh.

---

v3:
- simplify convoluted "if" statement

v2:
- rebase on top of master

 src/openvpn/tun.c | 92 +++++++++++++++++++++++++++++------------------
 1 file changed, 57 insertions(+), 35 deletions(-)
src/openvpn/tun.c +57 -35
index 8b16cd6a..a9036a6e 100644
@@ -6388,6 +6388,50 @@ tun_show_debug(struct tuntap *tt)
     }
 }
 
+static void
+netsh_delete_address_dns(const struct tuntap *tt, bool ipv6, struct gc_arena *gc)
+{
+    const char* ifconfig_ip_local;
+    struct argv argv = argv_new();
+
+    /* "store=active" is needed in Windows 8(.1) to delete the
+     * address we added (pointed out by Cedric Tabary).
+     */
+
+     /* netsh interface ipvX delete address \"%s\" %s */
+    if (ipv6)
+    {
+        ifconfig_ip_local = print_in6_addr(tt->local_ipv6, 0, gc);
+    }
+    else
+    {
+        ifconfig_ip_local = print_in_addr_t(tt->local, 0, gc);
+    }
+    argv_printf(&argv,
+                "%s%sc interface %s delete address %s %s store=active",
+                get_win_sys_path(),
+                NETSH_PATH_SUFFIX,
+                ipv6 ? "ipv6" : "ipv4",
+                tt->actual_name,
+                ifconfig_ip_local);
+
+    netsh_command(&argv, 1, M_WARN);
+
+    /* delete ipvX dns servers if any were set */
+    int len = ipv6 ? tt->options.dns6_len : tt->options.dns_len;
+    if (len > 0)
+    {
+        argv_printf(&argv,
+                    "%s%sc interface %s delete dns %s all",
+                    get_win_sys_path(),
+                    NETSH_PATH_SUFFIX,
+                    ipv6 ? "ipv6" : "ipv4",
+                    tt->actual_name);
+        netsh_command(&argv, 1, M_WARN);
+    }
+    argv_reset(&argv);
+}
+
 void
 close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx)
 {
@@ -6410,46 +6454,24 @@ close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx)
         }
         else
         {
-            const char *ifconfig_ipv6_local;
-            struct argv argv = argv_new();
-
-            /* "store=active" is needed in Windows 8(.1) to delete the
-             * address we added (pointed out by Cedric Tabary).
-             */
-
-            /* netsh interface ipv6 delete address \"%s\" %s */
-            ifconfig_ipv6_local = print_in6_addr(tt->local_ipv6, 0,  &gc);
-            argv_printf(&argv,
-                        "%s%sc interface ipv6 delete address %s %s store=active",
-                        get_win_sys_path(),
-                        NETSH_PATH_SUFFIX,
-                        tt->actual_name,
-                        ifconfig_ipv6_local);
-
-            netsh_command(&argv, 1, M_WARN);
-
-            /* delete ipv6 dns servers if any were set */
-            if (tt->options.dns6_len > 0)
-            {
-                argv_printf(&argv,
-                            "%s%sc interface ipv6 delete dns %s all",
-                            get_win_sys_path(),
-                            NETSH_PATH_SUFFIX,
-                            tt->actual_name);
-                netsh_command(&argv, 1, M_WARN);
-            }
-            argv_reset(&argv);
+            netsh_delete_address_dns(tt, true, &gc);
         }
     }
 #if 1
-    if (tt->wintun && tt->options.msg_channel)
+    if (tt->wintun)
     {
-        do_route_ipv4_service_tun(false, tt);
-        do_address_service(false, AF_INET, tt);
-        do_dns_service(false, AF_INET, tt);
+        if (tt->options.msg_channel)
+        {
+            do_route_ipv4_service_tun(false, tt);
+            do_address_service(false, AF_INET, tt);
+            do_dns_service(false, AF_INET, tt);
+        }
+        else
+        {
+            netsh_delete_address_dns(tt, false, &gc);
+        }
     }
-    else
-    if (tt->ipapi_context_defined)
+    else if (tt->ipapi_context_defined)
     {
         DWORD status;
         if ((status = DeleteIPAddress(tt->ipapi_context)) != NO_ERROR)
Signature · 1 line
2.17.1
#77 lore raw

Re: [Openvpn-devel] [PATCH v3 7/7] wintun: clear adapter settings on tun close

Hi,

Probably this is the only one in the series without an ACK.

v2 was reviewed by Simon and suggested changes are in here. This looks good to me.

Quoted 23 lines · Lev Stipakov

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

With tap-windows6 we clear adapter settings with DHCP, but since wintun doesn't do DHCP we do it with netsh.

---

v3:
- simplify convoluted "if" statement

v2:
- rebase on top of master

 src/openvpn/tun.c | 92 +++++++++++++++++++++++++++++------------------
 1 file changed, 57 insertions(+), 35 deletions(-)
src/openvpn/tun.c +0 -0
index 8b16cd6a..a9036a6e 100644

src/openvpn/tun.c tun_show_debug(struct tuntap *tt) · line 6394

Quoted 7 lines
src/openvpn/tun.c +4 -0
@@ -6388,6 +6388,50 @@ tun_show_debug(struct tuntap *tt)
     }
 }
 
+static void
+netsh_delete_address_dns(const struct tuntap *tt, bool ipv6, struct gc_arena *gc)
+{
+    const char* ifconfig_ip_local;

A minor nit: char *foo is our recommended style, I think.

Quoted 93 lines
src/openvpn/tun.c +49 -33
+    struct argv argv = argv_new();
+
+    /* "store=active" is needed in Windows 8(.1) to delete the
+     * address we added (pointed out by Cedric Tabary).
+     */
+
+     /* netsh interface ipvX delete address \"%s\" %s */
+    if (ipv6)
+    {
+        ifconfig_ip_local = print_in6_addr(tt->local_ipv6, 0, gc);
+    }
+    else
+    {
+        ifconfig_ip_local = print_in_addr_t(tt->local, 0, gc);
+    }
+    argv_printf(&argv,
+                "%s%sc interface %s delete address %s %s store=active",
+                get_win_sys_path(),
+                NETSH_PATH_SUFFIX,
+                ipv6 ? "ipv6" : "ipv4",
+                tt->actual_name,
+                ifconfig_ip_local);
+
+    netsh_command(&argv, 1, M_WARN);
+
+    /* delete ipvX dns servers if any were set */
+    int len = ipv6 ? tt->options.dns6_len : tt->options.dns_len;
+    if (len > 0)
+    {
+        argv_printf(&argv,
+                    "%s%sc interface %s delete dns %s all",
+                    get_win_sys_path(),
+                    NETSH_PATH_SUFFIX,
+                    ipv6 ? "ipv6" : "ipv4",
+                    tt->actual_name);
+        netsh_command(&argv, 1, M_WARN);
+    }
+    argv_reset(&argv);
+}
+
 void
 close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx)
 {
@@ -6410,46 +6454,24 @@ close_tun(struct tuntap *tt, openvpn_net_ctx_t *ctx)
         }
         else
         {
-            const char *ifconfig_ipv6_local;
-            struct argv argv = argv_new();
-
-            /* "store=active" is needed in Windows 8(.1) to delete the
-             * address we added (pointed out by Cedric Tabary).
-             */
-
-            /* netsh interface ipv6 delete address \"%s\" %s */
-            ifconfig_ipv6_local = print_in6_addr(tt->local_ipv6, 0,  &gc);
-            argv_printf(&argv,
-                        "%s%sc interface ipv6 delete address %s %s store=active",
-                        get_win_sys_path(),
-                        NETSH_PATH_SUFFIX,
-                        tt->actual_name,
-                        ifconfig_ipv6_local);
-
-            netsh_command(&argv, 1, M_WARN);
-
-            /* delete ipv6 dns servers if any were set */
-            if (tt->options.dns6_len > 0)
-            {
-                argv_printf(&argv,
-                            "%s%sc interface ipv6 delete dns %s all",
-                            get_win_sys_path(),
-                            NETSH_PATH_SUFFIX,
-                            tt->actual_name);
-                netsh_command(&argv, 1, M_WARN);
-            }
-            argv_reset(&argv);
+            netsh_delete_address_dns(tt, true, &gc);
         }
     }
 #if 1
-    if (tt->wintun && tt->options.msg_channel)
+    if (tt->wintun)
     {
-        do_route_ipv4_service_tun(false, tt);
-        do_address_service(false, AF_INET, tt);
-        do_dns_service(false, AF_INET, tt);
+        if (tt->options.msg_channel)
+        {
+            do_route_ipv4_service_tun(false, tt);
+            do_address_service(false, AF_INET, tt);
+            do_dns_service(false, AF_INET, tt);
+        }
+        else

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

Quoted 10 lines
src/openvpn/tun.c +4 -2
+        {
+            netsh_delete_address_dns(tt, false, &gc);
+        }
     }
-    else
-    if (tt->ipapi_context_defined)
+    else if (tt->ipapi_context_defined)
     {
         DWORD status;
         if ((status = DeleteIPAddress(tt->ipapi_context)) != NO_ERROR)

Acked by: [email protected]...

Selva

#78 lore raw

[Openvpn-devel] [PATCH applied] Re: wintun: clear adapter settings on tun close

In reply to Lev Stipakov

Your patch has been applied to the master branch.

Fixed the "char *foo" comment Selva had on the fly.

Test built on Ubuntu 1604/mingw. No further tests.

Stared a bit at the code. Not sure I like the way this overloads v4/v6 into the same function just to be able to share a bit of common code (at the expense of lots of conditionals which make the resulting function not easy to read, and a local variable "ifconfig_ip_local" which could be IPv4 or IPv6 now). Antonio has just *separated* IPv4 and IPv6 ifconfig into two independent functions to make the code more readable...

commit ef9b34044ec354902cd24bbcee244c6a494b6c7c

Author: Lev Stipakov
Date:   Tue Nov 12 16:44:00 2019 +0200
     wintun: clear adapter settings on tun close

     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]/msg19124.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering