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