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