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