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