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