iovec.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. * Verify iovec. The caller must ensure that the iovec is big enough
  30. * to hold the message iovec.
  31. *
  32. * Save time not doing access_ok. copy_*_user will make this work
  33. * in any case.
  34. */
  35. int verify_iovec(struct msghdr *m, struct iovec *iov, struct sockaddr_storage *address, int mode)
  36. {
  37. int size, ct, err;
  38. if (m->msg_namelen) {
  39. if (mode == VERIFY_READ) {
  40. void __user *namep;
  41. namep = (void __user __force *) m->msg_name;
  42. err = move_addr_to_kernel(namep, m->msg_namelen,
  43. address);
  44. if (err < 0)
  45. return err;
  46. }
  47. m->msg_name = address;
  48. } else {
  49. m->msg_name = NULL;
  50. }
  51. size = m->msg_iovlen * sizeof(struct iovec);
  52. if (copy_from_user(iov, (void __user __force *) m->msg_iov, size))
  53. return -EFAULT;
  54. m->msg_iov = iov;
  55. err = 0;
  56. for (ct = 0; ct < m->msg_iovlen; ct++) {
  57. size_t len = iov[ct].iov_len;
  58. if (len > INT_MAX - err) {
  59. len = INT_MAX - err;
  60. iov[ct].iov_len = len;
  61. }
  62. err += len;
  63. }
  64. return err;
  65. }
  66. /*
  67. * Copy kernel to iovec. Returns -EFAULT on error.
  68. */
  69. int memcpy_toiovecend(const struct iovec *iov, unsigned char *kdata,
  70. int offset, int len)
  71. {
  72. int copy;
  73. for (; len > 0; ++iov) {
  74. /* Skip over the finished iovecs */
  75. if (unlikely(offset >= iov->iov_len)) {
  76. offset -= iov->iov_len;
  77. continue;
  78. }
  79. copy = min_t(unsigned int, iov->iov_len - offset, len);
  80. if (copy_to_user(iov->iov_base + offset, kdata, copy))
  81. return -EFAULT;
  82. offset = 0;
  83. kdata += copy;
  84. len -= copy;
  85. }
  86. return 0;
  87. }
  88. EXPORT_SYMBOL(memcpy_toiovecend);
  89. /*
  90. * Copy iovec from kernel. Returns -EFAULT on error.
  91. */
  92. int memcpy_fromiovecend(unsigned char *kdata, const struct iovec *iov,
  93. int offset, int len)
  94. {
  95. /* Skip over the finished iovecs */
  96. while (offset >= iov->iov_len) {
  97. offset -= iov->iov_len;
  98. iov++;
  99. }
  100. while (len > 0) {
  101. u8 __user *base = iov->iov_base + offset;
  102. int copy = min_t(unsigned int, len, iov->iov_len - offset);
  103. offset = 0;
  104. if (copy_from_user(kdata, base, copy))
  105. return -EFAULT;
  106. len -= copy;
  107. kdata += copy;
  108. iov++;
  109. }
  110. return 0;
  111. }
  112. EXPORT_SYMBOL(memcpy_fromiovecend);
  113. /*
  114. * And now for the all-in-one: copy and checksum from a user iovec
  115. * directly to a datagram
  116. * Calls to csum_partial but the last must be in 32 bit chunks
  117. *
  118. * ip_build_xmit must ensure that when fragmenting only the last
  119. * call to this function will be unaligned also.
  120. */
  121. int csum_partial_copy_fromiovecend(unsigned char *kdata, struct iovec *iov,
  122. int offset, unsigned int len, __wsum *csump)
  123. {
  124. __wsum csum = *csump;
  125. int partial_cnt = 0, err = 0;
  126. /* Skip over the finished iovecs */
  127. while (offset >= iov->iov_len) {
  128. offset -= iov->iov_len;
  129. iov++;
  130. }
  131. while (len > 0) {
  132. u8 __user *base = iov->iov_base + offset;
  133. int copy = min_t(unsigned int, len, iov->iov_len - offset);
  134. offset = 0;
  135. /* There is a remnant from previous iov. */
  136. if (partial_cnt) {
  137. int par_len = 4 - partial_cnt;
  138. /* iov component is too short ... */
  139. if (par_len > copy) {
  140. if (copy_from_user(kdata, base, copy))
  141. goto out_fault;
  142. kdata += copy;
  143. base += copy;
  144. partial_cnt += copy;
  145. len -= copy;
  146. iov++;
  147. if (len)
  148. continue;
  149. *csump = csum_partial(kdata - partial_cnt,
  150. partial_cnt, csum);
  151. goto out;
  152. }
  153. if (copy_from_user(kdata, base, par_len))
  154. goto out_fault;
  155. csum = csum_partial(kdata - partial_cnt, 4, csum);
  156. kdata += par_len;
  157. base += par_len;
  158. copy -= par_len;
  159. len -= par_len;
  160. partial_cnt = 0;
  161. }
  162. if (len > copy) {
  163. partial_cnt = copy % 4;
  164. if (partial_cnt) {
  165. copy -= partial_cnt;
  166. if (copy_from_user(kdata + copy, base + copy,
  167. partial_cnt))
  168. goto out_fault;
  169. }
  170. }
  171. if (copy) {
  172. csum = csum_and_copy_from_user(base, kdata, copy,
  173. csum, &err);
  174. if (err)
  175. goto out;
  176. }
  177. len -= copy + partial_cnt;
  178. kdata += copy + partial_cnt;
  179. iov++;
  180. }
  181. *csump = csum;
  182. out:
  183. return err;
  184. out_fault:
  185. err = -EFAULT;
  186. goto out;
  187. }
  188. EXPORT_SYMBOL(csum_partial_copy_fromiovecend);