VirtualBox

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

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