blob: baafa53d84182731311dc5fdd92254f79968ca0d [file] [log] [blame]
Ben Schwartz66810f62017-10-16 19:27:46 -04001/*
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
Ben Schwartzded1b702017-10-25 14:41:02 -040017#define LOG_TAG "DnsTlsDispatcher"
18//#define LOG_NDEBUG 0
19
Bernie Innocentiad4e26e2019-01-30 11:16:36 +090020#include "DnsTlsDispatcher.h"
Frank Li08ce7b82019-06-27 10:11:52 -070021#include <netdutils/Stopwatch.h>
Bernie Innocentiad4e26e2019-01-30 11:16:36 +090022#include "DnsTlsSocketFactory.h"
Frank Li08ce7b82019-06-27 10:11:52 -070023#include "resolv_private.h"
24#include "stats.pb.h"
Ben Schwartz66810f62017-10-16 19:27:46 -040025
Ben Schwartzded1b702017-10-25 14:41:02 -040026#include "log/log.h"
27
Ben Schwartz66810f62017-10-16 19:27:46 -040028namespace android {
29namespace net {
30
Frank Li08ce7b82019-06-27 10:11:52 -070031using android::netdutils::Stopwatch;
Ben Schwartzded1b702017-10-25 14:41:02 -040032using netdutils::Slice;
33
Ben Schwartz66810f62017-10-16 19:27:46 -040034// static
35std::mutex DnsTlsDispatcher::sLock;
Erik Kline1564d482018-03-07 17:09:35 +090036
Mike Yu5ae61542018-10-19 22:11:43 +080037DnsTlsDispatcher::DnsTlsDispatcher() {
38 mFactory.reset(new DnsTlsSocketFactory());
39}
40
Erik Kline1564d482018-03-07 17:09:35 +090041std::list<DnsTlsServer> DnsTlsDispatcher::getOrderedServerList(
42 const std::list<DnsTlsServer> &tlsServers, unsigned mark) const {
43 // Our preferred DnsTlsServer order is:
44 // 1) reuse existing IPv6 connections
45 // 2) reuse existing IPv4 connections
46 // 3) establish new IPv6 connections
47 // 4) establish new IPv4 connections
48 std::list<DnsTlsServer> existing6;
49 std::list<DnsTlsServer> existing4;
50 std::list<DnsTlsServer> new6;
51 std::list<DnsTlsServer> new4;
52
53 // Pull out any servers for which we might have existing connections and
54 // place them at the from the list of servers to try.
55 {
Bernie Innocentiabf8a342018-08-10 15:17:16 +090056 std::lock_guard guard(sLock);
Erik Kline1564d482018-03-07 17:09:35 +090057
58 for (const auto& tlsServer : tlsServers) {
59 const Key key = std::make_pair(mark, tlsServer);
60 if (mStore.find(key) != mStore.end()) {
61 switch (tlsServer.ss.ss_family) {
62 case AF_INET:
63 existing4.push_back(tlsServer);
64 break;
65 case AF_INET6:
66 existing6.push_back(tlsServer);
67 break;
68 }
69 } else {
70 switch (tlsServer.ss.ss_family) {
71 case AF_INET:
72 new4.push_back(tlsServer);
73 break;
74 case AF_INET6:
75 new6.push_back(tlsServer);
76 break;
77 }
78 }
79 }
80 }
81
82 auto& out = existing6;
83 out.splice(out.cend(), existing4);
84 out.splice(out.cend(), new6);
85 out.splice(out.cend(), new4);
86 return out;
87}
88
Frank Li08ce7b82019-06-27 10:11:52 -070089DnsTlsTransport::Response DnsTlsDispatcher::query(const std::list<DnsTlsServer>& tlsServers,
90 res_state statp, const Slice query,
91 const Slice ans, int* resplen) {
92 const std::list<DnsTlsServer> orderedServers(getOrderedServerList(tlsServers, statp->_mark));
Erik Kline1564d482018-03-07 17:09:35 +090093
94 if (orderedServers.empty()) ALOGW("Empty DnsTlsServer list");
95
96 DnsTlsTransport::Response code = DnsTlsTransport::Response::internal_error;
Frank Li08ce7b82019-06-27 10:11:52 -070097 int serverCount = 0;
Erik Kline1564d482018-03-07 17:09:35 +090098 for (const auto& server : orderedServers) {
Frank Li08ce7b82019-06-27 10:11:52 -070099 DnsQueryEvent* dnsQueryEvent =
100 statp->event->mutable_dns_query_events()->add_dns_query_event();
101 dnsQueryEvent->set_rcode(NS_R_INTERNAL_ERROR);
102 Stopwatch query_stopwatch;
103 code = this->query(server, statp->_mark, query, ans, resplen);
104
105 dnsQueryEvent->set_latency_micros(saturate_cast<int32_t>(query_stopwatch.timeTakenUs()));
106 dnsQueryEvent->set_dns_server_index(serverCount++);
107 dnsQueryEvent->set_ip_version(ipFamilyToIPVersion(server.ss.ss_family));
108 dnsQueryEvent->set_protocol(PROTO_DOT);
109 dnsQueryEvent->set_type(getQueryType(query.base(), query.size()));
110
Erik Kline1564d482018-03-07 17:09:35 +0900111 switch (code) {
112 // These response codes are valid responses and not expected to
113 // change if another server is queried.
114 case DnsTlsTransport::Response::success:
Frank Li08ce7b82019-06-27 10:11:52 -0700115 dnsQueryEvent->set_rcode(
116 static_cast<NsRcode>(reinterpret_cast<HEADER*>(ans.base())->rcode));
117 [[fallthrough]];
Erik Kline1564d482018-03-07 17:09:35 +0900118 case DnsTlsTransport::Response::limit_error:
119 return code;
Erik Kline1564d482018-03-07 17:09:35 +0900120 // These response codes might differ when trying other servers, so
121 // keep iterating to see if we can get a different (better) result.
122 case DnsTlsTransport::Response::network_error:
Frank Li08ce7b82019-06-27 10:11:52 -0700123 // Sync from res_tls_send in res_send.cpp
124 dnsQueryEvent->set_rcode(NS_R_TIMEOUT);
125 [[fallthrough]];
Erik Kline1564d482018-03-07 17:09:35 +0900126 case DnsTlsTransport::Response::internal_error:
127 continue;
Erik Kline1564d482018-03-07 17:09:35 +0900128 // No "default" statement.
129 }
130 }
131
132 return code;
133}
134
Ben Schwartz66810f62017-10-16 19:27:46 -0400135DnsTlsTransport::Response DnsTlsDispatcher::query(const DnsTlsServer& server, unsigned mark,
Ben Schwartzded1b702017-10-25 14:41:02 -0400136 const Slice query,
137 const Slice ans, int *resplen) {
Ben Schwartz66810f62017-10-16 19:27:46 -0400138 const Key key = std::make_pair(mark, server);
139 Transport* xport;
140 {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900141 std::lock_guard guard(sLock);
Ben Schwartzded1b702017-10-25 14:41:02 -0400142 auto it = mStore.find(key);
143 if (it == mStore.end()) {
144 xport = new Transport(server, mark, mFactory.get());
145 mStore[key].reset(xport);
Ben Schwartz66810f62017-10-16 19:27:46 -0400146 } else {
147 xport = it->second.get();
148 }
149 ++xport->useCount;
150 }
151
Ben Schwartzded1b702017-10-25 14:41:02 -0400152 ALOGV("Sending query of length %zu", query.size());
Ben Schwartz33860762017-10-25 14:41:02 -0400153 auto res = xport->transport.query(query);
154 ALOGV("Awaiting response");
155 const auto& result = res.get();
Ben Schwartzded1b702017-10-25 14:41:02 -0400156 DnsTlsTransport::Response code = result.code;
157 if (code == DnsTlsTransport::Response::success) {
158 if (result.response.size() > ans.size()) {
159 ALOGV("Response too large: %zu > %zu", result.response.size(), ans.size());
160 code = DnsTlsTransport::Response::limit_error;
161 } else {
162 ALOGV("Got response successfully");
163 *resplen = result.response.size();
164 netdutils::copy(ans, netdutils::makeSlice(result.response));
165 }
166 } else {
Erik Kline1564d482018-03-07 17:09:35 +0900167 ALOGV("Query failed: %u", (unsigned int) code);
Ben Schwartzded1b702017-10-25 14:41:02 -0400168 }
169
Ben Schwartz66810f62017-10-16 19:27:46 -0400170 auto now = std::chrono::steady_clock::now();
171 {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900172 std::lock_guard guard(sLock);
Ben Schwartz66810f62017-10-16 19:27:46 -0400173 --xport->useCount;
174 xport->lastUsed = now;
175 cleanup(now);
176 }
Ben Schwartzded1b702017-10-25 14:41:02 -0400177 return code;
Ben Schwartz66810f62017-10-16 19:27:46 -0400178}
179
Ben Schwartzded1b702017-10-25 14:41:02 -0400180// This timeout effectively controls how long to keep SSL session tickets.
Ben Schwartz66810f62017-10-16 19:27:46 -0400181static constexpr std::chrono::minutes IDLE_TIMEOUT(5);
Ben Schwartz66810f62017-10-16 19:27:46 -0400182void DnsTlsDispatcher::cleanup(std::chrono::time_point<std::chrono::steady_clock> now) {
Ben Schwartzded1b702017-10-25 14:41:02 -0400183 // To avoid scanning mStore after every query, return early if a cleanup has been
184 // performed recently.
185 if (now - mLastCleanup < IDLE_TIMEOUT) {
Ben Schwartz66810f62017-10-16 19:27:46 -0400186 return;
187 }
Ben Schwartzded1b702017-10-25 14:41:02 -0400188 for (auto it = mStore.begin(); it != mStore.end();) {
Ben Schwartz66810f62017-10-16 19:27:46 -0400189 auto& s = it->second;
190 if (s->useCount == 0 && now - s->lastUsed > IDLE_TIMEOUT) {
Ben Schwartzded1b702017-10-25 14:41:02 -0400191 it = mStore.erase(it);
Ben Schwartz66810f62017-10-16 19:27:46 -0400192 } else {
193 ++it;
194 }
195 }
Ben Schwartzded1b702017-10-25 14:41:02 -0400196 mLastCleanup = now;
Ben Schwartz66810f62017-10-16 19:27:46 -0400197}
198
Ben Schwartzded1b702017-10-25 14:41:02 -0400199} // end of namespace net
200} // end of namespace android