blob: b1056d348256c83567f8b3e2ca915db7c55b0d2a [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
36struct PrivateDnsStatus {
37 PrivateDnsMode mode;
Mike Yufa985f72020-11-23 20:24:21 +080038
39 // TODO: change the type to std::vector<DnsTlsServer>.
Mike Yu3e829062019-08-07 14:01:14 +080040 std::map<DnsTlsServer, Validation, AddressComparator> serversMap;
Mike Yub601ff72018-11-01 20:07:00 +080041
Mike Yu3e829062019-08-07 14:01:14 +080042 std::list<DnsTlsServer> validatedServers() const {
43 std::list<DnsTlsServer> servers;
44
45 for (const auto& pair : serversMap) {
46 if (pair.second == Validation::success) {
47 servers.push_back(pair.first);
48 }
49 }
50 return servers;
51 }
Bernie Innocenti23c6e2a2019-05-16 15:18:35 +090052};
53
Mike Yub601ff72018-11-01 20:07:00 +080054class PrivateDnsConfiguration {
55 public:
Mike Yu60248242020-07-29 16:45:26 +080056 // The only instance of PrivateDnsConfiguration.
57 static PrivateDnsConfiguration& getInstance() {
58 static PrivateDnsConfiguration instance;
59 return instance;
60 }
61
Mike Yub601ff72018-11-01 20:07:00 +080062 int set(int32_t netId, uint32_t mark, const std::vector<std::string>& servers,
Mike Yu40e67072019-10-09 21:14:09 +080063 const std::string& name, const std::string& caCert) EXCLUDES(mPrivateDnsLock);
Mike Yub601ff72018-11-01 20:07:00 +080064
Mike Yu82ae84b2020-12-02 21:04:40 +080065 PrivateDnsStatus getStatus(unsigned netId) const EXCLUDES(mPrivateDnsLock);
Mike Yub601ff72018-11-01 20:07:00 +080066
Mike Yu3e829062019-08-07 14:01:14 +080067 void clear(unsigned netId) EXCLUDES(mPrivateDnsLock);
Mike Yub601ff72018-11-01 20:07:00 +080068
Mike Yue60ab412020-12-01 17:56:12 +080069 // Request |server| to be revalidated on a connection tagged with |mark|.
Mike Yu82ae84b2020-12-02 21:04:40 +080070 // Returns a Result to indicate if the request is accepted.
71 base::Result<void> requestValidation(unsigned netId, const DnsTlsServer& server, uint32_t mark)
Mike Yue60ab412020-12-01 17:56:12 +080072 EXCLUDES(mPrivateDnsLock);
73
Mike Yufa985f72020-11-23 20:24:21 +080074 struct ServerIdentity {
75 const netdutils::IPAddress ip;
76 const std::string name;
77 const int protocol;
78
79 explicit ServerIdentity(const DnsTlsServer& server)
80 : ip(netdutils::IPSockAddr::toIPSockAddr(server.ss).ip()),
81 name(server.name),
82 protocol(server.protocol) {}
83
84 bool operator<(const ServerIdentity& other) const {
85 return std::tie(ip, name, protocol) < std::tie(other.ip, other.name, other.protocol);
86 }
87 bool operator==(const ServerIdentity& other) const {
88 return std::tie(ip, name, protocol) == std::tie(other.ip, other.name, other.protocol);
89 }
90 };
91
Mike Yu5448c9e2020-12-14 16:45:16 +080092 void setObserver(PrivateDnsValidationObserver* observer);
93
Mike Yu3d5130d2020-12-21 17:57:18 +080094 void dump(netdutils::DumpWriter& dw) const;
95
Mike Yub601ff72018-11-01 20:07:00 +080096 private:
Mike Yufa985f72020-11-23 20:24:21 +080097 typedef std::map<ServerIdentity, DnsTlsServer> PrivateDnsTracker;
Zhang Wei-e7976c1d68adb2019-08-23 23:13:22 -050098 typedef std::set<DnsTlsServer, AddressComparator> ThreadTracker;
Mike Yub601ff72018-11-01 20:07:00 +080099
Mike Yu60248242020-07-29 16:45:26 +0800100 PrivateDnsConfiguration() = default;
101
Mike Yu82ae84b2020-12-02 21:04:40 +0800102 // Launchs a thread to run the validation for |server| on the network |netId|.
103 // |isRevalidation| is true if this call is due to a revalidation request.
104 void startValidation(const DnsTlsServer& server, unsigned netId, bool isRevalidation)
105 REQUIRES(mPrivateDnsLock);
Mike Yub601ff72018-11-01 20:07:00 +0800106
Mike Yu82ae84b2020-12-02 21:04:40 +0800107 bool recordPrivateDnsValidation(const DnsTlsServer& server, unsigned netId, bool success,
108 bool isRevalidation) EXCLUDES(mPrivateDnsLock);
Mike Yub601ff72018-11-01 20:07:00 +0800109
paulhu0664f692020-12-14 16:48:26 +0800110 void sendPrivateDnsValidationEvent(const DnsTlsServer& server, unsigned netId, bool success)
111 REQUIRES(mPrivateDnsLock);
112
Mike Yuf7717f52020-11-24 17:31:12 +0800113 // Decide if a validation for |server| is needed. Note that servers that have failed
Mike Yub601ff72018-11-01 20:07:00 +0800114 // multiple validation attempts but for which there is still a validating
115 // thread running are marked as being Validation::in_process.
Mike Yuf7717f52020-11-24 17:31:12 +0800116 bool needsValidation(const DnsTlsServer& server) REQUIRES(mPrivateDnsLock);
Mike Yu3334a5e2020-11-19 13:33:17 +0800117
Mike Yufa985f72020-11-23 20:24:21 +0800118 void updateServerState(const ServerIdentity& identity, Validation state, uint32_t netId)
Mike Yu3334a5e2020-11-19 13:33:17 +0800119 REQUIRES(mPrivateDnsLock);
Mike Yub601ff72018-11-01 20:07:00 +0800120
Mike Yu82ae84b2020-12-02 21:04:40 +0800121 mutable std::mutex mPrivateDnsLock;
Mike Yub601ff72018-11-01 20:07:00 +0800122 std::map<unsigned, PrivateDnsMode> mPrivateDnsModes GUARDED_BY(mPrivateDnsLock);
Mike Yuf7717f52020-11-24 17:31:12 +0800123
124 // Contains all servers for a network, along with their current validation status.
125 // In case a server is removed due to a configuration change, it remains in this map,
126 // but is marked inactive.
127 // Any pending validation threads will continue running because we have no way to cancel them.
Mike Yub601ff72018-11-01 20:07:00 +0800128 std::map<unsigned, PrivateDnsTracker> mPrivateDnsTransports GUARDED_BY(mPrivateDnsLock);
Mike Yu0ee1dc92020-11-09 14:56:54 +0800129
Mike Yu74770542020-12-15 14:25:21 +0800130 void notifyValidationStateUpdate(const std::string& serverIp, Validation validation,
131 uint32_t netId) const REQUIRES(mPrivateDnsLock);
Mike Yu0ee1dc92020-11-09 14:56:54 +0800132
Mike Yua6853e82020-12-11 19:47:06 +0800133 // TODO: fix the reentrancy problem.
134 PrivateDnsValidationObserver* mObserver GUARDED_BY(mPrivateDnsLock);
Mike Yu0ee1dc92020-11-09 14:56:54 +0800135
136 friend class PrivateDnsConfigurationTest;
Mike Yu3d5130d2020-12-21 17:57:18 +0800137
138 struct RecordEntry {
139 RecordEntry(uint32_t netId, const ServerIdentity& identity, Validation state)
140 : netId(netId), serverIdentity(identity), state(state) {}
141
142 const uint32_t netId;
143 const ServerIdentity serverIdentity;
144 const Validation state;
145 const std::chrono::system_clock::time_point timestamp = std::chrono::system_clock::now();
146 };
147
148 LockedRingBuffer<RecordEntry> mPrivateDnsLog{100};
Mike Yub601ff72018-11-01 20:07:00 +0800149};
150
Mike Yub601ff72018-11-01 20:07:00 +0800151} // namespace net
152} // namespace android