checksum.h 3.5 KB

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