checksum_32.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #ifndef __ASM_SH_CHECKSUM_H
  2. #define __ASM_SH_CHECKSUM_H
  3. /*
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License. See the file "COPYING" in the main directory of this archive
  6. * for more details.
  7. *
  8. * Copyright (C) 1999 by Kaz Kojima & Niibe Yutaka
  9. */
  10. #include <linux/in6.h>
  11. /*
  12. * computes the checksum of a memory block at buff, length len,
  13. * and adds in "sum" (32-bit)
  14. *
  15. * returns a 32-bit number suitable for feeding into itself
  16. * or csum_tcpudp_magic
  17. *
  18. * this function must be called with even lengths, except
  19. * for the last fragment, which may be odd
  20. *
  21. * it's best to have buff aligned on a 32-bit boundary
  22. */
  23. asmlinkage __wsum csum_partial(const void *buff, int len, __wsum sum);
  24. /*
  25. * the same as csum_partial, but copies from src while it
  26. * checksums, and handles user-space pointer exceptions correctly, when needed.
  27. *
  28. * here even more important to align src and dst on a 32-bit (or even
  29. * better 64-bit) boundary
  30. */
  31. asmlinkage __wsum csum_partial_copy_generic(const void *src, void *dst,
  32. int len, __wsum sum,
  33. int *src_err_ptr, int *dst_err_ptr);
  34. /*
  35. * Note: when you get a NULL pointer exception here this means someone
  36. * passed in an incorrect kernel address to one of these functions.
  37. *
  38. * If you use these functions directly please don't forget the
  39. * access_ok().
  40. */
  41. static inline
  42. __wsum csum_partial_copy_nocheck(const void *src, void *dst,
  43. int len, __wsum sum)
  44. {
  45. return csum_partial_copy_generic(src, dst, len, sum, NULL, NULL);
  46. }
  47. static inline
  48. __wsum csum_partial_copy_from_user(const void __user *src, void *dst,
  49. int len, __wsum sum, int *err_ptr)
  50. {
  51. return csum_partial_copy_generic((__force const void *)src, dst,
  52. len, sum, err_ptr, NULL);
  53. }
  54. /*
  55. * Fold a partial checksum
  56. */
  57. static inline __sum16 csum_fold(__wsum sum)
  58. {
  59. unsigned int __dummy;
  60. __asm__("swap.w %0, %1\n\t"
  61. "extu.w %0, %0\n\t"
  62. "extu.w %1, %1\n\t"
  63. "add %1, %0\n\t"
  64. "swap.w %0, %1\n\t"
  65. "add %1, %0\n\t"
  66. "not %0, %0\n\t"
  67. : "=r" (sum), "=&r" (__dummy)
  68. : "0" (sum)
  69. : "t");
  70. return (__force __sum16)sum;
  71. }
  72. /*
  73. * This is a version of ip_compute_csum() optimized for IP headers,
  74. * which always checksum on 4 octet boundaries.
  75. *
  76. * i386 version by Jorge Cwik <[email protected]>, adapted
  77. * for linux by * Arnt Gulbrandsen.
  78. */
  79. static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
  80. {
  81. unsigned int sum, __dummy0, __dummy1;
  82. __asm__ __volatile__(
  83. "mov.l @%1+, %0\n\t"
  84. "mov.l @%1+, %3\n\t"
  85. "add #-2, %2\n\t"
  86. "clrt\n\t"
  87. "1:\t"
  88. "addc %3, %0\n\t"
  89. "movt %4\n\t"
  90. "mov.l @%1+, %3\n\t"
  91. "dt %2\n\t"
  92. "bf/s 1b\n\t"
  93. " cmp/eq #1, %4\n\t"
  94. "addc %3, %0\n\t"
  95. "addc %2, %0" /* Here %2 is 0, add carry-bit */
  96. /* Since the input registers which are loaded with iph and ihl
  97. are modified, we must also specify them as outputs, or gcc
  98. will assume they contain their original values. */
  99. : "=r" (sum), "=r" (iph), "=r" (ihl), "=&r" (__dummy0), "=&z" (__dummy1)
  100. : "1" (iph), "2" (ihl)
  101. : "t", "memory");
  102. return csum_fold(sum);
  103. }
  104. static inline __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
  105. __u32 len, __u8 proto,
  106. __wsum sum)
  107. {
  108. #ifdef __LITTLE_ENDIAN__
  109. unsigned long len_proto = (proto + len) << 8;
  110. #else
  111. unsigned long len_proto = proto + len;
  112. #endif
  113. __asm__("clrt\n\t"
  114. "addc %0, %1\n\t"
  115. "addc %2, %1\n\t"
  116. "addc %3, %1\n\t"
  117. "movt %0\n\t"
  118. "add %1, %0"
  119. : "=r" (sum), "=r" (len_proto)
  120. : "r" (daddr), "r" (saddr), "1" (len_proto), "0" (sum)
  121. : "t");
  122. return sum;
  123. }
  124. /*
  125. * computes the checksum of the TCP/UDP pseudo-header
  126. * returns a 16-bit checksum, already complemented
  127. */
  128. static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
  129. __u32 len, __u8 proto,
  130. __wsum sum)
  131. {
  132. return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
  133. }
  134. /*
  135. * this routine is used for miscellaneous IP-like checksums, mainly
  136. * in icmp.c
  137. */
  138. static inline __sum16 ip_compute_csum(const void *buff, int len)
  139. {
  140. return csum_fold(csum_partial(buff, len, 0));
  141. }
  142. #define _HAVE_ARCH_IPV6_CSUM
  143. static inline __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
  144. const struct in6_addr *daddr,
  145. __u32 len, __u8 proto, __wsum sum)
  146. {
  147. unsigned int __dummy;
  148. __asm__("clrt\n\t"
  149. "mov.l @(0,%2), %1\n\t"
  150. "addc %1, %0\n\t"
  151. "mov.l @(4,%2), %1\n\t"
  152. "addc %1, %0\n\t"
  153. "mov.l @(8,%2), %1\n\t"
  154. "addc %1, %0\n\t"
  155. "mov.l @(12,%2), %1\n\t"
  156. "addc %1, %0\n\t"
  157. "mov.l @(0,%3), %1\n\t"
  158. "addc %1, %0\n\t"
  159. "mov.l @(4,%3), %1\n\t"
  160. "addc %1, %0\n\t"
  161. "mov.l @(8,%3), %1\n\t"
  162. "addc %1, %0\n\t"
  163. "mov.l @(12,%3), %1\n\t"
  164. "addc %1, %0\n\t"
  165. "addc %4, %0\n\t"
  166. "addc %5, %0\n\t"
  167. "movt %1\n\t"
  168. "add %1, %0\n"
  169. : "=r" (sum), "=&r" (__dummy)
  170. : "r" (saddr), "r" (daddr),
  171. "r" (htonl(len)), "r" (htonl(proto)), "0" (sum)
  172. : "t");
  173. return csum_fold(sum);
  174. }
  175. /*
  176. * Copy and checksum to user
  177. */
  178. #define HAVE_CSUM_COPY_USER
  179. static inline __wsum csum_and_copy_to_user(const void *src,
  180. void __user *dst,
  181. int len, __wsum sum,
  182. int *err_ptr)
  183. {
  184. if (access_ok(VERIFY_WRITE, dst, len))
  185. return csum_partial_copy_generic((__force const void *)src,
  186. dst, len, sum, NULL, err_ptr);
  187. if (len)
  188. *err_ptr = -EFAULT;
  189. return (__force __wsum)-1; /* invalid checksum */
  190. }
  191. #endif /* __ASM_SH_CHECKSUM_H */