1 /*
2  * Copyright (c) 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 "setup_context_ext.h"
17 
18 #include <new>
19 #include <string>
20 #include <vector>
21 
22 #include "inet_addr.h"
23 #include "napi_utils.h"
24 #include "net_manager_constants.h"
25 #include "netmanager_base_common_utils.h"
26 #include "netmgr_ext_log_wrapper.h"
27 #include "route.h"
28 
29 namespace OHOS {
30 namespace NetManagerStandard {
31 namespace {
32 constexpr uint8_t NET_FAMILY_IPV4 = 1;
33 constexpr uint8_t NET_FAMILY_IPV6 = 2;
34 constexpr int32_t NET_MASK_MAX_LENGTH = 32;
35 constexpr int32_t IPV6_NET_PREFIX_MAX_LENGTH = 128;
36 constexpr int32_t PARAM_JUST_OPTIONS = 1;
37 constexpr int32_t PARAM_OPTIONS_AND_CALLBACK = 2;
38 constexpr const char *CONFIG_ADDRESSES = "addresses";
39 constexpr const char *CONFIG_ROUTES = "routes";
40 constexpr const char *NET_ADDRESS = "address";
41 constexpr const char *NET_FAMILY = "family";
42 constexpr const char *NET_PORT = "port";
43 constexpr const char *NET_PREFIXLENGTH = "prefixLength";
44 constexpr const char *NET_INTERFACE = "interface";
45 constexpr const char *NET_DESTINATION = "destination";
46 constexpr const char *NET_GATEWAY = "gateway";
47 constexpr const char *NET_HAS_GATEWAY = "hasGateway";
48 constexpr const char *NET_ISDEFAULTROUTE = "isDefaultRoute";
49 constexpr const char *CONFIG_DNSADDRESSES = "dnsAddresses";
50 constexpr const char *CONFIG_SEARCHDOMAINS = "searchDomains";
51 constexpr const char *CONFIG_MTU = "mtu";
52 constexpr const char *CONFIG_ISIPV4ACCEPTED = "isIPv4Accepted";
53 constexpr const char *CONFIG_ISIPV6ACCEPTED = "isIPv6Accepted";
54 constexpr const char *CONFIG_ISLEGACY = "isLegacy";
55 constexpr const char *CONFIG_ISMETERED = "isMetered";
56 constexpr const char *CONFIG_ISBLOCKING = "isBlocking";
57 constexpr const char *CONFIG_TRUSTEDAPPLICATIONS = "trustedApplications";
58 constexpr const char *CONFIG_BLOCKEDAPPLICATIONS = "blockedApplications";
CheckParamsType(napi_env env,napi_value * params,size_t paramsCount)59 bool CheckParamsType(napi_env env, napi_value *params, size_t paramsCount)
60 {
61     switch (paramsCount) {
62         case PARAM_JUST_OPTIONS:
63             return (NapiUtils::GetValueType(env, params[0]) == napi_object);
64         case PARAM_OPTIONS_AND_CALLBACK:
65             return ((NapiUtils::GetValueType(env, params[0]) == napi_object) &&
66                     (NapiUtils::GetValueType(env, params[1]) == napi_function));
67         default:
68             return false;
69     }
70 }
71 
GetStringFromJsMandatoryItem(napi_env env,napi_value object,const std::string & key,std::string & value)72 bool GetStringFromJsMandatoryItem(napi_env env, napi_value object, const std::string &key, std::string &value)
73 {
74     if (NapiUtils::GetValueType(env, NapiUtils::GetNamedProperty(env, object, key)) != napi_string) {
75         NETMGR_EXT_LOG_E("param [%{public}s] type is mismatch", key.c_str());
76         return false;
77     }
78     value = NapiUtils::GetStringPropertyUtf8(env, object, key);
79     return (value.empty()) ? false : true;
80 }
81 
GetStringFromJsOptionItem(napi_env env,napi_value object,const std::string & key,std::string & value)82 void GetStringFromJsOptionItem(napi_env env, napi_value object, const std::string &key, std::string &value)
83 {
84     if (NapiUtils::HasNamedProperty(env, object, key)) {
85         if (NapiUtils::GetValueType(env, NapiUtils::GetNamedProperty(env, object, key)) == napi_string) {
86             value = NapiUtils::GetStringPropertyUtf8(env, object, key);
87             NETMGR_EXT_LOG_I("%{public}s: %{public}s", key.c_str(), value.c_str());
88         } else {
89             NETMGR_EXT_LOG_E("param [%{public}s] type is mismatch", key.c_str());
90         }
91     }
92 }
93 
GetUint8FromJsOptionItem(napi_env env,napi_value object,const std::string & key,uint8_t & value)94 void GetUint8FromJsOptionItem(napi_env env, napi_value object, const std::string &key, uint8_t &value)
95 {
96     if (NapiUtils::HasNamedProperty(env, object, key)) {
97         if (NapiUtils::GetValueType(env, NapiUtils::GetNamedProperty(env, object, key)) == napi_number) {
98             value = static_cast<uint8_t>(NapiUtils::GetUint32Property(env, object, key));
99             NETMGR_EXT_LOG_I("%{public}s: %{public}d", key.c_str(), value);
100         } else {
101             NETMGR_EXT_LOG_E("param [%{public}s] type is mismatch", key.c_str());
102         }
103     }
104 }
105 
GetBoolFromJsOptionItem(napi_env env,napi_value object,const std::string & key,bool & value)106 void GetBoolFromJsOptionItem(napi_env env, napi_value object, const std::string &key, bool &value)
107 {
108     if (NapiUtils::HasNamedProperty(env, object, key)) {
109         if (NapiUtils::GetValueType(env, NapiUtils::GetNamedProperty(env, object, key)) == napi_boolean) {
110             value = NapiUtils::GetBooleanProperty(env, object, key);
111             NETMGR_EXT_LOG_I("%{public}s: %{public}d", key.c_str(), value);
112         } else {
113             NETMGR_EXT_LOG_E("param [%{public}s] type is mismatch", key.c_str());
114         }
115     }
116 }
117 } // namespace
118 
SetUpContext(napi_env env,EventManager * manager)119 SetUpContext::SetUpContext(napi_env env, EventManager *manager) : BaseContext(env, manager) {}
120 
ParseParams(napi_value * params,size_t paramsCount)121 void SetUpContext::ParseParams(napi_value *params, size_t paramsCount)
122 {
123     if (!CheckParamsType(GetEnv(), params, paramsCount)) {
124         NETMGR_EXT_LOG_E("params type is mismatch");
125         SetNeedThrowException(true);
126         SetErrorCode(NETMANAGER_EXT_ERR_PARAMETER_ERROR);
127         return;
128     }
129     if (!ParseVpnConfig(params)) {
130         NETMGR_EXT_LOG_E("parse vpn config from js failed");
131         SetNeedThrowException(true);
132         SetErrorCode(NETMANAGER_EXT_ERR_PARAMETER_ERROR);
133         return;
134     }
135     if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
136         SetParseOK(SetCallback(params[1]) == napi_ok);
137         return;
138     }
139     SetParseOK(true);
140 }
141 
ParseVpnConfig(napi_value * params)142 bool SetUpContext::ParseVpnConfig(napi_value *params)
143 {
144     vpnConfig_ = new (std::nothrow) VpnConfig();
145     if (vpnConfig_ == nullptr) {
146         NETMGR_EXT_LOG_E("vpnConfig is nullptr");
147         return false;
148     }
149     if (!ParseAddrRouteParams(params[0]) || !ParseChoiceableParams(params[0])) {
150         return false;
151     }
152     return true;
153 }
154 
ParseAddressFamily(napi_env env,napi_value netAddress,uint8_t & family)155 static bool ParseAddressFamily(napi_env env, napi_value netAddress, uint8_t &family)
156 {
157     // The value is 1 for IPv4 and 2 for IPv6. The default value is 1.
158     if (!NapiUtils::HasNamedProperty(env, netAddress, NET_FAMILY)) {
159         family = AF_INET;
160         return true;
161     }
162     GetUint8FromJsOptionItem(env, netAddress, NET_FAMILY, family);
163     if (family == NET_FAMILY_IPV4) {
164         family = AF_INET;
165         return true;
166     } else if (family == NET_FAMILY_IPV6) {
167         family = AF_INET6;
168         return true;
169     } else {
170         NETMGR_EXT_LOG_E("family %{public}u is mismatch", family);
171         return false;
172     }
173 }
174 
ParseAddress(napi_env env,napi_value address,struct INetAddr & iNetAddr)175 static bool ParseAddress(napi_env env, napi_value address, struct INetAddr &iNetAddr)
176 {
177     napi_value netAddress = NapiUtils::GetNamedProperty(env, address, NET_ADDRESS);
178     if (NapiUtils::GetValueType(env, netAddress) != napi_object) {
179         NETMGR_EXT_LOG_E("param address type is mismatch");
180         return false;
181     }
182 
183     if (!GetStringFromJsMandatoryItem(env, netAddress, NET_ADDRESS, iNetAddr.address_)) {
184         NETMGR_EXT_LOG_E("get address-address failed");
185         return false;
186     }
187 
188     bool isIpv6 = CommonUtils::IsValidIPV6(iNetAddr.address_);
189     if (!isIpv6) {
190         if (!CommonUtils::IsValidIPV4(iNetAddr.address_)) {
191             NETMGR_EXT_LOG_E("invalid ip address");
192             return false;
193         }
194     }
195 
196     if (!ParseAddressFamily(env, netAddress, iNetAddr.family_)) {
197         return false;
198     }
199     GetUint8FromJsOptionItem(env, netAddress, NET_PORT, iNetAddr.port_);
200 
201     if (NapiUtils::GetValueType(env, NapiUtils::GetNamedProperty(env, address, NET_PREFIXLENGTH)) != napi_number) {
202         NETMGR_EXT_LOG_E("param [%{public}s] type is mismatch", NET_PREFIXLENGTH);
203         return false;
204     }
205     iNetAddr.prefixlen_ = static_cast<uint8_t>(NapiUtils::GetUint32Property(env, address, NET_PREFIXLENGTH));
206     if (isIpv6 && iNetAddr.prefixlen_ == 0) {
207         iNetAddr.prefixlen_ = CommonUtils::Ipv6PrefixLen(iNetAddr.address_);
208     }
209 
210     NETMGR_EXT_LOG_I("isIpv6:%{public}d, %{public}s: %{public}d", isIpv6, NET_PREFIXLENGTH, iNetAddr.prefixlen_);
211 
212     uint32_t prefix = iNetAddr.prefixlen_;
213     if (prefix == 0 || prefix >= (isIpv6 ? IPV6_NET_PREFIX_MAX_LENGTH : NET_MASK_MAX_LENGTH)) {
214         NETMGR_EXT_LOG_E("prefix: %{public}d error", prefix);
215         return false;
216     }
217     if (!isIpv6) {
218         uint32_t maskUint = (0xFFFFFFFF << (NET_MASK_MAX_LENGTH - prefix));
219         uint32_t ipAddrUint = CommonUtils::ConvertIpv4Address(iNetAddr.address_);
220         uint32_t subNetAddress = ipAddrUint & maskUint;
221         uint32_t boardcastAddress = subNetAddress | (~maskUint);
222         if ((ipAddrUint == subNetAddress) || (ipAddrUint == boardcastAddress)) {
223             NETMGR_EXT_LOG_E("invalid ip address");
224             return false;
225         }
226     }
227     return true;
228 }
229 
ParseDestination(napi_env env,napi_value jsRoute,struct INetAddr & iNetAddr)230 static bool ParseDestination(napi_env env, napi_value jsRoute, struct INetAddr &iNetAddr)
231 {
232     napi_value destination = NapiUtils::GetNamedProperty(env, jsRoute, NET_DESTINATION);
233     if (NapiUtils::GetValueType(env, destination) != napi_object) {
234         NETMGR_EXT_LOG_E("param destination type is mismatch");
235         return false;
236     }
237 
238     napi_value netAddress = NapiUtils::GetNamedProperty(env, destination, NET_ADDRESS);
239     if (NapiUtils::GetValueType(env, netAddress) != napi_object) {
240         NETMGR_EXT_LOG_E("param address type is mismatch");
241         return false;
242     }
243 
244     if (!GetStringFromJsMandatoryItem(env, netAddress, NET_ADDRESS, iNetAddr.address_)) {
245         NETMGR_EXT_LOG_E("get destination-address failed");
246         return false;
247     }
248 
249     if (!CommonUtils::IsValidIPV4(iNetAddr.address_) && !CommonUtils::IsValidIPV6(iNetAddr.address_)) {
250         NETMGR_EXT_LOG_E("invalid ip address");
251         return false;
252     }
253     if (!ParseAddressFamily(env, netAddress, iNetAddr.family_)) {
254         return false;
255     }
256     GetUint8FromJsOptionItem(env, netAddress, NET_PORT, iNetAddr.port_);
257     GetUint8FromJsOptionItem(env, destination, NET_PREFIXLENGTH, iNetAddr.prefixlen_);
258     return true;
259 }
260 
ParseGateway(napi_env env,napi_value jsRoute,struct INetAddr & iNetAddr)261 static bool ParseGateway(napi_env env, napi_value jsRoute, struct INetAddr &iNetAddr)
262 {
263     napi_value gateway = NapiUtils::GetNamedProperty(env, jsRoute, NET_GATEWAY);
264     if (NapiUtils::GetValueType(env, gateway) != napi_object) {
265         NETMGR_EXT_LOG_E("param gateway type is mismatch");
266         return false;
267     }
268 
269     GetStringFromJsMandatoryItem(env, gateway, NET_ADDRESS, iNetAddr.address_);
270 
271     if (!ParseAddressFamily(env, gateway, iNetAddr.family_)) {
272         return false;
273     }
274     GetUint8FromJsOptionItem(env, gateway, NET_PORT, iNetAddr.port_);
275     return true;
276 }
277 
ParseRoute(napi_env env,napi_value jsRoute,Route & route)278 static bool ParseRoute(napi_env env, napi_value jsRoute, Route &route)
279 {
280     GetStringFromJsOptionItem(env, jsRoute, NET_INTERFACE, route.iface_);
281 
282     if (!ParseDestination(env, jsRoute, route.destination_)) {
283         NETMGR_EXT_LOG_E("ParseDestination failed");
284         return false;
285     }
286     if (!ParseGateway(env, jsRoute, route.gateway_)) {
287         NETMGR_EXT_LOG_E("ParseGateway failed");
288         return false;
289     }
290 
291     GetBoolFromJsOptionItem(env, jsRoute, NET_HAS_GATEWAY, route.hasGateway_);
292     GetBoolFromJsOptionItem(env, jsRoute, NET_ISDEFAULTROUTE, route.isDefaultRoute_);
293     return true;
294 }
295 
ParseAddrRouteParams(napi_value config)296 bool SetUpContext::ParseAddrRouteParams(napi_value config)
297 {
298     // parse addresses.
299     if (NapiUtils::HasNamedProperty(GetEnv(), config, CONFIG_ADDRESSES)) {
300         napi_value addrArray = NapiUtils::GetNamedProperty(GetEnv(), config, CONFIG_ADDRESSES);
301         if (!NapiUtils::IsArray(GetEnv(), addrArray)) {
302             NETMGR_EXT_LOG_E("addresses is not array");
303             return false;
304         }
305         uint32_t addrLength = NapiUtils::GetArrayLength(GetEnv(), addrArray);
306         for (uint32_t i = 0; i < addrLength; ++i) { // set length limit.
307             INetAddr iNetAddr;
308             if (!ParseAddress(GetEnv(), NapiUtils::GetArrayElement(GetEnv(), addrArray, i), iNetAddr)) {
309                 NETMGR_EXT_LOG_E("ParseAddress failed");
310                 return false;
311             }
312             vpnConfig_->addresses_.emplace_back(iNetAddr);
313             bool isIpv6 = CommonUtils::IsValidIPV6(iNetAddr.address_);
314             vpnConfig_->isAcceptIPv4_ = !isIpv6;
315             vpnConfig_->isAcceptIPv6_ = isIpv6;
316         }
317     }
318 
319     // parse routes.
320     if (NapiUtils::HasNamedProperty(GetEnv(), config, CONFIG_ROUTES)) {
321         napi_value routes = NapiUtils::GetNamedProperty(GetEnv(), config, CONFIG_ROUTES);
322         if (!NapiUtils::IsArray(GetEnv(), routes)) {
323             NETMGR_EXT_LOG_E("routes is not array");
324             return false;
325         }
326         uint32_t routesLength = NapiUtils::GetArrayLength(GetEnv(), routes);
327         for (uint32_t idx = 0; idx < routesLength; ++idx) { // set length limit.
328             struct Route routeInfo;
329             if (!ParseRoute(GetEnv(), NapiUtils::GetArrayElement(GetEnv(), routes, idx), routeInfo)) {
330                 NETMGR_EXT_LOG_E("ParseRoute failed");
331                 return false;
332             }
333             vpnConfig_->routes_.emplace_back(routeInfo);
334         }
335     }
336     return true;
337 }
338 
ParseOptionArrayString(napi_env env,napi_value config,const std::string & key,std::vector<std::string> & vector)339 static bool ParseOptionArrayString(napi_env env, napi_value config, const std::string &key,
340                                    std::vector<std::string> &vector)
341 {
342     if (NapiUtils::HasNamedProperty(env, config, key)) {
343         napi_value array = NapiUtils::GetNamedProperty(env, config, key);
344         if (!NapiUtils::IsArray(env, array)) {
345             NETMGR_EXT_LOG_E("param [%{public}s] is not array", key.c_str());
346             return false;
347         }
348         uint32_t arrayLength = NapiUtils::GetArrayLength(env, array);
349         for (uint32_t i = 0; i < arrayLength; ++i) {
350             std::string item = NapiUtils::GetStringFromValueUtf8(env, NapiUtils::GetArrayElement(env, array, i));
351             NETMGR_EXT_LOG_D("%{public}s: %{public}s", key.c_str(), item.c_str());
352             vector.push_back(item);
353         }
354     }
355     return true;
356 }
357 
ParseChoiceableParams(napi_value config)358 bool SetUpContext::ParseChoiceableParams(napi_value config)
359 {
360     ParseOptionArrayString(GetEnv(), config, CONFIG_DNSADDRESSES, vpnConfig_->dnsAddresses_);
361     ParseOptionArrayString(GetEnv(), config, CONFIG_SEARCHDOMAINS, vpnConfig_->searchDomains_);
362 
363     if (NapiUtils::HasNamedProperty(GetEnv(), config, CONFIG_MTU)) {
364         if (NapiUtils::GetValueType(GetEnv(), NapiUtils::GetNamedProperty(GetEnv(), config, CONFIG_MTU)) ==
365             napi_number) {
366             vpnConfig_->mtu_ = NapiUtils::GetInt32Property(GetEnv(), config, CONFIG_MTU);
367             NETMGR_EXT_LOG_I("%{public}s: %{public}d", CONFIG_MTU, vpnConfig_->mtu_);
368         } else {
369             NETMGR_EXT_LOG_E("param [%{public}s] type is mismatch", CONFIG_MTU);
370         }
371     }
372 
373     GetBoolFromJsOptionItem(GetEnv(), config, CONFIG_ISIPV4ACCEPTED, vpnConfig_->isAcceptIPv4_);
374     GetBoolFromJsOptionItem(GetEnv(), config, CONFIG_ISIPV6ACCEPTED, vpnConfig_->isAcceptIPv6_);
375     GetBoolFromJsOptionItem(GetEnv(), config, CONFIG_ISLEGACY, vpnConfig_->isLegacy_);
376     GetBoolFromJsOptionItem(GetEnv(), config, CONFIG_ISMETERED, vpnConfig_->isMetered_);
377     GetBoolFromJsOptionItem(GetEnv(), config, CONFIG_ISBLOCKING, vpnConfig_->isBlocking_);
378 
379     ParseOptionArrayString(GetEnv(), config, CONFIG_TRUSTEDAPPLICATIONS, vpnConfig_->acceptedApplications_);
380     ParseOptionArrayString(GetEnv(), config, CONFIG_BLOCKEDAPPLICATIONS, vpnConfig_->refusedApplications_);
381     return true;
382 }
383 } // namespace NetManagerStandard
384 } // namespace OHOS
385