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 NETMGR_EXT_LOG_I("DevInterfaceStateCallback::OnInterfaceLinkStateChanged iface[%{public}s] up[%{public}d]",
84 ifName.c_str(), up);
85 std::regex re(IFACE_MATCH);
86 if (std::regex_search(ifName, re)) {
87 ethernetManagement_.UpdateInterfaceState(ifName, up);
88 }
89 return 0;
90 }
91
OnRouteChanged(bool,const std::string &,const std::string &,const std::string &)92 int32_t EthernetManagement::DevInterfaceStateCallback::OnRouteChanged(bool, const std::string &, const std::string &,
93 const std::string &)
94 {
95 return 0;
96 }
97
OnDhcpSuccess(NetsysControllerCallback::DhcpResult & dhcpResult)98 int32_t EthernetManagement::DevInterfaceStateCallback::OnDhcpSuccess(NetsysControllerCallback::DhcpResult &dhcpResult)
99 {
100 return 0;
101 }
102
OnBandwidthReachedLimit(const std::string & limitName,const std::string & iface)103 int32_t EthernetManagement::DevInterfaceStateCallback::OnBandwidthReachedLimit(const std::string &limitName,
104 const std::string &iface)
105 {
106 return 0;
107 }
108
EthernetManagement()109 EthernetManagement::EthernetManagement()
110 {
111 ethDhcpController_ = std::make_unique<EthernetDhcpController>();
112 ethDhcpNotifyCallback_ = new (std::nothrow) EhternetDhcpNotifyCallback(*this);
113 if (ethDhcpNotifyCallback_ != nullptr) {
114 ethDhcpController_->RegisterDhcpCallback(ethDhcpNotifyCallback_);
115 }
116
117 ethDevInterfaceStateCallback_ = new (std::nothrow) DevInterfaceStateCallback(*this);
118 if (ethDevInterfaceStateCallback_ != nullptr) {
119 NetsysController::GetInstance().RegisterCallback(ethDevInterfaceStateCallback_);
120 }
121
122 ethConfiguration_ = std::make_unique<EthernetConfiguration>();
123 ethConfiguration_->ReadSystemConfiguration(devCaps_, devCfgs_);
124 }
125
126 EthernetManagement::~EthernetManagement() = default;
127
UpdateInterfaceState(const std::string & dev,bool up)128 void EthernetManagement::UpdateInterfaceState(const std::string &dev, bool up)
129 {
130 NETMGR_EXT_LOG_D("EthernetManagement UpdateInterfaceState dev[%{public}s] up[%{public}d]", dev.c_str(), up);
131 std::unique_lock<std::mutex> lock(mutex_);
132 auto fit = devs_.find(dev);
133 if (fit == devs_.end()) {
134 return;
135 }
136 sptr<DevInterfaceState> devState = fit->second;
137 if (devState == nullptr) {
138 NETMGR_EXT_LOG_E("devState is nullptr");
139 return;
140 }
141 devState->SetLinkUp(up);
142 IPSetMode mode = devState->GetIPSetMode();
143 bool dhcpReqState = devState->GetDhcpReqState();
144 NETMGR_EXT_LOG_D("EthernetManagement UpdateInterfaceState mode[%{public}d] dhcpReqState[%{public}d]",
145 static_cast<int32_t>(mode), dhcpReqState);
146 if (up) {
147 devState->RemoteUpdateNetSupplierInfo();
148 if (mode == DHCP && !dhcpReqState) {
149 StartDhcpClient(dev, devState);
150 } else {
151 devState->RemoteUpdateNetLinkInfo();
152 }
153 } else {
154 if (mode == DHCP && dhcpReqState) {
155 StopDhcpClient(dev, devState);
156 }
157 devState->RemoteUpdateNetSupplierInfo();
158 netLinkConfigs_[dev] = nullptr;
159 }
160 }
161
UpdateDevInterfaceCfg(const std::string & iface,sptr<InterfaceConfiguration> cfg)162 int32_t EthernetManagement::UpdateDevInterfaceCfg(const std::string &iface, sptr<InterfaceConfiguration> cfg)
163 {
164 if (cfg == nullptr) {
165 NETMGR_EXT_LOG_E("cfg is nullptr");
166 return NETMANAGER_EXT_ERR_LOCAL_PTR_NULL;
167 }
168 std::unique_lock<std::mutex> lock(mutex_);
169 auto fit = devs_.find(iface);
170 if (fit == devs_.end() || fit->second == nullptr) {
171 NETMGR_EXT_LOG_E("The iface[%{public}s] device or device information does not exist", iface.c_str());
172 return ETHERNET_ERR_DEVICE_INFORMATION_NOT_EXIST;
173 }
174 if (!fit->second->GetLinkUp()) {
175 NETMGR_EXT_LOG_E("The iface[%{public}s] device is unlink", iface.c_str());
176 return ETHERNET_ERR_DEVICE_NOT_LINK;
177 }
178 if (!ethConfiguration_->WriteUserConfiguration(iface, cfg)) {
179 NETMGR_EXT_LOG_E("EthernetManagement write user configurations error!");
180 return ETHERNET_ERR_USER_CONIFGURATION_WRITE_FAIL;
181 }
182 if (fit->second->GetIfcfg()->mode_ != cfg->mode_) {
183 if (cfg->mode_ == DHCP) {
184 StartDhcpClient(iface, fit->second);
185 } else {
186 StopDhcpClient(iface, fit->second);
187 netLinkConfigs_[iface] = nullptr;
188 }
189 } else if (cfg->mode_ == DHCP) {
190 fit->second->UpdateNetHttpProxy(cfg->httpProxy_);
191 }
192 fit->second->SetIfcfg(cfg);
193 devCfgs_[iface] = cfg;
194 return NETMANAGER_EXT_SUCCESS;
195 }
196
UpdateDevInterfaceLinkInfo(EthernetDhcpCallback::DhcpResult & dhcpResult)197 int32_t EthernetManagement::UpdateDevInterfaceLinkInfo(EthernetDhcpCallback::DhcpResult &dhcpResult)
198 {
199 NETMGR_EXT_LOG_D("EthernetManagement::UpdateDevInterfaceLinkInfo");
200 std::lock_guard<std::mutex> locker(mutex_);
201 auto fit = devs_.find(dhcpResult.iface);
202 if (fit == devs_.end() || fit->second == nullptr) {
203 NETMGR_EXT_LOG_E("The iface[%{public}s] device or device information does not exist", dhcpResult.iface.c_str());
204 return ETHERNET_ERR_DEVICE_INFORMATION_NOT_EXIST;
205 }
206 if (!fit->second->GetLinkUp()) {
207 NETMGR_EXT_LOG_E("The iface[%{public}s] The device is not turned on", dhcpResult.iface.c_str());
208 return ETHERNET_ERR_DEVICE_NOT_LINK;
209 }
210
211 if (fit->second->GetIPSetMode() == IPSetMode::STATIC) {
212 NETMGR_EXT_LOG_E("The iface[%{public}s] set mode is STATIC now", dhcpResult.iface.c_str());
213 return ETHERNET_ERR_DEVICE_NOT_LINK;
214 }
215
216 auto &config = netLinkConfigs_[dhcpResult.iface];
217 if (config == nullptr) {
218 config = new (std::nothrow) StaticConfiguration();
219 if (config == nullptr) {
220 NETMGR_EXT_LOG_E("Iface:%{public}s's link info config is nullptr", dhcpResult.iface.c_str());
221 }
222 }
223
224 if (!ethConfiguration_->ConvertToConfiguration(dhcpResult, config)) {
225 NETMGR_EXT_LOG_E("EthernetManagement dhcp convert to configurations error!");
226 return ETHERNET_ERR_CONVERT_CONFIGURATINO_FAIL;
227 }
228 fit->second->UpdateLinkInfo(config);
229 fit->second->RemoteUpdateNetLinkInfo();
230 return NETMANAGER_EXT_SUCCESS;
231 }
232
GetDevInterfaceCfg(const std::string & iface,sptr<InterfaceConfiguration> & ifaceConfig)233 int32_t EthernetManagement::GetDevInterfaceCfg(const std::string &iface, sptr<InterfaceConfiguration> &ifaceConfig)
234 {
235 std::unique_lock<std::mutex> lock(mutex_);
236 auto fit = devs_.find(iface);
237 if (fit == devs_.end() || fit->second == nullptr) {
238 NETMGR_EXT_LOG_E("The iface[%{public}s] device does not exist", iface.c_str());
239 return ETHERNET_ERR_DEVICE_INFORMATION_NOT_EXIST;
240 }
241 if (!fit->second->GetLinkUp()) {
242 ifaceConfig = fit->second->GetIfcfg();
243 return NETMANAGER_EXT_SUCCESS;
244 }
245 auto temp = ethConfiguration_->MakeInterfaceConfiguration(fit->second->GetIfcfg(), fit->second->GetLinkInfo());
246 if (temp == nullptr) {
247 return ETHERNET_ERR_DEVICE_INFORMATION_NOT_EXIST;
248 }
249 *ifaceConfig = *temp;
250 return NETMANAGER_EXT_SUCCESS;
251 }
252
IsIfaceActive(const std::string & iface,int32_t & activeStatus)253 int32_t EthernetManagement::IsIfaceActive(const std::string &iface, int32_t &activeStatus)
254 {
255 std::unique_lock<std::mutex> lock(mutex_);
256 auto fit = devs_.find(iface);
257 if (fit == devs_.end() || fit->second == nullptr) {
258 NETMGR_EXT_LOG_E("The iface[%{public}s] device does not exist", iface.c_str());
259 return ETHERNET_ERR_DEVICE_INFORMATION_NOT_EXIST;
260 }
261 activeStatus = static_cast<int32_t>(fit->second->GetLinkUp());
262 return NETMANAGER_EXT_SUCCESS;
263 }
264
GetAllActiveIfaces(std::vector<std::string> & activeIfaces)265 int32_t EthernetManagement::GetAllActiveIfaces(std::vector<std::string> &activeIfaces)
266 {
267 std::unique_lock<std::mutex> lock(mutex_);
268 for (auto it = devs_.begin(); it != devs_.end(); ++it) {
269 if (it->second->GetLinkUp()) {
270 activeIfaces.push_back(it->first);
271 }
272 }
273 return NETMANAGER_EXT_SUCCESS;
274 }
275
ResetFactory()276 int32_t EthernetManagement::ResetFactory()
277 {
278 if (!ethConfiguration_->ClearAllUserConfiguration()) {
279 NETMGR_EXT_LOG_E("Failed to ResetFactory!");
280 return ETHERNET_ERR_USER_CONIFGURATION_CLEAR_FAIL;
281 }
282 NETMGR_EXT_LOG_I("Success to ResetFactory!");
283 return NETMANAGER_EXT_SUCCESS;
284 }
285
Init()286 void EthernetManagement::Init()
287 {
288 std::regex re(IFACE_MATCH);
289 std::vector<std::string> ifaces = NetsysController::GetInstance().InterfaceGetList();
290 if (ifaces.empty()) {
291 NETMGR_EXT_LOG_E("EthernetManagement link list is empty");
292 return;
293 }
294 NETMGR_EXT_LOG_D("EthernetManagement devs size[%{public}zd]", ifaces.size());
295 if (!ethConfiguration_->ReadUserConfiguration(devCfgs_)) {
296 NETMGR_EXT_LOG_E("EthernetManagement read user configurations error! ");
297 return;
298 }
299 for (const auto &devName : ifaces) {
300 NETMGR_EXT_LOG_D("EthernetManagement devName[%{public}s]", devName.c_str());
301 if (!std::regex_search(devName, re)) {
302 continue;
303 }
304 DevInterfaceAdd(devName);
305 }
306 std::thread t(&EthernetManagement::StartSetDevUpThd, shared_from_this());
307 std::string threadName = "SetDevUpThd";
308 pthread_setname_np(t.native_handle(), threadName.c_str());
309 t.detach();
310 NETMGR_EXT_LOG_D("EthernetManagement devs_ size[%{public}zd", devs_.size());
311 }
312
StartSetDevUpThd()313 void EthernetManagement::StartSetDevUpThd()
314 {
315 NETMGR_EXT_LOG_D("EthernetManagement StartSetDevUpThd in.");
316 for (auto &dev : devs_) {
317 std::string devName = dev.first;
318 if (IsIfaceLinkUp(devName)) {
319 continue;
320 }
321 while (true) {
322 if (NetsysController::GetInstance().SetInterfaceUp(devName) != ERR_NONE) {
323 sleep(SLEEP_TIME_S);
324 continue;
325 }
326 break;
327 }
328 }
329 }
330
IsIfaceLinkUp(const std::string & iface)331 bool EthernetManagement::IsIfaceLinkUp(const std::string &iface)
332 {
333 OHOS::nmd::InterfaceConfigurationParcel config;
334 config.ifName = iface;
335 if (NetsysController::GetInstance().GetInterfaceConfig(config) != ERR_NONE) {
336 return false;
337 }
338 if (std::find(config.flags.begin(), config.flags.end(), IFACE_LINK_UP) == config.flags.end() ||
339 std::find(config.flags.begin(), config.flags.end(), IFACE_RUNNING) == config.flags.end()) {
340 return false;
341 }
342 UpdateInterfaceState(iface, true);
343 return true;
344 }
345
StartDhcpClient(const std::string & dev,sptr<DevInterfaceState> & devState)346 void EthernetManagement::StartDhcpClient(const std::string &dev, sptr<DevInterfaceState> &devState)
347 {
348 NETMGR_EXT_LOG_D("EthernetManagement StartDhcpClient[%{public}s]", dev.c_str());
349 ethDhcpController_->StartDhcpClient(dev, true);
350 devState->SetDhcpReqState(true);
351 }
352
StopDhcpClient(const std::string & dev,sptr<DevInterfaceState> & devState)353 void EthernetManagement::StopDhcpClient(const std::string &dev, sptr<DevInterfaceState> &devState)
354 {
355 NETMGR_EXT_LOG_D("EthernetManagement StopDhcpClient[%{public}s]", dev.c_str());
356 ethDhcpController_->StopDhcpClient(dev, true);
357 devState->SetDhcpReqState(false);
358 }
359
DevInterfaceAdd(const std::string & devName)360 void EthernetManagement::DevInterfaceAdd(const std::string &devName)
361 {
362 NETMGR_EXT_LOG_D("Interface name:[%{public}s] add.", devName.c_str());
363 std::unique_lock<std::mutex> lock(mutex_);
364 auto fitDev = devs_.find(devName);
365 if (fitDev != devs_.end()) {
366 NETMGR_EXT_LOG_E("Interface name:[%{public}s] has added.", devName.c_str());
367 return;
368 }
369 sptr<DevInterfaceState> devState = new (std::nothrow) DevInterfaceState();
370 if (devState == nullptr) {
371 NETMGR_EXT_LOG_E("devState is nullptr");
372 return;
373 }
374 devs_.insert(std::make_pair(devName, devState));
375 devState->SetDevName(devName);
376 devState->RemoteRegisterNetSupplier();
377 auto fitCfg = devCfgs_.find(devName);
378 if (fitCfg != devCfgs_.end()) {
379 devState->SetIfcfg(fitCfg->second);
380 } else {
381 sptr<InterfaceConfiguration> ifCfg = new (std::nothrow) InterfaceConfiguration();
382 if (ifCfg == nullptr) {
383 NETMGR_EXT_LOG_E("ifCfg is nullptr");
384 return;
385 }
386 ifCfg->mode_ = DHCP;
387 devState->SetIfcfg(ifCfg);
388 }
389 auto fitCap = devCaps_.find(devName);
390 if (fitCap != devCaps_.end()) {
391 devState->SetNetCaps(fitCap->second);
392 }
393 }
394
DevInterfaceRemove(const std::string & devName)395 void EthernetManagement::DevInterfaceRemove(const std::string &devName)
396 {
397 NETMGR_EXT_LOG_D("Interface name:[%{public}s] remove.", devName.c_str());
398 std::unique_lock<std::mutex> lock(mutex_);
399 auto fitDev = devs_.find(devName);
400 if (fitDev != devs_.end()) {
401 if (fitDev->second != nullptr) {
402 fitDev->second->RemoteUnregisterNetSupplier();
403 }
404 devs_.erase(fitDev);
405 }
406 }
407
GetDumpInfo(std::string & info)408 void EthernetManagement::GetDumpInfo(std::string &info)
409 {
410 std::for_each(devs_.begin(), devs_.end(), [&info](const auto &dev) { dev.second->GetDumpInfo(info); });
411 }
412 } // namespace NetManagerStandard
413 } // namespace OHOS
414