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