1 /*
2 * Copyright (C) 2022 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 "wifi_net_agent.h"
17 #include <cinttypes>
18 #include <algorithm>
19 #include "inet_addr.h"
20 #include "ip_tools.h"
21 #include "iservice_registry.h"
22 #include "netsys_native_service_proxy.h"
23 #include "net_conn_client.h"
24 #include "system_ability_definition.h"
25 #include "wifi_common_util.h"
26 #include "wifi_logger.h"
27 #include "wifi_config_center.h"
28 #include "ipv6_address.h"
29 #include "wifi_global_func.h"
30 #include "wifi_app_state_aware.h"
31
32 DEFINE_WIFILOG_LABEL("WifiNetAgent");
33
34 namespace OHOS {
35 namespace Wifi {
36 constexpr const char *WIFI_NET_CONN_MGR_WORK_THREAD = "WIFI_NET_CONN_MGR_WORK_THREAD";
37 using namespace NetManagerStandard;
38
39 #define INVALID_SUPPLIER_ID 0
40 #define ACCEPT_UNVALIDATED 7
41
GetInstance()42 WifiNetAgent &WifiNetAgent::GetInstance()
43 {
44 static WifiNetAgent gWifiNetAgent;
45 return gWifiNetAgent;
46 }
47
WifiNetAgent()48 WifiNetAgent::WifiNetAgent()
49 {
50 netAgentEventHandler_ = std::make_unique<WifiEventHandler>(WIFI_NET_CONN_MGR_WORK_THREAD);
51 }
~WifiNetAgent()52 WifiNetAgent::~WifiNetAgent()
53 {
54 if (netAgentEventHandler_) {
55 netAgentEventHandler_.reset();
56 }
57 }
58
RegisterNetSupplier()59 bool WifiNetAgent::RegisterNetSupplier()
60 {
61 TimeStats timeStats(__func__);
62 WIFI_LOGI("Enter RegisterNetSupplier.");
63
64 std::string ident = "wifi";
65 using NetManagerStandard::NetBearType;
66 using NetManagerStandard::NetCap;
67 std::set<NetCap> netCaps {NetCap::NET_CAPABILITY_INTERNET};
68 if (supplierId != INVALID_SUPPLIER_ID) {
69 WIFI_LOGI("RegisterNetSupplier supplierId alread exist.");
70 return true;
71 }
72 int32_t result = NetConnClient::GetInstance().RegisterNetSupplier(NetBearType::BEARER_WIFI,
73 ident, netCaps, supplierId);
74 if (result == NETMANAGER_SUCCESS) {
75 WIFI_LOGI("Register NetSupplier successful, supplierId is [%{public}d]", supplierId);
76 return true;
77 }
78 WIFI_LOGI("Register NetSupplier failed");
79 return false;
80 }
81
RegisterNetSupplierCallback()82 bool WifiNetAgent::RegisterNetSupplierCallback()
83 {
84 TimeStats timeStats(__func__);
85 WIFI_LOGI("Enter RegisterNetSupplierCallback.");
86 sptr<NetConnCallback> pNetConnCallback = (std::make_unique<NetConnCallback>()).release();
87 if (pNetConnCallback == nullptr) {
88 WIFI_LOGE("pNetConnCallback is null\n");
89 return false;
90 }
91
92 int32_t result = NetConnClient::GetInstance().RegisterNetSupplierCallback(supplierId, pNetConnCallback);
93 if (result == NETMANAGER_SUCCESS) {
94 WIFI_LOGI("Register NetSupplierCallback successful");
95 return true;
96 }
97 WIFI_LOGE("Register NetSupplierCallback failed [%{public}d]", result);
98 return false;
99 }
100
UnregisterNetSupplier()101 void WifiNetAgent::UnregisterNetSupplier()
102 {
103 TimeStats timeStats(__func__);
104 WIFI_LOGI("Enter UnregisterNetSupplier.");
105 int32_t result = NetConnClient::GetInstance().UnregisterNetSupplier(supplierId);
106 WIFI_LOGI("Unregister network result:%{public}d", result);
107 supplierId = INVALID_SUPPLIER_ID;
108 }
109
UpdateNetSupplierInfo(const sptr<NetManagerStandard::NetSupplierInfo> & netSupplierInfo)110 void WifiNetAgent::UpdateNetSupplierInfo(const sptr<NetManagerStandard::NetSupplierInfo> &netSupplierInfo)
111 {
112 TimeStats timeStats(__func__);
113 WIFI_LOGI("Enter UpdateNetSupplierInfo.");
114 isWifiAvaliable_ = netSupplierInfo->isAvailable_;
115 int32_t result = NetConnClient::GetInstance().UpdateNetSupplierInfo(supplierId, netSupplierInfo);
116 WIFI_LOGI("Update network result:%{public}d", result);
117 }
118
UpdateNetLinkInfo(IpInfo & wifiIpInfo,IpV6Info & wifiIpV6Info,WifiProxyConfig & wifiProxyConfig,int instId)119 void WifiNetAgent::UpdateNetLinkInfo(IpInfo &wifiIpInfo, IpV6Info &wifiIpV6Info, WifiProxyConfig &wifiProxyConfig,
120 int instId)
121 {
122 TimeStats timeStats(__func__);
123 WIFI_LOGI("Enter UpdateNetLinkInfo.");
124 if (!isWifiAvaliable_) {
125 WIFI_LOGE("wifi is not avaliable, no need UpdateNetLinkInfo");
126 return;
127 }
128 sptr<NetManagerStandard::NetLinkInfo> netLinkInfo = (std::make_unique<NetManagerStandard::NetLinkInfo>()).release();
129 CreateNetLinkInfo(netLinkInfo, wifiIpInfo, wifiIpV6Info, wifiProxyConfig, instId);
130 int32_t result = NetConnClient::GetInstance().UpdateNetLinkInfo(supplierId, netLinkInfo);
131 WIFI_LOGI("UpdateNetLinkInfo result:%{public}d", result);
132 }
133
AddRoute(const std::string interface,const std::string ipAddress,int prefixLength)134 bool WifiNetAgent::AddRoute(const std::string interface, const std::string ipAddress, int prefixLength)
135 {
136 TimeStats timeStats(__func__);
137 LOGI("NetAgent add route");
138 unsigned int ipInt = IpTools::ConvertIpv4Address(ipAddress);
139 std::string mask = IpTools::ConvertIpv4Mask(prefixLength);
140 unsigned int maskInt = IpTools::ConvertIpv4Address(mask);
141 std::string strLocalRoute = IpTools::ConvertIpv4Address(ipInt & maskInt);
142 std::string destAddress = strLocalRoute + "/" + std::to_string(prefixLength);
143
144 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
145 if (samgr == nullptr) {
146 LOGE("GetSystemAbilityManager failed!");
147 return false;
148 }
149 auto remote = samgr->GetSystemAbility(COMM_NETSYS_NATIVE_SYS_ABILITY_ID);
150 if (remote == nullptr) {
151 LOGE("GetSystemAbility failed!");
152 return false;
153 }
154 OHOS::sptr<OHOS::NetsysNative::INetsysService> netsysService = iface_cast<NetsysNative::INetsysService>(remote);
155 if (netsysService == nullptr) {
156 LOGE("NetdService is nullptr!");
157 return false;
158 }
159 LOGI("Add route, interface: %{public}s, destAddress: %{public}s, ipAddress: %{public}s, prefixLength: %{public}d",
160 interface.c_str(), IpAnonymize(destAddress).c_str(), IpAnonymize(ipAddress).c_str(), prefixLength);
161 netsysService->NetworkAddRoute(OHOS::nmd::LOCAL_NETWORK_NETID, interface, destAddress, ipAddress);
162 LOGI("NetAgent add route finish");
163 return true;
164 }
165
DelInterfaceAddress(const std::string & interface,const std::string & ipAddress,int prefixLength)166 bool WifiNetAgent::DelInterfaceAddress(const std::string &interface, const std::string &ipAddress, int prefixLength)
167 {
168 int32_t result = NetConnClient::GetInstance().DelInterfaceAddress(interface, ipAddress, prefixLength);
169 if (result == NETMANAGER_SUCCESS) {
170 WIFI_LOGI("DelInterfaceAddress successful");
171 return true;
172 }
173 WIFI_LOGI("DelInterfaceAddress failed");
174 return false;
175 }
176
OnStaMachineUpdateNetLinkInfo(IpInfo wifiIpInfo,IpV6Info wifiIpV6Info,WifiProxyConfig wifiProxyConfig,int instId)177 void WifiNetAgent::OnStaMachineUpdateNetLinkInfo(IpInfo wifiIpInfo, IpV6Info wifiIpV6Info,
178 WifiProxyConfig wifiProxyConfig, int instId)
179 {
180 if (netAgentEventHandler_) {
181 #ifdef FEATURE_ITNETWORK_PREFERRED_SUPPORT
182 netAgentEventHandler_->PostSyncTask(
183 #else
184 netAgentEventHandler_->PostAsyncTask(
185 #endif
186 [this, wifiIpInfo, wifiIpV6Info, wifiProxyConfig, instId]() mutable {
187 this->UpdateNetLinkInfo(wifiIpInfo, wifiIpV6Info, wifiProxyConfig, instId);
188 });
189 }
190 }
191
OnStaMachineUpdateNetSupplierInfo(const sptr<NetManagerStandard::NetSupplierInfo> netSupplierInfo)192 void WifiNetAgent::OnStaMachineUpdateNetSupplierInfo(const sptr<NetManagerStandard::NetSupplierInfo> netSupplierInfo)
193 {
194 if (netAgentEventHandler_) {
195 #ifdef FEATURE_ITNETWORK_PREFERRED_SUPPORT
196 netAgentEventHandler_->PostSyncTask([this, netInfo = netSupplierInfo]() {
197 #else
198 netAgentEventHandler_->PostAsyncTask([this, netInfo = netSupplierInfo]() {
199 #endif
200 this->UpdateNetSupplierInfo(netInfo);
201 });
202 }
203 }
204
205 void WifiNetAgent::OnStaMachineWifiStart()
206 {
207 if (netAgentEventHandler_) {
208 #ifdef FEATURE_ITNETWORK_PREFERRED_SUPPORT
209 netAgentEventHandler_->PostSyncTask([this]() {
210 #else
211 netAgentEventHandler_->PostAsyncTask([this]() {
212 #endif
213 this->RegisterNetSupplier();
214 this->RegisterNetSupplierCallback();
215 });
216 }
217 }
218
219 void WifiNetAgent::OnStaMachineNetManagerRestart(const sptr<NetManagerStandard::NetSupplierInfo> netSupplierInfo,
220 int instId)
221 {
222 if (!netAgentEventHandler_) {
223 WIFI_LOGE("%{public}s netAgentEventHandler_ is null", __func__);
224 return;
225 }
226 #ifdef FEATURE_ITNETWORK_PREFERRED_SUPPORT
227 netAgentEventHandler_->PostSyncTask([this, supplierInfo = netSupplierInfo, m_instId = instId]() {
228 #else
229 netAgentEventHandler_->PostAsyncTask([this, supplierInfo = netSupplierInfo, m_instId = instId]() {
230 #endif
231 this->RegisterNetSupplier();
232 this->RegisterNetSupplierCallback();
233 WifiLinkedInfo linkedInfo;
234 WifiConfigCenter::GetInstance().GetLinkedInfo(linkedInfo, m_instId);
235 if (linkedInfo.connState == ConnState::CONNECTED) {
236 #ifndef OHOS_ARCH_LITE
237 if (supplierInfo != nullptr) {
238 TimeStats timeStats("Call UpdateNetSupplierInfo");
239 this->UpdateNetSupplierInfo(supplierInfo);
240 }
241 #endif
242 IpInfo wifiIpInfo;
243 WifiConfigCenter::GetInstance().GetIpInfo(wifiIpInfo, m_instId);
244 IpV6Info wifiIpV6Info;
245 WifiConfigCenter::GetInstance().GetIpv6Info(wifiIpV6Info, m_instId);
246 WifiDeviceConfig config;
247 WifiSettings::GetInstance().GetDeviceConfig(linkedInfo.networkId, config);
248 this->UpdateNetLinkInfo(wifiIpInfo, wifiIpV6Info, config.wifiProxyconfig, m_instId);
249 }
250 });
251 }
252
253 void WifiNetAgent::CreateNetLinkInfo(sptr<NetManagerStandard::NetLinkInfo> &netLinkInfo, IpInfo &wifiIpInfo,
254 IpV6Info &wifiIpV6Info, WifiProxyConfig &wifiProxyConfig, int instId)
255 {
256 netLinkInfo->ifaceName_ = WifiConfigCenter::GetInstance().GetStaIfaceName(instId);
257
258 SetNetLinkIPInfo(netLinkInfo, wifiIpInfo, wifiIpV6Info);
259 SetNetLinkHostRouteInfo(netLinkInfo, wifiIpInfo);
260 SetNetLinkRouteInfo(netLinkInfo, wifiIpInfo, wifiIpV6Info);
261 SetNetLinkDnsInfo(netLinkInfo, wifiIpInfo, wifiIpV6Info);
262 SetNetLinkLocalRouteInfo(netLinkInfo, wifiIpInfo, wifiIpV6Info);
263 if (wifiProxyConfig.configureMethod == ConfigureProxyMethod::AUTOCONFIGUE) {
264 /* Automatic proxy is not supported */
265 } else if (wifiProxyConfig.configureMethod == ConfigureProxyMethod::MANUALCONFIGUE) {
266 std::vector<std::string> exclusionList;
267 wifiProxyConfig.manualProxyConfig.GetExclusionObjectList(exclusionList);
268 std::list<std::string> tmpExclusionList;
269 std::copy_if(exclusionList.begin(), exclusionList.end(), std::back_inserter(tmpExclusionList),
270 [](const std::string &str) { return !str.empty(); } );
271 netLinkInfo->httpProxy_.SetHost(std::move(wifiProxyConfig.manualProxyConfig.serverHostName));
272 netLinkInfo->httpProxy_.SetPort(wifiProxyConfig.manualProxyConfig.serverPort);
273 netLinkInfo->httpProxy_.SetExclusionList(tmpExclusionList);
274 } else {
275 netLinkInfo->httpProxy_.SetHost("");
276 netLinkInfo->httpProxy_.SetPort(0);
277 }
278
279 return;
280 }
281
282 void WifiNetAgent::SetNetLinkIPInfo(sptr<NetManagerStandard::NetLinkInfo> &netLinkInfo, IpInfo &wifiIpInfo,
283 IpV6Info &wifiIpV6Info)
284 {
285 unsigned int prefixLength =
286 static_cast<unsigned int>(IpTools::GetMaskLength(IpTools::ConvertIpv4Address(wifiIpInfo.netmask)));
287 sptr<NetManagerStandard::INetAddr> netAddr = (std::make_unique<NetManagerStandard::INetAddr>()).release();
288 netAddr->type_ = NetManagerStandard::INetAddr::IPV4;
289 netAddr->family_ = NetManagerStandard::INetAddr::IPV4;
290 netAddr->address_ = IpTools::ConvertIpv4Address(wifiIpInfo.ipAddress);
291 netAddr->netMask_ = IpTools::ConvertIpv4Address(wifiIpInfo.netmask);
292 netAddr->prefixlen_ = prefixLength;
293 netLinkInfo->netAddrList_.push_back(*netAddr);
294
295 sptr<NetManagerStandard::INetAddr> netIpv6Addr = (std::make_unique<NetManagerStandard::INetAddr>()).release();
296 netIpv6Addr->type_ = NetManagerStandard::INetAddr::IPV6;
297 netIpv6Addr->family_ = NetManagerStandard::INetAddr::IPV6;
298 netIpv6Addr->netMask_ = wifiIpV6Info.netmask;
299 netIpv6Addr->prefixlen_ = 0;
300 if (!wifiIpV6Info.globalIpV6Address.empty()) {
301 netIpv6Addr->address_ = wifiIpV6Info.globalIpV6Address;
302 netLinkInfo->netAddrList_.push_back(*netIpv6Addr);
303 LOGI("SetNetLinkIPInfo globalIpv6:%{public}s", MacAnonymize(wifiIpV6Info.globalIpV6Address).c_str());
304 }
305 if (!wifiIpV6Info.randGlobalIpV6Address.empty()) {
306 netIpv6Addr->address_ = wifiIpV6Info.randGlobalIpV6Address;
307 netLinkInfo->netAddrList_.push_back(*netIpv6Addr);
308 LOGI("SetNetLinkIPInfo randGlobalIpv6:%{public}s", MacAnonymize(wifiIpV6Info.randGlobalIpV6Address).c_str());
309 }
310 if (!wifiIpV6Info.uniqueLocalAddress1.empty()) {
311 netIpv6Addr->address_ = wifiIpV6Info.uniqueLocalAddress1;
312 netLinkInfo->netAddrList_.push_back(*netIpv6Addr);
313 LOGI("SetNetLinkIPInfo LocalIpv6:%{public}s", MacAnonymize(wifiIpV6Info.uniqueLocalAddress1).c_str());
314 }
315 if (!wifiIpV6Info.uniqueLocalAddress2.empty()) {
316 netIpv6Addr->address_ = wifiIpV6Info.uniqueLocalAddress2;
317 netLinkInfo->netAddrList_.push_back(*netIpv6Addr);
318 LOGI("SetNetLinkIPInfo randLocalIpv6:%{public}s", MacAnonymize(wifiIpV6Info.uniqueLocalAddress2).c_str());
319 }
320 }
321
322 void WifiNetAgent::SetNetLinkDnsInfo(sptr<NetManagerStandard::NetLinkInfo> &netLinkInfo, IpInfo &wifiIpInfo,
323 IpV6Info &wifiIpV6Info)
324 {
325 sptr<NetManagerStandard::INetAddr> dns = (std::make_unique<NetManagerStandard::INetAddr>()).release();
326 dns->type_ = NetManagerStandard::INetAddr::IPV4;
327 dns->family_ = NetManagerStandard::INetAddr::IPV4;
328 if (wifiIpInfo.primaryDns != 0) {
329 dns->address_ = IpTools::ConvertIpv4Address(wifiIpInfo.primaryDns);
330 netLinkInfo->dnsList_.push_back(*dns);
331 LOGI("SetNetLinkDnsInfo ipv4 address:%{public}s", IpAnonymize(dns->address_).c_str());
332 }
333 if (wifiIpInfo.secondDns != 0) {
334 dns->address_ = IpTools::ConvertIpv4Address(wifiIpInfo.secondDns);
335 netLinkInfo->dnsList_.push_back(*dns);
336 LOGI("SetNetLinkDnsInfo ipv4 address:%{public}s", IpAnonymize(dns->address_).c_str());
337 }
338 sptr<NetManagerStandard::INetAddr> ipv6dns = (std::make_unique<NetManagerStandard::INetAddr>()).release();
339 ipv6dns->type_ = NetManagerStandard::INetAddr::IPV6;
340 ipv6dns->family_ = NetManagerStandard::INetAddr::IPV6;
341 if (!wifiIpV6Info.primaryDns.empty()) {
342 ipv6dns->address_ = wifiIpV6Info.primaryDns;
343 netLinkInfo->dnsList_.push_back(*ipv6dns);
344 LOGI("SetNetLinkDnsInfo ipv6:%{public}s", MacAnonymize(wifiIpV6Info.primaryDns).c_str());
345 }
346 if (!wifiIpV6Info.secondDns.empty()) {
347 ipv6dns->address_ = wifiIpV6Info.secondDns;
348 netLinkInfo->dnsList_.push_back(*ipv6dns);
349 LOGI("SetNetLinkDnsInfo ipv6:%{public}s", MacAnonymize(wifiIpV6Info.secondDns).c_str());
350 }
351 }
352
353 void WifiNetAgent::SetNetLinkRouteInfo(sptr<NetManagerStandard::NetLinkInfo> &netLinkInfo, IpInfo &wifiIpInfo,
354 IpV6Info &wifiIpV6Info)
355 {
356 sptr<NetManagerStandard::Route> route = (std::make_unique<NetManagerStandard::Route>()).release();
357 route->iface_ = netLinkInfo->ifaceName_;
358 route->destination_.type_ = NetManagerStandard::INetAddr::IPV4;
359 route->destination_.address_ = "0.0.0.0";
360 route->destination_.family_ = NetManagerStandard::INetAddr::IPV4;
361 route->gateway_.address_ = IpTools::ConvertIpv4Address(wifiIpInfo.gateway);
362 route->gateway_.family_ = NetManagerStandard::INetAddr::IPV4;
363 netLinkInfo->routeList_.push_back(*route);
364 LOGI("SetNetLinkRouteInfo gateway:%{public}s", IpAnonymize(route->gateway_.address_).c_str());
365 if (!wifiIpV6Info.gateway.empty()) {
366 sptr<NetManagerStandard::Route> ipv6route = (std::make_unique<NetManagerStandard::Route>()).release();
367 ipv6route->iface_ = netLinkInfo->ifaceName_;
368 ipv6route->destination_.type_ = NetManagerStandard::INetAddr::IPV6;
369 ipv6route->destination_.family_ = NetManagerStandard::INetAddr::IPV6;
370 ipv6route->destination_.address_ = "::";
371 ipv6route->destination_.prefixlen_ = 0;
372 ipv6route->gateway_.address_ = wifiIpV6Info.gateway;
373 ipv6route->gateway_.family_ = NetManagerStandard::INetAddr::IPV6;
374 netLinkInfo->routeList_.push_back(*ipv6route);
375 LOGI("SetNetLinkRouteInfo gateway:%{public}s", MacAnonymize(wifiIpV6Info.gateway).c_str());
376 }
377 }
378
379 void WifiNetAgent::SetNetLinkHostRouteInfo(sptr<NetManagerStandard::NetLinkInfo> &netLinkInfo, IpInfo &wifiIpInfo)
380 {
381 if ((wifiIpInfo.ipAddress & wifiIpInfo.netmask) != (wifiIpInfo.gateway & wifiIpInfo.netmask)) {
382 sptr<NetManagerStandard::Route> hostRoute = (std::make_unique<NetManagerStandard::Route>()).release();
383 hostRoute->iface_ = netLinkInfo->ifaceName_;
384 hostRoute->destination_.type_ = NetManagerStandard::INetAddr::IPV4;
385 hostRoute->destination_.address_ = IpTools::ConvertIpv4Address(wifiIpInfo.gateway);
386 hostRoute->destination_.family_ = NetManagerStandard::INetAddr::IPV4;
387 hostRoute->destination_.prefixlen_ = MAX_PREFIX_LEN;
388 netLinkInfo->routeList_.push_back(*hostRoute);
389 LOGI("SetNetLinkHostRouteInfo gateway:%{public}s", IpAnonymize(hostRoute->gateway_.address_).c_str());
390 }
391 }
392
393 void WifiNetAgent::SetNetLinkLocalRouteInfo(sptr<NetManagerStandard::NetLinkInfo> &netLinkInfo, IpInfo &wifiIpInfo,
394 IpV6Info &wifiIpV6Info)
395 {
396 unsigned int prefixLength =
397 static_cast<unsigned int>(IpTools::GetMaskLength(IpTools::ConvertIpv4Address(wifiIpInfo.netmask)));
398 sptr<NetManagerStandard::Route> localRoute = (std::make_unique<NetManagerStandard::Route>()).release();
399 std::string strLocalRoute = IpTools::ConvertIpv4Address(wifiIpInfo.ipAddress & wifiIpInfo.netmask);
400 localRoute->iface_ = netLinkInfo->ifaceName_;
401 localRoute->destination_.type_ = NetManagerStandard::INetAddr::IPV4;
402 localRoute->destination_.address_ = strLocalRoute;
403 localRoute->destination_.prefixlen_ = prefixLength;
404 localRoute->gateway_.address_ = "0.0.0.0";
405 netLinkInfo->routeList_.push_back(*localRoute);
406 LOGI("SetNetLinkLocalRouteInfo ifaceName_:%{public}s %{public}u %{public}s", netLinkInfo->ifaceName_.c_str(),
407 prefixLength, IpAnonymize(strLocalRoute).c_str());
408 if (!wifiIpV6Info.netmask.empty()) {
409 unsigned int ipv6PrefixLength = IpTools::GetIPV6MaskLength(wifiIpV6Info.netmask);
410 sptr<NetManagerStandard::Route> ipv6route = (std::make_unique<NetManagerStandard::Route>()).release();
411 ipv6route->iface_ = netLinkInfo->ifaceName_;
412 ipv6route->destination_.type_ = NetManagerStandard::INetAddr::IPV6;
413 ipv6route->destination_.prefixlen_ = ipv6PrefixLength;
414 ipv6route->gateway_.address_ = "";
415 if (!wifiIpV6Info.globalIpV6Address.empty()) {
416 ipv6route->destination_.address_ =
417 Ipv6Address::GetPrefixByAddr(wifiIpV6Info.globalIpV6Address, ipv6PrefixLength);
418 netLinkInfo->routeList_.push_back(*ipv6route);
419 LOGI("SetNetLinkLocalRouteInfo ipv6PrefixLength:%{public}u globalIpv6:%{public}s", ipv6PrefixLength,
420 MacAnonymize(wifiIpV6Info.globalIpV6Address).c_str());
421 }
422 if (!wifiIpV6Info.uniqueLocalAddress1.empty()) {
423 ipv6route->destination_.address_ =
424 Ipv6Address::GetPrefixByAddr(wifiIpV6Info.uniqueLocalAddress1, ipv6PrefixLength);
425 netLinkInfo->routeList_.push_back(*ipv6route);
426 LOGI("SetNetLinkLocalRouteInfo ipv6PrefixLength:%{public}u uniqueLocalIpv6:%{public}s", ipv6PrefixLength,
427 MacAnonymize(wifiIpV6Info.uniqueLocalAddress1).c_str());
428 }
429 }
430 }
431
432 void WifiNetAgent::InitWifiNetAgent(const WifiNetAgentCallbacks &wifiNetAgentCallbacks)
433 {
434 wifiNetAgentCallbacks_ = wifiNetAgentCallbacks;
435 }
436
437 void WifiNetAgent::ResetSupplierId()
438 {
439 supplierId = INVALID_SUPPLIER_ID;
440 }
441
442 uint32_t WifiNetAgent::GetSupplierId()
443 {
444 return supplierId;
445 }
446
447 bool WifiNetAgent::RequestNetwork(const int uid, const int networkId)
448 {
449 if (!wifiNetAgentCallbacks_.OnRequestNetwork) {
450 WIFI_LOGE("OnRequestNetwork is nullptr.");
451 return false;
452 }
453 if (wifiNetAgentCallbacks_.OnRequestNetwork(uid, networkId)) {
454 return true;
455 }
456 return false;
457 }
458
459 WifiNetAgent::NetConnCallback::NetConnCallback()
460 {
461 }
462
463 WifiNetAgent::NetConnCallback::~NetConnCallback()
464 {}
465
466 int32_t WifiNetAgent::NetConnCallback::RequestNetwork(
467 const std::string &ident, const std::set<NetManagerStandard::NetCap> &netCaps,
468 const NetManagerStandard::NetRequest &netrequest)
469 {
470 WIFI_LOGD("Enter NetConnCallback::RequestNetwork");
471 LogNetCaps(ident, netCaps);
472 #ifndef OHOS_ARCH_LITE
473 if (requestIds_.find(netrequest.requestId) != requestIds_.end()) {
474 return -1;
475 }
476 requestIds_.insert(netrequest.requestId);
477
478 int networkId = ConvertStringToInt(netrequest.ident);
479 if (networkId <= INVALID_NETWORK_ID || std::to_string(networkId) != netrequest.ident) {
480 WIFI_LOGE("networkId is invaild.");
481 return -1;
482 }
483
484 WIFI_LOGI("RequestNetwork uid[%{public}d], networkId[%{public}d].", netrequest.uid, networkId);
485 if (!WifiAppStateAware::GetInstance().IsForegroundApp(netrequest.uid)) {
486 WIFI_LOGE("App is not in foreground.");
487 return -1;
488 }
489
490 WifiLinkedInfo linkedInfo;
491 WifiConfigCenter::GetInstance().GetLinkedInfo(linkedInfo);
492 if (linkedInfo.connState == ConnState::CONNECTING || linkedInfo.connState == ConnState::CONNECTED) {
493 if (linkedInfo.networkId == networkId) {
494 WIFI_LOGI("RequestNetwork networkId is connecting or connected.");
495 return 0;
496 }
497 }
498
499 if (!WifiNetAgent::GetInstance().RequestNetwork(netrequest.uid, networkId)) {
500 WIFI_LOGE("RequestNetwork fail.");
501 return -1;
502 }
503 #endif
504 return 0;
505 }
506
507 int32_t WifiNetAgent::NetConnCallback::ReleaseNetwork(
508 const std::string &ident, const std::set<NetManagerStandard::NetCap> &netCaps)
509 {
510 WIFI_LOGD("Enter NetConnCallback::ReleaseNetwork");
511 LogNetCaps(ident, netCaps);
512 return 0;
513 }
514
515 void WifiNetAgent::NetConnCallback::LogNetCaps(
516 const std::string &ident, const std::set<NetManagerStandard::NetCap> &netCaps) const
517 {
518 WIFI_LOGD("ident=[%s]", ident.c_str());
519 std::string logStr;
520 const std::string logStrEnd("]");
521 logStr = "netCaps[";
522 for (auto netCap : netCaps) {
523 logStr += std::to_string(static_cast<uint32_t>(netCap));
524 logStr += " ";
525 }
526 logStr += logStrEnd;
527 WIFI_LOGD("%{public}s", logStr.c_str());
528 }
529
530 void WifiNetAgent::RestoreWifiConnection()
531 {
532 using NetManagerStandard::NetBearType;
533 int32_t result = NetConnClient::GetInstance().UpdateSupplierScore(NetBearType::BEARER_WIFI,
534 ACCEPT_UNVALIDATED, supplierId);
535 WIFI_LOGI("Restore Wifi Connection, result:%{public}d", result);
536 }
537 }
538 }
539