c-checksum.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * arch/sh/lib/csum_parial.c
  3. *
  4. * This file contains network checksum routines that are better done
  5. * in an architecture-specific manner due to speed..
  6. */
  7. #undef DEBUG
  8. #include <linux/config.h>
  9. #include <linux/string.h>
  10. #include <linux/errno.h>
  11. #include <linux/kernel.h>
  12. #include <asm/byteorder.h>
  13. #include <asm/uaccess.h>
  14. static inline unsigned short from64to16(unsigned long long x)
  15. {
  16. /* add up 32-bit words for 33 bits */
  17. x = (x & 0xffffffff) + (x >> 32);
  18. /* add up 16-bit and 17-bit words for 17+c bits */
  19. x = (x & 0xffff) + (x >> 16);
  20. /* add up 16-bit and 2-bit for 16+c bit */
  21. x = (x & 0xffff) + (x >> 16);
  22. /* add up carry.. */
  23. x = (x & 0xffff) + (x >> 16);
  24. return x;
  25. }
  26. static inline unsigned short foldto16(unsigned long x)
  27. {
  28. /* add up 16-bit for 17 bits */
  29. x = (x & 0xffff) + (x >> 16);
  30. /* add up carry.. */
  31. x = (x & 0xffff) + (x >> 16);
  32. return x;
  33. }
  34. static inline unsigned short myfoldto16(unsigned long long x)
  35. {
  36. /* Fold down to 32-bits so we don't loose in the typedef-less
  37. network stack. */
  38. /* 64 to 33 */
  39. x = (x & 0xffffffff) + (x >> 32);
  40. /* 33 to 32 */
  41. x = (x & 0xffffffff) + (x >> 32);
  42. /* add up 16-bit for 17 bits */
  43. x = (x & 0xffff) + (x >> 16);
  44. /* add up carry.. */
  45. x = (x & 0xffff) + (x >> 16);
  46. return x;
  47. }
  48. #define odd(x) ((x)&1)
  49. #define U16(x) ntohs(x)
  50. static unsigned long do_csum(const unsigned char *buff, int len)
  51. {
  52. int odd, count;
  53. unsigned long result = 0;
  54. pr_debug("do_csum buff %p, len %d (0x%x)\n", buff, len, len);
  55. #ifdef DEBUG
  56. for (i = 0; i < len; i++) {
  57. if ((i % 26) == 0)
  58. printk("\n");
  59. printk("%02X ", buff[i]);
  60. }
  61. #endif
  62. if (len <= 0)
  63. goto out;
  64. odd = 1 & (unsigned long) buff;
  65. if (odd) {
  66. result = *buff << 8;
  67. len--;
  68. buff++;
  69. }
  70. count = len >> 1; /* nr of 16-bit words.. */
  71. if (count) {
  72. if (2 & (unsigned long) buff) {
  73. result += *(unsigned short *) buff;
  74. count--;
  75. len -= 2;
  76. buff += 2;
  77. }
  78. count >>= 1; /* nr of 32-bit words.. */
  79. if (count) {
  80. unsigned long carry = 0;
  81. do {
  82. unsigned long w = *(unsigned long *) buff;
  83. buff += 4;
  84. count--;
  85. result += carry;
  86. result += w;
  87. carry = (w > result);
  88. } while (count);
  89. result += carry;
  90. result = (result & 0xffff) + (result >> 16);
  91. }
  92. if (len & 2) {
  93. result += *(unsigned short *) buff;
  94. buff += 2;
  95. }
  96. }
  97. if (len & 1)
  98. result += *buff;
  99. result = foldto16(result);
  100. if (odd)
  101. result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
  102. pr_debug("\nCHECKSUM is 0x%x\n", result);
  103. out:
  104. return result;
  105. }
  106. /* computes the checksum of a memory block at buff, length len,
  107. and adds in "sum" (32-bit) */
  108. unsigned int csum_partial(const unsigned char *buff, int len, unsigned int sum)
  109. {
  110. unsigned long long result = do_csum(buff, len);
  111. /* add in old sum, and carry.. */
  112. result += sum;
  113. /* 32+c bits -> 32 bits */
  114. result = (result & 0xffffffff) + (result >> 32);
  115. pr_debug("csum_partial, buff %p len %d sum 0x%x result=0x%016Lx\n",
  116. buff, len, sum, result);
  117. return result;
  118. }
  119. /* Copy while checksumming, otherwise like csum_partial. */
  120. unsigned int
  121. csum_partial_copy(const unsigned char *src, unsigned char *dst, int len, unsigned int sum)
  122. {
  123. sum = csum_partial(src, len, sum);
  124. memcpy(dst, src, len);
  125. return sum;
  126. }
  127. /* Copy from userspace and compute checksum. If we catch an exception
  128. then zero the rest of the buffer. */
  129. unsigned int
  130. csum_partial_copy_from_user(const unsigned char *src, unsigned char *dst, int len,
  131. unsigned int sum, int *err_ptr)
  132. {
  133. int missing;
  134. pr_debug
  135. ("csum_partial_copy_from_user src %p, dest %p, len %d, sum %08x, err_ptr %p\n",
  136. src, dst, len, sum, err_ptr);
  137. missing = copy_from_user(dst, src, len);
  138. pr_debug(" access_ok %d\n", __access_ok((unsigned long) src, len));
  139. pr_debug(" missing %d\n", missing);
  140. if (missing) {
  141. memset(dst + len - missing, 0, missing);
  142. *err_ptr = -EFAULT;
  143. }
  144. return csum_partial(dst, len, sum);
  145. }
  146. /* Copy to userspace and compute checksum. */
  147. unsigned int
  148. csum_partial_copy_to_user(const unsigned char *src, unsigned char *dst, int len,
  149. unsigned int sum, int *err_ptr)
  150. {
  151. sum = csum_partial(src, len, sum);
  152. if (copy_to_user(dst, src, len))
  153. *err_ptr = -EFAULT;
  154. return sum;
  155. }
  156. /*
  157. * This is a version of ip_compute_csum() optimized for IP headers,
  158. * which always checksum on 4 octet boundaries.
  159. */
  160. unsigned short ip_fast_csum(unsigned char *iph, unsigned int ihl)
  161. {
  162. pr_debug("ip_fast_csum %p,%d\n", iph, ihl);
  163. return ~do_csum(iph, ihl * 4);
  164. }
  165. unsigned int csum_tcpudp_nofold(unsigned long saddr,
  166. unsigned long daddr,
  167. unsigned short len,
  168. unsigned short proto, unsigned int sum)
  169. {
  170. unsigned long long result;
  171. pr_debug("ntohs(0x%x)=0x%x\n", 0xdead, ntohs(0xdead));
  172. pr_debug("htons(0x%x)=0x%x\n", 0xdead, htons(0xdead));
  173. result = ((unsigned long long) saddr +
  174. (unsigned long long) daddr +
  175. (unsigned long long) sum +
  176. ((unsigned long long) ntohs(len) << 16) +
  177. ((unsigned long long) proto << 8));
  178. /* Fold down to 32-bits so we don't loose in the typedef-less
  179. network stack. */
  180. /* 64 to 33 */
  181. result = (result & 0xffffffff) + (result >> 32);
  182. /* 33 to 32 */
  183. result = (result & 0xffffffff) + (result >> 32);
  184. pr_debug("%s saddr %x daddr %x len %x proto %x sum %x result %08Lx\n",
  185. __FUNCTION__, saddr, daddr, len, proto, sum, result);
  186. return result;
  187. }
  188. // Post SIM:
  189. unsigned int
  190. csum_partial_copy_nocheck(const unsigned char *src, unsigned char *dst, int len, unsigned int sum)
  191. {
  192. // unsigned dummy;
  193. pr_debug("csum_partial_copy_nocheck src %p dst %p len %d\n", src, dst,
  194. len);
  195. return csum_partial_copy(src, dst, len, sum);
  196. }