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> |
| 8 | #include <unistd.h> |
| 9 | |
| 10 | #include <utility> |
| 11 | |
| 12 | #include <base/bind.h> |
| 13 | #include <base/threading/thread_task_runner_handle.h> |
Garrick Evans | 9c7afb8 | 2021-01-29 22:38:03 +0900 | [diff] [blame] | 14 | #include <base/time/time.h> |
Garrick Evans | 48c84ef | 2021-01-28 11:29:42 +0900 | [diff] [blame] | 15 | #include <chromeos/patchpanel/net_util.h> |
| 16 | #include <shill/dbus-constants.h> |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 17 | |
| 18 | namespace dns_proxy { |
| 19 | |
Garrick Evans | 9c7afb8 | 2021-01-29 22:38:03 +0900 | [diff] [blame] | 20 | constexpr base::TimeDelta kShillPropertyAttemptDelay = |
| 21 | base::TimeDelta::FromMilliseconds(200); |
| 22 | |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 23 | constexpr char kSystemProxyType[] = "sys"; |
| 24 | constexpr char kDefaultProxyType[] = "def"; |
| 25 | constexpr char kARCProxyType[] = "arc"; |
Garrick Evans | 34650b3 | 2021-02-03 09:24:35 +0900 | [diff] [blame^] | 26 | constexpr uint16_t kDefaultPort = 13568; // port 53 in network order. |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 27 | |
| 28 | // static |
| 29 | const char* Proxy::TypeToString(Type t) { |
| 30 | switch (t) { |
| 31 | case Type::kSystem: |
| 32 | return kSystemProxyType; |
| 33 | case Type::kDefault: |
| 34 | return kDefaultProxyType; |
| 35 | case Type::kARC: |
| 36 | return kARCProxyType; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | // static |
| 41 | std::optional<Proxy::Type> Proxy::StringToType(const std::string& s) { |
| 42 | if (s == kSystemProxyType) |
| 43 | return Type::kSystem; |
| 44 | |
| 45 | if (s == kDefaultProxyType) |
| 46 | return Type::kDefault; |
| 47 | |
| 48 | if (s == kARCProxyType) |
| 49 | return Type::kARC; |
| 50 | |
| 51 | return std::nullopt; |
| 52 | } |
| 53 | |
| 54 | std::ostream& operator<<(std::ostream& stream, Proxy::Type type) { |
| 55 | stream << Proxy::TypeToString(type); |
| 56 | return stream; |
| 57 | } |
| 58 | |
| 59 | std::ostream& operator<<(std::ostream& stream, Proxy::Options opt) { |
| 60 | stream << "{" << Proxy::TypeToString(opt.type) << ":" << opt.ifname << "}"; |
| 61 | return stream; |
| 62 | } |
| 63 | |
| 64 | Proxy::Proxy(const Proxy::Options& opts) : opts_(opts) {} |
| 65 | |
| 66 | int Proxy::OnInit() { |
| 67 | LOG(INFO) << "Starting DNS proxy " << opts_; |
| 68 | |
| 69 | /// Run after Daemon::OnInit() |
| 70 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 71 | FROM_HERE, base::Bind(&Proxy::Setup, weak_factory_.GetWeakPtr())); |
| 72 | return DBusDaemon::OnInit(); |
| 73 | } |
| 74 | |
| 75 | void Proxy::OnShutdown(int*) { |
| 76 | LOG(INFO) << "Stopping DNS proxy " << opts_; |
Garrick Evans | 34650b3 | 2021-02-03 09:24:35 +0900 | [diff] [blame^] | 77 | if (opts_.type == Type::kSystem) |
| 78 | SetShillProperty(""); |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void Proxy::Setup() { |
| 82 | shill_.reset(new shill::Client(bus_)); |
| 83 | shill_->Init(); |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 84 | |
| 85 | patchpanel_ = patchpanel::Client::New(); |
| 86 | CHECK(patchpanel_) << "Failed to initialize patchpanel client"; |
| 87 | patchpanel_->RegisterOnAvailableCallback(base::BindRepeating( |
| 88 | &Proxy::OnPatchpanelReady, weak_factory_.GetWeakPtr())); |
| 89 | } |
| 90 | |
| 91 | void Proxy::OnPatchpanelReady(bool success) { |
| 92 | CHECK(success) << "Failed to connect to patchpanel"; |
| 93 | |
| 94 | // The default network proxy might actually be carrying Chrome, Crostini or |
| 95 | // if a VPN is on, even ARC traffic, but we attribute this as as "user" |
| 96 | // sourced. |
| 97 | patchpanel::TrafficCounter::Source traffic_source; |
| 98 | switch (opts_.type) { |
| 99 | case Type::kSystem: |
| 100 | traffic_source = patchpanel::TrafficCounter::SYSTEM; |
| 101 | break; |
| 102 | case Type::kARC: |
| 103 | traffic_source = patchpanel::TrafficCounter::ARC; |
| 104 | break; |
| 105 | default: |
| 106 | traffic_source = patchpanel::TrafficCounter::USER; |
| 107 | } |
| 108 | |
| 109 | // Note that using getpid() here requires that this minijail is not creating a |
| 110 | // new PID namespace. |
| 111 | // The default proxy (only) needs to use the VPN, if applicable, the others |
| 112 | // expressly need to avoid it. |
| 113 | auto res = patchpanel_->ConnectNamespace( |
| 114 | getpid(), opts_.ifname, true /* forward_user_traffic */, |
| 115 | opts_.type == Type::kDefault /* route_on_vpn */, traffic_source); |
| 116 | CHECK(res.first.is_valid()) |
| 117 | << "Failed to establish private network namespace"; |
| 118 | ns_fd_ = std::move(res.first); |
Garrick Evans | 9c7afb8 | 2021-01-29 22:38:03 +0900 | [diff] [blame] | 119 | ns_ = res.second; |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 120 | LOG(INFO) << "Sucessfully connected private network namespace:" |
Garrick Evans | 9c7afb8 | 2021-01-29 22:38:03 +0900 | [diff] [blame] | 121 | << ns_.host_ifname() << " <--> " << ns_.peer_ifname(); |
Garrick Evans | 48c84ef | 2021-01-28 11:29:42 +0900 | [diff] [blame] | 122 | |
Garrick Evans | 34650b3 | 2021-02-03 09:24:35 +0900 | [diff] [blame^] | 123 | // Now it's safe to register these handlers and respond to them. |
| 124 | shill_->RegisterDefaultDeviceChangedHandler(base::BindRepeating( |
| 125 | &Proxy::OnDefaultDeviceChanged, weak_factory_.GetWeakPtr())); |
| 126 | shill_->RegisterDeviceChangedHandler( |
| 127 | base::BindRepeating(&Proxy::OnDeviceChanged, weak_factory_.GetWeakPtr())); |
| 128 | |
Garrick Evans | 48c84ef | 2021-01-28 11:29:42 +0900 | [diff] [blame] | 129 | if (opts_.type == Type::kSystem) |
Garrick Evans | 34650b3 | 2021-02-03 09:24:35 +0900 | [diff] [blame^] | 130 | shill_->RegisterProcessChangedHandler( |
| 131 | base::BindRepeating(&Proxy::OnShillReset, weak_factory_.GetWeakPtr())); |
Garrick Evans | 9c7afb8 | 2021-01-29 22:38:03 +0900 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void Proxy::OnShillReset(bool reset) { |
| 135 | if (!reset) { |
| 136 | LOG(WARNING) << "Shill has been shutdown"; |
| 137 | return; |
| 138 | } |
| 139 | |
Garrick Evans | 34650b3 | 2021-02-03 09:24:35 +0900 | [diff] [blame^] | 140 | // Really this means shill crashed. To be safe, explicitly reset the proxy |
| 141 | // address. We don't want to crash on failure here because shill might still |
| 142 | // have this address and try to use it. This probably redundant though with us |
| 143 | // rediscovering the default device. |
| 144 | // TODO(garrick): Remove this if so. |
Garrick Evans | 9c7afb8 | 2021-01-29 22:38:03 +0900 | [diff] [blame] | 145 | LOG(WARNING) << "Shill has been reset"; |
Garrick Evans | 34650b3 | 2021-02-03 09:24:35 +0900 | [diff] [blame^] | 146 | SetShillProperty(patchpanel::IPv4AddressToString(ns_.host_ipv4_address())); |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 147 | } |
| 148 | |
Garrick Evans | 34650b3 | 2021-02-03 09:24:35 +0900 | [diff] [blame^] | 149 | void Proxy::OnDefaultDeviceChanged(const shill::Client::Device* const device) { |
| 150 | // ARC proxies will handle changes to their network in OnDeviceChanged. |
| 151 | if (opts_.type == Proxy::Type::kARC) |
| 152 | return; |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 153 | |
Garrick Evans | 34650b3 | 2021-02-03 09:24:35 +0900 | [diff] [blame^] | 154 | // Default service is either not ready yet or has just disconnected. |
| 155 | if (!device) { |
| 156 | // If it disconnected, shutdown the resolver. |
| 157 | if (device_) { |
| 158 | LOG(WARNING) << opts_ |
| 159 | << " is stopping because there is no default service"; |
| 160 | resolver_.reset(); |
| 161 | device_.reset(); |
| 162 | } |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | // The system proxy should ignore when a VPN is turned on as it must continue |
| 167 | // to work with the underlying physical interface. |
| 168 | // TODO(garrick): We need to handle the case when the system proxy is first |
| 169 | // started when a VPN is connected. In this case, we need to dig out the |
| 170 | // physical network device and use that from here forward. |
| 171 | if (opts_.type == Proxy::Type::kSystem && |
| 172 | device->type == shill::Client::Device::Type::kVPN) |
| 173 | return; |
| 174 | |
| 175 | // While this is enforced in shill as well, only enable resolution if the |
| 176 | // service online. |
| 177 | if (device->state != shill::Client::Device::ConnectionState::kOnline) { |
| 178 | if (device_) { |
| 179 | LOG(WARNING) << opts_ << " is stopping because the default device [" |
| 180 | << device->ifname << "] is offline"; |
| 181 | resolver_.reset(); |
| 182 | device_.reset(); |
| 183 | } |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | if (!device_) |
| 188 | device_ = std::make_unique<shill::Client::Device>(); |
| 189 | |
| 190 | // The default network has changed. |
| 191 | if (device->ifname != device_->ifname) |
| 192 | LOG(INFO) << opts_ << " is now tracking [" << device_->ifname << "]"; |
| 193 | |
| 194 | *device_.get() = *device; |
| 195 | |
| 196 | if (!resolver_) { |
| 197 | resolver_ = std::make_unique<Resolver>(); |
| 198 | |
| 199 | struct sockaddr_in addr = {0}; |
| 200 | addr.sin_family = AF_INET; |
| 201 | addr.sin_port = kDefaultPort; |
| 202 | addr.sin_addr.s_addr = |
| 203 | INADDR_ANY; // Since we're running in the private namespace. |
| 204 | CHECK(resolver_->Listen(reinterpret_cast<struct sockaddr*>(&addr))) |
| 205 | << opts_ << " failed to start relay loop"; |
| 206 | } |
| 207 | |
| 208 | // Update the resolver with the latest DNS config. |
| 209 | auto name_servers = device_->ipconfig.ipv4_dns_addresses; |
| 210 | name_servers.insert(name_servers.end(), |
| 211 | device_->ipconfig.ipv6_dns_addresses.begin(), |
| 212 | device_->ipconfig.ipv6_dns_addresses.end()); |
| 213 | resolver_->SetNameServers(name_servers); |
| 214 | LOG(INFO) << opts_ << " applied device DNS configuration"; |
| 215 | |
| 216 | // For the system proxy, we have to tell shill about it. We should start |
| 217 | // receiving DNS traffic on success. But if this fails, we don't have much |
| 218 | // choice but to just crash out and try again. |
| 219 | if (opts_.type == Type::kSystem) |
| 220 | SetShillProperty(patchpanel::IPv4AddressToString(ns_.host_ipv4_address()), |
| 221 | true /* die_on_failure */); |
| 222 | } |
| 223 | |
| 224 | void Proxy::OnDeviceChanged(const shill::Client::Device* const device) { |
| 225 | // TODO(garrick): ARC and default for VPN cases. |
| 226 | } |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 227 | |
Garrick Evans | 9c7afb8 | 2021-01-29 22:38:03 +0900 | [diff] [blame] | 228 | void Proxy::SetShillProperty(const std::string& addr, |
| 229 | bool die_on_failure, |
| 230 | uint8_t num_retries) { |
| 231 | if (opts_.type != Type::kSystem) { |
| 232 | LOG(DFATAL) << "Must be called from system proxy only"; |
| 233 | return; |
| 234 | } |
Garrick Evans | 48c84ef | 2021-01-28 11:29:42 +0900 | [diff] [blame] | 235 | |
Garrick Evans | 9c7afb8 | 2021-01-29 22:38:03 +0900 | [diff] [blame] | 236 | if (num_retries == 0) { |
| 237 | LOG(ERROR) << "Maximum number of retries exceeding attempt to" |
| 238 | << " set dns-proxy address property on shill"; |
| 239 | CHECK(!die_on_failure); |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | // This can only happen if called from OnShutdown and Setup had somehow failed |
| 244 | // to create the client... it's unlikely but regardless, that shill client |
| 245 | // isn't coming back so there's no point in retrying anything. |
Garrick Evans | 48c84ef | 2021-01-28 11:29:42 +0900 | [diff] [blame] | 246 | if (!shill_) { |
Garrick Evans | 9c7afb8 | 2021-01-29 22:38:03 +0900 | [diff] [blame] | 247 | LOG(ERROR) |
| 248 | << "No connection to shill - cannot set dns-proxy address property [" |
| 249 | << addr << "]."; |
| 250 | return; |
Garrick Evans | 48c84ef | 2021-01-28 11:29:42 +0900 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | brillo::ErrorPtr error; |
Garrick Evans | 9c7afb8 | 2021-01-29 22:38:03 +0900 | [diff] [blame] | 254 | if (shill_->ManagerProperties()->Set(shill::kDNSProxyIPv4AddressProperty, |
| 255 | addr, &error)) |
| 256 | return; |
| 257 | |
| 258 | LOG(ERROR) << "Failed to set dns-proxy address property [" << addr |
| 259 | << "] on shill: " << error->GetMessage() << ". Retrying..."; |
| 260 | |
| 261 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 262 | FROM_HERE, |
| 263 | base::Bind(&Proxy::SetShillProperty, weak_factory_.GetWeakPtr(), addr, |
| 264 | die_on_failure, num_retries - 1), |
| 265 | kShillPropertyAttemptDelay); |
Garrick Evans | 48c84ef | 2021-01-28 11:29:42 +0900 | [diff] [blame] | 266 | } |
| 267 | |
Garrick Evans | 066dc2c | 2020-12-10 10:43:55 +0900 | [diff] [blame] | 268 | } // namespace dns_proxy |