iovec.c 5.1 KB

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