1 | /* $Id: DrvTAP.cpp 26305 2010-02-05 19:00:45Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * DrvTAP - Universial TAP network transport driver.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2010 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 | /*******************************************************************************
|
---|
23 | * Header Files *
|
---|
24 | *******************************************************************************/
|
---|
25 | #define LOG_GROUP LOG_GROUP_DRV_TUN
|
---|
26 | #include <VBox/log.h>
|
---|
27 | #include <VBox/pdmdrv.h>
|
---|
28 | #include <VBox/pdmnetifs.h>
|
---|
29 |
|
---|
30 | #include <iprt/asm.h>
|
---|
31 | #include <iprt/assert.h>
|
---|
32 | #include <iprt/ctype.h>
|
---|
33 | #include <iprt/file.h>
|
---|
34 | #include <iprt/path.h>
|
---|
35 | #include <iprt/semaphore.h>
|
---|
36 | #include <iprt/string.h>
|
---|
37 | #include <iprt/thread.h>
|
---|
38 | #include <iprt/uuid.h>
|
---|
39 | #ifdef RT_OS_SOLARIS
|
---|
40 | # include <iprt/process.h>
|
---|
41 | # include <iprt/env.h>
|
---|
42 | # ifdef VBOX_WITH_CROSSBOW
|
---|
43 | # include <iprt/mem.h>
|
---|
44 | # endif
|
---|
45 | #endif
|
---|
46 |
|
---|
47 | #include <sys/ioctl.h>
|
---|
48 | #include <sys/poll.h>
|
---|
49 | #ifdef RT_OS_SOLARIS
|
---|
50 | # include <sys/stat.h>
|
---|
51 | # include <sys/ethernet.h>
|
---|
52 | # include <sys/sockio.h>
|
---|
53 | # include <netinet/in.h>
|
---|
54 | # include <netinet/in_systm.h>
|
---|
55 | # include <netinet/ip.h>
|
---|
56 | # include <netinet/ip_icmp.h>
|
---|
57 | # include <netinet/udp.h>
|
---|
58 | # include <netinet/tcp.h>
|
---|
59 | # include <net/if.h>
|
---|
60 | # include <stropts.h>
|
---|
61 | # include <fcntl.h>
|
---|
62 | # include <stdlib.h>
|
---|
63 | # include <stdio.h>
|
---|
64 | # ifdef VBOX_WITH_CROSSBOW
|
---|
65 | # include "solaris/vbox-libdlpi.h"
|
---|
66 | # endif
|
---|
67 | #else
|
---|
68 | # include <sys/fcntl.h>
|
---|
69 | #endif
|
---|
70 | #include <errno.h>
|
---|
71 | #include <unistd.h>
|
---|
72 |
|
---|
73 | #ifdef RT_OS_L4
|
---|
74 | # include <l4/vboxserver/file.h>
|
---|
75 | #endif
|
---|
76 |
|
---|
77 | #include "Builtins.h"
|
---|
78 |
|
---|
79 |
|
---|
80 | /*******************************************************************************
|
---|
81 | * Structures and Typedefs *
|
---|
82 | *******************************************************************************/
|
---|
83 | /**
|
---|
84 | * TAP driver instance data.
|
---|
85 | *
|
---|
86 | * @implements PDMINETWORKUP
|
---|
87 | */
|
---|
88 | typedef struct DRVTAP
|
---|
89 | {
|
---|
90 | /** The network interface. */
|
---|
91 | PDMINETWORKUP INetworkUp;
|
---|
92 | /** The network interface. */
|
---|
93 | PPDMINETWORKDOWN pIAboveNet;
|
---|
94 | /** Pointer to the driver instance. */
|
---|
95 | PPDMDRVINS pDrvIns;
|
---|
96 | /** TAP device file handle. */
|
---|
97 | RTFILE FileDevice;
|
---|
98 | /** The configured TAP device name. */
|
---|
99 | char *pszDeviceName;
|
---|
100 | #ifdef RT_OS_SOLARIS
|
---|
101 | # ifdef VBOX_WITH_CROSSBOW
|
---|
102 | /** Crossbow: MAC address of the device. */
|
---|
103 | RTMAC MacAddress;
|
---|
104 | /** Crossbow: Handle of the NIC. */
|
---|
105 | dlpi_handle_t pDeviceHandle;
|
---|
106 | # else
|
---|
107 | /** IP device file handle (/dev/udp). */
|
---|
108 | RTFILE IPFileDevice;
|
---|
109 | # endif
|
---|
110 | /** Whether device name is obtained from setup application. */
|
---|
111 | bool fStatic;
|
---|
112 | #endif
|
---|
113 | /** TAP setup application. */
|
---|
114 | char *pszSetupApplication;
|
---|
115 | /** TAP terminate application. */
|
---|
116 | char *pszTerminateApplication;
|
---|
117 | /** The write end of the control pipe. */
|
---|
118 | RTFILE PipeWrite;
|
---|
119 | /** The read end of the control pipe. */
|
---|
120 | RTFILE PipeRead;
|
---|
121 | /** Reader thread. */
|
---|
122 | PPDMTHREAD pThread;
|
---|
123 |
|
---|
124 | #ifdef VBOX_WITH_STATISTICS
|
---|
125 | /** Number of sent packets. */
|
---|
126 | STAMCOUNTER StatPktSent;
|
---|
127 | /** Number of sent bytes. */
|
---|
128 | STAMCOUNTER StatPktSentBytes;
|
---|
129 | /** Number of received packets. */
|
---|
130 | STAMCOUNTER StatPktRecv;
|
---|
131 | /** Number of received bytes. */
|
---|
132 | STAMCOUNTER StatPktRecvBytes;
|
---|
133 | /** Profiling packet transmit runs. */
|
---|
134 | STAMPROFILE StatTransmit;
|
---|
135 | /** Profiling packet receive runs. */
|
---|
136 | STAMPROFILEADV StatReceive;
|
---|
137 | #endif /* VBOX_WITH_STATISTICS */
|
---|
138 |
|
---|
139 | #ifdef LOG_ENABLED
|
---|
140 | /** The nano ts of the last transfer. */
|
---|
141 | uint64_t u64LastTransferTS;
|
---|
142 | /** The nano ts of the last receive. */
|
---|
143 | uint64_t u64LastReceiveTS;
|
---|
144 | #endif
|
---|
145 | } DRVTAP, *PDRVTAP;
|
---|
146 |
|
---|
147 |
|
---|
148 | /** Converts a pointer to TAP::INetworkUp to a PRDVTAP. */
|
---|
149 | #define PDMINETWORKUP_2_DRVTAP(pInterface) ( (PDRVTAP)((uintptr_t)pInterface - RT_OFFSETOF(DRVTAP, INetworkUp)) )
|
---|
150 |
|
---|
151 |
|
---|
152 | /*******************************************************************************
|
---|
153 | * Internal Functions *
|
---|
154 | *******************************************************************************/
|
---|
155 | #ifdef RT_OS_SOLARIS
|
---|
156 | # ifdef VBOX_WITH_CROSSBOW
|
---|
157 | static int SolarisOpenVNIC(PDRVTAP pThis);
|
---|
158 | static int SolarisDLPIErr2VBoxErr(int rc);
|
---|
159 | # else
|
---|
160 | static int SolarisTAPAttach(PDRVTAP pThis);
|
---|
161 | # endif
|
---|
162 | #endif
|
---|
163 |
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * @interface_method_impl{PDMINETWORKUP,pfnSendDeprecated}
|
---|
167 | */
|
---|
168 | static DECLCALLBACK(int) drvTAPSendDeprecated(PPDMINETWORKUP pInterface, const void *pvBuf, size_t cb)
|
---|
169 | {
|
---|
170 | PDRVTAP pThis = PDMINETWORKUP_2_DRVTAP(pInterface);
|
---|
171 | STAM_COUNTER_INC(&pThis->StatPktSent);
|
---|
172 | STAM_COUNTER_ADD(&pThis->StatPktSentBytes, cb);
|
---|
173 | STAM_PROFILE_START(&pThis->StatTransmit, a);
|
---|
174 |
|
---|
175 | #ifdef LOG_ENABLED
|
---|
176 | uint64_t u64Now = RTTimeProgramNanoTS();
|
---|
177 | LogFlow(("drvTAPSend: %-4d bytes at %llu ns deltas: r=%llu t=%llu\n",
|
---|
178 | cb, u64Now, u64Now - pThis->u64LastReceiveTS, u64Now - pThis->u64LastTransferTS));
|
---|
179 | pThis->u64LastTransferTS = u64Now;
|
---|
180 | #endif
|
---|
181 | Log2(("drvTAPSend: pvBuf=%p cb=%#x\n"
|
---|
182 | "%.*Rhxd\n",
|
---|
183 | pvBuf, cb, cb, pvBuf));
|
---|
184 |
|
---|
185 | int rc = RTFileWrite(pThis->FileDevice, pvBuf, cb, NULL);
|
---|
186 |
|
---|
187 | STAM_PROFILE_STOP(&pThis->StatTransmit, a);
|
---|
188 | AssertRC(rc);
|
---|
189 | return rc;
|
---|
190 | }
|
---|
191 |
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * @interface_method_impl{PDMINETWORKUP,pfnSetPromiscuousMode}
|
---|
195 | */
|
---|
196 | static DECLCALLBACK(void) drvTAPSetPromiscuousMode(PPDMINETWORKUP pInterface, bool fPromiscuous)
|
---|
197 | {
|
---|
198 | LogFlow(("drvTAPSetPromiscuousMode: fPromiscuous=%d\n", fPromiscuous));
|
---|
199 | /* nothing to do */
|
---|
200 | }
|
---|
201 |
|
---|
202 |
|
---|
203 | /**
|
---|
204 | * Notification on link status changes.
|
---|
205 | *
|
---|
206 | * @param pInterface Pointer to the interface structure containing the called function pointer.
|
---|
207 | * @param enmLinkState The new link state.
|
---|
208 | * @thread EMT
|
---|
209 | */
|
---|
210 | static DECLCALLBACK(void) drvTAPNotifyLinkChanged(PPDMINETWORKUP pInterface, PDMNETWORKLINKSTATE enmLinkState)
|
---|
211 | {
|
---|
212 | LogFlow(("drvNATNotifyLinkChanged: enmLinkState=%d\n", enmLinkState));
|
---|
213 | /** @todo take action on link down and up. Stop the polling and such like. */
|
---|
214 | }
|
---|
215 |
|
---|
216 |
|
---|
217 | /**
|
---|
218 | * Asynchronous I/O thread for handling receive.
|
---|
219 | *
|
---|
220 | * @returns VINF_SUCCESS (ignored).
|
---|
221 | * @param Thread Thread handle.
|
---|
222 | * @param pvUser Pointer to a DRVTAP structure.
|
---|
223 | */
|
---|
224 | static DECLCALLBACK(int) drvTAPAsyncIoThread(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
|
---|
225 | {
|
---|
226 | PDRVTAP pThis = PDMINS_2_DATA(pDrvIns, PDRVTAP);
|
---|
227 | LogFlow(("drvTAPAsyncIoThread: pThis=%p\n", pThis));
|
---|
228 |
|
---|
229 | if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
|
---|
230 | return VINF_SUCCESS;
|
---|
231 |
|
---|
232 | STAM_PROFILE_ADV_START(&pThis->StatReceive, a);
|
---|
233 |
|
---|
234 | /*
|
---|
235 | * Polling loop.
|
---|
236 | */
|
---|
237 | while (pThread->enmState == PDMTHREADSTATE_RUNNING)
|
---|
238 | {
|
---|
239 | /*
|
---|
240 | * Wait for something to become available.
|
---|
241 | */
|
---|
242 | struct pollfd aFDs[2];
|
---|
243 | aFDs[0].fd = pThis->FileDevice;
|
---|
244 | aFDs[0].events = POLLIN | POLLPRI;
|
---|
245 | aFDs[0].revents = 0;
|
---|
246 | aFDs[1].fd = pThis->PipeRead;
|
---|
247 | aFDs[1].events = POLLIN | POLLPRI | POLLERR | POLLHUP;
|
---|
248 | aFDs[1].revents = 0;
|
---|
249 | STAM_PROFILE_ADV_STOP(&pThis->StatReceive, a);
|
---|
250 | errno=0;
|
---|
251 | int rc = poll(&aFDs[0], RT_ELEMENTS(aFDs), -1 /* infinite */);
|
---|
252 |
|
---|
253 | /* this might have changed in the meantime */
|
---|
254 | if (pThread->enmState != PDMTHREADSTATE_RUNNING)
|
---|
255 | break;
|
---|
256 |
|
---|
257 | STAM_PROFILE_ADV_START(&pThis->StatReceive, a);
|
---|
258 | if ( rc > 0
|
---|
259 | && (aFDs[0].revents & (POLLIN | POLLPRI))
|
---|
260 | && !aFDs[1].revents)
|
---|
261 | {
|
---|
262 | /*
|
---|
263 | * Read the frame.
|
---|
264 | */
|
---|
265 | char achBuf[16384];
|
---|
266 | size_t cbRead = 0;
|
---|
267 | #ifdef VBOX_WITH_CROSSBOW
|
---|
268 | cbRead = sizeof(achBuf);
|
---|
269 | rc = g_pfnLibDlpiRecv(pThis->pDeviceHandle, NULL, NULL, achBuf, &cbRead, -1, NULL);
|
---|
270 | rc = RT_LIKELY(rc == DLPI_SUCCESS) ? VINF_SUCCESS : SolarisDLPIErr2VBoxErr(rc);
|
---|
271 | #else
|
---|
272 | /** @note At least on Linux we will never receive more than one network packet
|
---|
273 | * after poll() returned successfully. I don't know why but a second
|
---|
274 | * RTFileRead() operation will return with VERR_TRY_AGAIN in any case. */
|
---|
275 | rc = RTFileRead(pThis->FileDevice, achBuf, sizeof(achBuf), &cbRead);
|
---|
276 | #endif
|
---|
277 | if (RT_SUCCESS(rc))
|
---|
278 | {
|
---|
279 | /*
|
---|
280 | * Wait for the device to have space for this frame.
|
---|
281 | * Most guests use frame-sized receive buffers, hence non-zero cbMax
|
---|
282 | * automatically means there is enough room for entire frame. Some
|
---|
283 | * guests (eg. Solaris) use large chains of small receive buffers
|
---|
284 | * (each 128 or so bytes large). We will still start receiving as soon
|
---|
285 | * as cbMax is non-zero because:
|
---|
286 | * - it would be quite expensive for pfnCanReceive to accurately
|
---|
287 | * determine free receive buffer space
|
---|
288 | * - if we were waiting for enough free buffers, there is a risk
|
---|
289 | * of deadlocking because the guest could be waiting for a receive
|
---|
290 | * overflow error to allocate more receive buffers
|
---|
291 | */
|
---|
292 | STAM_PROFILE_ADV_STOP(&pThis->StatReceive, a);
|
---|
293 | int rc1 = pThis->pIAboveNet->pfnWaitReceiveAvail(pThis->pIAboveNet, RT_INDEFINITE_WAIT);
|
---|
294 | STAM_PROFILE_ADV_START(&pThis->StatReceive, a);
|
---|
295 |
|
---|
296 | /*
|
---|
297 | * A return code != VINF_SUCCESS means that we were woken up during a VM
|
---|
298 | * state transistion. Drop the packet and wait for the next one.
|
---|
299 | */
|
---|
300 | if (RT_FAILURE(rc1))
|
---|
301 | continue;
|
---|
302 |
|
---|
303 | /*
|
---|
304 | * Pass the data up.
|
---|
305 | */
|
---|
306 | #ifdef LOG_ENABLED
|
---|
307 | uint64_t u64Now = RTTimeProgramNanoTS();
|
---|
308 | LogFlow(("drvTAPAsyncIoThread: %-4d bytes at %llu ns deltas: r=%llu t=%llu\n",
|
---|
309 | cbRead, u64Now, u64Now - pThis->u64LastReceiveTS, u64Now - pThis->u64LastTransferTS));
|
---|
310 | pThis->u64LastReceiveTS = u64Now;
|
---|
311 | #endif
|
---|
312 | Log2(("drvTAPAsyncIoThread: cbRead=%#x\n" "%.*Rhxd\n", cbRead, cbRead, achBuf));
|
---|
313 | STAM_COUNTER_INC(&pThis->StatPktRecv);
|
---|
314 | STAM_COUNTER_ADD(&pThis->StatPktRecvBytes, cbRead);
|
---|
315 | rc1 = pThis->pIAboveNet->pfnReceive(pThis->pIAboveNet, achBuf, cbRead);
|
---|
316 | AssertRC(rc1);
|
---|
317 | }
|
---|
318 | else
|
---|
319 | {
|
---|
320 | LogFlow(("drvTAPAsyncIoThread: RTFileRead -> %Rrc\n", rc));
|
---|
321 | if (rc == VERR_INVALID_HANDLE)
|
---|
322 | break;
|
---|
323 | RTThreadYield();
|
---|
324 | }
|
---|
325 | }
|
---|
326 | else if ( rc > 0
|
---|
327 | && aFDs[1].revents)
|
---|
328 | {
|
---|
329 | LogFlow(("drvTAPAsyncIoThread: Control message: enmState=%d revents=%#x\n", pThread->enmState, aFDs[1].revents));
|
---|
330 | if (aFDs[1].revents & (POLLHUP | POLLERR | POLLNVAL))
|
---|
331 | break;
|
---|
332 |
|
---|
333 | /* drain the pipe */
|
---|
334 | char ch;
|
---|
335 | size_t cbRead;
|
---|
336 | RTFileRead(pThis->PipeRead, &ch, 1, &cbRead);
|
---|
337 | }
|
---|
338 | else
|
---|
339 | {
|
---|
340 | /*
|
---|
341 | * poll() failed for some reason. Yield to avoid eating too much CPU.
|
---|
342 | *
|
---|
343 | * EINTR errors have been seen frequently. They should be harmless, even
|
---|
344 | * if they are not supposed to occur in our setup.
|
---|
345 | */
|
---|
346 | if (errno == EINTR)
|
---|
347 | Log(("rc=%d revents=%#x,%#x errno=%p %s\n", rc, aFDs[0].revents, aFDs[1].revents, errno, strerror(errno)));
|
---|
348 | else
|
---|
349 | AssertMsgFailed(("rc=%d revents=%#x,%#x errno=%p %s\n", rc, aFDs[0].revents, aFDs[1].revents, errno, strerror(errno)));
|
---|
350 | RTThreadYield();
|
---|
351 | }
|
---|
352 | }
|
---|
353 |
|
---|
354 |
|
---|
355 | LogFlow(("drvTAPAsyncIoThread: returns %Rrc\n", VINF_SUCCESS));
|
---|
356 | STAM_PROFILE_ADV_STOP(&pThis->StatReceive, a);
|
---|
357 | return VINF_SUCCESS;
|
---|
358 | }
|
---|
359 |
|
---|
360 |
|
---|
361 | /**
|
---|
362 | * Unblock the send thread so it can respond to a state change.
|
---|
363 | *
|
---|
364 | * @returns VBox status code.
|
---|
365 | * @param pDevIns The pcnet device instance.
|
---|
366 | * @param pThread The send thread.
|
---|
367 | */
|
---|
368 | static DECLCALLBACK(int) drvTapAsyncIoWakeup(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
|
---|
369 | {
|
---|
370 | PDRVTAP pThis = PDMINS_2_DATA(pDrvIns, PDRVTAP);
|
---|
371 |
|
---|
372 | int rc = RTFileWrite(pThis->PipeWrite, "", 1, NULL);
|
---|
373 | AssertRC(rc);
|
---|
374 |
|
---|
375 | return VINF_SUCCESS;
|
---|
376 | }
|
---|
377 |
|
---|
378 |
|
---|
379 | #if defined(RT_OS_SOLARIS)
|
---|
380 | /**
|
---|
381 | * Calls OS-specific TAP setup application/script.
|
---|
382 | *
|
---|
383 | * @returns VBox error code.
|
---|
384 | * @param pThis The instance data.
|
---|
385 | */
|
---|
386 | static int drvTAPSetupApplication(PDRVTAP pThis)
|
---|
387 | {
|
---|
388 | char szCommand[4096];
|
---|
389 |
|
---|
390 | #ifdef VBOX_WITH_CROSSBOW
|
---|
391 | /* Convert MAC address bytes to string (required by Solaris' dladm). */
|
---|
392 | char *pszHex = "0123456789abcdef";
|
---|
393 | uint8_t *pMacAddr8 = pThis->MacAddress.au8;
|
---|
394 | char szMacAddress[3 * sizeof(RTMAC)];
|
---|
395 | for (unsigned int i = 0; i < sizeof(RTMAC); i++)
|
---|
396 | {
|
---|
397 | szMacAddress[3 * i] = pszHex[((*pMacAddr8 >> 4) & 0x0f)];
|
---|
398 | szMacAddress[3 * i + 1] = pszHex[(*pMacAddr8 & 0x0f)];
|
---|
399 | szMacAddress[3 * i + 2] = ':';
|
---|
400 | *pMacAddr8++;
|
---|
401 | }
|
---|
402 | szMacAddress[sizeof(szMacAddress) - 1] = 0;
|
---|
403 |
|
---|
404 | RTStrPrintf(szCommand, sizeof(szCommand), "%s %s %s", pThis->pszSetupApplication,
|
---|
405 | szMacAddress, pThis->fStatic ? pThis->pszDeviceName : "");
|
---|
406 | #else
|
---|
407 | RTStrPrintf(szCommand, sizeof(szCommand), "%s %s", pThis->pszSetupApplication,
|
---|
408 | pThis->fStatic ? pThis->pszDeviceName : "");
|
---|
409 | #endif
|
---|
410 |
|
---|
411 | /* Pipe open the setup application. */
|
---|
412 | Log2(("Starting TAP setup application: %s\n", szCommand));
|
---|
413 | FILE* pfSetupHandle = popen(szCommand, "r");
|
---|
414 | if (pfSetupHandle == 0)
|
---|
415 | {
|
---|
416 | LogRel(("TAP#%d: Failed to run TAP setup application: %s\n", pThis->pDrvIns->iInstance,
|
---|
417 | pThis->pszSetupApplication, strerror(errno)));
|
---|
418 | return VERR_HOSTIF_INIT_FAILED;
|
---|
419 | }
|
---|
420 | if (!pThis->fStatic)
|
---|
421 | {
|
---|
422 | /* Obtain device name from setup application. */
|
---|
423 | char acBuffer[64];
|
---|
424 | size_t cBufSize;
|
---|
425 | fgets(acBuffer, sizeof(acBuffer), pfSetupHandle);
|
---|
426 | cBufSize = strlen(acBuffer);
|
---|
427 | /* The script must return the name of the interface followed by a carriage return as the
|
---|
428 | first line of its output. We need a null-terminated string. */
|
---|
429 | if ((cBufSize < 2) || (acBuffer[cBufSize - 1] != '\n'))
|
---|
430 | {
|
---|
431 | pclose(pfSetupHandle);
|
---|
432 | LogRel(("The TAP interface setup script did not return the name of a TAP device.\n"));
|
---|
433 | return VERR_HOSTIF_INIT_FAILED;
|
---|
434 | }
|
---|
435 | /* Overwrite the terminating newline character. */
|
---|
436 | acBuffer[cBufSize - 1] = 0;
|
---|
437 | RTStrAPrintf(&pThis->pszDeviceName, "%s", acBuffer);
|
---|
438 | }
|
---|
439 | int rc = pclose(pfSetupHandle);
|
---|
440 | if (!WIFEXITED(rc))
|
---|
441 | {
|
---|
442 | LogRel(("The TAP interface setup script terminated abnormally.\n"));
|
---|
443 | return VERR_HOSTIF_INIT_FAILED;
|
---|
444 | }
|
---|
445 | if (WEXITSTATUS(rc) != 0)
|
---|
446 | {
|
---|
447 | LogRel(("The TAP interface setup script returned a non-zero exit code.\n"));
|
---|
448 | return VERR_HOSTIF_INIT_FAILED;
|
---|
449 | }
|
---|
450 | return VINF_SUCCESS;
|
---|
451 | }
|
---|
452 |
|
---|
453 |
|
---|
454 | /**
|
---|
455 | * Calls OS-specific TAP terminate application/script.
|
---|
456 | *
|
---|
457 | * @returns VBox error code.
|
---|
458 | * @param pThis The instance data.
|
---|
459 | */
|
---|
460 | static int drvTAPTerminateApplication(PDRVTAP pThis)
|
---|
461 | {
|
---|
462 | char *pszArgs[3];
|
---|
463 | pszArgs[0] = pThis->pszTerminateApplication;
|
---|
464 | pszArgs[1] = pThis->pszDeviceName;
|
---|
465 | pszArgs[2] = NULL;
|
---|
466 |
|
---|
467 | Log2(("Starting TAP terminate application: %s %s\n", pThis->pszTerminateApplication, pThis->pszDeviceName));
|
---|
468 | RTPROCESS pid = NIL_RTPROCESS;
|
---|
469 | int rc = RTProcCreate(pszArgs[0], pszArgs, RTENV_DEFAULT, 0, &pid);
|
---|
470 | if (RT_SUCCESS(rc))
|
---|
471 | {
|
---|
472 | RTPROCSTATUS Status;
|
---|
473 | rc = RTProcWait(pid, 0, &Status);
|
---|
474 | if (RT_SUCCESS(rc))
|
---|
475 | {
|
---|
476 | if ( Status.iStatus == 0
|
---|
477 | && Status.enmReason == RTPROCEXITREASON_NORMAL)
|
---|
478 | return VINF_SUCCESS;
|
---|
479 |
|
---|
480 | LogRel(("TAP#%d: Error running TAP terminate application: %s\n", pThis->pDrvIns->iInstance, pThis->pszTerminateApplication));
|
---|
481 | }
|
---|
482 | else
|
---|
483 | LogRel(("TAP#%d: RTProcWait failed for: %s\n", pThis->pDrvIns->iInstance, pThis->pszTerminateApplication));
|
---|
484 | }
|
---|
485 | else
|
---|
486 | {
|
---|
487 | /* Bad. RTProcCreate() failed! */
|
---|
488 | LogRel(("TAP#%d: Failed to fork() process for running TAP terminate application: %s\n", pThis->pDrvIns->iInstance,
|
---|
489 | pThis->pszTerminateApplication, strerror(errno)));
|
---|
490 | }
|
---|
491 | return VERR_HOSTIF_TERM_FAILED;
|
---|
492 | }
|
---|
493 |
|
---|
494 | #endif /* RT_OS_SOLARIS */
|
---|
495 |
|
---|
496 |
|
---|
497 | #ifdef RT_OS_SOLARIS
|
---|
498 | # ifdef VBOX_WITH_CROSSBOW
|
---|
499 | /**
|
---|
500 | * Crossbow: Open & configure the virtual NIC.
|
---|
501 | *
|
---|
502 | * @returns VBox error code.
|
---|
503 | * @param pThis The instance data.
|
---|
504 | */
|
---|
505 | static int SolarisOpenVNIC(PDRVTAP pThis)
|
---|
506 | {
|
---|
507 | /*
|
---|
508 | * Open & bind the NIC using the datalink provider routine.
|
---|
509 | */
|
---|
510 | int rc = g_pfnLibDlpiOpen(pThis->pszDeviceName, &pThis->pDeviceHandle, DLPI_RAW);
|
---|
511 | if (rc != DLPI_SUCCESS)
|
---|
512 | return PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_HOSTIF_INIT_FAILED, RT_SRC_POS,
|
---|
513 | N_("Failed to open VNIC \"%s\" in raw mode"), pThis->pszDeviceName);
|
---|
514 |
|
---|
515 | dlpi_info_t vnicInfo;
|
---|
516 | rc = g_pfnLibDlpiInfo(pThis->pDeviceHandle, &vnicInfo, 0);
|
---|
517 | if (rc == DLPI_SUCCESS)
|
---|
518 | {
|
---|
519 | if (vnicInfo.di_mactype == DL_ETHER)
|
---|
520 | {
|
---|
521 | rc = g_pfnLibDlpiBind(pThis->pDeviceHandle, DLPI_ANY_SAP, NULL);
|
---|
522 | if (rc == DLPI_SUCCESS)
|
---|
523 | {
|
---|
524 | rc = g_pfnLibDlpiSetPhysAddr(pThis->pDeviceHandle, DL_CURR_PHYS_ADDR, &pThis->MacAddress, ETHERADDRL);
|
---|
525 | if (rc == DLPI_SUCCESS)
|
---|
526 | {
|
---|
527 | rc = g_pfnLibDlpiPromiscon(pThis->pDeviceHandle, DL_PROMISC_SAP);
|
---|
528 | if (rc == DLPI_SUCCESS)
|
---|
529 | {
|
---|
530 | /* Need to use DL_PROMIS_PHYS (not multicast) as we cannot be sure what the guest needs. */
|
---|
531 | rc = g_pfnLibDlpiPromiscon(pThis->pDeviceHandle, DL_PROMISC_PHYS);
|
---|
532 | if (rc == DLPI_SUCCESS)
|
---|
533 | {
|
---|
534 | pThis->FileDevice = g_pfnLibDlpiFd(pThis->pDeviceHandle);
|
---|
535 | if (pThis->FileDevice >= 0)
|
---|
536 | {
|
---|
537 | Log(("SolarisOpenVNIC: %s -> %d\n", pThis->pszDeviceName, pThis->FileDevice));
|
---|
538 | return VINF_SUCCESS;
|
---|
539 | }
|
---|
540 |
|
---|
541 | rc = PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_HOSTIF_INIT_FAILED, RT_SRC_POS,
|
---|
542 | N_("Failed to obtain file descriptor for VNIC"));
|
---|
543 | }
|
---|
544 | else
|
---|
545 | rc = PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_HOSTIF_INIT_FAILED, RT_SRC_POS,
|
---|
546 | N_("Failed to set appropriate promiscous mode"));
|
---|
547 | }
|
---|
548 | else
|
---|
549 | rc = PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_HOSTIF_INIT_FAILED, RT_SRC_POS,
|
---|
550 | N_("Failed to activate promiscous mode for VNIC"));
|
---|
551 | }
|
---|
552 | else
|
---|
553 | rc = PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_HOSTIF_INIT_FAILED, RT_SRC_POS,
|
---|
554 | N_("Failed to set physical address for VNIC"));
|
---|
555 | }
|
---|
556 | else
|
---|
557 | rc = PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_HOSTIF_INIT_FAILED, RT_SRC_POS,
|
---|
558 | N_("Failed to bind VNIC"));
|
---|
559 | }
|
---|
560 | else
|
---|
561 | rc = PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_HOSTIF_INIT_FAILED, RT_SRC_POS,
|
---|
562 | N_("VNIC type is not ethernet"));
|
---|
563 | }
|
---|
564 | else
|
---|
565 | rc = PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_HOSTIF_INIT_FAILED, RT_SRC_POS,
|
---|
566 | N_("Failed to obtain VNIC info"));
|
---|
567 | g_pfnLibDlpiClose(pThis->pDeviceHandle);
|
---|
568 | return rc;
|
---|
569 | }
|
---|
570 |
|
---|
571 |
|
---|
572 | /**
|
---|
573 | * Crossbow: Converts a Solaris DLPI error code to a VBox error code.
|
---|
574 | *
|
---|
575 | * @returns corresponding VBox error code.
|
---|
576 | * @param rc DLPI error code (DLPI_* defines).
|
---|
577 | */
|
---|
578 | static int SolarisDLPIErr2VBoxErr(int rc)
|
---|
579 | {
|
---|
580 | switch (rc)
|
---|
581 | {
|
---|
582 | case DLPI_SUCCESS: return VINF_SUCCESS;
|
---|
583 | case DLPI_EINVAL: return VERR_INVALID_PARAMETER;
|
---|
584 | case DLPI_ELINKNAMEINVAL: return VERR_INVALID_NAME;
|
---|
585 | case DLPI_EINHANDLE: return VERR_INVALID_HANDLE;
|
---|
586 | case DLPI_ETIMEDOUT: return VERR_TIMEOUT;
|
---|
587 | case DLPI_FAILURE: return VERR_GENERAL_FAILURE;
|
---|
588 |
|
---|
589 | case DLPI_EVERNOTSUP:
|
---|
590 | case DLPI_EMODENOTSUP:
|
---|
591 | case DLPI_ERAWNOTSUP:
|
---|
592 | /* case DLPI_ENOTENOTSUP: */
|
---|
593 | case DLPI_EUNAVAILSAP: return VERR_NOT_SUPPORTED;
|
---|
594 |
|
---|
595 | /* Define VBox error codes for these, if really needed. */
|
---|
596 | case DLPI_ENOLINK:
|
---|
597 | case DLPI_EBADLINK:
|
---|
598 | /* case DLPI_ENOTEIDINVAL: */
|
---|
599 | case DLPI_EBADMSG:
|
---|
600 | case DLPI_ENOTSTYLE2: return VERR_GENERAL_FAILURE;
|
---|
601 | }
|
---|
602 |
|
---|
603 | AssertMsgFailed(("SolarisDLPIErr2VBoxErr: Unhandled error %d\n", rc));
|
---|
604 | return VERR_UNRESOLVED_ERROR;
|
---|
605 | }
|
---|
606 |
|
---|
607 | # else /* VBOX_WITH_CROSSBOW */
|
---|
608 |
|
---|
609 | /** From net/if_tun.h, installed by Universal TUN/TAP driver */
|
---|
610 | # define TUNNEWPPA (('T'<<16) | 0x0001)
|
---|
611 | /** Whether to enable ARP for TAP. */
|
---|
612 | # define VBOX_SOLARIS_TAP_ARP 1
|
---|
613 |
|
---|
614 | /**
|
---|
615 | * Creates/Attaches TAP device to IP.
|
---|
616 | *
|
---|
617 | * @returns VBox error code.
|
---|
618 | * @param pThis The instance data.
|
---|
619 | */
|
---|
620 | static DECLCALLBACK(int) SolarisTAPAttach(PDRVTAP pThis)
|
---|
621 | {
|
---|
622 | LogFlow(("SolarisTapAttach: pThis=%p\n", pThis));
|
---|
623 |
|
---|
624 |
|
---|
625 | int IPFileDes = open("/dev/udp", O_RDWR, 0);
|
---|
626 | if (IPFileDes < 0)
|
---|
627 | return PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_PDM_HIF_OPEN_FAILED, RT_SRC_POS,
|
---|
628 | N_("Failed to open /dev/udp. errno=%d"), errno);
|
---|
629 |
|
---|
630 | int TapFileDes = open("/dev/tap", O_RDWR, 0);
|
---|
631 | if (TapFileDes < 0)
|
---|
632 | return PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_PDM_HIF_OPEN_FAILED, RT_SRC_POS,
|
---|
633 | N_("Failed to open /dev/tap for TAP. errno=%d"), errno);
|
---|
634 |
|
---|
635 | /* Use the PPA from the ifname if possible (e.g "tap2", then use 2 as PPA) */
|
---|
636 | int iPPA = -1;
|
---|
637 | if (pThis->pszDeviceName)
|
---|
638 | {
|
---|
639 | size_t cch = strlen(pThis->pszDeviceName);
|
---|
640 | if (cch > 1 && RT_C_IS_DIGIT(pThis->pszDeviceName[cch - 1]) != 0)
|
---|
641 | iPPA = pThis->pszDeviceName[cch - 1] - '0';
|
---|
642 | }
|
---|
643 |
|
---|
644 | struct strioctl ioIF;
|
---|
645 | ioIF.ic_cmd = TUNNEWPPA;
|
---|
646 | ioIF.ic_len = sizeof(iPPA);
|
---|
647 | ioIF.ic_dp = (char *)(&iPPA);
|
---|
648 | ioIF.ic_timout = 0;
|
---|
649 | iPPA = ioctl(TapFileDes, I_STR, &ioIF);
|
---|
650 | if (iPPA < 0)
|
---|
651 | {
|
---|
652 | close(TapFileDes);
|
---|
653 | return PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_HOSTIF_IOCTL, RT_SRC_POS,
|
---|
654 | N_("Failed to get new interface. errno=%d"), errno);
|
---|
655 | }
|
---|
656 |
|
---|
657 | int InterfaceFD = open("/dev/tap", O_RDWR, 0);
|
---|
658 | if (!InterfaceFD)
|
---|
659 | return PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_PDM_HIF_OPEN_FAILED, RT_SRC_POS,
|
---|
660 | N_("Failed to open interface /dev/tap. errno=%d"), errno);
|
---|
661 |
|
---|
662 | if (ioctl(InterfaceFD, I_PUSH, "ip") == -1)
|
---|
663 | {
|
---|
664 | close(InterfaceFD);
|
---|
665 | return PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_HOSTIF_IOCTL, RT_SRC_POS,
|
---|
666 | N_("Failed to push IP. errno=%d"), errno);
|
---|
667 | }
|
---|
668 |
|
---|
669 | struct lifreq ifReq;
|
---|
670 | memset(&ifReq, 0, sizeof(ifReq));
|
---|
671 | if (ioctl(InterfaceFD, SIOCGLIFFLAGS, &ifReq) == -1)
|
---|
672 | LogRel(("TAP#%d: Failed to get interface flags.\n", pThis->pDrvIns->iInstance));
|
---|
673 |
|
---|
674 | ifReq.lifr_ppa = iPPA;
|
---|
675 | RTStrPrintf (ifReq.lifr_name, sizeof(ifReq.lifr_name), pThis->pszDeviceName);
|
---|
676 |
|
---|
677 | if (ioctl(InterfaceFD, SIOCSLIFNAME, &ifReq) == -1)
|
---|
678 | LogRel(("TAP#%d: Failed to set PPA. errno=%d\n", pThis->pDrvIns->iInstance, errno));
|
---|
679 |
|
---|
680 | if (ioctl(InterfaceFD, SIOCGLIFFLAGS, &ifReq) == -1)
|
---|
681 | LogRel(("TAP#%d: Failed to get interface flags after setting PPA. errno=%d\n", pThis->pDrvIns->iInstance, errno));
|
---|
682 |
|
---|
683 | #ifdef VBOX_SOLARIS_TAP_ARP
|
---|
684 | /* Interface */
|
---|
685 | if (ioctl(InterfaceFD, I_PUSH, "arp") == -1)
|
---|
686 | LogRel(("TAP#%d: Failed to push ARP to Interface FD. errno=%d\n", pThis->pDrvIns->iInstance, errno));
|
---|
687 |
|
---|
688 | /* IP */
|
---|
689 | if (ioctl(IPFileDes, I_POP, NULL) == -1)
|
---|
690 | LogRel(("TAP#%d: Failed I_POP from IP FD. errno=%d\n", pThis->pDrvIns->iInstance, errno));
|
---|
691 |
|
---|
692 | if (ioctl(IPFileDes, I_PUSH, "arp") == -1)
|
---|
693 | LogRel(("TAP#%d: Failed to push ARP to IP FD. errno=%d\n", pThis->pDrvIns->iInstance, errno));
|
---|
694 |
|
---|
695 | /* ARP */
|
---|
696 | int ARPFileDes = open("/dev/tap", O_RDWR, 0);
|
---|
697 | if (ARPFileDes < 0)
|
---|
698 | LogRel(("TAP#%d: Failed to open for /dev/tap for ARP. errno=%d", pThis->pDrvIns->iInstance, errno));
|
---|
699 |
|
---|
700 | if (ioctl(ARPFileDes, I_PUSH, "arp") == -1)
|
---|
701 | LogRel(("TAP#%d: Failed to push ARP to ARP FD. errno=%d\n", pThis->pDrvIns->iInstance, errno));
|
---|
702 |
|
---|
703 | ioIF.ic_cmd = SIOCSLIFNAME;
|
---|
704 | ioIF.ic_timout = 0;
|
---|
705 | ioIF.ic_len = sizeof(ifReq);
|
---|
706 | ioIF.ic_dp = (char *)&ifReq;
|
---|
707 | if (ioctl(ARPFileDes, I_STR, &ioIF) == -1)
|
---|
708 | LogRel(("TAP#%d: Failed to set interface name to ARP.\n", pThis->pDrvIns->iInstance));
|
---|
709 | #endif
|
---|
710 |
|
---|
711 | /* We must use I_LINK and not I_PLINK as I_PLINK makes the link persistent.
|
---|
712 | * Then we would not be able unlink the interface if we reuse it.
|
---|
713 | * Even 'unplumb' won't work after that.
|
---|
714 | */
|
---|
715 | int IPMuxID = ioctl(IPFileDes, I_LINK, InterfaceFD);
|
---|
716 | if (IPMuxID == -1)
|
---|
717 | {
|
---|
718 | close(InterfaceFD);
|
---|
719 | #ifdef VBOX_SOLARIS_TAP_ARP
|
---|
720 | close(ARPFileDes);
|
---|
721 | #endif
|
---|
722 | LogRel(("TAP#%d: Cannot link TAP device to IP.\n", pThis->pDrvIns->iInstance));
|
---|
723 | return PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_HOSTIF_IOCTL, RT_SRC_POS,
|
---|
724 | N_("Failed to link TAP device to IP. Check TAP interface name. errno=%d"), errno);
|
---|
725 | }
|
---|
726 |
|
---|
727 | #ifdef VBOX_SOLARIS_TAP_ARP
|
---|
728 | int ARPMuxID = ioctl(IPFileDes, I_LINK, ARPFileDes);
|
---|
729 | if (ARPMuxID == -1)
|
---|
730 | LogRel(("TAP#%d: Failed to link TAP device to ARP\n", pThis->pDrvIns->iInstance));
|
---|
731 |
|
---|
732 | close(ARPFileDes);
|
---|
733 | #endif
|
---|
734 | close(InterfaceFD);
|
---|
735 |
|
---|
736 | /* Reuse ifReq */
|
---|
737 | memset(&ifReq, 0, sizeof(ifReq));
|
---|
738 | RTStrPrintf (ifReq.lifr_name, sizeof(ifReq.lifr_name), pThis->pszDeviceName);
|
---|
739 | ifReq.lifr_ip_muxid = IPMuxID;
|
---|
740 | #ifdef VBOX_SOLARIS_TAP_ARP
|
---|
741 | ifReq.lifr_arp_muxid = ARPMuxID;
|
---|
742 | #endif
|
---|
743 |
|
---|
744 | if (ioctl(IPFileDes, SIOCSLIFMUXID, &ifReq) == -1)
|
---|
745 | {
|
---|
746 | #ifdef VBOX_SOLARIS_TAP_ARP
|
---|
747 | ioctl(IPFileDes, I_PUNLINK, ARPMuxID);
|
---|
748 | #endif
|
---|
749 | ioctl(IPFileDes, I_PUNLINK, IPMuxID);
|
---|
750 | close(IPFileDes);
|
---|
751 | LogRel(("TAP#%d: Failed to set Mux ID.\n", pThis->pDrvIns->iInstance));
|
---|
752 | return PDMDrvHlpVMSetError(pThis->pDrvIns, VERR_HOSTIF_IOCTL, RT_SRC_POS,
|
---|
753 | N_("Failed to set Mux ID. Check TAP interface name. errno=%d"), errno);
|
---|
754 | }
|
---|
755 |
|
---|
756 | pThis->FileDevice = (RTFILE)TapFileDes;
|
---|
757 | pThis->IPFileDevice = (RTFILE)IPFileDes;
|
---|
758 |
|
---|
759 | return VINF_SUCCESS;
|
---|
760 | }
|
---|
761 |
|
---|
762 | # endif /* VBOX_WITH_CROSSBOW */
|
---|
763 | #endif /* RT_OS_SOLARIS */
|
---|
764 |
|
---|
765 | /* -=-=-=-=- PDMIBASE -=-=-=-=- */
|
---|
766 |
|
---|
767 | /**
|
---|
768 | * @interface_method_impl{PDMIBASE,pfnQueryInterface}
|
---|
769 | */
|
---|
770 | static DECLCALLBACK(void *) drvTAPQueryInterface(PPDMIBASE pInterface, const char *pszIID)
|
---|
771 | {
|
---|
772 | PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
|
---|
773 | PDRVTAP pThis = PDMINS_2_DATA(pDrvIns, PDRVTAP);
|
---|
774 |
|
---|
775 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
|
---|
776 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMINETWORKUP, &pThis->INetworkUp);
|
---|
777 | return NULL;
|
---|
778 | }
|
---|
779 |
|
---|
780 | /* -=-=-=-=- PDMDRVREG -=-=-=-=- */
|
---|
781 |
|
---|
782 | /**
|
---|
783 | * Destruct a driver instance.
|
---|
784 | *
|
---|
785 | * Most VM resources are freed by the VM. This callback is provided so that any non-VM
|
---|
786 | * resources can be freed correctly.
|
---|
787 | *
|
---|
788 | * @param pDrvIns The driver instance data.
|
---|
789 | */
|
---|
790 | static DECLCALLBACK(void) drvTAPDestruct(PPDMDRVINS pDrvIns)
|
---|
791 | {
|
---|
792 | LogFlow(("drvTAPDestruct\n"));
|
---|
793 | PDRVTAP pThis = PDMINS_2_DATA(pDrvIns, PDRVTAP);
|
---|
794 | PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
|
---|
795 |
|
---|
796 | /*
|
---|
797 | * Terminate the control pipe.
|
---|
798 | */
|
---|
799 | if (pThis->PipeWrite != NIL_RTFILE)
|
---|
800 | {
|
---|
801 | int rc = RTFileClose(pThis->PipeWrite);
|
---|
802 | AssertRC(rc);
|
---|
803 | pThis->PipeWrite = NIL_RTFILE;
|
---|
804 | }
|
---|
805 | if (pThis->PipeRead != NIL_RTFILE)
|
---|
806 | {
|
---|
807 | int rc = RTFileClose(pThis->PipeRead);
|
---|
808 | AssertRC(rc);
|
---|
809 | pThis->PipeRead = NIL_RTFILE;
|
---|
810 | }
|
---|
811 |
|
---|
812 | #ifdef RT_OS_SOLARIS
|
---|
813 | /** @todo r=bird: This *does* need checking against ConsoleImpl2.cpp if used on non-solaris systems. */
|
---|
814 | if (pThis->FileDevice != NIL_RTFILE)
|
---|
815 | {
|
---|
816 | int rc = RTFileClose(pThis->FileDevice);
|
---|
817 | AssertRC(rc);
|
---|
818 | pThis->FileDevice = NIL_RTFILE;
|
---|
819 | }
|
---|
820 |
|
---|
821 | # ifndef VBOX_WITH_CROSSBOW
|
---|
822 | if (pThis->IPFileDevice != NIL_RTFILE)
|
---|
823 | {
|
---|
824 | int rc = RTFileClose(pThis->IPFileDevice);
|
---|
825 | AssertRC(rc);
|
---|
826 | pThis->IPFileDevice = NIL_RTFILE;
|
---|
827 | }
|
---|
828 | # endif
|
---|
829 |
|
---|
830 | /*
|
---|
831 | * Call TerminateApplication after closing the device otherwise
|
---|
832 | * TerminateApplication would not be able to unplumb it.
|
---|
833 | */
|
---|
834 | if (pThis->pszTerminateApplication)
|
---|
835 | drvTAPTerminateApplication(pThis);
|
---|
836 |
|
---|
837 | #endif /* RT_OS_SOLARIS */
|
---|
838 |
|
---|
839 | #ifdef RT_OS_SOLARIS
|
---|
840 | if (!pThis->fStatic)
|
---|
841 | RTStrFree(pThis->pszDeviceName); /* allocated by drvTAPSetupApplication */
|
---|
842 | else
|
---|
843 | MMR3HeapFree(pThis->pszDeviceName);
|
---|
844 | #else
|
---|
845 | MMR3HeapFree(pThis->pszDeviceName);
|
---|
846 | #endif
|
---|
847 | MMR3HeapFree(pThis->pszSetupApplication);
|
---|
848 | MMR3HeapFree(pThis->pszTerminateApplication);
|
---|
849 |
|
---|
850 | #ifdef VBOX_WITH_STATISTICS
|
---|
851 | /*
|
---|
852 | * Deregister statistics.
|
---|
853 | */
|
---|
854 | PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatPktSent);
|
---|
855 | PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatPktSentBytes);
|
---|
856 | PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatPktRecv);
|
---|
857 | PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatPktRecvBytes);
|
---|
858 | PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatTransmit);
|
---|
859 | PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->StatReceive);
|
---|
860 | #endif /* VBOX_WITH_STATISTICS */
|
---|
861 | }
|
---|
862 |
|
---|
863 |
|
---|
864 | /**
|
---|
865 | * Construct a TAP network transport driver instance.
|
---|
866 | *
|
---|
867 | * @copydoc FNPDMDRVCONSTRUCT
|
---|
868 | */
|
---|
869 | static DECLCALLBACK(int) drvTAPConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
|
---|
870 | {
|
---|
871 | PDRVTAP pThis = PDMINS_2_DATA(pDrvIns, PDRVTAP);
|
---|
872 | PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
|
---|
873 |
|
---|
874 | /*
|
---|
875 | * Init the static parts.
|
---|
876 | */
|
---|
877 | pThis->pDrvIns = pDrvIns;
|
---|
878 | pThis->FileDevice = NIL_RTFILE;
|
---|
879 | pThis->pszDeviceName = NULL;
|
---|
880 | #ifdef RT_OS_SOLARIS
|
---|
881 | # ifdef VBOX_WITH_CROSSBOW
|
---|
882 | pThis->pDeviceHandle = NULL;
|
---|
883 | # else
|
---|
884 | pThis->IPFileDevice = NIL_RTFILE;
|
---|
885 | # endif
|
---|
886 | pThis->fStatic = true;
|
---|
887 | #endif
|
---|
888 | pThis->pszSetupApplication = NULL;
|
---|
889 | pThis->pszTerminateApplication = NULL;
|
---|
890 |
|
---|
891 | /* IBase */
|
---|
892 | pDrvIns->IBase.pfnQueryInterface = drvTAPQueryInterface;
|
---|
893 | /* INetwork */
|
---|
894 | /** @todo implement the new INetworkUp interfaces. */
|
---|
895 | pThis->INetworkUp.pfnSendDeprecated = drvTAPSendDeprecated;
|
---|
896 | pThis->INetworkUp.pfnSetPromiscuousMode = drvTAPSetPromiscuousMode;
|
---|
897 | pThis->INetworkUp.pfnNotifyLinkChanged = drvTAPNotifyLinkChanged;
|
---|
898 |
|
---|
899 | /*
|
---|
900 | * Validate the config.
|
---|
901 | */
|
---|
902 | if (!CFGMR3AreValuesValid(pCfg, "Device\0InitProg\0TermProg\0FileHandle\0TAPSetupApplication\0TAPTerminateApplication\0MAC"))
|
---|
903 | return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES, "");
|
---|
904 |
|
---|
905 | /*
|
---|
906 | * Check that no-one is attached to us.
|
---|
907 | */
|
---|
908 | AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
|
---|
909 | ("Configuration error: Not possible to attach anything to this driver!\n"),
|
---|
910 | VERR_PDM_DRVINS_NO_ATTACH);
|
---|
911 |
|
---|
912 | /*
|
---|
913 | * Query the network port interface.
|
---|
914 | */
|
---|
915 | pThis->pIAboveNet = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMINETWORKDOWN);
|
---|
916 | if (!pThis->pIAboveNet)
|
---|
917 | return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_MISSING_INTERFACE_ABOVE,
|
---|
918 | N_("Configuration error: The above device/driver didn't export the network port interface"));
|
---|
919 |
|
---|
920 | /*
|
---|
921 | * Read the configuration.
|
---|
922 | */
|
---|
923 | int rc;
|
---|
924 | #if defined(RT_OS_SOLARIS) /** @todo Other platforms' TAP code should be moved here from ConsoleImpl & VBoxBFE. */
|
---|
925 | rc = CFGMR3QueryStringAlloc(pCfg, "TAPSetupApplication", &pThis->pszSetupApplication);
|
---|
926 | if (RT_SUCCESS(rc))
|
---|
927 | {
|
---|
928 | if (!RTPathExists(pThis->pszSetupApplication))
|
---|
929 | return PDMDrvHlpVMSetError(pDrvIns, VERR_HOSTIF_INIT_FAILED, RT_SRC_POS,
|
---|
930 | N_("Invalid TAP setup program path: %s"), pThis->pszSetupApplication);
|
---|
931 | }
|
---|
932 | else if (rc != VERR_CFGM_VALUE_NOT_FOUND)
|
---|
933 | return PDMDRV_SET_ERROR(pDrvIns, rc, N_("Configuration error: failed to query \"TAPTerminateApplication\""));
|
---|
934 |
|
---|
935 | rc = CFGMR3QueryStringAlloc(pCfg, "TAPTerminateApplication", &pThis->pszTerminateApplication);
|
---|
936 | if (RT_SUCCESS(rc))
|
---|
937 | {
|
---|
938 | if (!RTPathExists(pThis->pszTerminateApplication))
|
---|
939 | return PDMDrvHlpVMSetError(pDrvIns, VERR_HOSTIF_INIT_FAILED, RT_SRC_POS,
|
---|
940 | N_("Invalid TAP terminate program path: %s"), pThis->pszTerminateApplication);
|
---|
941 | }
|
---|
942 | else if (rc != VERR_CFGM_VALUE_NOT_FOUND)
|
---|
943 | return PDMDRV_SET_ERROR(pDrvIns, rc, N_("Configuration error: failed to query \"TAPTerminateApplication\""));
|
---|
944 |
|
---|
945 | # ifdef VBOX_WITH_CROSSBOW
|
---|
946 | rc = CFGMR3QueryBytes(pCfg, "MAC", &pThis->MacAddress, sizeof(pThis->MacAddress));
|
---|
947 | if (RT_FAILURE(rc))
|
---|
948 | return PDMDRV_SET_ERROR(pDrvIns, rc, N_("Configuration error: Failed to query \"MAC\""));
|
---|
949 | # endif
|
---|
950 |
|
---|
951 | rc = CFGMR3QueryStringAlloc(pCfg, "Device", &pThis->pszDeviceName);
|
---|
952 | if (RT_FAILURE(rc))
|
---|
953 | pThis->fStatic = false;
|
---|
954 |
|
---|
955 | /* Obtain the device name from the setup application (if none was specified). */
|
---|
956 | if (pThis->pszSetupApplication)
|
---|
957 | {
|
---|
958 | rc = drvTAPSetupApplication(pThis);
|
---|
959 | if (RT_FAILURE(rc))
|
---|
960 | return PDMDrvHlpVMSetError(pDrvIns, VERR_HOSTIF_INIT_FAILED, RT_SRC_POS,
|
---|
961 | N_("Error running TAP setup application. rc=%d"), rc);
|
---|
962 | }
|
---|
963 |
|
---|
964 | /*
|
---|
965 | * Do the setup.
|
---|
966 | */
|
---|
967 | # ifdef VBOX_WITH_CROSSBOW
|
---|
968 | if (!VBoxLibDlpiFound())
|
---|
969 | {
|
---|
970 | return PDMDrvHlpVMSetError(pDrvIns, VERR_HOSTIF_INIT_FAILED, RT_SRC_POS,
|
---|
971 | N_("Failed to load library %s required for host interface networking."), LIB_DLPI);
|
---|
972 | }
|
---|
973 | rc = SolarisOpenVNIC(pThis);
|
---|
974 | # else
|
---|
975 | rc = SolarisTAPAttach(pThis);
|
---|
976 | # endif
|
---|
977 | if (RT_FAILURE(rc))
|
---|
978 | return rc;
|
---|
979 |
|
---|
980 | #else /* !RT_OS_SOLARIS */
|
---|
981 |
|
---|
982 | int32_t iFile;
|
---|
983 | rc = CFGMR3QueryS32(pCfg, "FileHandle", &iFile);
|
---|
984 | if (RT_FAILURE(rc))
|
---|
985 | return PDMDRV_SET_ERROR(pDrvIns, rc,
|
---|
986 | N_("Configuration error: Query for \"FileHandle\" 32-bit signed integer failed"));
|
---|
987 | pThis->FileDevice = (RTFILE)iFile;
|
---|
988 | if (!RTFileIsValid(pThis->FileDevice))
|
---|
989 | return PDMDrvHlpVMSetError(pDrvIns, VERR_INVALID_HANDLE, RT_SRC_POS,
|
---|
990 | N_("The TAP file handle %RTfile is not valid"), pThis->FileDevice);
|
---|
991 | #endif /* !RT_OS_SOLARIS */
|
---|
992 |
|
---|
993 | /*
|
---|
994 | * Make sure the descriptor is non-blocking and valid.
|
---|
995 | *
|
---|
996 | * We should actually query if it's a TAP device, but I haven't
|
---|
997 | * found any way to do that.
|
---|
998 | */
|
---|
999 | if (fcntl(pThis->FileDevice, F_SETFL, O_NONBLOCK) == -1)
|
---|
1000 | return PDMDrvHlpVMSetError(pDrvIns, VERR_HOSTIF_IOCTL, RT_SRC_POS,
|
---|
1001 | N_("Configuration error: Failed to configure /dev/net/tun. errno=%d"), errno);
|
---|
1002 | /** @todo determine device name. This can be done by reading the link /proc/<pid>/fd/<fd> */
|
---|
1003 | Log(("drvTAPContruct: %d (from fd)\n", pThis->FileDevice));
|
---|
1004 | rc = VINF_SUCCESS;
|
---|
1005 |
|
---|
1006 | /*
|
---|
1007 | * Create the control pipe.
|
---|
1008 | */
|
---|
1009 | int fds[2];
|
---|
1010 | #ifdef RT_OS_L4
|
---|
1011 | /* XXX We need to tell the library which interface we are using */
|
---|
1012 | fds[0] = vboxrtLinuxFd2VBoxFd(VBOXRT_FT_TAP, 0);
|
---|
1013 | #endif
|
---|
1014 | if (pipe(&fds[0]) != 0) /** @todo RTPipeCreate() or something... */
|
---|
1015 | {
|
---|
1016 | rc = RTErrConvertFromErrno(errno);
|
---|
1017 | AssertRC(rc);
|
---|
1018 | return rc;
|
---|
1019 | }
|
---|
1020 | pThis->PipeRead = fds[0];
|
---|
1021 | pThis->PipeWrite = fds[1];
|
---|
1022 |
|
---|
1023 | /*
|
---|
1024 | * Create the async I/O thread.
|
---|
1025 | */
|
---|
1026 | rc = PDMDrvHlpPDMThreadCreate(pDrvIns, &pThis->pThread, pThis, drvTAPAsyncIoThread, drvTapAsyncIoWakeup, 128 * _1K, RTTHREADTYPE_IO, "TAP");
|
---|
1027 | AssertRCReturn(rc, rc);
|
---|
1028 |
|
---|
1029 | #ifdef VBOX_WITH_STATISTICS
|
---|
1030 | /*
|
---|
1031 | * Statistics.
|
---|
1032 | */
|
---|
1033 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatPktSent, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of sent packets.", "/Drivers/TAP%d/Packets/Sent", pDrvIns->iInstance);
|
---|
1034 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatPktSentBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Number of sent bytes.", "/Drivers/TAP%d/Bytes/Sent", pDrvIns->iInstance);
|
---|
1035 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatPktRecv, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of received packets.", "/Drivers/TAP%d/Packets/Received", pDrvIns->iInstance);
|
---|
1036 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatPktRecvBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Number of received bytes.", "/Drivers/TAP%d/Bytes/Received", pDrvIns->iInstance);
|
---|
1037 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatTransmit, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling packet transmit runs.", "/Drivers/TAP%d/Transmit", pDrvIns->iInstance);
|
---|
1038 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatReceive, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling packet receive runs.", "/Drivers/TAP%d/Receive", pDrvIns->iInstance);
|
---|
1039 | #endif /* VBOX_WITH_STATISTICS */
|
---|
1040 |
|
---|
1041 | return rc;
|
---|
1042 | }
|
---|
1043 |
|
---|
1044 |
|
---|
1045 | /**
|
---|
1046 | * TAP network transport driver registration record.
|
---|
1047 | */
|
---|
1048 | const PDMDRVREG g_DrvHostInterface =
|
---|
1049 | {
|
---|
1050 | /* u32Version */
|
---|
1051 | PDM_DRVREG_VERSION,
|
---|
1052 | /* szName */
|
---|
1053 | "HostInterface",
|
---|
1054 | /* szRCMod */
|
---|
1055 | "",
|
---|
1056 | /* szR0Mod */
|
---|
1057 | "",
|
---|
1058 | /* pszDescription */
|
---|
1059 | "TAP Network Transport Driver",
|
---|
1060 | /* fFlags */
|
---|
1061 | PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
|
---|
1062 | /* fClass. */
|
---|
1063 | PDM_DRVREG_CLASS_NETWORK,
|
---|
1064 | /* cMaxInstances */
|
---|
1065 | ~0,
|
---|
1066 | /* cbInstance */
|
---|
1067 | sizeof(DRVTAP),
|
---|
1068 | /* pfnConstruct */
|
---|
1069 | drvTAPConstruct,
|
---|
1070 | /* pfnDestruct */
|
---|
1071 | drvTAPDestruct,
|
---|
1072 | /* pfnRelocate */
|
---|
1073 | NULL,
|
---|
1074 | /* pfnIOCtl */
|
---|
1075 | NULL,
|
---|
1076 | /* pfnPowerOn */
|
---|
1077 | NULL,
|
---|
1078 | /* pfnReset */
|
---|
1079 | NULL,
|
---|
1080 | /* pfnSuspend */
|
---|
1081 | NULL, /** @todo Do power on, suspend and resume handlers! */
|
---|
1082 | /* pfnResume */
|
---|
1083 | NULL,
|
---|
1084 | /* pfnAttach */
|
---|
1085 | NULL,
|
---|
1086 | /* pfnDetach */
|
---|
1087 | NULL,
|
---|
1088 | /* pfnPowerOff */
|
---|
1089 | NULL,
|
---|
1090 | /* pfnSoftReset */
|
---|
1091 | NULL,
|
---|
1092 | /* u32EndVersion */
|
---|
1093 | PDM_DRVREG_VERSION
|
---|
1094 | };
|
---|
1095 |
|
---|