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. 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. err = move_addr_to_kernel(m->msg_name, m->msg_namelen,
  42. address);
  43. if (err < 0)
  44. return err;
  45. }
  46. m->msg_name = address;
  47. } else {
  48. m->msg_name = NULL;
  49. }
  50. size = m->msg_iovlen * sizeof(struct iovec);
  51. if (copy_from_user(iov, m->msg_iov, size))
  52. return -EFAULT;
  53. m->msg_iov = iov;
  54. err = 0;
  55. for (ct = 0; ct < m->msg_iovlen; ct++) {
  56. err += iov[ct].iov_len;
  57. /*
  58. * Goal is not to verify user data, but to prevent returning
  59. * negative value, which is interpreted as errno.
  60. * Overflow is still possible, but it is harmless.
  61. */
  62. if (err < 0)
  63. return -EMSGSIZE;
  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. /* Skip over the finished iovecs */
  141. while (offset >= iov->iov_len) {
  142. offset -= iov->iov_len;
  143. iov++;
  144. }
  145. while (len > 0) {
  146. u8 __user *base = iov->iov_base + offset;
  147. int copy = min_t(unsigned int, len, iov->iov_len - offset);
  148. offset = 0;
  149. if (copy_from_user(kdata, base, copy))
  150. return -EFAULT;
  151. len -= copy;
  152. kdata += copy;
  153. iov++;
  154. }
  155. return 0;
  156. }
  157. EXPORT_SYMBOL(memcpy_fromiovecend);
  158. /*
  159. * And now for the all-in-one: copy and checksum from a user iovec
  160. * directly to a datagram
  161. * Calls to csum_partial but the last must be in 32 bit chunks
  162. *
  163. * ip_build_xmit must ensure that when fragmenting only the last
  164. * call to this function will be unaligned also.
  165. */
  166. int csum_partial_copy_fromiovecend(unsigned char *kdata, struct iovec *iov,
  167. int offset, unsigned int len, __wsum *csump)
  168. {
  169. __wsum csum = *csump;
  170. int partial_cnt = 0, err = 0;
  171. /* Skip over the finished iovecs */
  172. while (offset >= iov->iov_len) {
  173. offset -= iov->iov_len;
  174. iov++;
  175. }
  176. while (len > 0) {
  177. u8 __user *base = iov->iov_base + offset;
  178. int copy = min_t(unsigned int, len, iov->iov_len - offset);
  179. offset = 0;
  180. /* There is a remnant from previous iov. */
  181. if (partial_cnt) {
  182. int par_len = 4 - partial_cnt;
  183. /* iov component is too short ... */
  184. if (par_len > copy) {
  185. if (copy_from_user(kdata, base, copy))
  186. goto out_fault;
  187. kdata += copy;
  188. base += copy;
  189. partial_cnt += copy;
  190. len -= copy;
  191. iov++;
  192. if (len)
  193. continue;
  194. *csump = csum_partial(kdata - partial_cnt,
  195. partial_cnt, csum);
  196. goto out;
  197. }
  198. if (copy_from_user(kdata, base, par_len))
  199. goto out_fault;
  200. csum = csum_partial(kdata - partial_cnt, 4, csum);
  201. kdata += par_len;
  202. base += par_len;
  203. copy -= par_len;
  204. len -= par_len;
  205. partial_cnt = 0;
  206. }
  207. if (len > copy) {
  208. partial_cnt = copy % 4;
  209. if (partial_cnt) {
  210. copy -= partial_cnt;
  211. if (copy_from_user(kdata + copy, base + copy,
  212. partial_cnt))
  213. goto out_fault;
  214. }
  215. }
  216. if (copy) {
  217. csum = csum_and_copy_from_user(base, kdata, copy,
  218. csum, &err);
  219. if (err)
  220. goto out;
  221. }
  222. len -= copy + partial_cnt;
  223. kdata += copy + partial_cnt;
  224. iov++;
  225. }
  226. *csump = csum;
  227. out:
  228. return err;
  229. out_fault:
  230. err = -EFAULT;
  231. goto out;
  232. }
  233. EXPORT_SYMBOL(csum_partial_copy_fromiovecend);