checksum.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * MIPS specific IP/TCP/UDP checksumming routines
  7. *
  8. * Authors: Ralf Baechle, <ralf@waldorf-gmbh.de>
  9. * Lots of code moved from tcp.c and ip.c; see those files
  10. * for more names.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. *
  17. * $Id: checksum.c,v 1.3 1997/12/01 17:57:34 ralf Exp $
  18. */
  19. #include <linux/module.h>
  20. #include <linux/types.h>
  21. #include <net/checksum.h>
  22. #include <asm/byteorder.h>
  23. #include <asm/string.h>
  24. #include <asm/uaccess.h>
  25. #define addc(_t,_r) \
  26. __asm__ __volatile__ ( \
  27. " add %0, %1, %0\n" \
  28. " addc %0, %%r0, %0\n" \
  29. : "=r"(_t) \
  30. : "r"(_r), "0"(_t));
  31. static inline unsigned short from32to16(unsigned int x)
  32. {
  33. /* 32 bits --> 16 bits + carry */
  34. x = (x & 0xffff) + (x >> 16);
  35. /* 16 bits + carry --> 16 bits including carry */
  36. x = (x & 0xffff) + (x >> 16);
  37. return (unsigned short)x;
  38. }
  39. static inline unsigned int do_csum(const unsigned char * buff, int len)
  40. {
  41. int odd, count;
  42. unsigned int result = 0;
  43. if (len <= 0)
  44. goto out;
  45. odd = 1 & (unsigned long) buff;
  46. if (odd) {
  47. result = be16_to_cpu(*buff);
  48. len--;
  49. buff++;
  50. }
  51. count = len >> 1; /* nr of 16-bit words.. */
  52. if (count) {
  53. if (2 & (unsigned long) buff) {
  54. result += *(unsigned short *) buff;
  55. count--;
  56. len -= 2;
  57. buff += 2;
  58. }
  59. count >>= 1; /* nr of 32-bit words.. */
  60. if (count) {
  61. while (count >= 4) {
  62. unsigned int r1, r2, r3, r4;
  63. r1 = *(unsigned int *)(buff + 0);
  64. r2 = *(unsigned int *)(buff + 4);
  65. r3 = *(unsigned int *)(buff + 8);
  66. r4 = *(unsigned int *)(buff + 12);
  67. addc(result, r1);
  68. addc(result, r2);
  69. addc(result, r3);
  70. addc(result, r4);
  71. count -= 4;
  72. buff += 16;
  73. }
  74. while (count) {
  75. unsigned int w = *(unsigned int *) buff;
  76. count--;
  77. buff += 4;
  78. addc(result, w);
  79. }
  80. result = (result & 0xffff) + (result >> 16);
  81. }
  82. if (len & 2) {
  83. result += *(unsigned short *) buff;
  84. buff += 2;
  85. }
  86. }
  87. if (len & 1)
  88. result += le16_to_cpu(*buff);
  89. result = from32to16(result);
  90. if (odd)
  91. result = swab16(result);
  92. out:
  93. return result;
  94. }
  95. /*
  96. * computes a partial checksum, e.g. for TCP/UDP fragments
  97. */
  98. /*
  99. * why bother folding?
  100. */
  101. __wsum csum_partial(const void *buff, int len, __wsum sum)
  102. {
  103. unsigned int result = do_csum(buff, len);
  104. addc(result, sum);
  105. return (__force __wsum)from32to16(result);
  106. }
  107. EXPORT_SYMBOL(csum_partial);
  108. /*
  109. * copy while checksumming, otherwise like csum_partial
  110. */
  111. __wsum csum_partial_copy_nocheck(const void *src, void *dst,
  112. int len, __wsum sum)
  113. {
  114. /*
  115. * It's 2:30 am and I don't feel like doing it real ...
  116. * This is lots slower than the real thing (tm)
  117. */
  118. sum = csum_partial(src, len, sum);
  119. memcpy(dst, src, len);
  120. return sum;
  121. }
  122. EXPORT_SYMBOL(csum_partial_copy_nocheck);
  123. /*
  124. * Copy from userspace and compute checksum. If we catch an exception
  125. * then zero the rest of the buffer.
  126. */
  127. __wsum csum_partial_copy_from_user(const void __user *src,
  128. void *dst, int len,
  129. __wsum sum, int *err_ptr)
  130. {
  131. int missing;
  132. missing = copy_from_user(dst, src, len);
  133. if (missing) {
  134. memset(dst + len - missing, 0, missing);
  135. *err_ptr = -EFAULT;
  136. }
  137. return csum_partial(dst, len, sum);
  138. }
  139. EXPORT_SYMBOL(csum_partial_copy_from_user);