checksum.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #include <linux/in6.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 csum_partial(const void *buff, int len, __wsum sum)
  28. {
  29. union register_pair rp = {
  30. .even = (unsigned long) buff,
  31. .odd = (unsigned long) len,
  32. };
  33. asm volatile(
  34. "0: cksm %[sum],%[rp]\n"
  35. " jo 0b\n"
  36. : [sum] "+&d" (sum), [rp] "+&d" (rp.pair) : : "cc", "memory");
  37. return sum;
  38. }
  39. /*
  40. * Fold a partial checksum without adding pseudo headers.
  41. */
  42. static inline __sum16 csum_fold(__wsum sum)
  43. {
  44. u32 csum = (__force u32) sum;
  45. csum += (csum >> 16) | (csum << 16);
  46. csum >>= 16;
  47. return (__force __sum16) ~csum;
  48. }
  49. /*
  50. * This is a version of ip_compute_csum() optimized for IP headers,
  51. * which always checksums on 4 octet boundaries.
  52. */
  53. static inline __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
  54. {
  55. __u64 csum = 0;
  56. __u32 *ptr = (u32 *)iph;
  57. csum += *ptr++;
  58. csum += *ptr++;
  59. csum += *ptr++;
  60. csum += *ptr++;
  61. ihl -= 4;
  62. while (ihl--)
  63. csum += *ptr++;
  64. csum += (csum >> 32) | (csum << 32);
  65. return csum_fold((__force __wsum)(csum >> 32));
  66. }
  67. /*
  68. * Computes the checksum of the TCP/UDP pseudo-header.
  69. * Returns a 32-bit checksum.
  70. */
  71. static inline __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr, __u32 len,
  72. __u8 proto, __wsum sum)
  73. {
  74. __u64 csum = (__force __u64)sum;
  75. csum += (__force __u32)saddr;
  76. csum += (__force __u32)daddr;
  77. csum += len;
  78. csum += proto;
  79. csum += (csum >> 32) | (csum << 32);
  80. return (__force __wsum)(csum >> 32);
  81. }
  82. /*
  83. * Computes the checksum of the TCP/UDP pseudo-header.
  84. * Returns a 16-bit checksum, already complemented.
  85. */
  86. static inline __sum16 csum_tcpudp_magic(__be32 saddr, __be32 daddr, __u32 len,
  87. __u8 proto, __wsum sum)
  88. {
  89. return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
  90. }
  91. /*
  92. * Used for miscellaneous IP-like checksums, mainly icmp.
  93. */
  94. static inline __sum16 ip_compute_csum(const void *buff, int len)
  95. {
  96. return csum_fold(csum_partial(buff, len, 0));
  97. }
  98. #define _HAVE_ARCH_IPV6_CSUM
  99. static inline __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
  100. const struct in6_addr *daddr,
  101. __u32 len, __u8 proto, __wsum csum)
  102. {
  103. __u64 sum = (__force __u64)csum;
  104. sum += (__force __u32)saddr->s6_addr32[0];
  105. sum += (__force __u32)saddr->s6_addr32[1];
  106. sum += (__force __u32)saddr->s6_addr32[2];
  107. sum += (__force __u32)saddr->s6_addr32[3];
  108. sum += (__force __u32)daddr->s6_addr32[0];
  109. sum += (__force __u32)daddr->s6_addr32[1];
  110. sum += (__force __u32)daddr->s6_addr32[2];
  111. sum += (__force __u32)daddr->s6_addr32[3];
  112. sum += len;
  113. sum += proto;
  114. sum += (sum >> 32) | (sum << 32);
  115. return csum_fold((__force __wsum)(sum >> 32));
  116. }
  117. #endif /* _S390_CHECKSUM_H */