1 | /* $Id: DevINIP.cpp 17802 2009-03-13 10:36:35Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * DevINIP - Internal Network IP stack device/service.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007-2009 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 | /*******************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 | #define LOG_GROUP LOG_GROUP_DEV_INIP
|
---|
27 | #include <iprt/cdefs.h> /* include early to allow __BEGIN_DECLS hack */
|
---|
28 | #include <iprt/mem.h> /* include anything of ours that the lwip headers use. */
|
---|
29 | #include <iprt/semaphore.h>
|
---|
30 | #include <iprt/thread.h>
|
---|
31 | #include <iprt/alloca.h>
|
---|
32 | /* All lwip header files are not C++ safe. So hack around this. */
|
---|
33 | __BEGIN_DECLS
|
---|
34 | #include "lwip/sys.h"
|
---|
35 | #include "lwip/stats.h"
|
---|
36 | #include "lwip/mem.h"
|
---|
37 | #include "lwip/memp.h"
|
---|
38 | #include "lwip/pbuf.h"
|
---|
39 | #include "lwip/netif.h"
|
---|
40 | #include "ipv4/lwip/ip.h"
|
---|
41 | #include "lwip/udp.h"
|
---|
42 | #include "lwip/tcp.h"
|
---|
43 | #include "lwip/tcpip.h"
|
---|
44 | #include "lwip/sockets.h"
|
---|
45 | #include "netif/etharp.h"
|
---|
46 | __END_DECLS
|
---|
47 | #include <VBox/pdmdev.h>
|
---|
48 | #include <VBox/tm.h>
|
---|
49 | #include <iprt/string.h>
|
---|
50 | #include <iprt/assert.h>
|
---|
51 |
|
---|
52 | #include "../Builtins.h"
|
---|
53 |
|
---|
54 |
|
---|
55 | /*******************************************************************************
|
---|
56 | * Macros and Defines *
|
---|
57 | *******************************************************************************/
|
---|
58 |
|
---|
59 | /** Maximum frame size this device can handle. */
|
---|
60 | #define DEVINIP_MAX_FRAME 1514
|
---|
61 |
|
---|
62 |
|
---|
63 | /*******************************************************************************
|
---|
64 | * Structures and Typedefs *
|
---|
65 | *******************************************************************************/
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Internal Network IP stack device instance data.
|
---|
69 | */
|
---|
70 | typedef struct DEVINTNETIP
|
---|
71 | {
|
---|
72 | /** The base interface. */
|
---|
73 | PDMIBASE IBase;
|
---|
74 | /** The network port this device provides. */
|
---|
75 | PDMINETWORKPORT INetworkPort;
|
---|
76 | /** The base interface of the network driver below us. */
|
---|
77 | PPDMIBASE pDrvBase;
|
---|
78 | /** The connector of the network driver below us. */
|
---|
79 | PPDMINETWORKCONNECTOR pDrv;
|
---|
80 | /** Pointer to the device instance. */
|
---|
81 | PPDMDEVINSR3 pDevIns;
|
---|
82 | /** MAC adress. */
|
---|
83 | RTMAC MAC;
|
---|
84 | /** Static IP address of the interface. */
|
---|
85 | char *pszIP;
|
---|
86 | /** Netmask of the interface. */
|
---|
87 | char *pszNetmask;
|
---|
88 | /** Gateway for the interface. */
|
---|
89 | char *pszGateway;
|
---|
90 | /** lwIP network interface description. */
|
---|
91 | struct netif IntNetIF;
|
---|
92 | /** lwIP ARP timer. */
|
---|
93 | PTMTIMERR3 ARPTimer;
|
---|
94 | /** lwIP TCP fast timer. */
|
---|
95 | PTMTIMERR3 TCPFastTimer;
|
---|
96 | /** lwIP TCP slow timer. */
|
---|
97 | PTMTIMERR3 TCPSlowTimer;
|
---|
98 | /** lwIP semaphore to coordinate TCPIP init/terminate. */
|
---|
99 | sys_sem_t LWIPTcpInitSem;
|
---|
100 | /** hack: get linking right. remove this eventually, once the device
|
---|
101 | * provides a proper interface to all IP stack functions. */
|
---|
102 | const void *pLinkHack;
|
---|
103 | } DEVINTNETIP, *PDEVINTNETIP;
|
---|
104 |
|
---|
105 |
|
---|
106 | /*******************************************************************************
|
---|
107 | * Global Variables *
|
---|
108 | *******************************************************************************/
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Pointer to the (only) instance data in this device.
|
---|
112 | */
|
---|
113 | static PDEVINTNETIP g_pDevINIPData = NULL;
|
---|
114 |
|
---|
115 | /*
|
---|
116 | * really ugly hack to avoid linking problems on unix style platforms
|
---|
117 | * using .a libraries for now.
|
---|
118 | */
|
---|
119 | static const PFNRT g_pDevINILinkHack[] =
|
---|
120 | {
|
---|
121 | (PFNRT)lwip_socket,
|
---|
122 | (PFNRT)lwip_close,
|
---|
123 | (PFNRT)lwip_setsockopt,
|
---|
124 | (PFNRT)lwip_recv,
|
---|
125 | (PFNRT)lwip_send,
|
---|
126 | (PFNRT)lwip_select
|
---|
127 | };
|
---|
128 |
|
---|
129 |
|
---|
130 | /*******************************************************************************
|
---|
131 | * Internal Functions *
|
---|
132 | *******************************************************************************/
|
---|
133 | static DECLCALLBACK(void) devINIPARPTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer);
|
---|
134 | static DECLCALLBACK(void) devINIPTCPFastTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer);
|
---|
135 | static DECLCALLBACK(void) devINIPTCPSlowTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer);
|
---|
136 | static DECLCALLBACK(err_t) devINIPOutput(struct netif *netif, struct pbuf *p,
|
---|
137 | struct ip_addr *ipaddr);
|
---|
138 | static DECLCALLBACK(err_t) devINIPOutputRaw(struct netif *netif,
|
---|
139 | struct pbuf *p);
|
---|
140 | static DECLCALLBACK(err_t) devINIPInterface(struct netif *netif);
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * ARP cache timeout handling for lwIP.
|
---|
144 | *
|
---|
145 | * @param pDevIns Device instance.
|
---|
146 | * @param pTimer Pointer to timer.
|
---|
147 | */
|
---|
148 | static DECLCALLBACK(void) devINIPARPTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer)
|
---|
149 | {
|
---|
150 | PDEVINTNETIP pThis = PDMINS_2_DATA(pDevIns, PDEVINTNETIP);
|
---|
151 | LogFlow(("%s: pDevIns=%p pTimer=%p\n", __FUNCTION__, pDevIns, pTimer));
|
---|
152 | lwip_etharp_tmr();
|
---|
153 | TMTimerSetMillies(pThis->ARPTimer, ARP_TMR_INTERVAL);
|
---|
154 | LogFlow(("%s: return\n", __FUNCTION__));
|
---|
155 | }
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * TCP fast timer handling for lwIP.
|
---|
159 | *
|
---|
160 | * @param pDevIns Device instance.
|
---|
161 | * @param pTimer Pointer to timer.
|
---|
162 | */
|
---|
163 | static DECLCALLBACK(void) devINIPTCPFastTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer)
|
---|
164 | {
|
---|
165 | PDEVINTNETIP pThis = PDMINS_2_DATA(pDevIns, PDEVINTNETIP);
|
---|
166 | LogFlow(("%s: pDevIns=%p pTimer=%p\n", __FUNCTION__, pDevIns, pTimer));
|
---|
167 | lwip_tcp_fasttmr();
|
---|
168 | TMTimerSetMillies(pThis->TCPFastTimer, TCP_FAST_INTERVAL);
|
---|
169 | LogFlow(("%s: return\n", __FUNCTION__));
|
---|
170 | }
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * TCP slow timer handling for lwIP.
|
---|
174 | *
|
---|
175 | * @param pDevIns Device instance.
|
---|
176 | * @param pTimer Pointer to timer.
|
---|
177 | */
|
---|
178 | static DECLCALLBACK(void) devINIPTCPSlowTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer)
|
---|
179 | {
|
---|
180 | PDEVINTNETIP pThis = PDMINS_2_DATA(pDevIns, PDEVINTNETIP);
|
---|
181 | LogFlow(("%s: pDevIns=%p pTimer=%p\n", __FUNCTION__, pDevIns, pTimer));
|
---|
182 | lwip_tcp_slowtmr();
|
---|
183 | TMTimerSetMillies(pThis->TCPSlowTimer, TCP_SLOW_INTERVAL);
|
---|
184 | LogFlow(("%s: return\n", __FUNCTION__));
|
---|
185 | }
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * Output a TCP/IP packet on the interface. Uses the generic lwIP ARP
|
---|
189 | * code to resolve the address and call the link-level packet function.
|
---|
190 | *
|
---|
191 | * @returns lwIP error code
|
---|
192 | * @param netif Interface on which to send IP packet.
|
---|
193 | * @param p Packet data.
|
---|
194 | * @param ipaddr Destination IP address.
|
---|
195 | */
|
---|
196 | static DECLCALLBACK(err_t) devINIPOutput(struct netif *netif, struct pbuf *p,
|
---|
197 | struct ip_addr *ipaddr)
|
---|
198 | {
|
---|
199 | err_t lrc;
|
---|
200 | LogFlow(("%s: netif=%p p=%p ipaddr=%#04x\n", __FUNCTION__, netif, p,
|
---|
201 | ipaddr->addr));
|
---|
202 | lrc = lwip_etharp_output(netif, ipaddr, p);
|
---|
203 | LogFlow(("%s: return %d\n", __FUNCTION__, lrc));
|
---|
204 | return lrc;
|
---|
205 | }
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Output a raw packet on the interface.
|
---|
209 | *
|
---|
210 | * @returns lwIP error code
|
---|
211 | * @param netif Interface on which to send frame.
|
---|
212 | * @param p Frame data.
|
---|
213 | */
|
---|
214 | static DECLCALLBACK(err_t) devINIPOutputRaw(struct netif *netif,
|
---|
215 | struct pbuf *p)
|
---|
216 | {
|
---|
217 | uint8_t aFrame[DEVINIP_MAX_FRAME], *pbBuf;
|
---|
218 | size_t cbBuf;
|
---|
219 | struct pbuf *q;
|
---|
220 | int rc = VINF_SUCCESS;
|
---|
221 |
|
---|
222 | LogFlow(("%s: netif=%p p=%p\n", __FUNCTION__, netif, p));
|
---|
223 | Assert(g_pDevINIPData);
|
---|
224 | Assert(g_pDevINIPData->pDrv);
|
---|
225 |
|
---|
226 | /* Silently ignore packets being sent while lwIP isn't set up. */
|
---|
227 | if (!g_pDevINIPData)
|
---|
228 | goto out;
|
---|
229 |
|
---|
230 | #if ETH_PAD_SIZE
|
---|
231 | lwip_pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
|
---|
232 | #endif
|
---|
233 |
|
---|
234 | pbBuf = &aFrame[0];
|
---|
235 | cbBuf = 0;
|
---|
236 | for (q = p; q != NULL; q = q->next)
|
---|
237 | {
|
---|
238 | if (cbBuf + q->len <= DEVINIP_MAX_FRAME)
|
---|
239 | {
|
---|
240 | memcpy(pbBuf, q->payload, q->len);
|
---|
241 | pbBuf += q->len;
|
---|
242 | cbBuf += q->len;
|
---|
243 | }
|
---|
244 | else
|
---|
245 | {
|
---|
246 | LogRel(("INIP: exceeded frame size\n"));
|
---|
247 | break;
|
---|
248 | }
|
---|
249 | }
|
---|
250 | if (cbBuf)
|
---|
251 | rc = g_pDevINIPData->pDrv->pfnSend(g_pDevINIPData->pDrv,
|
---|
252 | &aFrame[0], cbBuf);
|
---|
253 |
|
---|
254 | #if ETH_PAD_SIZE
|
---|
255 | lwip_pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
|
---|
256 | #endif
|
---|
257 |
|
---|
258 | out:
|
---|
259 | err_t lrc = ERR_OK;
|
---|
260 | if (RT_FAILURE(rc))
|
---|
261 | lrc = ERR_IF;
|
---|
262 | LogFlow(("%s: return %d (vbox: %Rrc)\n", __FUNCTION__, rc, lrc));
|
---|
263 | return lrc;
|
---|
264 | }
|
---|
265 |
|
---|
266 | /**
|
---|
267 | * Implements the ethernet interface backend initialization for lwIP.
|
---|
268 | *
|
---|
269 | * @returns lwIP error code
|
---|
270 | * @param netif Interface to configure.
|
---|
271 | */
|
---|
272 | static DECLCALLBACK(err_t) devINIPInterface(struct netif *netif)
|
---|
273 | {
|
---|
274 | LogFlow(("%s: netif=%p\n", __FUNCTION__, netif));
|
---|
275 | Assert(g_pDevINIPData != NULL);
|
---|
276 | netif->state = g_pDevINIPData;
|
---|
277 | netif->hwaddr_len = sizeof(g_pDevINIPData->MAC);
|
---|
278 | memcpy(netif->hwaddr, &g_pDevINIPData->MAC, sizeof(g_pDevINIPData->MAC));
|
---|
279 | netif->mtu = DEVINIP_MAX_FRAME;
|
---|
280 | netif->flags = NETIF_FLAG_BROADCAST;
|
---|
281 | netif->output = devINIPOutput;
|
---|
282 | netif->linkoutput = devINIPOutputRaw;
|
---|
283 |
|
---|
284 | lwip_etharp_init();
|
---|
285 | TMTimerSetMillies(g_pDevINIPData->ARPTimer, ARP_TMR_INTERVAL);
|
---|
286 | LogFlow(("%s: success\n", __FUNCTION__));
|
---|
287 | return ERR_OK;
|
---|
288 | }
|
---|
289 |
|
---|
290 | /**
|
---|
291 | * Wait until data can be received.
|
---|
292 | *
|
---|
293 | * @returns VBox status code. VINF_SUCCESS means there is at least one receive descriptor available.
|
---|
294 | * @param pInterface PDM network port interface pointer.
|
---|
295 | * @param cMillies Number of milliseconds to wait. 0 means return immediately.
|
---|
296 | */
|
---|
297 | static DECLCALLBACK(int) devINIPWaitInputAvail(PPDMINETWORKPORT pInterface, unsigned cMillies)
|
---|
298 | {
|
---|
299 | LogFlow(("%s: pInterface=%p\n", __FUNCTION__, pInterface));
|
---|
300 | LogFlow(("%s: return VINF_SUCCESS\n", __FUNCTION__));
|
---|
301 | return VINF_SUCCESS;
|
---|
302 | }
|
---|
303 |
|
---|
304 | /**
|
---|
305 | * Receive data and pass it to lwIP for processing.
|
---|
306 | *
|
---|
307 | * @returns VBox status code
|
---|
308 | * @param pInterface PDM network port interface pointer.
|
---|
309 | * @param pvBuf Pointer to frame data.
|
---|
310 | * @param cb Frame size.
|
---|
311 | */
|
---|
312 | static DECLCALLBACK(int) devINIPInput(PPDMINETWORKPORT pInterface,
|
---|
313 | const void *pvBuf, size_t cb)
|
---|
314 | {
|
---|
315 | const uint8_t *pbBuf = (const uint8_t *)pvBuf;
|
---|
316 | size_t len = cb;
|
---|
317 | const struct eth_hdr *ethhdr;
|
---|
318 | struct pbuf *p, *q;
|
---|
319 | int rc = VINF_SUCCESS;
|
---|
320 |
|
---|
321 | LogFlow(("%s: pInterface=%p pvBuf=%p cb=%lu\n", __FUNCTION__, pInterface,
|
---|
322 | pvBuf, cb));
|
---|
323 | Assert(g_pDevINIPData);
|
---|
324 | Assert(g_pDevINIPData->pDrv);
|
---|
325 |
|
---|
326 | /* Silently ignore packets being received while lwIP isn't set up. */
|
---|
327 | if (!g_pDevINIPData)
|
---|
328 | goto out;
|
---|
329 |
|
---|
330 | #if ETH_PAD_SIZE
|
---|
331 | len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
|
---|
332 | #endif
|
---|
333 |
|
---|
334 | /* We allocate a pbuf chain of pbufs from the pool. */
|
---|
335 | p = lwip_pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
|
---|
336 | if (p != NULL)
|
---|
337 | {
|
---|
338 | #if ETH_PAD_SIZE
|
---|
339 | lwip_pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
|
---|
340 | #endif
|
---|
341 |
|
---|
342 | for (q = p; q != NULL; q = q->next)
|
---|
343 | {
|
---|
344 | /* Fill the buffers, and clean out unused buffer space. */
|
---|
345 | memcpy(q->payload, pbBuf, RT_MIN(cb, q->len));
|
---|
346 | pbBuf += RT_MIN(cb, q->len);
|
---|
347 | if (q->len > cb)
|
---|
348 | memset(((uint8_t *)q->payload) + cb, '\0', q->len - cb);
|
---|
349 | cb -= RT_MIN(cb, q->len);
|
---|
350 | }
|
---|
351 |
|
---|
352 | ethhdr = (const struct eth_hdr *)p->payload;
|
---|
353 | struct netif *iface = &g_pDevINIPData->IntNetIF;
|
---|
354 | err_t lrc;
|
---|
355 | switch (htons(ethhdr->type))
|
---|
356 | {
|
---|
357 | case ETHTYPE_IP: /* IP packet */
|
---|
358 | lwip_pbuf_header(p, -(ssize_t)sizeof(struct eth_hdr));
|
---|
359 | lrc = iface->input(p, iface);
|
---|
360 | if (lrc)
|
---|
361 | rc = VERR_NET_IO_ERROR;
|
---|
362 | break;
|
---|
363 | case ETHTYPE_ARP: /* ARP packet */
|
---|
364 | lwip_etharp_arp_input(iface, (struct eth_addr *)iface->hwaddr, p);
|
---|
365 | break;
|
---|
366 | default:
|
---|
367 | lwip_pbuf_free(p);
|
---|
368 | }
|
---|
369 | }
|
---|
370 |
|
---|
371 | out:
|
---|
372 | LogFlow(("%s: return %Rrc\n", __FUNCTION__, rc));
|
---|
373 | return rc;
|
---|
374 | }
|
---|
375 |
|
---|
376 | /**
|
---|
377 | * Signals the end of lwIP TCPIP initialization.
|
---|
378 | *
|
---|
379 | * @param arg opaque argument, here the pointer to the semaphore.
|
---|
380 | */
|
---|
381 | static DECLCALLBACK(void) devINIPTcpipInitDone(void *arg)
|
---|
382 | {
|
---|
383 | sys_sem_t *sem = (sys_sem_t *)arg;
|
---|
384 | lwip_sys_sem_signal(*sem);
|
---|
385 | }
|
---|
386 |
|
---|
387 |
|
---|
388 | /**
|
---|
389 | * Queries an interface to the device.
|
---|
390 | *
|
---|
391 | * @returns Pointer to interface.
|
---|
392 | * @returns NULL if the interface was not supported by the device.
|
---|
393 | * @param pInterface Pointer to this interface structure.
|
---|
394 | * @param enmInterface The requested interface identification.
|
---|
395 | * @thread Any thread.
|
---|
396 | */
|
---|
397 | static DECLCALLBACK(void *) devINIPQueryInterface(PPDMIBASE pInterface,
|
---|
398 | PDMINTERFACE enmInterface)
|
---|
399 | {
|
---|
400 | PDEVINTNETIP pThis = (PDEVINTNETIP)((uintptr_t)pInterface - RT_OFFSETOF(DEVINTNETIP, IBase));
|
---|
401 | switch (enmInterface)
|
---|
402 | {
|
---|
403 | case PDMINTERFACE_BASE:
|
---|
404 | return &pThis->IBase;
|
---|
405 | case PDMINTERFACE_NETWORK_PORT:
|
---|
406 | return &pThis->INetworkPort;
|
---|
407 | default:
|
---|
408 | return NULL;
|
---|
409 | }
|
---|
410 | }
|
---|
411 |
|
---|
412 |
|
---|
413 | /**
|
---|
414 | * Construct an internal networking IP stack device instance.
|
---|
415 | *
|
---|
416 | * @returns VBox status.
|
---|
417 | * @param pDevIns The device instance data.
|
---|
418 | * If the registration structure is needed, pDevIns->pDevReg points to it.
|
---|
419 | * @param iInstance Instance number. Use this to figure out which registers
|
---|
420 | * and such to use.
|
---|
421 | * he device number is also found in pDevIns->iInstance, but since it's
|
---|
422 | * likely to be freqently used PDM passes it as parameter.
|
---|
423 | * @param pCfgHandle Configuration node handle for the device. Use this to obtain the configuration
|
---|
424 | * of the device instance. It's also found in pDevIns->pCfgHandle, but like
|
---|
425 | * iInstance it's expected to be used a bit in this function.
|
---|
426 | */
|
---|
427 | static DECLCALLBACK(int) devINIPConstruct(PPDMDEVINS pDevIns, int iInstance,
|
---|
428 | PCFGMNODE pCfgHandle)
|
---|
429 | {
|
---|
430 | PDEVINTNETIP pThis = PDMINS_2_DATA(pDevIns, PDEVINTNETIP);
|
---|
431 | int rc = VINF_SUCCESS;
|
---|
432 | LogFlow(("%s: pDevIns=%p iInstance=%d pCfgHandle=%p\n", __FUNCTION__,
|
---|
433 | pDevIns, iInstance, pCfgHandle));
|
---|
434 |
|
---|
435 | Assert(iInstance == 0);
|
---|
436 |
|
---|
437 | /*
|
---|
438 | * Validate the config.
|
---|
439 | */
|
---|
440 | if (!CFGMR3AreValuesValid(pCfgHandle, "MAC\0IP\0Netmask\0Gateway\0"))
|
---|
441 | {
|
---|
442 | rc = PDMDEV_SET_ERROR(pDevIns, VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES,
|
---|
443 | N_("Unknown Internal Networking IP configuration option"));
|
---|
444 | goto out;
|
---|
445 | }
|
---|
446 |
|
---|
447 | /*
|
---|
448 | * Init the static parts.
|
---|
449 | */
|
---|
450 | pThis->pszIP = NULL;
|
---|
451 | pThis->pszNetmask = NULL;
|
---|
452 | pThis->pszGateway = NULL;
|
---|
453 | /* Pointer to device instance */
|
---|
454 | pThis->pDevIns = pDevIns;
|
---|
455 | /* IBase */
|
---|
456 | pThis->IBase.pfnQueryInterface = devINIPQueryInterface;
|
---|
457 | /* INetworkPort */
|
---|
458 | pThis->INetworkPort.pfnWaitReceiveAvail = devINIPWaitInputAvail;
|
---|
459 | pThis->INetworkPort.pfnReceive = devINIPInput;
|
---|
460 |
|
---|
461 | /*
|
---|
462 | * Get the configuration settings.
|
---|
463 | */
|
---|
464 | rc = CFGMR3QueryBytes(pCfgHandle, "MAC", &pThis->MAC, sizeof(pThis->MAC));
|
---|
465 | if (rc == VERR_CFGM_NOT_BYTES)
|
---|
466 | {
|
---|
467 | char szMAC[64];
|
---|
468 | rc = CFGMR3QueryString(pCfgHandle, "MAC", &szMAC[0], sizeof(szMAC));
|
---|
469 | if (RT_SUCCESS(rc))
|
---|
470 | {
|
---|
471 | char *macStr = &szMAC[0];
|
---|
472 | char *pMac = (char *)&pThis->MAC;
|
---|
473 | for (uint32_t i = 0; i < 6; i++)
|
---|
474 | {
|
---|
475 | if ( !*macStr || !*(macStr + 1)
|
---|
476 | || *macStr == ':' || *(macStr + 1) == ':')
|
---|
477 | {
|
---|
478 | rc = PDMDEV_SET_ERROR(pDevIns,
|
---|
479 | VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES,
|
---|
480 | N_("Configuration error: Invalid \"MAC\" value"));
|
---|
481 | goto out;
|
---|
482 | }
|
---|
483 | char c1 = *macStr++ - '0';
|
---|
484 | if (c1 > 9)
|
---|
485 | c1 -= 7;
|
---|
486 | char c2 = *macStr++ - '0';
|
---|
487 | if (c2 > 9)
|
---|
488 | c2 -= 7;
|
---|
489 | *pMac++ = ((c1 & 0x0f) << 4) | (c2 & 0x0f);
|
---|
490 | if (i != 5 && *macStr == ':')
|
---|
491 | macStr++;
|
---|
492 | }
|
---|
493 | }
|
---|
494 | }
|
---|
495 | if (RT_FAILURE(rc))
|
---|
496 | {
|
---|
497 | PDMDEV_SET_ERROR(pDevIns, rc,
|
---|
498 | N_("Configuration error: Failed to get the \"MAC\" value"));
|
---|
499 | goto out;
|
---|
500 | }
|
---|
501 | rc = CFGMR3QueryStringAlloc(pCfgHandle, "IP", &pThis->pszIP);
|
---|
502 | if (RT_FAILURE(rc))
|
---|
503 | {
|
---|
504 | PDMDEV_SET_ERROR(pDevIns, rc,
|
---|
505 | N_("Configuration error: Failed to get the \"IP\" value"));
|
---|
506 | goto out;
|
---|
507 | }
|
---|
508 | rc = CFGMR3QueryStringAlloc(pCfgHandle, "Netmask", &pThis->pszNetmask);
|
---|
509 | if (RT_FAILURE(rc))
|
---|
510 | {
|
---|
511 | PDMDEV_SET_ERROR(pDevIns, rc,
|
---|
512 | N_("Configuration error: Failed to get the \"Netmask\" value"));
|
---|
513 | goto out;
|
---|
514 | }
|
---|
515 | rc = CFGMR3QueryStringAlloc(pCfgHandle, "Gateway", &pThis->pszGateway);
|
---|
516 | if (rc == VERR_CFGM_VALUE_NOT_FOUND)
|
---|
517 | rc = VINF_SUCCESS;
|
---|
518 | if (RT_FAILURE(rc))
|
---|
519 | {
|
---|
520 | PDMDEV_SET_ERROR(pDevIns, rc,
|
---|
521 | N_("Configuration error: Failed to get the \"Gateway\" value"));
|
---|
522 | goto out;
|
---|
523 | }
|
---|
524 |
|
---|
525 | /*
|
---|
526 | * Attach driver and query the network connector interface.
|
---|
527 | */
|
---|
528 | rc = PDMDevHlpDriverAttach(pDevIns, 0, &pThis->IBase, &pThis->pDrvBase,
|
---|
529 | "Network Port");
|
---|
530 | if (RT_FAILURE(rc))
|
---|
531 | {
|
---|
532 | pThis->pDrvBase = NULL;
|
---|
533 | pThis->pDrv = NULL;
|
---|
534 | goto out;
|
---|
535 | }
|
---|
536 | else
|
---|
537 | {
|
---|
538 | pThis->pDrv = (PPDMINETWORKCONNECTOR)pThis->pDrvBase->pfnQueryInterface(pThis->pDrvBase, PDMINTERFACE_NETWORK_CONNECTOR);
|
---|
539 | if (!pThis->pDrv)
|
---|
540 | {
|
---|
541 | AssertMsgFailed(("Failed to obtain the PDMINTERFACE_NETWORK_CONNECTOR interface!\n"));
|
---|
542 | rc = VERR_PDM_MISSING_INTERFACE_BELOW;
|
---|
543 | goto out;
|
---|
544 | }
|
---|
545 | }
|
---|
546 |
|
---|
547 | struct ip_addr ipaddr, netmask, gw;
|
---|
548 | struct in_addr ip;
|
---|
549 |
|
---|
550 | if (!inet_aton(pThis->pszIP, &ip))
|
---|
551 | {
|
---|
552 | rc = PDMDEV_SET_ERROR(pDevIns, VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES,
|
---|
553 | N_("Configuration error: Invalid \"IP\" value"));
|
---|
554 | goto out;
|
---|
555 | }
|
---|
556 | memcpy(&ipaddr, &ip, sizeof(ipaddr));
|
---|
557 | if (!inet_aton(pThis->pszNetmask, &ip))
|
---|
558 | {
|
---|
559 | rc = PDMDEV_SET_ERROR(pDevIns, VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES,
|
---|
560 | N_("Configuration error: Invalid \"Netmask\" value"));
|
---|
561 | goto out;
|
---|
562 | }
|
---|
563 | memcpy(&netmask, &ip, sizeof(netmask));
|
---|
564 | if (pThis->pszGateway)
|
---|
565 | {
|
---|
566 | if (!inet_aton(pThis->pszGateway, &ip))
|
---|
567 | {
|
---|
568 | rc = PDMDEV_SET_ERROR(pDevIns,
|
---|
569 | VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES,
|
---|
570 | N_("Configuration error: Invalid \"Gateway\" value"));
|
---|
571 | goto out;
|
---|
572 | }
|
---|
573 | memcpy(&gw, &ip, sizeof(gw));
|
---|
574 | }
|
---|
575 | else
|
---|
576 | {
|
---|
577 | inet_aton(pThis->pszIP, &ip);
|
---|
578 | memcpy(&gw, &ip, sizeof(gw));
|
---|
579 | }
|
---|
580 |
|
---|
581 | /*
|
---|
582 | * Initialize lwIP.
|
---|
583 | */
|
---|
584 | lwip_stats_init();
|
---|
585 | lwip_sys_init();
|
---|
586 | #if MEM_LIBC_MALLOC == 0
|
---|
587 | lwip_mem_init();
|
---|
588 | #endif
|
---|
589 | lwip_memp_init();
|
---|
590 | lwip_pbuf_init();
|
---|
591 | lwip_netif_init();
|
---|
592 | rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL, devINIPARPTimer, "lwIP ARP", &pThis->ARPTimer);
|
---|
593 | if (RT_FAILURE(rc))
|
---|
594 | goto out;
|
---|
595 | rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL, devINIPTCPFastTimer, "lwIP fast TCP", &pThis->TCPFastTimer);
|
---|
596 | if (RT_FAILURE(rc))
|
---|
597 | goto out;
|
---|
598 | TMTimerSetMillies(pThis->TCPFastTimer, TCP_FAST_INTERVAL);
|
---|
599 | rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL, devINIPTCPSlowTimer, "lwIP slow TCP", &pThis->TCPSlowTimer);
|
---|
600 | if (RT_FAILURE(rc))
|
---|
601 | goto out;
|
---|
602 | TMTimerSetMillies(pThis->TCPFastTimer, TCP_SLOW_INTERVAL);
|
---|
603 | pThis->LWIPTcpInitSem = lwip_sys_sem_new(0);
|
---|
604 | {
|
---|
605 | lwip_tcpip_init(devINIPTcpipInitDone, &pThis->LWIPTcpInitSem);
|
---|
606 | lwip_sys_sem_wait(pThis->LWIPTcpInitSem);
|
---|
607 | }
|
---|
608 |
|
---|
609 | /*
|
---|
610 | * Set up global pointer to interface data.
|
---|
611 | */
|
---|
612 | g_pDevINIPData = pThis;
|
---|
613 |
|
---|
614 | struct netif *ret;
|
---|
615 | pThis->IntNetIF.name[0] = 'I';
|
---|
616 | pThis->IntNetIF.name[1] = 'N';
|
---|
617 | ret = netif_add(&pThis->IntNetIF, &ipaddr, &netmask, &gw, NULL,
|
---|
618 | devINIPInterface, lwip_tcpip_input);
|
---|
619 | if (!ret)
|
---|
620 | {
|
---|
621 | rc = VERR_NET_NO_NETWORK;
|
---|
622 | goto out;
|
---|
623 | }
|
---|
624 |
|
---|
625 | lwip_netif_set_default(&pThis->IntNetIF);
|
---|
626 | lwip_netif_set_up(&pThis->IntNetIF);
|
---|
627 |
|
---|
628 | /* link hack */
|
---|
629 | pThis->pLinkHack = g_pDevINILinkHack;
|
---|
630 |
|
---|
631 | out:
|
---|
632 | LogFlow(("%s: return %Rrc\n", __FUNCTION__, rc));
|
---|
633 | return rc;
|
---|
634 | }
|
---|
635 |
|
---|
636 |
|
---|
637 | /**
|
---|
638 | * Destruct a device instance.
|
---|
639 | *
|
---|
640 | * Most VM resources are freed by the VM. This callback is provided so that any non-VM
|
---|
641 | * resources can be freed correctly.
|
---|
642 | *
|
---|
643 | * @returns VBox status.
|
---|
644 | * @param pDevIns The device instance data.
|
---|
645 | */
|
---|
646 | static DECLCALLBACK(int) devINIPDestruct(PPDMDEVINS pDevIns)
|
---|
647 | {
|
---|
648 | PDEVINTNETIP pThis = PDMINS_2_DATA(pDevIns, PDEVINTNETIP);
|
---|
649 |
|
---|
650 | LogFlow(("%s: pDevIns=%p\n", __FUNCTION__, pDevIns));
|
---|
651 |
|
---|
652 | if (g_pDevINIPData != NULL)
|
---|
653 | {
|
---|
654 | netif_set_down(&pThis->IntNetIF);
|
---|
655 | netif_remove(&pThis->IntNetIF);
|
---|
656 | tcpip_terminate();
|
---|
657 | lwip_sys_sem_wait(pThis->LWIPTcpInitSem);
|
---|
658 | lwip_sys_sem_free(pThis->LWIPTcpInitSem);
|
---|
659 | }
|
---|
660 |
|
---|
661 | if (pThis->pszIP)
|
---|
662 | MMR3HeapFree(pThis->pszIP);
|
---|
663 | if (pThis->pszNetmask)
|
---|
664 | MMR3HeapFree(pThis->pszNetmask);
|
---|
665 | if (pThis->pszGateway)
|
---|
666 | MMR3HeapFree(pThis->pszGateway);
|
---|
667 |
|
---|
668 | LogFlow(("%s: success\n", __FUNCTION__));
|
---|
669 | return VINF_SUCCESS;
|
---|
670 | }
|
---|
671 |
|
---|
672 |
|
---|
673 | /**
|
---|
674 | * Query whether lwIP is initialized or not. Since there is only a single
|
---|
675 | * instance of this device ever for a VM, it can be a global function.
|
---|
676 | *
|
---|
677 | * @returns True if lwIP is initialized.
|
---|
678 | */
|
---|
679 | bool DevINIPConfigured(void)
|
---|
680 | {
|
---|
681 | return g_pDevINIPData != NULL;
|
---|
682 | }
|
---|
683 |
|
---|
684 |
|
---|
685 | /**
|
---|
686 | * Internal network IP stack device registration record.
|
---|
687 | */
|
---|
688 | const PDMDEVREG g_DeviceINIP =
|
---|
689 | {
|
---|
690 | /* u32Version */
|
---|
691 | PDM_DEVREG_VERSION,
|
---|
692 | /* szDeviceName */
|
---|
693 | "IntNetIP",
|
---|
694 | /* szRCMod/szR0Mod */
|
---|
695 | "",
|
---|
696 | "",
|
---|
697 | /* pszDescription */
|
---|
698 | "Internal Network IP stack device",
|
---|
699 | /* fFlags */
|
---|
700 | PDM_DEVREG_FLAGS_DEFAULT_BITS,
|
---|
701 | /* fClass. As this is used by the storage devices, it must come earlier. */
|
---|
702 | PDM_DEVREG_CLASS_VMM_DEV,
|
---|
703 | /* cMaxInstances */
|
---|
704 | 1,
|
---|
705 | /* cbInstance */
|
---|
706 | sizeof(DEVINTNETIP),
|
---|
707 | /* pfnConstruct */
|
---|
708 | devINIPConstruct,
|
---|
709 | /* pfnDestruct */
|
---|
710 | devINIPDestruct,
|
---|
711 | /* pfnRelocate */
|
---|
712 | NULL,
|
---|
713 | /* pfnIOCtl */
|
---|
714 | NULL,
|
---|
715 | /* pfnPowerOn */
|
---|
716 | NULL,
|
---|
717 | /* pfnReset */
|
---|
718 | NULL,
|
---|
719 | /* pfnSuspend */
|
---|
720 | NULL,
|
---|
721 | /* pfnResume */
|
---|
722 | NULL,
|
---|
723 | /* pfnAttach */
|
---|
724 | NULL,
|
---|
725 | /* pfnDetach */
|
---|
726 | NULL,
|
---|
727 | /* pfnQueryInterface */
|
---|
728 | NULL,
|
---|
729 | /* pfnInitComplete */
|
---|
730 | NULL,
|
---|
731 | /* pfnPowerOff */
|
---|
732 | NULL,
|
---|
733 | /* pfnSoftReset */
|
---|
734 | NULL,
|
---|
735 | /* u32VersionEnd */
|
---|
736 | PDM_DEVREG_VERSION
|
---|
737 | };
|
---|