blob: 7baede9419fee8742952037b5495c72ab37fdcbb [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 Yu3d5130d2020-12-21 17:57:18 +080026#include <netdutils/DumpWriter.h>
Mike Yufa985f72020-11-23 20:24:21 +080027#include <netdutils/InternetAddresses.h>
Mike Yub601ff72018-11-01 20:07:00 +080028
29#include "DnsTlsServer.h"
Mike Yu3d5130d2020-12-21 17:57:18 +080030#include "LockedQueue.h"
Mike Yua6853e82020-12-11 19:47:06 +080031#include "PrivateDnsValidationObserver.h"
Bernie Innocentiec4219b2019-01-30 11:16:36 +090032
Mike Yub601ff72018-11-01 20:07:00 +080033namespace android {
34namespace net {
35
Mike Yuad96ef82021-02-20 18:35:14 +080036// TODO: decouple the dependency of DnsTlsServer.
Mike Yub601ff72018-11-01 20:07:00 +080037struct PrivateDnsStatus {
38 PrivateDnsMode mode;
Mike Yufa985f72020-11-23 20:24:21 +080039
40 // TODO: change the type to std::vector<DnsTlsServer>.
Mike Yu3e829062019-08-07 14:01:14 +080041 std::map<DnsTlsServer, Validation, AddressComparator> serversMap;
Mike Yub601ff72018-11-01 20:07:00 +080042
Mike Yu3e829062019-08-07 14:01:14 +080043 std::list<DnsTlsServer> validatedServers() const {
44 std::list<DnsTlsServer> servers;
45
46 for (const auto& pair : serversMap) {
47 if (pair.second == Validation::success) {
48 servers.push_back(pair.first);
49 }
50 }
51 return servers;
52 }
Bernie Innocenti23c6e2a2019-05-16 15:18:35 +090053};
54
Mike Yub601ff72018-11-01 20:07:00 +080055class PrivateDnsConfiguration {
56 public:
Mike Yufa985f72020-11-23 20:24:21 +080057 struct ServerIdentity {
Mike Yu453b5e42021-02-19 20:03:07 +080058 const netdutils::IPSockAddr sockaddr;
59 const std::string provider;
Mike Yufa985f72020-11-23 20:24:21 +080060
Mike Yucf56d232021-04-29 19:37:25 +080061 explicit ServerIdentity(const IPrivateDnsServer& server)
62 : sockaddr(server.addr()), provider(server.provider()) {}
Mike Yufa985f72020-11-23 20:24:21 +080063
64 bool operator<(const ServerIdentity& other) const {
Mike Yu453b5e42021-02-19 20:03:07 +080065 return std::tie(sockaddr, provider) < std::tie(other.sockaddr, other.provider);
Mike Yufa985f72020-11-23 20:24:21 +080066 }
67 bool operator==(const ServerIdentity& other) const {
Mike Yu453b5e42021-02-19 20:03:07 +080068 return std::tie(sockaddr, provider) == std::tie(other.sockaddr, other.provider);
Mike Yufa985f72020-11-23 20:24:21 +080069 }
70 };
71
Mike Yuad96ef82021-02-20 18:35:14 +080072 // The only instance of PrivateDnsConfiguration.
73 static PrivateDnsConfiguration& getInstance() {
74 static PrivateDnsConfiguration instance;
75 return instance;
76 }
77
78 int set(int32_t netId, uint32_t mark, const std::vector<std::string>& servers,
79 const std::string& name, const std::string& caCert) EXCLUDES(mPrivateDnsLock);
80
81 PrivateDnsStatus getStatus(unsigned netId) const EXCLUDES(mPrivateDnsLock);
82
83 void clear(unsigned netId) EXCLUDES(mPrivateDnsLock);
84
85 // Request the server to be revalidated on a connection tagged with |mark|.
86 // Returns a Result to indicate if the request is accepted.
87 base::Result<void> requestValidation(unsigned netId, const ServerIdentity& identity,
88 uint32_t mark) EXCLUDES(mPrivateDnsLock);
89
Mike Yu5448c9e2020-12-14 16:45:16 +080090 void setObserver(PrivateDnsValidationObserver* observer);
91
Mike Yu3d5130d2020-12-21 17:57:18 +080092 void dump(netdutils::DumpWriter& dw) const;
93
Mike Yub601ff72018-11-01 20:07:00 +080094 private:
Mike Yucf56d232021-04-29 19:37:25 +080095 typedef std::map<ServerIdentity, std::unique_ptr<IPrivateDnsServer>> PrivateDnsTracker;
Mike Yub601ff72018-11-01 20:07:00 +080096
Mike Yu60248242020-07-29 16:45:26 +080097 PrivateDnsConfiguration() = default;
98
Mike Yu82ae84b2020-12-02 21:04:40 +080099 // Launchs a thread to run the validation for |server| on the network |netId|.
100 // |isRevalidation| is true if this call is due to a revalidation request.
Mike Yuad96ef82021-02-20 18:35:14 +0800101 void startValidation(const ServerIdentity& identity, unsigned netId, bool isRevalidation)
Mike Yu82ae84b2020-12-02 21:04:40 +0800102 REQUIRES(mPrivateDnsLock);
Mike Yub601ff72018-11-01 20:07:00 +0800103
Mike Yuad96ef82021-02-20 18:35:14 +0800104 bool recordPrivateDnsValidation(const ServerIdentity& identity, unsigned netId, bool success,
Mike Yu82ae84b2020-12-02 21:04:40 +0800105 bool isRevalidation) EXCLUDES(mPrivateDnsLock);
Mike Yub601ff72018-11-01 20:07:00 +0800106
Mike Yuad96ef82021-02-20 18:35:14 +0800107 void sendPrivateDnsValidationEvent(const ServerIdentity& identity, unsigned netId, bool success)
paulhu0664f692020-12-14 16:48:26 +0800108 REQUIRES(mPrivateDnsLock);
109
Mike Yuf7717f52020-11-24 17:31:12 +0800110 // Decide if a validation for |server| is needed. Note that servers that have failed
Mike Yub601ff72018-11-01 20:07:00 +0800111 // multiple validation attempts but for which there is still a validating
112 // thread running are marked as being Validation::in_process.
Mike Yucf56d232021-04-29 19:37:25 +0800113 bool needsValidation(const IPrivateDnsServer& server) const REQUIRES(mPrivateDnsLock);
Mike Yu3334a5e2020-11-19 13:33:17 +0800114
Mike Yufa985f72020-11-23 20:24:21 +0800115 void updateServerState(const ServerIdentity& identity, Validation state, uint32_t netId)
Mike Yu3334a5e2020-11-19 13:33:17 +0800116 REQUIRES(mPrivateDnsLock);
Mike Yub601ff72018-11-01 20:07:00 +0800117
Mike Yuad96ef82021-02-20 18:35:14 +0800118 // For testing.
Mike Yucf56d232021-04-29 19:37:25 +0800119 base::Result<IPrivateDnsServer*> getPrivateDns(const ServerIdentity& identity, unsigned netId)
Mike Yuad96ef82021-02-20 18:35:14 +0800120 EXCLUDES(mPrivateDnsLock);
121
Mike Yucf56d232021-04-29 19:37:25 +0800122 base::Result<IPrivateDnsServer*> getPrivateDnsLocked(const ServerIdentity& identity,
123 unsigned netId) REQUIRES(mPrivateDnsLock);
Mike Yuad96ef82021-02-20 18:35:14 +0800124
Mike Yu82ae84b2020-12-02 21:04:40 +0800125 mutable std::mutex mPrivateDnsLock;
Mike Yub601ff72018-11-01 20:07:00 +0800126 std::map<unsigned, PrivateDnsMode> mPrivateDnsModes GUARDED_BY(mPrivateDnsLock);
Mike Yuf7717f52020-11-24 17:31:12 +0800127
128 // Contains all servers for a network, along with their current validation status.
129 // In case a server is removed due to a configuration change, it remains in this map,
130 // but is marked inactive.
131 // Any pending validation threads will continue running because we have no way to cancel them.
Mike Yub601ff72018-11-01 20:07:00 +0800132 std::map<unsigned, PrivateDnsTracker> mPrivateDnsTransports GUARDED_BY(mPrivateDnsLock);
Mike Yu0ee1dc92020-11-09 14:56:54 +0800133
Mike Yu453b5e42021-02-19 20:03:07 +0800134 void notifyValidationStateUpdate(const netdutils::IPSockAddr& sockaddr, Validation validation,
Mike Yu74770542020-12-15 14:25:21 +0800135 uint32_t netId) const REQUIRES(mPrivateDnsLock);
Mike Yu0ee1dc92020-11-09 14:56:54 +0800136
Mike Yua6853e82020-12-11 19:47:06 +0800137 // TODO: fix the reentrancy problem.
138 PrivateDnsValidationObserver* mObserver GUARDED_BY(mPrivateDnsLock);
Mike Yu0ee1dc92020-11-09 14:56:54 +0800139
140 friend class PrivateDnsConfigurationTest;
Mike Yu3d5130d2020-12-21 17:57:18 +0800141
142 struct RecordEntry {
143 RecordEntry(uint32_t netId, const ServerIdentity& identity, Validation state)
144 : netId(netId), serverIdentity(identity), state(state) {}
145
146 const uint32_t netId;
147 const ServerIdentity serverIdentity;
148 const Validation state;
149 const std::chrono::system_clock::time_point timestamp = std::chrono::system_clock::now();
150 };
151
152 LockedRingBuffer<RecordEntry> mPrivateDnsLog{100};
Mike Yub601ff72018-11-01 20:07:00 +0800153};
154
Mike Yub601ff72018-11-01 20:07:00 +0800155} // namespace net
156} // namespace android