1b1b8bc3fSopenharmony_ci/* 2b1b8bc3fSopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd. 3b1b8bc3fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4b1b8bc3fSopenharmony_ci * you may not use this file except in compliance with the License. 5b1b8bc3fSopenharmony_ci * You may obtain a copy of the License at 6b1b8bc3fSopenharmony_ci * 7b1b8bc3fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8b1b8bc3fSopenharmony_ci * 9b1b8bc3fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10b1b8bc3fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11b1b8bc3fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12b1b8bc3fSopenharmony_ci * See the License for the specific language governing permissions and 13b1b8bc3fSopenharmony_ci * limitations under the License. 14b1b8bc3fSopenharmony_ci */ 15b1b8bc3fSopenharmony_ci 16b1b8bc3fSopenharmony_ci#ifndef NETSYS_NATIVE_SERVICE_H 17b1b8bc3fSopenharmony_ci#define NETSYS_NATIVE_SERVICE_H 18b1b8bc3fSopenharmony_ci 19b1b8bc3fSopenharmony_ci#include <mutex> 20b1b8bc3fSopenharmony_ci 21b1b8bc3fSopenharmony_ci#include "system_ability.h" 22b1b8bc3fSopenharmony_ci#include "system_ability_status_change_stub.h" 23b1b8bc3fSopenharmony_ci 24b1b8bc3fSopenharmony_ci#include "bpf_stats.h" 25b1b8bc3fSopenharmony_ci#ifdef FEATURE_NET_FIREWALL_ENABLE 26b1b8bc3fSopenharmony_ci#include "bpf_netfirewall.h" 27b1b8bc3fSopenharmony_ci#endif 28b1b8bc3fSopenharmony_ci#include "dhcp_controller.h" 29b1b8bc3fSopenharmony_ci#include "fwmark_network.h" 30b1b8bc3fSopenharmony_ci#include "i_netsys_service.h" 31b1b8bc3fSopenharmony_ci#include "iremote_stub.h" 32b1b8bc3fSopenharmony_ci#include "net_diag_wrapper.h" 33b1b8bc3fSopenharmony_ci#include "net_manager_native.h" 34b1b8bc3fSopenharmony_ci#include "netlink_manager.h" 35b1b8bc3fSopenharmony_ci#include "netsys_native_service_stub.h" 36b1b8bc3fSopenharmony_ci#include "sharing_manager.h" 37b1b8bc3fSopenharmony_ci#include "netsys_access_policy.h" 38b1b8bc3fSopenharmony_ci#include "clat_manager.h" 39b1b8bc3fSopenharmony_ci#include "vnic_manager.h" 40b1b8bc3fSopenharmony_ci 41b1b8bc3fSopenharmony_cinamespace OHOS { 42b1b8bc3fSopenharmony_cinamespace NetsysNative { 43b1b8bc3fSopenharmony_ciclass NetsysNativeService : public SystemAbility, public NetsysNativeServiceStub, protected NoCopyable { 44b1b8bc3fSopenharmony_ci DECLARE_SYSTEM_ABILITY(NetsysNativeService); 45b1b8bc3fSopenharmony_ci 46b1b8bc3fSopenharmony_cipublic: 47b1b8bc3fSopenharmony_ci explicit NetsysNativeService(int32_t saID, bool runOnCreate = true) : SystemAbility(saID, runOnCreate){}; 48b1b8bc3fSopenharmony_ci ~NetsysNativeService() override = default; 49b1b8bc3fSopenharmony_ci 50b1b8bc3fSopenharmony_ci void OnStart() override; 51b1b8bc3fSopenharmony_ci void OnStop() override; 52b1b8bc3fSopenharmony_ci int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override; 53b1b8bc3fSopenharmony_ci 54b1b8bc3fSopenharmony_ci int32_t SetResolverConfig(uint16_t netId, uint16_t baseTimeoutMsec, uint8_t retryCount, 55b1b8bc3fSopenharmony_ci const std::vector<std::string> &servers, 56b1b8bc3fSopenharmony_ci const std::vector<std::string> &domains) override; 57b1b8bc3fSopenharmony_ci int32_t GetResolverConfig(uint16_t netId, std::vector<std::string> &servers, std::vector<std::string> &domains, 58b1b8bc3fSopenharmony_ci uint16_t &baseTimeoutMsec, uint8_t &retryCount) override; 59b1b8bc3fSopenharmony_ci int32_t CreateNetworkCache(uint16_t netId) override; 60b1b8bc3fSopenharmony_ci int32_t DestroyNetworkCache(uint16_t netId) override; 61b1b8bc3fSopenharmony_ci int32_t GetAddrInfo(const std::string &hostName, const std::string &serverName, const AddrInfo &hints, 62b1b8bc3fSopenharmony_ci uint16_t netId, std::vector<AddrInfo> &res) override; 63b1b8bc3fSopenharmony_ci int32_t SetInterfaceMtu(const std::string &interfaceName, int32_t mtu) override; 64b1b8bc3fSopenharmony_ci int32_t GetInterfaceMtu(const std::string &interfaceName) override; 65b1b8bc3fSopenharmony_ci 66b1b8bc3fSopenharmony_ci int32_t SetTcpBufferSizes(const std::string &tcpBufferSizes) override; 67b1b8bc3fSopenharmony_ci 68b1b8bc3fSopenharmony_ci int32_t RegisterNotifyCallback(sptr<INotifyCallback> &callback) override; 69b1b8bc3fSopenharmony_ci int32_t UnRegisterNotifyCallback(sptr<INotifyCallback> &callback) override; 70b1b8bc3fSopenharmony_ci 71b1b8bc3fSopenharmony_ci int32_t NetworkAddRoute(int32_t netId, const std::string &interfaceName, const std::string &destination, 72b1b8bc3fSopenharmony_ci const std::string &nextHop) override; 73b1b8bc3fSopenharmony_ci int32_t NetworkRemoveRoute(int32_t netId, const std::string &interfaceName, const std::string &destination, 74b1b8bc3fSopenharmony_ci const std::string &nextHop) override; 75b1b8bc3fSopenharmony_ci int32_t NetworkAddRouteParcel(int32_t netId, const RouteInfoParcel &routeInfo) override; 76b1b8bc3fSopenharmony_ci int32_t NetworkRemoveRouteParcel(int32_t netId, const RouteInfoParcel &routeInfo) override; 77b1b8bc3fSopenharmony_ci int32_t NetworkSetDefault(int32_t netId) override; 78b1b8bc3fSopenharmony_ci int32_t NetworkGetDefault() override; 79b1b8bc3fSopenharmony_ci int32_t NetworkClearDefault() override; 80b1b8bc3fSopenharmony_ci int32_t GetProcSysNet(int32_t family, int32_t which, const std::string &ifname, const std::string ¶meter, 81b1b8bc3fSopenharmony_ci std::string &value) override; 82b1b8bc3fSopenharmony_ci int32_t SetProcSysNet(int32_t family, int32_t which, const std::string &ifname, const std::string ¶meter, 83b1b8bc3fSopenharmony_ci std::string &value) override; 84b1b8bc3fSopenharmony_ci int32_t SetInternetPermission(uint32_t uid, uint8_t allow, uint8_t isBroker) override; 85b1b8bc3fSopenharmony_ci int32_t NetworkCreatePhysical(int32_t netId, int32_t permission) override; 86b1b8bc3fSopenharmony_ci int32_t NetworkCreateVirtual(int32_t netId, bool hasDns) override; 87b1b8bc3fSopenharmony_ci int32_t NetworkAddUids(int32_t netId, const std::vector<UidRange> &uidRanges) override; 88b1b8bc3fSopenharmony_ci int32_t NetworkDelUids(int32_t netId, const std::vector<UidRange> &uidRanges) override; 89b1b8bc3fSopenharmony_ci int32_t AddInterfaceAddress(const std::string &interfaceName, const std::string &addrString, 90b1b8bc3fSopenharmony_ci int32_t prefixLength) override; 91b1b8bc3fSopenharmony_ci int32_t DelInterfaceAddress(const std::string &interfaceName, const std::string &addrString, 92b1b8bc3fSopenharmony_ci int32_t prefixLength) override; 93b1b8bc3fSopenharmony_ci int32_t DelInterfaceAddress(const std::string &interfaceName, const std::string &addrString, 94b1b8bc3fSopenharmony_ci int32_t prefixLength, const std::string &netCapabilities) override; 95b1b8bc3fSopenharmony_ci int32_t InterfaceSetIpAddress(const std::string &ifaceName, const std::string &ipAddress) override; 96b1b8bc3fSopenharmony_ci int32_t InterfaceSetIffUp(const std::string &ifaceName) override; 97b1b8bc3fSopenharmony_ci int32_t NetworkAddInterface(int32_t netId, const std::string &iface, NetBearType netBearerType) override; 98b1b8bc3fSopenharmony_ci int32_t NetworkRemoveInterface(int32_t netId, const std::string &iface) override; 99b1b8bc3fSopenharmony_ci int32_t NetworkDestroy(int32_t netId) override; 100b1b8bc3fSopenharmony_ci int32_t CreateVnic(uint16_t mtu, const std::string &tunAddr, int32_t prefix, 101b1b8bc3fSopenharmony_ci const std::set<int32_t> &uids) override; 102b1b8bc3fSopenharmony_ci int32_t DestroyVnic() override; 103b1b8bc3fSopenharmony_ci int32_t EnableDistributedClientNet(const std::string &virnicAddr, const std::string &iif) override; 104b1b8bc3fSopenharmony_ci int32_t EnableDistributedServerNet(const std::string &iif, const std::string &devIface, 105b1b8bc3fSopenharmony_ci const std::string &dstAddr) override; 106b1b8bc3fSopenharmony_ci int32_t DisableDistributedNet(bool isServer) override; 107b1b8bc3fSopenharmony_ci int32_t GetFwmarkForNetwork(int32_t netId, MarkMaskParcel &markMaskParcel) override; 108b1b8bc3fSopenharmony_ci int32_t SetInterfaceConfig(const InterfaceConfigurationParcel &cfg) override; 109b1b8bc3fSopenharmony_ci int32_t GetInterfaceConfig(InterfaceConfigurationParcel &cfg) override; 110b1b8bc3fSopenharmony_ci int32_t InterfaceGetList(std::vector<std::string> &ifaces) override; 111b1b8bc3fSopenharmony_ci int32_t StartDhcpClient(const std::string &iface, bool bIpv6) override; 112b1b8bc3fSopenharmony_ci int32_t StopDhcpClient(const std::string &iface, bool bIpv6) override; 113b1b8bc3fSopenharmony_ci int32_t StartDhcpService(const std::string &iface, const std::string &ipv4addr) override; 114b1b8bc3fSopenharmony_ci int32_t StopDhcpService(const std::string &iface) override; 115b1b8bc3fSopenharmony_ci int32_t IpEnableForwarding(const std::string &requester) override; 116b1b8bc3fSopenharmony_ci int32_t IpDisableForwarding(const std::string &requester) override; 117b1b8bc3fSopenharmony_ci int32_t EnableNat(const std::string &downstreamIface, const std::string &upstreamIface) override; 118b1b8bc3fSopenharmony_ci int32_t DisableNat(const std::string &downstreamIface, const std::string &upstreamIface) override; 119b1b8bc3fSopenharmony_ci int32_t IpfwdAddInterfaceForward(const std::string &fromIface, const std::string &toiIface) override; 120b1b8bc3fSopenharmony_ci int32_t IpfwdRemoveInterfaceForward(const std::string &fromIface, const std::string &toiIface) override; 121b1b8bc3fSopenharmony_ci int32_t FirewallSetUidsDeniedListChain(uint32_t chain, const std::vector<uint32_t> &uids) override; 122b1b8bc3fSopenharmony_ci int32_t FirewallEnableChain(uint32_t chain, bool enable) override; 123b1b8bc3fSopenharmony_ci int32_t FirewallSetUidRule(uint32_t chain, const std::vector<uint32_t> &uids, uint32_t firewallRule) override; 124b1b8bc3fSopenharmony_ci int32_t BandwidthEnableDataSaver(bool enable) override; 125b1b8bc3fSopenharmony_ci int32_t BandwidthSetIfaceQuota(const std::string &ifName, int64_t bytes) override; 126b1b8bc3fSopenharmony_ci int32_t BandwidthRemoveIfaceQuota(const std::string &ifName) override; 127b1b8bc3fSopenharmony_ci int32_t FirewallSetUidsAllowedListChain(uint32_t chain, const std::vector<uint32_t> &uids) override; 128b1b8bc3fSopenharmony_ci int32_t BandwidthAddAllowedList(uint32_t uid) override; 129b1b8bc3fSopenharmony_ci int32_t BandwidthRemoveAllowedList(uint32_t uid) override; 130b1b8bc3fSopenharmony_ci int32_t BandwidthAddDeniedList(uint32_t uid) override; 131b1b8bc3fSopenharmony_ci int32_t BandwidthRemoveDeniedList(uint32_t uid) override; 132b1b8bc3fSopenharmony_ci int32_t ShareDnsSet(uint16_t netId) override; 133b1b8bc3fSopenharmony_ci int32_t StartDnsProxyListen() override; 134b1b8bc3fSopenharmony_ci int32_t StopDnsProxyListen() override; 135b1b8bc3fSopenharmony_ci int32_t GetNetworkSharingTraffic(const std::string &downIface, const std::string &upIface, 136b1b8bc3fSopenharmony_ci NetworkSharingTraffic &traffic) override; 137b1b8bc3fSopenharmony_ci int32_t GetTotalStats(uint64_t &stats, uint32_t type) override; 138b1b8bc3fSopenharmony_ci int32_t GetUidStats(uint64_t &stats, uint32_t type, uint32_t uid) override; 139b1b8bc3fSopenharmony_ci int32_t GetIfaceStats(uint64_t &stats, uint32_t type, const std::string &interfaceName) override; 140b1b8bc3fSopenharmony_ci int32_t GetAllSimStatsInfo(std::vector<OHOS::NetManagerStandard::NetStatsInfo> &stats) override; 141b1b8bc3fSopenharmony_ci int32_t DeleteSimStatsInfo(uint32_t uid) override; 142b1b8bc3fSopenharmony_ci int32_t GetAllStatsInfo(std::vector<OHOS::NetManagerStandard::NetStatsInfo> &stats) override; 143b1b8bc3fSopenharmony_ci int32_t DeleteStatsInfo(uint32_t uid) override; 144b1b8bc3fSopenharmony_ci int32_t SetIptablesCommandForRes(const std::string &cmd, std::string &respond, IptablesType ipType) override; 145b1b8bc3fSopenharmony_ci int32_t NetDiagPingHost(const NetDiagPingOption &pingOption, const sptr<INetDiagCallback> &callback) override; 146b1b8bc3fSopenharmony_ci int32_t NetDiagGetRouteTable(std::list<NetDiagRouteTable> &routeTables) override; 147b1b8bc3fSopenharmony_ci int32_t NetDiagGetSocketsInfo(NetDiagProtocolType socketType, NetDiagSocketsInfo &socketsInfo) override; 148b1b8bc3fSopenharmony_ci int32_t NetDiagGetInterfaceConfig(std::list<NetDiagIfaceConfig> &configs, const std::string &ifaceName) override; 149b1b8bc3fSopenharmony_ci int32_t NetDiagUpdateInterfaceConfig(const NetDiagIfaceConfig &config, const std::string &ifaceName, 150b1b8bc3fSopenharmony_ci bool add) override; 151b1b8bc3fSopenharmony_ci int32_t NetDiagSetInterfaceActiveState(const std::string &ifaceName, bool up) override; 152b1b8bc3fSopenharmony_ci int32_t AddStaticArp(const std::string &ipAddr, const std::string &macAddr, 153b1b8bc3fSopenharmony_ci const std::string &ifName) override; 154b1b8bc3fSopenharmony_ci int32_t DelStaticArp(const std::string &ipAddr, const std::string &macAddr, 155b1b8bc3fSopenharmony_ci const std::string &ifName) override; 156b1b8bc3fSopenharmony_ci int32_t RegisterDnsResultCallback(const sptr<INetDnsResultCallback> &callback, uint32_t timeStep) override; 157b1b8bc3fSopenharmony_ci int32_t UnregisterDnsResultCallback(const sptr<INetDnsResultCallback> &callback) override; 158b1b8bc3fSopenharmony_ci int32_t RegisterDnsHealthCallback(const sptr<INetDnsHealthCallback> &callback) override; 159b1b8bc3fSopenharmony_ci int32_t UnregisterDnsHealthCallback(const sptr<INetDnsHealthCallback> &callback) override; 160b1b8bc3fSopenharmony_ci int32_t GetCookieStats(uint64_t &stats, uint32_t type, uint64_t cookie) override; 161b1b8bc3fSopenharmony_ci int32_t GetNetworkSharingType(std::set<uint32_t>& sharingTypeIsOn) override; 162b1b8bc3fSopenharmony_ci int32_t UpdateNetworkSharingType(uint32_t type, bool isOpen) override; 163b1b8bc3fSopenharmony_ci 164b1b8bc3fSopenharmony_ci#ifdef FEATURE_NET_FIREWALL_ENABLE 165b1b8bc3fSopenharmony_ci int32_t SetFirewallRules(NetFirewallRuleType type, const std::vector<sptr<NetFirewallBaseRule>> &ruleList, 166b1b8bc3fSopenharmony_ci bool isFinish) override; 167b1b8bc3fSopenharmony_ci int32_t SetFirewallDefaultAction(FirewallRuleAction inDefault, FirewallRuleAction outDefault) override; 168b1b8bc3fSopenharmony_ci int32_t SetFirewallCurrentUserId(int32_t userId) override; 169b1b8bc3fSopenharmony_ci int32_t ClearFirewallRules(NetFirewallRuleType type) override; 170b1b8bc3fSopenharmony_ci int32_t RegisterNetFirewallCallback(const sptr<INetFirewallCallback> &callback) override; 171b1b8bc3fSopenharmony_ci int32_t UnRegisterNetFirewallCallback(const sptr<INetFirewallCallback> &callback) override; 172b1b8bc3fSopenharmony_ci#endif 173b1b8bc3fSopenharmony_ci#ifdef FEATURE_WEARABLE_DISTRIBUTED_NET_ENABLE 174b1b8bc3fSopenharmony_ci int32_t EnableWearableDistributedNetForward(const int32_t tcpPortId, const int32_t udpPortId) override; 175b1b8bc3fSopenharmony_ci int32_t DisableWearableDistributedNetForward() override; 176b1b8bc3fSopenharmony_ci#endif 177b1b8bc3fSopenharmony_ci int32_t SetIpv6PrivacyExtensions(const std::string &interfaceName, const uint32_t on) override; 178b1b8bc3fSopenharmony_ci int32_t SetEnableIpv6(const std::string &interfaceName, const uint32_t on) override; 179b1b8bc3fSopenharmony_ci 180b1b8bc3fSopenharmony_ci int32_t SetNetworkAccessPolicy(uint32_t uid, NetworkAccessPolicy policy, bool reconfirmFlag, 181b1b8bc3fSopenharmony_ci bool isBroker) override; 182b1b8bc3fSopenharmony_ci int32_t DeleteNetworkAccessPolicy(uint32_t uid) override; 183b1b8bc3fSopenharmony_ci int32_t NotifyNetBearerTypeChange(std::set<NetBearType> bearerTypes) override; 184b1b8bc3fSopenharmony_ci int32_t StartClat(const std::string &interfaceName, int32_t netId, const std::string &nat64PrefixStr) override; 185b1b8bc3fSopenharmony_ci int32_t StopClat(const std::string &interfaceName) override; 186b1b8bc3fSopenharmony_ci int32_t ClearFirewallAllRules() override; 187b1b8bc3fSopenharmony_ci int32_t SetNicTrafficAllowed(const std::vector<std::string> &ifaceNames, bool status) override; 188b1b8bc3fSopenharmony_ci#ifdef SUPPORT_SYSVPN 189b1b8bc3fSopenharmony_ci int32_t ProcessVpnStage(NetsysNative::SysVpnStageCode stage) override; 190b1b8bc3fSopenharmony_ci#endif // SUPPORT_SYSVPN 191b1b8bc3fSopenharmony_ci int32_t CloseSocketsUid(const std::string &ipAddr, uint32_t uid) override; 192b1b8bc3fSopenharmony_ciprotected: 193b1b8bc3fSopenharmony_ci void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 194b1b8bc3fSopenharmony_ci void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 195b1b8bc3fSopenharmony_ci 196b1b8bc3fSopenharmony_ciprivate: 197b1b8bc3fSopenharmony_ci NetsysNativeService(); 198b1b8bc3fSopenharmony_ci bool Init(); 199b1b8bc3fSopenharmony_ci void GetDumpMessage(std::string &message); 200b1b8bc3fSopenharmony_ci void OnNetManagerRestart(); 201b1b8bc3fSopenharmony_ci 202b1b8bc3fSopenharmony_ciprivate: 203b1b8bc3fSopenharmony_ci enum ServiceRunningState { 204b1b8bc3fSopenharmony_ci STATE_STOPPED = 0, 205b1b8bc3fSopenharmony_ci STATE_RUNNING, 206b1b8bc3fSopenharmony_ci }; 207b1b8bc3fSopenharmony_ci 208b1b8bc3fSopenharmony_ci ServiceRunningState state_{ServiceRunningState::STATE_STOPPED}; 209b1b8bc3fSopenharmony_ci 210b1b8bc3fSopenharmony_ci static sptr<NetsysNativeService> instance_; 211b1b8bc3fSopenharmony_ci 212b1b8bc3fSopenharmony_ci std::shared_ptr<IptablesWrapper> iptablesWrapper_ = nullptr; 213b1b8bc3fSopenharmony_ci std::unique_ptr<OHOS::nmd::NetManagerNative> netsysService_ = nullptr; 214b1b8bc3fSopenharmony_ci std::unique_ptr<OHOS::nmd::NetlinkManager> manager_ = nullptr; 215b1b8bc3fSopenharmony_ci std::unique_ptr<OHOS::nmd::DhcpController> dhcpController_ = nullptr; 216b1b8bc3fSopenharmony_ci std::unique_ptr<OHOS::nmd::FwmarkNetwork> fwmarkNetwork_ = nullptr; 217b1b8bc3fSopenharmony_ci std::unique_ptr<OHOS::nmd::SharingManager> sharingManager_ = nullptr; 218b1b8bc3fSopenharmony_ci std::unique_ptr<OHOS::NetManagerStandard::NetsysBpfStats> bpfStats_ = nullptr; 219b1b8bc3fSopenharmony_ci std::shared_ptr<OHOS::nmd::NetDiagWrapper> netDiagWrapper = nullptr; 220b1b8bc3fSopenharmony_ci#ifdef FEATURE_NET_FIREWALL_ENABLE 221b1b8bc3fSopenharmony_ci std::shared_ptr<OHOS::NetManagerStandard::NetsysBpfNetFirewall> bpfNetFirewall_ = nullptr; 222b1b8bc3fSopenharmony_ci#endif 223b1b8bc3fSopenharmony_ci std::unique_ptr<OHOS::nmd::ClatManager> clatManager_ = nullptr; 224b1b8bc3fSopenharmony_ci 225b1b8bc3fSopenharmony_ci sptr<INotifyCallback> notifyCallback_ = nullptr; 226b1b8bc3fSopenharmony_ci 227b1b8bc3fSopenharmony_ci std::mutex instanceLock_; 228b1b8bc3fSopenharmony_ci bool hasSARemoved_ = false; 229b1b8bc3fSopenharmony_ci std::set<uint32_t> sharingTypeIsOn_; 230b1b8bc3fSopenharmony_ci}; 231b1b8bc3fSopenharmony_ci} // namespace NetsysNative 232b1b8bc3fSopenharmony_ci} // namespace OHOS 233b1b8bc3fSopenharmony_ci#endif // NETSYS_NATIVE_SERVICE_H 234