iovec.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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_name && 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. m->msg_namelen = 0;
  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. * Note: this modifies the original iovec.
  71. */
  72. int memcpy_toiovec(struct iovec *iov, unsigned char *kdata, int len)
  73. {
  74. while (len > 0) {
  75. if (iov->iov_len) {
  76. int copy = min_t(unsigned int, iov->iov_len, len);
  77. if (copy_to_user(iov->iov_base, kdata, copy))
  78. return -EFAULT;
  79. kdata += copy;
  80. len -= copy;
  81. iov->iov_len -= copy;
  82. iov->iov_base += copy;
  83. }
  84. iov++;
  85. }
  86. return 0;
  87. }
  88. EXPORT_SYMBOL(memcpy_toiovec);
  89. /*
  90. * Copy kernel to iovec. Returns -EFAULT on error.
  91. */
  92. int memcpy_toiovecend(const struct iovec *iov, unsigned char *kdata,
  93. int offset, int len)
  94. {
  95. int copy;
  96. for (; len > 0; ++iov) {
  97. /* Skip over the finished iovecs */
  98. if (unlikely(offset >= iov->iov_len)) {
  99. offset -= iov->iov_len;
  100. continue;
  101. }
  102. copy = min_t(unsigned int, iov->iov_len - offset, len);
  103. if (copy_to_user(iov->iov_base + offset, kdata, copy))
  104. return -EFAULT;
  105. offset = 0;
  106. kdata += copy;
  107. len -= copy;
  108. }
  109. return 0;
  110. }
  111. EXPORT_SYMBOL(memcpy_toiovecend);
  112. /*
  113. * Copy iovec to kernel. Returns -EFAULT on error.
  114. *
  115. * Note: this modifies the original iovec.
  116. */
  117. int memcpy_fromiovec(unsigned char *kdata, struct iovec *iov, int len)
  118. {
  119. while (len > 0) {
  120. if (iov->iov_len) {
  121. int copy = min_t(unsigned int, len, iov->iov_len);
  122. if (copy_from_user(kdata, iov->iov_base, copy))
  123. return -EFAULT;
  124. len -= copy;
  125. kdata += copy;
  126. iov->iov_base += copy;
  127. iov->iov_len -= copy;
  128. }
  129. iov++;
  130. }
  131. return 0;
  132. }
  133. EXPORT_SYMBOL(memcpy_fromiovec);
  134. /*
  135. * Copy iovec from kernel. Returns -EFAULT on error.
  136. */
  137. int memcpy_fromiovecend(unsigned char *kdata, const struct iovec *iov,
  138. int offset, int len)
  139. {
  140. /* No data? Done! */
  141. if (len == 0)
  142. return 0;
  143. /* Skip over the finished iovecs */
  144. while (offset >= iov->iov_len) {
  145. offset -= iov->iov_len;
  146. iov++;
  147. }
  148. while (len > 0) {
  149. u8 __user *base = iov->iov_base + offset;
  150. int copy = min_t(unsigned int, len, iov->iov_len - offset);
  151. offset = 0;
  152. if (copy_from_user(kdata, base, copy))
  153. return -EFAULT;
  154. len -= copy;
  155. kdata += copy;
  156. iov++;
  157. }
  158. return 0;
  159. }
  160. EXPORT_SYMBOL(memcpy_fromiovecend);
  161. /*
  162. * And now for the all-in-one: copy and checksum from a user iovec
  163. * directly to a datagram
  164. * Calls to csum_partial but the last must be in 32 bit chunks
  165. *
  166. * ip_build_xmit must ensure that when fragmenting only the last
  167. * call to this function will be unaligned also.
  168. */
  169. int csum_partial_copy_fromiovecend(unsigned char *kdata, struct iovec *iov,
  170. int offset, unsigned int len, __wsum *csump)
  171. {
  172. __wsum csum = *csump;
  173. int partial_cnt = 0, err = 0;
  174. /* No data? Done! */
  175. if (len == 0)
  176. return 0;
  177. /* Skip over the finished iovecs */
  178. while (offset >= iov->iov_len) {
  179. offset -= iov->iov_len;
  180. iov++;
  181. }
  182. while (len > 0) {
  183. u8 __user *base = iov->iov_base + offset;
  184. int copy = min_t(unsigned int, len, iov->iov_len - offset);
  185. offset = 0;
  186. /* There is a remnant from previous iov. */
  187. if (partial_cnt) {
  188. int par_len = 4 - partial_cnt;
  189. /* iov component is too short ... */
  190. if (par_len > copy) {
  191. if (copy_from_user(kdata, base, copy))
  192. goto out_fault;
  193. kdata += copy;
  194. base += copy;
  195. partial_cnt += copy;
  196. len -= copy;
  197. iov++;
  198. if (len)
  199. continue;
  200. *csump = csum_partial(kdata - partial_cnt,
  201. partial_cnt, csum);
  202. goto out;
  203. }
  204. if (copy_from_user(kdata, base, par_len))
  205. goto out_fault;
  206. csum = csum_partial(kdata - partial_cnt, 4, csum);
  207. kdata += par_len;
  208. base += par_len;
  209. copy -= par_len;
  210. len -= par_len;
  211. partial_cnt = 0;
  212. }
  213. if (len > copy) {
  214. partial_cnt = copy % 4;
  215. if (partial_cnt) {
  216. copy -= partial_cnt;
  217. if (copy_from_user(kdata + copy, base + copy,
  218. partial_cnt))
  219. goto out_fault;
  220. }
  221. }
  222. if (copy) {
  223. csum = csum_and_copy_from_user(base, kdata, copy,
  224. csum, &err);
  225. if (err)
  226. goto out;
  227. }
  228. len -= copy + partial_cnt;
  229. kdata += copy + partial_cnt;
  230. iov++;
  231. }
  232. *csump = csum;
  233. out:
  234. return err;
  235. out_fault:
  236. err = -EFAULT;
  237. goto out;
  238. }
  239. EXPORT_SYMBOL(csum_partial_copy_fromiovecend);