checksum_wrappers.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. *
  16. * Copyright (C) IBM Corporation, 2010
  17. *
  18. * Author: Anton Blanchard <anton@au.ibm.com>
  19. */
  20. #include <linux/export.h>
  21. #include <linux/compiler.h>
  22. #include <linux/types.h>
  23. #include <asm/checksum.h>
  24. #include <asm/uaccess.h>
  25. __wsum csum_and_copy_from_user(const void __user *src, void *dst,
  26. int len, __wsum sum, int *err_ptr)
  27. {
  28. unsigned int csum;
  29. might_sleep();
  30. *err_ptr = 0;
  31. if (!len) {
  32. csum = 0;
  33. goto out;
  34. }
  35. if (unlikely((len < 0) || !access_ok(VERIFY_READ, src, len))) {
  36. *err_ptr = -EFAULT;
  37. csum = (__force unsigned int)sum;
  38. goto out;
  39. }
  40. csum = csum_partial_copy_generic((void __force *)src, dst,
  41. len, sum, err_ptr, NULL);
  42. if (unlikely(*err_ptr)) {
  43. int missing = __copy_from_user(dst, src, len);
  44. if (missing) {
  45. memset(dst + len - missing, 0, missing);
  46. *err_ptr = -EFAULT;
  47. } else {
  48. *err_ptr = 0;
  49. }
  50. csum = csum_partial(dst, len, sum);
  51. }
  52. out:
  53. return (__force __wsum)csum;
  54. }
  55. EXPORT_SYMBOL(csum_and_copy_from_user);
  56. __wsum csum_and_copy_to_user(const void *src, void __user *dst, int len,
  57. __wsum sum, int *err_ptr)
  58. {
  59. unsigned int csum;
  60. might_sleep();
  61. *err_ptr = 0;
  62. if (!len) {
  63. csum = 0;
  64. goto out;
  65. }
  66. if (unlikely((len < 0) || !access_ok(VERIFY_WRITE, dst, len))) {
  67. *err_ptr = -EFAULT;
  68. csum = -1; /* invalid checksum */
  69. goto out;
  70. }
  71. csum = csum_partial_copy_generic(src, (void __force *)dst,
  72. len, sum, NULL, err_ptr);
  73. if (unlikely(*err_ptr)) {
  74. csum = csum_partial(src, len, sum);
  75. if (copy_to_user(dst, src, len)) {
  76. *err_ptr = -EFAULT;
  77. csum = -1; /* invalid checksum */
  78. }
  79. }
  80. out:
  81. return (__force __wsum)csum;
  82. }
  83. EXPORT_SYMBOL(csum_and_copy_to_user);