| Elliott Hughes | ed57b98 | 2016-01-15 21:02:56 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2008 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 <net/if.h> | 
|  | 30 |  | 
|  | 31 | #include <errno.h> | 
|  | 32 | #include <ifaddrs.h> | 
|  | 33 | #include <linux/if_packet.h> | 
|  | 34 | #include <linux/netlink.h> | 
|  | 35 | #include <linux/rtnetlink.h> | 
|  | 36 | #include <linux/sockios.h> | 
|  | 37 | #include <stdlib.h> | 
|  | 38 | #include <string.h> | 
|  | 39 | #include <sys/ioctl.h> | 
|  | 40 | #include <sys/socket.h> | 
|  | 41 | #include <unistd.h> | 
|  | 42 |  | 
| Elliott Hughes | 6cb70ad | 2019-10-31 15:37:32 -0700 | [diff] [blame] | 43 | #include "private/ScopedFd.h" | 
| Elliott Hughes | ed57b98 | 2016-01-15 21:02:56 -0800 | [diff] [blame] | 44 |  | 
|  | 45 | #include "bionic_netlink.h" | 
|  | 46 |  | 
|  | 47 | char* if_indextoname(unsigned ifindex, char* ifname) { | 
| Elliott Hughes | 6cb70ad | 2019-10-31 15:37:32 -0700 | [diff] [blame] | 48 | ScopedFd s(socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC, 0)); | 
|  | 49 | if (s.get() == -1) return nullptr; | 
| Elliott Hughes | ed57b98 | 2016-01-15 21:02:56 -0800 | [diff] [blame] | 50 |  | 
| Elliott Hughes | 6cb70ad | 2019-10-31 15:37:32 -0700 | [diff] [blame] | 51 | ifreq ifr = {.ifr_ifindex = static_cast<int>(ifindex)}; | 
|  | 52 | return (ioctl(s.get(), SIOCGIFNAME, &ifr) == -1) ? nullptr | 
|  | 53 | : strncpy(ifname, ifr.ifr_name, IFNAMSIZ); | 
| Elliott Hughes | ed57b98 | 2016-01-15 21:02:56 -0800 | [diff] [blame] | 54 | } | 
|  | 55 |  | 
|  | 56 | unsigned if_nametoindex(const char* ifname) { | 
| Elliott Hughes | 6cb70ad | 2019-10-31 15:37:32 -0700 | [diff] [blame] | 57 | ScopedFd s(socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC, 0)); | 
|  | 58 | if (s.get() == -1) return 0; | 
| Elliott Hughes | ed57b98 | 2016-01-15 21:02:56 -0800 | [diff] [blame] | 59 |  | 
| Elliott Hughes | 6cb70ad | 2019-10-31 15:37:32 -0700 | [diff] [blame] | 60 | ifreq ifr = {}; | 
| Elliott Hughes | ed57b98 | 2016-01-15 21:02:56 -0800 | [diff] [blame] | 61 | strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); | 
|  | 62 | ifr.ifr_name[IFNAMSIZ - 1] = 0; | 
| Elliott Hughes | 6cb70ad | 2019-10-31 15:37:32 -0700 | [diff] [blame] | 63 | return (ioctl(s.get(), SIOCGIFINDEX, &ifr) == -1) ? 0 : ifr.ifr_ifindex; | 
| Elliott Hughes | ed57b98 | 2016-01-15 21:02:56 -0800 | [diff] [blame] | 64 | } | 
|  | 65 |  | 
|  | 66 | struct if_list { | 
|  | 67 | if_list* next; | 
|  | 68 | struct if_nameindex data; | 
|  | 69 |  | 
| Chih-Hung Hsieh | 62e3a07 | 2016-05-03 12:08:05 -0700 | [diff] [blame] | 70 | explicit if_list(if_list** list) { | 
| Elliott Hughes | ed57b98 | 2016-01-15 21:02:56 -0800 | [diff] [blame] | 71 | // push_front onto `list`. | 
|  | 72 | next = *list; | 
|  | 73 | *list = this; | 
|  | 74 | } | 
|  | 75 |  | 
|  | 76 | static void Free(if_list* list, bool names_too) { | 
|  | 77 | while (list) { | 
|  | 78 | if_list* it = list; | 
|  | 79 | list = it->next; | 
|  | 80 | if (names_too) free(it->data.if_name); | 
|  | 81 | free(it); | 
|  | 82 | } | 
|  | 83 | } | 
|  | 84 | }; | 
|  | 85 |  | 
|  | 86 | static void __if_nameindex_callback(void* context, nlmsghdr* hdr) { | 
|  | 87 | if_list** list = reinterpret_cast<if_list**>(context); | 
|  | 88 | if (hdr->nlmsg_type == RTM_NEWLINK) { | 
|  | 89 | ifinfomsg* ifi = reinterpret_cast<ifinfomsg*>(NLMSG_DATA(hdr)); | 
|  | 90 |  | 
|  | 91 | // Create a new entry and set the interface index. | 
|  | 92 | if_list* new_link = new if_list(list); | 
|  | 93 | new_link->data.if_index = ifi->ifi_index; | 
|  | 94 |  | 
|  | 95 | // Go through the various bits of information and find the name. | 
|  | 96 | rtattr* rta = IFLA_RTA(ifi); | 
|  | 97 | size_t rta_len = IFLA_PAYLOAD(hdr); | 
|  | 98 | while (RTA_OK(rta, rta_len)) { | 
|  | 99 | if (rta->rta_type == IFLA_IFNAME) { | 
|  | 100 | new_link->data.if_name = strndup(reinterpret_cast<char*>(RTA_DATA(rta)), RTA_PAYLOAD(rta)); | 
|  | 101 | } | 
|  | 102 | rta = RTA_NEXT(rta, rta_len); | 
|  | 103 | } | 
|  | 104 | } | 
|  | 105 | } | 
|  | 106 |  | 
|  | 107 | struct if_nameindex* if_nameindex() { | 
|  | 108 | if_list* list = nullptr; | 
|  | 109 |  | 
|  | 110 | // Open the netlink socket and ask for all the links; | 
|  | 111 | NetlinkConnection nc; | 
|  | 112 | bool okay = nc.SendRequest(RTM_GETLINK) && nc.ReadResponses(__if_nameindex_callback, &list); | 
|  | 113 | if (!okay) { | 
|  | 114 | if_list::Free(list, true); | 
|  | 115 | return nullptr; | 
|  | 116 | } | 
|  | 117 |  | 
|  | 118 | // Count the interfaces. | 
|  | 119 | size_t interface_count = 0; | 
|  | 120 | for (if_list* it = list; it != nullptr; it = it->next) { | 
|  | 121 | ++interface_count; | 
|  | 122 | } | 
|  | 123 |  | 
|  | 124 | // Build the array POSIX requires us to return. | 
|  | 125 | struct if_nameindex* result = new struct if_nameindex[interface_count + 1]; | 
|  | 126 | if (result) { | 
|  | 127 | struct if_nameindex* out = result; | 
|  | 128 | for (if_list* it = list; it != nullptr; it = it->next) { | 
|  | 129 | out->if_index = it->data.if_index; | 
|  | 130 | out->if_name = it->data.if_name; | 
|  | 131 | ++out; | 
|  | 132 | } | 
|  | 133 | out->if_index = 0; | 
|  | 134 | out->if_name = nullptr; | 
|  | 135 | } | 
|  | 136 |  | 
|  | 137 | // Free temporary storage. | 
|  | 138 | if_list::Free(list, false); | 
|  | 139 |  | 
|  | 140 | return result; | 
|  | 141 | } | 
|  | 142 |  | 
|  | 143 | void if_freenameindex(struct if_nameindex* array) { | 
|  | 144 | if (array == nullptr) return; | 
|  | 145 |  | 
|  | 146 | struct if_nameindex* ptr = array; | 
|  | 147 | while (ptr->if_index != 0 || ptr->if_name != nullptr) { | 
|  | 148 | free(ptr->if_name); | 
|  | 149 | ++ptr; | 
|  | 150 | } | 
|  | 151 |  | 
|  | 152 | delete[] array; | 
|  | 153 | } |