checksum.c 4.1 KB

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