checksum.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Network checksum routines
  3. *
  4. * Copyright (C) 1999, 2003 Hewlett-Packard Co
  5. * Stephane Eranian <eranian@hpl.hp.com>
  6. *
  7. * Most of the code coming from arch/alpha/lib/checksum.c
  8. *
  9. * This file contains network checksum routines that are better done
  10. * in an architecture-specific manner due to speed..
  11. */
  12. #include <linux/module.h>
  13. #include <linux/string.h>
  14. #include <asm/byteorder.h>
  15. static inline unsigned short
  16. from64to16 (unsigned long x)
  17. {
  18. /* add up 32-bit words for 33 bits */
  19. x = (x & 0xffffffff) + (x >> 32);
  20. /* add up 16-bit and 17-bit words for 17+c bits */
  21. x = (x & 0xffff) + (x >> 16);
  22. /* add up 16-bit and 2-bit for 16+c bit */
  23. x = (x & 0xffff) + (x >> 16);
  24. /* add up carry.. */
  25. x = (x & 0xffff) + (x >> 16);
  26. return x;
  27. }
  28. /*
  29. * computes the checksum of the TCP/UDP pseudo-header
  30. * returns a 16-bit checksum, already complemented.
  31. */
  32. __sum16
  33. csum_tcpudp_magic (__be32 saddr, __be32 daddr, unsigned short len,
  34. unsigned short proto, __wsum sum)
  35. {
  36. return (__force __sum16)~from64to16(
  37. (__force u64)saddr + (__force u64)daddr +
  38. (__force u64)sum + ((len + proto) << 8));
  39. }
  40. EXPORT_SYMBOL(csum_tcpudp_magic);
  41. __wsum
  42. csum_tcpudp_nofold (__be32 saddr, __be32 daddr, unsigned short len,
  43. unsigned short proto, __wsum sum)
  44. {
  45. unsigned long result;
  46. result = (__force u64)saddr + (__force u64)daddr +
  47. (__force u64)sum + ((len + proto) << 8);
  48. /* Fold down to 32-bits so we don't lose in the typedef-less network stack. */
  49. /* 64 to 33 */
  50. result = (result & 0xffffffff) + (result >> 32);
  51. /* 33 to 32 */
  52. result = (result & 0xffffffff) + (result >> 32);
  53. return (__force __wsum)result;
  54. }
  55. extern unsigned long do_csum (const unsigned char *, long);
  56. /*
  57. * computes the checksum of a memory block at buff, length len,
  58. * and adds in "sum" (32-bit)
  59. *
  60. * returns a 32-bit number suitable for feeding into itself
  61. * or csum_tcpudp_magic
  62. *
  63. * this function must be called with even lengths, except
  64. * for the last fragment, which may be odd
  65. *
  66. * it's best to have buff aligned on a 32-bit boundary
  67. */
  68. __wsum csum_partial(const void *buff, int len, __wsum sum)
  69. {
  70. u64 result = do_csum(buff, len);
  71. /* add in old sum, and carry.. */
  72. result += (__force u32)sum;
  73. /* 32+c bits -> 32 bits */
  74. result = (result & 0xffffffff) + (result >> 32);
  75. return (__force __wsum)result;
  76. }
  77. EXPORT_SYMBOL(csum_partial);
  78. /*
  79. * this routine is used for miscellaneous IP-like checksums, mainly
  80. * in icmp.c
  81. */
  82. __sum16 ip_compute_csum (const void *buff, int len)
  83. {
  84. return (__force __sum16)~do_csum(buff,len);
  85. }
  86. EXPORT_SYMBOL(ip_compute_csum);