checksum.c 4.6 KB

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