checksum.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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.1 2002/09/28 14:58:40 gerg Exp $
  18. */
  19. #include <net/checksum.h>
  20. #include <linux/module.h>
  21. #include <linux/types.h>
  22. #include <asm/byteorder.h>
  23. #include <asm/string.h>
  24. #include <asm/uaccess.h>
  25. static inline unsigned short from32to16 (unsigned long sum)
  26. {
  27. unsigned int result;
  28. /*
  29. %0 %1
  30. hsw %1, %0 H L L H
  31. add %1, %0 H L H+L+C H+L
  32. */
  33. asm ("hsw %1, %0; add %1, %0" : "=&r" (result) : "r" (sum));
  34. return result >> 16;
  35. }
  36. static inline unsigned int do_csum(const unsigned char * buff, int len)
  37. {
  38. int odd, count;
  39. unsigned int result = 0;
  40. if (len <= 0)
  41. goto out;
  42. odd = 1 & (unsigned long) buff;
  43. if (odd) {
  44. result = be16_to_cpu(*buff);
  45. len--;
  46. buff++;
  47. }
  48. count = len >> 1; /* nr of 16-bit words.. */
  49. if (count) {
  50. if (2 & (unsigned long) buff) {
  51. result += *(unsigned short *) buff;
  52. count--;
  53. len -= 2;
  54. buff += 2;
  55. }
  56. count >>= 1; /* nr of 32-bit words.. */
  57. if (count) {
  58. unsigned int carry = 0;
  59. do {
  60. unsigned int w = *(unsigned int *) buff;
  61. count--;
  62. buff += 4;
  63. result += carry;
  64. result += w;
  65. carry = (w > result);
  66. } while (count);
  67. result += carry;
  68. result = (result & 0xffff) + (result >> 16);
  69. }
  70. if (len & 2) {
  71. result += *(unsigned short *) buff;
  72. buff += 2;
  73. }
  74. }
  75. if (len & 1)
  76. result += le16_to_cpu(*buff);
  77. result = from32to16(result);
  78. if (odd)
  79. result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
  80. out:
  81. return result;
  82. }
  83. /*
  84. * This is a version of ip_compute_csum() optimized for IP headers,
  85. * which always checksum on 4 octet boundaries.
  86. */
  87. unsigned short ip_fast_csum(unsigned char * iph, unsigned int ihl)
  88. {
  89. return ~do_csum(iph,ihl*4);
  90. }
  91. /*
  92. * this routine is used for miscellaneous IP-like checksums, mainly
  93. * in icmp.c
  94. */
  95. unsigned short ip_compute_csum(const unsigned char * buff, int len)
  96. {
  97. return ~do_csum(buff,len);
  98. }
  99. /*
  100. * computes a partial checksum, e.g. for TCP/UDP fragments
  101. */
  102. unsigned int csum_partial(const unsigned char *buff, int len, unsigned int sum)
  103. {
  104. unsigned int result = do_csum(buff, len);
  105. /* add in old sum, and carry.. */
  106. result += sum;
  107. if(sum > result)
  108. result += 1;
  109. return result;
  110. }
  111. EXPORT_SYMBOL(csum_partial);
  112. /*
  113. * copy while checksumming, otherwise like csum_partial
  114. */
  115. unsigned int csum_partial_copy(const unsigned char *src, unsigned char *dst,
  116. int len, unsigned int sum)
  117. {
  118. /*
  119. * It's 2:30 am and I don't feel like doing it real ...
  120. * This is lots slower than the real thing (tm)
  121. */
  122. sum = csum_partial(src, len, sum);
  123. memcpy(dst, src, len);
  124. return sum;
  125. }
  126. /*
  127. * Copy from userspace and compute checksum. If we catch an exception
  128. * then zero the rest of the buffer.
  129. */
  130. unsigned int csum_partial_copy_from_user (const unsigned char *src,
  131. unsigned char *dst,
  132. int len, unsigned int sum,
  133. int *err_ptr)
  134. {
  135. int missing;
  136. missing = copy_from_user(dst, src, len);
  137. if (missing) {
  138. memset(dst + len - missing, 0, missing);
  139. *err_ptr = -EFAULT;
  140. }
  141. return csum_partial(dst, len, sum);
  142. }