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:
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 GetFwmarkForNetwork(int32_t netId, MarkMaskParcel &markMaskParcel) override;
104     int32_t SetInterfaceConfig(const InterfaceConfigurationParcel &cfg) override;
105     int32_t GetInterfaceConfig(InterfaceConfigurationParcel &cfg) override;
106     int32_t InterfaceGetList(std::vector<std::string> &ifaces) override;
107     int32_t StartDhcpClient(const std::string &iface, bool bIpv6) override;
108     int32_t StopDhcpClient(const std::string &iface, bool bIpv6) override;
109     int32_t StartDhcpService(const std::string &iface, const std::string &ipv4addr) override;
110     int32_t StopDhcpService(const std::string &iface) override;
111     int32_t IpEnableForwarding(const std::string &requester) override;
112     int32_t IpDisableForwarding(const std::string &requester) override;
113     int32_t EnableNat(const std::string &downstreamIface, const std::string &upstreamIface) override;
114     int32_t DisableNat(const std::string &downstreamIface, const std::string &upstreamIface) override;
115     int32_t IpfwdAddInterfaceForward(const std::string &fromIface, const std::string &toiIface) override;
116     int32_t IpfwdRemoveInterfaceForward(const std::string &fromIface, const std::string &toiIface) override;
117     int32_t FirewallSetUidsDeniedListChain(uint32_t chain, const std::vector<uint32_t> &uids) override;
118     int32_t FirewallEnableChain(uint32_t chain, bool enable) override;
119     int32_t FirewallSetUidRule(uint32_t chain, const std::vector<uint32_t> &uids, uint32_t firewallRule) override;
120     int32_t BandwidthEnableDataSaver(bool enable) override;
121     int32_t BandwidthSetIfaceQuota(const std::string &ifName, int64_t bytes) override;
122     int32_t BandwidthRemoveIfaceQuota(const std::string &ifName) override;
123     int32_t FirewallSetUidsAllowedListChain(uint32_t chain, const std::vector<uint32_t> &uids) override;
124     int32_t BandwidthAddAllowedList(uint32_t uid) override;
125     int32_t BandwidthRemoveAllowedList(uint32_t uid) override;
126     int32_t BandwidthAddDeniedList(uint32_t uid) override;
127     int32_t BandwidthRemoveDeniedList(uint32_t uid) override;
128     int32_t ShareDnsSet(uint16_t netId) override;
129     int32_t StartDnsProxyListen() override;
130     int32_t StopDnsProxyListen() override;
131     int32_t GetNetworkSharingTraffic(const std::string &downIface, const std::string &upIface,
132                                      NetworkSharingTraffic &traffic) override;
133     int32_t GetTotalStats(uint64_t &stats, uint32_t type) override;
134     int32_t GetUidStats(uint64_t &stats, uint32_t type, uint32_t uid) override;
135     int32_t GetIfaceStats(uint64_t &stats, uint32_t type, const std::string &interfaceName) override;
136     int32_t GetAllSimStatsInfo(std::vector<OHOS::NetManagerStandard::NetStatsInfo> &stats) override;
137     int32_t DeleteSimStatsInfo(uint32_t uid) override;
138     int32_t GetAllStatsInfo(std::vector<OHOS::NetManagerStandard::NetStatsInfo> &stats) override;
139     int32_t DeleteStatsInfo(uint32_t uid) override;
140     int32_t SetIptablesCommandForRes(const std::string &cmd, std::string &respond, IptablesType ipType) override;
141     int32_t NetDiagPingHost(const NetDiagPingOption &pingOption, const sptr<INetDiagCallback> &callback) override;
142     int32_t NetDiagGetRouteTable(std::list<NetDiagRouteTable> &routeTables) override;
143     int32_t NetDiagGetSocketsInfo(NetDiagProtocolType socketType, NetDiagSocketsInfo &socketsInfo) override;
144     int32_t NetDiagGetInterfaceConfig(std::list<NetDiagIfaceConfig> &configs, const std::string &ifaceName) override;
145     int32_t NetDiagUpdateInterfaceConfig(const NetDiagIfaceConfig &config, const std::string &ifaceName,
146                                          bool add) override;
147     int32_t NetDiagSetInterfaceActiveState(const std::string &ifaceName, bool up) override;
148     int32_t AddStaticArp(const std::string &ipAddr, const std::string &macAddr,
149                          const std::string &ifName) override;
150     int32_t DelStaticArp(const std::string &ipAddr, const std::string &macAddr,
151                          const std::string &ifName) override;
152     int32_t RegisterDnsResultCallback(const sptr<INetDnsResultCallback> &callback, uint32_t timeStep) override;
153     int32_t UnregisterDnsResultCallback(const sptr<INetDnsResultCallback> &callback) override;
154     int32_t RegisterDnsHealthCallback(const sptr<INetDnsHealthCallback> &callback) override;
155     int32_t UnregisterDnsHealthCallback(const sptr<INetDnsHealthCallback> &callback) override;
156     int32_t GetCookieStats(uint64_t &stats, uint32_t type, uint64_t cookie) override;
157     int32_t GetNetworkSharingType(std::set<uint32_t>& sharingTypeIsOn) override;
158     int32_t UpdateNetworkSharingType(uint32_t type, bool isOpen) override;
159 
160 #ifdef FEATURE_NET_FIREWALL_ENABLE
161     int32_t SetFirewallRules(NetFirewallRuleType type, const std::vector<sptr<NetFirewallBaseRule>> &ruleList,
162                              bool isFinish) override;
163     int32_t SetFirewallDefaultAction(FirewallRuleAction inDefault, FirewallRuleAction outDefault) override;
164     int32_t SetFirewallCurrentUserId(int32_t userId) override;
165     int32_t ClearFirewallRules(NetFirewallRuleType type) override;
166     int32_t RegisterNetFirewallCallback(const sptr<INetFirewallCallback> &callback) override;
167     int32_t UnRegisterNetFirewallCallback(const sptr<INetFirewallCallback> &callback) override;
168 #endif
169 
170     int32_t SetIpv6PrivacyExtensions(const std::string &interfaceName, const uint32_t on) override;
171     int32_t SetEnableIpv6(const std::string &interfaceName, const uint32_t on) override;
172 
173     int32_t SetNetworkAccessPolicy(uint32_t uid, NetworkAccessPolicy policy, bool reconfirmFlag,
174                                    bool isBroker) override;
175     int32_t DeleteNetworkAccessPolicy(uint32_t uid) override;
176     int32_t NotifyNetBearerTypeChange(std::set<NetBearType> bearerTypes) override;
177     int32_t StartClat(const std::string &interfaceName, int32_t netId, const std::string &nat64PrefixStr) override;
178     int32_t StopClat(const std::string &interfaceName) override;
179     int32_t ClearFirewallAllRules() override;
180     int32_t SetNicTrafficAllowed(const std::vector<std::string> &ifaceNames, bool status) override;
181 protected:
182     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
183     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
184 
185 private:
186     NetsysNativeService();
187     bool Init();
188     void GetDumpMessage(std::string &message);
189     void OnNetManagerRestart();
190 
191 private:
192     enum ServiceRunningState {
193         STATE_STOPPED = 0,
194         STATE_RUNNING,
195     };
196 
197     ServiceRunningState state_{ServiceRunningState::STATE_STOPPED};
198 
199     static sptr<NetsysNativeService> instance_;
200 
201     std::shared_ptr<IptablesWrapper> iptablesWrapper_ = nullptr;
202     std::unique_ptr<OHOS::nmd::NetManagerNative> netsysService_ = nullptr;
203     std::unique_ptr<OHOS::nmd::NetlinkManager> manager_ = nullptr;
204     std::unique_ptr<OHOS::nmd::DhcpController> dhcpController_ = nullptr;
205     std::unique_ptr<OHOS::nmd::FwmarkNetwork> fwmarkNetwork_ = nullptr;
206     std::unique_ptr<OHOS::nmd::SharingManager> sharingManager_ = nullptr;
207     std::unique_ptr<OHOS::NetManagerStandard::NetsysBpfStats> bpfStats_ = nullptr;
208     std::shared_ptr<OHOS::nmd::NetDiagWrapper> netDiagWrapper = nullptr;
209 #ifdef FEATURE_NET_FIREWALL_ENABLE
210     std::shared_ptr<OHOS::NetManagerStandard::NetsysBpfNetFirewall> bpfNetFirewall_ = nullptr;
211 #endif
212     std::unique_ptr<OHOS::nmd::ClatManager> clatManager_ = nullptr;
213 
214     sptr<INotifyCallback> notifyCallback_ = nullptr;
215 
216     std::mutex instanceLock_;
217     bool hasSARemoved_ = false;
218     std::set<uint32_t> sharingTypeIsOn_;
219 };
220 } // namespace NetsysNative
221 } // namespace OHOS
222 #endif // NETSYS_NATIVE_SERVICE_H
223