VirtualBox

source: vbox/trunk/src/VBox/NetworkServices/NetLib/VBoxNetUDP.cpp@ 92489

Last change on this file since 92489 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.6 KB
Line 
1/* $Id: VBoxNetUDP.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
2/** @file
3 * VBoxNetUDP - IntNet UDP Client Routines.
4 */
5
6/*
7 * Copyright (C) 2009-2020 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_DEFAULT
23#include "VBoxNetLib.h"
24#include <iprt/stream.h>
25#include <iprt/string.h>
26#include <iprt/rand.h>
27#include <VBox/log.h>
28#include <VBox/vmm/pdmnetinline.h>
29#include <VBox/intnetinline.h>
30
31
32/**
33 * Checks if the head of the receive ring is a UDP packet matching the given
34 * criteria.
35 *
36 * @returns Pointer to the data if it matches.
37 * @param pBuf The IntNet buffers.
38 * @param uDstPort The destination port to match.
39 * @param pDstMac The destination address to match if
40 * VBOXNETUDP_MATCH_UNICAST is specied.
41 * @param fFlags Flags indicating what to match and some debug stuff.
42 * See VBOXNETUDP_MATCH_*.
43 * @param pHdrs Where to return the pointers to the headers.
44 * Optional.
45 * @param pcb Where to return the size of the data on success.
46 */
47void *VBoxNetUDPMatch(PINTNETBUF pBuf, unsigned uDstPort, PCRTMAC pDstMac, uint32_t fFlags, PVBOXNETUDPHDRS pHdrs, size_t *pcb)
48{
49 /*
50 * Clear return values so we can return easier on mismatch.
51 */
52 *pcb = 0;
53 if (pHdrs)
54 {
55 pHdrs->pEth = NULL;
56 pHdrs->pIpv4 = NULL;
57 pHdrs->pUdp = NULL;
58 }
59
60 /*
61 * Valid IntNet Ethernet frame?
62 */
63 PCINTNETHDR pHdr = IntNetRingGetNextFrameToRead(&pBuf->Recv);
64 if ( !pHdr
65 || ( pHdr->u8Type != INTNETHDR_TYPE_FRAME
66 && pHdr->u8Type != INTNETHDR_TYPE_GSO))
67 return NULL;
68
69 size_t cbFrame = pHdr->cbFrame;
70 const void *pvFrame = IntNetHdrGetFramePtr(pHdr, pBuf);
71 PCPDMNETWORKGSO pGso = NULL;
72 if (pHdr->u8Type == INTNETHDR_TYPE_GSO)
73 {
74 pGso = (PCPDMNETWORKGSO)pvFrame;
75 if (!PDMNetGsoIsValid(pGso, cbFrame, cbFrame - sizeof(*pGso)))
76 return NULL;
77 /** @todo IPv6 UDP support, goes for this entire function really. Not really
78 * important yet since this is currently only used by the DHCP server. */
79 if (pGso->u8Type != PDMNETWORKGSOTYPE_IPV4_UDP)
80 return NULL;
81 pvFrame = pGso + 1;
82 cbFrame -= sizeof(*pGso);
83 }
84
85 PCRTNETETHERHDR pEthHdr = (PCRTNETETHERHDR)pvFrame;
86 if (pHdrs)
87 pHdrs->pEth = pEthHdr;
88
89#ifdef IN_RING3
90 /* Dump if to stderr/log if that's wanted. */
91 if (fFlags & VBOXNETUDP_MATCH_PRINT_STDERR)
92 {
93 RTStrmPrintf(g_pStdErr, "frame: cb=%04x dst=%.6Rhxs src=%.6Rhxs type=%04x%s\n",
94 cbFrame, &pEthHdr->DstMac, &pEthHdr->SrcMac, RT_BE2H_U16(pEthHdr->EtherType),
95 !memcmp(&pEthHdr->DstMac, pDstMac, sizeof(*pDstMac)) ? " Mine!" : "");
96 }
97#endif
98
99 /*
100 * Ethernet matching.
101 */
102
103 /* Ethernet min frame size. */
104 if (cbFrame < 64)
105 return NULL;
106
107 /* Match Ethertype: IPV4? */
108 /** @todo VLAN tagging? */
109 if (pEthHdr->EtherType != RT_H2BE_U16_C(RTNET_ETHERTYPE_IPV4))
110 return NULL;
111
112 /* Match destination address (ethernet) */
113 if ( ( !(fFlags & VBOXNETUDP_MATCH_UNICAST)
114 || memcmp(&pEthHdr->DstMac, pDstMac, sizeof(pEthHdr->DstMac)))
115 && ( !(fFlags & VBOXNETUDP_MATCH_BROADCAST)
116 || pEthHdr->DstMac.au16[0] != 0xffff
117 || pEthHdr->DstMac.au16[1] != 0xffff
118 || pEthHdr->DstMac.au16[2] != 0xffff))
119 return NULL;
120
121 /*
122 * If we're working on a GSO frame, we need to make sure the length fields
123 * are set correctly (they are usually set to 0).
124 */
125 if (pGso)
126 PDMNetGsoPrepForDirectUse(pGso, (void *)pvFrame, cbFrame, PDMNETCSUMTYPE_NONE);
127
128 /*
129 * IP validation and matching.
130 */
131 PCRTNETIPV4 pIpHdr = (PCRTNETIPV4)(pEthHdr + 1);
132 if (pHdrs)
133 pHdrs->pIpv4 = pIpHdr;
134
135 /* Protocol: UDP */
136 if (pIpHdr->ip_p != RTNETIPV4_PROT_UDP)
137 return NULL;
138
139 /* Valid IPv4 header? */
140 size_t const offIpHdr = (uintptr_t)pIpHdr - (uintptr_t)pEthHdr;
141 if (!RTNetIPv4IsHdrValid(pIpHdr, cbFrame - offIpHdr, cbFrame - offIpHdr, !pGso /*fChecksum*/))
142 return NULL;
143
144 /*
145 * UDP matching and validation.
146 */
147 PCRTNETUDP pUdpHdr = (PCRTNETUDP)((uint32_t *)pIpHdr + pIpHdr->ip_hl);
148 if (pHdrs)
149 pHdrs->pUdp = pUdpHdr;
150
151 /* Destination port */
152 if (RT_BE2H_U16(pUdpHdr->uh_dport) != uDstPort)
153 return NULL;
154
155 if (!pGso)
156 {
157 /* Validate the UDP header according to flags. */
158 size_t offUdpHdr = (uintptr_t)pUdpHdr - (uintptr_t)pEthHdr;
159 if (fFlags & (VBOXNETUDP_MATCH_CHECKSUM | VBOXNETUDP_MATCH_REQUIRE_CHECKSUM))
160 {
161 if (!RTNetIPv4IsUDPValid(pIpHdr, pUdpHdr, pUdpHdr + 1, cbFrame - offUdpHdr, true /*fChecksum*/))
162 return NULL;
163 if ( (fFlags & VBOXNETUDP_MATCH_REQUIRE_CHECKSUM)
164 && !pUdpHdr->uh_sum)
165 return NULL;
166 }
167 else
168 {
169 if (!RTNetIPv4IsUDPSizeValid(pIpHdr, pUdpHdr, cbFrame - offUdpHdr))
170 return NULL;
171 }
172 }
173
174 /*
175 * We've got a match!
176 */
177 *pcb = RT_N2H_U16(pUdpHdr->uh_ulen) - sizeof(*pUdpHdr);
178 return (void *)(pUdpHdr + 1);
179}
180
181
182/** Internal worker for VBoxNetUDPUnicast and VBoxNetUDPBroadcast. */
183static int vboxnetudpSend(PSUPDRVSESSION pSession, INTNETIFHANDLE hIf, PINTNETBUF pBuf,
184 RTNETADDRIPV4 SrcIPv4Addr, PCRTMAC pSrcMacAddr, unsigned uSrcPort,
185 RTNETADDRIPV4 DstIPv4Addr, PCRTMAC pDstMacAddr, unsigned uDstPort,
186 void const *pvData, size_t cbData)
187{
188 INTNETSEG aSegs[4];
189
190 /* the Ethernet header */
191 RTNETETHERHDR EtherHdr;
192 EtherHdr.DstMac = *pDstMacAddr;
193 EtherHdr.SrcMac = *pSrcMacAddr;
194 EtherHdr.EtherType = RT_H2BE_U16_C(RTNET_ETHERTYPE_IPV4);
195
196 aSegs[0].pv = &EtherHdr;
197 aSegs[0].cb = sizeof(EtherHdr);
198 aSegs[0].Phys = NIL_RTHCPHYS;
199
200 /* the IP header */
201 RTNETIPV4 IpHdr;
202 unsigned cbIdHdr = RT_UOFFSETOF(RTNETIPV4, ip_options);
203 IpHdr.ip_v = 4;
204 IpHdr.ip_hl = cbIdHdr >> 2;
205 IpHdr.ip_tos = 0;
206 IpHdr.ip_len = RT_H2BE_U16((uint16_t)(cbData + sizeof(RTNETUDP) + cbIdHdr));
207 IpHdr.ip_id = (uint16_t)RTRandU32();
208 IpHdr.ip_off = 0;
209 IpHdr.ip_ttl = 255;
210 IpHdr.ip_p = RTNETIPV4_PROT_UDP;
211 IpHdr.ip_sum = 0;
212 IpHdr.ip_src = SrcIPv4Addr;
213 IpHdr.ip_dst = DstIPv4Addr;
214 IpHdr.ip_sum = RTNetIPv4HdrChecksum(&IpHdr);
215
216 aSegs[1].pv = &IpHdr;
217 aSegs[1].cb = cbIdHdr;
218 aSegs[1].Phys = NIL_RTHCPHYS;
219
220
221 /* the UDP bit */
222 RTNETUDP UdpHdr;
223 UdpHdr.uh_sport = RT_H2BE_U16(uSrcPort);
224 UdpHdr.uh_dport = RT_H2BE_U16(uDstPort);
225 UdpHdr.uh_ulen = RT_H2BE_U16((uint16_t)(cbData + sizeof(RTNETUDP)));
226#if 0
227 UdpHdr.uh_sum = 0; /* pretend checksumming is disabled */
228#else
229 UdpHdr.uh_sum = RTNetIPv4UDPChecksum(&IpHdr, &UdpHdr, pvData);
230#endif
231
232 aSegs[2].pv = &UdpHdr;
233 aSegs[2].cb = sizeof(UdpHdr);
234 aSegs[2].Phys = NIL_RTHCPHYS;
235
236 /* the payload */
237 aSegs[3].pv = (void *)pvData;
238 aSegs[3].cb = (uint32_t)cbData;
239 aSegs[3].Phys = NIL_RTHCPHYS;
240
241
242 /* send it */
243 return VBoxNetIntIfSend(pSession, hIf, pBuf, RT_ELEMENTS(aSegs), &aSegs[0], true /* fFlush */);
244}
245
246
247/**
248 * Sends an unicast UDP packet.
249 *
250 * @returns VBox status code.
251 * @param pSession The support driver session handle.
252 * @param hIf The interface handle.
253 * @param pBuf The interface buffer.
254 * @param SrcIPv4Addr The source IPv4 address.
255 * @param pSrcMacAddr The source MAC address.
256 * @param uSrcPort The source port number.
257 * @param DstIPv4Addr The destination IPv4 address. Can be broadcast.
258 * @param pDstMacAddr The destination MAC address.
259 * @param uDstPort The destination port number.
260 * @param pvData The data payload.
261 * @param cbData The size of the data payload.
262 */
263int VBoxNetUDPUnicast(PSUPDRVSESSION pSession, INTNETIFHANDLE hIf, PINTNETBUF pBuf,
264 RTNETADDRIPV4 SrcIPv4Addr, PCRTMAC pSrcMacAddr, unsigned uSrcPort,
265 RTNETADDRIPV4 DstIPv4Addr, PCRTMAC pDstMacAddr, unsigned uDstPort,
266 void const *pvData, size_t cbData)
267{
268 return vboxnetudpSend(pSession, hIf, pBuf,
269 SrcIPv4Addr, pSrcMacAddr, uSrcPort,
270 DstIPv4Addr, pDstMacAddr, uDstPort,
271 pvData, cbData);
272}
273
274
275/**
276 * Sends a broadcast UDP packet.
277 *
278 * @returns VBox status code.
279 * @param pSession The support driver session handle.
280 * @param hIf The interface handle.
281 * @param pBuf The interface buffer.
282 * @param SrcIPv4Addr The source IPv4 address.
283 * @param pSrcMacAddr The source MAC address.
284 * @param uSrcPort The source port number.
285 * @param uDstPort The destination port number.
286 * @param pvData The data payload.
287 * @param cbData The size of the data payload.
288 */
289int VBoxNetUDPBroadcast(PSUPDRVSESSION pSession, INTNETIFHANDLE hIf, PINTNETBUF pBuf,
290 RTNETADDRIPV4 SrcIPv4Addr, PCRTMAC pSrcMacAddr, unsigned uSrcPort,
291 unsigned uDstPort,
292 void const *pvData, size_t cbData)
293{
294 RTNETADDRIPV4 IPv4AddrBrdCast;
295 IPv4AddrBrdCast.u = UINT32_C(0xffffffff);
296 RTMAC MacBrdCast;
297 MacBrdCast.au16[0] = MacBrdCast.au16[1] = MacBrdCast.au16[2] = UINT16_C(0xffff);
298
299 return vboxnetudpSend(pSession, hIf, pBuf,
300 SrcIPv4Addr, pSrcMacAddr, uSrcPort,
301 IPv4AddrBrdCast, &MacBrdCast, uDstPort,
302 pvData, cbData);
303}
304
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