1 /* 2 * Copyright (c) 2021-2023 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 NET_POLICY_SERVICE_H 17 #define NET_POLICY_SERVICE_H 18 19 #include <atomic> 20 #include <mutex> 21 22 #include "event_runner.h" 23 #include "singleton.h" 24 #include "system_ability.h" 25 #include "system_ability_definition.h" 26 27 #include "net_policy_callback.h" 28 #include "net_policy_event_handler.h" 29 #include "net_policy_firewall.h" 30 #include "net_policy_rule.h" 31 #include "net_policy_service_common.h" 32 #include "net_policy_service_stub.h" 33 #include "net_policy_traffic.h" 34 35 namespace OHOS { 36 namespace NetManagerStandard { 37 #define NET_SYMBOL_VISIBLE __attribute__ ((visibility("default"))) 38 class NET_SYMBOL_VISIBLE NetPolicyService : public SystemAbility, 39 public NetPolicyServiceStub, 40 public std::enable_shared_from_this<NetPolicyService> { 41 DECLARE_DELAYED_SINGLETON(NetPolicyService) 42 DECLARE_SYSTEM_ABILITY(NetPolicyService) 43 44 public: 45 void OnStart() override; 46 void OnStop() override; 47 int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override; 48 49 /** 50 * Set the network policy for the specified UID. 51 * @param uid The specified UID of app. 52 * @param policy The network policy for application. 53 * For details, see {@link NetUidPolicy}. 54 * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 55 */ 56 int32_t SetPolicyByUid(uint32_t uid, uint32_t policy) override; 57 58 /** 59 * Get the network policy of the specified UID. 60 * @param uid The specified UID of app. 61 * @param policy Return this uid's policy. 62 * For details, see {@link NetUidPolicy}. 63 * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 64 */ 65 int32_t GetPolicyByUid(uint32_t uid, uint32_t &policy) override; 66 67 /** 68 * Get the application UIDs of the specified policy. 69 * @param policy the network policy of the current UID of application. 70 * For details, see {@link NetUidPolicy}. 71 * @param uids The list of UIDs 72 * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 73 */ 74 int32_t GetUidsByPolicy(uint32_t policy, std::vector<uint32_t> &uids) override; 75 76 /** 77 * Get the status whether the specified uid app can access the metered network or non-metered network. 78 * @param uid The specified UID of application. 79 * @param metered Indicates metered network or non-metered network. 80 * @param isAllowed Return true means it's allowed to access the network. 81 * Return false means it's not allowed to access the network. 82 * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 83 */ 84 int32_t IsUidNetAllowed(uint32_t uid, bool metered, bool &isAllowed) override; 85 86 /** 87 * Get the status whether the specified uid app can access the specified iface network. 88 * @param uid The specified UID of application. 89 * @param ifaceName Iface name. 90 * @param isAllowed Return true means it's allowed to access the network. 91 * Return false means it's not allowed to access the network. 92 * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 93 */ 94 int32_t IsUidNetAllowed(uint32_t uid, const std::string &ifaceName, bool &isAllowed) override; 95 96 /** 97 * Register network policy change callback. 98 * @param callback Interface type pointer. 99 * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 100 */ 101 int32_t RegisterNetPolicyCallback(const sptr<INetPolicyCallback> &callback) override; 102 103 /** 104 * Unregister network policy change callback. 105 * @param callback Interface type pointer. 106 * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 107 */ 108 int32_t UnregisterNetPolicyCallback(const sptr<INetPolicyCallback> &callback) override; 109 110 /** 111 * Set policies by NetPolicyQuotaPolicy. 112 * @param quotaPolicies The list of network quota policy, {@link NetQuotaPolicy}. 113 * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 114 */ 115 int32_t SetNetQuotaPolicies(const std::vector<NetQuotaPolicy> "aPolicies) override; 116 117 /** 118 * Get network policies. 119 * @param quotaPolicies The list of network quota policy, {@link NetQuotaPolicy}. 120 * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 121 */ 122 int32_t GetNetQuotaPolicies(std::vector<NetQuotaPolicy> "aPolicies) override; 123 124 /** 125 * Reset network policies\rules\quota policies\firewall rules. 126 * @param simId Specify the matched simId of quota policy. 127 * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 128 */ 129 int32_t ResetPolicies(const std::string &simId) override; 130 131 /** 132 * Control if apps can use data on background. 133 * @param allowBackground Allow apps to use data on background. 134 * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 135 */ 136 int32_t SetBackgroundPolicy(bool allowBackground) override; 137 138 /** 139 * Get the status if apps can use data on background. 140 * @param backgroundPolicy True is allowed to use data on background. 141 * False is not allowed to use data on background. 142 * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 143 */ 144 int32_t GetBackgroundPolicy(bool &backgroundPolicy) override; 145 146 /** 147 * Get the background network restriction policy for the specified uid. 148 * @param uid uid The specified UID of application. 149 * @param backgroundPolicyOfUid The specified UID of backgroundPolicy. 150 * For details, see {@link NetBackgroundPolicy}. 151 * @return uint32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 152 */ 153 int32_t GetBackgroundPolicyByUid(uint32_t uid, uint32_t &backgroundPolicyOfUid) override; 154 155 /** 156 * Update the limit or warning remind time of quota policy. 157 * @param netType {@link NetBearType}. 158 * @param simId Specify the matched simId of quota policy when netType is cellular. 159 * @param remindType {@link RemindType}. 160 * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 161 */ 162 int32_t UpdateRemindPolicy(int32_t netType, const std::string &simId, uint32_t remindType) override; 163 164 /** 165 * Set the UID into device idle allow list. 166 * @param uid The specified UID of application. 167 * @param isAllowed The UID is into allowed list or not. 168 * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 169 */ 170 int32_t SetDeviceIdleTrustlist(const std::vector<uint32_t> &uids, bool isAllowed) override; 171 172 /** 173 * Get the allow list of UID in device idle mode. 174 * @param uids The list of UIDs 175 * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 176 */ 177 int32_t GetDeviceIdleTrustlist(std::vector<uint32_t> &uids) override; 178 179 /** 180 * Process network policy in device idle mode. 181 * @param enable Device idle mode is open or not. 182 * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 183 */ 184 int32_t SetDeviceIdlePolicy(bool enable) override; 185 186 /** 187 * Get the allow list of UID in power save mode. 188 * 189 * @param uids The list of UIDs. 190 * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 191 */ 192 int32_t GetPowerSaveTrustlist(std::vector<uint32_t> &uids) override; 193 194 /** 195 * Set the UID into power save allow list. 196 * @param uid The specified UID of application. 197 * @param isAllowed The UID is into allowed list or not. 198 * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 199 */ 200 int32_t SetPowerSaveTrustlist(const std::vector<uint32_t> &uids, bool isAllowed) override; 201 202 /** 203 * Process network policy in Power Save mode. 204 * 205 * @param enable Power save mode is open or not. 206 * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 207 */ 208 int32_t SetPowerSavePolicy(bool enable) override; 209 210 /** 211 * Check if you have permission 212 * 213 * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 214 */ 215 int32_t CheckPermission() override; 216 217 /** 218 * factory reset net policies 219 * 220 * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 221 */ 222 int32_t FactoryResetPolicies() override; 223 224 protected: 225 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 226 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 227 228 private: 229 void Init(); 230 int32_t GetDumpMessage(std::string &message); 231 232 void OnNetSysRestart(); 233 234 private: 235 enum ServiceRunningState { 236 STATE_STOPPED = 0, 237 STATE_RUNNING, 238 }; 239 240 std::shared_ptr<NetPolicyTraffic> netPolicyTraffic_ = nullptr; 241 std::shared_ptr<NetPolicyFirewall> netPolicyFirewall_ = nullptr; 242 std::shared_ptr<NetPolicyRule> netPolicyRule_ = nullptr; 243 std::shared_ptr<NetPolicyFile> netPolicyFile_ = nullptr; 244 ServiceRunningState state_ = ServiceRunningState::STATE_STOPPED; 245 std::shared_ptr<NetPolicyCore> netPolicyCore_ = nullptr; 246 std::shared_ptr<NetPolicyCallback> netPolicyCallback_ = nullptr; 247 sptr<NetPolicyServiceCommon> serviceComm_ = nullptr; 248 std::mutex mutex_; 249 std::vector<uint16_t> monthDay_; 250 251 bool hasSARemoved_ = false; 252 253 private: 254 void RegisterFactoryResetCallback(); 255 256 class FactoryResetCallBack : public IRemoteStub<INetFactoryResetCallback> { 257 public: FactoryResetCallBack(std::shared_ptr<NetPolicyService> netPolicy)258 FactoryResetCallBack(std::shared_ptr<NetPolicyService> netPolicy) 259 { 260 netPolicy_ = netPolicy; 261 } 262 OnNetFactoryReset()263 int32_t OnNetFactoryReset() 264 { 265 if (netPolicy_ != nullptr) { 266 netPolicy_->FactoryResetPolicies(); 267 return NETMANAGER_SUCCESS; 268 } else { 269 return NETMANAGER_ERR_LOCAL_PTR_NULL; 270 } 271 } 272 private: 273 std::shared_ptr<NetPolicyService> netPolicy_ = nullptr; 274 }; 275 sptr<INetFactoryResetCallback> netFactoryResetCallback_ = nullptr; 276 }; 277 } // namespace NetManagerStandard 278 } // namespace OHOS 279 #endif // NET_POLICY_SERVICE_H 280