Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 1 | // Copyright 2021 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef DNS_PROXY_PROXY_H_ |
| 6 | #define DNS_PROXY_PROXY_H_ |
| 7 | |
| 8 | #include <iostream> |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 9 | #include <map> |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 10 | #include <memory> |
| 11 | #include <optional> |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 12 | #include <set> |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 13 | #include <string> |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 14 | #include <vector> |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 15 | |
| 16 | #include <base/memory/weak_ptr.h> |
| 17 | #include <base/files/scoped_file.h> |
| 18 | #include <brillo/daemons/dbus_daemon.h> |
| 19 | #include <chromeos/patchpanel/dbus/client.h> |
Garrick Evans | 5fe2a4f | 2021-02-03 17:04:48 +0900 | [diff] [blame] | 20 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 21 | #include <shill/dbus/client/client.h> |
| 22 | |
Garrick Evans | 77e9a13 | 2021-05-13 10:51:13 +0900 | [diff] [blame] | 23 | #include "dns-proxy/chrome_features_service_client.h" |
Garrick Evans | 34650b3 | 2021-02-03 09:24:35 +0900 | [diff] [blame] | 24 | #include "dns-proxy/resolver.h" |
Garrick Evans | 77e9a13 | 2021-05-13 10:51:13 +0900 | [diff] [blame] | 25 | #include "dns-proxy/session_monitor.h" |
Garrick Evans | 34650b3 | 2021-02-03 09:24:35 +0900 | [diff] [blame] | 26 | |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 27 | namespace dns_proxy { |
| 28 | |
| 29 | // The process that runs the actual proxying code. |
| 30 | class Proxy : public brillo::DBusDaemon { |
| 31 | public: |
| 32 | enum class Type { kSystem, kDefault, kARC }; |
| 33 | |
| 34 | struct Options { |
| 35 | Type type; |
| 36 | // Required for ARC proxies as it specifies which physical interface |
| 37 | // should (always) be tracked. This field is ignored (but should be empty) |
| 38 | // for the system and default network proxies. |
| 39 | std::string ifname; |
| 40 | }; |
| 41 | |
| 42 | explicit Proxy(const Options& opts); |
Garrick Evans | 5fe2a4f | 2021-02-03 17:04:48 +0900 | [diff] [blame] | 43 | // For testing. |
| 44 | Proxy(const Options& opts, |
| 45 | std::unique_ptr<patchpanel::Client> patchpanel, |
| 46 | std::unique_ptr<shill::Client> shill); |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 47 | Proxy(const Proxy&) = delete; |
| 48 | Proxy& operator=(const Proxy&) = delete; |
Garrick Evans | 9c8797d | 2021-02-17 21:28:10 +0900 | [diff] [blame] | 49 | ~Proxy(); |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 50 | |
| 51 | static const char* TypeToString(Type t); |
| 52 | static std::optional<Type> StringToType(const std::string& s); |
| 53 | |
| 54 | protected: |
| 55 | int OnInit() override; |
| 56 | void OnShutdown(int*) override; |
| 57 | |
Garrick Evans | 2ca050d | 2021-02-09 18:21:36 +0900 | [diff] [blame] | 58 | // Added for testing. |
Jason Jeremy Iman | 845f293 | 2021-01-31 16:12:13 +0900 | [diff] [blame] | 59 | virtual std::unique_ptr<Resolver> NewResolver(base::TimeDelta timeout, |
| 60 | base::TimeDelta retry_delay, |
| 61 | int max_num_retries); |
Garrick Evans | 2ca050d | 2021-02-09 18:21:36 +0900 | [diff] [blame] | 62 | |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 63 | private: |
Garrick Evans | 9c7afb8 | 2021-01-29 22:38:03 +0900 | [diff] [blame] | 64 | static const uint8_t kMaxShillPropertyRetries = 10; |
| 65 | |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 66 | // Helper for parsing and applying shill's DNSProxyDOHProviders property. |
| 67 | class DoHConfig { |
| 68 | public: |
| 69 | DoHConfig() = default; |
| 70 | DoHConfig(const DoHConfig&) = delete; |
| 71 | DoHConfig& operator=(const DoHConfig&) = delete; |
| 72 | ~DoHConfig() = default; |
| 73 | |
| 74 | // Stores the resolver to configure whenever settings are updated. |
| 75 | void set_resolver(Resolver* resolver); |
| 76 | |
| 77 | // |nameservers| is the list of name servers for the network the proxy is |
| 78 | // tracking. |
| 79 | void set_nameservers(const std::vector<std::string>& nameservers); |
| 80 | |
| 81 | // |settings| is the DoH providers property we get from shill. It keys, as |
| 82 | // applicable, secure DNS provider endpoints to standard DNS name servers. |
Garrick Evans | 9e5cd1e | 2021-03-11 22:07:44 +0900 | [diff] [blame] | 83 | void set_providers(const brillo::VariantDictionary& providers); |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 84 | |
| 85 | void clear(); |
| 86 | |
| 87 | private: |
| 88 | void update(); |
| 89 | |
| 90 | Resolver* resolver_{nullptr}; |
| 91 | std::vector<std::string> nameservers_; |
| 92 | // If non-empty, the secure providers to use for always-on DoH. |
| 93 | std::set<std::string> secure_providers_; |
| 94 | // If non-empty, maps name servers to secure DNS providers, for automatic |
| 95 | // update. |
| 96 | std::map<std::string, std::string> auto_providers_; |
| 97 | }; |
| 98 | |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 99 | void Setup(); |
| 100 | void OnPatchpanelReady(bool success); |
Garrick Evans | 4f5428c | 2021-02-15 11:23:54 +0900 | [diff] [blame] | 101 | void OnPatchpanelReset(bool reset); |
Garrick Evans | fe99aaa | 2021-02-12 14:32:50 +0900 | [diff] [blame] | 102 | void OnShillReady(bool success); |
Garrick Evans | 9c7afb8 | 2021-01-29 22:38:03 +0900 | [diff] [blame] | 103 | void OnShillReset(bool reset); |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 104 | |
Garrick Evans | 77e9a13 | 2021-05-13 10:51:13 +0900 | [diff] [blame] | 105 | // Triggered by the session monitor whenever the user logs in or out. |
| 106 | void OnSessionStateChanged(bool login); |
| 107 | |
| 108 | // Triggered by the Chrome features client in response to checking the status |
| 109 | // of the DNSProxyEnabled feature value. |
| 110 | void OnFeatureEnabled(base::Optional<bool> enabled); |
| 111 | void Enable(); |
| 112 | void Disable(); |
| 113 | |
Garrick Evans | 34650b3 | 2021-02-03 09:24:35 +0900 | [diff] [blame] | 114 | // Triggered whenever the device attached to the default network changes. |
| 115 | // |device| can be null and indicates the default service is disconnected. |
| 116 | void OnDefaultDeviceChanged(const shill::Client::Device* const device); |
| 117 | void OnDeviceChanged(const shill::Client::Device* const device); |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 118 | |
Garrick Evans | 73e8e5e | 2021-04-27 10:16:26 +0900 | [diff] [blame] | 119 | void MaybeCreateResolver(); |
Garrick Evans | a8c12be | 2021-02-17 16:06:45 +0900 | [diff] [blame] | 120 | void UpdateNameServers(const shill::Client::IPConfig& ipconfig); |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 121 | void OnDoHProvidersChanged(const brillo::Any& value); |
Garrick Evans | a8c12be | 2021-02-17 16:06:45 +0900 | [diff] [blame] | 122 | |
Garrick Evans | 48c84ef | 2021-01-28 11:29:42 +0900 | [diff] [blame] | 123 | // Helper func for setting the dns-proxy address in shill. |
| 124 | // Only valid for the system proxy. |
Garrick Evans | 9c7afb8 | 2021-01-29 22:38:03 +0900 | [diff] [blame] | 125 | // Will retry on failure up to |num_retries| before possibly crashing the |
| 126 | // proxy. |
| 127 | void SetShillProperty(const std::string& addr, |
| 128 | bool die_on_failure = false, |
| 129 | uint8_t num_retries = kMaxShillPropertyRetries); |
Garrick Evans | 48c84ef | 2021-01-28 11:29:42 +0900 | [diff] [blame] | 130 | |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 131 | // Return the property accessor, creating it if needed. |
| 132 | shill::Client::ManagerPropertyAccessor* shill_props(); |
| 133 | |
Garrick Evans | 5fe2a4f | 2021-02-03 17:04:48 +0900 | [diff] [blame] | 134 | FRIEND_TEST(ProxyTest, SystemProxy_OnShutdownClearsAddressPropertyOnShill); |
| 135 | FRIEND_TEST(ProxyTest, NonSystemProxy_OnShutdownDoesNotCallShill); |
| 136 | FRIEND_TEST(ProxyTest, SystemProxy_SetShillPropertyWithNoRetriesCrashes); |
| 137 | FRIEND_TEST(ProxyTest, SystemProxy_SetShillPropertyDoesntCrashIfDieFalse); |
Garrick Evans | ab03c46 | 2021-02-15 20:54:20 +0900 | [diff] [blame] | 138 | FRIEND_TEST(ProxyTest, ShillInitializedWhenReady); |
Garrick Evans | 5fe2a4f | 2021-02-03 17:04:48 +0900 | [diff] [blame] | 139 | FRIEND_TEST(ProxyTest, SystemProxy_ConnectedNamedspace); |
| 140 | FRIEND_TEST(ProxyTest, DefaultProxy_ConnectedNamedspace); |
| 141 | FRIEND_TEST(ProxyTest, ArcProxy_ConnectedNamedspace); |
| 142 | FRIEND_TEST(ProxyTest, CrashOnConnectNamespaceFailure); |
| 143 | FRIEND_TEST(ProxyTest, CrashOnPatchpanelNotReady); |
| 144 | FRIEND_TEST(ProxyTest, ShillResetRestoresAddressProperty); |
Garrick Evans | 2ca050d | 2021-02-09 18:21:36 +0900 | [diff] [blame] | 145 | FRIEND_TEST(ProxyTest, StateClearedIfDefaultServiceDrops); |
| 146 | FRIEND_TEST(ProxyTest, ArcProxy_IgnoredIfDefaultServiceDrops); |
| 147 | FRIEND_TEST(ProxyTest, StateClearedIfDefaultServiceIsNotOnline); |
| 148 | FRIEND_TEST(ProxyTest, NewResolverStartsListeningOnDefaultServiceComesOnline); |
| 149 | FRIEND_TEST(ProxyTest, CrashOnListenFailure); |
| 150 | FRIEND_TEST(ProxyTest, NameServersUpdatedOnDefaultServiceComesOnline); |
| 151 | FRIEND_TEST(ProxyTest, |
| 152 | SystemProxy_ShillPropertyUpdatedOnDefaultServiceComesOnline); |
Garrick Evans | adde985 | 2021-02-15 20:16:53 +0900 | [diff] [blame] | 153 | FRIEND_TEST(ProxyTest, SystemProxy_IgnoresVPN); |
| 154 | FRIEND_TEST(ProxyTest, SystemProxy_GetsPhysicalDeviceOnInitialVPN); |
| 155 | FRIEND_TEST(ProxyTest, DefaultProxy_UsesVPN); |
Garrick Evans | 73e8e5e | 2021-04-27 10:16:26 +0900 | [diff] [blame] | 156 | FRIEND_TEST(ProxyTest, ArcProxy_NameServersUpdatedOnDeviceChangeEvent); |
| 157 | FRIEND_TEST(ProxyTest, SystemProxy_NameServersUpdatedOnDeviceChangeEvent); |
Garrick Evans | a8c12be | 2021-02-17 16:06:45 +0900 | [diff] [blame] | 158 | FRIEND_TEST(ProxyTest, DeviceChangeEventIgnored); |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 159 | FRIEND_TEST(ProxyTest, BasicDoHDisable); |
| 160 | FRIEND_TEST(ProxyTest, BasicDoHAlwaysOn); |
| 161 | FRIEND_TEST(ProxyTest, BasicDoHAutomatic); |
Garrick Evans | 4e1fc31 | 2021-05-10 14:47:31 +0900 | [diff] [blame] | 162 | FRIEND_TEST(ProxyTest, RemovesDNSQueryParameterTemplate_AlwaysOn); |
| 163 | FRIEND_TEST(ProxyTest, RemovesDNSQueryParameterTemplate_Automatic); |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 164 | FRIEND_TEST(ProxyTest, NewResolverConfiguredWhenSet); |
| 165 | FRIEND_TEST(ProxyTest, DoHModeChangingFixedNameServers); |
| 166 | FRIEND_TEST(ProxyTest, MultipleDoHProvidersForAlwaysOnMode); |
| 167 | FRIEND_TEST(ProxyTest, MultipleDoHProvidersForAutomaticMode); |
| 168 | FRIEND_TEST(ProxyTest, DoHBadAlwaysOnConfigSetsAutomaticMode); |
Garrick Evans | 77e9a13 | 2021-05-13 10:51:13 +0900 | [diff] [blame] | 169 | FRIEND_TEST(ProxyTest, FeatureEnablementCheckedOnSetup); |
| 170 | FRIEND_TEST(ProxyTest, LoginEventTriggersFeatureCheck); |
| 171 | FRIEND_TEST(ProxyTest, LogoutEventTriggersDisable); |
| 172 | FRIEND_TEST(ProxyTest, FeatureEnabled_LoginAfterLogout); |
| 173 | FRIEND_TEST(ProxyTest, FeatureDisabled_LoginAfterLogout); |
| 174 | FRIEND_TEST(ProxyTest, SystemProxy_ShillPropertyNotUpdatedIfFeatureDisabled); |
Garrick Evans | 5fe2a4f | 2021-02-03 17:04:48 +0900 | [diff] [blame] | 175 | |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 176 | const Options opts_; |
| 177 | std::unique_ptr<patchpanel::Client> patchpanel_; |
| 178 | std::unique_ptr<shill::Client> shill_; |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 179 | std::unique_ptr<shill::Client::ManagerPropertyAccessor> shill_props_; |
Garrick Evans | 77e9a13 | 2021-05-13 10:51:13 +0900 | [diff] [blame] | 180 | std::unique_ptr<ChromeFeaturesServiceClient> features_; |
| 181 | std::unique_ptr<SessionMonitor> session_; |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 182 | |
| 183 | base::ScopedFD ns_fd_; |
Garrick Evans | 9c7afb8 | 2021-01-29 22:38:03 +0900 | [diff] [blame] | 184 | patchpanel::ConnectNamespaceResponse ns_; |
Garrick Evans | 34650b3 | 2021-02-03 09:24:35 +0900 | [diff] [blame] | 185 | std::unique_ptr<Resolver> resolver_; |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 186 | DoHConfig doh_config_; |
Garrick Evans | 34650b3 | 2021-02-03 09:24:35 +0900 | [diff] [blame] | 187 | std::unique_ptr<shill::Client::Device> device_; |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 188 | |
Garrick Evans | 77e9a13 | 2021-05-13 10:51:13 +0900 | [diff] [blame] | 189 | bool feature_enabled_{false}; |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 190 | base::WeakPtrFactory<Proxy> weak_factory_{this}; |
| 191 | }; |
| 192 | |
| 193 | std::ostream& operator<<(std::ostream& stream, Proxy::Type type); |
| 194 | std::ostream& operator<<(std::ostream& stream, Proxy::Options opt); |
| 195 | |
| 196 | } // namespace dns_proxy |
| 197 | |
| 198 | #endif // DNS_PROXY_PROXY_H_ |