iovec.c 5.7 KB

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