iovec.c 5.7 KB

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