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

Linux/Unix编程

开发平台:

Unix_Linux

  1. #ifndef __ASM_SH_CHECKSUM_H
  2. #define __ASM_SH_CHECKSUM_H
  3. /*
  4.  * This file is subject to the terms and conditions of the GNU General Public
  5.  * License.  See the file "COPYING" in the main directory of this archive
  6.  * for more details.
  7.  *
  8.  * Copyright (C) 1999 by Kaz Kojima & Niibe Yutaka
  9.  */
  10. #include <linux/config.h>
  11. /*
  12.  * computes the checksum of a memory block at buff, length len,
  13.  * and adds in "sum" (32-bit)
  14.  *
  15.  * returns a 32-bit number suitable for feeding into itself
  16.  * or csum_tcpudp_magic
  17.  *
  18.  * this function must be called with even lengths, except
  19.  * for the last fragment, which may be odd
  20.  *
  21.  * it's best to have buff aligned on a 32-bit boundary
  22.  */
  23. asmlinkage unsigned int csum_partial(const unsigned char * buff, int len, unsigned int sum);
  24. /*
  25.  * the same as csum_partial, but copies from src while it
  26.  * checksums, and handles user-space pointer exceptions correctly, when needed.
  27.  *
  28.  * here even more important to align src and dst on a 32-bit (or even
  29.  * better 64-bit) boundary
  30.  */
  31. asmlinkage unsigned int csum_partial_copy_generic( const char *src, char *dst, int len, int sum,
  32.    int *src_err_ptr, int *dst_err_ptr);
  33. /*
  34.  * Note: when you get a NULL pointer exception here this means someone
  35.  * passed in an incorrect kernel address to one of these functions. 
  36.  *
  37.  * If you use these functions directly please don't forget the 
  38.  * verify_area().
  39.  */
  40. static __inline__
  41. unsigned int csum_partial_copy_nocheck ( const char *src, char *dst,
  42. int len, int sum)
  43. {
  44. return csum_partial_copy_generic ( src, dst, len, sum, NULL, NULL);
  45. }
  46. static __inline__
  47. unsigned int csum_partial_copy_from_user ( const char *src, char *dst,
  48. int len, int sum, int *err_ptr)
  49. {
  50. return csum_partial_copy_generic ( src, dst, len, sum, err_ptr, NULL);
  51. }
  52. /*
  53.  * These are the old (and unsafe) way of doing checksums, a warning message will be
  54.  * printed if they are used and an exeption occurs.
  55.  *
  56.  * these functions should go away after some time.
  57.  */
  58. #define csum_partial_copy_fromuser csum_partial_copy
  59. unsigned int csum_partial_copy( const char *src, char *dst, int len, int sum);
  60. /*
  61.  * Fold a partial checksum
  62.  */
  63. static __inline__ unsigned int csum_fold(unsigned int sum)
  64. {
  65. unsigned int __dummy;
  66. __asm__("swap.w %0, %1nt"
  67. "extu.w %0, %0nt"
  68. "extu.w %1, %1nt"
  69. "add %1, %0nt"
  70. "swap.w %0, %1nt"
  71. "add %1, %0nt"
  72. "not %0, %0nt"
  73. : "=r" (sum), "=&r" (__dummy)
  74. : "0" (sum)
  75. : "t");
  76. return sum;
  77. }
  78. /*
  79.  * This is a version of ip_compute_csum() optimized for IP headers,
  80.  * which always checksum on 4 octet boundaries.
  81.  *
  82.  *      i386 version by Jorge Cwik <jorge@laser.satlink.net>, adapted
  83.  *      for linux by * Arnt Gulbrandsen.
  84.  */
  85. static __inline__ unsigned short ip_fast_csum(unsigned char * iph, unsigned int ihl)
  86. {
  87. unsigned int sum, __dummy0, __dummy1;
  88. __asm__ __volatile__(
  89. "mov.l @%1+, %0nt"
  90. "mov.l @%1+, %3nt"
  91. "add #-2, %2nt"
  92. "clrtnt"
  93. "1:t"
  94. "addc %3, %0nt"
  95. "movt %4nt"
  96. "mov.l @%1+, %3nt"
  97. "dt %2nt"
  98. "bf/s 1bnt"
  99. " cmp/eq #1, %4nt"
  100. "addc %3, %0nt"
  101. "addc %2, %0"     /* Here %2 is 0, add carry-bit */
  102. /* Since the input registers which are loaded with iph and ihl
  103.    are modified, we must also specify them as outputs, or gcc
  104.    will assume they contain their original values. */
  105. : "=r" (sum), "=r" (iph), "=r" (ihl), "=&r" (__dummy0), "=&z" (__dummy1)
  106. : "1" (iph), "2" (ihl)
  107. : "t");
  108. return csum_fold(sum);
  109. }
  110. static __inline__ unsigned long csum_tcpudp_nofold(unsigned long saddr,
  111.    unsigned long daddr,
  112.    unsigned short len,
  113.    unsigned short proto,
  114.    unsigned int sum) 
  115. {
  116. #ifdef __LITTLE_ENDIAN__
  117. unsigned long len_proto = (ntohs(len)<<16)+proto*256;
  118. #else
  119. unsigned long len_proto = (proto<<16)+len;
  120. #endif
  121. __asm__("clrtnt"
  122. "addc %0, %1nt"
  123. "addc %2, %1nt"
  124. "addc %3, %1nt"
  125. "movt %0nt"
  126. "add %1, %0"
  127. : "=r" (sum), "=r" (len_proto)
  128. : "r" (daddr), "r" (saddr), "1" (len_proto), "0" (sum)
  129. : "t");
  130. return sum;
  131. }
  132. /*
  133.  * computes the checksum of the TCP/UDP pseudo-header
  134.  * returns a 16-bit checksum, already complemented
  135.  */
  136. static __inline__ unsigned short int csum_tcpudp_magic(unsigned long saddr,
  137.        unsigned long daddr,
  138.        unsigned short len,
  139.        unsigned short proto,
  140.        unsigned int sum) 
  141. {
  142. return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
  143. }
  144. /*
  145.  * this routine is used for miscellaneous IP-like checksums, mainly
  146.  * in icmp.c
  147.  */
  148. static __inline__ unsigned short ip_compute_csum(unsigned char * buff, int len)
  149. {
  150.     return csum_fold (csum_partial(buff, len, 0));
  151. }
  152. #define _HAVE_ARCH_IPV6_CSUM
  153. #ifdef CONFIG_IPV6
  154. static __inline__ unsigned short int csum_ipv6_magic(struct in6_addr *saddr,
  155.      struct in6_addr *daddr,
  156.      __u32 len,
  157.      unsigned short proto,
  158.      unsigned int sum) 
  159. {
  160. unsigned int __dummy;
  161. __asm__("clrtnt"
  162. "mov.l @(0,%2), %1nt"
  163. "addc %1, %0nt"
  164. "mov.l @(4,%2), %1nt"
  165. "addc %1, %0nt"
  166. "mov.l @(8,%2), %1nt"
  167. "addc %1, %0nt"
  168. "mov.l @(12,%2), %1nt"
  169. "addc %1, %0nt"
  170. "mov.l @(0,%3), %1nt"
  171. "addc %1, %0nt"
  172. "mov.l @(4,%3), %1nt"
  173. "addc %1, %0nt"
  174. "mov.l @(8,%3), %1nt"
  175. "addc %1, %0nt"
  176. "mov.l @(12,%3), %1nt"
  177. "addc %1, %0nt"
  178. "addc %4, %0nt"
  179. "addc %5, %0nt"
  180. "movt %1nt"
  181. "add %1, %0n"
  182. : "=r" (sum), "=&r" (__dummy)
  183. : "r" (saddr), "r" (daddr), 
  184.   "r" (htonl(len)), "r" (htonl(proto)), "0" (sum)
  185. : "t");
  186. return csum_fold(sum);
  187. }
  188. #endif
  189. /* 
  190.  * Copy and checksum to user
  191.  */
  192. #define HAVE_CSUM_COPY_USER
  193. static __inline__ unsigned int csum_and_copy_to_user (const char *src, char *dst,
  194.     int len, int sum, int *err_ptr)
  195. {
  196. if (access_ok(VERIFY_WRITE, dst, len))
  197. return csum_partial_copy_generic(src, dst, len, sum, NULL, err_ptr);
  198. if (len)
  199. *err_ptr = -EFAULT;
  200. return -1; /* invalid checksum */
  201. }
  202. #endif /* __ASM_SH_CHECKSUM_H */