csum_partial_copy.c 3.0 KB

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