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