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. unsigned short int
  33. csum_tcpudp_magic (unsigned long saddr, unsigned long daddr, unsigned short len,
  34. unsigned short proto, unsigned int sum)
  35. {
  36. return ~from64to16(saddr + daddr + sum + ((unsigned long) ntohs(len) << 16) +
  37. ((unsigned long) proto << 8));
  38. }
  39. EXPORT_SYMBOL(csum_tcpudp_magic);
  40. unsigned int
  41. csum_tcpudp_nofold (unsigned long saddr, unsigned long daddr, unsigned short len,
  42. unsigned short proto, unsigned int sum)
  43. {
  44. unsigned long result;
  45. result = (saddr + daddr + sum +
  46. ((unsigned long) ntohs(len) << 16) +
  47. ((unsigned long) 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 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. unsigned int
  69. csum_partial (const unsigned char * buff, int len, unsigned int sum)
  70. {
  71. unsigned long result = do_csum(buff, len);
  72. /* add in old sum, and carry.. */
  73. result += sum;
  74. /* 32+c bits -> 32 bits */
  75. result = (result & 0xffffffff) + (result >> 32);
  76. return 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. unsigned short
  84. ip_compute_csum (unsigned char * buff, int len)
  85. {
  86. return ~do_csum(buff,len);
  87. }
  88. EXPORT_SYMBOL(ip_compute_csum);