Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/kernel.h> |
| 22 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Jesper Juhl | e49332b | 2005-05-01 08:59:08 -0700 | [diff] [blame] | 34 | * Save time not doing access_ok. copy_*_user will make this work |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | * in any case. |
| 36 | */ |
| 37 | |
David S. Miller | 01db403 | 2010-09-27 20:24:54 -0700 | [diff] [blame] | 38 | long verify_iovec(struct msghdr *m, struct iovec *iov, struct sockaddr *address, int mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | { |
David S. Miller | 01db403 | 2010-09-27 20:24:54 -0700 | [diff] [blame] | 40 | int size, ct; |
| 41 | long err; |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 42 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | if (m->msg_namelen) { |
| 44 | if (mode == VERIFY_READ) { |
Namhyung Kim | a700d8b | 2010-09-08 03:48:47 +0000 | [diff] [blame] | 45 | void __user *namep; |
| 46 | namep = (void __user __force *) m->msg_name; |
| 47 | err = move_addr_to_kernel(namep, m->msg_namelen, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Namhyung Kim | a700d8b | 2010-09-08 03:48:47 +0000 | [diff] [blame] | 58 | if (copy_from_user(iov, (void __user __force *) m->msg_iov, size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 83 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Eric Dumazet | 9e34a5b | 2010-07-09 21:22:04 +0000 | [diff] [blame] | 101 | EXPORT_SYMBOL(memcpy_toiovec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
| 103 | /* |
Michael S. Tsirkin | 0a1ec07 | 2009-04-20 01:25:46 +0000 | [diff] [blame] | 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); |
Sridhar Samudrala | 2faef52 | 2009-06-05 09:35:44 +0000 | [diff] [blame] | 118 | if (copy_to_user(iov->iov_base + offset, kdata, copy)) |
Michael S. Tsirkin | 0a1ec07 | 2009-04-20 01:25:46 +0000 | [diff] [blame] | 119 | return -EFAULT; |
Sridhar Samudrala | 2faef52 | 2009-06-05 09:35:44 +0000 | [diff] [blame] | 120 | offset = 0; |
Michael S. Tsirkin | 0a1ec07 | 2009-04-20 01:25:46 +0000 | [diff] [blame] | 121 | kdata += copy; |
| 122 | len -= copy; |
| 123 | } |
| 124 | |
| 125 | return 0; |
| 126 | } |
Eric Dumazet | 9e34a5b | 2010-07-09 21:22:04 +0000 | [diff] [blame] | 127 | EXPORT_SYMBOL(memcpy_toiovecend); |
Michael S. Tsirkin | 0a1ec07 | 2009-04-20 01:25:46 +0000 | [diff] [blame] | 128 | |
| 129 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | * Copy iovec to kernel. Returns -EFAULT on error. |
| 131 | * |
| 132 | * Note: this modifies the original iovec. |
| 133 | */ |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 134 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Eric Dumazet | 9e34a5b | 2010-07-09 21:22:04 +0000 | [diff] [blame] | 152 | EXPORT_SYMBOL(memcpy_fromiovec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
| 154 | /* |
Michael S. Tsirkin | 6f26c9a | 2009-04-20 01:26:11 +0000 | [diff] [blame] | 155 | * Copy iovec from kernel. Returns -EFAULT on error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | */ |
Michael S. Tsirkin | 6f26c9a | 2009-04-20 01:26:11 +0000 | [diff] [blame] | 157 | |
| 158 | int memcpy_fromiovecend(unsigned char *kdata, const struct iovec *iov, |
| 159 | int offset, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Eric Dumazet | 9e34a5b | 2010-07-09 21:22:04 +0000 | [diff] [blame] | 181 | EXPORT_SYMBOL(memcpy_fromiovecend); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
Al Viro | 44bb936 | 2006-11-14 21:36:14 -0800 | [diff] [blame] | 192 | int offset, unsigned int len, __wsum *csump) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | { |
Al Viro | 44bb936 | 2006-11-14 21:36:14 -0800 | [diff] [blame] | 194 | __wsum csum = *csump; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 243 | partial_cnt)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 258 | *csump = csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | out: |
| 260 | return err; |
| 261 | |
| 262 | out_fault: |
| 263 | err = -EFAULT; |
| 264 | goto out; |
| 265 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | EXPORT_SYMBOL(csum_partial_copy_fromiovecend); |