iovec.c 5.6 KB

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