1 | /* $Id: NetIf-generic.cpp 47155 2013-07-15 11:33:55Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox Main - Generic NetIf implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-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 | #include <VBox/err.h>
|
---|
19 | #include <VBox/log.h>
|
---|
20 | #include <iprt/process.h>
|
---|
21 | #include <iprt/env.h>
|
---|
22 | #include <iprt/path.h>
|
---|
23 | #include <iprt/param.h>
|
---|
24 | #include <sys/ioctl.h>
|
---|
25 | #include <netinet/in.h>
|
---|
26 | #include <net/if.h>
|
---|
27 | #include <errno.h>
|
---|
28 | #include <unistd.h>
|
---|
29 |
|
---|
30 | #if defined(RT_OS_SOLARIS)
|
---|
31 | # include <sys/sockio.h>
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | #if defined(RT_OS_LINUX) || defined(RT_OS_DARWIN)
|
---|
35 | # include <cstdio>
|
---|
36 | #endif
|
---|
37 |
|
---|
38 | #include "HostNetworkInterfaceImpl.h"
|
---|
39 | #include "ProgressImpl.h"
|
---|
40 | #include "VirtualBoxImpl.h"
|
---|
41 | #include "netif.h"
|
---|
42 |
|
---|
43 | #define VBOXNETADPCTL_NAME "VBoxNetAdpCtl"
|
---|
44 |
|
---|
45 | static int NetIfAdpCtl(const char * pcszIfName, const char *pszAddr, const char *pszOption, const char *pszMask)
|
---|
46 | {
|
---|
47 | const char *args[] = { NULL, pcszIfName, pszAddr, pszOption, pszMask, NULL };
|
---|
48 |
|
---|
49 | char szAdpCtl[RTPATH_MAX];
|
---|
50 | int rc = RTPathExecDir(szAdpCtl, sizeof(szAdpCtl) - sizeof("/" VBOXNETADPCTL_NAME));
|
---|
51 | if (RT_FAILURE(rc))
|
---|
52 | {
|
---|
53 | LogRel(("NetIfAdpCtl: failed to get program path, rc=%Rrc.\n", rc));
|
---|
54 | return rc;
|
---|
55 | }
|
---|
56 | strcat(szAdpCtl, "/" VBOXNETADPCTL_NAME);
|
---|
57 | args[0] = szAdpCtl;
|
---|
58 | if (!RTPathExists(szAdpCtl))
|
---|
59 | {
|
---|
60 | LogRel(("NetIfAdpCtl: path %s does not exist. Failed to run " VBOXNETADPCTL_NAME " helper.\n",
|
---|
61 | szAdpCtl));
|
---|
62 | return VERR_FILE_NOT_FOUND;
|
---|
63 | }
|
---|
64 |
|
---|
65 | RTPROCESS pid;
|
---|
66 | rc = RTProcCreate(szAdpCtl, args, RTENV_DEFAULT, 0, &pid);
|
---|
67 | if (RT_SUCCESS(rc))
|
---|
68 | {
|
---|
69 | RTPROCSTATUS Status;
|
---|
70 | rc = RTProcWait(pid, 0, &Status);
|
---|
71 | if ( RT_SUCCESS(rc)
|
---|
72 | && Status.iStatus == 0
|
---|
73 | && Status.enmReason == RTPROCEXITREASON_NORMAL)
|
---|
74 | return VINF_SUCCESS;
|
---|
75 | }
|
---|
76 | else
|
---|
77 | LogRel(("NetIfAdpCtl: failed to create process for %.\n",
|
---|
78 | szAdpCtl));
|
---|
79 | return rc;
|
---|
80 | }
|
---|
81 |
|
---|
82 | static int NetIfAdpCtl(HostNetworkInterface * pIf, const char *pszAddr, const char *pszOption, const char *pszMask)
|
---|
83 | {
|
---|
84 | Bstr interfaceName;
|
---|
85 | pIf->COMGETTER(Name)(interfaceName.asOutParam());
|
---|
86 | Utf8Str strName(interfaceName);
|
---|
87 | return NetIfAdpCtl(strName.c_str(), pszAddr, pszOption, pszMask);
|
---|
88 | }
|
---|
89 |
|
---|
90 | int NetIfAdpCtlOut(const char * pcszName, const char * pcszCmd, char *pszBuffer, size_t cBufSize)
|
---|
91 | {
|
---|
92 | char szAdpCtl[RTPATH_MAX];
|
---|
93 | int rc = RTPathExecDir(szAdpCtl, sizeof(szAdpCtl) - sizeof("/" VBOXNETADPCTL_NAME " ") - strlen(pcszCmd));
|
---|
94 | if (RT_FAILURE(rc))
|
---|
95 | {
|
---|
96 | LogRel(("NetIfAdpCtlOut: Failed to get program path, rc=%Rrc\n", rc));
|
---|
97 | return VERR_INVALID_PARAMETER;
|
---|
98 | }
|
---|
99 | strcat(szAdpCtl, "/" VBOXNETADPCTL_NAME " ");
|
---|
100 | if (pcszName && strlen(pcszName) <= RTPATH_MAX - strlen(szAdpCtl) - 1 - strlen(pcszCmd))
|
---|
101 | {
|
---|
102 | strcat(szAdpCtl, pcszName);
|
---|
103 | strcat(szAdpCtl, " ");
|
---|
104 | strcat(szAdpCtl, pcszCmd);
|
---|
105 | }
|
---|
106 | else
|
---|
107 | {
|
---|
108 | LogRel(("NetIfAdpCtlOut: Command line is too long: %s%s %s\n", szAdpCtl, pcszName, pcszCmd));
|
---|
109 | return VERR_INVALID_PARAMETER;
|
---|
110 | }
|
---|
111 | if (strlen(szAdpCtl) < RTPATH_MAX - sizeof(" 2>&1"))
|
---|
112 | strcat(szAdpCtl, " 2>&1");
|
---|
113 | FILE *fp = popen(szAdpCtl, "r");
|
---|
114 | if (fp)
|
---|
115 | {
|
---|
116 | if (fgets(pszBuffer, cBufSize, fp))
|
---|
117 | {
|
---|
118 | if (!strncmp(VBOXNETADPCTL_NAME ":", pszBuffer, sizeof(VBOXNETADPCTL_NAME)))
|
---|
119 | {
|
---|
120 | LogRel(("NetIfAdpCtlOut: %s", pszBuffer));
|
---|
121 | rc = VERR_INTERNAL_ERROR;
|
---|
122 | }
|
---|
123 | }
|
---|
124 | else
|
---|
125 | {
|
---|
126 | LogRel(("NetIfAdpCtlOut: No output from " VBOXNETADPCTL_NAME));
|
---|
127 | rc = VERR_INTERNAL_ERROR;
|
---|
128 | }
|
---|
129 | pclose(fp);
|
---|
130 | }
|
---|
131 | return rc;
|
---|
132 | }
|
---|
133 |
|
---|
134 | int NetIfEnableStaticIpConfig(VirtualBox * /* vBox */, HostNetworkInterface * pIf, ULONG aOldIp, ULONG aNewIp, ULONG aMask)
|
---|
135 | {
|
---|
136 | const char *pszOption, *pszMask;
|
---|
137 | char szAddress[16]; /* 4*3 + 3*1 + 1 */
|
---|
138 | char szNetMask[16]; /* 4*3 + 3*1 + 1 */
|
---|
139 | uint8_t *pu8Addr = (uint8_t *)&aNewIp;
|
---|
140 | uint8_t *pu8Mask = (uint8_t *)&aMask;
|
---|
141 | if (aNewIp == 0)
|
---|
142 | {
|
---|
143 | pu8Addr = (uint8_t *)&aOldIp;
|
---|
144 | pszOption = "remove";
|
---|
145 | pszMask = NULL;
|
---|
146 | }
|
---|
147 | else
|
---|
148 | {
|
---|
149 | pszOption = "netmask";
|
---|
150 | pszMask = szNetMask;
|
---|
151 | RTStrPrintf(szNetMask, sizeof(szNetMask), "%d.%d.%d.%d",
|
---|
152 | pu8Mask[0], pu8Mask[1], pu8Mask[2], pu8Mask[3]);
|
---|
153 | }
|
---|
154 | RTStrPrintf(szAddress, sizeof(szAddress), "%d.%d.%d.%d",
|
---|
155 | pu8Addr[0], pu8Addr[1], pu8Addr[2], pu8Addr[3]);
|
---|
156 | return NetIfAdpCtl(pIf, szAddress, pszOption, pszMask);
|
---|
157 | }
|
---|
158 |
|
---|
159 | int NetIfEnableStaticIpConfigV6(VirtualBox * /* vBox */, HostNetworkInterface * pIf, IN_BSTR aOldIPV6Address, IN_BSTR aIPV6Address, ULONG aIPV6MaskPrefixLength)
|
---|
160 | {
|
---|
161 | char szAddress[5*8 + 1 + 5 + 1];
|
---|
162 | if (Bstr(aIPV6Address).length())
|
---|
163 | {
|
---|
164 | RTStrPrintf(szAddress, sizeof(szAddress), "%ls/%d",
|
---|
165 | aIPV6Address, aIPV6MaskPrefixLength);
|
---|
166 | return NetIfAdpCtl(pIf, szAddress, NULL, NULL);
|
---|
167 | }
|
---|
168 | else
|
---|
169 | {
|
---|
170 | RTStrPrintf(szAddress, sizeof(szAddress), "%ls",
|
---|
171 | aOldIPV6Address);
|
---|
172 | return NetIfAdpCtl(pIf, szAddress, "remove", NULL);
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | int NetIfEnableDynamicIpConfig(VirtualBox * /* vBox */, HostNetworkInterface * /* pIf */)
|
---|
177 | {
|
---|
178 | return VERR_NOT_IMPLEMENTED;
|
---|
179 | }
|
---|
180 |
|
---|
181 |
|
---|
182 | int NetIfCreateHostOnlyNetworkInterface(VirtualBox *pVBox,
|
---|
183 | IHostNetworkInterface **aHostNetworkInterface,
|
---|
184 | IProgress **aProgress,
|
---|
185 | const char *pcszName)
|
---|
186 | {
|
---|
187 | #if defined(RT_OS_LINUX) || defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD)
|
---|
188 | /* create a progress object */
|
---|
189 | ComObjPtr<Progress> progress;
|
---|
190 | progress.createObject();
|
---|
191 |
|
---|
192 | ComPtr<IHost> host;
|
---|
193 | HRESULT hrc = pVBox->COMGETTER(Host)(host.asOutParam());
|
---|
194 | if (SUCCEEDED(hrc))
|
---|
195 | {
|
---|
196 | hrc = progress->init(pVBox, host,
|
---|
197 | Bstr("Creating host only network interface").raw(),
|
---|
198 | FALSE /* aCancelable */);
|
---|
199 | if (SUCCEEDED(hrc))
|
---|
200 | {
|
---|
201 | progress.queryInterfaceTo(aProgress);
|
---|
202 |
|
---|
203 | char szAdpCtl[RTPATH_MAX];
|
---|
204 | int rc = RTPathExecDir(szAdpCtl, sizeof(szAdpCtl) - sizeof("/" VBOXNETADPCTL_NAME " add"));
|
---|
205 | if (RT_FAILURE(rc))
|
---|
206 | {
|
---|
207 | progress->notifyComplete(E_FAIL,
|
---|
208 | COM_IIDOF(IHostNetworkInterface),
|
---|
209 | HostNetworkInterface::getStaticComponentName(),
|
---|
210 | "Failed to get program path, rc=%Rrc\n", rc);
|
---|
211 | return rc;
|
---|
212 | }
|
---|
213 | strcat(szAdpCtl, "/" VBOXNETADPCTL_NAME " ");
|
---|
214 | if (pcszName && strlen(pcszName) <= RTPATH_MAX - strlen(szAdpCtl) - sizeof(" add"))
|
---|
215 | {
|
---|
216 | strcat(szAdpCtl, pcszName);
|
---|
217 | strcat(szAdpCtl, " add");
|
---|
218 | }
|
---|
219 | else
|
---|
220 | strcat(szAdpCtl, "add");
|
---|
221 | if (strlen(szAdpCtl) < RTPATH_MAX - sizeof(" 2>&1"))
|
---|
222 | strcat(szAdpCtl, " 2>&1");
|
---|
223 | FILE *fp = popen(szAdpCtl, "r");
|
---|
224 |
|
---|
225 | if (fp)
|
---|
226 | {
|
---|
227 | char szBuf[128]; /* We are not interested in long error messages. */
|
---|
228 | if (fgets(szBuf, sizeof(szBuf), fp))
|
---|
229 | {
|
---|
230 | /* Remove trailing new line characters. */
|
---|
231 | char *pLast = szBuf + strlen(szBuf) - 1;
|
---|
232 | if (pLast >= szBuf && *pLast == '\n')
|
---|
233 | *pLast = 0;
|
---|
234 |
|
---|
235 | if (!strncmp(VBOXNETADPCTL_NAME ":", szBuf, sizeof(VBOXNETADPCTL_NAME)))
|
---|
236 | {
|
---|
237 | progress->notifyComplete(E_FAIL,
|
---|
238 | COM_IIDOF(IHostNetworkInterface),
|
---|
239 | HostNetworkInterface::getStaticComponentName(),
|
---|
240 | "%s", szBuf);
|
---|
241 | pclose(fp);
|
---|
242 | return E_FAIL;
|
---|
243 | }
|
---|
244 |
|
---|
245 | size_t cbNameLen = strlen(szBuf) + 1;
|
---|
246 | PNETIFINFO pInfo = (PNETIFINFO)RTMemAllocZ(RT_OFFSETOF(NETIFINFO, szName[cbNameLen]));
|
---|
247 | if (!pInfo)
|
---|
248 | rc = VERR_NO_MEMORY;
|
---|
249 | else
|
---|
250 | {
|
---|
251 | strcpy(pInfo->szShortName, szBuf);
|
---|
252 | strcpy(pInfo->szName, szBuf);
|
---|
253 | rc = NetIfGetConfigByName(pInfo);
|
---|
254 | if (RT_FAILURE(rc))
|
---|
255 | {
|
---|
256 | progress->notifyComplete(E_FAIL,
|
---|
257 | COM_IIDOF(IHostNetworkInterface),
|
---|
258 | HostNetworkInterface::getStaticComponentName(),
|
---|
259 | "Failed to get config info for %s (as reported by '" VBOXNETADPCTL_NAME " add')\n", szBuf);
|
---|
260 | }
|
---|
261 | else
|
---|
262 | {
|
---|
263 | Bstr IfName(szBuf);
|
---|
264 | /* create a new uninitialized host interface object */
|
---|
265 | ComObjPtr<HostNetworkInterface> iface;
|
---|
266 | iface.createObject();
|
---|
267 | iface->init(IfName, HostNetworkInterfaceType_HostOnly, pInfo);
|
---|
268 | iface->setVirtualBox(pVBox);
|
---|
269 | iface.queryInterfaceTo(aHostNetworkInterface);
|
---|
270 | }
|
---|
271 | RTMemFree(pInfo);
|
---|
272 | }
|
---|
273 | if ((rc = pclose(fp)) != 0)
|
---|
274 | {
|
---|
275 | progress->notifyComplete(E_FAIL,
|
---|
276 | COM_IIDOF(IHostNetworkInterface),
|
---|
277 | HostNetworkInterface::getStaticComponentName(),
|
---|
278 | "Failed to execute '"VBOXNETADPCTL_NAME " add' (exit status: %d)", rc);
|
---|
279 | rc = VERR_INTERNAL_ERROR;
|
---|
280 | }
|
---|
281 | }
|
---|
282 | else
|
---|
283 | {
|
---|
284 | /* Failed to add an interface */
|
---|
285 | rc = VERR_PERMISSION_DENIED;
|
---|
286 | progress->notifyComplete(E_FAIL,
|
---|
287 | COM_IIDOF(IHostNetworkInterface),
|
---|
288 | HostNetworkInterface::getStaticComponentName(),
|
---|
289 | "Failed to execute '"VBOXNETADPCTL_NAME " add' (exit status: %d). Check permissions!", rc);
|
---|
290 | pclose(fp);
|
---|
291 | }
|
---|
292 | }
|
---|
293 | if (RT_SUCCESS(rc))
|
---|
294 | progress->notifyComplete(rc);
|
---|
295 | else
|
---|
296 | hrc = E_FAIL;
|
---|
297 | }
|
---|
298 | }
|
---|
299 |
|
---|
300 | return hrc;
|
---|
301 |
|
---|
302 | #else
|
---|
303 | NOREF(pVBox);
|
---|
304 | NOREF(aHostNetworkInterface);
|
---|
305 | NOREF(aProgress);
|
---|
306 | NOREF(pcszName);
|
---|
307 | return VERR_NOT_IMPLEMENTED;
|
---|
308 | #endif
|
---|
309 | }
|
---|
310 |
|
---|
311 | int NetIfRemoveHostOnlyNetworkInterface(VirtualBox *pVBox, IN_GUID aId,
|
---|
312 | IProgress **aProgress)
|
---|
313 | {
|
---|
314 | #if defined(RT_OS_LINUX) || defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD)
|
---|
315 | /* create a progress object */
|
---|
316 | ComObjPtr<Progress> progress;
|
---|
317 | progress.createObject();
|
---|
318 | ComPtr<IHost> host;
|
---|
319 | int rc = VINF_SUCCESS;
|
---|
320 | HRESULT hr = pVBox->COMGETTER(Host)(host.asOutParam());
|
---|
321 | if (SUCCEEDED(hr))
|
---|
322 | {
|
---|
323 | Bstr ifname;
|
---|
324 | ComPtr<IHostNetworkInterface> iface;
|
---|
325 | if (FAILED(host->FindHostNetworkInterfaceById(Guid(aId).toUtf16().raw(), iface.asOutParam())))
|
---|
326 | return VERR_INVALID_PARAMETER;
|
---|
327 | iface->COMGETTER(Name)(ifname.asOutParam());
|
---|
328 | if (ifname.isEmpty())
|
---|
329 | return VERR_INTERNAL_ERROR;
|
---|
330 |
|
---|
331 | rc = progress->init(pVBox, host,
|
---|
332 | Bstr("Removing host network interface").raw(),
|
---|
333 | FALSE /* aCancelable */);
|
---|
334 | if (SUCCEEDED(rc))
|
---|
335 | {
|
---|
336 | progress.queryInterfaceTo(aProgress);
|
---|
337 | rc = NetIfAdpCtl(Utf8Str(ifname).c_str(), "remove", NULL, NULL);
|
---|
338 | if (RT_FAILURE(rc))
|
---|
339 | progress->notifyComplete(E_FAIL,
|
---|
340 | COM_IIDOF(IHostNetworkInterface),
|
---|
341 | HostNetworkInterface::getStaticComponentName(),
|
---|
342 | "Failed to execute '"VBOXNETADPCTL_NAME "' (exit status: %d)", rc);
|
---|
343 | else
|
---|
344 | progress->notifyComplete(S_OK);
|
---|
345 | }
|
---|
346 | }
|
---|
347 | else
|
---|
348 | {
|
---|
349 | progress->notifyComplete(hr);
|
---|
350 | rc = VERR_INTERNAL_ERROR;
|
---|
351 | }
|
---|
352 | return rc;
|
---|
353 | #else
|
---|
354 | NOREF(pVBox);
|
---|
355 | NOREF(aId);
|
---|
356 | NOREF(aProgress);
|
---|
357 | return VERR_NOT_IMPLEMENTED;
|
---|
358 | #endif
|
---|
359 | }
|
---|
360 |
|
---|
361 | int NetIfGetConfig(HostNetworkInterface * /* pIf */, NETIFINFO *)
|
---|
362 | {
|
---|
363 | return VERR_NOT_IMPLEMENTED;
|
---|
364 | }
|
---|
365 |
|
---|
366 | int NetIfDhcpRediscover(VirtualBox * /* pVbox */, HostNetworkInterface * /* pIf */)
|
---|
367 | {
|
---|
368 | return VERR_NOT_IMPLEMENTED;
|
---|
369 | }
|
---|
370 |
|
---|
371 | /**
|
---|
372 | * Obtain the current state of the interface.
|
---|
373 | *
|
---|
374 | * @returns VBox status code.
|
---|
375 | *
|
---|
376 | * @param pcszIfName Interface name.
|
---|
377 | * @param penmState Where to store the retrieved state.
|
---|
378 | */
|
---|
379 | int NetIfGetState(const char *pcszIfName, NETIFSTATUS *penmState)
|
---|
380 | {
|
---|
381 | int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
|
---|
382 | if (sock < 0)
|
---|
383 | return VERR_OUT_OF_RESOURCES;
|
---|
384 | struct ifreq Req;
|
---|
385 | RT_ZERO(Req);
|
---|
386 | RTStrCopy(Req.ifr_name, sizeof(Req.ifr_name), pcszIfName);
|
---|
387 | if (ioctl(sock, SIOCGIFFLAGS, &Req) < 0)
|
---|
388 | {
|
---|
389 | Log(("NetIfGetState: ioctl(SIOCGIFFLAGS) -> %d\n", errno));
|
---|
390 | *penmState = NETIF_S_UNKNOWN;
|
---|
391 | }
|
---|
392 | else
|
---|
393 | *penmState = (Req.ifr_flags & IFF_UP) ? NETIF_S_UP : NETIF_S_DOWN;
|
---|
394 | close(sock);
|
---|
395 | return VINF_SUCCESS;
|
---|
396 | }
|
---|