blob: 58ef2a7160b855ba099cf5e8cc4162408407fff5 [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"
lifr94981782019-05-17 21:15:19 +080020#include <netdutils/Stopwatch.h>
Bernie Innocentiec4219b2019-01-30 11:16:36 +090021#include "DnsTlsSocketFactory.h"
lifr94981782019-05-17 21:15:19 +080022#include "resolv_private.h"
23#include "stats.pb.h"
Mike Yubab3daa2018-10-19 22:11:43 +080024
chenbruceaff85842019-05-31 15:46:42 +080025#include <android-base/logging.h>
Mike Yubab3daa2018-10-19 22:11:43 +080026
27namespace android {
28namespace net {
29
lifr94981782019-05-17 21:15:19 +080030using android::netdutils::Stopwatch;
Mike Yubab3daa2018-10-19 22:11:43 +080031using netdutils::Slice;
32
33// static
34std::mutex DnsTlsDispatcher::sLock;
35
36DnsTlsDispatcher::DnsTlsDispatcher() {
37 mFactory.reset(new DnsTlsSocketFactory());
38}
39
40std::list<DnsTlsServer> DnsTlsDispatcher::getOrderedServerList(
41 const std::list<DnsTlsServer> &tlsServers, unsigned mark) const {
42 // Our preferred DnsTlsServer order is:
43 // 1) reuse existing IPv6 connections
44 // 2) reuse existing IPv4 connections
45 // 3) establish new IPv6 connections
46 // 4) establish new IPv4 connections
47 std::list<DnsTlsServer> existing6;
48 std::list<DnsTlsServer> existing4;
49 std::list<DnsTlsServer> new6;
50 std::list<DnsTlsServer> new4;
51
52 // Pull out any servers for which we might have existing connections and
53 // place them at the from the list of servers to try.
54 {
55 std::lock_guard guard(sLock);
56
57 for (const auto& tlsServer : tlsServers) {
58 const Key key = std::make_pair(mark, tlsServer);
59 if (mStore.find(key) != mStore.end()) {
60 switch (tlsServer.ss.ss_family) {
61 case AF_INET:
62 existing4.push_back(tlsServer);
63 break;
64 case AF_INET6:
65 existing6.push_back(tlsServer);
66 break;
67 }
68 } else {
69 switch (tlsServer.ss.ss_family) {
70 case AF_INET:
71 new4.push_back(tlsServer);
72 break;
73 case AF_INET6:
74 new6.push_back(tlsServer);
75 break;
76 }
77 }
78 }
79 }
80
81 auto& out = existing6;
82 out.splice(out.cend(), existing4);
83 out.splice(out.cend(), new6);
84 out.splice(out.cend(), new4);
85 return out;
86}
87
lifr94981782019-05-17 21:15:19 +080088DnsTlsTransport::Response DnsTlsDispatcher::query(const std::list<DnsTlsServer>& tlsServers,
89 res_state statp, const Slice query,
90 const Slice ans, int* resplen) {
91 const std::list<DnsTlsServer> orderedServers(getOrderedServerList(tlsServers, statp->_mark));
Mike Yubab3daa2018-10-19 22:11:43 +080092
chenbruceaff85842019-05-31 15:46:42 +080093 if (orderedServers.empty()) LOG(WARNING) << "Empty DnsTlsServer list";
Mike Yubab3daa2018-10-19 22:11:43 +080094
95 DnsTlsTransport::Response code = DnsTlsTransport::Response::internal_error;
lifr94981782019-05-17 21:15:19 +080096 int serverCount = 0;
Mike Yubab3daa2018-10-19 22:11:43 +080097 for (const auto& server : orderedServers) {
lifr94981782019-05-17 21:15:19 +080098 DnsQueryEvent* dnsQueryEvent =
99 statp->event->mutable_dns_query_events()->add_dns_query_event();
lifrd4d9fbb2019-07-31 20:18:35 +0800100 Stopwatch queryStopwatch;
lifr94981782019-05-17 21:15:19 +0800101 code = this->query(server, statp->_mark, query, ans, resplen);
102
lifrd4d9fbb2019-07-31 20:18:35 +0800103 dnsQueryEvent->set_latency_micros(saturate_cast<int32_t>(queryStopwatch.timeTakenUs()));
lifr94981782019-05-17 21:15:19 +0800104 dnsQueryEvent->set_dns_server_index(serverCount++);
105 dnsQueryEvent->set_ip_version(ipFamilyToIPVersion(server.ss.ss_family));
106 dnsQueryEvent->set_protocol(PROTO_DOT);
107 dnsQueryEvent->set_type(getQueryType(query.base(), query.size()));
108
Mike Yubab3daa2018-10-19 22:11:43 +0800109 switch (code) {
110 // These response codes are valid responses and not expected to
111 // change if another server is queried.
112 case DnsTlsTransport::Response::success:
lifr94981782019-05-17 21:15:19 +0800113 dnsQueryEvent->set_rcode(
114 static_cast<NsRcode>(reinterpret_cast<HEADER*>(ans.base())->rcode));
lifrd4d9fbb2019-07-31 20:18:35 +0800115 return code;
Mike Yubab3daa2018-10-19 22:11:43 +0800116 case DnsTlsTransport::Response::limit_error:
lifrd4d9fbb2019-07-31 20:18:35 +0800117 dnsQueryEvent->set_rcode(NS_R_INTERNAL_ERROR);
Mike Yubab3daa2018-10-19 22:11:43 +0800118 return code;
Mike Yubab3daa2018-10-19 22:11:43 +0800119 // These response codes might differ when trying other servers, so
120 // keep iterating to see if we can get a different (better) result.
121 case DnsTlsTransport::Response::network_error:
lifr94981782019-05-17 21:15:19 +0800122 // Sync from res_tls_send in res_send.cpp
123 dnsQueryEvent->set_rcode(NS_R_TIMEOUT);
lifrd4d9fbb2019-07-31 20:18:35 +0800124 continue;
Mike Yubab3daa2018-10-19 22:11:43 +0800125 case DnsTlsTransport::Response::internal_error:
lifrd4d9fbb2019-07-31 20:18:35 +0800126 dnsQueryEvent->set_rcode(NS_R_INTERNAL_ERROR);
Mike Yubab3daa2018-10-19 22:11:43 +0800127 continue;
Mike Yubab3daa2018-10-19 22:11:43 +0800128 // No "default" statement.
129 }
130 }
131
132 return code;
133}
134
135DnsTlsTransport::Response DnsTlsDispatcher::query(const DnsTlsServer& server, unsigned mark,
136 const Slice query,
137 const Slice ans, int *resplen) {
138 const Key key = std::make_pair(mark, server);
139 Transport* xport;
140 {
141 std::lock_guard guard(sLock);
142 auto it = mStore.find(key);
143 if (it == mStore.end()) {
144 xport = new Transport(server, mark, mFactory.get());
145 mStore[key].reset(xport);
146 } else {
147 xport = it->second.get();
148 }
149 ++xport->useCount;
150 }
151
chenbruceaff85842019-05-31 15:46:42 +0800152 LOG(DEBUG) << "Sending query of length " << query.size();
Mike Yubab3daa2018-10-19 22:11:43 +0800153 auto res = xport->transport.query(query);
chenbruceaff85842019-05-31 15:46:42 +0800154 LOG(DEBUG) << "Awaiting response";
Mike Yubab3daa2018-10-19 22:11:43 +0800155 const auto& result = res.get();
156 DnsTlsTransport::Response code = result.code;
157 if (code == DnsTlsTransport::Response::success) {
158 if (result.response.size() > ans.size()) {
chenbruceaff85842019-05-31 15:46:42 +0800159 LOG(DEBUG) << "Response too large: " << result.response.size() << " > " << ans.size();
Mike Yubab3daa2018-10-19 22:11:43 +0800160 code = DnsTlsTransport::Response::limit_error;
161 } else {
chenbruceaff85842019-05-31 15:46:42 +0800162 LOG(DEBUG) << "Got response successfully";
Mike Yubab3daa2018-10-19 22:11:43 +0800163 *resplen = result.response.size();
164 netdutils::copy(ans, netdutils::makeSlice(result.response));
165 }
166 } else {
chenbruceaff85842019-05-31 15:46:42 +0800167 LOG(DEBUG) << "Query failed: " << (unsigned int)code;
Mike Yubab3daa2018-10-19 22:11:43 +0800168 }
169
170 auto now = std::chrono::steady_clock::now();
171 {
172 std::lock_guard guard(sLock);
173 --xport->useCount;
174 xport->lastUsed = now;
175 cleanup(now);
176 }
177 return code;
178}
179
180// This timeout effectively controls how long to keep SSL session tickets.
181static constexpr std::chrono::minutes IDLE_TIMEOUT(5);
182void DnsTlsDispatcher::cleanup(std::chrono::time_point<std::chrono::steady_clock> now) {
183 // To avoid scanning mStore after every query, return early if a cleanup has been
184 // performed recently.
185 if (now - mLastCleanup < IDLE_TIMEOUT) {
186 return;
187 }
188 for (auto it = mStore.begin(); it != mStore.end();) {
189 auto& s = it->second;
190 if (s->useCount == 0 && now - s->lastUsed > IDLE_TIMEOUT) {
191 it = mStore.erase(it);
192 } else {
193 ++it;
194 }
195 }
196 mLastCleanup = now;
197}
198
199} // end of namespace net
200} // end of namespace android