Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/os/linux/vm/ifaddrs.cpp
32285 views
/*1* Copyright (C) 2015 The Android Open Source Project2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* * Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* * Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in11* the documentation and/or other materials provided with the12* distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS15* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT16* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS17* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE18* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,19* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,20* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS21* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED22* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,23* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT24* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/27#include <ifaddrs.h>28#include <errno.h>29#include <linux/if_packet.h>30#include <linux/netlink.h>31#include <linux/rtnetlink.h>32#include <net/if.h>33#include <netinet/in.h>34#include <stdint.h>35#include <stdio.h>36#include <stdlib.h>37#include <string.h>38#include <unistd.h>3940#define nullptr 04142// The public ifaddrs struct is full of pointers. Rather than track several43// different allocations, we use a maximally-sized structure with the public44// part at offset 0, and pointers into its hidden tail.45struct ifaddrs_storage {46// Must come first, so that `ifaddrs_storage` is-a `ifaddrs`.47ifaddrs ifa;48// The interface index, so we can match RTM_NEWADDR messages with49// earlier RTM_NEWLINK messages (to copy the interface flags).50int interface_index;51// Storage for the pointers in `ifa`.52sockaddr_storage addr;53sockaddr_storage netmask;54sockaddr_storage ifa_ifu;55char name[IFNAMSIZ + 1];56ifaddrs_storage(ifaddrs** list) {57memset(this, 0, sizeof(*this));58// push_front onto `list`.59ifa.ifa_next = *list;60*list = reinterpret_cast<ifaddrs*>(this);61}62// Netlink gives us the address family in the header, and the63// sockaddr_in or sockaddr_in6 bytes as the payload. We need to64// stitch the two bits together into the sockaddr that's part of65// our portable interface.66void SetAddress(int family, const void* data, size_t byteCount) {67addr.ss_family = family;68memcpy(SockaddrBytes(family, &addr), data, byteCount);69ifa.ifa_addr = reinterpret_cast<sockaddr*>(&addr);70}71void SetBroadcastAddress(int family, const void* data, size_t byteCount) {72ifa_ifu.ss_family = family;73memcpy(SockaddrBytes(family, &ifa_ifu), data, byteCount);74ifa.ifa_dstaddr = reinterpret_cast<sockaddr*>(&ifa_ifu);75}76// Netlink gives us the prefix length as a bit count. We need to turn77// that into a BSD-compatible netmask represented by a sockaddr*.78void SetNetmask(int family, size_t prefix_length) {79// ...and work out the netmask from the prefix length.80netmask.ss_family = family;81uint8_t* dst = SockaddrBytes(family, &netmask);82memset(dst, 0xff, prefix_length / 8);83if ((prefix_length % 8) != 0) {84dst[prefix_length/8] = (0xff << (8 - (prefix_length % 8)));85}86ifa.ifa_netmask = reinterpret_cast<sockaddr*>(&netmask);87}88void SetPacketAttributes(int ifindex, unsigned short hatype, unsigned char halen) {89sockaddr_ll* sll = reinterpret_cast<sockaddr_ll*>(&addr);90sll->sll_ifindex = ifindex;91sll->sll_hatype = hatype;92sll->sll_halen = halen;93}94private:95// Returns a pointer to the first byte in the address data (which is96// stored in network byte order).97uint8_t* SockaddrBytes(int family, sockaddr_storage* ss) {98if (family == AF_INET) {99sockaddr_in* ss4 = reinterpret_cast<sockaddr_in*>(ss);100return reinterpret_cast<uint8_t*>(&ss4->sin_addr);101} else if (family == AF_INET6) {102sockaddr_in6* ss6 = reinterpret_cast<sockaddr_in6*>(ss);103return reinterpret_cast<uint8_t*>(&ss6->sin6_addr);104} else if (family == AF_PACKET) {105sockaddr_ll* sll = reinterpret_cast<sockaddr_ll*>(ss);106return reinterpret_cast<uint8_t*>(&sll->sll_addr);107}108return nullptr;109}110};111#if !defined(__clang__)112// GCC gets confused by NLMSG_DATA and doesn't realize that the old-style113// cast is from a system header and should be ignored.114#pragma GCC diagnostic ignored "-Wold-style-cast"115#endif116static void __handle_netlink_response(ifaddrs** out, nlmsghdr* hdr) {117if (hdr->nlmsg_type == RTM_NEWLINK) {118ifinfomsg* ifi = reinterpret_cast<ifinfomsg*>(NLMSG_DATA(hdr));119// Create a new ifaddr entry, and set the interface index and flags.120ifaddrs_storage* new_addr = new ifaddrs_storage(out);121new_addr->interface_index = ifi->ifi_index;122new_addr->ifa.ifa_flags = ifi->ifi_flags;123// Go through the various bits of information and find the name.124rtattr* rta = IFLA_RTA(ifi);125size_t rta_len = IFLA_PAYLOAD(hdr);126while (RTA_OK(rta, rta_len)) {127if (rta->rta_type == IFLA_ADDRESS) {128if (RTA_PAYLOAD(rta) < sizeof(new_addr->addr)) {129new_addr->SetAddress(AF_PACKET, RTA_DATA(rta), RTA_PAYLOAD(rta));130new_addr->SetPacketAttributes(ifi->ifi_index, ifi->ifi_type, RTA_PAYLOAD(rta));131}132} else if (rta->rta_type == IFLA_BROADCAST) {133if (RTA_PAYLOAD(rta) < sizeof(new_addr->ifa_ifu)) {134new_addr->SetBroadcastAddress(AF_PACKET, RTA_DATA(rta), RTA_PAYLOAD(rta));135new_addr->SetPacketAttributes(ifi->ifi_index, ifi->ifi_type, RTA_PAYLOAD(rta));136}137} else if (rta->rta_type == IFLA_IFNAME) {138if (RTA_PAYLOAD(rta) < sizeof(new_addr->name)) {139memcpy(new_addr->name, RTA_DATA(rta), RTA_PAYLOAD(rta));140new_addr->ifa.ifa_name = new_addr->name;141}142}143rta = RTA_NEXT(rta, rta_len);144}145} else if (hdr->nlmsg_type == RTM_NEWADDR) {146ifaddrmsg* msg = reinterpret_cast<ifaddrmsg*>(NLMSG_DATA(hdr));147// We should already know about this from an RTM_NEWLINK message.148const ifaddrs_storage* addr = reinterpret_cast<const ifaddrs_storage*>(*out);149while (addr != nullptr && addr->interface_index != static_cast<int>(msg->ifa_index)) {150addr = reinterpret_cast<const ifaddrs_storage*>(addr->ifa.ifa_next);151}152// If this is an unknown interface, ignore whatever we're being told about it.153if (addr == nullptr) return;154// Create a new ifaddr entry and copy what we already know.155ifaddrs_storage* new_addr = new ifaddrs_storage(out);156// We can just copy the name rather than look for IFA_LABEL.157strcpy(new_addr->name, addr->name);158new_addr->ifa.ifa_name = new_addr->name;159new_addr->ifa.ifa_flags = addr->ifa.ifa_flags;160new_addr->interface_index = addr->interface_index;161// Go through the various bits of information and find the address162// and any broadcast/destination address.163rtattr* rta = IFA_RTA(msg);164size_t rta_len = IFA_PAYLOAD(hdr);165while (RTA_OK(rta, rta_len)) {166if (rta->rta_type == IFA_ADDRESS) {167if (msg->ifa_family == AF_INET || msg->ifa_family == AF_INET6) {168new_addr->SetAddress(msg->ifa_family, RTA_DATA(rta), RTA_PAYLOAD(rta));169new_addr->SetNetmask(msg->ifa_family, msg->ifa_prefixlen);170}171} else if (rta->rta_type == IFA_BROADCAST) {172if (msg->ifa_family == AF_INET || msg->ifa_family == AF_INET6) {173new_addr->SetBroadcastAddress(msg->ifa_family, RTA_DATA(rta), RTA_PAYLOAD(rta));174}175}176rta = RTA_NEXT(rta, rta_len);177}178}179}180static bool __send_netlink_request(int fd, int type) {181struct NetlinkMessage {182nlmsghdr hdr;183rtgenmsg msg;184} request;185memset(&request, 0, sizeof(request));186request.hdr.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST;187request.hdr.nlmsg_type = type;188request.hdr.nlmsg_len = sizeof(request);189request.msg.rtgen_family = AF_UNSPEC; // All families.190return (TEMP_FAILURE_RETRY(send(fd, &request, sizeof(request), 0)) == sizeof(request));191}192static bool __read_netlink_responses(int fd, ifaddrs** out, char* buf, size_t buf_len) {193ssize_t bytes_read;194// Read through all the responses, handing interesting ones to __handle_netlink_response.195while ((bytes_read = TEMP_FAILURE_RETRY(recv(fd, buf, buf_len, 0))) > 0) {196nlmsghdr* hdr = reinterpret_cast<nlmsghdr*>(buf);197for (; NLMSG_OK(hdr, static_cast<size_t>(bytes_read)); hdr = NLMSG_NEXT(hdr, bytes_read)) {198if (hdr->nlmsg_type == NLMSG_DONE) return true;199if (hdr->nlmsg_type == NLMSG_ERROR) return false;200__handle_netlink_response(out, hdr);201}202}203// We only get here if recv fails before we see a NLMSG_DONE.204return false;205}206int getifaddrs(ifaddrs** out) {207// Make cleanup easy.208*out = nullptr;209// The kernel keeps packets under 8KiB (NLMSG_GOODSIZE),210// but that's a bit too large to go on the stack.211size_t buf_len = 8192;212char* buf = new char[buf_len];213if (buf == nullptr) return -1;214// Open the netlink socket and ask for all the links and addresses.215int fd = socket(PF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE);216bool okay = fd != -1 &&217__send_netlink_request(fd, RTM_GETLINK) && __read_netlink_responses(fd, out, buf, buf_len) &&218__send_netlink_request(fd, RTM_GETADDR) && __read_netlink_responses(fd, out, buf, buf_len);219if (!okay) {220freeifaddrs(*out);221// Ensure that callers crash if they forget to check for success.222*out = nullptr;223}224{225int saved_errno = errno;226close(fd);227delete[] buf;228errno = saved_errno;229}230return okay ? 0 : -1;231}232void freeifaddrs(ifaddrs* list) {233while (list != nullptr) {234ifaddrs* current = list;235list = list->ifa_next;236free(current);237}238}239240241