checksum.h 3.7 KB

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