checksum.h
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:6k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * This file is subject to the terms and conditions of the GNU General Public
  3.  * License.  See the file "COPYING" in the main directory of this archive
  4.  * for more details.
  5.  *
  6.  * Copyright (C) 1995, 1996, 1997, 1998, 1999 by Ralf Baechle
  7.  * Copyright (C) 1999 Silicon Graphics, Inc.
  8.  * Copyright (C) 2001 Thiemo Seufer.
  9.  * Copyright (C) 2002  Maciej W. Rozycki
  10.  */
  11. #ifndef _ASM_CHECKSUM_H
  12. #define _ASM_CHECKSUM_H
  13. #include <asm/uaccess.h>
  14. /*
  15.  * computes the checksum of a memory block at buff, length len,
  16.  * and adds in "sum" (32-bit)
  17.  *
  18.  * returns a 32-bit number suitable for feeding into itself
  19.  * or csum_tcpudp_magic
  20.  *
  21.  * this function must be called with even lengths, except
  22.  * for the last fragment, which may be odd
  23.  *
  24.  * it's best to have buff aligned on a 32-bit boundary
  25.  */
  26. unsigned int csum_partial(const unsigned char *buff, int len, unsigned int sum);
  27. /*
  28.  * this is a new version of the above that records errors it finds in *errp,
  29.  * but continues and zeros the rest of the buffer.
  30.  */
  31. #define csum_partial_copy_nocheck csum_partial_copy
  32. /*
  33.  * this is a new version of the above that records errors it finds in *errp,
  34.  * but continues and zeros the rest of the buffer.
  35.  */
  36. unsigned int csum_partial_copy_from_user(const char *src, char *dst, int len,
  37.                                          unsigned int sum, int *errp);
  38. /*
  39.  * Copy and checksum to user
  40.  */
  41. #define HAVE_CSUM_COPY_USER
  42. static inline unsigned int csum_and_copy_to_user (const char *src, char *dst,
  43.   int len, int sum,
  44.   int *err_ptr)
  45. {
  46. sum = csum_partial(src, len, sum);
  47. if (copy_to_user(dst, src, len)) {
  48. *err_ptr = -EFAULT;
  49. return -1;
  50. }
  51. return sum;
  52. }
  53. /*
  54.  * the same as csum_partial, but copies from user space (but on MIPS
  55.  * we have just one address space, so this is identical to the above)
  56.  *
  57.  * this is obsolete and will go away.
  58.  */
  59. #define csum_partial_copy_fromuser csum_partial_copy
  60. unsigned int csum_partial_copy(const char *src, char *dst, int len,
  61.        unsigned int sum);
  62. /*
  63.  * Fold a partial checksum without adding pseudo headers
  64.  */
  65. static inline unsigned short int csum_fold(unsigned int sum)
  66. {
  67. __asm__(
  68. ".settnoatttt# csum_foldnt"
  69. "sllt$1,%0,16nt"
  70. "addut%0,$1nt"
  71. "sltut$1,%0,$1nt"
  72. "srlt%0,%0,16nt"
  73. "addut%0,$1nt"
  74. "xorit%0,0xffffnt"
  75. ".settat"
  76. : "=r" (sum)
  77. : "0" (sum));
  78.   return sum;
  79. }
  80. /*
  81.  * This is a version of ip_compute_csum() optimized for IP headers,
  82.  * which always checksum on 4 octet boundaries.
  83.  *
  84.  * By Jorge Cwik <jorge@laser.satlink.net>, adapted for linux by
  85.  * Arnt Gulbrandsen.
  86.  */
  87. static inline unsigned short ip_fast_csum(unsigned char *iph,
  88.   unsigned int ihl)
  89. {
  90. unsigned int sum;
  91. unsigned long dummy;
  92. /*
  93.  * This is for 32-bit processors ...  but works just fine for 64-bit
  94.  * processors for now ...  XXX
  95.  */
  96. __asm__ __volatile__(
  97. ".settnoreorderttt# ip_fast_csumnt"
  98. ".settnoatnt"
  99. "lwt%0, (%1)nt"
  100. "subut%2, 4nt"
  101. "dsllt%2, 2nt"
  102. "lwt%3, 4(%1)nt"
  103. "daddut%2, %1nt"
  104. "addut%0, %3nt"
  105. "sltut$1, %0, %3nt"
  106. "lwt%3, 8(%1)nt"
  107. "addut%0, $1nt"
  108. "addut%0, %3nt"
  109. "sltut$1, %0, %3nt"
  110. "lwt%3, 12(%1)nt"
  111. "addut%0, $1nt"
  112. "addut%0, %3nt"
  113. "sltut$1, %0, %3nt"
  114. "addut%0, $1n"
  115. "1:tlwt%3, 16(%1)nt"
  116. "daddiut%1, 4n"
  117. "addut%0, %3nt"
  118. "sltut$1, %0, %3nt"
  119. "bnet%2, %1, 1bnt"
  120. " addut%0, $1n"
  121. "2:t.settatnt"
  122. ".settreorder"
  123. : "=&r" (sum), "=&r" (iph), "=&r" (ihl), "=&r" (dummy)
  124. : "1" (iph), "2" (ihl));
  125. return csum_fold(sum);
  126. }
  127. /*
  128.  * computes the checksum of the TCP/UDP pseudo-header
  129.  * returns a 16-bit checksum, already complemented
  130.  *
  131.  * Cast unsigned short expressions to unsigned long explicitly
  132.  * to avoid surprises resulting from implicit promotions to
  133.  * signed int.  --macro
  134.  */
  135. static inline unsigned long csum_tcpudp_nofold(unsigned long saddr,
  136.        unsigned long daddr,
  137.        unsigned short len,
  138.        unsigned short proto,
  139.        unsigned int sum)
  140. {
  141. __asm__(
  142. ".settnoatttt# csum_tcpudp_nofoldnt"
  143. "daddut%0, %2nt"
  144. "daddut%0, %3nt"
  145. "daddut%0, %4nt"
  146. "dsll32t$1, %0, 0nt"
  147. "daddut%0, $1nt"
  148. "dsrl32t%0, %0, 0nt"
  149. ".settat"
  150. : "=&r" (sum)
  151. : "0" (daddr), "r"(saddr),
  152. #ifdef __MIPSEL__
  153.   "r" (((unsigned long)ntohs(len)<<16)+proto*256),
  154. #else
  155.   "r" (((unsigned long)(proto)<<16)+len),
  156. #endif
  157.   "r" (sum));
  158. return sum;
  159. }
  160. /*
  161.  * computes the checksum of the TCP/UDP pseudo-header
  162.  * returns a 16-bit checksum, already complemented
  163.  */
  164. static inline unsigned short int csum_tcpudp_magic(unsigned long saddr,
  165.    unsigned long daddr,
  166.    unsigned short len,
  167.    unsigned short proto,
  168.    unsigned int sum)
  169. {
  170. return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
  171. }
  172. /*
  173.  * this routine is used for miscellaneous IP-like checksums, mainly
  174.  * in icmp.c
  175.  */
  176. static inline unsigned short ip_compute_csum(unsigned char * buff, int len)
  177. {
  178. return csum_fold(csum_partial(buff, len, 0));
  179. }
  180. #define _HAVE_ARCH_IPV6_CSUM
  181. static __inline__ unsigned short int csum_ipv6_magic(struct in6_addr *saddr,
  182.      struct in6_addr *daddr,
  183.      __u32 len,
  184.      unsigned short proto,
  185.      unsigned int sum)
  186. {
  187. __asm__(
  188. ".settnoreorderttt# csum_ipv6_magicnt"
  189. ".settnoatnt"
  190. "addut%0, %5ttt# proto (long in network byte order)nt"
  191. "sltut$1, %0, %5nt"
  192. "addut%0, $1nt"
  193. "addut%0, %6ttt# csumnt"
  194. "sltut$1, %0, %6nt"
  195. "lwt%1, 0(%2)ttt# four words source addressnt"
  196. "addut%0, $1nt"
  197. "addut%0, %1nt"
  198. "sltut$1, %0, %1nt"
  199. "lwt%1, 4(%2)nt"
  200. "addut%0, $1nt"
  201. "addut%0, %1nt"
  202. "sltut$1, %0, %1nt"
  203. "lwt%1, 8(%2)nt"
  204. "addut%0, $1nt"
  205. "addut%0, %1nt"
  206. "sltut$1, %0, %1nt"
  207. "lwt%1, 12(%2)nt"
  208. "addut%0, $1nt"
  209. "addut%0, %1nt"
  210. "sltut$1, %0, %1nt"
  211. "lwt%1, 0(%3)nt"
  212. "addut%0, $1nt"
  213. "addut%0, %1nt"
  214. "sltut$1, %0, %1nt"
  215. "lwt%1, 4(%3)nt"
  216. "addut%0, $1nt"
  217. "addut%0, %1nt"
  218. "sltut$1, %0, %1nt"
  219. "lwt%1, 8(%3)nt"
  220. "addut%0, $1nt"
  221. "addut%0, %1nt"
  222. "sltut$1, %0, %1nt"
  223. "lwt%1, 12(%3)nt"
  224. "addut%0, $1nt"
  225. "addut%0, %1nt"
  226. "sltut$1, %0, %1nt"
  227. "addut%0, $1ttt# Add final carrynt"
  228. ".settnoatnt"
  229. ".settnoreorder"
  230. : "=&r" (sum), "=&r" (proto)
  231. : "r" (saddr), "r" (daddr),
  232.   "0" (htonl(len)), "1" (htonl(proto)), "r" (sum));
  233. return csum_fold(sum);
  234. }
  235. #endif /* _ASM_CHECKSUM_H */