Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 1 | /* |
| 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 Yu | 3e82906 | 2019-08-07 14:01:14 +0800 | [diff] [blame] | 17 | #pragma once |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 18 | |
| 19 | #include <list> |
| 20 | #include <map> |
Mike Yu | 303b0df | 2018-12-24 17:05:02 +0800 | [diff] [blame] | 21 | #include <mutex> |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
| 24 | #include <android-base/thread_annotations.h> |
| 25 | |
| 26 | #include "DnsTlsServer.h" |
Bernie Innocenti | ec4219b | 2019-01-30 11:16:36 +0900 | [diff] [blame] | 27 | |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 28 | namespace android { |
| 29 | namespace net { |
| 30 | |
Bernie Innocenti | 23c6e2a | 2019-05-16 15:18:35 +0900 | [diff] [blame] | 31 | // The DNS over TLS mode on a specific netId. |
lifr | 6b83a6f | 2019-07-26 03:17:43 +0800 | [diff] [blame] | 32 | enum class PrivateDnsMode : uint8_t { OFF, OPPORTUNISTIC, STRICT }; |
Bernie Innocenti | 23c6e2a | 2019-05-16 15:18:35 +0900 | [diff] [blame] | 33 | |
| 34 | // Validation status of a DNS over TLS server (on a specific netId). |
| 35 | enum class Validation : uint8_t { in_process, success, fail, unknown_server, unknown_netid }; |
| 36 | |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 37 | struct PrivateDnsStatus { |
| 38 | PrivateDnsMode mode; |
Mike Yu | 3e82906 | 2019-08-07 14:01:14 +0800 | [diff] [blame] | 39 | std::map<DnsTlsServer, Validation, AddressComparator> serversMap; |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 40 | |
Mike Yu | 3e82906 | 2019-08-07 14:01:14 +0800 | [diff] [blame] | 41 | 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 Innocenti | 23c6e2a | 2019-05-16 15:18:35 +0900 | [diff] [blame] | 51 | }; |
| 52 | |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 53 | class PrivateDnsConfiguration { |
| 54 | public: |
| 55 | int set(int32_t netId, uint32_t mark, const std::vector<std::string>& servers, |
Mike Yu | 3e82906 | 2019-08-07 14:01:14 +0800 | [diff] [blame] | 56 | const std::string& name, const std::string& caCert) EXCLUDES(mPrivateDnsLock); |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 57 | |
Mike Yu | 3e82906 | 2019-08-07 14:01:14 +0800 | [diff] [blame] | 58 | PrivateDnsStatus getStatus(unsigned netId) EXCLUDES(mPrivateDnsLock); |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 59 | |
Mike Yu | 3e82906 | 2019-08-07 14:01:14 +0800 | [diff] [blame] | 60 | void clear(unsigned netId) EXCLUDES(mPrivateDnsLock); |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 61 | |
| 62 | private: |
| 63 | typedef std::map<DnsTlsServer, Validation, AddressComparator> PrivateDnsTracker; |
| 64 | |
| 65 | void validatePrivateDnsProvider(const DnsTlsServer& server, PrivateDnsTracker& tracker, |
| 66 | unsigned netId, uint32_t mark) REQUIRES(mPrivateDnsLock); |
| 67 | |
| 68 | bool recordPrivateDnsValidation(const DnsTlsServer& server, unsigned netId, bool success); |
| 69 | |
| 70 | // Start validation for newly added servers as well as any servers that have |
| 71 | // landed in Validation::fail state. Note that servers that have failed |
| 72 | // multiple validation attempts but for which there is still a validating |
| 73 | // thread running are marked as being Validation::in_process. |
| 74 | bool needsValidation(const PrivateDnsTracker& tracker, const DnsTlsServer& server); |
| 75 | |
| 76 | std::mutex mPrivateDnsLock; |
| 77 | std::map<unsigned, PrivateDnsMode> mPrivateDnsModes GUARDED_BY(mPrivateDnsLock); |
| 78 | // Structure for tracking the validation status of servers on a specific netId. |
| 79 | // Using the AddressComparator ensures at most one entry per IP address. |
| 80 | std::map<unsigned, PrivateDnsTracker> mPrivateDnsTransports GUARDED_BY(mPrivateDnsLock); |
Mike Yu | b601ff7 | 2018-11-01 20:07:00 +0800 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | extern PrivateDnsConfiguration gPrivateDnsConfiguration; |
| 84 | |
| 85 | } // namespace net |
| 86 | } // namespace android |