csum-wrappers.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* Copyright 2002,2003 Andi Kleen, SuSE Labs.
  2. * Subject to the GNU Public License v.2
  3. *
  4. * Wrappers of assembly checksum functions for x86-64.
  5. */
  6. #include <asm/checksum.h>
  7. #include <linux/module.h>
  8. /**
  9. * csum_partial_copy_from_user - Copy and checksum from user space.
  10. * @src: source address (user space)
  11. * @dst: destination address
  12. * @len: number of bytes to be copied.
  13. * @isum: initial sum that is added into the result (32bit unfolded)
  14. * @errp: set to -EFAULT for an bad source address.
  15. *
  16. * Returns an 32bit unfolded checksum of the buffer.
  17. * src and dst are best aligned to 64bits.
  18. */
  19. unsigned int
  20. csum_partial_copy_from_user(const unsigned char __user *src, unsigned char *dst,
  21. int len, unsigned int isum, int *errp)
  22. {
  23. might_sleep();
  24. *errp = 0;
  25. if (likely(access_ok(VERIFY_READ,src, len))) {
  26. /* Why 6, not 7? To handle odd addresses aligned we
  27. would need to do considerable complications to fix the
  28. checksum which is defined as an 16bit accumulator. The
  29. fix alignment code is primarily for performance
  30. compatibility with 32bit and that will handle odd
  31. addresses slowly too. */
  32. if (unlikely((unsigned long)src & 6)) {
  33. while (((unsigned long)src & 6) && len >= 2) {
  34. __u16 val16;
  35. *errp = __get_user(val16, (__u16 __user *)src);
  36. if (*errp)
  37. return isum;
  38. *(__u16 *)dst = val16;
  39. isum = add32_with_carry(isum, val16);
  40. src += 2;
  41. dst += 2;
  42. len -= 2;
  43. }
  44. }
  45. isum = csum_partial_copy_generic((__force void *)src,dst,len,isum,errp,NULL);
  46. if (likely(*errp == 0))
  47. return isum;
  48. }
  49. *errp = -EFAULT;
  50. memset(dst,0,len);
  51. return isum;
  52. }
  53. EXPORT_SYMBOL(csum_partial_copy_from_user);
  54. /**
  55. * csum_partial_copy_to_user - Copy and checksum to user space.
  56. * @src: source address
  57. * @dst: destination address (user space)
  58. * @len: number of bytes to be copied.
  59. * @isum: initial sum that is added into the result (32bit unfolded)
  60. * @errp: set to -EFAULT for an bad destination address.
  61. *
  62. * Returns an 32bit unfolded checksum of the buffer.
  63. * src and dst are best aligned to 64bits.
  64. */
  65. unsigned int
  66. csum_partial_copy_to_user(unsigned const char *src, unsigned char __user *dst,
  67. int len, unsigned int isum, int *errp)
  68. {
  69. might_sleep();
  70. if (unlikely(!access_ok(VERIFY_WRITE, dst, len))) {
  71. *errp = -EFAULT;
  72. return 0;
  73. }
  74. if (unlikely((unsigned long)dst & 6)) {
  75. while (((unsigned long)dst & 6) && len >= 2) {
  76. __u16 val16 = *(__u16 *)src;
  77. isum = add32_with_carry(isum, val16);
  78. *errp = __put_user(val16, (__u16 __user *)dst);
  79. if (*errp)
  80. return isum;
  81. src += 2;
  82. dst += 2;
  83. len -= 2;
  84. }
  85. }
  86. *errp = 0;
  87. return csum_partial_copy_generic(src, (void __force *)dst,len,isum,NULL,errp);
  88. }
  89. EXPORT_SYMBOL(csum_partial_copy_to_user);
  90. /**
  91. * csum_partial_copy_nocheck - Copy and checksum.
  92. * @src: source address
  93. * @dst: destination address
  94. * @len: number of bytes to be copied.
  95. * @isum: initial sum that is added into the result (32bit unfolded)
  96. *
  97. * Returns an 32bit unfolded checksum of the buffer.
  98. */
  99. unsigned int
  100. csum_partial_copy_nocheck(const unsigned char *src, unsigned char *dst, int len, unsigned int sum)
  101. {
  102. return csum_partial_copy_generic(src,dst,len,sum,NULL,NULL);
  103. }
  104. EXPORT_SYMBOL(csum_partial_copy_nocheck);
  105. unsigned short csum_ipv6_magic(struct in6_addr *saddr, struct in6_addr *daddr,
  106. __u32 len, unsigned short proto, unsigned int sum)
  107. {
  108. __u64 rest, sum64;
  109. rest = (__u64)htonl(len) + (__u64)htons(proto) + (__u64)sum;
  110. asm(" addq (%[saddr]),%[sum]\n"
  111. " adcq 8(%[saddr]),%[sum]\n"
  112. " adcq (%[daddr]),%[sum]\n"
  113. " adcq 8(%[daddr]),%[sum]\n"
  114. " adcq $0,%[sum]\n"
  115. : [sum] "=r" (sum64)
  116. : "[sum]" (rest),[saddr] "r" (saddr), [daddr] "r" (daddr));
  117. return csum_fold(add32_with_carry(sum64 & 0xffffffff, sum64>>32));
  118. }
  119. EXPORT_SYMBOL(csum_ipv6_magic);