aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2019-07-29 10:39:47 +0000
committerJason A. Donenfeld <Jason@zx2c4.com>2019-08-02 09:43:32 +0000
commit28ba2d46001b79fb860b3fda821adb8a6b3f34c4 (patch)
treefadb460d22e69479d2f96561e3e9887f173668aa
parentRewrite installer logic in C (diff)
downloadwintun-28ba2d46001b79fb860b3fda821adb8a6b3f34c4.tar.xz
wintun-28ba2d46001b79fb860b3fda821adb8a6b3f34c4.zip
Separate out atomic helpers
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
-rw-r--r--atomic.h54
-rw-r--r--wintun.c70
-rw-r--r--wintun.proj6
-rw-r--r--wintun.vcxproj3
-rw-r--r--wintun.vcxproj.filters3
5 files changed, 71 insertions, 65 deletions
diff --git a/atomic.h b/atomic.h
new file mode 100644
index 0000000..01dd35e
--- /dev/null
+++ b/atomic.h
@@ -0,0 +1,54 @@
+/* SPDX-License-Identifier: GPL-2.0
+ *
+ * Copyright (C) 2018-2019 WireGuard LLC. All Rights Reserved.
+ */
+
+#pragma once
+
+#include <wdm.h>
+
+static __forceinline VOID
+InterlockedSet(_Inout_ _Interlocked_operand_ LONG volatile *Target, _In_ LONG Value)
+{
+ *Target = Value;
+}
+
+static __forceinline VOID
+InterlockedSetU(_Inout_ _Interlocked_operand_ ULONG volatile *Target, _In_ ULONG Value)
+{
+ *Target = Value;
+}
+
+static __forceinline VOID
+InterlockedSetPointer(_Inout_ _Interlocked_operand_ VOID *volatile *Target, _In_opt_ VOID *Value)
+{
+ *Target = Value;
+}
+
+static __forceinline LONG
+InterlockedGet(_In_ _Interlocked_operand_ LONG volatile *Value)
+{
+ return *Value;
+}
+
+static __forceinline ULONG
+InterlockedGetU(_In_ _Interlocked_operand_ ULONG volatile *Value)
+{
+ return *Value;
+}
+
+static __forceinline PVOID
+InterlockedGetPointer(_In_ _Interlocked_operand_ PVOID volatile *Value)
+{
+ return *Value;
+}
+
+static __forceinline LONG64
+InterlockedGet64(_In_ _Interlocked_operand_ LONG64 volatile *Value)
+{
+#ifdef _WIN64
+ return *Value;
+#else
+ return InterlockedCompareExchange64(Value, 0, 0);
+#endif
+} \ No newline at end of file
diff --git a/wintun.c b/wintun.c
index 0c83d6c..fae78bc 100644
--- a/wintun.c
+++ b/wintun.c
@@ -9,6 +9,7 @@
#include <ndis.h>
#include <ntstrsafe.h>
#include "undocumented.h"
+#include "atomic.h"
#pragma warning(disable : 4100) /* unreferenced formal parameter */
#pragma warning(disable : 4200) /* nonstandard: zero-sized array in struct/union */
@@ -41,18 +42,18 @@
#define TUN_RING_WRAP(Value, Capacity) ((Value) & (Capacity - 1))
#if REG_DWORD == REG_DWORD_BIG_ENDIAN
-# define TUN_HTONS(x) ((USHORT)(x))
-# define TUN_HTONL(x) ((ULONG)(x))
+# define HTONS(x) ((USHORT)(x))
+# define HTONL(x) ((ULONG)(x))
#elif REG_DWORD == REG_DWORD_LITTLE_ENDIAN
-# define TUN_HTONS(x) ((((USHORT)(x)&0x00ff) << 8) | (((USHORT)(x)&0xff00) >> 8))
-# define TUN_HTONL(x) \
+# define HTONS(x) ((((USHORT)(x)&0x00ff) << 8) | (((USHORT)(x)&0xff00) >> 8))
+# define HTONL(x) \
((((ULONG)(x)&0x000000ff) << 24) | (((ULONG)(x)&0x0000ff00) << 8) | (((ULONG)(x)&0x00ff0000) >> 8) | \
(((ULONG)(x)&0xff000000) >> 24))
#else
# error "Unable to determine endianess"
#endif
-#define TUN_MEMORY_TAG TUN_HTONL('wtun')
+#define TUN_MEMORY_TAG HTONL('wtun')
typedef struct _TUN_PACKET
{
@@ -163,59 +164,6 @@ static DRIVER_DISPATCH *NdisDispatchDeviceControl, *NdisDispatchClose;
static ERESOURCE TunDispatchCtxGuard;
static SECURITY_DESCRIPTOR *TunDispatchSecurityDescriptor;
-static __forceinline VOID
-InterlockedSet(_Inout_ _Interlocked_operand_ LONG volatile *Target, _In_ LONG Value)
-{
- *Target = Value;
-}
-
-static __forceinline VOID
-InterlockedSetU(_Inout_ _Interlocked_operand_ ULONG volatile *Target, _In_ ULONG Value)
-{
- *Target = Value;
-}
-
-static __forceinline VOID
-InterlockedSetPointer(_Inout_ _Interlocked_operand_ VOID *volatile *Target, _In_opt_ VOID *Value)
-{
- *Target = Value;
-}
-
-static __forceinline LONG
-InterlockedGet(_In_ _Interlocked_operand_ LONG volatile *Value)
-{
- return *Value;
-}
-
-static __forceinline ULONG
-InterlockedGetU(_In_ _Interlocked_operand_ ULONG volatile *Value)
-{
- return *Value;
-}
-
-static __forceinline PVOID
-InterlockedGetPointer(_In_ _Interlocked_operand_ PVOID volatile *Value)
-{
- return *Value;
-}
-
-static __forceinline LONG64
-InterlockedGet64(_In_ _Interlocked_operand_ LONG64 volatile *Value)
-{
-#ifdef _WIN64
- return *Value;
-#else
- return InterlockedCompareExchange64(Value, 0, 0);
-#endif
-}
-
-#define TunInitUnicodeString(str, buf) \
- { \
- (str)->Length = 0; \
- (str)->MaximumLength = sizeof(buf); \
- (str)->Buffer = buf; \
- }
-
_IRQL_requires_max_(DISPATCH_LEVEL)
static VOID
TunIndicateStatus(_In_ NDIS_HANDLE MiniportAdapterHandle, _In_ NDIS_MEDIA_CONNECT_STATE MediaConnectState)
@@ -537,12 +485,12 @@ TunProcessReceiveData(_Inout_ TUN_CTX *Ctx)
if (PacketSize >= 20 && Packet->Data[0] >> 4 == 4)
{
NblFlags = NDIS_NBL_FLAGS_IS_IPV4;
- NblProto = TUN_HTONS(NDIS_ETH_TYPE_IPV4);
+ NblProto = HTONS(NDIS_ETH_TYPE_IPV4);
}
else if (PacketSize >= 40 && Packet->Data[0] >> 4 == 6)
{
NblFlags = NDIS_NBL_FLAGS_IS_IPV6;
- NblProto = TUN_HTONS(NDIS_ETH_TYPE_IPV6);
+ NblProto = HTONS(NDIS_ETH_TYPE_IPV6);
}
else
break;
@@ -1227,7 +1175,7 @@ TunOidQuery(_Inout_ TUN_CTX *Ctx, _Inout_ NDIS_OID_REQUEST *OidRequest)
return TunOidQueryWrite(OidRequest, TUN_MAX_RING_CAPACITY);
case OID_GEN_VENDOR_ID:
- return TunOidQueryWrite(OidRequest, TUN_HTONL(TUN_VENDOR_ID));
+ return TunOidQueryWrite(OidRequest, HTONL(TUN_VENDOR_ID));
case OID_GEN_VENDOR_DESCRIPTION:
return TunOidQueryWriteBuf(OidRequest, TUN_VENDOR_NAME, (ULONG)sizeof(TUN_VENDOR_NAME));
diff --git a/wintun.proj b/wintun.proj
index 2901616..87cf39c 100644
--- a/wintun.proj
+++ b/wintun.proj
@@ -30,17 +30,17 @@
-->
<Target Name="Driver-x86"
Outputs="x86\Release\wintun\wintun.sys;x86\Release\wintun\wintun.inf;x86\Release\wintun\wintun.cat"
- Inputs="undocumented.h;wintun.c;wintun.inf;wintun.props;wintun.rc;wintun.vcxproj">
+ Inputs="atomic.h;undocumented.h;wintun.c;wintun.inf;wintun.props;wintun.rc;wintun.vcxproj">
<MSBuild Projects="wintun.vcxproj" Targets="Build" Properties="Configuration=Release;Platform=Win32"/>
</Target>
<Target Name="Driver-amd64"
Outputs="amd64\Release\wintun\wintun.sys;amd64\Release\wintun\wintun.inf;amd64\Release\wintun\wintun.cat"
- Inputs="undocumented.h;wintun.c;wintun.inf;wintun.props;wintun.rc;wintun.vcxproj">
+ Inputs="atomic.h;undocumented.h;wintun.c;wintun.inf;wintun.props;wintun.rc;wintun.vcxproj">
<MSBuild Projects="wintun.vcxproj" Targets="Build" Properties="Configuration=Release;Platform=x64"/>
</Target>
<Target Name="Driver-arm64"
Outputs="arm64\Release\wintun\wintun.sys;arm64\Release\wintun\wintun.inf;arm64\Release\wintun\wintun.cat"
- Inputs="undocumented.h;wintun.c;wintun.inf;wintun.props;wintun.rc;wintun.vcxproj">
+ Inputs="atomic.h;undocumented.h;wintun.c;wintun.inf;wintun.props;wintun.rc;wintun.vcxproj">
<MSBuild Projects="wintun.vcxproj" Targets="Build" Properties="Configuration=Release;Platform=ARM64"/>
</Target>
diff --git a/wintun.vcxproj b/wintun.vcxproj
index a8deb98..c70f032 100644
--- a/wintun.vcxproj
+++ b/wintun.vcxproj
@@ -170,8 +170,9 @@
<FilesToPackage Include="$(TargetPath)" Condition="'$(ConfigurationType)'=='Driver' or '$(ConfigurationType)'=='DynamicLibrary'" />
</ItemGroup>
<ItemGroup>
+ <ClInclude Include="atomic.h" />
<ClInclude Include="undocumented.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets" />
-</Project>
+</Project> \ No newline at end of file
diff --git a/wintun.vcxproj.filters b/wintun.vcxproj.filters
index 3e19120..467cc48 100644
--- a/wintun.vcxproj.filters
+++ b/wintun.vcxproj.filters
@@ -33,5 +33,8 @@
<ClInclude Include="undocumented.h">
<Filter>Header Files</Filter>
</ClInclude>
+ <ClInclude Include="atomic.h">
+ <Filter>Header Files</Filter>
+ </ClInclude>
</ItemGroup>
</Project> \ No newline at end of file