csum_partial_copy.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Network Checksum & Copy routine
  4. *
  5. * Copyright (C) 1999, 2003-2004 Hewlett-Packard Co
  6. * Stephane Eranian <eranian@hpl.hp.com>
  7. *
  8. * Most of the code has been imported from Linux/Alpha
  9. */
  10. #include <linux/module.h>
  11. #include <linux/types.h>
  12. #include <linux/string.h>
  13. #include <linux/uaccess.h>
  14. /*
  15. * XXX Fixme: those 2 inlines are meant for debugging and will go away
  16. */
  17. static inline unsigned
  18. short from64to16(unsigned long x)
  19. {
  20. /* add up 32-bit words for 33 bits */
  21. x = (x & 0xffffffff) + (x >> 32);
  22. /* add up 16-bit and 17-bit words for 17+c bits */
  23. x = (x & 0xffff) + (x >> 16);
  24. /* add up 16-bit and 2-bit for 16+c bit */
  25. x = (x & 0xffff) + (x >> 16);
  26. /* add up carry.. */
  27. x = (x & 0xffff) + (x >> 16);
  28. return x;
  29. }
  30. static inline
  31. unsigned long do_csum_c(const unsigned char * buff, int len, unsigned int psum)
  32. {
  33. int odd, count;
  34. unsigned long result = (unsigned long)psum;
  35. if (len <= 0)
  36. goto out;
  37. odd = 1 & (unsigned long) buff;
  38. if (odd) {
  39. result = *buff << 8;
  40. len--;
  41. buff++;
  42. }
  43. count = len >> 1; /* nr of 16-bit words.. */
  44. if (count) {
  45. if (2 & (unsigned long) buff) {
  46. result += *(unsigned short *) buff;
  47. count--;
  48. len -= 2;
  49. buff += 2;
  50. }
  51. count >>= 1; /* nr of 32-bit words.. */
  52. if (count) {
  53. if (4 & (unsigned long) buff) {
  54. result += *(unsigned int *) buff;
  55. count--;
  56. len -= 4;
  57. buff += 4;
  58. }
  59. count >>= 1; /* nr of 64-bit words.. */
  60. if (count) {
  61. unsigned long carry = 0;
  62. do {
  63. unsigned long w = *(unsigned long *) buff;
  64. count--;
  65. buff += 8;
  66. result += carry;
  67. result += w;
  68. carry = (w > result);
  69. } while (count);
  70. result += carry;
  71. result = (result & 0xffffffff) + (result >> 32);
  72. }
  73. if (len & 4) {
  74. result += *(unsigned int *) buff;
  75. buff += 4;
  76. }
  77. }
  78. if (len & 2) {
  79. result += *(unsigned short *) buff;
  80. buff += 2;
  81. }
  82. }
  83. if (len & 1)
  84. result += *buff;
  85. result = from64to16(result);
  86. if (odd)
  87. result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
  88. out:
  89. return result;
  90. }
  91. /*
  92. * XXX Fixme
  93. *
  94. * This is very ugly but temporary. THIS NEEDS SERIOUS ENHANCEMENTS.
  95. * But it's very tricky to get right even in C.
  96. */
  97. extern unsigned long do_csum(const unsigned char *, long);
  98. __wsum
  99. csum_partial_copy_from_user(const void __user *src, void *dst,
  100. int len, __wsum psum, int *errp)
  101. {
  102. unsigned long result;
  103. /* XXX Fixme
  104. * for now we separate the copy from checksum for obvious
  105. * alignment difficulties. Look at the Alpha code and you'll be
  106. * scared.
  107. */
  108. if (__copy_from_user(dst, src, len) != 0 && errp)
  109. *errp = -EFAULT;
  110. result = do_csum(dst, len);
  111. /* add in old sum, and carry.. */
  112. result += (__force u32)psum;
  113. /* 32+c bits -> 32 bits */
  114. result = (result & 0xffffffff) + (result >> 32);
  115. return (__force __wsum)result;
  116. }
  117. EXPORT_SYMBOL(csum_partial_copy_from_user);
  118. __wsum
  119. csum_partial_copy_nocheck(const void *src, void *dst, int len, __wsum sum)
  120. {
  121. return csum_partial_copy_from_user((__force const void __user *)src,
  122. dst, len, sum, NULL);
  123. }
  124. EXPORT_SYMBOL(csum_partial_copy_nocheck);