c-checksum.c 5.0 KB

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