1 /*
2 * Copyright (c) 2021-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 #include "net_policy_service.h"
17
18 #include <algorithm>
19
20 #include "system_ability_definition.h"
21
22 #include "net_manager_center.h"
23 #include "net_manager_constants.h"
24 #include "net_mgr_log_wrapper.h"
25 #include "net_policy_constants.h"
26 #include "net_policy_core.h"
27 #include "net_policy_file.h"
28 #include "net_policy_inner_define.h"
29 #include "net_quota_policy.h"
30 #include "net_settings.h"
31 #include "netmanager_base_permission.h"
32
33 namespace OHOS {
34 namespace NetManagerStandard {
35 static std::atomic<bool>
36 g_RegisterToService(SystemAbility::MakeAndRegisterAbility(DelayedSingleton<NetPolicyService>::GetInstance().get()));
37
NetPolicyService()38 NetPolicyService::NetPolicyService()
39 : SystemAbility(COMM_NET_POLICY_MANAGER_SYS_ABILITY_ID, true), state_(STATE_STOPPED)
40 {
41 }
42
43 NetPolicyService::~NetPolicyService() = default;
44
OnStart()45 void NetPolicyService::OnStart()
46 {
47 NETMGR_LOG_I("NetPolicyService OnStart");
48 if (state_ == STATE_RUNNING) {
49 NETMGR_LOG_W("NetPolicyService already start.");
50 return;
51 }
52
53 if (!g_RegisterToService) {
54 g_RegisterToService =
55 SystemAbility::MakeAndRegisterAbility(DelayedSingleton<NetPolicyService>::GetInstance().get());
56 if (!g_RegisterToService) {
57 NETMGR_LOG_E("Register to local sa manager failed again, give up.");
58 return;
59 }
60 }
61 if (!Publish(DelayedSingleton<NetPolicyService>::GetInstance().get())) {
62 NETMGR_LOG_E("Register to sa manager failed");
63 return;
64 }
65
66 state_ = STATE_RUNNING;
67 Init();
68 }
69
OnStop()70 void NetPolicyService::OnStop()
71 {
72 runner_->Stop();
73 handler_.reset();
74 runner_.reset();
75 netPolicyCore_.reset();
76 netPolicyCallback_.reset();
77 netPolicyTraffic_.reset();
78 netPolicyFirewall_.reset();
79 netPolicyRule_.reset();
80 state_ = STATE_STOPPED;
81 g_RegisterToService = false;
82 }
83
Dump(int32_t fd,const std::vector<std::u16string> & args)84 int32_t NetPolicyService::Dump(int32_t fd, const std::vector<std::u16string> &args)
85 {
86 NETMGR_LOG_D("Start policy Dump, fd: %{public}d", fd);
87 std::string result;
88 GetDumpMessage(result);
89 int32_t ret = dprintf(fd, "%s\n", result.c_str());
90 return ret < 0 ? static_cast<int32_t>(NETMANAGER_ERR_PARAMETER_ERROR) : NETMANAGER_SUCCESS;
91 }
92
Init()93 void NetPolicyService::Init()
94 {
95 NETMGR_LOG_D("NetPolicyService Init");
96 handler_->PostTask(
97 [this]() {
98 serviceComm_ = (std::make_unique<NetPolicyServiceCommon>()).release();
99 NetManagerCenter::GetInstance().RegisterPolicyService(serviceComm_);
100 netPolicyCore_ = DelayedSingleton<NetPolicyCore>::GetInstance();
101 netPolicyCallback_ = DelayedSingleton<NetPolicyCallback>::GetInstance();
102 netPolicyTraffic_ = netPolicyCore_->CreateCore<NetPolicyTraffic>();
103 netPolicyFirewall_ = netPolicyCore_->CreateCore<NetPolicyFirewall>();
104 netPolicyRule_ = netPolicyCore_->CreateCore<NetPolicyRule>();
105 },
106 AppExecFwk::EventQueue::Priority::HIGH);
107 }
108
SetPolicyByUid(uint32_t uid,uint32_t policy)109 int32_t NetPolicyService::SetPolicyByUid(uint32_t uid, uint32_t policy)
110 {
111 NETMGR_LOG_D("SetPolicyByUid uid[%{public}d] policy[%{public}d]", uid, policy);
112 return netPolicyRule_->TransPolicyToRule(uid, policy);
113 }
114
GetPolicyByUid(uint32_t uid,uint32_t & policy)115 int32_t NetPolicyService::GetPolicyByUid(uint32_t uid, uint32_t &policy)
116 {
117 NETMGR_LOG_D("GetPolicyByUid uid[%{public}d]", uid);
118 return netPolicyRule_->GetPolicyByUid(uid, policy);
119 }
120
GetUidsByPolicy(uint32_t policy,std::vector<uint32_t> & uids)121 int32_t NetPolicyService::GetUidsByPolicy(uint32_t policy, std::vector<uint32_t> &uids)
122 {
123 NETMGR_LOG_D("GetUidsByPolicy policy[%{public}d]", policy);
124 return netPolicyRule_->GetUidsByPolicy(policy, uids);
125 }
126
IsUidNetAllowed(uint32_t uid,bool metered,bool & isAllowed)127 int32_t NetPolicyService::IsUidNetAllowed(uint32_t uid, bool metered, bool &isAllowed)
128 {
129 NETMGR_LOG_D("IsUidNetAllowed uid[%{public}d metered[%{public}d]", uid, metered);
130 if (NetSettings::GetInstance().IsSystem(uid)) {
131 isAllowed = true;
132 return NETMANAGER_SUCCESS;
133 }
134 if (netPolicyRule_ != nullptr) {
135 return netPolicyRule_->IsUidNetAllowed(uid, metered, isAllowed);
136 }
137 return NETMANAGER_ERR_LOCAL_PTR_NULL;
138 }
139
IsUidNetAllowed(uint32_t uid,const std::string & ifaceName,bool & isAllowed)140 int32_t NetPolicyService::IsUidNetAllowed(uint32_t uid, const std::string &ifaceName, bool &isAllowed)
141 {
142 NETMGR_LOG_D("IsUidNetAllowed uid[%{public}d ifaceName[%{public}s]", uid, ifaceName.c_str());
143 const auto &vec = netPolicyTraffic_->GetMeteredIfaces();
144 if (std::find(vec.begin(), vec.end(), ifaceName) != vec.end()) {
145 return IsUidNetAllowed(uid, true, isAllowed);
146 }
147 return IsUidNetAllowed(uid, false, isAllowed);
148 }
149
RegisterNetPolicyCallback(const sptr<INetPolicyCallback> & callback)150 int32_t NetPolicyService::RegisterNetPolicyCallback(const sptr<INetPolicyCallback> &callback)
151 {
152 NETMGR_LOG_D("RegisterNetPolicyCallback");
153 if (callback == nullptr) {
154 NETMGR_LOG_E("RegisterNetPolicyCallback parameter callback is null");
155 return NETMANAGER_ERR_LOCAL_PTR_NULL;
156 }
157
158 return netPolicyCallback_->RegisterNetPolicyCallback(callback);
159 }
160
UnregisterNetPolicyCallback(const sptr<INetPolicyCallback> & callback)161 int32_t NetPolicyService::UnregisterNetPolicyCallback(const sptr<INetPolicyCallback> &callback)
162 {
163 NETMGR_LOG_D("UnregisterNetPolicyCallback");
164 if (callback == nullptr) {
165 NETMGR_LOG_E("UnregisterNetPolicyCallback parameter callback is null");
166 return NETMANAGER_ERR_LOCAL_PTR_NULL;
167 }
168
169 return netPolicyCallback_->UnregisterNetPolicyCallback(callback);
170 }
171
SetNetQuotaPolicies(const std::vector<NetQuotaPolicy> & quotaPolicies)172 int32_t NetPolicyService::SetNetQuotaPolicies(const std::vector<NetQuotaPolicy> "aPolicies)
173 {
174 NETMGR_LOG_D("SetNetQuotaPolicies quotaPolicySize[%{public}zd]", quotaPolicies.size());
175 return netPolicyTraffic_->UpdateQuotaPolicies(quotaPolicies);
176 }
177
GetNetQuotaPolicies(std::vector<NetQuotaPolicy> & quotaPolicies)178 int32_t NetPolicyService::GetNetQuotaPolicies(std::vector<NetQuotaPolicy> "aPolicies)
179 {
180 NETMGR_LOG_D("GetNetQuotaPolicies begin");
181 return netPolicyTraffic_->GetNetQuotaPolicies(quotaPolicies);
182 }
183
ResetPolicies(const std::string & iccid)184 int32_t NetPolicyService::ResetPolicies(const std::string &iccid)
185 {
186 NETMGR_LOG_D("ResetPolicies begin");
187 if (netPolicyRule_ != nullptr && netPolicyFirewall_ != nullptr && netPolicyTraffic_ != nullptr) {
188 netPolicyRule_->ResetPolicies();
189 netPolicyFirewall_->ResetPolicies();
190 netPolicyTraffic_->ResetPolicies(iccid);
191 return NETMANAGER_SUCCESS;
192 }
193 return NETMANAGER_ERR_LOCAL_PTR_NULL;
194 }
195
SetBackgroundPolicy(bool allow)196 int32_t NetPolicyService::SetBackgroundPolicy(bool allow)
197 {
198 NETMGR_LOG_D("SetBackgroundPolicy allow[%{public}d]", allow);
199 return netPolicyRule_->SetBackgroundPolicy(allow);
200 }
201
GetBackgroundPolicy(bool & backgroundPolicy)202 int32_t NetPolicyService::GetBackgroundPolicy(bool &backgroundPolicy)
203 {
204 NETMGR_LOG_D("GetBackgroundPolicy begin");
205 return netPolicyRule_->GetBackgroundPolicy(backgroundPolicy);
206 }
207
GetBackgroundPolicyByUid(uint32_t uid,uint32_t & backgroundPolicyOfUid)208 int32_t NetPolicyService::GetBackgroundPolicyByUid(uint32_t uid, uint32_t &backgroundPolicyOfUid)
209 {
210 NETMGR_LOG_D("GetBackgroundPolicyByUid uid[%{public}d]", uid);
211 return netPolicyRule_->GetBackgroundPolicyByUid(uid, backgroundPolicyOfUid);
212 }
213
UpdateRemindPolicy(int32_t netType,const std::string & iccid,uint32_t remindType)214 int32_t NetPolicyService::UpdateRemindPolicy(int32_t netType, const std::string &iccid, uint32_t remindType)
215 {
216 NETMGR_LOG_D("UpdateRemindPolicy start");
217 return netPolicyTraffic_->UpdateRemindPolicy(netType, iccid, remindType);
218 }
219
SetDeviceIdleAllowedList(uint32_t uid,bool isAllowed)220 int32_t NetPolicyService::SetDeviceIdleAllowedList(uint32_t uid, bool isAllowed)
221 {
222 NETMGR_LOG_D("SetDeviceIdleAllowedList info: uid[%{public}d] isAllowed[%{public}d]", uid, isAllowed);
223 return netPolicyFirewall_->SetDeviceIdleAllowedList(uid, isAllowed);
224 }
225
GetDeviceIdleAllowedList(std::vector<uint32_t> & uids)226 int32_t NetPolicyService::GetDeviceIdleAllowedList(std::vector<uint32_t> &uids)
227 {
228 NETMGR_LOG_D("GetDeviceIdleAllowedList start");
229 return netPolicyFirewall_->GetDeviceIdleAllowedList(uids);
230 }
231
SetDeviceIdlePolicy(bool enable)232 int32_t NetPolicyService::SetDeviceIdlePolicy(bool enable)
233 {
234 NETMGR_LOG_D("SetDeviceIdlePolicy enable[%{public}d]", enable);
235 return netPolicyFirewall_->UpdateDeviceIdlePolicy(enable);
236 }
237
GetDumpMessage(std::string & message)238 int32_t NetPolicyService::GetDumpMessage(std::string &message)
239 {
240 netPolicyRule_->GetDumpMessage(message);
241 netPolicyTraffic_->GetDumpMessage(message);
242 return NETMANAGER_SUCCESS;
243 }
244 } // namespace NetManagerStandard
245 } // namespace OHOS
246