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 #include <cstdlib>
17 #include <net/route.h>
18 #include <netdb.h>
19 #include <unistd.h>
20
21 #include "ipc_skeleton.h"
22 #include "net_manager_constants.h"
23 #include "netmanager_base_common_utils.h"
24 #include "netmanager_base_permission.h"
25 #include "netnative_log_wrapper.h"
26 #include "netsys_native_service_stub.h"
27 #include "securec.h"
28 #include "i_net_dns_result_callback.h"
29 #include "i_net_dns_health_callback.h"
30
31 using namespace OHOS::NetManagerStandard::CommonUtils;
32 namespace OHOS {
33 namespace NetsysNative {
34 namespace {
35 constexpr int32_t MAX_VNIC_UID_ARRAY_SIZE = 20;
36 constexpr int32_t MAX_FLAG_NUM = 64;
37 constexpr int32_t MAX_DNS_CONFIG_SIZE = 4;
38 constexpr int32_t NETMANAGER_ERR_PERMISSION_DENIED = 201;
39 constexpr uint32_t UIDS_LIST_MAX_SIZE = 1024;
40 constexpr uint32_t MAX_UID_ARRAY_SIZE = 1024;
41 constexpr uint32_t MAX_CONFIG_LIST_SIZE = 1024;
42 constexpr uint32_t MAX_ROUTE_TABLE_SIZE = 128;
43 constexpr uint32_t MAX_IFACENAMES_SIZE = 128;
44 } // namespace
45
NetsysNativeServiceStub()46 NetsysNativeServiceStub::NetsysNativeServiceStub()
47 {
48 InitNetInfoOpToInterfaceMap();
49 InitBandwidthOpToInterfaceMap();
50 InitFirewallOpToInterfaceMap();
51 InitOpToInterfaceMapExt();
52 InitNetDiagOpToInterfaceMap();
53 InitNetDnsDiagOpToInterfaceMap();
54 InitStaticArpToInterfaceMap();
55 InitNetVnicInterfaceMap();
56 InitNetVirnicInterfaceMap();
57 #ifdef SUPPORT_SYSVPN
58 InitVpnOpToInterfaceMap();
59 #endif // SUPPORT_SYSVPN
60 uids_ = {UID_ROOT, UID_SHELL, UID_NET_MANAGER, UID_WIFI, UID_RADIO, UID_HIDUMPER_SERVICE,
61 UID_SAMGR, UID_PARAM_WATCHER, UID_EDM, UID_SECURITY_COLLECTOR};
62 }
63
InitNetInfoOpToInterfaceMap()64 void NetsysNativeServiceStub::InitNetInfoOpToInterfaceMap()
65 {
66 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_SET_RESOLVER_CONFIG)] =
67 &NetsysNativeServiceStub::CmdSetResolverConfig;
68 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_RESOLVER_CONFIG)] =
69 &NetsysNativeServiceStub::CmdGetResolverConfig;
70 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_CREATE_NETWORK_CACHE)] =
71 &NetsysNativeServiceStub::CmdCreateNetworkCache;
72 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_DESTROY_NETWORK_CACHE)] =
73 &NetsysNativeServiceStub::CmdDestroyNetworkCache;
74 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_ADDR_INFO)] =
75 &NetsysNativeServiceStub::CmdGetAddrInfo;
76 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_SET_MTU)] =
77 &NetsysNativeServiceStub::CmdSetInterfaceMtu;
78 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_GET_MTU)] =
79 &NetsysNativeServiceStub::CmdGetInterfaceMtu;
80 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_SET_TCP_BUFFER_SIZES)] =
81 &NetsysNativeServiceStub::CmdSetTcpBufferSizes;
82 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_REGISTER_NOTIFY_CALLBACK)] =
83 &NetsysNativeServiceStub::CmdRegisterNotifyCallback;
84 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_UNREGISTER_NOTIFY_CALLBACK)] =
85 &NetsysNativeServiceStub::CmdUnRegisterNotifyCallback;
86 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_ADD_ROUTE)] =
87 &NetsysNativeServiceStub::CmdNetworkAddRoute;
88 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_REMOVE_ROUTE)] =
89 &NetsysNativeServiceStub::CmdNetworkRemoveRoute;
90 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_ADD_ROUTE_PARCEL)] =
91 &NetsysNativeServiceStub::CmdNetworkAddRouteParcel;
92 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_REMOVE_ROUTE_PARCEL)] =
93 &NetsysNativeServiceStub::CmdNetworkRemoveRouteParcel;
94 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_SET_DEFAULT)] =
95 &NetsysNativeServiceStub::CmdNetworkSetDefault;
96 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_GET_DEFAULT)] =
97 &NetsysNativeServiceStub::CmdNetworkGetDefault;
98 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_CLEAR_DEFAULT)] =
99 &NetsysNativeServiceStub::CmdNetworkClearDefault;
100 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_PROC_SYS_NET)] =
101 &NetsysNativeServiceStub::CmdGetProcSysNet;
102 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_SET_PROC_SYS_NET)] =
103 &NetsysNativeServiceStub::CmdSetProcSysNet;
104 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_CREATE_PHYSICAL)] =
105 &NetsysNativeServiceStub::CmdNetworkCreatePhysical;
106 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_ADD_ADDRESS)] =
107 &NetsysNativeServiceStub::CmdAddInterfaceAddress;
108 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_DEL_ADDRESS)] =
109 &NetsysNativeServiceStub::CmdDelInterfaceAddress;
110 #ifdef FEATURE_WEARABLE_DISTRIBUTED_NET_ENABLE
111 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_ENABLE_WEARABLE_DISTRIBUTED_NET_FORWARD)] =
112 &NetsysNativeServiceStub::CmdEnableWearableDistributedNetForward;
113 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_DISABLE_WEARABLE_DISTRIBUTED_NET_FORWARD)] =
114 &NetsysNativeServiceStub::CmdDisableWearableDistributedNetForward;
115 #endif
116 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_SET_IPV6_PRIVCAY_EXTENSION)] =
117 &NetsysNativeServiceStub::CmdSetIpv6PrivacyExtensions;
118 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_ENABLE_IPV6)] =
119 &NetsysNativeServiceStub::CmdSetIpv6Enable;
120 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_START_CLAT)] =
121 &NetsysNativeServiceStub::CmdStartClat;
122 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_STOP_CLAT)] =
123 &NetsysNativeServiceStub::CmdStopClat;
124 }
125
InitBandwidthOpToInterfaceMap()126 void NetsysNativeServiceStub::InitBandwidthOpToInterfaceMap()
127 {
128 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_SHARING_NETWORK_TRAFFIC)] =
129 &NetsysNativeServiceStub::CmdGetNetworkSharingTraffic;
130 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_TOTAL_STATS)] =
131 &NetsysNativeServiceStub::CmdGetTotalStats;
132 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_UID_STATS)] =
133 &NetsysNativeServiceStub::CmdGetUidStats;
134 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_IFACE_STATS)] =
135 &NetsysNativeServiceStub::CmdGetIfaceStats;
136 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_ALL_SIM_STATS_INFO)] =
137 &NetsysNativeServiceStub::CmdGetAllSimStatsInfo;
138 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_DELETE_SIM_STATS_INFO)] =
139 &NetsysNativeServiceStub::CmdDeleteSimStatsInfo;
140 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_ALL_STATS_INFO)] =
141 &NetsysNativeServiceStub::CmdGetAllStatsInfo;
142 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_DELETE_STATS_INFO)] =
143 &NetsysNativeServiceStub::CmdDeleteStatsInfo;
144 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_COOKIE_STATS)] =
145 &NetsysNativeServiceStub::CmdGetCookieStats;
146 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_CREATE_VIRTUAL)] =
147 &NetsysNativeServiceStub::CmdNetworkCreateVirtual;
148 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_ADD_UIDS)] =
149 &NetsysNativeServiceStub::CmdNetworkAddUids;
150 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_DEL_UIDS)] =
151 &NetsysNativeServiceStub::CmdNetworkDelUids;
152 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_BANDWIDTH_ENABLE_DATA_SAVER)] =
153 &NetsysNativeServiceStub::CmdBandwidthEnableDataSaver;
154 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_BANDWIDTH_SET_IFACE_QUOTA)] =
155 &NetsysNativeServiceStub::CmdBandwidthSetIfaceQuota;
156 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_BANDWIDTH_REMOVE_IFACE_QUOTA)] =
157 &NetsysNativeServiceStub::CmdBandwidthRemoveIfaceQuota;
158 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_BANDWIDTH_ADD_DENIED_LIST)] =
159 &NetsysNativeServiceStub::CmdBandwidthAddDeniedList;
160 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_BANDWIDTH_REMOVE_DENIED_LIST)] =
161 &NetsysNativeServiceStub::CmdBandwidthRemoveDeniedList;
162 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_BANDWIDTH_ADD_ALLOWED_LIST)] =
163 &NetsysNativeServiceStub::CmdBandwidthAddAllowedList;
164 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_BANDWIDTH_REMOVE_ALLOWED_LIST)] =
165 &NetsysNativeServiceStub::CmdBandwidthRemoveAllowedList;
166 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_SET_INTERNET_PERMISSION)] =
167 &NetsysNativeServiceStub::CmdSetInternetPermission;
168 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_SET_NETWORK_ACCESS_POLICY)] =
169 &NetsysNativeServiceStub::CmdSetNetworkAccessPolicy;
170 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_DEL_NETWORK_ACCESS_POLICY)] =
171 &NetsysNativeServiceStub::CmdDelNetworkAccessPolicy;
172 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NOTIFY_NETWORK_BEARER_TYPE_CHANGE)] =
173 &NetsysNativeServiceStub::CmdNotifyNetBearerTypeChange;
174 }
175
InitFirewallOpToInterfaceMap()176 void NetsysNativeServiceStub::InitFirewallOpToInterfaceMap()
177 {
178 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_FIREWALL_SET_UID_ALLOWED_LIST_CHAIN)] =
179 &NetsysNativeServiceStub::CmdFirewallSetUidsAllowedListChain;
180 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_FIREWALL_SET_UID_DENIED_LIST_CHAIN)] =
181 &NetsysNativeServiceStub::CmdFirewallSetUidsDeniedListChain;
182 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_FIREWALL_ENABLE_CHAIN)] =
183 &NetsysNativeServiceStub::CmdFirewallEnableChain;
184 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_FIREWALL_SET_UID_RULE)] =
185 &NetsysNativeServiceStub::CmdFirewallSetUidRule;
186 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_NETWORK_SHARING_TYPE)] =
187 &NetsysNativeServiceStub::CmdGetNetworkSharingType;
188 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_UPDATE_NETWORK_SHARING_TYPE)] =
189 &NetsysNativeServiceStub::CmdUpdateNetworkSharingType;
190 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_CLEAR_FIREWALL_RULE)] =
191 &NetsysNativeServiceStub::CmdClearFirewallAllRules;
192 #ifdef FEATURE_NET_FIREWALL_ENABLE
193 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NET_FIREWALL_SET_RULES)] =
194 &NetsysNativeServiceStub::CmdSetFirewallRules;
195 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NET_FIREWALL_SET_DEFAULT_ACTION)] =
196 &NetsysNativeServiceStub::CmdSetFirewallDefaultAction;
197 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NET_FIREWALL_SET_USER_ID)] =
198 &NetsysNativeServiceStub::CmdSetFirewallCurrentUserId;
199 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NET_FIREWALL_CLEAR_RULES)] =
200 &NetsysNativeServiceStub::CmdClearFirewallRules;
201 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NET_FIREWALL_REGISTER)] =
202 &NetsysNativeServiceStub::CmdRegisterNetFirewallCallback;
203 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NET_FIREWALL_UNREGISTER)] =
204 &NetsysNativeServiceStub::CmdUnRegisterNetFirewallCallback;
205 #endif
206 }
207
208 #ifdef SUPPORT_SYSVPN
InitVpnOpToInterfaceMap()209 void NetsysNativeServiceStub::InitVpnOpToInterfaceMap()
210 {
211 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_PROCESS_VPN_STAGE)] =
212 &NetsysNativeServiceStub::CmdProcessVpnStage;
213 }
214 #endif
215
InitOpToInterfaceMapExt()216 void NetsysNativeServiceStub::InitOpToInterfaceMapExt()
217 {
218 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_SET_IP_ADDRESS)] =
219 &NetsysNativeServiceStub::CmdInterfaceSetIpAddress;
220 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_SET_IFF_UP)] =
221 &NetsysNativeServiceStub::CmdInterfaceSetIffUp;
222 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_ADD_INTERFACE)] =
223 &NetsysNativeServiceStub::CmdNetworkAddInterface;
224 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_REMOVE_INTERFACE)] =
225 &NetsysNativeServiceStub::CmdNetworkRemoveInterface;
226 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETWORK_DESTROY)] =
227 &NetsysNativeServiceStub::CmdNetworkDestroy;
228 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_GET_FWMARK_FOR_NETWORK)] =
229 &NetsysNativeServiceStub::CmdGetFwmarkForNetwork;
230 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_SET_CONFIG)] =
231 &NetsysNativeServiceStub::CmdSetInterfaceConfig;
232 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_GET_CONFIG)] =
233 &NetsysNativeServiceStub::CmdGetInterfaceConfig;
234 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_INTERFACE_GET_LIST)] =
235 &NetsysNativeServiceStub::CmdInterfaceGetList;
236 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_START_DHCP_CLIENT)] =
237 &NetsysNativeServiceStub::CmdStartDhcpClient;
238 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_STOP_DHCP_CLIENT)] =
239 &NetsysNativeServiceStub::CmdStopDhcpClient;
240 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_START_DHCP_SERVICE)] =
241 &NetsysNativeServiceStub::CmdStartDhcpService;
242 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_STOP_DHCP_SERVICE)] =
243 &NetsysNativeServiceStub::CmdStopDhcpService;
244 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_IPENABLE_FORWARDING)] =
245 &NetsysNativeServiceStub::CmdIpEnableForwarding;
246 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_IPDISABLE_FORWARDING)] =
247 &NetsysNativeServiceStub::CmdIpDisableForwarding;
248 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_ENABLE_NAT)] =
249 &NetsysNativeServiceStub::CmdEnableNat;
250 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_DISABLE_NAT)] =
251 &NetsysNativeServiceStub::CmdDisableNat;
252 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_IPFWD_ADD_INTERFACE_FORWARD)] =
253 &NetsysNativeServiceStub::CmdIpfwdAddInterfaceForward;
254 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_IPFWD_REMOVE_INTERFACE_FORWARD)] =
255 &NetsysNativeServiceStub::CmdIpfwdRemoveInterfaceForward;
256 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_SET_IPTABLES_CMD_FOR_RES)] =
257 &NetsysNativeServiceStub::CmdSetIptablesCommandForRes;
258 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_TETHER_DNS_SET)] =
259 &NetsysNativeServiceStub::CmdShareDnsSet;
260 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_START_DNS_PROXY_LISTEN)] =
261 &NetsysNativeServiceStub::CmdStartDnsProxyListen;
262 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_STOP_DNS_PROXY_LISTEN)] =
263 &NetsysNativeServiceStub::CmdStopDnsProxyListen;
264 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_SET_NIC_TRAFFIC_ALLOWED)] =
265 &NetsysNativeServiceStub::CmdSetNicTrafficAllowed;
266 }
267
InitNetDiagOpToInterfaceMap()268 void NetsysNativeServiceStub::InitNetDiagOpToInterfaceMap()
269 {
270 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETDIAG_PING_HOST)] =
271 &NetsysNativeServiceStub::CmdNetDiagPingHost;
272 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETDIAG_GET_ROUTE_TABLE)] =
273 &NetsysNativeServiceStub::CmdNetDiagGetRouteTable;
274 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETDIAG_GET_SOCKETS_INFO)] =
275 &NetsysNativeServiceStub::CmdNetDiagGetSocketsInfo;
276 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETDIAG_GET_IFACE_CONFIG)] =
277 &NetsysNativeServiceStub::CmdNetDiagGetInterfaceConfig;
278 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETDIAG_UPDATE_IFACE_CONFIG)] =
279 &NetsysNativeServiceStub::CmdNetDiagUpdateInterfaceConfig;
280 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NETDIAG_SET_IFACE_ACTIVE_STATE)] =
281 &NetsysNativeServiceStub::CmdNetDiagSetInterfaceActiveState;
282 }
283
InitStaticArpToInterfaceMap()284 void NetsysNativeServiceStub::InitStaticArpToInterfaceMap()
285 {
286 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_ADD_STATIC_ARP)] =
287 &NetsysNativeServiceStub::CmdAddStaticArp;
288 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_DEL_STATIC_ARP)] =
289 &NetsysNativeServiceStub::CmdDelStaticArp;
290 }
291
InitNetDnsDiagOpToInterfaceMap()292 void NetsysNativeServiceStub::InitNetDnsDiagOpToInterfaceMap()
293 {
294 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_REGISTER_DNS_RESULT_LISTENER)] =
295 &NetsysNativeServiceStub::CmdRegisterDnsResultListener;
296 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_UNREGISTER_DNS_RESULT_LISTENER)] =
297 &NetsysNativeServiceStub::CmdUnregisterDnsResultListener;
298 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_REGISTER_DNS_HEALTH_LISTENER)] =
299 &NetsysNativeServiceStub::CmdRegisterDnsHealthListener;
300 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_UNREGISTER_DNS_HEALTH_LISTENER)] =
301 &NetsysNativeServiceStub::CmdUnregisterDnsHealthListener;
302 }
303
InitNetVnicInterfaceMap()304 void NetsysNativeServiceStub::InitNetVnicInterfaceMap()
305 {
306 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_VNIC_CREATE)] =
307 &NetsysNativeServiceStub::CmdCreateVnic;
308 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_VNIC_DESTROY)] =
309 &NetsysNativeServiceStub::CmdDestroyVnic;
310 }
311
InitNetVirnicInterfaceMap()312 void NetsysNativeServiceStub::InitNetVirnicInterfaceMap()
313 {
314 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_ENABLE_DISTRIBUTE_CLIENT_NET)] =
315 &NetsysNativeServiceStub::CmdEnableDistributedClientNet;
316 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_ENABLE_DISTRIBUTE_SERVER_NET)] =
317 &NetsysNativeServiceStub::CmdEnableDistributedServerNet;
318 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_DISABLE_DISTRIBUTE_NET)] =
319 &NetsysNativeServiceStub::CmdDisableDistributedNet;
320 opToInterfaceMap_[static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_CLOSE_SOCKETS_UID)] =
321 &NetsysNativeServiceStub::CmdCloseSocketsUid;
322 }
323
OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)324 int32_t NetsysNativeServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
325 MessageOption &option)
326 {
327 NETNATIVE_LOG_D("Begin to call procedure with code %{public}u", code);
328 auto interfaceIndex = opToInterfaceMap_.find(code);
329 if (interfaceIndex == opToInterfaceMap_.end() || !interfaceIndex->second) {
330 NETNATIVE_LOGE("Cannot response request %d: unknown tranction", code);
331 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
332 }
333 auto uid = IPCSkeleton::GetCallingUid();
334 if (std::find(uids_.begin(), uids_.end(), uid) == uids_.end()) {
335 NETNATIVE_LOGE("This uid connot use netsys");
336 if (!reply.WriteInt32(NETMANAGER_ERR_PERMISSION_DENIED)) {
337 return IPC_STUB_WRITE_PARCEL_ERR;
338 }
339 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
340 }
341
342 if (code == static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_SET_IPTABLES_CMD_FOR_RES) && uid != UID_EDM &&
343 uid != UID_NET_MANAGER) {
344 if (!reply.WriteInt32(NETMANAGER_ERR_PERMISSION_DENIED)) {
345 return IPC_STUB_WRITE_PARCEL_ERR;
346 }
347 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
348 }
349
350 const std::u16string descriptor = NetsysNativeServiceStub::GetDescriptor();
351 const std::u16string remoteDescriptor = data.ReadInterfaceToken();
352 if (descriptor != remoteDescriptor) {
353 NETNATIVE_LOGE("Check remote descriptor failed");
354 return IPC_STUB_INVALID_DATA_ERR;
355 }
356
357 #ifdef FEATURE_NET_FIREWALL_ENABLE
358 if (code >= static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NET_FIREWALL_SET_DEFAULT_ACTION) &&
359 code <= static_cast<uint32_t>(NetsysInterfaceCode::NETSYS_NET_FIREWALL_UNREGISTER) &&
360 !NetManagerPermission::CheckPermission(Permission::NETSYS_INTERNAL)) {
361 if (!reply.WriteInt32(NETMANAGER_ERR_PERMISSION_DENIED)) {
362 return IPC_STUB_WRITE_PARCEL_ERR;
363 }
364 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
365 }
366 #endif
367
368 return (this->*(interfaceIndex->second))(data, reply);
369 }
370
CmdSetResolverConfig(MessageParcel &data, MessageParcel &reply)371 int32_t NetsysNativeServiceStub::CmdSetResolverConfig(MessageParcel &data, MessageParcel &reply)
372 {
373 uint16_t netId = 0;
374 uint16_t baseTimeoutMsec = 0;
375 uint8_t retryCount = 0;
376 std::vector<std::string> servers;
377 std::vector<std::string> domains;
378 if (!data.ReadUint16(netId)) {
379 return ERR_FLATTEN_OBJECT;
380 }
381 if (!data.ReadUint16(baseTimeoutMsec)) {
382 return ERR_FLATTEN_OBJECT;
383 }
384 if (!data.ReadUint8(retryCount)) {
385 return ERR_FLATTEN_OBJECT;
386 }
387 int32_t vServerSize;
388 if (!data.ReadInt32(vServerSize)) {
389 return ERR_FLATTEN_OBJECT;
390 }
391 vServerSize = (vServerSize > MAX_DNS_CONFIG_SIZE) ? MAX_DNS_CONFIG_SIZE : vServerSize;
392 std::string s;
393 for (int32_t i = 0; i < vServerSize; ++i) {
394 std::string().swap(s);
395 if (!data.ReadString(s)) {
396 return ERR_FLATTEN_OBJECT;
397 }
398 servers.push_back(s);
399 }
400 int32_t vDomainSize;
401 if (!data.ReadInt32(vDomainSize)) {
402 return ERR_FLATTEN_OBJECT;
403 }
404 vDomainSize = (vDomainSize > MAX_DNS_CONFIG_SIZE) ? MAX_DNS_CONFIG_SIZE : vDomainSize;
405 for (int32_t i = 0; i < vDomainSize; ++i) {
406 std::string().swap(s);
407 if (!data.ReadString(s)) {
408 return ERR_FLATTEN_OBJECT;
409 }
410 domains.push_back(s);
411 }
412
413 int32_t result = SetResolverConfig(netId, baseTimeoutMsec, retryCount, servers, domains);
414 reply.WriteInt32(result);
415 NETNATIVE_LOG_D("SetResolverConfig has received result %{public}d", result);
416
417 return ERR_NONE;
418 }
419
CmdGetResolverConfig(MessageParcel &data, MessageParcel &reply)420 int32_t NetsysNativeServiceStub::CmdGetResolverConfig(MessageParcel &data, MessageParcel &reply)
421 {
422 uint16_t baseTimeoutMsec;
423 uint8_t retryCount;
424 uint16_t netId = 0;
425 std::vector<std::string> servers;
426 std::vector<std::string> domains;
427
428 data.ReadUint16(netId);
429 int32_t result = GetResolverConfig(netId, servers, domains, baseTimeoutMsec, retryCount);
430 reply.WriteInt32(result);
431 reply.WriteUint16(baseTimeoutMsec);
432 reply.WriteUint8(retryCount);
433 auto vServerSize = static_cast<int32_t>(servers.size());
434 vServerSize = (vServerSize > MAX_DNS_CONFIG_SIZE) ? MAX_DNS_CONFIG_SIZE : vServerSize;
435 reply.WriteInt32(vServerSize);
436 int32_t index = 0;
437 for (auto &server : servers) {
438 if (++index > MAX_DNS_CONFIG_SIZE) {
439 break;
440 }
441 reply.WriteString(server);
442 }
443 auto vDomainsSize = static_cast<int32_t>(domains.size());
444 vDomainsSize = (vDomainsSize > MAX_DNS_CONFIG_SIZE) ? MAX_DNS_CONFIG_SIZE : vDomainsSize;
445 reply.WriteInt32(vDomainsSize);
446 std::vector<std::string>::iterator iterDomains;
447 index = 0;
448 for (iterDomains = domains.begin(); iterDomains != domains.end(); ++iterDomains) {
449 if (++index > MAX_DNS_CONFIG_SIZE) {
450 break;
451 }
452 reply.WriteString(*iterDomains);
453 }
454 NETNATIVE_LOG_D("GetResolverConfig has recved result %{public}d", result);
455 return ERR_NONE;
456 }
457
CmdCreateNetworkCache(MessageParcel &data, MessageParcel &reply)458 int32_t NetsysNativeServiceStub::CmdCreateNetworkCache(MessageParcel &data, MessageParcel &reply)
459 {
460 uint16_t netid = data.ReadUint16();
461 NETNATIVE_LOGI("CreateNetworkCache netid %{public}d", netid);
462 int32_t result = CreateNetworkCache(netid);
463 reply.WriteInt32(result);
464 NETNATIVE_LOG_D("CreateNetworkCache has recved result %{public}d", result);
465
466 return ERR_NONE;
467 }
468
CmdDestroyNetworkCache(MessageParcel &data, MessageParcel &reply)469 int32_t NetsysNativeServiceStub::CmdDestroyNetworkCache(MessageParcel &data, MessageParcel &reply)
470 {
471 uint16_t netId = data.ReadUint16();
472 int32_t result = DestroyNetworkCache(netId);
473 reply.WriteInt32(result);
474 NETNATIVE_LOG_D("DestroyNetworkCache has recved result %{public}d", result);
475
476 return ERR_NONE;
477 }
478
NetsysFreeAddrinfo(struct addrinfo *aihead)479 int32_t NetsysNativeServiceStub::NetsysFreeAddrinfo(struct addrinfo *aihead)
480 {
481 struct addrinfo *ai;
482 struct addrinfo *ainext;
483 for (ai = aihead; ai != nullptr; ai = ainext) {
484 if (ai->ai_addr != nullptr)
485 free(ai->ai_addr);
486 if (ai->ai_canonname != nullptr)
487 free(ai->ai_canonname);
488 ainext = ai->ai_next;
489 free(ai);
490 }
491 return ERR_NONE;
492 }
493
CmdGetAddrInfo(MessageParcel &data, MessageParcel &reply)494 int32_t NetsysNativeServiceStub::CmdGetAddrInfo(MessageParcel &data, MessageParcel &reply)
495 {
496 std::string hostName;
497 std::string serverName;
498 AddrInfo hints = {};
499 uint16_t netId;
500 if (!data.ReadString(hostName)) {
501 return IPC_STUB_INVALID_DATA_ERR;
502 }
503
504 if (!data.ReadString(serverName)) {
505 return IPC_STUB_INVALID_DATA_ERR;
506 }
507
508 auto p = data.ReadRawData(sizeof(AddrInfo));
509 if (p == nullptr) {
510 return IPC_STUB_INVALID_DATA_ERR;
511 }
512 if (memcpy_s(&hints, sizeof(AddrInfo), p, sizeof(AddrInfo)) != EOK) {
513 return IPC_STUB_INVALID_DATA_ERR;
514 }
515
516 if (!data.ReadUint16(netId)) {
517 return IPC_STUB_INVALID_DATA_ERR;
518 }
519
520 std::vector<AddrInfo> retInfo;
521 auto ret = GetAddrInfo(hostName, serverName, hints, netId, retInfo);
522 if (retInfo.size() > MAX_RESULTS) {
523 return IPC_STUB_INVALID_DATA_ERR;
524 }
525
526 if (!reply.WriteInt32(ret)) {
527 return IPC_STUB_WRITE_PARCEL_ERR;
528 }
529
530 if (ret != ERR_NONE) {
531 return ERR_NONE;
532 }
533
534 if (!reply.WriteUint32(static_cast<uint32_t>(retInfo.size()))) {
535 return IPC_STUB_WRITE_PARCEL_ERR;
536 }
537
538 for (const auto &info : retInfo) {
539 if (!reply.WriteRawData(&info, sizeof(AddrInfo))) {
540 return IPC_STUB_WRITE_PARCEL_ERR;
541 }
542 }
543 return ERR_NONE;
544 }
545
CmdSetInterfaceMtu(MessageParcel &data, MessageParcel &reply)546 int32_t NetsysNativeServiceStub::CmdSetInterfaceMtu(MessageParcel &data, MessageParcel &reply)
547 {
548 std::string ifName = data.ReadString();
549 int32_t mtu = data.ReadInt32();
550 int32_t result = SetInterfaceMtu(ifName, mtu);
551 reply.WriteInt32(result);
552 NETNATIVE_LOG_D("SetInterfaceMtu has recved result %{public}d", result);
553
554 return ERR_NONE;
555 }
556
CmdGetInterfaceMtu(MessageParcel &data, MessageParcel &reply)557 int32_t NetsysNativeServiceStub::CmdGetInterfaceMtu(MessageParcel &data, MessageParcel &reply)
558 {
559 std::string ifName = data.ReadString();
560 int32_t result = GetInterfaceMtu(ifName);
561 reply.WriteInt32(result);
562 NETNATIVE_LOG_D("GetInterfaceMtu has recved result %{public}d", result);
563
564 return ERR_NONE;
565 }
566
CmdSetTcpBufferSizes(MessageParcel &data, MessageParcel &reply)567 int32_t NetsysNativeServiceStub::CmdSetTcpBufferSizes(MessageParcel &data, MessageParcel &reply)
568 {
569 std::string tcpBufferSizes = data.ReadString();
570 int32_t result = SetTcpBufferSizes(tcpBufferSizes);
571 reply.WriteInt32(result);
572 NETNATIVE_LOG_D("SetTcpBufferSizes has recved result %{public}d", result);
573
574 return ERR_NONE;
575 }
576
CmdRegisterNotifyCallback(MessageParcel &data, MessageParcel &reply)577 int32_t NetsysNativeServiceStub::CmdRegisterNotifyCallback(MessageParcel &data, MessageParcel &reply)
578 {
579 NETNATIVE_LOG_D("Begin to dispatch cmd RegisterNotifyCallback");
580 sptr<IRemoteObject> remote = data.ReadRemoteObject();
581 if (remote == nullptr) {
582 NETNATIVE_LOGE("Callback ptr is nullptr.");
583 return -1;
584 }
585
586 sptr<INotifyCallback> callback = iface_cast<INotifyCallback>(remote);
587 int32_t result = RegisterNotifyCallback(callback);
588 reply.WriteInt32(result);
589 return ERR_NONE;
590 }
591
CmdUnRegisterNotifyCallback(MessageParcel &data, MessageParcel &reply)592 int32_t NetsysNativeServiceStub::CmdUnRegisterNotifyCallback(MessageParcel &data, MessageParcel &reply)
593 {
594 NETNATIVE_LOG_D("Begin to dispatch cmd UnRegisterNotifyCallback");
595 sptr<IRemoteObject> remote = data.ReadRemoteObject();
596 if (remote == nullptr) {
597 NETNATIVE_LOGE("Callback ptr is nullptr.");
598 return -1;
599 }
600
601 sptr<INotifyCallback> callback = iface_cast<INotifyCallback>(remote);
602 int32_t result = UnRegisterNotifyCallback(callback);
603 reply.WriteInt32(result);
604 return ERR_NONE;
605 }
606
CmdNetworkAddRoute(MessageParcel &data, MessageParcel &reply)607 int32_t NetsysNativeServiceStub::CmdNetworkAddRoute(MessageParcel &data, MessageParcel &reply)
608 {
609 int32_t netId = data.ReadInt32();
610 std::string ifName = data.ReadString();
611 std::string destination = data.ReadString();
612 std::string nextHop = data.ReadString();
613
614 NETNATIVE_LOGI("netId[%{public}d}, ifName[%{public}s], destination[%{public}s}, nextHop[%{public}s]", netId,
615 ifName.c_str(), ToAnonymousIp(destination).c_str(), ToAnonymousIp(nextHop).c_str());
616 int32_t result = NetworkAddRoute(netId, ifName, destination, nextHop);
617 reply.WriteInt32(result);
618 NETNATIVE_LOG_D("NetworkAddRoute has recved result %{public}d", result);
619 return result;
620 }
621
CmdNetworkRemoveRoute(MessageParcel &data, MessageParcel &reply)622 int32_t NetsysNativeServiceStub::CmdNetworkRemoveRoute(MessageParcel &data, MessageParcel &reply)
623 {
624 int32_t netId = data.ReadInt32();
625 std::string interfaceName = data.ReadString();
626 std::string destination = data.ReadString();
627 std::string nextHop = data.ReadString();
628
629 NETNATIVE_LOGI("netId[%{public}d}, ifName[%{public}s], destination[%{public}s}, nextHop[%{public}s]", netId,
630 interfaceName.c_str(), ToAnonymousIp(destination).c_str(), ToAnonymousIp(nextHop).c_str());
631 int32_t result = NetworkRemoveRoute(netId, interfaceName, destination, nextHop);
632 reply.WriteInt32(result);
633 NETNATIVE_LOG_D("NetworkRemoveRoute has recved result %{public}d", result);
634
635 return result;
636 }
637
CmdNetworkAddRouteParcel(MessageParcel &data, MessageParcel &reply)638 int32_t NetsysNativeServiceStub::CmdNetworkAddRouteParcel(MessageParcel &data, MessageParcel &reply)
639 {
640 RouteInfoParcel routeInfo = {};
641 int32_t netId = data.ReadInt32();
642 routeInfo.ifName = data.ReadString();
643 routeInfo.destination = data.ReadString();
644 routeInfo.nextHop = data.ReadString();
645 int32_t result = NetworkAddRouteParcel(netId, routeInfo);
646 reply.WriteInt32(result);
647 NETNATIVE_LOG_D("NetworkAddRouteParcel has recved result %{public}d", result);
648
649 return result;
650 }
651
CmdNetworkRemoveRouteParcel(MessageParcel &data, MessageParcel &reply)652 int32_t NetsysNativeServiceStub::CmdNetworkRemoveRouteParcel(MessageParcel &data, MessageParcel &reply)
653 {
654 RouteInfoParcel routeInfo = {};
655 int32_t netId = data.ReadInt32();
656 routeInfo.ifName = data.ReadString();
657 routeInfo.destination = data.ReadString();
658 routeInfo.nextHop = data.ReadString();
659
660 int32_t result = NetworkRemoveRouteParcel(netId, routeInfo);
661 reply.WriteInt32(result);
662 NETNATIVE_LOG_D("NetworkRemoveRouteParcel has recved result %{public}d", result);
663
664 return result;
665 }
666
CmdNetworkSetDefault(MessageParcel &data, MessageParcel &reply)667 int32_t NetsysNativeServiceStub::CmdNetworkSetDefault(MessageParcel &data, MessageParcel &reply)
668 {
669 int32_t netId = data.ReadInt32();
670
671 int32_t result = NetworkSetDefault(netId);
672 reply.WriteInt32(result);
673 NETNATIVE_LOG_D("NetworkSetDefault has recved result %{public}d", result);
674
675 return result;
676 }
677
CmdNetworkGetDefault(MessageParcel &data, MessageParcel &reply)678 int32_t NetsysNativeServiceStub::CmdNetworkGetDefault(MessageParcel &data, MessageParcel &reply)
679 {
680 int32_t result = NetworkGetDefault();
681 reply.WriteInt32(result);
682 NETNATIVE_LOG_D("NetworkGetDefault has recved result %{public}d", result);
683
684 return result;
685 }
686
CmdNetworkClearDefault(MessageParcel &data, MessageParcel &reply)687 int32_t NetsysNativeServiceStub::CmdNetworkClearDefault(MessageParcel &data, MessageParcel &reply)
688 {
689 int32_t result = NetworkClearDefault();
690 reply.WriteInt32(result);
691 NETNATIVE_LOG_D("NetworkClearDefault has recved result %{public}d", result);
692
693 return result;
694 }
695
CmdGetProcSysNet(MessageParcel &data, MessageParcel &reply)696 int32_t NetsysNativeServiceStub::CmdGetProcSysNet(MessageParcel &data, MessageParcel &reply)
697 {
698 NETNATIVE_LOG_D("Begin to dispatch cmd GetProcSysNet");
699 int32_t family = data.ReadInt32();
700 int32_t which = data.ReadInt32();
701 std::string ifname = data.ReadString();
702 std::string parameter = data.ReadString();
703 std::string value;
704 int32_t result = GetProcSysNet(family, which, ifname, parameter, value);
705 reply.WriteInt32(result);
706 std::string valueRsl = value;
707 reply.WriteString(valueRsl);
708 return result;
709 }
710
CmdSetProcSysNet(MessageParcel &data, MessageParcel &reply)711 int32_t NetsysNativeServiceStub::CmdSetProcSysNet(MessageParcel &data, MessageParcel &reply)
712 {
713 int32_t family = data.ReadInt32();
714 int32_t which = data.ReadInt32();
715 std::string ifname = data.ReadString();
716 std::string parameter = data.ReadString();
717 std::string value = data.ReadString();
718 int32_t result = SetProcSysNet(family, which, ifname, parameter, value);
719 reply.WriteInt32(result);
720 NETNATIVE_LOG_D("SetProcSysNet has recved result %{public}d", result);
721
722 return result;
723 }
724
CmdSetInternetPermission(MessageParcel &data, MessageParcel &reply)725 int32_t NetsysNativeServiceStub::CmdSetInternetPermission(MessageParcel &data, MessageParcel &reply)
726 {
727 uint32_t uid = data.ReadUint32();
728 uint8_t allow = data.ReadUint8();
729 uint8_t isBroker = data.ReadUint8();
730 int32_t result = SetInternetPermission(uid, allow, isBroker);
731 reply.WriteInt32(result);
732 NETNATIVE_LOG_D("SetInternetPermission has recved result %{public}d", result);
733 return result;
734 }
735
CmdNetworkCreatePhysical(MessageParcel &data, MessageParcel &reply)736 int32_t NetsysNativeServiceStub::CmdNetworkCreatePhysical(MessageParcel &data, MessageParcel &reply)
737 {
738 int32_t netId = data.ReadInt32();
739 int32_t permission = data.ReadInt32();
740
741 int32_t result = NetworkCreatePhysical(netId, permission);
742 reply.WriteInt32(result);
743 NETNATIVE_LOG_D("NetworkCreatePhysical has recved result %{public}d", result);
744
745 return result;
746 }
747
CmdNetworkCreateVirtual(MessageParcel &data, MessageParcel &reply)748 int32_t NetsysNativeServiceStub::CmdNetworkCreateVirtual(MessageParcel &data, MessageParcel &reply)
749 {
750 int32_t netId = 0;
751 bool hasDns = false;
752 if (!data.ReadInt32(netId) || !data.ReadBool(hasDns)) {
753 NETNATIVE_LOGE("read net id or hasDns failed");
754 return IPC_STUB_ERR;
755 }
756
757 int32_t result = NetworkCreateVirtual(netId, hasDns);
758 if (!reply.WriteInt32(result)) {
759 return IPC_STUB_WRITE_PARCEL_ERR;
760 }
761 NETNATIVE_LOG_D("NetworkCreateVirtual has recved result %{public}d", result);
762 return ERR_NONE;
763 }
764
CmdNetworkAddUids(MessageParcel &data, MessageParcel &reply)765 int32_t NetsysNativeServiceStub::CmdNetworkAddUids(MessageParcel &data, MessageParcel &reply)
766 {
767 int32_t netId = 0;
768 int32_t size = 0;
769 if (!data.ReadInt32(netId) || !data.ReadInt32(size)) {
770 NETNATIVE_LOGE("read net id or size failed");
771 return IPC_STUB_ERR;
772 }
773 size = (size > static_cast<int32_t>(MAX_UID_ARRAY_SIZE)) ? static_cast<int32_t>(MAX_UID_ARRAY_SIZE) : size;
774
775 sptr<UidRange> uid;
776 std::vector<UidRange> uidRanges;
777 for (int32_t index = 0; index < size; index++) {
778 uid = UidRange::Unmarshalling(data);
779 if (uid == nullptr) {
780 NETNATIVE_LOGE("UidRange::Unmarshalling(parcel) is null");
781 return IPC_STUB_ERR;
782 }
783 uidRanges.push_back(*uid);
784 }
785 int32_t result = NetworkAddUids(netId, uidRanges);
786 if (!reply.WriteInt32(result)) {
787 return IPC_STUB_WRITE_PARCEL_ERR;
788 }
789 NETNATIVE_LOG_D("NetworkAddUids has recved result %{public}d", result);
790 return ERR_NONE;
791 }
792
CmdNetworkDelUids(MessageParcel &data, MessageParcel &reply)793 int32_t NetsysNativeServiceStub::CmdNetworkDelUids(MessageParcel &data, MessageParcel &reply)
794 {
795 int32_t netId = 0;
796 int32_t size = 0;
797 if (!data.ReadInt32(netId) || !data.ReadInt32(size)) {
798 NETNATIVE_LOGE("read net id or size failed");
799 return IPC_STUB_ERR;
800 }
801
802 size = (size > static_cast<int32_t>(MAX_UID_ARRAY_SIZE)) ? static_cast<int32_t>(MAX_UID_ARRAY_SIZE) : size;
803
804 sptr<UidRange> uid;
805 std::vector<UidRange> uidRanges;
806 for (int32_t index = 0; index < size; index++) {
807 uid = UidRange::Unmarshalling(data);
808 if (uid == nullptr) {
809 NETNATIVE_LOGE("UidRange::Unmarshalling(parcel) is null");
810 return IPC_STUB_ERR;
811 }
812 uidRanges.push_back(*uid);
813 }
814 int32_t result = NetworkDelUids(netId, uidRanges);
815 if (!reply.WriteInt32(result)) {
816 return IPC_STUB_WRITE_PARCEL_ERR;
817 }
818 NETNATIVE_LOG_D("NetworkDelUids has recved result %{public}d", result);
819 return ERR_NONE;
820 }
821
CmdAddInterfaceAddress(MessageParcel &data, MessageParcel &reply)822 int32_t NetsysNativeServiceStub::CmdAddInterfaceAddress(MessageParcel &data, MessageParcel &reply)
823 {
824 std::string interfaceName = data.ReadString();
825 std::string ipAddr = data.ReadString();
826 int32_t prefixLength = data.ReadInt32();
827
828 int32_t result = AddInterfaceAddress(interfaceName, ipAddr, prefixLength);
829 reply.WriteInt32(result);
830 NETNATIVE_LOG_D("AddInterfaceAddress has recved result %{public}d", result);
831
832 return result;
833 }
834
CmdDelInterfaceAddress(MessageParcel &data, MessageParcel &reply)835 int32_t NetsysNativeServiceStub::CmdDelInterfaceAddress(MessageParcel &data, MessageParcel &reply)
836 {
837 std::string interfaceName = data.ReadString();
838 std::string ipAddr = data.ReadString();
839 int32_t prefixLength = data.ReadInt32();
840 std::string netCapabilities;
841 int32_t result = 0;
842 if (!data.ReadString(netCapabilities)) {
843 NETNATIVE_LOG_D("DelInterfaceAddress");
844 result = DelInterfaceAddress(interfaceName, ipAddr, prefixLength);
845 } else {
846 NETNATIVE_LOG_D("DelInterfaceAddress with netCapabilities %{public}s", netCapabilities.c_str());
847 result = DelInterfaceAddress(interfaceName, ipAddr, prefixLength, netCapabilities);
848 }
849 reply.WriteInt32(result);
850 NETNATIVE_LOG_D("DelInterfaceAddress has recved result %{public}d", result);
851
852 return result;
853 }
854
855 #ifdef FEATURE_WEARABLE_DISTRIBUTED_NET_ENABLE
CmdEnableWearableDistributedNetForward(MessageParcel &data, MessageParcel &reply)856 int32_t NetsysNativeServiceStub::CmdEnableWearableDistributedNetForward(MessageParcel &data, MessageParcel &reply)
857 {
858 NETNATIVE_LOGI("NetsysNativeServiceStub enable wearable distributed net forward");
859 int32_t tcpPort = data.ReadInt32();
860 int32_t udpPort = data.ReadInt32();
861 int32_t result = EnableWearableDistributedNetForward(tcpPort, udpPort);
862 if (!reply.WriteInt32(result)) {
863 return NETMANAGER_ERR_WRITE_DATA_FAIL;
864 }
865
866 return result;
867 }
868
CmdDisableWearableDistributedNetForward(MessageParcel &data, MessageParcel &reply)869 int32_t NetsysNativeServiceStub::CmdDisableWearableDistributedNetForward(MessageParcel &data, MessageParcel &reply)
870 {
871 NETNATIVE_LOGI("NetsysNativeServiceStub disable wearable distributed net forward");
872 int32_t result = DisableWearableDistributedNetForward();
873 if (!reply.WriteInt32(result)) {
874 return NETMANAGER_ERR_WRITE_DATA_FAIL;
875 }
876
877 return result;
878 }
879 #endif
880
CmdInterfaceSetIpAddress(MessageParcel &data, MessageParcel &reply)881 int32_t NetsysNativeServiceStub::CmdInterfaceSetIpAddress(MessageParcel &data, MessageParcel &reply)
882 {
883 std::string ifaceName = data.ReadString();
884 std::string ipAddress = data.ReadString();
885
886 int32_t result = InterfaceSetIpAddress(ifaceName, ipAddress);
887 reply.WriteInt32(result);
888 NETNATIVE_LOG_D("InterfaceSetIpAddress has recved result %{public}d", result);
889
890 return result;
891 }
892
CmdInterfaceSetIffUp(MessageParcel &data, MessageParcel &reply)893 int32_t NetsysNativeServiceStub::CmdInterfaceSetIffUp(MessageParcel &data, MessageParcel &reply)
894 {
895 std::string ifaceName = data.ReadString();
896
897 int32_t result = InterfaceSetIffUp(ifaceName);
898 reply.WriteInt32(result);
899 NETNATIVE_LOG_D("InterfaceSetIffUp has recved result %{public}d", result);
900
901 return result;
902 }
903
CmdNetworkAddInterface(MessageParcel &data, MessageParcel &reply)904 int32_t NetsysNativeServiceStub::CmdNetworkAddInterface(MessageParcel &data, MessageParcel &reply)
905 {
906 int32_t netId = data.ReadInt32();
907 std::string iface = data.ReadString();
908 NetBearType netBearerType = static_cast<NetBearType>(data.ReadUint8());
909
910 int32_t result = NetworkAddInterface(netId, iface, netBearerType);
911 reply.WriteInt32(result);
912 NETNATIVE_LOG_D("NetworkAddInterface has recved result %{public}d", result);
913
914 return result;
915 }
916
CmdNetworkRemoveInterface(MessageParcel &data, MessageParcel &reply)917 int32_t NetsysNativeServiceStub::CmdNetworkRemoveInterface(MessageParcel &data, MessageParcel &reply)
918 {
919 int32_t netId = data.ReadInt32();
920 std::string iface = data.ReadString();
921 int32_t result = NetworkRemoveInterface(netId, iface);
922 reply.WriteInt32(result);
923 NETNATIVE_LOG_D("NetworkRemoveInterface has recved result %{public}d", result);
924
925 return result;
926 }
927
CmdNetworkDestroy(MessageParcel &data, MessageParcel &reply)928 int32_t NetsysNativeServiceStub::CmdNetworkDestroy(MessageParcel &data, MessageParcel &reply)
929 {
930 int32_t netId = data.ReadInt32();
931 int32_t result = NetworkDestroy(netId);
932 reply.WriteInt32(result);
933 NETNATIVE_LOG_D("NetworkDestroy has recved result %{public}d", result);
934
935 return result;
936 }
937
CmdCreateVnic(MessageParcel &data, MessageParcel &reply)938 int32_t NetsysNativeServiceStub::CmdCreateVnic(MessageParcel &data, MessageParcel &reply)
939 {
940 uint16_t mtu = data.ReadUint16();
941 std::string tunAddr = data.ReadString();
942 int32_t prefix = data.ReadInt32();
943 std::set<int32_t> uids;
944 int32_t size = 0;
945 int32_t uid = 0;
946 if (!data.ReadInt32(size)) {
947 return NETMANAGER_ERR_READ_DATA_FAIL;
948 }
949
950 if (size < 0 || size > MAX_VNIC_UID_ARRAY_SIZE) {
951 NETNATIVE_LOGE("vnic uids size is invalid");
952 return NETMANAGER_ERR_READ_DATA_FAIL;
953 }
954
955 for (int32_t index = 0; index < size; index++) {
956 if (!data.ReadInt32(uid)) {
957 return NETMANAGER_ERR_READ_DATA_FAIL;
958 }
959 uids.insert(uid);
960 }
961 int32_t result = CreateVnic(mtu, tunAddr, prefix, uids);
962 reply.WriteInt32(result);
963 NETNATIVE_LOG_D("VnciCreate has recved result %{public}d", result);
964
965 return result;
966 }
967
CmdDestroyVnic(MessageParcel &data, MessageParcel &reply)968 int32_t NetsysNativeServiceStub::CmdDestroyVnic(MessageParcel &data, MessageParcel &reply)
969 {
970 int32_t result = DestroyVnic();
971 reply.WriteInt32(result);
972 NETNATIVE_LOG_D("VnicDestroy has recved result %{public}d", result);
973
974 return result;
975 }
976
CmdEnableDistributedClientNet(MessageParcel &data, MessageParcel &reply)977 int32_t NetsysNativeServiceStub::CmdEnableDistributedClientNet(MessageParcel &data, MessageParcel &reply)
978 {
979 std::string virnicAddr = data.ReadString();
980 std::string iif = data.ReadString();
981
982 int32_t result = EnableDistributedClientNet(virnicAddr, iif);
983 reply.WriteInt32(result);
984 NETNATIVE_LOG_D("CmdEnableDistributedClientNet has recved result %{public}d", result);
985
986 return result;
987 }
988
CmdEnableDistributedServerNet(MessageParcel &data, MessageParcel &reply)989 int32_t NetsysNativeServiceStub::CmdEnableDistributedServerNet(MessageParcel &data, MessageParcel &reply)
990 {
991 std::string iif = data.ReadString();
992 std::string devIface = data.ReadString();
993 std::string dstAddr = data.ReadString();
994
995 int32_t result = EnableDistributedServerNet(iif, devIface, dstAddr);
996 reply.WriteInt32(result);
997 NETNATIVE_LOG_D("CmdEnableDistributedServerNet has recved result %{public}d", result);
998
999 return result;
1000 }
1001
CmdDisableDistributedNet(MessageParcel &data, MessageParcel &reply)1002 int32_t NetsysNativeServiceStub::CmdDisableDistributedNet(MessageParcel &data, MessageParcel &reply)
1003 {
1004 bool isServer = data.ReadBool();
1005
1006 int32_t result = DisableDistributedNet(isServer);
1007 reply.WriteInt32(result);
1008 NETNATIVE_LOG_D("CmdDisableDistributedNet has recved result %{public}d", result);
1009
1010 return result;
1011 }
1012
CmdGetFwmarkForNetwork(MessageParcel &data, MessageParcel &reply)1013 int32_t NetsysNativeServiceStub::CmdGetFwmarkForNetwork(MessageParcel &data, MessageParcel &reply)
1014 {
1015 MarkMaskParcel markMaskParcel = {};
1016 int32_t netId = data.ReadInt32();
1017 markMaskParcel.mark = data.ReadInt32();
1018 markMaskParcel.mask = data.ReadInt32();
1019 int32_t result = GetFwmarkForNetwork(netId, markMaskParcel);
1020 reply.WriteInt32(result);
1021 NETNATIVE_LOG_D("GetFwmarkForNetwork has recved result %{public}d", result);
1022
1023 return result;
1024 }
1025
CmdSetInterfaceConfig(MessageParcel &data, MessageParcel &reply)1026 int32_t NetsysNativeServiceStub::CmdSetInterfaceConfig(MessageParcel &data, MessageParcel &reply)
1027 {
1028 InterfaceConfigurationParcel cfg = {};
1029 cfg.ifName = data.ReadString();
1030 cfg.hwAddr = data.ReadString();
1031 cfg.ipv4Addr = data.ReadString();
1032 cfg.prefixLength = data.ReadInt32();
1033 int32_t vSize = data.ReadInt32();
1034 vSize = (vSize > MAX_FLAG_NUM) ? MAX_FLAG_NUM : vSize;
1035 std::vector<std::string> vFlags;
1036 for (int i = 0; i < vSize; i++) {
1037 vFlags.emplace_back(data.ReadString());
1038 }
1039 cfg.flags.assign(vFlags.begin(), vFlags.end());
1040 int32_t result = SetInterfaceConfig(cfg);
1041 reply.WriteInt32(result);
1042 NETNATIVE_LOG_D("SetInterfaceConfig has recved result %{public}d", result);
1043
1044 return result;
1045 }
1046
CmdGetInterfaceConfig(MessageParcel &data, MessageParcel &reply)1047 int32_t NetsysNativeServiceStub::CmdGetInterfaceConfig(MessageParcel &data, MessageParcel &reply)
1048 {
1049 NETNATIVE_LOG_D("Begin to dispatch cmd GetInterfaceConfig");
1050 InterfaceConfigurationParcel cfg = {};
1051 cfg.ifName = data.ReadString();
1052 int32_t result = GetInterfaceConfig(cfg);
1053 reply.WriteInt32(result);
1054 reply.WriteString(cfg.ifName);
1055 reply.WriteString(cfg.hwAddr);
1056 reply.WriteString(cfg.ipv4Addr);
1057 reply.WriteInt32(cfg.prefixLength);
1058 int32_t vsize = static_cast<int32_t>(cfg.flags.size());
1059 vsize = vsize > MAX_DNS_CONFIG_SIZE ? MAX_DNS_CONFIG_SIZE : vsize;
1060 reply.WriteInt32(vsize);
1061 std::vector<std::string>::iterator iter;
1062 int32_t index = 0;
1063 for (iter = cfg.flags.begin(); iter != cfg.flags.end(); ++iter) {
1064 if (++index > MAX_DNS_CONFIG_SIZE) {
1065 break;
1066 }
1067 reply.WriteString(*iter);
1068 }
1069 return result;
1070 }
1071
CmdInterfaceGetList(MessageParcel &data, MessageParcel &reply)1072 int32_t NetsysNativeServiceStub::CmdInterfaceGetList(MessageParcel &data, MessageParcel &reply)
1073 {
1074 NETNATIVE_LOG_D("Begin to dispatch cmd InterfaceGetList");
1075 std::vector<std::string> ifaces;
1076 int32_t result = InterfaceGetList(ifaces);
1077 reply.WriteInt32(result);
1078 auto vsize = static_cast<int32_t>(ifaces.size());
1079 reply.WriteInt32(vsize);
1080 std::vector<std::string>::iterator iter;
1081 for (iter = ifaces.begin(); iter != ifaces.end(); ++iter) {
1082 reply.WriteString(*iter);
1083 }
1084 return result;
1085 }
1086
CmdStartDhcpClient(MessageParcel &data, MessageParcel &reply)1087 int32_t NetsysNativeServiceStub::CmdStartDhcpClient(MessageParcel &data, MessageParcel &reply)
1088 {
1089 NETNATIVE_LOG_D("Begin to dispatch cmd CmdStartDhcpClient");
1090 std::string iface = data.ReadString();
1091 bool bIpv6 = data.ReadBool();
1092 int32_t result = StartDhcpClient(iface, bIpv6);
1093 reply.WriteInt32(result);
1094 return result;
1095 }
1096
CmdStopDhcpClient(MessageParcel &data, MessageParcel &reply)1097 int32_t NetsysNativeServiceStub::CmdStopDhcpClient(MessageParcel &data, MessageParcel &reply)
1098 {
1099 NETNATIVE_LOG_D("Begin to dispatch cmd CmdStopDhcpClient");
1100 std::string iface = data.ReadString();
1101 bool bIpv6 = data.ReadBool();
1102 int32_t result = StopDhcpClient(iface, bIpv6);
1103 reply.WriteInt32(result);
1104 return result;
1105 }
1106
CmdStartDhcpService(MessageParcel &data, MessageParcel &reply)1107 int32_t NetsysNativeServiceStub::CmdStartDhcpService(MessageParcel &data, MessageParcel &reply)
1108 {
1109 NETNATIVE_LOG_D("Begin to dispatch cmd CmdStartDhcpService");
1110 std::string iface = data.ReadString();
1111 std::string ipv4addr = data.ReadString();
1112 int32_t result = StartDhcpService(iface, ipv4addr);
1113 reply.WriteInt32(result);
1114 return result;
1115 }
1116
CmdStopDhcpService(MessageParcel &data, MessageParcel &reply)1117 int32_t NetsysNativeServiceStub::CmdStopDhcpService(MessageParcel &data, MessageParcel &reply)
1118 {
1119 NETNATIVE_LOG_D("Begin to dispatch cmd CmdStopDhcpService");
1120 std::string iface = data.ReadString();
1121 int32_t result = StopDhcpService(iface);
1122 reply.WriteInt32(result);
1123 return result;
1124 }
1125
CmdIpEnableForwarding(MessageParcel &data, MessageParcel &reply)1126 int32_t NetsysNativeServiceStub::CmdIpEnableForwarding(MessageParcel &data, MessageParcel &reply)
1127 {
1128 NETNATIVE_LOG_D("Begin to dispatch cmd CmdIpEnableForwarding");
1129 const auto &requester = data.ReadString();
1130 int32_t result = IpEnableForwarding(requester);
1131 reply.WriteInt32(result);
1132 return result;
1133 }
1134
CmdIpDisableForwarding(MessageParcel &data, MessageParcel &reply)1135 int32_t NetsysNativeServiceStub::CmdIpDisableForwarding(MessageParcel &data, MessageParcel &reply)
1136 {
1137 NETNATIVE_LOG_D("Begin to dispatch cmd CmdIpDisableForwarding");
1138 const auto &requester = data.ReadString();
1139 int32_t result = IpDisableForwarding(requester);
1140 reply.WriteInt32(result);
1141 return result;
1142 }
1143
CmdEnableNat(MessageParcel &data, MessageParcel &reply)1144 int32_t NetsysNativeServiceStub::CmdEnableNat(MessageParcel &data, MessageParcel &reply)
1145 {
1146 NETNATIVE_LOG_D("Begin to dispatch cmd CmdEnableNat");
1147 const auto &downstreamIface = data.ReadString();
1148 const auto &upstreamIface = data.ReadString();
1149 int32_t result = EnableNat(downstreamIface, upstreamIface);
1150 reply.WriteInt32(result);
1151 return result;
1152 }
1153
CmdDisableNat(MessageParcel &data, MessageParcel &reply)1154 int32_t NetsysNativeServiceStub::CmdDisableNat(MessageParcel &data, MessageParcel &reply)
1155 {
1156 NETNATIVE_LOG_D("Begin to dispatch cmd CmdDisableNat");
1157 const auto &downstreamIface = data.ReadString();
1158 const auto &upstreamIface = data.ReadString();
1159 int32_t result = DisableNat(downstreamIface, upstreamIface);
1160 reply.WriteInt32(result);
1161 return result;
1162 }
1163
CmdIpfwdAddInterfaceForward(MessageParcel &data, MessageParcel &reply)1164 int32_t NetsysNativeServiceStub::CmdIpfwdAddInterfaceForward(MessageParcel &data, MessageParcel &reply)
1165 {
1166 NETNATIVE_LOG_D("Begin to dispatch cmd CmdIpfwdAddInterfaceForward");
1167 std::string fromIface = data.ReadString();
1168 std::string toIface = data.ReadString();
1169 int32_t result = IpfwdAddInterfaceForward(fromIface, toIface);
1170 reply.WriteInt32(result);
1171 return result;
1172 }
1173
CmdIpfwdRemoveInterfaceForward(MessageParcel &data, MessageParcel &reply)1174 int32_t NetsysNativeServiceStub::CmdIpfwdRemoveInterfaceForward(MessageParcel &data, MessageParcel &reply)
1175 {
1176 NETNATIVE_LOG_D("Begin to dispatch cmd CmdIpfwdRemoveInterfaceForward");
1177 const auto &fromIface = data.ReadString();
1178 const auto &toIface = data.ReadString();
1179 int32_t result = IpfwdRemoveInterfaceForward(fromIface, toIface);
1180 reply.WriteInt32(result);
1181 return result;
1182 }
1183
CmdBandwidthEnableDataSaver(MessageParcel &data, MessageParcel &reply)1184 int32_t NetsysNativeServiceStub::CmdBandwidthEnableDataSaver(MessageParcel &data, MessageParcel &reply)
1185 {
1186 NETNATIVE_LOG_D("Begin to dispatch cmd CmdBandwidthEnableDataSaver");
1187 bool enable = data.ReadBool();
1188 int32_t result = BandwidthEnableDataSaver(enable);
1189 reply.WriteInt32(result);
1190 return result;
1191 }
1192
CmdBandwidthSetIfaceQuota(MessageParcel &data, MessageParcel &reply)1193 int32_t NetsysNativeServiceStub::CmdBandwidthSetIfaceQuota(MessageParcel &data, MessageParcel &reply)
1194 {
1195 NETNATIVE_LOG_D("Begin to dispatch cmd CmdBandwidthSetIfaceQuota");
1196 std::string ifName = data.ReadString();
1197 int64_t bytes = data.ReadInt64();
1198 int32_t result = BandwidthSetIfaceQuota(ifName, bytes);
1199 reply.WriteInt32(result);
1200 return result;
1201 }
1202
CmdBandwidthRemoveIfaceQuota(MessageParcel &data, MessageParcel &reply)1203 int32_t NetsysNativeServiceStub::CmdBandwidthRemoveIfaceQuota(MessageParcel &data, MessageParcel &reply)
1204 {
1205 NETNATIVE_LOG_D("Begin to dispatch cmd CmdBandwidthRemoveIfaceQuota");
1206 std::string ifName = data.ReadString();
1207 int32_t result = BandwidthRemoveIfaceQuota(ifName);
1208 reply.WriteInt32(result);
1209 return result;
1210 }
1211
CmdBandwidthAddDeniedList(MessageParcel &data, MessageParcel &reply)1212 int32_t NetsysNativeServiceStub::CmdBandwidthAddDeniedList(MessageParcel &data, MessageParcel &reply)
1213 {
1214 NETNATIVE_LOG_D("Begin to dispatch cmd CmdBandwidthAddDeniedList");
1215 uint32_t uid = data.ReadUint32();
1216 int32_t result = BandwidthAddDeniedList(uid);
1217 reply.WriteInt32(result);
1218 return result;
1219 }
CmdBandwidthRemoveDeniedList(MessageParcel &data, MessageParcel &reply)1220 int32_t NetsysNativeServiceStub::CmdBandwidthRemoveDeniedList(MessageParcel &data, MessageParcel &reply)
1221 {
1222 NETNATIVE_LOG_D("Begin to dispatch cmd CmdBandwidthRemoveDeniedList");
1223 uint32_t uid = data.ReadUint32();
1224 int32_t result = BandwidthRemoveDeniedList(uid);
1225 reply.WriteInt32(result);
1226 return result;
1227 }
1228
CmdBandwidthAddAllowedList(MessageParcel &data, MessageParcel &reply)1229 int32_t NetsysNativeServiceStub::CmdBandwidthAddAllowedList(MessageParcel &data, MessageParcel &reply)
1230 {
1231 NETNATIVE_LOG_D("Begin to dispatch cmd CmdBandwidthAddAllowedList");
1232 uint32_t uid = data.ReadUint32();
1233 int32_t result = BandwidthAddAllowedList(uid);
1234 reply.WriteInt32(result);
1235 return result;
1236 }
1237
CmdBandwidthRemoveAllowedList(MessageParcel &data, MessageParcel &reply)1238 int32_t NetsysNativeServiceStub::CmdBandwidthRemoveAllowedList(MessageParcel &data, MessageParcel &reply)
1239 {
1240 NETNATIVE_LOG_D("Begin to dispatch cmd CmdBandwidthRemoveAllowedList");
1241 uint32_t uid = data.ReadUint32();
1242 int32_t result = BandwidthRemoveAllowedList(uid);
1243 reply.WriteInt32(result);
1244 return result;
1245 }
1246
CmdFirewallSetUidsAllowedListChain(MessageParcel &data, MessageParcel &reply)1247 int32_t NetsysNativeServiceStub::CmdFirewallSetUidsAllowedListChain(MessageParcel &data, MessageParcel &reply)
1248 {
1249 NETNATIVE_LOG_D("Begin to dispatch cmd CmdFirewallSetUidsAllowedListChain");
1250 uint32_t chain = data.ReadUint32();
1251 std::vector<uint32_t> uids;
1252 uint32_t uidSize = data.ReadUint32();
1253 uidSize = (uidSize > UIDS_LIST_MAX_SIZE) ? UIDS_LIST_MAX_SIZE : uidSize;
1254 for (uint32_t i = 0; i < uidSize; i++) {
1255 uint32_t uid = data.ReadUint32();
1256 uids.push_back(uid);
1257 }
1258 int32_t result = FirewallSetUidsAllowedListChain(chain, uids);
1259 reply.WriteInt32(result);
1260 return result;
1261 }
1262
CmdFirewallSetUidsDeniedListChain(MessageParcel &data, MessageParcel &reply)1263 int32_t NetsysNativeServiceStub::CmdFirewallSetUidsDeniedListChain(MessageParcel &data, MessageParcel &reply)
1264 {
1265 NETNATIVE_LOG_D("Begin to dispatch cmd CmdFirewallSetUidsDeniedListChain");
1266 uint32_t chain = data.ReadUint32();
1267 std::vector<uint32_t> uids;
1268 uint32_t uidSize = data.ReadUint32();
1269 uidSize = (uidSize > UIDS_LIST_MAX_SIZE) ? UIDS_LIST_MAX_SIZE : uidSize;
1270 for (uint32_t i = 0; i < uidSize; i++) {
1271 uint32_t uid = data.ReadUint32();
1272 uids.push_back(uid);
1273 }
1274 int32_t result = FirewallSetUidsDeniedListChain(chain, uids);
1275 reply.WriteInt32(result);
1276 return result;
1277 }
1278
CmdFirewallEnableChain(MessageParcel &data, MessageParcel &reply)1279 int32_t NetsysNativeServiceStub::CmdFirewallEnableChain(MessageParcel &data, MessageParcel &reply)
1280 {
1281 NETNATIVE_LOG_D("Begin to dispatch cmd CmdFirewallEnableChain");
1282 uint32_t chain = data.ReadUint32();
1283 bool enable = data.ReadBool();
1284 int32_t result = FirewallEnableChain(chain, enable);
1285 reply.WriteInt32(result);
1286 return result;
1287 }
1288
CmdFirewallSetUidRule(MessageParcel &data, MessageParcel &reply)1289 int32_t NetsysNativeServiceStub::CmdFirewallSetUidRule(MessageParcel &data, MessageParcel &reply)
1290 {
1291 NETNATIVE_LOG_D("Begin to dispatch cmd CmdFirewallSetUidRule");
1292 uint32_t chain = (unsigned)data.ReadUint32();
1293 std::vector<uint32_t> uids;
1294 data.ReadUInt32Vector(&uids);
1295 uint32_t firewallRule = (unsigned)data.ReadInt32();
1296 int32_t result = FirewallSetUidRule(chain, uids, firewallRule);
1297 reply.WriteInt32(result);
1298 return result;
1299 }
1300
CmdShareDnsSet(MessageParcel &data, MessageParcel &reply)1301 int32_t NetsysNativeServiceStub::CmdShareDnsSet(MessageParcel &data, MessageParcel &reply)
1302 {
1303 uint16_t netId = 0;
1304 data.ReadUint16(netId);
1305 int32_t result = ShareDnsSet(netId);
1306 reply.WriteInt32(result);
1307 NETNATIVE_LOG_D("ShareDnsSet has received result %{public}d", result);
1308
1309 return result;
1310 }
1311
CmdStartDnsProxyListen(MessageParcel &data, MessageParcel &reply)1312 int32_t NetsysNativeServiceStub::CmdStartDnsProxyListen(MessageParcel &data, MessageParcel &reply)
1313 {
1314 int32_t result = StartDnsProxyListen();
1315 reply.WriteInt32(result);
1316 NETNATIVE_LOG_D("StartDnsProxyListen has recved result %{public}d", result);
1317
1318 return result;
1319 }
1320
CmdStopDnsProxyListen(MessageParcel &data, MessageParcel &reply)1321 int32_t NetsysNativeServiceStub::CmdStopDnsProxyListen(MessageParcel &data, MessageParcel &reply)
1322 {
1323 int32_t result = StopDnsProxyListen();
1324 reply.WriteInt32(result);
1325 NETNATIVE_LOG_D("StopDnsProxyListen has recved result %{public}d", result);
1326
1327 return result;
1328 }
1329
CmdGetNetworkSharingTraffic(MessageParcel &data, MessageParcel &reply)1330 int32_t NetsysNativeServiceStub::CmdGetNetworkSharingTraffic(MessageParcel &data, MessageParcel &reply)
1331 {
1332 NETNATIVE_LOG_D("Begin to dispatch cmd GetNetworkSharingTraffic");
1333 std::string downIface = data.ReadString();
1334 std::string upIface = data.ReadString();
1335 NetworkSharingTraffic traffic;
1336 int32_t result = GetNetworkSharingTraffic(downIface, upIface, traffic);
1337 reply.WriteInt32(result);
1338 reply.WriteInt64(traffic.receive);
1339 reply.WriteInt64(traffic.send);
1340 reply.WriteInt64(traffic.all);
1341
1342 return result;
1343 }
1344
CmdGetTotalStats(MessageParcel &data, MessageParcel &reply)1345 int32_t NetsysNativeServiceStub::CmdGetTotalStats(MessageParcel &data, MessageParcel &reply)
1346 {
1347 uint32_t type = data.ReadUint32();
1348 uint64_t stats = 0;
1349 int32_t result = GetTotalStats(stats, type);
1350 if (!reply.WriteInt32(result)) {
1351 NETNATIVE_LOGE("Write parcel failed");
1352 return ERR_FLATTEN_OBJECT;
1353 }
1354 if (!reply.WriteUint64(stats)) {
1355 NETNATIVE_LOGE("Write parcel failed");
1356 return ERR_FLATTEN_OBJECT;
1357 }
1358 return result;
1359 }
1360
CmdGetUidStats(MessageParcel &data, MessageParcel &reply)1361 int32_t NetsysNativeServiceStub::CmdGetUidStats(MessageParcel &data, MessageParcel &reply)
1362 {
1363 uint32_t type = data.ReadUint32();
1364 uint32_t uId = data.ReadUint32();
1365 uint64_t stats = 0;
1366 int32_t result = GetUidStats(stats, type, uId);
1367 if (!reply.WriteInt32(result)) {
1368 NETNATIVE_LOGE("Write parcel failed");
1369 return ERR_FLATTEN_OBJECT;
1370 }
1371 if (!reply.WriteUint64(stats)) {
1372 NETNATIVE_LOGE("Write parcel failed");
1373 return ERR_FLATTEN_OBJECT;
1374 }
1375 return result;
1376 }
1377
CmdGetIfaceStats(MessageParcel &data, MessageParcel &reply)1378 int32_t NetsysNativeServiceStub::CmdGetIfaceStats(MessageParcel &data, MessageParcel &reply)
1379 {
1380 uint32_t type = data.ReadUint32();
1381 std::string interfaceName = data.ReadString();
1382 uint64_t stats = 0;
1383 int32_t result = GetIfaceStats(stats, type, interfaceName);
1384 if (!reply.WriteInt32(result)) {
1385 NETNATIVE_LOGE("Write parcel failed");
1386 return ERR_FLATTEN_OBJECT;
1387 }
1388 if (!reply.WriteUint64(stats)) {
1389 NETNATIVE_LOGE("Write parcel failed");
1390 return ERR_FLATTEN_OBJECT;
1391 }
1392 return result;
1393 }
1394
CmdGetAllSimStatsInfo(MessageParcel &data, MessageParcel &reply)1395 int32_t NetsysNativeServiceStub::CmdGetAllSimStatsInfo(MessageParcel &data, MessageParcel &reply)
1396 {
1397 std::vector<OHOS::NetManagerStandard::NetStatsInfo> stats;
1398 int32_t result = GetAllSimStatsInfo(stats);
1399 if (!reply.WriteInt32(result)) {
1400 NETNATIVE_LOGE("Write parcel failed");
1401 return ERR_FLATTEN_OBJECT;
1402 }
1403 if (!OHOS::NetManagerStandard::NetStatsInfo::Marshalling(reply, stats)) {
1404 NETNATIVE_LOGE("Read stats info failed");
1405 return ERR_FLATTEN_OBJECT;
1406 }
1407 return result;
1408 }
1409
CmdDeleteSimStatsInfo(MessageParcel &data, MessageParcel &reply)1410 int32_t NetsysNativeServiceStub::CmdDeleteSimStatsInfo(MessageParcel &data, MessageParcel &reply)
1411 {
1412 uint32_t uid = data.ReadUint32();
1413 int32_t ret = DeleteSimStatsInfo(uid);
1414 NETNATIVE_LOG_D("DeleteSimStatsInfo uid[%{public}d] ret[%{public}d]", uid, ret);
1415 if (!reply.WriteInt32(ret)) {
1416 NETNATIVE_LOGE("Write parcel failed");
1417 return ERR_FLATTEN_OBJECT;
1418 }
1419 return NetManagerStandard::NETMANAGER_SUCCESS;
1420 }
1421
CmdGetAllStatsInfo(MessageParcel &data, MessageParcel &reply)1422 int32_t NetsysNativeServiceStub::CmdGetAllStatsInfo(MessageParcel &data, MessageParcel &reply)
1423 {
1424 std::vector<OHOS::NetManagerStandard::NetStatsInfo> stats;
1425 int32_t result = GetAllStatsInfo(stats);
1426 if (!reply.WriteInt32(result)) {
1427 NETNATIVE_LOGE("Write parcel failed");
1428 return ERR_FLATTEN_OBJECT;
1429 }
1430 if (!OHOS::NetManagerStandard::NetStatsInfo::Marshalling(reply, stats)) {
1431 NETNATIVE_LOGE("Read stats info failed");
1432 return ERR_FLATTEN_OBJECT;
1433 }
1434 return result;
1435 }
1436
CmdDeleteStatsInfo(MessageParcel &data, MessageParcel &reply)1437 int32_t NetsysNativeServiceStub::CmdDeleteStatsInfo(MessageParcel &data, MessageParcel &reply)
1438 {
1439 uint32_t uid = data.ReadUint32();
1440 int32_t ret = DeleteStatsInfo(uid);
1441 NETNATIVE_LOG_D("DeleteStatsInfo uid[%{public}d] ret[%{public}d]", uid, ret);
1442 if (!reply.WriteInt32(ret)) {
1443 NETNATIVE_LOGE("Write parcel failed");
1444 return ERR_FLATTEN_OBJECT;
1445 }
1446 return NetManagerStandard::NETMANAGER_SUCCESS;
1447 }
1448
CmdSetIptablesCommandForRes(MessageParcel &data, MessageParcel &reply)1449 int32_t NetsysNativeServiceStub::CmdSetIptablesCommandForRes(MessageParcel &data, MessageParcel &reply)
1450 {
1451 if (!NetManagerStandard::NetManagerPermission::CheckNetSysInternalPermission(
1452 NetManagerStandard::Permission::NETSYS_INTERNAL)) {
1453 NETNATIVE_LOGE("CmdSetIptablesCommandForRes CheckNetSysInternalPermission failed");
1454 return NETMANAGER_ERR_PERMISSION_DENIED;
1455 }
1456 std::string cmd = data.ReadString();
1457 IptablesType ipType = static_cast<IptablesType>(data.ReadUint32());
1458 std::string respond;
1459 int32_t result = SetIptablesCommandForRes(cmd, respond, ipType);
1460 if (!reply.WriteInt32(result)) {
1461 NETNATIVE_LOGE("Write CmdSetIptablesCommandForRes result failed");
1462 return ERR_FLATTEN_OBJECT;
1463 }
1464 if (!reply.WriteString(respond)) {
1465 NETNATIVE_LOGE("Write CmdSetIptablesCommandForRes respond failed");
1466 return ERR_FLATTEN_OBJECT;
1467 }
1468 return NetManagerStandard::NETMANAGER_SUCCESS;
1469 }
1470
CmdNetDiagPingHost(MessageParcel &data, MessageParcel &reply)1471 int32_t NetsysNativeServiceStub::CmdNetDiagPingHost(MessageParcel &data, MessageParcel &reply)
1472 {
1473 NetDiagPingOption pingOption;
1474 if (!NetDiagPingOption::Unmarshalling(data, pingOption)) {
1475 NETNATIVE_LOGE("Unmarshalling failed.");
1476 return IPC_STUB_ERR;
1477 }
1478
1479 sptr<IRemoteObject> remote = data.ReadRemoteObject();
1480 if (remote == nullptr) {
1481 NETNATIVE_LOGE("remote is nullptr.");
1482 return IPC_STUB_ERR;
1483 }
1484
1485 sptr<INetDiagCallback> callback = iface_cast<INetDiagCallback>(remote);
1486 int32_t result = NetDiagPingHost(pingOption, callback);
1487 if (!reply.WriteInt32(result)) {
1488 NETNATIVE_LOGE("Write result failed");
1489 return ERR_FLATTEN_OBJECT;
1490 }
1491 return result;
1492 }
1493
CmdNetDiagGetRouteTable(MessageParcel &data, MessageParcel &reply)1494 int32_t NetsysNativeServiceStub::CmdNetDiagGetRouteTable(MessageParcel &data, MessageParcel &reply)
1495 {
1496 std::list<NetDiagRouteTable> routeTables;
1497 int32_t result = NetDiagGetRouteTable(routeTables);
1498 if (!reply.WriteInt32(result)) {
1499 NETNATIVE_LOGE("Write result failed");
1500 return ERR_FLATTEN_OBJECT;
1501 }
1502 if (result == NetManagerStandard::NETMANAGER_SUCCESS) {
1503 if (!reply.WriteUint32(
1504 static_cast<uint32_t>(std::min(MAX_ROUTE_TABLE_SIZE, static_cast<uint32_t>(routeTables.size()))))) {
1505 NETNATIVE_LOGE("Write uint32 failed");
1506 return ERR_FLATTEN_OBJECT;
1507 }
1508 uint32_t count = 0;
1509 for (const auto &routeTable : routeTables) {
1510 if (!routeTable.Marshalling(reply)) {
1511 NETNATIVE_LOGE("NetDiagRouteTable marshalling failed");
1512 return ERR_FLATTEN_OBJECT;
1513 }
1514 ++count;
1515 if (count >= MAX_ROUTE_TABLE_SIZE) {
1516 break;
1517 }
1518 }
1519 }
1520 return result;
1521 }
1522
CmdNetDiagGetSocketsInfo(MessageParcel &data, MessageParcel &reply)1523 int32_t NetsysNativeServiceStub::CmdNetDiagGetSocketsInfo(MessageParcel &data, MessageParcel &reply)
1524 {
1525 uint8_t socketType = 0;
1526 if (!data.ReadUint8(socketType)) {
1527 NETNATIVE_LOGE("Read uint8 failed");
1528 return ERR_FLATTEN_OBJECT;
1529 }
1530 NetDiagSocketsInfo socketsInfo;
1531 int32_t result = NetDiagGetSocketsInfo(static_cast<NetDiagProtocolType>(socketType), socketsInfo);
1532 if (!reply.WriteInt32(result)) {
1533 NETNATIVE_LOGE("Write result failed");
1534 return ERR_FLATTEN_OBJECT;
1535 }
1536 if (result == NetManagerStandard::NETMANAGER_SUCCESS) {
1537 if (!socketsInfo.Marshalling(reply)) {
1538 NETNATIVE_LOGE("NetDiagSocketsInfo marshalling failed.");
1539 return ERR_FLATTEN_OBJECT;
1540 }
1541 }
1542 return result;
1543 }
1544
CmdNetDiagGetInterfaceConfig(MessageParcel &data, MessageParcel &reply)1545 int32_t NetsysNativeServiceStub::CmdNetDiagGetInterfaceConfig(MessageParcel &data, MessageParcel &reply)
1546 {
1547 std::string ifaceName;
1548 if (!data.ReadString(ifaceName)) {
1549 NETNATIVE_LOGE("Read string failed");
1550 return ERR_FLATTEN_OBJECT;
1551 }
1552 std::list<NetDiagIfaceConfig> configList;
1553 int32_t result = NetDiagGetInterfaceConfig(configList, ifaceName);
1554 if (!reply.WriteInt32(result)) {
1555 NETNATIVE_LOGE("Write result failed");
1556 return ERR_FLATTEN_OBJECT;
1557 }
1558 if (result == NetManagerStandard::NETMANAGER_SUCCESS) {
1559 if (!reply.WriteUint32(
1560 static_cast<uint32_t>(std::min(MAX_CONFIG_LIST_SIZE, static_cast<uint32_t>(configList.size()))))) {
1561 NETNATIVE_LOGE("Write uint32 failed");
1562 return ERR_FLATTEN_OBJECT;
1563 }
1564 uint32_t count = 0;
1565 for (const auto &config : configList) {
1566 if (!config.Marshalling(reply)) {
1567 NETNATIVE_LOGE("NetDiagIfaceConfig marshalling failed");
1568 return ERR_FLATTEN_OBJECT;
1569 }
1570 ++count;
1571 if (count >= MAX_CONFIG_LIST_SIZE) {
1572 break;
1573 }
1574 }
1575 }
1576 return result;
1577 }
1578
CmdNetDiagUpdateInterfaceConfig(MessageParcel &data, MessageParcel &reply)1579 int32_t NetsysNativeServiceStub::CmdNetDiagUpdateInterfaceConfig(MessageParcel &data, MessageParcel &reply)
1580 {
1581 NetDiagIfaceConfig config;
1582 if (!NetDiagIfaceConfig::Unmarshalling(data, config)) {
1583 NETNATIVE_LOGE("NetDiagIfaceConfig unmarshalling failed.");
1584 return IPC_STUB_ERR;
1585 }
1586
1587 std::string ifaceName;
1588 if (!data.ReadString(ifaceName)) {
1589 NETNATIVE_LOGE("Read string failed");
1590 return ERR_FLATTEN_OBJECT;
1591 }
1592
1593 bool add = false;
1594 if (!data.ReadBool(add)) {
1595 NETNATIVE_LOGE("Read bool failed");
1596 return ERR_FLATTEN_OBJECT;
1597 }
1598
1599 int32_t result = NetDiagUpdateInterfaceConfig(config, ifaceName, add);
1600 if (!reply.WriteInt32(result)) {
1601 NETNATIVE_LOGE("Write result failed");
1602 return ERR_FLATTEN_OBJECT;
1603 }
1604 return result;
1605 }
1606
CmdNetDiagSetInterfaceActiveState(MessageParcel &data, MessageParcel &reply)1607 int32_t NetsysNativeServiceStub::CmdNetDiagSetInterfaceActiveState(MessageParcel &data, MessageParcel &reply)
1608 {
1609 std::string ifaceName;
1610 if (!data.ReadString(ifaceName)) {
1611 NETNATIVE_LOGE("Read string failed");
1612 return ERR_FLATTEN_OBJECT;
1613 }
1614
1615 bool up = false;
1616 if (!data.ReadBool(up)) {
1617 NETNATIVE_LOGE("Read bool failed");
1618 return ERR_FLATTEN_OBJECT;
1619 }
1620
1621 int32_t result = NetDiagSetInterfaceActiveState(ifaceName, up);
1622 if (!reply.WriteInt32(result)) {
1623 NETNATIVE_LOGE("Write result failed");
1624 return ERR_FLATTEN_OBJECT;
1625 }
1626 return result;
1627 }
1628
CmdAddStaticArp(MessageParcel &data, MessageParcel &reply)1629 int32_t NetsysNativeServiceStub::CmdAddStaticArp(MessageParcel &data, MessageParcel &reply)
1630 {
1631 std::string ipAddr = "";
1632 if (!data.ReadString(ipAddr)) {
1633 NETNATIVE_LOGE("Read string failed");
1634 return ERR_FLATTEN_OBJECT;
1635 }
1636
1637 std::string macAddr = "";
1638 if (!data.ReadString(macAddr)) {
1639 NETNATIVE_LOGE("Read string failed");
1640 return ERR_FLATTEN_OBJECT;
1641 }
1642
1643 std::string ifName = "";
1644 if (!data.ReadString(ifName)) {
1645 NETNATIVE_LOGE("Read string failed");
1646 return ERR_FLATTEN_OBJECT;
1647 }
1648
1649 int32_t result = AddStaticArp(ipAddr, macAddr, ifName);
1650 if (!reply.WriteInt32(result)) {
1651 NETNATIVE_LOGE("Write result failed");
1652 return ERR_FLATTEN_OBJECT;
1653 }
1654 NETNATIVE_LOG_D("CmdAddStaticArp has recved result %{public}d", result);
1655
1656 return result;
1657 }
1658
CmdDelStaticArp(MessageParcel &data, MessageParcel &reply)1659 int32_t NetsysNativeServiceStub::CmdDelStaticArp(MessageParcel &data, MessageParcel &reply)
1660 {
1661 std::string ipAddr = "";
1662 if (!data.ReadString(ipAddr)) {
1663 NETNATIVE_LOGE("Read string failed");
1664 return ERR_FLATTEN_OBJECT;
1665 }
1666
1667 std::string macAddr = "";
1668 if (!data.ReadString(macAddr)) {
1669 NETNATIVE_LOGE("Read string failed");
1670 return ERR_FLATTEN_OBJECT;
1671 }
1672
1673 std::string ifName = "";
1674 if (!data.ReadString(ifName)) {
1675 NETNATIVE_LOGE("Read string failed");
1676 return ERR_FLATTEN_OBJECT;
1677 }
1678
1679 int32_t result = DelStaticArp(ipAddr, macAddr, ifName);
1680 if (!reply.WriteInt32(result)) {
1681 NETNATIVE_LOGE("Write result failed");
1682 return ERR_FLATTEN_OBJECT;
1683 }
1684 NETNATIVE_LOG_D("CmdDelStaticArp has recved result %{public}d", result);
1685
1686 return result;
1687 }
1688
CmdRegisterDnsResultListener(MessageParcel &data, MessageParcel &reply)1689 int32_t NetsysNativeServiceStub::CmdRegisterDnsResultListener(MessageParcel &data, MessageParcel &reply)
1690 {
1691 int32_t result = NETMANAGER_SUCCESS;
1692 sptr<IRemoteObject> remote = data.ReadRemoteObject();
1693 if (remote == nullptr) {
1694 NETNATIVE_LOGE("Callback ptr is nullptr.");
1695 result = IPC_STUB_ERR;
1696 reply.WriteInt32(result);
1697 return result;
1698 }
1699
1700 sptr<INetDnsResultCallback> callback = iface_cast<INetDnsResultCallback>(remote);
1701 if (callback == nullptr) {
1702 result = ERR_FLATTEN_OBJECT;
1703 reply.WriteInt32(result);
1704 return result;
1705 }
1706
1707 uint32_t delay;
1708 if (!data.ReadUint32(delay)) {
1709 NETNATIVE_LOGE("Read uint32 failed");
1710 return ERR_FLATTEN_OBJECT;
1711 }
1712
1713 result = RegisterDnsResultCallback(callback, delay);
1714 reply.WriteInt32(result);
1715 return result;
1716 }
1717
CmdUnregisterDnsResultListener(MessageParcel &data, MessageParcel &reply)1718 int32_t NetsysNativeServiceStub::CmdUnregisterDnsResultListener(MessageParcel &data, MessageParcel &reply)
1719 {
1720 int32_t result = NETMANAGER_SUCCESS;
1721 sptr<IRemoteObject> remote = data.ReadRemoteObject();
1722 if (remote == nullptr) {
1723 NETNATIVE_LOGE("Callback ptr is nullptr.");
1724 result = IPC_STUB_ERR;
1725 reply.WriteInt32(result);
1726 return result;
1727 }
1728
1729 sptr<INetDnsResultCallback> callback = iface_cast<INetDnsResultCallback>(remote);
1730 if (callback == nullptr) {
1731 result = ERR_FLATTEN_OBJECT;
1732 reply.WriteInt32(result);
1733 return result;
1734 }
1735
1736 result = UnregisterDnsResultCallback(callback);
1737 reply.WriteInt32(result);
1738 return result;
1739 }
1740
CmdRegisterDnsHealthListener(MessageParcel &data, MessageParcel &reply)1741 int32_t NetsysNativeServiceStub::CmdRegisterDnsHealthListener(MessageParcel &data, MessageParcel &reply)
1742 {
1743 int32_t result = NETMANAGER_SUCCESS;
1744 sptr<IRemoteObject> remote = data.ReadRemoteObject();
1745 if (remote == nullptr) {
1746 NETNATIVE_LOGE("Callback ptr is nullptr.");
1747 result = IPC_STUB_ERR;
1748 reply.WriteInt32(result);
1749 return result;
1750 }
1751
1752 sptr<INetDnsHealthCallback> callback = iface_cast<INetDnsHealthCallback>(remote);
1753 if (callback == nullptr) {
1754 result = ERR_FLATTEN_OBJECT;
1755 reply.WriteInt32(result);
1756 return result;
1757 }
1758
1759 result = RegisterDnsHealthCallback(callback);
1760 reply.WriteInt32(result);
1761 return result;
1762 }
1763
CmdUnregisterDnsHealthListener(MessageParcel &data, MessageParcel &reply)1764 int32_t NetsysNativeServiceStub::CmdUnregisterDnsHealthListener(MessageParcel &data, MessageParcel &reply)
1765 {
1766 int32_t result = NETMANAGER_SUCCESS;
1767 sptr<IRemoteObject> remote = data.ReadRemoteObject();
1768 if (remote == nullptr) {
1769 NETNATIVE_LOGE("Callback ptr is nullptr.");
1770 result = IPC_STUB_ERR;
1771 reply.WriteInt32(result);
1772 return result;
1773 }
1774
1775 sptr<INetDnsHealthCallback> callback = iface_cast<INetDnsHealthCallback>(remote);
1776 if (callback == nullptr) {
1777 result = ERR_FLATTEN_OBJECT;
1778 reply.WriteInt32(result);
1779 return result;
1780 }
1781
1782 result = UnregisterDnsHealthCallback(callback);
1783 reply.WriteInt32(result);
1784 return result;
1785 }
1786
CmdGetCookieStats(MessageParcel &data, MessageParcel &reply)1787 int32_t NetsysNativeServiceStub::CmdGetCookieStats(MessageParcel &data, MessageParcel &reply)
1788 {
1789 uint32_t type = 0;
1790 if (!data.ReadUint32(type)) {
1791 NETNATIVE_LOGE("Read uint32 failed");
1792 return ERR_FLATTEN_OBJECT;
1793 }
1794
1795 uint64_t cookie = 0;
1796 if (!data.ReadUint64(cookie)) {
1797 NETNATIVE_LOGE("Read uint64 failed");
1798 return ERR_FLATTEN_OBJECT;
1799 }
1800
1801 uint64_t stats = 0;
1802 int32_t result = GetCookieStats(stats, type, cookie);
1803 if (!reply.WriteInt32(result)) {
1804 NETNATIVE_LOGE("Write parcel failed");
1805 return ERR_FLATTEN_OBJECT;
1806 }
1807 if (!reply.WriteUint64(stats)) {
1808 NETNATIVE_LOGE("Write parcel failed");
1809 return ERR_FLATTEN_OBJECT;
1810 }
1811 return result;
1812 }
1813
CmdGetNetworkSharingType(MessageParcel &data, MessageParcel &reply)1814 int32_t NetsysNativeServiceStub::CmdGetNetworkSharingType(MessageParcel &data, MessageParcel &reply)
1815 {
1816 std::set<uint32_t> sharingTypeIsOn;
1817 int32_t ret = GetNetworkSharingType(sharingTypeIsOn);
1818 if (!reply.WriteInt32(ret)) {
1819 NETNATIVE_LOGE("Write parcel failed");
1820 return ERR_FLATTEN_OBJECT;
1821 }
1822 if (!reply.WriteUint32(sharingTypeIsOn.size())) {
1823 NETNATIVE_LOGE("Write parcel failed");
1824 return ERR_FLATTEN_OBJECT;
1825 }
1826 for (auto mem : sharingTypeIsOn) {
1827 if (!reply.WriteUint32(mem)) {
1828 NETNATIVE_LOGE("Write parcel failed");
1829 return ERR_FLATTEN_OBJECT;
1830 }
1831 }
1832
1833 return ret;
1834 }
1835
CmdUpdateNetworkSharingType(MessageParcel &data, MessageParcel &reply)1836 int32_t NetsysNativeServiceStub::CmdUpdateNetworkSharingType(MessageParcel &data, MessageParcel &reply)
1837 {
1838 uint32_t type = ERR_NONE;
1839 if (!data.ReadUint32(type)) {
1840 NETNATIVE_LOGE("Read uint32 failed");
1841 return ERR_FLATTEN_OBJECT;
1842 }
1843 if (type < ERR_NONE) {
1844 NETNATIVE_LOGE("type parameter invalid");
1845 return ERR_INVALID_DATA;
1846 }
1847
1848 bool isOpen = false;
1849 if (!data.ReadBool(isOpen)) {
1850 NETNATIVE_LOGE("Read bool failed");
1851 return ERR_FLATTEN_OBJECT;
1852 }
1853
1854 int32_t ret = UpdateNetworkSharingType(type, isOpen);
1855 if (!reply.WriteInt32(ret)) {
1856 NETNATIVE_LOGE("Write parcel failed");
1857 return ERR_FLATTEN_OBJECT;
1858 }
1859
1860 return ret;
1861 }
1862
1863 #ifdef FEATURE_NET_FIREWALL_ENABLE
CmdSetFirewallRules(MessageParcel &data, MessageParcel &reply)1864 int32_t NetsysNativeServiceStub::CmdSetFirewallRules(MessageParcel &data, MessageParcel &reply)
1865 {
1866 int32_t type = 0;
1867 if (!data.ReadInt32(type)) {
1868 NETNATIVE_LOGE("Read rule type failed");
1869 return ERR_FLATTEN_OBJECT;
1870 }
1871 NetFirewallRuleType ruleType = static_cast<NetFirewallRuleType>(type);
1872 uint32_t size = 0;
1873 if (!data.ReadUint32(size)) {
1874 NETNATIVE_LOGE("Read size failed");
1875 return ERR_FLATTEN_OBJECT;
1876 }
1877 NETNATIVE_LOGI("NetsysNativeServiceStub::CmdSetFirewallRules ruleType=%{public}d, size=%{public}d", ruleType, size);
1878 uint32_t maxSize =
1879 ruleType == NetFirewallRuleType::RULE_IP ? FIREWALL_IPC_IP_RULE_PAGE_SIZE : FIREWALL_RULE_SIZE_MAX;
1880 if (size > maxSize) {
1881 return FIREWALL_ERR_EXCEED_MAX_IP;
1882 }
1883 bool isFinish = false;
1884 if (!data.ReadBool(isFinish)) {
1885 NETNATIVE_LOGE("Read isFinish failed");
1886 return ERR_FLATTEN_OBJECT;
1887 }
1888 std::vector<sptr<NetFirewallBaseRule>> ruleList;
1889 for (uint32_t i = 0; i < size; i++) {
1890 sptr<NetFirewallBaseRule> rule = nullptr;
1891 if (ruleType == NetFirewallRuleType::RULE_IP) {
1892 rule = NetFirewallIpRule::Unmarshalling(data);
1893 } else if (ruleType == NetFirewallRuleType::RULE_DOMAIN) {
1894 rule = NetFirewallDomainRule::Unmarshalling(data);
1895 } else if (ruleType == NetFirewallRuleType::RULE_DNS) {
1896 rule = NetFirewallDnsRule::Unmarshalling(data);
1897 }
1898 if (rule != nullptr) {
1899 ruleList.emplace_back(std::move(rule));
1900 }
1901 }
1902 return SetFirewallRules(ruleType, ruleList, isFinish);
1903 }
1904
CmdSetFirewallDefaultAction(MessageParcel &data, MessageParcel &reply)1905 int32_t NetsysNativeServiceStub::CmdSetFirewallDefaultAction(MessageParcel &data, MessageParcel &reply)
1906 {
1907 NETNATIVE_LOGI("NetsysNativeServiceStub::CmdSetFirewallDefaultAction");
1908 int32_t inDefault = 0;
1909 if (!data.ReadInt32(inDefault)) {
1910 NETNATIVE_LOGE("Read inDefault failed");
1911 return ERR_FLATTEN_OBJECT;
1912 }
1913 int32_t outDefault = 0;
1914 if (!data.ReadInt32(outDefault)) {
1915 NETNATIVE_LOGE("Read outDefault failed");
1916 return ERR_FLATTEN_OBJECT;
1917 }
1918 return SetFirewallDefaultAction(static_cast<FirewallRuleAction>(inDefault),
1919 static_cast<FirewallRuleAction>(outDefault));
1920 }
1921
CmdSetFirewallCurrentUserId(MessageParcel &data, MessageParcel &reply)1922 int32_t NetsysNativeServiceStub::CmdSetFirewallCurrentUserId(MessageParcel &data, MessageParcel &reply)
1923 {
1924 NETNATIVE_LOGI("NetsysNativeServiceStub::CmdSetFirewallCurrentUserId");
1925 int32_t userId = 0;
1926 if (!data.ReadInt32(userId)) {
1927 NETNATIVE_LOGE("Read userId failed");
1928 return ERR_FLATTEN_OBJECT;
1929 }
1930 return SetFirewallCurrentUserId(userId);
1931 }
1932
CmdClearFirewallRules(MessageParcel &data, MessageParcel &reply)1933 int32_t NetsysNativeServiceStub::CmdClearFirewallRules(MessageParcel &data, MessageParcel &reply)
1934 {
1935 NETNATIVE_LOGI("NetsysNativeServiceStub::CmdClearFirewallRules");
1936 int32_t type = 0;
1937 if (!data.ReadInt32(type)) {
1938 NETNATIVE_LOGE("Read clear type failed");
1939 return ERR_FLATTEN_OBJECT;
1940 }
1941
1942 NetFirewallRuleType clearType = static_cast<NetFirewallRuleType>(type);
1943 return ClearFirewallRules(clearType);
1944 }
1945
CmdRegisterNetFirewallCallback(MessageParcel &data, MessageParcel &reply)1946 int32_t NetsysNativeServiceStub::CmdRegisterNetFirewallCallback(MessageParcel &data, MessageParcel &reply)
1947 {
1948 int32_t result = NETMANAGER_SUCCESS;
1949 sptr<IRemoteObject> remote = data.ReadRemoteObject();
1950 if (remote == nullptr) {
1951 NETNATIVE_LOGE("Callback ptr is nullptr.");
1952 result = IPC_STUB_ERR;
1953 return result;
1954 }
1955
1956 sptr<INetFirewallCallback> callback = iface_cast<INetFirewallCallback>(remote);
1957 if (callback == nullptr) {
1958 result = ERR_FLATTEN_OBJECT;
1959 return result;
1960 }
1961
1962 return RegisterNetFirewallCallback(callback);
1963 }
1964
CmdUnRegisterNetFirewallCallback(MessageParcel &data, MessageParcel &reply)1965 int32_t NetsysNativeServiceStub::CmdUnRegisterNetFirewallCallback(MessageParcel &data, MessageParcel &reply)
1966 {
1967 int32_t result = NETMANAGER_SUCCESS;
1968 sptr<IRemoteObject> remote = data.ReadRemoteObject();
1969 if (remote == nullptr) {
1970 NETNATIVE_LOGE("Callback ptr is nullptr.");
1971 result = IPC_STUB_ERR;
1972 return result;
1973 }
1974
1975 sptr<INetFirewallCallback> callback = iface_cast<INetFirewallCallback>(remote);
1976 if (callback == nullptr) {
1977 result = ERR_FLATTEN_OBJECT;
1978 return result;
1979 }
1980
1981 return UnRegisterNetFirewallCallback(callback);
1982 }
1983 #endif
1984
CmdSetIpv6PrivacyExtensions(MessageParcel &data, MessageParcel &reply)1985 int32_t NetsysNativeServiceStub::CmdSetIpv6PrivacyExtensions(MessageParcel &data, MessageParcel &reply)
1986 {
1987 std::string interfaceName = data.ReadString();
1988 int32_t on = data.ReadInt32();
1989
1990 int32_t result = SetIpv6PrivacyExtensions(interfaceName, on);
1991 reply.WriteInt32(result);
1992 NETNATIVE_LOGI("SetIpv6PrivacyExtensions has recved result %{public}d", result);
1993
1994 return result;
1995 }
1996
CmdSetIpv6Enable(MessageParcel &data, MessageParcel &reply)1997 int32_t NetsysNativeServiceStub::CmdSetIpv6Enable(MessageParcel &data, MessageParcel &reply)
1998 {
1999 std::string interfaceName = data.ReadString();
2000 int32_t on = data.ReadInt32();
2001
2002 int32_t result = SetEnableIpv6(interfaceName, on);
2003 reply.WriteInt32(result);
2004 NETNATIVE_LOGI("SetIpv6Enable has recved result %{public}d", result);
2005
2006 return result;
2007 }
2008
CmdSetNetworkAccessPolicy(MessageParcel &data, MessageParcel &reply)2009 int32_t NetsysNativeServiceStub::CmdSetNetworkAccessPolicy(MessageParcel &data, MessageParcel &reply)
2010 {
2011 uint32_t uid = 0;
2012 if (!data.ReadUint32(uid)) {
2013 NETNATIVE_LOGE("Read uint32 failed");
2014 return ERR_FLATTEN_OBJECT;
2015 }
2016 uint8_t wifi_allow = 0;
2017 if (!data.ReadUint8(wifi_allow)) {
2018 NETNATIVE_LOGE("Read uint8 failed");
2019 return ERR_FLATTEN_OBJECT;
2020 }
2021 uint8_t cellular_allow = 0;
2022 if (!data.ReadUint8(cellular_allow)) {
2023 NETNATIVE_LOGE("Read uint8 failed");
2024 return ERR_FLATTEN_OBJECT;
2025 }
2026 bool reconfirmFlag = true;
2027 if (!data.ReadBool(reconfirmFlag)) {
2028 NETNATIVE_LOGE("Read bool failed");
2029 return ERR_FLATTEN_OBJECT;
2030 }
2031 bool isBroker = true;
2032 if (!data.ReadBool(isBroker)) {
2033 NETNATIVE_LOGE("Read bool failed");
2034 return ERR_FLATTEN_OBJECT;
2035 }
2036
2037 NetworkAccessPolicy policy;
2038 policy.wifiAllow = wifi_allow;
2039 policy.cellularAllow = cellular_allow;
2040 int32_t result = SetNetworkAccessPolicy(uid, policy, reconfirmFlag, isBroker);
2041 reply.WriteInt32(result);
2042 return result;
2043 }
2044
CmdDelNetworkAccessPolicy(MessageParcel &data, MessageParcel &reply)2045 int32_t NetsysNativeServiceStub::CmdDelNetworkAccessPolicy(MessageParcel &data, MessageParcel &reply)
2046 {
2047 uint32_t uid = 0;
2048 if (!data.ReadUint32(uid)) {
2049 NETNATIVE_LOGE("Read uint32 failed");
2050 return ERR_FLATTEN_OBJECT;
2051 }
2052
2053 int32_t result = DeleteNetworkAccessPolicy(uid);
2054 reply.WriteInt32(result);
2055 return result;
2056 }
2057
CmdNotifyNetBearerTypeChange(MessageParcel &data, MessageParcel &reply)2058 int32_t NetsysNativeServiceStub::CmdNotifyNetBearerTypeChange(MessageParcel &data, MessageParcel &reply)
2059 {
2060 std::set<NetBearType> bearerTypes;
2061
2062 uint32_t size = 0;
2063 uint32_t value = 0;
2064 if (!data.ReadUint32(size)) {
2065 return ERR_FLATTEN_OBJECT;
2066 }
2067
2068 for (uint32_t i = 0; i < size; i++) {
2069 if (!data.ReadUint32(value)) {
2070 return ERR_FLATTEN_OBJECT;
2071 }
2072 if (value >= BEARER_DEFAULT) {
2073 return ERR_FLATTEN_OBJECT;
2074 }
2075 bearerTypes.insert(static_cast<NetBearType>(value));
2076 }
2077 int32_t result = NotifyNetBearerTypeChange(bearerTypes);
2078 reply.WriteInt32(result);
2079 return result;
2080 }
2081
CmdStartClat(MessageParcel &data, MessageParcel &reply)2082 int32_t NetsysNativeServiceStub::CmdStartClat(MessageParcel &data, MessageParcel &reply)
2083 {
2084 if (!NetManagerStandard::NetManagerPermission::CheckNetSysInternalPermission(
2085 NetManagerStandard::Permission::NETSYS_INTERNAL)) {
2086 NETNATIVE_LOGE("CmdStartClat CheckNetSysInternalPermission failed");
2087 return NETMANAGER_ERR_PERMISSION_DENIED;
2088 }
2089
2090 std::string interfaceName;
2091 if (!data.ReadString(interfaceName)) {
2092 NETNATIVE_LOGE("Read string failed");
2093 return ERR_FLATTEN_OBJECT;
2094 }
2095
2096 int32_t netId = 0;
2097 if (!data.ReadInt32(netId)) {
2098 NETNATIVE_LOGE("Read int32 failed");
2099 return ERR_FLATTEN_OBJECT;
2100 }
2101
2102 std::string nat64PrefixStr;
2103 if (!data.ReadString(nat64PrefixStr)) {
2104 NETNATIVE_LOGE("Read string failed");
2105 return ERR_FLATTEN_OBJECT;
2106 }
2107
2108 int32_t result = StartClat(interfaceName, netId, nat64PrefixStr);
2109 if (!reply.WriteInt32(result)) {
2110 NETNATIVE_LOGE("Write result failed");
2111 return ERR_FLATTEN_OBJECT;
2112 }
2113 return result;
2114 }
2115
CmdStopClat(MessageParcel &data, MessageParcel &reply)2116 int32_t NetsysNativeServiceStub::CmdStopClat(MessageParcel &data, MessageParcel &reply)
2117 {
2118 if (!NetManagerStandard::NetManagerPermission::CheckNetSysInternalPermission(
2119 NetManagerStandard::Permission::NETSYS_INTERNAL)) {
2120 NETNATIVE_LOGE("CmdStopClat CheckNetSysInternalPermission failed");
2121 return NETMANAGER_ERR_PERMISSION_DENIED;
2122 }
2123
2124 std::string interfaceName;
2125 if (!data.ReadString(interfaceName)) {
2126 NETNATIVE_LOGE("Read string failed");
2127 return ERR_FLATTEN_OBJECT;
2128 }
2129
2130 int32_t result = StopClat(interfaceName);
2131 if (!reply.WriteInt32(result)) {
2132 NETNATIVE_LOGE("Write result failed");
2133 return ERR_FLATTEN_OBJECT;
2134 }
2135 return result;
2136 }
2137
CmdClearFirewallAllRules(MessageParcel &data, MessageParcel &reply)2138 int32_t NetsysNativeServiceStub::CmdClearFirewallAllRules(MessageParcel &data, MessageParcel &reply)
2139 {
2140 NETNATIVE_LOG_D("Begin to dispatch cmd CmdClearFirewallAllRules");
2141 int32_t result = ClearFirewallAllRules();
2142 reply.WriteInt32(result);
2143 return result;
2144 }
2145
CmdSetNicTrafficAllowed(MessageParcel &data, MessageParcel &reply)2146 int32_t NetsysNativeServiceStub::CmdSetNicTrafficAllowed(MessageParcel &data, MessageParcel &reply)
2147 {
2148 if (!NetManagerStandard::NetManagerPermission::CheckNetSysInternalPermission(
2149 NetManagerStandard::Permission::NETSYS_INTERNAL)) {
2150 NETNATIVE_LOGE("CmdSetNicTrafficAllowed CheckNetSysInternalPermission failed");
2151 return NETMANAGER_ERR_PERMISSION_DENIED;
2152 }
2153
2154 bool status = false;
2155 int32_t size = 0;
2156 if (!data.ReadBool(status) || !data.ReadInt32(size)) {
2157 NETNATIVE_LOGE("CmdSetNicTrafficAllowed read status or size failed");
2158 return ERR_FLATTEN_OBJECT;
2159 }
2160 if (size > static_cast<int32_t>(MAX_IFACENAMES_SIZE)) {
2161 NETNATIVE_LOGE("CmdSetNicTrafficAllowed read data size too big");
2162 return ERR_FLATTEN_OBJECT;
2163 }
2164 std::vector<std::string> ifaceNames;
2165 std::string ifaceName;
2166 for (int32_t index = 0; index < size; index++) {
2167 data.ReadString(ifaceName);
2168 if (ifaceName.empty()) {
2169 NETNATIVE_LOGE("CmdSetNicTrafficAllowed ifaceName is empty, size mismatch");
2170 return ERR_FLATTEN_OBJECT;
2171 }
2172 ifaceNames.push_back(ifaceName);
2173 }
2174 int32_t result = SetNicTrafficAllowed(ifaceNames, status);
2175 if (!reply.WriteInt32(result)) {
2176 NETNATIVE_LOGE("Write CmdSetNicTrafficAllowed result failed");
2177 return ERR_FLATTEN_OBJECT;
2178 }
2179 return NetManagerStandard::NETMANAGER_SUCCESS;
2180 }
2181
2182 #ifdef SUPPORT_SYSVPN
CmdProcessVpnStage(MessageParcel &data, MessageParcel &reply)2183 int32_t NetsysNativeServiceStub::CmdProcessVpnStage(MessageParcel &data, MessageParcel &reply)
2184 {
2185 if (!NetManagerStandard::NetManagerPermission::CheckNetSysInternalPermission(
2186 NetManagerStandard::Permission::NETSYS_INTERNAL)) {
2187 NETNATIVE_LOGE("CmdProcessVpnStage CheckNetSysInternalPermission failed");
2188 return NETMANAGER_ERR_PERMISSION_DENIED;
2189 }
2190
2191 int32_t stage = 0;
2192 if (!data.ReadInt32(stage)) {
2193 return ERR_FLATTEN_OBJECT;
2194 }
2195 int32_t result = ProcessVpnStage(static_cast<NetsysNative::SysVpnStageCode>(stage));
2196 if (!reply.WriteInt32(result)) {
2197 NETNATIVE_LOGE("Write CmdProcessVpnStage result failed");
2198 return ERR_FLATTEN_OBJECT;
2199 }
2200 return NetManagerStandard::NETMANAGER_SUCCESS;
2201 }
2202 #endif // SUPPORT_SYSVPN
2203
CmdCloseSocketsUid(MessageParcel &data, MessageParcel &reply)2204 int32_t NetsysNativeServiceStub::CmdCloseSocketsUid(MessageParcel &data, MessageParcel &reply)
2205 {
2206 NETNATIVE_LOG_D("Begin to CmdCloseSocketsUid");
2207 std::string ipAddr = data.ReadString();
2208 uint32_t uid = data.ReadUint32();
2209 int32_t result = CloseSocketsUid(ipAddr, uid);
2210 reply.WriteInt32(result);
2211 return result;
2212 }
2213 } // namespace NetsysNative
2214 } // namespace OHOS
2215