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
|
#include "stdafx.h"
#include "context.h"
#include <libcommon/string.h>
#include <libcommon/error.h>
#include <libcommon/memory.h>
#include <log/log.h>
#include <winsock2.h>
#include <ws2ipdef.h>
#include <iphlpapi.h>
#include <windows.h>
#include <vector>
#include <list>
#include <stdexcept>
#include <sstream>
#include <algorithm>
#include <setupapi.h>
#include <devguid.h>
#include <combaseapi.h>
#include <initguid.h>
#include <devpkey.h>
namespace
{
const wchar_t TAP_HARDWARE_ID[] = L"tap0901";
std::set<Context::NetworkAdapter> GetAllAdapters()
{
ULONG bufferSize = 0;
const ULONG flags = GAA_FLAG_SKIP_UNICAST | GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER;
auto status = GetAdaptersAddresses(AF_INET, flags, nullptr, nullptr, &bufferSize);
THROW_UNLESS(ERROR_BUFFER_OVERFLOW, status, "Probe for adapter listing buffer size");
// Memory is cheap, this avoids a looping construct.
bufferSize *= 2;
std::vector<uint8_t> buffer(bufferSize);
status = GetAdaptersAddresses(AF_INET, flags, nullptr,
reinterpret_cast<PIP_ADAPTER_ADDRESSES>(&buffer[0]), &bufferSize);
THROW_UNLESS(ERROR_SUCCESS, status, "Retrieve adapter listing");
std::set<Context::NetworkAdapter> adapters;
for (auto it = (PIP_ADAPTER_ADDRESSES)&buffer[0]; nullptr != it; it = it->Next)
{
adapters.emplace(Context::NetworkAdapter(common::string::ToWide(it->AdapterName),
it->Description, it->FriendlyName));
}
return adapters;
}
std::set<Context::NetworkAdapter> GetTapAdapters(const std::set<Context::NetworkAdapter> &adapters)
{
std::set<Context::NetworkAdapter> tapAdapters;
for (const auto &adapter : adapters)
{
static const wchar_t name[] = L"TAP-Windows Adapter V9";
//
// Compare partial name, because once you start having more TAP adapters
// they're named "TAP-Windows Adapter V9 #2" and so on.
//
if (0 == adapter.name.compare(0, _countof(name) - 1, name))
{
tapAdapters.insert(adapter);
}
}
return tapAdapters;
}
void LogAdapters(const std::wstring &description, const std::set<Context::NetworkAdapter> &adapters)
{
//
// Flatten the information so we can log it more easily.
//
std::vector<std::wstring> details;
for (const auto &adapter : adapters)
{
details.emplace_back(L"Adapter");
details.emplace_back(std::wstring(L" Guid: ").append(adapter.guid));
details.emplace_back(std::wstring(L" Name: ").append(adapter.name));
details.emplace_back(std::wstring(L" Alias: ").append(adapter.alias));
}
PluginLogWithDetails(description, details);
}
std::wstring GetNetCfgInstanceId(HDEVINFO devInfo, const SP_DEVINFO_DATA &devInfoData)
{
HKEY hNet = SetupDiOpenDevRegKey(
devInfo,
const_cast<SP_DEVINFO_DATA *>(&devInfoData),
DICS_FLAG_GLOBAL,
0,
DIREG_DRV,
KEY_READ
);
if (hNet == INVALID_HANDLE_VALUE)
{
throw std::runtime_error("SetupDiOpenDevRegKey Failed");
}
std::vector<wchar_t> instanceId(MAX_PATH + sizeof(L'\0'));
DWORD strSize = instanceId.size() * sizeof(wchar_t);
const auto status = RegGetValueW(
hNet,
nullptr,
L"NetCfgInstanceId",
RRF_RT_REG_SZ,
nullptr,
instanceId.data(),
&strSize
);
RegCloseKey(hNet);
if (ERROR_SUCCESS != status)
{
throw std::runtime_error("RegGetValue for NetCfgInstanceId failed");
}
instanceId[strSize / sizeof(wchar_t)] = L'\0';
return instanceId.data();
}
} // anonymous namespace
//static
std::optional<Context::NetworkAdapter> Context::FindMullvadAdapter(const std::set<Context::NetworkAdapter> &tapAdapters)
{
if (tapAdapters.empty())
{
return std::nullopt;
}
//
// Look for TAP adapter with alias "Mullvad".
//
auto findByAlias = [](const std::set<NetworkAdapter> &adapters, const std::wstring &alias)
{
const auto it = std::find_if(adapters.begin(), adapters.end(), [&alias](const NetworkAdapter &candidate)
{
return 0 == _wcsicmp(candidate.alias.c_str(), alias.c_str());
});
return it;
};
static const wchar_t baseAlias[] = L"Mullvad";
const auto mullvadAdapter = findByAlias(tapAdapters, baseAlias);
if (tapAdapters.end() != mullvadAdapter)
{
return { *mullvadAdapter };
}
//
// Look for TAP adapter with alias "Mullvad-1", "Mullvad-2", etc.
//
for (auto i = 0; i < 10; ++i)
{
std::wstringstream ss;
ss << baseAlias << L"-" << i;
const auto alias = ss.str();
const auto mullvadAdapter = findByAlias(tapAdapters, alias);
if (tapAdapters.end() != mullvadAdapter)
{
return { *mullvadAdapter };
}
}
return std::nullopt;
}
Context::BaselineStatus Context::establishBaseline()
{
m_baseline = GetAllAdapters();
const auto tapAdapters = GetTapAdapters(m_baseline);
if (tapAdapters.empty())
{
return BaselineStatus::NO_TAP_ADAPTERS_PRESENT;
}
if (FindMullvadAdapter(tapAdapters).has_value())
{
return BaselineStatus::MULLVAD_ADAPTER_PRESENT;
}
return BaselineStatus::SOME_TAP_ADAPTERS_PRESENT;
}
void Context::recordCurrentState()
{
m_currentState = GetAllAdapters();
}
Context::NetworkAdapter Context::getNewAdapter()
{
std::list<NetworkAdapter> added;
const auto baselineTaps = GetTapAdapters(m_baseline);
const auto currentTaps = GetTapAdapters(m_currentState);
for (const auto &adapter : currentTaps)
{
if (baselineTaps.end() == baselineTaps.find(adapter))
{
added.push_back(adapter);
}
}
if (added.size() != 1)
{
LogAdapters(L"Enumerable network adapters", m_currentState);
throw std::runtime_error("Unable to identify recently added TAP adapter");
}
return *added.begin();
}
//static
Context::DeletionResult Context::DeleteMullvadAdapter()
{
auto tapAdapters = GetTapAdapters(GetAllAdapters());
std::optional<NetworkAdapter> mullvadAdapter = FindMullvadAdapter(tapAdapters);
if (!mullvadAdapter.has_value())
{
throw std::runtime_error("Mullvad TAP adapter not found");
}
const auto mullvadGuid = mullvadAdapter.value().guid;
HDEVINFO devInfo = SetupDiGetClassDevsW(
&GUID_DEVCLASS_NET,
nullptr,
nullptr,
DIGCF_PRESENT
);
THROW_GLE_IF(INVALID_HANDLE_VALUE, devInfo, "SetupDiGetClassDevs() failed");
common::memory::ScopeDestructor cleanupDevList;
cleanupDevList += [&devInfo]()
{
SetupDiDestroyDeviceInfoList(devInfo);
};
SP_DEVINFO_DATA devInfoData;
std::vector<wchar_t> buffer;
DWORD nameLen;
int numRemainingAdapters = 0;
for (int memberIndex = 0; ; memberIndex++)
{
devInfoData = { 0 };
devInfoData.cbSize = sizeof(devInfoData);
if (FALSE == SetupDiEnumDeviceInfo(devInfo, memberIndex, &devInfoData))
{
if (GetLastError() == ERROR_NO_MORE_ITEMS)
{
/* done */
break;
}
THROW_GLE("Error enumerating network adapters");
}
if (FALSE == SetupDiGetDeviceRegistryPropertyW(
devInfo,
&devInfoData,
SPDRP_HARDWAREID,
nullptr,
nullptr,
0,
&nameLen
))
{
const auto status = GetLastError();
if (ERROR_INSUFFICIENT_BUFFER != status)
{
/* ERROR_INSUFFICIENT_BUFFER is expected */
if (ERROR_INVALID_DATA == status)
{
/* ERROR_INVALID_DATA may mean that the property does not exist */
continue;
}
THROW_GLE("Error obtaining network adapter hardware ID length");
}
}
buffer.resize(nameLen / sizeof(wchar_t) + 1);
buffer[nameLen / sizeof(wchar_t)] = L'\0';
if (FALSE == SetupDiGetDeviceRegistryPropertyW(
devInfo,
&devInfoData,
SPDRP_HARDWAREID,
nullptr,
reinterpret_cast<PBYTE>(buffer.data()),
(buffer.size() - 1) * sizeof(wchar_t),
nullptr
))
{
THROW_GLE("Error obtaining network adapter hardware ID");
}
if (wcscmp(TAP_HARDWARE_ID, buffer.data()) == 0)
{
std::wstring netCfgInstanceId = GetNetCfgInstanceId(devInfo, devInfoData);
if (netCfgInstanceId.compare(mullvadGuid) != 0)
{
numRemainingAdapters++;
continue;
}
if (FALSE == SetupDiRemoveDevice(
devInfo,
&devInfoData
))
{
THROW_GLE("Error removing Mullvad TAP device");
}
}
}
if (numRemainingAdapters > 0)
{
return DeletionResult::SOME_REMAINING_TAP_ADAPTERS;
}
return DeletionResult::NO_REMAINING_TAP_ADAPTERS;
}
|