1 /* 2 * Copyright (c) 2021-2022 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 "dhcp_controller.h" 25 #include "fwmark_network.h" 26 #include "i_netsys_service.h" 27 #include "iremote_stub.h" 28 #include "net_manager_native.h" 29 #include "netlink_manager.h" 30 #include "netsys_native_service_stub.h" 31 #include "sharing_manager.h" 32 33 namespace OHOS { 34 namespace NetsysNative { 35 class NetsysNativeService : public SystemAbility, public NetsysNativeServiceStub, protected NoCopyable { 36 DECLARE_SYSTEM_ABILITY(NetsysNativeService); 37 38 public: SystemAbility(saID,runOnCreate)39 explicit NetsysNativeService(int32_t saID, bool runOnCreate = true) : SystemAbility(saID, runOnCreate){}; 40 ~NetsysNativeService() = default; 41 42 void OnStart() override; 43 void OnStop() override; 44 int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override; 45 46 int32_t SetResolverConfig(uint16_t netId, uint16_t baseTimeoutMsec, uint8_t retryCount, 47 const std::vector<std::string> &servers, 48 const std::vector<std::string> &domains) override; 49 int32_t GetResolverConfig(const uint16_t netId, std::vector<std::string> &servers, 50 std::vector<std::string> &domains, uint16_t &baseTimeoutMsec, 51 uint8_t &retryCount) override; 52 int32_t CreateNetworkCache(const uint16_t netId) override; 53 int32_t DestroyNetworkCache(const 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 InterfaceSetMtu(const std::string &interfaceName, int32_t mtu) override; 57 int32_t InterfaceGetMtu(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 ipversion, int32_t which, const std::string &ifname, const std::string ¶meter, 72 std::string &value) override; 73 int32_t SetProcSysNet(int32_t ipversion, int32_t which, const std::string &ifname, const std::string ¶meter, 74 std::string &value) override; 75 int32_t NetworkCreatePhysical(int32_t netId, int32_t permission) override; 76 int32_t InterfaceAddAddress(const std::string &interfaceName, const std::string &addrString, 77 int32_t prefixLength) override; 78 int32_t InterfaceDelAddress(const std::string &interfaceName, const std::string &addrString, 79 int32_t prefixLength) override; 80 int32_t InterfaceSetIpAddress(const std::string &ifaceName, const std::string &ipAddress) override; 81 int32_t InterfaceSetIffUp(const std::string &ifaceName) override; 82 int32_t NetworkAddInterface(int32_t netId, const std::string &iface) override; 83 int32_t NetworkRemoveInterface(int32_t netId, const std::string &iface) override; 84 int32_t NetworkDestroy(int32_t netId) override; 85 int32_t GetFwmarkForNetwork(int32_t netId, MarkMaskParcel &markMaskParcel) override; 86 int32_t InterfaceSetConfig(const InterfaceConfigurationParcel &cfg) override; 87 int32_t InterfaceGetConfig(InterfaceConfigurationParcel &cfg) override; 88 int32_t InterfaceGetList(std::vector<std::string> &ifaces) override; 89 int32_t StartDhcpClient(const std::string &iface, bool bIpv6) override; 90 int32_t StopDhcpClient(const std::string &iface, bool bIpv6) override; 91 int32_t StartDhcpService(const std::string &iface, const std::string &ipv4addr) override; 92 int32_t StopDhcpService(const std::string &iface) override; 93 int32_t IpEnableForwarding(const std::string &requester) override; 94 int32_t IpDisableForwarding(const std::string &requester) override; 95 int32_t EnableNat(const std::string &downstreamIface, const std::string &upstreamIface) override; 96 int32_t DisableNat(const std::string &downstreamIface, const std::string &upstreamIface) override; 97 int32_t IpfwdAddInterfaceForward(const std::string &fromIface, const std::string &toiIface) override; 98 int32_t IpfwdRemoveInterfaceForward(const std::string &fromIface, const std::string &toiIface) override; 99 int32_t FirewallSetUidsDeniedListChain(uint32_t chain, const std::vector<uint32_t> &uids) override; 100 int32_t FirewallEnableChain(uint32_t chain, bool enable) override; 101 int32_t FirewallSetUidRule(uint32_t chain, uint32_t uid, uint32_t firewallRule) override; 102 int32_t BandwidthEnableDataSaver(bool enable) override; 103 int32_t BandwidthSetIfaceQuota(const std::string &ifName, int64_t bytes) override; 104 int32_t BandwidthRemoveIfaceQuota(const std::string &ifName) override; 105 int32_t FirewallSetUidsAllowedListChain(uint32_t chain, const std::vector<uint32_t> &uids) override; 106 int32_t BandwidthAddAllowedList(uint32_t uid) override; 107 int32_t BandwidthRemoveAllowedList(uint32_t uid) override; 108 int32_t BandwidthAddDeniedList(uint32_t uid) override; 109 int32_t BandwidthRemoveDeniedList(uint32_t uid) override; 110 int32_t ShareDnsSet(uint16_t netId) override; 111 int32_t StartDnsProxyListen() override; 112 int32_t StopDnsProxyListen() override; 113 int32_t GetNetworkSharingTraffic(const std::string &downIface, const std::string &upIface, 114 NetworkSharingTraffic &traffic) override; 115 116 protected: 117 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 118 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 119 120 private: 121 NetsysNativeService(); 122 bool Init(); 123 void GetDumpMessage(std::string &message); 124 void OnNetManagerRestart(); 125 126 private: 127 enum ServiceRunningState { 128 STATE_STOPPED = 0, 129 STATE_RUNNING, 130 }; 131 132 ServiceRunningState state_{ServiceRunningState::STATE_STOPPED}; 133 134 static sptr<NetsysNativeService> instance_; 135 136 std::unique_ptr<OHOS::nmd::NetManagerNative> netsysService_ = nullptr; 137 std::unique_ptr<OHOS::nmd::NetlinkManager> manager_ = nullptr; 138 std::unique_ptr<OHOS::nmd::DhcpController> dhcpController_ = nullptr; 139 std::unique_ptr<OHOS::nmd::FwmarkNetwork> fwmarkNetwork_ = nullptr; 140 std::unique_ptr<OHOS::nmd::SharingManager> sharingManager_ = nullptr; 141 142 sptr<INotifyCallback> notifyCallback_ = nullptr; 143 144 std::mutex instanceLock_; 145 bool hasSARemoved_ = false; 146 }; 147 } // namespace NetsysNative 148 } // namespace OHOS 149 #endif // NETSYS_NATIVE_SERVICE_H 150