checksum.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. unsigned int csum_partial(const unsigned char * buff, int len,
  21. unsigned int sum);
  22. /*
  23. * Note: when you get a NULL pointer exception here this means someone
  24. * passed in an incorrect kernel address to one of these functions.
  25. *
  26. * If you use these functions directly please don't forget the
  27. * access_ok().
  28. */
  29. static __inline__
  30. unsigned int csum_partial_copy_nocheck(const unsigned char *src, unsigned char *dst,
  31. int len, int sum)
  32. {
  33. memcpy(dst, src, len);
  34. return csum_partial(dst, len, sum);
  35. }
  36. /*
  37. * the same as csum_partial, but copies from src while it
  38. * checksums, and handles user-space pointer exceptions correctly, when needed.
  39. *
  40. * here even more important to align src and dst on a 32-bit (or even
  41. * better 64-bit) boundary
  42. */
  43. static __inline__
  44. unsigned int csum_partial_copy_from_user(const unsigned char __user *src,
  45. unsigned char *dst,
  46. int len, int sum, int *err_ptr)
  47. {
  48. if(copy_from_user(dst, src, len)){
  49. *err_ptr = -EFAULT;
  50. return(-1);
  51. }
  52. return csum_partial(dst, len, sum);
  53. }
  54. /*
  55. * These are the old (and unsafe) way of doing checksums, a warning message
  56. * will be printed if they are used and an exception occurs.
  57. *
  58. * these functions should go away after some time.
  59. */
  60. #define csum_partial_copy_fromuser csum_partial_copy_from_user
  61. /*
  62. * This is a version of ip_compute_csum() optimized for IP headers,
  63. * which always checksum on 4 octet boundaries.
  64. *
  65. * By Jorge Cwik <jorge@laser.satlink.net>, adapted for linux by
  66. * Arnt Gulbrandsen.
  67. */
  68. static inline unsigned short ip_fast_csum(unsigned char * iph,
  69. unsigned int ihl)
  70. {
  71. unsigned int sum;
  72. __asm__ __volatile__(
  73. "movl (%1), %0 ;\n"
  74. "subl $4, %2 ;\n"
  75. "jbe 2f ;\n"
  76. "addl 4(%1), %0 ;\n"
  77. "adcl 8(%1), %0 ;\n"
  78. "adcl 12(%1), %0 ;\n"
  79. "1: adcl 16(%1), %0 ;\n"
  80. "lea 4(%1), %1 ;\n"
  81. "decl %2 ;\n"
  82. "jne 1b ;\n"
  83. "adcl $0, %0 ;\n"
  84. "movl %0, %2 ;\n"
  85. "shrl $16, %0 ;\n"
  86. "addw %w2, %w0 ;\n"
  87. "adcl $0, %0 ;\n"
  88. "notl %0 ;\n"
  89. "2: ;\n"
  90. /* Since the input registers which are loaded with iph and ipl
  91. are modified, we must also specify them as outputs, or gcc
  92. will assume they contain their original values. */
  93. : "=r" (sum), "=r" (iph), "=r" (ihl)
  94. : "1" (iph), "2" (ihl)
  95. : "memory");
  96. return sum;
  97. }
  98. /*
  99. * Fold a partial checksum
  100. */
  101. static inline unsigned int csum_fold(unsigned int sum)
  102. {
  103. __asm__(
  104. "addl %1, %0 ;\n"
  105. "adcl $0xffff, %0 ;\n"
  106. : "=r" (sum)
  107. : "r" (sum << 16), "0" (sum & 0xffff0000)
  108. );
  109. return (~sum) >> 16;
  110. }
  111. static inline unsigned long csum_tcpudp_nofold(unsigned long saddr,
  112. unsigned long daddr,
  113. unsigned short len,
  114. unsigned short proto,
  115. unsigned int sum)
  116. {
  117. __asm__(
  118. "addl %1, %0 ;\n"
  119. "adcl %2, %0 ;\n"
  120. "adcl %3, %0 ;\n"
  121. "adcl $0, %0 ;\n"
  122. : "=r" (sum)
  123. : "g" (daddr), "g"(saddr), "g"((ntohs(len)<<16)+proto*256), "0"(sum));
  124. return sum;
  125. }
  126. /*
  127. * computes the checksum of the TCP/UDP pseudo-header
  128. * returns a 16-bit checksum, already complemented
  129. */
  130. static inline unsigned short int csum_tcpudp_magic(unsigned long saddr,
  131. unsigned long daddr,
  132. unsigned short len,
  133. unsigned short proto,
  134. unsigned int sum)
  135. {
  136. return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
  137. }
  138. /*
  139. * this routine is used for miscellaneous IP-like checksums, mainly
  140. * in icmp.c
  141. */
  142. static inline unsigned short ip_compute_csum(unsigned char * buff, int len)
  143. {
  144. return csum_fold (csum_partial(buff, len, 0));
  145. }
  146. #define _HAVE_ARCH_IPV6_CSUM
  147. static __inline__ unsigned short int csum_ipv6_magic(struct in6_addr *saddr,
  148. struct in6_addr *daddr,
  149. __u32 len,
  150. unsigned short proto,
  151. unsigned int sum)
  152. {
  153. __asm__(
  154. "addl 0(%1), %0 ;\n"
  155. "adcl 4(%1), %0 ;\n"
  156. "adcl 8(%1), %0 ;\n"
  157. "adcl 12(%1), %0 ;\n"
  158. "adcl 0(%2), %0 ;\n"
  159. "adcl 4(%2), %0 ;\n"
  160. "adcl 8(%2), %0 ;\n"
  161. "adcl 12(%2), %0 ;\n"
  162. "adcl %3, %0 ;\n"
  163. "adcl %4, %0 ;\n"
  164. "adcl $0, %0 ;\n"
  165. : "=&r" (sum)
  166. : "r" (saddr), "r" (daddr),
  167. "r"(htonl(len)), "r"(htonl(proto)), "0"(sum));
  168. return csum_fold(sum);
  169. }
  170. /*
  171. * Copy and checksum to user
  172. */
  173. #define HAVE_CSUM_COPY_USER
  174. static __inline__ unsigned int csum_and_copy_to_user(const unsigned char *src,
  175. unsigned char __user *dst,
  176. int len, int sum, int *err_ptr)
  177. {
  178. if (access_ok(VERIFY_WRITE, dst, len)){
  179. if(copy_to_user(dst, src, len)){
  180. *err_ptr = -EFAULT;
  181. return(-1);
  182. }
  183. return csum_partial(src, len, sum);
  184. }
  185. if (len)
  186. *err_ptr = -EFAULT;
  187. return -1; /* invalid checksum */
  188. }
  189. #endif
  190. /*
  191. * Overrides for Emacs so that we follow Linus's tabbing style.
  192. * Emacs will notice this stuff at the end of the file and automatically
  193. * adjust the settings for this buffer only. This must remain at the end
  194. * of the file.
  195. * ---------------------------------------------------------------------------
  196. * Local variables:
  197. * c-file-style: "linux"
  198. * End:
  199. */