blob: d1c4022f70002132015f3003ee6f9b55be1d3a74 [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();
100 dnsQueryEvent->set_rcode(NS_R_INTERNAL_ERROR);
101 Stopwatch query_stopwatch;
102 code = this->query(server, statp->_mark, query, ans, resplen);
103
104 dnsQueryEvent->set_latency_micros(saturate_cast<int32_t>(query_stopwatch.timeTakenUs()));
105 dnsQueryEvent->set_dns_server_index(serverCount++);
106 dnsQueryEvent->set_ip_version(ipFamilyToIPVersion(server.ss.ss_family));
107 dnsQueryEvent->set_protocol(PROTO_DOT);
108 dnsQueryEvent->set_type(getQueryType(query.base(), query.size()));
109
Mike Yubab3daa2018-10-19 22:11:43 +0800110 switch (code) {
111 // These response codes are valid responses and not expected to
112 // change if another server is queried.
113 case DnsTlsTransport::Response::success:
lifr94981782019-05-17 21:15:19 +0800114 dnsQueryEvent->set_rcode(
115 static_cast<NsRcode>(reinterpret_cast<HEADER*>(ans.base())->rcode));
116 [[fallthrough]];
Mike Yubab3daa2018-10-19 22:11:43 +0800117 case DnsTlsTransport::Response::limit_error:
118 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);
124 [[fallthrough]];
Mike Yubab3daa2018-10-19 22:11:43 +0800125 case DnsTlsTransport::Response::internal_error:
126 continue;
Mike Yubab3daa2018-10-19 22:11:43 +0800127 // No "default" statement.
128 }
129 }
130
131 return code;
132}
133
134DnsTlsTransport::Response DnsTlsDispatcher::query(const DnsTlsServer& server, unsigned mark,
135 const Slice query,
136 const Slice ans, int *resplen) {
137 const Key key = std::make_pair(mark, server);
138 Transport* xport;
139 {
140 std::lock_guard guard(sLock);
141 auto it = mStore.find(key);
142 if (it == mStore.end()) {
143 xport = new Transport(server, mark, mFactory.get());
144 mStore[key].reset(xport);
145 } else {
146 xport = it->second.get();
147 }
148 ++xport->useCount;
149 }
150
chenbruceaff85842019-05-31 15:46:42 +0800151 LOG(DEBUG) << "Sending query of length " << query.size();
Mike Yubab3daa2018-10-19 22:11:43 +0800152 auto res = xport->transport.query(query);
chenbruceaff85842019-05-31 15:46:42 +0800153 LOG(DEBUG) << "Awaiting response";
Mike Yubab3daa2018-10-19 22:11:43 +0800154 const auto& result = res.get();
155 DnsTlsTransport::Response code = result.code;
156 if (code == DnsTlsTransport::Response::success) {
157 if (result.response.size() > ans.size()) {
chenbruceaff85842019-05-31 15:46:42 +0800158 LOG(DEBUG) << "Response too large: " << result.response.size() << " > " << ans.size();
Mike Yubab3daa2018-10-19 22:11:43 +0800159 code = DnsTlsTransport::Response::limit_error;
160 } else {
chenbruceaff85842019-05-31 15:46:42 +0800161 LOG(DEBUG) << "Got response successfully";
Mike Yubab3daa2018-10-19 22:11:43 +0800162 *resplen = result.response.size();
163 netdutils::copy(ans, netdutils::makeSlice(result.response));
164 }
165 } else {
chenbruceaff85842019-05-31 15:46:42 +0800166 LOG(DEBUG) << "Query failed: " << (unsigned int)code;
Mike Yubab3daa2018-10-19 22:11:43 +0800167 }
168
169 auto now = std::chrono::steady_clock::now();
170 {
171 std::lock_guard guard(sLock);
172 --xport->useCount;
173 xport->lastUsed = now;
174 cleanup(now);
175 }
176 return code;
177}
178
179// This timeout effectively controls how long to keep SSL session tickets.
180static constexpr std::chrono::minutes IDLE_TIMEOUT(5);
181void DnsTlsDispatcher::cleanup(std::chrono::time_point<std::chrono::steady_clock> now) {
182 // To avoid scanning mStore after every query, return early if a cleanup has been
183 // performed recently.
184 if (now - mLastCleanup < IDLE_TIMEOUT) {
185 return;
186 }
187 for (auto it = mStore.begin(); it != mStore.end();) {
188 auto& s = it->second;
189 if (s->useCount == 0 && now - s->lastUsed > IDLE_TIMEOUT) {
190 it = mStore.erase(it);
191 } else {
192 ++it;
193 }
194 }
195 mLastCleanup = now;
196}
197
198} // end of namespace net
199} // end of namespace android