master
h 93 lines 3.72 KB
Raw
1 // Copyright (C) Microsoft Corporation. All rights reserved.
2
3 #pragma once
4
5 #include <netlistmgr.h>
6
7 #include "INetworkingEngine.h"
8 #include "GnsChannel.h"
9 #include "DnsResolver.h"
10 #include "WslCoreConfig.h"
11 #include "WslCoreNetworkEndpointSettings.h"
12 #include "WslCoreHostDnsInfo.h"
13 #include "WslCoreNetworkingSupport.h"
14 #include "hns_schema.h"
15
16 namespace wsl::core {
17
18 class NatNetworking final : public INetworkingEngine
19 {
20 public:
21 NatNetworking(
22 HCS_SYSTEM system,
23 wsl::windows::common::hcs::unique_hcn_network&& network,
24 GnsChannel&& gnsChannel,
25 Config& config,
26 wil::unique_socket&& dnsHvsocket,
27 LPCWSTR dnsOptions = LX_INIT_RESOLVCONF_FULL_HEADER);
28 ~NatNetworking() override;
29
30 // Note: This class cannot be moved because m_networkNotifyHandle captures a 'this' pointer.
31 NatNetworking(const NatNetworking&) = delete;
32 NatNetworking(NatNetworking&&) = delete;
33 NatNetworking& operator=(const NatNetworking&) = delete;
34 NatNetworking& operator=(NatNetworking&&) = delete;
35
36 void Initialize() override;
37 void TraceLoggingRundown() noexcept override;
38 void FillInitialConfiguration(LX_MINI_INIT_NETWORKING_CONFIGURATION& message) override;
39 void StartPortTracker(wil::unique_socket&& socket) override;
40
41 static wsl::windows::common::hcs::unique_hcn_network CreateNetwork(wsl::core::Config& config);
42 static bool IsHyperVFirewallSupported(const wsl::core::Config& vmConfig) noexcept;
43
44 private:
45 static void NETIOAPI_API_ OnNetworkConnectivityChange(PVOID context, NL_NETWORK_CONNECTIVITY_HINT hint);
46 static std::optional<ULONGLONG> FindNatInterfaceLuid(const SOCKADDR_INET& natAddress, const NL_NETWORK_CONNECTIVITY_HINT& currentConnectivityHint);
47 std::pair<networking::EphemeralHcnEndpoint, wsl::shared::hns::HNSEndpoint> CreateEndpoint(const std::wstring& IpAddress) const;
48 static wsl::windows::common::hcs::unique_hcn_network CreateNetworkInternal(wsl::core::Config& config);
49
50 void RefreshGuestConnection(NL_NETWORK_CONNECTIVITY_HINT connectivityHint) noexcept;
51 _Requires_lock_held_(m_lock)
52 void UpdateDns(std::optional<PCWSTR> gatewayAddress = std::nullopt) noexcept;
53 void UpdateMtu();
54 void AttachEndpoint(networking::EphemeralHcnEndpoint&& endpoint, const wsl::shared::hns::HNSEndpoint& properties);
55 void TelemetryConnectionCallback(NLM_CONNECTIVITY hostConnectivity, uint32_t telemetryCounter) noexcept;
56
57 mutable wil::srwlock m_lock;
58
59 // Handle for the Hcn* Api. Owned by the caller (WslCoreVm), this is a non-owning copy
60 const HCS_SYSTEM m_system{};
61 Config& m_config;
62 wsl::windows::common::hcs::unique_hcn_network m_network{};
63
64 bool m_connectivityTelemetryEnabled{false};
65 wsl::core::networking::ConnectivityTelemetry m_connectivityTelemetry;
66
67 // Optional DNS resolver used for DNS tunneling
68 std::optional<networking::DnsResolver> m_dnsTunnelingResolver;
69
70 std::wstring m_dnsTunnelingIpAddress;
71
72 std::chrono::time_point<std::chrono::steady_clock> m_objectCreationTime = std::chrono::steady_clock::now();
73
74 std::optional<networking::DnsSuffixRegistryWatcher> m_dnsSuffixRegistryWatcher;
75 // The latest DNS settings configured in Linux
76 _Guarded_by_(m_lock) networking::DnsInfo m_trackedDnsSettings {};
77
78 // If true, DNS settings are retrieved from host adapters (mirrored mode)
79 // rather than using the shared access DNS proxy
80 bool m_useMirrorDnsSettings = false;
81
82 // Options/header for /etc/resolv.conf
83 LPCWSTR m_dnsOptions = nullptr;
84
85 GnsChannel m_gnsChannel;
86 std::shared_ptr<networking::NetworkSettings> m_networkSettings;
87 networking::EphemeralHcnEndpoint m_endpoint;
88 ULONG m_networkMtu = 0;
89
90 networking::unique_notify_handle m_networkNotifyHandle{};
91 };
92
93 } // namespace wsl::core