VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/checksum/ipv4.cpp@ 25659

Last change on this file since 25659 was 25645, checked in by vboxsync, 15 years ago

IPRT,DoxyFile.Core: Mopped up the errors in the IPRT doxygen run.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 27.4 KB
Line 
1/* $Id: ipv4.cpp 25645 2010-01-05 09:29:31Z vboxsync $ */
2/** @file
3 * IPRT - IPv4 Checksum calculation and validation.
4 */
5
6/*
7 * Copyright (C) 2008 Sun Microsystems, Inc.
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 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 *
26 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#include <iprt/net.h>
36#include "internal/iprt.h"
37
38#include <iprt/asm.h>
39#include <iprt/assert.h>
40
41
42/**
43 * Calculates the checksum of the IPv4 header.
44 *
45 * @returns Checksum (network endian).
46 * @param pIpHdr Pointer to the IPv4 header to checksum, network endian (big).
47 * Assumes the caller already checked the minimum size requirement.
48 */
49RTDECL(uint16_t) RTNetIPv4HdrChecksum(PCRTNETIPV4 pIpHdr)
50{
51 uint16_t const *paw = (uint16_t const *)pIpHdr;
52 uint32_t u32Sum = paw[0] /* ip_hl */
53 + paw[1] /* ip_len */
54 + paw[2] /* ip_id */
55 + paw[3] /* ip_off */
56 + paw[4] /* ip_ttl */
57 /*+ paw[5] == 0 */ /* ip_sum */
58 + paw[6] /* ip_src */
59 + paw[7] /* ip_src:16 */
60 + paw[8] /* ip_dst */
61 + paw[9]; /* ip_dst:16 */
62 /* any options */
63 if (pIpHdr->ip_hl > 20 / 4)
64 {
65 /* this is a bit insane... (identical to the TCP header) */
66 switch (pIpHdr->ip_hl)
67 {
68 case 6: u32Sum += paw[10] + paw[11]; break;
69 case 7: u32Sum += paw[10] + paw[11] + paw[12] + paw[13]; break;
70 case 8: u32Sum += paw[10] + paw[11] + paw[12] + paw[13] + paw[14] + paw[15]; break;
71 case 9: u32Sum += paw[10] + paw[11] + paw[12] + paw[13] + paw[14] + paw[15] + paw[16] + paw[17]; break;
72 case 10: u32Sum += paw[10] + paw[11] + paw[12] + paw[13] + paw[14] + paw[15] + paw[16] + paw[17] + paw[18] + paw[19]; break;
73 case 11: u32Sum += paw[10] + paw[11] + paw[12] + paw[13] + paw[14] + paw[15] + paw[16] + paw[17] + paw[18] + paw[19] + paw[20] + paw[21]; break;
74 case 12: u32Sum += paw[10] + paw[11] + paw[12] + paw[13] + paw[14] + paw[15] + paw[16] + paw[17] + paw[18] + paw[19] + paw[20] + paw[21] + paw[22] + paw[23]; break;
75 case 13: u32Sum += paw[10] + paw[11] + paw[12] + paw[13] + paw[14] + paw[15] + paw[16] + paw[17] + paw[18] + paw[19] + paw[20] + paw[21] + paw[22] + paw[23] + paw[24] + paw[25]; break;
76 case 14: u32Sum += paw[10] + paw[11] + paw[12] + paw[13] + paw[14] + paw[15] + paw[16] + paw[17] + paw[18] + paw[19] + paw[20] + paw[21] + paw[22] + paw[23] + paw[24] + paw[25] + paw[26] + paw[27]; break;
77 case 15: u32Sum += paw[10] + paw[11] + paw[12] + paw[13] + paw[14] + paw[15] + paw[16] + paw[17] + paw[18] + paw[19] + paw[20] + paw[21] + paw[22] + paw[23] + paw[24] + paw[25] + paw[26] + paw[27] + paw[28] + paw[29]; break;
78 default:
79 AssertFailed();
80 }
81 }
82
83 /* 16-bit one complement fun */
84 u32Sum = (u32Sum >> 16) + (u32Sum & 0xffff); /* hi + low words */
85 u32Sum += u32Sum >> 16; /* carry */
86 return (uint16_t)~u32Sum;
87}
88RT_EXPORT_SYMBOL(RTNetIPv4HdrChecksum);
89
90
91/**
92 * Verifies the header version, header size, packet size, and header checksum
93 * of the specified IPv4 header.
94 *
95 * @returns true if valid, false if invalid.
96 * @param pIpHdr Pointer to the IPv4 header to validate. Network endian (big).
97 * @param cbHdrMax The max header size, or the max size of what pIpHdr points
98 * to if you like. Note that an IPv4 header can be up to 60 bytes.
99 * @param cbPktMax The max IP packet size, IP header and payload. This doesn't have
100 * to be mapped following pIpHdr.
101 */
102RTDECL(bool) RTNetIPv4IsHdrValid(PCRTNETIPV4 pIpHdr, size_t cbHdrMax, size_t cbPktMax)
103{
104 /*
105 * The header fields.
106 */
107 Assert(cbPktMax >= cbHdrMax);
108 if (RT_UNLIKELY(cbHdrMax < RTNETIPV4_MIN_LEN))
109 return false;
110 if (RT_UNLIKELY(pIpHdr->ip_hl * 4 < RTNETIPV4_MIN_LEN))
111 return false;
112 if (RT_UNLIKELY((size_t)pIpHdr->ip_hl * 4 > cbHdrMax))
113 {
114 Assert((size_t)pIpHdr->ip_hl * 4 > cbPktMax); /* You'll hit this if you mapped/copy too little of the header! */
115 return false;
116 }
117 if (RT_UNLIKELY(pIpHdr->ip_v != 4))
118 return false;
119 if (RT_UNLIKELY(RT_BE2H_U16(pIpHdr->ip_len) > cbPktMax))
120 return false;
121
122 /*
123 * The header checksum.
124 */
125 uint16_t u16Sum = RTNetIPv4HdrChecksum(pIpHdr);
126 if (RT_UNLIKELY(pIpHdr->ip_sum != u16Sum))
127 return false;
128 return true;
129}
130RT_EXPORT_SYMBOL(RTNetIPv4IsHdrValid);
131
132
133/**
134 * Calculates the checksum of a pseudo header given an IPv4 header [inlined].
135 *
136 * @returns 32-bit intermediary checksum value.
137 * @param pIpHdr The IP header (network endian (big)).
138 */
139DECLINLINE(uint32_t) rtNetIPv4PseudoChecksum(PCRTNETIPV4 pIpHdr)
140{
141 uint16_t cbPayload = RT_BE2H_U16(pIpHdr->ip_len) - pIpHdr->ip_hl * 4;
142 uint32_t u32Sum = pIpHdr->ip_src.au16[0]
143 + pIpHdr->ip_src.au16[1]
144 + pIpHdr->ip_dst.au16[0]
145 + pIpHdr->ip_dst.au16[1]
146#ifdef RT_BIG_ENDIAN
147 + pIpHdr->ip_p
148#else
149 + ((uint32_t)pIpHdr->ip_p << 8)
150#endif
151 + RT_H2BE_U16(cbPayload);
152 return u32Sum;
153}
154
155
156/**
157 * Calculates the checksum of a pseudo header given an IPv4 header.
158 *
159 * @returns 32-bit intermediary checksum value.
160 * @param pIpHdr The IP header (network endian (big)).
161 */
162RTDECL(uint32_t) RTNetIPv4PseudoChecksum(PCRTNETIPV4 pIpHdr)
163{
164 return rtNetIPv4PseudoChecksum(pIpHdr);
165}
166RT_EXPORT_SYMBOL(RTNetIPv4PseudoChecksum);
167
168
169/**
170 * Calculates the checksum of a pseudo header given the individual components.
171 *
172 * @returns 32-bit intermediary checksum value.
173 * @param SrcAddr The source address in host endian.
174 * @param DstAddr The destination address in host endian.
175 * @param bProtocol The protocol number.
176 * @param cbPkt The packet size (host endian of course) (no IPv4 header).
177 */
178RTDECL(uint32_t) RTNetIPv4PseudoChecksumBits(RTNETADDRIPV4 SrcAddr, RTNETADDRIPV4 DstAddr, uint8_t bProtocol, uint16_t cbPkt)
179{
180 uint32_t u32Sum = RT_H2BE_U16(SrcAddr.au16[0])
181 + RT_H2BE_U16(SrcAddr.au16[1])
182 + RT_H2BE_U16(DstAddr.au16[0])
183 + RT_H2BE_U16(DstAddr.au16[1])
184#ifdef RT_BIG_ENDIAN
185 + bProtocol
186#else
187 + ((uint32_t)bProtocol << 8)
188#endif
189 + RT_H2BE_U16(cbPkt);
190 return u32Sum;
191}
192RT_EXPORT_SYMBOL(RTNetIPv4PseudoChecksumBits);
193
194
195/**
196 * Adds the checksum of the UDP header to the intermediate checksum value [inlined].
197 *
198 * @returns 32-bit intermediary checksum value.
199 * @param pUdpHdr Pointer to the UDP header to checksum, network endian (big).
200 * @param u32Sum The 32-bit intermediate checksum value.
201 */
202DECLINLINE(uint32_t) rtNetIPv4AddUDPChecksum(PCRTNETUDP pUdpHdr, uint32_t u32Sum)
203{
204 u32Sum += pUdpHdr->uh_sport
205 + pUdpHdr->uh_dport
206 /*+ pUdpHdr->uh_sum = 0 */
207 + pUdpHdr->uh_ulen;
208 return u32Sum;
209}
210
211
212/**
213 * Adds the checksum of the UDP header to the intermediate checksum value.
214 *
215 * @returns 32-bit intermediary checksum value.
216 * @param pUdpHdr Pointer to the UDP header to checksum, network endian (big).
217 * @param u32Sum The 32-bit intermediate checksum value.
218 */
219RTDECL(uint32_t) RTNetIPv4AddUDPChecksum(PCRTNETUDP pUdpHdr, uint32_t u32Sum)
220{
221 return rtNetIPv4AddUDPChecksum(pUdpHdr, u32Sum);
222}
223RT_EXPORT_SYMBOL(RTNetIPv4AddUDPChecksum);
224
225
226/**
227 * Adds the checksum of the TCP header to the intermediate checksum value [inlined].
228 *
229 * @returns 32-bit intermediary checksum value.
230 * @param pTcpHdr Pointer to the TCP header to checksum, network
231 * endian (big). Assums the caller has already validate
232 * it and made sure the entire header is present.
233 * @param u32Sum The 32-bit intermediate checksum value.
234 */
235DECLINLINE(uint32_t) rtNetIPv4AddTCPChecksum(PCRTNETTCP pTcpHdr, uint32_t u32Sum)
236{
237 uint16_t const *paw = (uint16_t const *)pTcpHdr;
238 u32Sum += paw[0] /* th_sport */
239 + paw[1] /* th_dport */
240 + paw[2] /* th_seq */
241 + paw[3] /* th_seq:16 */
242 + paw[4] /* th_ack */
243 + paw[5] /* th_ack:16 */
244 + paw[6] /* th_off, th_x2, th_flags */
245 + paw[7] /* th_win */
246 /*+ paw[8] == 0 */ /* th_sum */
247 + paw[9]; /* th_urp */
248 if (pTcpHdr->th_off > RTNETTCP_MIN_LEN / 4)
249 {
250 /* this is a bit insane... (identical to the IPv4 header) */
251 switch (pTcpHdr->th_off)
252 {
253 case 6: u32Sum += paw[10] + paw[11]; break;
254 case 7: u32Sum += paw[10] + paw[11] + paw[12] + paw[13]; break;
255 case 8: u32Sum += paw[10] + paw[11] + paw[12] + paw[13] + paw[14] + paw[15]; break;
256 case 9: u32Sum += paw[10] + paw[11] + paw[12] + paw[13] + paw[14] + paw[15] + paw[16] + paw[17]; break;
257 case 10: u32Sum += paw[10] + paw[11] + paw[12] + paw[13] + paw[14] + paw[15] + paw[16] + paw[17] + paw[18] + paw[19]; break;
258 case 11: u32Sum += paw[10] + paw[11] + paw[12] + paw[13] + paw[14] + paw[15] + paw[16] + paw[17] + paw[18] + paw[19] + paw[20] + paw[21]; break;
259 case 12: u32Sum += paw[10] + paw[11] + paw[12] + paw[13] + paw[14] + paw[15] + paw[16] + paw[17] + paw[18] + paw[19] + paw[20] + paw[21] + paw[22] + paw[23]; break;
260 case 13: u32Sum += paw[10] + paw[11] + paw[12] + paw[13] + paw[14] + paw[15] + paw[16] + paw[17] + paw[18] + paw[19] + paw[20] + paw[21] + paw[22] + paw[23] + paw[24] + paw[25]; break;
261 case 14: u32Sum += paw[10] + paw[11] + paw[12] + paw[13] + paw[14] + paw[15] + paw[16] + paw[17] + paw[18] + paw[19] + paw[20] + paw[21] + paw[22] + paw[23] + paw[24] + paw[25] + paw[26] + paw[27]; break;
262 case 15: u32Sum += paw[10] + paw[11] + paw[12] + paw[13] + paw[14] + paw[15] + paw[16] + paw[17] + paw[18] + paw[19] + paw[20] + paw[21] + paw[22] + paw[23] + paw[24] + paw[25] + paw[26] + paw[27] + paw[28] + paw[29]; break;
263 default:
264 AssertFailed();
265 }
266 }
267
268 return u32Sum;
269}
270
271
272/**
273 * Adds the checksum of the TCP header to the intermediate checksum value.
274 *
275 * @returns 32-bit intermediary checksum value.
276 * @param pTcpHdr Pointer to the TCP header to checksum, network
277 * endian (big). Assums the caller has already validate
278 * it and made sure the entire header is present.
279 * @param u32Sum The 32-bit intermediate checksum value.
280 */
281RTDECL(uint32_t) RTNetIPv4AddTCPChecksum(PCRTNETTCP pTcpHdr, uint32_t u32Sum)
282{
283 return rtNetIPv4AddTCPChecksum(pTcpHdr, u32Sum);
284}
285RT_EXPORT_SYMBOL(RTNetIPv4AddTCPChecksum);
286
287
288/**
289 * Adds the checksum of the specified data segment to the intermediate checksum value [inlined].
290 *
291 * @returns 32-bit intermediary checksum value.
292 * @param pvData Pointer to the data that should be checksummed.
293 * @param cbData The number of bytes to checksum.
294 * @param u32Sum The 32-bit intermediate checksum value.
295 * @param pfOdd This is used to keep track of odd bits, initialize to false
296 * when starting to checksum the data (aka text) after a TCP
297 * or UDP header (data never start at an odd offset).
298 */
299DECLINLINE(uint32_t) rtNetIPv4AddDataChecksum(void const *pvData, size_t cbData, uint32_t u32Sum, bool *pfOdd)
300{
301 if (*pfOdd)
302 {
303#ifdef RT_BIG_ENDIAN
304 /* there was an odd byte in the previous chunk, add the lower byte. */
305 u32Sum += *(uint8_t *)pvData;
306#else
307 /* there was an odd byte in the previous chunk, add the upper byte. */
308 u32Sum += (uint32_t)*(uint8_t *)pvData << 8;
309#endif
310 /* skip the byte. */
311 cbData--;
312 if (!cbData)
313 return u32Sum;
314 pvData = (uint8_t const *)pvData + 1;
315 }
316
317 /* iterate the data. */
318 uint16_t const *pw = (uint16_t const *)pvData;
319 while (cbData > 1)
320 {
321 u32Sum += *pw;
322 pw++;
323 cbData -= 2;
324 }
325
326 /* handle odd byte. */
327 if (cbData)
328 {
329#ifdef RT_BIG_ENDIAN
330 u32Sum += (uint32_t)*(uint8_t *)pw << 8;
331#else
332 u32Sum += *(uint8_t *)pw;
333#endif
334 *pfOdd = true;
335 }
336 else
337 *pfOdd = false;
338 return u32Sum;
339}
340
341/**
342 * Adds the checksum of the specified data segment to the intermediate checksum value.
343 *
344 * @returns 32-bit intermediary checksum value.
345 * @param pvData The data bits to checksum.
346 * @param cbData The number of bytes to checksum.
347 * @param u32Sum The 32-bit intermediate checksum value.
348 * @param pfOdd This is used to keep track of odd bits, initialize to false
349 * when starting to checksum the data (aka text) after a TCP
350 * or UDP header (data never start at an odd offset).
351 */
352RTDECL(uint32_t) RTNetIPv4AddDataChecksum(void const *pvData, size_t cbData, uint32_t u32Sum, bool *pfOdd)
353{
354 return rtNetIPv4AddDataChecksum(pvData, cbData, u32Sum, pfOdd);
355}
356RT_EXPORT_SYMBOL(RTNetIPv4AddDataChecksum);
357
358
359/**
360 * Finalizes a IPv4 checksum [inlined].
361 *
362 * @returns The checksum (network endian).
363 * @param u32Sum The 32-bit intermediate checksum value.
364 */
365DECLINLINE(uint16_t) rtNetIPv4FinalizeChecksum(uint32_t u32Sum)
366{
367 /* 16-bit one complement fun */
368 u32Sum = (u32Sum >> 16) + (u32Sum & 0xffff); /* hi + low words */
369 u32Sum += u32Sum >> 16; /* carry */
370 return (uint16_t)~u32Sum;
371}
372
373
374/**
375 * Finalizes a IPv4 checksum.
376 *
377 * @returns The checksum (network endian).
378 * @param u32Sum The 32-bit intermediate checksum value.
379 */
380RTDECL(uint16_t) RTNetIPv4FinalizeChecksum(uint32_t u32Sum)
381{
382 return rtNetIPv4FinalizeChecksum(u32Sum);
383}
384RT_EXPORT_SYMBOL(RTNetIPv4FinalizeChecksum);
385
386
387/**
388 * Calculates the checksum for the UDP header given the IP header,
389 * UDP header and payload.
390 *
391 * @returns The checksum (network endian).
392 * @param pIpHdr Pointer to the IPv4 header, in network endian (big).
393 * @param pUdpHdr Pointer to the UDP header, in network endian (big).
394 * @param pvData Pointer to the UDP payload. The size is taken from the
395 * UDP header and the caller is supposed to have validated
396 * this before calling.
397 */
398RTDECL(uint16_t) RTNetIPv4UDPChecksum(PCRTNETIPV4 pIpHdr, PCRTNETUDP pUdpHdr, void const *pvData)
399{
400 uint32_t u32Sum = rtNetIPv4PseudoChecksum(pIpHdr);
401 u32Sum = rtNetIPv4AddUDPChecksum(pUdpHdr, u32Sum);
402 bool fOdd = false;
403 u32Sum = rtNetIPv4AddDataChecksum(pvData, RT_BE2H_U16(pUdpHdr->uh_ulen) - sizeof(*pUdpHdr), u32Sum, &fOdd);
404 return rtNetIPv4FinalizeChecksum(u32Sum);
405}
406RT_EXPORT_SYMBOL(RTNetIPv4UDPChecksum);
407
408
409/**
410 * Simple verficiation of an UDP packet size.
411 *
412 * @returns true if valid, false if invalid.
413 * @param pIpHdr Pointer to the IPv4 header, in network endian (big).
414 * This is assumed to be valid and the minimum size being mapped.
415 * @param pUdpHdr Pointer to the UDP header, in network endian (big).
416 * @param cbPktMax The max UDP packet size, UDP header and payload (data).
417 */
418DECLINLINE(bool) rtNetIPv4IsUDPSizeValid(PCRTNETIPV4 pIpHdr, PCRTNETUDP pUdpHdr, size_t cbPktMax)
419{
420 /*
421 * Size validation.
422 */
423 if (RT_UNLIKELY(cbPktMax < RTNETUDP_MIN_LEN))
424 return false;
425 size_t cb = RT_BE2H_U16(pUdpHdr->uh_ulen);
426 if (RT_UNLIKELY(cb > cbPktMax))
427 return false;
428 if (RT_UNLIKELY(cb > (size_t)(RT_BE2H_U16(pIpHdr->ip_len) - pIpHdr->ip_hl * 4)))
429 return false;
430 return true;
431}
432
433
434/**
435 * Simple verficiation of an UDP packet size.
436 *
437 * @returns true if valid, false if invalid.
438 * @param pIpHdr Pointer to the IPv4 header, in network endian (big).
439 * This is assumed to be valid and the minimum size being mapped.
440 * @param pUdpHdr Pointer to the UDP header, in network endian (big).
441 * @param cbPktMax The max UDP packet size, UDP header and payload (data).
442 */
443RTDECL(bool) RTNetIPv4IsUDPSizeValid(PCRTNETIPV4 pIpHdr, PCRTNETUDP pUdpHdr, size_t cbPktMax)
444{
445 return rtNetIPv4IsUDPSizeValid(pIpHdr, pUdpHdr, cbPktMax);
446}
447RT_EXPORT_SYMBOL(RTNetIPv4IsUDPSizeValid);
448
449
450/**
451 * Simple verficiation of an UDP packet (size + checksum).
452 *
453 * @returns true if valid, false if invalid.
454 * @param pIpHdr Pointer to the IPv4 header, in network endian (big).
455 * This is assumed to be valid and the minimum size being mapped.
456 * @param pUdpHdr Pointer to the UDP header, in network endian (big).
457 * @param pvData Pointer to the data, assuming it's one single segment
458 * and that cbPktMax - sizeof(RTNETUDP) is mapped here.
459 * @param cbPktMax The max UDP packet size, UDP header and payload (data).
460 */
461RTDECL(bool) RTNetIPv4IsUDPValid(PCRTNETIPV4 pIpHdr, PCRTNETUDP pUdpHdr, void const *pvData, size_t cbPktMax)
462{
463 if (RT_UNLIKELY(!rtNetIPv4IsUDPSizeValid(pIpHdr, pUdpHdr, cbPktMax)))
464 return false;
465 if (pUdpHdr->uh_sum)
466 {
467 uint16_t u16Sum = RTNetIPv4UDPChecksum(pIpHdr, pUdpHdr, pvData);
468 if (RT_UNLIKELY(pUdpHdr->uh_sum != u16Sum))
469 return false;
470 }
471 return true;
472}
473RT_EXPORT_SYMBOL(RTNetIPv4IsUDPValid);
474
475
476/**
477 * Calculates the checksum for the TCP header given the IP header,
478 * TCP header and payload.
479 *
480 * @returns The checksum (network endian).
481 * @param pIpHdr Pointer to the IPv4 header, in network endian (big).
482 * @param pTcpHdr Pointer to the TCP header, in network endian (big).
483 * @param pvData Pointer to the TCP payload. The size is taken from the
484 * TCP header and the caller is supposed to have validated
485 * this before calling.
486 * If NULL then we assume the data follows immediately after
487 * the TCP header.
488 */
489RTDECL(uint16_t) RTNetIPv4TCPChecksum(PCRTNETIPV4 pIpHdr, PCRTNETTCP pTcpHdr, void const *pvData)
490{
491 uint32_t u32Sum = rtNetIPv4PseudoChecksum(pIpHdr);
492 u32Sum = rtNetIPv4AddTCPChecksum(pTcpHdr, u32Sum);
493 bool fOdd = false;
494 size_t cbData = RT_BE2H_U16(pIpHdr->ip_len) - pIpHdr->ip_hl * 4 - pTcpHdr->th_off * 4;
495 u32Sum = rtNetIPv4AddDataChecksum(pvData ? pvData : (uint8_t const *)pTcpHdr + pTcpHdr->th_off * 4,
496 cbData, u32Sum, &fOdd);
497 return rtNetIPv4FinalizeChecksum(u32Sum);
498}
499RT_EXPORT_SYMBOL(RTNetIPv4TCPChecksum);
500
501
502/**
503 * Verficiation of a TCP header.
504 *
505 * @returns true if valid, false if invalid.
506 * @param pIpHdr Pointer to the IPv4 header, in network endian (big).
507 * This is assumed to be valid and the minimum size being mapped.
508 * @param pTcpHdr Pointer to the TCP header, in network endian (big).
509 * @param cbHdrMax The max TCP header size (what pTcpHdr points to).
510 * @param cbPktMax The max TCP packet size, TCP header and payload (data).
511 */
512DECLINLINE(bool) rtNetIPv4IsTCPSizeValid(PCRTNETIPV4 pIpHdr, PCRTNETTCP pTcpHdr, size_t cbHdrMax, size_t cbPktMax)
513{
514 Assert(cbPktMax >= cbHdrMax);
515
516 /*
517 * Size validations.
518 */
519 if (RT_UNLIKELY(cbPktMax < RTNETTCP_MIN_LEN))
520 return false;
521 size_t cbTcpHdr = pTcpHdr->th_off * 4;
522 if (RT_UNLIKELY(cbTcpHdr > cbHdrMax))
523 return false;
524 size_t cbTcp = RT_BE2H_U16(pIpHdr->ip_len) - pIpHdr->ip_hl * 4;
525 if (RT_UNLIKELY(cbTcp > cbPktMax))
526 return false;
527 return true;
528}
529
530
531/**
532 * Simple verficiation of an TCP packet size.
533 *
534 * @returns true if valid, false if invalid.
535 * @param pIpHdr Pointer to the IPv4 header, in network endian (big).
536 * This is assumed to be valid and the minimum size being mapped.
537 * @param pTcpHdr Pointer to the TCP header, in network endian (big).
538 * @param cbHdrMax The max TCP header size (what pTcpHdr points to).
539 * @param cbPktMax The max TCP packet size, TCP header and payload (data).
540 */
541RTDECL(bool) RTNetIPv4IsTCPSizeValid(PCRTNETIPV4 pIpHdr, PCRTNETTCP pTcpHdr, size_t cbHdrMax, size_t cbPktMax)
542{
543 return rtNetIPv4IsTCPSizeValid(pIpHdr, pTcpHdr, cbHdrMax, cbPktMax);
544}
545RT_EXPORT_SYMBOL(RTNetIPv4IsTCPSizeValid);
546
547
548/**
549 * Simple verficiation of an TCP packet (size + checksum).
550 *
551 * @returns true if valid, false if invalid.
552 * @param pIpHdr Pointer to the IPv4 header, in network endian (big).
553 * This is assumed to be valid and the minimum size being mapped.
554 * @param pTcpHdr Pointer to the TCP header, in network endian (big).
555 * @param cbHdrMax The max TCP header size (what pTcpHdr points to).
556 * @param pvData Pointer to the data, assuming it's one single segment
557 * and that cbPktMax - sizeof(RTNETTCP) is mapped here.
558 * If NULL then we assume the data follows immediately after
559 * the TCP header.
560 * @param cbPktMax The max TCP packet size, TCP header and payload (data).
561 */
562RTDECL(bool) RTNetIPv4IsTCPValid(PCRTNETIPV4 pIpHdr, PCRTNETTCP pTcpHdr, size_t cbHdrMax, void const *pvData, size_t cbPktMax)
563{
564 if (RT_UNLIKELY(!rtNetIPv4IsTCPSizeValid(pIpHdr, pTcpHdr, cbHdrMax, cbPktMax)))
565 return false;
566 uint16_t u16Sum = RTNetIPv4TCPChecksum(pIpHdr, pTcpHdr, pvData);
567 if (RT_UNLIKELY(pTcpHdr->th_sum != u16Sum))
568 return false;
569 return true;
570}
571RT_EXPORT_SYMBOL(RTNetIPv4IsTCPValid);
572
573
574/**
575 * Minimal validation of a DHCP packet.
576 *
577 * This will fail on BOOTP packets (if sufficient data is supplied).
578 * It will not verify the source and destination ports, that's the
579 * caller's responsibility.
580 *
581 * This function will ASSUME that the hardware type is ethernet
582 * and use that for htype/hlen validation.
583 *
584 * @returns true if valid, false if invalid.
585 * @param pUdpHdr Pointer to the UDP header, in network endian (big).
586 * This is assumed to be valid and fully mapped.
587 * @param pDhcp Pointer to the DHCP packet.
588 * This might not be the entire thing, see cbDhcp.
589 * @param cbDhcp The number of valid bytes that pDhcp points to.
590 * @param pMsgType Where to store the message type (if found).
591 * This will be set to 0 if not found and on failure.
592 */
593RTDECL(bool) RTNetIPv4IsDHCPValid(PCRTNETUDP pUdpHdr, PCRTNETBOOTP pDhcp, size_t cbDhcp, uint8_t *pMsgType)
594{
595 AssertPtrNull(pMsgType);
596 if (pMsgType)
597 *pMsgType = 0;
598
599 /*
600 * Validate all the header fields we're able to...
601 */
602 if (cbDhcp < RT_OFFSETOF(RTNETBOOTP, bp_op) + sizeof(pDhcp->bp_op))
603 return true;
604 if (RT_UNLIKELY( pDhcp->bp_op != RTNETBOOTP_OP_REQUEST
605 && pDhcp->bp_op != RTNETBOOTP_OP_REPLY))
606 return false;
607
608 if (cbDhcp < RT_OFFSETOF(RTNETBOOTP, bp_htype) + sizeof(pDhcp->bp_htype))
609 return true;
610 if (RT_UNLIKELY(pDhcp->bp_htype != RTNET_ARP_ETHER))
611 return false;
612
613 if (cbDhcp < RT_OFFSETOF(RTNETBOOTP, bp_hlen) + sizeof(pDhcp->bp_hlen))
614 return true;
615 if (RT_UNLIKELY(pDhcp->bp_hlen != sizeof(RTMAC)))
616 return false;
617
618 if (cbDhcp < RT_OFFSETOF(RTNETBOOTP, bp_flags) + sizeof(pDhcp->bp_flags))
619 return true;
620 if (RT_UNLIKELY(RT_BE2H_U16(pDhcp->bp_flags) & ~(RTNET_DHCP_FLAGS_NO_BROADCAST)))
621 return false;
622
623 /*
624 * Check the DHCP cookie and make sure it isn't followed by an END option
625 * (because that seems to be indicating that it's BOOTP and not DHCP).
626 */
627 ssize_t cbLeft = (ssize_t)cbDhcp - RT_OFFSETOF(RTNETBOOTP, bp_vend.Dhcp.dhcp_cookie) + sizeof(pDhcp->bp_vend.Dhcp.dhcp_cookie);
628 if (cbLeft < 0)
629 return true;
630 if (RT_UNLIKELY(RT_BE2H_U32(pDhcp->bp_vend.Dhcp.dhcp_cookie) != RTNET_DHCP_COOKIE))
631 return false;
632 if (cbLeft < 1)
633 return true;
634 PCRTNETDHCPOPT pOpt = (PCRTNETDHCPOPT)&pDhcp->bp_vend.Dhcp.dhcp_opts[0];
635 if (pOpt->dhcp_opt == RTNET_DHCP_OPT_END)
636 return false;
637
638 /*
639 * Scan the options until we find the message type or run out of message.
640 *
641 * We're not strict about termination (END) for many reasons, however,
642 * we don't accept END without MSG_TYPE.
643 */
644 uint8_t MsgType = 0;
645 while (cbLeft > 0)
646 {
647 if (pOpt->dhcp_opt == RTNET_DHCP_OPT_END)
648 {
649 /* Fail if no MSG_TYPE. */
650 if (!MsgType)
651 return false;
652 break;
653 }
654 if (pOpt->dhcp_opt == RTNET_DHCP_OPT_PAD)
655 {
656 pOpt = (PCRTNETDHCPOPT)((uint8_t const *)pOpt + 1);
657 cbLeft--;
658 }
659 else
660 {
661 switch (pOpt->dhcp_opt)
662 {
663 case RTNET_DHCP_OPT_MSG_TYPE:
664 {
665 if (cbLeft < 3)
666 return true;
667 MsgType = *(const uint8_t *)(pOpt + 1);
668 switch (MsgType)
669 {
670 case RTNET_DHCP_MT_DISCOVER:
671 case RTNET_DHCP_MT_OFFER:
672 case RTNET_DHCP_MT_REQUEST:
673 case RTNET_DHCP_MT_DECLINE:
674 case RTNET_DHCP_MT_ACK:
675 case RTNET_DHCP_MT_NAC:
676 case RTNET_DHCP_MT_RELEASE:
677 case RTNET_DHCP_MT_INFORM:
678 break;
679
680 default:
681 /* we don't know this message type, fail. */
682 return false;
683 }
684
685 /* Found a known message type, consider the job done. */
686 if (pMsgType)
687 *pMsgType = MsgType;
688 return true;
689 }
690 }
691
692 /* Skip the option. */
693 cbLeft -= pOpt->dhcp_len + sizeof(*pOpt);
694 pOpt = (PCRTNETDHCPOPT)((uint8_t const *)pOpt + pOpt->dhcp_len + sizeof(*pOpt));
695 }
696 }
697
698 return true;
699}
700RT_EXPORT_SYMBOL(RTNetIPv4IsDHCPValid);
701
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