1 /* $OpenBSD: criov.c,v 1.9 2002/01/29 15:48:29 jason Exp $ */
4 * Copyright (c) 1999 Theo de Raadt
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/errno.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
41 #include <sys/limits.h>
44 #include <opencrypto/cryptodev.h>
47 * This macro is only for avoiding code duplication, as we need to skip
48 * given number of bytes in the same way in three functions below.
50 #define CUIO_SKIP() do { \
51 KASSERT(off >= 0, ("%s: off %d < 0", __func__, off)); \
52 KASSERT(len >= 0, ("%s: len %d < 0", __func__, len)); \
54 KASSERT(iol >= 0, ("%s: empty in skip", __func__)); \
55 if (off < iov->iov_len) \
57 off -= iov->iov_len; \
64 cuio_copydata(struct uio* uio, int off, int len, caddr_t cp)
66 struct iovec *iov = uio->uio_iov;
67 int iol = uio->uio_iovcnt;
72 KASSERT(iol >= 0, ("%s: empty", __func__));
73 count = min(iov->iov_len - off, len);
74 bcopy(((caddr_t)iov->iov_base) + off, cp, count);
84 cuio_copyback(struct uio* uio, int off, int len, c_caddr_t cp)
86 struct iovec *iov = uio->uio_iov;
87 int iol = uio->uio_iovcnt;
92 KASSERT(iol >= 0, ("%s: empty", __func__));
93 count = min(iov->iov_len - off, len);
94 bcopy(cp, ((caddr_t)iov->iov_base) + off, count);
104 * Return the index and offset of location in iovec list.
107 cuio_getptr(struct uio *uio, int loc, int *off)
112 while (loc >= 0 && ind < uio->uio_iovcnt) {
113 len = uio->uio_iov[ind].iov_len;
122 if (ind > 0 && loc == 0) {
124 *off = uio->uio_iov[ind].iov_len;
132 crypto_cursor_init(struct crypto_buffer_cursor *cc,
133 const struct crypto_buffer *cb)
135 memset(cc, 0, sizeof(*cc));
136 cc->cc_type = cb->cb_type;
137 switch (cc->cc_type) {
138 case CRYPTO_BUF_CONTIG:
139 cc->cc_buf = cb->cb_buf;
140 cc->cc_buf_len = cb->cb_buf_len;
142 case CRYPTO_BUF_MBUF:
143 cc->cc_mbuf = cb->cb_mbuf;
146 cc->cc_iov = cb->cb_uio->uio_iov;
150 panic("%s: invalid buffer type %d", __func__, cb->cb_type);
157 crypto_cursor_advance(struct crypto_buffer_cursor *cc, size_t amount)
161 switch (cc->cc_type) {
162 case CRYPTO_BUF_CONTIG:
163 MPASS(cc->cc_buf_len >= amount);
164 cc->cc_buf += amount;
165 cc->cc_buf_len -= amount;
167 case CRYPTO_BUF_MBUF:
169 remain = cc->cc_mbuf->m_len - cc->cc_offset;
170 if (amount < remain) {
171 cc->cc_offset += amount;
175 cc->cc_mbuf = cc->cc_mbuf->m_next;
183 remain = cc->cc_iov->iov_len - cc->cc_offset;
184 if (amount < remain) {
185 cc->cc_offset += amount;
197 panic("%s: invalid buffer type %d", __func__, cc->cc_type);
204 crypto_cursor_segbase(struct crypto_buffer_cursor *cc)
206 switch (cc->cc_type) {
207 case CRYPTO_BUF_CONTIG:
209 case CRYPTO_BUF_MBUF:
210 if (cc->cc_mbuf == NULL)
212 KASSERT((cc->cc_mbuf->m_flags & M_EXTPG) == 0,
213 ("%s: not supported for unmapped mbufs", __func__));
214 return (mtod(cc->cc_mbuf, char *) + cc->cc_offset);
216 return ((char *)cc->cc_iov->iov_base + cc->cc_offset);
219 panic("%s: invalid buffer type %d", __func__, cc->cc_type);
226 crypto_cursor_seglen(struct crypto_buffer_cursor *cc)
228 switch (cc->cc_type) {
229 case CRYPTO_BUF_CONTIG:
230 return (cc->cc_buf_len);
231 case CRYPTO_BUF_MBUF:
232 if (cc->cc_mbuf == NULL)
234 return (cc->cc_mbuf->m_len - cc->cc_offset);
236 return (cc->cc_iov->iov_len - cc->cc_offset);
239 panic("%s: invalid buffer type %d", __func__, cc->cc_type);
246 crypto_cursor_copyback(struct crypto_buffer_cursor *cc, int size,
254 switch (cc->cc_type) {
255 case CRYPTO_BUF_CONTIG:
256 MPASS(cc->cc_buf_len >= size);
257 memcpy(cc->cc_buf, src, size);
259 cc->cc_buf_len -= size;
261 case CRYPTO_BUF_MBUF:
263 KASSERT((cc->cc_mbuf->m_flags & M_EXTPG) == 0,
264 ("%s: not supported for unmapped mbufs", __func__));
265 dst = mtod(cc->cc_mbuf, char *) + cc->cc_offset;
266 remain = cc->cc_mbuf->m_len - cc->cc_offset;
267 todo = MIN(remain, size);
268 memcpy(dst, src, todo);
271 cc->cc_offset += todo;
275 cc->cc_mbuf = cc->cc_mbuf->m_next;
283 dst = (char *)cc->cc_iov->iov_base + cc->cc_offset;
284 remain = cc->cc_iov->iov_len - cc->cc_offset;
285 todo = MIN(remain, size);
286 memcpy(dst, src, todo);
289 cc->cc_offset += todo;
301 panic("%s: invalid buffer type %d", __func__, cc->cc_type);
308 crypto_cursor_copydata(struct crypto_buffer_cursor *cc, int size, void *vdst)
315 switch (cc->cc_type) {
316 case CRYPTO_BUF_CONTIG:
317 MPASS(cc->cc_buf_len >= size);
318 memcpy(dst, cc->cc_buf, size);
320 cc->cc_buf_len -= size;
322 case CRYPTO_BUF_MBUF:
324 KASSERT((cc->cc_mbuf->m_flags & M_EXTPG) == 0,
325 ("%s: not supported for unmapped mbufs", __func__));
326 src = mtod(cc->cc_mbuf, const char *) + cc->cc_offset;
327 remain = cc->cc_mbuf->m_len - cc->cc_offset;
328 todo = MIN(remain, size);
329 memcpy(dst, src, todo);
332 cc->cc_offset += todo;
336 cc->cc_mbuf = cc->cc_mbuf->m_next;
344 src = (const char *)cc->cc_iov->iov_base +
346 remain = cc->cc_iov->iov_len - cc->cc_offset;
347 todo = MIN(remain, size);
348 memcpy(dst, src, todo);
351 cc->cc_offset += todo;
363 panic("%s: invalid buffer type %d", __func__, cc->cc_type);
370 * To avoid advancing 'cursor', make a local copy that gets advanced
374 crypto_cursor_copydata_noadv(struct crypto_buffer_cursor *cc, int size,
377 struct crypto_buffer_cursor copy;
380 crypto_cursor_copydata(©, size, vdst);
384 * Apply function f to the data in an iovec list starting "off" bytes from
385 * the beginning, continuing for "len" bytes.
388 cuio_apply(struct uio *uio, int off, int len,
389 int (*f)(void *, const void *, u_int), void *arg)
391 struct iovec *iov = uio->uio_iov;
392 int iol = uio->uio_iovcnt;
398 KASSERT(iol >= 0, ("%s: empty", __func__));
399 count = min(iov->iov_len - off, len);
400 rval = (*f)(arg, ((caddr_t)iov->iov_base) + off, count);
412 crypto_copyback(struct cryptop *crp, int off, int size, const void *src)
414 struct crypto_buffer *cb;
416 if (crp->crp_obuf.cb_type != CRYPTO_BUF_NONE)
420 switch (cb->cb_type) {
421 case CRYPTO_BUF_MBUF:
422 m_copyback(cb->cb_mbuf, off, size, src);
425 cuio_copyback(cb->cb_uio, off, size, src);
427 case CRYPTO_BUF_CONTIG:
428 MPASS(off + size <= cb->cb_buf_len);
429 bcopy(src, cb->cb_buf + off, size);
433 panic("invalid crp buf type %d", cb->cb_type);
440 crypto_copydata(struct cryptop *crp, int off, int size, void *dst)
443 switch (crp->crp_buf.cb_type) {
444 case CRYPTO_BUF_MBUF:
445 m_copydata(crp->crp_buf.cb_mbuf, off, size, dst);
448 cuio_copydata(crp->crp_buf.cb_uio, off, size, dst);
450 case CRYPTO_BUF_CONTIG:
451 MPASS(off + size <= crp->crp_buf.cb_buf_len);
452 bcopy(crp->crp_buf.cb_buf + off, dst, size);
456 panic("invalid crp buf type %d", crp->crp_buf.cb_type);
463 crypto_apply_buf(struct crypto_buffer *cb, int off, int len,
464 int (*f)(void *, const void *, u_int), void *arg)
468 switch (cb->cb_type) {
469 case CRYPTO_BUF_MBUF:
470 error = m_apply(cb->cb_mbuf, off, len,
471 (int (*)(void *, void *, u_int))f, arg);
474 error = cuio_apply(cb->cb_uio, off, len, f, arg);
476 case CRYPTO_BUF_CONTIG:
477 MPASS(off + len <= cb->cb_buf_len);
478 error = (*f)(arg, cb->cb_buf + off, len);
482 panic("invalid crypto buf type %d", cb->cb_type);
491 crypto_apply(struct cryptop *crp, int off, int len,
492 int (*f)(void *, const void *, u_int), void *arg)
494 return (crypto_apply_buf(&crp->crp_buf, off, len, f, arg));
498 m_contiguous_subsegment(struct mbuf *m, size_t skip, size_t len)
502 MPASS(skip <= INT_MAX);
504 m = m_getptr(m, (int)skip, &rel_off);
510 if (skip + len > m->m_len)
513 return (mtod(m, char*) + skip);
517 cuio_contiguous_segment(struct uio *uio, size_t skip, size_t len)
521 MPASS(skip <= INT_MAX);
522 idx = cuio_getptr(uio, (int)skip, &rel_off);
528 if (skip + len > uio->uio_iov[idx].iov_len)
530 return ((char *)uio->uio_iov[idx].iov_base + skip);
534 crypto_buffer_contiguous_subsegment(struct crypto_buffer *cb, size_t skip,
538 switch (cb->cb_type) {
539 case CRYPTO_BUF_MBUF:
540 return (m_contiguous_subsegment(cb->cb_mbuf, skip, len));
542 return (cuio_contiguous_segment(cb->cb_uio, skip, len));
543 case CRYPTO_BUF_CONTIG:
544 MPASS(skip + len <= cb->cb_buf_len);
545 return (cb->cb_buf + skip);
548 panic("invalid crp buf type %d", cb->cb_type);
555 crypto_contiguous_subsegment(struct cryptop *crp, size_t skip, size_t len)
557 return (crypto_buffer_contiguous_subsegment(&crp->crp_buf, skip, len));