VirtualBox

source: vbox/trunk/src/VBox/NetworkServices/UDPLib/VBoxNetUDP.cpp@ 17781

Last change on this file since 17781 was 17781, checked in by vboxsync, 16 years ago

bufixes + arp in progress

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.4 KB
Line 
1/* $Id: VBoxNetUDP.cpp 17781 2009-03-12 23:21:50Z vboxsync $ */
2/** @file
3 * VBoxNetUDP - UDP Library for IntNet.
4 */
5
6/*
7 * Copyright (C) 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* Header Files *
24*******************************************************************************/
25#define LOG_GROUP LOG_GROUP_DEFAULT
26#include "VBoxNetUDP.h"
27#include <iprt/stream.h>
28#include <iprt/string.h>
29#include <iprt/rand.h>
30#include <VBox/vmm.h>
31#include <VBox/log.h>
32
33
34/**
35 * Checks if the head of the receive ring is a UDP packet matching the given
36 * criteria.
37 *
38 * @returns Pointer to the data if it matches.
39 * @param pBuf The IntNet buffers.
40 * @param uDstPort The destination port to match.
41 * @param pDstMac The destination address to match if
42 * VBOXNETUDP_MATCH_UNICAST is specied.
43 * @param fFlags Flags indicating what to match and some debug stuff.
44 * See VBOXNETUDP_MATCH_*.
45 * @param pHdrs Where to return the pointers to the headers.
46 * Optional.
47 * @param pcb Where to return the size of the data on success.
48 */
49void *VBoxNetUDPMatch(PCINTNETBUF pBuf, unsigned uDstPort, PCRTMAC pDstMac, uint32_t fFlags, PVBOXNETUDPHDRS pHdrs, size_t *pcb)
50{
51 /*
52 * Clear return values so we can return easier on mismatch.
53 */
54 *pcb = 0;
55 if (pHdrs)
56 {
57 pHdrs->pEth = NULL;
58 pHdrs->pIpv4 = NULL;
59 pHdrs->pUdp = NULL;
60 }
61
62 /*
63 * Valid IntNet Ethernet frame?
64 */
65 PCINTNETHDR pHdr = (PINTNETHDR)((uintptr_t)pBuf + pBuf->Recv.offRead);
66 if (pHdr->u16Type != INTNETHDR_TYPE_FRAME)
67 return NULL;
68
69 size_t cbFrame = pHdr->cbFrame;
70 const void *pvFrame = INTNETHdrGetFramePtr(pHdr, pBuf);
71 PCRTNETETHERHDR pEthHdr = (PCRTNETETHERHDR)pvFrame;
72 if (pHdrs)
73 pHdrs->pEth = pEthHdr;
74
75#ifdef IN_RING3
76 /* Dump if to stderr/log if that's wanted. */
77 if (fFlags & VBOXNETUDP_MATCH_PRINT_STDERR)
78 {
79 RTStrmPrintf(g_pStdErr, "frame: cb=%04x dst=%.6Rhxs src=%.6Rhxs type=%04x%s\n",
80 cbFrame, &pEthHdr->DstMac, &pEthHdr->SrcMac, RT_BE2H_U16(pEthHdr->EtherType),
81 !memcmp(&pEthHdr->DstMac, pDstMac, sizeof(*pDstMac)) ? " Mine!" : "");
82 }
83#endif
84
85 /*
86 * Ethernet matching.
87 */
88
89 /* Ethernet min frame size. */
90 if (cbFrame < 64)
91 return NULL;
92
93 /* Match Ethertype: IPV4? */
94 /** @todo VLAN tagging? */
95 if (pEthHdr->EtherType != RT_H2BE_U16_C(RTNET_ETHERTYPE_IPV4))
96 return NULL;
97
98 /* Match destination address (ethernet) */
99 if ( ( !(fFlags & VBOXNETUDP_MATCH_UNICAST)
100 || memcmp(&pEthHdr->DstMac, pDstMac, sizeof(pEthHdr->DstMac)))
101 && ( !(fFlags & VBOXNETUDP_MATCH_BROADCAST)
102 || pEthHdr->DstMac.au16[0] != 0xffff
103 || pEthHdr->DstMac.au16[1] != 0xffff
104 || pEthHdr->DstMac.au16[2] != 0xffff))
105 return NULL;
106
107 /*
108 * IP validation and matching.
109 */
110 PCRTNETIPV4 pIpHdr = (PCRTNETIPV4)(pEthHdr + 1);
111 if (pHdrs)
112 pHdrs->pIpv4 = pIpHdr;
113
114 /* Protocol: UDP */
115 if (pIpHdr->ip_p != RTNETIPV4_PROT_UDP)
116 return NULL;
117
118 /* Valid IPv4 header? */
119 size_t const offIpHdr = (uintptr_t)pIpHdr - (uintptr_t)pEthHdr;
120 if (!RTNetIPv4IsHdrValid(pIpHdr, cbFrame - offIpHdr, cbFrame - offIpHdr))
121 return NULL;
122
123 /*
124 * UDP matching and validation.
125 */
126 PCRTNETUDP pUdpHdr = (PCRTNETUDP)((uint32_t *)pIpHdr + pIpHdr->ip_hl);
127 if (pHdrs)
128 pHdrs->pUdp = pUdpHdr;
129
130 /* Destination port */
131 if (RT_BE2H_U16(pUdpHdr->uh_dport) != uDstPort)
132 return NULL;
133
134 /* Validate the UDP header according to flags. */
135 size_t offUdpHdr = (uintptr_t)pUdpHdr - (uintptr_t)pEthHdr;
136 if (fFlags & (VBOXNETUDP_MATCH_CHECKSUM | VBOXNETUDP_MATCH_REQUIRE_CHECKSUM))
137 {
138 if (!RTNetIPv4IsUDPValid(pIpHdr, pUdpHdr, pUdpHdr + 1, cbFrame - offUdpHdr))
139 return NULL;
140 if ( (fFlags & VBOXNETUDP_MATCH_REQUIRE_CHECKSUM)
141 && !pUdpHdr->uh_sum)
142 return NULL;
143 }
144 else
145 {
146 if (!RTNetIPv4IsUDPSizeValid(pIpHdr, pUdpHdr, cbFrame - offUdpHdr))
147 return NULL;
148 }
149
150 /*
151 * We've got a match!
152 */
153 *pcb = pUdpHdr->uh_ulen - sizeof(*pUdpHdr);
154 return (void *)(pUdpHdr + 1);
155}
156
157
158/**
159 * Flushes the send buffer.
160 *
161 * @returns VBox status code.
162 * @param pSession The support driver session.
163 * @param hIf The interface handle to flush.
164 */
165static int vboxnetudpFlush(PSUPDRVSESSION pSession, INTNETIFHANDLE hIf)
166{
167 INTNETIFSENDREQ SendReq;
168 SendReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
169 SendReq.Hdr.cbReq = sizeof(SendReq);
170 SendReq.pSession = pSession;
171 SendReq.hIf = hIf;
172 return SUPCallVMMR0Ex(NIL_RTR0PTR, VMMR0_DO_INTNET_IF_SEND, 0, &SendReq.Hdr);
173}
174
175
176/**
177 * Copys the SG segments into the specified fram.
178 *
179 * @param pvFrame The frame buffer.
180 * @param cSegs The number of segments.
181 * @param paSegs The segments.
182 */
183static void vboxnetudpCopySG(void *pvFrame, size_t cSegs, PCINTNETSEG paSegs)
184{
185 uint8_t *pbDst = (uint8_t *)pvFrame;
186 for (size_t iSeg = 0; iSeg < cSegs; iSeg++)
187 {
188 memcpy(pbDst, paSegs[iSeg].pv, paSegs[iSeg].cb);
189 pbDst += paSegs[iSeg].cb;
190 }
191}
192
193
194/**
195 * Writes a frame packet to the buffer.
196 *
197 * @returns VBox status code.
198 * @param pBuf The buffer.
199 * @param pRingBuf The ring buffer to read from.
200 * @param cSegs The number of segments.
201 * @param paSegs The segments.
202 * @remark This is the same as INTNETRingWriteFrame and
203 * drvIntNetRingWriteFrame.
204 */
205static int vboxnetudpRingWriteFrame(PINTNETBUF pBuf, PINTNETRINGBUF pRingBuf,
206 size_t cSegs, PCINTNETSEG paSegs)
207{
208 /*
209 * Validate input.
210 */
211 Assert(pBuf);
212 Assert(pRingBuf);
213 uint32_t offWrite = pRingBuf->offWrite;
214 Assert(offWrite == RT_ALIGN_32(offWrite, sizeof(INTNETHDR)));
215 uint32_t offRead = pRingBuf->offRead;
216 Assert(offRead == RT_ALIGN_32(offRead, sizeof(INTNETHDR)));
217 AssertPtr(paSegs);
218 Assert(cSegs > 0);
219
220 /* Calc frame size. */
221 uint32_t cbFrame = 0;
222 for (size_t iSeg = 0; iSeg < cSegs; iSeg++)
223 cbFrame += paSegs[iSeg].cb;
224
225 Assert(cbFrame >= sizeof(RTMAC) * 2);
226
227
228 const uint32_t cb = RT_ALIGN_32(cbFrame, sizeof(INTNETHDR));
229 if (offRead <= offWrite)
230 {
231 /*
232 * Try fit it all before the end of the buffer.
233 */
234 if (pRingBuf->offEnd - offWrite >= cb + sizeof(INTNETHDR))
235 {
236 PINTNETHDR pHdr = (PINTNETHDR)((uint8_t *)pBuf + offWrite);
237 pHdr->u16Type = INTNETHDR_TYPE_FRAME;
238 pHdr->cbFrame = cbFrame;
239 pHdr->offFrame = sizeof(INTNETHDR);
240
241 vboxnetudpCopySG(pHdr + 1, cSegs, paSegs);
242
243 offWrite += cb + sizeof(INTNETHDR);
244 Assert(offWrite <= pRingBuf->offEnd && offWrite >= pRingBuf->offStart);
245 if (offWrite >= pRingBuf->offEnd)
246 offWrite = pRingBuf->offStart;
247 Log2(("WriteFrame: offWrite: %#x -> %#x (1)\n", pRingBuf->offWrite, offWrite));
248 ASMAtomicXchgU32(&pRingBuf->offWrite, offWrite);
249 return VINF_SUCCESS;
250 }
251
252 /*
253 * Try fit the frame at the start of the buffer.
254 * (The header fits before the end of the buffer because of alignment.)
255 */
256 AssertMsg(pRingBuf->offEnd - offWrite >= sizeof(INTNETHDR), ("offEnd=%x offWrite=%x\n", pRingBuf->offEnd, offWrite));
257 if (offRead - pRingBuf->offStart > cb) /* not >= ! */
258 {
259 PINTNETHDR pHdr = (PINTNETHDR)((uint8_t *)pBuf + offWrite);
260 void *pvFrameOut = (PINTNETHDR)((uint8_t *)pBuf + pRingBuf->offStart);
261 pHdr->u16Type = INTNETHDR_TYPE_FRAME;
262 pHdr->cbFrame = cbFrame;
263 pHdr->offFrame = (intptr_t)pvFrameOut - (intptr_t)pHdr;
264
265 vboxnetudpCopySG(pvFrameOut, cSegs, paSegs);
266
267 offWrite = pRingBuf->offStart + cb;
268 ASMAtomicXchgU32(&pRingBuf->offWrite, offWrite);
269 Log2(("WriteFrame: offWrite: %#x -> %#x (2)\n", pRingBuf->offWrite, offWrite));
270 return VINF_SUCCESS;
271 }
272 }
273 /*
274 * The reader is ahead of the writer, try fit it into that space.
275 */
276 else if (offRead - offWrite > cb + sizeof(INTNETHDR)) /* not >= ! */
277 {
278 PINTNETHDR pHdr = (PINTNETHDR)((uint8_t *)pBuf + offWrite);
279 pHdr->u16Type = INTNETHDR_TYPE_FRAME;
280 pHdr->cbFrame = cbFrame;
281 pHdr->offFrame = sizeof(INTNETHDR);
282
283 vboxnetudpCopySG(pHdr + 1, cSegs, paSegs);
284
285 offWrite += cb + sizeof(INTNETHDR);
286 ASMAtomicXchgU32(&pRingBuf->offWrite, offWrite);
287 Log2(("WriteFrame: offWrite: %#x -> %#x (3)\n", pRingBuf->offWrite, offWrite));
288 return VINF_SUCCESS;
289 }
290
291 /* (it didn't fit) */
292 /** @todo stats */
293 return VERR_BUFFER_OVERFLOW;
294}
295
296
297/** Internal worker for VBoxNetUDPUnicast and VBoxNetUDPBroadcast. */
298static int vboxnetudpSend(PSUPDRVSESSION pSession, INTNETIFHANDLE hIf, PINTNETBUF pBuf,
299 RTNETADDRIPV4 SrcIPv4Addr, PCRTMAC pSrcMacAddr, unsigned uSrcPort,
300 RTNETADDRIPV4 DstIPv4Addr, PCRTMAC pDstMacAddr, unsigned uDstPort,
301 void const *pvData, size_t cbData)
302{
303 INTNETSEG aSegs[4];
304
305 /* the Ethernet header */
306 RTNETETHERHDR EtherHdr;
307 EtherHdr.DstMac = *pDstMacAddr;
308 EtherHdr.SrcMac = *pSrcMacAddr;
309 EtherHdr.EtherType = RT_H2BE_U16_C(RTNET_ETHERTYPE_IPV4);
310
311 aSegs[0].pv = &EtherHdr;
312 aSegs[0].cb = sizeof(EtherHdr);
313 aSegs[0].Phys = NIL_RTHCPHYS;
314
315 /* the IP header */
316 RTNETIPV4 IpHdr;
317 size_t cbIdHdr = RT_UOFFSETOF(RTNETIPV4, ip_options);
318 IpHdr.ip_v = 4;
319 IpHdr.ip_hl = cbIdHdr / sizeof(uint32_t);
320 IpHdr.ip_tos = 0;
321 IpHdr.ip_len = RT_H2BE_U16(cbData + sizeof(RTNETUDP) + cbIdHdr);
322 IpHdr.ip_id = (uint16_t)RTRandU32();
323 IpHdr.ip_off = 0;
324 IpHdr.ip_ttl = 255;
325 IpHdr.ip_p = RTNETIPV4_PROT_UDP;
326 IpHdr.ip_sum = 0;
327 IpHdr.ip_src = SrcIPv4Addr;
328 IpHdr.ip_dst = DstIPv4Addr;
329 IpHdr.ip_sum = RTNetIPv4HdrChecksum(&IpHdr);
330
331 aSegs[1].pv = &IpHdr;
332 aSegs[1].cb = cbIdHdr;
333 aSegs[1].Phys = NIL_RTHCPHYS;
334
335
336 /* the UDP bit */
337 RTNETUDP UdpHdr;
338 UdpHdr.uh_sport = RT_H2BE_U16(uSrcPort);
339 UdpHdr.uh_dport = RT_H2BE_U16(uDstPort);
340 UdpHdr.uh_ulen = RT_H2BE_U16(cbData + sizeof(RTNETUDP));
341#if 0
342 UdpHdr.uh_sum = 0; /* pretend checksumming is disabled */
343#else
344 UdpHdr.uh_sum = RTNetIPv4UDPChecksum(&IpHdr, &UdpHdr, pvData);
345#endif
346
347 aSegs[2].pv = &UdpHdr;
348 aSegs[2].cb = sizeof(UdpHdr);
349 aSegs[2].Phys = NIL_RTHCPHYS;
350
351 /* the payload */
352 aSegs[3].pv = (void *)pvData;
353 aSegs[3].cb = cbData;
354 aSegs[3].Phys = NIL_RTHCPHYS;
355
356
357 /* write it */
358 int rc = vboxnetudpRingWriteFrame(pBuf, &pBuf->Send, RT_ELEMENTS(aSegs), &aSegs[0]);
359 int rc2 = vboxnetudpFlush(pSession, hIf);
360 if (RT_FAILURE(rc))
361 {
362 rc = vboxnetudpRingWriteFrame(pBuf, &pBuf->Send, RT_ELEMENTS(aSegs), &aSegs[0]);
363 rc2 = vboxnetudpFlush(pSession, hIf);
364 }
365
366 if (RT_SUCCESS(rc))
367 rc = rc2;
368 return rc;
369}
370
371
372/**
373 * Sends an unicast UDP packet.
374 *
375 * @returns VBox status code.
376 * @param pSession The support driver session handle.
377 * @param hIf The interface handle.
378 * @param pBuf The interface buffer.
379 * @param SrcIPv4Addr The source IPv4 address.
380 * @param pSrcMacAddr The source MAC address.
381 * @param uSrcPort The source port number.
382 * @param DstIPv4Addr The destination IPv4 address. Can be broadcast.
383 * @param pDstMacAddr The destination MAC address.
384 * @param uDstPort The destination port number.
385 * @param pvData The data payload.
386 * @param cbData The size of the data payload.
387 */
388int VBoxNetUDPUnicast(PSUPDRVSESSION pSession, INTNETIFHANDLE hIf, PINTNETBUF pBuf,
389 RTNETADDRIPV4 SrcIPv4Addr, PCRTMAC pSrcMacAddr, unsigned uSrcPort,
390 RTNETADDRIPV4 DstIPv4Addr, PCRTMAC pDstMacAddr, unsigned uDstPort,
391 void const *pvData, size_t cbData)
392{
393 return vboxnetudpSend(pSession, hIf, pBuf,
394 SrcIPv4Addr, pSrcMacAddr, uSrcPort,
395 DstIPv4Addr, pDstMacAddr, uDstPort,
396 pvData, cbData);
397}
398
399
400/**
401 * Sends a broadcast UDP packet.
402 *
403 * @returns VBox status code.
404 * @param pSession The support driver session handle.
405 * @param hIf The interface handle.
406 * @param pBuf The interface buffer.
407 * @param SrcIPv4Addr The source IPv4 address.
408 * @param pSrcMacAddr The source MAC address.
409 * @param uSrcPort The source port number.
410 * @param uDstPort The destination port number.
411 * @param pvData The data payload.
412 * @param cbData The size of the data payload.
413 */
414int VBoxNetUDPBroadcast(PSUPDRVSESSION pSession, INTNETIFHANDLE hIf, PINTNETBUF pBuf,
415 RTNETADDRIPV4 SrcIPv4Addr, PCRTMAC pSrcMacAddr, unsigned uSrcPort,
416 unsigned uDstPort,
417 void const *pvData, size_t cbData)
418{
419 RTNETADDRIPV4 IPv4AddrBrdCast;
420 IPv4AddrBrdCast.u = UINT32_C(0xffffffff);
421 RTMAC MacBrdCast;
422 MacBrdCast.au16[0] = MacBrdCast.au16[1] = MacBrdCast.au16[2] = UINT16_C(0xffff);
423
424 return vboxnetudpSend(pSession, hIf, pBuf,
425 SrcIPv4Addr, pSrcMacAddr, uSrcPort,
426 IPv4AddrBrdCast, &MacBrdCast, uDstPort,
427 pvData, cbData);
428}
429
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette