checksum.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 __user *src,
  34. unsigned char *dst, int len,
  35. unsigned int sum, int *errp);
  36. /*
  37. * Copy and checksum to user
  38. */
  39. #define HAVE_CSUM_COPY_USER
  40. static inline unsigned int csum_and_copy_to_user (const unsigned char *src,
  41. unsigned char __user *dst,
  42. int len, int sum,
  43. int *err_ptr)
  44. {
  45. might_sleep();
  46. sum = csum_partial(src, len, sum);
  47. if (copy_to_user(dst, src, len)) {
  48. *err_ptr = -EFAULT;
  49. return -1;
  50. }
  51. return sum;
  52. }
  53. /*
  54. * the same as csum_partial, but copies from user space (but on MIPS
  55. * we have just one address space, so this is identical to the above)
  56. */
  57. unsigned int csum_partial_copy_nocheck(const unsigned char *src, unsigned char *dst,
  58. int len, unsigned int sum);
  59. /*
  60. * Fold a partial checksum without adding pseudo headers
  61. */
  62. static inline unsigned short int csum_fold(unsigned int sum)
  63. {
  64. __asm__(
  65. " .set push # csum_fold\n"
  66. " .set noat \n"
  67. " sll $1, %0, 16 \n"
  68. " addu %0, $1 \n"
  69. " sltu $1, %0, $1 \n"
  70. " srl %0, %0, 16 \n"
  71. " addu %0, $1 \n"
  72. " xori %0, 0xffff \n"
  73. " .set pop"
  74. : "=r" (sum)
  75. : "0" (sum));
  76. return sum;
  77. }
  78. /*
  79. * This is a version of ip_compute_csum() optimized for IP headers,
  80. * which always checksum on 4 octet boundaries.
  81. *
  82. * By Jorge Cwik <jorge@laser.satlink.net>, adapted for linux by
  83. * Arnt Gulbrandsen.
  84. */
  85. static inline unsigned short ip_fast_csum(unsigned char *iph, unsigned int ihl)
  86. {
  87. unsigned int *word = (unsigned int *) iph;
  88. unsigned int *stop = word + ihl;
  89. unsigned int csum;
  90. int carry;
  91. csum = word[0];
  92. csum += word[1];
  93. carry = (csum < word[1]);
  94. csum += carry;
  95. csum += word[2];
  96. carry = (csum < word[2]);
  97. csum += carry;
  98. csum += word[3];
  99. carry = (csum < word[3]);
  100. csum += carry;
  101. word += 4;
  102. do {
  103. csum += *word;
  104. carry = (csum < *word);
  105. csum += carry;
  106. word++;
  107. } while (word != stop);
  108. return csum_fold(csum);
  109. }
  110. static inline unsigned int csum_tcpudp_nofold(unsigned long saddr,
  111. unsigned long daddr, unsigned short len, unsigned short proto,
  112. unsigned int sum)
  113. {
  114. __asm__(
  115. " .set push # csum_tcpudp_nofold\n"
  116. " .set noat \n"
  117. #ifdef CONFIG_32BIT
  118. " addu %0, %2 \n"
  119. " sltu $1, %0, %2 \n"
  120. " addu %0, $1 \n"
  121. " addu %0, %3 \n"
  122. " sltu $1, %0, %3 \n"
  123. " addu %0, $1 \n"
  124. " addu %0, %4 \n"
  125. " sltu $1, %0, %4 \n"
  126. " addu %0, $1 \n"
  127. #endif
  128. #ifdef CONFIG_64BIT
  129. " daddu %0, %2 \n"
  130. " daddu %0, %3 \n"
  131. " daddu %0, %4 \n"
  132. " dsll32 $1, %0, 0 \n"
  133. " daddu %0, $1 \n"
  134. " dsra32 %0, %0, 0 \n"
  135. #endif
  136. " .set pop"
  137. : "=r" (sum)
  138. : "0" (daddr), "r"(saddr),
  139. #ifdef __MIPSEL__
  140. "r" (((unsigned long)htons(len)<<16) + proto*256),
  141. #else
  142. "r" (((unsigned long)(proto)<<16) + len),
  143. #endif
  144. "r" (sum));
  145. return sum;
  146. }
  147. /*
  148. * computes the checksum of the TCP/UDP pseudo-header
  149. * returns a 16-bit checksum, already complemented
  150. */
  151. static inline unsigned short int csum_tcpudp_magic(unsigned long saddr,
  152. unsigned long daddr,
  153. unsigned short len,
  154. unsigned short proto,
  155. unsigned int sum)
  156. {
  157. return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
  158. }
  159. /*
  160. * this routine is used for miscellaneous IP-like checksums, mainly
  161. * in icmp.c
  162. */
  163. static inline unsigned short ip_compute_csum(unsigned char * buff, int len)
  164. {
  165. return csum_fold(csum_partial(buff, len, 0));
  166. }
  167. #define _HAVE_ARCH_IPV6_CSUM
  168. static __inline__ unsigned short int csum_ipv6_magic(struct in6_addr *saddr,
  169. struct in6_addr *daddr,
  170. __u32 len,
  171. unsigned short proto,
  172. unsigned int sum)
  173. {
  174. __asm__(
  175. " .set push # csum_ipv6_magic\n"
  176. " .set noreorder \n"
  177. " .set noat \n"
  178. " addu %0, %5 # proto (long in network byte order)\n"
  179. " sltu $1, %0, %5 \n"
  180. " addu %0, $1 \n"
  181. " addu %0, %6 # csum\n"
  182. " sltu $1, %0, %6 \n"
  183. " lw %1, 0(%2) # four words source address\n"
  184. " addu %0, $1 \n"
  185. " addu %0, %1 \n"
  186. " sltu $1, %0, %1 \n"
  187. " lw %1, 4(%2) \n"
  188. " addu %0, $1 \n"
  189. " addu %0, %1 \n"
  190. " sltu $1, %0, %1 \n"
  191. " lw %1, 8(%2) \n"
  192. " addu %0, $1 \n"
  193. " addu %0, %1 \n"
  194. " sltu $1, %0, %1 \n"
  195. " lw %1, 12(%2) \n"
  196. " addu %0, $1 \n"
  197. " addu %0, %1 \n"
  198. " sltu $1, %0, %1 \n"
  199. " lw %1, 0(%3) \n"
  200. " addu %0, $1 \n"
  201. " addu %0, %1 \n"
  202. " sltu $1, %0, %1 \n"
  203. " lw %1, 4(%3) \n"
  204. " addu %0, $1 \n"
  205. " addu %0, %1 \n"
  206. " sltu $1, %0, %1 \n"
  207. " lw %1, 8(%3) \n"
  208. " addu %0, $1 \n"
  209. " addu %0, %1 \n"
  210. " sltu $1, %0, %1 \n"
  211. " lw %1, 12(%3) \n"
  212. " addu %0, $1 \n"
  213. " addu %0, %1 \n"
  214. " sltu $1, %0, %1 \n"
  215. " addu %0, $1 # Add final carry\n"
  216. " .set pop"
  217. : "=r" (sum), "=r" (proto)
  218. : "r" (saddr), "r" (daddr),
  219. "0" (htonl(len)), "1" (htonl(proto)), "r" (sum));
  220. return csum_fold(sum);
  221. }
  222. #endif /* _ASM_CHECKSUM_H */