VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/generic/NetIf-generic.cpp@ 98288

Last change on this file since 98288 was 98288, checked in by vboxsync, 23 months ago

Main/src-server: rc -> hrc/vrc (partial). bugref:10223

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