checksum.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __UM_CHECKSUM_H
  3. #define __UM_CHECKSUM_H
  4. #include <linux/string.h>
  5. #include <linux/in6.h>
  6. #include <linux/uaccess.h>
  7. /*
  8. * computes the checksum of a memory block at buff, length len,
  9. * and adds in "sum" (32-bit)
  10. *
  11. * returns a 32-bit number suitable for feeding into itself
  12. * or csum_tcpudp_magic
  13. *
  14. * this function must be called with even lengths, except
  15. * for the last fragment, which may be odd
  16. *
  17. * it's best to have buff aligned on a 32-bit boundary
  18. */
  19. extern __wsum csum_partial(const void *buff, int len, __wsum sum);
  20. /**
  21. * csum_fold - Fold and invert a 32bit checksum.
  22. * sum: 32bit unfolded sum
  23. *
  24. * Fold a 32bit running checksum to 16bit and invert it. This is usually
  25. * the last step before putting a checksum into a packet.
  26. * Make sure not to mix with 64bit checksums.
  27. */
  28. static inline __sum16 csum_fold(__wsum sum)
  29. {
  30. __asm__(
  31. " addl %1,%0\n"
  32. " adcl $0xffff,%0"
  33. : "=r" (sum)
  34. : "r" ((__force u32)sum << 16),
  35. "0" ((__force u32)sum & 0xffff0000)
  36. );
  37. return (__force __sum16)(~(__force u32)sum >> 16);
  38. }
  39. /**
  40. * csum_tcpup_nofold - Compute an IPv4 pseudo header checksum.
  41. * @saddr: source address
  42. * @daddr: destination address
  43. * @len: length of packet
  44. * @proto: ip protocol of packet
  45. * @sum: initial sum to be added in (32bit unfolded)
  46. *
  47. * Returns the pseudo header checksum the input data. Result is
  48. * 32bit unfolded.
  49. */
  50. static inline __wsum
  51. csum_tcpudp_nofold(__be32 saddr, __be32 daddr, __u32 len,
  52. __u8 proto, __wsum sum)
  53. {
  54. asm(" addl %1, %0\n"
  55. " adcl %2, %0\n"
  56. " adcl %3, %0\n"
  57. " adcl $0, %0\n"
  58. : "=r" (sum)
  59. : "g" (daddr), "g" (saddr), "g" ((len + proto) << 8), "0" (sum));
  60. return sum;
  61. }
  62. /*
  63. * computes the checksum of the TCP/UDP pseudo-header
  64. * returns a 16-bit checksum, already complemented
  65. */
  66. static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
  67. __u32 len, __u8 proto,
  68. __wsum sum)
  69. {
  70. return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
  71. }
  72. /**
  73. * ip_fast_csum - Compute the IPv4 header checksum efficiently.
  74. * iph: ipv4 header
  75. * ihl: length of header / 4
  76. */
  77. static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
  78. {
  79. unsigned int sum;
  80. asm( " movl (%1), %0\n"
  81. " subl $4, %2\n"
  82. " jbe 2f\n"
  83. " addl 4(%1), %0\n"
  84. " adcl 8(%1), %0\n"
  85. " adcl 12(%1), %0\n"
  86. "1: adcl 16(%1), %0\n"
  87. " lea 4(%1), %1\n"
  88. " decl %2\n"
  89. " jne 1b\n"
  90. " adcl $0, %0\n"
  91. " movl %0, %2\n"
  92. " shrl $16, %0\n"
  93. " addw %w2, %w0\n"
  94. " adcl $0, %0\n"
  95. " notl %0\n"
  96. "2:"
  97. /* Since the input registers which are loaded with iph and ipl
  98. are modified, we must also specify them as outputs, or gcc
  99. will assume they contain their original values. */
  100. : "=r" (sum), "=r" (iph), "=r" (ihl)
  101. : "1" (iph), "2" (ihl)
  102. : "memory");
  103. return (__force __sum16)sum;
  104. }
  105. #ifdef CONFIG_X86_32
  106. # include "checksum_32.h"
  107. #else
  108. # include "checksum_64.h"
  109. #endif
  110. #endif