diff options
| author | Conrad Meyer <cem@FreeBSD.org> | 2018-07-18 00:56:25 +0000 |
|---|---|---|
| committer | Conrad Meyer <cem@FreeBSD.org> | 2018-07-18 00:56:25 +0000 |
| commit | 1b0909d51a8aa8b5ec5a61c2dc1a69642976a732 (patch) | |
| tree | b9c915e7d2ca9312b6f83308f05040a78875b6d7 /sys/dev/sec | |
| parent | 38b42191fb8cbdf348508c72b504e0dc3da1b53c (diff) | |
OpenCrypto: Convert sessions to opaque handles instead of integers
Track session objects in the framework, and pass handles between the
framework (OCF), consumers, and drivers. Avoid redundancy and complexity in
individual drivers by allocating session memory in the framework and
providing it to drivers in ::newsession().
Session handles are no longer integers with information encoded in various
high bits. Use of the CRYPTO_SESID2FOO() macros should be replaced with the
appropriate crypto_ses2foo() function on the opaque session handle.
Convert OCF drivers (in particular, cryptosoft, as well as myriad others) to
the opaque handle interface. Discard existing session tracking as much as
possible (quick pass). There may be additional code ripe for deletion.
Convert OCF consumers (ipsec, geom_eli, krb5, cryptodev) to handle-style
interface. The conversion is largely mechnical.
The change is documented in crypto.9.
Inspired by
https://lists.freebsd.org/pipermail/freebsd-arch/2018-January/018835.html .
No objection from: ae (ipsec portion)
Reported by: jhb
Notes
svn path=/head/; revision=336439
Diffstat (limited to 'sys/dev/sec')
| -rw-r--r-- | sys/dev/sec/sec.c | 107 | ||||
| -rw-r--r-- | sys/dev/sec/sec.h | 4 |
2 files changed, 7 insertions, 104 deletions
diff --git a/sys/dev/sec/sec.c b/sys/dev/sec/sec.c index 3d01bd99e4b7..76f808757845 100644 --- a/sys/dev/sec/sec.c +++ b/sys/dev/sec/sec.c @@ -85,10 +85,8 @@ static int sec_make_pointer(struct sec_softc *sc, struct sec_desc *desc, u_int n, void *data, bus_size_t doffset, bus_size_t dsize, int dtype); static int sec_make_pointer_direct(struct sec_softc *sc, struct sec_desc *desc, u_int n, bus_addr_t data, bus_size_t dsize); -static int sec_alloc_session(struct sec_softc *sc); -static int sec_newsession(device_t dev, u_int32_t *sidp, +static int sec_newsession(device_t dev, crypto_session_t cses, struct cryptoini *cri); -static int sec_freesession(device_t dev, uint64_t tid); static int sec_process(device_t dev, struct cryptop *crp, int hint); static int sec_split_cri(struct cryptoini *cri, struct cryptoini **enc, struct cryptoini **mac); @@ -101,7 +99,6 @@ static int sec_build_common_s_desc(struct sec_softc *sc, struct sec_desc *desc, struct sec_session *ses, struct cryptop *crp, struct cryptodesc *enc, struct cryptodesc *mac, int buftype); -static struct sec_session *sec_get_session(struct sec_softc *sc, u_int sid); static struct sec_desc *sec_find_desc(struct sec_softc *sc, bus_addr_t paddr); /* AESU */ @@ -140,7 +137,6 @@ static device_method_t sec_methods[] = { /* Crypto methods */ DEVMETHOD(cryptodev_newsession, sec_newsession), - DEVMETHOD(cryptodev_freesession,sec_freesession), DEVMETHOD(cryptodev_process, sec_process), DEVMETHOD_END @@ -180,15 +176,6 @@ sec_sync_dma_mem(struct sec_dma_mem *dma_mem, bus_dmasync_op_t op) bus_dmamap_sync(dma_mem->dma_tag, dma_mem->dma_map, op); } -static inline void -sec_free_session(struct sec_softc *sc, struct sec_session *ses) -{ - - SEC_LOCK(sc, sessions); - ses->ss_used = 0; - SEC_UNLOCK(sc, sessions); -} - static inline void * sec_get_pointer_data(struct sec_desc *desc, u_int n) { @@ -258,7 +245,8 @@ sec_attach(device_t dev) sc->sc_blocked = 0; sc->sc_shutdown = 0; - sc->sc_cid = crypto_get_driverid(dev, CRYPTOCAP_F_HARDWARE); + sc->sc_cid = crypto_get_driverid(dev, sizeof(struct sec_session), + CRYPTOCAP_F_HARDWARE); if (sc->sc_cid < 0) { device_printf(dev, "could not get crypto driver ID!\n"); return (ENXIO); @@ -269,8 +257,6 @@ sec_attach(device_t dev) "SEC Controller lock", MTX_DEF); mtx_init(&sc->sc_descriptors_lock, device_get_nameunit(dev), "SEC Descriptors lock", MTX_DEF); - mtx_init(&sc->sc_sessions_lock, device_get_nameunit(dev), - "SEC Sessions lock", MTX_DEF); /* Allocate I/O memory for SEC registers */ sc->sc_rrid = 0; @@ -411,7 +397,6 @@ fail2: fail1: mtx_destroy(&sc->sc_controller_lock); mtx_destroy(&sc->sc_descriptors_lock); - mtx_destroy(&sc->sc_sessions_lock); return (ENXIO); } @@ -482,7 +467,6 @@ sec_detach(device_t dev) mtx_destroy(&sc->sc_controller_lock); mtx_destroy(&sc->sc_descriptors_lock); - mtx_destroy(&sc->sc_sessions_lock); return (0); } @@ -1235,53 +1219,7 @@ sec_split_crp(struct cryptop *crp, struct cryptodesc **enc, } static int -sec_alloc_session(struct sec_softc *sc) -{ - struct sec_session *ses = NULL; - int sid = -1; - u_int i; - - SEC_LOCK(sc, sessions); - - for (i = 0; i < SEC_MAX_SESSIONS; i++) { - if (sc->sc_sessions[i].ss_used == 0) { - ses = &(sc->sc_sessions[i]); - ses->ss_used = 1; - ses->ss_ivlen = 0; - ses->ss_klen = 0; - ses->ss_mklen = 0; - sid = i; - break; - } - } - - SEC_UNLOCK(sc, sessions); - - return (sid); -} - -static struct sec_session * -sec_get_session(struct sec_softc *sc, u_int sid) -{ - struct sec_session *ses; - - if (sid >= SEC_MAX_SESSIONS) - return (NULL); - - SEC_LOCK(sc, sessions); - - ses = &(sc->sc_sessions[sid]); - - if (ses->ss_used == 0) - ses = NULL; - - SEC_UNLOCK(sc, sessions); - - return (ses); -} - -static int -sec_newsession(device_t dev, u_int32_t *sidp, struct cryptoini *cri) +sec_newsession(device_t dev, crypto_session_t cses, struct cryptoini *cri) { struct sec_softc *sc = device_get_softc(dev); struct sec_eu_methods *eu = sec_eus; @@ -1289,7 +1227,6 @@ sec_newsession(device_t dev, u_int32_t *sidp, struct cryptoini *cri) struct cryptoini *mac = NULL; struct sec_session *ses; int error = -1; - int sid; error = sec_split_cri(cri, &enc, &mac); if (error) @@ -1306,11 +1243,7 @@ sec_newsession(device_t dev, u_int32_t *sidp, struct cryptoini *cri) if (sc->sc_version < 3 && mac && mac->cri_klen > 256) return (E2BIG); - sid = sec_alloc_session(sc); - if (sid < 0) - return (ENOMEM); - - ses = sec_get_session(sc, sid); + ses = crypto_get_driver_session(cses); /* Find EU for this session */ while (eu->sem_make_desc != NULL) { @@ -1322,10 +1255,8 @@ sec_newsession(device_t dev, u_int32_t *sidp, struct cryptoini *cri) } /* If not found, return EINVAL */ - if (error < 0) { - sec_free_session(sc, ses); + if (error < 0) return (EINVAL); - } /* Save cipher key */ if (enc && enc->cri_key) { @@ -1340,28 +1271,10 @@ sec_newsession(device_t dev, u_int32_t *sidp, struct cryptoini *cri) } ses->ss_eu = eu; - *sidp = sid; - return (0); } static int -sec_freesession(device_t dev, uint64_t tid) -{ - struct sec_softc *sc = device_get_softc(dev); - struct sec_session *ses; - int error = 0; - - ses = sec_get_session(sc, CRYPTO_SESID2LID(tid)); - if (ses == NULL) - return (EINVAL); - - sec_free_session(sc, ses); - - return (error); -} - -static int sec_process(device_t dev, struct cryptop *crp, int hint) { struct sec_softc *sc = device_get_softc(dev); @@ -1370,13 +1283,7 @@ sec_process(device_t dev, struct cryptop *crp, int hint) struct sec_session *ses; int buftype, error = 0; - /* Check Session ID */ - ses = sec_get_session(sc, CRYPTO_SESID2LID(crp->crp_sid)); - if (ses == NULL) { - crp->crp_etype = EINVAL; - crypto_done(crp); - return (0); - } + ses = crypto_get_driver_session(crp->crp_session); /* Check for input length */ if (crp->crp_ilen > SEC_MAX_DMA_BLOCK_SIZE) { diff --git a/sys/dev/sec/sec.h b/sys/dev/sec/sec.h index 105b49a49e28..05b15039ad64 100644 --- a/sys/dev/sec/sec.h +++ b/sys/dev/sec/sec.h @@ -153,7 +153,6 @@ struct sec_eu_methods { }; struct sec_session { - u_int ss_used; struct sec_eu_methods *ss_eu; uint8_t ss_key[SEC_MAX_KEY_LEN]; uint8_t ss_mkey[SEC_MAX_KEY_LEN]; @@ -181,11 +180,8 @@ struct sec_softc { uint64_t sc_int_error_mask; uint64_t sc_channel_idle_mask; - struct sec_session sc_sessions[SEC_MAX_SESSIONS]; - struct mtx sc_controller_lock; struct mtx sc_descriptors_lock; - struct mtx sc_sessions_lock; struct sec_desc sc_desc[SEC_DESCRIPTORS]; u_int sc_free_desc_get_cnt; |
