1b1b8bc3fSopenharmony_ci/*
2b1b8bc3fSopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3b1b8bc3fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4b1b8bc3fSopenharmony_ci * you may not use this file except in compliance with the License.
5b1b8bc3fSopenharmony_ci * You may obtain a copy of the License at
6b1b8bc3fSopenharmony_ci *
7b1b8bc3fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8b1b8bc3fSopenharmony_ci *
9b1b8bc3fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10b1b8bc3fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11b1b8bc3fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12b1b8bc3fSopenharmony_ci * See the License for the specific language governing permissions and
13b1b8bc3fSopenharmony_ci * limitations under the License.
14b1b8bc3fSopenharmony_ci */
15b1b8bc3fSopenharmony_ci
16b1b8bc3fSopenharmony_ci#include <csignal>
17b1b8bc3fSopenharmony_ci#include <sys/types.h>
18b1b8bc3fSopenharmony_ci#include <regex>
19b1b8bc3fSopenharmony_ci#include <thread>
20b1b8bc3fSopenharmony_ci#include <unistd.h>
21b1b8bc3fSopenharmony_ci
22b1b8bc3fSopenharmony_ci#include "iservice_registry.h"
23b1b8bc3fSopenharmony_ci#include "system_ability_definition.h"
24b1b8bc3fSopenharmony_ci#include "bpf_loader.h"
25b1b8bc3fSopenharmony_ci#include "bpf_path.h"
26b1b8bc3fSopenharmony_ci#include "net_manager_constants.h"
27b1b8bc3fSopenharmony_ci#include "netmanager_base_common_utils.h"
28b1b8bc3fSopenharmony_ci#include "netnative_log_wrapper.h"
29b1b8bc3fSopenharmony_ci#include "netsys_native_service.h"
30b1b8bc3fSopenharmony_ci#ifdef SUPPORT_SYSVPN
31b1b8bc3fSopenharmony_ci#include "system_vpn_wrapper.h"
32b1b8bc3fSopenharmony_ci#endif // SUPPORT_SYSVPN
33b1b8bc3fSopenharmony_ci#ifdef ENABLE_NETSYS_ACCESS_POLICY_DIAG_LISTEN
34b1b8bc3fSopenharmony_ci#include "bpf_ring_buffer.h"
35b1b8bc3fSopenharmony_ci#endif
36b1b8bc3fSopenharmony_ci
37b1b8bc3fSopenharmony_ciusing namespace OHOS::NetManagerStandard::CommonUtils;
38b1b8bc3fSopenharmony_cinamespace OHOS {
39b1b8bc3fSopenharmony_cinamespace NetsysNative {
40b1b8bc3fSopenharmony_cistatic constexpr const char *BFP_NAME_NETSYS_PATH = "/system/etc/bpf/netsys.o";
41b1b8bc3fSopenharmony_ciconst std::regex REGEX_CMD_IPTABLES(std::string(R"(^-[\S]*[\s\S]*)"));
42b1b8bc3fSopenharmony_ci
43b1b8bc3fSopenharmony_ciREGISTER_SYSTEM_ABILITY_BY_ID(NetsysNativeService, COMM_NETSYS_NATIVE_SYS_ABILITY_ID, true)
44b1b8bc3fSopenharmony_ci
45b1b8bc3fSopenharmony_ciNetsysNativeService::NetsysNativeService()
46b1b8bc3fSopenharmony_ci    : SystemAbility(COMM_NETSYS_NATIVE_SYS_ABILITY_ID, true),
47b1b8bc3fSopenharmony_ci      netsysService_(nullptr),
48b1b8bc3fSopenharmony_ci      manager_(nullptr),
49b1b8bc3fSopenharmony_ci      notifyCallback_(nullptr)
50b1b8bc3fSopenharmony_ci{
51b1b8bc3fSopenharmony_ci}
52b1b8bc3fSopenharmony_ci
53b1b8bc3fSopenharmony_civoid NetsysNativeService::OnStart()
54b1b8bc3fSopenharmony_ci{
55b1b8bc3fSopenharmony_ci    NETNATIVE_LOGI("OnStart Begin");
56b1b8bc3fSopenharmony_ci    std::lock_guard<std::mutex> guard(instanceLock_);
57b1b8bc3fSopenharmony_ci    if (state_ == ServiceRunningState::STATE_RUNNING) {
58b1b8bc3fSopenharmony_ci        return;
59b1b8bc3fSopenharmony_ci    }
60b1b8bc3fSopenharmony_ci
61b1b8bc3fSopenharmony_ci    if (!Init()) {
62b1b8bc3fSopenharmony_ci        NETNATIVE_LOGE("NetsysNativeService init failed!");
63b1b8bc3fSopenharmony_ci        return;
64b1b8bc3fSopenharmony_ci    }
65b1b8bc3fSopenharmony_ci    bool res = SystemAbility::Publish(this);
66b1b8bc3fSopenharmony_ci    if (!res) {
67b1b8bc3fSopenharmony_ci        NETNATIVE_LOGE("publishing NetsysNativeService to sa manager failed!");
68b1b8bc3fSopenharmony_ci        return;
69b1b8bc3fSopenharmony_ci    }
70b1b8bc3fSopenharmony_ci    NETNATIVE_LOGI("Publish NetsysNativeService SUCCESS");
71b1b8bc3fSopenharmony_ci    state_ = ServiceRunningState::STATE_RUNNING;
72b1b8bc3fSopenharmony_ci    NETNATIVE_LOGI("start listener");
73b1b8bc3fSopenharmony_ci    manager_->StartListener();
74b1b8bc3fSopenharmony_ci#ifdef FEATURE_NET_FIREWALL_ENABLE
75b1b8bc3fSopenharmony_ci    bpfNetFirewall_->StartListener();
76b1b8bc3fSopenharmony_ci#endif
77b1b8bc3fSopenharmony_ci    NETNATIVE_LOGI("start listener end on start end");
78b1b8bc3fSopenharmony_ci}
79b1b8bc3fSopenharmony_ci
80b1b8bc3fSopenharmony_civoid NetsysNativeService::OnStop()
81b1b8bc3fSopenharmony_ci{
82b1b8bc3fSopenharmony_ci    std::lock_guard<std::mutex> guard(instanceLock_);
83b1b8bc3fSopenharmony_ci    state_ = ServiceRunningState::STATE_STOPPED;
84b1b8bc3fSopenharmony_ci    NETNATIVE_LOGI("stop listener");
85b1b8bc3fSopenharmony_ci    manager_->StopListener();
86b1b8bc3fSopenharmony_ci#ifdef FEATURE_NET_FIREWALL_ENABLE
87b1b8bc3fSopenharmony_ci    bpfNetFirewall_->StopListener();
88b1b8bc3fSopenharmony_ci    auto ret = OHOS::NetManagerStandard::UnloadElf(BFP_NAME_NETSYS_PATH);
89b1b8bc3fSopenharmony_ci    NETNATIVE_LOGI("UnloadElf is %{public}d", ret);
90b1b8bc3fSopenharmony_ci    if (ret == ElfLoadError::ELF_LOAD_ERR_NONE) {
91b1b8bc3fSopenharmony_ci        bpfNetFirewall_->SetBpfLoaded(false);
92b1b8bc3fSopenharmony_ci    }
93b1b8bc3fSopenharmony_ci#endif
94b1b8bc3fSopenharmony_ci    NETNATIVE_LOGI("stop listener end on stop end");
95b1b8bc3fSopenharmony_ci#ifdef ENABLE_NETSYS_ACCESS_POLICY_DIAG_LISTEN
96b1b8bc3fSopenharmony_ci    NetsysBpfRingBuffer::ExistRingBufferPoll();
97b1b8bc3fSopenharmony_ci#endif
98b1b8bc3fSopenharmony_ci}
99b1b8bc3fSopenharmony_ci
100b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::Dump(int32_t fd, const std::vector<std::u16string> &args)
101b1b8bc3fSopenharmony_ci{
102b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("Start Dump, fd: %{public}d", fd);
103b1b8bc3fSopenharmony_ci    std::string result;
104b1b8bc3fSopenharmony_ci    GetDumpMessage(result);
105b1b8bc3fSopenharmony_ci    int32_t ret = dprintf(fd, "%s\n", result.c_str());
106b1b8bc3fSopenharmony_ci    return ret < 0 ? SESSION_UNOPEN_ERR : ERR_NONE;
107b1b8bc3fSopenharmony_ci}
108b1b8bc3fSopenharmony_ci
109b1b8bc3fSopenharmony_civoid NetsysNativeService::GetDumpMessage(std::string &message)
110b1b8bc3fSopenharmony_ci{
111b1b8bc3fSopenharmony_ci    netsysService_->GetDumpInfo(message);
112b1b8bc3fSopenharmony_ci}
113b1b8bc3fSopenharmony_ci
114b1b8bc3fSopenharmony_civoid ExitHandler(int32_t signum)
115b1b8bc3fSopenharmony_ci{
116b1b8bc3fSopenharmony_ci    (void)signum;
117b1b8bc3fSopenharmony_ci    _Exit(1);
118b1b8bc3fSopenharmony_ci}
119b1b8bc3fSopenharmony_ci
120b1b8bc3fSopenharmony_cibool NetsysNativeService::Init()
121b1b8bc3fSopenharmony_ci{
122b1b8bc3fSopenharmony_ci    (void)signal(SIGTERM, ExitHandler);
123b1b8bc3fSopenharmony_ci    (void)signal(SIGABRT, ExitHandler);
124b1b8bc3fSopenharmony_ci
125b1b8bc3fSopenharmony_ci    netsysService_ = std::make_unique<nmd::NetManagerNative>();
126b1b8bc3fSopenharmony_ci    if (netsysService_ == nullptr) {
127b1b8bc3fSopenharmony_ci        NETNATIVE_LOGE("netsysService_ is nullptr!");
128b1b8bc3fSopenharmony_ci        return false;
129b1b8bc3fSopenharmony_ci    }
130b1b8bc3fSopenharmony_ci    netsysService_->Init();
131b1b8bc3fSopenharmony_ci
132b1b8bc3fSopenharmony_ci    manager_ = std::make_unique<OHOS::nmd::NetlinkManager>();
133b1b8bc3fSopenharmony_ci    if (manager_ == nullptr) {
134b1b8bc3fSopenharmony_ci        NETNATIVE_LOGE("manager_ is nullptr!");
135b1b8bc3fSopenharmony_ci        return false;
136b1b8bc3fSopenharmony_ci    }
137b1b8bc3fSopenharmony_ci    bpfStats_ = std::make_unique<OHOS::NetManagerStandard::NetsysBpfStats>();
138b1b8bc3fSopenharmony_ci    dhcpController_ = std::make_unique<OHOS::nmd::DhcpController>();
139b1b8bc3fSopenharmony_ci    fwmarkNetwork_ = std::make_unique<OHOS::nmd::FwmarkNetwork>();
140b1b8bc3fSopenharmony_ci    sharingManager_ = std::make_unique<SharingManager>();
141b1b8bc3fSopenharmony_ci    iptablesWrapper_ = IptablesWrapper::GetInstance();
142b1b8bc3fSopenharmony_ci    netDiagWrapper = NetDiagWrapper::GetInstance();
143b1b8bc3fSopenharmony_ci    clatManager_ = std::make_unique<OHOS::nmd::ClatManager>();
144b1b8bc3fSopenharmony_ci
145b1b8bc3fSopenharmony_ci    auto ret = OHOS::NetManagerStandard::LoadElf(BFP_NAME_NETSYS_PATH);
146b1b8bc3fSopenharmony_ci    NETNATIVE_LOGI("LoadElf is %{public}d", ret);
147b1b8bc3fSopenharmony_ci
148b1b8bc3fSopenharmony_ci#ifdef FEATURE_NET_FIREWALL_ENABLE
149b1b8bc3fSopenharmony_ci    bpfNetFirewall_ = NetsysBpfNetFirewall::GetInstance();
150b1b8bc3fSopenharmony_ci    if (ret == ElfLoadError::ELF_LOAD_ERR_NONE) {
151b1b8bc3fSopenharmony_ci        bpfNetFirewall_->SetBpfLoaded(true);
152b1b8bc3fSopenharmony_ci    }
153b1b8bc3fSopenharmony_ci    AddSystemAbilityListener(COMM_FIREWALL_MANAGER_SYS_ABILITY_ID);
154b1b8bc3fSopenharmony_ci    bpfNetFirewall_->LoadSystemAbility(COMM_FIREWALL_MANAGER_SYS_ABILITY_ID);
155b1b8bc3fSopenharmony_ci#endif
156b1b8bc3fSopenharmony_ci
157b1b8bc3fSopenharmony_ci#ifdef ENABLE_NETSYS_ACCESS_POLICY_DIAG_LISTEN
158b1b8bc3fSopenharmony_ci    NetsysBpfRingBuffer::ListenNetworkAccessPolicyEvent();
159b1b8bc3fSopenharmony_ci#endif
160b1b8bc3fSopenharmony_ci    AddSystemAbilityListener(COMM_NET_CONN_MANAGER_SYS_ABILITY_ID);
161b1b8bc3fSopenharmony_ci    return true;
162b1b8bc3fSopenharmony_ci}
163b1b8bc3fSopenharmony_ci
164b1b8bc3fSopenharmony_civoid NetsysNativeService::OnNetManagerRestart()
165b1b8bc3fSopenharmony_ci{
166b1b8bc3fSopenharmony_ci    NETNATIVE_LOGI("OnNetManagerRestart");
167b1b8bc3fSopenharmony_ci    if (netsysService_ != nullptr) {
168b1b8bc3fSopenharmony_ci        netsysService_->NetworkReinitRoute();
169b1b8bc3fSopenharmony_ci    }
170b1b8bc3fSopenharmony_ci    if (manager_ != nullptr && notifyCallback_ != nullptr) {
171b1b8bc3fSopenharmony_ci        manager_->UnregisterNetlinkCallback(notifyCallback_);
172b1b8bc3fSopenharmony_ci    }
173b1b8bc3fSopenharmony_ci}
174b1b8bc3fSopenharmony_ci
175b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::SetResolverConfig(uint16_t netId, uint16_t baseTimeoutMsec, uint8_t retryCount,
176b1b8bc3fSopenharmony_ci                                               const std::vector<std::string> &servers,
177b1b8bc3fSopenharmony_ci                                               const std::vector<std::string> &domains)
178b1b8bc3fSopenharmony_ci{
179b1b8bc3fSopenharmony_ci    netsysService_->DnsSetResolverConfig(netId, baseTimeoutMsec, retryCount, servers, domains);
180b1b8bc3fSopenharmony_ci    return 0;
181b1b8bc3fSopenharmony_ci}
182b1b8bc3fSopenharmony_ci
183b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::GetResolverConfig(uint16_t netid, std::vector<std::string> &servers,
184b1b8bc3fSopenharmony_ci                                               std::vector<std::string> &domains, uint16_t &baseTimeoutMsec,
185b1b8bc3fSopenharmony_ci                                               uint8_t &retryCount)
186b1b8bc3fSopenharmony_ci{
187b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("GetResolverConfig netid = %{public}d", netid);
188b1b8bc3fSopenharmony_ci    netsysService_->DnsGetResolverConfig(netid, servers, domains, baseTimeoutMsec, retryCount);
189b1b8bc3fSopenharmony_ci    return 0;
190b1b8bc3fSopenharmony_ci}
191b1b8bc3fSopenharmony_ci
192b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::CreateNetworkCache(uint16_t netid)
193b1b8bc3fSopenharmony_ci{
194b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("CreateNetworkCache Begin");
195b1b8bc3fSopenharmony_ci    netsysService_->DnsCreateNetworkCache(netid);
196b1b8bc3fSopenharmony_ci
197b1b8bc3fSopenharmony_ci    return 0;
198b1b8bc3fSopenharmony_ci}
199b1b8bc3fSopenharmony_ci
200b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::DestroyNetworkCache(uint16_t netId)
201b1b8bc3fSopenharmony_ci{
202b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("DestroyNetworkCache");
203b1b8bc3fSopenharmony_ci    return netsysService_->DnsDestroyNetworkCache(netId);
204b1b8bc3fSopenharmony_ci}
205b1b8bc3fSopenharmony_ci
206b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::GetAddrInfo(const std::string &hostName, const std::string &serverName,
207b1b8bc3fSopenharmony_ci                                         const AddrInfo &hints, uint16_t netId, std::vector<AddrInfo> &res)
208b1b8bc3fSopenharmony_ci{
209b1b8bc3fSopenharmony_ci    return netsysService_->DnsGetAddrInfo(hostName, serverName, hints, netId, res);
210b1b8bc3fSopenharmony_ci}
211b1b8bc3fSopenharmony_ci
212b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::SetInterfaceMtu(const std::string &interfaceName, int32_t mtu)
213b1b8bc3fSopenharmony_ci{
214b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("SetInterfaceMtu  Begin");
215b1b8bc3fSopenharmony_ci    return netsysService_->SetInterfaceMtu(interfaceName, mtu);
216b1b8bc3fSopenharmony_ci}
217b1b8bc3fSopenharmony_ci
218b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::GetInterfaceMtu(const std::string &interfaceName)
219b1b8bc3fSopenharmony_ci{
220b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("SetInterfaceMtu  Begin");
221b1b8bc3fSopenharmony_ci    return netsysService_->GetInterfaceMtu(interfaceName);
222b1b8bc3fSopenharmony_ci}
223b1b8bc3fSopenharmony_ci
224b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::SetTcpBufferSizes(const std::string &tcpBufferSizes)
225b1b8bc3fSopenharmony_ci{
226b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("SetTcpBufferSizes  Begin");
227b1b8bc3fSopenharmony_ci    return netsysService_->SetTcpBufferSizes(tcpBufferSizes);
228b1b8bc3fSopenharmony_ci}
229b1b8bc3fSopenharmony_ci
230b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::RegisterNotifyCallback(sptr<INotifyCallback> &callback)
231b1b8bc3fSopenharmony_ci{
232b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("RegisterNotifyCallback");
233b1b8bc3fSopenharmony_ci    notifyCallback_ = callback;
234b1b8bc3fSopenharmony_ci    dhcpController_->RegisterNotifyCallback(callback);
235b1b8bc3fSopenharmony_ci    manager_->RegisterNetlinkCallback(callback);
236b1b8bc3fSopenharmony_ci    return 0;
237b1b8bc3fSopenharmony_ci}
238b1b8bc3fSopenharmony_ci
239b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::UnRegisterNotifyCallback(sptr<INotifyCallback> &callback)
240b1b8bc3fSopenharmony_ci{
241b1b8bc3fSopenharmony_ci    NETNATIVE_LOGI("UnRegisterNotifyCallback");
242b1b8bc3fSopenharmony_ci    manager_->UnregisterNetlinkCallback(notifyCallback_);
243b1b8bc3fSopenharmony_ci    return 0;
244b1b8bc3fSopenharmony_ci}
245b1b8bc3fSopenharmony_ci
246b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::NetworkAddRoute(int32_t netId, const std::string &interfaceName,
247b1b8bc3fSopenharmony_ci                                             const std::string &destination, const std::string &nextHop)
248b1b8bc3fSopenharmony_ci{
249b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("NetworkAddRoute unpacket %{public}d %{public}s %{public}s %{public}s", netId,
250b1b8bc3fSopenharmony_ci                    interfaceName.c_str(), ToAnonymousIp(destination).c_str(), ToAnonymousIp(nextHop).c_str());
251b1b8bc3fSopenharmony_ci
252b1b8bc3fSopenharmony_ci    int32_t result = netsysService_->NetworkAddRoute(netId, interfaceName, destination, nextHop);
253b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("NetworkAddRoute %{public}d", result);
254b1b8bc3fSopenharmony_ci    return result;
255b1b8bc3fSopenharmony_ci}
256b1b8bc3fSopenharmony_ci
257b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::NetworkRemoveRoute(int32_t netId, const std::string &interfaceName,
258b1b8bc3fSopenharmony_ci                                                const std::string &destination, const std::string &nextHop)
259b1b8bc3fSopenharmony_ci{
260b1b8bc3fSopenharmony_ci    int32_t result = netsysService_->NetworkRemoveRoute(netId, interfaceName, destination, nextHop);
261b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("NetworkRemoveRoute %{public}d", result);
262b1b8bc3fSopenharmony_ci    return result;
263b1b8bc3fSopenharmony_ci}
264b1b8bc3fSopenharmony_ci
265b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::NetworkAddRouteParcel(int32_t netId, const RouteInfoParcel &routeInfo)
266b1b8bc3fSopenharmony_ci{
267b1b8bc3fSopenharmony_ci    int32_t result = netsysService_->NetworkAddRouteParcel(netId, routeInfo);
268b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("NetworkAddRouteParcel %{public}d", result);
269b1b8bc3fSopenharmony_ci    return result;
270b1b8bc3fSopenharmony_ci}
271b1b8bc3fSopenharmony_ci
272b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::NetworkRemoveRouteParcel(int32_t netId, const RouteInfoParcel &routeInfo)
273b1b8bc3fSopenharmony_ci{
274b1b8bc3fSopenharmony_ci    int32_t result = netsysService_->NetworkRemoveRouteParcel(netId, routeInfo);
275b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("NetworkRemoveRouteParcel %{public}d", result);
276b1b8bc3fSopenharmony_ci    return result;
277b1b8bc3fSopenharmony_ci}
278b1b8bc3fSopenharmony_ci
279b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::NetworkSetDefault(int32_t netId)
280b1b8bc3fSopenharmony_ci{
281b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("NetworkSetDefault in.");
282b1b8bc3fSopenharmony_ci    int32_t result = netsysService_->NetworkSetDefault(netId);
283b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("NetworkSetDefault out.");
284b1b8bc3fSopenharmony_ci    return result;
285b1b8bc3fSopenharmony_ci}
286b1b8bc3fSopenharmony_ci
287b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::NetworkGetDefault()
288b1b8bc3fSopenharmony_ci{
289b1b8bc3fSopenharmony_ci    int32_t result = netsysService_->NetworkGetDefault();
290b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("NetworkGetDefault");
291b1b8bc3fSopenharmony_ci    return result;
292b1b8bc3fSopenharmony_ci}
293b1b8bc3fSopenharmony_ci
294b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::NetworkClearDefault()
295b1b8bc3fSopenharmony_ci{
296b1b8bc3fSopenharmony_ci    int32_t result = netsysService_->NetworkClearDefault();
297b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("NetworkClearDefault");
298b1b8bc3fSopenharmony_ci    return result;
299b1b8bc3fSopenharmony_ci}
300b1b8bc3fSopenharmony_ci
301b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::GetProcSysNet(int32_t family, int32_t which, const std::string &ifname,
302b1b8bc3fSopenharmony_ci                                           const std::string &parameter, std::string &value)
303b1b8bc3fSopenharmony_ci{
304b1b8bc3fSopenharmony_ci    int32_t result = netsysService_->GetProcSysNet(family, which, ifname, parameter, &value);
305b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("GetProcSysNet");
306b1b8bc3fSopenharmony_ci    return result;
307b1b8bc3fSopenharmony_ci}
308b1b8bc3fSopenharmony_ci
309b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::SetProcSysNet(int32_t family, int32_t which, const std::string &ifname,
310b1b8bc3fSopenharmony_ci                                           const std::string &parameter, std::string &value)
311b1b8bc3fSopenharmony_ci{
312b1b8bc3fSopenharmony_ci    int32_t result = netsysService_->SetProcSysNet(family, which, ifname, parameter, value);
313b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("SetProcSysNet");
314b1b8bc3fSopenharmony_ci    return result;
315b1b8bc3fSopenharmony_ci}
316b1b8bc3fSopenharmony_ci
317b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::SetInternetPermission(uint32_t uid, uint8_t allow, uint8_t isBroker)
318b1b8bc3fSopenharmony_ci{
319b1b8bc3fSopenharmony_ci    int32_t result = netsysService_->SetInternetPermission(uid, allow, isBroker);
320b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("SetInternetPermission out.");
321b1b8bc3fSopenharmony_ci    return result;
322b1b8bc3fSopenharmony_ci}
323b1b8bc3fSopenharmony_ci
324b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::NetworkCreatePhysical(int32_t netId, int32_t permission)
325b1b8bc3fSopenharmony_ci{
326b1b8bc3fSopenharmony_ci    int32_t result = netsysService_->NetworkCreatePhysical(netId, permission);
327b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("NetworkCreatePhysical out.");
328b1b8bc3fSopenharmony_ci    return result;
329b1b8bc3fSopenharmony_ci}
330b1b8bc3fSopenharmony_ci
331b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::NetworkCreateVirtual(int32_t netId, bool hasDns)
332b1b8bc3fSopenharmony_ci{
333b1b8bc3fSopenharmony_ci    int32_t result = netsysService_->NetworkCreateVirtual(netId, hasDns);
334b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("NetworkCreateVirtual out.");
335b1b8bc3fSopenharmony_ci    return result;
336b1b8bc3fSopenharmony_ci}
337b1b8bc3fSopenharmony_ci
338b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::NetworkAddUids(int32_t netId, const std::vector<UidRange> &uidRanges)
339b1b8bc3fSopenharmony_ci{
340b1b8bc3fSopenharmony_ci    int32_t result = netsysService_->NetworkAddUids(netId, uidRanges);
341b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("NetworkAddUids out.");
342b1b8bc3fSopenharmony_ci    return result;
343b1b8bc3fSopenharmony_ci}
344b1b8bc3fSopenharmony_ci
345b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::NetworkDelUids(int32_t netId, const std::vector<UidRange> &uidRanges)
346b1b8bc3fSopenharmony_ci{
347b1b8bc3fSopenharmony_ci    int32_t result = netsysService_->NetworkDelUids(netId, uidRanges);
348b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("NetworkDelUids out.");
349b1b8bc3fSopenharmony_ci    return result;
350b1b8bc3fSopenharmony_ci}
351b1b8bc3fSopenharmony_ci
352b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::AddInterfaceAddress(const std::string &interfaceName, const std::string &addrString,
353b1b8bc3fSopenharmony_ci                                                 int32_t prefixLength)
354b1b8bc3fSopenharmony_ci{
355b1b8bc3fSopenharmony_ci    int32_t result = netsysService_->AddInterfaceAddress(interfaceName, addrString, prefixLength);
356b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("AddInterfaceAddress");
357b1b8bc3fSopenharmony_ci    return result;
358b1b8bc3fSopenharmony_ci}
359b1b8bc3fSopenharmony_ci
360b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::DelInterfaceAddress(const std::string &interfaceName, const std::string &addrString,
361b1b8bc3fSopenharmony_ci                                                 int32_t prefixLength)
362b1b8bc3fSopenharmony_ci{
363b1b8bc3fSopenharmony_ci    int32_t result = netsysService_->DelInterfaceAddress(interfaceName, addrString, prefixLength);
364b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("DelInterfaceAddress");
365b1b8bc3fSopenharmony_ci    return result;
366b1b8bc3fSopenharmony_ci}
367b1b8bc3fSopenharmony_ci
368b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::DelInterfaceAddress(const std::string &interfaceName, const std::string &addrString,
369b1b8bc3fSopenharmony_ci                                                 int32_t prefixLength, const std::string &netCapabilities)
370b1b8bc3fSopenharmony_ci{
371b1b8bc3fSopenharmony_ci    int32_t result = netsysService_->DelInterfaceAddress(interfaceName, addrString, prefixLength, netCapabilities);
372b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("DelInterfaceAddress");
373b1b8bc3fSopenharmony_ci    return result;
374b1b8bc3fSopenharmony_ci}
375b1b8bc3fSopenharmony_ci
376b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::InterfaceSetIpAddress(const std::string &ifaceName, const std::string &ipAddress)
377b1b8bc3fSopenharmony_ci{
378b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("InterfaceSetIpAddress");
379b1b8bc3fSopenharmony_ci    return netsysService_->InterfaceSetIpAddress(ifaceName, ipAddress);
380b1b8bc3fSopenharmony_ci}
381b1b8bc3fSopenharmony_ci
382b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::InterfaceSetIffUp(const std::string &ifaceName)
383b1b8bc3fSopenharmony_ci{
384b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("InterfaceSetIffUp");
385b1b8bc3fSopenharmony_ci    return netsysService_->InterfaceSetIffUp(ifaceName);
386b1b8bc3fSopenharmony_ci}
387b1b8bc3fSopenharmony_ci
388b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::NetworkAddInterface(int32_t netId, const std::string &iface, NetBearType netBearerType)
389b1b8bc3fSopenharmony_ci{
390b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("NetworkAddInterface");
391b1b8bc3fSopenharmony_ci    int32_t result = netsysService_->NetworkAddInterface(netId, iface, netBearerType);
392b1b8bc3fSopenharmony_ci    return result;
393b1b8bc3fSopenharmony_ci}
394b1b8bc3fSopenharmony_ci
395b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::NetworkRemoveInterface(int32_t netId, const std::string &iface)
396b1b8bc3fSopenharmony_ci{
397b1b8bc3fSopenharmony_ci    int32_t result = netsysService_->NetworkRemoveInterface(netId, iface);
398b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("NetworkRemoveInterface");
399b1b8bc3fSopenharmony_ci    return result;
400b1b8bc3fSopenharmony_ci}
401b1b8bc3fSopenharmony_ci
402b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::NetworkDestroy(int32_t netId)
403b1b8bc3fSopenharmony_ci{
404b1b8bc3fSopenharmony_ci    int32_t result = netsysService_->NetworkDestroy(netId);
405b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("NetworkDestroy");
406b1b8bc3fSopenharmony_ci    return result;
407b1b8bc3fSopenharmony_ci}
408b1b8bc3fSopenharmony_ci
409b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::CreateVnic(uint16_t mtu, const std::string &tunAddr, int32_t prefix,
410b1b8bc3fSopenharmony_ci                                        const std::set<int32_t> &uids)
411b1b8bc3fSopenharmony_ci{
412b1b8bc3fSopenharmony_ci    int32_t result = netsysService_->CreateVnic(mtu, tunAddr, prefix, uids);
413b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("CreateVnic");
414b1b8bc3fSopenharmony_ci    return result;
415b1b8bc3fSopenharmony_ci}
416b1b8bc3fSopenharmony_ci
417b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::DestroyVnic()
418b1b8bc3fSopenharmony_ci{
419b1b8bc3fSopenharmony_ci    int32_t result = netsysService_->DestroyVnic();
420b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("DestroyVnic");
421b1b8bc3fSopenharmony_ci    return result;
422b1b8bc3fSopenharmony_ci}
423b1b8bc3fSopenharmony_ci
424b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::EnableDistributedClientNet(const std::string &virnicAddr,
425b1b8bc3fSopenharmony_ci                                                        const std::string &iif)
426b1b8bc3fSopenharmony_ci{
427b1b8bc3fSopenharmony_ci    if (virnicAddr.empty() || iif.empty()) {
428b1b8bc3fSopenharmony_ci        NETNATIVE_LOGE("EnableDistributedClientNet param is empty.");
429b1b8bc3fSopenharmony_ci        return NetManagerStandard::NETMANAGER_ERR_INVALID_PARAMETER;
430b1b8bc3fSopenharmony_ci    }
431b1b8bc3fSopenharmony_ci    int32_t result = netsysService_->EnableDistributedClientNet(virnicAddr, iif);
432b1b8bc3fSopenharmony_ci    NETNATIVE_LOGI("EnableDistributedClientNet");
433b1b8bc3fSopenharmony_ci    return result;
434b1b8bc3fSopenharmony_ci}
435b1b8bc3fSopenharmony_ci
436b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::EnableDistributedServerNet(const std::string &iif, const std::string &devIface,
437b1b8bc3fSopenharmony_ci                                                        const std::string &dstAddr)
438b1b8bc3fSopenharmony_ci{
439b1b8bc3fSopenharmony_ci    if (iif.empty() || devIface.empty() || dstAddr.empty()) {
440b1b8bc3fSopenharmony_ci        NETNATIVE_LOGE("EnableDistributedServerNet param is empty.");
441b1b8bc3fSopenharmony_ci        return NetManagerStandard::NETMANAGER_ERR_INVALID_PARAMETER;
442b1b8bc3fSopenharmony_ci    }
443b1b8bc3fSopenharmony_ci    int32_t result = netsysService_->EnableDistributedServerNet(iif, devIface, dstAddr);
444b1b8bc3fSopenharmony_ci    NETNATIVE_LOGI("EnableDistributedServerNet");
445b1b8bc3fSopenharmony_ci    return result;
446b1b8bc3fSopenharmony_ci}
447b1b8bc3fSopenharmony_ci
448b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::DisableDistributedNet(bool isServer)
449b1b8bc3fSopenharmony_ci{
450b1b8bc3fSopenharmony_ci    int32_t result = netsysService_->DisableDistributedNet(isServer);
451b1b8bc3fSopenharmony_ci    NETNATIVE_LOGI("DisableDistributedNet");
452b1b8bc3fSopenharmony_ci    return result;
453b1b8bc3fSopenharmony_ci}
454b1b8bc3fSopenharmony_ci
455b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::GetFwmarkForNetwork(int32_t netId, MarkMaskParcel &markMaskParcel)
456b1b8bc3fSopenharmony_ci{
457b1b8bc3fSopenharmony_ci    markMaskParcel = netsysService_->GetFwmarkForNetwork(netId);
458b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("GetFwmarkForNetwork");
459b1b8bc3fSopenharmony_ci    return ERR_NONE;
460b1b8bc3fSopenharmony_ci}
461b1b8bc3fSopenharmony_ci
462b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::SetInterfaceConfig(const InterfaceConfigurationParcel &cfg)
463b1b8bc3fSopenharmony_ci{
464b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("SetInterfaceConfig");
465b1b8bc3fSopenharmony_ci    netsysService_->SetInterfaceConfig(cfg);
466b1b8bc3fSopenharmony_ci    return ERR_NONE;
467b1b8bc3fSopenharmony_ci}
468b1b8bc3fSopenharmony_ci
469b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::GetInterfaceConfig(InterfaceConfigurationParcel &cfg)
470b1b8bc3fSopenharmony_ci{
471b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("GetInterfaceConfig");
472b1b8bc3fSopenharmony_ci    std::string ifName = cfg.ifName;
473b1b8bc3fSopenharmony_ci    cfg = netsysService_->GetInterfaceConfig(ifName);
474b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("GetInterfaceConfig end");
475b1b8bc3fSopenharmony_ci    return ERR_NONE;
476b1b8bc3fSopenharmony_ci}
477b1b8bc3fSopenharmony_ci
478b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::InterfaceGetList(std::vector<std::string> &ifaces)
479b1b8bc3fSopenharmony_ci{
480b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("InterfaceGetList");
481b1b8bc3fSopenharmony_ci    ifaces = netsysService_->InterfaceGetList();
482b1b8bc3fSopenharmony_ci    return ERR_NONE;
483b1b8bc3fSopenharmony_ci}
484b1b8bc3fSopenharmony_ci
485b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::StartDhcpClient(const std::string &iface, bool bIpv6)
486b1b8bc3fSopenharmony_ci{
487b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("StartDhcpClient");
488b1b8bc3fSopenharmony_ci    dhcpController_->StartClient(iface, bIpv6);
489b1b8bc3fSopenharmony_ci    return ERR_NONE;
490b1b8bc3fSopenharmony_ci}
491b1b8bc3fSopenharmony_ci
492b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::StopDhcpClient(const std::string &iface, bool bIpv6)
493b1b8bc3fSopenharmony_ci{
494b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("StopDhcpClient");
495b1b8bc3fSopenharmony_ci    dhcpController_->StopClient(iface, bIpv6);
496b1b8bc3fSopenharmony_ci    return ERR_NONE;
497b1b8bc3fSopenharmony_ci}
498b1b8bc3fSopenharmony_ci
499b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::StartDhcpService(const std::string &iface, const std::string &ipv4addr)
500b1b8bc3fSopenharmony_ci{
501b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("StartDhcpService");
502b1b8bc3fSopenharmony_ci    dhcpController_->StartDhcpService(iface, ipv4addr);
503b1b8bc3fSopenharmony_ci    return ERR_NONE;
504b1b8bc3fSopenharmony_ci}
505b1b8bc3fSopenharmony_ci
506b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::StopDhcpService(const std::string &iface)
507b1b8bc3fSopenharmony_ci{
508b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("StopDhcpService");
509b1b8bc3fSopenharmony_ci    dhcpController_->StopDhcpService(iface);
510b1b8bc3fSopenharmony_ci    return ERR_NONE;
511b1b8bc3fSopenharmony_ci}
512b1b8bc3fSopenharmony_ci
513b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::IpEnableForwarding(const std::string &requester)
514b1b8bc3fSopenharmony_ci{
515b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("ipEnableForwarding");
516b1b8bc3fSopenharmony_ci    return netsysService_->IpEnableForwarding(requester);
517b1b8bc3fSopenharmony_ci}
518b1b8bc3fSopenharmony_ci
519b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::IpDisableForwarding(const std::string &requester)
520b1b8bc3fSopenharmony_ci{
521b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("ipDisableForwarding");
522b1b8bc3fSopenharmony_ci    return netsysService_->IpDisableForwarding(requester);
523b1b8bc3fSopenharmony_ci}
524b1b8bc3fSopenharmony_ci
525b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::EnableNat(const std::string &downstreamIface, const std::string &upstreamIface)
526b1b8bc3fSopenharmony_ci{
527b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("enableNat");
528b1b8bc3fSopenharmony_ci    return netsysService_->EnableNat(downstreamIface, upstreamIface);
529b1b8bc3fSopenharmony_ci}
530b1b8bc3fSopenharmony_ci
531b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::DisableNat(const std::string &downstreamIface, const std::string &upstreamIface)
532b1b8bc3fSopenharmony_ci{
533b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("disableNat");
534b1b8bc3fSopenharmony_ci    return netsysService_->DisableNat(downstreamIface, upstreamIface);
535b1b8bc3fSopenharmony_ci}
536b1b8bc3fSopenharmony_ci
537b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::IpfwdAddInterfaceForward(const std::string &fromIface, const std::string &toIface)
538b1b8bc3fSopenharmony_ci{
539b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("ipfwdAddInterfaceForward");
540b1b8bc3fSopenharmony_ci    return netsysService_->IpfwdAddInterfaceForward(fromIface, toIface);
541b1b8bc3fSopenharmony_ci}
542b1b8bc3fSopenharmony_ci
543b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::IpfwdRemoveInterfaceForward(const std::string &fromIface, const std::string &toIface)
544b1b8bc3fSopenharmony_ci{
545b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("ipfwdRemoveInterfaceForward");
546b1b8bc3fSopenharmony_ci    return netsysService_->IpfwdRemoveInterfaceForward(fromIface, toIface);
547b1b8bc3fSopenharmony_ci}
548b1b8bc3fSopenharmony_ci
549b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::BandwidthEnableDataSaver(bool enable)
550b1b8bc3fSopenharmony_ci{
551b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("bandwidthEnableDataSaver");
552b1b8bc3fSopenharmony_ci    return netsysService_->BandwidthEnableDataSaver(enable);
553b1b8bc3fSopenharmony_ci}
554b1b8bc3fSopenharmony_ci
555b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::BandwidthSetIfaceQuota(const std::string &ifName, int64_t bytes)
556b1b8bc3fSopenharmony_ci{
557b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("BandwidthSetIfaceQuota");
558b1b8bc3fSopenharmony_ci    return netsysService_->BandwidthSetIfaceQuota(ifName, bytes);
559b1b8bc3fSopenharmony_ci}
560b1b8bc3fSopenharmony_ci
561b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::BandwidthRemoveIfaceQuota(const std::string &ifName)
562b1b8bc3fSopenharmony_ci{
563b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("BandwidthRemoveIfaceQuota");
564b1b8bc3fSopenharmony_ci    return netsysService_->BandwidthRemoveIfaceQuota(ifName);
565b1b8bc3fSopenharmony_ci}
566b1b8bc3fSopenharmony_ci
567b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::BandwidthAddDeniedList(uint32_t uid)
568b1b8bc3fSopenharmony_ci{
569b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("BandwidthAddDeniedList");
570b1b8bc3fSopenharmony_ci    return netsysService_->BandwidthAddDeniedList(uid);
571b1b8bc3fSopenharmony_ci}
572b1b8bc3fSopenharmony_ci
573b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::BandwidthRemoveDeniedList(uint32_t uid)
574b1b8bc3fSopenharmony_ci{
575b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("BandwidthRemoveDeniedList");
576b1b8bc3fSopenharmony_ci    return netsysService_->BandwidthRemoveDeniedList(uid);
577b1b8bc3fSopenharmony_ci}
578b1b8bc3fSopenharmony_ci
579b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::BandwidthAddAllowedList(uint32_t uid)
580b1b8bc3fSopenharmony_ci{
581b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("BandwidthAddAllowedList");
582b1b8bc3fSopenharmony_ci    return netsysService_->BandwidthAddAllowedList(uid);
583b1b8bc3fSopenharmony_ci}
584b1b8bc3fSopenharmony_ci
585b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::BandwidthRemoveAllowedList(uint32_t uid)
586b1b8bc3fSopenharmony_ci{
587b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("BandwidthRemoveAllowedList");
588b1b8bc3fSopenharmony_ci    return netsysService_->BandwidthRemoveAllowedList(uid);
589b1b8bc3fSopenharmony_ci}
590b1b8bc3fSopenharmony_ci
591b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::FirewallSetUidsAllowedListChain(uint32_t chain, const std::vector<uint32_t> &uids)
592b1b8bc3fSopenharmony_ci{
593b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("FirewallSetUidsAllowedListChain");
594b1b8bc3fSopenharmony_ci    return netsysService_->FirewallSetUidsAllowedListChain(chain, uids);
595b1b8bc3fSopenharmony_ci}
596b1b8bc3fSopenharmony_ci
597b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::FirewallSetUidsDeniedListChain(uint32_t chain, const std::vector<uint32_t> &uids)
598b1b8bc3fSopenharmony_ci{
599b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("FirewallSetUidsDeniedListChain");
600b1b8bc3fSopenharmony_ci    return netsysService_->FirewallSetUidsDeniedListChain(chain, uids);
601b1b8bc3fSopenharmony_ci}
602b1b8bc3fSopenharmony_ci
603b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::FirewallEnableChain(uint32_t chain, bool enable)
604b1b8bc3fSopenharmony_ci{
605b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("FirewallEnableChain");
606b1b8bc3fSopenharmony_ci    return netsysService_->FirewallEnableChain(chain, enable);
607b1b8bc3fSopenharmony_ci}
608b1b8bc3fSopenharmony_ci
609b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::FirewallSetUidRule(uint32_t chain, const std::vector<uint32_t> &uids,
610b1b8bc3fSopenharmony_ci                                                uint32_t firewallRule)
611b1b8bc3fSopenharmony_ci{
612b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("firewallSetUidRule");
613b1b8bc3fSopenharmony_ci    return netsysService_->FirewallSetUidRule(chain, uids, firewallRule);
614b1b8bc3fSopenharmony_ci}
615b1b8bc3fSopenharmony_ci
616b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::ShareDnsSet(uint16_t netid)
617b1b8bc3fSopenharmony_ci{
618b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("NetsysNativeService ShareDnsSet");
619b1b8bc3fSopenharmony_ci    if (netsysService_ == nullptr) {
620b1b8bc3fSopenharmony_ci        NETNATIVE_LOGE("netsysService_ is null");
621b1b8bc3fSopenharmony_ci        return -1;
622b1b8bc3fSopenharmony_ci    }
623b1b8bc3fSopenharmony_ci    netsysService_->ShareDnsSet(netid);
624b1b8bc3fSopenharmony_ci    return ERR_NONE;
625b1b8bc3fSopenharmony_ci}
626b1b8bc3fSopenharmony_ci
627b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::StartDnsProxyListen()
628b1b8bc3fSopenharmony_ci{
629b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("NetsysNativeService StartDnsProxyListen");
630b1b8bc3fSopenharmony_ci    if (netsysService_ == nullptr) {
631b1b8bc3fSopenharmony_ci        NETNATIVE_LOGE("netsysService_ is null");
632b1b8bc3fSopenharmony_ci        return -1;
633b1b8bc3fSopenharmony_ci    }
634b1b8bc3fSopenharmony_ci    netsysService_->StartDnsProxyListen();
635b1b8bc3fSopenharmony_ci    return ERR_NONE;
636b1b8bc3fSopenharmony_ci}
637b1b8bc3fSopenharmony_ci
638b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::StopDnsProxyListen()
639b1b8bc3fSopenharmony_ci{
640b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("NetsysNativeService StopDnsProxyListen");
641b1b8bc3fSopenharmony_ci    if (netsysService_ == nullptr) {
642b1b8bc3fSopenharmony_ci        NETNATIVE_LOGE("netsysService_ is null");
643b1b8bc3fSopenharmony_ci        return -1;
644b1b8bc3fSopenharmony_ci    }
645b1b8bc3fSopenharmony_ci    netsysService_->StopDnsProxyListen();
646b1b8bc3fSopenharmony_ci    return ERR_NONE;
647b1b8bc3fSopenharmony_ci}
648b1b8bc3fSopenharmony_ci
649b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::GetNetworkSharingTraffic(const std::string &downIface, const std::string &upIface,
650b1b8bc3fSopenharmony_ci                                                      NetworkSharingTraffic &traffic)
651b1b8bc3fSopenharmony_ci{
652b1b8bc3fSopenharmony_ci    if (sharingManager_ == nullptr) {
653b1b8bc3fSopenharmony_ci        NETNATIVE_LOGE("manager is null.");
654b1b8bc3fSopenharmony_ci        return NetManagerStandard::NETMANAGER_ERROR;
655b1b8bc3fSopenharmony_ci    }
656b1b8bc3fSopenharmony_ci    return sharingManager_->GetNetworkSharingTraffic(downIface, upIface, traffic);
657b1b8bc3fSopenharmony_ci}
658b1b8bc3fSopenharmony_ci
659b1b8bc3fSopenharmony_civoid NetsysNativeService::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
660b1b8bc3fSopenharmony_ci{
661b1b8bc3fSopenharmony_ci    NETNATIVE_LOGI("OnAddSystemAbility systemAbilityId[%{public}d]", systemAbilityId);
662b1b8bc3fSopenharmony_ci    if (systemAbilityId == COMM_NET_CONN_MANAGER_SYS_ABILITY_ID) {
663b1b8bc3fSopenharmony_ci        if (!hasSARemoved_) {
664b1b8bc3fSopenharmony_ci            hasSARemoved_ = true;
665b1b8bc3fSopenharmony_ci            return;
666b1b8bc3fSopenharmony_ci        }
667b1b8bc3fSopenharmony_ci        OnNetManagerRestart();
668b1b8bc3fSopenharmony_ci    }
669b1b8bc3fSopenharmony_ci}
670b1b8bc3fSopenharmony_ci
671b1b8bc3fSopenharmony_civoid NetsysNativeService::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
672b1b8bc3fSopenharmony_ci{
673b1b8bc3fSopenharmony_ci    NETNATIVE_LOGI("OnRemoveSystemAbility systemAbilityId[%{public}d]", systemAbilityId);
674b1b8bc3fSopenharmony_ci    if (systemAbilityId == COMM_NET_CONN_MANAGER_SYS_ABILITY_ID) {
675b1b8bc3fSopenharmony_ci        OnNetManagerRestart();
676b1b8bc3fSopenharmony_ci        hasSARemoved_ = true;
677b1b8bc3fSopenharmony_ci#ifdef FEATURE_NET_FIREWALL_ENABLE
678b1b8bc3fSopenharmony_ci    } else if (systemAbilityId == COMM_FIREWALL_MANAGER_SYS_ABILITY_ID) {
679b1b8bc3fSopenharmony_ci        bpfNetFirewall_->LoadSystemAbility(COMM_FIREWALL_MANAGER_SYS_ABILITY_ID);
680b1b8bc3fSopenharmony_ci#endif
681b1b8bc3fSopenharmony_ci    }
682b1b8bc3fSopenharmony_ci}
683b1b8bc3fSopenharmony_ci
684b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::GetTotalStats(uint64_t &stats, uint32_t type)
685b1b8bc3fSopenharmony_ci{
686b1b8bc3fSopenharmony_ci    if (bpfStats_ == nullptr) {
687b1b8bc3fSopenharmony_ci        NETNATIVE_LOGE("bpfStats is null.");
688b1b8bc3fSopenharmony_ci        return NetManagerStandard::NETMANAGER_ERROR;
689b1b8bc3fSopenharmony_ci    }
690b1b8bc3fSopenharmony_ci
691b1b8bc3fSopenharmony_ci    return bpfStats_->GetTotalStats(stats, static_cast<OHOS::NetManagerStandard::StatsType>(type));
692b1b8bc3fSopenharmony_ci}
693b1b8bc3fSopenharmony_ci
694b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::GetUidStats(uint64_t &stats, uint32_t type, uint32_t uid)
695b1b8bc3fSopenharmony_ci{
696b1b8bc3fSopenharmony_ci    if (bpfStats_ == nullptr) {
697b1b8bc3fSopenharmony_ci        NETNATIVE_LOGE("bpfStats is null.");
698b1b8bc3fSopenharmony_ci        return NetManagerStandard::NETMANAGER_ERROR;
699b1b8bc3fSopenharmony_ci    }
700b1b8bc3fSopenharmony_ci
701b1b8bc3fSopenharmony_ci    return bpfStats_->GetUidStats(stats, static_cast<OHOS::NetManagerStandard::StatsType>(type), uid);
702b1b8bc3fSopenharmony_ci}
703b1b8bc3fSopenharmony_ci
704b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::GetIfaceStats(uint64_t &stats, uint32_t type, const std::string &interfaceName)
705b1b8bc3fSopenharmony_ci{
706b1b8bc3fSopenharmony_ci    if (bpfStats_ == nullptr) {
707b1b8bc3fSopenharmony_ci        NETNATIVE_LOGE("bpfStats is null.");
708b1b8bc3fSopenharmony_ci        return NetManagerStandard::NETMANAGER_ERROR;
709b1b8bc3fSopenharmony_ci    }
710b1b8bc3fSopenharmony_ci
711b1b8bc3fSopenharmony_ci    return bpfStats_->GetIfaceStats(stats, static_cast<OHOS::NetManagerStandard::StatsType>(type), interfaceName);
712b1b8bc3fSopenharmony_ci}
713b1b8bc3fSopenharmony_ci
714b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::GetAllSimStatsInfo(std::vector<OHOS::NetManagerStandard::NetStatsInfo> &stats)
715b1b8bc3fSopenharmony_ci{
716b1b8bc3fSopenharmony_ci    if (bpfStats_ == nullptr) {
717b1b8bc3fSopenharmony_ci        NETNATIVE_LOGE("bpfStats is null.");
718b1b8bc3fSopenharmony_ci        return NetManagerStandard::NETMANAGER_ERROR;
719b1b8bc3fSopenharmony_ci    }
720b1b8bc3fSopenharmony_ci    return bpfStats_->GetAllSimStatsInfo(stats);
721b1b8bc3fSopenharmony_ci}
722b1b8bc3fSopenharmony_ci
723b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::DeleteSimStatsInfo(uint32_t uid)
724b1b8bc3fSopenharmony_ci{
725b1b8bc3fSopenharmony_ci    NETNATIVE_LOGI("DeleteSimStatsInfo uid[%{public}u]", uid);
726b1b8bc3fSopenharmony_ci    if (bpfStats_ == nullptr) {
727b1b8bc3fSopenharmony_ci        NETNATIVE_LOGE("bpfStats is null.");
728b1b8bc3fSopenharmony_ci        return NetManagerStandard::NETMANAGER_ERROR;
729b1b8bc3fSopenharmony_ci    }
730b1b8bc3fSopenharmony_ci    return bpfStats_->DeleteStatsInfo(APP_UID_SIM_STATS_MAP_PATH, uid);
731b1b8bc3fSopenharmony_ci}
732b1b8bc3fSopenharmony_ci
733b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::GetAllStatsInfo(std::vector<OHOS::NetManagerStandard::NetStatsInfo> &stats)
734b1b8bc3fSopenharmony_ci{
735b1b8bc3fSopenharmony_ci    if (bpfStats_ == nullptr) {
736b1b8bc3fSopenharmony_ci        NETNATIVE_LOGE("bpfStats is null.");
737b1b8bc3fSopenharmony_ci        return NetManagerStandard::NETMANAGER_ERROR;
738b1b8bc3fSopenharmony_ci    }
739b1b8bc3fSopenharmony_ci
740b1b8bc3fSopenharmony_ci    return bpfStats_->GetAllStatsInfo(stats);
741b1b8bc3fSopenharmony_ci}
742b1b8bc3fSopenharmony_ci
743b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::DeleteStatsInfo(uint32_t uid)
744b1b8bc3fSopenharmony_ci{
745b1b8bc3fSopenharmony_ci    NETNATIVE_LOGI("DeleteStatsInfo uid[%{public}u]", uid);
746b1b8bc3fSopenharmony_ci    if (bpfStats_ == nullptr) {
747b1b8bc3fSopenharmony_ci        NETNATIVE_LOGE("bpfStats is null.");
748b1b8bc3fSopenharmony_ci        return NetManagerStandard::NETMANAGER_ERROR;
749b1b8bc3fSopenharmony_ci    }
750b1b8bc3fSopenharmony_ci    return bpfStats_->DeleteStatsInfo(APP_UID_IF_STATS_MAP_PATH, uid);
751b1b8bc3fSopenharmony_ci}
752b1b8bc3fSopenharmony_ci
753b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::SetIptablesCommandForRes(const std::string &cmd, std::string &respond, IptablesType ipType)
754b1b8bc3fSopenharmony_ci{
755b1b8bc3fSopenharmony_ci    if (!regex_match(cmd, REGEX_CMD_IPTABLES)) {
756b1b8bc3fSopenharmony_ci        NETNATIVE_LOGE("IptablesWrapper command format is invalid");
757b1b8bc3fSopenharmony_ci        return NetManagerStandard::NETMANAGER_ERR_INVALID_PARAMETER;
758b1b8bc3fSopenharmony_ci    }
759b1b8bc3fSopenharmony_ci    if (iptablesWrapper_ == nullptr) {
760b1b8bc3fSopenharmony_ci        NETNATIVE_LOGE("SetIptablesCommandForRes iptablesWrapper_ is null");
761b1b8bc3fSopenharmony_ci        return NetManagerStandard::NETMANAGER_ERROR;
762b1b8bc3fSopenharmony_ci    }
763b1b8bc3fSopenharmony_ci    switch (ipType) {
764b1b8bc3fSopenharmony_ci        case IptablesType::IPTYPE_IPV4:
765b1b8bc3fSopenharmony_ci            respond = iptablesWrapper_->RunCommandForRes(OHOS::nmd::IpType::IPTYPE_IPV4, cmd);
766b1b8bc3fSopenharmony_ci            break;
767b1b8bc3fSopenharmony_ci        case IptablesType::IPTYPE_IPV6:
768b1b8bc3fSopenharmony_ci            respond = iptablesWrapper_->RunCommandForRes(OHOS::nmd::IpType::IPTYPE_IPV6, cmd);
769b1b8bc3fSopenharmony_ci            break;
770b1b8bc3fSopenharmony_ci        case IptablesType::IPTYPE_IPV4V6:
771b1b8bc3fSopenharmony_ci            respond = iptablesWrapper_->RunCommandForRes(OHOS::nmd::IpType::IPTYPE_IPV4V6, cmd);
772b1b8bc3fSopenharmony_ci            break;
773b1b8bc3fSopenharmony_ci        default:
774b1b8bc3fSopenharmony_ci            NETNATIVE_LOGE("IptablesWrapper ipputType is invalid");
775b1b8bc3fSopenharmony_ci            return NetManagerStandard::NETMANAGER_ERR_INVALID_PARAMETER;
776b1b8bc3fSopenharmony_ci    }
777b1b8bc3fSopenharmony_ci    return NetManagerStandard::NETMANAGER_SUCCESS;
778b1b8bc3fSopenharmony_ci}
779b1b8bc3fSopenharmony_ci
780b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::NetDiagPingHost(const NetDiagPingOption &pingOption,
781b1b8bc3fSopenharmony_ci                                             const sptr<INetDiagCallback> &callback)
782b1b8bc3fSopenharmony_ci{
783b1b8bc3fSopenharmony_ci    if (netDiagWrapper == nullptr) {
784b1b8bc3fSopenharmony_ci        NETNATIVE_LOGE("netDiagWrapper is null");
785b1b8bc3fSopenharmony_ci        return NetManagerStandard::NETMANAGER_ERR_LOCAL_PTR_NULL;
786b1b8bc3fSopenharmony_ci    }
787b1b8bc3fSopenharmony_ci    return netDiagWrapper->PingHost(pingOption, callback);
788b1b8bc3fSopenharmony_ci}
789b1b8bc3fSopenharmony_ci
790b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::NetDiagGetRouteTable(std::list<NetDiagRouteTable> &routeTables)
791b1b8bc3fSopenharmony_ci{
792b1b8bc3fSopenharmony_ci    if (netDiagWrapper == nullptr) {
793b1b8bc3fSopenharmony_ci        NETNATIVE_LOGE("netDiagWrapper is null");
794b1b8bc3fSopenharmony_ci        return NetManagerStandard::NETMANAGER_ERR_LOCAL_PTR_NULL;
795b1b8bc3fSopenharmony_ci    }
796b1b8bc3fSopenharmony_ci    return netDiagWrapper->GetRouteTable(routeTables);
797b1b8bc3fSopenharmony_ci}
798b1b8bc3fSopenharmony_ci
799b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::NetDiagGetSocketsInfo(NetDiagProtocolType socketType, NetDiagSocketsInfo &socketsInfo)
800b1b8bc3fSopenharmony_ci{
801b1b8bc3fSopenharmony_ci    if (netDiagWrapper == nullptr) {
802b1b8bc3fSopenharmony_ci        NETNATIVE_LOGE("netDiagWrapper is null");
803b1b8bc3fSopenharmony_ci        return NetManagerStandard::NETMANAGER_ERR_LOCAL_PTR_NULL;
804b1b8bc3fSopenharmony_ci    }
805b1b8bc3fSopenharmony_ci    return netDiagWrapper->GetSocketsInfo(socketType, socketsInfo);
806b1b8bc3fSopenharmony_ci}
807b1b8bc3fSopenharmony_ci
808b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::NetDiagGetInterfaceConfig(std::list<NetDiagIfaceConfig> &configs,
809b1b8bc3fSopenharmony_ci                                                       const std::string &ifaceName)
810b1b8bc3fSopenharmony_ci{
811b1b8bc3fSopenharmony_ci    if (netDiagWrapper == nullptr) {
812b1b8bc3fSopenharmony_ci        NETNATIVE_LOGE("netDiagWrapper is null");
813b1b8bc3fSopenharmony_ci        return NetManagerStandard::NETMANAGER_ERR_LOCAL_PTR_NULL;
814b1b8bc3fSopenharmony_ci    }
815b1b8bc3fSopenharmony_ci    return netDiagWrapper->GetInterfaceConfig(configs, ifaceName);
816b1b8bc3fSopenharmony_ci}
817b1b8bc3fSopenharmony_ci
818b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::NetDiagUpdateInterfaceConfig(const NetDiagIfaceConfig &config,
819b1b8bc3fSopenharmony_ci                                                          const std::string &ifaceName, bool add)
820b1b8bc3fSopenharmony_ci{
821b1b8bc3fSopenharmony_ci    if (netDiagWrapper == nullptr) {
822b1b8bc3fSopenharmony_ci        NETNATIVE_LOGE("netDiagWrapper is null");
823b1b8bc3fSopenharmony_ci        return NetManagerStandard::NETMANAGER_ERR_LOCAL_PTR_NULL;
824b1b8bc3fSopenharmony_ci    }
825b1b8bc3fSopenharmony_ci    return netDiagWrapper->UpdateInterfaceConfig(config, ifaceName, add);
826b1b8bc3fSopenharmony_ci}
827b1b8bc3fSopenharmony_ci
828b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::NetDiagSetInterfaceActiveState(const std::string &ifaceName, bool up)
829b1b8bc3fSopenharmony_ci{
830b1b8bc3fSopenharmony_ci    if (netDiagWrapper == nullptr) {
831b1b8bc3fSopenharmony_ci        NETNATIVE_LOGE("netDiagWrapper is null");
832b1b8bc3fSopenharmony_ci        return NetManagerStandard::NETMANAGER_ERR_LOCAL_PTR_NULL;
833b1b8bc3fSopenharmony_ci    }
834b1b8bc3fSopenharmony_ci    return netDiagWrapper->SetInterfaceActiveState(ifaceName, up);
835b1b8bc3fSopenharmony_ci}
836b1b8bc3fSopenharmony_ci
837b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::AddStaticArp(const std::string &ipAddr, const std::string &macAddr,
838b1b8bc3fSopenharmony_ci                                          const std::string &ifName)
839b1b8bc3fSopenharmony_ci{
840b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("AddStaticArp");
841b1b8bc3fSopenharmony_ci    if (netsysService_ == nullptr) {
842b1b8bc3fSopenharmony_ci        NETNATIVE_LOGE("netsysService_ is null");
843b1b8bc3fSopenharmony_ci        return NetManagerStandard::NETMANAGER_ERR_LOCAL_PTR_NULL;
844b1b8bc3fSopenharmony_ci    }
845b1b8bc3fSopenharmony_ci    return netsysService_->AddStaticArp(ipAddr, macAddr, ifName);
846b1b8bc3fSopenharmony_ci}
847b1b8bc3fSopenharmony_ci
848b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::DelStaticArp(const std::string &ipAddr, const std::string &macAddr,
849b1b8bc3fSopenharmony_ci                                          const std::string &ifName)
850b1b8bc3fSopenharmony_ci{
851b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("DelStaticArp");
852b1b8bc3fSopenharmony_ci    if (netsysService_ == nullptr) {
853b1b8bc3fSopenharmony_ci        NETNATIVE_LOGE("netsysService_ is null");
854b1b8bc3fSopenharmony_ci        return NetManagerStandard::NETMANAGER_ERR_LOCAL_PTR_NULL;
855b1b8bc3fSopenharmony_ci    }
856b1b8bc3fSopenharmony_ci    return netsysService_->DelStaticArp(ipAddr, macAddr, ifName);
857b1b8bc3fSopenharmony_ci}
858b1b8bc3fSopenharmony_ci
859b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::RegisterDnsResultCallback(const sptr<INetDnsResultCallback> &callback, uint32_t timeStep)
860b1b8bc3fSopenharmony_ci{
861b1b8bc3fSopenharmony_ci    return netsysService_->RegisterDnsResultCallback(callback, timeStep);
862b1b8bc3fSopenharmony_ci}
863b1b8bc3fSopenharmony_ci
864b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::UnregisterDnsResultCallback(const sptr<INetDnsResultCallback> &callback)
865b1b8bc3fSopenharmony_ci{
866b1b8bc3fSopenharmony_ci    return netsysService_->UnregisterDnsResultCallback(callback);
867b1b8bc3fSopenharmony_ci}
868b1b8bc3fSopenharmony_ci
869b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::RegisterDnsHealthCallback(const sptr<INetDnsHealthCallback> &callback)
870b1b8bc3fSopenharmony_ci{
871b1b8bc3fSopenharmony_ci    return netsysService_->RegisterDnsHealthCallback(callback);
872b1b8bc3fSopenharmony_ci}
873b1b8bc3fSopenharmony_ci
874b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::UnregisterDnsHealthCallback(const sptr<INetDnsHealthCallback> &callback)
875b1b8bc3fSopenharmony_ci{
876b1b8bc3fSopenharmony_ci    return netsysService_->UnregisterDnsHealthCallback(callback);
877b1b8bc3fSopenharmony_ci}
878b1b8bc3fSopenharmony_ci
879b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::SetIpv6PrivacyExtensions(const std::string &interfaceName, const uint32_t on)
880b1b8bc3fSopenharmony_ci{
881b1b8bc3fSopenharmony_ci    int32_t result = netsysService_->SetIpv6PrivacyExtensions(interfaceName, on);
882b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("SetIpv6PrivacyExtensions");
883b1b8bc3fSopenharmony_ci    return result;
884b1b8bc3fSopenharmony_ci}
885b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::SetEnableIpv6(const std::string &interfaceName, const uint32_t on)
886b1b8bc3fSopenharmony_ci{
887b1b8bc3fSopenharmony_ci    int32_t result = netsysService_->SetEnableIpv6(interfaceName, on);
888b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("SetEnableIpv6");
889b1b8bc3fSopenharmony_ci    return result;
890b1b8bc3fSopenharmony_ci}
891b1b8bc3fSopenharmony_ci
892b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::GetCookieStats(uint64_t &stats, uint32_t type, uint64_t cookie)
893b1b8bc3fSopenharmony_ci{
894b1b8bc3fSopenharmony_ci    if (bpfStats_ == nullptr) {
895b1b8bc3fSopenharmony_ci        NETNATIVE_LOGE("bpfStats is null.");
896b1b8bc3fSopenharmony_ci        return NetManagerStandard::NETMANAGER_ERROR;
897b1b8bc3fSopenharmony_ci    }
898b1b8bc3fSopenharmony_ci
899b1b8bc3fSopenharmony_ci    return bpfStats_->GetCookieStats(stats, static_cast<OHOS::NetManagerStandard::StatsType>(type), cookie);
900b1b8bc3fSopenharmony_ci}
901b1b8bc3fSopenharmony_ci
902b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::GetNetworkSharingType(std::set<uint32_t>& sharingTypeIsOn)
903b1b8bc3fSopenharmony_ci{
904b1b8bc3fSopenharmony_ci    NETNATIVE_LOGI("GetNetworkSharingType");
905b1b8bc3fSopenharmony_ci    std::lock_guard<std::mutex> guard(instanceLock_);
906b1b8bc3fSopenharmony_ci    sharingTypeIsOn = sharingTypeIsOn_;
907b1b8bc3fSopenharmony_ci    return NETSYS_SUCCESS;
908b1b8bc3fSopenharmony_ci}
909b1b8bc3fSopenharmony_ci
910b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::UpdateNetworkSharingType(uint32_t type, bool isOpen)
911b1b8bc3fSopenharmony_ci{
912b1b8bc3fSopenharmony_ci    NETNATIVE_LOGI("UpdateNetworkSharingType");
913b1b8bc3fSopenharmony_ci    std::lock_guard<std::mutex> guard(instanceLock_);
914b1b8bc3fSopenharmony_ci    if (isOpen) {
915b1b8bc3fSopenharmony_ci        sharingTypeIsOn_.insert(type);
916b1b8bc3fSopenharmony_ci    } else {
917b1b8bc3fSopenharmony_ci        sharingTypeIsOn_.erase(type);
918b1b8bc3fSopenharmony_ci    }
919b1b8bc3fSopenharmony_ci    return NETSYS_SUCCESS;
920b1b8bc3fSopenharmony_ci}
921b1b8bc3fSopenharmony_ci
922b1b8bc3fSopenharmony_ci#ifdef FEATURE_NET_FIREWALL_ENABLE
923b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::SetFirewallRules(NetFirewallRuleType type,
924b1b8bc3fSopenharmony_ci                                              const std::vector<sptr<NetFirewallBaseRule>> &ruleList, bool isFinish)
925b1b8bc3fSopenharmony_ci{
926b1b8bc3fSopenharmony_ci    NETNATIVE_LOGI("NetsysNativeService::SetFirewallRules: size=%{public}zu isFinish=%{public}" PRId32, ruleList.size(),
927b1b8bc3fSopenharmony_ci                   isFinish);
928b1b8bc3fSopenharmony_ci    int32_t ret = NETSYS_SUCCESS;
929b1b8bc3fSopenharmony_ci    switch (type) {
930b1b8bc3fSopenharmony_ci        case NetFirewallRuleType::RULE_IP:
931b1b8bc3fSopenharmony_ci            ret = bpfNetFirewall_->SetFirewallRules(ruleList, isFinish);
932b1b8bc3fSopenharmony_ci            break;
933b1b8bc3fSopenharmony_ci        case NetFirewallRuleType::RULE_DOMAIN:
934b1b8bc3fSopenharmony_ci        case NetFirewallRuleType::RULE_DNS:
935b1b8bc3fSopenharmony_ci            ret = netsysService_->SetFirewallRules(type, ruleList, isFinish);
936b1b8bc3fSopenharmony_ci            break;
937b1b8bc3fSopenharmony_ci        default:
938b1b8bc3fSopenharmony_ci            break;
939b1b8bc3fSopenharmony_ci    }
940b1b8bc3fSopenharmony_ci    return ret;
941b1b8bc3fSopenharmony_ci}
942b1b8bc3fSopenharmony_ci
943b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::SetFirewallDefaultAction(FirewallRuleAction inDefault, FirewallRuleAction outDefault)
944b1b8bc3fSopenharmony_ci{
945b1b8bc3fSopenharmony_ci    NETNATIVE_LOGI("NetsysNativeService::SetFirewallDefaultAction");
946b1b8bc3fSopenharmony_ci    int32_t ret = netsysService_->SetFirewallDefaultAction(inDefault, outDefault);
947b1b8bc3fSopenharmony_ci    ret += bpfNetFirewall_->SetFirewallDefaultAction(inDefault, outDefault);
948b1b8bc3fSopenharmony_ci    return ret;
949b1b8bc3fSopenharmony_ci}
950b1b8bc3fSopenharmony_ci
951b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::SetFirewallCurrentUserId(int32_t userId)
952b1b8bc3fSopenharmony_ci{
953b1b8bc3fSopenharmony_ci    NETNATIVE_LOGI("NetsysNativeService::SetFirewallCurrentUserId");
954b1b8bc3fSopenharmony_ci    int32_t ret = netsysService_->SetFirewallCurrentUserId(userId);
955b1b8bc3fSopenharmony_ci    ret += bpfNetFirewall_->SetFirewallCurrentUserId(userId);
956b1b8bc3fSopenharmony_ci    return ret;
957b1b8bc3fSopenharmony_ci}
958b1b8bc3fSopenharmony_ci
959b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::ClearFirewallRules(NetFirewallRuleType type)
960b1b8bc3fSopenharmony_ci{
961b1b8bc3fSopenharmony_ci    NETNATIVE_LOGI("NetsysNativeService::ClearFirewallRules");
962b1b8bc3fSopenharmony_ci    int32_t ret = NETSYS_SUCCESS;
963b1b8bc3fSopenharmony_ci    switch (type) {
964b1b8bc3fSopenharmony_ci        case NetFirewallRuleType::RULE_IP:
965b1b8bc3fSopenharmony_ci            ret = bpfNetFirewall_->ClearFirewallRules();
966b1b8bc3fSopenharmony_ci            break;
967b1b8bc3fSopenharmony_ci        case NetFirewallRuleType::RULE_DNS:
968b1b8bc3fSopenharmony_ci        case NetFirewallRuleType::RULE_DOMAIN:
969b1b8bc3fSopenharmony_ci            ret = netsysService_->ClearFirewallRules(type);
970b1b8bc3fSopenharmony_ci            break;
971b1b8bc3fSopenharmony_ci        case NetFirewallRuleType::RULE_ALL:
972b1b8bc3fSopenharmony_ci            ret = bpfNetFirewall_->ClearFirewallRules();
973b1b8bc3fSopenharmony_ci            ret += netsysService_->ClearFirewallRules(NetFirewallRuleType::RULE_ALL);
974b1b8bc3fSopenharmony_ci            break;
975b1b8bc3fSopenharmony_ci        default:
976b1b8bc3fSopenharmony_ci            break;
977b1b8bc3fSopenharmony_ci    }
978b1b8bc3fSopenharmony_ci    return ret;
979b1b8bc3fSopenharmony_ci}
980b1b8bc3fSopenharmony_ci
981b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::RegisterNetFirewallCallback(const sptr<INetFirewallCallback> &callback)
982b1b8bc3fSopenharmony_ci{
983b1b8bc3fSopenharmony_ci    NETNATIVE_LOGI("NetsysNativeService::RegisterNetFirewallCallback");
984b1b8bc3fSopenharmony_ci    int32_t ret = netsysService_->RegisterNetFirewallCallback(callback);
985b1b8bc3fSopenharmony_ci    ret += bpfNetFirewall_->RegisterCallback(callback);
986b1b8bc3fSopenharmony_ci    return ret;
987b1b8bc3fSopenharmony_ci}
988b1b8bc3fSopenharmony_ci
989b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::UnRegisterNetFirewallCallback(const sptr<INetFirewallCallback> &callback)
990b1b8bc3fSopenharmony_ci{
991b1b8bc3fSopenharmony_ci    NETNATIVE_LOGI("NetsysNativeService::UnRegisterNetFirewallCallback");
992b1b8bc3fSopenharmony_ci    int32_t ret = netsysService_->UnRegisterNetFirewallCallback(callback);
993b1b8bc3fSopenharmony_ci    ret += bpfNetFirewall_->UnregisterCallback(callback);
994b1b8bc3fSopenharmony_ci    return ret;
995b1b8bc3fSopenharmony_ci}
996b1b8bc3fSopenharmony_ci#endif
997b1b8bc3fSopenharmony_ci
998b1b8bc3fSopenharmony_ci#ifdef FEATURE_WEARABLE_DISTRIBUTED_NET_ENABLE
999b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::EnableWearableDistributedNetForward(const int32_t tcpPortId, const int32_t udpPortId)
1000b1b8bc3fSopenharmony_ci{
1001b1b8bc3fSopenharmony_ci    NETNATIVE_LOGI("Enabling wearable distributed net forward for TCP port and UDP port");
1002b1b8bc3fSopenharmony_ci    return netsysService_->EnableWearableDistributedNetForward(tcpPortId, udpPortId);
1003b1b8bc3fSopenharmony_ci}
1004b1b8bc3fSopenharmony_ci
1005b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::DisableWearableDistributedNetForward()
1006b1b8bc3fSopenharmony_ci{
1007b1b8bc3fSopenharmony_ci    NETNATIVE_LOGI("NetsysNativeService Disable Wearable Distributed NetForward");
1008b1b8bc3fSopenharmony_ci    return netsysService_->DisableWearableDistributedNetForward();
1009b1b8bc3fSopenharmony_ci}
1010b1b8bc3fSopenharmony_ci#endif
1011b1b8bc3fSopenharmony_ci
1012b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::SetNetworkAccessPolicy(uint32_t uid, NetworkAccessPolicy policy, bool reconfirmFlag,
1013b1b8bc3fSopenharmony_ci                                                    bool isBroker)
1014b1b8bc3fSopenharmony_ci{
1015b1b8bc3fSopenharmony_ci    NETNATIVE_LOGI("SetNetworkAccessPolicy");
1016b1b8bc3fSopenharmony_ci
1017b1b8bc3fSopenharmony_ci    return netsysService_->SetNetworkAccessPolicy(uid, policy, reconfirmFlag, isBroker);
1018b1b8bc3fSopenharmony_ci}
1019b1b8bc3fSopenharmony_ci
1020b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::DeleteNetworkAccessPolicy(uint32_t uid)
1021b1b8bc3fSopenharmony_ci{
1022b1b8bc3fSopenharmony_ci    NETNATIVE_LOGI("DeleteNetworkAccessPolicy");
1023b1b8bc3fSopenharmony_ci    return netsysService_->DeleteNetworkAccessPolicy(uid);
1024b1b8bc3fSopenharmony_ci}
1025b1b8bc3fSopenharmony_ci
1026b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::NotifyNetBearerTypeChange(std::set<NetBearType> bearerTypes)
1027b1b8bc3fSopenharmony_ci{
1028b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("NotifyNetBearerTypeChange");
1029b1b8bc3fSopenharmony_ci    return netsysService_->NotifyNetBearerTypeChange(bearerTypes);
1030b1b8bc3fSopenharmony_ci}
1031b1b8bc3fSopenharmony_ci
1032b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::StartClat(const std::string &interfaceName, int32_t netId,
1033b1b8bc3fSopenharmony_ci                                       const std::string &nat64PrefixStr)
1034b1b8bc3fSopenharmony_ci{
1035b1b8bc3fSopenharmony_ci    int32_t result = clatManager_->ClatStart(interfaceName, netId, nat64PrefixStr, netsysService_.get());
1036b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("StartClat");
1037b1b8bc3fSopenharmony_ci    return result;
1038b1b8bc3fSopenharmony_ci}
1039b1b8bc3fSopenharmony_ci
1040b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::StopClat(const std::string &interfaceName)
1041b1b8bc3fSopenharmony_ci{
1042b1b8bc3fSopenharmony_ci    int32_t result = clatManager_->ClatStop(interfaceName);
1043b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("StartClat");
1044b1b8bc3fSopenharmony_ci    return result;
1045b1b8bc3fSopenharmony_ci}
1046b1b8bc3fSopenharmony_ci
1047b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::ClearFirewallAllRules()
1048b1b8bc3fSopenharmony_ci{
1049b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("ClearFirewallAllRules");
1050b1b8bc3fSopenharmony_ci    return netsysService_->ClearFirewallAllRules();
1051b1b8bc3fSopenharmony_ci}
1052b1b8bc3fSopenharmony_ci
1053b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::SetNicTrafficAllowed(const std::vector<std::string> &ifaceNames, bool allowed)
1054b1b8bc3fSopenharmony_ci{
1055b1b8bc3fSopenharmony_ci    if (iptablesWrapper_ == nullptr) {
1056b1b8bc3fSopenharmony_ci        NETNATIVE_LOGE("SetNicTrafficAllowed iptablesWrapper_ is null");
1057b1b8bc3fSopenharmony_ci        return NetManagerStandard::NETMANAGER_ERROR;
1058b1b8bc3fSopenharmony_ci    }
1059b1b8bc3fSopenharmony_ci    bool ret = false;
1060b1b8bc3fSopenharmony_ci    std::vector<std::string> cmds;
1061b1b8bc3fSopenharmony_ci    for (const std::string& ifaceName : ifaceNames) {
1062b1b8bc3fSopenharmony_ci        if (allowed) {
1063b1b8bc3fSopenharmony_ci            NETNATIVE_LOG_D("SetNicTrafficAllowed %{public}s allowed", ifaceName.c_str());
1064b1b8bc3fSopenharmony_ci            cmds.push_back("-t raw -D OUTPUT -o " + ifaceName + " -j DROP");
1065b1b8bc3fSopenharmony_ci            cmds.push_back("-t raw -D PREROUTING -i " + ifaceName + " -j DROP");
1066b1b8bc3fSopenharmony_ci        } else {
1067b1b8bc3fSopenharmony_ci            NETNATIVE_LOG_D("SetNicTrafficAllowed %{public}s disallowed", ifaceName.c_str());
1068b1b8bc3fSopenharmony_ci            cmds.push_back("-t raw -I OUTPUT -o " + ifaceName + " -j DROP");
1069b1b8bc3fSopenharmony_ci            cmds.push_back("-t raw -I PREROUTING -i " + ifaceName + " -j DROP");
1070b1b8bc3fSopenharmony_ci        }
1071b1b8bc3fSopenharmony_ci    }
1072b1b8bc3fSopenharmony_ci    ret = IptablesWrapper::GetInstance()->RunMutipleCommands(OHOS::nmd::IpType::IPTYPE_IPV4V6, cmds);
1073b1b8bc3fSopenharmony_ci    if (ret) {
1074b1b8bc3fSopenharmony_ci        NETNATIVE_LOGE("SetNicTrafficAllowed iptablesWrapper_ apply failed");
1075b1b8bc3fSopenharmony_ci        return NetManagerStandard::NETMANAGER_ERROR;
1076b1b8bc3fSopenharmony_ci    }
1077b1b8bc3fSopenharmony_ci    NETNATIVE_LOG_D("SetNicTrafficAllowed iptablesWrapper_ apply success");
1078b1b8bc3fSopenharmony_ci    return NetManagerStandard::NETMANAGER_SUCCESS;
1079b1b8bc3fSopenharmony_ci}
1080b1b8bc3fSopenharmony_ci
1081b1b8bc3fSopenharmony_ci#ifdef SUPPORT_SYSVPN
1082b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::ProcessVpnStage(NetsysNative::SysVpnStageCode stage)
1083b1b8bc3fSopenharmony_ci{
1084b1b8bc3fSopenharmony_ci    NETNATIVE_LOGI("ProcessVpnStage stage %{public}d", stage);
1085b1b8bc3fSopenharmony_ci    if (SystemVpnWrapper::GetInstance() == nullptr) {
1086b1b8bc3fSopenharmony_ci        NETNATIVE_LOGE("ProcessVpnStage SystemVpnWrapper is null");
1087b1b8bc3fSopenharmony_ci        return NetManagerStandard::NETMANAGER_ERROR;
1088b1b8bc3fSopenharmony_ci    }
1089b1b8bc3fSopenharmony_ci    int32_t ret = SystemVpnWrapper::GetInstance()->Update(stage);
1090b1b8bc3fSopenharmony_ci    if (ret != NetManagerStandard::NETMANAGER_SUCCESS) {
1091b1b8bc3fSopenharmony_ci        NETNATIVE_LOGE("ProcessVpnStage failed");
1092b1b8bc3fSopenharmony_ci        return NetManagerStandard::NETMANAGER_ERROR;
1093b1b8bc3fSopenharmony_ci    }
1094b1b8bc3fSopenharmony_ci    return NetManagerStandard::NETMANAGER_SUCCESS;
1095b1b8bc3fSopenharmony_ci}
1096b1b8bc3fSopenharmony_ci#endif // SUPPORT_SYSVPN
1097b1b8bc3fSopenharmony_ci
1098b1b8bc3fSopenharmony_ciint32_t NetsysNativeService::CloseSocketsUid(const std::string &ipAddr, uint32_t uid)
1099b1b8bc3fSopenharmony_ci{
1100b1b8bc3fSopenharmony_ci    NETNATIVE_LOGI("CloseSocketsUid uid[%{public}d]", uid);
1101b1b8bc3fSopenharmony_ci    return netsysService_->CloseSocketsUid(ipAddr, uid);
1102b1b8bc3fSopenharmony_ci}
1103b1b8bc3fSopenharmony_ci} // namespace NetsysNative
1104b1b8bc3fSopenharmony_ci} // namespace OHOS
1105