blob: 472f1cd10dec26db7f7c64e3bac92db024b33236 [file] [log] [blame]
Mike Yub601ff72018-11-01 20:07:00 +08001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Ken Chen5471dca2019-04-15 15:25:35 +080017#define LOG_TAG "resolv"
Mike Yub601ff72018-11-01 20:07:00 +080018
Mike Yu303b0df2018-12-24 17:05:02 +080019#include "PrivateDnsConfiguration.h"
20
chenbruceaff85842019-05-31 15:46:42 +080021#include <android-base/logging.h>
Mike Yu04f1d482019-08-08 11:09:32 +080022#include <android-base/stringprintf.h>
Mike Yu04f1d482019-08-08 11:09:32 +080023#include <netdutils/ThreadUtil.h>
Mike Yub601ff72018-11-01 20:07:00 +080024#include <sys/socket.h>
25
Bernie Innocentiec4219b2019-01-30 11:16:36 +090026#include "DnsTlsTransport.h"
Mike Yu303b0df2018-12-24 17:05:02 +080027#include "ResolverEventReporter.h"
Bernie Innocentiec4219b2019-01-30 11:16:36 +090028#include "netd_resolv/resolv.h"
Mike Yub601ff72018-11-01 20:07:00 +080029#include "netdutils/BackoffSequence.h"
Mike Yu9c720102019-11-14 11:34:33 +080030#include "util.h"
Mike Yub601ff72018-11-01 20:07:00 +080031
Mike Yucb902c92020-05-20 19:26:56 +080032using android::base::StringPrintf;
33using android::netdutils::setThreadName;
Mike Yua772c202019-09-23 17:47:21 +080034using std::chrono::milliseconds;
35
Mike Yub601ff72018-11-01 20:07:00 +080036namespace android {
Mike Yub601ff72018-11-01 20:07:00 +080037namespace net {
38
Mike Yub601ff72018-11-01 20:07:00 +080039bool parseServer(const char* server, sockaddr_storage* parsed) {
Nick Desaulnierscd6395a2019-10-11 09:15:24 -070040 addrinfo hints = {
41 .ai_flags = AI_NUMERICHOST | AI_NUMERICSERV,
42 .ai_family = AF_UNSPEC,
43 };
Mike Yub601ff72018-11-01 20:07:00 +080044 addrinfo* res;
45
46 int err = getaddrinfo(server, "853", &hints, &res);
47 if (err != 0) {
chenbruceaff85842019-05-31 15:46:42 +080048 LOG(WARNING) << "Failed to parse server address (" << server << "): " << gai_strerror(err);
Mike Yub601ff72018-11-01 20:07:00 +080049 return false;
50 }
51
52 memcpy(parsed, res->ai_addr, res->ai_addrlen);
53 freeaddrinfo(res);
54 return true;
55}
56
Mike Yub601ff72018-11-01 20:07:00 +080057int PrivateDnsConfiguration::set(int32_t netId, uint32_t mark,
58 const std::vector<std::string>& servers, const std::string& name,
Mike Yu40e67072019-10-09 21:14:09 +080059 const std::string& caCert) {
Mike Yu04f1d482019-08-08 11:09:32 +080060 LOG(DEBUG) << "PrivateDnsConfiguration::set(" << netId << ", 0x" << std::hex << mark << std::dec
Mike Yu40e67072019-10-09 21:14:09 +080061 << ", " << servers.size() << ", " << name << ")";
Mike Yub601ff72018-11-01 20:07:00 +080062
63 // Parse the list of servers that has been passed in
Mike Yufa985f72020-11-23 20:24:21 +080064 PrivateDnsTracker tmp;
Mike Yu40e67072019-10-09 21:14:09 +080065 for (const auto& s : servers) {
Mike Yub601ff72018-11-01 20:07:00 +080066 sockaddr_storage parsed;
Mike Yu40e67072019-10-09 21:14:09 +080067 if (!parseServer(s.c_str(), &parsed)) {
Mike Yub601ff72018-11-01 20:07:00 +080068 return -EINVAL;
69 }
70 DnsTlsServer server(parsed);
71 server.name = name;
waynema0e73c2e2019-07-31 15:04:08 +080072 server.certificate = caCert;
Mike Yue60ab412020-12-01 17:56:12 +080073 server.mark = mark;
Mike Yufa985f72020-11-23 20:24:21 +080074 tmp[ServerIdentity(server)] = server;
Mike Yub601ff72018-11-01 20:07:00 +080075 }
76
77 std::lock_guard guard(mPrivateDnsLock);
waynema0e73c2e2019-07-31 15:04:08 +080078 if (!name.empty()) {
Mike Yub601ff72018-11-01 20:07:00 +080079 mPrivateDnsModes[netId] = PrivateDnsMode::STRICT;
Mike Yufa985f72020-11-23 20:24:21 +080080 } else if (!tmp.empty()) {
Mike Yub601ff72018-11-01 20:07:00 +080081 mPrivateDnsModes[netId] = PrivateDnsMode::OPPORTUNISTIC;
82 } else {
83 mPrivateDnsModes[netId] = PrivateDnsMode::OFF;
84 mPrivateDnsTransports.erase(netId);
Mike Yuf7717f52020-11-24 17:31:12 +080085 // TODO: signal validation threads to stop.
Mike Yub601ff72018-11-01 20:07:00 +080086 return 0;
87 }
88
89 // Create the tracker if it was not present
Mike Yuf7717f52020-11-24 17:31:12 +080090 auto& tracker = mPrivateDnsTransports[netId];
Mike Yub601ff72018-11-01 20:07:00 +080091
Mike Yuf7717f52020-11-24 17:31:12 +080092 // Add the servers if not contained in tracker.
Mike Yufa985f72020-11-23 20:24:21 +080093 for (const auto& [identity, server] : tmp) {
Mike Yuf7717f52020-11-24 17:31:12 +080094 if (tracker.find(identity) == tracker.end()) {
95 tracker[identity] = server;
96 }
97 }
Mike Yu3334a5e2020-11-19 13:33:17 +080098
Mike Yuf7717f52020-11-24 17:31:12 +080099 for (auto& [identity, server] : tracker) {
100 const bool active = tmp.find(identity) != tmp.end();
101 server.setActive(active);
102
103 // For simplicity, deem the validation result of inactive servers as unreliable.
104 if (!server.active() && server.validationState() == Validation::success) {
105 updateServerState(identity, Validation::success_but_expired, netId);
106 }
107
108 if (needsValidation(server)) {
Mike Yufa985f72020-11-23 20:24:21 +0800109 updateServerState(identity, Validation::in_process, netId);
Mike Yu74770542020-12-15 14:25:21 +0800110 startValidation(server, netId);
Mike Yub601ff72018-11-01 20:07:00 +0800111 }
112 }
Mike Yue655b1d2019-08-28 17:49:59 +0800113
Mike Yu8d9da4a2020-11-09 17:09:14 +0800114 return 0;
Mike Yub601ff72018-11-01 20:07:00 +0800115}
116
117PrivateDnsStatus PrivateDnsConfiguration::getStatus(unsigned netId) {
118 PrivateDnsStatus status{PrivateDnsMode::OFF, {}};
Mike Yub601ff72018-11-01 20:07:00 +0800119 std::lock_guard guard(mPrivateDnsLock);
120
121 const auto mode = mPrivateDnsModes.find(netId);
122 if (mode == mPrivateDnsModes.end()) return status;
123 status.mode = mode->second;
124
125 const auto netPair = mPrivateDnsTransports.find(netId);
126 if (netPair != mPrivateDnsTransports.end()) {
Mike Yufa985f72020-11-23 20:24:21 +0800127 for (const auto& [_, server] : netPair->second) {
Mike Yuf7717f52020-11-24 17:31:12 +0800128 if (server.active()) {
129 status.serversMap.emplace(server, server.validationState());
130 }
Mike Yub601ff72018-11-01 20:07:00 +0800131 }
132 }
133
134 return status;
135}
136
Mike Yub601ff72018-11-01 20:07:00 +0800137void PrivateDnsConfiguration::clear(unsigned netId) {
chenbruceaff85842019-05-31 15:46:42 +0800138 LOG(DEBUG) << "PrivateDnsConfiguration::clear(" << netId << ")";
Mike Yub601ff72018-11-01 20:07:00 +0800139 std::lock_guard guard(mPrivateDnsLock);
140 mPrivateDnsModes.erase(netId);
141 mPrivateDnsTransports.erase(netId);
142}
143
Mike Yue60ab412020-12-01 17:56:12 +0800144bool PrivateDnsConfiguration::requestValidation(unsigned netId, const DnsTlsServer& server,
145 uint32_t mark) {
146 std::lock_guard guard(mPrivateDnsLock);
147 auto netPair = mPrivateDnsTransports.find(netId);
148 if (netPair == mPrivateDnsTransports.end()) {
149 return false;
150 }
151
152 auto& tracker = netPair->second;
153 const ServerIdentity identity = ServerIdentity(server);
154 auto it = tracker.find(identity);
155 if (it == tracker.end()) {
156 return false;
157 }
158
159 const DnsTlsServer& target = it->second;
160
161 if (!target.active()) return false;
162
163 if (target.validationState() != Validation::success) return false;
164
165 // Don't run the validation if |mark| (from android_net_context.dns_mark) is different.
166 // This is to protect validation from running on unexpected marks.
167 // Validation should be associated with a mark gotten by system permission.
168 if (target.mark != mark) return false;
169
170 updateServerState(identity, Validation::in_process, netId);
Mike Yu74770542020-12-15 14:25:21 +0800171 startValidation(target, netId);
Mike Yue60ab412020-12-01 17:56:12 +0800172 return true;
173}
174
Mike Yu74770542020-12-15 14:25:21 +0800175void PrivateDnsConfiguration::startValidation(const DnsTlsServer& server, unsigned netId)
176 REQUIRES(mPrivateDnsLock) {
Mike Yub601ff72018-11-01 20:07:00 +0800177 // Note that capturing |server| and |netId| in this lambda create copies.
Mike Yu74770542020-12-15 14:25:21 +0800178 std::thread validate_thread([this, server, netId] {
Mike Yucb902c92020-05-20 19:26:56 +0800179 setThreadName(StringPrintf("TlsVerify_%u", netId).c_str());
Mike Yu04f1d482019-08-08 11:09:32 +0800180
Mike Yub601ff72018-11-01 20:07:00 +0800181 // cat /proc/sys/net/ipv4/tcp_syn_retries yields "6".
182 //
183 // Start with a 1 minute delay and backoff to once per hour.
184 //
185 // Assumptions:
186 // [1] Each TLS validation is ~10KB of certs+handshake+payload.
187 // [2] Network typically provision clients with <=4 nameservers.
188 // [3] Average month has 30 days.
189 //
190 // Each validation pass in a given hour is ~1.2MB of data. And 24
191 // such validation passes per day is about ~30MB per month, in the
192 // worst case. Otherwise, this will cost ~600 SYNs per month
193 // (6 SYNs per ip, 4 ips per validation pass, 24 passes per day).
Bernie Innocenti23c6e2a2019-05-16 15:18:35 +0900194 auto backoff = netdutils::BackoffSequence<>::Builder()
Mike Yub601ff72018-11-01 20:07:00 +0800195 .withInitialRetransmissionTime(std::chrono::seconds(60))
196 .withMaximumRetransmissionTime(std::chrono::seconds(3600))
197 .build();
198
199 while (true) {
200 // ::validate() is a blocking call that performs network operations.
201 // It can take milliseconds to minutes, up to the SYN retry limit.
Mike Yu74770542020-12-15 14:25:21 +0800202 LOG(WARNING) << "Validating DnsTlsServer " << server.toIpString() << " with mark 0x"
203 << std::hex << server.mark;
204 const bool success = DnsTlsTransport::validate(server, server.mark);
205 LOG(WARNING) << "validateDnsTlsServer returned " << success << " for "
206 << server.toIpString();
Mike Yub601ff72018-11-01 20:07:00 +0800207
208 const bool needs_reeval = this->recordPrivateDnsValidation(server, netId, success);
209 if (!needs_reeval) {
210 break;
211 }
212
213 if (backoff.hasNextTimeout()) {
Mike Yuf7717f52020-11-24 17:31:12 +0800214 // TODO: make the thread able to receive signals to shutdown early.
Mike Yub601ff72018-11-01 20:07:00 +0800215 std::this_thread::sleep_for(backoff.getNextTimeout());
216 } else {
217 break;
218 }
219 }
220 });
221 validate_thread.detach();
222}
223
224bool PrivateDnsConfiguration::recordPrivateDnsValidation(const DnsTlsServer& server, unsigned netId,
225 bool success) {
226 constexpr bool NEEDS_REEVALUATION = true;
227 constexpr bool DONT_REEVALUATE = false;
Mike Yufa985f72020-11-23 20:24:21 +0800228 const ServerIdentity identity = ServerIdentity(server);
Mike Yub601ff72018-11-01 20:07:00 +0800229
230 std::lock_guard guard(mPrivateDnsLock);
231
232 auto netPair = mPrivateDnsTransports.find(netId);
233 if (netPair == mPrivateDnsTransports.end()) {
chenbruceaff85842019-05-31 15:46:42 +0800234 LOG(WARNING) << "netId " << netId << " was erased during private DNS validation";
Mike Yu74770542020-12-15 14:25:21 +0800235 notifyValidationStateUpdate(identity.ip.toString(), Validation::fail, netId);
Mike Yub601ff72018-11-01 20:07:00 +0800236 return DONT_REEVALUATE;
237 }
238
239 const auto mode = mPrivateDnsModes.find(netId);
240 if (mode == mPrivateDnsModes.end()) {
chenbruceaff85842019-05-31 15:46:42 +0800241 LOG(WARNING) << "netId " << netId << " has no private DNS validation mode";
Mike Yu74770542020-12-15 14:25:21 +0800242 notifyValidationStateUpdate(identity.ip.toString(), Validation::fail, netId);
Mike Yub601ff72018-11-01 20:07:00 +0800243 return DONT_REEVALUATE;
244 }
245 const bool modeDoesReevaluation = (mode->second == PrivateDnsMode::STRICT);
246
247 bool reevaluationStatus =
248 (success || !modeDoesReevaluation) ? DONT_REEVALUATE : NEEDS_REEVALUATION;
249
250 auto& tracker = netPair->second;
Mike Yufa985f72020-11-23 20:24:21 +0800251 auto serverPair = tracker.find(identity);
Mike Yub601ff72018-11-01 20:07:00 +0800252 if (serverPair == tracker.end()) {
Mike Yu4499ee32020-11-24 20:05:18 +0800253 LOG(WARNING) << "Server " << server.toIpString()
chenbruceaff85842019-05-31 15:46:42 +0800254 << " was removed during private DNS validation";
Mike Yub601ff72018-11-01 20:07:00 +0800255 success = false;
256 reevaluationStatus = DONT_REEVALUATE;
Mike Yufa985f72020-11-23 20:24:21 +0800257 } else if (!(serverPair->second == server)) {
Mike Yub601ff72018-11-01 20:07:00 +0800258 // TODO: It doesn't seem correct to overwrite the tracker entry for
259 // |server| down below in this circumstance... Fix this.
Mike Yu4499ee32020-11-24 20:05:18 +0800260 LOG(WARNING) << "Server " << server.toIpString()
chenbruceaff85842019-05-31 15:46:42 +0800261 << " was changed during private DNS validation";
Mike Yub601ff72018-11-01 20:07:00 +0800262 success = false;
263 reevaluationStatus = DONT_REEVALUATE;
Mike Yuf7717f52020-11-24 17:31:12 +0800264 } else if (!serverPair->second.active()) {
Mike Yu4499ee32020-11-24 20:05:18 +0800265 LOG(WARNING) << "Server " << server.toIpString() << " was removed from the configuration";
Mike Yuf7717f52020-11-24 17:31:12 +0800266 success = false;
267 reevaluationStatus = DONT_REEVALUATE;
Mike Yub601ff72018-11-01 20:07:00 +0800268 }
269
Mike Yu303b0df2018-12-24 17:05:02 +0800270 // Send a validation event to NetdEventListenerService.
Hungming Chena32c8c12019-01-25 10:47:40 +0800271 const auto& listeners = ResolverEventReporter::getInstance().getListeners();
272 if (listeners.size() != 0) {
273 for (const auto& it : listeners) {
Mike Yu4499ee32020-11-24 20:05:18 +0800274 it->onPrivateDnsValidationEvent(netId, server.toIpString(), server.name, success);
Hungming Chena32c8c12019-01-25 10:47:40 +0800275 }
Mike Yu04f1d482019-08-08 11:09:32 +0800276 LOG(DEBUG) << "Sent validation " << (success ? "success" : "failure") << " event on netId "
Mike Yu4499ee32020-11-24 20:05:18 +0800277 << netId << " for " << server.toIpString() << " with hostname {" << server.name
278 << "}";
Mike Yu303b0df2018-12-24 17:05:02 +0800279 } else {
chenbruceaff85842019-05-31 15:46:42 +0800280 LOG(ERROR)
281 << "Validation event not sent since no INetdEventListener receiver is available.";
Mike Yub601ff72018-11-01 20:07:00 +0800282 }
283
284 if (success) {
Mike Yufa985f72020-11-23 20:24:21 +0800285 updateServerState(identity, Validation::success, netId);
Mike Yub601ff72018-11-01 20:07:00 +0800286 } else {
287 // Validation failure is expected if a user is on a captive portal.
288 // TODO: Trigger a second validation attempt after captive portal login
289 // succeeds.
Mike Yu3334a5e2020-11-19 13:33:17 +0800290 const auto result = (reevaluationStatus == NEEDS_REEVALUATION) ? Validation::in_process
291 : Validation::fail;
Mike Yufa985f72020-11-23 20:24:21 +0800292 updateServerState(identity, result, netId);
Mike Yub601ff72018-11-01 20:07:00 +0800293 }
Mike Yu04f1d482019-08-08 11:09:32 +0800294 LOG(WARNING) << "Validation " << (success ? "success" : "failed");
Mike Yub601ff72018-11-01 20:07:00 +0800295
296 return reevaluationStatus;
297}
298
Mike Yufa985f72020-11-23 20:24:21 +0800299void PrivateDnsConfiguration::updateServerState(const ServerIdentity& identity, Validation state,
Mike Yu3334a5e2020-11-19 13:33:17 +0800300 uint32_t netId) {
301 auto netPair = mPrivateDnsTransports.find(netId);
Mike Yufa985f72020-11-23 20:24:21 +0800302 if (netPair == mPrivateDnsTransports.end()) {
Mike Yu74770542020-12-15 14:25:21 +0800303 notifyValidationStateUpdate(identity.ip.toString(), Validation::fail, netId);
Mike Yufa985f72020-11-23 20:24:21 +0800304 return;
Mike Yu3334a5e2020-11-19 13:33:17 +0800305 }
306
Mike Yufa985f72020-11-23 20:24:21 +0800307 auto& tracker = netPair->second;
308 if (tracker.find(identity) == tracker.end()) {
Mike Yu74770542020-12-15 14:25:21 +0800309 notifyValidationStateUpdate(identity.ip.toString(), Validation::fail, netId);
Mike Yufa985f72020-11-23 20:24:21 +0800310 return;
311 }
312
313 tracker[identity].setValidationState(state);
Mike Yu74770542020-12-15 14:25:21 +0800314 notifyValidationStateUpdate(identity.ip.toString(), state, netId);
Mike Yu3334a5e2020-11-19 13:33:17 +0800315}
316
Mike Yuf7717f52020-11-24 17:31:12 +0800317bool PrivateDnsConfiguration::needsValidation(const DnsTlsServer& server) {
318 // The server is not expected to be used on the network.
319 if (!server.active()) return false;
Zhang Wei-e7976c1d68adb2019-08-23 23:13:22 -0500320
Mike Yuf7717f52020-11-24 17:31:12 +0800321 // The server is newly added.
322 if (server.validationState() == Validation::unknown_server) return true;
Zhang Wei-e7976c1d68adb2019-08-23 23:13:22 -0500323
Mike Yuf7717f52020-11-24 17:31:12 +0800324 // The server has failed at least one validation attempt. Give it another try.
325 if (server.validationState() == Validation::fail) return true;
326
327 // The previous validation result might be unreliable.
328 if (server.validationState() == Validation::success_but_expired) return true;
329
330 return false;
Mike Yub601ff72018-11-01 20:07:00 +0800331}
332
Mike Yu0ee1dc92020-11-09 14:56:54 +0800333void PrivateDnsConfiguration::setObserver(Observer* observer) {
334 std::lock_guard guard(mPrivateDnsLock);
335 mObserver = observer;
336}
337
Mike Yu74770542020-12-15 14:25:21 +0800338void PrivateDnsConfiguration::notifyValidationStateUpdate(const std::string& serverIp,
339 Validation validation,
340 uint32_t netId) const {
Mike Yu0ee1dc92020-11-09 14:56:54 +0800341 if (mObserver) {
Mike Yufa985f72020-11-23 20:24:21 +0800342 mObserver->onValidationStateUpdate(serverIp, validation, netId);
Mike Yu0ee1dc92020-11-09 14:56:54 +0800343 }
344}
345
Mike Yub601ff72018-11-01 20:07:00 +0800346} // namespace net
347} // namespace android