blob: 41eac9dfe0c155303e66891788d0073a26c20ccb [file] [log] [blame]
Mike Yubab3daa2018-10-19 22:11:43 +08001/*
2 * Copyright (C) 2017 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 Yubab3daa2018-10-19 22:11:43 +080018
Bernie Innocentiec4219b2019-01-30 11:16:36 +090019#include "DnsTlsDispatcher.h"
Mike Yue655b1d2019-08-28 17:49:59 +080020
lifr94981782019-05-17 21:15:19 +080021#include <netdutils/Stopwatch.h>
Mike Yue655b1d2019-08-28 17:49:59 +080022
Bernie Innocentiec4219b2019-01-30 11:16:36 +090023#include "DnsTlsSocketFactory.h"
Mike Yue655b1d2019-08-28 17:49:59 +080024#include "resolv_cache.h"
lifr94981782019-05-17 21:15:19 +080025#include "resolv_private.h"
26#include "stats.pb.h"
Mike Yubab3daa2018-10-19 22:11:43 +080027
chenbruceaff85842019-05-31 15:46:42 +080028#include <android-base/logging.h>
Mike Yubab3daa2018-10-19 22:11:43 +080029
30namespace android {
31namespace net {
32
Mike Yue655b1d2019-08-28 17:49:59 +080033using android::netdutils::IPSockAddr;
lifr94981782019-05-17 21:15:19 +080034using android::netdutils::Stopwatch;
Mike Yubab3daa2018-10-19 22:11:43 +080035using netdutils::Slice;
36
37// static
38std::mutex DnsTlsDispatcher::sLock;
39
40DnsTlsDispatcher::DnsTlsDispatcher() {
41 mFactory.reset(new DnsTlsSocketFactory());
42}
43
Mike Yu9e8cf8d2020-10-26 19:04:33 +080044DnsTlsDispatcher& DnsTlsDispatcher::getInstance() {
45 static DnsTlsDispatcher instance;
46 return instance;
47}
48
Mike Yubab3daa2018-10-19 22:11:43 +080049std::list<DnsTlsServer> DnsTlsDispatcher::getOrderedServerList(
50 const std::list<DnsTlsServer> &tlsServers, unsigned mark) const {
51 // Our preferred DnsTlsServer order is:
52 // 1) reuse existing IPv6 connections
53 // 2) reuse existing IPv4 connections
54 // 3) establish new IPv6 connections
55 // 4) establish new IPv4 connections
56 std::list<DnsTlsServer> existing6;
57 std::list<DnsTlsServer> existing4;
58 std::list<DnsTlsServer> new6;
59 std::list<DnsTlsServer> new4;
60
61 // Pull out any servers for which we might have existing connections and
62 // place them at the from the list of servers to try.
63 {
64 std::lock_guard guard(sLock);
65
66 for (const auto& tlsServer : tlsServers) {
67 const Key key = std::make_pair(mark, tlsServer);
68 if (mStore.find(key) != mStore.end()) {
69 switch (tlsServer.ss.ss_family) {
70 case AF_INET:
71 existing4.push_back(tlsServer);
72 break;
73 case AF_INET6:
74 existing6.push_back(tlsServer);
75 break;
76 }
77 } else {
78 switch (tlsServer.ss.ss_family) {
79 case AF_INET:
80 new4.push_back(tlsServer);
81 break;
82 case AF_INET6:
83 new6.push_back(tlsServer);
84 break;
85 }
86 }
87 }
88 }
89
90 auto& out = existing6;
91 out.splice(out.cend(), existing4);
92 out.splice(out.cend(), new6);
93 out.splice(out.cend(), new4);
94 return out;
95}
96
lifr94981782019-05-17 21:15:19 +080097DnsTlsTransport::Response DnsTlsDispatcher::query(const std::list<DnsTlsServer>& tlsServers,
98 res_state statp, const Slice query,
99 const Slice ans, int* resplen) {
100 const std::list<DnsTlsServer> orderedServers(getOrderedServerList(tlsServers, statp->_mark));
Mike Yubab3daa2018-10-19 22:11:43 +0800101
chenbruceaff85842019-05-31 15:46:42 +0800102 if (orderedServers.empty()) LOG(WARNING) << "Empty DnsTlsServer list";
Mike Yubab3daa2018-10-19 22:11:43 +0800103
104 DnsTlsTransport::Response code = DnsTlsTransport::Response::internal_error;
lifr94981782019-05-17 21:15:19 +0800105 int serverCount = 0;
Mike Yubab3daa2018-10-19 22:11:43 +0800106 for (const auto& server : orderedServers) {
lifr94981782019-05-17 21:15:19 +0800107 DnsQueryEvent* dnsQueryEvent =
108 statp->event->mutable_dns_query_events()->add_dns_query_event();
Mike Yucb2bb7c2019-11-22 20:42:13 +0800109
110 bool connectTriggered = false;
lifrd4d9fbb2019-07-31 20:18:35 +0800111 Stopwatch queryStopwatch;
Mike Yucb2bb7c2019-11-22 20:42:13 +0800112 code = this->query(server, statp->_mark, query, ans, resplen, &connectTriggered);
lifr94981782019-05-17 21:15:19 +0800113
lifrd4d9fbb2019-07-31 20:18:35 +0800114 dnsQueryEvent->set_latency_micros(saturate_cast<int32_t>(queryStopwatch.timeTakenUs()));
lifr94981782019-05-17 21:15:19 +0800115 dnsQueryEvent->set_dns_server_index(serverCount++);
116 dnsQueryEvent->set_ip_version(ipFamilyToIPVersion(server.ss.ss_family));
117 dnsQueryEvent->set_protocol(PROTO_DOT);
118 dnsQueryEvent->set_type(getQueryType(query.base(), query.size()));
Mike Yucb2bb7c2019-11-22 20:42:13 +0800119 dnsQueryEvent->set_connected(connectTriggered);
lifr94981782019-05-17 21:15:19 +0800120
Mike Yubab3daa2018-10-19 22:11:43 +0800121 switch (code) {
122 // These response codes are valid responses and not expected to
123 // change if another server is queried.
124 case DnsTlsTransport::Response::success:
lifr94981782019-05-17 21:15:19 +0800125 dnsQueryEvent->set_rcode(
126 static_cast<NsRcode>(reinterpret_cast<HEADER*>(ans.base())->rcode));
Mike Yue655b1d2019-08-28 17:49:59 +0800127 resolv_stats_add(statp->netid, IPSockAddr::toIPSockAddr(server.ss), dnsQueryEvent);
lifrd4d9fbb2019-07-31 20:18:35 +0800128 return code;
Mike Yubab3daa2018-10-19 22:11:43 +0800129 case DnsTlsTransport::Response::limit_error:
lifrd4d9fbb2019-07-31 20:18:35 +0800130 dnsQueryEvent->set_rcode(NS_R_INTERNAL_ERROR);
Mike Yue655b1d2019-08-28 17:49:59 +0800131 resolv_stats_add(statp->netid, IPSockAddr::toIPSockAddr(server.ss), dnsQueryEvent);
Mike Yubab3daa2018-10-19 22:11:43 +0800132 return code;
Mike Yubab3daa2018-10-19 22:11:43 +0800133 // These response codes might differ when trying other servers, so
134 // keep iterating to see if we can get a different (better) result.
135 case DnsTlsTransport::Response::network_error:
lifr94981782019-05-17 21:15:19 +0800136 // Sync from res_tls_send in res_send.cpp
137 dnsQueryEvent->set_rcode(NS_R_TIMEOUT);
Mike Yue655b1d2019-08-28 17:49:59 +0800138 resolv_stats_add(statp->netid, IPSockAddr::toIPSockAddr(server.ss), dnsQueryEvent);
139 break;
Mike Yubab3daa2018-10-19 22:11:43 +0800140 case DnsTlsTransport::Response::internal_error:
lifrd4d9fbb2019-07-31 20:18:35 +0800141 dnsQueryEvent->set_rcode(NS_R_INTERNAL_ERROR);
Mike Yue655b1d2019-08-28 17:49:59 +0800142 resolv_stats_add(statp->netid, IPSockAddr::toIPSockAddr(server.ss), dnsQueryEvent);
143 break;
Mike Yubab3daa2018-10-19 22:11:43 +0800144 // No "default" statement.
145 }
146 }
147
148 return code;
149}
150
151DnsTlsTransport::Response DnsTlsDispatcher::query(const DnsTlsServer& server, unsigned mark,
Mike Yucb2bb7c2019-11-22 20:42:13 +0800152 const Slice query, const Slice ans, int* resplen,
153 bool* connectTriggered) {
Mike Yue9b78d82020-05-20 20:58:49 +0800154 // TODO: This can cause the resolver to create multiple connections to the same DoT server
155 // merely due to different mark, such as the bit explicitlySelected unset.
156 // See if we can save them and just create one connection for one DoT server.
Mike Yubab3daa2018-10-19 22:11:43 +0800157 const Key key = std::make_pair(mark, server);
158 Transport* xport;
159 {
160 std::lock_guard guard(sLock);
161 auto it = mStore.find(key);
162 if (it == mStore.end()) {
163 xport = new Transport(server, mark, mFactory.get());
164 mStore[key].reset(xport);
165 } else {
166 xport = it->second.get();
167 }
168 ++xport->useCount;
169 }
170
Mike Yu568ed6c2020-07-01 12:02:14 +0800171 // Don't call this function and hold sLock at the same time because of the following reason:
172 // TLS handshake requires a lock which is also needed by this function, if the handshake gets
173 // stuck, this function also gets blocked.
174 const int connectCounter = xport->transport.getConnectCounter();
175
chenbruceaff85842019-05-31 15:46:42 +0800176 LOG(DEBUG) << "Sending query of length " << query.size();
Mike Yubab3daa2018-10-19 22:11:43 +0800177 auto res = xport->transport.query(query);
chenbruceaff85842019-05-31 15:46:42 +0800178 LOG(DEBUG) << "Awaiting response";
Mike Yubab3daa2018-10-19 22:11:43 +0800179 const auto& result = res.get();
Mike Yu568ed6c2020-07-01 12:02:14 +0800180 *connectTriggered = (xport->transport.getConnectCounter() > connectCounter);
181
Mike Yubab3daa2018-10-19 22:11:43 +0800182 DnsTlsTransport::Response code = result.code;
183 if (code == DnsTlsTransport::Response::success) {
184 if (result.response.size() > ans.size()) {
chenbruceaff85842019-05-31 15:46:42 +0800185 LOG(DEBUG) << "Response too large: " << result.response.size() << " > " << ans.size();
Mike Yubab3daa2018-10-19 22:11:43 +0800186 code = DnsTlsTransport::Response::limit_error;
187 } else {
chenbruceaff85842019-05-31 15:46:42 +0800188 LOG(DEBUG) << "Got response successfully";
Mike Yubab3daa2018-10-19 22:11:43 +0800189 *resplen = result.response.size();
190 netdutils::copy(ans, netdutils::makeSlice(result.response));
191 }
192 } else {
chenbruceaff85842019-05-31 15:46:42 +0800193 LOG(DEBUG) << "Query failed: " << (unsigned int)code;
Mike Yubab3daa2018-10-19 22:11:43 +0800194 }
195
196 auto now = std::chrono::steady_clock::now();
197 {
198 std::lock_guard guard(sLock);
Mike Yubab3daa2018-10-19 22:11:43 +0800199 --xport->useCount;
200 xport->lastUsed = now;
201 cleanup(now);
202 }
203 return code;
204}
205
206// This timeout effectively controls how long to keep SSL session tickets.
207static constexpr std::chrono::minutes IDLE_TIMEOUT(5);
208void DnsTlsDispatcher::cleanup(std::chrono::time_point<std::chrono::steady_clock> now) {
209 // To avoid scanning mStore after every query, return early if a cleanup has been
210 // performed recently.
211 if (now - mLastCleanup < IDLE_TIMEOUT) {
212 return;
213 }
214 for (auto it = mStore.begin(); it != mStore.end();) {
215 auto& s = it->second;
216 if (s->useCount == 0 && now - s->lastUsed > IDLE_TIMEOUT) {
217 it = mStore.erase(it);
218 } else {
219 ++it;
220 }
221 }
222 mLastCleanup = now;
223}
224
225} // end of namespace net
226} // end of namespace android