checksum.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #ifndef _X86_64_CHECKSUM_H
  2. #define _X86_64_CHECKSUM_H
  3. /*
  4. * Checksums for x86-64
  5. * Copyright 2002 by Andi Kleen, SuSE Labs
  6. * with some code from asm-i386/checksum.h
  7. */
  8. #include <linux/compiler.h>
  9. #include <asm/uaccess.h>
  10. #include <asm/byteorder.h>
  11. /**
  12. * csum_fold - Fold and invert a 32bit checksum.
  13. * sum: 32bit unfolded sum
  14. *
  15. * Fold a 32bit running checksum to 16bit and invert it. This is usually
  16. * the last step before putting a checksum into a packet.
  17. * Make sure not to mix with 64bit checksums.
  18. */
  19. static inline unsigned int csum_fold(unsigned int sum)
  20. {
  21. __asm__(
  22. " addl %1,%0\n"
  23. " adcl $0xffff,%0"
  24. : "=r" (sum)
  25. : "r" (sum << 16), "0" (sum & 0xffff0000)
  26. );
  27. return (~sum) >> 16;
  28. }
  29. /*
  30. * This is a version of ip_compute_csum() optimized for IP headers,
  31. * which always checksum on 4 octet boundaries.
  32. *
  33. * By Jorge Cwik <jorge@laser.satlink.net>, adapted for linux by
  34. * Arnt Gulbrandsen.
  35. */
  36. /**
  37. * ip_fast_csum - Compute the IPv4 header checksum efficiently.
  38. * iph: ipv4 header
  39. * ihl: length of header / 4
  40. */
  41. static inline unsigned short ip_fast_csum(unsigned char *iph, unsigned int ihl)
  42. {
  43. unsigned int sum;
  44. asm( " movl (%1), %0\n"
  45. " subl $4, %2\n"
  46. " jbe 2f\n"
  47. " addl 4(%1), %0\n"
  48. " adcl 8(%1), %0\n"
  49. " adcl 12(%1), %0\n"
  50. "1: adcl 16(%1), %0\n"
  51. " lea 4(%1), %1\n"
  52. " decl %2\n"
  53. " jne 1b\n"
  54. " adcl $0, %0\n"
  55. " movl %0, %2\n"
  56. " shrl $16, %0\n"
  57. " addw %w2, %w0\n"
  58. " adcl $0, %0\n"
  59. " notl %0\n"
  60. "2:"
  61. /* Since the input registers which are loaded with iph and ihl
  62. are modified, we must also specify them as outputs, or gcc
  63. will assume they contain their original values. */
  64. : "=r" (sum), "=r" (iph), "=r" (ihl)
  65. : "1" (iph), "2" (ihl)
  66. : "memory");
  67. return(sum);
  68. }
  69. /**
  70. * csum_tcpup_nofold - Compute an IPv4 pseudo header checksum.
  71. * @saddr: source address
  72. * @daddr: destination address
  73. * @len: length of packet
  74. * @proto: ip protocol of packet
  75. * @sum: initial sum to be added in (32bit unfolded)
  76. *
  77. * Returns the pseudo header checksum the input data. Result is
  78. * 32bit unfolded.
  79. */
  80. static inline unsigned long
  81. csum_tcpudp_nofold(unsigned saddr, unsigned daddr, unsigned short len,
  82. unsigned short proto, unsigned int sum)
  83. {
  84. asm(" addl %1, %0\n"
  85. " adcl %2, %0\n"
  86. " adcl %3, %0\n"
  87. " adcl $0, %0\n"
  88. : "=r" (sum)
  89. : "g" (daddr), "g" (saddr), "g" ((ntohs(len)<<16)+proto*256), "0" (sum));
  90. return sum;
  91. }
  92. /**
  93. * csum_tcpup_magic - Compute an IPv4 pseudo header checksum.
  94. * @saddr: source address
  95. * @daddr: destination address
  96. * @len: length of packet
  97. * @proto: ip protocol of packet
  98. * @sum: initial sum to be added in (32bit unfolded)
  99. *
  100. * Returns the 16bit pseudo header checksum the input data already
  101. * complemented and ready to be filled in.
  102. */
  103. static inline unsigned short int
  104. csum_tcpudp_magic(unsigned long saddr, unsigned long daddr,
  105. unsigned short len, unsigned short proto, unsigned int sum)
  106. {
  107. return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
  108. }
  109. /**
  110. * csum_partial - Compute an internet checksum.
  111. * @buff: buffer to be checksummed
  112. * @len: length of buffer.
  113. * @sum: initial sum to be added in (32bit unfolded)
  114. *
  115. * Returns the 32bit unfolded internet checksum of the buffer.
  116. * Before filling it in it needs to be csum_fold()'ed.
  117. * buff should be aligned to a 64bit boundary if possible.
  118. */
  119. extern unsigned int csum_partial(const unsigned char *buff, unsigned len, unsigned int sum);
  120. #define _HAVE_ARCH_COPY_AND_CSUM_FROM_USER 1
  121. #define HAVE_CSUM_COPY_USER 1
  122. /* Do not call this directly. Use the wrappers below */
  123. extern unsigned long csum_partial_copy_generic(const unsigned char *src, const unsigned char *dst,
  124. unsigned len,
  125. unsigned sum,
  126. int *src_err_ptr, int *dst_err_ptr);
  127. extern unsigned int csum_partial_copy_from_user(const unsigned char __user *src, unsigned char *dst,
  128. int len, unsigned int isum, int *errp);
  129. extern unsigned int csum_partial_copy_to_user(const unsigned char *src, unsigned char __user *dst,
  130. int len, unsigned int isum, int *errp);
  131. extern unsigned int csum_partial_copy_nocheck(const unsigned char *src, unsigned char *dst, int len,
  132. unsigned int sum);
  133. /* Old names. To be removed. */
  134. #define csum_and_copy_to_user csum_partial_copy_to_user
  135. #define csum_and_copy_from_user csum_partial_copy_from_user
  136. /**
  137. * ip_compute_csum - Compute an 16bit IP checksum.
  138. * @buff: buffer address.
  139. * @len: length of buffer.
  140. *
  141. * Returns the 16bit folded/inverted checksum of the passed buffer.
  142. * Ready to fill in.
  143. */
  144. extern unsigned short ip_compute_csum(unsigned char * buff, int len);
  145. /**
  146. * csum_ipv6_magic - Compute checksum of an IPv6 pseudo header.
  147. * @saddr: source address
  148. * @daddr: destination address
  149. * @len: length of packet
  150. * @proto: protocol of packet
  151. * @sum: initial sum (32bit unfolded) to be added in
  152. *
  153. * Computes an IPv6 pseudo header checksum. This sum is added the checksum
  154. * into UDP/TCP packets and contains some link layer information.
  155. * Returns the unfolded 32bit checksum.
  156. */
  157. struct in6_addr;
  158. #define _HAVE_ARCH_IPV6_CSUM 1
  159. extern unsigned short
  160. csum_ipv6_magic(struct in6_addr *saddr, struct in6_addr *daddr,
  161. __u32 len, unsigned short proto, unsigned int sum);
  162. static inline unsigned add32_with_carry(unsigned a, unsigned b)
  163. {
  164. asm("addl %2,%0\n\t"
  165. "adcl $0,%0"
  166. : "=r" (a)
  167. : "0" (a), "r" (b));
  168. return a;
  169. }
  170. #endif