1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef NETSYS_DNS_PARAM_CACHE_H 17 #define NETSYS_DNS_PARAM_CACHE_H 18 19 #include <iostream> 20 #include <map> 21 22 #include "ffrt.h" 23 #include "dns_resolv_config.h" 24 #include "netnative_log_wrapper.h" 25 #include "uid_range.h" 26 #ifdef FEATURE_NET_FIREWALL_ENABLE 27 #include "netfirewall_parcel.h" 28 #include "i_netfirewall_callback.h" 29 #include "suffix_match_trie.h" 30 #include <unordered_map> 31 #endif 32 #if DNS_CONFIG_DEBUG 33 #ifdef DNS_CONFIG_PRINT 34 #undef DNS_CONFIG_PRINT 35 #endif 36 #define DNS_CONFIG_PRINT(fmt, ...) NETNATIVE_LOGI("DNS" fmt, ##__VA_ARGS__) 37 #else 38 #define DNS_CONFIG_PRINT(fmt, ...) 39 #endif 40 41 namespace OHOS::nmd { 42 #ifdef FEATURE_NET_FIREWALL_ENABLE 43 using namespace OHOS::NetManagerStandard; 44 #endif 45 class DnsParamCache { 46 public: 47 ~DnsParamCache() = default; 48 49 static DnsParamCache &GetInstance(); 50 51 // for net_conn_service 52 int32_t SetResolverConfig(uint16_t netId, uint16_t baseTimeoutMsec, uint8_t retryCount, 53 const std::vector<std::string> &servers, const std::vector<std::string> &domains); 54 55 int32_t CreateCacheForNet(uint16_t netId); 56 57 void SetDefaultNetwork(uint16_t netId); 58 59 // for client 60 void SetDnsCache(uint16_t netId, const std::string &hostName, const AddrInfo &addrInfo); 61 62 void SetCacheDelayed(uint16_t netId, const std::string &hostName); 63 64 std::vector<AddrInfo> GetDnsCache(uint16_t netId, const std::string &hostName); 65 66 int32_t GetResolverConfig(uint16_t netId, std::vector<std::string> &servers, std::vector<std::string> &domains, 67 uint16_t &baseTimeoutMsec, uint8_t &retryCount); 68 69 int32_t GetResolverConfig(uint16_t netId, uint32_t uid, std::vector<std::string> &servers, 70 std::vector<std::string> &domains, uint16_t &baseTimeoutMsec, uint8_t &retryCount); 71 72 int32_t GetDefaultNetwork() const; 73 74 void GetDumpInfo(std::string &info); 75 76 int32_t DestroyNetworkCache(uint16_t netId); 77 78 bool IsIpv6Enable(uint16_t netId); 79 80 void EnableIpv6(uint16_t netId); 81 82 int32_t AddUidRange(uint32_t netId, const std::vector<NetManagerStandard::UidRange> &uidRanges); 83 84 int32_t DelUidRange(uint32_t netId, const std::vector<NetManagerStandard::UidRange> &uidRanges); 85 86 bool IsVpnOpen() const; 87 88 #ifdef FEATURE_NET_FIREWALL_ENABLE 89 int32_t SetFirewallDefaultAction(FirewallRuleAction inDefault, FirewallRuleAction outDefault); 90 SetFirewallCurrentUserId(int32_t userId)91 int32_t SetFirewallCurrentUserId(int32_t userId) 92 { 93 currentUserId_ = userId; 94 return 0; 95 } 96 97 int32_t SetFirewallRules(NetFirewallRuleType type, const std::vector<sptr<NetFirewallBaseRule>> &ruleList, 98 bool isFinish); 99 100 int32_t ClearFirewallRules(NetFirewallRuleType type); 101 SetCallingUid(uint32_t callingUid)102 void SetCallingUid(uint32_t callingUid) 103 { 104 callingUid_ = callingUid; 105 } 106 GetCallingUid()107 uint32_t GetCallingUid() 108 { 109 return callingUid_; 110 } 111 112 int32_t RegisterNetFirewallCallback(const sptr<NetsysNative::INetFirewallCallback> &callback); 113 114 int32_t UnRegisterNetFirewallCallback(const sptr<NetsysNative::INetFirewallCallback> &callback); 115 #endif 116 117 private: 118 DnsParamCache(); 119 120 std::vector<NetManagerStandard::UidRange> vpnUidRanges_; 121 122 int32_t vpnNetId_; 123 124 ffrt::mutex cacheMutex_; 125 126 ffrt::mutex uidRangeMutex_; 127 128 std::atomic_uint defaultNetId_; 129 130 std::map<uint16_t, DnsResolvConfig> serverConfigMap_; 131 132 static std::vector<std::string> SelectNameservers(const std::vector<std::string> &servers); 133 134 #ifdef FEATURE_NET_FIREWALL_ENABLE 135 int32_t GetUserId(int32_t appUid); 136 137 bool GetDnsServersByAppUid(int32_t appUid, std::vector<std::string> &servers); 138 139 void BuildFirewallDomainLsmTrie(const sptr<NetFirewallDomainRule> &rule, const std::string &domain); 140 141 void BuildFirewallDomainMap(const sptr<NetFirewallDomainRule> &rule, const std::string &domain); 142 143 int32_t SetFirewallDnsRules(const std::vector<sptr<NetFirewallDnsRule>> &ruleList); 144 145 int32_t SetFirewallDomainRules(const std::vector<sptr<NetFirewallDomainRule>> &ruleList); 146 147 FirewallRuleAction GetFirewallRuleAction(int32_t appUid, const std::vector<sptr<NetFirewallDomainRule>> &rules); 148 149 bool checkEmpty4InterceptDomain(const std::string &hostName); 150 151 bool IsInterceptDomain(int32_t appUid, const std::string &host); 152 153 void NotifyDomianIntercept(int32_t appUid, const std::string &host); 154 155 std::vector<sptr<NetFirewallDomainRule>> firewallDomainRules_; 156 157 std::vector<sptr<NetFirewallDnsRule>> firewallDnsRules_; 158 159 sptr<NetManagerStandard::InterceptRecord> oldRecord_ = nullptr; 160 161 std::unordered_map<int32_t, std::vector<sptr<NetFirewallDnsRule>>> netFirewallDnsRuleMap_; 162 163 std::unordered_map<std::string, std::vector<sptr<NetFirewallDomainRule>>> netFirewallDomainRulesAllowMap_; 164 165 std::unordered_map<std::string, std::vector<sptr<NetFirewallDomainRule>>> netFirewallDomainRulesDenyMap_; 166 167 std::shared_ptr<NetManagerStandard::SuffixMatchTrie<std::vector<sptr<NetFirewallDomainRule>>>> domainAllowLsmTrie_ = 168 nullptr; 169 170 std::shared_ptr<NetManagerStandard::SuffixMatchTrie<std::vector<sptr<NetFirewallDomainRule>>>> domainDenyLsmTrie_ = 171 nullptr; 172 173 uint32_t callingUid_; 174 175 int32_t currentUserId_ = 0; 176 177 std::vector<sptr<NetsysNative::INetFirewallCallback>> callbacks_; 178 179 FirewallRuleAction firewallDefaultAction_ = FirewallRuleAction::RULE_INVALID; 180 #endif 181 }; 182 } // namespace OHOS::nmd 183 #endif // NETSYS_DNS_PARAM_CACHE_H 184