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