VirtualBox

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

Last change on this file since 22875 was 22875, checked in by vboxsync, 15 years ago

FreeBSD: Add support for bridged and hostonly networking. Contributed by Fredrik Lindberg

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