checksum_64.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_X86_CHECKSUM_64_H
  3. #define _ASM_X86_CHECKSUM_64_H
  4. /*
  5. * Checksums for x86-64
  6. * Copyright 2002 by Andi Kleen, SuSE Labs
  7. * with some code from asm-x86/checksum.h
  8. */
  9. #include <linux/compiler.h>
  10. #include <linux/uaccess.h>
  11. #include <asm/byteorder.h>
  12. /**
  13. * csum_fold - Fold and invert a 32bit checksum.
  14. * sum: 32bit unfolded sum
  15. *
  16. * Fold a 32bit running checksum to 16bit and invert it. This is usually
  17. * the last step before putting a checksum into a packet.
  18. * Make sure not to mix with 64bit checksums.
  19. */
  20. static inline __sum16 csum_fold(__wsum sum)
  21. {
  22. asm(" addl %1,%0\n"
  23. " adcl $0xffff,%0"
  24. : "=r" (sum)
  25. : "r" ((__force u32)sum << 16),
  26. "0" ((__force u32)sum & 0xffff0000));
  27. return (__force __sum16)(~(__force u32)sum >> 16);
  28. }
  29. /*
  30. * This is a version of ip_compute_csum() optimized for IP headers,
  31. * which always checksum on 4 octet boundaries.
  32. *
  33. * By Jorge Cwik <[email protected]>, adapted for linux by
  34. * Arnt Gulbrandsen.
  35. */
  36. /**
  37. * ip_fast_csum - Compute the IPv4 header checksum efficiently.
  38. * iph: ipv4 header
  39. * ihl: length of header / 4
  40. */
  41. static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
  42. {
  43. unsigned int sum;
  44. asm(" movl (%1), %0\n"
  45. " subl $4, %2\n"
  46. " jbe 2f\n"
  47. " addl 4(%1), %0\n"
  48. " adcl 8(%1), %0\n"
  49. " adcl 12(%1), %0\n"
  50. "1: adcl 16(%1), %0\n"
  51. " lea 4(%1), %1\n"
  52. " decl %2\n"
  53. " jne 1b\n"
  54. " adcl $0, %0\n"
  55. " movl %0, %2\n"
  56. " shrl $16, %0\n"
  57. " addw %w2, %w0\n"
  58. " adcl $0, %0\n"
  59. " notl %0\n"
  60. "2:"
  61. /* Since the input registers which are loaded with iph and ihl
  62. are modified, we must also specify them as outputs, or gcc
  63. will assume they contain their original values. */
  64. : "=r" (sum), "=r" (iph), "=r" (ihl)
  65. : "1" (iph), "2" (ihl)
  66. : "memory");
  67. return (__force __sum16)sum;
  68. }
  69. /**
  70. * csum_tcpup_nofold - Compute an IPv4 pseudo header checksum.
  71. * @saddr: source address
  72. * @daddr: destination address
  73. * @len: length of packet
  74. * @proto: ip protocol of packet
  75. * @sum: initial sum to be added in (32bit unfolded)
  76. *
  77. * Returns the pseudo header checksum the input data. Result is
  78. * 32bit unfolded.
  79. */
  80. static inline __wsum
  81. csum_tcpudp_nofold(__be32 saddr, __be32 daddr, __u32 len,
  82. __u8 proto, __wsum sum)
  83. {
  84. asm(" addl %1, %0\n"
  85. " adcl %2, %0\n"
  86. " adcl %3, %0\n"
  87. " adcl $0, %0\n"
  88. : "=r" (sum)
  89. : "g" (daddr), "g" (saddr),
  90. "g" ((len + proto)<<8), "0" (sum));
  91. return sum;
  92. }
  93. /**
  94. * csum_tcpup_magic - Compute an IPv4 pseudo header checksum.
  95. * @saddr: source address
  96. * @daddr: destination address
  97. * @len: length of packet
  98. * @proto: ip protocol of packet
  99. * @sum: initial sum to be added in (32bit unfolded)
  100. *
  101. * Returns the 16bit pseudo header checksum the input data already
  102. * complemented and ready to be filled in.
  103. */
  104. static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
  105. __u32 len, __u8 proto,
  106. __wsum sum)
  107. {
  108. return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
  109. }
  110. /**
  111. * csum_partial - Compute an internet checksum.
  112. * @buff: buffer to be checksummed
  113. * @len: length of buffer.
  114. * @sum: initial sum to be added in (32bit unfolded)
  115. *
  116. * Returns the 32bit unfolded internet checksum of the buffer.
  117. * Before filling it in it needs to be csum_fold()'ed.
  118. * buff should be aligned to a 64bit boundary if possible.
  119. */
  120. extern __wsum csum_partial(const void *buff, int len, __wsum sum);
  121. /* Do not call this directly. Use the wrappers below */
  122. extern __visible __wsum csum_partial_copy_generic(const void *src, void *dst, int len);
  123. extern __wsum csum_and_copy_from_user(const void __user *src, void *dst, int len);
  124. extern __wsum csum_and_copy_to_user(const void *src, void __user *dst, int len);
  125. extern __wsum csum_partial_copy_nocheck(const void *src, void *dst, int len);
  126. /**
  127. * ip_compute_csum - Compute an 16bit IP checksum.
  128. * @buff: buffer address.
  129. * @len: length of buffer.
  130. *
  131. * Returns the 16bit folded/inverted checksum of the passed buffer.
  132. * Ready to fill in.
  133. */
  134. extern __sum16 ip_compute_csum(const void *buff, int len);
  135. /**
  136. * csum_ipv6_magic - Compute checksum of an IPv6 pseudo header.
  137. * @saddr: source address
  138. * @daddr: destination address
  139. * @len: length of packet
  140. * @proto: protocol of packet
  141. * @sum: initial sum (32bit unfolded) to be added in
  142. *
  143. * Computes an IPv6 pseudo header checksum. This sum is added the checksum
  144. * into UDP/TCP packets and contains some link layer information.
  145. * Returns the unfolded 32bit checksum.
  146. */
  147. struct in6_addr;
  148. #define _HAVE_ARCH_IPV6_CSUM 1
  149. extern __sum16
  150. csum_ipv6_magic(const struct in6_addr *saddr, const struct in6_addr *daddr,
  151. __u32 len, __u8 proto, __wsum sum);
  152. static inline unsigned add32_with_carry(unsigned a, unsigned b)
  153. {
  154. asm("addl %2,%0\n\t"
  155. "adcl $0,%0"
  156. : "=r" (a)
  157. : "0" (a), "rm" (b));
  158. return a;
  159. }
  160. #define HAVE_ARCH_CSUM_ADD
  161. static inline __wsum csum_add(__wsum csum, __wsum addend)
  162. {
  163. return (__force __wsum)add32_with_carry((__force unsigned)csum,
  164. (__force unsigned)addend);
  165. }
  166. #endif /* _ASM_X86_CHECKSUM_64_H */