checksum.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. * Copyright (C) 2014 Imagination Technologies Ltd.
  11. */
  12. #ifndef _ASM_CHECKSUM_H
  13. #define _ASM_CHECKSUM_H
  14. #ifdef CONFIG_GENERIC_CSUM
  15. #include <asm-generic/checksum.h>
  16. #else
  17. #include <linux/in6.h>
  18. #include <linux/uaccess.h>
  19. /*
  20. * computes the checksum of a memory block at buff, length len,
  21. * and adds in "sum" (32-bit)
  22. *
  23. * returns a 32-bit number suitable for feeding into itself
  24. * or csum_tcpudp_magic
  25. *
  26. * this function must be called with even lengths, except
  27. * for the last fragment, which may be odd
  28. *
  29. * it's best to have buff aligned on a 32-bit boundary
  30. */
  31. __wsum csum_partial(const void *buff, int len, __wsum sum);
  32. __wsum __csum_partial_copy_from_user(const void __user *src, void *dst, int len);
  33. __wsum __csum_partial_copy_to_user(const void *src, void __user *dst, int len);
  34. #define _HAVE_ARCH_COPY_AND_CSUM_FROM_USER
  35. static inline
  36. __wsum csum_and_copy_from_user(const void __user *src, void *dst, int len)
  37. {
  38. might_fault();
  39. if (!access_ok(src, len))
  40. return 0;
  41. return __csum_partial_copy_from_user(src, dst, len);
  42. }
  43. /*
  44. * Copy and checksum to user
  45. */
  46. #define HAVE_CSUM_COPY_USER
  47. static inline
  48. __wsum csum_and_copy_to_user(const void *src, void __user *dst, int len)
  49. {
  50. might_fault();
  51. if (!access_ok(dst, len))
  52. return 0;
  53. return __csum_partial_copy_to_user(src, dst, len);
  54. }
  55. /*
  56. * the same as csum_partial, but copies from user space (but on MIPS
  57. * we have just one address space, so this is identical to the above)
  58. */
  59. #define _HAVE_ARCH_CSUM_AND_COPY
  60. __wsum __csum_partial_copy_nocheck(const void *src, void *dst, int len);
  61. static inline __wsum csum_partial_copy_nocheck(const void *src, void *dst, int len)
  62. {
  63. return __csum_partial_copy_nocheck(src, dst, len);
  64. }
  65. /*
  66. * Fold a partial checksum without adding pseudo headers
  67. */
  68. static inline __sum16 csum_fold(__wsum csum)
  69. {
  70. u32 sum = (__force u32)csum;
  71. sum += (sum << 16);
  72. csum = (__force __wsum)(sum < (__force u32)csum);
  73. sum >>= 16;
  74. sum += (__force u32)csum;
  75. return (__force __sum16)~sum;
  76. }
  77. #define csum_fold csum_fold
  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 <[email protected]>, adapted for linux by
  83. * Arnt Gulbrandsen.
  84. */
  85. static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
  86. {
  87. const unsigned int *word = iph;
  88. const 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. #define ip_fast_csum ip_fast_csum
  111. static inline __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
  112. __u32 len, __u8 proto,
  113. __wsum isum)
  114. {
  115. const unsigned int sh32 = IS_ENABLED(CONFIG_64BIT) ? 32 : 0;
  116. unsigned long sum = (__force unsigned long)daddr;
  117. unsigned long tmp;
  118. __u32 osum;
  119. tmp = (__force unsigned long)saddr;
  120. sum += tmp;
  121. if (IS_ENABLED(CONFIG_32BIT))
  122. sum += sum < tmp;
  123. /*
  124. * We know PROTO + LEN has the sign bit clear, so cast to a signed
  125. * type to avoid an extraneous zero-extension where TMP is 64-bit.
  126. */
  127. tmp = (__s32)(proto + len);
  128. tmp <<= IS_ENABLED(CONFIG_CPU_LITTLE_ENDIAN) ? 8 : 0;
  129. sum += tmp;
  130. if (IS_ENABLED(CONFIG_32BIT))
  131. sum += sum < tmp;
  132. tmp = (__force unsigned long)isum;
  133. sum += tmp;
  134. if (IS_ENABLED(CONFIG_32BIT)) {
  135. sum += sum < tmp;
  136. osum = sum;
  137. } else if (IS_ENABLED(CONFIG_64BIT)) {
  138. tmp = sum << sh32;
  139. sum += tmp;
  140. osum = sum < tmp;
  141. osum += sum >> sh32;
  142. } else {
  143. BUILD_BUG();
  144. }
  145. return (__force __wsum)osum;
  146. }
  147. #define csum_tcpudp_nofold csum_tcpudp_nofold
  148. /*
  149. * this routine is used for miscellaneous IP-like checksums, mainly
  150. * in icmp.c
  151. */
  152. static inline __sum16 ip_compute_csum(const void *buff, int len)
  153. {
  154. return csum_fold(csum_partial(buff, len, 0));
  155. }
  156. #define _HAVE_ARCH_IPV6_CSUM
  157. static __inline__ __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
  158. const struct in6_addr *daddr,
  159. __u32 len, __u8 proto,
  160. __wsum sum)
  161. {
  162. __wsum tmp;
  163. __asm__(
  164. " .set push # csum_ipv6_magic\n"
  165. " .set noreorder \n"
  166. " .set noat \n"
  167. " addu %0, %5 # proto (long in network byte order)\n"
  168. " sltu $1, %0, %5 \n"
  169. " addu %0, $1 \n"
  170. " addu %0, %6 # csum\n"
  171. " sltu $1, %0, %6 \n"
  172. " lw %1, 0(%2) # four words source address\n"
  173. " addu %0, $1 \n"
  174. " addu %0, %1 \n"
  175. " sltu $1, %0, %1 \n"
  176. " lw %1, 4(%2) \n"
  177. " addu %0, $1 \n"
  178. " addu %0, %1 \n"
  179. " sltu $1, %0, %1 \n"
  180. " lw %1, 8(%2) \n"
  181. " addu %0, $1 \n"
  182. " addu %0, %1 \n"
  183. " sltu $1, %0, %1 \n"
  184. " lw %1, 12(%2) \n"
  185. " addu %0, $1 \n"
  186. " addu %0, %1 \n"
  187. " sltu $1, %0, %1 \n"
  188. " lw %1, 0(%3) \n"
  189. " addu %0, $1 \n"
  190. " addu %0, %1 \n"
  191. " sltu $1, %0, %1 \n"
  192. " lw %1, 4(%3) \n"
  193. " addu %0, $1 \n"
  194. " addu %0, %1 \n"
  195. " sltu $1, %0, %1 \n"
  196. " lw %1, 8(%3) \n"
  197. " addu %0, $1 \n"
  198. " addu %0, %1 \n"
  199. " sltu $1, %0, %1 \n"
  200. " lw %1, 12(%3) \n"
  201. " addu %0, $1 \n"
  202. " addu %0, %1 \n"
  203. " sltu $1, %0, %1 \n"
  204. " addu %0, $1 # Add final carry\n"
  205. " .set pop"
  206. : "=&r" (sum), "=&r" (tmp)
  207. : "r" (saddr), "r" (daddr),
  208. "0" (htonl(len)), "r" (htonl(proto)), "r" (sum));
  209. return csum_fold(sum);
  210. }
  211. #include <asm-generic/checksum.h>
  212. #endif /* CONFIG_GENERIC_CSUM */
  213. #endif /* _ASM_CHECKSUM_H */