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