checksum.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /* SCTP kernel reference Implementation
  3. * Copyright (c) 1999-2001 Motorola, Inc.
  4. * Copyright (c) 2001-2003 International Business Machines, Corp.
  5. *
  6. * This file is part of the SCTP kernel reference Implementation
  7. *
  8. * SCTP Checksum functions
  9. *
  10. * Please send any bug reports or fixes you make to the
  11. * email address(es):
  12. * lksctp developers <[email protected]>
  13. *
  14. * Written or modified by:
  15. * Dinakaran Joseph
  16. * Jon Grimm <[email protected]>
  17. * Sridhar Samudrala <[email protected]>
  18. *
  19. * Rewritten to use libcrc32c by:
  20. * Vlad Yasevich <[email protected]>
  21. */
  22. #ifndef __sctp_checksum_h__
  23. #define __sctp_checksum_h__
  24. #include <linux/types.h>
  25. #include <net/sctp/sctp.h>
  26. #include <linux/crc32c.h>
  27. #include <linux/crc32.h>
  28. static inline __wsum sctp_csum_update(const void *buff, int len, __wsum sum)
  29. {
  30. /* This uses the crypto implementation of crc32c, which is either
  31. * implemented w/ hardware support or resolves to __crc32c_le().
  32. */
  33. return (__force __wsum)crc32c((__force __u32)sum, buff, len);
  34. }
  35. static inline __wsum sctp_csum_combine(__wsum csum, __wsum csum2,
  36. int offset, int len)
  37. {
  38. return (__force __wsum)__crc32c_le_combine((__force __u32)csum,
  39. (__force __u32)csum2, len);
  40. }
  41. static const struct skb_checksum_ops sctp_csum_ops = {
  42. .update = sctp_csum_update,
  43. .combine = sctp_csum_combine,
  44. };
  45. static inline __le32 sctp_compute_cksum(const struct sk_buff *skb,
  46. unsigned int offset)
  47. {
  48. struct sctphdr *sh = (struct sctphdr *)(skb->data + offset);
  49. __le32 old = sh->checksum;
  50. __wsum new;
  51. sh->checksum = 0;
  52. new = ~__skb_checksum(skb, offset, skb->len - offset, ~(__wsum)0,
  53. &sctp_csum_ops);
  54. sh->checksum = old;
  55. return cpu_to_le32((__force __u32)new);
  56. }
  57. #endif /* __sctp_checksum_h__ */