blob: f4657c2127b4a9bc998cb0e91bf5405f83d5846e [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
YOSHIFUJI Hideaki230b1832008-07-19 22:35:47 -070038int verify_iovec(struct msghdr *m, struct iovec *iov, struct sockaddr *address, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
40 int size, err, ct;
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 if (m->msg_namelen) {
43 if (mode == VERIFY_READ) {
Namhyung Kima700d8b2010-09-08 03:48:47 +000044 void __user *namep;
45 namep = (void __user __force *) m->msg_name;
46 err = move_addr_to_kernel(namep, m->msg_namelen,
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 address);
48 if (err < 0)
49 return err;
50 }
51 m->msg_name = address;
52 } else {
53 m->msg_name = NULL;
54 }
55
56 size = m->msg_iovlen * sizeof(struct iovec);
Namhyung Kima700d8b2010-09-08 03:48:47 +000057 if (copy_from_user(iov, (void __user __force *) m->msg_iov, size))
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 return -EFAULT;
59
60 m->msg_iov = iov;
61 err = 0;
62
63 for (ct = 0; ct < m->msg_iovlen; ct++) {
64 err += iov[ct].iov_len;
65 /*
66 * Goal is not to verify user data, but to prevent returning
67 * negative value, which is interpreted as errno.
68 * Overflow is still possible, but it is harmless.
69 */
70 if (err < 0)
71 return -EMSGSIZE;
72 }
73
74 return err;
75}
76
77/*
78 * Copy kernel to iovec. Returns -EFAULT on error.
79 *
80 * Note: this modifies the original iovec.
81 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083int memcpy_toiovec(struct iovec *iov, unsigned char *kdata, int len)
84{
85 while (len > 0) {
86 if (iov->iov_len) {
87 int copy = min_t(unsigned int, iov->iov_len, len);
88 if (copy_to_user(iov->iov_base, kdata, copy))
89 return -EFAULT;
90 kdata += copy;
91 len -= copy;
92 iov->iov_len -= copy;
93 iov->iov_base += copy;
94 }
95 iov++;
96 }
97
98 return 0;
99}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000100EXPORT_SYMBOL(memcpy_toiovec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102/*
Michael S. Tsirkin0a1ec072009-04-20 01:25:46 +0000103 * Copy kernel to iovec. Returns -EFAULT on error.
104 */
105
106int memcpy_toiovecend(const struct iovec *iov, unsigned char *kdata,
107 int offset, int len)
108{
109 int copy;
110 for (; len > 0; ++iov) {
111 /* Skip over the finished iovecs */
112 if (unlikely(offset >= iov->iov_len)) {
113 offset -= iov->iov_len;
114 continue;
115 }
116 copy = min_t(unsigned int, iov->iov_len - offset, len);
Sridhar Samudrala2faef522009-06-05 09:35:44 +0000117 if (copy_to_user(iov->iov_base + offset, kdata, copy))
Michael S. Tsirkin0a1ec072009-04-20 01:25:46 +0000118 return -EFAULT;
Sridhar Samudrala2faef522009-06-05 09:35:44 +0000119 offset = 0;
Michael S. Tsirkin0a1ec072009-04-20 01:25:46 +0000120 kdata += copy;
121 len -= copy;
122 }
123
124 return 0;
125}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000126EXPORT_SYMBOL(memcpy_toiovecend);
Michael S. Tsirkin0a1ec072009-04-20 01:25:46 +0000127
128/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 * Copy iovec to kernel. Returns -EFAULT on error.
130 *
131 * Note: this modifies the original iovec.
132 */
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134int memcpy_fromiovec(unsigned char *kdata, struct iovec *iov, int len)
135{
136 while (len > 0) {
137 if (iov->iov_len) {
138 int copy = min_t(unsigned int, len, iov->iov_len);
139 if (copy_from_user(kdata, iov->iov_base, copy))
140 return -EFAULT;
141 len -= copy;
142 kdata += copy;
143 iov->iov_base += copy;
144 iov->iov_len -= copy;
145 }
146 iov++;
147 }
148
149 return 0;
150}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000151EXPORT_SYMBOL(memcpy_fromiovec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153/*
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000154 * Copy iovec from kernel. Returns -EFAULT on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 */
Michael S. Tsirkin6f26c9a2009-04-20 01:26:11 +0000156
157int memcpy_fromiovecend(unsigned char *kdata, const struct iovec *iov,
158 int offset, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
160 /* Skip over the finished iovecs */
161 while (offset >= iov->iov_len) {
162 offset -= iov->iov_len;
163 iov++;
164 }
165
166 while (len > 0) {
167 u8 __user *base = iov->iov_base + offset;
168 int copy = min_t(unsigned int, len, iov->iov_len - offset);
169
170 offset = 0;
171 if (copy_from_user(kdata, base, copy))
172 return -EFAULT;
173 len -= copy;
174 kdata += copy;
175 iov++;
176 }
177
178 return 0;
179}
Eric Dumazet9e34a5b2010-07-09 21:22:04 +0000180EXPORT_SYMBOL(memcpy_fromiovecend);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182/*
183 * And now for the all-in-one: copy and checksum from a user iovec
184 * directly to a datagram
185 * Calls to csum_partial but the last must be in 32 bit chunks
186 *
187 * ip_build_xmit must ensure that when fragmenting only the last
188 * call to this function will be unaligned also.
189 */
190int csum_partial_copy_fromiovecend(unsigned char *kdata, struct iovec *iov,
Al Viro44bb9362006-11-14 21:36:14 -0800191 int offset, unsigned int len, __wsum *csump)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Al Viro44bb9362006-11-14 21:36:14 -0800193 __wsum csum = *csump;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 int partial_cnt = 0, err = 0;
195
196 /* Skip over the finished iovecs */
197 while (offset >= iov->iov_len) {
198 offset -= iov->iov_len;
199 iov++;
200 }
201
202 while (len > 0) {
203 u8 __user *base = iov->iov_base + offset;
204 int copy = min_t(unsigned int, len, iov->iov_len - offset);
205
206 offset = 0;
207
208 /* There is a remnant from previous iov. */
209 if (partial_cnt) {
210 int par_len = 4 - partial_cnt;
211
212 /* iov component is too short ... */
213 if (par_len > copy) {
214 if (copy_from_user(kdata, base, copy))
215 goto out_fault;
216 kdata += copy;
217 base += copy;
218 partial_cnt += copy;
219 len -= copy;
220 iov++;
221 if (len)
222 continue;
223 *csump = csum_partial(kdata - partial_cnt,
224 partial_cnt, csum);
225 goto out;
226 }
227 if (copy_from_user(kdata, base, par_len))
228 goto out_fault;
229 csum = csum_partial(kdata - partial_cnt, 4, csum);
230 kdata += par_len;
231 base += par_len;
232 copy -= par_len;
233 len -= par_len;
234 partial_cnt = 0;
235 }
236
237 if (len > copy) {
238 partial_cnt = copy % 4;
239 if (partial_cnt) {
240 copy -= partial_cnt;
241 if (copy_from_user(kdata + copy, base + copy,
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900242 partial_cnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 goto out_fault;
244 }
245 }
246
247 if (copy) {
248 csum = csum_and_copy_from_user(base, kdata, copy,
249 csum, &err);
250 if (err)
251 goto out;
252 }
253 len -= copy + partial_cnt;
254 kdata += copy + partial_cnt;
255 iov++;
256 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900257 *csump = csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258out:
259 return err;
260
261out_fault:
262 err = -EFAULT;
263 goto out;
264}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265EXPORT_SYMBOL(csum_partial_copy_fromiovecend);