checksum_32.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Licensed under the GPL
  3. */
  4. #ifndef __UM_SYSDEP_CHECKSUM_H
  5. #define __UM_SYSDEP_CHECKSUM_H
  6. #include "linux/in6.h"
  7. #include "linux/string.h"
  8. /*
  9. * computes the checksum of a memory block at buff, length len,
  10. * and adds in "sum" (32-bit)
  11. *
  12. * returns a 32-bit number suitable for feeding into itself
  13. * or csum_tcpudp_magic
  14. *
  15. * this function must be called with even lengths, except
  16. * for the last fragment, which may be odd
  17. *
  18. * it's best to have buff aligned on a 32-bit boundary
  19. */
  20. __wsum csum_partial(const void *buff, int len, __wsum sum);
  21. /*
  22. * Note: when you get a NULL pointer exception here this means someone
  23. * passed in an incorrect kernel address to one of these functions.
  24. *
  25. * If you use these functions directly please don't forget the
  26. * access_ok().
  27. */
  28. static __inline__
  29. __wsum csum_partial_copy_nocheck(const void *src, void *dst,
  30. int len, __wsum sum)
  31. {
  32. memcpy(dst, src, len);
  33. return csum_partial(dst, len, sum);
  34. }
  35. /*
  36. * the same as csum_partial, but copies from src while it
  37. * checksums, and handles user-space pointer exceptions correctly, when needed.
  38. *
  39. * here even more important to align src and dst on a 32-bit (or even
  40. * better 64-bit) boundary
  41. */
  42. static __inline__
  43. __wsum csum_partial_copy_from_user(const void __user *src, void *dst,
  44. int len, __wsum sum, int *err_ptr)
  45. {
  46. if (copy_from_user(dst, src, len)) {
  47. *err_ptr = -EFAULT;
  48. return (__force __wsum)-1;
  49. }
  50. return csum_partial(dst, len, sum);
  51. }
  52. /*
  53. * This is a version of ip_compute_csum() optimized for IP headers,
  54. * which always checksum on 4 octet boundaries.
  55. *
  56. * By Jorge Cwik <jorge@laser.satlink.net>, adapted for linux by
  57. * Arnt Gulbrandsen.
  58. */
  59. static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
  60. {
  61. unsigned int sum;
  62. __asm__ __volatile__(
  63. "movl (%1), %0 ;\n"
  64. "subl $4, %2 ;\n"
  65. "jbe 2f ;\n"
  66. "addl 4(%1), %0 ;\n"
  67. "adcl 8(%1), %0 ;\n"
  68. "adcl 12(%1), %0 ;\n"
  69. "1: adcl 16(%1), %0 ;\n"
  70. "lea 4(%1), %1 ;\n"
  71. "decl %2 ;\n"
  72. "jne 1b ;\n"
  73. "adcl $0, %0 ;\n"
  74. "movl %0, %2 ;\n"
  75. "shrl $16, %0 ;\n"
  76. "addw %w2, %w0 ;\n"
  77. "adcl $0, %0 ;\n"
  78. "notl %0 ;\n"
  79. "2: ;\n"
  80. /* Since the input registers which are loaded with iph and ipl
  81. are modified, we must also specify them as outputs, or gcc
  82. will assume they contain their original values. */
  83. : "=r" (sum), "=r" (iph), "=r" (ihl)
  84. : "1" (iph), "2" (ihl)
  85. : "memory");
  86. return (__force __sum16)sum;
  87. }
  88. /*
  89. * Fold a partial checksum
  90. */
  91. static inline __sum16 csum_fold(__wsum sum)
  92. {
  93. __asm__(
  94. "addl %1, %0 ;\n"
  95. "adcl $0xffff, %0 ;\n"
  96. : "=r" (sum)
  97. : "r" ((__force u32)sum << 16),
  98. "0" ((__force u32)sum & 0xffff0000)
  99. );
  100. return (__force __sum16)(~(__force u32)sum >> 16);
  101. }
  102. static inline __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
  103. unsigned short len,
  104. unsigned short proto,
  105. __wsum sum)
  106. {
  107. __asm__(
  108. "addl %1, %0 ;\n"
  109. "adcl %2, %0 ;\n"
  110. "adcl %3, %0 ;\n"
  111. "adcl $0, %0 ;\n"
  112. : "=r" (sum)
  113. : "g" (daddr), "g"(saddr), "g"((len + proto) << 8), "0"(sum));
  114. return sum;
  115. }
  116. /*
  117. * computes the checksum of the TCP/UDP pseudo-header
  118. * returns a 16-bit checksum, already complemented
  119. */
  120. static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
  121. unsigned short len,
  122. unsigned short proto,
  123. __wsum sum)
  124. {
  125. return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
  126. }
  127. /*
  128. * this routine is used for miscellaneous IP-like checksums, mainly
  129. * in icmp.c
  130. */
  131. static inline __sum16 ip_compute_csum(const void *buff, int len)
  132. {
  133. return csum_fold (csum_partial(buff, len, 0));
  134. }
  135. #define _HAVE_ARCH_IPV6_CSUM
  136. static __inline__ __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
  137. const struct in6_addr *daddr,
  138. __u32 len, unsigned short proto,
  139. __wsum sum)
  140. {
  141. __asm__(
  142. "addl 0(%1), %0 ;\n"
  143. "adcl 4(%1), %0 ;\n"
  144. "adcl 8(%1), %0 ;\n"
  145. "adcl 12(%1), %0 ;\n"
  146. "adcl 0(%2), %0 ;\n"
  147. "adcl 4(%2), %0 ;\n"
  148. "adcl 8(%2), %0 ;\n"
  149. "adcl 12(%2), %0 ;\n"
  150. "adcl %3, %0 ;\n"
  151. "adcl %4, %0 ;\n"
  152. "adcl $0, %0 ;\n"
  153. : "=&r" (sum)
  154. : "r" (saddr), "r" (daddr),
  155. "r"(htonl(len)), "r"(htonl(proto)), "0"(sum));
  156. return csum_fold(sum);
  157. }
  158. /*
  159. * Copy and checksum to user
  160. */
  161. #define HAVE_CSUM_COPY_USER
  162. static __inline__ __wsum csum_and_copy_to_user(const void *src,
  163. void __user *dst,
  164. int len, __wsum sum, int *err_ptr)
  165. {
  166. if (access_ok(VERIFY_WRITE, dst, len)) {
  167. if (copy_to_user(dst, src, len)) {
  168. *err_ptr = -EFAULT;
  169. return (__force __wsum)-1;
  170. }
  171. return csum_partial(src, len, sum);
  172. }
  173. if (len)
  174. *err_ptr = -EFAULT;
  175. return (__force __wsum)-1; /* invalid checksum */
  176. }
  177. #endif