]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/core/iovec.c
net/core: remove address space warnings on verify_iovec()
[net-next-2.6.git] / net / core / iovec.c
CommitLineData
1da177e4
LT
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
19#include <linux/errno.h>
20#include <linux/module.h>
1da177e4
LT
21#include <linux/kernel.h>
22#include <linux/mm.h>
1da177e4
LT
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>
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 *
e49332bd 34 * Save time not doing access_ok. copy_*_user will make this work
1da177e4
LT
35 * in any case.
36 */
37
230b1839 38int verify_iovec(struct msghdr *m, struct iovec *iov, struct sockaddr *address, int mode)
1da177e4
LT
39{
40 int size, err, ct;
4ec93edb 41
1da177e4
LT
42 if (m->msg_namelen) {
43 if (mode == VERIFY_READ) {
a700d8be
NK
44 void __user *namep;
45 namep = (void __user __force *) m->msg_name;
46 err = move_addr_to_kernel(namep, m->msg_namelen,
1da177e4
LT
47 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);
a700d8be 57 if (copy_from_user(iov, (void __user __force *) m->msg_iov, size))
1da177e4
LT
58 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 */
4ec93edb 82
1da177e4
LT
83int 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}
9e34a5b5 100EXPORT_SYMBOL(memcpy_toiovec);
1da177e4 101
0a1ec07a
MT
102/*
103 * 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);
2faef52b 117 if (copy_to_user(iov->iov_base + offset, kdata, copy))
0a1ec07a 118 return -EFAULT;
2faef52b 119 offset = 0;
0a1ec07a
MT
120 kdata += copy;
121 len -= copy;
122 }
123
124 return 0;
125}
9e34a5b5 126EXPORT_SYMBOL(memcpy_toiovecend);
0a1ec07a 127
1da177e4
LT
128/*
129 * Copy iovec to kernel. Returns -EFAULT on error.
130 *
131 * Note: this modifies the original iovec.
132 */
4ec93edb 133
1da177e4
LT
134int 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}
9e34a5b5 151EXPORT_SYMBOL(memcpy_fromiovec);
1da177e4
LT
152
153/*
6f26c9a7 154 * Copy iovec from kernel. Returns -EFAULT on error.
1da177e4 155 */
6f26c9a7
MT
156
157int memcpy_fromiovecend(unsigned char *kdata, const struct iovec *iov,
158 int offset, int len)
1da177e4
LT
159{
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}
9e34a5b5 180EXPORT_SYMBOL(memcpy_fromiovecend);
1da177e4
LT
181
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,
44bb9363 191 int offset, unsigned int len, __wsum *csump)
1da177e4 192{
44bb9363 193 __wsum csum = *csump;
1da177e4
LT
194 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,
4ec93edb 242 partial_cnt))
1da177e4
LT
243 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 }
4ec93edb 257 *csump = csum;
1da177e4
LT
258out:
259 return err;
260
261out_fault:
262 err = -EFAULT;
263 goto out;
264}
1da177e4 265EXPORT_SYMBOL(csum_partial_copy_fromiovecend);