VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/checksum/ipv6.cpp

Last change on this file was 98103, checked in by vboxsync, 16 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.5 KB
Line 
1/* $Id: ipv6.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - IPv6 Checksum calculation and validation.
4 */
5
6/*
7 * Copyright (C) 2008-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <iprt/net.h>
42#include "internal/iprt.h"
43
44#include <iprt/asm.h>
45#include <iprt/assert.h>
46
47
48/**
49 * @copydoc RTNetIPv6PseudoChecksumBits
50 */
51DECLINLINE(uint32_t) rtNetIPv6PseudoChecksumBits(PCRTNETADDRIPV6 pSrcAddr, PCRTNETADDRIPV6 pDstAddr,
52 uint8_t bProtocol, uint32_t cbPkt)
53{
54 uint32_t u32Sum = pSrcAddr->au16[0]
55 + pSrcAddr->au16[1]
56 + pSrcAddr->au16[2]
57 + pSrcAddr->au16[3]
58 + pSrcAddr->au16[4]
59 + pSrcAddr->au16[5]
60 + pSrcAddr->au16[6]
61 + pSrcAddr->au16[7]
62 + pDstAddr->au16[0]
63 + pDstAddr->au16[1]
64 + pDstAddr->au16[2]
65 + pDstAddr->au16[3]
66 + pDstAddr->au16[4]
67 + pDstAddr->au16[5]
68 + pDstAddr->au16[6]
69 + pDstAddr->au16[7]
70 + RT_H2BE_U16(RT_HIWORD(cbPkt))
71 + RT_H2BE_U16(RT_LOWORD(cbPkt))
72 + 0
73 + RT_H2BE_U16(RT_MAKE_U16(bProtocol, 0));
74 return u32Sum;
75}
76
77
78/**
79 * Calculates the checksum of a pseudo header given an IPv6 header, ASSUMING
80 * that there are no headers between the IPv6 header and the upper layer header.
81 *
82 * Use this method with create care! In most cases you should be using
83 * RTNetIPv6PseudoChecksumEx.
84 *
85 * @returns 32-bit intermediary checksum value.
86 * @param pIpHdr The IPv6 header (network endian (big)).
87 */
88RTDECL(uint32_t) RTNetIPv6PseudoChecksum(PCRTNETIPV6 pIpHdr)
89{
90 return rtNetIPv6PseudoChecksumBits(&pIpHdr->ip6_src, &pIpHdr->ip6_dst,
91 pIpHdr->ip6_nxt, RT_N2H_U16(pIpHdr->ip6_plen));
92}
93RT_EXPORT_SYMBOL(RTNetIPv6PseudoChecksum);
94
95
96/**
97 * Calculates the checksum of a pseudo header given an IPv6 header.
98 *
99 * @returns 32-bit intermediary checksum value.
100 * @param pIpHdr The IPv6 header (network endian (big)).
101 * @param bProtocol The protocol number. This can be the same as the
102 * ip6_nxt field, but doesn't need to be.
103 * @param cbPkt The packet size (host endian of course). This can
104 * be the same as the ip6_plen field, but as with @a
105 * bProtocol it won't be when extension headers are
106 * present. For UDP this will be uh_ulen converted to
107 * host endian.
108 */
109RTDECL(uint32_t) RTNetIPv6PseudoChecksumEx(PCRTNETIPV6 pIpHdr, uint8_t bProtocol, uint16_t cbPkt)
110{
111 return rtNetIPv6PseudoChecksumBits(&pIpHdr->ip6_src, &pIpHdr->ip6_dst, bProtocol, cbPkt);
112}
113RT_EXPORT_SYMBOL(RTNetIPv6PseudoChecksumEx);
114
115
116/**
117 * Calculates the checksum of a pseudo header given the individual components.
118 *
119 * @returns 32-bit intermediary checksum value.
120 * @param pSrcAddr Pointer to the source address in network endian.
121 * @param pDstAddr Pointer to the destination address in network endian.
122 * @param bProtocol The protocol number. This can be the same as the
123 * ip6_nxt field, but doesn't need to be.
124 * @param cbPkt The packet size (host endian of course). This can
125 * be the same as the ip6_plen field, but as with @a
126 * bProtocol it won't be when extension headers are
127 * present. For UDP this will be uh_ulen converted to
128 * host endian.
129 */
130RTDECL(uint32_t) RTNetIPv6PseudoChecksumBits(PCRTNETADDRIPV6 pSrcAddr, PCRTNETADDRIPV6 pDstAddr,
131 uint8_t bProtocol, uint16_t cbPkt)
132{
133 return rtNetIPv6PseudoChecksumBits(pSrcAddr, pDstAddr, bProtocol, cbPkt);
134}
135RT_EXPORT_SYMBOL(RTNetIPv6PseudoChecksumBits);
136
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use