checksum_wrappers.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 <linux/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. allow_read_from_user(src, len);
  31. *err_ptr = 0;
  32. if (!len) {
  33. csum = 0;
  34. goto out;
  35. }
  36. if (unlikely((len < 0) || !access_ok(VERIFY_READ, src, len))) {
  37. *err_ptr = -EFAULT;
  38. csum = (__force unsigned int)sum;
  39. goto out;
  40. }
  41. csum = csum_partial_copy_generic((void __force *)src, dst,
  42. len, sum, err_ptr, NULL);
  43. if (unlikely(*err_ptr)) {
  44. int missing = __copy_from_user(dst, src, len);
  45. if (missing) {
  46. memset(dst + len - missing, 0, missing);
  47. *err_ptr = -EFAULT;
  48. } else {
  49. *err_ptr = 0;
  50. }
  51. csum = csum_partial(dst, len, sum);
  52. }
  53. out:
  54. prevent_read_from_user(src, len);
  55. return (__force __wsum)csum;
  56. }
  57. EXPORT_SYMBOL(csum_and_copy_from_user);
  58. __wsum csum_and_copy_to_user(const void *src, void __user *dst, int len,
  59. __wsum sum, int *err_ptr)
  60. {
  61. unsigned int csum;
  62. might_sleep();
  63. allow_write_to_user(dst, len);
  64. *err_ptr = 0;
  65. if (!len) {
  66. csum = 0;
  67. goto out;
  68. }
  69. if (unlikely((len < 0) || !access_ok(VERIFY_WRITE, dst, len))) {
  70. *err_ptr = -EFAULT;
  71. csum = -1; /* invalid checksum */
  72. goto out;
  73. }
  74. csum = csum_partial_copy_generic(src, (void __force *)dst,
  75. len, sum, NULL, err_ptr);
  76. if (unlikely(*err_ptr)) {
  77. csum = csum_partial(src, len, sum);
  78. if (copy_to_user(dst, src, len)) {
  79. *err_ptr = -EFAULT;
  80. csum = -1; /* invalid checksum */
  81. }
  82. }
  83. out:
  84. prevent_write_to_user(dst, len);
  85. return (__force __wsum)csum;
  86. }
  87. EXPORT_SYMBOL(csum_and_copy_to_user);