blob: 01779f7a3660508d7ae6d996a389a115f0ebb514 [file] [log] [blame]
Elliott Hughes9cddb482016-01-04 20:38:05 +00001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18
19#include <ifaddrs.h>
20
Yi Kong64b481c2016-01-13 11:28:14 +000021#include <dirent.h>
Elliott Hughes22950682016-10-14 12:15:32 -070022#include <fcntl.h>
Elliott Hughes5d843732016-01-11 21:30:07 -080023#include <net/ethernet.h>
24#include <net/if.h>
25#include <netdb.h>
Yi Kongfdb29632015-12-22 17:07:23 +000026#include <netinet/in.h>
Yi Kong64b481c2016-01-13 11:28:14 +000027#include <sys/ioctl.h>
28
Elliott Hughes2ddb20d2024-01-04 16:44:57 -080029// (glibc as of 2.37 has redefinitions if you include these before <net/if.h>.)
30#include <linux/if.h>
31#include <linux/if_packet.h>
32
Yi Kong64b481c2016-01-13 11:28:14 +000033#include <algorithm>
34#include <map>
Elliott Hughes22950682016-10-14 12:15:32 -070035#include <thread>
Yi Kong64b481c2016-01-13 11:28:14 +000036#include <vector>
Yi Kongfdb29632015-12-22 17:07:23 +000037
Elliott Hughes95646e62023-09-21 14:11:19 -070038#include "utils.h"
39
Elliott Hughes9cddb482016-01-04 20:38:05 +000040TEST(ifaddrs, freeifaddrs_null) {
41 freeifaddrs(nullptr);
42}
43
Yi Kong64b481c2016-01-13 11:28:14 +000044// We can't statically say much about what network interfaces are available, but we can be pretty
45// sure there's a loopback interface, and that it has IPv4, IPv6, and AF_PACKET entries.
46TEST(ifaddrs, getifaddrs_lo) {
Elliott Hughes9cddb482016-01-04 20:38:05 +000047 ifaddrs* addrs = nullptr;
48
49 ASSERT_EQ(0, getifaddrs(&addrs));
50 ASSERT_TRUE(addrs != nullptr);
51
Yi Kongfdb29632015-12-22 17:07:23 +000052 ifaddrs* lo_inet4 = nullptr;
53 ifaddrs* lo_inet6 = nullptr;
54 ifaddrs* lo_packet = nullptr;
Elliott Hughes9cddb482016-01-04 20:38:05 +000055 for (ifaddrs* addr = addrs; addr != nullptr; addr = addr->ifa_next) {
Yi Kongfdb29632015-12-22 17:07:23 +000056 if (addr->ifa_name && strcmp(addr->ifa_name, "lo") == 0) {
57 if (addr->ifa_addr && addr->ifa_addr->sa_family == AF_INET) lo_inet4 = addr;
58 else if (addr->ifa_addr && addr->ifa_addr->sa_family == AF_INET6) lo_inet6 = addr;
59 else if (addr->ifa_addr && addr->ifa_addr->sa_family == AF_PACKET) lo_packet = addr;
60 }
Elliott Hughes9cddb482016-01-04 20:38:05 +000061 }
Yi Kongfdb29632015-12-22 17:07:23 +000062
63 // Does the IPv4 entry look right?
64 ASSERT_TRUE(lo_inet4 != nullptr);
65 const sockaddr_in* sa_inet4 = reinterpret_cast<const sockaddr_in*>(lo_inet4->ifa_addr);
66 ASSERT_TRUE(ntohl(sa_inet4->sin_addr.s_addr) == INADDR_LOOPBACK);
67
68 // Does the IPv6 entry look right?
69 ASSERT_TRUE(lo_inet6 != nullptr);
70 const sockaddr_in6* sa_inet6 = reinterpret_cast<const sockaddr_in6*>(lo_inet6->ifa_addr);
71 ASSERT_TRUE(IN6_IS_ADDR_LOOPBACK(&sa_inet6->sin6_addr));
72
73 // Does the AF_PACKET entry look right?
74 ASSERT_TRUE(lo_packet != nullptr);
75 const sockaddr_ll* sa_ll = reinterpret_cast<const sockaddr_ll*>(lo_packet->ifa_addr);
76 ASSERT_EQ(6, sa_ll->sll_halen);
Elliott Hughes9cddb482016-01-04 20:38:05 +000077
78 freeifaddrs(addrs);
79}
Elliott Hughes5d843732016-01-11 21:30:07 -080080
Yi Kong64b481c2016-01-13 11:28:14 +000081// Check that getifaddrs sees the same list of interfaces as /sys/class/net.
82TEST(ifaddrs, getifaddrs_interfaces) {
83 std::vector<std::string> ifaddrs_socks;
84 {
85 ifaddrs* addrs;
86 ASSERT_EQ(0, getifaddrs(&addrs));
87
88 for (ifaddrs* addr = addrs; addr != nullptr; addr = addr->ifa_next) {
89 int family = addr->ifa_addr ? addr->ifa_addr->sa_family :
90 addr->ifa_broadaddr ? addr->ifa_broadaddr->sa_family :
91 AF_UNSPEC;
92
93 if (family == AF_PACKET || family == AF_UNSPEC) {
94 ifaddrs_socks.push_back(std::string(addr->ifa_name));
95 }
96 }
97
98 freeifaddrs(addrs);
99 }
100
101 std::vector<std::string> sys_class_net;
102 {
Elliott Hughes7dac8b82016-02-17 14:19:48 -0800103 std::unique_ptr<DIR, decltype(&closedir)> d(opendir("/sys/class/net"), closedir);
Yi Kong64b481c2016-01-13 11:28:14 +0000104 ASSERT_TRUE(d != nullptr);
105 dirent* dir;
106 while ((dir = readdir(d.get())) != nullptr) {
107 if (dir->d_type == DT_LNK) {
108 sys_class_net.push_back(std::string(dir->d_name));
109 }
110 }
111 }
112
113 ASSERT_TRUE(std::is_permutation(ifaddrs_socks.begin(), ifaddrs_socks.end(),
114 sys_class_net.begin()));
115}
116
Elliott Hughesbf977702016-02-29 13:31:59 -0800117static void CheckAddressIsInSet(const std::string& if_name, bool unicast,
118 const std::set<in_addr_t>& addrs) {
Christopher Ferris2a391882024-12-19 13:44:35 -0800119 ifreq ifr = {.ifr_addr.sa_family = AF_INET};
Elliott Hughesbf977702016-02-29 13:31:59 -0800120 if_name.copy(ifr.ifr_name, IFNAMSIZ - 1);
121
122 int fd = socket(AF_INET, SOCK_DGRAM, 0);
123 ASSERT_TRUE(fd != -1);
124
125 int request = SIOCGIFADDR;
126 if (!unicast) {
127 // For non-unicast, the specific ioctl to use depends on whether the IFF_BROADCAST flag is set.
128 ASSERT_EQ(0, ioctl(fd, SIOCGIFFLAGS, &ifr)) << if_name << ' ' << strerror(errno);
129 request = ((ifr.ifr_flags & IFF_BROADCAST) != 0) ? SIOCGIFBRDADDR : SIOCGIFDSTADDR;
130 }
131
132 ASSERT_EQ(0, ioctl(fd, request, &ifr)) << if_name << ' ' << strerror(errno);
133 close(fd);
134
135 sockaddr_in* sock = reinterpret_cast<sockaddr_in*>(&ifr.ifr_addr);
136 in_addr_t addr = sock->sin_addr.s_addr;
137
Elliott Hughesf3724772024-06-25 11:18:09 +0000138 EXPECT_TRUE(addrs.contains(addr)) << if_name << ' ' << std::hex << ntohl(addr);
Elliott Hughesbf977702016-02-29 13:31:59 -0800139}
140
Yi Kong64b481c2016-01-13 11:28:14 +0000141TEST(ifaddrs, getifaddrs_INET) {
Elliott Hughesbf977702016-02-29 13:31:59 -0800142 std::map<std::string, std::set<in_addr_t>> inet_addrs;
143 std::map<std::string, std::set<in_addr_t>> broad_addrs;
Yi Kong64b481c2016-01-13 11:28:14 +0000144
Elliott Hughes2d5e21f2016-02-03 07:42:33 -0800145 // Collect the IPv4 addresses for each interface.
146 ifaddrs* addrs;
147 ASSERT_EQ(0, getifaddrs(&addrs));
148 for (ifaddrs* addr = addrs; addr != nullptr; addr = addr->ifa_next) {
149 if (addr->ifa_name && addr->ifa_addr && addr->ifa_addr->sa_family == AF_INET) {
150 auto sock = reinterpret_cast<sockaddr_in*>(addr->ifa_addr);
Elliott Hughesbf977702016-02-29 13:31:59 -0800151 inet_addrs[addr->ifa_name].insert(sock->sin_addr.s_addr);
Yi Kong64b481c2016-01-13 11:28:14 +0000152 }
Elliott Hughes2d5e21f2016-02-03 07:42:33 -0800153 if (addr->ifa_name && addr->ifa_broadaddr && addr->ifa_broadaddr->sa_family == AF_INET) {
154 auto sock = reinterpret_cast<sockaddr_in*>(addr->ifa_broadaddr);
Elliott Hughesbf977702016-02-29 13:31:59 -0800155 broad_addrs[addr->ifa_name].insert(sock->sin_addr.s_addr);
Elliott Hughes2d5e21f2016-02-03 07:42:33 -0800156 }
Yi Kong64b481c2016-01-13 11:28:14 +0000157 }
Elliott Hughes2d5e21f2016-02-03 07:42:33 -0800158 freeifaddrs(addrs);
Yi Kong64b481c2016-01-13 11:28:14 +0000159
Elliott Hughesbf977702016-02-29 13:31:59 -0800160 // Check that the addresses returned by the SIOCGIFADDR and SIOCGIFBRDADDR/SIOCGIFDSTADDR ioctls
Elliott Hughes2d5e21f2016-02-03 07:42:33 -0800161 // are in our collections.
Elliott Hughesbf977702016-02-29 13:31:59 -0800162 for (const auto& it : inet_addrs) CheckAddressIsInSet(it.first, true, it.second);
163 for (const auto& it : broad_addrs) CheckAddressIsInSet(it.first, false, it.second);
Yi Kong64b481c2016-01-13 11:28:14 +0000164}
165
Elliott Hughes5d843732016-01-11 21:30:07 -0800166static void print_sockaddr_ll(const char* what, const sockaddr* p) {
167 const sockaddr_ll* s = reinterpret_cast<const sockaddr_ll*>(p);
Elliott Hughesed57b982016-01-15 21:02:56 -0800168 printf("\t\t%s\t", what);
Elliott Hughes5d843732016-01-11 21:30:07 -0800169 for (int i = 0; i < s->sll_halen; ++i) {
170 if (i > 0) printf(":");
171 printf("%02X", s->sll_addr[i]);
172 }
173 printf(" (%d bytes)\n", s->sll_halen);
174}
175
176static void print_sockaddr_inet(const char* what, const sockaddr* addr) {
177 char host[NI_MAXHOST];
178 int family = addr->sa_family;
179 int error = getnameinfo(addr,
180 (family == AF_INET) ? sizeof(sockaddr_in) : sizeof(sockaddr_in6),
181 host, NI_MAXHOST, nullptr, 0, NI_NUMERICHOST);
182 if (error != 0) {
183 printf("%d getnameinfo() failed: %s\n", family, gai_strerror(error));
184 strcpy(host, "???");
185 }
Elliott Hughesed57b982016-01-15 21:02:56 -0800186 printf("\t\t%s: <%s>\n", what, host);
Elliott Hughes5d843732016-01-11 21:30:07 -0800187}
188
Elliott Hughesbf977702016-02-29 13:31:59 -0800189static const char* FamilyToName(int family) {
Elliott Hughes5d843732016-01-11 21:30:07 -0800190 if (family == AF_INET) return "AF_INET";
191 if (family == AF_INET6) return "AF_INET6";
192 if (family == AF_PACKET) return "AF_PACKET";
193 if (family == AF_UNSPEC) return "AF_UNSPEC";
194 return "?";
195}
196
Elliott Hughesbf977702016-02-29 13:31:59 -0800197static std::string FlagsToString(short flags) {
198 std::string result;
199 if ((flags & IFF_UP) != 0) result += " UP";
200 if ((flags & IFF_BROADCAST) != 0) result += " BROADCAST";
201 if ((flags & IFF_DEBUG) != 0) result += " DEBUG";
202 if ((flags & IFF_LOOPBACK) != 0) result += " LOOPBACK";
203 if ((flags & IFF_POINTOPOINT) != 0) result += " POINTOPOINT";
204 if ((flags & IFF_NOTRAILERS) != 0) result += " NOTRAILERS";
205 if ((flags & IFF_RUNNING) != 0) result += " RUNNING";
206 if ((flags & IFF_NOARP) != 0) result += " NOARP";
207 if ((flags & IFF_PROMISC) != 0) result += " PROMISC";
208 if ((flags & IFF_ALLMULTI) != 0) result += " ALLMULTI";
209 if ((flags & IFF_MASTER) != 0) result += " MASTER";
210 if ((flags & IFF_SLAVE) != 0) result += " SLAVE";
211 if ((flags & IFF_MULTICAST) != 0) result += " MULTICAST";
212 if ((flags & IFF_PORTSEL) != 0) result += " PORTSEL";
213 if ((flags & IFF_AUTOMEDIA) != 0) result += " AUTOMEDIA";
214 if ((flags & IFF_DYNAMIC) != 0) result += " DYNAMIC";
Elliott Hughesbf977702016-02-29 13:31:59 -0800215 if ((flags & IFF_LOWER_UP) != 0) result += " LOWER_UP";
Elliott Hughesbf977702016-02-29 13:31:59 -0800216 if ((flags & IFF_DORMANT) != 0) result += " DORMANT";
Elliott Hughesbf977702016-02-29 13:31:59 -0800217 if ((flags & IFF_ECHO) != 0) result += " ECHO";
Elliott Hughesbf977702016-02-29 13:31:59 -0800218 return result;
219}
220
Elliott Hughes5d843732016-01-11 21:30:07 -0800221// Not really a test, but a useful debugging tool.
222TEST(ifaddrs, dump) {
223 ifaddrs* addrs;
224 ASSERT_EQ(0, getifaddrs(&addrs));
225
226 for (ifaddrs* ifa = addrs; ifa != nullptr; ifa = ifa->ifa_next) {
227 int family = ifa->ifa_addr ? ifa->ifa_addr->sa_family :
228 ifa->ifa_broadaddr ? ifa->ifa_broadaddr->sa_family : AF_UNSPEC;
229
Elliott Hughesed57b982016-01-15 21:02:56 -0800230 printf("\t%s\n"
Elliott Hughesbf977702016-02-29 13:31:59 -0800231 "\t\t%s (%d) flags=%#x%s\n",
232 ifa->ifa_name, FamilyToName(family), family,
233 ifa->ifa_flags, FlagsToString(ifa->ifa_flags).c_str());
Elliott Hughes5d843732016-01-11 21:30:07 -0800234
235 if (family == AF_PACKET) {
236 if (ifa->ifa_addr) print_sockaddr_ll("hwaddr", ifa->ifa_addr);
237 if (ifa->ifa_broadaddr) print_sockaddr_ll("hwbroad", ifa->ifa_addr);
238 } else if (family == AF_INET || family == AF_INET6) {
239 if (ifa->ifa_addr) print_sockaddr_inet("address", ifa->ifa_addr);
240 if (ifa->ifa_broadaddr && (ifa->ifa_flags & (IFF_BROADCAST | IFF_POINTOPOINT)) != 0) {
241 print_sockaddr_inet((ifa->ifa_flags & IFF_BROADCAST) ? "broadcast" : "destination",
242 ifa->ifa_broadaddr);
243 }
244 }
245
246 fflush(stdout);
247 }
248
249 freeifaddrs(addrs);
250}
Elliott Hughes7dac8b82016-02-17 14:19:48 -0800251
252TEST(ifaddrs, inet6_scope_ids) {
253 ifaddrs* addrs;
254 ASSERT_EQ(0, getifaddrs(&addrs));
255
256 for (ifaddrs* ifa = addrs; ifa != nullptr; ifa = ifa->ifa_next) {
257 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
258 sockaddr_in6* sa6 = reinterpret_cast<sockaddr_in6*>(ifa->ifa_addr);
259 // Any link-local IPv6 address should have a scope id. (http://b/27219454.)
260 // 0 isn't a valid interface index, so that would mean the scope id wasn't set.
261 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr) || IN6_IS_ADDR_MC_LINKLOCAL(&sa6->sin6_addr)) {
262 ASSERT_NE(sa6->sin6_scope_id, 0U);
263 }
264 }
265 }
266
267 freeifaddrs(addrs);
268}
Elliott Hughes22950682016-10-14 12:15:32 -0700269
270TEST(ifaddrs, kernel_bug_31038971) {
271 // Some kernels had a bug that would lead to an NLMSG_ERROR response,
272 // but bionic wasn't setting errno based on the value in the message.
273 // This is the test for the kernel bug, but on a device with a bad
274 // kernel this test was also useful for testing the bionic errno fix.
275 std::vector<std::thread*> threads;
276 for (size_t i = 0; i < 128; ++i) {
277 threads.push_back(new std::thread([]() {
278 ifaddrs* addrs = nullptr;
279 ASSERT_EQ(0, getifaddrs(&addrs)) << strerror(errno);
280 freeifaddrs(addrs);
281 }));
282 }
283 for (auto& t : threads) {
284 t->join();
285 delete t;
286 }
287}
288
289TEST(ifaddrs, errno_EMFILE) {
290 std::vector<int> fds;
291 while (true) {
292 int fd = open("/dev/null", O_RDONLY|O_CLOEXEC);
293 if (fd == -1) {
Elliott Hughes95646e62023-09-21 14:11:19 -0700294 ASSERT_ERRNO(EMFILE);
Elliott Hughes22950682016-10-14 12:15:32 -0700295 break;
296 }
297 fds.push_back(fd);
298 }
299
300 ifaddrs* addrs;
301 EXPECT_EQ(-1, getifaddrs(&addrs));
Elliott Hughes95646e62023-09-21 14:11:19 -0700302 EXPECT_ERRNO(EMFILE);
Elliott Hughes22950682016-10-14 12:15:32 -0700303
304 for (int fd : fds) close(fd);
305}