• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2024 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 #ifndef INCLUDE_NET_MANAGER_NATIVE_H
17 #define INCLUDE_NET_MANAGER_NATIVE_H
18 
19 #include <memory>
20 #include <string>
21 #include <vector>
22 
23 #include "bandwidth_manager.h"
24 #include "conn_manager.h"
25 #include "dns_manager.h"
26 #include "firewall_manager.h"
27 #include "interface_manager.h"
28 #include "interface_type.h"
29 #include "route_manager.h"
30 #include "vnic_manager.h"
31 #include "route_type.h"
32 #include "sharing_manager.h"
33 #include "uid_range.h"
34 #include "net_all_capabilities.h"
35 #ifdef FEATURE_WEARABLE_DISTRIBUTED_NET_ENABLE
36 #include "wearable_distributed_net_manager.h"
37 #endif
38 
39 namespace OHOS {
40 namespace nmd {
41 using namespace OHOS::NetManagerStandard;
42 using namespace OHOS::NetsysNative;
43 class NetManagerNative {
44 public:
45     NetManagerNative();
46     ~NetManagerNative() = default;
47 
48     static void GetOriginInterfaceIndex();
49     static std::vector<uint32_t> GetCurrentInterfaceIndex();
50     static void UpdateInterfaceIndex(uint32_t infIndex);
51 
52     void Init();
53 
54     int32_t NetworkReinitRoute();
55     int32_t SetInternetPermission(uint32_t uid, uint8_t allow, uint8_t isBroker);
56     int32_t NetworkCreatePhysical(int32_t netId, int32_t permission);
57     int32_t NetworkCreateVirtual(int32_t netId, bool hasDns);
58     int32_t NetworkDestroy(int32_t netId, bool isVpnNet = false);
59     int32_t CreateVnic(uint16_t mtu, const std::string &tunAddr, int32_t prefix, const std::set<int32_t> &uids);
60     int32_t DestroyVnic();
61     int32_t EnableDistributedClientNet(const std::string &virnicAddr, const std::string &iif);
62     int32_t EnableDistributedServerNet(const std::string &iif, const std::string &devIface, const std::string &dstAddr);
63     int32_t DisableDistributedNet(bool isServer);
64     int32_t NetworkAddUids(int32_t netId, const std::vector<UidRange> &uidRanges);
65     int32_t NetworkDelUids(int32_t netId, const std::vector<UidRange> &uidRanges);
66     int32_t NetworkAddInterface(int32_t netId, std::string iface, NetBearType netBearerType);
67     int32_t NetworkRemoveInterface(int32_t netId, std::string iface);
68 
69     MarkMaskParcel GetFwmarkForNetwork(int32_t netId);
70     int32_t NetworkAddRoute(int32_t netId, std::string ifName, std::string destination, std::string nextHop,
71         bool isExcludedRoute = false);
72     int32_t NetworkRemoveRoute(int32_t netId, std::string ifName, std::string destination, std::string nextHop);
73     int32_t NetworkGetDefault();
74     int32_t NetworkSetDefault(int32_t netId);
75     int32_t NetworkClearDefault();
76     int32_t NetworkSetPermissionForNetwork(int32_t netId, NetworkPermission permission);
77     std::vector<std::string> InterfaceGetList();
78 
79     int32_t SetProcSysNet(int32_t family, int32_t which, const std::string ifname, const std::string parameter,
80                           const std::string value);
81     int32_t GetProcSysNet(int32_t family, int32_t which, const std::string ifname, const std::string parameter,
82                           std::string *value);
83 
84     nmd::InterfaceConfigurationParcel GetInterfaceConfig(std::string ifName);
85     void SetInterfaceConfig(InterfaceConfigurationParcel cfg);
86     void ClearInterfaceAddrs(const std::string ifName);
87     int32_t GetInterfaceMtu(std::string ifName);
88     int32_t SetInterfaceMtu(std::string ifName, int32_t mtuValue);
89     int32_t SetTcpBufferSizes(const std::string &tcpBufferSizes);
90     int32_t AddInterfaceAddress(std::string ifName, std::string addrString, int32_t prefixLength);
91     int32_t DelInterfaceAddress(std::string ifName, std::string addrString, int32_t prefixLength);
92     int32_t DelInterfaceAddress(std::string ifName, std::string addrString, int32_t prefixLength,
93                                 const std::string &netCapabilities);
94     int32_t InterfaceSetIpAddress(const std::string &ifaceName, const std::string &ipAddress);
95     int32_t InterfaceSetIffUp(std::string ifaceName);
96     int32_t NetworkAddRouteParcel(int32_t netId, RouteInfoParcel routeInfo);
97     int32_t NetworkRemoveRouteParcel(int32_t netId, RouteInfoParcel routeInfo);
98 
99     int64_t GetCellularRxBytes();
100     int64_t GetCellularTxBytes();
101     int64_t GetAllRxBytes();
102     int64_t GetAllTxBytes();
103     int64_t GetUidTxBytes(int32_t uid);
104     int64_t GetUidRxBytes(int32_t uid);
105     int64_t GetIfaceRxBytes(std::string interfaceName);
106     int64_t GetIfaceTxBytes(std::string interfaceName);
107     int32_t IpEnableForwarding(const std::string &requester);
108     int32_t IpDisableForwarding(const std::string &requester);
109     int32_t EnableNat(const std::string &downstreamIface, const std::string &upstreamIface);
110     int32_t DisableNat(const std::string &downstreamIface, const std::string &upsteramIface);
111     int32_t IpfwdAddInterfaceForward(const std::string &fromIface, const std::string &toIface);
112     int32_t IpfwdRemoveInterfaceForward(const std::string &fromIface, const std::string &toIface);
113 
114     int32_t DnsSetResolverConfig(uint16_t netId, uint16_t baseTimeoutMsec, uint8_t retryCount,
115                                  const std::vector<std::string> &servers, const std::vector<std::string> &domains);
116     int32_t DnsGetResolverConfig(uint16_t netId, std::vector<std::string> &servers, std::vector<std::string> &domains,
117                                  uint16_t &baseTimeoutMsec, uint8_t &retryCount);
118     int32_t DnsCreateNetworkCache(uint16_t netid, bool isVpnNet = false);
119     int32_t DnsDestroyNetworkCache(uint16_t netId, bool isVpnNet = false);
120     int32_t BandwidthEnableDataSaver(bool enable);
121     int32_t BandwidthSetIfaceQuota(const std::string &ifName, int64_t bytes);
122     int32_t BandwidthRemoveIfaceQuota(const std::string &ifName);
123     int32_t BandwidthAddDeniedList(uint32_t uid);
124     int32_t BandwidthRemoveDeniedList(uint32_t uid);
125     int32_t BandwidthAddAllowedList(uint32_t uid);
126     int32_t BandwidthRemoveAllowedList(uint32_t uid);
127 
128     int32_t FirewallSetUidsAllowedListChain(uint32_t chain, const std::vector<uint32_t> &uids);
129     int32_t FirewallSetUidsDeniedListChain(uint32_t chain, const std::vector<uint32_t> &uids);
130     int32_t FirewallEnableChain(uint32_t chain, bool enable);
131     int32_t FirewallSetUidRule(uint32_t chain, const std::vector<uint32_t> &uids, uint32_t firewallRule);
132     void ShareDnsSet(uint16_t netId);
133     void StartDnsProxyListen();
134     void StopDnsProxyListen();
135     void GetDumpInfo(std::string &infos);
136     int32_t DnsGetAddrInfo(const std::string &hostName, const std::string &serverName, const AddrInfo &hints,
137                            uint16_t netId, std::vector<AddrInfo> &res);
138     int32_t AddStaticArp(const std::string &ipAddr, const std::string &macAddr, const std::string &ifName);
139     int32_t DelStaticArp(const std::string &ipAddr, const std::string &macAddr, const std::string &ifName);
140     int32_t AddStaticIpv6Addr(const std::string &ipv6Addr, const std::string &macAddr, const std::string &ifName);
141     int32_t DelStaticIpv6Addr(const std::string &ipv6Addr, const std::string &macAddr, const std::string &ifName);
142     int32_t RegisterDnsResultCallback(const sptr<INetDnsResultCallback> &callback, uint32_t timeStep);
143     int32_t UnregisterDnsResultCallback(const sptr<INetDnsResultCallback> &callback);
144     int32_t RegisterDnsHealthCallback(const sptr<INetDnsHealthCallback> &callback);
145     int32_t UnregisterDnsHealthCallback(const sptr<INetDnsHealthCallback> &callback);
146     int32_t SetIpv6PrivacyExtensions(const std::string &interfaceName, const uint32_t on);
147     int32_t SetEnableIpv6(const std::string &interfaceName, const uint32_t on);
148 #ifdef FEATURE_NET_FIREWALL_ENABLE
149     int32_t SetFirewallDefaultAction(FirewallRuleAction inDefault, FirewallRuleAction outDefault);
150     int32_t SetFirewallCurrentUserId(int32_t userId);
151     int32_t SetFirewallRules(NetFirewallRuleType type, const std::vector<sptr<NetFirewallBaseRule>> &ruleList,
152                              bool isFinish);
153     int32_t ClearFirewallRules(NetFirewallRuleType type);
154     int32_t RegisterNetFirewallCallback(const sptr<NetsysNative::INetFirewallCallback> &callback);
155     int32_t UnRegisterNetFirewallCallback(const sptr<NetsysNative::INetFirewallCallback> &callback);
156 #endif
157 #ifdef FEATURE_WEARABLE_DISTRIBUTED_NET_ENABLE
158     int32_t EnableWearableDistributedNetForward(const int32_t tcpPortId, const int32_t udpPortId);
159     int32_t DisableWearableDistributedNetForward();
160 #endif
161     int32_t SetNetworkAccessPolicy(uint32_t uid, NetworkAccessPolicy policy, bool reconfirmFlag);
162     int32_t DeleteNetworkAccessPolicy(uint32_t uid);
163     int32_t NotifyNetBearerTypeChange(std::set<NetBearType> bearerTypes);
164     int32_t ClearFirewallAllRules();
165     int32_t CloseSocketsUid(const std::string &ipAddr, uint32_t uid);
166     int32_t DnsSetUserDefinedServerFlag(uint16_t netId, bool flag);
167     int32_t FlushDnsCache(uint16_t netId);
168 #ifdef SUPPORT_SYSVPN
169     int32_t UpdateVpnRules(uint16_t netId, const std::vector<std::string> &extMessages, bool add);
170 #endif // SUPPORT_SYSVPN
171     int32_t SetDnsCache(uint16_t netId, const std::string &hostName, const AddrInfo &addrInfo);
172 #ifdef FEATURE_ENTERPRISE_ROUTE_CUSTOM
173     int32_t UpdateEnterpriseRoute(const std::string &interfaceName, uint32_t uid, bool add);
174 #endif
175 private:
176     std::shared_ptr<BandwidthManager> bandwidthManager_ = nullptr;
177     std::shared_ptr<ConnManager> connManager_ = nullptr;
178     std::shared_ptr<FirewallManager> firewallManager_ = nullptr;
179 #ifdef FEATURE_WEARABLE_DISTRIBUTED_NET_ENABLE
180     std::shared_ptr<WearableDistributedNet> wearableDistributedNet_ = nullptr;
181 #endif
182     std::shared_ptr<RouteManager> routeManager_ = nullptr;
183     std::shared_ptr<InterfaceManager> interfaceManager_ = nullptr;
184     std::shared_ptr<SharingManager> sharingManager_ = nullptr;
185     std::shared_ptr<DnsManager> dnsManager_ = nullptr;
186     static inline std::vector<uint32_t> interfaceIdex_;
187 };
188 } // namespace nmd
189 } // namespace OHOS
190 #endif // !INCLUDE_NET_MANAGER_NATIVE_H
191