crypto.c revision 1.48.4.1 1 /* $NetBSD: crypto.c,v 1.48.4.1 2017/04/21 16:54:07 bouyer Exp $ */
2 /* $FreeBSD: src/sys/opencrypto/crypto.c,v 1.4.2.5 2003/02/26 00:14:05 sam Exp $ */
3 /* $OpenBSD: crypto.c,v 1.41 2002/07/17 23:52:38 art Exp $ */
4
5 /*-
6 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Coyote Point Systems, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /*
35 * The author of this code is Angelos D. Keromytis (angelos (at) cis.upenn.edu)
36 *
37 * This code was written by Angelos D. Keromytis in Athens, Greece, in
38 * February 2000. Network Security Technologies Inc. (NSTI) kindly
39 * supported the development of this code.
40 *
41 * Copyright (c) 2000, 2001 Angelos D. Keromytis
42 *
43 * Permission to use, copy, and modify this software with or without fee
44 * is hereby granted, provided that this entire notice is included in
45 * all source code copies of any software which is or includes a copy or
46 * modification of this software.
47 *
48 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
49 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
50 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
51 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
52 * PURPOSE.
53 */
54
55 #include <sys/cdefs.h>
56 __KERNEL_RCSID(0, "$NetBSD: crypto.c,v 1.48.4.1 2017/04/21 16:54:07 bouyer Exp $");
57
58 #include <sys/param.h>
59 #include <sys/reboot.h>
60 #include <sys/systm.h>
61 #include <sys/malloc.h>
62 #include <sys/proc.h>
63 #include <sys/pool.h>
64 #include <sys/kthread.h>
65 #include <sys/once.h>
66 #include <sys/sysctl.h>
67 #include <sys/intr.h>
68 #include <sys/errno.h>
69 #include <sys/module.h>
70
71 #if defined(_KERNEL_OPT)
72 #include "opt_ocf.h"
73 #endif
74
75 #include <opencrypto/cryptodev.h>
76 #include <opencrypto/xform.h> /* XXX for M_XDATA */
77
78 static kmutex_t crypto_q_mtx;
79 static kmutex_t crypto_ret_q_mtx;
80 static kcondvar_t cryptoret_cv;
81 kmutex_t crypto_mtx;
82
83 /* below are kludges for residual code wrtitten to FreeBSD interfaces */
84 #define SWI_CRYPTO 17
85 #define register_swi(lvl, fn) \
86 softint_establish(SOFTINT_NET|SOFTINT_MPSAFE, (void (*)(void *))fn, NULL)
87 #define unregister_swi(lvl, fn) softint_disestablish(softintr_cookie)
88 #define setsoftcrypto(x) softint_schedule(x)
89
90 int crypto_ret_q_check(struct cryptop *);
91
92 /*
93 * Crypto drivers register themselves by allocating a slot in the
94 * crypto_drivers table with crypto_get_driverid() and then registering
95 * each algorithm they support with crypto_register() and crypto_kregister().
96 */
97 static struct cryptocap *crypto_drivers;
98 static int crypto_drivers_num;
99 static void *softintr_cookie;
100 static int crypto_exit_flag;
101
102 /*
103 * There are two queues for crypto requests; one for symmetric (e.g.
104 * cipher) operations and one for asymmetric (e.g. MOD) operations.
105 * See below for how synchronization is handled.
106 */
107 static TAILQ_HEAD(,cryptop) crp_q = /* request queues */
108 TAILQ_HEAD_INITIALIZER(crp_q);
109 static TAILQ_HEAD(,cryptkop) crp_kq =
110 TAILQ_HEAD_INITIALIZER(crp_kq);
111
112 /*
113 * There are two queues for processing completed crypto requests; one
114 * for the symmetric and one for the asymmetric ops. We only need one
115 * but have two to avoid type futzing (cryptop vs. cryptkop). See below
116 * for how synchronization is handled.
117 */
118 static TAILQ_HEAD(crprethead, cryptop) crp_ret_q = /* callback queues */
119 TAILQ_HEAD_INITIALIZER(crp_ret_q);
120 static TAILQ_HEAD(krprethead, cryptkop) crp_ret_kq =
121 TAILQ_HEAD_INITIALIZER(crp_ret_kq);
122
123 /*
124 * Crypto op and desciptor data structures are allocated
125 * from separate private zones(FreeBSD)/pools(netBSD/OpenBSD) .
126 */
127 struct pool cryptop_pool;
128 struct pool cryptodesc_pool;
129 struct pool cryptkop_pool;
130
131 int crypto_usercrypto = 1; /* userland may open /dev/crypto */
132 int crypto_userasymcrypto = 1; /* userland may do asym crypto reqs */
133 /*
134 * cryptodevallowsoft is (intended to be) sysctl'able, controlling
135 * access to hardware versus software transforms as below:
136 *
137 * crypto_devallowsoft < 0: Force userlevel requests to use software
138 * transforms, always
139 * crypto_devallowsoft = 0: Use hardware if present, grant userlevel
140 * requests for non-accelerated transforms
141 * (handling the latter in software)
142 * crypto_devallowsoft > 0: Allow user requests only for transforms which
143 * are hardware-accelerated.
144 */
145 int crypto_devallowsoft = 1; /* only use hardware crypto */
146
147 SYSCTL_SETUP(sysctl_opencrypto_setup, "sysctl opencrypto subtree setup")
148 {
149
150 sysctl_createv(clog, 0, NULL, NULL,
151 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
152 CTLTYPE_INT, "usercrypto",
153 SYSCTL_DESCR("Enable/disable user-mode access to "
154 "crypto support"),
155 NULL, 0, &crypto_usercrypto, 0,
156 CTL_KERN, CTL_CREATE, CTL_EOL);
157 sysctl_createv(clog, 0, NULL, NULL,
158 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
159 CTLTYPE_INT, "userasymcrypto",
160 SYSCTL_DESCR("Enable/disable user-mode access to "
161 "asymmetric crypto support"),
162 NULL, 0, &crypto_userasymcrypto, 0,
163 CTL_KERN, CTL_CREATE, CTL_EOL);
164 sysctl_createv(clog, 0, NULL, NULL,
165 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
166 CTLTYPE_INT, "cryptodevallowsoft",
167 SYSCTL_DESCR("Enable/disable use of software "
168 "asymmetric crypto support"),
169 NULL, 0, &crypto_devallowsoft, 0,
170 CTL_KERN, CTL_CREATE, CTL_EOL);
171 }
172
173 MALLOC_DEFINE(M_CRYPTO_DATA, "crypto", "crypto session records");
174
175 /*
176 * Synchronization: read carefully, this is non-trivial.
177 *
178 * Crypto requests are submitted via crypto_dispatch. Typically
179 * these come in from network protocols at spl0 (output path) or
180 * spl[,soft]net (input path).
181 *
182 * Requests are typically passed on the driver directly, but they
183 * may also be queued for processing by a software interrupt thread,
184 * cryptointr, that runs at splsoftcrypto. This thread dispatches
185 * the requests to crypto drivers (h/w or s/w) who call crypto_done
186 * when a request is complete. Hardware crypto drivers are assumed
187 * to register their IRQ's as network devices so their interrupt handlers
188 * and subsequent "done callbacks" happen at spl[imp,net].
189 *
190 * Completed crypto ops are queued for a separate kernel thread that
191 * handles the callbacks at spl0. This decoupling insures the crypto
192 * driver interrupt service routine is not delayed while the callback
193 * takes place and that callbacks are delivered after a context switch
194 * (as opposed to a software interrupt that clients must block).
195 *
196 * This scheme is not intended for SMP machines.
197 */
198 static void cryptointr(void); /* swi thread to dispatch ops */
199 static void cryptoret(void); /* kernel thread for callbacks*/
200 static struct lwp *cryptothread;
201 static int crypto_destroy(bool);
202 static int crypto_invoke(struct cryptop *crp, int hint);
203 static int crypto_kinvoke(struct cryptkop *krp, int hint);
204
205 static struct cryptostats cryptostats;
206 #ifdef CRYPTO_TIMING
207 static int crypto_timing = 0;
208 #endif
209
210 #ifdef _MODULE
211 static struct sysctllog *sysctl_opencrypto_clog;
212 #endif
213
214 static int
215 crypto_init0(void)
216 {
217 int error;
218
219 mutex_init(&crypto_mtx, MUTEX_DEFAULT, IPL_NONE);
220 mutex_init(&crypto_q_mtx, MUTEX_DEFAULT, IPL_NET);
221 mutex_init(&crypto_ret_q_mtx, MUTEX_DEFAULT, IPL_NET);
222 cv_init(&cryptoret_cv, "crypto_w");
223 pool_init(&cryptop_pool, sizeof(struct cryptop), 0, 0,
224 0, "cryptop", NULL, IPL_NET);
225 pool_init(&cryptodesc_pool, sizeof(struct cryptodesc), 0, 0,
226 0, "cryptodesc", NULL, IPL_NET);
227 pool_init(&cryptkop_pool, sizeof(struct cryptkop), 0, 0,
228 0, "cryptkop", NULL, IPL_NET);
229
230 crypto_drivers = malloc(CRYPTO_DRIVERS_INITIAL *
231 sizeof(struct cryptocap), M_CRYPTO_DATA, M_NOWAIT | M_ZERO);
232 if (crypto_drivers == NULL) {
233 printf("crypto_init: cannot malloc driver table\n");
234 return ENOMEM;
235 }
236 crypto_drivers_num = CRYPTO_DRIVERS_INITIAL;
237
238 softintr_cookie = register_swi(SWI_CRYPTO, cryptointr);
239 error = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
240 (void (*)(void *))cryptoret, NULL, &cryptothread, "cryptoret");
241 if (error) {
242 printf("crypto_init: cannot start cryptoret thread; error %d",
243 error);
244 return crypto_destroy(false);
245 }
246
247 #ifdef _MODULE
248 sysctl_opencrypto_setup(&sysctl_opencrypto_clog);
249 #endif
250 return 0;
251 }
252
253 int
254 crypto_init(void)
255 {
256 static ONCE_DECL(crypto_init_once);
257
258 return RUN_ONCE(&crypto_init_once, crypto_init0);
259 }
260
261 static int
262 crypto_destroy(bool exit_kthread)
263 {
264 int i;
265
266 if (exit_kthread) {
267 mutex_spin_enter(&crypto_ret_q_mtx);
268
269 /* if we have any in-progress requests, don't unload */
270 if (!TAILQ_EMPTY(&crp_q) || !TAILQ_EMPTY(&crp_kq)) {
271 mutex_spin_exit(&crypto_ret_q_mtx);
272 return EBUSY;
273 }
274
275 for (i = 0; i < crypto_drivers_num; i++)
276 if (crypto_drivers[i].cc_sessions != 0)
277 break;
278 if (i < crypto_drivers_num) {
279 mutex_spin_exit(&crypto_ret_q_mtx);
280 return EBUSY;
281 }
282
283 /* kick the cryptoret thread and wait for it to exit */
284 crypto_exit_flag = 1;
285 cv_signal(&cryptoret_cv);
286
287 while (crypto_exit_flag != 0)
288 cv_wait(&cryptoret_cv, &crypto_ret_q_mtx);
289 mutex_spin_exit(&crypto_ret_q_mtx);
290 }
291
292 #ifdef _MODULE
293 if (sysctl_opencrypto_clog != NULL)
294 sysctl_teardown(&sysctl_opencrypto_clog);
295 #endif
296
297 unregister_swi(SWI_CRYPTO, cryptointr);
298
299 if (crypto_drivers != NULL)
300 free(crypto_drivers, M_CRYPTO_DATA);
301
302 pool_destroy(&cryptop_pool);
303 pool_destroy(&cryptodesc_pool);
304 pool_destroy(&cryptkop_pool);
305
306 cv_destroy(&cryptoret_cv);
307
308 mutex_destroy(&crypto_ret_q_mtx);
309 mutex_destroy(&crypto_q_mtx);
310 mutex_destroy(&crypto_mtx);
311
312 return 0;
313 }
314
315 /*
316 * Create a new session. Must be called with crypto_mtx held.
317 */
318 int
319 crypto_newsession(u_int64_t *sid, struct cryptoini *cri, int hard)
320 {
321 struct cryptoini *cr;
322 u_int32_t hid, lid;
323 int err = EINVAL;
324
325 mutex_enter(&crypto_mtx);
326
327 if (crypto_drivers == NULL)
328 goto done;
329
330 /*
331 * The algorithm we use here is pretty stupid; just use the
332 * first driver that supports all the algorithms we need.
333 *
334 * XXX We need more smarts here (in real life too, but that's
335 * XXX another story altogether).
336 */
337
338 for (hid = 0; hid < crypto_drivers_num; hid++) {
339 /*
340 * If it's not initialized or has remaining sessions
341 * referencing it, skip.
342 */
343 if (crypto_drivers[hid].cc_newsession == NULL ||
344 (crypto_drivers[hid].cc_flags & CRYPTOCAP_F_CLEANUP))
345 continue;
346
347 /* Hardware required -- ignore software drivers. */
348 if (hard > 0 &&
349 (crypto_drivers[hid].cc_flags & CRYPTOCAP_F_SOFTWARE))
350 continue;
351 /* Software required -- ignore hardware drivers. */
352 if (hard < 0 &&
353 (crypto_drivers[hid].cc_flags & CRYPTOCAP_F_SOFTWARE) == 0)
354 continue;
355
356 /* See if all the algorithms are supported. */
357 for (cr = cri; cr; cr = cr->cri_next)
358 if (crypto_drivers[hid].cc_alg[cr->cri_alg] == 0) {
359 DPRINTF(("crypto_newsession: alg %d not supported\n", cr->cri_alg));
360 break;
361 }
362
363 if (cr == NULL) {
364 /* Ok, all algorithms are supported. */
365
366 /*
367 * Can't do everything in one session.
368 *
369 * XXX Fix this. We need to inject a "virtual" session layer right
370 * XXX about here.
371 */
372
373 /* Call the driver initialization routine. */
374 lid = hid; /* Pass the driver ID. */
375 err = crypto_drivers[hid].cc_newsession(
376 crypto_drivers[hid].cc_arg, &lid, cri);
377 if (err == 0) {
378 (*sid) = hid;
379 (*sid) <<= 32;
380 (*sid) |= (lid & 0xffffffff);
381 crypto_drivers[hid].cc_sessions++;
382 } else {
383 DPRINTF(("%s: crypto_drivers[%d].cc_newsession() failed. error=%d\n",
384 __func__, hid, err));
385 }
386 goto done;
387 /*break;*/
388 }
389 }
390 done:
391 mutex_exit(&crypto_mtx);
392 return err;
393 }
394
395 /*
396 * Delete an existing session (or a reserved session on an unregistered
397 * driver). Must be called with crypto_mtx mutex held.
398 */
399 int
400 crypto_freesession(u_int64_t sid)
401 {
402 u_int32_t hid;
403 int err = 0;
404
405 mutex_enter(&crypto_mtx);
406
407 if (crypto_drivers == NULL) {
408 err = EINVAL;
409 goto done;
410 }
411
412 /* Determine two IDs. */
413 hid = CRYPTO_SESID2HID(sid);
414
415 if (hid >= crypto_drivers_num) {
416 err = ENOENT;
417 goto done;
418 }
419
420 if (crypto_drivers[hid].cc_sessions)
421 crypto_drivers[hid].cc_sessions--;
422
423 /* Call the driver cleanup routine, if available. */
424 if (crypto_drivers[hid].cc_freesession) {
425 err = crypto_drivers[hid].cc_freesession(
426 crypto_drivers[hid].cc_arg, sid);
427 }
428 else
429 err = 0;
430
431 /*
432 * If this was the last session of a driver marked as invalid,
433 * make the entry available for reuse.
434 */
435 if ((crypto_drivers[hid].cc_flags & CRYPTOCAP_F_CLEANUP) &&
436 crypto_drivers[hid].cc_sessions == 0)
437 memset(&crypto_drivers[hid], 0, sizeof(struct cryptocap));
438
439 done:
440 mutex_exit(&crypto_mtx);
441 return err;
442 }
443
444 /*
445 * Return an unused driver id. Used by drivers prior to registering
446 * support for the algorithms they handle.
447 */
448 int32_t
449 crypto_get_driverid(u_int32_t flags)
450 {
451 struct cryptocap *newdrv;
452 int i;
453
454 (void)crypto_init(); /* XXX oh, this is foul! */
455
456 mutex_enter(&crypto_mtx);
457 for (i = 0; i < crypto_drivers_num; i++)
458 if (crypto_drivers[i].cc_process == NULL &&
459 (crypto_drivers[i].cc_flags & CRYPTOCAP_F_CLEANUP) == 0 &&
460 crypto_drivers[i].cc_sessions == 0)
461 break;
462
463 /* Out of entries, allocate some more. */
464 if (i == crypto_drivers_num) {
465 /* Be careful about wrap-around. */
466 if (2 * crypto_drivers_num <= crypto_drivers_num) {
467 mutex_exit(&crypto_mtx);
468 printf("crypto: driver count wraparound!\n");
469 return -1;
470 }
471
472 newdrv = malloc(2 * crypto_drivers_num *
473 sizeof(struct cryptocap), M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
474 if (newdrv == NULL) {
475 mutex_exit(&crypto_mtx);
476 printf("crypto: no space to expand driver table!\n");
477 return -1;
478 }
479
480 memcpy(newdrv, crypto_drivers,
481 crypto_drivers_num * sizeof(struct cryptocap));
482
483 crypto_drivers_num *= 2;
484
485 free(crypto_drivers, M_CRYPTO_DATA);
486 crypto_drivers = newdrv;
487 }
488
489 /* NB: state is zero'd on free */
490 crypto_drivers[i].cc_sessions = 1; /* Mark */
491 crypto_drivers[i].cc_flags = flags;
492
493 if (bootverbose)
494 printf("crypto: assign driver %u, flags %u\n", i, flags);
495
496 mutex_exit(&crypto_mtx);
497
498 return i;
499 }
500
501 static struct cryptocap *
502 crypto_checkdriver(u_int32_t hid)
503 {
504 if (crypto_drivers == NULL)
505 return NULL;
506 return (hid >= crypto_drivers_num ? NULL : &crypto_drivers[hid]);
507 }
508
509 /*
510 * Register support for a key-related algorithm. This routine
511 * is called once for each algorithm supported a driver.
512 */
513 int
514 crypto_kregister(u_int32_t driverid, int kalg, u_int32_t flags,
515 int (*kprocess)(void *, struct cryptkop *, int),
516 void *karg)
517 {
518 struct cryptocap *cap;
519 int err;
520
521 mutex_enter(&crypto_mtx);
522
523 cap = crypto_checkdriver(driverid);
524 if (cap != NULL &&
525 (CRK_ALGORITM_MIN <= kalg && kalg <= CRK_ALGORITHM_MAX)) {
526 /*
527 * XXX Do some performance testing to determine placing.
528 * XXX We probably need an auxiliary data structure that
529 * XXX describes relative performances.
530 */
531
532 cap->cc_kalg[kalg] = flags | CRYPTO_ALG_FLAG_SUPPORTED;
533 if (bootverbose) {
534 printf("crypto: driver %u registers key alg %u "
535 " flags %u\n",
536 driverid,
537 kalg,
538 flags
539 );
540 }
541
542 if (cap->cc_kprocess == NULL) {
543 cap->cc_karg = karg;
544 cap->cc_kprocess = kprocess;
545 }
546 err = 0;
547 } else
548 err = EINVAL;
549
550 mutex_exit(&crypto_mtx);
551 return err;
552 }
553
554 /*
555 * Register support for a non-key-related algorithm. This routine
556 * is called once for each such algorithm supported by a driver.
557 */
558 int
559 crypto_register(u_int32_t driverid, int alg, u_int16_t maxoplen,
560 u_int32_t flags,
561 int (*newses)(void *, u_int32_t*, struct cryptoini*),
562 int (*freeses)(void *, u_int64_t),
563 int (*process)(void *, struct cryptop *, int),
564 void *arg)
565 {
566 struct cryptocap *cap;
567 int err;
568
569 mutex_enter(&crypto_mtx);
570
571 cap = crypto_checkdriver(driverid);
572 /* NB: algorithms are in the range [1..max] */
573 if (cap != NULL &&
574 (CRYPTO_ALGORITHM_MIN <= alg && alg <= CRYPTO_ALGORITHM_MAX)) {
575 /*
576 * XXX Do some performance testing to determine placing.
577 * XXX We probably need an auxiliary data structure that
578 * XXX describes relative performances.
579 */
580
581 cap->cc_alg[alg] = flags | CRYPTO_ALG_FLAG_SUPPORTED;
582 cap->cc_max_op_len[alg] = maxoplen;
583 if (bootverbose) {
584 printf("crypto: driver %u registers alg %u "
585 "flags %u maxoplen %u\n",
586 driverid,
587 alg,
588 flags,
589 maxoplen
590 );
591 }
592
593 if (cap->cc_process == NULL) {
594 cap->cc_arg = arg;
595 cap->cc_newsession = newses;
596 cap->cc_process = process;
597 cap->cc_freesession = freeses;
598 cap->cc_sessions = 0; /* Unmark */
599 }
600 err = 0;
601 } else
602 err = EINVAL;
603
604 mutex_exit(&crypto_mtx);
605 return err;
606 }
607
608 /*
609 * Unregister a crypto driver. If there are pending sessions using it,
610 * leave enough information around so that subsequent calls using those
611 * sessions will correctly detect the driver has been unregistered and
612 * reroute requests.
613 */
614 int
615 crypto_unregister(u_int32_t driverid, int alg)
616 {
617 int i, err;
618 u_int32_t ses;
619 struct cryptocap *cap;
620
621 mutex_enter(&crypto_mtx);
622
623 cap = crypto_checkdriver(driverid);
624 if (cap != NULL &&
625 (CRYPTO_ALGORITHM_MIN <= alg && alg <= CRYPTO_ALGORITHM_MAX) &&
626 cap->cc_alg[alg] != 0) {
627 cap->cc_alg[alg] = 0;
628 cap->cc_max_op_len[alg] = 0;
629
630 /* Was this the last algorithm ? */
631 for (i = 1; i <= CRYPTO_ALGORITHM_MAX; i++)
632 if (cap->cc_alg[i] != 0)
633 break;
634
635 if (i == CRYPTO_ALGORITHM_MAX + 1) {
636 ses = cap->cc_sessions;
637 memset(cap, 0, sizeof(struct cryptocap));
638 if (ses != 0) {
639 /*
640 * If there are pending sessions, just mark as invalid.
641 */
642 cap->cc_flags |= CRYPTOCAP_F_CLEANUP;
643 cap->cc_sessions = ses;
644 }
645 }
646 err = 0;
647 } else
648 err = EINVAL;
649
650 mutex_exit(&crypto_mtx);
651 return err;
652 }
653
654 /*
655 * Unregister all algorithms associated with a crypto driver.
656 * If there are pending sessions using it, leave enough information
657 * around so that subsequent calls using those sessions will
658 * correctly detect the driver has been unregistered and reroute
659 * requests.
660 *
661 * XXX careful. Don't change this to call crypto_unregister() for each
662 * XXX registered algorithm unless you drop the mutex across the calls;
663 * XXX you can't take it recursively.
664 */
665 int
666 crypto_unregister_all(u_int32_t driverid)
667 {
668 int i, err;
669 u_int32_t ses;
670 struct cryptocap *cap;
671
672 mutex_enter(&crypto_mtx);
673 cap = crypto_checkdriver(driverid);
674 if (cap != NULL) {
675 for (i = CRYPTO_ALGORITHM_MIN; i <= CRYPTO_ALGORITHM_MAX; i++) {
676 cap->cc_alg[i] = 0;
677 cap->cc_max_op_len[i] = 0;
678 }
679 ses = cap->cc_sessions;
680 memset(cap, 0, sizeof(struct cryptocap));
681 if (ses != 0) {
682 /*
683 * If there are pending sessions, just mark as invalid.
684 */
685 cap->cc_flags |= CRYPTOCAP_F_CLEANUP;
686 cap->cc_sessions = ses;
687 }
688 err = 0;
689 } else
690 err = EINVAL;
691
692 mutex_exit(&crypto_mtx);
693 return err;
694 }
695
696 /*
697 * Clear blockage on a driver. The what parameter indicates whether
698 * the driver is now ready for cryptop's and/or cryptokop's.
699 */
700 int
701 crypto_unblock(u_int32_t driverid, int what)
702 {
703 struct cryptocap *cap;
704 int needwakeup, err;
705
706 mutex_spin_enter(&crypto_q_mtx);
707 cap = crypto_checkdriver(driverid);
708 if (cap != NULL) {
709 needwakeup = 0;
710 if (what & CRYPTO_SYMQ) {
711 needwakeup |= cap->cc_qblocked;
712 cap->cc_qblocked = 0;
713 }
714 if (what & CRYPTO_ASYMQ) {
715 needwakeup |= cap->cc_kqblocked;
716 cap->cc_kqblocked = 0;
717 }
718 err = 0;
719 if (needwakeup)
720 setsoftcrypto(softintr_cookie);
721 mutex_spin_exit(&crypto_q_mtx);
722 } else {
723 err = EINVAL;
724 mutex_spin_exit(&crypto_q_mtx);
725 }
726
727 return err;
728 }
729
730 /*
731 * Dispatch a crypto request to a driver or queue
732 * it, to be processed by the kernel thread.
733 */
734 int
735 crypto_dispatch(struct cryptop *crp)
736 {
737 u_int32_t hid = CRYPTO_SESID2HID(crp->crp_sid);
738 int result;
739
740 mutex_spin_enter(&crypto_q_mtx);
741 DPRINTF(("crypto_dispatch: crp %p, alg %d\n",
742 crp, crp->crp_desc->crd_alg));
743
744 cryptostats.cs_ops++;
745
746 #ifdef CRYPTO_TIMING
747 if (crypto_timing)
748 nanouptime(&crp->crp_tstamp);
749 #endif
750 if ((crp->crp_flags & CRYPTO_F_BATCH) == 0) {
751 struct cryptocap *cap;
752 /*
753 * Caller marked the request to be processed
754 * immediately; dispatch it directly to the
755 * driver unless the driver is currently blocked.
756 */
757 cap = crypto_checkdriver(hid);
758 if (cap && !cap->cc_qblocked) {
759 mutex_spin_exit(&crypto_q_mtx);
760 result = crypto_invoke(crp, 0);
761 if (result == ERESTART) {
762 /*
763 * The driver ran out of resources, mark the
764 * driver ``blocked'' for cryptop's and put
765 * the op on the queue.
766 */
767 mutex_spin_enter(&crypto_q_mtx);
768 crypto_drivers[hid].cc_qblocked = 1;
769 TAILQ_INSERT_HEAD(&crp_q, crp, crp_next);
770 cryptostats.cs_blocks++;
771 mutex_spin_exit(&crypto_q_mtx);
772 }
773 goto out_released;
774 } else {
775 /*
776 * The driver is blocked, just queue the op until
777 * it unblocks and the swi thread gets kicked.
778 */
779 TAILQ_INSERT_TAIL(&crp_q, crp, crp_next);
780 result = 0;
781 }
782 } else {
783 int wasempty = TAILQ_EMPTY(&crp_q);
784 /*
785 * Caller marked the request as ``ok to delay'';
786 * queue it for the swi thread. This is desirable
787 * when the operation is low priority and/or suitable
788 * for batching.
789 */
790 TAILQ_INSERT_TAIL(&crp_q, crp, crp_next);
791 if (wasempty) {
792 setsoftcrypto(softintr_cookie);
793 mutex_spin_exit(&crypto_q_mtx);
794 result = 0;
795 goto out_released;
796 }
797
798 result = 0;
799 }
800
801 mutex_spin_exit(&crypto_q_mtx);
802 out_released:
803 return result;
804 }
805
806 /*
807 * Add an asymetric crypto request to a queue,
808 * to be processed by the kernel thread.
809 */
810 int
811 crypto_kdispatch(struct cryptkop *krp)
812 {
813 struct cryptocap *cap;
814 int result;
815
816 mutex_spin_enter(&crypto_q_mtx);
817 cryptostats.cs_kops++;
818
819 cap = crypto_checkdriver(krp->krp_hid);
820 if (cap && !cap->cc_kqblocked) {
821 mutex_spin_exit(&crypto_q_mtx);
822 result = crypto_kinvoke(krp, 0);
823 if (result == ERESTART) {
824 /*
825 * The driver ran out of resources, mark the
826 * driver ``blocked'' for cryptop's and put
827 * the op on the queue.
828 */
829 mutex_spin_enter(&crypto_q_mtx);
830 crypto_drivers[krp->krp_hid].cc_kqblocked = 1;
831 TAILQ_INSERT_HEAD(&crp_kq, krp, krp_next);
832 cryptostats.cs_kblocks++;
833 mutex_spin_exit(&crypto_q_mtx);
834 }
835 } else {
836 /*
837 * The driver is blocked, just queue the op until
838 * it unblocks and the swi thread gets kicked.
839 */
840 TAILQ_INSERT_TAIL(&crp_kq, krp, krp_next);
841 result = 0;
842 mutex_spin_exit(&crypto_q_mtx);
843 }
844
845 return result;
846 }
847
848 /*
849 * Dispatch an assymetric crypto request to the appropriate crypto devices.
850 */
851 static int
852 crypto_kinvoke(struct cryptkop *krp, int hint)
853 {
854 u_int32_t hid;
855 int error;
856
857 /* Sanity checks. */
858 if (krp == NULL)
859 return EINVAL;
860 if (krp->krp_callback == NULL) {
861 cv_destroy(&krp->krp_cv);
862 pool_put(&cryptkop_pool, krp);
863 return EINVAL;
864 }
865
866 mutex_enter(&crypto_mtx);
867 for (hid = 0; hid < crypto_drivers_num; hid++) {
868 if ((crypto_drivers[hid].cc_flags & CRYPTOCAP_F_SOFTWARE) &&
869 crypto_devallowsoft == 0)
870 continue;
871 if (crypto_drivers[hid].cc_kprocess == NULL)
872 continue;
873 if ((crypto_drivers[hid].cc_kalg[krp->krp_op] &
874 CRYPTO_ALG_FLAG_SUPPORTED) == 0)
875 continue;
876 break;
877 }
878 if (hid < crypto_drivers_num) {
879 int (*process)(void *, struct cryptkop *, int);
880 void *arg;
881
882 process = crypto_drivers[hid].cc_kprocess;
883 arg = crypto_drivers[hid].cc_karg;
884 mutex_exit(&crypto_mtx);
885 krp->krp_hid = hid;
886 error = (*process)(arg, krp, hint);
887 } else {
888 mutex_exit(&crypto_mtx);
889 error = ENODEV;
890 }
891
892 if (error) {
893 krp->krp_status = error;
894 crypto_kdone(krp);
895 }
896 return 0;
897 }
898
899 #ifdef CRYPTO_TIMING
900 static void
901 crypto_tstat(struct cryptotstat *ts, struct timespec *tv)
902 {
903 struct timespec now, t;
904
905 nanouptime(&now);
906 t.tv_sec = now.tv_sec - tv->tv_sec;
907 t.tv_nsec = now.tv_nsec - tv->tv_nsec;
908 if (t.tv_nsec < 0) {
909 t.tv_sec--;
910 t.tv_nsec += 1000000000;
911 }
912 timespecadd(&ts->acc, &t, &t);
913 if (timespeccmp(&t, &ts->min, <))
914 ts->min = t;
915 if (timespeccmp(&t, &ts->max, >))
916 ts->max = t;
917 ts->count++;
918
919 *tv = now;
920 }
921 #endif
922
923 /*
924 * Dispatch a crypto request to the appropriate crypto devices.
925 */
926 static int
927 crypto_invoke(struct cryptop *crp, int hint)
928 {
929 u_int32_t hid;
930
931 #ifdef CRYPTO_TIMING
932 if (crypto_timing)
933 crypto_tstat(&cryptostats.cs_invoke, &crp->crp_tstamp);
934 #endif
935 /* Sanity checks. */
936 if (crp == NULL)
937 return EINVAL;
938 if (crp->crp_callback == NULL) {
939 return EINVAL;
940 }
941 if (crp->crp_desc == NULL) {
942 crp->crp_etype = EINVAL;
943 crypto_done(crp);
944 return 0;
945 }
946
947 hid = CRYPTO_SESID2HID(crp->crp_sid);
948
949 if (hid < crypto_drivers_num) {
950 int (*process)(void *, struct cryptop *, int);
951 void *arg;
952
953 if (crypto_drivers[hid].cc_flags & CRYPTOCAP_F_CLEANUP)
954 crypto_freesession(crp->crp_sid);
955 process = crypto_drivers[hid].cc_process;
956 arg = crypto_drivers[hid].cc_arg;
957
958 /*
959 * Invoke the driver to process the request.
960 */
961 DPRINTF(("calling process for %p\n", crp));
962 return (*process)(arg, crp, hint);
963 } else {
964 struct cryptodesc *crd;
965 u_int64_t nid = 0;
966
967 /*
968 * Driver has unregistered; migrate the session and return
969 * an error to the caller so they'll resubmit the op.
970 */
971 for (crd = crp->crp_desc; crd->crd_next; crd = crd->crd_next)
972 crd->CRD_INI.cri_next = &(crd->crd_next->CRD_INI);
973
974 if (crypto_newsession(&nid, &(crp->crp_desc->CRD_INI), 0) == 0)
975 crp->crp_sid = nid;
976
977 crp->crp_etype = EAGAIN;
978
979 crypto_done(crp);
980 return 0;
981 }
982 }
983
984 /*
985 * Release a set of crypto descriptors.
986 */
987 void
988 crypto_freereq(struct cryptop *crp)
989 {
990 struct cryptodesc *crd;
991
992 if (crp == NULL)
993 return;
994 DPRINTF(("crypto_freereq[%u]: crp %p\n",
995 CRYPTO_SESID2LID(crp->crp_sid), crp));
996
997 /* sanity check */
998 if (crp->crp_flags & CRYPTO_F_ONRETQ) {
999 panic("crypto_freereq() freeing crp on RETQ\n");
1000 }
1001
1002 while ((crd = crp->crp_desc) != NULL) {
1003 crp->crp_desc = crd->crd_next;
1004 pool_put(&cryptodesc_pool, crd);
1005 }
1006 pool_put(&cryptop_pool, crp);
1007 }
1008
1009 /*
1010 * Acquire a set of crypto descriptors.
1011 */
1012 struct cryptop *
1013 crypto_getreq(int num)
1014 {
1015 struct cryptodesc *crd;
1016 struct cryptop *crp;
1017
1018 crp = pool_get(&cryptop_pool, 0);
1019 if (crp == NULL) {
1020 return NULL;
1021 }
1022 memset(crp, 0, sizeof(struct cryptop));
1023
1024 while (num--) {
1025 crd = pool_get(&cryptodesc_pool, 0);
1026 if (crd == NULL) {
1027 crypto_freereq(crp);
1028 return NULL;
1029 }
1030
1031 memset(crd, 0, sizeof(struct cryptodesc));
1032 crd->crd_next = crp->crp_desc;
1033 crp->crp_desc = crd;
1034 }
1035
1036 return crp;
1037 }
1038
1039 /*
1040 * Invoke the callback on behalf of the driver.
1041 */
1042 void
1043 crypto_done(struct cryptop *crp)
1044 {
1045 int wasempty;
1046
1047 if (crp->crp_etype != 0)
1048 cryptostats.cs_errs++;
1049 #ifdef CRYPTO_TIMING
1050 if (crypto_timing)
1051 crypto_tstat(&cryptostats.cs_done, &crp->crp_tstamp);
1052 #endif
1053 DPRINTF(("crypto_done[%u]: crp %p\n",
1054 CRYPTO_SESID2LID(crp->crp_sid), crp));
1055
1056 /*
1057 * Normal case; queue the callback for the thread.
1058 *
1059 * The return queue is manipulated by the swi thread
1060 * and, potentially, by crypto device drivers calling
1061 * back to mark operations completed. Thus we need
1062 * to mask both while manipulating the return queue.
1063 */
1064 if (crp->crp_flags & CRYPTO_F_CBIMM) {
1065 /*
1066 * Do the callback directly. This is ok when the
1067 * callback routine does very little (e.g. the
1068 * /dev/crypto callback method just does a wakeup).
1069 */
1070 mutex_spin_enter(&crypto_ret_q_mtx);
1071 crp->crp_flags |= CRYPTO_F_DONE;
1072 mutex_spin_exit(&crypto_ret_q_mtx);
1073
1074 #ifdef CRYPTO_TIMING
1075 if (crypto_timing) {
1076 /*
1077 * NB: We must copy the timestamp before
1078 * doing the callback as the cryptop is
1079 * likely to be reclaimed.
1080 */
1081 struct timespec t = crp->crp_tstamp;
1082 crypto_tstat(&cryptostats.cs_cb, &t);
1083 crp->crp_callback(crp);
1084 crypto_tstat(&cryptostats.cs_finis, &t);
1085 } else
1086 #endif
1087 crp->crp_callback(crp);
1088 } else {
1089 mutex_spin_enter(&crypto_ret_q_mtx);
1090 crp->crp_flags |= CRYPTO_F_DONE;
1091 #if 0
1092 if (crp->crp_flags & CRYPTO_F_USER) {
1093 /*
1094 * TODO:
1095 * If crp->crp_flags & CRYPTO_F_USER and the used
1096 * encryption driver does all the processing in
1097 * the same context, we can skip enqueueing crp_ret_q
1098 * and cv_signal(&cryptoret_cv).
1099 */
1100 DPRINTF(("crypto_done[%u]: crp %p CRYPTO_F_USER\n",
1101 CRYPTO_SESID2LID(crp->crp_sid), crp));
1102 } else
1103 #endif
1104 {
1105 wasempty = TAILQ_EMPTY(&crp_ret_q);
1106 DPRINTF(("crypto_done[%u]: queueing %p\n",
1107 CRYPTO_SESID2LID(crp->crp_sid), crp));
1108 crp->crp_flags |= CRYPTO_F_ONRETQ;
1109 TAILQ_INSERT_TAIL(&crp_ret_q, crp, crp_next);
1110 if (wasempty) {
1111 DPRINTF(("crypto_done[%u]: waking cryptoret, "
1112 "crp %p hit empty queue\n.",
1113 CRYPTO_SESID2LID(crp->crp_sid), crp));
1114 cv_signal(&cryptoret_cv);
1115 }
1116 }
1117 mutex_spin_exit(&crypto_ret_q_mtx);
1118 }
1119 }
1120
1121 /*
1122 * Invoke the callback on behalf of the driver.
1123 */
1124 void
1125 crypto_kdone(struct cryptkop *krp)
1126 {
1127 int wasempty;
1128
1129 if (krp->krp_status != 0)
1130 cryptostats.cs_kerrs++;
1131
1132 krp->krp_flags |= CRYPTO_F_DONE;
1133
1134 /*
1135 * The return queue is manipulated by the swi thread
1136 * and, potentially, by crypto device drivers calling
1137 * back to mark operations completed. Thus we need
1138 * to mask both while manipulating the return queue.
1139 */
1140 if (krp->krp_flags & CRYPTO_F_CBIMM) {
1141 krp->krp_callback(krp);
1142 } else {
1143 mutex_spin_enter(&crypto_ret_q_mtx);
1144 wasempty = TAILQ_EMPTY(&crp_ret_kq);
1145 krp->krp_flags |= CRYPTO_F_ONRETQ;
1146 TAILQ_INSERT_TAIL(&crp_ret_kq, krp, krp_next);
1147 if (wasempty)
1148 cv_signal(&cryptoret_cv);
1149 mutex_spin_exit(&crypto_ret_q_mtx);
1150 }
1151 }
1152
1153 int
1154 crypto_getfeat(int *featp)
1155 {
1156 int hid, kalg, feat = 0;
1157
1158 mutex_enter(&crypto_mtx);
1159
1160 if (crypto_userasymcrypto == 0)
1161 goto out;
1162
1163 for (hid = 0; hid < crypto_drivers_num; hid++) {
1164 if ((crypto_drivers[hid].cc_flags & CRYPTOCAP_F_SOFTWARE) &&
1165 crypto_devallowsoft == 0) {
1166 continue;
1167 }
1168 if (crypto_drivers[hid].cc_kprocess == NULL)
1169 continue;
1170 for (kalg = 0; kalg < CRK_ALGORITHM_MAX; kalg++)
1171 if ((crypto_drivers[hid].cc_kalg[kalg] &
1172 CRYPTO_ALG_FLAG_SUPPORTED) != 0)
1173 feat |= 1 << kalg;
1174 }
1175 out:
1176 mutex_exit(&crypto_mtx);
1177 *featp = feat;
1178 return (0);
1179 }
1180
1181 /*
1182 * Software interrupt thread to dispatch crypto requests.
1183 */
1184 static void
1185 cryptointr(void)
1186 {
1187 struct cryptop *crp, *submit, *cnext;
1188 struct cryptkop *krp, *knext;
1189 struct cryptocap *cap;
1190 int result, hint;
1191
1192 cryptostats.cs_intrs++;
1193 mutex_spin_enter(&crypto_q_mtx);
1194 do {
1195 /*
1196 * Find the first element in the queue that can be
1197 * processed and look-ahead to see if multiple ops
1198 * are ready for the same driver.
1199 */
1200 submit = NULL;
1201 hint = 0;
1202 TAILQ_FOREACH_SAFE(crp, &crp_q, crp_next, cnext) {
1203 u_int32_t hid = CRYPTO_SESID2HID(crp->crp_sid);
1204 cap = crypto_checkdriver(hid);
1205 if (cap == NULL || cap->cc_process == NULL) {
1206 /* Op needs to be migrated, process it. */
1207 if (submit == NULL)
1208 submit = crp;
1209 break;
1210 }
1211 if (!cap->cc_qblocked) {
1212 if (submit != NULL) {
1213 /*
1214 * We stop on finding another op,
1215 * regardless whether its for the same
1216 * driver or not. We could keep
1217 * searching the queue but it might be
1218 * better to just use a per-driver
1219 * queue instead.
1220 */
1221 if (CRYPTO_SESID2HID(submit->crp_sid)
1222 == hid)
1223 hint = CRYPTO_HINT_MORE;
1224 break;
1225 } else {
1226 submit = crp;
1227 if ((submit->crp_flags & CRYPTO_F_BATCH) == 0)
1228 break;
1229 /* keep scanning for more are q'd */
1230 }
1231 }
1232 }
1233 if (submit != NULL) {
1234 TAILQ_REMOVE(&crp_q, submit, crp_next);
1235 mutex_spin_exit(&crypto_q_mtx);
1236 result = crypto_invoke(submit, hint);
1237 /* we must take here as the TAILQ op or kinvoke
1238 may need this mutex below. sigh. */
1239 mutex_spin_enter(&crypto_q_mtx);
1240 if (result == ERESTART) {
1241 /*
1242 * The driver ran out of resources, mark the
1243 * driver ``blocked'' for cryptop's and put
1244 * the request back in the queue. It would
1245 * best to put the request back where we got
1246 * it but that's hard so for now we put it
1247 * at the front. This should be ok; putting
1248 * it at the end does not work.
1249 */
1250 /* XXX validate sid again? */
1251 crypto_drivers[CRYPTO_SESID2HID(submit->crp_sid)].cc_qblocked = 1;
1252 TAILQ_INSERT_HEAD(&crp_q, submit, crp_next);
1253 cryptostats.cs_blocks++;
1254 }
1255 }
1256
1257 /* As above, but for key ops */
1258 TAILQ_FOREACH_SAFE(krp, &crp_kq, krp_next, knext) {
1259 cap = crypto_checkdriver(krp->krp_hid);
1260 if (cap == NULL || cap->cc_kprocess == NULL) {
1261 /* Op needs to be migrated, process it. */
1262 break;
1263 }
1264 if (!cap->cc_kqblocked)
1265 break;
1266 }
1267 if (krp != NULL) {
1268 TAILQ_REMOVE(&crp_kq, krp, krp_next);
1269 mutex_spin_exit(&crypto_q_mtx);
1270 result = crypto_kinvoke(krp, 0);
1271 /* the next iteration will want the mutex. :-/ */
1272 mutex_spin_enter(&crypto_q_mtx);
1273 if (result == ERESTART) {
1274 /*
1275 * The driver ran out of resources, mark the
1276 * driver ``blocked'' for cryptkop's and put
1277 * the request back in the queue. It would
1278 * best to put the request back where we got
1279 * it but that's hard so for now we put it
1280 * at the front. This should be ok; putting
1281 * it at the end does not work.
1282 */
1283 /* XXX validate sid again? */
1284 crypto_drivers[krp->krp_hid].cc_kqblocked = 1;
1285 TAILQ_INSERT_HEAD(&crp_kq, krp, krp_next);
1286 cryptostats.cs_kblocks++;
1287 }
1288 }
1289 } while (submit != NULL || krp != NULL);
1290 mutex_spin_exit(&crypto_q_mtx);
1291 }
1292
1293 /*
1294 * Kernel thread to do callbacks.
1295 */
1296 static void
1297 cryptoret(void)
1298 {
1299 struct cryptop *crp;
1300 struct cryptkop *krp;
1301
1302 mutex_spin_enter(&crypto_ret_q_mtx);
1303 for (;;) {
1304 crp = TAILQ_FIRST(&crp_ret_q);
1305 if (crp != NULL) {
1306 TAILQ_REMOVE(&crp_ret_q, crp, crp_next);
1307 crp->crp_flags &= ~CRYPTO_F_ONRETQ;
1308 }
1309 krp = TAILQ_FIRST(&crp_ret_kq);
1310 if (krp != NULL) {
1311 TAILQ_REMOVE(&crp_ret_kq, krp, krp_next);
1312 krp->krp_flags &= ~CRYPTO_F_ONRETQ;
1313 }
1314
1315 /* drop before calling any callbacks. */
1316 if (crp == NULL && krp == NULL) {
1317
1318 /* Check for the exit condition. */
1319 if (crypto_exit_flag != 0) {
1320
1321 /* Time to die. */
1322 crypto_exit_flag = 0;
1323 cv_broadcast(&cryptoret_cv);
1324 mutex_spin_exit(&crypto_ret_q_mtx);
1325 kthread_exit(0);
1326 }
1327
1328 cryptostats.cs_rets++;
1329 cv_wait(&cryptoret_cv, &crypto_ret_q_mtx);
1330 continue;
1331 }
1332
1333 mutex_spin_exit(&crypto_ret_q_mtx);
1334
1335 if (crp != NULL) {
1336 #ifdef CRYPTO_TIMING
1337 if (crypto_timing) {
1338 /*
1339 * NB: We must copy the timestamp before
1340 * doing the callback as the cryptop is
1341 * likely to be reclaimed.
1342 */
1343 struct timespec t = crp->crp_tstamp;
1344 crypto_tstat(&cryptostats.cs_cb, &t);
1345 crp->crp_callback(crp);
1346 crypto_tstat(&cryptostats.cs_finis, &t);
1347 } else
1348 #endif
1349 {
1350 crp->crp_callback(crp);
1351 }
1352 }
1353 if (krp != NULL)
1354 krp->krp_callback(krp);
1355
1356 mutex_spin_enter(&crypto_ret_q_mtx);
1357 }
1358 }
1359
1360 /* NetBSD module interface */
1361
1362 MODULE(MODULE_CLASS_MISC, opencrypto, NULL);
1363
1364 static int
1365 opencrypto_modcmd(modcmd_t cmd, void *opaque)
1366 {
1367 int error = 0;
1368
1369 switch (cmd) {
1370 case MODULE_CMD_INIT:
1371 #ifdef _MODULE
1372 error = crypto_init();
1373 #endif
1374 break;
1375 case MODULE_CMD_FINI:
1376 #ifdef _MODULE
1377 error = crypto_destroy(true);
1378 #endif
1379 break;
1380 default:
1381 error = ENOTTY;
1382 }
1383 return error;
1384 }
1385