checksum.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #ifndef __UM_CHECKSUM_H
  2. #define __UM_CHECKSUM_H
  3. #include <linux/string.h>
  4. #include <linux/in6.h>
  5. /*
  6. * computes the checksum of a memory block at buff, length len,
  7. * and adds in "sum" (32-bit)
  8. *
  9. * returns a 32-bit number suitable for feeding into itself
  10. * or csum_tcpudp_magic
  11. *
  12. * this function must be called with even lengths, except
  13. * for the last fragment, which may be odd
  14. *
  15. * it's best to have buff aligned on a 32-bit boundary
  16. */
  17. extern __wsum csum_partial(const void *buff, int len, __wsum sum);
  18. /*
  19. * Note: when you get a NULL pointer exception here this means someone
  20. * passed in an incorrect kernel address to one of these functions.
  21. *
  22. * If you use these functions directly please don't forget the
  23. * access_ok().
  24. */
  25. static __inline__
  26. __wsum csum_partial_copy_nocheck(const void *src, void *dst,
  27. int len, __wsum sum)
  28. {
  29. memcpy(dst, src, len);
  30. return csum_partial(dst, len, sum);
  31. }
  32. /*
  33. * the same as csum_partial, but copies from src while it
  34. * checksums, and handles user-space pointer exceptions correctly, when needed.
  35. *
  36. * here even more important to align src and dst on a 32-bit (or even
  37. * better 64-bit) boundary
  38. */
  39. static __inline__
  40. __wsum csum_partial_copy_from_user(const void __user *src, void *dst,
  41. int len, __wsum sum, int *err_ptr)
  42. {
  43. if (copy_from_user(dst, src, len)) {
  44. *err_ptr = -EFAULT;
  45. return (__force __wsum)-1;
  46. }
  47. return csum_partial(dst, len, sum);
  48. }
  49. /**
  50. * csum_fold - Fold and invert a 32bit checksum.
  51. * sum: 32bit unfolded sum
  52. *
  53. * Fold a 32bit running checksum to 16bit and invert it. This is usually
  54. * the last step before putting a checksum into a packet.
  55. * Make sure not to mix with 64bit checksums.
  56. */
  57. static inline __sum16 csum_fold(__wsum sum)
  58. {
  59. __asm__(
  60. " addl %1,%0\n"
  61. " adcl $0xffff,%0"
  62. : "=r" (sum)
  63. : "r" ((__force u32)sum << 16),
  64. "0" ((__force u32)sum & 0xffff0000)
  65. );
  66. return (__force __sum16)(~(__force u32)sum >> 16);
  67. }
  68. /**
  69. * csum_tcpup_nofold - Compute an IPv4 pseudo header checksum.
  70. * @saddr: source address
  71. * @daddr: destination address
  72. * @len: length of packet
  73. * @proto: ip protocol of packet
  74. * @sum: initial sum to be added in (32bit unfolded)
  75. *
  76. * Returns the pseudo header checksum the input data. Result is
  77. * 32bit unfolded.
  78. */
  79. static inline __wsum
  80. csum_tcpudp_nofold(__be32 saddr, __be32 daddr, unsigned short len,
  81. unsigned short proto, __wsum sum)
  82. {
  83. asm(" addl %1, %0\n"
  84. " adcl %2, %0\n"
  85. " adcl %3, %0\n"
  86. " adcl $0, %0\n"
  87. : "=r" (sum)
  88. : "g" (daddr), "g" (saddr), "g" ((len + proto) << 8), "0" (sum));
  89. return sum;
  90. }
  91. /*
  92. * computes the checksum of the TCP/UDP pseudo-header
  93. * returns a 16-bit checksum, already complemented
  94. */
  95. static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
  96. unsigned short len,
  97. unsigned short proto,
  98. __wsum sum)
  99. {
  100. return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
  101. }
  102. /**
  103. * ip_fast_csum - Compute the IPv4 header checksum efficiently.
  104. * iph: ipv4 header
  105. * ihl: length of header / 4
  106. */
  107. static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
  108. {
  109. unsigned int sum;
  110. asm( " movl (%1), %0\n"
  111. " subl $4, %2\n"
  112. " jbe 2f\n"
  113. " addl 4(%1), %0\n"
  114. " adcl 8(%1), %0\n"
  115. " adcl 12(%1), %0\n"
  116. "1: adcl 16(%1), %0\n"
  117. " lea 4(%1), %1\n"
  118. " decl %2\n"
  119. " jne 1b\n"
  120. " adcl $0, %0\n"
  121. " movl %0, %2\n"
  122. " shrl $16, %0\n"
  123. " addw %w2, %w0\n"
  124. " adcl $0, %0\n"
  125. " notl %0\n"
  126. "2:"
  127. /* Since the input registers which are loaded with iph and ipl
  128. are modified, we must also specify them as outputs, or gcc
  129. will assume they contain their original values. */
  130. : "=r" (sum), "=r" (iph), "=r" (ihl)
  131. : "1" (iph), "2" (ihl)
  132. : "memory");
  133. return (__force __sum16)sum;
  134. }
  135. #ifdef CONFIG_X86_32
  136. # include "checksum_32.h"
  137. #else
  138. # include "checksum_64.h"
  139. #endif
  140. #endif