checksum.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * arch/alpha/lib/checksum.c
  4. *
  5. * This file contains network checksum routines that are better done
  6. * in an architecture-specific manner due to speed..
  7. * Comments in other versions indicate that the algorithms are from RFC1071
  8. *
  9. * accelerated versions (and 21264 assembly versions ) contributed by
  10. * Rick Gorton <[email protected]>
  11. */
  12. #include <linux/module.h>
  13. #include <linux/string.h>
  14. #include <asm/byteorder.h>
  15. static inline unsigned short from64to16(unsigned long x)
  16. {
  17. /* Using extract instructions is a bit more efficient
  18. than the original shift/bitmask version. */
  19. union {
  20. unsigned long ul;
  21. unsigned int ui[2];
  22. unsigned short us[4];
  23. } in_v, tmp_v, out_v;
  24. in_v.ul = x;
  25. tmp_v.ul = (unsigned long) in_v.ui[0] + (unsigned long) in_v.ui[1];
  26. /* Since the bits of tmp_v.sh[3] are going to always be zero,
  27. we don't have to bother to add that in. */
  28. out_v.ul = (unsigned long) tmp_v.us[0] + (unsigned long) tmp_v.us[1]
  29. + (unsigned long) tmp_v.us[2];
  30. /* Similarly, out_v.us[2] is always zero for the final add. */
  31. return out_v.us[0] + out_v.us[1];
  32. }
  33. /*
  34. * computes the checksum of the TCP/UDP pseudo-header
  35. * returns a 16-bit checksum, already complemented.
  36. */
  37. __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
  38. __u32 len, __u8 proto, __wsum sum)
  39. {
  40. return (__force __sum16)~from64to16(
  41. (__force u64)saddr + (__force u64)daddr +
  42. (__force u64)sum + ((len + proto) << 8));
  43. }
  44. EXPORT_SYMBOL(csum_tcpudp_magic);
  45. __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
  46. __u32 len, __u8 proto, __wsum sum)
  47. {
  48. unsigned long result;
  49. result = (__force u64)saddr + (__force u64)daddr +
  50. (__force u64)sum + ((len + proto) << 8);
  51. /* Fold down to 32-bits so we don't lose in the typedef-less
  52. network stack. */
  53. /* 64 to 33 */
  54. result = (result & 0xffffffff) + (result >> 32);
  55. /* 33 to 32 */
  56. result = (result & 0xffffffff) + (result >> 32);
  57. return (__force __wsum)result;
  58. }
  59. EXPORT_SYMBOL(csum_tcpudp_nofold);
  60. /*
  61. * Do a 64-bit checksum on an arbitrary memory area..
  62. *
  63. * This isn't a great routine, but it's not _horrible_ either. The
  64. * inner loop could be unrolled a bit further, and there are better
  65. * ways to do the carry, but this is reasonable.
  66. */
  67. static inline unsigned long do_csum(const unsigned char * buff, int len)
  68. {
  69. int odd, count;
  70. unsigned long result = 0;
  71. if (len <= 0)
  72. goto out;
  73. odd = 1 & (unsigned long) buff;
  74. if (odd) {
  75. result = *buff << 8;
  76. len--;
  77. buff++;
  78. }
  79. count = len >> 1; /* nr of 16-bit words.. */
  80. if (count) {
  81. if (2 & (unsigned long) buff) {
  82. result += *(unsigned short *) buff;
  83. count--;
  84. len -= 2;
  85. buff += 2;
  86. }
  87. count >>= 1; /* nr of 32-bit words.. */
  88. if (count) {
  89. if (4 & (unsigned long) buff) {
  90. result += *(unsigned int *) buff;
  91. count--;
  92. len -= 4;
  93. buff += 4;
  94. }
  95. count >>= 1; /* nr of 64-bit words.. */
  96. if (count) {
  97. unsigned long carry = 0;
  98. do {
  99. unsigned long w = *(unsigned long *) buff;
  100. count--;
  101. buff += 8;
  102. result += carry;
  103. result += w;
  104. carry = (w > result);
  105. } while (count);
  106. result += carry;
  107. result = (result & 0xffffffff) + (result >> 32);
  108. }
  109. if (len & 4) {
  110. result += *(unsigned int *) buff;
  111. buff += 4;
  112. }
  113. }
  114. if (len & 2) {
  115. result += *(unsigned short *) buff;
  116. buff += 2;
  117. }
  118. }
  119. if (len & 1)
  120. result += *buff;
  121. result = from64to16(result);
  122. if (odd)
  123. result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
  124. out:
  125. return result;
  126. }
  127. /*
  128. * This is a version of ip_compute_csum() optimized for IP headers,
  129. * which always checksum on 4 octet boundaries.
  130. */
  131. __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
  132. {
  133. return (__force __sum16)~do_csum(iph,ihl*4);
  134. }
  135. EXPORT_SYMBOL(ip_fast_csum);
  136. /*
  137. * computes the checksum of a memory block at buff, length len,
  138. * and adds in "sum" (32-bit)
  139. *
  140. * returns a 32-bit number suitable for feeding into itself
  141. * or csum_tcpudp_magic
  142. *
  143. * this function must be called with even lengths, except
  144. * for the last fragment, which may be odd
  145. *
  146. * it's best to have buff aligned on a 32-bit boundary
  147. */
  148. __wsum csum_partial(const void *buff, int len, __wsum sum)
  149. {
  150. unsigned long result = do_csum(buff, len);
  151. /* add in old sum, and carry.. */
  152. result += (__force u32)sum;
  153. /* 32+c bits -> 32 bits */
  154. result = (result & 0xffffffff) + (result >> 32);
  155. return (__force __wsum)result;
  156. }
  157. EXPORT_SYMBOL(csum_partial);
  158. /*
  159. * this routine is used for miscellaneous IP-like checksums, mainly
  160. * in icmp.c
  161. */
  162. __sum16 ip_compute_csum(const void *buff, int len)
  163. {
  164. return (__force __sum16)~from64to16(do_csum(buff,len));
  165. }
  166. EXPORT_SYMBOL(ip_compute_csum);