checksum_32.h 4.9 KB

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