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 "interface_manager.h"
17
18 #include <arpa/inet.h>
19 #include <cerrno>
20 #include <cstdlib>
21 #include <cstring>
22 #include <dirent.h>
23 #include <fcntl.h>
24 #include <linux/if_ether.h>
25 #include <net/if.h>
26 #include <netinet/in.h>
27 #include <sys/ioctl.h>
28 #include <sys/socket.h>
29 #include <sys/types.h>
30 #include <system_error>
31 #include <unistd.h>
32
33 #include "netlink_manager.h"
34 #include "netlink_msg.h"
35 #include "netlink_socket.h"
36 #include "netlink_socket_diag.h"
37 #include "net_manager_constants.h"
38 #include "netmanager_base_common_utils.h"
39 #include "netnative_log_wrapper.h"
40 #include "securec.h"
41
42 namespace OHOS {
43 namespace nmd {
44 using namespace NetManagerStandard;
45 using namespace NetManagerStandard::CommonUtils;
46
47 namespace {
48 constexpr const char *SYS_NET_PATH = "/sys/class/net/";
49 constexpr const char *MTU_PATH = "/mtu";
50 constexpr int32_t FILE_PERMISSION = 0666;
51 constexpr uint32_t ARRAY_OFFSET_1_INDEX = 1;
52 constexpr uint32_t ARRAY_OFFSET_2_INDEX = 2;
53 constexpr uint32_t ARRAY_OFFSET_3_INDEX = 3;
54 constexpr uint32_t ARRAY_OFFSET_4_INDEX = 4;
55 constexpr uint32_t ARRAY_OFFSET_5_INDEX = 5;
56 constexpr uint32_t MOVE_BIT_LEFT31 = 31;
57 constexpr uint32_t BIT_MAX = 32;
58 constexpr uint32_t IOCTL_RETRY_TIME = 32;
59 constexpr int32_t MAX_MTU_LEN = 11;
60
CheckFilePath(const std::string & fileName,std::string & realPath)61 bool CheckFilePath(const std::string &fileName, std::string &realPath)
62 {
63 char tmpPath[PATH_MAX] = {0};
64 if (!realpath(fileName.c_str(), tmpPath)) {
65 NETNATIVE_LOGE("file name is illegal");
66 return false;
67 }
68 realPath = tmpPath;
69 return true;
70 }
71 } // namespace
72
GetMtu(const char * interfaceName)73 int InterfaceManager::GetMtu(const char *interfaceName)
74 {
75 if (interfaceName == nullptr) {
76 NETNATIVE_LOGE("interfaceName is null");
77 return -1;
78 }
79
80 if (!CheckIfaceName(interfaceName)) {
81 NETNATIVE_LOGE("InterfaceManager::GetMtu isIfaceName fail %{public}d", errno);
82 return -1;
83 }
84 std::string mtuPath = std::string(SYS_NET_PATH).append(interfaceName).append(MTU_PATH);
85 std::string realPath;
86 if (!CheckFilePath(mtuPath, realPath)) {
87 NETNATIVE_LOGE("file does not exist! ");
88 return -1;
89 }
90 int fd = open(realPath.c_str(), 0, FILE_PERMISSION);
91 if (fd == -1) {
92 NETNATIVE_LOGE("InterfaceManager::GetMtu open fail %{public}d", errno);
93 return -1;
94 }
95
96 char originMtuValue[MAX_MTU_LEN] = {0};
97 int nread = read(fd, originMtuValue, (sizeof(char) * (MAX_MTU_LEN - 1)));
98 if (nread == -1 || nread == 0) {
99 NETNATIVE_LOGE("InterfaceManager::GetMtu read fail %{public}d", errno);
100 close(fd);
101 return -1;
102 }
103 close(fd);
104
105 int32_t mtu = -1;
106 (void)NetManagerStandard::CommonUtils::ParseInt(originMtuValue, &mtu);
107 return mtu;
108 }
109
SetMtu(const char * interfaceName,const char * mtuValue)110 int InterfaceManager::SetMtu(const char *interfaceName, const char *mtuValue)
111 {
112 if (interfaceName == nullptr || mtuValue == nullptr) {
113 NETNATIVE_LOGE("interfaceName or mtuValue is null");
114 return -1;
115 }
116
117 if (!CheckIfaceName(interfaceName)) {
118 NETNATIVE_LOGE("InterfaceManager::SetMtu isIfaceName fail %{public}d", errno);
119 }
120 int32_t sockfd = socket(AF_INET, SOCK_DGRAM, 0);
121 if (sockfd < 0) {
122 NETNATIVE_LOGE("InterfaceManager::SetMtu socket fail %{public}d", errno);
123 return -1;
124 }
125
126 struct ifreq ifr;
127 if (memset_s(&ifr, sizeof(ifr), 0, sizeof(ifr)) != EOK) {
128 close(sockfd);
129 return -1;
130 }
131 if (strncpy_s(ifr.ifr_name, IFNAMSIZ, interfaceName, strlen(interfaceName)) != EOK) {
132 close(sockfd);
133 return -1;
134 }
135
136 int32_t mtu = StrToInt(mtuValue);
137 ifr.ifr_mtu = mtu;
138
139 if (ioctl(sockfd, SIOCSIFMTU, &ifr) < 0) {
140 NETNATIVE_LOGE("InterfaceManager::SetMtu ioctl fail %{public}d", errno);
141 close(sockfd);
142 return -1;
143 }
144
145 close(sockfd);
146 return 0;
147 }
148
GetInterfaceNames()149 std::vector<std::string> InterfaceManager::GetInterfaceNames()
150 {
151 std::vector<std::string> ifaceNames;
152 DIR *dir(nullptr);
153 struct dirent *de(nullptr);
154
155 dir = opendir(SYS_NET_PATH);
156 if (dir == nullptr) {
157 NETNATIVE_LOGE("InterfaceManager::GetInterfaceNames opendir fail %{public}d", errno);
158 return ifaceNames;
159 }
160
161 de = readdir(dir);
162 while (de != nullptr) {
163 if ((de->d_name[0] != '.') && ((de->d_type == DT_DIR) || (de->d_type == DT_LNK))) {
164 ifaceNames.push_back(std::string(de->d_name));
165 }
166 de = readdir(dir);
167 }
168 closedir(dir);
169
170 return ifaceNames;
171 }
172
ModifyAddress(uint32_t action,const char * interfaceName,const char * addr,int prefixLen)173 int InterfaceManager::ModifyAddress(uint32_t action, const char *interfaceName, const char *addr, int prefixLen)
174 {
175 if (interfaceName == nullptr || addr == nullptr) {
176 return -1;
177 }
178 uint32_t index = if_nametoindex(interfaceName);
179 if (index == 0) {
180 NETNATIVE_LOGE("InterfaceManager::ModifyAddress, if_nametoindex error %{public}d", errno);
181 return -errno;
182 }
183 auto family = CommonUtils::GetAddrFamily(addr);
184 if (family != AF_INET && family != AF_INET6) {
185 NETNATIVE_LOGE("Ivalid ip address: %{public}s", addr);
186 return NETMANAGER_ERR_PARAMETER_ERROR;
187 }
188
189 ifaddrmsg ifm = {static_cast<uint8_t>(family), static_cast<uint8_t>(prefixLen), 0, 0, index};
190 nmd::NetlinkMsg nlmsg(NLM_F_CREATE | NLM_F_EXCL, nmd::NETLINK_MAX_LEN, getpid());
191 nlmsg.AddAddress(action, ifm);
192
193 if (family == AF_INET6) {
194 in6_addr in6Addr;
195 if (inet_pton(AF_INET6, addr, &in6Addr) == -1) {
196 NETNATIVE_LOGE("Modify ipv6 address, inet_pton error %{public}d", errno);
197 return NETMANAGER_ERR_INTERNAL;
198 }
199 nlmsg.AddAttr(IFA_LOCAL, &in6Addr, sizeof(in6Addr));
200 } else {
201 in_addr inAddr;
202 if (inet_pton(AF_INET, addr, &inAddr) == -1) {
203 NETNATIVE_LOGE("Modify ipv4 address, inet_pton error %{public}d", errno);
204 return NETMANAGER_ERR_INTERNAL;
205 }
206 nlmsg.AddAttr(IFA_LOCAL, &inAddr, sizeof(inAddr));
207 if (action == RTM_NEWADDR) {
208 inAddr.s_addr |= htonl((1U << (BIT_MAX - prefixLen)) - 1);
209 nlmsg.AddAttr(IFA_BROADCAST, &inAddr, sizeof(inAddr));
210 }
211 }
212
213 NETNATIVE_LOGI("InterfaceManager::ModifyAddress:%{public}u %{public}s %{public}s %{public}d", action, interfaceName,
214 ToAnonymousIp(addr).c_str(), prefixLen);
215
216 auto ret = SendNetlinkMsgToKernel(nlmsg.GetNetLinkMessage());
217 if (ret < 0) {
218 return -EIO;
219 }
220 return NETMANAGER_SUCCESS;
221 }
222
AddAddress(const char * interfaceName,const char * addr,int prefixLen)223 int InterfaceManager::AddAddress(const char *interfaceName, const char *addr, int prefixLen)
224 {
225 return ModifyAddress(RTM_NEWADDR, interfaceName, addr, prefixLen);
226 }
227
DelAddress(const char * interfaceName,const char * addr,int prefixLen)228 int InterfaceManager::DelAddress(const char *interfaceName, const char *addr, int prefixLen)
229 {
230 NetLinkSocketDiag socketDiag;
231 socketDiag.DestroyLiveSockets(addr, true);
232 return ModifyAddress(RTM_DELADDR, interfaceName, addr, prefixLen);
233 }
234
Ipv4NetmaskToPrefixLength(in_addr_t mask)235 int Ipv4NetmaskToPrefixLength(in_addr_t mask)
236 {
237 int prefixLength = 0;
238 uint32_t m = ntohl(mask);
239 const uint32_t referenceValue = 1;
240 while (m & (referenceValue << MOVE_BIT_LEFT31)) {
241 prefixLength++;
242 m = m << referenceValue;
243 }
244 return prefixLength;
245 }
246
HwAddrToStr(char * hwaddr)247 std::string HwAddrToStr(char *hwaddr)
248 {
249 char buf[64] = {'\0'};
250 if (hwaddr != nullptr) {
251 errno_t result =
252 sprintf_s(buf, sizeof(buf), "%02x:%02x:%02x:%02x:%02x:%02x", hwaddr[0], hwaddr[ARRAY_OFFSET_1_INDEX],
253 hwaddr[ARRAY_OFFSET_2_INDEX], hwaddr[ARRAY_OFFSET_3_INDEX], hwaddr[ARRAY_OFFSET_4_INDEX],
254 hwaddr[ARRAY_OFFSET_5_INDEX]);
255 if (result != 0) {
256 NETNATIVE_LOGE("[hwAddrToStr]: result %{public}d", result);
257 }
258 }
259 return std::string(buf);
260 }
261
UpdateIfaceConfigFlags(unsigned flags,nmd::InterfaceConfigurationParcel & ifaceConfig)262 void UpdateIfaceConfigFlags(unsigned flags, nmd::InterfaceConfigurationParcel &ifaceConfig)
263 {
264 ifaceConfig.flags.emplace_back(flags & IFF_UP ? "up" : "down");
265 if (flags & IFF_BROADCAST) {
266 ifaceConfig.flags.emplace_back("broadcast");
267 }
268 if (flags & IFF_LOOPBACK) {
269 ifaceConfig.flags.emplace_back("loopback");
270 }
271 if (flags & IFF_POINTOPOINT) {
272 ifaceConfig.flags.emplace_back("point-to-point");
273 }
274 if (flags & IFF_RUNNING) {
275 ifaceConfig.flags.emplace_back("running");
276 }
277 if (flags & IFF_MULTICAST) {
278 ifaceConfig.flags.emplace_back("multicast");
279 }
280 }
281
GetIfaceConfig(const std::string & ifName)282 InterfaceConfigurationParcel InterfaceManager::GetIfaceConfig(const std::string &ifName)
283 {
284 NETNATIVE_LOGI("GetIfaceConfig in. ifName %{public}s", ifName.c_str());
285 struct in_addr addr = {};
286 nmd::InterfaceConfigurationParcel ifaceConfig;
287
288 int fd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
289 struct ifreq ifr = {};
290 strncpy_s(ifr.ifr_name, IFNAMSIZ, ifName.c_str(), ifName.length());
291
292 ifaceConfig.ifName = ifName;
293 if (ioctl(fd, SIOCGIFADDR, &ifr) != -1) {
294 addr.s_addr = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
295 ifaceConfig.ipv4Addr = std::string(inet_ntoa(addr));
296 }
297 if (ioctl(fd, SIOCGIFNETMASK, &ifr) != -1) {
298 ifaceConfig.prefixLength = Ipv4NetmaskToPrefixLength(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr);
299 }
300 if (ioctl(fd, SIOCGIFFLAGS, &ifr) != -1) {
301 UpdateIfaceConfigFlags(ifr.ifr_flags, ifaceConfig);
302 }
303 if (ioctl(fd, SIOCGIFHWADDR, &ifr) != -1) {
304 ifaceConfig.hwAddr = HwAddrToStr(ifr.ifr_hwaddr.sa_data);
305 }
306 close(fd);
307 return ifaceConfig;
308 }
309
SetIfaceConfig(const nmd::InterfaceConfigurationParcel & ifaceConfig)310 int InterfaceManager::SetIfaceConfig(const nmd::InterfaceConfigurationParcel &ifaceConfig)
311 {
312 struct ifreq ifr = {};
313 if (strncpy_s(ifr.ifr_name, IFNAMSIZ, ifaceConfig.ifName.c_str(), ifaceConfig.ifName.length()) != 0) {
314 NETNATIVE_LOGE("ifaceConfig strncpy_s error.");
315 return -1;
316 }
317
318 if (ifaceConfig.flags.empty()) {
319 NETNATIVE_LOGE("ifaceConfig flags is empty.");
320 return -1;
321 }
322 int fd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
323 if (fd < 0) {
324 NETNATIVE_LOGE("ifaceConfig socket error, errno[%{public}d]", errno);
325 return -1;
326 }
327 if (ioctl(fd, SIOCGIFFLAGS, &ifr) == -1) {
328 char errmsg[INTERFACE_ERR_MAX_LEN] = {0};
329 strerror_r(errno, errmsg, INTERFACE_ERR_MAX_LEN);
330 NETNATIVE_LOGE("fail to set interface config. strerror[%{public}s]", errmsg);
331 close(fd);
332 return -1;
333 }
334 short flags = ifr.ifr_flags;
335 auto fit = std::find(ifaceConfig.flags.begin(), ifaceConfig.flags.end(), "up");
336 if (fit != std::end(ifaceConfig.flags)) {
337 uint16_t ifrFlags = static_cast<uint16_t>(ifr.ifr_flags);
338 ifrFlags = ifrFlags | IFF_UP;
339 ifr.ifr_flags = static_cast<short>(ifrFlags);
340 }
341 fit = std::find(ifaceConfig.flags.begin(), ifaceConfig.flags.end(), "down");
342 if (fit != std::end(ifaceConfig.flags)) {
343 ifr.ifr_flags = (short)((uint16_t)ifr.ifr_flags & (~IFF_UP));
344 }
345 if (ifr.ifr_flags == flags) {
346 close(fd);
347 return 1;
348 }
349 uint32_t retry = 0;
350 do {
351 if (ioctl(fd, SIOCSIFFLAGS, &ifr) != -1) {
352 break;
353 }
354 ++retry;
355 } while (errno == ETIMEDOUT && retry < IOCTL_RETRY_TIME);
356 NETNATIVE_LOGI("set ifr flags=[%{public}d] strerror=[%{public}s] retry=[%{public}u]", ifr.ifr_flags,
357 strerror(errno), retry);
358 close(fd);
359 return 1;
360 }
361
SetIpAddress(const std::string & ifaceName,const std::string & ipAddress)362 int InterfaceManager::SetIpAddress(const std::string &ifaceName, const std::string &ipAddress)
363 {
364 struct ifreq ifr;
365 struct in_addr ipv4Addr = {INADDR_ANY};
366
367 if (memset_s(&ifr, sizeof(ifr), 0, sizeof(ifr)) != EOK) {
368 NETNATIVE_LOGE("memset is false");
369 return -1;
370 }
371 if (strncpy_s(ifr.ifr_name, IFNAMSIZ, ifaceName.c_str(), strlen(ifaceName.c_str())) != EOK) {
372 NETNATIVE_LOGE("strncpy is false");
373 return -1;
374 }
375 if (inet_aton(ipAddress.c_str(), &ipv4Addr) == 0) {
376 NETNATIVE_LOGE("set net ip is false");
377 return -1;
378 }
379 sockaddr_in *sin = reinterpret_cast<struct sockaddr_in *>(&ifr.ifr_addr);
380 sin->sin_family = AF_INET;
381 sin->sin_port = 0;
382 sin->sin_addr = ipv4Addr;
383 int32_t inetSocket = socket(AF_INET, SOCK_DGRAM, 0);
384 if (ioctl(inetSocket, SIOCSIFADDR, &ifr) < 0) {
385 NETNATIVE_LOGE("set ip address ioctl SIOCSIFADDR error: %{public}s", strerror(errno));
386 close(inetSocket);
387 return -1;
388 }
389 close(inetSocket);
390 return 0;
391 }
392
SetIffUp(const std::string & ifaceName)393 int InterfaceManager::SetIffUp(const std::string &ifaceName)
394 {
395 struct ifreq ifr;
396
397 if (memset_s(&ifr, sizeof(ifr), 0, sizeof(ifr)) != EOK) {
398 NETNATIVE_LOGE("memset is false");
399 return -1;
400 }
401 if (strncpy_s(ifr.ifr_name, IFNAMSIZ, ifaceName.c_str(), strlen(ifaceName.c_str())) != EOK) {
402 NETNATIVE_LOGE("strncpy is false");
403 return -1;
404 }
405 uint32_t flagVal = (IFF_UP | IFF_MULTICAST);
406 ifr.ifr_flags = static_cast<short int>(flagVal);
407
408 int32_t inetSocket = socket(AF_INET, SOCK_DGRAM, 0);
409 if (ioctl(inetSocket, SIOCSIFFLAGS, &ifr) < 0) {
410 NETNATIVE_LOGE("set iface up ioctl SIOCSIFFLAGS error: %{public}s", strerror(errno));
411 close(inetSocket);
412 return -1;
413 }
414 close(inetSocket);
415 return 0;
416 }
417 } // namespace nmd
418 } // namespace OHOS
419