1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
|
/* $Id: NetIf-darwin.cpp $ */
/** @file
* Main - NetIfList, Darwin implementation.
*/
/*
* Copyright (C) 2008-2022 Oracle and/or its affiliates.
*
* This file is part of VirtualBox base platform packages, as
* available from https://www.virtualbox.org.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, in version 3 of the
* License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses>.
*
* SPDX-License-Identifier: GPL-3.0-only
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#define LOG_GROUP LOG_GROUP_MAIN_HOST
/*
* Deal with conflicts first.
* PVM - BSD mess, that FreeBSD has correct a long time ago.
* iprt/types.h before sys/param.h - prevents UINT32_C and friends.
*/
#include <iprt/types.h>
#include <sys/param.h>
#undef PVM
#include <iprt/errcore.h>
#include <iprt/alloc.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/sysctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_types.h>
#include <net/route.h>
#include <ifaddrs.h>
#include <errno.h>
#include <unistd.h>
#include <list>
#include "HostNetworkInterfaceImpl.h"
#include "netif.h"
#include "iokit.h"
#include "LoggingNew.h"
#if 0
int NetIfList(std::list <ComObjPtr<HostNetworkInterface> > &list)
{
int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
if (sock < 0)
{
Log(("NetIfList: socket() -> %d\n", errno));
return NULL;
}
struct ifaddrs *IfAddrs, *pAddr;
int rc = getifaddrs(&IfAddrs);
if (rc)
{
close(sock);
Log(("NetIfList: getifaddrs() -> %d\n", rc));
return VERR_INTERNAL_ERROR;
}
PDARWINETHERNIC pEtherNICs = DarwinGetEthernetControllers();
while (pEtherNICs)
{
size_t cbNameLen = strlen(pEtherNICs->szName) + 1;
PNETIFINFO pNew = (PNETIFINFO)RTMemAllocZ(RT_OFFSETOF(NETIFINFO, szName[cbNameLen]));
pNew->MACAddress = pEtherNICs->Mac;
pNew->enmMediumType = NETIF_T_ETHERNET;
pNew->Uuid = pEtherNICs->Uuid;
Assert(sizeof(pNew->szShortName) > sizeof(pEtherNICs->szBSDName));
memcpy(pNew->szShortName, pEtherNICs->szBSDName, sizeof(pEtherNICs->szBSDName));
pNew->szShortName[sizeof(pEtherNICs->szBSDName)] = '\0';
memcpy(pNew->szName, pEtherNICs->szName, cbNameLen);
struct ifreq IfReq;
RTStrCopy(IfReq.ifr_name, sizeof(IfReq.ifr_name), pNew->szShortName);
if (ioctl(sock, SIOCGIFFLAGS, &IfReq) < 0)
{
Log(("NetIfList: ioctl(SIOCGIFFLAGS) -> %d\n", errno));
pNew->enmStatus = NETIF_S_UNKNOWN;
}
else
pNew->enmStatus = (IfReq.ifr_flags & IFF_UP) ? NETIF_S_UP : NETIF_S_DOWN;
for (pAddr = IfAddrs; pAddr != NULL; pAddr = pAddr->ifa_next)
{
if (strcmp(pNew->szShortName, pAddr->ifa_name))
continue;
struct sockaddr_in *pIPAddr, *pIPNetMask;
struct sockaddr_in6 *pIPv6Addr, *pIPv6NetMask;
switch (pAddr->ifa_addr->sa_family)
{
case AF_INET:
if (pNew->IPAddress.u)
break;
pIPAddr = (struct sockaddr_in *)pAddr->ifa_addr;
Assert(sizeof(pNew->IPAddress) == sizeof(pIPAddr->sin_addr));
pNew->IPAddress.u = pIPAddr->sin_addr.s_addr;
pIPNetMask = (struct sockaddr_in *)pAddr->ifa_netmask;
Assert(pIPNetMask->sin_family == AF_INET);
Assert(sizeof(pNew->IPNetMask) == sizeof(pIPNetMask->sin_addr));
pNew->IPNetMask.u = pIPNetMask->sin_addr.s_addr;
break;
case AF_INET6:
if (pNew->IPv6Address.s.Lo || pNew->IPv6Address.s.Hi)
break;
pIPv6Addr = (struct sockaddr_in6 *)pAddr->ifa_addr;
Assert(sizeof(pNew->IPv6Address) == sizeof(pIPv6Addr->sin6_addr));
memcpy(pNew->IPv6Address.au8,
pIPv6Addr->sin6_addr.__u6_addr.__u6_addr8,
sizeof(pNew->IPv6Address));
pIPv6NetMask = (struct sockaddr_in6 *)pAddr->ifa_netmask;
Assert(pIPv6NetMask->sin6_family == AF_INET6);
Assert(sizeof(pNew->IPv6NetMask) == sizeof(pIPv6NetMask->sin6_addr));
memcpy(pNew->IPv6NetMask.au8,
pIPv6NetMask->sin6_addr.__u6_addr.__u6_addr8,
sizeof(pNew->IPv6NetMask));
break;
}
}
ComObjPtr<HostNetworkInterface> IfObj;
IfObj.createObject();
if (SUCCEEDED(IfObj->init(Bstr(pEtherNICs->szName), HostNetworkInterfaceType_Bridged, pNew)))
list.push_back(IfObj);
RTMemFree(pNew);
/* next, free current */
void *pvFree = pEtherNICs;
pEtherNICs = pEtherNICs->pNext;
RTMemFree(pvFree);
}
freeifaddrs(IfAddrs);
close(sock);
return VINF_SUCCESS;
}
#else
#define ROUNDUP(a) \
(((a) & (sizeof(u_long) - 1)) ? (1 + ((a) | (sizeof(u_long) - 1))) : (a))
#define ADVANCE(x, n) (x += (n)->sa_len ? ROUNDUP((n)->sa_len) : sizeof(u_long))
void extractAddresses(int iAddrMask, caddr_t cp, caddr_t cplim, struct sockaddr **pAddresses)
{
struct sockaddr *sa;
for (int i = 0; i < RTAX_MAX && cp < cplim; i++) {
if (iAddrMask & (1 << i))
{
sa = (struct sockaddr *)cp;
pAddresses[i] = sa;
ADVANCE(cp, sa);
}
else
pAddresses[i] = NULL;
}
}
void extractAddressesToNetInfo(int iAddrMask, caddr_t cp, caddr_t cplim, PNETIFINFO pInfo)
{
struct sockaddr *addresses[RTAX_MAX];
extractAddresses(iAddrMask, cp, cplim, addresses);
switch (addresses[RTAX_IFA]->sa_family)
{
case AF_INET:
if (!pInfo->IPAddress.u)
{
pInfo->IPAddress.u = ((struct sockaddr_in *)addresses[RTAX_IFA])->sin_addr.s_addr;
pInfo->IPNetMask.u = ((struct sockaddr_in *)addresses[RTAX_NETMASK])->sin_addr.s_addr;
}
break;
case AF_INET6:
if (!pInfo->IPv6Address.s.Lo && !pInfo->IPv6Address.s.Hi)
{
memcpy(pInfo->IPv6Address.au8,
((struct sockaddr_in6 *)addresses[RTAX_IFA])->sin6_addr.__u6_addr.__u6_addr8,
sizeof(pInfo->IPv6Address));
memcpy(pInfo->IPv6NetMask.au8,
((struct sockaddr_in6 *)addresses[RTAX_NETMASK])->sin6_addr.__u6_addr.__u6_addr8,
sizeof(pInfo->IPv6NetMask));
}
break;
default:
Log(("NetIfList: Unsupported address family: %u\n", addresses[RTAX_IFA]->sa_family));
break;
}
}
static int getDefaultIfaceIndex(unsigned short *pu16Index)
{
size_t cbNeeded;
char *pBuf, *pNext;
int aiMib[6];
struct sockaddr *addresses[RTAX_MAX];
aiMib[0] = CTL_NET;
aiMib[1] = PF_ROUTE;
aiMib[2] = 0;
aiMib[3] = PF_INET; /* address family */
aiMib[4] = NET_RT_DUMP;
aiMib[5] = 0;
if (sysctl(aiMib, 6, NULL, &cbNeeded, NULL, 0) < 0)
{
Log(("getDefaultIfaceIndex: Failed to get estimate for list size (errno=%d).\n", errno));
return RTErrConvertFromErrno(errno);
}
if ((pBuf = (char *)RTMemAlloc(cbNeeded)) == NULL)
return VERR_NO_MEMORY;
if (sysctl(aiMib, 6, pBuf, &cbNeeded, NULL, 0) < 0)
{
RTMemFree(pBuf);
Log(("getDefaultIfaceIndex: Failed to retrieve interface table (errno=%d).\n", errno));
return RTErrConvertFromErrno(errno);
}
char *pEnd = pBuf + cbNeeded;
struct rt_msghdr *pRtMsg;
for (pNext = pBuf; pNext < pEnd; pNext += pRtMsg->rtm_msglen)
{
pRtMsg = (struct rt_msghdr *)pNext;
if (pRtMsg->rtm_type != RTM_GET)
{
Log(("getDefaultIfaceIndex: Got message %u while expecting %u.\n",
pRtMsg->rtm_type, RTM_GET));
//rc = VERR_INTERNAL_ERROR;
continue;
}
if ((char*)(pRtMsg + 1) < pEnd)
{
/* Extract addresses from the message. */
extractAddresses(pRtMsg->rtm_addrs, (char *)(pRtMsg + 1),
pRtMsg->rtm_msglen + 1 + (char *)pRtMsg, addresses);
if ((pRtMsg->rtm_addrs & RTA_DST)
&& (pRtMsg->rtm_addrs & RTA_NETMASK))
{
if (addresses[RTAX_DST]->sa_family != AF_INET)
continue;
struct sockaddr_in *addr = (struct sockaddr_in *)addresses[RTAX_DST];
struct sockaddr_in *mask = (struct sockaddr_in *)addresses[RTAX_NETMASK];
if ((addr->sin_addr.s_addr == INADDR_ANY) &&
mask &&
(ntohl(mask->sin_addr.s_addr) == 0L ||
mask->sin_len == 0))
{
*pu16Index = pRtMsg->rtm_index;
RTMemFree(pBuf);
return VINF_SUCCESS;
}
}
}
}
RTMemFree(pBuf);
return 0; /* Failed to find default interface, take the first one in the list. */
}
int NetIfList(std::list <ComObjPtr<HostNetworkInterface> > &list)
{
int rc = VINF_SUCCESS;
size_t cbNeeded;
char *pBuf, *pNext;
int aiMib[6];
unsigned short u16DefaultIface = 0; /* initialized to shut up gcc */
/* Get the index of the interface associated with default route. */
rc = getDefaultIfaceIndex(&u16DefaultIface);
if (RT_FAILURE(rc))
return rc;
aiMib[0] = CTL_NET;
aiMib[1] = PF_ROUTE;
aiMib[2] = 0;
aiMib[3] = 0; /* address family */
aiMib[4] = NET_RT_IFLIST;
aiMib[5] = 0;
if (sysctl(aiMib, 6, NULL, &cbNeeded, NULL, 0) < 0)
{
Log(("NetIfList: Failed to get estimate for list size (errno=%d).\n", errno));
return RTErrConvertFromErrno(errno);
}
if ((pBuf = (char*)RTMemAlloc(cbNeeded)) == NULL)
return VERR_NO_MEMORY;
if (sysctl(aiMib, 6, pBuf, &cbNeeded, NULL, 0) < 0)
{
RTMemFree(pBuf);
Log(("NetIfList: Failed to retrieve interface table (errno=%d).\n", errno));
return RTErrConvertFromErrno(errno);
}
int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
if (sock < 0)
{
RTMemFree(pBuf);
Log(("NetIfList: socket() -> %d\n", errno));
return RTErrConvertFromErrno(errno);
}
PDARWINETHERNIC pNIC;
PDARWINETHERNIC pEtherNICs = DarwinGetEthernetControllers();
char *pEnd = pBuf + cbNeeded;
for (pNext = pBuf; pNext < pEnd;)
{
struct if_msghdr *pIfMsg = (struct if_msghdr *)pNext;
if (pIfMsg->ifm_type != RTM_IFINFO)
{
Log(("NetIfList: Got message %u while expecting %u.\n",
pIfMsg->ifm_type, RTM_IFINFO));
rc = VERR_INTERNAL_ERROR;
break;
}
struct sockaddr_dl *pSdl = (struct sockaddr_dl *)(pIfMsg + 1);
size_t cbNameLen = pSdl->sdl_nlen + 1;
Assert(pSdl->sdl_nlen < sizeof(pNIC->szBSDName));
for (pNIC = pEtherNICs; pNIC; pNIC = pNIC->pNext)
if ( !strncmp(pSdl->sdl_data, pNIC->szBSDName, pSdl->sdl_nlen)
&& pNIC->szBSDName[pSdl->sdl_nlen] == '\0')
{
cbNameLen = strlen(pNIC->szName) + 1;
break;
}
PNETIFINFO pNew = (PNETIFINFO)RTMemAllocZ(RT_UOFFSETOF_DYN(NETIFINFO, szName[cbNameLen]));
if (!pNew)
{
rc = VERR_NO_MEMORY;
break;
}
memcpy(pNew->MACAddress.au8, LLADDR(pSdl), sizeof(pNew->MACAddress.au8));
pNew->enmMediumType = NETIF_T_ETHERNET;
Assert(sizeof(pNew->szShortName) > pSdl->sdl_nlen);
memcpy(pNew->szShortName, pSdl->sdl_data, RT_MIN(pSdl->sdl_nlen, sizeof(pNew->szShortName) - 1));
/*
* If we found the adapter in the list returned by
* DarwinGetEthernetControllers() copy the name and UUID from there.
*/
if (pNIC)
{
memcpy(pNew->szName, pNIC->szName, cbNameLen);
pNew->Uuid = pNIC->Uuid;
pNew->fWireless = pNIC->fWireless;
}
else
{
memcpy(pNew->szName, pSdl->sdl_data, pSdl->sdl_nlen);
/* Generate UUID from name and MAC address. */
RTUUID uuid;
RTUuidClear(&uuid);
memcpy(&uuid, pNew->szShortName, RT_MIN(cbNameLen, sizeof(uuid)));
uuid.Gen.u8ClockSeqHiAndReserved = (uuid.Gen.u8ClockSeqHiAndReserved & 0x3f) | 0x80;
uuid.Gen.u16TimeHiAndVersion = (uuid.Gen.u16TimeHiAndVersion & 0x0fff) | 0x4000;
memcpy(uuid.Gen.au8Node, pNew->MACAddress.au8, sizeof(uuid.Gen.au8Node));
pNew->Uuid = uuid;
}
pNext += pIfMsg->ifm_msglen;
while (pNext < pEnd)
{
struct ifa_msghdr *pIfAddrMsg = (struct ifa_msghdr *)pNext;
if (pIfAddrMsg->ifam_type != RTM_NEWADDR)
break;
extractAddressesToNetInfo(pIfAddrMsg->ifam_addrs,
(char *)(pIfAddrMsg + 1),
pIfAddrMsg->ifam_msglen + (char *)pIfAddrMsg,
pNew);
pNext += pIfAddrMsg->ifam_msglen;
}
if (pSdl->sdl_type == IFT_ETHER)
{
struct ifreq IfReq;
RTStrCopy(IfReq.ifr_name, sizeof(IfReq.ifr_name), pNew->szShortName);
if (ioctl(sock, SIOCGIFFLAGS, &IfReq) < 0)
{
Log(("NetIfList: ioctl(SIOCGIFFLAGS) -> %d\n", errno));
pNew->enmStatus = NETIF_S_UNKNOWN;
}
else
pNew->enmStatus = (IfReq.ifr_flags & IFF_UP) ? NETIF_S_UP : NETIF_S_DOWN;
HostNetworkInterfaceType_T enmType;
if (strncmp(pNew->szName, RT_STR_TUPLE("vboxnet")))
enmType = HostNetworkInterfaceType_Bridged;
else
enmType = HostNetworkInterfaceType_HostOnly;
ComObjPtr<HostNetworkInterface> IfObj;
IfObj.createObject();
if (SUCCEEDED(IfObj->init(Bstr(pNew->szName), enmType, pNew)))
{
/* Make sure the default interface gets to the beginning. */
if (pIfMsg->ifm_index == u16DefaultIface)
list.push_front(IfObj);
else
list.push_back(IfObj);
}
}
RTMemFree(pNew);
}
for (pNIC = pEtherNICs; pNIC;)
{
void *pvFree = pNIC;
pNIC = pNIC->pNext;
RTMemFree(pvFree);
}
close(sock);
RTMemFree(pBuf);
return rc;
}
int NetIfGetConfigByName(PNETIFINFO pInfo)
{
int rc = VINF_SUCCESS;
size_t cbNeeded;
char *pBuf, *pNext;
int aiMib[6];
aiMib[0] = CTL_NET;
aiMib[1] = PF_ROUTE;
aiMib[2] = 0;
aiMib[3] = 0; /* address family */
aiMib[4] = NET_RT_IFLIST;
aiMib[5] = 0;
if (sysctl(aiMib, 6, NULL, &cbNeeded, NULL, 0) < 0)
{
Log(("NetIfList: Failed to get estimate for list size (errno=%d).\n", errno));
return RTErrConvertFromErrno(errno);
}
if ((pBuf = (char*)RTMemAlloc(cbNeeded)) == NULL)
return VERR_NO_MEMORY;
if (sysctl(aiMib, 6, pBuf, &cbNeeded, NULL, 0) < 0)
{
RTMemFree(pBuf);
Log(("NetIfList: Failed to retrieve interface table (errno=%d).\n", errno));
return RTErrConvertFromErrno(errno);
}
int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
if (sock < 0)
{
RTMemFree(pBuf);
Log(("NetIfList: socket() -> %d\n", errno));
return RTErrConvertFromErrno(errno);
}
char *pEnd = pBuf + cbNeeded;
for (pNext = pBuf; pNext < pEnd;)
{
struct if_msghdr *pIfMsg = (struct if_msghdr *)pNext;
if (pIfMsg->ifm_type != RTM_IFINFO)
{
Log(("NetIfList: Got message %u while expecting %u.\n",
pIfMsg->ifm_type, RTM_IFINFO));
rc = VERR_INTERNAL_ERROR;
break;
}
struct sockaddr_dl *pSdl = (struct sockaddr_dl *)(pIfMsg + 1);
bool fSkip = !!strncmp(pInfo->szShortName, pSdl->sdl_data, pSdl->sdl_nlen)
|| pInfo->szShortName[pSdl->sdl_nlen] != '\0';
pNext += pIfMsg->ifm_msglen;
while (pNext < pEnd)
{
struct ifa_msghdr *pIfAddrMsg = (struct ifa_msghdr *)pNext;
if (pIfAddrMsg->ifam_type != RTM_NEWADDR)
break;
if (!fSkip)
extractAddressesToNetInfo(pIfAddrMsg->ifam_addrs,
(char *)(pIfAddrMsg + 1),
pIfAddrMsg->ifam_msglen + (char *)pIfAddrMsg,
pInfo);
pNext += pIfAddrMsg->ifam_msglen;
}
if (!fSkip && pSdl->sdl_type == IFT_ETHER)
{
size_t cbNameLen = pSdl->sdl_nlen + 1;
memcpy(pInfo->MACAddress.au8, LLADDR(pSdl), sizeof(pInfo->MACAddress.au8));
pInfo->enmMediumType = NETIF_T_ETHERNET;
/* Generate UUID from name and MAC address. */
RTUUID uuid;
RTUuidClear(&uuid);
memcpy(&uuid, pInfo->szShortName, RT_MIN(cbNameLen, sizeof(uuid)));
uuid.Gen.u8ClockSeqHiAndReserved = (uuid.Gen.u8ClockSeqHiAndReserved & 0x3f) | 0x80;
uuid.Gen.u16TimeHiAndVersion = (uuid.Gen.u16TimeHiAndVersion & 0x0fff) | 0x4000;
memcpy(uuid.Gen.au8Node, pInfo->MACAddress.au8, sizeof(uuid.Gen.au8Node));
pInfo->Uuid = uuid;
struct ifreq IfReq;
RTStrCopy(IfReq.ifr_name, sizeof(IfReq.ifr_name), pInfo->szShortName);
if (ioctl(sock, SIOCGIFFLAGS, &IfReq) < 0)
{
Log(("NetIfList: ioctl(SIOCGIFFLAGS) -> %d\n", errno));
pInfo->enmStatus = NETIF_S_UNKNOWN;
}
else
pInfo->enmStatus = (IfReq.ifr_flags & IFF_UP) ? NETIF_S_UP : NETIF_S_DOWN;
return VINF_SUCCESS;
}
}
close(sock);
RTMemFree(pBuf);
return rc;
}
/**
* Retrieve the physical link speed in megabits per second. If the interface is
* not up or otherwise unavailable the zero speed is returned.
*
* @returns VBox status code.
*
* @param pcszIfName Interface name.
* @param puMbits Where to store the link speed.
*/
int NetIfGetLinkSpeed(const char *pcszIfName, uint32_t *puMbits)
{
RT_NOREF(pcszIfName, puMbits);
return VERR_NOT_IMPLEMENTED;
}
#endif
|