cgd.c revision 1.48.2.1 1 /* $NetBSD: cgd.c,v 1.48.2.1 2007/12/04 13:02:55 ad 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.48.2.1 2007/12/04 13:02:55 ad 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 lwp *);
92 static int cgd_ioctl_clr(struct cgd_softc *, void *, struct lwp *);
93 static int cgdinit(struct cgd_softc *, const char *, struct vnode *,
94 struct lwp *);
95 static void cgd_cipher(struct cgd_softc *, void *, void *,
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 static struct dkdriver cgddkdriver = {
111 .d_strategy = cgdstrategy,
112 .d_minphys = minphys,
113 };
114
115 /* DIAGNOSTIC and DEBUG definitions */
116
117 #if defined(CGDDEBUG) && !defined(DEBUG)
118 #define DEBUG
119 #endif
120
121 #ifdef DEBUG
122 int cgddebug = 0;
123
124 #define CGDB_FOLLOW 0x1
125 #define CGDB_IO 0x2
126 #define CGDB_CRYPTO 0x4
127
128 #define IFDEBUG(x,y) if (cgddebug & (x)) y
129 #define DPRINTF(x,y) IFDEBUG(x, printf y)
130 #define DPRINTF_FOLLOW(y) DPRINTF(CGDB_FOLLOW, y)
131
132 static void hexprint(const char *, void *, int);
133
134 #else
135 #define IFDEBUG(x,y)
136 #define DPRINTF(x,y)
137 #define DPRINTF_FOLLOW(y)
138 #endif
139
140 #ifdef DIAGNOSTIC
141 #define DIAGPANIC(x) panic x
142 #define DIAGCONDPANIC(x,y) if (x) panic y
143 #else
144 #define DIAGPANIC(x)
145 #define DIAGCONDPANIC(x,y)
146 #endif
147
148 /* Global variables */
149
150 struct cgd_softc *cgd_softc;
151 int numcgd = 0;
152
153 /* Utility Functions */
154
155 #define CGDUNIT(x) DISKUNIT(x)
156 #define GETCGD_SOFTC(_cs, x) if (!((_cs) = getcgd_softc(x))) return ENXIO
157
158 static struct cgd_softc *
159 getcgd_softc(dev_t dev)
160 {
161 int unit = CGDUNIT(dev);
162
163 DPRINTF_FOLLOW(("getcgd_softc(0x%x): unit = %d\n", dev, unit));
164 if (unit >= numcgd)
165 return NULL;
166 return &cgd_softc[unit];
167 }
168
169 /* The code */
170
171 static void
172 cgdsoftc_init(struct cgd_softc *cs, int num)
173 {
174 char sbuf[DK_XNAME_SIZE];
175
176 memset(cs, 0x0, sizeof(*cs));
177 snprintf(sbuf, DK_XNAME_SIZE, "cgd%d", num);
178 simple_lock_init(&cs->sc_slock);
179 dk_sc_init(&cs->sc_dksc, cs, sbuf);
180 disk_init(&cs->sc_dksc.sc_dkdev, cs->sc_dksc.sc_xname, &cgddkdriver);
181 }
182
183 void
184 cgdattach(int num)
185 {
186 int i;
187
188 DPRINTF_FOLLOW(("cgdattach(%d)\n", num));
189 if (num <= 0) {
190 DIAGPANIC(("cgdattach: count <= 0"));
191 return;
192 }
193
194 cgd_softc = (void *)malloc(num * sizeof(*cgd_softc), M_DEVBUF, M_NOWAIT);
195 if (!cgd_softc) {
196 printf("WARNING: unable to malloc(9) memory for crypt disks\n");
197 DIAGPANIC(("cgdattach: cannot malloc(9) enough memory"));
198 return;
199 }
200
201 numcgd = num;
202 for (i=0; i<num; i++)
203 cgdsoftc_init(&cgd_softc[i], i);
204 }
205
206 static int
207 cgdopen(dev_t dev, int flags, int fmt, struct lwp *l)
208 {
209 struct cgd_softc *cs;
210
211 DPRINTF_FOLLOW(("cgdopen(%d, %d)\n", dev, flags));
212 GETCGD_SOFTC(cs, dev);
213 return dk_open(di, &cs->sc_dksc, dev, flags, fmt, l);
214 }
215
216 static int
217 cgdclose(dev_t dev, int flags, int fmt, struct lwp *l)
218 {
219 struct cgd_softc *cs;
220
221 DPRINTF_FOLLOW(("cgdclose(%d, %d)\n", dev, flags));
222 GETCGD_SOFTC(cs, dev);
223 return dk_close(di, &cs->sc_dksc, dev, flags, fmt, l);
224 }
225
226 static void
227 cgdstrategy(struct buf *bp)
228 {
229 struct cgd_softc *cs = getcgd_softc(bp->b_dev);
230
231 DPRINTF_FOLLOW(("cgdstrategy(%p): b_bcount = %ld\n", bp,
232 (long)bp->b_bcount));
233 /* XXXrcd: Should we test for (cs != NULL)? */
234 dk_strategy(di, &cs->sc_dksc, bp);
235 return;
236 }
237
238 static int
239 cgdsize(dev_t dev)
240 {
241 struct cgd_softc *cs = getcgd_softc(dev);
242
243 DPRINTF_FOLLOW(("cgdsize(%d)\n", dev));
244 if (!cs)
245 return -1;
246 return dk_size(di, &cs->sc_dksc, dev);
247 }
248
249 /*
250 * cgd_{get,put}data are functions that deal with getting a buffer
251 * for the new encrypted data. We have a buffer per device so that
252 * we can ensure that we can always have a transaction in flight.
253 * We use this buffer first so that we have one less piece of
254 * malloc'ed data at any given point.
255 */
256
257 static void *
258 cgd_getdata(struct dk_softc *dksc, unsigned long size)
259 {
260 struct cgd_softc *cs =dksc->sc_osc;
261 void * data = NULL;
262
263 simple_lock(&cs->sc_slock);
264 if (cs->sc_data_used == 0) {
265 cs->sc_data_used = 1;
266 data = cs->sc_data;
267 }
268 simple_unlock(&cs->sc_slock);
269
270 if (data)
271 return data;
272
273 return malloc(size, M_DEVBUF, M_NOWAIT);
274 }
275
276 static void
277 cgd_putdata(struct dk_softc *dksc, void *data)
278 {
279 struct cgd_softc *cs =dksc->sc_osc;
280
281 if (data == cs->sc_data) {
282 simple_lock(&cs->sc_slock);
283 cs->sc_data_used = 0;
284 simple_unlock(&cs->sc_slock);
285 } else {
286 free(data, M_DEVBUF);
287 }
288 }
289
290 static int
291 cgdstart(struct dk_softc *dksc, struct buf *bp)
292 {
293 struct cgd_softc *cs = dksc->sc_osc;
294 struct buf *nbp;
295 void * addr;
296 void * newaddr;
297 daddr_t bn;
298 struct vnode *vp;
299
300 DPRINTF_FOLLOW(("cgdstart(%p, %p)\n", dksc, bp));
301 disk_busy(&dksc->sc_dkdev); /* XXX: put in dksubr.c */
302
303 bn = bp->b_rawblkno;
304
305 /*
306 * We attempt to allocate all of our resources up front, so that
307 * we can fail quickly if they are unavailable.
308 */
309
310 nbp = getiobuf(cs->sc_tvn, false);
311 if (nbp == NULL) {
312 disk_unbusy(&dksc->sc_dkdev, 0, (bp->b_flags & B_READ));
313 return -1;
314 }
315
316 /*
317 * If we are writing, then we need to encrypt the outgoing
318 * block into a new block of memory. If we fail, then we
319 * return an error and let the dksubr framework deal with it.
320 */
321 newaddr = addr = bp->b_data;
322 if ((bp->b_flags & B_READ) == 0) {
323 newaddr = cgd_getdata(dksc, bp->b_bcount);
324 if (!newaddr) {
325 putiobuf(nbp);
326 disk_unbusy(&dksc->sc_dkdev, 0, (bp->b_flags & B_READ));
327 return -1;
328 }
329 cgd_cipher(cs, newaddr, addr, bp->b_bcount, bn,
330 DEV_BSIZE, CGD_CIPHER_ENCRYPT);
331 }
332
333 nbp->b_data = newaddr;
334 nbp->b_flags = bp->b_flags;
335 nbp->b_oflags = bp->b_oflags;
336 nbp->b_cflags = bp->b_cflags;
337 nbp->b_iodone = cgdiodone;
338 nbp->b_proc = bp->b_proc;
339 nbp->b_blkno = bn;
340 nbp->b_bcount = bp->b_bcount;
341 nbp->b_private = bp;
342
343 BIO_COPYPRIO(nbp, bp);
344
345 if ((nbp->b_flags & B_READ) == 0) {
346 vp = nbp->b_vp;
347 mutex_enter(&vp->v_interlock);
348 vp->v_numoutput++;
349 mutex_exit(&vp->v_interlock);
350 }
351 VOP_STRATEGY(cs->sc_tvn, nbp);
352 return 0;
353 }
354
355 /* expected to be called at splbio() */
356 static void
357 cgdiodone(struct buf *nbp)
358 {
359 struct buf *obp = nbp->b_private;
360 struct cgd_softc *cs = getcgd_softc(obp->b_dev);
361 struct dk_softc *dksc = &cs->sc_dksc;
362
363 KDASSERT(cs);
364
365 DPRINTF_FOLLOW(("cgdiodone(%p)\n", nbp));
366 DPRINTF(CGDB_IO, ("cgdiodone: bp %p bcount %d resid %d\n",
367 obp, obp->b_bcount, obp->b_resid));
368 DPRINTF(CGDB_IO, (" dev 0x%x, nbp %p bn %" PRId64 " addr %p bcnt %d\n",
369 nbp->b_dev, nbp, nbp->b_blkno, nbp->b_data,
370 nbp->b_bcount));
371 if (nbp->b_error != 0) {
372 obp->b_error = nbp->b_error;
373 printf("%s: error %d\n", dksc->sc_xname, obp->b_error);
374 }
375
376 /* Perform the decryption if we are reading.
377 *
378 * Note: use the blocknumber from nbp, since it is what
379 * we used to encrypt the blocks.
380 */
381
382 if (nbp->b_flags & B_READ)
383 cgd_cipher(cs, obp->b_data, obp->b_data, obp->b_bcount,
384 nbp->b_blkno, DEV_BSIZE, CGD_CIPHER_DECRYPT);
385
386 /* If we allocated memory, free it now... */
387 if (nbp->b_data != obp->b_data)
388 cgd_putdata(dksc, nbp->b_data);
389
390 putiobuf(nbp);
391
392 /* Request is complete for whatever reason */
393 obp->b_resid = 0;
394 if (obp->b_error != 0)
395 obp->b_resid = obp->b_bcount;
396 disk_unbusy(&dksc->sc_dkdev, obp->b_bcount - obp->b_resid,
397 (obp->b_flags & B_READ));
398 biodone(obp);
399 dk_iodone(di, dksc);
400 }
401
402 /* XXX: we should probably put these into dksubr.c, mostly */
403 static int
404 cgdread(dev_t dev, struct uio *uio, int flags)
405 {
406 struct cgd_softc *cs;
407 struct dk_softc *dksc;
408
409 DPRINTF_FOLLOW(("cgdread(%d, %p, %d)\n", dev, uio, flags));
410 GETCGD_SOFTC(cs, dev);
411 dksc = &cs->sc_dksc;
412 if ((dksc->sc_flags & DKF_INITED) == 0)
413 return ENXIO;
414 return physio(cgdstrategy, NULL, dev, B_READ, minphys, uio);
415 }
416
417 /* XXX: we should probably put these into dksubr.c, mostly */
418 static int
419 cgdwrite(dev_t dev, struct uio *uio, int flags)
420 {
421 struct cgd_softc *cs;
422 struct dk_softc *dksc;
423
424 DPRINTF_FOLLOW(("cgdwrite(%d, %p, %d)\n", dev, uio, flags));
425 GETCGD_SOFTC(cs, dev);
426 dksc = &cs->sc_dksc;
427 if ((dksc->sc_flags & DKF_INITED) == 0)
428 return ENXIO;
429 return physio(cgdstrategy, NULL, dev, B_WRITE, minphys, uio);
430 }
431
432 static int
433 cgdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
434 {
435 struct cgd_softc *cs;
436 struct dk_softc *dksc;
437 struct disk *dk;
438 int ret;
439 int part = DISKPART(dev);
440 int pmask = 1 << part;
441
442 DPRINTF_FOLLOW(("cgdioctl(%d, %ld, %p, %d, %p)\n",
443 dev, cmd, data, flag, l));
444 GETCGD_SOFTC(cs, dev);
445 dksc = &cs->sc_dksc;
446 dk = &dksc->sc_dkdev;
447 switch (cmd) {
448 case CGDIOCSET:
449 case CGDIOCCLR:
450 if ((flag & FWRITE) == 0)
451 return EBADF;
452 }
453
454 switch (cmd) {
455 case CGDIOCSET:
456 if (dksc->sc_flags & DKF_INITED)
457 ret = EBUSY;
458 else
459 ret = cgd_ioctl_set(cs, data, l);
460 break;
461 case CGDIOCCLR:
462 if (!(dksc->sc_flags & DKF_INITED)) {
463 ret = ENXIO;
464 break;
465 }
466 if (DK_BUSY(&cs->sc_dksc, pmask)) {
467 ret = EBUSY;
468 break;
469 }
470 ret = cgd_ioctl_clr(cs, data, l);
471 break;
472 default:
473 ret = dk_ioctl(di, dksc, dev, cmd, data, flag, l);
474 break;
475 }
476
477 return ret;
478 }
479
480 static int
481 cgddump(dev_t dev, daddr_t blkno, void *va, size_t size)
482 {
483 struct cgd_softc *cs;
484
485 DPRINTF_FOLLOW(("cgddump(%d, %" PRId64 ", %p, %lu)\n", dev, blkno, va,
486 (unsigned long)size));
487 GETCGD_SOFTC(cs, dev);
488 return dk_dump(di, &cs->sc_dksc, dev, blkno, va, size);
489 }
490
491 /*
492 * XXXrcd:
493 * for now we hardcode the maximum key length.
494 */
495 #define MAX_KEYSIZE 1024
496
497 /* ARGSUSED */
498 static int
499 cgd_ioctl_set(struct cgd_softc *cs, void *data, struct lwp *l)
500 {
501 struct cgd_ioctl *ci = data;
502 struct vnode *vp;
503 int ret;
504 size_t keybytes; /* key length in bytes */
505 const char *cp;
506 char *inbuf;
507
508 cp = ci->ci_disk;
509 if ((ret = dk_lookup(cp, l, &vp, UIO_USERSPACE)) != 0)
510 return ret;
511
512 inbuf = malloc(MAX_KEYSIZE, M_TEMP, M_WAITOK);
513
514 if ((ret = cgdinit(cs, cp, vp, l)) != 0)
515 goto bail;
516
517 (void)memset(inbuf, 0, MAX_KEYSIZE);
518 ret = copyinstr(ci->ci_alg, inbuf, 256, NULL);
519 if (ret)
520 goto bail;
521 cs->sc_cfuncs = cryptfuncs_find(inbuf);
522 if (!cs->sc_cfuncs) {
523 ret = EINVAL;
524 goto bail;
525 }
526
527 /* right now we only support encblkno, so hard-code it */
528 (void)memset(inbuf, 0, MAX_KEYSIZE);
529 ret = copyinstr(ci->ci_ivmethod, inbuf, MAX_KEYSIZE, NULL);
530 if (ret)
531 goto bail;
532 if (strcmp("encblkno", inbuf)) {
533 ret = EINVAL;
534 goto bail;
535 }
536
537 keybytes = ci->ci_keylen / 8 + 1;
538 if (keybytes > MAX_KEYSIZE) {
539 ret = EINVAL;
540 goto bail;
541 }
542 (void)memset(inbuf, 0, MAX_KEYSIZE);
543 ret = copyin(ci->ci_key, inbuf, keybytes);
544 if (ret)
545 goto bail;
546
547 cs->sc_cdata.cf_blocksize = ci->ci_blocksize;
548 cs->sc_cdata.cf_mode = CGD_CIPHER_CBC_ENCBLKNO;
549 cs->sc_cdata.cf_priv = cs->sc_cfuncs->cf_init(ci->ci_keylen, inbuf,
550 &cs->sc_cdata.cf_blocksize);
551 (void)memset(inbuf, 0, MAX_KEYSIZE);
552 if (!cs->sc_cdata.cf_priv) {
553 printf("cgd: unable to initialize cipher\n");
554 ret = EINVAL; /* XXX is this the right error? */
555 goto bail;
556 }
557 free(inbuf, M_TEMP);
558
559 bufq_alloc(&cs->sc_dksc.sc_bufq, "fcfs", 0);
560
561 cs->sc_data = malloc(MAXPHYS, M_DEVBUF, M_WAITOK);
562 cs->sc_data_used = 0;
563
564 cs->sc_dksc.sc_flags |= DKF_INITED;
565
566 /* Attach the disk. */
567 disk_attach(&cs->sc_dksc.sc_dkdev);
568
569 /* Try and read the disklabel. */
570 dk_getdisklabel(di, &cs->sc_dksc, 0 /* XXX ? */);
571
572 /* Discover wedges on this disk. */
573 dkwedge_discover(&cs->sc_dksc.sc_dkdev);
574
575 return 0;
576
577 bail:
578 free(inbuf, M_TEMP);
579 (void)vn_close(vp, FREAD|FWRITE, l->l_cred, l);
580 return ret;
581 }
582
583 /* ARGSUSED */
584 static int
585 cgd_ioctl_clr(struct cgd_softc *cs, void *data, struct lwp *l)
586 {
587 int s;
588
589 /* Delete all of our wedges. */
590 dkwedge_delall(&cs->sc_dksc.sc_dkdev);
591
592 /* Kill off any queued buffers. */
593 s = splbio();
594 bufq_drain(cs->sc_dksc.sc_bufq);
595 splx(s);
596 bufq_free(cs->sc_dksc.sc_bufq);
597
598 (void)vn_close(cs->sc_tvn, FREAD|FWRITE, l->l_cred, l);
599 cs->sc_cfuncs->cf_destroy(cs->sc_cdata.cf_priv);
600 free(cs->sc_tpath, M_DEVBUF);
601 free(cs->sc_data, M_DEVBUF);
602 cs->sc_data_used = 0;
603 cs->sc_dksc.sc_flags &= ~DKF_INITED;
604 disk_detach(&cs->sc_dksc.sc_dkdev);
605
606 return 0;
607 }
608
609 static int
610 cgdinit(struct cgd_softc *cs, const char *cpath, struct vnode *vp,
611 struct lwp *l)
612 {
613 struct dk_geom *pdg;
614 struct partinfo dpart;
615 struct vattr va;
616 size_t size;
617 int maxsecsize = 0;
618 int ret;
619 char *tmppath;
620
621 cs->sc_dksc.sc_size = 0;
622 cs->sc_tvn = vp;
623 cs->sc_tpath = NULL;
624
625 tmppath = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
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, l->l_cred)) != 0)
633 goto bail;
634
635 cs->sc_tdev = va.va_rdev;
636
637 ret = VOP_IOCTL(vp, DIOCGPART, &dpart, FREAD, l->l_cred);
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 free(tmppath, M_TEMP);
667 if (ret && cs->sc_tpath)
668 free(cs->sc_tpath, M_DEVBUF);
669 return ret;
670 }
671
672 /*
673 * Our generic cipher entry point. This takes care of the
674 * IV mode and passes off the work to the specific cipher.
675 * We implement here the IV method ``encrypted block
676 * number''.
677 *
678 * For the encryption case, we accomplish this by setting
679 * up a struct uio where the first iovec of the source is
680 * the blocknumber and the first iovec of the dest is a
681 * sink. We then call the cipher with an IV of zero, and
682 * the right thing happens.
683 *
684 * For the decryption case, we use the same basic mechanism
685 * for symmetry, but we encrypt the block number in the
686 * first iovec.
687 *
688 * We mainly do this to avoid requiring the definition of
689 * an ECB mode.
690 *
691 * XXXrcd: for now we rely on our own crypto framework defined
692 * in dev/cgd_crypto.c. This will change when we
693 * get a generic kernel crypto framework.
694 */
695
696 static void
697 blkno2blkno_buf(char *sbuf, daddr_t blkno)
698 {
699 int i;
700
701 /* Set up the blkno in blkno_buf, here we do not care much
702 * about the final layout of the information as long as we
703 * can guarantee that each sector will have a different IV
704 * and that the endianness of the machine will not affect
705 * the representation that we have chosen.
706 *
707 * We choose this representation, because it does not rely
708 * on the size of buf (which is the blocksize of the cipher),
709 * but allows daddr_t to grow without breaking existing
710 * disks.
711 *
712 * Note that blkno2blkno_buf does not take a size as input,
713 * and hence must be called on a pre-zeroed buffer of length
714 * greater than or equal to sizeof(daddr_t).
715 */
716 for (i=0; i < sizeof(daddr_t); i++) {
717 *sbuf++ = blkno & 0xff;
718 blkno >>= 8;
719 }
720 }
721
722 static void
723 cgd_cipher(struct cgd_softc *cs, void *dstv, void *srcv,
724 size_t len, daddr_t blkno, size_t secsize, int dir)
725 {
726 char *dst = dstv;
727 char *src = srcv;
728 cfunc_cipher *cipher = cs->sc_cfuncs->cf_cipher;
729 struct uio dstuio;
730 struct uio srcuio;
731 struct iovec dstiov[2];
732 struct iovec srciov[2];
733 size_t blocksize = cs->sc_cdata.cf_blocksize;
734 char sink[blocksize];
735 char zero_iv[blocksize];
736 char blkno_buf[blocksize];
737
738 DPRINTF_FOLLOW(("cgd_cipher() dir=%d\n", dir));
739
740 DIAGCONDPANIC(len % blocksize != 0,
741 ("cgd_cipher: len %% blocksize != 0"));
742
743 /* ensure that sizeof(daddr_t) <= blocksize (for encblkno IVing) */
744 DIAGCONDPANIC(sizeof(daddr_t) > blocksize,
745 ("cgd_cipher: sizeof(daddr_t) > blocksize"));
746
747 memset(zero_iv, 0x0, sizeof(zero_iv));
748
749 dstuio.uio_iov = dstiov;
750 dstuio.uio_iovcnt = 2;
751
752 srcuio.uio_iov = srciov;
753 srcuio.uio_iovcnt = 2;
754
755 dstiov[0].iov_base = sink;
756 dstiov[0].iov_len = blocksize;
757 srciov[0].iov_base = blkno_buf;
758 srciov[0].iov_len = blocksize;
759 dstiov[1].iov_len = secsize;
760 srciov[1].iov_len = secsize;
761
762 for (; len > 0; len -= secsize) {
763 dstiov[1].iov_base = dst;
764 srciov[1].iov_base = src;
765
766 memset(blkno_buf, 0x0, sizeof(blkno_buf));
767 blkno2blkno_buf(blkno_buf, blkno);
768 if (dir == CGD_CIPHER_DECRYPT) {
769 dstuio.uio_iovcnt = 1;
770 srcuio.uio_iovcnt = 1;
771 IFDEBUG(CGDB_CRYPTO, hexprint("step 0: blkno_buf",
772 blkno_buf, sizeof(blkno_buf)));
773 cipher(cs->sc_cdata.cf_priv, &dstuio, &srcuio,
774 zero_iv, CGD_CIPHER_ENCRYPT);
775 memcpy(blkno_buf, sink, blocksize);
776 dstuio.uio_iovcnt = 2;
777 srcuio.uio_iovcnt = 2;
778 }
779
780 IFDEBUG(CGDB_CRYPTO, hexprint("step 1: blkno_buf",
781 blkno_buf, sizeof(blkno_buf)));
782 cipher(cs->sc_cdata.cf_priv, &dstuio, &srcuio, zero_iv, dir);
783 IFDEBUG(CGDB_CRYPTO, hexprint("step 2: sink",
784 sink, sizeof(sink)));
785
786 dst += secsize;
787 src += secsize;
788 blkno++;
789 }
790 }
791
792 #ifdef DEBUG
793 static void
794 hexprint(const char *start, void *buf, int len)
795 {
796 char *c = buf;
797
798 DIAGCONDPANIC(len < 0, ("hexprint: called with len < 0"));
799 printf("%s: len=%06d 0x", start, len);
800 while (len--)
801 printf("%02x", (unsigned char) *c++);
802 }
803 #endif
804