checksum_32.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #ifndef _ASM_X86_CHECKSUM_32_H
  2. #define _ASM_X86_CHECKSUM_32_H
  3. #include <linux/in6.h>
  4. #include <asm/uaccess.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. asmlinkage __wsum csum_partial(const void *buff, int len, __wsum sum);
  18. /*
  19. * the same as csum_partial, but copies from src while it
  20. * checksums, and handles user-space pointer exceptions correctly, when needed.
  21. *
  22. * here even more important to align src and dst on a 32-bit (or even
  23. * better 64-bit) boundary
  24. */
  25. asmlinkage __wsum csum_partial_copy_generic(const void *src, void *dst,
  26. int len, __wsum sum,
  27. int *src_err_ptr, int *dst_err_ptr);
  28. /*
  29. * Note: when you get a NULL pointer exception here this means someone
  30. * passed in an incorrect kernel address to one of these functions.
  31. *
  32. * If you use these functions directly please don't forget the
  33. * access_ok().
  34. */
  35. static inline __wsum csum_partial_copy_nocheck(const void *src, void *dst,
  36. int len, __wsum sum)
  37. {
  38. return csum_partial_copy_generic(src, dst, len, sum, NULL, NULL);
  39. }
  40. static inline __wsum csum_partial_copy_from_user(const void __user *src,
  41. void *dst,
  42. int len, __wsum sum,
  43. int *err_ptr)
  44. {
  45. might_sleep();
  46. return csum_partial_copy_generic((__force void *)src, dst,
  47. len, sum, err_ptr, NULL);
  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 <jorge@laser.satlink.net>, 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. unsigned short len,
  98. unsigned short proto,
  99. __wsum sum)
  100. {
  101. asm("addl %1, %0 ;\n"
  102. "adcl %2, %0 ;\n"
  103. "adcl %3, %0 ;\n"
  104. "adcl $0, %0 ;\n"
  105. : "=r" (sum)
  106. : "g" (daddr), "g"(saddr),
  107. "g" ((len + proto) << 8), "0" (sum));
  108. return sum;
  109. }
  110. /*
  111. * computes the checksum of the TCP/UDP pseudo-header
  112. * returns a 16-bit checksum, already complemented
  113. */
  114. static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
  115. unsigned short len,
  116. unsigned short proto,
  117. __wsum sum)
  118. {
  119. return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
  120. }
  121. /*
  122. * this routine is used for miscellaneous IP-like checksums, mainly
  123. * in icmp.c
  124. */
  125. static inline __sum16 ip_compute_csum(const void *buff, int len)
  126. {
  127. return csum_fold(csum_partial(buff, len, 0));
  128. }
  129. #define _HAVE_ARCH_IPV6_CSUM
  130. static inline __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
  131. const struct in6_addr *daddr,
  132. __u32 len, unsigned short proto,
  133. __wsum sum)
  134. {
  135. asm("addl 0(%1), %0 ;\n"
  136. "adcl 4(%1), %0 ;\n"
  137. "adcl 8(%1), %0 ;\n"
  138. "adcl 12(%1), %0 ;\n"
  139. "adcl 0(%2), %0 ;\n"
  140. "adcl 4(%2), %0 ;\n"
  141. "adcl 8(%2), %0 ;\n"
  142. "adcl 12(%2), %0 ;\n"
  143. "adcl %3, %0 ;\n"
  144. "adcl %4, %0 ;\n"
  145. "adcl $0, %0 ;\n"
  146. : "=&r" (sum)
  147. : "r" (saddr), "r" (daddr),
  148. "r" (htonl(len)), "r" (htonl(proto)), "0" (sum)
  149. : "memory");
  150. return csum_fold(sum);
  151. }
  152. /*
  153. * Copy and checksum to user
  154. */
  155. #define HAVE_CSUM_COPY_USER
  156. static inline __wsum csum_and_copy_to_user(const void *src,
  157. void __user *dst,
  158. int len, __wsum sum,
  159. int *err_ptr)
  160. {
  161. might_sleep();
  162. if (access_ok(VERIFY_WRITE, dst, len))
  163. return csum_partial_copy_generic(src, (__force void *)dst,
  164. len, sum, NULL, err_ptr);
  165. if (len)
  166. *err_ptr = -EFAULT;
  167. return (__force __wsum)-1; /* invalid checksum */
  168. }
  169. #endif /* _ASM_X86_CHECKSUM_32_H */