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