blob: d03d76f9791f961f28915b3107c3a8555d506a4a [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
70 // Netlink gives us the address family in the header, and the
71 // sockaddr_in or sockaddr_in6 bytes as the payload. We need to
72 // stitch the two bits together into the sockaddr that's part of
73 // our portable interface.
74 void SetAddress(int family, const void* data, size_t byteCount) {
75 addr.ss_family = family;
76 memcpy(SockaddrBytes(family, &addr), data, byteCount);
77 ifa.ifa_addr = reinterpret_cast<sockaddr*>(&addr);
78 }
79
80 void SetBroadcastAddress(int family, const void* data, size_t byteCount) {
81 ifa_ifu.ss_family = family;
82 memcpy(SockaddrBytes(family, &ifa_ifu), data, byteCount);
83 ifa.ifa_dstaddr = reinterpret_cast<sockaddr*>(&ifa_ifu);
84 }
85
86 // Netlink gives us the prefix length as a bit count. We need to turn
87 // that into a BSD-compatible netmask represented by a sockaddr*.
88 void SetNetmask(int family, size_t prefix_length) {
89 // ...and work out the netmask from the prefix length.
90 netmask.ss_family = family;
91 uint8_t* dst = SockaddrBytes(family, &netmask);
92 memset(dst, 0xff, prefix_length / 8);
93 if ((prefix_length % 8) != 0) {
94 dst[prefix_length/8] = (0xff << (8 - (prefix_length % 8)));
95 }
96 ifa.ifa_netmask = reinterpret_cast<sockaddr*>(&netmask);
97 }
98
Yi Kongfdb29632015-12-22 17:07:23 +000099 void SetPacketAttributes(int ifindex, unsigned short hatype, unsigned char halen) {
100 sockaddr_ll* sll = reinterpret_cast<sockaddr_ll*>(&addr);
101 sll->sll_ifindex = ifindex;
102 sll->sll_hatype = hatype;
103 sll->sll_halen = halen;
104 }
105
Elliott Hughes9cddb482016-01-04 20:38:05 +0000106 private:
107 // Returns a pointer to the first byte in the address data (which is
108 // stored in network byte order).
109 uint8_t* SockaddrBytes(int family, sockaddr_storage* ss) {
110 if (family == AF_INET) {
111 sockaddr_in* ss4 = reinterpret_cast<sockaddr_in*>(ss);
112 return reinterpret_cast<uint8_t*>(&ss4->sin_addr);
113 } else if (family == AF_INET6) {
114 sockaddr_in6* ss6 = reinterpret_cast<sockaddr_in6*>(ss);
115 return reinterpret_cast<uint8_t*>(&ss6->sin6_addr);
Yi Kongfdb29632015-12-22 17:07:23 +0000116 } else if (family == AF_PACKET) {
117 sockaddr_ll* sll = reinterpret_cast<sockaddr_ll*>(ss);
118 return reinterpret_cast<uint8_t*>(&sll->sll_addr);
Elliott Hughes9cddb482016-01-04 20:38:05 +0000119 }
120 return nullptr;
121 }
122};
123
Elliott Hughesed57b982016-01-15 21:02:56 -0800124static void __getifaddrs_callback(void* context, nlmsghdr* hdr) {
125 ifaddrs** out = reinterpret_cast<ifaddrs**>(context);
Elliott Hughes9cddb482016-01-04 20:38:05 +0000126
Elliott Hughes9cddb482016-01-04 20:38:05 +0000127 if (hdr->nlmsg_type == RTM_NEWLINK) {
128 ifinfomsg* ifi = reinterpret_cast<ifinfomsg*>(NLMSG_DATA(hdr));
129
130 // Create a new ifaddr entry, and set the interface index and flags.
131 ifaddrs_storage* new_addr = new ifaddrs_storage(out);
132 new_addr->interface_index = ifi->ifi_index;
133 new_addr->ifa.ifa_flags = ifi->ifi_flags;
134
135 // Go through the various bits of information and find the name.
136 rtattr* rta = IFLA_RTA(ifi);
137 size_t rta_len = IFLA_PAYLOAD(hdr);
138 while (RTA_OK(rta, rta_len)) {
Yi Kongfdb29632015-12-22 17:07:23 +0000139 if (rta->rta_type == IFLA_ADDRESS) {
140 if (RTA_PAYLOAD(rta) < sizeof(new_addr->addr)) {
141 new_addr->SetAddress(AF_PACKET, RTA_DATA(rta), RTA_PAYLOAD(rta));
142 new_addr->SetPacketAttributes(ifi->ifi_index, ifi->ifi_type, RTA_PAYLOAD(rta));
143 }
144 } else if (rta->rta_type == IFLA_BROADCAST) {
145 if (RTA_PAYLOAD(rta) < sizeof(new_addr->ifa_ifu)) {
146 new_addr->SetBroadcastAddress(AF_PACKET, RTA_DATA(rta), RTA_PAYLOAD(rta));
147 new_addr->SetPacketAttributes(ifi->ifi_index, ifi->ifi_type, RTA_PAYLOAD(rta));
148 }
149 } else if (rta->rta_type == IFLA_IFNAME) {
150 if (RTA_PAYLOAD(rta) < sizeof(new_addr->name)) {
151 memcpy(new_addr->name, RTA_DATA(rta), RTA_PAYLOAD(rta));
152 new_addr->ifa.ifa_name = new_addr->name;
153 }
Elliott Hughes9cddb482016-01-04 20:38:05 +0000154 }
155 rta = RTA_NEXT(rta, rta_len);
156 }
157 } else if (hdr->nlmsg_type == RTM_NEWADDR) {
158 ifaddrmsg* msg = reinterpret_cast<ifaddrmsg*>(NLMSG_DATA(hdr));
159
160 // We should already know about this from an RTM_NEWLINK message.
Yi Kongfdb29632015-12-22 17:07:23 +0000161 const ifaddrs_storage* addr = reinterpret_cast<const ifaddrs_storage*>(*out);
Elliott Hughes9cddb482016-01-04 20:38:05 +0000162 while (addr != nullptr && addr->interface_index != static_cast<int>(msg->ifa_index)) {
Yi Kongfdb29632015-12-22 17:07:23 +0000163 addr = reinterpret_cast<const ifaddrs_storage*>(addr->ifa.ifa_next);
Elliott Hughes9cddb482016-01-04 20:38:05 +0000164 }
165 // If this is an unknown interface, ignore whatever we're being told about it.
166 if (addr == nullptr) return;
167
168 // Create a new ifaddr entry and copy what we already know.
169 ifaddrs_storage* new_addr = new ifaddrs_storage(out);
170 // We can just copy the name rather than look for IFA_LABEL.
171 strcpy(new_addr->name, addr->name);
172 new_addr->ifa.ifa_name = new_addr->name;
173 new_addr->ifa.ifa_flags = addr->ifa.ifa_flags;
174 new_addr->interface_index = addr->interface_index;
175
176 // Go through the various bits of information and find the address
177 // and any broadcast/destination address.
178 rtattr* rta = IFA_RTA(msg);
179 size_t rta_len = IFA_PAYLOAD(hdr);
180 while (RTA_OK(rta, rta_len)) {
181 if (rta->rta_type == IFA_ADDRESS) {
182 if (msg->ifa_family == AF_INET || msg->ifa_family == AF_INET6) {
Yi Kongfdb29632015-12-22 17:07:23 +0000183 new_addr->SetAddress(msg->ifa_family, RTA_DATA(rta), RTA_PAYLOAD(rta));
184 new_addr->SetNetmask(msg->ifa_family, msg->ifa_prefixlen);
Elliott Hughes9cddb482016-01-04 20:38:05 +0000185 }
186 } else if (rta->rta_type == IFA_BROADCAST) {
187 if (msg->ifa_family == AF_INET || msg->ifa_family == AF_INET6) {
Yi Kongfdb29632015-12-22 17:07:23 +0000188 new_addr->SetBroadcastAddress(msg->ifa_family, RTA_DATA(rta), RTA_PAYLOAD(rta));
Elliott Hughes9cddb482016-01-04 20:38:05 +0000189 }
190 }
191 rta = RTA_NEXT(rta, rta_len);
192 }
193 }
194}
195
Elliott Hughes9cddb482016-01-04 20:38:05 +0000196int getifaddrs(ifaddrs** out) {
Elliott Hughesed57b982016-01-15 21:02:56 -0800197 // We construct the result directly into `out`, so terminate the list.
Elliott Hughes9cddb482016-01-04 20:38:05 +0000198 *out = nullptr;
199
Elliott Hughes9cddb482016-01-04 20:38:05 +0000200 // Open the netlink socket and ask for all the links and addresses.
Elliott Hughesed57b982016-01-15 21:02:56 -0800201 NetlinkConnection nc;
202 bool okay = nc.SendRequest(RTM_GETLINK) && nc.ReadResponses(__getifaddrs_callback, out) &&
203 nc.SendRequest(RTM_GETADDR) && nc.ReadResponses(__getifaddrs_callback, out);
Elliott Hughes9cddb482016-01-04 20:38:05 +0000204 if (!okay) {
205 freeifaddrs(*out);
206 // Ensure that callers crash if they forget to check for success.
207 *out = nullptr;
Elliott Hughesed57b982016-01-15 21:02:56 -0800208 return -1;
Elliott Hughes9cddb482016-01-04 20:38:05 +0000209 }
Elliott Hughesed57b982016-01-15 21:02:56 -0800210
211 return 0;
Elliott Hughes9cddb482016-01-04 20:38:05 +0000212}
213
214void freeifaddrs(ifaddrs* list) {
215 while (list != nullptr) {
216 ifaddrs* current = list;
217 list = list->ifa_next;
218 free(current);
219 }
220}