checksum.c 2.5 KB

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