• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 "ethernet_management.h"
17 
18 #include <regex>
19 #include <thread>
20 #include <pthread.h>
21 #include <unistd.h>
22 
23 #include "net_manager_constants.h"
24 #include "netmgr_ext_log_wrapper.h"
25 #include "netsys_controller.h"
26 #include "securec.h"
27 
28 namespace OHOS {
29 namespace NetManagerStandard {
30 const std::string IFACE_MATCH = "eth\\d";
31 constexpr const char *IFACE_LINK_UP = "up";
32 constexpr const char *IFACE_RUNNING = "running";
33 constexpr int SLEEP_TIME_S = 2;
OnDhcpSuccess(EthernetDhcpCallback::DhcpResult & dhcpResult)34 int32_t EthernetManagement::EhternetDhcpNotifyCallback::OnDhcpSuccess(EthernetDhcpCallback::DhcpResult &dhcpResult)
35 {
36     ethernetManagement_.UpdateDevInterfaceLinkInfo(dhcpResult);
37     return 0;
38 }
39 
OnInterfaceAddressUpdated(const std::string &,const std::string &,int,int)40 int32_t EthernetManagement::DevInterfaceStateCallback::OnInterfaceAddressUpdated(const std::string &,
41                                                                                  const std::string &, int, int)
42 {
43     return 0;
44 }
45 
OnInterfaceAddressRemoved(const std::string &,const std::string &,int,int)46 int32_t EthernetManagement::DevInterfaceStateCallback::OnInterfaceAddressRemoved(const std::string &,
47                                                                                  const std::string &, int, int)
48 {
49     return 0;
50 }
51 
OnInterfaceAdded(const std::string & iface)52 int32_t EthernetManagement::DevInterfaceStateCallback::OnInterfaceAdded(const std::string &iface)
53 {
54     std::regex re(IFACE_MATCH);
55     if (std::regex_search(iface, re)) {
56         ethernetManagement_.DevInterfaceAdd(iface);
57         if (NetsysController::GetInstance().SetInterfaceUp(iface) != ERR_NONE) {
58             NETMGR_EXT_LOG_E("Iface[%{public}s] added set up fail!", iface.c_str());
59         }
60     }
61     return 0;
62 }
63 
OnInterfaceRemoved(const std::string & iface)64 int32_t EthernetManagement::DevInterfaceStateCallback::OnInterfaceRemoved(const std::string &iface)
65 {
66     std::regex re(IFACE_MATCH);
67     if (std::regex_search(iface, re)) {
68         ethernetManagement_.DevInterfaceRemove(iface);
69         if (NetsysController::GetInstance().SetInterfaceDown(iface) != ERR_NONE) {
70             NETMGR_EXT_LOG_E("Iface[%{public}s] added set down fail!", iface.c_str());
71         }
72     }
73     return 0;
74 }
75 
OnInterfaceChanged(const std::string &,bool)76 int32_t EthernetManagement::DevInterfaceStateCallback::OnInterfaceChanged(const std::string &, bool)
77 {
78     return 0;
79 }
80 
OnInterfaceLinkStateChanged(const std::string & ifName,bool up)81 int32_t EthernetManagement::DevInterfaceStateCallback::OnInterfaceLinkStateChanged(const std::string &ifName, bool up)
82 {
83     auto startTime = std::chrono::steady_clock::now();
84     std::regex re(IFACE_MATCH);
85     if (std::regex_search(ifName, re)) {
86         ethernetManagement_.UpdateInterfaceState(ifName, up);
87     }
88     auto endTime = std::chrono::steady_clock::now();
89     auto durationNs = std::chrono::duration_cast<std::chrono::nanoseconds>(endTime - startTime);
90     NETMGR_EXT_LOG_I("OnInterfaceLinkStateChanged iface[%{public}s] up[%{public}d], cost=%{public}lld",
91         ifName.c_str(), up, durationNs.count());
92     return 0;
93 }
94 
OnRouteChanged(bool,const std::string &,const std::string &,const std::string &)95 int32_t EthernetManagement::DevInterfaceStateCallback::OnRouteChanged(bool, const std::string &, const std::string &,
96                                                                       const std::string &)
97 {
98     return 0;
99 }
100 
OnDhcpSuccess(NetsysControllerCallback::DhcpResult & dhcpResult)101 int32_t EthernetManagement::DevInterfaceStateCallback::OnDhcpSuccess(NetsysControllerCallback::DhcpResult &dhcpResult)
102 {
103     return 0;
104 }
105 
OnBandwidthReachedLimit(const std::string & limitName,const std::string & iface)106 int32_t EthernetManagement::DevInterfaceStateCallback::OnBandwidthReachedLimit(const std::string &limitName,
107                                                                                const std::string &iface)
108 {
109     return 0;
110 }
111 
GetInstance()112 EthernetManagement &EthernetManagement::GetInstance()
113 {
114     static EthernetManagement gInstance;
115     return gInstance;
116 }
117 
EthernetManagement()118 EthernetManagement::EthernetManagement()
119 {
120     ethDhcpController_ = std::make_unique<EthernetDhcpController>();
121     ethDhcpNotifyCallback_ = new (std::nothrow) EhternetDhcpNotifyCallback(*this);
122     if (ethDhcpNotifyCallback_ != nullptr) {
123         ethDhcpController_->RegisterDhcpCallback(ethDhcpNotifyCallback_);
124     }
125 
126     ethDevInterfaceStateCallback_ = new (std::nothrow) DevInterfaceStateCallback(*this);
127     ethConfiguration_ = std::make_unique<EthernetConfiguration>();
128     ethConfiguration_->ReadSystemConfiguration(devCaps_, devCfgs_);
129     ethLanManageMent_ = std::make_unique<EthernetLanManagement>();
130 }
131 
132 EthernetManagement::~EthernetManagement() = default;
133 
UpdateInterfaceState(const std::string & dev,bool up)134 void EthernetManagement::UpdateInterfaceState(const std::string &dev, bool up)
135 {
136     NETMGR_EXT_LOG_D("EthernetManagement UpdateInterfaceState dev[%{public}s] up[%{public}d]", dev.c_str(), up);
137     std::unique_lock<std::mutex> lock(mutex_);
138     auto fit = devs_.find(dev);
139     if (fit == devs_.end()) {
140         return;
141     }
142     sptr<DevInterfaceState> devState = fit->second;
143     if (devState == nullptr) {
144         NETMGR_EXT_LOG_E("devState is nullptr");
145         return;
146     }
147     devState->SetLinkUp(up);
148     IPSetMode mode = devState->GetIPSetMode();
149     bool dhcpReqState = devState->GetDhcpReqState();
150     NETMGR_EXT_LOG_D("EthernetManagement UpdateInterfaceState mode[%{public}d] dhcpReqState[%{public}d]",
151                      static_cast<int32_t>(mode), dhcpReqState);
152     if (up) {
153         if (!devState->IsLanIface()) {
154             devState->RemoteUpdateNetSupplierInfo();
155         }
156         if ((mode == DHCP || mode == LAN_DHCP) && !dhcpReqState) {
157             StartDhcpClient(dev, devState);
158         } else {
159             if (devState->IsLanIface()) {
160                 ethLanManageMent_->UpdateLanLinkInfo(devState);
161             } else {
162                 devState->RemoteUpdateNetLinkInfo();
163             }
164         }
165     } else {
166         if ((mode == DHCP || mode == LAN_DHCP) && dhcpReqState) {
167             StopDhcpClient(dev, devState);
168         }
169         if (devState->IsLanIface()) {
170             ethLanManageMent_->ReleaseLanNetLink(devState);
171         } else {
172             devState->RemoteUpdateNetSupplierInfo();
173         }
174         netLinkConfigs_[dev] = nullptr;
175     }
176 }
177 
UpdateDevInterfaceCfg(const std::string & iface,sptr<InterfaceConfiguration> cfg)178 int32_t EthernetManagement::UpdateDevInterfaceCfg(const std::string &iface, sptr<InterfaceConfiguration> cfg)
179 {
180     if (cfg == nullptr) {
181         NETMGR_EXT_LOG_E("cfg is nullptr");
182         return NETMANAGER_EXT_ERR_LOCAL_PTR_NULL;
183     }
184     std::unique_lock<std::mutex> lock(mutex_);
185     auto fit = devs_.find(iface);
186     if (fit == devs_.end() || fit->second == nullptr) {
187         NETMGR_EXT_LOG_E("The iface[%{public}s] device or device information does not exist", iface.c_str());
188         return ETHERNET_ERR_DEVICE_INFORMATION_NOT_EXIST;
189     }
190     if (!fit->second->GetLinkUp()) {
191         NETMGR_EXT_LOG_E("The iface[%{public}s] device is unlink", iface.c_str());
192         return ETHERNET_ERR_DEVICE_NOT_LINK;
193     }
194     if (!ModeInputCheck(fit->second->GetIfcfg()->mode_, cfg->mode_)) {
195         NETMGR_EXT_LOG_E("The iface[%{public}s] device can not exchange between WAN and LAN", iface.c_str());
196         return NETMANAGER_ERR_INVALID_PARAMETER;
197     }
198     if (!ethConfiguration_->WriteUserConfiguration(iface, cfg)) {
199         NETMGR_EXT_LOG_E("EthernetManagement write user configurations error!");
200         return ETHERNET_ERR_USER_CONIFGURATION_WRITE_FAIL;
201     }
202     if (fit->second->GetIfcfg()->mode_ != cfg->mode_) {
203         if (cfg->mode_ == DHCP || cfg->mode_ == LAN_DHCP) {
204             StartDhcpClient(iface, fit->second);
205         } else {
206             StopDhcpClient(iface, fit->second);
207             netLinkConfigs_[iface] = nullptr;
208         }
209     } else if (cfg->mode_ == DHCP) {
210         fit->second->UpdateNetHttpProxy(cfg->httpProxy_);
211     }
212     if (fit->second->IsLanIface()) {
213         ethLanManageMent_->GetOldLinkInfo(fit->second);
214         fit->second->SetLancfg(cfg);
215         ethLanManageMent_->UpdateLanLinkInfo(fit->second);
216     } else {
217         fit->second->SetIfcfg(cfg);
218     }
219     devCfgs_[iface] = cfg;
220     return NETMANAGER_EXT_SUCCESS;
221 }
222 
UpdateDevInterfaceLinkInfo(EthernetDhcpCallback::DhcpResult & dhcpResult)223 int32_t EthernetManagement::UpdateDevInterfaceLinkInfo(EthernetDhcpCallback::DhcpResult &dhcpResult)
224 {
225     NETMGR_EXT_LOG_D("EthernetManagement::UpdateDevInterfaceLinkInfo");
226     std::lock_guard<std::mutex> locker(mutex_);
227     auto fit = devs_.find(dhcpResult.iface);
228     if (fit == devs_.end() || fit->second == nullptr) {
229         NETMGR_EXT_LOG_E("The iface[%{public}s] device or device information does not exist", dhcpResult.iface.c_str());
230         return ETHERNET_ERR_DEVICE_INFORMATION_NOT_EXIST;
231     }
232     if (!fit->second->GetLinkUp()) {
233         NETMGR_EXT_LOG_E("The iface[%{public}s] The device is not turned on", dhcpResult.iface.c_str());
234         return ETHERNET_ERR_DEVICE_NOT_LINK;
235     }
236 
237     IPSetMode mode = fit->second->GetIPSetMode();
238     if (mode == IPSetMode::STATIC || mode == IPSetMode::LAN_STATIC) {
239         NETMGR_EXT_LOG_E("The iface[%{public}s] set mode is STATIC now", dhcpResult.iface.c_str());
240         return ETHERNET_ERR_DEVICE_NOT_LINK;
241     }
242 
243     auto &config = netLinkConfigs_[dhcpResult.iface];
244     if (config == nullptr) {
245         config = new (std::nothrow) StaticConfiguration();
246         if (config == nullptr) {
247             NETMGR_EXT_LOG_E("Iface:%{public}s's link info config is nullptr", dhcpResult.iface.c_str());
248         }
249     }
250 
251     if (!ethConfiguration_->ConvertToConfiguration(dhcpResult, config)) {
252         NETMGR_EXT_LOG_E("EthernetManagement dhcp convert to configurations error!");
253         return ETHERNET_ERR_CONVERT_CONFIGURATINO_FAIL;
254     }
255     if (fit->second->IsLanIface()) {
256         ethLanManageMent_->GetOldLinkInfo(fit->second);
257         fit->second->UpdateLanLinkInfo(config);
258         ethLanManageMent_->UpdateLanLinkInfo(fit->second);
259     } else {
260         fit->second->UpdateLinkInfo(config);
261         fit->second->RemoteUpdateNetLinkInfo();
262     }
263     return NETMANAGER_EXT_SUCCESS;
264 }
265 
GetDevInterfaceCfg(const std::string & iface,sptr<InterfaceConfiguration> & ifaceConfig)266 int32_t EthernetManagement::GetDevInterfaceCfg(const std::string &iface, sptr<InterfaceConfiguration> &ifaceConfig)
267 {
268     std::unique_lock<std::mutex> lock(mutex_);
269     auto fit = devs_.find(iface);
270     if (fit == devs_.end() || fit->second == nullptr) {
271         NETMGR_EXT_LOG_E("The iface[%{public}s] device does not exist", iface.c_str());
272         return ETHERNET_ERR_DEVICE_INFORMATION_NOT_EXIST;
273     }
274     if (!fit->second->GetLinkUp()) {
275         ifaceConfig = fit->second->GetIfcfg();
276         return NETMANAGER_EXT_SUCCESS;
277     }
278     auto temp = ethConfiguration_->MakeInterfaceConfiguration(fit->second->GetIfcfg(), fit->second->GetLinkInfo());
279     if (temp == nullptr) {
280         return ETHERNET_ERR_DEVICE_INFORMATION_NOT_EXIST;
281     }
282     *ifaceConfig = *temp;
283     return NETMANAGER_EXT_SUCCESS;
284 }
285 
IsIfaceActive(const std::string & iface,int32_t & activeStatus)286 int32_t EthernetManagement::IsIfaceActive(const std::string &iface, int32_t &activeStatus)
287 {
288     std::unique_lock<std::mutex> lock(mutex_);
289     auto fit = devs_.find(iface);
290     if (fit == devs_.end() || fit->second == nullptr) {
291         NETMGR_EXT_LOG_E("The iface[%{public}s] device does not exist", iface.c_str());
292         return ETHERNET_ERR_DEVICE_INFORMATION_NOT_EXIST;
293     }
294     activeStatus = static_cast<int32_t>(fit->second->GetLinkUp());
295     return NETMANAGER_EXT_SUCCESS;
296 }
297 
GetAllActiveIfaces(std::vector<std::string> & activeIfaces)298 int32_t EthernetManagement::GetAllActiveIfaces(std::vector<std::string> &activeIfaces)
299 {
300     std::unique_lock<std::mutex> lock(mutex_);
301     for (auto it = devs_.begin(); it != devs_.end(); ++it) {
302         if (it->second->GetLinkUp()) {
303             activeIfaces.push_back(it->first);
304         }
305     }
306     return NETMANAGER_EXT_SUCCESS;
307 }
308 
ResetFactory()309 int32_t EthernetManagement::ResetFactory()
310 {
311     if (!ethConfiguration_->ClearAllUserConfiguration()) {
312         NETMGR_EXT_LOG_E("Failed to ResetFactory!");
313         return ETHERNET_ERR_USER_CONIFGURATION_CLEAR_FAIL;
314     }
315     NETMGR_EXT_LOG_I("Success to ResetFactory!");
316     return NETMANAGER_EXT_SUCCESS;
317 }
318 
Init()319 void EthernetManagement::Init()
320 {
321     static const unsigned int SLEEP_TIME = 4;
322     std::this_thread::sleep_for(std::chrono::seconds(SLEEP_TIME));
323     if (ethDevInterfaceStateCallback_ != nullptr) {
324         NetsysController::GetInstance().RegisterCallback(ethDevInterfaceStateCallback_);
325     }
326     std::regex re(IFACE_MATCH);
327     std::vector<std::string> ifaces = NetsysController::GetInstance().InterfaceGetList();
328     if (ifaces.empty()) {
329         NETMGR_EXT_LOG_E("EthernetManagement link list is empty");
330         return;
331     }
332     NETMGR_EXT_LOG_D("EthernetManagement devs size[%{public}zd]", ifaces.size());
333     if (!ethConfiguration_->ReadUserConfiguration(devCfgs_)) {
334         NETMGR_EXT_LOG_E("EthernetManagement read user configurations error! ");
335         return;
336     }
337     for (const auto &devName : ifaces) {
338         NETMGR_EXT_LOG_D("EthernetManagement devName[%{public}s]", devName.c_str());
339         if (!std::regex_search(devName, re)) {
340             continue;
341         }
342         DevInterfaceAdd(devName);
343     }
344     std::thread t(&EthernetManagement::StartSetDevUpThd, &EthernetManagement::GetInstance());
345     std::string threadName = "SetDevUpThd";
346     pthread_setname_np(t.native_handle(), threadName.c_str());
347     t.detach();
348     NETMGR_EXT_LOG_D("EthernetManagement devs_ size[%{public}zd", devs_.size());
349 }
350 
StartSetDevUpThd()351 void EthernetManagement::StartSetDevUpThd()
352 {
353     NETMGR_EXT_LOG_D("EthernetManagement StartSetDevUpThd in.");
354     std::map<std::string, sptr<DevInterfaceState>> tempDevMap;
355     {
356         std::unique_lock<std::mutex> lock(mutex_);
357         tempDevMap = devs_;
358     }
359 
360     for (auto &dev : tempDevMap) {
361         std::string devName = dev.first;
362         if (IsIfaceLinkUp(devName)) {
363             continue;
364         }
365         while (true) {
366             if (NetsysController::GetInstance().SetInterfaceUp(devName) != ERR_NONE) {
367                 sleep(SLEEP_TIME_S);
368                 continue;
369             }
370             break;
371         }
372     }
373 }
374 
IsIfaceLinkUp(const std::string & iface)375 bool EthernetManagement::IsIfaceLinkUp(const std::string &iface)
376 {
377     OHOS::nmd::InterfaceConfigurationParcel config;
378     config.ifName = iface;
379     if (NetsysController::GetInstance().GetInterfaceConfig(config) != ERR_NONE) {
380         return false;
381     }
382     if (std::find(config.flags.begin(), config.flags.end(), IFACE_LINK_UP) == config.flags.end() ||
383         std::find(config.flags.begin(), config.flags.end(), IFACE_RUNNING) == config.flags.end()) {
384         return false;
385     }
386     UpdateInterfaceState(iface, true);
387     return true;
388 }
389 
StartDhcpClient(const std::string & dev,sptr<DevInterfaceState> & devState)390 void EthernetManagement::StartDhcpClient(const std::string &dev, sptr<DevInterfaceState> &devState)
391 {
392     NETMGR_EXT_LOG_D("EthernetManagement StartDhcpClient[%{public}s]", dev.c_str());
393     ethDhcpController_->StartClient(dev, true);
394     devState->SetDhcpReqState(true);
395 }
396 
StopDhcpClient(const std::string & dev,sptr<DevInterfaceState> & devState)397 void EthernetManagement::StopDhcpClient(const std::string &dev, sptr<DevInterfaceState> &devState)
398 {
399     NETMGR_EXT_LOG_D("EthernetManagement StopDhcpClient[%{public}s]", dev.c_str());
400     ethDhcpController_->StopClient(dev, true);
401     devState->SetDhcpReqState(false);
402 }
403 
DevInterfaceAdd(const std::string & devName)404 void EthernetManagement::DevInterfaceAdd(const std::string &devName)
405 {
406     NETMGR_EXT_LOG_D("Interface name:[%{public}s] add.", devName.c_str());
407     std::unique_lock<std::mutex> lock(mutex_);
408     auto fitDev = devs_.find(devName);
409     if (fitDev != devs_.end()) {
410         NETMGR_EXT_LOG_E("Interface name:[%{public}s] has added.", devName.c_str());
411         return;
412     }
413     sptr<DevInterfaceState> devState = new (std::nothrow) DevInterfaceState();
414     if (devState == nullptr) {
415         NETMGR_EXT_LOG_E("devState is nullptr");
416         return;
417     }
418     ethConfiguration_->ReadSystemConfiguration(devCaps_, devCfgs_);
419     devs_.insert(std::make_pair(devName, devState));
420     devState->SetDevName(devName);
421     auto fitCfg = devCfgs_.find(devName);
422     if (fitCfg != devCfgs_.end()) {
423         if (fitCfg->second->mode_ == LAN_STATIC || fitCfg->second->mode_ == LAN_DHCP) {
424             NETMGR_EXT_LOG_D("Lan Interface name:[%{public}s] add, mode [%{public}d]",
425                              devName.c_str(), fitCfg->second->mode_);
426             devState->SetLancfg(fitCfg->second);
427             ethLanManageMent_->UpdateLanLinkInfo(devState);
428             return;
429         }
430         devState->RemoteRegisterNetSupplier();
431         devState->SetIfcfg(fitCfg->second);
432     } else {
433         sptr<InterfaceConfiguration> ifCfg = new (std::nothrow) InterfaceConfiguration();
434         if (ifCfg == nullptr) {
435             NETMGR_EXT_LOG_E("ifCfg is nullptr");
436             return;
437         }
438         ifCfg->mode_ = DHCP;
439         devState->RemoteRegisterNetSupplier();
440         devState->SetIfcfg(ifCfg);
441     }
442     auto fitCap = devCaps_.find(devName);
443     if (fitCap != devCaps_.end()) {
444         devState->SetNetCaps(fitCap->second);
445     }
446 }
447 
DevInterfaceRemove(const std::string & devName)448 void EthernetManagement::DevInterfaceRemove(const std::string &devName)
449 {
450     NETMGR_EXT_LOG_D("Interface name:[%{public}s] remove.", devName.c_str());
451     std::unique_lock<std::mutex> lock(mutex_);
452     auto fitDev = devs_.find(devName);
453     if (fitDev != devs_.end()) {
454         if (fitDev->second != nullptr) {
455             fitDev->second->RemoteUnregisterNetSupplier();
456         }
457         devs_.erase(fitDev);
458     }
459 }
460 
GetDumpInfo(std::string & info)461 void EthernetManagement::GetDumpInfo(std::string &info)
462 {
463     std::for_each(devs_.begin(), devs_.end(), [&info](const auto &dev) { dev.second->GetDumpInfo(info); });
464 }
465 
ModeInputCheck(IPSetMode origin,IPSetMode input)466 bool EthernetManagement::ModeInputCheck(IPSetMode origin, IPSetMode input)
467 {
468     if (origin == STATIC || origin == DHCP) {
469         if (input == LAN_STATIC || input == LAN_DHCP) {
470             return false;
471         }
472     } else if (origin == LAN_STATIC || origin == LAN_DHCP) {
473         if (input == STATIC || input == DHCP) {
474             return false;
475         }
476     }
477     return true;
478 }
479 } // namespace NetManagerStandard
480 } // namespace OHOS
481