checksum_32.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_X86_CHECKSUM_32_H
  3. #define _ASM_X86_CHECKSUM_32_H
  4. #include <linux/in6.h>
  5. #include <linux/uaccess.h>
  6. /*
  7. * computes the checksum of a memory block at buff, length len,
  8. * and adds in "sum" (32-bit)
  9. *
  10. * returns a 32-bit number suitable for feeding into itself
  11. * or csum_tcpudp_magic
  12. *
  13. * this function must be called with even lengths, except
  14. * for the last fragment, which may be odd
  15. *
  16. * it's best to have buff aligned on a 32-bit boundary
  17. */
  18. asmlinkage __wsum csum_partial(const void *buff, int len, __wsum sum);
  19. /*
  20. * the same as csum_partial, but copies from src while it
  21. * checksums, and handles user-space pointer exceptions correctly, when needed.
  22. *
  23. * here even more important to align src and dst on a 32-bit (or even
  24. * better 64-bit) boundary
  25. */
  26. asmlinkage __wsum csum_partial_copy_generic(const void *src, void *dst, int len);
  27. /*
  28. * Note: when you get a NULL pointer exception here this means someone
  29. * passed in an incorrect kernel address to one of these functions.
  30. *
  31. * If you use these functions directly please don't forget the
  32. * access_ok().
  33. */
  34. static inline __wsum csum_partial_copy_nocheck(const void *src, void *dst, int len)
  35. {
  36. return csum_partial_copy_generic(src, dst, len);
  37. }
  38. static inline __wsum csum_and_copy_from_user(const void __user *src,
  39. void *dst, int len)
  40. {
  41. __wsum ret;
  42. might_sleep();
  43. if (!user_access_begin(src, len))
  44. return 0;
  45. ret = csum_partial_copy_generic((__force void *)src, dst, len);
  46. user_access_end();
  47. return ret;
  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. * By Jorge Cwik <[email protected]>, adapted for linux by
  54. * Arnt Gulbrandsen.
  55. */
  56. static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
  57. {
  58. unsigned int sum;
  59. asm volatile("movl (%1), %0 ;\n"
  60. "subl $4, %2 ;\n"
  61. "jbe 2f ;\n"
  62. "addl 4(%1), %0 ;\n"
  63. "adcl 8(%1), %0 ;\n"
  64. "adcl 12(%1), %0;\n"
  65. "1: adcl 16(%1), %0 ;\n"
  66. "lea 4(%1), %1 ;\n"
  67. "decl %2 ;\n"
  68. "jne 1b ;\n"
  69. "adcl $0, %0 ;\n"
  70. "movl %0, %2 ;\n"
  71. "shrl $16, %0 ;\n"
  72. "addw %w2, %w0 ;\n"
  73. "adcl $0, %0 ;\n"
  74. "notl %0 ;\n"
  75. "2: ;\n"
  76. /* Since the input registers which are loaded with iph and ihl
  77. are modified, we must also specify them as outputs, or gcc
  78. will assume they contain their original values. */
  79. : "=r" (sum), "=r" (iph), "=r" (ihl)
  80. : "1" (iph), "2" (ihl)
  81. : "memory");
  82. return (__force __sum16)sum;
  83. }
  84. /*
  85. * Fold a partial checksum
  86. */
  87. static inline __sum16 csum_fold(__wsum sum)
  88. {
  89. asm("addl %1, %0 ;\n"
  90. "adcl $0xffff, %0 ;\n"
  91. : "=r" (sum)
  92. : "r" ((__force u32)sum << 16),
  93. "0" ((__force u32)sum & 0xffff0000));
  94. return (__force __sum16)(~(__force u32)sum >> 16);
  95. }
  96. static inline __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
  97. __u32 len, __u8 proto,
  98. __wsum sum)
  99. {
  100. asm("addl %1, %0 ;\n"
  101. "adcl %2, %0 ;\n"
  102. "adcl %3, %0 ;\n"
  103. "adcl $0, %0 ;\n"
  104. : "=r" (sum)
  105. : "g" (daddr), "g"(saddr),
  106. "g" ((len + proto) << 8), "0" (sum));
  107. return sum;
  108. }
  109. /*
  110. * computes the checksum of the TCP/UDP pseudo-header
  111. * returns a 16-bit checksum, already complemented
  112. */
  113. static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
  114. __u32 len, __u8 proto,
  115. __wsum sum)
  116. {
  117. return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
  118. }
  119. /*
  120. * this routine is used for miscellaneous IP-like checksums, mainly
  121. * in icmp.c
  122. */
  123. static inline __sum16 ip_compute_csum(const void *buff, int len)
  124. {
  125. return csum_fold(csum_partial(buff, len, 0));
  126. }
  127. #define _HAVE_ARCH_IPV6_CSUM
  128. static inline __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
  129. const struct in6_addr *daddr,
  130. __u32 len, __u8 proto, __wsum sum)
  131. {
  132. asm("addl 0(%1), %0 ;\n"
  133. "adcl 4(%1), %0 ;\n"
  134. "adcl 8(%1), %0 ;\n"
  135. "adcl 12(%1), %0 ;\n"
  136. "adcl 0(%2), %0 ;\n"
  137. "adcl 4(%2), %0 ;\n"
  138. "adcl 8(%2), %0 ;\n"
  139. "adcl 12(%2), %0 ;\n"
  140. "adcl %3, %0 ;\n"
  141. "adcl %4, %0 ;\n"
  142. "adcl $0, %0 ;\n"
  143. : "=&r" (sum)
  144. : "r" (saddr), "r" (daddr),
  145. "r" (htonl(len)), "r" (htonl(proto)), "0" (sum)
  146. : "memory");
  147. return csum_fold(sum);
  148. }
  149. /*
  150. * Copy and checksum to user
  151. */
  152. static inline __wsum csum_and_copy_to_user(const void *src,
  153. void __user *dst,
  154. int len)
  155. {
  156. __wsum ret;
  157. might_sleep();
  158. if (!user_access_begin(dst, len))
  159. return 0;
  160. ret = csum_partial_copy_generic(src, (__force void *)dst, len);
  161. user_access_end();
  162. return ret;
  163. }
  164. #endif /* _ASM_X86_CHECKSUM_32_H */