cgd.c revision 1.50.6.2 1 /* $NetBSD: cgd.c,v 1.50.6.2 2008/04/05 23:33:20 mjf 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.50.6.2 2008/04/05 23:33:20 mjf 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/conf.h>
58
59 #include <dev/dkvar.h>
60 #include <dev/cgdvar.h>
61
62 /* Entry Point Functions */
63
64 void cgdattach(int);
65
66 static dev_type_open(cgdopen);
67 static dev_type_close(cgdclose);
68 static dev_type_read(cgdread);
69 static dev_type_write(cgdwrite);
70 static dev_type_ioctl(cgdioctl);
71 static dev_type_strategy(cgdstrategy);
72 static dev_type_dump(cgddump);
73 static dev_type_size(cgdsize);
74
75 const struct bdevsw cgd_bdevsw = {
76 cgdopen, cgdclose, cgdstrategy, cgdioctl,
77 cgddump, cgdsize, D_DISK
78 };
79
80 const struct cdevsw cgd_cdevsw = {
81 cgdopen, cgdclose, cgdread, cgdwrite, cgdioctl,
82 nostop, notty, nopoll, nommap, nokqfilter, D_DISK
83 };
84
85 /* Internal Functions */
86
87 static int cgdstart(struct dk_softc *, struct buf *);
88 static void cgdiodone(struct buf *);
89
90 static int cgd_ioctl_set(struct cgd_softc *, void *, struct lwp *);
91 static int cgd_ioctl_clr(struct cgd_softc *, void *, struct lwp *);
92 static int cgdinit(struct cgd_softc *, const char *, struct vnode *,
93 struct lwp *);
94 static void cgd_cipher(struct cgd_softc *, void *, void *,
95 size_t, daddr_t, size_t, int);
96
97 /* Pseudo-disk Interface */
98
99 static struct dk_intf the_dkintf = {
100 DTYPE_CGD,
101 "cgd",
102 cgdopen,
103 cgdclose,
104 cgdstrategy,
105 cgdstart,
106 };
107 static struct dk_intf *di = &the_dkintf;
108
109 static struct dkdriver cgddkdriver = {
110 .d_strategy = cgdstrategy,
111 .d_minphys = minphys,
112 };
113
114 /* DIAGNOSTIC and DEBUG definitions */
115
116 #if defined(CGDDEBUG) && !defined(DEBUG)
117 #define DEBUG
118 #endif
119
120 #ifdef DEBUG
121 int cgddebug = 0;
122
123 #define CGDB_FOLLOW 0x1
124 #define CGDB_IO 0x2
125 #define CGDB_CRYPTO 0x4
126
127 #define IFDEBUG(x,y) if (cgddebug & (x)) y
128 #define DPRINTF(x,y) IFDEBUG(x, printf y)
129 #define DPRINTF_FOLLOW(y) DPRINTF(CGDB_FOLLOW, y)
130
131 static void hexprint(const char *, void *, int);
132
133 #else
134 #define IFDEBUG(x,y)
135 #define DPRINTF(x,y)
136 #define DPRINTF_FOLLOW(y)
137 #endif
138
139 #ifdef DIAGNOSTIC
140 #define DIAGPANIC(x) panic x
141 #define DIAGCONDPANIC(x,y) if (x) panic y
142 #else
143 #define DIAGPANIC(x)
144 #define DIAGCONDPANIC(x,y)
145 #endif
146
147 /* Global variables */
148
149 struct cgd_softc *cgd_softc;
150 int numcgd = 0;
151
152 /* Utility Functions */
153
154 #define CGDUNIT(x) DISKUNIT(x)
155 #define GETCGD_SOFTC(_cs, x) if (!((_cs) = getcgd_softc(x))) return ENXIO
156
157 static struct cgd_softc *
158 getcgd_softc(dev_t dev)
159 {
160 int unit = CGDUNIT(dev);
161
162 DPRINTF_FOLLOW(("getcgd_softc(0x%x): unit = %d\n", dev, unit));
163 if (unit >= numcgd)
164 return NULL;
165 return &cgd_softc[unit];
166 }
167
168 /* The code */
169
170 static void
171 cgdsoftc_init(struct cgd_softc *cs, int num)
172 {
173 char sbuf[DK_XNAME_SIZE];
174
175 memset(cs, 0x0, sizeof(*cs));
176 snprintf(sbuf, DK_XNAME_SIZE, "cgd%d", num);
177 simple_lock_init(&cs->sc_slock);
178 dk_sc_init(&cs->sc_dksc, cs, sbuf);
179 disk_init(&cs->sc_dksc.sc_dkdev, cs->sc_dksc.sc_xname, &cgddkdriver);
180 }
181
182 void
183 cgdattach(int num)
184 {
185 int i, j, bmaj, cmaj;
186
187 DPRINTF_FOLLOW(("cgdattach(%d)\n", num));
188 if (num <= 0) {
189 DIAGPANIC(("cgdattach: count <= 0"));
190 return;
191 }
192
193 cgd_softc = (void *)malloc(num * sizeof(*cgd_softc), M_DEVBUF, M_NOWAIT);
194 if (!cgd_softc) {
195 printf("WARNING: unable to malloc(9) memory for crypt disks\n");
196 DIAGPANIC(("cgdattach: cannot malloc(9) enough memory"));
197 return;
198 }
199
200 bmaj = bdevsw_lookup_major(&cgd_bdevsw);
201 cmaj = cdevsw_lookup_major(&cgd_cdevsw);
202
203 numcgd = num;
204 for (i=0; i<num; i++) {
205 cgdsoftc_init(&cgd_softc[i], i);
206 for (j = 0; j < MAXPARTITIONS; j++) {
207 device_register_name(MAKEDISKDEV(bmaj, i, j), NULL,
208 false, DEV_DISK, "cgd%d%c", i, 'a' + j);
209 device_register_name(MAKEDISKDEV(cmaj, i, j), NULL,
210 true, DEV_DISK, "rcgd%d%c", i, 'a' + j);
211 }
212 }
213 }
214
215 static int
216 cgdopen(dev_t dev, int flags, int fmt, struct lwp *l)
217 {
218 struct cgd_softc *cs;
219
220 DPRINTF_FOLLOW(("cgdopen(%d, %d)\n", dev, flags));
221 GETCGD_SOFTC(cs, dev);
222 return dk_open(di, &cs->sc_dksc, dev, flags, fmt, l);
223 }
224
225 static int
226 cgdclose(dev_t dev, int flags, int fmt, struct lwp *l)
227 {
228 struct cgd_softc *cs;
229
230 DPRINTF_FOLLOW(("cgdclose(%d, %d)\n", dev, flags));
231 GETCGD_SOFTC(cs, dev);
232 return dk_close(di, &cs->sc_dksc, dev, flags, fmt, l);
233 }
234
235 static void
236 cgdstrategy(struct buf *bp)
237 {
238 struct cgd_softc *cs = getcgd_softc(bp->b_dev);
239
240 DPRINTF_FOLLOW(("cgdstrategy(%p): b_bcount = %ld\n", bp,
241 (long)bp->b_bcount));
242 /* XXXrcd: Should we test for (cs != NULL)? */
243 dk_strategy(di, &cs->sc_dksc, bp);
244 return;
245 }
246
247 static int
248 cgdsize(dev_t dev)
249 {
250 struct cgd_softc *cs = getcgd_softc(dev);
251
252 DPRINTF_FOLLOW(("cgdsize(%d)\n", dev));
253 if (!cs)
254 return -1;
255 return dk_size(di, &cs->sc_dksc, dev);
256 }
257
258 /*
259 * cgd_{get,put}data are functions that deal with getting a buffer
260 * for the new encrypted data. We have a buffer per device so that
261 * we can ensure that we can always have a transaction in flight.
262 * We use this buffer first so that we have one less piece of
263 * malloc'ed data at any given point.
264 */
265
266 static void *
267 cgd_getdata(struct dk_softc *dksc, unsigned long size)
268 {
269 struct cgd_softc *cs =dksc->sc_osc;
270 void * data = NULL;
271
272 simple_lock(&cs->sc_slock);
273 if (cs->sc_data_used == 0) {
274 cs->sc_data_used = 1;
275 data = cs->sc_data;
276 }
277 simple_unlock(&cs->sc_slock);
278
279 if (data)
280 return data;
281
282 return malloc(size, M_DEVBUF, M_NOWAIT);
283 }
284
285 static void
286 cgd_putdata(struct dk_softc *dksc, void *data)
287 {
288 struct cgd_softc *cs =dksc->sc_osc;
289
290 if (data == cs->sc_data) {
291 simple_lock(&cs->sc_slock);
292 cs->sc_data_used = 0;
293 simple_unlock(&cs->sc_slock);
294 } else {
295 free(data, M_DEVBUF);
296 }
297 }
298
299 static int
300 cgdstart(struct dk_softc *dksc, struct buf *bp)
301 {
302 struct cgd_softc *cs = dksc->sc_osc;
303 struct buf *nbp;
304 void * addr;
305 void * newaddr;
306 daddr_t bn;
307 struct vnode *vp;
308
309 DPRINTF_FOLLOW(("cgdstart(%p, %p)\n", dksc, bp));
310 disk_busy(&dksc->sc_dkdev); /* XXX: put in dksubr.c */
311
312 bn = bp->b_rawblkno;
313
314 /*
315 * We attempt to allocate all of our resources up front, so that
316 * we can fail quickly if they are unavailable.
317 */
318
319 nbp = getiobuf(cs->sc_tvn, false);
320 if (nbp == NULL) {
321 disk_unbusy(&dksc->sc_dkdev, 0, (bp->b_flags & B_READ));
322 return -1;
323 }
324
325 /*
326 * If we are writing, then we need to encrypt the outgoing
327 * block into a new block of memory. If we fail, then we
328 * return an error and let the dksubr framework deal with it.
329 */
330 newaddr = addr = bp->b_data;
331 if ((bp->b_flags & B_READ) == 0) {
332 newaddr = cgd_getdata(dksc, bp->b_bcount);
333 if (!newaddr) {
334 putiobuf(nbp);
335 disk_unbusy(&dksc->sc_dkdev, 0, (bp->b_flags & B_READ));
336 return -1;
337 }
338 cgd_cipher(cs, newaddr, addr, bp->b_bcount, bn,
339 DEV_BSIZE, CGD_CIPHER_ENCRYPT);
340 }
341
342 nbp->b_data = newaddr;
343 nbp->b_flags = bp->b_flags;
344 nbp->b_oflags = bp->b_oflags;
345 nbp->b_cflags = bp->b_cflags;
346 nbp->b_iodone = cgdiodone;
347 nbp->b_proc = bp->b_proc;
348 nbp->b_blkno = bn;
349 nbp->b_bcount = bp->b_bcount;
350 nbp->b_private = bp;
351
352 BIO_COPYPRIO(nbp, bp);
353
354 if ((nbp->b_flags & B_READ) == 0) {
355 vp = nbp->b_vp;
356 mutex_enter(&vp->v_interlock);
357 vp->v_numoutput++;
358 mutex_exit(&vp->v_interlock);
359 }
360 VOP_STRATEGY(cs->sc_tvn, nbp);
361 return 0;
362 }
363
364 /* expected to be called at splbio() */
365 static void
366 cgdiodone(struct buf *nbp)
367 {
368 struct buf *obp = nbp->b_private;
369 struct cgd_softc *cs = getcgd_softc(obp->b_dev);
370 struct dk_softc *dksc = &cs->sc_dksc;
371
372 KDASSERT(cs);
373
374 DPRINTF_FOLLOW(("cgdiodone(%p)\n", nbp));
375 DPRINTF(CGDB_IO, ("cgdiodone: bp %p bcount %d resid %d\n",
376 obp, obp->b_bcount, obp->b_resid));
377 DPRINTF(CGDB_IO, (" dev 0x%x, nbp %p bn %" PRId64 " addr %p bcnt %d\n",
378 nbp->b_dev, nbp, nbp->b_blkno, nbp->b_data,
379 nbp->b_bcount));
380 if (nbp->b_error != 0) {
381 obp->b_error = nbp->b_error;
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 putiobuf(nbp);
400
401 /* Request is complete for whatever reason */
402 obp->b_resid = 0;
403 if (obp->b_error != 0)
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, void *data, int flag, struct lwp *l)
443 {
444 struct cgd_softc *cs;
445 struct dk_softc *dksc;
446 struct disk *dk;
447 int ret;
448 int part = DISKPART(dev);
449 int pmask = 1 << part;
450
451 DPRINTF_FOLLOW(("cgdioctl(%d, %ld, %p, %d, %p)\n",
452 dev, cmd, data, flag, l));
453 GETCGD_SOFTC(cs, dev);
454 dksc = &cs->sc_dksc;
455 dk = &dksc->sc_dkdev;
456 switch (cmd) {
457 case CGDIOCSET:
458 case CGDIOCCLR:
459 if ((flag & FWRITE) == 0)
460 return EBADF;
461 }
462
463 switch (cmd) {
464 case CGDIOCSET:
465 if (dksc->sc_flags & DKF_INITED)
466 ret = EBUSY;
467 else
468 ret = cgd_ioctl_set(cs, data, l);
469 break;
470 case CGDIOCCLR:
471 if (!(dksc->sc_flags & DKF_INITED)) {
472 ret = ENXIO;
473 break;
474 }
475 if (DK_BUSY(&cs->sc_dksc, pmask)) {
476 ret = EBUSY;
477 break;
478 }
479 ret = cgd_ioctl_clr(cs, data, l);
480 break;
481 default:
482 ret = dk_ioctl(di, dksc, dev, cmd, data, flag, l);
483 break;
484 }
485
486 return ret;
487 }
488
489 static int
490 cgddump(dev_t dev, daddr_t blkno, void *va, size_t size)
491 {
492 struct cgd_softc *cs;
493
494 DPRINTF_FOLLOW(("cgddump(%d, %" PRId64 ", %p, %lu)\n", dev, blkno, va,
495 (unsigned long)size));
496 GETCGD_SOFTC(cs, dev);
497 return dk_dump(di, &cs->sc_dksc, dev, blkno, va, size);
498 }
499
500 /*
501 * XXXrcd:
502 * for now we hardcode the maximum key length.
503 */
504 #define MAX_KEYSIZE 1024
505
506 /* ARGSUSED */
507 static int
508 cgd_ioctl_set(struct cgd_softc *cs, void *data, struct lwp *l)
509 {
510 struct cgd_ioctl *ci = data;
511 struct vnode *vp;
512 int ret;
513 size_t keybytes; /* key length in bytes */
514 const char *cp;
515 char *inbuf;
516
517 cp = ci->ci_disk;
518 if ((ret = dk_lookup(cp, l, &vp, UIO_USERSPACE)) != 0)
519 return ret;
520
521 inbuf = malloc(MAX_KEYSIZE, M_TEMP, M_WAITOK);
522
523 if ((ret = cgdinit(cs, cp, vp, l)) != 0)
524 goto bail;
525
526 (void)memset(inbuf, 0, MAX_KEYSIZE);
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 (void)memset(inbuf, 0, MAX_KEYSIZE);
538 ret = copyinstr(ci->ci_ivmethod, inbuf, MAX_KEYSIZE, 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 (void)memset(inbuf, 0, MAX_KEYSIZE);
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 (void)memset(inbuf, 0, MAX_KEYSIZE);
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 free(inbuf, M_TEMP);
567
568 bufq_alloc(&cs->sc_dksc.sc_bufq, "fcfs", 0);
569
570 cs->sc_data = malloc(MAXPHYS, M_DEVBUF, M_WAITOK);
571 cs->sc_data_used = 0;
572
573 cs->sc_dksc.sc_flags |= DKF_INITED;
574
575 /* Attach the disk. */
576 disk_attach(&cs->sc_dksc.sc_dkdev);
577
578 /* Try and read the disklabel. */
579 dk_getdisklabel(di, &cs->sc_dksc, 0 /* XXX ? */);
580
581 /* Discover wedges on this disk. */
582 dkwedge_discover(&cs->sc_dksc.sc_dkdev);
583
584 return 0;
585
586 bail:
587 free(inbuf, M_TEMP);
588 (void)vn_close(vp, FREAD|FWRITE, l->l_cred);
589 return ret;
590 }
591
592 /* ARGSUSED */
593 static int
594 cgd_ioctl_clr(struct cgd_softc *cs, void *data, struct lwp *l)
595 {
596 int s;
597
598 /* Delete all of our wedges. */
599 dkwedge_delall(&cs->sc_dksc.sc_dkdev);
600
601 /* Kill off any queued buffers. */
602 s = splbio();
603 bufq_drain(cs->sc_dksc.sc_bufq);
604 splx(s);
605 bufq_free(cs->sc_dksc.sc_bufq);
606
607 (void)vn_close(cs->sc_tvn, FREAD|FWRITE, l->l_cred);
608 cs->sc_cfuncs->cf_destroy(cs->sc_cdata.cf_priv);
609 free(cs->sc_tpath, M_DEVBUF);
610 free(cs->sc_data, M_DEVBUF);
611 cs->sc_data_used = 0;
612 cs->sc_dksc.sc_flags &= ~DKF_INITED;
613 disk_detach(&cs->sc_dksc.sc_dkdev);
614
615 return 0;
616 }
617
618 static int
619 cgdinit(struct cgd_softc *cs, const char *cpath, struct vnode *vp,
620 struct lwp *l)
621 {
622 struct dk_geom *pdg;
623 struct partinfo dpart;
624 struct vattr va;
625 size_t size;
626 int maxsecsize = 0;
627 int ret;
628 char *tmppath;
629
630 cs->sc_dksc.sc_size = 0;
631 cs->sc_tvn = vp;
632 cs->sc_tpath = NULL;
633
634 tmppath = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
635 ret = copyinstr(cpath, tmppath, MAXPATHLEN, &cs->sc_tpathlen);
636 if (ret)
637 goto bail;
638 cs->sc_tpath = malloc(cs->sc_tpathlen, M_DEVBUF, M_WAITOK);
639 memcpy(cs->sc_tpath, tmppath, cs->sc_tpathlen);
640
641 if ((ret = VOP_GETATTR(vp, &va, l->l_cred)) != 0)
642 goto bail;
643
644 cs->sc_tdev = va.va_rdev;
645
646 ret = VOP_IOCTL(vp, DIOCGPART, &dpart, FREAD, l->l_cred);
647 if (ret)
648 goto bail;
649
650 maxsecsize =
651 ((dpart.disklab->d_secsize > maxsecsize) ?
652 dpart.disklab->d_secsize : maxsecsize);
653 size = dpart.part->p_size;
654
655 if (!size) {
656 ret = ENODEV;
657 goto bail;
658 }
659
660 cs->sc_dksc.sc_size = size;
661
662 /*
663 * XXX here we should probe the underlying device. If we
664 * are accessing a partition of type RAW_PART, then
665 * we should populate our initial geometry with the
666 * geometry that we discover from the device.
667 */
668 pdg = &cs->sc_dksc.sc_geom;
669 pdg->pdg_secsize = DEV_BSIZE;
670 pdg->pdg_ntracks = 1;
671 pdg->pdg_nsectors = 1024 * (1024 / pdg->pdg_secsize);
672 pdg->pdg_ncylinders = cs->sc_dksc.sc_size / pdg->pdg_nsectors;
673
674 bail:
675 free(tmppath, M_TEMP);
676 if (ret && cs->sc_tpath)
677 free(cs->sc_tpath, M_DEVBUF);
678 return ret;
679 }
680
681 /*
682 * Our generic cipher entry point. This takes care of the
683 * IV mode and passes off the work to the specific cipher.
684 * We implement here the IV method ``encrypted block
685 * number''.
686 *
687 * For the encryption case, we accomplish this by setting
688 * up a struct uio where the first iovec of the source is
689 * the blocknumber and the first iovec of the dest is a
690 * sink. We then call the cipher with an IV of zero, and
691 * the right thing happens.
692 *
693 * For the decryption case, we use the same basic mechanism
694 * for symmetry, but we encrypt the block number in the
695 * first iovec.
696 *
697 * We mainly do this to avoid requiring the definition of
698 * an ECB mode.
699 *
700 * XXXrcd: for now we rely on our own crypto framework defined
701 * in dev/cgd_crypto.c. This will change when we
702 * get a generic kernel crypto framework.
703 */
704
705 static void
706 blkno2blkno_buf(char *sbuf, daddr_t blkno)
707 {
708 int i;
709
710 /* Set up the blkno in blkno_buf, here we do not care much
711 * about the final layout of the information as long as we
712 * can guarantee that each sector will have a different IV
713 * and that the endianness of the machine will not affect
714 * the representation that we have chosen.
715 *
716 * We choose this representation, because it does not rely
717 * on the size of buf (which is the blocksize of the cipher),
718 * but allows daddr_t to grow without breaking existing
719 * disks.
720 *
721 * Note that blkno2blkno_buf does not take a size as input,
722 * and hence must be called on a pre-zeroed buffer of length
723 * greater than or equal to sizeof(daddr_t).
724 */
725 for (i=0; i < sizeof(daddr_t); i++) {
726 *sbuf++ = blkno & 0xff;
727 blkno >>= 8;
728 }
729 }
730
731 static void
732 cgd_cipher(struct cgd_softc *cs, void *dstv, void *srcv,
733 size_t len, daddr_t blkno, size_t secsize, int dir)
734 {
735 char *dst = dstv;
736 char *src = srcv;
737 cfunc_cipher *cipher = cs->sc_cfuncs->cf_cipher;
738 struct uio dstuio;
739 struct uio srcuio;
740 struct iovec dstiov[2];
741 struct iovec srciov[2];
742 size_t blocksize = cs->sc_cdata.cf_blocksize;
743 char sink[blocksize];
744 char zero_iv[blocksize];
745 char blkno_buf[blocksize];
746
747 DPRINTF_FOLLOW(("cgd_cipher() dir=%d\n", dir));
748
749 DIAGCONDPANIC(len % blocksize != 0,
750 ("cgd_cipher: len %% blocksize != 0"));
751
752 /* ensure that sizeof(daddr_t) <= blocksize (for encblkno IVing) */
753 DIAGCONDPANIC(sizeof(daddr_t) > blocksize,
754 ("cgd_cipher: sizeof(daddr_t) > blocksize"));
755
756 memset(zero_iv, 0x0, sizeof(zero_iv));
757
758 dstuio.uio_iov = dstiov;
759 dstuio.uio_iovcnt = 2;
760
761 srcuio.uio_iov = srciov;
762 srcuio.uio_iovcnt = 2;
763
764 dstiov[0].iov_base = sink;
765 dstiov[0].iov_len = blocksize;
766 srciov[0].iov_base = blkno_buf;
767 srciov[0].iov_len = blocksize;
768 dstiov[1].iov_len = secsize;
769 srciov[1].iov_len = secsize;
770
771 for (; len > 0; len -= secsize) {
772 dstiov[1].iov_base = dst;
773 srciov[1].iov_base = src;
774
775 memset(blkno_buf, 0x0, sizeof(blkno_buf));
776 blkno2blkno_buf(blkno_buf, blkno);
777 if (dir == CGD_CIPHER_DECRYPT) {
778 dstuio.uio_iovcnt = 1;
779 srcuio.uio_iovcnt = 1;
780 IFDEBUG(CGDB_CRYPTO, hexprint("step 0: blkno_buf",
781 blkno_buf, sizeof(blkno_buf)));
782 cipher(cs->sc_cdata.cf_priv, &dstuio, &srcuio,
783 zero_iv, CGD_CIPHER_ENCRYPT);
784 memcpy(blkno_buf, sink, blocksize);
785 dstuio.uio_iovcnt = 2;
786 srcuio.uio_iovcnt = 2;
787 }
788
789 IFDEBUG(CGDB_CRYPTO, hexprint("step 1: blkno_buf",
790 blkno_buf, sizeof(blkno_buf)));
791 cipher(cs->sc_cdata.cf_priv, &dstuio, &srcuio, zero_iv, dir);
792 IFDEBUG(CGDB_CRYPTO, hexprint("step 2: sink",
793 sink, sizeof(sink)));
794
795 dst += secsize;
796 src += secsize;
797 blkno++;
798 }
799 }
800
801 #ifdef DEBUG
802 static void
803 hexprint(const char *start, void *buf, int len)
804 {
805 char *c = buf;
806
807 DIAGCONDPANIC(len < 0, ("hexprint: called with len < 0"));
808 printf("%s: len=%06d 0x", start, len);
809 while (len--)
810 printf("%02x", (unsigned char) *c++);
811 }
812 #endif
813