blob: 04f6a47a3861d2a81f5421ecd030161b5aefc514 [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 Yu3d5130d2020-12-21 17:57:18 +080025#include <netdutils/DumpWriter.h>
Mike Yufa985f72020-11-23 20:24:21 +080026#include <netdutils/InternetAddresses.h>
Mike Yub601ff72018-11-01 20:07:00 +080027
28#include "DnsTlsServer.h"
Mike Yu3d5130d2020-12-21 17:57:18 +080029#include "LockedQueue.h"
Mike Yua6853e82020-12-11 19:47:06 +080030#include "PrivateDnsValidationObserver.h"
Bernie Innocentiec4219b2019-01-30 11:16:36 +090031
Mike Yub601ff72018-11-01 20:07:00 +080032namespace android {
33namespace net {
34
35struct 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 Yu5448c9e2020-12-14 16:45:16 +080091 void setObserver(PrivateDnsValidationObserver* observer);
92
Mike Yu3d5130d2020-12-21 17:57:18 +080093 void dump(netdutils::DumpWriter& dw) const;
94
Mike Yub601ff72018-11-01 20:07:00 +080095 private:
Mike Yufa985f72020-11-23 20:24:21 +080096 typedef std::map<ServerIdentity, DnsTlsServer> PrivateDnsTracker;
Zhang Wei-e7976c1d68adb2019-08-23 23:13:22 -050097 typedef std::set<DnsTlsServer, AddressComparator> ThreadTracker;
Mike Yub601ff72018-11-01 20:07:00 +080098
Mike Yu60248242020-07-29 16:45:26 +080099 PrivateDnsConfiguration() = default;
100
Mike Yu74770542020-12-15 14:25:21 +0800101 void startValidation(const DnsTlsServer& server, unsigned netId) REQUIRES(mPrivateDnsLock);
Mike Yub601ff72018-11-01 20:07:00 +0800102
Mike Yu3334a5e2020-11-19 13:33:17 +0800103 bool recordPrivateDnsValidation(const DnsTlsServer& server, unsigned netId, bool success)
104 EXCLUDES(mPrivateDnsLock);
Mike Yub601ff72018-11-01 20:07:00 +0800105
paulhu0664f692020-12-14 16:48:26 +0800106 void sendPrivateDnsValidationEvent(const DnsTlsServer& server, unsigned netId, bool success)
107 REQUIRES(mPrivateDnsLock);
108
Mike Yuf7717f52020-11-24 17:31:12 +0800109 // Decide if a validation for |server| is needed. Note that servers that have failed
Mike Yub601ff72018-11-01 20:07:00 +0800110 // multiple validation attempts but for which there is still a validating
111 // thread running are marked as being Validation::in_process.
Mike Yuf7717f52020-11-24 17:31:12 +0800112 bool needsValidation(const DnsTlsServer& server) REQUIRES(mPrivateDnsLock);
Mike Yu3334a5e2020-11-19 13:33:17 +0800113
Mike Yufa985f72020-11-23 20:24:21 +0800114 void updateServerState(const ServerIdentity& identity, Validation state, uint32_t netId)
Mike Yu3334a5e2020-11-19 13:33:17 +0800115 REQUIRES(mPrivateDnsLock);
Mike Yub601ff72018-11-01 20:07:00 +0800116
117 std::mutex mPrivateDnsLock;
118 std::map<unsigned, PrivateDnsMode> mPrivateDnsModes GUARDED_BY(mPrivateDnsLock);
Mike Yuf7717f52020-11-24 17:31:12 +0800119
120 // Contains all servers for a network, along with their current validation status.
121 // In case a server is removed due to a configuration change, it remains in this map,
122 // but is marked inactive.
123 // Any pending validation threads will continue running because we have no way to cancel them.
Mike Yub601ff72018-11-01 20:07:00 +0800124 std::map<unsigned, PrivateDnsTracker> mPrivateDnsTransports GUARDED_BY(mPrivateDnsLock);
Mike Yu0ee1dc92020-11-09 14:56:54 +0800125
Mike Yu74770542020-12-15 14:25:21 +0800126 void notifyValidationStateUpdate(const std::string& serverIp, Validation validation,
127 uint32_t netId) const REQUIRES(mPrivateDnsLock);
Mike Yu0ee1dc92020-11-09 14:56:54 +0800128
Mike Yua6853e82020-12-11 19:47:06 +0800129 // TODO: fix the reentrancy problem.
130 PrivateDnsValidationObserver* mObserver GUARDED_BY(mPrivateDnsLock);
Mike Yu0ee1dc92020-11-09 14:56:54 +0800131
132 friend class PrivateDnsConfigurationTest;
Mike Yu3d5130d2020-12-21 17:57:18 +0800133
134 struct RecordEntry {
135 RecordEntry(uint32_t netId, const ServerIdentity& identity, Validation state)
136 : netId(netId), serverIdentity(identity), state(state) {}
137
138 const uint32_t netId;
139 const ServerIdentity serverIdentity;
140 const Validation state;
141 const std::chrono::system_clock::time_point timestamp = std::chrono::system_clock::now();
142 };
143
144 LockedRingBuffer<RecordEntry> mPrivateDnsLog{100};
Mike Yub601ff72018-11-01 20:07:00 +0800145};
146
Mike Yub601ff72018-11-01 20:07:00 +0800147} // namespace net
148} // namespace android