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 "dev_interface_state.h"
17
18 #include "inet_addr.h"
19 #include "net_manager_center.h"
20 #include "net_manager_constants.h"
21 #include "netmanager_base_common_utils.h"
22 #include "netmgr_ext_log_wrapper.h"
23 #include "netsys_controller.h"
24 #include "route.h"
25 #include "static_configuration.h"
26
27 namespace OHOS {
28 namespace NetManagerStandard {
29 namespace {
30 constexpr const char *DEFAULT_ROUTE_ADDR = "0.0.0.0";
31 } // namespace
32
DevInterfaceState()33 DevInterfaceState::DevInterfaceState()
34 {
35 netSupplierInfo_ = new (std::nothrow) NetSupplierInfo();
36 if (netSupplierInfo_ == nullptr) {
37 NETMGR_EXT_LOG_E("NetSupplierInfo new failed");
38 }
39 }
40
SetDevName(const std::string & devName)41 void DevInterfaceState::SetDevName(const std::string &devName)
42 {
43 devName_ = devName;
44 }
45
SetNetCaps(const std::set<NetCap> & netCaps)46 void DevInterfaceState::SetNetCaps(const std::set<NetCap> &netCaps)
47 {
48 netCaps_ = netCaps;
49 }
50
SetLinkUp(bool up)51 void DevInterfaceState::SetLinkUp(bool up)
52 {
53 linkUp_ = up;
54 }
55
SetlinkInfo(sptr<NetLinkInfo> & linkInfo)56 void DevInterfaceState::SetlinkInfo(sptr<NetLinkInfo> &linkInfo)
57 {
58 linkInfo_ = linkInfo;
59 }
60
SetIfcfg(sptr<InterfaceConfiguration> & ifCfg)61 void DevInterfaceState::SetIfcfg(sptr<InterfaceConfiguration> &ifCfg)
62 {
63 ifCfg_ = ifCfg;
64 if (ifCfg_->mode_ == STATIC) {
65 UpdateLinkInfo();
66 if (connLinkState_ == LINK_AVAILABLE) {
67 RemoteUpdateNetLinkInfo();
68 }
69 }
70 }
71
SetLancfg(sptr<InterfaceConfiguration> & ifCfg)72 void DevInterfaceState::SetLancfg(sptr<InterfaceConfiguration> &ifCfg)
73 {
74 ifCfg_ = ifCfg;
75 if (ifCfg_->mode_ == LAN_STATIC) {
76 UpdateLanLinkInfo();
77 }
78 }
79
SetDhcpReqState(bool dhcpReqState)80 void DevInterfaceState::SetDhcpReqState(bool dhcpReqState)
81 {
82 dhcpReqState_ = dhcpReqState;
83 }
84
GetDevName() const85 std::string DevInterfaceState::GetDevName() const
86 {
87 return devName_;
88 }
89
GetNetCaps() const90 const std::set<NetCap> &DevInterfaceState::GetNetCaps() const
91 {
92 return netCaps_;
93 }
94
GetNetCaps()95 std::set<NetCap> DevInterfaceState::GetNetCaps()
96 {
97 return netCaps_;
98 }
99
GetLinkUp() const100 bool DevInterfaceState::GetLinkUp() const
101 {
102 return linkUp_;
103 }
104
GetLinkInfo() const105 sptr<NetLinkInfo> DevInterfaceState::GetLinkInfo() const
106 {
107 return linkInfo_;
108 }
109
GetIfcfg() const110 sptr<InterfaceConfiguration> DevInterfaceState::GetIfcfg() const
111 {
112 return ifCfg_;
113 }
114
IsLanIface()115 bool DevInterfaceState::IsLanIface()
116 {
117 if (ifCfg_ == nullptr) {
118 return false;
119 }
120 if (ifCfg_->mode_ == LAN_STATIC || ifCfg_->mode_ == LAN_DHCP) {
121 return true;
122 }
123 return false;
124 }
125
GetIPSetMode() const126 IPSetMode DevInterfaceState::GetIPSetMode() const
127 {
128 if (ifCfg_ == nullptr) {
129 return IPSetMode::STATIC;
130 }
131 return ifCfg_->mode_;
132 }
133
GetDhcpReqState() const134 bool DevInterfaceState::GetDhcpReqState() const
135 {
136 return dhcpReqState_;
137 }
138
RemoteRegisterNetSupplier()139 void DevInterfaceState::RemoteRegisterNetSupplier()
140 {
141 if (connLinkState_ == UNREGISTERED) {
142 if (netCaps_.empty()) {
143 netCaps_.insert(NET_CAPABILITY_INTERNET);
144 }
145 int32_t result =
146 NetManagerCenter::GetInstance().RegisterNetSupplier(bearerType_, devName_, netCaps_, netSupplier_);
147 if (result == NETMANAGER_SUCCESS) {
148 connLinkState_ = REGISTERED;
149 }
150 NETMGR_EXT_LOG_D("DevInterfaceCfg RemoteRegisterNetSupplier netSupplier_[%{public}d]", netSupplier_);
151 }
152 }
153
RemoteUnregisterNetSupplier()154 void DevInterfaceState::RemoteUnregisterNetSupplier()
155 {
156 if (connLinkState_ == UNREGISTERED) {
157 return;
158 }
159 int ret = NetManagerCenter::GetInstance().UnregisterNetSupplier(netSupplier_);
160 if (ret == NETMANAGER_SUCCESS) {
161 connLinkState_ = UNREGISTERED;
162 netSupplier_ = 0;
163 }
164 }
165
RemoteUpdateNetLinkInfo()166 void DevInterfaceState::RemoteUpdateNetLinkInfo()
167 {
168 if (connLinkState_ == LINK_UNAVAILABLE) {
169 NETMGR_EXT_LOG_E("DevInterfaceCfg RemoteUpdateNetLinkInfo regState_:LINK_UNAVAILABLE");
170 return;
171 }
172 if (linkInfo_ == nullptr) {
173 NETMGR_EXT_LOG_E("DevInterfaceCfg RemoteUpdateNetLinkInfo linkInfo_ is nullptr");
174 return;
175 }
176 NetManagerCenter::GetInstance().UpdateNetLinkInfo(netSupplier_, linkInfo_);
177 }
178
RemoteUpdateNetSupplierInfo()179 void DevInterfaceState::RemoteUpdateNetSupplierInfo()
180 {
181 if (connLinkState_ == UNREGISTERED) {
182 NETMGR_EXT_LOG_E("DevInterfaceCfg RemoteUpdateNetSupplierInfo regState_:UNREGISTERED");
183 return;
184 }
185 if (netSupplierInfo_ == nullptr) {
186 NETMGR_EXT_LOG_E("DevInterfaceCfg RemoteUpdateNetSupplierInfo netSupplierInfo_ is nullptr");
187 return;
188 }
189 UpdateSupplierAvailable();
190 NetManagerCenter::GetInstance().UpdateNetSupplierInfo(netSupplier_, netSupplierInfo_);
191 }
192
UpdateNetHttpProxy(const HttpProxy & httpProxy)193 void DevInterfaceState::UpdateNetHttpProxy(const HttpProxy &httpProxy)
194 {
195 if (httpProxy == ifCfg_->httpProxy_) {
196 NETMGR_EXT_LOG_E("The currently set http proxy is the same as the entered http proxy");
197 return;
198 }
199 ifCfg_->httpProxy_ = httpProxy;
200 if (connLinkState_ == LINK_AVAILABLE) {
201 if (linkInfo_ == nullptr) {
202 NETMGR_EXT_LOG_E("linkInfo_ is nullptr");
203 return;
204 }
205 linkInfo_->httpProxy_ = httpProxy;
206 RemoteUpdateNetLinkInfo();
207 }
208 }
209
UpdateLinkInfo()210 void DevInterfaceState::UpdateLinkInfo()
211 {
212 if (ifCfg_ == nullptr || ifCfg_->mode_ != STATIC) {
213 return;
214 }
215 if (linkInfo_ == nullptr) {
216 linkInfo_ = new (std::nothrow) NetLinkInfo();
217 if (linkInfo_ == nullptr) {
218 NETMGR_EXT_LOG_E("linkInfo_ is nullptr");
219 return;
220 }
221 }
222 std::list<INetAddr>().swap(linkInfo_->netAddrList_);
223 std::list<Route>().swap(linkInfo_->routeList_);
224 std::list<INetAddr>().swap(linkInfo_->dnsList_);
225 linkInfo_->ifaceName_ = devName_;
226 for (const auto &ipAddr : ifCfg_->ipStatic_.ipAddrList_) {
227 linkInfo_->netAddrList_.push_back(ipAddr);
228 }
229
230 for (const auto &netAddr : ifCfg_->ipStatic_.routeList_) {
231 Route route;
232 route.iface_ = devName_;
233 route.destination_ = netAddr;
234 GetTargetNetAddrWithSameFamily(netAddr.address_, ifCfg_->ipStatic_.gatewayList_, route.gateway_);
235 linkInfo_->routeList_.push_back(route);
236 }
237 CreateLocalRoute(devName_, ifCfg_->ipStatic_.ipAddrList_, ifCfg_->ipStatic_.netMaskList_);
238
239 for (auto dnsServer : ifCfg_->ipStatic_.dnsServers_) {
240 linkInfo_->dnsList_.push_back(dnsServer);
241 }
242 linkInfo_->httpProxy_ = ifCfg_->httpProxy_;
243 }
244
UpdateLanLinkInfo()245 void DevInterfaceState::UpdateLanLinkInfo()
246 {
247 if (ifCfg_ == nullptr || ifCfg_->mode_ != LAN_STATIC) {
248 return;
249 }
250 if (linkInfo_ == nullptr) {
251 linkInfo_ = new (std::nothrow) NetLinkInfo();
252 if (linkInfo_ == nullptr) {
253 NETMGR_EXT_LOG_E("linkInfo_ is nullptr");
254 return;
255 }
256 }
257 std::list<INetAddr>().swap(linkInfo_->netAddrList_);
258 std::list<Route>().swap(linkInfo_->routeList_);
259 linkInfo_->ifaceName_ = devName_;
260 for (const auto &ipAddr : ifCfg_->ipStatic_.ipAddrList_) {
261 linkInfo_->netAddrList_.push_back(ipAddr);
262 }
263
264 for (const auto &netAddr : ifCfg_->ipStatic_.routeList_) {
265 Route route;
266 route.iface_ = devName_;
267 route.destination_ = netAddr;
268 GetRoutePrefixlen(netAddr.address_, ifCfg_->ipStatic_.netMaskList_, route.destination_);
269 GetTargetNetAddrWithSameFamily(netAddr.address_, ifCfg_->ipStatic_.gatewayList_, route.gateway_);
270 linkInfo_->routeList_.push_back(route);
271 }
272 }
273
UpdateLanLinkInfo(const sptr<StaticConfiguration> & config)274 void DevInterfaceState::UpdateLanLinkInfo(const sptr<StaticConfiguration> &config)
275 {
276 if (config == nullptr) {
277 NETMGR_EXT_LOG_E("config is nullptr");
278 return;
279 }
280 if (linkInfo_ == nullptr) {
281 linkInfo_ = new (std::nothrow) NetLinkInfo();
282 if (linkInfo_ == nullptr) {
283 NETMGR_EXT_LOG_E("NetLinkInfo new failed");
284 }
285 }
286 std::list<INetAddr>().swap(linkInfo_->netAddrList_);
287 std::list<Route>().swap(linkInfo_->routeList_);
288 linkInfo_->ifaceName_ = devName_;
289 for (const auto &ipAddr : config->ipAddrList_) {
290 linkInfo_->netAddrList_.push_back(ipAddr);
291 }
292
293 for (const auto &routeAddr : config->routeList_) {
294 Route routeStc;
295 routeStc.iface_ = devName_;
296 routeStc.destination_ = routeAddr;
297 GetRoutePrefixlen(routeAddr.address_, config->netMaskList_, routeStc.destination_);
298 GetTargetNetAddrWithSameFamily(routeAddr.address_, config->gatewayList_, routeStc.gateway_);
299 linkInfo_->routeList_.push_back(routeStc);
300 }
301 }
302
UpdateLinkInfo(const sptr<StaticConfiguration> & config)303 void DevInterfaceState::UpdateLinkInfo(const sptr<StaticConfiguration> &config)
304 {
305 if (config == nullptr) {
306 NETMGR_EXT_LOG_E("config is nullptr");
307 return;
308 }
309 if (linkInfo_ == nullptr) {
310 linkInfo_ = new (std::nothrow) NetLinkInfo();
311 if (linkInfo_ == nullptr) {
312 NETMGR_EXT_LOG_E("NetLinkInfo new failed");
313 }
314 }
315
316 std::list<INetAddr>().swap(linkInfo_->netAddrList_);
317 std::list<Route>().swap(linkInfo_->routeList_);
318 std::list<INetAddr>().swap(linkInfo_->dnsList_);
319 linkInfo_->ifaceName_ = devName_;
320 for (const auto &ipAddr : config->ipAddrList_) {
321 linkInfo_->netAddrList_.push_back(ipAddr);
322 }
323
324 for (const auto &routeAddr : config->routeList_) {
325 Route routeStc;
326 routeStc.iface_ = devName_;
327 routeStc.destination_ = routeAddr;
328 GetTargetNetAddrWithSameFamily(routeAddr.address_, config->gatewayList_, routeStc.gateway_);
329 linkInfo_->routeList_.push_back(routeStc);
330 }
331 CreateLocalRoute(devName_, config->ipAddrList_, config->netMaskList_);
332
333 for (auto dns : config->dnsServers_) {
334 linkInfo_->dnsList_.push_back(dns);
335 }
336 if (ifCfg_) {
337 linkInfo_->httpProxy_ = ifCfg_->httpProxy_;
338 }
339 }
340
UpdateSupplierAvailable()341 void DevInterfaceState::UpdateSupplierAvailable()
342 {
343 netSupplierInfo_->isAvailable_ = linkUp_;
344 connLinkState_ = linkUp_ ? LINK_AVAILABLE : LINK_UNAVAILABLE;
345 }
346
CreateLocalRoute(const std::string & iface,const std::vector<INetAddr> & ipAddrList,const std::vector<INetAddr> & netMaskList)347 void DevInterfaceState::CreateLocalRoute(const std::string &iface, const std::vector<INetAddr> &ipAddrList,
348 const std::vector<INetAddr> &netMaskList)
349 {
350 if (linkInfo_ == nullptr) {
351 NETMGR_EXT_LOG_E("linkInfo_ is nullptr");
352 return;
353 }
354
355 for (const auto &ipAddr : ipAddrList) {
356 auto family = CommonUtils::GetAddrFamily(ipAddr.address_);
357 std::string routeAddr = (family == AF_INET6) ? CommonUtils::GetIpv6Prefix(ipAddr.address_, ipAddr.prefixlen_)
358 : GetIpv4Prefix(ipAddr.address_, netMaskList);
359 Route localRoute;
360 localRoute.iface_ = iface;
361 localRoute.destination_.type_ = family;
362 localRoute.destination_.address_ = routeAddr;
363 localRoute.destination_.prefixlen_ = ipAddr.prefixlen_;
364 localRoute.gateway_.address_ = (family == AF_INET) ? DEFAULT_ROUTE_ADDR : "";
365 linkInfo_->routeList_.push_back(localRoute);
366 }
367 }
368
GetIpv4Prefix(const std::string & ipv4Addr,const std::vector<INetAddr> & netMaskList)369 std::string DevInterfaceState::GetIpv4Prefix(const std::string &ipv4Addr, const std::vector<INetAddr> &netMaskList)
370 {
371 INetAddr maskAddr;
372 GetTargetNetAddrWithSameFamily(ipv4Addr, netMaskList, maskAddr);
373 uint32_t ipInt = CommonUtils::ConvertIpv4Address(ipv4Addr);
374 uint32_t maskInt = CommonUtils::ConvertIpv4Address(maskAddr.address_);
375 return CommonUtils::ConvertIpv4Address(ipInt & maskInt);
376 }
377
GetTargetNetAddrWithSameFamily(const std::string & bySrcAddr,const std::vector<INetAddr> & fromAddrList,INetAddr & targetNetAddr)378 void DevInterfaceState::GetTargetNetAddrWithSameFamily(const std::string &bySrcAddr,
379 const std::vector<INetAddr> &fromAddrList,
380 INetAddr &targetNetAddr)
381 {
382 auto family = CommonUtils::GetAddrFamily(bySrcAddr);
383 for (const auto &addr : fromAddrList) {
384 if (family != CommonUtils::GetAddrFamily(addr.address_)) {
385 continue;
386 }
387 targetNetAddr = addr;
388 return;
389 }
390 }
391
GetRoutePrefixlen(const std::string & bySrcAddr,const std::vector<INetAddr> & fromAddrList,INetAddr & targetNetAddr)392 void DevInterfaceState::GetRoutePrefixlen(const std::string &bySrcAddr,
393 const std::vector<INetAddr> &fromAddrList,
394 INetAddr &targetNetAddr)
395 {
396 auto route_family = CommonUtils::GetAddrFamily(bySrcAddr);
397 for (const auto &netMask : fromAddrList) {
398 auto route_mask_family = CommonUtils::GetAddrFamily(netMask.address_);
399 if (route_family == route_mask_family) {
400 targetNetAddr.prefixlen_ = (route_family == AF_INET6)
401 ? static_cast<uint32_t>(CommonUtils::Ipv6PrefixLen(netMask.address_))
402 : static_cast<uint32_t>(CommonUtils::Ipv4PrefixLen(netMask.address_));
403 }
404 }
405 }
406
GetDumpInfo(std::string & info)407 void DevInterfaceState::GetDumpInfo(std::string &info)
408 {
409 const std::string TAB = " ";
410 std::list<std::string> dumpInfo = {
411 "DevName: " + devName_,
412 "ConnLinkState: " + std::to_string(connLinkState_),
413 "LinkUp: " + std::to_string(linkUp_),
414 "DHCPReqState: " + std::to_string(dhcpReqState_),
415 };
416 std::string data = "DevInterfaceState: \n";
417 std::for_each(dumpInfo.begin(), dumpInfo.end(),
418 [&data, &TAB](const auto &msg) { data.append(TAB + TAB + msg + "\n"); });
419 if (linkInfo_ != nullptr) {
420 data.append(linkInfo_->ToString(TAB) + "\n");
421 }
422 if (netSupplierInfo_ != nullptr) {
423 data.append(netSupplierInfo_->ToString(TAB) + "\n");
424 }
425 if (ifCfg_ != nullptr) {
426 data.append("\n" + TAB + TAB + "InterfaceConfig: \n" + TAB + TAB + TAB +
427 "Mode: " + std::to_string(ifCfg_->mode_) + "\n");
428 data.append("\nConfig: \n");
429 data.append(TAB + TAB + "IpAddr: ");
430 std::for_each(ifCfg_->ipStatic_.ipAddrList_.begin(), ifCfg_->ipStatic_.ipAddrList_.end(),
431 [&data, &TAB](const auto &ipAddr) { data.append(TAB + TAB + ipAddr.ToString(TAB)); });
432
433 data.append("\n" + TAB + TAB + "Route: ");
434 std::for_each(ifCfg_->ipStatic_.routeList_.begin(), ifCfg_->ipStatic_.routeList_.end(),
435 [&data, &TAB](const auto &routeAddr) { data.append(TAB + TAB + routeAddr.ToString(TAB)); });
436
437 data.append("\n" + TAB + TAB + "GateWay: ");
438 std::for_each(ifCfg_->ipStatic_.gatewayList_.begin(), ifCfg_->ipStatic_.gatewayList_.end(),
439 [&data, &TAB](const auto &gateway) { data.append(TAB + TAB + gateway.ToString(TAB)); });
440
441 data.append("\n" + TAB + TAB + "NetMask: ");
442 std::for_each(ifCfg_->ipStatic_.netMaskList_.begin(), ifCfg_->ipStatic_.netMaskList_.end(),
443 [&data, &TAB](const auto &netMask) { data.append(TAB + TAB + netMask.ToString(TAB)); });
444
445 data.append("\n" + TAB + TAB + "DNSServers: ");
446 std::for_each(ifCfg_->ipStatic_.dnsServers_.begin(), ifCfg_->ipStatic_.dnsServers_.end(),
447 [&data, &TAB](const auto &server) { data.append(TAB + TAB + server.ToString(TAB)); });
448
449 data.append("\n" + TAB + TAB + "Domain: " + ifCfg_->ipStatic_.domain_ + "\n" + TAB + TAB + "NetCaps: {");
450 std::for_each(netCaps_.begin(), netCaps_.end(),
451 [&data, &TAB](const auto &cap) { data.append(std::to_string(cap) + ", "); });
452 data.append("}\n");
453 }
454 data.append(TAB + TAB + "BearerType :" + std::to_string(bearerType_) + "\n");
455 info.append(data);
456 }
457 } // namespace NetManagerStandard
458 } // namespace OHOS
459