blob: f7e6592bdb23453eeec1fa1c1573c9677cf37cd5 [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 Yub0d27fa2021-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 Yu60248242020-07-29 16:45:26 +080098 PrivateDnsConfiguration() = default;
99
Mike Yu82ae84b2020-12-02 21:04:40 +0800100 // Launchs a thread to run the validation for |server| on the network |netId|.
101 // |isRevalidation| is true if this call is due to a revalidation request.
Mike Yuad96ef82021-02-20 18:35:14 +0800102 void startValidation(const ServerIdentity& identity, unsigned netId, bool isRevalidation)
Mike Yu82ae84b2020-12-02 21:04:40 +0800103 REQUIRES(mPrivateDnsLock);
Mike Yub601ff72018-11-01 20:07:00 +0800104
Mike Yuad96ef82021-02-20 18:35:14 +0800105 bool recordPrivateDnsValidation(const ServerIdentity& identity, unsigned netId, bool success,
Mike Yu82ae84b2020-12-02 21:04:40 +0800106 bool isRevalidation) EXCLUDES(mPrivateDnsLock);
Mike Yub601ff72018-11-01 20:07:00 +0800107
Mike Yuad96ef82021-02-20 18:35:14 +0800108 void sendPrivateDnsValidationEvent(const ServerIdentity& identity, unsigned netId, bool success)
paulhu0664f692020-12-14 16:48:26 +0800109 REQUIRES(mPrivateDnsLock);
110
Mike Yuf7717f52020-11-24 17:31:12 +0800111 // Decide if a validation for |server| is needed. Note that servers that have failed
Mike Yub601ff72018-11-01 20:07:00 +0800112 // multiple validation attempts but for which there is still a validating
113 // thread running are marked as being Validation::in_process.
Mike Yucf56d232021-04-29 19:37:25 +0800114 bool needsValidation(const IPrivateDnsServer& server) const REQUIRES(mPrivateDnsLock);
Mike Yu3334a5e2020-11-19 13:33:17 +0800115
Mike Yufa985f72020-11-23 20:24:21 +0800116 void updateServerState(const ServerIdentity& identity, Validation state, uint32_t netId)
Mike Yu3334a5e2020-11-19 13:33:17 +0800117 REQUIRES(mPrivateDnsLock);
Mike Yub601ff72018-11-01 20:07:00 +0800118
Mike Yuad96ef82021-02-20 18:35:14 +0800119 // For testing.
Mike Yucf56d232021-04-29 19:37:25 +0800120 base::Result<IPrivateDnsServer*> getPrivateDns(const ServerIdentity& identity, unsigned netId)
Mike Yuad96ef82021-02-20 18:35:14 +0800121 EXCLUDES(mPrivateDnsLock);
122
Mike Yucf56d232021-04-29 19:37:25 +0800123 base::Result<IPrivateDnsServer*> getPrivateDnsLocked(const ServerIdentity& identity,
124 unsigned netId) REQUIRES(mPrivateDnsLock);
Mike Yuad96ef82021-02-20 18:35:14 +0800125
Mike Yu82ae84b2020-12-02 21:04:40 +0800126 mutable std::mutex mPrivateDnsLock;
Mike Yub601ff72018-11-01 20:07:00 +0800127 std::map<unsigned, PrivateDnsMode> mPrivateDnsModes GUARDED_BY(mPrivateDnsLock);
Mike Yuf7717f52020-11-24 17:31:12 +0800128
129 // Contains all servers for a network, along with their current validation status.
130 // In case a server is removed due to a configuration change, it remains in this map,
131 // but is marked inactive.
132 // Any pending validation threads will continue running because we have no way to cancel them.
Mike Yub601ff72018-11-01 20:07:00 +0800133 std::map<unsigned, PrivateDnsTracker> mPrivateDnsTransports GUARDED_BY(mPrivateDnsLock);
Mike Yu0ee1dc92020-11-09 14:56:54 +0800134
Mike Yu453b5e42021-02-19 20:03:07 +0800135 void notifyValidationStateUpdate(const netdutils::IPSockAddr& sockaddr, Validation validation,
Mike Yu74770542020-12-15 14:25:21 +0800136 uint32_t netId) const REQUIRES(mPrivateDnsLock);
Mike Yu0ee1dc92020-11-09 14:56:54 +0800137
Mike Yua6853e82020-12-11 19:47:06 +0800138 // TODO: fix the reentrancy problem.
139 PrivateDnsValidationObserver* mObserver GUARDED_BY(mPrivateDnsLock);
Mike Yu0ee1dc92020-11-09 14:56:54 +0800140
141 friend class PrivateDnsConfigurationTest;
Mike Yu3d5130d2020-12-21 17:57:18 +0800142
Mike Yub0d27fa2021-05-13 16:44:18 +0800143 // It's not const because PrivateDnsConfigurationTest needs to override it.
144 // TODO: make it const by dependency injection.
145 netdutils::BackoffSequence<>::Builder mBackoffBuilder =
146 netdutils::BackoffSequence<>::Builder()
147 .withInitialRetransmissionTime(std::chrono::seconds(60))
148 .withMaximumRetransmissionTime(std::chrono::seconds(3600));
149
Mike Yu3d5130d2020-12-21 17:57:18 +0800150 struct RecordEntry {
151 RecordEntry(uint32_t netId, const ServerIdentity& identity, Validation state)
152 : netId(netId), serverIdentity(identity), state(state) {}
153
154 const uint32_t netId;
155 const ServerIdentity serverIdentity;
156 const Validation state;
157 const std::chrono::system_clock::time_point timestamp = std::chrono::system_clock::now();
158 };
159
160 LockedRingBuffer<RecordEntry> mPrivateDnsLog{100};
Mike Yub601ff72018-11-01 20:07:00 +0800161};
162
Mike Yub601ff72018-11-01 20:07:00 +0800163} // namespace net
164} // namespace android