1 | /** @file
|
---|
2 | * PDM - Networking Helpers, Inlined Code. (DEV,++)
|
---|
3 | *
|
---|
4 | * This is all inlined because it's too tedious to create 2-3 libraries to
|
---|
5 | * contain it all (same bad excuse as for intnetinline.h).
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2010 Oracle Corporation
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
12 | * available from http://www.virtualbox.org. This file is free software;
|
---|
13 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
14 | * General Public License (GPL) as published by the Free Software
|
---|
15 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
16 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
17 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
18 | *
|
---|
19 | * The contents of this file may alternatively be used under the terms
|
---|
20 | * of the Common Development and Distribution License Version 1.0
|
---|
21 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
22 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
23 | * CDDL are applicable instead of those of the GPL.
|
---|
24 | *
|
---|
25 | * You may elect to license modified versions of this file under the
|
---|
26 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
27 | */
|
---|
28 |
|
---|
29 |
|
---|
30 | /*******************************************************************************
|
---|
31 | * Header Files *
|
---|
32 | *******************************************************************************/
|
---|
33 | #include <VBox/log.h>
|
---|
34 | #include <VBox/types.h>
|
---|
35 | #include <iprt/asm.h>
|
---|
36 | #include <iprt/assert.h>
|
---|
37 | #include <iprt/net.h>
|
---|
38 | #include <iprt/string.h>
|
---|
39 |
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Checksum type.
|
---|
43 | */
|
---|
44 | typedef enum PDMNETCSUMTYPE
|
---|
45 | {
|
---|
46 | /** No checksum. */
|
---|
47 | PDMNETCSUMTYPE_NONE = 0,
|
---|
48 | /** Normal TCP checksum. */
|
---|
49 | PDMNETCSUMTYPE_COMPLETE,
|
---|
50 | /** Checksum on pseudo header (used with GSO). */
|
---|
51 | PDMNETCSUMTYPE_PSEUDO,
|
---|
52 | /** The usual 32-bit hack. */
|
---|
53 | PDMNETCSUMTYPE_32_BIT_HACK = 0x7fffffff
|
---|
54 | } PDMNETCSUMTYPE;
|
---|
55 |
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Validates the GSO context.
|
---|
59 | *
|
---|
60 | * @returns true if valid, false if not (not asserted or logged).
|
---|
61 | * @param pGso The GSO context.
|
---|
62 | * @param cbGsoMax The max size of the GSO context.
|
---|
63 | * @param cbFrame The max size of the GSO frame (use to validate
|
---|
64 | * the MSS).
|
---|
65 | */
|
---|
66 | DECLINLINE(bool) PDMNetGsoIsValid(PCPDMNETWORKGSO pGso, size_t cbGsoMax, size_t cbFrame)
|
---|
67 | {
|
---|
68 | PDMNETWORKGSOTYPE enmType;
|
---|
69 |
|
---|
70 | if (RT_UNLIKELY(cbGsoMax < sizeof(*pGso)))
|
---|
71 | return false;
|
---|
72 |
|
---|
73 | enmType = (PDMNETWORKGSOTYPE)pGso->u8Type;
|
---|
74 | if (RT_UNLIKELY( enmType <= PDMNETWORKGSOTYPE_INVALID || enmType >= PDMNETWORKGSOTYPE_END ))
|
---|
75 | return false;
|
---|
76 |
|
---|
77 | /* all types requires both headers. */
|
---|
78 | if (RT_UNLIKELY( pGso->offHdr1 < sizeof(RTNETETHERHDR) ))
|
---|
79 | return false;
|
---|
80 | if (RT_UNLIKELY( pGso->offHdr2 <= pGso->offHdr1 ))
|
---|
81 | return false;
|
---|
82 | if (RT_UNLIKELY( pGso->cbHdrsTotal <= pGso->offHdr2 ))
|
---|
83 | return false;
|
---|
84 |
|
---|
85 | /* min size of the 1st header(s). */
|
---|
86 | switch (enmType)
|
---|
87 | {
|
---|
88 | case PDMNETWORKGSOTYPE_IPV4_TCP:
|
---|
89 | case PDMNETWORKGSOTYPE_IPV4_UDP:
|
---|
90 | if (RT_UNLIKELY( (unsigned)pGso->offHdr2 - pGso->offHdr1 < RTNETIPV4_MIN_LEN ))
|
---|
91 | return false;
|
---|
92 | break;
|
---|
93 | case PDMNETWORKGSOTYPE_IPV6_TCP:
|
---|
94 | case PDMNETWORKGSOTYPE_IPV6_UDP:
|
---|
95 | if (RT_UNLIKELY( (unsigned)pGso->offHdr2 - pGso->offHdr1 < RTNETIPV6_MIN_LEN ))
|
---|
96 | return false;
|
---|
97 | break;
|
---|
98 | case PDMNETWORKGSOTYPE_IPV4_IPV6_TCP:
|
---|
99 | case PDMNETWORKGSOTYPE_IPV4_IPV6_UDP:
|
---|
100 | if (RT_UNLIKELY( (unsigned)pGso->offHdr2 - pGso->offHdr1 < RTNETIPV4_MIN_LEN + RTNETIPV6_MIN_LEN ))
|
---|
101 | return false;
|
---|
102 | break;
|
---|
103 | case PDMNETWORKGSOTYPE_INVALID:
|
---|
104 | case PDMNETWORKGSOTYPE_END:
|
---|
105 | break;
|
---|
106 | /* no default case! want gcc warnings. */
|
---|
107 | }
|
---|
108 |
|
---|
109 | /* min size of the 2nd header. */
|
---|
110 | switch (enmType)
|
---|
111 | {
|
---|
112 | case PDMNETWORKGSOTYPE_IPV4_TCP:
|
---|
113 | case PDMNETWORKGSOTYPE_IPV6_TCP:
|
---|
114 | case PDMNETWORKGSOTYPE_IPV4_IPV6_TCP:
|
---|
115 | if (RT_UNLIKELY( (unsigned)pGso->cbHdrsTotal - pGso->offHdr2 < RTNETTCP_MIN_LEN ))
|
---|
116 | return false;
|
---|
117 | break;
|
---|
118 | case PDMNETWORKGSOTYPE_IPV4_UDP:
|
---|
119 | case PDMNETWORKGSOTYPE_IPV6_UDP:
|
---|
120 | case PDMNETWORKGSOTYPE_IPV4_IPV6_UDP:
|
---|
121 | if (RT_UNLIKELY( (unsigned)pGso->cbHdrsTotal - pGso->offHdr2 < RTNETUDP_MIN_LEN ))
|
---|
122 | return false;
|
---|
123 | break;
|
---|
124 | case PDMNETWORKGSOTYPE_INVALID:
|
---|
125 | case PDMNETWORKGSOTYPE_END:
|
---|
126 | break;
|
---|
127 | /* no default case! want gcc warnings. */
|
---|
128 | }
|
---|
129 |
|
---|
130 | /* There must be at more than one segment. */
|
---|
131 | if (RT_UNLIKELY( cbFrame <= pGso->cbHdrsTotal ))
|
---|
132 | return false;
|
---|
133 | if (RT_UNLIKELY( cbFrame - pGso->cbHdrsTotal < pGso->cbMaxSeg ))
|
---|
134 | return false;
|
---|
135 |
|
---|
136 | return true;
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Returns the length of header for a particular segment/fragment.
|
---|
142 | *
|
---|
143 | * We cannot simply treat UDP header as a part of payload because we do not
|
---|
144 | * want to modify the payload but still need to modify the checksum field in
|
---|
145 | * UDP header. So we want to include UDP header when calculating the length
|
---|
146 | * of headers in the first segment getting it copied to a temporary buffer
|
---|
147 | * along with other headers.
|
---|
148 | *
|
---|
149 | * @returns Length of headers (including UDP header for the first fragment).
|
---|
150 | * @param pGso The GSO context.
|
---|
151 | * @param iSeg The segment index.
|
---|
152 | */
|
---|
153 | DECLINLINE(uint8_t) pdmNetSegHdrLen(PCPDMNETWORKGSO pGso, uint32_t iSeg)
|
---|
154 | {
|
---|
155 | return iSeg ? pGso->cbHdrsSeg : pGso->cbHdrsTotal;
|
---|
156 | }
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * Returns the length of payload for a particular segment/fragment.
|
---|
160 | *
|
---|
161 | * The first segment does not contain UDP header. The size of UDP header is
|
---|
162 | * determined as the difference between the total headers size and the size
|
---|
163 | * used during segmentation.
|
---|
164 | *
|
---|
165 | * @returns Length of payload (including UDP header for the first fragment).
|
---|
166 | * @param pGso The GSO context.
|
---|
167 | * @param iSeg The segment that we're carving out (0-based).
|
---|
168 | * @param cSegs The number of segments in the GSO frame.
|
---|
169 | * @param cbFrame The size of the GSO frame.
|
---|
170 | */
|
---|
171 | DECLINLINE(uint32_t) pdmNetSegPayloadLen(PCPDMNETWORKGSO pGso, uint32_t iSeg, uint32_t cSegs, uint32_t cbFrame)
|
---|
172 | {
|
---|
173 | if (iSeg + 1 == cSegs)
|
---|
174 | return cbFrame - iSeg * pGso->cbMaxSeg - pdmNetSegHdrLen(pGso, iSeg);
|
---|
175 | else
|
---|
176 | return pGso->cbMaxSeg - (iSeg ? 0 : pGso->cbHdrsTotal - pGso->cbHdrsSeg);
|
---|
177 | }
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * Calculates the number of segments a GSO frame will be segmented into.
|
---|
181 | *
|
---|
182 | * @returns Segment count.
|
---|
183 | * @param pGso The GSO context.
|
---|
184 | * @param cbFrame The GSO frame size (header proto + payload).
|
---|
185 | */
|
---|
186 | DECLINLINE(uint32_t) PDMNetGsoCalcSegmentCount(PCPDMNETWORKGSO pGso, size_t cbFrame)
|
---|
187 | {
|
---|
188 | size_t cbPayload;
|
---|
189 | Assert(PDMNetGsoIsValid(pGso, sizeof(*pGso), cbFrame));
|
---|
190 | cbPayload = cbFrame - pGso->cbHdrsSeg;
|
---|
191 | return (uint32_t)((cbPayload + pGso->cbMaxSeg - 1) / pGso->cbMaxSeg);
|
---|
192 | }
|
---|
193 |
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * Used to find the IPv6 header when handling 4to6 tunneling.
|
---|
197 | *
|
---|
198 | * @returns Offset of the IPv6 header.
|
---|
199 | * @param pbSegHdrs The headers / frame start.
|
---|
200 | * @param offIpHdr The offset of the IPv4 header.
|
---|
201 | */
|
---|
202 | DECLINLINE(uint8_t) pgmNetGsoCalcIpv6Offset(uint8_t *pbSegHdrs, uint8_t offIPv4Hdr)
|
---|
203 | {
|
---|
204 | PCRTNETIPV4 pIPv4Hdr = (PCRTNETIPV4)&pbSegHdrs[offIPv4Hdr];
|
---|
205 | return offIPv4Hdr + pIPv4Hdr->ip_hl * 4;
|
---|
206 | }
|
---|
207 |
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * Update an UDP header after carving out a segment
|
---|
211 | *
|
---|
212 | * @param u32PseudoSum The pseudo checksum.
|
---|
213 | * @param pbSegHdrs Pointer to the header bytes / frame start.
|
---|
214 | * @param offUdpHdr The offset into @a pbSegHdrs of the UDP header.
|
---|
215 | * @param pbPayload Pointer to the payload bytes.
|
---|
216 | * @param cbPayload The amount of payload.
|
---|
217 | * @param cbHdrs The size of all the headers.
|
---|
218 | * @param enmCsumType Whether to checksum the payload, the pseudo
|
---|
219 | * header or nothing.
|
---|
220 | * @internal
|
---|
221 | */
|
---|
222 | DECLINLINE(void) pdmNetGsoUpdateUdpHdr(uint32_t u32PseudoSum, uint8_t *pbSegHdrs, uint8_t offUdpHdr,
|
---|
223 | uint8_t const *pbPayload, uint32_t cbPayload, uint8_t cbHdrs,
|
---|
224 | PDMNETCSUMTYPE enmCsumType)
|
---|
225 | {
|
---|
226 | PRTNETUDP pUdpHdr = (PRTNETUDP)&pbSegHdrs[offUdpHdr];
|
---|
227 | pUdpHdr->uh_ulen = RT_H2N_U16(cbPayload + cbHdrs - offUdpHdr);
|
---|
228 | switch (enmCsumType)
|
---|
229 | {
|
---|
230 | case PDMNETCSUMTYPE_NONE:
|
---|
231 | pUdpHdr->uh_sum = 0;
|
---|
232 | break;
|
---|
233 | case PDMNETCSUMTYPE_COMPLETE:
|
---|
234 | pUdpHdr->uh_sum = RTNetUDPChecksum(u32PseudoSum, pUdpHdr);
|
---|
235 | break;
|
---|
236 | case PDMNETCSUMTYPE_PSEUDO:
|
---|
237 | pUdpHdr->uh_sum = ~RTNetIPv4FinalizeChecksum(u32PseudoSum);
|
---|
238 | break;
|
---|
239 | default:
|
---|
240 | NOREF(pbPayload);
|
---|
241 | AssertFailed();
|
---|
242 | break;
|
---|
243 | }
|
---|
244 | }
|
---|
245 |
|
---|
246 |
|
---|
247 | /**
|
---|
248 | * Update an UDP header after carving out an IP fragment
|
---|
249 | *
|
---|
250 | * @param u32PseudoSum The pseudo checksum.
|
---|
251 | * @param pbSegHdrs Pointer to the header bytes copy
|
---|
252 | * @param pbFrame Pointer to the frame start.
|
---|
253 | * @param offUdpHdr The offset into @a pbSegHdrs of the UDP header.
|
---|
254 | *
|
---|
255 | * @internal
|
---|
256 | */
|
---|
257 | DECLINLINE(void) pdmNetGsoUpdateUdpHdrUfo(uint32_t u32PseudoSum, uint8_t *pbSegHdrs, const uint8_t *pbFrame, uint8_t offUdpHdr)
|
---|
258 | {
|
---|
259 | PCRTNETUDP pcUdpHdrOrig = (PCRTNETUDP)&pbFrame[offUdpHdr];
|
---|
260 | PRTNETUDP pUdpHdr = (PRTNETUDP)&pbSegHdrs[offUdpHdr];
|
---|
261 | pUdpHdr->uh_sum = RTNetUDPChecksum(u32PseudoSum, pcUdpHdrOrig);
|
---|
262 | }
|
---|
263 |
|
---|
264 |
|
---|
265 | /**
|
---|
266 | * Update a TCP header after carving out a segment.
|
---|
267 | *
|
---|
268 | * @param u32PseudoSum The pseudo checksum.
|
---|
269 | * @param pbSegHdrs Pointer to the header bytes / frame start.
|
---|
270 | * @param offTcpHdr The offset into @a pbSegHdrs of the TCP header.
|
---|
271 | * @param pbPayload Pointer to the payload bytes.
|
---|
272 | * @param cbPayload The amount of payload.
|
---|
273 | * @param offPayload The offset into the payload that we're splitting
|
---|
274 | * up. We're ASSUMING that the payload follows
|
---|
275 | * immediately after the TCP header w/ options.
|
---|
276 | * @param cbHdrs The size of all the headers.
|
---|
277 | * @param fLastSeg Set if this is the last segment.
|
---|
278 | * @param enmCsumType Whether to checksum the payload, the pseudo
|
---|
279 | * header or nothing.
|
---|
280 | * @internal
|
---|
281 | */
|
---|
282 | DECLINLINE(void) pdmNetGsoUpdateTcpHdr(uint32_t u32PseudoSum, uint8_t *pbSegHdrs, uint8_t offTcpHdr,
|
---|
283 | uint8_t const *pbPayload, uint32_t cbPayload, uint32_t offPayload, uint8_t cbHdrs,
|
---|
284 | bool fLastSeg, PDMNETCSUMTYPE enmCsumType)
|
---|
285 | {
|
---|
286 | PRTNETTCP pTcpHdr = (PRTNETTCP)&pbSegHdrs[offTcpHdr];
|
---|
287 | pTcpHdr->th_seq = RT_H2N_U32(RT_N2H_U32(pTcpHdr->th_seq) + offPayload);
|
---|
288 | if (!fLastSeg)
|
---|
289 | pTcpHdr->th_flags &= ~(RTNETTCP_F_FIN | RTNETTCP_F_PSH);
|
---|
290 | switch (enmCsumType)
|
---|
291 | {
|
---|
292 | case PDMNETCSUMTYPE_NONE:
|
---|
293 | pTcpHdr->th_sum = 0;
|
---|
294 | break;
|
---|
295 | case PDMNETCSUMTYPE_COMPLETE:
|
---|
296 | pTcpHdr->th_sum = RTNetTCPChecksum(u32PseudoSum, pTcpHdr, pbPayload, cbPayload);
|
---|
297 | break;
|
---|
298 | case PDMNETCSUMTYPE_PSEUDO:
|
---|
299 | pTcpHdr->th_sum = ~RTNetIPv4FinalizeChecksum(u32PseudoSum);
|
---|
300 | break;
|
---|
301 | default:
|
---|
302 | NOREF(cbHdrs);
|
---|
303 | AssertFailed();
|
---|
304 | break;
|
---|
305 | }
|
---|
306 | }
|
---|
307 |
|
---|
308 |
|
---|
309 | /**
|
---|
310 | * Updates a IPv6 header after carving out a segment.
|
---|
311 | *
|
---|
312 | * @returns 32-bit intermediary checksum value for the pseudo header.
|
---|
313 | * @param pbSegHdrs Pointer to the header bytes.
|
---|
314 | * @param offIpHdr The offset into @a pbSegHdrs of the IP header.
|
---|
315 | * @param cbSegPayload The amount of segmented payload. Not to be
|
---|
316 | * confused with the IP payload.
|
---|
317 | * @param cbHdrs The size of all the headers.
|
---|
318 | * @param offPktHdr Offset of the protocol packet header. For the
|
---|
319 | * pseudo header checksum calulation.
|
---|
320 | * @param bProtocol The protocol type. For the pseudo header.
|
---|
321 | * @internal
|
---|
322 | */
|
---|
323 | DECLINLINE(uint32_t) pdmNetGsoUpdateIPv6Hdr(uint8_t *pbSegHdrs, uint8_t offIpHdr, uint32_t cbSegPayload, uint8_t cbHdrs,
|
---|
324 | uint8_t offPktHdr, uint8_t bProtocol)
|
---|
325 | {
|
---|
326 | PRTNETIPV6 pIpHdr = (PRTNETIPV6)&pbSegHdrs[offIpHdr];
|
---|
327 | uint16_t cbPayload = (uint16_t)(cbHdrs - (offIpHdr + sizeof(RTNETIPV6)) + cbSegPayload);
|
---|
328 | pIpHdr->ip6_plen = RT_H2N_U16(cbPayload);
|
---|
329 | return RTNetIPv6PseudoChecksumEx(pIpHdr, bProtocol, (uint16_t)(cbHdrs - offPktHdr + cbSegPayload));
|
---|
330 | }
|
---|
331 |
|
---|
332 |
|
---|
333 | /**
|
---|
334 | * Updates a IPv4 header after carving out a segment.
|
---|
335 | *
|
---|
336 | * @returns 32-bit intermediary checksum value for the pseudo header.
|
---|
337 | * @param pbSegHdrs Pointer to the header bytes.
|
---|
338 | * @param offIpHdr The offset into @a pbSegHdrs of the IP header.
|
---|
339 | * @param cbSegPayload The amount of segmented payload.
|
---|
340 | * @param iSeg The segment index.
|
---|
341 | * @param cbHdrs The size of all the headers.
|
---|
342 | * @internal
|
---|
343 | */
|
---|
344 | DECLINLINE(uint32_t) pdmNetGsoUpdateIPv4Hdr(uint8_t *pbSegHdrs, uint8_t offIpHdr, uint32_t cbSegPayload,
|
---|
345 | uint32_t iSeg, uint8_t cbHdrs)
|
---|
346 | {
|
---|
347 | PRTNETIPV4 pIpHdr = (PRTNETIPV4)&pbSegHdrs[offIpHdr];
|
---|
348 | pIpHdr->ip_len = RT_H2N_U16(cbHdrs - offIpHdr + cbSegPayload);
|
---|
349 | pIpHdr->ip_id = RT_H2N_U16(RT_N2H_U16(pIpHdr->ip_id) + iSeg);
|
---|
350 | pIpHdr->ip_sum = RTNetIPv4HdrChecksum(pIpHdr);
|
---|
351 | return RTNetIPv4PseudoChecksum(pIpHdr);
|
---|
352 | }
|
---|
353 |
|
---|
354 |
|
---|
355 | /**
|
---|
356 | * Updates a IPv4 header after carving out an IP fragment.
|
---|
357 | *
|
---|
358 | * @param pbSegHdrs Pointer to the header bytes.
|
---|
359 | * @param offIpHdr The offset into @a pbSegHdrs of the IP header.
|
---|
360 | * @param cbSegPayload The amount of segmented payload.
|
---|
361 | * @param offFragment The offset of this fragment for reassembly.
|
---|
362 | * @param iSeg The segment index.
|
---|
363 | * @param cbHdrs The size of all the headers.
|
---|
364 | * @param fLastFragment True if this is the last fragment of datagram.
|
---|
365 | * @internal
|
---|
366 | */
|
---|
367 | DECLINLINE(void) pdmNetGsoUpdateIPv4HdrUfo(uint8_t *pbSegHdrs, uint8_t offIpHdr, uint32_t cbSegPayload,
|
---|
368 | uint32_t offFragment, uint8_t cbHdrs, bool fLastFragment)
|
---|
369 | {
|
---|
370 | PRTNETIPV4 pIpHdr = (PRTNETIPV4)&pbSegHdrs[offIpHdr];
|
---|
371 | pIpHdr->ip_len = RT_H2N_U16(cbHdrs - offIpHdr + cbSegPayload);
|
---|
372 | pIpHdr->ip_off = RT_H2N_U16((offFragment / 8) | (fLastFragment ? 0 : RTNETIPV4_FLAGS_MF));
|
---|
373 | pIpHdr->ip_sum = RTNetIPv4HdrChecksum(pIpHdr);
|
---|
374 | }
|
---|
375 |
|
---|
376 |
|
---|
377 | /**
|
---|
378 | * Carves out the specified segment in a destructive manner.
|
---|
379 | *
|
---|
380 | * This is for sequentially carving out segments and pushing them along for
|
---|
381 | * processing or sending. To avoid allocating a temporary buffer for
|
---|
382 | * constructing the segment in, we trash the previous frame by putting the
|
---|
383 | * header at the end of it.
|
---|
384 | *
|
---|
385 | * @returns Pointer to the segment frame that we've carved out.
|
---|
386 | * @param pGso The GSO context data.
|
---|
387 | * @param pbFrame Pointer to the GSO frame.
|
---|
388 | * @param cbFrame The size of the GSO frame.
|
---|
389 | * @param pbHdrScatch Pointer to a pGso->cbHdrs sized area where we
|
---|
390 | * can save the original header prototypes on the
|
---|
391 | * first call (@a iSeg is 0) and retrieve it on
|
---|
392 | * susequent calls. (Just use a 256 bytes
|
---|
393 | * buffer to make life easy.)
|
---|
394 | * @param iSeg The segment that we're carving out (0-based).
|
---|
395 | * @param cSegs The number of segments in the GSO frame. Use
|
---|
396 | * PDMNetGsoCalcSegmentCount to find this.
|
---|
397 | * @param pcbSegFrame Where to return the size of the returned segment
|
---|
398 | * frame.
|
---|
399 | */
|
---|
400 | DECLINLINE(void *) PDMNetGsoCarveSegmentQD(PCPDMNETWORKGSO pGso, uint8_t *pbFrame, size_t cbFrame, uint8_t *pbHdrScatch,
|
---|
401 | uint32_t iSeg, uint32_t cSegs, uint32_t *pcbSegFrame)
|
---|
402 | {
|
---|
403 | /*
|
---|
404 | * Figure out where the payload is and where the header starts before we
|
---|
405 | * do the protocol specific carving.
|
---|
406 | */
|
---|
407 | uint8_t * const pbSegHdrs = pbFrame + pGso->cbMaxSeg * iSeg;
|
---|
408 | uint8_t * const pbSegPayload = pbSegHdrs + pGso->cbHdrsSeg;
|
---|
409 | uint32_t const cbSegPayload = pdmNetSegPayloadLen(pGso, iSeg, cSegs, (uint32_t)cbFrame);
|
---|
410 | uint32_t const cbSegFrame = cbSegPayload + pGso->cbHdrsSeg;
|
---|
411 |
|
---|
412 | /*
|
---|
413 | * Check assumptions (doing it after declaring the variables because of C).
|
---|
414 | */
|
---|
415 | Assert(iSeg < cSegs);
|
---|
416 | Assert(cSegs == PDMNetGsoCalcSegmentCount(pGso, cbFrame));
|
---|
417 | Assert(PDMNetGsoIsValid(pGso, sizeof(*pGso), cbFrame));
|
---|
418 |
|
---|
419 | /*
|
---|
420 | * Copy the header and do the protocol specific massaging of it.
|
---|
421 | */
|
---|
422 | if (iSeg != 0)
|
---|
423 | memcpy(pbSegHdrs, pbHdrScatch, pGso->cbHdrsSeg);
|
---|
424 | else
|
---|
425 | memcpy(pbHdrScatch, pbSegHdrs, pGso->cbHdrsSeg); /* There is no need to save UDP header */
|
---|
426 |
|
---|
427 | switch ((PDMNETWORKGSOTYPE)pGso->u8Type)
|
---|
428 | {
|
---|
429 | case PDMNETWORKGSOTYPE_IPV4_TCP:
|
---|
430 | pdmNetGsoUpdateTcpHdr(pdmNetGsoUpdateIPv4Hdr(pbSegHdrs, pGso->offHdr1, cbSegPayload, iSeg, pGso->cbHdrsSeg),
|
---|
431 | pbSegHdrs, pGso->offHdr2, pbSegPayload, cbSegPayload, iSeg * pGso->cbMaxSeg,
|
---|
432 | pGso->cbHdrsSeg, iSeg + 1 == cSegs, PDMNETCSUMTYPE_COMPLETE);
|
---|
433 | break;
|
---|
434 | case PDMNETWORKGSOTYPE_IPV4_UDP:
|
---|
435 | if (iSeg == 0)
|
---|
436 | pdmNetGsoUpdateUdpHdrUfo(RTNetIPv4PseudoChecksum((PRTNETIPV4)&pbFrame[pGso->offHdr1]),
|
---|
437 | pbSegHdrs, pbFrame, pGso->offHdr2);
|
---|
438 | pdmNetGsoUpdateIPv4HdrUfo(pbSegHdrs, pGso->offHdr1, cbSegPayload, iSeg * pGso->cbMaxSeg,
|
---|
439 | pdmNetSegHdrLen(pGso, iSeg), iSeg + 1 == cSegs);
|
---|
440 | break;
|
---|
441 | case PDMNETWORKGSOTYPE_IPV6_TCP:
|
---|
442 | pdmNetGsoUpdateTcpHdr(pdmNetGsoUpdateIPv6Hdr(pbSegHdrs, pGso->offHdr1, cbSegPayload, pGso->cbHdrsSeg,
|
---|
443 | pGso->offHdr2, RTNETIPV4_PROT_TCP),
|
---|
444 | pbSegHdrs, pGso->offHdr2, pbSegPayload, cbSegPayload, iSeg * pGso->cbMaxSeg,
|
---|
445 | pGso->cbHdrsSeg, iSeg + 1 == cSegs, PDMNETCSUMTYPE_COMPLETE);
|
---|
446 | break;
|
---|
447 | case PDMNETWORKGSOTYPE_IPV6_UDP:
|
---|
448 | pdmNetGsoUpdateUdpHdr(pdmNetGsoUpdateIPv6Hdr(pbSegHdrs, pGso->offHdr1, cbSegPayload, pGso->cbHdrsSeg,
|
---|
449 | pGso->offHdr2, RTNETIPV4_PROT_UDP),
|
---|
450 | pbSegHdrs, pGso->offHdr2, pbSegPayload, cbSegPayload, pGso->cbHdrsSeg, PDMNETCSUMTYPE_COMPLETE);
|
---|
451 | break;
|
---|
452 | case PDMNETWORKGSOTYPE_IPV4_IPV6_TCP:
|
---|
453 | pdmNetGsoUpdateIPv4Hdr(pbSegHdrs, pGso->offHdr1, cbSegPayload, iSeg, pGso->cbHdrsSeg);
|
---|
454 | pdmNetGsoUpdateTcpHdr(pdmNetGsoUpdateIPv6Hdr(pbSegHdrs, pgmNetGsoCalcIpv6Offset(pbSegHdrs, pGso->offHdr1),
|
---|
455 | cbSegPayload, pGso->cbHdrsSeg, pGso->offHdr2, RTNETIPV4_PROT_TCP),
|
---|
456 | pbSegHdrs, pGso->offHdr2, pbSegPayload, cbSegPayload, iSeg * pGso->cbMaxSeg,
|
---|
457 | pGso->cbHdrsSeg, iSeg + 1 == cSegs, PDMNETCSUMTYPE_COMPLETE);
|
---|
458 | break;
|
---|
459 | case PDMNETWORKGSOTYPE_IPV4_IPV6_UDP:
|
---|
460 | pdmNetGsoUpdateIPv4Hdr(pbSegHdrs, pGso->offHdr1, cbSegPayload, iSeg, pGso->cbHdrsSeg);
|
---|
461 | pdmNetGsoUpdateUdpHdr(pdmNetGsoUpdateIPv6Hdr(pbSegHdrs, pgmNetGsoCalcIpv6Offset(pbSegHdrs, pGso->offHdr1),
|
---|
462 | cbSegPayload, pGso->cbHdrsSeg, pGso->offHdr2, RTNETIPV4_PROT_UDP),
|
---|
463 | pbSegHdrs, pGso->offHdr2, pbSegPayload, cbSegPayload, pGso->cbHdrsSeg, PDMNETCSUMTYPE_COMPLETE);
|
---|
464 | break;
|
---|
465 | case PDMNETWORKGSOTYPE_INVALID:
|
---|
466 | case PDMNETWORKGSOTYPE_END:
|
---|
467 | /* no default! wnat gcc warnings. */
|
---|
468 | break;
|
---|
469 | }
|
---|
470 |
|
---|
471 | *pcbSegFrame = cbSegFrame;
|
---|
472 | return pbSegHdrs;
|
---|
473 | }
|
---|
474 |
|
---|
475 |
|
---|
476 | /**
|
---|
477 | * Carves out the specified segment in a non-destructive manner.
|
---|
478 | *
|
---|
479 | * The segment headers and segment payload is kept separate here. The GSO frame
|
---|
480 | * is still expected to be one linear chunk of data, but we don't modify any of
|
---|
481 | * it.
|
---|
482 | *
|
---|
483 | * @returns The offset into the GSO frame of the payload.
|
---|
484 | * @param pGso The GSO context data.
|
---|
485 | * @param pbFrame Pointer to the GSO frame. Used for retrieving
|
---|
486 | * the header prototype and for checksumming the
|
---|
487 | * payload. The buffer is not modified.
|
---|
488 | * @param cbFrame The size of the GSO frame.
|
---|
489 | * @param iSeg The segment that we're carving out (0-based).
|
---|
490 | * @param cSegs The number of segments in the GSO frame. Use
|
---|
491 | * PDMNetGsoCalcSegmentCount to find this.
|
---|
492 | * @param pbSegHdrs Where to return the headers for the segment
|
---|
493 | * that's been carved out. The buffer must be at
|
---|
494 | * least pGso->cbHdrs in size, using a 256 byte
|
---|
495 | * buffer is a recommended simplification.
|
---|
496 | * @param pcbSegHdrs Where to return the size of the returned
|
---|
497 | * segment headers.
|
---|
498 | * @param pcbSegPayload Where to return the size of the returned
|
---|
499 | * segment payload.
|
---|
500 | */
|
---|
501 | DECLINLINE(uint32_t) PDMNetGsoCarveSegment(PCPDMNETWORKGSO pGso, const uint8_t *pbFrame, size_t cbFrame,
|
---|
502 | uint32_t iSeg, uint32_t cSegs, uint8_t *pbSegHdrs,
|
---|
503 | uint32_t *pcbSegHdrs, uint32_t *pcbSegPayload)
|
---|
504 | {
|
---|
505 | /*
|
---|
506 | * Figure out where the payload is and where the header starts before we
|
---|
507 | * do the protocol specific carving.
|
---|
508 | */
|
---|
509 | uint32_t const cbSegHdrs = pdmNetSegHdrLen(pGso, iSeg);
|
---|
510 | uint8_t const * const pbSegPayload = pbFrame + cbSegHdrs + iSeg * pGso->cbMaxSeg;
|
---|
511 | uint32_t const cbSegPayload = pdmNetSegPayloadLen(pGso, iSeg, cSegs, (uint32_t)cbFrame);
|
---|
512 |
|
---|
513 | /*
|
---|
514 | * Check assumptions (doing it after declaring the variables because of C).
|
---|
515 | */
|
---|
516 | Assert(iSeg < cSegs);
|
---|
517 | Assert(cSegs == PDMNetGsoCalcSegmentCount(pGso, cbFrame));
|
---|
518 | Assert(PDMNetGsoIsValid(pGso, sizeof(*pGso), cbFrame));
|
---|
519 |
|
---|
520 | /*
|
---|
521 | * Copy the header and do the protocol specific massaging of it.
|
---|
522 | */
|
---|
523 | memcpy(pbSegHdrs, pbFrame, pGso->cbHdrsTotal); /* include UDP header */
|
---|
524 |
|
---|
525 | switch ((PDMNETWORKGSOTYPE)pGso->u8Type)
|
---|
526 | {
|
---|
527 | case PDMNETWORKGSOTYPE_IPV4_TCP:
|
---|
528 | pdmNetGsoUpdateTcpHdr(pdmNetGsoUpdateIPv4Hdr(pbSegHdrs, pGso->offHdr1, cbSegPayload, iSeg, cbSegHdrs),
|
---|
529 | pbSegHdrs, pGso->offHdr2, pbSegPayload, cbSegPayload, iSeg * pGso->cbMaxSeg,
|
---|
530 | cbSegHdrs, iSeg + 1 == cSegs, PDMNETCSUMTYPE_COMPLETE);
|
---|
531 | break;
|
---|
532 | case PDMNETWORKGSOTYPE_IPV4_UDP:
|
---|
533 | if (iSeg == 0)
|
---|
534 | pdmNetGsoUpdateUdpHdrUfo(RTNetIPv4PseudoChecksum((PRTNETIPV4)&pbFrame[pGso->offHdr1]),
|
---|
535 | pbSegHdrs, pbFrame, pGso->offHdr2);
|
---|
536 | pdmNetGsoUpdateIPv4HdrUfo(pbSegHdrs, pGso->offHdr1, cbSegPayload, iSeg * pGso->cbMaxSeg,
|
---|
537 | cbSegHdrs, iSeg + 1 == cSegs);
|
---|
538 | break;
|
---|
539 | case PDMNETWORKGSOTYPE_IPV6_TCP:
|
---|
540 | pdmNetGsoUpdateTcpHdr(pdmNetGsoUpdateIPv6Hdr(pbSegHdrs, pGso->offHdr1, cbSegPayload, cbSegHdrs,
|
---|
541 | pGso->offHdr2, RTNETIPV4_PROT_TCP),
|
---|
542 | pbSegHdrs, pGso->offHdr2, pbSegPayload, cbSegPayload, iSeg * pGso->cbMaxSeg,
|
---|
543 | cbSegHdrs, iSeg + 1 == cSegs, PDMNETCSUMTYPE_COMPLETE);
|
---|
544 | break;
|
---|
545 | case PDMNETWORKGSOTYPE_IPV6_UDP:
|
---|
546 | pdmNetGsoUpdateUdpHdr(pdmNetGsoUpdateIPv6Hdr(pbSegHdrs, pGso->offHdr1, cbSegPayload, cbSegHdrs,
|
---|
547 | pGso->offHdr2, RTNETIPV4_PROT_UDP),
|
---|
548 | pbSegHdrs, pGso->offHdr2, pbSegPayload, cbSegPayload, cbSegHdrs, PDMNETCSUMTYPE_COMPLETE);
|
---|
549 | break;
|
---|
550 | case PDMNETWORKGSOTYPE_IPV4_IPV6_TCP:
|
---|
551 | pdmNetGsoUpdateIPv4Hdr(pbSegHdrs, pGso->offHdr1, cbSegPayload, iSeg, cbSegHdrs);
|
---|
552 | pdmNetGsoUpdateTcpHdr(pdmNetGsoUpdateIPv6Hdr(pbSegHdrs, pgmNetGsoCalcIpv6Offset(pbSegHdrs, pGso->offHdr1),
|
---|
553 | cbSegPayload, cbSegHdrs, pGso->offHdr2, RTNETIPV4_PROT_TCP),
|
---|
554 | pbSegHdrs, pGso->offHdr2, pbSegPayload, cbSegPayload, iSeg * pGso->cbMaxSeg,
|
---|
555 | cbSegHdrs, iSeg + 1 == cSegs, PDMNETCSUMTYPE_COMPLETE);
|
---|
556 | break;
|
---|
557 | case PDMNETWORKGSOTYPE_IPV4_IPV6_UDP:
|
---|
558 | pdmNetGsoUpdateIPv4Hdr(pbSegHdrs, pGso->offHdr1, cbSegPayload, iSeg, cbSegHdrs);
|
---|
559 | pdmNetGsoUpdateUdpHdr(pdmNetGsoUpdateIPv6Hdr(pbSegHdrs, pgmNetGsoCalcIpv6Offset(pbSegHdrs, pGso->offHdr1),
|
---|
560 | cbSegPayload, cbSegHdrs, pGso->offHdr2, RTNETIPV4_PROT_UDP),
|
---|
561 | pbSegHdrs, pGso->offHdr2, pbSegPayload, cbSegPayload, cbSegHdrs, PDMNETCSUMTYPE_COMPLETE);
|
---|
562 | break;
|
---|
563 | case PDMNETWORKGSOTYPE_INVALID:
|
---|
564 | case PDMNETWORKGSOTYPE_END:
|
---|
565 | /* no default! wnat gcc warnings. */
|
---|
566 | break;
|
---|
567 | }
|
---|
568 |
|
---|
569 | *pcbSegHdrs = cbSegHdrs;
|
---|
570 | *pcbSegPayload = cbSegPayload;
|
---|
571 | return cbSegHdrs + iSeg * pGso->cbMaxSeg;
|
---|
572 | }
|
---|
573 |
|
---|
574 |
|
---|
575 | /**
|
---|
576 | * Prepares the GSO frame for direct use without any segmenting.
|
---|
577 | *
|
---|
578 | * @param pGso The GSO context.
|
---|
579 | * @param pvFrame The frame to prepare.
|
---|
580 | * @param cbFrame The frame size.
|
---|
581 | * @param enmCsumType Whether to checksum the payload, the pseudo
|
---|
582 | * header or nothing.
|
---|
583 | */
|
---|
584 | DECLINLINE(void) PDMNetGsoPrepForDirectUse(PCPDMNETWORKGSO pGso, void *pvFrame, size_t cbFrame, PDMNETCSUMTYPE enmCsumType)
|
---|
585 | {
|
---|
586 | /*
|
---|
587 | * Figure out where the payload is and where the header starts before we
|
---|
588 | * do the protocol bits.
|
---|
589 | */
|
---|
590 | uint8_t * const pbHdrs = (uint8_t *)pvFrame;
|
---|
591 | uint8_t * const pbPayload = pbHdrs + pGso->cbHdrsTotal;
|
---|
592 | uint32_t const cbFrame32 = (uint32_t)cbFrame;
|
---|
593 | uint32_t const cbPayload = cbFrame32 - pGso->cbHdrsTotal;
|
---|
594 |
|
---|
595 | /*
|
---|
596 | * Check assumptions (doing it after declaring the variables because of C).
|
---|
597 | */
|
---|
598 | Assert(PDMNetGsoIsValid(pGso, sizeof(*pGso), cbFrame));
|
---|
599 |
|
---|
600 | /*
|
---|
601 | * Get down to busienss.
|
---|
602 | */
|
---|
603 | switch ((PDMNETWORKGSOTYPE)pGso->u8Type)
|
---|
604 | {
|
---|
605 | case PDMNETWORKGSOTYPE_IPV4_TCP:
|
---|
606 | pdmNetGsoUpdateTcpHdr(pdmNetGsoUpdateIPv4Hdr(pbHdrs, pGso->offHdr1, cbFrame32 - pGso->cbHdrsTotal, 0, pGso->cbHdrsTotal),
|
---|
607 | pbHdrs, pGso->offHdr2, pbPayload, cbPayload, 0, pGso->cbHdrsTotal, true, enmCsumType);
|
---|
608 | break;
|
---|
609 | case PDMNETWORKGSOTYPE_IPV4_UDP:
|
---|
610 | pdmNetGsoUpdateUdpHdr(pdmNetGsoUpdateIPv4Hdr(pbHdrs, pGso->offHdr1, cbFrame32 - pGso->cbHdrsTotal, 0, pGso->cbHdrsTotal),
|
---|
611 | pbHdrs, pGso->offHdr2, pbPayload, cbPayload, pGso->cbHdrsTotal, enmCsumType);
|
---|
612 | break;
|
---|
613 | case PDMNETWORKGSOTYPE_IPV6_TCP:
|
---|
614 | pdmNetGsoUpdateTcpHdr(pdmNetGsoUpdateIPv6Hdr(pbHdrs, pGso->offHdr1, cbPayload, pGso->cbHdrsTotal,
|
---|
615 | pGso->offHdr2, RTNETIPV4_PROT_TCP),
|
---|
616 | pbHdrs, pGso->offHdr2, pbPayload, cbPayload, 0, pGso->cbHdrsTotal, true, enmCsumType);
|
---|
617 | break;
|
---|
618 | case PDMNETWORKGSOTYPE_IPV6_UDP:
|
---|
619 | pdmNetGsoUpdateUdpHdr(pdmNetGsoUpdateIPv6Hdr(pbHdrs, pGso->offHdr1, cbPayload, pGso->cbHdrsTotal,
|
---|
620 | pGso->offHdr2, RTNETIPV4_PROT_UDP),
|
---|
621 | pbHdrs, pGso->offHdr2, pbPayload, cbPayload, pGso->cbHdrsTotal, enmCsumType);
|
---|
622 | break;
|
---|
623 | case PDMNETWORKGSOTYPE_IPV4_IPV6_TCP:
|
---|
624 | pdmNetGsoUpdateIPv4Hdr(pbHdrs, pGso->offHdr1, cbPayload, 0, pGso->cbHdrsTotal);
|
---|
625 | pdmNetGsoUpdateTcpHdr(pdmNetGsoUpdateIPv6Hdr(pbHdrs, pgmNetGsoCalcIpv6Offset(pbHdrs, pGso->offHdr1),
|
---|
626 | cbPayload, pGso->cbHdrsTotal, pGso->offHdr2, RTNETIPV4_PROT_TCP),
|
---|
627 | pbHdrs, pGso->offHdr2, pbPayload, cbPayload, 0, pGso->cbHdrsTotal, true, enmCsumType);
|
---|
628 | break;
|
---|
629 | case PDMNETWORKGSOTYPE_IPV4_IPV6_UDP:
|
---|
630 | pdmNetGsoUpdateIPv4Hdr(pbHdrs, pGso->offHdr1, cbPayload, 0, pGso->cbHdrsTotal);
|
---|
631 | pdmNetGsoUpdateUdpHdr(pdmNetGsoUpdateIPv6Hdr(pbHdrs, pgmNetGsoCalcIpv6Offset(pbHdrs, pGso->offHdr1),
|
---|
632 | cbPayload, pGso->cbHdrsTotal, pGso->offHdr2, RTNETIPV4_PROT_UDP),
|
---|
633 | pbHdrs, pGso->offHdr2, pbPayload, cbPayload, pGso->cbHdrsTotal, enmCsumType);
|
---|
634 | break;
|
---|
635 | case PDMNETWORKGSOTYPE_INVALID:
|
---|
636 | case PDMNETWORKGSOTYPE_END:
|
---|
637 | /* no default! wnat gcc warnings. */
|
---|
638 | break;
|
---|
639 | }
|
---|
640 | }
|
---|
641 |
|
---|
642 |
|
---|
643 | /**
|
---|
644 | * Gets the GSO type name string.
|
---|
645 | *
|
---|
646 | * @returns Pointer to read only name string.
|
---|
647 | * @param enmType The type.
|
---|
648 | */
|
---|
649 | DECLINLINE(const char *) PDMNetGsoTypeName(PDMNETWORKGSOTYPE enmType)
|
---|
650 | {
|
---|
651 | switch (enmType)
|
---|
652 | {
|
---|
653 | case PDMNETWORKGSOTYPE_IPV4_TCP: return "TCPv4";
|
---|
654 | case PDMNETWORKGSOTYPE_IPV6_TCP: return "TCPv6";
|
---|
655 | case PDMNETWORKGSOTYPE_IPV4_UDP: return "UDPv4";
|
---|
656 | case PDMNETWORKGSOTYPE_IPV6_UDP: return "UDPv6";
|
---|
657 | case PDMNETWORKGSOTYPE_IPV4_IPV6_TCP: return "4to6TCP";
|
---|
658 | case PDMNETWORKGSOTYPE_IPV4_IPV6_UDP: return "4to6UDP";
|
---|
659 | case PDMNETWORKGSOTYPE_INVALID: return "invalid";
|
---|
660 | case PDMNETWORKGSOTYPE_END: return "end";
|
---|
661 | }
|
---|
662 | return "bad-gso-type";
|
---|
663 | }
|
---|
664 |
|
---|