csum.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. // Copyright (C) 2019-2020 Arm Ltd.
  3. #include <linux/compiler.h>
  4. #include <linux/kasan-checks.h>
  5. #include <linux/kernel.h>
  6. #include <net/checksum.h>
  7. static u64 accumulate(u64 sum, u64 data)
  8. {
  9. sum += data;
  10. if (sum < data)
  11. sum += 1;
  12. return sum;
  13. }
  14. /*
  15. * We over-read the buffer and this makes KASAN unhappy. Instead, disable
  16. * instrumentation and call kasan explicitly.
  17. */
  18. unsigned int __no_sanitize_address do_csum(const unsigned char *buff, int len)
  19. {
  20. unsigned int offset, shift, sum;
  21. const u64 *ptr;
  22. u64 data, sum64 = 0;
  23. if (unlikely(len <= 0))
  24. return 0;
  25. offset = (unsigned long)buff & 7;
  26. /*
  27. * This is to all intents and purposes safe, since rounding down cannot
  28. * result in a different page or cache line being accessed, and @buff
  29. * should absolutely not be pointing to anything read-sensitive. We do,
  30. * however, have to be careful not to piss off KASAN, which means using
  31. * unchecked reads to accommodate the head and tail, for which we'll
  32. * compensate with an explicit check up-front.
  33. */
  34. kasan_check_read(buff, len);
  35. ptr = (u64 *)(buff - offset);
  36. len = len + offset - 8;
  37. /*
  38. * Head: zero out any excess leading bytes. Shifting back by the same
  39. * amount should be at least as fast as any other way of handling the
  40. * odd/even alignment, and means we can ignore it until the very end.
  41. */
  42. shift = offset * 8;
  43. data = *ptr++;
  44. data = (data >> shift) << shift;
  45. /*
  46. * Body: straightforward aligned loads from here on (the paired loads
  47. * underlying the quadword type still only need dword alignment). The
  48. * main loop strictly excludes the tail, so the second loop will always
  49. * run at least once.
  50. */
  51. while (unlikely(len > 64)) {
  52. __uint128_t tmp1, tmp2, tmp3, tmp4;
  53. tmp1 = *(__uint128_t *)ptr;
  54. tmp2 = *(__uint128_t *)(ptr + 2);
  55. tmp3 = *(__uint128_t *)(ptr + 4);
  56. tmp4 = *(__uint128_t *)(ptr + 6);
  57. len -= 64;
  58. ptr += 8;
  59. /* This is the "don't dump the carry flag into a GPR" idiom */
  60. tmp1 += (tmp1 >> 64) | (tmp1 << 64);
  61. tmp2 += (tmp2 >> 64) | (tmp2 << 64);
  62. tmp3 += (tmp3 >> 64) | (tmp3 << 64);
  63. tmp4 += (tmp4 >> 64) | (tmp4 << 64);
  64. tmp1 = ((tmp1 >> 64) << 64) | (tmp2 >> 64);
  65. tmp1 += (tmp1 >> 64) | (tmp1 << 64);
  66. tmp3 = ((tmp3 >> 64) << 64) | (tmp4 >> 64);
  67. tmp3 += (tmp3 >> 64) | (tmp3 << 64);
  68. tmp1 = ((tmp1 >> 64) << 64) | (tmp3 >> 64);
  69. tmp1 += (tmp1 >> 64) | (tmp1 << 64);
  70. tmp1 = ((tmp1 >> 64) << 64) | sum64;
  71. tmp1 += (tmp1 >> 64) | (tmp1 << 64);
  72. sum64 = tmp1 >> 64;
  73. }
  74. while (len > 8) {
  75. __uint128_t tmp;
  76. sum64 = accumulate(sum64, data);
  77. tmp = *(__uint128_t *)ptr;
  78. len -= 16;
  79. ptr += 2;
  80. data = tmp >> 64;
  81. sum64 = accumulate(sum64, tmp);
  82. }
  83. if (len > 0) {
  84. sum64 = accumulate(sum64, data);
  85. data = *ptr;
  86. len -= 8;
  87. }
  88. /*
  89. * Tail: zero any over-read bytes similarly to the head, again
  90. * preserving odd/even alignment.
  91. */
  92. shift = len * -8;
  93. data = (data << shift) >> shift;
  94. sum64 = accumulate(sum64, data);
  95. /* Finally, folding */
  96. sum64 += (sum64 >> 32) | (sum64 << 32);
  97. sum = sum64 >> 32;
  98. sum += (sum >> 16) | (sum << 16);
  99. if (offset & 1)
  100. return (u16)swab32(sum);
  101. return sum >> 16;
  102. }
  103. __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
  104. const struct in6_addr *daddr,
  105. __u32 len, __u8 proto, __wsum csum)
  106. {
  107. __uint128_t src, dst;
  108. u64 sum = (__force u64)csum;
  109. src = *(const __uint128_t *)saddr->s6_addr;
  110. dst = *(const __uint128_t *)daddr->s6_addr;
  111. sum += (__force u32)htonl(len);
  112. sum += (u32)proto << 24;
  113. src += (src >> 64) | (src << 64);
  114. dst += (dst >> 64) | (dst << 64);
  115. sum = accumulate(sum, src >> 64);
  116. sum = accumulate(sum, dst >> 64);
  117. sum += ((sum >> 32) | (sum << 32));
  118. return csum_fold((__force __wsum)(sum >> 32));
  119. }
  120. EXPORT_SYMBOL(csum_ipv6_magic);