Annotation of src/sys/dev/cgd.c, Revision 1.97
1.97 ! riastrad 1: /* $NetBSD: cgd.c,v 1.96 2015/01/02 19:42:06 christos Exp $ */
1.1 elric 2:
3: /*-
4: * Copyright (c) 2002 The NetBSD Foundation, Inc.
5: * All rights reserved.
6: *
7: * This code is derived from software contributed to The NetBSD Foundation
8: * by Roland C. Dowdeswell.
9: *
10: * Redistribution and use in source and binary forms, with or without
11: * modification, are permitted provided that the following conditions
12: * are met:
13: * 1. Redistributions of source code must retain the above copyright
14: * notice, this list of conditions and the following disclaimer.
15: * 2. Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in the
17: * documentation and/or other materials provided with the distribution.
18: *
19: * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29: * POSSIBILITY OF SUCH DAMAGE.
30: */
31:
32: #include <sys/cdefs.h>
1.97 ! riastrad 33: __KERNEL_RCSID(0, "$NetBSD: cgd.c,v 1.96 2015/01/02 19:42:06 christos Exp $");
1.1 elric 34:
35: #include <sys/types.h>
36: #include <sys/param.h>
37: #include <sys/systm.h>
38: #include <sys/proc.h>
39: #include <sys/errno.h>
40: #include <sys/buf.h>
1.21 yamt 41: #include <sys/bufq.h>
1.1 elric 42: #include <sys/malloc.h>
1.74 jruoho 43: #include <sys/module.h>
1.1 elric 44: #include <sys/pool.h>
45: #include <sys/ioctl.h>
46: #include <sys/device.h>
47: #include <sys/disk.h>
48: #include <sys/disklabel.h>
49: #include <sys/fcntl.h>
1.71 dholland 50: #include <sys/namei.h> /* for pathbuf */
1.1 elric 51: #include <sys/vnode.h>
52: #include <sys/conf.h>
1.62 christos 53: #include <sys/syslog.h>
1.1 elric 54:
55: #include <dev/dkvar.h>
56: #include <dev/cgdvar.h>
57:
1.88 hannken 58: #include <miscfs/specfs/specdev.h> /* for v_rdev */
59:
1.1 elric 60: /* Entry Point Functions */
61:
62: void cgdattach(int);
63:
1.18 thorpej 64: static dev_type_open(cgdopen);
65: static dev_type_close(cgdclose);
66: static dev_type_read(cgdread);
67: static dev_type_write(cgdwrite);
68: static dev_type_ioctl(cgdioctl);
69: static dev_type_strategy(cgdstrategy);
70: static dev_type_dump(cgddump);
71: static dev_type_size(cgdsize);
1.1 elric 72:
73: const struct bdevsw cgd_bdevsw = {
1.84 dholland 74: .d_open = cgdopen,
75: .d_close = cgdclose,
76: .d_strategy = cgdstrategy,
77: .d_ioctl = cgdioctl,
78: .d_dump = cgddump,
79: .d_psize = cgdsize,
1.89 dholland 80: .d_discard = nodiscard,
1.84 dholland 81: .d_flag = D_DISK
1.1 elric 82: };
83:
84: const struct cdevsw cgd_cdevsw = {
1.84 dholland 85: .d_open = cgdopen,
86: .d_close = cgdclose,
87: .d_read = cgdread,
88: .d_write = cgdwrite,
89: .d_ioctl = cgdioctl,
90: .d_stop = nostop,
91: .d_tty = notty,
92: .d_poll = nopoll,
93: .d_mmap = nommap,
94: .d_kqfilter = nokqfilter,
1.90 dholland 95: .d_discard = nodiscard,
1.84 dholland 96: .d_flag = D_DISK
1.1 elric 97: };
98:
1.65 dyoung 99: static int cgd_match(device_t, cfdata_t, void *);
100: static void cgd_attach(device_t, device_t, void *);
101: static int cgd_detach(device_t, int);
102: static struct cgd_softc *cgd_spawn(int);
103: static int cgd_destroy(device_t);
104:
1.1 elric 105: /* Internal Functions */
106:
1.87 bouyer 107: static void cgdstart(struct dk_softc *);
1.1 elric 108: static void cgdiodone(struct buf *);
109:
1.32 christos 110: static int cgd_ioctl_set(struct cgd_softc *, void *, struct lwp *);
1.65 dyoung 111: static int cgd_ioctl_clr(struct cgd_softc *, struct lwp *);
1.78 christos 112: static int cgd_ioctl_get(dev_t, void *, struct lwp *);
1.27 drochner 113: static int cgdinit(struct cgd_softc *, const char *, struct vnode *,
1.32 christos 114: struct lwp *);
1.44 christos 115: static void cgd_cipher(struct cgd_softc *, void *, void *,
1.1 elric 116: size_t, daddr_t, size_t, int);
117:
118: /* Pseudo-disk Interface */
119:
120: static struct dk_intf the_dkintf = {
1.96 christos 121: DKTYPE_CGD,
1.1 elric 122: "cgd",
123: cgdopen,
124: cgdclose,
125: cgdstrategy,
126: cgdstart,
127: };
128: static struct dk_intf *di = &the_dkintf;
129:
1.29 yamt 130: static struct dkdriver cgddkdriver = {
131: .d_strategy = cgdstrategy,
132: .d_minphys = minphys,
133: };
134:
1.65 dyoung 135: CFATTACH_DECL3_NEW(cgd, sizeof(struct cgd_softc),
136: cgd_match, cgd_attach, cgd_detach, NULL, NULL, NULL, DVF_DETACH_SHUTDOWN);
137: extern struct cfdriver cgd_cd;
138:
1.1 elric 139: /* DIAGNOSTIC and DEBUG definitions */
140:
141: #if defined(CGDDEBUG) && !defined(DEBUG)
142: #define DEBUG
143: #endif
144:
145: #ifdef DEBUG
146: int cgddebug = 0;
147:
148: #define CGDB_FOLLOW 0x1
149: #define CGDB_IO 0x2
150: #define CGDB_CRYPTO 0x4
151:
152: #define IFDEBUG(x,y) if (cgddebug & (x)) y
153: #define DPRINTF(x,y) IFDEBUG(x, printf y)
154: #define DPRINTF_FOLLOW(y) DPRINTF(CGDB_FOLLOW, y)
155:
1.26 drochner 156: static void hexprint(const char *, void *, int);
1.1 elric 157:
158: #else
159: #define IFDEBUG(x,y)
160: #define DPRINTF(x,y)
161: #define DPRINTF_FOLLOW(y)
162: #endif
163:
164: #ifdef DIAGNOSTIC
1.22 perry 165: #define DIAGPANIC(x) panic x
1.1 elric 166: #define DIAGCONDPANIC(x,y) if (x) panic y
167: #else
168: #define DIAGPANIC(x)
169: #define DIAGCONDPANIC(x,y)
170: #endif
171:
172: /* Global variables */
173:
174: /* Utility Functions */
175:
176: #define CGDUNIT(x) DISKUNIT(x)
177: #define GETCGD_SOFTC(_cs, x) if (!((_cs) = getcgd_softc(x))) return ENXIO
178:
1.65 dyoung 179: /* The code */
180:
1.1 elric 181: static struct cgd_softc *
182: getcgd_softc(dev_t dev)
183: {
184: int unit = CGDUNIT(dev);
1.65 dyoung 185: struct cgd_softc *sc;
1.1 elric 186:
1.56 cegger 187: DPRINTF_FOLLOW(("getcgd_softc(0x%"PRIx64"): unit = %d\n", dev, unit));
1.65 dyoung 188:
189: sc = device_lookup_private(&cgd_cd, unit);
190: if (sc == NULL)
191: sc = cgd_spawn(unit);
192: return sc;
1.1 elric 193: }
194:
1.65 dyoung 195: static int
196: cgd_match(device_t self, cfdata_t cfdata, void *aux)
197: {
198:
199: return 1;
200: }
1.1 elric 201:
202: static void
1.65 dyoung 203: cgd_attach(device_t parent, device_t self, void *aux)
1.1 elric 204: {
1.65 dyoung 205: struct cgd_softc *sc = device_private(self);
1.1 elric 206:
1.85 skrll 207: mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_BIO);
1.77 elric 208: dk_sc_init(&sc->sc_dksc, device_xname(self));
209: sc->sc_dksc.sc_dev = self;
1.65 dyoung 210: disk_init(&sc->sc_dksc.sc_dkdev, sc->sc_dksc.sc_xname, &cgddkdriver);
1.70 joerg 211:
212: if (!pmf_device_register(self, NULL, NULL))
213: aprint_error_dev(self, "unable to register power management hooks\n");
1.65 dyoung 214: }
215:
216:
217: static int
218: cgd_detach(device_t self, int flags)
219: {
1.67 dyoung 220: int ret;
221: const int pmask = 1 << RAW_PART;
1.65 dyoung 222: struct cgd_softc *sc = device_private(self);
1.67 dyoung 223: struct dk_softc *dksc = &sc->sc_dksc;
224:
225: if (DK_BUSY(dksc, pmask))
226: return EBUSY;
1.65 dyoung 227:
1.67 dyoung 228: if ((dksc->sc_flags & DKF_INITED) != 0 &&
229: (ret = cgd_ioctl_clr(sc, curlwp)) != 0)
230: return ret;
1.65 dyoung 231:
1.67 dyoung 232: disk_destroy(&dksc->sc_dkdev);
1.86 christos 233: mutex_destroy(&sc->sc_lock);
1.65 dyoung 234:
1.67 dyoung 235: return 0;
1.1 elric 236: }
237:
238: void
239: cgdattach(int num)
240: {
1.65 dyoung 241: int error;
242:
243: error = config_cfattach_attach(cgd_cd.cd_name, &cgd_ca);
244: if (error != 0)
245: aprint_error("%s: unable to register cfattach\n",
246: cgd_cd.cd_name);
247: }
248:
249: static struct cgd_softc *
250: cgd_spawn(int unit)
251: {
252: cfdata_t cf;
253:
254: cf = malloc(sizeof(*cf), M_DEVBUF, M_WAITOK);
255: cf->cf_name = cgd_cd.cd_name;
256: cf->cf_atname = cgd_cd.cd_name;
257: cf->cf_unit = unit;
258: cf->cf_fstate = FSTATE_STAR;
259:
260: return device_private(config_attach_pseudo(cf));
261: }
262:
263: static int
264: cgd_destroy(device_t dev)
265: {
266: int error;
267: cfdata_t cf;
1.1 elric 268:
1.65 dyoung 269: cf = device_cfdata(dev);
270: error = config_detach(dev, DETACH_QUIET);
271: if (error)
272: return error;
273: free(cf, M_DEVBUF);
274: return 0;
1.1 elric 275: }
276:
1.18 thorpej 277: static int
1.32 christos 278: cgdopen(dev_t dev, int flags, int fmt, struct lwp *l)
1.1 elric 279: {
280: struct cgd_softc *cs;
281:
1.56 cegger 282: DPRINTF_FOLLOW(("cgdopen(0x%"PRIx64", %d)\n", dev, flags));
1.1 elric 283: GETCGD_SOFTC(cs, dev);
1.32 christos 284: return dk_open(di, &cs->sc_dksc, dev, flags, fmt, l);
1.1 elric 285: }
286:
1.18 thorpej 287: static int
1.32 christos 288: cgdclose(dev_t dev, int flags, int fmt, struct lwp *l)
1.1 elric 289: {
1.65 dyoung 290: int error;
1.1 elric 291: struct cgd_softc *cs;
1.65 dyoung 292: struct dk_softc *dksc;
1.1 elric 293:
1.56 cegger 294: DPRINTF_FOLLOW(("cgdclose(0x%"PRIx64", %d)\n", dev, flags));
1.1 elric 295: GETCGD_SOFTC(cs, dev);
1.65 dyoung 296: dksc = &cs->sc_dksc;
297: if ((error = dk_close(di, dksc, dev, flags, fmt, l)) != 0)
298: return error;
299:
300: if ((dksc->sc_flags & DKF_INITED) == 0) {
1.77 elric 301: if ((error = cgd_destroy(cs->sc_dksc.sc_dev)) != 0) {
302: aprint_error_dev(dksc->sc_dev,
1.65 dyoung 303: "unable to detach instance\n");
304: return error;
305: }
306: }
307: return 0;
1.1 elric 308: }
309:
1.18 thorpej 310: static void
1.1 elric 311: cgdstrategy(struct buf *bp)
312: {
313: struct cgd_softc *cs = getcgd_softc(bp->b_dev);
314:
315: DPRINTF_FOLLOW(("cgdstrategy(%p): b_bcount = %ld\n", bp,
316: (long)bp->b_bcount));
1.72 riastrad 317:
318: /*
319: * Reject unaligned writes. We can encrypt and decrypt only
320: * complete disk sectors, and we let the ciphers require their
321: * buffers to be aligned to 32-bit boundaries.
322: */
323: if (bp->b_blkno < 0 ||
324: (bp->b_bcount % DEV_BSIZE) != 0 ||
325: ((uintptr_t)bp->b_data & 3) != 0) {
326: bp->b_error = EINVAL;
327: bp->b_resid = bp->b_bcount;
328: biodone(bp);
329: return;
330: }
331:
1.1 elric 332: /* XXXrcd: Should we test for (cs != NULL)? */
333: dk_strategy(di, &cs->sc_dksc, bp);
334: return;
335: }
336:
1.18 thorpej 337: static int
1.1 elric 338: cgdsize(dev_t dev)
339: {
340: struct cgd_softc *cs = getcgd_softc(dev);
341:
1.56 cegger 342: DPRINTF_FOLLOW(("cgdsize(0x%"PRIx64")\n", dev));
1.1 elric 343: if (!cs)
344: return -1;
345: return dk_size(di, &cs->sc_dksc, dev);
346: }
347:
1.16 elric 348: /*
349: * cgd_{get,put}data are functions that deal with getting a buffer
350: * for the new encrypted data. We have a buffer per device so that
351: * we can ensure that we can always have a transaction in flight.
352: * We use this buffer first so that we have one less piece of
353: * malloc'ed data at any given point.
354: */
355:
356: static void *
357: cgd_getdata(struct dk_softc *dksc, unsigned long size)
358: {
1.77 elric 359: struct cgd_softc *cs = (struct cgd_softc *)dksc;
1.44 christos 360: void * data = NULL;
1.16 elric 361:
1.85 skrll 362: mutex_enter(&cs->sc_lock);
1.16 elric 363: if (cs->sc_data_used == 0) {
364: cs->sc_data_used = 1;
365: data = cs->sc_data;
366: }
1.85 skrll 367: mutex_exit(&cs->sc_lock);
1.16 elric 368:
369: if (data)
370: return data;
371:
372: return malloc(size, M_DEVBUF, M_NOWAIT);
373: }
374:
1.1 elric 375: static void
1.44 christos 376: cgd_putdata(struct dk_softc *dksc, void *data)
1.16 elric 377: {
1.77 elric 378: struct cgd_softc *cs = (struct cgd_softc *)dksc;
1.16 elric 379:
380: if (data == cs->sc_data) {
1.85 skrll 381: mutex_enter(&cs->sc_lock);
1.16 elric 382: cs->sc_data_used = 0;
1.85 skrll 383: mutex_exit(&cs->sc_lock);
1.16 elric 384: } else {
385: free(data, M_DEVBUF);
386: }
387: }
388:
1.87 bouyer 389: static void
390: cgdstart(struct dk_softc *dksc)
1.1 elric 391: {
1.77 elric 392: struct cgd_softc *cs = (struct cgd_softc *)dksc;
1.87 bouyer 393: struct buf *bp, *nbp;
394: #ifdef DIAGNOSTIC
395: struct buf *qbp;
396: #endif
1.44 christos 397: void * addr;
398: void * newaddr;
1.1 elric 399: daddr_t bn;
1.49 ad 400: struct vnode *vp;
1.1 elric 401:
1.87 bouyer 402: while ((bp = bufq_peek(dksc->sc_bufq)) != NULL) {
1.1 elric 403:
1.87 bouyer 404: DPRINTF_FOLLOW(("cgdstart(%p, %p)\n", dksc, bp));
405: disk_busy(&dksc->sc_dkdev);
1.22 perry 406:
1.87 bouyer 407: bn = bp->b_rawblkno;
1.16 elric 408:
1.87 bouyer 409: /*
410: * We attempt to allocate all of our resources up front, so that
411: * we can fail quickly if they are unavailable.
412: */
413: nbp = getiobuf(cs->sc_tvn, false);
414: if (nbp == NULL) {
1.16 elric 415: disk_unbusy(&dksc->sc_dkdev, 0, (bp->b_flags & B_READ));
1.87 bouyer 416: break;
1.16 elric 417: }
1.1 elric 418:
1.87 bouyer 419: /*
420: * If we are writing, then we need to encrypt the outgoing
421: * block into a new block of memory.
422: */
423: newaddr = addr = bp->b_data;
424: if ((bp->b_flags & B_READ) == 0) {
425: newaddr = cgd_getdata(dksc, bp->b_bcount);
426: if (!newaddr) {
427: putiobuf(nbp);
428: disk_unbusy(&dksc->sc_dkdev, 0, (bp->b_flags & B_READ));
429: break;
430: }
431: cgd_cipher(cs, newaddr, addr, bp->b_bcount, bn,
432: DEV_BSIZE, CGD_CIPHER_ENCRYPT);
433: }
434: /* we now have all needed resources to process this buf */
435: #ifdef DIAGNOSTIC
436: qbp = bufq_get(dksc->sc_bufq);
437: KASSERT(bp == qbp);
438: #else
439: (void)bufq_get(dksc->sc_bufq);
440: #endif
441: nbp->b_data = newaddr;
442: nbp->b_flags = bp->b_flags;
443: nbp->b_oflags = bp->b_oflags;
444: nbp->b_cflags = bp->b_cflags;
445: nbp->b_iodone = cgdiodone;
446: nbp->b_proc = bp->b_proc;
447: nbp->b_blkno = bn;
448: nbp->b_bcount = bp->b_bcount;
449: nbp->b_private = bp;
450:
451: BIO_COPYPRIO(nbp, bp);
452:
453: if ((nbp->b_flags & B_READ) == 0) {
454: vp = nbp->b_vp;
455: mutex_enter(vp->v_interlock);
456: vp->v_numoutput++;
457: mutex_exit(vp->v_interlock);
458: }
459: VOP_STRATEGY(cs->sc_tvn, nbp);
1.17 dbj 460: }
1.1 elric 461: }
462:
1.18 thorpej 463: static void
1.17 dbj 464: cgdiodone(struct buf *nbp)
1.1 elric 465: {
1.17 dbj 466: struct buf *obp = nbp->b_private;
467: struct cgd_softc *cs = getcgd_softc(obp->b_dev);
1.1 elric 468: struct dk_softc *dksc = &cs->sc_dksc;
1.69 bouyer 469: int s;
1.22 perry 470:
1.17 dbj 471: KDASSERT(cs);
1.1 elric 472:
1.17 dbj 473: DPRINTF_FOLLOW(("cgdiodone(%p)\n", nbp));
1.20 yamt 474: DPRINTF(CGDB_IO, ("cgdiodone: bp %p bcount %d resid %d\n",
1.1 elric 475: obp, obp->b_bcount, obp->b_resid));
1.56 cegger 476: DPRINTF(CGDB_IO, (" dev 0x%"PRIx64", nbp %p bn %" PRId64 " addr %p bcnt %d\n",
1.17 dbj 477: nbp->b_dev, nbp, nbp->b_blkno, nbp->b_data,
478: nbp->b_bcount));
1.46 ad 479: if (nbp->b_error != 0) {
480: obp->b_error = nbp->b_error;
1.62 christos 481: DPRINTF(CGDB_IO, ("%s: error %d\n", dksc->sc_xname,
482: obp->b_error));
1.1 elric 483: }
484:
1.16 elric 485: /* Perform the decryption if we are reading.
1.1 elric 486: *
487: * Note: use the blocknumber from nbp, since it is what
488: * we used to encrypt the blocks.
489: */
490:
1.16 elric 491: if (nbp->b_flags & B_READ)
1.1 elric 492: cgd_cipher(cs, obp->b_data, obp->b_data, obp->b_bcount,
493: nbp->b_blkno, DEV_BSIZE, CGD_CIPHER_DECRYPT);
494:
1.16 elric 495: /* If we allocated memory, free it now... */
1.1 elric 496: if (nbp->b_data != obp->b_data)
1.16 elric 497: cgd_putdata(dksc, nbp->b_data);
1.1 elric 498:
1.33 yamt 499: putiobuf(nbp);
1.1 elric 500:
501: /* Request is complete for whatever reason */
502: obp->b_resid = 0;
1.46 ad 503: if (obp->b_error != 0)
1.1 elric 504: obp->b_resid = obp->b_bcount;
1.69 bouyer 505: s = splbio();
1.5 mrg 506: disk_unbusy(&dksc->sc_dkdev, obp->b_bcount - obp->b_resid,
507: (obp->b_flags & B_READ));
1.1 elric 508: biodone(obp);
1.87 bouyer 509: cgdstart(dksc);
1.69 bouyer 510: splx(s);
1.1 elric 511: }
512:
513: /* XXX: we should probably put these into dksubr.c, mostly */
1.18 thorpej 514: static int
1.40 christos 515: cgdread(dev_t dev, struct uio *uio, int flags)
1.1 elric 516: {
517: struct cgd_softc *cs;
518: struct dk_softc *dksc;
519:
1.56 cegger 520: DPRINTF_FOLLOW(("cgdread(0x%llx, %p, %d)\n",
521: (unsigned long long)dev, uio, flags));
1.1 elric 522: GETCGD_SOFTC(cs, dev);
523: dksc = &cs->sc_dksc;
524: if ((dksc->sc_flags & DKF_INITED) == 0)
525: return ENXIO;
526: return physio(cgdstrategy, NULL, dev, B_READ, minphys, uio);
527: }
528:
529: /* XXX: we should probably put these into dksubr.c, mostly */
1.18 thorpej 530: static int
1.40 christos 531: cgdwrite(dev_t dev, struct uio *uio, int flags)
1.1 elric 532: {
533: struct cgd_softc *cs;
534: struct dk_softc *dksc;
535:
1.56 cegger 536: DPRINTF_FOLLOW(("cgdwrite(0x%"PRIx64", %p, %d)\n", dev, uio, flags));
1.1 elric 537: GETCGD_SOFTC(cs, dev);
538: dksc = &cs->sc_dksc;
539: if ((dksc->sc_flags & DKF_INITED) == 0)
540: return ENXIO;
541: return physio(cgdstrategy, NULL, dev, B_WRITE, minphys, uio);
542: }
543:
1.18 thorpej 544: static int
1.44 christos 545: cgdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
1.1 elric 546: {
547: struct cgd_softc *cs;
548: struct dk_softc *dksc;
549: int part = DISKPART(dev);
550: int pmask = 1 << part;
551:
1.56 cegger 552: DPRINTF_FOLLOW(("cgdioctl(0x%"PRIx64", %ld, %p, %d, %p)\n",
1.32 christos 553: dev, cmd, data, flag, l));
1.78 christos 554:
1.1 elric 555: switch (cmd) {
1.93 christos 556: case CGDIOCGET:
557: return cgd_ioctl_get(dev, data, l);
1.1 elric 558: case CGDIOCSET:
559: case CGDIOCCLR:
560: if ((flag & FWRITE) == 0)
561: return EBADF;
1.78 christos 562: /* FALLTHROUGH */
563: default:
564: GETCGD_SOFTC(cs, dev);
565: dksc = &cs->sc_dksc;
566: break;
1.1 elric 567: }
568:
569: switch (cmd) {
570: case CGDIOCSET:
571: if (dksc->sc_flags & DKF_INITED)
1.68 dyoung 572: return EBUSY;
573: return cgd_ioctl_set(cs, data, l);
1.1 elric 574: case CGDIOCCLR:
1.65 dyoung 575: if (DK_BUSY(&cs->sc_dksc, pmask))
1.68 dyoung 576: return EBUSY;
577: return cgd_ioctl_clr(cs, l);
1.57 apb 578: case DIOCCACHESYNC:
579: /*
580: * XXX Do we really need to care about having a writable
581: * file descriptor here?
582: */
583: if ((flag & FWRITE) == 0)
584: return (EBADF);
585:
586: /*
587: * We pass this call down to the underlying disk.
588: */
1.68 dyoung 589: return VOP_IOCTL(cs->sc_tvn, cmd, data, flag, l->l_cred);
1.1 elric 590: default:
1.68 dyoung 591: return dk_ioctl(di, dksc, dev, cmd, data, flag, l);
1.93 christos 592: case CGDIOCGET:
593: KASSERT(0);
594: return EINVAL;
1.1 elric 595: }
596: }
597:
1.18 thorpej 598: static int
1.44 christos 599: cgddump(dev_t dev, daddr_t blkno, void *va, size_t size)
1.1 elric 600: {
601: struct cgd_softc *cs;
602:
1.56 cegger 603: DPRINTF_FOLLOW(("cgddump(0x%"PRIx64", %" PRId64 ", %p, %lu)\n",
604: dev, blkno, va, (unsigned long)size));
1.1 elric 605: GETCGD_SOFTC(cs, dev);
606: return dk_dump(di, &cs->sc_dksc, dev, blkno, va, size);
607: }
608:
609: /*
610: * XXXrcd:
611: * for now we hardcode the maximum key length.
612: */
613: #define MAX_KEYSIZE 1024
614:
1.53 christos 615: static const struct {
616: const char *n;
617: int v;
618: int d;
619: } encblkno[] = {
620: { "encblkno", CGD_CIPHER_CBC_ENCBLKNO8, 1 },
621: { "encblkno8", CGD_CIPHER_CBC_ENCBLKNO8, 1 },
622: { "encblkno1", CGD_CIPHER_CBC_ENCBLKNO1, 8 },
623: };
624:
1.1 elric 625: /* ARGSUSED */
626: static int
1.32 christos 627: cgd_ioctl_set(struct cgd_softc *cs, void *data, struct lwp *l)
1.1 elric 628: {
629: struct cgd_ioctl *ci = data;
630: struct vnode *vp;
631: int ret;
1.53 christos 632: size_t i;
1.43 cbiere 633: size_t keybytes; /* key length in bytes */
1.27 drochner 634: const char *cp;
1.71 dholland 635: struct pathbuf *pb;
1.36 christos 636: char *inbuf;
1.80 christos 637: struct dk_softc *dksc = &cs->sc_dksc;
1.1 elric 638:
639: cp = ci->ci_disk;
1.71 dholland 640:
641: ret = pathbuf_copyin(ci->ci_disk, &pb);
642: if (ret != 0) {
643: return ret;
644: }
645: ret = dk_lookup(pb, l, &vp);
646: pathbuf_destroy(pb);
647: if (ret != 0) {
1.1 elric 648: return ret;
1.71 dholland 649: }
1.1 elric 650:
1.36 christos 651: inbuf = malloc(MAX_KEYSIZE, M_TEMP, M_WAITOK);
652:
1.32 christos 653: if ((ret = cgdinit(cs, cp, vp, l)) != 0)
1.1 elric 654: goto bail;
655:
1.36 christos 656: (void)memset(inbuf, 0, MAX_KEYSIZE);
1.1 elric 657: ret = copyinstr(ci->ci_alg, inbuf, 256, NULL);
658: if (ret)
659: goto bail;
660: cs->sc_cfuncs = cryptfuncs_find(inbuf);
661: if (!cs->sc_cfuncs) {
662: ret = EINVAL;
663: goto bail;
664: }
665:
1.43 cbiere 666: (void)memset(inbuf, 0, MAX_KEYSIZE);
1.36 christos 667: ret = copyinstr(ci->ci_ivmethod, inbuf, MAX_KEYSIZE, NULL);
1.1 elric 668: if (ret)
669: goto bail;
1.53 christos 670:
671: for (i = 0; i < __arraycount(encblkno); i++)
672: if (strcmp(encblkno[i].n, inbuf) == 0)
673: break;
674:
675: if (i == __arraycount(encblkno)) {
1.1 elric 676: ret = EINVAL;
677: goto bail;
678: }
679:
1.15 dan 680: keybytes = ci->ci_keylen / 8 + 1;
681: if (keybytes > MAX_KEYSIZE) {
1.1 elric 682: ret = EINVAL;
683: goto bail;
684: }
1.53 christos 685:
1.36 christos 686: (void)memset(inbuf, 0, MAX_KEYSIZE);
1.15 dan 687: ret = copyin(ci->ci_key, inbuf, keybytes);
1.1 elric 688: if (ret)
689: goto bail;
690:
691: cs->sc_cdata.cf_blocksize = ci->ci_blocksize;
1.53 christos 692: cs->sc_cdata.cf_mode = encblkno[i].v;
1.78 christos 693: cs->sc_cdata.cf_keylen = ci->ci_keylen;
1.1 elric 694: cs->sc_cdata.cf_priv = cs->sc_cfuncs->cf_init(ci->ci_keylen, inbuf,
695: &cs->sc_cdata.cf_blocksize);
1.62 christos 696: if (cs->sc_cdata.cf_blocksize > CGD_MAXBLOCKSIZE) {
697: log(LOG_WARNING, "cgd: Disallowed cipher with blocksize %zu > %u\n",
1.63 christos 698: cs->sc_cdata.cf_blocksize, CGD_MAXBLOCKSIZE);
1.62 christos 699: cs->sc_cdata.cf_priv = NULL;
700: }
1.78 christos 701:
1.53 christos 702: /*
703: * The blocksize is supposed to be in bytes. Unfortunately originally
704: * it was expressed in bits. For compatibility we maintain encblkno
705: * and encblkno8.
706: */
707: cs->sc_cdata.cf_blocksize /= encblkno[i].d;
1.97 ! riastrad 708: (void)explicit_memset(inbuf, 0, MAX_KEYSIZE);
1.1 elric 709: if (!cs->sc_cdata.cf_priv) {
710: ret = EINVAL; /* XXX is this the right error? */
711: goto bail;
712: }
1.36 christos 713: free(inbuf, M_TEMP);
1.1 elric 714:
1.80 christos 715: bufq_alloc(&dksc->sc_bufq, "fcfs", 0);
1.16 elric 716:
717: cs->sc_data = malloc(MAXPHYS, M_DEVBUF, M_WAITOK);
718: cs->sc_data_used = 0;
719:
1.80 christos 720: dksc->sc_flags |= DKF_INITED;
1.1 elric 721:
1.80 christos 722: disk_set_info(dksc->sc_dev, &dksc->sc_dkdev, NULL);
1.77 elric 723:
1.1 elric 724: /* Attach the disk. */
1.80 christos 725: disk_attach(&dksc->sc_dkdev);
1.1 elric 726:
727: /* Try and read the disklabel. */
1.80 christos 728: dk_getdisklabel(di, dksc, 0 /* XXX ? (cause of PR 41704) */);
1.1 elric 729:
1.29 yamt 730: /* Discover wedges on this disk. */
1.80 christos 731: dkwedge_discover(&dksc->sc_dkdev);
1.29 yamt 732:
1.1 elric 733: return 0;
734:
735: bail:
1.36 christos 736: free(inbuf, M_TEMP);
1.51 ad 737: (void)vn_close(vp, FREAD|FWRITE, l->l_cred);
1.1 elric 738: return ret;
739: }
740:
741: /* ARGSUSED */
742: static int
1.65 dyoung 743: cgd_ioctl_clr(struct cgd_softc *cs, struct lwp *l)
1.1 elric 744: {
1.16 elric 745: int s;
1.80 christos 746: struct dk_softc *dksc = &cs->sc_dksc;
1.65 dyoung 747:
748: if ((dksc->sc_flags & DKF_INITED) == 0)
749: return ENXIO;
1.16 elric 750:
1.29 yamt 751: /* Delete all of our wedges. */
1.80 christos 752: dkwedge_delall(&dksc->sc_dkdev);
1.29 yamt 753:
1.16 elric 754: /* Kill off any queued buffers. */
755: s = splbio();
1.80 christos 756: bufq_drain(dksc->sc_bufq);
1.16 elric 757: splx(s);
1.80 christos 758: bufq_free(dksc->sc_bufq);
1.1 elric 759:
1.51 ad 760: (void)vn_close(cs->sc_tvn, FREAD|FWRITE, l->l_cred);
1.1 elric 761: cs->sc_cfuncs->cf_destroy(cs->sc_cdata.cf_priv);
762: free(cs->sc_tpath, M_DEVBUF);
1.16 elric 763: free(cs->sc_data, M_DEVBUF);
764: cs->sc_data_used = 0;
1.80 christos 765: dksc->sc_flags &= ~DKF_INITED;
766: disk_detach(&dksc->sc_dkdev);
1.1 elric 767:
768: return 0;
769: }
770:
771: static int
1.78 christos 772: cgd_ioctl_get(dev_t dev, void *data, struct lwp *l)
773: {
1.81 martin 774: struct cgd_softc *cs = getcgd_softc(dev);
1.78 christos 775: struct cgd_user *cgu;
776: int unit;
1.80 christos 777: struct dk_softc *dksc = &cs->sc_dksc;
1.78 christos 778:
779: unit = CGDUNIT(dev);
780: cgu = (struct cgd_user *)data;
781:
782: DPRINTF_FOLLOW(("cgd_ioctl_get(0x%"PRIx64", %d, %p, %p)\n",
783: dev, unit, data, l));
784:
785: if (cgu->cgu_unit == -1)
786: cgu->cgu_unit = unit;
787:
788: if (cgu->cgu_unit < 0)
789: return EINVAL; /* XXX: should this be ENXIO? */
790:
791: cs = device_lookup_private(&cgd_cd, unit);
1.80 christos 792: if (cs == NULL || (dksc->sc_flags & DKF_INITED) == 0) {
1.78 christos 793: cgu->cgu_dev = 0;
794: cgu->cgu_alg[0] = '\0';
795: cgu->cgu_blocksize = 0;
796: cgu->cgu_mode = 0;
797: cgu->cgu_keylen = 0;
798: }
799: else {
800: cgu->cgu_dev = cs->sc_tdev;
801: strlcpy(cgu->cgu_alg, cs->sc_cfuncs->cf_name,
802: sizeof(cgu->cgu_alg));
803: cgu->cgu_blocksize = cs->sc_cdata.cf_blocksize;
804: cgu->cgu_mode = cs->sc_cdata.cf_mode;
805: cgu->cgu_keylen = cs->sc_cdata.cf_keylen;
806: }
807: return 0;
808: }
809:
810: static int
1.27 drochner 811: cgdinit(struct cgd_softc *cs, const char *cpath, struct vnode *vp,
1.32 christos 812: struct lwp *l)
1.1 elric 813: {
1.80 christos 814: struct disk_geom *dg;
1.1 elric 815: int ret;
1.36 christos 816: char *tmppath;
1.76 christos 817: uint64_t psize;
818: unsigned secsize;
1.80 christos 819: struct dk_softc *dksc = &cs->sc_dksc;
1.1 elric 820:
821: cs->sc_tvn = vp;
1.36 christos 822: cs->sc_tpath = NULL;
1.1 elric 823:
1.36 christos 824: tmppath = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1.1 elric 825: ret = copyinstr(cpath, tmppath, MAXPATHLEN, &cs->sc_tpathlen);
826: if (ret)
827: goto bail;
828: cs->sc_tpath = malloc(cs->sc_tpathlen, M_DEVBUF, M_WAITOK);
829: memcpy(cs->sc_tpath, tmppath, cs->sc_tpathlen);
830:
1.88 hannken 831: cs->sc_tdev = vp->v_rdev;
1.1 elric 832:
1.76 christos 833: if ((ret = getdisksize(vp, &psize, &secsize)) != 0)
1.1 elric 834: goto bail;
835:
1.76 christos 836: if (psize == 0) {
1.1 elric 837: ret = ENODEV;
838: goto bail;
839: }
840:
841: /*
842: * XXX here we should probe the underlying device. If we
843: * are accessing a partition of type RAW_PART, then
844: * we should populate our initial geometry with the
845: * geometry that we discover from the device.
846: */
1.80 christos 847: dg = &dksc->sc_dkdev.dk_geom;
848: memset(dg, 0, sizeof(*dg));
849: dg->dg_secperunit = psize;
850: // XXX: Inherit?
851: dg->dg_secsize = DEV_BSIZE;
852: dg->dg_ntracks = 1;
853: dg->dg_nsectors = 1024 * (1024 / dg->dg_secsize);
854: dg->dg_ncylinders = dg->dg_secperunit / dg->dg_nsectors;
1.1 elric 855:
856: bail:
1.36 christos 857: free(tmppath, M_TEMP);
1.1 elric 858: if (ret && cs->sc_tpath)
859: free(cs->sc_tpath, M_DEVBUF);
860: return ret;
861: }
862:
863: /*
864: * Our generic cipher entry point. This takes care of the
865: * IV mode and passes off the work to the specific cipher.
866: * We implement here the IV method ``encrypted block
867: * number''.
1.22 perry 868: *
1.1 elric 869: * For the encryption case, we accomplish this by setting
870: * up a struct uio where the first iovec of the source is
871: * the blocknumber and the first iovec of the dest is a
872: * sink. We then call the cipher with an IV of zero, and
873: * the right thing happens.
1.22 perry 874: *
1.1 elric 875: * For the decryption case, we use the same basic mechanism
876: * for symmetry, but we encrypt the block number in the
877: * first iovec.
878: *
879: * We mainly do this to avoid requiring the definition of
880: * an ECB mode.
881: *
882: * XXXrcd: for now we rely on our own crypto framework defined
883: * in dev/cgd_crypto.c. This will change when we
884: * get a generic kernel crypto framework.
885: */
886:
887: static void
1.25 xtraeme 888: blkno2blkno_buf(char *sbuf, daddr_t blkno)
1.1 elric 889: {
890: int i;
891:
892: /* Set up the blkno in blkno_buf, here we do not care much
893: * about the final layout of the information as long as we
894: * can guarantee that each sector will have a different IV
895: * and that the endianness of the machine will not affect
896: * the representation that we have chosen.
897: *
898: * We choose this representation, because it does not rely
899: * on the size of buf (which is the blocksize of the cipher),
900: * but allows daddr_t to grow without breaking existing
901: * disks.
902: *
903: * Note that blkno2blkno_buf does not take a size as input,
904: * and hence must be called on a pre-zeroed buffer of length
905: * greater than or equal to sizeof(daddr_t).
906: */
907: for (i=0; i < sizeof(daddr_t); i++) {
1.25 xtraeme 908: *sbuf++ = blkno & 0xff;
1.1 elric 909: blkno >>= 8;
910: }
911: }
912:
913: static void
1.44 christos 914: cgd_cipher(struct cgd_softc *cs, void *dstv, void *srcv,
915: size_t len, daddr_t blkno, size_t secsize, int dir)
1.1 elric 916: {
1.44 christos 917: char *dst = dstv;
918: char *src = srcv;
1.1 elric 919: cfunc_cipher *cipher = cs->sc_cfuncs->cf_cipher;
920: struct uio dstuio;
921: struct uio srcuio;
922: struct iovec dstiov[2];
923: struct iovec srciov[2];
1.42 christos 924: size_t blocksize = cs->sc_cdata.cf_blocksize;
1.62 christos 925: char sink[CGD_MAXBLOCKSIZE];
926: char zero_iv[CGD_MAXBLOCKSIZE];
927: char blkno_buf[CGD_MAXBLOCKSIZE];
1.1 elric 928:
929: DPRINTF_FOLLOW(("cgd_cipher() dir=%d\n", dir));
930:
1.22 perry 931: DIAGCONDPANIC(len % blocksize != 0,
1.1 elric 932: ("cgd_cipher: len %% blocksize != 0"));
933:
934: /* ensure that sizeof(daddr_t) <= blocksize (for encblkno IVing) */
935: DIAGCONDPANIC(sizeof(daddr_t) > blocksize,
936: ("cgd_cipher: sizeof(daddr_t) > blocksize"));
937:
1.64 christos 938: memset(zero_iv, 0x0, blocksize);
1.1 elric 939:
940: dstuio.uio_iov = dstiov;
941: dstuio.uio_iovcnt = 2;
942:
943: srcuio.uio_iov = srciov;
944: srcuio.uio_iovcnt = 2;
945:
946: dstiov[0].iov_base = sink;
947: dstiov[0].iov_len = blocksize;
948: srciov[0].iov_base = blkno_buf;
949: srciov[0].iov_len = blocksize;
950: dstiov[1].iov_len = secsize;
951: srciov[1].iov_len = secsize;
952:
953: for (; len > 0; len -= secsize) {
954: dstiov[1].iov_base = dst;
955: srciov[1].iov_base = src;
956:
1.64 christos 957: memset(blkno_buf, 0x0, blocksize);
1.1 elric 958: blkno2blkno_buf(blkno_buf, blkno);
959: if (dir == CGD_CIPHER_DECRYPT) {
960: dstuio.uio_iovcnt = 1;
961: srcuio.uio_iovcnt = 1;
962: IFDEBUG(CGDB_CRYPTO, hexprint("step 0: blkno_buf",
1.64 christos 963: blkno_buf, blocksize));
1.1 elric 964: cipher(cs->sc_cdata.cf_priv, &dstuio, &srcuio,
965: zero_iv, CGD_CIPHER_ENCRYPT);
966: memcpy(blkno_buf, sink, blocksize);
967: dstuio.uio_iovcnt = 2;
968: srcuio.uio_iovcnt = 2;
969: }
970:
971: IFDEBUG(CGDB_CRYPTO, hexprint("step 1: blkno_buf",
1.64 christos 972: blkno_buf, blocksize));
1.1 elric 973: cipher(cs->sc_cdata.cf_priv, &dstuio, &srcuio, zero_iv, dir);
974: IFDEBUG(CGDB_CRYPTO, hexprint("step 2: sink",
1.64 christos 975: sink, blocksize));
1.1 elric 976:
977: dst += secsize;
978: src += secsize;
979: blkno++;
980: }
981: }
982:
983: #ifdef DEBUG
984: static void
1.26 drochner 985: hexprint(const char *start, void *buf, int len)
1.1 elric 986: {
987: char *c = buf;
988:
989: DIAGCONDPANIC(len < 0, ("hexprint: called with len < 0"));
990: printf("%s: len=%06d 0x", start, len);
991: while (len--)
1.43 cbiere 992: printf("%02x", (unsigned char) *c++);
1.1 elric 993: }
994: #endif
1.58 haad 995:
1.83 pgoyette 996: MODULE(MODULE_CLASS_DRIVER, cgd, "dk_subr");
1.74 jruoho 997:
1.58 haad 998: #ifdef _MODULE
1.66 dyoung 999: CFDRIVER_DECL(cgd, DV_DISK, NULL);
1.74 jruoho 1000: #endif
1.58 haad 1001:
1002: static int
1003: cgd_modcmd(modcmd_t cmd, void *arg)
1004: {
1.82 martin 1005: int error = 0;
1.74 jruoho 1006:
1.82 martin 1007: #ifdef _MODULE
1.91 justin 1008: devmajor_t bmajor = -1, cmajor = -1;
1.82 martin 1009: #endif
1.74 jruoho 1010:
1.58 haad 1011: switch (cmd) {
1012: case MODULE_CMD_INIT:
1.74 jruoho 1013: #ifdef _MODULE
1.66 dyoung 1014: error = config_cfdriver_attach(&cgd_cd);
1015: if (error)
1016: break;
1017:
1018: error = config_cfattach_attach(cgd_cd.cd_name, &cgd_ca);
1019: if (error) {
1020: config_cfdriver_detach(&cgd_cd);
1021: aprint_error("%s: unable to register cfattach\n",
1022: cgd_cd.cd_name);
1023: break;
1024: }
1.74 jruoho 1025:
1.66 dyoung 1026: error = devsw_attach("cgd", &cgd_bdevsw, &bmajor,
1.58 haad 1027: &cgd_cdevsw, &cmajor);
1.66 dyoung 1028: if (error) {
1029: config_cfattach_detach(cgd_cd.cd_name, &cgd_ca);
1030: config_cfdriver_detach(&cgd_cd);
1031: break;
1032: }
1.74 jruoho 1033: #endif
1.58 haad 1034: break;
1035:
1036: case MODULE_CMD_FINI:
1.74 jruoho 1037: #ifdef _MODULE
1.66 dyoung 1038: error = config_cfattach_detach(cgd_cd.cd_name, &cgd_ca);
1039: if (error)
1040: break;
1041: config_cfdriver_detach(&cgd_cd);
1042: devsw_detach(&cgd_bdevsw, &cgd_cdevsw);
1.74 jruoho 1043: #endif
1.58 haad 1044: break;
1045:
1046: case MODULE_CMD_STAT:
1047: return ENOTTY;
1048:
1049: default:
1050: return ENOTTY;
1051: }
1052:
1053: return error;
1054: }
CVSweb <webmaster@jp.NetBSD.org>