blob: 1fb16d4f1a18a69c79d07d4bb045a24ecf1d6240 [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
31#include <errno.h>
Yi Kongfdb29632015-12-22 17:07:23 +000032#include <linux/if_packet.h>
Elliott Hughes9cddb482016-01-04 20:38:05 +000033#include <net/if.h>
34#include <netinet/in.h>
35#include <stdint.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <unistd.h>
40
Elliott Hughesed57b982016-01-15 21:02:56 -080041#include "private/ErrnoRestorer.h"
42
43#include "bionic_netlink.h"
44
Elliott Hughes9cddb482016-01-04 20:38:05 +000045// The public ifaddrs struct is full of pointers. Rather than track several
46// different allocations, we use a maximally-sized structure with the public
47// part at offset 0, and pointers into its hidden tail.
48struct ifaddrs_storage {
49 // Must come first, so that `ifaddrs_storage` is-a `ifaddrs`.
50 ifaddrs ifa;
51
52 // The interface index, so we can match RTM_NEWADDR messages with
53 // earlier RTM_NEWLINK messages (to copy the interface flags).
54 int interface_index;
55
56 // Storage for the pointers in `ifa`.
57 sockaddr_storage addr;
58 sockaddr_storage netmask;
59 sockaddr_storage ifa_ifu;
60 char name[IFNAMSIZ + 1];
61
62 ifaddrs_storage(ifaddrs** list) {
63 memset(this, 0, sizeof(*this));
64
65 // push_front onto `list`.
66 ifa.ifa_next = *list;
67 *list = reinterpret_cast<ifaddrs*>(this);
68 }
69
Elliott Hughes9cddb482016-01-04 20:38:05 +000070 void SetAddress(int family, const void* data, size_t byteCount) {
Elliott Hughes7dac8b82016-02-17 14:19:48 -080071 ifa.ifa_addr = CopyAddress(family, data, byteCount, &addr);
Elliott Hughes9cddb482016-01-04 20:38:05 +000072 }
73
74 void SetBroadcastAddress(int family, const void* data, size_t byteCount) {
Elliott Hughes7dac8b82016-02-17 14:19:48 -080075 ifa.ifa_dstaddr = CopyAddress(family, data, byteCount, &ifa_ifu);
Elliott Hughes9cddb482016-01-04 20:38:05 +000076 }
77
78 // Netlink gives us the prefix length as a bit count. We need to turn
79 // that into a BSD-compatible netmask represented by a sockaddr*.
80 void SetNetmask(int family, size_t prefix_length) {
81 // ...and work out the netmask from the prefix length.
82 netmask.ss_family = family;
83 uint8_t* dst = SockaddrBytes(family, &netmask);
84 memset(dst, 0xff, prefix_length / 8);
85 if ((prefix_length % 8) != 0) {
86 dst[prefix_length/8] = (0xff << (8 - (prefix_length % 8)));
87 }
88 ifa.ifa_netmask = reinterpret_cast<sockaddr*>(&netmask);
89 }
90
Yi Kongfdb29632015-12-22 17:07:23 +000091 void SetPacketAttributes(int ifindex, unsigned short hatype, unsigned char halen) {
92 sockaddr_ll* sll = reinterpret_cast<sockaddr_ll*>(&addr);
93 sll->sll_ifindex = ifindex;
94 sll->sll_hatype = hatype;
95 sll->sll_halen = halen;
96 }
97
Elliott Hughes9cddb482016-01-04 20:38:05 +000098 private:
Elliott Hughes7dac8b82016-02-17 14:19:48 -080099 sockaddr* CopyAddress(int family, const void* data, size_t byteCount, sockaddr_storage* ss) {
100 // Netlink gives us the address family in the header, and the
101 // sockaddr_in or sockaddr_in6 bytes as the payload. We need to
102 // stitch the two bits together into the sockaddr that's part of
103 // our portable interface.
104 ss->ss_family = family;
105 memcpy(SockaddrBytes(family, ss), data, byteCount);
106
107 // For IPv6 we might also have to set the scope id.
108 if (family == AF_INET6 && (IN6_IS_ADDR_LINKLOCAL(data) || IN6_IS_ADDR_MC_LINKLOCAL(data))) {
109 reinterpret_cast<sockaddr_in6*>(ss)->sin6_scope_id = interface_index;
110 }
111
112 return reinterpret_cast<sockaddr*>(ss);
113 }
114
Elliott Hughes9cddb482016-01-04 20:38:05 +0000115 // Returns a pointer to the first byte in the address data (which is
116 // stored in network byte order).
117 uint8_t* SockaddrBytes(int family, sockaddr_storage* ss) {
118 if (family == AF_INET) {
119 sockaddr_in* ss4 = reinterpret_cast<sockaddr_in*>(ss);
120 return reinterpret_cast<uint8_t*>(&ss4->sin_addr);
121 } else if (family == AF_INET6) {
122 sockaddr_in6* ss6 = reinterpret_cast<sockaddr_in6*>(ss);
123 return reinterpret_cast<uint8_t*>(&ss6->sin6_addr);
Yi Kongfdb29632015-12-22 17:07:23 +0000124 } else if (family == AF_PACKET) {
125 sockaddr_ll* sll = reinterpret_cast<sockaddr_ll*>(ss);
126 return reinterpret_cast<uint8_t*>(&sll->sll_addr);
Elliott Hughes9cddb482016-01-04 20:38:05 +0000127 }
128 return nullptr;
129 }
130};
131
Elliott Hughesed57b982016-01-15 21:02:56 -0800132static void __getifaddrs_callback(void* context, nlmsghdr* hdr) {
133 ifaddrs** out = reinterpret_cast<ifaddrs**>(context);
Elliott Hughes9cddb482016-01-04 20:38:05 +0000134
Elliott Hughes9cddb482016-01-04 20:38:05 +0000135 if (hdr->nlmsg_type == RTM_NEWLINK) {
136 ifinfomsg* ifi = reinterpret_cast<ifinfomsg*>(NLMSG_DATA(hdr));
137
138 // Create a new ifaddr entry, and set the interface index and flags.
139 ifaddrs_storage* new_addr = new ifaddrs_storage(out);
140 new_addr->interface_index = ifi->ifi_index;
141 new_addr->ifa.ifa_flags = ifi->ifi_flags;
142
143 // Go through the various bits of information and find the name.
144 rtattr* rta = IFLA_RTA(ifi);
145 size_t rta_len = IFLA_PAYLOAD(hdr);
146 while (RTA_OK(rta, rta_len)) {
Yi Kongfdb29632015-12-22 17:07:23 +0000147 if (rta->rta_type == IFLA_ADDRESS) {
148 if (RTA_PAYLOAD(rta) < sizeof(new_addr->addr)) {
149 new_addr->SetAddress(AF_PACKET, RTA_DATA(rta), RTA_PAYLOAD(rta));
150 new_addr->SetPacketAttributes(ifi->ifi_index, ifi->ifi_type, RTA_PAYLOAD(rta));
151 }
152 } else if (rta->rta_type == IFLA_BROADCAST) {
153 if (RTA_PAYLOAD(rta) < sizeof(new_addr->ifa_ifu)) {
154 new_addr->SetBroadcastAddress(AF_PACKET, RTA_DATA(rta), RTA_PAYLOAD(rta));
155 new_addr->SetPacketAttributes(ifi->ifi_index, ifi->ifi_type, RTA_PAYLOAD(rta));
156 }
157 } else if (rta->rta_type == IFLA_IFNAME) {
158 if (RTA_PAYLOAD(rta) < sizeof(new_addr->name)) {
159 memcpy(new_addr->name, RTA_DATA(rta), RTA_PAYLOAD(rta));
160 new_addr->ifa.ifa_name = new_addr->name;
161 }
Elliott Hughes9cddb482016-01-04 20:38:05 +0000162 }
163 rta = RTA_NEXT(rta, rta_len);
164 }
165 } else if (hdr->nlmsg_type == RTM_NEWADDR) {
166 ifaddrmsg* msg = reinterpret_cast<ifaddrmsg*>(NLMSG_DATA(hdr));
167
168 // We should already know about this from an RTM_NEWLINK message.
Yi Kongfdb29632015-12-22 17:07:23 +0000169 const ifaddrs_storage* addr = reinterpret_cast<const ifaddrs_storage*>(*out);
Elliott Hughes9cddb482016-01-04 20:38:05 +0000170 while (addr != nullptr && addr->interface_index != static_cast<int>(msg->ifa_index)) {
Yi Kongfdb29632015-12-22 17:07:23 +0000171 addr = reinterpret_cast<const ifaddrs_storage*>(addr->ifa.ifa_next);
Elliott Hughes9cddb482016-01-04 20:38:05 +0000172 }
173 // If this is an unknown interface, ignore whatever we're being told about it.
174 if (addr == nullptr) return;
175
176 // Create a new ifaddr entry and copy what we already know.
177 ifaddrs_storage* new_addr = new ifaddrs_storage(out);
178 // We can just copy the name rather than look for IFA_LABEL.
179 strcpy(new_addr->name, addr->name);
180 new_addr->ifa.ifa_name = new_addr->name;
181 new_addr->ifa.ifa_flags = addr->ifa.ifa_flags;
182 new_addr->interface_index = addr->interface_index;
183
184 // Go through the various bits of information and find the address
185 // and any broadcast/destination address.
186 rtattr* rta = IFA_RTA(msg);
187 size_t rta_len = IFA_PAYLOAD(hdr);
188 while (RTA_OK(rta, rta_len)) {
189 if (rta->rta_type == IFA_ADDRESS) {
190 if (msg->ifa_family == AF_INET || msg->ifa_family == AF_INET6) {
Yi Kongfdb29632015-12-22 17:07:23 +0000191 new_addr->SetAddress(msg->ifa_family, RTA_DATA(rta), RTA_PAYLOAD(rta));
192 new_addr->SetNetmask(msg->ifa_family, msg->ifa_prefixlen);
Elliott Hughes9cddb482016-01-04 20:38:05 +0000193 }
194 } else if (rta->rta_type == IFA_BROADCAST) {
195 if (msg->ifa_family == AF_INET || msg->ifa_family == AF_INET6) {
Yi Kongfdb29632015-12-22 17:07:23 +0000196 new_addr->SetBroadcastAddress(msg->ifa_family, RTA_DATA(rta), RTA_PAYLOAD(rta));
Elliott Hughes9cddb482016-01-04 20:38:05 +0000197 }
198 }
199 rta = RTA_NEXT(rta, rta_len);
200 }
201 }
202}
203
Elliott Hughes9cddb482016-01-04 20:38:05 +0000204int getifaddrs(ifaddrs** out) {
Elliott Hughesed57b982016-01-15 21:02:56 -0800205 // We construct the result directly into `out`, so terminate the list.
Elliott Hughes9cddb482016-01-04 20:38:05 +0000206 *out = nullptr;
207
Elliott Hughes9cddb482016-01-04 20:38:05 +0000208 // Open the netlink socket and ask for all the links and addresses.
Elliott Hughesed57b982016-01-15 21:02:56 -0800209 NetlinkConnection nc;
210 bool okay = nc.SendRequest(RTM_GETLINK) && nc.ReadResponses(__getifaddrs_callback, out) &&
211 nc.SendRequest(RTM_GETADDR) && nc.ReadResponses(__getifaddrs_callback, out);
Elliott Hughes9cddb482016-01-04 20:38:05 +0000212 if (!okay) {
213 freeifaddrs(*out);
214 // Ensure that callers crash if they forget to check for success.
215 *out = nullptr;
Elliott Hughesed57b982016-01-15 21:02:56 -0800216 return -1;
Elliott Hughes9cddb482016-01-04 20:38:05 +0000217 }
Elliott Hughesed57b982016-01-15 21:02:56 -0800218
219 return 0;
Elliott Hughes9cddb482016-01-04 20:38:05 +0000220}
221
222void freeifaddrs(ifaddrs* list) {
223 while (list != nullptr) {
224 ifaddrs* current = list;
225 list = list->ifa_next;
226 free(current);
227 }
228}