checksum.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* $Id: checksum.h,v 1.19 2002/02/09 19:49:31 davem Exp $ */
  2. #ifndef __SPARC64_CHECKSUM_H
  3. #define __SPARC64_CHECKSUM_H
  4. /* checksum.h: IP/UDP/TCP checksum routines on the V9.
  5. *
  6. * Copyright(C) 1995 Linus Torvalds
  7. * Copyright(C) 1995 Miguel de Icaza
  8. * Copyright(C) 1996 David S. Miller
  9. * Copyright(C) 1996 Eddie C. Dost
  10. * Copyright(C) 1997 Jakub Jelinek
  11. *
  12. * derived from:
  13. * Alpha checksum c-code
  14. * ix86 inline assembly
  15. * RFC1071 Computing the Internet Checksum
  16. */
  17. #include <linux/in6.h>
  18. #include <asm/uaccess.h>
  19. /* computes the checksum of a memory block at buff, length len,
  20. * and adds in "sum" (32-bit)
  21. *
  22. * returns a 32-bit number suitable for feeding into itself
  23. * or csum_tcpudp_magic
  24. *
  25. * this function must be called with even lengths, except
  26. * for the last fragment, which may be odd
  27. *
  28. * it's best to have buff aligned on a 32-bit boundary
  29. */
  30. extern unsigned int csum_partial(const unsigned char * buff, int len, unsigned int sum);
  31. /* the same as csum_partial, but copies from user space while it
  32. * checksums
  33. *
  34. * here even more important to align src and dst on a 32-bit (or even
  35. * better 64-bit) boundary
  36. */
  37. extern unsigned int csum_partial_copy_nocheck(const unsigned char *src,
  38. unsigned char *dst,
  39. int len, unsigned int sum);
  40. extern long __csum_partial_copy_from_user(const unsigned char __user *src,
  41. unsigned char *dst, int len,
  42. unsigned int sum);
  43. static inline unsigned int
  44. csum_partial_copy_from_user(const unsigned char __user *src,
  45. unsigned char *dst, int len,
  46. unsigned int sum, int *err)
  47. {
  48. long ret = __csum_partial_copy_from_user(src, dst, len, sum);
  49. if (ret < 0)
  50. *err = -EFAULT;
  51. return (unsigned int) ret;
  52. }
  53. /*
  54. * Copy and checksum to user
  55. */
  56. #define HAVE_CSUM_COPY_USER
  57. extern long __csum_partial_copy_to_user(const unsigned char *src,
  58. unsigned char __user *dst, int len,
  59. unsigned int sum);
  60. static inline unsigned int
  61. csum_and_copy_to_user(const unsigned char *src,
  62. unsigned char __user *dst, int len,
  63. unsigned int sum, int *err)
  64. {
  65. long ret = __csum_partial_copy_to_user(src, dst, len, sum);
  66. if (ret < 0)
  67. *err = -EFAULT;
  68. return (unsigned int) ret;
  69. }
  70. /* ihl is always 5 or greater, almost always is 5, and iph is word aligned
  71. * the majority of the time.
  72. */
  73. extern unsigned short ip_fast_csum(__const__ unsigned char *iph,
  74. unsigned int ihl);
  75. /* Fold a partial checksum without adding pseudo headers. */
  76. static inline unsigned short csum_fold(unsigned int sum)
  77. {
  78. unsigned int tmp;
  79. __asm__ __volatile__(
  80. " addcc %0, %1, %1\n"
  81. " srl %1, 16, %1\n"
  82. " addc %1, %%g0, %1\n"
  83. " xnor %%g0, %1, %0\n"
  84. : "=&r" (sum), "=r" (tmp)
  85. : "0" (sum), "1" (sum<<16)
  86. : "cc");
  87. return (sum & 0xffff);
  88. }
  89. static inline unsigned long csum_tcpudp_nofold(unsigned long saddr,
  90. unsigned long daddr,
  91. unsigned int len,
  92. unsigned short proto,
  93. unsigned int sum)
  94. {
  95. __asm__ __volatile__(
  96. " addcc %1, %0, %0\n"
  97. " addccc %2, %0, %0\n"
  98. " addccc %3, %0, %0\n"
  99. " addc %0, %%g0, %0\n"
  100. : "=r" (sum), "=r" (saddr)
  101. : "r" (daddr), "r" ((proto<<16)+len), "0" (sum), "1" (saddr)
  102. : "cc");
  103. return sum;
  104. }
  105. /*
  106. * computes the checksum of the TCP/UDP pseudo-header
  107. * returns a 16-bit checksum, already complemented
  108. */
  109. static inline unsigned short int csum_tcpudp_magic(unsigned long saddr,
  110. unsigned long daddr,
  111. unsigned short len,
  112. unsigned short proto,
  113. unsigned int sum)
  114. {
  115. return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
  116. }
  117. #define _HAVE_ARCH_IPV6_CSUM
  118. static inline unsigned short int csum_ipv6_magic(struct in6_addr *saddr,
  119. struct in6_addr *daddr,
  120. __u32 len,
  121. unsigned short proto,
  122. unsigned int sum)
  123. {
  124. __asm__ __volatile__ (
  125. " addcc %3, %4, %%g7\n"
  126. " addccc %5, %%g7, %%g7\n"
  127. " lduw [%2 + 0x0c], %%g2\n"
  128. " lduw [%2 + 0x08], %%g3\n"
  129. " addccc %%g2, %%g7, %%g7\n"
  130. " lduw [%2 + 0x04], %%g2\n"
  131. " addccc %%g3, %%g7, %%g7\n"
  132. " lduw [%2 + 0x00], %%g3\n"
  133. " addccc %%g2, %%g7, %%g7\n"
  134. " lduw [%1 + 0x0c], %%g2\n"
  135. " addccc %%g3, %%g7, %%g7\n"
  136. " lduw [%1 + 0x08], %%g3\n"
  137. " addccc %%g2, %%g7, %%g7\n"
  138. " lduw [%1 + 0x04], %%g2\n"
  139. " addccc %%g3, %%g7, %%g7\n"
  140. " lduw [%1 + 0x00], %%g3\n"
  141. " addccc %%g2, %%g7, %%g7\n"
  142. " addccc %%g3, %%g7, %0\n"
  143. " addc 0, %0, %0\n"
  144. : "=&r" (sum)
  145. : "r" (saddr), "r" (daddr), "r"(htonl(len)),
  146. "r"(htonl(proto)), "r"(sum)
  147. : "g2", "g3", "g7", "cc");
  148. return csum_fold(sum);
  149. }
  150. /* this routine is used for miscellaneous IP-like checksums, mainly in icmp.c */
  151. static inline unsigned short ip_compute_csum(unsigned char * buff, int len)
  152. {
  153. return csum_fold(csum_partial(buff, len, 0));
  154. }
  155. #endif /* !(__SPARC64_CHECKSUM_H) */