checksum.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. /*
  14. * computes the checksum of a memory block at buff, length len,
  15. * and adds in "sum" (32-bit)
  16. *
  17. * returns a 32-bit number suitable for feeding into itself
  18. * or csum_tcpudp_magic
  19. *
  20. * this function must be called with even lengths, except
  21. * for the last fragment, which may be odd
  22. *
  23. * it's best to have buff aligned on a 32-bit boundary
  24. */
  25. __wsum csum_partial(const void *buff, int len, __wsum sum);
  26. /*
  27. * the same as csum_partial, but copies from src while it
  28. * checksums, and handles user-space pointer exceptions correctly, when needed.
  29. *
  30. * here even more important to align src and dst on a 32-bit (or even
  31. * better 64-bit) boundary
  32. */
  33. __wsum
  34. csum_partial_copy_nocheck(const void *src, void *dst, int len, __wsum sum);
  35. __wsum
  36. csum_partial_copy_from_user(const void __user *src, void *dst, int len, __wsum sum, int *err_ptr);
  37. /*
  38. * Fold a partial checksum without adding pseudo headers
  39. */
  40. static inline __sum16 csum_fold(__wsum sum)
  41. {
  42. __asm__(
  43. "add %0, %1, %1, ror #16 @ csum_fold"
  44. : "=r" (sum)
  45. : "r" (sum)
  46. : "cc");
  47. return (__force __sum16)(~(__force u32)sum >> 16);
  48. }
  49. /*
  50. * This is a version of ip_compute_csum() optimized for IP headers,
  51. * which always checksum on 4 octet boundaries.
  52. */
  53. static inline __sum16
  54. ip_fast_csum(const void *iph, unsigned int ihl)
  55. {
  56. unsigned int tmp1;
  57. __wsum sum;
  58. __asm__ __volatile__(
  59. "ldr %0, [%1], #4 @ ip_fast_csum \n\
  60. ldr %3, [%1], #4 \n\
  61. sub %2, %2, #5 \n\
  62. adds %0, %0, %3 \n\
  63. ldr %3, [%1], #4 \n\
  64. adcs %0, %0, %3 \n\
  65. ldr %3, [%1], #4 \n\
  66. 1: adcs %0, %0, %3 \n\
  67. ldr %3, [%1], #4 \n\
  68. tst %2, #15 @ do this carefully \n\
  69. subne %2, %2, #1 @ without destroying \n\
  70. bne 1b @ the carry flag \n\
  71. adcs %0, %0, %3 \n\
  72. adc %0, %0, #0"
  73. : "=r" (sum), "=r" (iph), "=r" (ihl), "=r" (tmp1)
  74. : "1" (iph), "2" (ihl)
  75. : "cc", "memory");
  76. return csum_fold(sum);
  77. }
  78. static inline __wsum
  79. csum_tcpudp_nofold(__be32 saddr, __be32 daddr, __u32 len,
  80. __u8 proto, __wsum sum)
  81. {
  82. u32 lenprot = len + proto;
  83. if (__builtin_constant_p(sum) && sum == 0) {
  84. __asm__(
  85. "adds %0, %1, %2 @ csum_tcpudp_nofold0 \n\t"
  86. #ifdef __ARMEB__
  87. "adcs %0, %0, %3 \n\t"
  88. #else
  89. "adcs %0, %0, %3, ror #8 \n\t"
  90. #endif
  91. "adc %0, %0, #0"
  92. : "=&r" (sum)
  93. : "r" (daddr), "r" (saddr), "r" (lenprot)
  94. : "cc");
  95. } else {
  96. __asm__(
  97. "adds %0, %1, %2 @ csum_tcpudp_nofold \n\t"
  98. "adcs %0, %0, %3 \n\t"
  99. #ifdef __ARMEB__
  100. "adcs %0, %0, %4 \n\t"
  101. #else
  102. "adcs %0, %0, %4, ror #8 \n\t"
  103. #endif
  104. "adc %0, %0, #0"
  105. : "=&r"(sum)
  106. : "r" (sum), "r" (daddr), "r" (saddr), "r" (lenprot)
  107. : "cc");
  108. }
  109. return sum;
  110. }
  111. /*
  112. * computes the checksum of the TCP/UDP pseudo-header
  113. * returns a 16-bit checksum, already complemented
  114. */
  115. static inline __sum16
  116. csum_tcpudp_magic(__be32 saddr, __be32 daddr, __u32 len,
  117. __u8 proto, __wsum sum)
  118. {
  119. return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
  120. }
  121. /*
  122. * this routine is used for miscellaneous IP-like checksums, mainly
  123. * in icmp.c
  124. */
  125. static inline __sum16
  126. ip_compute_csum(const void *buff, int len)
  127. {
  128. return csum_fold(csum_partial(buff, len, 0));
  129. }
  130. #define _HAVE_ARCH_IPV6_CSUM
  131. extern __wsum
  132. __csum_ipv6_magic(const struct in6_addr *saddr, const struct in6_addr *daddr, __be32 len,
  133. __be32 proto, __wsum sum);
  134. static inline __sum16
  135. csum_ipv6_magic(const struct in6_addr *saddr, const struct in6_addr *daddr,
  136. __u32 len, __u8 proto, __wsum sum)
  137. {
  138. return csum_fold(__csum_ipv6_magic(saddr, daddr, htonl(len),
  139. htonl(proto), sum));
  140. }
  141. #endif