checksum.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * IP/TCP/UDP checksumming routines
  7. *
  8. * Authors: Jorge Cwik, <jorge@laser.satlink.net>
  9. * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
  10. * Tom May, <ftom@netcom.com>
  11. * Andreas Schwab, <schwab@issan.informatik.uni-dortmund.de>
  12. * Lots of code moved from tcp.c and ip.c; see those files
  13. * for more names.
  14. *
  15. * 03/02/96 Jes Sorensen, Andreas Schwab, Roman Hodek:
  16. * Fixed some nasty bugs, causing some horrible crashes.
  17. * A: At some points, the sum (%0) was used as
  18. * length-counter instead of the length counter
  19. * (%1). Thanks to Roman Hodek for pointing this out.
  20. * B: GCC seems to mess up if one uses too many
  21. * data-registers to hold input values and one tries to
  22. * specify d0 and d1 as scratch registers. Letting gcc choose these
  23. * registers itself solves the problem.
  24. *
  25. * This program is free software; you can redistribute it and/or
  26. * modify it under the terms of the GNU General Public License
  27. * as published by the Free Software Foundation; either version
  28. * 2 of the License, or (at your option) any later version.
  29. */
  30. /* Revised by Kenneth Albanowski for m68knommu. Basic problem: unaligned access kills, so most
  31. of the assembly has to go. */
  32. #include <net/checksum.h>
  33. #include <asm/checksum.h>
  34. #include <linux/module.h>
  35. static inline unsigned short from32to16(unsigned long x)
  36. {
  37. /* add up 16-bit and 16-bit for 16+c bit */
  38. x = (x & 0xffff) + (x >> 16);
  39. /* add up carry.. */
  40. x = (x & 0xffff) + (x >> 16);
  41. return x;
  42. }
  43. static unsigned long do_csum(const unsigned char * buff, int len)
  44. {
  45. int odd, count;
  46. unsigned long result = 0;
  47. if (len <= 0)
  48. goto out;
  49. odd = 1 & (unsigned long) buff;
  50. if (odd) {
  51. result = *buff;
  52. len--;
  53. buff++;
  54. }
  55. count = len >> 1; /* nr of 16-bit words.. */
  56. if (count) {
  57. if (2 & (unsigned long) buff) {
  58. result += *(unsigned short *) buff;
  59. count--;
  60. len -= 2;
  61. buff += 2;
  62. }
  63. count >>= 1; /* nr of 32-bit words.. */
  64. if (count) {
  65. unsigned long carry = 0;
  66. do {
  67. unsigned long w = *(unsigned long *) buff;
  68. count--;
  69. buff += 4;
  70. result += carry;
  71. result += w;
  72. carry = (w > result);
  73. } while (count);
  74. result += carry;
  75. result = (result & 0xffff) + (result >> 16);
  76. }
  77. if (len & 2) {
  78. result += *(unsigned short *) buff;
  79. buff += 2;
  80. }
  81. }
  82. if (len & 1)
  83. result += (*buff << 8);
  84. result = from32to16(result);
  85. if (odd)
  86. result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
  87. out:
  88. return result;
  89. }
  90. /*
  91. * computes the checksum of a memory block at buff, length len,
  92. * and adds in "sum" (32-bit)
  93. *
  94. * returns a 32-bit number suitable for feeding into itself
  95. * or csum_tcpudp_magic
  96. *
  97. * this function must be called with even lengths, except
  98. * for the last fragment, which may be odd
  99. *
  100. * it's best to have buff aligned on a 32-bit boundary
  101. */
  102. unsigned int csum_partial(const unsigned char * buff, int len, unsigned int sum)
  103. {
  104. unsigned int result = do_csum(buff, len);
  105. /* add in old sum, and carry.. */
  106. result += sum;
  107. if (sum > result)
  108. result += 1;
  109. return result;
  110. }
  111. EXPORT_SYMBOL(csum_partial);
  112. /*
  113. * this routine is used for miscellaneous IP-like checksums, mainly
  114. * in icmp.c
  115. */
  116. unsigned short ip_compute_csum(const unsigned char * buff, int len)
  117. {
  118. return ~do_csum(buff, len);
  119. }
  120. EXPORT_SYMBOL(ip_compute_csum);
  121. /*
  122. * copy from fs while checksumming, otherwise like csum_partial
  123. */
  124. unsigned int
  125. csum_partial_copy_from_user(const char __user *src, char *dst,
  126. int len, int sum, int *csum_err)
  127. {
  128. int rem;
  129. if (csum_err)
  130. *csum_err = 0;
  131. rem = copy_from_user(dst, src, len);
  132. if (rem != 0) {
  133. if (csum_err)
  134. *csum_err = -EFAULT;
  135. memset(dst + len - rem, 0, rem);
  136. len = rem;
  137. }
  138. return csum_partial(dst, len, sum);
  139. }
  140. EXPORT_SYMBOL(csum_partial_copy_from_user);
  141. /*
  142. * copy from ds while checksumming, otherwise like csum_partial
  143. */
  144. unsigned int
  145. csum_partial_copy(const char *src, char *dst, int len, int sum)
  146. {
  147. memcpy(dst, src, len);
  148. return csum_partial(dst, len, sum);
  149. }
  150. EXPORT_SYMBOL(csum_partial_copy);