blob: c4bcd2dc9fc859403dc0d34f7d74cc79dca76fac [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>
Qijiang Fan713061e2021-03-08 15:45:12 +090015#include <base/check.h>
Garrick Evansd41fdbf2021-03-03 09:15:48 +090016#include <base/strings/string_split.h>
Garrick Evans066dc2c2020-12-10 10:43:55 +090017#include <base/threading/thread_task_runner_handle.h>
Garrick Evans9c7afb82021-01-29 22:38:03 +090018#include <base/time/time.h>
Garrick Evans48c84ef2021-01-28 11:29:42 +090019#include <chromeos/patchpanel/net_util.h>
20#include <shill/dbus-constants.h>
Garrick Evans066dc2c2020-12-10 10:43:55 +090021
22namespace dns_proxy {
23
Garrick Evans9c7afb82021-01-29 22:38:03 +090024constexpr base::TimeDelta kShillPropertyAttemptDelay =
25 base::TimeDelta::FromMilliseconds(200);
Jason Jeremy Iman1bb71c22021-01-26 21:49:55 +090026constexpr base::TimeDelta kRequestTimeout = base::TimeDelta::FromSeconds(10000);
Jason Jeremy Iman845f2932021-01-31 16:12:13 +090027constexpr base::TimeDelta kRequestRetryDelay =
28 base::TimeDelta::FromMilliseconds(200);
Garrick Evans9c7afb82021-01-29 22:38:03 +090029
Garrick Evans066dc2c2020-12-10 10:43:55 +090030constexpr char kSystemProxyType[] = "sys";
31constexpr char kDefaultProxyType[] = "def";
32constexpr char kARCProxyType[] = "arc";
Jason Jeremy Iman845f2932021-01-31 16:12:13 +090033constexpr int32_t kRequestMaxRetry = 1;
Garrick Evans34650b32021-02-03 09:24:35 +090034constexpr uint16_t kDefaultPort = 13568; // port 53 in network order.
Garrick Evans304a5f42021-02-15 20:34:55 +090035constexpr char kIfAddrAny[] = "0.0.0.0";
36
Garrick Evans066dc2c2020-12-10 10:43:55 +090037// static
38const 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
50std::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
63std::ostream& operator<<(std::ostream& stream, Proxy::Type type) {
64 stream << Proxy::TypeToString(type);
65 return stream;
66}
67
68std::ostream& operator<<(std::ostream& stream, Proxy::Options opt) {
69 stream << "{" << Proxy::TypeToString(opt.type) << ":" << opt.ifname << "}";
70 return stream;
71}
72
73Proxy::Proxy(const Proxy::Options& opts) : opts_(opts) {}
74
Garrick Evans5fe2a4f2021-02-03 17:04:48 +090075Proxy::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 Evans9c8797d2021-02-17 21:28:10 +090082Proxy::~Proxy() {
83 if (bus_)
84 bus_->ShutdownAndBlock();
85}
86
Garrick Evans066dc2c2020-12-10 10:43:55 +090087int 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
96void Proxy::OnShutdown(int*) {
97 LOG(INFO) << "Stopping DNS proxy " << opts_;
Garrick Evans34650b32021-02-03 09:24:35 +090098 if (opts_.type == Type::kSystem)
99 SetShillProperty("");
Garrick Evans066dc2c2020-12-10 10:43:55 +0900100}
101
102void Proxy::Setup() {
Garrick Evans5fe2a4f2021-02-03 17:04:48 +0900103 // This is only to account for the injected client for testing.
Garrick Evans5fe2a4f2021-02-03 17:04:48 +0900104 if (!patchpanel_)
105 patchpanel_ = patchpanel::Client::New();
106
Garrick Evans066dc2c2020-12-10 10:43:55 +0900107 CHECK(patchpanel_) << "Failed to initialize patchpanel client";
Garrick Evansfe99aaa2021-02-12 14:32:50 +0900108
109 // This is only to account for the injected client for testing.
110 if (!shill_)
111 shill_.reset(new shill::Client(bus_));
112
Garrick Evans066dc2c2020-12-10 10:43:55 +0900113 patchpanel_->RegisterOnAvailableCallback(base::BindRepeating(
114 &Proxy::OnPatchpanelReady, weak_factory_.GetWeakPtr()));
Garrick Evans4f5428c2021-02-15 11:23:54 +0900115 patchpanel_->RegisterProcessChangedCallback(base::BindRepeating(
116 &Proxy::OnPatchpanelReset, weak_factory_.GetWeakPtr()));
Garrick Evansfe99aaa2021-02-12 14:32:50 +0900117
118 shill_->RegisterOnAvailableCallback(
119 base::BindOnce(&Proxy::OnShillReady, weak_factory_.GetWeakPtr()));
Garrick Evans066dc2c2020-12-10 10:43:55 +0900120}
121
122void 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 Evans9c7afb82021-01-29 22:38:03 +0900150 ns_ = res.second;
Garrick Evans066dc2c2020-12-10 10:43:55 +0900151 LOG(INFO) << "Sucessfully connected private network namespace:"
Garrick Evans9c7afb82021-01-29 22:38:03 +0900152 << ns_.host_ifname() << " <--> " << ns_.peer_ifname();
Garrick Evans48c84ef2021-01-28 11:29:42 +0900153
Garrick Evans34650b32021-02-03 09:24:35 +0900154 // 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 Evans48c84ef2021-01-28 11:29:42 +0900160 if (opts_.type == Type::kSystem)
Garrick Evans34650b32021-02-03 09:24:35 +0900161 shill_->RegisterProcessChangedHandler(
162 base::BindRepeating(&Proxy::OnShillReset, weak_factory_.GetWeakPtr()));
Garrick Evans9c7afb82021-01-29 22:38:03 +0900163}
164
Garrick Evans4f5428c2021-02-15 11:23:54 +0900165void 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 Evansfe99aaa2021-02-12 14:32:50 +0900175void Proxy::OnShillReady(bool success) {
176 CHECK(success) << "Failed to connect to shill";
177 shill_->Init();
178}
179
Garrick Evans9c7afb82021-01-29 22:38:03 +0900180void Proxy::OnShillReset(bool reset) {
181 if (!reset) {
182 LOG(WARNING) << "Shill has been shutdown";
Garrick Evansfe99aaa2021-02-12 14:32:50 +0900183 // Watch for it to return.
184 shill_->RegisterOnAvailableCallback(
185 base::BindOnce(&Proxy::OnShillReady, weak_factory_.GetWeakPtr()));
Garrick Evans9c7afb82021-01-29 22:38:03 +0900186 return;
187 }
188
Garrick Evans34650b32021-02-03 09:24:35 +0900189 // 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 Evans9c7afb82021-01-29 22:38:03 +0900194 LOG(WARNING) << "Shill has been reset";
Garrick Evansaaf9d412021-02-15 11:25:21 +0900195 SetShillProperty(patchpanel::IPv4AddressToString(ns_.peer_ipv4_address()));
Garrick Evans066dc2c2020-12-10 10:43:55 +0900196}
197
Jason Jeremy Iman845f2932021-01-31 16:12:13 +0900198std::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 Evans2ca050d2021-02-09 18:21:36 +0900202}
203
Garrick Evans34650b32021-02-03 09:24:35 +0900204void 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 Evans066dc2c2020-12-10 10:43:55 +0900208
Garrick Evans34650b32021-02-03 09:24:35 +0900209 // 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 Evansd41fdbf2021-03-03 09:15:48 +0900215 doh_config_.clear();
Garrick Evans34650b32021-02-03 09:24:35 +0900216 resolver_.reset();
217 device_.reset();
218 }
219 return;
220 }
221
Garrick Evansadde9852021-02-15 20:16:53 +0900222 shill::Client::Device new_default_device = *device;
223
Garrick Evans34650b32021-02-03 09:24:35 +0900224 // The system proxy should ignore when a VPN is turned on as it must continue
225 // to work with the underlying physical interface.
Garrick Evans34650b32021-02-03 09:24:35 +0900226 if (opts_.type == Proxy::Type::kSystem &&
Garrick Evansadde9852021-02-15 20:16:53 +0900227 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 Evans34650b32021-02-03 09:24:35 +0900241
242 // While this is enforced in shill as well, only enable resolution if the
243 // service online.
Garrick Evansadde9852021-02-15 20:16:53 +0900244 if (new_default_device.state !=
245 shill::Client::Device::ConnectionState::kOnline) {
Garrick Evans34650b32021-02-03 09:24:35 +0900246 if (device_) {
247 LOG(WARNING) << opts_ << " is stopping because the default device ["
Garrick Evansadde9852021-02-15 20:16:53 +0900248 << new_default_device.ifname << "] is offline";
Garrick Evansd41fdbf2021-03-03 09:15:48 +0900249 doh_config_.clear();
Garrick Evans34650b32021-02-03 09:24:35 +0900250 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 Evansadde9852021-02-15 20:16:53 +0900260 if (new_default_device.ifname != device_->ifname)
261 LOG(INFO) << opts_ << " is now tracking [" << new_default_device.ifname
262 << "]";
Garrick Evans34650b32021-02-03 09:24:35 +0900263
Garrick Evansadde9852021-02-15 20:16:53 +0900264 *device_.get() = new_default_device;
Garrick Evans34650b32021-02-03 09:24:35 +0900265
266 if (!resolver_) {
Jason Jeremy Iman845f2932021-01-31 16:12:13 +0900267 resolver_ =
268 NewResolver(kRequestTimeout, kRequestRetryDelay, kRequestMaxRetry);
Garrick Evansd41fdbf2021-03-03 09:15:48 +0900269 doh_config_.set_resolver(resolver_.get());
Garrick Evans34650b32021-02-03 09:24:35 +0900270
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 Evans2ca050d2021-02-09 18:21:36 +0900276
Jason Jeremy Iman6fd98552021-01-27 04:19:07 +0900277 CHECK(resolver_->ListenUDP(reinterpret_cast<struct sockaddr*>(&addr)))
278 << opts_ << " failed to start UDP relay loop";
Garrick Evansa6bfc322021-03-02 15:50:54 +0900279 LOG_IF(DFATAL,
280 !resolver_->ListenTCP(reinterpret_cast<struct sockaddr*>(&addr)))
Jason Jeremy Iman6fd98552021-01-27 04:19:07 +0900281 << opts_ << " failed to start TCP relay loop";
Garrick Evansd41fdbf2021-03-03 09:15:48 +0900282
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 Evans34650b32021-02-03 09:24:35 +0900292 }
293
294 // Update the resolver with the latest DNS config.
Garrick Evansa8c12be2021-02-17 16:06:45 +0900295 UpdateNameServers(device_->ipconfig);
Garrick Evans34650b32021-02-03 09:24:35 +0900296
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 Evansaaf9d412021-02-15 11:25:21 +0900301 SetShillProperty(patchpanel::IPv4AddressToString(ns_.peer_ipv4_address()),
Garrick Evans34650b32021-02-03 09:24:35 +0900302 true /* die_on_failure */);
303}
304
Garrick Evansd41fdbf2021-03-03 09:15:48 +0900305shill::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 Evans34650b32021-02-03 09:24:35 +0900316void Proxy::OnDeviceChanged(const shill::Client::Device* const device) {
Garrick Evansa8c12be2021-02-17 16:06:45 +0900317 // 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
334void 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 Evansd41fdbf2021-03-03 09:15:48 +0900341 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 Evansa8c12be2021-02-17 16:06:45 +0900344 LOG(INFO) << opts_ << " applied device DNS configuration";
Garrick Evans34650b32021-02-03 09:24:35 +0900345}
Garrick Evans066dc2c2020-12-10 10:43:55 +0900346
Garrick Evansd41fdbf2021-03-03 09:15:48 +0900347void Proxy::OnDoHProvidersChanged(const brillo::Any& value) {
348 doh_config_.set_providers(value.Get<std::map<std::string, std::string>>());
349}
350
Garrick Evans9c7afb82021-01-29 22:38:03 +0900351void 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 Evans48c84ef2021-01-28 11:29:42 +0900358
Garrick Evans9c7afb82021-01-29 22:38:03 +0900359 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 Evans48c84ef2021-01-28 11:29:42 +0900369 if (!shill_) {
Garrick Evans9c7afb82021-01-29 22:38:03 +0900370 LOG(ERROR)
371 << "No connection to shill - cannot set dns-proxy address property ["
372 << addr << "].";
373 return;
Garrick Evans48c84ef2021-01-28 11:29:42 +0900374 }
375
376 brillo::ErrorPtr error;
Garrick Evansd41fdbf2021-03-03 09:15:48 +0900377 if (shill_props()->Set(shill::kDNSProxyIPv4AddressProperty, addr, &error))
Garrick Evans9c7afb82021-01-29 22:38:03 +0900378 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 Evans48c84ef2021-01-28 11:29:42 +0900388}
389
Garrick Evansd41fdbf2021-03-03 09:15:48 +0900390void Proxy::DoHConfig::set_resolver(Resolver* resolver) {
391 resolver_ = resolver;
392 update();
393}
394
395void Proxy::DoHConfig::set_nameservers(
396 const std::vector<std::string>& nameservers) {
397 nameservers_ = nameservers;
398 update();
399}
400
401void 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
441void 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
465void Proxy::DoHConfig::clear() {
466 resolver_ = nullptr;
467 secure_providers_.clear();
468 auto_providers_.clear();
469}
470
Garrick Evans066dc2c2020-12-10 10:43:55 +0900471} // namespace dns_proxy