checksum.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * File: arch/blackfin/lib/checksum.c
  3. * Based on: none - original work
  4. * Author:
  5. *
  6. * Created:
  7. * Description: An implementation of the TCP/IP protocol suite for the LINUX
  8. * operating system. INET is implemented using the BSD Socket
  9. * interface as the means of communication with the user level.
  10. *
  11. * Modified:
  12. * Copyright 2004-2006 Analog Devices Inc.
  13. *
  14. * Bugs: Enter bugs at http://blackfin.uclinux.org/
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, see the file COPYING, or write
  28. * to the Free Software Foundation, Inc.,
  29. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  30. */
  31. #include <net/checksum.h>
  32. #include <asm/checksum.h>
  33. #ifdef CONFIG_IP_CHECKSUM_L1
  34. static unsigned short do_csum(const unsigned char *buff, int len)__attribute__((l1_text));
  35. #endif
  36. static unsigned short do_csum(const unsigned char *buff, int len)
  37. {
  38. register unsigned long sum = 0;
  39. int swappem = 0;
  40. if (1 & (unsigned long)buff) {
  41. sum = *buff << 8;
  42. buff++;
  43. len--;
  44. ++swappem;
  45. }
  46. while (len > 1) {
  47. sum += *(unsigned short *)buff;
  48. buff += 2;
  49. len -= 2;
  50. }
  51. if (len > 0)
  52. sum += *buff;
  53. /* Fold 32-bit sum to 16 bits */
  54. while (sum >> 16)
  55. sum = (sum & 0xffff) + (sum >> 16);
  56. if (swappem)
  57. sum = ((sum & 0xff00) >> 8) + ((sum & 0x00ff) << 8);
  58. return sum;
  59. }
  60. /*
  61. * This is a version of ip_compute_csum() optimized for IP headers,
  62. * which always checksum on 4 octet boundaries.
  63. */
  64. unsigned short ip_fast_csum(unsigned char *iph, unsigned int ihl)
  65. {
  66. return ~do_csum(iph, ihl * 4);
  67. }
  68. /*
  69. * computes the checksum of a memory block at buff, length len,
  70. * and adds in "sum" (32-bit)
  71. *
  72. * returns a 32-bit number suitable for feeding into itself
  73. * or csum_tcpudp_magic
  74. *
  75. * this function must be called with even lengths, except
  76. * for the last fragment, which may be odd
  77. *
  78. * it's best to have buff aligned on a 32-bit boundary
  79. */
  80. unsigned int csum_partial(const unsigned char *buff, int len, unsigned int sum)
  81. {
  82. /*
  83. * Just in case we get nasty checksum data...
  84. * Like 0xffff6ec3 in the case of our IPv6 multicast header.
  85. * We fold to begin with, as well as at the end.
  86. */
  87. sum = (sum & 0xffff) + (sum >> 16);
  88. sum += do_csum(buff, len);
  89. sum = (sum & 0xffff) + (sum >> 16);
  90. return sum;
  91. }
  92. /*
  93. * this routine is used for miscellaneous IP-like checksums, mainly
  94. * in icmp.c
  95. */
  96. unsigned short ip_compute_csum(const unsigned char *buff, int len)
  97. {
  98. return ~do_csum(buff, len);
  99. }
  100. /*
  101. * copy from fs while checksumming, otherwise like csum_partial
  102. */
  103. unsigned int
  104. csum_partial_copy_from_user(const unsigned char *src, unsigned char *dst,
  105. int len, int sum, int *csum_err)
  106. {
  107. if (csum_err)
  108. *csum_err = 0;
  109. memcpy(dst, src, len);
  110. return csum_partial(dst, len, sum);
  111. }
  112. /*
  113. * copy from ds while checksumming, otherwise like csum_partial
  114. */
  115. unsigned int csum_partial_copy(const unsigned char *src, unsigned char *dst,
  116. int len, int sum)
  117. {
  118. memcpy(dst, src, len);
  119. return csum_partial(dst, len, sum);
  120. }