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 | |
YOSHIFUJI Hideaki | 230b183 | 2008-07-19 22:35:47 -0700 | [diff] [blame] | 38 | int 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 | { |
| 40 | int size, err, ct; |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | if (m->msg_namelen) { |
| 43 | if (mode == VERIFY_READ) { |
Namhyung Kim | a700d8b | 2010-09-08 03:48:47 +0000 | [diff] [blame^] | 44 | void __user *namep; |
| 45 | namep = (void __user __force *) m->msg_name; |
| 46 | err = move_addr_to_kernel(namep, m->msg_namelen, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Namhyung Kim | a700d8b | 2010-09-08 03:48:47 +0000 | [diff] [blame^] | 57 | if (copy_from_user(iov, (void __user __force *) m->msg_iov, size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 82 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Eric Dumazet | 9e34a5b | 2010-07-09 21:22:04 +0000 | [diff] [blame] | 100 | EXPORT_SYMBOL(memcpy_toiovec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
| 102 | /* |
Michael S. Tsirkin | 0a1ec07 | 2009-04-20 01:25:46 +0000 | [diff] [blame] | 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); |
Sridhar Samudrala | 2faef52 | 2009-06-05 09:35:44 +0000 | [diff] [blame] | 117 | if (copy_to_user(iov->iov_base + offset, kdata, copy)) |
Michael S. Tsirkin | 0a1ec07 | 2009-04-20 01:25:46 +0000 | [diff] [blame] | 118 | return -EFAULT; |
Sridhar Samudrala | 2faef52 | 2009-06-05 09:35:44 +0000 | [diff] [blame] | 119 | offset = 0; |
Michael S. Tsirkin | 0a1ec07 | 2009-04-20 01:25:46 +0000 | [diff] [blame] | 120 | kdata += copy; |
| 121 | len -= copy; |
| 122 | } |
| 123 | |
| 124 | return 0; |
| 125 | } |
Eric Dumazet | 9e34a5b | 2010-07-09 21:22:04 +0000 | [diff] [blame] | 126 | EXPORT_SYMBOL(memcpy_toiovecend); |
Michael S. Tsirkin | 0a1ec07 | 2009-04-20 01:25:46 +0000 | [diff] [blame] | 127 | |
| 128 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | * Copy iovec to kernel. Returns -EFAULT on error. |
| 130 | * |
| 131 | * Note: this modifies the original iovec. |
| 132 | */ |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 133 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Eric Dumazet | 9e34a5b | 2010-07-09 21:22:04 +0000 | [diff] [blame] | 151 | EXPORT_SYMBOL(memcpy_fromiovec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
| 153 | /* |
Michael S. Tsirkin | 6f26c9a | 2009-04-20 01:26:11 +0000 | [diff] [blame] | 154 | * Copy iovec from kernel. Returns -EFAULT on error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | */ |
Michael S. Tsirkin | 6f26c9a | 2009-04-20 01:26:11 +0000 | [diff] [blame] | 156 | |
| 157 | int memcpy_fromiovecend(unsigned char *kdata, const struct iovec *iov, |
| 158 | int offset, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Eric Dumazet | 9e34a5b | 2010-07-09 21:22:04 +0000 | [diff] [blame] | 180 | EXPORT_SYMBOL(memcpy_fromiovecend); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
| 190 | int csum_partial_copy_fromiovecend(unsigned char *kdata, struct iovec *iov, |
Al Viro | 44bb936 | 2006-11-14 21:36:14 -0800 | [diff] [blame] | 191 | int offset, unsigned int len, __wsum *csump) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | { |
Al Viro | 44bb936 | 2006-11-14 21:36:14 -0800 | [diff] [blame] | 193 | __wsum csum = *csump; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 242 | partial_cnt)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 257 | *csump = csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | out: |
| 259 | return err; |
| 260 | |
| 261 | out_fault: |
| 262 | err = -EFAULT; |
| 263 | goto out; |
| 264 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | EXPORT_SYMBOL(csum_partial_copy_fromiovecend); |