2 * iovec manipulation routines.
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.
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.
19 #include <linux/errno.h>
20 #include <linux/module.h>
21 #include <linux/kernel.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>
31 * Verify iovec. The caller must ensure that the iovec is big enough
32 * to hold the message iovec.
34 * Save time not doing access_ok. copy_*_user will make this work
38 long verify_iovec(struct msghdr *m, struct iovec *iov, struct sockaddr *address, int mode)
44 if (mode == VERIFY_READ) {
46 namep = (void __user __force *) m->msg_name;
47 err = move_addr_to_kernel(namep, m->msg_namelen,
52 m->msg_name = address;
57 size = m->msg_iovlen * sizeof(struct iovec);
58 if (copy_from_user(iov, (void __user __force *) m->msg_iov, size))
64 for (ct = 0; ct < m->msg_iovlen; ct++) {
65 err += iov[ct].iov_len;
67 * Goal is not to verify user data, but to prevent returning
68 * negative value, which is interpreted as errno.
69 * Overflow is still possible, but it is harmless.
79 * Copy kernel to iovec. Returns -EFAULT on error.
81 * Note: this modifies the original iovec.
84 int memcpy_toiovec(struct iovec *iov, unsigned char *kdata, int len)
88 int copy = min_t(unsigned int, iov->iov_len, len);
89 if (copy_to_user(iov->iov_base, kdata, copy))
94 iov->iov_base += copy;
101 EXPORT_SYMBOL(memcpy_toiovec);
104 * Copy kernel to iovec. Returns -EFAULT on error.
107 int memcpy_toiovecend(const struct iovec *iov, unsigned char *kdata,
111 for (; len > 0; ++iov) {
112 /* Skip over the finished iovecs */
113 if (unlikely(offset >= iov->iov_len)) {
114 offset -= iov->iov_len;
117 copy = min_t(unsigned int, iov->iov_len - offset, len);
118 if (copy_to_user(iov->iov_base + offset, kdata, copy))
127 EXPORT_SYMBOL(memcpy_toiovecend);
130 * Copy iovec to kernel. Returns -EFAULT on error.
132 * Note: this modifies the original iovec.
135 int memcpy_fromiovec(unsigned char *kdata, struct iovec *iov, int len)
139 int copy = min_t(unsigned int, len, iov->iov_len);
140 if (copy_from_user(kdata, iov->iov_base, copy))
144 iov->iov_base += copy;
145 iov->iov_len -= copy;
152 EXPORT_SYMBOL(memcpy_fromiovec);
155 * Copy iovec from kernel. Returns -EFAULT on error.
158 int memcpy_fromiovecend(unsigned char *kdata, const struct iovec *iov,
161 /* Skip over the finished iovecs */
162 while (offset >= iov->iov_len) {
163 offset -= iov->iov_len;
168 u8 __user *base = iov->iov_base + offset;
169 int copy = min_t(unsigned int, len, iov->iov_len - offset);
172 if (copy_from_user(kdata, base, copy))
181 EXPORT_SYMBOL(memcpy_fromiovecend);
184 * And now for the all-in-one: copy and checksum from a user iovec
185 * directly to a datagram
186 * Calls to csum_partial but the last must be in 32 bit chunks
188 * ip_build_xmit must ensure that when fragmenting only the last
189 * call to this function will be unaligned also.
191 int csum_partial_copy_fromiovecend(unsigned char *kdata, struct iovec *iov,
192 int offset, unsigned int len, __wsum *csump)
194 __wsum csum = *csump;
195 int partial_cnt = 0, err = 0;
197 /* Skip over the finished iovecs */
198 while (offset >= iov->iov_len) {
199 offset -= iov->iov_len;
204 u8 __user *base = iov->iov_base + offset;
205 int copy = min_t(unsigned int, len, iov->iov_len - offset);
209 /* There is a remnant from previous iov. */
211 int par_len = 4 - partial_cnt;
213 /* iov component is too short ... */
214 if (par_len > copy) {
215 if (copy_from_user(kdata, base, copy))
224 *csump = csum_partial(kdata - partial_cnt,
228 if (copy_from_user(kdata, base, par_len))
230 csum = csum_partial(kdata - partial_cnt, 4, csum);
239 partial_cnt = copy % 4;
242 if (copy_from_user(kdata + copy, base + copy,
249 csum = csum_and_copy_from_user(base, kdata, copy,
254 len -= copy + partial_cnt;
255 kdata += copy + partial_cnt;
266 EXPORT_SYMBOL(csum_partial_copy_fromiovecend);