checksum.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. * 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, unsigned short len,
  79. unsigned short proto, __wsum sum)
  80. {
  81. __asm__(
  82. "adds %0, %1, %2 @ csum_tcpudp_nofold \n\
  83. adcs %0, %0, %3 \n"
  84. #ifdef __ARMEB__
  85. "adcs %0, %0, %4 \n"
  86. #else
  87. "adcs %0, %0, %4, lsl #8 \n"
  88. #endif
  89. "adcs %0, %0, %5 \n\
  90. adc %0, %0, #0"
  91. : "=&r"(sum)
  92. : "r" (sum), "r" (daddr), "r" (saddr), "r" (len), "Ir" (htons(proto))
  93. : "cc");
  94. return sum;
  95. }
  96. /*
  97. * computes the checksum of the TCP/UDP pseudo-header
  98. * returns a 16-bit checksum, already complemented
  99. */
  100. static inline __sum16
  101. csum_tcpudp_magic(__be32 saddr, __be32 daddr, unsigned short len,
  102. unsigned short proto, __wsum sum)
  103. {
  104. return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
  105. }
  106. /*
  107. * this routine is used for miscellaneous IP-like checksums, mainly
  108. * in icmp.c
  109. */
  110. static inline __sum16
  111. ip_compute_csum(const void *buff, int len)
  112. {
  113. return csum_fold(csum_partial(buff, len, 0));
  114. }
  115. #define _HAVE_ARCH_IPV6_CSUM
  116. extern __wsum
  117. __csum_ipv6_magic(const struct in6_addr *saddr, const struct in6_addr *daddr, __be32 len,
  118. __be32 proto, __wsum sum);
  119. static inline __sum16
  120. csum_ipv6_magic(const struct in6_addr *saddr, const struct in6_addr *daddr, __u32 len,
  121. unsigned short proto, __wsum sum)
  122. {
  123. return csum_fold(__csum_ipv6_magic(saddr, daddr, htonl(len),
  124. htonl(proto), sum));
  125. }
  126. #endif