Separate out atomic helpers
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
parent
22e2da002d
commit
28ba2d4600
54
atomic.h
Normal file
54
atomic.h
Normal file
@ -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
|
||||||
|
}
|
70
wintun.c
70
wintun.c
@ -9,6 +9,7 @@
|
|||||||
#include <ndis.h>
|
#include <ndis.h>
|
||||||
#include <ntstrsafe.h>
|
#include <ntstrsafe.h>
|
||||||
#include "undocumented.h"
|
#include "undocumented.h"
|
||||||
|
#include "atomic.h"
|
||||||
|
|
||||||
#pragma warning(disable : 4100) /* unreferenced formal parameter */
|
#pragma warning(disable : 4100) /* unreferenced formal parameter */
|
||||||
#pragma warning(disable : 4200) /* nonstandard: zero-sized array in struct/union */
|
#pragma warning(disable : 4200) /* nonstandard: zero-sized array in struct/union */
|
||||||
@ -41,18 +42,18 @@
|
|||||||
#define TUN_RING_WRAP(Value, Capacity) ((Value) & (Capacity - 1))
|
#define TUN_RING_WRAP(Value, Capacity) ((Value) & (Capacity - 1))
|
||||||
|
|
||||||
#if REG_DWORD == REG_DWORD_BIG_ENDIAN
|
#if REG_DWORD == REG_DWORD_BIG_ENDIAN
|
||||||
# define TUN_HTONS(x) ((USHORT)(x))
|
# define HTONS(x) ((USHORT)(x))
|
||||||
# define TUN_HTONL(x) ((ULONG)(x))
|
# define HTONL(x) ((ULONG)(x))
|
||||||
#elif REG_DWORD == REG_DWORD_LITTLE_ENDIAN
|
#elif REG_DWORD == REG_DWORD_LITTLE_ENDIAN
|
||||||
# define TUN_HTONS(x) ((((USHORT)(x)&0x00ff) << 8) | (((USHORT)(x)&0xff00) >> 8))
|
# define HTONS(x) ((((USHORT)(x)&0x00ff) << 8) | (((USHORT)(x)&0xff00) >> 8))
|
||||||
# define TUN_HTONL(x) \
|
# define HTONL(x) \
|
||||||
((((ULONG)(x)&0x000000ff) << 24) | (((ULONG)(x)&0x0000ff00) << 8) | (((ULONG)(x)&0x00ff0000) >> 8) | \
|
((((ULONG)(x)&0x000000ff) << 24) | (((ULONG)(x)&0x0000ff00) << 8) | (((ULONG)(x)&0x00ff0000) >> 8) | \
|
||||||
(((ULONG)(x)&0xff000000) >> 24))
|
(((ULONG)(x)&0xff000000) >> 24))
|
||||||
#else
|
#else
|
||||||
# error "Unable to determine endianess"
|
# error "Unable to determine endianess"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define TUN_MEMORY_TAG TUN_HTONL('wtun')
|
#define TUN_MEMORY_TAG HTONL('wtun')
|
||||||
|
|
||||||
typedef struct _TUN_PACKET
|
typedef struct _TUN_PACKET
|
||||||
{
|
{
|
||||||
@ -163,59 +164,6 @@ static DRIVER_DISPATCH *NdisDispatchDeviceControl, *NdisDispatchClose;
|
|||||||
static ERESOURCE TunDispatchCtxGuard;
|
static ERESOURCE TunDispatchCtxGuard;
|
||||||
static SECURITY_DESCRIPTOR *TunDispatchSecurityDescriptor;
|
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)
|
_IRQL_requires_max_(DISPATCH_LEVEL)
|
||||||
static VOID
|
static VOID
|
||||||
TunIndicateStatus(_In_ NDIS_HANDLE MiniportAdapterHandle, _In_ NDIS_MEDIA_CONNECT_STATE MediaConnectState)
|
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)
|
if (PacketSize >= 20 && Packet->Data[0] >> 4 == 4)
|
||||||
{
|
{
|
||||||
NblFlags = NDIS_NBL_FLAGS_IS_IPV4;
|
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)
|
else if (PacketSize >= 40 && Packet->Data[0] >> 4 == 6)
|
||||||
{
|
{
|
||||||
NblFlags = NDIS_NBL_FLAGS_IS_IPV6;
|
NblFlags = NDIS_NBL_FLAGS_IS_IPV6;
|
||||||
NblProto = TUN_HTONS(NDIS_ETH_TYPE_IPV6);
|
NblProto = HTONS(NDIS_ETH_TYPE_IPV6);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
break;
|
break;
|
||||||
@ -1227,7 +1175,7 @@ TunOidQuery(_Inout_ TUN_CTX *Ctx, _Inout_ NDIS_OID_REQUEST *OidRequest)
|
|||||||
return TunOidQueryWrite(OidRequest, TUN_MAX_RING_CAPACITY);
|
return TunOidQueryWrite(OidRequest, TUN_MAX_RING_CAPACITY);
|
||||||
|
|
||||||
case OID_GEN_VENDOR_ID:
|
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:
|
case OID_GEN_VENDOR_DESCRIPTION:
|
||||||
return TunOidQueryWriteBuf(OidRequest, TUN_VENDOR_NAME, (ULONG)sizeof(TUN_VENDOR_NAME));
|
return TunOidQueryWriteBuf(OidRequest, TUN_VENDOR_NAME, (ULONG)sizeof(TUN_VENDOR_NAME));
|
||||||
|
@ -30,17 +30,17 @@
|
|||||||
-->
|
-->
|
||||||
<Target Name="Driver-x86"
|
<Target Name="Driver-x86"
|
||||||
Outputs="x86\Release\wintun\wintun.sys;x86\Release\wintun\wintun.inf;x86\Release\wintun\wintun.cat"
|
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"/>
|
<MSBuild Projects="wintun.vcxproj" Targets="Build" Properties="Configuration=Release;Platform=Win32"/>
|
||||||
</Target>
|
</Target>
|
||||||
<Target Name="Driver-amd64"
|
<Target Name="Driver-amd64"
|
||||||
Outputs="amd64\Release\wintun\wintun.sys;amd64\Release\wintun\wintun.inf;amd64\Release\wintun\wintun.cat"
|
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"/>
|
<MSBuild Projects="wintun.vcxproj" Targets="Build" Properties="Configuration=Release;Platform=x64"/>
|
||||||
</Target>
|
</Target>
|
||||||
<Target Name="Driver-arm64"
|
<Target Name="Driver-arm64"
|
||||||
Outputs="arm64\Release\wintun\wintun.sys;arm64\Release\wintun\wintun.inf;arm64\Release\wintun\wintun.cat"
|
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"/>
|
<MSBuild Projects="wintun.vcxproj" Targets="Build" Properties="Configuration=Release;Platform=ARM64"/>
|
||||||
</Target>
|
</Target>
|
||||||
|
|
||||||
|
@ -170,8 +170,9 @@
|
|||||||
<FilesToPackage Include="$(TargetPath)" Condition="'$(ConfigurationType)'=='Driver' or '$(ConfigurationType)'=='DynamicLibrary'" />
|
<FilesToPackage Include="$(TargetPath)" Condition="'$(ConfigurationType)'=='Driver' or '$(ConfigurationType)'=='DynamicLibrary'" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClInclude Include="atomic.h" />
|
||||||
<ClInclude Include="undocumented.h" />
|
<ClInclude Include="undocumented.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
<ImportGroup Label="ExtensionTargets" />
|
<ImportGroup Label="ExtensionTargets" />
|
||||||
</Project>
|
</Project>
|
@ -33,5 +33,8 @@
|
|||||||
<ClInclude Include="undocumented.h">
|
<ClInclude Include="undocumented.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="atomic.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
Loading…
Reference in New Issue
Block a user