checksum.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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, __u32 len,
  34. __u8 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, __u32 len,
  43. __u8 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. EXPORT_SYMBOL(csum_tcpudp_nofold);
  56. extern unsigned long do_csum (const unsigned char *, long);
  57. /*
  58. * computes the checksum of a memory block at buff, length len,
  59. * and adds in "sum" (32-bit)
  60. *
  61. * returns a 32-bit number suitable for feeding into itself
  62. * or csum_tcpudp_magic
  63. *
  64. * this function must be called with even lengths, except
  65. * for the last fragment, which may be odd
  66. *
  67. * it's best to have buff aligned on a 32-bit boundary
  68. */
  69. __wsum csum_partial(const void *buff, int len, __wsum sum)
  70. {
  71. u64 result = do_csum(buff, len);
  72. /* add in old sum, and carry.. */
  73. result += (__force u32)sum;
  74. /* 32+c bits -> 32 bits */
  75. result = (result & 0xffffffff) + (result >> 32);
  76. return (__force __wsum)result;
  77. }
  78. EXPORT_SYMBOL(csum_partial);
  79. /*
  80. * this routine is used for miscellaneous IP-like checksums, mainly
  81. * in icmp.c
  82. */
  83. __sum16 ip_compute_csum (const void *buff, int len)
  84. {
  85. return (__force __sum16)~do_csum(buff,len);
  86. }
  87. EXPORT_SYMBOL(ip_compute_csum);