1b1b8bc3fSopenharmony_ci/* 2b1b8bc3fSopenharmony_ci * Copyright (c) 2022-2023 Huawei Device Co., Ltd. 3b1b8bc3fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4b1b8bc3fSopenharmony_ci * you may not use this file except in compliance with the License. 5b1b8bc3fSopenharmony_ci * You may obtain a copy of the License at 6b1b8bc3fSopenharmony_ci * 7b1b8bc3fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8b1b8bc3fSopenharmony_ci * 9b1b8bc3fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10b1b8bc3fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11b1b8bc3fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12b1b8bc3fSopenharmony_ci * See the License for the specific language governing permissions and 13b1b8bc3fSopenharmony_ci * limitations under the License. 14b1b8bc3fSopenharmony_ci */ 15b1b8bc3fSopenharmony_ci 16b1b8bc3fSopenharmony_ci#ifndef NET_POLICY_RULE_H 17b1b8bc3fSopenharmony_ci#define NET_POLICY_RULE_H 18b1b8bc3fSopenharmony_ci 19b1b8bc3fSopenharmony_ci#include <map> 20b1b8bc3fSopenharmony_ci 21b1b8bc3fSopenharmony_ci#include "net_policy_base.h" 22b1b8bc3fSopenharmony_ci#include "netmanager_base_common_utils.h" 23b1b8bc3fSopenharmony_ci#include "net_access_policy.h" 24b1b8bc3fSopenharmony_ci 25b1b8bc3fSopenharmony_cinamespace OHOS { 26b1b8bc3fSopenharmony_cinamespace NetManagerStandard { 27b1b8bc3fSopenharmony_ciconstexpr uint32_t POLICY_TRANS_CONDITION_MASK = 0b11111111110000000000; 28b1b8bc3fSopenharmony_ciconstexpr uint32_t POLICY_TRANS_RULE_MASK = 0b00000000001111111000; 29b1b8bc3fSopenharmony_ciconstexpr uint32_t POLICY_TRANS_NET_CTRL_MASK = 0b00000000000000000111; 30b1b8bc3fSopenharmony_ciconstexpr u_int8_t CONDITION_START_BIT = 10; 31b1b8bc3fSopenharmony_ciconstexpr u_int8_t RULE_START_BIT = 3; 32b1b8bc3fSopenharmony_ci 33b1b8bc3fSopenharmony_cienum PolicyTransCtrl { 34b1b8bc3fSopenharmony_ci POLICY_TRANS_CTRL_NONE = 0b000, 35b1b8bc3fSopenharmony_ci POLICY_TRANS_CTRL_ADD_DENIEDLIST = 0b010, 36b1b8bc3fSopenharmony_ci POLICY_TRANS_CTRL_ADD_ALLOWEDLIST = 0b100, 37b1b8bc3fSopenharmony_ci POLICY_TRANS_CTRL_REMOVE_ALL = 0b001, 38b1b8bc3fSopenharmony_ci}; 39b1b8bc3fSopenharmony_ci 40b1b8bc3fSopenharmony_cistruct UidPolicyRule { 41b1b8bc3fSopenharmony_ci uint32_t policy_ = 0; 42b1b8bc3fSopenharmony_ci uint32_t rule_ = 1 << 7; 43b1b8bc3fSopenharmony_ci uint32_t netsys_ = 7; 44b1b8bc3fSopenharmony_ci}; 45b1b8bc3fSopenharmony_ci 46b1b8bc3fSopenharmony_ciclass NetPolicyRule : public NetPolicyBase { 47b1b8bc3fSopenharmony_cipublic: 48b1b8bc3fSopenharmony_ci NetPolicyRule(); 49b1b8bc3fSopenharmony_ci void Init(); 50b1b8bc3fSopenharmony_ci void HandleEvent(int32_t eventId, const std::shared_ptr<PolicyEvent> &policyEvent); 51b1b8bc3fSopenharmony_ci 52b1b8bc3fSopenharmony_ci /** 53b1b8bc3fSopenharmony_ci * Transform policy to rule and netsys-control. 54b1b8bc3fSopenharmony_ci * 55b1b8bc3fSopenharmony_ci * @param uid The UID of application. 56b1b8bc3fSopenharmony_ci * @param policy See {@link NetUidPolicy}. 57b1b8bc3fSopenharmony_ci * @return Returns 0 success. Otherwise fail. 58b1b8bc3fSopenharmony_ci * @permission ohos.permission.CONNECTIVITY_INTERNAL 59b1b8bc3fSopenharmony_ci * @systemapi Hide this for inner system use. 60b1b8bc3fSopenharmony_ci */ 61b1b8bc3fSopenharmony_ci int32_t TransPolicyToRule(uint32_t uid, uint32_t policy); 62b1b8bc3fSopenharmony_ci 63b1b8bc3fSopenharmony_ci /** 64b1b8bc3fSopenharmony_ci * Get the status whether the specified uid app can access the metered network or non-metered network. 65b1b8bc3fSopenharmony_ci * 66b1b8bc3fSopenharmony_ci * @param uid The specified UID of application. 67b1b8bc3fSopenharmony_ci * @param metered Indicates meterd network or non-metered network. 68b1b8bc3fSopenharmony_ci * @param isAllowed Return true means it's allowed to access the network. 69b1b8bc3fSopenharmony_ci * Return false means it's not allowed to access the network. 70b1b8bc3fSopenharmony_ci * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 71b1b8bc3fSopenharmony_ci * @permission ohos.permission.CONNECTIVITY_INTERNAL 72b1b8bc3fSopenharmony_ci * @systemapi Hide this for inner system use. 73b1b8bc3fSopenharmony_ci */ 74b1b8bc3fSopenharmony_ci int32_t IsUidNetAllowed(uint32_t uid, bool metered, bool &isAllowed); 75b1b8bc3fSopenharmony_ci 76b1b8bc3fSopenharmony_ci /** 77b1b8bc3fSopenharmony_ci * Get the network policy of the specified UID. 78b1b8bc3fSopenharmony_ci * 79b1b8bc3fSopenharmony_ci * @param uid The specified UID of app. 80b1b8bc3fSopenharmony_ci * @param policy Return this uid's policy. 81b1b8bc3fSopenharmony_ci * For details, see {@link NetUidPolicy}. 82b1b8bc3fSopenharmony_ci * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 83b1b8bc3fSopenharmony_ci * @permission ohos.permission.CONNECTIVITY_INTERNAL 84b1b8bc3fSopenharmony_ci * @systemapi Hide this for inner system use. 85b1b8bc3fSopenharmony_ci */ 86b1b8bc3fSopenharmony_ci int32_t GetPolicyByUid(uint32_t uid, uint32_t &policy); 87b1b8bc3fSopenharmony_ci 88b1b8bc3fSopenharmony_ci /** 89b1b8bc3fSopenharmony_ci * Get the application UIDs of the specified policy. 90b1b8bc3fSopenharmony_ci * 91b1b8bc3fSopenharmony_ci * @param policy the network policy of the current UID of application. 92b1b8bc3fSopenharmony_ci * @param uids The list of UIDs. 93b1b8bc3fSopenharmony_ci * For details, see {@link NetUidPolicy}. 94b1b8bc3fSopenharmony_ci * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 95b1b8bc3fSopenharmony_ci * @permission ohos.permission.CONNECTIVITY_INTERNAL 96b1b8bc3fSopenharmony_ci * @systemapi Hide this for inner system use. 97b1b8bc3fSopenharmony_ci */ 98b1b8bc3fSopenharmony_ci int32_t GetUidsByPolicy(uint32_t policy, std::vector<uint32_t> &uids); 99b1b8bc3fSopenharmony_ci 100b1b8bc3fSopenharmony_ci /** 101b1b8bc3fSopenharmony_ci * Reset network policies and rules. 102b1b8bc3fSopenharmony_ci * 103b1b8bc3fSopenharmony_ci * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 104b1b8bc3fSopenharmony_ci * @permission ohos.permission.CONNECTIVITY_INTERNAL 105b1b8bc3fSopenharmony_ci * @systemapi Hide this for inner system use. 106b1b8bc3fSopenharmony_ci */ 107b1b8bc3fSopenharmony_ci int32_t ResetPolicies(); 108b1b8bc3fSopenharmony_ci 109b1b8bc3fSopenharmony_ci /** 110b1b8bc3fSopenharmony_ci * Control if apps can use data on background. 111b1b8bc3fSopenharmony_ci * 112b1b8bc3fSopenharmony_ci * @param allow Allow apps to use data on background. 113b1b8bc3fSopenharmony_ci * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 114b1b8bc3fSopenharmony_ci * @permission ohos.permission.CONNECTIVITY_INTERNAL 115b1b8bc3fSopenharmony_ci * @systemapi Hide this for inner system use. 116b1b8bc3fSopenharmony_ci */ 117b1b8bc3fSopenharmony_ci int32_t SetBackgroundPolicy(bool allow); 118b1b8bc3fSopenharmony_ci 119b1b8bc3fSopenharmony_ci /** 120b1b8bc3fSopenharmony_ci * Get the background network restriction policy for the specified uid. 121b1b8bc3fSopenharmony_ci * 122b1b8bc3fSopenharmony_ci * @param uid The specified UID of application. 123b1b8bc3fSopenharmony_ci * @param backgroundPolicyOfUid The specified UID of backgroundPolicy. 124b1b8bc3fSopenharmony_ci * For details, see {@link NetBackgroundPolicy}. 125b1b8bc3fSopenharmony_ci * @return uint32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 126b1b8bc3fSopenharmony_ci * @permission ohos.permission.CONNECTIVITY_INTERNAL 127b1b8bc3fSopenharmony_ci * @systemapi Hide this for inner system use. 128b1b8bc3fSopenharmony_ci */ 129b1b8bc3fSopenharmony_ci int32_t GetBackgroundPolicyByUid(uint32_t uid, uint32_t &backgroundPolicyOfUid); 130b1b8bc3fSopenharmony_ci 131b1b8bc3fSopenharmony_ci /** 132b1b8bc3fSopenharmony_ci * Get the status if apps can use data on background. 133b1b8bc3fSopenharmony_ci * @param backgroundPolicy True is allowed to use data on background. 134b1b8bc3fSopenharmony_ci * False is not allowed to use data on background. 135b1b8bc3fSopenharmony_ci * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 136b1b8bc3fSopenharmony_ci * @permission ohos.permission.CONNECTIVITY_INTERNAL 137b1b8bc3fSopenharmony_ci * @systemapi Hide this for inner system use. 138b1b8bc3fSopenharmony_ci */ 139b1b8bc3fSopenharmony_ci int32_t GetBackgroundPolicy(bool &backgroundPolicy); 140b1b8bc3fSopenharmony_ci 141b1b8bc3fSopenharmony_ci /** 142b1b8bc3fSopenharmony_ci * Get the Dump Message object. 143b1b8bc3fSopenharmony_ci */ 144b1b8bc3fSopenharmony_ci void GetDumpMessage(std::string &message); 145b1b8bc3fSopenharmony_ci 146b1b8bc3fSopenharmony_ci // When system's mode status is changed, do this function. 147b1b8bc3fSopenharmony_ci void TransPolicyToRule(); 148b1b8bc3fSopenharmony_ci 149b1b8bc3fSopenharmony_ci /** 150b1b8bc3fSopenharmony_ci * Set the policy to access the network of the specified application. 151b1b8bc3fSopenharmony_ci * 152b1b8bc3fSopenharmony_ci * @param uid The specified UID of application. 153b1b8bc3fSopenharmony_ci * @param policy The network access policy of application, {@link NetworkAccessPolicy}. 154b1b8bc3fSopenharmony_ci * @param reconfirmFlag true means a reconfirm diaglog trigger while policy deny network access. 155b1b8bc3fSopenharmony_ci * @param isBroker true means the broker application. 156b1b8bc3fSopenharmony_ci * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 157b1b8bc3fSopenharmony_ci * @permission ohos.permission.CONNECTIVITY_INTERNAL 158b1b8bc3fSopenharmony_ci * @systemapi Hide this for inner system use. 159b1b8bc3fSopenharmony_ci */ 160b1b8bc3fSopenharmony_ci int32_t SetNetworkAccessPolicy(uint32_t uid, NetworkAccessPolicy Policy, bool reconfirmFlag, bool isBroker); 161b1b8bc3fSopenharmony_ci 162b1b8bc3fSopenharmony_ci /** 163b1b8bc3fSopenharmony_ci * Delete the policy to access the network of the specified application from map. 164b1b8bc3fSopenharmony_ci * 165b1b8bc3fSopenharmony_ci * @param uid The specified UID of application. 166b1b8bc3fSopenharmony_ci * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 167b1b8bc3fSopenharmony_ci * @permission ohos.permission.CONNECTIVITY_INTERNAL 168b1b8bc3fSopenharmony_ci * @systemapi Hide this for inner system use. 169b1b8bc3fSopenharmony_ci */ 170b1b8bc3fSopenharmony_ci int32_t DeleteNetworkAccessPolicy(uint32_t uid); 171b1b8bc3fSopenharmony_ci 172b1b8bc3fSopenharmony_ci /** 173b1b8bc3fSopenharmony_ci * Set NIC Traffic allowed or disallowed 174b1b8bc3fSopenharmony_ci * 175b1b8bc3fSopenharmony_ci * @param ifaceNames ifaceNames 176b1b8bc3fSopenharmony_ci * @param status true for allowed, false for disallowed 177b1b8bc3fSopenharmony_ci * @return Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}. 178b1b8bc3fSopenharmony_ci */ 179b1b8bc3fSopenharmony_ci int32_t PolicySetNicTrafficAllowed(const std::vector<std::string> &ifaceNames, bool status); 180b1b8bc3fSopenharmony_ci 181b1b8bc3fSopenharmony_ciprivate: 182b1b8bc3fSopenharmony_ci void NetsysCtrl(uint32_t uid, uint32_t netsysCtrl); 183b1b8bc3fSopenharmony_ci void TransConditionToRuleAndNetsys(uint32_t policyCondition, uint32_t uid, uint32_t policy); 184b1b8bc3fSopenharmony_ci uint32_t MoveToConditionBit(uint32_t value); 185b1b8bc3fSopenharmony_ci uint32_t MoveToRuleBit(uint32_t value); 186b1b8bc3fSopenharmony_ci uint32_t ChangePolicyToPolicyTransitionCondition(uint32_t policy); 187b1b8bc3fSopenharmony_ci uint32_t BuildTransCondition(uint32_t uid, uint32_t policy); 188b1b8bc3fSopenharmony_ci uint32_t GetMatchTransCondition(uint32_t policyCondition); 189b1b8bc3fSopenharmony_ci void ProcessCtrlNone(uint32_t uid); 190b1b8bc3fSopenharmony_ci void ProcessCtrlAddAllowedList(uint32_t uid); 191b1b8bc3fSopenharmony_ci 192b1b8bc3fSopenharmony_ci // When a uid add into some forbidden list, do this function. 193b1b8bc3fSopenharmony_ci void TransPolicyToRule(uint32_t uid); 194b1b8bc3fSopenharmony_ci bool IsIdleMode(); 195b1b8bc3fSopenharmony_ci bool InIdleAllowedList(uint32_t uid); 196b1b8bc3fSopenharmony_ci bool IsLimitByAdmin(); 197b1b8bc3fSopenharmony_ci bool IsForeground(uint32_t uid); 198b1b8bc3fSopenharmony_ci bool IsPowerSave(); 199b1b8bc3fSopenharmony_ci bool InPowerSaveAllowedList(uint32_t uid); 200b1b8bc3fSopenharmony_ci bool IsLimitedBackground(); 201b1b8bc3fSopenharmony_ci void DeleteUid(uint32_t uid); 202b1b8bc3fSopenharmony_ci bool IsValidNetPolicy(uint32_t policy); 203b1b8bc3fSopenharmony_ci void UpdateForegroundUidList(uint32_t uid, bool isForeground); 204b1b8bc3fSopenharmony_ci 205b1b8bc3fSopenharmony_ciprivate: 206b1b8bc3fSopenharmony_ci std::map<uint32_t, UidPolicyRule> uidPolicyRules_; 207b1b8bc3fSopenharmony_ci bool backgroundAllow_ = true; 208b1b8bc3fSopenharmony_ci bool deviceIdleMode_ = false; 209b1b8bc3fSopenharmony_ci bool powerSaveMode_ = false; 210b1b8bc3fSopenharmony_ci std::set<uint32_t> deviceIdleAllowedList_; 211b1b8bc3fSopenharmony_ci std::set<uint32_t> powerSaveAllowedList_; 212b1b8bc3fSopenharmony_ci std::set<uint32_t> foregroundUidList_; 213b1b8bc3fSopenharmony_ci std::mutex foregroundUidListMutex_; 214b1b8bc3fSopenharmony_ci 215b1b8bc3fSopenharmony_ciprivate: 216b1b8bc3fSopenharmony_ci /** 217b1b8bc3fSopenharmony_ci * The map for transforming conditions to net rule and netsys control. 218b1b8bc3fSopenharmony_ci * 219b1b8bc3fSopenharmony_ci * Example: 220b1b8bc3fSopenharmony_ci * bit 11~20 bit 4~10 bit 1~3 221b1b8bc3fSopenharmony_ci * 0000001100 0010000 110 222b1b8bc3fSopenharmony_ci * Condition Rule Netsys control 223b1b8bc3fSopenharmony_ci * 224b1b8bc3fSopenharmony_ci * Condition: see {@link enum PolicyTransCondition} 225b1b8bc3fSopenharmony_ci * Rule: see {@link enum NetUidRule} 226b1b8bc3fSopenharmony_ci * Netsys control: see {@link enum NetsysOperation} 227b1b8bc3fSopenharmony_ci * 228b1b8bc3fSopenharmony_ci * Transform Flow: 229b1b8bc3fSopenharmony_ci * 1. According to the status of system(such as device idle mode or power save mode) 230b1b8bc3fSopenharmony_ci * and the net policy of uid, construct the Condition by bit operations. 231b1b8bc3fSopenharmony_ci * 2. Find the matched Condition in this map. 232b1b8bc3fSopenharmony_ci * 3. Get the rule bits and netsys-control bits from the matched Condition. 233b1b8bc3fSopenharmony_ci * 4. Process the corresponding operations. 234b1b8bc3fSopenharmony_ci */ 235b1b8bc3fSopenharmony_ci static inline const std::vector<uint32_t> POLICY_TRANS_MAP = { 236b1b8bc3fSopenharmony_ci 0b00011000000100000000, 0b10000000000000100010, 0b00010000001000000000, 0b00000010100000010100, 237b1b8bc3fSopenharmony_ci 0b00000011000000001100, 0b00000010010000001100, 0b01000000001000000000, 0b00000100100000010100, 238b1b8bc3fSopenharmony_ci 0b00000100010000100010, 0b00000000010000000001, 0b00000001000000100010, 0b00000000000000000001, 239b1b8bc3fSopenharmony_ci }; 240b1b8bc3fSopenharmony_ci}; 241b1b8bc3fSopenharmony_ci} // namespace NetManagerStandard 242b1b8bc3fSopenharmony_ci} // namespace OHOS 243b1b8bc3fSopenharmony_ci#endif // NET_POLICY_RULE_H 244