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/slab.h>
  23. #include <linux/net.h>
  24. #include <linux/in6.h>
  25. #include <asm/uaccess.h>
  26. #include <asm/byteorder.h>
  27. #include <net/checksum.h>
  28. #include <net/sock.h>
  29. /*
  30. * Verify iovec. The caller must ensure that the iovec is big enough
  31. * to hold the message iovec.
  32. *
  33. * Save time not doing access_ok. copy_*_user will make this work
  34. * in any case.
  35. */
  36. int verify_iovec(struct msghdr *m, struct iovec *iov, struct sockaddr *address, int mode)
  37. {
  38. int size, err, ct;
  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. /*
  89. * Copy kernel to iovec. Returns -EFAULT on error.
  90. */
  91. int memcpy_toiovecend(const struct iovec *iov, unsigned char *kdata,
  92. int offset, int len)
  93. {
  94. int copy;
  95. for (; len > 0; ++iov) {
  96. /* Skip over the finished iovecs */
  97. if (unlikely(offset >= iov->iov_len)) {
  98. offset -= iov->iov_len;
  99. continue;
  100. }
  101. copy = min_t(unsigned int, iov->iov_len - offset, len);
  102. if (copy_to_user(iov->iov_base + offset, kdata, copy))
  103. return -EFAULT;
  104. offset = 0;
  105. kdata += copy;
  106. len -= copy;
  107. }
  108. return 0;
  109. }
  110. /*
  111. * Copy iovec to kernel. Returns -EFAULT on error.
  112. *
  113. * Note: this modifies the original iovec.
  114. */
  115. int memcpy_fromiovec(unsigned char *kdata, struct iovec *iov, int len)
  116. {
  117. while (len > 0) {
  118. if (iov->iov_len) {
  119. int copy = min_t(unsigned int, len, iov->iov_len);
  120. if (copy_from_user(kdata, iov->iov_base, copy))
  121. return -EFAULT;
  122. len -= copy;
  123. kdata += copy;
  124. iov->iov_base += copy;
  125. iov->iov_len -= copy;
  126. }
  127. iov++;
  128. }
  129. return 0;
  130. }
  131. /*
  132. * Copy iovec from kernel. Returns -EFAULT on error.
  133. */
  134. int memcpy_fromiovecend(unsigned char *kdata, const struct iovec *iov,
  135. int offset, int len)
  136. {
  137. /* Skip over the finished iovecs */
  138. while (offset >= iov->iov_len) {
  139. offset -= iov->iov_len;
  140. iov++;
  141. }
  142. while (len > 0) {
  143. u8 __user *base = iov->iov_base + offset;
  144. int copy = min_t(unsigned int, len, iov->iov_len - offset);
  145. offset = 0;
  146. if (copy_from_user(kdata, base, copy))
  147. return -EFAULT;
  148. len -= copy;
  149. kdata += copy;
  150. iov++;
  151. }
  152. return 0;
  153. }
  154. /*
  155. * And now for the all-in-one: copy and checksum from a user iovec
  156. * directly to a datagram
  157. * Calls to csum_partial but the last must be in 32 bit chunks
  158. *
  159. * ip_build_xmit must ensure that when fragmenting only the last
  160. * call to this function will be unaligned also.
  161. */
  162. int csum_partial_copy_fromiovecend(unsigned char *kdata, struct iovec *iov,
  163. int offset, unsigned int len, __wsum *csump)
  164. {
  165. __wsum csum = *csump;
  166. int partial_cnt = 0, err = 0;
  167. /* Skip over the finished iovecs */
  168. while (offset >= iov->iov_len) {
  169. offset -= iov->iov_len;
  170. iov++;
  171. }
  172. while (len > 0) {
  173. u8 __user *base = iov->iov_base + offset;
  174. int copy = min_t(unsigned int, len, iov->iov_len - offset);
  175. offset = 0;
  176. /* There is a remnant from previous iov. */
  177. if (partial_cnt) {
  178. int par_len = 4 - partial_cnt;
  179. /* iov component is too short ... */
  180. if (par_len > copy) {
  181. if (copy_from_user(kdata, base, copy))
  182. goto out_fault;
  183. kdata += copy;
  184. base += copy;
  185. partial_cnt += copy;
  186. len -= copy;
  187. iov++;
  188. if (len)
  189. continue;
  190. *csump = csum_partial(kdata - partial_cnt,
  191. partial_cnt, csum);
  192. goto out;
  193. }
  194. if (copy_from_user(kdata, base, par_len))
  195. goto out_fault;
  196. csum = csum_partial(kdata - partial_cnt, 4, csum);
  197. kdata += par_len;
  198. base += par_len;
  199. copy -= par_len;
  200. len -= par_len;
  201. partial_cnt = 0;
  202. }
  203. if (len > copy) {
  204. partial_cnt = copy % 4;
  205. if (partial_cnt) {
  206. copy -= partial_cnt;
  207. if (copy_from_user(kdata + copy, base + copy,
  208. partial_cnt))
  209. goto out_fault;
  210. }
  211. }
  212. if (copy) {
  213. csum = csum_and_copy_from_user(base, kdata, copy,
  214. csum, &err);
  215. if (err)
  216. goto out;
  217. }
  218. len -= copy + partial_cnt;
  219. kdata += copy + partial_cnt;
  220. iov++;
  221. }
  222. *csump = csum;
  223. out:
  224. return err;
  225. out_fault:
  226. err = -EFAULT;
  227. goto out;
  228. }
  229. EXPORT_SYMBOL(csum_partial_copy_fromiovecend);
  230. EXPORT_SYMBOL(memcpy_fromiovec);
  231. EXPORT_SYMBOL(memcpy_fromiovecend);
  232. EXPORT_SYMBOL(memcpy_toiovec);
  233. EXPORT_SYMBOL(memcpy_toiovecend);