blob: 8a70cff8369b1b2372c9a19cf12a6ca735df6bc1 [file] [log] [blame]
Garrick Evans066dc2c2020-12-10 10:43:55 +09001// 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 Evans4f5428c2021-02-15 11:23:54 +09008#include <sysexits.h>
Garrick Evans066dc2c2020-12-10 10:43:55 +09009#include <unistd.h>
10
Garrick Evansd41fdbf2021-03-03 09:15:48 +090011#include <set>
Garrick Evans066dc2c2020-12-10 10:43:55 +090012#include <utility>
13
14#include <base/bind.h>
Garrick Evansd41fdbf2021-03-03 09:15:48 +090015#include <base/strings/string_split.h>
Garrick Evans066dc2c2020-12-10 10:43:55 +090016#include <base/threading/thread_task_runner_handle.h>
Garrick Evans9c7afb82021-01-29 22:38:03 +090017#include <base/time/time.h>
Garrick Evans48c84ef2021-01-28 11:29:42 +090018#include <chromeos/patchpanel/net_util.h>
19#include <shill/dbus-constants.h>
Garrick Evans066dc2c2020-12-10 10:43:55 +090020
21namespace dns_proxy {
22
Garrick Evans9c7afb82021-01-29 22:38:03 +090023constexpr base::TimeDelta kShillPropertyAttemptDelay =
24 base::TimeDelta::FromMilliseconds(200);
Jason Jeremy Iman1bb71c22021-01-26 21:49:55 +090025constexpr base::TimeDelta kRequestTimeout = base::TimeDelta::FromSeconds(10000);
Jason Jeremy Iman845f2932021-01-31 16:12:13 +090026constexpr base::TimeDelta kRequestRetryDelay =
27 base::TimeDelta::FromMilliseconds(200);
Garrick Evans9c7afb82021-01-29 22:38:03 +090028
Garrick Evans066dc2c2020-12-10 10:43:55 +090029constexpr char kSystemProxyType[] = "sys";
30constexpr char kDefaultProxyType[] = "def";
31constexpr char kARCProxyType[] = "arc";
Jason Jeremy Iman845f2932021-01-31 16:12:13 +090032constexpr int32_t kRequestMaxRetry = 1;
Garrick Evans34650b32021-02-03 09:24:35 +090033constexpr uint16_t kDefaultPort = 13568; // port 53 in network order.
Garrick Evans304a5f42021-02-15 20:34:55 +090034constexpr char kIfAddrAny[] = "0.0.0.0";
35
Garrick Evans066dc2c2020-12-10 10:43:55 +090036// static
37const char* Proxy::TypeToString(Type t) {
38 switch (t) {
39 case Type::kSystem:
40 return kSystemProxyType;
41 case Type::kDefault:
42 return kDefaultProxyType;
43 case Type::kARC:
44 return kARCProxyType;
45 }
46}
47
48// static
49std::optional<Proxy::Type> Proxy::StringToType(const std::string& s) {
50 if (s == kSystemProxyType)
51 return Type::kSystem;
52
53 if (s == kDefaultProxyType)
54 return Type::kDefault;
55
56 if (s == kARCProxyType)
57 return Type::kARC;
58
59 return std::nullopt;
60}
61
62std::ostream& operator<<(std::ostream& stream, Proxy::Type type) {
63 stream << Proxy::TypeToString(type);
64 return stream;
65}
66
67std::ostream& operator<<(std::ostream& stream, Proxy::Options opt) {
68 stream << "{" << Proxy::TypeToString(opt.type) << ":" << opt.ifname << "}";
69 return stream;
70}
71
72Proxy::Proxy(const Proxy::Options& opts) : opts_(opts) {}
73
Garrick Evans5fe2a4f2021-02-03 17:04:48 +090074Proxy::Proxy(const Options& opts,
75 std::unique_ptr<patchpanel::Client> patchpanel,
76 std::unique_ptr<shill::Client> shill)
77 : opts_(opts),
78 patchpanel_(std::move(patchpanel)),
79 shill_(std::move(shill)) {}
80
Garrick Evans9c8797d2021-02-17 21:28:10 +090081Proxy::~Proxy() {
82 if (bus_)
83 bus_->ShutdownAndBlock();
84}
85
Garrick Evans066dc2c2020-12-10 10:43:55 +090086int Proxy::OnInit() {
87 LOG(INFO) << "Starting DNS proxy " << opts_;
88
89 /// Run after Daemon::OnInit()
90 base::ThreadTaskRunnerHandle::Get()->PostTask(
91 FROM_HERE, base::Bind(&Proxy::Setup, weak_factory_.GetWeakPtr()));
92 return DBusDaemon::OnInit();
93}
94
95void Proxy::OnShutdown(int*) {
96 LOG(INFO) << "Stopping DNS proxy " << opts_;
Garrick Evans34650b32021-02-03 09:24:35 +090097 if (opts_.type == Type::kSystem)
98 SetShillProperty("");
Garrick Evans066dc2c2020-12-10 10:43:55 +090099}
100
101void Proxy::Setup() {
Garrick Evans5fe2a4f2021-02-03 17:04:48 +0900102 // This is only to account for the injected client for testing.
Garrick Evans5fe2a4f2021-02-03 17:04:48 +0900103 if (!patchpanel_)
104 patchpanel_ = patchpanel::Client::New();
105
Garrick Evans066dc2c2020-12-10 10:43:55 +0900106 CHECK(patchpanel_) << "Failed to initialize patchpanel client";
Garrick Evansfe99aaa2021-02-12 14:32:50 +0900107
108 // This is only to account for the injected client for testing.
109 if (!shill_)
110 shill_.reset(new shill::Client(bus_));
111
Garrick Evans066dc2c2020-12-10 10:43:55 +0900112 patchpanel_->RegisterOnAvailableCallback(base::BindRepeating(
113 &Proxy::OnPatchpanelReady, weak_factory_.GetWeakPtr()));
Garrick Evans4f5428c2021-02-15 11:23:54 +0900114 patchpanel_->RegisterProcessChangedCallback(base::BindRepeating(
115 &Proxy::OnPatchpanelReset, weak_factory_.GetWeakPtr()));
Garrick Evansfe99aaa2021-02-12 14:32:50 +0900116
117 shill_->RegisterOnAvailableCallback(
118 base::BindOnce(&Proxy::OnShillReady, weak_factory_.GetWeakPtr()));
Garrick Evans066dc2c2020-12-10 10:43:55 +0900119}
120
121void Proxy::OnPatchpanelReady(bool success) {
122 CHECK(success) << "Failed to connect to patchpanel";
123
124 // The default network proxy might actually be carrying Chrome, Crostini or
125 // if a VPN is on, even ARC traffic, but we attribute this as as "user"
126 // sourced.
127 patchpanel::TrafficCounter::Source traffic_source;
128 switch (opts_.type) {
129 case Type::kSystem:
130 traffic_source = patchpanel::TrafficCounter::SYSTEM;
131 break;
132 case Type::kARC:
133 traffic_source = patchpanel::TrafficCounter::ARC;
134 break;
135 default:
136 traffic_source = patchpanel::TrafficCounter::USER;
137 }
138
139 // Note that using getpid() here requires that this minijail is not creating a
140 // new PID namespace.
141 // The default proxy (only) needs to use the VPN, if applicable, the others
142 // expressly need to avoid it.
143 auto res = patchpanel_->ConnectNamespace(
144 getpid(), opts_.ifname, true /* forward_user_traffic */,
145 opts_.type == Type::kDefault /* route_on_vpn */, traffic_source);
146 CHECK(res.first.is_valid())
147 << "Failed to establish private network namespace";
148 ns_fd_ = std::move(res.first);
Garrick Evans9c7afb82021-01-29 22:38:03 +0900149 ns_ = res.second;
Garrick Evans066dc2c2020-12-10 10:43:55 +0900150 LOG(INFO) << "Sucessfully connected private network namespace:"
Garrick Evans9c7afb82021-01-29 22:38:03 +0900151 << ns_.host_ifname() << " <--> " << ns_.peer_ifname();
Garrick Evans48c84ef2021-01-28 11:29:42 +0900152
Garrick Evans34650b32021-02-03 09:24:35 +0900153 // Now it's safe to register these handlers and respond to them.
154 shill_->RegisterDefaultDeviceChangedHandler(base::BindRepeating(
155 &Proxy::OnDefaultDeviceChanged, weak_factory_.GetWeakPtr()));
156 shill_->RegisterDeviceChangedHandler(
157 base::BindRepeating(&Proxy::OnDeviceChanged, weak_factory_.GetWeakPtr()));
158
Garrick Evans48c84ef2021-01-28 11:29:42 +0900159 if (opts_.type == Type::kSystem)
Garrick Evans34650b32021-02-03 09:24:35 +0900160 shill_->RegisterProcessChangedHandler(
161 base::BindRepeating(&Proxy::OnShillReset, weak_factory_.GetWeakPtr()));
Garrick Evans9c7afb82021-01-29 22:38:03 +0900162}
163
Garrick Evans4f5428c2021-02-15 11:23:54 +0900164void Proxy::OnPatchpanelReset(bool reset) {
165 // If patchpanel crashes, the proxy is useless since the connected virtual
166 // network is gone. So the best bet is to exit and have the controller restart
167 // us. Note if this is the system proxy, it will inform shill on shutdown.
168 LOG(ERROR) << "Patchpanel has been shutdown - restarting DNS proxy " << opts_;
169 QuitWithExitCode(EX_UNAVAILABLE);
170
171 LOG(WARNING) << "Patchpanel has been reset";
172}
173
Garrick Evansfe99aaa2021-02-12 14:32:50 +0900174void Proxy::OnShillReady(bool success) {
175 CHECK(success) << "Failed to connect to shill";
176 shill_->Init();
177}
178
Garrick Evans9c7afb82021-01-29 22:38:03 +0900179void Proxy::OnShillReset(bool reset) {
180 if (!reset) {
181 LOG(WARNING) << "Shill has been shutdown";
Garrick Evansfe99aaa2021-02-12 14:32:50 +0900182 // Watch for it to return.
183 shill_->RegisterOnAvailableCallback(
184 base::BindOnce(&Proxy::OnShillReady, weak_factory_.GetWeakPtr()));
Garrick Evans9c7afb82021-01-29 22:38:03 +0900185 return;
186 }
187
Garrick Evans34650b32021-02-03 09:24:35 +0900188 // Really this means shill crashed. To be safe, explicitly reset the proxy
189 // address. We don't want to crash on failure here because shill might still
190 // have this address and try to use it. This probably redundant though with us
191 // rediscovering the default device.
192 // TODO(garrick): Remove this if so.
Garrick Evans9c7afb82021-01-29 22:38:03 +0900193 LOG(WARNING) << "Shill has been reset";
Garrick Evansaaf9d412021-02-15 11:25:21 +0900194 SetShillProperty(patchpanel::IPv4AddressToString(ns_.peer_ipv4_address()));
Garrick Evans066dc2c2020-12-10 10:43:55 +0900195}
196
Jason Jeremy Iman845f2932021-01-31 16:12:13 +0900197std::unique_ptr<Resolver> Proxy::NewResolver(base::TimeDelta timeout,
198 base::TimeDelta retry_delay,
199 int max_num_retries) {
200 return std::make_unique<Resolver>(timeout, retry_delay, max_num_retries);
Garrick Evans2ca050d2021-02-09 18:21:36 +0900201}
202
Garrick Evans34650b32021-02-03 09:24:35 +0900203void Proxy::OnDefaultDeviceChanged(const shill::Client::Device* const device) {
204 // ARC proxies will handle changes to their network in OnDeviceChanged.
205 if (opts_.type == Proxy::Type::kARC)
206 return;
Garrick Evans066dc2c2020-12-10 10:43:55 +0900207
Garrick Evans34650b32021-02-03 09:24:35 +0900208 // Default service is either not ready yet or has just disconnected.
209 if (!device) {
210 // If it disconnected, shutdown the resolver.
211 if (device_) {
212 LOG(WARNING) << opts_
213 << " is stopping because there is no default service";
Garrick Evansd41fdbf2021-03-03 09:15:48 +0900214 doh_config_.clear();
Garrick Evans34650b32021-02-03 09:24:35 +0900215 resolver_.reset();
216 device_.reset();
217 }
218 return;
219 }
220
Garrick Evansadde9852021-02-15 20:16:53 +0900221 shill::Client::Device new_default_device = *device;
222
Garrick Evans34650b32021-02-03 09:24:35 +0900223 // The system proxy should ignore when a VPN is turned on as it must continue
224 // to work with the underlying physical interface.
Garrick Evans34650b32021-02-03 09:24:35 +0900225 if (opts_.type == Proxy::Type::kSystem &&
Garrick Evansadde9852021-02-15 20:16:53 +0900226 device->type == shill::Client::Device::Type::kVPN) {
227 if (device_)
228 return;
229
230 // No device means that the system proxy has started up with a VPN as the
231 // default network; which means we need to dig out the physical network
232 // device and use that from here forward.
233 auto dd = shill_->DefaultDevice(true /* exclude_vpn */);
234 if (!dd) {
235 LOG(ERROR) << "No default non-VPN device found";
236 return;
237 }
238 new_default_device = *dd.get();
239 }
Garrick Evans34650b32021-02-03 09:24:35 +0900240
241 // While this is enforced in shill as well, only enable resolution if the
242 // service online.
Garrick Evansadde9852021-02-15 20:16:53 +0900243 if (new_default_device.state !=
244 shill::Client::Device::ConnectionState::kOnline) {
Garrick Evans34650b32021-02-03 09:24:35 +0900245 if (device_) {
246 LOG(WARNING) << opts_ << " is stopping because the default device ["
Garrick Evansadde9852021-02-15 20:16:53 +0900247 << new_default_device.ifname << "] is offline";
Garrick Evansd41fdbf2021-03-03 09:15:48 +0900248 doh_config_.clear();
Garrick Evans34650b32021-02-03 09:24:35 +0900249 resolver_.reset();
250 device_.reset();
251 }
252 return;
253 }
254
255 if (!device_)
256 device_ = std::make_unique<shill::Client::Device>();
257
258 // The default network has changed.
Garrick Evansadde9852021-02-15 20:16:53 +0900259 if (new_default_device.ifname != device_->ifname)
260 LOG(INFO) << opts_ << " is now tracking [" << new_default_device.ifname
261 << "]";
Garrick Evans34650b32021-02-03 09:24:35 +0900262
Garrick Evansadde9852021-02-15 20:16:53 +0900263 *device_.get() = new_default_device;
Garrick Evans34650b32021-02-03 09:24:35 +0900264
265 if (!resolver_) {
Jason Jeremy Iman845f2932021-01-31 16:12:13 +0900266 resolver_ =
267 NewResolver(kRequestTimeout, kRequestRetryDelay, kRequestMaxRetry);
Garrick Evansd41fdbf2021-03-03 09:15:48 +0900268 doh_config_.set_resolver(resolver_.get());
Garrick Evans34650b32021-02-03 09:24:35 +0900269
270 struct sockaddr_in addr = {0};
271 addr.sin_family = AF_INET;
272 addr.sin_port = kDefaultPort;
273 addr.sin_addr.s_addr =
274 INADDR_ANY; // Since we're running in the private namespace.
Garrick Evans2ca050d2021-02-09 18:21:36 +0900275
Jason Jeremy Iman6fd98552021-01-27 04:19:07 +0900276 CHECK(resolver_->ListenUDP(reinterpret_cast<struct sockaddr*>(&addr)))
277 << opts_ << " failed to start UDP relay loop";
Garrick Evansa6bfc322021-03-02 15:50:54 +0900278 LOG_IF(DFATAL,
279 !resolver_->ListenTCP(reinterpret_cast<struct sockaddr*>(&addr)))
Jason Jeremy Iman6fd98552021-01-27 04:19:07 +0900280 << opts_ << " failed to start TCP relay loop";
Garrick Evansd41fdbf2021-03-03 09:15:48 +0900281
282 // Fetch the DoH settings.
283 brillo::ErrorPtr error;
284 std::map<std::string, std::string> doh_providers;
285 if (shill_props()->Get(shill::kDNSProxyDOHProvidersProperty, &doh_providers,
286 &error))
287 OnDoHProvidersChanged(brillo::Any(doh_providers));
288 else
289 LOG(ERROR) << opts_ << " failed to obtain DoH configuration from shill: "
290 << error->GetMessage();
Garrick Evans34650b32021-02-03 09:24:35 +0900291 }
292
293 // Update the resolver with the latest DNS config.
Garrick Evansa8c12be2021-02-17 16:06:45 +0900294 UpdateNameServers(device_->ipconfig);
Garrick Evans34650b32021-02-03 09:24:35 +0900295
296 // For the system proxy, we have to tell shill about it. We should start
297 // receiving DNS traffic on success. But if this fails, we don't have much
298 // choice but to just crash out and try again.
299 if (opts_.type == Type::kSystem)
Garrick Evansaaf9d412021-02-15 11:25:21 +0900300 SetShillProperty(patchpanel::IPv4AddressToString(ns_.peer_ipv4_address()),
Garrick Evans34650b32021-02-03 09:24:35 +0900301 true /* die_on_failure */);
302}
303
Garrick Evansd41fdbf2021-03-03 09:15:48 +0900304shill::Client::ManagerPropertyAccessor* Proxy::shill_props() {
305 if (!shill_props_) {
306 shill_props_ = shill_->ManagerProperties();
307 shill_props_->Watch(shill::kDNSProxyDOHProvidersProperty,
308 base::BindRepeating(&Proxy::OnDoHProvidersChanged,
309 weak_factory_.GetWeakPtr()));
310 }
311
312 return shill_props_.get();
313}
314
Garrick Evans34650b32021-02-03 09:24:35 +0900315void Proxy::OnDeviceChanged(const shill::Client::Device* const device) {
Garrick Evansa8c12be2021-02-17 16:06:45 +0900316 // Ignore if there is no tracked device or it's different.
317 if (!device || !device_ || device_->ifname != device->ifname)
318 return;
319
320 // We don't need to worry about this here since the default proxy always/only
321 // tracks the default device and any update will be handled by
322 // OnDefaultDeviceChanged.
323 if (opts_.type == Type::kDefault)
324 return;
325
326 if (device_->ipconfig == device->ipconfig)
327 return;
328
329 UpdateNameServers(device->ipconfig);
330 device_->ipconfig = device->ipconfig;
331}
332
333void Proxy::UpdateNameServers(const shill::Client::IPConfig& ipconfig) {
334 auto name_servers = ipconfig.ipv4_dns_addresses;
335 // Shill sometimes adds 0.0.0.0 for some reason - so strip any if so.
336 name_servers.erase(
337 std::remove_if(name_servers.begin(), name_servers.end(),
338 [](const std::string& s) { return s == kIfAddrAny; }),
339 name_servers.end());
Garrick Evansd41fdbf2021-03-03 09:15:48 +0900340 name_servers.insert(name_servers.end(), ipconfig.ipv6_dns_addresses.begin(),
341 ipconfig.ipv6_dns_addresses.end());
342 doh_config_.set_nameservers(name_servers);
Garrick Evansa8c12be2021-02-17 16:06:45 +0900343 LOG(INFO) << opts_ << " applied device DNS configuration";
Garrick Evans34650b32021-02-03 09:24:35 +0900344}
Garrick Evans066dc2c2020-12-10 10:43:55 +0900345
Garrick Evansd41fdbf2021-03-03 09:15:48 +0900346void Proxy::OnDoHProvidersChanged(const brillo::Any& value) {
347 doh_config_.set_providers(value.Get<std::map<std::string, std::string>>());
348}
349
Garrick Evans9c7afb82021-01-29 22:38:03 +0900350void Proxy::SetShillProperty(const std::string& addr,
351 bool die_on_failure,
352 uint8_t num_retries) {
353 if (opts_.type != Type::kSystem) {
354 LOG(DFATAL) << "Must be called from system proxy only";
355 return;
356 }
Garrick Evans48c84ef2021-01-28 11:29:42 +0900357
Garrick Evans9c7afb82021-01-29 22:38:03 +0900358 if (num_retries == 0) {
359 LOG(ERROR) << "Maximum number of retries exceeding attempt to"
360 << " set dns-proxy address property on shill";
361 CHECK(!die_on_failure);
362 return;
363 }
364
365 // This can only happen if called from OnShutdown and Setup had somehow failed
366 // to create the client... it's unlikely but regardless, that shill client
367 // isn't coming back so there's no point in retrying anything.
Garrick Evans48c84ef2021-01-28 11:29:42 +0900368 if (!shill_) {
Garrick Evans9c7afb82021-01-29 22:38:03 +0900369 LOG(ERROR)
370 << "No connection to shill - cannot set dns-proxy address property ["
371 << addr << "].";
372 return;
Garrick Evans48c84ef2021-01-28 11:29:42 +0900373 }
374
375 brillo::ErrorPtr error;
Garrick Evansd41fdbf2021-03-03 09:15:48 +0900376 if (shill_props()->Set(shill::kDNSProxyIPv4AddressProperty, addr, &error))
Garrick Evans9c7afb82021-01-29 22:38:03 +0900377 return;
378
379 LOG(ERROR) << "Failed to set dns-proxy address property [" << addr
380 << "] on shill: " << error->GetMessage() << ". Retrying...";
381
382 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
383 FROM_HERE,
384 base::Bind(&Proxy::SetShillProperty, weak_factory_.GetWeakPtr(), addr,
385 die_on_failure, num_retries - 1),
386 kShillPropertyAttemptDelay);
Garrick Evans48c84ef2021-01-28 11:29:42 +0900387}
388
Garrick Evansd41fdbf2021-03-03 09:15:48 +0900389void Proxy::DoHConfig::set_resolver(Resolver* resolver) {
390 resolver_ = resolver;
391 update();
392}
393
394void Proxy::DoHConfig::set_nameservers(
395 const std::vector<std::string>& nameservers) {
396 nameservers_ = nameservers;
397 update();
398}
399
400void Proxy::DoHConfig::set_providers(
401 const std::map<std::string, std::string>& providers) {
402 secure_providers_.clear();
403 auto_providers_.clear();
404
405 if (providers.empty()) {
406 LOG(INFO) << "DoH: off";
407 update();
408 return;
409 }
410
411 for (const auto& [endpoint, nameservers] : providers) {
412 // We expect that in secure, always-on to find one (or more) endpoints with
413 // no nameservers.
414 if (nameservers.empty()) {
415 secure_providers_.insert(endpoint);
416 continue;
417 }
418
419 // Remap nameserver -> secure endpoint so we can quickly determine if DoH
420 // should be attempted when the name servers change.
421 for (const auto& ns :
422 base::SplitString(nameservers, ",", base::TRIM_WHITESPACE,
423 base::SPLIT_WANT_NONEMPTY)) {
424 auto_providers_[ns] = endpoint;
425 }
426 }
427
428 // If for some reason, both collections are non-empty, prefer the automatic
429 // upgrade configuration.
430 if (!auto_providers_.empty()) {
431 secure_providers_.clear();
432 LOG(INFO) << "DoH: automatic";
433 }
434 if (!secure_providers_.empty()) {
435 LOG(INFO) << "DoH: always-on";
436 }
437 update();
438}
439
440void Proxy::DoHConfig::update() {
441 if (!resolver_)
442 return;
443
444 resolver_->SetNameServers(nameservers_);
445
446 std::set<std::string> doh_providers;
447 bool doh_always_on = false;
448 if (!secure_providers_.empty()) {
449 doh_providers = secure_providers_;
450 doh_always_on = true;
451 } else if (!auto_providers_.empty()) {
452 for (const auto& ns : nameservers_) {
453 const auto it = auto_providers_.find(ns);
454 if (it != auto_providers_.end()) {
455 doh_providers.emplace(it->second);
456 }
457 }
458 }
459
460 resolver_->SetDoHProviders(
461 std::vector(doh_providers.begin(), doh_providers.end()), doh_always_on);
462}
463
464void Proxy::DoHConfig::clear() {
465 resolver_ = nullptr;
466 secure_providers_.clear();
467 auto_providers_.clear();
468}
469
Garrick Evans066dc2c2020-12-10 10:43:55 +0900470} // namespace dns_proxy