checksum.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * INET An implementation of the TCP/IP protocol suite for the LINUX
  4. * operating system. INET is implemented using the BSD Socket
  5. * interface as the means of communication with the user level.
  6. *
  7. * Checksumming functions for IP, TCP, UDP and so on
  8. *
  9. * Authors: Jorge Cwik, <jorge@laser.satlink.net>
  10. * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
  11. * Borrows very liberally from tcp.c and ip.c, see those
  12. * files for more names.
  13. */
  14. #ifndef _CHECKSUM_H
  15. #define _CHECKSUM_H
  16. #include <linux/errno.h>
  17. #include <asm/types.h>
  18. #include <asm/byteorder.h>
  19. #include <asm/checksum.h>
  20. #if !defined(_HAVE_ARCH_COPY_AND_CSUM_FROM_USER) || !defined(HAVE_CSUM_COPY_USER)
  21. #include <linux/uaccess.h>
  22. #endif
  23. #ifndef _HAVE_ARCH_COPY_AND_CSUM_FROM_USER
  24. static __always_inline
  25. __wsum csum_and_copy_from_user (const void __user *src, void *dst,
  26. int len)
  27. {
  28. if (copy_from_user(dst, src, len))
  29. return 0;
  30. return csum_partial(dst, len, ~0U);
  31. }
  32. #endif
  33. #ifndef HAVE_CSUM_COPY_USER
  34. static __always_inline __wsum csum_and_copy_to_user
  35. (const void *src, void __user *dst, int len)
  36. {
  37. __wsum sum = csum_partial(src, len, ~0U);
  38. if (copy_to_user(dst, src, len) == 0)
  39. return sum;
  40. return 0;
  41. }
  42. #endif
  43. #ifndef _HAVE_ARCH_CSUM_AND_COPY
  44. static __always_inline __wsum
  45. csum_partial_copy_nocheck(const void *src, void *dst, int len)
  46. {
  47. memcpy(dst, src, len);
  48. return csum_partial(dst, len, 0);
  49. }
  50. #endif
  51. #ifndef HAVE_ARCH_CSUM_ADD
  52. static __always_inline __wsum csum_add(__wsum csum, __wsum addend)
  53. {
  54. u32 res = (__force u32)csum;
  55. res += (__force u32)addend;
  56. return (__force __wsum)(res + (res < (__force u32)addend));
  57. }
  58. #endif
  59. static __always_inline __wsum csum_sub(__wsum csum, __wsum addend)
  60. {
  61. return csum_add(csum, ~addend);
  62. }
  63. static __always_inline __sum16 csum16_add(__sum16 csum, __be16 addend)
  64. {
  65. u16 res = (__force u16)csum;
  66. res += (__force u16)addend;
  67. return (__force __sum16)(res + (res < (__force u16)addend));
  68. }
  69. static __always_inline __sum16 csum16_sub(__sum16 csum, __be16 addend)
  70. {
  71. return csum16_add(csum, ~addend);
  72. }
  73. #ifndef HAVE_ARCH_CSUM_SHIFT
  74. static __always_inline __wsum csum_shift(__wsum sum, int offset)
  75. {
  76. /* rotate sum to align it with a 16b boundary */
  77. if (offset & 1)
  78. return (__force __wsum)ror32((__force u32)sum, 8);
  79. return sum;
  80. }
  81. #endif
  82. static __always_inline __wsum
  83. csum_block_add(__wsum csum, __wsum csum2, int offset)
  84. {
  85. return csum_add(csum, csum_shift(csum2, offset));
  86. }
  87. static __always_inline __wsum
  88. csum_block_add_ext(__wsum csum, __wsum csum2, int offset, int len)
  89. {
  90. return csum_block_add(csum, csum2, offset);
  91. }
  92. static __always_inline __wsum
  93. csum_block_sub(__wsum csum, __wsum csum2, int offset)
  94. {
  95. return csum_block_add(csum, ~csum2, offset);
  96. }
  97. static __always_inline __wsum csum_unfold(__sum16 n)
  98. {
  99. return (__force __wsum)n;
  100. }
  101. static __always_inline
  102. __wsum csum_partial_ext(const void *buff, int len, __wsum sum)
  103. {
  104. return csum_partial(buff, len, sum);
  105. }
  106. #define CSUM_MANGLED_0 ((__force __sum16)0xffff)
  107. static __always_inline void csum_replace_by_diff(__sum16 *sum, __wsum diff)
  108. {
  109. *sum = csum_fold(csum_add(diff, ~csum_unfold(*sum)));
  110. }
  111. static __always_inline void csum_replace4(__sum16 *sum, __be32 from, __be32 to)
  112. {
  113. __wsum tmp = csum_sub(~csum_unfold(*sum), (__force __wsum)from);
  114. *sum = csum_fold(csum_add(tmp, (__force __wsum)to));
  115. }
  116. /* Implements RFC 1624 (Incremental Internet Checksum)
  117. * 3. Discussion states :
  118. * HC' = ~(~HC + ~m + m')
  119. * m : old value of a 16bit field
  120. * m' : new value of a 16bit field
  121. */
  122. static __always_inline void csum_replace2(__sum16 *sum, __be16 old, __be16 new)
  123. {
  124. *sum = ~csum16_add(csum16_sub(~(*sum), old), new);
  125. }
  126. static inline void csum_replace(__wsum *csum, __wsum old, __wsum new)
  127. {
  128. *csum = csum_add(csum_sub(*csum, old), new);
  129. }
  130. struct sk_buff;
  131. void inet_proto_csum_replace4(__sum16 *sum, struct sk_buff *skb,
  132. __be32 from, __be32 to, bool pseudohdr);
  133. void inet_proto_csum_replace16(__sum16 *sum, struct sk_buff *skb,
  134. const __be32 *from, const __be32 *to,
  135. bool pseudohdr);
  136. void inet_proto_csum_replace_by_diff(__sum16 *sum, struct sk_buff *skb,
  137. __wsum diff, bool pseudohdr, bool ipv6);
  138. static __always_inline
  139. void inet_proto_csum_replace2(__sum16 *sum, struct sk_buff *skb,
  140. __be16 from, __be16 to, bool pseudohdr)
  141. {
  142. inet_proto_csum_replace4(sum, skb, (__force __be32)from,
  143. (__force __be32)to, pseudohdr);
  144. }
  145. static __always_inline __wsum remcsum_adjust(void *ptr, __wsum csum,
  146. int start, int offset)
  147. {
  148. __sum16 *psum = (__sum16 *)(ptr + offset);
  149. __wsum delta;
  150. /* Subtract out checksum up to start */
  151. csum = csum_sub(csum, csum_partial(ptr, start, 0));
  152. /* Set derived checksum in packet */
  153. delta = csum_sub((__force __wsum)csum_fold(csum),
  154. (__force __wsum)*psum);
  155. *psum = csum_fold(csum);
  156. return delta;
  157. }
  158. static __always_inline void remcsum_unadjust(__sum16 *psum, __wsum delta)
  159. {
  160. *psum = csum_fold(csum_sub(delta, (__force __wsum)*psum));
  161. }
  162. static __always_inline __wsum wsum_negate(__wsum val)
  163. {
  164. return (__force __wsum)-((__force u32)val);
  165. }
  166. #endif