Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 1 | /* |
| 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 Kong | 64b481c | 2016-01-13 11:28:14 +0000 | [diff] [blame] | 21 | #include <dirent.h> |
Elliott Hughes | 2295068 | 2016-10-14 12:15:32 -0700 | [diff] [blame] | 22 | #include <fcntl.h> |
Yi Kong | fdb2963 | 2015-12-22 17:07:23 +0000 | [diff] [blame] | 23 | #include <linux/if_packet.h> |
Elliott Hughes | 5d84373 | 2016-01-11 21:30:07 -0800 | [diff] [blame] | 24 | #include <net/ethernet.h> |
| 25 | #include <net/if.h> |
| 26 | #include <netdb.h> |
Yi Kong | fdb2963 | 2015-12-22 17:07:23 +0000 | [diff] [blame] | 27 | #include <netinet/in.h> |
Yi Kong | 64b481c | 2016-01-13 11:28:14 +0000 | [diff] [blame] | 28 | #include <sys/ioctl.h> |
| 29 | |
| 30 | #include <algorithm> |
| 31 | #include <map> |
Elliott Hughes | 2295068 | 2016-10-14 12:15:32 -0700 | [diff] [blame] | 32 | #include <thread> |
Yi Kong | 64b481c | 2016-01-13 11:28:14 +0000 | [diff] [blame] | 33 | #include <vector> |
Yi Kong | fdb2963 | 2015-12-22 17:07:23 +0000 | [diff] [blame] | 34 | |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 35 | #include "utils.h" |
| 36 | |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 37 | TEST(ifaddrs, freeifaddrs_null) { |
| 38 | freeifaddrs(nullptr); |
| 39 | } |
| 40 | |
Yi Kong | 64b481c | 2016-01-13 11:28:14 +0000 | [diff] [blame] | 41 | // We can't statically say much about what network interfaces are available, but we can be pretty |
| 42 | // sure there's a loopback interface, and that it has IPv4, IPv6, and AF_PACKET entries. |
| 43 | TEST(ifaddrs, getifaddrs_lo) { |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 44 | ifaddrs* addrs = nullptr; |
| 45 | |
| 46 | ASSERT_EQ(0, getifaddrs(&addrs)); |
| 47 | ASSERT_TRUE(addrs != nullptr); |
| 48 | |
Yi Kong | fdb2963 | 2015-12-22 17:07:23 +0000 | [diff] [blame] | 49 | ifaddrs* lo_inet4 = nullptr; |
| 50 | ifaddrs* lo_inet6 = nullptr; |
| 51 | ifaddrs* lo_packet = nullptr; |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 52 | for (ifaddrs* addr = addrs; addr != nullptr; addr = addr->ifa_next) { |
Yi Kong | fdb2963 | 2015-12-22 17:07:23 +0000 | [diff] [blame] | 53 | if (addr->ifa_name && strcmp(addr->ifa_name, "lo") == 0) { |
| 54 | if (addr->ifa_addr && addr->ifa_addr->sa_family == AF_INET) lo_inet4 = addr; |
| 55 | else if (addr->ifa_addr && addr->ifa_addr->sa_family == AF_INET6) lo_inet6 = addr; |
| 56 | else if (addr->ifa_addr && addr->ifa_addr->sa_family == AF_PACKET) lo_packet = addr; |
| 57 | } |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 58 | } |
Yi Kong | fdb2963 | 2015-12-22 17:07:23 +0000 | [diff] [blame] | 59 | |
| 60 | // Does the IPv4 entry look right? |
| 61 | ASSERT_TRUE(lo_inet4 != nullptr); |
| 62 | const sockaddr_in* sa_inet4 = reinterpret_cast<const sockaddr_in*>(lo_inet4->ifa_addr); |
| 63 | ASSERT_TRUE(ntohl(sa_inet4->sin_addr.s_addr) == INADDR_LOOPBACK); |
| 64 | |
| 65 | // Does the IPv6 entry look right? |
| 66 | ASSERT_TRUE(lo_inet6 != nullptr); |
| 67 | const sockaddr_in6* sa_inet6 = reinterpret_cast<const sockaddr_in6*>(lo_inet6->ifa_addr); |
| 68 | ASSERT_TRUE(IN6_IS_ADDR_LOOPBACK(&sa_inet6->sin6_addr)); |
| 69 | |
| 70 | // Does the AF_PACKET entry look right? |
| 71 | ASSERT_TRUE(lo_packet != nullptr); |
| 72 | const sockaddr_ll* sa_ll = reinterpret_cast<const sockaddr_ll*>(lo_packet->ifa_addr); |
| 73 | ASSERT_EQ(6, sa_ll->sll_halen); |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 74 | |
| 75 | freeifaddrs(addrs); |
| 76 | } |
Elliott Hughes | 5d84373 | 2016-01-11 21:30:07 -0800 | [diff] [blame] | 77 | |
Yi Kong | 64b481c | 2016-01-13 11:28:14 +0000 | [diff] [blame] | 78 | // Check that getifaddrs sees the same list of interfaces as /sys/class/net. |
| 79 | TEST(ifaddrs, getifaddrs_interfaces) { |
| 80 | std::vector<std::string> ifaddrs_socks; |
| 81 | { |
| 82 | ifaddrs* addrs; |
| 83 | ASSERT_EQ(0, getifaddrs(&addrs)); |
| 84 | |
| 85 | for (ifaddrs* addr = addrs; addr != nullptr; addr = addr->ifa_next) { |
| 86 | int family = addr->ifa_addr ? addr->ifa_addr->sa_family : |
| 87 | addr->ifa_broadaddr ? addr->ifa_broadaddr->sa_family : |
| 88 | AF_UNSPEC; |
| 89 | |
| 90 | if (family == AF_PACKET || family == AF_UNSPEC) { |
| 91 | ifaddrs_socks.push_back(std::string(addr->ifa_name)); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | freeifaddrs(addrs); |
| 96 | } |
| 97 | |
| 98 | std::vector<std::string> sys_class_net; |
| 99 | { |
Elliott Hughes | 7dac8b8 | 2016-02-17 14:19:48 -0800 | [diff] [blame] | 100 | std::unique_ptr<DIR, decltype(&closedir)> d(opendir("/sys/class/net"), closedir); |
Yi Kong | 64b481c | 2016-01-13 11:28:14 +0000 | [diff] [blame] | 101 | ASSERT_TRUE(d != nullptr); |
| 102 | dirent* dir; |
| 103 | while ((dir = readdir(d.get())) != nullptr) { |
| 104 | if (dir->d_type == DT_LNK) { |
| 105 | sys_class_net.push_back(std::string(dir->d_name)); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | ASSERT_TRUE(std::is_permutation(ifaddrs_socks.begin(), ifaddrs_socks.end(), |
| 111 | sys_class_net.begin())); |
| 112 | } |
| 113 | |
Elliott Hughes | bf97770 | 2016-02-29 13:31:59 -0800 | [diff] [blame] | 114 | static void CheckAddressIsInSet(const std::string& if_name, bool unicast, |
| 115 | const std::set<in_addr_t>& addrs) { |
| 116 | ifreq ifr; |
| 117 | memset(&ifr, 0, sizeof(ifr)); |
| 118 | ifr.ifr_addr.sa_family = AF_INET; |
| 119 | if_name.copy(ifr.ifr_name, IFNAMSIZ - 1); |
| 120 | |
| 121 | int fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 122 | ASSERT_TRUE(fd != -1); |
| 123 | |
| 124 | int request = SIOCGIFADDR; |
| 125 | if (!unicast) { |
| 126 | // For non-unicast, the specific ioctl to use depends on whether the IFF_BROADCAST flag is set. |
| 127 | ASSERT_EQ(0, ioctl(fd, SIOCGIFFLAGS, &ifr)) << if_name << ' ' << strerror(errno); |
| 128 | request = ((ifr.ifr_flags & IFF_BROADCAST) != 0) ? SIOCGIFBRDADDR : SIOCGIFDSTADDR; |
| 129 | } |
| 130 | |
| 131 | ASSERT_EQ(0, ioctl(fd, request, &ifr)) << if_name << ' ' << strerror(errno); |
| 132 | close(fd); |
| 133 | |
| 134 | sockaddr_in* sock = reinterpret_cast<sockaddr_in*>(&ifr.ifr_addr); |
| 135 | in_addr_t addr = sock->sin_addr.s_addr; |
| 136 | |
| 137 | EXPECT_TRUE(addrs.find(addr) != addrs.end()) << if_name << ' ' << std::hex << ntohl(addr); |
| 138 | } |
| 139 | |
Yi Kong | 64b481c | 2016-01-13 11:28:14 +0000 | [diff] [blame] | 140 | TEST(ifaddrs, getifaddrs_INET) { |
Elliott Hughes | bf97770 | 2016-02-29 13:31:59 -0800 | [diff] [blame] | 141 | std::map<std::string, std::set<in_addr_t>> inet_addrs; |
| 142 | std::map<std::string, std::set<in_addr_t>> broad_addrs; |
Yi Kong | 64b481c | 2016-01-13 11:28:14 +0000 | [diff] [blame] | 143 | |
Elliott Hughes | 2d5e21f | 2016-02-03 07:42:33 -0800 | [diff] [blame] | 144 | // Collect the IPv4 addresses for each interface. |
| 145 | ifaddrs* addrs; |
| 146 | ASSERT_EQ(0, getifaddrs(&addrs)); |
| 147 | for (ifaddrs* addr = addrs; addr != nullptr; addr = addr->ifa_next) { |
| 148 | if (addr->ifa_name && addr->ifa_addr && addr->ifa_addr->sa_family == AF_INET) { |
| 149 | auto sock = reinterpret_cast<sockaddr_in*>(addr->ifa_addr); |
Elliott Hughes | bf97770 | 2016-02-29 13:31:59 -0800 | [diff] [blame] | 150 | inet_addrs[addr->ifa_name].insert(sock->sin_addr.s_addr); |
Yi Kong | 64b481c | 2016-01-13 11:28:14 +0000 | [diff] [blame] | 151 | } |
Elliott Hughes | 2d5e21f | 2016-02-03 07:42:33 -0800 | [diff] [blame] | 152 | if (addr->ifa_name && addr->ifa_broadaddr && addr->ifa_broadaddr->sa_family == AF_INET) { |
| 153 | auto sock = reinterpret_cast<sockaddr_in*>(addr->ifa_broadaddr); |
Elliott Hughes | bf97770 | 2016-02-29 13:31:59 -0800 | [diff] [blame] | 154 | broad_addrs[addr->ifa_name].insert(sock->sin_addr.s_addr); |
Elliott Hughes | 2d5e21f | 2016-02-03 07:42:33 -0800 | [diff] [blame] | 155 | } |
Yi Kong | 64b481c | 2016-01-13 11:28:14 +0000 | [diff] [blame] | 156 | } |
Elliott Hughes | 2d5e21f | 2016-02-03 07:42:33 -0800 | [diff] [blame] | 157 | freeifaddrs(addrs); |
Yi Kong | 64b481c | 2016-01-13 11:28:14 +0000 | [diff] [blame] | 158 | |
Elliott Hughes | bf97770 | 2016-02-29 13:31:59 -0800 | [diff] [blame] | 159 | // Check that the addresses returned by the SIOCGIFADDR and SIOCGIFBRDADDR/SIOCGIFDSTADDR ioctls |
Elliott Hughes | 2d5e21f | 2016-02-03 07:42:33 -0800 | [diff] [blame] | 160 | // are in our collections. |
Elliott Hughes | bf97770 | 2016-02-29 13:31:59 -0800 | [diff] [blame] | 161 | for (const auto& it : inet_addrs) CheckAddressIsInSet(it.first, true, it.second); |
| 162 | for (const auto& it : broad_addrs) CheckAddressIsInSet(it.first, false, it.second); |
Yi Kong | 64b481c | 2016-01-13 11:28:14 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Elliott Hughes | 5d84373 | 2016-01-11 21:30:07 -0800 | [diff] [blame] | 165 | static void print_sockaddr_ll(const char* what, const sockaddr* p) { |
| 166 | const sockaddr_ll* s = reinterpret_cast<const sockaddr_ll*>(p); |
Elliott Hughes | ed57b98 | 2016-01-15 21:02:56 -0800 | [diff] [blame] | 167 | printf("\t\t%s\t", what); |
Elliott Hughes | 5d84373 | 2016-01-11 21:30:07 -0800 | [diff] [blame] | 168 | for (int i = 0; i < s->sll_halen; ++i) { |
| 169 | if (i > 0) printf(":"); |
| 170 | printf("%02X", s->sll_addr[i]); |
| 171 | } |
| 172 | printf(" (%d bytes)\n", s->sll_halen); |
| 173 | } |
| 174 | |
| 175 | static void print_sockaddr_inet(const char* what, const sockaddr* addr) { |
| 176 | char host[NI_MAXHOST]; |
| 177 | int family = addr->sa_family; |
| 178 | int error = getnameinfo(addr, |
| 179 | (family == AF_INET) ? sizeof(sockaddr_in) : sizeof(sockaddr_in6), |
| 180 | host, NI_MAXHOST, nullptr, 0, NI_NUMERICHOST); |
| 181 | if (error != 0) { |
| 182 | printf("%d getnameinfo() failed: %s\n", family, gai_strerror(error)); |
| 183 | strcpy(host, "???"); |
| 184 | } |
Elliott Hughes | ed57b98 | 2016-01-15 21:02:56 -0800 | [diff] [blame] | 185 | printf("\t\t%s: <%s>\n", what, host); |
Elliott Hughes | 5d84373 | 2016-01-11 21:30:07 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Elliott Hughes | bf97770 | 2016-02-29 13:31:59 -0800 | [diff] [blame] | 188 | static const char* FamilyToName(int family) { |
Elliott Hughes | 5d84373 | 2016-01-11 21:30:07 -0800 | [diff] [blame] | 189 | if (family == AF_INET) return "AF_INET"; |
| 190 | if (family == AF_INET6) return "AF_INET6"; |
| 191 | if (family == AF_PACKET) return "AF_PACKET"; |
| 192 | if (family == AF_UNSPEC) return "AF_UNSPEC"; |
| 193 | return "?"; |
| 194 | } |
| 195 | |
Elliott Hughes | bf97770 | 2016-02-29 13:31:59 -0800 | [diff] [blame] | 196 | static std::string FlagsToString(short flags) { |
| 197 | std::string result; |
| 198 | if ((flags & IFF_UP) != 0) result += " UP"; |
| 199 | if ((flags & IFF_BROADCAST) != 0) result += " BROADCAST"; |
| 200 | if ((flags & IFF_DEBUG) != 0) result += " DEBUG"; |
| 201 | if ((flags & IFF_LOOPBACK) != 0) result += " LOOPBACK"; |
| 202 | if ((flags & IFF_POINTOPOINT) != 0) result += " POINTOPOINT"; |
| 203 | if ((flags & IFF_NOTRAILERS) != 0) result += " NOTRAILERS"; |
| 204 | if ((flags & IFF_RUNNING) != 0) result += " RUNNING"; |
| 205 | if ((flags & IFF_NOARP) != 0) result += " NOARP"; |
| 206 | if ((flags & IFF_PROMISC) != 0) result += " PROMISC"; |
| 207 | if ((flags & IFF_ALLMULTI) != 0) result += " ALLMULTI"; |
| 208 | if ((flags & IFF_MASTER) != 0) result += " MASTER"; |
| 209 | if ((flags & IFF_SLAVE) != 0) result += " SLAVE"; |
| 210 | if ((flags & IFF_MULTICAST) != 0) result += " MULTICAST"; |
| 211 | if ((flags & IFF_PORTSEL) != 0) result += " PORTSEL"; |
| 212 | if ((flags & IFF_AUTOMEDIA) != 0) result += " AUTOMEDIA"; |
| 213 | if ((flags & IFF_DYNAMIC) != 0) result += " DYNAMIC"; |
| 214 | #if defined(IFF_LOWER_UP) |
| 215 | if ((flags & IFF_LOWER_UP) != 0) result += " LOWER_UP"; |
| 216 | #endif |
| 217 | #if defined(IFF_DORMANT) |
| 218 | if ((flags & IFF_DORMANT) != 0) result += " DORMANT"; |
| 219 | #endif |
| 220 | #if defined(IFF_ECHO) |
| 221 | if ((flags & IFF_ECHO) != 0) result += " ECHO"; |
| 222 | #endif |
| 223 | return result; |
| 224 | } |
| 225 | |
Elliott Hughes | 5d84373 | 2016-01-11 21:30:07 -0800 | [diff] [blame] | 226 | // Not really a test, but a useful debugging tool. |
| 227 | TEST(ifaddrs, dump) { |
| 228 | ifaddrs* addrs; |
| 229 | ASSERT_EQ(0, getifaddrs(&addrs)); |
| 230 | |
| 231 | for (ifaddrs* ifa = addrs; ifa != nullptr; ifa = ifa->ifa_next) { |
| 232 | int family = ifa->ifa_addr ? ifa->ifa_addr->sa_family : |
| 233 | ifa->ifa_broadaddr ? ifa->ifa_broadaddr->sa_family : AF_UNSPEC; |
| 234 | |
Elliott Hughes | ed57b98 | 2016-01-15 21:02:56 -0800 | [diff] [blame] | 235 | printf("\t%s\n" |
Elliott Hughes | bf97770 | 2016-02-29 13:31:59 -0800 | [diff] [blame] | 236 | "\t\t%s (%d) flags=%#x%s\n", |
| 237 | ifa->ifa_name, FamilyToName(family), family, |
| 238 | ifa->ifa_flags, FlagsToString(ifa->ifa_flags).c_str()); |
Elliott Hughes | 5d84373 | 2016-01-11 21:30:07 -0800 | [diff] [blame] | 239 | |
| 240 | if (family == AF_PACKET) { |
| 241 | if (ifa->ifa_addr) print_sockaddr_ll("hwaddr", ifa->ifa_addr); |
| 242 | if (ifa->ifa_broadaddr) print_sockaddr_ll("hwbroad", ifa->ifa_addr); |
| 243 | } else if (family == AF_INET || family == AF_INET6) { |
| 244 | if (ifa->ifa_addr) print_sockaddr_inet("address", ifa->ifa_addr); |
| 245 | if (ifa->ifa_broadaddr && (ifa->ifa_flags & (IFF_BROADCAST | IFF_POINTOPOINT)) != 0) { |
| 246 | print_sockaddr_inet((ifa->ifa_flags & IFF_BROADCAST) ? "broadcast" : "destination", |
| 247 | ifa->ifa_broadaddr); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | fflush(stdout); |
| 252 | } |
| 253 | |
| 254 | freeifaddrs(addrs); |
| 255 | } |
Elliott Hughes | 7dac8b8 | 2016-02-17 14:19:48 -0800 | [diff] [blame] | 256 | |
| 257 | TEST(ifaddrs, inet6_scope_ids) { |
| 258 | ifaddrs* addrs; |
| 259 | ASSERT_EQ(0, getifaddrs(&addrs)); |
| 260 | |
| 261 | for (ifaddrs* ifa = addrs; ifa != nullptr; ifa = ifa->ifa_next) { |
| 262 | if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) { |
| 263 | sockaddr_in6* sa6 = reinterpret_cast<sockaddr_in6*>(ifa->ifa_addr); |
| 264 | // Any link-local IPv6 address should have a scope id. (http://b/27219454.) |
| 265 | // 0 isn't a valid interface index, so that would mean the scope id wasn't set. |
| 266 | if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr) || IN6_IS_ADDR_MC_LINKLOCAL(&sa6->sin6_addr)) { |
| 267 | ASSERT_NE(sa6->sin6_scope_id, 0U); |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | freeifaddrs(addrs); |
| 273 | } |
Elliott Hughes | 2295068 | 2016-10-14 12:15:32 -0700 | [diff] [blame] | 274 | |
| 275 | TEST(ifaddrs, kernel_bug_31038971) { |
| 276 | // Some kernels had a bug that would lead to an NLMSG_ERROR response, |
| 277 | // but bionic wasn't setting errno based on the value in the message. |
| 278 | // This is the test for the kernel bug, but on a device with a bad |
| 279 | // kernel this test was also useful for testing the bionic errno fix. |
| 280 | std::vector<std::thread*> threads; |
| 281 | for (size_t i = 0; i < 128; ++i) { |
| 282 | threads.push_back(new std::thread([]() { |
| 283 | ifaddrs* addrs = nullptr; |
| 284 | ASSERT_EQ(0, getifaddrs(&addrs)) << strerror(errno); |
| 285 | freeifaddrs(addrs); |
| 286 | })); |
| 287 | } |
| 288 | for (auto& t : threads) { |
| 289 | t->join(); |
| 290 | delete t; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | TEST(ifaddrs, errno_EMFILE) { |
| 295 | std::vector<int> fds; |
| 296 | while (true) { |
| 297 | int fd = open("/dev/null", O_RDONLY|O_CLOEXEC); |
| 298 | if (fd == -1) { |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 299 | ASSERT_ERRNO(EMFILE); |
Elliott Hughes | 2295068 | 2016-10-14 12:15:32 -0700 | [diff] [blame] | 300 | break; |
| 301 | } |
| 302 | fds.push_back(fd); |
| 303 | } |
| 304 | |
| 305 | ifaddrs* addrs; |
| 306 | EXPECT_EQ(-1, getifaddrs(&addrs)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 307 | EXPECT_ERRNO(EMFILE); |
Elliott Hughes | 2295068 | 2016-10-14 12:15:32 -0700 | [diff] [blame] | 308 | |
| 309 | for (int fd : fds) close(fd); |
| 310 | } |