blob: 5d42935c6b979fd20cd344ccafcfb1910991b1c1 [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>
25
26#include "DnsTlsServer.h"
Bernie Innocentiec4219b2019-01-30 11:16:36 +090027
Mike Yub601ff72018-11-01 20:07:00 +080028namespace android {
29namespace net {
30
Bernie Innocenti23c6e2a2019-05-16 15:18:35 +090031// The DNS over TLS mode on a specific netId.
lifr6b83a6f2019-07-26 03:17:43 +080032enum class PrivateDnsMode : uint8_t { OFF, OPPORTUNISTIC, STRICT };
Bernie Innocenti23c6e2a2019-05-16 15:18:35 +090033
34// Validation status of a DNS over TLS server (on a specific netId).
35enum class Validation : uint8_t { in_process, success, fail, unknown_server, unknown_netid };
36
Mike Yub601ff72018-11-01 20:07:00 +080037struct PrivateDnsStatus {
38 PrivateDnsMode mode;
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
68 private:
69 typedef std::map<DnsTlsServer, Validation, AddressComparator> PrivateDnsTracker;
Zhang Wei-e7976c1d68adb2019-08-23 23:13:22 -050070 typedef std::set<DnsTlsServer, AddressComparator> ThreadTracker;
Mike Yub601ff72018-11-01 20:07:00 +080071
Mike Yu60248242020-07-29 16:45:26 +080072 PrivateDnsConfiguration() = default;
73
Mike Yub601ff72018-11-01 20:07:00 +080074 void validatePrivateDnsProvider(const DnsTlsServer& server, PrivateDnsTracker& tracker,
75 unsigned netId, uint32_t mark) REQUIRES(mPrivateDnsLock);
76
77 bool recordPrivateDnsValidation(const DnsTlsServer& server, unsigned netId, bool success);
78
Zhang Wei-e7976c1d68adb2019-08-23 23:13:22 -050079 bool needValidateThread(const DnsTlsServer& server, unsigned netId) REQUIRES(mPrivateDnsLock);
80 void cleanValidateThreadTracker(const DnsTlsServer& server, unsigned netId);
81
Mike Yub601ff72018-11-01 20:07:00 +080082 // Start validation for newly added servers as well as any servers that have
83 // landed in Validation::fail state. Note that servers that have failed
84 // multiple validation attempts but for which there is still a validating
85 // thread running are marked as being Validation::in_process.
86 bool needsValidation(const PrivateDnsTracker& tracker, const DnsTlsServer& server);
87
88 std::mutex mPrivateDnsLock;
89 std::map<unsigned, PrivateDnsMode> mPrivateDnsModes GUARDED_BY(mPrivateDnsLock);
90 // Structure for tracking the validation status of servers on a specific netId.
91 // Using the AddressComparator ensures at most one entry per IP address.
92 std::map<unsigned, PrivateDnsTracker> mPrivateDnsTransports GUARDED_BY(mPrivateDnsLock);
Zhang Wei-e7976c1d68adb2019-08-23 23:13:22 -050093 std::map<unsigned, ThreadTracker> mPrivateDnsValidateThreads GUARDED_BY(mPrivateDnsLock);
Mike Yu0ee1dc92020-11-09 14:56:54 +080094
95 // For testing. The observer is notified of onValidationStateUpdate 1) when a validation is
96 // about to begin or 2) when a validation finishes.
97 class Observer {
98 public:
99 virtual ~Observer(){};
100 virtual void onValidationStateUpdate(const std::string& server, Validation validation,
101 uint32_t netId) = 0;
102 };
103
104 void setObserver(Observer* observer);
105 void maybeNotifyObserver(const DnsTlsServer& server, Validation validation,
106 uint32_t netId) const REQUIRES(mPrivateDnsLock);
107
108 Observer* mObserver GUARDED_BY(mPrivateDnsLock);
109
110 friend class PrivateDnsConfigurationTest;
Mike Yub601ff72018-11-01 20:07:00 +0800111};
112
Mike Yub601ff72018-11-01 20:07:00 +0800113} // namespace net
114} // namespace android