blob: 6c2a76b56af603bbd94cd5990f8f03537c9c3c65 [file] [log] [blame]
Mike Yub601ff72018-11-01 20:07:00 +08001/*
2 * Copyright (C) 2018 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
Mike Yu3e829062019-08-07 14:01:14 +080017#pragma once
Mike Yub601ff72018-11-01 20:07:00 +080018
Luke Huang2fe9c732021-07-06 01:48:02 +080019#include <array>
Mike Yub601ff72018-11-01 20:07:00 +080020#include <list>
21#include <map>
Mike Yu303b0df2018-12-24 17:05:02 +080022#include <mutex>
Mike Yub601ff72018-11-01 20:07:00 +080023#include <vector>
24
Luke Huang2fe9c732021-07-06 01:48:02 +080025#include <android-base/format.h>
26#include <android-base/logging.h>
Mike Yu82ae84b2020-12-02 21:04:40 +080027#include <android-base/result.h>
Mike Yub601ff72018-11-01 20:07:00 +080028#include <android-base/thread_annotations.h>
Mike Yu8058bd02021-05-13 16:44:18 +080029#include <netdutils/BackoffSequence.h>
Mike Yu3d5130d2020-12-21 17:57:18 +080030#include <netdutils/DumpWriter.h>
Mike Yufa985f72020-11-23 20:24:21 +080031#include <netdutils/InternetAddresses.h>
Luke Huang2fe9c732021-07-06 01:48:02 +080032#include <netdutils/Slice.h>
Mike Yub601ff72018-11-01 20:07:00 +080033
34#include "DnsTlsServer.h"
Mike Yu3d5130d2020-12-21 17:57:18 +080035#include "LockedQueue.h"
Mike Yua6853e82020-12-11 19:47:06 +080036#include "PrivateDnsValidationObserver.h"
Luke Huang2fe9c732021-07-06 01:48:02 +080037#include "doh.h"
Bernie Innocentiec4219b2019-01-30 11:16:36 +090038
Mike Yub601ff72018-11-01 20:07:00 +080039namespace android {
40namespace net {
41
Mike Yuad96ef82021-02-20 18:35:14 +080042// TODO: decouple the dependency of DnsTlsServer.
Mike Yub601ff72018-11-01 20:07:00 +080043struct PrivateDnsStatus {
44 PrivateDnsMode mode;
Mike Yufa985f72020-11-23 20:24:21 +080045
46 // TODO: change the type to std::vector<DnsTlsServer>.
Mike Yu3e829062019-08-07 14:01:14 +080047 std::map<DnsTlsServer, Validation, AddressComparator> serversMap;
Mike Yub601ff72018-11-01 20:07:00 +080048
Mike Yu3e829062019-08-07 14:01:14 +080049 std::list<DnsTlsServer> validatedServers() const {
50 std::list<DnsTlsServer> servers;
51
52 for (const auto& pair : serversMap) {
53 if (pair.second == Validation::success) {
54 servers.push_back(pair.first);
55 }
56 }
57 return servers;
58 }
Bernie Innocenti23c6e2a2019-05-16 15:18:35 +090059};
60
Mike Yub601ff72018-11-01 20:07:00 +080061class PrivateDnsConfiguration {
62 public:
Mike Yufa985f72020-11-23 20:24:21 +080063 struct ServerIdentity {
Mike Yu453b5e42021-02-19 20:03:07 +080064 const netdutils::IPSockAddr sockaddr;
65 const std::string provider;
Mike Yufa985f72020-11-23 20:24:21 +080066
Mike Yucf56d232021-04-29 19:37:25 +080067 explicit ServerIdentity(const IPrivateDnsServer& server)
68 : sockaddr(server.addr()), provider(server.provider()) {}
Luke Huang2fe9c732021-07-06 01:48:02 +080069 ServerIdentity(const netdutils::IPSockAddr& addr, const std::string& host)
70 : sockaddr(addr), provider(host) {}
Mike Yufa985f72020-11-23 20:24:21 +080071
72 bool operator<(const ServerIdentity& other) const {
Mike Yu453b5e42021-02-19 20:03:07 +080073 return std::tie(sockaddr, provider) < std::tie(other.sockaddr, other.provider);
Mike Yufa985f72020-11-23 20:24:21 +080074 }
75 bool operator==(const ServerIdentity& other) const {
Mike Yu453b5e42021-02-19 20:03:07 +080076 return std::tie(sockaddr, provider) == std::tie(other.sockaddr, other.provider);
Mike Yufa985f72020-11-23 20:24:21 +080077 }
78 };
79
Mike Yuad96ef82021-02-20 18:35:14 +080080 // The only instance of PrivateDnsConfiguration.
81 static PrivateDnsConfiguration& getInstance() {
82 static PrivateDnsConfiguration instance;
83 return instance;
84 }
85
86 int set(int32_t netId, uint32_t mark, const std::vector<std::string>& servers,
87 const std::string& name, const std::string& caCert) EXCLUDES(mPrivateDnsLock);
88
Luke Huang2fe9c732021-07-06 01:48:02 +080089 void initDoh() EXCLUDES(mPrivateDnsLock);
90
91 int setDoh(int32_t netId, uint32_t mark, const std::vector<std::string>& servers,
92 const std::string& name, const std::string& caCert) EXCLUDES(mPrivateDnsLock);
93
Mike Yuad96ef82021-02-20 18:35:14 +080094 PrivateDnsStatus getStatus(unsigned netId) const EXCLUDES(mPrivateDnsLock);
95
96 void clear(unsigned netId) EXCLUDES(mPrivateDnsLock);
97
Luke Huang2fe9c732021-07-06 01:48:02 +080098 void clearDoh(unsigned netId) EXCLUDES(mPrivateDnsLock);
99
100 ssize_t dohQuery(unsigned netId, const netdutils::Slice query, const netdutils::Slice answer,
101 uint64_t timeoutMs) EXCLUDES(mPrivateDnsLock);
102
Mike Yuad96ef82021-02-20 18:35:14 +0800103 // Request the server to be revalidated on a connection tagged with |mark|.
104 // Returns a Result to indicate if the request is accepted.
105 base::Result<void> requestValidation(unsigned netId, const ServerIdentity& identity,
106 uint32_t mark) EXCLUDES(mPrivateDnsLock);
107
Mike Yu5448c9e2020-12-14 16:45:16 +0800108 void setObserver(PrivateDnsValidationObserver* observer);
109
Mike Yu3d5130d2020-12-21 17:57:18 +0800110 void dump(netdutils::DumpWriter& dw) const;
111
Luke Huang2fe9c732021-07-06 01:48:02 +0800112 void onDohStatusUpdate(uint32_t netId, bool success, const char* ipAddr, const char* host)
113 EXCLUDES(mPrivateDnsLock);
114
Mike Yub601ff72018-11-01 20:07:00 +0800115 private:
Mike Yucf56d232021-04-29 19:37:25 +0800116 typedef std::map<ServerIdentity, std::unique_ptr<IPrivateDnsServer>> PrivateDnsTracker;
Mike Yub601ff72018-11-01 20:07:00 +0800117
Mike Yu60248242020-07-29 16:45:26 +0800118 PrivateDnsConfiguration() = default;
119
Mike Yu82ae84b2020-12-02 21:04:40 +0800120 // Launchs a thread to run the validation for |server| on the network |netId|.
121 // |isRevalidation| is true if this call is due to a revalidation request.
Mike Yuad96ef82021-02-20 18:35:14 +0800122 void startValidation(const ServerIdentity& identity, unsigned netId, bool isRevalidation)
Mike Yu82ae84b2020-12-02 21:04:40 +0800123 REQUIRES(mPrivateDnsLock);
Mike Yub601ff72018-11-01 20:07:00 +0800124
Mike Yuad96ef82021-02-20 18:35:14 +0800125 bool recordPrivateDnsValidation(const ServerIdentity& identity, unsigned netId, bool success,
Mike Yu5daa40d2021-06-10 21:34:32 +0800126 bool isRevalidation) EXCLUDES(mPrivateDnsLock);
Mike Yub601ff72018-11-01 20:07:00 +0800127
Luke Huang2fe9c732021-07-06 01:48:02 +0800128 void sendPrivateDnsValidationEvent(const ServerIdentity& identity, unsigned netId,
129 bool success) const REQUIRES(mPrivateDnsLock);
paulhu0664f692020-12-14 16:48:26 +0800130
Mike Yuf7717f52020-11-24 17:31:12 +0800131 // Decide if a validation for |server| is needed. Note that servers that have failed
Mike Yub601ff72018-11-01 20:07:00 +0800132 // multiple validation attempts but for which there is still a validating
133 // thread running are marked as being Validation::in_process.
Mike Yucf56d232021-04-29 19:37:25 +0800134 bool needsValidation(const IPrivateDnsServer& server) const REQUIRES(mPrivateDnsLock);
Mike Yu3334a5e2020-11-19 13:33:17 +0800135
Mike Yufa985f72020-11-23 20:24:21 +0800136 void updateServerState(const ServerIdentity& identity, Validation state, uint32_t netId)
Mike Yu3334a5e2020-11-19 13:33:17 +0800137 REQUIRES(mPrivateDnsLock);
Mike Yub601ff72018-11-01 20:07:00 +0800138
Mike Yuad96ef82021-02-20 18:35:14 +0800139 // For testing.
Mike Yucf56d232021-04-29 19:37:25 +0800140 base::Result<IPrivateDnsServer*> getPrivateDns(const ServerIdentity& identity, unsigned netId)
Mike Yuad96ef82021-02-20 18:35:14 +0800141 EXCLUDES(mPrivateDnsLock);
142
Mike Yucf56d232021-04-29 19:37:25 +0800143 base::Result<IPrivateDnsServer*> getPrivateDnsLocked(const ServerIdentity& identity,
144 unsigned netId) REQUIRES(mPrivateDnsLock);
Mike Yuad96ef82021-02-20 18:35:14 +0800145
Luke Huang2fe9c732021-07-06 01:48:02 +0800146 void initDohLocked() REQUIRES(mPrivateDnsLock);
147
Mike Yu82ae84b2020-12-02 21:04:40 +0800148 mutable std::mutex mPrivateDnsLock;
Mike Yub601ff72018-11-01 20:07:00 +0800149 std::map<unsigned, PrivateDnsMode> mPrivateDnsModes GUARDED_BY(mPrivateDnsLock);
Mike Yuf7717f52020-11-24 17:31:12 +0800150
151 // Contains all servers for a network, along with their current validation status.
152 // In case a server is removed due to a configuration change, it remains in this map,
153 // but is marked inactive.
154 // Any pending validation threads will continue running because we have no way to cancel them.
Mike Yub601ff72018-11-01 20:07:00 +0800155 std::map<unsigned, PrivateDnsTracker> mPrivateDnsTransports GUARDED_BY(mPrivateDnsLock);
Mike Yu0ee1dc92020-11-09 14:56:54 +0800156
Mike Yu453b5e42021-02-19 20:03:07 +0800157 void notifyValidationStateUpdate(const netdutils::IPSockAddr& sockaddr, Validation validation,
Mike Yu74770542020-12-15 14:25:21 +0800158 uint32_t netId) const REQUIRES(mPrivateDnsLock);
Mike Yu0ee1dc92020-11-09 14:56:54 +0800159
Luke Huang2fe9c732021-07-06 01:48:02 +0800160 bool needReportEvent(uint32_t netId, ServerIdentity identity, bool success) const
161 REQUIRES(mPrivateDnsLock);
162
Mike Yua6853e82020-12-11 19:47:06 +0800163 // TODO: fix the reentrancy problem.
164 PrivateDnsValidationObserver* mObserver GUARDED_BY(mPrivateDnsLock);
Mike Yu0ee1dc92020-11-09 14:56:54 +0800165
Luke Huang2fe9c732021-07-06 01:48:02 +0800166 DohDispatcher* mDohDispatcher;
167
Mike Yu0ee1dc92020-11-09 14:56:54 +0800168 friend class PrivateDnsConfigurationTest;
Mike Yu3d5130d2020-12-21 17:57:18 +0800169
Mike Yu8058bd02021-05-13 16:44:18 +0800170 // It's not const because PrivateDnsConfigurationTest needs to override it.
171 // TODO: make it const by dependency injection.
172 netdutils::BackoffSequence<>::Builder mBackoffBuilder =
173 netdutils::BackoffSequence<>::Builder()
174 .withInitialRetransmissionTime(std::chrono::seconds(60))
175 .withMaximumRetransmissionTime(std::chrono::seconds(3600));
176
Luke Huang2fe9c732021-07-06 01:48:02 +0800177 struct DohIdentity {
178 std::string httpsTemplate;
179 std::string ipAddr;
180 std::string host;
181 Validation status;
182 bool operator<(const DohIdentity& other) const {
183 return std::tie(ipAddr, host) < std::tie(other.ipAddr, other.host);
184 }
185 bool operator==(const DohIdentity& other) const {
186 return std::tie(ipAddr, host) == std::tie(other.ipAddr, other.host);
187 }
188 bool operator<(const ServerIdentity& other) const {
189 std::string otherIp = other.sockaddr.ip().toString();
190 return std::tie(ipAddr, host) < std::tie(otherIp, other.provider);
191 }
192 bool operator==(const ServerIdentity& other) const {
193 std::string otherIp = other.sockaddr.ip().toString();
194 return std::tie(ipAddr, host) == std::tie(otherIp, other.provider);
195 }
196 };
197
198 struct DohProviderEntry {
199 std::string provider;
200 std::set<std::string> ips;
201 std::string host;
202 std::string httpsTemplate;
Mike Yu5e406a32021-07-06 21:01:17 +0800203 bool forTesting;
Luke Huang2fe9c732021-07-06 01:48:02 +0800204 base::Result<DohIdentity> getDohIdentity(const std::vector<std::string>& ips,
205 const std::string& host) const {
206 if (!host.empty() && this->host != host) return Errorf("host {} not matched", host);
207 for (const auto& ip : ips) {
208 if (this->ips.find(ip) == this->ips.end()) continue;
209 LOG(INFO) << fmt::format("getDohIdentity: {} {}", ip, host);
210 // Only pick the first one for now.
211 return DohIdentity{httpsTemplate, ip, host, Validation::in_process};
212 }
213 return Errorf("server not matched");
214 };
215 };
216
217 // TODO: Move below DoH relevant stuff into Rust implementation.
218 std::map<unsigned, DohIdentity> mDohTracker GUARDED_BY(mPrivateDnsLock);
Mike Yu5e406a32021-07-06 21:01:17 +0800219 std::array<DohProviderEntry, 3> mAvailableDoHProviders = {{
Luke Huang2fe9c732021-07-06 01:48:02 +0800220 {"Google",
221 {"2001:4860:4860::8888", "2001:4860:4860::8844", "8.8.8.8", "8.8.4.4"},
222 "dns.google",
Mike Yu5e406a32021-07-06 21:01:17 +0800223 "https://dns.google/dns-query",
224 false},
Luke Huang2fe9c732021-07-06 01:48:02 +0800225 {"Cloudflare",
226 {"2606:4700::6810:f8f9", "2606:4700::6810:f9f9", "104.16.248.249", "104.16.249.249"},
227 "cloudflare-dns.com",
Mike Yu5e406a32021-07-06 21:01:17 +0800228 "https://cloudflare-dns.com/dns-query",
229 false},
230
231 // The DoH provider for testing.
232 {"ResolverTestProvider",
233 {"127.0.0.3", "::1"},
234 "example.com",
235 "https://example.com/dns-query",
236 true},
Luke Huang2fe9c732021-07-06 01:48:02 +0800237 }};
238
Mike Yu3d5130d2020-12-21 17:57:18 +0800239 struct RecordEntry {
240 RecordEntry(uint32_t netId, const ServerIdentity& identity, Validation state)
241 : netId(netId), serverIdentity(identity), state(state) {}
242
243 const uint32_t netId;
244 const ServerIdentity serverIdentity;
245 const Validation state;
246 const std::chrono::system_clock::time_point timestamp = std::chrono::system_clock::now();
247 };
248
249 LockedRingBuffer<RecordEntry> mPrivateDnsLog{100};
Mike Yub601ff72018-11-01 20:07:00 +0800250};
251
Mike Yub601ff72018-11-01 20:07:00 +0800252} // namespace net
253} // namespace android