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 | |
| 31 | #include <errno.h> |
Yi Kong | fdb2963 | 2015-12-22 17:07:23 +0000 | [diff] [blame^] | 32 | #include <linux/if_packet.h> |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 33 | #include <linux/netlink.h> |
| 34 | #include <linux/rtnetlink.h> |
| 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 | |
| 43 | // The public ifaddrs struct is full of pointers. Rather than track several |
| 44 | // different allocations, we use a maximally-sized structure with the public |
| 45 | // part at offset 0, and pointers into its hidden tail. |
| 46 | struct ifaddrs_storage { |
| 47 | // Must come first, so that `ifaddrs_storage` is-a `ifaddrs`. |
| 48 | ifaddrs ifa; |
| 49 | |
| 50 | // The interface index, so we can match RTM_NEWADDR messages with |
| 51 | // earlier RTM_NEWLINK messages (to copy the interface flags). |
| 52 | int interface_index; |
| 53 | |
| 54 | // Storage for the pointers in `ifa`. |
| 55 | sockaddr_storage addr; |
| 56 | sockaddr_storage netmask; |
| 57 | sockaddr_storage ifa_ifu; |
| 58 | char name[IFNAMSIZ + 1]; |
| 59 | |
| 60 | ifaddrs_storage(ifaddrs** list) { |
| 61 | memset(this, 0, sizeof(*this)); |
| 62 | |
| 63 | // push_front onto `list`. |
| 64 | ifa.ifa_next = *list; |
| 65 | *list = reinterpret_cast<ifaddrs*>(this); |
| 66 | } |
| 67 | |
| 68 | // Netlink gives us the address family in the header, and the |
| 69 | // sockaddr_in or sockaddr_in6 bytes as the payload. We need to |
| 70 | // stitch the two bits together into the sockaddr that's part of |
| 71 | // our portable interface. |
| 72 | void SetAddress(int family, const void* data, size_t byteCount) { |
| 73 | addr.ss_family = family; |
| 74 | memcpy(SockaddrBytes(family, &addr), data, byteCount); |
| 75 | ifa.ifa_addr = reinterpret_cast<sockaddr*>(&addr); |
| 76 | } |
| 77 | |
| 78 | void SetBroadcastAddress(int family, const void* data, size_t byteCount) { |
| 79 | ifa_ifu.ss_family = family; |
| 80 | memcpy(SockaddrBytes(family, &ifa_ifu), data, byteCount); |
| 81 | ifa.ifa_dstaddr = reinterpret_cast<sockaddr*>(&ifa_ifu); |
| 82 | } |
| 83 | |
| 84 | // Netlink gives us the prefix length as a bit count. We need to turn |
| 85 | // that into a BSD-compatible netmask represented by a sockaddr*. |
| 86 | void SetNetmask(int family, size_t prefix_length) { |
| 87 | // ...and work out the netmask from the prefix length. |
| 88 | netmask.ss_family = family; |
| 89 | uint8_t* dst = SockaddrBytes(family, &netmask); |
| 90 | memset(dst, 0xff, prefix_length / 8); |
| 91 | if ((prefix_length % 8) != 0) { |
| 92 | dst[prefix_length/8] = (0xff << (8 - (prefix_length % 8))); |
| 93 | } |
| 94 | ifa.ifa_netmask = reinterpret_cast<sockaddr*>(&netmask); |
| 95 | } |
| 96 | |
Yi Kong | fdb2963 | 2015-12-22 17:07:23 +0000 | [diff] [blame^] | 97 | void SetPacketAttributes(int ifindex, unsigned short hatype, unsigned char halen) { |
| 98 | sockaddr_ll* sll = reinterpret_cast<sockaddr_ll*>(&addr); |
| 99 | sll->sll_ifindex = ifindex; |
| 100 | sll->sll_hatype = hatype; |
| 101 | sll->sll_halen = halen; |
| 102 | } |
| 103 | |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 104 | private: |
| 105 | // Returns a pointer to the first byte in the address data (which is |
| 106 | // stored in network byte order). |
| 107 | uint8_t* SockaddrBytes(int family, sockaddr_storage* ss) { |
| 108 | if (family == AF_INET) { |
| 109 | sockaddr_in* ss4 = reinterpret_cast<sockaddr_in*>(ss); |
| 110 | return reinterpret_cast<uint8_t*>(&ss4->sin_addr); |
| 111 | } else if (family == AF_INET6) { |
| 112 | sockaddr_in6* ss6 = reinterpret_cast<sockaddr_in6*>(ss); |
| 113 | return reinterpret_cast<uint8_t*>(&ss6->sin6_addr); |
Yi Kong | fdb2963 | 2015-12-22 17:07:23 +0000 | [diff] [blame^] | 114 | } else if (family == AF_PACKET) { |
| 115 | sockaddr_ll* sll = reinterpret_cast<sockaddr_ll*>(ss); |
| 116 | return reinterpret_cast<uint8_t*>(&sll->sll_addr); |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 117 | } |
| 118 | return nullptr; |
| 119 | } |
| 120 | }; |
| 121 | |
| 122 | #if !defined(__clang__) |
| 123 | // GCC gets confused by NLMSG_DATA and doesn't realize that the old-style |
| 124 | // cast is from a system header and should be ignored. |
| 125 | #pragma GCC diagnostic ignored "-Wold-style-cast" |
| 126 | #endif |
| 127 | |
| 128 | static void __handle_netlink_response(ifaddrs** out, nlmsghdr* hdr) { |
| 129 | if (hdr->nlmsg_type == RTM_NEWLINK) { |
| 130 | ifinfomsg* ifi = reinterpret_cast<ifinfomsg*>(NLMSG_DATA(hdr)); |
| 131 | |
| 132 | // Create a new ifaddr entry, and set the interface index and flags. |
| 133 | ifaddrs_storage* new_addr = new ifaddrs_storage(out); |
| 134 | new_addr->interface_index = ifi->ifi_index; |
| 135 | new_addr->ifa.ifa_flags = ifi->ifi_flags; |
| 136 | |
| 137 | // Go through the various bits of information and find the name. |
| 138 | rtattr* rta = IFLA_RTA(ifi); |
| 139 | size_t rta_len = IFLA_PAYLOAD(hdr); |
| 140 | while (RTA_OK(rta, rta_len)) { |
Yi Kong | fdb2963 | 2015-12-22 17:07:23 +0000 | [diff] [blame^] | 141 | if (rta->rta_type == IFLA_ADDRESS) { |
| 142 | if (RTA_PAYLOAD(rta) < sizeof(new_addr->addr)) { |
| 143 | new_addr->SetAddress(AF_PACKET, RTA_DATA(rta), RTA_PAYLOAD(rta)); |
| 144 | new_addr->SetPacketAttributes(ifi->ifi_index, ifi->ifi_type, RTA_PAYLOAD(rta)); |
| 145 | } |
| 146 | } else if (rta->rta_type == IFLA_BROADCAST) { |
| 147 | if (RTA_PAYLOAD(rta) < sizeof(new_addr->ifa_ifu)) { |
| 148 | new_addr->SetBroadcastAddress(AF_PACKET, RTA_DATA(rta), RTA_PAYLOAD(rta)); |
| 149 | new_addr->SetPacketAttributes(ifi->ifi_index, ifi->ifi_type, RTA_PAYLOAD(rta)); |
| 150 | } |
| 151 | } else if (rta->rta_type == IFLA_IFNAME) { |
| 152 | if (RTA_PAYLOAD(rta) < sizeof(new_addr->name)) { |
| 153 | memcpy(new_addr->name, RTA_DATA(rta), RTA_PAYLOAD(rta)); |
| 154 | new_addr->ifa.ifa_name = new_addr->name; |
| 155 | } |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 156 | } |
| 157 | rta = RTA_NEXT(rta, rta_len); |
| 158 | } |
| 159 | } else if (hdr->nlmsg_type == RTM_NEWADDR) { |
| 160 | ifaddrmsg* msg = reinterpret_cast<ifaddrmsg*>(NLMSG_DATA(hdr)); |
| 161 | |
| 162 | // We should already know about this from an RTM_NEWLINK message. |
Yi Kong | fdb2963 | 2015-12-22 17:07:23 +0000 | [diff] [blame^] | 163 | const ifaddrs_storage* addr = reinterpret_cast<const ifaddrs_storage*>(*out); |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 164 | while (addr != nullptr && addr->interface_index != static_cast<int>(msg->ifa_index)) { |
Yi Kong | fdb2963 | 2015-12-22 17:07:23 +0000 | [diff] [blame^] | 165 | addr = reinterpret_cast<const ifaddrs_storage*>(addr->ifa.ifa_next); |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 166 | } |
| 167 | // If this is an unknown interface, ignore whatever we're being told about it. |
| 168 | if (addr == nullptr) return; |
| 169 | |
| 170 | // Create a new ifaddr entry and copy what we already know. |
| 171 | ifaddrs_storage* new_addr = new ifaddrs_storage(out); |
| 172 | // We can just copy the name rather than look for IFA_LABEL. |
| 173 | strcpy(new_addr->name, addr->name); |
| 174 | new_addr->ifa.ifa_name = new_addr->name; |
| 175 | new_addr->ifa.ifa_flags = addr->ifa.ifa_flags; |
| 176 | new_addr->interface_index = addr->interface_index; |
| 177 | |
| 178 | // Go through the various bits of information and find the address |
| 179 | // and any broadcast/destination address. |
| 180 | rtattr* rta = IFA_RTA(msg); |
| 181 | size_t rta_len = IFA_PAYLOAD(hdr); |
| 182 | while (RTA_OK(rta, rta_len)) { |
| 183 | if (rta->rta_type == IFA_ADDRESS) { |
| 184 | if (msg->ifa_family == AF_INET || msg->ifa_family == AF_INET6) { |
Yi Kong | fdb2963 | 2015-12-22 17:07:23 +0000 | [diff] [blame^] | 185 | new_addr->SetAddress(msg->ifa_family, RTA_DATA(rta), RTA_PAYLOAD(rta)); |
| 186 | new_addr->SetNetmask(msg->ifa_family, msg->ifa_prefixlen); |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 187 | } |
| 188 | } else if (rta->rta_type == IFA_BROADCAST) { |
| 189 | if (msg->ifa_family == AF_INET || msg->ifa_family == AF_INET6) { |
Yi Kong | fdb2963 | 2015-12-22 17:07:23 +0000 | [diff] [blame^] | 190 | new_addr->SetBroadcastAddress(msg->ifa_family, RTA_DATA(rta), RTA_PAYLOAD(rta)); |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | rta = RTA_NEXT(rta, rta_len); |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | static bool __send_netlink_request(int fd, int type) { |
| 199 | struct NetlinkMessage { |
| 200 | nlmsghdr hdr; |
| 201 | rtgenmsg msg; |
| 202 | } request; |
| 203 | memset(&request, 0, sizeof(request)); |
| 204 | request.hdr.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST; |
| 205 | request.hdr.nlmsg_type = type; |
| 206 | request.hdr.nlmsg_len = sizeof(request); |
| 207 | request.msg.rtgen_family = AF_UNSPEC; // All families. |
| 208 | return (TEMP_FAILURE_RETRY(send(fd, &request, sizeof(request), 0)) == sizeof(request)); |
| 209 | } |
| 210 | |
| 211 | static bool __read_netlink_responses(int fd, ifaddrs** out, char* buf, size_t buf_len) { |
| 212 | ssize_t bytes_read; |
| 213 | // Read through all the responses, handing interesting ones to __handle_netlink_response. |
| 214 | while ((bytes_read = TEMP_FAILURE_RETRY(recv(fd, buf, buf_len, 0))) > 0) { |
| 215 | nlmsghdr* hdr = reinterpret_cast<nlmsghdr*>(buf); |
| 216 | for (; NLMSG_OK(hdr, static_cast<size_t>(bytes_read)); hdr = NLMSG_NEXT(hdr, bytes_read)) { |
| 217 | if (hdr->nlmsg_type == NLMSG_DONE) return true; |
| 218 | if (hdr->nlmsg_type == NLMSG_ERROR) return false; |
| 219 | __handle_netlink_response(out, hdr); |
| 220 | } |
| 221 | } |
| 222 | // We only get here if recv fails before we see a NLMSG_DONE. |
| 223 | return false; |
| 224 | } |
| 225 | |
| 226 | int getifaddrs(ifaddrs** out) { |
| 227 | // Make cleanup easy. |
| 228 | *out = nullptr; |
| 229 | |
| 230 | // The kernel keeps packets under 8KiB (NLMSG_GOODSIZE), |
| 231 | // but that's a bit too large to go on the stack. |
| 232 | size_t buf_len = 8192; |
| 233 | char* buf = new char[buf_len]; |
| 234 | if (buf == nullptr) return -1; |
| 235 | |
| 236 | // Open the netlink socket and ask for all the links and addresses. |
| 237 | int fd = socket(PF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE); |
| 238 | bool okay = fd != -1 && |
| 239 | __send_netlink_request(fd, RTM_GETLINK) && __read_netlink_responses(fd, out, buf, buf_len) && |
| 240 | __send_netlink_request(fd, RTM_GETADDR) && __read_netlink_responses(fd, out, buf, buf_len); |
| 241 | |
| 242 | if (!okay) { |
| 243 | freeifaddrs(*out); |
| 244 | // Ensure that callers crash if they forget to check for success. |
| 245 | *out = nullptr; |
| 246 | } |
| 247 | { |
| 248 | int saved_errno = errno; |
| 249 | close(fd); |
| 250 | delete[] buf; |
| 251 | errno = saved_errno; |
| 252 | } |
| 253 | return okay ? 0 : -1; |
| 254 | } |
| 255 | |
| 256 | void freeifaddrs(ifaddrs* list) { |
| 257 | while (list != nullptr) { |
| 258 | ifaddrs* current = list; |
| 259 | list = list->ifa_next; |
| 260 | free(current); |
| 261 | } |
| 262 | } |