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