blob: 72aceb1fe4fae6c072b12e5acc4c97bc4ed73cf7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
19#include <linux/errno.h>
20#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/kernel.h>
22#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#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/*
31 * Verify iovec. The caller must ensure that the iovec is big enough
32 * to hold the message iovec.
33 *
Jesper Juhle49332b2005-05-01 08:59:08 -070034 * Save time not doing access_ok. copy_*_user will make this work
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 * in any case.
36 */
37
David S. Miller01db4032010-09-27 20:24:54 -070038long verify_iovec(struct msghdr *m, struct iovec *iov, struct sockaddr *address, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
David S. Miller01db4032010-09-27 20:24:54 -070040 int size, ct;
41 long err;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 if (m->msg_namelen) {
44 if (mode == VERIFY_READ) {
Namhyung Kima700d8b2010-09-08 03:48:47 +000045 void __user *namep;
46 namep = (void __user __force *) m->msg_name;
47 err = move_addr_to_kernel(namep, m->msg_namelen,
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 address);
49 if (err < 0)
50 return err;
51 }
52 m->msg_name = address;
53 } else {
54 m->msg_name = NULL;
55 }
56
57 size = m->msg_iovlen * sizeof(struct iovec);
Namhyung Kima700d8b2010-09-08 03:48:47 +000058 if (copy_from_user(iov, (void __user __force *) m->msg_iov, size))
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 return -EFAULT;
60
61 m->msg_iov = iov;
62 err = 0;
63
64 for (ct = 0; ct < m->msg_iovlen; ct++) {
65 err += iov[ct].iov_len;
66 /*
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.
70 */
71 if (err < 0)
72 return -EMSGSIZE;
73 }
74
75 return err;
76}
77
78/*
79 * Copy kernel to iovec. Returns -EFAULT on error.
80 *
81 * Note: this modifies the original iovec.
82 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090083
Linus Torvalds1da177e2005-04-16 15:20:36 -070084int memcpy_toiovec(struct iovec *iov, unsigned char *kdata, int len)
85{
86 while (len > 0) {
87 if (iov->iov_len) {
88 int copy = min_t(unsigned int, iov->iov_len, len);
89 if (copy_to_user(iov->iov_base, kdata, copy))
90 return -EFAULT;
91 kdata += copy;
92 len -= copy;
93 iov->iov_len -= copy;
94 iov->iov_base += copy;
95 }
96 iov++;
97 }
98
99 return 0;
100}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000101EXPORT_SYMBOL(memcpy_toiovec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103/*
Michael S. Tsirkin0a1ec072009-04-20 01:25:46 +0000104 * Copy kernel to iovec. Returns -EFAULT on error.
105 */
106
107int memcpy_toiovecend(const struct iovec *iov, unsigned char *kdata,
108 int offset, int len)
109{
110 int copy;
111 for (; len > 0; ++iov) {
112 /* Skip over the finished iovecs */
113 if (unlikely(offset >= iov->iov_len)) {
114 offset -= iov->iov_len;
115 continue;
116 }
117 copy = min_t(unsigned int, iov->iov_len - offset, len);
Sridhar Samudrala2faef522009-06-05 09:35:44 +0000118 if (copy_to_user(iov->iov_base + offset, kdata, copy))
Michael S. Tsirkin0a1ec072009-04-20 01:25:46 +0000119 return -EFAULT;
Sridhar Samudrala2faef522009-06-05 09:35:44 +0000120 offset = 0;
Michael S. Tsirkin0a1ec072009-04-20 01:25:46 +0000121 kdata += copy;
122 len -= copy;
123 }
124
125 return 0;
126}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000127EXPORT_SYMBOL(memcpy_toiovecend);
Michael S. Tsirkin0a1ec072009-04-20 01:25:46 +0000128
129/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 * Copy iovec to kernel. Returns -EFAULT on error.
131 *
132 * Note: this modifies the original iovec.
133 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135int memcpy_fromiovec(unsigned char *kdata, struct iovec *iov, int len)
136{
137 while (len > 0) {
138 if (iov->iov_len) {
139 int copy = min_t(unsigned int, len, iov->iov_len);
140 if (copy_from_user(kdata, iov->iov_base, copy))
141 return -EFAULT;
142 len -= copy;
143 kdata += copy;
144 iov->iov_base += copy;
145 iov->iov_len -= copy;
146 }
147 iov++;
148 }
149
150 return 0;
151}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000152EXPORT_SYMBOL(memcpy_fromiovec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154/*
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000155 * Copy iovec from kernel. Returns -EFAULT on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 */
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000157
158int memcpy_fromiovecend(unsigned char *kdata, const struct iovec *iov,
159 int offset, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 /* Skip over the finished iovecs */
162 while (offset >= iov->iov_len) {
163 offset -= iov->iov_len;
164 iov++;
165 }
166
167 while (len > 0) {
168 u8 __user *base = iov->iov_base + offset;
169 int copy = min_t(unsigned int, len, iov->iov_len - offset);
170
171 offset = 0;
172 if (copy_from_user(kdata, base, copy))
173 return -EFAULT;
174 len -= copy;
175 kdata += copy;
176 iov++;
177 }
178
179 return 0;
180}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000181EXPORT_SYMBOL(memcpy_fromiovecend);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183/*
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
187 *
188 * ip_build_xmit must ensure that when fragmenting only the last
189 * call to this function will be unaligned also.
190 */
191int csum_partial_copy_fromiovecend(unsigned char *kdata, struct iovec *iov,
Al Viro44bb9362006-11-14 21:36:14 -0800192 int offset, unsigned int len, __wsum *csump)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Al Viro44bb9362006-11-14 21:36:14 -0800194 __wsum csum = *csump;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 int partial_cnt = 0, err = 0;
196
197 /* Skip over the finished iovecs */
198 while (offset >= iov->iov_len) {
199 offset -= iov->iov_len;
200 iov++;
201 }
202
203 while (len > 0) {
204 u8 __user *base = iov->iov_base + offset;
205 int copy = min_t(unsigned int, len, iov->iov_len - offset);
206
207 offset = 0;
208
209 /* There is a remnant from previous iov. */
210 if (partial_cnt) {
211 int par_len = 4 - partial_cnt;
212
213 /* iov component is too short ... */
214 if (par_len > copy) {
215 if (copy_from_user(kdata, base, copy))
216 goto out_fault;
217 kdata += copy;
218 base += copy;
219 partial_cnt += copy;
220 len -= copy;
221 iov++;
222 if (len)
223 continue;
224 *csump = csum_partial(kdata - partial_cnt,
225 partial_cnt, csum);
226 goto out;
227 }
228 if (copy_from_user(kdata, base, par_len))
229 goto out_fault;
230 csum = csum_partial(kdata - partial_cnt, 4, csum);
231 kdata += par_len;
232 base += par_len;
233 copy -= par_len;
234 len -= par_len;
235 partial_cnt = 0;
236 }
237
238 if (len > copy) {
239 partial_cnt = copy % 4;
240 if (partial_cnt) {
241 copy -= partial_cnt;
242 if (copy_from_user(kdata + copy, base + copy,
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900243 partial_cnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 goto out_fault;
245 }
246 }
247
248 if (copy) {
249 csum = csum_and_copy_from_user(base, kdata, copy,
250 csum, &err);
251 if (err)
252 goto out;
253 }
254 len -= copy + partial_cnt;
255 kdata += copy + partial_cnt;
256 iov++;
257 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900258 *csump = csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259out:
260 return err;
261
262out_fault:
263 err = -EFAULT;
264 goto out;
265}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266EXPORT_SYMBOL(csum_partial_copy_fromiovecend);