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