cgd.c revision 1.28 1 /* $NetBSD: cgd.c,v 1.28 2005/08/20 12:01:04 yamt Exp $ */
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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: cgd.c,v 1.28 2005/08/20 12:01:04 yamt Exp $");
41
42 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/proc.h>
46 #include <sys/errno.h>
47 #include <sys/buf.h>
48 #include <sys/bufq.h>
49 #include <sys/malloc.h>
50 #include <sys/pool.h>
51 #include <sys/ioctl.h>
52 #include <sys/device.h>
53 #include <sys/disk.h>
54 #include <sys/disklabel.h>
55 #include <sys/fcntl.h>
56 #include <sys/vnode.h>
57 #include <sys/lock.h>
58 #include <sys/conf.h>
59
60 #include <dev/dkvar.h>
61 #include <dev/cgdvar.h>
62
63 /* Entry Point Functions */
64
65 void cgdattach(int);
66
67 static dev_type_open(cgdopen);
68 static dev_type_close(cgdclose);
69 static dev_type_read(cgdread);
70 static dev_type_write(cgdwrite);
71 static dev_type_ioctl(cgdioctl);
72 static dev_type_strategy(cgdstrategy);
73 static dev_type_dump(cgddump);
74 static dev_type_size(cgdsize);
75
76 const struct bdevsw cgd_bdevsw = {
77 cgdopen, cgdclose, cgdstrategy, cgdioctl,
78 cgddump, cgdsize, D_DISK
79 };
80
81 const struct cdevsw cgd_cdevsw = {
82 cgdopen, cgdclose, cgdread, cgdwrite, cgdioctl,
83 nostop, notty, nopoll, nommap, nokqfilter, D_DISK
84 };
85
86 /* Internal Functions */
87
88 static int cgdstart(struct dk_softc *, struct buf *);
89 static void cgdiodone(struct buf *);
90
91 static int cgd_ioctl_set(struct cgd_softc *, void *, struct proc *);
92 static int cgd_ioctl_clr(struct cgd_softc *, void *, struct proc *);
93 static int cgdinit(struct cgd_softc *, const char *, struct vnode *,
94 struct proc *);
95 static void cgd_cipher(struct cgd_softc *, caddr_t, caddr_t,
96 size_t, daddr_t, size_t, int);
97
98 /* Pseudo-disk Interface */
99
100 static struct dk_intf the_dkintf = {
101 DTYPE_CGD,
102 "cgd",
103 cgdopen,
104 cgdclose,
105 cgdstrategy,
106 cgdstart,
107 };
108 static struct dk_intf *di = &the_dkintf;
109
110 /* DIAGNOSTIC and DEBUG definitions */
111
112 #if defined(CGDDEBUG) && !defined(DEBUG)
113 #define DEBUG
114 #endif
115
116 #ifdef DEBUG
117 int cgddebug = 0;
118
119 #define CGDB_FOLLOW 0x1
120 #define CGDB_IO 0x2
121 #define CGDB_CRYPTO 0x4
122
123 #define IFDEBUG(x,y) if (cgddebug & (x)) y
124 #define DPRINTF(x,y) IFDEBUG(x, printf y)
125 #define DPRINTF_FOLLOW(y) DPRINTF(CGDB_FOLLOW, y)
126
127 static void hexprint(const char *, void *, int);
128
129 #else
130 #define IFDEBUG(x,y)
131 #define DPRINTF(x,y)
132 #define DPRINTF_FOLLOW(y)
133 #endif
134
135 #ifdef DIAGNOSTIC
136 #define DIAGPANIC(x) panic x
137 #define DIAGCONDPANIC(x,y) if (x) panic y
138 #else
139 #define DIAGPANIC(x)
140 #define DIAGCONDPANIC(x,y)
141 #endif
142
143 /* Global variables */
144
145 struct cgd_softc *cgd_softc;
146 int numcgd = 0;
147
148 /* Utility Functions */
149
150 #define CGDUNIT(x) DISKUNIT(x)
151 #define GETCGD_SOFTC(_cs, x) if (!((_cs) = getcgd_softc(x))) return ENXIO
152
153 static struct cgd_softc *
154 getcgd_softc(dev_t dev)
155 {
156 int unit = CGDUNIT(dev);
157
158 DPRINTF_FOLLOW(("getcgd_softc(0x%x): unit = %d\n", dev, unit));
159 if (unit >= numcgd)
160 return NULL;
161 return &cgd_softc[unit];
162 }
163
164 /* The code */
165
166 static void
167 cgdsoftc_init(struct cgd_softc *cs, int num)
168 {
169 char sbuf[DK_XNAME_SIZE];
170
171 memset(cs, 0x0, sizeof(*cs));
172 snprintf(sbuf, DK_XNAME_SIZE, "cgd%d", num);
173 simple_lock_init(&cs->sc_slock);
174 dk_sc_init(&cs->sc_dksc, cs, sbuf);
175 pseudo_disk_init(&cs->sc_dksc.sc_dkdev);
176 }
177
178 void
179 cgdattach(int num)
180 {
181 int i;
182
183 DPRINTF_FOLLOW(("cgdattach(%d)\n", num));
184 if (num <= 0) {
185 DIAGPANIC(("cgdattach: count <= 0"));
186 return;
187 }
188
189 cgd_softc = (void *)malloc(num * sizeof(*cgd_softc), M_DEVBUF, M_NOWAIT);
190 if (!cgd_softc) {
191 printf("WARNING: unable to malloc(9) memory for crypt disks\n");
192 DIAGPANIC(("cgdattach: cannot malloc(9) enough memory"));
193 return;
194 }
195
196 numcgd = num;
197 for (i=0; i<num; i++)
198 cgdsoftc_init(&cgd_softc[i], i);
199 }
200
201 static int
202 cgdopen(dev_t dev, int flags, int fmt, struct proc *p)
203 {
204 struct cgd_softc *cs;
205
206 DPRINTF_FOLLOW(("cgdopen(%d, %d)\n", dev, flags));
207 GETCGD_SOFTC(cs, dev);
208 return dk_open(di, &cs->sc_dksc, dev, flags, fmt, p);
209 }
210
211 static int
212 cgdclose(dev_t dev, int flags, int fmt, struct proc *p)
213 {
214 struct cgd_softc *cs;
215
216 DPRINTF_FOLLOW(("cgdclose(%d, %d)\n", dev, flags));
217 GETCGD_SOFTC(cs, dev);
218 return dk_close(di, &cs->sc_dksc, dev, flags, fmt, p);
219 }
220
221 static void
222 cgdstrategy(struct buf *bp)
223 {
224 struct cgd_softc *cs = getcgd_softc(bp->b_dev);
225
226 DPRINTF_FOLLOW(("cgdstrategy(%p): b_bcount = %ld\n", bp,
227 (long)bp->b_bcount));
228 /* XXXrcd: Should we test for (cs != NULL)? */
229 dk_strategy(di, &cs->sc_dksc, bp);
230 return;
231 }
232
233 static int
234 cgdsize(dev_t dev)
235 {
236 struct cgd_softc *cs = getcgd_softc(dev);
237
238 DPRINTF_FOLLOW(("cgdsize(%d)\n", dev));
239 if (!cs)
240 return -1;
241 return dk_size(di, &cs->sc_dksc, dev);
242 }
243
244 /*
245 * cgd_{get,put}data are functions that deal with getting a buffer
246 * for the new encrypted data. We have a buffer per device so that
247 * we can ensure that we can always have a transaction in flight.
248 * We use this buffer first so that we have one less piece of
249 * malloc'ed data at any given point.
250 */
251
252 static void *
253 cgd_getdata(struct dk_softc *dksc, unsigned long size)
254 {
255 struct cgd_softc *cs =dksc->sc_osc;
256 caddr_t data = NULL;
257
258 simple_lock(&cs->sc_slock);
259 if (cs->sc_data_used == 0) {
260 cs->sc_data_used = 1;
261 data = cs->sc_data;
262 }
263 simple_unlock(&cs->sc_slock);
264
265 if (data)
266 return data;
267
268 return malloc(size, M_DEVBUF, M_NOWAIT);
269 }
270
271 static void
272 cgd_putdata(struct dk_softc *dksc, caddr_t data)
273 {
274 struct cgd_softc *cs =dksc->sc_osc;
275
276 if (data == cs->sc_data) {
277 simple_lock(&cs->sc_slock);
278 cs->sc_data_used = 0;
279 simple_unlock(&cs->sc_slock);
280 } else {
281 free(data, M_DEVBUF);
282 }
283 }
284
285 static int
286 cgdstart(struct dk_softc *dksc, struct buf *bp)
287 {
288 struct cgd_softc *cs = dksc->sc_osc;
289 struct buf *nbp;
290 struct partition *pp;
291 caddr_t addr;
292 caddr_t newaddr;
293 daddr_t bn;
294 int s;
295
296 DPRINTF_FOLLOW(("cgdstart(%p, %p)\n", dksc, bp));
297 disk_busy(&dksc->sc_dkdev); /* XXX: put in dksubr.c */
298
299 /* XXXrcd:
300 * Translate partition relative blocks to absolute blocks,
301 * this probably belongs (somehow) in dksubr.c, since it
302 * is independant of the underlying code... This will require
303 * that the interface be expanded slightly, though.
304 */
305 bn = bp->b_blkno;
306 if (DISKPART(bp->b_dev) != RAW_PART) {
307 pp = &cs->sc_dksc.sc_dkdev.dk_label->d_partitions[DISKPART(bp->b_dev)];
308 bn += pp->p_offset;
309 }
310
311 /*
312 * We attempt to allocate all of our resources up front, so that
313 * we can fail quickly if they are unavailable.
314 */
315
316 s = splbio();
317 nbp = pool_get(&bufpool, PR_NOWAIT);
318 splx(s);
319 if (nbp == NULL) {
320 disk_unbusy(&dksc->sc_dkdev, 0, (bp->b_flags & B_READ));
321 return -1;
322 }
323
324 /*
325 * If we are writing, then we need to encrypt the outgoing
326 * block into a new block of memory. If we fail, then we
327 * return an error and let the dksubr framework deal with it.
328 */
329 newaddr = addr = bp->b_data;
330 if ((bp->b_flags & B_READ) == 0) {
331 newaddr = cgd_getdata(dksc, bp->b_bcount);
332 if (!newaddr) {
333 s = splbio();
334 pool_put(&bufpool, nbp);
335 splx(s);
336 disk_unbusy(&dksc->sc_dkdev, 0, (bp->b_flags & B_READ));
337 return -1;
338 }
339 cgd_cipher(cs, newaddr, addr, bp->b_bcount, bn,
340 DEV_BSIZE, CGD_CIPHER_ENCRYPT);
341 }
342
343 BUF_INIT(nbp);
344 nbp->b_data = newaddr;
345 nbp->b_flags = bp->b_flags | B_CALL;
346 nbp->b_iodone = cgdiodone;
347 nbp->b_proc = bp->b_proc;
348 nbp->b_blkno = bn;
349 nbp->b_vp = cs->sc_tvn;
350 nbp->b_bcount = bp->b_bcount;
351 nbp->b_private = bp;
352
353 BIO_COPYPRIO(nbp, bp);
354
355 if ((nbp->b_flags & B_READ) == 0) {
356 V_INCR_NUMOUTPUT(nbp->b_vp);
357 }
358 VOP_STRATEGY(cs->sc_tvn, nbp);
359 return 0;
360 }
361
362 /* expected to be called at splbio() */
363 static void
364 cgdiodone(struct buf *nbp)
365 {
366 struct buf *obp = nbp->b_private;
367 struct cgd_softc *cs = getcgd_softc(obp->b_dev);
368 struct dk_softc *dksc = &cs->sc_dksc;
369
370 KDASSERT(cs);
371
372 DPRINTF_FOLLOW(("cgdiodone(%p)\n", nbp));
373 DPRINTF(CGDB_IO, ("cgdiodone: bp %p bcount %d resid %d\n",
374 obp, obp->b_bcount, obp->b_resid));
375 DPRINTF(CGDB_IO, (" dev 0x%x, nbp %p bn %" PRId64 " addr %p bcnt %d\n",
376 nbp->b_dev, nbp, nbp->b_blkno, nbp->b_data,
377 nbp->b_bcount));
378 if (nbp->b_flags & B_ERROR) {
379 obp->b_flags |= B_ERROR;
380 obp->b_error = nbp->b_error ? nbp->b_error : EIO;
381
382 printf("%s: error %d\n", dksc->sc_xname, obp->b_error);
383 }
384
385 /* Perform the decryption if we are reading.
386 *
387 * Note: use the blocknumber from nbp, since it is what
388 * we used to encrypt the blocks.
389 */
390
391 if (nbp->b_flags & B_READ)
392 cgd_cipher(cs, obp->b_data, obp->b_data, obp->b_bcount,
393 nbp->b_blkno, DEV_BSIZE, CGD_CIPHER_DECRYPT);
394
395 /* If we allocated memory, free it now... */
396 if (nbp->b_data != obp->b_data)
397 cgd_putdata(dksc, nbp->b_data);
398
399 pool_put(&bufpool, nbp);
400
401 /* Request is complete for whatever reason */
402 obp->b_resid = 0;
403 if (obp->b_flags & B_ERROR)
404 obp->b_resid = obp->b_bcount;
405 disk_unbusy(&dksc->sc_dkdev, obp->b_bcount - obp->b_resid,
406 (obp->b_flags & B_READ));
407 biodone(obp);
408 dk_iodone(di, dksc);
409 }
410
411 /* XXX: we should probably put these into dksubr.c, mostly */
412 static int
413 cgdread(dev_t dev, struct uio *uio, int flags)
414 {
415 struct cgd_softc *cs;
416 struct dk_softc *dksc;
417
418 DPRINTF_FOLLOW(("cgdread(%d, %p, %d)\n", dev, uio, flags));
419 GETCGD_SOFTC(cs, dev);
420 dksc = &cs->sc_dksc;
421 if ((dksc->sc_flags & DKF_INITED) == 0)
422 return ENXIO;
423 return physio(cgdstrategy, NULL, dev, B_READ, minphys, uio);
424 }
425
426 /* XXX: we should probably put these into dksubr.c, mostly */
427 static int
428 cgdwrite(dev_t dev, struct uio *uio, int flags)
429 {
430 struct cgd_softc *cs;
431 struct dk_softc *dksc;
432
433 DPRINTF_FOLLOW(("cgdwrite(%d, %p, %d)\n", dev, uio, flags));
434 GETCGD_SOFTC(cs, dev);
435 dksc = &cs->sc_dksc;
436 if ((dksc->sc_flags & DKF_INITED) == 0)
437 return ENXIO;
438 return physio(cgdstrategy, NULL, dev, B_WRITE, minphys, uio);
439 }
440
441 static int
442 cgdioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
443 {
444 struct cgd_softc *cs;
445 struct dk_softc *dksc;
446 int ret;
447 int part = DISKPART(dev);
448 int pmask = 1 << part;
449
450 DPRINTF_FOLLOW(("cgdioctl(%d, %ld, %p, %d, %p)\n",
451 dev, cmd, data, flag, p));
452 GETCGD_SOFTC(cs, dev);
453 dksc = &cs->sc_dksc;
454 switch (cmd) {
455 case CGDIOCSET:
456 case CGDIOCCLR:
457 if ((flag & FWRITE) == 0)
458 return EBADF;
459 }
460
461 if ((ret = lockmgr(&dksc->sc_lock, LK_EXCLUSIVE, NULL)) != 0)
462 return ret;
463
464 switch (cmd) {
465 case CGDIOCSET:
466 if (dksc->sc_flags & DKF_INITED)
467 ret = EBUSY;
468 else
469 ret = cgd_ioctl_set(cs, data, p);
470 break;
471 case CGDIOCCLR:
472 if (!(dksc->sc_flags & DKF_INITED)) {
473 ret = ENXIO;
474 break;
475 }
476 if (DK_BUSY(&cs->sc_dksc, pmask)) {
477 ret = EBUSY;
478 break;
479 }
480 ret = cgd_ioctl_clr(cs, data, p);
481 break;
482 default:
483 ret = dk_ioctl(di, dksc, dev, cmd, data, flag, p);
484 break;
485 }
486
487 lockmgr(&dksc->sc_lock, LK_RELEASE, NULL);
488 return ret;
489 }
490
491 static int
492 cgddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
493 {
494 struct cgd_softc *cs;
495
496 DPRINTF_FOLLOW(("cgddump(%d, %" PRId64 ", %p, %lu)\n", dev, blkno, va,
497 (unsigned long)size));
498 GETCGD_SOFTC(cs, dev);
499 return dk_dump(di, &cs->sc_dksc, dev, blkno, va, size);
500 }
501
502 /*
503 * XXXrcd:
504 * for now we hardcode the maximum key length.
505 */
506 #define MAX_KEYSIZE 1024
507
508 /* ARGSUSED */
509 static int
510 cgd_ioctl_set(struct cgd_softc *cs, void *data, struct proc *p)
511 {
512 struct cgd_ioctl *ci = data;
513 struct vnode *vp;
514 int ret;
515 int keybytes; /* key length in bytes */
516 const char *cp;
517 char inbuf[MAX_KEYSIZE];
518
519 cp = ci->ci_disk;
520 if ((ret = dk_lookup(cp, p, &vp)) != 0)
521 return ret;
522
523 if ((ret = cgdinit(cs, cp, vp, p)) != 0)
524 goto bail;
525
526 memset(inbuf, 0x0, sizeof(inbuf));
527 ret = copyinstr(ci->ci_alg, inbuf, 256, NULL);
528 if (ret)
529 goto bail;
530 cs->sc_cfuncs = cryptfuncs_find(inbuf);
531 if (!cs->sc_cfuncs) {
532 ret = EINVAL;
533 goto bail;
534 }
535
536 /* right now we only support encblkno, so hard-code it */
537 memset(inbuf, 0x0, sizeof(inbuf));
538 ret = copyinstr(ci->ci_ivmethod, inbuf, sizeof(inbuf), NULL);
539 if (ret)
540 goto bail;
541 if (strcmp("encblkno", inbuf)) {
542 ret = EINVAL;
543 goto bail;
544 }
545
546 keybytes = ci->ci_keylen / 8 + 1;
547 if (keybytes > MAX_KEYSIZE) {
548 ret = EINVAL;
549 goto bail;
550 }
551 memset(inbuf, 0x0, sizeof(inbuf));
552 ret = copyin(ci->ci_key, inbuf, keybytes);
553 if (ret)
554 goto bail;
555
556 cs->sc_cdata.cf_blocksize = ci->ci_blocksize;
557 cs->sc_cdata.cf_mode = CGD_CIPHER_CBC_ENCBLKNO;
558 cs->sc_cdata.cf_priv = cs->sc_cfuncs->cf_init(ci->ci_keylen, inbuf,
559 &cs->sc_cdata.cf_blocksize);
560 memset(inbuf, 0x0, sizeof(inbuf));
561 if (!cs->sc_cdata.cf_priv) {
562 printf("cgd: unable to initialize cipher\n");
563 ret = EINVAL; /* XXX is this the right error? */
564 goto bail;
565 }
566
567 bufq_alloc(&cs->sc_dksc.sc_bufq, BUFQ_FCFS);
568
569 cs->sc_data = malloc(MAXPHYS, M_DEVBUF, M_WAITOK);
570 cs->sc_data_used = 0;
571
572 cs->sc_dksc.sc_flags |= DKF_INITED;
573
574 /* Attach the disk. */
575 pseudo_disk_attach(&cs->sc_dksc.sc_dkdev);
576
577 /* Try and read the disklabel. */
578 dk_getdisklabel(di, &cs->sc_dksc, 0 /* XXX ? */);
579
580 return 0;
581
582 bail:
583 (void)vn_close(vp, FREAD|FWRITE, p->p_ucred, p);
584 return ret;
585 }
586
587 /* ARGSUSED */
588 static int
589 cgd_ioctl_clr(struct cgd_softc *cs, void *data, struct proc *p)
590 {
591 int s;
592
593 /* Kill off any queued buffers. */
594 s = splbio();
595 bufq_drain(&cs->sc_dksc.sc_bufq);
596 splx(s);
597 bufq_free(&cs->sc_dksc.sc_bufq);
598
599 (void)vn_close(cs->sc_tvn, FREAD|FWRITE, p->p_ucred, p);
600 cs->sc_cfuncs->cf_destroy(cs->sc_cdata.cf_priv);
601 free(cs->sc_tpath, M_DEVBUF);
602 free(cs->sc_data, M_DEVBUF);
603 cs->sc_data_used = 0;
604 cs->sc_dksc.sc_flags &= ~DKF_INITED;
605 pseudo_disk_detach(&cs->sc_dksc.sc_dkdev);
606
607 return 0;
608 }
609
610 static int
611 cgdinit(struct cgd_softc *cs, const char *cpath, struct vnode *vp,
612 struct proc *p)
613 {
614 struct dk_geom *pdg;
615 struct partinfo dpart;
616 struct vattr va;
617 size_t size;
618 int maxsecsize = 0;
619 int ret;
620 char tmppath[MAXPATHLEN];
621
622 cs->sc_dksc.sc_size = 0;
623 cs->sc_tvn = vp;
624
625 memset(tmppath, 0x0, sizeof(tmppath));
626 ret = copyinstr(cpath, tmppath, MAXPATHLEN, &cs->sc_tpathlen);
627 if (ret)
628 goto bail;
629 cs->sc_tpath = malloc(cs->sc_tpathlen, M_DEVBUF, M_WAITOK);
630 memcpy(cs->sc_tpath, tmppath, cs->sc_tpathlen);
631
632 if ((ret = VOP_GETATTR(vp, &va, p->p_ucred, p)) != 0)
633 goto bail;
634
635 cs->sc_tdev = va.va_rdev;
636
637 ret = VOP_IOCTL(vp, DIOCGPART, &dpart, FREAD, p->p_ucred, p);
638 if (ret)
639 goto bail;
640
641 maxsecsize =
642 ((dpart.disklab->d_secsize > maxsecsize) ?
643 dpart.disklab->d_secsize : maxsecsize);
644 size = dpart.part->p_size;
645
646 if (!size) {
647 ret = ENODEV;
648 goto bail;
649 }
650
651 cs->sc_dksc.sc_size = size;
652
653 /*
654 * XXX here we should probe the underlying device. If we
655 * are accessing a partition of type RAW_PART, then
656 * we should populate our initial geometry with the
657 * geometry that we discover from the device.
658 */
659 pdg = &cs->sc_dksc.sc_geom;
660 pdg->pdg_secsize = DEV_BSIZE;
661 pdg->pdg_ntracks = 1;
662 pdg->pdg_nsectors = 1024 * (1024 / pdg->pdg_secsize);
663 pdg->pdg_ncylinders = cs->sc_dksc.sc_size / pdg->pdg_nsectors;
664
665 bail:
666 if (ret && cs->sc_tpath)
667 free(cs->sc_tpath, M_DEVBUF);
668 return ret;
669 }
670
671 /*
672 * Our generic cipher entry point. This takes care of the
673 * IV mode and passes off the work to the specific cipher.
674 * We implement here the IV method ``encrypted block
675 * number''.
676 *
677 * For the encryption case, we accomplish this by setting
678 * up a struct uio where the first iovec of the source is
679 * the blocknumber and the first iovec of the dest is a
680 * sink. We then call the cipher with an IV of zero, and
681 * the right thing happens.
682 *
683 * For the decryption case, we use the same basic mechanism
684 * for symmetry, but we encrypt the block number in the
685 * first iovec.
686 *
687 * We mainly do this to avoid requiring the definition of
688 * an ECB mode.
689 *
690 * XXXrcd: for now we rely on our own crypto framework defined
691 * in dev/cgd_crypto.c. This will change when we
692 * get a generic kernel crypto framework.
693 */
694
695 static void
696 blkno2blkno_buf(char *sbuf, daddr_t blkno)
697 {
698 int i;
699
700 /* Set up the blkno in blkno_buf, here we do not care much
701 * about the final layout of the information as long as we
702 * can guarantee that each sector will have a different IV
703 * and that the endianness of the machine will not affect
704 * the representation that we have chosen.
705 *
706 * We choose this representation, because it does not rely
707 * on the size of buf (which is the blocksize of the cipher),
708 * but allows daddr_t to grow without breaking existing
709 * disks.
710 *
711 * Note that blkno2blkno_buf does not take a size as input,
712 * and hence must be called on a pre-zeroed buffer of length
713 * greater than or equal to sizeof(daddr_t).
714 */
715 for (i=0; i < sizeof(daddr_t); i++) {
716 *sbuf++ = blkno & 0xff;
717 blkno >>= 8;
718 }
719 }
720
721 static void
722 cgd_cipher(struct cgd_softc *cs, caddr_t dst, caddr_t src,
723 size_t len, daddr_t blkno, size_t secsize, int dir)
724 {
725 cfunc_cipher *cipher = cs->sc_cfuncs->cf_cipher;
726 struct uio dstuio;
727 struct uio srcuio;
728 struct iovec dstiov[2];
729 struct iovec srciov[2];
730 int blocksize = cs->sc_cdata.cf_blocksize;
731 char sink[blocksize];
732 char zero_iv[blocksize];
733 char blkno_buf[blocksize];
734
735 DPRINTF_FOLLOW(("cgd_cipher() dir=%d\n", dir));
736
737 DIAGCONDPANIC(len % blocksize != 0,
738 ("cgd_cipher: len %% blocksize != 0"));
739
740 /* ensure that sizeof(daddr_t) <= blocksize (for encblkno IVing) */
741 DIAGCONDPANIC(sizeof(daddr_t) > blocksize,
742 ("cgd_cipher: sizeof(daddr_t) > blocksize"));
743
744 memset(zero_iv, 0x0, sizeof(zero_iv));
745
746 dstuio.uio_iov = dstiov;
747 dstuio.uio_iovcnt = 2;
748
749 srcuio.uio_iov = srciov;
750 srcuio.uio_iovcnt = 2;
751
752 dstiov[0].iov_base = sink;
753 dstiov[0].iov_len = blocksize;
754 srciov[0].iov_base = blkno_buf;
755 srciov[0].iov_len = blocksize;
756 dstiov[1].iov_len = secsize;
757 srciov[1].iov_len = secsize;
758
759 for (; len > 0; len -= secsize) {
760 dstiov[1].iov_base = dst;
761 srciov[1].iov_base = src;
762
763 memset(blkno_buf, 0x0, sizeof(blkno_buf));
764 blkno2blkno_buf(blkno_buf, blkno);
765 if (dir == CGD_CIPHER_DECRYPT) {
766 dstuio.uio_iovcnt = 1;
767 srcuio.uio_iovcnt = 1;
768 IFDEBUG(CGDB_CRYPTO, hexprint("step 0: blkno_buf",
769 blkno_buf, sizeof(blkno_buf)));
770 cipher(cs->sc_cdata.cf_priv, &dstuio, &srcuio,
771 zero_iv, CGD_CIPHER_ENCRYPT);
772 memcpy(blkno_buf, sink, blocksize);
773 dstuio.uio_iovcnt = 2;
774 srcuio.uio_iovcnt = 2;
775 }
776
777 IFDEBUG(CGDB_CRYPTO, hexprint("step 1: blkno_buf",
778 blkno_buf, sizeof(blkno_buf)));
779 cipher(cs->sc_cdata.cf_priv, &dstuio, &srcuio, zero_iv, dir);
780 IFDEBUG(CGDB_CRYPTO, hexprint("step 2: sink",
781 sink, sizeof(sink)));
782
783 dst += secsize;
784 src += secsize;
785 blkno++;
786 }
787 }
788
789 #ifdef DEBUG
790 static void
791 hexprint(const char *start, void *buf, int len)
792 {
793 char *c = buf;
794
795 DIAGCONDPANIC(len < 0, ("hexprint: called with len < 0"));
796 printf("%s: len=%06d 0x", start, len);
797 while (len--)
798 printf("%02x", (unsigned) *c++);
799 }
800 #endif
801