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