checksum.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * arch/arm/include/asm/checksum.h
  4. *
  5. * IP checksum routines
  6. *
  7. * Copyright (C) Original authors of ../asm-i386/checksum.h
  8. * Copyright (C) 1996-1999 Russell King
  9. */
  10. #ifndef __ASM_ARM_CHECKSUM_H
  11. #define __ASM_ARM_CHECKSUM_H
  12. #include <linux/in6.h>
  13. #include <linux/uaccess.h>
  14. /*
  15. * computes the checksum of a memory block at buff, length len,
  16. * and adds in "sum" (32-bit)
  17. *
  18. * returns a 32-bit number suitable for feeding into itself
  19. * or csum_tcpudp_magic
  20. *
  21. * this function must be called with even lengths, except
  22. * for the last fragment, which may be odd
  23. *
  24. * it's best to have buff aligned on a 32-bit boundary
  25. */
  26. __wsum csum_partial(const void *buff, int len, __wsum sum);
  27. /*
  28. * the same as csum_partial, but copies from src while it
  29. * checksums, and handles user-space pointer exceptions correctly, when needed.
  30. *
  31. * here even more important to align src and dst on a 32-bit (or even
  32. * better 64-bit) boundary
  33. */
  34. __wsum
  35. csum_partial_copy_nocheck(const void *src, void *dst, int len);
  36. __wsum
  37. csum_partial_copy_from_user(const void __user *src, void *dst, int len);
  38. #define _HAVE_ARCH_COPY_AND_CSUM_FROM_USER
  39. #define _HAVE_ARCH_CSUM_AND_COPY
  40. static inline
  41. __wsum csum_and_copy_from_user(const void __user *src, void *dst, int len)
  42. {
  43. if (!access_ok(src, len))
  44. return 0;
  45. return csum_partial_copy_from_user(src, dst, len);
  46. }
  47. /*
  48. * Fold a partial checksum without adding pseudo headers
  49. */
  50. static inline __sum16 csum_fold(__wsum sum)
  51. {
  52. __asm__(
  53. "add %0, %1, %1, ror #16 @ csum_fold"
  54. : "=r" (sum)
  55. : "r" (sum)
  56. : "cc");
  57. return (__force __sum16)(~(__force u32)sum >> 16);
  58. }
  59. /*
  60. * This is a version of ip_compute_csum() optimized for IP headers,
  61. * which always checksum on 4 octet boundaries.
  62. */
  63. static inline __sum16
  64. ip_fast_csum(const void *iph, unsigned int ihl)
  65. {
  66. unsigned int tmp1;
  67. __wsum sum;
  68. __asm__ __volatile__(
  69. "ldr %0, [%1], #4 @ ip_fast_csum \n\
  70. ldr %3, [%1], #4 \n\
  71. sub %2, %2, #5 \n\
  72. adds %0, %0, %3 \n\
  73. ldr %3, [%1], #4 \n\
  74. adcs %0, %0, %3 \n\
  75. ldr %3, [%1], #4 \n\
  76. 1: adcs %0, %0, %3 \n\
  77. ldr %3, [%1], #4 \n\
  78. tst %2, #15 @ do this carefully \n\
  79. subne %2, %2, #1 @ without destroying \n\
  80. bne 1b @ the carry flag \n\
  81. adcs %0, %0, %3 \n\
  82. adc %0, %0, #0"
  83. : "=r" (sum), "=r" (iph), "=r" (ihl), "=r" (tmp1)
  84. : "1" (iph), "2" (ihl)
  85. : "cc", "memory");
  86. return csum_fold(sum);
  87. }
  88. static inline __wsum
  89. csum_tcpudp_nofold(__be32 saddr, __be32 daddr, __u32 len,
  90. __u8 proto, __wsum sum)
  91. {
  92. u32 lenprot = len + proto;
  93. if (__builtin_constant_p(sum) && sum == 0) {
  94. __asm__(
  95. "adds %0, %1, %2 @ csum_tcpudp_nofold0 \n\t"
  96. #ifdef __ARMEB__
  97. "adcs %0, %0, %3 \n\t"
  98. #else
  99. "adcs %0, %0, %3, ror #8 \n\t"
  100. #endif
  101. "adc %0, %0, #0"
  102. : "=&r" (sum)
  103. : "r" (daddr), "r" (saddr), "r" (lenprot)
  104. : "cc");
  105. } else {
  106. __asm__(
  107. "adds %0, %1, %2 @ csum_tcpudp_nofold \n\t"
  108. "adcs %0, %0, %3 \n\t"
  109. #ifdef __ARMEB__
  110. "adcs %0, %0, %4 \n\t"
  111. #else
  112. "adcs %0, %0, %4, ror #8 \n\t"
  113. #endif
  114. "adc %0, %0, #0"
  115. : "=&r"(sum)
  116. : "r" (sum), "r" (daddr), "r" (saddr), "r" (lenprot)
  117. : "cc");
  118. }
  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
  126. csum_tcpudp_magic(__be32 saddr, __be32 daddr, __u32 len,
  127. __u8 proto, __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
  136. ip_compute_csum(const void *buff, int len)
  137. {
  138. return csum_fold(csum_partial(buff, len, 0));
  139. }
  140. #define _HAVE_ARCH_IPV6_CSUM
  141. extern __wsum
  142. __csum_ipv6_magic(const struct in6_addr *saddr, const struct in6_addr *daddr, __be32 len,
  143. __be32 proto, __wsum sum);
  144. static inline __sum16
  145. csum_ipv6_magic(const struct in6_addr *saddr, const struct in6_addr *daddr,
  146. __u32 len, __u8 proto, __wsum sum)
  147. {
  148. return csum_fold(__csum_ipv6_magic(saddr, daddr, htonl(len),
  149. htonl(proto), sum));
  150. }
  151. #endif