2020-10-24 08:28:17 +02:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0
|
|
|
|
*
|
2021-01-30 16:45:26 +01:00
|
|
|
* Copyright (C) 2018-2021 WireGuard LLC. All Rights Reserved.
|
2020-10-24 08:28:17 +02:00
|
|
|
*/
|
|
|
|
|
2020-10-31 11:55:26 +01:00
|
|
|
#include "adapter.h"
|
|
|
|
#include "elevate.h"
|
|
|
|
#include "entry.h"
|
|
|
|
#include "logger.h"
|
|
|
|
#include "wintun.h"
|
|
|
|
#include <Windows.h>
|
2020-10-24 08:28:17 +02:00
|
|
|
|
|
|
|
#pragma warning(disable : 4200) /* nonstandard: zero-sized array in struct/union */
|
|
|
|
|
|
|
|
#define TUN_ALIGNMENT sizeof(ULONG)
|
|
|
|
#define TUN_ALIGN(Size) (((ULONG)(Size) + ((ULONG)TUN_ALIGNMENT - 1)) & ~((ULONG)TUN_ALIGNMENT - 1))
|
|
|
|
#define TUN_IS_ALIGNED(Size) (!((ULONG)(Size) & ((ULONG)TUN_ALIGNMENT - 1)))
|
|
|
|
#define TUN_MAX_PACKET_SIZE TUN_ALIGN(sizeof(TUN_PACKET) + WINTUN_MAX_IP_PACKET_SIZE)
|
|
|
|
#define TUN_RING_CAPACITY(Size) ((Size) - sizeof(TUN_RING) - (TUN_MAX_PACKET_SIZE - TUN_ALIGNMENT))
|
|
|
|
#define TUN_RING_SIZE(Capacity) (sizeof(TUN_RING) + (Capacity) + (TUN_MAX_PACKET_SIZE - TUN_ALIGNMENT))
|
|
|
|
#define TUN_RING_WRAP(Value, Capacity) ((Value) & (Capacity - 1))
|
2020-10-25 10:01:35 +01:00
|
|
|
#define LOCK_SPIN_COUNT 0x10000
|
|
|
|
#define TUN_PACKET_RELEASE ((DWORD)0x80000000)
|
2020-10-24 08:28:17 +02:00
|
|
|
|
|
|
|
typedef struct _TUN_PACKET
|
|
|
|
{
|
|
|
|
ULONG Size;
|
2020-10-25 10:01:35 +01:00
|
|
|
UCHAR Data[];
|
2020-10-24 08:28:17 +02:00
|
|
|
} TUN_PACKET;
|
|
|
|
|
|
|
|
typedef struct _TUN_RING
|
|
|
|
{
|
|
|
|
volatile ULONG Head;
|
|
|
|
volatile ULONG Tail;
|
|
|
|
volatile LONG Alertable;
|
|
|
|
UCHAR Data[];
|
|
|
|
} TUN_RING;
|
|
|
|
|
|
|
|
#define TUN_IOCTL_REGISTER_RINGS CTL_CODE(51820U, 0x970U, METHOD_BUFFERED, FILE_READ_DATA | FILE_WRITE_DATA)
|
|
|
|
|
|
|
|
typedef struct _TUN_REGISTER_RINGS
|
|
|
|
{
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
ULONG RingSize;
|
|
|
|
TUN_RING *Ring;
|
|
|
|
HANDLE TailMoved;
|
|
|
|
} Send, Receive;
|
|
|
|
} TUN_REGISTER_RINGS;
|
|
|
|
|
|
|
|
typedef struct _TUN_SESSION
|
|
|
|
{
|
|
|
|
ULONG Capacity;
|
2020-10-25 10:01:35 +01:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
ULONG Tail;
|
|
|
|
ULONG TailRelease;
|
|
|
|
ULONG PacketsToRelease;
|
|
|
|
CRITICAL_SECTION Lock;
|
|
|
|
} Receive;
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
ULONG Head;
|
|
|
|
ULONG HeadRelease;
|
|
|
|
ULONG PacketsToRelease;
|
|
|
|
CRITICAL_SECTION Lock;
|
|
|
|
} Send;
|
2020-10-24 08:28:17 +02:00
|
|
|
TUN_REGISTER_RINGS Descriptor;
|
|
|
|
HANDLE Handle;
|
|
|
|
} TUN_SESSION;
|
|
|
|
|
2020-11-03 12:29:34 +01:00
|
|
|
_Return_type_success_(return != NULL) TUN_SESSION *WINAPI
|
|
|
|
WintunStartSession(_In_ const WINTUN_ADAPTER *Adapter, _In_ DWORD Capacity)
|
2020-10-24 08:28:17 +02:00
|
|
|
{
|
2020-11-03 12:29:34 +01:00
|
|
|
DWORD LastError;
|
2020-11-04 12:55:25 +01:00
|
|
|
TUN_SESSION *Session = Zalloc(sizeof(TUN_SESSION));
|
2020-11-03 12:29:34 +01:00
|
|
|
if (!Session)
|
|
|
|
{
|
2020-11-04 12:55:25 +01:00
|
|
|
LastError = GetLastError();
|
2021-06-24 12:12:13 +02:00
|
|
|
goto cleanup;
|
2020-11-03 12:29:34 +01:00
|
|
|
}
|
2020-10-24 08:28:17 +02:00
|
|
|
const ULONG RingSize = TUN_RING_SIZE(Capacity);
|
|
|
|
BYTE *AllocatedRegion = VirtualAlloc(0, (size_t)RingSize * 2, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
|
|
|
|
if (!AllocatedRegion)
|
|
|
|
{
|
2021-02-02 13:12:45 +01:00
|
|
|
LastError = LOG_LAST_ERROR(L"Failed to allocate ring memory (requested size: 0x%zx)", (size_t)RingSize * 2);
|
2020-10-24 08:28:17 +02:00
|
|
|
goto cleanupRings;
|
|
|
|
}
|
2020-11-03 12:29:34 +01:00
|
|
|
Session->Descriptor.Send.RingSize = RingSize;
|
|
|
|
Session->Descriptor.Send.Ring = (TUN_RING *)AllocatedRegion;
|
|
|
|
Session->Descriptor.Send.TailMoved = CreateEventW(&SecurityAttributes, FALSE, FALSE, NULL);
|
|
|
|
if (!Session->Descriptor.Send.TailMoved)
|
2020-10-24 08:28:17 +02:00
|
|
|
{
|
2020-11-03 12:29:34 +01:00
|
|
|
LastError = LOG_LAST_ERROR(L"Failed to create send event");
|
2021-06-24 12:12:13 +02:00
|
|
|
goto cleanupAllocatedRegion;
|
2020-10-24 08:28:17 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 12:29:34 +01:00
|
|
|
Session->Descriptor.Receive.RingSize = RingSize;
|
|
|
|
Session->Descriptor.Receive.Ring = (TUN_RING *)(AllocatedRegion + RingSize);
|
|
|
|
Session->Descriptor.Receive.TailMoved = CreateEventW(&SecurityAttributes, FALSE, FALSE, NULL);
|
|
|
|
if (!Session->Descriptor.Receive.TailMoved)
|
2020-10-24 08:28:17 +02:00
|
|
|
{
|
2020-11-03 12:29:34 +01:00
|
|
|
LastError = LOG_LAST_ERROR(L"Failed to create receive event");
|
2020-10-24 08:28:17 +02:00
|
|
|
goto cleanupSendTailMoved;
|
|
|
|
}
|
|
|
|
|
2020-11-05 15:25:52 +01:00
|
|
|
Session->Handle = AdapterOpenDeviceObject(Adapter);
|
2020-11-03 12:29:34 +01:00
|
|
|
if (Session->Handle == INVALID_HANDLE_VALUE)
|
2020-10-24 08:28:17 +02:00
|
|
|
{
|
2020-11-03 12:29:34 +01:00
|
|
|
LastError = LOG(WINTUN_LOG_ERR, L"Failed to open adapter device object");
|
2020-10-24 08:28:17 +02:00
|
|
|
goto cleanupReceiveTailMoved;
|
|
|
|
}
|
|
|
|
DWORD BytesReturned;
|
|
|
|
if (!DeviceIoControl(
|
2020-11-03 12:29:34 +01:00
|
|
|
Session->Handle,
|
2020-10-30 13:08:57 +01:00
|
|
|
TUN_IOCTL_REGISTER_RINGS,
|
2020-11-03 12:29:34 +01:00
|
|
|
&Session->Descriptor,
|
2020-10-30 13:08:57 +01:00
|
|
|
sizeof(TUN_REGISTER_RINGS),
|
|
|
|
NULL,
|
|
|
|
0,
|
|
|
|
&BytesReturned,
|
2020-10-24 08:28:17 +02:00
|
|
|
NULL))
|
|
|
|
{
|
2020-11-03 12:29:34 +01:00
|
|
|
LastError = LOG_LAST_ERROR(L"Failed to register rings");
|
2020-10-24 08:28:17 +02:00
|
|
|
goto cleanupHandle;
|
|
|
|
}
|
2020-11-03 12:29:34 +01:00
|
|
|
Session->Capacity = Capacity;
|
|
|
|
(void)InitializeCriticalSectionAndSpinCount(&Session->Receive.Lock, LOCK_SPIN_COUNT);
|
|
|
|
(void)InitializeCriticalSectionAndSpinCount(&Session->Send.Lock, LOCK_SPIN_COUNT);
|
|
|
|
return Session;
|
2020-10-24 08:28:17 +02:00
|
|
|
cleanupHandle:
|
2020-11-03 12:29:34 +01:00
|
|
|
CloseHandle(Session->Handle);
|
2020-10-24 08:28:17 +02:00
|
|
|
cleanupReceiveTailMoved:
|
2020-11-03 12:29:34 +01:00
|
|
|
CloseHandle(Session->Descriptor.Receive.TailMoved);
|
2020-10-24 08:28:17 +02:00
|
|
|
cleanupSendTailMoved:
|
2020-11-03 12:29:34 +01:00
|
|
|
CloseHandle(Session->Descriptor.Send.TailMoved);
|
2020-10-24 08:28:17 +02:00
|
|
|
cleanupAllocatedRegion:
|
|
|
|
VirtualFree(AllocatedRegion, 0, MEM_RELEASE);
|
|
|
|
cleanupRings:
|
2020-11-04 12:55:25 +01:00
|
|
|
Free(Session);
|
2021-06-24 12:12:13 +02:00
|
|
|
cleanup:
|
2020-11-03 12:29:34 +01:00
|
|
|
SetLastError(LastError);
|
|
|
|
return NULL;
|
2020-10-24 08:28:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void WINAPI
|
|
|
|
WintunEndSession(_In_ TUN_SESSION *Session)
|
|
|
|
{
|
2020-10-25 10:01:35 +01:00
|
|
|
DeleteCriticalSection(&Session->Send.Lock);
|
|
|
|
DeleteCriticalSection(&Session->Receive.Lock);
|
2020-10-24 08:28:17 +02:00
|
|
|
CloseHandle(Session->Handle);
|
|
|
|
CloseHandle(Session->Descriptor.Send.TailMoved);
|
|
|
|
CloseHandle(Session->Descriptor.Receive.TailMoved);
|
|
|
|
VirtualFree(Session->Descriptor.Send.Ring, 0, MEM_RELEASE);
|
2020-11-04 12:55:25 +01:00
|
|
|
Free(Session);
|
2020-10-24 08:28:17 +02:00
|
|
|
}
|
|
|
|
|
2020-11-02 16:04:33 +01:00
|
|
|
HANDLE WINAPI
|
|
|
|
WintunGetReadWaitEvent(_In_ TUN_SESSION *Session)
|
|
|
|
{
|
|
|
|
return Session->Descriptor.Send.TailMoved;
|
|
|
|
}
|
|
|
|
|
2020-11-03 12:29:34 +01:00
|
|
|
_Return_type_success_(return != NULL) _Ret_bytecount_(*PacketSize) BYTE *WINAPI
|
|
|
|
WintunReceivePacket(_In_ TUN_SESSION *Session, _Out_ DWORD *PacketSize)
|
2020-10-24 08:28:17 +02:00
|
|
|
{
|
2020-11-03 12:29:34 +01:00
|
|
|
DWORD LastError;
|
2020-10-25 10:01:35 +01:00
|
|
|
EnterCriticalSection(&Session->Send.Lock);
|
|
|
|
if (Session->Send.Head >= Session->Capacity)
|
2020-10-24 08:28:17 +02:00
|
|
|
{
|
2020-11-03 12:29:34 +01:00
|
|
|
LastError = ERROR_HANDLE_EOF;
|
2020-10-25 10:01:35 +01:00
|
|
|
goto cleanup;
|
2020-10-24 08:28:17 +02:00
|
|
|
}
|
2020-10-30 16:58:50 +01:00
|
|
|
const ULONG BuffTail = ReadULongAcquire(&Session->Descriptor.Send.Ring->Tail);
|
2020-10-25 10:01:35 +01:00
|
|
|
if (BuffTail >= Session->Capacity)
|
|
|
|
{
|
2020-11-03 12:29:34 +01:00
|
|
|
LastError = ERROR_HANDLE_EOF;
|
2020-10-25 10:01:35 +01:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (Session->Send.Head == BuffTail)
|
|
|
|
{
|
2020-11-03 12:29:34 +01:00
|
|
|
LastError = ERROR_NO_MORE_ITEMS;
|
2020-10-25 10:01:35 +01:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
const ULONG BuffContent = TUN_RING_WRAP(BuffTail - Session->Send.Head, Session->Capacity);
|
|
|
|
if (BuffContent < sizeof(TUN_PACKET))
|
|
|
|
{
|
2020-11-03 12:29:34 +01:00
|
|
|
LastError = ERROR_INVALID_DATA;
|
2020-10-25 10:01:35 +01:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
TUN_PACKET *BuffPacket = (TUN_PACKET *)&Session->Descriptor.Send.Ring->Data[Session->Send.Head];
|
|
|
|
if (BuffPacket->Size > WINTUN_MAX_IP_PACKET_SIZE)
|
|
|
|
{
|
2020-11-03 12:29:34 +01:00
|
|
|
LastError = ERROR_INVALID_DATA;
|
2020-10-25 10:01:35 +01:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
const ULONG AlignedPacketSize = TUN_ALIGN(sizeof(TUN_PACKET) + BuffPacket->Size);
|
|
|
|
if (AlignedPacketSize > BuffContent)
|
|
|
|
{
|
2020-11-03 12:29:34 +01:00
|
|
|
LastError = ERROR_INVALID_DATA;
|
2020-10-25 10:01:35 +01:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
*PacketSize = BuffPacket->Size;
|
2020-11-03 12:29:34 +01:00
|
|
|
BYTE *Packet = BuffPacket->Data;
|
2020-10-25 10:01:35 +01:00
|
|
|
Session->Send.Head = TUN_RING_WRAP(Session->Send.Head + AlignedPacketSize, Session->Capacity);
|
|
|
|
Session->Send.PacketsToRelease++;
|
2020-11-03 12:29:34 +01:00
|
|
|
LeaveCriticalSection(&Session->Send.Lock);
|
|
|
|
return Packet;
|
2020-10-25 10:01:35 +01:00
|
|
|
cleanup:
|
|
|
|
LeaveCriticalSection(&Session->Send.Lock);
|
2020-11-03 12:29:34 +01:00
|
|
|
SetLastError(LastError);
|
|
|
|
return NULL;
|
2020-10-25 10:01:35 +01:00
|
|
|
}
|
2020-10-24 08:28:17 +02:00
|
|
|
|
2020-10-25 10:01:35 +01:00
|
|
|
void WINAPI
|
2020-11-05 13:01:41 +01:00
|
|
|
WintunReleaseReceivePacket(_In_ TUN_SESSION *Session, _In_ const BYTE *Packet)
|
2020-10-25 10:01:35 +01:00
|
|
|
{
|
|
|
|
EnterCriticalSection(&Session->Send.Lock);
|
|
|
|
TUN_PACKET *ReleasedBuffPacket = (TUN_PACKET *)(Packet - offsetof(TUN_PACKET, Data));
|
|
|
|
ReleasedBuffPacket->Size |= TUN_PACKET_RELEASE;
|
|
|
|
while (Session->Send.PacketsToRelease)
|
|
|
|
{
|
|
|
|
const TUN_PACKET *BuffPacket = (TUN_PACKET *)&Session->Descriptor.Send.Ring->Data[Session->Send.HeadRelease];
|
|
|
|
if ((BuffPacket->Size & TUN_PACKET_RELEASE) == 0)
|
|
|
|
break;
|
|
|
|
const ULONG AlignedPacketSize = TUN_ALIGN(sizeof(TUN_PACKET) + (BuffPacket->Size & ~TUN_PACKET_RELEASE));
|
|
|
|
Session->Send.HeadRelease = TUN_RING_WRAP(Session->Send.HeadRelease + AlignedPacketSize, Session->Capacity);
|
|
|
|
Session->Send.PacketsToRelease--;
|
|
|
|
}
|
2020-10-30 16:58:50 +01:00
|
|
|
WriteULongRelease(&Session->Descriptor.Send.Ring->Head, Session->Send.HeadRelease);
|
2020-10-25 10:01:35 +01:00
|
|
|
LeaveCriticalSection(&Session->Send.Lock);
|
2020-10-24 08:28:17 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 12:29:34 +01:00
|
|
|
_Return_type_success_(return != NULL) _Ret_bytecount_(PacketSize) BYTE *WINAPI
|
|
|
|
WintunAllocateSendPacket(_In_ TUN_SESSION *Session, _In_ DWORD PacketSize)
|
2020-10-24 08:28:17 +02:00
|
|
|
{
|
2020-11-03 12:29:34 +01:00
|
|
|
DWORD LastError;
|
2020-10-25 10:01:35 +01:00
|
|
|
EnterCriticalSection(&Session->Receive.Lock);
|
|
|
|
if (Session->Receive.Tail >= Session->Capacity)
|
2020-10-24 08:28:17 +02:00
|
|
|
{
|
2020-11-03 12:29:34 +01:00
|
|
|
LastError = ERROR_HANDLE_EOF;
|
2020-10-25 10:01:35 +01:00
|
|
|
goto cleanup;
|
2020-10-24 08:28:17 +02:00
|
|
|
}
|
2020-10-25 10:01:35 +01:00
|
|
|
const ULONG AlignedPacketSize = TUN_ALIGN(sizeof(TUN_PACKET) + PacketSize);
|
2020-10-30 16:58:50 +01:00
|
|
|
const ULONG BuffHead = ReadULongAcquire(&Session->Descriptor.Receive.Ring->Head);
|
2020-10-25 10:01:35 +01:00
|
|
|
if (BuffHead >= Session->Capacity)
|
|
|
|
{
|
2020-11-03 12:29:34 +01:00
|
|
|
LastError = ERROR_HANDLE_EOF;
|
2020-10-25 10:01:35 +01:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
const ULONG BuffSpace = TUN_RING_WRAP(BuffHead - Session->Receive.Tail - TUN_ALIGNMENT, Session->Capacity);
|
|
|
|
if (AlignedPacketSize > BuffSpace)
|
|
|
|
{
|
2020-11-03 12:29:34 +01:00
|
|
|
LastError = ERROR_BUFFER_OVERFLOW;
|
2020-10-25 10:01:35 +01:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
TUN_PACKET *BuffPacket = (TUN_PACKET *)&Session->Descriptor.Receive.Ring->Data[Session->Receive.Tail];
|
|
|
|
BuffPacket->Size = PacketSize | TUN_PACKET_RELEASE;
|
2020-11-03 12:29:34 +01:00
|
|
|
BYTE *Packet = BuffPacket->Data;
|
2020-10-25 10:01:35 +01:00
|
|
|
Session->Receive.Tail = TUN_RING_WRAP(Session->Receive.Tail + AlignedPacketSize, Session->Capacity);
|
|
|
|
Session->Receive.PacketsToRelease++;
|
2020-11-03 12:29:34 +01:00
|
|
|
LeaveCriticalSection(&Session->Receive.Lock);
|
|
|
|
return Packet;
|
2020-10-25 10:01:35 +01:00
|
|
|
cleanup:
|
|
|
|
LeaveCriticalSection(&Session->Receive.Lock);
|
2020-11-03 12:29:34 +01:00
|
|
|
SetLastError(LastError);
|
|
|
|
return NULL;
|
2020-10-25 10:01:35 +01:00
|
|
|
}
|
2020-10-24 08:28:17 +02:00
|
|
|
|
2020-10-25 10:01:35 +01:00
|
|
|
void WINAPI
|
|
|
|
WintunSendPacket(_In_ TUN_SESSION *Session, _In_ const BYTE *Packet)
|
|
|
|
{
|
|
|
|
EnterCriticalSection(&Session->Receive.Lock);
|
|
|
|
TUN_PACKET *ReleasedBuffPacket = (TUN_PACKET *)(Packet - offsetof(TUN_PACKET, Data));
|
|
|
|
ReleasedBuffPacket->Size &= ~TUN_PACKET_RELEASE;
|
|
|
|
while (Session->Receive.PacketsToRelease)
|
|
|
|
{
|
|
|
|
const TUN_PACKET *BuffPacket =
|
|
|
|
(TUN_PACKET *)&Session->Descriptor.Receive.Ring->Data[Session->Receive.TailRelease];
|
|
|
|
if (BuffPacket->Size & TUN_PACKET_RELEASE)
|
|
|
|
break;
|
|
|
|
const ULONG AlignedPacketSize = TUN_ALIGN(sizeof(TUN_PACKET) + BuffPacket->Size);
|
|
|
|
Session->Receive.TailRelease =
|
|
|
|
TUN_RING_WRAP(Session->Receive.TailRelease + AlignedPacketSize, Session->Capacity);
|
|
|
|
Session->Receive.PacketsToRelease--;
|
|
|
|
}
|
2020-11-18 17:35:42 +01:00
|
|
|
if (Session->Descriptor.Receive.Ring->Tail != Session->Receive.TailRelease) {
|
|
|
|
WriteULongRelease(&Session->Descriptor.Receive.Ring->Tail, Session->Receive.TailRelease);
|
|
|
|
if (ReadAcquire(&Session->Descriptor.Receive.Ring->Alertable))
|
|
|
|
SetEvent(Session->Descriptor.Receive.TailMoved);
|
|
|
|
}
|
2020-10-25 10:01:35 +01:00
|
|
|
LeaveCriticalSection(&Session->Receive.Lock);
|
2020-10-24 08:28:17 +02:00
|
|
|
}
|