Luke Huang | 7b26b20 | 2019-03-28 14:09:24 +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" |
Luke Huang | 7b26b20 | 2019-03-28 14:09:24 +0800 | [diff] [blame] | 18 | |
| 19 | #include "Dns64Configuration.h" |
| 20 | |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 21 | #include <android-base/logging.h> |
Luke Huang | 7b26b20 | 2019-03-28 14:09:24 +0800 | [diff] [blame] | 22 | #include <netdb.h> |
Luke Huang | 9d2d25b | 2019-06-14 00:34:05 +0800 | [diff] [blame] | 23 | #include <netdutils/BackoffSequence.h> |
| 24 | #include <netdutils/DumpWriter.h> |
| 25 | #include <netdutils/InternetAddresses.h> |
Mike Yu | 04f1d48 | 2019-08-08 11:09:32 +0800 | [diff] [blame] | 26 | #include <netdutils/ThreadUtil.h> |
Luke Huang | 7b26b20 | 2019-03-28 14:09:24 +0800 | [diff] [blame] | 27 | #include <thread> |
| 28 | #include <utility> |
| 29 | |
| 30 | #include <arpa/inet.h> |
| 31 | |
| 32 | #include "DnsResolver.h" |
Bernie Innocenti | e71a28a | 2019-05-29 00:42:35 +0900 | [diff] [blame] | 33 | #include "getaddrinfo.h" |
Luke Huang | 7b26b20 | 2019-03-28 14:09:24 +0800 | [diff] [blame] | 34 | #include "netd_resolv/resolv.h" |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 35 | #include "stats.pb.h" |
Luke Huang | 7b26b20 | 2019-03-28 14:09:24 +0800 | [diff] [blame] | 36 | |
| 37 | namespace android { |
| 38 | |
| 39 | using netdutils::DumpWriter; |
| 40 | using netdutils::IPAddress; |
| 41 | using netdutils::IPPrefix; |
Luke Huang | 9d2d25b | 2019-06-14 00:34:05 +0800 | [diff] [blame] | 42 | using netdutils::ScopedAddrinfo; |
Mike Yu | cb902c9 | 2020-05-20 19:26:56 +0800 | [diff] [blame] | 43 | using netdutils::setThreadName; |
Luke Huang | 7b26b20 | 2019-03-28 14:09:24 +0800 | [diff] [blame] | 44 | |
| 45 | namespace net { |
| 46 | |
| 47 | const char Dns64Configuration::kIPv4OnlyHost[] = "ipv4only.arpa."; |
| 48 | const char Dns64Configuration::kIPv4Literal1[] = "192.0.0.170"; |
| 49 | const char Dns64Configuration::kIPv4Literal2[] = "192.0.0.171"; |
| 50 | |
| 51 | void Dns64Configuration::startPrefixDiscovery(unsigned netId) { |
| 52 | std::lock_guard guard(mMutex); |
| 53 | |
| 54 | // TODO: Keep previous prefix for a while |
| 55 | // Currently, we remove current prefix, if any, before starting a prefix discovery. |
| 56 | // This causes that Netd and framework temporarily forgets DNS64 prefix even the prefix may be |
| 57 | // discovered in a short time. |
| 58 | removeDns64Config(netId); |
| 59 | |
| 60 | Dns64Config cfg(getNextId(), netId); |
| 61 | // Emplace a copy of |cfg| in the map. |
| 62 | mDns64Configs.emplace(std::make_pair(netId, cfg)); |
| 63 | |
| 64 | // Note that capturing |cfg| in this lambda creates a copy. |
Mike Yu | 04f1d48 | 2019-08-08 11:09:32 +0800 | [diff] [blame] | 65 | std::thread discovery_thread([this, cfg, netId] { |
chenbruce | 9b72daa | 2021-08-20 00:00:49 +0800 | [diff] [blame] | 66 | setThreadName(fmt::format("Nat64Pfx_{}", netId)); |
Mike Yu | 04f1d48 | 2019-08-08 11:09:32 +0800 | [diff] [blame] | 67 | |
Luke Huang | 7b26b20 | 2019-03-28 14:09:24 +0800 | [diff] [blame] | 68 | // Make a mutable copy rather than mark the whole lambda mutable. |
| 69 | // No particular reason. |
| 70 | Dns64Config evalCfg(cfg); |
| 71 | |
| 72 | auto backoff = netdutils::BackoffSequence<>::Builder() |
| 73 | .withInitialRetransmissionTime(std::chrono::seconds(1)) |
| 74 | .withMaximumRetransmissionTime(std::chrono::seconds(3600)) |
| 75 | .build(); |
| 76 | |
| 77 | while (true) { |
| 78 | if (!this->shouldContinueDiscovery(evalCfg)) break; |
| 79 | |
| 80 | android_net_context netcontext{}; |
| 81 | mGetNetworkContextCallback(evalCfg.netId, 0, &netcontext); |
| 82 | |
| 83 | // Prefix discovery must bypass private DNS because in strict mode |
| 84 | // the server generally won't know the NAT64 prefix. |
| 85 | netcontext.flags |= NET_CONTEXT_FLAG_USE_LOCAL_NAMESERVERS; |
| 86 | if (doRfc7050PrefixDiscovery(netcontext, &evalCfg)) { |
| 87 | this->recordDns64Config(evalCfg); |
| 88 | break; |
| 89 | } |
| 90 | |
| 91 | if (!this->shouldContinueDiscovery(evalCfg)) break; |
| 92 | |
| 93 | if (!backoff.hasNextTimeout()) break; |
| 94 | { |
| 95 | std::unique_lock<std::mutex> cvGuard(mMutex); |
| 96 | // TODO: Consider some chrono math, combined with wait_until() |
| 97 | // perhaps, to prevent early re-resolves from the removal of |
| 98 | // other netids with IPv6-only nameservers. |
| 99 | mCv.wait_for(cvGuard, backoff.getNextTimeout()); |
| 100 | } |
| 101 | } |
| 102 | }); |
| 103 | discovery_thread.detach(); |
| 104 | } |
| 105 | |
| 106 | void Dns64Configuration::stopPrefixDiscovery(unsigned netId) { |
| 107 | std::lock_guard guard(mMutex); |
| 108 | removeDns64Config(netId); |
| 109 | mCv.notify_all(); |
| 110 | } |
| 111 | |
| 112 | IPPrefix Dns64Configuration::getPrefix64Locked(unsigned netId) const REQUIRES(mMutex) { |
| 113 | const auto& iter = mDns64Configs.find(netId); |
| 114 | if (iter != mDns64Configs.end()) return iter->second.prefix64; |
| 115 | |
| 116 | return IPPrefix{}; |
| 117 | } |
| 118 | |
| 119 | IPPrefix Dns64Configuration::getPrefix64(unsigned netId) const { |
| 120 | std::lock_guard guard(mMutex); |
| 121 | return getPrefix64Locked(netId); |
| 122 | } |
| 123 | |
| 124 | void Dns64Configuration::dump(DumpWriter& dw, unsigned netId) { |
| 125 | static const char kLabel[] = "DNS64 config"; |
| 126 | |
| 127 | std::lock_guard guard(mMutex); |
| 128 | |
| 129 | const auto& iter = mDns64Configs.find(netId); |
| 130 | if (iter == mDns64Configs.end()) { |
| 131 | dw.println("%s: none", kLabel); |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | const Dns64Config& cfg = iter->second; |
| 136 | if (cfg.prefix64.length() == 0) { |
| 137 | dw.println("%s: no prefix yet discovered", kLabel); |
| 138 | } else { |
Lorenzo Colitti | 511b6d4 | 2020-05-21 22:57:31 +0900 | [diff] [blame] | 139 | dw.println("%s: %s prefix %s", kLabel, cfg.isFromPrefixDiscovery() ? "discovered" : "set", |
| 140 | cfg.prefix64.toString().c_str()); |
Luke Huang | 7b26b20 | 2019-03-28 14:09:24 +0800 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
| 144 | // NOTE: The full RFC 7050 DNS64 discovery process is not implemented here. |
| 145 | // Instead, and more simplistic version of the same thing is done, and it |
| 146 | // currently assumes the DNS64 prefix is a /96. |
| 147 | bool Dns64Configuration::doRfc7050PrefixDiscovery(const android_net_context& netcontext, |
| 148 | Dns64Config* cfg) { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 149 | LOG(WARNING) << "(" << cfg->netId << ", " << cfg->discoveryId |
| 150 | << ") Detecting NAT64 prefix from DNS..."; |
Luke Huang | 7b26b20 | 2019-03-28 14:09:24 +0800 | [diff] [blame] | 151 | |
| 152 | const struct addrinfo hints = { |
| 153 | .ai_family = AF_INET6, |
| 154 | }; |
| 155 | |
| 156 | // TODO: Refactor so that netd can get all the regular getaddrinfo handling |
| 157 | // that regular apps get. We bypass the UNIX socket connection back to |
| 158 | // ourselves, which means we also bypass all the special netcontext flag |
| 159 | // handling and the resolver event logging. |
| 160 | struct addrinfo* res = nullptr; |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 161 | NetworkDnsEventReported event; |
| 162 | const int status = |
| 163 | resolv_getaddrinfo(kIPv4OnlyHost, nullptr, &hints, &netcontext, &res, &event); |
Luke Huang | 7b26b20 | 2019-03-28 14:09:24 +0800 | [diff] [blame] | 164 | ScopedAddrinfo result(res); |
| 165 | if (status != 0) { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 166 | LOG(WARNING) << "(" << cfg->netId << ", " << cfg->discoveryId << ") plat_prefix/dns(" |
| 167 | << kIPv4OnlyHost << ") status = " << status << "/" << gai_strerror(status); |
Luke Huang | 7b26b20 | 2019-03-28 14:09:24 +0800 | [diff] [blame] | 168 | return false; |
| 169 | } |
| 170 | |
| 171 | // Use only the first result. If other records are present, possibly |
| 172 | // with differing DNS64 prefixes they are ignored. Note that this is a |
| 173 | // violation of https://tools.ietf.org/html/rfc7050#section-3 |
| 174 | // |
| 175 | // "A node MUST look through all of the received AAAA resource records |
| 176 | // to collect one or more Pref64::/n." |
| 177 | // |
| 178 | // TODO: Consider remedying this. |
| 179 | if (result->ai_family != AF_INET6) { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 180 | LOG(WARNING) << "(" << cfg->netId << ", " << cfg->discoveryId |
| 181 | << ") plat_prefix/unexpected address family: " << result->ai_family; |
Luke Huang | 7b26b20 | 2019-03-28 14:09:24 +0800 | [diff] [blame] | 182 | return false; |
| 183 | } |
| 184 | const IPAddress ipv6(reinterpret_cast<sockaddr_in6*>(result->ai_addr)->sin6_addr); |
| 185 | // Only /96 DNS64 prefixes are supported at this time. |
| 186 | cfg->prefix64 = IPPrefix(ipv6, 96); |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 187 | LOG(WARNING) << "(" << cfg->netId << ", " << cfg->discoveryId << ") Detected NAT64 prefix " |
| 188 | << cfg->prefix64.toString(); |
Luke Huang | 7b26b20 | 2019-03-28 14:09:24 +0800 | [diff] [blame] | 189 | return true; |
| 190 | } |
| 191 | |
| 192 | bool Dns64Configuration::isDiscoveryInProgress(const Dns64Config& cfg) const REQUIRES(mMutex) { |
| 193 | const auto& iter = mDns64Configs.find(cfg.netId); |
| 194 | if (iter == mDns64Configs.end()) return false; |
| 195 | |
| 196 | const Dns64Config& currentCfg = iter->second; |
| 197 | return (currentCfg.discoveryId == cfg.discoveryId); |
| 198 | } |
| 199 | |
| 200 | bool Dns64Configuration::reportNat64PrefixStatus(unsigned netId, bool added, const IPPrefix& pfx) { |
| 201 | if (pfx.ip().family() != AF_INET6 || pfx.ip().scope_id() != 0) { |
chenbruce | aff8584 | 2019-05-31 15:46:42 +0800 | [diff] [blame] | 202 | LOG(WARNING) << "Abort to send NAT64 prefix notification. Unexpected NAT64 prefix (" |
| 203 | << netId << ", " << added << ", " << pfx.toString() << ")."; |
Luke Huang | 7b26b20 | 2019-03-28 14:09:24 +0800 | [diff] [blame] | 204 | return false; |
| 205 | } |
| 206 | Nat64PrefixInfo args = {netId, added, pfx.ip().toString(), (uint8_t)pfx.length()}; |
| 207 | mPrefixCallback(args); |
| 208 | return true; |
| 209 | } |
| 210 | |
| 211 | bool Dns64Configuration::shouldContinueDiscovery(const Dns64Config& cfg) { |
| 212 | std::lock_guard guard(mMutex); |
| 213 | return isDiscoveryInProgress(cfg); |
| 214 | } |
| 215 | |
| 216 | void Dns64Configuration::removeDns64Config(unsigned netId) REQUIRES(mMutex) { |
Lorenzo Colitti | e51091d | 2020-05-20 02:48:44 +0900 | [diff] [blame] | 217 | const auto& iter = mDns64Configs.find(netId); |
| 218 | if (iter == mDns64Configs.end()) return; |
| 219 | |
| 220 | Dns64Config cfg = iter->second; |
| 221 | mDns64Configs.erase(iter); |
| 222 | |
| 223 | // Only report a prefix removed event if the prefix was discovered, not if it was set. |
| 224 | if (cfg.isFromPrefixDiscovery() && !cfg.prefix64.isUninitialized()) { |
| 225 | reportNat64PrefixStatus(netId, PREFIX_REMOVED, cfg.prefix64); |
Luke Huang | 7b26b20 | 2019-03-28 14:09:24 +0800 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
| 229 | void Dns64Configuration::recordDns64Config(const Dns64Config& cfg) { |
| 230 | std::lock_guard guard(mMutex); |
| 231 | if (!isDiscoveryInProgress(cfg)) return; |
| 232 | |
| 233 | removeDns64Config(cfg.netId); |
| 234 | mDns64Configs.emplace(std::make_pair(cfg.netId, cfg)); |
| 235 | |
| 236 | reportNat64PrefixStatus(cfg.netId, PREFIX_ADDED, cfg.prefix64); |
Lorenzo Colitti | e51091d | 2020-05-20 02:48:44 +0900 | [diff] [blame] | 237 | } |
Luke Huang | 7b26b20 | 2019-03-28 14:09:24 +0800 | [diff] [blame] | 238 | |
Lorenzo Colitti | 511b6d4 | 2020-05-21 22:57:31 +0900 | [diff] [blame] | 239 | int Dns64Configuration::setPrefix64(unsigned netId, const IPPrefix& pfx) { |
| 240 | if (pfx.isUninitialized() || pfx.family() != AF_INET6 || pfx.length() != 96) { |
Lorenzo Colitti | e51091d | 2020-05-20 02:48:44 +0900 | [diff] [blame] | 241 | return -EINVAL; |
| 242 | } |
| 243 | |
| 244 | std::lock_guard guard(mMutex); |
| 245 | |
| 246 | // This method may only be called if prefix discovery has been stopped or was never started. |
| 247 | auto iter = mDns64Configs.find(netId); |
| 248 | if (iter != mDns64Configs.end()) { |
| 249 | if (iter->second.isFromPrefixDiscovery()) { |
| 250 | return -EEXIST; |
| 251 | } else { |
| 252 | mDns64Configs.erase(iter); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | Dns64Config cfg(kNoDiscoveryId, netId); |
Lorenzo Colitti | 511b6d4 | 2020-05-21 22:57:31 +0900 | [diff] [blame] | 257 | cfg.prefix64 = pfx; |
Lorenzo Colitti | e51091d | 2020-05-20 02:48:44 +0900 | [diff] [blame] | 258 | mDns64Configs.emplace(std::make_pair(netId, cfg)); |
| 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | int Dns64Configuration::clearPrefix64(unsigned netId) { |
| 264 | std::lock_guard guard(mMutex); |
| 265 | |
| 266 | const auto& iter = mDns64Configs.find(netId); |
| 267 | if (iter == mDns64Configs.end() || iter->second.isFromPrefixDiscovery()) { |
| 268 | return -ENOENT; |
| 269 | } |
| 270 | |
| 271 | mDns64Configs.erase(iter); |
| 272 | |
| 273 | return 0; |
Luke Huang | 7b26b20 | 2019-03-28 14:09:24 +0800 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | } // namespace net |
| 277 | } // namespace android |