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
|
#include "stdafx.h"
#include "error.h"
#include "device.h"
#include "service.h"
#include "log.h"
#include "version.h"
#include "wintun.h"
#include "wireguard.h"
#include "devenum.h"
#include <string>
#include <libcommon/error.h>
#include <libcommon/memory.h>
#include <libcommon/string.h>
#include <initguid.h>
#include <devpkey.h>
#include <devguid.h>
#include <io.h>
#include <fcntl.h>
namespace
{
constexpr wchar_t SPLIT_TUNNEL_HARDWARE_ID[] = L"Root\\mullvad-split-tunnel";
DEFINE_GUID(WFP_CALLOUTS_CLASS_ID,
0x57465043, 0x616C, 0x6C6F, 0x75, 0x74, 0x5F, 0x63, 0x6C, 0x61, 0x73, 0x73);
constexpr wchar_t SPLIT_TUNNEL_DEVICE_NAME[] = L"Mullvad Split Tunnel Device";
enum ReturnCode
{
GENERAL_SUCCESS = 0,
GENERAL_ERROR = 1,
ST_DRIVER_NONE_INSTALLED = 2,
ST_DRIVER_SAME_VERSION_INSTALLED = 3,
ST_DRIVER_OLDER_VERSION_INSTALLED = 4,
ST_DRIVER_NEWER_VERSION_INSTALLED = 5
};
class ArgumentContext
{
public:
ArgumentContext(const std::vector<std::wstring> &args)
: m_args(args)
, m_remaining(m_args.size())
{
}
size_t total() const
{
return m_args.size();
}
void ensureExactArgumentCount(size_t count) const
{
if (m_args.size() != count)
{
throw std::runtime_error("Invalid number of arguments");
}
}
const std::wstring &next()
{
if (0 == m_remaining)
{
throw std::runtime_error("Argument missing");
}
const auto &str = m_args.at(m_args.size() - m_remaining);
--m_remaining;
return str;
}
private:
const std::vector<std::wstring> &m_args;
size_t m_remaining;
};
void ResetDriverState()
{
auto deviceHandle = OpenSplitTunnelDevice();
common::memory::ScopeDestructor dtor;
dtor += [deviceHandle]()
{
CloseSplitTunnelDevice(deviceHandle);
};
SendIoControlReset(deviceHandle);
}
std::unique_ptr<DeviceEnumerator> CreateSplitTunnelDeviceEnumerator()
{
return DeviceEnumerator::Create(WFP_CALLOUTS_CLASS_ID, [](HDEVINFO deviceInfoSet, const SP_DEVINFO_DATA &deviceInfo)
{
auto candidateDeviceName = GetDeviceStringProperty(deviceInfoSet, deviceInfo, &DEVPKEY_NAME);
return 0 == candidateDeviceName.compare(SPLIT_TUNNEL_DEVICE_NAME);
});
}
//
// CommandSplitTunnelEvaluate()
//
// Search for existing device.
// Evaluate if provided inf can/should be installed.
//
ReturnCode CommandSplitTunnelEvaluate(const std::vector<std::wstring> &args)
{
ArgumentContext argsContext(args);
argsContext.ensureExactArgumentCount(1);
const auto infPath = argsContext.next();
//
// Find first matching device
//
auto enumerator = CreateSplitTunnelDeviceEnumerator();
EnumeratedDevice device;
if (!enumerator->next(device))
{
return ReturnCode::ST_DRIVER_NONE_INSTALLED;
}
//
// Retrieve driver versions
//
auto existingVersion = GetDriverVersion(device);
auto proposedVersion = InfGetDriverVersion(infPath);
//
// Compare driver versions
//
switch (EvaluateDriverUpgrade(existingVersion, proposedVersion))
{
case DRIVER_UPGRADE_STATUS::WOULD_UPGRADE:
return ReturnCode::ST_DRIVER_OLDER_VERSION_INSTALLED;
case DRIVER_UPGRADE_STATUS::WOULD_DOWNGRADE:
return ReturnCode::ST_DRIVER_NEWER_VERSION_INSTALLED;
case DRIVER_UPGRADE_STATUS::WOULD_INSTALL_SAME_VERSION:
return ReturnCode::ST_DRIVER_SAME_VERSION_INSTALLED;
default:
Log(L"Unexpected return value from EvaluateDriverUpgrade()");
}
return ReturnCode::GENERAL_ERROR;
}
ReturnCode CommandSplitTunnelNewInstall(const std::vector<std::wstring> &args)
{
ArgumentContext argsContext(args);
argsContext.ensureExactArgumentCount(1);
const auto infPath = argsContext.next();
CreateDevice(WFP_CALLOUTS_CLASS_ID, SPLIT_TUNNEL_DEVICE_NAME, SPLIT_TUNNEL_HARDWARE_ID);
InstallDriverForDevice(SPLIT_TUNNEL_HARDWARE_ID, infPath);
return ReturnCode::GENERAL_SUCCESS;
}
//
// CommandSplitTunnelRemove()
//
// Reset driver
// Uninstall device
// Stop service
// Delete service
//
ReturnCode CommandSplitTunnelRemove(const std::vector<std::wstring> &args)
{
ArgumentContext argsContext(args);
argsContext.ensureExactArgumentCount(0);
//
// Find first matching device
//
auto enumerator = CreateSplitTunnelDeviceEnumerator();
EnumeratedDevice device;
if (!enumerator->next(device))
{
Log(L"Could not find split tunnel device");
return ReturnCode::GENERAL_SUCCESS;
}
ResetDriverState();
UninstallDevice(device);
PokeService(L"mullvad-split-tunnel", true, true);
return ReturnCode::GENERAL_SUCCESS;
}
//
// CommandSplitTunnelForceInstall()
//
// There's an existing device that needs to be stopped and removed.
// After this, create a new device and associate the specified inf.
//
ReturnCode CommandSplitTunnelForceInstall(const std::vector<std::wstring> &args)
{
auto status = CommandSplitTunnelRemove({});
if (ReturnCode::GENERAL_SUCCESS != status)
{
return status;
}
return CommandSplitTunnelNewInstall(args);
}
ReturnCode CommandWintunDeletePool(const std::vector<std::wstring> &args)
{
ArgumentContext argsContext(args);
argsContext.ensureExactArgumentCount(1);
const auto poolName = argsContext.next();
WintunDll wintun;
BOOL rebootRequired;
if (FALSE == wintun.deletePoolDriver(poolName.c_str(), &rebootRequired))
{
throw std::runtime_error("Failed to delete wintun pool");
}
std::wstringstream ss;
ss << L"Successfully deleted wintun pool. Reboot required: " << rebootRequired;
Log(ss.str());
return ReturnCode::GENERAL_SUCCESS;
}
ReturnCode CommandWintunDeleteAbandonedDevice(const std::vector<std::wstring> &args)
{
ArgumentContext argsContext(args);
argsContext.ensureExactArgumentCount(0);
auto enumerator = DeviceEnumerator::Create(GUID_DEVCLASS_NET, [](HDEVINFO deviceInfoSet, const SP_DEVINFO_DATA &deviceInfo)
{
static wchar_t WintunMullvadAdapter[] = L"{AFE43773-E1F8-4EBB-8536-576AB86AFE9A}";
auto candidateAdapterGuid = GetDeviceNetCfgInstanceId(deviceInfoSet, deviceInfo);
return 0 == _wcsicmp(candidateAdapterGuid.c_str(), WintunMullvadAdapter);
});
EnumeratedDevice device;
if (enumerator->next(device))
{
UninstallDevice(device);
}
return GENERAL_SUCCESS;
}
ReturnCode CommandWireGuardNtCleanup(const std::vector<std::wstring> &args)
{
ArgumentContext argsContext(args);
argsContext.ensureExactArgumentCount(0);
WireGuardNtDll wgNt;
if (FALSE == wgNt.deleteDriver())
{
throw std::runtime_error("Failed to delete WireGuardNT driver");
}
Log(L"Successfully deleted WireGuardNT driver");
return ReturnCode::GENERAL_SUCCESS;
}
} // anonymous namespace
int wmain(int argc, const wchar_t *argv[])
{
if (-1 == _setmode(_fileno(stdout), _O_U16TEXT)
|| -1 == _setmode(_fileno(stderr), _O_U16TEXT))
{
Log(L"Failed to set translation mode");
}
if (argc < 2)
{
Log(L"Command not specified");
return ReturnCode::GENERAL_ERROR;
}
//
// Re-package command arguments
//
const std::wstring command = argv[1];
std::vector<std::wstring> arguments;
for (size_t argumentIndex = 2; argumentIndex < argc; ++argumentIndex)
{
arguments.emplace_back(argv[argumentIndex]);
}
//
// Declare all handlers
//
struct CommandHandler
{
std::wstring commandName;
std::function<ReturnCode(const std::vector<std::wstring> &)> handler;
};
std::vector<CommandHandler> handlers =
{
{ L"st-evaluate", CommandSplitTunnelEvaluate },
{ L"st-new-install", CommandSplitTunnelNewInstall },
{ L"st-force-install", CommandSplitTunnelForceInstall },
{ L"st-remove", CommandSplitTunnelRemove },
{ L"wintun-delete-pool-driver", CommandWintunDeletePool },
{ L"wintun-delete-abandoned-device", CommandWintunDeleteAbandonedDevice },
{ L"wg-nt-cleanup", CommandWireGuardNtCleanup }
};
//
// Find and invoke matching handler
//
for (const auto &candidate : handlers)
{
if (0 != _wcsicmp(command.c_str(), candidate.commandName.c_str()))
{
continue;
}
try
{
return candidate.handler(arguments);
}
catch (const common::error::WindowsException &e)
{
Log(common::string::ToWide(e.what()));
return e.errorCode();
}
catch (const std::exception &e)
{
Log(common::string::ToWide(e.what()));
return GENERAL_ERROR;
}
catch (...)
{
Log(L"Unknown exception was raised/thrown");
return GENERAL_ERROR;
}
}
//
// Could not find matching handler
//
Log(L"Could not find handler for specified command");
return GENERAL_ERROR;
}
|