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