VirtualBox

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

Last change on this file since 55095 was 54959, checked in by vboxsync, 10 years ago

NetIf-freebsd: Added { } to prevent a compiler warning. Thank you Jung-uk Kim.

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