Merge git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging-2.6
[pandora-kernel.git] / net / core / iovec.c
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>
21 #include <linux/kernel.h>
22 #include <linux/mm.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>
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  *
34  *      Save time not doing access_ok. copy_*_user will make this work
35  *      in any case.
36  */
37
38 long verify_iovec(struct msghdr *m, struct iovec *iov, struct sockaddr *address, int mode)
39 {
40         int size, ct;
41         long err;
42
43         if (m->msg_namelen) {
44                 if (mode == VERIFY_READ) {
45                         void __user *namep;
46                         namep = (void __user __force *) m->msg_name;
47                         err = move_addr_to_kernel(namep, m->msg_namelen,
48                                                   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);
58         if (copy_from_user(iov, (void __user __force *) m->msg_iov, size))
59                 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  */
83
84 int 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 }
101 EXPORT_SYMBOL(memcpy_toiovec);
102
103 /*
104  *      Copy kernel to iovec. Returns -EFAULT on error.
105  */
106
107 int 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);
118                 if (copy_to_user(iov->iov_base + offset, kdata, copy))
119                         return -EFAULT;
120                 offset = 0;
121                 kdata += copy;
122                 len -= copy;
123         }
124
125         return 0;
126 }
127 EXPORT_SYMBOL(memcpy_toiovecend);
128
129 /*
130  *      Copy iovec to kernel. Returns -EFAULT on error.
131  *
132  *      Note: this modifies the original iovec.
133  */
134
135 int 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 }
152 EXPORT_SYMBOL(memcpy_fromiovec);
153
154 /*
155  *      Copy iovec from kernel. Returns -EFAULT on error.
156  */
157
158 int memcpy_fromiovecend(unsigned char *kdata, const struct iovec *iov,
159                         int offset, int len)
160 {
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 }
181 EXPORT_SYMBOL(memcpy_fromiovecend);
182
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  */
191 int csum_partial_copy_fromiovecend(unsigned char *kdata, struct iovec *iov,
192                                  int offset, unsigned int len, __wsum *csump)
193 {
194         __wsum csum = *csump;
195         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,
243                                                 partial_cnt))
244                                         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         }
258         *csump = csum;
259 out:
260         return err;
261
262 out_fault:
263         err = -EFAULT;
264         goto out;
265 }
266 EXPORT_SYMBOL(csum_partial_copy_fromiovecend);