iovec.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. if (m->msg_name)
  48. m->msg_name = address;
  49. } else {
  50. m->msg_name = NULL;
  51. }
  52. size = m->msg_iovlen * sizeof(struct iovec);
  53. if (copy_from_user(iov, (void __user __force *) m->msg_iov, size))
  54. return -EFAULT;
  55. m->msg_iov = iov;
  56. err = 0;
  57. for (ct = 0; ct < m->msg_iovlen; ct++) {
  58. size_t len = iov[ct].iov_len;
  59. if (len > INT_MAX - err) {
  60. len = INT_MAX - err;
  61. iov[ct].iov_len = len;
  62. }
  63. err += len;
  64. }
  65. return err;
  66. }
  67. /*
  68. * Copy kernel to iovec. Returns -EFAULT on error.
  69. */
  70. int memcpy_toiovecend(const struct iovec *iov, unsigned char *kdata,
  71. int offset, int len)
  72. {
  73. int copy;
  74. for (; len > 0; ++iov) {
  75. /* Skip over the finished iovecs */
  76. if (unlikely(offset >= iov->iov_len)) {
  77. offset -= iov->iov_len;
  78. continue;
  79. }
  80. copy = min_t(unsigned int, iov->iov_len - offset, len);
  81. if (copy_to_user(iov->iov_base + offset, kdata, copy))
  82. return -EFAULT;
  83. offset = 0;
  84. kdata += copy;
  85. len -= copy;
  86. }
  87. return 0;
  88. }
  89. EXPORT_SYMBOL(memcpy_toiovecend);
  90. /*
  91. * Copy iovec to kernel. Returns -EFAULT on error.
  92. */
  93. int memcpy_fromiovecend(unsigned char *kdata, const struct iovec *iov,
  94. int offset, int len)
  95. {
  96. /* Skip over the finished iovecs */
  97. while (offset >= iov->iov_len) {
  98. offset -= iov->iov_len;
  99. iov++;
  100. }
  101. while (len > 0) {
  102. u8 __user *base = iov->iov_base + offset;
  103. int copy = min_t(unsigned int, len, iov->iov_len - offset);
  104. offset = 0;
  105. if (copy_from_user(kdata, base, copy))
  106. return -EFAULT;
  107. len -= copy;
  108. kdata += copy;
  109. iov++;
  110. }
  111. return 0;
  112. }
  113. EXPORT_SYMBOL(memcpy_fromiovecend);
  114. /*
  115. * And now for the all-in-one: copy and checksum from a user iovec
  116. * directly to a datagram
  117. * Calls to csum_partial but the last must be in 32 bit chunks
  118. *
  119. * ip_build_xmit must ensure that when fragmenting only the last
  120. * call to this function will be unaligned also.
  121. */
  122. int csum_partial_copy_fromiovecend(unsigned char *kdata, struct iovec *iov,
  123. int offset, unsigned int len, __wsum *csump)
  124. {
  125. __wsum csum = *csump;
  126. int partial_cnt = 0, err = 0;
  127. /* Skip over the finished iovecs */
  128. while (offset >= iov->iov_len) {
  129. offset -= iov->iov_len;
  130. iov++;
  131. }
  132. while (len > 0) {
  133. u8 __user *base = iov->iov_base + offset;
  134. int copy = min_t(unsigned int, len, iov->iov_len - offset);
  135. offset = 0;
  136. /* There is a remnant from previous iov. */
  137. if (partial_cnt) {
  138. int par_len = 4 - partial_cnt;
  139. /* iov component is too short ... */
  140. if (par_len > copy) {
  141. if (copy_from_user(kdata, base, copy))
  142. goto out_fault;
  143. kdata += copy;
  144. base += copy;
  145. partial_cnt += copy;
  146. len -= copy;
  147. iov++;
  148. if (len)
  149. continue;
  150. *csump = csum_partial(kdata - partial_cnt,
  151. partial_cnt, csum);
  152. goto out;
  153. }
  154. if (copy_from_user(kdata, base, par_len))
  155. goto out_fault;
  156. csum = csum_partial(kdata - partial_cnt, 4, csum);
  157. kdata += par_len;
  158. base += par_len;
  159. copy -= par_len;
  160. len -= par_len;
  161. partial_cnt = 0;
  162. }
  163. if (len > copy) {
  164. partial_cnt = copy % 4;
  165. if (partial_cnt) {
  166. copy -= partial_cnt;
  167. if (copy_from_user(kdata + copy, base + copy,
  168. partial_cnt))
  169. goto out_fault;
  170. }
  171. }
  172. if (copy) {
  173. csum = csum_and_copy_from_user(base, kdata, copy,
  174. csum, &err);
  175. if (err)
  176. goto out;
  177. }
  178. len -= copy + partial_cnt;
  179. kdata += copy + partial_cnt;
  180. iov++;
  181. }
  182. *csump = csum;
  183. out:
  184. return err;
  185. out_fault:
  186. err = -EFAULT;
  187. goto out;
  188. }
  189. EXPORT_SYMBOL(csum_partial_copy_fromiovecend);
  190. unsigned long iov_pages(const struct iovec *iov, int offset,
  191. unsigned long nr_segs)
  192. {
  193. unsigned long seg, base;
  194. int pages = 0, len, size;
  195. while (nr_segs && (offset >= iov->iov_len)) {
  196. offset -= iov->iov_len;
  197. ++iov;
  198. --nr_segs;
  199. }
  200. for (seg = 0; seg < nr_segs; seg++) {
  201. base = (unsigned long)iov[seg].iov_base + offset;
  202. len = iov[seg].iov_len - offset;
  203. size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT;
  204. pages += size;
  205. offset = 0;
  206. }
  207. return pages;
  208. }
  209. EXPORT_SYMBOL(iov_pages);