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