iovec.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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_storage *address, int mode)
  36. {
  37. int size, ct, err;
  38. if (m->msg_name && 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. m->msg_namelen = 0;
  51. }
  52. size = m->msg_iovlen * sizeof(struct iovec);
  53. if (copy_from_user(iov, (void __user __force *) m->msg_iov, size))
  54. return -EFAULT;
  55. m->msg_iov = iov;
  56. err = 0;
  57. for (ct = 0; ct < m->msg_iovlen; ct++) {
  58. size_t len = iov[ct].iov_len;
  59. if (len > INT_MAX - err) {
  60. len = INT_MAX - err;
  61. iov[ct].iov_len = len;
  62. }
  63. err += len;
  64. }
  65. return err;
  66. }
  67. /*
  68. * And now for the all-in-one: copy and checksum from a user iovec
  69. * directly to a datagram
  70. * Calls to csum_partial but the last must be in 32 bit chunks
  71. *
  72. * ip_build_xmit must ensure that when fragmenting only the last
  73. * call to this function will be unaligned also.
  74. */
  75. int csum_partial_copy_fromiovecend(unsigned char *kdata, struct iovec *iov,
  76. int offset, unsigned int len, __wsum *csump)
  77. {
  78. __wsum csum = *csump;
  79. int partial_cnt = 0, err = 0;
  80. /* Skip over the finished iovecs */
  81. while (offset >= iov->iov_len) {
  82. offset -= iov->iov_len;
  83. iov++;
  84. }
  85. while (len > 0) {
  86. u8 __user *base = iov->iov_base + offset;
  87. int copy = min_t(unsigned int, len, iov->iov_len - offset);
  88. offset = 0;
  89. /* There is a remnant from previous iov. */
  90. if (partial_cnt) {
  91. int par_len = 4 - partial_cnt;
  92. /* iov component is too short ... */
  93. if (par_len > copy) {
  94. if (copy_from_user(kdata, base, copy))
  95. goto out_fault;
  96. kdata += copy;
  97. base += copy;
  98. partial_cnt += copy;
  99. len -= copy;
  100. iov++;
  101. if (len)
  102. continue;
  103. *csump = csum_partial(kdata - partial_cnt,
  104. partial_cnt, csum);
  105. goto out;
  106. }
  107. if (copy_from_user(kdata, base, par_len))
  108. goto out_fault;
  109. csum = csum_partial(kdata - partial_cnt, 4, csum);
  110. kdata += par_len;
  111. base += par_len;
  112. copy -= par_len;
  113. len -= par_len;
  114. partial_cnt = 0;
  115. }
  116. if (len > copy) {
  117. partial_cnt = copy % 4;
  118. if (partial_cnt) {
  119. copy -= partial_cnt;
  120. if (copy_from_user(kdata + copy, base + copy,
  121. partial_cnt))
  122. goto out_fault;
  123. }
  124. }
  125. if (copy) {
  126. csum = csum_and_copy_from_user(base, kdata, copy,
  127. csum, &err);
  128. if (err)
  129. goto out;
  130. }
  131. len -= copy + partial_cnt;
  132. kdata += copy + partial_cnt;
  133. iov++;
  134. }
  135. *csump = csum;
  136. out:
  137. return err;
  138. out_fault:
  139. err = -EFAULT;
  140. goto out;
  141. }
  142. EXPORT_SYMBOL(csum_partial_copy_fromiovecend);
  143. unsigned long iov_pages(const struct iovec *iov, int offset,
  144. unsigned long nr_segs)
  145. {
  146. unsigned long seg, base;
  147. int pages = 0, len, size;
  148. while (nr_segs && (offset >= iov->iov_len)) {
  149. offset -= iov->iov_len;
  150. ++iov;
  151. --nr_segs;
  152. }
  153. for (seg = 0; seg < nr_segs; seg++) {
  154. base = (unsigned long)iov[seg].iov_base + offset;
  155. len = iov[seg].iov_len - offset;
  156. size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT;
  157. pages += size;
  158. offset = 0;
  159. }
  160. return pages;
  161. }
  162. EXPORT_SYMBOL(iov_pages);