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
41namespace OHOS {
42namespace NetsysNative {
43class NetsysNativeService : public SystemAbility, public NetsysNativeServiceStub, protected NoCopyable {
44    DECLARE_SYSTEM_ABILITY(NetsysNativeService);
45
46public:
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) override;
60    int32_t DestroyNetworkCache(uint16_t netId) 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) 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 GetTotalStats(uint64_t &stats, uint32_t type) override;
138    int32_t GetUidStats(uint64_t &stats, uint32_t type, uint32_t uid) override;
139    int32_t GetIfaceStats(uint64_t &stats, uint32_t type, const std::string &interfaceName) override;
140    int32_t GetAllSimStatsInfo(std::vector<OHOS::NetManagerStandard::NetStatsInfo> &stats) override;
141    int32_t DeleteSimStatsInfo(uint32_t uid) override;
142    int32_t GetAllStatsInfo(std::vector<OHOS::NetManagerStandard::NetStatsInfo> &stats) override;
143    int32_t DeleteStatsInfo(uint32_t uid) override;
144    int32_t SetIptablesCommandForRes(const std::string &cmd, std::string &respond, IptablesType ipType) override;
145    int32_t NetDiagPingHost(const NetDiagPingOption &pingOption, const sptr<INetDiagCallback> &callback) override;
146    int32_t NetDiagGetRouteTable(std::list<NetDiagRouteTable> &routeTables) override;
147    int32_t NetDiagGetSocketsInfo(NetDiagProtocolType socketType, NetDiagSocketsInfo &socketsInfo) override;
148    int32_t NetDiagGetInterfaceConfig(std::list<NetDiagIfaceConfig> &configs, const std::string &ifaceName) override;
149    int32_t NetDiagUpdateInterfaceConfig(const NetDiagIfaceConfig &config, const std::string &ifaceName,
150                                         bool add) override;
151    int32_t NetDiagSetInterfaceActiveState(const std::string &ifaceName, bool up) override;
152    int32_t AddStaticArp(const std::string &ipAddr, const std::string &macAddr,
153                         const std::string &ifName) override;
154    int32_t DelStaticArp(const std::string &ipAddr, const std::string &macAddr,
155                         const std::string &ifName) override;
156    int32_t RegisterDnsResultCallback(const sptr<INetDnsResultCallback> &callback, uint32_t timeStep) override;
157    int32_t UnregisterDnsResultCallback(const sptr<INetDnsResultCallback> &callback) override;
158    int32_t RegisterDnsHealthCallback(const sptr<INetDnsHealthCallback> &callback) override;
159    int32_t UnregisterDnsHealthCallback(const sptr<INetDnsHealthCallback> &callback) override;
160    int32_t GetCookieStats(uint64_t &stats, uint32_t type, uint64_t cookie) override;
161    int32_t GetNetworkSharingType(std::set<uint32_t>& sharingTypeIsOn) override;
162    int32_t UpdateNetworkSharingType(uint32_t type, bool isOpen) override;
163
164#ifdef FEATURE_NET_FIREWALL_ENABLE
165    int32_t SetFirewallRules(NetFirewallRuleType type, const std::vector<sptr<NetFirewallBaseRule>> &ruleList,
166                             bool isFinish) override;
167    int32_t SetFirewallDefaultAction(FirewallRuleAction inDefault, FirewallRuleAction outDefault) override;
168    int32_t SetFirewallCurrentUserId(int32_t userId) override;
169    int32_t ClearFirewallRules(NetFirewallRuleType type) override;
170    int32_t RegisterNetFirewallCallback(const sptr<INetFirewallCallback> &callback) override;
171    int32_t UnRegisterNetFirewallCallback(const sptr<INetFirewallCallback> &callback) override;
172#endif
173#ifdef FEATURE_WEARABLE_DISTRIBUTED_NET_ENABLE
174    int32_t EnableWearableDistributedNetForward(const int32_t tcpPortId, const int32_t udpPortId) override;
175    int32_t DisableWearableDistributedNetForward() override;
176#endif
177    int32_t SetIpv6PrivacyExtensions(const std::string &interfaceName, const uint32_t on) override;
178    int32_t SetEnableIpv6(const std::string &interfaceName, const uint32_t on) override;
179
180    int32_t SetNetworkAccessPolicy(uint32_t uid, NetworkAccessPolicy policy, bool reconfirmFlag,
181                                   bool isBroker) override;
182    int32_t DeleteNetworkAccessPolicy(uint32_t uid) override;
183    int32_t NotifyNetBearerTypeChange(std::set<NetBearType> bearerTypes) override;
184    int32_t StartClat(const std::string &interfaceName, int32_t netId, const std::string &nat64PrefixStr) override;
185    int32_t StopClat(const std::string &interfaceName) override;
186    int32_t ClearFirewallAllRules() override;
187    int32_t SetNicTrafficAllowed(const std::vector<std::string> &ifaceNames, bool status) override;
188#ifdef SUPPORT_SYSVPN
189    int32_t ProcessVpnStage(NetsysNative::SysVpnStageCode stage) override;
190#endif // SUPPORT_SYSVPN
191    int32_t CloseSocketsUid(const std::string &ipAddr, uint32_t uid) override;
192protected:
193    void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
194    void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
195
196private:
197    NetsysNativeService();
198    bool Init();
199    void GetDumpMessage(std::string &message);
200    void OnNetManagerRestart();
201
202private:
203    enum ServiceRunningState {
204        STATE_STOPPED = 0,
205        STATE_RUNNING,
206    };
207
208    ServiceRunningState state_{ServiceRunningState::STATE_STOPPED};
209
210    static sptr<NetsysNativeService> instance_;
211
212    std::shared_ptr<IptablesWrapper> iptablesWrapper_ = nullptr;
213    std::unique_ptr<OHOS::nmd::NetManagerNative> netsysService_ = nullptr;
214    std::unique_ptr<OHOS::nmd::NetlinkManager> manager_ = nullptr;
215    std::unique_ptr<OHOS::nmd::DhcpController> dhcpController_ = nullptr;
216    std::unique_ptr<OHOS::nmd::FwmarkNetwork> fwmarkNetwork_ = nullptr;
217    std::unique_ptr<OHOS::nmd::SharingManager> sharingManager_ = nullptr;
218    std::unique_ptr<OHOS::NetManagerStandard::NetsysBpfStats> bpfStats_ = nullptr;
219    std::shared_ptr<OHOS::nmd::NetDiagWrapper> netDiagWrapper = nullptr;
220#ifdef FEATURE_NET_FIREWALL_ENABLE
221    std::shared_ptr<OHOS::NetManagerStandard::NetsysBpfNetFirewall> bpfNetFirewall_ = nullptr;
222#endif
223    std::unique_ptr<OHOS::nmd::ClatManager> clatManager_ = nullptr;
224
225    sptr<INotifyCallback> notifyCallback_ = nullptr;
226
227    std::mutex instanceLock_;
228    bool hasSARemoved_ = false;
229    std::set<uint32_t> sharingTypeIsOn_;
230};
231} // namespace NetsysNative
232} // namespace OHOS
233#endif // NETSYS_NATIVE_SERVICE_H
234