patch-2_6_7-vs1_9_1_12
[linux-2.6.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/sched.h>
22 #include <linux/kernel.h>
23 #include <linux/mm.h>
24 #include <linux/slab.h>
25 #include <linux/net.h>
26 #include <linux/in6.h>
27 #include <asm/uaccess.h>
28 #include <asm/byteorder.h>
29 #include <net/checksum.h>
30 #include <net/sock.h>
31
32 /*
33  *      Verify iovec. The caller must ensure that the iovec is big enough
34  *      to hold the message iovec.
35  *
36  *      Save time not doing verify_area. copy_*_user will make this work
37  *      in any case.
38  */
39
40 int verify_iovec(struct msghdr *m, struct iovec *iov, char *address, int mode)
41 {
42         int size, err, ct;
43         
44         if (m->msg_namelen) {
45                 if (mode == VERIFY_READ) {
46                         err = move_addr_to_kernel(m->msg_name, m->msg_namelen,
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);
57         if (copy_from_user(iov, m->msg_iov, size))
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  */
82  
83 int 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 }
100
101 /*
102  *      In kernel copy to iovec. Returns -EFAULT on error.
103  *
104  *      Note: this modifies the original iovec.
105  */
106  
107 void memcpy_tokerneliovec(struct iovec *iov, unsigned char *kdata, int len)
108 {
109         while (len > 0) {
110                 if (iov->iov_len) {
111                         int copy = min_t(unsigned int, iov->iov_len, len);
112                         memcpy(iov->iov_base, kdata, copy);
113                         kdata += copy;
114                         len -= copy;
115                         iov->iov_len -= copy;
116                         iov->iov_base += copy;
117                 }
118                 iov++;
119         }
120 }
121
122
123 /*
124  *      Copy iovec to kernel. Returns -EFAULT on error.
125  *
126  *      Note: this modifies the original iovec.
127  */
128  
129 int memcpy_fromiovec(unsigned char *kdata, struct iovec *iov, int len)
130 {
131         while (len > 0) {
132                 if (iov->iov_len) {
133                         int copy = min_t(unsigned int, len, iov->iov_len);
134                         if (copy_from_user(kdata, iov->iov_base, copy))
135                                 return -EFAULT;
136                         len -= copy;
137                         kdata += copy;
138                         iov->iov_base += copy;
139                         iov->iov_len -= copy;
140                 }
141                 iov++;
142         }
143
144         return 0;
145 }
146
147 /*
148  *      For use with ip_build_xmit
149  */
150 int memcpy_fromiovecend(unsigned char *kdata, struct iovec *iov, int offset,
151                         int len)
152 {
153         /* Skip over the finished iovecs */
154         while (offset >= iov->iov_len) {
155                 offset -= iov->iov_len;
156                 iov++;
157         }
158
159         while (len > 0) {
160                 u8 __user *base = iov->iov_base + offset;
161                 int copy = min_t(unsigned int, len, iov->iov_len - offset);
162
163                 offset = 0;
164                 if (copy_from_user(kdata, base, copy))
165                         return -EFAULT;
166                 len -= copy;
167                 kdata += copy;
168                 iov++;
169         }
170
171         return 0;
172 }
173
174 /*
175  *      And now for the all-in-one: copy and checksum from a user iovec
176  *      directly to a datagram
177  *      Calls to csum_partial but the last must be in 32 bit chunks
178  *
179  *      ip_build_xmit must ensure that when fragmenting only the last
180  *      call to this function will be unaligned also.
181  */
182 int csum_partial_copy_fromiovecend(unsigned char *kdata, struct iovec *iov,
183                                  int offset, unsigned int len, int *csump)
184 {
185         int csum = *csump;
186         int partial_cnt = 0, err = 0;
187
188         /* Skip over the finished iovecs */
189         while (offset >= iov->iov_len) {
190                 offset -= iov->iov_len;
191                 iov++;
192         }
193
194         while (len > 0) {
195                 u8 __user *base = iov->iov_base + offset;
196                 int copy = min_t(unsigned int, len, iov->iov_len - offset);
197
198                 offset = 0;
199
200                 /* There is a remnant from previous iov. */
201                 if (partial_cnt) {
202                         int par_len = 4 - partial_cnt;
203
204                         /* iov component is too short ... */
205                         if (par_len > copy) {
206                                 if (copy_from_user(kdata, base, copy))
207                                         goto out_fault;
208                                 kdata += copy;
209                                 base += copy;
210                                 partial_cnt += copy;
211                                 len -= copy;
212                                 iov++;
213                                 if (len)
214                                         continue;
215                                 *csump = csum_partial(kdata - partial_cnt,
216                                                          partial_cnt, csum);
217                                 goto out;
218                         }
219                         if (copy_from_user(kdata, base, par_len))
220                                 goto out_fault;
221                         csum = csum_partial(kdata - partial_cnt, 4, csum);
222                         kdata += par_len;
223                         base  += par_len;
224                         copy  -= par_len;
225                         len   -= par_len;
226                         partial_cnt = 0;
227                 }
228
229                 if (len > copy) {
230                         partial_cnt = copy % 4;
231                         if (partial_cnt) {
232                                 copy -= partial_cnt;
233                                 if (copy_from_user(kdata + copy, base + copy,
234                                                 partial_cnt))
235                                         goto out_fault;
236                         }
237                 }
238
239                 if (copy) {
240                         csum = csum_and_copy_from_user(base, kdata, copy,
241                                                         csum, &err);
242                         if (err)
243                                 goto out;
244                 }
245                 len   -= copy + partial_cnt;
246                 kdata += copy + partial_cnt;
247                 iov++;
248         }
249         *csump = csum;
250 out:
251         return err;
252
253 out_fault:
254         err = -EFAULT;
255         goto out;
256 }
257
258 EXPORT_SYMBOL(csum_partial_copy_fromiovecend);
259 EXPORT_SYMBOL(memcpy_fromiovec);
260 EXPORT_SYMBOL(memcpy_fromiovecend);
261 EXPORT_SYMBOL(memcpy_toiovec);
262 EXPORT_SYMBOL(memcpy_tokerneliovec);