1 | /* $Id: DevINIP.cpp 45356 2013-04-05 07:01:27Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * DevINIP - Internal Network IP stack device/service.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007-2013 Oracle Corporation
|
---|
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 |
|
---|
18 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #define LOG_GROUP LOG_GROUP_DEV_INIP
|
---|
23 | #include <iprt/cdefs.h> /* include early to allow RT_C_DECLS_BEGIN hack */
|
---|
24 | #include <iprt/mem.h> /* include anything of ours that the lwip headers use. */
|
---|
25 | #include <iprt/semaphore.h>
|
---|
26 | #include <iprt/thread.h>
|
---|
27 | #include <iprt/alloca.h>
|
---|
28 | /* All lwip header files are not C++ safe. So hack around this. */
|
---|
29 | RT_C_DECLS_BEGIN
|
---|
30 | #include "lwip/sys.h"
|
---|
31 | #include "lwip/stats.h"
|
---|
32 | #include "lwip/mem.h"
|
---|
33 | #include "lwip/memp.h"
|
---|
34 | #include "lwip/pbuf.h"
|
---|
35 | #include "lwip/netif.h"
|
---|
36 | #ifndef VBOX_WITH_NEW_LWIP
|
---|
37 | # include "ipv4/lwip/ip.h"
|
---|
38 | #else
|
---|
39 | # include "lwip/api.h"
|
---|
40 | # include "lwip/tcp_impl.h"
|
---|
41 | # include "ipv6/lwip/ethip6.h"
|
---|
42 | #endif
|
---|
43 | #include "lwip/udp.h"
|
---|
44 | #include "lwip/tcp.h"
|
---|
45 | #include "lwip/tcpip.h"
|
---|
46 | #include "lwip/sockets.h"
|
---|
47 | #include "netif/etharp.h"
|
---|
48 | RT_C_DECLS_END
|
---|
49 | #include <VBox/vmm/pdmdev.h>
|
---|
50 | #include <VBox/vmm/pdmnetifs.h>
|
---|
51 | #include <VBox/vmm/tm.h>
|
---|
52 | #include <iprt/assert.h>
|
---|
53 | #include <iprt/string.h>
|
---|
54 | #include <iprt/uuid.h>
|
---|
55 |
|
---|
56 | #include "VBoxDD.h"
|
---|
57 |
|
---|
58 |
|
---|
59 | /*******************************************************************************
|
---|
60 | * Macros and Defines *
|
---|
61 | *******************************************************************************/
|
---|
62 |
|
---|
63 | /** Maximum frame size this device can handle. */
|
---|
64 | #define DEVINIP_MAX_FRAME 1514
|
---|
65 |
|
---|
66 |
|
---|
67 | /*******************************************************************************
|
---|
68 | * Structures and Typedefs *
|
---|
69 | *******************************************************************************/
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Internal Network IP stack device instance data.
|
---|
73 | *
|
---|
74 | * @implements PDMIBASE
|
---|
75 | * @implements PDMINETWORKDOWN
|
---|
76 | */
|
---|
77 | typedef struct DEVINTNETIP
|
---|
78 | {
|
---|
79 | /** The base interface for LUN\#0. */
|
---|
80 | PDMIBASE IBase;
|
---|
81 | /** The network port this device provides (LUN\#0). */
|
---|
82 | PDMINETWORKDOWN INetworkDown;
|
---|
83 | /** The network configuration port this device provides (LUN\#0). */
|
---|
84 | PDMINETWORKCONFIG INetworkConfig;
|
---|
85 | /** The base interface of the network driver below us. */
|
---|
86 | PPDMIBASE pDrvBase;
|
---|
87 | /** The connector of the network driver below us. */
|
---|
88 | PPDMINETWORKUP pDrv;
|
---|
89 | /** Pointer to the device instance. */
|
---|
90 | PPDMDEVINSR3 pDevIns;
|
---|
91 | /** MAC address. */
|
---|
92 | RTMAC MAC;
|
---|
93 | /** Static IP address of the interface. */
|
---|
94 | char *pszIP;
|
---|
95 | /** Netmask of the interface. */
|
---|
96 | char *pszNetmask;
|
---|
97 | /** Gateway for the interface. */
|
---|
98 | char *pszGateway;
|
---|
99 | /** lwIP network interface description. */
|
---|
100 | struct netif IntNetIF;
|
---|
101 | /** lwIP ARP timer. */
|
---|
102 | PTMTIMERR3 ARPTimer;
|
---|
103 | /** lwIP TCP fast timer. */
|
---|
104 | PTMTIMERR3 TCPFastTimer;
|
---|
105 | /** lwIP TCP slow timer. */
|
---|
106 | PTMTIMERR3 TCPSlowTimer;
|
---|
107 | /** lwIP semaphore to coordinate TCPIP init/terminate. */
|
---|
108 | sys_sem_t LWIPTcpInitSem;
|
---|
109 | /** hack: get linking right. remove this eventually, once the device
|
---|
110 | * provides a proper interface to all IP stack functions. */
|
---|
111 | const void *pLinkHack;
|
---|
112 | /** Flag whether the link is up. */
|
---|
113 | bool fLnkUp;
|
---|
114 | } DEVINTNETIP, *PDEVINTNETIP;
|
---|
115 |
|
---|
116 |
|
---|
117 | /*******************************************************************************
|
---|
118 | * Global Variables *
|
---|
119 | *******************************************************************************/
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Pointer to the (only) instance data in this device.
|
---|
123 | */
|
---|
124 | static PDEVINTNETIP g_pDevINIPData = NULL;
|
---|
125 |
|
---|
126 | /*
|
---|
127 | * really ugly hack to avoid linking problems on unix style platforms
|
---|
128 | * using .a libraries for now.
|
---|
129 | */
|
---|
130 | static const PFNRT g_pDevINILinkHack[] =
|
---|
131 | {
|
---|
132 | (PFNRT)lwip_socket,
|
---|
133 | (PFNRT)lwip_close,
|
---|
134 | (PFNRT)lwip_setsockopt,
|
---|
135 | (PFNRT)lwip_recv,
|
---|
136 | (PFNRT)lwip_send,
|
---|
137 | (PFNRT)lwip_select
|
---|
138 | };
|
---|
139 |
|
---|
140 |
|
---|
141 | /*******************************************************************************
|
---|
142 | * Internal Functions *
|
---|
143 | *******************************************************************************/
|
---|
144 | static DECLCALLBACK(void) devINIPARPTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer);
|
---|
145 | static DECLCALLBACK(void) devINIPTCPFastTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer);
|
---|
146 | static DECLCALLBACK(void) devINIPTCPSlowTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer);
|
---|
147 | static DECLCALLBACK(err_t) devINIPOutput(struct netif *netif, struct pbuf *p, struct ip_addr *ipaddr);
|
---|
148 | static DECLCALLBACK(err_t) devINIPOutputRaw(struct netif *netif, struct pbuf *p);
|
---|
149 | static DECLCALLBACK(err_t) devINIPInterface(struct netif *netif);
|
---|
150 |
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * ARP cache timeout handling for lwIP.
|
---|
154 | *
|
---|
155 | * @param pDevIns Device instance.
|
---|
156 | * @param pTimer Pointer to timer.
|
---|
157 | */
|
---|
158 | static DECLCALLBACK(void) devINIPARPTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
|
---|
159 | {
|
---|
160 | PDEVINTNETIP pThis = (PDEVINTNETIP)pvUser;
|
---|
161 | LogFlow(("%s: pDevIns=%p pTimer=%p\n", __FUNCTION__, pDevIns, pTimer));
|
---|
162 | lwip_etharp_tmr();
|
---|
163 | TMTimerSetMillies(pThis->ARPTimer, ARP_TMR_INTERVAL);
|
---|
164 | LogFlow(("%s: return\n", __FUNCTION__));
|
---|
165 | }
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * TCP fast timer handling for lwIP.
|
---|
169 | *
|
---|
170 | * @param pDevIns Device instance.
|
---|
171 | * @param pTimer Pointer to timer.
|
---|
172 | */
|
---|
173 | static DECLCALLBACK(void) devINIPTCPFastTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
|
---|
174 | {
|
---|
175 | PDEVINTNETIP pThis = (PDEVINTNETIP)pvUser;
|
---|
176 | LogFlow(("%s: pDevIns=%p pTimer=%p\n", __FUNCTION__, pDevIns, pTimer));
|
---|
177 | lwip_tcp_fasttmr();
|
---|
178 | TMTimerSetMillies(pThis->TCPFastTimer, TCP_FAST_INTERVAL);
|
---|
179 | LogFlow(("%s: return\n", __FUNCTION__));
|
---|
180 | }
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * TCP slow timer handling for lwIP.
|
---|
184 | *
|
---|
185 | * @param pDevIns Device instance.
|
---|
186 | * @param pTimer Pointer to timer.
|
---|
187 | */
|
---|
188 | static DECLCALLBACK(void) devINIPTCPSlowTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
|
---|
189 | {
|
---|
190 | PDEVINTNETIP pThis = (PDEVINTNETIP)pvUser;
|
---|
191 | LogFlow(("%s: pDevIns=%p pTimer=%p\n", __FUNCTION__, pDevIns, pTimer));
|
---|
192 | lwip_tcp_slowtmr();
|
---|
193 | TMTimerSetMillies(pThis->TCPSlowTimer, TCP_SLOW_INTERVAL);
|
---|
194 | LogFlow(("%s: return\n", __FUNCTION__));
|
---|
195 | }
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * Output a TCP/IP packet on the interface. Uses the generic lwIP ARP
|
---|
199 | * code to resolve the address and call the link-level packet function.
|
---|
200 | *
|
---|
201 | * @returns lwIP error code
|
---|
202 | * @param netif Interface on which to send IP packet.
|
---|
203 | * @param p Packet data.
|
---|
204 | * @param ipaddr Destination IP address.
|
---|
205 | */
|
---|
206 | static DECLCALLBACK(err_t) devINIPOutput(struct netif *netif, struct pbuf *p, struct ip_addr *ipaddr)
|
---|
207 | {
|
---|
208 | err_t lrc;
|
---|
209 | LogFlow(("%s: netif=%p p=%p ipaddr=%#04x\n", __FUNCTION__, netif, p,
|
---|
210 | ipaddr->addr));
|
---|
211 | #ifndef VBOX_WITH_NEW_LWIP
|
---|
212 | lrc = lwip_etharp_output(netif, ipaddr, p);
|
---|
213 | #else
|
---|
214 | lrc = lwip_etharp_output(netif, p, ipaddr);
|
---|
215 | #endif
|
---|
216 | LogFlow(("%s: return %d\n", __FUNCTION__, lrc));
|
---|
217 | return lrc;
|
---|
218 | }
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * Output a raw packet on the interface.
|
---|
222 | *
|
---|
223 | * @returns lwIP error code
|
---|
224 | * @param netif Interface on which to send frame.
|
---|
225 | * @param p Frame data.
|
---|
226 | */
|
---|
227 | static DECLCALLBACK(err_t) devINIPOutputRaw(struct netif *netif, struct pbuf *p)
|
---|
228 | {
|
---|
229 | int rc = VINF_SUCCESS;
|
---|
230 |
|
---|
231 | LogFlow(("%s: netif=%p p=%p\n", __FUNCTION__, netif, p));
|
---|
232 | Assert(g_pDevINIPData);
|
---|
233 | Assert(g_pDevINIPData->pDrv);
|
---|
234 |
|
---|
235 | /* Silently ignore packets being sent while lwIP isn't set up. */
|
---|
236 | if (g_pDevINIPData)
|
---|
237 | {
|
---|
238 | PPDMSCATTERGATHER pSgBuf;
|
---|
239 |
|
---|
240 | rc = g_pDevINIPData->pDrv->pfnBeginXmit(g_pDevINIPData->pDrv, true /* fOnWorkerThread */);
|
---|
241 | if (RT_FAILURE(rc))
|
---|
242 | return ERR_IF;
|
---|
243 |
|
---|
244 | rc = g_pDevINIPData->pDrv->pfnAllocBuf(g_pDevINIPData->pDrv, DEVINIP_MAX_FRAME, NULL /*pGso*/, &pSgBuf);
|
---|
245 | if (RT_SUCCESS(rc))
|
---|
246 | {
|
---|
247 | #if ETH_PAD_SIZE
|
---|
248 | lwip_pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
|
---|
249 | #endif
|
---|
250 |
|
---|
251 | uint8_t *pbBuf = pSgBuf ? (uint8_t *)pSgBuf->aSegs[0].pvSeg : NULL;
|
---|
252 | size_t cbBuf = 0;
|
---|
253 | for (struct pbuf *q = p; q != NULL; q = q->next)
|
---|
254 | {
|
---|
255 | if (cbBuf + q->len <= DEVINIP_MAX_FRAME)
|
---|
256 | {
|
---|
257 | if (RT_LIKELY(pbBuf))
|
---|
258 | {
|
---|
259 | memcpy(pbBuf, q->payload, q->len);
|
---|
260 | pbBuf += q->len;
|
---|
261 | }
|
---|
262 | cbBuf += q->len;
|
---|
263 | }
|
---|
264 | else
|
---|
265 | {
|
---|
266 | LogRel(("INIP: exceeded frame size\n"));
|
---|
267 | break;
|
---|
268 | }
|
---|
269 | }
|
---|
270 | if (cbBuf)
|
---|
271 | {
|
---|
272 | pSgBuf->cbUsed = cbBuf;
|
---|
273 | rc = g_pDevINIPData->pDrv->pfnSendBuf(g_pDevINIPData->pDrv, pSgBuf, true /* fOnWorkerThread */);
|
---|
274 | }
|
---|
275 | else
|
---|
276 | rc = g_pDevINIPData->pDrv->pfnFreeBuf(g_pDevINIPData->pDrv, pSgBuf);
|
---|
277 |
|
---|
278 | #if ETH_PAD_SIZE
|
---|
279 | lwip_pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
|
---|
280 | #endif
|
---|
281 | }
|
---|
282 |
|
---|
283 | g_pDevINIPData->pDrv->pfnEndXmit(g_pDevINIPData->pDrv);
|
---|
284 | }
|
---|
285 |
|
---|
286 | err_t lrc = ERR_OK;
|
---|
287 | if (RT_FAILURE(rc))
|
---|
288 | lrc = ERR_IF;
|
---|
289 | LogFlow(("%s: return %d (vbox: %Rrc)\n", __FUNCTION__, rc, lrc));
|
---|
290 | return lrc;
|
---|
291 | }
|
---|
292 |
|
---|
293 | /**
|
---|
294 | * Implements the ethernet interface backend initialization for lwIP.
|
---|
295 | *
|
---|
296 | * @returns lwIP error code
|
---|
297 | * @param netif Interface to configure.
|
---|
298 | */
|
---|
299 | static DECLCALLBACK(err_t) devINIPInterface(struct netif *netif)
|
---|
300 | {
|
---|
301 | LogFlow(("%s: netif=%p\n", __FUNCTION__, netif));
|
---|
302 | Assert(g_pDevINIPData != NULL);
|
---|
303 | netif->state = g_pDevINIPData;
|
---|
304 | netif->hwaddr_len = sizeof(g_pDevINIPData->MAC);
|
---|
305 | memcpy(netif->hwaddr, &g_pDevINIPData->MAC, sizeof(g_pDevINIPData->MAC));
|
---|
306 | netif->mtu = DEVINIP_MAX_FRAME;
|
---|
307 | netif->flags = NETIF_FLAG_BROADCAST;
|
---|
308 | #ifdef VBOX_WITH_NEW_LWIP
|
---|
309 | /** @todo why explicit ARP routing required for 1.2.0 case? */
|
---|
310 | netif->flags |= NETIF_FLAG_ETHARP;
|
---|
311 | netif->flags |= NETIF_FLAG_ETHERNET;
|
---|
312 | /* Note! We always assign link-local IPv6 address */
|
---|
313 | netif_create_ip6_linklocal_address(netif, 0);
|
---|
314 | netif_ip6_addr_set_state(netif, 0, IP6_ADDR_VALID);
|
---|
315 | netif->output_ip6 = ethip6_output;
|
---|
316 | netif->ip6_autoconfig_enabled=1;
|
---|
317 | LogFunc(("netif: ipv6:%RTnaipv6\n", &netif->ip6_addr[0].addr[0]));
|
---|
318 | netif->output = lwip_etharp_output;
|
---|
319 | #else
|
---|
320 | netif->output = devINIPOutput;
|
---|
321 | #endif
|
---|
322 | netif->linkoutput = devINIPOutputRaw;
|
---|
323 |
|
---|
324 | lwip_etharp_init();
|
---|
325 | TMTimerSetMillies(g_pDevINIPData->ARPTimer, ARP_TMR_INTERVAL);
|
---|
326 | LogFlow(("%s: success\n", __FUNCTION__));
|
---|
327 | return ERR_OK;
|
---|
328 | }
|
---|
329 |
|
---|
330 | /**
|
---|
331 | * Parses CFGM parameters related to network connection
|
---|
332 | */
|
---|
333 | static DECLCALLBACK(int) devINIPNetworkConfiguration(PPDMDEVINS pDevIns, PDEVINTNETIP pThis, PCFGMNODE pCfg)
|
---|
334 | {
|
---|
335 | int rc = VINF_SUCCESS;
|
---|
336 | rc = CFGMR3QueryStringAlloc(pCfg, "IP", &pThis->pszIP);
|
---|
337 | if (RT_FAILURE(rc))
|
---|
338 | {
|
---|
339 | PDMDEV_SET_ERROR(pDevIns, rc,
|
---|
340 | N_("Configuration error: Failed to get the \"IP\" value"));
|
---|
341 | /* @todo: perhaps we should panic if IPv4 address isn't specify, with assumtion that
|
---|
342 | * ISCSI target specified in IPv6 form.
|
---|
343 | */
|
---|
344 | return rc;
|
---|
345 | }
|
---|
346 |
|
---|
347 | rc = CFGMR3QueryStringAlloc(pCfg, "Netmask", &pThis->pszNetmask);
|
---|
348 | if (RT_FAILURE(rc))
|
---|
349 | {
|
---|
350 | PDMDEV_SET_ERROR(pDevIns, rc,
|
---|
351 | N_("Configuration error: Failed to get the \"Netmask\" value"));
|
---|
352 | return rc;
|
---|
353 | }
|
---|
354 | rc = CFGMR3QueryStringAlloc(pCfg, "Gateway", &pThis->pszGateway);
|
---|
355 | if ( RT_FAILURE(rc)
|
---|
356 | && rc != VERR_CFGM_VALUE_NOT_FOUND)
|
---|
357 | {
|
---|
358 | PDMDEV_SET_ERROR(pDevIns, rc,
|
---|
359 | N_("Configuration error: Failed to get the \"Gateway\" value"));
|
---|
360 | return rc;
|
---|
361 | }
|
---|
362 | return VINF_SUCCESS;
|
---|
363 | }
|
---|
364 |
|
---|
365 | /**
|
---|
366 | * Wait until data can be received.
|
---|
367 | *
|
---|
368 | * @returns VBox status code. VINF_SUCCESS means there is at least one receive descriptor available.
|
---|
369 | * @param pInterface PDM network port interface pointer.
|
---|
370 | * @param cMillies Number of milliseconds to wait. 0 means return immediately.
|
---|
371 | */
|
---|
372 | static DECLCALLBACK(int) devINIPNetworkDown_WaitInputAvail(PPDMINETWORKDOWN pInterface, RTMSINTERVAL cMillies)
|
---|
373 | {
|
---|
374 | LogFlow(("%s: pInterface=%p\n", __FUNCTION__, pInterface));
|
---|
375 | LogFlow(("%s: return VINF_SUCCESS\n", __FUNCTION__));
|
---|
376 | return VINF_SUCCESS;
|
---|
377 | }
|
---|
378 |
|
---|
379 | /**
|
---|
380 | * Receive data and pass it to lwIP for processing.
|
---|
381 | *
|
---|
382 | * @returns VBox status code
|
---|
383 | * @param pInterface PDM network port interface pointer.
|
---|
384 | * @param pvBuf Pointer to frame data.
|
---|
385 | * @param cb Frame size.
|
---|
386 | */
|
---|
387 | static DECLCALLBACK(int) devINIPNetworkDown_Input(PPDMINETWORKDOWN pInterface, const void *pvBuf, size_t cb)
|
---|
388 | {
|
---|
389 | const uint8_t *pbBuf = (const uint8_t *)pvBuf;
|
---|
390 | size_t len = cb;
|
---|
391 | const struct eth_hdr *ethhdr;
|
---|
392 | struct pbuf *p, *q;
|
---|
393 | int rc = VINF_SUCCESS;
|
---|
394 |
|
---|
395 | LogFlow(("%s: pInterface=%p pvBuf=%p cb=%lu\n", __FUNCTION__, pInterface, pvBuf, cb));
|
---|
396 | Assert(g_pDevINIPData);
|
---|
397 | Assert(g_pDevINIPData->pDrv);
|
---|
398 |
|
---|
399 | /* Silently ignore packets being received while lwIP isn't set up. */
|
---|
400 | if (!g_pDevINIPData)
|
---|
401 | {
|
---|
402 | LogFlow(("%s: return %Rrc (no global)\n", __FUNCTION__, rc));
|
---|
403 | return VINF_SUCCESS;
|
---|
404 | }
|
---|
405 |
|
---|
406 | #if ETH_PAD_SIZE
|
---|
407 | len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
|
---|
408 | #endif
|
---|
409 |
|
---|
410 | /* We allocate a pbuf chain of pbufs from the pool. */
|
---|
411 | p = lwip_pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
|
---|
412 | if (p != NULL)
|
---|
413 | {
|
---|
414 | #if ETH_PAD_SIZE
|
---|
415 | lwip_pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
|
---|
416 | #endif
|
---|
417 |
|
---|
418 | for (q = p; q != NULL; q = q->next)
|
---|
419 | {
|
---|
420 | /* Fill the buffers, and clean out unused buffer space. */
|
---|
421 | memcpy(q->payload, pbBuf, RT_MIN(cb, q->len));
|
---|
422 | pbBuf += RT_MIN(cb, q->len);
|
---|
423 | if (q->len > cb)
|
---|
424 | memset(((uint8_t *)q->payload) + cb, '\0', q->len - cb);
|
---|
425 | cb -= RT_MIN(cb, q->len);
|
---|
426 | }
|
---|
427 |
|
---|
428 | ethhdr = (const struct eth_hdr *)p->payload;
|
---|
429 | struct netif *iface = &g_pDevINIPData->IntNetIF;
|
---|
430 | err_t lrc;
|
---|
431 | #ifndef VBOX_WITH_NEW_LWIP
|
---|
432 | switch (htons(ethhdr->type))
|
---|
433 | {
|
---|
434 | case ETHTYPE_IP: /* IP packet */
|
---|
435 | lwip_pbuf_header(p, -(ssize_t)sizeof(struct eth_hdr));
|
---|
436 | lrc = iface->input(p, iface);
|
---|
437 | if (lrc)
|
---|
438 | rc = VERR_NET_IO_ERROR;
|
---|
439 | break;
|
---|
440 | case ETHTYPE_ARP: /* ARP packet */
|
---|
441 | lwip_etharp_arp_input(iface, (struct eth_addr *)iface->hwaddr, p);
|
---|
442 | break;
|
---|
443 | default:
|
---|
444 | lwip_pbuf_free(p);
|
---|
445 | }
|
---|
446 | #else
|
---|
447 | /* We've setup flags NETIF_FLAG_ETHARP and NETIF_FLAG_ETHERNET
|
---|
448 | so this should be thread-safe. */
|
---|
449 | tcpip_input(p,iface);
|
---|
450 | #endif
|
---|
451 | }
|
---|
452 |
|
---|
453 | out:
|
---|
454 | LogFlow(("%s: return %Rrc\n", __FUNCTION__, rc));
|
---|
455 | return rc;
|
---|
456 | }
|
---|
457 |
|
---|
458 | /**
|
---|
459 | * @interface_method_impl{PDMINETWORKDOWN,pfnXmitPending}
|
---|
460 | */
|
---|
461 | static DECLCALLBACK(void) devINIPNetworkDown_XmitPending(PPDMINETWORKDOWN pInterface)
|
---|
462 | {
|
---|
463 | NOREF(pInterface);
|
---|
464 | }
|
---|
465 |
|
---|
466 |
|
---|
467 | /**
|
---|
468 | * Signals the end of lwIP TCPIP initialization.
|
---|
469 | *
|
---|
470 | * @param arg opaque argument, here the pointer to the semaphore.
|
---|
471 | */
|
---|
472 | static DECLCALLBACK(void) devINIPTcpipInitDone(void *arg)
|
---|
473 | {
|
---|
474 | sys_sem_t *sem = (sys_sem_t *)arg;
|
---|
475 | #ifndef VBOX_WITH_NEW_LWIP
|
---|
476 | lwip_sys_sem_signal(*sem);
|
---|
477 | #else
|
---|
478 | lwip_sys_sem_signal(sem);
|
---|
479 | #endif
|
---|
480 | }
|
---|
481 |
|
---|
482 |
|
---|
483 | /**
|
---|
484 | * Gets the current Media Access Control (MAC) address.
|
---|
485 | *
|
---|
486 | * @returns VBox status code.
|
---|
487 | * @param pInterface Pointer to the interface structure containing the called function pointer.
|
---|
488 | * @param pMac Where to store the MAC address.
|
---|
489 | * @thread EMT
|
---|
490 | */
|
---|
491 | static DECLCALLBACK(int) devINIPGetMac(PPDMINETWORKCONFIG pInterface, PRTMAC pMac)
|
---|
492 | {
|
---|
493 | PDEVINTNETIP pThis = RT_FROM_MEMBER(pInterface, DEVINTNETIP, INetworkConfig);
|
---|
494 | memcpy(pMac, pThis->MAC.au8, sizeof(RTMAC));
|
---|
495 | return VINF_SUCCESS;
|
---|
496 | }
|
---|
497 |
|
---|
498 | /**
|
---|
499 | * Gets the new link state.
|
---|
500 | *
|
---|
501 | * @returns The current link state.
|
---|
502 | * @param pInterface Pointer to the interface structure containing the called function pointer.
|
---|
503 | * @thread EMT
|
---|
504 | */
|
---|
505 | static DECLCALLBACK(PDMNETWORKLINKSTATE) devINIPGetLinkState(PPDMINETWORKCONFIG pInterface)
|
---|
506 | {
|
---|
507 | PDEVINTNETIP pThis = RT_FROM_MEMBER(pInterface, DEVINTNETIP, INetworkConfig);
|
---|
508 | if (pThis->fLnkUp)
|
---|
509 | return PDMNETWORKLINKSTATE_UP;
|
---|
510 | return PDMNETWORKLINKSTATE_DOWN;
|
---|
511 | }
|
---|
512 |
|
---|
513 |
|
---|
514 | /**
|
---|
515 | * Sets the new link state.
|
---|
516 | *
|
---|
517 | * @returns VBox status code.
|
---|
518 | * @param pInterface Pointer to the interface structure containing the called function pointer.
|
---|
519 | * @param enmState The new link state
|
---|
520 | */
|
---|
521 | static DECLCALLBACK(int) devINIPSetLinkState(PPDMINETWORKCONFIG pInterface, PDMNETWORKLINKSTATE enmState)
|
---|
522 | {
|
---|
523 | PDEVINTNETIP pThis = RT_FROM_MEMBER(pInterface, DEVINTNETIP, INetworkConfig);
|
---|
524 | bool fNewUp = enmState == PDMNETWORKLINKSTATE_UP;
|
---|
525 |
|
---|
526 | if (fNewUp != pThis->fLnkUp)
|
---|
527 | {
|
---|
528 | if (fNewUp)
|
---|
529 | {
|
---|
530 | LogFlowFunc(("Link is up\n"));
|
---|
531 | pThis->fLnkUp = true;
|
---|
532 | }
|
---|
533 | else
|
---|
534 | {
|
---|
535 | LogFlowFunc(("Link is down\n"));
|
---|
536 | pThis->fLnkUp = false;
|
---|
537 | }
|
---|
538 | if (pThis->pDrv)
|
---|
539 | pThis->pDrv->pfnNotifyLinkChanged(pThis->pDrv, enmState);
|
---|
540 | }
|
---|
541 | return VINF_SUCCESS;
|
---|
542 | }
|
---|
543 |
|
---|
544 | /* -=-=-=-=- PDMIBASE -=-=-=-=- */
|
---|
545 |
|
---|
546 | /**
|
---|
547 | * @interface_method_impl{PDMIBASE,pfnQueryInterface}
|
---|
548 | */
|
---|
549 | static DECLCALLBACK(void *) devINIPQueryInterface(PPDMIBASE pInterface,
|
---|
550 | const char *pszIID)
|
---|
551 | {
|
---|
552 | PDEVINTNETIP pThis = RT_FROM_MEMBER(pInterface, DEVINTNETIP, IBase);
|
---|
553 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThis->IBase);
|
---|
554 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMINETWORKDOWN, &pThis->INetworkDown);
|
---|
555 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMINETWORKCONFIG, &pThis->INetworkConfig);
|
---|
556 | return NULL;
|
---|
557 | }
|
---|
558 |
|
---|
559 | /* -=-=-=-=- PDMDEVREG -=-=-=-=- */
|
---|
560 |
|
---|
561 | /**
|
---|
562 | * Destruct a device instance.
|
---|
563 | *
|
---|
564 | * Most VM resources are freed by the VM. This callback is provided so that any non-VM
|
---|
565 | * resources can be freed correctly.
|
---|
566 | *
|
---|
567 | * @returns VBox status.
|
---|
568 | * @param pDevIns The device instance data.
|
---|
569 | */
|
---|
570 | static DECLCALLBACK(int) devINIPDestruct(PPDMDEVINS pDevIns)
|
---|
571 | {
|
---|
572 | PDEVINTNETIP pThis = PDMINS_2_DATA(pDevIns, PDEVINTNETIP);
|
---|
573 |
|
---|
574 | LogFlow(("%s: pDevIns=%p\n", __FUNCTION__, pDevIns));
|
---|
575 | PDMDEV_CHECK_VERSIONS_RETURN_QUIET(pDevIns);
|
---|
576 |
|
---|
577 | if (g_pDevINIPData != NULL)
|
---|
578 | {
|
---|
579 | netif_set_down(&pThis->IntNetIF);
|
---|
580 | netif_remove(&pThis->IntNetIF);
|
---|
581 | tcpip_terminate();
|
---|
582 | #ifndef VBOX_WITH_NEW_LWIP
|
---|
583 | lwip_sys_sem_wait(pThis->LWIPTcpInitSem);
|
---|
584 | lwip_sys_sem_free(pThis->LWIPTcpInitSem);
|
---|
585 | #else
|
---|
586 | lwip_sys_sem_wait(&pThis->LWIPTcpInitSem, 0);
|
---|
587 | lwip_sys_sem_free(&pThis->LWIPTcpInitSem);
|
---|
588 | #endif
|
---|
589 | }
|
---|
590 |
|
---|
591 | MMR3HeapFree(pThis->pszIP);
|
---|
592 | pThis->pszIP = NULL;
|
---|
593 | MMR3HeapFree(pThis->pszNetmask);
|
---|
594 | pThis->pszNetmask = NULL;
|
---|
595 | MMR3HeapFree(pThis->pszGateway);
|
---|
596 | pThis->pszGateway = NULL;
|
---|
597 |
|
---|
598 | LogFlow(("%s: success\n", __FUNCTION__));
|
---|
599 | return VINF_SUCCESS;
|
---|
600 | }
|
---|
601 |
|
---|
602 |
|
---|
603 | /**
|
---|
604 | * @interface_method_impl{PDMDEVREG,pfnConstruct}
|
---|
605 | */
|
---|
606 | static DECLCALLBACK(int) devINIPConstruct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
|
---|
607 | {
|
---|
608 | PDEVINTNETIP pThis = PDMINS_2_DATA(pDevIns, PDEVINTNETIP);
|
---|
609 | int rc = VINF_SUCCESS;
|
---|
610 | #ifdef VBOX_WITH_NEW_LWIP
|
---|
611 | err_t errRc = ERR_OK;
|
---|
612 | #endif
|
---|
613 | LogFlow(("%s: pDevIns=%p iInstance=%d pCfg=%p\n", __FUNCTION__,
|
---|
614 | pDevIns, iInstance, pCfg));
|
---|
615 |
|
---|
616 | Assert(iInstance == 0);
|
---|
617 | PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
|
---|
618 |
|
---|
619 | /*
|
---|
620 | * Validate the config.
|
---|
621 | */
|
---|
622 | if (!CFGMR3AreValuesValid(pCfg, "MAC\0IP\0"
|
---|
623 | #ifdef VBOX_WITH_NEW_LWIP
|
---|
624 | "IPv6\0"
|
---|
625 | #endif
|
---|
626 | "Netmask\0Gateway\0"))
|
---|
627 | return PDMDEV_SET_ERROR(pDevIns, VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES,
|
---|
628 | N_("Unknown Internal Networking IP configuration option"));
|
---|
629 |
|
---|
630 | /*
|
---|
631 | * Init the static parts.
|
---|
632 | */
|
---|
633 | pThis->pszIP = NULL;
|
---|
634 | pThis->pszNetmask = NULL;
|
---|
635 | pThis->pszGateway = NULL;
|
---|
636 | /* Pointer to device instance */
|
---|
637 | pThis->pDevIns = pDevIns;
|
---|
638 | /* IBase */
|
---|
639 | pThis->IBase.pfnQueryInterface = devINIPQueryInterface;
|
---|
640 | /* INetworkDown */
|
---|
641 | pThis->INetworkDown.pfnWaitReceiveAvail = devINIPNetworkDown_WaitInputAvail;
|
---|
642 | pThis->INetworkDown.pfnReceive = devINIPNetworkDown_Input;
|
---|
643 | pThis->INetworkDown.pfnXmitPending = devINIPNetworkDown_XmitPending;
|
---|
644 | /* INetworkConfig */
|
---|
645 | pThis->INetworkConfig.pfnGetMac = devINIPGetMac;
|
---|
646 | pThis->INetworkConfig.pfnGetLinkState = devINIPGetLinkState;
|
---|
647 | pThis->INetworkConfig.pfnSetLinkState = devINIPSetLinkState;
|
---|
648 |
|
---|
649 | /*
|
---|
650 | * Get the configuration settings.
|
---|
651 | */
|
---|
652 | rc = CFGMR3QueryBytes(pCfg, "MAC", &pThis->MAC, sizeof(pThis->MAC));
|
---|
653 | if (rc == VERR_CFGM_NOT_BYTES)
|
---|
654 | {
|
---|
655 | char szMAC[64];
|
---|
656 | rc = CFGMR3QueryString(pCfg, "MAC", &szMAC[0], sizeof(szMAC));
|
---|
657 | if (RT_SUCCESS(rc))
|
---|
658 | {
|
---|
659 | char *macStr = &szMAC[0];
|
---|
660 | char *pMac = (char *)&pThis->MAC;
|
---|
661 | for (uint32_t i = 0; i < 6; i++)
|
---|
662 | {
|
---|
663 | if ( !*macStr || !*(macStr + 1)
|
---|
664 | || *macStr == ':' || *(macStr + 1) == ':')
|
---|
665 | return PDMDEV_SET_ERROR(pDevIns,
|
---|
666 | VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES,
|
---|
667 | N_("Configuration error: Invalid \"MAC\" value"));
|
---|
668 | char c1 = *macStr++ - '0';
|
---|
669 | if (c1 > 9)
|
---|
670 | c1 -= 7;
|
---|
671 | char c2 = *macStr++ - '0';
|
---|
672 | if (c2 > 9)
|
---|
673 | c2 -= 7;
|
---|
674 | *pMac++ = ((c1 & 0x0f) << 4) | (c2 & 0x0f);
|
---|
675 | if (i != 5 && *macStr == ':')
|
---|
676 | macStr++;
|
---|
677 | }
|
---|
678 | }
|
---|
679 | }
|
---|
680 | if (RT_FAILURE(rc))
|
---|
681 | return PDMDEV_SET_ERROR(pDevIns, rc,
|
---|
682 | N_("Configuration error: Failed to get the \"MAC\" value"));
|
---|
683 | rc = devINIPNetworkConfiguration(pDevIns, pThis, pCfg);
|
---|
684 | AssertLogRelRCReturn(rc, rc);
|
---|
685 |
|
---|
686 | /*
|
---|
687 | * Attach driver and query the network connector interface.
|
---|
688 | */
|
---|
689 | rc = PDMDevHlpDriverAttach(pDevIns, 0, &pThis->IBase, &pThis->pDrvBase, "Network Port");
|
---|
690 | if (RT_FAILURE(rc))
|
---|
691 | {
|
---|
692 | pThis->pDrvBase = NULL;
|
---|
693 | pThis->pDrv = NULL;
|
---|
694 | return PDMDEV_SET_ERROR(pDevIns, rc, N_("Error attaching device below us"));
|
---|
695 | }
|
---|
696 | pThis->pDrv = PDMIBASE_QUERY_INTERFACE(pThis->pDrvBase, PDMINETWORKUP);
|
---|
697 | AssertMsgReturn(pThis->pDrv, ("Failed to obtain the PDMINETWORKUP interface!\n"), VERR_PDM_MISSING_INTERFACE_BELOW);
|
---|
698 |
|
---|
699 | struct ip_addr ipaddr, netmask, gw;
|
---|
700 | struct in_addr ip;
|
---|
701 |
|
---|
702 | if (!inet_aton(pThis->pszIP, &ip))
|
---|
703 | return PDMDEV_SET_ERROR(pDevIns, VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES,
|
---|
704 | N_("Configuration error: Invalid \"IP\" value"));
|
---|
705 | memcpy(&ipaddr, &ip, sizeof(ipaddr));
|
---|
706 | if (!inet_aton(pThis->pszNetmask, &ip))
|
---|
707 | return PDMDEV_SET_ERROR(pDevIns, VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES,
|
---|
708 | N_("Configuration error: Invalid \"Netmask\" value"));
|
---|
709 | memcpy(&netmask, &ip, sizeof(netmask));
|
---|
710 | if (pThis->pszGateway)
|
---|
711 | {
|
---|
712 | if (!inet_aton(pThis->pszGateway, &ip))
|
---|
713 | return PDMDEV_SET_ERROR(pDevIns, VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES,
|
---|
714 | N_("Configuration error: Invalid \"Gateway\" value"));
|
---|
715 | memcpy(&gw, &ip, sizeof(gw));
|
---|
716 | }
|
---|
717 | else
|
---|
718 | {
|
---|
719 | inet_aton(pThis->pszIP, &ip);
|
---|
720 | memcpy(&gw, &ip, sizeof(gw));
|
---|
721 | }
|
---|
722 |
|
---|
723 | /*
|
---|
724 | * Initialize lwIP.
|
---|
725 | */
|
---|
726 | lwip_stats_init();
|
---|
727 | lwip_sys_init();
|
---|
728 | #if MEM_LIBC_MALLOC == 0
|
---|
729 | lwip_mem_init();
|
---|
730 | #endif
|
---|
731 | lwip_memp_init();
|
---|
732 | lwip_pbuf_init();
|
---|
733 | lwip_netif_init();
|
---|
734 | rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL, devINIPARPTimer, pThis,
|
---|
735 | TMTIMER_FLAGS_NO_CRIT_SECT, "lwIP ARP", &pThis->ARPTimer);
|
---|
736 | AssertRCReturn(rc, rc);
|
---|
737 | rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL, devINIPTCPFastTimer, pThis,
|
---|
738 | TMTIMER_FLAGS_NO_CRIT_SECT, "lwIP fast TCP", &pThis->TCPFastTimer);
|
---|
739 | AssertRCReturn(rc, rc);
|
---|
740 | TMTimerSetMillies(pThis->TCPFastTimer, TCP_FAST_INTERVAL);
|
---|
741 | rc = PDMDevHlpTMTimerCreate(pDevIns, TMCLOCK_VIRTUAL, devINIPTCPSlowTimer, pThis,
|
---|
742 | TMTIMER_FLAGS_NO_CRIT_SECT, "lwIP slow TCP", &pThis->TCPSlowTimer);
|
---|
743 | AssertRCReturn(rc, rc);
|
---|
744 | TMTimerSetMillies(pThis->TCPFastTimer, TCP_SLOW_INTERVAL);
|
---|
745 | #ifndef VBOX_WITH_NEW_LWIP
|
---|
746 | pThis->LWIPTcpInitSem = lwip_sys_sem_new(0);
|
---|
747 | {
|
---|
748 | lwip_tcpip_init(devINIPTcpipInitDone, &pThis->LWIPTcpInitSem);
|
---|
749 | lwip_sys_sem_wait(pThis->LWIPTcpInitSem);
|
---|
750 | }
|
---|
751 | #else
|
---|
752 | errRc = lwip_sys_sem_new(&pThis->LWIPTcpInitSem, 0);
|
---|
753 | /* VERR_INTERNAL_ERROR perhaps should be replaced with right error code */
|
---|
754 | AssertReturn(errRc == ERR_OK, VERR_INTERNAL_ERROR);
|
---|
755 | {
|
---|
756 | lwip_tcpip_init(devINIPTcpipInitDone, &pThis->LWIPTcpInitSem);
|
---|
757 | lwip_sys_sem_wait(&pThis->LWIPTcpInitSem, 0);
|
---|
758 | }
|
---|
759 | #endif
|
---|
760 |
|
---|
761 | /*
|
---|
762 | * Set up global pointer to interface data.
|
---|
763 | */
|
---|
764 | g_pDevINIPData = pThis;
|
---|
765 |
|
---|
766 | struct netif *ret;
|
---|
767 | pThis->IntNetIF.name[0] = 'I';
|
---|
768 | pThis->IntNetIF.name[1] = 'N';
|
---|
769 | ret = netif_add(&pThis->IntNetIF, &ipaddr, &netmask, &gw, NULL,
|
---|
770 | devINIPInterface, lwip_tcpip_input);
|
---|
771 | if (!ret)
|
---|
772 | return PDMDEV_SET_ERROR(pDevIns, VERR_NET_NO_NETWORK, N_("netif_add failed"));
|
---|
773 |
|
---|
774 | lwip_netif_set_default(&pThis->IntNetIF);
|
---|
775 | lwip_netif_set_up(&pThis->IntNetIF);
|
---|
776 |
|
---|
777 | /* link hack */
|
---|
778 | pThis->pLinkHack = g_pDevINILinkHack;
|
---|
779 |
|
---|
780 | LogFlow(("%s: return %Rrc\n", __FUNCTION__, rc));
|
---|
781 | return rc;
|
---|
782 | }
|
---|
783 |
|
---|
784 |
|
---|
785 | /**
|
---|
786 | * Query whether lwIP is initialized or not. Since there is only a single
|
---|
787 | * instance of this device ever for a VM, it can be a global function.
|
---|
788 | *
|
---|
789 | * @returns True if lwIP is initialized.
|
---|
790 | */
|
---|
791 | bool DevINIPConfigured(void)
|
---|
792 | {
|
---|
793 | return g_pDevINIPData != NULL;
|
---|
794 | }
|
---|
795 |
|
---|
796 |
|
---|
797 | /**
|
---|
798 | * Internal network IP stack device registration record.
|
---|
799 | */
|
---|
800 | const PDMDEVREG g_DeviceINIP =
|
---|
801 | {
|
---|
802 | /* u32Version */
|
---|
803 | PDM_DEVREG_VERSION,
|
---|
804 | /* szName */
|
---|
805 | "IntNetIP",
|
---|
806 | /* szRCMod/szR0Mod */
|
---|
807 | "",
|
---|
808 | "",
|
---|
809 | /* pszDescription */
|
---|
810 | "Internal Network IP stack device",
|
---|
811 | /* fFlags */
|
---|
812 | PDM_DEVREG_FLAGS_DEFAULT_BITS,
|
---|
813 | /* fClass. As this is used by the storage devices, it must come earlier. */
|
---|
814 | PDM_DEVREG_CLASS_VMM_DEV,
|
---|
815 | /* cMaxInstances */
|
---|
816 | 1,
|
---|
817 | /* cbInstance */
|
---|
818 | sizeof(DEVINTNETIP),
|
---|
819 | /* pfnConstruct */
|
---|
820 | devINIPConstruct,
|
---|
821 | /* pfnDestruct */
|
---|
822 | devINIPDestruct,
|
---|
823 | /* pfnRelocate */
|
---|
824 | NULL,
|
---|
825 | /* pfnMemSetup */
|
---|
826 | NULL,
|
---|
827 | /* pfnPowerOn */
|
---|
828 | NULL,
|
---|
829 | /* pfnReset */
|
---|
830 | NULL,
|
---|
831 | /* pfnSuspend */
|
---|
832 | NULL,
|
---|
833 | /* pfnResume */
|
---|
834 | NULL,
|
---|
835 | /* pfnAttach */
|
---|
836 | NULL,
|
---|
837 | /* pfnDetach */
|
---|
838 | NULL,
|
---|
839 | /* pfnQueryInterface */
|
---|
840 | NULL,
|
---|
841 | /* pfnInitComplete */
|
---|
842 | NULL,
|
---|
843 | /* pfnPowerOff */
|
---|
844 | NULL,
|
---|
845 | /* pfnSoftReset */
|
---|
846 | NULL,
|
---|
847 | /* u32VersionEnd */
|
---|
848 | PDM_DEVREG_VERSION
|
---|
849 | };
|
---|