summaryrefslogtreecommitdiffstats
path: root/src/VBox/Devices/Network/Pcap.cpp
blob: e622e76851276c15d9336c37b37863c13ec25668 (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
/* $Id: Pcap.cpp $ */
/** @file
 * Helpers for writing libpcap files.
 */

/*
 * Copyright (C) 2006-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                                                                                                                 *
*********************************************************************************************************************************/
#include "Pcap.h"

#include <iprt/file.h>
#include <iprt/stream.h>
#include <iprt/time.h>
#include <iprt/errcore.h>
#include <VBox/vmm/pdmnetinline.h>


/*********************************************************************************************************************************
*   Structures and Typedefs                                                                                                      *
*********************************************************************************************************************************/

/* "libpcap" magic */
#define PCAP_MAGIC  0xa1b2c3d4

/* "libpcap" file header (minus magic number). */
struct pcap_hdr
{
    uint16_t    version_major;  /* major version number                         = 2 */
    uint16_t    version_minor;  /* minor version number                         = 4 */
    int32_t     thiszone;       /* GMT to local correction                      = 0 */
    uint32_t    sigfigs;        /* accuracy of timestamps                       = 0 */
    uint32_t    snaplen;        /* max length of captured packets, in octets    = 0xffff */
    uint32_t    network;        /* data link type                               = 01 */
};

/* "libpcap" record header. */
struct pcaprec_hdr
{
    uint32_t    ts_sec;         /* timestamp seconds */
    uint32_t    ts_usec;        /* timestamp microseconds */
    uint32_t    incl_len;       /* number of octets of packet saved in file */
    uint32_t    orig_len;       /* actual length of packet */
};

struct pcaprec_hdr_init
{
    uint32_t            u32Magic;
    struct pcap_hdr     pcap;
};


/*********************************************************************************************************************************
*   Global Variables                                                                                                             *
*********************************************************************************************************************************/
static pcaprec_hdr_init const s_Hdr =
{
    PCAP_MAGIC,
    { 2, 4, 0, 0, 0xffff, 1 },
};

static const char s_szDummyData[] = { 0, 0, 0, 0 };

/**
 * Internal helper.
 */
static void pcapCalcHeader(struct pcaprec_hdr *pHdr, uint64_t StartNanoTS, size_t cbFrame, size_t cbMax)
{
    uint64_t u64TS = RTTimeNanoTS() - StartNanoTS;
    pHdr->ts_sec   = (uint32_t)(u64TS / 1000000000);
    pHdr->ts_usec  = (uint32_t)((u64TS / 1000) % 1000000);
    pHdr->incl_len = (uint32_t)RT_MIN(cbFrame, cbMax);
    pHdr->orig_len = (uint32_t)cbFrame;
}


/**
 * Internal helper.
 */
static void pcapUpdateHeader(struct pcaprec_hdr *pHdr, size_t cbFrame, size_t cbMax)
{
    pHdr->incl_len = (uint32_t)RT_MIN(cbFrame, cbMax);
    pHdr->orig_len = (uint32_t)cbFrame;
}


/**
 * Writes the stream header.
 *
 * @returns IPRT status code, @see RTStrmWrite.
 *
 * @param   pStream         The stream handle.
 * @param   StartNanoTS     What to subtract from the RTTimeNanoTS output.
 */
int PcapStreamHdr(PRTSTREAM pStream, uint64_t StartNanoTS)
{
    int rc1 = RTStrmWrite(pStream, &s_Hdr, sizeof(s_Hdr));
    int rc2 = PcapStreamFrame(pStream, StartNanoTS, s_szDummyData, 60, sizeof(s_szDummyData));
    return RT_SUCCESS(rc1) ? rc2 : rc1;
}


/**
 * Writes a frame to a stream.
 *
 * @returns IPRT status code, @see RTStrmWrite.
 *
 * @param   pStream         The stream handle.
 * @param   StartNanoTS     What to subtract from the RTTimeNanoTS output.
 * @param   pvFrame         The start of the frame.
 * @param   cbFrame         The size of the frame.
 * @param   cbMax           The max number of bytes to include in the file.
 */
int PcapStreamFrame(PRTSTREAM pStream, uint64_t StartNanoTS, const void *pvFrame, size_t cbFrame, size_t cbMax)
{
    struct pcaprec_hdr Hdr;
    pcapCalcHeader(&Hdr, StartNanoTS, cbFrame, cbMax);
    int rc1 = RTStrmWrite(pStream, &Hdr, sizeof(Hdr));
    int rc2 = RTStrmWrite(pStream, pvFrame, Hdr.incl_len);
    return RT_SUCCESS(rc1) ? rc2 : rc1;
}


/**
 * Writes a GSO frame to a stream.
 *
 * @returns IPRT status code, @see RTStrmWrite.
 *
 * @param   pStream         The stream handle.
 * @param   StartNanoTS     What to subtract from the RTTimeNanoTS output.
 * @param   pGso            Pointer to the GSO context.
 * @param   pvFrame         The start of the GSO frame.
 * @param   cbFrame         The size of the GSO frame.
 * @param   cbSegMax        The max number of bytes to include in the file for
 *                          each segment.
 */
int PcapStreamGsoFrame(PRTSTREAM pStream, uint64_t StartNanoTS, PCPDMNETWORKGSO pGso,
                       const void *pvFrame, size_t cbFrame, size_t cbSegMax)
{
    struct pcaprec_hdr Hdr;
    pcapCalcHeader(&Hdr, StartNanoTS, 0, 0);

    uint8_t const  *pbFrame = (uint8_t const *)pvFrame;
    uint8_t         abHdrs[256];
    uint32_t const  cSegs   = PDMNetGsoCalcSegmentCount(pGso, cbFrame);
    for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
    {
        uint32_t cbSegPayload, cbHdrs;
        uint32_t offSegPayload = PDMNetGsoCarveSegment(pGso, pbFrame, cbFrame, iSeg, cSegs, abHdrs, &cbHdrs, &cbSegPayload);

        pcapUpdateHeader(&Hdr, cbHdrs + cbSegPayload, cbSegMax);
        int rc = RTStrmWrite(pStream, &Hdr, sizeof(Hdr));
        if (RT_FAILURE(rc))
            return rc;

        rc = RTStrmWrite(pStream, abHdrs, RT_MIN(Hdr.incl_len, cbHdrs));
        if (RT_SUCCESS(rc) && Hdr.incl_len > cbHdrs)
            rc = RTStrmWrite(pStream, pbFrame + offSegPayload, Hdr.incl_len - cbHdrs);
        if (RT_FAILURE(rc))
            return rc;
    }

    return VINF_SUCCESS;
}


/**
 * Writes the file header.
 *
 * @returns IPRT status code, @see RTFileWrite.
 *
 * @param   File            The file handle.
 * @param   StartNanoTS     What to subtract from the RTTimeNanoTS output.
 */
int PcapFileHdr(RTFILE File, uint64_t StartNanoTS)
{
    int rc1 = RTFileWrite(File, &s_Hdr, sizeof(s_Hdr), NULL);
    int rc2 = PcapFileFrame(File, StartNanoTS, s_szDummyData, 60, sizeof(s_szDummyData));
    return RT_SUCCESS(rc1) ? rc2 : rc1;
}


/**
 * Writes a frame to a file.
 *
 * @returns IPRT status code, @see RTFileWrite.
 *
 * @param   File            The file handle.
 * @param   StartNanoTS     What to subtract from the RTTimeNanoTS output.
 * @param   pvFrame         The start of the frame.
 * @param   cbFrame         The size of the frame.
 * @param   cbMax           The max number of bytes to include in the file.
 */
int PcapFileFrame(RTFILE File, uint64_t StartNanoTS, const void *pvFrame, size_t cbFrame, size_t cbMax)
{
    struct pcaprec_hdr  Hdr;
    pcapCalcHeader(&Hdr, StartNanoTS, cbFrame, cbMax);
    int rc1 = RTFileWrite(File, &Hdr, sizeof(Hdr), NULL);
    int rc2 = RTFileWrite(File, pvFrame, Hdr.incl_len, NULL);
    return RT_SUCCESS(rc1) ? rc2 : rc1;
}


/**
 * Writes a GSO frame to a file.
 *
 * @returns IPRT status code, @see RTFileWrite.
 *
 * @param   File            The file handle.
 * @param   StartNanoTS     What to subtract from the RTTimeNanoTS output.
 * @param   pGso            Pointer to the GSO context.
 * @param   pvFrame         The start of the GSO frame.
 * @param   cbFrame         The size of the GSO frame.
 * @param   cbSegMax        The max number of bytes to include in the file for
 *                          each segment.
 */
int PcapFileGsoFrame(RTFILE File, uint64_t StartNanoTS, PCPDMNETWORKGSO pGso,
                     const void *pvFrame, size_t cbFrame, size_t cbSegMax)
{
    struct pcaprec_hdr Hdr;
    pcapCalcHeader(&Hdr, StartNanoTS, 0, 0);

    uint8_t const  *pbFrame = (uint8_t const *)pvFrame;
    uint8_t         abHdrs[256];
    uint32_t const  cSegs   = PDMNetGsoCalcSegmentCount(pGso, cbFrame);
    for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
    {
        uint32_t cbSegPayload, cbHdrs;
        uint32_t offSegPayload = PDMNetGsoCarveSegment(pGso, pbFrame, cbFrame, iSeg, cSegs, abHdrs, &cbHdrs, &cbSegPayload);

        pcapUpdateHeader(&Hdr, cbHdrs + cbSegPayload, cbSegMax);
        int rc = RTFileWrite(File, &Hdr, sizeof(Hdr), NULL);
        if (RT_FAILURE(rc))
            return rc;

        rc = RTFileWrite(File, abHdrs, RT_MIN(Hdr.incl_len, cbHdrs), NULL);
        if (RT_SUCCESS(rc) && Hdr.incl_len > cbHdrs)
            rc = RTFileWrite(File, pbFrame + offSegPayload, Hdr.incl_len - cbHdrs, NULL);
        if (RT_FAILURE(rc))
            return rc;
    }

    return VINF_SUCCESS;
}