blob: 7ca0464666744452538937d326e35e995b2cdfc5 [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
44std::list<DnsTlsServer> DnsTlsDispatcher::getOrderedServerList(
45 const std::list<DnsTlsServer> &tlsServers, unsigned mark) const {
46 // Our preferred DnsTlsServer order is:
47 // 1) reuse existing IPv6 connections
48 // 2) reuse existing IPv4 connections
49 // 3) establish new IPv6 connections
50 // 4) establish new IPv4 connections
51 std::list<DnsTlsServer> existing6;
52 std::list<DnsTlsServer> existing4;
53 std::list<DnsTlsServer> new6;
54 std::list<DnsTlsServer> new4;
55
56 // Pull out any servers for which we might have existing connections and
57 // place them at the from the list of servers to try.
58 {
59 std::lock_guard guard(sLock);
60
61 for (const auto& tlsServer : tlsServers) {
62 const Key key = std::make_pair(mark, tlsServer);
63 if (mStore.find(key) != mStore.end()) {
64 switch (tlsServer.ss.ss_family) {
65 case AF_INET:
66 existing4.push_back(tlsServer);
67 break;
68 case AF_INET6:
69 existing6.push_back(tlsServer);
70 break;
71 }
72 } else {
73 switch (tlsServer.ss.ss_family) {
74 case AF_INET:
75 new4.push_back(tlsServer);
76 break;
77 case AF_INET6:
78 new6.push_back(tlsServer);
79 break;
80 }
81 }
82 }
83 }
84
85 auto& out = existing6;
86 out.splice(out.cend(), existing4);
87 out.splice(out.cend(), new6);
88 out.splice(out.cend(), new4);
89 return out;
90}
91
lifr94981782019-05-17 21:15:19 +080092DnsTlsTransport::Response DnsTlsDispatcher::query(const std::list<DnsTlsServer>& tlsServers,
93 res_state statp, const Slice query,
94 const Slice ans, int* resplen) {
95 const std::list<DnsTlsServer> orderedServers(getOrderedServerList(tlsServers, statp->_mark));
Mike Yubab3daa2018-10-19 22:11:43 +080096
chenbruceaff85842019-05-31 15:46:42 +080097 if (orderedServers.empty()) LOG(WARNING) << "Empty DnsTlsServer list";
Mike Yubab3daa2018-10-19 22:11:43 +080098
99 DnsTlsTransport::Response code = DnsTlsTransport::Response::internal_error;
lifr94981782019-05-17 21:15:19 +0800100 int serverCount = 0;
Mike Yubab3daa2018-10-19 22:11:43 +0800101 for (const auto& server : orderedServers) {
lifr94981782019-05-17 21:15:19 +0800102 DnsQueryEvent* dnsQueryEvent =
103 statp->event->mutable_dns_query_events()->add_dns_query_event();
lifrd4d9fbb2019-07-31 20:18:35 +0800104 Stopwatch queryStopwatch;
lifr94981782019-05-17 21:15:19 +0800105 code = this->query(server, statp->_mark, query, ans, resplen);
106
lifrd4d9fbb2019-07-31 20:18:35 +0800107 dnsQueryEvent->set_latency_micros(saturate_cast<int32_t>(queryStopwatch.timeTakenUs()));
lifr94981782019-05-17 21:15:19 +0800108 dnsQueryEvent->set_dns_server_index(serverCount++);
109 dnsQueryEvent->set_ip_version(ipFamilyToIPVersion(server.ss.ss_family));
110 dnsQueryEvent->set_protocol(PROTO_DOT);
111 dnsQueryEvent->set_type(getQueryType(query.base(), query.size()));
112
Mike Yubab3daa2018-10-19 22:11:43 +0800113 switch (code) {
114 // These response codes are valid responses and not expected to
115 // change if another server is queried.
116 case DnsTlsTransport::Response::success:
lifr94981782019-05-17 21:15:19 +0800117 dnsQueryEvent->set_rcode(
118 static_cast<NsRcode>(reinterpret_cast<HEADER*>(ans.base())->rcode));
Mike Yue655b1d2019-08-28 17:49:59 +0800119 resolv_stats_add(statp->netid, IPSockAddr::toIPSockAddr(server.ss), dnsQueryEvent);
lifrd4d9fbb2019-07-31 20:18:35 +0800120 return code;
Mike Yubab3daa2018-10-19 22:11:43 +0800121 case DnsTlsTransport::Response::limit_error:
lifrd4d9fbb2019-07-31 20:18:35 +0800122 dnsQueryEvent->set_rcode(NS_R_INTERNAL_ERROR);
Mike Yue655b1d2019-08-28 17:49:59 +0800123 resolv_stats_add(statp->netid, IPSockAddr::toIPSockAddr(server.ss), dnsQueryEvent);
Mike Yubab3daa2018-10-19 22:11:43 +0800124 return code;
Mike Yubab3daa2018-10-19 22:11:43 +0800125 // These response codes might differ when trying other servers, so
126 // keep iterating to see if we can get a different (better) result.
127 case DnsTlsTransport::Response::network_error:
lifr94981782019-05-17 21:15:19 +0800128 // Sync from res_tls_send in res_send.cpp
129 dnsQueryEvent->set_rcode(NS_R_TIMEOUT);
Mike Yue655b1d2019-08-28 17:49:59 +0800130 resolv_stats_add(statp->netid, IPSockAddr::toIPSockAddr(server.ss), dnsQueryEvent);
131 break;
Mike Yubab3daa2018-10-19 22:11:43 +0800132 case DnsTlsTransport::Response::internal_error:
lifrd4d9fbb2019-07-31 20:18:35 +0800133 dnsQueryEvent->set_rcode(NS_R_INTERNAL_ERROR);
Mike Yue655b1d2019-08-28 17:49:59 +0800134 resolv_stats_add(statp->netid, IPSockAddr::toIPSockAddr(server.ss), dnsQueryEvent);
135 break;
Mike Yubab3daa2018-10-19 22:11:43 +0800136 // No "default" statement.
137 }
138 }
139
140 return code;
141}
142
143DnsTlsTransport::Response DnsTlsDispatcher::query(const DnsTlsServer& server, unsigned mark,
144 const Slice query,
145 const Slice ans, int *resplen) {
146 const Key key = std::make_pair(mark, server);
147 Transport* xport;
148 {
149 std::lock_guard guard(sLock);
150 auto it = mStore.find(key);
151 if (it == mStore.end()) {
152 xport = new Transport(server, mark, mFactory.get());
153 mStore[key].reset(xport);
154 } else {
155 xport = it->second.get();
156 }
157 ++xport->useCount;
158 }
159
chenbruceaff85842019-05-31 15:46:42 +0800160 LOG(DEBUG) << "Sending query of length " << query.size();
Mike Yubab3daa2018-10-19 22:11:43 +0800161 auto res = xport->transport.query(query);
chenbruceaff85842019-05-31 15:46:42 +0800162 LOG(DEBUG) << "Awaiting response";
Mike Yubab3daa2018-10-19 22:11:43 +0800163 const auto& result = res.get();
164 DnsTlsTransport::Response code = result.code;
165 if (code == DnsTlsTransport::Response::success) {
166 if (result.response.size() > ans.size()) {
chenbruceaff85842019-05-31 15:46:42 +0800167 LOG(DEBUG) << "Response too large: " << result.response.size() << " > " << ans.size();
Mike Yubab3daa2018-10-19 22:11:43 +0800168 code = DnsTlsTransport::Response::limit_error;
169 } else {
chenbruceaff85842019-05-31 15:46:42 +0800170 LOG(DEBUG) << "Got response successfully";
Mike Yubab3daa2018-10-19 22:11:43 +0800171 *resplen = result.response.size();
172 netdutils::copy(ans, netdutils::makeSlice(result.response));
173 }
174 } else {
chenbruceaff85842019-05-31 15:46:42 +0800175 LOG(DEBUG) << "Query failed: " << (unsigned int)code;
Mike Yubab3daa2018-10-19 22:11:43 +0800176 }
177
178 auto now = std::chrono::steady_clock::now();
179 {
180 std::lock_guard guard(sLock);
181 --xport->useCount;
182 xport->lastUsed = now;
183 cleanup(now);
184 }
185 return code;
186}
187
188// This timeout effectively controls how long to keep SSL session tickets.
189static constexpr std::chrono::minutes IDLE_TIMEOUT(5);
190void DnsTlsDispatcher::cleanup(std::chrono::time_point<std::chrono::steady_clock> now) {
191 // To avoid scanning mStore after every query, return early if a cleanup has been
192 // performed recently.
193 if (now - mLastCleanup < IDLE_TIMEOUT) {
194 return;
195 }
196 for (auto it = mStore.begin(); it != mStore.end();) {
197 auto& s = it->second;
198 if (s->useCount == 0 && now - s->lastUsed > IDLE_TIMEOUT) {
199 it = mStore.erase(it);
200 } else {
201 ++it;
202 }
203 }
204 mLastCleanup = now;
205}
206
207} // end of namespace net
208} // end of namespace android