iovec.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * iovec manipulation routines.
  3. *
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version
  8. * 2 of the License, or (at your option) any later version.
  9. *
  10. * Fixes:
  11. * Andrew Lunn : Errors in iovec copying.
  12. * Pedro Roque : Added memcpy_fromiovecend and
  13. * csum_..._fromiovecend.
  14. * Andi Kleen : fixed error handling for 2.1
  15. * Alexey Kuznetsov: 2.1 optimisations
  16. * Andi Kleen : Fix csum*fromiovecend for IPv6.
  17. */
  18. #include <linux/errno.h>
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/mm.h>
  22. #include <linux/net.h>
  23. #include <linux/in6.h>
  24. #include <asm/uaccess.h>
  25. #include <asm/byteorder.h>
  26. #include <net/checksum.h>
  27. #include <net/sock.h>
  28. /*
  29. * And now for the all-in-one: copy and checksum from a user iovec
  30. * directly to a datagram
  31. * Calls to csum_partial but the last must be in 32 bit chunks
  32. *
  33. * ip_build_xmit must ensure that when fragmenting only the last
  34. * call to this function will be unaligned also.
  35. */
  36. int csum_partial_copy_fromiovecend(unsigned char *kdata, struct iovec *iov,
  37. int offset, unsigned int len, __wsum *csump)
  38. {
  39. __wsum csum = *csump;
  40. int partial_cnt = 0, err = 0;
  41. /* Skip over the finished iovecs */
  42. while (offset >= iov->iov_len) {
  43. offset -= iov->iov_len;
  44. iov++;
  45. }
  46. while (len > 0) {
  47. u8 __user *base = iov->iov_base + offset;
  48. int copy = min_t(unsigned int, len, iov->iov_len - offset);
  49. offset = 0;
  50. /* There is a remnant from previous iov. */
  51. if (partial_cnt) {
  52. int par_len = 4 - partial_cnt;
  53. /* iov component is too short ... */
  54. if (par_len > copy) {
  55. if (copy_from_user(kdata, base, copy))
  56. goto out_fault;
  57. kdata += copy;
  58. base += copy;
  59. partial_cnt += copy;
  60. len -= copy;
  61. iov++;
  62. if (len)
  63. continue;
  64. *csump = csum_partial(kdata - partial_cnt,
  65. partial_cnt, csum);
  66. goto out;
  67. }
  68. if (copy_from_user(kdata, base, par_len))
  69. goto out_fault;
  70. csum = csum_partial(kdata - partial_cnt, 4, csum);
  71. kdata += par_len;
  72. base += par_len;
  73. copy -= par_len;
  74. len -= par_len;
  75. partial_cnt = 0;
  76. }
  77. if (len > copy) {
  78. partial_cnt = copy % 4;
  79. if (partial_cnt) {
  80. copy -= partial_cnt;
  81. if (copy_from_user(kdata + copy, base + copy,
  82. partial_cnt))
  83. goto out_fault;
  84. }
  85. }
  86. if (copy) {
  87. csum = csum_and_copy_from_user(base, kdata, copy,
  88. csum, &err);
  89. if (err)
  90. goto out;
  91. }
  92. len -= copy + partial_cnt;
  93. kdata += copy + partial_cnt;
  94. iov++;
  95. }
  96. *csump = csum;
  97. out:
  98. return err;
  99. out_fault:
  100. err = -EFAULT;
  101. goto out;
  102. }
  103. EXPORT_SYMBOL(csum_partial_copy_fromiovecend);
  104. unsigned long iov_pages(const struct iovec *iov, int offset,
  105. unsigned long nr_segs)
  106. {
  107. unsigned long seg, base;
  108. int pages = 0, len, size;
  109. while (nr_segs && (offset >= iov->iov_len)) {
  110. offset -= iov->iov_len;
  111. ++iov;
  112. --nr_segs;
  113. }
  114. for (seg = 0; seg < nr_segs; seg++) {
  115. base = (unsigned long)iov[seg].iov_base + offset;
  116. len = iov[seg].iov_len - offset;
  117. size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT;
  118. pages += size;
  119. offset = 0;
  120. }
  121. return pages;
  122. }
  123. EXPORT_SYMBOL(iov_pages);