master
h 109 lines 4.97 KB
Raw
1 // Copyright (C) Microsoft Corporation. All rights reserved.
2
3 #pragma once
4
5 #include "precomp.h"
6 #include "INetworkingEngine.h"
7 #include "GnsChannel.h"
8 #include "DnsResolver.h"
9 #include "WslCoreConfig.h"
10 #include "WslCoreNetworkEndpointSettings.h"
11 #include "WslCoreMessageQueue.h"
12 #include "GnsPortTrackerChannel.h"
13 #include "GnsRpcServer.h"
14 #include "WslCoreGuestNetworkService.h"
15 #include "IMirroredNetworkManager.h"
16
17 namespace wsl::core {
18
19 class MirroredNetworking : public INetworkingEngine
20 {
21 public:
22 MirroredNetworking(HCS_SYSTEM system, GnsChannel&& gnsChannel, const Config& config, GUID runtimeId, wil::unique_socket&& dnsHvsocket);
23 ~MirroredNetworking() override;
24
25 MirroredNetworking(const MirroredNetworking&) = delete;
26 MirroredNetworking& operator=(const MirroredNetworking) = delete;
27 MirroredNetworking(MirroredNetworking&&) = delete;
28 MirroredNetworking& operator=(MirroredNetworking&&) = delete;
29
30 void Initialize() override;
31
32 void TraceLoggingRundown() noexcept override;
33
34 void FillInitialConfiguration(LX_MINI_INIT_NETWORKING_CONFIGURATION& message) override;
35
36 void StartPortTracker(wil::unique_socket&& socket) override;
37
38 /// <summary>
39 /// Returns true if the interface should be constrained, false otherwise.
40 ///
41 /// This function determines if the input InterfaceGuid corresponds to an interface
42 /// that should be constrained. One can configure the ExternalInterfaceConstraint, which means
43 /// that all interfaces OTHER than the ExternalInterfaceConstraint will have its traffic constrained
44 /// (i.e restricted to only local subnet access).
45 ///
46 /// This function returns TRUE if there is an ExternalInterfaceConstraint configured AND
47 /// this interface does not match the ExternalInterfaceConstraint (which means that this interface is
48 /// restricted to communicate ONLY over the local subnet)
49 /// This function returns FALSE otherwise (which means that this interface has no restrictions on it)
50 ///
51 /// If any errors occur while trying to determine the ExternalInterfaceConstraint, this function will
52 /// default to returning FALSE (i.e non-constrained, normal traffic allowed interface)
53 /// </summary>
54 static bool IsExternalInterfaceConstrained(const HCN_NETWORK network) noexcept;
55
56 static bool IsHyperVFirewallSupported(const wsl::core::Config& vmConfig) noexcept;
57
58 private:
59 void AddNetworkEndpoint(const GUID& NetworkId) noexcept;
60
61 HRESULT OnNetworkEndpointChange(const GUID& Endpoint, _In_ LPCWSTR Settings) const noexcept;
62
63 // callbacks
64 HRESULT NetworkManagerGnsMessageCallback(
65 LX_MESSAGE_TYPE messageType, std::wstring notificationString, networking::GnsCallbackFlags callbackFlags, _Out_opt_ int* returnedValueFromGns) noexcept;
66 static void GuestNetworkServiceCallback(DWORD NotificationType, HRESULT NotificationStatus, _In_opt_ PCWSTR NotificationData) noexcept;
67 static void CALLBACK s_GuestNetworkServiceCallback(DWORD NotificationType, _In_ void* Context, HRESULT NotificationStatus, _In_opt_ PCWSTR NotificationData);
68
69 // Handle owned by WslCoreVm
70 const HCS_SYSTEM m_system{};
71 const GUID m_runtimeId;
72 const Config& m_config;
73
74 // holding the MTA for our COM callback
75 wsl::windows::common::helpers::unique_mta_cookie m_mtaCookie{};
76 std::optional<GnsPortTrackerChannel> m_gnsPortTrackerChannel;
77 std::shared_ptr<GnsRpcServer> m_gnsRpcServer;
78 // mutable allows m_gnsMessageQueue to submit from const methods
79 mutable WslCoreMessageQueue m_gnsMessageQueue;
80 networking::GuestNetworkService m_guestNetworkService;
81
82 // m_network* and m_gnsChannel must be accessed only from within the m_networkingQueue
83 // which serializes all workitems through a single-threaded queue
84 // This unwinds the locking/dependencies with the GNS channel (and its callbacks) and HNS APIs (Hcn*)
85 GnsChannel m_gnsChannel;
86 std::unique_ptr<networking::IMirroredNetworkManager> m_networkManager;
87 mutable WslCoreMessageQueue m_networkingQueue;
88
89 // Optional DNS resolver used for DNS tunneling
90 std::optional<networking::DnsResolver> m_dnsTunnelingResolver;
91
92 std::optional<networking::DnsSuffixRegistryWatcher> m_dnsSuffixRegistryWatcher;
93
94 std::optional<networking::NetworkSettings> m_networkPreferredSettings;
95 ULONG m_networkNatMtu = ULONG_MAX;
96 networking::unique_notify_handle m_networkNotificationHandle{};
97 networking::unique_notify_handle m_interfaceNotificationHandle{};
98 networking::unique_notify_handle m_routeNotificationHandle{};
99 networking::unique_notify_handle m_addressNotificationHandle{};
100
101 // track network-id to endpoint-id
102 // we can avoid recreating vmNICs by reusing the same endpoint-id values
103 std::map<GUID, GUID, wsl::windows::common::helpers::GuidLess> m_networkIdMappings;
104
105 // Ephemeral port range allocated for the VM.
106 std::pair<uint16_t, uint16_t> m_ephemeralPortRange;
107 };
108
109 } // namespace wsl::core