net/mlx4_en: Fix mixed PFC and Global pause user control requests
[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 int verify_iovec(struct msghdr *m, struct iovec *iov, struct sockaddr *address, int mode)
39 {
40         int size, ct, err;
41
42         if (m->msg_name && m->msg_namelen) {
43                 if (mode == VERIFY_READ) {
44                         void __user *namep;
45                         namep = (void __user __force *) m->msg_name;
46                         err = move_addr_to_kernel(namep, 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                 m->msg_namelen = 0;
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                 size_t len = iov[ct].iov_len;
66
67                 if (len > INT_MAX - err) {
68                         len = INT_MAX - err;
69                         iov[ct].iov_len = len;
70                 }
71                 err += len;
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 EXPORT_SYMBOL(memcpy_toiovec);
101
102 /*
103  *      Copy kernel to iovec. Returns -EFAULT on error.
104  */
105
106 int 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);
117                 if (copy_to_user(iov->iov_base + offset, kdata, copy))
118                         return -EFAULT;
119                 offset = 0;
120                 kdata += copy;
121                 len -= copy;
122         }
123
124         return 0;
125 }
126 EXPORT_SYMBOL(memcpy_toiovecend);
127
128 /*
129  *      Copy iovec to kernel. Returns -EFAULT on error.
130  *
131  *      Note: this modifies the original iovec.
132  */
133
134 int 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 }
151 EXPORT_SYMBOL(memcpy_fromiovec);
152
153 /*
154  *      Copy iovec from kernel. Returns -EFAULT on error.
155  */
156
157 int memcpy_fromiovecend(unsigned char *kdata, const struct iovec *iov,
158                         int offset, int len)
159 {
160         /* No data? Done! */
161         if (len == 0)
162                 return 0;
163
164         /* Skip over the finished iovecs */
165         while (offset >= iov->iov_len) {
166                 offset -= iov->iov_len;
167                 iov++;
168         }
169
170         while (len > 0) {
171                 u8 __user *base = iov->iov_base + offset;
172                 int copy = min_t(unsigned int, len, iov->iov_len - offset);
173
174                 offset = 0;
175                 if (copy_from_user(kdata, base, copy))
176                         return -EFAULT;
177                 len -= copy;
178                 kdata += copy;
179                 iov++;
180         }
181
182         return 0;
183 }
184 EXPORT_SYMBOL(memcpy_fromiovecend);
185
186 /*
187  *      And now for the all-in-one: copy and checksum from a user iovec
188  *      directly to a datagram
189  *      Calls to csum_partial but the last must be in 32 bit chunks
190  *
191  *      ip_build_xmit must ensure that when fragmenting only the last
192  *      call to this function will be unaligned also.
193  */
194 int csum_partial_copy_fromiovecend(unsigned char *kdata, struct iovec *iov,
195                                  int offset, unsigned int len, __wsum *csump)
196 {
197         __wsum csum = *csump;
198         int partial_cnt = 0, err = 0;
199
200         /* Skip over the finished iovecs */
201         while (offset >= iov->iov_len) {
202                 offset -= iov->iov_len;
203                 iov++;
204         }
205
206         while (len > 0) {
207                 u8 __user *base = iov->iov_base + offset;
208                 int copy = min_t(unsigned int, len, iov->iov_len - offset);
209
210                 offset = 0;
211
212                 /* There is a remnant from previous iov. */
213                 if (partial_cnt) {
214                         int par_len = 4 - partial_cnt;
215
216                         /* iov component is too short ... */
217                         if (par_len > copy) {
218                                 if (copy_from_user(kdata, base, copy))
219                                         goto out_fault;
220                                 kdata += copy;
221                                 base += copy;
222                                 partial_cnt += copy;
223                                 len -= copy;
224                                 iov++;
225                                 if (len)
226                                         continue;
227                                 *csump = csum_partial(kdata - partial_cnt,
228                                                          partial_cnt, csum);
229                                 goto out;
230                         }
231                         if (copy_from_user(kdata, base, par_len))
232                                 goto out_fault;
233                         csum = csum_partial(kdata - partial_cnt, 4, csum);
234                         kdata += par_len;
235                         base  += par_len;
236                         copy  -= par_len;
237                         len   -= par_len;
238                         partial_cnt = 0;
239                 }
240
241                 if (len > copy) {
242                         partial_cnt = copy % 4;
243                         if (partial_cnt) {
244                                 copy -= partial_cnt;
245                                 if (copy_from_user(kdata + copy, base + copy,
246                                                 partial_cnt))
247                                         goto out_fault;
248                         }
249                 }
250
251                 if (copy) {
252                         csum = csum_and_copy_from_user(base, kdata, copy,
253                                                         csum, &err);
254                         if (err)
255                                 goto out;
256                 }
257                 len   -= copy + partial_cnt;
258                 kdata += copy + partial_cnt;
259                 iov++;
260         }
261         *csump = csum;
262 out:
263         return err;
264
265 out_fault:
266         err = -EFAULT;
267         goto out;
268 }
269 EXPORT_SYMBOL(csum_partial_copy_fromiovecend);