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 #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> g_RegisterToService(
36 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 ? 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 if (netPolicyCallback_ == nullptr) {
159 NETMGR_LOG_E("netPolicyCallback_ is null");
160 return NETMANAGER_ERR_LOCAL_PTR_NULL;
161 }
162
163 return netPolicyCallback_->RegisterNetPolicyCallbackAsync(callback);
164 }
165
UnregisterNetPolicyCallback(const sptr<INetPolicyCallback> & callback)166 int32_t NetPolicyService::UnregisterNetPolicyCallback(const sptr<INetPolicyCallback> &callback)
167 {
168 NETMGR_LOG_D("UnregisterNetPolicyCallback");
169 if (callback == nullptr) {
170 NETMGR_LOG_E("UnregisterNetPolicyCallback parameter callback is null");
171 return NETMANAGER_ERR_LOCAL_PTR_NULL;
172 }
173
174 if (netPolicyCallback_ == nullptr) {
175 NETMGR_LOG_E("netPolicyCallback_ is null");
176 return NETMANAGER_ERR_LOCAL_PTR_NULL;
177 }
178 return netPolicyCallback_->UnregisterNetPolicyCallbackAsync(callback);
179 }
180
SetNetQuotaPolicies(const std::vector<NetQuotaPolicy> & quotaPolicies)181 int32_t NetPolicyService::SetNetQuotaPolicies(const std::vector<NetQuotaPolicy> "aPolicies)
182 {
183 NETMGR_LOG_D("SetNetQuotaPolicies quotaPolicySize[%{public}zd]", quotaPolicies.size());
184 return netPolicyTraffic_->UpdateQuotaPolicies(quotaPolicies);
185 }
186
GetNetQuotaPolicies(std::vector<NetQuotaPolicy> & quotaPolicies)187 int32_t NetPolicyService::GetNetQuotaPolicies(std::vector<NetQuotaPolicy> "aPolicies)
188 {
189 NETMGR_LOG_D("GetNetQuotaPolicies begin");
190 return netPolicyTraffic_->GetNetQuotaPolicies(quotaPolicies);
191 }
192
ResetPolicies(const std::string & simId)193 int32_t NetPolicyService::ResetPolicies(const std::string &simId)
194 {
195 NETMGR_LOG_D("ResetPolicies begin");
196 if (netPolicyRule_ != nullptr && netPolicyFirewall_ != nullptr && netPolicyTraffic_ != nullptr) {
197 netPolicyRule_->ResetPolicies();
198 netPolicyFirewall_->ResetPolicies();
199 netPolicyTraffic_->ResetPolicies(simId);
200 return NETMANAGER_SUCCESS;
201 }
202 return NETMANAGER_ERR_LOCAL_PTR_NULL;
203 }
204
SetBackgroundPolicy(bool allow)205 int32_t NetPolicyService::SetBackgroundPolicy(bool allow)
206 {
207 NETMGR_LOG_D("SetBackgroundPolicy allow[%{public}d]", allow);
208 return netPolicyRule_->SetBackgroundPolicy(allow);
209 }
210
GetBackgroundPolicy(bool & backgroundPolicy)211 int32_t NetPolicyService::GetBackgroundPolicy(bool &backgroundPolicy)
212 {
213 NETMGR_LOG_D("GetBackgroundPolicy begin");
214 return netPolicyRule_->GetBackgroundPolicy(backgroundPolicy);
215 }
216
GetBackgroundPolicyByUid(uint32_t uid,uint32_t & backgroundPolicyOfUid)217 int32_t NetPolicyService::GetBackgroundPolicyByUid(uint32_t uid, uint32_t &backgroundPolicyOfUid)
218 {
219 NETMGR_LOG_D("GetBackgroundPolicyByUid uid[%{public}d]", uid);
220 return netPolicyRule_->GetBackgroundPolicyByUid(uid, backgroundPolicyOfUid);
221 }
222
UpdateRemindPolicy(int32_t netType,const std::string & simId,uint32_t remindType)223 int32_t NetPolicyService::UpdateRemindPolicy(int32_t netType, const std::string &simId, uint32_t remindType)
224 {
225 NETMGR_LOG_D("UpdateRemindPolicy start");
226 return netPolicyTraffic_->UpdateRemindPolicy(netType, simId, remindType);
227 }
228
SetDeviceIdleTrustlist(const std::vector<uint32_t> & uids,bool isAllowed)229 int32_t NetPolicyService::SetDeviceIdleTrustlist(const std::vector<uint32_t> &uids, bool isAllowed)
230 {
231 NETMGR_LOG_D("SetDeviceIdleTrustlist start");
232 return netPolicyFirewall_->SetDeviceIdleTrustlist(uids, isAllowed);
233 }
234
GetDeviceIdleTrustlist(std::vector<uint32_t> & uids)235 int32_t NetPolicyService::GetDeviceIdleTrustlist(std::vector<uint32_t> &uids)
236 {
237 NETMGR_LOG_D("GetDeviceIdleTrustlist start");
238 return netPolicyFirewall_->GetDeviceIdleTrustlist(uids);
239 }
240
SetDeviceIdlePolicy(bool enable)241 int32_t NetPolicyService::SetDeviceIdlePolicy(bool enable)
242 {
243 NETMGR_LOG_D("SetDeviceIdlePolicy enable[%{public}d]", enable);
244 return netPolicyFirewall_->UpdateDeviceIdlePolicy(enable);
245 }
246
GetPowerSaveTrustlist(std::vector<uint32_t> & uids)247 int32_t NetPolicyService::GetPowerSaveTrustlist(std::vector<uint32_t> &uids)
248 {
249 NETMGR_LOG_D("GetPowerSaveTrustlist start");
250 return netPolicyFirewall_->GetPowerSaveTrustlist(uids);
251 }
252
SetPowerSaveTrustlist(const std::vector<uint32_t> & uids,bool isAllowed)253 int32_t NetPolicyService::SetPowerSaveTrustlist(const std::vector<uint32_t> &uids, bool isAllowed)
254 {
255 NETMGR_LOG_D("SetPowerSaveTrustlist start");
256 return netPolicyFirewall_->SetPowerSaveTrustlist(uids, isAllowed);
257 }
258
SetPowerSavePolicy(bool enable)259 int32_t NetPolicyService::SetPowerSavePolicy(bool enable)
260 {
261 NETMGR_LOG_D("SetPowerSavePolicy enable[%{public}d]", enable);
262 return netPolicyFirewall_->UpdatePowerSavePolicy(enable);
263 }
264
GetDumpMessage(std::string & message)265 int32_t NetPolicyService::GetDumpMessage(std::string &message)
266 {
267 netPolicyRule_->GetDumpMessage(message);
268 netPolicyTraffic_->GetDumpMessage(message);
269 return NETMANAGER_SUCCESS;
270 }
271
CheckPermission()272 int32_t NetPolicyService::CheckPermission()
273 {
274 return NETMANAGER_SUCCESS;
275 }
276 } // namespace NetManagerStandard
277 } // namespace OHOS
278