blob: 4a32defb68fe54177b874f9b47223801a7a1d9a9 [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
24#include <android-base/thread_annotations.h>
Mike Yufa985f72020-11-23 20:24:21 +080025#include <netdutils/InternetAddresses.h>
Mike Yub601ff72018-11-01 20:07:00 +080026
27#include "DnsTlsServer.h"
Mike Yua6853e82020-12-11 19:47:06 +080028#include "PrivateDnsValidationObserver.h"
Bernie Innocentiec4219b2019-01-30 11:16:36 +090029
Mike Yub601ff72018-11-01 20:07:00 +080030namespace android {
31namespace net {
32
33struct PrivateDnsStatus {
34 PrivateDnsMode mode;
Mike Yufa985f72020-11-23 20:24:21 +080035
36 // TODO: change the type to std::vector<DnsTlsServer>.
Mike Yu3e829062019-08-07 14:01:14 +080037 std::map<DnsTlsServer, Validation, AddressComparator> serversMap;
Mike Yub601ff72018-11-01 20:07:00 +080038
Mike Yu3e829062019-08-07 14:01:14 +080039 std::list<DnsTlsServer> validatedServers() const {
40 std::list<DnsTlsServer> servers;
41
42 for (const auto& pair : serversMap) {
43 if (pair.second == Validation::success) {
44 servers.push_back(pair.first);
45 }
46 }
47 return servers;
48 }
Bernie Innocenti23c6e2a2019-05-16 15:18:35 +090049};
50
Mike Yub601ff72018-11-01 20:07:00 +080051class PrivateDnsConfiguration {
52 public:
Mike Yu60248242020-07-29 16:45:26 +080053 // The only instance of PrivateDnsConfiguration.
54 static PrivateDnsConfiguration& getInstance() {
55 static PrivateDnsConfiguration instance;
56 return instance;
57 }
58
Mike Yub601ff72018-11-01 20:07:00 +080059 int set(int32_t netId, uint32_t mark, const std::vector<std::string>& servers,
Mike Yu40e67072019-10-09 21:14:09 +080060 const std::string& name, const std::string& caCert) EXCLUDES(mPrivateDnsLock);
Mike Yub601ff72018-11-01 20:07:00 +080061
Mike Yu3e829062019-08-07 14:01:14 +080062 PrivateDnsStatus getStatus(unsigned netId) EXCLUDES(mPrivateDnsLock);
Mike Yub601ff72018-11-01 20:07:00 +080063
Mike Yu3e829062019-08-07 14:01:14 +080064 void clear(unsigned netId) EXCLUDES(mPrivateDnsLock);
Mike Yub601ff72018-11-01 20:07:00 +080065
Mike Yue60ab412020-12-01 17:56:12 +080066 // Request |server| to be revalidated on a connection tagged with |mark|.
67 // Return true if the request is accepted; otherwise, return false.
68 bool requestValidation(unsigned netId, const DnsTlsServer& server, uint32_t mark)
69 EXCLUDES(mPrivateDnsLock);
70
Mike Yufa985f72020-11-23 20:24:21 +080071 struct ServerIdentity {
72 const netdutils::IPAddress ip;
73 const std::string name;
74 const int protocol;
75
76 explicit ServerIdentity(const DnsTlsServer& server)
77 : ip(netdutils::IPSockAddr::toIPSockAddr(server.ss).ip()),
78 name(server.name),
79 protocol(server.protocol) {}
80
81 bool operator<(const ServerIdentity& other) const {
82 return std::tie(ip, name, protocol) < std::tie(other.ip, other.name, other.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 };
88
Mike Yub601ff72018-11-01 20:07:00 +080089 private:
Mike Yufa985f72020-11-23 20:24:21 +080090 typedef std::map<ServerIdentity, DnsTlsServer> PrivateDnsTracker;
Zhang Wei-e7976c1d68adb2019-08-23 23:13:22 -050091 typedef std::set<DnsTlsServer, AddressComparator> ThreadTracker;
Mike Yub601ff72018-11-01 20:07:00 +080092
Mike Yu60248242020-07-29 16:45:26 +080093 PrivateDnsConfiguration() = default;
94
Mike Yu74770542020-12-15 14:25:21 +080095 void startValidation(const DnsTlsServer& server, unsigned netId) REQUIRES(mPrivateDnsLock);
Mike Yub601ff72018-11-01 20:07:00 +080096
Mike Yu3334a5e2020-11-19 13:33:17 +080097 bool recordPrivateDnsValidation(const DnsTlsServer& server, unsigned netId, bool success)
98 EXCLUDES(mPrivateDnsLock);
Mike Yub601ff72018-11-01 20:07:00 +080099
Mike Yuf7717f52020-11-24 17:31:12 +0800100 // Decide if a validation for |server| is needed. Note that servers that have failed
Mike Yub601ff72018-11-01 20:07:00 +0800101 // multiple validation attempts but for which there is still a validating
102 // thread running are marked as being Validation::in_process.
Mike Yuf7717f52020-11-24 17:31:12 +0800103 bool needsValidation(const DnsTlsServer& server) REQUIRES(mPrivateDnsLock);
Mike Yu3334a5e2020-11-19 13:33:17 +0800104
Mike Yufa985f72020-11-23 20:24:21 +0800105 void updateServerState(const ServerIdentity& identity, Validation state, uint32_t netId)
Mike Yu3334a5e2020-11-19 13:33:17 +0800106 REQUIRES(mPrivateDnsLock);
Mike Yub601ff72018-11-01 20:07:00 +0800107
108 std::mutex mPrivateDnsLock;
109 std::map<unsigned, PrivateDnsMode> mPrivateDnsModes GUARDED_BY(mPrivateDnsLock);
Mike Yuf7717f52020-11-24 17:31:12 +0800110
111 // Contains all servers for a network, along with their current validation status.
112 // In case a server is removed due to a configuration change, it remains in this map,
113 // but is marked inactive.
114 // Any pending validation threads will continue running because we have no way to cancel them.
Mike Yub601ff72018-11-01 20:07:00 +0800115 std::map<unsigned, PrivateDnsTracker> mPrivateDnsTransports GUARDED_BY(mPrivateDnsLock);
Mike Yu0ee1dc92020-11-09 14:56:54 +0800116
Mike Yua6853e82020-12-11 19:47:06 +0800117 void setObserver(PrivateDnsValidationObserver* observer);
Mike Yu74770542020-12-15 14:25:21 +0800118 void notifyValidationStateUpdate(const std::string& serverIp, Validation validation,
119 uint32_t netId) const REQUIRES(mPrivateDnsLock);
Mike Yu0ee1dc92020-11-09 14:56:54 +0800120
Mike Yua6853e82020-12-11 19:47:06 +0800121 // TODO: fix the reentrancy problem.
122 PrivateDnsValidationObserver* mObserver GUARDED_BY(mPrivateDnsLock);
Mike Yu0ee1dc92020-11-09 14:56:54 +0800123
124 friend class PrivateDnsConfigurationTest;
Mike Yub601ff72018-11-01 20:07:00 +0800125};
126
Mike Yub601ff72018-11-01 20:07:00 +0800127} // namespace net
128} // namespace android