blob: 722ed71952c10b57b273f3681cf27bd102110f0d [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 Yufa985f72020-11-23 20:24:21 +080068 struct ServerIdentity {
69 const netdutils::IPAddress ip;
70 const std::string name;
71 const int protocol;
72
73 explicit ServerIdentity(const DnsTlsServer& server)
74 : ip(netdutils::IPSockAddr::toIPSockAddr(server.ss).ip()),
75 name(server.name),
76 protocol(server.protocol) {}
77
78 bool operator<(const ServerIdentity& other) const {
79 return std::tie(ip, name, protocol) < std::tie(other.ip, other.name, other.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 };
85
Mike Yub601ff72018-11-01 20:07:00 +080086 private:
Mike Yufa985f72020-11-23 20:24:21 +080087 typedef std::map<ServerIdentity, DnsTlsServer> PrivateDnsTracker;
Zhang Wei-e7976c1d68adb2019-08-23 23:13:22 -050088 typedef std::set<DnsTlsServer, AddressComparator> ThreadTracker;
Mike Yub601ff72018-11-01 20:07:00 +080089
Mike Yu60248242020-07-29 16:45:26 +080090 PrivateDnsConfiguration() = default;
91
Mike Yu3334a5e2020-11-19 13:33:17 +080092 void startValidation(const DnsTlsServer& server, unsigned netId, uint32_t mark)
93 REQUIRES(mPrivateDnsLock);
Mike Yub601ff72018-11-01 20:07:00 +080094
Mike Yu3334a5e2020-11-19 13:33:17 +080095 bool recordPrivateDnsValidation(const DnsTlsServer& server, unsigned netId, bool success)
96 EXCLUDES(mPrivateDnsLock);
Mike Yub601ff72018-11-01 20:07:00 +080097
Mike Yuf7717f52020-11-24 17:31:12 +080098 // Decide if a validation for |server| is needed. Note that servers that have failed
Mike Yub601ff72018-11-01 20:07:00 +080099 // multiple validation attempts but for which there is still a validating
100 // thread running are marked as being Validation::in_process.
Mike Yuf7717f52020-11-24 17:31:12 +0800101 bool needsValidation(const DnsTlsServer& server) REQUIRES(mPrivateDnsLock);
Mike Yu3334a5e2020-11-19 13:33:17 +0800102
Mike Yufa985f72020-11-23 20:24:21 +0800103 void updateServerState(const ServerIdentity& identity, Validation state, uint32_t netId)
Mike Yu3334a5e2020-11-19 13:33:17 +0800104 REQUIRES(mPrivateDnsLock);
Mike Yub601ff72018-11-01 20:07:00 +0800105
106 std::mutex mPrivateDnsLock;
107 std::map<unsigned, PrivateDnsMode> mPrivateDnsModes GUARDED_BY(mPrivateDnsLock);
Mike Yuf7717f52020-11-24 17:31:12 +0800108
109 // Contains all servers for a network, along with their current validation status.
110 // In case a server is removed due to a configuration change, it remains in this map,
111 // but is marked inactive.
112 // Any pending validation threads will continue running because we have no way to cancel them.
Mike Yub601ff72018-11-01 20:07:00 +0800113 std::map<unsigned, PrivateDnsTracker> mPrivateDnsTransports GUARDED_BY(mPrivateDnsLock);
Mike Yu0ee1dc92020-11-09 14:56:54 +0800114
115 // For testing. The observer is notified of onValidationStateUpdate 1) when a validation is
Mike Yufa985f72020-11-23 20:24:21 +0800116 // about to begin or 2) when a validation finishes. If a validation finishes when in OFF mode
117 // or when the network has been destroyed, |validation| will be Validation::fail.
118 // WARNING: The Observer is notified while the lock is being held. Be careful not to call
119 // any method of PrivateDnsConfiguration from the observer.
Mike Yu3334a5e2020-11-19 13:33:17 +0800120 // TODO: fix the reentrancy problem.
Mike Yu0ee1dc92020-11-09 14:56:54 +0800121 class Observer {
122 public:
123 virtual ~Observer(){};
Mike Yufa985f72020-11-23 20:24:21 +0800124 virtual void onValidationStateUpdate(const std::string& serverIp, Validation validation,
Mike Yu0ee1dc92020-11-09 14:56:54 +0800125 uint32_t netId) = 0;
126 };
127
128 void setObserver(Observer* observer);
Mike Yufa985f72020-11-23 20:24:21 +0800129 void maybeNotifyObserver(const std::string& serverIp, Validation validation,
Mike Yu0ee1dc92020-11-09 14:56:54 +0800130 uint32_t netId) const REQUIRES(mPrivateDnsLock);
131
132 Observer* mObserver GUARDED_BY(mPrivateDnsLock);
133
134 friend class PrivateDnsConfigurationTest;
Mike Yub601ff72018-11-01 20:07:00 +0800135};
136
Mike Yub601ff72018-11-01 20:07:00 +0800137} // namespace net
138} // namespace android