blob: 20b4fa247fa3a8f8da6a9de695eb1788ce3ffd62 [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"
Bernie Innocentiec4219b2019-01-30 11:16:36 +090028
Mike Yub601ff72018-11-01 20:07:00 +080029namespace android {
30namespace net {
31
Bernie Innocenti23c6e2a2019-05-16 15:18:35 +090032// The DNS over TLS mode on a specific netId.
lifr6b83a6f2019-07-26 03:17:43 +080033enum class PrivateDnsMode : uint8_t { OFF, OPPORTUNISTIC, STRICT };
Bernie Innocenti23c6e2a2019-05-16 15:18:35 +090034
Mike Yub601ff72018-11-01 20:07:00 +080035struct PrivateDnsStatus {
36 PrivateDnsMode mode;
Mike Yufa985f72020-11-23 20:24:21 +080037
38 // TODO: change the type to std::vector<DnsTlsServer>.
Mike Yu3e829062019-08-07 14:01:14 +080039 std::map<DnsTlsServer, Validation, AddressComparator> serversMap;
Mike Yub601ff72018-11-01 20:07:00 +080040
Mike Yu3e829062019-08-07 14:01:14 +080041 std::list<DnsTlsServer> validatedServers() const {
42 std::list<DnsTlsServer> servers;
43
44 for (const auto& pair : serversMap) {
45 if (pair.second == Validation::success) {
46 servers.push_back(pair.first);
47 }
48 }
49 return servers;
50 }
Bernie Innocenti23c6e2a2019-05-16 15:18:35 +090051};
52
Mike Yub601ff72018-11-01 20:07:00 +080053class PrivateDnsConfiguration {
54 public:
Mike Yu60248242020-07-29 16:45:26 +080055 // The only instance of PrivateDnsConfiguration.
56 static PrivateDnsConfiguration& getInstance() {
57 static PrivateDnsConfiguration instance;
58 return instance;
59 }
60
Mike Yub601ff72018-11-01 20:07:00 +080061 int set(int32_t netId, uint32_t mark, const std::vector<std::string>& servers,
Mike Yu40e67072019-10-09 21:14:09 +080062 const std::string& name, const std::string& caCert) EXCLUDES(mPrivateDnsLock);
Mike Yub601ff72018-11-01 20:07:00 +080063
Mike Yu3e829062019-08-07 14:01:14 +080064 PrivateDnsStatus getStatus(unsigned netId) EXCLUDES(mPrivateDnsLock);
Mike Yub601ff72018-11-01 20:07:00 +080065
Mike Yu3e829062019-08-07 14:01:14 +080066 void clear(unsigned netId) EXCLUDES(mPrivateDnsLock);
Mike Yub601ff72018-11-01 20:07:00 +080067
Mike Yue60ab412020-12-01 17:56:12 +080068 // Request |server| to be revalidated on a connection tagged with |mark|.
69 // Return true if the request is accepted; otherwise, return false.
70 bool requestValidation(unsigned netId, const DnsTlsServer& server, uint32_t mark)
71 EXCLUDES(mPrivateDnsLock);
72
Mike Yufa985f72020-11-23 20:24:21 +080073 struct ServerIdentity {
74 const netdutils::IPAddress ip;
75 const std::string name;
76 const int protocol;
77
78 explicit ServerIdentity(const DnsTlsServer& server)
79 : ip(netdutils::IPSockAddr::toIPSockAddr(server.ss).ip()),
80 name(server.name),
81 protocol(server.protocol) {}
82
83 bool operator<(const ServerIdentity& other) const {
84 return std::tie(ip, name, protocol) < std::tie(other.ip, other.name, other.protocol);
85 }
86 bool operator==(const ServerIdentity& other) const {
87 return std::tie(ip, name, protocol) == std::tie(other.ip, other.name, other.protocol);
88 }
89 };
90
Mike Yub601ff72018-11-01 20:07:00 +080091 private:
Mike Yufa985f72020-11-23 20:24:21 +080092 typedef std::map<ServerIdentity, DnsTlsServer> PrivateDnsTracker;
Zhang Wei-e7976c1d68adb2019-08-23 23:13:22 -050093 typedef std::set<DnsTlsServer, AddressComparator> ThreadTracker;
Mike Yub601ff72018-11-01 20:07:00 +080094
Mike Yu60248242020-07-29 16:45:26 +080095 PrivateDnsConfiguration() = default;
96
Mike Yu3334a5e2020-11-19 13:33:17 +080097 void startValidation(const DnsTlsServer& server, unsigned netId, uint32_t mark)
98 REQUIRES(mPrivateDnsLock);
Mike Yub601ff72018-11-01 20:07:00 +080099
Mike Yu3334a5e2020-11-19 13:33:17 +0800100 bool recordPrivateDnsValidation(const DnsTlsServer& server, unsigned netId, bool success)
101 EXCLUDES(mPrivateDnsLock);
Mike Yub601ff72018-11-01 20:07:00 +0800102
Mike Yuf7717f52020-11-24 17:31:12 +0800103 // Decide if a validation for |server| is needed. Note that servers that have failed
Mike Yub601ff72018-11-01 20:07:00 +0800104 // multiple validation attempts but for which there is still a validating
105 // thread running are marked as being Validation::in_process.
Mike Yuf7717f52020-11-24 17:31:12 +0800106 bool needsValidation(const DnsTlsServer& server) REQUIRES(mPrivateDnsLock);
Mike Yu3334a5e2020-11-19 13:33:17 +0800107
Mike Yufa985f72020-11-23 20:24:21 +0800108 void updateServerState(const ServerIdentity& identity, Validation state, uint32_t netId)
Mike Yu3334a5e2020-11-19 13:33:17 +0800109 REQUIRES(mPrivateDnsLock);
Mike Yub601ff72018-11-01 20:07:00 +0800110
111 std::mutex mPrivateDnsLock;
112 std::map<unsigned, PrivateDnsMode> mPrivateDnsModes GUARDED_BY(mPrivateDnsLock);
Mike Yuf7717f52020-11-24 17:31:12 +0800113
114 // Contains all servers for a network, along with their current validation status.
115 // In case a server is removed due to a configuration change, it remains in this map,
116 // but is marked inactive.
117 // Any pending validation threads will continue running because we have no way to cancel them.
Mike Yub601ff72018-11-01 20:07:00 +0800118 std::map<unsigned, PrivateDnsTracker> mPrivateDnsTransports GUARDED_BY(mPrivateDnsLock);
Mike Yu0ee1dc92020-11-09 14:56:54 +0800119
120 // For testing. The observer is notified of onValidationStateUpdate 1) when a validation is
Mike Yufa985f72020-11-23 20:24:21 +0800121 // about to begin or 2) when a validation finishes. If a validation finishes when in OFF mode
122 // or when the network has been destroyed, |validation| will be Validation::fail.
123 // WARNING: The Observer is notified while the lock is being held. Be careful not to call
124 // any method of PrivateDnsConfiguration from the observer.
Mike Yu3334a5e2020-11-19 13:33:17 +0800125 // TODO: fix the reentrancy problem.
Mike Yu0ee1dc92020-11-09 14:56:54 +0800126 class Observer {
127 public:
128 virtual ~Observer(){};
Mike Yufa985f72020-11-23 20:24:21 +0800129 virtual void onValidationStateUpdate(const std::string& serverIp, Validation validation,
Mike Yu0ee1dc92020-11-09 14:56:54 +0800130 uint32_t netId) = 0;
131 };
132
133 void setObserver(Observer* observer);
Mike Yufa985f72020-11-23 20:24:21 +0800134 void maybeNotifyObserver(const std::string& serverIp, Validation validation,
Mike Yu0ee1dc92020-11-09 14:56:54 +0800135 uint32_t netId) const REQUIRES(mPrivateDnsLock);
136
137 Observer* mObserver GUARDED_BY(mPrivateDnsLock);
138
139 friend class PrivateDnsConfigurationTest;
Mike Yub601ff72018-11-01 20:07:00 +0800140};
141
Mike Yub601ff72018-11-01 20:07:00 +0800142} // namespace net
143} // namespace android