checksum.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #ifndef _I386_CHECKSUM_H
  2. #define _I386_CHECKSUM_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 unsigned int csum_partial(const unsigned char * buff, int len, unsigned int 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 unsigned int csum_partial_copy_generic(const unsigned char *src, unsigned char *dst,
  26. int len, int sum, int *src_err_ptr, int *dst_err_ptr);
  27. /*
  28. * Note: when you get a NULL pointer exception here this means someone
  29. * passed in an incorrect kernel address to one of these functions.
  30. *
  31. * If you use these functions directly please don't forget the
  32. * access_ok().
  33. */
  34. static __inline__
  35. unsigned int csum_partial_copy_nocheck (const unsigned char *src, unsigned char *dst,
  36. int len, int sum)
  37. {
  38. return csum_partial_copy_generic ( src, dst, len, sum, NULL, NULL);
  39. }
  40. static __inline__
  41. unsigned int csum_partial_copy_from_user(const unsigned char __user *src, unsigned char *dst,
  42. int len, int sum, int *err_ptr)
  43. {
  44. might_sleep();
  45. return csum_partial_copy_generic((__force unsigned char *)src, dst,
  46. len, sum, err_ptr, NULL);
  47. }
  48. /*
  49. * This is a version of ip_compute_csum() optimized for IP headers,
  50. * which always checksum on 4 octet boundaries.
  51. *
  52. * By Jorge Cwik <jorge@laser.satlink.net>, adapted for linux by
  53. * Arnt Gulbrandsen.
  54. */
  55. static inline unsigned short ip_fast_csum(unsigned char * iph,
  56. unsigned int ihl)
  57. {
  58. unsigned int sum;
  59. __asm__ __volatile__(
  60. "movl (%1), %0 ;\n"
  61. "subl $4, %2 ;\n"
  62. "jbe 2f ;\n"
  63. "addl 4(%1), %0 ;\n"
  64. "adcl 8(%1), %0 ;\n"
  65. "adcl 12(%1), %0 ;\n"
  66. "1: adcl 16(%1), %0 ;\n"
  67. "lea 4(%1), %1 ;\n"
  68. "decl %2 ;\n"
  69. "jne 1b ;\n"
  70. "adcl $0, %0 ;\n"
  71. "movl %0, %2 ;\n"
  72. "shrl $16, %0 ;\n"
  73. "addw %w2, %w0 ;\n"
  74. "adcl $0, %0 ;\n"
  75. "notl %0 ;\n"
  76. "2: ;\n"
  77. /* Since the input registers which are loaded with iph and ihl
  78. are modified, we must also specify them as outputs, or gcc
  79. will assume they contain their original values. */
  80. : "=r" (sum), "=r" (iph), "=r" (ihl)
  81. : "1" (iph), "2" (ihl)
  82. : "memory");
  83. return(sum);
  84. }
  85. /*
  86. * Fold a partial checksum
  87. */
  88. static inline unsigned int csum_fold(unsigned int sum)
  89. {
  90. __asm__(
  91. "addl %1, %0 ;\n"
  92. "adcl $0xffff, %0 ;\n"
  93. : "=r" (sum)
  94. : "r" (sum << 16), "0" (sum & 0xffff0000)
  95. );
  96. return (~sum) >> 16;
  97. }
  98. static inline unsigned long csum_tcpudp_nofold(unsigned long saddr,
  99. unsigned long daddr,
  100. unsigned short len,
  101. unsigned short proto,
  102. unsigned int sum)
  103. {
  104. __asm__(
  105. "addl %1, %0 ;\n"
  106. "adcl %2, %0 ;\n"
  107. "adcl %3, %0 ;\n"
  108. "adcl $0, %0 ;\n"
  109. : "=r" (sum)
  110. : "g" (daddr), "g"(saddr), "g"((ntohs(len)<<16)+proto*256), "0"(sum));
  111. return sum;
  112. }
  113. /*
  114. * computes the checksum of the TCP/UDP pseudo-header
  115. * returns a 16-bit checksum, already complemented
  116. */
  117. static inline unsigned short int csum_tcpudp_magic(unsigned long saddr,
  118. unsigned long daddr,
  119. unsigned short len,
  120. unsigned short proto,
  121. unsigned int sum)
  122. {
  123. return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
  124. }
  125. /*
  126. * this routine is used for miscellaneous IP-like checksums, mainly
  127. * in icmp.c
  128. */
  129. static inline unsigned short ip_compute_csum(unsigned char * buff, int len)
  130. {
  131. return csum_fold (csum_partial(buff, len, 0));
  132. }
  133. #define _HAVE_ARCH_IPV6_CSUM
  134. static __inline__ unsigned short int csum_ipv6_magic(struct in6_addr *saddr,
  135. struct in6_addr *daddr,
  136. __u32 len,
  137. unsigned short proto,
  138. unsigned int sum)
  139. {
  140. __asm__(
  141. "addl 0(%1), %0 ;\n"
  142. "adcl 4(%1), %0 ;\n"
  143. "adcl 8(%1), %0 ;\n"
  144. "adcl 12(%1), %0 ;\n"
  145. "adcl 0(%2), %0 ;\n"
  146. "adcl 4(%2), %0 ;\n"
  147. "adcl 8(%2), %0 ;\n"
  148. "adcl 12(%2), %0 ;\n"
  149. "adcl %3, %0 ;\n"
  150. "adcl %4, %0 ;\n"
  151. "adcl $0, %0 ;\n"
  152. : "=&r" (sum)
  153. : "r" (saddr), "r" (daddr),
  154. "r"(htonl(len)), "r"(htonl(proto)), "0"(sum));
  155. return csum_fold(sum);
  156. }
  157. /*
  158. * Copy and checksum to user
  159. */
  160. #define HAVE_CSUM_COPY_USER
  161. static __inline__ unsigned int csum_and_copy_to_user(const unsigned char *src,
  162. unsigned char __user *dst,
  163. int len, int sum,
  164. int *err_ptr)
  165. {
  166. might_sleep();
  167. if (access_ok(VERIFY_WRITE, dst, len))
  168. return csum_partial_copy_generic(src, (__force unsigned char *)dst, len, sum, NULL, err_ptr);
  169. if (len)
  170. *err_ptr = -EFAULT;
  171. return -1; /* invalid checksum */
  172. }
  173. #endif