ipcsum.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (C) 2012 by egnite GmbH
  3. * Copyright (C) 2001-2005 by egnite Software GmbH
  4. * Copyright (c) 1993 by Digital Equipment Corporation
  5. * Copyright (c) 1983, 1993 by The Regents of the University of California
  6. *
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. Neither the name of the copyright holders nor the names of
  19. * contributors may be used to endorse or promote products derived
  20. * from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  29. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  30. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  31. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  32. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33. * SUCH DAMAGE.
  34. *
  35. * For additional information see http://www.ethernut.de/
  36. */
  37. /*!
  38. * \file net/ipcsum.c
  39. * \brief IP checksum calculation.
  40. *
  41. * \verbatim
  42. * $Id: ipcsum.c 4937 2013-01-22 11:38:42Z haraldkipp $
  43. * \endverbatim
  44. */
  45. #define NUT_LEGACY_IPCSUM
  46. #include <netinet/ipcsum.h>
  47. /*!
  48. * \addtogroup xgIP
  49. *
  50. * \brief Internet Protocol checksum and related support.
  51. *
  52. */
  53. /*@{*/
  54. /*!
  55. * \brief Calculate a partial IP checksum of a buffer.
  56. *
  57. * The caller must create the one's complement of the final result.
  58. *
  59. * \param ics Initial checksum from previous parts.
  60. * \param buf Pointer to the buffer.
  61. * \param len Number of bytes in the buffer.
  62. *
  63. * \return Partial checksum in network byte order.
  64. */
  65. uint16_t NutIpChkSumPartial(uint16_t ics, const void *buf, int len)
  66. {
  67. register uint32_t sum = ics;
  68. register uint8_t *cp = (uint8_t *) buf;
  69. #ifdef NUT_LEGACY_IPCSUM
  70. /* Sum up 16 bit values. */
  71. while (len > 1) {
  72. #ifdef __BIG_ENDIAN__
  73. sum += ((uint16_t)*cp << 8) | *(cp + 1);
  74. #else
  75. sum += ((uint16_t)*(cp + 1) << 8) | *cp;
  76. #endif
  77. cp += 2;
  78. len -= 2;
  79. }
  80. #else /* NUT_LEGACY_IPCSUM */
  81. register uint32_t *wp;
  82. int step;
  83. /*
  84. * Sum up until the first 32-bit aligned value.
  85. */
  86. step = (uintptr_t) buf & 3;
  87. if (step > 1) {
  88. #ifdef __BIG_ENDIAN__
  89. sum += ((uint16_t)*cp << 8) | *(cp + 1);
  90. #else
  91. sum += ((uint16_t)*(cp + 1) << 8) | *cp;
  92. #endif
  93. cp += 2;
  94. step -= 2;
  95. }
  96. if (step) {
  97. #ifdef __BIG_ENDIAN__
  98. sum += (uint16_t)*cp << 8;
  99. #else
  100. sum += *cp;
  101. #endif
  102. cp++;
  103. }
  104. /*
  105. * Sum up all aligned 32 bit values.
  106. *
  107. * This significantly accelerates the calculation on 32 bit machines.
  108. * On 8 bit CPUs the advantage is partial loop unwinding.
  109. */
  110. for (wp = (uint32_t *) cp; len > 3; len -= 4) {
  111. /* Add value. */
  112. sum += *wp;
  113. /* Add carry. */
  114. sum += *wp++ > sum;
  115. }
  116. /*
  117. * Sum up the remaining 16-bit value, if any.
  118. */
  119. cp = (uint8_t *) wp;
  120. if (len > 1) {
  121. #ifdef __BIG_ENDIAN__
  122. sum += ((uint16_t)*cp << 8) | *(cp + 1);
  123. #else
  124. sum += ((uint16_t)*(cp + 1) << 8) | *cp;
  125. #endif
  126. cp += 2;
  127. len -= 2;
  128. }
  129. #endif /* NUT_LEGACY_IPCSUM */
  130. /* Add remaining byte on odd lengths. */
  131. if (len) {
  132. #ifdef __BIG_ENDIAN__
  133. sum += (uint16_t)*cp << 8;
  134. #else
  135. sum += *cp;
  136. #endif
  137. }
  138. /* Fold upper 16 bits to lower ones. */
  139. while (sum >> 16) {
  140. sum = (uint16_t)sum + (sum >> 16);
  141. }
  142. return (uint16_t) sum;
  143. }
  144. /*!
  145. * \brief Calculates an the final IP checksum over a block of data.
  146. *
  147. * Unlike the partial checksum in NutIpChkSumPartial(), this function takes
  148. * the one's complement of the final result, thus making it the full checksum.
  149. */
  150. uint16_t NutIpChkSum(uint16_t ics, const void *buf, int len)
  151. {
  152. return ~NutIpChkSumPartial(ics, buf, len);
  153. }
  154. /*
  155. * Details of the pseudo header used as part of the
  156. * calculation of UDP and TCP header checksums.
  157. */
  158. struct NUT_PACKED_TYPE pseudo_hdr {
  159. uint32_t ph_src_addr;
  160. uint32_t ph_dest_addr;
  161. uint8_t ph_zero;
  162. uint8_t ph_protocol;
  163. uint16_t ph_len;
  164. };
  165. /*!
  166. * \brief Calculates the partial IP pseudo checksum.
  167. *
  168. */
  169. uint32_t NutIpPseudoChkSumPartial(uint32_t src_addr, uint32_t dest_addr, uint8_t protocol, int len)
  170. {
  171. struct pseudo_hdr ph;
  172. ph.ph_src_addr = src_addr;
  173. ph.ph_dest_addr = dest_addr;
  174. ph.ph_zero = 0;
  175. ph.ph_protocol = protocol;
  176. ph.ph_len = len;
  177. return NutIpChkSumPartial(0, &ph, sizeof(struct pseudo_hdr));
  178. }
  179. /*@}*/