blob: 1208d10c01f5a1cdd6f18826ac4b74b5787629cb [file] [log] [blame]
Luke Huang7b26b202019-03-28 14:09:24 +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"
Luke Huang7b26b202019-03-28 14:09:24 +080018
19#include "Dns64Configuration.h"
20
chenbruceaff85842019-05-31 15:46:42 +080021#include <android-base/logging.h>
Luke Huang7b26b202019-03-28 14:09:24 +080022#include <netdb.h>
Luke Huang9d2d25b2019-06-14 00:34:05 +080023#include <netdutils/BackoffSequence.h>
24#include <netdutils/DumpWriter.h>
25#include <netdutils/InternetAddresses.h>
Mike Yu04f1d482019-08-08 11:09:32 +080026#include <netdutils/ThreadUtil.h>
Luke Huangf4c98f22019-06-14 01:57:00 +080027#include <netid_client.h>
Luke Huang7b26b202019-03-28 14:09:24 +080028#include <thread>
29#include <utility>
30
31#include <arpa/inet.h>
32
33#include "DnsResolver.h"
Bernie Innocentie71a28a2019-05-29 00:42:35 +090034#include "getaddrinfo.h"
Luke Huang7b26b202019-03-28 14:09:24 +080035#include "netd_resolv/resolv.h"
lifr94981782019-05-17 21:15:19 +080036#include "stats.pb.h"
Luke Huang7b26b202019-03-28 14:09:24 +080037
38namespace android {
39
Mike Yucb902c92020-05-20 19:26:56 +080040using android::base::StringPrintf;
lifr94981782019-05-17 21:15:19 +080041using android::net::NetworkDnsEventReported;
Luke Huang7b26b202019-03-28 14:09:24 +080042using netdutils::DumpWriter;
43using netdutils::IPAddress;
44using netdutils::IPPrefix;
Luke Huang9d2d25b2019-06-14 00:34:05 +080045using netdutils::ScopedAddrinfo;
Mike Yucb902c92020-05-20 19:26:56 +080046using netdutils::setThreadName;
Luke Huang7b26b202019-03-28 14:09:24 +080047
48namespace net {
49
50const char Dns64Configuration::kIPv4OnlyHost[] = "ipv4only.arpa.";
51const char Dns64Configuration::kIPv4Literal1[] = "192.0.0.170";
52const char Dns64Configuration::kIPv4Literal2[] = "192.0.0.171";
53
54void Dns64Configuration::startPrefixDiscovery(unsigned netId) {
55 std::lock_guard guard(mMutex);
56
57 // TODO: Keep previous prefix for a while
58 // Currently, we remove current prefix, if any, before starting a prefix discovery.
59 // This causes that Netd and framework temporarily forgets DNS64 prefix even the prefix may be
60 // discovered in a short time.
61 removeDns64Config(netId);
62
63 Dns64Config cfg(getNextId(), netId);
64 // Emplace a copy of |cfg| in the map.
65 mDns64Configs.emplace(std::make_pair(netId, cfg));
66
67 // Note that capturing |cfg| in this lambda creates a copy.
Mike Yu04f1d482019-08-08 11:09:32 +080068 std::thread discovery_thread([this, cfg, netId] {
Mike Yucb902c92020-05-20 19:26:56 +080069 setThreadName(StringPrintf("Nat64Pfx_%u", netId).c_str());
Mike Yu04f1d482019-08-08 11:09:32 +080070
Luke Huang7b26b202019-03-28 14:09:24 +080071 // Make a mutable copy rather than mark the whole lambda mutable.
72 // No particular reason.
73 Dns64Config evalCfg(cfg);
74
75 auto backoff = netdutils::BackoffSequence<>::Builder()
76 .withInitialRetransmissionTime(std::chrono::seconds(1))
77 .withMaximumRetransmissionTime(std::chrono::seconds(3600))
78 .build();
79
80 while (true) {
81 if (!this->shouldContinueDiscovery(evalCfg)) break;
82
83 android_net_context netcontext{};
84 mGetNetworkContextCallback(evalCfg.netId, 0, &netcontext);
85
86 // Prefix discovery must bypass private DNS because in strict mode
87 // the server generally won't know the NAT64 prefix.
88 netcontext.flags |= NET_CONTEXT_FLAG_USE_LOCAL_NAMESERVERS;
89 if (doRfc7050PrefixDiscovery(netcontext, &evalCfg)) {
90 this->recordDns64Config(evalCfg);
91 break;
92 }
93
94 if (!this->shouldContinueDiscovery(evalCfg)) break;
95
96 if (!backoff.hasNextTimeout()) break;
97 {
98 std::unique_lock<std::mutex> cvGuard(mMutex);
99 // TODO: Consider some chrono math, combined with wait_until()
100 // perhaps, to prevent early re-resolves from the removal of
101 // other netids with IPv6-only nameservers.
102 mCv.wait_for(cvGuard, backoff.getNextTimeout());
103 }
104 }
105 });
106 discovery_thread.detach();
107}
108
109void Dns64Configuration::stopPrefixDiscovery(unsigned netId) {
110 std::lock_guard guard(mMutex);
111 removeDns64Config(netId);
112 mCv.notify_all();
113}
114
115IPPrefix Dns64Configuration::getPrefix64Locked(unsigned netId) const REQUIRES(mMutex) {
116 const auto& iter = mDns64Configs.find(netId);
117 if (iter != mDns64Configs.end()) return iter->second.prefix64;
118
119 return IPPrefix{};
120}
121
122IPPrefix Dns64Configuration::getPrefix64(unsigned netId) const {
123 std::lock_guard guard(mMutex);
124 return getPrefix64Locked(netId);
125}
126
127void Dns64Configuration::dump(DumpWriter& dw, unsigned netId) {
128 static const char kLabel[] = "DNS64 config";
129
130 std::lock_guard guard(mMutex);
131
132 const auto& iter = mDns64Configs.find(netId);
133 if (iter == mDns64Configs.end()) {
134 dw.println("%s: none", kLabel);
135 return;
136 }
137
138 const Dns64Config& cfg = iter->second;
139 if (cfg.prefix64.length() == 0) {
140 dw.println("%s: no prefix yet discovered", kLabel);
141 } else {
142 dw.println("%s: discovered prefix %s", kLabel, cfg.prefix64.toString().c_str());
143 }
144}
145
146// NOTE: The full RFC 7050 DNS64 discovery process is not implemented here.
147// Instead, and more simplistic version of the same thing is done, and it
148// currently assumes the DNS64 prefix is a /96.
149bool Dns64Configuration::doRfc7050PrefixDiscovery(const android_net_context& netcontext,
150 Dns64Config* cfg) {
chenbruceaff85842019-05-31 15:46:42 +0800151 LOG(WARNING) << "(" << cfg->netId << ", " << cfg->discoveryId
152 << ") Detecting NAT64 prefix from DNS...";
Luke Huang7b26b202019-03-28 14:09:24 +0800153
154 const struct addrinfo hints = {
155 .ai_family = AF_INET6,
156 };
157
158 // TODO: Refactor so that netd can get all the regular getaddrinfo handling
159 // that regular apps get. We bypass the UNIX socket connection back to
160 // ourselves, which means we also bypass all the special netcontext flag
161 // handling and the resolver event logging.
162 struct addrinfo* res = nullptr;
lifr94981782019-05-17 21:15:19 +0800163 NetworkDnsEventReported event;
164 const int status =
165 resolv_getaddrinfo(kIPv4OnlyHost, nullptr, &hints, &netcontext, &res, &event);
Luke Huang7b26b202019-03-28 14:09:24 +0800166 ScopedAddrinfo result(res);
167 if (status != 0) {
chenbruceaff85842019-05-31 15:46:42 +0800168 LOG(WARNING) << "(" << cfg->netId << ", " << cfg->discoveryId << ") plat_prefix/dns("
169 << kIPv4OnlyHost << ") status = " << status << "/" << gai_strerror(status);
Luke Huang7b26b202019-03-28 14:09:24 +0800170 return false;
171 }
172
173 // Use only the first result. If other records are present, possibly
174 // with differing DNS64 prefixes they are ignored. Note that this is a
175 // violation of https://tools.ietf.org/html/rfc7050#section-3
176 //
177 // "A node MUST look through all of the received AAAA resource records
178 // to collect one or more Pref64::/n."
179 //
180 // TODO: Consider remedying this.
181 if (result->ai_family != AF_INET6) {
chenbruceaff85842019-05-31 15:46:42 +0800182 LOG(WARNING) << "(" << cfg->netId << ", " << cfg->discoveryId
183 << ") plat_prefix/unexpected address family: " << result->ai_family;
Luke Huang7b26b202019-03-28 14:09:24 +0800184 return false;
185 }
186 const IPAddress ipv6(reinterpret_cast<sockaddr_in6*>(result->ai_addr)->sin6_addr);
187 // Only /96 DNS64 prefixes are supported at this time.
188 cfg->prefix64 = IPPrefix(ipv6, 96);
chenbruceaff85842019-05-31 15:46:42 +0800189 LOG(WARNING) << "(" << cfg->netId << ", " << cfg->discoveryId << ") Detected NAT64 prefix "
190 << cfg->prefix64.toString();
Luke Huang7b26b202019-03-28 14:09:24 +0800191 return true;
192}
193
194bool Dns64Configuration::isDiscoveryInProgress(const Dns64Config& cfg) const REQUIRES(mMutex) {
195 const auto& iter = mDns64Configs.find(cfg.netId);
196 if (iter == mDns64Configs.end()) return false;
197
198 const Dns64Config& currentCfg = iter->second;
199 return (currentCfg.discoveryId == cfg.discoveryId);
200}
201
202bool Dns64Configuration::reportNat64PrefixStatus(unsigned netId, bool added, const IPPrefix& pfx) {
203 if (pfx.ip().family() != AF_INET6 || pfx.ip().scope_id() != 0) {
chenbruceaff85842019-05-31 15:46:42 +0800204 LOG(WARNING) << "Abort to send NAT64 prefix notification. Unexpected NAT64 prefix ("
205 << netId << ", " << added << ", " << pfx.toString() << ").";
Luke Huang7b26b202019-03-28 14:09:24 +0800206 return false;
207 }
208 Nat64PrefixInfo args = {netId, added, pfx.ip().toString(), (uint8_t)pfx.length()};
209 mPrefixCallback(args);
210 return true;
211}
212
213bool Dns64Configuration::shouldContinueDiscovery(const Dns64Config& cfg) {
214 std::lock_guard guard(mMutex);
215 return isDiscoveryInProgress(cfg);
216}
217
218void Dns64Configuration::removeDns64Config(unsigned netId) REQUIRES(mMutex) {
219 IPPrefix prefix = getPrefix64Locked(netId);
220 mDns64Configs.erase(netId);
221 if (!prefix.isUninitialized()) {
222 reportNat64PrefixStatus(netId, PREFIX_REMOVED, prefix);
223 }
224}
225
226void Dns64Configuration::recordDns64Config(const Dns64Config& cfg) {
227 std::lock_guard guard(mMutex);
228 if (!isDiscoveryInProgress(cfg)) return;
229
230 removeDns64Config(cfg.netId);
231 mDns64Configs.emplace(std::make_pair(cfg.netId, cfg));
232
233 reportNat64PrefixStatus(cfg.netId, PREFIX_ADDED, cfg.prefix64);
234
235 // TODO: consider extending INetdEventListener to report the DNS64 prefix
236 // up to ConnectivityService to potentially include this in LinkProperties.
237}
238
239} // namespace net
240} // namespace android