checksum.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 unsigned int do_csum(const unsigned char *buff, int len)
  34. {
  35. int odd;
  36. unsigned int result = 0;
  37. if (len <= 0)
  38. goto out;
  39. odd = 1 & (unsigned long) buff;
  40. if (odd) {
  41. #ifdef __LITTLE_ENDIAN
  42. result += (*buff << 8);
  43. #else
  44. result = *buff;
  45. #endif
  46. len--;
  47. buff++;
  48. }
  49. if (len >= 2) {
  50. if (2 & (unsigned long) buff) {
  51. result += *(unsigned short *) buff;
  52. len -= 2;
  53. buff += 2;
  54. }
  55. if (len >= 4) {
  56. const unsigned char *end = buff + ((unsigned)len & ~3);
  57. unsigned int carry = 0;
  58. do {
  59. unsigned int w = *(unsigned int *) buff;
  60. buff += 4;
  61. result += carry;
  62. result += w;
  63. carry = (w > result);
  64. } while (buff < end);
  65. result += carry;
  66. result = (result & 0xffff) + (result >> 16);
  67. }
  68. if (len & 2) {
  69. result += *(unsigned short *) buff;
  70. buff += 2;
  71. }
  72. }
  73. if (len & 1)
  74. #ifdef __LITTLE_ENDIAN
  75. result += *buff;
  76. #else
  77. result += (*buff << 8);
  78. #endif
  79. result = csum_from32to16(result);
  80. if (odd)
  81. result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
  82. out:
  83. return result;
  84. }
  85. #endif
  86. #ifndef ip_fast_csum
  87. /*
  88. * This is a version of ip_compute_csum() optimized for IP headers,
  89. * which always checksum on 4 octet boundaries.
  90. */
  91. __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
  92. {
  93. return (__force __sum16)~do_csum(iph, ihl*4);
  94. }
  95. EXPORT_SYMBOL(ip_fast_csum);
  96. #endif
  97. /*
  98. * computes the checksum of a memory block at buff, length len,
  99. * and adds in "sum" (32-bit)
  100. *
  101. * returns a 32-bit number suitable for feeding into itself
  102. * or csum_tcpudp_magic
  103. *
  104. * this function must be called with even lengths, except
  105. * for the last fragment, which may be odd
  106. *
  107. * it's best to have buff aligned on a 32-bit boundary
  108. */
  109. __wsum csum_partial(const void *buff, int len, __wsum wsum)
  110. {
  111. unsigned int sum = (__force unsigned int)wsum;
  112. unsigned int result = do_csum(buff, len);
  113. /* add in old sum, and carry.. */
  114. result += sum;
  115. if (sum > result)
  116. result += 1;
  117. return (__force __wsum)result;
  118. }
  119. EXPORT_SYMBOL(csum_partial);
  120. /*
  121. * this routine is used for miscellaneous IP-like checksums, mainly
  122. * in icmp.c
  123. */
  124. __sum16 ip_compute_csum(const void *buff, int len)
  125. {
  126. return (__force __sum16)~do_csum(buff, len);
  127. }
  128. EXPORT_SYMBOL(ip_compute_csum);
  129. #ifndef csum_tcpudp_nofold
  130. static inline u32 from64to32(u64 x)
  131. {
  132. /* add up 32-bit and 32-bit for 32+c bit */
  133. x = (x & 0xffffffff) + (x >> 32);
  134. /* add up carry.. */
  135. x = (x & 0xffffffff) + (x >> 32);
  136. return (u32)x;
  137. }
  138. __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
  139. __u32 len, __u8 proto, __wsum sum)
  140. {
  141. unsigned long long s = (__force u32)sum;
  142. s += (__force u32)saddr;
  143. s += (__force u32)daddr;
  144. #ifdef __BIG_ENDIAN
  145. s += proto + len;
  146. #else
  147. s += (proto + len) << 8;
  148. #endif
  149. return (__force __wsum)from64to32(s);
  150. }
  151. EXPORT_SYMBOL(csum_tcpudp_nofold);
  152. #endif