1 | /* $Id: ipv6.cpp 69111 2017-10-17 14:26:02Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - IPv6 Checksum calculation and validation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2017 Oracle Corporation
|
---|
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 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include <iprt/net.h>
|
---|
32 | #include "internal/iprt.h"
|
---|
33 |
|
---|
34 | #include <iprt/asm.h>
|
---|
35 | #include <iprt/assert.h>
|
---|
36 |
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * @copydoc RTNetIPv6PseudoChecksumBits
|
---|
40 | */
|
---|
41 | DECLINLINE(uint32_t) rtNetIPv6PseudoChecksumBits(PCRTNETADDRIPV6 pSrcAddr, PCRTNETADDRIPV6 pDstAddr,
|
---|
42 | uint8_t bProtocol, uint32_t cbPkt)
|
---|
43 | {
|
---|
44 | uint32_t u32Sum = pSrcAddr->au16[0]
|
---|
45 | + pSrcAddr->au16[1]
|
---|
46 | + pSrcAddr->au16[2]
|
---|
47 | + pSrcAddr->au16[3]
|
---|
48 | + pSrcAddr->au16[4]
|
---|
49 | + pSrcAddr->au16[5]
|
---|
50 | + pSrcAddr->au16[6]
|
---|
51 | + pSrcAddr->au16[7]
|
---|
52 | + pDstAddr->au16[0]
|
---|
53 | + pDstAddr->au16[1]
|
---|
54 | + pDstAddr->au16[2]
|
---|
55 | + pDstAddr->au16[3]
|
---|
56 | + pDstAddr->au16[4]
|
---|
57 | + pDstAddr->au16[5]
|
---|
58 | + pDstAddr->au16[6]
|
---|
59 | + pDstAddr->au16[7]
|
---|
60 | + RT_H2BE_U16(RT_HIWORD(cbPkt))
|
---|
61 | + RT_H2BE_U16(RT_LOWORD(cbPkt))
|
---|
62 | + 0
|
---|
63 | + RT_H2BE_U16(RT_MAKE_U16(bProtocol, 0));
|
---|
64 | return u32Sum;
|
---|
65 | }
|
---|
66 |
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Calculates the checksum of a pseudo header given an IPv6 header, ASSUMING
|
---|
70 | * that there are no headers between the IPv6 header and the upper layer header.
|
---|
71 | *
|
---|
72 | * Use this method with create care! In most cases you should be using
|
---|
73 | * RTNetIPv6PseudoChecksumEx.
|
---|
74 | *
|
---|
75 | * @returns 32-bit intermediary checksum value.
|
---|
76 | * @param pIpHdr The IPv6 header (network endian (big)).
|
---|
77 | */
|
---|
78 | RTDECL(uint32_t) RTNetIPv6PseudoChecksum(PCRTNETIPV6 pIpHdr)
|
---|
79 | {
|
---|
80 | return rtNetIPv6PseudoChecksumBits(&pIpHdr->ip6_src, &pIpHdr->ip6_dst,
|
---|
81 | pIpHdr->ip6_nxt, RT_N2H_U16(pIpHdr->ip6_plen));
|
---|
82 | }
|
---|
83 | RT_EXPORT_SYMBOL(RTNetIPv6PseudoChecksum);
|
---|
84 |
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Calculates the checksum of a pseudo header given an IPv6 header.
|
---|
88 | *
|
---|
89 | * @returns 32-bit intermediary checksum value.
|
---|
90 | * @param pIpHdr The IPv6 header (network endian (big)).
|
---|
91 | * @param bProtocol The protocol number. This can be the same as the
|
---|
92 | * ip6_nxt field, but doesn't need to be.
|
---|
93 | * @param cbPkt The packet size (host endian of course). This can
|
---|
94 | * be the same as the ip6_plen field, but as with @a
|
---|
95 | * bProtocol it won't be when extension headers are
|
---|
96 | * present. For UDP this will be uh_ulen converted to
|
---|
97 | * host endian.
|
---|
98 | */
|
---|
99 | RTDECL(uint32_t) RTNetIPv6PseudoChecksumEx(PCRTNETIPV6 pIpHdr, uint8_t bProtocol, uint16_t cbPkt)
|
---|
100 | {
|
---|
101 | return rtNetIPv6PseudoChecksumBits(&pIpHdr->ip6_src, &pIpHdr->ip6_dst, bProtocol, cbPkt);
|
---|
102 | }
|
---|
103 | RT_EXPORT_SYMBOL(RTNetIPv6PseudoChecksumEx);
|
---|
104 |
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Calculates the checksum of a pseudo header given the individual components.
|
---|
108 | *
|
---|
109 | * @returns 32-bit intermediary checksum value.
|
---|
110 | * @param pSrcAddr Pointer to the source address in network endian.
|
---|
111 | * @param pDstAddr Pointer to the destination address in network endian.
|
---|
112 | * @param bProtocol The protocol number. This can be the same as the
|
---|
113 | * ip6_nxt field, but doesn't need to be.
|
---|
114 | * @param cbPkt The packet size (host endian of course). This can
|
---|
115 | * be the same as the ip6_plen field, but as with @a
|
---|
116 | * bProtocol it won't be when extension headers are
|
---|
117 | * present. For UDP this will be uh_ulen converted to
|
---|
118 | * host endian.
|
---|
119 | */
|
---|
120 | RTDECL(uint32_t) RTNetIPv6PseudoChecksumBits(PCRTNETADDRIPV6 pSrcAddr, PCRTNETADDRIPV6 pDstAddr,
|
---|
121 | uint8_t bProtocol, uint16_t cbPkt)
|
---|
122 | {
|
---|
123 | return rtNetIPv6PseudoChecksumBits(pSrcAddr, pDstAddr, bProtocol, cbPkt);
|
---|
124 | }
|
---|
125 | RT_EXPORT_SYMBOL(RTNetIPv6PseudoChecksumBits);
|
---|
126 |
|
---|