• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "net_manager_constants.h"
26 #include "netmanager_base_common_utils.h"
27 #include "netnative_log_wrapper.h"
28 #include "netsys_native_service.h"
29 
30 using namespace OHOS::NetManagerStandard::CommonUtils;
31 namespace OHOS {
32 namespace NetsysNative {
33 constexpr int32_t START_TIME_MS = 1900;
34 constexpr int32_t EXTRA_MONTH = 1;
35 static constexpr const char *BFP_NAME_NETSYS_PATH = "/system/etc/bpf/netsys.o";
36 const std::regex REGEX_CMD_IPTABLES(std::string(R"(^-[\S]*[\s\S]*)"));
37 
REGISTER_SYSTEM_ABILITY_BY_ID(NetsysNativeService,COMM_NETSYS_NATIVE_SYS_ABILITY_ID,true)38 REGISTER_SYSTEM_ABILITY_BY_ID(NetsysNativeService, COMM_NETSYS_NATIVE_SYS_ABILITY_ID, true)
39 
40 NetsysNativeService::NetsysNativeService()
41     : SystemAbility(COMM_NETSYS_NATIVE_SYS_ABILITY_ID, true),
42       netsysService_(nullptr),
43       manager_(nullptr),
44       notifyCallback_(nullptr)
45 {
46 }
47 
OnStart()48 void NetsysNativeService::OnStart()
49 {
50     NETNATIVE_LOGI("NetsysNativeService::OnStart Begin");
51     std::lock_guard<std::mutex> guard(instanceLock_);
52     if (state_ == ServiceRunningState::STATE_RUNNING) {
53         return;
54     }
55 
56     if (!Init()) {
57         NETNATIVE_LOGE("NetsysNativeService init failed!");
58         return;
59     }
60     bool res = SystemAbility::Publish(this);
61     if (!res) {
62         NETNATIVE_LOGE("publishing NetsysNativeService to sa manager failed!");
63         return;
64     }
65     NETNATIVE_LOGI("Publish NetsysNativeService SUCCESS");
66     state_ = ServiceRunningState::STATE_RUNNING;
67     struct tm *timeNow;
68     time_t second = time(0);
69     if (second < 0) {
70         return;
71     }
72     timeNow = localtime(&second);
73     if (timeNow != nullptr) {
74         NETNATIVE_LOGI(
75             "NetsysNativeService start time:%{public}d-%{public}d-%{public}d %{public}d:%{public}d:%{public}d",
76             timeNow->tm_year + START_TIME_MS, timeNow->tm_mon + EXTRA_MONTH, timeNow->tm_mday, timeNow->tm_hour,
77             timeNow->tm_min, timeNow->tm_sec);
78     }
79     manager_->StartListener();
80 }
81 
OnStop()82 void NetsysNativeService::OnStop()
83 {
84     std::lock_guard<std::mutex> guard(instanceLock_);
85     struct tm *timeNow;
86     time_t second = time(0);
87     if (second < 0) {
88         return;
89     }
90     timeNow = localtime(&second);
91     if (timeNow != nullptr) {
92         NETNATIVE_LOGI(
93             "NetsysNativeService dump time:%{public}d-%{public}d-%{public}d %{public}d:%{public}d:%{public}d",
94             timeNow->tm_year + START_TIME_MS, timeNow->tm_mon + EXTRA_MONTH, timeNow->tm_mday, timeNow->tm_hour,
95             timeNow->tm_min, timeNow->tm_sec);
96     }
97     state_ = ServiceRunningState::STATE_STOPPED;
98     manager_->StopListener();
99 }
100 
Dump(int32_t fd,const std::vector<std::u16string> & args)101 int32_t NetsysNativeService::Dump(int32_t fd, const std::vector<std::u16string> &args)
102 {
103     NETNATIVE_LOG_D("Start Dump, fd: %{public}d", fd);
104     std::string result;
105     GetDumpMessage(result);
106     int32_t ret = dprintf(fd, "%s\n", result.c_str());
107     return ret < 0 ? SESSION_UNOPEN_ERR : ERR_NONE;
108 }
109 
GetDumpMessage(std::string & message)110 void NetsysNativeService::GetDumpMessage(std::string &message)
111 {
112     netsysService_->GetDumpInfo(message);
113 }
114 
ExitHandler(int32_t signum)115 void ExitHandler(int32_t signum)
116 {
117     (void)signum;
118     _Exit(1);
119 }
120 
Init()121 bool NetsysNativeService::Init()
122 {
123     (void)signal(SIGTERM, ExitHandler);
124     (void)signal(SIGABRT, ExitHandler);
125 
126     netsysService_ = std::make_unique<nmd::NetManagerNative>();
127     if (netsysService_ == nullptr) {
128         NETNATIVE_LOGE("netsysService_ is nullptr!");
129         return false;
130     }
131     netsysService_->Init();
132 
133     manager_ = std::make_unique<OHOS::nmd::NetlinkManager>();
134     if (manager_ == nullptr) {
135         NETNATIVE_LOGE("manager_ is nullptr!");
136         return false;
137     }
138     bpfStats_ = std::make_unique<OHOS::NetManagerStandard::NetsysBpfStats>();
139     dhcpController_ = std::make_unique<OHOS::nmd::DhcpController>();
140     fwmarkNetwork_ = std::make_unique<OHOS::nmd::FwmarkNetwork>();
141     sharingManager_ = std::make_unique<SharingManager>();
142     iptablesWrapper_ = IptablesWrapper::GetInstance();
143 
144     auto ret = OHOS::NetManagerStandard::LoadElf(BFP_NAME_NETSYS_PATH);
145     NETNATIVE_LOGI("LoadElf is %{public}d", ret);
146     AddSystemAbilityListener(COMM_NET_CONN_MANAGER_SYS_ABILITY_ID);
147     return true;
148 }
149 
OnNetManagerRestart()150 void NetsysNativeService::OnNetManagerRestart()
151 {
152     NETNATIVE_LOGI("NetsysNativeClient::OnNetManagerRestart");
153     if (netsysService_ != nullptr) {
154         netsysService_->NetworkReinitRoute();
155     }
156     if (manager_ != nullptr && notifyCallback_ != nullptr) {
157         manager_->UnregisterNetlinkCallback(notifyCallback_);
158     }
159 }
160 
SetResolverConfig(uint16_t netId,uint16_t baseTimeoutMsec,uint8_t retryCount,const std::vector<std::string> & servers,const std::vector<std::string> & domains)161 int32_t NetsysNativeService::SetResolverConfig(uint16_t netId, uint16_t baseTimeoutMsec, uint8_t retryCount,
162                                                const std::vector<std::string> &servers,
163                                                const std::vector<std::string> &domains)
164 {
165     netsysService_->DnsSetResolverConfig(netId, baseTimeoutMsec, retryCount, servers, domains);
166     return 0;
167 }
168 
GetResolverConfig(uint16_t netid,std::vector<std::string> & servers,std::vector<std::string> & domains,uint16_t & baseTimeoutMsec,uint8_t & retryCount)169 int32_t NetsysNativeService::GetResolverConfig(uint16_t netid, std::vector<std::string> &servers,
170                                                std::vector<std::string> &domains, uint16_t &baseTimeoutMsec,
171                                                uint8_t &retryCount)
172 {
173     NETNATIVE_LOG_D("GetResolverConfig netid = %{public}d", netid);
174     netsysService_->DnsGetResolverConfig(netid, servers, domains, baseTimeoutMsec, retryCount);
175     return 0;
176 }
177 
CreateNetworkCache(uint16_t netid)178 int32_t NetsysNativeService::CreateNetworkCache(uint16_t netid)
179 {
180     NETNATIVE_LOG_D("CreateNetworkCache Begin");
181     netsysService_->DnsCreateNetworkCache(netid);
182 
183     return 0;
184 }
185 
DestroyNetworkCache(uint16_t netId)186 int32_t NetsysNativeService::DestroyNetworkCache(uint16_t netId)
187 {
188     NETNATIVE_LOG_D("DestroyNetworkCache");
189     return netsysService_->DnsDestroyNetworkCache(netId);
190 }
191 
GetAddrInfo(const std::string & hostName,const std::string & serverName,const AddrInfo & hints,uint16_t netId,std::vector<AddrInfo> & res)192 int32_t NetsysNativeService::GetAddrInfo(const std::string &hostName, const std::string &serverName,
193                                          const AddrInfo &hints, uint16_t netId, std::vector<AddrInfo> &res)
194 {
195     return netsysService_->DnsGetAddrInfo(hostName, serverName, hints, netId, res);
196 }
197 
SetInterfaceMtu(const std::string & interfaceName,int32_t mtu)198 int32_t NetsysNativeService::SetInterfaceMtu(const std::string &interfaceName, int32_t mtu)
199 {
200     NETNATIVE_LOG_D("SetInterfaceMtu  Begin");
201     return netsysService_->SetInterfaceMtu(interfaceName, mtu);
202 }
203 
GetInterfaceMtu(const std::string & interfaceName)204 int32_t NetsysNativeService::GetInterfaceMtu(const std::string &interfaceName)
205 {
206     NETNATIVE_LOG_D("SetInterfaceMtu  Begin");
207     return netsysService_->GetInterfaceMtu(interfaceName);
208 }
209 
RegisterNotifyCallback(sptr<INotifyCallback> & callback)210 int32_t NetsysNativeService::RegisterNotifyCallback(sptr<INotifyCallback> &callback)
211 {
212     NETNATIVE_LOG_D("RegisterNotifyCallback");
213     notifyCallback_ = callback;
214     dhcpController_->RegisterNotifyCallback(callback);
215     manager_->RegisterNetlinkCallback(callback);
216     return 0;
217 }
218 
UnRegisterNotifyCallback(sptr<INotifyCallback> & callback)219 int32_t NetsysNativeService::UnRegisterNotifyCallback(sptr<INotifyCallback> &callback)
220 {
221     NETNATIVE_LOGI("UnRegisterNotifyCallback");
222     manager_->UnregisterNetlinkCallback(notifyCallback_);
223     return 0;
224 }
225 
NetworkAddRoute(int32_t netId,const std::string & interfaceName,const std::string & destination,const std::string & nextHop)226 int32_t NetsysNativeService::NetworkAddRoute(int32_t netId, const std::string &interfaceName,
227                                              const std::string &destination, const std::string &nextHop)
228 {
229     NETNATIVE_LOG_D("NetsysNativeService::NetworkAddRoute unpacket %{public}d %{public}s %{public}s %{public}s", netId,
230                     interfaceName.c_str(), ToAnonymousIp(destination).c_str(), ToAnonymousIp(nextHop).c_str());
231 
232     int32_t result = netsysService_->NetworkAddRoute(netId, interfaceName, destination, nextHop);
233     NETNATIVE_LOG_D("NetworkAddRoute %{public}d", result);
234     return result;
235 }
236 
NetworkRemoveRoute(int32_t netId,const std::string & interfaceName,const std::string & destination,const std::string & nextHop)237 int32_t NetsysNativeService::NetworkRemoveRoute(int32_t netId, const std::string &interfaceName,
238                                                 const std::string &destination, const std::string &nextHop)
239 {
240     int32_t result = netsysService_->NetworkRemoveRoute(netId, interfaceName, destination, nextHop);
241     NETNATIVE_LOG_D("NetworkRemoveRoute %{public}d", result);
242     return result;
243 }
244 
NetworkAddRouteParcel(int32_t netId,const RouteInfoParcel & routeInfo)245 int32_t NetsysNativeService::NetworkAddRouteParcel(int32_t netId, const RouteInfoParcel &routeInfo)
246 {
247     int32_t result = netsysService_->NetworkAddRouteParcel(netId, routeInfo);
248     NETNATIVE_LOG_D("NetworkAddRouteParcel %{public}d", result);
249     return result;
250 }
251 
NetworkRemoveRouteParcel(int32_t netId,const RouteInfoParcel & routeInfo)252 int32_t NetsysNativeService::NetworkRemoveRouteParcel(int32_t netId, const RouteInfoParcel &routeInfo)
253 {
254     int32_t result = netsysService_->NetworkRemoveRouteParcel(netId, routeInfo);
255     NETNATIVE_LOG_D("NetworkRemoveRouteParcel %{public}d", result);
256     return result;
257 }
258 
NetworkSetDefault(int32_t netId)259 int32_t NetsysNativeService::NetworkSetDefault(int32_t netId)
260 {
261     NETNATIVE_LOG_D("NetworkSetDefault in.");
262     int32_t result = netsysService_->NetworkSetDefault(netId);
263     NETNATIVE_LOG_D("NetworkSetDefault out.");
264     return result;
265 }
266 
NetworkGetDefault()267 int32_t NetsysNativeService::NetworkGetDefault()
268 {
269     int32_t result = netsysService_->NetworkGetDefault();
270     NETNATIVE_LOG_D("NetworkGetDefault");
271     return result;
272 }
273 
NetworkClearDefault()274 int32_t NetsysNativeService::NetworkClearDefault()
275 {
276     int32_t result = netsysService_->NetworkClearDefault();
277     NETNATIVE_LOG_D("NetworkClearDefault");
278     return result;
279 }
280 
GetProcSysNet(int32_t family,int32_t which,const std::string & ifname,const std::string & parameter,std::string & value)281 int32_t NetsysNativeService::GetProcSysNet(int32_t family, int32_t which, const std::string &ifname,
282                                            const std::string &parameter, std::string &value)
283 {
284     int32_t result = netsysService_->GetProcSysNet(family, which, ifname, parameter, &value);
285     NETNATIVE_LOG_D("GetProcSysNet");
286     return result;
287 }
288 
SetProcSysNet(int32_t family,int32_t which,const std::string & ifname,const std::string & parameter,std::string & value)289 int32_t NetsysNativeService::SetProcSysNet(int32_t family, int32_t which, const std::string &ifname,
290                                            const std::string &parameter, std::string &value)
291 {
292     int32_t result = netsysService_->SetProcSysNet(family, which, ifname, parameter, value);
293     NETNATIVE_LOG_D("SetProcSysNet");
294     return result;
295 }
296 
SetInternetPermission(uint32_t uid,uint8_t allow)297 int32_t NetsysNativeService::SetInternetPermission(uint32_t uid, uint8_t allow)
298 {
299     int32_t result = netsysService_->SetInternetPermission(uid, allow);
300     NETNATIVE_LOG_D("SetInternetPermission out.");
301     return result;
302 }
303 
NetworkCreatePhysical(int32_t netId,int32_t permission)304 int32_t NetsysNativeService::NetworkCreatePhysical(int32_t netId, int32_t permission)
305 {
306     int32_t result = netsysService_->NetworkCreatePhysical(netId, permission);
307     NETNATIVE_LOG_D("NetworkCreatePhysical out.");
308     return result;
309 }
310 
NetworkCreateVirtual(int32_t netId,bool hasDns)311 int32_t NetsysNativeService::NetworkCreateVirtual(int32_t netId, bool hasDns)
312 {
313     int32_t result = netsysService_->NetworkCreateVirtual(netId, hasDns);
314     NETNATIVE_LOG_D("NetworkCreateVirtual out.");
315     return result;
316 }
317 
NetworkAddUids(int32_t netId,const std::vector<UidRange> & uidRanges)318 int32_t NetsysNativeService::NetworkAddUids(int32_t netId, const std::vector<UidRange> &uidRanges)
319 {
320     int32_t result = netsysService_->NetworkAddUids(netId, uidRanges);
321     NETNATIVE_LOG_D("NetworkAddUids out.");
322     return result;
323 }
324 
NetworkDelUids(int32_t netId,const std::vector<UidRange> & uidRanges)325 int32_t NetsysNativeService::NetworkDelUids(int32_t netId, const std::vector<UidRange> &uidRanges)
326 {
327     int32_t result = netsysService_->NetworkDelUids(netId, uidRanges);
328     NETNATIVE_LOG_D("NetworkDelUids out.");
329     return result;
330 }
331 
AddInterfaceAddress(const std::string & interfaceName,const std::string & addrString,int32_t prefixLength)332 int32_t NetsysNativeService::AddInterfaceAddress(const std::string &interfaceName, const std::string &addrString,
333                                                  int32_t prefixLength)
334 {
335     int32_t result = netsysService_->AddInterfaceAddress(interfaceName, addrString, prefixLength);
336     NETNATIVE_LOG_D("AddInterfaceAddress");
337     return result;
338 }
339 
DelInterfaceAddress(const std::string & interfaceName,const std::string & addrString,int32_t prefixLength)340 int32_t NetsysNativeService::DelInterfaceAddress(const std::string &interfaceName, const std::string &addrString,
341                                                  int32_t prefixLength)
342 {
343     int32_t result = netsysService_->DelInterfaceAddress(interfaceName, addrString, prefixLength);
344     NETNATIVE_LOG_D("DelInterfaceAddress");
345     return result;
346 }
347 
InterfaceSetIpAddress(const std::string & ifaceName,const std::string & ipAddress)348 int32_t NetsysNativeService::InterfaceSetIpAddress(const std::string &ifaceName, const std::string &ipAddress)
349 {
350     NETNATIVE_LOG_D("InterfaceSetIpAddress");
351     return netsysService_->InterfaceSetIpAddress(ifaceName, ipAddress);
352 }
353 
InterfaceSetIffUp(const std::string & ifaceName)354 int32_t NetsysNativeService::InterfaceSetIffUp(const std::string &ifaceName)
355 {
356     NETNATIVE_LOG_D("InterfaceSetIffUp");
357     return netsysService_->InterfaceSetIffUp(ifaceName);
358 }
359 
NetworkAddInterface(int32_t netId,const std::string & iface)360 int32_t NetsysNativeService::NetworkAddInterface(int32_t netId, const std::string &iface)
361 {
362     NETNATIVE_LOG_D("NetworkAddInterface");
363     int32_t result = netsysService_->NetworkAddInterface(netId, iface);
364     return result;
365 }
366 
NetworkRemoveInterface(int32_t netId,const std::string & iface)367 int32_t NetsysNativeService::NetworkRemoveInterface(int32_t netId, const std::string &iface)
368 {
369     int32_t result = netsysService_->NetworkRemoveInterface(netId, iface);
370     NETNATIVE_LOG_D("NetworkRemoveInterface");
371     return result;
372 }
373 
NetworkDestroy(int32_t netId)374 int32_t NetsysNativeService::NetworkDestroy(int32_t netId)
375 {
376     int32_t result = netsysService_->NetworkDestroy(netId);
377     NETNATIVE_LOG_D("NetworkDestroy");
378     return result;
379 }
380 
GetFwmarkForNetwork(int32_t netId,MarkMaskParcel & markMaskParcel)381 int32_t NetsysNativeService::GetFwmarkForNetwork(int32_t netId, MarkMaskParcel &markMaskParcel)
382 {
383     markMaskParcel = netsysService_->GetFwmarkForNetwork(netId);
384     NETNATIVE_LOG_D("GetFwmarkForNetwork");
385     return ERR_NONE;
386 }
387 
SetInterfaceConfig(const InterfaceConfigurationParcel & cfg)388 int32_t NetsysNativeService::SetInterfaceConfig(const InterfaceConfigurationParcel &cfg)
389 {
390     NETNATIVE_LOG_D("SetInterfaceConfig");
391     netsysService_->SetInterfaceConfig(cfg);
392     return ERR_NONE;
393 }
394 
GetInterfaceConfig(InterfaceConfigurationParcel & cfg)395 int32_t NetsysNativeService::GetInterfaceConfig(InterfaceConfigurationParcel &cfg)
396 {
397     NETNATIVE_LOG_D("GetInterfaceConfig");
398     std::string ifName = cfg.ifName;
399     cfg = netsysService_->GetInterfaceConfig(ifName);
400     NETNATIVE_LOG_D("GetInterfaceConfig end");
401     return ERR_NONE;
402 }
403 
InterfaceGetList(std::vector<std::string> & ifaces)404 int32_t NetsysNativeService::InterfaceGetList(std::vector<std::string> &ifaces)
405 {
406     NETNATIVE_LOG_D("InterfaceGetList");
407     ifaces = netsysService_->InterfaceGetList();
408     return ERR_NONE;
409 }
410 
StartDhcpClient(const std::string & iface,bool bIpv6)411 int32_t NetsysNativeService::StartDhcpClient(const std::string &iface, bool bIpv6)
412 {
413     NETNATIVE_LOG_D("StartDhcpClient");
414     dhcpController_->StartDhcpClient(iface, bIpv6);
415     return ERR_NONE;
416 }
417 
StopDhcpClient(const std::string & iface,bool bIpv6)418 int32_t NetsysNativeService::StopDhcpClient(const std::string &iface, bool bIpv6)
419 {
420     NETNATIVE_LOG_D("StopDhcpClient");
421     dhcpController_->StopDhcpClient(iface, bIpv6);
422     return ERR_NONE;
423 }
424 
StartDhcpService(const std::string & iface,const std::string & ipv4addr)425 int32_t NetsysNativeService::StartDhcpService(const std::string &iface, const std::string &ipv4addr)
426 {
427     NETNATIVE_LOG_D("StartDhcpService");
428     dhcpController_->StartDhcpService(iface, ipv4addr);
429     return ERR_NONE;
430 }
431 
StopDhcpService(const std::string & iface)432 int32_t NetsysNativeService::StopDhcpService(const std::string &iface)
433 {
434     NETNATIVE_LOG_D("StopDhcpService");
435     dhcpController_->StopDhcpService(iface);
436     return ERR_NONE;
437 }
438 
IpEnableForwarding(const std::string & requester)439 int32_t NetsysNativeService::IpEnableForwarding(const std::string &requester)
440 {
441     NETNATIVE_LOG_D("ipEnableForwarding");
442     return netsysService_->IpEnableForwarding(requester);
443 }
444 
IpDisableForwarding(const std::string & requester)445 int32_t NetsysNativeService::IpDisableForwarding(const std::string &requester)
446 {
447     NETNATIVE_LOG_D("ipDisableForwarding");
448     return netsysService_->IpDisableForwarding(requester);
449 }
450 
EnableNat(const std::string & downstreamIface,const std::string & upstreamIface)451 int32_t NetsysNativeService::EnableNat(const std::string &downstreamIface, const std::string &upstreamIface)
452 {
453     NETNATIVE_LOG_D("enableNat");
454     return netsysService_->EnableNat(downstreamIface, upstreamIface);
455 }
456 
DisableNat(const std::string & downstreamIface,const std::string & upstreamIface)457 int32_t NetsysNativeService::DisableNat(const std::string &downstreamIface, const std::string &upstreamIface)
458 {
459     NETNATIVE_LOG_D("disableNat");
460     return netsysService_->DisableNat(downstreamIface, upstreamIface);
461 }
462 
IpfwdAddInterfaceForward(const std::string & fromIface,const std::string & toIface)463 int32_t NetsysNativeService::IpfwdAddInterfaceForward(const std::string &fromIface, const std::string &toIface)
464 {
465     NETNATIVE_LOG_D("ipfwdAddInterfaceForward");
466     return netsysService_->IpfwdAddInterfaceForward(fromIface, toIface);
467 }
468 
IpfwdRemoveInterfaceForward(const std::string & fromIface,const std::string & toIface)469 int32_t NetsysNativeService::IpfwdRemoveInterfaceForward(const std::string &fromIface, const std::string &toIface)
470 {
471     NETNATIVE_LOG_D("ipfwdRemoveInterfaceForward");
472     return netsysService_->IpfwdRemoveInterfaceForward(fromIface, toIface);
473 }
474 
BandwidthEnableDataSaver(bool enable)475 int32_t NetsysNativeService::BandwidthEnableDataSaver(bool enable)
476 {
477     NETNATIVE_LOG_D("bandwidthEnableDataSaver");
478     return netsysService_->BandwidthEnableDataSaver(enable);
479 }
480 
BandwidthSetIfaceQuota(const std::string & ifName,int64_t bytes)481 int32_t NetsysNativeService::BandwidthSetIfaceQuota(const std::string &ifName, int64_t bytes)
482 {
483     NETNATIVE_LOG_D("BandwidthSetIfaceQuota");
484     return netsysService_->BandwidthSetIfaceQuota(ifName, bytes);
485 }
486 
BandwidthRemoveIfaceQuota(const std::string & ifName)487 int32_t NetsysNativeService::BandwidthRemoveIfaceQuota(const std::string &ifName)
488 {
489     NETNATIVE_LOG_D("BandwidthRemoveIfaceQuota");
490     return netsysService_->BandwidthRemoveIfaceQuota(ifName);
491 }
492 
BandwidthAddDeniedList(uint32_t uid)493 int32_t NetsysNativeService::BandwidthAddDeniedList(uint32_t uid)
494 {
495     NETNATIVE_LOG_D("BandwidthAddDeniedList");
496     return netsysService_->BandwidthAddDeniedList(uid);
497 }
498 
BandwidthRemoveDeniedList(uint32_t uid)499 int32_t NetsysNativeService::BandwidthRemoveDeniedList(uint32_t uid)
500 {
501     NETNATIVE_LOG_D("BandwidthRemoveDeniedList");
502     return netsysService_->BandwidthRemoveDeniedList(uid);
503 }
504 
BandwidthAddAllowedList(uint32_t uid)505 int32_t NetsysNativeService::BandwidthAddAllowedList(uint32_t uid)
506 {
507     NETNATIVE_LOG_D("BandwidthAddAllowedList");
508     return netsysService_->BandwidthAddAllowedList(uid);
509 }
510 
BandwidthRemoveAllowedList(uint32_t uid)511 int32_t NetsysNativeService::BandwidthRemoveAllowedList(uint32_t uid)
512 {
513     NETNATIVE_LOG_D("BandwidthRemoveAllowedList");
514     return netsysService_->BandwidthRemoveAllowedList(uid);
515 }
516 
FirewallSetUidsAllowedListChain(uint32_t chain,const std::vector<uint32_t> & uids)517 int32_t NetsysNativeService::FirewallSetUidsAllowedListChain(uint32_t chain, const std::vector<uint32_t> &uids)
518 {
519     NETNATIVE_LOG_D("FirewallSetUidsAllowedListChain");
520     return netsysService_->FirewallSetUidsAllowedListChain(chain, uids);
521 }
522 
FirewallSetUidsDeniedListChain(uint32_t chain,const std::vector<uint32_t> & uids)523 int32_t NetsysNativeService::FirewallSetUidsDeniedListChain(uint32_t chain, const std::vector<uint32_t> &uids)
524 {
525     NETNATIVE_LOG_D("FirewallSetUidsDeniedListChain");
526     return netsysService_->FirewallSetUidsDeniedListChain(chain, uids);
527 }
528 
FirewallEnableChain(uint32_t chain,bool enable)529 int32_t NetsysNativeService::FirewallEnableChain(uint32_t chain, bool enable)
530 {
531     NETNATIVE_LOG_D("FirewallEnableChain");
532     return netsysService_->FirewallEnableChain(chain, enable);
533 }
534 
FirewallSetUidRule(uint32_t chain,const std::vector<uint32_t> & uids,uint32_t firewallRule)535 int32_t NetsysNativeService::FirewallSetUidRule(uint32_t chain, const std::vector<uint32_t> &uids,
536                                                 uint32_t firewallRule)
537 {
538     NETNATIVE_LOG_D("firewallSetUidRule");
539     return netsysService_->FirewallSetUidRule(chain, uids, firewallRule);
540 }
541 
ShareDnsSet(uint16_t netid)542 int32_t NetsysNativeService::ShareDnsSet(uint16_t netid)
543 {
544     NETNATIVE_LOG_D("NetsysNativeService ShareDnsSet");
545     if (netsysService_ == nullptr) {
546         NETNATIVE_LOGE("netsysService_ is null");
547         return -1;
548     }
549     netsysService_->ShareDnsSet(netid);
550     return ERR_NONE;
551 }
552 
StartDnsProxyListen()553 int32_t NetsysNativeService::StartDnsProxyListen()
554 {
555     NETNATIVE_LOG_D("NetsysNativeService StartDnsProxyListen");
556     if (netsysService_ == nullptr) {
557         NETNATIVE_LOGE("netsysService_ is null");
558         return -1;
559     }
560     netsysService_->StartDnsProxyListen();
561     return ERR_NONE;
562 }
563 
StopDnsProxyListen()564 int32_t NetsysNativeService::StopDnsProxyListen()
565 {
566     NETNATIVE_LOG_D("NetsysNativeService StopDnsProxyListen");
567     if (netsysService_ == nullptr) {
568         NETNATIVE_LOGE("netsysService_ is null");
569         return -1;
570     }
571     netsysService_->StopDnsProxyListen();
572     return ERR_NONE;
573 }
574 
GetNetworkSharingTraffic(const std::string & downIface,const std::string & upIface,NetworkSharingTraffic & traffic)575 int32_t NetsysNativeService::GetNetworkSharingTraffic(const std::string &downIface, const std::string &upIface,
576                                                       NetworkSharingTraffic &traffic)
577 {
578     if (sharingManager_ == nullptr) {
579         NETNATIVE_LOGE("manager is null.");
580         return NetManagerStandard::NETMANAGER_ERROR;
581     }
582     return sharingManager_->GetNetworkSharingTraffic(downIface, upIface, traffic);
583 }
584 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)585 void NetsysNativeService::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
586 {
587     NETNATIVE_LOGI("NetsysNativeService::OnAddSystemAbility systemAbilityId[%{public}d]", systemAbilityId);
588     if (systemAbilityId == COMM_NET_CONN_MANAGER_SYS_ABILITY_ID) {
589         if (!hasSARemoved_) {
590             hasSARemoved_ = true;
591             return;
592         }
593         OnNetManagerRestart();
594     }
595 }
596 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)597 void NetsysNativeService::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
598 {
599     NETNATIVE_LOGI("NetsysNativeService::OnRemoveSystemAbility systemAbilityId[%{public}d]", systemAbilityId);
600     if (systemAbilityId == COMM_NET_CONN_MANAGER_SYS_ABILITY_ID) {
601         OnNetManagerRestart();
602         hasSARemoved_ = true;
603     }
604 }
605 
GetTotalStats(uint64_t & stats,uint32_t type)606 int32_t NetsysNativeService::GetTotalStats(uint64_t &stats, uint32_t type)
607 {
608     if (bpfStats_ == nullptr) {
609         NETNATIVE_LOGE("bpfStats is null.");
610         return NetManagerStandard::NETMANAGER_ERROR;
611     }
612 
613     return bpfStats_->GetTotalStats(stats, static_cast<OHOS::NetManagerStandard::StatsType>(type));
614 }
615 
GetUidStats(uint64_t & stats,uint32_t type,uint32_t uid)616 int32_t NetsysNativeService::GetUidStats(uint64_t &stats, uint32_t type, uint32_t uid)
617 {
618     if (bpfStats_ == nullptr) {
619         NETNATIVE_LOGE("bpfStats is null.");
620         return NetManagerStandard::NETMANAGER_ERROR;
621     }
622 
623     return bpfStats_->GetUidStats(stats, static_cast<OHOS::NetManagerStandard::StatsType>(type), uid);
624 }
625 
GetIfaceStats(uint64_t & stats,uint32_t type,const std::string & interfaceName)626 int32_t NetsysNativeService::GetIfaceStats(uint64_t &stats, uint32_t type, const std::string &interfaceName)
627 {
628     if (bpfStats_ == nullptr) {
629         NETNATIVE_LOGE("bpfStats is null.");
630         return NetManagerStandard::NETMANAGER_ERROR;
631     }
632 
633     return bpfStats_->GetIfaceStats(stats, static_cast<OHOS::NetManagerStandard::StatsType>(type), interfaceName);
634 }
635 
GetAllStatsInfo(std::vector<OHOS::NetManagerStandard::NetStatsInfo> & stats)636 int32_t NetsysNativeService::GetAllStatsInfo(std::vector<OHOS::NetManagerStandard::NetStatsInfo> &stats)
637 {
638     if (bpfStats_ == nullptr) {
639         NETNATIVE_LOGE("bpfStats is null.");
640         return NetManagerStandard::NETMANAGER_ERROR;
641     }
642 
643     return bpfStats_->GetAllStatsInfo(stats);
644 }
645 
SetIptablesCommandForRes(const std::string & cmd,std::string & respond)646 int32_t NetsysNativeService::SetIptablesCommandForRes(const std::string &cmd, std::string &respond)
647 {
648     if (!regex_match(cmd, REGEX_CMD_IPTABLES)) {
649         NETNATIVE_LOGE("IptablesWrapper command format is invalid");
650         return NetManagerStandard::NETMANAGER_ERR_INVALID_PARAMETER;
651     }
652     if (iptablesWrapper_ == nullptr) {
653         NETNATIVE_LOGE("SetIptablesCommandForRes iptablesWrapper_ is null");
654         return NetManagerStandard::NETMANAGER_ERROR;
655     }
656     respond = iptablesWrapper_->RunCommandForRes(IPTYPE_IPV4V6, cmd);
657     return NetManagerStandard::NETMANAGER_SUCCESS;
658 }
659 } // namespace NetsysNative
660 } // namespace OHOS
661