checksum.h 3.2 KB

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