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_client.h"
17 #include <thread>
18 
19 #include "iservice_registry.h"
20 #include "system_ability_definition.h"
21 #include "net_mgr_log_wrapper.h"
22 
23 static constexpr uint32_t WAIT_FOR_SERVICE_TIME_MS = 500;
24 static constexpr uint32_t MAX_GET_SERVICE_COUNT = 10;
25 
26 namespace OHOS {
27 namespace NetManagerStandard {
NetPolicyClient()28 NetPolicyClient::NetPolicyClient() : netPolicyService_(nullptr), deathRecipient_(nullptr), callback_(nullptr) {}
29 
30 NetPolicyClient::~NetPolicyClient() = default;
31 
SetPolicyByUid(uint32_t uid,uint32_t policy)32 int32_t NetPolicyClient::SetPolicyByUid(uint32_t uid, uint32_t policy)
33 {
34     sptr<INetPolicyService> proxy = GetProxy();
35     if (proxy == nullptr) {
36         NETMGR_LOG_E("proxy is nullptr");
37         return NETMANAGER_ERR_GET_PROXY_FAIL;
38     }
39 
40     return proxy->SetPolicyByUid(uid, policy);
41 }
42 
GetPolicyByUid(uint32_t uid,uint32_t & policy)43 int32_t NetPolicyClient::GetPolicyByUid(uint32_t uid, uint32_t &policy)
44 {
45     sptr<INetPolicyService> proxy = GetProxy();
46     if (proxy == nullptr) {
47         NETMGR_LOG_E("proxy is nullptr");
48         return NETMANAGER_ERR_GET_PROXY_FAIL;
49     }
50     return proxy->GetPolicyByUid(uid, policy);
51 }
52 
GetUidsByPolicy(uint32_t policy,std::vector<uint32_t> & uids)53 int32_t NetPolicyClient::GetUidsByPolicy(uint32_t policy, std::vector<uint32_t> &uids)
54 {
55     sptr<INetPolicyService> proxy = GetProxy();
56     if (proxy == nullptr) {
57         NETMGR_LOG_E("proxy is nullptr");
58         return NETMANAGER_ERR_GET_PROXY_FAIL;
59     }
60 
61     return proxy->GetUidsByPolicy(policy, uids);
62 }
63 
IsUidNetAllowed(uint32_t uid,bool metered,bool & isAllowed)64 int32_t NetPolicyClient::IsUidNetAllowed(uint32_t uid, bool metered, bool &isAllowed)
65 {
66     sptr<INetPolicyService> proxy = GetProxy();
67     if (proxy == nullptr) {
68         NETMGR_LOG_E("proxy is nullptr");
69         return NETMANAGER_ERR_GET_PROXY_FAIL;
70     }
71     return proxy->IsUidNetAllowed(uid, metered, isAllowed);
72 }
73 
IsUidNetAllowed(uint32_t uid,const std::string & ifaceName,bool & isAllowed)74 int32_t NetPolicyClient::IsUidNetAllowed(uint32_t uid, const std::string &ifaceName, bool &isAllowed)
75 {
76     sptr<INetPolicyService> proxy = GetProxy();
77     if (proxy == nullptr) {
78         NETMGR_LOG_E("proxy is nullptr");
79         return NETMANAGER_ERR_GET_PROXY_FAIL;
80     }
81 
82     return proxy->IsUidNetAllowed(uid, ifaceName, isAllowed);
83 }
84 
IsUidNetAccess(uint32_t uid,bool isMetered,bool & isAllowed)85 int32_t NetPolicyClient::IsUidNetAccess(uint32_t uid, bool isMetered, bool &isAllowed)
86 {
87     return IsUidNetAllowed(uid, isMetered, isAllowed);
88 }
89 
IsUidNetAccess(uint32_t uid,const std::string & ifaceName,bool & isAllowed)90 int32_t NetPolicyClient::IsUidNetAccess(uint32_t uid, const std::string &ifaceName, bool &isAllowed)
91 {
92     return IsUidNetAllowed(uid, ifaceName, isAllowed);
93 }
94 
GetProxy()95 sptr<INetPolicyService> NetPolicyClient::GetProxy()
96 {
97     std::lock_guard lock(mutex_);
98 
99     if (netPolicyService_ != nullptr) {
100         NETMGR_LOG_D("get proxy is ok");
101         return netPolicyService_;
102     }
103 
104     NETMGR_LOG_I("execute GetSystemAbilityManager");
105     sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
106     if (sam == nullptr) {
107         NETMGR_LOG_E("NetPolicyManager::GetProxy(), get SystemAbilityManager failed");
108         return nullptr;
109     }
110 
111     sptr<IRemoteObject> remote = sam->CheckSystemAbility(COMM_NET_POLICY_MANAGER_SYS_ABILITY_ID);
112     if (remote == nullptr) {
113         NETMGR_LOG_E("get Remote service failed");
114         return nullptr;
115     }
116 
117     deathRecipient_ = new (std::nothrow) NetPolicyDeathRecipient(*this);
118     if (deathRecipient_ == nullptr) {
119         NETMGR_LOG_E("get deathRecipient_ failed");
120         return nullptr;
121     }
122     if ((remote->IsProxyObject()) && (!remote->AddDeathRecipient(deathRecipient_))) {
123         NETMGR_LOG_E("add death recipient failed");
124         return nullptr;
125     }
126 
127     netPolicyService_ = iface_cast<INetPolicyService>(remote);
128     if (netPolicyService_ == nullptr) {
129         NETMGR_LOG_E("get Remote service proxy failed");
130         return nullptr;
131     }
132 
133     return netPolicyService_;
134 }
135 
RecoverCallback()136 void NetPolicyClient::RecoverCallback()
137 {
138     uint32_t count = 0;
139     while (GetProxy() == nullptr && count < MAX_GET_SERVICE_COUNT) {
140         std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_FOR_SERVICE_TIME_MS));
141         count++;
142     }
143     auto proxy = GetProxy();
144     NETMGR_LOG_W("Get proxy %{public}s, count: %{public}u", proxy == nullptr ? "failed" : "success", count);
145     if (proxy != nullptr && callback_ != nullptr) {
146         int32_t ret = proxy->RegisterNetPolicyCallback(callback_);
147         NETMGR_LOG_D("Register result %{public}d", ret);
148     }
149 }
150 
OnRemoteDied(const wptr<IRemoteObject> & remote)151 void NetPolicyClient::OnRemoteDied(const wptr<IRemoteObject> &remote)
152 {
153     NETMGR_LOG_D("on remote died");
154     if (remote == nullptr) {
155         NETMGR_LOG_E("remote object is nullptr");
156         return;
157     }
158     {
159         std::lock_guard lock(mutex_);
160         if (netPolicyService_ == nullptr) {
161             NETMGR_LOG_E("netPolicyService_ is nullptr");
162             return;
163         }
164 
165         sptr<IRemoteObject> local = netPolicyService_->AsObject();
166         if (local != remote.promote()) {
167             NETMGR_LOG_E("proxy and stub is not same remote object");
168             return;
169         }
170 
171         local->RemoveDeathRecipient(deathRecipient_);
172         netPolicyService_ = nullptr;
173     }
174 
175     if (callback_ != nullptr) {
176         NETMGR_LOG_D("on remote died recover callback");
177         std::thread t([this]() {
178             RecoverCallback();
179         });
180         std::string threadName = "netpolicyRecoverCallback";
181         pthread_setname_np(t.native_handle(), threadName.c_str());
182         t.detach();
183     }
184 }
185 
RegisterNetPolicyCallback(const sptr<INetPolicyCallback> & callback)186 int32_t NetPolicyClient::RegisterNetPolicyCallback(const sptr<INetPolicyCallback> &callback)
187 {
188     NETMGR_LOG_D("RegisterNetPolicyCallback client in");
189     sptr<INetPolicyService> proxy = GetProxy();
190     if (proxy == nullptr) {
191         NETMGR_LOG_E("proxy is nullptr");
192         return NETMANAGER_ERR_GET_PROXY_FAIL;
193     }
194     int32_t ret = proxy->RegisterNetPolicyCallback(callback);
195     if (ret == NETMANAGER_SUCCESS) {
196         NETMGR_LOG_D("RegisterNetPolicyCallback success, save callback");
197         callback_ = callback;
198     }
199 
200     return ret;
201 }
202 
UnregisterNetPolicyCallback(const sptr<INetPolicyCallback> & callback)203 int32_t NetPolicyClient::UnregisterNetPolicyCallback(const sptr<INetPolicyCallback> &callback)
204 {
205     sptr<INetPolicyService> proxy = GetProxy();
206     if (proxy == nullptr) {
207         NETMGR_LOG_E("proxy is nullptr");
208         return NETMANAGER_ERR_GET_PROXY_FAIL;
209     }
210     int32_t ret = proxy->UnregisterNetPolicyCallback(callback);
211     if (ret == NETMANAGER_SUCCESS) {
212         NETMGR_LOG_D("UnRegisterNetPolicyCallback success, delete callback");
213         callback_ = nullptr;
214     }
215 
216     return ret;
217 }
218 
SetNetQuotaPolicies(const std::vector<NetQuotaPolicy> & quotaPolicies)219 int32_t NetPolicyClient::SetNetQuotaPolicies(const std::vector<NetQuotaPolicy> &quotaPolicies)
220 {
221     if (quotaPolicies.empty()) {
222         NETMGR_LOG_E("quotaPolicies is empty");
223         return NetPolicyResultCode::POLICY_ERR_INVALID_QUOTA_POLICY;
224     }
225 
226     if (quotaPolicies.size() > QUOTA_POLICY_MAX_SIZE) {
227         NETMGR_LOG_E("quotaPolicies's size is greater than the maximum, size is [%{public}zu]", quotaPolicies.size());
228         return NetPolicyResultCode::POLICY_ERR_INVALID_QUOTA_POLICY;
229     }
230 
231     sptr<INetPolicyService> proxy = GetProxy();
232     if (proxy == nullptr) {
233         NETMGR_LOG_E("proxy is nullptr");
234         return NETMANAGER_ERR_GET_PROXY_FAIL;
235     }
236 
237     return proxy->SetNetQuotaPolicies(quotaPolicies);
238 }
239 
GetNetQuotaPolicies(std::vector<NetQuotaPolicy> & quotaPolicies)240 int32_t NetPolicyClient::GetNetQuotaPolicies(std::vector<NetQuotaPolicy> &quotaPolicies)
241 {
242     sptr<INetPolicyService> proxy = GetProxy();
243     if (proxy == nullptr) {
244         NETMGR_LOG_E("proxy is nullptr");
245         return NETMANAGER_ERR_GET_PROXY_FAIL;
246     }
247 
248     return proxy->GetNetQuotaPolicies(quotaPolicies);
249 }
250 
SetFactoryPolicy(const std::string & simId)251 NetPolicyResultCode NetPolicyClient::SetFactoryPolicy(const std::string &simId)
252 {
253     return static_cast<NetPolicyResultCode>(ResetPolicies(simId));
254 }
255 
ResetPolicies(const std::string & simId)256 int32_t NetPolicyClient::ResetPolicies(const std::string &simId)
257 {
258     sptr<INetPolicyService> proxy = GetProxy();
259     if (proxy == nullptr) {
260         NETMGR_LOG_E("proxy is nullptr");
261         return NETMANAGER_ERR_GET_PROXY_FAIL;
262     }
263 
264     return proxy->ResetPolicies(simId);
265 }
266 
SetBackgroundPolicy(bool isBackgroundPolicyAllow)267 int32_t NetPolicyClient::SetBackgroundPolicy(bool isBackgroundPolicyAllow)
268 {
269     sptr<INetPolicyService> proxy = GetProxy();
270     if (proxy == nullptr) {
271         NETMGR_LOG_E("proxy is nullptr");
272         return NETMANAGER_ERR_GET_PROXY_FAIL;
273     }
274 
275     return proxy->SetBackgroundPolicy(isBackgroundPolicyAllow);
276 }
277 
GetBackgroundPolicy(bool & backgroundPolicy)278 int32_t NetPolicyClient::GetBackgroundPolicy(bool &backgroundPolicy)
279 {
280     sptr<INetPolicyService> proxy = GetProxy();
281     if (proxy == nullptr) {
282         NETMGR_LOG_E("proxy is nullptr");
283         return NETMANAGER_ERR_GET_PROXY_FAIL;
284     }
285 
286     return proxy->GetBackgroundPolicy(backgroundPolicy);
287 }
288 
GetBackgroundPolicyByUid(uint32_t uid,uint32_t & backgroundPolicyOfUid)289 int32_t NetPolicyClient::GetBackgroundPolicyByUid(uint32_t uid, uint32_t &backgroundPolicyOfUid)
290 {
291     sptr<INetPolicyService> proxy = GetProxy();
292     if (proxy == nullptr) {
293         NETMGR_LOG_E("proxy is nullptr");
294         return NETMANAGER_ERR_GET_PROXY_FAIL;
295     }
296     return proxy->GetBackgroundPolicyByUid(uid, backgroundPolicyOfUid);
297 }
298 
SetSnoozePolicy(int8_t netType,const std::string & simId)299 NetPolicyResultCode NetPolicyClient::SetSnoozePolicy(int8_t netType, const std::string &simId)
300 {
301     return static_cast<NetPolicyResultCode>(UpdateRemindPolicy(netType, simId, RemindType::REMIND_TYPE_LIMIT));
302 }
303 
UpdateRemindPolicy(int32_t netType,const std::string & simId,uint32_t remindType)304 int32_t NetPolicyClient::UpdateRemindPolicy(int32_t netType, const std::string &simId, uint32_t remindType)
305 {
306     sptr<INetPolicyService> proxy = GetProxy();
307     if (proxy == nullptr) {
308         NETMGR_LOG_E("proxy is nullptr");
309         return NETMANAGER_ERR_GET_PROXY_FAIL;
310     }
311 
312     return proxy->UpdateRemindPolicy(netType, simId, remindType);
313 }
314 
SetIdleTrustlist(uint32_t uid,bool isTrustlist)315 NetPolicyResultCode NetPolicyClient::SetIdleTrustlist(uint32_t uid, bool isTrustlist)
316 {
317     return static_cast<NetPolicyResultCode>(SetDeviceIdleTrustlist({uid}, isTrustlist));
318 }
319 
SetDeviceIdleTrustlist(const std::vector<uint32_t> & uids,bool isAllowed)320 int32_t NetPolicyClient::SetDeviceIdleTrustlist(const std::vector<uint32_t> &uids, bool isAllowed)
321 {
322     sptr<INetPolicyService> proxy = GetProxy();
323     if (proxy == nullptr) {
324         NETMGR_LOG_E("proxy is nullptr");
325         return NETMANAGER_ERR_GET_PROXY_FAIL;
326     }
327 
328     return proxy->SetDeviceIdleTrustlist(uids, isAllowed);
329 }
330 
GetIdleTrustlist(std::vector<uint32_t> & uids)331 NetPolicyResultCode NetPolicyClient::GetIdleTrustlist(std::vector<uint32_t> &uids)
332 {
333     return static_cast<NetPolicyResultCode>(GetDeviceIdleTrustlist(uids));
334 }
335 
GetDeviceIdleTrustlist(std::vector<uint32_t> & uids)336 int32_t NetPolicyClient::GetDeviceIdleTrustlist(std::vector<uint32_t> &uids)
337 {
338     sptr<INetPolicyService> proxy = GetProxy();
339     if (proxy == nullptr) {
340         NETMGR_LOG_E("proxy is nullptr");
341         return NETMANAGER_ERR_GET_PROXY_FAIL;
342     }
343 
344     return proxy->GetDeviceIdleTrustlist(uids);
345 }
346 
SetDeviceIdlePolicy(bool enable)347 int32_t NetPolicyClient::SetDeviceIdlePolicy(bool enable)
348 {
349     sptr<INetPolicyService> proxy = GetProxy();
350     if (proxy == nullptr) {
351         NETMGR_LOG_E("proxy is nullptr");
352         return NETMANAGER_ERR_GET_PROXY_FAIL;
353     }
354 
355     return proxy->SetDeviceIdlePolicy(enable);
356 }
357 
GetPowerSaveTrustlist(std::vector<uint32_t> & uids)358 int32_t NetPolicyClient::GetPowerSaveTrustlist(std::vector<uint32_t> &uids)
359 {
360     sptr<INetPolicyService> proxy = GetProxy();
361     if (proxy == nullptr) {
362         NETMGR_LOG_E("proxy is nullptr");
363         return NETMANAGER_ERR_GET_PROXY_FAIL;
364     }
365 
366     return proxy->GetPowerSaveTrustlist(uids);
367 }
368 
SetPowerSaveTrustlist(const std::vector<uint32_t> & uids,bool isAllowed)369 int32_t NetPolicyClient::SetPowerSaveTrustlist(const std::vector<uint32_t> &uids, bool isAllowed)
370 {
371     sptr<INetPolicyService> proxy = GetProxy();
372     if (proxy == nullptr) {
373         NETMGR_LOG_E("proxy is nullptr");
374         return NETMANAGER_ERR_GET_PROXY_FAIL;
375     }
376 
377     return proxy->SetPowerSaveTrustlist(uids, isAllowed);
378 }
379 
SetPowerSavePolicy(bool enable)380 int32_t NetPolicyClient::SetPowerSavePolicy(bool enable)
381 {
382     sptr<INetPolicyService> proxy = GetProxy();
383     if (proxy == nullptr) {
384         NETMGR_LOG_E("proxy is nullptr");
385         return NETMANAGER_ERR_GET_PROXY_FAIL;
386     }
387 
388     return proxy->SetPowerSavePolicy(enable);
389 }
390 
CheckPermission()391 int32_t NetPolicyClient::CheckPermission()
392 {
393     sptr<INetPolicyService> proxy = GetProxy();
394     if (proxy == nullptr) {
395         NETMGR_LOG_E("proxy is nullptr");
396         return NETMANAGER_ERR_GET_PROXY_FAIL;
397     }
398 
399     return proxy->CheckPermission();
400 }
401 
SetNetworkAccessPolicy(uint32_t uid,NetworkAccessPolicy policy,bool reconfirmFlag)402 int32_t NetPolicyClient::SetNetworkAccessPolicy(uint32_t uid, NetworkAccessPolicy policy, bool reconfirmFlag)
403 {
404     sptr<INetPolicyService> proxy = GetProxy();
405     if (proxy == nullptr) {
406         NETMGR_LOG_E("proxy is nullptr");
407         return NETMANAGER_ERR_GET_PROXY_FAIL;
408     }
409 
410     return proxy->SetNetworkAccessPolicy(uid, policy, reconfirmFlag);
411 }
412 
GetNetworkAccessPolicy(AccessPolicyParameter parameter,AccessPolicySave & policy)413 int32_t NetPolicyClient::GetNetworkAccessPolicy(AccessPolicyParameter parameter, AccessPolicySave& policy)
414 {
415     sptr<INetPolicyService> proxy = GetProxy();
416     if (proxy == nullptr) {
417         NETMGR_LOG_E("proxy is nullptr");
418         return NETMANAGER_ERR_GET_PROXY_FAIL;
419     }
420     return proxy->GetNetworkAccessPolicy(parameter, policy);
421 }
422 
NotifyNetAccessPolicyDiag(uint32_t uid)423 int32_t NetPolicyClient::NotifyNetAccessPolicyDiag(uint32_t uid)
424 {
425     sptr<INetPolicyService> proxy = GetProxy();
426     if (proxy == nullptr) {
427         NETMGR_LOG_E("proxy is nullptr");
428         return NETMANAGER_ERR_GET_PROXY_FAIL;
429     }
430 
431     return proxy->NotifyNetAccessPolicyDiag(uid);
432 }
433 
SetNicTrafficAllowed(const std::vector<std::string> & ifaceNames,bool status)434 int32_t NetPolicyClient::SetNicTrafficAllowed(const std::vector<std::string> &ifaceNames, bool status)
435 {
436     sptr<INetPolicyService> proxy = GetProxy();
437     if (proxy == nullptr) {
438         NETMGR_LOG_E("proxy is nullptr");
439         return NETMANAGER_ERR_GET_PROXY_FAIL;
440     }
441 
442     return proxy->SetNicTrafficAllowed(ifaceNames, status);
443 }
444 } // namespace NetManagerStandard
445 } // namespace OHOS
446