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 | #include "dns-proxy/proxy.h" |
| 6 | |
| 7 | #include <sys/types.h> |
Garrick Evans | 4f5428c | 2021-02-15 11:23:54 +0900 | [diff] [blame] | 8 | #include <sysexits.h> |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 9 | #include <unistd.h> |
| 10 | |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 11 | #include <set> |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 12 | #include <utility> |
| 13 | |
| 14 | #include <base/bind.h> |
Qijiang Fan | 713061e | 2021-03-08 15:45:12 +0900 | [diff] [blame] | 15 | #include <base/check.h> |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 16 | #include <base/strings/string_split.h> |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 17 | #include <base/threading/thread_task_runner_handle.h> |
Garrick Evans | 9c7afb8 | 2021-01-29 22:38:03 +0900 | [diff] [blame] | 18 | #include <base/time/time.h> |
Garrick Evans | 48c84ef | 2021-01-28 11:29:42 +0900 | [diff] [blame] | 19 | #include <chromeos/patchpanel/net_util.h> |
| 20 | #include <shill/dbus-constants.h> |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 21 | |
| 22 | namespace dns_proxy { |
| 23 | |
Garrick Evans | 9c7afb8 | 2021-01-29 22:38:03 +0900 | [diff] [blame] | 24 | constexpr base::TimeDelta kShillPropertyAttemptDelay = |
| 25 | base::TimeDelta::FromMilliseconds(200); |
Jason Jeremy Iman | 1bb71c2 | 2021-01-26 21:49:55 +0900 | [diff] [blame] | 26 | constexpr base::TimeDelta kRequestTimeout = base::TimeDelta::FromSeconds(10000); |
Jason Jeremy Iman | 845f293 | 2021-01-31 16:12:13 +0900 | [diff] [blame] | 27 | constexpr base::TimeDelta kRequestRetryDelay = |
| 28 | base::TimeDelta::FromMilliseconds(200); |
Garrick Evans | 9c7afb8 | 2021-01-29 22:38:03 +0900 | [diff] [blame] | 29 | |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 30 | constexpr char kSystemProxyType[] = "sys"; |
| 31 | constexpr char kDefaultProxyType[] = "def"; |
| 32 | constexpr char kARCProxyType[] = "arc"; |
Jason Jeremy Iman | 845f293 | 2021-01-31 16:12:13 +0900 | [diff] [blame] | 33 | constexpr int32_t kRequestMaxRetry = 1; |
Garrick Evans | 34650b3 | 2021-02-03 09:24:35 +0900 | [diff] [blame] | 34 | constexpr uint16_t kDefaultPort = 13568; // port 53 in network order. |
Garrick Evans | 304a5f4 | 2021-02-15 20:34:55 +0900 | [diff] [blame] | 35 | constexpr char kIfAddrAny[] = "0.0.0.0"; |
| 36 | |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 37 | // static |
| 38 | const char* Proxy::TypeToString(Type t) { |
| 39 | switch (t) { |
| 40 | case Type::kSystem: |
| 41 | return kSystemProxyType; |
| 42 | case Type::kDefault: |
| 43 | return kDefaultProxyType; |
| 44 | case Type::kARC: |
| 45 | return kARCProxyType; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | // static |
| 50 | std::optional<Proxy::Type> Proxy::StringToType(const std::string& s) { |
| 51 | if (s == kSystemProxyType) |
| 52 | return Type::kSystem; |
| 53 | |
| 54 | if (s == kDefaultProxyType) |
| 55 | return Type::kDefault; |
| 56 | |
| 57 | if (s == kARCProxyType) |
| 58 | return Type::kARC; |
| 59 | |
| 60 | return std::nullopt; |
| 61 | } |
| 62 | |
| 63 | std::ostream& operator<<(std::ostream& stream, Proxy::Type type) { |
| 64 | stream << Proxy::TypeToString(type); |
| 65 | return stream; |
| 66 | } |
| 67 | |
| 68 | std::ostream& operator<<(std::ostream& stream, Proxy::Options opt) { |
| 69 | stream << "{" << Proxy::TypeToString(opt.type) << ":" << opt.ifname << "}"; |
| 70 | return stream; |
| 71 | } |
| 72 | |
| 73 | Proxy::Proxy(const Proxy::Options& opts) : opts_(opts) {} |
| 74 | |
Garrick Evans | 5fe2a4f | 2021-02-03 17:04:48 +0900 | [diff] [blame] | 75 | Proxy::Proxy(const Options& opts, |
| 76 | std::unique_ptr<patchpanel::Client> patchpanel, |
| 77 | std::unique_ptr<shill::Client> shill) |
| 78 | : opts_(opts), |
| 79 | patchpanel_(std::move(patchpanel)), |
| 80 | shill_(std::move(shill)) {} |
| 81 | |
Garrick Evans | 9c8797d | 2021-02-17 21:28:10 +0900 | [diff] [blame] | 82 | Proxy::~Proxy() { |
| 83 | if (bus_) |
| 84 | bus_->ShutdownAndBlock(); |
| 85 | } |
| 86 | |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 87 | int Proxy::OnInit() { |
| 88 | LOG(INFO) << "Starting DNS proxy " << opts_; |
| 89 | |
| 90 | /// Run after Daemon::OnInit() |
| 91 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 92 | FROM_HERE, base::Bind(&Proxy::Setup, weak_factory_.GetWeakPtr())); |
| 93 | return DBusDaemon::OnInit(); |
| 94 | } |
| 95 | |
| 96 | void Proxy::OnShutdown(int*) { |
| 97 | LOG(INFO) << "Stopping DNS proxy " << opts_; |
Garrick Evans | 34650b3 | 2021-02-03 09:24:35 +0900 | [diff] [blame] | 98 | if (opts_.type == Type::kSystem) |
| 99 | SetShillProperty(""); |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | void Proxy::Setup() { |
Garrick Evans | 5fe2a4f | 2021-02-03 17:04:48 +0900 | [diff] [blame] | 103 | // This is only to account for the injected client for testing. |
Garrick Evans | 5fe2a4f | 2021-02-03 17:04:48 +0900 | [diff] [blame] | 104 | if (!patchpanel_) |
| 105 | patchpanel_ = patchpanel::Client::New(); |
| 106 | |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 107 | CHECK(patchpanel_) << "Failed to initialize patchpanel client"; |
Garrick Evans | fe99aaa | 2021-02-12 14:32:50 +0900 | [diff] [blame] | 108 | |
| 109 | // This is only to account for the injected client for testing. |
| 110 | if (!shill_) |
| 111 | shill_.reset(new shill::Client(bus_)); |
| 112 | |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 113 | patchpanel_->RegisterOnAvailableCallback(base::BindRepeating( |
| 114 | &Proxy::OnPatchpanelReady, weak_factory_.GetWeakPtr())); |
Garrick Evans | 4f5428c | 2021-02-15 11:23:54 +0900 | [diff] [blame] | 115 | patchpanel_->RegisterProcessChangedCallback(base::BindRepeating( |
| 116 | &Proxy::OnPatchpanelReset, weak_factory_.GetWeakPtr())); |
Garrick Evans | fe99aaa | 2021-02-12 14:32:50 +0900 | [diff] [blame] | 117 | |
| 118 | shill_->RegisterOnAvailableCallback( |
| 119 | base::BindOnce(&Proxy::OnShillReady, weak_factory_.GetWeakPtr())); |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void Proxy::OnPatchpanelReady(bool success) { |
| 123 | CHECK(success) << "Failed to connect to patchpanel"; |
| 124 | |
| 125 | // The default network proxy might actually be carrying Chrome, Crostini or |
| 126 | // if a VPN is on, even ARC traffic, but we attribute this as as "user" |
| 127 | // sourced. |
| 128 | patchpanel::TrafficCounter::Source traffic_source; |
| 129 | switch (opts_.type) { |
| 130 | case Type::kSystem: |
| 131 | traffic_source = patchpanel::TrafficCounter::SYSTEM; |
| 132 | break; |
| 133 | case Type::kARC: |
| 134 | traffic_source = patchpanel::TrafficCounter::ARC; |
| 135 | break; |
| 136 | default: |
| 137 | traffic_source = patchpanel::TrafficCounter::USER; |
| 138 | } |
| 139 | |
| 140 | // Note that using getpid() here requires that this minijail is not creating a |
| 141 | // new PID namespace. |
| 142 | // The default proxy (only) needs to use the VPN, if applicable, the others |
| 143 | // expressly need to avoid it. |
| 144 | auto res = patchpanel_->ConnectNamespace( |
| 145 | getpid(), opts_.ifname, true /* forward_user_traffic */, |
| 146 | opts_.type == Type::kDefault /* route_on_vpn */, traffic_source); |
| 147 | CHECK(res.first.is_valid()) |
| 148 | << "Failed to establish private network namespace"; |
| 149 | ns_fd_ = std::move(res.first); |
Garrick Evans | 9c7afb8 | 2021-01-29 22:38:03 +0900 | [diff] [blame] | 150 | ns_ = res.second; |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 151 | LOG(INFO) << "Sucessfully connected private network namespace:" |
Garrick Evans | 9c7afb8 | 2021-01-29 22:38:03 +0900 | [diff] [blame] | 152 | << ns_.host_ifname() << " <--> " << ns_.peer_ifname(); |
Garrick Evans | 48c84ef | 2021-01-28 11:29:42 +0900 | [diff] [blame] | 153 | |
Garrick Evans | 34650b3 | 2021-02-03 09:24:35 +0900 | [diff] [blame] | 154 | // Now it's safe to register these handlers and respond to them. |
| 155 | shill_->RegisterDefaultDeviceChangedHandler(base::BindRepeating( |
| 156 | &Proxy::OnDefaultDeviceChanged, weak_factory_.GetWeakPtr())); |
| 157 | shill_->RegisterDeviceChangedHandler( |
| 158 | base::BindRepeating(&Proxy::OnDeviceChanged, weak_factory_.GetWeakPtr())); |
| 159 | |
Garrick Evans | 48c84ef | 2021-01-28 11:29:42 +0900 | [diff] [blame] | 160 | if (opts_.type == Type::kSystem) |
Garrick Evans | 34650b3 | 2021-02-03 09:24:35 +0900 | [diff] [blame] | 161 | shill_->RegisterProcessChangedHandler( |
| 162 | base::BindRepeating(&Proxy::OnShillReset, weak_factory_.GetWeakPtr())); |
Garrick Evans | 9c7afb8 | 2021-01-29 22:38:03 +0900 | [diff] [blame] | 163 | } |
| 164 | |
Garrick Evans | 4f5428c | 2021-02-15 11:23:54 +0900 | [diff] [blame] | 165 | void Proxy::OnPatchpanelReset(bool reset) { |
| 166 | // If patchpanel crashes, the proxy is useless since the connected virtual |
| 167 | // network is gone. So the best bet is to exit and have the controller restart |
| 168 | // us. Note if this is the system proxy, it will inform shill on shutdown. |
| 169 | LOG(ERROR) << "Patchpanel has been shutdown - restarting DNS proxy " << opts_; |
| 170 | QuitWithExitCode(EX_UNAVAILABLE); |
| 171 | |
| 172 | LOG(WARNING) << "Patchpanel has been reset"; |
| 173 | } |
| 174 | |
Garrick Evans | fe99aaa | 2021-02-12 14:32:50 +0900 | [diff] [blame] | 175 | void Proxy::OnShillReady(bool success) { |
| 176 | CHECK(success) << "Failed to connect to shill"; |
| 177 | shill_->Init(); |
| 178 | } |
| 179 | |
Garrick Evans | 9c7afb8 | 2021-01-29 22:38:03 +0900 | [diff] [blame] | 180 | void Proxy::OnShillReset(bool reset) { |
| 181 | if (!reset) { |
| 182 | LOG(WARNING) << "Shill has been shutdown"; |
Garrick Evans | fe99aaa | 2021-02-12 14:32:50 +0900 | [diff] [blame] | 183 | // Watch for it to return. |
| 184 | shill_->RegisterOnAvailableCallback( |
| 185 | base::BindOnce(&Proxy::OnShillReady, weak_factory_.GetWeakPtr())); |
Garrick Evans | 9c7afb8 | 2021-01-29 22:38:03 +0900 | [diff] [blame] | 186 | return; |
| 187 | } |
| 188 | |
Garrick Evans | 34650b3 | 2021-02-03 09:24:35 +0900 | [diff] [blame] | 189 | // Really this means shill crashed. To be safe, explicitly reset the proxy |
| 190 | // address. We don't want to crash on failure here because shill might still |
| 191 | // have this address and try to use it. This probably redundant though with us |
| 192 | // rediscovering the default device. |
| 193 | // TODO(garrick): Remove this if so. |
Garrick Evans | 9c7afb8 | 2021-01-29 22:38:03 +0900 | [diff] [blame] | 194 | LOG(WARNING) << "Shill has been reset"; |
Garrick Evans | aaf9d41 | 2021-02-15 11:25:21 +0900 | [diff] [blame] | 195 | SetShillProperty(patchpanel::IPv4AddressToString(ns_.peer_ipv4_address())); |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 196 | } |
| 197 | |
Jason Jeremy Iman | 845f293 | 2021-01-31 16:12:13 +0900 | [diff] [blame] | 198 | std::unique_ptr<Resolver> Proxy::NewResolver(base::TimeDelta timeout, |
| 199 | base::TimeDelta retry_delay, |
| 200 | int max_num_retries) { |
| 201 | return std::make_unique<Resolver>(timeout, retry_delay, max_num_retries); |
Garrick Evans | 2ca050d | 2021-02-09 18:21:36 +0900 | [diff] [blame] | 202 | } |
| 203 | |
Garrick Evans | 34650b3 | 2021-02-03 09:24:35 +0900 | [diff] [blame] | 204 | void Proxy::OnDefaultDeviceChanged(const shill::Client::Device* const device) { |
| 205 | // ARC proxies will handle changes to their network in OnDeviceChanged. |
| 206 | if (opts_.type == Proxy::Type::kARC) |
| 207 | return; |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 208 | |
Garrick Evans | 34650b3 | 2021-02-03 09:24:35 +0900 | [diff] [blame] | 209 | // Default service is either not ready yet or has just disconnected. |
| 210 | if (!device) { |
| 211 | // If it disconnected, shutdown the resolver. |
| 212 | if (device_) { |
| 213 | LOG(WARNING) << opts_ |
| 214 | << " is stopping because there is no default service"; |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 215 | doh_config_.clear(); |
Garrick Evans | 34650b3 | 2021-02-03 09:24:35 +0900 | [diff] [blame] | 216 | resolver_.reset(); |
| 217 | device_.reset(); |
| 218 | } |
| 219 | return; |
| 220 | } |
| 221 | |
Garrick Evans | adde985 | 2021-02-15 20:16:53 +0900 | [diff] [blame] | 222 | shill::Client::Device new_default_device = *device; |
| 223 | |
Garrick Evans | 34650b3 | 2021-02-03 09:24:35 +0900 | [diff] [blame] | 224 | // The system proxy should ignore when a VPN is turned on as it must continue |
| 225 | // to work with the underlying physical interface. |
Garrick Evans | 34650b3 | 2021-02-03 09:24:35 +0900 | [diff] [blame] | 226 | if (opts_.type == Proxy::Type::kSystem && |
Garrick Evans | adde985 | 2021-02-15 20:16:53 +0900 | [diff] [blame] | 227 | device->type == shill::Client::Device::Type::kVPN) { |
| 228 | if (device_) |
| 229 | return; |
| 230 | |
| 231 | // No device means that the system proxy has started up with a VPN as the |
| 232 | // default network; which means we need to dig out the physical network |
| 233 | // device and use that from here forward. |
| 234 | auto dd = shill_->DefaultDevice(true /* exclude_vpn */); |
| 235 | if (!dd) { |
| 236 | LOG(ERROR) << "No default non-VPN device found"; |
| 237 | return; |
| 238 | } |
| 239 | new_default_device = *dd.get(); |
| 240 | } |
Garrick Evans | 34650b3 | 2021-02-03 09:24:35 +0900 | [diff] [blame] | 241 | |
| 242 | // While this is enforced in shill as well, only enable resolution if the |
| 243 | // service online. |
Garrick Evans | adde985 | 2021-02-15 20:16:53 +0900 | [diff] [blame] | 244 | if (new_default_device.state != |
| 245 | shill::Client::Device::ConnectionState::kOnline) { |
Garrick Evans | 34650b3 | 2021-02-03 09:24:35 +0900 | [diff] [blame] | 246 | if (device_) { |
| 247 | LOG(WARNING) << opts_ << " is stopping because the default device [" |
Garrick Evans | adde985 | 2021-02-15 20:16:53 +0900 | [diff] [blame] | 248 | << new_default_device.ifname << "] is offline"; |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 249 | doh_config_.clear(); |
Garrick Evans | 34650b3 | 2021-02-03 09:24:35 +0900 | [diff] [blame] | 250 | resolver_.reset(); |
| 251 | device_.reset(); |
| 252 | } |
| 253 | return; |
| 254 | } |
| 255 | |
| 256 | if (!device_) |
| 257 | device_ = std::make_unique<shill::Client::Device>(); |
| 258 | |
| 259 | // The default network has changed. |
Garrick Evans | adde985 | 2021-02-15 20:16:53 +0900 | [diff] [blame] | 260 | if (new_default_device.ifname != device_->ifname) |
| 261 | LOG(INFO) << opts_ << " is now tracking [" << new_default_device.ifname |
| 262 | << "]"; |
Garrick Evans | 34650b3 | 2021-02-03 09:24:35 +0900 | [diff] [blame] | 263 | |
Garrick Evans | adde985 | 2021-02-15 20:16:53 +0900 | [diff] [blame] | 264 | *device_.get() = new_default_device; |
Garrick Evans | 34650b3 | 2021-02-03 09:24:35 +0900 | [diff] [blame] | 265 | |
| 266 | if (!resolver_) { |
Jason Jeremy Iman | 845f293 | 2021-01-31 16:12:13 +0900 | [diff] [blame] | 267 | resolver_ = |
| 268 | NewResolver(kRequestTimeout, kRequestRetryDelay, kRequestMaxRetry); |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 269 | doh_config_.set_resolver(resolver_.get()); |
Garrick Evans | 34650b3 | 2021-02-03 09:24:35 +0900 | [diff] [blame] | 270 | |
| 271 | struct sockaddr_in addr = {0}; |
| 272 | addr.sin_family = AF_INET; |
| 273 | addr.sin_port = kDefaultPort; |
| 274 | addr.sin_addr.s_addr = |
| 275 | INADDR_ANY; // Since we're running in the private namespace. |
Garrick Evans | 2ca050d | 2021-02-09 18:21:36 +0900 | [diff] [blame] | 276 | |
Jason Jeremy Iman | 6fd9855 | 2021-01-27 04:19:07 +0900 | [diff] [blame] | 277 | CHECK(resolver_->ListenUDP(reinterpret_cast<struct sockaddr*>(&addr))) |
| 278 | << opts_ << " failed to start UDP relay loop"; |
Garrick Evans | a6bfc32 | 2021-03-02 15:50:54 +0900 | [diff] [blame] | 279 | LOG_IF(DFATAL, |
| 280 | !resolver_->ListenTCP(reinterpret_cast<struct sockaddr*>(&addr))) |
Jason Jeremy Iman | 6fd9855 | 2021-01-27 04:19:07 +0900 | [diff] [blame] | 281 | << opts_ << " failed to start TCP relay loop"; |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 282 | |
| 283 | // Fetch the DoH settings. |
| 284 | brillo::ErrorPtr error; |
| 285 | std::map<std::string, std::string> doh_providers; |
| 286 | if (shill_props()->Get(shill::kDNSProxyDOHProvidersProperty, &doh_providers, |
| 287 | &error)) |
| 288 | OnDoHProvidersChanged(brillo::Any(doh_providers)); |
| 289 | else |
| 290 | LOG(ERROR) << opts_ << " failed to obtain DoH configuration from shill: " |
| 291 | << error->GetMessage(); |
Garrick Evans | 34650b3 | 2021-02-03 09:24:35 +0900 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | // Update the resolver with the latest DNS config. |
Garrick Evans | a8c12be | 2021-02-17 16:06:45 +0900 | [diff] [blame] | 295 | UpdateNameServers(device_->ipconfig); |
Garrick Evans | 34650b3 | 2021-02-03 09:24:35 +0900 | [diff] [blame] | 296 | |
| 297 | // For the system proxy, we have to tell shill about it. We should start |
| 298 | // receiving DNS traffic on success. But if this fails, we don't have much |
| 299 | // choice but to just crash out and try again. |
| 300 | if (opts_.type == Type::kSystem) |
Garrick Evans | aaf9d41 | 2021-02-15 11:25:21 +0900 | [diff] [blame] | 301 | SetShillProperty(patchpanel::IPv4AddressToString(ns_.peer_ipv4_address()), |
Garrick Evans | 34650b3 | 2021-02-03 09:24:35 +0900 | [diff] [blame] | 302 | true /* die_on_failure */); |
| 303 | } |
| 304 | |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 305 | shill::Client::ManagerPropertyAccessor* Proxy::shill_props() { |
| 306 | if (!shill_props_) { |
| 307 | shill_props_ = shill_->ManagerProperties(); |
| 308 | shill_props_->Watch(shill::kDNSProxyDOHProvidersProperty, |
| 309 | base::BindRepeating(&Proxy::OnDoHProvidersChanged, |
| 310 | weak_factory_.GetWeakPtr())); |
| 311 | } |
| 312 | |
| 313 | return shill_props_.get(); |
| 314 | } |
| 315 | |
Garrick Evans | 34650b3 | 2021-02-03 09:24:35 +0900 | [diff] [blame] | 316 | void Proxy::OnDeviceChanged(const shill::Client::Device* const device) { |
Garrick Evans | a8c12be | 2021-02-17 16:06:45 +0900 | [diff] [blame] | 317 | // Ignore if there is no tracked device or it's different. |
| 318 | if (!device || !device_ || device_->ifname != device->ifname) |
| 319 | return; |
| 320 | |
| 321 | // We don't need to worry about this here since the default proxy always/only |
| 322 | // tracks the default device and any update will be handled by |
| 323 | // OnDefaultDeviceChanged. |
| 324 | if (opts_.type == Type::kDefault) |
| 325 | return; |
| 326 | |
| 327 | if (device_->ipconfig == device->ipconfig) |
| 328 | return; |
| 329 | |
| 330 | UpdateNameServers(device->ipconfig); |
| 331 | device_->ipconfig = device->ipconfig; |
| 332 | } |
| 333 | |
| 334 | void Proxy::UpdateNameServers(const shill::Client::IPConfig& ipconfig) { |
| 335 | auto name_servers = ipconfig.ipv4_dns_addresses; |
| 336 | // Shill sometimes adds 0.0.0.0 for some reason - so strip any if so. |
| 337 | name_servers.erase( |
| 338 | std::remove_if(name_servers.begin(), name_servers.end(), |
| 339 | [](const std::string& s) { return s == kIfAddrAny; }), |
| 340 | name_servers.end()); |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 341 | name_servers.insert(name_servers.end(), ipconfig.ipv6_dns_addresses.begin(), |
| 342 | ipconfig.ipv6_dns_addresses.end()); |
| 343 | doh_config_.set_nameservers(name_servers); |
Garrick Evans | a8c12be | 2021-02-17 16:06:45 +0900 | [diff] [blame] | 344 | LOG(INFO) << opts_ << " applied device DNS configuration"; |
Garrick Evans | 34650b3 | 2021-02-03 09:24:35 +0900 | [diff] [blame] | 345 | } |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 346 | |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 347 | void Proxy::OnDoHProvidersChanged(const brillo::Any& value) { |
| 348 | doh_config_.set_providers(value.Get<std::map<std::string, std::string>>()); |
| 349 | } |
| 350 | |
Garrick Evans | 9c7afb8 | 2021-01-29 22:38:03 +0900 | [diff] [blame] | 351 | void Proxy::SetShillProperty(const std::string& addr, |
| 352 | bool die_on_failure, |
| 353 | uint8_t num_retries) { |
| 354 | if (opts_.type != Type::kSystem) { |
| 355 | LOG(DFATAL) << "Must be called from system proxy only"; |
| 356 | return; |
| 357 | } |
Garrick Evans | 48c84ef | 2021-01-28 11:29:42 +0900 | [diff] [blame] | 358 | |
Garrick Evans | 9c7afb8 | 2021-01-29 22:38:03 +0900 | [diff] [blame] | 359 | if (num_retries == 0) { |
| 360 | LOG(ERROR) << "Maximum number of retries exceeding attempt to" |
| 361 | << " set dns-proxy address property on shill"; |
| 362 | CHECK(!die_on_failure); |
| 363 | return; |
| 364 | } |
| 365 | |
| 366 | // This can only happen if called from OnShutdown and Setup had somehow failed |
| 367 | // to create the client... it's unlikely but regardless, that shill client |
| 368 | // isn't coming back so there's no point in retrying anything. |
Garrick Evans | 48c84ef | 2021-01-28 11:29:42 +0900 | [diff] [blame] | 369 | if (!shill_) { |
Garrick Evans | 9c7afb8 | 2021-01-29 22:38:03 +0900 | [diff] [blame] | 370 | LOG(ERROR) |
| 371 | << "No connection to shill - cannot set dns-proxy address property [" |
| 372 | << addr << "]."; |
| 373 | return; |
Garrick Evans | 48c84ef | 2021-01-28 11:29:42 +0900 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | brillo::ErrorPtr error; |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 377 | if (shill_props()->Set(shill::kDNSProxyIPv4AddressProperty, addr, &error)) |
Garrick Evans | 9c7afb8 | 2021-01-29 22:38:03 +0900 | [diff] [blame] | 378 | return; |
| 379 | |
| 380 | LOG(ERROR) << "Failed to set dns-proxy address property [" << addr |
| 381 | << "] on shill: " << error->GetMessage() << ". Retrying..."; |
| 382 | |
| 383 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 384 | FROM_HERE, |
| 385 | base::Bind(&Proxy::SetShillProperty, weak_factory_.GetWeakPtr(), addr, |
| 386 | die_on_failure, num_retries - 1), |
| 387 | kShillPropertyAttemptDelay); |
Garrick Evans | 48c84ef | 2021-01-28 11:29:42 +0900 | [diff] [blame] | 388 | } |
| 389 | |
Garrick Evans | d41fdbf | 2021-03-03 09:15:48 +0900 | [diff] [blame] | 390 | void Proxy::DoHConfig::set_resolver(Resolver* resolver) { |
| 391 | resolver_ = resolver; |
| 392 | update(); |
| 393 | } |
| 394 | |
| 395 | void Proxy::DoHConfig::set_nameservers( |
| 396 | const std::vector<std::string>& nameservers) { |
| 397 | nameservers_ = nameservers; |
| 398 | update(); |
| 399 | } |
| 400 | |
| 401 | void Proxy::DoHConfig::set_providers( |
| 402 | const std::map<std::string, std::string>& providers) { |
| 403 | secure_providers_.clear(); |
| 404 | auto_providers_.clear(); |
| 405 | |
| 406 | if (providers.empty()) { |
| 407 | LOG(INFO) << "DoH: off"; |
| 408 | update(); |
| 409 | return; |
| 410 | } |
| 411 | |
| 412 | for (const auto& [endpoint, nameservers] : providers) { |
| 413 | // We expect that in secure, always-on to find one (or more) endpoints with |
| 414 | // no nameservers. |
| 415 | if (nameservers.empty()) { |
| 416 | secure_providers_.insert(endpoint); |
| 417 | continue; |
| 418 | } |
| 419 | |
| 420 | // Remap nameserver -> secure endpoint so we can quickly determine if DoH |
| 421 | // should be attempted when the name servers change. |
| 422 | for (const auto& ns : |
| 423 | base::SplitString(nameservers, ",", base::TRIM_WHITESPACE, |
| 424 | base::SPLIT_WANT_NONEMPTY)) { |
| 425 | auto_providers_[ns] = endpoint; |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | // If for some reason, both collections are non-empty, prefer the automatic |
| 430 | // upgrade configuration. |
| 431 | if (!auto_providers_.empty()) { |
| 432 | secure_providers_.clear(); |
| 433 | LOG(INFO) << "DoH: automatic"; |
| 434 | } |
| 435 | if (!secure_providers_.empty()) { |
| 436 | LOG(INFO) << "DoH: always-on"; |
| 437 | } |
| 438 | update(); |
| 439 | } |
| 440 | |
| 441 | void Proxy::DoHConfig::update() { |
| 442 | if (!resolver_) |
| 443 | return; |
| 444 | |
| 445 | resolver_->SetNameServers(nameservers_); |
| 446 | |
| 447 | std::set<std::string> doh_providers; |
| 448 | bool doh_always_on = false; |
| 449 | if (!secure_providers_.empty()) { |
| 450 | doh_providers = secure_providers_; |
| 451 | doh_always_on = true; |
| 452 | } else if (!auto_providers_.empty()) { |
| 453 | for (const auto& ns : nameservers_) { |
| 454 | const auto it = auto_providers_.find(ns); |
| 455 | if (it != auto_providers_.end()) { |
| 456 | doh_providers.emplace(it->second); |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | resolver_->SetDoHProviders( |
| 462 | std::vector(doh_providers.begin(), doh_providers.end()), doh_always_on); |
| 463 | } |
| 464 | |
| 465 | void Proxy::DoHConfig::clear() { |
| 466 | resolver_ = nullptr; |
| 467 | secure_providers_.clear(); |
| 468 | auto_providers_.clear(); |
| 469 | } |
| 470 | |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 471 | } // namespace dns_proxy |