checksum.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * linux/include/asm-arm/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. * This is a version of ip_compute_csum() optimized for IP headers,
  38. * which always checksum on 4 octet boundaries.
  39. */
  40. static inline __sum16
  41. ip_fast_csum(const void *iph, unsigned int ihl)
  42. {
  43. unsigned int sum, tmp1;
  44. __asm__ __volatile__(
  45. "ldr %0, [%1], #4 @ ip_fast_csum \n\
  46. ldr %3, [%1], #4 \n\
  47. sub %2, %2, #5 \n\
  48. adds %0, %0, %3 \n\
  49. ldr %3, [%1], #4 \n\
  50. adcs %0, %0, %3 \n\
  51. ldr %3, [%1], #4 \n\
  52. 1: adcs %0, %0, %3 \n\
  53. ldr %3, [%1], #4 \n\
  54. tst %2, #15 @ do this carefully \n\
  55. subne %2, %2, #1 @ without destroying \n\
  56. bne 1b @ the carry flag \n\
  57. adcs %0, %0, %3 \n\
  58. adc %0, %0, #0 \n\
  59. adds %0, %0, %0, lsl #16 \n\
  60. addcs %0, %0, #0x10000 \n\
  61. mvn %0, %0 \n\
  62. mov %0, %0, lsr #16"
  63. : "=r" (sum), "=r" (iph), "=r" (ihl), "=r" (tmp1)
  64. : "1" (iph), "2" (ihl)
  65. : "cc", "memory");
  66. return (__force __sum16)sum;
  67. }
  68. /*
  69. * Fold a partial checksum without adding pseudo headers
  70. */
  71. static inline __sum16 csum_fold(__wsum sum)
  72. {
  73. __asm__(
  74. "adds %0, %1, %1, lsl #16 @ csum_fold \n\
  75. addcs %0, %0, #0x10000"
  76. : "=r" (sum)
  77. : "r" (sum)
  78. : "cc");
  79. return (__force __sum16)(~(__force u32)sum >> 16);
  80. }
  81. static inline __wsum
  82. csum_tcpudp_nofold(__be32 saddr, __be32 daddr, unsigned short len,
  83. unsigned short proto, __wsum sum)
  84. {
  85. __asm__(
  86. "adds %0, %1, %2 @ csum_tcpudp_nofold \n\
  87. adcs %0, %0, %3 \n"
  88. #ifdef __ARMEB__
  89. "adcs %0, %0, %4 \n"
  90. #else
  91. "adcs %0, %0, %4, lsl #8 \n"
  92. #endif
  93. "adcs %0, %0, %5 \n\
  94. adc %0, %0, #0"
  95. : "=&r"(sum)
  96. : "r" (sum), "r" (daddr), "r" (saddr), "r" (len), "Ir" (htons(proto))
  97. : "cc");
  98. return sum;
  99. }
  100. /*
  101. * computes the checksum of the TCP/UDP pseudo-header
  102. * returns a 16-bit checksum, already complemented
  103. */
  104. static inline __sum16
  105. csum_tcpudp_magic(__be32 saddr, __be32 daddr, unsigned short len,
  106. unsigned short proto, __wsum sum)
  107. {
  108. __asm__(
  109. "adds %0, %1, %2 @ csum_tcpudp_magic \n\
  110. adcs %0, %0, %3 \n"
  111. #ifdef __ARMEB__
  112. "adcs %0, %0, %4 \n"
  113. #else
  114. "adcs %0, %0, %4, lsl #8 \n"
  115. #endif
  116. "adcs %0, %0, %5 \n\
  117. adc %0, %0, #0 \n\
  118. adds %0, %0, %0, lsl #16 \n\
  119. addcs %0, %0, #0x10000 \n\
  120. mvn %0, %0"
  121. : "=&r"(sum)
  122. : "r" (sum), "r" (daddr), "r" (saddr), "r" (len), "Ir" (htons(proto))
  123. : "cc");
  124. return (__force __sum16)((__force u32)sum >> 16);
  125. }
  126. /*
  127. * this routine is used for miscellaneous IP-like checksums, mainly
  128. * in icmp.c
  129. */
  130. static inline __sum16
  131. ip_compute_csum(const void *buff, int len)
  132. {
  133. return csum_fold(csum_partial(buff, len, 0));
  134. }
  135. #define _HAVE_ARCH_IPV6_CSUM
  136. extern __wsum
  137. __csum_ipv6_magic(const struct in6_addr *saddr, const struct in6_addr *daddr, __be32 len,
  138. __be32 proto, __wsum sum);
  139. static inline __sum16
  140. csum_ipv6_magic(const struct in6_addr *saddr, const struct in6_addr *daddr, __u32 len,
  141. unsigned short proto, __wsum sum)
  142. {
  143. return csum_fold(__csum_ipv6_magic(saddr, daddr, htonl(len),
  144. htonl(proto), sum));
  145. }
  146. #endif