summaryrefslogtreecommitdiffstats
path: root/src/VBox/NetworkServices/NetLib/VBoxNetUDP.cpp
blob: 0981ccecb4e8b67d206c52018276d569e2d5f061 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/* $Id: VBoxNetUDP.cpp $ */
/** @file
 * VBoxNetUDP - IntNet UDP Client Routines.
 */

/*
 * Copyright (C) 2009-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#define LOG_GROUP LOG_GROUP_DEFAULT
#include "VBoxNetLib.h"
#include <iprt/stream.h>
#include <iprt/string.h>
#include <iprt/rand.h>
#include <VBox/log.h>
#include <VBox/vmm/pdmnetinline.h>
#include <VBox/intnetinline.h>


/**
 * Checks if the head of the receive ring is a UDP packet matching the given
 * criteria.
 *
 * @returns Pointer to the data if it matches.
 * @param   pBuf            The IntNet buffers.
 * @param   uDstPort        The destination port to match.
 * @param   pDstMac         The destination address to match if
 *                          VBOXNETUDP_MATCH_UNICAST is specied.
 * @param   fFlags          Flags indicating what to match and some debug stuff.
 *                          See VBOXNETUDP_MATCH_*.
 * @param   pHdrs           Where to return the pointers to the headers.
 *                          Optional.
 * @param   pcb             Where to return the size of the data on success.
 */
void *VBoxNetUDPMatch(PINTNETBUF pBuf, unsigned uDstPort, PCRTMAC pDstMac, uint32_t fFlags, PVBOXNETUDPHDRS pHdrs, size_t *pcb)
{
    /*
     * Clear return values so we can return easier on mismatch.
     */
    *pcb = 0;
    if (pHdrs)
    {
        pHdrs->pEth  = NULL;
        pHdrs->pIpv4 = NULL;
        pHdrs->pUdp  = NULL;
    }

    /*
     * Valid IntNet Ethernet frame?
     */
    PCINTNETHDR pHdr = IntNetRingGetNextFrameToRead(&pBuf->Recv);
    if (    !pHdr
        ||  (   pHdr->u8Type != INTNETHDR_TYPE_FRAME
             && pHdr->u8Type != INTNETHDR_TYPE_GSO))
        return NULL;

    size_t          cbFrame = pHdr->cbFrame;
    const void     *pvFrame = IntNetHdrGetFramePtr(pHdr, pBuf);
    PCPDMNETWORKGSO pGso    = NULL;
    if (pHdr->u8Type == INTNETHDR_TYPE_GSO)
    {
        pGso = (PCPDMNETWORKGSO)pvFrame;
        if (!PDMNetGsoIsValid(pGso, cbFrame, cbFrame - sizeof(*pGso)))
            return NULL;
        /** @todo IPv6 UDP support, goes for this entire function really.  Not really
         *        important yet since this is currently only used by the DHCP server. */
        if (pGso->u8Type != PDMNETWORKGSOTYPE_IPV4_UDP)
            return NULL;
        pvFrame  = pGso + 1;
        cbFrame -= sizeof(*pGso);
    }

    PCRTNETETHERHDR pEthHdr = (PCRTNETETHERHDR)pvFrame;
    if (pHdrs)
        pHdrs->pEth = pEthHdr;

#ifdef IN_RING3
    /* Dump if to stderr/log if that's wanted. */
    if (fFlags & VBOXNETUDP_MATCH_PRINT_STDERR)
    {
        RTStrmPrintf(g_pStdErr, "frame: cb=%04x dst=%.6Rhxs src=%.6Rhxs type=%04x%s\n",
                     cbFrame, &pEthHdr->DstMac, &pEthHdr->SrcMac, RT_BE2H_U16(pEthHdr->EtherType),
                     !memcmp(&pEthHdr->DstMac, pDstMac, sizeof(*pDstMac)) ? " Mine!" : "");
    }
#endif

    /*
     * Ethernet matching.
     */

    /* Ethernet min frame size. */
    if (cbFrame < 64)
        return NULL;

    /* Match Ethertype: IPV4? */
    /** @todo VLAN tagging? */
    if (pEthHdr->EtherType != RT_H2BE_U16_C(RTNET_ETHERTYPE_IPV4))
        return NULL;

    /* Match destination address (ethernet) */
    if (    (   !(fFlags & VBOXNETUDP_MATCH_UNICAST)
             || memcmp(&pEthHdr->DstMac, pDstMac, sizeof(pEthHdr->DstMac)))
        &&  (   !(fFlags & VBOXNETUDP_MATCH_BROADCAST)
             || pEthHdr->DstMac.au16[0] != 0xffff
             || pEthHdr->DstMac.au16[1] != 0xffff
             || pEthHdr->DstMac.au16[2] != 0xffff))
        return NULL;

    /*
     * If we're working on a GSO frame, we need to make sure the length fields
     * are set correctly (they are usually set to 0).
     */
    if (pGso)
        PDMNetGsoPrepForDirectUse(pGso, (void *)pvFrame, cbFrame, PDMNETCSUMTYPE_NONE);

    /*
     * IP validation and matching.
     */
    PCRTNETIPV4 pIpHdr = (PCRTNETIPV4)(pEthHdr + 1);
    if (pHdrs)
        pHdrs->pIpv4 = pIpHdr;

    /* Protocol: UDP */
    if (pIpHdr->ip_p != RTNETIPV4_PROT_UDP)
        return NULL;

    /* Valid IPv4 header? */
    size_t const offIpHdr = (uintptr_t)pIpHdr - (uintptr_t)pEthHdr;
    if (!RTNetIPv4IsHdrValid(pIpHdr, cbFrame - offIpHdr, cbFrame - offIpHdr, !pGso /*fChecksum*/))
        return NULL;

    /*
     * UDP matching and validation.
     */
    PCRTNETUDP  pUdpHdr   = (PCRTNETUDP)((uint32_t *)pIpHdr + pIpHdr->ip_hl);
    if (pHdrs)
        pHdrs->pUdp = pUdpHdr;

    /* Destination port */
    if (RT_BE2H_U16(pUdpHdr->uh_dport) != uDstPort)
        return NULL;

    if (!pGso)
    {
        /* Validate the UDP header according to flags. */
        size_t      offUdpHdr = (uintptr_t)pUdpHdr - (uintptr_t)pEthHdr;
        if (fFlags & (VBOXNETUDP_MATCH_CHECKSUM | VBOXNETUDP_MATCH_REQUIRE_CHECKSUM))
        {
            if (!RTNetIPv4IsUDPValid(pIpHdr, pUdpHdr, pUdpHdr + 1, cbFrame - offUdpHdr, true /*fChecksum*/))
                return NULL;
            if (    (fFlags & VBOXNETUDP_MATCH_REQUIRE_CHECKSUM)
                &&  !pUdpHdr->uh_sum)
                return NULL;
        }
        else
        {
            if (!RTNetIPv4IsUDPSizeValid(pIpHdr, pUdpHdr, cbFrame - offUdpHdr))
                return NULL;
        }
    }

    /*
     * We've got a match!
     */
    *pcb = RT_N2H_U16(pUdpHdr->uh_ulen) - sizeof(*pUdpHdr);
    return (void *)(pUdpHdr + 1);
}


/** Internal worker for VBoxNetUDPUnicast and VBoxNetUDPBroadcast. */
static int vboxnetudpSend(PSUPDRVSESSION pSession, INTNETIFHANDLE hIf, PINTNETBUF pBuf,
                          RTNETADDRIPV4 SrcIPv4Addr, PCRTMAC pSrcMacAddr, unsigned uSrcPort,
                          RTNETADDRIPV4 DstIPv4Addr, PCRTMAC pDstMacAddr, unsigned uDstPort,
                          void const *pvData, size_t cbData)
{
    INTNETSEG aSegs[4];

    /* the Ethernet header */
    RTNETETHERHDR EtherHdr;
    EtherHdr.DstMac     = *pDstMacAddr;
    EtherHdr.SrcMac     = *pSrcMacAddr;
    EtherHdr.EtherType  = RT_H2BE_U16_C(RTNET_ETHERTYPE_IPV4);

    aSegs[0].pv   = &EtherHdr;
    aSegs[0].cb   = sizeof(EtherHdr);
    aSegs[0].Phys = NIL_RTHCPHYS;

    /* the IP header */
    RTNETIPV4 IpHdr;
    unsigned  cbIdHdr = RT_UOFFSETOF(RTNETIPV4, ip_options);
    IpHdr.ip_v          = 4;
    IpHdr.ip_hl         = cbIdHdr >> 2;
    IpHdr.ip_tos        = 0;
    IpHdr.ip_len        = RT_H2BE_U16((uint16_t)(cbData + sizeof(RTNETUDP) + cbIdHdr));
    IpHdr.ip_id         = (uint16_t)RTRandU32();
    IpHdr.ip_off        = 0;
    IpHdr.ip_ttl        = 255;
    IpHdr.ip_p          = RTNETIPV4_PROT_UDP;
    IpHdr.ip_sum        = 0;
    IpHdr.ip_src        = SrcIPv4Addr;
    IpHdr.ip_dst        = DstIPv4Addr;
    IpHdr.ip_sum        = RTNetIPv4HdrChecksum(&IpHdr);

    aSegs[1].pv   = &IpHdr;
    aSegs[1].cb   = cbIdHdr;
    aSegs[1].Phys = NIL_RTHCPHYS;


    /* the UDP bit */
    RTNETUDP UdpHdr;
    UdpHdr.uh_sport     = RT_H2BE_U16(uSrcPort);
    UdpHdr.uh_dport     = RT_H2BE_U16(uDstPort);
    UdpHdr.uh_ulen      = RT_H2BE_U16((uint16_t)(cbData + sizeof(RTNETUDP)));
#if 0
    UdpHdr.uh_sum       = 0; /* pretend checksumming is disabled */
#else
    UdpHdr.uh_sum       = RTNetIPv4UDPChecksum(&IpHdr, &UdpHdr, pvData);
#endif

    aSegs[2].pv   = &UdpHdr;
    aSegs[2].cb   = sizeof(UdpHdr);
    aSegs[2].Phys = NIL_RTHCPHYS;

    /* the payload */
    aSegs[3].pv   = (void *)pvData;
    aSegs[3].cb   = (uint32_t)cbData;
    aSegs[3].Phys = NIL_RTHCPHYS;


    /* send it */
    return VBoxNetIntIfSend(pSession, hIf, pBuf, RT_ELEMENTS(aSegs), &aSegs[0], true /* fFlush */);
}


/**
 * Sends an unicast UDP packet.
 *
 * @returns VBox status code.
 * @param   pSession        The support driver session handle.
 * @param   hIf             The interface handle.
 * @param   pBuf            The interface buffer.
 * @param   SrcIPv4Addr     The source IPv4 address.
 * @param   pSrcMacAddr     The source MAC address.
 * @param   uSrcPort        The source port number.
 * @param   DstIPv4Addr     The destination IPv4 address. Can be broadcast.
 * @param   pDstMacAddr     The destination MAC address.
 * @param   uDstPort        The destination port number.
 * @param   pvData          The data payload.
 * @param   cbData          The size of the data payload.
 */
int     VBoxNetUDPUnicast(PSUPDRVSESSION pSession, INTNETIFHANDLE hIf, PINTNETBUF pBuf,
                          RTNETADDRIPV4 SrcIPv4Addr, PCRTMAC pSrcMacAddr, unsigned uSrcPort,
                          RTNETADDRIPV4 DstIPv4Addr, PCRTMAC pDstMacAddr, unsigned uDstPort,
                          void const *pvData, size_t cbData)
{
    return vboxnetudpSend(pSession, hIf, pBuf,
                          SrcIPv4Addr, pSrcMacAddr, uSrcPort,
                          DstIPv4Addr, pDstMacAddr, uDstPort,
                          pvData, cbData);
}


/**
 * Sends a broadcast UDP packet.
 *
 * @returns VBox status code.
 * @param   pSession        The support driver session handle.
 * @param   hIf             The interface handle.
 * @param   pBuf            The interface buffer.
 * @param   SrcIPv4Addr     The source IPv4 address.
 * @param   pSrcMacAddr     The source MAC address.
 * @param   uSrcPort        The source port number.
 * @param   uDstPort        The destination port number.
 * @param   pvData          The data payload.
 * @param   cbData          The size of the data payload.
 */
int     VBoxNetUDPBroadcast(PSUPDRVSESSION pSession, INTNETIFHANDLE hIf, PINTNETBUF pBuf,
                            RTNETADDRIPV4 SrcIPv4Addr, PCRTMAC pSrcMacAddr, unsigned uSrcPort,
                            unsigned uDstPort,
                            void const *pvData, size_t cbData)
{
    RTNETADDRIPV4   IPv4AddrBrdCast;
    IPv4AddrBrdCast.u = UINT32_C(0xffffffff);
    RTMAC           MacBrdCast;
    MacBrdCast.au16[0] = MacBrdCast.au16[1] = MacBrdCast.au16[2] = UINT16_C(0xffff);

    return vboxnetudpSend(pSession, hIf, pBuf,
                          SrcIPv4Addr, pSrcMacAddr, uSrcPort,
                          IPv4AddrBrdCast, &MacBrdCast, uDstPort,
                          pvData, cbData);
}