checksum.h 3.6 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 unsigned csum_partial(const unsigned char *buff, unsigned len,
  10. unsigned sum);
  11. /*
  12. * Note: when you get a NULL pointer exception here this means someone
  13. * passed in an incorrect kernel address to one of these functions.
  14. *
  15. * If you use these functions directly please don't forget the
  16. * access_ok().
  17. */
  18. static __inline__
  19. unsigned int csum_partial_copy_nocheck(const unsigned char *src, unsigned char *dst,
  20. int len, int sum)
  21. {
  22. memcpy(dst, src, len);
  23. return(csum_partial(dst, len, sum));
  24. }
  25. static __inline__
  26. unsigned int csum_partial_copy_from_user(const unsigned char *src,
  27. unsigned char *dst, int len, int sum,
  28. int *err_ptr)
  29. {
  30. if(copy_from_user(dst, src, len)){
  31. *err_ptr = -EFAULT;
  32. return(-1);
  33. }
  34. return csum_partial(dst, len, sum);
  35. }
  36. /**
  37. * csum_fold - Fold and invert a 32bit checksum.
  38. * sum: 32bit unfolded sum
  39. *
  40. * Fold a 32bit running checksum to 16bit and invert it. This is usually
  41. * the last step before putting a checksum into a packet.
  42. * Make sure not to mix with 64bit checksums.
  43. */
  44. static inline unsigned int csum_fold(unsigned int sum)
  45. {
  46. __asm__(
  47. " addl %1,%0\n"
  48. " adcl $0xffff,%0"
  49. : "=r" (sum)
  50. : "r" (sum << 16), "0" (sum & 0xffff0000)
  51. );
  52. return (~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 unsigned long
  66. csum_tcpudp_nofold(unsigned saddr, unsigned daddr, unsigned short len,
  67. unsigned short proto, unsigned int 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" ((ntohs(len)<<16)+proto*256), "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 unsigned short int csum_tcpudp_magic(unsigned long saddr,
  82. unsigned long daddr,
  83. unsigned short len,
  84. unsigned short proto,
  85. unsigned int sum)
  86. {
  87. return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
  88. }
  89. /**
  90. * ip_fast_csum - Compute the IPv4 header checksum efficiently.
  91. * iph: ipv4 header
  92. * ihl: length of header / 4
  93. */
  94. static inline unsigned short ip_fast_csum(unsigned char *iph, unsigned int ihl)
  95. {
  96. unsigned int sum;
  97. asm( " movl (%1), %0\n"
  98. " subl $4, %2\n"
  99. " jbe 2f\n"
  100. " addl 4(%1), %0\n"
  101. " adcl 8(%1), %0\n"
  102. " adcl 12(%1), %0\n"
  103. "1: adcl 16(%1), %0\n"
  104. " lea 4(%1), %1\n"
  105. " decl %2\n"
  106. " jne 1b\n"
  107. " adcl $0, %0\n"
  108. " movl %0, %2\n"
  109. " shrl $16, %0\n"
  110. " addw %w2, %w0\n"
  111. " adcl $0, %0\n"
  112. " notl %0\n"
  113. "2:"
  114. /* Since the input registers which are loaded with iph and ipl
  115. are modified, we must also specify them as outputs, or gcc
  116. will assume they contain their original values. */
  117. : "=r" (sum), "=r" (iph), "=r" (ihl)
  118. : "1" (iph), "2" (ihl)
  119. : "memory");
  120. return(sum);
  121. }
  122. static inline unsigned add32_with_carry(unsigned a, unsigned b)
  123. {
  124. asm("addl %2,%0\n\t"
  125. "adcl $0,%0"
  126. : "=r" (a)
  127. : "0" (a), "r" (b));
  128. return a;
  129. }
  130. extern unsigned short ip_compute_csum(unsigned char * buff, int len);
  131. #endif