Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/os/linux/vm/ifaddrs.cpp
32285 views
1
/*
2
* Copyright (C) 2015 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
#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>
40
41
#define nullptr 0
42
43
// The public ifaddrs struct is full of pointers. Rather than track several
44
// different allocations, we use a maximally-sized structure with the public
45
// part at offset 0, and pointers into its hidden tail.
46
struct ifaddrs_storage {
47
// Must come first, so that `ifaddrs_storage` is-a `ifaddrs`.
48
ifaddrs ifa;
49
// The interface index, so we can match RTM_NEWADDR messages with
50
// earlier RTM_NEWLINK messages (to copy the interface flags).
51
int interface_index;
52
// Storage for the pointers in `ifa`.
53
sockaddr_storage addr;
54
sockaddr_storage netmask;
55
sockaddr_storage ifa_ifu;
56
char name[IFNAMSIZ + 1];
57
ifaddrs_storage(ifaddrs** list) {
58
memset(this, 0, sizeof(*this));
59
// push_front onto `list`.
60
ifa.ifa_next = *list;
61
*list = reinterpret_cast<ifaddrs*>(this);
62
}
63
// Netlink gives us the address family in the header, and the
64
// sockaddr_in or sockaddr_in6 bytes as the payload. We need to
65
// stitch the two bits together into the sockaddr that's part of
66
// our portable interface.
67
void SetAddress(int family, const void* data, size_t byteCount) {
68
addr.ss_family = family;
69
memcpy(SockaddrBytes(family, &addr), data, byteCount);
70
ifa.ifa_addr = reinterpret_cast<sockaddr*>(&addr);
71
}
72
void SetBroadcastAddress(int family, const void* data, size_t byteCount) {
73
ifa_ifu.ss_family = family;
74
memcpy(SockaddrBytes(family, &ifa_ifu), data, byteCount);
75
ifa.ifa_dstaddr = reinterpret_cast<sockaddr*>(&ifa_ifu);
76
}
77
// Netlink gives us the prefix length as a bit count. We need to turn
78
// that into a BSD-compatible netmask represented by a sockaddr*.
79
void SetNetmask(int family, size_t prefix_length) {
80
// ...and work out the netmask from the prefix length.
81
netmask.ss_family = family;
82
uint8_t* dst = SockaddrBytes(family, &netmask);
83
memset(dst, 0xff, prefix_length / 8);
84
if ((prefix_length % 8) != 0) {
85
dst[prefix_length/8] = (0xff << (8 - (prefix_length % 8)));
86
}
87
ifa.ifa_netmask = reinterpret_cast<sockaddr*>(&netmask);
88
}
89
void SetPacketAttributes(int ifindex, unsigned short hatype, unsigned char halen) {
90
sockaddr_ll* sll = reinterpret_cast<sockaddr_ll*>(&addr);
91
sll->sll_ifindex = ifindex;
92
sll->sll_hatype = hatype;
93
sll->sll_halen = halen;
94
}
95
private:
96
// Returns a pointer to the first byte in the address data (which is
97
// stored in network byte order).
98
uint8_t* SockaddrBytes(int family, sockaddr_storage* ss) {
99
if (family == AF_INET) {
100
sockaddr_in* ss4 = reinterpret_cast<sockaddr_in*>(ss);
101
return reinterpret_cast<uint8_t*>(&ss4->sin_addr);
102
} else if (family == AF_INET6) {
103
sockaddr_in6* ss6 = reinterpret_cast<sockaddr_in6*>(ss);
104
return reinterpret_cast<uint8_t*>(&ss6->sin6_addr);
105
} else if (family == AF_PACKET) {
106
sockaddr_ll* sll = reinterpret_cast<sockaddr_ll*>(ss);
107
return reinterpret_cast<uint8_t*>(&sll->sll_addr);
108
}
109
return nullptr;
110
}
111
};
112
#if !defined(__clang__)
113
// GCC gets confused by NLMSG_DATA and doesn't realize that the old-style
114
// cast is from a system header and should be ignored.
115
#pragma GCC diagnostic ignored "-Wold-style-cast"
116
#endif
117
static void __handle_netlink_response(ifaddrs** out, nlmsghdr* hdr) {
118
if (hdr->nlmsg_type == RTM_NEWLINK) {
119
ifinfomsg* ifi = reinterpret_cast<ifinfomsg*>(NLMSG_DATA(hdr));
120
// Create a new ifaddr entry, and set the interface index and flags.
121
ifaddrs_storage* new_addr = new ifaddrs_storage(out);
122
new_addr->interface_index = ifi->ifi_index;
123
new_addr->ifa.ifa_flags = ifi->ifi_flags;
124
// Go through the various bits of information and find the name.
125
rtattr* rta = IFLA_RTA(ifi);
126
size_t rta_len = IFLA_PAYLOAD(hdr);
127
while (RTA_OK(rta, rta_len)) {
128
if (rta->rta_type == IFLA_ADDRESS) {
129
if (RTA_PAYLOAD(rta) < sizeof(new_addr->addr)) {
130
new_addr->SetAddress(AF_PACKET, RTA_DATA(rta), RTA_PAYLOAD(rta));
131
new_addr->SetPacketAttributes(ifi->ifi_index, ifi->ifi_type, RTA_PAYLOAD(rta));
132
}
133
} else if (rta->rta_type == IFLA_BROADCAST) {
134
if (RTA_PAYLOAD(rta) < sizeof(new_addr->ifa_ifu)) {
135
new_addr->SetBroadcastAddress(AF_PACKET, RTA_DATA(rta), RTA_PAYLOAD(rta));
136
new_addr->SetPacketAttributes(ifi->ifi_index, ifi->ifi_type, RTA_PAYLOAD(rta));
137
}
138
} else if (rta->rta_type == IFLA_IFNAME) {
139
if (RTA_PAYLOAD(rta) < sizeof(new_addr->name)) {
140
memcpy(new_addr->name, RTA_DATA(rta), RTA_PAYLOAD(rta));
141
new_addr->ifa.ifa_name = new_addr->name;
142
}
143
}
144
rta = RTA_NEXT(rta, rta_len);
145
}
146
} else if (hdr->nlmsg_type == RTM_NEWADDR) {
147
ifaddrmsg* msg = reinterpret_cast<ifaddrmsg*>(NLMSG_DATA(hdr));
148
// We should already know about this from an RTM_NEWLINK message.
149
const ifaddrs_storage* addr = reinterpret_cast<const ifaddrs_storage*>(*out);
150
while (addr != nullptr && addr->interface_index != static_cast<int>(msg->ifa_index)) {
151
addr = 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.
154
if (addr == nullptr) return;
155
// Create a new ifaddr entry and copy what we already know.
156
ifaddrs_storage* new_addr = new ifaddrs_storage(out);
157
// We can just copy the name rather than look for IFA_LABEL.
158
strcpy(new_addr->name, addr->name);
159
new_addr->ifa.ifa_name = new_addr->name;
160
new_addr->ifa.ifa_flags = addr->ifa.ifa_flags;
161
new_addr->interface_index = addr->interface_index;
162
// Go through the various bits of information and find the address
163
// and any broadcast/destination address.
164
rtattr* rta = IFA_RTA(msg);
165
size_t rta_len = IFA_PAYLOAD(hdr);
166
while (RTA_OK(rta, rta_len)) {
167
if (rta->rta_type == IFA_ADDRESS) {
168
if (msg->ifa_family == AF_INET || msg->ifa_family == AF_INET6) {
169
new_addr->SetAddress(msg->ifa_family, RTA_DATA(rta), RTA_PAYLOAD(rta));
170
new_addr->SetNetmask(msg->ifa_family, msg->ifa_prefixlen);
171
}
172
} else if (rta->rta_type == IFA_BROADCAST) {
173
if (msg->ifa_family == AF_INET || msg->ifa_family == AF_INET6) {
174
new_addr->SetBroadcastAddress(msg->ifa_family, RTA_DATA(rta), RTA_PAYLOAD(rta));
175
}
176
}
177
rta = RTA_NEXT(rta, rta_len);
178
}
179
}
180
}
181
static bool __send_netlink_request(int fd, int type) {
182
struct NetlinkMessage {
183
nlmsghdr hdr;
184
rtgenmsg msg;
185
} request;
186
memset(&request, 0, sizeof(request));
187
request.hdr.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST;
188
request.hdr.nlmsg_type = type;
189
request.hdr.nlmsg_len = sizeof(request);
190
request.msg.rtgen_family = AF_UNSPEC; // All families.
191
return (TEMP_FAILURE_RETRY(send(fd, &request, sizeof(request), 0)) == sizeof(request));
192
}
193
static bool __read_netlink_responses(int fd, ifaddrs** out, char* buf, size_t buf_len) {
194
ssize_t bytes_read;
195
// Read through all the responses, handing interesting ones to __handle_netlink_response.
196
while ((bytes_read = TEMP_FAILURE_RETRY(recv(fd, buf, buf_len, 0))) > 0) {
197
nlmsghdr* hdr = reinterpret_cast<nlmsghdr*>(buf);
198
for (; NLMSG_OK(hdr, static_cast<size_t>(bytes_read)); hdr = NLMSG_NEXT(hdr, bytes_read)) {
199
if (hdr->nlmsg_type == NLMSG_DONE) return true;
200
if (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.
205
return false;
206
}
207
int 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.
212
size_t buf_len = 8192;
213
char* buf = new char[buf_len];
214
if (buf == nullptr) return -1;
215
// Open the netlink socket and ask for all the links and addresses.
216
int fd = socket(PF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE);
217
bool 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);
220
if (!okay) {
221
freeifaddrs(*out);
222
// Ensure that callers crash if they forget to check for success.
223
*out = nullptr;
224
}
225
{
226
int saved_errno = errno;
227
close(fd);
228
delete[] buf;
229
errno = saved_errno;
230
}
231
return okay ? 0 : -1;
232
}
233
void freeifaddrs(ifaddrs* list) {
234
while (list != nullptr) {
235
ifaddrs* current = list;
236
list = list->ifa_next;
237
free(current);
238
}
239
}
240
241