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 "ethernet_lan_management.h"
17 #include "netmgr_ext_log_wrapper.h"
18 #include "net_conn_client.h"
19 #include "netsys_controller.h"
20 #include "netmanager_base_common_utils.h"
21 #include "route.h"
22 #include "net_manager_constants.h"
23 
24 namespace OHOS {
25 namespace NetManagerStandard {
26 namespace {
27 constexpr const int32_t LOCAL_NET_ID = 99;
28 } // namespace
29 
EthernetLanManagement()30 EthernetLanManagement::EthernetLanManagement()
31 {
32 }
33 
GetOldLinkInfo(sptr<DevInterfaceState> & devState)34 void EthernetLanManagement::GetOldLinkInfo(sptr<DevInterfaceState> &devState)
35 {
36     if (devState == nullptr) {
37         NETMGR_EXT_LOG_D("EthernetLanManagement:GetOldLinkInfo fail due to devState is nullptr");
38         return;
39     }
40     if (devState->GetLinkInfo() == nullptr) {
41         NETMGR_EXT_LOG_W("EthernetLanManagement:GetOldLinkInfo fail due to linkInfo is NULL");
42         netLinkInfo_.Initialize();
43         return;
44     }
45     netLinkInfo_ = *(devState->GetLinkInfo());
46 }
47 
UpdateLanLinkInfo(sptr<DevInterfaceState> & devState)48 int32_t EthernetLanManagement::UpdateLanLinkInfo(sptr<DevInterfaceState> &devState)
49 {
50     if (devState == nullptr) {
51         NETMGR_EXT_LOG_D("EthernetLanManagement:UpdateLanLinkInfo fail due to devState is nullptr");
52         return NETMANAGER_ERR_INTERNAL;
53     }
54     if (!devState->GetLinkUp()) {
55         NETMGR_EXT_LOG_D("EthernetLanManagement:UpdateLanLinkInfo fail due to not link up");
56         return ETHERNET_ERR_DEVICE_NOT_LINK;
57     }
58     if (devState->GetLinkInfo() == nullptr) {
59         NETMGR_EXT_LOG_E("EthernetLanManagement:UpdateLanLinkInfo fail due to newNetLinkInfo is NULL");
60         return NETMANAGER_ERR_INTERNAL;
61     }
62     NetLinkInfo newNetLinkInfo = *(devState->GetLinkInfo());
63     int32_t ret = NETMANAGER_SUCCESS;
64     ret += DelIp(newNetLinkInfo);
65     ret += SetIp(newNetLinkInfo);
66     ret += DelRoute(newNetLinkInfo);
67     ret += SetRoute(newNetLinkInfo);
68     return ret;
69 }
70 
ReleaseLanNetLink(sptr<DevInterfaceState> & devState)71 int32_t EthernetLanManagement::ReleaseLanNetLink(sptr<DevInterfaceState> &devState)
72 {
73     NETMGR_EXT_LOG_D("EthernetLanManagement:ReleaseLanNetLink...");
74     if (devState == nullptr) {
75         NETMGR_EXT_LOG_D("EthernetLanManagement:ReleaseLanNetLink fail due to devState is nullptr");
76         return NETMANAGER_ERR_INTERNAL;
77     }
78     if (devState->GetLinkInfo() == nullptr) {
79         NETMGR_EXT_LOG_E("EthernetLanManagement:ReleaseLanNetLink fail due to newNetLinkInfo is NULL");
80         return NETMANAGER_ERR_INTERNAL;
81     }
82     NetLinkInfo newNetLinkInfo = *(devState->GetLinkInfo());
83     int32_t ret = NETMANAGER_SUCCESS;
84     for (const auto &inetAddr : newNetLinkInfo.netAddrList_) {
85         auto family = CommonUtils::GetAddrFamily(inetAddr.address_);
86         auto prefixLen = inetAddr.prefixlen_ ? static_cast<int32_t>(inetAddr.prefixlen_)
87                                              : ((family == AF_INET6) ? CommonUtils::Ipv6PrefixLen(inetAddr.netMask_)
88                                                                      : CommonUtils::Ipv4PrefixLen(inetAddr.netMask_));
89         ret += NetConnClient::GetInstance().DelInterfaceAddress(newNetLinkInfo.ifaceName_,
90                                                                 inetAddr.address_, prefixLen);
91         if (ret != NETMANAGER_SUCCESS) {
92             NETMGR_EXT_LOG_E("del lan interface[%{public}s] address[%{private}s] failed",
93                              newNetLinkInfo.ifaceName_.c_str(), inetAddr.address_.c_str());
94         }
95     }
96     for (const auto &route : newNetLinkInfo.routeList_) {
97         std::string destAddress = route.destination_.address_ + "/" + std::to_string(route.destination_.prefixlen_);
98         ret += NetConnClient::GetInstance().RemoveNetworkRoute(LOCAL_NET_ID, route.iface_, destAddress,
99                                                                route.gateway_.address_);
100         if (ret != NETMANAGER_SUCCESS) {
101             NETMGR_EXT_LOG_E("del lan[%{public}s] route failed, destAddress[%{private}s], nexthop[%{private}s]",
102                              route.iface_.c_str(), destAddress.c_str(), route.gateway_.address_.c_str());
103         }
104     }
105     return ret;
106 }
107 
SetIp(const NetLinkInfo & newNetLinkInfo)108 int32_t EthernetLanManagement::SetIp(const NetLinkInfo &newNetLinkInfo)
109 {
110     NETMGR_EXT_LOG_D("EthernetLanManagement:SetIp...");
111     for (const auto &inetAddr : newNetLinkInfo.netAddrList_) {
112         if (netLinkInfo_.HasNetAddr(inetAddr)) {
113             NETMGR_EXT_LOG_W("Same ip address:[%{public}s], there is no need to add it again",
114                              CommonUtils::ToAnonymousIp(inetAddr.address_).c_str());
115             continue;
116         }
117         auto family = CommonUtils::GetAddrFamily(inetAddr.address_);
118         auto prefixLen = inetAddr.prefixlen_ ? static_cast<int32_t>(inetAddr.prefixlen_)
119                                              : ((family == AF_INET6) ? CommonUtils::Ipv6PrefixLen(inetAddr.netMask_)
120                                                                      : CommonUtils::Ipv4PrefixLen(inetAddr.netMask_));
121         auto ret = NetConnClient::GetInstance().AddInterfaceAddress(newNetLinkInfo.ifaceName_,
122                                                                     inetAddr.address_, prefixLen);
123         if (ret != NETMANAGER_SUCCESS) {
124             NETMGR_EXT_LOG_E("set lan interface address failed");
125             return ret;
126         }
127     }
128     return NETMANAGER_SUCCESS;
129 }
130 
DelIp(const NetLinkInfo & newNetLinkInfo)131 int32_t EthernetLanManagement::DelIp(const NetLinkInfo &newNetLinkInfo)
132 {
133     NETMGR_EXT_LOG_D("EthernetLanManagement:DelIp...");
134     for (const auto &inetAddr : netLinkInfo_.netAddrList_) {
135         if (newNetLinkInfo.HasNetAddr(inetAddr)) {
136             NETMGR_EXT_LOG_W("Same ip address:[%{public}s], there is not need to be deleted",
137                              CommonUtils::ToAnonymousIp(inetAddr.address_).c_str());
138             continue;
139         }
140         auto family = CommonUtils::GetAddrFamily(inetAddr.address_);
141         auto prefixLen = inetAddr.prefixlen_ ? static_cast<int32_t>(inetAddr.prefixlen_)
142                                              : ((family == AF_INET6) ? CommonUtils::Ipv6PrefixLen(inetAddr.netMask_)
143                                                                      : CommonUtils::Ipv4PrefixLen(inetAddr.netMask_));
144         auto ret = NetConnClient::GetInstance().DelInterfaceAddress(netLinkInfo_.ifaceName_,
145                                                                     inetAddr.address_, prefixLen);
146         if (ret != NETMANAGER_SUCCESS) {
147             NETMGR_EXT_LOG_E("del lan interface address failed");
148             return ret;
149         }
150     }
151     return NETMANAGER_SUCCESS;
152 }
153 
SetRoute(const NetLinkInfo & newNetLinkInfo)154 int32_t EthernetLanManagement::SetRoute(const NetLinkInfo &newNetLinkInfo)
155 {
156     for (const auto &route : newNetLinkInfo.routeList_) {
157         if (netLinkInfo_.HasRoute(route)) {
158             NETMGR_EXT_LOG_W("Same route:[%{public}s]  ifo, there is no need to add it again",
159                              CommonUtils::ToAnonymousIp(route.destination_.address_).c_str());
160             continue;
161         }
162         std::string destAddress = route.destination_.address_ + "/" + std::to_string(route.destination_.prefixlen_);
163         auto ret = NetConnClient::GetInstance().AddNetworkRoute(LOCAL_NET_ID, route.iface_, destAddress,
164                                                                 route.gateway_.address_);
165         if (ret != NETMANAGER_SUCCESS) {
166             NETMGR_EXT_LOG_E("Set lan route failed");
167             return ret;
168         }
169     }
170     return NETMANAGER_SUCCESS;
171 }
172 
DelRoute(const NetLinkInfo & newNetLinkInfo)173 int32_t EthernetLanManagement::DelRoute(const NetLinkInfo &newNetLinkInfo)
174 {
175     for (const auto &route : netLinkInfo_.routeList_) {
176         if (newNetLinkInfo.HasRoute(route)) {
177             NETMGR_EXT_LOG_W("Same route:[%{public}s]  ifo, there is not need to be deleted",
178                              CommonUtils::ToAnonymousIp(route.destination_.address_).c_str());
179             continue;
180         }
181         std::string destAddress = route.destination_.address_ + "/" + std::to_string(route.destination_.prefixlen_);
182         auto ret = NetConnClient::GetInstance().RemoveNetworkRoute(LOCAL_NET_ID, route.iface_, destAddress,
183                                                                    route.gateway_.address_);
184         if (ret != NETMANAGER_SUCCESS) {
185             NETMGR_EXT_LOG_E("del lan route failed");
186             return ret;
187         }
188     }
189     return NETMANAGER_SUCCESS;
190 }
191 
192 } // namespace NetManagerStandard
193 } // namespace OHOS
194 
195