Home | History | Annotate | Download | only in bionic
      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 
     43 #include "private/ErrnoRestorer.h"
     44 
     45 #include "bionic_netlink.h"
     46 
     47 char* if_indextoname(unsigned ifindex, char* ifname) {
     48   int s = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC, 0);
     49   if (s == -1) return nullptr;
     50 
     51   struct ifreq ifr;
     52   memset(&ifr, 0, sizeof(ifr));
     53   ifr.ifr_ifindex = ifindex;
     54 
     55   int rc = ioctl(s, SIOCGIFNAME, &ifr);
     56   ErrnoRestorer errno_restorer;
     57   close(s);
     58   return (rc == -1) ? nullptr : strncpy(ifname, ifr.ifr_name, IFNAMSIZ);
     59 }
     60 
     61 unsigned if_nametoindex(const char* ifname) {
     62   int s = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC, 0);
     63   if (s == -1) return 0;
     64 
     65   struct ifreq ifr;
     66   memset(&ifr, 0, sizeof(ifr));
     67   strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
     68   ifr.ifr_name[IFNAMSIZ - 1] = 0;
     69 
     70   int rc = ioctl(s, SIOCGIFINDEX, &ifr);
     71   ErrnoRestorer errno_restorer;
     72   close(s);
     73   return (rc == -1) ? 0 : ifr.ifr_ifindex;
     74 }
     75 
     76 struct if_list {
     77   if_list* next;
     78   struct if_nameindex data;
     79 
     80   explicit if_list(if_list** list) {
     81     // push_front onto `list`.
     82     next = *list;
     83     *list = this;
     84   }
     85 
     86   static void Free(if_list* list, bool names_too) {
     87     while (list) {
     88       if_list* it = list;
     89       list = it->next;
     90       if (names_too) free(it->data.if_name);
     91       free(it);
     92     }
     93   }
     94 };
     95 
     96 static void __if_nameindex_callback(void* context, nlmsghdr* hdr) {
     97   if_list** list = reinterpret_cast<if_list**>(context);
     98   if (hdr->nlmsg_type == RTM_NEWLINK) {
     99     ifinfomsg* ifi = reinterpret_cast<ifinfomsg*>(NLMSG_DATA(hdr));
    100 
    101     // Create a new entry and set the interface index.
    102     if_list* new_link = new if_list(list);
    103     new_link->data.if_index = ifi->ifi_index;
    104 
    105     // Go through the various bits of information and find the name.
    106     rtattr* rta = IFLA_RTA(ifi);
    107     size_t rta_len = IFLA_PAYLOAD(hdr);
    108     while (RTA_OK(rta, rta_len)) {
    109       if (rta->rta_type == IFLA_IFNAME) {
    110         new_link->data.if_name = strndup(reinterpret_cast<char*>(RTA_DATA(rta)), RTA_PAYLOAD(rta));
    111       }
    112       rta = RTA_NEXT(rta, rta_len);
    113     }
    114   }
    115 }
    116 
    117 struct if_nameindex* if_nameindex() {
    118   if_list* list = nullptr;
    119 
    120   // Open the netlink socket and ask for all the links;
    121   NetlinkConnection nc;
    122   bool okay = nc.SendRequest(RTM_GETLINK) && nc.ReadResponses(__if_nameindex_callback, &list);
    123   if (!okay) {
    124     if_list::Free(list, true);
    125     return nullptr;
    126   }
    127 
    128   // Count the interfaces.
    129   size_t interface_count = 0;
    130   for (if_list* it = list; it != nullptr; it = it->next) {
    131     ++interface_count;
    132   }
    133 
    134   // Build the array POSIX requires us to return.
    135   struct if_nameindex* result = new struct if_nameindex[interface_count + 1];
    136   if (result) {
    137     struct if_nameindex* out = result;
    138     for (if_list* it = list; it != nullptr; it = it->next) {
    139       out->if_index = it->data.if_index;
    140       out->if_name = it->data.if_name;
    141       ++out;
    142     }
    143     out->if_index = 0;
    144     out->if_name = nullptr;
    145   }
    146 
    147   // Free temporary storage.
    148   if_list::Free(list, false);
    149 
    150   return result;
    151 }
    152 
    153 void if_freenameindex(struct if_nameindex* array) {
    154   if (array == nullptr) return;
    155 
    156   struct if_nameindex* ptr = array;
    157   while (ptr->if_index != 0 || ptr->if_name != nullptr) {
    158     free(ptr->if_name);
    159     ++ptr;
    160   }
    161 
    162   delete[] array;
    163 }
    164