checksum.h 3.9 KB

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