checksum.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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/in6.h>
  14. #include <asm/uaccess.h>
  15. /*
  16. * computes the checksum of a memory block at buff, length len,
  17. * and adds in "sum" (32-bit)
  18. *
  19. * returns a 32-bit number suitable for feeding into itself
  20. * or csum_tcpudp_magic
  21. *
  22. * this function must be called with even lengths, except
  23. * for the last fragment, which may be odd
  24. *
  25. * it's best to have buff aligned on a 32-bit boundary
  26. */
  27. unsigned int csum_partial(const unsigned char *buff, int len, unsigned int sum);
  28. /*
  29. * this is a new version of the above that records errors it finds in *errp,
  30. * but continues and zeros the rest of the buffer.
  31. */
  32. unsigned int csum_partial_copy_from_user(const unsigned char __user *src,
  33. 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 push # csum_fold\n"
  65. " .set noat \n"
  66. " sll $1, %0, 16 \n"
  67. " addu %0, $1 \n"
  68. " sltu $1, %0, $1 \n"
  69. " srl %0, %0, 16 \n"
  70. " addu %0, $1 \n"
  71. " xori %0, 0xffff \n"
  72. " .set pop"
  73. : "=r" (sum)
  74. : "0" (sum));
  75. return sum;
  76. }
  77. /*
  78. * This is a version of ip_compute_csum() optimized for IP headers,
  79. * which always checksum on 4 octet boundaries.
  80. *
  81. * By Jorge Cwik <jorge@laser.satlink.net>, adapted for linux by
  82. * Arnt Gulbrandsen.
  83. */
  84. static inline unsigned short ip_fast_csum(unsigned char *iph, unsigned int ihl)
  85. {
  86. unsigned int *word = (unsigned int *) iph;
  87. unsigned int *stop = word + ihl;
  88. unsigned int csum;
  89. int carry;
  90. csum = word[0];
  91. csum += word[1];
  92. carry = (csum < word[1]);
  93. csum += carry;
  94. csum += word[2];
  95. carry = (csum < word[2]);
  96. csum += carry;
  97. csum += word[3];
  98. carry = (csum < word[3]);
  99. csum += carry;
  100. word += 4;
  101. do {
  102. csum += *word;
  103. carry = (csum < *word);
  104. csum += carry;
  105. word++;
  106. } while (word != stop);
  107. return csum_fold(csum);
  108. }
  109. static inline unsigned int csum_tcpudp_nofold(unsigned long saddr,
  110. unsigned long daddr, unsigned short len, unsigned short proto,
  111. unsigned int sum)
  112. {
  113. __asm__(
  114. " .set push # csum_tcpudp_nofold\n"
  115. " .set noat \n"
  116. #ifdef CONFIG_32BIT
  117. " addu %0, %2 \n"
  118. " sltu $1, %0, %2 \n"
  119. " addu %0, $1 \n"
  120. " addu %0, %3 \n"
  121. " sltu $1, %0, %3 \n"
  122. " addu %0, $1 \n"
  123. " addu %0, %4 \n"
  124. " sltu $1, %0, %4 \n"
  125. " addu %0, $1 \n"
  126. #endif
  127. #ifdef CONFIG_64BIT
  128. " daddu %0, %2 \n"
  129. " daddu %0, %3 \n"
  130. " daddu %0, %4 \n"
  131. " dsll32 $1, %0, 0 \n"
  132. " daddu %0, $1 \n"
  133. " dsra32 %0, %0, 0 \n"
  134. #endif
  135. " .set pop"
  136. : "=r" (sum)
  137. : "0" (daddr), "r"(saddr),
  138. #ifdef __MIPSEL__
  139. "r" (((unsigned long)htons(len)<<16) + proto*256),
  140. #else
  141. "r" (((unsigned long)(proto)<<16) + len),
  142. #endif
  143. "r" (sum));
  144. return sum;
  145. }
  146. /*
  147. * computes the checksum of the TCP/UDP pseudo-header
  148. * returns a 16-bit checksum, already complemented
  149. */
  150. static inline unsigned short int csum_tcpudp_magic(unsigned long saddr,
  151. unsigned long daddr,
  152. unsigned short len,
  153. unsigned short proto,
  154. unsigned int sum)
  155. {
  156. return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
  157. }
  158. /*
  159. * this routine is used for miscellaneous IP-like checksums, mainly
  160. * in icmp.c
  161. */
  162. static inline unsigned short ip_compute_csum(unsigned char * buff, int len)
  163. {
  164. return csum_fold(csum_partial(buff, len, 0));
  165. }
  166. #define _HAVE_ARCH_IPV6_CSUM
  167. static __inline__ unsigned short int csum_ipv6_magic(struct in6_addr *saddr,
  168. struct in6_addr *daddr,
  169. __u32 len,
  170. unsigned short proto,
  171. unsigned int sum)
  172. {
  173. __asm__(
  174. " .set push # csum_ipv6_magic\n"
  175. " .set noreorder \n"
  176. " .set noat \n"
  177. " addu %0, %5 # proto (long in network byte order)\n"
  178. " sltu $1, %0, %5 \n"
  179. " addu %0, $1 \n"
  180. " addu %0, %6 # csum\n"
  181. " sltu $1, %0, %6 \n"
  182. " lw %1, 0(%2) # four words source address\n"
  183. " addu %0, $1 \n"
  184. " addu %0, %1 \n"
  185. " sltu $1, %0, %1 \n"
  186. " lw %1, 4(%2) \n"
  187. " addu %0, $1 \n"
  188. " addu %0, %1 \n"
  189. " sltu $1, %0, %1 \n"
  190. " lw %1, 8(%2) \n"
  191. " addu %0, $1 \n"
  192. " addu %0, %1 \n"
  193. " sltu $1, %0, %1 \n"
  194. " lw %1, 12(%2) \n"
  195. " addu %0, $1 \n"
  196. " addu %0, %1 \n"
  197. " sltu $1, %0, %1 \n"
  198. " lw %1, 0(%3) \n"
  199. " addu %0, $1 \n"
  200. " addu %0, %1 \n"
  201. " sltu $1, %0, %1 \n"
  202. " lw %1, 4(%3) \n"
  203. " addu %0, $1 \n"
  204. " addu %0, %1 \n"
  205. " sltu $1, %0, %1 \n"
  206. " lw %1, 8(%3) \n"
  207. " addu %0, $1 \n"
  208. " addu %0, %1 \n"
  209. " sltu $1, %0, %1 \n"
  210. " lw %1, 12(%3) \n"
  211. " addu %0, $1 \n"
  212. " addu %0, %1 \n"
  213. " sltu $1, %0, %1 \n"
  214. " addu %0, $1 # Add final carry\n"
  215. " .set pop"
  216. : "=r" (sum), "=r" (proto)
  217. : "r" (saddr), "r" (daddr),
  218. "0" (htonl(len)), "1" (htonl(proto)), "r" (sum));
  219. return csum_fold(sum);
  220. }
  221. #endif /* _ASM_CHECKSUM_H */