iovec.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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, char *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 iovec to kernel. Returns -EFAULT on error.
  90. *
  91. * Note: this modifies the original iovec.
  92. */
  93. int memcpy_fromiovec(unsigned char *kdata, struct iovec *iov, int len)
  94. {
  95. while (len > 0) {
  96. if (iov->iov_len) {
  97. int copy = min_t(unsigned int, len, iov->iov_len);
  98. if (copy_from_user(kdata, iov->iov_base, copy))
  99. return -EFAULT;
  100. len -= copy;
  101. kdata += copy;
  102. iov->iov_base += copy;
  103. iov->iov_len -= copy;
  104. }
  105. iov++;
  106. }
  107. return 0;
  108. }
  109. /*
  110. * For use with ip_build_xmit
  111. */
  112. int memcpy_fromiovecend(unsigned char *kdata, struct iovec *iov, int offset,
  113. int len)
  114. {
  115. /* Skip over the finished iovecs */
  116. while (offset >= iov->iov_len) {
  117. offset -= iov->iov_len;
  118. iov++;
  119. }
  120. while (len > 0) {
  121. u8 __user *base = iov->iov_base + offset;
  122. int copy = min_t(unsigned int, len, iov->iov_len - offset);
  123. offset = 0;
  124. if (copy_from_user(kdata, base, copy))
  125. return -EFAULT;
  126. len -= copy;
  127. kdata += copy;
  128. iov++;
  129. }
  130. return 0;
  131. }
  132. /*
  133. * And now for the all-in-one: copy and checksum from a user iovec
  134. * directly to a datagram
  135. * Calls to csum_partial but the last must be in 32 bit chunks
  136. *
  137. * ip_build_xmit must ensure that when fragmenting only the last
  138. * call to this function will be unaligned also.
  139. */
  140. int csum_partial_copy_fromiovecend(unsigned char *kdata, struct iovec *iov,
  141. int offset, unsigned int len, __wsum *csump)
  142. {
  143. __wsum csum = *csump;
  144. int partial_cnt = 0, err = 0;
  145. /* Skip over the finished iovecs */
  146. while (offset >= iov->iov_len) {
  147. offset -= iov->iov_len;
  148. iov++;
  149. }
  150. while (len > 0) {
  151. u8 __user *base = iov->iov_base + offset;
  152. int copy = min_t(unsigned int, len, iov->iov_len - offset);
  153. offset = 0;
  154. /* There is a remnant from previous iov. */
  155. if (partial_cnt) {
  156. int par_len = 4 - partial_cnt;
  157. /* iov component is too short ... */
  158. if (par_len > copy) {
  159. if (copy_from_user(kdata, base, copy))
  160. goto out_fault;
  161. kdata += copy;
  162. base += copy;
  163. partial_cnt += copy;
  164. len -= copy;
  165. iov++;
  166. if (len)
  167. continue;
  168. *csump = csum_partial(kdata - partial_cnt,
  169. partial_cnt, csum);
  170. goto out;
  171. }
  172. if (copy_from_user(kdata, base, par_len))
  173. goto out_fault;
  174. csum = csum_partial(kdata - partial_cnt, 4, csum);
  175. kdata += par_len;
  176. base += par_len;
  177. copy -= par_len;
  178. len -= par_len;
  179. partial_cnt = 0;
  180. }
  181. if (len > copy) {
  182. partial_cnt = copy % 4;
  183. if (partial_cnt) {
  184. copy -= partial_cnt;
  185. if (copy_from_user(kdata + copy, base + copy,
  186. partial_cnt))
  187. goto out_fault;
  188. }
  189. }
  190. if (copy) {
  191. csum = csum_and_copy_from_user(base, kdata, copy,
  192. csum, &err);
  193. if (err)
  194. goto out;
  195. }
  196. len -= copy + partial_cnt;
  197. kdata += copy + partial_cnt;
  198. iov++;
  199. }
  200. *csump = csum;
  201. out:
  202. return err;
  203. out_fault:
  204. err = -EFAULT;
  205. goto out;
  206. }
  207. EXPORT_SYMBOL(csum_partial_copy_fromiovecend);
  208. EXPORT_SYMBOL(memcpy_fromiovec);
  209. EXPORT_SYMBOL(memcpy_fromiovecend);
  210. EXPORT_SYMBOL(memcpy_toiovec);