crypto.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741
  1. /*-
  2. * Linux port done by David McCullough <[email protected]>
  3. * Copyright (C) 2006-2007 David McCullough
  4. * Copyright (C) 2004-2005 Intel Corporation.
  5. * The license and original author are listed below.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * Copyright (c) 2002-2006 Sam Leffler. All rights reserved.
  9. *
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  19. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  20. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #if 0
  30. #include <sys/cdefs.h>
  31. __FBSDID("$FreeBSD: src/sys/opencrypto/crypto.c,v 1.27 2007/03/21 03:42:51 sam Exp $");
  32. #endif
  33. /*
  34. * Cryptographic Subsystem.
  35. *
  36. * This code is derived from the Openbsd Cryptographic Framework (OCF)
  37. * that has the copyright shown below. Very little of the original
  38. * code remains.
  39. */
  40. /*-
  41. * The author of this code is Angelos D. Keromytis ([email protected])
  42. *
  43. * This code was written by Angelos D. Keromytis in Athens, Greece, in
  44. * February 2000. Network Security Technologies Inc. (NSTI) kindly
  45. * supported the development of this code.
  46. *
  47. * Copyright (c) 2000, 2001 Angelos D. Keromytis
  48. *
  49. * Permission to use, copy, and modify this software with or without fee
  50. * is hereby granted, provided that this entire notice is included in
  51. * all source code copies of any software which is or includes a copy or
  52. * modification of this software.
  53. *
  54. * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
  55. * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
  56. * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
  57. * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
  58. * PURPOSE.
  59. *
  60. __FBSDID("$FreeBSD: src/sys/opencrypto/crypto.c,v 1.16 2005/01/07 02:29:16 imp Exp $");
  61. */
  62. #ifndef AUTOCONF_INCLUDED
  63. #include <linux/config.h>
  64. #endif
  65. #include <linux/module.h>
  66. #include <linux/init.h>
  67. #include <linux/list.h>
  68. #include <linux/slab.h>
  69. #include <linux/wait.h>
  70. #include <linux/sched.h>
  71. #include <linux/spinlock.h>
  72. #include <linux/version.h>
  73. #include <cryptodev.h>
  74. /*
  75. * keep track of whether or not we have been initialised, a big
  76. * issue if we are linked into the kernel and a driver gets started before
  77. * us
  78. */
  79. static int crypto_initted = 0;
  80. /*
  81. * Crypto drivers register themselves by allocating a slot in the
  82. * crypto_drivers table with crypto_get_driverid() and then registering
  83. * each algorithm they support with crypto_register() and crypto_kregister().
  84. */
  85. /*
  86. * lock on driver table
  87. * we track its state as spin_is_locked does not do anything on non-SMP boxes
  88. */
  89. static spinlock_t crypto_drivers_lock;
  90. static int crypto_drivers_locked; /* for non-SMP boxes */
  91. #define CRYPTO_DRIVER_LOCK() \
  92. ({ \
  93. spin_lock_irqsave(&crypto_drivers_lock, d_flags); \
  94. crypto_drivers_locked = 1; \
  95. dprintk("%s,%d: DRIVER_LOCK()\n", __FILE__, __LINE__); \
  96. })
  97. #define CRYPTO_DRIVER_UNLOCK() \
  98. ({ \
  99. dprintk("%s,%d: DRIVER_UNLOCK()\n", __FILE__, __LINE__); \
  100. crypto_drivers_locked = 0; \
  101. spin_unlock_irqrestore(&crypto_drivers_lock, d_flags); \
  102. })
  103. #define CRYPTO_DRIVER_ASSERT() \
  104. ({ \
  105. if (!crypto_drivers_locked) { \
  106. dprintk("%s,%d: DRIVER_ASSERT!\n", __FILE__, __LINE__); \
  107. } \
  108. })
  109. /*
  110. * Crypto device/driver capabilities structure.
  111. *
  112. * Synchronization:
  113. * (d) - protected by CRYPTO_DRIVER_LOCK()
  114. * (q) - protected by CRYPTO_Q_LOCK()
  115. * Not tagged fields are read-only.
  116. */
  117. struct cryptocap {
  118. device_t cc_dev; /* (d) device/driver */
  119. u_int32_t cc_sessions; /* (d) # of sessions */
  120. u_int32_t cc_koperations; /* (d) # os asym operations */
  121. /*
  122. * Largest possible operator length (in bits) for each type of
  123. * encryption algorithm. XXX not used
  124. */
  125. u_int16_t cc_max_op_len[CRYPTO_ALGORITHM_MAX + 1];
  126. u_int8_t cc_alg[CRYPTO_ALGORITHM_MAX + 1];
  127. u_int8_t cc_kalg[CRK_ALGORITHM_MAX + 1];
  128. int cc_flags; /* (d) flags */
  129. #define CRYPTOCAP_F_CLEANUP 0x80000000 /* needs resource cleanup */
  130. int cc_qblocked; /* (q) symmetric q blocked */
  131. int cc_kqblocked; /* (q) asymmetric q blocked */
  132. };
  133. static struct cryptocap *crypto_drivers = NULL;
  134. static int crypto_drivers_num = 0;
  135. /*
  136. * There are two queues for crypto requests; one for symmetric (e.g.
  137. * cipher) operations and one for asymmetric (e.g. MOD)operations.
  138. * A single mutex is used to lock access to both queues. We could
  139. * have one per-queue but having one simplifies handling of block/unblock
  140. * operations.
  141. */
  142. static int crp_sleep = 0;
  143. static LIST_HEAD(crp_q); /* request queues */
  144. static LIST_HEAD(crp_kq);
  145. static spinlock_t crypto_q_lock;
  146. int crypto_all_qblocked = 0; /* protect with Q_LOCK */
  147. module_param(crypto_all_qblocked, int, 0444);
  148. MODULE_PARM_DESC(crypto_all_qblocked, "Are all crypto queues blocked");
  149. int crypto_all_kqblocked = 0; /* protect with Q_LOCK */
  150. module_param(crypto_all_kqblocked, int, 0444);
  151. MODULE_PARM_DESC(crypto_all_kqblocked, "Are all asym crypto queues blocked");
  152. #define CRYPTO_Q_LOCK() \
  153. ({ \
  154. spin_lock_irqsave(&crypto_q_lock, q_flags); \
  155. dprintk("%s,%d: Q_LOCK()\n", __FILE__, __LINE__); \
  156. })
  157. #define CRYPTO_Q_UNLOCK() \
  158. ({ \
  159. dprintk("%s,%d: Q_UNLOCK()\n", __FILE__, __LINE__); \
  160. spin_unlock_irqrestore(&crypto_q_lock, q_flags); \
  161. })
  162. /*
  163. * There are two queues for processing completed crypto requests; one
  164. * for the symmetric and one for the asymmetric ops. We only need one
  165. * but have two to avoid type futzing (cryptop vs. cryptkop). A single
  166. * mutex is used to lock access to both queues. Note that this lock
  167. * must be separate from the lock on request queues to insure driver
  168. * callbacks don't generate lock order reversals.
  169. */
  170. static LIST_HEAD(crp_ret_q); /* callback queues */
  171. static LIST_HEAD(crp_ret_kq);
  172. static spinlock_t crypto_ret_q_lock;
  173. #define CRYPTO_RETQ_LOCK() \
  174. ({ \
  175. spin_lock_irqsave(&crypto_ret_q_lock, r_flags); \
  176. dprintk("%s,%d: RETQ_LOCK\n", __FILE__, __LINE__); \
  177. })
  178. #define CRYPTO_RETQ_UNLOCK() \
  179. ({ \
  180. dprintk("%s,%d: RETQ_UNLOCK\n", __FILE__, __LINE__); \
  181. spin_unlock_irqrestore(&crypto_ret_q_lock, r_flags); \
  182. })
  183. #define CRYPTO_RETQ_EMPTY() (list_empty(&crp_ret_q) && list_empty(&crp_ret_kq))
  184. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
  185. static kmem_cache_t *cryptop_zone;
  186. static kmem_cache_t *cryptodesc_zone;
  187. #else
  188. static struct kmem_cache *cryptop_zone;
  189. static struct kmem_cache *cryptodesc_zone;
  190. #endif
  191. #define debug crypto_debug
  192. int crypto_debug = 0;
  193. module_param(crypto_debug, int, 0644);
  194. MODULE_PARM_DESC(crypto_debug, "Enable debug");
  195. EXPORT_SYMBOL(crypto_debug);
  196. /*
  197. * Maximum number of outstanding crypto requests before we start
  198. * failing requests. We need this to prevent DOS when too many
  199. * requests are arriving for us to keep up. Otherwise we will
  200. * run the system out of memory. Since crypto is slow, we are
  201. * usually the bottleneck that needs to say, enough is enough.
  202. *
  203. * We cannot print errors when this condition occurs, we are already too
  204. * slow, printing anything will just kill us
  205. */
  206. static int crypto_q_cnt = 0;
  207. module_param(crypto_q_cnt, int, 0444);
  208. MODULE_PARM_DESC(crypto_q_cnt,
  209. "Current number of outstanding crypto requests");
  210. static int crypto_q_max = 1000;
  211. module_param(crypto_q_max, int, 0644);
  212. MODULE_PARM_DESC(crypto_q_max,
  213. "Maximum number of outstanding crypto requests");
  214. #define bootverbose crypto_verbose
  215. static int crypto_verbose = 0;
  216. module_param(crypto_verbose, int, 0644);
  217. MODULE_PARM_DESC(crypto_verbose,
  218. "Enable verbose crypto startup");
  219. int crypto_usercrypto = 1; /* userland may do crypto reqs */
  220. module_param(crypto_usercrypto, int, 0644);
  221. MODULE_PARM_DESC(crypto_usercrypto,
  222. "Enable/disable user-mode access to crypto support");
  223. int crypto_userasymcrypto = 1; /* userland may do asym crypto reqs */
  224. module_param(crypto_userasymcrypto, int, 0644);
  225. MODULE_PARM_DESC(crypto_userasymcrypto,
  226. "Enable/disable user-mode access to asymmetric crypto support");
  227. int crypto_devallowsoft = 0; /* only use hardware crypto */
  228. module_param(crypto_devallowsoft, int, 0644);
  229. MODULE_PARM_DESC(crypto_devallowsoft,
  230. "Enable/disable use of software crypto support");
  231. static pid_t cryptoproc = (pid_t) -1;
  232. static struct completion cryptoproc_exited;
  233. static DECLARE_WAIT_QUEUE_HEAD(cryptoproc_wait);
  234. static pid_t cryptoretproc = (pid_t) -1;
  235. static struct completion cryptoretproc_exited;
  236. static DECLARE_WAIT_QUEUE_HEAD(cryptoretproc_wait);
  237. static int crypto_proc(void *arg);
  238. static int crypto_ret_proc(void *arg);
  239. static int crypto_invoke(struct cryptocap *cap, struct cryptop *crp, int hint);
  240. static int crypto_kinvoke(struct cryptkop *krp, int flags);
  241. static void crypto_exit(void);
  242. static int crypto_init(void);
  243. static struct cryptostats cryptostats;
  244. static struct cryptocap *
  245. crypto_checkdriver(u_int32_t hid)
  246. {
  247. if (crypto_drivers == NULL)
  248. return NULL;
  249. return (hid >= crypto_drivers_num ? NULL : &crypto_drivers[hid]);
  250. }
  251. /*
  252. * Compare a driver's list of supported algorithms against another
  253. * list; return non-zero if all algorithms are supported.
  254. */
  255. static int
  256. driver_suitable(const struct cryptocap *cap, const struct cryptoini *cri)
  257. {
  258. const struct cryptoini *cr;
  259. /* See if all the algorithms are supported. */
  260. for (cr = cri; cr; cr = cr->cri_next)
  261. if (cap->cc_alg[cr->cri_alg] == 0)
  262. return 0;
  263. return 1;
  264. }
  265. /*
  266. * Select a driver for a new session that supports the specified
  267. * algorithms and, optionally, is constrained according to the flags.
  268. * The algorithm we use here is pretty stupid; just use the
  269. * first driver that supports all the algorithms we need. If there
  270. * are multiple drivers we choose the driver with the fewest active
  271. * sessions. We prefer hardware-backed drivers to software ones.
  272. *
  273. * XXX We need more smarts here (in real life too, but that's
  274. * XXX another story altogether).
  275. */
  276. static struct cryptocap *
  277. crypto_select_driver(const struct cryptoini *cri, int flags)
  278. {
  279. struct cryptocap *cap, *best;
  280. int match, hid;
  281. CRYPTO_DRIVER_ASSERT();
  282. /*
  283. * Look first for hardware crypto devices if permitted.
  284. */
  285. if (flags & CRYPTOCAP_F_HARDWARE)
  286. match = CRYPTOCAP_F_HARDWARE;
  287. else
  288. match = CRYPTOCAP_F_SOFTWARE;
  289. best = NULL;
  290. again:
  291. for (hid = 0; hid < crypto_drivers_num; hid++) {
  292. cap = &crypto_drivers[hid];
  293. /*
  294. * If it's not initialized, is in the process of
  295. * going away, or is not appropriate (hardware
  296. * or software based on match), then skip.
  297. */
  298. if (cap->cc_dev == NULL ||
  299. (cap->cc_flags & CRYPTOCAP_F_CLEANUP) ||
  300. (cap->cc_flags & match) == 0)
  301. continue;
  302. /* verify all the algorithms are supported. */
  303. if (driver_suitable(cap, cri)) {
  304. if (best == NULL ||
  305. cap->cc_sessions < best->cc_sessions)
  306. best = cap;
  307. }
  308. }
  309. if (best != NULL)
  310. return best;
  311. if (match == CRYPTOCAP_F_HARDWARE && (flags & CRYPTOCAP_F_SOFTWARE)) {
  312. /* sort of an Algol 68-style for loop */
  313. match = CRYPTOCAP_F_SOFTWARE;
  314. goto again;
  315. }
  316. return best;
  317. }
  318. /*
  319. * Create a new session. The crid argument specifies a crypto
  320. * driver to use or constraints on a driver to select (hardware
  321. * only, software only, either). Whatever driver is selected
  322. * must be capable of the requested crypto algorithms.
  323. */
  324. int
  325. crypto_newsession(u_int64_t *sid, struct cryptoini *cri, int crid)
  326. {
  327. struct cryptocap *cap;
  328. u_int32_t hid, lid;
  329. int err;
  330. unsigned long d_flags;
  331. CRYPTO_DRIVER_LOCK();
  332. if ((crid & (CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE)) == 0) {
  333. /*
  334. * Use specified driver; verify it is capable.
  335. */
  336. cap = crypto_checkdriver(crid);
  337. if (cap != NULL && !driver_suitable(cap, cri))
  338. cap = NULL;
  339. } else {
  340. /*
  341. * No requested driver; select based on crid flags.
  342. */
  343. cap = crypto_select_driver(cri, crid);
  344. /*
  345. * if NULL then can't do everything in one session.
  346. * XXX Fix this. We need to inject a "virtual" session
  347. * XXX layer right about here.
  348. */
  349. }
  350. if (cap != NULL) {
  351. /* Call the driver initialization routine. */
  352. hid = cap - crypto_drivers;
  353. lid = hid; /* Pass the driver ID. */
  354. cap->cc_sessions++;
  355. CRYPTO_DRIVER_UNLOCK();
  356. err = CRYPTODEV_NEWSESSION(cap->cc_dev, &lid, cri);
  357. CRYPTO_DRIVER_LOCK();
  358. if (err == 0) {
  359. (*sid) = (cap->cc_flags & 0xff000000)
  360. | (hid & 0x00ffffff);
  361. (*sid) <<= 32;
  362. (*sid) |= (lid & 0xffffffff);
  363. } else
  364. cap->cc_sessions--;
  365. } else
  366. err = EINVAL;
  367. CRYPTO_DRIVER_UNLOCK();
  368. return err;
  369. }
  370. static void
  371. crypto_remove(struct cryptocap *cap)
  372. {
  373. CRYPTO_DRIVER_ASSERT();
  374. if (cap->cc_sessions == 0 && cap->cc_koperations == 0)
  375. bzero(cap, sizeof(*cap));
  376. }
  377. /*
  378. * Delete an existing session (or a reserved session on an unregistered
  379. * driver).
  380. */
  381. int
  382. crypto_freesession(u_int64_t sid)
  383. {
  384. struct cryptocap *cap;
  385. u_int32_t hid;
  386. int err = 0;
  387. unsigned long d_flags;
  388. dprintk("%s()\n", __FUNCTION__);
  389. CRYPTO_DRIVER_LOCK();
  390. if (crypto_drivers == NULL) {
  391. err = EINVAL;
  392. goto done;
  393. }
  394. /* Determine two IDs. */
  395. hid = CRYPTO_SESID2HID(sid);
  396. if (hid >= crypto_drivers_num) {
  397. dprintk("%s - INVALID DRIVER NUM %d\n", __FUNCTION__, hid);
  398. err = ENOENT;
  399. goto done;
  400. }
  401. cap = &crypto_drivers[hid];
  402. if (cap->cc_dev) {
  403. CRYPTO_DRIVER_UNLOCK();
  404. /* Call the driver cleanup routine, if available, unlocked. */
  405. err = CRYPTODEV_FREESESSION(cap->cc_dev, sid);
  406. CRYPTO_DRIVER_LOCK();
  407. }
  408. if (cap->cc_sessions)
  409. cap->cc_sessions--;
  410. if (cap->cc_flags & CRYPTOCAP_F_CLEANUP)
  411. crypto_remove(cap);
  412. done:
  413. CRYPTO_DRIVER_UNLOCK();
  414. return err;
  415. }
  416. /*
  417. * Return an unused driver id. Used by drivers prior to registering
  418. * support for the algorithms they handle.
  419. */
  420. int32_t
  421. crypto_get_driverid(device_t dev, int flags)
  422. {
  423. struct cryptocap *newdrv;
  424. int i;
  425. unsigned long d_flags;
  426. if ((flags & (CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE)) == 0) {
  427. printf("%s: no flags specified when registering driver\n",
  428. device_get_nameunit(dev));
  429. return -1;
  430. }
  431. CRYPTO_DRIVER_LOCK();
  432. for (i = 0; i < crypto_drivers_num; i++) {
  433. if (crypto_drivers[i].cc_dev == NULL &&
  434. (crypto_drivers[i].cc_flags & CRYPTOCAP_F_CLEANUP) == 0) {
  435. break;
  436. }
  437. }
  438. /* Out of entries, allocate some more. */
  439. if (i == crypto_drivers_num) {
  440. /* Be careful about wrap-around. */
  441. if (2 * crypto_drivers_num <= crypto_drivers_num) {
  442. CRYPTO_DRIVER_UNLOCK();
  443. printk("crypto: driver count wraparound!\n");
  444. return -1;
  445. }
  446. newdrv = kmalloc(2 * crypto_drivers_num * sizeof(struct cryptocap),
  447. GFP_KERNEL);
  448. if (newdrv == NULL) {
  449. CRYPTO_DRIVER_UNLOCK();
  450. printk("crypto: no space to expand driver table!\n");
  451. return -1;
  452. }
  453. memcpy(newdrv, crypto_drivers,
  454. crypto_drivers_num * sizeof(struct cryptocap));
  455. memset(&newdrv[crypto_drivers_num], 0,
  456. crypto_drivers_num * sizeof(struct cryptocap));
  457. crypto_drivers_num *= 2;
  458. kfree(crypto_drivers);
  459. crypto_drivers = newdrv;
  460. }
  461. /* NB: state is zero'd on free */
  462. crypto_drivers[i].cc_sessions = 1; /* Mark */
  463. crypto_drivers[i].cc_dev = dev;
  464. crypto_drivers[i].cc_flags = flags;
  465. if (bootverbose)
  466. printf("crypto: assign %s driver id %u, flags %u\n",
  467. device_get_nameunit(dev), i, flags);
  468. CRYPTO_DRIVER_UNLOCK();
  469. return i;
  470. }
  471. /*
  472. * Lookup a driver by name. We match against the full device
  473. * name and unit, and against just the name. The latter gives
  474. * us a simple widlcarding by device name. On success return the
  475. * driver/hardware identifier; otherwise return -1.
  476. */
  477. int
  478. crypto_find_driver(const char *match)
  479. {
  480. int i, len = strlen(match);
  481. unsigned long d_flags;
  482. CRYPTO_DRIVER_LOCK();
  483. for (i = 0; i < crypto_drivers_num; i++) {
  484. device_t dev = crypto_drivers[i].cc_dev;
  485. if (dev == NULL ||
  486. (crypto_drivers[i].cc_flags & CRYPTOCAP_F_CLEANUP))
  487. continue;
  488. if (strncmp(match, device_get_nameunit(dev), len) == 0 ||
  489. strncmp(match, device_get_name(dev), len) == 0)
  490. break;
  491. }
  492. CRYPTO_DRIVER_UNLOCK();
  493. return i < crypto_drivers_num ? i : -1;
  494. }
  495. /*
  496. * Return the device_t for the specified driver or NULL
  497. * if the driver identifier is invalid.
  498. */
  499. device_t
  500. crypto_find_device_byhid(int hid)
  501. {
  502. struct cryptocap *cap = crypto_checkdriver(hid);
  503. return cap != NULL ? cap->cc_dev : NULL;
  504. }
  505. /*
  506. * Return the device/driver capabilities.
  507. */
  508. int
  509. crypto_getcaps(int hid)
  510. {
  511. struct cryptocap *cap = crypto_checkdriver(hid);
  512. return cap != NULL ? cap->cc_flags : 0;
  513. }
  514. /*
  515. * Register support for a key-related algorithm. This routine
  516. * is called once for each algorithm supported a driver.
  517. */
  518. int
  519. crypto_kregister(u_int32_t driverid, int kalg, u_int32_t flags)
  520. {
  521. struct cryptocap *cap;
  522. int err;
  523. unsigned long d_flags;
  524. dprintk("%s()\n", __FUNCTION__);
  525. CRYPTO_DRIVER_LOCK();
  526. cap = crypto_checkdriver(driverid);
  527. if (cap != NULL &&
  528. (CRK_ALGORITM_MIN <= kalg && kalg <= CRK_ALGORITHM_MAX)) {
  529. /*
  530. * XXX Do some performance testing to determine placing.
  531. * XXX We probably need an auxiliary data structure that
  532. * XXX describes relative performances.
  533. */
  534. cap->cc_kalg[kalg] = flags | CRYPTO_ALG_FLAG_SUPPORTED;
  535. if (bootverbose)
  536. printf("crypto: %s registers key alg %u flags %u\n"
  537. , device_get_nameunit(cap->cc_dev)
  538. , kalg
  539. , flags
  540. );
  541. err = 0;
  542. } else
  543. err = EINVAL;
  544. CRYPTO_DRIVER_UNLOCK();
  545. return err;
  546. }
  547. /*
  548. * Register support for a non-key-related algorithm. This routine
  549. * is called once for each such algorithm supported by a driver.
  550. */
  551. int
  552. crypto_register(u_int32_t driverid, int alg, u_int16_t maxoplen,
  553. u_int32_t flags)
  554. {
  555. struct cryptocap *cap;
  556. int err;
  557. unsigned long d_flags;
  558. dprintk("%s(id=0x%x, alg=%d, maxoplen=%d, flags=0x%x)\n", __FUNCTION__,
  559. driverid, alg, maxoplen, flags);
  560. CRYPTO_DRIVER_LOCK();
  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. cap->cc_alg[alg] = flags | CRYPTO_ALG_FLAG_SUPPORTED;
  571. cap->cc_max_op_len[alg] = maxoplen;
  572. if (bootverbose)
  573. printf("crypto: %s registers alg %u flags %u maxoplen %u\n"
  574. , device_get_nameunit(cap->cc_dev)
  575. , alg
  576. , flags
  577. , maxoplen
  578. );
  579. cap->cc_sessions = 0; /* Unmark */
  580. err = 0;
  581. } else
  582. err = EINVAL;
  583. CRYPTO_DRIVER_UNLOCK();
  584. return err;
  585. }
  586. static void
  587. driver_finis(struct cryptocap *cap)
  588. {
  589. u_int32_t ses, kops;
  590. CRYPTO_DRIVER_ASSERT();
  591. ses = cap->cc_sessions;
  592. kops = cap->cc_koperations;
  593. bzero(cap, sizeof(*cap));
  594. if (ses != 0 || kops != 0) {
  595. /*
  596. * If there are pending sessions,
  597. * just mark as invalid.
  598. */
  599. cap->cc_flags |= CRYPTOCAP_F_CLEANUP;
  600. cap->cc_sessions = ses;
  601. cap->cc_koperations = kops;
  602. }
  603. }
  604. /*
  605. * Unregister a crypto driver. If there are pending sessions using it,
  606. * leave enough information around so that subsequent calls using those
  607. * sessions will correctly detect the driver has been unregistered and
  608. * reroute requests.
  609. */
  610. int
  611. crypto_unregister(u_int32_t driverid, int alg)
  612. {
  613. struct cryptocap *cap;
  614. int i, err;
  615. unsigned long d_flags;
  616. dprintk("%s()\n", __FUNCTION__);
  617. CRYPTO_DRIVER_LOCK();
  618. cap = crypto_checkdriver(driverid);
  619. if (cap != NULL &&
  620. (CRYPTO_ALGORITHM_MIN <= alg && alg <= CRYPTO_ALGORITHM_MAX) &&
  621. cap->cc_alg[alg] != 0) {
  622. cap->cc_alg[alg] = 0;
  623. cap->cc_max_op_len[alg] = 0;
  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. if (i == CRYPTO_ALGORITHM_MAX + 1)
  629. driver_finis(cap);
  630. err = 0;
  631. } else
  632. err = EINVAL;
  633. CRYPTO_DRIVER_UNLOCK();
  634. return err;
  635. }
  636. /*
  637. * Unregister all algorithms associated with a crypto driver.
  638. * If there are pending sessions using it, leave enough information
  639. * around so that subsequent calls using those sessions will
  640. * correctly detect the driver has been unregistered and reroute
  641. * requests.
  642. */
  643. int
  644. crypto_unregister_all(u_int32_t driverid)
  645. {
  646. struct cryptocap *cap;
  647. int err;
  648. unsigned long d_flags;
  649. dprintk("%s()\n", __FUNCTION__);
  650. CRYPTO_DRIVER_LOCK();
  651. cap = crypto_checkdriver(driverid);
  652. if (cap != NULL) {
  653. driver_finis(cap);
  654. err = 0;
  655. } else
  656. err = EINVAL;
  657. CRYPTO_DRIVER_UNLOCK();
  658. return err;
  659. }
  660. /*
  661. * Clear blockage on a driver. The what parameter indicates whether
  662. * the driver is now ready for cryptop's and/or cryptokop's.
  663. */
  664. int
  665. crypto_unblock(u_int32_t driverid, int what)
  666. {
  667. struct cryptocap *cap;
  668. int err;
  669. unsigned long q_flags;
  670. CRYPTO_Q_LOCK();
  671. cap = crypto_checkdriver(driverid);
  672. if (cap != NULL) {
  673. if (what & CRYPTO_SYMQ) {
  674. cap->cc_qblocked = 0;
  675. crypto_all_qblocked = 0;
  676. }
  677. if (what & CRYPTO_ASYMQ) {
  678. cap->cc_kqblocked = 0;
  679. crypto_all_kqblocked = 0;
  680. }
  681. if (crp_sleep)
  682. wake_up_interruptible(&cryptoproc_wait);
  683. err = 0;
  684. } else
  685. err = EINVAL;
  686. CRYPTO_Q_UNLOCK(); //DAVIDM should this be a driver lock
  687. return err;
  688. }
  689. /*
  690. * Add a crypto request to a queue, to be processed by the kernel thread.
  691. */
  692. int
  693. crypto_dispatch(struct cryptop *crp)
  694. {
  695. struct cryptocap *cap;
  696. int result = -1;
  697. unsigned long q_flags;
  698. dprintk("%s()\n", __FUNCTION__);
  699. cryptostats.cs_ops++;
  700. CRYPTO_Q_LOCK();
  701. if (crypto_q_cnt >= crypto_q_max) {
  702. CRYPTO_Q_UNLOCK();
  703. cryptostats.cs_drops++;
  704. return ENOMEM;
  705. }
  706. crypto_q_cnt++;
  707. /*
  708. * Caller marked the request to be processed immediately; dispatch
  709. * it directly to the driver unless the driver is currently blocked.
  710. */
  711. if ((crp->crp_flags & CRYPTO_F_BATCH) == 0) {
  712. int hid = CRYPTO_SESID2HID(crp->crp_sid);
  713. cap = crypto_checkdriver(hid);
  714. /* Driver cannot disappear when there is an active session. */
  715. KASSERT(cap != NULL, ("%s: Driver disappeared.", __func__));
  716. if (!cap->cc_qblocked) {
  717. crypto_all_qblocked = 0;
  718. crypto_drivers[hid].cc_qblocked = 1;
  719. CRYPTO_Q_UNLOCK();
  720. result = crypto_invoke(cap, crp, 0);
  721. CRYPTO_Q_LOCK();
  722. if (result != ERESTART)
  723. crypto_drivers[hid].cc_qblocked = 0;
  724. }
  725. }
  726. if (result == ERESTART) {
  727. /*
  728. * The driver ran out of resources, mark the
  729. * driver ``blocked'' for cryptop's and put
  730. * the request back in the queue. It would
  731. * best to put the request back where we got
  732. * it but that's hard so for now we put it
  733. * at the front. This should be ok; putting
  734. * it at the end does not work.
  735. */
  736. list_add(&crp->crp_next, &crp_q);
  737. cryptostats.cs_blocks++;
  738. } else if (result == -1) {
  739. TAILQ_INSERT_TAIL(&crp_q, crp, crp_next);
  740. }
  741. if (crp_sleep)
  742. wake_up_interruptible(&cryptoproc_wait);
  743. CRYPTO_Q_UNLOCK();
  744. return 0;
  745. }
  746. /*
  747. * Add an asymetric crypto request to a queue,
  748. * to be processed by the kernel thread.
  749. */
  750. int
  751. crypto_kdispatch(struct cryptkop *krp)
  752. {
  753. int error;
  754. unsigned long q_flags;
  755. cryptostats.cs_kops++;
  756. error = crypto_kinvoke(krp, krp->krp_crid);
  757. if (error == ERESTART) {
  758. CRYPTO_Q_LOCK();
  759. TAILQ_INSERT_TAIL(&crp_kq, krp, krp_next);
  760. if (crp_sleep)
  761. wake_up_interruptible(&cryptoproc_wait);
  762. CRYPTO_Q_UNLOCK();
  763. error = 0;
  764. }
  765. return error;
  766. }
  767. /*
  768. * Verify a driver is suitable for the specified operation.
  769. */
  770. static __inline int
  771. kdriver_suitable(const struct cryptocap *cap, const struct cryptkop *krp)
  772. {
  773. return (cap->cc_kalg[krp->krp_op] & CRYPTO_ALG_FLAG_SUPPORTED) != 0;
  774. }
  775. /*
  776. * Select a driver for an asym operation. The driver must
  777. * support the necessary algorithm. The caller can constrain
  778. * which device is selected with the flags parameter. The
  779. * algorithm we use here is pretty stupid; just use the first
  780. * driver that supports the algorithms we need. If there are
  781. * multiple suitable drivers we choose the driver with the
  782. * fewest active operations. We prefer hardware-backed
  783. * drivers to software ones when either may be used.
  784. */
  785. static struct cryptocap *
  786. crypto_select_kdriver(const struct cryptkop *krp, int flags)
  787. {
  788. struct cryptocap *cap, *best, *blocked;
  789. int match, hid;
  790. CRYPTO_DRIVER_ASSERT();
  791. /*
  792. * Look first for hardware crypto devices if permitted.
  793. */
  794. if (flags & CRYPTOCAP_F_HARDWARE)
  795. match = CRYPTOCAP_F_HARDWARE;
  796. else
  797. match = CRYPTOCAP_F_SOFTWARE;
  798. best = NULL;
  799. blocked = NULL;
  800. again:
  801. for (hid = 0; hid < crypto_drivers_num; hid++) {
  802. cap = &crypto_drivers[hid];
  803. /*
  804. * If it's not initialized, is in the process of
  805. * going away, or is not appropriate (hardware
  806. * or software based on match), then skip.
  807. */
  808. if (cap->cc_dev == NULL ||
  809. (cap->cc_flags & CRYPTOCAP_F_CLEANUP) ||
  810. (cap->cc_flags & match) == 0)
  811. continue;
  812. /* verify all the algorithms are supported. */
  813. if (kdriver_suitable(cap, krp)) {
  814. if (best == NULL ||
  815. cap->cc_koperations < best->cc_koperations)
  816. best = cap;
  817. }
  818. }
  819. if (best != NULL)
  820. return best;
  821. if (match == CRYPTOCAP_F_HARDWARE && (flags & CRYPTOCAP_F_SOFTWARE)) {
  822. /* sort of an Algol 68-style for loop */
  823. match = CRYPTOCAP_F_SOFTWARE;
  824. goto again;
  825. }
  826. return best;
  827. }
  828. /*
  829. * Dispatch an assymetric crypto request.
  830. */
  831. static int
  832. crypto_kinvoke(struct cryptkop *krp, int crid)
  833. {
  834. struct cryptocap *cap = NULL;
  835. int error;
  836. unsigned long d_flags;
  837. KASSERT(krp != NULL, ("%s: krp == NULL", __func__));
  838. KASSERT(krp->krp_callback != NULL,
  839. ("%s: krp->crp_callback == NULL", __func__));
  840. CRYPTO_DRIVER_LOCK();
  841. if ((crid & (CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE)) == 0) {
  842. cap = crypto_checkdriver(crid);
  843. if (cap != NULL) {
  844. /*
  845. * Driver present, it must support the necessary
  846. * algorithm and, if s/w drivers are excluded,
  847. * it must be registered as hardware-backed.
  848. */
  849. if (!kdriver_suitable(cap, krp) ||
  850. (!crypto_devallowsoft &&
  851. (cap->cc_flags & CRYPTOCAP_F_HARDWARE) == 0))
  852. cap = NULL;
  853. }
  854. } else {
  855. /*
  856. * No requested driver; select based on crid flags.
  857. */
  858. if (!crypto_devallowsoft) /* NB: disallow s/w drivers */
  859. crid &= ~CRYPTOCAP_F_SOFTWARE;
  860. cap = crypto_select_kdriver(krp, crid);
  861. }
  862. if (cap != NULL && !cap->cc_kqblocked) {
  863. krp->krp_hid = cap - crypto_drivers;
  864. cap->cc_koperations++;
  865. CRYPTO_DRIVER_UNLOCK();
  866. error = CRYPTODEV_KPROCESS(cap->cc_dev, krp, 0);
  867. CRYPTO_DRIVER_LOCK();
  868. if (error == ERESTART) {
  869. cap->cc_koperations--;
  870. CRYPTO_DRIVER_UNLOCK();
  871. return (error);
  872. }
  873. /* return the actual device used */
  874. krp->krp_crid = krp->krp_hid;
  875. } else {
  876. /*
  877. * NB: cap is !NULL if device is blocked; in
  878. * that case return ERESTART so the operation
  879. * is resubmitted if possible.
  880. */
  881. error = (cap == NULL) ? ENODEV : ERESTART;
  882. }
  883. CRYPTO_DRIVER_UNLOCK();
  884. if (error) {
  885. krp->krp_status = error;
  886. crypto_kdone(krp);
  887. }
  888. return 0;
  889. }
  890. /*
  891. * Dispatch a crypto request to the appropriate crypto devices.
  892. */
  893. static int
  894. crypto_invoke(struct cryptocap *cap, struct cryptop *crp, int hint)
  895. {
  896. KASSERT(crp != NULL, ("%s: crp == NULL", __func__));
  897. KASSERT(crp->crp_callback != NULL,
  898. ("%s: crp->crp_callback == NULL", __func__));
  899. KASSERT(crp->crp_desc != NULL, ("%s: crp->crp_desc == NULL", __func__));
  900. dprintk("%s()\n", __FUNCTION__);
  901. #ifdef CRYPTO_TIMING
  902. if (crypto_timing)
  903. crypto_tstat(&cryptostats.cs_invoke, &crp->crp_tstamp);
  904. #endif
  905. if (cap->cc_flags & CRYPTOCAP_F_CLEANUP) {
  906. struct cryptodesc *crd;
  907. u_int64_t nid;
  908. /*
  909. * Driver has unregistered; migrate the session and return
  910. * an error to the caller so they'll resubmit the op.
  911. *
  912. * XXX: What if there are more already queued requests for this
  913. * session?
  914. */
  915. crypto_freesession(crp->crp_sid);
  916. for (crd = crp->crp_desc; crd->crd_next; crd = crd->crd_next)
  917. crd->CRD_INI.cri_next = &(crd->crd_next->CRD_INI);
  918. /* XXX propagate flags from initial session? */
  919. if (crypto_newsession(&nid, &(crp->crp_desc->CRD_INI),
  920. CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE) == 0)
  921. crp->crp_sid = nid;
  922. crp->crp_etype = EAGAIN;
  923. crypto_done(crp);
  924. return 0;
  925. } else {
  926. /*
  927. * Invoke the driver to process the request.
  928. */
  929. return CRYPTODEV_PROCESS(cap->cc_dev, crp, hint);
  930. }
  931. }
  932. /*
  933. * Release a set of crypto descriptors.
  934. */
  935. void
  936. crypto_freereq(struct cryptop *crp)
  937. {
  938. struct cryptodesc *crd;
  939. if (crp == NULL)
  940. return;
  941. #ifdef DIAGNOSTIC
  942. {
  943. struct cryptop *crp2;
  944. unsigned long q_flags;
  945. CRYPTO_Q_LOCK();
  946. TAILQ_FOREACH(crp2, &crp_q, crp_next) {
  947. KASSERT(crp2 != crp,
  948. ("Freeing cryptop from the crypto queue (%p).",
  949. crp));
  950. }
  951. CRYPTO_Q_UNLOCK();
  952. CRYPTO_RETQ_LOCK();
  953. TAILQ_FOREACH(crp2, &crp_ret_q, crp_next) {
  954. KASSERT(crp2 != crp,
  955. ("Freeing cryptop from the return queue (%p).",
  956. crp));
  957. }
  958. CRYPTO_RETQ_UNLOCK();
  959. }
  960. #endif
  961. while ((crd = crp->crp_desc) != NULL) {
  962. crp->crp_desc = crd->crd_next;
  963. kmem_cache_free(cryptodesc_zone, crd);
  964. }
  965. kmem_cache_free(cryptop_zone, crp);
  966. }
  967. /*
  968. * Acquire a set of crypto descriptors.
  969. */
  970. struct cryptop *
  971. crypto_getreq(int num)
  972. {
  973. struct cryptodesc *crd;
  974. struct cryptop *crp;
  975. crp = kmem_cache_alloc(cryptop_zone, SLAB_ATOMIC);
  976. if (crp != NULL) {
  977. memset(crp, 0, sizeof(*crp));
  978. INIT_LIST_HEAD(&crp->crp_next);
  979. init_waitqueue_head(&crp->crp_waitq);
  980. while (num--) {
  981. crd = kmem_cache_alloc(cryptodesc_zone, SLAB_ATOMIC);
  982. if (crd == NULL) {
  983. crypto_freereq(crp);
  984. return NULL;
  985. }
  986. memset(crd, 0, sizeof(*crd));
  987. crd->crd_next = crp->crp_desc;
  988. crp->crp_desc = crd;
  989. }
  990. }
  991. return crp;
  992. }
  993. /*
  994. * Invoke the callback on behalf of the driver.
  995. */
  996. void
  997. crypto_done(struct cryptop *crp)
  998. {
  999. unsigned long q_flags;
  1000. dprintk("%s()\n", __FUNCTION__);
  1001. if ((crp->crp_flags & CRYPTO_F_DONE) == 0) {
  1002. crp->crp_flags |= CRYPTO_F_DONE;
  1003. CRYPTO_Q_LOCK();
  1004. crypto_q_cnt--;
  1005. CRYPTO_Q_UNLOCK();
  1006. } else
  1007. printk("crypto: crypto_done op already done, flags 0x%x",
  1008. crp->crp_flags);
  1009. if (crp->crp_etype != 0)
  1010. cryptostats.cs_errs++;
  1011. /*
  1012. * CBIMM means unconditionally do the callback immediately;
  1013. * CBIFSYNC means do the callback immediately only if the
  1014. * operation was done synchronously. Both are used to avoid
  1015. * doing extraneous context switches; the latter is mostly
  1016. * used with the software crypto driver.
  1017. */
  1018. if ((crp->crp_flags & CRYPTO_F_CBIMM) ||
  1019. ((crp->crp_flags & CRYPTO_F_CBIFSYNC) &&
  1020. (CRYPTO_SESID2CAPS(crp->crp_sid) & CRYPTOCAP_F_SYNC))) {
  1021. /*
  1022. * Do the callback directly. This is ok when the
  1023. * callback routine does very little (e.g. the
  1024. * /dev/crypto callback method just does a wakeup).
  1025. */
  1026. crp->crp_callback(crp);
  1027. } else {
  1028. unsigned long r_flags;
  1029. /*
  1030. * Normal case; queue the callback for the thread.
  1031. */
  1032. CRYPTO_RETQ_LOCK();
  1033. if (CRYPTO_RETQ_EMPTY())
  1034. wake_up_interruptible(&cryptoretproc_wait);/* shared wait channel */
  1035. TAILQ_INSERT_TAIL(&crp_ret_q, crp, crp_next);
  1036. CRYPTO_RETQ_UNLOCK();
  1037. }
  1038. }
  1039. /*
  1040. * Invoke the callback on behalf of the driver.
  1041. */
  1042. void
  1043. crypto_kdone(struct cryptkop *krp)
  1044. {
  1045. struct cryptocap *cap;
  1046. unsigned long d_flags;
  1047. if ((krp->krp_flags & CRYPTO_KF_DONE) != 0)
  1048. printk("crypto: crypto_kdone op already done, flags 0x%x",
  1049. krp->krp_flags);
  1050. krp->krp_flags |= CRYPTO_KF_DONE;
  1051. if (krp->krp_status != 0)
  1052. cryptostats.cs_kerrs++;
  1053. CRYPTO_DRIVER_LOCK();
  1054. /* XXX: What if driver is loaded in the meantime? */
  1055. if (krp->krp_hid < crypto_drivers_num) {
  1056. cap = &crypto_drivers[krp->krp_hid];
  1057. cap->cc_koperations--;
  1058. KASSERT(cap->cc_koperations >= 0, ("cc_koperations < 0"));
  1059. if (cap->cc_flags & CRYPTOCAP_F_CLEANUP)
  1060. crypto_remove(cap);
  1061. }
  1062. CRYPTO_DRIVER_UNLOCK();
  1063. /*
  1064. * CBIMM means unconditionally do the callback immediately;
  1065. * This is used to avoid doing extraneous context switches
  1066. */
  1067. if ((krp->krp_flags & CRYPTO_KF_CBIMM)) {
  1068. /*
  1069. * Do the callback directly. This is ok when the
  1070. * callback routine does very little (e.g. the
  1071. * /dev/crypto callback method just does a wakeup).
  1072. */
  1073. krp->krp_callback(krp);
  1074. } else {
  1075. unsigned long r_flags;
  1076. /*
  1077. * Normal case; queue the callback for the thread.
  1078. */
  1079. CRYPTO_RETQ_LOCK();
  1080. if (CRYPTO_RETQ_EMPTY())
  1081. wake_up_interruptible(&cryptoretproc_wait);/* shared wait channel */
  1082. TAILQ_INSERT_TAIL(&crp_ret_kq, krp, krp_next);
  1083. CRYPTO_RETQ_UNLOCK();
  1084. }
  1085. }
  1086. int
  1087. crypto_getfeat(int *featp)
  1088. {
  1089. int hid, kalg, feat = 0;
  1090. unsigned long d_flags;
  1091. CRYPTO_DRIVER_LOCK();
  1092. for (hid = 0; hid < crypto_drivers_num; hid++) {
  1093. const struct cryptocap *cap = &crypto_drivers[hid];
  1094. if ((cap->cc_flags & CRYPTOCAP_F_SOFTWARE) &&
  1095. !crypto_devallowsoft) {
  1096. continue;
  1097. }
  1098. for (kalg = 0; kalg < CRK_ALGORITHM_MAX; kalg++)
  1099. if (cap->cc_kalg[kalg] & CRYPTO_ALG_FLAG_SUPPORTED)
  1100. feat |= 1 << kalg;
  1101. }
  1102. CRYPTO_DRIVER_UNLOCK();
  1103. *featp = feat;
  1104. return (0);
  1105. }
  1106. /*
  1107. * Crypto thread, dispatches crypto requests.
  1108. */
  1109. static int
  1110. crypto_proc(void *arg)
  1111. {
  1112. struct cryptop *crp, *submit;
  1113. struct cryptkop *krp, *krpp;
  1114. struct cryptocap *cap;
  1115. u_int32_t hid;
  1116. int result, hint;
  1117. unsigned long q_flags;
  1118. ocf_daemonize("crypto");
  1119. CRYPTO_Q_LOCK();
  1120. for (;;) {
  1121. /*
  1122. * we need to make sure we don't get into a busy loop with nothing
  1123. * to do, the two crypto_all_*blocked vars help us find out when
  1124. * we are all full and can do nothing on any driver or Q. If so we
  1125. * wait for an unblock.
  1126. */
  1127. crypto_all_qblocked = !list_empty(&crp_q);
  1128. /*
  1129. * Find the first element in the queue that can be
  1130. * processed and look-ahead to see if multiple ops
  1131. * are ready for the same driver.
  1132. */
  1133. submit = NULL;
  1134. hint = 0;
  1135. list_for_each_entry(crp, &crp_q, crp_next) {
  1136. hid = CRYPTO_SESID2HID(crp->crp_sid);
  1137. cap = crypto_checkdriver(hid);
  1138. /*
  1139. * Driver cannot disappear when there is an active
  1140. * session.
  1141. */
  1142. KASSERT(cap != NULL, ("%s:%u Driver disappeared.",
  1143. __func__, __LINE__));
  1144. if (cap == NULL || cap->cc_dev == NULL) {
  1145. /* Op needs to be migrated, process it. */
  1146. if (submit == NULL)
  1147. submit = crp;
  1148. break;
  1149. }
  1150. if (!cap->cc_qblocked) {
  1151. if (submit != NULL) {
  1152. /*
  1153. * We stop on finding another op,
  1154. * regardless whether its for the same
  1155. * driver or not. We could keep
  1156. * searching the queue but it might be
  1157. * better to just use a per-driver
  1158. * queue instead.
  1159. */
  1160. if (CRYPTO_SESID2HID(submit->crp_sid) == hid)
  1161. hint = CRYPTO_HINT_MORE;
  1162. break;
  1163. } else {
  1164. submit = crp;
  1165. if ((submit->crp_flags & CRYPTO_F_BATCH) == 0)
  1166. break;
  1167. /* keep scanning for more are q'd */
  1168. }
  1169. }
  1170. }
  1171. if (submit != NULL) {
  1172. hid = CRYPTO_SESID2HID(submit->crp_sid);
  1173. crypto_all_qblocked = 0;
  1174. list_del(&submit->crp_next);
  1175. crypto_drivers[hid].cc_qblocked = 1;
  1176. cap = crypto_checkdriver(hid);
  1177. CRYPTO_Q_UNLOCK();
  1178. KASSERT(cap != NULL, ("%s:%u Driver disappeared.",
  1179. __func__, __LINE__));
  1180. result = crypto_invoke(cap, submit, hint);
  1181. CRYPTO_Q_LOCK();
  1182. if (result == ERESTART) {
  1183. /*
  1184. * The driver ran out of resources, mark the
  1185. * driver ``blocked'' for cryptop's and put
  1186. * the request back in the queue. It would
  1187. * best to put the request back where we got
  1188. * it but that's hard so for now we put it
  1189. * at the front. This should be ok; putting
  1190. * it at the end does not work.
  1191. */
  1192. /* XXX validate sid again? */
  1193. list_add(&submit->crp_next, &crp_q);
  1194. cryptostats.cs_blocks++;
  1195. } else
  1196. crypto_drivers[hid].cc_qblocked=0;
  1197. }
  1198. crypto_all_kqblocked = !list_empty(&crp_kq);
  1199. /* As above, but for key ops */
  1200. krp = NULL;
  1201. list_for_each_entry(krpp, &crp_kq, krp_next) {
  1202. cap = crypto_checkdriver(krpp->krp_hid);
  1203. if (cap == NULL || cap->cc_dev == NULL) {
  1204. /*
  1205. * Operation needs to be migrated, invalidate
  1206. * the assigned device so it will reselect a
  1207. * new one below. Propagate the original
  1208. * crid selection flags if supplied.
  1209. */
  1210. krp->krp_hid = krp->krp_crid &
  1211. (CRYPTOCAP_F_SOFTWARE|CRYPTOCAP_F_HARDWARE);
  1212. if (krp->krp_hid == 0)
  1213. krp->krp_hid =
  1214. CRYPTOCAP_F_SOFTWARE|CRYPTOCAP_F_HARDWARE;
  1215. break;
  1216. }
  1217. if (!cap->cc_kqblocked) {
  1218. krp = krpp;
  1219. break;
  1220. }
  1221. }
  1222. if (krp != NULL) {
  1223. crypto_all_kqblocked = 0;
  1224. list_del(&krp->krp_next);
  1225. crypto_drivers[krp->krp_hid].cc_kqblocked = 1;
  1226. CRYPTO_Q_UNLOCK();
  1227. result = crypto_kinvoke(krp, krp->krp_hid);
  1228. CRYPTO_Q_LOCK();
  1229. if (result == ERESTART) {
  1230. /*
  1231. * The driver ran out of resources, mark the
  1232. * driver ``blocked'' for cryptkop's and put
  1233. * the request back in the queue. It would
  1234. * best to put the request back where we got
  1235. * it but that's hard so for now we put it
  1236. * at the front. This should be ok; putting
  1237. * it at the end does not work.
  1238. */
  1239. /* XXX validate sid again? */
  1240. list_add(&krp->krp_next, &crp_kq);
  1241. cryptostats.cs_kblocks++;
  1242. } else
  1243. crypto_drivers[krp->krp_hid].cc_kqblocked = 0;
  1244. }
  1245. if (submit == NULL && krp == NULL) {
  1246. /*
  1247. * Nothing more to be processed. Sleep until we're
  1248. * woken because there are more ops to process.
  1249. * This happens either by submission or by a driver
  1250. * becoming unblocked and notifying us through
  1251. * crypto_unblock. Note that when we wakeup we
  1252. * start processing each queue again from the
  1253. * front. It's not clear that it's important to
  1254. * preserve this ordering since ops may finish
  1255. * out of order if dispatched to different devices
  1256. * and some become blocked while others do not.
  1257. */
  1258. dprintk("%s - sleeping (qe=%d qb=%d kqe=%d kqb=%d)\n",
  1259. __FUNCTION__,
  1260. list_empty(&crp_q), crypto_all_qblocked,
  1261. list_empty(&crp_kq), crypto_all_kqblocked);
  1262. CRYPTO_Q_UNLOCK();
  1263. crp_sleep = 1;
  1264. wait_event_interruptible(cryptoproc_wait,
  1265. !(list_empty(&crp_q) || crypto_all_qblocked) ||
  1266. !(list_empty(&crp_kq) || crypto_all_kqblocked) ||
  1267. cryptoproc == (pid_t) -1);
  1268. crp_sleep = 0;
  1269. if (signal_pending (current)) {
  1270. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
  1271. spin_lock_irq(&current->sigmask_lock);
  1272. #endif
  1273. flush_signals(current);
  1274. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
  1275. spin_unlock_irq(&current->sigmask_lock);
  1276. #endif
  1277. }
  1278. CRYPTO_Q_LOCK();
  1279. dprintk("%s - awake\n", __FUNCTION__);
  1280. if (cryptoproc == (pid_t) -1)
  1281. break;
  1282. cryptostats.cs_intrs++;
  1283. }
  1284. }
  1285. CRYPTO_Q_UNLOCK();
  1286. complete_and_exit(&cryptoproc_exited, 0);
  1287. }
  1288. /*
  1289. * Crypto returns thread, does callbacks for processed crypto requests.
  1290. * Callbacks are done here, rather than in the crypto drivers, because
  1291. * callbacks typically are expensive and would slow interrupt handling.
  1292. */
  1293. static int
  1294. crypto_ret_proc(void *arg)
  1295. {
  1296. struct cryptop *crpt;
  1297. struct cryptkop *krpt;
  1298. unsigned long r_flags;
  1299. ocf_daemonize("crypto_ret");
  1300. CRYPTO_RETQ_LOCK();
  1301. for (;;) {
  1302. /* Harvest return q's for completed ops */
  1303. crpt = NULL;
  1304. if (!list_empty(&crp_ret_q))
  1305. crpt = list_entry(crp_ret_q.next, typeof(*crpt), crp_next);
  1306. if (crpt != NULL)
  1307. list_del(&crpt->crp_next);
  1308. krpt = NULL;
  1309. if (!list_empty(&crp_ret_kq))
  1310. krpt = list_entry(crp_ret_kq.next, typeof(*krpt), krp_next);
  1311. if (krpt != NULL)
  1312. list_del(&krpt->krp_next);
  1313. if (crpt != NULL || krpt != NULL) {
  1314. CRYPTO_RETQ_UNLOCK();
  1315. /*
  1316. * Run callbacks unlocked.
  1317. */
  1318. if (crpt != NULL)
  1319. crpt->crp_callback(crpt);
  1320. if (krpt != NULL)
  1321. krpt->krp_callback(krpt);
  1322. CRYPTO_RETQ_LOCK();
  1323. } else {
  1324. /*
  1325. * Nothing more to be processed. Sleep until we're
  1326. * woken because there are more returns to process.
  1327. */
  1328. dprintk("%s - sleeping\n", __FUNCTION__);
  1329. CRYPTO_RETQ_UNLOCK();
  1330. wait_event_interruptible(cryptoretproc_wait,
  1331. cryptoretproc == (pid_t) -1 ||
  1332. !list_empty(&crp_ret_q) ||
  1333. !list_empty(&crp_ret_kq));
  1334. if (signal_pending (current)) {
  1335. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
  1336. spin_lock_irq(&current->sigmask_lock);
  1337. #endif
  1338. flush_signals(current);
  1339. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
  1340. spin_unlock_irq(&current->sigmask_lock);
  1341. #endif
  1342. }
  1343. CRYPTO_RETQ_LOCK();
  1344. dprintk("%s - awake\n", __FUNCTION__);
  1345. if (cryptoretproc == (pid_t) -1) {
  1346. dprintk("%s - EXITING!\n", __FUNCTION__);
  1347. break;
  1348. }
  1349. cryptostats.cs_rets++;
  1350. }
  1351. }
  1352. CRYPTO_RETQ_UNLOCK();
  1353. complete_and_exit(&cryptoretproc_exited, 0);
  1354. }
  1355. #if 0 /* should put this into /proc or something */
  1356. static void
  1357. db_show_drivers(void)
  1358. {
  1359. int hid;
  1360. db_printf("%12s %4s %4s %8s %2s %2s\n"
  1361. , "Device"
  1362. , "Ses"
  1363. , "Kops"
  1364. , "Flags"
  1365. , "QB"
  1366. , "KB"
  1367. );
  1368. for (hid = 0; hid < crypto_drivers_num; hid++) {
  1369. const struct cryptocap *cap = &crypto_drivers[hid];
  1370. if (cap->cc_dev == NULL)
  1371. continue;
  1372. db_printf("%-12s %4u %4u %08x %2u %2u\n"
  1373. , device_get_nameunit(cap->cc_dev)
  1374. , cap->cc_sessions
  1375. , cap->cc_koperations
  1376. , cap->cc_flags
  1377. , cap->cc_qblocked
  1378. , cap->cc_kqblocked
  1379. );
  1380. }
  1381. }
  1382. DB_SHOW_COMMAND(crypto, db_show_crypto)
  1383. {
  1384. struct cryptop *crp;
  1385. db_show_drivers();
  1386. db_printf("\n");
  1387. db_printf("%4s %8s %4s %4s %4s %4s %8s %8s\n",
  1388. "HID", "Caps", "Ilen", "Olen", "Etype", "Flags",
  1389. "Desc", "Callback");
  1390. TAILQ_FOREACH(crp, &crp_q, crp_next) {
  1391. db_printf("%4u %08x %4u %4u %4u %04x %8p %8p\n"
  1392. , (int) CRYPTO_SESID2HID(crp->crp_sid)
  1393. , (int) CRYPTO_SESID2CAPS(crp->crp_sid)
  1394. , crp->crp_ilen, crp->crp_olen
  1395. , crp->crp_etype
  1396. , crp->crp_flags
  1397. , crp->crp_desc
  1398. , crp->crp_callback
  1399. );
  1400. }
  1401. if (!TAILQ_EMPTY(&crp_ret_q)) {
  1402. db_printf("\n%4s %4s %4s %8s\n",
  1403. "HID", "Etype", "Flags", "Callback");
  1404. TAILQ_FOREACH(crp, &crp_ret_q, crp_next) {
  1405. db_printf("%4u %4u %04x %8p\n"
  1406. , (int) CRYPTO_SESID2HID(crp->crp_sid)
  1407. , crp->crp_etype
  1408. , crp->crp_flags
  1409. , crp->crp_callback
  1410. );
  1411. }
  1412. }
  1413. }
  1414. DB_SHOW_COMMAND(kcrypto, db_show_kcrypto)
  1415. {
  1416. struct cryptkop *krp;
  1417. db_show_drivers();
  1418. db_printf("\n");
  1419. db_printf("%4s %5s %4s %4s %8s %4s %8s\n",
  1420. "Op", "Status", "#IP", "#OP", "CRID", "HID", "Callback");
  1421. TAILQ_FOREACH(krp, &crp_kq, krp_next) {
  1422. db_printf("%4u %5u %4u %4u %08x %4u %8p\n"
  1423. , krp->krp_op
  1424. , krp->krp_status
  1425. , krp->krp_iparams, krp->krp_oparams
  1426. , krp->krp_crid, krp->krp_hid
  1427. , krp->krp_callback
  1428. );
  1429. }
  1430. if (!TAILQ_EMPTY(&crp_ret_q)) {
  1431. db_printf("%4s %5s %8s %4s %8s\n",
  1432. "Op", "Status", "CRID", "HID", "Callback");
  1433. TAILQ_FOREACH(krp, &crp_ret_kq, krp_next) {
  1434. db_printf("%4u %5u %08x %4u %8p\n"
  1435. , krp->krp_op
  1436. , krp->krp_status
  1437. , krp->krp_crid, krp->krp_hid
  1438. , krp->krp_callback
  1439. );
  1440. }
  1441. }
  1442. }
  1443. #endif
  1444. static int
  1445. crypto_init(void)
  1446. {
  1447. int error;
  1448. dprintk("%s(0x%x)\n", __FUNCTION__, (int) crypto_init);
  1449. if (crypto_initted)
  1450. return 0;
  1451. crypto_initted = 1;
  1452. spin_lock_init(&crypto_drivers_lock);
  1453. spin_lock_init(&crypto_q_lock);
  1454. spin_lock_init(&crypto_ret_q_lock);
  1455. cryptop_zone = kmem_cache_create("cryptop", sizeof(struct cryptop),
  1456. 0, SLAB_HWCACHE_ALIGN, NULL
  1457. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
  1458. , NULL
  1459. #endif
  1460. );
  1461. cryptodesc_zone = kmem_cache_create("cryptodesc", sizeof(struct cryptodesc),
  1462. 0, SLAB_HWCACHE_ALIGN, NULL
  1463. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
  1464. , NULL
  1465. #endif
  1466. );
  1467. if (cryptodesc_zone == NULL || cryptop_zone == NULL) {
  1468. printk("crypto: crypto_init cannot setup crypto zones\n");
  1469. error = ENOMEM;
  1470. goto bad;
  1471. }
  1472. crypto_drivers_num = CRYPTO_DRIVERS_INITIAL;
  1473. crypto_drivers = kmalloc(crypto_drivers_num * sizeof(struct cryptocap),
  1474. GFP_KERNEL);
  1475. if (crypto_drivers == NULL) {
  1476. printk("crypto: crypto_init cannot setup crypto drivers\n");
  1477. error = ENOMEM;
  1478. goto bad;
  1479. }
  1480. memset(crypto_drivers, 0, crypto_drivers_num * sizeof(struct cryptocap));
  1481. init_completion(&cryptoproc_exited);
  1482. init_completion(&cryptoretproc_exited);
  1483. cryptoproc = 0; /* to avoid race condition where proc runs first */
  1484. cryptoproc = kernel_thread(crypto_proc, NULL, CLONE_FS|CLONE_FILES);
  1485. if (cryptoproc < 0) {
  1486. error = cryptoproc;
  1487. printk("crypto: crypto_init cannot start crypto thread; error %d",
  1488. error);
  1489. goto bad;
  1490. }
  1491. cryptoretproc = 0; /* to avoid race condition where proc runs first */
  1492. cryptoretproc = kernel_thread(crypto_ret_proc, NULL, CLONE_FS|CLONE_FILES);
  1493. if (cryptoretproc < 0) {
  1494. error = cryptoretproc;
  1495. printk("crypto: crypto_init cannot start cryptoret thread; error %d",
  1496. error);
  1497. goto bad;
  1498. }
  1499. return 0;
  1500. bad:
  1501. crypto_exit();
  1502. return error;
  1503. }
  1504. static void
  1505. crypto_exit(void)
  1506. {
  1507. pid_t p;
  1508. unsigned long d_flags;
  1509. dprintk("%s()\n", __FUNCTION__);
  1510. /*
  1511. * Terminate any crypto threads.
  1512. */
  1513. CRYPTO_DRIVER_LOCK();
  1514. p = cryptoproc;
  1515. cryptoproc = (pid_t) -1;
  1516. kill_proc(p, SIGTERM, 1);
  1517. wake_up_interruptible(&cryptoproc_wait);
  1518. CRYPTO_DRIVER_UNLOCK();
  1519. wait_for_completion(&cryptoproc_exited);
  1520. CRYPTO_DRIVER_LOCK();
  1521. p = cryptoretproc;
  1522. cryptoretproc = (pid_t) -1;
  1523. kill_proc(p, SIGTERM, 1);
  1524. wake_up_interruptible(&cryptoretproc_wait);
  1525. CRYPTO_DRIVER_UNLOCK();
  1526. wait_for_completion(&cryptoretproc_exited);
  1527. /* XXX flush queues??? */
  1528. /*
  1529. * Reclaim dynamically allocated resources.
  1530. */
  1531. if (crypto_drivers != NULL)
  1532. kfree(crypto_drivers);
  1533. if (cryptodesc_zone != NULL)
  1534. kmem_cache_destroy(cryptodesc_zone);
  1535. if (cryptop_zone != NULL)
  1536. kmem_cache_destroy(cryptop_zone);
  1537. }
  1538. EXPORT_SYMBOL(crypto_newsession);
  1539. EXPORT_SYMBOL(crypto_freesession);
  1540. EXPORT_SYMBOL(crypto_get_driverid);
  1541. EXPORT_SYMBOL(crypto_kregister);
  1542. EXPORT_SYMBOL(crypto_register);
  1543. EXPORT_SYMBOL(crypto_unregister);
  1544. EXPORT_SYMBOL(crypto_unregister_all);
  1545. EXPORT_SYMBOL(crypto_unblock);
  1546. EXPORT_SYMBOL(crypto_dispatch);
  1547. EXPORT_SYMBOL(crypto_kdispatch);
  1548. EXPORT_SYMBOL(crypto_freereq);
  1549. EXPORT_SYMBOL(crypto_getreq);
  1550. EXPORT_SYMBOL(crypto_done);
  1551. EXPORT_SYMBOL(crypto_kdone);
  1552. EXPORT_SYMBOL(crypto_getfeat);
  1553. EXPORT_SYMBOL(crypto_userasymcrypto);
  1554. EXPORT_SYMBOL(crypto_getcaps);
  1555. EXPORT_SYMBOL(crypto_find_driver);
  1556. EXPORT_SYMBOL(crypto_find_device_byhid);
  1557. module_init(crypto_init);
  1558. module_exit(crypto_exit);
  1559. MODULE_LICENSE("BSD");
  1560. MODULE_AUTHOR("David McCullough <[email protected]>");
  1561. MODULE_DESCRIPTION("OCF (OpenBSD Cryptographic Framework)");