VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/freebsd/NetIf-freebsd.cpp

Last change on this file was 106061, checked in by vboxsync, 3 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.9 KB
Line 
1/* $Id: NetIf-freebsd.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * Main - NetIfList, FreeBSD implementation.
4 */
5
6/*
7 * Copyright (C) 2008-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28/*
29 * Original (C) 2009 Fredrik Lindberg <fli@shapeshifter.se>. Contributed
30 * to VirtualBox under the MIT license by the author.
31 */
32
33
34/*********************************************************************************************************************************
35* Header Files *
36*********************************************************************************************************************************/
37#define LOG_GROUP LOG_GROUP_MAIN_HOST
38#include <sys/types.h>
39
40#include <sys/sysctl.h>
41#include <sys/socket.h>
42#include <sys/sockio.h>
43#include <net/if.h>
44#include <net/if_types.h>
45#include <net80211/ieee80211_ioctl.h>
46
47#include <net/route.h>
48/*
49 * route.h includes net/radix.h which for some reason defines Free as a wrapper
50 * around free. This collides with Free defined in xpcom/include/nsIMemory.h
51 * Undefine it and hope for the best
52 */
53#undef Free
54
55#include <net/if_dl.h>
56#include <netinet/in.h>
57
58#include <stdlib.h>
59#include <stdio.h>
60#include <unistd.h>
61#include <errno.h>
62
63#include <list>
64
65#include "HostNetworkInterfaceImpl.h"
66#include "netif.h"
67#include "LoggingNew.h"
68
69#define ROUNDUP(a) \
70 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
71#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
72
73void extractAddresses(int iAddrMask, caddr_t cp, caddr_t cplim, struct sockaddr **pAddresses)
74{
75 struct sockaddr *sa;
76
77 for (int i = 0; i < RTAX_MAX && cp < cplim; i++) {
78 if (!(iAddrMask & (1 << i)))
79 continue;
80
81 sa = (struct sockaddr *)cp;
82
83 pAddresses[i] = sa;
84
85 ADVANCE(cp, sa);
86 }
87}
88
89static int getDefaultIfaceIndex(unsigned short *pu16Index, int family)
90{
91 size_t cbNeeded;
92 char *pBuf, *pNext;
93 int aiMib[6];
94 struct sockaddr *addresses[RTAX_MAX];
95 aiMib[0] = CTL_NET;
96 aiMib[1] = PF_ROUTE;
97 aiMib[2] = 0;
98 aiMib[3] = family; /* address family */
99 aiMib[4] = NET_RT_DUMP;
100 aiMib[5] = 0;
101
102 if (sysctl(aiMib, 6, NULL, &cbNeeded, NULL, 0) < 0)
103 {
104 Log(("getDefaultIfaceIndex: Failed to get estimate for list size (errno=%d).\n", errno));
105 return RTErrConvertFromErrno(errno);
106 }
107 if ((pBuf = (char*)malloc(cbNeeded)) == NULL)
108 return VERR_NO_MEMORY;
109 if (sysctl(aiMib, 6, pBuf, &cbNeeded, NULL, 0) < 0)
110 {
111 free(pBuf);
112 Log(("getDefaultIfaceIndex: Failed to retrieve interface table (errno=%d).\n", errno));
113 return RTErrConvertFromErrno(errno);
114 }
115
116 char *pEnd = pBuf + cbNeeded;
117 struct rt_msghdr *pRtMsg;
118 for (pNext = pBuf; pNext < pEnd; pNext += pRtMsg->rtm_msglen)
119 {
120 pRtMsg = (struct rt_msghdr *)pNext;
121
122 if (pRtMsg->rtm_type != RTM_GET)
123 {
124 Log(("getDefaultIfaceIndex: Got message %u while expecting %u.\n",
125 pRtMsg->rtm_type, RTM_GET));
126 //vrc = VERR_INTERNAL_ERROR;
127 continue;
128 }
129 if ((char*)(pRtMsg + 1) < pEnd)
130 {
131 /* Extract addresses from the message. */
132 extractAddresses(pRtMsg->rtm_addrs, (char *)(pRtMsg + 1),
133 pRtMsg->rtm_msglen + (char *)pRtMsg, addresses);
134 if ((pRtMsg->rtm_addrs & RTA_DST))
135 {
136 if (addresses[RTAX_DST]->sa_family != AF_INET)
137 continue;
138 struct sockaddr_in *addr = (struct sockaddr_in *)addresses[RTAX_DST];
139 struct sockaddr_in *mask = (struct sockaddr_in *)addresses[RTAX_NETMASK];
140 if ((addr->sin_addr.s_addr == INADDR_ANY) &&
141 mask &&
142 (ntohl(mask->sin_addr.s_addr) == 0L ||
143 mask->sin_len == 0))
144 {
145 *pu16Index = pRtMsg->rtm_index;
146 free(pBuf);
147 return VINF_SUCCESS;
148 }
149 }
150 }
151 }
152 free(pBuf);
153 return VERR_INTERNAL_ERROR;
154
155}
156
157void extractAddressesToNetInfo(int iAddrMask, caddr_t cp, caddr_t cplim, PNETIFINFO pInfo)
158{
159 struct sockaddr *addresses[RTAX_MAX];
160
161 extractAddresses(iAddrMask, cp, cplim, addresses);
162 switch (addresses[RTAX_IFA]->sa_family)
163 {
164 case AF_INET:
165 if (!pInfo->IPAddress.u)
166 {
167 pInfo->IPAddress.u = ((struct sockaddr_in *)addresses[RTAX_IFA])->sin_addr.s_addr;
168 pInfo->IPNetMask.u = ((struct sockaddr_in *)addresses[RTAX_NETMASK])->sin_addr.s_addr;
169 }
170 break;
171 case AF_INET6:
172 if (!pInfo->IPv6Address.s.Lo && !pInfo->IPv6Address.s.Hi)
173 {
174 memcpy(pInfo->IPv6Address.au8,
175 ((struct sockaddr_in6 *)addresses[RTAX_IFA])->sin6_addr.__u6_addr.__u6_addr8,
176 sizeof(pInfo->IPv6Address));
177 memcpy(pInfo->IPv6NetMask.au8,
178 ((struct sockaddr_in6 *)addresses[RTAX_NETMASK])->sin6_addr.__u6_addr.__u6_addr8,
179 sizeof(pInfo->IPv6NetMask));
180 }
181 break;
182 default:
183 Log(("NetIfList: Unsupported address family: %u\n", addresses[RTAX_IFA]->sa_family));
184 break;
185 }
186}
187
188
189static bool isWireless(const char *pszName)
190{
191 bool fWireless = false;
192 int iSock = socket(AF_INET, SOCK_DGRAM, 0);
193 if (iSock >= 0)
194 {
195 struct ieee80211req WReq;
196 uint8_t abData[32];
197
198 RT_ZERO(WReq);
199 strncpy(WReq.i_name, pszName, sizeof(WReq.i_name));
200 WReq.i_type = IEEE80211_IOC_SSID;
201 WReq.i_val = -1;
202 WReq.i_data = abData;
203 WReq.i_len = sizeof(abData);
204
205 fWireless = ioctl(iSock, SIOCG80211, &WReq) >= 0;
206 close(iSock);
207 }
208
209 return fWireless;
210}
211
212int NetIfList(std::list <ComObjPtr<HostNetworkInterface> > &list)
213{
214 int vrc = VINF_SUCCESS;
215 size_t cbNeeded;
216 char *pBuf, *pNext;
217 int aiMib[6];
218 unsigned short u16DefaultIface = 0; /* shut up gcc. */
219 bool fDefaultIfaceExistent = true;
220
221 /* Get the index of the interface associated with default route. */
222 vrc = getDefaultIfaceIndex(&u16DefaultIface, PF_INET);
223 if (RT_FAILURE(vrc))
224 {
225 fDefaultIfaceExistent = false;
226 vrc = VINF_SUCCESS;
227 }
228
229 aiMib[0] = CTL_NET;
230 aiMib[1] = PF_ROUTE;
231 aiMib[2] = 0;
232 aiMib[3] = 0; /* address family */
233 aiMib[4] = NET_RT_IFLIST;
234 aiMib[5] = 0;
235
236 if (sysctl(aiMib, 6, NULL, &cbNeeded, NULL, 0) < 0)
237 {
238 Log(("NetIfList: Failed to get estimate for list size (errno=%d).\n", errno));
239 return RTErrConvertFromErrno(errno);
240 }
241 if ((pBuf = (char*)malloc(cbNeeded)) == NULL)
242 return VERR_NO_MEMORY;
243 if (sysctl(aiMib, 6, pBuf, &cbNeeded, NULL, 0) < 0)
244 {
245 free(pBuf);
246 Log(("NetIfList: Failed to retrieve interface table (errno=%d).\n", errno));
247 return RTErrConvertFromErrno(errno);
248 }
249
250 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
251 if (sock < 0)
252 {
253 free(pBuf);
254 Log(("NetIfList: socket() -> %d\n", errno));
255 return RTErrConvertFromErrno(errno);
256 }
257
258 char *pEnd = pBuf + cbNeeded;
259 for (pNext = pBuf; pNext < pEnd;)
260 {
261 struct if_msghdr *pIfMsg = (struct if_msghdr *)pNext;
262
263 if (pIfMsg->ifm_type != RTM_IFINFO)
264 {
265 Log(("NetIfList: Got message %u while expecting %u.\n",
266 pIfMsg->ifm_type, RTM_IFINFO));
267 vrc = VERR_INTERNAL_ERROR;
268 break;
269 }
270 struct sockaddr_dl *pSdl = (struct sockaddr_dl *)(pIfMsg + 1);
271
272 size_t cbNameLen = pSdl->sdl_nlen + 1;
273 PNETIFINFO pNew = (PNETIFINFO)RTMemAllocZ(RT_UOFFSETOF_DYN(NETIFINFO, szName[cbNameLen]));
274 if (!pNew)
275 {
276 vrc = VERR_NO_MEMORY;
277 break;
278 }
279 memcpy(pNew->MACAddress.au8, LLADDR(pSdl), sizeof(pNew->MACAddress.au8));
280 pNew->enmMediumType = NETIF_T_ETHERNET;
281 Assert(sizeof(pNew->szShortName) >= cbNameLen);
282 strlcpy(pNew->szShortName, pSdl->sdl_data, cbNameLen);
283 strlcpy(pNew->szName, pSdl->sdl_data, cbNameLen);
284 /* Generate UUID from name and MAC address. */
285 RTUUID uuid;
286 RTUuidClear(&uuid);
287 memcpy(&uuid, pNew->szShortName, RT_MIN(cbNameLen, sizeof(uuid)));
288 uuid.Gen.u8ClockSeqHiAndReserved = (uuid.Gen.u8ClockSeqHiAndReserved & 0x3f) | 0x80;
289 uuid.Gen.u16TimeHiAndVersion = (uuid.Gen.u16TimeHiAndVersion & 0x0fff) | 0x4000;
290 memcpy(uuid.Gen.au8Node, pNew->MACAddress.au8, sizeof(uuid.Gen.au8Node));
291 pNew->Uuid = uuid;
292
293 pNext += pIfMsg->ifm_msglen;
294 while (pNext < pEnd)
295 {
296 struct ifa_msghdr *pIfAddrMsg = (struct ifa_msghdr *)pNext;
297
298 if (pIfAddrMsg->ifam_type != RTM_NEWADDR)
299 break;
300 extractAddressesToNetInfo(pIfAddrMsg->ifam_addrs,
301 (char *)(pIfAddrMsg + 1),
302 pIfAddrMsg->ifam_msglen + (char *)pIfAddrMsg,
303 pNew);
304 pNext += pIfAddrMsg->ifam_msglen;
305 }
306
307 if (pSdl->sdl_type == IFT_ETHER || pSdl->sdl_type == IFT_L2VLAN)
308 {
309 struct ifreq IfReq;
310 RTStrCopy(IfReq.ifr_name, sizeof(IfReq.ifr_name), pNew->szShortName);
311 if (ioctl(sock, SIOCGIFFLAGS, &IfReq) < 0)
312 {
313 Log(("NetIfList: ioctl(SIOCGIFFLAGS) -> %d\n", errno));
314 pNew->enmStatus = NETIF_S_UNKNOWN;
315 }
316 else
317 pNew->enmStatus = (IfReq.ifr_flags & IFF_UP) ? NETIF_S_UP : NETIF_S_DOWN;
318
319 HostNetworkInterfaceType_T enmType;
320 if (strncmp(pNew->szName, RT_STR_TUPLE("vboxnet")))
321 enmType = HostNetworkInterfaceType_Bridged;
322 else
323 enmType = HostNetworkInterfaceType_HostOnly;
324
325 pNew->fWireless = isWireless(pNew->szName);
326
327 ComObjPtr<HostNetworkInterface> IfObj;
328 IfObj.createObject();
329 if (SUCCEEDED(IfObj->init(Bstr(pNew->szName), enmType, pNew)))
330 {
331 /* Make sure the default interface gets to the beginning. */
332 if ( fDefaultIfaceExistent
333 && pIfMsg->ifm_index == u16DefaultIface)
334 list.push_front(IfObj);
335 else
336 list.push_back(IfObj);
337 }
338 }
339 RTMemFree(pNew);
340 }
341
342 close(sock);
343 free(pBuf);
344 return vrc;
345}
346
347int NetIfGetConfigByName(PNETIFINFO pInfo)
348{
349 int vrc = VINF_SUCCESS;
350 size_t cbNeeded;
351 char *pBuf, *pNext;
352 int aiMib[6];
353
354 aiMib[0] = CTL_NET;
355 aiMib[1] = PF_ROUTE;
356 aiMib[2] = 0;
357 aiMib[3] = 0; /* address family */
358 aiMib[4] = NET_RT_IFLIST;
359 aiMib[5] = 0;
360
361 if (sysctl(aiMib, 6, NULL, &cbNeeded, NULL, 0) < 0)
362 {
363 Log(("NetIfList: Failed to get estimate for list size (errno=%d).\n", errno));
364 return RTErrConvertFromErrno(errno);
365 }
366 if ((pBuf = (char*)malloc(cbNeeded)) == NULL)
367 return VERR_NO_MEMORY;
368 if (sysctl(aiMib, 6, pBuf, &cbNeeded, NULL, 0) < 0)
369 {
370 free(pBuf);
371 Log(("NetIfList: Failed to retrieve interface table (errno=%d).\n", errno));
372 return RTErrConvertFromErrno(errno);
373 }
374
375 int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
376 if (sock < 0)
377 {
378 free(pBuf);
379 Log(("NetIfList: socket() -> %d\n", errno));
380 return RTErrConvertFromErrno(errno);
381 }
382
383 char *pEnd = pBuf + cbNeeded;
384 for (pNext = pBuf; pNext < pEnd;)
385 {
386 struct if_msghdr *pIfMsg = (struct if_msghdr *)pNext;
387
388 if (pIfMsg->ifm_type != RTM_IFINFO)
389 {
390 Log(("NetIfList: Got message %u while expecting %u.\n",
391 pIfMsg->ifm_type, RTM_IFINFO));
392 vrc = VERR_INTERNAL_ERROR;
393 break;
394 }
395 struct sockaddr_dl *pSdl = (struct sockaddr_dl *)(pIfMsg + 1);
396
397 bool fSkip = !!strcmp(pInfo->szShortName, pSdl->sdl_data);
398
399 pNext += pIfMsg->ifm_msglen;
400 while (pNext < pEnd)
401 {
402 struct ifa_msghdr *pIfAddrMsg = (struct ifa_msghdr *)pNext;
403
404 if (pIfAddrMsg->ifam_type != RTM_NEWADDR)
405 break;
406 if (!fSkip)
407 extractAddressesToNetInfo(pIfAddrMsg->ifam_addrs,
408 (char *)(pIfAddrMsg + 1),
409 pIfAddrMsg->ifam_msglen + (char *)pIfAddrMsg,
410 pInfo);
411 pNext += pIfAddrMsg->ifam_msglen;
412 }
413
414 if (!fSkip && (pSdl->sdl_type == IFT_ETHER || pSdl->sdl_type == IFT_L2VLAN))
415 {
416 size_t cbNameLen = pSdl->sdl_nlen + 1;
417 memcpy(pInfo->MACAddress.au8, LLADDR(pSdl), sizeof(pInfo->MACAddress.au8));
418 pInfo->enmMediumType = NETIF_T_ETHERNET;
419 /* Generate UUID from name and MAC address. */
420 RTUUID uuid;
421 RTUuidClear(&uuid);
422 memcpy(&uuid, pInfo->szShortName, RT_MIN(cbNameLen, sizeof(uuid)));
423 uuid.Gen.u8ClockSeqHiAndReserved = (uuid.Gen.u8ClockSeqHiAndReserved & 0x3f) | 0x80;
424 uuid.Gen.u16TimeHiAndVersion = (uuid.Gen.u16TimeHiAndVersion & 0x0fff) | 0x4000;
425 memcpy(uuid.Gen.au8Node, pInfo->MACAddress.au8, sizeof(uuid.Gen.au8Node));
426 pInfo->Uuid = uuid;
427
428 struct ifreq IfReq;
429 RTStrCopy(IfReq.ifr_name, sizeof(IfReq.ifr_name), pInfo->szShortName);
430 if (ioctl(sock, SIOCGIFFLAGS, &IfReq) < 0)
431 {
432 Log(("NetIfList: ioctl(SIOCGIFFLAGS) -> %d\n", errno));
433 pInfo->enmStatus = NETIF_S_UNKNOWN;
434 }
435 else
436 pInfo->enmStatus = (IfReq.ifr_flags & IFF_UP) ? NETIF_S_UP : NETIF_S_DOWN;
437
438 return VINF_SUCCESS;
439 }
440 }
441 close(sock);
442 free(pBuf);
443 return vrc;
444}
445
446/**
447 * Retrieve the physical link speed in megabits per second. If the interface is
448 * not up or otherwise unavailable the zero speed is returned.
449 *
450 * @returns VBox status code.
451 *
452 * @param pcszIfName Interface name.
453 * @param puMbits Where to store the link speed.
454 */
455int NetIfGetLinkSpeed(const char * /*pcszIfName*/, uint32_t * /*puMbits*/)
456{
457 return VERR_NOT_IMPLEMENTED;
458}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette