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/ScopedFd.h"
44
45 #include "bionic_netlink.h"
46
if_indextoname(unsigned ifindex,char * ifname)47 char* if_indextoname(unsigned ifindex, char* ifname) {
48 ScopedFd s(socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC, 0));
49 if (s.get() == -1) return nullptr;
50
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);
54 }
55
if_nametoindex(const char * ifname)56 unsigned if_nametoindex(const char* ifname) {
57 ScopedFd s(socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC, 0));
58 if (s.get() == -1) return 0;
59
60 ifreq ifr = {};
61 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
62 ifr.ifr_name[IFNAMSIZ - 1] = 0;
63 return (ioctl(s.get(), SIOCGIFINDEX, &ifr) == -1) ? 0 : ifr.ifr_ifindex;
64 }
65
66 struct if_list {
67 if_list* next;
68 struct if_nameindex data;
69
if_listif_list70 explicit if_list(if_list** list) {
71 // push_front onto `list`.
72 next = *list;
73 *list = this;
74 }
75
Freeif_list76 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
__if_nameindex_callback(void * context,nlmsghdr * hdr)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
if_nameindex()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
if_freenameindex(struct if_nameindex * array)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 }
154