1 /* 2 * Copyright (c) 2020, The OpenThread Authors. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 3. Neither the name of the copyright holder nor the 13 * names of its contributors may be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 /** 30 * @file 31 * This file includes definitions for checksum calculation. 32 */ 33 34 #ifndef CHECKSUM_HPP_ 35 #define CHECKSUM_HPP_ 36 37 #include "openthread-core-config.h" 38 39 #include <stdint.h> 40 41 #include "common/message.hpp" 42 #include "net/ip4_types.hpp" 43 #include "net/ip6_address.hpp" 44 #include "net/ip6_headers.hpp" 45 #include "net/socket.hpp" 46 47 namespace ot { 48 49 /** 50 * Implements IP checksum calculation and verification. 51 */ 52 class Checksum 53 { 54 friend class ChecksumTester; 55 56 public: 57 /** 58 * Verifies the checksum in a given message (if UDP/ICMP6). 59 * 60 * @param[in] aMessage The message to verify its checksum. The `aMessage.GetOffset()` should point to start 61 * UDP/ICMP6 header. 62 * @param[in] aMessageInfo The message info associated with @p aMessage. 63 * @param[in] aIpProto The Internet Protocol value. 64 * 65 * @retval kErrorNone The checksum is valid if UDP/ICMP6 protocol, or not a UDP/ICMP6 protocol. 66 * @retval kErrorDrop The check is not valid and message should be dropped. 67 */ 68 static Error VerifyMessageChecksum(const Message &aMessage, const Ip6::MessageInfo &aMessageInfo, uint8_t aIpProto); 69 70 /** 71 * Calculates and then updates the checksum in a given message (if TCP/UDP/ICMPv6). 72 * 73 * @param[in,out] aMessage The message to update the checksum in. The `aMessage.GetOffset()` should point to start 74 * of the TCP/UDP/ICMPv6 header. On exit the checksum field in TCP/UDP/ICMPv6 header in the 75 * message is updated. 76 * @param[in] aSource The source address. 77 * @param[in] aDestination The destination address. 78 * @param[in] aIpProto The Internet Protocol value. 79 */ 80 static void UpdateMessageChecksum(Message &aMessage, 81 const Ip6::Address &aSource, 82 const Ip6::Address &aDestination, 83 uint8_t aIpProto); 84 85 /** 86 * Calculates and then updates the checksum in a given IPv4 message (if TCP/UDP/ICMP(v4)). 87 * 88 * @param[in,out] aMessage The message to update the checksum in. The `aMessage.GetOffset()` should point to start 89 * of the TCP/UDP/ICMP(v4) header. On exit the checksum field in TCP/UDP/ICMP(v4) header in 90 * the message is updated. 91 * @param[in] aSource The source address. 92 * @param[in] aDestination The destination address. 93 * @param[in] aIpProto The Internet Protocol value. 94 */ 95 static void UpdateMessageChecksum(Message &aMessage, 96 const Ip4::Address &aSource, 97 const Ip4::Address &aDestination, 98 uint8_t aIpProto); 99 100 /** 101 * Calculates and then updates the checksum field in the IPv4 header. 102 * 103 * @param[in,out] aHeader The IPv4 header to update the checksum in. 104 */ 105 static void UpdateIp4HeaderChecksum(Ip4::Header &aHeader); 106 107 private: Checksum(void)108 Checksum(void) 109 : mValue(0) 110 , mAtOddIndex(false) 111 { 112 } 113 GetValue(void) const114 uint16_t GetValue(void) const { return mValue; } 115 void AddUint8(uint8_t aUint8); 116 void AddUint16(uint16_t aUint16); 117 void AddData(const uint8_t *aBuffer, uint16_t aLength); 118 void WriteToMessage(uint16_t aOffset, Message &aMessage) const; 119 void Calculate(const Ip6::Address &aSource, 120 const Ip6::Address &aDestination, 121 uint8_t aIpProto, 122 const Message &aMessage); 123 void Calculate(const Ip4::Address &aSource, 124 const Ip4::Address &aDestination, 125 uint8_t aIpProto, 126 const Message &aMessage); 127 128 static constexpr uint16_t kValidRxChecksum = 0xffff; 129 130 uint16_t mValue; 131 bool mAtOddIndex; 132 }; 133 134 } // namespace ot 135 136 #endif // CHECKSUM_HPP_ 137