in_cksum.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* in_cksum.c
  2. * 4.4-Lite-2 Internet checksum routine, modified to take a vector of
  3. * pointers/lengths giving the pieces to be checksummed. Also using
  4. * Tahoe/CGI version of ADDCARRY(x) macro instead of from portable version.
  5. */
  6. /*
  7. * Copyright (c) 1988, 1992, 1993
  8. * The Regents of the University of California. All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  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 University nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. *
  34. * @(#)in_cksum.c 8.1 (Berkeley) 6/10/93
  35. */
  36. #ifdef HAVE_CONFIG_H
  37. # include "config.h"
  38. #endif
  39. #include <netdissect-stdinc.h>
  40. #include "netdissect.h"
  41. /*
  42. * Checksum routine for Internet Protocol family headers (Portable Version).
  43. *
  44. * This routine is very heavily used in the network
  45. * code and should be modified for each CPU to be as fast as possible.
  46. */
  47. #define ADDCARRY(x) {if ((x) > 65535) (x) -= 65535;}
  48. #define REDUCE {l_util.l = sum; sum = l_util.s[0] + l_util.s[1]; ADDCARRY(sum);}
  49. uint16_t
  50. in_cksum(const struct cksum_vec *vec, int veclen)
  51. {
  52. register const uint16_t *w;
  53. register int sum = 0;
  54. register int mlen = 0;
  55. int byte_swapped = 0;
  56. union {
  57. uint8_t c[2];
  58. uint16_t s;
  59. } s_util;
  60. union {
  61. uint16_t s[2];
  62. uint32_t l;
  63. } l_util;
  64. for (; veclen != 0; vec++, veclen--) {
  65. if (vec->len == 0)
  66. continue;
  67. w = (const uint16_t *)(const void *)vec->ptr;
  68. if (mlen == -1) {
  69. /*
  70. * The first byte of this chunk is the continuation
  71. * of a word spanning between this chunk and the
  72. * last chunk.
  73. *
  74. * s_util.c[0] is already saved when scanning previous
  75. * chunk.
  76. */
  77. s_util.c[1] = *(const uint8_t *)w;
  78. sum += s_util.s;
  79. w = (const uint16_t *)(const void *)((const uint8_t *)w + 1);
  80. mlen = vec->len - 1;
  81. } else
  82. mlen = vec->len;
  83. /*
  84. * Force to even boundary.
  85. */
  86. if ((1 & (uintptr_t) w) && (mlen > 0)) {
  87. REDUCE;
  88. sum <<= 8;
  89. s_util.c[0] = *(const uint8_t *)w;
  90. w = (const uint16_t *)(const void *)((const uint8_t *)w + 1);
  91. mlen--;
  92. byte_swapped = 1;
  93. }
  94. /*
  95. * Unroll the loop to make overhead from
  96. * branches &c small.
  97. */
  98. while ((mlen -= 32) >= 0) {
  99. sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
  100. sum += w[4]; sum += w[5]; sum += w[6]; sum += w[7];
  101. sum += w[8]; sum += w[9]; sum += w[10]; sum += w[11];
  102. sum += w[12]; sum += w[13]; sum += w[14]; sum += w[15];
  103. w += 16;
  104. }
  105. mlen += 32;
  106. while ((mlen -= 8) >= 0) {
  107. sum += w[0]; sum += w[1]; sum += w[2]; sum += w[3];
  108. w += 4;
  109. }
  110. mlen += 8;
  111. if (mlen == 0 && byte_swapped == 0)
  112. continue;
  113. REDUCE;
  114. while ((mlen -= 2) >= 0) {
  115. sum += *w++;
  116. }
  117. if (byte_swapped) {
  118. REDUCE;
  119. sum <<= 8;
  120. byte_swapped = 0;
  121. if (mlen == -1) {
  122. s_util.c[1] = *(const uint8_t *)w;
  123. sum += s_util.s;
  124. mlen = 0;
  125. } else
  126. mlen = -1;
  127. } else if (mlen == -1)
  128. s_util.c[0] = *(const uint8_t *)w;
  129. }
  130. if (mlen == -1) {
  131. /* The last mbuf has odd # of bytes. Follow the
  132. standard (the odd byte may be shifted left by 8 bits
  133. or not as determined by endian-ness of the machine) */
  134. s_util.c[1] = 0;
  135. sum += s_util.s;
  136. }
  137. REDUCE;
  138. return (~sum & 0xffff);
  139. }
  140. /*
  141. * Given the host-byte-order value of the checksum field in a packet
  142. * header, and the network-byte-order computed checksum of the data
  143. * that the checksum covers (including the checksum itself), compute
  144. * what the checksum field *should* have been.
  145. */
  146. uint16_t
  147. in_cksum_shouldbe(uint16_t sum, uint16_t computed_sum)
  148. {
  149. uint32_t shouldbe;
  150. /*
  151. * The value that should have gone into the checksum field
  152. * is the negative of the value gotten by summing up everything
  153. * *but* the checksum field.
  154. *
  155. * We can compute that by subtracting the value of the checksum
  156. * field from the sum of all the data in the packet, and then
  157. * computing the negative of that value.
  158. *
  159. * "sum" is the value of the checksum field, and "computed_sum"
  160. * is the negative of the sum of all the data in the packets,
  161. * so that's -(-computed_sum - sum), or (sum + computed_sum).
  162. *
  163. * All the arithmetic in question is one's complement, so the
  164. * addition must include an end-around carry; we do this by
  165. * doing the arithmetic in 32 bits (with no sign-extension),
  166. * and then adding the upper 16 bits of the sum, which contain
  167. * the carry, to the lower 16 bits of the sum, and then do it
  168. * again in case *that* sum produced a carry.
  169. *
  170. * As RFC 1071 notes, the checksum can be computed without
  171. * byte-swapping the 16-bit words; summing 16-bit words
  172. * on a big-endian machine gives a big-endian checksum, which
  173. * can be directly stuffed into the big-endian checksum fields
  174. * in protocol headers, and summing words on a little-endian
  175. * machine gives a little-endian checksum, which must be
  176. * byte-swapped before being stuffed into a big-endian checksum
  177. * field.
  178. *
  179. * "computed_sum" is a network-byte-order value, so we must put
  180. * it in host byte order before subtracting it from the
  181. * host-byte-order value from the header; the adjusted checksum
  182. * will be in host byte order, which is what we'll return.
  183. */
  184. shouldbe = sum;
  185. shouldbe += ntohs(computed_sum);
  186. shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
  187. shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
  188. return shouldbe;
  189. }