• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <net/if.h>
17 
18 #include "interface_manager.h"
19 #include "net_manager_constants.h"
20 #include "net_manager_native.h"
21 #include "netmanager_base_common_utils.h"
22 #include "netnative_log_wrapper.h"
23 #include "network_permission.h"
24 #include "route_manager.h"
25 #include "traffic_manager.h"
26 #include "vpn_manager.h"
27 
28 using namespace OHOS::NetManagerStandard::CommonUtils;
29 std::vector<uint32_t> OHOS::nmd::NetManagerNative::interfaceIdex_;
30 
31 namespace OHOS {
32 namespace nmd {
33 namespace {
34 constexpr const char *TUN_CARD_NAME = "vpn-tun";
35 } // namespace
36 
NetManagerNative()37 NetManagerNative::NetManagerNative()
38     : bandwidthManager_(std::make_shared<BandwidthManager>()),
39       connManager_(std::make_shared<ConnManager>()),
40       firewallManager_(std::make_shared<FirewallManager>()),
41       routeManager_(std::make_shared<RouteManager>()),
42       interfaceManager_(std::make_shared<InterfaceManager>()),
43       sharingManager_(std::make_shared<SharingManager>()),
44       dnsManager_(std::make_shared<DnsManager>())
45 {
46 }
47 
GetOriginInterfaceIndex()48 void NetManagerNative::GetOriginInterfaceIndex()
49 {
50     std::vector<std::string> ifNameList = InterfaceManager::GetInterfaceNames();
51     interfaceIdex_.clear();
52     for (auto iter = ifNameList.begin(); iter != ifNameList.end(); ++iter) {
53         uint32_t infIndex = if_nametoindex((*iter).c_str());
54         interfaceIdex_.push_back(infIndex);
55     }
56 }
57 
UpdateInterfaceIndex(uint32_t infIndex)58 void NetManagerNative::UpdateInterfaceIndex(uint32_t infIndex)
59 {
60     interfaceIdex_.push_back(infIndex);
61 }
62 
GetCurrentInterfaceIndex()63 std::vector<uint32_t> NetManagerNative::GetCurrentInterfaceIndex()
64 {
65     return interfaceIdex_;
66 }
67 
Init()68 void NetManagerNative::Init()
69 {
70     GetOriginInterfaceIndex();
71 }
72 
NetworkReinitRoute()73 int32_t NetManagerNative::NetworkReinitRoute()
74 {
75     return connManager_->ReinitRoute();
76 }
77 
SetInternetPermission(uint32_t uid,uint8_t allow)78 int32_t NetManagerNative::SetInternetPermission(uint32_t uid, uint8_t allow)
79 {
80     return connManager_->SetInternetPermission(uid, allow);
81 }
82 
NetworkCreatePhysical(int32_t netId,int32_t permission)83 int32_t NetManagerNative::NetworkCreatePhysical(int32_t netId, int32_t permission)
84 {
85     return connManager_->CreatePhysicalNetwork(static_cast<uint16_t>(netId),
86                                                static_cast<NetworkPermission>(permission));
87 }
88 
NetworkCreateVirtual(int32_t netId,bool hasDns)89 int32_t NetManagerNative::NetworkCreateVirtual(int32_t netId, bool hasDns)
90 {
91     return connManager_->CreateVirtualNetwork(netId, hasDns);
92 }
93 
NetworkDestroy(int32_t netId)94 int32_t NetManagerNative::NetworkDestroy(int32_t netId)
95 {
96     return connManager_->DestroyNetwork(netId);
97 }
98 
NetworkAddUids(int32_t netId,const std::vector<UidRange> & uidRanges)99 int32_t NetManagerNative::NetworkAddUids(int32_t netId, const std::vector<UidRange> &uidRanges)
100 {
101     return connManager_->AddUidsToNetwork(netId, uidRanges);
102 }
103 
NetworkDelUids(int32_t netId,const std::vector<UidRange> & uidRanges)104 int32_t NetManagerNative::NetworkDelUids(int32_t netId, const std::vector<UidRange> &uidRanges)
105 {
106     return connManager_->RemoveUidsFromNetwork(netId, uidRanges);
107 }
108 
NetworkAddInterface(int32_t netId,std::string interfaceName)109 int32_t NetManagerNative::NetworkAddInterface(int32_t netId, std::string interfaceName)
110 {
111     return connManager_->AddInterfaceToNetwork(netId, interfaceName);
112 }
113 
NetworkRemoveInterface(int32_t netId,std::string interfaceName)114 int32_t NetManagerNative::NetworkRemoveInterface(int32_t netId, std::string interfaceName)
115 {
116     return connManager_->RemoveInterfaceFromNetwork(netId, interfaceName);
117 }
118 
AddInterfaceAddress(std::string ifName,std::string addrString,int32_t prefixLength)119 int32_t NetManagerNative::AddInterfaceAddress(std::string ifName, std::string addrString, int32_t prefixLength)
120 {
121     if (strncmp(ifName.c_str(), TUN_CARD_NAME, strlen(TUN_CARD_NAME)) != 0) {
122         return interfaceManager_->AddAddress(ifName.c_str(), addrString.c_str(), prefixLength);
123     }
124     return VpnManager::GetInstance().SetVpnAddress(ifName, addrString, prefixLength);
125 }
126 
DelInterfaceAddress(std::string ifName,std::string addrString,int32_t prefixLength)127 int32_t NetManagerNative::DelInterfaceAddress(std::string ifName, std::string addrString, int32_t prefixLength)
128 {
129     if (strncmp(ifName.c_str(), TUN_CARD_NAME, strlen(TUN_CARD_NAME)) != 0) {
130         return interfaceManager_->DelAddress(ifName.c_str(), addrString.c_str(), prefixLength);
131     }
132     return NETMANAGER_SUCCESS;
133 }
134 
NetworkAddRoute(int32_t netId,std::string interfaceName,std::string destination,std::string nextHop)135 int32_t NetManagerNative::NetworkAddRoute(int32_t netId, std::string interfaceName, std::string destination,
136                                           std::string nextHop)
137 {
138     return connManager_->AddRoute(netId, interfaceName, destination, nextHop);
139 }
140 
NetworkRemoveRoute(int32_t netId,std::string interfaceName,std::string destination,std::string nextHop)141 int32_t NetManagerNative::NetworkRemoveRoute(int32_t netId, std::string interfaceName, std::string destination,
142                                              std::string nextHop)
143 {
144     return connManager_->RemoveRoute(netId, interfaceName, destination, nextHop);
145 }
146 
NetworkGetDefault()147 int32_t NetManagerNative::NetworkGetDefault()
148 {
149     return connManager_->GetDefaultNetwork();
150 }
151 
NetworkSetDefault(int32_t netId)152 int32_t NetManagerNative::NetworkSetDefault(int32_t netId)
153 {
154     dnsManager_->SetDefaultNetwork(netId);
155     return connManager_->SetDefaultNetwork(netId);
156 }
157 
NetworkClearDefault()158 int32_t NetManagerNative::NetworkClearDefault()
159 {
160     return connManager_->ClearDefaultNetwork();
161 }
162 
NetworkSetPermissionForNetwork(int32_t netId,NetworkPermission permission)163 int32_t NetManagerNative::NetworkSetPermissionForNetwork(int32_t netId, NetworkPermission permission)
164 {
165     return connManager_->SetPermissionForNetwork(netId, permission);
166 }
167 
InterfaceGetList()168 std::vector<std::string> NetManagerNative::InterfaceGetList()
169 {
170     return InterfaceManager::GetInterfaceNames();
171 }
172 
GetInterfaceConfig(std::string interfaceName)173 nmd::InterfaceConfigurationParcel NetManagerNative::GetInterfaceConfig(std::string interfaceName)
174 {
175     return InterfaceManager::GetIfaceConfig(interfaceName.c_str());
176 }
177 
SetInterfaceConfig(nmd::InterfaceConfigurationParcel parcel)178 void NetManagerNative::SetInterfaceConfig(nmd::InterfaceConfigurationParcel parcel)
179 {
180     InterfaceManager::SetIfaceConfig(parcel);
181 }
182 
ClearInterfaceAddrs(const std::string ifName)183 void NetManagerNative::ClearInterfaceAddrs(const std::string ifName) {}
184 
GetInterfaceMtu(std::string ifName)185 int32_t NetManagerNative::GetInterfaceMtu(std::string ifName)
186 {
187     return InterfaceManager::GetMtu(ifName.c_str());
188 }
189 
SetInterfaceMtu(std::string ifName,int32_t mtuValue)190 int32_t NetManagerNative::SetInterfaceMtu(std::string ifName, int32_t mtuValue)
191 {
192     if (strncmp(ifName.c_str(), TUN_CARD_NAME, strlen(TUN_CARD_NAME)) != 0) {
193         return InterfaceManager::SetMtu(ifName.c_str(), std::to_string(mtuValue).c_str());
194     }
195     return VpnManager::GetInstance().SetVpnMtu(ifName, mtuValue);
196 }
197 
InterfaceSetIpAddress(const std::string & ifaceName,const std::string & ipAddress)198 int32_t NetManagerNative::InterfaceSetIpAddress(const std::string &ifaceName, const std::string &ipAddress)
199 {
200     return InterfaceManager::SetIpAddress(ifaceName.c_str(), ipAddress.c_str());
201 }
202 
InterfaceSetIffUp(std::string ifaceName)203 int32_t NetManagerNative::InterfaceSetIffUp(std::string ifaceName)
204 {
205     return InterfaceManager::SetIffUp(ifaceName.c_str());
206 }
207 
GetFwmarkForNetwork(int32_t netId)208 nmd::MarkMaskParcel NetManagerNative::GetFwmarkForNetwork(int32_t netId)
209 {
210     nmd::MarkMaskParcel mark;
211     mark.mark = connManager_->GetFwmarkForNetwork(netId);
212     mark.mask = 0XFFFF;
213     return mark;
214 }
215 
NetworkAddRouteParcel(int32_t netId,RouteInfoParcel parcel)216 int32_t NetManagerNative::NetworkAddRouteParcel(int32_t netId, RouteInfoParcel parcel)
217 {
218     return connManager_->AddRoute(netId, parcel.ifName, parcel.destination, parcel.nextHop);
219 }
220 
NetworkRemoveRouteParcel(int32_t netId,RouteInfoParcel parcel)221 int32_t NetManagerNative::NetworkRemoveRouteParcel(int32_t netId, RouteInfoParcel parcel)
222 {
223     return connManager_->RemoveRoute(netId, parcel.ifName, parcel.destination, parcel.nextHop);
224 }
225 
SetProcSysNet(int32_t family,int32_t which,const std::string ifname,const std::string parameter,const std::string value)226 int32_t NetManagerNative::SetProcSysNet(int32_t family, int32_t which, const std::string ifname,
227                                         const std::string parameter, const std::string value)
228 {
229     return 0;
230 }
231 
GetProcSysNet(int32_t family,int32_t which,const std::string ifname,const std::string parameter,std::string * value)232 int32_t NetManagerNative::GetProcSysNet(int32_t family, int32_t which, const std::string ifname,
233                                         const std::string parameter, std::string *value)
234 {
235     return 0;
236 }
237 
GetCellularRxBytes()238 int64_t NetManagerNative::GetCellularRxBytes()
239 {
240     return 0;
241 }
242 
GetCellularTxBytes()243 int64_t NetManagerNative::GetCellularTxBytes()
244 {
245     return 0;
246 }
247 
GetAllRxBytes()248 int64_t NetManagerNative::GetAllRxBytes()
249 {
250     return nmd::TrafficManager::GetAllRxTraffic();
251 }
252 
GetAllTxBytes()253 int64_t NetManagerNative::GetAllTxBytes()
254 {
255     return nmd::TrafficManager::GetAllTxTraffic();
256 }
257 
GetUidTxBytes(int32_t uid)258 int64_t NetManagerNative::GetUidTxBytes(int32_t uid)
259 {
260     return 0;
261 }
262 
GetUidRxBytes(int32_t uid)263 int64_t NetManagerNative::GetUidRxBytes(int32_t uid)
264 {
265     return 0;
266 }
267 
GetIfaceRxBytes(std::string interfaceName)268 int64_t NetManagerNative::GetIfaceRxBytes(std::string interfaceName)
269 {
270     nmd::TrafficStatsParcel interfaceTraffic = nmd::TrafficManager::GetInterfaceTraffic(interfaceName);
271     return interfaceTraffic.rxBytes;
272 }
273 
GetIfaceTxBytes(std::string interfaceName)274 int64_t NetManagerNative::GetIfaceTxBytes(std::string interfaceName)
275 {
276     nmd::TrafficStatsParcel interfaceTraffic = nmd::TrafficManager::GetInterfaceTraffic(interfaceName);
277     return interfaceTraffic.txBytes;
278 }
279 
IpEnableForwarding(const std::string & requester)280 int32_t NetManagerNative::IpEnableForwarding(const std::string &requester)
281 {
282     return sharingManager_->IpEnableForwarding(requester);
283 }
284 
IpDisableForwarding(const std::string & requester)285 int32_t NetManagerNative::IpDisableForwarding(const std::string &requester)
286 {
287     return sharingManager_->IpDisableForwarding(requester);
288 }
289 
EnableNat(const std::string & downstreamIface,const std::string & upstreamIface)290 int32_t NetManagerNative::EnableNat(const std::string &downstreamIface, const std::string &upstreamIface)
291 {
292     return sharingManager_->EnableNat(downstreamIface, upstreamIface);
293 }
294 
DisableNat(const std::string & downstreamIface,const std::string & upstreamIface)295 int32_t NetManagerNative::DisableNat(const std::string &downstreamIface, const std::string &upstreamIface)
296 {
297     return sharingManager_->DisableNat(downstreamIface, upstreamIface);
298 }
299 
IpfwdAddInterfaceForward(const std::string & fromIface,const std::string & toIface)300 int32_t NetManagerNative::IpfwdAddInterfaceForward(const std::string &fromIface, const std::string &toIface)
301 {
302     return sharingManager_->IpfwdAddInterfaceForward(fromIface, toIface);
303 }
304 
IpfwdRemoveInterfaceForward(const std::string & fromIface,const std::string & toIface)305 int32_t NetManagerNative::IpfwdRemoveInterfaceForward(const std::string &fromIface, const std::string &toIface)
306 {
307     return sharingManager_->IpfwdRemoveInterfaceForward(fromIface, toIface);
308 }
309 
DnsSetResolverConfig(uint16_t netId,uint16_t baseTimeoutMsec,uint8_t retryCount,const std::vector<std::string> & servers,const std::vector<std::string> & domains)310 int32_t NetManagerNative::DnsSetResolverConfig(uint16_t netId, uint16_t baseTimeoutMsec, uint8_t retryCount,
311                                                const std::vector<std::string> &servers,
312                                                const std::vector<std::string> &domains)
313 {
314     return dnsManager_->SetResolverConfig(netId, baseTimeoutMsec, retryCount, servers, domains);
315 }
316 
DnsGetResolverConfig(uint16_t netId,std::vector<std::string> & servers,std::vector<std::string> & domains,uint16_t & baseTimeoutMsec,uint8_t & retryCount)317 int32_t NetManagerNative::DnsGetResolverConfig(uint16_t netId, std::vector<std::string> &servers,
318                                                std::vector<std::string> &domains, uint16_t &baseTimeoutMsec,
319                                                uint8_t &retryCount)
320 {
321     return dnsManager_->GetResolverConfig(netId, servers, domains, baseTimeoutMsec, retryCount);
322 }
323 
DnsCreateNetworkCache(uint16_t netId)324 int32_t NetManagerNative::DnsCreateNetworkCache(uint16_t netId)
325 {
326     return dnsManager_->CreateNetworkCache(netId);
327 }
328 
DnsDestroyNetworkCache(uint16_t netId)329 int32_t NetManagerNative::DnsDestroyNetworkCache(uint16_t netId)
330 {
331     return dnsManager_->DestroyNetworkCache(netId);
332 }
333 
BandwidthEnableDataSaver(bool enable)334 int32_t NetManagerNative::BandwidthEnableDataSaver(bool enable)
335 {
336     return bandwidthManager_->EnableDataSaver(enable);
337 }
338 
BandwidthSetIfaceQuota(const std::string & ifName,int64_t bytes)339 int32_t NetManagerNative::BandwidthSetIfaceQuota(const std::string &ifName, int64_t bytes)
340 {
341     return bandwidthManager_->SetIfaceQuota(ifName, bytes);
342 }
343 
BandwidthRemoveIfaceQuota(const std::string & ifName)344 int32_t NetManagerNative::BandwidthRemoveIfaceQuota(const std::string &ifName)
345 {
346     return bandwidthManager_->RemoveIfaceQuota(ifName);
347 }
348 
BandwidthAddDeniedList(uint32_t uid)349 int32_t NetManagerNative::BandwidthAddDeniedList(uint32_t uid)
350 {
351     return bandwidthManager_->AddDeniedList(uid);
352 }
353 
BandwidthRemoveDeniedList(uint32_t uid)354 int32_t NetManagerNative::BandwidthRemoveDeniedList(uint32_t uid)
355 {
356     return bandwidthManager_->RemoveDeniedList(uid);
357 }
358 
BandwidthAddAllowedList(uint32_t uid)359 int32_t NetManagerNative::BandwidthAddAllowedList(uint32_t uid)
360 {
361     return bandwidthManager_->AddAllowedList(uid);
362 }
363 
BandwidthRemoveAllowedList(uint32_t uid)364 int32_t NetManagerNative::BandwidthRemoveAllowedList(uint32_t uid)
365 {
366     return bandwidthManager_->RemoveAllowedList(uid);
367 }
368 
FirewallSetUidsAllowedListChain(uint32_t chain,const std::vector<uint32_t> & uids)369 int32_t NetManagerNative::FirewallSetUidsAllowedListChain(uint32_t chain, const std::vector<uint32_t> &uids)
370 {
371     auto chainType = static_cast<NetManagerStandard::ChainType>(chain);
372     return firewallManager_->SetUidsAllowedListChain(chainType, uids);
373 }
374 
FirewallSetUidsDeniedListChain(uint32_t chain,const std::vector<uint32_t> & uids)375 int32_t NetManagerNative::FirewallSetUidsDeniedListChain(uint32_t chain, const std::vector<uint32_t> &uids)
376 {
377     auto chainType = static_cast<NetManagerStandard::ChainType>(chain);
378     return firewallManager_->SetUidsDeniedListChain(chainType, uids);
379 }
380 
FirewallEnableChain(uint32_t chain,bool enable)381 int32_t NetManagerNative::FirewallEnableChain(uint32_t chain, bool enable)
382 {
383     auto chainType = static_cast<NetManagerStandard::ChainType>(chain);
384     return firewallManager_->EnableChain(chainType, enable);
385 }
386 
FirewallSetUidRule(uint32_t chain,const std::vector<uint32_t> & uids,uint32_t firewallRule)387 int32_t NetManagerNative::FirewallSetUidRule(uint32_t chain, const std::vector<uint32_t> &uids, uint32_t firewallRule)
388 {
389     auto chainType = static_cast<NetManagerStandard::ChainType>(chain);
390     auto rule = static_cast<NetManagerStandard::FirewallRule>(firewallRule);
391     for (auto &uid : uids) {
392         auto ret = firewallManager_->SetUidRule(chainType, uid, rule);
393         if (ret != NetManagerStandard::NETMANAGER_SUCCESS) {
394             return ret;
395         }
396     }
397     return NetManagerStandard::NETMANAGER_SUCCESS;
398 }
399 
ShareDnsSet(uint16_t netId)400 void NetManagerNative::ShareDnsSet(uint16_t netId)
401 {
402     dnsManager_->ShareDnsSet(netId);
403 }
404 
StartDnsProxyListen()405 void NetManagerNative::StartDnsProxyListen()
406 {
407     dnsManager_->StartDnsProxyListen();
408 }
409 
StopDnsProxyListen()410 void NetManagerNative::StopDnsProxyListen()
411 {
412     dnsManager_->StopDnsProxyListen();
413 }
414 
DnsGetAddrInfo(const std::string & hostName,const std::string & serverName,const AddrInfo & hints,uint16_t netId,std::vector<AddrInfo> & res)415 int32_t NetManagerNative::DnsGetAddrInfo(const std::string &hostName, const std::string &serverName,
416                                          const AddrInfo &hints, uint16_t netId, std::vector<AddrInfo> &res)
417 {
418     return dnsManager_->GetAddrInfo(hostName, serverName, hints, netId, res);
419 }
420 
GetDumpInfo(std::string & infos)421 void NetManagerNative::GetDumpInfo(std::string &infos)
422 {
423     connManager_->GetDumpInfos(infos);
424     dnsManager_->GetDumpInfo(infos);
425 }
426 } // namespace nmd
427 } // namespace OHOS
428