-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathDnsResolver.cpp
More file actions
423 lines (353 loc) · 17.6 KB
/
DnsResolver.cpp
File metadata and controls
423 lines (353 loc) · 17.6 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// Copyright (C) Microsoft Corporation. All rights reserved.
#include <LxssDynamicFunction.h>
#include "precomp.h"
#include "DnsResolver.h"
using wsl::core::networking::DnsResolver;
static constexpr auto c_dnsModuleName = L"dnsapi.dll";
std::optional<LxssDynamicFunction<decltype(DnsQueryRaw)>> DnsResolver::s_dnsQueryRaw;
std::optional<LxssDynamicFunction<decltype(DnsCancelQueryRaw)>> DnsResolver::s_dnsCancelQueryRaw;
std::optional<LxssDynamicFunction<decltype(DnsQueryRawResultFree)>> DnsResolver::s_dnsQueryRawResultFree;
HRESULT DnsResolver::LoadDnsResolverMethods() noexcept
{
static wil::shared_hmodule dnsModule;
static DWORD loadError = ERROR_SUCCESS;
static std::once_flag dnsLoadFlag;
// Load DNS dll only once
std::call_once(dnsLoadFlag, [&]() {
dnsModule.reset(LoadLibraryEx(c_dnsModuleName, nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32));
if (!dnsModule)
{
loadError = GetLastError();
}
});
RETURN_IF_WIN32_ERROR_MSG(loadError, "LoadLibraryEx %ls", c_dnsModuleName);
// Initialize dynamic functions for the DNS tunneling Windows APIs.
// using the non-throwing instance of LxssDynamicFunction as to not end up in the Error telemetry
LxssDynamicFunction<decltype(DnsQueryRaw)> local_dnsQueryRaw{DynamicFunctionErrorLogs::None};
RETURN_IF_FAILED_EXPECTED(local_dnsQueryRaw.load(dnsModule, "DnsQueryRaw"));
LxssDynamicFunction<decltype(DnsCancelQueryRaw)> local_dnsCancelQueryRaw{DynamicFunctionErrorLogs::None};
RETURN_IF_FAILED_EXPECTED(local_dnsCancelQueryRaw.load(dnsModule, "DnsCancelQueryRaw"));
LxssDynamicFunction<decltype(DnsQueryRawResultFree)> local_dnsQueryRawResultFree{DynamicFunctionErrorLogs::None};
RETURN_IF_FAILED_EXPECTED(local_dnsQueryRawResultFree.load(dnsModule, "DnsQueryRawResultFree"));
// Make a dummy call to the DNS APIs to verify if they are working. The APIs are going to be present
// on older Windows versions, where they can be turned on/off. If turned off, the APIs
// will be unusable and will return ERROR_CALL_NOT_IMPLEMENTED.
if (local_dnsQueryRaw(nullptr, nullptr) == ERROR_CALL_NOT_IMPLEMENTED)
{
RETURN_IF_WIN32_ERROR_EXPECTED(ERROR_CALL_NOT_IMPLEMENTED);
}
s_dnsQueryRaw.emplace(std::move(local_dnsQueryRaw));
s_dnsCancelQueryRaw.emplace(std::move(local_dnsCancelQueryRaw));
s_dnsQueryRawResultFree.emplace(std::move(local_dnsQueryRawResultFree));
return S_OK;
}
DnsResolver::DnsResolver(wil::unique_socket&& dnsHvsocket, DnsResolverFlags flags) :
m_dnsChannel(
std::move(dnsHvsocket),
[this](const gsl::span<gsl::byte> dnsBuffer, const LX_GNS_DNS_CLIENT_IDENTIFIER& dnsClientIdentifier) {
ProcessDnsRequest(dnsBuffer, dnsClientIdentifier);
}),
m_flags(flags)
{
// Initialize as signaled, as there are no requests yet
m_allRequestsFinished.SetEvent();
// Read external interface constraint regkey
const auto lxssKey = windows::common::registry::OpenLxssMachineKey(KEY_READ);
m_externalInterfaceConstraintName =
windows::common::registry::ReadString(lxssKey.get(), nullptr, c_interfaceConstraintKey, L"");
if (!m_externalInterfaceConstraintName.empty())
{
ResolveExternalInterfaceConstraintIndex();
WSL_LOG(
"DnsResolver::DnsResolver",
TraceLoggingValue(m_externalInterfaceConstraintName.c_str(), "m_externalInterfaceConstraintName"),
TraceLoggingValue(m_externalInterfaceConstraintIndex, "m_externalInterfaceConstraintIndex"));
// Register for interface change notifications. Notifications are used to determine if the external interface constraint setting is applicable.
THROW_IF_WIN32_ERROR(NotifyIpInterfaceChange(AF_UNSPEC, &DnsResolver::InterfaceChangeCallback, this, FALSE, &m_interfaceNotificationHandle));
}
}
DnsResolver::~DnsResolver() noexcept
{
Stop();
}
void DnsResolver::GenerateTelemetry() noexcept
try
{
// Find the 3 most common DNS API failures
uint32_t mostCommonDnsStatusError = 0;
uint32_t mostCommonDnsStatusErrorCount = 0;
uint32_t secondCommonDnsStatusError = 0;
uint32_t secondCommonDnsStatusErrorCount = 0;
uint32_t thirdCommonDnsStatusError = 0;
uint32_t thirdCommonDnsStatusErrorCount = 0;
std::vector<std::pair<uint32_t, uint32_t>> failures(m_dnsApiFailures.size());
std::copy(m_dnsApiFailures.begin(), m_dnsApiFailures.end(), failures.begin());
// Sort in descending order based on failure count
std::sort(failures.begin(), failures.end(), [](const auto& lhs, const auto& rhs) { return lhs.second > rhs.second; });
if (failures.size() >= 1)
{
mostCommonDnsStatusError = failures[0].first;
mostCommonDnsStatusErrorCount = failures[0].second;
}
if (failures.size() >= 2)
{
secondCommonDnsStatusError = failures[1].first;
secondCommonDnsStatusErrorCount = failures[1].second;
}
if (failures.size() >= 3)
{
thirdCommonDnsStatusError = failures[2].first;
thirdCommonDnsStatusErrorCount = failures[2].second;
}
// Add telemetry with DNS tunneling statistics, before shutting down
WSL_LOG(
"DnsTunnelingStatistics",
TraceLoggingValue(m_totalUdpQueries.load(), "totalUdpQueries"),
TraceLoggingValue(m_successfulUdpQueries.load(), "successfulUdpQueries"),
TraceLoggingValue(m_totalTcpQueries.load(), "totalTcpQueries"),
TraceLoggingValue(m_successfulTcpQueries.load(), "successfulTcpQueries"),
TraceLoggingValue(m_queriesWithNullResult.load(), "queriesWithNullResult"),
TraceLoggingValue(m_failedDnsQueryRawCalls.load(), "FailedDnsQueryRawCalls"),
TraceLoggingValue(m_dnsApiFailures.size(), "totalDnsStatusErrorInstances"),
TraceLoggingValue(mostCommonDnsStatusError, "mostCommonDnsStatusError"),
TraceLoggingValue(mostCommonDnsStatusErrorCount, "mostCommonDnsStatusErrorCount"),
TraceLoggingValue(secondCommonDnsStatusError, "secondCommonDnsStatusError"),
TraceLoggingValue(secondCommonDnsStatusErrorCount, "secondCommonDnsStatusErrorCount"),
TraceLoggingValue(thirdCommonDnsStatusError, "thirdCommonDnsStatusError"),
TraceLoggingValue(thirdCommonDnsStatusErrorCount, "thirdCommonDnsStatusErrorCount"));
}
CATCH_LOG()
void DnsResolver::Stop() noexcept
try
{
WSL_LOG("DnsResolver::Stop");
// Scoped m_dnsLock
{
const std::lock_guard lock(m_dnsLock);
m_stopped = true;
// Cancel existing requests. Cancel is complete when DnsQueryRawCallback is
// invoked with status == ERROR_CANCELLED
// N.B. Cancelling can end up calling the DnsQueryRawCallback directly on this same thread. i.e., while this
// lock is held. Which is fine because m_dnsLock is a recursive mutex.
// N.B. Cancelling a query will synchronously remove the query from m_dnsRequests, which invalidates iterators.
std::vector<DNS_QUERY_RAW_CANCEL*> cancelHandles;
cancelHandles.reserve(m_dnsRequests.size());
for (auto& [_, context] : m_dnsRequests)
{
cancelHandles.emplace_back(&context->m_cancelHandle);
}
for (const auto e : cancelHandles)
{
LOG_IF_WIN32_ERROR(s_dnsCancelQueryRaw.value()(e));
}
}
// Wait for all requests to complete. At this point no new requests can be started since the object is stopped.
// We are only waiting for existing requests to finish.
m_allRequestsFinished.wait();
// Stop the response queue first as it can make calls in m_dnsChannel
m_dnsResponseQueue.cancel();
m_dnsChannel.Stop();
// Stop interface change notifications
m_interfaceNotificationHandle.reset();
GenerateTelemetry();
}
CATCH_LOG()
void DnsResolver::ProcessDnsRequest(const gsl::span<gsl::byte> dnsBuffer, const LX_GNS_DNS_CLIENT_IDENTIFIER& dnsClientIdentifier) noexcept
try
{
const std::lock_guard lock(m_dnsLock);
if (m_stopped)
{
return;
}
WSL_LOG_DEBUG(
"DnsResolver::ProcessDnsRequest - received new DNS request",
TraceLoggingValue(dnsBuffer.size(), "DNS buffer size"),
TraceLoggingValue(dnsClientIdentifier.Protocol == IPPROTO_UDP ? "UDP" : "TCP", "Protocol"),
TraceLoggingValue(dnsClientIdentifier.DnsClientId, "DNS client id"),
TraceLoggingValue(!m_externalInterfaceConstraintName.empty(), "Is ExternalInterfaceConstraint configured"),
TraceLoggingValue(m_externalInterfaceConstraintIndex, "m_externalInterfaceConstraintIndex"));
// If the external interface constraint is configured but it is *not* present/up, WSL should be net-blind, so we avoid making DNS requests.
if (!m_externalInterfaceConstraintName.empty() && m_externalInterfaceConstraintIndex == 0)
{
return;
}
dnsClientIdentifier.Protocol == IPPROTO_UDP ? m_totalUdpQueries++ : m_totalTcpQueries++;
// Get next request id. If value reaches UINT_MAX + 1 it will be automatically reset to 0
const auto requestId = m_currentRequestId++;
// Create the DNS request context
auto context = std::make_unique<DnsResolver::DnsQueryContext>(
requestId, dnsClientIdentifier, [this](_Inout_ DnsResolver::DnsQueryContext* context, _Inout_opt_ DNS_QUERY_RAW_RESULT* queryResults) {
HandleDnsQueryCompletion(context, queryResults);
});
auto [it, _] = m_dnsRequests.emplace(requestId, std::move(context));
const auto localContext = it->second.get();
auto removeContextOnError = wil::scope_exit([&] { WI_VERIFY(m_dnsRequests.erase(requestId) == 1); });
// Fill DNS request structure
DNS_QUERY_RAW_REQUEST request{};
request.version = DNS_QUERY_RAW_REQUEST_VERSION1;
request.resultsVersion = DNS_QUERY_RAW_RESULTS_VERSION1;
request.dnsQueryRawSize = static_cast<ULONG>(dnsBuffer.size());
request.dnsQueryRaw = (PBYTE)dnsBuffer.data();
request.protocol = (dnsClientIdentifier.Protocol == IPPROTO_TCP) ? DNS_PROTOCOL_TCP : DNS_PROTOCOL_UDP;
request.queryCompletionCallback = DnsResolver::DnsQueryRawCallback;
request.queryContext = localContext;
// Only unicast UDP & TCP queries are tunneled. Pass this flag to tell Windows DNS client to *not* resolve using multicast.
request.queryOptions |= DNS_QUERY_NO_MULTICAST;
// DnsQueryRaw bypasses the Windows DNS cache internally, which means all responses return with TTL values
// reset to their original values. Pass this flag so that the DNS client preserves the real (decremented) TTL
// values from cached upstream responses, enabling downstream caches (e.g. systemd-resolved) to use meaningful TTLs.
request.queryOptions |= DNS_QUERY_DONT_RESET_TTL_VALUES;
// In a DNS request from Linux there might be DNS records that Windows DNS client does not know how to parse.
// By default in this case Windows will fail the request. When the flag is enabled, Windows will extract the
// question from the DNS request and attempt to resolve it, ignoring the unknown records.
if (WI_IsFlagSet(m_flags, DnsResolverFlags::BestEffortDnsParsing))
{
request.queryRawOptions |= DNS_QUERY_RAW_OPTION_BEST_EFFORT_PARSE;
}
// If the external interface constraint is configured and present on the host, only send DNS requests on that interface.
if (m_externalInterfaceConstraintIndex != 0)
{
request.interfaceIndex = m_externalInterfaceConstraintIndex;
}
// Start the DNS request
// N.B. DnsQueryRaw bypasses the Windows DNS cache; DNS_QUERY_DONT_RESET_TTL_VALUES is set above
// so that responses carry real TTL values for downstream caching (e.g. systemd-resolved).
const auto result = s_dnsQueryRaw.value()(&request, &localContext->m_cancelHandle);
if (result != DNS_REQUEST_PENDING)
{
m_failedDnsQueryRawCalls++;
WSL_LOG(
"ProcessDnsRequestFailed",
TraceLoggingValue(requestId, "requestId"),
TraceLoggingValue(result, "result"),
TraceLoggingValue("DnsQueryRaw", "executionStep"));
return;
}
removeContextOnError.release();
m_allRequestsFinished.ResetEvent();
}
CATCH_LOG()
void DnsResolver::HandleDnsQueryCompletion(_Inout_ DnsResolver::DnsQueryContext* queryContext, _Inout_opt_ DNS_QUERY_RAW_RESULT* queryResults) noexcept
try
{
// Always free the query result structure
const auto freeQueryResults = wil::scope_exit([&] {
if (queryResults != nullptr)
{
s_dnsQueryRawResultFree.value()(queryResults);
}
});
const std::lock_guard lock(m_dnsLock);
if (queryResults != nullptr)
{
WSL_LOG(
"DnsResolver::HandleDnsQueryCompletion",
TraceLoggingValue(queryContext->m_id, "queryContext->m_id"),
TraceLoggingValue(queryResults->queryStatus, "queryResults->queryStatus"),
TraceLoggingValue(queryResults->queryRawResponse != nullptr, "validResponse"));
// Note: The response may be valid even if queryResults->queryStatus is not 0, for example when the DNS server returns a negative response.
if (queryResults->queryRawResponse != nullptr)
{
queryContext->m_dnsClientIdentifier.Protocol == IPPROTO_UDP ? m_successfulUdpQueries++ : m_successfulTcpQueries++;
}
// the Windows DNS API returned failure
else
{
if (m_dnsApiFailures.find(queryResults->queryStatus) == m_dnsApiFailures.end())
{
m_dnsApiFailures[queryResults->queryStatus] = 1;
}
else
{
m_dnsApiFailures[queryResults->queryStatus]++;
}
}
}
else
{
WSL_LOG(
"DnsResolver::HandleDnsQueryCompletion - received a NULL queryResults",
TraceLoggingValue(queryContext->m_id, "queryContext->m_id"));
m_queriesWithNullResult++;
}
if (!m_stopped && queryResults != nullptr && queryResults->queryRawResponse != nullptr)
{
// Copy DNS response buffer
std::vector<gsl::byte> dnsResponse(queryResults->queryRawResponseSize);
CopyMemory(dnsResponse.data(), queryResults->queryRawResponse, queryResults->queryRawResponseSize);
WSL_LOG_DEBUG(
"DnsResolver::HandleDnsQueryCompletion - received new DNS response",
TraceLoggingValue(dnsResponse.size(), "DNS buffer size"),
TraceLoggingValue(queryContext->m_dnsClientIdentifier.Protocol == IPPROTO_UDP ? "UDP" : "TCP", "Protocol"),
TraceLoggingValue(queryContext->m_dnsClientIdentifier.DnsClientId, "DNS client id"));
// Schedule the DNS response to be sent to Linux
m_dnsResponseQueue.submit([this, dnsResponse = std::move(dnsResponse), dnsClientIdentifier = queryContext->m_dnsClientIdentifier]() mutable {
m_dnsChannel.SendDnsMessage(gsl::make_span(dnsResponse), dnsClientIdentifier);
});
}
// Stop tracking this DNS request and delete the request context
WI_VERIFY(m_dnsRequests.erase(queryContext->m_id) == 1);
// Set event if all tracked requests have finished
if (m_dnsRequests.empty())
{
m_allRequestsFinished.SetEvent();
}
}
CATCH_LOG()
void DnsResolver::ResolveExternalInterfaceConstraintIndex() noexcept
try
{
const std::lock_guard lock(m_dnsLock);
if (m_stopped)
{
return;
}
if (m_externalInterfaceConstraintName.empty())
{
return;
}
NET_LUID interfaceLuid{};
ULONG interfaceIndex = 0;
// Update the interface index on every exit path.
// The calls below to convert interface name to index will fail if the interface does not exist anymore,
// in which case we still need to reset the interface index to its default value of 0.
const auto setInterfaceIndex = wil::scope_exit([&] {
if (interfaceIndex != m_externalInterfaceConstraintIndex)
{
WSL_LOG(
"DnsResolver::ResolveExternalInterfaceConstraintIndex - setting m_externalInterfaceConstraintIndex to new value",
TraceLoggingValue(m_externalInterfaceConstraintIndex, "old interface index"),
TraceLoggingValue(interfaceIndex, "new interface index"));
m_externalInterfaceConstraintIndex = interfaceIndex;
}
});
// If external interface constraint is configured, query to see if it's present on the host.
auto errorCode = ConvertInterfaceAliasToLuid(m_externalInterfaceConstraintName.c_str(), &interfaceLuid);
if (FAILED_WIN32_LOG(errorCode))
{
return;
}
errorCode = ConvertInterfaceLuidToIndex(&interfaceLuid, reinterpret_cast<PNET_IFINDEX>(&interfaceIndex));
if (FAILED_WIN32_LOG(errorCode))
{
return;
}
}
CATCH_LOG()
VOID CALLBACK DnsResolver::DnsQueryRawCallback(_In_ VOID* queryContext, _Inout_opt_ DNS_QUERY_RAW_RESULT* queryResults) noexcept
try
{
assert(queryContext != nullptr);
const auto context = static_cast<DnsQueryContext*>(queryContext);
// Call into DnsResolver parent object to process the query result
context->m_handleQueryCompletion(context, queryResults);
}
CATCH_LOG()
VOID CALLBACK DnsResolver::InterfaceChangeCallback(_In_ PVOID context, PMIB_IPINTERFACE_ROW, MIB_NOTIFICATION_TYPE) noexcept
try
{
const auto dnsResolver = static_cast<DnsResolver*>(context);
dnsResolver->ResolveExternalInterfaceConstraintIndex();
}
CATCH_LOG()