1 /* $OpenBSD: crypto.c,v 1.38 2002/06/11 11:14:29 beck Exp $ */
3 * The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu)
5 * This code was written by Angelos D. Keromytis in Athens, Greece, in
6 * February 2000. Network Security Technologies Inc. (NSTI) kindly
7 * supported the development of this code.
9 * Copyright (c) 2000, 2001 Angelos D. Keromytis
11 * Permission to use, copy, and modify this software with or without fee
12 * is hereby granted, provided that this entire notice is included in
13 * all source code copies of any software which is or includes a copy or
14 * modification of this software.
16 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
18 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
19 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
23 #include <sys/cdefs.h>
24 __FBSDID("$FreeBSD$");
26 #define CRYPTO_TIMING /* enable timing support */
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/eventhandler.h>
31 #include <sys/kernel.h>
32 #include <sys/kthread.h>
34 #include <sys/module.h>
35 #include <sys/mutex.h>
36 #include <sys/malloc.h>
38 #include <sys/sysctl.h>
41 #include <opencrypto/cryptodev.h>
42 #include <opencrypto/xform.h> /* XXX for M_XDATA */
45 * Crypto drivers register themselves by allocating a slot in the
46 * crypto_drivers table with crypto_get_driverid() and then registering
47 * each algorithm they support with crypto_register() and crypto_kregister().
49 static struct mtx crypto_drivers_mtx; /* lock on driver table */
50 #define CRYPTO_DRIVER_LOCK() mtx_lock(&crypto_drivers_mtx)
51 #define CRYPTO_DRIVER_UNLOCK() mtx_unlock(&crypto_drivers_mtx)
52 static struct cryptocap *crypto_drivers = NULL;
53 static int crypto_drivers_num = 0;
56 * There are two queues for crypto requests; one for symmetric (e.g.
57 * cipher) operations and one for asymmetric (e.g. MOD)operations.
58 * A single mutex is used to lock access to both queues. We could
59 * have one per-queue but having one simplifies handling of block/unblock
62 static TAILQ_HEAD(,cryptop) crp_q; /* request queues */
63 static TAILQ_HEAD(,cryptkop) crp_kq;
64 static struct mtx crypto_q_mtx;
65 #define CRYPTO_Q_LOCK() mtx_lock(&crypto_q_mtx)
66 #define CRYPTO_Q_UNLOCK() mtx_unlock(&crypto_q_mtx)
69 * There are two queues for processing completed crypto requests; one
70 * for the symmetric and one for the asymmetric ops. We only need one
71 * but have two to avoid type futzing (cryptop vs. cryptkop). A single
72 * mutex is used to lock access to both queues. Note that this lock
73 * must be separate from the lock on request queues to insure driver
74 * callbacks don't generate lock order reversals.
76 static TAILQ_HEAD(,cryptop) crp_ret_q; /* callback queues */
77 static TAILQ_HEAD(,cryptkop) crp_ret_kq;
78 static struct mtx crypto_ret_q_mtx;
79 #define CRYPTO_RETQ_LOCK() mtx_lock(&crypto_ret_q_mtx)
80 #define CRYPTO_RETQ_UNLOCK() mtx_unlock(&crypto_ret_q_mtx)
82 static uma_zone_t cryptop_zone;
83 static uma_zone_t cryptodesc_zone;
85 int crypto_userasymcrypto = 1; /* userland may do asym crypto reqs */
86 SYSCTL_INT(_kern, OID_AUTO, userasymcrypto, CTLFLAG_RW,
87 &crypto_userasymcrypto, 0,
88 "Enable/disable user-mode access to asymmetric crypto support");
89 int crypto_devallowsoft = 0; /* only use hardware crypto for asym */
90 SYSCTL_INT(_kern, OID_AUTO, cryptodevallowsoft, CTLFLAG_RW,
91 &crypto_devallowsoft, 0,
92 "Enable/disable use of software asym crypto support");
94 MALLOC_DEFINE(M_CRYPTO_DATA, "crypto", "crypto session records");
96 static void crypto_proc(void);
97 static struct proc *cryptoproc;
98 static void crypto_ret_proc(void);
99 static struct proc *cryptoretproc;
100 static void crypto_destroy(void);
101 static int crypto_invoke(struct cryptop *crp, int hint);
102 static int crypto_kinvoke(struct cryptkop *krp, int hint);
104 static struct cryptostats cryptostats;
105 SYSCTL_STRUCT(_kern, OID_AUTO, crypto_stats, CTLFLAG_RW, &cryptostats,
106 cryptostats, "Crypto system statistics");
109 static int crypto_timing = 0;
110 SYSCTL_INT(_debug, OID_AUTO, crypto_timing, CTLFLAG_RW,
111 &crypto_timing, 0, "Enable/disable crypto timing support");
119 mtx_init(&crypto_drivers_mtx, "crypto", "crypto driver table",
124 mtx_init(&crypto_q_mtx, "crypto", "crypto op queues", MTX_DEF);
126 TAILQ_INIT(&crp_ret_q);
127 TAILQ_INIT(&crp_ret_kq);
128 mtx_init(&crypto_ret_q_mtx, "crypto", "crypto return queues", MTX_DEF);
130 cryptop_zone = uma_zcreate("cryptop", sizeof (struct cryptop),
132 UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
133 cryptodesc_zone = uma_zcreate("cryptodesc", sizeof (struct cryptodesc),
135 UMA_ALIGN_PTR, UMA_ZONE_ZINIT);
136 if (cryptodesc_zone == NULL || cryptop_zone == NULL) {
137 printf("crypto_init: cannot setup crypto zones\n");
142 crypto_drivers_num = CRYPTO_DRIVERS_INITIAL;
143 crypto_drivers = malloc(crypto_drivers_num *
144 sizeof(struct cryptocap), M_CRYPTO_DATA, M_NOWAIT | M_ZERO);
145 if (crypto_drivers == NULL) {
146 printf("crypto_init: cannot setup crypto drivers\n");
151 error = kthread_create((void (*)(void *)) crypto_proc, NULL,
152 &cryptoproc, 0, 0, "crypto");
154 printf("crypto_init: cannot start crypto thread; error %d",
159 error = kthread_create((void (*)(void *)) crypto_ret_proc, NULL,
160 &cryptoretproc, 0, 0, "crypto returns");
162 printf("crypto_init: cannot start cryptoret thread; error %d",
173 * Signal a crypto thread to terminate. We use the driver
174 * table lock to synchronize the sleep/wakeups so that we
175 * are sure the threads have terminated before we release
176 * the data structures they use. See crypto_finis below
177 * for the other half of this song-and-dance.
180 crypto_terminate(struct proc **pp, void *q)
184 mtx_assert(&crypto_drivers_mtx, MA_OWNED);
189 PROC_LOCK(p); /* NB: insure we don't miss wakeup */
190 CRYPTO_DRIVER_UNLOCK(); /* let crypto_finis progress */
191 msleep(p, &p->p_mtx, PWAIT, "crypto_destroy", 0);
193 CRYPTO_DRIVER_LOCK();
201 * Terminate any crypto threads.
203 CRYPTO_DRIVER_LOCK();
204 crypto_terminate(&cryptoproc, &crp_q);
205 crypto_terminate(&cryptoretproc, &crp_ret_q);
206 CRYPTO_DRIVER_UNLOCK();
208 /* XXX flush queues??? */
211 * Reclaim dynamically allocated resources.
213 if (crypto_drivers != NULL)
214 free(crypto_drivers, M_CRYPTO_DATA);
216 if (cryptodesc_zone != NULL)
217 uma_zdestroy(cryptodesc_zone);
218 if (cryptop_zone != NULL)
219 uma_zdestroy(cryptop_zone);
220 mtx_destroy(&crypto_q_mtx);
221 mtx_destroy(&crypto_ret_q_mtx);
222 mtx_destroy(&crypto_drivers_mtx);
226 * Initialization code, both for static and dynamic loading.
229 crypto_modevent(module_t mod, int type, void *unused)
235 error = crypto_init();
236 if (error == 0 && bootverbose)
237 printf("crypto: <crypto core>\n");
240 /*XXX disallow if active sessions */
248 static moduledata_t crypto_mod = {
253 MODULE_VERSION(crypto, 1);
254 DECLARE_MODULE(crypto, crypto_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
255 MODULE_DEPEND(crypto, zlib, 1, 1, 1);
258 * Create a new session.
261 crypto_newsession(u_int64_t *sid, struct cryptoini *cri, int hard)
263 struct cryptoini *cr;
267 CRYPTO_DRIVER_LOCK();
269 if (crypto_drivers == NULL)
273 * The algorithm we use here is pretty stupid; just use the
274 * first driver that supports all the algorithms we need.
276 * XXX We need more smarts here (in real life too, but that's
277 * XXX another story altogether).
280 for (hid = 0; hid < crypto_drivers_num; hid++) {
281 struct cryptocap *cap = &crypto_drivers[hid];
283 * If it's not initialized or has remaining sessions
284 * referencing it, skip.
286 if (cap->cc_newsession == NULL ||
287 (cap->cc_flags & CRYPTOCAP_F_CLEANUP))
290 /* Hardware required -- ignore software drivers. */
291 if (hard > 0 && (cap->cc_flags & CRYPTOCAP_F_SOFTWARE))
293 /* Software required -- ignore hardware drivers. */
294 if (hard < 0 && (cap->cc_flags & CRYPTOCAP_F_SOFTWARE) == 0)
297 /* See if all the algorithms are supported. */
298 for (cr = cri; cr; cr = cr->cri_next)
299 if (cap->cc_alg[cr->cri_alg] == 0)
303 /* Ok, all algorithms are supported. */
306 * Can't do everything in one session.
308 * XXX Fix this. We need to inject a "virtual" session layer right
312 /* Call the driver initialization routine. */
313 lid = hid; /* Pass the driver ID. */
314 err = (*cap->cc_newsession)(cap->cc_arg, &lid, cri);
316 /* XXX assert (hid &~ 0xffffff) == 0 */
317 /* XXX assert (cap->cc_flags &~ 0xff) == 0 */
318 (*sid) = ((cap->cc_flags & 0xff) << 24) | hid;
320 (*sid) |= (lid & 0xffffffff);
327 CRYPTO_DRIVER_UNLOCK();
332 * Delete an existing session (or a reserved session on an unregistered
336 crypto_freesession(u_int64_t sid)
341 CRYPTO_DRIVER_LOCK();
343 if (crypto_drivers == NULL) {
348 /* Determine two IDs. */
349 hid = CRYPTO_SESID2HID(sid);
351 if (hid >= crypto_drivers_num) {
356 if (crypto_drivers[hid].cc_sessions)
357 crypto_drivers[hid].cc_sessions--;
359 /* Call the driver cleanup routine, if available. */
360 if (crypto_drivers[hid].cc_freesession)
361 err = crypto_drivers[hid].cc_freesession(
362 crypto_drivers[hid].cc_arg, sid);
367 * If this was the last session of a driver marked as invalid,
368 * make the entry available for reuse.
370 if ((crypto_drivers[hid].cc_flags & CRYPTOCAP_F_CLEANUP) &&
371 crypto_drivers[hid].cc_sessions == 0)
372 bzero(&crypto_drivers[hid], sizeof(struct cryptocap));
375 CRYPTO_DRIVER_UNLOCK();
380 * Return an unused driver id. Used by drivers prior to registering
381 * support for the algorithms they handle.
384 crypto_get_driverid(u_int32_t flags)
386 struct cryptocap *newdrv;
389 CRYPTO_DRIVER_LOCK();
391 for (i = 0; i < crypto_drivers_num; i++)
392 if (crypto_drivers[i].cc_process == NULL &&
393 (crypto_drivers[i].cc_flags & CRYPTOCAP_F_CLEANUP) == 0 &&
394 crypto_drivers[i].cc_sessions == 0)
397 /* Out of entries, allocate some more. */
398 if (i == crypto_drivers_num) {
399 /* Be careful about wrap-around. */
400 if (2 * crypto_drivers_num <= crypto_drivers_num) {
401 CRYPTO_DRIVER_UNLOCK();
402 printf("crypto: driver count wraparound!\n");
406 newdrv = malloc(2 * crypto_drivers_num *
407 sizeof(struct cryptocap), M_CRYPTO_DATA, M_NOWAIT|M_ZERO);
408 if (newdrv == NULL) {
409 CRYPTO_DRIVER_UNLOCK();
410 printf("crypto: no space to expand driver table!\n");
414 bcopy(crypto_drivers, newdrv,
415 crypto_drivers_num * sizeof(struct cryptocap));
417 crypto_drivers_num *= 2;
419 free(crypto_drivers, M_CRYPTO_DATA);
420 crypto_drivers = newdrv;
423 /* NB: state is zero'd on free */
424 crypto_drivers[i].cc_sessions = 1; /* Mark */
425 crypto_drivers[i].cc_flags = flags;
427 printf("crypto: assign driver %u, flags %u\n", i, flags);
429 CRYPTO_DRIVER_UNLOCK();
434 static struct cryptocap *
435 crypto_checkdriver(u_int32_t hid)
437 if (crypto_drivers == NULL)
439 return (hid >= crypto_drivers_num ? NULL : &crypto_drivers[hid]);
443 * Register support for a key-related algorithm. This routine
444 * is called once for each algorithm supported a driver.
447 crypto_kregister(u_int32_t driverid, int kalg, u_int32_t flags,
448 int (*kprocess)(void*, struct cryptkop *, int),
451 struct cryptocap *cap;
454 CRYPTO_DRIVER_LOCK();
456 cap = crypto_checkdriver(driverid);
458 (CRK_ALGORITM_MIN <= kalg && kalg <= CRK_ALGORITHM_MAX)) {
460 * XXX Do some performance testing to determine placing.
461 * XXX We probably need an auxiliary data structure that
462 * XXX describes relative performances.
465 cap->cc_kalg[kalg] = flags | CRYPTO_ALG_FLAG_SUPPORTED;
467 printf("crypto: driver %u registers key alg %u flags %u\n"
473 if (cap->cc_kprocess == NULL) {
475 cap->cc_kprocess = kprocess;
481 CRYPTO_DRIVER_UNLOCK();
486 * Register support for a non-key-related algorithm. This routine
487 * is called once for each such algorithm supported by a driver.
490 crypto_register(u_int32_t driverid, int alg, u_int16_t maxoplen,
492 int (*newses)(void*, u_int32_t*, struct cryptoini*),
493 int (*freeses)(void*, u_int64_t),
494 int (*process)(void*, struct cryptop *, int),
497 struct cryptocap *cap;
500 CRYPTO_DRIVER_LOCK();
502 cap = crypto_checkdriver(driverid);
503 /* NB: algorithms are in the range [1..max] */
505 (CRYPTO_ALGORITHM_MIN <= alg && alg <= CRYPTO_ALGORITHM_MAX)) {
507 * XXX Do some performance testing to determine placing.
508 * XXX We probably need an auxiliary data structure that
509 * XXX describes relative performances.
512 cap->cc_alg[alg] = flags | CRYPTO_ALG_FLAG_SUPPORTED;
513 cap->cc_max_op_len[alg] = maxoplen;
515 printf("crypto: driver %u registers alg %u flags %u maxoplen %u\n"
522 if (cap->cc_process == NULL) {
524 cap->cc_newsession = newses;
525 cap->cc_process = process;
526 cap->cc_freesession = freeses;
527 cap->cc_sessions = 0; /* Unmark */
533 CRYPTO_DRIVER_UNLOCK();
538 * Unregister a crypto driver. If there are pending sessions using it,
539 * leave enough information around so that subsequent calls using those
540 * sessions will correctly detect the driver has been unregistered and
544 crypto_unregister(u_int32_t driverid, int alg)
548 struct cryptocap *cap;
550 CRYPTO_DRIVER_LOCK();
552 cap = crypto_checkdriver(driverid);
554 (CRYPTO_ALGORITHM_MIN <= alg && alg <= CRYPTO_ALGORITHM_MAX) &&
555 cap->cc_alg[alg] != 0) {
556 cap->cc_alg[alg] = 0;
557 cap->cc_max_op_len[alg] = 0;
559 /* Was this the last algorithm ? */
560 for (i = 1; i <= CRYPTO_ALGORITHM_MAX; i++)
561 if (cap->cc_alg[i] != 0)
564 if (i == CRYPTO_ALGORITHM_MAX + 1) {
565 ses = cap->cc_sessions;
566 bzero(cap, sizeof(struct cryptocap));
569 * If there are pending sessions, just mark as invalid.
571 cap->cc_flags |= CRYPTOCAP_F_CLEANUP;
572 cap->cc_sessions = ses;
579 CRYPTO_DRIVER_UNLOCK();
584 * Unregister all algorithms associated with a crypto driver.
585 * If there are pending sessions using it, leave enough information
586 * around so that subsequent calls using those sessions will
587 * correctly detect the driver has been unregistered and reroute
591 crypto_unregister_all(u_int32_t driverid)
595 struct cryptocap *cap;
597 CRYPTO_DRIVER_LOCK();
599 cap = crypto_checkdriver(driverid);
601 for (i = CRYPTO_ALGORITHM_MIN; i <= CRYPTO_ALGORITHM_MAX; i++) {
603 cap->cc_max_op_len[i] = 0;
605 ses = cap->cc_sessions;
606 bzero(cap, sizeof(struct cryptocap));
609 * If there are pending sessions, just mark as invalid.
611 cap->cc_flags |= CRYPTOCAP_F_CLEANUP;
612 cap->cc_sessions = ses;
618 CRYPTO_DRIVER_UNLOCK();
623 * Clear blockage on a driver. The what parameter indicates whether
624 * the driver is now ready for cryptop's and/or cryptokop's.
627 crypto_unblock(u_int32_t driverid, int what)
629 struct cryptocap *cap;
633 cap = crypto_checkdriver(driverid);
636 if (what & CRYPTO_SYMQ) {
637 needwakeup |= cap->cc_qblocked;
638 cap->cc_qblocked = 0;
640 if (what & CRYPTO_ASYMQ) {
641 needwakeup |= cap->cc_kqblocked;
642 cap->cc_kqblocked = 0;
655 * Add a crypto request to a queue, to be processed by the kernel thread.
658 crypto_dispatch(struct cryptop *crp)
660 u_int32_t hid = CRYPTO_SESID2HID(crp->crp_sid);
663 cryptostats.cs_ops++;
667 binuptime(&crp->crp_tstamp);
671 if ((crp->crp_flags & CRYPTO_F_BATCH) == 0) {
672 struct cryptocap *cap;
674 * Caller marked the request to be processed
675 * immediately; dispatch it directly to the
676 * driver unless the driver is currently blocked.
678 cap = crypto_checkdriver(hid);
679 if (cap && !cap->cc_qblocked) {
680 result = crypto_invoke(crp, 0);
681 if (result == ERESTART) {
683 * The driver ran out of resources, mark the
684 * driver ``blocked'' for cryptop's and put
685 * the request on the queue.
687 * XXX ops are placed at the tail so their
688 * order is preserved but this can place them
689 * behind batch'd ops.
691 crypto_drivers[hid].cc_qblocked = 1;
692 TAILQ_INSERT_TAIL(&crp_q, crp, crp_next);
693 cryptostats.cs_blocks++;
698 * The driver is blocked, just queue the op until
699 * it unblocks and the kernel thread gets kicked.
701 TAILQ_INSERT_TAIL(&crp_q, crp, crp_next);
707 * Caller marked the request as ``ok to delay'';
708 * queue it for the dispatch thread. This is desirable
709 * when the operation is low priority and/or suitable
712 wasempty = TAILQ_EMPTY(&crp_q);
713 TAILQ_INSERT_TAIL(&crp_q, crp, crp_next);
724 * Add an asymetric crypto request to a queue,
725 * to be processed by the kernel thread.
728 crypto_kdispatch(struct cryptkop *krp)
730 struct cryptocap *cap;
733 cryptostats.cs_kops++;
736 cap = crypto_checkdriver(krp->krp_hid);
737 if (cap && !cap->cc_kqblocked) {
738 result = crypto_kinvoke(krp, 0);
739 if (result == ERESTART) {
741 * The driver ran out of resources, mark the
742 * driver ``blocked'' for cryptkop's and put
743 * the request back in the queue. It would
744 * best to put the request back where we got
745 * it but that's hard so for now we put it
746 * at the front. This should be ok; putting
747 * it at the end does not work.
749 crypto_drivers[krp->krp_hid].cc_kqblocked = 1;
750 TAILQ_INSERT_TAIL(&crp_kq, krp, krp_next);
751 cryptostats.cs_kblocks++;
755 * The driver is blocked, just queue the op until
756 * it unblocks and the kernel thread gets kicked.
758 TAILQ_INSERT_TAIL(&crp_kq, krp, krp_next);
767 * Dispatch an assymetric crypto request to the appropriate crypto devices.
770 crypto_kinvoke(struct cryptkop *krp, int hint)
775 mtx_assert(&crypto_q_mtx, MA_OWNED);
780 if (krp->krp_callback == NULL) {
781 free(krp, M_XDATA); /* XXX allocated in cryptodev */
785 for (hid = 0; hid < crypto_drivers_num; hid++) {
786 if ((crypto_drivers[hid].cc_flags & CRYPTOCAP_F_SOFTWARE) &&
787 !crypto_devallowsoft)
789 if (crypto_drivers[hid].cc_kprocess == NULL)
791 if ((crypto_drivers[hid].cc_kalg[krp->krp_op] &
792 CRYPTO_ALG_FLAG_SUPPORTED) == 0)
796 if (hid < crypto_drivers_num) {
798 error = crypto_drivers[hid].cc_kprocess(
799 crypto_drivers[hid].cc_karg, krp, hint);
804 krp->krp_status = error;
812 crypto_tstat(struct cryptotstat *ts, struct bintime *bt)
814 struct bintime now, delta;
820 delta.frac = now.frac - bt->frac;
821 delta.sec = now.sec - bt->sec;
824 bintime2timespec(&delta, &t);
825 timespecadd(&ts->acc, &t);
826 if (timespeccmp(&t, &ts->min, <))
828 if (timespeccmp(&t, &ts->max, >))
837 * Dispatch a crypto request to the appropriate crypto devices.
840 crypto_invoke(struct cryptop *crp, int hint)
843 int (*process)(void*, struct cryptop *, int);
847 crypto_tstat(&cryptostats.cs_invoke, &crp->crp_tstamp);
852 if (crp->crp_callback == NULL) {
856 if (crp->crp_desc == NULL) {
857 crp->crp_etype = EINVAL;
862 hid = CRYPTO_SESID2HID(crp->crp_sid);
863 if (hid < crypto_drivers_num) {
864 if (crypto_drivers[hid].cc_flags & CRYPTOCAP_F_CLEANUP)
865 crypto_freesession(crp->crp_sid);
866 process = crypto_drivers[hid].cc_process;
871 if (process == NULL) {
872 struct cryptodesc *crd;
876 * Driver has unregistered; migrate the session and return
877 * an error to the caller so they'll resubmit the op.
879 for (crd = crp->crp_desc; crd->crd_next; crd = crd->crd_next)
880 crd->CRD_INI.cri_next = &(crd->crd_next->CRD_INI);
882 if (crypto_newsession(&nid, &(crp->crp_desc->CRD_INI), 0) == 0)
885 crp->crp_etype = EAGAIN;
890 * Invoke the driver to process the request.
892 return (*process)(crypto_drivers[hid].cc_arg, crp, hint);
897 * Release a set of crypto descriptors.
900 crypto_freereq(struct cryptop *crp)
902 struct cryptodesc *crd;
907 while ((crd = crp->crp_desc) != NULL) {
908 crp->crp_desc = crd->crd_next;
909 uma_zfree(cryptodesc_zone, crd);
912 uma_zfree(cryptop_zone, crp);
916 * Acquire a set of crypto descriptors.
919 crypto_getreq(int num)
921 struct cryptodesc *crd;
924 crp = uma_zalloc(cryptop_zone, M_NOWAIT|M_ZERO);
927 crd = uma_zalloc(cryptodesc_zone, M_NOWAIT|M_ZERO);
933 crd->crd_next = crp->crp_desc;
941 * Invoke the callback on behalf of the driver.
944 crypto_done(struct cryptop *crp)
946 KASSERT((crp->crp_flags & CRYPTO_F_DONE) == 0,
947 ("crypto_done: op already done, flags 0x%x", crp->crp_flags));
948 crp->crp_flags |= CRYPTO_F_DONE;
949 if (crp->crp_etype != 0)
950 cryptostats.cs_errs++;
953 crypto_tstat(&cryptostats.cs_done, &crp->crp_tstamp);
956 * CBIMM means unconditionally do the callback immediately;
957 * CBIFSYNC means do the callback immediately only if the
958 * operation was done synchronously. Both are used to avoid
959 * doing extraneous context switches; the latter is mostly
960 * used with the software crypto driver.
962 if ((crp->crp_flags & CRYPTO_F_CBIMM) ||
963 ((crp->crp_flags & CRYPTO_F_CBIFSYNC) &&
964 (CRYPTO_SESID2CAPS(crp->crp_sid) & CRYPTOCAP_F_SYNC))) {
966 * Do the callback directly. This is ok when the
967 * callback routine does very little (e.g. the
968 * /dev/crypto callback method just does a wakeup).
973 * NB: We must copy the timestamp before
974 * doing the callback as the cryptop is
975 * likely to be reclaimed.
977 struct bintime t = crp->crp_tstamp;
978 crypto_tstat(&cryptostats.cs_cb, &t);
979 crp->crp_callback(crp);
980 crypto_tstat(&cryptostats.cs_finis, &t);
983 crp->crp_callback(crp);
987 * Normal case; queue the callback for the thread.
990 wasempty = TAILQ_EMPTY(&crp_ret_q);
991 TAILQ_INSERT_TAIL(&crp_ret_q, crp, crp_next);
994 wakeup_one(&crp_ret_q); /* shared wait channel */
995 CRYPTO_RETQ_UNLOCK();
1000 * Invoke the callback on behalf of the driver.
1003 crypto_kdone(struct cryptkop *krp)
1007 if (krp->krp_status != 0)
1008 cryptostats.cs_kerrs++;
1010 wasempty = TAILQ_EMPTY(&crp_ret_kq);
1011 TAILQ_INSERT_TAIL(&crp_ret_kq, krp, krp_next);
1014 wakeup_one(&crp_ret_q); /* shared wait channel */
1015 CRYPTO_RETQ_UNLOCK();
1019 crypto_getfeat(int *featp)
1021 int hid, kalg, feat = 0;
1023 if (!crypto_userasymcrypto)
1026 CRYPTO_DRIVER_LOCK();
1027 for (hid = 0; hid < crypto_drivers_num; hid++) {
1028 if ((crypto_drivers[hid].cc_flags & CRYPTOCAP_F_SOFTWARE) &&
1029 !crypto_devallowsoft) {
1032 if (crypto_drivers[hid].cc_kprocess == NULL)
1034 for (kalg = 0; kalg < CRK_ALGORITHM_MAX; kalg++)
1035 if ((crypto_drivers[hid].cc_kalg[kalg] &
1036 CRYPTO_ALG_FLAG_SUPPORTED) != 0)
1039 CRYPTO_DRIVER_UNLOCK();
1046 * Terminate a thread at module unload. The process that
1047 * initiated this is waiting for us to signal that we're gone;
1048 * wake it up and exit. We use the driver table lock to insure
1049 * we don't do the wakeup before they're waiting. There is no
1050 * race here because the waiter sleeps on the proc lock for the
1051 * thread so it gets notified at the right time because of an
1052 * extra wakeup that's done in exit1().
1055 crypto_finis(void *chan)
1057 CRYPTO_DRIVER_LOCK();
1059 CRYPTO_DRIVER_UNLOCK();
1064 * Crypto thread, dispatches crypto requests.
1069 struct cryptop *crp, *submit;
1070 struct cryptkop *krp;
1071 struct cryptocap *cap;
1077 * Find the first element in the queue that can be
1078 * processed and look-ahead to see if multiple ops
1079 * are ready for the same driver.
1083 TAILQ_FOREACH(crp, &crp_q, crp_next) {
1084 u_int32_t hid = CRYPTO_SESID2HID(crp->crp_sid);
1085 cap = crypto_checkdriver(hid);
1086 if (cap == NULL || cap->cc_process == NULL) {
1087 /* Op needs to be migrated, process it. */
1092 if (!cap->cc_qblocked) {
1093 if (submit != NULL) {
1095 * We stop on finding another op,
1096 * regardless whether its for the same
1097 * driver or not. We could keep
1098 * searching the queue but it might be
1099 * better to just use a per-driver
1102 if (CRYPTO_SESID2HID(submit->crp_sid) == hid)
1103 hint = CRYPTO_HINT_MORE;
1107 if ((submit->crp_flags & CRYPTO_F_BATCH) == 0)
1109 /* keep scanning for more are q'd */
1113 if (submit != NULL) {
1114 TAILQ_REMOVE(&crp_q, submit, crp_next);
1115 result = crypto_invoke(submit, hint);
1116 if (result == ERESTART) {
1118 * The driver ran out of resources, mark the
1119 * driver ``blocked'' for cryptop's and put
1120 * the request back in the queue. It would
1121 * best to put the request back where we got
1122 * it but that's hard so for now we put it
1123 * at the front. This should be ok; putting
1124 * it at the end does not work.
1126 /* XXX validate sid again? */
1127 crypto_drivers[CRYPTO_SESID2HID(submit->crp_sid)].cc_qblocked = 1;
1128 TAILQ_INSERT_HEAD(&crp_q, submit, crp_next);
1129 cryptostats.cs_blocks++;
1133 /* As above, but for key ops */
1134 TAILQ_FOREACH(krp, &crp_kq, krp_next) {
1135 cap = crypto_checkdriver(krp->krp_hid);
1136 if (cap == NULL || cap->cc_kprocess == NULL) {
1137 /* Op needs to be migrated, process it. */
1140 if (!cap->cc_kqblocked)
1144 TAILQ_REMOVE(&crp_kq, krp, krp_next);
1145 result = crypto_kinvoke(krp, 0);
1146 if (result == ERESTART) {
1148 * The driver ran out of resources, mark the
1149 * driver ``blocked'' for cryptkop's and put
1150 * the request back in the queue. It would
1151 * best to put the request back where we got
1152 * it but that's hard so for now we put it
1153 * at the front. This should be ok; putting
1154 * it at the end does not work.
1156 /* XXX validate sid again? */
1157 crypto_drivers[krp->krp_hid].cc_kqblocked = 1;
1158 TAILQ_INSERT_HEAD(&crp_kq, krp, krp_next);
1159 cryptostats.cs_kblocks++;
1163 if (submit == NULL && krp == NULL) {
1165 * Nothing more to be processed. Sleep until we're
1166 * woken because there are more ops to process.
1167 * This happens either by submission or by a driver
1168 * becoming unblocked and notifying us through
1169 * crypto_unblock. Note that when we wakeup we
1170 * start processing each queue again from the
1171 * front. It's not clear that it's important to
1172 * preserve this ordering since ops may finish
1173 * out of order if dispatched to different devices
1174 * and some become blocked while others do not.
1176 msleep(&crp_q, &crypto_q_mtx, PWAIT, "crypto_wait", 0);
1177 if (cryptoproc == NULL)
1179 cryptostats.cs_intrs++;
1184 crypto_finis(&crp_q);
1188 * Crypto returns thread, does callbacks for processed crypto requests.
1189 * Callbacks are done here, rather than in the crypto drivers, because
1190 * callbacks typically are expensive and would slow interrupt handling.
1193 crypto_ret_proc(void)
1195 struct cryptop *crpt;
1196 struct cryptkop *krpt;
1200 /* Harvest return q's for completed ops */
1201 crpt = TAILQ_FIRST(&crp_ret_q);
1203 TAILQ_REMOVE(&crp_ret_q, crpt, crp_next);
1205 krpt = TAILQ_FIRST(&crp_ret_kq);
1207 TAILQ_REMOVE(&crp_ret_kq, krpt, krp_next);
1209 if (crpt != NULL || krpt != NULL) {
1210 CRYPTO_RETQ_UNLOCK();
1212 * Run callbacks unlocked.
1215 #ifdef CRYPTO_TIMING
1216 if (crypto_timing) {
1218 * NB: We must copy the timestamp before
1219 * doing the callback as the cryptop is
1220 * likely to be reclaimed.
1222 struct bintime t = crpt->crp_tstamp;
1223 crypto_tstat(&cryptostats.cs_cb, &t);
1224 crpt->crp_callback(crpt);
1225 crypto_tstat(&cryptostats.cs_finis, &t);
1228 crpt->crp_callback(crpt);
1231 krpt->krp_callback(krpt);
1235 * Nothing more to be processed. Sleep until we're
1236 * woken because there are more returns to process.
1238 msleep(&crp_ret_q, &crypto_ret_q_mtx, PWAIT,
1239 "crypto_ret_wait", 0);
1240 if (cryptoretproc == NULL)
1242 cryptostats.cs_rets++;
1245 CRYPTO_RETQ_UNLOCK();
1247 crypto_finis(&crp_ret_q);