• 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 #include "dhcp_controller.h"
26 #include "fwmark_network.h"
27 #include "i_netsys_service.h"
28 #include "iremote_stub.h"
29 #include "net_manager_native.h"
30 #include "netlink_manager.h"
31 #include "netsys_native_service_stub.h"
32 #include "sharing_manager.h"
33 
34 namespace OHOS {
35 namespace NetsysNative {
36 class NetsysNativeService : public SystemAbility, public NetsysNativeServiceStub, protected NoCopyable {
37     DECLARE_SYSTEM_ABILITY(NetsysNativeService);
38 
39 public:
SystemAbility(saID,runOnCreate)40     explicit NetsysNativeService(int32_t saID, bool runOnCreate = true) : SystemAbility(saID, runOnCreate){};
41     ~NetsysNativeService() override = default;
42 
43     void OnStart() override;
44     void OnStop() override;
45     int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override;
46 
47     int32_t SetResolverConfig(uint16_t netId, uint16_t baseTimeoutMsec, uint8_t retryCount,
48                               const std::vector<std::string> &servers,
49                               const std::vector<std::string> &domains) override;
50     int32_t GetResolverConfig(uint16_t netId, std::vector<std::string> &servers, std::vector<std::string> &domains,
51                               uint16_t &baseTimeoutMsec, uint8_t &retryCount) override;
52     int32_t CreateNetworkCache(uint16_t netId) override;
53     int32_t DestroyNetworkCache(uint16_t netId) override;
54     int32_t GetAddrInfo(const std::string &hostName, const std::string &serverName, const AddrInfo &hints,
55                         uint16_t netId, std::vector<AddrInfo> &res) override;
56     int32_t SetInterfaceMtu(const std::string &interfaceName, int32_t mtu) override;
57     int32_t GetInterfaceMtu(const std::string &interfaceName) override;
58 
59     int32_t RegisterNotifyCallback(sptr<INotifyCallback> &callback) override;
60     int32_t UnRegisterNotifyCallback(sptr<INotifyCallback> &callback) override;
61 
62     int32_t NetworkAddRoute(int32_t netId, const std::string &interfaceName, const std::string &destination,
63                             const std::string &nextHop) override;
64     int32_t NetworkRemoveRoute(int32_t netId, const std::string &interfaceName, const std::string &destination,
65                                const std::string &nextHop) override;
66     int32_t NetworkAddRouteParcel(int32_t netId, const RouteInfoParcel &routeInfo) override;
67     int32_t NetworkRemoveRouteParcel(int32_t netId, const RouteInfoParcel &routeInfo) override;
68     int32_t NetworkSetDefault(int32_t netId) override;
69     int32_t NetworkGetDefault() override;
70     int32_t NetworkClearDefault() override;
71     int32_t GetProcSysNet(int32_t family, int32_t which, const std::string &ifname, const std::string &parameter,
72                           std::string &value) override;
73     int32_t SetProcSysNet(int32_t family, int32_t which, const std::string &ifname, const std::string &parameter,
74                           std::string &value) override;
75     int32_t SetInternetPermission(uint32_t uid, uint8_t allow) override;
76     int32_t NetworkCreatePhysical(int32_t netId, int32_t permission) override;
77     int32_t NetworkCreateVirtual(int32_t netId, bool hasDns) override;
78     int32_t NetworkAddUids(int32_t netId, const std::vector<UidRange> &uidRanges) override;
79     int32_t NetworkDelUids(int32_t netId, const std::vector<UidRange> &uidRanges) override;
80     int32_t AddInterfaceAddress(const std::string &interfaceName, const std::string &addrString,
81                                 int32_t prefixLength) override;
82     int32_t DelInterfaceAddress(const std::string &interfaceName, const std::string &addrString,
83                                 int32_t prefixLength) override;
84     int32_t InterfaceSetIpAddress(const std::string &ifaceName, const std::string &ipAddress) override;
85     int32_t InterfaceSetIffUp(const std::string &ifaceName) override;
86     int32_t NetworkAddInterface(int32_t netId, const std::string &iface) override;
87     int32_t NetworkRemoveInterface(int32_t netId, const std::string &iface) override;
88     int32_t NetworkDestroy(int32_t netId) override;
89     int32_t GetFwmarkForNetwork(int32_t netId, MarkMaskParcel &markMaskParcel) override;
90     int32_t SetInterfaceConfig(const InterfaceConfigurationParcel &cfg) override;
91     int32_t GetInterfaceConfig(InterfaceConfigurationParcel &cfg) override;
92     int32_t InterfaceGetList(std::vector<std::string> &ifaces) override;
93     int32_t StartDhcpClient(const std::string &iface, bool bIpv6) override;
94     int32_t StopDhcpClient(const std::string &iface, bool bIpv6) override;
95     int32_t StartDhcpService(const std::string &iface, const std::string &ipv4addr) override;
96     int32_t StopDhcpService(const std::string &iface) override;
97     int32_t IpEnableForwarding(const std::string &requester) override;
98     int32_t IpDisableForwarding(const std::string &requester) override;
99     int32_t EnableNat(const std::string &downstreamIface, const std::string &upstreamIface) override;
100     int32_t DisableNat(const std::string &downstreamIface, const std::string &upstreamIface) override;
101     int32_t IpfwdAddInterfaceForward(const std::string &fromIface, const std::string &toiIface) override;
102     int32_t IpfwdRemoveInterfaceForward(const std::string &fromIface, const std::string &toiIface) override;
103     int32_t FirewallSetUidsDeniedListChain(uint32_t chain, const std::vector<uint32_t> &uids) override;
104     int32_t FirewallEnableChain(uint32_t chain, bool enable) override;
105     int32_t FirewallSetUidRule(uint32_t chain, const std::vector<uint32_t> &uids, uint32_t firewallRule) override;
106     int32_t BandwidthEnableDataSaver(bool enable) override;
107     int32_t BandwidthSetIfaceQuota(const std::string &ifName, int64_t bytes) override;
108     int32_t BandwidthRemoveIfaceQuota(const std::string &ifName) override;
109     int32_t FirewallSetUidsAllowedListChain(uint32_t chain, const std::vector<uint32_t> &uids) override;
110     int32_t BandwidthAddAllowedList(uint32_t uid) override;
111     int32_t BandwidthRemoveAllowedList(uint32_t uid) override;
112     int32_t BandwidthAddDeniedList(uint32_t uid) override;
113     int32_t BandwidthRemoveDeniedList(uint32_t uid) override;
114     int32_t ShareDnsSet(uint16_t netId) override;
115     int32_t StartDnsProxyListen() override;
116     int32_t StopDnsProxyListen() override;
117     int32_t GetNetworkSharingTraffic(const std::string &downIface, const std::string &upIface,
118                                      NetworkSharingTraffic &traffic) override;
119     int32_t GetTotalStats(uint64_t &stats, uint32_t type) override;
120     int32_t GetUidStats(uint64_t &stats, uint32_t type, uint32_t uid) override;
121     int32_t GetIfaceStats(uint64_t &stats, uint32_t type, const std::string &interfaceName) override;
122     int32_t GetAllStatsInfo(std::vector<OHOS::NetManagerStandard::NetStatsInfo> &stats) override;
123     int32_t SetIptablesCommandForRes(const std::string &cmd, std::string &respond) override;
124 
125 protected:
126     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
127     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
128 
129 private:
130     NetsysNativeService();
131     bool Init();
132     void GetDumpMessage(std::string &message);
133     void OnNetManagerRestart();
134 
135 private:
136     enum ServiceRunningState {
137         STATE_STOPPED = 0,
138         STATE_RUNNING,
139     };
140 
141     ServiceRunningState state_{ServiceRunningState::STATE_STOPPED};
142 
143     static sptr<NetsysNativeService> instance_;
144 
145     std::unique_ptr<OHOS::nmd::NetManagerNative> netsysService_ = nullptr;
146     std::unique_ptr<OHOS::nmd::NetlinkManager> manager_ = nullptr;
147     std::unique_ptr<OHOS::nmd::DhcpController> dhcpController_ = nullptr;
148     std::unique_ptr<OHOS::nmd::FwmarkNetwork> fwmarkNetwork_ = nullptr;
149     std::unique_ptr<OHOS::nmd::SharingManager> sharingManager_ = nullptr;
150     std::unique_ptr<OHOS::NetManagerStandard::NetsysBpfStats> bpfStats_ = nullptr;
151     std::shared_ptr<IptablesWrapper> iptablesWrapper_ = nullptr;
152 
153     sptr<INotifyCallback> notifyCallback_ = nullptr;
154 
155     std::mutex instanceLock_;
156     bool hasSARemoved_ = false;
157 };
158 } // namespace NetsysNative
159 } // namespace OHOS
160 #endif // NETSYS_NATIVE_SERVICE_H
161