checksum.h 3.8 KB

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