Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <ifaddrs.h> |
| 30 | |
Bram Bonné | 8e20ac4 | 2020-07-20 11:18:03 +0200 | [diff] [blame] | 31 | #include <async_safe/log.h> |
Bram Bonné | d3df35e | 2020-01-23 17:05:42 +0100 | [diff] [blame] | 32 | #include <cutils/misc.h> // FIRST_APPLICATION_UID |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 33 | #include <errno.h> |
Yi Kong | fdb2963 | 2015-12-22 17:07:23 +0000 | [diff] [blame] | 34 | #include <linux/if_packet.h> |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 35 | #include <net/if.h> |
| 36 | #include <netinet/in.h> |
| 37 | #include <stdint.h> |
| 38 | #include <stdio.h> |
| 39 | #include <stdlib.h> |
| 40 | #include <string.h> |
| 41 | #include <unistd.h> |
| 42 | |
Elliott Hughes | ed57b98 | 2016-01-15 21:02:56 -0800 | [diff] [blame] | 43 | #include "private/ErrnoRestorer.h" |
| 44 | |
Bram Bonné | 95ca52a | 2020-12-03 19:03:55 +0100 | [diff] [blame^] | 45 | #include "bionic_appcompat.h" |
Elliott Hughes | ed57b98 | 2016-01-15 21:02:56 -0800 | [diff] [blame] | 46 | #include "bionic_netlink.h" |
| 47 | |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 48 | // The public ifaddrs struct is full of pointers. Rather than track several |
| 49 | // different allocations, we use a maximally-sized structure with the public |
| 50 | // part at offset 0, and pointers into its hidden tail. |
| 51 | struct ifaddrs_storage { |
| 52 | // Must come first, so that `ifaddrs_storage` is-a `ifaddrs`. |
| 53 | ifaddrs ifa; |
| 54 | |
| 55 | // The interface index, so we can match RTM_NEWADDR messages with |
| 56 | // earlier RTM_NEWLINK messages (to copy the interface flags). |
| 57 | int interface_index; |
| 58 | |
| 59 | // Storage for the pointers in `ifa`. |
| 60 | sockaddr_storage addr; |
| 61 | sockaddr_storage netmask; |
| 62 | sockaddr_storage ifa_ifu; |
| 63 | char name[IFNAMSIZ + 1]; |
| 64 | |
Chih-Hung Hsieh | 62e3a07 | 2016-05-03 12:08:05 -0700 | [diff] [blame] | 65 | explicit ifaddrs_storage(ifaddrs** list) { |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 66 | memset(this, 0, sizeof(*this)); |
| 67 | |
| 68 | // push_front onto `list`. |
| 69 | ifa.ifa_next = *list; |
| 70 | *list = reinterpret_cast<ifaddrs*>(this); |
| 71 | } |
| 72 | |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 73 | void SetAddress(int family, const void* data, size_t byteCount) { |
Elliott Hughes | ef925e5 | 2016-03-01 17:27:12 -0800 | [diff] [blame] | 74 | // The kernel currently uses the order IFA_ADDRESS, IFA_LOCAL, IFA_BROADCAST |
| 75 | // in inet_fill_ifaddr, but let's not assume that will always be true... |
| 76 | if (ifa.ifa_addr == nullptr) { |
| 77 | // This is an IFA_ADDRESS and haven't seen an IFA_LOCAL yet, so assume this is the |
| 78 | // local address. SetLocalAddress will fix things if we later see an IFA_LOCAL. |
Elliott Hughes | 7dac8b8 | 2016-02-17 14:19:48 -0800 | [diff] [blame] | 79 | ifa.ifa_addr = CopyAddress(family, data, byteCount, &addr); |
Elliott Hughes | ef925e5 | 2016-03-01 17:27:12 -0800 | [diff] [blame] | 80 | } else { |
| 81 | // We already saw an IFA_LOCAL, which implies this is a destination address. |
| 82 | ifa.ifa_dstaddr = CopyAddress(family, data, byteCount, &ifa_ifu); |
| 83 | } |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void SetBroadcastAddress(int family, const void* data, size_t byteCount) { |
Elliott Hughes | ef925e5 | 2016-03-01 17:27:12 -0800 | [diff] [blame] | 87 | // ifa_broadaddr and ifa_dstaddr overlap in a union. Unfortunately, it's possible |
| 88 | // to have an interface with both. Keeping the last thing the kernel gives us seems |
| 89 | // to be glibc 2.19's behavior too, so our choice is being source compatible with |
| 90 | // badly-written code that assumes ifa_broadaddr and ifa_dstaddr are interchangeable |
| 91 | // or supporting interfaces with both addresses configured. My assumption is that |
| 92 | // bad code is more common than weird network interfaces... |
| 93 | ifa.ifa_broadaddr = CopyAddress(family, data, byteCount, &ifa_ifu); |
| 94 | } |
| 95 | |
| 96 | void SetLocalAddress(int family, const void* data, size_t byteCount) { |
| 97 | // The kernel source says "for point-to-point IFA_ADDRESS is DESTINATION address, |
| 98 | // local address is supplied in IFA_LOCAL attribute". |
| 99 | // -- http://lxr.free-electrons.com/source/include/uapi/linux/if_addr.h#L17 |
| 100 | |
| 101 | // So copy any existing IFA_ADDRESS into ifa_dstaddr... |
| 102 | if (ifa.ifa_addr != nullptr) { |
| 103 | ifa.ifa_dstaddr = reinterpret_cast<sockaddr*>(memcpy(&ifa_ifu, &addr, sizeof(addr))); |
| 104 | } |
| 105 | // ...and then put this IFA_LOCAL into ifa_addr. |
| 106 | ifa.ifa_addr = CopyAddress(family, data, byteCount, &addr); |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | // Netlink gives us the prefix length as a bit count. We need to turn |
| 110 | // that into a BSD-compatible netmask represented by a sockaddr*. |
| 111 | void SetNetmask(int family, size_t prefix_length) { |
Elliott Hughes | ef925e5 | 2016-03-01 17:27:12 -0800 | [diff] [blame] | 112 | // ...and work out the netmask from the prefix length. |
| 113 | netmask.ss_family = family; |
| 114 | uint8_t* dst = SockaddrBytes(family, &netmask); |
| 115 | memset(dst, 0xff, prefix_length / 8); |
| 116 | if ((prefix_length % 8) != 0) { |
| 117 | dst[prefix_length/8] = (0xff << (8 - (prefix_length % 8))); |
| 118 | } |
| 119 | ifa.ifa_netmask = reinterpret_cast<sockaddr*>(&netmask); |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Yi Kong | fdb2963 | 2015-12-22 17:07:23 +0000 | [diff] [blame] | 122 | void SetPacketAttributes(int ifindex, unsigned short hatype, unsigned char halen) { |
| 123 | sockaddr_ll* sll = reinterpret_cast<sockaddr_ll*>(&addr); |
| 124 | sll->sll_ifindex = ifindex; |
| 125 | sll->sll_hatype = hatype; |
| 126 | sll->sll_halen = halen; |
| 127 | } |
| 128 | |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 129 | private: |
Elliott Hughes | 7dac8b8 | 2016-02-17 14:19:48 -0800 | [diff] [blame] | 130 | sockaddr* CopyAddress(int family, const void* data, size_t byteCount, sockaddr_storage* ss) { |
Elliott Hughes | ef925e5 | 2016-03-01 17:27:12 -0800 | [diff] [blame] | 131 | // Netlink gives us the address family in the header, and the |
| 132 | // sockaddr_in or sockaddr_in6 bytes as the payload. We need to |
| 133 | // stitch the two bits together into the sockaddr that's part of |
| 134 | // our portable interface. |
| 135 | ss->ss_family = family; |
| 136 | memcpy(SockaddrBytes(family, ss), data, byteCount); |
Elliott Hughes | 7dac8b8 | 2016-02-17 14:19:48 -0800 | [diff] [blame] | 137 | |
Elliott Hughes | ef925e5 | 2016-03-01 17:27:12 -0800 | [diff] [blame] | 138 | // For IPv6 we might also have to set the scope id. |
| 139 | if (family == AF_INET6 && (IN6_IS_ADDR_LINKLOCAL(data) || IN6_IS_ADDR_MC_LINKLOCAL(data))) { |
| 140 | reinterpret_cast<sockaddr_in6*>(ss)->sin6_scope_id = interface_index; |
| 141 | } |
Elliott Hughes | 7dac8b8 | 2016-02-17 14:19:48 -0800 | [diff] [blame] | 142 | |
Elliott Hughes | ef925e5 | 2016-03-01 17:27:12 -0800 | [diff] [blame] | 143 | return reinterpret_cast<sockaddr*>(ss); |
Elliott Hughes | 7dac8b8 | 2016-02-17 14:19:48 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 146 | // Returns a pointer to the first byte in the address data (which is |
| 147 | // stored in network byte order). |
| 148 | uint8_t* SockaddrBytes(int family, sockaddr_storage* ss) { |
| 149 | if (family == AF_INET) { |
| 150 | sockaddr_in* ss4 = reinterpret_cast<sockaddr_in*>(ss); |
| 151 | return reinterpret_cast<uint8_t*>(&ss4->sin_addr); |
| 152 | } else if (family == AF_INET6) { |
| 153 | sockaddr_in6* ss6 = reinterpret_cast<sockaddr_in6*>(ss); |
| 154 | return reinterpret_cast<uint8_t*>(&ss6->sin6_addr); |
Yi Kong | fdb2963 | 2015-12-22 17:07:23 +0000 | [diff] [blame] | 155 | } else if (family == AF_PACKET) { |
| 156 | sockaddr_ll* sll = reinterpret_cast<sockaddr_ll*>(ss); |
| 157 | return reinterpret_cast<uint8_t*>(&sll->sll_addr); |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 158 | } |
| 159 | return nullptr; |
| 160 | } |
| 161 | }; |
| 162 | |
Elliott Hughes | ed57b98 | 2016-01-15 21:02:56 -0800 | [diff] [blame] | 163 | static void __getifaddrs_callback(void* context, nlmsghdr* hdr) { |
| 164 | ifaddrs** out = reinterpret_cast<ifaddrs**>(context); |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 165 | |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 166 | if (hdr->nlmsg_type == RTM_NEWLINK) { |
| 167 | ifinfomsg* ifi = reinterpret_cast<ifinfomsg*>(NLMSG_DATA(hdr)); |
| 168 | |
| 169 | // Create a new ifaddr entry, and set the interface index and flags. |
| 170 | ifaddrs_storage* new_addr = new ifaddrs_storage(out); |
| 171 | new_addr->interface_index = ifi->ifi_index; |
| 172 | new_addr->ifa.ifa_flags = ifi->ifi_flags; |
| 173 | |
| 174 | // Go through the various bits of information and find the name. |
| 175 | rtattr* rta = IFLA_RTA(ifi); |
| 176 | size_t rta_len = IFLA_PAYLOAD(hdr); |
| 177 | while (RTA_OK(rta, rta_len)) { |
Yi Kong | fdb2963 | 2015-12-22 17:07:23 +0000 | [diff] [blame] | 178 | if (rta->rta_type == IFLA_ADDRESS) { |
| 179 | if (RTA_PAYLOAD(rta) < sizeof(new_addr->addr)) { |
| 180 | new_addr->SetAddress(AF_PACKET, RTA_DATA(rta), RTA_PAYLOAD(rta)); |
| 181 | new_addr->SetPacketAttributes(ifi->ifi_index, ifi->ifi_type, RTA_PAYLOAD(rta)); |
| 182 | } |
| 183 | } else if (rta->rta_type == IFLA_BROADCAST) { |
| 184 | if (RTA_PAYLOAD(rta) < sizeof(new_addr->ifa_ifu)) { |
| 185 | new_addr->SetBroadcastAddress(AF_PACKET, RTA_DATA(rta), RTA_PAYLOAD(rta)); |
| 186 | new_addr->SetPacketAttributes(ifi->ifi_index, ifi->ifi_type, RTA_PAYLOAD(rta)); |
| 187 | } |
| 188 | } else if (rta->rta_type == IFLA_IFNAME) { |
| 189 | if (RTA_PAYLOAD(rta) < sizeof(new_addr->name)) { |
| 190 | memcpy(new_addr->name, RTA_DATA(rta), RTA_PAYLOAD(rta)); |
| 191 | new_addr->ifa.ifa_name = new_addr->name; |
| 192 | } |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 193 | } |
| 194 | rta = RTA_NEXT(rta, rta_len); |
| 195 | } |
| 196 | } else if (hdr->nlmsg_type == RTM_NEWADDR) { |
| 197 | ifaddrmsg* msg = reinterpret_cast<ifaddrmsg*>(NLMSG_DATA(hdr)); |
| 198 | |
Bram Bonné | d54ad07 | 2020-01-15 14:17:07 +0100 | [diff] [blame] | 199 | // We might already know about this interface from an RTM_NEWLINK message. |
| 200 | const ifaddrs_storage* known_addr = reinterpret_cast<const ifaddrs_storage*>(*out); |
| 201 | while (known_addr != nullptr && known_addr->interface_index != static_cast<int>(msg->ifa_index)) { |
| 202 | known_addr = reinterpret_cast<const ifaddrs_storage*>(known_addr->ifa.ifa_next); |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 203 | } |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 204 | |
Bram Bonné | d54ad07 | 2020-01-15 14:17:07 +0100 | [diff] [blame] | 205 | // Create a new ifaddr entry, and set the interface index. |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 206 | ifaddrs_storage* new_addr = new ifaddrs_storage(out); |
Bram Bonné | d54ad07 | 2020-01-15 14:17:07 +0100 | [diff] [blame] | 207 | new_addr->interface_index = static_cast<int>(msg->ifa_index); |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 208 | |
Bram Bonné | d54ad07 | 2020-01-15 14:17:07 +0100 | [diff] [blame] | 209 | // If this is a known interface, copy what we already know. |
Bram Bonné | 8e20ac4 | 2020-07-20 11:18:03 +0200 | [diff] [blame] | 210 | // If we don't know about this interface yet, we try to resolve the name and flags using ioctl |
| 211 | // calls during postprocessing. |
Bram Bonné | d54ad07 | 2020-01-15 14:17:07 +0100 | [diff] [blame] | 212 | if (known_addr != nullptr) { |
| 213 | strcpy(new_addr->name, known_addr->name); |
| 214 | new_addr->ifa.ifa_name = new_addr->name; |
| 215 | new_addr->ifa.ifa_flags = known_addr->ifa.ifa_flags; |
Bram Bonné | d54ad07 | 2020-01-15 14:17:07 +0100 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | // Go through the various bits of information and find the name, address |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 219 | // and any broadcast/destination address. |
| 220 | rtattr* rta = IFA_RTA(msg); |
| 221 | size_t rta_len = IFA_PAYLOAD(hdr); |
| 222 | while (RTA_OK(rta, rta_len)) { |
| 223 | if (rta->rta_type == IFA_ADDRESS) { |
| 224 | if (msg->ifa_family == AF_INET || msg->ifa_family == AF_INET6) { |
Yi Kong | fdb2963 | 2015-12-22 17:07:23 +0000 | [diff] [blame] | 225 | new_addr->SetAddress(msg->ifa_family, RTA_DATA(rta), RTA_PAYLOAD(rta)); |
| 226 | new_addr->SetNetmask(msg->ifa_family, msg->ifa_prefixlen); |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 227 | } |
| 228 | } else if (rta->rta_type == IFA_BROADCAST) { |
Elliott Hughes | ef925e5 | 2016-03-01 17:27:12 -0800 | [diff] [blame] | 229 | if (msg->ifa_family == AF_INET) { |
Yi Kong | fdb2963 | 2015-12-22 17:07:23 +0000 | [diff] [blame] | 230 | new_addr->SetBroadcastAddress(msg->ifa_family, RTA_DATA(rta), RTA_PAYLOAD(rta)); |
Bram Bonné | d54ad07 | 2020-01-15 14:17:07 +0100 | [diff] [blame] | 231 | if (known_addr == nullptr) { |
| 232 | // We did not read the broadcast flag from an RTM_NEWLINK message. |
| 233 | // Ensure that it is set. |
| 234 | new_addr->ifa.ifa_flags |= IFF_BROADCAST; |
| 235 | } |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 236 | } |
Elliott Hughes | ef925e5 | 2016-03-01 17:27:12 -0800 | [diff] [blame] | 237 | } else if (rta->rta_type == IFA_LOCAL) { |
| 238 | if (msg->ifa_family == AF_INET || msg->ifa_family == AF_INET6) { |
| 239 | new_addr->SetLocalAddress(msg->ifa_family, RTA_DATA(rta), RTA_PAYLOAD(rta)); |
| 240 | } |
Bram Bonné | d54ad07 | 2020-01-15 14:17:07 +0100 | [diff] [blame] | 241 | } else if (rta->rta_type == IFA_LABEL) { |
| 242 | if (RTA_PAYLOAD(rta) < sizeof(new_addr->name)) { |
| 243 | memcpy(new_addr->name, RTA_DATA(rta), RTA_PAYLOAD(rta)); |
| 244 | new_addr->ifa.ifa_name = new_addr->name; |
| 245 | } |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 246 | } |
| 247 | rta = RTA_NEXT(rta, rta_len); |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
Bram Bonné | 14e5c3c | 2020-02-21 11:47:20 +0100 | [diff] [blame] | 252 | static void resolve_or_remove_nameless_interfaces(ifaddrs** list) { |
Bram Bonné | d54ad07 | 2020-01-15 14:17:07 +0100 | [diff] [blame] | 253 | ifaddrs_storage* addr = reinterpret_cast<ifaddrs_storage*>(*list); |
| 254 | ifaddrs_storage* prev_addr = nullptr; |
| 255 | while (addr != nullptr) { |
| 256 | ifaddrs* next_addr = addr->ifa.ifa_next; |
Bram Bonné | 14e5c3c | 2020-02-21 11:47:20 +0100 | [diff] [blame] | 257 | |
| 258 | // Try resolving interfaces without a name first. |
| 259 | if (strlen(addr->name) == 0) { |
| 260 | if (if_indextoname(addr->interface_index, addr->name) != nullptr) { |
| 261 | addr->ifa.ifa_name = addr->name; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | // If the interface could not be resolved, remove it. |
Bram Bonné | d54ad07 | 2020-01-15 14:17:07 +0100 | [diff] [blame] | 266 | if (strlen(addr->name) == 0) { |
| 267 | if (prev_addr == nullptr) { |
| 268 | *list = next_addr; |
| 269 | } else { |
| 270 | prev_addr->ifa.ifa_next = next_addr; |
| 271 | } |
| 272 | free(addr); |
| 273 | } else { |
| 274 | prev_addr = addr; |
| 275 | } |
Bram Bonné | 8e20ac4 | 2020-07-20 11:18:03 +0200 | [diff] [blame] | 276 | |
Bram Bonné | d54ad07 | 2020-01-15 14:17:07 +0100 | [diff] [blame] | 277 | addr = reinterpret_cast<ifaddrs_storage*>(next_addr); |
| 278 | } |
| 279 | } |
| 280 | |
Bram Bonné | 8e20ac4 | 2020-07-20 11:18:03 +0200 | [diff] [blame] | 281 | static void get_interface_flags_via_ioctl(ifaddrs** list) { |
| 282 | ScopedFd s(socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0)); |
| 283 | if (s.get() == -1) { |
| 284 | async_safe_format_log(ANDROID_LOG_ERROR, "libc", |
| 285 | "socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC) failed in ifaddrs: %s", |
| 286 | strerror(errno)); |
| 287 | return; |
| 288 | } |
| 289 | |
| 290 | for (ifaddrs_storage* addr = reinterpret_cast<ifaddrs_storage*>(*list); addr != nullptr; |
| 291 | addr = reinterpret_cast<ifaddrs_storage*>(addr->ifa.ifa_next)) { |
| 292 | ifreq ifr = {}; |
| 293 | strlcpy(ifr.ifr_name, addr->ifa.ifa_name, sizeof(ifr.ifr_name)); |
| 294 | if (ioctl(s.get(), SIOCGIFFLAGS, &ifr) != -1) { |
| 295 | addr->ifa.ifa_flags = ifr.ifr_flags; |
| 296 | } else { |
| 297 | async_safe_format_log(ANDROID_LOG_ERROR, "libc", |
| 298 | "ioctl(SIOCGIFFLAGS) for \"%s\" failed in ifaddrs: %s", |
| 299 | addr->ifa.ifa_name, strerror(errno)); |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 304 | int getifaddrs(ifaddrs** out) { |
Elliott Hughes | ed57b98 | 2016-01-15 21:02:56 -0800 | [diff] [blame] | 305 | // We construct the result directly into `out`, so terminate the list. |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 306 | *out = nullptr; |
| 307 | |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 308 | // Open the netlink socket and ask for all the links and addresses. |
Elliott Hughes | ed57b98 | 2016-01-15 21:02:56 -0800 | [diff] [blame] | 309 | NetlinkConnection nc; |
Bram Bonné | d994cd7 | 2020-12-01 10:00:55 +0000 | [diff] [blame] | 310 | // SELinux policy only allows RTM_GETLINK messages to be sent by: |
| 311 | // - System apps |
| 312 | // - Apps with a target SDK version lower than R |
Bram Bonné | d3df35e | 2020-01-23 17:05:42 +0100 | [diff] [blame] | 313 | bool getlink_success = false; |
Bram Bonné | 95ca52a | 2020-12-03 19:03:55 +0100 | [diff] [blame^] | 314 | if (!should_apply_soft_mac_getlink_restrictions()) { |
Bram Bonné | d3df35e | 2020-01-23 17:05:42 +0100 | [diff] [blame] | 315 | getlink_success = nc.SendRequest(RTM_GETLINK) && nc.ReadResponses(__getifaddrs_callback, out); |
Bram Bonné | 95ca52a | 2020-12-03 19:03:55 +0100 | [diff] [blame^] | 316 | } else if (android_get_application_target_sdk_version() < __ANDROID_API_R__) { |
| 317 | async_safe_format_log(ANDROID_LOG_WARN, "mac-restrictions", |
| 318 | "ifaddr no longer returns link info. Please follow instructions at " |
| 319 | "go/netlink-bug if this app behaves incorrectly."); |
Bram Bonné | d3df35e | 2020-01-23 17:05:42 +0100 | [diff] [blame] | 320 | } |
Bram Bonné | d54ad07 | 2020-01-15 14:17:07 +0100 | [diff] [blame] | 321 | bool getaddr_success = |
| 322 | nc.SendRequest(RTM_GETADDR) && nc.ReadResponses(__getifaddrs_callback, out); |
| 323 | |
| 324 | if (!getaddr_success) { |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 325 | freeifaddrs(*out); |
| 326 | // Ensure that callers crash if they forget to check for success. |
| 327 | *out = nullptr; |
Elliott Hughes | ed57b98 | 2016-01-15 21:02:56 -0800 | [diff] [blame] | 328 | return -1; |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 329 | } |
Elliott Hughes | ed57b98 | 2016-01-15 21:02:56 -0800 | [diff] [blame] | 330 | |
Bram Bonné | d54ad07 | 2020-01-15 14:17:07 +0100 | [diff] [blame] | 331 | if (!getlink_success) { |
Bram Bonné | d54ad07 | 2020-01-15 14:17:07 +0100 | [diff] [blame] | 332 | // If we weren't able to depend on GETLINK messages, it's possible some |
Bram Bonné | 14e5c3c | 2020-02-21 11:47:20 +0100 | [diff] [blame] | 333 | // interfaces never got their name set. Resolve them using if_indextoname or remove them. |
| 334 | resolve_or_remove_nameless_interfaces(out); |
Bram Bonné | 8e20ac4 | 2020-07-20 11:18:03 +0200 | [diff] [blame] | 335 | // Similarly, without GETLINK messages, interfaces will not have their flags set. |
| 336 | // Resolve them using the SIOCGIFFLAGS ioctl call. |
| 337 | get_interface_flags_via_ioctl(out); |
Bram Bonné | d54ad07 | 2020-01-15 14:17:07 +0100 | [diff] [blame] | 338 | } |
| 339 | |
Elliott Hughes | ed57b98 | 2016-01-15 21:02:56 -0800 | [diff] [blame] | 340 | return 0; |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | void freeifaddrs(ifaddrs* list) { |
| 344 | while (list != nullptr) { |
| 345 | ifaddrs* current = list; |
| 346 | list = list->ifa_next; |
| 347 | free(current); |
| 348 | } |
| 349 | } |