PageRenderTime 56ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/net/core/iovec.c

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