checksum.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. * accellerated 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. unsigned short int csum_tcpudp_magic(unsigned long saddr,
  37. unsigned long daddr,
  38. unsigned short len,
  39. unsigned short proto,
  40. unsigned int sum)
  41. {
  42. return ~from64to16(saddr + daddr + sum +
  43. ((unsigned long) ntohs(len) << 16) +
  44. ((unsigned long) proto << 8));
  45. }
  46. unsigned int csum_tcpudp_nofold(unsigned long saddr,
  47. unsigned long daddr,
  48. unsigned short len,
  49. unsigned short proto,
  50. unsigned int sum)
  51. {
  52. unsigned long result;
  53. result = (saddr + daddr + sum +
  54. ((unsigned long) ntohs(len) << 16) +
  55. ((unsigned long) proto << 8));
  56. /* Fold down to 32-bits so we don't lose in the typedef-less
  57. network stack. */
  58. /* 64 to 33 */
  59. result = (result & 0xffffffff) + (result >> 32);
  60. /* 33 to 32 */
  61. result = (result & 0xffffffff) + (result >> 32);
  62. return result;
  63. }
  64. /*
  65. * Do a 64-bit checksum on an arbitrary memory area..
  66. *
  67. * This isn't a great routine, but it's not _horrible_ either. The
  68. * inner loop could be unrolled a bit further, and there are better
  69. * ways to do the carry, but this is reasonable.
  70. */
  71. static inline unsigned long do_csum(const unsigned char * buff, int len)
  72. {
  73. int odd, count;
  74. unsigned long result = 0;
  75. if (len <= 0)
  76. goto out;
  77. odd = 1 & (unsigned long) buff;
  78. if (odd) {
  79. result = *buff << 8;
  80. len--;
  81. buff++;
  82. }
  83. count = len >> 1; /* nr of 16-bit words.. */
  84. if (count) {
  85. if (2 & (unsigned long) buff) {
  86. result += *(unsigned short *) buff;
  87. count--;
  88. len -= 2;
  89. buff += 2;
  90. }
  91. count >>= 1; /* nr of 32-bit words.. */
  92. if (count) {
  93. if (4 & (unsigned long) buff) {
  94. result += *(unsigned int *) buff;
  95. count--;
  96. len -= 4;
  97. buff += 4;
  98. }
  99. count >>= 1; /* nr of 64-bit words.. */
  100. if (count) {
  101. unsigned long carry = 0;
  102. do {
  103. unsigned long w = *(unsigned long *) buff;
  104. count--;
  105. buff += 8;
  106. result += carry;
  107. result += w;
  108. carry = (w > result);
  109. } while (count);
  110. result += carry;
  111. result = (result & 0xffffffff) + (result >> 32);
  112. }
  113. if (len & 4) {
  114. result += *(unsigned int *) buff;
  115. buff += 4;
  116. }
  117. }
  118. if (len & 2) {
  119. result += *(unsigned short *) buff;
  120. buff += 2;
  121. }
  122. }
  123. if (len & 1)
  124. result += *buff;
  125. result = from64to16(result);
  126. if (odd)
  127. result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
  128. out:
  129. return result;
  130. }
  131. /*
  132. * This is a version of ip_compute_csum() optimized for IP headers,
  133. * which always checksum on 4 octet boundaries.
  134. */
  135. unsigned short ip_fast_csum(unsigned char * iph, unsigned int ihl)
  136. {
  137. return ~do_csum(iph,ihl*4);
  138. }
  139. /*
  140. * computes the checksum of a memory block at buff, length len,
  141. * and adds in "sum" (32-bit)
  142. *
  143. * returns a 32-bit number suitable for feeding into itself
  144. * or csum_tcpudp_magic
  145. *
  146. * this function must be called with even lengths, except
  147. * for the last fragment, which may be odd
  148. *
  149. * it's best to have buff aligned on a 32-bit boundary
  150. */
  151. unsigned int csum_partial(const unsigned char * buff, int len, unsigned int sum)
  152. {
  153. unsigned long result = do_csum(buff, len);
  154. /* add in old sum, and carry.. */
  155. result += sum;
  156. /* 32+c bits -> 32 bits */
  157. result = (result & 0xffffffff) + (result >> 32);
  158. return result;
  159. }
  160. EXPORT_SYMBOL(csum_partial);
  161. /*
  162. * this routine is used for miscellaneous IP-like checksums, mainly
  163. * in icmp.c
  164. */
  165. unsigned short ip_compute_csum(unsigned char * buff, int len)
  166. {
  167. return ~from64to16(do_csum(buff,len));
  168. }