-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathVirtioNetworking.cpp
More file actions
409 lines (347 loc) · 14.8 KB
/
VirtioNetworking.cpp
File metadata and controls
409 lines (347 loc) · 14.8 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
// Copyright (C) Microsoft Corporation. All rights reserved.
#include "precomp.h"
#include "VirtioNetworking.h"
#include "GuestDeviceManager.h"
#include "Stringify.h"
#include "stringshared.h"
using namespace wsl::core::networking;
using namespace wsl::shared;
using namespace wsl::windows::common::stringify;
using wsl::core::VirtioNetworking;
static constexpr auto c_eth0DeviceName = L"eth0";
static constexpr auto c_loopbackDeviceName = TEXT(LX_INIT_LOOPBACK_DEVICE_NAME);
VirtioNetworking::VirtioNetworking(
GnsChannel&& gnsChannel,
VirtioNetworkingFlags flags,
LPCWSTR dnsOptions,
std::shared_ptr<GuestDeviceManager> guestDeviceManager,
wil::shared_handle userToken,
wil::unique_socket&& dnsHvsocket) :
m_guestDeviceManager(std::move(guestDeviceManager)),
m_userToken(std::move(userToken)),
m_gnsChannel(std::move(gnsChannel)),
m_flags(flags),
m_dnsOptions(dnsOptions)
{
THROW_HR_IF_MSG(
E_INVALIDARG,
((!!dnsHvsocket != WI_IsFlagSet(m_flags, VirtioNetworkingFlags::DnsTunnelingSocket)) ||
(WI_IsFlagSet(m_flags, VirtioNetworkingFlags::DnsTunnelingSocket) && WI_IsFlagSet(m_flags, VirtioNetworkingFlags::DnsTunneling))),
"Incompatible DNS settings");
if (dnsHvsocket)
{
networking::DnsResolverFlags resolverFlags{};
m_dnsTunnelingResolver.emplace(std::move(dnsHvsocket), resolverFlags);
}
}
VirtioNetworking::~VirtioNetworking()
{
// Unregister the network notification callback to prevent it from using the GNS channel.
m_networkNotifyHandle.reset();
// Stop the GNS channel to unblock any stuck communications with the guest.
m_gnsChannel.Stop();
}
void VirtioNetworking::Initialize()
{
// Initialize adapter state.
RefreshGuestConnection();
if (WI_IsFlagSet(m_flags, VirtioNetworkingFlags::LocalhostRelay))
{
SetupLoopbackDevice();
}
THROW_IF_WIN32_ERROR(NotifyNetworkConnectivityHintChange(&VirtioNetworking::OnNetworkConnectivityChange, this, TRUE, &m_networkNotifyHandle));
}
void VirtioNetworking::TraceLoggingRundown() noexcept
{
auto lock = m_lock.lock_exclusive();
WSL_LOG("VirtioNetworking::TraceLoggingRundown", TRACE_NETWORKSETTINGS_OBJECT(m_networkSettings));
}
void VirtioNetworking::FillInitialConfiguration(LX_MINI_INIT_NETWORKING_CONFIGURATION& message)
{
message.NetworkingMode = LxMiniInitNetworkingModeVirtioProxy;
message.DisableIpv6 = WI_IsFlagClear(m_flags, VirtioNetworkingFlags::Ipv6);
message.EnableDhcpClient = false;
message.PortTrackerType = LX_MINI_INIT_PORT_TRACKER_TYPE::LxMiniInitPortTrackerTypeMirrored;
}
void VirtioNetworking::StartPortTracker(wil::unique_socket&& socket)
{
WI_ASSERT(!m_gnsPortTrackerChannel.has_value());
m_gnsPortTrackerChannel.emplace(
std::move(socket),
[&](const SOCKADDR_INET& addr, int protocol, bool allocate) { return HandlePortNotification(addr, protocol, allocate); },
[](const std::string&, bool) {}); // TODO: reconsider if InterfaceStateCallback is needed.
}
void NETIOAPI_API_ VirtioNetworking::OnNetworkConnectivityChange(PVOID context, NL_NETWORK_CONNECTIVITY_HINT hint)
{
static_cast<VirtioNetworking*>(context)->RefreshGuestConnection();
}
HRESULT VirtioNetworking::HandlePortNotification(const SOCKADDR_INET& addr, int protocol, bool allocate) const noexcept
{
if (addr.si_family == AF_INET6 && WI_IsFlagClear(m_flags, VirtioNetworkingFlags::Ipv6))
{
return S_OK;
}
int result = 0;
const auto ipAddress = (addr.si_family == AF_INET) ? reinterpret_cast<const void*>(&addr.Ipv4.sin_addr)
: reinterpret_cast<const void*>(&addr.Ipv6.sin6_addr);
const bool loopback = INET_IS_ADDR_LOOPBACK(addr.si_family, ipAddress);
const bool unspecified = INET_IS_ADDR_UNSPECIFIED(addr.si_family, ipAddress);
if (addr.si_family == AF_INET && loopback)
{
// Only intercepting 127.0.0.1; any other loopback address will remain on 'lo'.
if (addr.Ipv4.sin_addr.s_addr != htonl(INADDR_LOOPBACK))
{
return result;
}
}
if (WI_IsFlagSet(m_flags, VirtioNetworkingFlags::LocalhostRelay) && (unspecified || loopback))
{
SOCKADDR_INET localAddr = addr;
if (!loopback)
{
INETADDR_SETLOOPBACK(reinterpret_cast<PSOCKADDR>(&localAddr));
if (addr.si_family == AF_INET)
{
localAddr.Ipv4.sin_port = addr.Ipv4.sin_port;
}
else
{
localAddr.Ipv6.sin6_port = addr.Ipv6.sin6_port;
}
}
result = ModifyOpenPorts(c_loopbackDeviceName, localAddr, protocol, allocate);
LOG_HR_IF_MSG(
E_FAIL, result != S_OK, "Failure adding localhost relay port %d", INETADDR_PORT(reinterpret_cast<const SOCKADDR*>(&localAddr)));
}
if (!loopback)
{
const int localResult = ModifyOpenPorts(c_eth0DeviceName, addr, protocol, allocate);
LOG_HR_IF_MSG(E_FAIL, localResult != S_OK, "Failure adding relay port %d", INETADDR_PORT(reinterpret_cast<const SOCKADDR*>(&addr)));
if (result == 0)
{
result = localResult;
}
}
return result;
}
int VirtioNetworking::ModifyOpenPorts(_In_ PCWSTR tag, _In_ const SOCKADDR_INET& addr, _In_ int protocol, _In_ bool isOpen) const
{
if (protocol != IPPROTO_TCP && protocol != IPPROTO_UDP)
{
LOG_HR_MSG(HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED), "Unsupported bind protocol %d", protocol);
return 0;
}
auto lock = m_lock.lock_exclusive();
const auto server = m_guestDeviceManager->GetRemoteFileSystem(VIRTIO_NET_CLASS_ID, c_defaultDeviceTag);
if (server)
{
std::wstring portString = std::format(L"tag={};port_number={}", tag, INETADDR_PORT(reinterpret_cast<const SOCKADDR*>(&addr)));
if (protocol == IPPROTO_UDP)
{
portString += L";udp";
}
if (!isOpen)
{
portString += L";allocate=false";
}
else
{
const auto addrStr = wsl::windows::common::string::SockAddrInetToWstring(addr);
portString += std::format(L";listen_addr={}", addrStr);
}
LOG_IF_FAILED(server->AddShare(portString.c_str(), nullptr, 0));
}
return 0;
}
void VirtioNetworking::RefreshGuestConnection() noexcept
try
{
// Query current networking information before acquiring the lock.
auto networkSettings = GetHostEndpointSettings();
std::wstring device_options;
auto appendOption = [&device_options](std::wstring_view key, std::wstring_view value) {
if (!value.empty())
{
std::format_to(std::back_inserter(device_options), L"{}{}={}", device_options.empty() ? L"" : L";", key, value);
}
};
appendOption(L"client_ip", networkSettings->PreferredIpAddress.AddressString);
appendOption(L"client_mac", networkSettings->MacAddress);
std::wstring default_route = networkSettings->GetBestGatewayAddressString();
appendOption(L"gateway_ip", default_route);
appendOption(L"gateway_mac", networkSettings->GetBestGatewayMacAddress(AF_INET));
if (WI_IsFlagSet(m_flags, VirtioNetworkingFlags::Ipv6))
{
appendOption(L"client_ip_ipv6", networkSettings->PreferredIpv6Address.AddressString);
appendOption(L"gateway_mac_ipv6", networkSettings->GetBestGatewayMacAddress(AF_INET6));
}
networking::DnsInfo currentDns{};
if (WI_IsFlagSet(m_flags, VirtioNetworkingFlags::DnsTunneling))
{
currentDns = networking::HostDnsInfo::GetDnsTunnelingSettings(default_route);
}
else if (WI_IsFlagSet(m_flags, VirtioNetworkingFlags::DnsTunnelingSocket))
{
currentDns = networking::HostDnsInfo::GetDnsTunnelingSettings(TEXT(LX_INIT_DNS_TUNNELING_IP_ADDRESS));
}
else
{
wsl::core::networking::DnsSettingsFlags dnsFlags = networking::DnsSettingsFlags::IncludeVpn;
WI_SetFlagIf(dnsFlags, networking::DnsSettingsFlags::IncludeIpv6Servers, WI_IsFlagSet(m_flags, VirtioNetworkingFlags::Ipv6));
currentDns = networking::HostDnsInfo::GetDnsSettings(dnsFlags);
}
const auto minMtu = GetMinimumConnectedInterfaceMtu();
// Acquire the lock and perform device updates.
auto lock = m_lock.lock_exclusive();
// Add virtio net adapter to guest. If the adapter already exists update adapter state.
if (device_options != m_trackedDeviceOptions)
{
m_trackedDeviceOptions = device_options;
if (!m_adapterId.has_value())
{
m_adapterId = m_guestDeviceManager->AddGuestDevice(
VIRTIO_NET_DEVICE_ID, VIRTIO_NET_CLASS_ID, c_eth0DeviceName, nullptr, device_options.c_str(), 0, m_userToken.get());
}
else
{
const auto server = m_guestDeviceManager->GetRemoteFileSystem(VIRTIO_NET_CLASS_ID, c_defaultDeviceTag);
if (server)
{
LOG_IF_FAILED(server->AddSharePath(c_eth0DeviceName, device_options.c_str(), 0));
}
}
}
UpdateIpv4Address(networkSettings->PreferredIpAddress);
if (WI_IsFlagSet(m_flags, VirtioNetworkingFlags::Ipv6))
{
UpdateIpv6Address(networkSettings->PreferredIpv6Address);
}
UpdateDefaultRoute(default_route);
UpdateDnsSettings(currentDns);
UpdateMtu(minMtu);
m_networkSettings = std::move(networkSettings);
}
CATCH_LOG();
void VirtioNetworking::SetupLoopbackDevice()
{
m_localhostAdapterId = m_guestDeviceManager->AddGuestDevice(
VIRTIO_NET_DEVICE_ID,
VIRTIO_NET_CLASS_ID,
c_loopbackDeviceName,
nullptr,
L"client_ip=127.0.0.1;client_mac=00:11:22:33:44:55",
0,
m_userToken.get());
// The loopback gateway (see LX_INIT_IPV4_LOOPBACK_GATEWAY_ADDRESS) is 169.254.73.152, so assign loopback0 an
// address of 169.254.73.153 with a netmask of 30 so that the only addresses associated with this adapter are
// itself and the gateway.
// N.B. The MAC address is advertised with the virtio device so doesn't need to be explicitly set.
hns::HNSEndpoint endpointProperties;
endpointProperties.ID = m_localhostAdapterId.value();
endpointProperties.IPAddress = L"169.254.73.153";
endpointProperties.PrefixLength = 30;
endpointProperties.PortFriendlyName = c_loopbackDeviceName;
m_gnsChannel.SendEndpointState(endpointProperties);
hns::CreateDeviceRequest createLoopbackDevice;
createLoopbackDevice.deviceName = c_loopbackDeviceName;
createLoopbackDevice.type = hns::DeviceType::Loopback;
createLoopbackDevice.lowerEdgeAdapterId = m_localhostAdapterId.value();
constexpr auto loopbackType = GnsMessageType(createLoopbackDevice);
m_gnsChannel.SendNetworkDeviceMessage(loopbackType, ToJsonW(createLoopbackDevice).c_str());
}
void VirtioNetworking::SendDefaultRoute(const std::wstring& gateway, hns::ModifyRequestType requestType)
{
if (gateway.empty() || !m_adapterId.has_value())
{
return;
}
wsl::shared::hns::Route route;
route.NextHop = gateway;
route.DestinationPrefix = LX_INIT_DEFAULT_ROUTE_PREFIX;
route.Family = AF_INET;
hns::ModifyGuestEndpointSettingRequest<hns::Route> request;
request.RequestType = requestType;
request.ResourceType = hns::GuestEndpointResourceType::Route;
request.Settings = route;
m_gnsChannel.SendHnsNotification(ToJsonW(request).c_str(), m_adapterId.value());
}
void VirtioNetworking::UpdateDefaultRoute(const std::wstring& gateway)
{
if (gateway == m_trackedDefaultRoute || !m_adapterId.has_value())
{
return;
}
SendDefaultRoute(m_trackedDefaultRoute, hns::ModifyRequestType::Remove);
m_trackedDefaultRoute = gateway;
SendDefaultRoute(gateway, hns::ModifyRequestType::Add);
}
void VirtioNetworking::UpdateDnsSettings(const networking::DnsInfo& dns)
{
if (dns == m_trackedDnsSettings || !m_adapterId.has_value())
{
return;
}
m_trackedDnsSettings = dns;
hns::ModifyGuestEndpointSettingRequest<hns::DNS> notification{};
notification.RequestType = hns::ModifyRequestType::Update;
notification.ResourceType = hns::GuestEndpointResourceType::DNS;
notification.Settings = networking::BuildDnsNotification(dns, m_dnsOptions);
m_gnsChannel.SendHnsNotification(ToJsonW(notification).c_str(), m_adapterId.value());
}
void VirtioNetworking::UpdateIpv4Address(const networking::EndpointIpAddress& ipAddress)
{
if (ipAddress == m_trackedIpv4Address || ipAddress.AddressString.empty() || !m_adapterId.has_value())
{
return;
}
m_trackedIpv4Address = ipAddress;
// N.B. SendEndpointState triggers SetAdapterConfiguration on the Linux side
// which brings the interface UP and configures the full adapter state.
hns::HNSEndpoint endpointProperties;
endpointProperties.ID = m_adapterId.value();
endpointProperties.IPAddress = ipAddress.AddressString;
endpointProperties.PrefixLength = ipAddress.PrefixLength;
m_gnsChannel.SendEndpointState(endpointProperties);
}
void VirtioNetworking::SendIpv6Address(const networking::EndpointIpAddress& ipAddress, hns::ModifyRequestType requestType)
{
WI_ASSERT(WI_IsFlagSet(m_flags, VirtioNetworkingFlags::Ipv6));
if (ipAddress.AddressString.empty() || !m_adapterId.has_value())
{
return;
}
// The HNSEndpoint schema doesn't support IPv6 addresses, so use ModifyGuestEndpointSettingRequest.
hns::ModifyGuestEndpointSettingRequest<hns::IPAddress> request;
request.RequestType = requestType;
request.ResourceType = hns::GuestEndpointResourceType::IPAddress;
request.Settings.Address = ipAddress.AddressString;
request.Settings.Family = ipAddress.Address.si_family;
request.Settings.OnLinkPrefixLength = ipAddress.PrefixLength;
request.Settings.PreferredLifetime = ULONG_MAX;
m_gnsChannel.SendHnsNotification(ToJsonW(request).c_str(), m_adapterId.value());
}
void VirtioNetworking::UpdateIpv6Address(const networking::EndpointIpAddress& ipAddress)
{
WI_ASSERT(WI_IsFlagSet(m_flags, VirtioNetworkingFlags::Ipv6));
if (ipAddress == m_trackedIpv6Address || !m_adapterId.has_value())
{
return;
}
SendIpv6Address(m_trackedIpv6Address, hns::ModifyRequestType::Remove);
m_trackedIpv6Address = ipAddress;
SendIpv6Address(ipAddress, hns::ModifyRequestType::Add);
}
void VirtioNetworking::UpdateMtu(std::optional<ULONG> mtu)
{
if (!mtu || mtu.value() == m_networkMtu || !m_adapterId.has_value())
{
return;
}
m_networkMtu = mtu.value();
hns::ModifyGuestEndpointSettingRequest<hns::NetworkInterface> notification{};
notification.ResourceType = hns::GuestEndpointResourceType::Interface;
notification.RequestType = hns::ModifyRequestType::Update;
notification.Settings.Connected = true;
notification.Settings.NlMtu = m_networkMtu;
m_gnsChannel.SendHnsNotification(ToJsonW(notification).c_str(), m_adapterId.value());
}