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_firewall.h"
17
18 #include "ipc_skeleton.h"
19
20 #include "firewall_rule.h"
21 #include "net_policy_core.h"
22 #include "net_policy_event_handler.h"
23 #include "net_settings.h"
24
25 namespace OHOS {
26 namespace NetManagerStandard {
Init()27 void NetPolicyFirewall::Init()
28 {
29 deviceIdleFirewallRule_ = FirewallRule::CreateFirewallRule(FIREWALL_CHAIN_DEVICE_IDLE);
30 }
31
SetDeviceIdleAllowedList(uint32_t uid,bool isAllowed)32 int32_t NetPolicyFirewall::SetDeviceIdleAllowedList(uint32_t uid, bool isAllowed)
33 {
34 deviceIdleFirewallRule_->SetAllowedList(uid, isAllowed ? FIREWALL_RULE_ALLOW : FIREWALL_RULE_DENY);
35
36 std::shared_ptr<PolicyEvent> eventData = std::make_shared<PolicyEvent>();
37 eventData->eventId = NetPolicyEventHandler::MSG_DEVICE_IDLE_LIST_UPDATED;
38 eventData->deviceIdleList = deviceIdleFirewallRule_->GetAllowedList();
39 SendEvent(NetPolicyEventHandler::MSG_DEVICE_IDLE_LIST_UPDATED, eventData);
40 return NETMANAGER_SUCCESS;
41 }
42
GetDeviceIdleAllowedList(std::vector<uint32_t> & uids)43 int32_t NetPolicyFirewall::GetDeviceIdleAllowedList(std::vector<uint32_t> &uids)
44 {
45 uids = deviceIdleFirewallRule_->GetAllowedList();
46 return NETMANAGER_SUCCESS;
47 }
48
UpdateDeviceIdlePolicy(bool enable)49 int32_t NetPolicyFirewall::UpdateDeviceIdlePolicy(bool enable)
50 {
51 if (deviceIdleMode_ == enable) {
52 NETMGR_LOG_W("Same device idle policy.");
53 return NETMANAGER_ERR_PARAMETER_ERROR;
54 }
55 if (enable) {
56 deviceIdleFirewallRule_->SetAllowedList();
57 }
58 NetmanagerHiTrace::NetmanagerStartSyncTrace("Update firewall status start");
59 deviceIdleFirewallRule_->EnableFirewall(enable);
60 NetmanagerHiTrace::NetmanagerFinishSyncTrace("Update firewall status end");
61 deviceIdleMode_ = enable;
62 // notify to other core.
63 auto policyEvent = std::make_shared<PolicyEvent>();
64 policyEvent->deviceIdleMode = enable;
65 NetmanagerHiTrace::NetmanagerStartSyncTrace("Notify other policy class status start");
66 SendEvent(NetPolicyEventHandler::MSG_DEVICE_IDLE_MODE_CHANGED, policyEvent);
67 NetmanagerHiTrace::NetmanagerFinishSyncTrace("Notify other policy class status end");
68 return NETMANAGER_SUCCESS;
69 }
70
ResetPolicies()71 void NetPolicyFirewall::ResetPolicies()
72 {
73 deviceIdleFirewallRule_->ClearAllowedList();
74 deviceIdleFirewallRule_->ClearDeniedList();
75 UpdateDeviceIdlePolicy(false);
76 }
77
DeleteUid(uint32_t uid)78 void NetPolicyFirewall::DeleteUid(uint32_t uid)
79 {
80 deviceIdleFirewallRule_->RemoveFromAllowedList(uid);
81 }
82
HandleEvent(int32_t eventId,const std::shared_ptr<PolicyEvent> & policyEvent)83 void NetPolicyFirewall::HandleEvent(int32_t eventId, const std::shared_ptr<PolicyEvent> &policyEvent)
84 {
85 switch (eventId) {
86 case NetPolicyEventHandler::MSG_UID_REMOVED:
87 DeleteUid(policyEvent->deletedUid);
88 break;
89 default:
90 break;
91 }
92 }
93 } // namespace NetManagerStandard
94 } // namespace OHOS
95