checksum.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * S390 fast network checksum routines
  3. *
  4. * S390 version
  5. * Copyright IBM Corp. 1999
  6. * Author(s): Ulrich Hild (first version)
  7. * Martin Schwidefsky (heavily optimized CKSM version)
  8. * D.J. Barrow (third attempt)
  9. */
  10. #ifndef _S390_CHECKSUM_H
  11. #define _S390_CHECKSUM_H
  12. #include <asm/uaccess.h>
  13. /*
  14. * computes the checksum of a memory block at buff, length len,
  15. * and adds in "sum" (32-bit)
  16. *
  17. * returns a 32-bit number suitable for feeding into itself
  18. * or csum_tcpudp_magic
  19. *
  20. * this function must be called with even lengths, except
  21. * for the last fragment, which may be odd
  22. *
  23. * it's best to have buff aligned on a 32-bit boundary
  24. */
  25. static inline __wsum
  26. csum_partial(const void *buff, int len, __wsum sum)
  27. {
  28. register unsigned long reg2 asm("2") = (unsigned long) buff;
  29. register unsigned long reg3 asm("3") = (unsigned long) len;
  30. asm volatile(
  31. "0: cksm %0,%1\n" /* do checksum on longs */
  32. " jo 0b\n"
  33. : "+d" (sum), "+d" (reg2), "+d" (reg3) : : "cc", "memory");
  34. return sum;
  35. }
  36. /*
  37. * the same as csum_partial_copy, but copies from user space.
  38. *
  39. * here even more important to align src and dst on a 32-bit (or even
  40. * better 64-bit) boundary
  41. *
  42. * Copy from userspace and compute checksum. If we catch an exception
  43. * then zero the rest of the buffer.
  44. */
  45. static inline __wsum
  46. csum_partial_copy_from_user(const void __user *src, void *dst,
  47. int len, __wsum sum,
  48. int *err_ptr)
  49. {
  50. int missing;
  51. missing = copy_from_user(dst, src, len);
  52. if (missing) {
  53. memset(dst + len - missing, 0, missing);
  54. *err_ptr = -EFAULT;
  55. }
  56. return csum_partial(dst, len, sum);
  57. }
  58. static inline __wsum
  59. csum_partial_copy_nocheck (const void *src, void *dst, int len, __wsum sum)
  60. {
  61. memcpy(dst,src,len);
  62. return csum_partial(dst, len, sum);
  63. }
  64. /*
  65. * Fold a partial checksum without adding pseudo headers
  66. */
  67. static inline __sum16 csum_fold(__wsum sum)
  68. {
  69. u32 csum = (__force u32) sum;
  70. csum += (csum >> 16) + (csum << 16);
  71. csum >>= 16;
  72. return (__force __sum16) ~csum;
  73. }
  74. /*
  75. * This is a version of ip_compute_csum() optimized for IP headers,
  76. * which always checksum on 4 octet boundaries.
  77. *
  78. */
  79. static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
  80. {
  81. return csum_fold(csum_partial(iph, ihl*4, 0));
  82. }
  83. /*
  84. * computes the checksum of the TCP/UDP pseudo-header
  85. * returns a 32-bit checksum
  86. */
  87. static inline __wsum
  88. csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
  89. unsigned short len, unsigned short proto,
  90. __wsum sum)
  91. {
  92. __u32 csum = (__force __u32)sum;
  93. csum += (__force __u32)saddr;
  94. if (csum < (__force __u32)saddr)
  95. csum++;
  96. csum += (__force __u32)daddr;
  97. if (csum < (__force __u32)daddr)
  98. csum++;
  99. csum += len + proto;
  100. if (csum < len + proto)
  101. csum++;
  102. return (__force __wsum)csum;
  103. }
  104. /*
  105. * computes the checksum of the TCP/UDP pseudo-header
  106. * returns a 16-bit checksum, already complemented
  107. */
  108. static inline __sum16
  109. csum_tcpudp_magic(__be32 saddr, __be32 daddr,
  110. unsigned short len, unsigned short proto,
  111. __wsum sum)
  112. {
  113. return csum_fold(csum_tcpudp_nofold(saddr,daddr,len,proto,sum));
  114. }
  115. /*
  116. * this routine is used for miscellaneous IP-like checksums, mainly
  117. * in icmp.c
  118. */
  119. static inline __sum16 ip_compute_csum(const void *buff, int len)
  120. {
  121. return csum_fold(csum_partial(buff, len, 0));
  122. }
  123. #endif /* _S390_CHECKSUM_H */