aboutsummaryrefslogtreecommitdiffstats
path: root/api/logger.c
blob: 7ad6c5c8f4f977b8d378859e84a45de0f56dd9a1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* SPDX-License-Identifier: GPL-2.0
 *
 * Copyright (C) 2018-2021 WireGuard LLC. All Rights Reserved.
 */

#include "../driver/ioctl.h"
#include "logger.h"
#include "adapter.h"
#include "ntdll.h"
#include <Windows.h>
#include <iphlpapi.h>
#include <winternl.h>
#include <wchar.h>
#include <stdlib.h>

static BOOL CALLBACK
NopLogger(_In_ WIREGUARD_LOGGER_LEVEL Level, _In_ DWORD64 Timestamp, _In_z_ LPCWSTR LogLine)
{
    return TRUE;
}

WIREGUARD_LOGGER_CALLBACK Logger = NopLogger;

static DWORD64 Now(VOID)
{
    LARGE_INTEGER Timestamp;
    NtQuerySystemTime(&Timestamp);
    return Timestamp.QuadPart;
}

_Use_decl_annotations_
VOID WINAPI
WireGuardSetLogger(WIREGUARD_LOGGER_CALLBACK NewLogger)
{
    if (!NewLogger)
        NewLogger = NopLogger;
    Logger = NewLogger;
}

static VOID
StrTruncate(_Inout_count_(StrChars) LPWSTR Str, _In_ SIZE_T StrChars)
{
    Str[StrChars - 2] = L'\u2026'; /* Horizontal Ellipsis */
    Str[StrChars - 1] = 0;
}

_Use_decl_annotations_
DWORD
LoggerLog(WIREGUARD_LOGGER_LEVEL Level, LPCWSTR LogLine)
{
    DWORD LastError = GetLastError();
    Logger(Level, Now(), LogLine);
    SetLastError(LastError);
    return LastError;
}

_Use_decl_annotations_
DWORD
LoggerLogV(WIREGUARD_LOGGER_LEVEL Level, LPCWSTR Format, va_list Args)
{
    DWORD LastError = GetLastError();
    WCHAR LogLine[0x400];
    if (_vsnwprintf_s(LogLine, _countof(LogLine), _TRUNCATE, Format, Args) == -1)
        StrTruncate(LogLine, _countof(LogLine));
    Logger(Level, Now(), LogLine);
    SetLastError(LastError);
    return LastError;
}

_Use_decl_annotations_
DWORD
LoggerError(DWORD Error, LPCWSTR Prefix)
{
    LPWSTR SystemMessage = NULL, FormattedMessage = NULL;
    FormatMessageW(
        FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_MAX_WIDTH_MASK,
        NULL,
        HRESULT_FROM_SETUPAPI(Error),
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (VOID *)&SystemMessage,
        0,
        NULL);
    FormatMessageW(
        FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_ARGUMENT_ARRAY |
            FORMAT_MESSAGE_MAX_WIDTH_MASK,
        SystemMessage ? L"%1: %3(Code 0x%2!08X!)" : L"%1: Code 0x%2!08X!",
        0,
        0,
        (VOID *)&FormattedMessage,
        0,
        (va_list *)(DWORD_PTR[]){ (DWORD_PTR)Prefix, (DWORD_PTR)Error, (DWORD_PTR)SystemMessage });
    if (FormattedMessage)
        Logger(WIREGUARD_LOG_ERR, Now(), FormattedMessage);
    LocalFree(FormattedMessage);
    LocalFree(SystemMessage);
    return Error;
}

_Use_decl_annotations_
DWORD
LoggerErrorV(DWORD Error, LPCWSTR Format, va_list Args)
{
    WCHAR LogLine[0x400];
    if (_vsnwprintf_s(LogLine, _countof(LogLine), _TRUNCATE, Format, Args) == -1)
        StrTruncate(LogLine, _countof(LogLine));
    return LoggerError(Error, LogLine);
}

_Use_decl_annotations_
VOID
LoggerGetRegistryKeyPath(HKEY Key, LPWSTR Path)
{
    DWORD LastError = GetLastError();
    if (Key == NULL)
    {
        wcsncpy_s(Path, MAX_REG_PATH, L"<null>", _TRUNCATE);
        goto out;
    }
    if (_snwprintf_s(Path, MAX_REG_PATH, _TRUNCATE, L"0x%p", Key) == -1)
        StrTruncate(Path, MAX_REG_PATH);
    union
    {
        KEY_NAME_INFORMATION KeyNameInfo;
        WCHAR Data[offsetof(KEY_NAME_INFORMATION, Name) + MAX_REG_PATH];
    } Buffer;
    DWORD Size;
    if (!NT_SUCCESS(NtQueryKey(Key, 3, &Buffer, sizeof(Buffer), &Size)) ||
        Size < offsetof(KEY_NAME_INFORMATION, Name) || Buffer.KeyNameInfo.NameLength >= MAX_REG_PATH * sizeof(WCHAR))
        goto out;
    Buffer.KeyNameInfo.NameLength /= sizeof(WCHAR);
    wmemcpy_s(Path, MAX_REG_PATH, Buffer.KeyNameInfo.Name, Buffer.KeyNameInfo.NameLength);
    Path[Buffer.KeyNameInfo.NameLength] = L'\0';
out:
    SetLastError(LastError);
}

static DWORD WINAPI
LogReaderThread(_In_ LPVOID Parameter)
{
    WIREGUARD_ADAPTER *Adapter = Parameter;
    HANDLE ControlFile = AdapterOpenDeviceObject(Adapter);
    if (ControlFile == INVALID_HANDLE_VALUE)
    {
        LOG_LAST_ERROR(L"Unable to enable logging");
        WriteULongNoFence(&Adapter->LogState, WIREGUARD_ADAPTER_LOG_OFF);
        return 0;
    }
    while (ReadULongNoFence(&Adapter->LogState) != WIREGUARD_ADAPTER_LOG_OFF)
    {
        WG_IOCTL_LOG_ENTRY Entry = { 0 };
        WCHAR WideLine[sizeof(Entry.Msg) + 32] = { 0 };
        DWORD Bytes = sizeof(Entry);
        if (!DeviceIoControl(ControlFile, WG_IOCTL_READ_LOG_LINE, NULL, 0, &Entry, Bytes, &Bytes, NULL))
        {
            BOOL IsAbort = GetLastError() == ERROR_OPERATION_ABORTED;
            CloseHandle(ControlFile);
            if (ReadULongNoFence(&Adapter->LogState) == WIREGUARD_ADAPTER_LOG_OFF)
                return 0;
            if (IsAbort)
                Sleep(5000);
            for (DWORD i = 0;; ++i)
            {
                ControlFile = AdapterOpenDeviceObject(Adapter);
                if (ControlFile == INVALID_HANDLE_VALUE)
                {
                    if (i < 10 && ReadULongNoFence(&Adapter->LogState) != WIREGUARD_ADAPTER_LOG_OFF)
                    {
                        Sleep(1000);
                        continue;
                    }
                    LOG_LAST_ERROR(L"Failed to reopen handle for logging after adapter disappeared");
                    WriteULongNoFence(&Adapter->LogState, WIREGUARD_ADAPTER_LOG_OFF);
                    return 0;
                }
                else
                    break;
            }
            continue;
        }
        WIREGUARD_LOGGER_LEVEL Level;
        if (Entry.Msg[0] == '1')
            Level = WIREGUARD_LOG_ERR;
        else if (Entry.Msg[0] == '2')
            Level = WIREGUARD_LOG_WARN;
        else if (Entry.Msg[0] == '3')
            Level = WIREGUARD_LOG_INFO;
        else
            continue;
        DWORD Offset = 0;
        if (ReadULongNoFence(&Adapter->LogState) == WIREGUARD_ADAPTER_LOG_ON_WITH_PREFIX)
        {
            if (!Adapter->IfIndex)
            {
                NET_LUID Luid;
                WireGuardGetAdapterLUID(Adapter, &Luid);
                ConvertInterfaceLuidToIndex(&Luid, &Adapter->IfIndex);
            }
            Offset = swprintf_s(WideLine, _countof(WideLine), L"%u: ", Adapter->IfIndex);
        }
        if (!MultiByteToWideChar(CP_UTF8, 0, &Entry.Msg[1], -1, WideLine + Offset, _countof(WideLine) - Offset))
            continue;
        Logger(Level, Entry.Timestamp, WideLine);
    }
    CloseHandle(ControlFile);
    return 0;
}

_Use_decl_annotations_
BOOL WINAPI
WireGuardSetAdapterLogging(WIREGUARD_ADAPTER *Adapter, WIREGUARD_ADAPTER_LOG_STATE LogState)
{
    DWORD CurrentState = ReadULongNoFence(&Adapter->LogState);
    if (CurrentState == (DWORD)LogState)
        return TRUE;
    WriteULongNoFence(&Adapter->LogState, LogState);
    if (CurrentState != WIREGUARD_ADAPTER_LOG_OFF && LogState == WIREGUARD_ADAPTER_LOG_OFF && Adapter->LogThread)
    {
        CancelSynchronousIo(Adapter->LogThread);
        BOOL Ret = WaitForSingleObject(Adapter->LogThread, INFINITE);
        CloseHandle(Adapter->LogThread);
        Adapter->LogThread = NULL;
        return Ret;
    }
    if (CurrentState == WIREGUARD_ADAPTER_LOG_OFF && LogState != WIREGUARD_ADAPTER_LOG_OFF && !Adapter->LogThread)
    {
        Adapter->LogThread = CreateThread(NULL, 0, LogReaderThread, Adapter, 0, NULL);
        return Adapter->LogThread != NULL;
    }
    return TRUE;
}