blob: 22ecdb4363582262bdecdaf5fd2c3218a000af14 [file] [log] [blame]
Elliott Hughes9cddb482016-01-04 20:38:05 +00001/*
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é8e20ac42020-07-20 11:18:03 +020031#include <async_safe/log.h>
Bram Bonnéd3df35e2020-01-23 17:05:42 +010032#include <cutils/misc.h> // FIRST_APPLICATION_UID
Elliott Hughes9cddb482016-01-04 20:38:05 +000033#include <errno.h>
Yi Kongfdb29632015-12-22 17:07:23 +000034#include <linux/if_packet.h>
Elliott Hughes9cddb482016-01-04 20:38:05 +000035#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 Hughesed57b982016-01-15 21:02:56 -080043#include "private/ErrnoRestorer.h"
44
Bram Bonné95ca52a2020-12-03 19:03:55 +010045#include "bionic_appcompat.h"
Elliott Hughesed57b982016-01-15 21:02:56 -080046#include "bionic_netlink.h"
47
Elliott Hughes9cddb482016-01-04 20:38:05 +000048// 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.
51struct 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 Hsieh62e3a072016-05-03 12:08:05 -070065 explicit ifaddrs_storage(ifaddrs** list) {
Elliott Hughes9cddb482016-01-04 20:38:05 +000066 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 Hughes9cddb482016-01-04 20:38:05 +000073 void SetAddress(int family, const void* data, size_t byteCount) {
Elliott Hughesef925e52016-03-01 17:27:12 -080074 // 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 Hughes7dac8b82016-02-17 14:19:48 -080079 ifa.ifa_addr = CopyAddress(family, data, byteCount, &addr);
Elliott Hughesef925e52016-03-01 17:27:12 -080080 } 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 Hughes9cddb482016-01-04 20:38:05 +000084 }
85
86 void SetBroadcastAddress(int family, const void* data, size_t byteCount) {
Elliott Hughesef925e52016-03-01 17:27:12 -080087 // 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 Hughes9cddb482016-01-04 20:38:05 +0000107 }
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 Hughesef925e52016-03-01 17:27:12 -0800112 // ...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 Hughes9cddb482016-01-04 20:38:05 +0000120 }
121
Yi Kongfdb29632015-12-22 17:07:23 +0000122 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 Hughes9cddb482016-01-04 20:38:05 +0000129 private:
Elliott Hughes7dac8b82016-02-17 14:19:48 -0800130 sockaddr* CopyAddress(int family, const void* data, size_t byteCount, sockaddr_storage* ss) {
Elliott Hughesef925e52016-03-01 17:27:12 -0800131 // 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 Hughes7dac8b82016-02-17 14:19:48 -0800137
Elliott Hughesef925e52016-03-01 17:27:12 -0800138 // 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 Hughes7dac8b82016-02-17 14:19:48 -0800142
Elliott Hughesef925e52016-03-01 17:27:12 -0800143 return reinterpret_cast<sockaddr*>(ss);
Elliott Hughes7dac8b82016-02-17 14:19:48 -0800144 }
145
Elliott Hughes9cddb482016-01-04 20:38:05 +0000146 // 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 Kongfdb29632015-12-22 17:07:23 +0000155 } else if (family == AF_PACKET) {
156 sockaddr_ll* sll = reinterpret_cast<sockaddr_ll*>(ss);
157 return reinterpret_cast<uint8_t*>(&sll->sll_addr);
Elliott Hughes9cddb482016-01-04 20:38:05 +0000158 }
159 return nullptr;
160 }
161};
162
Elliott Hughesed57b982016-01-15 21:02:56 -0800163static void __getifaddrs_callback(void* context, nlmsghdr* hdr) {
164 ifaddrs** out = reinterpret_cast<ifaddrs**>(context);
Elliott Hughes9cddb482016-01-04 20:38:05 +0000165
Elliott Hughes9cddb482016-01-04 20:38:05 +0000166 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 Kongfdb29632015-12-22 17:07:23 +0000178 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 Hughes9cddb482016-01-04 20:38:05 +0000193 }
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éd54ad072020-01-15 14:17:07 +0100199 // 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 Hughes9cddb482016-01-04 20:38:05 +0000203 }
Elliott Hughes9cddb482016-01-04 20:38:05 +0000204
Bram Bonnéd54ad072020-01-15 14:17:07 +0100205 // Create a new ifaddr entry, and set the interface index.
Elliott Hughes9cddb482016-01-04 20:38:05 +0000206 ifaddrs_storage* new_addr = new ifaddrs_storage(out);
Bram Bonnéd54ad072020-01-15 14:17:07 +0100207 new_addr->interface_index = static_cast<int>(msg->ifa_index);
Elliott Hughes9cddb482016-01-04 20:38:05 +0000208
Bram Bonnéd54ad072020-01-15 14:17:07 +0100209 // If this is a known interface, copy what we already know.
Bram Bonné8e20ac42020-07-20 11:18:03 +0200210 // 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éd54ad072020-01-15 14:17:07 +0100212 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éd54ad072020-01-15 14:17:07 +0100216 }
217
218 // Go through the various bits of information and find the name, address
Elliott Hughes9cddb482016-01-04 20:38:05 +0000219 // 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 Kongfdb29632015-12-22 17:07:23 +0000225 new_addr->SetAddress(msg->ifa_family, RTA_DATA(rta), RTA_PAYLOAD(rta));
226 new_addr->SetNetmask(msg->ifa_family, msg->ifa_prefixlen);
Elliott Hughes9cddb482016-01-04 20:38:05 +0000227 }
228 } else if (rta->rta_type == IFA_BROADCAST) {
Elliott Hughesef925e52016-03-01 17:27:12 -0800229 if (msg->ifa_family == AF_INET) {
Yi Kongfdb29632015-12-22 17:07:23 +0000230 new_addr->SetBroadcastAddress(msg->ifa_family, RTA_DATA(rta), RTA_PAYLOAD(rta));
Bram Bonnéd54ad072020-01-15 14:17:07 +0100231 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 Hughes9cddb482016-01-04 20:38:05 +0000236 }
Elliott Hughesef925e52016-03-01 17:27:12 -0800237 } 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éd54ad072020-01-15 14:17:07 +0100241 } 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 Hughes9cddb482016-01-04 20:38:05 +0000246 }
247 rta = RTA_NEXT(rta, rta_len);
248 }
249 }
250}
251
Bram Bonné14e5c3c2020-02-21 11:47:20 +0100252static void resolve_or_remove_nameless_interfaces(ifaddrs** list) {
Bram Bonnéd54ad072020-01-15 14:17:07 +0100253 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é14e5c3c2020-02-21 11:47:20 +0100257
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éd54ad072020-01-15 14:17:07 +0100266 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é8e20ac42020-07-20 11:18:03 +0200276
Bram Bonnéd54ad072020-01-15 14:17:07 +0100277 addr = reinterpret_cast<ifaddrs_storage*>(next_addr);
278 }
279}
280
Bram Bonné8e20ac42020-07-20 11:18:03 +0200281static 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 Hughes9cddb482016-01-04 20:38:05 +0000304int getifaddrs(ifaddrs** out) {
Elliott Hughesed57b982016-01-15 21:02:56 -0800305 // We construct the result directly into `out`, so terminate the list.
Elliott Hughes9cddb482016-01-04 20:38:05 +0000306 *out = nullptr;
307
Elliott Hughes9cddb482016-01-04 20:38:05 +0000308 // Open the netlink socket and ask for all the links and addresses.
Elliott Hughesed57b982016-01-15 21:02:56 -0800309 NetlinkConnection nc;
Bram Bonnéd994cd72020-12-01 10:00:55 +0000310 // 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éd3df35e2020-01-23 17:05:42 +0100313 bool getlink_success = false;
Bram Bonné95ca52a2020-12-03 19:03:55 +0100314 if (!should_apply_soft_mac_getlink_restrictions()) {
Bram Bonnéd3df35e2020-01-23 17:05:42 +0100315 getlink_success = nc.SendRequest(RTM_GETLINK) && nc.ReadResponses(__getifaddrs_callback, out);
Bram Bonné95ca52a2020-12-03 19:03:55 +0100316 } 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éd3df35e2020-01-23 17:05:42 +0100320 }
Bram Bonnéd54ad072020-01-15 14:17:07 +0100321 bool getaddr_success =
322 nc.SendRequest(RTM_GETADDR) && nc.ReadResponses(__getifaddrs_callback, out);
323
324 if (!getaddr_success) {
Elliott Hughes9cddb482016-01-04 20:38:05 +0000325 freeifaddrs(*out);
326 // Ensure that callers crash if they forget to check for success.
327 *out = nullptr;
Elliott Hughesed57b982016-01-15 21:02:56 -0800328 return -1;
Elliott Hughes9cddb482016-01-04 20:38:05 +0000329 }
Elliott Hughesed57b982016-01-15 21:02:56 -0800330
Bram Bonnéd54ad072020-01-15 14:17:07 +0100331 if (!getlink_success) {
Bram Bonnéd54ad072020-01-15 14:17:07 +0100332 // If we weren't able to depend on GETLINK messages, it's possible some
Bram Bonné14e5c3c2020-02-21 11:47:20 +0100333 // interfaces never got their name set. Resolve them using if_indextoname or remove them.
334 resolve_or_remove_nameless_interfaces(out);
Bram Bonné8e20ac42020-07-20 11:18:03 +0200335 // 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éd54ad072020-01-15 14:17:07 +0100338 }
339
Elliott Hughesed57b982016-01-15 21:02:56 -0800340 return 0;
Elliott Hughes9cddb482016-01-04 20:38:05 +0000341}
342
343void freeifaddrs(ifaddrs* list) {
344 while (list != nullptr) {
345 ifaddrs* current = list;
346 list = list->ifa_next;
347 free(current);
348 }
349}