VirtualBox

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

Last change on this file since 16935 was 12821, checked in by vboxsync, 16 years ago

annoying warning

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