blob: 3d400408c4657d4816634cd029b17d39a91a3f56 [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
19#include <list>
20#include <map>
Mike Yu303b0df2018-12-24 17:05:02 +080021#include <mutex>
Mike Yub601ff72018-11-01 20:07:00 +080022#include <vector>
23
Mike Yu82ae84b2020-12-02 21:04:40 +080024#include <android-base/result.h>
Mike Yub601ff72018-11-01 20:07:00 +080025#include <android-base/thread_annotations.h>
Mike Yu8058bd02021-05-13 16:44:18 +080026#include <netdutils/BackoffSequence.h>
Mike Yu3d5130d2020-12-21 17:57:18 +080027#include <netdutils/DumpWriter.h>
Mike Yufa985f72020-11-23 20:24:21 +080028#include <netdutils/InternetAddresses.h>
Mike Yub601ff72018-11-01 20:07:00 +080029
30#include "DnsTlsServer.h"
Mike Yu3d5130d2020-12-21 17:57:18 +080031#include "LockedQueue.h"
Mike Yua6853e82020-12-11 19:47:06 +080032#include "PrivateDnsValidationObserver.h"
Bernie Innocentiec4219b2019-01-30 11:16:36 +090033
Mike Yub601ff72018-11-01 20:07:00 +080034namespace android {
35namespace net {
36
Mike Yuad96ef82021-02-20 18:35:14 +080037// TODO: decouple the dependency of DnsTlsServer.
Mike Yub601ff72018-11-01 20:07:00 +080038struct PrivateDnsStatus {
39 PrivateDnsMode mode;
Mike Yufa985f72020-11-23 20:24:21 +080040
41 // TODO: change the type to std::vector<DnsTlsServer>.
Mike Yu3e829062019-08-07 14:01:14 +080042 std::map<DnsTlsServer, Validation, AddressComparator> serversMap;
Mike Yub601ff72018-11-01 20:07:00 +080043
Mike Yu3e829062019-08-07 14:01:14 +080044 std::list<DnsTlsServer> validatedServers() const {
45 std::list<DnsTlsServer> servers;
46
47 for (const auto& pair : serversMap) {
48 if (pair.second == Validation::success) {
49 servers.push_back(pair.first);
50 }
51 }
52 return servers;
53 }
Bernie Innocenti23c6e2a2019-05-16 15:18:35 +090054};
55
Mike Yub601ff72018-11-01 20:07:00 +080056class PrivateDnsConfiguration {
57 public:
Mike Yufa985f72020-11-23 20:24:21 +080058 struct ServerIdentity {
Mike Yu453b5e42021-02-19 20:03:07 +080059 const netdutils::IPSockAddr sockaddr;
60 const std::string provider;
Mike Yufa985f72020-11-23 20:24:21 +080061
Mike Yucf56d232021-04-29 19:37:25 +080062 explicit ServerIdentity(const IPrivateDnsServer& server)
63 : sockaddr(server.addr()), provider(server.provider()) {}
Mike Yufa985f72020-11-23 20:24:21 +080064
65 bool operator<(const ServerIdentity& other) const {
Mike Yu453b5e42021-02-19 20:03:07 +080066 return std::tie(sockaddr, provider) < std::tie(other.sockaddr, other.provider);
Mike Yufa985f72020-11-23 20:24:21 +080067 }
68 bool operator==(const ServerIdentity& other) const {
Mike Yu453b5e42021-02-19 20:03:07 +080069 return std::tie(sockaddr, provider) == std::tie(other.sockaddr, other.provider);
Mike Yufa985f72020-11-23 20:24:21 +080070 }
71 };
72
Mike Yuad96ef82021-02-20 18:35:14 +080073 // The only instance of PrivateDnsConfiguration.
74 static PrivateDnsConfiguration& getInstance() {
75 static PrivateDnsConfiguration instance;
76 return instance;
77 }
78
79 int set(int32_t netId, uint32_t mark, const std::vector<std::string>& servers,
80 const std::string& name, const std::string& caCert) EXCLUDES(mPrivateDnsLock);
81
82 PrivateDnsStatus getStatus(unsigned netId) const EXCLUDES(mPrivateDnsLock);
83
84 void clear(unsigned netId) EXCLUDES(mPrivateDnsLock);
85
86 // Request the server to be revalidated on a connection tagged with |mark|.
87 // Returns a Result to indicate if the request is accepted.
88 base::Result<void> requestValidation(unsigned netId, const ServerIdentity& identity,
89 uint32_t mark) EXCLUDES(mPrivateDnsLock);
90
Mike Yu5448c9e2020-12-14 16:45:16 +080091 void setObserver(PrivateDnsValidationObserver* observer);
92
Mike Yu3d5130d2020-12-21 17:57:18 +080093 void dump(netdutils::DumpWriter& dw) const;
94
Mike Yub601ff72018-11-01 20:07:00 +080095 private:
Mike Yucf56d232021-04-29 19:37:25 +080096 typedef std::map<ServerIdentity, std::unique_ptr<IPrivateDnsServer>> PrivateDnsTracker;
Mike Yub601ff72018-11-01 20:07:00 +080097
Mike Yu1aede812021-05-11 14:49:30 +080098 static constexpr int kMaxPrivateDnsLatencyThresholdMs = 2000;
99 static constexpr int kMinPrivateDnsLatencyThresholdMs = 500;
100
Mike Yu60248242020-07-29 16:45:26 +0800101 PrivateDnsConfiguration() = default;
102
Mike Yu82ae84b2020-12-02 21:04:40 +0800103 // Launchs a thread to run the validation for |server| on the network |netId|.
104 // |isRevalidation| is true if this call is due to a revalidation request.
Mike Yuad96ef82021-02-20 18:35:14 +0800105 void startValidation(const ServerIdentity& identity, unsigned netId, bool isRevalidation)
Mike Yu82ae84b2020-12-02 21:04:40 +0800106 REQUIRES(mPrivateDnsLock);
Mike Yub601ff72018-11-01 20:07:00 +0800107
Mike Yuad96ef82021-02-20 18:35:14 +0800108 bool recordPrivateDnsValidation(const ServerIdentity& identity, unsigned netId, bool success,
Mike Yu7135fbf2021-06-10 20:27:36 +0800109 bool isRevalidation, bool latencyTooHigh)
110 EXCLUDES(mPrivateDnsLock);
Mike Yub601ff72018-11-01 20:07:00 +0800111
Mike Yuad96ef82021-02-20 18:35:14 +0800112 void sendPrivateDnsValidationEvent(const ServerIdentity& identity, unsigned netId, bool success)
paulhu0664f692020-12-14 16:48:26 +0800113 REQUIRES(mPrivateDnsLock);
114
Mike Yuf7717f52020-11-24 17:31:12 +0800115 // Decide if a validation for |server| is needed. Note that servers that have failed
Mike Yub601ff72018-11-01 20:07:00 +0800116 // multiple validation attempts but for which there is still a validating
117 // thread running are marked as being Validation::in_process.
Mike Yucf56d232021-04-29 19:37:25 +0800118 bool needsValidation(const IPrivateDnsServer& server) const REQUIRES(mPrivateDnsLock);
Mike Yu3334a5e2020-11-19 13:33:17 +0800119
Mike Yufa985f72020-11-23 20:24:21 +0800120 void updateServerState(const ServerIdentity& identity, Validation state, uint32_t netId)
Mike Yu3334a5e2020-11-19 13:33:17 +0800121 REQUIRES(mPrivateDnsLock);
Mike Yub601ff72018-11-01 20:07:00 +0800122
Mike Yuad96ef82021-02-20 18:35:14 +0800123 // For testing.
Mike Yucf56d232021-04-29 19:37:25 +0800124 base::Result<IPrivateDnsServer*> getPrivateDns(const ServerIdentity& identity, unsigned netId)
Mike Yuad96ef82021-02-20 18:35:14 +0800125 EXCLUDES(mPrivateDnsLock);
126
Mike Yucf56d232021-04-29 19:37:25 +0800127 base::Result<IPrivateDnsServer*> getPrivateDnsLocked(const ServerIdentity& identity,
128 unsigned netId) REQUIRES(mPrivateDnsLock);
Mike Yuad96ef82021-02-20 18:35:14 +0800129
Mike Yu1aede812021-05-11 14:49:30 +0800130 void updateServerLatencyThreshold(const ServerIdentity& identity,
131 std::optional<int64_t> latencyThreshold, uint32_t netId)
132 EXCLUDES(mPrivateDnsLock);
133
Mike Yu82ae84b2020-12-02 21:04:40 +0800134 mutable std::mutex mPrivateDnsLock;
Mike Yub601ff72018-11-01 20:07:00 +0800135 std::map<unsigned, PrivateDnsMode> mPrivateDnsModes GUARDED_BY(mPrivateDnsLock);
Mike Yuf7717f52020-11-24 17:31:12 +0800136
137 // Contains all servers for a network, along with their current validation status.
138 // In case a server is removed due to a configuration change, it remains in this map,
139 // but is marked inactive.
140 // Any pending validation threads will continue running because we have no way to cancel them.
Mike Yub601ff72018-11-01 20:07:00 +0800141 std::map<unsigned, PrivateDnsTracker> mPrivateDnsTransports GUARDED_BY(mPrivateDnsLock);
Mike Yu0ee1dc92020-11-09 14:56:54 +0800142
Mike Yu453b5e42021-02-19 20:03:07 +0800143 void notifyValidationStateUpdate(const netdutils::IPSockAddr& sockaddr, Validation validation,
Mike Yu74770542020-12-15 14:25:21 +0800144 uint32_t netId) const REQUIRES(mPrivateDnsLock);
Mike Yu0ee1dc92020-11-09 14:56:54 +0800145
Mike Yua6853e82020-12-11 19:47:06 +0800146 // TODO: fix the reentrancy problem.
147 PrivateDnsValidationObserver* mObserver GUARDED_BY(mPrivateDnsLock);
Mike Yu0ee1dc92020-11-09 14:56:54 +0800148
149 friend class PrivateDnsConfigurationTest;
Mike Yu3d5130d2020-12-21 17:57:18 +0800150
Mike Yu8058bd02021-05-13 16:44:18 +0800151 // It's not const because PrivateDnsConfigurationTest needs to override it.
152 // TODO: make it const by dependency injection.
153 netdutils::BackoffSequence<>::Builder mBackoffBuilder =
154 netdutils::BackoffSequence<>::Builder()
155 .withInitialRetransmissionTime(std::chrono::seconds(60))
156 .withMaximumRetransmissionTime(std::chrono::seconds(3600));
157
Mike Yu3d5130d2020-12-21 17:57:18 +0800158 struct RecordEntry {
159 RecordEntry(uint32_t netId, const ServerIdentity& identity, Validation state)
160 : netId(netId), serverIdentity(identity), state(state) {}
161
162 const uint32_t netId;
163 const ServerIdentity serverIdentity;
164 const Validation state;
165 const std::chrono::system_clock::time_point timestamp = std::chrono::system_clock::now();
166 };
167
168 LockedRingBuffer<RecordEntry> mPrivateDnsLog{100};
Mike Yub601ff72018-11-01 20:07:00 +0800169};
170
Mike Yub601ff72018-11-01 20:07:00 +0800171} // namespace net
172} // namespace android