1 /*
2  * Copyright (c) 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 "netsys_policy_wrapper.h"
17 #include "iptables_type.h"
18 
19 namespace OHOS {
20 namespace NetManagerStandard {
21 NetsysPolicyWrapper::NetsysPolicyWrapper() = default;
22 
23 NetsysPolicyWrapper::~NetsysPolicyWrapper() = default;
24 
RegisterNetsysCallback(sptr<NetsysControllerCallback> callback)25 int32_t NetsysPolicyWrapper::RegisterNetsysCallback(sptr<NetsysControllerCallback> callback)
26 {
27     auto netsysReturnValue = NetsysController::GetInstance().RegisterCallback(callback);
28     NETMGR_LOG_D("NetsysPolicyWrapper:RegisterNetsysObserver netsys return[%{public}d]", netsysReturnValue);
29     return netsysReturnValue;
30 }
31 
BandwidthEnableDataSaver(bool enable)32 int32_t NetsysPolicyWrapper::BandwidthEnableDataSaver(bool enable)
33 {
34     auto netsysReturnValue = NetsysController::GetInstance().BandwidthEnableDataSaver(enable);
35     NETMGR_LOG_D("NetsysPolicyWrapper:EnableDataSaver enable[%{public}d] netsys return[%{public}d]", enable,
36                  netsysReturnValue);
37     return netsysReturnValue;
38 }
39 
BandwidthSetIfaceQuota(const std::string & iface,int64_t bytes)40 int32_t NetsysPolicyWrapper::BandwidthSetIfaceQuota(const std::string &iface, int64_t bytes)
41 {
42     auto netsysReturnValue = NetsysController::GetInstance().BandwidthSetIfaceQuota(iface, bytes);
43     NETMGR_LOG_D("NetsysPolicyWrapper:SetIfaceQuota iface[%{public}s] bytes[%{public}s] netsys return[%{public}d]",
44                  iface.c_str(), std::to_string(bytes).c_str(), netsysReturnValue);
45     return netsysReturnValue;
46 }
47 
BandwidthRemoveIfaceQuota(const std::string & iface)48 int32_t NetsysPolicyWrapper::BandwidthRemoveIfaceQuota(const std::string &iface)
49 {
50     auto netsysReturnValue = NetsysController::GetInstance().BandwidthRemoveIfaceQuota(iface);
51     NETMGR_LOG_D("NetsysPolicyWrapper:RemoveIfaceQuota iface[%{public}s] netsys return[%{public}d]", iface.c_str(),
52                  netsysReturnValue);
53     return netsysReturnValue;
54 }
55 
BandwidthAddDeniedList(uint32_t uid)56 int32_t NetsysPolicyWrapper::BandwidthAddDeniedList(uint32_t uid)
57 {
58     auto netsysReturnValue = NetsysController::GetInstance().BandwidthAddDeniedList(uid);
59     NETMGR_LOG_D("NetsysPolicyWrapper:AddDeniedList uid[%{public}u] netsys return[%{public}d]", uid, netsysReturnValue);
60     return netsysReturnValue;
61 }
62 
BandwidthRemoveDeniedList(uint32_t uid)63 int32_t NetsysPolicyWrapper::BandwidthRemoveDeniedList(uint32_t uid)
64 {
65     auto netsysReturnValue = NetsysController::GetInstance().BandwidthRemoveDeniedList(uid);
66     NETMGR_LOG_D("NetsysPolicyWrapper:RemoveDeniedList uid[%{public}u] netsys return[%{public}d]", uid,
67                  netsysReturnValue);
68     return netsysReturnValue;
69 }
70 
BandwidthAddAllowedList(uint32_t uid)71 int32_t NetsysPolicyWrapper::BandwidthAddAllowedList(uint32_t uid)
72 {
73     auto netsysReturnValue = NetsysController::GetInstance().BandwidthAddAllowedList(uid);
74     NETMGR_LOG_D("NetsysPolicyWrapper:AddAllowedList uid[%{public}u] netsys return[%{public}d]", uid,
75                  netsysReturnValue);
76     return netsysReturnValue;
77 }
78 
BandwidthRemoveAllowedList(uint32_t uid)79 int32_t NetsysPolicyWrapper::BandwidthRemoveAllowedList(uint32_t uid)
80 {
81     auto netsysReturnValue = NetsysController::GetInstance().BandwidthRemoveAllowedList(uid);
82     NETMGR_LOG_D("NetsysPolicyWrapper:RemoveAllowedLists uid[%{public}u] netsys return[%{public}d]", uid,
83                  netsysReturnValue);
84     return netsysReturnValue;
85 }
86 
PowerSaveUpdataAllowedList(uint32_t uid,uint32_t firewallRule)87 int32_t NetsysPolicyWrapper::PowerSaveUpdataAllowedList(uint32_t uid, uint32_t firewallRule)
88 {
89     std::vector<uint32_t> uids{ uid };
90     uint32_t chain = ChainType::CHAIN_OHFW_ALLOWED_LIST_BOX;
91     auto netsysReturnValue = NetsysController::GetInstance().FirewallSetUidRule(chain, uids, firewallRule);
92     NETMGR_LOG_D("NetsysPolicyWrapper:PowerSaveUpdataAllowedList uid[%{public}u] netsys return[%{public}d]", uid,
93                  netsysReturnValue);
94     return netsysReturnValue;
95 }
96 
FirewallSetUidsAllowedListChain(uint32_t chain,const std::vector<uint32_t> & uids)97 int32_t NetsysPolicyWrapper::FirewallSetUidsAllowedListChain(uint32_t chain, const std::vector<uint32_t> &uids)
98 {
99     auto netsysReturnValue = NetsysController::GetInstance().FirewallSetUidsAllowedListChain(chain, uids);
100     NETMGR_LOG_D("SetUidsAllowedListChain chain[%{public}u] uids size[%{public}zu] netsys return[%{public}d]", chain,
101                  uids.size(), netsysReturnValue);
102     return netsysReturnValue;
103 }
104 
FirewallSetUidsDeniedListChain(uint32_t chain,const std::vector<uint32_t> & uids)105 int32_t NetsysPolicyWrapper::FirewallSetUidsDeniedListChain(uint32_t chain, const std::vector<uint32_t> &uids)
106 {
107     auto netsysReturnValue = NetsysController::GetInstance().FirewallSetUidsDeniedListChain(chain, uids);
108     NETMGR_LOG_D("SetUidsDeniedListChain chain[%{public}u] uids size[%{public}zu] netsys return[%{public}d]", chain,
109                  uids.size(), netsysReturnValue);
110     return netsysReturnValue;
111 }
112 
FirewallSetUidRule(uint32_t chain,const std::vector<uint32_t> & uids,uint32_t firewallRule)113 int32_t NetsysPolicyWrapper::FirewallSetUidRule(uint32_t chain, const std::vector<uint32_t> &uids,
114                                                 uint32_t firewallRule)
115 {
116     auto netsysReturnValue = NetsysController::GetInstance().FirewallSetUidRule(chain, uids, firewallRule);
117     return netsysReturnValue;
118 }
119 
FirewallEnableChain(uint32_t chain,bool enable)120 int32_t NetsysPolicyWrapper::FirewallEnableChain(uint32_t chain, bool enable)
121 {
122     auto netsysReturnValue = NetsysController::GetInstance().FirewallEnableChain(chain, enable);
123     NETMGR_LOG_D("FirewallEnableChain chain[%{public}u] enable[%{public}d] netsys return[%{public}d]", chain, enable,
124                  netsysReturnValue);
125     return netsysReturnValue;
126 }
127 
SetNetworkAccessPolicy(uint32_t uid,NetworkAccessPolicy policy,bool reconfirmFlag,bool isBroker)128 int32_t NetsysPolicyWrapper::SetNetworkAccessPolicy(uint32_t uid, NetworkAccessPolicy policy, bool reconfirmFlag,
129                                                     bool isBroker)
130 {
131     auto netsysReturnValue =
132         NetsysController::GetInstance().SetNetworkAccessPolicy(uid, policy, reconfirmFlag, isBroker);
133     NETMGR_LOG_D(
134         "SetNetworkAccessPolicy uid[%{public}u] policy wifi_enable[%{public}d] and cellular_enable[%{public}d] "
135         "netsys return[%{public}d]",
136         uid, policy.wifiAllow, policy.cellularAllow, netsysReturnValue);
137     return netsysReturnValue;
138 }
139 
DeleteNetworkAccessPolicy(uint32_t uid)140 int32_t NetsysPolicyWrapper::DeleteNetworkAccessPolicy(uint32_t uid)
141 {
142     auto netsysReturnValue = NetsysController::GetInstance().DeleteNetworkAccessPolicy(uid);
143     return netsysReturnValue;
144 }
145 
ClearFirewallAllRules()146 int32_t NetsysPolicyWrapper::ClearFirewallAllRules()
147 {
148     auto netsysReturnValue = NetsysController::GetInstance().ClearFirewallAllRules();
149     return netsysReturnValue;
150 }
151 
SetNicTrafficAllowed(const std::vector<std::string> & ifaceNames,bool status)152 int32_t NetsysPolicyWrapper::SetNicTrafficAllowed(const std::vector<std::string> &ifaceNames, bool status)
153 {
154     auto netsysReturnValue = NetsysController::GetInstance().SetNicTrafficAllowed(ifaceNames, status);
155     return netsysReturnValue;
156 }
157 } // namespace NetManagerStandard
158 } // namespace OHOS
159