checksum.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * Checksumming functions for IP, TCP, UDP and so on
  7. *
  8. * Authors: Jorge Cwik, <jorge@laser.satlink.net>
  9. * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
  10. * Borrows very liberally from tcp.c and ip.c, see those
  11. * files for more names.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License
  15. * as published by the Free Software Foundation; either version
  16. * 2 of the License, or (at your option) any later version.
  17. */
  18. #ifndef _CHECKSUM_H
  19. #define _CHECKSUM_H
  20. #include <linux/errno.h>
  21. #include <asm/types.h>
  22. #include <asm/byteorder.h>
  23. #include <asm/uaccess.h>
  24. #include <asm/checksum.h>
  25. #ifndef _HAVE_ARCH_COPY_AND_CSUM_FROM_USER
  26. static inline
  27. unsigned int csum_and_copy_from_user (const unsigned char __user *src, unsigned char *dst,
  28. int len, int sum, int *err_ptr)
  29. {
  30. if (access_ok(VERIFY_READ, src, len))
  31. return csum_partial_copy_from_user(src, dst, len, sum, err_ptr);
  32. if (len)
  33. *err_ptr = -EFAULT;
  34. return sum;
  35. }
  36. #endif
  37. #ifndef HAVE_CSUM_COPY_USER
  38. static __inline__ unsigned int csum_and_copy_to_user
  39. (const unsigned char *src, unsigned char __user *dst, int len, unsigned int sum, int *err_ptr)
  40. {
  41. sum = csum_partial(src, len, sum);
  42. if (access_ok(VERIFY_WRITE, dst, len)) {
  43. if (copy_to_user(dst, src, len) == 0)
  44. return sum;
  45. }
  46. if (len)
  47. *err_ptr = -EFAULT;
  48. return -1; /* invalid checksum */
  49. }
  50. #endif
  51. static inline unsigned int csum_add(unsigned int csum, unsigned int addend)
  52. {
  53. csum += addend;
  54. return csum + (csum < addend);
  55. }
  56. static inline unsigned int csum_sub(unsigned int csum, unsigned int addend)
  57. {
  58. return csum_add(csum, ~addend);
  59. }
  60. static inline unsigned int
  61. csum_block_add(unsigned int csum, unsigned int csum2, int offset)
  62. {
  63. if (offset&1)
  64. csum2 = ((csum2&0xFF00FF)<<8)+((csum2>>8)&0xFF00FF);
  65. return csum_add(csum, csum2);
  66. }
  67. static inline unsigned int
  68. csum_block_sub(unsigned int csum, unsigned int csum2, int offset)
  69. {
  70. if (offset&1)
  71. csum2 = ((csum2&0xFF00FF)<<8)+((csum2>>8)&0xFF00FF);
  72. return csum_sub(csum, csum2);
  73. }
  74. #endif