• 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 #ifndef NETSYS_NATIVE_SERVICE_H
17 #define NETSYS_NATIVE_SERVICE_H
18 
19 #include <mutex>
20 
21 #include "system_ability.h"
22 #include "system_ability_status_change_stub.h"
23 
24 #include "bpf_stats.h"
25 #ifdef FEATURE_NET_FIREWALL_ENABLE
26 #include "bpf_netfirewall.h"
27 #endif
28 #include "dhcp_controller.h"
29 #include "fwmark_network.h"
30 #include "i_netsys_service.h"
31 #include "iremote_stub.h"
32 #include "net_diag_wrapper.h"
33 #include "net_manager_native.h"
34 #include "netlink_manager.h"
35 #include "netsys_native_service_stub.h"
36 #include "sharing_manager.h"
37 #include "netsys_access_policy.h"
38 #include "clat_manager.h"
39 #include "vnic_manager.h"
40 
41 namespace OHOS {
42 namespace NetsysNative {
43 class NetsysNativeService : public SystemAbility, public NetsysNativeServiceStub, protected NoCopyable {
44     DECLARE_SYSTEM_ABILITY(NetsysNativeService);
45 
46 public:
SystemAbility(saID,runOnCreate)47     explicit NetsysNativeService(int32_t saID, bool runOnCreate = true) : SystemAbility(saID, runOnCreate){};
48     ~NetsysNativeService() override = default;
49 
50     void OnStart() override;
51     void OnStop() override;
52     int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override;
53 
54     int32_t SetResolverConfig(uint16_t netId, uint16_t baseTimeoutMsec, uint8_t retryCount,
55                               const std::vector<std::string> &servers,
56                               const std::vector<std::string> &domains) override;
57     int32_t GetResolverConfig(uint16_t netId, std::vector<std::string> &servers, std::vector<std::string> &domains,
58                               uint16_t &baseTimeoutMsec, uint8_t &retryCount) override;
59     int32_t CreateNetworkCache(uint16_t netId, bool isVpnNet = false) override;
60     int32_t DestroyNetworkCache(uint16_t netId, bool isVpnNet = false) override;
61     int32_t GetAddrInfo(const std::string &hostName, const std::string &serverName, const AddrInfo &hints,
62                         uint16_t netId, std::vector<AddrInfo> &res) override;
63     int32_t SetInterfaceMtu(const std::string &interfaceName, int32_t mtu) override;
64     int32_t GetInterfaceMtu(const std::string &interfaceName) override;
65 
66     int32_t SetTcpBufferSizes(const std::string &tcpBufferSizes) override;
67 
68     int32_t RegisterNotifyCallback(sptr<INotifyCallback> &callback) override;
69     int32_t UnRegisterNotifyCallback(sptr<INotifyCallback> &callback) override;
70 
71     int32_t NetworkAddRoute(int32_t netId, const std::string &interfaceName, const std::string &destination,
72                             const std::string &nextHop) override;
73     int32_t NetworkRemoveRoute(int32_t netId, const std::string &interfaceName, const std::string &destination,
74                                const std::string &nextHop) override;
75     int32_t NetworkAddRouteParcel(int32_t netId, const RouteInfoParcel &routeInfo) override;
76     int32_t NetworkRemoveRouteParcel(int32_t netId, const RouteInfoParcel &routeInfo) override;
77     int32_t NetworkSetDefault(int32_t netId) override;
78     int32_t NetworkGetDefault() override;
79     int32_t NetworkClearDefault() override;
80     int32_t GetProcSysNet(int32_t family, int32_t which, const std::string &ifname, const std::string &parameter,
81                           std::string &value) override;
82     int32_t SetProcSysNet(int32_t family, int32_t which, const std::string &ifname, const std::string &parameter,
83                           std::string &value) override;
84     int32_t SetInternetPermission(uint32_t uid, uint8_t allow, uint8_t isBroker) override;
85     int32_t NetworkCreatePhysical(int32_t netId, int32_t permission) override;
86     int32_t NetworkCreateVirtual(int32_t netId, bool hasDns) override;
87     int32_t NetworkAddUids(int32_t netId, const std::vector<UidRange> &uidRanges) override;
88     int32_t NetworkDelUids(int32_t netId, const std::vector<UidRange> &uidRanges) override;
89     int32_t AddInterfaceAddress(const std::string &interfaceName, const std::string &addrString,
90                                 int32_t prefixLength) override;
91     int32_t DelInterfaceAddress(const std::string &interfaceName, const std::string &addrString,
92                                 int32_t prefixLength) override;
93     int32_t DelInterfaceAddress(const std::string &interfaceName, const std::string &addrString,
94                                 int32_t prefixLength, const std::string &netCapabilities) override;
95     int32_t InterfaceSetIpAddress(const std::string &ifaceName, const std::string &ipAddress) override;
96     int32_t InterfaceSetIffUp(const std::string &ifaceName) override;
97     int32_t NetworkAddInterface(int32_t netId, const std::string &iface, NetBearType netBearerType) override;
98     int32_t NetworkRemoveInterface(int32_t netId, const std::string &iface) override;
99     int32_t NetworkDestroy(int32_t netId, bool isVpnNet = false) override;
100     int32_t CreateVnic(uint16_t mtu, const std::string &tunAddr, int32_t prefix,
101                        const std::set<int32_t> &uids) override;
102     int32_t DestroyVnic() override;
103     int32_t EnableDistributedClientNet(const std::string &virnicAddr, const std::string &iif) override;
104     int32_t EnableDistributedServerNet(const std::string &iif, const std::string &devIface,
105                                        const std::string &dstAddr) override;
106     int32_t DisableDistributedNet(bool isServer) override;
107     int32_t GetFwmarkForNetwork(int32_t netId, MarkMaskParcel &markMaskParcel) override;
108     int32_t SetInterfaceConfig(const InterfaceConfigurationParcel &cfg) override;
109     int32_t GetInterfaceConfig(InterfaceConfigurationParcel &cfg) override;
110     int32_t InterfaceGetList(std::vector<std::string> &ifaces) override;
111     int32_t StartDhcpClient(const std::string &iface, bool bIpv6) override;
112     int32_t StopDhcpClient(const std::string &iface, bool bIpv6) override;
113     int32_t StartDhcpService(const std::string &iface, const std::string &ipv4addr) override;
114     int32_t StopDhcpService(const std::string &iface) override;
115     int32_t IpEnableForwarding(const std::string &requester) override;
116     int32_t IpDisableForwarding(const std::string &requester) override;
117     int32_t EnableNat(const std::string &downstreamIface, const std::string &upstreamIface) override;
118     int32_t DisableNat(const std::string &downstreamIface, const std::string &upstreamIface) override;
119     int32_t IpfwdAddInterfaceForward(const std::string &fromIface, const std::string &toiIface) override;
120     int32_t IpfwdRemoveInterfaceForward(const std::string &fromIface, const std::string &toiIface) override;
121     int32_t FirewallSetUidsDeniedListChain(uint32_t chain, const std::vector<uint32_t> &uids) override;
122     int32_t FirewallEnableChain(uint32_t chain, bool enable) override;
123     int32_t FirewallSetUidRule(uint32_t chain, const std::vector<uint32_t> &uids, uint32_t firewallRule) override;
124     int32_t BandwidthEnableDataSaver(bool enable) override;
125     int32_t BandwidthSetIfaceQuota(const std::string &ifName, int64_t bytes) override;
126     int32_t BandwidthRemoveIfaceQuota(const std::string &ifName) override;
127     int32_t FirewallSetUidsAllowedListChain(uint32_t chain, const std::vector<uint32_t> &uids) override;
128     int32_t BandwidthAddAllowedList(uint32_t uid) override;
129     int32_t BandwidthRemoveAllowedList(uint32_t uid) override;
130     int32_t BandwidthAddDeniedList(uint32_t uid) override;
131     int32_t BandwidthRemoveDeniedList(uint32_t uid) override;
132     int32_t ShareDnsSet(uint16_t netId) override;
133     int32_t StartDnsProxyListen() override;
134     int32_t StopDnsProxyListen() override;
135     int32_t GetNetworkSharingTraffic(const std::string &downIface, const std::string &upIface,
136                                      NetworkSharingTraffic &traffic) override;
137     int32_t GetNetworkCellularSharingTraffic(NetworkSharingTraffic &traffic, std::string &ifaceName) override;
138     int32_t GetTotalStats(uint64_t &stats, uint32_t type) override;
139     int32_t GetUidStats(uint64_t &stats, uint32_t type, uint32_t uid) override;
140     int32_t GetIfaceStats(uint64_t &stats, uint32_t type, const std::string &interfaceName) override;
141     int32_t GetAllSimStatsInfo(std::vector<OHOS::NetManagerStandard::NetStatsInfo> &stats) override;
142     int32_t DeleteSimStatsInfo(uint32_t uid) override;
143     int32_t GetAllStatsInfo(std::vector<OHOS::NetManagerStandard::NetStatsInfo> &stats) override;
144     int32_t DeleteStatsInfo(uint32_t uid) override;
145     int32_t SetNetStateTrafficMap(uint8_t flag, uint64_t availableTraffic) override;
146     int32_t GetNetStateTrafficMap(uint8_t flag, uint64_t &availableTraffic) override;
147     int32_t ClearIncreaseTrafficMap() override;
148     int32_t UpdateIfIndexMap(int8_t key, uint64_t index) override;
149     int32_t SetIptablesCommandForRes(const std::string &cmd, std::string &respond, IptablesType ipType) override;
150     int32_t SetIpCommandForRes(const std::string &cmd, std::string &respond) override;
151     int32_t NetDiagPingHost(const NetDiagPingOption &pingOption, const sptr<INetDiagCallback> &callback) override;
152     int32_t NetDiagGetRouteTable(std::list<NetDiagRouteTable> &routeTables) override;
153     int32_t NetDiagGetSocketsInfo(NetDiagProtocolType socketType, NetDiagSocketsInfo &socketsInfo) override;
154     int32_t NetDiagGetInterfaceConfig(std::list<NetDiagIfaceConfig> &configs, const std::string &ifaceName) override;
155     int32_t NetDiagUpdateInterfaceConfig(const NetDiagIfaceConfig &config, const std::string &ifaceName,
156                                          bool add) override;
157     int32_t NetDiagSetInterfaceActiveState(const std::string &ifaceName, bool up) override;
158     int32_t AddStaticArp(const std::string &ipAddr, const std::string &macAddr,
159                          const std::string &ifName) override;
160     int32_t DelStaticArp(const std::string &ipAddr, const std::string &macAddr,
161                          const std::string &ifName) override;
162     int32_t RegisterDnsResultCallback(const sptr<INetDnsResultCallback> &callback, uint32_t timeStep) override;
163     int32_t UnregisterDnsResultCallback(const sptr<INetDnsResultCallback> &callback) override;
164     int32_t RegisterDnsHealthCallback(const sptr<INetDnsHealthCallback> &callback) override;
165     int32_t UnregisterDnsHealthCallback(const sptr<INetDnsHealthCallback> &callback) override;
166     int32_t GetCookieStats(uint64_t &stats, uint32_t type, uint64_t cookie) override;
167     int32_t GetNetworkSharingType(std::set<uint32_t>& sharingTypeIsOn) override;
168     int32_t UpdateNetworkSharingType(uint32_t type, bool isOpen) override;
169     int32_t RegisterNetsysTrafficCallback(const sptr<INetsysTrafficCallback> &callback) override;
170     int32_t UnRegisterNetsysTrafficCallback(const sptr<INetsysTrafficCallback> &callback) override;
171 
172 #ifdef FEATURE_NET_FIREWALL_ENABLE
173     int32_t SetFirewallRules(NetFirewallRuleType type, const std::vector<sptr<NetFirewallBaseRule>> &ruleList,
174                              bool isFinish) override;
175     int32_t SetFirewallDefaultAction(int32_t userId, FirewallRuleAction inDefault,
176                                      FirewallRuleAction outDefault) override;
177     int32_t SetFirewallCurrentUserId(int32_t userId) override;
178     int32_t ClearFirewallRules(NetFirewallRuleType type) override;
179     int32_t RegisterNetFirewallCallback(const sptr<INetFirewallCallback> &callback) override;
180     int32_t UnRegisterNetFirewallCallback(const sptr<INetFirewallCallback> &callback) override;
181 #endif
182 #ifdef FEATURE_WEARABLE_DISTRIBUTED_NET_ENABLE
183     int32_t EnableWearableDistributedNetForward(const int32_t tcpPortId, const int32_t udpPortId) override;
184     int32_t DisableWearableDistributedNetForward() override;
185 #endif
186     int32_t SetIpv6PrivacyExtensions(const std::string &interfaceName, const uint32_t on) override;
187     int32_t SetEnableIpv6(const std::string &interfaceName, const uint32_t on) override;
188 
189     int32_t SetNetworkAccessPolicy(uint32_t uid, NetworkAccessPolicy policy, bool reconfirmFlag) override;
190     int32_t DeleteNetworkAccessPolicy(uint32_t uid) override;
191     int32_t NotifyNetBearerTypeChange(std::set<NetBearType> bearerTypes) override;
192     int32_t StartClat(const std::string &interfaceName, int32_t netId, const std::string &nat64PrefixStr) override;
193     int32_t StopClat(const std::string &interfaceName) override;
194     int32_t ClearFirewallAllRules() override;
195 #ifdef SUPPORT_SYSVPN
196     int32_t ProcessVpnStage(NetsysNative::SysVpnStageCode stage) override;
197 #endif // SUPPORT_SYSVPN
198     int32_t CloseSocketsUid(const std::string &ipAddr, uint32_t uid) override;
199     int32_t SetBrokerUidAccessPolicyMap(const std::unordered_map<uint32_t, uint32_t> &uidMaps) override;
200     int32_t DelBrokerUidAccessPolicyMap(uint32_t uid) override;
201     int32_t SetNicTrafficAllowed(const std::vector<std::string> &ifaceNames, bool status) override;
202     int32_t SetUserDefinedServerFlag(uint16_t netId, bool flag) override;
203 protected:
204     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
205     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
206 
207 private:
208     NetsysNativeService();
209     bool Init();
210     void GetDumpMessage(std::string &message);
211     void OnNetManagerRestart();
212 
213 private:
214     enum ServiceRunningState {
215         STATE_STOPPED = 0,
216         STATE_RUNNING,
217     };
218 
219     ServiceRunningState state_{ServiceRunningState::STATE_STOPPED};
220 
221     static sptr<NetsysNativeService> instance_;
222 
223     std::shared_ptr<IptablesWrapper> iptablesWrapper_ = nullptr;
224     std::unique_ptr<OHOS::nmd::NetManagerNative> netsysService_ = nullptr;
225     std::unique_ptr<OHOS::nmd::NetlinkManager> manager_ = nullptr;
226     std::unique_ptr<OHOS::nmd::DhcpController> dhcpController_ = nullptr;
227     std::unique_ptr<OHOS::nmd::FwmarkNetwork> fwmarkNetwork_ = nullptr;
228     std::unique_ptr<OHOS::nmd::SharingManager> sharingManager_ = nullptr;
229     std::unique_ptr<OHOS::NetManagerStandard::NetsysBpfStats> bpfStats_ = nullptr;
230     std::shared_ptr<OHOS::nmd::NetDiagWrapper> netDiagWrapper = nullptr;
231 #ifdef FEATURE_NET_FIREWALL_ENABLE
232     std::shared_ptr<OHOS::NetManagerStandard::NetsysBpfNetFirewall> bpfNetFirewall_ = nullptr;
233 #endif
234     std::unique_ptr<OHOS::nmd::ClatManager> clatManager_ = nullptr;
235 
236     sptr<INotifyCallback> notifyCallback_ = nullptr;
237 
238     std::mutex instanceLock_;
239     bool hasSARemoved_ = false;
240     std::set<uint32_t> sharingTypeIsOn_;
241 };
242 } // namespace NetsysNative
243 } // namespace OHOS
244 #endif // NETSYS_NATIVE_SERVICE_H
245