checksum.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright 2004-2009 Analog Devices Inc.
  3. *
  4. * Licensed under the GPL-2 or later.
  5. *
  6. * An implementation of the TCP/IP protocol suite for the LINUX operating
  7. * system. INET is implemented using the BSD Socket interface as the
  8. * means of communication with the user level.
  9. *
  10. */
  11. #include <linux/module.h>
  12. #include <net/checksum.h>
  13. #include <asm/checksum.h>
  14. #ifdef CONFIG_IP_CHECKSUM_L1
  15. static unsigned short do_csum(const unsigned char *buff, int len)__attribute__((l1_text));
  16. #endif
  17. static unsigned short do_csum(const unsigned char *buff, int len)
  18. {
  19. register unsigned long sum = 0;
  20. int swappem = 0;
  21. if (1 & (unsigned long)buff) {
  22. sum = *buff << 8;
  23. buff++;
  24. len--;
  25. ++swappem;
  26. }
  27. while (len > 1) {
  28. sum += *(unsigned short *)buff;
  29. buff += 2;
  30. len -= 2;
  31. }
  32. if (len > 0)
  33. sum += *buff;
  34. /* Fold 32-bit sum to 16 bits */
  35. while (sum >> 16)
  36. sum = (sum & 0xffff) + (sum >> 16);
  37. if (swappem)
  38. sum = ((sum & 0xff00) >> 8) + ((sum & 0x00ff) << 8);
  39. return sum;
  40. }
  41. /*
  42. * This is a version of ip_compute_csum() optimized for IP headers,
  43. * which always checksum on 4 octet boundaries.
  44. */
  45. __sum16 ip_fast_csum(unsigned char *iph, unsigned int ihl)
  46. {
  47. return (__force __sum16)~do_csum(iph, ihl * 4);
  48. }
  49. EXPORT_SYMBOL(ip_fast_csum);
  50. /*
  51. * computes the checksum of a memory block at buff, length len,
  52. * and adds in "sum" (32-bit)
  53. *
  54. * returns a 32-bit number suitable for feeding into itself
  55. * or csum_tcpudp_magic
  56. *
  57. * this function must be called with even lengths, except
  58. * for the last fragment, which may be odd
  59. *
  60. * it's best to have buff aligned on a 32-bit boundary
  61. */
  62. __wsum csum_partial(const void *buff, int len, __wsum sum)
  63. {
  64. /*
  65. * Just in case we get nasty checksum data...
  66. * Like 0xffff6ec3 in the case of our IPv6 multicast header.
  67. * We fold to begin with, as well as at the end.
  68. */
  69. sum = (sum & 0xffff) + (sum >> 16);
  70. sum += do_csum(buff, len);
  71. sum = (sum & 0xffff) + (sum >> 16);
  72. return sum;
  73. }
  74. EXPORT_SYMBOL(csum_partial);
  75. /*
  76. * this routine is used for miscellaneous IP-like checksums, mainly
  77. * in icmp.c
  78. */
  79. __sum16 ip_compute_csum(const void *buff, int len)
  80. {
  81. return (__force __sum16)~do_csum(buff, len);
  82. }
  83. EXPORT_SYMBOL(ip_compute_csum);
  84. /*
  85. * copy from fs while checksumming, otherwise like csum_partial
  86. */
  87. __wsum
  88. csum_partial_copy_from_user(const void __user *src, void *dst,
  89. int len, __wsum sum, int *csum_err)
  90. {
  91. if (csum_err)
  92. *csum_err = 0;
  93. memcpy(dst, (__force void *)src, len);
  94. return csum_partial(dst, len, sum);
  95. }
  96. EXPORT_SYMBOL(csum_partial_copy_from_user);
  97. /*
  98. * copy from ds while checksumming, otherwise like csum_partial
  99. */
  100. __wsum csum_partial_copy(const void *src, void *dst, int len, __wsum sum)
  101. {
  102. memcpy(dst, src, len);
  103. return csum_partial(dst, len, sum);
  104. }
  105. EXPORT_SYMBOL(csum_partial_copy);