checksum.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * arch/alpha/lib/checksum.c
  3. *
  4. * This file contains network checksum routines that are better done
  5. * in an architecture-specific manner due to speed..
  6. * Comments in other versions indicate that the algorithms are from RFC1071
  7. *
  8. * accelerated versions (and 21264 assembly versions ) contributed by
  9. * Rick Gorton <rick.gorton@alpha-processor.com>
  10. */
  11. #include <linux/module.h>
  12. #include <linux/string.h>
  13. #include <asm/byteorder.h>
  14. static inline unsigned short from64to16(unsigned long x)
  15. {
  16. /* Using extract instructions is a bit more efficient
  17. than the original shift/bitmask version. */
  18. union {
  19. unsigned long ul;
  20. unsigned int ui[2];
  21. unsigned short us[4];
  22. } in_v, tmp_v, out_v;
  23. in_v.ul = x;
  24. tmp_v.ul = (unsigned long) in_v.ui[0] + (unsigned long) in_v.ui[1];
  25. /* Since the bits of tmp_v.sh[3] are going to always be zero,
  26. we don't have to bother to add that in. */
  27. out_v.ul = (unsigned long) tmp_v.us[0] + (unsigned long) tmp_v.us[1]
  28. + (unsigned long) tmp_v.us[2];
  29. /* Similarly, out_v.us[2] is always zero for the final add. */
  30. return out_v.us[0] + out_v.us[1];
  31. }
  32. /*
  33. * computes the checksum of the TCP/UDP pseudo-header
  34. * returns a 16-bit checksum, already complemented.
  35. */
  36. __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
  37. unsigned short len,
  38. unsigned short proto,
  39. __wsum sum)
  40. {
  41. return (__force __sum16)~from64to16(
  42. (__force u64)saddr + (__force u64)daddr +
  43. (__force u64)sum + ((len + proto) << 8));
  44. }
  45. __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
  46. unsigned short len,
  47. unsigned short proto,
  48. __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. /*
  138. * computes the checksum of a memory block at buff, length len,
  139. * and adds in "sum" (32-bit)
  140. *
  141. * returns a 32-bit number suitable for feeding into itself
  142. * or csum_tcpudp_magic
  143. *
  144. * this function must be called with even lengths, except
  145. * for the last fragment, which may be odd
  146. *
  147. * it's best to have buff aligned on a 32-bit boundary
  148. */
  149. __wsum csum_partial(const void *buff, int len, __wsum sum)
  150. {
  151. unsigned long result = do_csum(buff, len);
  152. /* add in old sum, and carry.. */
  153. result += (__force u32)sum;
  154. /* 32+c bits -> 32 bits */
  155. result = (result & 0xffffffff) + (result >> 32);
  156. return (__force __wsum)result;
  157. }
  158. EXPORT_SYMBOL(csum_partial);
  159. /*
  160. * this routine is used for miscellaneous IP-like checksums, mainly
  161. * in icmp.c
  162. */
  163. __sum16 ip_compute_csum(const void *buff, int len)
  164. {
  165. return (__force __sum16)~from64to16(do_csum(buff,len));
  166. }