xref: /netbsd-src/sys/dev/cgd.c (revision e5548b402ae4c44fb816de42c7bba9581ce23ef5)
1 /* $NetBSD: cgd.c,v 1.32 2005/12/11 12:20:53 christos 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.32 2005/12/11 12:20:53 christos 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 *, 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 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 	cs->sc_dksc.sc_dkdev.dk_driver = &cgddkdriver;
181 	pseudo_disk_init(&cs->sc_dksc.sc_dkdev);
182 }
183 
184 void
185 cgdattach(int num)
186 {
187 	int	i;
188 
189 	DPRINTF_FOLLOW(("cgdattach(%d)\n", num));
190 	if (num <= 0) {
191 		DIAGPANIC(("cgdattach: count <= 0"));
192 		return;
193 	}
194 
195 	cgd_softc = (void *)malloc(num * sizeof(*cgd_softc), M_DEVBUF, M_NOWAIT);
196 	if (!cgd_softc) {
197 		printf("WARNING: unable to malloc(9) memory for crypt disks\n");
198 		DIAGPANIC(("cgdattach: cannot malloc(9) enough memory"));
199 		return;
200 	}
201 
202 	numcgd = num;
203 	for (i=0; i<num; i++)
204 		cgdsoftc_init(&cgd_softc[i], i);
205 }
206 
207 static int
208 cgdopen(dev_t dev, int flags, int fmt, struct lwp *l)
209 {
210 	struct	cgd_softc *cs;
211 
212 	DPRINTF_FOLLOW(("cgdopen(%d, %d)\n", dev, flags));
213 	GETCGD_SOFTC(cs, dev);
214 	return dk_open(di, &cs->sc_dksc, dev, flags, fmt, l);
215 }
216 
217 static int
218 cgdclose(dev_t dev, int flags, int fmt, struct lwp *l)
219 {
220 	struct	cgd_softc *cs;
221 
222 	DPRINTF_FOLLOW(("cgdclose(%d, %d)\n", dev, flags));
223 	GETCGD_SOFTC(cs, dev);
224 	return dk_close(di, &cs->sc_dksc, dev, flags, fmt, l);
225 }
226 
227 static void
228 cgdstrategy(struct buf *bp)
229 {
230 	struct	cgd_softc *cs = getcgd_softc(bp->b_dev);
231 
232 	DPRINTF_FOLLOW(("cgdstrategy(%p): b_bcount = %ld\n", bp,
233 	    (long)bp->b_bcount));
234 	/* XXXrcd: Should we test for (cs != NULL)? */
235 	dk_strategy(di, &cs->sc_dksc, bp);
236 	return;
237 }
238 
239 static int
240 cgdsize(dev_t dev)
241 {
242 	struct cgd_softc *cs = getcgd_softc(dev);
243 
244 	DPRINTF_FOLLOW(("cgdsize(%d)\n", dev));
245 	if (!cs)
246 		return -1;
247 	return dk_size(di, &cs->sc_dksc, dev);
248 }
249 
250 /*
251  * cgd_{get,put}data are functions that deal with getting a buffer
252  * for the new encrypted data.  We have a buffer per device so that
253  * we can ensure that we can always have a transaction in flight.
254  * We use this buffer first so that we have one less piece of
255  * malloc'ed data at any given point.
256  */
257 
258 static void *
259 cgd_getdata(struct dk_softc *dksc, unsigned long size)
260 {
261 	struct	cgd_softc *cs =dksc->sc_osc;
262 	caddr_t	data = NULL;
263 
264 	simple_lock(&cs->sc_slock);
265 	if (cs->sc_data_used == 0) {
266 		cs->sc_data_used = 1;
267 		data = cs->sc_data;
268 	}
269 	simple_unlock(&cs->sc_slock);
270 
271 	if (data)
272 		return data;
273 
274 	return malloc(size, M_DEVBUF, M_NOWAIT);
275 }
276 
277 static void
278 cgd_putdata(struct dk_softc *dksc, caddr_t data)
279 {
280 	struct	cgd_softc *cs =dksc->sc_osc;
281 
282 	if (data == cs->sc_data) {
283 		simple_lock(&cs->sc_slock);
284 		cs->sc_data_used = 0;
285 		simple_unlock(&cs->sc_slock);
286 	} else {
287 		free(data, M_DEVBUF);
288 	}
289 }
290 
291 static int
292 cgdstart(struct dk_softc *dksc, struct buf *bp)
293 {
294 	struct	cgd_softc *cs = dksc->sc_osc;
295 	struct	buf *nbp;
296 	caddr_t	addr;
297 	caddr_t	newaddr;
298 	daddr_t	bn;
299 	int s;
300 
301 	DPRINTF_FOLLOW(("cgdstart(%p, %p)\n", dksc, bp));
302 	disk_busy(&dksc->sc_dkdev); /* XXX: put in dksubr.c */
303 
304 	bn = bp->b_rawblkno;
305 
306 	/*
307 	 * We attempt to allocate all of our resources up front, so that
308 	 * we can fail quickly if they are unavailable.
309 	 */
310 
311 	s = splbio();
312 	nbp = pool_get(&bufpool, PR_NOWAIT);
313 	splx(s);
314 	if (nbp == NULL) {
315 		disk_unbusy(&dksc->sc_dkdev, 0, (bp->b_flags & B_READ));
316 		return -1;
317 	}
318 
319 	/*
320 	 * If we are writing, then we need to encrypt the outgoing
321 	 * block into a new block of memory.  If we fail, then we
322 	 * return an error and let the dksubr framework deal with it.
323 	 */
324 	newaddr = addr = bp->b_data;
325 	if ((bp->b_flags & B_READ) == 0) {
326 		newaddr = cgd_getdata(dksc, bp->b_bcount);
327 		if (!newaddr) {
328 			s = splbio();
329 			pool_put(&bufpool, nbp);
330 			splx(s);
331 			disk_unbusy(&dksc->sc_dkdev, 0, (bp->b_flags & B_READ));
332 			return -1;
333 		}
334 		cgd_cipher(cs, newaddr, addr, bp->b_bcount, bn,
335 		    DEV_BSIZE, CGD_CIPHER_ENCRYPT);
336 	}
337 
338 	BUF_INIT(nbp);
339 	nbp->b_data = newaddr;
340 	nbp->b_flags = bp->b_flags | B_CALL;
341 	nbp->b_iodone = cgdiodone;
342 	nbp->b_proc = bp->b_proc;
343 	nbp->b_blkno = bn;
344 	nbp->b_vp = cs->sc_tvn;
345 	nbp->b_bcount = bp->b_bcount;
346 	nbp->b_private = bp;
347 
348 	BIO_COPYPRIO(nbp, bp);
349 
350 	if ((nbp->b_flags & B_READ) == 0) {
351 		V_INCR_NUMOUTPUT(nbp->b_vp);
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%x, 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_flags & B_ERROR) {
374 		obp->b_flags |= B_ERROR;
375 		obp->b_error  = nbp->b_error ? nbp->b_error : EIO;
376 
377 		printf("%s: error %d\n", dksc->sc_xname, obp->b_error);
378 	}
379 
380 	/* Perform the decryption if we are reading.
381 	 *
382 	 * Note: use the blocknumber from nbp, since it is what
383 	 *       we used to encrypt the blocks.
384 	 */
385 
386 	if (nbp->b_flags & B_READ)
387 		cgd_cipher(cs, obp->b_data, obp->b_data, obp->b_bcount,
388 		    nbp->b_blkno, DEV_BSIZE, CGD_CIPHER_DECRYPT);
389 
390 	/* If we allocated memory, free it now... */
391 	if (nbp->b_data != obp->b_data)
392 		cgd_putdata(dksc, nbp->b_data);
393 
394 	pool_put(&bufpool, nbp);
395 
396 	/* Request is complete for whatever reason */
397 	obp->b_resid = 0;
398 	if (obp->b_flags & B_ERROR)
399 		obp->b_resid = obp->b_bcount;
400 	disk_unbusy(&dksc->sc_dkdev, obp->b_bcount - obp->b_resid,
401 	    (obp->b_flags & B_READ));
402 	biodone(obp);
403 	dk_iodone(di, dksc);
404 }
405 
406 /* XXX: we should probably put these into dksubr.c, mostly */
407 static int
408 cgdread(dev_t dev, struct uio *uio, int flags)
409 {
410 	struct	cgd_softc *cs;
411 	struct	dk_softc *dksc;
412 
413 	DPRINTF_FOLLOW(("cgdread(%d, %p, %d)\n", dev, uio, flags));
414 	GETCGD_SOFTC(cs, dev);
415 	dksc = &cs->sc_dksc;
416 	if ((dksc->sc_flags & DKF_INITED) == 0)
417 		return ENXIO;
418 	return physio(cgdstrategy, NULL, dev, B_READ, minphys, uio);
419 }
420 
421 /* XXX: we should probably put these into dksubr.c, mostly */
422 static int
423 cgdwrite(dev_t dev, struct uio *uio, int flags)
424 {
425 	struct	cgd_softc *cs;
426 	struct	dk_softc *dksc;
427 
428 	DPRINTF_FOLLOW(("cgdwrite(%d, %p, %d)\n", dev, uio, flags));
429 	GETCGD_SOFTC(cs, dev);
430 	dksc = &cs->sc_dksc;
431 	if ((dksc->sc_flags & DKF_INITED) == 0)
432 		return ENXIO;
433 	return physio(cgdstrategy, NULL, dev, B_WRITE, minphys, uio);
434 }
435 
436 static int
437 cgdioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
438 {
439 	struct	cgd_softc *cs;
440 	struct	dk_softc *dksc;
441 	struct	disk *dk;
442 	int	ret;
443 	int	part = DISKPART(dev);
444 	int	pmask = 1 << part;
445 
446 	DPRINTF_FOLLOW(("cgdioctl(%d, %ld, %p, %d, %p)\n",
447 	    dev, cmd, data, flag, l));
448 	GETCGD_SOFTC(cs, dev);
449 	dksc = &cs->sc_dksc;
450 	dk = &dksc->sc_dkdev;
451 	switch (cmd) {
452 	case CGDIOCSET:
453 	case CGDIOCCLR:
454 		if ((flag & FWRITE) == 0)
455 			return EBADF;
456 	}
457 
458 	switch (cmd) {
459 	case CGDIOCSET:
460 		if (dksc->sc_flags & DKF_INITED)
461 			ret = EBUSY;
462 		else
463 			ret = cgd_ioctl_set(cs, data, l);
464 		break;
465 	case CGDIOCCLR:
466 		if (!(dksc->sc_flags & DKF_INITED)) {
467 			ret = ENXIO;
468 			break;
469 		}
470 		if (DK_BUSY(&cs->sc_dksc, pmask)) {
471 			ret = EBUSY;
472 			break;
473 		}
474 		ret = cgd_ioctl_clr(cs, data, l);
475 		break;
476 	default:
477 		ret = dk_ioctl(di, dksc, dev, cmd, data, flag, l);
478 		break;
479 	}
480 
481 	return ret;
482 }
483 
484 static int
485 cgddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
486 {
487 	struct	cgd_softc *cs;
488 
489 	DPRINTF_FOLLOW(("cgddump(%d, %" PRId64 ", %p, %lu)\n", dev, blkno, va,
490 	    (unsigned long)size));
491 	GETCGD_SOFTC(cs, dev);
492 	return dk_dump(di, &cs->sc_dksc, dev, blkno, va, size);
493 }
494 
495 /*
496  * XXXrcd:
497  *  for now we hardcode the maximum key length.
498  */
499 #define MAX_KEYSIZE	1024
500 
501 /* ARGSUSED */
502 static int
503 cgd_ioctl_set(struct cgd_softc *cs, void *data, struct lwp *l)
504 {
505 	struct	 cgd_ioctl *ci = data;
506 	struct	 vnode *vp;
507 	int	 ret;
508 	int	 keybytes;			/* key length in bytes */
509 	const char *cp;
510 	char	 inbuf[MAX_KEYSIZE];
511 
512 	cp = ci->ci_disk;
513 	if ((ret = dk_lookup(cp, l, &vp)) != 0)
514 		return ret;
515 
516 	if ((ret = cgdinit(cs, cp, vp, l)) != 0)
517 		goto bail;
518 
519 	memset(inbuf, 0x0, sizeof(inbuf));
520 	ret = copyinstr(ci->ci_alg, inbuf, 256, NULL);
521 	if (ret)
522 		goto bail;
523 	cs->sc_cfuncs = cryptfuncs_find(inbuf);
524 	if (!cs->sc_cfuncs) {
525 		ret = EINVAL;
526 		goto bail;
527 	}
528 
529 	/* right now we only support encblkno, so hard-code it */
530 	memset(inbuf, 0x0, sizeof(inbuf));
531 	ret = copyinstr(ci->ci_ivmethod, inbuf, sizeof(inbuf), NULL);
532 	if (ret)
533 		goto bail;
534 	if (strcmp("encblkno", inbuf)) {
535 		ret = EINVAL;
536 		goto bail;
537 	}
538 
539 	keybytes = ci->ci_keylen / 8 + 1;
540 	if (keybytes > MAX_KEYSIZE) {
541 		ret = EINVAL;
542 		goto bail;
543 	}
544 	memset(inbuf, 0x0, sizeof(inbuf));
545 	ret = copyin(ci->ci_key, inbuf, keybytes);
546 	if (ret)
547 		goto bail;
548 
549 	cs->sc_cdata.cf_blocksize = ci->ci_blocksize;
550 	cs->sc_cdata.cf_mode = CGD_CIPHER_CBC_ENCBLKNO;
551 	cs->sc_cdata.cf_priv = cs->sc_cfuncs->cf_init(ci->ci_keylen, inbuf,
552 	    &cs->sc_cdata.cf_blocksize);
553 	memset(inbuf, 0x0, sizeof(inbuf));
554 	if (!cs->sc_cdata.cf_priv) {
555 		printf("cgd: unable to initialize cipher\n");
556 		ret = EINVAL;		/* XXX is this the right error? */
557 		goto bail;
558 	}
559 
560 	bufq_alloc(&cs->sc_dksc.sc_bufq, "fcfs", 0);
561 
562 	cs->sc_data = malloc(MAXPHYS, M_DEVBUF, M_WAITOK);
563 	cs->sc_data_used = 0;
564 
565 	cs->sc_dksc.sc_flags |= DKF_INITED;
566 
567 	/* Attach the disk. */
568 	pseudo_disk_attach(&cs->sc_dksc.sc_dkdev);
569 
570 	/* Try and read the disklabel. */
571 	dk_getdisklabel(di, &cs->sc_dksc, 0 /* XXX ? */);
572 
573 	/* Discover wedges on this disk. */
574 	dkwedge_discover(&cs->sc_dksc.sc_dkdev);
575 
576 	return 0;
577 
578 bail:
579 	(void)vn_close(vp, FREAD|FWRITE, l->l_proc->p_ucred, 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_proc->p_ucred, 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 	pseudo_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[MAXPATHLEN];
620 
621 	cs->sc_dksc.sc_size = 0;
622 	cs->sc_tvn = vp;
623 
624 	memset(tmppath, 0x0, sizeof(tmppath));
625 	ret = copyinstr(cpath, tmppath, MAXPATHLEN, &cs->sc_tpathlen);
626 	if (ret)
627 		goto bail;
628 	cs->sc_tpath = malloc(cs->sc_tpathlen, M_DEVBUF, M_WAITOK);
629 	memcpy(cs->sc_tpath, tmppath, cs->sc_tpathlen);
630 
631 	if ((ret = VOP_GETATTR(vp, &va, l->l_proc->p_ucred, l)) != 0)
632 		goto bail;
633 
634 	cs->sc_tdev = va.va_rdev;
635 
636 	ret = VOP_IOCTL(vp, DIOCGPART, &dpart, FREAD, l->l_proc->p_ucred, l);
637 	if (ret)
638 		goto bail;
639 
640 	maxsecsize =
641 	    ((dpart.disklab->d_secsize > maxsecsize) ?
642 	    dpart.disklab->d_secsize : maxsecsize);
643 	size = dpart.part->p_size;
644 
645 	if (!size) {
646 		ret = ENODEV;
647 		goto bail;
648 	}
649 
650 	cs->sc_dksc.sc_size = size;
651 
652 	/*
653 	 * XXX here we should probe the underlying device.  If we
654 	 *     are accessing a partition of type RAW_PART, then
655 	 *     we should populate our initial geometry with the
656 	 *     geometry that we discover from the device.
657 	 */
658 	pdg = &cs->sc_dksc.sc_geom;
659 	pdg->pdg_secsize = DEV_BSIZE;
660 	pdg->pdg_ntracks = 1;
661 	pdg->pdg_nsectors = 1024 * (1024 / pdg->pdg_secsize);
662 	pdg->pdg_ncylinders = cs->sc_dksc.sc_size / pdg->pdg_nsectors;
663 
664 bail:
665 	if (ret && cs->sc_tpath)
666 		free(cs->sc_tpath, M_DEVBUF);
667 	return ret;
668 }
669 
670 /*
671  * Our generic cipher entry point.  This takes care of the
672  * IV mode and passes off the work to the specific cipher.
673  * We implement here the IV method ``encrypted block
674  * number''.
675  *
676  * For the encryption case, we accomplish this by setting
677  * up a struct uio where the first iovec of the source is
678  * the blocknumber and the first iovec of the dest is a
679  * sink.  We then call the cipher with an IV of zero, and
680  * the right thing happens.
681  *
682  * For the decryption case, we use the same basic mechanism
683  * for symmetry, but we encrypt the block number in the
684  * first iovec.
685  *
686  * We mainly do this to avoid requiring the definition of
687  * an ECB mode.
688  *
689  * XXXrcd: for now we rely on our own crypto framework defined
690  *         in dev/cgd_crypto.c.  This will change when we
691  *         get a generic kernel crypto framework.
692  */
693 
694 static void
695 blkno2blkno_buf(char *sbuf, daddr_t blkno)
696 {
697 	int	i;
698 
699 	/* Set up the blkno in blkno_buf, here we do not care much
700 	 * about the final layout of the information as long as we
701 	 * can guarantee that each sector will have a different IV
702 	 * and that the endianness of the machine will not affect
703 	 * the representation that we have chosen.
704 	 *
705 	 * We choose this representation, because it does not rely
706 	 * on the size of buf (which is the blocksize of the cipher),
707 	 * but allows daddr_t to grow without breaking existing
708 	 * disks.
709 	 *
710 	 * Note that blkno2blkno_buf does not take a size as input,
711 	 * and hence must be called on a pre-zeroed buffer of length
712 	 * greater than or equal to sizeof(daddr_t).
713 	 */
714 	for (i=0; i < sizeof(daddr_t); i++) {
715 		*sbuf++ = blkno & 0xff;
716 		blkno >>= 8;
717 	}
718 }
719 
720 static void
721 cgd_cipher(struct cgd_softc *cs, caddr_t dst, caddr_t src,
722 	   size_t len, daddr_t blkno, size_t secsize, int dir)
723 {
724 	cfunc_cipher	*cipher = cs->sc_cfuncs->cf_cipher;
725 	struct uio	dstuio;
726 	struct uio	srcuio;
727 	struct iovec	dstiov[2];
728 	struct iovec	srciov[2];
729 	int		blocksize = cs->sc_cdata.cf_blocksize;
730 	char		sink[blocksize];
731 	char		zero_iv[blocksize];
732 	char		blkno_buf[blocksize];
733 
734 	DPRINTF_FOLLOW(("cgd_cipher() dir=%d\n", dir));
735 
736 	DIAGCONDPANIC(len % blocksize != 0,
737 	    ("cgd_cipher: len %% blocksize != 0"));
738 
739 	/* ensure that sizeof(daddr_t) <= blocksize (for encblkno IVing) */
740 	DIAGCONDPANIC(sizeof(daddr_t) > blocksize,
741 	    ("cgd_cipher: sizeof(daddr_t) > blocksize"));
742 
743 	memset(zero_iv, 0x0, sizeof(zero_iv));
744 
745 	dstuio.uio_iov = dstiov;
746 	dstuio.uio_iovcnt = 2;
747 
748 	srcuio.uio_iov = srciov;
749 	srcuio.uio_iovcnt = 2;
750 
751 	dstiov[0].iov_base = sink;
752 	dstiov[0].iov_len  = blocksize;
753 	srciov[0].iov_base = blkno_buf;
754 	srciov[0].iov_len  = blocksize;
755 	dstiov[1].iov_len  = secsize;
756 	srciov[1].iov_len  = secsize;
757 
758 	for (; len > 0; len -= secsize) {
759 		dstiov[1].iov_base = dst;
760 		srciov[1].iov_base = src;
761 
762 		memset(blkno_buf, 0x0, sizeof(blkno_buf));
763 		blkno2blkno_buf(blkno_buf, blkno);
764 		if (dir == CGD_CIPHER_DECRYPT) {
765 			dstuio.uio_iovcnt = 1;
766 			srcuio.uio_iovcnt = 1;
767 			IFDEBUG(CGDB_CRYPTO, hexprint("step 0: blkno_buf",
768 			    blkno_buf, sizeof(blkno_buf)));
769 			cipher(cs->sc_cdata.cf_priv, &dstuio, &srcuio,
770 			    zero_iv, CGD_CIPHER_ENCRYPT);
771 			memcpy(blkno_buf, sink, blocksize);
772 			dstuio.uio_iovcnt = 2;
773 			srcuio.uio_iovcnt = 2;
774 		}
775 
776 		IFDEBUG(CGDB_CRYPTO, hexprint("step 1: blkno_buf",
777 		    blkno_buf, sizeof(blkno_buf)));
778 		cipher(cs->sc_cdata.cf_priv, &dstuio, &srcuio, zero_iv, dir);
779 		IFDEBUG(CGDB_CRYPTO, hexprint("step 2: sink",
780 		    sink, sizeof(sink)));
781 
782 		dst += secsize;
783 		src += secsize;
784 		blkno++;
785 	}
786 }
787 
788 #ifdef DEBUG
789 static void
790 hexprint(const char *start, void *buf, int len)
791 {
792 	char	*c = buf;
793 
794 	DIAGCONDPANIC(len < 0, ("hexprint: called with len < 0"));
795 	printf("%s: len=%06d 0x", start, len);
796 	while (len--)
797 		printf("%02x", (unsigned) *c++);
798 }
799 #endif
800