1 | /* $Id: ipv6.cpp 28026 2010-04-07 06:43:54Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - IPv6 Checksum calculation and validation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2010 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.alldomusa.eu.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 | * @copydoc RTNetIPv6PseudoChecksumBits
|
---|
44 | */
|
---|
45 | DECLINLINE(uint32_t) rtNetIPv6PseudoChecksumBits(PCRTNETADDRIPV6 pSrcAddr, PCRTNETADDRIPV6 pDstAddr,
|
---|
46 | uint8_t bProtocol, uint32_t cbPkt)
|
---|
47 | {
|
---|
48 | uint32_t u32Sum = pSrcAddr->au16[0]
|
---|
49 | + pSrcAddr->au16[1]
|
---|
50 | + pSrcAddr->au16[2]
|
---|
51 | + pSrcAddr->au16[3]
|
---|
52 | + pSrcAddr->au16[4]
|
---|
53 | + pSrcAddr->au16[5]
|
---|
54 | + pSrcAddr->au16[6]
|
---|
55 | + pSrcAddr->au16[7]
|
---|
56 | + pDstAddr->au16[0]
|
---|
57 | + pDstAddr->au16[1]
|
---|
58 | + pDstAddr->au16[2]
|
---|
59 | + pDstAddr->au16[3]
|
---|
60 | + pDstAddr->au16[4]
|
---|
61 | + pDstAddr->au16[5]
|
---|
62 | + pDstAddr->au16[6]
|
---|
63 | + pDstAddr->au16[7]
|
---|
64 | + RT_H2BE_U16(RT_HIWORD(cbPkt))
|
---|
65 | + RT_H2BE_U16(RT_LOWORD(cbPkt))
|
---|
66 | + 0
|
---|
67 | + RT_H2BE_U16(RT_MAKE_U16(0, bProtocol));
|
---|
68 | return u32Sum;
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Calculates the checksum of a pseudo header given an IPv6 header, ASSUMING
|
---|
74 | * that there are no headers between the IPv6 header and the upper layer header.
|
---|
75 | *
|
---|
76 | * Use this method with create care! In most cases you should be using
|
---|
77 | * RTNetIPv6PseudoChecksumEx.
|
---|
78 | *
|
---|
79 | * @returns 32-bit intermediary checksum value.
|
---|
80 | * @param pIpHdr The IPv6 header (network endian (big)).
|
---|
81 | */
|
---|
82 | RTDECL(uint32_t) RTNetIPv6PseudoChecksum(PCRTNETIPV6 pIpHdr)
|
---|
83 | {
|
---|
84 | return rtNetIPv6PseudoChecksumBits(&pIpHdr->ip6_src, &pIpHdr->ip6_dst,
|
---|
85 | pIpHdr->ip6_nxt, RT_N2H_U16(pIpHdr->ip6_plen));
|
---|
86 | }
|
---|
87 | RT_EXPORT_SYMBOL(RTNetIPv6PseudoChecksum);
|
---|
88 |
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Calculates the checksum of a pseudo header given an IPv6 header.
|
---|
92 | *
|
---|
93 | * @returns 32-bit intermediary checksum value.
|
---|
94 | * @param pIpHdr The IPv6 header (network endian (big)).
|
---|
95 | * @param bProtocol The protocol number. This can be the same as the
|
---|
96 | * ip6_nxt field, but doesn't need to be.
|
---|
97 | * @param cbPkt The packet size (host endian of course). This can
|
---|
98 | * be the same as the ip6_plen field, but as with @a
|
---|
99 | * bProtocol it won't be when extension headers are
|
---|
100 | * present. For UDP this will be uh_ulen converted to
|
---|
101 | * host endian.
|
---|
102 | */
|
---|
103 | RTDECL(uint32_t) RTNetIPv6PseudoChecksumEx(PCRTNETIPV6 pIpHdr, uint8_t bProtocol, uint16_t cbPkt)
|
---|
104 | {
|
---|
105 | return rtNetIPv6PseudoChecksumBits(&pIpHdr->ip6_src, &pIpHdr->ip6_dst, bProtocol, cbPkt);
|
---|
106 | }
|
---|
107 | RT_EXPORT_SYMBOL(RTNetIPv6PseudoChecksumEx);
|
---|
108 |
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Calculates the checksum of a pseudo header given the individual components.
|
---|
112 | *
|
---|
113 | * @returns 32-bit intermediary checksum value.
|
---|
114 | * @param pSrcAddr Pointer to the source address in network endian.
|
---|
115 | * @param pDstAddr Pointer to the destination address in network endian.
|
---|
116 | * @param bProtocol The protocol number. This can be the same as the
|
---|
117 | * ip6_nxt field, but doesn't need to be.
|
---|
118 | * @param cbPkt The packet size (host endian of course). This can
|
---|
119 | * be the same as the ip6_plen field, but as with @a
|
---|
120 | * bProtocol it won't be when extension headers are
|
---|
121 | * present. For UDP this will be uh_ulen converted to
|
---|
122 | * host endian.
|
---|
123 | */
|
---|
124 | RTDECL(uint32_t) RTNetIPv6PseudoChecksumBits(PCRTNETADDRIPV6 pSrcAddr, PCRTNETADDRIPV6 pDstAddr,
|
---|
125 | uint8_t bProtocol, uint16_t cbPkt)
|
---|
126 | {
|
---|
127 | return rtNetIPv6PseudoChecksumBits(pSrcAddr, pDstAddr, bProtocol, cbPkt);
|
---|
128 | }
|
---|
129 | RT_EXPORT_SYMBOL(RTNetIPv6PseudoChecksumBits);
|
---|
130 |
|
---|