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