checksum_64.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Licensed under the GPL
  3. */
  4. #ifndef __UM_SYSDEP_CHECKSUM_H
  5. #define __UM_SYSDEP_CHECKSUM_H
  6. #include "linux/string.h"
  7. #include "linux/in6.h"
  8. #include "asm/uaccess.h"
  9. extern __wsum csum_partial(const void *buff, int len, __wsum sum);
  10. /*
  11. * Note: when you get a NULL pointer exception here this means someone
  12. * passed in an incorrect kernel address to one of these functions.
  13. *
  14. * If you use these functions directly please don't forget the
  15. * access_ok().
  16. */
  17. static __inline__
  18. __wsum csum_partial_copy_nocheck(const void *src, void *dst,
  19. int len, __wsum sum)
  20. {
  21. memcpy(dst, src, len);
  22. return(csum_partial(dst, len, sum));
  23. }
  24. static __inline__
  25. __wsum csum_partial_copy_from_user(const void __user *src,
  26. void *dst, int len, __wsum sum,
  27. int *err_ptr)
  28. {
  29. if (copy_from_user(dst, src, len)) {
  30. *err_ptr = -EFAULT;
  31. return (__force __wsum)-1;
  32. }
  33. return csum_partial(dst, len, sum);
  34. }
  35. /**
  36. * csum_fold - Fold and invert a 32bit checksum.
  37. * sum: 32bit unfolded sum
  38. *
  39. * Fold a 32bit running checksum to 16bit and invert it. This is usually
  40. * the last step before putting a checksum into a packet.
  41. * Make sure not to mix with 64bit checksums.
  42. */
  43. static inline __sum16 csum_fold(__wsum sum)
  44. {
  45. __asm__(
  46. " addl %1,%0\n"
  47. " adcl $0xffff,%0"
  48. : "=r" (sum)
  49. : "r" ((__force u32)sum << 16),
  50. "0" ((__force u32)sum & 0xffff0000)
  51. );
  52. return (__force __sum16)(~(__force u32)sum >> 16);
  53. }
  54. /**
  55. * csum_tcpup_nofold - Compute an IPv4 pseudo header checksum.
  56. * @saddr: source address
  57. * @daddr: destination address
  58. * @len: length of packet
  59. * @proto: ip protocol of packet
  60. * @sum: initial sum to be added in (32bit unfolded)
  61. *
  62. * Returns the pseudo header checksum the input data. Result is
  63. * 32bit unfolded.
  64. */
  65. static inline __wsum
  66. csum_tcpudp_nofold(__be32 saddr, __be32 daddr, unsigned short len,
  67. unsigned short proto, __wsum sum)
  68. {
  69. asm(" addl %1, %0\n"
  70. " adcl %2, %0\n"
  71. " adcl %3, %0\n"
  72. " adcl $0, %0\n"
  73. : "=r" (sum)
  74. : "g" (daddr), "g" (saddr), "g" ((len + proto) << 8), "0" (sum));
  75. return sum;
  76. }
  77. /*
  78. * computes the checksum of the TCP/UDP pseudo-header
  79. * returns a 16-bit checksum, already complemented
  80. */
  81. static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr,
  82. unsigned short len,
  83. unsigned short proto,
  84. __wsum sum)
  85. {
  86. return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
  87. }
  88. /**
  89. * ip_fast_csum - Compute the IPv4 header checksum efficiently.
  90. * iph: ipv4 header
  91. * ihl: length of header / 4
  92. */
  93. static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
  94. {
  95. unsigned int sum;
  96. asm( " movl (%1), %0\n"
  97. " subl $4, %2\n"
  98. " jbe 2f\n"
  99. " addl 4(%1), %0\n"
  100. " adcl 8(%1), %0\n"
  101. " adcl 12(%1), %0\n"
  102. "1: adcl 16(%1), %0\n"
  103. " lea 4(%1), %1\n"
  104. " decl %2\n"
  105. " jne 1b\n"
  106. " adcl $0, %0\n"
  107. " movl %0, %2\n"
  108. " shrl $16, %0\n"
  109. " addw %w2, %w0\n"
  110. " adcl $0, %0\n"
  111. " notl %0\n"
  112. "2:"
  113. /* Since the input registers which are loaded with iph and ipl
  114. are modified, we must also specify them as outputs, or gcc
  115. will assume they contain their original values. */
  116. : "=r" (sum), "=r" (iph), "=r" (ihl)
  117. : "1" (iph), "2" (ihl)
  118. : "memory");
  119. return (__force __sum16)sum;
  120. }
  121. static inline unsigned add32_with_carry(unsigned a, unsigned b)
  122. {
  123. asm("addl %2,%0\n\t"
  124. "adcl $0,%0"
  125. : "=r" (a)
  126. : "0" (a), "r" (b));
  127. return a;
  128. }
  129. extern __sum16 ip_compute_csum(const void *buff, int len);
  130. #endif