| Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 1 | /* |
| 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 Yu | 3e82906 | 2019-08-07 14:01:14 +0800 | [diff] [blame] | 17 | #pragma once |
| Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 18 | |
| Luke Huang | 2fe9c73 | 2021-07-06 01:48:02 +0800 | [diff] [blame] | 19 | #include <array> |
| Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 20 | #include <list> |
| 21 | #include <map> |
| Mike Yu | 303b0df | 2018-12-24 17:05:02 +0800 | [diff] [blame] | 22 | #include <mutex> |
| Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 23 | #include <vector> |
| 24 | |
| Luke Huang | 2fe9c73 | 2021-07-06 01:48:02 +0800 | [diff] [blame] | 25 | #include <android-base/format.h> |
| 26 | #include <android-base/logging.h> |
| Mike Yu | 82ae84b | 2020-12-02 21:04:40 +0800 | [diff] [blame] | 27 | #include <android-base/result.h> |
| Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 28 | #include <android-base/thread_annotations.h> |
| Mike Yu | 8058bd0 | 2021-05-13 16:44:18 +0800 | [diff] [blame] | 29 | #include <netdutils/BackoffSequence.h> |
| Mike Yu | 3d5130d | 2020-12-21 17:57:18 +0800 | [diff] [blame] | 30 | #include <netdutils/DumpWriter.h> |
| Mike Yu | fa985f7 | 2020-11-23 20:24:21 +0800 | [diff] [blame] | 31 | #include <netdutils/InternetAddresses.h> |
| Luke Huang | 2fe9c73 | 2021-07-06 01:48:02 +0800 | [diff] [blame] | 32 | #include <netdutils/Slice.h> |
| Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 33 | |
| 34 | #include "DnsTlsServer.h" |
| Mike Yu | 3d5130d | 2020-12-21 17:57:18 +0800 | [diff] [blame] | 35 | #include "LockedQueue.h" |
| Mike Yu | a6853e8 | 2020-12-11 19:47:06 +0800 | [diff] [blame] | 36 | #include "PrivateDnsValidationObserver.h" |
| Luke Huang | 2fe9c73 | 2021-07-06 01:48:02 +0800 | [diff] [blame] | 37 | #include "doh.h" |
| Bernie Innocenti | ec4219b | 2019-01-30 11:16:36 +0900 | [diff] [blame] | 38 | |
| Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 39 | namespace android { |
| 40 | namespace net { |
| 41 | |
| Mike Yu | ad96ef8 | 2021-02-20 18:35:14 +0800 | [diff] [blame] | 42 | // TODO: decouple the dependency of DnsTlsServer. |
| Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 43 | struct PrivateDnsStatus { |
| 44 | PrivateDnsMode mode; |
| Mike Yu | fa985f7 | 2020-11-23 20:24:21 +0800 | [diff] [blame] | 45 | |
| 46 | // TODO: change the type to std::vector<DnsTlsServer>. |
| Mike Yu | 3e82906 | 2019-08-07 14:01:14 +0800 | [diff] [blame] | 47 | std::map<DnsTlsServer, Validation, AddressComparator> serversMap; |
| Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 48 | |
| Mike Yu | 3e82906 | 2019-08-07 14:01:14 +0800 | [diff] [blame] | 49 | 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 Innocenti | 23c6e2a | 2019-05-16 15:18:35 +0900 | [diff] [blame] | 59 | }; |
| 60 | |
| Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 61 | class PrivateDnsConfiguration { |
| 62 | public: |
| Mike Yu | fa985f7 | 2020-11-23 20:24:21 +0800 | [diff] [blame] | 63 | struct ServerIdentity { |
| Mike Yu | 453b5e4 | 2021-02-19 20:03:07 +0800 | [diff] [blame] | 64 | const netdutils::IPSockAddr sockaddr; |
| 65 | const std::string provider; |
| Mike Yu | fa985f7 | 2020-11-23 20:24:21 +0800 | [diff] [blame] | 66 | |
| Mike Yu | cf56d23 | 2021-04-29 19:37:25 +0800 | [diff] [blame] | 67 | explicit ServerIdentity(const IPrivateDnsServer& server) |
| 68 | : sockaddr(server.addr()), provider(server.provider()) {} |
| Luke Huang | 2fe9c73 | 2021-07-06 01:48:02 +0800 | [diff] [blame] | 69 | ServerIdentity(const netdutils::IPSockAddr& addr, const std::string& host) |
| 70 | : sockaddr(addr), provider(host) {} |
| Mike Yu | fa985f7 | 2020-11-23 20:24:21 +0800 | [diff] [blame] | 71 | |
| 72 | bool operator<(const ServerIdentity& other) const { |
| Mike Yu | 453b5e4 | 2021-02-19 20:03:07 +0800 | [diff] [blame] | 73 | return std::tie(sockaddr, provider) < std::tie(other.sockaddr, other.provider); |
| Mike Yu | fa985f7 | 2020-11-23 20:24:21 +0800 | [diff] [blame] | 74 | } |
| 75 | bool operator==(const ServerIdentity& other) const { |
| Mike Yu | 453b5e4 | 2021-02-19 20:03:07 +0800 | [diff] [blame] | 76 | return std::tie(sockaddr, provider) == std::tie(other.sockaddr, other.provider); |
| Mike Yu | fa985f7 | 2020-11-23 20:24:21 +0800 | [diff] [blame] | 77 | } |
| 78 | }; |
| 79 | |
| Mike Yu | ad96ef8 | 2021-02-20 18:35:14 +0800 | [diff] [blame] | 80 | // 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 Huang | 2fe9c73 | 2021-07-06 01:48:02 +0800 | [diff] [blame] | 89 | 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 Yu | ad96ef8 | 2021-02-20 18:35:14 +0800 | [diff] [blame] | 94 | PrivateDnsStatus getStatus(unsigned netId) const EXCLUDES(mPrivateDnsLock); |
| 95 | |
| 96 | void clear(unsigned netId) EXCLUDES(mPrivateDnsLock); |
| 97 | |
| Luke Huang | 2fe9c73 | 2021-07-06 01:48:02 +0800 | [diff] [blame] | 98 | 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 Yu | ad96ef8 | 2021-02-20 18:35:14 +0800 | [diff] [blame] | 103 | // 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 Yu | 5448c9e | 2020-12-14 16:45:16 +0800 | [diff] [blame] | 108 | void setObserver(PrivateDnsValidationObserver* observer); |
| 109 | |
| Mike Yu | 3d5130d | 2020-12-21 17:57:18 +0800 | [diff] [blame] | 110 | void dump(netdutils::DumpWriter& dw) const; |
| 111 | |
| Luke Huang | 2fe9c73 | 2021-07-06 01:48:02 +0800 | [diff] [blame] | 112 | void onDohStatusUpdate(uint32_t netId, bool success, const char* ipAddr, const char* host) |
| 113 | EXCLUDES(mPrivateDnsLock); |
| 114 | |
| Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 115 | private: |
| Mike Yu | cf56d23 | 2021-04-29 19:37:25 +0800 | [diff] [blame] | 116 | typedef std::map<ServerIdentity, std::unique_ptr<IPrivateDnsServer>> PrivateDnsTracker; |
| Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 117 | |
| Mike Yu | 6024824 | 2020-07-29 16:45:26 +0800 | [diff] [blame] | 118 | PrivateDnsConfiguration() = default; |
| 119 | |
| Mike Yu | 82ae84b | 2020-12-02 21:04:40 +0800 | [diff] [blame] | 120 | // 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 Yu | ad96ef8 | 2021-02-20 18:35:14 +0800 | [diff] [blame] | 122 | void startValidation(const ServerIdentity& identity, unsigned netId, bool isRevalidation) |
| Mike Yu | 82ae84b | 2020-12-02 21:04:40 +0800 | [diff] [blame] | 123 | REQUIRES(mPrivateDnsLock); |
| Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 124 | |
| Mike Yu | ad96ef8 | 2021-02-20 18:35:14 +0800 | [diff] [blame] | 125 | bool recordPrivateDnsValidation(const ServerIdentity& identity, unsigned netId, bool success, |
| Mike Yu | 5daa40d | 2021-06-10 21:34:32 +0800 | [diff] [blame] | 126 | bool isRevalidation) EXCLUDES(mPrivateDnsLock); |
| Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 127 | |
| Luke Huang | 2fe9c73 | 2021-07-06 01:48:02 +0800 | [diff] [blame] | 128 | void sendPrivateDnsValidationEvent(const ServerIdentity& identity, unsigned netId, |
| 129 | bool success) const REQUIRES(mPrivateDnsLock); |
| paulhu | 0664f69 | 2020-12-14 16:48:26 +0800 | [diff] [blame] | 130 | |
| Mike Yu | f7717f5 | 2020-11-24 17:31:12 +0800 | [diff] [blame] | 131 | // Decide if a validation for |server| is needed. Note that servers that have failed |
| Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 132 | // multiple validation attempts but for which there is still a validating |
| 133 | // thread running are marked as being Validation::in_process. |
| Mike Yu | cf56d23 | 2021-04-29 19:37:25 +0800 | [diff] [blame] | 134 | bool needsValidation(const IPrivateDnsServer& server) const REQUIRES(mPrivateDnsLock); |
| Mike Yu | 3334a5e | 2020-11-19 13:33:17 +0800 | [diff] [blame] | 135 | |
| Mike Yu | fa985f7 | 2020-11-23 20:24:21 +0800 | [diff] [blame] | 136 | void updateServerState(const ServerIdentity& identity, Validation state, uint32_t netId) |
| Mike Yu | 3334a5e | 2020-11-19 13:33:17 +0800 | [diff] [blame] | 137 | REQUIRES(mPrivateDnsLock); |
| Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 138 | |
| Mike Yu | ad96ef8 | 2021-02-20 18:35:14 +0800 | [diff] [blame] | 139 | // For testing. |
| Mike Yu | cf56d23 | 2021-04-29 19:37:25 +0800 | [diff] [blame] | 140 | base::Result<IPrivateDnsServer*> getPrivateDns(const ServerIdentity& identity, unsigned netId) |
| Mike Yu | ad96ef8 | 2021-02-20 18:35:14 +0800 | [diff] [blame] | 141 | EXCLUDES(mPrivateDnsLock); |
| 142 | |
| Mike Yu | cf56d23 | 2021-04-29 19:37:25 +0800 | [diff] [blame] | 143 | base::Result<IPrivateDnsServer*> getPrivateDnsLocked(const ServerIdentity& identity, |
| 144 | unsigned netId) REQUIRES(mPrivateDnsLock); |
| Mike Yu | ad96ef8 | 2021-02-20 18:35:14 +0800 | [diff] [blame] | 145 | |
| Luke Huang | 2fe9c73 | 2021-07-06 01:48:02 +0800 | [diff] [blame] | 146 | void initDohLocked() REQUIRES(mPrivateDnsLock); |
| 147 | |
| Mike Yu | 82ae84b | 2020-12-02 21:04:40 +0800 | [diff] [blame] | 148 | mutable std::mutex mPrivateDnsLock; |
| Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 149 | std::map<unsigned, PrivateDnsMode> mPrivateDnsModes GUARDED_BY(mPrivateDnsLock); |
| Mike Yu | f7717f5 | 2020-11-24 17:31:12 +0800 | [diff] [blame] | 150 | |
| 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 Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 155 | std::map<unsigned, PrivateDnsTracker> mPrivateDnsTransports GUARDED_BY(mPrivateDnsLock); |
| Mike Yu | 0ee1dc9 | 2020-11-09 14:56:54 +0800 | [diff] [blame] | 156 | |
| Mike Yu | 453b5e4 | 2021-02-19 20:03:07 +0800 | [diff] [blame] | 157 | void notifyValidationStateUpdate(const netdutils::IPSockAddr& sockaddr, Validation validation, |
| Mike Yu | 7477054 | 2020-12-15 14:25:21 +0800 | [diff] [blame] | 158 | uint32_t netId) const REQUIRES(mPrivateDnsLock); |
| Mike Yu | 0ee1dc9 | 2020-11-09 14:56:54 +0800 | [diff] [blame] | 159 | |
| Luke Huang | 2fe9c73 | 2021-07-06 01:48:02 +0800 | [diff] [blame] | 160 | bool needReportEvent(uint32_t netId, ServerIdentity identity, bool success) const |
| 161 | REQUIRES(mPrivateDnsLock); |
| 162 | |
| Mike Yu | a6853e8 | 2020-12-11 19:47:06 +0800 | [diff] [blame] | 163 | // TODO: fix the reentrancy problem. |
| 164 | PrivateDnsValidationObserver* mObserver GUARDED_BY(mPrivateDnsLock); |
| Mike Yu | 0ee1dc9 | 2020-11-09 14:56:54 +0800 | [diff] [blame] | 165 | |
| Luke Huang | 2fe9c73 | 2021-07-06 01:48:02 +0800 | [diff] [blame] | 166 | DohDispatcher* mDohDispatcher; |
| 167 | |
| Mike Yu | 0ee1dc9 | 2020-11-09 14:56:54 +0800 | [diff] [blame] | 168 | friend class PrivateDnsConfigurationTest; |
| Mike Yu | 3d5130d | 2020-12-21 17:57:18 +0800 | [diff] [blame] | 169 | |
| Mike Yu | 8058bd0 | 2021-05-13 16:44:18 +0800 | [diff] [blame] | 170 | // 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 Huang | 2fe9c73 | 2021-07-06 01:48:02 +0800 | [diff] [blame] | 177 | 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 Yu | 5e406a3 | 2021-07-06 21:01:17 +0800 | [diff] [blame] | 203 | bool forTesting; |
| Luke Huang | 2fe9c73 | 2021-07-06 01:48:02 +0800 | [diff] [blame] | 204 | 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 Yu | 5e406a3 | 2021-07-06 21:01:17 +0800 | [diff] [blame] | 219 | std::array<DohProviderEntry, 3> mAvailableDoHProviders = {{ |
| Luke Huang | 2fe9c73 | 2021-07-06 01:48:02 +0800 | [diff] [blame] | 220 | {"Google", |
| 221 | {"2001:4860:4860::8888", "2001:4860:4860::8844", "8.8.8.8", "8.8.4.4"}, |
| 222 | "dns.google", |
| Mike Yu | 5e406a3 | 2021-07-06 21:01:17 +0800 | [diff] [blame] | 223 | "https://dns.google/dns-query", |
| 224 | false}, |
| Luke Huang | 2fe9c73 | 2021-07-06 01:48:02 +0800 | [diff] [blame] | 225 | {"Cloudflare", |
| 226 | {"2606:4700::6810:f8f9", "2606:4700::6810:f9f9", "104.16.248.249", "104.16.249.249"}, |
| 227 | "cloudflare-dns.com", |
| Mike Yu | 5e406a3 | 2021-07-06 21:01:17 +0800 | [diff] [blame] | 228 | "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 Huang | 2fe9c73 | 2021-07-06 01:48:02 +0800 | [diff] [blame] | 237 | }}; |
| 238 | |
| Mike Yu | 3d5130d | 2020-12-21 17:57:18 +0800 | [diff] [blame] | 239 | 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 Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 250 | }; |
| 251 | |
| Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 252 | } // namespace net |
| 253 | } // namespace android |