blob: 954d43ba9e5200a18053d1c011ec9852172494aa [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éd54ad072020-01-15 14:17:07 +010031#include <async_safe/log.h>
Elliott Hughes9cddb482016-01-04 20:38:05 +000032#include <errno.h>
Yi Kongfdb29632015-12-22 17:07:23 +000033#include <linux/if_packet.h>
Elliott Hughes9cddb482016-01-04 20:38:05 +000034#include <net/if.h>
35#include <netinet/in.h>
36#include <stdint.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40#include <unistd.h>
41
Elliott Hughesed57b982016-01-15 21:02:56 -080042#include "private/ErrnoRestorer.h"
43
44#include "bionic_netlink.h"
45
Elliott Hughes9cddb482016-01-04 20:38:05 +000046// The public ifaddrs struct is full of pointers. Rather than track several
47// different allocations, we use a maximally-sized structure with the public
48// part at offset 0, and pointers into its hidden tail.
49struct ifaddrs_storage {
50 // Must come first, so that `ifaddrs_storage` is-a `ifaddrs`.
51 ifaddrs ifa;
52
53 // The interface index, so we can match RTM_NEWADDR messages with
54 // earlier RTM_NEWLINK messages (to copy the interface flags).
55 int interface_index;
56
57 // Storage for the pointers in `ifa`.
58 sockaddr_storage addr;
59 sockaddr_storage netmask;
60 sockaddr_storage ifa_ifu;
61 char name[IFNAMSIZ + 1];
62
Chih-Hung Hsieh62e3a072016-05-03 12:08:05 -070063 explicit ifaddrs_storage(ifaddrs** list) {
Elliott Hughes9cddb482016-01-04 20:38:05 +000064 memset(this, 0, sizeof(*this));
65
66 // push_front onto `list`.
67 ifa.ifa_next = *list;
68 *list = reinterpret_cast<ifaddrs*>(this);
69 }
70
Elliott Hughes9cddb482016-01-04 20:38:05 +000071 void SetAddress(int family, const void* data, size_t byteCount) {
Elliott Hughesef925e52016-03-01 17:27:12 -080072 // The kernel currently uses the order IFA_ADDRESS, IFA_LOCAL, IFA_BROADCAST
73 // in inet_fill_ifaddr, but let's not assume that will always be true...
74 if (ifa.ifa_addr == nullptr) {
75 // This is an IFA_ADDRESS and haven't seen an IFA_LOCAL yet, so assume this is the
76 // local address. SetLocalAddress will fix things if we later see an IFA_LOCAL.
Elliott Hughes7dac8b82016-02-17 14:19:48 -080077 ifa.ifa_addr = CopyAddress(family, data, byteCount, &addr);
Elliott Hughesef925e52016-03-01 17:27:12 -080078 } else {
79 // We already saw an IFA_LOCAL, which implies this is a destination address.
80 ifa.ifa_dstaddr = CopyAddress(family, data, byteCount, &ifa_ifu);
81 }
Elliott Hughes9cddb482016-01-04 20:38:05 +000082 }
83
84 void SetBroadcastAddress(int family, const void* data, size_t byteCount) {
Elliott Hughesef925e52016-03-01 17:27:12 -080085 // ifa_broadaddr and ifa_dstaddr overlap in a union. Unfortunately, it's possible
86 // to have an interface with both. Keeping the last thing the kernel gives us seems
87 // to be glibc 2.19's behavior too, so our choice is being source compatible with
88 // badly-written code that assumes ifa_broadaddr and ifa_dstaddr are interchangeable
89 // or supporting interfaces with both addresses configured. My assumption is that
90 // bad code is more common than weird network interfaces...
91 ifa.ifa_broadaddr = CopyAddress(family, data, byteCount, &ifa_ifu);
92 }
93
94 void SetLocalAddress(int family, const void* data, size_t byteCount) {
95 // The kernel source says "for point-to-point IFA_ADDRESS is DESTINATION address,
96 // local address is supplied in IFA_LOCAL attribute".
97 // -- http://lxr.free-electrons.com/source/include/uapi/linux/if_addr.h#L17
98
99 // So copy any existing IFA_ADDRESS into ifa_dstaddr...
100 if (ifa.ifa_addr != nullptr) {
101 ifa.ifa_dstaddr = reinterpret_cast<sockaddr*>(memcpy(&ifa_ifu, &addr, sizeof(addr)));
102 }
103 // ...and then put this IFA_LOCAL into ifa_addr.
104 ifa.ifa_addr = CopyAddress(family, data, byteCount, &addr);
Elliott Hughes9cddb482016-01-04 20:38:05 +0000105 }
106
107 // Netlink gives us the prefix length as a bit count. We need to turn
108 // that into a BSD-compatible netmask represented by a sockaddr*.
109 void SetNetmask(int family, size_t prefix_length) {
Elliott Hughesef925e52016-03-01 17:27:12 -0800110 // ...and work out the netmask from the prefix length.
111 netmask.ss_family = family;
112 uint8_t* dst = SockaddrBytes(family, &netmask);
113 memset(dst, 0xff, prefix_length / 8);
114 if ((prefix_length % 8) != 0) {
115 dst[prefix_length/8] = (0xff << (8 - (prefix_length % 8)));
116 }
117 ifa.ifa_netmask = reinterpret_cast<sockaddr*>(&netmask);
Elliott Hughes9cddb482016-01-04 20:38:05 +0000118 }
119
Yi Kongfdb29632015-12-22 17:07:23 +0000120 void SetPacketAttributes(int ifindex, unsigned short hatype, unsigned char halen) {
121 sockaddr_ll* sll = reinterpret_cast<sockaddr_ll*>(&addr);
122 sll->sll_ifindex = ifindex;
123 sll->sll_hatype = hatype;
124 sll->sll_halen = halen;
125 }
126
Elliott Hughes9cddb482016-01-04 20:38:05 +0000127 private:
Elliott Hughes7dac8b82016-02-17 14:19:48 -0800128 sockaddr* CopyAddress(int family, const void* data, size_t byteCount, sockaddr_storage* ss) {
Elliott Hughesef925e52016-03-01 17:27:12 -0800129 // Netlink gives us the address family in the header, and the
130 // sockaddr_in or sockaddr_in6 bytes as the payload. We need to
131 // stitch the two bits together into the sockaddr that's part of
132 // our portable interface.
133 ss->ss_family = family;
134 memcpy(SockaddrBytes(family, ss), data, byteCount);
Elliott Hughes7dac8b82016-02-17 14:19:48 -0800135
Elliott Hughesef925e52016-03-01 17:27:12 -0800136 // For IPv6 we might also have to set the scope id.
137 if (family == AF_INET6 && (IN6_IS_ADDR_LINKLOCAL(data) || IN6_IS_ADDR_MC_LINKLOCAL(data))) {
138 reinterpret_cast<sockaddr_in6*>(ss)->sin6_scope_id = interface_index;
139 }
Elliott Hughes7dac8b82016-02-17 14:19:48 -0800140
Elliott Hughesef925e52016-03-01 17:27:12 -0800141 return reinterpret_cast<sockaddr*>(ss);
Elliott Hughes7dac8b82016-02-17 14:19:48 -0800142 }
143
Elliott Hughes9cddb482016-01-04 20:38:05 +0000144 // Returns a pointer to the first byte in the address data (which is
145 // stored in network byte order).
146 uint8_t* SockaddrBytes(int family, sockaddr_storage* ss) {
147 if (family == AF_INET) {
148 sockaddr_in* ss4 = reinterpret_cast<sockaddr_in*>(ss);
149 return reinterpret_cast<uint8_t*>(&ss4->sin_addr);
150 } else if (family == AF_INET6) {
151 sockaddr_in6* ss6 = reinterpret_cast<sockaddr_in6*>(ss);
152 return reinterpret_cast<uint8_t*>(&ss6->sin6_addr);
Yi Kongfdb29632015-12-22 17:07:23 +0000153 } else if (family == AF_PACKET) {
154 sockaddr_ll* sll = reinterpret_cast<sockaddr_ll*>(ss);
155 return reinterpret_cast<uint8_t*>(&sll->sll_addr);
Elliott Hughes9cddb482016-01-04 20:38:05 +0000156 }
157 return nullptr;
158 }
159};
160
Elliott Hughesed57b982016-01-15 21:02:56 -0800161static void __getifaddrs_callback(void* context, nlmsghdr* hdr) {
162 ifaddrs** out = reinterpret_cast<ifaddrs**>(context);
Elliott Hughes9cddb482016-01-04 20:38:05 +0000163
Elliott Hughes9cddb482016-01-04 20:38:05 +0000164 if (hdr->nlmsg_type == RTM_NEWLINK) {
165 ifinfomsg* ifi = reinterpret_cast<ifinfomsg*>(NLMSG_DATA(hdr));
166
167 // Create a new ifaddr entry, and set the interface index and flags.
168 ifaddrs_storage* new_addr = new ifaddrs_storage(out);
169 new_addr->interface_index = ifi->ifi_index;
170 new_addr->ifa.ifa_flags = ifi->ifi_flags;
171
172 // Go through the various bits of information and find the name.
173 rtattr* rta = IFLA_RTA(ifi);
174 size_t rta_len = IFLA_PAYLOAD(hdr);
175 while (RTA_OK(rta, rta_len)) {
Yi Kongfdb29632015-12-22 17:07:23 +0000176 if (rta->rta_type == IFLA_ADDRESS) {
177 if (RTA_PAYLOAD(rta) < sizeof(new_addr->addr)) {
178 new_addr->SetAddress(AF_PACKET, RTA_DATA(rta), RTA_PAYLOAD(rta));
179 new_addr->SetPacketAttributes(ifi->ifi_index, ifi->ifi_type, RTA_PAYLOAD(rta));
180 }
181 } else if (rta->rta_type == IFLA_BROADCAST) {
182 if (RTA_PAYLOAD(rta) < sizeof(new_addr->ifa_ifu)) {
183 new_addr->SetBroadcastAddress(AF_PACKET, RTA_DATA(rta), RTA_PAYLOAD(rta));
184 new_addr->SetPacketAttributes(ifi->ifi_index, ifi->ifi_type, RTA_PAYLOAD(rta));
185 }
186 } else if (rta->rta_type == IFLA_IFNAME) {
187 if (RTA_PAYLOAD(rta) < sizeof(new_addr->name)) {
188 memcpy(new_addr->name, RTA_DATA(rta), RTA_PAYLOAD(rta));
189 new_addr->ifa.ifa_name = new_addr->name;
190 }
Elliott Hughes9cddb482016-01-04 20:38:05 +0000191 }
192 rta = RTA_NEXT(rta, rta_len);
193 }
194 } else if (hdr->nlmsg_type == RTM_NEWADDR) {
195 ifaddrmsg* msg = reinterpret_cast<ifaddrmsg*>(NLMSG_DATA(hdr));
196
Bram Bonnéd54ad072020-01-15 14:17:07 +0100197 // We might already know about this interface from an RTM_NEWLINK message.
198 const ifaddrs_storage* known_addr = reinterpret_cast<const ifaddrs_storage*>(*out);
199 while (known_addr != nullptr && known_addr->interface_index != static_cast<int>(msg->ifa_index)) {
200 known_addr = reinterpret_cast<const ifaddrs_storage*>(known_addr->ifa.ifa_next);
Elliott Hughes9cddb482016-01-04 20:38:05 +0000201 }
Elliott Hughes9cddb482016-01-04 20:38:05 +0000202
Bram Bonnéd54ad072020-01-15 14:17:07 +0100203 // Create a new ifaddr entry, and set the interface index.
Elliott Hughes9cddb482016-01-04 20:38:05 +0000204 ifaddrs_storage* new_addr = new ifaddrs_storage(out);
Bram Bonnéd54ad072020-01-15 14:17:07 +0100205 new_addr->interface_index = static_cast<int>(msg->ifa_index);
Elliott Hughes9cddb482016-01-04 20:38:05 +0000206
Bram Bonnéd54ad072020-01-15 14:17:07 +0100207 // If this is a known interface, copy what we already know.
208 if (known_addr != nullptr) {
209 strcpy(new_addr->name, known_addr->name);
210 new_addr->ifa.ifa_name = new_addr->name;
211 new_addr->ifa.ifa_flags = known_addr->ifa.ifa_flags;
212 } else {
213 new_addr->ifa.ifa_flags = msg->ifa_flags;
214 }
215
216 // Go through the various bits of information and find the name, address
Elliott Hughes9cddb482016-01-04 20:38:05 +0000217 // and any broadcast/destination address.
218 rtattr* rta = IFA_RTA(msg);
219 size_t rta_len = IFA_PAYLOAD(hdr);
220 while (RTA_OK(rta, rta_len)) {
221 if (rta->rta_type == IFA_ADDRESS) {
222 if (msg->ifa_family == AF_INET || msg->ifa_family == AF_INET6) {
Yi Kongfdb29632015-12-22 17:07:23 +0000223 new_addr->SetAddress(msg->ifa_family, RTA_DATA(rta), RTA_PAYLOAD(rta));
224 new_addr->SetNetmask(msg->ifa_family, msg->ifa_prefixlen);
Elliott Hughes9cddb482016-01-04 20:38:05 +0000225 }
226 } else if (rta->rta_type == IFA_BROADCAST) {
Elliott Hughesef925e52016-03-01 17:27:12 -0800227 if (msg->ifa_family == AF_INET) {
Yi Kongfdb29632015-12-22 17:07:23 +0000228 new_addr->SetBroadcastAddress(msg->ifa_family, RTA_DATA(rta), RTA_PAYLOAD(rta));
Bram Bonnéd54ad072020-01-15 14:17:07 +0100229 if (known_addr == nullptr) {
230 // We did not read the broadcast flag from an RTM_NEWLINK message.
231 // Ensure that it is set.
232 new_addr->ifa.ifa_flags |= IFF_BROADCAST;
233 }
Elliott Hughes9cddb482016-01-04 20:38:05 +0000234 }
Elliott Hughesef925e52016-03-01 17:27:12 -0800235 } else if (rta->rta_type == IFA_LOCAL) {
236 if (msg->ifa_family == AF_INET || msg->ifa_family == AF_INET6) {
237 new_addr->SetLocalAddress(msg->ifa_family, RTA_DATA(rta), RTA_PAYLOAD(rta));
238 }
Bram Bonnéd54ad072020-01-15 14:17:07 +0100239 } else if (rta->rta_type == IFA_LABEL) {
240 if (RTA_PAYLOAD(rta) < sizeof(new_addr->name)) {
241 memcpy(new_addr->name, RTA_DATA(rta), RTA_PAYLOAD(rta));
242 new_addr->ifa.ifa_name = new_addr->name;
243 }
Elliott Hughes9cddb482016-01-04 20:38:05 +0000244 }
245 rta = RTA_NEXT(rta, rta_len);
246 }
247 }
248}
249
Bram Bonnéd54ad072020-01-15 14:17:07 +0100250static void remove_nameless_interfaces(ifaddrs** list) {
251 ifaddrs_storage* addr = reinterpret_cast<ifaddrs_storage*>(*list);
252 ifaddrs_storage* prev_addr = nullptr;
253 while (addr != nullptr) {
254 ifaddrs* next_addr = addr->ifa.ifa_next;
255 if (strlen(addr->name) == 0) {
256 if (prev_addr == nullptr) {
257 *list = next_addr;
258 } else {
259 prev_addr->ifa.ifa_next = next_addr;
260 }
261 free(addr);
262 } else {
263 prev_addr = addr;
264 }
265 addr = reinterpret_cast<ifaddrs_storage*>(next_addr);
266 }
267}
268
Elliott Hughes9cddb482016-01-04 20:38:05 +0000269int getifaddrs(ifaddrs** out) {
Elliott Hughesed57b982016-01-15 21:02:56 -0800270 // We construct the result directly into `out`, so terminate the list.
Elliott Hughes9cddb482016-01-04 20:38:05 +0000271 *out = nullptr;
272
Elliott Hughes9cddb482016-01-04 20:38:05 +0000273 // Open the netlink socket and ask for all the links and addresses.
Elliott Hughesed57b982016-01-15 21:02:56 -0800274 NetlinkConnection nc;
Bram Bonnéd54ad072020-01-15 14:17:07 +0100275 bool getlink_success =
276 nc.SendRequest(RTM_GETLINK) && nc.ReadResponses(__getifaddrs_callback, out);
277 bool getaddr_success =
278 nc.SendRequest(RTM_GETADDR) && nc.ReadResponses(__getifaddrs_callback, out);
279
280 if (!getaddr_success) {
Elliott Hughes9cddb482016-01-04 20:38:05 +0000281 freeifaddrs(*out);
282 // Ensure that callers crash if they forget to check for success.
283 *out = nullptr;
Elliott Hughesed57b982016-01-15 21:02:56 -0800284 return -1;
Elliott Hughes9cddb482016-01-04 20:38:05 +0000285 }
Elliott Hughesed57b982016-01-15 21:02:56 -0800286
Bram Bonnéd54ad072020-01-15 14:17:07 +0100287 if (!getlink_success) {
288 async_safe_format_log(ANDROID_LOG_INFO, "ifaddrs", "Failed to send RTM_GETLINK request");
289 // If we weren't able to depend on GETLINK messages, it's possible some
290 // interfaces never got their name set. Remove those.
291 remove_nameless_interfaces(out);
292 }
293
Elliott Hughesed57b982016-01-15 21:02:56 -0800294 return 0;
Elliott Hughes9cddb482016-01-04 20:38:05 +0000295}
296
297void freeifaddrs(ifaddrs* list) {
298 while (list != nullptr) {
299 ifaddrs* current = list;
300 list = list->ifa_next;
301 free(current);
302 }
303}