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