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