checksum.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 1995, 96, 97, 98, 99, 2001 by Ralf Baechle
  7. * Copyright (C) 1999 Silicon Graphics, Inc.
  8. * Copyright (C) 2001 Thiemo Seufer.
  9. * Copyright (C) 2002 Maciej W. Rozycki
  10. */
  11. #ifndef _ASM_CHECKSUM_H
  12. #define _ASM_CHECKSUM_H
  13. #include <linux/config.h>
  14. #include <linux/in6.h>
  15. #include <asm/uaccess.h>
  16. /*
  17. * computes the checksum of a memory block at buff, length len,
  18. * and adds in "sum" (32-bit)
  19. *
  20. * returns a 32-bit number suitable for feeding into itself
  21. * or csum_tcpudp_magic
  22. *
  23. * this function must be called with even lengths, except
  24. * for the last fragment, which may be odd
  25. *
  26. * it's best to have buff aligned on a 32-bit boundary
  27. */
  28. unsigned int csum_partial(const unsigned char *buff, int len, unsigned int sum);
  29. /*
  30. * this is a new version of the above that records errors it finds in *errp,
  31. * but continues and zeros the rest of the buffer.
  32. */
  33. unsigned int csum_partial_copy_from_user(const unsigned char *src, unsigned char *dst, int len,
  34. unsigned int sum, int *errp);
  35. /*
  36. * Copy and checksum to user
  37. */
  38. #define HAVE_CSUM_COPY_USER
  39. static inline unsigned int csum_and_copy_to_user (const unsigned char *src,
  40. unsigned char __user *dst,
  41. int len, int sum,
  42. int *err_ptr)
  43. {
  44. might_sleep();
  45. sum = csum_partial(src, len, sum);
  46. if (copy_to_user(dst, src, len)) {
  47. *err_ptr = -EFAULT;
  48. return -1;
  49. }
  50. return sum;
  51. }
  52. /*
  53. * the same as csum_partial, but copies from user space (but on MIPS
  54. * we have just one address space, so this is identical to the above)
  55. */
  56. unsigned int csum_partial_copy_nocheck(const unsigned char *src, unsigned char *dst,
  57. int len, unsigned int sum);
  58. /*
  59. * Fold a partial checksum without adding pseudo headers
  60. */
  61. static inline unsigned short int csum_fold(unsigned int sum)
  62. {
  63. __asm__(
  64. ".set\tnoat\t\t\t# csum_fold\n\t"
  65. "sll\t$1,%0,16\n\t"
  66. "addu\t%0,$1\n\t"
  67. "sltu\t$1,%0,$1\n\t"
  68. "srl\t%0,%0,16\n\t"
  69. "addu\t%0,$1\n\t"
  70. "xori\t%0,0xffff\n\t"
  71. ".set\tat"
  72. : "=r" (sum)
  73. : "0" (sum));
  74. return sum;
  75. }
  76. /*
  77. * This is a version of ip_compute_csum() optimized for IP headers,
  78. * which always checksum on 4 octet boundaries.
  79. *
  80. * By Jorge Cwik <jorge@laser.satlink.net>, adapted for linux by
  81. * Arnt Gulbrandsen.
  82. */
  83. static inline unsigned short ip_fast_csum(unsigned char *iph, unsigned int ihl)
  84. {
  85. unsigned int *word = (unsigned int *) iph;
  86. unsigned int *stop = word + ihl;
  87. unsigned int csum;
  88. int carry;
  89. csum = word[0];
  90. csum += word[1];
  91. carry = (csum < word[1]);
  92. csum += carry;
  93. csum += word[2];
  94. carry = (csum < word[2]);
  95. csum += carry;
  96. csum += word[3];
  97. carry = (csum < word[3]);
  98. csum += carry;
  99. word += 4;
  100. do {
  101. csum += *word;
  102. carry = (csum < *word);
  103. csum += carry;
  104. word++;
  105. } while (word != stop);
  106. return csum_fold(csum);
  107. }
  108. static inline unsigned int csum_tcpudp_nofold(unsigned long saddr,
  109. unsigned long daddr, unsigned short len, unsigned short proto,
  110. unsigned int sum)
  111. {
  112. __asm__(
  113. ".set\tnoat\t\t\t# csum_tcpudp_nofold\n\t"
  114. #ifdef CONFIG_32BIT
  115. "addu\t%0, %2\n\t"
  116. "sltu\t$1, %0, %2\n\t"
  117. "addu\t%0, $1\n\t"
  118. "addu\t%0, %3\n\t"
  119. "sltu\t$1, %0, %3\n\t"
  120. "addu\t%0, $1\n\t"
  121. "addu\t%0, %4\n\t"
  122. "sltu\t$1, %0, %4\n\t"
  123. "addu\t%0, $1\n\t"
  124. #endif
  125. #ifdef CONFIG_64BIT
  126. "daddu\t%0, %2\n\t"
  127. "daddu\t%0, %3\n\t"
  128. "daddu\t%0, %4\n\t"
  129. "dsll32\t$1, %0, 0\n\t"
  130. "daddu\t%0, $1\n\t"
  131. "dsrl32\t%0, %0, 0\n\t"
  132. #endif
  133. ".set\tat"
  134. : "=r" (sum)
  135. : "0" (daddr), "r"(saddr),
  136. #ifdef __MIPSEL__
  137. "r" (((unsigned long)htons(len)<<16) + proto*256),
  138. #else
  139. "r" (((unsigned long)(proto)<<16) + len),
  140. #endif
  141. "r" (sum));
  142. return sum;
  143. }
  144. /*
  145. * computes the checksum of the TCP/UDP pseudo-header
  146. * returns a 16-bit checksum, already complemented
  147. */
  148. static inline unsigned short int csum_tcpudp_magic(unsigned long saddr,
  149. unsigned long daddr,
  150. unsigned short len,
  151. unsigned short proto,
  152. unsigned int sum)
  153. {
  154. return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
  155. }
  156. /*
  157. * this routine is used for miscellaneous IP-like checksums, mainly
  158. * in icmp.c
  159. */
  160. static inline unsigned short ip_compute_csum(unsigned char * buff, int len)
  161. {
  162. return csum_fold(csum_partial(buff, len, 0));
  163. }
  164. #define _HAVE_ARCH_IPV6_CSUM
  165. static __inline__ unsigned short int csum_ipv6_magic(struct in6_addr *saddr,
  166. struct in6_addr *daddr,
  167. __u32 len,
  168. unsigned short proto,
  169. unsigned int sum)
  170. {
  171. __asm__(
  172. ".set\tpush\t\t\t# csum_ipv6_magic\n\t"
  173. ".set\tnoreorder\n\t"
  174. ".set\tnoat\n\t"
  175. "addu\t%0, %5\t\t\t# proto (long in network byte order)\n\t"
  176. "sltu\t$1, %0, %5\n\t"
  177. "addu\t%0, $1\n\t"
  178. "addu\t%0, %6\t\t\t# csum\n\t"
  179. "sltu\t$1, %0, %6\n\t"
  180. "lw\t%1, 0(%2)\t\t\t# four words source address\n\t"
  181. "addu\t%0, $1\n\t"
  182. "addu\t%0, %1\n\t"
  183. "sltu\t$1, %0, %1\n\t"
  184. "lw\t%1, 4(%2)\n\t"
  185. "addu\t%0, $1\n\t"
  186. "addu\t%0, %1\n\t"
  187. "sltu\t$1, %0, %1\n\t"
  188. "lw\t%1, 8(%2)\n\t"
  189. "addu\t%0, $1\n\t"
  190. "addu\t%0, %1\n\t"
  191. "sltu\t$1, %0, %1\n\t"
  192. "lw\t%1, 12(%2)\n\t"
  193. "addu\t%0, $1\n\t"
  194. "addu\t%0, %1\n\t"
  195. "sltu\t$1, %0, %1\n\t"
  196. "lw\t%1, 0(%3)\n\t"
  197. "addu\t%0, $1\n\t"
  198. "addu\t%0, %1\n\t"
  199. "sltu\t$1, %0, %1\n\t"
  200. "lw\t%1, 4(%3)\n\t"
  201. "addu\t%0, $1\n\t"
  202. "addu\t%0, %1\n\t"
  203. "sltu\t$1, %0, %1\n\t"
  204. "lw\t%1, 8(%3)\n\t"
  205. "addu\t%0, $1\n\t"
  206. "addu\t%0, %1\n\t"
  207. "sltu\t$1, %0, %1\n\t"
  208. "lw\t%1, 12(%3)\n\t"
  209. "addu\t%0, $1\n\t"
  210. "addu\t%0, %1\n\t"
  211. "sltu\t$1, %0, %1\n\t"
  212. "addu\t%0, $1\t\t\t# Add final carry\n\t"
  213. ".set\tpop"
  214. : "=r" (sum), "=r" (proto)
  215. : "r" (saddr), "r" (daddr),
  216. "0" (htonl(len)), "1" (htonl(proto)), "r" (sum));
  217. return csum_fold(sum);
  218. }
  219. #endif /* _ASM_CHECKSUM_H */