Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 1 | /* |
| 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 Chen | 5471dca | 2019-04-15 15:25:35 +0800 | [diff] [blame] | 17 | #define LOG_TAG "resolv" |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 18 | |
Mike Yu | 303b0df | 2018-12-24 17:05:02 +0800 | [diff] [blame] | 19 | #include "PrivateDnsConfiguration.h" |
| 20 | |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 21 | #include <android-base/logging.h> |
Mike Yu | 04f1d48 | 2019-08-08 11:09:32 +0800 | [diff] [blame] | 22 | #include <android-base/stringprintf.h> |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 23 | #include <netdb.h> |
Mike Yu | 04f1d48 | 2019-08-08 11:09:32 +0800 | [diff] [blame] | 24 | #include <netdutils/ThreadUtil.h> |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 25 | #include <sys/socket.h> |
| 26 | |
Bernie Innocenti | ec4219b | 2019-01-30 11:16:36 +0900 | [diff] [blame] | 27 | #include "DnsTlsTransport.h" |
Mike Yu | 303b0df | 2018-12-24 17:05:02 +0800 | [diff] [blame] | 28 | #include "ResolverEventReporter.h" |
Bernie Innocenti | ec4219b | 2019-01-30 11:16:36 +0900 | [diff] [blame] | 29 | #include "netd_resolv/resolv.h" |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 30 | #include "netdutils/BackoffSequence.h" |
| 31 | |
Mike Yu | a772c20 | 2019-09-23 17:47:21 +0800 | [diff] [blame^] | 32 | using std::chrono::milliseconds; |
| 33 | |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 34 | namespace android { |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 35 | namespace net { |
| 36 | |
| 37 | std::string addrToString(const sockaddr_storage* addr) { |
| 38 | char out[INET6_ADDRSTRLEN] = {0}; |
| 39 | getnameinfo((const sockaddr*) addr, sizeof(sockaddr_storage), out, INET6_ADDRSTRLEN, nullptr, 0, |
| 40 | NI_NUMERICHOST); |
| 41 | return std::string(out); |
| 42 | } |
| 43 | |
| 44 | bool parseServer(const char* server, sockaddr_storage* parsed) { |
| 45 | addrinfo hints = {.ai_family = AF_UNSPEC, .ai_flags = AI_NUMERICHOST | AI_NUMERICSERV}; |
| 46 | addrinfo* res; |
| 47 | |
| 48 | int err = getaddrinfo(server, "853", &hints, &res); |
| 49 | if (err != 0) { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 50 | LOG(WARNING) << "Failed to parse server address (" << server << "): " << gai_strerror(err); |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 51 | return false; |
| 52 | } |
| 53 | |
| 54 | memcpy(parsed, res->ai_addr, res->ai_addrlen); |
| 55 | freeaddrinfo(res); |
| 56 | return true; |
| 57 | } |
| 58 | |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 59 | int PrivateDnsConfiguration::set(int32_t netId, uint32_t mark, |
| 60 | const std::vector<std::string>& servers, const std::string& name, |
Mike Yu | a772c20 | 2019-09-23 17:47:21 +0800 | [diff] [blame^] | 61 | const std::string& caCert, int32_t connectTimeoutMs) { |
Mike Yu | 04f1d48 | 2019-08-08 11:09:32 +0800 | [diff] [blame] | 62 | LOG(DEBUG) << "PrivateDnsConfiguration::set(" << netId << ", 0x" << std::hex << mark << std::dec |
Mike Yu | a772c20 | 2019-09-23 17:47:21 +0800 | [diff] [blame^] | 63 | << ", " << servers.size() << ", " << name << ", " << connectTimeoutMs << "ms)"; |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 64 | |
| 65 | // Parse the list of servers that has been passed in |
| 66 | std::set<DnsTlsServer> tlsServers; |
| 67 | for (size_t i = 0; i < servers.size(); ++i) { |
| 68 | sockaddr_storage parsed; |
| 69 | if (!parseServer(servers[i].c_str(), &parsed)) { |
| 70 | return -EINVAL; |
| 71 | } |
| 72 | DnsTlsServer server(parsed); |
| 73 | server.name = name; |
waynema | 0e73c2e | 2019-07-31 15:04:08 +0800 | [diff] [blame] | 74 | server.certificate = caCert; |
Mike Yu | a772c20 | 2019-09-23 17:47:21 +0800 | [diff] [blame^] | 75 | |
| 76 | // connectTimeoutMs = 0: use the default timeout value. |
| 77 | // connectTimeoutMs < 0: invalid timeout value. |
| 78 | if (connectTimeoutMs > 0) { |
| 79 | // Set a specific timeout value but limit it to be at least 1 second. |
| 80 | server.connectTimeout = |
| 81 | (connectTimeoutMs < 1000) ? milliseconds(1000) : milliseconds(connectTimeoutMs); |
| 82 | } |
| 83 | |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 84 | tlsServers.insert(server); |
| 85 | } |
| 86 | |
| 87 | std::lock_guard guard(mPrivateDnsLock); |
waynema | 0e73c2e | 2019-07-31 15:04:08 +0800 | [diff] [blame] | 88 | if (!name.empty()) { |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 89 | mPrivateDnsModes[netId] = PrivateDnsMode::STRICT; |
| 90 | } else if (!tlsServers.empty()) { |
| 91 | mPrivateDnsModes[netId] = PrivateDnsMode::OPPORTUNISTIC; |
| 92 | } else { |
| 93 | mPrivateDnsModes[netId] = PrivateDnsMode::OFF; |
| 94 | mPrivateDnsTransports.erase(netId); |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | // Create the tracker if it was not present |
| 99 | auto netPair = mPrivateDnsTransports.find(netId); |
| 100 | if (netPair == mPrivateDnsTransports.end()) { |
| 101 | // No TLS tracker yet for this netId. |
| 102 | bool added; |
| 103 | std::tie(netPair, added) = mPrivateDnsTransports.emplace(netId, PrivateDnsTracker()); |
| 104 | if (!added) { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 105 | LOG(ERROR) << "Memory error while recording private DNS for netId " << netId; |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 106 | return -ENOMEM; |
| 107 | } |
| 108 | } |
| 109 | auto& tracker = netPair->second; |
| 110 | |
| 111 | // Remove any servers from the tracker that are not in |servers| exactly. |
| 112 | for (auto it = tracker.begin(); it != tracker.end();) { |
| 113 | if (tlsServers.count(it->first) == 0) { |
| 114 | it = tracker.erase(it); |
| 115 | } else { |
| 116 | ++it; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // Add any new or changed servers to the tracker, and initiate async checks for them. |
| 121 | for (const auto& server : tlsServers) { |
| 122 | if (needsValidation(tracker, server)) { |
| 123 | validatePrivateDnsProvider(server, tracker, netId, mark); |
| 124 | } |
| 125 | } |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | PrivateDnsStatus PrivateDnsConfiguration::getStatus(unsigned netId) { |
| 130 | PrivateDnsStatus status{PrivateDnsMode::OFF, {}}; |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 131 | std::lock_guard guard(mPrivateDnsLock); |
| 132 | |
| 133 | const auto mode = mPrivateDnsModes.find(netId); |
| 134 | if (mode == mPrivateDnsModes.end()) return status; |
| 135 | status.mode = mode->second; |
| 136 | |
| 137 | const auto netPair = mPrivateDnsTransports.find(netId); |
| 138 | if (netPair != mPrivateDnsTransports.end()) { |
| 139 | for (const auto& serverPair : netPair->second) { |
Mike Yu | 3e82906 | 2019-08-07 14:01:14 +0800 | [diff] [blame] | 140 | status.serversMap.emplace(serverPair.first, serverPair.second); |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
| 144 | return status; |
| 145 | } |
| 146 | |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 147 | void PrivateDnsConfiguration::clear(unsigned netId) { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 148 | LOG(DEBUG) << "PrivateDnsConfiguration::clear(" << netId << ")"; |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 149 | std::lock_guard guard(mPrivateDnsLock); |
| 150 | mPrivateDnsModes.erase(netId); |
| 151 | mPrivateDnsTransports.erase(netId); |
| 152 | } |
| 153 | |
| 154 | void PrivateDnsConfiguration::validatePrivateDnsProvider(const DnsTlsServer& server, |
| 155 | PrivateDnsTracker& tracker, unsigned netId, |
| 156 | uint32_t mark) REQUIRES(mPrivateDnsLock) { |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 157 | tracker[server] = Validation::in_process; |
Mike Yu | 04f1d48 | 2019-08-08 11:09:32 +0800 | [diff] [blame] | 158 | LOG(DEBUG) << "Server " << addrToString(&server.ss) << " marked as in_process on netId " |
| 159 | << netId << ". Tracker now has size " << tracker.size(); |
| 160 | |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 161 | // Note that capturing |server| and |netId| in this lambda create copies. |
| 162 | std::thread validate_thread([this, server, netId, mark] { |
Mike Yu | 04f1d48 | 2019-08-08 11:09:32 +0800 | [diff] [blame] | 163 | netdutils::setThreadName(android::base::StringPrintf("TlsVerify_%u", netId).c_str()); |
| 164 | |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 165 | // cat /proc/sys/net/ipv4/tcp_syn_retries yields "6". |
| 166 | // |
| 167 | // Start with a 1 minute delay and backoff to once per hour. |
| 168 | // |
| 169 | // Assumptions: |
| 170 | // [1] Each TLS validation is ~10KB of certs+handshake+payload. |
| 171 | // [2] Network typically provision clients with <=4 nameservers. |
| 172 | // [3] Average month has 30 days. |
| 173 | // |
| 174 | // Each validation pass in a given hour is ~1.2MB of data. And 24 |
| 175 | // such validation passes per day is about ~30MB per month, in the |
| 176 | // worst case. Otherwise, this will cost ~600 SYNs per month |
| 177 | // (6 SYNs per ip, 4 ips per validation pass, 24 passes per day). |
Bernie Innocenti | 23c6e2a | 2019-05-16 15:18:35 +0900 | [diff] [blame] | 178 | auto backoff = netdutils::BackoffSequence<>::Builder() |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 179 | .withInitialRetransmissionTime(std::chrono::seconds(60)) |
| 180 | .withMaximumRetransmissionTime(std::chrono::seconds(3600)) |
| 181 | .build(); |
| 182 | |
| 183 | while (true) { |
| 184 | // ::validate() is a blocking call that performs network operations. |
| 185 | // It can take milliseconds to minutes, up to the SYN retry limit. |
Mike Yu | 04f1d48 | 2019-08-08 11:09:32 +0800 | [diff] [blame] | 186 | LOG(WARNING) << "Validating DnsTlsServer on netId " << netId; |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 187 | const bool success = DnsTlsTransport::validate(server, netId, mark); |
Mike Yu | 04f1d48 | 2019-08-08 11:09:32 +0800 | [diff] [blame] | 188 | LOG(DEBUG) << "validateDnsTlsServer returned " << success << " for " |
| 189 | << addrToString(&server.ss); |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 190 | |
| 191 | const bool needs_reeval = this->recordPrivateDnsValidation(server, netId, success); |
| 192 | if (!needs_reeval) { |
| 193 | break; |
| 194 | } |
| 195 | |
| 196 | if (backoff.hasNextTimeout()) { |
| 197 | std::this_thread::sleep_for(backoff.getNextTimeout()); |
| 198 | } else { |
| 199 | break; |
| 200 | } |
| 201 | } |
| 202 | }); |
| 203 | validate_thread.detach(); |
| 204 | } |
| 205 | |
| 206 | bool PrivateDnsConfiguration::recordPrivateDnsValidation(const DnsTlsServer& server, unsigned netId, |
| 207 | bool success) { |
| 208 | constexpr bool NEEDS_REEVALUATION = true; |
| 209 | constexpr bool DONT_REEVALUATE = false; |
| 210 | |
| 211 | std::lock_guard guard(mPrivateDnsLock); |
| 212 | |
| 213 | auto netPair = mPrivateDnsTransports.find(netId); |
| 214 | if (netPair == mPrivateDnsTransports.end()) { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 215 | LOG(WARNING) << "netId " << netId << " was erased during private DNS validation"; |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 216 | return DONT_REEVALUATE; |
| 217 | } |
| 218 | |
| 219 | const auto mode = mPrivateDnsModes.find(netId); |
| 220 | if (mode == mPrivateDnsModes.end()) { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 221 | LOG(WARNING) << "netId " << netId << " has no private DNS validation mode"; |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 222 | return DONT_REEVALUATE; |
| 223 | } |
| 224 | const bool modeDoesReevaluation = (mode->second == PrivateDnsMode::STRICT); |
| 225 | |
| 226 | bool reevaluationStatus = |
| 227 | (success || !modeDoesReevaluation) ? DONT_REEVALUATE : NEEDS_REEVALUATION; |
| 228 | |
| 229 | auto& tracker = netPair->second; |
| 230 | auto serverPair = tracker.find(server); |
| 231 | if (serverPair == tracker.end()) { |
Mike Yu | 3e82906 | 2019-08-07 14:01:14 +0800 | [diff] [blame] | 232 | // TODO: Consider not adding this server to the tracker since this server is not expected |
| 233 | // to be one of the private DNS servers for this network now. This could prevent this |
| 234 | // server from being included when dumping status. |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 235 | LOG(WARNING) << "Server " << addrToString(&server.ss) |
| 236 | << " was removed during private DNS validation"; |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 237 | success = false; |
| 238 | reevaluationStatus = DONT_REEVALUATE; |
| 239 | } else if (!(serverPair->first == server)) { |
| 240 | // TODO: It doesn't seem correct to overwrite the tracker entry for |
| 241 | // |server| down below in this circumstance... Fix this. |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 242 | LOG(WARNING) << "Server " << addrToString(&server.ss) |
| 243 | << " was changed during private DNS validation"; |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 244 | success = false; |
| 245 | reevaluationStatus = DONT_REEVALUATE; |
| 246 | } |
| 247 | |
Mike Yu | 303b0df | 2018-12-24 17:05:02 +0800 | [diff] [blame] | 248 | // Send a validation event to NetdEventListenerService. |
Hungming Chen | a32c8c1 | 2019-01-25 10:47:40 +0800 | [diff] [blame] | 249 | const auto& listeners = ResolverEventReporter::getInstance().getListeners(); |
| 250 | if (listeners.size() != 0) { |
| 251 | for (const auto& it : listeners) { |
| 252 | it->onPrivateDnsValidationEvent(netId, addrToString(&server.ss), server.name, success); |
| 253 | } |
Mike Yu | 04f1d48 | 2019-08-08 11:09:32 +0800 | [diff] [blame] | 254 | LOG(DEBUG) << "Sent validation " << (success ? "success" : "failure") << " event on netId " |
| 255 | << netId << " for " << addrToString(&server.ss) << " with hostname {" |
| 256 | << server.name << "}"; |
Mike Yu | 303b0df | 2018-12-24 17:05:02 +0800 | [diff] [blame] | 257 | } else { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 258 | LOG(ERROR) |
| 259 | << "Validation event not sent since no INetdEventListener receiver is available."; |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | if (success) { |
| 263 | tracker[server] = Validation::success; |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 264 | } else { |
| 265 | // Validation failure is expected if a user is on a captive portal. |
| 266 | // TODO: Trigger a second validation attempt after captive portal login |
| 267 | // succeeds. |
| 268 | tracker[server] = (reevaluationStatus == NEEDS_REEVALUATION) ? Validation::in_process |
| 269 | : Validation::fail; |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 270 | } |
Mike Yu | 04f1d48 | 2019-08-08 11:09:32 +0800 | [diff] [blame] | 271 | LOG(WARNING) << "Validation " << (success ? "success" : "failed"); |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 272 | |
| 273 | return reevaluationStatus; |
| 274 | } |
| 275 | |
| 276 | // Start validation for newly added servers as well as any servers that have |
| 277 | // landed in Validation::fail state. Note that servers that have failed |
| 278 | // multiple validation attempts but for which there is still a validating |
| 279 | // thread running are marked as being Validation::in_process. |
| 280 | bool PrivateDnsConfiguration::needsValidation(const PrivateDnsTracker& tracker, |
| 281 | const DnsTlsServer& server) { |
| 282 | const auto& iter = tracker.find(server); |
| 283 | return (iter == tracker.end()) || (iter->second == Validation::fail); |
| 284 | } |
| 285 | |
| 286 | PrivateDnsConfiguration gPrivateDnsConfiguration; |
| 287 | |
| 288 | } // namespace net |
| 289 | } // namespace android |