Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-aarch32-jdk8u
Path: blob/jdk8u272-b10-aarch32-20201026/hotspot/src/os/linux/vm/ifaddrs.cpp
48773 views
1
2
/*
3
* Copyright (C) 2015 The Android Open Source Project
4
* All rights reserved.
5
*
6
* Redistribution and use in source and binary forms, with or without
7
* modification, are permitted provided that the following conditions
8
* are met:
9
* * Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
* * Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in
13
* the documentation and/or other materials provided with the
14
* distribution.
15
*
16
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
23
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
* SUCH DAMAGE.
28
*/
29
#include <ifaddrs.h>
30
#include <errno.h>
31
#include <linux/if_packet.h>
32
#include <linux/netlink.h>
33
#include <linux/rtnetlink.h>
34
#include <net/if.h>
35
#include <netinet/in.h>
36
#include <stdint.h>
37
#include <stdio.h>
38
#include <stdlib.h>
39
#include <string.h>
40
#include <unistd.h>
41
42
#define nullptr 0
43
44
// The public ifaddrs struct is full of pointers. Rather than track several
45
// different allocations, we use a maximally-sized structure with the public
46
// part at offset 0, and pointers into its hidden tail.
47
struct ifaddrs_storage {
48
// Must come first, so that `ifaddrs_storage` is-a `ifaddrs`.
49
ifaddrs ifa;
50
// The interface index, so we can match RTM_NEWADDR messages with
51
// earlier RTM_NEWLINK messages (to copy the interface flags).
52
int interface_index;
53
// Storage for the pointers in `ifa`.
54
sockaddr_storage addr;
55
sockaddr_storage netmask;
56
sockaddr_storage ifa_ifu;
57
char name[IFNAMSIZ + 1];
58
ifaddrs_storage(ifaddrs** list) {
59
memset(this, 0, sizeof(*this));
60
// push_front onto `list`.
61
ifa.ifa_next = *list;
62
*list = reinterpret_cast<ifaddrs*>(this);
63
}
64
// Netlink gives us the address family in the header, and the
65
// sockaddr_in or sockaddr_in6 bytes as the payload. We need to
66
// stitch the two bits together into the sockaddr that's part of
67
// our portable interface.
68
void SetAddress(int family, const void* data, size_t byteCount) {
69
addr.ss_family = family;
70
memcpy(SockaddrBytes(family, &addr), data, byteCount);
71
ifa.ifa_addr = reinterpret_cast<sockaddr*>(&addr);
72
}
73
void SetBroadcastAddress(int family, const void* data, size_t byteCount) {
74
ifa_ifu.ss_family = family;
75
memcpy(SockaddrBytes(family, &ifa_ifu), data, byteCount);
76
ifa.ifa_dstaddr = reinterpret_cast<sockaddr*>(&ifa_ifu);
77
}
78
// Netlink gives us the prefix length as a bit count. We need to turn
79
// that into a BSD-compatible netmask represented by a sockaddr*.
80
void SetNetmask(int family, size_t prefix_length) {
81
// ...and work out the netmask from the prefix length.
82
netmask.ss_family = family;
83
uint8_t* dst = SockaddrBytes(family, &netmask);
84
memset(dst, 0xff, prefix_length / 8);
85
if ((prefix_length % 8) != 0) {
86
dst[prefix_length/8] = (0xff << (8 - (prefix_length % 8)));
87
}
88
ifa.ifa_netmask = reinterpret_cast<sockaddr*>(&netmask);
89
}
90
void SetPacketAttributes(int ifindex, unsigned short hatype, unsigned char halen) {
91
sockaddr_ll* sll = reinterpret_cast<sockaddr_ll*>(&addr);
92
sll->sll_ifindex = ifindex;
93
sll->sll_hatype = hatype;
94
sll->sll_halen = halen;
95
}
96
private:
97
// Returns a pointer to the first byte in the address data (which is
98
// stored in network byte order).
99
uint8_t* SockaddrBytes(int family, sockaddr_storage* ss) {
100
if (family == AF_INET) {
101
sockaddr_in* ss4 = reinterpret_cast<sockaddr_in*>(ss);
102
return reinterpret_cast<uint8_t*>(&ss4->sin_addr);
103
} else if (family == AF_INET6) {
104
sockaddr_in6* ss6 = reinterpret_cast<sockaddr_in6*>(ss);
105
return reinterpret_cast<uint8_t*>(&ss6->sin6_addr);
106
} else if (family == AF_PACKET) {
107
sockaddr_ll* sll = reinterpret_cast<sockaddr_ll*>(ss);
108
return reinterpret_cast<uint8_t*>(&sll->sll_addr);
109
}
110
return nullptr;
111
}
112
};
113
#if !defined(__clang__)
114
// GCC gets confused by NLMSG_DATA and doesn't realize that the old-style
115
// cast is from a system header and should be ignored.
116
#pragma GCC diagnostic ignored "-Wold-style-cast"
117
#endif
118
static void __handle_netlink_response(ifaddrs** out, nlmsghdr* hdr) {
119
if (hdr->nlmsg_type == RTM_NEWLINK) {
120
ifinfomsg* ifi = reinterpret_cast<ifinfomsg*>(NLMSG_DATA(hdr));
121
// Create a new ifaddr entry, and set the interface index and flags.
122
ifaddrs_storage* new_addr = new ifaddrs_storage(out);
123
new_addr->interface_index = ifi->ifi_index;
124
new_addr->ifa.ifa_flags = ifi->ifi_flags;
125
// Go through the various bits of information and find the name.
126
rtattr* rta = IFLA_RTA(ifi);
127
size_t rta_len = IFLA_PAYLOAD(hdr);
128
while (RTA_OK(rta, rta_len)) {
129
if (rta->rta_type == IFLA_ADDRESS) {
130
if (RTA_PAYLOAD(rta) < sizeof(new_addr->addr)) {
131
new_addr->SetAddress(AF_PACKET, RTA_DATA(rta), RTA_PAYLOAD(rta));
132
new_addr->SetPacketAttributes(ifi->ifi_index, ifi->ifi_type, RTA_PAYLOAD(rta));
133
}
134
} else if (rta->rta_type == IFLA_BROADCAST) {
135
if (RTA_PAYLOAD(rta) < sizeof(new_addr->ifa_ifu)) {
136
new_addr->SetBroadcastAddress(AF_PACKET, RTA_DATA(rta), RTA_PAYLOAD(rta));
137
new_addr->SetPacketAttributes(ifi->ifi_index, ifi->ifi_type, RTA_PAYLOAD(rta));
138
}
139
} else if (rta->rta_type == IFLA_IFNAME) {
140
if (RTA_PAYLOAD(rta) < sizeof(new_addr->name)) {
141
memcpy(new_addr->name, RTA_DATA(rta), RTA_PAYLOAD(rta));
142
new_addr->ifa.ifa_name = new_addr->name;
143
}
144
}
145
rta = RTA_NEXT(rta, rta_len);
146
}
147
} else if (hdr->nlmsg_type == RTM_NEWADDR) {
148
ifaddrmsg* msg = reinterpret_cast<ifaddrmsg*>(NLMSG_DATA(hdr));
149
// We should already know about this from an RTM_NEWLINK message.
150
const ifaddrs_storage* addr = reinterpret_cast<const ifaddrs_storage*>(*out);
151
while (addr != nullptr && addr->interface_index != static_cast<int>(msg->ifa_index)) {
152
addr = reinterpret_cast<const ifaddrs_storage*>(addr->ifa.ifa_next);
153
}
154
// If this is an unknown interface, ignore whatever we're being told about it.
155
if (addr == nullptr) return;
156
// Create a new ifaddr entry and copy what we already know.
157
ifaddrs_storage* new_addr = new ifaddrs_storage(out);
158
// We can just copy the name rather than look for IFA_LABEL.
159
strcpy(new_addr->name, addr->name);
160
new_addr->ifa.ifa_name = new_addr->name;
161
new_addr->ifa.ifa_flags = addr->ifa.ifa_flags;
162
new_addr->interface_index = addr->interface_index;
163
// Go through the various bits of information and find the address
164
// and any broadcast/destination address.
165
rtattr* rta = IFA_RTA(msg);
166
size_t rta_len = IFA_PAYLOAD(hdr);
167
while (RTA_OK(rta, rta_len)) {
168
if (rta->rta_type == IFA_ADDRESS) {
169
if (msg->ifa_family == AF_INET || msg->ifa_family == AF_INET6) {
170
new_addr->SetAddress(msg->ifa_family, RTA_DATA(rta), RTA_PAYLOAD(rta));
171
new_addr->SetNetmask(msg->ifa_family, msg->ifa_prefixlen);
172
}
173
} else if (rta->rta_type == IFA_BROADCAST) {
174
if (msg->ifa_family == AF_INET || msg->ifa_family == AF_INET6) {
175
new_addr->SetBroadcastAddress(msg->ifa_family, RTA_DATA(rta), RTA_PAYLOAD(rta));
176
}
177
}
178
rta = RTA_NEXT(rta, rta_len);
179
}
180
}
181
}
182
static bool __send_netlink_request(int fd, int type) {
183
struct NetlinkMessage {
184
nlmsghdr hdr;
185
rtgenmsg msg;
186
} request;
187
memset(&request, 0, sizeof(request));
188
request.hdr.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST;
189
request.hdr.nlmsg_type = type;
190
request.hdr.nlmsg_len = sizeof(request);
191
request.msg.rtgen_family = AF_UNSPEC; // All families.
192
return (TEMP_FAILURE_RETRY(send(fd, &request, sizeof(request), 0)) == sizeof(request));
193
}
194
static bool __read_netlink_responses(int fd, ifaddrs** out, char* buf, size_t buf_len) {
195
ssize_t bytes_read;
196
// Read through all the responses, handing interesting ones to __handle_netlink_response.
197
while ((bytes_read = TEMP_FAILURE_RETRY(recv(fd, buf, buf_len, 0))) > 0) {
198
nlmsghdr* hdr = reinterpret_cast<nlmsghdr*>(buf);
199
for (; NLMSG_OK(hdr, static_cast<size_t>(bytes_read)); hdr = NLMSG_NEXT(hdr, bytes_read)) {
200
if (hdr->nlmsg_type == NLMSG_DONE) return true;
201
if (hdr->nlmsg_type == NLMSG_ERROR) return false;
202
__handle_netlink_response(out, hdr);
203
}
204
}
205
// We only get here if recv fails before we see a NLMSG_DONE.
206
return false;
207
}
208
int getifaddrs(ifaddrs** out) {
209
// Make cleanup easy.
210
*out = nullptr;
211
// The kernel keeps packets under 8KiB (NLMSG_GOODSIZE),
212
// but that's a bit too large to go on the stack.
213
size_t buf_len = 8192;
214
char* buf = new char[buf_len];
215
if (buf == nullptr) return -1;
216
// Open the netlink socket and ask for all the links and addresses.
217
int fd = socket(PF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE);
218
bool okay = fd != -1 &&
219
__send_netlink_request(fd, RTM_GETLINK) && __read_netlink_responses(fd, out, buf, buf_len) &&
220
__send_netlink_request(fd, RTM_GETADDR) && __read_netlink_responses(fd, out, buf, buf_len);
221
if (!okay) {
222
freeifaddrs(*out);
223
// Ensure that callers crash if they forget to check for success.
224
*out = nullptr;
225
}
226
{
227
int saved_errno = errno;
228
close(fd);
229
delete[] buf;
230
errno = saved_errno;
231
}
232
return okay ? 0 : -1;
233
}
234
void freeifaddrs(ifaddrs* list) {
235
while (list != nullptr) {
236
ifaddrs* current = list;
237
list = list->ifa_next;
238
free(current);
239
}
240
}
241
242