Root/target/linux/generic-2.6/files/crypto/ocf/criov.c

1/* $OpenBSD: criov.c,v 1.9 2002/01/29 15:48:29 jason Exp $ */
2
3/*
4 * Linux port done by David McCullough <david_mccullough@securecomputing.com>
5 * Copyright (C) 2006-2007 David McCullough
6 * Copyright (C) 2004-2005 Intel Corporation.
7 * The license and original author are listed below.
8 *
9 * Copyright (c) 1999 Theo de Raadt
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 *
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34__FBSDID("$FreeBSD: src/sys/opencrypto/criov.c,v 1.5 2006/06/04 22:15:13 pjd Exp $");
35 */
36
37#ifndef AUTOCONF_INCLUDED
38#include <linux/config.h>
39#endif
40#include <linux/module.h>
41#include <linux/init.h>
42#include <linux/slab.h>
43#include <linux/uio.h>
44#include <linux/skbuff.h>
45#include <linux/kernel.h>
46#include <linux/mm.h>
47#include <asm/io.h>
48
49#include <uio.h>
50#include <cryptodev.h>
51
52/*
53 * This macro is only for avoiding code duplication, as we need to skip
54 * given number of bytes in the same way in three functions below.
55 */
56#define CUIO_SKIP() do { \
57    KASSERT(off >= 0, ("%s: off %d < 0", __func__, off)); \
58    KASSERT(len >= 0, ("%s: len %d < 0", __func__, len)); \
59    while (off > 0) { \
60        KASSERT(iol >= 0, ("%s: empty in skip", __func__)); \
61        if (off < iov->iov_len) \
62            break; \
63        off -= iov->iov_len; \
64        iol--; \
65        iov++; \
66    } \
67} while (0)
68
69void
70cuio_copydata(struct uio* uio, int off, int len, caddr_t cp)
71{
72    struct iovec *iov = uio->uio_iov;
73    int iol = uio->uio_iovcnt;
74    unsigned count;
75
76    CUIO_SKIP();
77    while (len > 0) {
78        KASSERT(iol >= 0, ("%s: empty", __func__));
79        count = min((int)(iov->iov_len - off), len);
80        memcpy(cp, ((caddr_t)iov->iov_base) + off, count);
81        len -= count;
82        cp += count;
83        off = 0;
84        iol--;
85        iov++;
86    }
87}
88
89void
90cuio_copyback(struct uio* uio, int off, int len, caddr_t cp)
91{
92    struct iovec *iov = uio->uio_iov;
93    int iol = uio->uio_iovcnt;
94    unsigned count;
95
96    CUIO_SKIP();
97    while (len > 0) {
98        KASSERT(iol >= 0, ("%s: empty", __func__));
99        count = min((int)(iov->iov_len - off), len);
100        memcpy(((caddr_t)iov->iov_base) + off, cp, count);
101        len -= count;
102        cp += count;
103        off = 0;
104        iol--;
105        iov++;
106    }
107}
108
109/*
110 * Return a pointer to iov/offset of location in iovec list.
111 */
112struct iovec *
113cuio_getptr(struct uio *uio, int loc, int *off)
114{
115    struct iovec *iov = uio->uio_iov;
116    int iol = uio->uio_iovcnt;
117
118    while (loc >= 0) {
119        /* Normal end of search */
120        if (loc < iov->iov_len) {
121                *off = loc;
122                return (iov);
123        }
124
125        loc -= iov->iov_len;
126        if (iol == 0) {
127            if (loc == 0) {
128                /* Point at the end of valid data */
129                *off = iov->iov_len;
130                return (iov);
131            } else
132                return (NULL);
133        } else {
134            iov++, iol--;
135        }
136        }
137
138    return (NULL);
139}
140
141EXPORT_SYMBOL(cuio_copyback);
142EXPORT_SYMBOL(cuio_copydata);
143EXPORT_SYMBOL(cuio_getptr);
144
145
146static void
147skb_copy_bits_back(struct sk_buff *skb, int offset, caddr_t cp, int len)
148{
149    int i;
150    if (offset < skb_headlen(skb)) {
151        memcpy(skb->data + offset, cp, min_t(int, skb_headlen(skb), len));
152        len -= skb_headlen(skb);
153        cp += skb_headlen(skb);
154    }
155    offset -= skb_headlen(skb);
156    for (i = 0; len > 0 && i < skb_shinfo(skb)->nr_frags; i++) {
157        if (offset < skb_shinfo(skb)->frags[i].size) {
158            memcpy(page_address(skb_shinfo(skb)->frags[i].page) +
159                    skb_shinfo(skb)->frags[i].page_offset,
160                    cp, min_t(int, skb_shinfo(skb)->frags[i].size, len));
161            len -= skb_shinfo(skb)->frags[i].size;
162            cp += skb_shinfo(skb)->frags[i].size;
163        }
164        offset -= skb_shinfo(skb)->frags[i].size;
165    }
166}
167
168void
169crypto_copyback(int flags, caddr_t buf, int off, int size, caddr_t in)
170{
171
172    if ((flags & CRYPTO_F_SKBUF) != 0)
173        skb_copy_bits_back((struct sk_buff *)buf, off, in, size);
174    else if ((flags & CRYPTO_F_IOV) != 0)
175        cuio_copyback((struct uio *)buf, off, size, in);
176    else
177        bcopy(in, buf + off, size);
178}
179
180void
181crypto_copydata(int flags, caddr_t buf, int off, int size, caddr_t out)
182{
183
184    if ((flags & CRYPTO_F_SKBUF) != 0)
185        skb_copy_bits((struct sk_buff *)buf, off, out, size);
186    else if ((flags & CRYPTO_F_IOV) != 0)
187        cuio_copydata((struct uio *)buf, off, size, out);
188    else
189        bcopy(buf + off, out, size);
190}
191
192int
193crypto_apply(int flags, caddr_t buf, int off, int len,
194    int (*f)(void *, void *, u_int), void *arg)
195{
196#if 0
197    int error;
198
199    if ((flags & CRYPTO_F_SKBUF) != 0)
200        error = XXXXXX((struct mbuf *)buf, off, len, f, arg);
201    else if ((flags & CRYPTO_F_IOV) != 0)
202        error = cuio_apply((struct uio *)buf, off, len, f, arg);
203    else
204        error = (*f)(arg, buf + off, len);
205    return (error);
206#else
207    KASSERT(0, ("crypto_apply not implemented!\n"));
208#endif
209    return 0;
210}
211
212EXPORT_SYMBOL(crypto_copyback);
213EXPORT_SYMBOL(crypto_copydata);
214EXPORT_SYMBOL(crypto_apply);
215
216

Archive Download this file



interactive