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